From 64362cb41728c8b5e751361c569e3ecccb2b3734 Mon Sep 17 00:00:00 2001 From: valentinbreiz Date: Tue, 8 Feb 2022 20:55:55 +0000 Subject: [PATCH 1/2] Disable interrupt when touching memory --- source/Cosmos.Core/Memory/Heap.cs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/source/Cosmos.Core/Memory/Heap.cs b/source/Cosmos.Core/Memory/Heap.cs index 4d6c3a4e51..9c3d3fd403 100644 --- a/source/Cosmos.Core/Memory/Heap.cs +++ b/source/Cosmos.Core/Memory/Heap.cs @@ -39,17 +39,25 @@ public static unsafe void Init() /// Byte pointer to the start of the block. public static byte* Alloc(uint aSize) { + CPU.DisableInterrupts(); + if (aSize <= HeapSmall.mMaxItemSize) { - return HeapSmall.Alloc((ushort)aSize); + byte* ptr = HeapSmall.Alloc((ushort)aSize); + CPU.EnableInterrupts(); + return ptr; } else if (aSize <= HeapMedium.MaxItemSize) { - return HeapMedium.Alloc(aSize); + byte* ptr = HeapMedium.Alloc(aSize); + CPU.EnableInterrupts(); + return ptr; } else { - return HeapLarge.Alloc(aSize); + byte* ptr = HeapLarge.Alloc(aSize); + CPU.EnableInterrupts(); + return ptr; } } @@ -77,6 +85,8 @@ public static uint SafeAlloc(uint aSize) /// public static void Free(void* aPtr) { + CPU.DisableInterrupts(); + //TODO find a better way to remove the double look up here for GetPageType and then again in the // .Free methods which actually free the entries in the RAT. //Debugger.DoSendNumber(0x77); @@ -95,6 +105,8 @@ public static void Free(void* aPtr) default: throw new Exception("Heap item not found in RAT."); } + + CPU.EnableInterrupts(); } /// From 3fdb336988a789df3c9a8d8165d35b4c260dbfee Mon Sep 17 00:00:00 2001 From: valentinbreiz Date: Wed, 9 Feb 2022 16:19:38 +0000 Subject: [PATCH 2/2] Disable interrupts in Heap.Collect, not free --- source/Cosmos.Core/Memory/Heap.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/source/Cosmos.Core/Memory/Heap.cs b/source/Cosmos.Core/Memory/Heap.cs index 9c3d3fd403..69fee9c49a 100644 --- a/source/Cosmos.Core/Memory/Heap.cs +++ b/source/Cosmos.Core/Memory/Heap.cs @@ -85,8 +85,6 @@ public static uint SafeAlloc(uint aSize) /// public static void Free(void* aPtr) { - CPU.DisableInterrupts(); - //TODO find a better way to remove the double look up here for GetPageType and then again in the // .Free methods which actually free the entries in the RAT. //Debugger.DoSendNumber(0x77); @@ -105,8 +103,6 @@ public static void Free(void* aPtr) default: throw new Exception("Heap item not found in RAT."); } - - CPU.EnableInterrupts(); } /// @@ -115,6 +111,9 @@ public static void Free(void* aPtr) /// Number of objects freed public static int Collect() { + //Disable interrupts: Prevent CPU exception when allocation is called from interrupt code + CPU.DisableInterrupts(); + // Mark and sweep objects from roots // 1. Check if a page is in use if medium/large mark and sweep object // 2. Go throught the SMT table for small objects and go through pages by size @@ -234,6 +233,9 @@ public static int Collect() rootSMTPtr = rootSMTPtr->LargerSize; } + + //Enable interrupts back + CPU.EnableInterrupts(); return freed; }