Formatting and cleanup

This commit is contained in:
Maschell 2021-09-24 16:51:11 +02:00
parent 18a2889a36
commit d0e432fb69
7 changed files with 96 additions and 94 deletions

View File

@ -26,8 +26,8 @@ public:
typedef void (*Callback)(CThread *thread, void *arg); typedef void (*Callback)(CThread *thread, void *arg);
//! constructor //! constructor
CThread(int32_t iAttr, int32_t iPriority = 16, int32_t iStackSize = 0x8000, CThread::Callback callback = NULL, void *callbackArg = NULL) explicit CThread(int32_t iAttr, int32_t iPriority = 16, int32_t iStackSize = 0x8000, CThread::Callback callback = nullptr, void *callbackArg = nullptr)
: pThread(NULL), pThreadStack(NULL), pCallback(callback), pCallbackArg(callbackArg) { : pThread(nullptr), pThreadStack(nullptr), pCallback(callback), pCallbackArg(callbackArg) {
//! save attribute assignment //! save attribute assignment
iAttributes = iAttr; iAttributes = iAttr;
//! allocate the thread //! allocate the thread
@ -50,24 +50,24 @@ public:
} }
//! Get thread ID //! Get thread ID
virtual void *getThread() const { [[nodiscard]] virtual void *getThread() const {
return pThread; return pThread;
} }
//! Thread entry function //! Thread entry function
virtual void executeThread(void) { virtual void executeThread() {
if (pCallback) if (pCallback)
pCallback(this, pCallbackArg); pCallback(this, pCallbackArg);
} }
//! Suspend thread //! Suspend thread
virtual void suspendThread(void) { virtual void suspendThread() {
if (isThreadSuspended()) return; if (isThreadSuspended()) return;
if (pThread) OSSuspendThread(pThread); if (pThread) OSSuspendThread(pThread);
} }
//! Resume thread //! Resume thread
virtual void resumeThread(void) { virtual void resumeThread() {
if (!isThreadSuspended()) return; if (!isThreadSuspended()) return;
if (pThread) OSResumeThread(pThread); if (pThread) OSResumeThread(pThread);
} }
@ -78,36 +78,36 @@ public:
} }
//! Check if thread is suspended //! Check if thread is suspended
virtual bool isThreadSuspended(void) const { [[nodiscard]] virtual bool isThreadSuspended() const {
if (pThread) return OSIsThreadSuspended(pThread); if (pThread) return OSIsThreadSuspended(pThread);
return false; return false;
} }
//! Check if thread is terminated //! Check if thread is terminated
virtual bool isThreadTerminated(void) const { [[nodiscard]] virtual bool isThreadTerminated() const {
if (pThread) return OSIsThreadTerminated(pThread); if (pThread) return OSIsThreadTerminated(pThread);
return false; return false;
} }
//! Check if thread is running //! Check if thread is running
virtual bool isThreadRunning(void) const { [[nodiscard]] virtual bool isThreadRunning() const {
return !isThreadSuspended() && !isThreadRunning(); return !isThreadSuspended() && !isThreadRunning();
} }
//! Gets the thread affinity. //! Gets the thread affinity.
virtual uint16_t getThreadAffinity(void) const { [[nodiscard]] virtual uint16_t getThreadAffinity() const {
if (pThread) return OSGetThreadAffinity(pThread); if (pThread) return OSGetThreadAffinity(pThread);
return 0; return 0;
} }
//! Shutdown thread //! Shutdown thread
virtual void shutdownThread(void) { virtual void shutdownThread() {
//! wait for thread to finish //! wait for thread to finish
if (pThread && !(iAttributes & eAttributeDetach)) { if (pThread && !(iAttributes & eAttributeDetach)) {
while (isThreadSuspended()) { while (isThreadSuspended()) {
resumeThread(); resumeThread();
} }
OSJoinThread(pThread, NULL); OSJoinThread(pThread, nullptr);
} }
//! free the thread stack buffer //! free the thread stack buffer
if (pThreadStack) { if (pThreadStack) {
@ -116,8 +116,8 @@ public:
if (pThread) { if (pThread) {
free(pThread); free(pThread);
} }
pThread = NULL; pThread = nullptr;
pThreadStack = NULL; pThreadStack = nullptr;
} }
//! Thread attributes //! Thread attributes

View File

@ -3,8 +3,8 @@
#include "memory_mapping.h" #include "memory_mapping.h"
DECL_FUNCTION(int32_t, KiEffectiveToPhysical, uint32_t addressSpace, uint32_t virtualAddress) { DECL_FUNCTION(uint32_t, KiEffectiveToPhysical, uint32_t addressSpace, uint32_t virtualAddress) {
int32_t result = real_KiEffectiveToPhysical(addressSpace, virtualAddress); uint32_t result = real_KiEffectiveToPhysical(addressSpace, virtualAddress);
if (result == 0) { if (result == 0) {
return MemoryMapping_EffectiveToPhysical(virtualAddress); return MemoryMapping_EffectiveToPhysical(virtualAddress);
} }
@ -12,23 +12,23 @@ DECL_FUNCTION(int32_t, KiEffectiveToPhysical, uint32_t addressSpace, uint32_t vi
} }
DECL_FUNCTION(int32_t, sCheckDataRange, uint32_t address, uint32_t maxDataSize) { DECL_FUNCTION(int32_t, sCheckDataRange, uint32_t address, uint32_t maxDataSize) {
if((address & 0xF0000000) == 0x80000000){ if ((address & 0xF0000000) == 0x80000000) {
return 1; return 1;
} }
return real_sCheckDataRange(address, maxDataSize); return real_sCheckDataRange(address, maxDataSize);
} }
DECL_FUNCTION(int32_t, KiPhysicalToEffectiveCached, uint32_t addressSpace, uint32_t virtualAddress) { DECL_FUNCTION(uint32_t, KiPhysicalToEffectiveCached, uint32_t addressSpace, uint32_t virtualAddress) {
int32_t result = real_KiPhysicalToEffectiveCached(addressSpace, virtualAddress); uint32_t result = real_KiPhysicalToEffectiveCached(addressSpace, virtualAddress);
if (result == 0) { if (result == 0) {
return MemoryMapping_PhysicalToEffective(virtualAddress); return MemoryMapping_PhysicalToEffective(virtualAddress);
} }
return result; return result;
} }
DECL_FUNCTION(int32_t, KiPhysicalToEffectiveUncached, uint32_t addressSpace, uint32_t virtualAddress) { DECL_FUNCTION(uint32_t, KiPhysicalToEffectiveUncached, uint32_t addressSpace, uint32_t virtualAddress) {
int32_t result = real_KiPhysicalToEffectiveUncached(addressSpace, virtualAddress); uint32_t result = real_KiPhysicalToEffectiveUncached(addressSpace, virtualAddress);
if (result == 0) { if (result == 0) {
return MemoryMapping_PhysicalToEffective(virtualAddress); return MemoryMapping_PhysicalToEffective(virtualAddress);

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <function_patcher/function_patching.h> #include <function_patcher/function_patching.h>
#include <stdint.h> #include <cstdint>
extern function_replacement_data_t function_replacements[] __attribute__((section(".data"))); extern function_replacement_data_t function_replacements[] __attribute__((section(".data")));

View File

@ -18,7 +18,7 @@ extern "C" {
#define DEBUG_FUNCTION_LINE(FMT, ARGS...)do { \ #define DEBUG_FUNCTION_LINE(FMT, ARGS...)do { \
WHBLogPrintf("[%23s]%30s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \ WHBLogPrintf("[%23s]%30s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \
} while (0); } while (0)
#define DEBUG_FUNCTION_LINE_WRITE(FMT, ARGS...)do { \ #define DEBUG_FUNCTION_LINE_WRITE(FMT, ARGS...)do { \
WHBLogWritef("[%23s]%30s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \ WHBLogWritef("[%23s]%30s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \

View File

@ -1,6 +1,3 @@
#include <coreinit/debug.h>
#include <cstddef>
#include <malloc.h>
#include <wums.h> #include <wums.h>
#include <whb/log.h> #include <whb/log.h>
#include <whb/log_udp.h> #include <whb/log_udp.h>
@ -14,7 +11,7 @@ WUMS_MODULE_EXPORT_NAME("homebrew_memorymapping");
WUMS_MODULE_SKIP_ENTRYPOINT(); WUMS_MODULE_SKIP_ENTRYPOINT();
WUMS_MODULE_INIT_BEFORE_RELOCATION_DONE_HOOK(); WUMS_MODULE_INIT_BEFORE_RELOCATION_DONE_HOOK();
WUMS_INITIALIZE(args) { WUMS_INITIALIZE(args) {
WHBLogUdpInit(); WHBLogUdpInit();
DEBUG_FUNCTION_LINE("Setting up memory mapping!"); DEBUG_FUNCTION_LINE("Setting up memory mapping!");
static uint8_t ucSetupRequired = 1; static uint8_t ucSetupRequired = 1;
@ -37,7 +34,7 @@ WUMS_APPLICATION_STARTS() {
//MemoryMapping_CreateHeaps(); //MemoryMapping_CreateHeaps();
for (int32_t i = 0; /* waiting for a break */; i++) { for (int32_t i = 0; /* waiting for a break */; i++) {
if (mem_mapping[i].physical_addresses == NULL) { if (mem_mapping[i].physical_addresses == nullptr) {
break; break;
} }
void *address = (void *) (mem_mapping[i].effective_start_address); void *address = (void *) (mem_mapping[i].effective_start_address);

View File

@ -9,24 +9,24 @@
#include "memory.h" #include "memory.h"
#include "logger.h" #include "logger.h"
#include "CThread.h" #include "CThread.h"
#include <stdio.h> #include <cstring>
#include <string.h> #include <coreinit/debug.h>
#include <cstdio>
// #define DEBUG_FUNCTION_LINE(x,...) // #define DEBUG_FUNCTION_LINE(x,...)
void runOnAllCores(CThread::Callback callback, void *callbackArg, int32_t iAttr = 0, int32_t iPriority = 16, int32_t iStackSize = 0x8000) { void runOnAllCores(CThread::Callback callback, void *callbackArg, int32_t iAttr = 0, int32_t iPriority = 16, int32_t iStackSize = 0x8000) {
int32_t aff[] = {CThread::eAttributeAffCore2, CThread::eAttributeAffCore1, CThread::eAttributeAffCore0}; int32_t aff[] = {CThread::eAttributeAffCore2, CThread::eAttributeAffCore1, CThread::eAttributeAffCore0};
for (uint32_t i = 0; i < (sizeof(aff) / sizeof(aff[0])); i++) { for (int i: aff) {
CThread *thread = CThread::create(callback, callbackArg, iAttr | aff[i], iPriority, iStackSize); CThread *thread = CThread::create(callback, callbackArg, iAttr | i, iPriority, iStackSize);
thread->resumeThread(); thread->resumeThread();
delete thread; delete thread;
} }
} }
void writeKernelNOPs(CThread *thread, void *arg) { void writeKernelNOPs(CThread *thread, void *arg) {
uint16_t core = OSGetThreadAffinity(OSGetCurrentThread()); DEBUG_FUNCTION_LINE_VERBOSE("Writing kernel NOPs on core %d", OSGetThreadAffinity(OSGetCurrentThread()) / 2);
DEBUG_FUNCTION_LINE_VERBOSE("Writing kernel NOPs on core %d", core/2);
KernelNOPAtPhysicalAddress(0xFFF1D754); KernelNOPAtPhysicalAddress(0xFFF1D754);
KernelNOPAtPhysicalAddress(0xFFF1D64C); KernelNOPAtPhysicalAddress(0xFFF1D64C);
@ -48,17 +48,15 @@ void writeKernelNOPs(CThread *thread, void *arg) {
} }
void writeSegmentRegister(CThread *thread, void *arg) { void writeSegmentRegister(CThread *thread, void *arg) {
sr_table_t *table = (sr_table_t *) arg; auto *table = (sr_table_t *) arg;
uint16_t core = OSGetThreadAffinity(OSGetCurrentThread()); DEBUG_FUNCTION_LINE_VERBOSE("Writing segment register to core %d", OSGetThreadAffinity(OSGetCurrentThread()) / 2);
DEBUG_FUNCTION_LINE_VERBOSE("Writing segment register to core %d", core/2);
DCFlushRange(table, sizeof(sr_table_t)); DCFlushRange(table, sizeof(sr_table_t));
KernelWriteSRs(table); KernelWriteSRs(table);
} }
void readAndPrintSegmentRegister(CThread *thread, void *arg) { void readAndPrintSegmentRegister(CThread *thread, void *arg) {
uint16_t core = OSGetThreadAffinity(OSGetCurrentThread()); DEBUG_FUNCTION_LINE_VERBOSE("Reading segment register and page table from core %d", OSGetThreadAffinity(OSGetCurrentThread()) / 2);
DEBUG_FUNCTION_LINE_VERBOSE("Reading segment register and page table from core %d", core/2);
sr_table_t srTable; sr_table_t srTable;
memset(&srTable, 0, sizeof(srTable)); memset(&srTable, 0, sizeof(srTable));
@ -97,7 +95,7 @@ void MemoryMapping_searchEmptyMemoryRegions() {
DEBUG_FUNCTION_LINE("Searching for empty memory."); DEBUG_FUNCTION_LINE("Searching for empty memory.");
for (int32_t i = 0;; i++) { for (int32_t i = 0;; i++) {
if (mem_mapping[i].physical_addresses == NULL) { if (mem_mapping[i].physical_addresses == nullptr) {
break; break;
} }
uint32_t ea_start_address = mem_mapping[i].effective_start_address; uint32_t ea_start_address = mem_mapping[i].effective_start_address;
@ -114,7 +112,7 @@ void MemoryMapping_searchEmptyMemoryRegions() {
ea_size += pa_end_address - pa_start_address; ea_size += pa_end_address - pa_start_address;
} }
uint32_t *flush_start = (uint32_t *) ea_start_address; auto *flush_start = (uint32_t *) ea_start_address;
uint32_t flush_size = ea_size; uint32_t flush_size = ea_size;
DEBUG_FUNCTION_LINE("Flushing %08X (%d kB) at %08X.", flush_size, flush_size / 1024, flush_start); DEBUG_FUNCTION_LINE("Flushing %08X (%d kB) at %08X.", flush_size, flush_size / 1024, flush_start);
@ -122,14 +120,14 @@ void MemoryMapping_searchEmptyMemoryRegions() {
DEBUG_FUNCTION_LINE("Searching in memory region %d. 0x%08X - 0x%08X. Size 0x%08X (%d KBytes).", i + 1, ea_start_address, ea_start_address + ea_size, ea_size, ea_size / 1024); DEBUG_FUNCTION_LINE("Searching in memory region %d. 0x%08X - 0x%08X. Size 0x%08X (%d KBytes).", i + 1, ea_start_address, ea_start_address + ea_size, ea_size, ea_size / 1024);
bool success = true; bool success = true;
uint32_t *memory_ptr = (uint32_t *) ea_start_address; auto *memory_ptr = (uint32_t *) ea_start_address;
bool inFailRange = false; bool inFailRange = false;
uint32_t startFailing = 0; uint32_t startFailing = 0;
uint32_t startGood = ea_start_address; uint32_t startGood = ea_start_address;
for (uint32_t j = 0; j < ea_size / 4; j++) { for (uint32_t j = 0; j < ea_size / 4; j++) {
if (memory_ptr[j] != 0) { if (memory_ptr[j] != 0) {
success = false; success = false;
if (!success && !inFailRange) { if (!inFailRange) {
if ((((uint32_t) &memory_ptr[j]) - (uint32_t) startGood) / 1024 > 512) { if ((((uint32_t) &memory_ptr[j]) - (uint32_t) startGood) / 1024 > 512) {
uint32_t start_addr = startGood & 0xFFFE0000; uint32_t start_addr = startGood & 0xFFFE0000;
if (start_addr != startGood) { if (start_addr != startGood) {
@ -137,7 +135,8 @@ void MemoryMapping_searchEmptyMemoryRegions() {
} }
uint32_t end_addr = ((uint32_t) &memory_ptr[j]) - MEMORY_START_BASE; uint32_t end_addr = ((uint32_t) &memory_ptr[j]) - MEMORY_START_BASE;
end_addr = (end_addr & 0xFFFE0000); end_addr = (end_addr & 0xFFFE0000);
DEBUG_FUNCTION_LINE("+ Free between 0x%08X and 0x%08X size: %u kB", start_addr - MEMORY_START_BASE, end_addr, (((uint32_t) end_addr) - ((uint32_t) startGood - MEMORY_START_BASE)) / 1024); DEBUG_FUNCTION_LINE("+ Free between 0x%08X and 0x%08X size: %u kB", start_addr - MEMORY_START_BASE, end_addr,
(((uint32_t) end_addr) - ((uint32_t) startGood - MEMORY_START_BASE)) / 1024);
} }
startFailing = (uint32_t) &memory_ptr[j]; startFailing = (uint32_t) &memory_ptr[j];
inFailRange = true; inFailRange = true;
@ -174,7 +173,7 @@ void MemoryMapping_writeTestValuesToMemory() {
uint32_t testBuffer[chunk_size]; uint32_t testBuffer[chunk_size];
for (int32_t i = 0;; i++) { for (int32_t i = 0;; i++) {
if (mem_mapping[i].physical_addresses == NULL) { if (mem_mapping[i].physical_addresses == nullptr) {
break; break;
} }
uint32_t cur_ea_start_address = mem_mapping[i].effective_start_address; uint32_t cur_ea_start_address = mem_mapping[i].effective_start_address;
@ -204,7 +203,7 @@ void MemoryMapping_writeTestValuesToMemory() {
} }
//DEBUG_FUNCTION_LINE("testBuffer[%d] = %d",i % chunk_size,i); //DEBUG_FUNCTION_LINE("testBuffer[%d] = %d",i % chunk_size,i);
} }
uint32_t *flush_start = (uint32_t *) cur_ea_start_address; auto *flush_start = (uint32_t *) cur_ea_start_address;
uint32_t flush_size = pa_size; uint32_t flush_size = pa_size;
cur_ea_start_address += pa_size; cur_ea_start_address += pa_size;
@ -221,7 +220,7 @@ void MemoryMapping_readTestValuesFromMemory() {
DEBUG_FUNCTION_LINE("Testing reading the written values."); DEBUG_FUNCTION_LINE("Testing reading the written values.");
for (int32_t i = 0;; i++) { for (int32_t i = 0;; i++) {
if (mem_mapping[i].physical_addresses == NULL) { if (mem_mapping[i].physical_addresses == nullptr) {
break; break;
} }
uint32_t ea_start_address = mem_mapping[i].effective_start_address; uint32_t ea_start_address = mem_mapping[i].effective_start_address;
@ -238,7 +237,7 @@ void MemoryMapping_readTestValuesFromMemory() {
ea_size += pa_end_address - pa_start_address; ea_size += pa_end_address - pa_start_address;
} }
uint32_t *flush_start = (uint32_t *) ea_start_address; auto *flush_start = (uint32_t *) ea_start_address;
uint32_t flush_size = ea_size; uint32_t flush_size = ea_size;
DEBUG_FUNCTION_LINE("Flushing %08X (%d kB) at %08X to map memory.", flush_size, flush_size / 1024, flush_start); DEBUG_FUNCTION_LINE("Flushing %08X (%d kB) at %08X to map memory.", flush_size, flush_size / 1024, flush_start);
@ -246,14 +245,14 @@ void MemoryMapping_readTestValuesFromMemory() {
DEBUG_FUNCTION_LINE("Testing memory region %d. 0x%08X - 0x%08X. Size 0x%08X (%d KBytes).", i + 1, ea_start_address, ea_start_address + ea_size, ea_size, ea_size / 1024); DEBUG_FUNCTION_LINE("Testing memory region %d. 0x%08X - 0x%08X. Size 0x%08X (%d KBytes).", i + 1, ea_start_address, ea_start_address + ea_size, ea_size, ea_size / 1024);
bool success = true; bool success = true;
uint32_t *memory_ptr = (uint32_t *) ea_start_address; auto *memory_ptr = (uint32_t *) ea_start_address;
bool inFailRange = false; bool inFailRange = false;
uint32_t startFailing = 0; uint32_t startFailing = 0;
uint32_t startGood = ea_start_address; uint32_t startGood = ea_start_address;
for (uint32_t j = 0; j < ea_size / 4; j++) { for (uint32_t j = 0; j < ea_size / 4; j++) {
if (memory_ptr[j] != j) { if (memory_ptr[j] != j) {
success = false; success = false;
if (!success && !inFailRange) { if (!inFailRange) {
DEBUG_FUNCTION_LINE("+ Good between 0x%08X and 0x%08X size: %u kB", startGood, &memory_ptr[j], (((uint32_t) &memory_ptr[j]) - (uint32_t) startGood) / 1024); DEBUG_FUNCTION_LINE("+ Good between 0x%08X and 0x%08X size: %u kB", startGood, &memory_ptr[j], (((uint32_t) &memory_ptr[j]) - (uint32_t) startGood) / 1024);
startFailing = (uint32_t) &memory_ptr[j]; startFailing = (uint32_t) &memory_ptr[j];
inFailRange = true; inFailRange = true;
@ -286,7 +285,7 @@ void MemoryMapping_readTestValuesFromMemory() {
void MemoryMapping_memoryMappingForRegions(const memory_mapping_t *memory_mapping, sr_table_t SRTable, uint32_t *translation_table) { void MemoryMapping_memoryMappingForRegions(const memory_mapping_t *memory_mapping, sr_table_t SRTable, uint32_t *translation_table) {
for (int32_t i = 0; /* waiting for a break */; i++) { for (int32_t i = 0; /* waiting for a break */; i++) {
//DEBUG_FUNCTION_LINE("In loop %d",i); //DEBUG_FUNCTION_LINE("In loop %d",i);
if (memory_mapping[i].physical_addresses == NULL) { if (memory_mapping[i].physical_addresses == nullptr) {
//DEBUG_FUNCTION_LINE("break %d",i); //DEBUG_FUNCTION_LINE("break %d",i);
break; break;
} }
@ -305,7 +304,8 @@ void MemoryMapping_memoryMappingForRegions(const memory_mapping_t *memory_mappin
break; break;
} }
uint32_t pa_size = pa_end_address - pa_start_address; uint32_t pa_size = pa_end_address - pa_start_address;
DEBUG_FUNCTION_LINE_VERBOSE("Adding page table entry %d for mapping area %d. %08X-%08X => %08X-%08X...", j + 1, i + 1, cur_ea_start_address, memory_mapping[i].effective_start_address + pa_size, pa_start_address, pa_end_address); DEBUG_FUNCTION_LINE_VERBOSE("Adding page table entry %d for mapping area %d. %08X-%08X => %08X-%08X...", j + 1, i + 1, cur_ea_start_address,
memory_mapping[i].effective_start_address + pa_size, pa_start_address, pa_end_address);
if (!MemoryMapping_mapMemory(pa_start_address, pa_end_address, cur_ea_start_address, SRTable, translation_table)) { if (!MemoryMapping_mapMemory(pa_start_address, pa_end_address, cur_ea_start_address, SRTable, translation_table)) {
//log_print("error =("); //log_print("error =(");
DEBUG_FUNCTION_LINE("Failed to map memory."); DEBUG_FUNCTION_LINE("Failed to map memory.");
@ -322,9 +322,9 @@ void MemoryMapping_memoryMappingForRegions(const memory_mapping_t *memory_mappin
void MemoryMapping_setupMemoryMapping() { void MemoryMapping_setupMemoryMapping() {
// Override all writes to SR8 with nops. // Override all writes to SR8 with nops.
// Override some memory region checks inside the kernel // Override some memory region checks inside the kernel
runOnAllCores(writeKernelNOPs,NULL); runOnAllCores(writeKernelNOPs, nullptr);
//runOnAllCores(readAndPrintSegmentRegister,NULL,0,16,0x80000); //runOnAllCores(readAndPrintSegmentRegister,nullptr,0,16,0x80000);
sr_table_t srTableCpy; sr_table_t srTableCpy;
uint32_t pageTableCpy[0x8000]; uint32_t pageTableCpy[0x8000];
@ -356,7 +356,7 @@ void MemoryMapping_setupMemoryMapping() {
DEBUG_FUNCTION_LINE_VERBOSE("Writing segment registers...", segment_index, segment_content); DEBUG_FUNCTION_LINE_VERBOSE("Writing segment registers...", segment_index, segment_content);
// Writing the segment registers to ALL cores. // Writing the segment registers to ALL cores.
// //
//writeSegmentRegister(NULL, &srTableCpy); //writeSegmentRegister(nullptr, &srTableCpy);
runOnAllCores(writeSegmentRegister, &srTableCpy); runOnAllCores(writeSegmentRegister, &srTableCpy);
@ -372,7 +372,7 @@ void MemoryMapping_setupMemoryMapping() {
//printPageTableTranslation(srTableCpy,pageTableCpy); //printPageTableTranslation(srTableCpy,pageTableCpy);
//runOnAllCores(readAndPrintSegmentRegister,NULL,0,16,0x80000); //runOnAllCores(readAndPrintSegmentRegister,nullptr,0,16,0x80000);
//searchEmptyMemoryRegions(); //searchEmptyMemoryRegions();
@ -383,14 +383,16 @@ void MemoryMapping_setupMemoryMapping() {
} }
void *MemoryMapping_alloc(uint32_t size, uint32_t align) { void *MemoryMapping_alloc(uint32_t size, uint32_t align) {
void *res = NULL; void *res = nullptr;
for (int32_t i = 0; /* waiting for a break */; i++) { for (int32_t i = 0; /* waiting for a break */; i++) {
if (mem_mapping[i].physical_addresses == NULL) { if (mem_mapping[i].physical_addresses == nullptr) {
break; break;
} }
MEMHeapHandle heapHandle = (MEMHeapHandle) mem_mapping[i].effective_start_address; auto heapHandle = (MEMHeapHandle) mem_mapping[i].effective_start_address;
MEMExpHeap *heap = (MEMExpHeap *) heapHandle; auto *heap = (MEMExpHeap *) heapHandle;
OSUninterruptibleSpinLock_Acquire(&heap->header.lock); auto header = (MEMHeapHeader *) heap;
OSUninterruptibleSpinLock_Acquire(&header->lock);
res = MEMAllocFromExpHeapEx(heapHandle, size, align); res = MEMAllocFromExpHeapEx(heapHandle, size, align);
auto cur = heap->usedList.head; auto cur = heap->usedList.head;
while (cur != nullptr) { while (cur != nullptr) {
@ -402,7 +404,7 @@ void *MemoryMapping_alloc(uint32_t size, uint32_t align) {
DCFlushRange(cur, sizeof(MEMExpHeapBlock)); DCFlushRange(cur, sizeof(MEMExpHeapBlock));
cur = cur->next; cur = cur->next;
} }
OSUninterruptibleSpinLock_Release(&heap->header.lock); OSUninterruptibleSpinLock_Release(&header->lock);
if (res != nullptr) { if (res != nullptr) {
break; break;
} }
@ -411,19 +413,19 @@ void *MemoryMapping_alloc(uint32_t size, uint32_t align) {
} }
void *MemoryMapping_allocVideoMemory(uint32_t size, uint32_t align) { void *MemoryMapping_allocVideoMemory(uint32_t size, uint32_t align) {
void *res = NULL; void *res = nullptr;
for (int32_t i = 0; /* waiting for a break */; i++) { for (int32_t i = 0; /* waiting for a break */; i++) {
if (mem_mapping[i].physical_addresses == NULL) { if (mem_mapping[i].physical_addresses == nullptr) {
break; break;
} }
uint32_t effectiveAddress = mem_mapping[i].effective_start_address; uint32_t effectiveAddress = mem_mapping[i].effective_start_address;
// Skip non-video memory // Skip non-video memory
if(effectiveAddress < MEMORY_START_VIDEO || effectiveAddress > MEMORY_END_VIDEO){ if (effectiveAddress < MEMORY_START_VIDEO || effectiveAddress > MEMORY_END_VIDEO) {
continue; continue;
} }
res = MEMAllocFromExpHeapEx((MEMHeapHandle) mem_mapping[i].effective_start_address, size, align); res = MEMAllocFromExpHeapEx((MEMHeapHandle) mem_mapping[i].effective_start_address, size, align);
if (res != NULL) { if (res != nullptr) {
break; break;
} }
} }
@ -432,18 +434,20 @@ void *MemoryMapping_allocVideoMemory(uint32_t size, uint32_t align) {
void MemoryMapping_free(void *ptr) { void MemoryMapping_free(void *ptr) {
if (ptr == NULL) { if (ptr == nullptr) {
return; return;
} }
uint32_t ptr_val = (uint32_t) ptr; auto ptr_val = (uint32_t) ptr;
for (int32_t i = 0; /* waiting for a break */; i++) { for (int32_t i = 0; /* waiting for a break */; i++) {
if (mem_mapping[i].physical_addresses == NULL) { if (mem_mapping[i].physical_addresses == nullptr) {
break; break;
} }
if (ptr_val > mem_mapping[i].effective_start_address && ptr_val < mem_mapping[i].effective_end_address) { if (ptr_val > mem_mapping[i].effective_start_address && ptr_val < mem_mapping[i].effective_end_address) {
MEMHeapHandle heapHandle = (MEMHeapHandle) mem_mapping[i].effective_start_address; auto heapHandle = (MEMHeapHandle) mem_mapping[i].effective_start_address;
MEMExpHeap *heap = (MEMExpHeap *) heapHandle; auto *heap = (MEMExpHeap *) heapHandle;
OSUninterruptibleSpinLock_Acquire(&heap->header.lock); auto *header = (MEMHeapHeader *) heapHandle;
OSUninterruptibleSpinLock_Acquire(&header->lock);
MEMFreeToExpHeap((MEMHeapHandle) mem_mapping[i].effective_start_address, ptr); MEMFreeToExpHeap((MEMHeapHandle) mem_mapping[i].effective_start_address, ptr);
auto cur = heap->usedList.head; auto cur = heap->usedList.head;
while (cur != nullptr) { while (cur != nullptr) {
@ -455,7 +459,7 @@ void MemoryMapping_free(void *ptr) {
DCFlushRange(cur, sizeof(MEMExpHeapBlock)); DCFlushRange(cur, sizeof(MEMExpHeapBlock));
cur = cur->next; cur = cur->next;
} }
OSUninterruptibleSpinLock_Release(&heap->header.lock); OSUninterruptibleSpinLock_Release(&header->lock);
break; break;
} }
} }
@ -468,7 +472,7 @@ uint32_t MemoryMapping_MEMGetAllocatableSize() {
uint32_t MemoryMapping_MEMGetAllocatableSizeEx(uint32_t align) { uint32_t MemoryMapping_MEMGetAllocatableSizeEx(uint32_t align) {
uint32_t res = 0; uint32_t res = 0;
for (int32_t i = 0; /* waiting for a break */; i++) { for (int32_t i = 0; /* waiting for a break */; i++) {
if (mem_mapping[i].physical_addresses == NULL) { if (mem_mapping[i].physical_addresses == nullptr) {
break; break;
} }
uint32_t curRes = MEMGetAllocatableSizeForExpHeapEx((MEMHeapHandle) mem_mapping[i].effective_start_address, align); uint32_t curRes = MEMGetAllocatableSizeForExpHeapEx((MEMHeapHandle) mem_mapping[i].effective_start_address, align);
@ -483,7 +487,7 @@ uint32_t MemoryMapping_MEMGetAllocatableSizeEx(uint32_t align) {
uint32_t MemoryMapping_GetFreeSpace() { uint32_t MemoryMapping_GetFreeSpace() {
uint32_t res = 0; uint32_t res = 0;
for (int32_t i = 0; /* waiting for a break */; i++) { for (int32_t i = 0; /* waiting for a break */; i++) {
if (mem_mapping[i].physical_addresses == NULL) { if (mem_mapping[i].physical_addresses == nullptr) {
break; break;
} }
uint32_t curRes = MEMGetTotalFreeSizeForExpHeap((MEMHeapHandle) mem_mapping[i].effective_start_address); uint32_t curRes = MEMGetTotalFreeSizeForExpHeap((MEMHeapHandle) mem_mapping[i].effective_start_address);
@ -495,7 +499,7 @@ uint32_t MemoryMapping_GetFreeSpace() {
void MemoryMapping_CreateHeaps() { void MemoryMapping_CreateHeaps() {
for (int32_t i = 0; /* waiting for a break */; i++) { for (int32_t i = 0; /* waiting for a break */; i++) {
if (mem_mapping[i].physical_addresses == NULL) { if (mem_mapping[i].physical_addresses == nullptr) {
break; break;
} }
void *address = (void *) (mem_mapping[i].effective_start_address); void *address = (void *) (mem_mapping[i].effective_start_address);
@ -509,7 +513,7 @@ void MemoryMapping_CreateHeaps() {
void MemoryMapping_DestroyHeaps() { void MemoryMapping_DestroyHeaps() {
for (int32_t i = 0; /* waiting for a break */; i++) { for (int32_t i = 0; /* waiting for a break */; i++) {
if (mem_mapping[i].physical_addresses == NULL) { if (mem_mapping[i].physical_addresses == nullptr) {
break; break;
} }
void *address = (void *) (mem_mapping[i].effective_start_address); void *address = (void *) (mem_mapping[i].effective_start_address);
@ -579,7 +583,7 @@ uint32_t MemoryMapping_getAreaSizeFromPageTable(uint32_t start, uint32_t maxSize
bool MemoryMapping_getPageEntryForAddress(uint32_t SDR1, uint32_t addr, uint32_t vsid, uint32_t *translation_table, uint32_t *oPTEH, uint32_t *oPTEL, bool checkSecondHash) { bool MemoryMapping_getPageEntryForAddress(uint32_t SDR1, uint32_t addr, uint32_t vsid, uint32_t *translation_table, uint32_t *oPTEH, uint32_t *oPTEL, bool checkSecondHash) {
uint32_t pageMask = SDR1 & 0x1FF; uint32_t pageMask = SDR1 & 0x1FF;
uint32_t pageIndex = (addr >> PAGE_INDEX_SHIFT) & PAGE_INDEX_MASK; uint32_t pageIndex = (addr >> PAGE_INDEX_SHIFT) & PAGE_INDEX_MASK;
uint32_t primaryHash = (vsid & 0x7FFFF) ^pageIndex; uint32_t primaryHash = (vsid & 0x7FFFF) ^ pageIndex;
if (MemoryMapping_getPageEntryForAddressEx(SDR1, addr, vsid, primaryHash, translation_table, oPTEH, oPTEL, 0)) { if (MemoryMapping_getPageEntryForAddressEx(SDR1, addr, vsid, primaryHash, translation_table, oPTEH, oPTEL, 0)) {
return true; return true;
@ -712,10 +716,10 @@ void MemoryMapping_printPageTableTranslation(sr_table_t srTable, uint32_t *trans
const char *access1[] = {"read/write", "read/write", "read/write", "read only"}; const char *access1[] = {"read/write", "read/write", "read/write", "read only"};
const char *access2[] = {"no access", "read only", "read/write", "read only"}; const char *access2[] = {"no access", "read only", "read/write", "read only"};
for (std::vector<pageInformation>::iterator it = pageInfos.begin(); it != pageInfos.end(); ++it) { for (auto cur: pageInfos) {
pageInformation cur = *it; DEBUG_FUNCTION_LINE_VERBOSE("%08X %08X -> %08X %08X. user access %s. supervisor access %s. %s", cur.addr, cur.addr + cur.size, cur.phys, cur.phys + cur.size,
DEBUG_FUNCTION_LINE_VERBOSE("%08X %08X -> %08X %08X. user access %s. supervisor access %s. %s", cur.addr, cur.addr + cur.size, cur.phys, cur.phys + cur.size, cur.kp ? access2[cur.pp] : access1[cur.pp], cur.kp ? access2[cur.pp] : access1[cur.pp],
cur.ks ? access2[cur.pp] : access1[cur.pp], cur.nx ? "not executable" : "executable"); cur.ks ? access2[cur.pp] : access1[cur.pp], cur.nx ? "not executable" : "executable");
} }
} }
@ -766,7 +770,7 @@ bool MemoryMapping_mapMemory(uint32_t pa_start_address, uint32_t pa_end_address,
uint32_t primary_hash = (VSID & 0x7FFFF); uint32_t primary_hash = (VSID & 0x7FFFF);
uint32_t hashvalue1 = primary_hash ^page_index; uint32_t hashvalue1 = primary_hash ^ page_index;
// hashvalue 2 is the complement of the first hash. // hashvalue 2 is the complement of the first hash.
uint32_t hashvalue2 = ~hashvalue1; uint32_t hashvalue2 = ~hashvalue1;
@ -792,7 +796,7 @@ bool MemoryMapping_mapMemory(uint32_t pa_start_address, uint32_t pa_end_address,
uint32_t pteh = translation_table[index]; uint32_t pteh = translation_table[index];
// Check if it's already taken. The first bit indicates if the PTE-slot inside // Check if it's already taken. The first bit indicates if the PTE-slot inside
// this group is already taken. // this group is already taken.
if ((pteh == 0)) { if (pteh == 0) {
// If we found a free slot, set the PTEH and PTEL value. // If we found a free slot, set the PTEH and PTEL value.
//DEBUG_FUNCTION_LINE("Used slot %d. PTEGaddr1 %08X addr %08X",j+1,PTEGaddr1 - (HTABORG << 16),PTEGoffset); //DEBUG_FUNCTION_LINE("Used slot %d. PTEGaddr1 %08X addr %08X",j+1,PTEGaddr1 - (HTABORG << 16),PTEGoffset);
translation_table[index] = PTEH; translation_table[index] = PTEH;
@ -819,7 +823,7 @@ bool MemoryMapping_mapMemory(uint32_t pa_start_address, uint32_t pa_end_address,
int32_t index = (PTEGoffset / 4); int32_t index = (PTEGoffset / 4);
uint32_t pteh = translation_table[index]; uint32_t pteh = translation_table[index];
//Check if it's already taken. //Check if it's already taken.
if ((pteh == 0)) { if (pteh == 0) {
translation_table[index] = PTEH; translation_table[index] = PTEH;
translation_table[index + 1] = PTEL; translation_table[index + 1] = PTEL;
setSuccessfully = true; setSuccessfully = true;
@ -846,10 +850,10 @@ uint32_t MemoryMapping_PhysicalToEffective(uint32_t phyiscalAddress) {
} }
uint32_t result = 0; uint32_t result = 0;
const memory_values_t *curMemValues = NULL; const memory_values_t *curMemValues = nullptr;
//iterate through all own mapped memory regions //iterate through all own mapped memory regions
for (int32_t i = 0; true; i++) { for (int32_t i = 0; true; i++) {
if (mem_mapping[i].physical_addresses == NULL) { if (mem_mapping[i].physical_addresses == nullptr) {
break; break;
} }
@ -879,11 +883,11 @@ uint32_t MemoryMapping_EffectiveToPhysical(uint32_t effectiveAddress) {
uint32_t result = 0; uint32_t result = 0;
// CAUTION: The data may be fragmented between multiple areas in PA. // CAUTION: The data may be fragmented between multiple areas in PA.
const memory_values_t *curMemValues = NULL; const memory_values_t *curMemValues = nullptr;
uint32_t curOffset = 0; uint32_t curOffset = 0;
for (int32_t i = 0; true; i++) { for (int32_t i = 0; true; i++) {
if (mem_mapping[i].physical_addresses == NULL) { if (mem_mapping[i].physical_addresses == nullptr) {
break; break;
} }
if (effectiveAddress >= mem_mapping[i].effective_start_address && effectiveAddress < mem_mapping[i].effective_end_address) { if (effectiveAddress >= mem_mapping[i].effective_start_address && effectiveAddress < mem_mapping[i].effective_end_address) {
@ -893,7 +897,7 @@ uint32_t MemoryMapping_EffectiveToPhysical(uint32_t effectiveAddress) {
} }
} }
if (curMemValues == NULL) { if (curMemValues == nullptr) {
return result; return result;
} }

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include <stddef.h> #include <cstddef>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -84,7 +84,7 @@ const memory_values_t mem_vals_video[] = {
// void * heap = (void*) SharedReadHeapTrackingAddr - [delta due mapping e.g (0xF8000000 + 0x80000000)]; // void * heap = (void*) SharedReadHeapTrackingAddr - [delta due mapping e.g (0xF8000000 + 0x80000000)];
// int size = 0x20000; // value have to be a multiple of 0x20000; // int size = 0x20000; // value have to be a multiple of 0x20000;
// while(true){ // while(true){
// void * outPtr = NULL; // void * outPtr = nullptr;
// if(TinyHeap_Alloc(heap,size, 0x20000,&outPtr) == 0){ // value have to be a multiple of 0x20000; // if(TinyHeap_Alloc(heap,size, 0x20000,&outPtr) == 0){ // value have to be a multiple of 0x20000;
// DEBUG_FUNCTION_LINE("Allocated %d kb on heap %08X (PA %08X)\n",size/1024,(uint32_t)outPtr, OSEffectiveToPhysical(outPtr)); // DEBUG_FUNCTION_LINE("Allocated %d kb on heap %08X (PA %08X)\n",size/1024,(uint32_t)outPtr, OSEffectiveToPhysical(outPtr));
// TinyHeap_Free(heap, outPtr); // TinyHeap_Free(heap, outPtr);
@ -115,7 +115,7 @@ const memory_mapping_t mem_mapping[] = {
{MEMORY_HEAP2, MEMORY_HEAP3, mem_vals_heap_3}, {MEMORY_HEAP2, MEMORY_HEAP3, mem_vals_heap_3},
{MEMORY_HEAP3, MEMORY_HEAP4, mem_vals_heap_4}, {MEMORY_HEAP3, MEMORY_HEAP4, mem_vals_heap_4},
{MEMORY_START_VIDEO, MEMORY_END_VIDEO, mem_vals_video}, {MEMORY_START_VIDEO, MEMORY_END_VIDEO, mem_vals_video},
{0, 0, NULL} {0, 0, nullptr}
}; };
@ -160,7 +160,7 @@ const memory_values_t mem_vals_heap[] = {
// overridden. // overridden.
// {0x28000000 + 0x00000000, 0x28000000 + 0x0A000000}, // // {0x28000000 + 0x00000000, 0x28000000 + 0x0A000000}, //
{0, 0} {0, 0}
}; };