diff --git a/Makefile b/Makefile index ae2396e..0f034af 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,14 @@ -KERNEL=kernel.bin -ISO=os_image.iso +# +# Makefile Configuration +# -ARCH=amd64 CC=cc LD=ld AS=nasm +ARCH=amd64 +ARCH_COMMON=x86_common + +BUILD=build CFLAGS += -std=c2x -ffreestanding -lgcc -isystem include -pipe CFLAGS += -Wall -Wextra -pedantic @@ -13,24 +17,37 @@ CFLAGS += -DPAGE_SIZE=4096 LDFLAGS += -nmagic --no-warn-rwx-segments -nostdlib -H_SRC = $(shell find src include -type f -name "*.h") +# +# Makefile +# -C_SRC = $(shell find src -type f -name "*.c") +ARCH_COMMON += $(ARCH) + +KERNEL=kernel.bin +ISO=os_image.iso + +ARCH_DIRS = $(patsubst %,src/arch/%,$(ARCH_COMMON)) +ARCH_INCLUDES = $(shell find $(ARCH_DIRS) -type d -name "include") +CFLAGS_ARCH = $(patsubst %, -isystem %,$(ARCH_INCLUDES)) + +H_SRC = $(shell find src include -type f -name "*.h" -not -path "src/arch/*") +HA_SRC = $(shell find $(ARCH_DIRS) -type f -name "*.h") + +C_SRC = $(shell find src -type f -name "*.c" -not -path "src/arch/*") C_OBJ = $(patsubst %.c,build/%.o,$(C_SRC)) +CA_SRC = $(shell find $(ARCH_DIRS) -type f -name "*.c") +CA_OBJ = $(patsubst %.c,build/%.o,$(CA_SRC)) -A_SRC = $(shell find src -type f -name "*.S" -not -path "src/arch/*") -A_SRC += $(shell find src/arch/$(ARCH) -type f -name "*.S") +A_SRC = $(shell find $(ARCH_DIRS) -type f -name "*.S") A_OBJ = $(patsubst %.S,build/%.S.o,$(A_SRC)) .PHONY: all clean all: build/$(ISO) -build: build/$(ISO) - clean: @printf "\033[31m RM \033[0m%s\n" build - @rm -rf build/* + @rm -fr ./build $(A_OBJ): build/%.S.o : %.S @mkdir -p $(@D) @@ -42,10 +59,15 @@ $(C_OBJ): build/%.o : %.c @printf "\033[34m CC \033[0m%s\n" $< @$(CC) -c $(CFLAGS) -o $@ $< -build/$(KERNEL): arch/$(ARCH)/linker.ld $(A_OBJ) $(C_OBJ) $(H_SRC) +$(CA_OBJ): build/%.o : %.c + @mkdir -p $(@D) + @printf "\033[34m CC \033[0m%s\n" $< + @$(CC) -c $(CFLAGS) $(CFLAGS_ARCH) -o $@ $< + +build/$(KERNEL): arch/$(ARCH)/linker.ld $(A_OBJ) $(C_OBJ) $(CA_OBJ) $(H_SRC) $(HA_SRC) @mkdir -p $(@D) @printf "\033[32m LD \033[0m%s\n" $@ - @$(LD) $(LDFLAGS) -T arch/$(ARCH)/linker.ld -o build/$(KERNEL) $(A_OBJ) $(C_OBJ) + @$(LD) $(LDFLAGS) -T arch/$(ARCH)/linker.ld -o build/$(KERNEL) $(A_OBJ) $(C_OBJ) $(CA_OBJ) build/$(ISO): build/$(KERNEL) grub.cfg @mkdir -p $(@D) diff --git a/debugger.o b/debugger.o new file mode 100644 index 0000000..9700bf8 Binary files /dev/null and b/debugger.o differ diff --git a/include/fpu.h b/include/fpu.h deleted file mode 100644 index 9f094c2..0000000 --- a/include/fpu.h +++ /dev/null @@ -1,3 +0,0 @@ -#pragma once - -void enable_fpu(void); diff --git a/include/sys/acpi.h b/include/sys/acpi.h new file mode 100644 index 0000000..97eeab9 --- /dev/null +++ b/include/sys/acpi.h @@ -0,0 +1,177 @@ +#pragma once + +#include + +#ifndef ACPI_INTERNAL + #error "Do not include , only use " +#endif + +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; +}; + +void acpi_sys_enable(struct acpi_state *state); +int acpi_sys_shutdown(struct acpi_state *state); diff --git a/include/sys/pci.h b/include/sys/pci.h new file mode 100644 index 0000000..1451434 --- /dev/null +++ b/include/sys/pci.h @@ -0,0 +1,16 @@ +#pragma once + +#ifndef PCI_INTERNAL + #error "do not include , only use " +#endif + +#include +#include + +uint32_t pci_sys_rcfg_d(struct pci_device dev, uint8_t offset); +uint16_t pci_sys_rcfg_w(struct pci_device dev, uint8_t offset); +uint8_t pci_sys_rcfg_b(struct pci_device dev, uint8_t offset); + +void pci_sys_wcfg_d(struct pci_device dev, uint8_t offset, uint32_t dword); +void pci_sys_wcfg_w(struct pci_device dev, uint8_t offset, uint16_t word); +void pci_sys_wcfg_b(struct pci_device dev, uint8_t offset, uint8_t byte); diff --git a/include/memory/physalloc.h b/include/sys/physalloc.h similarity index 89% rename from include/memory/physalloc.h rename to include/sys/physalloc.h index e95e418..6837972 100644 --- a/include/memory/physalloc.h +++ b/include/sys/physalloc.h @@ -1,7 +1,7 @@ #pragma once #ifndef MEMORY_INTERNAL - #error "Do not include headers from , only use " + #error "Do not include headers from , only use " #endif /** diff --git a/include/memory/virtalloc.h b/include/sys/virtalloc.h similarity index 100% rename from include/memory/virtalloc.h rename to include/sys/virtalloc.h diff --git a/src/arch/amd64/acpi.c b/src/acpi.c similarity index 50% rename from src/arch/amd64/acpi.c rename to src/acpi.c index a7f38b6..c6a0cce 100644 --- a/src/arch/amd64/acpi.c +++ b/src/acpi.c @@ -6,174 +6,8 @@ #include #include -#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; -}; +#define ACPI_INTERNAL +#include /* global state, idk a better way rn */ static struct acpi_state state; @@ -284,7 +118,6 @@ static void acpi_handle_table(struct acpi_header *header) { 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; @@ -299,18 +132,14 @@ static void acpi_load_table(uint64_t addr) { } 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))) + 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"); @@ -323,15 +152,11 @@ int acpi_init(void *rootsdp) { } else { panic("invalid acpi rev: %d\n", rsdp->revision); } - kprintf("\n"); - - outb(state.fadt->smi_command_port,state.fadt->acpi_enable); - + acpi_sys_enable(&state); return 0; } int acpi_shutdown(void) { - outw((unsigned int) state.fadt->pm1_a_control_block, state.SLP_TYPb | state.SLP_EN); - return -1; + return acpi_sys_shutdown(&state); } 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 #include #include +#include +#include #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/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 #include #include #include -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/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 #include #include +#include #define MEMORY_INTERNAL -#include -#include +#include +#include #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 #include #include - +#include #include -#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 #include #include +#include +#include #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); diff --git a/src/arch/x86_common/acpi.c b/src/arch/x86_common/acpi.c new file mode 100644 index 0000000..2392185 --- /dev/null +++ b/src/arch/x86_common/acpi.c @@ -0,0 +1,13 @@ +#include + +#define ACPI_INTERNAL +#include + +void acpi_sys_enable(struct acpi_state *state) { + outb(state->fadt->smi_command_port, state->fadt->acpi_enable); +} + +int acpi_sys_shutdown(struct acpi_state *state) { + outw((unsigned int) state->fadt->pm1_a_control_block, state->SLP_TYPb | state->SLP_EN); + return -1; +} diff --git a/src/arch/amd64/drivers/bochs.c b/src/arch/x86_common/drivers/bochs.c similarity index 98% rename from src/arch/amd64/drivers/bochs.c rename to src/arch/x86_common/drivers/bochs.c index b3908f9..8e9ca2b 100644 --- a/src/arch/amd64/drivers/bochs.c +++ b/src/arch/x86_common/drivers/bochs.c @@ -4,8 +4,7 @@ #include #include #include - -#include "../bindings.h" +#include #define INDEX 0x1CE #define DATA 0x1CF diff --git a/src/arch/x86_common/drivers/pci.c b/src/arch/x86_common/drivers/pci.c new file mode 100644 index 0000000..579562a --- /dev/null +++ b/src/arch/x86_common/drivers/pci.c @@ -0,0 +1,57 @@ +#include +#include + +#define PCI_INTERNAL +#include + +#define CONF_ADDR 0xCF8 +#define CONF_DATA 0xCFC + +uint32_t pci_sys_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_sys_rcfg_w(struct pci_device dev, uint8_t offset) { + uint32_t dword = pci_sys_rcfg_d(dev, offset); + return (uint16_t)((dword >> ((offset & 2) * 8)) & 0xFFFF); +} + +uint8_t pci_sys_rcfg_b(struct pci_device dev, uint8_t offset) { + uint32_t dword = pci_sys_rcfg_d(dev, offset); + return (uint8_t)((dword >> ((offset & 3) * 8)) & 0xFF); +} + +void pci_sys_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_sys_wcfg_w(struct pci_device dev, uint8_t offset, uint16_t word) { + size_t shift = (offset & 2) * 8; + uint32_t dword = pci_sys_rcfg_d(dev, offset); + dword &= ~(0xFFFF << shift); + dword |= word << shift; + pci_sys_wcfg_d(dev, offset, dword); +} + +void pci_sys_wcfg_b(struct pci_device dev, uint8_t offset, uint8_t byte) { + size_t shift = (offset & 3) * 8; + uint32_t dword = pci_sys_rcfg_d(dev, offset); + dword &= ~(0xFF << shift); + dword |= byte << shift; + pci_sys_wcfg_d(dev, offset, dword); +} diff --git a/src/arch/amd64/drivers/pic.c b/src/arch/x86_common/drivers/pic.c similarity index 98% rename from src/arch/amd64/drivers/pic.c rename to src/arch/x86_common/drivers/pic.c index be7716f..d911648 100644 --- a/src/arch/amd64/drivers/pic.c +++ b/src/arch/x86_common/drivers/pic.c @@ -1,5 +1,5 @@ -#include "../bindings.h" -#include "pic.h" +#include +#include #define PIC1 0x20 /* IO base address for master PIC */ #define PIC2 0xA0 /* IO base address for slave PIC */ diff --git a/src/arch/amd64/drivers/serial.c b/src/arch/x86_common/drivers/serial.c similarity index 98% rename from src/arch/amd64/drivers/serial.c rename to src/arch/x86_common/drivers/serial.c index b9e351e..255f8fc 100644 --- a/src/arch/amd64/drivers/serial.c +++ b/src/arch/x86_common/drivers/serial.c @@ -1,6 +1,5 @@ #include - -#include "../bindings.h" +#include #define PORT 0x3F8 diff --git a/src/arch/amd64/bindings.h b/src/arch/x86_common/include/bindings.h similarity index 100% rename from src/arch/amd64/bindings.h rename to src/arch/x86_common/include/bindings.h diff --git a/src/arch/amd64/mboot.h b/src/arch/x86_common/include/mboot.h similarity index 100% rename from src/arch/amd64/mboot.h rename to src/arch/x86_common/include/mboot.h diff --git a/src/arch/amd64/drivers/pic.h b/src/arch/x86_common/include/pic.h similarity index 100% rename from src/arch/amd64/drivers/pic.h rename to src/arch/x86_common/include/pic.h diff --git a/src/arch/amd64/mboot.c b/src/arch/x86_common/mboot.c similarity index 99% rename from src/arch/amd64/mboot.c rename to src/arch/x86_common/mboot.c index 9fd7fc2..db82b9d 100644 --- a/src/arch/amd64/mboot.c +++ b/src/arch/x86_common/mboot.c @@ -2,8 +2,7 @@ #include #include #include - -#include "mboot.h" +#include #define MBOOT_HEADER_MAGIC 0x36D76289 diff --git a/src/arch/amd64/drivers/pci.c b/src/drivers/pci.c similarity index 73% rename from src/arch/amd64/drivers/pci.c rename to src/drivers/pci.c index 7aa11c8..6e87b86 100644 --- a/src/arch/amd64/drivers/pci.c +++ b/src/drivers/pci.c @@ -1,12 +1,9 @@ -#include #include -#include #include +#include -#include "../bindings.h" - -#define CONF_ADDR 0xCF8 -#define CONF_DATA 0xCFC +#define PCI_INTERNAL +#include #define TABLE_LEN 16 @@ -20,58 +17,34 @@ struct pci_table_entry { 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; + return pci_sys_rcfg_d(dev, offset); } 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); + return pci_sys_rcfg_w(dev, offset); } -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); +uint8_t pci_rcfg_b(struct pci_device dev, uint8_t offset) { + return pci_sys_rcfg_b(dev, offset); } 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); + pci_sys_wcfg_d(dev, offset, 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); + pci_sys_wcfg_w(dev, offset, word); } 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); + pci_sys_wcfg_b(dev, offset, byte); } +static struct pci_table_entry pci_table[TABLE_LEN]; +static size_t pci_table_next = 0; + + 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", diff --git a/src/kmain.c b/src/kmain.c index b04de49..5621256 100644 --- a/src/kmain.c +++ b/src/kmain.c @@ -1,5 +1,4 @@ #include -#include #include #include #include @@ -7,7 +6,6 @@ #include void kmain(struct boot_info *info) { - enable_fpu(); memory_init(&info->map); pci_init(); screen_init(); diff --git a/src/memory/physalloc.c b/src/memory/physalloc.c index 3115e35..ec1cc0d 100644 --- a/src/memory/physalloc.c +++ b/src/memory/physalloc.c @@ -4,8 +4,8 @@ #include #define MEMORY_INTERNAL -#include -#include +#include +#include extern char kernel_start; extern char kernel_end; diff --git a/src/memory/virtalloc.c b/src/memory/virtalloc.c index b5cc126..c9c6dbf 100644 --- a/src/memory/virtalloc.c +++ b/src/memory/virtalloc.c @@ -6,7 +6,7 @@ #include #define MEMORY_INTERNAL -#include +#include struct addr_node { uintptr_t start;