CosmosOS / Cosmos

Cosmos is an operating system "construction kit". Build your own OS using managed languages such as C#, VB.NET, and more!
https://www.goCosmos.org
BSD 3-Clause "New" or "Revised" License
2.94k stars 554 forks source link

Error Handler? #464

Closed ghost closed 5 years ago

ghost commented 8 years ago

How can i catch Critical errors like division by zero or any other like that. Because i tried using try but it still crashes. Also i found out one files called INTs that is supposed to catch the critical system errors and i took the Plug from dewitcher (https://dewitcher.codeplex.com/) but it is not working. This is the code that i use (slightly modified from dewitcher so it will work in the newest version of Cosmos and be compatible with my OS):

using MihailOS.Core;
using Cosmos.IL2CPU.Plugs;
using CPUx86 = Cosmos.Assembler.x86;
using Cosmos.Core;
using IRQContext = Cosmos.Core.INTs.IRQContext;
namespace MihailOS.Plugs
{
    [Plug(typeof(INTs))]
    public static class INTsImpl
    {
        public delegate void IRQ0called();
        public static IRQ0called onCalled = delegate { PIT.called = true; };

        public static void HandleInterrupt_Default(ref INTs aThis, ref IRQContext aContext)
        {
            if (aContext.Interrupt >= 0x20 && aContext.Interrupt <= 0x2F)
            {
                if (aContext.Interrupt >= 0x28)
                {
                    Global.PIC.EoiSlave();
                }
                else
                {
                    Global.PIC.EoiMaster();
                }
            }
        }

        public static void HandleInterrupt_00(ref INTs aThis, ref IRQContext aContext)
        {
            Error.Init("Divide by zero Exception", "That was not nice!", true);
        }

        public static void HandleInterrupt_01(ref INTs aThis, ref IRQContext aContext)
        {
            Error.Init("Debug Exception", "I tought that we thought this?", true);
        }

        public static void HandleInterrupt_02(ref INTs aThis, ref IRQContext aContext)
        {
            Error.Init("Non Maskable Interrupt Exception", "That's a big error!", true);
        }

        public static void HandleInterrupt_03(ref INTs aThis, ref IRQContext aContext)
        {
            Error.Init("Breakpoint Exception", "Well... you didn't even give it time to load.", true);
        }

        public static void HandleInterrupt_04(ref INTs aThis, ref IRQContext aContext)
        {
            Error.Init("Into Detected Overflow Exception", "It's too much for me!", true);
        }

        public static void HandleInterrupt_05(ref INTs aThis, ref IRQContext aContext)
        {
            Error.Init("Out of Bounds Exception", "Why did you give me that? It's empty!", true);
        }

        public static void HandleInterrupt_06(ref INTs aThis, ref IRQContext aContext)
        {
            Error.Init("Invalid Opcode", "I did not understand that code!", true);
        }

        public static void HandleInterrupt_07(ref INTs aThis, ref IRQContext aContext)
        {
            Error.Init("No Coprocessor Exception", "How old is your PC?", true);
        }

        public static void HandleInterrupt_08(ref INTs aThis, ref IRQContext aContext)
        {
            Error.Init("Double Fault Exception", "If your name is Mihail, you are an idiot.", true);
        }

        public static void HandleInterrupt_09(ref INTs aThis, ref IRQContext aContext)
        {
            Error.Init("Coprocessor Segment Overrun Exception", "You messed up.", true);
        }

        public static void HandleInterrupt_0A(ref INTs aThis, ref IRQContext aContext)
        {
            Error.Init("Bad TSS Exception", "Nope, not today; I'm out!", true);
        }

        public static void HandleInterrupt_0B(ref INTs aThis, ref IRQContext aContext)
        {
            Error.Init("Segment not present", "BYE!", true);
        }

        public static void HandleInterrupt_0C(ref INTs aThis, ref IRQContext aContext)
        {
            Error.Init("Stack Fault Exception", "Do i have to write a description for every error?", true);
        }

        public static void HandleInterrupt_0E(ref INTs aThis, ref IRQContext aContext)
        {
            Error.Init("Page Fault Exception", "It's not my fault that it doesn't work!", true);
        }

        public static void HandleInterrupt_0F(ref INTs aThis, ref IRQContext aContext)
        {
            Error.Init("Unknown Interrupt Exception", "WHAT THE FUCK DID YOU DO?", true);
        }

        public static void HandleInterrupt_10(ref INTs aThis, ref IRQContext aContext)
        {
            Error.Init("Coprocessor Fault Exception", "Your PC, sucks!", true);
        }

        public static void HandleInterrupt_11(ref INTs aThis, ref IRQContext aContext)
        {
            Error.Init("Alignment Exception", "It's because of bad programming, ofc.", true);
        }

        public static void HandleInterrupt_12(ref INTs aThis, ref IRQContext aContext)
        {
            Error.Init("Machine Check Exception", "How did you even manage to get here.", true);
        }

        // IRQ0
        public static void HandleInterrupt_20(ref INTs aThis, ref IRQContext aContext)
        {
            Global.PIC.EoiMaster();
            PortIO.outb(0x20, 0x20);
            onCalled();
        }
    }

    public class STIEnabler
    {
        public static void Enable()
        {

        }
    }
    [Plug(Target = typeof(STIEnabler))]
    public class Enable : AssemblerMethod
    {
        public override void AssembleNew(Cosmos.Assembler.Assembler aAssembler, object aMethodInfo)
        {
            new CPUx86.Sti();
       }
   }

}

EDIT: Because i wasn't able to find the References i took the DLLs from my cosmos dev kit and i put them in one folder and i referenced the DLLs in the project. And i also have my MihailOS.Plugs project referenced to my MihailOS Boot project.

cjhannah commented 8 years ago

I believe it isn't possible to catch CPU exceptions unless you modify the devkit code. Just try to avoid them.

charlesbetros commented 8 years ago

Did you check the generated assembly code to see if your plug got included and is getting called? If your code is being plugged properly then try stepping through it with the Cosmos Assembly Debug Window or Bochs debugger to see what's going wrong.

On Sep 13, 2016, at 15:37, hannah notifications@github.com wrote:

I believe it isn't possible to catch CPU exceptions unless you modify the devkit code.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

ghost commented 8 years ago

I think that it isn't even called. Because it works the same with and without it.

ghost commented 7 years ago

Did you use try and catch?

ghost commented 7 years ago

Try catching fatal errors if you can.

quajak commented 6 years ago

Were you able to check if your plug is used or not?