mirror of
https://github.com/hedge-dev/UnleashedRecomp
synced 2026-06-08 12:28:15 -04:00
Combine guest memory and function table into one virtual allocation.
This commit is contained in:
@@ -1,60 +0,0 @@
|
||||
#include <stdafx.h>
|
||||
#include "code_cache.h"
|
||||
#include "ppc_context.h"
|
||||
|
||||
CodeCache::CodeCache()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
bucket = (char*)VirtualAlloc(nullptr, 0x200000000, MEM_RESERVE, PAGE_READWRITE);
|
||||
assert(bucket != nullptr);
|
||||
#else
|
||||
bucket = (char*)mmap(NULL, 0x200000000, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
|
||||
assert(bucket != (char*)MAP_FAILED);
|
||||
#endif
|
||||
}
|
||||
|
||||
CodeCache::~CodeCache()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
VirtualFree(bucket, 0, MEM_RELEASE);
|
||||
#else
|
||||
munmap(bucket, 0x200000000);
|
||||
#endif
|
||||
}
|
||||
|
||||
void CodeCache::Init()
|
||||
{
|
||||
for (size_t i = 0; PPCFuncMappings[i].guest != 0; i++)
|
||||
{
|
||||
if (PPCFuncMappings[i].host != nullptr)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
VirtualAlloc(bucket + PPCFuncMappings[i].guest * 2, sizeof(void*), MEM_COMMIT, PAGE_READWRITE);
|
||||
#endif
|
||||
*(void**)(bucket + PPCFuncMappings[i].guest * 2) = (void*)PPCFuncMappings[i].host;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CodeCache::Insert(uint32_t guest, PPCFunc* host)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
VirtualAlloc(bucket + static_cast<uint64_t>(guest) * 2, sizeof(void*), MEM_COMMIT, PAGE_READWRITE);
|
||||
#endif
|
||||
*reinterpret_cast<PPCFunc**>(bucket + static_cast<uint64_t>(guest) * 2) = host;
|
||||
}
|
||||
|
||||
void* CodeCache::Find(uint32_t guest) const
|
||||
{
|
||||
return *reinterpret_cast<void**>(bucket + static_cast<uint64_t>(guest) * 2);
|
||||
}
|
||||
|
||||
SWA_API PPCFunc* KeFindHostFunction(uint32_t guest)
|
||||
{
|
||||
return reinterpret_cast<PPCFunc*>(g_codeCache.Find(guest));
|
||||
}
|
||||
|
||||
SWA_API void KeInsertHostFunction(uint32_t guest, PPCFunc* function)
|
||||
{
|
||||
g_codeCache.Insert(guest, function);
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
struct CodeCache
|
||||
{
|
||||
char* bucket{};
|
||||
|
||||
CodeCache();
|
||||
~CodeCache();
|
||||
|
||||
void Init();
|
||||
void Insert(uint32_t guest, PPCFunc* host);
|
||||
|
||||
void* Find(uint32_t guest) const;
|
||||
};
|
||||
|
||||
SWA_API PPCFunc* KeFindHostFunction(uint32_t guest);
|
||||
SWA_API void KeInsertHostFunction(uint32_t guest, PPCFunc* function);
|
||||
|
||||
extern CodeCache g_codeCache;
|
||||
@@ -1,23 +0,0 @@
|
||||
#pragma once
|
||||
#include "ppc_context.h"
|
||||
#include <kernel/memory.h>
|
||||
|
||||
struct GuestCode
|
||||
{
|
||||
inline static void Run(void* hostAddress, PPCContext* ctx, void* baseAddress)
|
||||
{
|
||||
ctx->fpscr.loadFromHost();
|
||||
reinterpret_cast<PPCFunc*>(hostAddress)(*ctx, reinterpret_cast<uint8_t*>(baseAddress));
|
||||
}
|
||||
|
||||
inline static void Run(void* hostAddress, PPCContext* ctx)
|
||||
{
|
||||
ctx->fpscr.loadFromHost();
|
||||
reinterpret_cast<PPCFunc*>(hostAddress)(*ctx, reinterpret_cast<uint8_t*>(g_memory.base));
|
||||
}
|
||||
|
||||
inline static void Run(void* hostAddress)
|
||||
{
|
||||
Run(hostAddress, GetPPCContext());
|
||||
}
|
||||
};
|
||||
@@ -3,8 +3,6 @@
|
||||
#include <kernel/memory.h>
|
||||
#include <kernel/heap.h>
|
||||
#include <kernel/function.h>
|
||||
#include "code_cache.h"
|
||||
#include "guest_code.h"
|
||||
#include "ppc_context.h"
|
||||
|
||||
constexpr size_t PCR_SIZE = 0xAB0;
|
||||
@@ -29,7 +27,6 @@ GuestThreadContext::GuestThreadContext(uint32_t cpuNumber)
|
||||
*(uint32_t*)(thread + PCR_SIZE + 0x10) = 0xFFFFFFFF; // that one TLS entry that felt quirky
|
||||
*(uint32_t*)(thread + PCR_SIZE + TLS_SIZE + 0x14C) = ByteSwap(GuestThread::GetCurrentThreadId()); // thread id
|
||||
|
||||
ppcContext.fn = (uint8_t*)g_codeCache.bucket;
|
||||
ppcContext.r1.u64 = g_memory.MapVirtual(thread + PCR_SIZE + TLS_SIZE + TEB_SIZE + STACK_SIZE); // stack pointer
|
||||
ppcContext.r13.u64 = g_memory.MapVirtual(thread);
|
||||
ppcContext.fpscr.loadFromHost();
|
||||
@@ -78,7 +75,7 @@ uint32_t GuestThread::Start(const GuestThreadParams& params)
|
||||
GuestThreadContext ctx(cpuNumber);
|
||||
ctx.ppcContext.r3.u64 = params.value;
|
||||
|
||||
reinterpret_cast<PPCFunc*>(g_codeCache.Find(params.function))(ctx.ppcContext, reinterpret_cast<uint8_t*>(g_memory.base));
|
||||
g_memory.FindFunction(params.function)(ctx.ppcContext, g_memory.base);
|
||||
|
||||
return ctx.ppcContext.r3.u32;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user