MOS Source Code
Loading...
Searching...
No Matches
idt.c
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2
4
8
9static idt_entry_t idt[IDT_ENTRY_COUNT] __aligned(16) = { 0 };
10static idtr_t idtr;
11
12#define IDT_FLAG_P (1 << 7)
13
14#define STS_IG32 0xE // 32-bit Interrupt Gate
15#define STS_TG32 0xF // 32-bit Trap Gate
16
17__attribute__((sysv_abi)) void idt_flush(idtr_t *idtr);
18
19static void idt_set_descriptor(u8 vector, void *isr, bool usermode, bool is_trap)
20{
21 idt_entry_t *descriptor = &idt[vector];
22
23 descriptor->isr_veryhigh = (uintn) isr >> 32;
24 descriptor->reserved2 = 0;
25
26 descriptor->isr_low = (uintn) isr & 0xFFFF;
27 descriptor->isr_high = (uintn) isr >> 16;
28 descriptor->segment = GDT_SEGMENT_KCODE;
29 descriptor->present = true;
30 descriptor->dpl = usermode ? 3 : 0;
31 descriptor->type = is_trap ? STS_TG32 : STS_IG32;
32 descriptor->zero = 0;
33 descriptor->reserved = 0;
34}
35
37{
38 idt_flush(&idtr);
39}
40
42{
43 for (u8 isr = 0; isr < ISR_MAX_COUNT; isr++)
44 idt_set_descriptor(isr, isr_stub_table[isr], false, false);
45
46 for (u8 irq_n = 0; irq_n < IRQ_MAX_COUNT; irq_n++)
47 idt_set_descriptor(irq_n + IRQ_BASE, irq_stub_table[irq_n], false, false);
48
49 // system calls
50 idt_set_descriptor(MOS_SYSCALL_INTR, isr_stub_table[MOS_SYSCALL_INTR], true, true);
51
52 for (u8 ipi_n = 0; ipi_n < IPI_TYPE_MAX; ipi_n++)
53 idt_set_descriptor(ipi_n + IPI_BASE, isr_stub_table[ipi_n + IPI_BASE], false, false);
54
55 idtr.base = &idt[0];
56 idtr.limit = (u16) sizeof(idt_entry_t) * IDT_ENTRY_COUNT - 1;
57}
#define GDT_SEGMENT_KCODE
Definition descriptors.h:17
#define STS_IG32
Definition idt.c:14
__attribute__((sysv_abi))
Definition idt.c:17
#define STS_TG32
Definition idt.c:15
void x86_init_percpu_idt()
Definition idt.c:36
void x86_idt_init()
Definition idt.c:41
static idtr_t idtr
Definition idt.c:10
@ IPI_TYPE_MAX
Definition ipi.h:16
#define __aligned(x)
Definition mos_global.h:27
Definition descriptors.h:87
u32 zero
Definition descriptors.h:92
u16 segment
Definition descriptors.h:89
u32 isr_veryhigh
Definition descriptors.h:96
u32 reserved2
Definition descriptors.h:97
u16 isr_low
Definition descriptors.h:88
u32 dpl
Definition descriptors.h:93
u32 isr_high
Definition descriptors.h:95
u32 reserved
Definition descriptors.h:90
u32 present
Definition descriptors.h:94
u32 type
Definition descriptors.h:91
u16 limit
idt_entry_t * base
unsigned long uintn
Definition types.h:30
unsigned short u16
Definition types.h:20
unsigned char u8
Definition types.h:19
#define IPI_BASE
void * isr_stub_table[]
void * irq_stub_table[]
#define ISR_MAX_COUNT
#define IDT_ENTRY_COUNT
#define IRQ_BASE
#define IRQ_MAX_COUNT
#define MOS_SYSCALL_INTR