Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdint.h"
- #include "intr.h"
- #include "console.h"
- extern void intr_stub_0();
- extern void intr_stub_32();
- #define GDT_FLAG_DATASEG 0x02
- #define GDT_FLAG_CODESEG 0x0a
- #define GDT_FLAG_TSS 0x09
- #define GDT_FLAG_SEGMENT 0x10
- #define GDT_FLAG_RING0 0x00
- #define GDT_FLAG_RING3 0x60
- #define GDT_FLAG_PRESENT 0x80
- #define GDT_FLAG_4K 0x800
- #define GDT_FLAG_32_BIT 0x400
- #define GDT_ENTRIES 5
- static uint64_t gdt[GDT_ENTRIES];
- #define IDT_ENTRIES 256
- static long long unsigned int idt[IDT_ENTRIES];
- static void gdt_set_entry(int i, unsigned int base, unsigned int limit, int flags) {
- gdt[i] = limit & 0xffffLL;
- gdt[i] |= (base & 0xffffffLL) << 16;
- gdt[i] |= (flags & 0xffLL) << 40;
- gdt[i] |= ((limit >> 16) & 0xfLL) << 48;
- gdt[i] |= ((flags >> 8) & 0xffLL) << 52;
- gdt[i] |= ((base >> 24) & 0xffLL) << 56;
- }
- void init_gdt() {
- struct {
- uint16_t limit;
- void* pointer;
- } __attribute__((packed)) gdtp = {
- .limit = GDT_ENTRIES * 8 - 1,
- .pointer = gdt,
- };
- gdt_set_entry(0, 0, 0, 0);
- gdt_set_entry(1, 0, 0xfffff, GDT_FLAG_SEGMENT | GDT_FLAG_32_BIT | GDT_FLAG_CODESEG | GDT_FLAG_4K | GDT_FLAG_PRESENT);
- gdt_set_entry(2, 0, 0xfffff, GDT_FLAG_SEGMENT | GDT_FLAG_32_BIT | GDT_FLAG_DATASEG | GDT_FLAG_4K | GDT_FLAG_PRESENT);
- gdt_set_entry(3, 0, 0xfffff, GDT_FLAG_SEGMENT | GDT_FLAG_32_BIT | GDT_FLAG_CODESEG | GDT_FLAG_4K | GDT_FLAG_PRESENT | GDT_FLAG_RING3);
- gdt_set_entry(4, 0, 0xfffff, GDT_FLAG_SEGMENT | GDT_FLAG_32_BIT | GDT_FLAG_DATASEG | GDT_FLAG_4K | GDT_FLAG_PRESENT | GDT_FLAG_RING3);
- asm volatile("lgdt %0" : : "m" (gdtp));
- asm volatile(
- "mov $0x10, %ax;"
- "mov %ax, %ds;"
- "mov %ax, %es;"
- "mov %ax, %ss;"
- "ljmp $0x8, $.1;"
- ".1:"
- );
- }
- #define IDT_FLAG_INTERRUPT_GATE 0xe
- #define IDT_FLAG_PRESENT 0x80
- #define IDT_FLAG_RING0 0x00
- #define IDT_FLAG_RING3 0x60
- static void idt_set_entry(int i, void (*fn)(), unsigned int selector, int flags) {
- unsigned long int handler = (unsigned long int) fn;
- idt[i] = handler & 0xffffLL;
- idt[i] = (selector & 0xffffLL) << 16;
- idt[i] = (flags & 0xffLL) << 40;
- idt[i] = ((handler >> 16) & 0xffffLL) << 48;
- }
- void init_intr() {
- struct {
- unsigned short int limit;
- void* pointer;
- } __attribute__((packed)) idtp = {
- .limit = IDT_ENTRIES * 8 - 1,
- .pointer = idt,
- };
- idt_set_entry(0, intr_stub_32, 0x8, IDT_FLAG_INTERRUPT_GATE | IDT_FLAG_RING0 | IDT_FLAG_PRESENT);
- asm volatile("lidt %0" : : "m" (idtp));
- }
- void handle_interrupt() {
- kprintf("an interrupt!\n");
- while(1);
- }
Advertisement
Add Comment
Please, Sign In to add comment