From 9a4ff385bdf291d614eb8dec0a807c3f1157b84d Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Tue, 14 Mar 2023 04:25:06 -0400 Subject: [PATCH] pic: enable legacy pic serial interrupts --- dev/apic.c | 78 +++++++++++++++++++++++++++++++++------- dev/serial.c | 9 +++++ include/dev/apic.h | 10 ++++-- include/dev/serial.h | 6 ++++ include/kernel/gdt.h | 8 +++-- include/kernel/idt.h | 8 +++-- include/kernel/pio.h | 5 +++ init/descriptor.S | 4 +-- init/gdt.c | 8 +++-- init/idt.c | 85 ++++++++++++++++++++++++++++++++++++++++++-- init/main.c | 13 ++++--- mem/early.c | 2 +- 12 files changed, 202 insertions(+), 34 deletions(-) diff --git a/dev/apic.c b/dev/apic.c index 874e34b..219333a 100644 --- a/dev/apic.c +++ b/dev/apic.c @@ -1,5 +1,3 @@ -/* SPDX-License-Identifier: GPL-2.0-only */ -#include #include #include #include @@ -9,25 +7,59 @@ static void *madt_entries[0x20]; static int n_madt_entries = 0; + static uintptr_t ioapic_addr = 0; +static uintptr_t lapic_addr = 0; +static uint8_t lapic_id = 0; static const char *madt_entry_types[] = { "LAPIC", "IOAPIC", "IOAPIC_IRQ_SRC_OVRD", "IOAPIC_NMI_SRC", "LAPIC_NMI", "LAPIC_ADDR_OVRD", "INVALID", "INVALID", "INVALID", "CPU_LOCAL_X2_APIC" }; -uint32_t ioapic_read(uintptr_t addr, uint8_t reg) +inline uint32_t ioapic_read(uintptr_t addr, uint8_t reg) { *(volatile uint32_t *)(addr) = reg; return *(volatile uint32_t *)(addr + 0x10); } -void ioapic_write(uintptr_t addr, uint8_t reg, uint32_t value) +inline void ioapic_write(uintptr_t addr, uint8_t reg, uint32_t value) { *(volatile uint32_t *)(addr) = reg; *(volatile uint32_t *)(addr + 0x10) = value; } +void ioapic_redirect_insert(uint8_t irq, uint8_t isr, uint8_t low_active, uint8_t level_trigger) +{ + int entryno = 0x10 + irq * 2; + struct apic_redirect entry; + entry.isr = isr; + entry.delv_mode = 0; + entry.dest_mode = 0; + entry.polarity = low_active; + entry.trigger = level_trigger; + entry.mask = 0; + entry.dest = lapic_id; + + ioapic_write(ioapic_addr, entryno, entry.low); + ioapic_write(ioapic_addr, entryno + 1, entry.high); + + entryno += 2; +} + +inline uint32_t lapic_read(uintptr_t addr, unsigned int regoffset) +{ + volatile uint32_t *lapic = (volatile uint32_t *)addr; + return lapic[regoffset >> 2]; +} + +inline uint32_t lapic_write(uintptr_t addr, unsigned int regoffset, uint32_t value) +{ + volatile uint32_t *lapic = (volatile uint32_t *)addr; + lapic[regoffset >> 2] = value; + return lapic[regoffset >> 2]; +} + int madt_parse_next_entry(int offset) { if ((unsigned)offset >= (__madt->h.length - 1)) @@ -41,9 +73,12 @@ int madt_parse_next_entry(int offset) /* Can't declare variables inside switch statement */ struct madt_ioapic *m_ioapic = (struct madt_ioapic *)madt; + struct madt_lapic *m_lapic = (struct madt_lapic *)madt; switch (type) { case MADT_LAPIC: + lapic_id = m_lapic->apic_id; + printf(LOG_INFO "LAPIC ID: %xh\n", m_lapic->apic_id); break; case MADT_IOAPIC: ioapic_addr = m_ioapic->ioapic_addr; @@ -71,24 +106,41 @@ int madt_parse_next_entry(int offset) return offset + len; } +void apic_eoi() +{ + lapic_write(lapic_addr, 0xB0, 0); +} + void apic_init() { if (__madt == NULL) { printf(LOG_WARN "Failed to locate MADT\n"); - } else { - /* First variable entry in MADT */ - int offset = 0x2C; - int length = __madt->h.length; - printf(LOG_INFO "MADT Length: %x\n", length); - - while ((offset = madt_parse_next_entry(offset))) - ; + return; } - ioapic_addr += hhdm_start; + int offset = 0x2C; + int length = __madt->h.length; + printf(LOG_INFO "MADT Length: %x\n", length); + + while ((offset = madt_parse_next_entry(offset))) + ; + + lapic_addr = __madt->lapic_addr; + printf(LOG_INFO "IOAPIC ADDR: %X\n", ioapic_addr); + printf(LOG_INFO "LAPIC ADDR : %X\n", lapic_addr); + uint32_t r = ioapic_read(ioapic_addr, 0); + r |= 0x04000000; + ioapic_write(ioapic_addr, 0, r); + printf(LOG_WARN "IOAPIC ID:%x\n", r); + + printf(LOG_WARN "LAPIC_ID:%xh\n", lapic_id); + ioapic_redirect_insert(4, 0x21, 0, 0); + /* Set spurious interrupt vector to 0xFF and enable the LAPIC */ + lapic_id = lapic_write(lapic_addr, 0xF0, 0x1FF); + printf(LOG_WARN "%x\n", lapic_id); printf(LOG_SUCCESS "APIC initialized\n"); } diff --git a/dev/serial.c b/dev/serial.c index 75c0048..f537696 100644 --- a/dev/serial.c +++ b/dev/serial.c @@ -23,6 +23,7 @@ int serial_init() return 1; } + outb(COM1 | 1, 0x01); /* Enable interrupts */ outb(COM1 | 4, 0x0F); /* Enter normal operation */ printf(LOG_SUCCESS "Serial COM1 initialized\n"); return 0; @@ -36,3 +37,11 @@ void serial_write(char c) outb(COM1, c); } + +void serial_trap() +{ + while ((inb(COM1 | 5) & 0x01) == 0) + ; + char c = inb(COM1); + printf("%c", c); +} diff --git a/include/dev/apic.h b/include/dev/apic.h index 814120f..a1958d3 100644 --- a/include/dev/apic.h +++ b/include/dev/apic.h @@ -108,7 +108,7 @@ struct madt_x2_lapic { struct apic_redirect { union { struct { - uint64_t vector : 8; + uint64_t isr : 8; uint64_t delv_mode : 3; uint64_t dest_mode : 1; uint64_t delv_status : 1; @@ -118,16 +118,20 @@ struct apic_redirect { uint64_t mask : 1; uint64_t reserved : 39; uint64_t dest : 8; - }; + } PACKED; struct { uint32_t low; uint32_t high; - }; + } PACKED; }; } PACKED; void apic_init(); int madt_parse_next_entry(int offset); +uint32_t ioapic_read(uintptr_t addr, uint8_t reg); +void ioapic_write(uintptr_t addr, uint8_t reg, uint32_t value); +uint32_t lapic_read(uintptr_t addr, unsigned int regoffset); +uint32_t lapic_write(uintptr_t addr, unsigned int regoffset, uint32_t value); #endif /* __ASM__ */ #endif /* _APIC_H_ */ diff --git a/include/dev/serial.h b/include/dev/serial.h index d9f4ed5..5f176ef 100644 --- a/include/dev/serial.h +++ b/include/dev/serial.h @@ -4,7 +4,13 @@ #define COM1 0x3F8 +#ifndef __ASM__ + +void isr_serial_input(); +void isr_serial_input_1(); void serial_write(char c); int serial_init(); +#endif + #endif /* _SERIAL_H_ */ diff --git a/include/kernel/gdt.h b/include/kernel/gdt.h index 2423062..061e0d2 100644 --- a/include/kernel/gdt.h +++ b/include/kernel/gdt.h @@ -11,9 +11,11 @@ #define GDT_ACCESS_DATA_RING3 0b11110010 #define GDT_ACCESS_CODE_RING3 0b11111010 +#define GDT_SEGMENT_CODE_RING0 0x28 + #ifndef __ASM__ -#include +#include struct gdt_desc { uint16_t limit; @@ -22,12 +24,12 @@ struct gdt_desc { uint8_t access; uint8_t flags; uint8_t base_3; -}; +} PACKED; struct gdtr { uint16_t size; uint64_t offset; -}; +} PACKED; extern struct gdtr __gdtr; extern struct gdt_desc __gdt[GDT_NUMDESC]; diff --git a/include/kernel/idt.h b/include/kernel/idt.h index 6a3df59..c561349 100644 --- a/include/kernel/idt.h +++ b/include/kernel/idt.h @@ -11,7 +11,7 @@ #ifndef __ASM__ -#include +#include struct irqdesc { uint16_t offset_low; @@ -21,14 +21,16 @@ struct irqdesc { uint16_t offset_mid; uint32_t offset_high; uint32_t reserved; -}; +} PACKED; struct idtr { uint16_t size; uint64_t offset; -}; +} PACKED; void idt_init(); +void cli(); +void sti(); extern struct idtr __idtr; extern struct irqdesc __idt[IDT_NUM_VECTORS]; diff --git a/include/kernel/pio.h b/include/kernel/pio.h index 706d32f..13b72dd 100644 --- a/include/kernel/pio.h +++ b/include/kernel/pio.h @@ -16,4 +16,9 @@ static inline uint8_t inb(uint16_t port) return ret; } +static inline void iowait() +{ + outb(0x80, 0); +} + #endif /* _PIO_H_ */ diff --git a/init/descriptor.S b/init/descriptor.S index fd40392..abe2256 100644 --- a/init/descriptor.S +++ b/init/descriptor.S @@ -31,10 +31,10 @@ __idt: .global gdt_load gdt_load: - lgdt (__gdtr) + lgdt __gdtr ret .global idt_load idt_load: - lidt (__idtr) + lidt __idtr ret diff --git a/init/gdt.c b/init/gdt.c index bbe58fe..e979264 100644 --- a/init/gdt.c +++ b/init/gdt.c @@ -23,10 +23,14 @@ 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_RING3, 0xA0); - gdt_insert(4, GDT_ACCESS_DATA_RING3, 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); + __gdtr.size = (sizeof(__gdt) - 1); __gdtr.offset = (uint64_t)__gdt; + gdt_load(); printf(LOG_SUCCESS "GDT initialized\n"); diff --git a/init/idt.c b/init/idt.c index 89da3a4..be84146 100644 --- a/init/idt.c +++ b/init/idt.c @@ -1,11 +1,55 @@ /* SPDX-License-Identifier: GPL-2.0-only */ +#include +#include #include +#include #include -void idt_load(); +#define exception(_i) _exception_##_i +#define exception_decl(_j) void exception(_j)() -void idt_insert(uint16_t gate, uint8_t type, uint8_t ist, uint16_t segment, - void *func) +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); + +void idt_load(); +void isr_sched(); + +void sched_test() +{ + printf("a"); +} + +void idt_insert(uint16_t gate, uint8_t type, uint8_t ist, uint16_t segment, void *func) { uintptr_t f = (uintptr_t)(func); struct irqdesc *irqdesc = (__idt + gate); @@ -20,6 +64,41 @@ void idt_insert(uint16_t gate, uint8_t type, uint8_t ist, uint16_t segment, 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(0x24, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, isr_serial_input); + __idtr.size = (IDT_SIZE - 1); __idtr.offset = (uint64_t)__idt; idt_load(); diff --git a/init/main.c b/init/main.c index 47dd43c..53595b8 100644 --- a/init/main.c +++ b/init/main.c @@ -5,7 +5,7 @@ #include #include -#include +#include #include #include @@ -31,17 +31,22 @@ void kmain(void) bss_start = (uintptr_t)(&__bss_start); bss_end = (uintptr_t)(&__bss_end); - serial_init(); gdt_init(); idt_init(); + serial_init(); + /* Initialize early kernel memory array */ const uintptr_t one_gb = 0x40000000; char kmem[one_gb] ALIGN(0x1000); mem_early_init(kmem, one_gb); - acpi_init(); - apic_init(); + cli(); + pic_mask(4, 0); + pic_init(); + sti(); + + printf("# "); done(); } diff --git a/mem/early.c b/mem/early.c index 16d3009..a5e40ad 100644 --- a/mem/early.c +++ b/mem/early.c @@ -110,7 +110,7 @@ void mem_early_init(char *mem, size_t len) kmap(kpaddr, kvaddr, kernel_size, attrs.val); kmap(0x0000, hhdm_start, 0x100000000, attrs.val); - kmap(0x1000, 0x1000, 0x100000000, attrs.val); + kmap(0x0000, 0x0000, 0x100000000, attrs.val); uintptr_t pml4_paddr = (uintptr_t)pml4; pml4_paddr -= hhdm_start;