Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/HLE/HLE.h
3186 views
1
// Copyright (c) 2012- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#pragma once
19
20
#include <cstdio>
21
#include <cstdarg>
22
#include <type_traits>
23
#include <string_view>
24
25
#include "Common/CommonTypes.h"
26
#include "Common/Log.h"
27
#include "Core/MIPS/MIPS.h"
28
#include "Core/ConfigValues.h"
29
30
#ifdef _MSC_VER
31
#pragma warning (error: 4834) // discarding return value of function with 'nodiscard' attribute
32
#endif
33
34
class PointerWrap;
35
class PSPAction;
36
typedef void (* HLEFunc)();
37
38
enum {
39
// The low 8 bits are a value, indicating special jit handling.
40
// Currently there are none.
41
42
// The remaining 24 bits are flags.
43
// Don't allow the call within an interrupt. Not yet implemented.
44
HLE_NOT_IN_INTERRUPT = 1 << 8,
45
// Don't allow the call if dispatch or interrupts are disabled.
46
HLE_NOT_DISPATCH_SUSPENDED = 1 << 9,
47
// Indicates the call should write zeros to the stack (stackBytesToClear in the table.)
48
HLE_CLEAR_STACK_BYTES = 1 << 10,
49
// Indicates that this call operates in kernel mode.
50
HLE_KERNEL_SYSCALL = 1 << 11,
51
};
52
53
struct HLEFunction {
54
// This is the id, or nid, of the function (which is how it's linked.)
55
// Generally, the truncated least significant 32 bits of a SHA-1 hash.
56
u32 ID;
57
// A pointer to the C++ handler; see FunctionWrappers.h for helpers.
58
HLEFunc func;
59
// Name of the function. Often the same as func, this should match the PSP func's name and hash.
60
const char *name;
61
// The return type, see argmask for the possible values.
62
// An additional value is possible - 'v', which means void (no return type.)
63
char retmask;
64
// The argument types, in order. Use the following characters:
65
// - x: for u32 (shown as hex in log)
66
// - i: for int/s32
67
// - f: for float
68
// - X: uppercase, for u64
69
// - I: uppercase, for s64
70
// - F: uppercase, for double
71
// - s: a string pointer (const char *, utf-8)
72
// - p: a pointer (e.g. u32 *, shown with value in log)
73
const char *argmask;
74
// Flags (see above, e.g. HLE_NOT_IN_INTERRUPT.)
75
u32 flags;
76
// See HLE_CLEAR_STACK_BYTES.
77
u32 stackBytesToClear;
78
};
79
80
struct HLEModule {
81
std::string_view name;
82
int numFunctions;
83
const HLEFunction *funcTable;
84
};
85
86
typedef char SyscallModuleName[32];
87
88
struct Syscall {
89
SyscallModuleName moduleName;
90
u32 symAddr;
91
u32 nid;
92
};
93
94
#define PARAM(n) currentMIPS->r[MIPS_REG_A0 + n]
95
#define PARAM64(n) (currentMIPS->r[MIPS_REG_A0 + n] | ((u64)currentMIPS->r[MIPS_REG_A0 + n + 1] << 32))
96
#define PARAMF(n) currentMIPS->f[12 + n]
97
#define RETURN(n) currentMIPS->r[MIPS_REG_V0] = n;
98
#define RETURN64(n) {u64 RETURN64_tmp = n; currentMIPS->r[MIPS_REG_V0] = RETURN64_tmp & 0xFFFFFFFF; currentMIPS->r[MIPS_REG_V1] = RETURN64_tmp >> 32;}
99
#define RETURNF(fl) currentMIPS->f[0] = fl
100
101
struct HLEModuleMeta {
102
// This is the modname (name from the PRX header). Probably, we should really blacklist on the module names of the exported symbol metadata.
103
const char *modname;
104
const char *importName; // Technically a module can export functions with different module names, but doesn't seem to happen.
105
DisableHLEFlags disableFlag;
106
};
107
108
const HLEModuleMeta *GetHLEModuleMetaByFlag(DisableHLEFlags flag);
109
const HLEModuleMeta *GetHLEModuleMeta(std::string_view modname);
110
bool ShouldHLEModule(std::string_view modname, bool *wasDisabledManually = nullptr);
111
bool ShouldHLEModuleByImportName(std::string_view importModuleName);
112
113
const char *GetHLEFuncName(std::string_view module, u32 nib);
114
const char *GetHLEFuncName(int module, int func);
115
const HLEModule *GetHLEModuleByName(std::string_view name);
116
const HLEFunction *GetHLEFunc(std::string_view module, u32 nib);
117
int GetHLEFuncIndexByNib(int moduleIndex, u32 nib);
118
int GetHLEModuleIndex(std::string_view modulename);
119
u32 GetNibByName(std::string_view module, std::string_view function);
120
121
void RegisterHLEModule(std::string_view name, int numFunctions, const HLEFunction *funcTable);
122
int GetNumRegisteredHLEModules();
123
const HLEModule *GetHLEModuleByIndex(int index);
124
DisableHLEFlags AlwaysDisableHLEFlags();
125
126
// Run the current thread's callbacks after the syscall finishes.
127
void hleCheckCurrentCallbacks();
128
// Reschedule after the syscall finishes.
129
void hleReSchedule(const char *reason);
130
// Reschedule and go into a callback processing state after the syscall finishes.
131
void hleReSchedule(bool callbacks, const char *reason);
132
// Run interrupts after the syscall finishes.
133
void hleRunInterrupts();
134
// Pause emulation after the syscall finishes.
135
void hleDebugBreak();
136
// Don't set temp regs to 0xDEADBEEF.
137
void hleSkipDeadbeef();
138
// Set time spent in realtime sync.
139
void hleSetFlipTime(double t);
140
// Check if the current syscall context is kernel.
141
bool hleIsKernelMode();
142
// Enqueue a MIPS function to be called after this HLE call finishes.
143
void hleEnqueueCall(u32 func, int argc, const u32 *argv, PSPAction *afterAction = nullptr);
144
145
// Delays the result for usec microseconds, allowing other threads to run during this time.
146
u32 hleDelayResult(u32 result, const char *reason, int usec);
147
u64 hleDelayResult(u64 result, const char *reason, int usec);
148
void hleEatCycles(int cycles);
149
void hleEatMicro(int usec);
150
151
// Don't manually call this, it's called by the various syscall return macros.
152
// This should only be called once per syscall!
153
void hleLeave();
154
155
void hleCoreTimingForceCheck();
156
157
// Causes the syscall to not fully execute immediately, instead give the Ge a chance to
158
// execute display lists.
159
void hleSplitSyscallOverGe();
160
161
// Called after a split syscall from System.cpp
162
void hleFinishSyscallAfterGe();
163
164
[[nodiscard]]
165
inline int hleDelayResult(int result, const char *reason, int usec) {
166
return hleDelayResult((u32) result, reason, usec);
167
}
168
169
[[nodiscard]]
170
inline s64 hleDelayResult(s64 result, const char *reason, int usec) {
171
return hleDelayResult((u64) result, reason, usec);
172
}
173
174
void HLEInit();
175
void HLEDoState(PointerWrap &p);
176
void HLEShutdown();
177
u32 GetSyscallOp(std::string_view module, u32 nib);
178
bool WriteHLESyscall(std::string_view module, u32 nib, u32 address);
179
void CallSyscall(MIPSOpcode op);
180
void WriteFuncStub(u32 stubAddr, u32 symAddr);
181
void WriteFuncMissingStub(u32 stubAddr, u32 nid);
182
183
void HLEReturnFromMipsCall();
184
185
const HLEFunction *GetSyscallFuncPointer(MIPSOpcode op);
186
// For jit, takes arg: const HLEFunction *
187
void *GetQuickSyscallFunc(MIPSOpcode op);
188
189
void hleDoLogInternal(Log t, LogLevel level, u64 res, const char *file, int line, const char *reportTag, const char *reason, const char *formatted_reason);
190
191
template <bool leave, bool convert_code, typename T>
192
[[nodiscard]]
193
#ifdef __GNUC__
194
__attribute__((format(printf, 7, 8)))
195
#endif
196
NO_INLINE
197
T hleDoLog(Log t, LogLevel level, T res, const char *file, int line, const char *reportTag, const char *reasonFmt, ...) {
198
if (!GenericLogEnabled(t, level)) {
199
if (leave) {
200
hleLeave();
201
}
202
return res;
203
}
204
205
if (convert_code && (int)res >= 0) {
206
level = LogLevel::LDEBUG;
207
}
208
209
char formatted_reason[4096] = {0};
210
if (reasonFmt != nullptr) {
211
va_list args;
212
va_start(args, reasonFmt);
213
formatted_reason[0] = ':';
214
formatted_reason[1] = ' ';
215
vsnprintf(formatted_reason + 2, sizeof(formatted_reason) - 3, reasonFmt, args);
216
formatted_reason[sizeof(formatted_reason) - 1] = '\0';
217
va_end(args);
218
}
219
220
u64 fmtRes = res;
221
if (std::is_floating_point<T>::value) {
222
// We reinterpret as the bits for now, so we can have a common helper.
223
fmtRes = *(const u32 *)&res;
224
} else if (std::is_signed<T>::value) {
225
fmtRes = (s64)res;
226
}
227
hleDoLogInternal(t, level, fmtRes, file, line, reportTag, reasonFmt, formatted_reason);
228
if (leave) {
229
hleLeave();
230
}
231
return res;
232
}
233
234
template <bool leave, bool convert_code, typename T>
235
[[nodiscard]]
236
NO_INLINE
237
T hleDoLog(Log t, LogLevel level, T res, const char *file, int line, const char *reportTag) {
238
if (((int)level > MAX_LOGLEVEL || !GenericLogEnabled(t, level)) && !reportTag) {
239
if (leave) {
240
hleLeave();
241
}
242
return res;
243
}
244
245
if (convert_code && (int)res >= 0) {
246
level = LogLevel::LDEBUG;
247
}
248
249
u64 fmtRes = res;
250
if (std::is_floating_point<T>::value) {
251
// We reinterpret as the bits for now, so we can have a common helper.
252
fmtRes = *(const u32 *)&res;
253
} else if (std::is_signed<T>::value) {
254
fmtRes = (s64)res;
255
}
256
hleDoLogInternal(t, level, fmtRes, file, line, reportTag, nullptr, "");
257
if (leave) {
258
hleLeave();
259
}
260
return res;
261
}
262
263
// These unwind the log stack.
264
template <typename T>
265
[[nodiscard]]
266
inline T hleNoLog(T t) {
267
hleLeave();
268
return t;
269
}
270
inline void hleNoLogVoid() {
271
hleLeave();
272
}
273
274
// TODO: See if we can search the tables with constexpr tricks!
275
void hlePushFuncDesc(std::string_view module, std::string_view funcName);
276
277
// Calls a syscall from another syscall, managing the logging stack.
278
template<class R, class F, typename... Args>
279
inline R hleCallImpl(std::string_view module, std::string_view funcName, F func, Args... args) {
280
hlePushFuncDesc(module, funcName);
281
return func(args...);
282
}
283
284
// Note: ## is to eat the last comma if it's not needed (no arguments).
285
#define hleCall(module, retType, funcName, ...) hleCallImpl<retType>(#module, #funcName, funcName, ## __VA_ARGS__)
286
287
// This is just a quick way to force logging to be more visible for one file.
288
#ifdef HLE_LOG_FORCE
289
#define HLE_LOG_LDEBUG LNOTICE
290
#define HLE_LOG_LVERBOSE LDEBUG
291
292
#undef DEBUG_LOG
293
#define DEBUG_LOG NOTICE_LOG
294
#undef VERBOSE_LOG
295
#define VERBOSE_LOG DEBUG_LOG
296
#undef DEBUG_LEVEL
297
#define DEBUG_LEVEL NOTICE_LEVEL
298
#undef VERBOSE_LEVEL
299
#define VERBOSE_LEVEL NOTICE_LEVEL
300
#else
301
#define HLE_LOG_LDEBUG LogLevel::LDEBUG
302
#define HLE_LOG_LVERBOSE LogLevel::LVERBOSE
303
#endif
304
305
// IMPORTANT: These *must* only be used directly in HLE functions. They cannot be used by utility functions
306
// called by them. Use regular ERROR_LOG etc for those.
307
308
#define hleLogReturnHelper(convert, t, level, res, ...) \
309
(((int)level <= MAX_LOGLEVEL) ? hleDoLog<true, convert>(t, level, (res), __FILE__, __LINE__, nullptr, ##__VA_ARGS__) : hleNoLog(res))
310
311
#define hleLogError(t, res, ...) hleLogReturnHelper(false, t, LogLevel::LERROR, res, ##__VA_ARGS__)
312
#define hleLogWarning(t, res, ...) hleLogReturnHelper(false, t, LogLevel::LWARNING, res, ##__VA_ARGS__)
313
#define hleLogDebug(t, res, ...) hleLogReturnHelper(false, t, HLE_LOG_LDEBUG, res, ##__VA_ARGS__)
314
#define hleLogInfo(t, res, ...) hleLogReturnHelper(false, t, LogLevel::LINFO, res, ##__VA_ARGS__)
315
#define hleLogVerbose(t, res, ...) hleLogReturnHelper(false, t, HLE_LOG_LVERBOSE, res, ##__VA_ARGS__)
316
317
#define hleLogDebugOrWarn(t, res, ...) hleLogReturnHelper(true, t, LogLevel::LWARNING, res, ##__VA_ARGS__)
318
#define hleLogDebugOrError(t, res, ...) hleLogReturnHelper(true, t, LogLevel::LERROR, res, ##__VA_ARGS__)
319
320
#define hleReportError(t, res, ...) hleDoLog<true, false>(t, LogLevel::LERROR, res, __FILE__, __LINE__, "", ##__VA_ARGS__)
321
#define hleReportWarning(t, res, ...) hleDoLog<true, false>(t, LogLevel::LWARNING, res, __FILE__, __LINE__, "", ##__VA_ARGS__)
322
323