2023-11-11 11:13:43 +01:00
|
|
|
#include "core/hle/libraries/libkernel/libkernel.h"
|
|
|
|
|
2023-11-05 12:41:10 +01:00
|
|
|
#include "common/debug.h"
|
2023-11-11 11:13:43 +01:00
|
|
|
#include "common/log.h"
|
2023-11-05 12:41:10 +01:00
|
|
|
#include "common/singleton.h"
|
2023-11-06 00:11:54 +01:00
|
|
|
#include "core/hle/kernel/Objects/physical_memory.h"
|
|
|
|
#include "core/hle/kernel/cpu_management.h"
|
|
|
|
#include "core/hle/kernel/event_queues.h"
|
|
|
|
#include "core/hle/kernel/memory_management.h"
|
2023-10-31 07:47:58 +01:00
|
|
|
#include "core/hle/libraries/libkernel/file_system.h"
|
|
|
|
#include "core/hle/libraries/libkernel/time_management.h"
|
2023-11-06 00:11:54 +01:00
|
|
|
#include "core/hle/libraries/libs.h"
|
2023-11-11 11:13:43 +01:00
|
|
|
#include "core/loader/elf.h"
|
2023-11-06 00:11:54 +01:00
|
|
|
|
|
|
|
#ifdef _WIN64
|
|
|
|
#include <windows.h>
|
2023-11-19 11:55:07 +01:00
|
|
|
#include <io.h>
|
2023-11-06 00:11:54 +01:00
|
|
|
#endif
|
2023-11-10 18:52:41 +01:00
|
|
|
#include "thread_management.h"
|
2023-07-13 11:56:36 +02:00
|
|
|
|
2023-11-06 00:11:54 +01:00
|
|
|
namespace Core::Libraries::LibKernel {
|
2023-07-13 11:56:36 +02:00
|
|
|
|
2023-11-19 11:55:07 +01:00
|
|
|
constexpr bool log_libkernel_file = true; // disable it to disable logging
|
|
|
|
|
2023-10-06 20:49:53 +02:00
|
|
|
static u64 g_stack_chk_guard = 0xDEADBEEF54321ABC; // dummy return
|
2023-07-13 11:56:36 +02:00
|
|
|
|
2023-10-06 20:49:53 +02:00
|
|
|
int32_t PS4_SYSV_ABI sceKernelReleaseDirectMemory(off_t start, size_t len) {
|
|
|
|
BREAKPOINT();
|
|
|
|
return 0;
|
|
|
|
}
|
2023-07-14 13:29:13 +02:00
|
|
|
|
2023-11-11 11:13:43 +01:00
|
|
|
static PS4_SYSV_ABI void stack_chk_fail() { BREAKPOINT(); }
|
2023-11-06 00:11:54 +01:00
|
|
|
|
2023-11-11 11:13:43 +01:00
|
|
|
int PS4_SYSV_ABI sceKernelMunmap(void* addr, size_t len) { BREAKPOINT(); }
|
|
|
|
|
2023-11-19 11:55:07 +01:00
|
|
|
void PS4_SYSV_ABI sceKernelUsleep(unsigned int microseconds) { std::this_thread::sleep_for(std::chrono::microseconds(microseconds)); }
|
|
|
|
|
|
|
|
struct iovec {
|
|
|
|
void* iov_base; /* Base address. */
|
|
|
|
size_t iov_len; /* Length. */
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t PS4_SYSV_ABI _writev(int fd, const struct iovec* iov, int iovcn) {
|
|
|
|
// weird it gives fd ==0 and writes to stdout , i am not sure if it that is valid (found in openorbis)
|
|
|
|
size_t total_written = 0;
|
|
|
|
for (int i = 0; i < iovcn; i++) {
|
|
|
|
total_written += ::fwrite(iov[i].iov_base, 1, iov[i].iov_len, stdout);
|
|
|
|
}
|
|
|
|
return total_written;
|
|
|
|
}
|
|
|
|
|
2023-11-11 11:13:43 +01:00
|
|
|
static thread_local int libc_error;
|
|
|
|
int* PS4_SYSV_ABI __Error() { return &libc_error; }
|
2023-10-30 19:22:25 +01:00
|
|
|
|
2023-11-19 11:55:07 +01:00
|
|
|
#define PROT_READ 0x1
|
|
|
|
#define PROT_WRITE 0x2
|
|
|
|
|
|
|
|
int PS4_SYSV_ABI sceKernelMmap(void* addr, u64 len, int prot, int flags, int fd, off_t offset, void** res) {
|
|
|
|
PRINT_FUNCTION_NAME();
|
|
|
|
if (prot > 3) // READ,WRITE or bitwise READ | WRITE supported
|
|
|
|
{
|
|
|
|
LOG_ERROR_IF(log_libkernel_file, "sceKernelMmap prot ={} not supported\n", prot);
|
|
|
|
}
|
|
|
|
DWORD flProtect;
|
|
|
|
if (prot & PROT_WRITE) {
|
|
|
|
flProtect = PAGE_READWRITE;
|
|
|
|
}
|
|
|
|
off_t end = len + offset;
|
|
|
|
HANDLE mmap_fd, h;
|
|
|
|
if (fd == -1)
|
|
|
|
mmap_fd = INVALID_HANDLE_VALUE;
|
|
|
|
else
|
|
|
|
mmap_fd = (HANDLE)_get_osfhandle(fd);
|
|
|
|
h = CreateFileMapping(mmap_fd, NULL, flProtect, 0, end, NULL);
|
|
|
|
int k = GetLastError();
|
|
|
|
if (NULL == h) return -1;
|
|
|
|
DWORD dwDesiredAccess;
|
|
|
|
if (prot & PROT_WRITE)
|
|
|
|
dwDesiredAccess = FILE_MAP_WRITE;
|
|
|
|
else
|
|
|
|
dwDesiredAccess = FILE_MAP_READ;
|
|
|
|
void* ret = MapViewOfFile(h, dwDesiredAccess, 0, offset, len);
|
|
|
|
if (ret == NULL) {
|
|
|
|
CloseHandle(h);
|
|
|
|
ret = nullptr;
|
|
|
|
}
|
|
|
|
*res = ret;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
PS4_SYSV_ABI void* posix_mmap(void* addr, u64 len, int prot, int flags, int fd, u64 offset) {
|
|
|
|
void* ptr;
|
|
|
|
LOG_INFO_IF(log_libkernel_file, "posix mmap redirect to sceKernelMmap\n");
|
|
|
|
// posix call the difference is that there is a different behaviour when it doesn't return 0 or SCE_OK
|
|
|
|
int result = sceKernelMmap(addr, len, prot, flags, fd, offset, &ptr);
|
|
|
|
if (result != 0) {
|
|
|
|
BREAKPOINT();
|
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2023-11-06 00:11:54 +01:00
|
|
|
void LibKernel_Register(Loader::SymbolsResolver* sym) {
|
2023-10-06 20:49:53 +02:00
|
|
|
// obj
|
2023-11-06 00:11:54 +01:00
|
|
|
LIB_OBJ("f7uOxY9mM1U", "libkernel", 1, "libkernel", 1, 1, &g_stack_chk_guard);
|
2023-10-06 20:49:53 +02:00
|
|
|
// memory
|
2023-11-06 00:11:54 +01:00
|
|
|
LIB_FUNCTION("rTXw65xmLIA", "libkernel", 1, "libkernel", 1, 1, Kernel::sceKernelAllocateDirectMemory);
|
|
|
|
LIB_FUNCTION("pO96TwzOm5E", "libkernel", 1, "libkernel", 1, 1, Kernel::sceKernelGetDirectMemorySize);
|
|
|
|
LIB_FUNCTION("L-Q3LEjIbgA", "libkernel", 1, "libkernel", 1, 1, Kernel::sceKernelMapDirectMemory);
|
2023-10-06 20:49:53 +02:00
|
|
|
LIB_FUNCTION("MBuItvba6z8", "libkernel", 1, "libkernel", 1, 1, sceKernelReleaseDirectMemory);
|
|
|
|
LIB_FUNCTION("cQke9UuBQOk", "libkernel", 1, "libkernel", 1, 1, sceKernelMunmap);
|
|
|
|
// equeue
|
2023-11-06 00:11:54 +01:00
|
|
|
LIB_FUNCTION("D0OdFMjp46I", "libkernel", 1, "libkernel", 1, 1, Kernel::sceKernelCreateEqueue);
|
|
|
|
LIB_FUNCTION("fzyMKs9kim0", "libkernel", 1, "libkernel", 1, 1, Kernel::sceKernelWaitEqueue);
|
2023-10-06 20:49:53 +02:00
|
|
|
// misc
|
2023-11-06 00:11:54 +01:00
|
|
|
LIB_FUNCTION("WslcK1FQcGI", "libkernel", 1, "libkernel", 1, 1, Kernel::sceKernelIsNeoMode);
|
2023-10-06 20:49:53 +02:00
|
|
|
LIB_FUNCTION("Ou3iL1abvng", "libkernel", 1, "libkernel", 1, 1, stack_chk_fail);
|
2023-11-11 11:13:43 +01:00
|
|
|
LIB_FUNCTION("9BcDykPmo1I", "libkernel", 1, "libkernel", 1, 1, __Error);
|
2023-11-19 11:55:07 +01:00
|
|
|
LIB_FUNCTION("BPE9s9vQQXo", "libkernel", 1, "libkernel", 1, 1, posix_mmap);
|
|
|
|
LIB_FUNCTION("1jfXLRVzisc", "libkernel", 1, "libkernel", 1, 1, sceKernelUsleep);
|
|
|
|
LIB_FUNCTION("YSHRBRLn2pI", "libkernel", 1, "libkernel", 1, 1, _writev);
|
2023-11-11 11:13:43 +01:00
|
|
|
|
2023-10-30 22:04:57 +01:00
|
|
|
Core::Libraries::LibKernel::fileSystemSymbolsRegister(sym);
|
2023-10-30 11:53:52 +01:00
|
|
|
Core::Libraries::LibKernel::timeSymbolsRegister(sym);
|
2023-11-10 18:52:41 +01:00
|
|
|
Core::Libraries::LibKernel::pthreadSymbolsRegister(sym);
|
2023-10-06 20:49:53 +02:00
|
|
|
}
|
2023-07-13 11:56:36 +02:00
|
|
|
|
2023-11-11 11:13:43 +01:00
|
|
|
} // namespace Core::Libraries::LibKernel
|