Question about Interrupts and Exceptions

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
elderK

Question about Interrupts and Exceptions

Post by elderK »

Hello all,

First of all, Id like to say that the resources OS FAQ and Megatokyo provide are AWESOME.

Secondly, Ive been reading through the OS FAQ and browsing the web, and ive developed a basic little kernel.

:) My bootloader loads my Kernel, enters Protected Mode, sets up the Stack stuff and then jumps into Kernel code (Which is written in C).

Ive got a few Kernel functions (basic ports, basic video etc).
But, now ive come to the conclusion that I really really need an Exception and Interrupt handler.

And ... im not entirely sure how to implement one, even after reading the OS FAQ aritcles about it a few times.

Itd be much appreciated if anyone here could provide me with some good links on how to implement such handlers.

Also, I was wondering.. if I can implement the Interrupt / Exception handlers from within Kernel code, or atleast code it in C.

Thank you!
~zeii.
paulbarker

Re:Question about Interrupts and Exceptions

Post by paulbarker »

You need the low-level portion of the handlers in assembly language (to save registers and other such things), but then you can pass control to a C function. The best thing to do is look at some existing code, play around with it and then work out how to port it to your OS. My suggestion would be GeekOS, which my own kernel is based on.
Midas
Member
Member
Posts: 140
Joined: Sat Jun 24, 2006 4:40 pm
Location: Falkirk, Scotland
Contact:

Re:Question about Interrupts and Exceptions

Post by Midas »

If you do a little disassembly, and see how your compiler deals with functions then you can manually do some things in inline assembler. My copy of GCC (and I'd assume therefore other versions, too) surrounds functions with:

Code: Select all

push EBP
mov EBP, ESP
; Code to allocate space for local variables on stack
; Function code
leave
ret
So it should theoretically be possible to write an exception handler with pure C by doing:

Code: Select all

void MyHandler()
{
asm volatile("pushad")
unsigned long MyExampleVar = 0;
/*
 Your handler code
*/
asm volatile("popad; leave; iret")
}
Which would assemble to

Code: Select all

push EBP
mov EBP, ESP
sub ESP, 4
pushad
pushf
; Your handler code
popad
leave
iret
leave ; This line and the next are left in the binary but
ret ; are never executed - a waste of two bytes
This is a fairly ugly way of doing it and would, I suspect, be frowned upon. You'll note also that even this 'pure C' method (as I described it) still requires some assembler - this is because C doesn't support things such as directly modifying the registers, etc. It should also be noted that the above code doesn't do anything handy like get the segment values, EIP or anything at all from the erroring function - all of which are pushed by the CPU on an exception. In addition, were you to want to do a stack dump then you would need to use an ASM stub to call a C function - as you need the value of the erroring process' EBP - which is pushed and replaced with an EBP for the current function by the C compiler.

So yes, while it is possible to do it this way, it's better to have an ASM 'stub' which calls your C function (you might want to look up C calling conventions).

EDIT: A little tidying of typos
EDIT2: A few notes on constraints of this
Regards,
Angus [Óengus] 'Midas' Lepper
elderK

Re:Question about Interrupts and Exceptions

Post by elderK »

The thing that confuses me...

Is the Bootloader sets up the GDT and protected mode stuff, then jumps to the Kernel, which is entirely in C (Although some functions for the Kernel have inline ASM).

Is it possible... to setup the IDT in the bootloader, then have the handlers in the C Kernel?

~Zeii.
elderK

Re:Question about Interrupts and Exceptions

Post by elderK »

Also, (this is going to make me sound like an idiot :/)

Anywho, Once im in PM and such, I can use all of my functions, pass arguments, strings etc, it all works fine.

But, for fun, I wrote a GDT loading routine, thatll set the GDT up the way I want it.

I called it from the Kernel, even though the GDT was already setup.
The result - Tripple fault.

Im curious to why this happens, does it fault because everythings already setup nicely, and with a reset of the GDT, all the return addresses and such get fried?

I have little idea why it tripple faults, so any explanation would be appreciated :).

~zeii
Midas
Member
Member
Posts: 140
Joined: Sat Jun 24, 2006 4:40 pm
Location: Falkirk, Scotland
Contact:

Re:Question about Interrupts and Exceptions

Post by Midas »

You could setup the IDT and handlers seperately, but I would suggest setting them both up in the kernel, as it makes it easier to set the base addresses of the IDT entries.

There are a variety of reasons that your code triple faults while loading the GDT - as you don't have an IDT, absolutely any exception will cause your code to triple fault. I'd recommend running your code under Bochs (with or without the debugger) to get a little more information about the crash.

What setup are you using for your GDT? Is your bootloader written by you, or are you using GRUB?

If you're using GRUB (or a multiboot-compliant bootloader) then your GDT will be setup with a NULL descriptor and code and data segments for 0 - 4GB flat addressing.

This GDT however should be replaced ASAP by your kernel.
Regards,
Angus [Óengus] 'Midas' Lepper
elderK

Re:Question about Interrupts and Exceptions

Post by elderK »

im using a Flat memory model, I suppose jsut like grub.
Im not sure what to go to, since atm all im wanting to make is something like a protected mode MSDOS.

Anywho, ive been working on my Interrupt system and such, and... Ive written an Interrupt handler and all... to no avail.

Exceptions still tripple fault, even though ive got an exception handler.

Perhaps I should upload my source?

~zeii.
Midas
Member
Member
Posts: 140
Joined: Sat Jun 24, 2006 4:40 pm
Location: Falkirk, Scotland
Contact:

Re:Question about Interrupts and Exceptions

Post by Midas »

It might be useful to see the code you use to setup your IDT and interrupt handler.
Regards,
Angus [Óengus] 'Midas' Lepper
elderK

Re:Question about Interrupts and Exceptions

Post by elderK »

Alright, Here is the source to my enitre OS so far.
Bootloader, etc... All the Kernel code, the Assembler stubs and such for Interrupts, yadda yadda.

Its in TAR.GZ format, so... If you are on Windows, youll need something like Winrar to open it.

http://homepages.ihug.co.nz/~scroodle/Citadel-src-220506.tar.gz
~zeii.
Midas
Member
Member
Posts: 140
Joined: Sat Jun 24, 2006 4:40 pm
Location: Falkirk, Scotland
Contact:

Re:Question about Interrupts and Exceptions

Post by Midas »

Okay, listing points as I see them.

1. In your makefile, consider changing your [tt]rm[/tt] to [tt]rm -f[/tt] so that they don't throw errors when you run [tt]make all[/tt] without having previously run [tt]make cf[/tt] successfully.

2. This code doesn't actually want to compile at all for me - I get about 160 lines of warnings and errors.

3. In your ISRs, you've written ISRs specifically for each Intel-reserved interrupt. Since these are all going to have exactly the same handler (Either a handler which ignores them, a handler which prints a message and panics, or no handler so that the machine triple faults) since we have no idea how to deal with them as they aren't defined yet. You might be better off (in terms of code reuse) writing the one handler for the reserved interrupts (15,19-31) and setting the base of all the reserved IDT entries to this one handler. It's up to you, but it might make your (pretty darn clean) code cleaner and certainly a little easier to read.

A bootable floppy image might be useful, as then I can see what Bochs debugger reckons the error is, and when it happens (after the exception, for example: if the interrupt vectors are something like 0 then 0xD then a reset, then that means that the IDT is setup wrongly).

I couldn't see anything immediately obvious when I took a look, but if we know where the problem occurs then we can take a closer look at a specific part.

Incidentally, I've attached the errors I get when trying to run [tt]make cf[/tt], without making any changes to the code.
Regards,
Angus [Óengus] 'Midas' Lepper
elderK

Re:Question about Interrupts and Exceptions

Post by elderK »

Ive fixed a few obvious bugs I found in the Interrupt system.

I can now call the ISRs in Software, and theyll call the Exception Handler.

So, Ive isolated the problem to the IDT Table.
The Addresses for the ISR functions must be wrong, and sadly - I have no Idea what so ever how to fix it.

Ill upload a Floppy Image in a few moments for you.
Ah, I cant see any attachments on your post.

Floppy Image: http://homepages.ihug.co.nz/~scroodle/citadel_floppy.img

Give me about 5 minutes to get the Image up.
Also, do you have MSN? Itd be handy if we could speak in Real time.

~Zeii
Midas
Member
Member
Posts: 140
Joined: Sat Jun 24, 2006 4:40 pm
Location: Falkirk, Scotland
Contact:

Re:Question about Interrupts and Exceptions

Post by Midas »

Oops, my session timed out before, I obviously forgot to reattach the file. This time :P

Well, it is indeed a problem with the IDT.

'Vector: 0
Vector: 8
00001126755e[CPU ] exception(): 3rd (13) exception with no resolution, shutdown status is 00h, resetting'

Currently stepping through the code to find where things go wrong.

I'm afraid I don't have MSN nor any IM facility available RATM.
Regards,
Angus [Óengus] 'Midas' Lepper
elderK

Re:Question about Interrupts and Exceptions

Post by elderK »

I dont see any errors listed.
Just the NASM / GCC / LD Commands.

Im bughunting my C, scanning to see if there is anything else at all obvious

~Zeii.
Midas
Member
Member
Posts: 140
Joined: Sat Jun 24, 2006 4:40 pm
Location: Falkirk, Scotland
Contact:

Re:Question about Interrupts and Exceptions

Post by Midas »

zeii wrote: I dont see any errors listed.
Just the NASM / GCC / LD Commands.

Im bughunting my C, scanning to see if there is anything else at all obvious

~Zeii.
Once more, apologies, I forgot. :P stderr doesn't get redirected.

Okay, stepping through in the Bochs debugger shows that the IDT gets loaded with a base of 0x0E000000, and a limit of 0x7FF.

Printing this part of memory shows that this is all set to 0xFF.
Regards,
Angus [Óengus] 'Midas' Lepper
Midas
Member
Member
Posts: 140
Joined: Sat Jun 24, 2006 4:40 pm
Location: Falkirk, Scotland
Contact:

Re:Question about Interrupts and Exceptions

Post by Midas »

Hmm, just doesn't seem to want to upload for me. >.<
Regards,
Angus [Óengus] 'Midas' Lepper
Post Reply