summaryrefslogtreecommitdiff
path: root/src/arch/amd64
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2024-02-04 14:19:54 -0500
committerFreya Murphy <freya@freyacat.org>2024-02-04 14:19:54 -0500
commit1b09896afcf562d199d4df8d671601bba2b1f081 (patch)
treeb4ee4ae8e6e1ff5c5b27fe97509752b17fae330b /src/arch/amd64
parentfix acpi on uefi, kprint fixes (diff)
downloadcorn-main.tar.gz
corn-main.tar.bz2
corn-main.zip
refactor archHEADmain
Diffstat (limited to 'src/arch/amd64')
-rw-r--r--src/arch/amd64/acpi.c337
-rw-r--r--src/arch/amd64/bindings.h53
-rw-r--r--src/arch/amd64/cpu/idt.c6
-rw-r--r--src/arch/amd64/drivers/bochs.c73
-rw-r--r--src/arch/amd64/drivers/pci.c166
-rw-r--r--src/arch/amd64/drivers/pic.c89
-rw-r--r--src/arch/amd64/drivers/pic.h31
-rw-r--r--src/arch/amd64/drivers/serial.c48
-rw-r--r--src/arch/amd64/fpu.c5
-rw-r--r--src/arch/amd64/fpu.h3
-rw-r--r--src/arch/amd64/mboot.c284
-rw-r--r--src/arch/amd64/mboot.h11
-rw-r--r--src/arch/amd64/paging.c6
-rw-r--r--src/arch/amd64/panic.c4
-rw-r--r--src/arch/amd64/shim.c6
15 files changed, 17 insertions, 1105 deletions
diff --git a/src/arch/amd64/acpi.c b/src/arch/amd64/acpi.c
deleted file mode 100644
index a7f38b6..0000000
--- a/src/arch/amd64/acpi.c
+++ /dev/null
@@ -1,337 +0,0 @@
-#include <acpi.h>
-#include <lib.h>
-#include <memory.h>
-#include <stdint.h>
-#include <stddef.h>
-#include <string.h>
-#include <panic.h>
-
-#include "bindings.h"
-
-struct acpi_header {
- uint32_t signature;
- uint32_t length;
- uint8_t revision;
- uint8_t checksum;
- uint8_t oem_id[6];
- uint8_t oem_table_id[8];
- uint32_t oem_revision;
- uint32_t creator_id;
- uint32_t creator_revision;
-} __attribute__((packed));
-
-// root system descriptor pointer
-// ACPI 1.0
-struct rsdp {
- uint8_t signature[8];
- uint8_t checksum;
- uint8_t oemid[6];
- uint8_t revision;
- uint32_t rsdt_addr;
-} __attribute__((packed));
-
-// eXtended system descriptor pointer
-// ACPI 2.0
-struct xsdp {
- char signature[8];
- uint8_t checksum;
- char oemid[6];
- uint8_t revision;
- uint32_t rsdt_addr;
- uint32_t length;
- uint64_t xsdt_addr;
- uint8_t extendeid_checksum;
- uint8_t reserved[3];
-} __attribute__((packed));
-
-// root system descriptor table
-// ACPI 1.0
-struct rsdt {
- struct acpi_header h;
- uint32_t sdt_pointers[];
-} __attribute__((packed));
-
-// eXtended system descriptor table
-// ACPI 2.0
-struct xsdt {
- struct acpi_header h;
- uint64_t sdt_pointers[];
-} __attribute__((packed));
-
-
-// generic address structure
-struct gas {
- uint8_t address_space;
- uint8_t bit_width;
- uint8_t bit_offset;
- uint8_t access_size;
- uint64_t address;
-};
-
-// differentiated system description table
-struct dsdt {
- struct acpi_header h;
- char s5_addr[];
-} __attribute__((packed));
-
-struct apic {
- struct acpi_header h;
- // todo
-} __attribute__((packed));
-
-struct hept {
- struct acpi_header h;
- // todo
-} __attribute__((packed));
-
-struct waet {
- struct acpi_header h;
- // todo
-} __attribute__((packed));
-
-// fixed acpi description table
-struct fadt {
- struct acpi_header h;
- uint32_t firmware_ctrl;
- uint32_t dsdt;
-
- // field used in ACPI 1.0; no longer in use, for compatibility only
- uint8_t reserved;
-
- uint8_t preferred_power_management_profile;
- uint16_t sci_interrupt;
- uint32_t smi_command_port;
- uint8_t acpi_enable;
- uint8_t acpi_disable;
- uint8_t s4bios_req;
- uint8_t pstate_control;
- uint32_t pm1_a_event_block;
- uint32_t pm1_b_event_block;
- uint32_t pm1_a_control_block;
- uint32_t pm1_b_control_block;
- uint32_t pm2_control_block;
- uint32_t pm_timer_block;
- uint32_t gpe0_block;
- uint32_t gpe1_block;
- uint8_t pm1_event_length;
- uint8_t pm1_control_length;
- uint8_t pm2_control_length;
- uint8_t pm_timer_length;
- uint8_t gpe0_length;
- uint8_t gpe1_length;
- uint8_t gpe1_base;
- uint8_t cstate_control;
- uint16_t worst_c2_latency;
- uint16_t worst_c3_latency;
- uint16_t flush_size;
- uint16_t flush_stride;
- uint8_t duty_offset;
- uint8_t duty_width;
- uint8_t day_alarm;
- uint8_t month_alarm;
- uint8_t century;
-
- // reserved in ACPI 1.0; used since ACPI 2.0+
- uint16_t boot_architecture_flags;
-
- uint8_t reserved_2;
- uint32_t flags;
-
- // 12 byte structure; see below for details
- struct gas reset_reg;
-
- uint8_t reset_value;
- uint8_t reserved_3[3];
-
- // 64bit pointers - Available on ACPI 2.0+
- uint64_t x_firmware_control;
- uint64_t x_dsdt;
-
- struct gas x_pm1_a_event_block;
- struct gas x_pm1_b_event_block;
- struct gas x_pm1_a_control_block;
- struct gas x_pm1_b_control_block;
- struct gas x_pm2_control_block;
- struct gas x_pm_timer_block;
- struct gas x_gpe0_block;
- struct gas x_gpe1_block;
-} __attribute__((packed));
-
-struct acpi_state {
- union {
- struct xsdt *xsdt;
- struct rsdt *rsdt;
- } sdt;
- struct fadt *fadt;
- struct dsdt *dsdt;
- struct apic *apic;
- struct hept *hept;
- struct waet *waet;
- uint8_t version;
-
- uint16_t SLP_TYPa;
- uint16_t SLP_TYPb;
- uint16_t SLP_EN;
- uint16_t SCI_EN;
-};
-
-/* global state, idk a better way rn */
-static struct acpi_state state;
-
-static bool checksum(uint8_t *data, size_t len) {
- unsigned char sum = 0;
- for (size_t i = 0; i < len; i++)
- sum += data[i];
- return sum == 0;
-}
-
-static int read_s5_addr(struct dsdt *dsdt) {
- char *s5_addr = dsdt->s5_addr;
- int dsdt_len = dsdt->h.length - sizeof(struct acpi_header);
-
- while (0 < dsdt_len--) {
- if (memcmp(s5_addr, "_S5_", 4) == 0)
- break;
- s5_addr++;
- }
-
- if (dsdt_len > 0) {
- // check for valid AML structure
- if ( ( *(s5_addr-1) == 0x08 || ( *(s5_addr-2) == 0x08 && *(s5_addr-1) == '\\') ) && *(s5_addr+4) == 0x12 ) {
- s5_addr += 5;
- s5_addr += ((*s5_addr &0xC0)>>6) +2; // calculate PkgLength size
-
- if (*s5_addr == 0x0A)
- s5_addr++; // skip byteprefix
- state.SLP_TYPa = *(s5_addr)<<10;
- s5_addr++;
-
- if (*s5_addr == 0x0A)
- s5_addr++; // skip byteprefix
- state.SLP_TYPb = *(s5_addr)<<10;
-
- state.SLP_EN = 1<<13;
- state.SCI_EN = 1;
-
- } else {
- return -1;
- }
- } else {
- return -1;
- }
-
- return -1;
-}
-
-static void acpi_load_table(uint64_t addr);
-
-static void acpi_load_rsdt_tables(struct rsdt *rsdt) {
- int entries = (rsdt->h.length - sizeof(rsdt->h)) / 4;
- for (int i = 0; i < entries; i++) {
- uint32_t addr = rsdt->sdt_pointers[i];
- acpi_load_table(addr);
- }
-}
-
-static void acpi_load_xsdt_tables(struct xsdt *xsdt) {
- int entries = (xsdt->h.length - sizeof(xsdt->h)) / 8;
- for (int i = 0; i < entries; i++) {
- uint64_t addr = xsdt->sdt_pointers[i];
- acpi_load_table(addr);
- }
-}
-
-#define SIG_RSDT 0x54445352
-#define SIG_XSDT 0x54445358
-#define SIG_FACP 0x50434146
-#define SIG_DSDT 0x54445344
-#define SIG_APIC 0x43495041
-#define SIG_HEPT 0x54455048
-#define SIG_WAET 0x54454157
-
-static void acpi_handle_table(struct acpi_header *header) {
- switch (header->signature) {
- case SIG_RSDT:
- state.sdt.rsdt = (struct rsdt *) header;
- acpi_load_rsdt_tables(state.sdt.rsdt);
- break;
- case SIG_XSDT:
- state.sdt.xsdt = (struct xsdt *) header;
- acpi_load_xsdt_tables(state.sdt.xsdt);
- break;
- case SIG_FACP:
- state.fadt = (struct fadt *) header;
- acpi_load_table(state.fadt->dsdt);
- break;
- case SIG_DSDT:
- state.dsdt = (struct dsdt *) header;
- read_s5_addr(state.dsdt);
- break;
- case SIG_APIC:
- state.apic = (struct apic *) header;
- break;
- case SIG_HEPT:
- state.hept = (struct hept *) header;
- break;
- case SIG_WAET:
- state.waet = (struct waet *) header;
- break;
- default:
- break;
- }
-}
-
-static void acpi_load_table(uint64_t addr) {
- struct acpi_header *temp, *mapped;
- uint32_t length;
-
- temp = (struct acpi_header * ) (uintptr_t) addr;
- mapped = mmap(temp, sizeof(struct acpi_header));
- length = mapped->length;
- unmap(mapped);
- mapped = mmap(temp, length);
- if (!checksum((uint8_t *) mapped, mapped->length)) {
- unmap(mapped);
- return;
- }
- kprintf("%.*s: %#016lx\n", 4, (char*)&mapped->signature, (size_t)temp);
- acpi_handle_table(mapped);
-}
-
-int acpi_init(void *rootsdp) {
-
- memset(&state, 0, sizeof(struct acpi_state));
-
- struct rsdp *rsdp = (struct rsdp *) rootsdp;
-
- if (!checksum((uint8_t *)rsdp, sizeof(struct rsdp)))
- return -1;
-
- if (memcmp(rsdp->signature, "RSD PTR ", 8) != 0) {
- panic("invalid acpi rsdp signature: %.*s\n", 8, rsdp->signature);
- }
-
- if (rsdp->revision == 0) {
- state.version = 0;
- kprintf("ACPI 1.0\n");
- acpi_load_table(rsdp->rsdt_addr);
- } else if (rsdp->revision == 2) {
- state.version = 2;
- struct xsdp *xsdp = (struct xsdp *) rsdp;
- kprintf("ACPI 2.0\n");
- acpi_load_table(xsdp->xsdt_addr);
- } else {
- panic("invalid acpi rev: %d\n", rsdp->revision);
- }
-
- kprintf("\n");
-
- outb(state.fadt->smi_command_port,state.fadt->acpi_enable);
-
- return 0;
-}
-
-int acpi_shutdown(void) {
- outw((unsigned int) state.fadt->pm1_a_control_block, state.SLP_TYPb | state.SLP_EN);
- return -1;
-}
diff --git a/src/arch/amd64/bindings.h b/src/arch/amd64/bindings.h
deleted file mode 100644
index 9406774..0000000
--- a/src/arch/amd64/bindings.h
+++ /dev/null
@@ -1,53 +0,0 @@
-#pragma once
-
-#include <stdint.h>
-
-static inline uint8_t inb(uint16_t port) {
- uint8_t ret;
- __asm__ volatile ("inb %1, %0" : "=a"(ret) : "Nd"(port));
- return ret;
-}
-
-static inline void outb(uint16_t port, uint8_t val) {
- __asm__ volatile ("outb %0, %1" : : "a"(val), "Nd"(port));
-}
-
-static inline uint16_t inw(uint16_t port) {
- uint16_t ret;
- __asm__ volatile ("inw %1, %0" : "=a"(ret) : "Nd"(port));
- return ret;
-}
-
-static inline void outw(uint16_t port, uint16_t val) {
- __asm__ volatile ("outw %0, %1" : : "a"(val), "Nd"(port));
-}
-
-static inline uint32_t inl(uint16_t port) {
- uint32_t ret;
- __asm__ volatile ("inl %1, %0" : "=a"(ret) : "Nd"(port));
- return ret;
-}
-
-static inline void outl(uint16_t port, uint32_t val) {
- __asm__ volatile ("outl %0, %1" : : "a"(val), "Nd"(port));
-}
-
-static inline void io_wait(void) {
- outb(0x80, 0);
-}
-
-static inline void sti(void) {
- __asm__ volatile ("sti");
-}
-
-static inline void cli(void) {
- __asm__ volatile ("cli");
-}
-
-static inline void int_wait(void) {
- __asm__ volatile ("sti; hlt");
-}
-
-static inline void halt(void) {
- __asm__ volatile ("cli; hlt");
-}
diff --git a/src/arch/amd64/cpu/idt.c b/src/arch/amd64/cpu/idt.c
index 5c2ef5c..a2da88f 100644
--- a/src/arch/amd64/cpu/idt.c
+++ b/src/arch/amd64/cpu/idt.c
@@ -1,14 +1,14 @@
#include <stdint.h>
#include <stddef.h>
#include <lib.h>
+#include <bindings.h>
+#include <pic.h>
#include "idt.h"
#include "backtrace.h"
#include "debugger.h"
#include "registers.h"
#include "../paging.h"
-#include "../bindings.h"
-#include "../drivers/pic.h"
#define IDT_SIZE 256
@@ -182,7 +182,7 @@ void idt_exception_handler(uint64_t exception, uint64_t code, struct isr_regs *s
}
kputs("\n");
-
+
isr_print_regs(state);
kputs("\n");
diff --git a/src/arch/amd64/drivers/bochs.c b/src/arch/amd64/drivers/bochs.c
deleted file mode 100644
index b3908f9..0000000
--- a/src/arch/amd64/drivers/bochs.c
+++ /dev/null
@@ -1,73 +0,0 @@
-#include <lib.h>
-#include <memory.h>
-#include <stdint.h>
-#include <panic.h>
-#include <pci.h>
-#include <bochs.h>
-
-#include "../bindings.h"
-
-#define INDEX 0x1CE
-#define DATA 0x1CF
-
-#define INDEX_ID 0
-#define INDEX_XRES 1
-#define INDEX_YRES 2
-#define INDEX_BPP 3
-#define INDEX_ENABLE 4
-#define INDEX_BANK 5
-#define INDEX_VIRT_WIDTH 6
-#define INDEX_VIRT_HEIGHT 7
-#define INDEX_X_OFFSET 8
-#define INDEX_Y_OFFSET 9
-
-#define DATA_DISP_DISABLE 0x00
-#define DATA_DISP_ENABLE 0x01
-#define DATA_LFB_ENABLE 0x40
-#define DATA_NO_CLEAR_MEM 0x80
-
-#define BOCHS_PCI_VENDOR 0x1234
-#define BOCHS_PCI_DEVICE 0x1111
-
-static void write(uint16_t index, uint16_t data) {
- outw(INDEX, index);
- outw(DATA, data);
-}
-
-static uint16_t read(uint16_t value) {
- outw(INDEX, value);
- return inw(DATA);
-}
-
-static int is_available(void) {
- return (read(INDEX_ID) == 0xB0C5);
-}
-
-static void set_mode(uint16_t width, uint16_t height, uint16_t bit_depth, int lfb, int clear) {
- write(INDEX_ENABLE, DATA_DISP_DISABLE);
- write(INDEX_XRES, width);
- write(INDEX_YRES, height);
- write(INDEX_BPP, bit_depth);
- write(INDEX_ENABLE, DATA_DISP_ENABLE |
- (lfb ? DATA_LFB_ENABLE : 0) |
- (clear ? 0 : DATA_NO_CLEAR_MEM));
-}
-
-volatile uint32_t *bochs_init(uint16_t width, uint16_t height, uint8_t bit_depth) {
-
- set_mode(width, height, bit_depth, true, true);
-
- if (!is_available())
- return NULL;
-
- struct pci_device bochs = {0};
- bool found = pci_findby_id(&bochs, BOCHS_PCI_DEVICE, BOCHS_PCI_VENDOR, NULL);
- if (!found)
- return NULL;
-
- uint32_t bar0 = pci_rcfg_d(bochs, PCI_BAR0_D);
- uint32_t *addr = (uint32_t *) (uintptr_t) bar0;
- addr = mmap(addr, width * height * bit_depth);
-
- return addr;
-}
diff --git a/src/arch/amd64/drivers/pci.c b/src/arch/amd64/drivers/pci.c
deleted file mode 100644
index 7aa11c8..0000000
--- a/src/arch/amd64/drivers/pci.c
+++ /dev/null
@@ -1,166 +0,0 @@
-#include <stdint.h>
-#include <pci.h>
-#include <panic.h>
-#include <lib.h>
-
-#include "../bindings.h"
-
-#define CONF_ADDR 0xCF8
-#define CONF_DATA 0xCFC
-
-#define TABLE_LEN 16
-
-struct pci_table_entry {
- struct pci_device device;
- uint16_t device_id;
- uint16_t vendor_id;
- uint8_t class;
- uint8_t subclass;
- uint8_t prog_if;
- uint8_t revision;
-};
-
-static struct pci_table_entry pci_table[TABLE_LEN];
-static size_t pci_table_next = 0;
-
-uint32_t pci_rcfg_d(struct pci_device dev, uint8_t offset) {
- uint32_t addr = 0x80000000;
- addr |= ((uint32_t)dev.bus) << 16;
- addr |= ((uint32_t)dev.device) << 11;
- addr |= ((uint32_t)dev.function) << 8;
- addr |= offset & 0xFC;
-
- outl(CONF_ADDR, addr);
- uint32_t in = inl(CONF_DATA);
- return in;
-}
-
-uint16_t pci_rcfg_w(struct pci_device dev, uint8_t offset) {
- uint32_t dword = pci_rcfg_d(dev, offset);
- return (uint16_t)((dword >> ((offset & 2) * 8)) & 0xFFFF);
-}
-
-uint8_t pci_rcfg_b(struct pci_device dev, uint8_t offset) {
- uint32_t dword = pci_rcfg_d(dev, offset);
- return (uint8_t)((dword >> ((offset & 3) * 8)) & 0xFF);
-}
-
-void pci_wcfg_d(struct pci_device dev, uint8_t offset, uint32_t dword) {
- uint32_t addr = 0x80000000;
- addr |= ((uint32_t)dev.bus) << 16;
- addr |= ((uint32_t)dev.device) << 11;
- addr |= ((uint32_t)dev.function) << 8;
- addr |= offset & 0xFC;
-
- outl(CONF_ADDR, addr);
- outl(CONF_DATA, dword);
-}
-
-void pci_wcfg_w(struct pci_device dev, uint8_t offset, uint16_t word) {
- size_t shift = (offset & 2) * 8;
- uint32_t dword = pci_rcfg_d(dev, offset);
- dword &= ~(0xFFFF << shift);
- dword |= word << shift;
- pci_wcfg_d(dev, offset, dword);
-}
-
-void pci_wcfg_b(struct pci_device dev, uint8_t offset, uint8_t byte) {
- size_t shift = (offset & 3) * 8;
- uint32_t dword = pci_rcfg_d(dev, offset);
- dword &= ~(0xFF << shift);
- dword |= byte << shift;
- pci_wcfg_d(dev, offset, dword);
-}
-
-static void print_device(struct pci_table_entry *entry) {
- kprintf(
- "BUS: %#-4x DEV: %#-4x FUNC: %#-4x ID: %04x:%04x CLASS: %02x:%02x:%02x REV: %#02x\n",
- entry->device.bus,
- entry->device.device,
- entry->device.function,
- entry->vendor_id,
- entry->device_id,
- entry->class,
- entry->subclass,
- entry->prog_if,
- entry->revision
- );
-}
-
-static struct pci_table_entry *load_device(struct pci_device dev) {
- if (pci_table_next >= TABLE_LEN) panic("Too many PCI devices: limit is %d", TABLE_LEN);
- struct pci_table_entry *entry = &pci_table[pci_table_next++];
- entry->device = dev;
- uint32_t dword0 = pci_rcfg_d(dev, 0);
- uint32_t dword2 = pci_rcfg_d(dev, 8);
-
- entry->device_id = (dword0 >> 16) & 0xFFFF;
- entry->vendor_id = dword0 & 0xFFFF;
-
- entry->class = (dword2 >> 24) & 0xFF;
- entry->subclass = (dword2 >> 16) & 0xFF;
- entry->prog_if = (dword2 >> 8) & 0xFF;
- entry->revision = dword2 & 0xFF;
-
- return entry;
-}
-
-void pci_init(void) {
- pci_table_next = 0;
- struct pci_device pcidev;
- for (int bus = 0; bus < 256; bus++) {
- pcidev.bus = bus;
- for (int dev = 0; dev < 32; dev++) {
- pcidev.device = dev;
- pcidev.function = 0;
-
- uint16_t vendor = pci_rcfg_w(pcidev, 0);
- if (vendor == 0xFFFF) continue;
-
- load_device(pcidev);
-
- uint8_t header_type = pci_rcfg_b(pcidev, 14);
-
- if (!(header_type & 0x80)) continue;
- for (int func = 1; func < 8; func++) {
- pcidev.function = func;
-
- uint16_t vendor = pci_rcfg_w(pcidev, 0);
- if (vendor == 0xFFFF) continue;
-
- load_device(pcidev);
- }
- }
- }
- kprintf("PCI DEVICES\n");
- for (size_t i = 0; i < pci_table_next; i++) {
- print_device(&pci_table[i]);
- }
- kprintf("\n");
-}
-
-bool pci_findby_class(struct pci_device *dest, uint8_t class, uint8_t subclass, size_t *offset) {
- size_t o = 0;
- if (offset == NULL) offset = &o;
- for (; *offset < pci_table_next; (*offset)++) {
- struct pci_table_entry *entry = &pci_table[*offset];
- if (entry->class == class && entry->subclass == subclass) {
- *dest = entry->device;
- return true;
- }
- }
- return false;
-}
-
-bool pci_findby_id(struct pci_device *dest, uint16_t device, uint16_t vendor, size_t *offset) {
- size_t o = 0;
- if (offset == NULL) offset = &o;
- for (; *offset < pci_table_next; (*offset)++) {
- struct pci_table_entry *entry = &pci_table[*offset];
- if (entry->device_id == device && entry->vendor_id == vendor) {
- *dest = entry->device;
- return true;
- }
- }
- return false;
-}
diff --git a/src/arch/amd64/drivers/pic.c b/src/arch/amd64/drivers/pic.c
deleted file mode 100644
index be7716f..0000000
--- a/src/arch/amd64/drivers/pic.c
+++ /dev/null
@@ -1,89 +0,0 @@
-#include "../bindings.h"
-#include "pic.h"
-
-#define PIC1 0x20 /* IO base address for master PIC */
-#define PIC2 0xA0 /* IO base address for slave PIC */
-#define PIC1_COMMAND PIC1
-#define PIC1_DATA (PIC1+1)
-#define PIC2_COMMAND PIC2
-#define PIC2_DATA (PIC2+1)
-
-#define PIC_EOI 0x20 /* End-of-interrupt command code */
-
-#define ICW1_ICW4 0x01 /* Indicates that ICW4 will be present */
-#define ICW1_SINGLE 0x02 /* Single (cascade) mode */
-#define ICW1_INTERVAL4 0x04 /* Call address interval 4 (8) */
-#define ICW1_LEVEL 0x08 /* Level triggered (edge) mode */
-#define ICW1_INIT 0x10 /* Initialization - required! */
-
-#define ICW4_8086 0x01 /* 8086/88 (MCS-80/85) mode */
-#define ICW4_AUTO 0x02 /* Auto (normal) EOI */
-#define ICW4_BUF_SLAVE 0x08 /* Buffered mode/slave */
-#define ICW4_BUF_MASTER 0x0C /* Buffered mode/master */
-#define ICW4_SFNM 0x10 /* Special fully nested (not) */
-
-void pic_remap(void) {
- uint8_t a1, a2;
-
- a1 = inb(PIC1_DATA); // save masks
- a2 = inb(PIC2_DATA);
-
- outb(PIC1_COMMAND, ICW1_INIT | ICW1_ICW4); // starts the initialization sequence (in cascade mode)
- io_wait();
- outb(PIC2_COMMAND, ICW1_INIT | ICW1_ICW4);
- io_wait();
- outb(PIC1_DATA, PIC_REMAP_OFFSET); // ICW2: Master PIC vector offset
- io_wait();
- outb(PIC2_DATA, PIC_REMAP_OFFSET + 8); // ICW2: Slave PIC vector offset
- io_wait();
- outb(PIC1_DATA, 4); // ICW3: tell Master PIC that there is a slave PIC at IRQ2 (0000 0100)
- io_wait();
- outb(PIC2_DATA, 2); // ICW3: tell Slave PIC its cascade identity (0000 0010)
- io_wait();
-
- outb(PIC1_DATA, ICW4_8086); // ICW4: have the PICs use 8086 mode (and not 8080 mode)
- io_wait();
- outb(PIC2_DATA, ICW4_8086);
- io_wait();
-
- outb(PIC1_DATA, a1); // restore saved masks.
- outb(PIC2_DATA, a2);
-}
-
-void pic_mask(int irq) {
- uint16_t port;
- uint8_t mask;
- if (irq < 8) {
- port = PIC1_DATA;
- } else {
- port = PIC2_DATA;
- irq -= 8;
- }
- mask = inb(port) | (1 << irq);
- outb(port, mask);
-}
-
-void pic_unmask(int irq) {
- uint16_t port;
- uint8_t mask;
- if (irq < 8) {
- port = PIC1_DATA;
- } else {
- irq -= 8;
- port = PIC2_DATA;
- }
- mask = inb(port) & ~(1 << irq);
- outb(port, mask);
-}
-
-void pic_disable(void) {
- outb(PIC1_DATA, 0xff);
- outb(PIC2_DATA, 0xff);
-}
-
-void pic_eoi(int irq) {
- if (irq >= 8) {
- outb(PIC2_COMMAND, PIC_EOI);
- }
- outb(PIC1_COMMAND, PIC_EOI);
-}
diff --git a/src/arch/amd64/drivers/pic.h b/src/arch/amd64/drivers/pic.h
deleted file mode 100644
index 2a4670e..0000000
--- a/src/arch/amd64/drivers/pic.h
+++ /dev/null
@@ -1,31 +0,0 @@
-#pragma once
-
-#define PIC_REMAP_OFFSET 0x20
-
-/**
- * Remaps the pie, i.e. initializes it
- */
-void pic_remap(void);
-
-/**
- * Masks an external irq to stop firing until un masked
- * @param irq - the irq to mask
- */
-void pic_mask(int irq);
-
-/**
- * Unmasks an external irq to allow interrupts to continue for that irq
- * @param irq - the irq to unmask
- */
-void pic_unmask(int irq);
-
-/**
- * Disabled the pick
- */
-void pic_disable(void);
-
-/**
- * Tells the pick that the interrupt has ended
- * @param irq - the irq that has ended
- */
-void pic_eoi(int irq);
diff --git a/src/arch/amd64/drivers/serial.c b/src/arch/amd64/drivers/serial.c
deleted file mode 100644
index b9e351e..0000000
--- a/src/arch/amd64/drivers/serial.c
+++ /dev/null
@@ -1,48 +0,0 @@
-#include <serial.h>
-
-#include "../bindings.h"
-
-#define PORT 0x3F8
-
-// initialize the specified COM port for serial
-// see https://wiki.osdev.org/Serial_Ports
-// and https://en.wikibooks.org/wiki/Serial_Programming/8250_UART_Programming
-int serial_init(void) {
- outb(PORT + 1, 0x00); // disable interrupts
- outb(PORT + 3, 0x80); // enable DLAB (divisor latch access bit)
- outb(PORT + 0, 0x03); // (lo) Set baud divisor to 3 38400 baud
- outb(PORT + 1, 0x00); // (hi)
- outb(PORT + 3, 0x03); // disable DLAB, set 8 bits per word, one stop bit, no parity
- outb(PORT + 2, 0xC7); // enable and clear FIFOs, set to maximum threshold
- // outb(port + 4, 0x0B); // TODO copied this from osdev wiki but i don't think you need it here
- outb(PORT + 4, 0x1E); // set in loopback mode for test
- outb(PORT + 0, 0xAE); // test by sending 0xAE
-
- uint8_t response = inb(PORT + 0);
- if (response != 0xAE) {
- // TODO panic here?
- return -1;
- }
-
- // disable loopback, enable IRQs
- outb(PORT + 4, 0x0F);
- return 0;
-}
-
-uint8_t serial_in(void) {
- // wait for data to be available
- while ((inb(PORT + 5) & 0x01) == 0);
- return inb(PORT);
-}
-
-void serial_out(uint8_t ch) {
- // wait for output to be free
- while ((inb(PORT + 5) & 0x20) == 0);
- outb(PORT, ch);
-}
-
-void serial_out_str(const char *str) {
- for (; *str != '\0'; str++) {
- serial_out(*str);
- }
-}
diff --git a/src/arch/amd64/fpu.c b/src/arch/amd64/fpu.c
index 5a8aa92..292264c 100644
--- a/src/arch/amd64/fpu.c
+++ b/src/arch/amd64/fpu.c
@@ -1,9 +1,10 @@
-#include <fpu.h>
#include <stddef.h>
#include <stdint.h>
#include <lib.h>
-void enable_fpu(void) {
+#include "fpu.h"
+
+void fpu_enable(void) {
size_t cr4;
uint16_t cw = 0x37F;
__asm__ volatile ("mov %%cr4, %0" : "=r"(cr4));
diff --git a/src/arch/amd64/fpu.h b/src/arch/amd64/fpu.h
new file mode 100644
index 0000000..bd7ca67
--- /dev/null
+++ b/src/arch/amd64/fpu.h
@@ -0,0 +1,3 @@
+#pragma once
+
+void fpu_enable(void);
diff --git a/src/arch/amd64/mboot.c b/src/arch/amd64/mboot.c
deleted file mode 100644
index 9fd7fc2..0000000
--- a/src/arch/amd64/mboot.c
+++ /dev/null
@@ -1,284 +0,0 @@
-#include <shim.h>
-#include <lib.h>
-#include <stdint.h>
-#include <panic.h>
-
-#include "mboot.h"
-
-#define MBOOT_HEADER_MAGIC 0x36D76289
-
-#define MBOOT_CMDLINE 1
-#define MBOOT_MEMORY_MAP 6
-#define MBOOT_FRAMEBUFFER 8
-#define MBOOT_ELF_SYMBOLS 9
-#define MBOOT_OLD_RSDP 14
-#define MBOOT_NEW_RSDP 15
-
-struct mboot_info {
- uint32_t total_size;
- uint32_t reserved;
- char tags[];
-};
-
-struct mboot_tag {
- uint32_t type;
- uint32_t size;
- char data[];
-};
-
-struct mboot_tag_elf_sections {
- uint32_t type;
- uint32_t size;
- uint16_t num;
- uint16_t entsize;
- uint16_t shndx;
- uint16_t reserved;
- char sections[];
-};
-
-struct mboot_tag_elf_sections_entry {
- uint32_t sh_name;
- uint32_t sh_type;
- uint64_t sh_flags;
- uint64_t sh_addr;
- uint64_t sh_offset;
- uint64_t sh_size;
- uint32_t sh_link;
- uint32_t sh_info;
- uint64_t sh_addralign;
- uint64_t sh_entsize;
-};
-
-struct mboot_mmap_entry {
- uint64_t addr;
- uint64_t len;
- uint32_t type;
- uint32_t zero;
-};
-
-struct mboot_tag_mmap {
- uint32_t type;
- uint32_t size;
- uint32_t entry_size;
- uint32_t entry_version;
- struct mboot_mmap_entry entries[];
-};
-
-
-struct mboot_tag_old_rsdp {
- uint32_t type;
- uint32_t size;
- char rsdp[];
-};
-
-struct mboot_tag_new_rsdp {
- uint32_t type;
- uint32_t size;
- char rsdp[];
-};
-
-struct mboot_tag_cmdline {
- uint32_t type;
- uint32_t size;
- uint8_t cmdline[];
-};
-
-struct mboot_tag_framebuffer {
- uint32_t type;
- uint32_t size;
- uint64_t framebuffer_addr;
- uint32_t framebuffer_pitch;
- uint32_t framebuffer_width;
- uint32_t framebuffer_height;
- uint8_t framebuffer_bpp;
- uint8_t framebuffer_type;
- uint16_t reserved;
-};
-
-static void read_symbols(
- volatile struct boot_info *shim_info,
- volatile struct mboot_tag_elf_sections *sections
-) {
- shim_info->symbol_table = (void * volatile) sections->sections;
-
-// struct mboot_elf_section_header *section =
-// (struct mboot_elf_section_header *) (layout->elf_section_headers);
-//
-// for (uint32_t i = 0; i < layout->num; i++) {
-// char buf[20];
-//
-// ultoa(i, buf, 10);
-// serial_out_str("[");
-// serial_out_str(buf);
-// serial_out_str("]\t");
-//
-// serial_out_str((char *)(kaddr(symtab) + section->sh_name));
-// serial_out('\t');
-//
-// ultoa(section->sh_type, buf, 16);
-// serial_out_str("type: 0x");
-// serial_out_str(buf);
-// serial_out('\t');
-//
-// ultoa(section->sh_addr, buf, 16);
-// serial_out_str("addr: 0x");
-// serial_out_str(buf);
-// serial_out('\t');
-//
-// ultoa(section->sh_offset, buf, 16);
-// serial_out_str("offset: 0x");
-// serial_out_str(buf);
-// serial_out('\n');
-//
-// section++;
-// }
-}
-
-static void read_cmdline(
- volatile struct boot_info *shim_info,
- volatile struct mboot_tag_cmdline *cmdline
-) {
- uint32_t size = cmdline->size - 8;
- if (size >= CMDLINE_MAX)
- size = CMDLINE_MAX; // truncate :(
- memcpyv(shim_info->cmdline, cmdline->cmdline, size);
- shim_info->cmdline[size] = '\0';
-}
-
-static void read_framebuffer(
- volatile struct boot_info *shim_info,
- volatile struct mboot_tag_framebuffer *framebuffer
-) {
- shim_info->fb.addr = framebuffer->framebuffer_addr;
- shim_info->fb.width = framebuffer->framebuffer_width;
- shim_info->fb.height = framebuffer->framebuffer_height;
- shim_info->fb.pitch = framebuffer->framebuffer_pitch;
- shim_info->fb.bit_depth = framebuffer->framebuffer_bpp;
-}
-
-static const char *segment_type[] = {
- "Reserved",
- "Free",
- "Reserved",
- "ACPI Reserved",
- "Hibernation",
- "Defective",
- "Unknown"
-};
-
-static void read_memory_map(
- volatile struct boot_info *shim_info,
- volatile struct mboot_tag_mmap *map
-) {
- int idx = 0;
- uintptr_t i = (uintptr_t)map->entries;
- kprintf("MEMORY MAP\n");
- char buf[20];
- for (;
- i < (uintptr_t)map->entries + map->size;
- i += map->entry_size, idx++
- ) {
- struct mboot_mmap_entry *seg = (struct mboot_mmap_entry *) i;
- const char *type = NULL;
- if (seg->type > 4)
- type = segment_type[6];
- else
- type = segment_type[seg->type];
- kprintf("ADDR: %16p LEN: %4s TYPE: %s (%d)\n",
- (void *)seg->addr,
- btoa(seg->len, buf),
- type,
- seg->type
- );
- if (seg->type != 1 || seg->len < 1)
- continue;
- shim_info->map.entries[idx].addr = seg->addr;
- shim_info->map.entries[idx].len = seg->len;
- }
- shim_info->map.entry_count = idx;
-}
-
-static void read_old_rsdp(
- volatile struct boot_info *shim_info,
- volatile struct mboot_tag_old_rsdp *rsdp
-) {
- if (shim_info->acpi_table != NULL)
- return; // xsdp is newer and has been loaded
- shim_info->acpi_table = (void *volatile) rsdp->rsdp;
-}
-
-static void read_new_rsdp(
- volatile struct boot_info *shim_info,
- volatile struct mboot_tag_new_rsdp *rsdp
-) {
- shim_info->acpi_table = (void *volatile) rsdp->rsdp;
-}
-
-void mboot_load_info(
- long mboot_magic,
- volatile const void *mboot_data_ptr,
- volatile struct boot_info *shim_info
-) {
-
- if (mboot_magic != MBOOT_HEADER_MAGIC)
- panic("invalid multiboot magic");
-
- memsetv(shim_info, 0, sizeof(struct boot_info));
-
- struct mboot_info *mboot_info = (struct mboot_info *) mboot_data_ptr;
- const char *mboot_end = ((char *) mboot_info) + mboot_info->total_size;
-
- char *tag_ptr = mboot_info->tags;
-
- while (tag_ptr < mboot_end) {
- struct mboot_tag *tag = (struct mboot_tag *) tag_ptr;
-
- switch (tag->type) {
- case MBOOT_CMDLINE:
- read_cmdline(
- shim_info,
- (struct mboot_tag_cmdline *) tag
- );
- break;
- case MBOOT_FRAMEBUFFER:
- read_framebuffer(
- shim_info,
- (struct mboot_tag_framebuffer *) tag
- );
- break;
- case MBOOT_MEMORY_MAP:
- read_memory_map(
- shim_info,
- (struct mboot_tag_mmap *) tag
- );
- break;
- case MBOOT_ELF_SYMBOLS:
- read_symbols(
- shim_info,
- (struct mboot_tag_elf_sections *) tag
- );
- break;
- case MBOOT_OLD_RSDP:
- read_old_rsdp(
- shim_info,
- (struct mboot_tag_old_rsdp *) tag
- );
- break;
- case MBOOT_NEW_RSDP:
- read_new_rsdp(
- shim_info,
- (struct mboot_tag_new_rsdp *) tag
- );
- break;
- default:
- break;
- }
-
- int size = tag->size;
- if (size % 8 != 0) {
- size += 8 - (size % 8);
- }
-
- tag_ptr += size;
- }
-}
diff --git a/src/arch/amd64/mboot.h b/src/arch/amd64/mboot.h
deleted file mode 100644
index d1ca1a0..0000000
--- a/src/arch/amd64/mboot.h
+++ /dev/null
@@ -1,11 +0,0 @@
-#pragma once
-
-#include <shim.h>
-#include <memory.h>
-
-/**
- * Loads the multi boot information
- * @param mboot_info - the pointer passed from multiboot2
- * @param shim_info - the info to be collected by shim
- */
-void mboot_load_info(long mboot_magic, const volatile void *mboot_data_ptr, volatile struct boot_info *shim_info);
diff --git a/src/arch/amd64/paging.c b/src/arch/amd64/paging.c
index c857d0b..7a718e1 100644
--- a/src/arch/amd64/paging.c
+++ b/src/arch/amd64/paging.c
@@ -3,13 +3,13 @@
#include <stdint.h>
#include <lib.h>
#include <memory.h>
+#include <bindings.h>
#define MEMORY_INTERNAL
-#include <memory/physalloc.h>
-#include <memory/virtalloc.h>
+#include <sys/physalloc.h>
+#include <sys/virtalloc.h>
#include "paging.h"
-#include "bindings.h"
// PAGE MAP LEVEL 4 ENTRY
struct pml4e {
diff --git a/src/arch/amd64/panic.c b/src/arch/amd64/panic.c
index 48e65f5..6acc88c 100644
--- a/src/arch/amd64/panic.c
+++ b/src/arch/amd64/panic.c
@@ -1,11 +1,9 @@
#include <panic.h>
#include <backtrace.h>
#include <stdarg.h>
-
+#include <bindings.h>
#include <lib.h>
-#include "bindings.h"
-
_Noreturn void _panic_impl(char *line, char *file, char *format, ...) {
cli();
va_list list;
diff --git a/src/arch/amd64/shim.c b/src/arch/amd64/shim.c
index c1c3368..05d995c 100644
--- a/src/arch/amd64/shim.c
+++ b/src/arch/amd64/shim.c
@@ -3,11 +3,12 @@
#include <lib.h>
#include <shim.h>
#include <memory.h>
+#include <mboot.h>
+#include <pic.h>
#include "paging.h"
-#include "mboot.h"
+#include "fpu.h"
#include "cpu/idt.h"
-#include "drivers/pic.h"
static struct boot_info boot_info;
@@ -16,6 +17,7 @@ void *amd64_shim(long mboot_magic, volatile void *mboot_data_ptr) {
paging_init();
pic_remap();
idt_init();
+ fpu_enable();
mboot_load_info(mboot_magic, mboot_data_ptr, &boot_info);