KernelModule/source/main.cpp

229 lines
6.6 KiB
C++
Raw Permalink Normal View History

#include "version.h"
#include <coreinit/cache.h>
2022-02-04 17:35:49 +01:00
#include <coreinit/memorymap.h>
2020-05-28 21:52:29 +02:00
#include <whb/log.h>
#include <whb/log_udp.h>
2022-02-04 17:35:49 +01:00
#include <wums.h>
2020-05-28 21:52:29 +02:00
WUMS_MODULE_EXPORT_NAME("homebrew_kernel");
2022-01-26 13:19:11 +01:00
WUMS_MODULE_SKIP_INIT_FINI();
WUMS_MODULE_INIT_BEFORE_RELOCATION_DONE_HOOK();
2020-05-28 21:52:29 +02:00
2023-07-19 18:23:55 +02:00
#define MODULE_VERSION "v0.2.1"
#define MODULE_VERSION_FULL MODULE_VERSION MODULE_VERSION_EXTRA
#define KERN_SYSCALL_TBL1 0xFFE84C70 //Unknown
#define KERN_SYSCALL_TBL2 0xFFE85070 //Games
#define KERN_SYSCALL_TBL3 0xFFE85470 //Loader
#define KERN_SYSCALL_TBL4 0xFFEAAA60 //Home menu
#define KERN_SYSCALL_TBL5 0xFFEAAE60 //Browser
2020-05-28 21:52:29 +02:00
2021-09-24 16:48:16 +02:00
typedef struct sr_table_t {
uint32_t value[16];
uint32_t sdr1;
} sr_table_t;
2020-06-06 17:57:58 +02:00
extern "C" void SCKernelCopyData(uint32_t dst, uint32_t src, uint32_t len);
2020-06-06 17:57:07 +02:00
2020-06-06 17:57:58 +02:00
extern "C" void KernelCopyDataInternal(uint32_t dst, uint32_t src, uint32_t len);
2020-06-06 17:57:07 +02:00
2020-06-06 17:57:58 +02:00
extern "C" void KernelWriteSRsInternal(sr_table_t *table);
2020-06-06 17:57:07 +02:00
2020-06-06 17:57:58 +02:00
extern "C" void KernelReadSRsInternal(sr_table_t *table);
2020-05-28 21:52:29 +02:00
void kernelInitialize();
2020-06-06 17:57:07 +02:00
void KernelReadSRsInternalFunc(sr_table_t *table) {
uint32_t i = 0;
// calculate PT_size ((end-start)*8/4096)*4 or (end-start)/128
// Minimum page table size is 64Kbytes.
asm volatile("eieio; isync");
2022-02-04 17:35:49 +01:00
asm volatile("mfspr %0, 25"
: "=r"(table->sdr1));
2022-02-04 17:35:49 +01:00
asm volatile("mfsr %0, 0"
: "=r"(table->value[i]));
i++;
2022-02-04 17:35:49 +01:00
asm volatile("mfsr %0, 1"
: "=r"(table->value[i]));
i++;
2022-02-04 17:35:49 +01:00
asm volatile("mfsr %0, 2"
: "=r"(table->value[i]));
i++;
2022-02-04 17:35:49 +01:00
asm volatile("mfsr %0, 3"
: "=r"(table->value[i]));
i++;
2022-02-04 17:35:49 +01:00
asm volatile("mfsr %0, 4"
: "=r"(table->value[i]));
i++;
2022-02-04 17:35:49 +01:00
asm volatile("mfsr %0, 5"
: "=r"(table->value[i]));
i++;
2022-02-04 17:35:49 +01:00
asm volatile("mfsr %0, 6"
: "=r"(table->value[i]));
i++;
2022-02-04 17:35:49 +01:00
asm volatile("mfsr %0, 7"
: "=r"(table->value[i]));
i++;
2022-02-04 17:35:49 +01:00
asm volatile("mfsr %0, 8"
: "=r"(table->value[i]));
i++;
2022-02-04 17:35:49 +01:00
asm volatile("mfsr %0, 9"
: "=r"(table->value[i]));
i++;
2022-02-04 17:35:49 +01:00
asm volatile("mfsr %0, 10"
: "=r"(table->value[i]));
i++;
2022-02-04 17:35:49 +01:00
asm volatile("mfsr %0, 11"
: "=r"(table->value[i]));
i++;
2022-02-04 17:35:49 +01:00
asm volatile("mfsr %0, 12"
: "=r"(table->value[i]));
i++;
2022-02-04 17:35:49 +01:00
asm volatile("mfsr %0, 13"
: "=r"(table->value[i]));
i++;
2022-02-04 17:35:49 +01:00
asm volatile("mfsr %0, 14"
: "=r"(table->value[i]));
i++;
2022-02-04 17:35:49 +01:00
asm volatile("mfsr %0, 15"
: "=r"(table->value[i]));
i++;
asm volatile("eieio; isync");
}
2020-06-06 17:57:07 +02:00
void KernelWriteSRsInternalFunc(sr_table_t *table) {
asm volatile("eieio; isync");
// Writing didn't work for all at once so we only write number 8.
// TODO: fix this and change it if required.
/*asm volatile("mtsr 0, %0" : : "r" (table->value[i])); i++;
asm volatile("mtsr 1, %0" : : "r" (table->value[i])); i++;
asm volatile("mtsr 2, %0" : : "r" (table->value[i])); i++;
asm volatile("mtsr 3, %0" : : "r" (table->value[i])); i++;
asm volatile("mtsr 4, %0" : : "r" (table->value[i])); i++;
asm volatile("mtsr 5, %0" : : "r" (table->value[i])); i++;*/
2022-04-29 10:12:07 +02:00
asm volatile("mtsr 6, %0"
:
: "r"(table->value[6]));
asm volatile("mtsr 7, %0"
:
: "r"(table->value[7]));
2022-02-04 17:35:49 +01:00
asm volatile("mtsr 8, %0"
:
: "r"(table->value[8]));
/*asm volatile("mtsr 9, %0" : : "r" (table->value[i])); i++;
asm volatile("mtsr 10, %0" : : "r" (table->value[i])); i++;
asm volatile("mtsr 11, %0" : : "r" (table->value[i])); i++;
asm volatile("mtsr 12, %0" : : "r" (table->value[i])); i++;
asm volatile("mtsr 13, %0" : : "r" (table->value[i])); i++;
asm volatile("mtsr 14, %0" : : "r" (table->value[i])); i++;
asm volatile("mtsr 15, %0" : : "r" (table->value[i])); i++;*/
asm volatile("isync");
}
2020-06-06 17:57:07 +02:00
void KernelCopyData(uint32_t dst, uint32_t src, uint32_t len) {
2020-05-28 21:52:29 +02:00
KernelCopyDataInternal(dst, src, len);
}
2020-06-06 17:57:07 +02:00
void KernelWriteSRs(sr_table_t *table) {
KernelWriteSRsInternal(table);
}
2020-06-06 17:57:07 +02:00
void KernelReadSRs(sr_table_t *table) {
KernelReadSRsInternal(table);
}
2020-05-28 21:52:29 +02:00
/* Write a 32-bit word with kernel permissions */
2022-02-04 17:35:49 +01:00
void __attribute__((noinline)) kern_write(void *addr, uint32_t value) {
asm volatile(
2023-03-26 11:48:24 +02:00
"stwu 1, -0x10(1)\n"
"stw 2, 0x08(1)\n"
"stw 13, 0x0C(1)\n"
2022-02-04 17:35:49 +01:00
"li 3,1\n"
"li 4,0\n"
"mr 5,%1\n"
"li 6,0\n"
"li 7,0\n"
"lis 8,1\n"
"mr 9,%0\n"
"mr %1,1\n"
"li 0,0x3500\n"
"sc\n"
"nop\n"
"mr 1,%1\n"
2023-03-26 11:48:24 +02:00
"lwz 2, 0x8(1)\n"
"lwz 13, 0xC(1)\n"
"addi 1, 1, 0x10\n"
2022-02-04 17:35:49 +01:00
:
: "r"(addr), "r"(value)
: "memory", "ctr", "lr", "0", "3", "4", "5", "6", "7", "8", "9", "10",
"11", "12");
2020-05-28 21:52:29 +02:00
}
/* Read a 32-bit word with kernel permissions */
2022-02-04 17:35:49 +01:00
uint32_t __attribute__((noinline)) kern_read(const void *addr) {
2020-05-28 21:52:29 +02:00
uint32_t result;
2022-02-04 17:35:49 +01:00
asm volatile(
"li 3,1\n"
"li 4,0\n"
"li 5,0\n"
"li 6,0\n"
"li 7,0\n"
"lis 8,1\n"
"mr 9,%1\n"
"li 0,0x3400\n"
"mr %0,1\n"
"sc\n"
"nop\n"
"mr 1,%0\n"
"mr %0,3\n"
: "=r"(result)
: "b"(addr)
: "memory", "ctr", "lr", "0", "3", "4", "5", "6", "7", "8", "9", "10",
"11", "12");
2020-05-28 21:52:29 +02:00
return result;
}
2021-10-31 16:06:42 +01:00
void KernelPatchSyscall(int index, uint32_t addr) {
2020-05-28 21:52:29 +02:00
kern_write((void *) (KERN_SYSCALL_TBL1 + index * 4), addr);
kern_write((void *) (KERN_SYSCALL_TBL2 + index * 4), addr);
kern_write((void *) (KERN_SYSCALL_TBL3 + index * 4), addr);
kern_write((void *) (KERN_SYSCALL_TBL4 + index * 4), addr);
kern_write((void *) (KERN_SYSCALL_TBL5 + index * 4), addr);
}
void kernelInitialize() {
static uint8_t ucSyscallsSetupRequired = 1;
2020-06-06 17:57:07 +02:00
if (!ucSyscallsSetupRequired) {
2020-05-28 21:52:29 +02:00
return;
}
ucSyscallsSetupRequired = 0;
2021-10-31 16:06:42 +01:00
KernelPatchSyscall(0x25, (uint32_t) SCKernelCopyData);
KernelPatchSyscall(0x36, (uint32_t) KernelReadSRsInternalFunc);
KernelPatchSyscall(0x0A, (uint32_t) KernelWriteSRsInternalFunc);
}
2021-09-17 18:34:23 +02:00
WUMS_INITIALIZE(myargs) {
kernelInitialize();
2020-05-28 21:52:29 +02:00
}
2021-10-31 16:06:42 +01:00
WUMS_APPLICATION_STARTS() {
OSReport("Running KernelModule " MODULE_VERSION_FULL "\n");
}
2021-10-31 16:06:42 +01:00
WUMS_EXPORT_FUNCTION(KernelCopyData);
WUMS_EXPORT_FUNCTION(KernelWriteSRs);
WUMS_EXPORT_FUNCTION(KernelReadSRs);
2023-07-19 18:23:55 +02:00
WUMS_EXPORT_FUNCTION(KernelPatchSyscall);