-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Doing Interrupts in C, no Assembly needed.
- Loading branch information
1 parent
e4d4bfe
commit e1f8757
Showing
9 changed files
with
195 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,13 @@ | ||
set architecture i386:x86-64 | ||
# AT&T syntax is awful | ||
set disassembly-flavor intel | ||
|
||
# Pretty-printing :) | ||
enable pretty-printer | ||
|
||
# Cool layout | ||
layout split | ||
|
||
file Kernel/Arch/x86_64-pc/BlobOS.elf | ||
|
||
target remote :1234 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 10 additions & 2 deletions
12
Kernel/Arch/x86_64-pc/Include/System/ISR.h → ...rch/x86_64-pc/Include/System/Interrupts.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,20 @@ | ||
#pragma once | ||
#include <stdint.h> | ||
|
||
struct interrupt_frame { | ||
uint64_t ip; | ||
uint64_t cs; | ||
uint64_t flags; | ||
uint64_t sp; | ||
uint64_t ss; | ||
}; | ||
|
||
typedef struct { | ||
uint64_t r15, r14, r13, r12, r11, r10, r9, r8; | ||
uint64_t rdi, rsi, rbx, rdx, rcx, rax; | ||
uint64_t interrupt, error; | ||
uint64_t error; | ||
} __attribute__((packed)) Registers; | ||
|
||
typedef void (*ISRHandler)(Registers *regs); | ||
|
||
void isr_register_handler(int interrupt, ISRHandler handler); | ||
void Load_Exceptions(void); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[bits 64] | ||
[global reloadSegments] | ||
|
||
reloadSegments: | ||
push 0x08 ; Push code segment to stack | ||
lea rax, [rel .reload_CS] ; Load address of .reload_CS into RAX | ||
push rax ; Push this value to the stack | ||
retfq ; Perform a far return | ||
.reload_CS: | ||
; Reload data segment registers | ||
mov ax, 0x10 ; 0x10 is the 64-bit data segment | ||
mov ds, ax | ||
mov es, ax | ||
mov fs, ax | ||
mov gs, ax | ||
mov ss, ax | ||
ret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
#include <Kernel/Panic.h> | ||
#include <LibK/stdio.h> | ||
#include <System/IDT.h> | ||
#include <System/Interrupts.h> | ||
|
||
__attribute__((interrupt)) void C_Int_0(struct interrupt_frame *frame) { | ||
kprintf("Divide By Zero Error #00\n"); | ||
} | ||
|
||
__attribute__((interrupt)) void C_Int_1(struct interrupt_frame *frame) { | ||
panic("Debug Error #DB"); | ||
} | ||
__attribute__((interrupt)) void C_Int_2(struct interrupt_frame *frame) { | ||
panic("NMI Interrupt #--"); | ||
} | ||
__attribute__((interrupt)) void C_Int_3(struct interrupt_frame *frame) { | ||
panic("Breakpoint #BP"); | ||
} | ||
__attribute__((interrupt)) void C_Int_4(struct interrupt_frame *frame) { | ||
panic("Overflow #OF"); | ||
} | ||
__attribute__((interrupt)) void C_Int_5(struct interrupt_frame *frame) { | ||
panic("BOUND Range Exceeded #BR"); | ||
} | ||
__attribute__((interrupt)) void C_Int_6(struct interrupt_frame *frame) { | ||
panic("Invalid Opcode #UD"); | ||
} | ||
__attribute__((interrupt)) void C_Int_7(struct interrupt_frame *frame) { | ||
panic("Device Not Available #NM"); | ||
} | ||
__attribute__((interrupt)) void C_Int_8(struct interrupt_frame *frame, | ||
uint64_t error_code) { | ||
panic("Double Fault #DF"); | ||
} | ||
__attribute__((interrupt)) void C_Int_9(struct interrupt_frame *frame) { | ||
panic("Coprocessor Segment Overrun #NA"); | ||
} | ||
__attribute__((interrupt)) void C_Int_10(struct interrupt_frame *frame, | ||
uint64_t error_code) { | ||
panic("Invalid TSS #TS"); | ||
} | ||
__attribute__((interrupt)) void C_Int_11(struct interrupt_frame *frame, | ||
uint64_t error_code) { | ||
panic("Segment Not Present #NP"); | ||
} | ||
__attribute__((interrupt)) void C_Int_12(struct interrupt_frame *frame, | ||
uint64_t error_code) { | ||
panic("Stack Segment Fault #SS"); | ||
} | ||
__attribute__((interrupt)) void C_Int_13(struct interrupt_frame *frame, | ||
uint64_t error_code) { | ||
kprintf("%p", error_code); | ||
asm("hlt"); | ||
// panic("General Protection Fault #GP"); | ||
} | ||
__attribute__((interrupt)) void C_Int_14(struct interrupt_frame *frame, | ||
uint64_t error_code) { | ||
uint64_t *faultAddress; | ||
asm volatile("mov %%cr2, %0" : "=r"(faultAddress)::"memory"); | ||
kprintf("%p", faultAddress); | ||
// panic("Page Fault #PF"); | ||
} | ||
__attribute__((interrupt)) void C_Int_16(struct interrupt_frame *frame) { | ||
panic("FPU Floating-Point Error #MF"); | ||
} | ||
__attribute__((interrupt)) void C_Int_17(struct interrupt_frame *frame, | ||
uint64_t error_code) { | ||
panic("Alignment Check #AC"); | ||
} | ||
__attribute__((interrupt)) void C_Int_18(struct interrupt_frame *frame) { | ||
panic("Machine Check #MC"); | ||
} | ||
__attribute__((interrupt)) void C_Int_19(struct interrupt_frame *frame) { | ||
panic("SIMD Floating-Point #XF"); | ||
} | ||
|
||
// 20 to 31 are Intel reserved. | ||
|
||
__attribute__((interrupt)) void | ||
Default_INT_Handler(struct interrupt_frame *frame) { | ||
kprintf("Unhandled interrupt!\n"); | ||
} | ||
|
||
void Load_Exceptions(void) { | ||
IDT_Add_Int(0, C_Int_0, IDT_FLAGS_INTERRUPT_GATE); | ||
IDT_Add_Int(1, C_Int_1, IDT_FLAGS_INTERRUPT_GATE); | ||
IDT_Add_Int(2, C_Int_2, IDT_FLAGS_INTERRUPT_GATE); | ||
IDT_Add_Int(3, C_Int_3, IDT_FLAGS_INTERRUPT_GATE); | ||
IDT_Add_Int(4, C_Int_4, IDT_FLAGS_INTERRUPT_GATE); | ||
IDT_Add_Int(5, C_Int_5, IDT_FLAGS_INTERRUPT_GATE); | ||
IDT_Add_Int(6, C_Int_6, IDT_FLAGS_INTERRUPT_GATE); | ||
IDT_Add_Int(7, C_Int_7, IDT_FLAGS_INTERRUPT_GATE); | ||
IDT_Add_Int(8, C_Int_8, IDT_FLAGS_INTERRUPT_GATE); | ||
IDT_Add_Int(9, C_Int_9, IDT_FLAGS_INTERRUPT_GATE); | ||
IDT_Add_Int(10, C_Int_10, IDT_FLAGS_INTERRUPT_GATE); | ||
IDT_Add_Int(11, C_Int_11, IDT_FLAGS_INTERRUPT_GATE); | ||
IDT_Add_Int(12, C_Int_12, IDT_FLAGS_INTERRUPT_GATE); | ||
IDT_Add_Int(13, C_Int_13, IDT_FLAGS_INTERRUPT_GATE); | ||
IDT_Add_Int(14, C_Int_14, IDT_FLAGS_INTERRUPT_GATE); | ||
// 15 is Intel reserved. | ||
IDT_Add_Int(15, Default_INT_Handler, IDT_FLAGS_INTERRUPT_GATE); | ||
IDT_Add_Int(16, C_Int_16, IDT_FLAGS_INTERRUPT_GATE); | ||
IDT_Add_Int(17, C_Int_17, IDT_FLAGS_INTERRUPT_GATE); | ||
IDT_Add_Int(18, C_Int_18, IDT_FLAGS_INTERRUPT_GATE); | ||
IDT_Add_Int(19, C_Int_19, IDT_FLAGS_INTERRUPT_GATE); | ||
for (uint8_t i = 20; i < 255; i++) { | ||
IDT_Add_Int(i, Default_INT_Handler, IDT_FLAGS_INTERRUPT_GATE); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters