/* * Starboard 15 Vita virtual-memory mapping on sceKernelMemBlock/kubridge. * * Supports single-region allocations, page-aligned protect subranges, and * exact-size unmap. Partial unmap and cross-region unmap fail closed. * W^X enforced. Executable permission needs kubridge on hardware. */ #include "starboard/memory.h" #include "starboard/log.h" #include #include #include #include #include #include #include #include namespace { constexpr uint64_t kPageSize=4096u; // Cobalt's musl allocator maps ~508 KiB chunks; 64 slots capped it at ~32 MiB. constexpr int kMaxMaps=1024; constexpr uint64_t kMaximum=128u*1024u*1024u; struct Mapping { void* base; SceUID memid; uint64_t requested; uint64_t aligned; // Pages already returned by a partial SbMemoryUnmap (bit set = released). // A Vita memblock cannot be split, so released pages are made inaccessible // and the memblock is freed once every page has been released. uint8_t* released; uint32_t released_pages; }; Mapping maps[kMaxMaps]={}; pthread_mutex_t lock=PTHREAD_MUTEX_INITIALIZER; bool validate_flags(int flags,bool allow_exec) { const int known=kSbMemoryMapProtectRead| kSbMemoryMapProtectWrite|kSbMemoryMapProtectExec; // flags==0 is kSbMemoryMapProtectReserved: address space that is not // accessible until SbMemoryProtect() (Cobalt's musl allocator relies on it). // Vita cannot reserve without committing, so it is backed by a memblock // protected KU_KERNEL_PROT_NONE. if((flags&~known)!=0)return false; if(!allow_exec && (flags&kSbMemoryMapProtectExec))return false; if((flags&kSbMemoryMapProtectWrite) && !(flags&kSbMemoryMapProtectRead)) return false; if((flags&kSbMemoryMapProtectExec) && (flags&kSbMemoryMapProtectWrite))return false; return true; } unsigned prot(int flags) { return ((flags&kSbMemoryMapProtectRead)?KU_KERNEL_PROT_READ:0u) | ((flags&kSbMemoryMapProtectWrite)?KU_KERNEL_PROT_WRITE:0u) | ((flags&kSbMemoryMapProtectExec)?KU_KERNEL_PROT_EXEC:0u); } volatile int g_vm_log_lines=0; void vm_log(const char* fmt,...) __attribute__((format(printf,1,2))); void vm_log(const char* fmt,...){ if(__sync_add_and_fetch(&g_vm_log_lines,1)>400)return; char line[200]; va_list ap; va_start(ap,fmt); vsnprintf(line,sizeof(line),fmt,ap); va_end(ap); SbLogRaw(line); } bool safe_align(int64_t wanted,uint64_t* rounded) { if(wanted<=0 || (uint64_t)wanted>kMaximum)return false; *rounded=((uint64_t)wanted+kPageSize-1)&~(kPageSize-1); return *rounded <= kMaximum; } } extern "C" { int ytv_memory_map_count(){ pthread_mutex_lock(&lock); int n=0; for(int i=0;i=0)maps[index]={base,uid,(uint64_t)size_bytes,size}; pthread_mutex_unlock(&lock); if(index<0) { vm_log("YTV_VM_MAP_FAIL n=%d reason=table_full\n",num); sceKernelFreeMemBlock(uid); return SB_MEMORY_MAP_FAILED; } vm_log("YTV_VM_MAP n=%d base=%p size=%u flags=%d\n",num,base,(unsigned)size,flags); return base; } bool SbMemoryUnmap(void* address,int64_t size_bytes) { // munmap semantics: any page-aligned range inside one mapping may be // released (musl/V8 reserve extra space and trim it for alignment). if(!address||size_bytes<=0)return false; uintptr_t begin=(uintptr_t)address; uint64_t rounded=0; if((begin&(kPageSize-1)) || !safe_align(size_bytes,&rounded))return false; pthread_mutex_lock(&lock); int index=-1; for(int i=0;i=b && (uint64_t)(begin-b)+rounded<=maps[i].aligned) { index=i;break; } } if(index<0) { pthread_mutex_unlock(&lock); vm_log("YTV_VM_UNMAP_FAIL %p size=%lld not inside a mapping\n",address,(long long)size_bytes); return false; } Mapping& m=maps[index]; uint32_t total_pages=(uint32_t)(m.aligned/kPageSize); uint32_t first=(uint32_t)((begin-(uintptr_t)m.base)/kPageSize); uint32_t count=(uint32_t)(rounded/kPageSize); int result=0; if(first==0 && count==total_pages && m.released_pages==0) { result=sceKernelFreeMemBlock(m.memid); // whole mapping at once if(result>=0){free(m.released);m={};} } else { if(!m.released) { m.released=(uint8_t*)calloc((total_pages+7)/8,1); if(!m.released){pthread_mutex_unlock(&lock);return false;} } // Released pages must fault like unmapped memory. result=kuKernelMemProtect(address,(SceSize)rounded,KU_KERNEL_PROT_NONE); if(result>=0) { for(uint32_t p=first;p=0){free(m.released);m={};} } } } pthread_mutex_unlock(&lock); vm_log("YTV_VM_UNMAP %p size=%lld pages=%u+%u/%u result=0x%x\n",address, (long long)size_bytes,first,count,total_pages,(unsigned)result); return result>=0; } bool SbMemoryProtect(void* address,int64_t size_bytes,int flags) { if(flags&kSbMemoryMapProtectExec) vm_log("YTV_VM_PROTECT_EXEC %p size=%lld flags=%d\n",address,(long long)size_bytes,flags); if(!address || size_bytes<=0 || !validate_flags(flags,true)){ vm_log("YTV_VM_PROTECT_REJECT %p size=%lld flags=%d\n",address,(long long)size_bytes,flags); return false; } uintptr_t begin=(uintptr_t)address; if(begin&(kPageSize-1))return false; uint64_t rounded=0; if(!safe_align(size_bytes,&rounded))return false; pthread_mutex_lock(&lock); bool within=false; for(int i=0;i=b && (uint64_t)(begin-b)+rounded<=m.aligned) { within=true; // Pages released by a partial unmap no longer exist for the caller. if(m.released) { uint32_t first=(uint32_t)((begin-b)/kPageSize); for(uint32_t p=first;p=0; } } // extern C