1
votes

I have read the AMD64 Developer manual on interrupt routines. According to the manual,

The interrupt handler must pop the error code off the stack if one was pushed by the interrupt or exception. IRET restores the interrupted program's rIP, CS and rFLAGS by popping their saved values off of the stack and into their respective registers.

Therefore, would an empty ISR handler look something along this ASM code?

add rsp, 4       ;pop err code off stack
iretq

I am assuming the size of the error code is 4 bytes, as other websites have told me. I'm pretty sure this is totally wrong, but some guidance will help.

1
I'd be very surprised if the error code wasn't a full 8 bytes for a long-mode (64-bit) kernel, keeping the kernel stack 8-byte aligned.Peter Cordes
The error code is 8 bytes in 64-bit mode. Not all exceptions have an error code. You will have to treat exceptions with error codes slightly differently than ones without them. You can get a list of the exceptions and whether they have an error code here: wiki.osdev.org/Exceptions . External interrupts (IRQs) don't have error codes.Michael Petch
The table on the website helps a lot, thanks. @MichaelPetch. Would an empty handler that does not have an error code just be iretq and one that does have an error code would be add rsp, 8, iretq?Arush Agarampur
correct, no error number means you just do iretq to return.Michael Petch
Thanks a lot. If you can make that an answer that'd be great!Arush Agarampur

1 Answers

4
votes

The error code size in long-mode (x64) is 8 bytes long. So instead of adding 4 bytes to the stack pointer, you will need to add 8 bytes.

In addition, not all exceptions push an error code onto the stack. A table that contains which exceptions do and do not can be found here: https://wiki.osdev.org/Exceptions

If a hander does not push an error code, the empty handler is just the iretq instruction to return from the handler. If it DOES push an error code, we simply add 8 bytes to the stack pointer and then return from the handler.

add rsp, 8
iretq

Thanks @MichaelPetch