[Sovled] Problem with 32 bit interrupt handling in C.

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.
psimonson1988
Member
Member
Posts: 40
Joined: Tue Jul 16, 2019 8:40 pm

[Sovled] Problem with 32 bit interrupt handling in C.

Post by psimonson1988 »

Hey again guys,

Well I decided to redo some of the HAL interface and the kernel but, now when I try to use interrupts it fails. With a triple fault, not sure why. Would someone please take a look at the code? To see if you guys can figure it out. I've been doing this for a few days and I must have made a stupid mistake without spotting it. Not sure which file that mistake could be in though. I would like to know why it keeps triple faulting when I use interrupts though.

Project Link boot32-barebones (Development Branch) : https://github.com/psimonson/boot32-bar ... evelopment

PS: The development branch is the one I'm working on. Also everything I modified is in the kernel directory, somewhere in there is where the problem is.

Thanks in advance,
Philip
Last edited by psimonson1988 on Thu Dec 03, 2020 6:31 pm, edited 1 time in total.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Problem with 32 bit interrupt handling in C.

Post by Solar »

"Here's my repository, pls debug"? That's new levels of not SSCCE... 8)
Every good solution is obvious once you've found it.
User avatar
bloodline
Member
Member
Posts: 264
Joined: Tue Sep 15, 2020 8:07 am
Location: London, UK

Re: Problem with 32 bit interrupt handling in C.

Post by bloodline »

Solar wrote:"Here's my repository, pls debug"? That's new levels of not SSCCE... 8)
Hahaha! I wish I had thought of that... would have have saved me loads of time! =D>

Seriously though, my source code is available for anyone who wants to have a go debugging my mess :wink:
CuriOS: A single address space GUI based operating system built upon a fairly pure Microkernel/Nanokernel. Download latest bootable x86 Disk Image: https://github.com/h5n1xp/CuriOS/blob/main/disk.img.zip
Discord:https://discord.gg/zn2vV2Su
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Problem with 32 bit interrupt handling in C.

Post by iansjack »

So, what debugging hae you done so far?

(Hint - single-stepping under a debugger can be very instructive.)
psimonson1988
Member
Member
Posts: 40
Joined: Tue Jul 16, 2019 8:40 pm

Re: Problem with 32 bit interrupt handling in C.

Post by psimonson1988 »

Are there any tutorials on how to debug a kernel in QEMU or could you help guide me if you've got the time? Because so far the only debugging I've done is on normal applications. Not sure how to debug a kernel in QEMU. Please help and thanks.
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Problem with 32 bit interrupt handling in C.

Post by iansjack »

User avatar
austanss
Member
Member
Posts: 377
Joined: Sun Oct 11, 2020 9:46 pm
Location: United States

Re: Problem with 32 bit interrupt handling in C.

Post by austanss »

Your issue is that you have no assembly in your kernel or HAL at all.
Skylight: https://github.com/austanss/skylight

I make stupid mistakes and my vision is terrible. Not a good combination.

NOTE: Never respond to my posts with "it's too hard".
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Problem with 32 bit interrupt handling in C.

Post by iansjack »

rizxt wrote:Your issue is that you have no assembly in your kernel or HAL at all.
Did you miss the various bits of inline assembly code? What is missing that you think is necessary?
User avatar
austanss
Member
Member
Posts: 377
Joined: Sun Oct 11, 2020 9:46 pm
Location: United States

Re: Problem with 32 bit interrupt handling in C.

Post by austanss »

Literally, the Wiki states that you can't make an interrupt handler in C using just inline assembly.
Skylight: https://github.com/austanss/skylight

I make stupid mistakes and my vision is terrible. Not a good combination.

NOTE: Never respond to my posts with "it's too hard".
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Problem with 32 bit interrupt handling in C.

Post by iansjack »

I can't find that statement, but it's obviously false. The only thing I can find on the subject shows you precisely how you could use inline assembler for this purpose or, even simpler, how to use "__attribute__((interrupt))".

It's true that the OP's interrupt handlers won't work correctly as they are written, because they don't include an "iret" instruction, but as the end with an infinite loop this would not cause the triple fault.
User avatar
austanss
Member
Member
Posts: 377
Joined: Sun Oct 11, 2020 9:46 pm
Location: United States

Re: Problem with 32 bit interrupt handling in C.

Post by austanss »

iansjack stupidly wrote: I can't find that statement
https://wiki.osdev.org/Interrupt_Servic ... he_Problem
Skylight: https://github.com/austanss/skylight

I make stupid mistakes and my vision is terrible. Not a good combination.

NOTE: Never respond to my posts with "it's too hard".
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Problem with 32 bit interrupt handling in C.

Post by iansjack »

I still can't find that statement, just an example of how not to do it.
User avatar
austanss
Member
Member
Posts: 377
Joined: Sun Oct 11, 2020 9:46 pm
Location: United States

Re: Problem with 32 bit interrupt handling in C.

Post by austanss »

It results in a triple fault because ebp is pushed but never popped... CPU pops important registers back from the stack and since the stack is corrupted it faults.
Skylight: https://github.com/austanss/skylight

I make stupid mistakes and my vision is terrible. Not a good combination.

NOTE: Never respond to my posts with "it's too hard".
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Problem with 32 bit interrupt handling in C.

Post by iansjack »

So, as shown further down in that article, you either use inline assembler to insert the correct instructions or you use the directive. It's not the way I would do it, as I'm not a fan of inline assembler, but it can be done.

In this case, as the interrupt routine never tried to return, it's unlikely to be the cause of the triple fault.
MichaelPetch
Member
Member
Posts: 797
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: Problem with 32 bit interrupt handling in C.

Post by MichaelPetch »

As Ianjacks point out there is the __attribute__((interrupt)) that was introduced in GCC 7+. Unfortunately it has some restrictions that may be a problem for some kind of interrupts. If you want reliable access to the state of *all* the registers as they were before the interrupt was called - you can't. There is a mechanism that does work on old and new GCC and that is to write the entry point to an interrupt in pure basic inline assembly at global scope and then have that call into C code. That can be found here: https://stackoverflow.com/a/43311525/3857942 . You can then create C macros to generate these stubs for a large number of entries for ISRs and IRQs. Although the sample code doesn't pass a parameter to the handlers that can be added. That code in the answer was meant as a simplification and meant to show how this can be done.
Post Reply