2012-11-01 15:19:01 +00:00
|
|
|
// Copyright (c) 2012- PPSSPP Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
2012-11-04 22:01:49 +00:00
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official git repository and contact information can be found at
|
|
|
|
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
|
|
|
|
|
2015-03-22 07:12:08 +00:00
|
|
|
#include <cstdarg>
|
2012-11-01 15:19:01 +00:00
|
|
|
#include <map>
|
2012-12-28 22:33:00 +00:00
|
|
|
#include <vector>
|
2013-07-05 07:00:41 +00:00
|
|
|
#include <string>
|
2013-12-29 23:11:29 +00:00
|
|
|
|
2020-10-04 08:04:01 +00:00
|
|
|
#include "Common/Profiler/Profiler.h"
|
2013-12-29 23:11:29 +00:00
|
|
|
|
2020-08-15 10:25:39 +00:00
|
|
|
#include "Common/Log.h"
|
2020-08-10 07:12:51 +00:00
|
|
|
#include "Common/Serialize/SerializeFuncs.h"
|
2020-08-15 18:53:08 +00:00
|
|
|
#include "Common/TimeUtil.h"
|
2013-12-29 23:11:29 +00:00
|
|
|
#include "Core/Config.h"
|
2020-08-10 04:20:42 +00:00
|
|
|
#include "Core/Core.h"
|
2013-03-09 19:37:57 +00:00
|
|
|
#include "Core/CoreTiming.h"
|
2015-04-06 01:03:50 +00:00
|
|
|
#include "Core/MemMapHelpers.h"
|
2013-03-02 07:14:00 +00:00
|
|
|
#include "Core/Reporting.h"
|
2013-12-29 23:11:29 +00:00
|
|
|
#include "Core/System.h"
|
|
|
|
#include "Core/MIPS/MIPS.h"
|
|
|
|
#include "Core/MIPS/MIPSCodeUtils.h"
|
|
|
|
#include "Core/HLE/HLETables.h"
|
|
|
|
#include "Core/HLE/sceIo.h"
|
|
|
|
#include "Core/HLE/sceAudio.h"
|
|
|
|
#include "Core/HLE/sceKernelMemory.h"
|
|
|
|
#include "Core/HLE/sceKernelThread.h"
|
|
|
|
#include "Core/HLE/sceKernelInterrupt.h"
|
|
|
|
#include "Core/HLE/HLE.h"
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2012-12-09 06:39:36 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
// Do nothing after the syscall.
|
2014-03-03 16:16:53 +00:00
|
|
|
HLE_AFTER_NOTHING = 0x00,
|
2012-12-09 06:39:36 +00:00
|
|
|
// Reschedule immediately after the syscall.
|
2014-03-03 16:16:53 +00:00
|
|
|
HLE_AFTER_RESCHED = 0x01,
|
2012-12-09 06:39:36 +00:00
|
|
|
// Call current thread's callbacks after the syscall.
|
|
|
|
HLE_AFTER_CURRENT_CALLBACKS = 0x02,
|
|
|
|
// Reschedule and process current thread's callbacks after the syscall.
|
|
|
|
HLE_AFTER_RESCHED_CALLBACKS = 0x08,
|
2012-12-21 05:54:40 +00:00
|
|
|
// Run interrupts (and probably reschedule) after the syscall.
|
2014-03-03 16:16:53 +00:00
|
|
|
HLE_AFTER_RUN_INTERRUPTS = 0x10,
|
2012-12-25 05:47:54 +00:00
|
|
|
// Switch to CORE_STEPPING after the syscall (for debugging.)
|
2014-03-03 16:16:53 +00:00
|
|
|
HLE_AFTER_DEBUG_BREAK = 0x20,
|
2013-10-30 05:53:25 +00:00
|
|
|
// Don't fill temp regs with 0xDEADBEEF.
|
2014-03-03 16:16:53 +00:00
|
|
|
HLE_AFTER_SKIP_DEADBEEF = 0x40,
|
2020-03-21 23:09:23 +00:00
|
|
|
// Execute pending mips calls.
|
|
|
|
HLE_AFTER_QUEUED_CALLS = 0x80,
|
2012-12-09 06:39:36 +00:00
|
|
|
};
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2012-12-09 09:16:19 +00:00
|
|
|
static std::vector<HLEModule> moduleDB;
|
2013-03-09 19:37:57 +00:00
|
|
|
static int delayedResultEvent = -1;
|
2012-12-09 09:16:19 +00:00
|
|
|
static int hleAfterSyscall = HLE_AFTER_NOTHING;
|
2013-01-26 18:59:17 +00:00
|
|
|
static const char *hleAfterSyscallReschedReason;
|
2015-03-22 07:12:08 +00:00
|
|
|
static const HLEFunction *latestSyscall = nullptr;
|
2021-10-23 23:56:15 +00:00
|
|
|
static uint32_t latestSyscallPC = 0;
|
2016-05-07 19:34:27 +00:00
|
|
|
static int idleOp;
|
2012-12-09 09:16:19 +00:00
|
|
|
|
2020-03-21 23:09:23 +00:00
|
|
|
struct HLEMipsCallInfo {
|
|
|
|
u32 func;
|
|
|
|
PSPAction *action;
|
|
|
|
std::vector<u32> args;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct HLEMipsCallStack {
|
|
|
|
u32_le nextOff;
|
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
u32_le func;
|
|
|
|
u32_le actionIndex;
|
|
|
|
u32_le argc;
|
|
|
|
};
|
|
|
|
struct {
|
|
|
|
u32_le ra;
|
|
|
|
u32_le v0;
|
|
|
|
u32_le v1;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
// No need to save state, always flushed at a syscall end.
|
|
|
|
static std::vector<HLEMipsCallInfo> enqueuedMipsCalls;
|
|
|
|
// Does need to be saved, referenced by the stack and owned.
|
|
|
|
static std::vector<PSPAction *> mipsCallActions;
|
|
|
|
|
2013-03-09 19:37:57 +00:00
|
|
|
void hleDelayResultFinish(u64 userdata, int cycleslate)
|
|
|
|
{
|
|
|
|
u32 error;
|
|
|
|
SceUID threadID = (SceUID) userdata;
|
2013-06-09 06:39:49 +00:00
|
|
|
SceUID verify = __KernelGetWaitID(threadID, WAITTYPE_HLEDELAY, error);
|
2013-03-11 09:52:15 +00:00
|
|
|
// The top 32 bits of userdata are the top 32 bits of the 64 bit result.
|
|
|
|
// We can't just put it all in userdata because we need to know the threadID...
|
|
|
|
u64 result = (userdata & 0xFFFFFFFF00000000ULL) | __KernelGetWaitValue(threadID, error);
|
2013-03-09 19:37:57 +00:00
|
|
|
|
|
|
|
if (error == 0 && verify == 1)
|
2014-06-24 07:44:02 +00:00
|
|
|
{
|
2013-03-09 19:37:57 +00:00
|
|
|
__KernelResumeThreadFromWait(threadID, result);
|
2014-06-24 07:44:02 +00:00
|
|
|
__KernelReSchedule("woke from hle delay");
|
|
|
|
}
|
2013-03-09 19:37:57 +00:00
|
|
|
else
|
2024-07-14 12:42:59 +00:00
|
|
|
WARN_LOG(Log::HLE, "Someone else woke up HLE-blocked thread %d?", threadID);
|
2013-03-09 19:37:57 +00:00
|
|
|
}
|
|
|
|
|
2020-03-21 23:09:23 +00:00
|
|
|
void HLEInit() {
|
2012-11-01 15:19:01 +00:00
|
|
|
RegisterAllModules();
|
2013-03-09 19:37:57 +00:00
|
|
|
delayedResultEvent = CoreTiming::RegisterEvent("HLEDelayedResult", hleDelayResultFinish);
|
2016-05-07 19:34:27 +00:00
|
|
|
idleOp = GetSyscallOp("FakeSysCalls", NID_IDLE);
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2020-03-21 23:09:23 +00:00
|
|
|
void HLEDoState(PointerWrap &p) {
|
|
|
|
auto s = p.Section("HLE", 1, 2);
|
2013-09-15 03:23:03 +00:00
|
|
|
if (!s)
|
|
|
|
return;
|
|
|
|
|
2018-01-01 16:57:08 +00:00
|
|
|
// Can't be inside a syscall, reset this so errors aren't misleading.
|
|
|
|
latestSyscall = nullptr;
|
2021-10-23 23:56:15 +00:00
|
|
|
latestSyscallPC = 0;
|
2020-08-10 04:20:42 +00:00
|
|
|
Do(p, delayedResultEvent);
|
2013-03-09 19:37:57 +00:00
|
|
|
CoreTiming::RestoreRegisterEvent(delayedResultEvent, "HLEDelayedResult", hleDelayResultFinish);
|
2020-03-21 23:09:23 +00:00
|
|
|
|
|
|
|
if (s >= 2) {
|
|
|
|
int actions = (int)mipsCallActions.size();
|
2020-08-10 04:20:42 +00:00
|
|
|
Do(p, actions);
|
2020-03-21 23:09:23 +00:00
|
|
|
if (actions != (int)mipsCallActions.size()) {
|
|
|
|
mipsCallActions.resize(actions);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &action : mipsCallActions) {
|
|
|
|
int actionTypeID = action != nullptr ? action->actionTypeID : -1;
|
2020-08-10 04:20:42 +00:00
|
|
|
Do(p, actionTypeID);
|
2020-03-21 23:09:23 +00:00
|
|
|
if (actionTypeID != -1) {
|
|
|
|
if (p.mode == p.MODE_READ)
|
|
|
|
action = __KernelCreateAction(actionTypeID);
|
|
|
|
action->DoState(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-28 06:14:31 +00:00
|
|
|
}
|
|
|
|
|
2020-03-21 23:09:23 +00:00
|
|
|
void HLEShutdown() {
|
2012-12-25 05:04:28 +00:00
|
|
|
hleAfterSyscall = HLE_AFTER_NOTHING;
|
2015-03-22 07:12:08 +00:00
|
|
|
latestSyscall = nullptr;
|
2021-10-23 23:56:15 +00:00
|
|
|
latestSyscallPC = 0;
|
2012-11-01 15:19:01 +00:00
|
|
|
moduleDB.clear();
|
2020-03-21 23:09:23 +00:00
|
|
|
enqueuedMipsCalls.clear();
|
|
|
|
for (auto p : mipsCallActions) {
|
|
|
|
delete p;
|
|
|
|
}
|
|
|
|
mipsCallActions.clear();
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RegisterModule(const char *name, int numFunctions, const HLEFunction *funcTable)
|
|
|
|
{
|
|
|
|
HLEModule module = {name, numFunctions, funcTable};
|
|
|
|
moduleDB.push_back(module);
|
|
|
|
}
|
|
|
|
|
|
|
|
int GetModuleIndex(const char *moduleName)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < moduleDB.size(); i++)
|
|
|
|
if (strcmp(moduleName, moduleDB[i].name) == 0)
|
|
|
|
return (int)i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int GetFuncIndex(int moduleIndex, u32 nib)
|
|
|
|
{
|
|
|
|
const HLEModule &module = moduleDB[moduleIndex];
|
|
|
|
for (int i = 0; i < module.numFunctions; i++)
|
|
|
|
{
|
|
|
|
if (module.funcTable[i].ID == nib)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 GetNibByName(const char *moduleName, const char *function)
|
|
|
|
{
|
|
|
|
int moduleIndex = GetModuleIndex(moduleName);
|
2015-01-19 16:34:12 +00:00
|
|
|
if (moduleIndex == -1)
|
|
|
|
return -1;
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
const HLEModule &module = moduleDB[moduleIndex];
|
|
|
|
for (int i = 0; i < module.numFunctions; i++)
|
|
|
|
{
|
|
|
|
if (!strcmp(module.funcTable[i].name, function))
|
|
|
|
return module.funcTable[i].ID;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const HLEFunction *GetFunc(const char *moduleName, u32 nib)
|
|
|
|
{
|
|
|
|
int moduleIndex = GetModuleIndex(moduleName);
|
|
|
|
if (moduleIndex != -1)
|
|
|
|
{
|
|
|
|
int idx = GetFuncIndex(moduleIndex, nib);
|
|
|
|
if (idx != -1)
|
|
|
|
return &(moduleDB[moduleIndex].funcTable[idx]);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *GetFuncName(const char *moduleName, u32 nib)
|
|
|
|
{
|
2020-07-19 15:47:02 +00:00
|
|
|
_dbg_assert_msg_(moduleName != nullptr, "Invalid module name.");
|
2013-01-02 02:08:42 +00:00
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
const HLEFunction *func = GetFunc(moduleName,nib);
|
|
|
|
if (func)
|
|
|
|
return func->name;
|
2013-01-02 02:08:42 +00:00
|
|
|
|
|
|
|
static char temp[256];
|
2023-04-28 19:04:05 +00:00
|
|
|
snprintf(temp, sizeof(temp), "[UNK: 0x%08x]", nib);
|
2013-01-02 02:08:42 +00:00
|
|
|
return temp;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 15:01:59 +00:00
|
|
|
u32 GetSyscallOp(const char *moduleName, u32 nib) {
|
2013-09-01 01:35:59 +00:00
|
|
|
// Special case to hook up bad imports.
|
2017-03-08 15:01:59 +00:00
|
|
|
if (moduleName == NULL) {
|
2013-09-01 01:35:59 +00:00
|
|
|
return (0x03FFFFCC); // invalid syscall
|
|
|
|
}
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
int modindex = GetModuleIndex(moduleName);
|
2017-03-08 15:01:59 +00:00
|
|
|
if (modindex != -1) {
|
2012-11-01 15:19:01 +00:00
|
|
|
int funcindex = GetFuncIndex(modindex, nib);
|
2017-03-08 15:01:59 +00:00
|
|
|
if (funcindex != -1) {
|
2012-11-01 15:19:01 +00:00
|
|
|
return (0x0000000c | (modindex<<18) | (funcindex<<6));
|
2017-03-08 15:01:59 +00:00
|
|
|
} else {
|
2024-07-14 12:42:59 +00:00
|
|
|
INFO_LOG(Log::HLE, "Syscall (%s, %08x) unknown", moduleName, nib);
|
2012-11-01 15:19:01 +00:00
|
|
|
return (0x0003FFCC | (modindex<<18)); // invalid syscall
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-07-14 12:42:59 +00:00
|
|
|
ERROR_LOG(Log::HLE, "Unknown module %s!", moduleName);
|
2013-01-27 10:37:45 +00:00
|
|
|
return (0x03FFFFCC); // invalid syscall
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-01 01:35:59 +00:00
|
|
|
bool FuncImportIsSyscall(const char *module, u32 nib)
|
|
|
|
{
|
|
|
|
return GetFunc(module, nib) != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WriteFuncStub(u32 stubAddr, u32 symAddr)
|
|
|
|
{
|
|
|
|
// Note that this should be J not JAL, as otherwise control will return to the stub..
|
|
|
|
Memory::Write_U32(MIPS_MAKE_J(symAddr), stubAddr);
|
|
|
|
// Note: doing that, we can't trace external module calls, so maybe something else should be done to debug more efficiently
|
|
|
|
// Perhaps a syscall here (and verify support in jit), marking the module by uid (debugIdentifier)?
|
|
|
|
Memory::Write_U32(MIPS_MAKE_NOP(), stubAddr + 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WriteFuncMissingStub(u32 stubAddr, u32 nid)
|
|
|
|
{
|
|
|
|
// Write a trap so we notice this func if it's called before resolving.
|
|
|
|
Memory::Write_U32(MIPS_MAKE_JR_RA(), stubAddr); // jr ra
|
|
|
|
Memory::Write_U32(GetSyscallOp(NULL, nid), stubAddr + 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WriteSyscall(const char *moduleName, u32 nib, u32 address)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
|
|
|
if (nib == 0)
|
|
|
|
{
|
2024-07-14 12:42:59 +00:00
|
|
|
WARN_LOG_REPORT(Log::HLE, "Wrote patched out nid=0 syscall (%s)", moduleName);
|
2012-11-01 15:19:01 +00:00
|
|
|
Memory::Write_U32(MIPS_MAKE_JR_RA(), address); //patched out?
|
|
|
|
Memory::Write_U32(MIPS_MAKE_NOP(), address+4); //patched out?
|
2013-09-01 01:35:59 +00:00
|
|
|
return true;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
2012-11-04 18:01:20 +00:00
|
|
|
int modindex = GetModuleIndex(moduleName);
|
|
|
|
if (modindex != -1)
|
|
|
|
{
|
|
|
|
Memory::Write_U32(MIPS_MAKE_JR_RA(), address); // jr ra
|
|
|
|
Memory::Write_U32(GetSyscallOp(moduleName, nib), address + 4);
|
2013-09-01 01:35:59 +00:00
|
|
|
return true;
|
2012-11-04 18:01:20 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-07-14 12:42:59 +00:00
|
|
|
ERROR_LOG_REPORT(Log::HLE, "Unable to write unknown syscall: %s/%08x", moduleName, nib);
|
2013-09-01 01:35:59 +00:00
|
|
|
return false;
|
2012-11-04 18:01:20 +00:00
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *GetFuncName(int moduleIndex, int func)
|
|
|
|
{
|
|
|
|
if (moduleIndex >= 0 && moduleIndex < (int)moduleDB.size())
|
|
|
|
{
|
|
|
|
const HLEModule &module = moduleDB[moduleIndex];
|
2013-06-26 18:30:49 +00:00
|
|
|
if (func >= 0 && func < module.numFunctions)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
|
|
|
return module.funcTable[func].name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "[unknown]";
|
|
|
|
}
|
|
|
|
|
2012-12-09 06:39:36 +00:00
|
|
|
void hleCheckCurrentCallbacks()
|
|
|
|
{
|
|
|
|
hleAfterSyscall |= HLE_AFTER_CURRENT_CALLBACKS;
|
|
|
|
}
|
|
|
|
|
|
|
|
void hleReSchedule(const char *reason)
|
|
|
|
{
|
2014-03-01 23:14:17 +00:00
|
|
|
#ifdef _DEBUG
|
2020-07-19 15:47:02 +00:00
|
|
|
_dbg_assert_msg_(reason != nullptr && strlen(reason) < 256, "hleReSchedule: Invalid or too long reason.");
|
2014-03-01 23:14:17 +00:00
|
|
|
#endif
|
2012-12-09 06:39:36 +00:00
|
|
|
|
|
|
|
hleAfterSyscall |= HLE_AFTER_RESCHED;
|
2012-12-09 17:12:49 +00:00
|
|
|
|
|
|
|
if (!reason)
|
2013-01-26 18:59:17 +00:00
|
|
|
hleAfterSyscallReschedReason = "Invalid reason";
|
2012-12-09 17:12:49 +00:00
|
|
|
else
|
2013-01-26 18:59:17 +00:00
|
|
|
hleAfterSyscallReschedReason = reason;
|
2012-12-09 06:39:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void hleReSchedule(bool callbacks, const char *reason)
|
|
|
|
{
|
|
|
|
hleReSchedule(reason);
|
2012-12-09 23:08:44 +00:00
|
|
|
if (callbacks)
|
|
|
|
hleAfterSyscall |= HLE_AFTER_RESCHED_CALLBACKS;
|
2012-12-09 06:39:36 +00:00
|
|
|
}
|
|
|
|
|
2012-12-21 05:54:40 +00:00
|
|
|
void hleRunInterrupts()
|
|
|
|
{
|
|
|
|
hleAfterSyscall |= HLE_AFTER_RUN_INTERRUPTS;
|
|
|
|
}
|
|
|
|
|
2012-12-25 05:47:54 +00:00
|
|
|
void hleDebugBreak()
|
2012-12-09 09:16:19 +00:00
|
|
|
{
|
2012-12-25 05:47:54 +00:00
|
|
|
hleAfterSyscall |= HLE_AFTER_DEBUG_BREAK;
|
|
|
|
}
|
|
|
|
|
2013-10-30 05:53:25 +00:00
|
|
|
void hleSkipDeadbeef()
|
|
|
|
{
|
|
|
|
hleAfterSyscall |= HLE_AFTER_SKIP_DEADBEEF;
|
|
|
|
}
|
|
|
|
|
2012-12-25 05:47:54 +00:00
|
|
|
// Pauses execution after an HLE call.
|
2012-12-25 05:58:54 +00:00
|
|
|
bool hleExecuteDebugBreak(const HLEFunction &func)
|
2012-12-25 05:47:54 +00:00
|
|
|
{
|
|
|
|
const u32 NID_SUSPEND_INTR = 0x092968F4, NID_RESUME_INTR = 0x5F10D406;
|
|
|
|
|
|
|
|
// Never break on these, they're noise.
|
|
|
|
u32 blacklistedNIDs[] = {NID_SUSPEND_INTR, NID_RESUME_INTR, NID_IDLE};
|
2013-02-18 16:44:59 +00:00
|
|
|
for (size_t i = 0; i < ARRAY_SIZE(blacklistedNIDs); ++i)
|
2012-12-25 05:47:54 +00:00
|
|
|
{
|
|
|
|
if (func.ID == blacklistedNIDs[i])
|
2012-12-25 05:58:54 +00:00
|
|
|
return false;
|
2012-12-25 05:47:54 +00:00
|
|
|
}
|
|
|
|
|
2021-10-23 23:56:15 +00:00
|
|
|
Core_EnableStepping(true, "hle.step", latestSyscallPC);
|
2012-12-25 05:58:54 +00:00
|
|
|
return true;
|
2012-12-25 05:47:54 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 07:40:12 +00:00
|
|
|
u32 hleDelayResult(u32 result, const char *reason, int usec) {
|
|
|
|
if (!__KernelIsDispatchEnabled()) {
|
2024-07-14 12:42:59 +00:00
|
|
|
WARN_LOG(Log::HLE, "%s: Dispatch disabled, not delaying HLE result (right thing to do?)", latestSyscall ? latestSyscall->name : "?");
|
2021-02-07 07:40:12 +00:00
|
|
|
} else {
|
|
|
|
SceUID thread = __KernelGetCurThread();
|
|
|
|
if (KernelIsThreadWaiting(thread))
|
2024-07-14 12:42:59 +00:00
|
|
|
ERROR_LOG(Log::HLE, "%s: Delaying a thread that's already waiting", latestSyscall ? latestSyscall->name : "?");
|
2021-02-07 07:40:12 +00:00
|
|
|
CoreTiming::ScheduleEvent(usToCycles(usec), delayedResultEvent, thread);
|
2013-06-09 06:39:49 +00:00
|
|
|
__KernelWaitCurThread(WAITTYPE_HLEDELAY, 1, result, 0, false, reason);
|
2013-04-03 07:04:17 +00:00
|
|
|
}
|
2013-03-10 17:59:59 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-02-07 07:40:12 +00:00
|
|
|
u64 hleDelayResult(u64 result, const char *reason, int usec) {
|
|
|
|
if (!__KernelIsDispatchEnabled()) {
|
2024-07-14 12:42:59 +00:00
|
|
|
WARN_LOG(Log::HLE, "%s: Dispatch disabled, not delaying HLE result (right thing to do?)", latestSyscall ? latestSyscall->name : "?");
|
2021-02-07 07:40:12 +00:00
|
|
|
} else {
|
|
|
|
SceUID thread = __KernelGetCurThread();
|
|
|
|
if (KernelIsThreadWaiting(thread))
|
2024-07-14 12:42:59 +00:00
|
|
|
ERROR_LOG(Log::HLE, "%s: Delaying a thread that's already waiting", latestSyscall ? latestSyscall->name : "?");
|
2021-02-07 07:40:12 +00:00
|
|
|
u64 param = (result & 0xFFFFFFFF00000000) | thread;
|
2013-04-03 07:04:17 +00:00
|
|
|
CoreTiming::ScheduleEvent(usToCycles(usec), delayedResultEvent, param);
|
2021-02-07 07:40:12 +00:00
|
|
|
__KernelWaitCurThread(WAITTYPE_HLEDELAY, 1, (u32)result, 0, false, reason);
|
2013-04-03 07:04:17 +00:00
|
|
|
}
|
2013-03-09 19:37:57 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-08-04 16:19:47 +00:00
|
|
|
void hleEatCycles(int cycles) {
|
2013-03-09 21:41:42 +00:00
|
|
|
// Maybe this should Idle, at least for larger delays? Could that cause issues?
|
2013-03-19 14:48:41 +00:00
|
|
|
currentMIPS->downcount -= cycles;
|
|
|
|
}
|
|
|
|
|
2016-08-04 16:19:47 +00:00
|
|
|
void hleEatMicro(int usec) {
|
2013-03-19 14:48:41 +00:00
|
|
|
hleEatCycles((int) usToCycles(usec));
|
2013-03-09 21:41:42 +00:00
|
|
|
}
|
|
|
|
|
2016-08-04 16:19:47 +00:00
|
|
|
bool hleIsKernelMode() {
|
|
|
|
return latestSyscall && (latestSyscall->flags & HLE_KERNEL_SYSCALL) != 0;
|
|
|
|
}
|
|
|
|
|
2020-03-21 23:09:23 +00:00
|
|
|
void hleEnqueueCall(u32 func, int argc, const u32 *argv, PSPAction *afterAction) {
|
|
|
|
std::vector<u32> args;
|
|
|
|
args.resize(argc);
|
|
|
|
memcpy(args.data(), argv, argc * sizeof(u32));
|
|
|
|
|
|
|
|
enqueuedMipsCalls.push_back({ func, afterAction, args });
|
|
|
|
|
|
|
|
hleAfterSyscall |= HLE_AFTER_QUEUED_CALLS;
|
|
|
|
}
|
|
|
|
|
|
|
|
void hleFlushCalls() {
|
|
|
|
u32 &sp = currentMIPS->r[MIPS_REG_SP];
|
|
|
|
PSPPointer<HLEMipsCallStack> stackData;
|
2024-07-14 12:42:59 +00:00
|
|
|
VERBOSE_LOG(Log::HLE, "Flushing %d HLE mips calls from %s, sp=%08x", (int)enqueuedMipsCalls.size(), latestSyscall ? latestSyscall->name : "?", sp);
|
2020-03-21 23:09:23 +00:00
|
|
|
|
|
|
|
// First, we'll add a marker for the final return.
|
|
|
|
sp -= sizeof(HLEMipsCallStack);
|
|
|
|
stackData.ptr = sp;
|
|
|
|
stackData->nextOff = 0xFFFFFFFF;
|
|
|
|
stackData->ra = currentMIPS->pc;
|
|
|
|
stackData->v0 = currentMIPS->r[MIPS_REG_V0];
|
|
|
|
stackData->v1 = currentMIPS->r[MIPS_REG_V1];
|
|
|
|
|
|
|
|
// Now we'll set up the first in the chain.
|
|
|
|
currentMIPS->pc = enqueuedMipsCalls[0].func;
|
|
|
|
currentMIPS->r[MIPS_REG_RA] = HLEMipsCallReturnAddress();
|
|
|
|
for (int i = 0; i < (int)enqueuedMipsCalls[0].args.size(); i++) {
|
|
|
|
currentMIPS->r[MIPS_REG_A0 + i] = enqueuedMipsCalls[0].args[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
// For stack info, process the first enqueued call last, so we run it first.
|
|
|
|
// We don't actually need to store 0's args, but keep it consistent.
|
|
|
|
for (int i = (int)enqueuedMipsCalls.size() - 1; i >= 0; --i) {
|
|
|
|
auto &info = enqueuedMipsCalls[i];
|
|
|
|
u32 stackRequired = (int)info.args.size() * sizeof(u32) + sizeof(HLEMipsCallStack);
|
|
|
|
u32 stackAligned = (stackRequired + 0xF) & ~0xF;
|
|
|
|
|
|
|
|
sp -= stackAligned;
|
|
|
|
stackData.ptr = sp;
|
|
|
|
stackData->nextOff = stackAligned;
|
|
|
|
stackData->func = info.func;
|
|
|
|
if (info.action) {
|
|
|
|
stackData->actionIndex = (int)mipsCallActions.size();
|
|
|
|
mipsCallActions.push_back(info.action);
|
|
|
|
} else {
|
|
|
|
stackData->actionIndex = 0xFFFFFFFF;
|
|
|
|
}
|
|
|
|
stackData->argc = (int)info.args.size();
|
|
|
|
for (int j = 0; j < (int)info.args.size(); ++j) {
|
|
|
|
Memory::Write_U32(info.args[j], sp + sizeof(HLEMipsCallStack) + j * sizeof(u32));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
enqueuedMipsCalls.clear();
|
|
|
|
|
2024-07-14 12:42:59 +00:00
|
|
|
DEBUG_LOG(Log::HLE, "Executing HLE mips call at %08x, sp=%08x", currentMIPS->pc, sp);
|
2020-03-21 23:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void HLEReturnFromMipsCall() {
|
|
|
|
u32 &sp = currentMIPS->r[MIPS_REG_SP];
|
|
|
|
PSPPointer<HLEMipsCallStack> stackData;
|
|
|
|
|
|
|
|
// At this point, we may have another mips call to run, or be at the end...
|
|
|
|
stackData.ptr = sp;
|
|
|
|
|
|
|
|
if ((stackData->nextOff & 0x0000000F) != 0 || !Memory::IsValidAddress(sp + stackData->nextOff)) {
|
2024-07-14 12:42:59 +00:00
|
|
|
ERROR_LOG(Log::HLE, "Corrupt stack on HLE mips call return: %08x", stackData->nextOff);
|
2020-07-04 18:19:56 +00:00
|
|
|
Core_UpdateState(CORE_RUNTIME_ERROR);
|
2020-03-21 23:09:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stackData->actionIndex != 0xFFFFFFFF && stackData->actionIndex < (u32)mipsCallActions.size()) {
|
|
|
|
PSPAction *&action = mipsCallActions[stackData->actionIndex];
|
2024-07-14 12:42:59 +00:00
|
|
|
VERBOSE_LOG(Log::HLE, "Executing action for HLE mips call at %08x, sp=%08x", stackData->func, sp);
|
2020-03-21 23:09:23 +00:00
|
|
|
|
|
|
|
// Search for the saved v0/v1 values, to preserve the PSPAction API...
|
|
|
|
PSPPointer<HLEMipsCallStack> finalMarker = stackData;
|
|
|
|
while ((finalMarker->nextOff & 0x0000000F) == 0 && Memory::IsValidAddress(finalMarker.ptr + finalMarker->nextOff)) {
|
|
|
|
finalMarker.ptr += finalMarker->nextOff;
|
|
|
|
}
|
2020-07-04 18:19:56 +00:00
|
|
|
|
2020-03-21 23:09:23 +00:00
|
|
|
if (finalMarker->nextOff != 0xFFFFFFFF) {
|
2024-07-14 12:42:59 +00:00
|
|
|
ERROR_LOG(Log::HLE, "Corrupt stack on HLE mips call return action: %08x", finalMarker->nextOff);
|
2020-07-04 18:19:56 +00:00
|
|
|
Core_UpdateState(CORE_RUNTIME_ERROR);
|
2020-03-21 23:09:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MipsCall mc;
|
|
|
|
mc.savedV0 = finalMarker->v0;
|
|
|
|
mc.savedV1 = finalMarker->v1;
|
|
|
|
action->run(mc);
|
|
|
|
finalMarker->v0 = mc.savedV0;
|
|
|
|
finalMarker->v1 = mc.savedV1;
|
|
|
|
|
|
|
|
delete action;
|
|
|
|
action = nullptr;
|
|
|
|
|
|
|
|
// Note: the action could actually enqueue more, adding another layer on stack after this.
|
|
|
|
}
|
|
|
|
|
|
|
|
sp += stackData->nextOff;
|
|
|
|
stackData.ptr = sp;
|
|
|
|
|
|
|
|
if (stackData->nextOff == 0xFFFFFFFF) {
|
|
|
|
// We're done. Grab the HLE result's v0/v1 and return from the syscall.
|
|
|
|
currentMIPS->pc = stackData->ra;
|
|
|
|
currentMIPS->r[MIPS_REG_V0] = stackData->v0;
|
|
|
|
currentMIPS->r[MIPS_REG_V1] = stackData->v1;
|
|
|
|
|
|
|
|
sp += sizeof(HLEMipsCallStack);
|
|
|
|
|
|
|
|
bool canClear = true;
|
|
|
|
for (auto p : mipsCallActions) {
|
|
|
|
canClear = canClear && p == nullptr;
|
|
|
|
}
|
|
|
|
if (canClear) {
|
|
|
|
mipsCallActions.clear();
|
|
|
|
}
|
|
|
|
|
2024-07-14 12:42:59 +00:00
|
|
|
VERBOSE_LOG(Log::HLE, "Finished HLE mips calls, v0=%08x, sp=%08x", currentMIPS->r[MIPS_REG_V0], sp);
|
2020-03-21 23:09:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alright, we have another to call.
|
2020-03-26 00:11:02 +00:00
|
|
|
hleSkipDeadbeef();
|
2020-03-21 23:09:23 +00:00
|
|
|
currentMIPS->pc = stackData->func;
|
|
|
|
currentMIPS->r[MIPS_REG_RA] = HLEMipsCallReturnAddress();
|
|
|
|
for (int i = 0; i < (int)stackData->argc; i++) {
|
|
|
|
currentMIPS->r[MIPS_REG_A0 + i] = Memory::Read_U32(sp + sizeof(HLEMipsCallStack) + i * sizeof(u32));
|
|
|
|
}
|
2024-07-14 12:42:59 +00:00
|
|
|
DEBUG_LOG(Log::HLE, "Executing next HLE mips call at %08x, sp=%08x", currentMIPS->pc, sp);
|
2020-03-21 23:09:23 +00:00
|
|
|
}
|
|
|
|
|
2013-10-30 05:53:25 +00:00
|
|
|
const static u32 deadbeefRegs[12] = {0xDEADBEEF, 0xDEADBEEF, 0xDEADBEEF, 0xDEADBEEF, 0xDEADBEEF, 0xDEADBEEF, 0xDEADBEEF, 0xDEADBEEF, 0xDEADBEEF, 0xDEADBEEF, 0xDEADBEEF, 0xDEADBEEF};
|
|
|
|
inline static void SetDeadbeefRegs()
|
|
|
|
{
|
2023-07-30 00:51:52 +00:00
|
|
|
// Not exactly the same, but any time a syscall happens, it should clear ll.
|
|
|
|
currentMIPS->llBit = 0;
|
|
|
|
|
2013-10-30 06:02:05 +00:00
|
|
|
if (g_Config.bSkipDeadbeefFilling)
|
|
|
|
return;
|
|
|
|
|
2013-10-30 05:53:25 +00:00
|
|
|
currentMIPS->r[MIPS_REG_COMPILER_SCRATCH] = 0xDEADBEEF;
|
|
|
|
// Set all the arguments and temp regs.
|
|
|
|
memcpy(¤tMIPS->r[MIPS_REG_A0], deadbeefRegs, sizeof(deadbeefRegs));
|
2015-03-01 17:58:47 +00:00
|
|
|
currentMIPS->r[MIPS_REG_T8] = 0xDEADBEEF;
|
|
|
|
currentMIPS->r[MIPS_REG_T9] = 0xDEADBEEF;
|
2013-10-30 05:53:25 +00:00
|
|
|
|
|
|
|
currentMIPS->lo = 0xDEADBEEF;
|
|
|
|
currentMIPS->hi = 0xDEADBEEF;
|
|
|
|
}
|
|
|
|
|
2013-11-02 20:15:44 +00:00
|
|
|
inline void hleFinishSyscall(const HLEFunction &info)
|
2012-12-25 05:47:54 +00:00
|
|
|
{
|
2013-10-30 05:53:25 +00:00
|
|
|
if ((hleAfterSyscall & HLE_AFTER_SKIP_DEADBEEF) == 0)
|
|
|
|
SetDeadbeefRegs();
|
|
|
|
|
2020-03-21 23:09:23 +00:00
|
|
|
if ((hleAfterSyscall & HLE_AFTER_QUEUED_CALLS) != 0)
|
|
|
|
hleFlushCalls();
|
2014-06-28 10:06:30 +00:00
|
|
|
if ((hleAfterSyscall & HLE_AFTER_CURRENT_CALLBACKS) != 0 && (hleAfterSyscall & HLE_AFTER_RESCHED_CALLBACKS) == 0)
|
2012-12-09 09:16:19 +00:00
|
|
|
__KernelForceCallbacks();
|
|
|
|
|
2012-12-21 05:54:40 +00:00
|
|
|
if ((hleAfterSyscall & HLE_AFTER_RUN_INTERRUPTS) != 0)
|
|
|
|
__RunOnePendingInterrupt();
|
|
|
|
|
2012-12-09 09:16:19 +00:00
|
|
|
if ((hleAfterSyscall & HLE_AFTER_RESCHED_CALLBACKS) != 0)
|
|
|
|
__KernelReSchedule(true, hleAfterSyscallReschedReason);
|
|
|
|
else if ((hleAfterSyscall & HLE_AFTER_RESCHED) != 0)
|
|
|
|
__KernelReSchedule(hleAfterSyscallReschedReason);
|
|
|
|
|
2012-12-25 05:58:54 +00:00
|
|
|
if ((hleAfterSyscall & HLE_AFTER_DEBUG_BREAK) != 0)
|
|
|
|
{
|
2013-11-02 20:15:44 +00:00
|
|
|
if (!hleExecuteDebugBreak(info))
|
2012-12-25 05:58:54 +00:00
|
|
|
{
|
|
|
|
// We'll do it next syscall.
|
|
|
|
hleAfterSyscall = HLE_AFTER_DEBUG_BREAK;
|
2013-01-26 18:59:17 +00:00
|
|
|
hleAfterSyscallReschedReason = 0;
|
2012-12-25 05:58:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-09 09:16:19 +00:00
|
|
|
hleAfterSyscall = HLE_AFTER_NOTHING;
|
2013-01-26 18:59:17 +00:00
|
|
|
hleAfterSyscallReschedReason = 0;
|
2012-12-09 09:16:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-31 07:47:25 +00:00
|
|
|
static void updateSyscallStats(int modulenum, int funcnum, double total)
|
2013-01-11 16:37:54 +00:00
|
|
|
{
|
|
|
|
const char *name = moduleDB[modulenum].funcTable[funcnum].name;
|
|
|
|
// Ignore this one, especially for msInSyscalls (although that ignores CoreTiming events.)
|
|
|
|
if (0 == strcmp(name, "_sceKernelIdle"))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (total > kernelStats.slowestSyscallTime)
|
|
|
|
{
|
|
|
|
kernelStats.slowestSyscallTime = total;
|
|
|
|
kernelStats.slowestSyscallName = name;
|
|
|
|
}
|
|
|
|
kernelStats.msInSyscalls += total;
|
|
|
|
|
|
|
|
KernelStatsSyscall statCall(modulenum, funcnum);
|
|
|
|
auto summedStat = kernelStats.summedMsInSyscalls.find(statCall);
|
|
|
|
if (summedStat == kernelStats.summedMsInSyscalls.end())
|
|
|
|
{
|
|
|
|
kernelStats.summedMsInSyscalls[statCall] = total;
|
|
|
|
if (total > kernelStats.summedSlowestSyscallTime)
|
|
|
|
{
|
|
|
|
kernelStats.summedSlowestSyscallTime = total;
|
|
|
|
kernelStats.summedSlowestSyscallName = name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
double newTotal = kernelStats.summedMsInSyscalls[statCall] += total;
|
|
|
|
if (newTotal > kernelStats.summedSlowestSyscallTime)
|
|
|
|
{
|
|
|
|
kernelStats.summedSlowestSyscallTime = newTotal;
|
|
|
|
kernelStats.summedSlowestSyscallName = name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-02 20:15:44 +00:00
|
|
|
inline void CallSyscallWithFlags(const HLEFunction *info)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2015-03-22 07:12:08 +00:00
|
|
|
latestSyscall = info;
|
2021-10-23 23:56:15 +00:00
|
|
|
latestSyscallPC = currentMIPS->pc;
|
2013-11-02 20:15:44 +00:00
|
|
|
const u32 flags = info->flags;
|
2015-03-02 00:03:12 +00:00
|
|
|
|
|
|
|
if (flags & HLE_CLEAR_STACK_BYTES) {
|
|
|
|
u32 stackStart = __KernelGetCurThreadStackStart();
|
|
|
|
if (currentMIPS->r[MIPS_REG_SP] - info->stackBytesToClear >= stackStart) {
|
2021-02-03 07:53:16 +00:00
|
|
|
Memory::Memset(currentMIPS->r[MIPS_REG_SP] - info->stackBytesToClear, 0, info->stackBytesToClear, "HLEStackClear");
|
2015-03-02 00:03:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((flags & HLE_NOT_DISPATCH_SUSPENDED) && !__KernelIsDispatchEnabled()) {
|
2024-07-14 12:42:59 +00:00
|
|
|
RETURN(hleLogDebug(Log::HLE, SCE_KERNEL_ERROR_CAN_NOT_WAIT, "dispatch suspended"));
|
2015-03-07 22:05:31 +00:00
|
|
|
} else if ((flags & HLE_NOT_IN_INTERRUPT) && __IsInInterrupt()) {
|
2024-07-14 12:42:59 +00:00
|
|
|
RETURN(hleLogDebug(Log::HLE, SCE_KERNEL_ERROR_ILLEGAL_CONTEXT, "in interrupt"));
|
2015-03-02 00:03:12 +00:00
|
|
|
} else {
|
2013-11-02 20:15:44 +00:00
|
|
|
info->func();
|
2015-03-02 00:03:12 +00:00
|
|
|
}
|
2013-11-02 20:15:44 +00:00
|
|
|
|
|
|
|
if (hleAfterSyscall != HLE_AFTER_NOTHING)
|
|
|
|
hleFinishSyscall(*info);
|
|
|
|
else
|
|
|
|
SetDeadbeefRegs();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void CallSyscallWithoutFlags(const HLEFunction *info)
|
|
|
|
{
|
2015-03-22 07:12:08 +00:00
|
|
|
latestSyscall = info;
|
2021-10-23 23:56:15 +00:00
|
|
|
latestSyscallPC = currentMIPS->pc;
|
2013-11-02 20:15:44 +00:00
|
|
|
info->func();
|
|
|
|
|
|
|
|
if (hleAfterSyscall != HLE_AFTER_NOTHING)
|
|
|
|
hleFinishSyscall(*info);
|
|
|
|
else
|
|
|
|
SetDeadbeefRegs();
|
|
|
|
}
|
|
|
|
|
2017-03-09 10:36:16 +00:00
|
|
|
const HLEFunction *GetSyscallFuncPointer(MIPSOpcode op)
|
2013-11-02 20:15:44 +00:00
|
|
|
{
|
2012-11-01 15:19:01 +00:00
|
|
|
u32 callno = (op >> 6) & 0xFFFFF; //20 bits
|
|
|
|
int funcnum = callno & 0xFFF;
|
|
|
|
int modulenum = (callno & 0xFF000) >> 12;
|
2014-06-08 11:03:03 +00:00
|
|
|
if (funcnum == 0xfff) {
|
2024-07-14 12:42:59 +00:00
|
|
|
ERROR_LOG(Log::HLE, "Unknown syscall: Module: %s (module: %d func: %d)", modulenum > (int)moduleDB.size() ? "(unknown)" : moduleDB[modulenum].name, modulenum, funcnum);
|
2014-06-08 11:03:03 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-06-15 17:01:54 +00:00
|
|
|
if (modulenum >= (int)moduleDB.size()) {
|
2024-07-14 12:42:59 +00:00
|
|
|
ERROR_LOG(Log::HLE, "Syscall had bad module number %d - probably executing garbage", modulenum);
|
2014-06-08 11:03:03 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (funcnum >= moduleDB[modulenum].numFunctions) {
|
2024-07-14 12:42:59 +00:00
|
|
|
ERROR_LOG(Log::HLE, "Syscall had bad function number %d in module %d - probably executing garbage", funcnum, modulenum);
|
2013-11-02 20:15:44 +00:00
|
|
|
return NULL;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
2013-11-02 20:15:44 +00:00
|
|
|
return &moduleDB[modulenum].funcTable[funcnum];
|
|
|
|
}
|
|
|
|
|
2017-03-24 01:57:18 +00:00
|
|
|
void *GetQuickSyscallFunc(MIPSOpcode op) {
|
|
|
|
if (coreCollectDebugStats)
|
|
|
|
return nullptr;
|
2013-11-02 20:15:44 +00:00
|
|
|
|
2017-03-09 10:36:16 +00:00
|
|
|
const HLEFunction *info = GetSyscallFuncPointer(op);
|
2013-11-02 20:15:44 +00:00
|
|
|
if (!info || !info->func)
|
2017-03-24 01:57:18 +00:00
|
|
|
return nullptr;
|
2024-07-14 12:42:59 +00:00
|
|
|
DEBUG_LOG(Log::HLE, "Compiling syscall to %s", info->name);
|
2013-11-02 20:15:44 +00:00
|
|
|
|
|
|
|
// TODO: Do this with a flag?
|
2017-03-24 01:57:18 +00:00
|
|
|
if (op == idleOp)
|
2013-11-02 20:15:44 +00:00
|
|
|
return (void *)info->func;
|
|
|
|
if (info->flags != 0)
|
|
|
|
return (void *)&CallSyscallWithFlags;
|
|
|
|
return (void *)&CallSyscallWithoutFlags;
|
|
|
|
}
|
|
|
|
|
2014-06-14 15:42:18 +00:00
|
|
|
static double hleSteppingTime = 0.0;
|
2021-08-15 03:18:06 +00:00
|
|
|
void hleSetSteppingTime(double t) {
|
2014-06-14 15:42:18 +00:00
|
|
|
hleSteppingTime += t;
|
|
|
|
}
|
|
|
|
|
2021-08-15 03:18:06 +00:00
|
|
|
static double hleFlipTime = 0.0;
|
|
|
|
void hleSetFlipTime(double t) {
|
|
|
|
hleFlipTime = t;
|
|
|
|
}
|
|
|
|
|
2013-11-02 20:15:44 +00:00
|
|
|
void CallSyscall(MIPSOpcode op)
|
|
|
|
{
|
2015-07-03 19:05:08 +00:00
|
|
|
PROFILE_THIS_SCOPE("syscall");
|
2017-03-24 01:57:18 +00:00
|
|
|
double start = 0.0; // need to initialize to fix the race condition where coreCollectDebugStats is enabled in the middle of this func.
|
2017-08-17 20:51:20 +00:00
|
|
|
if (coreCollectDebugStats) {
|
2013-11-02 20:15:44 +00:00
|
|
|
start = time_now_d();
|
|
|
|
}
|
2015-03-02 00:03:12 +00:00
|
|
|
|
2017-03-09 10:36:16 +00:00
|
|
|
const HLEFunction *info = GetSyscallFuncPointer(op);
|
2014-07-14 03:34:10 +00:00
|
|
|
if (!info) {
|
|
|
|
RETURN(SCE_KERNEL_ERROR_LIBRARY_NOT_YET_LINKED);
|
2013-11-02 20:48:35 +00:00
|
|
|
return;
|
2014-07-14 03:34:10 +00:00
|
|
|
}
|
2013-11-02 20:48:35 +00:00
|
|
|
|
2016-05-07 19:34:27 +00:00
|
|
|
if (info->func) {
|
|
|
|
if (op == idleOp)
|
2013-11-02 20:48:35 +00:00
|
|
|
info->func();
|
|
|
|
else if (info->flags != 0)
|
|
|
|
CallSyscallWithFlags(info);
|
2013-03-25 06:30:32 +00:00
|
|
|
else
|
2013-11-02 20:48:35 +00:00
|
|
|
CallSyscallWithoutFlags(info);
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
2014-07-14 03:34:10 +00:00
|
|
|
else {
|
|
|
|
RETURN(SCE_KERNEL_ERROR_LIBRARY_NOT_YET_LINKED);
|
2024-07-14 12:42:59 +00:00
|
|
|
ERROR_LOG_REPORT(Log::HLE, "Unimplemented HLE function %s", info->name ? info->name : "(\?\?\?)");
|
2014-07-14 03:34:10 +00:00
|
|
|
}
|
2013-11-02 20:48:35 +00:00
|
|
|
|
2017-08-17 20:51:20 +00:00
|
|
|
if (coreCollectDebugStats) {
|
2013-11-02 20:15:44 +00:00
|
|
|
u32 callno = (op >> 6) & 0xFFFFF; //20 bits
|
|
|
|
int funcnum = callno & 0xFFF;
|
|
|
|
int modulenum = (callno & 0xFF000) >> 12;
|
2014-06-14 15:42:18 +00:00
|
|
|
double total = time_now_d() - start - hleSteppingTime;
|
2021-08-15 03:18:06 +00:00
|
|
|
if (total >= hleFlipTime)
|
|
|
|
total -= hleFlipTime;
|
2021-01-31 23:06:11 +00:00
|
|
|
_dbg_assert_msg_(total >= 0.0, "Time spent in syscall became negative");
|
2014-06-14 15:42:18 +00:00
|
|
|
hleSteppingTime = 0.0;
|
2021-08-15 03:18:06 +00:00
|
|
|
hleFlipTime = 0.0;
|
2014-06-14 15:42:18 +00:00
|
|
|
updateSyscallStats(modulenum, funcnum, total);
|
2013-02-03 04:04:45 +00:00
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
2015-03-22 07:12:08 +00:00
|
|
|
|
|
|
|
size_t hleFormatLogArgs(char *message, size_t sz, const char *argmask) {
|
|
|
|
char *p = message;
|
|
|
|
size_t used = 0;
|
|
|
|
|
|
|
|
#define APPEND_FMT(...) do { \
|
|
|
|
if (used < sz) { \
|
|
|
|
size_t c = snprintf(p, sz - used, __VA_ARGS__); \
|
|
|
|
used += c; \
|
|
|
|
p += c; \
|
|
|
|
} \
|
|
|
|
} while (false)
|
|
|
|
|
2015-03-23 04:06:08 +00:00
|
|
|
int reg = 0;
|
|
|
|
int regf = 0;
|
|
|
|
for (size_t i = 0, n = strlen(argmask); i < n; ++i, ++reg) {
|
|
|
|
u32 regval;
|
|
|
|
if (reg < 8) {
|
|
|
|
regval = PARAM(reg);
|
|
|
|
} else {
|
|
|
|
u32 sp = currentMIPS->r[MIPS_REG_SP];
|
|
|
|
// Goes upward on stack.
|
2015-04-08 19:44:32 +00:00
|
|
|
// NOTE: Currently we only support > 8 for 32-bit integer args.
|
2015-04-08 07:55:49 +00:00
|
|
|
regval = Memory::Read_U32(sp + (reg - 8) * 4);
|
2015-03-23 04:06:08 +00:00
|
|
|
}
|
2015-03-22 23:57:56 +00:00
|
|
|
|
2015-03-22 07:12:08 +00:00
|
|
|
switch (argmask[i]) {
|
|
|
|
case 'p':
|
2015-04-08 19:44:32 +00:00
|
|
|
if (Memory::IsValidAddress(regval)) {
|
|
|
|
APPEND_FMT("%08x[%08x]", regval, Memory::Read_U32(regval));
|
2015-03-22 07:12:08 +00:00
|
|
|
} else {
|
2015-04-08 19:44:32 +00:00
|
|
|
APPEND_FMT("%08x[invalid]", regval);
|
2015-03-22 07:12:08 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-01-25 06:22:23 +00:00
|
|
|
case 'P':
|
|
|
|
if (Memory::IsValidAddress(regval)) {
|
|
|
|
APPEND_FMT("%08x[%016llx]", regval, Memory::Read_U64(regval));
|
|
|
|
} else {
|
|
|
|
APPEND_FMT("%08x[invalid]", regval);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-03-22 07:12:08 +00:00
|
|
|
case 's':
|
2015-04-08 19:44:32 +00:00
|
|
|
if (Memory::IsValidAddress(regval)) {
|
2016-01-25 06:52:19 +00:00
|
|
|
const char *s = Memory::GetCharPointer(regval);
|
2023-04-17 02:17:45 +00:00
|
|
|
const int safeLen = Memory::ValidSize(regval, 128);
|
|
|
|
if (strnlen(s, safeLen) >= safeLen) {
|
|
|
|
APPEND_FMT("%.*s...", safeLen, Memory::GetCharPointer(regval));
|
2016-01-25 06:52:19 +00:00
|
|
|
} else {
|
2023-04-17 02:17:45 +00:00
|
|
|
APPEND_FMT("%.*s", safeLen, Memory::GetCharPointer(regval));
|
2016-01-25 06:52:19 +00:00
|
|
|
}
|
2015-03-22 07:12:08 +00:00
|
|
|
} else {
|
|
|
|
APPEND_FMT("(invalid)");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'x':
|
2015-04-08 19:44:32 +00:00
|
|
|
APPEND_FMT("%08x", regval);
|
2015-03-22 07:12:08 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'i':
|
2015-04-08 19:44:32 +00:00
|
|
|
APPEND_FMT("%d", regval);
|
2015-03-23 04:06:08 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'X':
|
|
|
|
case 'I':
|
|
|
|
// 64-bit regs are always aligned.
|
|
|
|
if ((reg & 1))
|
|
|
|
++reg;
|
|
|
|
APPEND_FMT("%016llx", PARAM64(reg));
|
|
|
|
++reg;
|
2015-03-22 07:12:08 +00:00
|
|
|
break;
|
|
|
|
|
2015-03-23 04:06:08 +00:00
|
|
|
case 'f':
|
2015-03-29 03:46:37 +00:00
|
|
|
APPEND_FMT("%f", PARAMF(regf++));
|
2015-03-23 04:06:08 +00:00
|
|
|
// This doesn't consume a gp reg.
|
|
|
|
--reg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
// TODO: Double? Does it ever happen?
|
|
|
|
|
2015-03-22 07:12:08 +00:00
|
|
|
default:
|
2020-07-19 15:47:02 +00:00
|
|
|
_dbg_assert_msg_(false, "Invalid argmask character: %c", argmask[i]);
|
2015-04-08 19:44:32 +00:00
|
|
|
APPEND_FMT(" -- invalid arg format: %c -- %08x", argmask[i], regval);
|
2015-03-22 07:12:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i + 1 < n) {
|
|
|
|
APPEND_FMT(", ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (used > sz) {
|
|
|
|
message[sz - 1] = '\0';
|
|
|
|
} else {
|
|
|
|
message[used] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef APPEND_FMT
|
|
|
|
return used;
|
|
|
|
}
|
|
|
|
|
2024-07-14 12:42:59 +00:00
|
|
|
void hleDoLogInternal(Log t, LogLevel level, u64 res, const char *file, int line, const char *reportTag, char retmask, const char *reason, const char *formatted_reason) {
|
2015-03-22 07:12:08 +00:00
|
|
|
char formatted_args[4096];
|
2018-01-01 16:57:08 +00:00
|
|
|
const char *funcName = "?";
|
|
|
|
u32 funcFlags = 0;
|
|
|
|
if (latestSyscall) {
|
2020-07-19 15:47:02 +00:00
|
|
|
_dbg_assert_(latestSyscall->argmask != nullptr);
|
2018-01-01 16:57:08 +00:00
|
|
|
hleFormatLogArgs(formatted_args, sizeof(formatted_args), latestSyscall->argmask);
|
|
|
|
|
|
|
|
// This acts as an override (for error returns which are usually hex.)
|
|
|
|
if (retmask == '\0')
|
|
|
|
retmask = latestSyscall->retmask;
|
2015-03-23 00:11:52 +00:00
|
|
|
|
2018-01-01 16:57:08 +00:00
|
|
|
funcName = latestSyscall->name;
|
|
|
|
funcFlags = latestSyscall->flags;
|
2022-09-24 01:47:11 +00:00
|
|
|
} else {
|
|
|
|
strcpy(formatted_args, "?");
|
2018-01-01 16:57:08 +00:00
|
|
|
}
|
2015-03-22 07:12:08 +00:00
|
|
|
|
|
|
|
const char *fmt;
|
|
|
|
if (retmask == 'x') {
|
2016-08-04 16:19:47 +00:00
|
|
|
fmt = "%s%08llx=%s(%s)%s";
|
2016-02-07 03:53:56 +00:00
|
|
|
// Truncate the high bits of the result (from any sign extension.)
|
|
|
|
res = (u32)res;
|
2016-01-25 06:22:23 +00:00
|
|
|
} else if (retmask == 'i' || retmask == 'I') {
|
2016-08-04 16:19:47 +00:00
|
|
|
fmt = "%s%lld=%s(%s)%s";
|
2016-02-07 03:53:56 +00:00
|
|
|
} else if (retmask == 'f') {
|
|
|
|
// TODO: For now, floats are just shown as bits.
|
2016-08-04 16:19:47 +00:00
|
|
|
fmt = "%s%08x=%s(%s)%s";
|
2015-03-22 07:12:08 +00:00
|
|
|
} else {
|
2020-07-19 15:47:02 +00:00
|
|
|
_dbg_assert_msg_(false, "Invalid return format: %c", retmask);
|
2016-08-04 16:19:47 +00:00
|
|
|
fmt = "%s%08llx=%s(%s)%s";
|
2015-03-22 07:12:08 +00:00
|
|
|
}
|
|
|
|
|
2018-01-01 16:57:08 +00:00
|
|
|
const char *kernelFlag = (funcFlags & HLE_KERNEL_SYSCALL) != 0 ? "K " : "";
|
|
|
|
GenericLog(level, t, file, line, fmt, kernelFlag, res, funcName, formatted_args, formatted_reason);
|
2015-03-22 07:12:08 +00:00
|
|
|
|
|
|
|
if (reportTag != nullptr) {
|
|
|
|
// A blank string means always log, not just once.
|
2020-08-16 20:38:15 +00:00
|
|
|
if (reportTag[0] == '\0' || Reporting::ShouldLogNTimes(reportTag, 1)) {
|
2015-03-22 07:12:08 +00:00
|
|
|
// Here we want the original key, so that different args, etc. group together.
|
2018-01-01 16:57:08 +00:00
|
|
|
std::string key = std::string(kernelFlag) + std::string("%08x=") + funcName + "(%s)";
|
2015-03-22 07:12:08 +00:00
|
|
|
if (reason != nullptr)
|
|
|
|
key += std::string(": ") + reason;
|
|
|
|
|
|
|
|
char formatted_message[8192];
|
2018-01-01 16:57:08 +00:00
|
|
|
snprintf(formatted_message, sizeof(formatted_message), fmt, kernelFlag, res, funcName, formatted_args, formatted_reason);
|
2015-03-22 07:12:08 +00:00
|
|
|
Reporting::ReportMessageFormatted(key.c_str(), formatted_message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|