Skip to content

Commit

Permalink
First Work on new Memory Management
Browse files Browse the repository at this point in the history
  • Loading branch information
tiger committed Apr 15, 2021
1 parent 54d821f commit a9d5525
Show file tree
Hide file tree
Showing 11 changed files with 697 additions and 614 deletions.
871 changes: 433 additions & 438 deletions Setup/Cosmos.iss

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions source/Cosmos.Core/CPU.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ public void Reboot()
/// </summary>
public static void EnableInterrupts()
{
mInterruptsEnabled = true;
DoEnableInterrupts();
mInterruptsEnabled = false;
//DoEnableInterrupts();
}

/// <summary>
Expand Down
156 changes: 90 additions & 66 deletions source/Cosmos.Core/GCImplementation.cs
Original file line number Diff line number Diff line change
@@ -1,66 +1,90 @@
#if DEBUG
//#define GC_DEBUG
#endif
using System;
using System.Diagnostics;
using IL2CPU.API;
using IL2CPU.API.Attribs;

namespace Cosmos.Core
{
/// <summary>
/// GCImplementation class. Garbage collector. Mostly not implemented.
/// </summary>
/// <remarks>Most of the class is yet to be implemented.</remarks>
[DebuggerStepThrough]
public static class GCImplementation
{
/// <summary>
/// Acquire lock. Not implemented.
/// </summary>
/// <exception cref="NotImplementedException">Thrown always.</exception>
private static void AcquireLock()
{
throw new NotImplementedException();
}

/// <summary>
/// Release lock. Not implemented.
/// </summary>
/// <exception cref="NotImplementedException">Thrown always.</exception>
private static void ReleaseLock()
{
throw new NotImplementedException();
}

/// <summary>
/// Alloc new object. Plugged.
/// </summary>
[PlugMethod(PlugRequired = true)]
public static uint AllocNewObject(uint aSize)
{
throw new NotImplementedException();

}

/// <summary>
/// Increase reference count of an given object. Plugged.
/// </summary>
/// <param name="aObject">An object to increase to reference count of.</param>
/// <exception cref="NotImplementedException">Thrown on fatal error, contact support.</exception>
public static unsafe void IncRefCount(uint aObject)
{
throw new NotImplementedException();
}

/// <summary>
/// Decrease reference count of an given object. Plugged.
/// </summary>
/// <param name="aObject">An object to decrease to reference count of.</param>
/// <exception cref="NotImplementedException">Thrown on fatal error, contact support.</exception>
public static unsafe void DecRefCount(uint aObject)
{
throw new NotImplementedException();
}
}
}
#if DEBUG
//#define GC_DEBUG
#endif
using System;
using System.Diagnostics;
using IL2CPU.API;
using IL2CPU.API.Attribs;

namespace Cosmos.Core
{
/// <summary>
/// GCImplementation class. Garbage collector. Mostly not implemented.
/// </summary>
/// <remarks>Most of the class is yet to be implemented.</remarks>
[DebuggerStepThrough]
public static class GCImplementation
{
private static bool isInitialized;
/// <summary>
/// Acquire lock. Not implemented.
/// </summary>
/// <exception cref="NotImplementedException">Thrown always.</exception>
private static void AcquireLock()
{
throw new NotImplementedException();
}

/// <summary>
/// Release lock. Not implemented.
/// </summary>
/// <exception cref="NotImplementedException">Thrown always.</exception>
private static void ReleaseLock()
{
throw new NotImplementedException();
}

/// <summary>
/// Alloc new object.
/// </summary>
public unsafe static uint AllocNewObject(uint aSize)
{


if (!isInitialized)
{
isInitialized = true;
Init();
return (uint)Memory.Heap.Alloc(aSize);
}

else
{
return (uint)Memory.Heap.Alloc(aSize);
}

}

/// <summary>
/// Increase reference count of an given object. Plugged.
/// </summary>
/// <param name="aObject">An object to increase to reference count of.</param>
/// <exception cref="NotImplementedException">Thrown on fatal error, contact support.</exception>
public static unsafe void IncRefCount(uint aObject)
{
throw new NotImplementedException();
}

/// <summary>
/// Decrease reference count of an given object. Plugged.
/// </summary>
/// <param name="aObject">An object to decrease to reference count of.</param>
/// <exception cref="NotImplementedException">Thrown on fatal error, contact support.</exception>
public static unsafe void DecRefCount(uint aObject)
{
throw new NotImplementedException();
}

public static unsafe void Init()
{


byte* memPtr = (byte*)CPU.GetEndOfKernel();
memPtr += Memory.RAT.PageSize - (uint)memPtr % Memory.RAT.PageSize;
Debug.Kernel.Debugger.DoSendNumber((uint)CPU.GetMemoryMap()[3].Length - (128 * 1024 * 1024));
Memory.RAT.Init(memPtr, (uint)CPU.GetMemoryMap()[3].Length - (128 * 1024 * 1024));

}

}
}
135 changes: 78 additions & 57 deletions source/Cosmos.Core/Memory/HeapLarge.cs
Original file line number Diff line number Diff line change
@@ -1,57 +1,78 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Native = System.UInt32;

namespace Cosmos.Core.Memory
{
/// <summary>
/// HeapLarge class. Used to alloc and free large memory blocks on the heap.
/// </summary>
unsafe static public class HeapLarge
{
/// <summary>
/// Prefix block. Used to store meta information.
/// </summary>
public const Native PrefixBytes = 4 * sizeof(Native);

/// <summary>
/// Init HeapLarge instance.
/// </summary>
/// <remarks>Empty function</remarks>
static public void Init()
{
}

/// <summary>
/// Alloc memory block, of a given size.
/// </summary>
/// <param name="aSize">A size of block to alloc, in bytes.</param>
/// <returns>Byte pointer to the start of the block.</returns>
static public byte* Alloc(Native aSize)
{
Native xPages = (Native)((aSize + PrefixBytes) / RAT.PageSize) + 1;
var xPtr = (Native*)RAT.AllocPages(RAT.PageType.HeapLarge, xPages);

xPtr[0] = xPages * RAT.PageSize - PrefixBytes; // Allocated data size
xPtr[1] = aSize; // Actual data size
xPtr[2] = 0; // Ref count
xPtr[3] = 0; // Ptr to first

return (byte*)xPtr + PrefixBytes;
}

/// <summary>
/// Free block.
/// </summary>
/// <param name="aPtr">A pointer to the block.</param>
/// <exception cref="Exception">Thrown if page type is not found.</exception>
static public void Free(void* aPtr)
{
// TODO - Should check the page type before freeing to make sure it is a Large?
// or just trust the caller to avoid adding overhead?
var xPageIdx = RAT.GetFirstRAT(aPtr);
RAT.Free(xPageIdx);
}
}
}
using System;
using System.Linq;
using System.Threading.Tasks;
using Native = System.UInt32;

namespace Cosmos.Core.Memory
{
/// <summary>
/// HeapLarge class. Used to alloc and free large memory blocks on the heap.
/// </summary>
unsafe static public class HeapLarge
{
/// <summary>
/// Prefix block. Used to store meta information.
/// </summary>
public const Native PrefixBytes = 4 * sizeof(Native);

/// <summary>
/// Init HeapLarge instance.
/// </summary>
/// <remarks>Empty function</remarks>
static public void Init()
{
}

/// <summary>
/// Alloc memory block, of a given size.
/// </summary>
/// <param name="aSize">A size of block to alloc, in bytes.</param>
/// <returns>Byte pointer to the start of the block.</returns>
/// Debug.Kernel.Debugger.DoSendNumber(PrefixBytes);
//Debug.Kernel.Debugger.DoSendNumber((uint) xPtr);
static public byte* Alloc(Native aSize)
{
//Debug.Kernel.Debugger.DoSendNumber(aSize);
//Debug.Kernel.Debugger.DoBochsBreak();
Native xPages = (Native)((aSize + PrefixBytes) / RAT.PageSize) + 1;
if(xPages == 0)
{
//Debug.Kernel.Debugger.DoSendNumber(xPages);
//Debug.Kernel.Debugger.DoBochsBreak();
}
var xPtr = (Native*)RAT.AllocPages(RAT.PageType.HeapLarge, xPages);
Debug.Kernel.Debugger.DoSendNumber((uint)xPtr);
if ((uint)xPtr == 0)
{

// Debug.Kernel.Debugger.DoSendNumber((uint)xPtr);
//Debug.Kernel.Debugger.DoSendNumber(2);
// Debug.Kernel.Debugger.DoBochsBreak();
}
if(PrefixBytes == 0)
{
//Debug.Kernel.Debugger.DoSendNumber(3);
//Debug.Kernel.Debugger.DoBochsBreak();
}
xPtr[0] = xPages * RAT.PageSize - PrefixBytes; // Allocated data size
xPtr[1] = aSize; // Actual data size
xPtr[2] = 0; // Ref count
xPtr[3] = 0; // Ptr to first,
//Debug.Kernel.Debugger.DoSendNumber((uint)xPtr + PrefixBytes);
return (byte*)xPtr + PrefixBytes;
}

/// <summary>
/// Free block.
/// </summary>
/// <param name="aPtr">A pointer to the block.</param>
/// <exception cref="Exception">Thrown if page type is not found.</exception>
static public void Free(void* aPtr)
{
// TODO - Should check the page type before freeing to make sure it is a Large?
// or just trust the caller to avoid adding overhead?
var xPageIdx = RAT.GetFirstRAT(aPtr);
RAT.Free(xPageIdx);
}
}
}
1 change: 1 addition & 0 deletions source/Cosmos.Core/Memory/HeapMedium.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ static public void Init()
/// <returns>Byte pointer to the start of the block.</returns>
static public byte* Alloc(Native aSize)
{
Cosmos.Debug.Kernel.Debugger.DoSendNumber(MaxItemSize);
return HeapLarge.Alloc(aSize);
}

Expand Down
13 changes: 9 additions & 4 deletions source/Cosmos.Core/Memory/HeapSmall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ static public void Init()
static void InitSMT(Native aMaxItemSize)
{
mMaxItemSize = aMaxItemSize;
mSMT = (void**)RAT.AllocBytes(RAT.PageType.HeapSmall, mMaxItemSize * (Native)sizeof(void*));
// Cosmos.Debug.Kernel.Debugger.DoSendNumber(mMaxItemSize);
mSMT = (void**)RAT.AllocBytes(RAT.PageType.HeapSmall, mMaxItemSize * 8,true);
// Cosmos.Debug.Kernel.Debugger.DoSendNumber((uint)mSMT);
// Cosmos.Debug.Kernel.Debugger.DoBochsBreak();
// Debug.Kernel.Debugger.SendKernelPanic(06);
}

/// <summary>
Expand Down Expand Up @@ -101,7 +105,7 @@ static void CreatePage(Native aItemSize)
// In future may put some up top and be larger as well.
Native xPages = 1;
var xPtr = (Native*)RAT.AllocPages(RAT.PageType.HeapSmall, xPages);

// Cosmos.Debug.Kernel.Debugger.DoSendNumber((uint)xPtr);
// Ptr to next page of same size
xPtr[0] = 0;
//
Expand All @@ -113,8 +117,9 @@ static void CreatePage(Native aItemSize)
// # of free slots
xPtr[2] = xItemCount;
//
xPtr = xPtr + 3;

xPtr = xPtr + 3;
// Cosmos.Debug.Kernel.Debugger.DoSendNumber(mMaxItemSize);
// Cosmos.Debug.Kernel.Debugger.DoSendNumber((uint)xPtr);
for (Native i = 0; i < xItemCount; i++)
{
byte* xSlotPtr = (byte*)xPtr + i * xSlotSize;
Expand Down
Loading

0 comments on commit a9d5525

Please sign in to comment.