OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 3:59 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Neverending Exceptions?
PostPosted: Tue Nov 28, 2017 5:25 pm 
Offline

Joined: Tue Nov 28, 2017 2:24 pm
Posts: 1
Time for my first post! So, I am having problems setting up the GDT and IDT (or it could have occured somewhere else). What happens is an exception is immediately triggered when the STI instruction is executed. I have observed that it initially starts out with a double fault (afaik), but to my knowledge it is not pushing an error code like it should, throwing off the stack. If I remove the
Code:
add esp, 4
, only one exception is triggered and the instruction pointer register seems to hang at a weird place in memory without going anywhere afterwards. I have dumped the memory where the tables reside, and they look normal to me. The stack also doesn't seem to have the necessary information for IRET before it is executed. Can anyone find my error?
...
Edit:
Conclusion: Because I had written my own bootloader, the PIC was incorrectly mapped to the exception interrupts and I needed to manually reprogram it. Thanks Brendan!


Last edited by campital on Wed Nov 29, 2017 5:15 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Neverending Exceptions?
PostPosted: Tue Nov 28, 2017 11:37 pm 
Offline
Member
Member
User avatar

Joined: Sat Jan 15, 2005 12:00 am
Posts: 8561
Location: At his keyboard!
Hi,

campital wrote:
Time for my first post! So, I am having problems setting up the GDT and IDT (or it could have occured somewhere else). What happens is an exception is immediately triggered when the STI instruction is executed. I have observed that it initially starts out with a double fault (afaik), but to my knowledge it is not pushing an error code like it should, throwing off the stack.


That's not a double fault, it's a PIT (Programmable Interval Timer) IRQ.

Unfortunately, once upon a time (a long time ago) Intel said "the first 32 interrupt vectors are reserved for exceptions" and IBM decided to ignore Intel's advice by configuring the PIC (Programmable Interrupt Controller) chip to use interrupts 0x08 to 0x0F. Then Intel added more functionality that used more (previously reserved/unused) interrupts for exceptions, but it was too late - for compatibility with IBM's PC everyone had to keep using interrupts 0x08 to 0x0F for IRQs. Because of this, the default is for the PIT (which is IRQ0) to use interrupt 0x08, even though this interrupt is also used for the double fault exception handler. Note: After the original mistake had been made, IBM added a second PIC chip in a "master and slave" configuration, where the original PIC chip became the "master PIC chip" and had to continue using interrupts 0x08 to 0x0F, but the new "slave PIC chip" used interrupts 0x70 to 0x77 to avoid interrupts reserved for exceptions.

To fix this, every newer OS (that doesn't have to care about compatibility) reconfigures the PIC chips so that IRQs aren't using interrupts that are reserved for exceptions. E.g. most people reconfigure the PICs so that interrupts 0x20 to 0x27 are used for the master PIC and interrupts 0x28 to 0x2F are used for the slave PIC.

The correct way to do this is to mask everything in both PIC chips, then let BIOS handle any "pending" IRQs (with no real reason to bother using "CLI" or "STI" because all IRQs are masked anyway); then reconfigure the PIC chips; then unmask each IRQ if/when a device drivers needs that specific IRQ.

Unfortunately a lot of people do this wrong (and/or they use GRUB which does it wrong) - they'll do "CLI", then they'll do other stuff while any IRQs that occur become "unhandled pending IRQs" because they weren't masked; then they'll reconfigure the PIC chips (which doesn't clear out any "unhandled pending IRQs"), then they'll do more other stuff while more IRQs will become "unhandled pending IRQs", and then they'll do "STI" and immediately after that they'll receive any "unhanded pending IRQs" that should've been masked but weren't. ;)


Cheers,

Brendan

_________________
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot], SanderR and 72 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group