Common code for interrupt handlers

This commit is contained in:
Braiden Vasco 2017-11-05 13:07:36 +00:00
parent be7b16afc0
commit 84fdb622b8
8 changed files with 199 additions and 252 deletions

View File

@ -1,78 +1,40 @@
#include "config.h"
%include "interrupt.asm"
[EXTERN exception_handler]
%macro EXCEPTION_NOERRCODE 1
[GLOBAL exception_%1]
exception_%1:
cli
push byte 0
push byte %1
jmp exception_common_stub
%endmacro
INTERRUPT_NOERRCODE 0 ; #DE - Divide Error Exception
INTERRUPT_NOERRCODE 1 ; #DB - Debug Exception
INTERRUPT_NOERRCODE 2 ; NMI - Non-maskable interrupt
INTERRUPT_NOERRCODE 3 ; #BP - Breakpoint Exception
INTERRUPT_NOERRCODE 4 ; #OF - Overflow Exception
INTERRUPT_NOERRCODE 5 ; #BR - BOUND Range Exceeded Exception
INTERRUPT_NOERRCODE 6 ; #UD - Invalid Opcode Exception
INTERRUPT_NOERRCODE 7 ; #NM - Device Not Available Exception
INTERRUPT_ERRCODE 8 ; #DF - Double Fault Exception
INTERRUPT_NOERRCODE 9 ; Reserved - Coprocessor Segment Overrun
INTERRUPT_ERRCODE 10 ; #TS - Invalid TSS Exception
INTERRUPT_ERRCODE 11 ; #NP - Segment Not Present
INTERRUPT_ERRCODE 12 ; #SS - Stack Fault Exception
INTERRUPT_ERRCODE 13 ; #GP - General Protection Exception
INTERRUPT_ERRCODE 14 ; #PF - Page-Fault Exception
INTERRUPT_NOERRCODE 15 ; Reserved
INTERRUPT_NOERRCODE 16 ; #MF - x87 FPU Floating-Point Error
INTERRUPT_ERRCODE 17 ; #AC - Alignment Check Exception
INTERRUPT_NOERRCODE 18 ; #MC - Machine-Check Exception
INTERRUPT_NOERRCODE 19 ; #XF - SIMD Floating-Point Exception
INTERRUPT_NOERRCODE 20 ; Reserved
INTERRUPT_NOERRCODE 21 ; Reserved
INTERRUPT_NOERRCODE 22 ; Reserved
INTERRUPT_NOERRCODE 23 ; Reserved
INTERRUPT_NOERRCODE 24 ; Reserved
INTERRUPT_NOERRCODE 25 ; Reserved
INTERRUPT_NOERRCODE 26 ; Reserved
INTERRUPT_NOERRCODE 27 ; Reserved
INTERRUPT_NOERRCODE 28 ; Reserved
INTERRUPT_NOERRCODE 29 ; Reserved
INTERRUPT_NOERRCODE 30 ; Reserved
INTERRUPT_NOERRCODE 31 ; Reserved
%macro EXCEPTION_ERRCODE 1
[GLOBAL exception_%1]
exception_%1:
cli
push byte %1
jmp exception_common_stub
%endmacro
EXCEPTION_NOERRCODE 0 ; #DE - Divide Error Exception
EXCEPTION_NOERRCODE 1 ; #DB - Debug Exception
EXCEPTION_NOERRCODE 2 ; NMI - Non-maskable interrupt
EXCEPTION_NOERRCODE 3 ; #BP - Breakpoint Exception
EXCEPTION_NOERRCODE 4 ; #OF - Overflow Exception
EXCEPTION_NOERRCODE 5 ; #BR - BOUND Range Exceeded Exception
EXCEPTION_NOERRCODE 6 ; #UD - Invalid Opcode Exception
EXCEPTION_NOERRCODE 7 ; #NM - Device Not Available Exception
EXCEPTION_ERRCODE 8 ; #DF - Double Fault Exception
EXCEPTION_NOERRCODE 9 ; Reserved - Coprocessor Segment Overrun
EXCEPTION_ERRCODE 10 ; #TS - Invalid TSS Exception
EXCEPTION_ERRCODE 11 ; #NP - Segment Not Present
EXCEPTION_ERRCODE 12 ; #SS - Stack Fault Exception
EXCEPTION_ERRCODE 13 ; #GP - General Protection Exception
EXCEPTION_ERRCODE 14 ; #PF - Page-Fault Exception
EXCEPTION_NOERRCODE 15 ; Reserved
EXCEPTION_NOERRCODE 16 ; #MF - x87 FPU Floating-Point Error
EXCEPTION_ERRCODE 17 ; #AC - Alignment Check Exception
EXCEPTION_NOERRCODE 18 ; #MC - Machine-Check Exception
EXCEPTION_NOERRCODE 19 ; #XF - SIMD Floating-Point Exception
EXCEPTION_NOERRCODE 20 ; Reserved
EXCEPTION_NOERRCODE 21 ; Reserved
EXCEPTION_NOERRCODE 22 ; Reserved
EXCEPTION_NOERRCODE 23 ; Reserved
EXCEPTION_NOERRCODE 24 ; Reserved
EXCEPTION_NOERRCODE 25 ; Reserved
EXCEPTION_NOERRCODE 26 ; Reserved
EXCEPTION_NOERRCODE 27 ; Reserved
EXCEPTION_NOERRCODE 28 ; Reserved
EXCEPTION_NOERRCODE 29 ; Reserved
EXCEPTION_NOERRCODE 30 ; Reserved
EXCEPTION_NOERRCODE 31 ; Reserved
exception_common_stub:
pusha ; Pushes edi,esi,ebp,esp,ebx,edx,ecx,eax
mov ax, ds ; Lower 16-bits of eax = ds.
push eax ; save the data segment descriptor
mov ax, GDT_KERNEL_DS_SELECTOR
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
call exception_handler
pop eax ; reload the original data segment descriptor
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
popa ; Pops edi,esi,ebp...
add esp, 8 ; Cleans up the pushed error code and pushed ISR number
sti
iret ; pops 5 things at once: CS, EIP, EFLAGS, SS, and ESP
INTERRUPT_COMMON exception_handler, GDT_KERNEL_DS_SELECTOR

View File

@ -1,37 +1,37 @@
#ifndef KERNELMQ_INCLUDED_EXCEPTION
#define KERNELMQ_INCLUDED_EXCEPTION 1
void exception_0();
void exception_1();
void exception_2();
void exception_3();
void exception_4();
void exception_5();
void exception_6();
void exception_7();
void exception_8();
void exception_9();
void exception_10();
void exception_11();
void exception_12();
void exception_13();
void exception_14();
void exception_15();
void exception_16();
void exception_17();
void exception_18();
void exception_19();
void exception_20();
void exception_21();
void exception_22();
void exception_23();
void exception_24();
void exception_25();
void exception_26();
void exception_27();
void exception_28();
void exception_29();
void exception_30();
void exception_31();
void interrupt_0();
void interrupt_1();
void interrupt_2();
void interrupt_3();
void interrupt_4();
void interrupt_5();
void interrupt_6();
void interrupt_7();
void interrupt_8();
void interrupt_9();
void interrupt_10();
void interrupt_11();
void interrupt_12();
void interrupt_13();
void interrupt_14();
void interrupt_15();
void interrupt_16();
void interrupt_17();
void interrupt_18();
void interrupt_19();
void interrupt_20();
void interrupt_21();
void interrupt_22();
void interrupt_23();
void interrupt_24();
void interrupt_25();
void interrupt_26();
void interrupt_27();
void interrupt_28();
void interrupt_29();
void interrupt_30();
void interrupt_31();
#endif

View File

@ -1,55 +1,25 @@
#include "config.h"
%include "interrupt.asm"
[EXTERN hwint_handler]
%macro HWINT 2
[GLOBAL hwint_%1]
hwint_%1:
cli
push byte 0
push byte %2
jmp hwint_common_stub
%endmacro
INTERRUPT_NOERRCODE 32 ; Programmable Interval Timer
INTERRUPT_NOERRCODE 33 ; Keyboard
INTERRUPT_NOERRCODE 34 ; Slave PIC
INTERRUPT_NOERRCODE 35 ; COM 2/4
INTERRUPT_NOERRCODE 36 ; COM 1/3
INTERRUPT_NOERRCODE 37 ; LPT 2
INTERRUPT_NOERRCODE 38 ; Floppy Drive Controller
INTERRUPT_NOERRCODE 39 ; LPT 1
HWINT 0, 32 ; Programmable Interval Timer
HWINT 1, 33 ; Keyboard
HWINT 2, 34 ; Slave PIC
HWINT 3, 35 ; COM 2/4
HWINT 4, 36 ; COM 1/3
HWINT 5, 37 ; LPT 2
HWINT 6, 38 ; Floppy Drive Controller
HWINT 7, 39 ; LPT 1
INTERRUPT_NOERRCODE 40 ; Real Time Clock
INTERRUPT_NOERRCODE 41 ; Master PIC
INTERRUPT_NOERRCODE 42 ; Reserved
INTERRUPT_NOERRCODE 43 ; Reserved
INTERRUPT_NOERRCODE 44 ; Reserved
INTERRUPT_NOERRCODE 45 ; Coprocessor exception
INTERRUPT_NOERRCODE 46 ; Hard Drive Controller
INTERRUPT_NOERRCODE 47 ; Reserved
HWINT 8, 40 ; Real Time Clock
HWINT 9, 41 ; Master PIC
HWINT 10, 42 ; Reserved
HWINT 11, 43 ; Reserved
HWINT 12, 44 ; Reserved
HWINT 13, 45 ; Coprocessor exception
HWINT 14, 46 ; Hard Drive Controller
HWINT 15, 47 ; Reserved
hwint_common_stub:
pusha ; Pushes edi,esi,ebp,esp,ebx,edx,ecx,eax
mov ax, ds ; Lower 16-bits of eax = ds.
push eax ; save the data segment descriptor
mov ax, GDT_KERNEL_DS_SELECTOR
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
call hwint_handler
pop eax ; reload the original data segment descriptor
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
popa ; Pops edi,esi,ebp...
add esp, 8 ; Cleans up the pushed error code and pushed ISR number
sti
iret ; pops 5 things at once: CS, EIP, EFLAGS, SS, and ESP
INTERRUPT_COMMON hwint_handler, GDT_KERNEL_DS_SELECTOR

View File

@ -5,21 +5,21 @@ typedef void(*hwint_handler_t)();
void hwint_register_handler(unsigned int int_no, hwint_handler_t handler);
void hwint_0();
void hwint_1();
void hwint_2();
void hwint_3();
void hwint_4();
void hwint_5();
void hwint_6();
void hwint_7();
void hwint_8();
void hwint_9();
void hwint_10();
void hwint_11();
void hwint_12();
void hwint_13();
void hwint_14();
void hwint_15();
void interrupt_32();
void interrupt_33();
void interrupt_34();
void interrupt_35();
void interrupt_36();
void interrupt_37();
void interrupt_38();
void interrupt_39();
void interrupt_40();
void interrupt_41();
void interrupt_42();
void interrupt_43();
void interrupt_44();
void interrupt_45();
void interrupt_46();
void interrupt_47();
#endif

43
arch/interrupt.asm Normal file
View File

@ -0,0 +1,43 @@
%macro INTERRUPT_NOERRCODE 1
[GLOBAL interrupt_%1]
interrupt_%1:
cli
push byte 0
push byte %1
jmp interrupt_common
%endmacro
%macro INTERRUPT_ERRCODE 1
[GLOBAL interrupt_%1]
interrupt_%1:
cli
push byte %1
jmp interrupt_common
%endmacro
%macro INTERRUPT_COMMON 2
interrupt_common:
pusha ; Pushes edi,esi,ebp,esp,ebx,edx,ecx,eax
mov ax, ds ; Lower 16-bits of eax = ds.
push eax ; save the data segment descriptor
mov ax, %2
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
call %1
pop eax ; reload the original data segment descriptor
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
popa ; Pops edi,esi,ebp...
add esp, 8 ; Cleans up the pushed error code and pushed ISR number
sti
iret ; pops 5 things at once: CS, EIP, EFLAGS, SS, and ESP
%endmacro

View File

@ -80,57 +80,57 @@ void protected_initialize()
kmemset(idt_entries, 0, sizeof(idt_entries));
idt_set_gate(0, (unsigned int)exception_0, 0x08, 0x8E);
idt_set_gate(1, (unsigned int)exception_1, 0x08, 0x8E);
idt_set_gate(2, (unsigned int)exception_2, 0x08, 0x8E);
idt_set_gate(3, (unsigned int)exception_3, 0x08, 0x8E);
idt_set_gate(4, (unsigned int)exception_4, 0x08, 0x8E);
idt_set_gate(5, (unsigned int)exception_5, 0x08, 0x8E);
idt_set_gate(6, (unsigned int)exception_6, 0x08, 0x8E);
idt_set_gate(7, (unsigned int)exception_7, 0x08, 0x8E);
idt_set_gate(8, (unsigned int)exception_8, 0x08, 0x8E);
idt_set_gate(9, (unsigned int)exception_9, 0x08, 0x8E);
idt_set_gate(10, (unsigned int)exception_10, 0x08, 0x8E);
idt_set_gate(11, (unsigned int)exception_11, 0x08, 0x8E);
idt_set_gate(12, (unsigned int)exception_12, 0x08, 0x8E);
idt_set_gate(13, (unsigned int)exception_13, 0x08, 0x8E);
idt_set_gate(14, (unsigned int)exception_14, 0x08, 0x8E);
idt_set_gate(15, (unsigned int)exception_15, 0x08, 0x8E);
idt_set_gate(16, (unsigned int)exception_16, 0x08, 0x8E);
idt_set_gate(17, (unsigned int)exception_17, 0x08, 0x8E);
idt_set_gate(18, (unsigned int)exception_18, 0x08, 0x8E);
idt_set_gate(19, (unsigned int)exception_19, 0x08, 0x8E);
idt_set_gate(20, (unsigned int)exception_20, 0x08, 0x8E);
idt_set_gate(21, (unsigned int)exception_21, 0x08, 0x8E);
idt_set_gate(22, (unsigned int)exception_22, 0x08, 0x8E);
idt_set_gate(23, (unsigned int)exception_23, 0x08, 0x8E);
idt_set_gate(24, (unsigned int)exception_24, 0x08, 0x8E);
idt_set_gate(25, (unsigned int)exception_25, 0x08, 0x8E);
idt_set_gate(26, (unsigned int)exception_26, 0x08, 0x8E);
idt_set_gate(27, (unsigned int)exception_27, 0x08, 0x8E);
idt_set_gate(28, (unsigned int)exception_28, 0x08, 0x8E);
idt_set_gate(29, (unsigned int)exception_29, 0x08, 0x8E);
idt_set_gate(30, (unsigned int)exception_30, 0x08, 0x8E);
idt_set_gate(31, (unsigned int)exception_31, 0x08, 0x8E);
idt_set_gate(0, (unsigned int)interrupt_0, 0x08, 0x8E);
idt_set_gate(1, (unsigned int)interrupt_1, 0x08, 0x8E);
idt_set_gate(2, (unsigned int)interrupt_2, 0x08, 0x8E);
idt_set_gate(3, (unsigned int)interrupt_3, 0x08, 0x8E);
idt_set_gate(4, (unsigned int)interrupt_4, 0x08, 0x8E);
idt_set_gate(5, (unsigned int)interrupt_5, 0x08, 0x8E);
idt_set_gate(6, (unsigned int)interrupt_6, 0x08, 0x8E);
idt_set_gate(7, (unsigned int)interrupt_7, 0x08, 0x8E);
idt_set_gate(8, (unsigned int)interrupt_8, 0x08, 0x8E);
idt_set_gate(9, (unsigned int)interrupt_9, 0x08, 0x8E);
idt_set_gate(10, (unsigned int)interrupt_10, 0x08, 0x8E);
idt_set_gate(11, (unsigned int)interrupt_11, 0x08, 0x8E);
idt_set_gate(12, (unsigned int)interrupt_12, 0x08, 0x8E);
idt_set_gate(13, (unsigned int)interrupt_13, 0x08, 0x8E);
idt_set_gate(14, (unsigned int)interrupt_14, 0x08, 0x8E);
idt_set_gate(15, (unsigned int)interrupt_15, 0x08, 0x8E);
idt_set_gate(16, (unsigned int)interrupt_16, 0x08, 0x8E);
idt_set_gate(17, (unsigned int)interrupt_17, 0x08, 0x8E);
idt_set_gate(18, (unsigned int)interrupt_18, 0x08, 0x8E);
idt_set_gate(19, (unsigned int)interrupt_19, 0x08, 0x8E);
idt_set_gate(20, (unsigned int)interrupt_20, 0x08, 0x8E);
idt_set_gate(21, (unsigned int)interrupt_21, 0x08, 0x8E);
idt_set_gate(22, (unsigned int)interrupt_22, 0x08, 0x8E);
idt_set_gate(23, (unsigned int)interrupt_23, 0x08, 0x8E);
idt_set_gate(24, (unsigned int)interrupt_24, 0x08, 0x8E);
idt_set_gate(25, (unsigned int)interrupt_25, 0x08, 0x8E);
idt_set_gate(26, (unsigned int)interrupt_26, 0x08, 0x8E);
idt_set_gate(27, (unsigned int)interrupt_27, 0x08, 0x8E);
idt_set_gate(28, (unsigned int)interrupt_28, 0x08, 0x8E);
idt_set_gate(29, (unsigned int)interrupt_29, 0x08, 0x8E);
idt_set_gate(30, (unsigned int)interrupt_30, 0x08, 0x8E);
idt_set_gate(31, (unsigned int)interrupt_31, 0x08, 0x8E);
idt_set_gate(32, (unsigned int)hwint_0, 0x08, 0x8E);
idt_set_gate(33, (unsigned int)hwint_1, 0x08, 0x8E);
idt_set_gate(34, (unsigned int)hwint_2, 0x08, 0x8E);
idt_set_gate(35, (unsigned int)hwint_3, 0x08, 0x8E);
idt_set_gate(36, (unsigned int)hwint_4, 0x08, 0x8E);
idt_set_gate(37, (unsigned int)hwint_5, 0x08, 0x8E);
idt_set_gate(38, (unsigned int)hwint_6, 0x08, 0x8E);
idt_set_gate(39, (unsigned int)hwint_7, 0x08, 0x8E);
idt_set_gate(40, (unsigned int)hwint_8, 0x08, 0x8E);
idt_set_gate(41, (unsigned int)hwint_9, 0x08, 0x8E);
idt_set_gate(42, (unsigned int)hwint_10, 0x08, 0x8E);
idt_set_gate(43, (unsigned int)hwint_11, 0x08, 0x8E);
idt_set_gate(44, (unsigned int)hwint_12, 0x08, 0x8E);
idt_set_gate(45, (unsigned int)hwint_13, 0x08, 0x8E);
idt_set_gate(46, (unsigned int)hwint_14, 0x08, 0x8E);
idt_set_gate(47, (unsigned int)hwint_15, 0x08, 0x8E);
idt_set_gate(32, (unsigned int)interrupt_32, 0x08, 0x8E);
idt_set_gate(33, (unsigned int)interrupt_33, 0x08, 0x8E);
idt_set_gate(34, (unsigned int)interrupt_34, 0x08, 0x8E);
idt_set_gate(35, (unsigned int)interrupt_35, 0x08, 0x8E);
idt_set_gate(36, (unsigned int)interrupt_36, 0x08, 0x8E);
idt_set_gate(37, (unsigned int)interrupt_37, 0x08, 0x8E);
idt_set_gate(38, (unsigned int)interrupt_38, 0x08, 0x8E);
idt_set_gate(39, (unsigned int)interrupt_39, 0x08, 0x8E);
idt_set_gate(40, (unsigned int)interrupt_40, 0x08, 0x8E);
idt_set_gate(41, (unsigned int)interrupt_41, 0x08, 0x8E);
idt_set_gate(42, (unsigned int)interrupt_42, 0x08, 0x8E);
idt_set_gate(43, (unsigned int)interrupt_43, 0x08, 0x8E);
idt_set_gate(44, (unsigned int)interrupt_44, 0x08, 0x8E);
idt_set_gate(45, (unsigned int)interrupt_45, 0x08, 0x8E);
idt_set_gate(46, (unsigned int)interrupt_46, 0x08, 0x8E);
idt_set_gate(47, (unsigned int)interrupt_47, 0x08, 0x8E);
idt_set_gate(INT_SYSCALL, (unsigned int)syscall_gate, 0x08, 0x8E | 0x60);
idt_set_gate(INT_SYSCALL, (unsigned int)interrupt_0x80, 0x08, 0x8E | 0x60);
logger_info("Load GDT.");

View File

@ -1,37 +1,9 @@
#include "config.h"
%include "interrupt.asm"
[EXTERN syscall_handler]
[GLOBAL syscall_gate]
INTERRUPT_NOERRCODE INT_SYSCALL
syscall_gate:
cli
push byte 0
push byte INT_SYSCALL
pusha ; Pushes edi,esi,ebp,esp,ebx,edx,ecx,eax
mov ax, ds ; Lower 16-bits of eax = ds.
push eax ; save the data segment descriptor
mov ax, GDT_KERNEL_DS_SELECTOR
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
call syscall_handler
pop eax ; reload the original data segment descriptor
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
popa ; Pops edi,esi,ebp...
add esp, 8 ; Cleans up the pushed error code and pushed ISR number
sti
iret ; pops 5 things at once: CS, EIP, EFLAGS, SS, and ESP
INTERRUPT_COMMON syscall_handler, GDT_KERNEL_DS_SELECTOR

View File

@ -1,6 +1,6 @@
#ifndef KERNELMQ_INCLUDED_SYSCALL
#define KERNELMQ_INCLUDED_SYSCALL 1
void syscall_gate();
void interrupt_0x80();
#endif