Merge pull request #1251 from unknownbrackets/module-minor

Cleanup sceKernelGetModuleIdByAddress()
This commit is contained in:
Henrik Rydgård 2013-04-11 00:45:42 -07:00
commit b5b13c6ab5
6 changed files with 49 additions and 42 deletions

View file

@ -200,7 +200,7 @@ bool ElfReader::LoadInto(u32 loadAddress)
totalEnd = p->p_vaddr + p->p_memsz; totalEnd = p->p_vaddr + p->p_memsz;
} }
} }
u32 totalSize = totalEnd - totalStart; totalSize = totalEnd - totalStart;
if (!bRelocate) if (!bRelocate)
{ {
// Binary is prerelocated, load it where the first segment starts // Binary is prerelocated, load it where the first segment starts

View file

@ -111,6 +111,11 @@ public:
return vaddr; return vaddr;
} }
u32 GetTotalSize()
{
return totalSize;
}
// More indepth stuff:) // More indepth stuff:)
bool LoadInto(u32 vaddr); bool LoadInto(u32 vaddr);
bool LoadSymbols(); bool LoadSymbols();
@ -127,6 +132,7 @@ private:
u32 *sectionAddrs; u32 *sectionAddrs;
bool bRelocate; bool bRelocate;
u32 entryPoint; u32 entryPoint;
u32 totalSize;
u32 vaddr; u32 vaddr;
u32 segmentVAddr[32]; u32 segmentVAddr[32];
}; };

View file

@ -397,22 +397,20 @@ public:
return static_cast<T *>(pool[realHandle]); return static_cast<T *>(pool[realHandle]);
} }
template <class T> template <class T, typename ArgT>
T* GetByModuleByEntryAddr(u32 entryAddr) void Iterate(bool func(T *, ArgT), ArgT arg)
{ {
for (int i = 0; i < maxCount; ++i) for (int i = 0; i < maxCount; i++)
{ {
T* t = dynamic_cast<T*>(pool[i]); if (!occupied[i])
continue;
T *t = dynamic_cast<T *>(pool[i]);
if (t) if (t)
{ {
if (t->nm.entry_addr == entryAddr) if (!func(t, arg))
{ break;
return t;
}
} }
} }
return 0;
} }
static u32 GetMissingErrorCode() { return -1; } // TODO static u32 GetMissingErrorCode() { return -1; } // TODO

View file

@ -165,12 +165,14 @@ public:
{ {
p.Do(nm); p.Do(nm);
p.Do(memoryBlockAddr); p.Do(memoryBlockAddr);
p.Do(memoryBlockSize);
p.DoMarker("Module"); p.DoMarker("Module");
} }
NativeModule nm; NativeModule nm;
u32 memoryBlockAddr; u32 memoryBlockAddr;
u32 memoryBlockSize;
bool isFake; bool isFake;
}; };
@ -232,7 +234,6 @@ struct SceKernelSMOption {
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
// STATE BEGIN // STATE BEGIN
static int actionAfterModule; static int actionAfterModule;
static SceUID mainModuleID; // hack
// STATE END // STATE END
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
@ -243,7 +244,6 @@ void __KernelModuleInit()
void __KernelModuleDoState(PointerWrap &p) void __KernelModuleDoState(PointerWrap &p)
{ {
p.Do(mainModuleID);
p.Do(actionAfterModule); p.Do(actionAfterModule);
__KernelRestoreActionType(actionAfterModule, AfterModuleEntryCall::Create); __KernelRestoreActionType(actionAfterModule, AfterModuleEntryCall::Create);
p.DoMarker("sceKernelModule"); p.DoMarker("sceKernelModule");
@ -316,6 +316,7 @@ Module *__KernelLoadELFFromPtr(const u8 *ptr, u32 loadAddress, std::string *erro
return 0; return 0;
} }
module->memoryBlockAddr = reader.GetVaddr(); module->memoryBlockAddr = reader.GetVaddr();
module->memoryBlockSize = reader.GetTotalSize();
struct libent struct libent
{ {
@ -654,7 +655,6 @@ void __KernelStartModule(Module *m, int args, const char *argp, SceKernelSMOptio
} }
__KernelSetupRootThread(m->GetUID(), args, argp, options->priority, options->stacksize, options->attribute); __KernelSetupRootThread(m->GetUID(), args, argp, options->priority, options->stacksize, options->attribute);
mainModuleID = m->GetUID();
//TODO: if current thread, put it in wait state, waiting for the new thread //TODO: if current thread, put it in wait state, waiting for the new thread
} }
@ -722,7 +722,7 @@ bool __KernelLoadExec(const char *filename, SceKernelLoadExecParam *param, std::
__KernelStartModule(module, (u32)strlen(filename) + 1, filename, &option); __KernelStartModule(module, (u32)strlen(filename) + 1, filename, &option);
__KernelStartIdleThreads(); __KernelStartIdleThreads(module->GetUID());
return true; return true;
} }
@ -893,24 +893,36 @@ u32 sceKernelStopUnloadSelfModuleWithStatus(u32 moduleId, u32 argSize, u32 argp,
return 0; return 0;
} }
struct GetModuleIdByAddressArg
{
u32 addr;
SceUID result;
};
bool __GetModuleIdByAddressIterator(Module *module, GetModuleIdByAddressArg *state)
{
const u32 start = module->memoryBlockAddr, size = module->memoryBlockSize;
printf("%08x - %08x ? %08x\n", start, start + size, state->addr);
if (start <= state->addr && start + size > state->addr)
{
state->result = module->GetUID();
return false;
}
return true;
}
u32 sceKernelGetModuleIdByAddress(u32 moduleAddr) u32 sceKernelGetModuleIdByAddress(u32 moduleAddr)
{ {
ERROR_LOG(HLE,"HACKIMPL sceKernelGetModuleIdByAddress(%08x)", moduleAddr); GetModuleIdByAddressArg state;
state.addr = moduleAddr;
state.result = SCE_KERNEL_ERROR_UNKNOWN_MODULE;
if ((moduleAddr & 0xFFFF0000) == 0x08800000) kernelObjects.Iterate(&__GetModuleIdByAddressIterator, &state);
{ if (state.result == SCE_KERNEL_ERROR_UNKNOWN_MODULE)
return mainModuleID; ERROR_LOG(HLE, "sceKernelGetModuleIdByAddress(%08x): module not found", moduleAddr)
}
else else
{ DEBUG_LOG(HLE, "%x=sceKernelGetModuleIdByAddress(%08x)", state.result, moduleAddr);
Module* foundMod= kernelObjects.GetByModuleByEntryAddr<Module>(moduleAddr); return state.result;
if(foundMod)
{
return foundMod->GetUID();
}
}
return 0;
} }
u32 sceKernelGetModuleId() u32 sceKernelGetModuleId()

View file

@ -722,9 +722,6 @@ MipsCallManager mipsCalls;
int actionAfterCallback; int actionAfterCallback;
int actionAfterMipsCall; int actionAfterMipsCall;
// This seems nasty
SceUID curModule;
// Doesn't need state saving. // Doesn't need state saving.
WaitTypeFuncs waitTypeFuncs[NUM_WAITTYPES]; WaitTypeFuncs waitTypeFuncs[NUM_WAITTYPES];
@ -869,7 +866,6 @@ void __KernelThreadingDoState(PointerWrap &p)
p.Do(threadqueue, dv); p.Do(threadqueue, dv);
p.DoArray(threadIdleID, ARRAY_SIZE(threadIdleID)); p.DoArray(threadIdleID, ARRAY_SIZE(threadIdleID));
p.Do(dispatchEnabled); p.Do(dispatchEnabled);
p.Do(curModule);
p.Do(threadReadyQueue); p.Do(threadReadyQueue);
@ -948,13 +944,13 @@ void __KernelChangeReadyState(SceUID threadID, bool ready)
WARN_LOG(HLE, "Trying to change the ready state of an unknown thread?"); WARN_LOG(HLE, "Trying to change the ready state of an unknown thread?");
} }
void __KernelStartIdleThreads() void __KernelStartIdleThreads(SceUID moduleId)
{ {
for (int i = 0; i < 2; i++) for (int i = 0; i < 2; i++)
{ {
u32 error; u32 error;
Thread *t = kernelObjects.Get<Thread>(threadIdleID[i], error); Thread *t = kernelObjects.Get<Thread>(threadIdleID[i], error);
t->nt.gpreg = __KernelGetModuleGP(curModule); t->nt.gpreg = __KernelGetModuleGP(moduleId);
t->context.r[MIPS_REG_GP] = t->nt.gpreg; t->context.r[MIPS_REG_GP] = t->nt.gpreg;
//t->context.pc += 4; // ADJUSTPC //t->context.pc += 4; // ADJUSTPC
threadReadyQueue.prepare(t->nt.currentPriority); threadReadyQueue.prepare(t->nt.currentPriority);
@ -1063,7 +1059,6 @@ void __KernelThreadingShutdown()
cbReturnHackAddr = 0; cbReturnHackAddr = 0;
currentThread = 0; currentThread = 0;
intReturnHackAddr = 0; intReturnHackAddr = 0;
curModule = 0;
hleCurrentThreadName = NULL; hleCurrentThreadName = NULL;
} }
@ -1701,7 +1696,6 @@ Thread *__KernelCreateThread(SceUID &id, SceUID moduleId, const char *name, u32
void __KernelSetupRootThread(SceUID moduleID, int args, const char *argp, int prio, int stacksize, int attr) void __KernelSetupRootThread(SceUID moduleID, int args, const char *argp, int prio, int stacksize, int attr)
{ {
curModule = moduleID;
//grab mips regs //grab mips regs
SceUID id; SceUID id;
Thread *thread = __KernelCreateThread(id, moduleID, "root", currentMIPS->pc, prio, stacksize, attr); Thread *thread = __KernelCreateThread(id, moduleID, "root", currentMIPS->pc, prio, stacksize, attr);
@ -1758,10 +1752,7 @@ int __KernelCreateThread(const char *threadName, SceUID moduleID, u32 entry, u32
int sceKernelCreateThread(const char *threadName, u32 entry, u32 prio, int stacksize, u32 attr, u32 optionAddr) int sceKernelCreateThread(const char *threadName, u32 entry, u32 prio, int stacksize, u32 attr, u32 optionAddr)
{ {
SceUID moduleId = curModule; return __KernelCreateThread(threadName, __KernelGetCurThreadModuleId(), entry, prio, stacksize, attr, optionAddr);
if (__GetCurrentThread())
moduleId = __GetCurrentThread()->moduleId;
return __KernelCreateThread(threadName, moduleId, entry, prio, stacksize, attr, optionAddr);
} }

View file

@ -185,7 +185,7 @@ u32 __KernelNotifyCallbackType(RegisteredCallbackType type, SceUID cbId, int not
SceUID __KernelGetCurThread(); SceUID __KernelGetCurThread();
SceUID __KernelGetCurThreadModuleId(); SceUID __KernelGetCurThreadModuleId();
void __KernelSetupRootThread(SceUID moduleId, int args, const char *argp, int prio, int stacksize, int attr); //represents the real PSP elf loader, run before execution void __KernelSetupRootThread(SceUID moduleId, int args, const char *argp, int prio, int stacksize, int attr); //represents the real PSP elf loader, run before execution
void __KernelStartIdleThreads(); void __KernelStartIdleThreads(SceUID moduleId);
void __KernelReturnFromThread(); // Called as HLE function void __KernelReturnFromThread(); // Called as HLE function
u32 __KernelGetThreadPrio(SceUID id); u32 __KernelGetThreadPrio(SceUID id);
bool __KernelThreadSortPriority(SceUID thread1, SceUID thread2); bool __KernelThreadSortPriority(SceUID thread1, SceUID thread2);