I am writing a kernel by myself, and after the first page error interrupt handler, when IRET is executed, it causes an interrupt 13(general protection), and error code is 0x18. I don’t know what is wrong, the content pushed on the stack comes from the cpu.
Here is the register state when interrupt occurs, and memory where the registers were stored.In addition, IRET is returned from a page error interrupt handler.
It is sure that %ESP is the same before IRET executing and interrupt occurring.


If the exception is from
IRETitself, then most likelyIRETis failing to restore one of the saved segment registers, but the value (8 or 0x18, btw?) is somehow wrong. It can be wrong because you never (re)initialized the register in protected mode or your handler set it to a bad value before doingIRETor something happened to the GDT…EDIT: From the picture it’s apparent that the page fault handler didn’t remove the exception code (value of 4 at address in
ESP) before executingIRET. And soIRETinterpreted 4 as the new value forEIP, 0x1000018 as the new value forCSand 0x23 as the new value forEFLAGS, whereas it should be using 0x1000018, 0x23 and 0x3206 for those three registers. Obviously, a data segment selector (which 0x1000018 is interpreted as after truncation to 0x0018) cannot be loaded intoCSand this causes #GP(0x18).