software interrupt
This commit is contained in:
parent
57ec3792e9
commit
8620679634
3 changed files with 61 additions and 2 deletions
|
|
@ -2,5 +2,6 @@
|
||||||
#define KARLOS_MEM_H
|
#define KARLOS_MEM_H
|
||||||
|
|
||||||
void init_gdt();
|
void init_gdt();
|
||||||
|
void init_idt();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -95,10 +95,13 @@ void _start()
|
||||||
|
|
||||||
// memory stuff
|
// memory stuff
|
||||||
init_gdt();
|
init_gdt();
|
||||||
|
init_idt();
|
||||||
|
|
||||||
println("Test after init_gdt");
|
println("Test after init");
|
||||||
|
|
||||||
check_initrd();
|
__asm__("int $0x80" :: );
|
||||||
|
|
||||||
|
// check_initrd();
|
||||||
|
|
||||||
// hang for now
|
// hang for now
|
||||||
panic("end of kernel");
|
panic("end of kernel");
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include "output.h" // remove later, this is only for interrupt handler
|
||||||
|
|
||||||
uint64_t gdt[3];
|
uint64_t gdt[3];
|
||||||
|
|
||||||
|
|
@ -49,3 +50,57 @@ void init_gdt() {
|
||||||
__asm__ ("mov %0, %%ds" :: "r"(DATA_SEGMENT << 3));
|
__asm__ ("mov %0, %%ds" :: "r"(DATA_SEGMENT << 3));
|
||||||
// TODO set code segment -> iret?
|
// TODO set code segment -> iret?
|
||||||
}
|
}
|
||||||
|
|
||||||
|
__attribute__ ((packed))
|
||||||
|
struct int_desc_entry {
|
||||||
|
uint16_t offset1;
|
||||||
|
uint16_t selector;
|
||||||
|
uint8_t ist;
|
||||||
|
uint8_t attr;
|
||||||
|
uint16_t offset2;
|
||||||
|
uint32_t offset3;
|
||||||
|
uint32_t pad;
|
||||||
|
};
|
||||||
|
|
||||||
|
#define PRIV_KERNEL 0
|
||||||
|
#define PRIV_USER 3
|
||||||
|
|
||||||
|
void write_segment_selector(uint16_t *ss, uint16_t index) {
|
||||||
|
*ss = (uint16_t)PRIV_KERNEL
|
||||||
|
| 0 // use GDT
|
||||||
|
| (index << 3)
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define GATE_TYPE_INTERRUPT 0xe
|
||||||
|
|
||||||
|
void write_int_desc_entry(struct int_desc_entry *e, uint64_t offset) {
|
||||||
|
e->offset1 = (uint16_t) (offset & 0xffff);
|
||||||
|
write_segment_selector(&e->selector, CODE_SEGMENT);
|
||||||
|
e->ist = 0;
|
||||||
|
e->attr = (uint8_t)GATE_TYPE_INTERRUPT
|
||||||
|
| (uint8_t)(PRIV_KERNEL << 5)
|
||||||
|
| (uint8_t)(1 << 7) // present
|
||||||
|
;
|
||||||
|
e->offset2 = (uint16_t) ((offset >> 16) & 0xffff);
|
||||||
|
e->offset3 = (uint32_t) ((offset >> 32) & 0xffffffff);
|
||||||
|
e->pad = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void basic_interrupt_handler(void) {
|
||||||
|
panic("Hello Interrupt!\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
#define NUM_INTERRUPTS 256
|
||||||
|
struct int_desc_entry idt[NUM_INTERRUPTS];
|
||||||
|
|
||||||
|
void init_idt() {
|
||||||
|
write_int_desc_entry(&idt[0x80], (uint64_t)(intptr_t)basic_interrupt_handler);
|
||||||
|
|
||||||
|
uint8_t idtr[10];
|
||||||
|
|
||||||
|
*(uint16_t*)idtr = sizeof(idt) - 1;
|
||||||
|
*(uint64_t**)(idtr + 2) = (uint64_t*)idt;
|
||||||
|
|
||||||
|
__asm__ ("lidt (%0)" :: "r"(idtr));
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue