From 298aaf63f15350e6248d5a96e8c6a63b0ec93e0f Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Thu, 20 May 2021 20:41:24 +0200 Subject: Major restructuring --- kernel/Makefile | 2 +- kernel/drivers/acpi.c | 4 +- kernel/drivers/cpu.c | 2 +- kernel/drivers/fb.c | 79 ----------------- kernel/drivers/gdt.c | 2 +- kernel/drivers/ide.c | 8 +- kernel/drivers/interrupts.c | 6 +- kernel/drivers/mbr.c | 4 +- kernel/drivers/pci.c | 4 +- kernel/drivers/ps2/keyboard.c | 6 +- kernel/drivers/ps2/mouse.c | 6 +- kernel/drivers/ps2/ps2.c | 4 +- kernel/drivers/rtc.c | 6 +- kernel/drivers/rtl8139.c | 8 +- kernel/drivers/serial.c | 4 +- kernel/drivers/timer.c | 8 +- kernel/drivers/vbe.c | 79 +++++++++++++++++ kernel/drivers/vmware.c | 6 +- kernel/features/bus.c | 2 +- kernel/features/fs.c | 6 +- kernel/features/io.c | 30 +++++-- kernel/features/load.c | 2 +- kernel/features/logger.c | 4 +- kernel/features/mm.c | 4 +- kernel/features/net.c | 8 +- kernel/features/proc.c | 6 +- kernel/features/syscall.c | 6 +- kernel/inc/acpi.h | 190 ---------------------------------------- kernel/inc/cpu.h | 178 ------------------------------------- kernel/inc/drivers/acpi.h | 190 ++++++++++++++++++++++++++++++++++++++++ kernel/inc/drivers/cpu.h | 178 +++++++++++++++++++++++++++++++++++++ kernel/inc/drivers/gdt.h | 68 ++++++++++++++ kernel/inc/drivers/ide.h | 75 ++++++++++++++++ kernel/inc/drivers/interrupts.h | 95 ++++++++++++++++++++ kernel/inc/drivers/mbr.h | 28 ++++++ kernel/inc/drivers/pci.h | 102 +++++++++++++++++++++ kernel/inc/drivers/ps2.h | 59 +++++++++++++ kernel/inc/drivers/rtc.h | 21 +++++ kernel/inc/drivers/rtl8139.h | 36 ++++++++ kernel/inc/drivers/serial.h | 14 +++ kernel/inc/drivers/timer.h | 17 ++++ kernel/inc/drivers/vbe.h | 10 +++ kernel/inc/drivers/vmware.h | 12 +++ kernel/inc/fb.h | 10 --- kernel/inc/fpu.h | 8 -- kernel/inc/fs.h | 4 - kernel/inc/gdt.h | 68 -------------- kernel/inc/ide.h | 75 ---------------- kernel/inc/interrupts.h | 95 -------------------- kernel/inc/io.h | 2 +- kernel/inc/mbr.h | 28 ------ kernel/inc/mm.h | 2 +- kernel/inc/pci.h | 102 --------------------- kernel/inc/proc.h | 2 +- kernel/inc/ps2.h | 59 ------------- kernel/inc/rtc.h | 21 ----- kernel/inc/rtl8139.h | 36 -------- kernel/inc/serial.h | 14 --- kernel/inc/timer.h | 17 ---- kernel/inc/vmware.h | 12 --- kernel/main.c | 14 +-- kernel/multiboot.c | 35 ++++---- 62 files changed, 1095 insertions(+), 1088 deletions(-) delete mode 100644 kernel/drivers/fb.c create mode 100644 kernel/drivers/vbe.c delete mode 100644 kernel/inc/acpi.h delete mode 100644 kernel/inc/cpu.h create mode 100644 kernel/inc/drivers/acpi.h create mode 100644 kernel/inc/drivers/cpu.h create mode 100644 kernel/inc/drivers/gdt.h create mode 100644 kernel/inc/drivers/ide.h create mode 100644 kernel/inc/drivers/interrupts.h create mode 100644 kernel/inc/drivers/mbr.h create mode 100644 kernel/inc/drivers/pci.h create mode 100644 kernel/inc/drivers/ps2.h create mode 100644 kernel/inc/drivers/rtc.h create mode 100644 kernel/inc/drivers/rtl8139.h create mode 100644 kernel/inc/drivers/serial.h create mode 100644 kernel/inc/drivers/timer.h create mode 100644 kernel/inc/drivers/vbe.h create mode 100644 kernel/inc/drivers/vmware.h delete mode 100644 kernel/inc/fb.h delete mode 100644 kernel/inc/fpu.h delete mode 100644 kernel/inc/gdt.h delete mode 100644 kernel/inc/ide.h delete mode 100644 kernel/inc/interrupts.h delete mode 100644 kernel/inc/mbr.h delete mode 100644 kernel/inc/pci.h delete mode 100644 kernel/inc/ps2.h delete mode 100644 kernel/inc/rtc.h delete mode 100644 kernel/inc/rtl8139.h delete mode 100644 kernel/inc/serial.h delete mode 100644 kernel/inc/timer.h delete mode 100644 kernel/inc/vmware.h (limited to 'kernel') diff --git a/kernel/Makefile b/kernel/Makefile index 14fc055..853f03f 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -12,7 +12,7 @@ COBJS = entry_asm.o \ drivers/rtc.o \ drivers/mbr.o \ drivers/ide.o \ - drivers/fb.o \ + drivers/vbe.o \ drivers/timer.o \ drivers/vmware.o \ drivers/ps2/ps2.o \ diff --git a/kernel/drivers/acpi.c b/kernel/drivers/acpi.c index b8dad22..6860a43 100644 --- a/kernel/drivers/acpi.c +++ b/kernel/drivers/acpi.c @@ -1,8 +1,8 @@ // MIT License, Copyright (c) 2020 Marvin Borner -#include +#include #include -#include +#include #include #include #include diff --git a/kernel/drivers/cpu.c b/kernel/drivers/cpu.c index 296e73a..8927d1d 100644 --- a/kernel/drivers/cpu.c +++ b/kernel/drivers/cpu.c @@ -2,7 +2,7 @@ // This file is a wrapper around some CPU asm calls #include -#include +#include #include #include #include diff --git a/kernel/drivers/fb.c b/kernel/drivers/fb.c deleted file mode 100644 index 7ef1088..0000000 --- a/kernel/drivers/fb.c +++ /dev/null @@ -1,79 +0,0 @@ -// MIT License, Copyright (c) 2021 Marvin Borner - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define FB_SIZE (vbe->height * vbe->pitch) - -struct vbe_basic { - u8 stuff1[16]; - u16 pitch; - u16 width; - u16 height; - u8 stuff2[18]; - u8 *fb; - u8 stuff3[212]; -}; - -PROTECTED static struct vbe_basic *vbe = NULL; - -static u32 fb_map_buffer(struct page_dir *dir) -{ - assert(vbe); - struct memory_range r = - virtual_alloc(dir, memory_range_around((u32)vbe->fb, FB_SIZE), MEMORY_USER); - return r.base; -} - -static u32 fb_owner = 0; -static res fb_ioctl(u32 request, void *arg1, void *arg2, void *arg3) -{ - UNUSED(arg3); - - switch (request) { - case IOCTL_FB_GET: { - if (!vbe) - return -ENOENT; - - u32 size = MIN(sizeof(*vbe), (u32)arg2); - if (!memory_writable_range(memory_range(arg1, size))) - return -EFAULT; - - if (fb_owner != 0 && proc_from_pid(fb_owner)) - return -EBUSY; - fb_owner = proc_current()->pid; - - u32 fb = fb_map_buffer(proc_current()->page_dir); - - stac(); - memcpy(arg1, vbe, size); - ((struct vbe_basic *)arg1)->fb = (u8 *)fb; - clac(); - - return EOK; - } - default: - return -EINVAL; - } -} - -CLEAR void fb_install(void) -{ - vbe = (void *)multiboot_vbe(); - - struct io_dev *dev = zalloc(sizeof(*dev)); - dev->control = fb_ioctl; - io_add(IO_FRAMEBUFFER, dev); - - // Set framebuffer range used to prevent unwanted writing - physical_set_used(memory_range_around((u32)vbe->fb, FB_SIZE)); -} diff --git a/kernel/drivers/gdt.c b/kernel/drivers/gdt.c index 0e8086f..aa76f2f 100644 --- a/kernel/drivers/gdt.c +++ b/kernel/drivers/gdt.c @@ -2,7 +2,7 @@ #include #include -#include +#include #include #define GDT_MAX_LIMIT 0xffff diff --git a/kernel/drivers/ide.c b/kernel/drivers/ide.c index ea02d9d..9e09860 100644 --- a/kernel/drivers/ide.c +++ b/kernel/drivers/ide.c @@ -1,13 +1,13 @@ // MIT License, Copyright (c) 2020 Marvin Borner #include -#include +#include #include #include -#include -#include +#include +#include #include -#include +#include #include #include diff --git a/kernel/drivers/interrupts.c b/kernel/drivers/interrupts.c index 5b57e56..e9ab0ce 100644 --- a/kernel/drivers/interrupts.c +++ b/kernel/drivers/interrupts.c @@ -2,14 +2,14 @@ // TODO: Remove some magic numbers #include -#include +#include #include -#include +#include #include #include #include #include -#include +#include /** * IDT diff --git a/kernel/drivers/mbr.c b/kernel/drivers/mbr.c index c85061d..c9eba15 100644 --- a/kernel/drivers/mbr.c +++ b/kernel/drivers/mbr.c @@ -2,8 +2,8 @@ #include #include -#include -#include +#include +#include #include #include #include diff --git a/kernel/drivers/pci.c b/kernel/drivers/pci.c index 829f5b9..55806c2 100644 --- a/kernel/drivers/pci.c +++ b/kernel/drivers/pci.c @@ -2,10 +2,10 @@ // Uses parts of the ToAruOS Project, released under the terms of the NCSA // Copyright (C) 2011-2018 K. Lange -#include +#include #include #include -#include +#include CLEAR void pci_write_field(u32 device, int field, u32 value) { diff --git a/kernel/drivers/ps2/keyboard.c b/kernel/drivers/ps2/keyboard.c index be057d7..bddb280 100644 --- a/kernel/drivers/ps2/keyboard.c +++ b/kernel/drivers/ps2/keyboard.c @@ -1,14 +1,14 @@ // MIT License, Copyright (c) 2020 Marvin Borner -#include +#include #include #include -#include +#include #include #include #include #include -#include +#include #include #include #include diff --git a/kernel/drivers/ps2/mouse.c b/kernel/drivers/ps2/mouse.c index abca0a8..2942e34 100644 --- a/kernel/drivers/ps2/mouse.c +++ b/kernel/drivers/ps2/mouse.c @@ -1,14 +1,14 @@ // MIT License, Copyright (c) 2020 Marvin Borner #include -#include +#include #include -#include +#include #include #include #include #include -#include +#include #include #include #include diff --git a/kernel/drivers/ps2/ps2.c b/kernel/drivers/ps2/ps2.c index 076a0bf..f8d849b 100644 --- a/kernel/drivers/ps2/ps2.c +++ b/kernel/drivers/ps2/ps2.c @@ -1,10 +1,10 @@ // MIT License, Copyright (c) 2021 Marvin Borner #include -#include +#include #include #include -#include +#include #define PS2_TIMEOUT 100 diff --git a/kernel/drivers/rtc.c b/kernel/drivers/rtc.c index 2da61da..63d9461 100644 --- a/kernel/drivers/rtc.c +++ b/kernel/drivers/rtc.c @@ -1,12 +1,12 @@ // MIT License, Copyright (c) 2021 Marvin Borner -#include +#include #include #include #include -#include +#include #include -#include +#include static u8 rtc_busy(void) { diff --git a/kernel/drivers/rtl8139.c b/kernel/drivers/rtl8139.c index 753bd6a..fdbc0c8 100644 --- a/kernel/drivers/rtl8139.c +++ b/kernel/drivers/rtl8139.c @@ -2,14 +2,14 @@ // Uses parts of the ToAruOS Project, released under the terms of the NCSA // Copyright (C) 2011-2018 K. Lange -#include +#include #include -#include +#include #include #include -#include +#include #include -#include +#include static int rtl_irq = 0; static u8 mac[6] = { 0 }; diff --git a/kernel/drivers/serial.c b/kernel/drivers/serial.c index 06403f6..95ac02d 100644 --- a/kernel/drivers/serial.c +++ b/kernel/drivers/serial.c @@ -1,9 +1,9 @@ // MIT License, Copyright (c) 2020 Marvin Borner #include -#include +#include #include -#include +#include #include #define PORT 0x3f8 diff --git a/kernel/drivers/timer.c b/kernel/drivers/timer.c index 176b499..c586088 100644 --- a/kernel/drivers/timer.c +++ b/kernel/drivers/timer.c @@ -1,13 +1,13 @@ // MIT License, Copyright (c) 2020 Marvin Borner -#include +#include #include -#include +#include #include #include #include -#include -#include +#include +#include static u32 timer_ticks = 0; PROTECTED static u8 call_scheduler = 0; diff --git a/kernel/drivers/vbe.c b/kernel/drivers/vbe.c new file mode 100644 index 0000000..b384936 --- /dev/null +++ b/kernel/drivers/vbe.c @@ -0,0 +1,79 @@ +// MIT License, Copyright (c) 2021 Marvin Borner + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define FB_SIZE (vbe->height * vbe->pitch) + +struct vbe_basic { + u8 stuff1[16]; + u16 pitch; + u16 width; + u16 height; + u8 stuff2[18]; + u8 *fb; + u8 stuff3[212]; +}; + +PROTECTED static struct vbe_basic *vbe = NULL; + +static u32 vbe_map_buffer(struct page_dir *dir) +{ + assert(vbe); + struct memory_range r = + virtual_alloc(dir, memory_range_around((u32)vbe->fb, FB_SIZE), MEMORY_USER); + return r.base; +} + +static u32 fb_owner = 0; +static res vbe_control(u32 request, void *arg1, void *arg2, void *arg3) +{ + UNUSED(arg3); + + switch (request) { + case IOCTL_FB_GET: { + if (!vbe) + return -ENOENT; + + u32 size = MIN(sizeof(*vbe), (u32)arg2); + if (!memory_writable_range(memory_range(arg1, size))) + return -EFAULT; + + if (fb_owner != 0 && proc_from_pid(fb_owner)) + return -EBUSY; + fb_owner = proc_current()->pid; + + u32 fb = vbe_map_buffer(proc_current()->page_dir); + + stac(); + memcpy(arg1, vbe, size); + ((struct vbe_basic *)arg1)->fb = (u8 *)fb; + clac(); + + return EOK; + } + default: + return -EINVAL; + } +} + +CLEAR void vbe_install(u32 data) +{ + vbe = (void *)data; + + struct io_dev *dev = zalloc(sizeof(*dev)); + dev->control = vbe_control; + io_add(IO_FRAMEBUFFER, dev); + + // Set framebuffer range used to prevent unwanted writing + physical_set_used(memory_range_around((u32)vbe->fb, FB_SIZE)); +} diff --git a/kernel/drivers/vmware.c b/kernel/drivers/vmware.c index c82042f..6f95d02 100644 --- a/kernel/drivers/vmware.c +++ b/kernel/drivers/vmware.c @@ -2,13 +2,13 @@ // VMWare extensions/backdoors for better VM integration #include -#include +#include #include #include #include -#include +#include #include -#include +#include #define VMWARE_CMD_VERSION 0x0a diff --git a/kernel/features/bus.c b/kernel/features/bus.c index 4a09fc0..d58cee3 100644 --- a/kernel/features/bus.c +++ b/kernel/features/bus.c @@ -2,7 +2,7 @@ #include #include -#include +#include #include #include #include diff --git a/kernel/features/fs.c b/kernel/features/fs.c index 7d62c61..c5ebfcd 100644 --- a/kernel/features/fs.c +++ b/kernel/features/fs.c @@ -1,13 +1,13 @@ // MIT License, Copyright (c) 2020 Marvin Borner #include -#include +#include #include #include #include #include -#include -#include +#include +#include #include #include #include diff --git a/kernel/features/io.c b/kernel/features/io.c index bc7977a..4bbc4e0 100644 --- a/kernel/features/io.c +++ b/kernel/features/io.c @@ -2,22 +2,23 @@ #include #include -#include #include -#include -#include +#include +#include +#include +#include +#include +#include #include #include #include #include #include +#include #include -#include #include #include #include -#include -#include struct io_listener { u32 group; @@ -211,6 +212,10 @@ CLEAR void io_install(void) for (u32 i = 0; i < IO_MAX; i++) io_listeners[i] = list_new(); + /** + * Keyboard & mouse detection + */ + ps2_detect(); u8 ps2_keyboard = ps2_keyboard_detect(); @@ -226,8 +231,19 @@ CLEAR void io_install(void) ps2_mouse_install(ps2_mouse); } + /** + * Framebuffer detection + */ + + u32 vbe = multiboot_vbe(); + if (vbe) + vbe_install(vbe); + + /** + * Other devices + */ + timer_install(); logger_install(); - fb_install(); bus_install(); } diff --git a/kernel/features/load.c b/kernel/features/load.c index 645c4d2..1059240 100644 --- a/kernel/features/load.c +++ b/kernel/features/load.c @@ -1,6 +1,6 @@ // MIT License, Copyright (c) 2020 Marvin Borner -#include +#include #include #include #include diff --git a/kernel/features/logger.c b/kernel/features/logger.c index 7db9a82..23efa93 100644 --- a/kernel/features/logger.c +++ b/kernel/features/logger.c @@ -1,13 +1,13 @@ // MIT License, Copyright (c) 2021 Marvin Borner -#include +#include #include #include #include #include #include #include -#include +#include static res logger_write(const void *buf, u32 offset, u32 count) { diff --git a/kernel/features/mm.c b/kernel/features/mm.c index 0f80fda..81645af 100644 --- a/kernel/features/mm.c +++ b/kernel/features/mm.c @@ -2,10 +2,10 @@ // MIT License, Copyright (c) 2021 Marvin Borner #include -#include +#include #include #include -#include +#include #include #include #include diff --git a/kernel/features/net.c b/kernel/features/net.c index 530837b..9e40ea6 100644 --- a/kernel/features/net.c +++ b/kernel/features/net.c @@ -1,18 +1,18 @@ // MIT License, Copyright (c) 2020 Marvin Borner #include -#include +#include #include #include #include #include -#include +#include #include #include -#include +#include #include #include -#include +#include static u32 current_ip_addr = 0; static u32 gateway_addr = 0; diff --git a/kernel/features/proc.c b/kernel/features/proc.c index 4dd90c8..019b0ed 100644 --- a/kernel/features/proc.c +++ b/kernel/features/proc.c @@ -1,10 +1,10 @@ // MIT License, Copyright (c) 2020 Marvin Borner #include -#include +#include #include #include -#include +#include #include #include #include @@ -12,7 +12,7 @@ #include #include #include -#include +#include #define PROC(node) ((struct proc *)node->data) diff --git a/kernel/features/syscall.c b/kernel/features/syscall.c index b66cd2e..b376a13 100644 --- a/kernel/features/syscall.c +++ b/kernel/features/syscall.c @@ -1,9 +1,9 @@ // MIT License, Copyright (c) 2020 Marvin Borner -#include +#include #include #include -#include +#include #include #include #include @@ -14,7 +14,7 @@ #include #include #include -#include +#include static void syscall_handler(struct regs *r) { diff --git a/kernel/inc/acpi.h b/kernel/inc/acpi.h deleted file mode 100644 index 29dcd32..0000000 --- a/kernel/inc/acpi.h +++ /dev/null @@ -1,190 +0,0 @@ -// MIT License, Copyright (c) 2020 Marvin Borner - -#ifndef ACPI_H -#define ACPI_H - -#include - -#define RSDP_MAGIC "RSD PTR " -#define RSDT_MAGIC "RSDT" -#define MADT_MAGIC "APIC" -#define FADT_MAGIC "FACP" -#define HPET_MAGIC "HPET" - -#define HPET_MAX_PERIOD 0x05F5E100 - -struct address_structure { - u8 address_space_id; - u8 register_bit_width; - u8 register_bit_offset; - u8 reserved; - u32 phys; // Actually u64 -}; - -/** - * General headers - */ - -struct sdt_header { - char signature[4]; - u32 length; - u8 revision; - u8 checksum; - char oem_id[6]; - char oem_table_id[8]; - u32 oem_revision; - u32 creator_id; - u32 creator_revision; -}; - -struct sdp_header { - char signature[8]; - u8 checksum; - char oem_id[6]; - u8 revision; -}; - -/** - * RSDT - */ - -struct rsdt { - struct sdt_header header; - u32 sdt_pointer[]; -}; - -/** - * MADT - */ - -struct madt_entry_header { - u8 type; - u8 length; -}; - -struct madt { - struct sdt_header header; - u32 local_address; - u32 flags; - struct madt_entry_header entry; -} PACKED; - -#define MADT_LOCAL_APIC_ENTRY 0 -#define MADT_IO_APIC_ENTRY 1 -#define MADT_INT_SRC_OVERRIDE_ENTRY 2 -#define MADT_NON_MASKABLE_INT_ENTRY 4 // Where's 3? -#define MADT_LOCAL_APIC_OVERRIDE_ENTRY 5 - -struct madt_local_apic_entry { - struct madt_entry_header header; - u8 processor_id; - u8 id; - u32 flags; -} PACKED; - -struct madt_io_apic_entry { - struct madt_entry_header header; - u8 id; - u8 reserved; - u32 address; - u32 global_system_interrupt_base; -} PACKED; - -struct madt_int_src_override_entry { - struct madt_entry_header header; - u8 bus_source; - u8 irq_source; - u32 global_system_interrupt; - u16 flags; -} PACKED; - -struct madt_non_maskable_int_entry { - struct madt_entry_header header; - u8 processor_id; - u16 flags; - u8 lint_number; -} PACKED; - -struct madt_local_apic_override_entry { - struct madt_entry_header header; - u16 reserved; - u64 address; -} PACKED; - -/** - * FADT - */ - -struct fadt { - struct sdt_header header; - // TODO: FADT table (big!) -} PACKED; - -/** - * HPET - */ - -struct hpet { - struct sdt_header header; - u8 hardware_rev_id; - u8 comparator_count : 5; - u8 counter_size : 1; - u8 reserved : 1; - u8 legacy_replacement : 1; - u16 pci_vendor_id; - struct address_structure address; - u8 hpet_number; - u16 minimum_tick; - u8 page_protection; -} PACKED; - -enum hpet_features { hpet_counter_size = 1 << 3, hpet_legacy_replacement_support = 1 << 5 }; -enum hpet_config { hpet_enable = 1 << 0, hpet_legacy_replacement = 1 << 1 }; -enum hpet_timer { - hpet_type = 1 << 1, - hpet_enable_timer = 1 << 2, - hpet_periodic = 1 << 3, - hpet_periodic_support = 1 << 4, - hpet_size = 1 << 5, // 1 if 64 bit - hpet_set_accumulator = 1 << 6, - hpet_force_32 = 1 << 8, // For 64 bit - hpet_apic_routing = 1 << 13, - hpet_fsb = 1 << 14, - hpet_fsb_support = 1 << 15, - /* routing_capability = 1 << 63 */ -}; - -struct hpet_registers { - u32 features; // enum hpet_features - u32 tick_period; - u64 reserved1; - u64 config; // enum hpet_config - u64 reserved2; - u32 int_status; // For timer #n - u32 reserved3; - u8 reserved4[200]; // Why?! - u32 counter; - u32 counter_high; // 0 due to 64 bit - u64 reserved5; - u64 timer0; // enum hpet_timer - u64 timer_comparator0; // In femtoseconds -} PACKED; - -/** - * RSDP - */ - -struct rsdp { - struct sdp_header header; - struct rsdt *rsdt; -}; - -struct madt *madt; -struct fadt *fadt; -struct hpet *hpet; - -void acpi_install(void); -void hpet_install(u32 period); -void madt_install(void); - -#endif diff --git a/kernel/inc/cpu.h b/kernel/inc/cpu.h deleted file mode 100644 index 7ac6074..0000000 --- a/kernel/inc/cpu.h +++ /dev/null @@ -1,178 +0,0 @@ -// MIT License, Copyright (c) 2020 Marvin Borner - -#ifndef CPU_H -#define CPU_H - -#include - -static inline void spinlock(u32 *ptr) -{ - u32 prev; - do - __asm__ volatile("lock xchgl %0,%1" : "=a"(prev) : "m"(*ptr), "a"(1)); - while (prev); -} - -u8 inb(u16 port); -u16 inw(u16 port); -u32 inl(u16 port); - -void outb(u16 port, u8 data); -void outw(u16 port, u16 data); -void outl(u16 port, u32 data); - -void cpu_print(void); -void cpu_enable_features(void); -void fpu_restore(void); - -u32 cr0_get(void); -void cr0_set(u32 cr0); -u32 cr3_get(void); -void cr3_set(u32 cr3); -u32 cr4_get(void); -void cr4_set(u32 cr4); - -void clac(void); -void stac(void); - -void cli(void); -void sti(void); - -struct cpuid { - u32 eax; - u32 ebx; - u32 ecx; - u32 edx; -}; - -enum cpuid_requests { - CPUID_VENDOR_STRING, - CPUID_FEATURES, - CPUID_TLB, - CPUID_SERIAL, - CPUID_EXT_FEATURES = 7, -}; - -enum cpuid_features { - CPUID_FEAT_ECX_SSE3 = 1u << 0, - CPUID_FEAT_ECX_PCLMUL = 1u << 1, - CPUID_FEAT_ECX_DTES64 = 1u << 2, - CPUID_FEAT_ECX_MONITOR = 1u << 3, - CPUID_FEAT_ECX_DS_CPL = 1u << 4, - CPUID_FEAT_ECX_VMX = 1u << 5, - CPUID_FEAT_ECX_SMX = 1u << 6, - CPUID_FEAT_ECX_EST = 1u << 7, - CPUID_FEAT_ECX_TM2 = 1u << 8, - CPUID_FEAT_ECX_SSSE3 = 1u << 9, - CPUID_FEAT_ECX_CID = 1u << 10, - CPUID_FEAT_ECX_FMA = 1u << 12, - CPUID_FEAT_ECX_CX16 = 1u << 13, - CPUID_FEAT_ECX_ETPRD = 1u << 14, - CPUID_FEAT_ECX_PDCM = 1u << 15, - CPUID_FEAT_ECX_PCIDE = 1u << 17, - CPUID_FEAT_ECX_DCA = 1u << 18, - CPUID_FEAT_ECX_SSE4_1 = 1u << 19, - CPUID_FEAT_ECX_SSE4_2 = 1u << 20, - CPUID_FEAT_ECX_x2APIC = 1u << 21, - CPUID_FEAT_ECX_MOVBE = 1u << 22, - CPUID_FEAT_ECX_POPCNT = 1u << 23, - CPUID_FEAT_ECX_AES = 1u << 25, - CPUID_FEAT_ECX_XSAVE = 1u << 26, - CPUID_FEAT_ECX_OSXSAVE = 1u << 27, - CPUID_FEAT_ECX_AVX = 1u << 28, - CPUID_FEAT_ECX_F16C = 1u << 29, - CPUID_FEAT_ECX_RDRND = 1u << 30, - - CPUID_FEAT_EDX_FPU = 1u << 0, - CPUID_FEAT_EDX_VME = 1u << 1, - CPUID_FEAT_EDX_DE = 1u << 2, - CPUID_FEAT_EDX_PSE = 1u << 3, - CPUID_FEAT_EDX_TSC = 1u << 4, - CPUID_FEAT_EDX_MSR = 1u << 5, - CPUID_FEAT_EDX_PAE = 1u << 6, - CPUID_FEAT_EDX_MCE = 1u << 7, - CPUID_FEAT_EDX_CX8 = 1u << 8, - CPUID_FEAT_EDX_APIC = 1u << 9, - CPUID_FEAT_EDX_SEP = 1u << 11, - CPUID_FEAT_EDX_MTRR = 1u << 12, - CPUID_FEAT_EDX_PGE = 1u << 13, - CPUID_FEAT_EDX_MCA = 1u << 14, - CPUID_FEAT_EDX_CMOV = 1u << 15, - CPUID_FEAT_EDX_PAT = 1u << 16, - CPUID_FEAT_EDX_PSE36 = 1u << 17, - CPUID_FEAT_EDX_PSN = 1u << 18, - CPUID_FEAT_EDX_CLF = 1u << 19, - CPUID_FEAT_EDX_DTES = 1u << 21, - CPUID_FEAT_EDX_ACPI = 1u << 22, - CPUID_FEAT_EDX_MMX = 1u << 23, - CPUID_FEAT_EDX_FXSR = 1u << 24, - CPUID_FEAT_EDX_SSE = 1u << 25, - CPUID_FEAT_EDX_SSE2 = 1u << 26, - CPUID_FEAT_EDX_SS = 1u << 27, - CPUID_FEAT_EDX_HTT = 1u << 28, - CPUID_FEAT_EDX_TM1 = 1u << 29, - CPUID_FEAT_EDX_IA64 = 1u << 30, - - CPUID_EXT_FEAT_EBX_FSGSBASE = 1u << 0, - CPUID_EXT_FEAT_EBX_SGX = 1u << 2, - CPUID_EXT_FEAT_EBX_BMI1 = 1u << 3, - CPUID_EXT_FEAT_EBX_HLE = 1u << 4, - CPUID_EXT_FEAT_EBX_AVX2 = 1u << 5, - CPUID_EXT_FEAT_EBX_SMEP = 1u << 7, - CPUID_EXT_FEAT_EBX_BMI2 = 1u << 8, - CPUID_EXT_FEAT_EBX_ERMS = 1u << 9, - CPUID_EXT_FEAT_EBX_INVPCID = 1u << 10, - CPUID_EXT_FEAT_EBX_RTM = 1u << 11, - CPUID_EXT_FEAT_EBX_PQM = 1u << 12, - CPUID_EXT_FEAT_EBX_MPX = 1u << 14, - CPUID_EXT_FEAT_EBX_PQE = 1u << 15, - CPUID_EXT_FEAT_EBX_AVX512F = 1u << 16, - CPUID_EXT_FEAT_EBX_AVX512DQ = 1u << 17, - CPUID_EXT_FEAT_EBX_RDSEED = 1u << 18, - CPUID_EXT_FEAT_EBX_ADX = 1u << 19, - CPUID_EXT_FEAT_EBX_SMAP = 1u << 20, - CPUID_EXT_FEAT_EBX_AVX512IFMA = 1u << 21, - CPUID_EXT_FEAT_EBX_PCOMMIT = 1u << 22, - CPUID_EXT_FEAT_EBX_CLFLUSHOPT = 1u << 23, - CPUID_EXT_FEAT_EBX_CLWB = 1u << 24, - CPUID_EXT_FEAT_EBX_INTELPT = 1u << 25, - CPUID_EXT_FEAT_EBX_AVX512PF = 1u << 26, - CPUID_EXT_FEAT_EBX_AVX512ER = 1u << 27, - CPUID_EXT_FEAT_EBX_AVX512CD = 1u << 28, - CPUID_EXT_FEAT_EBX_SHA = 1u << 29, - CPUID_EXT_FEAT_EBX_AVX512BW = 1u << 30, - - CPUID_EXT_FEAT_ECX_PREFETCHWT1 = 1u << 0, - CPUID_EXT_FEAT_ECX_AVX512VBMI = 1u << 1, - CPUID_EXT_FEAT_ECX_UMIP = 1u << 2, - CPUID_EXT_FEAT_ECX_PKU = 1u << 3, - CPUID_EXT_FEAT_ECX_OSPKE = 1u << 4, - CPUID_EXT_FEAT_ECX_WAITPKG = 1u << 5, - CPUID_EXT_FEAT_ECX_AVX512VBMI2 = 1u << 6, - CPUID_EXT_FEAT_ECX_CETSS = 1u << 7, - CPUID_EXT_FEAT_ECX_GFNI = 1u << 8, - CPUID_EXT_FEAT_ECX_VAES = 1u << 9, - CPUID_EXT_FEAT_ECX_VPCLMULQDQ = 1u << 10, - CPUID_EXT_FEAT_ECX_AVX512VNNI = 1u << 11, - CPUID_EXT_FEAT_ECX_AVX512BITALG = 1u << 12, - CPUID_EXT_FEAT_ECX_AVX512VPOPCNTDQ = 1u << 14, - CPUID_EXT_FEAT_ECX_MAWAU1 = 1u << 17, - CPUID_EXT_FEAT_ECX_MAWAU2 = 1u << 18, - CPUID_EXT_FEAT_ECX_MAWAU3 = 1u << 19, - CPUID_EXT_FEAT_ECX_MAWAU4 = 1u << 20, - CPUID_EXT_FEAT_ECX_MAWAU5 = 1u << 21, - CPUID_EXT_FEAT_ECX_RDPID = 1u << 22, - CPUID_EXT_FEAT_ECX_CLDEMOTE = 1u << 25, - CPUID_EXT_FEAT_ECX_MOVDIRI = 1u << 27, - CPUID_EXT_FEAT_ECX_MOVDIR64B = 1u << 28, - CPUID_EXT_FEAT_ECX_ENQCMD = 1u << 29, - CPUID_EXT_FEAT_ECX_SGXLC = 1u << 30, - - CPUID_EXT_INFO_EDX_NX = 1u << 20, -}; - -extern struct cpuid cpu_features; -extern struct cpuid cpu_extended_information; -extern struct cpuid cpu_extended_features; - -#endif diff --git a/kernel/inc/drivers/acpi.h b/kernel/inc/drivers/acpi.h new file mode 100644 index 0000000..29dcd32 --- /dev/null +++ b/kernel/inc/drivers/acpi.h @@ -0,0 +1,190 @@ +// MIT License, Copyright (c) 2020 Marvin Borner + +#ifndef ACPI_H +#define ACPI_H + +#include + +#define RSDP_MAGIC "RSD PTR " +#define RSDT_MAGIC "RSDT" +#define MADT_MAGIC "APIC" +#define FADT_MAGIC "FACP" +#define HPET_MAGIC "HPET" + +#define HPET_MAX_PERIOD 0x05F5E100 + +struct address_structure { + u8 address_space_id; + u8 register_bit_width; + u8 register_bit_offset; + u8 reserved; + u32 phys; // Actually u64 +}; + +/** + * General headers + */ + +struct sdt_header { + char signature[4]; + u32 length; + u8 revision; + u8 checksum; + char oem_id[6]; + char oem_table_id[8]; + u32 oem_revision; + u32 creator_id; + u32 creator_revision; +}; + +struct sdp_header { + char signature[8]; + u8 checksum; + char oem_id[6]; + u8 revision; +}; + +/** + * RSDT + */ + +struct rsdt { + struct sdt_header header; + u32 sdt_pointer[]; +}; + +/** + * MADT + */ + +struct madt_entry_header { + u8 type; + u8 length; +}; + +struct madt { + struct sdt_header header; + u32 local_address; + u32 flags; + struct madt_entry_header entry; +} PACKED; + +#define MADT_LOCAL_APIC_ENTRY 0 +#define MADT_IO_APIC_ENTRY 1 +#define MADT_INT_SRC_OVERRIDE_ENTRY 2 +#define MADT_NON_MASKABLE_INT_ENTRY 4 // Where's 3? +#define MADT_LOCAL_APIC_OVERRIDE_ENTRY 5 + +struct madt_local_apic_entry { + struct madt_entry_header header; + u8 processor_id; + u8 id; + u32 flags; +} PACKED; + +struct madt_io_apic_entry { + struct madt_entry_header header; + u8 id; + u8 reserved; + u32 address; + u32 global_system_interrupt_base; +} PACKED; + +struct madt_int_src_override_entry { + struct madt_entry_header header; + u8 bus_source; + u8 irq_source; + u32 global_system_interrupt; + u16 flags; +} PACKED; + +struct madt_non_maskable_int_entry { + struct madt_entry_header header; + u8 processor_id; + u16 flags; + u8 lint_number; +} PACKED; + +struct madt_local_apic_override_entry { + struct madt_entry_header header; + u16 reserved; + u64 address; +} PACKED; + +/** + * FADT + */ + +struct fadt { + struct sdt_header header; + // TODO: FADT table (big!) +} PACKED; + +/** + * HPET + */ + +struct hpet { + struct sdt_header header; + u8 hardware_rev_id; + u8 comparator_count : 5; + u8 counter_size : 1; + u8 reserved : 1; + u8 legacy_replacement : 1; + u16 pci_vendor_id; + struct address_structure address; + u8 hpet_number; + u16 minimum_tick; + u8 page_protection; +} PACKED; + +enum hpet_features { hpet_counter_size = 1 << 3, hpet_legacy_replacement_support = 1 << 5 }; +enum hpet_config { hpet_enable = 1 << 0, hpet_legacy_replacement = 1 << 1 }; +enum hpet_timer { + hpet_type = 1 << 1, + hpet_enable_timer = 1 << 2, + hpet_periodic = 1 << 3, + hpet_periodic_support = 1 << 4, + hpet_size = 1 << 5, // 1 if 64 bit + hpet_set_accumulator = 1 << 6, + hpet_force_32 = 1 << 8, // For 64 bit + hpet_apic_routing = 1 << 13, + hpet_fsb = 1 << 14, + hpet_fsb_support = 1 << 15, + /* routing_capability = 1 << 63 */ +}; + +struct hpet_registers { + u32 features; // enum hpet_features + u32 tick_period; + u64 reserved1; + u64 config; // enum hpet_config + u64 reserved2; + u32 int_status; // For timer #n + u32 reserved3; + u8 reserved4[200]; // Why?! + u32 counter; + u32 counter_high; // 0 due to 64 bit + u64 reserved5; + u64 timer0; // enum hpet_timer + u64 timer_comparator0; // In femtoseconds +} PACKED; + +/** + * RSDP + */ + +struct rsdp { + struct sdp_header header; + struct rsdt *rsdt; +}; + +struct madt *madt; +struct fadt *fadt; +struct hpet *hpet; + +void acpi_install(void); +void hpet_install(u32 period); +void madt_install(void); + +#endif diff --git a/kernel/inc/drivers/cpu.h b/kernel/inc/drivers/cpu.h new file mode 100644 index 0000000..7ac6074 --- /dev/null +++ b/kernel/inc/drivers/cpu.h @@ -0,0 +1,178 @@ +// MIT License, Copyright (c) 2020 Marvin Borner + +#ifndef CPU_H +#define CPU_H + +#include + +static inline void spinlock(u32 *ptr) +{ + u32 prev; + do + __asm__ volatile("lock xchgl %0,%1" : "=a"(prev) : "m"(*ptr), "a"(1)); + while (prev); +} + +u8 inb(u16 port); +u16 inw(u16 port); +u32 inl(u16 port); + +void outb(u16 port, u8 data); +void outw(u16 port, u16 data); +void outl(u16 port, u32 data); + +void cpu_print(void); +void cpu_enable_features(void); +void fpu_restore(void); + +u32 cr0_get(void); +void cr0_set(u32 cr0); +u32 cr3_get(void); +void cr3_set(u32 cr3); +u32 cr4_get(void); +void cr4_set(u32 cr4); + +void clac(void); +void stac(void); + +void cli(void); +void sti(void); + +struct cpuid { + u32 eax; + u32 ebx; + u32 ecx; + u32 edx; +}; + +enum cpuid_requests { + CPUID_VENDOR_STRING, + CPUID_FEATURES, + CPUID_TLB, + CPUID_SERIAL, + CPUID_EXT_FEATURES = 7, +}; + +enum cpuid_features { + CPUID_FEAT_ECX_SSE3 = 1u << 0, + CPUID_FEAT_ECX_PCLMUL = 1u << 1, + CPUID_FEAT_ECX_DTES64 = 1u << 2, + CPUID_FEAT_ECX_MONITOR = 1u << 3, + CPUID_FEAT_ECX_DS_CPL = 1u << 4, + CPUID_FEAT_ECX_VMX = 1u << 5, + CPUID_FEAT_ECX_SMX = 1u << 6, + CPUID_FEAT_ECX_EST = 1u << 7, + CPUID_FEAT_ECX_TM2 = 1u << 8, + CPUID_FEAT_ECX_SSSE3 = 1u << 9, + CPUID_FEAT_ECX_CID = 1u << 10, + CPUID_FEAT_ECX_FMA = 1u << 12, + CPUID_FEAT_ECX_CX16 = 1u << 13, + CPUID_FEAT_ECX_ETPRD = 1u << 14, + CPUID_FEAT_ECX_PDCM = 1u << 15, + CPUID_FEAT_ECX_PCIDE = 1u << 17, + CPUID_FEAT_ECX_DCA = 1u << 18, + CPUID_FEAT_ECX_SSE4_1 = 1u << 19, + CPUID_FEAT_ECX_SSE4_2 = 1u << 20, + CPUID_FEAT_ECX_x2APIC = 1u << 21, + CPUID_FEAT_ECX_MOVBE = 1u << 22, + CPUID_FEAT_ECX_POPCNT = 1u << 23, + CPUID_FEAT_ECX_AES = 1u << 25, + CPUID_FEAT_ECX_XSAVE = 1u << 26, + CPUID_FEAT_ECX_OSXSAVE = 1u << 27, + CPUID_FEAT_ECX_AVX = 1u << 28, + CPUID_FEAT_ECX_F16C = 1u << 29, + CPUID_FEAT_ECX_RDRND = 1u << 30, + + CPUID_FEAT_EDX_FPU = 1u << 0, + CPUID_FEAT_EDX_VME = 1u << 1, + CPUID_FEAT_EDX_DE = 1u << 2, + CPUID_FEAT_EDX_PSE = 1u << 3, + CPUID_FEAT_EDX_TSC = 1u << 4, + CPUID_FEAT_EDX_MSR = 1u << 5, + CPUID_FEAT_EDX_PAE = 1u << 6, + CPUID_FEAT_EDX_MCE = 1u << 7, + CPUID_FEAT_EDX_CX8 = 1u << 8, + CPUID_FEAT_EDX_APIC = 1u << 9, + CPUID_FEAT_EDX_SEP = 1u << 11, + CPUID_FEAT_EDX_MTRR = 1u << 12, + CPUID_FEAT_EDX_PGE = 1u << 13, + CPUID_FEAT_EDX_MCA = 1u << 14, + CPUID_FEAT_EDX_CMOV = 1u << 15, + CPUID_FEAT_EDX_PAT = 1u << 16, + CPUID_FEAT_EDX_PSE36 = 1u << 17, + CPUID_FEAT_EDX_PSN = 1u << 18, + CPUID_FEAT_EDX_CLF = 1u << 19, + CPUID_FEAT_EDX_DTES = 1u << 21, + CPUID_FEAT_EDX_ACPI = 1u << 22, + CPUID_FEAT_EDX_MMX = 1u << 23, + CPUID_FEAT_EDX_FXSR = 1u << 24, + CPUID_FEAT_EDX_SSE = 1u << 25, + CPUID_FEAT_EDX_SSE2 = 1u << 26, + CPUID_FEAT_EDX_SS = 1u << 27, + CPUID_FEAT_EDX_HTT = 1u << 28, + CPUID_FEAT_EDX_TM1 = 1u << 29, + CPUID_FEAT_EDX_IA64 = 1u << 30, + + CPUID_EXT_FEAT_EBX_FSGSBASE = 1u << 0, + CPUID_EXT_FEAT_EBX_SGX = 1u << 2, + CPUID_EXT_FEAT_EBX_BMI1 = 1u << 3, + CPUID_EXT_FEAT_EBX_HLE = 1u << 4, + CPUID_EXT_FEAT_EBX_AVX2 = 1u << 5, + CPUID_EXT_FEAT_EBX_SMEP = 1u << 7, + CPUID_EXT_FEAT_EBX_BMI2 = 1u << 8, + CPUID_EXT_FEAT_EBX_ERMS = 1u << 9, + CPUID_EXT_FEAT_EBX_INVPCID = 1u << 10, + CPUID_EXT_FEAT_EBX_RTM = 1u << 11, + CPUID_EXT_FEAT_EBX_PQM = 1u << 12, + CPUID_EXT_FEAT_EBX_MPX = 1u << 14, + CPUID_EXT_FEAT_EBX_PQE = 1u << 15, + CPUID_EXT_FEAT_EBX_AVX512F = 1u << 16, + CPUID_EXT_FEAT_EBX_AVX512DQ = 1u << 17, + CPUID_EXT_FEAT_EBX_RDSEED = 1u << 18, + CPUID_EXT_FEAT_EBX_ADX = 1u << 19, + CPUID_EXT_FEAT_EBX_SMAP = 1u << 20, + CPUID_EXT_FEAT_EBX_AVX512IFMA = 1u << 21, + CPUID_EXT_FEAT_EBX_PCOMMIT = 1u << 22, + CPUID_EXT_FEAT_EBX_CLFLUSHOPT = 1u << 23, + CPUID_EXT_FEAT_EBX_CLWB = 1u << 24, + CPUID_EXT_FEAT_EBX_INTELPT = 1u << 25, + CPUID_EXT_FEAT_EBX_AVX512PF = 1u << 26, + CPUID_EXT_FEAT_EBX_AVX512ER = 1u << 27, + CPUID_EXT_FEAT_EBX_AVX512CD = 1u << 28, + CPUID_EXT_FEAT_EBX_SHA = 1u << 29, + CPUID_EXT_FEAT_EBX_AVX512BW = 1u << 30, + + CPUID_EXT_FEAT_ECX_PREFETCHWT1 = 1u << 0, + CPUID_EXT_FEAT_ECX_AVX512VBMI = 1u << 1, + CPUID_EXT_FEAT_ECX_UMIP = 1u << 2, + CPUID_EXT_FEAT_ECX_PKU = 1u << 3, + CPUID_EXT_FEAT_ECX_OSPKE = 1u << 4, + CPUID_EXT_FEAT_ECX_WAITPKG = 1u << 5, + CPUID_EXT_FEAT_ECX_AVX512VBMI2 = 1u << 6, + CPUID_EXT_FEAT_ECX_CETSS = 1u << 7, + CPUID_EXT_FEAT_ECX_GFNI = 1u << 8, + CPUID_EXT_FEAT_ECX_VAES = 1u << 9, + CPUID_EXT_FEAT_ECX_VPCLMULQDQ = 1u << 10, + CPUID_EXT_FEAT_ECX_AVX512VNNI = 1u << 11, + CPUID_EXT_FEAT_ECX_AVX512BITALG = 1u << 12, + CPUID_EXT_FEAT_ECX_AVX512VPOPCNTDQ = 1u << 14, + CPUID_EXT_FEAT_ECX_MAWAU1 = 1u << 17, + CPUID_EXT_FEAT_ECX_MAWAU2 = 1u << 18, + CPUID_EXT_FEAT_ECX_MAWAU3 = 1u << 19, + CPUID_EXT_FEAT_ECX_MAWAU4 = 1u << 20, + CPUID_EXT_FEAT_ECX_MAWAU5 = 1u << 21, + CPUID_EXT_FEAT_ECX_RDPID = 1u << 22, + CPUID_EXT_FEAT_ECX_CLDEMOTE = 1u << 25, + CPUID_EXT_FEAT_ECX_MOVDIRI = 1u << 27, + CPUID_EXT_FEAT_ECX_MOVDIR64B = 1u << 28, + CPUID_EXT_FEAT_ECX_ENQCMD = 1u << 29, + CPUID_EXT_FEAT_ECX_SGXLC = 1u << 30, + + CPUID_EXT_INFO_EDX_NX = 1u << 20, +}; + +extern struct cpuid cpu_features; +extern struct cpuid cpu_extended_information; +extern struct cpuid cpu_extended_features; + +#endif diff --git a/kernel/inc/drivers/gdt.h b/kernel/inc/drivers/gdt.h new file mode 100644 index 0000000..7b9c65a --- /dev/null +++ b/kernel/inc/drivers/gdt.h @@ -0,0 +1,68 @@ +// MIT License, Copyright (c) 2021 Marvin Borner + +#ifndef GDT_H +#define GDT_H + +#include + +#define GDT_ROOT_CODE_GATE 1 +#define GDT_ROOT_DATA_GATE 2 +#define GDT_USER_CODE_GATE 3 +#define GDT_USER_DATA_GATE 4 +#define GDT_TSS_GATE 5 + +#define GDT_SUPER_CODE_OFFSET (gdt_offset(GDT_ROOT_CODE_GATE)) +#define GDT_SUPER_DATA_OFFSET (gdt_offset(GDT_ROOT_DATA_GATE)) +#define GDT_USER_CODE_OFFSET (gdt_offset(GDT_USER_CODE_GATE) | 3) +#define GDT_USER_DATA_OFFSET (gdt_offset(GDT_USER_DATA_GATE) | 3) +#define GDT_TSS_OFFSET (gdt_offset(GDT_TSS_GATE)) + +struct gdt_entry { + u16 limit_low; + u16 base_low; + u8 base_middle; + u8 access; + u8 granularity; + u8 base_high; +} PACKED; + +struct gdt_ptr { + u16 limit; + void *base; +} PACKED; + +struct tss_entry { + u32 prev_tss; + u32 esp0; + u32 ss0; + u32 esp1; + u32 ss1; + u32 esp2; + u32 ss2; + u32 cr3; + u32 eip; + u32 eflags; + u32 eax; + u32 ecx; + u32 edx; + u32 ebx; + u32 esp; + u32 ebp; + u32 esi; + u32 edi; + u32 es; + u32 cs; + u32 ss; + u32 ds; + u32 fs; + u32 gs; + u32 ldt; + u16 trap; + u16 iomap_base; +} PACKED; + +CONST u8 gdt_offset(u8 gate); +void gdt_install(u32 esp); +void tss_set_stack(u32 esp); + +#endif diff --git a/kernel/inc/drivers/ide.h b/kernel/inc/drivers/ide.h new file mode 100644 index 0000000..dbe0652 --- /dev/null +++ b/kernel/inc/drivers/ide.h @@ -0,0 +1,75 @@ +// MIT License, Copyright (c) 2020 Marvin Borner + +#ifndef IDE_H +#define IDE_H + +#include + +#define BLOCK_SIZE 1024 +#define BLOCK_COUNT 256 // BLOCK_SIZE / sizeof(u32) +#define SECTOR_SIZE 512 +#define SECTOR_COUNT (BLOCK_SIZE / SECTOR_SIZE) + +#define ATA_PRIMARY_IO 0x1f0 +#define ATA_SECONDARY_IO 0x170 + +// From spec +#define ATA_PRIMARY 0x00 +#define ATA_SECONDARY 0x01 +#define ATA_READ 0x00 +#define ATA_WRITE 0x013 +#define ATA_MASTER 0x00 +#define ATA_SLAVE 0x01 +#define ATA_SR_BSY 0x80 +#define ATA_SR_DRDY 0x40 +#define ATA_SR_DF 0x20 +#define ATA_SR_DSC 0x10 +#define ATA_SR_DRQ 0x08 +#define ATA_SR_CORR 0x04 +#define ATA_SR_IDX 0x02 +#define ATA_SR_ERR 0x01 +#define ATA_REG_DATA 0x00 +#define ATA_REG_ERROR 0x01 +#define ATA_REG_FEATURES 0x01 +#define ATA_REG_SECCOUNT0 0x02 +#define ATA_REG_LBA0 0x03 +#define ATA_REG_LBA1 0x04 +#define ATA_REG_LBA2 0x05 +#define ATA_REG_HDDEVSEL 0x06 +#define ATA_REG_COMMAND 0x07 +#define ATA_REG_STATUS 0x07 +#define ATA_REG_SECCOUNT1 0x08 +#define ATA_REG_LBA3 0x09 +#define ATA_REG_LBA4 0x0a +#define ATA_REG_LBA5 0x0b +#define ATA_REG_CONTROL 0x0c +#define ATA_REG_ALTSTATUS 0x0c +#define ATA_REG_DEVADDRESS 0x0d +#define ATA_CMD_READ_PIO 0x20 +#define ATA_CMD_READ_PIO_EXT 0x24 +#define ATA_CMD_READ_DMA 0xc8 +#define ATA_CMD_READ_DMA_EXT 0x25 +#define ATA_CMD_WRITE_PIO 0x30 +#define ATA_CMD_WRITE_PIO_EXT 0x34 +#define ATA_CMD_WRITE_DMA 0xca +#define ATA_CMD_WRITE_DMA_EXT 0x35 +#define ATA_CMD_CACHE_FLUSH 0xe7 +#define ATA_CMD_CACHE_FLUSH_EXT 0xea +#define ATA_CMD_PACKET 0xa0 +#define ATA_CMD_IDENTIFY_PACKET 0xa1 +#define ATA_CMD_IDENTIFY 0xec +#define ATA_IDENT_DEVICETYPE 0 +#define ATA_IDENT_CYLINDERS 2 +#define ATA_IDENT_HEADS 6 +#define ATA_IDENT_SECTORS 12 +#define ATA_IDENT_SERIAL 20 +#define ATA_IDENT_MODEL 54 +#define ATA_IDENT_CAPABILITIES 98 +#define ATA_IDENT_FIELDVALID 106 +#define ATA_IDENT_MAX_LBA 120 +#define ATA_IDENT_COMMANDSETS 164 +#define ATA_IDENT_MAX_LBA_EXT 200 + +void ata_install(void); + +#endif diff --git a/kernel/inc/drivers/interrupts.h b/kernel/inc/drivers/interrupts.h new file mode 100644 index 0000000..7c0c1e7 --- /dev/null +++ b/kernel/inc/drivers/interrupts.h @@ -0,0 +1,95 @@ +// MIT License, Copyright (c) 2020 Marvin Borner + +#ifndef IDT_H +#define IDT_H + +#include + +struct regs { + u32 gs, fs, es, ds; + u32 edi, esi, ebp, esp, ebx, edx, ecx, eax; + u32 int_no, err_code; + u32 eip, cs, eflags, useresp, ss; +}; + +struct idt_entry { + u16 base_low; + u16 sel; // Kernel segment + u8 always0; // Always 0 + u8 flags; + u16 base_high; +} PACKED; + +struct idt_ptr { + u16 limit; + void *base; +} PACKED; + +void idt_set_gate(u8 num, u32 base, u16 sel, u8 flags); + +void irq_install_handler(int irq, void (*handler)(struct regs *r)) NONNULL; +void irq_uninstall_handler(int irq); + +void isr_install_handler(int isr, void (*handler)(struct regs *r)) NONNULL; +void isr_uninstall_handler(int isr); +void isr_panic(struct regs *r) NONNULL; + +void interrupts_install(void); + +// External handlers (ASM) + +extern void isr0(struct regs *r); +extern void isr1(struct regs *r); +extern void isr2(struct regs *r); +extern void isr3(struct regs *r); +extern void isr4(struct regs *r); +extern void isr5(struct regs *r); +extern void isr6(struct regs *r); +extern void isr7(struct regs *r); +extern void isr8(struct regs *r); +extern void isr9(struct regs *r); +extern void isr10(struct regs *r); +extern void isr11(struct regs *r); +extern void isr12(struct regs *r); +extern void isr13(struct regs *r); +extern void isr14(struct regs *r); +extern void isr15(struct regs *r); +extern void isr16(struct regs *r); +extern void isr17(struct regs *r); +extern void isr18(struct regs *r); +extern void isr19(struct regs *r); +extern void isr20(struct regs *r); +extern void isr21(struct regs *r); +extern void isr22(struct regs *r); +extern void isr23(struct regs *r); +extern void isr24(struct regs *r); +extern void isr25(struct regs *r); +extern void isr26(struct regs *r); +extern void isr27(struct regs *r); +extern void isr28(struct regs *r); +extern void isr29(struct regs *r); +extern void isr30(struct regs *r); +extern void isr31(struct regs *r); +extern void isr127(struct regs *r); +extern void isr128(struct regs *r); + +extern void irq0(struct regs *r); +extern void irq1(struct regs *r); +extern void irq2(struct regs *r); +extern void irq3(struct regs *r); +extern void irq4(struct regs *r); +extern void irq5(struct regs *r); +extern void irq6(struct regs *r); +extern void irq7(struct regs *r); +extern void irq8(struct regs *r); +extern void irq9(struct regs *r); +extern void irq10(struct regs *r); +extern void irq11(struct regs *r); +extern void irq12(struct regs *r); +extern void irq13(struct regs *r); +extern void irq14(struct regs *r); +extern void irq15(struct regs *r); +extern void irq127(struct regs *r); +extern void irq128(struct regs *r); + +#endif diff --git a/kernel/inc/drivers/mbr.h b/kernel/inc/drivers/mbr.h new file mode 100644 index 0000000..07f6da0 --- /dev/null +++ b/kernel/inc/drivers/mbr.h @@ -0,0 +1,28 @@ +// MIT License, Copyright (c) 2021 Marvin Borner + +#ifndef MBR_H +#define MBR_H + +#include +#include + +struct mbr_entry { + u8 attributes; + u8 chs_start[3]; + u8 type; + u8 chs_end[3]; + u32 start; + u32 size; +} PACKED; + +struct mbr { + u8 bootstrap[440]; + u32 signature; + u16 reserved; + struct mbr_entry entries[4]; + u16 magic; +} PACKED; + +u8 mbr_load(struct vfs_dev *dev); + +#endif diff --git a/kernel/inc/drivers/pci.h b/kernel/inc/drivers/pci.h new file mode 100644 index 0000000..9429f29 --- /dev/null +++ b/kernel/inc/drivers/pci.h @@ -0,0 +1,102 @@ +// MIT License, Copyright (c) 2020 Marvin Borner + +#ifndef PCI_H +#define PCI_H + +#include + +#define PCI_VENDOR_ID 0x00 // 2 +#define PCI_DEVICE_ID 0x02 // 2 +#define PCI_COMMAND 0x04 // 2 +#define PCI_STATUS 0x06 // 2 +#define PCI_REVISION_ID 0x08 // 1 + +#define PCI_PROG_IF 0x09 // 1 +#define PCI_SUBCLASS 0x0a // 1 +#define PCI_CLASS 0x0b // 1 +#define PCI_CACHE_LINE_SIZE 0x0c // 1 +#define PCI_LATENCY_TIMER 0x0d // 1 +#define PCI_HEADER_TYPE 0x0e // 1 +#define PCI_BIST 0x0f // 1 +#define PCI_BAR0 0x10 // 4 +#define PCI_BAR1 0x14 // 4 +#define PCI_BAR2 0x18 // 4 +#define PCI_BAR3 0x1C // 4 +#define PCI_BAR4 0x20 // 4 +#define PCI_BAR5 0x24 // 4 + +#define PCI_INTERRUPT_LINE 0x3C // 1 + +#define PCI_SECONDARY_BUS 0x19 // 1 + +#define PCI_HEADER_TYPE_DEVICE 0 +#define PCI_HEADER_TYPE_BRIDGE 1 +#define PCI_HEADER_TYPE_CARDBUS 2 + +#define PCI_TYPE_BRIDGE 0x0604 +#define PCI_TYPE_SATA 0x0106 + +#define PCI_ADDRESS_PORT 0xCF8 +#define PCI_VALUE_PORT 0xCFC + +#define PCI_NONE 0xFFFF + +typedef void (*pci_func_t)(u32 device, u16 vendor_id, u16 device_id, void *extra); + +struct pci_device_descriptor { + u32 port_base; + u32 interrupt; + + u8 bus; + u8 slot; + u8 func; + + u16 vendor_id; + u16 device_id; + + u8 class_id; + u8 subclass_id; + u8 interface_id; + + u8 revision; +}; + +static inline u8 pci_extract_bus(u32 device) +{ + return (u8)((device >> 16)); +} + +static inline u8 pci_extract_slot(u32 device) +{ + return (u8)((device >> 8)); +} + +static inline u8 pci_extract_func(u32 device) +{ + return (u8)(device); +} + +static inline u32 pci_get_addr(u32 device, int field) +{ + return 0x80000000 | (u32)(pci_extract_bus(device) << 16) | + (u32)(pci_extract_slot(device) << 11) | (u32)(pci_extract_func(device) << 8) | + ((field)&0xFC); +} + +static inline u32 pci_box_device(int bus, int slot, int func) +{ + return (u32)((bus << 16) | (slot << 8) | func); +} + +u32 pci_read_field(u32 device, int field, int size); +void pci_write_field(u32 device, int field, u32 value); +u16 pci_find_type(u32 dev); +void pci_scan_hit(pci_func_t f, u32 dev, void *extra) NONNULL; +void pci_scan_func(pci_func_t f, int type, int bus, int slot, int func, void *extra) NONNULL; +void pci_scan_slot(pci_func_t f, int type, int bus, int slot, void *extra) NONNULL; +void pci_scan_bus(pci_func_t f, int type, int bus, void *extra) NONNULL; +void pci_scan(pci_func_t f, int type, void *extra) NONNULL; +int pci_get_interrupt(u32 device); +void pci_install(void); + +#endif diff --git a/kernel/inc/drivers/ps2.h b/kernel/inc/drivers/ps2.h new file mode 100644 index 0000000..5db8b57 --- /dev/null +++ b/kernel/inc/drivers/ps2.h @@ -0,0 +1,59 @@ +// MIT License, Copyright (c) 2021 Marvin Borner + +#ifndef PS2_H +#define PS2_H + +#include + +#define PS2_ACK 0xfa +#define PS2_RESEND 0xfe + +#define PS2_TYPE_STANDARD_MOUSE 0x0000 +#define PS2_TYPE_WHEEL_MOUSE 0x0003 +#define PS2_TYPE_BUTTON_MOUSE 0x0004 +#define PS2_TYPE_TRANSLATION_KEYBOARD1 0xab41 +#define PS2_TYPE_TRANSLATION_KEYBOARD2 0xabc1 +#define PS2_TYPE_STANDARD_KEYBOARD 0xab83 + +#define PS2_KEYBOARD(type) \ + ((type) == PS2_TYPE_TRANSLATION_KEYBOARD1 || (type) == PS2_TYPE_TRANSLATION_KEYBOARD2 || \ + (type) == PS2_TYPE_STANDARD_KEYBOARD) +#define PS2_MOUSE(type) \ + ((type) == PS2_TYPE_STANDARD_MOUSE || (type) == PS2_TYPE_WHEEL_MOUSE || \ + (type) == PS2_TYPE_BUTTON_MOUSE) + +struct ps2_status { + u8 in_full : 1; + u8 out_full : 1; + u8 system : 1; + u8 command : 1; + u8 reserved : 2; + u8 error_time : 1; + u8 error_parity : 1; +}; + +struct ps2_config { + u8 first_int : 1; + u8 second_int : 1; + u8 running : 1; + u8 zero1 : 1; + u8 first_clock_disabled : 1; + u8 second_clock_disabled : 1; + u8 first_translation : 1; + u8 zero2 : 1; +}; + +u8 ps2_read_data(void); +u8 ps2_write_data(u8 byte); +u8 ps2_write_device(u8 device, u8 data); + +void ps2_detect(void); +u8 ps2_keyboard_detect(void); +u8 ps2_mouse_detect(void); +void ps2_mouse_enable(u8 device); +void ps2_mouse_install(u8 device); + +void ps2_keyboard_reset(void); +void ps2_keyboard_install(u8 device); + +#endif diff --git a/kernel/inc/drivers/rtc.h b/kernel/inc/drivers/rtc.h new file mode 100644 index 0000000..44a9c9e --- /dev/null +++ b/kernel/inc/drivers/rtc.h @@ -0,0 +1,21 @@ +// MIT License, Copyright (c) 2021 Marvin Borner + +#ifndef RTC_H +#define RTC_H + +#include + +struct rtc { + u8 second; + u8 minute; + u8 hour; + u8 day; + u8 month; + u32 year; +}; + +struct rtc rtc_read(void); +u32 rtc_stamp(void); +CLEAR void rtc_install(void); + +#endif diff --git a/kernel/inc/drivers/rtl8139.h b/kernel/inc/drivers/rtl8139.h new file mode 100644 index 0000000..0d748af --- /dev/null +++ b/kernel/inc/drivers/rtl8139.h @@ -0,0 +1,36 @@ +// MIT License, Copyright (c) 2020 Marvin Borner + +#ifndef RTL8139_H +#define RTL8139_H + +#include + +#define RX_BUF_SIZE 0x2000 + +#define RTL8139_VENDOR_ID 0x10ec +#define RTL8139_DEVICE_ID 0x8139 + +#define RTL_ROK (1 << 0) +#define RTL_TOK (1 << 2) + +#define RTL_PORT_MAC 0x00 +#define RTL_PORT_MAR 0x08 +#define RTL_PORT_TXSTAT 0x10 +#define RTL_PORT_TXBUF 0x20 +#define RTL_PORT_RBSTART 0x30 +#define RTL_PORT_CMD 0x37 +#define RTL_PORT_RXPTR 0x38 +#define RTL_PORT_RXADDR 0x3A +#define RTL_PORT_IMR 0x3C +#define RTL_PORT_ISR 0x3E +#define RTL_PORT_TCR 0x40 +#define RTL_PORT_RCR 0x44 +#define RTL_PORT_RXMISS 0x4C +#define RTL_PORT_CONFIG 0x52 + +int rtl8139_install(void); +int rtl8139_installed(void); +void rtl8139_send_packet(void *data, u32 len) NONNULL; +u8 *rtl8139_get_mac(void); + +#endif diff --git a/kernel/inc/drivers/serial.h b/kernel/inc/drivers/serial.h new file mode 100644 index 0000000..72c9dc1 --- /dev/null +++ b/kernel/inc/drivers/serial.h @@ -0,0 +1,14 @@ +// MIT License, Copyright (c) 2020 Marvin Borner + +#ifndef SERIAL_H +#define SERIAL_H + +#include + +void serial_install(void); +void serial_enable(void); +void serial_disable(void); +void serial_print(const char *data) NONNULL; +void serial_put(char ch); + +#endif diff --git a/kernel/inc/drivers/timer.h b/kernel/inc/drivers/timer.h new file mode 100644 index 0000000..9ff23f8 --- /dev/null +++ b/kernel/inc/drivers/timer.h @@ -0,0 +1,17 @@ +// MIT License, Copyright (c) 2020 Marvin Borner + +#ifndef TIMER_H +#define TIMER_H + +#include +#include + +u32 timer_get(void); +void timer_wait(u32 ticks); +void timer_install(void); +void timer_handler(struct regs *r) NONNULL; + +void scheduler_enable(void); +void scheduler_disable(void); + +#endif diff --git a/kernel/inc/drivers/vbe.h b/kernel/inc/drivers/vbe.h new file mode 100644 index 0000000..5b2275a --- /dev/null +++ b/kernel/inc/drivers/vbe.h @@ -0,0 +1,10 @@ +// MIT License, Copyright (c) 2021 Marvin Borner + +#ifndef VBE_H +#define VBE_H + +#include + +void vbe_install(u32 data) NONNULL; + +#endif diff --git a/kernel/inc/drivers/vmware.h b/kernel/inc/drivers/vmware.h new file mode 100644 index 0000000..243c624 --- /dev/null +++ b/kernel/inc/drivers/vmware.h @@ -0,0 +1,12 @@ +// MIT License, Copyright (c) 2021 Marvin Borner + +#ifndef VMWARE_H +#define VMWARE_H + +#include + +u8 vmware_detect(void); +u8 vmware_mouse_detect(void); +void vmware_mouse_install(u8 device); + +#endif diff --git a/kernel/inc/fb.h b/kernel/inc/fb.h deleted file mode 100644 index b3486e0..0000000 --- a/kernel/inc/fb.h +++ /dev/null @@ -1,10 +0,0 @@ -// MIT License, Copyright (c) 2021 Marvin Borner - -#ifndef FB_H -#define FB_H - -#include - -void fb_install(void) NONNULL; - -#endif diff --git a/kernel/inc/fpu.h b/kernel/inc/fpu.h deleted file mode 100644 index d3ec8d9..0000000 --- a/kernel/inc/fpu.h +++ /dev/null @@ -1,8 +0,0 @@ -// MIT License, Copyright (c) 2020 Marvin Borner - -#ifndef FPU_H -#define FPU_H - -void fpu_install(void); - -#endif diff --git a/kernel/inc/fs.h b/kernel/inc/fs.h index a1cb696..1ea4b21 100644 --- a/kernel/inc/fs.h +++ b/kernel/inc/fs.h @@ -21,8 +21,6 @@ struct vfs_dev { void *data; res (*read)(void *buf, u32 offset, u32 count, struct vfs_dev *dev) NONNULL; res (*write)(const void *buf, u32 offset, u32 count, struct vfs_dev *dev) NONNULL; - res (*ioctl)(u32 request, void *arg1, void *arg2, void *arg3, struct vfs_dev *dev) - ATTR((nonnull(5))); }; /** @@ -40,8 +38,6 @@ struct vfs { struct vfs_dev *dev) NONNULL; res (*write)(const char *path, const void *buf, u32 offset, u32 count, struct vfs_dev *dev) NONNULL; - res (*ioctl)(const char *path, u32 request, void *arg1, void *arg2, void *arg3, - struct vfs_dev *dev) ATTR((nonnull(1, 6))); res (*stat)(const char *path, struct stat *buf, struct vfs_dev *dev) NONNULL; res (*block)(const char *path, u32 func_ptr, struct vfs_dev *dev) NONNULL; res (*perm)(const char *path, enum vfs_perm perm, struct vfs_dev *dev) NONNULL; diff --git a/kernel/inc/gdt.h b/kernel/inc/gdt.h deleted file mode 100644 index 7b9c65a..0000000 --- a/kernel/inc/gdt.h +++ /dev/null @@ -1,68 +0,0 @@ -// MIT License, Copyright (c) 2021 Marvin Borner - -#ifndef GDT_H -#define GDT_H - -#include - -#define GDT_ROOT_CODE_GATE 1 -#define GDT_ROOT_DATA_GATE 2 -#define GDT_USER_CODE_GATE 3 -#define GDT_USER_DATA_GATE 4 -#define GDT_TSS_GATE 5 - -#define GDT_SUPER_CODE_OFFSET (gdt_offset(GDT_ROOT_CODE_GATE)) -#define GDT_SUPER_DATA_OFFSET (gdt_offset(GDT_ROOT_DATA_GATE)) -#define GDT_USER_CODE_OFFSET (gdt_offset(GDT_USER_CODE_GATE) | 3) -#define GDT_USER_DATA_OFFSET (gdt_offset(GDT_USER_DATA_GATE) | 3) -#define GDT_TSS_OFFSET (gdt_offset(GDT_TSS_GATE)) - -struct gdt_entry { - u16 limit_low; - u16 base_low; - u8 base_middle; - u8 access; - u8 granularity; - u8 base_high; -} PACKED; - -struct gdt_ptr { - u16 limit; - void *base; -} PACKED; - -struct tss_entry { - u32 prev_tss; - u32 esp0; - u32 ss0; - u32 esp1; - u32 ss1; - u32 esp2; - u32 ss2; - u32 cr3; - u32 eip; - u32 eflags; - u32 eax; - u32 ecx; - u32 edx; - u32 ebx; - u32 esp; - u32 ebp; - u32 esi; - u32 edi; - u32 es; - u32 cs; - u32 ss; - u32 ds; - u32 fs; - u32 gs; - u32 ldt; - u16 trap; - u16 iomap_base; -} PACKED; - -CONST u8 gdt_offset(u8 gate); -void gdt_install(u32 esp); -void tss_set_stack(u32 esp); - -#endif diff --git a/kernel/inc/ide.h b/kernel/inc/ide.h deleted file mode 100644 index dbe0652..0000000 --- a/kernel/inc/ide.h +++ /dev/null @@ -1,75 +0,0 @@ -// MIT License, Copyright (c) 2020 Marvin Borner - -#ifndef IDE_H -#define IDE_H - -#include - -#define BLOCK_SIZE 1024 -#define BLOCK_COUNT 256 // BLOCK_SIZE / sizeof(u32) -#define SECTOR_SIZE 512 -#define SECTOR_COUNT (BLOCK_SIZE / SECTOR_SIZE) - -#define ATA_PRIMARY_IO 0x1f0 -#define ATA_SECONDARY_IO 0x170 - -// From spec -#define ATA_PRIMARY 0x00 -#define ATA_SECONDARY 0x01 -#define ATA_READ 0x00 -#define ATA_WRITE 0x013 -#define ATA_MASTER 0x00 -#define ATA_SLAVE 0x01 -#define ATA_SR_BSY 0x80 -#define ATA_SR_DRDY 0x40 -#define ATA_SR_DF 0x20 -#define ATA_SR_DSC 0x10 -#define ATA_SR_DRQ 0x08 -#define ATA_SR_CORR 0x04 -#define ATA_SR_IDX 0x02 -#define ATA_SR_ERR 0x01 -#define ATA_REG_DATA 0x00 -#define ATA_REG_ERROR 0x01 -#define ATA_REG_FEATURES 0x01 -#define ATA_REG_SECCOUNT0 0x02 -#define ATA_REG_LBA0 0x03 -#define ATA_REG_LBA1 0x04 -#define ATA_REG_LBA2 0x05 -#define ATA_REG_HDDEVSEL 0x06 -#define ATA_REG_COMMAND 0x07 -#define ATA_REG_STATUS 0x07 -#define ATA_REG_SECCOUNT1 0x08 -#define ATA_REG_LBA3 0x09 -#define ATA_REG_LBA4 0x0a -#define ATA_REG_LBA5 0x0b -#define ATA_REG_CONTROL 0x0c -#define ATA_REG_ALTSTATUS 0x0c -#define ATA_REG_DEVADDRESS 0x0d -#define ATA_CMD_READ_PIO 0x20 -#define ATA_CMD_READ_PIO_EXT 0x24 -#define ATA_CMD_READ_DMA 0xc8 -#define ATA_CMD_READ_DMA_EXT 0x25 -#define ATA_CMD_WRITE_PIO 0x30 -#define ATA_CMD_WRITE_PIO_EXT 0x34 -#define ATA_CMD_WRITE_DMA 0xca -#define ATA_CMD_WRITE_DMA_EXT 0x35 -#define ATA_CMD_CACHE_FLUSH 0xe7 -#define ATA_CMD_CACHE_FLUSH_EXT 0xea -#define ATA_CMD_PACKET 0xa0 -#define ATA_CMD_IDENTIFY_PACKET 0xa1 -#define ATA_CMD_IDENTIFY 0xec -#define ATA_IDENT_DEVICETYPE 0 -#define ATA_IDENT_CYLINDERS 2 -#define ATA_IDENT_HEADS 6 -#define ATA_IDENT_SECTORS 12 -#define ATA_IDENT_SERIAL 20 -#define ATA_IDENT_MODEL 54 -#define ATA_IDENT_CAPABILITIES 98 -#define ATA_IDENT_FIELDVALID 106 -#define ATA_IDENT_MAX_LBA 120 -#define ATA_IDENT_COMMANDSETS 164 -#define ATA_IDENT_MAX_LBA_EXT 200 - -void ata_install(void); - -#endif diff --git a/kernel/inc/interrupts.h b/kernel/inc/interrupts.h deleted file mode 100644 index 7c0c1e7..0000000 --- a/kernel/inc/interrupts.h +++ /dev/null @@ -1,95 +0,0 @@ -// MIT License, Copyright (c) 2020 Marvin Borner - -#ifndef IDT_H -#define IDT_H - -#include - -struct regs { - u32 gs, fs, es, ds; - u32 edi, esi, ebp, esp, ebx, edx, ecx, eax; - u32 int_no, err_code; - u32 eip, cs, eflags, useresp, ss; -}; - -struct idt_entry { - u16 base_low; - u16 sel; // Kernel segment - u8 always0; // Always 0 - u8 flags; - u16 base_high; -} PACKED; - -struct idt_ptr { - u16 limit; - void *base; -} PACKED; - -void idt_set_gate(u8 num, u32 base, u16 sel, u8 flags); - -void irq_install_handler(int irq, void (*handler)(struct regs *r)) NONNULL; -void irq_uninstall_handler(int irq); - -void isr_install_handler(int isr, void (*handler)(struct regs *r)) NONNULL; -void isr_uninstall_handler(int isr); -void isr_panic(struct regs *r) NONNULL; - -void interrupts_install(void); - -// External handlers (ASM) - -extern void isr0(struct regs *r); -extern void isr1(struct regs *r); -extern void isr2(struct regs *r); -extern void isr3(struct regs *r); -extern void isr4(struct regs *r); -extern void isr5(struct regs *r); -extern void isr6(struct regs *r); -extern void isr7(struct regs *r); -extern void isr8(struct regs *r); -extern void isr9(struct regs *r); -extern void isr10(struct regs *r); -extern void isr11(struct regs *r); -extern void isr12(struct regs *r); -extern void isr13(struct regs *r); -extern void isr14(struct regs *r); -extern void isr15(struct regs *r); -extern void isr16(struct regs *r); -extern void isr17(struct regs *r); -extern void isr18(struct regs *r); -extern void isr19(struct regs *r); -extern void isr20(struct regs *r); -extern void isr21(struct regs *r); -extern void isr22(struct regs *r); -extern void isr23(struct regs *r); -extern void isr24(struct regs *r); -extern void isr25(struct regs *r); -extern void isr26(struct regs *r); -extern void isr27(struct regs *r); -extern void isr28(struct regs *r); -extern void isr29(struct regs *r); -extern void isr30(struct regs *r); -extern void isr31(struct regs *r); -extern void isr127(struct regs *r); -extern void isr128(struct regs *r); - -extern void irq0(struct regs *r); -extern void irq1(struct regs *r); -extern void irq2(struct regs *r); -extern void irq3(struct regs *r); -extern void irq4(struct regs *r); -extern void irq5(struct regs *r); -extern void irq6(struct regs *r); -extern void irq7(struct regs *r); -extern void irq8(struct regs *r); -extern void irq9(struct regs *r); -extern void irq10(struct regs *r); -extern void irq11(struct regs *r); -extern void irq12(struct regs *r); -extern void irq13(struct regs *r); -extern void irq14(struct regs *r); -extern void irq15(struct regs *r); -extern void irq127(struct regs *r); -extern void irq128(struct regs *r); - -#endif diff --git a/kernel/inc/io.h b/kernel/inc/io.h index b3e8b9b..275fcae 100644 --- a/kernel/inc/io.h +++ b/kernel/inc/io.h @@ -4,7 +4,7 @@ #define IO_H #include -#include +#include #include #include diff --git a/kernel/inc/mbr.h b/kernel/inc/mbr.h deleted file mode 100644 index 07f6da0..0000000 --- a/kernel/inc/mbr.h +++ /dev/null @@ -1,28 +0,0 @@ -// MIT License, Copyright (c) 2021 Marvin Borner - -#ifndef MBR_H -#define MBR_H - -#include -#include - -struct mbr_entry { - u8 attributes; - u8 chs_start[3]; - u8 type; - u8 chs_end[3]; - u32 start; - u32 size; -} PACKED; - -struct mbr { - u8 bootstrap[440]; - u32 signature; - u16 reserved; - struct mbr_entry entries[4]; - u16 magic; -} PACKED; - -u8 mbr_load(struct vfs_dev *dev); - -#endif diff --git a/kernel/inc/mm.h b/kernel/inc/mm.h index a8116e6..8f6f06a 100644 --- a/kernel/inc/mm.h +++ b/kernel/inc/mm.h @@ -5,7 +5,7 @@ #include #include -#include +#include struct memory_range { u32 base; diff --git a/kernel/inc/pci.h b/kernel/inc/pci.h deleted file mode 100644 index 9429f29..0000000 --- a/kernel/inc/pci.h +++ /dev/null @@ -1,102 +0,0 @@ -// MIT License, Copyright (c) 2020 Marvin Borner - -#ifndef PCI_H -#define PCI_H - -#include - -#define PCI_VENDOR_ID 0x00 // 2 -#define PCI_DEVICE_ID 0x02 // 2 -#define PCI_COMMAND 0x04 // 2 -#define PCI_STATUS 0x06 // 2 -#define PCI_REVISION_ID 0x08 // 1 - -#define PCI_PROG_IF 0x09 // 1 -#define PCI_SUBCLASS 0x0a // 1 -#define PCI_CLASS 0x0b // 1 -#define PCI_CACHE_LINE_SIZE 0x0c // 1 -#define PCI_LATENCY_TIMER 0x0d // 1 -#define PCI_HEADER_TYPE 0x0e // 1 -#define PCI_BIST 0x0f // 1 -#define PCI_BAR0 0x10 // 4 -#define PCI_BAR1 0x14 // 4 -#define PCI_BAR2 0x18 // 4 -#define PCI_BAR3 0x1C // 4 -#define PCI_BAR4 0x20 // 4 -#define PCI_BAR5 0x24 // 4 - -#define PCI_INTERRUPT_LINE 0x3C // 1 - -#define PCI_SECONDARY_BUS 0x19 // 1 - -#define PCI_HEADER_TYPE_DEVICE 0 -#define PCI_HEADER_TYPE_BRIDGE 1 -#define PCI_HEADER_TYPE_CARDBUS 2 - -#define PCI_TYPE_BRIDGE 0x0604 -#define PCI_TYPE_SATA 0x0106 - -#define PCI_ADDRESS_PORT 0xCF8 -#define PCI_VALUE_PORT 0xCFC - -#define PCI_NONE 0xFFFF - -typedef void (*pci_func_t)(u32 device, u16 vendor_id, u16 device_id, void *extra); - -struct pci_device_descriptor { - u32 port_base; - u32 interrupt; - - u8 bus; - u8 slot; - u8 func; - - u16 vendor_id; - u16 device_id; - - u8 class_id; - u8 subclass_id; - u8 interface_id; - - u8 revision; -}; - -static inline u8 pci_extract_bus(u32 device) -{ - return (u8)((device >> 16)); -} - -static inline u8 pci_extract_slot(u32 device) -{ - return (u8)((device >> 8)); -} - -static inline u8 pci_extract_func(u32 device) -{ - return (u8)(device); -} - -static inline u32 pci_get_addr(u32 device, int field) -{ - return 0x80000000 | (u32)(pci_extract_bus(device) << 16) | - (u32)(pci_extract_slot(device) << 11) | (u32)(pci_extract_func(device) << 8) | - ((field)&0xFC); -} - -static inline u32 pci_box_device(int bus, int slot, int func) -{ - return (u32)((bus << 16) | (slot << 8) | func); -} - -u32 pci_read_field(u32 device, int field, int size); -void pci_write_field(u32 device, int field, u32 value); -u16 pci_find_type(u32 dev); -void pci_scan_hit(pci_func_t f, u32 dev, void *extra) NONNULL; -void pci_scan_func(pci_func_t f, int type, int bus, int slot, int func, void *extra) NONNULL; -void pci_scan_slot(pci_func_t f, int type, int bus, int slot, void *extra) NONNULL; -void pci_scan_bus(pci_func_t f, int type, int bus, void *extra) NONNULL; -void pci_scan(pci_func_t f, int type, void *extra) NONNULL; -int pci_get_interrupt(u32 device); -void pci_install(void); - -#endif diff --git a/kernel/inc/proc.h b/kernel/inc/proc.h index 582ca22..1144782 100644 --- a/kernel/inc/proc.h +++ b/kernel/inc/proc.h @@ -4,7 +4,7 @@ #define PROC_H #include -#include +#include #include #include #include diff --git a/kernel/inc/ps2.h b/kernel/inc/ps2.h deleted file mode 100644 index 5db8b57..0000000 --- a/kernel/inc/ps2.h +++ /dev/null @@ -1,59 +0,0 @@ -// MIT License, Copyright (c) 2021 Marvin Borner - -#ifndef PS2_H -#define PS2_H - -#include - -#define PS2_ACK 0xfa -#define PS2_RESEND 0xfe - -#define PS2_TYPE_STANDARD_MOUSE 0x0000 -#define PS2_TYPE_WHEEL_MOUSE 0x0003 -#define PS2_TYPE_BUTTON_MOUSE 0x0004 -#define PS2_TYPE_TRANSLATION_KEYBOARD1 0xab41 -#define PS2_TYPE_TRANSLATION_KEYBOARD2 0xabc1 -#define PS2_TYPE_STANDARD_KEYBOARD 0xab83 - -#define PS2_KEYBOARD(type) \ - ((type) == PS2_TYPE_TRANSLATION_KEYBOARD1 || (type) == PS2_TYPE_TRANSLATION_KEYBOARD2 || \ - (type) == PS2_TYPE_STANDARD_KEYBOARD) -#define PS2_MOUSE(type) \ - ((type) == PS2_TYPE_STANDARD_MOUSE || (type) == PS2_TYPE_WHEEL_MOUSE || \ - (type) == PS2_TYPE_BUTTON_MOUSE) - -struct ps2_status { - u8 in_full : 1; - u8 out_full : 1; - u8 system : 1; - u8 command : 1; - u8 reserved : 2; - u8 error_time : 1; - u8 error_parity : 1; -}; - -struct ps2_config { - u8 first_int : 1; - u8 second_int : 1; - u8 running : 1; - u8 zero1 : 1; - u8 first_clock_disabled : 1; - u8 second_clock_disabled : 1; - u8 first_translation : 1; - u8 zero2 : 1; -}; - -u8 ps2_read_data(void); -u8 ps2_write_data(u8 byte); -u8 ps2_write_device(u8 device, u8 data); - -void ps2_detect(void); -u8 ps2_keyboard_detect(void); -u8 ps2_mouse_detect(void); -void ps2_mouse_enable(u8 device); -void ps2_mouse_install(u8 device); - -void ps2_keyboard_reset(void); -void ps2_keyboard_install(u8 device); - -#endif diff --git a/kernel/inc/rtc.h b/kernel/inc/rtc.h deleted file mode 100644 index 44a9c9e..0000000 --- a/kernel/inc/rtc.h +++ /dev/null @@ -1,21 +0,0 @@ -// MIT License, Copyright (c) 2021 Marvin Borner - -#ifndef RTC_H -#define RTC_H - -#include - -struct rtc { - u8 second; - u8 minute; - u8 hour; - u8 day; - u8 month; - u32 year; -}; - -struct rtc rtc_read(void); -u32 rtc_stamp(void); -CLEAR void rtc_install(void); - -#endif diff --git a/kernel/inc/rtl8139.h b/kernel/inc/rtl8139.h deleted file mode 100644 index 0d748af..0000000 --- a/kernel/inc/rtl8139.h +++ /dev/null @@ -1,36 +0,0 @@ -// MIT License, Copyright (c) 2020 Marvin Borner - -#ifndef RTL8139_H -#define RTL8139_H - -#include - -#define RX_BUF_SIZE 0x2000 - -#define RTL8139_VENDOR_ID 0x10ec -#define RTL8139_DEVICE_ID 0x8139 - -#define RTL_ROK (1 << 0) -#define RTL_TOK (1 << 2) - -#define RTL_PORT_MAC 0x00 -#define RTL_PORT_MAR 0x08 -#define RTL_PORT_TXSTAT 0x10 -#define RTL_PORT_TXBUF 0x20 -#define RTL_PORT_RBSTART 0x30 -#define RTL_PORT_CMD 0x37 -#define RTL_PORT_RXPTR 0x38 -#define RTL_PORT_RXADDR 0x3A -#define RTL_PORT_IMR 0x3C -#define RTL_PORT_ISR 0x3E -#define RTL_PORT_TCR 0x40 -#define RTL_PORT_RCR 0x44 -#define RTL_PORT_RXMISS 0x4C -#define RTL_PORT_CONFIG 0x52 - -int rtl8139_install(void); -int rtl8139_installed(void); -void rtl8139_send_packet(void *data, u32 len) NONNULL; -u8 *rtl8139_get_mac(void); - -#endif diff --git a/kernel/inc/serial.h b/kernel/inc/serial.h deleted file mode 100644 index 72c9dc1..0000000 --- a/kernel/inc/serial.h +++ /dev/null @@ -1,14 +0,0 @@ -// MIT License, Copyright (c) 2020 Marvin Borner - -#ifndef SERIAL_H -#define SERIAL_H - -#include - -void serial_install(void); -void serial_enable(void); -void serial_disable(void); -void serial_print(const char *data) NONNULL; -void serial_put(char ch); - -#endif diff --git a/kernel/inc/timer.h b/kernel/inc/timer.h deleted file mode 100644 index 0712e37..0000000 --- a/kernel/inc/timer.h +++ /dev/null @@ -1,17 +0,0 @@ -// MIT License, Copyright (c) 2020 Marvin Borner - -#ifndef TIMER_H -#define TIMER_H - -#include -#include - -u32 timer_get(void); -void timer_wait(u32 ticks); -void timer_install(void); -void timer_handler(struct regs *r) NONNULL; - -void scheduler_enable(void); -void scheduler_disable(void); - -#endif diff --git a/kernel/inc/vmware.h b/kernel/inc/vmware.h deleted file mode 100644 index 243c624..0000000 --- a/kernel/inc/vmware.h +++ /dev/null @@ -1,12 +0,0 @@ -// MIT License, Copyright (c) 2021 Marvin Borner - -#ifndef VMWARE_H -#define VMWARE_H - -#include - -u8 vmware_detect(void); -u8 vmware_mouse_detect(void); -void vmware_mouse_install(u8 device); - -#endif diff --git a/kernel/main.c b/kernel/main.c index 93bfc29..76d8412 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -1,20 +1,20 @@ // MIT License, Copyright (c) 2020 Marvin Borner -#include +#include #include -#include -#include -#include +#include +#include +#include #include #include #include #include #include #include -#include +#include #include -#include -#include +#include +#include #include PROTECTED extern u32 __stack_chk_guard; diff --git a/kernel/multiboot.c b/kernel/multiboot.c index 4c3e3d0..02a05cf 100644 --- a/kernel/multiboot.c +++ b/kernel/multiboot.c @@ -2,12 +2,13 @@ #include #include +#include #include #include #include -#include PROTECTED static struct multiboot_info *info = NULL; +PROTECTED static u8 vbe_available = 0; PROTECTED static char vbe[256] = { 0 }; CLEAR static void multiboot_parse_cmdline(const char *line) @@ -22,25 +23,11 @@ CLEAR static void multiboot_parse_cmdline(const char *line) start += 3; } } - - assert(info->flags & MULTIBOOT_INFO_VBE_INFO); - memcpy(vbe, (void *)info->vbe_mode_info, sizeof(vbe)); -} - -CLEAR void multiboot_init(u32 magic, u32 addr) -{ - assert(magic == MULTIBOOT_MAGIC); - info = (void *)addr; - - if (info->flags & MULTIBOOT_INFO_CMDLINE) - multiboot_parse_cmdline((const char *)info->cmdline); - - serial_print("Kernel was compiled at " __TIME__ " on " __DATE__ "\n"); } CLEAR u32 multiboot_vbe(void) { - return (u32)vbe; + return vbe_available ? (u32)vbe : 0; } CLEAR void multiboot_mmap(void) @@ -69,3 +56,19 @@ CLEAR void multiboot_mmap(void) physical_set_total(total); } + +CLEAR void multiboot_init(u32 magic, u32 addr) +{ + assert(magic == MULTIBOOT_MAGIC); + info = (void *)addr; + + if (info->flags & MULTIBOOT_INFO_CMDLINE) + multiboot_parse_cmdline((const char *)info->cmdline); + + if (info->flags & MULTIBOOT_INFO_VBE_INFO) { + memcpy(vbe, (void *)info->vbe_mode_info, sizeof(vbe)); + vbe_available = 1; + } + + serial_print("Kernel was compiled at " __TIME__ " on " __DATE__ "\n"); +} -- cgit v1.2.3