This is the interrupt:
Code:
%macro ISR_ERRCODE 1
[GLOBAL isr%1]
isr%1:
cli
push byte %1
jmp isr_common_stub
%endmacro
Here is the code that handles interrupts:
Code:
[EXTERN isr_handler]
;# Common ISR code
isr_common_stub:
;# 1. Save CPU state
pusha
mov ax, ds ;# Lower 16-bits of eax = ds.
push eax ;# save the data segment descriptor
mov ax, 0x10 ;# kernel data segment descriptor
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
;# 2. Call C handler
call isr_handler
;# 3. Restore state
pop eax
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
popa
add esp, 8 ;# Cleans up the pushed error code and pushed ISR number
sti ;# enable interruption again
iret ;# pops 5 things at once: CS, EIP, EFLAGS, SS, and ESP
Code:
typedef struct {
u32 ds; /* Data segment selector */
u32 edi, esi, ebp, esp, ebx, edx, ecx, eax; /* Pushed by pusha. */
u32 int_no, err_code; /* Interrupt number and error code (if applicable) */
u32 eip, cs, eflags, useresp, ss; /* Pushed by the processor automatically */
} registers_t;
Code:
void isr_handler(registers_t r) {
/* this definition should be elsewhere */
char *exception_messages[] = {
"Division By Zero",
"Debug",
"Non Maskable Interrupt",
"Breakpoint",
"Into Detected Overflow",
"Out of Bounds",
"Invalid Opcode",
"No Coprocessor",
"Double Fault",
"Coprocessor Segment Overrun",
"Bad TSS",
"Segment Not Present",
"Stack Fault",
"General Protection Fault",
"Page Fault",
"Unknown Interrupt",
"Coprocessor Fault",
"Alignment Check",
"Machine Check",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved"
};
/* ========================= */
set_char_attr(YELLOW_FORGND);
print("received interrupt ");
print_int(r.int_no);
print(": ");
print(exception_messages[r.int_no]);
print("\n");
// if interrupt handler is set, run it
if(interrupt_handlers[r.int_no]) {
interrupt_handlers[r.int_no](r);
}
// stay there
while(1);
}
And here is the function that handles GPF:
Code:
static void general_protection_fault(registers_t r) {
set_char_attr(BLUE_BACKGND | WHITE_FORGND);
print("/!\\ GENERAL PROTECTION FAULT, SYSTEM HALTED /!\\ err code = ");
print_hex(r.err_code);
__asm__("cli\nhlt");
while(1);
}
And this is what Qemu shows me:
Attachment:
Capture d’écran 2018-06-05 à 2.00.14 PM.png [ 40.08 KiB | Viewed 8851 times ]
As I have read, the error code that GPF shows is the segment that caused the error, however i do not understand why the error is 0x000010d8. Is 0x000010d8 a segment index ?? I am quiet confused...
Thank you for your replies!