PatchworkOS  966e257
A non-POSIX operating system.
Loading...
Searching...
No Matches
idt.h
Go to the documentation of this file.
1#pragma once
2
3#include <kernel/defs.h>
4
5#include <stdint.h>
6
7/**
8 * @brief Interrupt Descriptor Table
9 * @defgroup kernel_cpu_idt IDT
10 * @ingroup kernel_cpu
11 *
12 * The Interrupt Descriptor Table tells a CPU what to do when it receives an interrupt or exception.
13 *
14 * @{
15 */
16
17/**
18 * @brief Number of IDT gates.
19 */
20#define IDT_GATE_AMOUNT (UINT8_MAX + 1)
21
22/**
23 * @brief IDT gate attributes.
24 * @enum idt_attributes_t
25 */
26typedef enum
27{
28 IDT_ATTR_INTERRUPT = 0b1110, ///< Interrupt gate, will disable interrupts when invoked.
29 IDT_ATTR_TRAP = 0b1111, ///< Trap gate, will NOT disable interrupts when invoked.
30
31 IDT_ATTR_RING0 = 0b00, ///< Can be invoked from ring 0 or hardware only.
32 IDT_ATTR_RING1 = 0b01, ///< Can be invoked from ring 1 or lower.
33 IDT_ATTR_RING2 = 0b10, ///< Can be invoked from ring 2 or lower.
34 IDT_ATTR_RING3 = 0b11, ///< Can be invoked from ring 3 or lower.
35
36 IDT_ATTR_PRESENT = 1 << 7 ///< Must be set for the entry to be valid.
38
39/**
40 * @brief IDT descriptor structure.
41 * @struct idt_desc_t
42 *
43 * Used to load the IDT with the `lidt` instruction.
44 */
45typedef struct PACKED
46{
47 uint16_t size; ///< Size of the IDT in bytes - 1.
48 uint64_t offset; ///< Address of the IDT.
50
51/**
52 * @brief IDT gate structure.
53 * @struct idt_gate_t
54 *
55 * Represents a single entry in the IDT.
56 */
57typedef struct PACKED
58{
59 uint16_t offsetLow; ///< Lower 16 bits of handler function address.
60 uint16_t codeSegment; ///< Code segment selector in the GDT.
61 uint8_t ist; ///< Interrupt Stack Table offset, 0 = dont use IST, see `tss_t`.
62 uint8_t attributes; ///< Type and attributes, see `idt_attributes_t`.
63 uint16_t offsetMid; ///< Middle 16 bits of handler function address.
64 uint32_t offsetHigh; ///< Upper 32 bits of handler function address.
67
68/**
69 * @brief IDT structure.
70 * @struct idt_t
71 *
72 * Represents the entire IDT.
73 */
78
79/**
80 * @brief Initialize the IDT structure in memory.
81 *
82 * This will setup the IDT structure in memory, but will not load it. Loading is done in `idt_cpu_load()`.
83 *
84 * The IDT is setup according to the values in `irq_virt_t`.
85 */
86void idt_init(void);
87
88/**
89 * @brief Load the IDT on the current CPU.
90 *
91 * This will load the IDT using the `lidt` instruction.
92 *
93 * Must be called after `idt_init()`.
94 */
95void idt_cpu_load(void);
96
97/** @} */
void idt_init(void)
Initialize the IDT structure in memory.
Definition idt.c:27
#define IDT_GATE_AMOUNT
Number of IDT gates.
Definition idt.h:20
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_RING3
Can be invoked from ring 3 or lower.
Definition idt.h:34
@ IDT_ATTR_RING1
Can be invoked from ring 1 or lower.
Definition idt.h:32
@ 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
@ IDT_ATTR_TRAP
Trap gate, will NOT disable interrupts when invoked.
Definition idt.h:29
@ IDT_ATTR_RING2
Can be invoked from ring 2 or lower.
Definition idt.h:33
#define PACKED
GCC packed attribute.
Definition defs.h:32
static start_entry_t entries[START_ENTRY_MAX]
Definition start_menu.c:21
__UINT32_TYPE__ uint32_t
Definition stdint.h:15
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINT8_TYPE__ uint8_t
Definition stdint.h:11
__UINT16_TYPE__ uint16_t
Definition stdint.h:13
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