OurOS 0.1
Operating System built by McGill Students
 
Loading...
Searching...
No Matches
idt.h
1#ifndef _CPU_IDT_H
2#define _CPU_IDT_H
3
4#include <stdint.h>
5
6// Flags (strikingly similar to GDT)
7#define IDT_PRESENT 0x80
8#define IDT_RING_0 0x00
9#define IDT_RING_1 0x20
10#define IDT_RING_2 0x40
11#define IDT_RING_3 0x60
12#define IDT_INT_GATE 0x0E
13#define IDT_TRAP_GATE 0x0F
14
15#define IDT_KTRAP_GATE (IDT_PRESENT + IDT_RING_0 + IDT_TRAP_GATE)
16#define IDT_KINT_GATE (IDT_PRESENT + IDT_RING_0 + IDT_INT_GATE)
17
18#define IDT_ENTRY_CREATE(addr, selector, ist, flags) { \
19 (addr) & 0xffff, /* 16-bit address (low) */ \
20 (selector) & 0xffff, /* 16-bit Code Selector */ \
21 (ist) & 0xff, /* Interrupt Stack Table */ \
22 (flags) & 0xff, /* Flags (one byte) */ \
23 (addr) >> 16 & 0xffff, /* Second two low bytes */ \
24 (addr) >> 32, /* Four high bytes */ \
25 0, /* Reserved (four bytes) */ \
26}
27
28// A IDT is necessary to handle interrupts
29struct cpu_idt_entry {
30 uint16_t address_low;
31 uint16_t selector;
32 uint8_t ist;
33 uint8_t flags;
34 uint16_t address_mid;
35 uint32_t address_high;
36 uint32_t reserved;
37} __attribute__((packed));
38
39// Represents the contents of the IDTR in memory
40struct cpu_idt_register {
41 uint16_t limit;
42 uint64_t base;
43} __attribute__((packed));
44
45// The data structure placed on the stack
46// by the CPU when an ISR is invoked
47struct cpu_idt_iframe {
48 uint64_t rip;
49 uint64_t cs;
50 uint64_t rflags;
51 uint64_t rsp;
52 uint64_t ss;
53};
54
55// Represents the state of the CPU following an interrupt
56struct cpu_idt_interrupt_state {
57 uint64_t r15;
58 uint64_t r14;
59 uint64_t r13;
60 uint64_t r12;
61 uint64_t r11;
62 uint64_t r10;
63 uint64_t r9;
64 uint64_t r8;
65 uint64_t rsi;
66 uint64_t rdi;
67 uint64_t rdx;
68 uint64_t rcx;
69 uint64_t rbx;
70 uint64_t rax;
71 uint64_t vector;
72 uint64_t error_code;
73 struct cpu_idt_iframe iframe;
74};
75
76
77// Advertise the kernel's idt to the CPU
78void cpu_idt_bind(void);
79// Describe the currently loaded IDT.
80void cpu_idt_describe(char *buf, uint64_t bufsz);
81
82static inline void enable_interrupts() {
83 asm("sti");
84}
85
86static inline void disable_interrupts() {
87 asm("cli");
88}
89
90#endif