Big code refactor, get rid of HAL entirely

This commit is contained in:
2025-11-11 21:26:27 +01:00
parent 7015bc9576
commit 566b35f4d5
84 changed files with 477 additions and 520 deletions

View File

@ -1,7 +1,6 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include "hal/hal.h" #include "FastLZ/fastlz.h"
#include "fastlz.h"
#include "std/string.h" #include "std/string.h"
#define SIXPACK_OK 0 #define SIXPACK_OK 0

View File

@ -46,7 +46,6 @@ SRCFILES += $(call GRABSRC, \
fs/portlfs \ fs/portlfs \
baseimg \ baseimg \
proc \ proc \
hal/$(ARCH) \
std \ std \
flanterm/src \ flanterm/src \
flanterm/src/flanterm_backends \ flanterm/src/flanterm_backends \
@ -60,6 +59,9 @@ SRCFILES += $(call GRABSRC, \
diskpart \ diskpart \
FastLZ \ FastLZ \
io \ io \
intr \
cpu \
vmm \
) )
CFILES := $(call GET_CFILES, $(SRCFILES)) CFILES := $(call GET_CFILES, $(SRCFILES))

View File

@ -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_

View File

@ -1,15 +1,15 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include <limine.h> #include <limine.h>
#include "baseimg.h" #include "baseimg/baseimg.h"
#include "bootinfo/bootinfo.h" #include "bootinfo/bootinfo.h"
#include "kprintf.h"
#include "util/util.h" #include "util/util.h"
#include "hal/hal.h"
#include "dlmalloc/malloc.h" #include "dlmalloc/malloc.h"
#include "FastLZ/fastlz.h" #include "FastLZ/fastlz.h"
#include "FastLZ/6unpack_mem.h" #include "FastLZ/6unpack_mem.h"
#include "std/string.h" #include "std/string.h"
#include "cpu/hang.h"
#include "kprintf.h"
#define BASEIMG_DECOMPRESSED (1024*1024*4) #define BASEIMG_DECOMPRESSED (1024*1024*4)
@ -38,7 +38,7 @@ void baseimg_init(void) {
if (baseimg == NULL) { if (baseimg == NULL) {
ERR("baseimg", "base.img.6pk not found\n"); ERR("baseimg", "base.img.6pk not found\n");
hal_hang(); cpu_hang();
} else { } else {
LOG("baseimg", "base.img.6pk found\n"); LOG("baseimg", "base.img.6pk found\n");
LOG("baseimg", "addr = %p, size = %lu\n", baseimg->address, baseimg->size); LOG("baseimg", "addr = %p, size = %lu\n", baseimg->address, baseimg->size);
@ -55,7 +55,7 @@ void baseimg_init(void) {
BASEIMG_DECOMP_ADDR, BASEIMG_DECOMPRESSED); BASEIMG_DECOMP_ADDR, BASEIMG_DECOMPRESSED);
if (res < 0) { if (res < 0) {
ERR("baseimg", "could not uncompress base.img.6pk\n"); ERR("baseimg", "could not uncompress base.img.6pk\n");
hal_hang(); cpu_hang();
} }
BASEIMG_DECOMP_SIZE = res; BASEIMG_DECOMP_SIZE = res;
kprintf("%p %zu\n", BASEIMG_DECOMP_ADDR, BASEIMG_DECOMP_SIZE); kprintf("%p %zu\n", BASEIMG_DECOMP_ADDR, BASEIMG_DECOMP_SIZE);

View File

@ -1,7 +1,7 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include <stdbool.h> #include <stdbool.h>
#include "bitmap.h" #include "bitmap/bitmap.h"
#include "util/util.h" #include "util/util.h"
#include "kprintf.h" #include "kprintf.h"

View File

@ -4,8 +4,7 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include <stdbool.h> #include <stdbool.h>
#include "vmm/vmm.h"
#include "hal/hal.h"
typedef struct { typedef struct {
uint8_t *map; uint8_t *map;
@ -17,7 +16,7 @@ typedef struct {
} BitMap; } BitMap;
#define BITMAP_BLOCKS_PER_BYTE 8 #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) #define BITMAP_INVALID_BLOCK ((size_t)-1)
void *bitmap_toptr(BitMap *bm, size_t block); void *bitmap_toptr(BitMap *bm, size_t block);

View File

@ -1,8 +1,8 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include <limine.h> #include <limine.h>
#include "bootinfo.h" #include "bootinfo/bootinfo.h"
#include "hal/hal.h" #include "cpu/hang.h"
BootInfo BOOT_INFO; BootInfo BOOT_INFO;
@ -26,7 +26,7 @@ DEFINE_REQ(module, MODULE);
void bootinfo_init(void) { void bootinfo_init(void) {
if (framebuffer_req.response == NULL || framebuffer_req.response->framebuffer_count < 1) { if (framebuffer_req.response == NULL || framebuffer_req.response->framebuffer_count < 1) {
hal_hang(); cpu_hang();
} }
BOOT_INFO.fb = framebuffer_req.response->framebuffers[0]; 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; struct limine_paging_mode_response *pagingres = paging_req.response;
if (pagingres->mode != LIMINE_PAGING_MODE_X86_64_4LVL) { if (pagingres->mode != LIMINE_PAGING_MODE_X86_64_4LVL) {
hal_hang(); cpu_hang();
} }
struct limine_hhdm_response *hhdmres = hhdm_req.response; struct limine_hhdm_response *hhdmres = hhdm_req.response;
@ -68,7 +68,7 @@ void bootinfo_init(void) {
} }
} }
if (BOOT_INFO.smp_bspindex == (uint64_t)-1) { if (BOOT_INFO.smp_bspindex == (uint64_t)-1) {
hal_hang(); cpu_hang();
} }
struct limine_rsdp_response *rsdpres = rsdp_req.response; struct limine_rsdp_response *rsdpres = rsdp_req.response;

View File

@ -1,8 +1,7 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include "compiler/attr.h" #include "compiler/attr.h"
#include "hal/hal.h" #include "cpu/gdt.h"
#include "gdt.h"
#include "std/string.h" #include "std/string.h"
#define GDT_PRESENT 0x80 #define GDT_PRESENT 0x80

View File

@ -1,5 +1,8 @@
#ifndef HAL_GDT_H_ #ifndef CPU_GDT_H_
#define HAL_GDT_H_ #define CPU_GDT_H_
#include <stdint.h>
#include "compiler/attr.h"
#define KCODE 0x08 #define KCODE 0x08
#define KDATA 0x10 #define KDATA 0x10
@ -23,4 +26,4 @@ ALIGNED(16) extern Tss tss;
void gdt_init(void); void gdt_init(void);
#endif // HAL_GDT_H_ #endif // CPU_GDT_H_

10
kernel/cpu/hang.c Normal file
View File

@ -0,0 +1,10 @@
#include "cpu/hang.h"
#include "intr/intr.h"
void cpu_hang(void) {
intr_disable();
for(;;) {
asm("hlt");
}
}

6
kernel/cpu/hang.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef CPU_HANG_H_
#define CPU_HANG_H_
void cpu_hang(void);
#endif // CPU_HANG_H_

View File

@ -1,14 +1,13 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include "spinlock/spinlock.h" #include "spinlock/spinlock.h"
#include "dev.h"
#include "hshtb.h"
#include "hal/hal.h"
#include "std/string.h" #include "std/string.h"
#include "termdev.h" #include "dev/dev.h"
#include "ps2kbdev.h" #include "dev/termdev.h"
#include "serialdev.h" #include "dev/ps2kbdev.h"
#include "fbdev.h" #include "dev/serialdev.h"
#include "dev/fbdev.h"
#include "hshtb.h"
DevTable DEVTABLE; DevTable DEVTABLE;

View File

@ -1,15 +1,14 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include "fbdev.h" #include "dev/fbdev.h"
#include "dev.h" #include "dev/dev.h"
#include "sysdefs/dev.h" #include "sysdefs/dev.h"
#include "hshtb.h"
#include "spinlock/spinlock.h" #include "spinlock/spinlock.h"
#include "util/util.h" #include "util/util.h"
#include "hal/hal.h"
#include "bootinfo/bootinfo.h" #include "bootinfo/bootinfo.h"
#include "errors.h"
#include "std/string.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) { int32_t fbdev_getinfo(struct Dev *dev, uint8_t *buffer, size_t len, uint64_t pid) {
(void)dev; (void)pid; (void)dev; (void)pid;

View File

@ -1,16 +1,15 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include "kprintf.h" #include "dev/ps2kbdev.h"
#include "hal/hal.h" #include "dev/dev.h"
#include "ps2kbdev.h"
#include "dev.h"
#include "errors.h"
#include "dlmalloc/malloc.h" #include "dlmalloc/malloc.h"
#include "util/util.h" #include "util/util.h"
#include "hshtb.h"
#include "sysdefs/dev.h" #include "sysdefs/dev.h"
#include "proc/proc.h" #include "proc/proc.h"
#include "io/io.h" #include "io/io.h"
#include "errors.h"
#include "hshtb.h"
#include "kprintf.h"
#define KB_CTL_STATUS 0x64 #define KB_CTL_STATUS 0x64
#define KB_DATA_IN_BUF 0x01 #define KB_DATA_IN_BUF 0x01

View File

@ -1,14 +1,13 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include "dev.h" #include "dev/dev.h"
#include "serialdev.h" #include "dev/serialdev.h"
#include "errors.h"
#include "util/util.h" #include "util/util.h"
#include "hshtb.h"
#include "sysdefs/dev.h" #include "sysdefs/dev.h"
#include "kprintf.h"
#include "hal/hal.h"
#include "io/io.h" #include "io/io.h"
#include "errors.h"
#include "hshtb.h"
#include "kprintf.h"
// https://wiki.osdev.org/Serial_Ports // https://wiki.osdev.org/Serial_Ports

View File

@ -1,13 +1,12 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include "kprintf.h" #include "dev/termdev.h"
#include "hal/hal.h" #include "dev/dev.h"
#include "termdev.h"
#include "dev.h"
#include "errors.h"
#include "util/util.h" #include "util/util.h"
#include "hshtb.h"
#include "sysdefs/dev.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) { int32_t termdev_putch(struct Dev *dev, uint8_t *buffer, size_t len, uint64_t pid) {
(void)dev; (void)pid; (void)dev; (void)pid;

View File

@ -1,7 +1,7 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include "storedev/storedev.h" #include "storedev/storedev.h"
#include "mbr.h" #include "diskpart/mbr.h"
#include "util/util.h" #include "util/util.h"
#include "kprintf.h" #include "kprintf.h"

View File

@ -2,15 +2,14 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include "storedev/storedev.h" #include "storedev/storedev.h"
#include "mbr.h" #include "diskpart/mbr.h"
#include "diskpart.h" #include "diskpart/diskpart.h"
#include "compiler/attr.h" #include "compiler/attr.h"
#include "errors.h"
#include "kprintf.h"
#include "hal/hal.h"
#include "util/util.h" #include "util/util.h"
#include "dev/dev.h" #include "dev/dev.h"
#include "std/string.h" #include "std/string.h"
#include "errors.h"
#include "kprintf.h"
typedef struct { typedef struct {
uint8_t driveattrs; uint8_t driveattrs;

View File

@ -1,15 +1,16 @@
// Config // Config
#include <stddef.h> #include <stddef.h>
#include "hal/hal.h"
#include "spinlock/spinlock.h" #include "spinlock/spinlock.h"
#include "kprintf.h"
#include "bitmap/bitmap.h" #include "bitmap/bitmap.h"
#include "util/util.h" #include "util/util.h"
#include "pmm/pmm.h" #include "pmm/pmm.h"
#include "malloc.h" #include "dlmalloc/malloc.h"
#include "bootinfo/bootinfo.h" #include "bootinfo/bootinfo.h"
#include "std/string.h" #include "std/string.h"
#include "vmm/vmm.h"
#include "cpu/hang.h"
#include "kprintf.h"
#define USE_DL_PREFIX 1 #define USE_DL_PREFIX 1
#define LACKS_SYS_TYPES_H 1 #define LACKS_SYS_TYPES_H 1
@ -27,7 +28,7 @@
#define ABORT \ #define ABORT \
do { \ do { \
ERR("dlmalloc", "Aborting..."); \ ERR("dlmalloc", "Aborting..."); \
hal_hang(); \ cpu_hang(); \
} while(0) } while(0)
#define MALLOC_FAILURE_ACTION #define MALLOC_FAILURE_ACTION
#define HAVE_MORECORE 1 #define HAVE_MORECORE 1

View File

@ -2,11 +2,10 @@
#include <stdbool.h> #include <stdbool.h>
#include "fs/littlefs/lfs.h" #include "fs/littlefs/lfs.h"
#include "vfs/vfs.h" #include "vfs/vfs.h"
#include "dlmalloc/malloc.h"
#include "std/string.h"
#include "errors.h" #include "errors.h"
#include "kprintf.h" #include "kprintf.h"
#include "dlmalloc/malloc.h"
#include "hal/hal.h"
#include "std/string.h"
int32_t littlefs_cleanup(struct VfsMountPoint *vmp) { int32_t littlefs_cleanup(struct VfsMountPoint *vmp) {
dlfree((void *)vmp->fs.littlefs.instance.cfg); dlfree((void *)vmp->fs.littlefs.instance.cfg);

View File

@ -1,22 +0,0 @@
#ifndef KERNEL_HAL_HAL_H_
#define KERNEL_HAL_HAL_H_
#include <stdint.h>
#include <stddef.h>
__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_

View File

@ -1,27 +0,0 @@
#include <stdint.h>
#include <stdbool.h>
#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);
}

View File

@ -1,4 +0,0 @@
.global hal_loadpd
hal_loadpd:
mov %rdi, %cr3
retq

View File

@ -1,6 +0,0 @@
#ifndef HAL_PAGING_H_
#define HAL_PAGING_H_
void hal_loadpd(PgTable *cr3);
#endif // HAL_PAGING_H_

View File

@ -1,22 +0,0 @@
#include <stddef.h>
#include <stdint.h>
#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);
}

View File

@ -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_

View File

@ -1,11 +0,0 @@
#ifndef HAL_PIT_H_
#define HAL_PIT_H_
#include <stdint.h>
extern volatile uint32_t PIT_TICKS;
void pit_init(void);
void pit_wait(uint32_t ms);
#endif // HAL_PIT_H_

View File

@ -1,6 +0,0 @@
#ifndef HAL_SWITCH_H_
#define HAL_SWITCH_H_
extern void hal_switchproc(void *newsp, PgTable *cr3);
#endif // HAL_SWITCH_H_

View File

@ -1,129 +0,0 @@
#include <stddef.h>
#include <stdint.h>
#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();
}

View File

@ -4,7 +4,6 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include "hal/hal.h"
#include "util/util.h" #include "util/util.h"
#include "std/string.h" #include "std/string.h"

View File

@ -1,21 +1,21 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include "intr.h" #include "intr/intr.h"
#include "intr/pic.h"
#include "intr/pit.h"
#include "io/io.h" #include "io/io.h"
#include "gdt.h" #include "cpu/gdt.h"
#include "hal/hal.h"
#include "kprintf.h"
#include "compiler/attr.h" #include "compiler/attr.h"
#include "pic.h"
#include "pit.h"
#include "proc/proc.h" #include "proc/proc.h"
#include "syscall/syscall.h" #include "syscall/syscall.h"
#include "errors.h"
#include "ipc/pipe/pipe.h" #include "ipc/pipe/pipe.h"
#include "rbuf/rbuf.h" #include "rbuf/rbuf.h"
#include "dlmalloc/malloc.h" #include "dlmalloc/malloc.h"
#include "util/util.h" #include "util/util.h"
#include "cpu/hang.h"
#include "kprintf.h"
#include "errors.h"
typedef struct IntrHandler { typedef struct IntrHandler {
struct IntrHandler *next; struct IntrHandler *next;
@ -45,11 +45,11 @@ void backtrace(BackTraceFrame *bt) {
} }
} }
void hal_intr_disable(void) { void intr_disable(void) {
asm volatile("cli"); asm volatile("cli");
} }
void hal_intr_enable(void) { void intr_enable(void) {
asm volatile("sti"); asm volatile("sti");
} }
@ -160,6 +160,9 @@ void intr_init(void) {
idt_setentry(0x80, (uint64_t)&intr_vec128, 0xEE); idt_setentry(0x80, (uint64_t)&intr_vec128, 0xEE);
idt_init(); idt_init();
intr_pic_init();
intr_pit_init();
intr_disable();
} }
void intr_dumpframe(IntrStackFrame *frame) { 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; uint64_t sysnum = frame->regs.rax;
if (sysnum < SYSCALLS_MAX) { if (sysnum < SYSCALLS_MAX) {
SyscallFn fn = SYSCALL_TABLE[sysnum]; SyscallFn fn = SYSCALL_TABLE[sysnum];
@ -194,7 +197,7 @@ void hal_syscalldispatch(IntrStackFrame *frame) {
return; return;
} }
uint64_t calling_proc_pid = PROCS.current->pid; 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, 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); 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) { void intr_handleintr(IntrStackFrame *frame) {
if (frame->trapnum <= 31) { if (frame->trapnum <= 31) {
kprintf("ERROR %s, 0x%lX\n", exceptions[frame->trapnum], frame->errnum); kprintf("ERROR %s, 0x%lX\n", exceptions[frame->trapnum], frame->errnum);
@ -221,13 +219,13 @@ void intr_handleintr(IntrStackFrame *frame) {
proc_sched((void *)frame); proc_sched((void *)frame);
} else { } else {
kprintf("Kernel error :(\n"); kprintf("Kernel error :(\n");
hal_hang(); cpu_hang();
} }
} else if (frame->trapnum >= 32 && frame->trapnum <= 47) { } else if (frame->trapnum >= 32 && frame->trapnum <= 47) {
switch (frame->trapnum) { switch (frame->trapnum) {
case INTR_IRQBASE+0: case INTR_IRQBASE+0:
PIT_TICKS++; PIT_TICKS++;
intr_eoi(); intr_pic_eoi();
proc_sched((void *)frame); proc_sched((void *)frame);
break; break;
default: default:
@ -237,11 +235,11 @@ void intr_handleintr(IntrStackFrame *frame) {
ih->fn(); ih->fn();
} }
} }
intr_eoi(); intr_pic_eoi();
break; break;
} }
} else if (frame->trapnum == 0x80) { } else if (frame->trapnum == 0x80) {
hal_syscalldispatch(frame); intr_syscalldispatch(frame);
} }
} }

View File

@ -1,10 +1,11 @@
#ifndef HAL_INTR_H_ #ifndef INTR_INTR_H_
#define HAL_INTR_H_ #define INTR_INTR_H_
#include <stdint.h>
#include "compiler/attr.h"
#define INTR_IRQBASE 0x20 #define INTR_IRQBASE 0x20
#include "compiler/attr.h"
typedef struct { typedef struct {
uint64_t r15; uint64_t r15;
uint64_t r14; uint64_t r14;
@ -34,6 +35,8 @@ typedef struct {
} PACKED IntrStackFrame; } PACKED IntrStackFrame;
void intr_attchhandler(void (*fn)(void), int irq); void intr_attchhandler(void (*fn)(void), int irq);
void intr_disable(void);
void intr_enable(void);
void intr_init(void); void intr_init(void);
#endif // HAL_INTR_H_ #endif // INTR_INTR_H_

View File

@ -1,4 +1,4 @@
#include "regs.S" #include "cpu/regs.S"
.extern intr_handleintr .extern intr_handleintr

46
kernel/intr/pic.c Normal file
View File

@ -0,0 +1,46 @@
#include <stddef.h>
#include <stdint.h>
#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);
}

7
kernel/intr/pic.h Normal file
View File

@ -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_

View File

@ -1,5 +1,5 @@
#include <stdint.h> #include <stdint.h>
#include "pit.h" #include "intr/pit.h"
#include "io/io.h" #include "io/io.h"
#define PIT_COUNTER0 0x40 #define PIT_COUNTER0 0x40
@ -25,7 +25,7 @@
volatile uint32_t PIT_TICKS; volatile uint32_t PIT_TICKS;
void pit_init(void) { void intr_pit_init(void) {
uint32_t hz = 1000; uint32_t hz = 1000;
uint32_t div = PIT_FREQ / hz; uint32_t div = PIT_FREQ / hz;
io_out8(PIT_CMD, PIT_CMD_BINARY | PIT_CMD_MODE3 | PIT_CMD_RW_BOTH | PIT_CMD_COUNTER0); 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); io_out8(PIT_COUNTER0, div >> 8);
} }
void pit_wait(uint32_t ms) { void intr_pit_wait(uint32_t ms) {
uint32_t now = PIT_TICKS; uint32_t now = PIT_TICKS;
while (PIT_TICKS - now < ms); while (PIT_TICKS - now < ms);
} }

11
kernel/intr/pit.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef INTR_PIT_H_
#define INTR_PIT_H_
#include <stdint.h>
extern volatile uint32_t PIT_TICKS;
void intr_pit_init(void);
void intr_pit_wait(uint32_t ms);
#endif // INTR_PIT_H_

View File

@ -1,5 +1,5 @@
#include <stdint.h> #include <stdint.h>
#include "io.h" #include "io/io.h"
uint8_t io_in8(uint16_t port) { uint8_t io_in8(uint16_t port) {
uint8_t r; uint8_t r;

View File

@ -2,12 +2,11 @@
#include <stddef.h> #include <stddef.h>
#include "rbuf/rbuf.h" #include "rbuf/rbuf.h"
#include "spinlock/spinlock.h" #include "spinlock/spinlock.h"
#include "hal/hal.h"
#include "dlmalloc/malloc.h" #include "dlmalloc/malloc.h"
#include "errors.h" #include "ipc/pipe/pipe.h"
#include "pipe.h"
#include "kprintf.h"
#include "std/string.h" #include "std/string.h"
#include "kprintf.h"
#include "errors.h"
int32_t ipc_pipeinit(IpcPipe *pipe, uint64_t pid) { int32_t ipc_pipeinit(IpcPipe *pipe, uint64_t pid) {
memset(pipe, 0, sizeof(*pipe)); memset(pipe, 0, sizeof(*pipe));

View File

@ -1,7 +1,6 @@
#include <limine.h> #include <limine.h>
#include "kprintf.h" #include "kprintf.h"
#include "banner.h" #include "banner.h"
#include "hal/hal.h"
#include "bootinfo/bootinfo.h" #include "bootinfo/bootinfo.h"
#include "pmm/pmm.h" #include "pmm/pmm.h"
#include "term/term.h" #include "term/term.h"
@ -15,6 +14,9 @@
#include "randcrypto/randcrypto.h" #include "randcrypto/randcrypto.h"
#include "time/time.h" #include "time/time.h"
#include "diskpart/diskpart.h" #include "diskpart/diskpart.h"
#include "vmm/vmm.h"
#include "cpu/hang.h"
#include "cpu/gdt.h"
void log_bootinfo(void) { void log_bootinfo(void) {
char buf[100]; char buf[100];
@ -34,16 +36,17 @@ static volatile LIMINE_BASE_REVISION(2);
void kmain(void) { void kmain(void) {
if (LIMINE_BASE_REVISION_SUPPORTED == false) { if (LIMINE_BASE_REVISION_SUPPORTED == false) {
hal_hang(); cpu_hang();
} }
bootinfo_init(); bootinfo_init();
term_init(BOOT_INFO.fb->address); term_init(BOOT_INFO.fb->address);
log_bootinfo(); log_bootinfo();
log_time(); log_time();
hal_init(); gdt_init();
intr_init();
pmm_init(); pmm_init();
hal_vmm_init(); vmm_init();
randcrypto_init(); randcrypto_init();
dev_init(); dev_init();
storedev_init(); storedev_init();

View File

@ -1,5 +1,4 @@
#include "path.h" #include "path.h"
#include "hal/hal.h"
#include "std/string.h" #include "std/string.h"
void path_parse(const char *in, char *mp, char *path) { void path_parse(const char *in, char *mp, char *path) {

View File

@ -1,13 +1,13 @@
#include <stddef.h> #include <stddef.h>
#include <limine.h> #include <limine.h>
#include "pmm.h" #include "pmm/pmm.h"
#include "kprintf.h"
#include "bitmap/bitmap.h" #include "bitmap/bitmap.h"
#include "bootinfo/bootinfo.h" #include "bootinfo/bootinfo.h"
#include "spinlock/spinlock.h" #include "spinlock/spinlock.h"
#include "util/util.h" #include "util/util.h"
#include "hal/hal.h"
#include "std/string.h" #include "std/string.h"
#include "cpu/hang.h"
#include "kprintf.h"
PhysMem PHYS_MEM; PhysMem PHYS_MEM;
@ -32,7 +32,7 @@ void pmm_init(void) {
if (!memmap_ent) { if (!memmap_ent) {
ERR("pmm", "required memory: {%lx}\n", bm->nbytes); ERR("pmm", "required memory: {%lx}\n", bm->nbytes);
hal_hang(); cpu_hang();
} }
size_t physbegin = memmap_ent->base; size_t physbegin = memmap_ent->base;

View File

@ -1,15 +1,11 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include "hal/hal.h"
#include "spinlock/spinlock.h" #include "spinlock/spinlock.h"
#include "proc.h" #include "proc/proc.h"
#include "dlmalloc/malloc.h" #include "dlmalloc/malloc.h"
#include "pmm/pmm.h" #include "pmm/pmm.h"
#include "util/util.h" #include "util/util.h"
#include "kprintf.h"
#include "elf.h"
#include "errors.h"
#include "vfs/vfs.h" #include "vfs/vfs.h"
#include "bootinfo/bootinfo.h" #include "bootinfo/bootinfo.h"
#include "ipc/pipe/pipe.h" #include "ipc/pipe/pipe.h"
@ -17,6 +13,13 @@
#include "sysdefs/fs.h" #include "sysdefs/fs.h"
#include "time/time.h" #include "time/time.h"
#include "std/string.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 #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; aux.phdr = (uint64_t)phdr->p_vaddr;
} break; } break;
case PT_LOAD: { case PT_LOAD: {
uint64_t off = phdr->p_vaddr & (HAL_PAGE_SIZE - 1); uint64_t off = phdr->p_vaddr & (VMM_PAGE_SIZE - 1);
uint64_t blocks = (off + phdr->p_memsz + HAL_PAGE_SIZE - 1) / HAL_PAGE_SIZE; uint64_t blocks = (off + phdr->p_memsz + VMM_PAGE_SIZE - 1) / VMM_PAGE_SIZE;
uint8_t *physaddr = pmm_alloc(blocks); 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); 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) { 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)); VasRange *range = dlmalloc(sizeof(*range));
range->virtstart = virtaddr; range->virtstart = virtaddr;
range->physstart = physaddr; range->physstart = physaddr;
range->size = blocks * HAL_PAGE_SIZE; range->size = blocks * VMM_PAGE_SIZE;
range->pgflags = pgflags; range->pgflags = pgflags;
LL_APPEND(proc->vas, range); LL_APPEND(proc->vas, range);
@ -110,7 +113,7 @@ Proc *proc_spawnuser(char *mountpoint, char *path) {
ksprintf(proc->name, "%s:%s", mountpoint, path); ksprintf(proc->name, "%s:%s", mountpoint, path);
memset(&proc->platformdata.trapframe, 0, sizeof(proc->platformdata.trapframe)); 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; uint8_t *kstackp = (uint8_t *)pmm_alloc(PROC_STACKBLOCKS) + PROC_STACKSIZE;
proc->platformdata.kstack = kstackp; proc->platformdata.kstack = kstackp;
@ -120,8 +123,8 @@ Proc *proc_spawnuser(char *mountpoint, char *path) {
uint64_t virttop = PROC_USER_STACK_TOP; uint64_t virttop = PROC_USER_STACK_TOP;
uint64_t virtbase = virttop - PROC_STACKSIZE; uint64_t virtbase = virttop - PROC_STACKSIZE;
uint32_t flags = HAL_PG_RW | HAL_PG_PRESENT | HAL_PG_USER; uint32_t flags = VMM_PG_RW | VMM_PG_PRESENT | VMM_PG_USER;
hal_vmm_map_range(proc->platformdata.cr3, (void *)virtbase, (void *)pstackp, PROC_STACKSIZE, flags); vmm_map_range(proc->platformdata.cr3, (void *)virtbase, (void *)pstackp, PROC_STACKSIZE, flags);
VasRange *range = dlmalloc(sizeof(*range)); VasRange *range = dlmalloc(sizeof(*range));
range->virtstart = (uint8_t *)virtbase; range->virtstart = (uint8_t *)virtbase;
@ -203,8 +206,8 @@ void proc_reaper(void) {
VasRange *vas, *vastmp; VasRange *vas, *vastmp;
LL_FOREACH_SAFE(zombie->vas, vas, vastmp) { LL_FOREACH_SAFE(zombie->vas, vas, vastmp) {
hal_vmm_unmap_range(zombie->platformdata.cr3, vas->virtstart, vas->physstart, vas->size); vmm_unmap_range(zombie->platformdata.cr3, vas->virtstart, vas->physstart, vas->size);
pmm_free((uintptr_t)vas->physstart, vas->size / HAL_PAGE_SIZE); pmm_free((uintptr_t)vas->physstart, vas->size / VMM_PAGE_SIZE);
dlfree(vas); dlfree(vas);
} }
@ -221,7 +224,7 @@ void proc_reaper(void) {
} }
void proc_sched(void *cpustate) { void proc_sched(void *cpustate) {
hal_intr_disable(); intr_disable();
sched_ticks++; sched_ticks++;
memcpy(&PROCS.current->platformdata.trapframe, cpustate, sizeof(IntrStackFrame)); 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); 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) { void proc_kill(Proc *proc) {
@ -259,5 +262,5 @@ void proc_init(void) {
init->state = PROC_READY; init->state = PROC_READY;
tss.rsp0 = (uint64_t)VIRT(PROCS.current->platformdata.kstack); 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);
} }

View File

@ -2,7 +2,6 @@
#define PROC_PROC_H_ #define PROC_PROC_H_
#include <stdint.h> #include <stdint.h>
#include "hal/hal.h"
#include "spinlock/spinlock.h" #include "spinlock/spinlock.h"
#include "bitmap/bitmap.h" #include "bitmap/bitmap.h"
#include "vfs/vfs.h" #include "vfs/vfs.h"
@ -11,6 +10,8 @@
#include "sysdefs/time.h" #include "sysdefs/time.h"
#include "dev/dev.h" #include "dev/dev.h"
#include "std/string.h" #include "std/string.h"
#include "intr/intr.h"
#include "vmm/vmm.h"
#define PROC_NAME_MAX 0x100 #define PROC_NAME_MAX 0x100

View File

@ -1,7 +1,7 @@
#include "regs.S" #include "cpu/regs.S"
.global hal_switchproc .global proc_switch
hal_switchproc: proc_switch:
testq %rsi, %rsi testq %rsi, %rsi
je 1f je 1f
movq %rsi, %cr3 movq %rsi, %cr3

8
kernel/proc/switch.h Normal file
View File

@ -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_

View File

@ -1,5 +1,5 @@
.global hal_randnum .global randcrypto_get_physrand
hal_randnum: randcrypto_get_physrand:
mov $100, %ecx mov $100, %ecx
xor %eax, %eax xor %eax, %eax

View File

@ -0,0 +1,8 @@
#ifndef RANDCRYPTO_PHYSDRAND_H_
#define RANDCRYPTO_PHYSDRAND_H_
#include <stdint.h>
int32_t randcrypto_get_physrand(void);
#endif // RANDCRYPTO_PHYSDRAND_H_

View File

@ -1,5 +1,5 @@
#include "randcrypto.h" #include "randcrypto/randcrypto.h"
#include "uniqid.h" #include "randcrypto/uniqid.h"
void randcrypto_init(void) { void randcrypto_init(void) {
randcrypto_uniqid_init(); randcrypto_uniqid_init();

View File

@ -1,9 +1,8 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include "uniqid.h" #include "randcrypto/uniqid.h"
#include "atomic.h" #include "randcrypto/physrand.h"
#include "spinlock/spinlock.h" #include "spinlock/spinlock.h"
#include "hal/hal.h"
static const char *base62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; static const char *base62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
@ -28,7 +27,7 @@ void uniqseed(void) {
return; return;
uint32_t seed = 0; uint32_t seed = 0;
int32_t r = hal_randnum(); int32_t r = randcrypto_get_physrand();
if (r != -1) { if (r != -1) {
seed = (uint32_t)r; seed = (uint32_t)r;
} else { } else {
@ -48,7 +47,7 @@ void randcrypto_gen_uniqid(char *out, size_t n) {
uniqseed(); uniqseed();
int32_t r = hal_randnum(); int32_t r = randcrypto_get_physrand();
uint32_t extra = (r != -1) ? (uint32_t)r : uniqstate * 0x27d4eb2dU; uint32_t extra = (r != -1) ? (uint32_t)r : uniqstate * 0x27d4eb2dU;
uniqstate += 0x9E3779B1u; uniqstate += 0x9E3779B1u;

View File

@ -1,9 +1,8 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include <stdbool.h> #include <stdbool.h>
#include "rbuf.h" #include "rbuf/rbuf.h"
#include "kprintf.h" #include "kprintf.h"
#include "hal/hal.h"
void rbuf_init(RBuf *rbuf, uint8_t *buf, size_t bufsize) { void rbuf_init(RBuf *rbuf, uint8_t *buf, size_t bufsize) {
rbuf->buffer = buf; rbuf->buffer = buf;

View File

@ -1,8 +1,7 @@
#include <stdatomic.h> #include <stdatomic.h>
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include "spinlock.h" #include "spinlock/spinlock.h"
#include "hal/hal.h"
#include "kprintf.h" #include "kprintf.h"
#define SPINLOCK_HINT() asm volatile("pause") #define SPINLOCK_HINT() asm volatile("pause")

View File

@ -1,6 +1,5 @@
#include <stdlib.h> #include <stdlib.h>
#include "dlmalloc/malloc.h" #include "dlmalloc/malloc.h"
#include "hal/hal.h"
void *malloc(size_t size) { void *malloc(size_t size) {
return dlmalloc(size); return dlmalloc(size);

View File

@ -1,15 +1,14 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include <stdbool.h> #include <stdbool.h>
#include "atasd.h" #include "storedev/atasd.h"
#include "storedev.h" #include "storedev/storedev.h"
#include "hal/hal.h"
#include "util/util.h" #include "util/util.h"
#include "vfs/vfs.h"
#include "io/io.h"
#include "kprintf.h" #include "kprintf.h"
#include "errors.h" #include "errors.h"
#include "hshtb.h" #include "hshtb.h"
#include "vfs/vfs.h"
#include "io/io.h"
#define ATA_REG_DATA 0x00 #define ATA_REG_DATA 0x00
#define ATA_REG_ERROR 0x01 #define ATA_REG_ERROR 0x01

View File

@ -1,7 +1,7 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include "partsd.h" #include "storedev/partsd.h"
#include "storedev.h" #include "storedev/storedev.h"
#include "errors.h" #include "errors.h"
#include "kprintf.h" #include "kprintf.h"

View File

@ -1,14 +1,13 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include "spinlock/spinlock.h" #include "spinlock/spinlock.h"
#include "errors.h"
#include "dlmalloc/malloc.h" #include "dlmalloc/malloc.h"
#include "ramsd.h" #include "storedev/ramsd.h"
#include "storedev.h" #include "storedev/storedev.h"
#include "hal/hal.h"
#include "util/util.h" #include "util/util.h"
#include "kprintf.h"
#include "std/string.h" #include "std/string.h"
#include "kprintf.h"
#include "errors.h"
int32_t ramsd_init(struct StoreDev *sd, void *extra) { int32_t ramsd_init(struct StoreDev *sd, void *extra) {
RamSdInitExtra *e = extra; RamSdInitExtra *e = extra;

View File

@ -2,17 +2,17 @@
#include <stddef.h> #include <stddef.h>
#include "storedev.h" #include "storedev.h"
#include "spinlock/spinlock.h" #include "spinlock/spinlock.h"
#include "kprintf.h"
#include "errors.h"
#include "dlmalloc/malloc.h" #include "dlmalloc/malloc.h"
#include "ramsd.h" #include "storedev/ramsd.h"
#include "atasd.h" #include "storedev/atasd.h"
#include "storedev/partsd.h"
#include "util/util.h" #include "util/util.h"
#include "dev/dev.h" #include "dev/dev.h"
#include "hshtb.h"
#include "hal/hal.h"
#include "randcrypto/uniqid.h" #include "randcrypto/uniqid.h"
#include "sysdefs/dev.h" #include "sysdefs/dev.h"
#include "kprintf.h"
#include "hshtb.h"
#include "errors.h"
StoreDevList STOREDEV_LIST; StoreDevList STOREDEV_LIST;

View File

@ -4,9 +4,9 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include "spinlock/spinlock.h" #include "spinlock/spinlock.h"
#include "ramsd.h" #include "storedev/ramsd.h"
#include "atasd.h" #include "storedev/atasd.h"
#include "partsd.h" #include "storedev/partsd.h"
#include "compiler/attr.h" #include "compiler/attr.h"
#include "dev/dev.h" #include "dev/dev.h"

View File

@ -1,15 +1,15 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include "kprintf.h" #include "syscall/syscall.h"
#include "syscall.h"
#include "errors.h"
#include "spinlock/spinlock.h" #include "spinlock/spinlock.h"
#include "proc/proc.h" #include "proc/proc.h"
#include "sysdefs/dev.h" #include "sysdefs/dev.h"
#include "util/util.h" #include "util/util.h"
#include "hshtb.h"
#include "dev/dev.h" #include "dev/dev.h"
#include "std/string.h" #include "std/string.h"
#include "errors.h"
#include "kprintf.h"
#include "hshtb.h"
int32_t SYSCALL2(sys_dev_gethandle, dev1, devname1) { int32_t SYSCALL2(sys_dev_gethandle, dev1, devname1) {
uint64_t *devh = (uint64_t *)dev1; uint64_t *devh = (uint64_t *)dev1;

View File

@ -3,7 +3,7 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include "syscall.h" #include "syscall/syscall.h"
int32_t SYSCALL2(sys_dev_gethandle, dev1, devname1); int32_t SYSCALL2(sys_dev_gethandle, dev1, devname1);
int32_t SYSCALL0(sys_dev_listsize); int32_t SYSCALL0(sys_dev_listsize);

View File

@ -1,14 +1,14 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include "syscall.h" #include "syscall/syscall.h"
#include "sysdefs/fs.h" #include "sysdefs/fs.h"
#include "errors.h"
#include "path/path.h" #include "path/path.h"
#include "vfs/vfs.h" #include "vfs/vfs.h"
#include "kprintf.h"
#include "proc/proc.h" #include "proc/proc.h"
#include "spinlock/spinlock.h" #include "spinlock/spinlock.h"
#include "util/util.h" #include "util/util.h"
#include "errors.h"
#include "kprintf.h"
#define _MP_MAX 0xff #define _MP_MAX 0xff
#define _PATH_MAX VFS_PATH_MAX #define _PATH_MAX VFS_PATH_MAX

View File

@ -3,7 +3,7 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include "syscall.h" #include "syscall/syscall.h"
int32_t SYSCALL2(sys_fs_openf, opath1, oflags1); int32_t SYSCALL2(sys_fs_openf, opath1, oflags1);
int32_t SYSCALL1(sys_fs_closef, fsh1); int32_t SYSCALL1(sys_fs_closef, fsh1);

View File

@ -1,12 +1,12 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include "ipcpipe.h" #include "syscall/ipcpipe.h"
#include "ipc/pipe/pipe.h" #include "ipc/pipe/pipe.h"
#include "dlmalloc/malloc.h" #include "dlmalloc/malloc.h"
#include "proc/proc.h" #include "proc/proc.h"
#include "spinlock/spinlock.h" #include "spinlock/spinlock.h"
#include "errors.h"
#include "util/util.h" #include "util/util.h"
#include "errors.h"
#include "kprintf.h" #include "kprintf.h"
int32_t SYSCALL4(sys_ipc_piperead, pid1, pipenum1, buffer1, len1) { int32_t SYSCALL4(sys_ipc_piperead, pid1, pipenum1, buffer1, len1) {

View File

@ -1,7 +1,9 @@
#ifndef SYSCALL_IPCPIPE_H_ #ifndef SYSCALL_IPCPIPE_H_
#define SYSCALL_IPCPIPE_H_ #define SYSCALL_IPCPIPE_H_
#include "syscall.h" #include <stdint.h>
#include <stddef.h>
#include "syscall/syscall.h"
int32_t SYSCALL4(sys_ipc_piperead, pid1, pipenum1, buffer1, len1); int32_t SYSCALL4(sys_ipc_piperead, pid1, pipenum1, buffer1, len1);
int32_t SYSCALL4(sys_ipc_pipewrite, pid1, pipenum1, buffer1, len1); int32_t SYSCALL4(sys_ipc_pipewrite, pid1, pipenum1, buffer1, len1);

View File

@ -1,17 +1,17 @@
#include <stdint.h> #include <stdint.h>
#include "syscall.h" #include "syscall/syscall.h"
#include "mman.h" #include "syscall/mman.h"
#include "spinlock/spinlock.h" #include "spinlock/spinlock.h"
#include "proc/proc.h" #include "proc/proc.h"
#include "hal/hal.h"
#include "pmm/pmm.h" #include "pmm/pmm.h"
#include "util/util.h" #include "util/util.h"
#include "errors.h"
#include "sysdefs/mman.h" #include "sysdefs/mman.h"
#include "bootinfo/bootinfo.h" #include "bootinfo/bootinfo.h"
#include "dlmalloc/malloc.h" #include "dlmalloc/malloc.h"
#include "kprintf.h"
#include "std/string.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) { int32_t SYSCALL5(sys_mman_map, addr1, size1, prot1, flags1, out1) {
uint8_t *addr = (uint8_t *)addr1; 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; uint64_t flags = flags1;
uint8_t **out = (uint8_t **)out1; 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) { if (out != NULL) {
*out = NULL; *out = NULL;
} }
return E_INVALIDARGUMENT; 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); 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); spinlock_acquire(&PROCS.spinlock);
Proc *proc = NULL; Proc *proc = NULL;
@ -38,7 +38,7 @@ int32_t SYSCALL5(sys_mman_map, addr1, size1, prot1, flags1, out1) {
uint8_t *virt = NULL; uint8_t *virt = NULL;
if ((flags & MMAN_MAP_F_FIXED) && addr != 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) { if (out != NULL) {
*out = NULL; *out = NULL;
} }
@ -49,19 +49,19 @@ int32_t SYSCALL5(sys_mman_map, addr1, size1, prot1, flags1, out1) {
virt = addr; virt = addr;
} else { } else {
virt = (uint8_t *)proc->mman_map_base; 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) { 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)); VasRange *range = dlmalloc(sizeof(*range));
range->virtstart = virt; range->virtstart = virt;
range->physstart = phys; range->physstart = phys;
range->size = pages * HAL_PAGE_SIZE; range->size = pages * VMM_PAGE_SIZE;
range->pgflags = pflags; range->pgflags = pflags;
LL_APPEND(proc->vas, range); LL_APPEND(proc->vas, range);
@ -98,9 +98,9 @@ int32_t SYSCALL1(sys_mman_unmap, addr1) {
return E_INVALIDARGUMENT; 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); 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); dlfree(tofree);
return E_OK; return E_OK;
} }

View File

@ -1,7 +1,9 @@
#ifndef SYSCALL_MMAN_H_ #ifndef SYSCALL_MMAN_H_
#define SYSCALL_MMAN_H_ #define SYSCALL_MMAN_H_
#include "syscall.h" #include <stdint.h>
#include <stddef.h>
#include "syscall/syscall.h"
int32_t SYSCALL5(sys_mman_map, addr1, size1, prot1, flags1, out1); int32_t SYSCALL5(sys_mman_map, addr1, size1, prot1, flags1, out1);
int32_t SYSCALL1(sys_mman_unmap, addr1); int32_t SYSCALL1(sys_mman_unmap, addr1);

View File

@ -1,17 +1,17 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include "syscall.h" #include "syscall/syscall.h"
#include "proc/proc.h" #include "proc/proc.h"
#include "spinlock/spinlock.h" #include "spinlock/spinlock.h"
#include "errors.h"
#include "util/util.h" #include "util/util.h"
#include "sysdefs/proc.h" #include "sysdefs/proc.h"
#include "vfs/vfs.h" #include "vfs/vfs.h"
#include "path/path.h" #include "path/path.h"
#include "kprintf.h"
#include "dlmalloc/malloc.h" #include "dlmalloc/malloc.h"
#include "ipc/pipe/pipe.h" #include "ipc/pipe/pipe.h"
#include "std/string.h" #include "std/string.h"
#include "errors.h"
#include "kprintf.h"
#define _MP_MAX 0xff #define _MP_MAX 0xff
#define _PATH_MAX VFS_PATH_MAX #define _PATH_MAX VFS_PATH_MAX

View File

@ -3,7 +3,7 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include "syscall.h" #include "syscall/syscall.h"
int32_t SYSCALL1(sys_proc_kill, pid1); int32_t SYSCALL1(sys_proc_kill, pid1);
int32_t SYSCALL3(sys_proc_spawn, opath1, args1, argslen1); int32_t SYSCALL3(sys_proc_spawn, opath1, args1, argslen1);

View File

@ -1,8 +1,8 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include "hal/hal.h" #include "syscall/syscall.h"
#include "syscall.h" #include "randcrypto/physrand.h"
int32_t SYSCALL0(sys_rand) { int32_t SYSCALL0(sys_rand) {
return hal_randnum(); return randcrypto_get_physrand();
} }

View File

@ -1,7 +1,9 @@
#ifndef SYSCALL_RANDCRYPTO_H_ #ifndef SYSCALL_RANDCRYPTO_H_
#define SYSCALL_RANDCRYPTO_H_ #define SYSCALL_RANDCRYPTO_H_
#include "syscall.h" #include <stdint.h>
#include <stddef.h>
#include "syscall/syscall.h"
int32_t SYSCALL0(sys_rand); int32_t SYSCALL0(sys_rand);

View File

@ -1,10 +1,10 @@
#include <stdint.h> #include <stdint.h>
#include "syscall.h" #include "syscall/syscall.h"
#include "sched.h" #include "syscall/sched.h"
#include "proc/proc.h" #include "proc/proc.h"
#include "spinlock/spinlock.h" #include "spinlock/spinlock.h"
#include "intr/pit.h"
#include "errors.h" #include "errors.h"
#include "hal/hal.h"
int32_t SYSCALL0(sys_schedrelease) { int32_t SYSCALL0(sys_schedrelease) {
return E_DOSCHEDULING; return E_DOSCHEDULING;
@ -12,6 +12,6 @@ int32_t SYSCALL0(sys_schedrelease) {
int32_t SYSCALL1(sys_schedsleep, ms1) { int32_t SYSCALL1(sys_schedsleep, ms1) {
uint32_t ms = (uint32_t)ms1; uint32_t ms = (uint32_t)ms1;
hal_wait(ms); intr_pit_wait(ms);
return E_OK; return E_OK;
} }

View File

@ -2,7 +2,8 @@
#define SYSCALL_SCHED_H_ #define SYSCALL_SCHED_H_
#include <stdint.h> #include <stdint.h>
#include "syscall.h" #include <stddef.h>
#include "syscall/syscall.h"
int32_t SYSCALL0(sys_schedrelease); int32_t SYSCALL0(sys_schedrelease);
int32_t SYSCALL1(sys_schedsleep, ms1); int32_t SYSCALL1(sys_schedsleep, ms1);

View File

@ -1,17 +1,17 @@
#include <stdint.h> #include <stdint.h>
#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 "errors.h"
#include "kprintf.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) { int32_t SYSCALL1(sys_debugprint, string) {
char *p = (char *)string; char *p = (char *)string;

View File

@ -3,7 +3,7 @@
#include <stdint.h> #include <stdint.h>
#include "compiler/attr.h" #include "compiler/attr.h"
#include "hal/hal.h" #include "intr/intr.h"
#define SYSCALLS_MAX 0xff #define SYSCALLS_MAX 0xff

View File

@ -1,7 +1,7 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include "syscall.h" #include "syscall/syscall.h"
#include "time.h" #include "syscall/time.h"
#include "sysdefs/time.h" #include "sysdefs/time.h"
#include "time/time.h" #include "time/time.h"
#include "errors.h" #include "errors.h"

View File

@ -3,6 +3,7 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include "syscall/syscall.h"
int32_t SYSCALL1(sys_time, timestruct1); int32_t SYSCALL1(sys_time, timestruct1);

View File

@ -1,10 +1,8 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include <stdbool.h> #include <stdbool.h>
#include "hal/hal.h" #include "syscall/syscall.h"
#include "syscall.h" #include "syscall/vfs.h"
#include "vfs.h"
#include "errors.h"
#include "sysdefs/dev.h" #include "sysdefs/dev.h"
#include "sysdefs/vfs.h" #include "sysdefs/vfs.h"
#include "vfs/vfs.h" #include "vfs/vfs.h"
@ -13,8 +11,9 @@
#include "proc/proc.h" #include "proc/proc.h"
#include "storedev/storedev.h" #include "storedev/storedev.h"
#include "util/util.h" #include "util/util.h"
#include "hshtb.h"
#include "std/string.h" #include "std/string.h"
#include "hshtb.h"
#include "errors.h"
int32_t SYSCALL4(sys_vfsmount, mountpoint1, fstypestr1, devid1, format1) { int32_t SYSCALL4(sys_vfsmount, mountpoint1, fstypestr1, devid1, format1) {
int32_t ret = E_OK; int32_t ret = E_OK;

View File

@ -2,7 +2,7 @@
#include "flanterm_backends/fb.h" #include "flanterm_backends/fb.h"
#include "spinlock/spinlock.h" #include "spinlock/spinlock.h"
#include "bootinfo/bootinfo.h" #include "bootinfo/bootinfo.h"
#include "term.h" #include "term/term.h"
#include "fm-t-437.f16.h" #include "fm-t-437.f16.h"
Term TERM; Term TERM;

View File

@ -1,7 +1,6 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include "time.h" #include "time/time.h"
#include "hal/hal.h"
#include "std/string.h" #include "std/string.h"
#include "io/io.h" #include "io/io.h"

View File

@ -1,7 +1,7 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include "util/util.h"
#include "kprintf.h" #include "kprintf.h"
#include "util.h"
char *util_get_filename(char *path) { char *util_get_filename(char *path) {
char *lastslash = path; char *lastslash = path;

View File

@ -1,18 +1,16 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include "vfs.h" #include "vfs/vfs.h"
#include "kprintf.h"
#include "spinlock/spinlock.h" #include "spinlock/spinlock.h"
#include "hal/hal.h"
#include "util/util.h" #include "util/util.h"
#include "hshtb.h"
#include "assert.h"
#include "errors.h"
#include "fs/portlfs/portlfs.h" #include "fs/portlfs/portlfs.h"
#include "storedev/storedev.h" #include "storedev/storedev.h"
#include "baseimg/baseimg.h" #include "baseimg/baseimg.h"
#include "dlmalloc/malloc.h" #include "dlmalloc/malloc.h"
#include "std/string.h" #include "std/string.h"
#include "kprintf.h"
#include "errors.h"
#include "hshtb.h"
VfsTable VFS_TABLE; VfsTable VFS_TABLE;
VfsBusyObjs VFS_BUSY_VOBJS; VfsBusyObjs VFS_BUSY_VOBJS;

127
kernel/vmm/vmm.c Normal file
View File

@ -0,0 +1,127 @@
#include <stddef.h>
#include <stdint.h>
#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();
}

View File

@ -1,11 +1,13 @@
#ifndef HAL_VMM_H_ #ifndef VMM_VMM_H_
#define HAL_VMM_H_ #define VMM_VMM_H_
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#include "compiler/attr.h" #include "compiler/attr.h"
#define VMM_PAGE_SIZE 0x1000
#define VIRT(X) ((void *)(BOOT_INFO.hhdm_off + (uint64_t)(X))) #define VIRT(X) ((void *)(BOOT_INFO.hhdm_off + (uint64_t)(X)))
typedef struct VasRange { typedef struct VasRange {
@ -18,9 +20,9 @@ typedef struct VasRange {
} PACKED VasRange; } PACKED VasRange;
enum { enum {
HAL_PG_PRESENT = 1<<0, VMM_PG_PRESENT = 1<<0,
HAL_PG_RW = 1<<1, VMM_PG_RW = 1<<1,
HAL_PG_USER = 1<<2, VMM_PG_USER = 1<<2,
}; };
typedef struct { typedef struct {
@ -52,12 +54,12 @@ typedef struct {
extern uint64_t KERNEL_CR3; extern uint64_t KERNEL_CR3;
void hal_vmm_init(void); void vmm_init(void);
void hal_vmm_unmap_page(uint64_t cr3phys, uint64_t virtaddr); void 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); void vmm_map_page(uint64_t cr3phys, uint64_t virtaddr, uint64_t physaddr, uint32_t flags);
uint64_t hal_vmm_current_cr3(void); uint64_t vmm_current_cr3(void);
void hal_vmm_map_range(uint64_t cr3phys, void *virtstart, void *physstart, size_t size, uint32_t flags); void 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); void vmm_unmap_range(uint64_t cr3phys, void *virtstart, void *physstart, size_t size);
uint64_t hal_vmm_userproc_pml4_phys(); uint64_t vmm_userproc_pml4_phys(void);
#endif // HAL_VMM_H_ #endif // VMM_VMM_H_