LumaOS  v0.1.116
An experimental x86-64 OS
Loading...
Searching...
No Matches
idt.c
Go to the documentation of this file.
1
9#include <arch/x86_64/idt.h>
10#include <arch/x86_64/irq.h>
11#include <kernel/kprintf.h>
12#include <kernel/log.h>
13#include <kernel/vars.h>
14#include <stdint.h>
15
16#include "mm/vmm.h"
17
21static void hcf(void);
22
24const char *idt_exception_table[] = {
25 "#DE Divide Error", // 0
26 "#DB Debug", // 1
27 "NMI Non-maskable Interrupt", // 2
28 "#BP Breakpoint", // 3
29 "#OF Overflow", // 4
30 "#BR Bound Range Exceeded", // 5
31 "#UD Invalid Opcode", // 6
32 "#NM Device Not Available", // 7
33 "#DF Double Fault", // 8
34 " Coprocessor Segment Overrun", // 9
35 "#TS Invalid TSS", // 10
36 "#NP Segment Not Present", // 11
37 "#SS Stack-Segment Fault", // 12
38 "#GP General Protection Fault", // 13
39 "#PF Page Fault", // 14
40 "", // 15
41 "#MF x87 Floating-Point Exception", // 16
42 "#AC Alignment Check", // 17
43 "#MC Machine Check", // 18
44 "#XM SIMD Floating-Point Exception", // 19
45 "#VE Virtualization Exception", // 20
46 "#CP Control Protection Exception", // 21
47 "", // 22
48 "", // 23
49 "", // 24
50 "", // 25
51 "", // 26
52 "", // 27
53 "#HV Hypervisor Injection Exception", // 28
54 "#VC VMM Communication Exception", // 29
55 "#SX Security Exception", // 30
56 "" // 31
57};
58
59struct idt_entry idt[256]; // a global, so it starts as all zeros
60
61struct idtr _idtr;
62
64 // Check if the vector is an IRQ
65 if (frame->vector >= 32 && frame->vector < 48)
66 {
67 irq_handler(frame);
68 return;
69 }
70
71 uint64_t _vector = frame->vector;
72 uint64_t _error_code = frame->error_code;
73
74 if (_vector == 14)
75 {
76 uint64_t fault_address;
77 asm volatile ("mov %%cr2, %0" : "=r" (fault_address));
78
79 if (vmm_page_fault_handler(fault_address, _error_code))
80 return; // Return if the page fault handler handled the fault
81
82 // Else, continue with the exception handling
83 }
84
85 kprintf("\n\n===============\n= EXCEPTION =\n===============\n");
86
87 // Base Info
88 kprintf("\n%ld: %s\n\n", _vector, idt_exception_table[_vector]);
89
90 // If the gate has an error code, print it
91 if (_vector == 8 ||
92 _vector == 10 ||
93 _vector == 11 ||
94 _vector == 12 ||
95 _vector == 13 ||
96 _vector == 14 ||
97 _vector == 17 ||
98 _vector == 21 ||
99 _vector == 29 ||
100 _vector == 30) {
101 kprintf("Error Code: %ld\n\n", _error_code);
102 }
103
104 // Registers
105 kprintf("RIP: %8lx CS: %4lx RFLAGS: %lb RSP: %8lx SS: %4lx\n\n", frame->rip, frame->cs, frame->rflags,
106 frame->rsp, frame->ss);
107 kprintf("RAX: %8lx RBX: %8lx RCX: %8lx RDX: %8lx\n", frame->rax, frame->rbx, frame->rcx, frame->rdx);
108 kprintf("RBP: %8lx RDI: %8lx RSI: %8lx\n", frame->rbp, frame->rdi, frame->rsi);
109 kprintf("R8: %8lx R9: %8lx R10: %8lx R11: %8lx\n", frame->r8, frame->r9, frame->r10, frame->r11);
110 kprintf("R12: %8lx R13: %8lx R14: %8lx R15: %8lx\n\n", frame->r12, frame->r13, frame->r14, frame->r15);
111
112 if (_vector == 14) {
113 // PAGE FAULT
114 uint64_t cr2;
115 asm volatile ("mov %%cr2, %0" : "=r"(cr2));
116
117 bool present = _error_code & 0b1;
118 bool write = (_error_code >> 1) & 0b1;
119 bool user_mode = (_error_code >> 2) & 0b1;
120 bool reserved = (_error_code >> 3) & 0b1;
121 bool instruction_fetch = (_error_code >> 4) & 0b1;
122
123 kprintf("CR2: 0x%lx\n", cr2);
124 kprintf("Present: %s\n", present ? "TRUE" : "FALSE");
125 kprintf("Read/Write: %s\n", write ? "WRITE" : "READ");
126 kprintf("User/Kernel Mode: %s\n", user_mode ? "USER" : "KERNEL");
127 kprintf("Reserved: %s\n", reserved ? "TRUE" : "FALSE");
128 kprintf("Instruction Fetch: %s\n\n", instruction_fetch ? "TRUE" : "FALSE");
129 }
130
131 // BACKTRACE
132 kprintf("Stack Backtrace:\n");
133
134 #define MAX_DEPTH 16
135
136 uint64_t *rbp = (uint64_t *) frame->rbp;
137
138 for (int i = 0; i < MAX_DEPTH; i++) {
139 uint64_t stack_frame = (uint64_t) rbp;
140 uint64_t offset = get_hddm_offset();
141
142 // Validate frame alignment and lower boundary.
143 if (stack_frame == 0 ||
144 stack_frame % 8 != 0 ||
145 stack_frame < offset) {
146 break;
147 }
148
149 // Ensure both frame entries are accessible here.
150 uint64_t previous_rbp = rbp[0];
151 uint64_t return_addr = rbp[1];
152
153 if (return_addr == 0)
154 break;
155
156 kprintf("%16p: Return Addr %16p\n",
157 (void *) stack_frame,
158 (void *) return_addr);
159
160 if (previous_rbp == 0 ||
161 previous_rbp <= stack_frame) {
162 break;
163 }
164
165 rbp = (uint64_t *) previous_rbp;
166 }
167
168 if (_vector != 3)
169 hcf();
170}
171
172void init_idt(void) {
173 LOG_DEBUG("Initializing IDT gates...\n");
174 for (int i = 0; i < 48; i++) {
175 idt_set_gate(i, isr_stub_table[i], 0x08, 0, 0x8E);
176 }
177
178 idt_set_gate(8, isr_stub_table[8], 0x08, 1, 0x8E);
179
180 _idtr.limit = sizeof(idt) - 1;
181 _idtr.base = (uint64_t) &idt;
182
183 LOG_DEBUG("Loading IDT...\n");
184 asm volatile ("lidt %0" : : "m"(_idtr) : "memory");
185 LOG_DEBUG("IDT Loaded successfully!\n\n");
186}
187
188void idt_set_gate(int vector, uint64_t handler, uint16_t selector, uint8_t ist, uint8_t type_attr) {
189 idt[vector].selector = selector;
190 idt[vector].ist = ist & 0b111;
191 idt[vector].type_attr = type_attr;
192 idt[vector].offset_low = handler & 0xffff;
193 idt[vector].offset_mid = (handler >> 16) & 0xffff;
194 idt[vector].offset_high = (handler >> 32) & 0xffffffff;
195}
196
197static void hcf(void) {
198 for (;;) {
199 asm ("hlt"); // Halt the cpu
200 }
201}
const char * idt_exception_table[]
List of exceptions as strings.
Definition idt.c:24
void init_idt(void)
Initializes the IDT and loads it.
Definition idt.c:172
void exception_handler(struct interrupt_frame *frame)
Prints information about an exception.
Definition idt.c:63
static void hcf(void)
Halts the cpu.
Definition idt.c:197
void idt_set_gate(int vector, uint64_t handler, uint16_t selector, uint8_t ist, uint8_t type_attr)
Sets a gate (entry) for the idt.
Definition idt.c:188
Initializes and holds the IDT for the kernel.
void irq_handler(struct interrupt_frame *frame)
Calls the correct handler and returns the interrupt.
Definition irq.c:25
IRQ Handler and utilities.
Tools for printing to the console and serial output.
void kprintf(const char *fmt,...) __attribute__((format(printf
Wrapper for a standard print.
Logging utilities for the kernel.
The frame holding the registers when an interrupt happens.
Definition idt.h:16
Kernel wide variables.
uint64_t get_hddm_offset(void)
Gets the offset for the HDDM.
Definition vars.c:18
Virtual memory manager.
bool vmm_page_fault_handler(uint64_t fault_address, uint64_t error_code)
Handles a page fault in the given address space (demand paging).
Definition vmm.c:163