starting point for interrupt handlers though some of this code isn't ready to be used, it serves as a reference point for later

This commit is contained in:
shockrah 2019-10-13 23:14:49 -07:00
parent c84997dea0
commit 7d9800f1eb
7 changed files with 167 additions and 0 deletions

3
idt.h Normal file
View File

@ -0,0 +1,3 @@
#include "types.h"
void load_idt(void* idt_addr);

7
idt.s Normal file
View File

@ -0,0 +1,7 @@
global load_idt
load_idt:
; takes the address of the first element in the Interrupt descriptor table
mov eax, [esp+4]
lidt eax
ret

96
interrupt_entry.s Normal file
View File

@ -0,0 +1,96 @@
; Dispatching to our interrupt handler code from assembly because some of this is
; enourmous pain in C
; Save the register state
; call the C handler
; executes iret(expects the stack to look like struct stack_state)
; If the interrupt we need to handle doesn't produce an error code then must provide a 0
; for consistency
extern interrupt_handler
%macro no_err_handler 1
global interrupt_handler_%1 ; defined in interrupts.h
interrupt_handler_%1:
push dword 0
push dword %1
jmp common_int_handler
%endmacro
; deals with the intterrupts that do give us an error code
%macro err_code_handler 1
global interrupt_handler_%1
interrupt_handler_%1:
push dword %1
jmp common_int_handler
%endmacro
common_int_handler:
; save thigns before do our C-call
push eax
push ebx
push ecx
push edx
push cs
push ds
push es
push fs
push gs
push ss
push esi
push edi
push ebp
push esp
call interrupt_handler
; restore state to the function
pop eax
pop ebx
pop ecx
pop edx
pop cs
pop ds
pop es
pop fs
pop gs
pop ss
pop esi
pop edi
pop ebp
pop esp
add esp, 8
; special instruction which lets us jump back to code
; which was previously interrupted
iret
no_err_handler 1
no_err_handler 2
no_err_handler 3
no_err_handler 4
no_err_handler 5
no_err_handler 6
no_err_handler 7
err_code_handler 8
no_err_handler 9
err_code_handler 10
err_code_handler 11
err_code_handler 12
err_code_handler 13
err_code_handler 14
no_err_handler 15
no_err_handler 16
err_code_handler 17

5
interrupts.c Normal file
View File

@ -0,0 +1,5 @@
#include "interrupts.h"
#include "types.h"
void interrupt_handler(struct cpu_reg_state cpu, struct stack_state stack, u32 interrupt_code) {
}

34
interrupts.h Normal file
View File

@ -0,0 +1,34 @@
#include "types.h"
struct cpu_reg_state {
u32 eax;
u32 ebx;
u32 ecx;
u32 edx;
u16 cs;
u16 ds;
u16 es;
u16 fs;
u16 gs;
u16 ss;
u32 esi;
u32 edi;
u32 ebp;
u32 eip;
u32 esp;
u32 eflags;
}__attribute__((packed));
// Populated by the interrupt handler itself
struct stack_state {
u32 error_code; // 8 10 11 12 13 14 17 <= ony interrupts that actually use the error code
u32 eip;
u16 cs;
u32 eflags;
};
void interrupt_handler(struct cpu_reg_state, struct stack_state, u32);

14
kernel.c Normal file
View File

@ -0,0 +1,14 @@
#include "kernel.h"
#include "types.h"
#include "gdt.h"
#include "idt.h"
GDT GDT_PTR;
GDT _GDT[6]; // because there are only 6 segments we care about
u32 kinit() {
init_segment_selectors(); // found in gdt.s
}
void kmain() {
}

8
kernel.h Normal file
View File

@ -0,0 +1,8 @@
#include "types.h"
#include "interrupts.h"
// handles the main initialization things for the main kernel routine later on
u32 kinit();
void kmain();