From 51c92881afe135350dacc31f40e80da2f9a2acea Mon Sep 17 00:00:00 2001 From: Maschell Date: Sat, 6 Jun 2020 22:39:25 +0200 Subject: [PATCH] Add kernel patches to allow usage of the memory mapping --- Makefile | 2 +- source/function_replacements.cpp | 57 ++++++++++++++++++++++++++++++++ source/function_replacements.h | 7 ++++ source/main.cpp | 6 ++++ 4 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 source/function_replacements.cpp create mode 100644 source/function_replacements.h diff --git a/Makefile b/Makefile index 689b0fd..a6413a5 100644 --- a/Makefile +++ b/Makefile @@ -38,7 +38,7 @@ CXXFLAGS := $(CFLAGS) -std=c++17 ASFLAGS := -g $(ARCH) LDFLAGS = -g $(ARCH) $(WUMSSPECS) -Wl,-Map,$(notdir $*.map) -LIBS := -lwums -lwut -lkernel +LIBS := -lwums -lwut -lkernel -lfunctionpatcher #------------------------------------------------------------------------------- # list of directories containing libraries, this must be the top level diff --git a/source/function_replacements.cpp b/source/function_replacements.cpp new file mode 100644 index 0000000..083b442 --- /dev/null +++ b/source/function_replacements.cpp @@ -0,0 +1,57 @@ +#include "function_replacements.h" + +#include "memory_mapping.h" + + +DECL_FUNCTION(int32_t, KiEffectiveToPhysical, uint32_t addressSpace, uint32_t virtualAddress) { + int32_t result = real_KiEffectiveToPhysical(addressSpace, virtualAddress); + if (result == 0) { + return MemoryMapping_EffectiveToPhysical(virtualAddress); + } + return result; +} + + +DECL_FUNCTION(int32_t, KiPhysicalToEffectiveCached, uint32_t addressSpace, uint32_t virtualAddress) { + int32_t result = real_KiPhysicalToEffectiveCached(addressSpace, virtualAddress); + if (result == 0) { + return MemoryMapping_PhysicalToEffective(virtualAddress); + } + return result; +} + +DECL_FUNCTION(int32_t, KiPhysicalToEffectiveUncached, uint32_t addressSpace, uint32_t virtualAddress) { + int32_t result = real_KiPhysicalToEffectiveUncached(addressSpace, virtualAddress); + if (result == 0) { + return MemoryMapping_PhysicalToEffective(virtualAddress); + + } + return result; +} + +DECL_FUNCTION(uint32_t, IPCKDriver_ValidatePhysicalAddress, uint32_t u1, uint32_t physStart, uint32_t physEnd) { + uint32_t result = MemoryMapping_PhysicalToEffective(physStart) > 0; + if (result) { + return result; + } + + return real_IPCKDriver_ValidatePhysicalAddress(u1, physStart, physEnd); +} + +DECL_FUNCTION(uint32_t, KiIsEffectiveRangeValid, uint32_t addressSpace, uint32_t virtualAddress, uint32_t size) { + uint32_t result = real_KiIsEffectiveRangeValid(addressSpace, virtualAddress, size); + if (result == 0) { + result = MemoryMapping_EffectiveToPhysical(virtualAddress) > 0; + } + return result; +} + +function_replacement_data_t function_replacements[] __attribute__((section(".data"))) = { + REPLACE_FUNCTION_VIA_ADDRESS(KiEffectiveToPhysical, 0xffee0aac, 0xffee0aac), + REPLACE_FUNCTION_VIA_ADDRESS(KiPhysicalToEffectiveCached, 0xffee0a3c, 0xffee0a3c), + REPLACE_FUNCTION_VIA_ADDRESS(KiPhysicalToEffectiveUncached, 0xffee0a80, 0xffee0a80), + REPLACE_FUNCTION_VIA_ADDRESS(KiIsEffectiveRangeValid, 0xffee0d6c, 0xffee0d6c), + REPLACE_FUNCTION_VIA_ADDRESS(IPCKDriver_ValidatePhysicalAddress, 0xfff0cb5c, 0xfff0cb5c), +}; + +uint32_t function_replacements_size __attribute__((section(".data"))) = sizeof(function_replacements) / sizeof(function_replacement_data_t); \ No newline at end of file diff --git a/source/function_replacements.h b/source/function_replacements.h new file mode 100644 index 0000000..e5682b5 --- /dev/null +++ b/source/function_replacements.h @@ -0,0 +1,7 @@ +#pragma once +#include +#include + +extern function_replacement_data_t function_replacements[] __attribute__((section(".data"))); + +extern uint32_t function_replacements_size __attribute__((section(".data"))); \ No newline at end of file diff --git a/source/main.cpp b/source/main.cpp index 7264349..d77cd66 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -6,7 +6,9 @@ #include #include #include "memory_mapping.h" +#include #include "logger.h" +#include "function_replacements.h" WUMS_MODULE_EXPORT_NAME("homebrew_memorymapping"); @@ -21,6 +23,10 @@ WUMS_INITIALIZE() { MemoryMapping_setupMemoryMapping(); MemoryMapping_CreateHeaps(); DEBUG_FUNCTION_LINE("total free space %d KiB", MemoryMapping_GetFreeSpace() / 1024); + + DEBUG_FUNCTION_LINE("Patch functions"); + FunctionPatcherPatchFunction(function_replacements, function_replacements_size); + DEBUG_FUNCTION_LINE("Patch functions finished"); } WUMS_APPLICATION_STARTS() {