public static unsafe void Throw(FlingOops.Exception ex) { FlingOops.GC.IncrementRefCount(ex); BasicConsole.WriteLine("Exception thrown"); BasicConsole.WriteLine(ex.Message); if (State->CurrentHandlerPtr->Ex != null) { //GC ref count remains consistent because the Ex pointer below is going to be replaced but // same pointer stored in InnerException. // Result is ref count goes: +1 here, -1 below ex.InnerException = (FlingOops.Exception)Utilities.ObjectUtilities.GetObject(State->CurrentHandlerPtr->Ex); } if (ex.InstructionAddress == 0) { ex.InstructionAddress = *((uint *)BasePointer + 1); } State->CurrentHandlerPtr->Ex = Utilities.ObjectUtilities.GetHandle(ex); State->CurrentHandlerPtr->ExPending = 1; HandleException(); // We never expect to get here... HaltReason = "HandleException returned!"; BasicConsole.WriteLine(HaltReason); // Try to cause fault *((byte *)0xDEADBEEF) = 0; }
public static void Init() { //Colour info stored in the high byte: // Hi-4-bits: Background // Lo-4-bits: Foreground //Black background bg_colour = (char)0x0000; //White foreground default_colour = (char)0x0F00; //Yellow foreground warning_colour = (char)0x0E00; //Red foreground error_colour = (char)0x0400; //Green foreground success_colour = (char)0x0200; //Background | Foreground colour = (char)(bg_colour | default_colour); Initialised = true; #if MIPS //Reset output (/terminal) colour BasicConsole.SetTextColour(BasicConsole.default_colour); #endif }
public static void Throw_IndexOutOfRangeException() { HaltReason = "Index out of range exception."; BasicConsole.WriteLine(HaltReason); //FlingOops.Exception ex = new FlingOops.Exceptions.IndexOutOfRangeException(0, 0); //ex.InstructionAddress = *((uint*)BasePointer + 1); //Throw(ex); }
public static void *NewObj(FlingOops.Type theType) { if (!Enabled) { BasicConsole.SetTextColour(BasicConsole.warning_colour); BasicConsole.WriteLine("Warning! GC returning null pointer because GC not enabled."); BasicConsole.DelayOutput(10); BasicConsole.SetTextColour(BasicConsole.default_colour); return(null); } //try //{ InsideGC = true; //Alloc space for GC header that prefixes object data //Alloc space for new object uint totalSize = theType.Size; totalSize += (uint)sizeof(GCHeader); GCHeader *newObjPtr = (GCHeader *)Heap.AllocZeroed(totalSize, "GC : NewObject"); if ((UInt32)newObjPtr == 0) { InsideGC = false; BasicConsole.SetTextColour(BasicConsole.error_colour); BasicConsole.WriteLine("Error! GC can't create a new object because the heap returned a null pointer."); BasicConsole.DelayOutput(10); BasicConsole.SetTextColour(BasicConsole.default_colour); return(null); } NumObjs++; //Initialise the GCHeader SetSignature(newObjPtr); newObjPtr->RefCount = 1; //Initialise the object _Type field FlingOops.ObjectWithType newObj = (FlingOops.ObjectWithType)Utilities.ObjectUtilities.GetObject(newObjPtr + 1); newObj._Type = theType; //Move past GCHeader byte *newObjBytePtr = (byte *)(newObjPtr + 1); InsideGC = false; return(newObjBytePtr); //} //finally //{ //} }
public static void Throw_NullReferenceException(uint address) { HaltReason = "Null reference exception. Instruction: 0x "; FillString(address, 48, HaltReason); BasicConsole.WriteLine(HaltReason); //FlingOops.Exception ex = new FlingOops.Exceptions.NullReferenceException(); //ex.InstructionAddress = address; //Throw(ex); }
public static unsafe void HandleEndFinally() { if (State == null || State->CurrentHandlerPtr == null) { // If we get to here, it's an unhandled exception HaltReason = "Cannot end finally on null handler!"; BasicConsole.WriteLine(HaltReason); BasicConsole.DelayOutput(5); // Try to cause fault *((byte *)0xDEADBEEF) = 0; } // Leaving a "finally" critical section cleanly // We need to handle 2 cases: // Case 1 : Pending exception // Case 2 : No pending exception if (State->CurrentHandlerPtr->ExPending != 0) { // Case 1 : Pending exception //BasicConsole.WriteLine("End finally with ex"); HandleException(); } else { // Case 2 : No pending exception //BasicConsole.WriteLine("End finally without ex"); State->CurrentHandlerPtr->InHandler = 0; uint EBP = State->CurrentHandlerPtr->EBP; uint ESP = State->CurrentHandlerPtr->ESP; byte *retAddr = State->CurrentHandlerPtr->HandlerAddress;//(byte*)*((uint*)(BasePointer + 4)); //BasicConsole.Write("Continue ptr (from HandlerAddress): "); //BasicConsole.WriteLine((uint)State->CurrentHandlerPtr->HandlerAddress); //BasicConsole.Write("Actual continue addr (from EBP): "); //BasicConsole.WriteLine(*((uint*)(BasePointer + 4))); State->CurrentHandlerPtr = State->CurrentHandlerPtr->PrevHandlerPtr; ArbitaryReturn(EBP, ESP + (uint)sizeof(ExceptionHandlerInfo), retAddr); } }
public static unsafe FlingOops.String New(int length) { if (length < 0) { BasicConsole.WriteLine("ArgumentException! String.New, length less than zero."); ExceptionMethods.Throw(new Exception("Parameter \"length\" cannot be less than 0 in FlingOops.String.New(int length).")); } FlingOops.String result = (FlingOops.String)Utilities.ObjectUtilities.GetObject(GC.NewString(length)); if (result == null) { BasicConsole.WriteLine("NullReferenceException! String.New, result is null."); ExceptionMethods.Throw(new Exception()); } return(result); }
public static unsafe void HandleException() { //BasicConsole.WriteLine("Handle exception"); if (State != null) { if (State->CurrentHandlerPtr != null) { if (State->CurrentHandlerPtr->InHandler != 0) { State->CurrentHandlerPtr->InHandler = 0; if (State->CurrentHandlerPtr->PrevHandlerPtr != null) { State->CurrentHandlerPtr->PrevHandlerPtr->Ex = State->CurrentHandlerPtr->Ex; State->CurrentHandlerPtr->PrevHandlerPtr->ExPending = State->CurrentHandlerPtr->ExPending; } State->CurrentHandlerPtr = State->CurrentHandlerPtr->PrevHandlerPtr; } } ExceptionHandlerInfo *CurrHandlerPtr = State->CurrentHandlerPtr; if (CurrHandlerPtr != null) { if ((uint)CurrHandlerPtr->HandlerAddress != 0x00000000u) { if ((uint)CurrHandlerPtr->FilterAddress != 0x00000000u) { //Catch handler CurrHandlerPtr->ExPending = 0; } CurrHandlerPtr->InHandler = 1; ArbitaryReturn(CurrHandlerPtr->EBP, CurrHandlerPtr->ESP, CurrHandlerPtr->HandlerAddress); } } } // If we get to here, it's an unhandled exception HaltReason = "Unhandled / improperly handled exception!"; BasicConsole.WriteLine(HaltReason); // Try to cause fault *((byte *)0xDEADBEEF) = 0; }
public static void Main() { #if MIPS FlingOops.MIPS.CI20.Kernel.Start(); #elif x86 FlingOops.x86.Kernel.Start(); #endif BasicConsole.Init(); BasicConsole.WriteLine("Kernel executing..."); CompilerTests.RunTests(); #if MIPS FlingOops.MIPS.CI20.Kernel.End(); #elif x86 FlingOops.x86.Kernel.End(); #endif }
public static void _IncrementRefCount(byte *objPtr) { if ((uint)objPtr < (uint)sizeof(GCHeader)) { BasicConsole.SetTextColour(BasicConsole.error_colour); BasicConsole.WriteLine("Error! GC can't increment ref count of an object in low memory."); BasicConsole.DelayOutput(5); BasicConsole.SetTextColour(BasicConsole.default_colour); } objPtr -= sizeof(GCHeader); GCHeader *gcHeaderPtr = (GCHeader *)objPtr; if (CheckSignature(gcHeaderPtr)) { gcHeaderPtr->RefCount++; if (gcHeaderPtr->RefCount > 0) { RemoveObjectToCleanup(gcHeaderPtr); } } }
public static void *Alloc(UInt32 size, UInt32 boundary, FlingOops.String caller) { #if HEAP_TRACE BasicConsole.SetTextColour(BasicConsole.warning_colour); BasicConsole.WriteLine("Attempt to alloc mem...."); BasicConsole.SetTextColour(BasicConsole.default_colour); #endif HeapBlock *b; byte * bm; UInt32 bcnt; UInt32 x, y, z; UInt32 bneed; byte nid; #if MIPS if (boundary < 4) { boundary = 4; } #endif if (boundary > 1) { size += (boundary - 1); } /* iterate blocks */ for (b = fblock; (UInt32)b != 0; b = b->next) { /* check if block has enough room */ if (b->size - (b->used * b->bsize) >= size) { bcnt = b->size / b->bsize; bneed = (size / b->bsize) * b->bsize < size ? size / b->bsize + 1 : size / b->bsize; bm = (byte *)&b[1]; for (x = (b->lfb + 1 >= bcnt ? 0 : b->lfb + 1); x != b->lfb; ++x) { /* just wrap around */ if (x >= bcnt) { x = 0; } if (bm[x] == 0) { /* count free blocks */ for (y = 0; bm[x + y] == 0 && y < bneed && (x + y) < bcnt; ++y) { ; } /* we have enough, now allocate them */ if (y == bneed) { /* find ID that does not match left or right */ nid = GetNID(bm[x - 1], bm[x + y]); /* allocate by setting id */ for (z = 0; z < y; ++z) { bm[x + z] = nid; } /* optimization */ b->lfb = (x + bneed) - 2; /* count used blocks NOT bytes */ b->used += y; void *result = (void *)(x * b->bsize + (UInt32)(&b[1])); if (boundary > 1) { result = (void *)((((UInt32)result) + (boundary - 1)) & ~(boundary - 1)); #if HEAP_TRACE ExitCritical(); BasicConsole.WriteLine(((FlingOops.String) "Allocated address ") + (uint)result + " on boundary " + boundary + " for " + caller); EnterCritical("Alloc:Boundary condition"); #endif } return(result); } /* x will be incremented by one ONCE more in our FOR loop */ x += (y - 1); continue; } } } } #if HEAP_TRACE BasicConsole.SetTextColour(BasicConsole.error_colour); BasicConsole.WriteLine("!!Heap out of memory!!"); BasicConsole.SetTextColour(BasicConsole.default_colour); BasicConsole.DelayOutput(2); #endif return(null); }
public static void _DecrementRefCount(byte *objPtr) { if ((uint)objPtr < (uint)sizeof(GCHeader)) { BasicConsole.SetTextColour(BasicConsole.error_colour); BasicConsole.WriteLine("Error! GC can't decrement ref count of an object in low memory."); BasicConsole.DelayOutput(5); BasicConsole.SetTextColour(BasicConsole.default_colour); } GCHeader *gcHeaderPtr = (GCHeader *)(objPtr - sizeof(GCHeader)); if (CheckSignature(gcHeaderPtr)) { gcHeaderPtr->RefCount--; //If the ref count goes below 0 then there was a circular reference somewhere. // In actuality we don't care we can just only do cleanup when the ref count is // exactly 0. if (gcHeaderPtr->RefCount == 0) { #if GC_TRACE BasicConsole.WriteLine("Cleaning up object..."); #endif FlingOops.Object obj = (FlingOops.Object)Utilities.ObjectUtilities.GetObject(objPtr); if (obj is FlingOops.Array) { //Decrement ref count of elements FlingOops.Array arr = (FlingOops.Array)obj; if (!arr.elemType.IsValueType) { FlingOops.Object[] objArr = (FlingOops.Object[])Utilities.ObjectUtilities.GetObject(objPtr); for (int i = 0; i < arr.length; i++) { DecrementRefCount(objArr[i], true); } } } //Cleanup fields FieldInfo *FieldInfoPtr = obj._Type.FieldTablePtr; //Loop through all fields. The if-block at the end handles moving to parent // fields. while (FieldInfoPtr != null) { if (FieldInfoPtr->Size > 0) { FlingOops.Type fieldType = (FlingOops.Type)Utilities.ObjectUtilities.GetObject(FieldInfoPtr->FieldType); if (!fieldType.IsValueType && !fieldType.IsPointer) { byte * fieldPtr = objPtr + FieldInfoPtr->Offset; FlingOops.Object theFieldObj = (FlingOops.Object)Utilities.ObjectUtilities.GetObject(fieldPtr); DecrementRefCount(theFieldObj, true); #if GC_TRACE BasicConsole.WriteLine("Cleaned up field."); #endif } FieldInfoPtr++; } if (FieldInfoPtr->Size == 0) { FieldInfoPtr = (FieldInfo *)FieldInfoPtr->FieldType; } } #if GC_TRACE BasicConsole.WriteLine("Adding object to clean up..."); #endif AddObjectToCleanup(gcHeaderPtr, objPtr); #if GC_TRACE BasicConsole.WriteLine("Completed decrement ref count processing."); #endif } } }
public static void *NewString(int length) { if (!Enabled) { BasicConsole.SetTextColour(BasicConsole.warning_colour); BasicConsole.WriteLine("Warning! GC returning null pointer because GC not enabled."); BasicConsole.DelayOutput(10); BasicConsole.SetTextColour(BasicConsole.default_colour); return(null); } //try { if (length < 0) { BasicConsole.SetTextColour(BasicConsole.error_colour); BasicConsole.WriteLine("Error! GC can't create a new string because \"length\" is less than 0."); BasicConsole.DelayOutput(5); BasicConsole.SetTextColour(BasicConsole.default_colour); return(null); //ExceptionMethods.Throw_OverflowException(); } InsideGC = true; //Alloc space for GC header that prefixes object data //Alloc space for new string object //Alloc space for new string chars uint totalSize = ((FlingOops.Type) typeof(FlingOops.String)).Size; totalSize += /*char size in bytes*/ 2 * (uint)length; totalSize += (uint)sizeof(GCHeader); GCHeader *newObjPtr = (GCHeader *)Heap.AllocZeroed(totalSize, "GC : NewString"); if ((UInt32)newObjPtr == 0) { InsideGC = false; BasicConsole.SetTextColour(BasicConsole.error_colour); BasicConsole.WriteLine("Error! GC can't create a new string because the heap returned a null pointer."); BasicConsole.DelayOutput(10); BasicConsole.SetTextColour(BasicConsole.default_colour); return(null); } NumObjs++; NumStrings++; //Initialise the GCHeader SetSignature(newObjPtr); //RefCount to 0 initially because of FlingOops.String.New should be used // - In theory, New should be called, creates new string and passes it back to caller // Caller is then required to store the string in a variable resulting in inc. // ref count so ref count = 1 in only stored location. // Caller is not allowed to just "discard" (i.e. use Pop IL op or C# that generates // Pop IL op) so ref count will always at some point be incremented and later // decremented by managed code. OR the variable will stay in a static var until // the OS exits... newObjPtr->RefCount = 0; FlingOops.String newStr = (FlingOops.String)Utilities.ObjectUtilities.GetObject(newObjPtr + 1); newStr._Type = (FlingOops.Type) typeof(FlingOops.String); newStr.length = length; //Move past GCHeader byte *newObjBytePtr = (byte *)(newObjPtr + 1); InsideGC = false; return(newObjBytePtr); } //finally { //ExitCritical(); } }
public static void *NewArr(int length, FlingOops.Type elemType) { if (!Enabled) { BasicConsole.SetTextColour(BasicConsole.warning_colour); BasicConsole.WriteLine("Warning! GC returning null pointer because GC not enabled."); BasicConsole.DelayOutput(10); BasicConsole.SetTextColour(BasicConsole.default_colour); return(null); } //try { if (length < 0) { BasicConsole.WriteLine("length < 0. Overflow exception."); return(null); //ExceptionMethods.Throw_OverflowException(); } InsideGC = true; //Alloc space for GC header that prefixes object data //Alloc space for new array object //Alloc space for new array elems uint totalSize = ((FlingOops.Type) typeof(FlingOops.Array)).Size; if (elemType.IsValueType) { totalSize += elemType.Size * (uint)length; } else { totalSize += elemType.StackSize * (uint)length; } totalSize += (uint)sizeof(GCHeader); GCHeader *newObjPtr = (GCHeader *)Heap.AllocZeroed(totalSize, "GC : NewArray"); if ((UInt32)newObjPtr == 0) { InsideGC = false; BasicConsole.SetTextColour(BasicConsole.error_colour); BasicConsole.WriteLine("Error! GC can't create a new array because the heap returned a null pointer."); BasicConsole.DelayOutput(10); BasicConsole.SetTextColour(BasicConsole.default_colour); return(null); } NumObjs++; //Initialise the GCHeader SetSignature(newObjPtr); newObjPtr->RefCount = 1; FlingOops.Array newArr = (FlingOops.Array)Utilities.ObjectUtilities.GetObject(newObjPtr + 1); newArr._Type = (FlingOops.Type) typeof(FlingOops.Array); newArr.length = length; newArr.elemType = elemType; //Move past GCHeader byte *newObjBytePtr = (byte *)(newObjPtr + 1); InsideGC = false; return(newObjBytePtr); } //finally { } }
public static unsafe void AddExceptionHandlerInfo( void *handlerPtr, void *filterPtr) { if (State == null) { BasicConsole.SetTextColour(BasicConsole.error_colour); BasicConsole.WriteLine("Error! ExceptionMethods.State is null."); BasicConsole.DelayOutput(10); BasicConsole.SetTextColour(BasicConsole.default_colour); } //if (filterPtr != null) //{ // BasicConsole.WriteLine("Enter try-catch block"); //} //else //{ // BasicConsole.WriteLine("Enter try-finally block"); //} AddExceptionHandlerInfo_EntryStackState *BasePtr = (AddExceptionHandlerInfo_EntryStackState *)BasePointer; uint LocalsSize = (uint)BasePtr - (uint)StackPointer; // Create space for setting up handler info StackPointer -= sizeof(ExceptionHandlerInfo); // Setup handler info ExceptionHandlerInfo *ExHndlrPtr = (ExceptionHandlerInfo *)StackPointer; ExHndlrPtr->EBP = BasePtr->EBP; // EBP + 8 (for ret addr, ebp) + 8 (for args) - sizeof(ExceptionHandlerInfo) ExHndlrPtr->ESP = (uint)BasePtr + 8 + 8 - (uint)sizeof(ExceptionHandlerInfo); ExHndlrPtr->FilterAddress = (byte *)filterPtr; ExHndlrPtr->HandlerAddress = (byte *)handlerPtr; ExHndlrPtr->PrevHandlerPtr = State->CurrentHandlerPtr; ExHndlrPtr->InHandler = 0; ExHndlrPtr->ExPending = 0; ExHndlrPtr->Ex = null; State->CurrentHandlerPtr = (ExceptionHandlerInfo *)((byte *)ExHndlrPtr + (LocalsSize + 12)); StackPointer -= 8; // For duplicate (empty) args StackPointer -= 8; // For duplicate ebp, ret addr // Setup the duplicate stack data // - Nothing to do for args - duplicate values don't matter // - Copy over ebp and return address uint *DuplicateValsStackPointer = (uint *)StackPointer; *DuplicateValsStackPointer = BasePtr->EBP; *(DuplicateValsStackPointer + 1) = BasePtr->RetAddr; ShiftStack((byte *)ExHndlrPtr + sizeof(ExceptionHandlerInfo) - 4, LocalsSize + 12); // Shift stack pointer to correct position - eliminates "empty space" of duplicates StackPointer += 16; // MethodEnd will: // - Add size of locals to esp // - Pop EBP // - Ret to ret address // Caller will: // - Add size of args to esp // Which should leave the stack at the bottom of the (shifted up) ex handler info }
public static unsafe void HandleLeave(void *continuePtr) { if (State == null || State->CurrentHandlerPtr == null) { // If we get to here, it's an unhandled exception HaltReason = "Cannot leave on null handler! Address: 0x "; uint y = *((uint *)(BasePointer + 4)); int offset = 48; #region Address while (offset > 40) { uint rem = y & 0xFu; switch (rem) { case 0: HaltReason[offset] = '0'; break; case 1: HaltReason[offset] = '1'; break; case 2: HaltReason[offset] = '2'; break; case 3: HaltReason[offset] = '3'; break; case 4: HaltReason[offset] = '4'; break; case 5: HaltReason[offset] = '5'; break; case 6: HaltReason[offset] = '6'; break; case 7: HaltReason[offset] = '7'; break; case 8: HaltReason[offset] = '8'; break; case 9: HaltReason[offset] = '9'; break; case 10: HaltReason[offset] = 'A'; break; case 11: HaltReason[offset] = 'B'; break; case 12: HaltReason[offset] = 'C'; break; case 13: HaltReason[offset] = 'D'; break; case 14: HaltReason[offset] = 'E'; break; case 15: HaltReason[offset] = 'F'; break; } y >>= 4; offset--; } #endregion BasicConsole.WriteLine(HaltReason); BasicConsole.DelayOutput(5); // Try to cause fault *((byte *)0xDEADBEEF) = 0; } // Leaving a critical section cleanly // We need to handle 2 cases: // Case 1 : Leaving "try" or "catch" of a try-catch // Case 2 : Leaving the "try" of a try-finally if ((uint)State->CurrentHandlerPtr->FilterAddress != 0x0u) { // Case 1 : Leaving "try" or "catch" of a try-catch if (State->CurrentHandlerPtr->Ex != null) { FlingOops.GC.DecrementRefCount((FlingOops.Object)Utilities.ObjectUtilities.GetObject(State->CurrentHandlerPtr->Ex)); State->CurrentHandlerPtr->Ex = null; } State->CurrentHandlerPtr->InHandler = 0; uint EBP = State->CurrentHandlerPtr->EBP; uint ESP = State->CurrentHandlerPtr->ESP; //BasicConsole.WriteLine("Leave try or catch of try-catch"); //BasicConsole.WriteLine((uint)continuePtr); State->CurrentHandlerPtr = State->CurrentHandlerPtr->PrevHandlerPtr; ArbitaryReturn(EBP, ESP + (uint)sizeof(ExceptionHandlerInfo), (byte *)continuePtr); } else { // Case 2 : Leaving the "try" of a try-finally State->CurrentHandlerPtr->InHandler = 1; byte *handlerAddress = State->CurrentHandlerPtr->HandlerAddress; //BasicConsole.WriteLine("Leave try of try-finally"); //BasicConsole.Write("Handler address: "); //BasicConsole.WriteLine((uint)handlerAddress); //BasicConsole.Write("Continue ptr: "); //BasicConsole.WriteLine((uint)continuePtr); State->CurrentHandlerPtr->HandlerAddress = (byte *)continuePtr; ArbitaryReturn(State->CurrentHandlerPtr->EBP, State->CurrentHandlerPtr->ESP, handlerAddress); } }