diff --git a/kernel/FastLZ/6unpack_mem.c b/kernel/FastLZ/6unpack_mem.c index 57e13f4..a04250b 100644 --- a/kernel/FastLZ/6unpack_mem.c +++ b/kernel/FastLZ/6unpack_mem.c @@ -1,7 +1,6 @@ #include #include -#include "hal/hal.h" -#include "fastlz.h" +#include "FastLZ/fastlz.h" #include "std/string.h" #define SIXPACK_OK 0 diff --git a/kernel/Makefile b/kernel/Makefile index da94b42..fd73288 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -46,7 +46,6 @@ SRCFILES += $(call GRABSRC, \ fs/portlfs \ baseimg \ proc \ - hal/$(ARCH) \ std \ flanterm/src \ flanterm/src/flanterm_backends \ @@ -60,6 +59,9 @@ SRCFILES += $(call GRABSRC, \ diskpart \ FastLZ \ io \ + intr \ + cpu \ + vmm \ ) CFILES := $(call GET_CFILES, $(SRCFILES)) diff --git a/kernel/assert.h b/kernel/assert.h deleted file mode 100644 index 39fa9db..0000000 --- a/kernel/assert.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef ASSERT_H_ -#define ASSERT_H_ - -#include "kprintf.h" -#include "hal/hal.h" - -#define ASSERT(mod, cond, fmt, ...) \ - do { \ - if (!(cond)) { \ - ERR(mod, fmt, ##__VA_ARGS__); \ - hal_hang(); \ - } \ - } while(0) - -#endif // ASSERT_H_ diff --git a/kernel/baseimg/baseimg.c b/kernel/baseimg/baseimg.c index 0e3a077..9888fb8 100644 --- a/kernel/baseimg/baseimg.c +++ b/kernel/baseimg/baseimg.c @@ -1,15 +1,15 @@ #include #include #include -#include "baseimg.h" +#include "baseimg/baseimg.h" #include "bootinfo/bootinfo.h" -#include "kprintf.h" #include "util/util.h" -#include "hal/hal.h" #include "dlmalloc/malloc.h" #include "FastLZ/fastlz.h" #include "FastLZ/6unpack_mem.h" #include "std/string.h" +#include "cpu/hang.h" +#include "kprintf.h" #define BASEIMG_DECOMPRESSED (1024*1024*4) @@ -38,7 +38,7 @@ void baseimg_init(void) { if (baseimg == NULL) { ERR("baseimg", "base.img.6pk not found\n"); - hal_hang(); + cpu_hang(); } else { LOG("baseimg", "base.img.6pk found\n"); LOG("baseimg", "addr = %p, size = %lu\n", baseimg->address, baseimg->size); @@ -55,7 +55,7 @@ void baseimg_init(void) { BASEIMG_DECOMP_ADDR, BASEIMG_DECOMPRESSED); if (res < 0) { ERR("baseimg", "could not uncompress base.img.6pk\n"); - hal_hang(); + cpu_hang(); } BASEIMG_DECOMP_SIZE = res; kprintf("%p %zu\n", BASEIMG_DECOMP_ADDR, BASEIMG_DECOMP_SIZE); diff --git a/kernel/bitmap/bitmap.c b/kernel/bitmap/bitmap.c index 52a41c0..c1ad188 100644 --- a/kernel/bitmap/bitmap.c +++ b/kernel/bitmap/bitmap.c @@ -1,7 +1,7 @@ #include #include #include -#include "bitmap.h" +#include "bitmap/bitmap.h" #include "util/util.h" #include "kprintf.h" diff --git a/kernel/bitmap/bitmap.h b/kernel/bitmap/bitmap.h index f061112..700a12f 100644 --- a/kernel/bitmap/bitmap.h +++ b/kernel/bitmap/bitmap.h @@ -4,8 +4,7 @@ #include #include #include - -#include "hal/hal.h" +#include "vmm/vmm.h" typedef struct { uint8_t *map; @@ -17,7 +16,7 @@ typedef struct { } BitMap; #define BITMAP_BLOCKS_PER_BYTE 8 -#define BITMAP_BLOCK_SIZE HAL_PAGE_SIZE +#define BITMAP_BLOCK_SIZE VMM_PAGE_SIZE #define BITMAP_INVALID_BLOCK ((size_t)-1) void *bitmap_toptr(BitMap *bm, size_t block); diff --git a/kernel/bootinfo/bootinfo.c b/kernel/bootinfo/bootinfo.c index 84dcead..187f58c 100644 --- a/kernel/bootinfo/bootinfo.c +++ b/kernel/bootinfo/bootinfo.c @@ -1,8 +1,8 @@ #include #include #include -#include "bootinfo.h" -#include "hal/hal.h" +#include "bootinfo/bootinfo.h" +#include "cpu/hang.h" BootInfo BOOT_INFO; @@ -26,7 +26,7 @@ DEFINE_REQ(module, MODULE); void bootinfo_init(void) { if (framebuffer_req.response == NULL || framebuffer_req.response->framebuffer_count < 1) { - hal_hang(); + cpu_hang(); } BOOT_INFO.fb = framebuffer_req.response->framebuffers[0]; @@ -35,7 +35,7 @@ void bootinfo_init(void) { struct limine_paging_mode_response *pagingres = paging_req.response; if (pagingres->mode != LIMINE_PAGING_MODE_X86_64_4LVL) { - hal_hang(); + cpu_hang(); } struct limine_hhdm_response *hhdmres = hhdm_req.response; @@ -68,7 +68,7 @@ void bootinfo_init(void) { } } if (BOOT_INFO.smp_bspindex == (uint64_t)-1) { - hal_hang(); + cpu_hang(); } struct limine_rsdp_response *rsdpres = rsdp_req.response; diff --git a/kernel/hal/x86_64/gdt.c b/kernel/cpu/gdt.c similarity index 98% rename from kernel/hal/x86_64/gdt.c rename to kernel/cpu/gdt.c index 2aadce5..0d9ae6f 100644 --- a/kernel/hal/x86_64/gdt.c +++ b/kernel/cpu/gdt.c @@ -1,8 +1,7 @@ #include #include #include "compiler/attr.h" -#include "hal/hal.h" -#include "gdt.h" +#include "cpu/gdt.h" #include "std/string.h" #define GDT_PRESENT 0x80 diff --git a/kernel/hal/x86_64/gdt.h b/kernel/cpu/gdt.h similarity index 76% rename from kernel/hal/x86_64/gdt.h rename to kernel/cpu/gdt.h index 889bdc8..e3dee66 100644 --- a/kernel/hal/x86_64/gdt.h +++ b/kernel/cpu/gdt.h @@ -1,5 +1,8 @@ -#ifndef HAL_GDT_H_ -#define HAL_GDT_H_ +#ifndef CPU_GDT_H_ +#define CPU_GDT_H_ + +#include +#include "compiler/attr.h" #define KCODE 0x08 #define KDATA 0x10 @@ -23,4 +26,4 @@ ALIGNED(16) extern Tss tss; void gdt_init(void); -#endif // HAL_GDT_H_ +#endif // CPU_GDT_H_ diff --git a/kernel/cpu/hang.c b/kernel/cpu/hang.c new file mode 100644 index 0000000..8409891 --- /dev/null +++ b/kernel/cpu/hang.c @@ -0,0 +1,10 @@ +#include "cpu/hang.h" +#include "intr/intr.h" + +void cpu_hang(void) { + intr_disable(); + for(;;) { + asm("hlt"); + } +} + diff --git a/kernel/cpu/hang.h b/kernel/cpu/hang.h new file mode 100644 index 0000000..69b4bd8 --- /dev/null +++ b/kernel/cpu/hang.h @@ -0,0 +1,6 @@ +#ifndef CPU_HANG_H_ +#define CPU_HANG_H_ + +void cpu_hang(void); + +#endif // CPU_HANG_H_ diff --git a/kernel/hal/x86_64/regs.S b/kernel/cpu/regs.S similarity index 100% rename from kernel/hal/x86_64/regs.S rename to kernel/cpu/regs.S diff --git a/kernel/dev/dev.c b/kernel/dev/dev.c index a4c14ac..b5d49db 100644 --- a/kernel/dev/dev.c +++ b/kernel/dev/dev.c @@ -1,14 +1,13 @@ #include #include #include "spinlock/spinlock.h" -#include "dev.h" -#include "hshtb.h" -#include "hal/hal.h" #include "std/string.h" -#include "termdev.h" -#include "ps2kbdev.h" -#include "serialdev.h" -#include "fbdev.h" +#include "dev/dev.h" +#include "dev/termdev.h" +#include "dev/ps2kbdev.h" +#include "dev/serialdev.h" +#include "dev/fbdev.h" +#include "hshtb.h" DevTable DEVTABLE; diff --git a/kernel/dev/fbdev.c b/kernel/dev/fbdev.c index 8cfa676..71bc9ac 100644 --- a/kernel/dev/fbdev.c +++ b/kernel/dev/fbdev.c @@ -1,15 +1,14 @@ #include #include -#include "fbdev.h" -#include "dev.h" +#include "dev/fbdev.h" +#include "dev/dev.h" #include "sysdefs/dev.h" -#include "hshtb.h" #include "spinlock/spinlock.h" #include "util/util.h" -#include "hal/hal.h" #include "bootinfo/bootinfo.h" -#include "errors.h" #include "std/string.h" +#include "errors.h" +#include "hshtb.h" int32_t fbdev_getinfo(struct Dev *dev, uint8_t *buffer, size_t len, uint64_t pid) { (void)dev; (void)pid; diff --git a/kernel/dev/ps2kbdev.c b/kernel/dev/ps2kbdev.c index 62f7bed..7b0cb65 100644 --- a/kernel/dev/ps2kbdev.c +++ b/kernel/dev/ps2kbdev.c @@ -1,16 +1,15 @@ #include #include -#include "kprintf.h" -#include "hal/hal.h" -#include "ps2kbdev.h" -#include "dev.h" -#include "errors.h" +#include "dev/ps2kbdev.h" +#include "dev/dev.h" #include "dlmalloc/malloc.h" #include "util/util.h" -#include "hshtb.h" #include "sysdefs/dev.h" #include "proc/proc.h" #include "io/io.h" +#include "errors.h" +#include "hshtb.h" +#include "kprintf.h" #define KB_CTL_STATUS 0x64 #define KB_DATA_IN_BUF 0x01 diff --git a/kernel/dev/serialdev.c b/kernel/dev/serialdev.c index 7377cd3..d567331 100644 --- a/kernel/dev/serialdev.c +++ b/kernel/dev/serialdev.c @@ -1,14 +1,13 @@ #include #include -#include "dev.h" -#include "serialdev.h" -#include "errors.h" +#include "dev/dev.h" +#include "dev/serialdev.h" #include "util/util.h" -#include "hshtb.h" #include "sysdefs/dev.h" -#include "kprintf.h" -#include "hal/hal.h" #include "io/io.h" +#include "errors.h" +#include "hshtb.h" +#include "kprintf.h" // https://wiki.osdev.org/Serial_Ports diff --git a/kernel/dev/termdev.c b/kernel/dev/termdev.c index cf82dfc..305388d 100644 --- a/kernel/dev/termdev.c +++ b/kernel/dev/termdev.c @@ -1,13 +1,12 @@ #include #include -#include "kprintf.h" -#include "hal/hal.h" -#include "termdev.h" -#include "dev.h" -#include "errors.h" +#include "dev/termdev.h" +#include "dev/dev.h" #include "util/util.h" -#include "hshtb.h" #include "sysdefs/dev.h" +#include "kprintf.h" +#include "hshtb.h" +#include "errors.h" int32_t termdev_putch(struct Dev *dev, uint8_t *buffer, size_t len, uint64_t pid) { (void)dev; (void)pid; diff --git a/kernel/diskpart/diskpart.c b/kernel/diskpart/diskpart.c index 669697a..28489ae 100644 --- a/kernel/diskpart/diskpart.c +++ b/kernel/diskpart/diskpart.c @@ -1,7 +1,7 @@ #include #include #include "storedev/storedev.h" -#include "mbr.h" +#include "diskpart/mbr.h" #include "util/util.h" #include "kprintf.h" diff --git a/kernel/diskpart/mbr.c b/kernel/diskpart/mbr.c index b880624..a07a1de 100644 --- a/kernel/diskpart/mbr.c +++ b/kernel/diskpart/mbr.c @@ -2,15 +2,14 @@ #include #include #include "storedev/storedev.h" -#include "mbr.h" -#include "diskpart.h" +#include "diskpart/mbr.h" +#include "diskpart/diskpart.h" #include "compiler/attr.h" -#include "errors.h" -#include "kprintf.h" -#include "hal/hal.h" #include "util/util.h" #include "dev/dev.h" #include "std/string.h" +#include "errors.h" +#include "kprintf.h" typedef struct { uint8_t driveattrs; diff --git a/kernel/dlmalloc/dlmalloc_port.inc b/kernel/dlmalloc/dlmalloc_port.inc index 3fd1fc5..857f629 100644 --- a/kernel/dlmalloc/dlmalloc_port.inc +++ b/kernel/dlmalloc/dlmalloc_port.inc @@ -1,15 +1,16 @@ // Config #include -#include "hal/hal.h" #include "spinlock/spinlock.h" -#include "kprintf.h" #include "bitmap/bitmap.h" #include "util/util.h" #include "pmm/pmm.h" -#include "malloc.h" +#include "dlmalloc/malloc.h" #include "bootinfo/bootinfo.h" #include "std/string.h" +#include "vmm/vmm.h" +#include "cpu/hang.h" +#include "kprintf.h" #define USE_DL_PREFIX 1 #define LACKS_SYS_TYPES_H 1 @@ -27,7 +28,7 @@ #define ABORT \ do { \ ERR("dlmalloc", "Aborting..."); \ - hal_hang(); \ + cpu_hang(); \ } while(0) #define MALLOC_FAILURE_ACTION #define HAVE_MORECORE 1 diff --git a/kernel/fs/portlfs/portlfs.c b/kernel/fs/portlfs/portlfs.c index e75e4be..6090450 100644 --- a/kernel/fs/portlfs/portlfs.c +++ b/kernel/fs/portlfs/portlfs.c @@ -2,11 +2,10 @@ #include #include "fs/littlefs/lfs.h" #include "vfs/vfs.h" +#include "dlmalloc/malloc.h" +#include "std/string.h" #include "errors.h" #include "kprintf.h" -#include "dlmalloc/malloc.h" -#include "hal/hal.h" -#include "std/string.h" int32_t littlefs_cleanup(struct VfsMountPoint *vmp) { dlfree((void *)vmp->fs.littlefs.instance.cfg); diff --git a/kernel/hal/hal.h b/kernel/hal/hal.h deleted file mode 100644 index b589717..0000000 --- a/kernel/hal/hal.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef KERNEL_HAL_HAL_H_ -#define KERNEL_HAL_HAL_H_ - -#include -#include - -__attribute__((noreturn)) void hal_hang(void); - -void hal_init(void); -void hal_intr_disable(void); -void hal_intr_enable(void); -void hal_wait(uint32_t ms); -int32_t hal_randnum(void); - -#define HAL_PAGE_SIZE 0x1000 -#include "x86_64/vmm.h" -#include "x86_64/switch.h" -#include "x86_64/paging.h" -#include "x86_64/intr.h" -#include "x86_64/gdt.h" - -#endif // KERNEL_HAL_HAL_H_ diff --git a/kernel/hal/x86_64/hal.c b/kernel/hal/x86_64/hal.c deleted file mode 100644 index e08a4ba..0000000 --- a/kernel/hal/x86_64/hal.c +++ /dev/null @@ -1,27 +0,0 @@ -#include -#include -#include "hal/hal.h" -#include "kprintf.h" -#include "gdt.h" -#include "intr.h" -#include "pic.h" -#include "pit.h" - -void hal_init(void) { - gdt_init(); - intr_init(); - pic_init(); - pit_init(); - hal_intr_disable(); -} - -__attribute__((noreturn)) void hal_hang(void) { - for(;;) { - asm("hlt"); - } -} - -void hal_wait(uint32_t ms) { - pit_wait(ms); -} - diff --git a/kernel/hal/x86_64/paging.S b/kernel/hal/x86_64/paging.S deleted file mode 100644 index 9e1a9de..0000000 --- a/kernel/hal/x86_64/paging.S +++ /dev/null @@ -1,4 +0,0 @@ -.global hal_loadpd -hal_loadpd: - mov %rdi, %cr3 - retq diff --git a/kernel/hal/x86_64/paging.h b/kernel/hal/x86_64/paging.h deleted file mode 100644 index e50679b..0000000 --- a/kernel/hal/x86_64/paging.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef HAL_PAGING_H_ -#define HAL_PAGING_H_ - -void hal_loadpd(PgTable *cr3); - -#endif // HAL_PAGING_H_ diff --git a/kernel/hal/x86_64/pic.c b/kernel/hal/x86_64/pic.c deleted file mode 100644 index 5275395..0000000 --- a/kernel/hal/x86_64/pic.c +++ /dev/null @@ -1,22 +0,0 @@ -#include -#include -#include "pic.h" -#include "io/io.h" -#include "intr.h" - -void pic_init(void) { - io_out8(PIC1_CMD, ICW1_INIT | ICW1_ICW4); - io_out8(PIC2_CMD, ICW1_INIT | ICW1_ICW4); - - io_out8(PIC1_DATA, INTR_IRQBASE); - io_out8(PIC2_DATA, INTR_IRQBASE+8); - - io_out8(PIC1_DATA, 2); - io_out8(PIC2_DATA, 2); - - io_out8(PIC1_DATA, ICW4_8086); - io_out8(PIC2_DATA, ICW4_8086); - - io_out8(PIC1_DATA, 0); - io_out8(PIC2_DATA, 0); -} diff --git a/kernel/hal/x86_64/pic.h b/kernel/hal/x86_64/pic.h deleted file mode 100644 index fb08aa8..0000000 --- a/kernel/hal/x86_64/pic.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef HAL_PIC_H_ -#define HAL_PIC_H_ - -#define PIC1_CMD 0x0020 -#define PIC1_DATA 0x0021 -#define PIC2_CMD 0x00A0 -#define PIC2_DATA 0x00A1 - -#define PIC_EOI 0x20 - -#define ICW1_ICW4 0x01 -#define ICW1_SINGLE 0x02 -#define ICW1_ADI 0x04 -#define ICW1_LTIM 0x08 -#define ICW1_INIT 0x10 - -#define ICW4_8086 0x01 -#define ICW4_AUTO 0x02 -#define ICW4_BUFSLAVE 0x04 -#define ICW4_BUFMASTER 0x0C -#define ICW4_SFNM 0x10 - -void pic_init(void); - -#endif // HAL_PIC_H_ diff --git a/kernel/hal/x86_64/pit.h b/kernel/hal/x86_64/pit.h deleted file mode 100644 index a04ad55..0000000 --- a/kernel/hal/x86_64/pit.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef HAL_PIT_H_ -#define HAL_PIT_H_ - -#include - -extern volatile uint32_t PIT_TICKS; - -void pit_init(void); -void pit_wait(uint32_t ms); - -#endif // HAL_PIT_H_ diff --git a/kernel/hal/x86_64/switch.h b/kernel/hal/x86_64/switch.h deleted file mode 100644 index 5112a87..0000000 --- a/kernel/hal/x86_64/switch.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef HAL_SWITCH_H_ -#define HAL_SWITCH_H_ - -extern void hal_switchproc(void *newsp, PgTable *cr3); - -#endif // HAL_SWITCH_H_ diff --git a/kernel/hal/x86_64/vmm.c b/kernel/hal/x86_64/vmm.c deleted file mode 100644 index e1b31c3..0000000 --- a/kernel/hal/x86_64/vmm.c +++ /dev/null @@ -1,129 +0,0 @@ -#include -#include -#include "vmm.h" -#include "hal/hal.h" -#include "bootinfo/bootinfo.h" -#include "pmm/pmm.h" -#include "paging.h" -#include "proc/proc.h" -#include "kprintf.h" -#include "spinlock/spinlock.h" -#include "std/string.h" - -uint64_t KERNEL_CR3 = 0; -SpinLock spinlock; - -uint64_t hal_vmm_current_cr3(void) { - uint64_t cr3; - asm volatile("mov %%cr3, %0" : "=r"(cr3)); - return cr3; -} - -PgIndex hal_vmm_pageindex(uint64_t vaddr) { - PgIndex ret; - - ret.pml4 = (vaddr >> 39) & 0x1ff; - ret.pml3 = (vaddr >> 30) & 0x1ff; - ret.pml2 = (vaddr >> 21) & 0x1ff; - ret.pml1 = (vaddr >> 12) & 0x1ff; - - return ret; -} - -uint64_t *hal_vmm_nexttable(uint64_t *table, uint64_t ent, bool alloc) { - uint64_t entry = table[ent]; - uint64_t phys; - if (entry & HAL_PG_PRESENT) { - phys = entry & ~0xFFFULL; - } else { - if (!alloc) { - return NULL; - } - - uint8_t *newphys = pmm_alloc(1); - phys = (uint64_t)newphys; - memset(VIRT(phys), 0, HAL_PAGE_SIZE); - table[ent] = phys | HAL_PG_USER | HAL_PG_RW | HAL_PG_PRESENT; - } - return (uint64_t *)((uint8_t *)VIRT(phys)); -} - -void hal_vmm_map_page(uint64_t cr3phys, uint64_t virtaddr, uint64_t physaddr, uint32_t flags) { - uint64_t *pml4 = (uint64_t *)VIRT(cr3phys); - PgIndex pi = hal_vmm_pageindex(virtaddr); - - uint64_t *pml3 = hal_vmm_nexttable(pml4, pi.pml4, true); - uint64_t *pml2 = hal_vmm_nexttable(pml3, pi.pml3, true); - uint64_t *pml1 = hal_vmm_nexttable(pml2, pi.pml2, true); - uint64_t *pte = &pml1[pi.pml1]; - - *pte = (physaddr & ~0xFFFULL) | ((uint64_t)flags & 0x7ULL); -} - -void hal_vmm_unmap_page(uint64_t cr3phys, uint64_t virtaddr) { - uint64_t *pml4 = (uint64_t *)VIRT(cr3phys); - PgIndex pi = hal_vmm_pageindex(virtaddr); - - uint64_t *pml3 = hal_vmm_nexttable(pml4, pi.pml4, false); - uint64_t *pml2 = hal_vmm_nexttable(pml3, pi.pml3, false); - uint64_t *pml1 = hal_vmm_nexttable(pml2, pi.pml2, false); - uint64_t *pte = &pml1[pi.pml1]; - - *pte &= ~HAL_PG_PRESENT; -} - -void hal_vmm_map_range(uint64_t cr3phys, void *virtstart, void *physstart, size_t size, uint32_t flags) { - if (size % HAL_PAGE_SIZE != 0 || (uint64_t)virtstart % HAL_PAGE_SIZE != 0 || (uint64_t)physstart % HAL_PAGE_SIZE != 0) { - return; - } - - spinlock_acquire(&spinlock); - uint8_t *vaddr = (uint8_t *)virtstart; - uint8_t *paddr = (uint8_t *)physstart; - uint8_t *end = (uint8_t *)virtstart + size; - for (; vaddr < end; vaddr += HAL_PAGE_SIZE, paddr += HAL_PAGE_SIZE) { - hal_vmm_map_page(cr3phys, (uint64_t)vaddr, (uint64_t)paddr, flags); - } - spinlock_release(&spinlock); -} - -void hal_vmm_unmap_range(uint64_t cr3phys, void *virtstart, void *physstart, size_t size) { - if (size % HAL_PAGE_SIZE != 0 || (uint64_t)virtstart % HAL_PAGE_SIZE != 0 || (uint64_t)physstart % HAL_PAGE_SIZE != 0) { - return; - } - - spinlock_acquire(&spinlock); - uint8_t *vaddr = (uint8_t *)virtstart; - uint8_t *end = vaddr + size; - - for (; vaddr < end; vaddr += HAL_PAGE_SIZE) { - hal_vmm_unmap_page(cr3phys, (uint64_t)vaddr); - } - spinlock_release(&spinlock); -} - -void hal_vmm_map_kern(uint64_t targetcr3) { - uint64_t *kcr3 = (uint64_t *)VIRT(KERNEL_CR3); - uint64_t *cr3 = (uint64_t *)VIRT(targetcr3); - for (size_t i = 0; i < 512; i++) { - cr3[i] = kcr3[i]; - } -} - -uint64_t hal_vmm_userproc_pml4_phys(void) { - uint8_t *cr3phys = pmm_alloc(1); - uint64_t phys = (uint64_t)cr3phys; - memset(VIRT(phys), 0, HAL_PAGE_SIZE); - - uint64_t *kcr3 = (uint64_t *)VIRT(KERNEL_CR3); - uint64_t *pml4 = (uint64_t *)VIRT(phys); - for (size_t i = 256; i < 512; i++) { - pml4[i] = kcr3[i]; - } - return phys; -} - -void hal_vmm_init(void) { - spinlock_init(&spinlock); - KERNEL_CR3 = hal_vmm_current_cr3(); -} diff --git a/kernel/hshtb.h b/kernel/hshtb.h index 2db9394..a8d108d 100644 --- a/kernel/hshtb.h +++ b/kernel/hshtb.h @@ -4,7 +4,6 @@ #include #include #include -#include "hal/hal.h" #include "util/util.h" #include "std/string.h" diff --git a/kernel/hal/x86_64/intr.c b/kernel/intr/intr.c similarity index 93% rename from kernel/hal/x86_64/intr.c rename to kernel/intr/intr.c index 80ad299..e699b8b 100644 --- a/kernel/hal/x86_64/intr.c +++ b/kernel/intr/intr.c @@ -1,21 +1,21 @@ #include #include #include -#include "intr.h" +#include "intr/intr.h" +#include "intr/pic.h" +#include "intr/pit.h" #include "io/io.h" -#include "gdt.h" -#include "hal/hal.h" -#include "kprintf.h" +#include "cpu/gdt.h" #include "compiler/attr.h" -#include "pic.h" -#include "pit.h" #include "proc/proc.h" #include "syscall/syscall.h" -#include "errors.h" #include "ipc/pipe/pipe.h" #include "rbuf/rbuf.h" #include "dlmalloc/malloc.h" #include "util/util.h" +#include "cpu/hang.h" +#include "kprintf.h" +#include "errors.h" typedef struct IntrHandler { struct IntrHandler *next; @@ -45,11 +45,11 @@ void backtrace(BackTraceFrame *bt) { } } -void hal_intr_disable(void) { +void intr_disable(void) { asm volatile("cli"); } -void hal_intr_enable(void) { +void intr_enable(void) { asm volatile("sti"); } @@ -160,6 +160,9 @@ void intr_init(void) { idt_setentry(0x80, (uint64_t)&intr_vec128, 0xEE); idt_init(); + intr_pic_init(); + intr_pit_init(); + intr_disable(); } void intr_dumpframe(IntrStackFrame *frame) { @@ -185,7 +188,7 @@ void intr_dumpframe(IntrStackFrame *frame) { ); } -void hal_syscalldispatch(IntrStackFrame *frame) { +void intr_syscalldispatch(IntrStackFrame *frame) { uint64_t sysnum = frame->regs.rax; if (sysnum < SYSCALLS_MAX) { SyscallFn fn = SYSCALL_TABLE[sysnum]; @@ -194,7 +197,7 @@ void hal_syscalldispatch(IntrStackFrame *frame) { return; } uint64_t calling_proc_pid = PROCS.current->pid; - hal_intr_enable(); + intr_enable(); int32_t ret = fn(frame, calling_proc_pid, frame->regs.rdi, frame->regs.rsi, frame->regs.rdx, frame->regs.r10, frame->regs.r8, frame->regs.r9); @@ -205,11 +208,6 @@ void hal_syscalldispatch(IntrStackFrame *frame) { } } -void intr_eoi() { - io_out8(PIC2_CMD, PIC_EOI); - io_out8(PIC1_CMD, PIC_EOI); -} - void intr_handleintr(IntrStackFrame *frame) { if (frame->trapnum <= 31) { kprintf("ERROR %s, 0x%lX\n", exceptions[frame->trapnum], frame->errnum); @@ -221,13 +219,13 @@ void intr_handleintr(IntrStackFrame *frame) { proc_sched((void *)frame); } else { kprintf("Kernel error :(\n"); - hal_hang(); + cpu_hang(); } } else if (frame->trapnum >= 32 && frame->trapnum <= 47) { switch (frame->trapnum) { case INTR_IRQBASE+0: PIT_TICKS++; - intr_eoi(); + intr_pic_eoi(); proc_sched((void *)frame); break; default: @@ -237,11 +235,11 @@ void intr_handleintr(IntrStackFrame *frame) { ih->fn(); } } - intr_eoi(); + intr_pic_eoi(); break; } } else if (frame->trapnum == 0x80) { - hal_syscalldispatch(frame); + intr_syscalldispatch(frame); } } diff --git a/kernel/hal/x86_64/intr.h b/kernel/intr/intr.h similarity index 80% rename from kernel/hal/x86_64/intr.h rename to kernel/intr/intr.h index cff9dc5..5445d6c 100644 --- a/kernel/hal/x86_64/intr.h +++ b/kernel/intr/intr.h @@ -1,10 +1,11 @@ -#ifndef HAL_INTR_H_ -#define HAL_INTR_H_ +#ifndef INTR_INTR_H_ +#define INTR_INTR_H_ + +#include +#include "compiler/attr.h" #define INTR_IRQBASE 0x20 -#include "compiler/attr.h" - typedef struct { uint64_t r15; uint64_t r14; @@ -34,6 +35,8 @@ typedef struct { } PACKED IntrStackFrame; void intr_attchhandler(void (*fn)(void), int irq); +void intr_disable(void); +void intr_enable(void); void intr_init(void); -#endif // HAL_INTR_H_ +#endif // INTR_INTR_H_ diff --git a/kernel/hal/x86_64/intr0.S b/kernel/intr/intr0.S similarity index 99% rename from kernel/hal/x86_64/intr0.S rename to kernel/intr/intr0.S index 62a6c50..9370e05 100644 --- a/kernel/hal/x86_64/intr0.S +++ b/kernel/intr/intr0.S @@ -1,4 +1,4 @@ -#include "regs.S" +#include "cpu/regs.S" .extern intr_handleintr diff --git a/kernel/intr/pic.c b/kernel/intr/pic.c new file mode 100644 index 0000000..7465df4 --- /dev/null +++ b/kernel/intr/pic.c @@ -0,0 +1,46 @@ +#include +#include +#include "io/io.h" +#include "intr/pic.h" +#include "intr/intr.h" + +#define PIC1_CMD 0x0020 +#define PIC1_DATA 0x0021 +#define PIC2_CMD 0x00A0 +#define PIC2_DATA 0x00A1 + +#define PIC_EOI 0x20 + +#define ICW1_ICW4 0x01 +#define ICW1_SINGLE 0x02 +#define ICW1_ADI 0x04 +#define ICW1_LTIM 0x08 +#define ICW1_INIT 0x10 + +#define ICW4_8086 0x01 +#define ICW4_AUTO 0x02 +#define ICW4_BUFSLAVE 0x04 +#define ICW4_BUFMASTER 0x0C +#define ICW4_SFNM 0x10 + +void intr_pic_init(void) { + io_out8(PIC1_CMD, ICW1_INIT | ICW1_ICW4); + io_out8(PIC2_CMD, ICW1_INIT | ICW1_ICW4); + + io_out8(PIC1_DATA, INTR_IRQBASE); + io_out8(PIC2_DATA, INTR_IRQBASE+8); + + io_out8(PIC1_DATA, 2); + io_out8(PIC2_DATA, 2); + + io_out8(PIC1_DATA, ICW4_8086); + io_out8(PIC2_DATA, ICW4_8086); + + io_out8(PIC1_DATA, 0); + io_out8(PIC2_DATA, 0); +} + +void intr_pic_eoi(void) { + io_out8(PIC2_CMD, PIC_EOI); + io_out8(PIC1_CMD, PIC_EOI); +} diff --git a/kernel/intr/pic.h b/kernel/intr/pic.h new file mode 100644 index 0000000..7de9b6c --- /dev/null +++ b/kernel/intr/pic.h @@ -0,0 +1,7 @@ +#ifndef INTR_PIC_H_ +#define INTR_PIC_H_ + +void intr_pic_init(void); +void intr_pic_eoi(void); + +#endif // INTR_PIC_H_ diff --git a/kernel/hal/x86_64/pit.c b/kernel/intr/pit.c similarity index 91% rename from kernel/hal/x86_64/pit.c rename to kernel/intr/pit.c index 6a7903f..9046365 100644 --- a/kernel/hal/x86_64/pit.c +++ b/kernel/intr/pit.c @@ -1,5 +1,5 @@ #include -#include "pit.h" +#include "intr/pit.h" #include "io/io.h" #define PIT_COUNTER0 0x40 @@ -25,7 +25,7 @@ volatile uint32_t PIT_TICKS; -void pit_init(void) { +void intr_pit_init(void) { uint32_t hz = 1000; uint32_t div = PIT_FREQ / hz; io_out8(PIT_CMD, PIT_CMD_BINARY | PIT_CMD_MODE3 | PIT_CMD_RW_BOTH | PIT_CMD_COUNTER0); @@ -33,7 +33,7 @@ void pit_init(void) { io_out8(PIT_COUNTER0, div >> 8); } -void pit_wait(uint32_t ms) { +void intr_pit_wait(uint32_t ms) { uint32_t now = PIT_TICKS; while (PIT_TICKS - now < ms); } diff --git a/kernel/intr/pit.h b/kernel/intr/pit.h new file mode 100644 index 0000000..03ffebf --- /dev/null +++ b/kernel/intr/pit.h @@ -0,0 +1,11 @@ +#ifndef INTR_PIT_H_ +#define INTR_PIT_H_ + +#include + +extern volatile uint32_t PIT_TICKS; + +void intr_pit_init(void); +void intr_pit_wait(uint32_t ms); + +#endif // INTR_PIT_H_ diff --git a/kernel/io/io.c b/kernel/io/io.c index 6e75653..57b0f38 100644 --- a/kernel/io/io.c +++ b/kernel/io/io.c @@ -1,5 +1,5 @@ #include -#include "io.h" +#include "io/io.h" uint8_t io_in8(uint16_t port) { uint8_t r; diff --git a/kernel/ipc/pipe/pipe.c b/kernel/ipc/pipe/pipe.c index 8766020..f9437db 100644 --- a/kernel/ipc/pipe/pipe.c +++ b/kernel/ipc/pipe/pipe.c @@ -2,12 +2,11 @@ #include #include "rbuf/rbuf.h" #include "spinlock/spinlock.h" -#include "hal/hal.h" #include "dlmalloc/malloc.h" -#include "errors.h" -#include "pipe.h" -#include "kprintf.h" +#include "ipc/pipe/pipe.h" #include "std/string.h" +#include "kprintf.h" +#include "errors.h" int32_t ipc_pipeinit(IpcPipe *pipe, uint64_t pid) { memset(pipe, 0, sizeof(*pipe)); diff --git a/kernel/kmain.c b/kernel/kmain.c index b78fa20..fbc9201 100644 --- a/kernel/kmain.c +++ b/kernel/kmain.c @@ -1,7 +1,6 @@ #include #include "kprintf.h" #include "banner.h" -#include "hal/hal.h" #include "bootinfo/bootinfo.h" #include "pmm/pmm.h" #include "term/term.h" @@ -15,6 +14,9 @@ #include "randcrypto/randcrypto.h" #include "time/time.h" #include "diskpart/diskpart.h" +#include "vmm/vmm.h" +#include "cpu/hang.h" +#include "cpu/gdt.h" void log_bootinfo(void) { char buf[100]; @@ -34,16 +36,17 @@ static volatile LIMINE_BASE_REVISION(2); void kmain(void) { if (LIMINE_BASE_REVISION_SUPPORTED == false) { - hal_hang(); + cpu_hang(); } bootinfo_init(); term_init(BOOT_INFO.fb->address); log_bootinfo(); log_time(); - hal_init(); + gdt_init(); + intr_init(); pmm_init(); - hal_vmm_init(); + vmm_init(); randcrypto_init(); dev_init(); storedev_init(); diff --git a/kernel/path/path.c b/kernel/path/path.c index 6c70ab6..3f19e46 100644 --- a/kernel/path/path.c +++ b/kernel/path/path.c @@ -1,5 +1,4 @@ #include "path.h" -#include "hal/hal.h" #include "std/string.h" void path_parse(const char *in, char *mp, char *path) { diff --git a/kernel/pmm/pmm.c b/kernel/pmm/pmm.c index 7f66639..c720af5 100644 --- a/kernel/pmm/pmm.c +++ b/kernel/pmm/pmm.c @@ -1,13 +1,13 @@ #include #include -#include "pmm.h" -#include "kprintf.h" +#include "pmm/pmm.h" #include "bitmap/bitmap.h" #include "bootinfo/bootinfo.h" #include "spinlock/spinlock.h" #include "util/util.h" -#include "hal/hal.h" #include "std/string.h" +#include "cpu/hang.h" +#include "kprintf.h" PhysMem PHYS_MEM; @@ -32,7 +32,7 @@ void pmm_init(void) { if (!memmap_ent) { ERR("pmm", "required memory: {%lx}\n", bm->nbytes); - hal_hang(); + cpu_hang(); } size_t physbegin = memmap_ent->base; diff --git a/kernel/proc/proc.c b/kernel/proc/proc.c index d62e77d..1552684 100644 --- a/kernel/proc/proc.c +++ b/kernel/proc/proc.c @@ -1,15 +1,11 @@ #include #include #include -#include "hal/hal.h" #include "spinlock/spinlock.h" -#include "proc.h" +#include "proc/proc.h" #include "dlmalloc/malloc.h" #include "pmm/pmm.h" #include "util/util.h" -#include "kprintf.h" -#include "elf.h" -#include "errors.h" #include "vfs/vfs.h" #include "bootinfo/bootinfo.h" #include "ipc/pipe/pipe.h" @@ -17,6 +13,13 @@ #include "sysdefs/fs.h" #include "time/time.h" #include "std/string.h" +#include "cpu/gdt.h" +#include "intr/intr.h" +#include "switch.h" +#include "vmm/vmm.h" +#include "elf.h" +#include "errors.h" +#include "kprintf.h" #define PROC_REAPER_FREQ 30 @@ -48,25 +51,25 @@ ElfAuxval proc_load_elf_segs(Proc *proc, uint8_t *data) { aux.phdr = (uint64_t)phdr->p_vaddr; } break; case PT_LOAD: { - uint64_t off = phdr->p_vaddr & (HAL_PAGE_SIZE - 1); - uint64_t blocks = (off + phdr->p_memsz + HAL_PAGE_SIZE - 1) / HAL_PAGE_SIZE; + uint64_t off = phdr->p_vaddr & (VMM_PAGE_SIZE - 1); + uint64_t blocks = (off + phdr->p_memsz + VMM_PAGE_SIZE - 1) / VMM_PAGE_SIZE; uint8_t *physaddr = pmm_alloc(blocks); - uint8_t *virtaddr = (uint8_t *)(phdr->p_vaddr & ~(HAL_PAGE_SIZE - 1)); + uint8_t *virtaddr = (uint8_t *)(phdr->p_vaddr & ~(VMM_PAGE_SIZE - 1)); - memset(VIRT(physaddr), 0, blocks * HAL_PAGE_SIZE); + memset(VIRT(physaddr), 0, blocks * VMM_PAGE_SIZE); memcpy(VIRT(physaddr) + off, (data + phdr->p_offset), phdr->p_filesz); - uint32_t pgflags = HAL_PG_USER | HAL_PG_PRESENT; + uint32_t pgflags = VMM_PG_USER | VMM_PG_PRESENT; if (phdr->p_flags & PF_W) { - pgflags |= HAL_PG_RW; + pgflags |= VMM_PG_RW; } - hal_vmm_map_range(proc->platformdata.cr3, virtaddr, physaddr, blocks * HAL_PAGE_SIZE, pgflags); + vmm_map_range(proc->platformdata.cr3, virtaddr, physaddr, blocks * VMM_PAGE_SIZE, pgflags); VasRange *range = dlmalloc(sizeof(*range)); range->virtstart = virtaddr; range->physstart = physaddr; - range->size = blocks * HAL_PAGE_SIZE; + range->size = blocks * VMM_PAGE_SIZE; range->pgflags = pgflags; LL_APPEND(proc->vas, range); @@ -110,7 +113,7 @@ Proc *proc_spawnuser(char *mountpoint, char *path) { ksprintf(proc->name, "%s:%s", mountpoint, path); memset(&proc->platformdata.trapframe, 0, sizeof(proc->platformdata.trapframe)); - proc->platformdata.cr3 = hal_vmm_userproc_pml4_phys(); + proc->platformdata.cr3 = vmm_userproc_pml4_phys(); uint8_t *kstackp = (uint8_t *)pmm_alloc(PROC_STACKBLOCKS) + PROC_STACKSIZE; proc->platformdata.kstack = kstackp; @@ -120,8 +123,8 @@ Proc *proc_spawnuser(char *mountpoint, char *path) { uint64_t virttop = PROC_USER_STACK_TOP; uint64_t virtbase = virttop - PROC_STACKSIZE; - uint32_t flags = HAL_PG_RW | HAL_PG_PRESENT | HAL_PG_USER; - hal_vmm_map_range(proc->platformdata.cr3, (void *)virtbase, (void *)pstackp, PROC_STACKSIZE, flags); + uint32_t flags = VMM_PG_RW | VMM_PG_PRESENT | VMM_PG_USER; + vmm_map_range(proc->platformdata.cr3, (void *)virtbase, (void *)pstackp, PROC_STACKSIZE, flags); VasRange *range = dlmalloc(sizeof(*range)); range->virtstart = (uint8_t *)virtbase; @@ -203,8 +206,8 @@ void proc_reaper(void) { VasRange *vas, *vastmp; LL_FOREACH_SAFE(zombie->vas, vas, vastmp) { - hal_vmm_unmap_range(zombie->platformdata.cr3, vas->virtstart, vas->physstart, vas->size); - pmm_free((uintptr_t)vas->physstart, vas->size / HAL_PAGE_SIZE); + vmm_unmap_range(zombie->platformdata.cr3, vas->virtstart, vas->physstart, vas->size); + pmm_free((uintptr_t)vas->physstart, vas->size / VMM_PAGE_SIZE); dlfree(vas); } @@ -221,7 +224,7 @@ void proc_reaper(void) { } void proc_sched(void *cpustate) { - hal_intr_disable(); + intr_disable(); sched_ticks++; memcpy(&PROCS.current->platformdata.trapframe, cpustate, sizeof(IntrStackFrame)); @@ -233,7 +236,7 @@ void proc_sched(void *cpustate) { } tss.rsp0 = (uint64_t)VIRT(PROCS.current->platformdata.kstack); - hal_switchproc(&PROCS.current->platformdata.trapframe, (void *)PROCS.current->platformdata.cr3); + proc_switch(&PROCS.current->platformdata.trapframe, (void *)PROCS.current->platformdata.cr3); } void proc_kill(Proc *proc) { @@ -259,5 +262,5 @@ void proc_init(void) { init->state = PROC_READY; tss.rsp0 = (uint64_t)VIRT(PROCS.current->platformdata.kstack); - hal_switchproc(&PROCS.current->platformdata.trapframe, (void *)PROCS.current->platformdata.cr3); + proc_switch(&PROCS.current->platformdata.trapframe, (void *)PROCS.current->platformdata.cr3); } diff --git a/kernel/proc/proc.h b/kernel/proc/proc.h index 870c02e..ce12799 100644 --- a/kernel/proc/proc.h +++ b/kernel/proc/proc.h @@ -2,7 +2,6 @@ #define PROC_PROC_H_ #include -#include "hal/hal.h" #include "spinlock/spinlock.h" #include "bitmap/bitmap.h" #include "vfs/vfs.h" @@ -11,6 +10,8 @@ #include "sysdefs/time.h" #include "dev/dev.h" #include "std/string.h" +#include "intr/intr.h" +#include "vmm/vmm.h" #define PROC_NAME_MAX 0x100 diff --git a/kernel/hal/x86_64/switch.S b/kernel/proc/switch.S similarity index 64% rename from kernel/hal/x86_64/switch.S rename to kernel/proc/switch.S index 670ceed..d877ce1 100644 --- a/kernel/hal/x86_64/switch.S +++ b/kernel/proc/switch.S @@ -1,7 +1,7 @@ -#include "regs.S" +#include "cpu/regs.S" -.global hal_switchproc -hal_switchproc: +.global proc_switch +proc_switch: testq %rsi, %rsi je 1f movq %rsi, %cr3 diff --git a/kernel/proc/switch.h b/kernel/proc/switch.h new file mode 100644 index 0000000..0c54701 --- /dev/null +++ b/kernel/proc/switch.h @@ -0,0 +1,8 @@ +#ifndef PROC_SWITCH_H_ +#define PROC_SWITCH_H_ + +#include "vmm/vmm.h" + +extern void proc_switch(void *newsp, PgTable *cr3); + +#endif // PROC_SWITCH_H_ diff --git a/kernel/hal/x86_64/randnum.S b/kernel/randcrypto/physrand.S similarity index 67% rename from kernel/hal/x86_64/randnum.S rename to kernel/randcrypto/physrand.S index 6f74c1d..6b4e375 100644 --- a/kernel/hal/x86_64/randnum.S +++ b/kernel/randcrypto/physrand.S @@ -1,5 +1,5 @@ -.global hal_randnum -hal_randnum: +.global randcrypto_get_physrand +randcrypto_get_physrand: mov $100, %ecx xor %eax, %eax diff --git a/kernel/randcrypto/physrand.h b/kernel/randcrypto/physrand.h new file mode 100644 index 0000000..f173d06 --- /dev/null +++ b/kernel/randcrypto/physrand.h @@ -0,0 +1,8 @@ +#ifndef RANDCRYPTO_PHYSDRAND_H_ +#define RANDCRYPTO_PHYSDRAND_H_ + +#include + +int32_t randcrypto_get_physrand(void); + +#endif // RANDCRYPTO_PHYSDRAND_H_ diff --git a/kernel/randcrypto/randcrypto.c b/kernel/randcrypto/randcrypto.c index 9951ea8..2a3dba1 100644 --- a/kernel/randcrypto/randcrypto.c +++ b/kernel/randcrypto/randcrypto.c @@ -1,5 +1,5 @@ -#include "randcrypto.h" -#include "uniqid.h" +#include "randcrypto/randcrypto.h" +#include "randcrypto/uniqid.h" void randcrypto_init(void) { randcrypto_uniqid_init(); diff --git a/kernel/randcrypto/uniqid.c b/kernel/randcrypto/uniqid.c index 3e2975d..cc885de 100644 --- a/kernel/randcrypto/uniqid.c +++ b/kernel/randcrypto/uniqid.c @@ -1,9 +1,8 @@ #include #include -#include "uniqid.h" -#include "atomic.h" +#include "randcrypto/uniqid.h" +#include "randcrypto/physrand.h" #include "spinlock/spinlock.h" -#include "hal/hal.h" static const char *base62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; @@ -28,7 +27,7 @@ void uniqseed(void) { return; uint32_t seed = 0; - int32_t r = hal_randnum(); + int32_t r = randcrypto_get_physrand(); if (r != -1) { seed = (uint32_t)r; } else { @@ -48,7 +47,7 @@ void randcrypto_gen_uniqid(char *out, size_t n) { uniqseed(); - int32_t r = hal_randnum(); + int32_t r = randcrypto_get_physrand(); uint32_t extra = (r != -1) ? (uint32_t)r : uniqstate * 0x27d4eb2dU; uniqstate += 0x9E3779B1u; diff --git a/kernel/rbuf/rbuf.c b/kernel/rbuf/rbuf.c index de4745e..aed0240 100644 --- a/kernel/rbuf/rbuf.c +++ b/kernel/rbuf/rbuf.c @@ -1,9 +1,8 @@ #include #include #include -#include "rbuf.h" +#include "rbuf/rbuf.h" #include "kprintf.h" -#include "hal/hal.h" void rbuf_init(RBuf *rbuf, uint8_t *buf, size_t bufsize) { rbuf->buffer = buf; diff --git a/kernel/spinlock/spinlock.c b/kernel/spinlock/spinlock.c index 0578cbb..b2199b7 100644 --- a/kernel/spinlock/spinlock.c +++ b/kernel/spinlock/spinlock.c @@ -1,8 +1,7 @@ #include #include #include -#include "spinlock.h" -#include "hal/hal.h" +#include "spinlock/spinlock.h" #include "kprintf.h" #define SPINLOCK_HINT() asm volatile("pause") diff --git a/kernel/std/stdlib.c b/kernel/std/stdlib.c index 0064cac..3d0a61b 100644 --- a/kernel/std/stdlib.c +++ b/kernel/std/stdlib.c @@ -1,6 +1,5 @@ #include #include "dlmalloc/malloc.h" -#include "hal/hal.h" void *malloc(size_t size) { return dlmalloc(size); diff --git a/kernel/storedev/atasd.c b/kernel/storedev/atasd.c index df08d1d..fa37e61 100644 --- a/kernel/storedev/atasd.c +++ b/kernel/storedev/atasd.c @@ -1,15 +1,14 @@ #include #include #include -#include "atasd.h" -#include "storedev.h" -#include "hal/hal.h" +#include "storedev/atasd.h" +#include "storedev/storedev.h" #include "util/util.h" +#include "vfs/vfs.h" +#include "io/io.h" #include "kprintf.h" #include "errors.h" #include "hshtb.h" -#include "vfs/vfs.h" -#include "io/io.h" #define ATA_REG_DATA 0x00 #define ATA_REG_ERROR 0x01 diff --git a/kernel/storedev/partsd.c b/kernel/storedev/partsd.c index 8f2ca1e..7191ee4 100644 --- a/kernel/storedev/partsd.c +++ b/kernel/storedev/partsd.c @@ -1,7 +1,7 @@ #include #include -#include "partsd.h" -#include "storedev.h" +#include "storedev/partsd.h" +#include "storedev/storedev.h" #include "errors.h" #include "kprintf.h" diff --git a/kernel/storedev/ramsd.c b/kernel/storedev/ramsd.c index 252a77b..1b0882e 100644 --- a/kernel/storedev/ramsd.c +++ b/kernel/storedev/ramsd.c @@ -1,14 +1,13 @@ #include #include #include "spinlock/spinlock.h" -#include "errors.h" #include "dlmalloc/malloc.h" -#include "ramsd.h" -#include "storedev.h" -#include "hal/hal.h" +#include "storedev/ramsd.h" +#include "storedev/storedev.h" #include "util/util.h" -#include "kprintf.h" #include "std/string.h" +#include "kprintf.h" +#include "errors.h" int32_t ramsd_init(struct StoreDev *sd, void *extra) { RamSdInitExtra *e = extra; diff --git a/kernel/storedev/storedev.c b/kernel/storedev/storedev.c index b8ff585..f9284b7 100644 --- a/kernel/storedev/storedev.c +++ b/kernel/storedev/storedev.c @@ -2,17 +2,17 @@ #include #include "storedev.h" #include "spinlock/spinlock.h" -#include "kprintf.h" -#include "errors.h" #include "dlmalloc/malloc.h" -#include "ramsd.h" -#include "atasd.h" +#include "storedev/ramsd.h" +#include "storedev/atasd.h" +#include "storedev/partsd.h" #include "util/util.h" #include "dev/dev.h" -#include "hshtb.h" -#include "hal/hal.h" #include "randcrypto/uniqid.h" #include "sysdefs/dev.h" +#include "kprintf.h" +#include "hshtb.h" +#include "errors.h" StoreDevList STOREDEV_LIST; diff --git a/kernel/storedev/storedev.h b/kernel/storedev/storedev.h index b70167a..4d1f5be 100644 --- a/kernel/storedev/storedev.h +++ b/kernel/storedev/storedev.h @@ -4,9 +4,9 @@ #include #include #include "spinlock/spinlock.h" -#include "ramsd.h" -#include "atasd.h" -#include "partsd.h" +#include "storedev/ramsd.h" +#include "storedev/atasd.h" +#include "storedev/partsd.h" #include "compiler/attr.h" #include "dev/dev.h" diff --git a/kernel/syscall/dev.c b/kernel/syscall/dev.c index fbbd437..db65e7f 100644 --- a/kernel/syscall/dev.c +++ b/kernel/syscall/dev.c @@ -1,15 +1,15 @@ #include #include -#include "kprintf.h" -#include "syscall.h" -#include "errors.h" +#include "syscall/syscall.h" #include "spinlock/spinlock.h" #include "proc/proc.h" #include "sysdefs/dev.h" #include "util/util.h" -#include "hshtb.h" #include "dev/dev.h" #include "std/string.h" +#include "errors.h" +#include "kprintf.h" +#include "hshtb.h" int32_t SYSCALL2(sys_dev_gethandle, dev1, devname1) { uint64_t *devh = (uint64_t *)dev1; diff --git a/kernel/syscall/dev.h b/kernel/syscall/dev.h index 72ae2fb..d17a573 100644 --- a/kernel/syscall/dev.h +++ b/kernel/syscall/dev.h @@ -3,7 +3,7 @@ #include #include -#include "syscall.h" +#include "syscall/syscall.h" int32_t SYSCALL2(sys_dev_gethandle, dev1, devname1); int32_t SYSCALL0(sys_dev_listsize); diff --git a/kernel/syscall/fs.c b/kernel/syscall/fs.c index 0337665..ffc02a4 100644 --- a/kernel/syscall/fs.c +++ b/kernel/syscall/fs.c @@ -1,14 +1,14 @@ #include #include -#include "syscall.h" +#include "syscall/syscall.h" #include "sysdefs/fs.h" -#include "errors.h" #include "path/path.h" #include "vfs/vfs.h" -#include "kprintf.h" #include "proc/proc.h" #include "spinlock/spinlock.h" #include "util/util.h" +#include "errors.h" +#include "kprintf.h" #define _MP_MAX 0xff #define _PATH_MAX VFS_PATH_MAX diff --git a/kernel/syscall/fs.h b/kernel/syscall/fs.h index f6e1189..96bffe7 100644 --- a/kernel/syscall/fs.h +++ b/kernel/syscall/fs.h @@ -3,7 +3,7 @@ #include #include -#include "syscall.h" +#include "syscall/syscall.h" int32_t SYSCALL2(sys_fs_openf, opath1, oflags1); int32_t SYSCALL1(sys_fs_closef, fsh1); diff --git a/kernel/syscall/ipcpipe.c b/kernel/syscall/ipcpipe.c index fd20dc7..8f6ea7c 100644 --- a/kernel/syscall/ipcpipe.c +++ b/kernel/syscall/ipcpipe.c @@ -1,12 +1,12 @@ #include #include -#include "ipcpipe.h" +#include "syscall/ipcpipe.h" #include "ipc/pipe/pipe.h" #include "dlmalloc/malloc.h" #include "proc/proc.h" #include "spinlock/spinlock.h" -#include "errors.h" #include "util/util.h" +#include "errors.h" #include "kprintf.h" int32_t SYSCALL4(sys_ipc_piperead, pid1, pipenum1, buffer1, len1) { diff --git a/kernel/syscall/ipcpipe.h b/kernel/syscall/ipcpipe.h index 37d9f62..ae171c8 100644 --- a/kernel/syscall/ipcpipe.h +++ b/kernel/syscall/ipcpipe.h @@ -1,7 +1,9 @@ #ifndef SYSCALL_IPCPIPE_H_ #define SYSCALL_IPCPIPE_H_ -#include "syscall.h" +#include +#include +#include "syscall/syscall.h" int32_t SYSCALL4(sys_ipc_piperead, pid1, pipenum1, buffer1, len1); int32_t SYSCALL4(sys_ipc_pipewrite, pid1, pipenum1, buffer1, len1); diff --git a/kernel/syscall/mman.c b/kernel/syscall/mman.c index 672f59e..9e40c57 100644 --- a/kernel/syscall/mman.c +++ b/kernel/syscall/mman.c @@ -1,17 +1,17 @@ #include -#include "syscall.h" -#include "mman.h" +#include "syscall/syscall.h" +#include "syscall/mman.h" #include "spinlock/spinlock.h" #include "proc/proc.h" -#include "hal/hal.h" #include "pmm/pmm.h" #include "util/util.h" -#include "errors.h" #include "sysdefs/mman.h" #include "bootinfo/bootinfo.h" #include "dlmalloc/malloc.h" -#include "kprintf.h" #include "std/string.h" +#include "vmm/vmm.h" +#include "errors.h" +#include "kprintf.h" int32_t SYSCALL5(sys_mman_map, addr1, size1, prot1, flags1, out1) { uint8_t *addr = (uint8_t *)addr1; @@ -20,16 +20,16 @@ int32_t SYSCALL5(sys_mman_map, addr1, size1, prot1, flags1, out1) { uint64_t flags = flags1; uint8_t **out = (uint8_t **)out1; - if (size == 0 || (size % HAL_PAGE_SIZE != 0)) { + if (size == 0 || (size % VMM_PAGE_SIZE != 0)) { if (out != NULL) { *out = NULL; } return E_INVALIDARGUMENT; } - size_t pages = _DIV_ROUNDUP(size, HAL_PAGE_SIZE); + size_t pages = _DIV_ROUNDUP(size, VMM_PAGE_SIZE); uint8_t *phys = (uint8_t *)pmm_alloc(pages); - memset(VIRT(phys), 0, pages * HAL_PAGE_SIZE); + memset(VIRT(phys), 0, pages * VMM_PAGE_SIZE); spinlock_acquire(&PROCS.spinlock); Proc *proc = NULL; @@ -38,7 +38,7 @@ int32_t SYSCALL5(sys_mman_map, addr1, size1, prot1, flags1, out1) { uint8_t *virt = NULL; if ((flags & MMAN_MAP_F_FIXED) && addr != NULL) { - if ((uintptr_t)addr % HAL_PAGE_SIZE != 0) { + if ((uintptr_t)addr % VMM_PAGE_SIZE != 0) { if (out != NULL) { *out = NULL; } @@ -49,19 +49,19 @@ int32_t SYSCALL5(sys_mman_map, addr1, size1, prot1, flags1, out1) { virt = addr; } else { virt = (uint8_t *)proc->mman_map_base; - proc->mman_map_base += pages * HAL_PAGE_SIZE; + proc->mman_map_base += pages * VMM_PAGE_SIZE; } - uint64_t pflags = HAL_PG_USER | HAL_PG_PRESENT; + uint64_t pflags = VMM_PG_USER | VMM_PG_PRESENT; if (prot & MMAN_MAP_PF_RW) { - pflags |= HAL_PG_RW; + pflags |= VMM_PG_RW; } - hal_vmm_map_range(proc->platformdata.cr3, virt, phys, pages * HAL_PAGE_SIZE, pflags); + vmm_map_range(proc->platformdata.cr3, virt, phys, pages * VMM_PAGE_SIZE, pflags); VasRange *range = dlmalloc(sizeof(*range)); range->virtstart = virt; range->physstart = phys; - range->size = pages * HAL_PAGE_SIZE; + range->size = pages * VMM_PAGE_SIZE; range->pgflags = pflags; LL_APPEND(proc->vas, range); @@ -98,9 +98,9 @@ int32_t SYSCALL1(sys_mman_unmap, addr1) { return E_INVALIDARGUMENT; } - hal_vmm_unmap_range(proc->platformdata.cr3, tofree->virtstart, tofree->physstart, tofree->size); + vmm_unmap_range(proc->platformdata.cr3, tofree->virtstart, tofree->physstart, tofree->size); LL_REMOVE(proc->vas, tofree); - pmm_free((uintptr_t)tofree->physstart, tofree->size / HAL_PAGE_SIZE); + pmm_free((uintptr_t)tofree->physstart, tofree->size / VMM_PAGE_SIZE); dlfree(tofree); return E_OK; } diff --git a/kernel/syscall/mman.h b/kernel/syscall/mman.h index 91bb445..7846fd7 100644 --- a/kernel/syscall/mman.h +++ b/kernel/syscall/mman.h @@ -1,7 +1,9 @@ #ifndef SYSCALL_MMAN_H_ #define SYSCALL_MMAN_H_ -#include "syscall.h" +#include +#include +#include "syscall/syscall.h" int32_t SYSCALL5(sys_mman_map, addr1, size1, prot1, flags1, out1); int32_t SYSCALL1(sys_mman_unmap, addr1); diff --git a/kernel/syscall/proc.c b/kernel/syscall/proc.c index 61390cf..94e4e4e 100644 --- a/kernel/syscall/proc.c +++ b/kernel/syscall/proc.c @@ -1,17 +1,17 @@ #include #include -#include "syscall.h" +#include "syscall/syscall.h" #include "proc/proc.h" #include "spinlock/spinlock.h" -#include "errors.h" #include "util/util.h" #include "sysdefs/proc.h" #include "vfs/vfs.h" #include "path/path.h" -#include "kprintf.h" #include "dlmalloc/malloc.h" #include "ipc/pipe/pipe.h" #include "std/string.h" +#include "errors.h" +#include "kprintf.h" #define _MP_MAX 0xff #define _PATH_MAX VFS_PATH_MAX diff --git a/kernel/syscall/proc.h b/kernel/syscall/proc.h index 3b3513d..271703f 100644 --- a/kernel/syscall/proc.h +++ b/kernel/syscall/proc.h @@ -3,7 +3,7 @@ #include #include -#include "syscall.h" +#include "syscall/syscall.h" int32_t SYSCALL1(sys_proc_kill, pid1); int32_t SYSCALL3(sys_proc_spawn, opath1, args1, argslen1); diff --git a/kernel/syscall/randcrypto.c b/kernel/syscall/randcrypto.c index 28c7bb6..6875cb9 100644 --- a/kernel/syscall/randcrypto.c +++ b/kernel/syscall/randcrypto.c @@ -1,8 +1,8 @@ #include #include -#include "hal/hal.h" -#include "syscall.h" +#include "syscall/syscall.h" +#include "randcrypto/physrand.h" int32_t SYSCALL0(sys_rand) { - return hal_randnum(); + return randcrypto_get_physrand(); } diff --git a/kernel/syscall/randcrypto.h b/kernel/syscall/randcrypto.h index a4e42dd..2278ae2 100644 --- a/kernel/syscall/randcrypto.h +++ b/kernel/syscall/randcrypto.h @@ -1,7 +1,9 @@ #ifndef SYSCALL_RANDCRYPTO_H_ #define SYSCALL_RANDCRYPTO_H_ -#include "syscall.h" +#include +#include +#include "syscall/syscall.h" int32_t SYSCALL0(sys_rand); diff --git a/kernel/syscall/sched.c b/kernel/syscall/sched.c index 726ba27..885a978 100644 --- a/kernel/syscall/sched.c +++ b/kernel/syscall/sched.c @@ -1,10 +1,10 @@ #include -#include "syscall.h" -#include "sched.h" +#include "syscall/syscall.h" +#include "syscall/sched.h" #include "proc/proc.h" #include "spinlock/spinlock.h" +#include "intr/pit.h" #include "errors.h" -#include "hal/hal.h" int32_t SYSCALL0(sys_schedrelease) { return E_DOSCHEDULING; @@ -12,6 +12,6 @@ int32_t SYSCALL0(sys_schedrelease) { int32_t SYSCALL1(sys_schedsleep, ms1) { uint32_t ms = (uint32_t)ms1; - hal_wait(ms); + intr_pit_wait(ms); return E_OK; } diff --git a/kernel/syscall/sched.h b/kernel/syscall/sched.h index bcb3d44..a37c9d1 100644 --- a/kernel/syscall/sched.h +++ b/kernel/syscall/sched.h @@ -2,7 +2,8 @@ #define SYSCALL_SCHED_H_ #include -#include "syscall.h" +#include +#include "syscall/syscall.h" int32_t SYSCALL0(sys_schedrelease); int32_t SYSCALL1(sys_schedsleep, ms1); diff --git a/kernel/syscall/syscall.c b/kernel/syscall/syscall.c index e71f0ee..44d0c92 100644 --- a/kernel/syscall/syscall.c +++ b/kernel/syscall/syscall.c @@ -1,17 +1,17 @@ #include -#include "syscall.h" +#include "sysdefs/syscall.h" +#include "syscall/syscall.h" +#include "syscall/ipcpipe.h" +#include "syscall/mman.h" +#include "syscall/sched.h" +#include "syscall/randcrypto.h" +#include "syscall/vfs.h" +#include "syscall/proc.h" +#include "syscall/fs.h" +#include "syscall/dev.h" +#include "syscall/time.h" #include "errors.h" #include "kprintf.h" -#include "sysdefs/syscall.h" -#include "ipcpipe.h" -#include "mman.h" -#include "sched.h" -#include "randcrypto.h" -#include "vfs.h" -#include "proc.h" -#include "fs.h" -#include "dev.h" -#include "time.h" int32_t SYSCALL1(sys_debugprint, string) { char *p = (char *)string; diff --git a/kernel/syscall/syscall.h b/kernel/syscall/syscall.h index f3c87d1..fe9756a 100644 --- a/kernel/syscall/syscall.h +++ b/kernel/syscall/syscall.h @@ -3,7 +3,7 @@ #include #include "compiler/attr.h" -#include "hal/hal.h" +#include "intr/intr.h" #define SYSCALLS_MAX 0xff diff --git a/kernel/syscall/time.c b/kernel/syscall/time.c index 5e8f8b4..89b2838 100644 --- a/kernel/syscall/time.c +++ b/kernel/syscall/time.c @@ -1,7 +1,7 @@ #include #include -#include "syscall.h" -#include "time.h" +#include "syscall/syscall.h" +#include "syscall/time.h" #include "sysdefs/time.h" #include "time/time.h" #include "errors.h" diff --git a/kernel/syscall/time.h b/kernel/syscall/time.h index def1cc3..0618060 100644 --- a/kernel/syscall/time.h +++ b/kernel/syscall/time.h @@ -3,6 +3,7 @@ #include #include +#include "syscall/syscall.h" int32_t SYSCALL1(sys_time, timestruct1); diff --git a/kernel/syscall/vfs.c b/kernel/syscall/vfs.c index 3f03fb8..f1f15a8 100644 --- a/kernel/syscall/vfs.c +++ b/kernel/syscall/vfs.c @@ -1,10 +1,8 @@ #include #include #include -#include "hal/hal.h" -#include "syscall.h" -#include "vfs.h" -#include "errors.h" +#include "syscall/syscall.h" +#include "syscall/vfs.h" #include "sysdefs/dev.h" #include "sysdefs/vfs.h" #include "vfs/vfs.h" @@ -13,8 +11,9 @@ #include "proc/proc.h" #include "storedev/storedev.h" #include "util/util.h" -#include "hshtb.h" #include "std/string.h" +#include "hshtb.h" +#include "errors.h" int32_t SYSCALL4(sys_vfsmount, mountpoint1, fstypestr1, devid1, format1) { int32_t ret = E_OK; diff --git a/kernel/term/term.c b/kernel/term/term.c index 910f7f5..051777d 100644 --- a/kernel/term/term.c +++ b/kernel/term/term.c @@ -2,7 +2,7 @@ #include "flanterm_backends/fb.h" #include "spinlock/spinlock.h" #include "bootinfo/bootinfo.h" -#include "term.h" +#include "term/term.h" #include "fm-t-437.f16.h" Term TERM; diff --git a/kernel/time/time.c b/kernel/time/time.c index 0e2c790..8171e47 100644 --- a/kernel/time/time.c +++ b/kernel/time/time.c @@ -1,7 +1,6 @@ #include #include -#include "time.h" -#include "hal/hal.h" +#include "time/time.h" #include "std/string.h" #include "io/io.h" diff --git a/kernel/util/util.c b/kernel/util/util.c index a2c5226..b6c0407 100644 --- a/kernel/util/util.c +++ b/kernel/util/util.c @@ -1,7 +1,7 @@ #include #include +#include "util/util.h" #include "kprintf.h" -#include "util.h" char *util_get_filename(char *path) { char *lastslash = path; diff --git a/kernel/vfs/vfs.c b/kernel/vfs/vfs.c index 0018447..3e31c9c 100644 --- a/kernel/vfs/vfs.c +++ b/kernel/vfs/vfs.c @@ -1,18 +1,16 @@ #include #include -#include "vfs.h" -#include "kprintf.h" +#include "vfs/vfs.h" #include "spinlock/spinlock.h" -#include "hal/hal.h" #include "util/util.h" -#include "hshtb.h" -#include "assert.h" -#include "errors.h" #include "fs/portlfs/portlfs.h" #include "storedev/storedev.h" #include "baseimg/baseimg.h" #include "dlmalloc/malloc.h" #include "std/string.h" +#include "kprintf.h" +#include "errors.h" +#include "hshtb.h" VfsTable VFS_TABLE; VfsBusyObjs VFS_BUSY_VOBJS; diff --git a/kernel/vmm/vmm.c b/kernel/vmm/vmm.c new file mode 100644 index 0000000..9d53aff --- /dev/null +++ b/kernel/vmm/vmm.c @@ -0,0 +1,127 @@ +#include +#include +#include "vmm/vmm.h" +#include "bootinfo/bootinfo.h" +#include "pmm/pmm.h" +#include "proc/proc.h" +#include "spinlock/spinlock.h" +#include "std/string.h" +#include "kprintf.h" + +uint64_t KERNEL_CR3 = 0; +SpinLock spinlock; + +uint64_t vmm_current_cr3(void) { + uint64_t cr3; + asm volatile("mov %%cr3, %0" : "=r"(cr3)); + return cr3; +} + +PgIndex vmm_pageindex(uint64_t vaddr) { + PgIndex ret; + + ret.pml4 = (vaddr >> 39) & 0x1ff; + ret.pml3 = (vaddr >> 30) & 0x1ff; + ret.pml2 = (vaddr >> 21) & 0x1ff; + ret.pml1 = (vaddr >> 12) & 0x1ff; + + return ret; +} + +uint64_t *vmm_nexttable(uint64_t *table, uint64_t ent, bool alloc) { + uint64_t entry = table[ent]; + uint64_t phys; + if (entry & VMM_PG_PRESENT) { + phys = entry & ~0xFFFULL; + } else { + if (!alloc) { + return NULL; + } + + uint8_t *newphys = pmm_alloc(1); + phys = (uint64_t)newphys; + memset(VIRT(phys), 0, VMM_PAGE_SIZE); + table[ent] = phys | VMM_PG_USER | VMM_PG_RW | VMM_PG_PRESENT; + } + return (uint64_t *)((uint8_t *)VIRT(phys)); +} + +void vmm_map_page(uint64_t cr3phys, uint64_t virtaddr, uint64_t physaddr, uint32_t flags) { + uint64_t *pml4 = (uint64_t *)VIRT(cr3phys); + PgIndex pi = vmm_pageindex(virtaddr); + + uint64_t *pml3 = vmm_nexttable(pml4, pi.pml4, true); + uint64_t *pml2 = vmm_nexttable(pml3, pi.pml3, true); + uint64_t *pml1 = vmm_nexttable(pml2, pi.pml2, true); + uint64_t *pte = &pml1[pi.pml1]; + + *pte = (physaddr & ~0xFFFULL) | ((uint64_t)flags & 0x7ULL); +} + +void vmm_unmap_page(uint64_t cr3phys, uint64_t virtaddr) { + uint64_t *pml4 = (uint64_t *)VIRT(cr3phys); + PgIndex pi = vmm_pageindex(virtaddr); + + uint64_t *pml3 = vmm_nexttable(pml4, pi.pml4, false); + uint64_t *pml2 = vmm_nexttable(pml3, pi.pml3, false); + uint64_t *pml1 = vmm_nexttable(pml2, pi.pml2, false); + uint64_t *pte = &pml1[pi.pml1]; + + *pte &= ~VMM_PG_PRESENT; +} + +void vmm_map_range(uint64_t cr3phys, void *virtstart, void *physstart, size_t size, uint32_t flags) { + if (size % VMM_PAGE_SIZE != 0 || (uint64_t)virtstart % VMM_PAGE_SIZE != 0 || (uint64_t)physstart % VMM_PAGE_SIZE != 0) { + return; + } + + spinlock_acquire(&spinlock); + uint8_t *vaddr = (uint8_t *)virtstart; + uint8_t *paddr = (uint8_t *)physstart; + uint8_t *end = (uint8_t *)virtstart + size; + for (; vaddr < end; vaddr += VMM_PAGE_SIZE, paddr += VMM_PAGE_SIZE) { + vmm_map_page(cr3phys, (uint64_t)vaddr, (uint64_t)paddr, flags); + } + spinlock_release(&spinlock); +} + +void vmm_unmap_range(uint64_t cr3phys, void *virtstart, void *physstart, size_t size) { + if (size % VMM_PAGE_SIZE != 0 || (uint64_t)virtstart % VMM_PAGE_SIZE != 0 || (uint64_t)physstart % VMM_PAGE_SIZE != 0) { + return; + } + + spinlock_acquire(&spinlock); + uint8_t *vaddr = (uint8_t *)virtstart; + uint8_t *end = vaddr + size; + + for (; vaddr < end; vaddr += VMM_PAGE_SIZE) { + vmm_unmap_page(cr3phys, (uint64_t)vaddr); + } + spinlock_release(&spinlock); +} + +void vmm_map_kern(uint64_t targetcr3) { + uint64_t *kcr3 = (uint64_t *)VIRT(KERNEL_CR3); + uint64_t *cr3 = (uint64_t *)VIRT(targetcr3); + for (size_t i = 0; i < 512; i++) { + cr3[i] = kcr3[i]; + } +} + +uint64_t vmm_userproc_pml4_phys(void) { + uint8_t *cr3phys = pmm_alloc(1); + uint64_t phys = (uint64_t)cr3phys; + memset(VIRT(phys), 0, VMM_PAGE_SIZE); + + uint64_t *kcr3 = (uint64_t *)VIRT(KERNEL_CR3); + uint64_t *pml4 = (uint64_t *)VIRT(phys); + for (size_t i = 256; i < 512; i++) { + pml4[i] = kcr3[i]; + } + return phys; +} + +void vmm_init(void) { + spinlock_init(&spinlock); + KERNEL_CR3 = vmm_current_cr3(); +} diff --git a/kernel/hal/x86_64/vmm.h b/kernel/vmm/vmm.h similarity index 56% rename from kernel/hal/x86_64/vmm.h rename to kernel/vmm/vmm.h index cf3ed6c..41f9203 100644 --- a/kernel/hal/x86_64/vmm.h +++ b/kernel/vmm/vmm.h @@ -1,11 +1,13 @@ -#ifndef HAL_VMM_H_ -#define HAL_VMM_H_ +#ifndef VMM_VMM_H_ +#define VMM_VMM_H_ #include #include #include #include "compiler/attr.h" +#define VMM_PAGE_SIZE 0x1000 + #define VIRT(X) ((void *)(BOOT_INFO.hhdm_off + (uint64_t)(X))) typedef struct VasRange { @@ -18,9 +20,9 @@ typedef struct VasRange { } PACKED VasRange; enum { - HAL_PG_PRESENT = 1<<0, - HAL_PG_RW = 1<<1, - HAL_PG_USER = 1<<2, + VMM_PG_PRESENT = 1<<0, + VMM_PG_RW = 1<<1, + VMM_PG_USER = 1<<2, }; typedef struct { @@ -52,12 +54,12 @@ typedef struct { extern uint64_t KERNEL_CR3; -void hal_vmm_init(void); -void hal_vmm_unmap_page(uint64_t cr3phys, uint64_t virtaddr); -void hal_vmm_map_page(uint64_t cr3phys, uint64_t virtaddr, uint64_t physaddr, uint32_t flags); -uint64_t hal_vmm_current_cr3(void); -void hal_vmm_map_range(uint64_t cr3phys, void *virtstart, void *physstart, size_t size, uint32_t flags); -void hal_vmm_unmap_range(uint64_t cr3phys, void *virtstart, void *physstart, size_t size); -uint64_t hal_vmm_userproc_pml4_phys(); +void vmm_init(void); +void vmm_unmap_page(uint64_t cr3phys, uint64_t virtaddr); +void vmm_map_page(uint64_t cr3phys, uint64_t virtaddr, uint64_t physaddr, uint32_t flags); +uint64_t vmm_current_cr3(void); +void vmm_map_range(uint64_t cr3phys, void *virtstart, void *physstart, size_t size, uint32_t flags); +void vmm_unmap_range(uint64_t cr3phys, void *virtstart, void *physstart, size_t size); +uint64_t vmm_userproc_pml4_phys(void); -#endif // HAL_VMM_H_ +#endif // VMM_VMM_H_