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; }
code_pool::code_pool(uint32_t icount) : instruction_count(icount), instructions(0), next_instruction(0), flush_start(0) { printf("\n\ncode_pool icount: %i\n\n", icount); memset(labels, 0, sizeof(labels)); memset(branches, 0, sizeof(branches)); #ifdef USE_POSIX_MEMALIGN if (posix_memalign((void**)&instructions, 4096, instruction_count * 4)) { fprintf(stderr, "posix_memalign failed\n"); abort(); } #elif defined(_3DS) if(!_instructions){ _instructions = (uint32_t*)memalign(4096, instruction_count * 4); if (!_instructions) { fprintf(stderr, "memalign failed\n"); abort(); } ReprotectMemory((unsigned int*)_instructions, (instruction_count * 4) / 4096, 7); } instructions = _instructions; #elif defined(__vita__) block = sceKernelAllocMemBlockForVM("desmume_rwx_block", instruction_count * 4); if (block < 0) { fprintf(stderr, "sceKernelAllocMemBlockForVM failed\n"); abort(); } if (sceKernelGetMemBlockBase(block, (void **)&instructions) < 0) { fprintf(stderr, "sceKernelGetMemBlockBase failed\n"); abort(); } #else instructions = (uint32_t*)memalign(4096, instruction_count * 4); if (!instructions) { fprintf(stderr, "memalign failed\n"); abort(); } #endif #if !defined(_3DS) && !defined(__vita__) if (mprotect(instructions, instruction_count * 4, PROT_READ | PROT_WRITE | PROT_EXEC)) { fprintf(stderr, "mprotect failed\n"); abort(); } #endif }
void _init_vita_heap(void) { int _newlib_vm_size = 0; if (&_newlib_vm_size_user != NULL) { _newlib_vm_size = _newlib_vm_size_user; _newlib_vm_memblock = sceKernelAllocMemBlockForVM("code", _newlib_vm_size_user); if (_newlib_vm_memblock < 0){ //sceClibPrintf("sceKernelAllocMemBlockForVM failed\n"); goto failure; } }else{ _newlib_vm_memblock = 0; } // Create a mutex to use inside _sbrk_r if (sceKernelCreateLwMutex((struct SceKernelLwMutexWork*)_newlib_sbrk_mutex, "sbrk mutex", 0, 0, 0) < 0) { goto failure; } _newlib_heap_size = _newlib_heap_size_user; _newlib_heap_size -= _newlib_vm_size; _newlib_heap_memblock = sceKernelAllocMemBlock("Newlib heap", 0x0c20d060, _newlib_heap_size, 0); if (_newlib_heap_memblock < 0) { goto failure; } if (sceKernelGetMemBlockBase(_newlib_heap_memblock, (void**)&_newlib_heap_base) < 0) { goto failure; } _newlib_heap_end = _newlib_heap_base + _newlib_heap_size; _newlib_heap_cur = _newlib_heap_base; return; failure: _newlib_vm_memblock = 0; _newlib_heap_memblock = 0; _newlib_heap_base = 0; _newlib_heap_cur = 0; }