PatchworkOS  966e257
A non-POSIX operating system.
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/irq.h>
6#include <kernel/cpu/tss.h>
7
8#include <sys/proc.h>
9
11
12extern void idt_load_descriptor(idt_desc_t* descriptor);
13
14static idt_gate_t idt_gate(void* vectorHandler, idt_attributes_t attr, tss_ist_t ist)
15{
16 idt_gate_t gate;
17 gate.offsetLow = (uint64_t)vectorHandler & 0xFFFF;
19 gate.ist = ist;
20 gate.attributes = attr;
21 gate.offsetMid = ((uint64_t)vectorHandler >> 16) & 0xFFFF;
22 gate.offsetHigh = ((uint64_t)vectorHandler >> 32) & 0xFFFFFFFF;
23 gate.reserved = 0;
24 return gate;
25}
26
27void idt_init(void)
28{
30
31 for (interrupt_vector_t vector = 0; vector < VECTOR_EXCEPTION_END; vector++)
32 {
33 if (vector == VECTOR_DOUBLE_FAULT)
34 {
35 idt.entries[vector] = idt_gate(vectorTable[vector], attr, TSS_IST_DOUBLE_FAULT);
36 continue;
37 }
38 if (vector == VECTOR_NMI)
39 {
40 idt.entries[vector] = idt_gate(vectorTable[vector], attr, TSS_IST_NMI);
41 continue;
42 }
43
44 idt.entries[vector] = idt_gate(vectorTable[vector], attr, TSS_IST_EXCEPTION);
45 }
46
47 for (interrupt_vector_t vector = VECTOR_EXCEPTION_END; vector < VECTOR_TOTAL_AMOUNT; vector++)
48 {
49 idt.entries[vector] = idt_gate(vectorTable[vector], attr, TSS_IST_INTERRUPT);
50 }
51}
52
53void idt_cpu_load(void)
54{
55 idt_desc_t idtDesc;
56 idtDesc.size = (sizeof(idt_t)) - 1;
57 idtDesc.offset = (uint64_t)&idt;
58 idt_load_descriptor(&idtDesc);
59}
@ GDT_KERNEL_CODE
Kernel code segment selector.
Definition gdt.h:39
void idt_init(void)
Initialize the IDT structure in memory.
Definition idt.c:27
idt_attributes_t
IDT gate attributes.
Definition idt.h:27
void idt_cpu_load(void)
Load the IDT on the current CPU.
Definition idt.c:53
@ IDT_ATTR_PRESENT
Must be set for the entry to be valid.
Definition idt.h:36
@ IDT_ATTR_RING0
Can be invoked from ring 0 or hardware only.
Definition idt.h:31
@ IDT_ATTR_INTERRUPT
Interrupt gate, will disable interrupts when invoked.
Definition idt.h:28
void * vectorTable[IDT_GATE_AMOUNT]
Pointers to functions to handle each vector.
interrupt_vector_t
Interrupt Vectors.
Definition interrupt.h:94
@ VECTOR_DOUBLE_FAULT
Definition interrupt.h:104
@ VECTOR_NMI
Definition interrupt.h:98
@ VECTOR_TOTAL_AMOUNT
Definition interrupt.h:135
@ VECTOR_EXCEPTION_END
Exclusive end of exceptions.
Definition interrupt.h:121
#define TSS_IST_NMI
The IST index to use for non-maskable interrupts.
Definition tss.h:50
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:55
#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
The size of a memory page in bytes.
Definition proc.h:106
static idt_gate_t idt_gate(void *vectorHandler, idt_attributes_t attr, tss_ist_t ist)
Definition idt.c:14
void idt_load_descriptor(idt_desc_t *descriptor)
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
IDT descriptor structure.
Definition idt.h:46
uint16_t size
Size of the IDT in bytes - 1.
Definition idt.h:47
uint64_t offset
Address of the IDT.
Definition idt.h:48
IDT gate structure.
Definition idt.h:58
uint8_t attributes
Type and attributes, see idt_attributes_t.
Definition idt.h:62
uint32_t reserved
Definition idt.h:65
uint8_t ist
Interrupt Stack Table offset, 0 = dont use IST, see tss_t.
Definition idt.h:61
uint16_t offsetLow
Lower 16 bits of handler function address.
Definition idt.h:59
uint32_t offsetHigh
Upper 32 bits of handler function address.
Definition idt.h:64
uint16_t codeSegment
Code segment selector in the GDT.
Definition idt.h:60
uint16_t offsetMid
Middle 16 bits of handler function address.
Definition idt.h:63
IDT structure.
Definition idt.h:75