PatchworkOS
Loading...
Searching...
No Matches
idt.c
Go to the documentation of this file.
1#include <kernel/cpu/idt.h>
2
3#include <kernel/cpu/gdt.h>
5#include <kernel/cpu/tss.h>
6
7#include <sys/proc.h>
8
10
11extern void idt_load_descriptor(idt_desc_t* descriptor);
12
13static idt_gate_t idt_gate(void* vectorHandler, idt_attributes_t attr, tss_ist_t ist)
14{
15 idt_gate_t gate;
16 gate.offsetLow = (uint64_t)vectorHandler & 0xFFFF;
18 gate.ist = ist;
19 gate.attributes = attr;
20 gate.offsetMid = ((uint64_t)vectorHandler >> 16) & 0xFFFF;
21 gate.offsetHigh = ((uint64_t)vectorHandler >> 32) & 0xFFFFFFFF;
22 gate.reserved = 0;
23 return gate;
24}
25
26void idt_init(void)
27{
29
30 for (interrupt_t vector = 0; vector < EXCEPTION_AMOUNT; vector++)
31 {
32 if (vector == EXCEPTION_DOUBLE_FAULT)
33 {
34 idt.entries[vector] = idt_gate(vectorTable[vector], attr, TSS_IST_DOUBLE_FAULT);
35 continue;
36 }
37
38 idt.entries[vector] = idt_gate(vectorTable[vector], attr, TSS_IST_EXCEPTION);
39 }
40
41 for (interrupt_t vector = EXCEPTION_AMOUNT; vector < INTERRUPT_AMOUNT; vector++)
42 {
43 idt.entries[vector] = idt_gate(vectorTable[vector], attr, TSS_IST_INTERRUPT);
44 }
45}
46
47void idt_cpu_load(void)
48{
49 idt_desc_t idtDesc;
50 idtDesc.size = (sizeof(idt_t)) - 1;
51 idtDesc.offset = (uint64_t)&idt;
52 idt_load_descriptor(&idtDesc);
53}
@ GDT_KERNEL_CODE
Kernel code segment selector.
Definition gdt.h:39
void idt_init(void)
Initialize the IDT structure in memory.
Definition idt.c:26
idt_attributes_t
IDT gate attributes.
Definition idt.h:23
void idt_cpu_load(void)
Load the IDT on the current CPU.
Definition idt.c:47
@ IDT_ATTR_PRESENT
Must be set for the entry to be valid.
Definition idt.h:32
@ IDT_ATTR_RING0
Can be invoked from ring 0 or hardware only.
Definition idt.h:27
@ IDT_ATTR_INTERRUPT
Interrupt gate, will disable interrupts when invoked.
Definition idt.h:24
void * vectorTable[INTERRUPT_AMOUNT]
Pointers to functions to handle each vector.
interrupt_t
CPU vector identifiers.
Definition interrupt.h:97
@ EXCEPTION_AMOUNT
Definition interrupt.h:130
@ EXCEPTION_DOUBLE_FAULT
Definition interrupt.h:106
@ INTERRUPT_AMOUNT
Definition interrupt.h:139
tss_ist_t
Interrupt Stack Table indices.
Definition tss.h:25
#define TSS_IST_INTERRUPT
The IST index to use for other interrupts.
Definition tss.h:50
#define TSS_IST_EXCEPTION
The IST index to use for exceptions.
Definition tss.h:40
#define TSS_IST_DOUBLE_FAULT
The IST index to use for double faults.
Definition tss.h:45
#define ALIGNED(alignment)
GCC aligned attribute.
Definition defs.h:22
#define PAGE_SIZE
Memory page size.
Definition proc.h:140
static idt_gate_t idt_gate(void *vectorHandler, idt_attributes_t attr, tss_ist_t ist)
Definition idt.c:13
void idt_load_descriptor(idt_desc_t *descriptor)
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
IDT descriptor structure.
Definition idt.h:42
uint16_t size
Size of the IDT in bytes - 1.
Definition idt.h:43
uint64_t offset
Address of the IDT.
Definition idt.h:44
IDT gate structure.
Definition idt.h:54
uint8_t attributes
Type and attributes, see idt_attributes_t.
Definition idt.h:58
uint32_t reserved
Definition idt.h:61
uint8_t ist
Interrupt Stack Table offset, 0 = dont use IST, see tss_t.
Definition idt.h:57
uint16_t offsetLow
Lower 16 bits of handler function address.
Definition idt.h:55
uint32_t offsetHigh
Upper 32 bits of handler function address.
Definition idt.h:60
uint16_t codeSegment
Code segment selector in the GDT.
Definition idt.h:56
uint16_t offsetMid
Middle 16 bits of handler function address.
Definition idt.h:59
IDT structure.
Definition idt.h:71