Пример #1
0
        public static void Setup()
        {
            // Initialize GDT before IDT, because IDT Entries requires a valid Segment Selector
            Multiboot.Setup();
            GDT.Setup();

            // At this stage, allocating memory does not work, so you are only allowed to use ValueTypes or static classes.
            IDT.SetInterruptHandler(null);
            Panic.Setup();

            // Initialize interrupts
            PIC.Setup();
            IDT.Setup();

            // Initializing the memory management
            PageFrameAllocator.Setup();
            PageTable.Setup();
            VirtualPageAllocator.Setup();
            GC.Setup();

            // At this point we can use objects
            Scheduler.Setup();
            SmbiosManager.Setup();
            ConsoleManager.Setup();

            Logger.Log("Kernel initialized");
        }
Пример #2
0
        /// <summary>
        /// Helps to track memroy changes. Checks if memroy has changed between two calls.
        /// </summary>
        /// <param name="startAddress"></param>
        /// <param name="bytes"></param>
        /// <param name="panic">Halt the kernel</param>
        /// <param name="message">Display a custom message</param>
        /// <returns></returns>
        public static bool MemoryChanged(uint startAddress, uint bytes, bool panic = false, string message = null)
        {
            //Improvement: Allocate memory for multiple items and check for different input values.
            var checksum = FlechterChecksum.Fletcher16(startAddress, bytes);

            memoyChangedItem.beforeLastCheckum = memoyChangedItem.lastChecksum;
            memoyChangedItem.lastChecksum      = checksum;
            memoyChangedItem.checks++;

            if (memoyChangedItem.checks > 1 && memoyChangedItem.beforeLastCheckum != memoyChangedItem.lastChecksum)
            {
                if (panic)
                {
                    if (message == null)
                    {
                        Panic.Error("Memory Changed Exception");
                    }
                    else
                    {
                        Panic.Error(message);
                    }
                }

                return(true);
            }
            else
            {
                return(false);
            }
        }
Пример #3
0
        public static void Setup()
        {
            // At this stage, allocating memory does not work, so you are only allowed to use ValueTypes or static classes.
            IDT.SetInterruptHandler(null);
            Panic.Setup();
            Debugger.Setup(Serial.COM1);

            // Initialize interrupts
            PIC.Setup();
            IDT.Setup();

            // Initializing the memory management
            Multiboot.Setup();
            GDT.Setup();
            PageFrameAllocator.Setup();
            PageTable.Setup();
            VirtualPageAllocator.Setup();
            GC.Setup();

            // At this point we can use objects
            Scheduler.Setup();
            SmbiosManager.Setup();
            ConsoleManager.Setup();
            Internal.Setup();
        }
Пример #4
0
        public static void Setup()
        {
            // Initialize GDT before IDT, because IDT Entries requies a valid Segment Selector
            // This never happend before, because on fast computers GDT.Setup() was called
            // before a Interrupt,for example clock, got triggered.
            Multiboot.Setup();
            GDT.Setup();

            // At this stage, allocating memory does not work, so you are only allowed to use ValueTypes or static classes.
            IDT.SetInterruptHandler(null);
            Panic.Setup();
            Debugger.Setup(Serial.COM1);

            // Initialize interrupts
            PIC.Setup();
            IDT.Setup();

            // Initializing the memory management
            PageFrameAllocator.Setup();
            PageTable.Setup();
            VirtualPageAllocator.Setup();
            GC.Setup();

            // At this point we can use objects
            Scheduler.Setup();
            SmbiosManager.Setup();
            ConsoleManager.Setup();
            Internal.Setup();
        }
Пример #5
0
        /// <summary>
        /// Interrupts the handler.
        /// </summary>
        /// <param name="stackStatePointer">The stack state pointer.</param>
        private unsafe static void ProcessInterrupt(uint stackStatePointer)
        {
            //KernelMessage.WriteLine("Interrupt occured");

            var stack = (IDTStack *)stackStatePointer;
            var irq   = stack->Interrupt;
            var info  = IDTManager.handlers[irq];

            IDTManager.RaisedCount++;

            if (info.CountStatistcs)
            {
                IDTManager.RaisedCountCustom++;
            }
            if (info.Trace)
            {
                KernelMessage.WriteLine("Interrupt: {0}", irq);
            }

            var col = Screen.column;
            var row = Screen.row;

            Screen.column = 0;
            Screen.Goto(2, 35);
            Screen.Write("Interrupts: ");
            Screen.Write(IDTManager.RaisedCount);
            Screen.Goto(3, 35);
            Screen.Write("IntNoClock: ");
            Screen.Write(IDTManager.RaisedCountCustom);
            Screen.row    = row;
            Screen.column = col;

            if (irq < 0 || irq > 255)
            {
                Panic.Error("Invalid Interrupt");
            }

            if (info.Handler == null)
            {
                //Panic.Error("Handlr is null");
            }
            else
            {
            }

            info.Handler(stack);

            PIC.SendEndOfInterrupt(irq);
        }
Пример #6
0
        /// <summary>
        /// Creates the process.
        /// </summary>
        /// <returns></returns>
        public static uint CreateProcess()
        {
            // TODO: Lock

            uint slot = FindEmptySlot();

            if (slot == 0)
            {
                Panic.Now(5);
            }

            CreateProcess(slot);

            // TODO: Unlock

            return(slot);
        }
Пример #7
0
        /// <summary>
        /// Creates the task.
        /// </summary>
        /// <returns></returns>
        public static uint CreateTask(uint processid, uint eip)
        {
            // TODO: Lock

            uint slot = FindEmptySlot();

            if (slot == 0)
            {
                Panic.Now(5);
            }

            CreateTask(processid, slot, eip);

            // TODO: Unlock

            return(slot);
        }
Пример #8
0
 private unsafe static void Error(IDTStack *stack, string message)
 {
     Panic.ESP       = stack->ESP;
     Panic.EBP       = stack->EBP;
     Panic.EIP       = stack->EIP;
     Panic.EAX       = stack->EAX;
     Panic.EBX       = stack->EBX;
     Panic.ECX       = stack->ECX;
     Panic.EDX       = stack->EDX;
     Panic.EDI       = stack->EDI;
     Panic.ESI       = stack->ESI;
     Panic.CS        = stack->CS;
     Panic.ErrorCode = stack->ErrorCode;
     Panic.EFLAGS    = stack->EFLAGS;
     Panic.Interrupt = stack->Interrupt;
     Panic.Error(message);
 }
Пример #9
0
        /// <summary>
        /// Handle Page Faults
        /// </summary>
        /// <param name="errorCode">The error code.</param>
        public static void Fault(uint errorCode)
        {
            uint virtualpage = Native.GetCR2();

            if (virtualpage == 0x0)
            {
                Panic.Now(2);                   // Can't map null! what happened?
            }

            // TODO: acquire lock

            uint physicalpage = PageFrameAllocator.Allocate();

            if (physicalpage == 0x0)
            {
                Panic.Now(1);                   // Panic! Out of memory
            }
            PageTable.MapVirtualAddressToPhysical(virtualpage, physicalpage);

            // TODO: release lock
        }
Пример #10
0
        /// <summary>
        /// Handle Page Faults
        /// </summary>
        /// <param name="errorCode">The error code.</param>
        public static void Fault(uint errorCode)
        {
            uint virtualpage = Native.GetCR2();

            if (virtualpage == 0x0)
            {
                Panic.Now(2);                   // Can't map null! what happened?
            }

            //bool taken = false;
            //spinLock.Enter(ref taken);

            uint physicalpage = PageFrameAllocator.Allocate();

            if (physicalpage == 0x0)
            {
                Panic.Now(1);                   // Panic! Out of memory
            }
            PageTable.MapVirtualAddressToPhysical(virtualpage, physicalpage);

            //spinLock.Exit();
        }
Пример #11
0
 private static void Error(uint ebp, uint eip, string message)
 {
     Panic.SetStackPointer(ebp, eip);
     Panic.Error(message);
 }
Пример #12
0
        /// <summary>
        /// Interrupts the handler.
        /// </summary>
        /// <param name="stack">The stack.</param>
        private unsafe static void ProcessInterrupt(IDTStack *stack)
        {
            DebugClient.Process();

            switch (stack->Interrupt)
            {
            case 0:
                Error(stack->EBP, stack->EIP, "Divide Error");
                break;

            case 4:
                Error(stack->EBP, stack->EIP, "Arithmetic Overflow Exception");
                break;

            case 5:
                Error(stack->EBP, stack->EIP, "Bound Check Error");
                break;

            case 6:
                Error(stack->EBP, stack->EIP, "Invalid Opcode");
                break;

            case 7:
                Error(stack->EBP, stack->EIP, "Co-processor Not Available");
                break;

            case 8:

                //TODO: Analyze the double fault
                Error(stack->EBP, stack->EIP, "Double Fault");
                break;

            case 9:
                Error(stack->EBP, stack->EIP, "Co-processor Segment Overrun");
                break;

            case 10:
                Error(stack->EBP, stack->EIP, "Invalid TSS");
                break;

            case 11:
                Error(stack->EBP, stack->EIP, "Segment Not Present");
                break;

            case 12:
                Error(stack->EBP, stack->EIP, "Stack Exception");
                break;

            case 13:
                Error(stack->EBP, stack->EIP, "General Protection Exception");
                break;

            case 14:

                // Check if Null Pointer Exception
                // Otherwise handle as Page Fault

                var cr2 = Native.GetCR2() >> 5;
                if (cr2 < 0x1000)
                {
                    Error(stack->EBP, stack->EIP, "Null Pointer Exception");
                }

                //spinLock.Enter(ref taken);

                uint physicalpage = PageFrameAllocator.Allocate();

                if (physicalpage == 0x0)
                {
                    // Panic! Out of memory
                    Panic.SetStackPointer(stack->EBP, stack->EIP);
                    Panic.Error(cr2);
                }

                PageTable.MapVirtualAddressToPhysical(Native.GetCR2(), physicalpage);

                //spinLock.Exit();

                break;

            case 16:
                Error(stack->EBP, stack->EIP, "Co-processor Error");
                break;

            case 19:
                Error(stack->EBP, stack->EIP, "SIMD Floating-Point Exception");
                break;

            default:
                if (interruptHandler != null)
                {
                    interruptHandler(stack->Interrupt, stack->ErrorCode);
                }
                break;
            }

            PIC.SendEndOfInterrupt(stack->Interrupt);
        }
Пример #13
0
        /// <summary>
        /// Interrupts the handler.
        /// </summary>
        /// <param name="stack">The stack.</param>
        private unsafe static void ProcessInterrupt(IDTStack *stack)
        {
            DebugClient.Process();

            switch (stack->Interrupt)
            {
            case 0:
                Error(stack->EBP, stack->EIP, "Divide Error");
                break;

            case 4:
                Error(stack->EBP, stack->EIP, "Arithmetic Overflow Exception");
                break;

            case 5:
                Error(stack->EBP, stack->EIP, "Bound Check Error");
                break;

            case 6:
                Error(stack->EBP, stack->EIP, "Invalid Opcode");
                break;

            case 7:
                Error(stack->EBP, stack->EIP, "Coprocessor Not Available");
                break;

            case 8:

                //TODO: Analyze the double fault
                Error(stack->EBP, stack->EIP, "Double Fault");
                break;

            case 9:
                Error(stack->EBP, stack->EIP, "Coprocessor Segment Overrun");
                break;

            case 10:
                Error(stack->EBP, stack->EIP, "Invalid TSS");
                break;

            case 11:
                Error(stack->EBP, stack->EIP, "Segment Not Present");
                break;

            case 12:
                Error(stack->EBP, stack->EIP, "Stack Exception");
                break;

            case 13:
                Error(stack->EBP, stack->EIP, "General Protection Exception");
                break;

            case 14:

                // Page Fault!
                var cr2 = Native.GetCR2() >> 5;
                if (cr2 < 0x1000)
                {
                    Error(stack->EBP, stack->EIP, "Null Pointer Exception");
                    break;
                }

                //PageFaultHandler.Fault(errorCode);
                Panic.SetStackPointer(stack->EBP, stack->EIP);
                Panic.Error(cr2);
                break;

            case 16:
                Error(stack->EBP, stack->EIP, "Coprocessor Error");
                break;

            case 19:
                Error(stack->EBP, stack->EIP, "SIMD Floating-Point Exception");
                break;

            default:
                if (interruptHandler != null)
                {
                    interruptHandler(stack->Interrupt, stack->ErrorCode);
                }
                break;
            }

            PIC.SendEndOfInterrupt(stack->Interrupt);
        }