Compare commits

..

3 Commits

Author SHA1 Message Date
566b35f4d5 Big code refactor, get rid of HAL entirely 2025-11-11 21:26:27 +01:00
7015bc9576 Decouple I/O from HAL 2025-11-11 19:59:01 +01:00
344952fb5f Move string functions/utils from HAL to std/string 2025-11-11 19:54:09 +01:00
88 changed files with 673 additions and 739 deletions

View File

@ -1,7 +1,7 @@
#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"
#define SIXPACK_OK 0 #define SIXPACK_OK 0
#define SIXPACK_ERR_MAGIC -1 #define SIXPACK_ERR_MAGIC -1
@ -93,7 +93,7 @@ int sixpack_decompress_mem(const uint8_t *input, size_t in_size,
/* Stored (uncompressed) */ /* Stored (uncompressed) */
if (written + size > out_cap) if (written + size > out_cap)
return SIXPACK_ERR_NO_SPACE; return SIXPACK_ERR_NO_SPACE;
hal_memcpy(output + written, chunk_data, size); memcpy(output + written, chunk_data, size);
written += size; written += size;
} else if (opts == 1) { } else if (opts == 1) {
/* FastLZ compressed */ /* FastLZ compressed */

View File

@ -46,8 +46,6 @@ SRCFILES += $(call GRABSRC, \
fs/portlfs \ fs/portlfs \
baseimg \ baseimg \
proc \ proc \
hal \
hal/$(ARCH) \
std \ std \
flanterm/src \ flanterm/src \
flanterm/src/flanterm_backends \ flanterm/src/flanterm_backends \
@ -60,6 +58,10 @@ SRCFILES += $(call GRABSRC, \
time \ time \
diskpart \ diskpart \
FastLZ \ FastLZ \
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,14 +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 "cpu/hang.h"
#include "kprintf.h"
#define BASEIMG_DECOMPRESSED (1024*1024*4) #define BASEIMG_DECOMPRESSED (1024*1024*4)
@ -29,7 +30,7 @@ void baseimg_init(void) {
LOG("baseimg", "looking for base image...\n"); LOG("baseimg", "looking for base image...\n");
for (size_t i = 0; i < BOOT_INFO.modules->module_count; i++) { for (size_t i = 0; i < BOOT_INFO.modules->module_count; i++) {
struct limine_file *module = BOOT_INFO.modules->modules[i]; struct limine_file *module = BOOT_INFO.modules->modules[i];
if (hal_strcmp(util_get_filename(module->path), "base.img.6pk") == 0) { if (strcmp(util_get_filename(module->path), "base.img.6pk") == 0) {
baseimg = module; baseimg = module;
break; break;
} }
@ -37,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);
@ -54,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,8 @@
#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"
#define GDT_PRESENT 0x80 #define GDT_PRESENT 0x80
#define GDT_TSS 0x89 #define GDT_TSS 0x89
@ -43,7 +43,7 @@ void gdt_setentry(GdtEntry *ent, uint32_t base, uint32_t limit, uint8_t acc, uin
} }
void gdt_init(void) { void gdt_init(void) {
hal_memset(&tss, 0, sizeof(tss)); memset(&tss, 0, sizeof(tss));
tss.iopb_off = sizeof(tss); tss.iopb_off = sizeof(tss);
tss.rsp0 = (uint64_t)(kernelstack + sizeof(kernelstack)); tss.rsp0 = (uint64_t)(kernelstack + sizeof(kernelstack));

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,18 +1,18 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include "spinlock/spinlock.h" #include "spinlock/spinlock.h"
#include "dev.h" #include "std/string.h"
#include "dev/dev.h"
#include "dev/termdev.h"
#include "dev/ps2kbdev.h"
#include "dev/serialdev.h"
#include "dev/fbdev.h"
#include "hshtb.h" #include "hshtb.h"
#include "hal/hal.h"
#include "termdev.h"
#include "ps2kbdev.h"
#include "serialdev.h"
#include "fbdev.h"
DevTable DEVTABLE; DevTable DEVTABLE;
void dev_init(void) { void dev_init(void) {
hal_memset(&DEVTABLE, 0, sizeof(DEVTABLE)); memset(&DEVTABLE, 0, sizeof(DEVTABLE));
spinlock_init(&DEVTABLE.spinlock); spinlock_init(&DEVTABLE.spinlock);
termdev_init(); termdev_init();

View File

@ -1,14 +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 "std/string.h"
#include "errors.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;
@ -24,7 +24,7 @@ int32_t fbdev_getinfo(struct Dev *dev, uint8_t *buffer, size_t len, uint64_t pid
.fontw = 8, .fontw = 8,
.fonth = 16, .fonth = 16,
}; };
hal_memcpy(buffer, &info, sizeof(info)); memcpy(buffer, &info, sizeof(info));
return E_OK; return E_OK;
} }

View File

@ -1,15 +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 "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,13 +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 "io/io.h"
#include "errors.h"
#include "hshtb.h"
#include "kprintf.h" #include "kprintf.h"
#include "hal/hal.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,14 +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 "errors.h"
#include "kprintf.h"
typedef struct { typedef struct {
uint8_t driveattrs; uint8_t driveattrs;
@ -62,7 +62,7 @@ bool diskpart_mbr_probe(StoreDev *sd) {
LOG("diskpart", "probing for MBR\n"); LOG("diskpart", "probing for MBR\n");
MBR mbr; MBR mbr;
hal_memset(&mbr, 0, sizeof(mbr)); memset(&mbr, 0, sizeof(mbr));
sd->read(sd, (uint8_t *const)&mbr, 0, 0, sizeof(mbr)); sd->read(sd, (uint8_t *const)&mbr, 0, 0, sizeof(mbr));
if (!(mbr.validsign[0] == 0x55 && mbr.validsign[1] == 0xAA)) { if (!(mbr.validsign[0] == 0x55 && mbr.validsign[1] == 0xAA)) {

View File

@ -1,14 +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 "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
@ -26,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
@ -67,7 +69,7 @@ void *sbrk(long inc) {
uint64_t blocks = _DIV_ROUNDUP(inc, BITMAP_BLOCK_SIZE); uint64_t blocks = _DIV_ROUNDUP(inc, BITMAP_BLOCK_SIZE);
uint8_t *virt = VIRT(pmm_alloc(blocks)); uint8_t *virt = VIRT(pmm_alloc(blocks));
hal_memset(virt, 0, blocks * BITMAP_BLOCK_SIZE); memset(virt, 0, blocks * BITMAP_BLOCK_SIZE);
_last = (void *)(virt + inc); _last = (void *)(virt + inc);
return virt; return virt;
} }

View File

@ -2,10 +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"
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);
@ -132,7 +132,7 @@ struct VfsObj *littlefs_open(struct VfsMountPoint *vmp, const char *path, uint32
if (vobj == NULL) { if (vobj == NULL) {
return NULL; return NULL;
} }
hal_memset(vobj, 0, sizeof(*vobj)); memset(vobj, 0, sizeof(*vobj));
spinlock_init(&vobj->spinlock); spinlock_init(&vobj->spinlock);
int lfs_flags = 0; int lfs_flags = 0;
@ -214,7 +214,7 @@ int32_t littlefs_fetchdirent(struct VfsMountPoint *vmp, const char *path, FsDire
direntbuf->stat.type = FSSTAT_DIR; direntbuf->stat.type = FSSTAT_DIR;
} }
hal_memcpy(direntbuf->name, entinfo.name, sizeof(direntbuf->name)); memcpy(direntbuf->name, entinfo.name, sizeof(direntbuf->name));
break; break;
} }
i++; i++;

View File

@ -1,34 +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_memset(void *p, int c, size_t n);
void *hal_memcpy(void *dst, const void *src, size_t n);
size_t hal_strlen(char *s);
int hal_memcmp(const void *s1, const void *s2, int len);
int hal_strcmp(const char *a, const char *b);
size_t hal_strcspn(const char *s, const char *reject);
size_t hal_strspn(const char *s, const char *accept);
char *hal_strcpy(char *dest, const char *src);
char *hal_strchr(const char *s, int c);
char *hal_strstr(const char *str, const char *substring);
char *hal_string_combine(char *dest, const char *src);
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/io.h"
#include "x86_64/gdt.h"
#endif // KERNEL_HAL_HAL_H_

View File

@ -1,152 +0,0 @@
#include <stdint.h>
#include <stddef.h>
#include "hal.h"
void *hal_memset(void *p, int c, size_t n) {
char *cp = p;
for (size_t i = 0; i < n; i++) cp[i] = c;
return p;
}
void *hal_memcpy(void *dst, const void *src, size_t n) {
char *a = dst;
const char *b = src;
for (size_t i = 0; i < n; i++) a[i] = b[i];
return dst;
}
size_t hal_strlen(char *s) {
char *s2;
for (s2 = s; *s2; ++s2);
return (s2 - s);
}
// https://aticleworld.com/memcmp-in-c/
int hal_memcmp(const void *s1, const void *s2, int len)
{
unsigned char *p = (unsigned char *)s1;
unsigned char *q = (unsigned char *)s2;
int charCompareStatus = 0;
//If both pointer pointing same memory block
if (s1 == s2)
{
return charCompareStatus;
}
while (len > 0)
{
if (*p != *q)
{ //compare the mismatching character
charCompareStatus = (*p >*q)?1:-1;
break;
}
len--;
p++;
q++;
}
return charCompareStatus;
}
int hal_strcmp(const char *a, const char *b) {
while (*a && (*a == *b)) {
a++, b++;
}
return (unsigned char)*a - (unsigned char)*b;
}
size_t hal_strcspn(const char *s, const char *reject) {
size_t count = 0;
for (; *s != '\0'; ++s) {
const char *r = reject;
while (*r != '\0') {
if (*s == *r) {
return count;
}
r++;
}
count++;
}
return count;
}
size_t hal_strspn(const char *s, const char *accept) {
size_t count = 0;
for (; *s != '\0'; ++s) {
const char *a = accept;
int matched = 0;
while (*a != '\0') {
if (*s == *a) {
matched = 1;
break;
}
a++;
}
if (!matched) {
return count;
}
count++;
}
return count;
}
char *hal_strcpy(char *dest, const char *src) {
char *d = dest;
while ((*d++ = *src++) != '\0') {
;
}
return dest;
}
char *hal_strchr(const char *s, int c) {
char ch = (char)c;
while (*s != '\0') {
if (*s == ch) {
return (char *)s;
}
s++;
}
if (ch == '\0') {
return (char *)s;
}
return NULL;
}
char *hal_strstr(const char *str, const char *substring)
{
const char *a;
const char *b;
b = substring;
if (*b == 0) {
return (char *) str;
}
for ( ; *str != 0; str += 1) {
if (*str != *b) {
continue;
}
a = str;
while (1) {
if (*b == 0) {
return (char *) str;
}
if (*a++ != *b++) {
break;
}
}
b = substring;
}
return NULL;
}
char *hal_string_combine(char *dest, const char *src) {
size_t i, j;
for(i = 0; dest[i] != '\0'; i++);
for(j = 0; src[j] != '\0'; j++) {
dest[i+j] = src[j];
}
dest[i+j] = '\0';
return dest;
}

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.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,128 +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"
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;
hal_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;
hal_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,8 +4,8 @@
#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"
#define HSHTB_FNV32_OFF 0x811c9dc5u #define HSHTB_FNV32_OFF 0x811c9dc5u
#define HSHTB_FNV32_PRIME 0x01000193u #define HSHTB_FNV32_PRIME 0x01000193u
@ -32,7 +32,7 @@ enum {
#define HSHTB_ALLOC(tb, keyfield, k, out) \ #define HSHTB_ALLOC(tb, keyfield, k, out) \
do { \ do { \
size_t __len = __HSHTB_ARRAY_LEN(tb); \ size_t __len = __HSHTB_ARRAY_LEN(tb); \
uint32_t __h = hshtb_fnv32((k), hal_strlen((k))); \ uint32_t __h = hshtb_fnv32((k), strlen((k))); \
size_t __idx = __h % __len; \ size_t __idx = __h % __len; \
size_t __start = __idx; \ size_t __start = __idx; \
typeof(&(tb)[0]) __tomb = NULL; \ typeof(&(tb)[0]) __tomb = NULL; \
@ -40,13 +40,13 @@ enum {
do { \ do { \
if ((tb)[__idx]._hshtbstate == HSHTB_EMPTY) { \ if ((tb)[__idx]._hshtbstate == HSHTB_EMPTY) { \
typeof(&(tb)[0]) __slot = __tomb ? __tomb : &(tb)[__idx]; \ typeof(&(tb)[0]) __slot = __tomb ? __tomb : &(tb)[__idx]; \
hal_memset(__slot, 0, sizeof(*__slot)); \ memset(__slot, 0, sizeof(*__slot)); \
__slot->_hshtbstate = HSHTB_TAKEN; \ __slot->_hshtbstate = HSHTB_TAKEN; \
hal_memcpy(__slot->keyfield, (k), MIN(sizeof(__slot->keyfield) - 1, hal_strlen((k)))); \ memcpy(__slot->keyfield, (k), MIN(sizeof(__slot->keyfield) - 1, strlen((k)))); \
(out) = __slot; \ (out) = __slot; \
break; \ break; \
} \ } \
if ((tb)[__idx]._hshtbstate == HSHTB_TAKEN && hal_strcmp((tb)[__idx].keyfield, (k)) == 0) { \ if ((tb)[__idx]._hshtbstate == HSHTB_TAKEN && strcmp((tb)[__idx].keyfield, (k)) == 0) { \
(out) = &(tb)[__idx]; \ (out) = &(tb)[__idx]; \
break; \ break; \
} \ } \
@ -60,7 +60,7 @@ enum {
#define HSHTB_GET(tb, keyfield, k, out) \ #define HSHTB_GET(tb, keyfield, k, out) \
do { \ do { \
size_t __len = __HSHTB_ARRAY_LEN(tb); \ size_t __len = __HSHTB_ARRAY_LEN(tb); \
uint32_t __h = hshtb_fnv32((k), hal_strlen((k))); \ uint32_t __h = hshtb_fnv32((k), strlen((k))); \
size_t __idx = __h % __len; \ size_t __idx = __h % __len; \
size_t __start = __idx; \ size_t __start = __idx; \
(out) = NULL; \ (out) = NULL; \
@ -68,7 +68,7 @@ enum {
if ((tb)[__idx]._hshtbstate == HSHTB_EMPTY) { \ if ((tb)[__idx]._hshtbstate == HSHTB_EMPTY) { \
break; \ break; \
} \ } \
if ((tb)[__idx]._hshtbstate == HSHTB_TAKEN && hal_strcmp((tb)[__idx].keyfield, (k)) == 0) { \ if ((tb)[__idx]._hshtbstate == HSHTB_TAKEN && strcmp((tb)[__idx].keyfield, (k)) == 0) { \
(out) = &(tb)[__idx]; \ (out) = &(tb)[__idx]; \
break; \ break; \
} \ } \

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 "io.h" #include "intr/pic.h"
#include "gdt.h" #include "intr/pit.h"
#include "hal/hal.h" #include "io/io.h"
#include "kprintf.h" #include "cpu/gdt.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,6 +1,6 @@
#include <stdint.h> #include <stdint.h>
#include "pit.h" #include "intr/pit.h"
#include "io.h" #include "io/io.h"
#define PIT_COUNTER0 0x40 #define PIT_COUNTER0 0x40
#define PIT_CMD 0x43 #define PIT_CMD 0x43
@ -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,4 +1,5 @@
#include <stdint.h> #include <stdint.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

@ -1,5 +1,5 @@
#ifndef HAL_IO_H_ #ifndef IO_IO_H_
#define HAL_IO_H_ #define IO_IO_H_
#include <stdint.h> #include <stdint.h>
@ -15,4 +15,4 @@ void io_out32(uint16_t port, uint32_t value);
void io_ins16(uint16_t port, void *addr, int cnt); void io_ins16(uint16_t port, void *addr, int cnt);
void io_outs16(uint16_t port, const void *addr, int cnt); void io_outs16(uint16_t port, const void *addr, int cnt);
#endif // HAL_IO_H_ #endif // IO_IO_H_

View File

@ -2,14 +2,14 @@
#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 "std/string.h"
#include "kprintf.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) {
hal_memset(pipe, 0, sizeof(*pipe)); memset(pipe, 0, sizeof(*pipe));
spinlock_init(&pipe->spinlock); spinlock_init(&pipe->spinlock);
pipe->ownerpid = pid; pipe->ownerpid = pid;

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,5 @@
#include "path.h" #include "path.h"
#include "hal/hal.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) {
if (in == 0 || *in == 0) { if (in == 0 || *in == 0) {
@ -45,8 +45,8 @@ void path_parse(const char *in, char *mp, char *path) {
return; return;
} }
if (hal_strstr(path, "/../") || hal_strstr(path, "/./") if (strstr(path, "/../") || strstr(path, "/./")
|| hal_strcmp(path, "..") == 0 || hal_strcmp(path, ".") == 0) { || strcmp(path, "..") == 0 || strcmp(path, ".") == 0) {
mp[0] = 0; mp[0] = 0;
path[0] = 0; path[0] = 0;
return; return;

View File

@ -1,12 +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 "cpu/hang.h"
#include "kprintf.h"
PhysMem PHYS_MEM; PhysMem PHYS_MEM;
@ -31,13 +32,13 @@ 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;
bm->map = (uint8_t *)(physbegin + BOOT_INFO.hhdm_off); bm->map = (uint8_t *)(physbegin + BOOT_INFO.hhdm_off);
hal_memset(bm->map, 0xff, bm->nbytes); memset(bm->map, 0xff, bm->nbytes);
for (size_t i = 0; i < BOOT_INFO.memmap_entrycount; i++) { for (size_t i = 0; i < BOOT_INFO.memmap_entrycount; i++) {
struct limine_memmap_entry *entry = BOOT_INFO.memmap_entries[i]; struct limine_memmap_entry *entry = BOOT_INFO.memmap_entries[i];
if (entry == memmap_ent) { if (entry == memmap_ent) {

View File

@ -1,21 +1,25 @@
#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"
#include "sysdefs/proc.h" #include "sysdefs/proc.h"
#include "sysdefs/fs.h" #include "sysdefs/fs.h"
#include "time/time.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 #define PROC_REAPER_FREQ 30
@ -47,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));
hal_memset(VIRT(physaddr), 0, blocks * HAL_PAGE_SIZE); memset(VIRT(physaddr), 0, blocks * VMM_PAGE_SIZE);
hal_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);
@ -105,11 +109,11 @@ Proc *proc_spawnuser(char *mountpoint, char *path) {
vfs_close(vobj); vfs_close(vobj);
Proc *proc = dlmalloc(sizeof(*proc)); Proc *proc = dlmalloc(sizeof(*proc));
hal_memset(proc, 0, sizeof(*proc)); memset(proc, 0, sizeof(*proc));
ksprintf(proc->name, "%s:%s", mountpoint, path); ksprintf(proc->name, "%s:%s", mountpoint, path);
hal_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;
@ -119,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;
@ -202,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);
} }
@ -220,10 +224,10 @@ void proc_reaper(void) {
} }
void proc_sched(void *cpustate) { void proc_sched(void *cpustate) {
hal_intr_disable(); intr_disable();
sched_ticks++; sched_ticks++;
hal_memcpy(&PROCS.current->platformdata.trapframe, cpustate, sizeof(IntrStackFrame)); memcpy(&PROCS.current->platformdata.trapframe, cpustate, sizeof(IntrStackFrame));
PROCS.current = proc_nextready(); PROCS.current = proc_nextready();
@ -232,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) {
@ -258,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"
@ -10,6 +9,9 @@
#include "sysdefs/proc.h" #include "sysdefs/proc.h"
#include "sysdefs/time.h" #include "sysdefs/time.h"
#include "dev/dev.h" #include "dev/dev.h"
#include "std/string.h"
#include "intr/intr.h"
#include "vmm/vmm.h"
#define PROC_NAME_MAX 0x100 #define PROC_NAME_MAX 0x100
@ -90,7 +92,7 @@ void proc_kill(Proc *proc);
#define PROC_ARG(proc, str) \ #define PROC_ARG(proc, str) \
do { \ do { \
ProcArg *__arg = dlmalloc(sizeof(*__arg)); \ ProcArg *__arg = dlmalloc(sizeof(*__arg)); \
hal_strcpy(__arg->string, (str)); \ strcpy(__arg->string, (str)); \
LL_APPEND((proc)->procargs.list, __arg); \ LL_APPEND((proc)->procargs.list, __arg); \
(proc)->procargs.len++; \ (proc)->procargs.len++; \
} while(0) } while(0)

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,39 +1,151 @@
#include <stddef.h> #include <stddef.h>
#include "hal/hal.h" #include <stdint.h>
void *memset(void *p, int c, size_t n) { void *memset(void *p, int c, size_t n) {
return hal_memset(p,c,n); char *cp = p;
for (size_t i = 0; i < n; i++) cp[i] = c;
return p;
} }
void *memcpy(void *dst, const void *src, size_t n) { void *memcpy(void *dst, const void *src, size_t n) {
return hal_memcpy(dst,src,n); char *a = dst;
const char *b = src;
for (size_t i = 0; i < n; i++) a[i] = b[i];
return dst;
} }
size_t strlen(char *s) { size_t strlen(char *s) {
return hal_strlen(s); char *s2;
for (s2 = s; *s2; ++s2);
return (s2 - s);
}
// https://aticleworld.com/memcmp-in-c/
int memcmp(const void *s1, const void *s2, int len)
{
unsigned char *p = (unsigned char *)s1;
unsigned char *q = (unsigned char *)s2;
int charCompareStatus = 0;
//If both pointer pointing same memory block
if (s1 == s2)
{
return charCompareStatus;
}
while (len > 0)
{
if (*p != *q)
{ //compare the mismatching character
charCompareStatus = (*p >*q)?1:-1;
break;
}
len--;
p++;
q++;
}
return charCompareStatus;
} }
int strcmp(const char *a, const char *b) { int strcmp(const char *a, const char *b) {
return hal_strcmp(a, b); while (*a && (*a == *b)) {
a++, b++;
}
return (unsigned char)*a - (unsigned char)*b;
} }
size_t strcspn(const char *s, const char *reject) { size_t strcspn(const char *s, const char *reject) {
return hal_strcspn(s, reject); size_t count = 0;
for (; *s != '\0'; ++s) {
const char *r = reject;
while (*r != '\0') {
if (*s == *r) {
return count;
}
r++;
}
count++;
}
return count;
} }
size_t strspn(const char *s, const char *accept) { size_t strspn(const char *s, const char *accept) {
return hal_strspn(s, accept); size_t count = 0;
for (; *s != '\0'; ++s) {
const char *a = accept;
int matched = 0;
while (*a != '\0') {
if (*s == *a) {
matched = 1;
break;
}
a++;
}
if (!matched) {
return count;
}
count++;
}
return count;
} }
char *strcpy(char *dest, const char *src) { char *strcpy(char *dest, const char *src) {
return hal_strcpy(dest, src); char *d = dest;
while ((*d++ = *src++) != '\0') {
;
}
return dest;
} }
char *strchr(const char *s, int c) { char *strchr(const char *s, int c) {
return hal_strchr(s, c); char ch = (char)c;
while (*s != '\0') {
if (*s == ch) {
return (char *)s;
}
s++;
}
if (ch == '\0') {
return (char *)s;
}
return NULL;
} }
int memcmp(const void *s1, const void *s2, int len) { char *strstr(const char *str, const char *substring)
return hal_memcmp(s1, s2, len); {
const char *a;
const char *b;
b = substring;
if (*b == 0) {
return (char *) str;
}
for ( ; *str != 0; str += 1) {
if (*str != *b) {
continue;
}
a = str;
while (1) {
if (*b == 0) {
return (char *) str;
}
if (*a++ != *b++) {
break;
}
}
b = substring;
}
return NULL;
}
char *strcat(char *dest, const char *src) {
size_t i, j;
for(i = 0; dest[i] != '\0'; i++);
for(j = 0; src[j] != '\0'; j++) {
dest[i+j] = src[j];
}
dest[i+j] = '\0';
return dest;
} }

View File

@ -12,5 +12,7 @@ size_t strspn(const char *s, const char *accept);
char *strcpy(char *dest, const char *src); char *strcpy(char *dest, const char *src);
char *strchr(const char *s, int c); char *strchr(const char *s, int c);
int memcmp(const void *s1, const void *s2, int len); int memcmp(const void *s1, const void *s2, int len);
char *strstr(const char *str, const char *substring);
char *strcat(char *dest, const char *src);
#endif // STD_STRING_H_ #endif // STD_STRING_H_

View File

@ -1,14 +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"
#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,13 +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 "std/string.h"
#include "kprintf.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;
@ -26,7 +26,7 @@ int32_t ramsd_init(struct StoreDev *sd, void *extra) {
int32_t ramsd_read(struct StoreDev *sd, uint8_t *const buffer, ptrdiff_t sector, ptrdiff_t off, size_t size) { int32_t ramsd_read(struct StoreDev *sd, uint8_t *const buffer, ptrdiff_t sector, ptrdiff_t off, size_t size) {
RamSd *ramsd = &sd->sd.ramsd; RamSd *ramsd = &sd->sd.ramsd;
spinlock_acquire(&sd->spinlock); spinlock_acquire(&sd->spinlock);
hal_memcpy(buffer, ramsd->buffer + (sector * sd->sectorsize + off), size); memcpy(buffer, ramsd->buffer + (sector * sd->sectorsize + off), size);
spinlock_release(&sd->spinlock); spinlock_release(&sd->spinlock);
return E_OK; return E_OK;
} }
@ -34,7 +34,7 @@ int32_t ramsd_read(struct StoreDev *sd, uint8_t *const buffer, ptrdiff_t sector,
int32_t ramsd_write(struct StoreDev *sd, const uint8_t *const buffer, ptrdiff_t sector, ptrdiff_t off, size_t size) { int32_t ramsd_write(struct StoreDev *sd, const uint8_t *const buffer, ptrdiff_t sector, ptrdiff_t off, size_t size) {
RamSd *ramsd = &sd->sd.ramsd; RamSd *ramsd = &sd->sd.ramsd;
spinlock_acquire(&sd->spinlock); spinlock_acquire(&sd->spinlock);
hal_memcpy(ramsd->buffer + (sector * sd->sectorsize + off), buffer, size); memcpy(ramsd->buffer + (sector * sd->sectorsize + off), buffer, size);
spinlock_release(&sd->spinlock); spinlock_release(&sd->spinlock);
return E_OK; return E_OK;
} }

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,14 +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 "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;
@ -87,7 +88,7 @@ int32_t SYSCALL2(sys_dev_stat, devstat1, idx1) {
for (size_t i = 0; i < LEN(DEVTABLE.devs); i++) { for (size_t i = 0; i < LEN(DEVTABLE.devs); i++) {
if (i == idx && DEVTABLE.devs[i]._hshtbstate == HSHTB_TAKEN) { if (i == idx && DEVTABLE.devs[i]._hshtbstate == HSHTB_TAKEN) {
Dev *dev = &DEVTABLE.devs[i]; Dev *dev = &DEVTABLE.devs[i];
hal_memcpy(devstat->name, dev->ident, sizeof(dev->ident)); memcpy(devstat->name, dev->ident, sizeof(dev->ident));
for (size_t j = 0; j < DEV_FNS_MAX; j++) { for (size_t j = 0; j < DEV_FNS_MAX; j++) {
if (dev->fns[j] != NULL) { if (dev->fns[j] != NULL) {
devstat->nfns++; devstat->nfns++;

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,15 +1,16 @@
#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 "std/string.h"
#include "vmm/vmm.h"
#include "errors.h"
#include "kprintf.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) {
@ -19,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);
hal_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;
@ -37,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;
} }
@ -48,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);
@ -97,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,16 +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 "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
@ -178,7 +179,7 @@ int32_t SYSCALL4(sys_proc_argv, pid1, argslen1, argbuf1, maxargs1) {
ret = E_INVALIDARGUMENT; ret = E_INVALIDARGUMENT;
goto done; goto done;
} }
hal_strcpy(argbuf[i], arg->string); strcpy(argbuf[i], arg->string);
} }
*argslen = i; *argslen = i;
@ -214,7 +215,7 @@ int32_t SYSCALL2(sys_proc_stat, pidx, pstat1) {
LL_FOREACH_SAFE_IDX(PROCS.procs, p, ptmp, i) { LL_FOREACH_SAFE_IDX(PROCS.procs, p, ptmp, i) {
if (i == pidx) { if (i == pidx) {
stat->pid = p->pid; stat->pid = p->pid;
hal_strcpy(stat->name, p->name); strcpy(stat->name, p->name);
stat->state = p->state; stat->state = p->state;
VasRange *vas, *vastmp; VasRange *vas, *vastmp;
@ -222,7 +223,7 @@ int32_t SYSCALL2(sys_proc_stat, pidx, pstat1) {
stat->usemem += vas->size; stat->usemem += vas->size;
} }
hal_memcpy(&stat->time, &p->time, sizeof(p->time)); memcpy(&stat->time, &p->time, sizeof(p->time));
break; break;
} }
} }

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,7 +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 "std/string.h"
#include "hshtb.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;
@ -30,7 +30,7 @@ int32_t SYSCALL4(sys_vfsmount, mountpoint1, fstypestr1, devid1, format1) {
int32_t fstype; int32_t fstype;
if (hal_strcmp(fstypestr, "LittleFS") == 0) { if (strcmp(fstypestr, "LittleFS") == 0) {
fstype = VFS_LITTLEFS; fstype = VFS_LITTLEFS;
} else { } else {
ret = E_INVALIDARGUMENT; ret = E_INVALIDARGUMENT;
@ -76,7 +76,7 @@ int32_t SYSCALL1(sys_vfsavailmounts, availmounts1) {
for (size_t i = 0; i < VFS_MOUNTPOINTS_MAX; i++) { for (size_t i = 0; i < VFS_MOUNTPOINTS_MAX; i++) {
VfsMountPoint *vmp = &VFS_TABLE.mountpoints[i]; VfsMountPoint *vmp = &VFS_TABLE.mountpoints[i];
if (vmp->_hshtbstate == HSHTB_TAKEN) { if (vmp->_hshtbstate == HSHTB_TAKEN) {
hal_memcpy(availmounts->labels[i], vmp->label, VFS_MOUNTPOINT_LABEL_MAX); memcpy(availmounts->labels[i], vmp->label, VFS_MOUNTPOINT_LABEL_MAX);
availmounts->fieldavail[i] = 1; availmounts->fieldavail[i] = 1;
} }
} }
@ -105,7 +105,7 @@ int32_t SYSCALL2(sys_vfsmountstat, statbuf1, label1) {
return E_NOENTRY; return E_NOENTRY;
} }
hal_memcpy(statbuf->label, vmp->label, sizeof(statbuf->label)); memcpy(statbuf->label, vmp->label, sizeof(statbuf->label));
statbuf->fstype = vmp->fstype; statbuf->fstype = vmp->fstype;
StoreDev *vmpsd = vmp->backingsd; StoreDev *vmpsd = vmp->backingsd;
@ -113,7 +113,7 @@ int32_t SYSCALL2(sys_vfsmountstat, statbuf1, label1) {
for (size_t i = 0; i < LEN(DEVTABLE.devs); i++) { for (size_t i = 0; i < LEN(DEVTABLE.devs); i++) {
Dev *dev = &DEVTABLE.devs[i]; Dev *dev = &DEVTABLE.devs[i];
if (dev->extra != NULL && ((StoreDev *)dev->extra)->_magic == STOREDEV_MAGIC && vmpsd == dev->extra) { if (dev->extra != NULL && ((StoreDev *)dev->extra)->_magic == STOREDEV_MAGIC && vmpsd == dev->extra) {
hal_memcpy(statbuf->devname, dev->ident, sizeof(statbuf->devname)); memcpy(statbuf->devname, dev->ident, sizeof(statbuf->devname));
break; break;
} }
} }

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,8 @@
#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 "io/io.h"
#define CMOS_PORT 0x70 #define CMOS_PORT 0x70
#define CMOS_RETURN 0x71 #define CMOS_RETURN 0x71
@ -48,7 +49,7 @@ void time_get(Time *time) {
continue; continue;
} }
time_fill(&t2); time_fill(&t2);
if (hal_memcmp(&t1, &t2, sizeof(t1)) == 0) { if (memcmp(&t1, &t2, sizeof(t1)) == 0) {
break; break;
} }
} }

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,17 +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 "kprintf.h"
#include "errors.h"
#include "hshtb.h"
VfsTable VFS_TABLE; VfsTable VFS_TABLE;
VfsBusyObjs VFS_BUSY_VOBJS; VfsBusyObjs VFS_BUSY_VOBJS;
@ -19,16 +18,16 @@ VfsBusyObjs VFS_BUSY_VOBJS;
#define CHECK_BUSY_VFSOBJ() \ #define CHECK_BUSY_VFSOBJ() \
char *tmpbuf = dlmalloc(VFS_MOUNTPOINT_LABEL_MAX+1+VFS_PATH_MAX); \ char *tmpbuf = dlmalloc(VFS_MOUNTPOINT_LABEL_MAX+1+VFS_PATH_MAX); \
\ \
hal_memset(tmpbuf, 0, VFS_MOUNTPOINT_LABEL_MAX+1+VFS_PATH_MAX); \ memset(tmpbuf, 0, VFS_MOUNTPOINT_LABEL_MAX+1+VFS_PATH_MAX); \
hal_string_combine(tmpbuf, mountpoint); \ strcat(tmpbuf, mountpoint); \
hal_string_combine(tmpbuf, ":"); \ strcat(tmpbuf, ":"); \
hal_string_combine(tmpbuf, path); \ strcat(tmpbuf, path); \
\ \
bool busy = false; \ bool busy = false; \
VfsObj *vobj, *vobjtmp; \ VfsObj *vobj, *vobjtmp; \
spinlock_acquire(&VFS_BUSY_VOBJS.spinlock); \ spinlock_acquire(&VFS_BUSY_VOBJS.spinlock); \
LL_FOREACH_SAFE(VFS_BUSY_VOBJS.vobjs, vobj, vobjtmp) { \ LL_FOREACH_SAFE(VFS_BUSY_VOBJS.vobjs, vobj, vobjtmp) { \
if (hal_strcmp(vobj->path, tmpbuf) == 0) { \ if (strcmp(vobj->path, tmpbuf) == 0) { \
busy = true; \ busy = true; \
break; \ break; \
} \ } \
@ -39,7 +38,7 @@ VfsBusyObjs VFS_BUSY_VOBJS;
void vfs_init_littlefs(VfsMountPoint *mp, bool format) { void vfs_init_littlefs(VfsMountPoint *mp, bool format) {
struct lfs_config *cfg = dlmalloc(sizeof(*cfg)); struct lfs_config *cfg = dlmalloc(sizeof(*cfg));
hal_memset(cfg, 0, sizeof(*cfg)); memset(cfg, 0, sizeof(*cfg));
cfg->context = mp; cfg->context = mp;
cfg->read = &portlfs_read; cfg->read = &portlfs_read;
cfg->prog = &portlfs_prog; cfg->prog = &portlfs_prog;
@ -147,7 +146,7 @@ int32_t vfs_mount(char *mountpoint, int32_t fstype, StoreDev *backingsd, bool fo
return E_NOMEMORY; return E_NOMEMORY;
} }
hal_memcpy(mp->label, mountpoint, hal_strlen(mountpoint)); memcpy(mp->label, mountpoint, strlen(mountpoint));
mp->backingsd = backingsd; mp->backingsd = backingsd;
mp->fstype = fstype; mp->fstype = fstype;
switch (fstype) { switch (fstype) {
@ -178,7 +177,7 @@ int32_t vfs_unmount(char *mountpoint) {
spinlock_release(&mp->spinlock); spinlock_release(&mp->spinlock);
return err; return err;
} }
hal_memset(mp, 0, sizeof(*mp)); memset(mp, 0, sizeof(*mp));
spinlock_release(&mp->spinlock); spinlock_release(&mp->spinlock);
return E_OK; return E_OK;
} }
@ -205,9 +204,9 @@ VfsObj *vfs_open(char *mountpoint, const char *path, uint32_t flags) {
return NULL; return NULL;
} }
hal_string_combine(vobj1->path, mountpoint); strcat(vobj1->path, mountpoint);
hal_string_combine(vobj1->path, ":"); strcat(vobj1->path, ":");
hal_string_combine(vobj1->path, path); strcat(vobj1->path, path);
spinlock_acquire(&VFS_BUSY_VOBJS.spinlock); spinlock_acquire(&VFS_BUSY_VOBJS.spinlock);
LL_APPEND(VFS_BUSY_VOBJS.vobjs, vobj1); LL_APPEND(VFS_BUSY_VOBJS.vobjs, vobj1);
@ -224,9 +223,9 @@ void vfs_close(VfsObj *vobj) {
} }
void vfs_init(void) { void vfs_init(void) {
hal_memset(&VFS_TABLE, 0, sizeof(VFS_TABLE)); memset(&VFS_TABLE, 0, sizeof(VFS_TABLE));
spinlock_init(&VFS_TABLE.spinlock); spinlock_init(&VFS_TABLE.spinlock);
hal_memset(&VFS_BUSY_VOBJS, 0, sizeof(VFS_BUSY_VOBJS)); memset(&VFS_BUSY_VOBJS, 0, sizeof(VFS_BUSY_VOBJS));
spinlock_init(&VFS_BUSY_VOBJS.spinlock); spinlock_init(&VFS_BUSY_VOBJS.spinlock);
{ {

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_