diff options
Diffstat (limited to 'src/kernel')
-rw-r--r-- | src/kernel/arch/i386/boot.c | 40 | ||||
-rw-r--r-- | src/kernel/arch/i386/driver/ps2.c | 21 | ||||
-rw-r--r-- | src/kernel/arch/i386/driver/ps2.h | 11 | ||||
-rw-r--r-- | src/kernel/arch/i386/driver/serial.c | 25 | ||||
-rw-r--r-- | src/kernel/arch/i386/driver/serial.h | 13 | ||||
-rw-r--r-- | src/kernel/arch/i386/tty/tty.c | 2 | ||||
-rw-r--r-- | src/kernel/main.c | 3 | ||||
-rw-r--r-- | src/kernel/mem/alloc.c | 4 | ||||
-rw-r--r-- | src/kernel/mem/alloc.h | 2 | ||||
-rw-r--r-- | src/kernel/syscalls.c | 3 | ||||
-rw-r--r-- | src/kernel/vfs/mount.c | 68 | ||||
-rw-r--r-- | src/kernel/vfs/mount.h | 4 | ||||
-rw-r--r-- | src/kernel/vfs/request.c | 2 | ||||
-rw-r--r-- | src/kernel/vfs/request.h | 9 | ||||
-rw-r--r-- | src/kernel/vfs/root.c | 7 | ||||
-rw-r--r-- | src/kernel/vfs/root.h | 5 |
16 files changed, 107 insertions, 112 deletions
diff --git a/src/kernel/arch/i386/boot.c b/src/kernel/arch/i386/boot.c index a3716ee..be80f73 100644 --- a/src/kernel/arch/i386/boot.c +++ b/src/kernel/arch/i386/boot.c @@ -1,19 +1,34 @@ #include <kernel/arch/generic.h> #include <kernel/arch/i386/ata.h> #include <kernel/arch/i386/boot.h> +#include <kernel/arch/i386/driver/ps2.h> +#include <kernel/arch/i386/driver/serial.h> #include <kernel/arch/i386/gdt.h> #include <kernel/arch/i386/interrupts/idt.h> #include <kernel/arch/i386/interrupts/irq.h> #include <kernel/arch/i386/multiboot.h> #include <kernel/main.h> +#include <kernel/mem/alloc.h> #include <kernel/panic.h> +#include <kernel/vfs/root.h> void tty_init(void); // TODO put this in a header file +static void find_init(struct multiboot_info *multiboot, struct kmain_info *info) +{ + struct multiboot_mod *module = &multiboot->mods[0]; + if (multiboot->mods_count < 1) { + kprintf("can't find init! "); + panic_invalid_state(); + } + info->init.at = module->start; + info->init.size = module->end - module->start; + +} + void kmain_early(struct multiboot_info *multiboot) { struct kmain_info info; - // setup some basic stuff tty_init(); kprintf("gdt..."); gdt_init(); @@ -21,20 +36,19 @@ void kmain_early(struct multiboot_info *multiboot) { idt_init(); kprintf("irq..."); irq_init(); - kprintf("ata..."); - ata_init(); - - { // find the init module - struct multiboot_mod *module = &multiboot->mods[0]; - if (multiboot->mods_count < 1) { - kprintf("can't find init! "); - panic_invalid_state(); // no init - } - info.init.at = module->start; - info.init.size = module->end - module->start; - } info.memtop = (void*) (multiboot->mem_upper * 1024); + find_init(multiboot, &info); + kprintf("mem...\n"); + mem_init(&info); + + kprintf("rootfs..."); + vfs_root_init(); + ps2_init(); + serial_init(); + + kprintf("ata..."); + ata_init(); kmain(info); } diff --git a/src/kernel/arch/i386/driver/ps2.c b/src/kernel/arch/i386/driver/ps2.c index 8955100..97e2878 100644 --- a/src/kernel/arch/i386/driver/ps2.c +++ b/src/kernel/arch/i386/driver/ps2.c @@ -2,6 +2,7 @@ #include <kernel/arch/i386/interrupts/irq.h> #include <kernel/mem/virt.h> #include <kernel/panic.h> +#include <kernel/vfs/request.h> #include <shared/container/ring.h> #include <shared/mem.h> @@ -12,23 +13,33 @@ static volatile ring_t backlog = {(void*)backlog_buf, BACKLOG_CAPACITY, 0, 0}; static struct vfs_request *blocked_on = NULL; -bool ps2_ready(void) { + +static void accept(struct vfs_request *req); +static bool is_ready(struct vfs_backend *self); + + +static struct vfs_backend backend = BACKEND_KERN(is_ready, accept); +void ps2_init(void) { vfs_mount_root_register("/ps2", &backend); } + + + +static bool ps2_ready(void) { return ring_size((void*)&backlog) > 0; } void ps2_recv(uint8_t s) { ring_put1b((void*)&backlog, s); if (blocked_on) { - vfs_ps2_accept(blocked_on); + accept(blocked_on); blocked_on = NULL; } } -size_t ps2_read(uint8_t *buf, size_t len) { +static size_t ps2_read(uint8_t *buf, size_t len) { return ring_get((void*)&backlog, buf, len); } -void vfs_ps2_accept(struct vfs_request *req) { +static void accept(struct vfs_request *req) { // when you fix something here go also fix it in the COM1 driver static uint8_t buf[32]; // pretty damn stupid int ret; @@ -60,4 +71,4 @@ void vfs_ps2_accept(struct vfs_request *req) { } } -bool vfs_ps2_ready(struct vfs_backend *self) { return blocked_on == NULL; } +static bool is_ready(struct vfs_backend *self) { return blocked_on == NULL; } diff --git a/src/kernel/arch/i386/driver/ps2.h b/src/kernel/arch/i386/driver/ps2.h index c5d0ce1..54e8fb2 100644 --- a/src/kernel/arch/i386/driver/ps2.h +++ b/src/kernel/arch/i386/driver/ps2.h @@ -1,12 +1,5 @@ #pragma once -#include <kernel/vfs/request.h> -#include <stdbool.h> -#include <stddef.h> #include <stdint.h> -bool ps2_ready(void); -void ps2_recv(uint8_t scancode); -size_t ps2_read(uint8_t *buf, size_t max_len); - -void vfs_ps2_accept(struct vfs_request *); -bool vfs_ps2_ready(struct vfs_backend *); +void ps2_recv(uint8_t s); +void ps2_init(void); diff --git a/src/kernel/arch/i386/driver/serial.c b/src/kernel/arch/i386/driver/serial.c index 170cc4d..614d4e0 100644 --- a/src/kernel/arch/i386/driver/serial.c +++ b/src/kernel/arch/i386/driver/serial.c @@ -16,6 +16,17 @@ static const int COM1 = 0x3f8; static struct vfs_request *blocked_on = NULL; +static void accept(struct vfs_request *req); +static bool is_ready(struct vfs_backend *self); + + +static struct vfs_backend backend = BACKEND_KERN(is_ready, accept); +void serial_init(void) { vfs_mount_root_register("/com1", &backend); } + + + + + static void serial_selftest(void) { char b = 0x69; port_out8(COM1 + 4, 0b00011110); // enable loopback mode @@ -23,7 +34,7 @@ static void serial_selftest(void) { assert(port_in8(COM1) == b); } -void serial_init(void) { +void serial_preinit(void) { // see https://www.sci.muni.cz/docs/pc/serport.txt // set baud rate divisor port_out8(COM1 + 3, 0b10000000); // enable DLAB @@ -40,20 +51,20 @@ void serial_init(void) { } -bool serial_ready(void) { +static bool serial_ready(void) { return ring_size((void*)&backlog) > 0; } void serial_irq(void) { ring_put1b((void*)&backlog, port_in8(COM1)); if (blocked_on) { - vfs_com1_accept(blocked_on); + accept(blocked_on); blocked_on = NULL; // TODO vfs_backend_tryaccept } } -size_t serial_read(char *buf, size_t len) { +static size_t serial_read(char *buf, size_t len) { return ring_get((void*)&backlog, buf, len); } @@ -69,8 +80,8 @@ void serial_write(const char *buf, size_t len) { } -void vfs_com1_accept(struct vfs_request *req) { - static uint8_t buf[32]; +static void accept(struct vfs_request *req) { + static char buf[32]; int ret; switch (req->type) { case VFSOP_OPEN: @@ -109,4 +120,4 @@ void vfs_com1_accept(struct vfs_request *req) { } } -bool vfs_com1_ready(struct vfs_backend *self) { return blocked_on == NULL; } +static bool is_ready(struct vfs_backend *self) { return blocked_on == NULL; } diff --git a/src/kernel/arch/i386/driver/serial.h b/src/kernel/arch/i386/driver/serial.h index 6aff142..6a4876e 100644 --- a/src/kernel/arch/i386/driver/serial.h +++ b/src/kernel/arch/i386/driver/serial.h @@ -3,13 +3,8 @@ #include <stdbool.h> #include <stddef.h> -void serial_init(void); +void serial_preinit(void); +void serial_irq(void); +void serial_write(const char *buf, size_t len); -bool serial_ready(void); -void serial_irq(void); -size_t serial_read(char *buf, size_t len); - -void serial_write(const char *buf, size_t len); - -void vfs_com1_accept(struct vfs_request *); -bool vfs_com1_ready(struct vfs_backend *); +void serial_init(void); diff --git a/src/kernel/arch/i386/tty/tty.c b/src/kernel/arch/i386/tty/tty.c index d4a97fb..a1964f9 100644 --- a/src/kernel/arch/i386/tty/tty.c +++ b/src/kernel/arch/i386/tty/tty.c @@ -5,7 +5,7 @@ void tty_init(void) { vga_clear(); - serial_init(); + serial_preinit(); vga_write("\x03 ", 2); // cp437 heart serial_write("<3 ", 3); diff --git a/src/kernel/main.c b/src/kernel/main.c index 5023ea9..bbdd234 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -7,9 +7,6 @@ #include <stdint.h> void kmain(struct kmain_info info) { - kprintf("mem...\n"); - mem_init(&info); - kprintf("tests...\n"); tests_all(); diff --git a/src/kernel/mem/alloc.c b/src/kernel/mem/alloc.c index 3925689..797b018 100644 --- a/src/kernel/mem/alloc.c +++ b/src/kernel/mem/alloc.c @@ -102,9 +102,9 @@ void page_free(void *first_addr, size_t pages) { } } -void kmalloc_sanity(void *addr) { +void kmalloc_sanity(const void *addr) { assert(addr); - struct malloc_hdr *hdr = addr - sizeof(struct malloc_hdr); + const struct malloc_hdr *hdr = addr - sizeof(struct malloc_hdr); assert(hdr->magic == MALLOC_MAGIC); if (hdr->next) assert(hdr->next->prev == hdr); if (hdr->prev) assert(hdr->prev->next == hdr); diff --git a/src/kernel/mem/alloc.h b/src/kernel/mem/alloc.h index 0760d67..99fd931 100644 --- a/src/kernel/mem/alloc.h +++ b/src/kernel/mem/alloc.h @@ -12,6 +12,6 @@ void *page_alloc(size_t pages); // frees `pages` consecutive pages starting from *first void page_free(void *first, size_t pages); -void kmalloc_sanity(void *addr); +void kmalloc_sanity(const void *addr); void *kmalloc(size_t len); void kfree(void *ptr); diff --git a/src/kernel/syscalls.c b/src/kernel/syscalls.c index aa2d45f..01d42a0 100644 --- a/src/kernel/syscalls.c +++ b/src/kernel/syscalls.c @@ -186,7 +186,8 @@ handle_t _syscall_fs_fork2(void) { if (front < 0) SYSCALL_RETURN(-1); process_current->handles[front] = handle_init(HANDLE_FS_FRONT); - backend = kmalloc(sizeof *backend); // TODO never freed + backend = kmalloc(sizeof *backend); + backend->heap = true; backend->is_user = true; backend->potential_handlers = 1; backend->refcount = 2; // child + handle diff --git a/src/kernel/vfs/mount.c b/src/kernel/vfs/mount.c index 178cc54..77cc14b 100644 --- a/src/kernel/vfs/mount.c +++ b/src/kernel/vfs/mount.c @@ -1,63 +1,13 @@ #include <kernel/mem/alloc.h> #include <kernel/panic.h> #include <kernel/vfs/mount.h> -#include <kernel/vfs/root.h> #include <shared/mem.h> // TODO not the place where this should be done -#include <kernel/arch/i386/driver/ps2.h> -#include <kernel/arch/i386/driver/serial.h> +static struct vfs_mount *mount_root = NULL; struct vfs_mount *vfs_mount_seed(void) { - struct vfs_mount *mount = kmalloc(sizeof *mount); - struct vfs_backend *backend = kmalloc(sizeof *backend); - backend->is_user = false; - backend->potential_handlers = 1; - backend->refcount = 1; - backend->kern.ready = vfs_root_ready; - backend->kern.accept = vfs_root_accept; - *mount = (struct vfs_mount){ - .prev = NULL, - .prefix = NULL, - .prefix_len = 0, - .backend = backend, - .refs = 1, - }; - - // what a mess. - // TODO register funcs - struct vfs_mount *ps2 = kmalloc(sizeof *ps2); - backend = kmalloc(sizeof *backend); - backend->is_user = false; - backend->potential_handlers = 1; - backend->refcount = 1; - backend->kern.ready = vfs_ps2_ready; - backend->kern.accept = vfs_ps2_accept; - *ps2 = (struct vfs_mount){ - .prev = mount, - .prefix = "/ps2", - .prefix_len = 4, - .backend = backend, - .refs = 1, - }; - - // oh god oh fuck - struct vfs_mount *com = kmalloc(sizeof *com); - backend = kmalloc(sizeof *backend); - backend->is_user = false; - backend->potential_handlers = 1; - backend->refcount = 1; - backend->kern.ready = vfs_com1_ready; - backend->kern.accept = vfs_com1_accept; - *com = (struct vfs_mount){ - .prev = ps2, - .prefix = "/com1", - .prefix_len = 5, - .backend = backend, - .refs = 1, - }; - - return com; + return mount_root; } struct vfs_mount *vfs_mount_resolve( @@ -90,8 +40,20 @@ void vfs_mount_remref(struct vfs_mount *mnt) { if (mnt->backend) vfs_backend_refdown(mnt->backend); if (mnt->prefix_owned) - kfree(mnt->prefix); + kfree((void*)mnt->prefix); kfree(mnt); if (prev) vfs_mount_remref(prev); } + +void vfs_mount_root_register(const char *path, struct vfs_backend *backend) { + struct vfs_mount *mount = kmalloc(sizeof *mount); + *mount = (struct vfs_mount){ + .prev = mount_root, + .prefix = path, + .prefix_len = strlen(path), + .backend = backend, + .refs = 1, + }; + mount_root = mount; +} diff --git a/src/kernel/vfs/mount.h b/src/kernel/vfs/mount.h index 4aa08d4..bcd3460 100644 --- a/src/kernel/vfs/mount.h +++ b/src/kernel/vfs/mount.h @@ -4,7 +4,7 @@ struct vfs_mount { struct vfs_mount *prev; - char *prefix; + const char *prefix; size_t prefix_len; bool prefix_owned; struct vfs_backend *backend; @@ -20,3 +20,5 @@ struct vfs_mount *vfs_mount_resolve( struct vfs_mount *top, const char *path, size_t path_len); /** Decrements the reference count, potentially freeing the mount. */ void vfs_mount_remref(struct vfs_mount *mnt); + +void vfs_mount_root_register(const char *path, struct vfs_backend *backend); diff --git a/src/kernel/vfs/request.c b/src/kernel/vfs/request.c index 3e7c9f0..0c3084d 100644 --- a/src/kernel/vfs/request.c +++ b/src/kernel/vfs/request.c @@ -134,5 +134,5 @@ void vfs_backend_refdown(struct vfs_backend *b) { if (--(b->refcount) > 0) return; assert(!b->queue); - kfree(b); + if (b->heap) kfree(b); } diff --git a/src/kernel/vfs/request.h b/src/kernel/vfs/request.h index 9dc6088..e034bc6 100644 --- a/src/kernel/vfs/request.h +++ b/src/kernel/vfs/request.h @@ -14,6 +14,7 @@ struct vfs_backend { * struct process * struct handle */ + bool heap; size_t potential_handlers; // 0 - orphaned struct vfs_request *queue; @@ -64,3 +65,11 @@ void vfs_backend_tryaccept(struct vfs_backend *); void vfs_backend_user_accept(struct vfs_request *req); void vfs_backend_refdown(struct vfs_backend *); + + +#define BACKEND_KERN(ready, accept) ((struct vfs_backend){\ + .is_user = false, \ + .heap = false, \ + .potential_handlers = 1, \ + .refcount = 1, \ + .kern = {ready, accept}}) diff --git a/src/kernel/vfs/root.c b/src/kernel/vfs/root.c index 989c77e..8ba2043 100644 --- a/src/kernel/vfs/root.c +++ b/src/kernel/vfs/root.c @@ -130,7 +130,7 @@ static int handle(struct vfs_request *req, bool *ready) { } } -void vfs_root_accept(struct vfs_request *req) { +static void accept(struct vfs_request *req) { if (req->caller) { bool ready = true; int ret = handle(req, &ready); @@ -142,6 +142,9 @@ void vfs_root_accept(struct vfs_request *req) { } } -bool vfs_root_ready(struct vfs_backend *self) { +static bool is_ready(struct vfs_backend *self) { return true; } + +static struct vfs_backend backend = BACKEND_KERN(is_ready, accept); +void vfs_root_init(void) { vfs_mount_root_register("", &backend); } diff --git a/src/kernel/vfs/root.h b/src/kernel/vfs/root.h index 3db958d..dd72202 100644 --- a/src/kernel/vfs/root.h +++ b/src/kernel/vfs/root.h @@ -1,5 +1,2 @@ #pragma once -#include <kernel/vfs/request.h> - -void vfs_root_accept(struct vfs_request *); -bool vfs_root_ready(struct vfs_backend *); +void vfs_root_init(void); |