Guest User

intr.c

a guest
May 14th, 2015
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.50 KB | None | 0 0
  1. #include "stdint.h"
  2. #include "intr.h"
  3. #include "console.h"
  4.  
  5. extern void intr_stub_0();
  6. extern void intr_stub_32();
  7.  
  8. #define GDT_FLAG_DATASEG 0x02
  9. #define GDT_FLAG_CODESEG 0x0a
  10. #define GDT_FLAG_TSS 0x09
  11.  
  12. #define GDT_FLAG_SEGMENT 0x10
  13. #define GDT_FLAG_RING0 0x00
  14. #define GDT_FLAG_RING3 0x60
  15. #define GDT_FLAG_PRESENT 0x80
  16.  
  17. #define GDT_FLAG_4K 0x800
  18. #define GDT_FLAG_32_BIT 0x400
  19.  
  20. #define GDT_ENTRIES 5
  21. static uint64_t gdt[GDT_ENTRIES];
  22.  
  23. #define IDT_ENTRIES 256
  24. static long long unsigned int idt[IDT_ENTRIES];
  25.  
  26. static void gdt_set_entry(int i, unsigned int base, unsigned int limit, int flags) {
  27.     gdt[i] = limit & 0xffffLL;
  28.     gdt[i] |= (base & 0xffffffLL) << 16;
  29.     gdt[i] |= (flags & 0xffLL) << 40;
  30.     gdt[i] |= ((limit >> 16) & 0xfLL) << 48;
  31.     gdt[i] |= ((flags >> 8) & 0xffLL) << 52;
  32.     gdt[i] |= ((base >> 24) & 0xffLL) << 56;
  33. }
  34.  
  35. void init_gdt() {
  36.     struct {
  37.         uint16_t limit;
  38.         void* pointer;
  39.     } __attribute__((packed)) gdtp = {
  40.         .limit = GDT_ENTRIES * 8 - 1,
  41.         .pointer = gdt,
  42.     };
  43.  
  44.     gdt_set_entry(0, 0, 0, 0);
  45.     gdt_set_entry(1, 0, 0xfffff, GDT_FLAG_SEGMENT | GDT_FLAG_32_BIT | GDT_FLAG_CODESEG | GDT_FLAG_4K | GDT_FLAG_PRESENT);
  46.     gdt_set_entry(2, 0, 0xfffff, GDT_FLAG_SEGMENT | GDT_FLAG_32_BIT | GDT_FLAG_DATASEG | GDT_FLAG_4K | GDT_FLAG_PRESENT);
  47.     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);
  48.     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);
  49.  
  50.     asm volatile("lgdt %0" : : "m" (gdtp));
  51.  
  52.     asm volatile(
  53.         "mov $0x10, %ax;"
  54.         "mov %ax, %ds;"
  55.         "mov %ax, %es;"
  56.         "mov %ax, %ss;"
  57.         "ljmp $0x8, $.1;"
  58.         ".1:"
  59.     );
  60. }
  61.  
  62. #define IDT_FLAG_INTERRUPT_GATE 0xe
  63. #define IDT_FLAG_PRESENT 0x80
  64. #define IDT_FLAG_RING0 0x00
  65. #define IDT_FLAG_RING3 0x60
  66.  
  67. static void idt_set_entry(int i, void (*fn)(), unsigned int selector, int flags) {
  68.     unsigned long int handler = (unsigned long int) fn;
  69.     idt[i] = handler & 0xffffLL;
  70.     idt[i] = (selector & 0xffffLL) << 16;
  71.     idt[i] = (flags & 0xffLL) << 40;
  72.     idt[i] = ((handler >> 16) & 0xffffLL) << 48;
  73. }
  74.  
  75. void init_intr() {
  76.     struct {
  77.         unsigned short int limit;
  78.         void* pointer;
  79.     } __attribute__((packed)) idtp = {
  80.         .limit = IDT_ENTRIES * 8 - 1,
  81.         .pointer = idt,
  82.     };
  83.  
  84.     idt_set_entry(0, intr_stub_32, 0x8, IDT_FLAG_INTERRUPT_GATE | IDT_FLAG_RING0 | IDT_FLAG_PRESENT);
  85.  
  86.     asm volatile("lidt %0" : : "m" (idtp));
  87. }
  88.  
  89. void handle_interrupt() {
  90.     kprintf("an interrupt!\n");
  91.     while(1);
  92. }
Advertisement
Add Comment
Please, Sign In to add comment