2017-11-02 12:04:40 -04:00
|
|
|
#include "protected.h"
|
2017-11-01 02:07:03 -04:00
|
|
|
|
2017-11-02 12:21:33 -04:00
|
|
|
#include "config.h"
|
2017-11-01 02:07:03 -04:00
|
|
|
#include "logger.h"
|
|
|
|
|
2017-11-02 12:21:33 -04:00
|
|
|
struct GdtPointer {
|
|
|
|
unsigned short limit;
|
|
|
|
unsigned int base;
|
|
|
|
}
|
|
|
|
__attribute__((packed));
|
|
|
|
|
2017-11-01 06:47:43 -04:00
|
|
|
struct IdtPointer {
|
|
|
|
unsigned short limit;
|
|
|
|
unsigned int base;
|
|
|
|
}
|
|
|
|
__attribute__((packed));
|
|
|
|
|
2017-11-02 12:21:33 -04:00
|
|
|
struct GdtEntry {
|
|
|
|
unsigned short limit_low;
|
|
|
|
unsigned short base_low;
|
|
|
|
unsigned char base_middle;
|
|
|
|
unsigned char access;
|
|
|
|
unsigned char granularity;
|
|
|
|
unsigned char base_high;
|
|
|
|
}
|
|
|
|
__attribute__((packed));
|
|
|
|
|
2017-11-01 06:47:43 -04:00
|
|
|
struct IdtEntry {
|
|
|
|
unsigned short base_lo;
|
|
|
|
unsigned short sel;
|
|
|
|
unsigned char always0;
|
|
|
|
unsigned char flags;
|
|
|
|
unsigned short base_hi;
|
|
|
|
}
|
|
|
|
__attribute__((packed));
|
|
|
|
|
2017-11-02 12:21:33 -04:00
|
|
|
static struct GdtPointer gdt_pointer;
|
2017-11-01 02:07:03 -04:00
|
|
|
static struct IdtPointer idt_pointer;
|
|
|
|
|
2017-11-02 12:21:33 -04:00
|
|
|
static struct GdtEntry gdt_entries[GDT_SIZE];
|
|
|
|
static struct IdtEntry idt_entries[IDT_SIZE];
|
|
|
|
|
2017-11-02 12:36:28 -04:00
|
|
|
static void gdt_initialize();
|
|
|
|
static void idt_initialize();
|
|
|
|
|
2017-11-02 12:21:33 -04:00
|
|
|
static void gdt_set_gate(int num, unsigned int base, unsigned int limit, unsigned char access, unsigned char gran);
|
2017-11-01 02:07:03 -04:00
|
|
|
|
2017-11-01 06:08:09 -04:00
|
|
|
static void idt_set_gate(unsigned char num, unsigned int base, unsigned short sel, unsigned char flags);
|
2017-11-01 02:07:03 -04:00
|
|
|
|
2017-11-02 21:50:53 -04:00
|
|
|
void gdt_flush(const struct GdtPointer *pointer);
|
|
|
|
void idt_flush(const struct IdtPointer *pointer);
|
2017-11-01 02:07:03 -04:00
|
|
|
|
|
|
|
void isr0();
|
|
|
|
void isr1();
|
|
|
|
void isr2();
|
|
|
|
void isr3();
|
|
|
|
void isr4();
|
|
|
|
void isr5();
|
|
|
|
void isr6();
|
|
|
|
void isr7();
|
|
|
|
void isr8();
|
|
|
|
void isr9();
|
|
|
|
void isr10();
|
|
|
|
void isr11();
|
|
|
|
void isr12();
|
|
|
|
void isr13();
|
|
|
|
void isr14();
|
|
|
|
void isr15();
|
|
|
|
void isr16();
|
|
|
|
void isr17();
|
|
|
|
void isr18();
|
|
|
|
void isr19();
|
|
|
|
void isr20();
|
|
|
|
void isr21();
|
|
|
|
void isr22();
|
|
|
|
void isr23();
|
|
|
|
void isr24();
|
|
|
|
void isr25();
|
|
|
|
void isr26();
|
|
|
|
void isr27();
|
|
|
|
void isr28();
|
|
|
|
void isr29();
|
|
|
|
void isr30();
|
|
|
|
void isr31();
|
|
|
|
|
2017-11-02 12:36:28 -04:00
|
|
|
void protected_initialize()
|
|
|
|
{
|
|
|
|
gdt_initialize();
|
|
|
|
idt_initialize();
|
|
|
|
}
|
|
|
|
|
2017-11-02 12:21:33 -04:00
|
|
|
void gdt_initialize()
|
|
|
|
{
|
|
|
|
logger_info("Setup GDT.");
|
|
|
|
|
|
|
|
gdt_set_gate(GDT_NULL_INDEX, 0, 0x00000000, 0, 0);
|
|
|
|
gdt_set_gate(GDT_KERNEL_CS_INDEX, 0, 0xFFFFFFFF, 0x9A, 0xCF);
|
|
|
|
gdt_set_gate(GDT_KERNEL_DS_INDEX, 0, 0xFFFFFFFF, 0x92, 0xCF);
|
|
|
|
gdt_set_gate(GDT_USER_CS_INDEX, 0, 0xFFFFFFFF, 0xFA, 0xCF);
|
|
|
|
gdt_set_gate(GDT_USER_DS_INDEX, 0, 0xFFFFFFFF, 0xF2, 0xCF);
|
|
|
|
|
|
|
|
logger_info("Load GDT.");
|
|
|
|
|
|
|
|
gdt_pointer.limit = sizeof(struct GdtEntry) * GDT_SIZE - 1;
|
|
|
|
gdt_pointer.base = (unsigned int)&gdt_entries;
|
|
|
|
|
2017-11-02 21:50:53 -04:00
|
|
|
gdt_flush(&gdt_pointer);
|
2017-11-02 12:21:33 -04:00
|
|
|
}
|
|
|
|
|
2017-11-01 02:07:03 -04:00
|
|
|
void idt_initialize()
|
|
|
|
{
|
|
|
|
logger_info("Setup IDT.");
|
|
|
|
|
2017-11-02 13:16:10 -04:00
|
|
|
for (unsigned char *p = (unsigned char*)idt_entries; p < (unsigned char*)&idt_entries[IDT_SIZE]; ++p) {
|
2017-11-01 02:07:03 -04:00
|
|
|
*p = 0;
|
|
|
|
}
|
|
|
|
|
2017-11-01 06:08:09 -04:00
|
|
|
idt_set_gate(0, (unsigned int)isr0, 0x08, 0x8E);
|
|
|
|
idt_set_gate(1, (unsigned int)isr1, 0x08, 0x8E);
|
|
|
|
idt_set_gate(2, (unsigned int)isr2, 0x08, 0x8E);
|
|
|
|
idt_set_gate(3, (unsigned int)isr3, 0x08, 0x8E);
|
|
|
|
idt_set_gate(4, (unsigned int)isr4, 0x08, 0x8E);
|
|
|
|
idt_set_gate(5, (unsigned int)isr5, 0x08, 0x8E);
|
|
|
|
idt_set_gate(6, (unsigned int)isr6, 0x08, 0x8E);
|
|
|
|
idt_set_gate(7, (unsigned int)isr7, 0x08, 0x8E);
|
|
|
|
idt_set_gate(8, (unsigned int)isr8, 0x08, 0x8E);
|
|
|
|
idt_set_gate(9, (unsigned int)isr9, 0x08, 0x8E);
|
|
|
|
idt_set_gate(10, (unsigned int)isr10, 0x08, 0x8E);
|
|
|
|
idt_set_gate(11, (unsigned int)isr11, 0x08, 0x8E);
|
|
|
|
idt_set_gate(12, (unsigned int)isr12, 0x08, 0x8E);
|
|
|
|
idt_set_gate(13, (unsigned int)isr13, 0x08, 0x8E);
|
|
|
|
idt_set_gate(14, (unsigned int)isr14, 0x08, 0x8E);
|
|
|
|
idt_set_gate(15, (unsigned int)isr15, 0x08, 0x8E);
|
|
|
|
idt_set_gate(16, (unsigned int)isr16, 0x08, 0x8E);
|
|
|
|
idt_set_gate(17, (unsigned int)isr17, 0x08, 0x8E);
|
|
|
|
idt_set_gate(18, (unsigned int)isr18, 0x08, 0x8E);
|
|
|
|
idt_set_gate(19, (unsigned int)isr19, 0x08, 0x8E);
|
|
|
|
idt_set_gate(20, (unsigned int)isr20, 0x08, 0x8E);
|
|
|
|
idt_set_gate(21, (unsigned int)isr21, 0x08, 0x8E);
|
|
|
|
idt_set_gate(22, (unsigned int)isr22, 0x08, 0x8E);
|
|
|
|
idt_set_gate(23, (unsigned int)isr23, 0x08, 0x8E);
|
|
|
|
idt_set_gate(24, (unsigned int)isr24, 0x08, 0x8E);
|
|
|
|
idt_set_gate(25, (unsigned int)isr25, 0x08, 0x8E);
|
|
|
|
idt_set_gate(26, (unsigned int)isr26, 0x08, 0x8E);
|
|
|
|
idt_set_gate(27, (unsigned int)isr27, 0x08, 0x8E);
|
|
|
|
idt_set_gate(28, (unsigned int)isr28, 0x08, 0x8E);
|
|
|
|
idt_set_gate(29, (unsigned int)isr29, 0x08, 0x8E);
|
|
|
|
idt_set_gate(30, (unsigned int)isr30, 0x08, 0x8E);
|
|
|
|
idt_set_gate(31, (unsigned int)isr31, 0x08, 0x8E);
|
2017-11-01 02:07:03 -04:00
|
|
|
|
|
|
|
logger_info("Load IDT.");
|
|
|
|
|
2017-11-02 13:16:10 -04:00
|
|
|
idt_pointer.limit = sizeof(struct IdtEntry) * IDT_SIZE - 1;
|
2017-11-01 06:08:09 -04:00
|
|
|
idt_pointer.base = (unsigned int)&idt_entries;
|
2017-11-01 02:07:03 -04:00
|
|
|
|
2017-11-02 21:50:53 -04:00
|
|
|
idt_flush(&idt_pointer);
|
2017-11-01 02:07:03 -04:00
|
|
|
}
|
|
|
|
|
2017-11-02 12:21:33 -04:00
|
|
|
void gdt_set_gate(int num, unsigned int base, unsigned int limit, unsigned char access, unsigned char gran)
|
|
|
|
{
|
|
|
|
gdt_entries[num].base_low = (base & 0xFFFF);
|
|
|
|
gdt_entries[num].base_middle = (base >> 16) & 0xFF;
|
|
|
|
gdt_entries[num].base_high = (base >> 24) & 0xFF;
|
|
|
|
|
|
|
|
gdt_entries[num].limit_low = (limit & 0xFFFF);
|
|
|
|
gdt_entries[num].granularity = (limit >> 16) & 0x0F;
|
|
|
|
|
|
|
|
gdt_entries[num].granularity |= gran & 0xF0;
|
|
|
|
|
|
|
|
gdt_entries[num].access = access;
|
|
|
|
}
|
|
|
|
|
2017-11-01 06:08:09 -04:00
|
|
|
void idt_set_gate(unsigned char num, unsigned int base, unsigned short sel, unsigned char flags)
|
2017-11-01 02:07:03 -04:00
|
|
|
{
|
|
|
|
idt_entries[num].base_lo = base & 0xFFFF;
|
|
|
|
idt_entries[num].base_hi = (base >> 16) & 0xFFFF;
|
|
|
|
idt_entries[num].sel = sel;
|
|
|
|
idt_entries[num].always0 = 0;
|
|
|
|
// We must uncomment the OR below when we get to using user-mode.
|
|
|
|
// It sets the interrupt gate's privilege level to 3.
|
|
|
|
idt_entries[num].flags = flags /* | 0x60 */;
|
|
|
|
}
|