Example #1
0
   void co_init()
   {
      int ret;
      void *base;

      block = sceKernelAllocMemBlockForVM("libco",
            MB_ALIGN(FOUR_KB_ALIGN(sizeof co_swap_function)));
      if (block < 0)
         return;

      /* get base address */
      ret = sceKernelGetMemBlockBase(block, &base);
      if (ret < 0)
         return;

      /* set domain to be writable by user */
      ret = sceKernelOpenVMDomain();
      if (ret < 0)
         return;

      memcpy(base, co_swap_function, sizeof co_swap_function);

      /* set domain back to read-only */
      ret = sceKernelCloseVMDomain();
      if (ret < 0)
         return;

      /* flush icache */
      ret = sceKernelSyncVMDomain(block, base,
            MB_ALIGN(FOUR_KB_ALIGN(sizeof co_swap_function)));
      if (ret < 0)
         return;

      co_swap = (void(*)(cothread_t, cothread_t))base;
   }
Example #2
0
File: main.c Project: 173210/VHL
_start(UVL_Context *ctx)
{
    void * const vhlStubTop = getVhlStubTop();
    void * const vhlPrimaryStubTop = (void *)((uintptr_t)vhlStubTop + 16);
    void * const vhlPrimaryStubBtm = (void *)((uintptr_t)vhlPrimaryStubTop + vhlPrimaryStubSize);

    const SceModuleImports * cachedImports[CACHED_IMPORTED_MODULE_NUM];
    nidTable_entry libkernelBase;
    SceModuleInfo *libkernelInfo;
    SceUID uid;
    void *p;
    int err;

    ctx->logline("Starting VHL...");

    /* It may get wrong if you change the order of nid_table_resolveVhl*
       See stub.S to know what imports will be resolved with those functions. */
    ctx->logline("Resolving VHL puts");
    nid_table_resolveVhlPuts(vhlStubTop, ctx);
    ctx->psvFlushIcache(vhlStubTop, 16);

    DEBUG_LOG_("Searching for SceLibKernel");
    if (nid_table_analyzeStub(ctx->libkernel_anchor, 0, &libkernelBase) != ANALYZE_STUB_OK) {
        DEBUG_LOG_("Failed to find the base of SceLibKernel");
        return -1;
    }

    libkernelBase.value.i = B_UNSET(libkernelBase.value.i, 0);

    libkernelInfo = nid_table_findModuleInfo(libkernelBase.value.p, KERNEL_MODULE_SIZE, "SceLibKernel");
    if (libkernelInfo == NULL) {
        DEBUG_LOG_("Failed to find the module information of SceLibKernel");
        return -1;
    }

    DEBUG_LOG_("Searching for cached imports");
    nidCacheFindCachedImports(libkernelInfo, cachedImports);

    DEBUG_LOG_("Resolving VHL primary imports");
    nid_table_resolveVhlPrimaryImports(vhlPrimaryStubTop, vhlPrimaryStubSize,
                                       libkernelInfo, cachedImports, ctx);
    ctx->psvFlushIcache(vhlPrimaryStubTop, vhlPrimaryStubSize);

    DEBUG_LOG_("Allocating memory for VHL");
    uid = sceKernelAllocMemBlock("vhlGlobals", SCE_KERNEL_MEMBLOCK_TYPE_USER_RW,
                                 FOUR_KB_ALIGN(sizeof(globals_t)), NULL);
    if (uid < 0) {
        DEBUG_LOG("Failed to allocate memory block 0x%08X", uid);
        return uid;
    }

    err = sceKernelGetMemBlockBase(uid, &p);
    if (err < 0) {
        DEBUG_LOG("Failed to retrive memory block 0x%08X", err);
        return uid;
    }

    ctx->psvUnlockMem();
    globals = p;
    ctx->psvLockMem();

    DEBUG_LOG_("Initializing table");
    nid_storage_initialize();

    DEBUG_LOG_("Searching and Adding stubs to table...");
    nid_table_addAllStubs();

    DEBUG_LOG_("Resolving VHL secondary imports");
    nid_table_resolveVhlSecondaryImports(vhlPrimaryStubBtm, vhlSecondaryStubSize,
                                         libkernelInfo, cachedImports, ctx);
    ctx->psvFlushIcache(vhlPrimaryStubBtm, vhlSecondaryStubSize);

    DEBUG_LOG_("Adding stubs to table with cache");
    if (nid_table_addNIDCacheToTable(cachedImports) < 0)
        return -1;

    DEBUG_LOG_("Adding hooks to table");
    nid_table_addAllHooks();

    //TODO find a way to free unused memory

    block_manager_initialize();  //Initialize the elf block slots

    //TODO decide how to handle plugins

    config_initialize();

    DEBUG_LOG_("Loading menu...");

    if(elf_parser_load(globals->allocatedBlocks, "pss0:/top/Documents/homebrew.self", NULL) < 0) {
        internal_printf("Load failed!");
        return -1;
    }
    puts("Load succeeded! Launching!");

    elf_parser_start(globals->allocatedBlocks, 0);

    while(1) {
        //Delay thread and check for flags and things to update every once in a while, check for exit combination
        //calls.LogLine("Menu exited! Relaunching...");
        sceKernelDelayThread(16000);  //Update stuff once every 16 ms
    }

    return 0;
}