x86: add GDT and IDT
Signed-off-by: Michael Shaw <spencershawiscool@gmail.com>
This commit is contained in:
parent
f10e3f31d7
commit
e5b55a6ba5
25 changed files with 1211 additions and 13 deletions
|
|
@ -1,2 +1,6 @@
|
|||
# SPDX-License-Identifier: GPL-2.0-only
|
||||
obj-y += amd64_init.o
|
||||
obj-y += gdt.o
|
||||
obj-y += idt.o
|
||||
obj-y += trap.o
|
||||
obj-y += exception.o
|
||||
|
|
|
|||
|
|
@ -1,4 +1,6 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include "descriptor.h"
|
||||
|
||||
void yield()
|
||||
{
|
||||
for (;;) {
|
||||
|
|
@ -10,3 +12,9 @@ void _panic()
|
|||
{
|
||||
yield();
|
||||
}
|
||||
|
||||
void arch_init()
|
||||
{
|
||||
gdt_init();
|
||||
idt_init();
|
||||
}
|
||||
|
|
|
|||
91
amd64/init/descriptor.h
Normal file
91
amd64/init/descriptor.h
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _DESCRIPTOR_H_
|
||||
#define _DESCRIPTOR_H_
|
||||
|
||||
#define GDT_ACCESS_DATA_RING0 0b10010010
|
||||
#define GDT_ACCESS_CODE_RING0 0b10011010
|
||||
|
||||
#define GDT_ACCESS_DATA_RING3 0b11110010
|
||||
#define GDT_ACCESS_CODE_RING3 0b11111010
|
||||
|
||||
#define GDT_SEGMENT_CODE_RING0 0x28
|
||||
#define GDT_SEGMENT_DATA_RING0 0x30
|
||||
#define GDT_SEGMENT_CODE_RING3 0x38
|
||||
#define GDT_SEGMENT_DATA_RING3 0x40
|
||||
|
||||
#define GATE_INTR 0x8E
|
||||
#define GATE_TRAP 0x8F
|
||||
|
||||
#define GATE_INTR_DPL3 0xEE
|
||||
|
||||
|
||||
#include <kernel/common.h>
|
||||
|
||||
struct gdt_desc {
|
||||
uint16_t limit;
|
||||
uint16_t base;
|
||||
uint8_t base_2;
|
||||
uint8_t access;
|
||||
uint8_t flags;
|
||||
uint8_t base_3;
|
||||
} PACKED;
|
||||
|
||||
struct gdt_desc_sys {
|
||||
uint16_t limit;
|
||||
uint16_t base;
|
||||
uint8_t base_2;
|
||||
uint8_t access;
|
||||
uint8_t flags;
|
||||
uint8_t base_3;
|
||||
uint32_t base_4;
|
||||
uint32_t reserved;
|
||||
} PACKED;
|
||||
|
||||
struct tss {
|
||||
uint32_t reserved;
|
||||
uint64_t rsp0;
|
||||
uint64_t rsp1;
|
||||
uint64_t rsp2;
|
||||
uint64_t reserved2;
|
||||
uint64_t ist1;
|
||||
uint64_t ist2;
|
||||
uint64_t ist3;
|
||||
uint64_t ist4;
|
||||
uint64_t ist5;
|
||||
uint64_t ist6;
|
||||
uint64_t ist7;
|
||||
uint64_t reserved3;
|
||||
uint16_t reserved4;
|
||||
uint16_t iomap_base;
|
||||
} PACKED;
|
||||
|
||||
struct gdtr {
|
||||
uint16_t size;
|
||||
uint64_t offset;
|
||||
} PACKED;
|
||||
|
||||
struct irq_desc {
|
||||
uint16_t offset_low;
|
||||
uint16_t segment;
|
||||
uint8_t ist;
|
||||
uint8_t attrs;
|
||||
uint16_t offset_mid;
|
||||
uint32_t offset_high;
|
||||
uint32_t reserved;
|
||||
} PACKED;
|
||||
|
||||
struct idtr {
|
||||
uint16_t size;
|
||||
uint64_t offset;
|
||||
} PACKED;
|
||||
|
||||
extern struct gdtr __gdtr;
|
||||
extern struct gdt_desc __gdt[64];
|
||||
|
||||
extern struct idtr __idtr;
|
||||
extern struct irq_desc __idt[64];
|
||||
|
||||
void gdt_init();
|
||||
void idt_init();
|
||||
|
||||
#endif /* _DESCRIPTOR_H_ */
|
||||
42
amd64/init/exception.c
Normal file
42
amd64/init/exception.c
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/common.h>
|
||||
#include <lib/stdio.h>
|
||||
|
||||
static const char *exception_names[] = {
|
||||
"Divide-by-zero Error",
|
||||
"Debug",
|
||||
"Non-maskable Interrupt",
|
||||
"Breakpoint",
|
||||
"Overflow",
|
||||
"Bound Range Exceeded",
|
||||
"Invalid Opcode",
|
||||
"Device Not Available",
|
||||
"Double Fault",
|
||||
"Coprocessor Segment Overrun",
|
||||
"Invalid TSS",
|
||||
"Segment Not Present",
|
||||
"Stack-Segment Fault",
|
||||
"General Protection Fault",
|
||||
"Page Fault",
|
||||
"Reserved",
|
||||
"x87 Floating-Point Exception",
|
||||
"Alignment Check",
|
||||
"Machine Check",
|
||||
"SIMD Floating-Point Exception",
|
||||
"Virtualization Exception",
|
||||
"Invalid exception",
|
||||
"Invalid exception",
|
||||
"Invalid exception",
|
||||
"Invalid exception",
|
||||
"Invalid exception",
|
||||
"Invalid exception",
|
||||
"Invalid exception",
|
||||
"Security Exception",
|
||||
"Reserved",
|
||||
"Triple Fault",
|
||||
};
|
||||
|
||||
void exception_handler(u64 no, u64 err)
|
||||
{
|
||||
panic("(Exception) %s(%02xh)\n", exception_names[no], no);
|
||||
}
|
||||
40
amd64/init/gdt.c
Normal file
40
amd64/init/gdt.c
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/common.h>
|
||||
|
||||
#include "descriptor.h"
|
||||
#include "../arch/reg.h"
|
||||
|
||||
static size_t gdt_index = 0;
|
||||
|
||||
void gdt_insert(uint8_t offset, uint8_t access, uint8_t flags)
|
||||
{
|
||||
struct gdt_desc *desc = (__gdt + offset);
|
||||
desc->limit = 0;
|
||||
desc->base = 0;
|
||||
desc->base_2 = 0;
|
||||
desc->base_3 = 0;
|
||||
|
||||
desc->access = access;
|
||||
desc->flags = flags;
|
||||
gdt_index++;
|
||||
}
|
||||
|
||||
void gdt_init()
|
||||
{
|
||||
gdt_insert(0, 0, 0);
|
||||
gdt_insert(1, GDT_ACCESS_CODE_RING0, 0xA0);
|
||||
gdt_insert(2, GDT_ACCESS_DATA_RING0, 0xC0);
|
||||
gdt_insert(3, GDT_ACCESS_CODE_RING0, 0xA0);
|
||||
gdt_insert(4, GDT_ACCESS_DATA_RING0, 0xC0);
|
||||
gdt_insert(5, GDT_ACCESS_CODE_RING0, 0xA0);
|
||||
gdt_insert(6, GDT_ACCESS_DATA_RING0, 0xC0);
|
||||
gdt_insert(7, GDT_ACCESS_DATA_RING3, 0xC0);
|
||||
gdt_insert(8, GDT_ACCESS_CODE_RING3, 0xA0);
|
||||
|
||||
__gdtr.size = (sizeof(__gdt) - 1);
|
||||
__gdtr.offset = (uint64_t)__gdt;
|
||||
|
||||
lgdt();
|
||||
|
||||
krdy("GDT initialized\n");
|
||||
}
|
||||
172
amd64/init/idt.c
Normal file
172
amd64/init/idt.c
Normal file
|
|
@ -0,0 +1,172 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/common.h>
|
||||
#include "descriptor.h"
|
||||
#include "../arch/reg.h"
|
||||
|
||||
#define exception(_i) _exception_##_i
|
||||
#define exception_decl(_j) void exception(_j)()
|
||||
|
||||
#define irq_decl(_i) void _irq_##_i()
|
||||
#define __irq(_i) _irq_##_i
|
||||
|
||||
static bool irq_mapped[16] = { false };
|
||||
static void *irq_handlers[16] = { NULL };
|
||||
|
||||
exception_decl(0x00);
|
||||
exception_decl(0x01);
|
||||
exception_decl(0x02);
|
||||
exception_decl(0x03);
|
||||
exception_decl(0x04);
|
||||
exception_decl(0x05);
|
||||
exception_decl(0x06);
|
||||
exception_decl(0x07);
|
||||
exception_decl(0x08);
|
||||
exception_decl(0x09);
|
||||
exception_decl(0x0A);
|
||||
exception_decl(0x0B);
|
||||
exception_decl(0x0C);
|
||||
exception_decl(0x0D);
|
||||
exception_decl(0x0E);
|
||||
exception_decl(0x0F);
|
||||
exception_decl(0x10);
|
||||
exception_decl(0x11);
|
||||
exception_decl(0x12);
|
||||
exception_decl(0x13);
|
||||
exception_decl(0x14);
|
||||
exception_decl(0x15);
|
||||
exception_decl(0x16);
|
||||
exception_decl(0x17);
|
||||
exception_decl(0x18);
|
||||
exception_decl(0x19);
|
||||
exception_decl(0x1A);
|
||||
exception_decl(0x1B);
|
||||
exception_decl(0x1C);
|
||||
exception_decl(0x1D);
|
||||
exception_decl(0x1E);
|
||||
exception_decl(0x1F);
|
||||
|
||||
irq_decl(0x00);
|
||||
irq_decl(0x01);
|
||||
irq_decl(0x02);
|
||||
irq_decl(0x03);
|
||||
irq_decl(0x04);
|
||||
irq_decl(0x05);
|
||||
irq_decl(0x06);
|
||||
irq_decl(0x07);
|
||||
irq_decl(0x08);
|
||||
irq_decl(0x09);
|
||||
irq_decl(0x0A);
|
||||
irq_decl(0x0B);
|
||||
irq_decl(0x0C);
|
||||
irq_decl(0x0D);
|
||||
irq_decl(0x0E);
|
||||
irq_decl(0x0F);
|
||||
|
||||
void idt_insert(uint16_t gate, uint8_t type, uint8_t ist, uint16_t segment, void *func)
|
||||
{
|
||||
uintptr_t f = (uintptr_t)(func);
|
||||
struct irq_desc *irq_desc = (__idt + gate);
|
||||
irq_desc->offset_low = f & 0xFFFF;
|
||||
irq_desc->offset_mid = ((f >> 0x10) & 0xFFFF);
|
||||
irq_desc->offset_high = ((f >> 0x20) & 0xFFFFFFFF);
|
||||
|
||||
irq_desc->segment = segment;
|
||||
irq_desc->ist = ist;
|
||||
irq_desc->attrs = type;
|
||||
}
|
||||
|
||||
int irq_map(uint8_t irq, void *func)
|
||||
{
|
||||
if (irq_mapped[irq])
|
||||
return 1;
|
||||
|
||||
irq_mapped[irq] = true;
|
||||
irq_handlers[irq] = func;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void irq_unmap(uint8_t irq)
|
||||
{
|
||||
irq_mapped[irq] = false;
|
||||
irq_handlers[irq] = NULL;
|
||||
idt_insert(irq + 0x20, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, NULL);
|
||||
}
|
||||
|
||||
int irq_highest_free()
|
||||
{
|
||||
for (int i = 14; i >= 0; i--) {
|
||||
if (!irq_mapped[i])
|
||||
return i;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
void irq_handler(int irq)
|
||||
{
|
||||
if (irq_handlers[irq] != NULL) {
|
||||
void (*handler)() = irq_handlers[irq];
|
||||
handler();
|
||||
}
|
||||
}
|
||||
|
||||
void idt_init()
|
||||
{
|
||||
idt_insert(0x00, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x00));
|
||||
idt_insert(0x01, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x01));
|
||||
idt_insert(0x02, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x02));
|
||||
idt_insert(0x03, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x03));
|
||||
idt_insert(0x04, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x04));
|
||||
idt_insert(0x05, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x05));
|
||||
idt_insert(0x06, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x06));
|
||||
idt_insert(0x07, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x07));
|
||||
idt_insert(0x08, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x08));
|
||||
idt_insert(0x09, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x09));
|
||||
idt_insert(0x0A, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x0A));
|
||||
idt_insert(0x0B, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x0B));
|
||||
idt_insert(0x0C, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x0C));
|
||||
idt_insert(0x0D, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x0D));
|
||||
idt_insert(0x0E, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x0E));
|
||||
idt_insert(0x0F, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x0F));
|
||||
idt_insert(0x10, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x10));
|
||||
idt_insert(0x11, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x11));
|
||||
idt_insert(0x12, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x12));
|
||||
idt_insert(0x13, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x13));
|
||||
idt_insert(0x14, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x14));
|
||||
idt_insert(0x15, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x15));
|
||||
idt_insert(0x16, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x16));
|
||||
idt_insert(0x17, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x17));
|
||||
idt_insert(0x18, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x18));
|
||||
idt_insert(0x19, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x19));
|
||||
idt_insert(0x1A, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x1A));
|
||||
idt_insert(0x1B, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x1B));
|
||||
idt_insert(0x1C, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x1C));
|
||||
idt_insert(0x1D, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x1D));
|
||||
idt_insert(0x1E, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x1E));
|
||||
idt_insert(0x1F, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x1F));
|
||||
|
||||
idt_insert(0x20, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x00));
|
||||
idt_insert(0x21, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x01));
|
||||
idt_insert(0x22, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x02));
|
||||
idt_insert(0x23, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x03));
|
||||
idt_insert(0x24, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x04));
|
||||
idt_insert(0x25, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x05));
|
||||
idt_insert(0x26, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x06));
|
||||
idt_insert(0x27, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x07));
|
||||
idt_insert(0x28, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x08));
|
||||
idt_insert(0x29, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x09));
|
||||
idt_insert(0x2A, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x0A));
|
||||
idt_insert(0x2B, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x0B));
|
||||
idt_insert(0x2C, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x0C));
|
||||
idt_insert(0x2D, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x0D));
|
||||
idt_insert(0x2E, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x0E));
|
||||
idt_insert(0x2F, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x0F));
|
||||
|
||||
__idtr.size = sizeof(__idt) - 1;
|
||||
__idtr.offset = (uint64_t)__idt;
|
||||
|
||||
lidt();
|
||||
|
||||
krdy("IDT loaded\n");
|
||||
}
|
||||
68
amd64/init/trap.S
Normal file
68
amd64/init/trap.S
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
.intel_syntax noprefix
|
||||
|
||||
.macro exception nr
|
||||
.global _exception_\nr
|
||||
_exception_\nr:
|
||||
mov rdi, \nr
|
||||
call exception_handler
|
||||
iretq
|
||||
.endm
|
||||
|
||||
.macro irq nr
|
||||
.global _irq_\nr
|
||||
_irq_\nr:
|
||||
mov rdi, \nr
|
||||
call irq_handler
|
||||
iretq
|
||||
.endm
|
||||
|
||||
exception 0x00
|
||||
exception 0x01
|
||||
exception 0x02
|
||||
exception 0x03
|
||||
exception 0x04
|
||||
exception 0x05
|
||||
exception 0x06
|
||||
exception 0x07
|
||||
exception 0x08
|
||||
exception 0x09
|
||||
exception 0x0A
|
||||
exception 0x0B
|
||||
exception 0x0C
|
||||
exception 0x0D
|
||||
exception 0x0E
|
||||
exception 0x0F
|
||||
exception 0x10
|
||||
exception 0x11
|
||||
exception 0x12
|
||||
exception 0x13
|
||||
exception 0x14
|
||||
exception 0x15
|
||||
exception 0x16
|
||||
exception 0x17
|
||||
exception 0x18
|
||||
exception 0x19
|
||||
exception 0x1A
|
||||
exception 0x1B
|
||||
exception 0x1C
|
||||
exception 0x1D
|
||||
exception 0x1E
|
||||
exception 0x1F
|
||||
|
||||
irq 0x00
|
||||
irq 0x01
|
||||
irq 0x02
|
||||
irq 0x03
|
||||
irq 0x04
|
||||
irq 0x05
|
||||
irq 0x06
|
||||
irq 0x07
|
||||
irq 0x08
|
||||
irq 0x09
|
||||
irq 0x0A
|
||||
irq 0x0B
|
||||
irq 0x0C
|
||||
irq 0x0D
|
||||
irq 0x0E
|
||||
irq 0x0F
|
||||
Loading…
Reference in a new issue