Working PIT irqs, fix GDT bugs
This commit is contained in:
@@ -40,67 +40,61 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
#include <sys/isr.h>
|
||||
#include <sys/pic.h>
|
||||
|
||||
#define KERNEL_INTR_STACK_SIZE 8192
|
||||
|
||||
static volatile struct gdt_entry gdt[6];
|
||||
static volatile struct gdt_ptr gdtptr;
|
||||
static volatile struct tss tss;
|
||||
static volatile struct idt_entry idt[0x100];
|
||||
static volatile struct idt_ptr idtptr;
|
||||
static volatile byte_t kernel_intr_stack[KERNEL_INTR_STACK_SIZE];
|
||||
|
||||
static struct { volatile uint32_t eflags; atomic_int nesting; } intr_state =
|
||||
{ .eflags = 0, .nesting = 0 };
|
||||
|
||||
static void gdt_make_entry(int index, uint32_t base, uint32_t limit,
|
||||
uint8_t access, uint8_t gran) {
|
||||
gdt[index].base_low = (base & 0xFFFF);
|
||||
gdt[index].base_mid = (base >> 16) & 0xFF;
|
||||
gdt[index].base_up = (base >> 24) & 0xFF;
|
||||
gdt[index].limit_low = (limit & 0xFFFF);
|
||||
gdt[index].gran = ((limit >> 16) & 0x0F);
|
||||
gdt[index].gran |= (gran & 0xF0);
|
||||
gdt[index].access = access;
|
||||
uint8_t type, uint8_t privl, uint8_t system) {
|
||||
gdt[index].limit_low = (uint16_t)limit;
|
||||
gdt[index].limit_up = (uint16_t)(limit >> 16);
|
||||
gdt[index].base_low = (uint16_t)base;
|
||||
gdt[index].base_mid = (uint8_t)(base >> 16);
|
||||
gdt[index].base_up = (uint8_t)(base >> 24);
|
||||
gdt[index].access = type | (system << 4) | (privl << 5) | (1<<7);
|
||||
gdt[index].limit_up |= (1<<7) | (1<<6);
|
||||
}
|
||||
|
||||
static void gdt_init(void) {
|
||||
gdtptr.limit = ELEM_SIZE(gdt) * LEN(gdt) - 1;
|
||||
gdtptr.limit = sizeof(gdt) - 1;
|
||||
gdtptr.base = (uint32_t)&gdt;
|
||||
gdt_make_entry(0, 0, 0, 0, 0);
|
||||
gdt_make_entry(1, 0, 0xFFFFF, 0x9A, 0xCF);
|
||||
gdt_make_entry(2, 0, 0xFFFFF, 0x92, 0xCF);
|
||||
gdt_make_entry(3, 0, 0xFFFFF, 0xFA, 0xCF);
|
||||
gdt_make_entry(4, 0, 0xFFFFF, 0xF2, 0xCF);
|
||||
gdt_make_entry(0, 0, 0, 0, 0, 0);
|
||||
gdt_make_entry(GDT_KCODE, 0, 0xFFFFF, GDT_CODESEG, GDT_KERNEL_PRIVL, GDT_MEMORY);
|
||||
gdt_make_entry(GDT_KDATA, 0, 0xFFFFF, GDT_DATASEG, GDT_KERNEL_PRIVL, GDT_MEMORY);
|
||||
gdt_make_entry(GDT_UCODE, 0, 0xFFFFF, GDT_CODESEG, GDT_USER_PRIVL, GDT_MEMORY);
|
||||
gdt_make_entry(GDT_UDATA, 0, 0xFFFFF, GDT_DATASEG, GDT_USER_PRIVL, GDT_MEMORY);
|
||||
gdt_make_entry(GDT_TSS, (uint32_t)&tss, TSS_SIZE, GDT_TSS_SEG, GDT_KERNEL_PRIVL, GDT_SYSTEM);
|
||||
|
||||
gdt_flush((ptr_t)&gdtptr);
|
||||
}
|
||||
|
||||
static void tss_init(void) {
|
||||
uint32_t base = (uint32_t)&tss;
|
||||
gdt_make_entry(5, base, base + sizeof(struct tss), 0xE9, 0);
|
||||
|
||||
memset((void *)&tss, 0, sizeof(tss));
|
||||
|
||||
uptr_t sp;
|
||||
__asm__ volatile("mov %%esp, %0" : "=r"(sp));
|
||||
|
||||
tss.ss0 = 0x10;
|
||||
tss.esp0 = (uint32_t)sp;
|
||||
tss.cs = 0x08 | 0x3;
|
||||
tss.ds = 0x10 | 0x3;
|
||||
tss.es = 0x10 | 0x3;
|
||||
tss.fs = 0x10 | 0x3;
|
||||
tss.fs = 0x10 | 0x3;
|
||||
tss.ss = 0x10 | 0x3;
|
||||
tss.iomap = sizeof(tss);
|
||||
tss.ldt_selector = 0;
|
||||
tss.prev_tss = GDT_TSS;
|
||||
tss.iomap_base = sizeof(struct tss);
|
||||
tss.esp_0 = (uint32_t)&kernel_intr_stack[KERNEL_INTR_STACK_SIZE - 1];
|
||||
tss.ss_0 = GDT_KERNEL_DS;
|
||||
|
||||
tss_flush();
|
||||
}
|
||||
|
||||
static void idt_make_entry(int index, uint32_t base, uint32_t sel, uint8_t flags) {
|
||||
static void idt_make_entry(int index, uint32_t base, uint32_t sel, uint8_t type, uint8_t access) {
|
||||
volatile struct idt_entry *ent = &idt[index];
|
||||
ent->base_low = (base & 0xFFFF);
|
||||
ent->base_up = ((base >> 16) & 0xFFFF);
|
||||
ent->base_low = (uint16_t)(base & 0xFFFF);
|
||||
ent->base_up = (uint16_t)((base >> 16) & 0xFFFF);
|
||||
ent->sel = (uint16_t)sel;
|
||||
ent->always0 = 0;
|
||||
ent->sel = sel;
|
||||
ent->flags = flags | 0x60;
|
||||
ent->flags = type | (access << 5) | (1<<7);
|
||||
}
|
||||
|
||||
void idt_init(void) {
|
||||
@@ -108,55 +102,55 @@ void idt_init(void) {
|
||||
idtptr.base = (uint32_t)idt;
|
||||
idtptr.limit = sizeof(idt) - 1;
|
||||
|
||||
idt_make_entry(0, (uint32_t)&except0, 0x08, 0x8E);
|
||||
idt_make_entry(1, (uint32_t)&except1, 0x08, 0x8E);
|
||||
idt_make_entry(2, (uint32_t)&except2, 0x08, 0x8E);
|
||||
idt_make_entry(3, (uint32_t)&except3, 0x08, 0x8E);
|
||||
idt_make_entry(4, (uint32_t)&except4, 0x08, 0x8E);
|
||||
idt_make_entry(5, (uint32_t)&except5, 0x08, 0x8E);
|
||||
idt_make_entry(6, (uint32_t)&except6, 0x08, 0x8E);
|
||||
idt_make_entry(7, (uint32_t)&except7, 0x08, 0x8E);
|
||||
idt_make_entry(8, (uint32_t)&except8, 0x08, 0x8E);
|
||||
idt_make_entry(9, (uint32_t)&except9, 0x08, 0x8E);
|
||||
idt_make_entry(10, (uint32_t)&except10, 0x08, 0x8E);
|
||||
idt_make_entry(11, (uint32_t)&except11, 0x08, 0x8E);
|
||||
idt_make_entry(12, (uint32_t)&except12, 0x08, 0x8E);
|
||||
idt_make_entry(13, (uint32_t)&except13, 0x08, 0x8E);
|
||||
idt_make_entry(14, (uint32_t)&except14, 0x08, 0x8E);
|
||||
idt_make_entry(15, (uint32_t)&except15, 0x08, 0x8E);
|
||||
idt_make_entry(16, (uint32_t)&except16, 0x08, 0x8E);
|
||||
idt_make_entry(17, (uint32_t)&except17, 0x08, 0x8E);
|
||||
idt_make_entry(18, (uint32_t)&except18, 0x08, 0x8E);
|
||||
idt_make_entry(19, (uint32_t)&except19, 0x08, 0x8E);
|
||||
idt_make_entry(20, (uint32_t)&except20, 0x08, 0x8E);
|
||||
idt_make_entry(21, (uint32_t)&except21, 0x08, 0x8E);
|
||||
idt_make_entry(22, (uint32_t)&except22, 0x08, 0x8E);
|
||||
idt_make_entry(23, (uint32_t)&except23, 0x08, 0x8E);
|
||||
idt_make_entry(24, (uint32_t)&except24, 0x08, 0x8E);
|
||||
idt_make_entry(25, (uint32_t)&except25, 0x08, 0x8E);
|
||||
idt_make_entry(26, (uint32_t)&except26, 0x08, 0x8E);
|
||||
idt_make_entry(27, (uint32_t)&except27, 0x08, 0x8E);
|
||||
idt_make_entry(28, (uint32_t)&except28, 0x08, 0x8E);
|
||||
idt_make_entry(29, (uint32_t)&except29, 0x08, 0x8E);
|
||||
idt_make_entry(30, (uint32_t)&except30, 0x08, 0x8E);
|
||||
idt_make_entry(31, (uint32_t)&except31, 0x08, 0x8E);
|
||||
idt_make_entry(32, (uint32_t)&irq0, 0x08, 0x8E);
|
||||
idt_make_entry(33, (uint32_t)&irq1, 0x08, 0x8E);
|
||||
idt_make_entry(34, (uint32_t)&irq2, 0x08, 0x8E);
|
||||
idt_make_entry(35, (uint32_t)&irq3, 0x08, 0x8E);
|
||||
idt_make_entry(36, (uint32_t)&irq4, 0x08, 0x8E);
|
||||
idt_make_entry(37, (uint32_t)&irq5, 0x08, 0x8E);
|
||||
idt_make_entry(38, (uint32_t)&irq6, 0x08, 0x8E);
|
||||
idt_make_entry(39, (uint32_t)&irq7, 0x08, 0x8E);
|
||||
idt_make_entry(40, (uint32_t)&irq8, 0x08, 0x8E);
|
||||
idt_make_entry(41, (uint32_t)&irq9, 0x08, 0x8E);
|
||||
idt_make_entry(42, (uint32_t)&irq10, 0x08, 0x8E);
|
||||
idt_make_entry(43, (uint32_t)&irq11, 0x08, 0x8E);
|
||||
idt_make_entry(44, (uint32_t)&irq12, 0x08, 0x8E);
|
||||
idt_make_entry(45, (uint32_t)&irq13, 0x08, 0x8E);
|
||||
idt_make_entry(46, (uint32_t)&irq14, 0x08, 0x8E);
|
||||
idt_make_entry(47, (uint32_t)&irq15, 0x08, 0x8E);
|
||||
idt_make_entry(128, (uint32_t)&except128, 0x08, 0xEE);
|
||||
idt_make_entry(0, (uint32_t)&except0, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(1, (uint32_t)&except1, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(2, (uint32_t)&except2, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(3, (uint32_t)&except3, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(4, (uint32_t)&except4, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(5, (uint32_t)&except5, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(6, (uint32_t)&except6, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(7, (uint32_t)&except7, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(8, (uint32_t)&except8, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(9, (uint32_t)&except9, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(10, (uint32_t)&except10, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(11, (uint32_t)&except11, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(12, (uint32_t)&except12, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(13, (uint32_t)&except13, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(14, (uint32_t)&except14, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(15, (uint32_t)&except15, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(16, (uint32_t)&except16, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(17, (uint32_t)&except17, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(18, (uint32_t)&except18, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(19, (uint32_t)&except19, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(20, (uint32_t)&except20, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(21, (uint32_t)&except21, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(22, (uint32_t)&except22, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(23, (uint32_t)&except23, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(24, (uint32_t)&except24, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(25, (uint32_t)&except25, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(26, (uint32_t)&except26, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(27, (uint32_t)&except27, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(28, (uint32_t)&except28, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(29, (uint32_t)&except29, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(30, (uint32_t)&except30, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(31, (uint32_t)&except31, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(32, (uint32_t)&irq0, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(33, (uint32_t)&irq1, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(34, (uint32_t)&irq2, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(35, (uint32_t)&irq3, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(36, (uint32_t)&irq4, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(37, (uint32_t)&irq5, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(38, (uint32_t)&irq6, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(39, (uint32_t)&irq7, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(40, (uint32_t)&irq8, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(41, (uint32_t)&irq9, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(42, (uint32_t)&irq10, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(43, (uint32_t)&irq11, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(44, (uint32_t)&irq12, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(45, (uint32_t)&irq13, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(46, (uint32_t)&irq14, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(47, (uint32_t)&irq15, GDT_KERNEL_CS, 0x0E, 0);
|
||||
idt_make_entry(128, (uint32_t)&except128, GDT_KERNEL_CS, 0x0E, 3);
|
||||
|
||||
idt_flush((ptr_t)&idtptr);
|
||||
}
|
||||
@@ -164,8 +158,8 @@ void idt_init(void) {
|
||||
void cpu_init(void) {
|
||||
gdt_init();
|
||||
tss_init();
|
||||
pic_init();
|
||||
idt_init();
|
||||
pic_init();
|
||||
}
|
||||
|
||||
static uint32_t intr_save1(void) {
|
||||
|
||||
Reference in New Issue
Block a user