From ef941d094b4dbf61dd5c485c593a4b932ec55323 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Fri, 10 Jul 2026 13:02:05 -0400 Subject: [PATCH 01/18] init: remove tty from main Signed-off-by: Michael Shaw --- init/main.c | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/init/main.c b/init/main.c index 3a37c76..335b1f5 100644 --- a/init/main.c +++ b/init/main.c @@ -59,19 +59,6 @@ static void kmain() vfs_init(); arch_init(); device_layer_init(); - tty_layer_init(); - - void *serial_port = arch_serial_init(); - tty_printf = tty_create("ttyS0", serial_port); - - int rows; - int cols; - - tty_clear_screen(tty_printf); - tty_get_size(tty_printf, &rows, &cols); - tty_setlf(tty_printf); - tty_set_cursor_pos(tty_printf, 0, 0); - /* start the kernel */ page_init(); do_initcalls(); From da4023412c61ee1ed435963236288fe991ef6b5f Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Fri, 10 Jul 2026 14:27:14 -0400 Subject: [PATCH 02/18] Makefile: run: use kvm Signed-off-by: Michael Shaw --- scripts/Makefile.limine | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/Makefile.limine b/scripts/Makefile.limine index 66e92f9..5ae0859 100644 --- a/scripts/Makefile.limine +++ b/scripts/Makefile.limine @@ -37,6 +37,7 @@ run: edk2-ovmf $(IMG) qemu-system-$(ARCH) \ -M q35 \ -drive if=pflash,unit=0,format=raw,file=edk2-ovmf/ovmf-code-$(ARCH).fd,readonly=on \ + -accel kvm \ -nographic \ -hda $(IMG) \ $(QEMUFLAGS) From 9bbe42b95a1c1c286e31088c41feed32152fddeb Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Fri, 10 Jul 2026 14:53:41 -0400 Subject: [PATCH 03/18] main: remove debug function Signed-off-by: Michael Shaw --- init/main.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/init/main.c b/init/main.c index 335b1f5..a45cf2e 100644 --- a/init/main.c +++ b/init/main.c @@ -25,6 +25,8 @@ #include #include #include +#include +#include #include #include @@ -43,13 +45,6 @@ static void do_initcalls() (*fn)(); } -static void indent(int indent) -{ - for (int i = 0; i < indent; i++) { - kprintf(" "); - } -} - static void kmain() { /* data types */ @@ -58,11 +53,9 @@ static void kmain() kalloc_init(); vfs_init(); arch_init(); - device_layer_init(); - /* start the kernel */ - page_init(); - do_initcalls(); + page_init(); + ramfs_init(); struct limine_file *file = module_res->modules[0]; fs_t *initramfs; @@ -72,6 +65,11 @@ static void kmain() if (res < 0) panic("failed to load initramfs: %s\n", strerror(-res)); + /* start the kernel */ + devfs_init(); + device_layer_init(); + do_initcalls(); + /* do nothing, for now :) */ yield(); } From 7c8b46eedab638ccdc572f71c8a073250aedf2a0 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Fri, 10 Jul 2026 15:13:05 -0400 Subject: [PATCH 04/18] vfs: add mount support Signed-off-by: Michael Shaw --- fs/vfs/mount.c | 283 +++++++++++++++++++++++++++++++++++++++++++++++ fs/vfs/vfs.c | 35 +++--- include/fs/vfs.h | 20 +++- 3 files changed, 318 insertions(+), 20 deletions(-) create mode 100644 fs/vfs/mount.c diff --git a/fs/vfs/mount.c b/fs/vfs/mount.c new file mode 100644 index 0000000..d39f6d2 --- /dev/null +++ b/fs/vfs/mount.c @@ -0,0 +1,283 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include +#include + +#include + +#include +#include + +static struct mount *root_mount = NULL; +static spinlock_t mount_lock = 0; + +fs_t *vfs_get_root(void) +{ + if (!root_mount) + return NULL; + return root_mount->fs; +} + +struct mount *vfs_get_root_mount(void) +{ + return root_mount; +} + +struct mount *mount_find_child(struct mount *parent, dirent_t *dent) +{ + spinlock_acquire(&mount_lock); + + list_item_t *item = parent->children.head; + while (item) { + struct mount *m = item->ptr; + if (m->mountpoint == dent) { + spinlock_release(&mount_lock); + return m; + } + item = item->next; + } + + spinlock_release(&mount_lock); + return NULL; +} + +static int load_inode(dirent_t *dent) +{ + if (dent->inode) + return 0; + + int res = dent->fs->fs_ops.open_inode(dent->fs, dent->ino_num, dent, &dent->inode); + return res; +} + +static int mount_traverse(const char *path, struct mount **out_mount, dirent_t **out_dirent) +{ + if (!root_mount) + return -ENOSYS; + + while (*path == '/') + path++; + + if (*path == '\0') { + *out_mount = root_mount; + *out_dirent = root_mount->fs->root_dirent; + if (*out_dirent) + dirent_inc_ref(*out_dirent); + return 0; + } + + struct mount *cur = root_mount; + dirent_t *cur_dent = root_mount->fs->root_dirent; + + if (!cur_dent) + return -ENOENT; + + dirent_inc_ref(cur_dent); + + const char *p = path; + + while (*p != '\0') { + const char *comp_start = p; + while (*p != '/' && *p != '\0') + p++; + + size_t comp_len = p - comp_start; + if (comp_len > FILE_NAME_MAX_LEN) { + dirent_dec_ref(cur_dent); + return -ENAMETOOLONG; + } + + char name[FILE_NAME_MAX_LEN + 1]; + memcpy(name, comp_start, comp_len); + name[comp_len] = '\0'; + + while (*p == '/') + p++; + + bool is_last = (*p == '\0'); + + dirent_t *found = NULL; + int res = inode_find_dirent(cur_dent->inode, name, &found); + if (res < 0) { + dirent_dec_ref(cur_dent); + return res; + } + + res = load_inode(found); + if (res < 0) { + dirent_dec_ref(found); + dirent_dec_ref(cur_dent); + return res; + } + + if (is_last) { + dirent_dec_ref(cur_dent); + *out_mount = cur; + *out_dirent = found; + return 0; + } + + struct mount *child = NULL; + list_item_t *item = cur->children.head; + while (item) { + struct mount *m = item->ptr; + if (m->mountpoint == found) { + child = m; + break; + } + item = item->next; + } + + dirent_dec_ref(cur_dent); + + if (child) { + dirent_dec_ref(found); + cur = child; + cur_dent = cur->fs->root_dirent; + if (!cur_dent) + return -ENOENT; + dirent_inc_ref(cur_dent); + } else { + cur_dent = found; + } + } + + dirent_dec_ref(cur_dent); + *out_mount = cur; + *out_dirent = cur->fs->root_dirent; + if (*out_dirent) + dirent_inc_ref(*out_dirent); + return 0; +} + +int vfs_mount(const char *path, fs_t *fs, u64 flags) +{ + if (!fs) + return -EINVAL; + + spinlock_acquire(&mount_lock); + + if (!path) { + if (root_mount) { + spinlock_release(&mount_lock); + return -EBUSY; + } + + struct mount *m = kzalloc(sizeof(*m), ALLOC_KERN); + if (!m) { + spinlock_release(&mount_lock); + return -ENOMEM; + } + + m->parent = NULL; + m->fs = fs; + m->mountpoint = NULL; + m->flags = flags; + m->child_link = NULL; + list_create(&m->children); + + root_mount = m; + spinlock_release(&mount_lock); + return 0; + } + + struct mount *parent = NULL; + dirent_t *mp = NULL; + int res = mount_traverse(path, &parent, &mp); + if (res < 0) { + spinlock_release(&mount_lock); + return res; + } + + if ((mp->inode->mode & S_IFMT) != S_IFDIR) { + dirent_dec_ref(mp); + spinlock_release(&mount_lock); + return -ENOTDIR; + } + + list_item_t *item = parent->children.head; + while (item) { + struct mount *m = item->ptr; + if (m->mountpoint == mp) { + dirent_dec_ref(mp); + spinlock_release(&mount_lock); + return -EBUSY; + } + item = item->next; + } + + struct mount *m = kzalloc(sizeof(*m), ALLOC_KERN); + if (!m) { + dirent_dec_ref(mp); + spinlock_release(&mount_lock); + return -ENOMEM; + } + + m->parent = parent; + m->fs = fs; + m->mountpoint = mp; + m->flags = flags; + + list_create(&m->children); + + m->child_link = list_insert(&parent->children, m); + if (!m->child_link) { + dirent_dec_ref(mp); + kfree(m); + spinlock_release(&mount_lock); + return -ENOMEM; + } + + spinlock_release(&mount_lock); + return 0; +} + +int vfs_umount(const char *path) +{ + if (!path) + return -EINVAL; + + spinlock_acquire(&mount_lock); + + struct mount *parent = NULL; + dirent_t *mp = NULL; + int res = mount_traverse(path, &parent, &mp); + if (res < 0) { + spinlock_release(&mount_lock); + return res; + } + + struct mount *target = NULL; + list_item_t *item = parent->children.head; + while (item) { + struct mount *m = item->ptr; + if (m->mountpoint == mp) { + target = m; + break; + } + item = item->next; + } + + dirent_dec_ref(mp); + + if (!target) { + spinlock_release(&mount_lock); + return -EINVAL; + } + + if (target == root_mount) { + spinlock_release(&mount_lock); + return -EINVAL; + } + + if (target->children.head) { + spinlock_release(&mount_lock); + return -EBUSY; + } + + list_remove(target->child_link); + dirent_dec_ref(target->mountpoint); + kfree(target); + + spinlock_release(&mount_lock); + return 0; +} diff --git a/fs/vfs/vfs.c b/fs/vfs/vfs.c index a673cc5..8a2faad 100644 --- a/fs/vfs/vfs.c +++ b/fs/vfs/vfs.c @@ -6,7 +6,6 @@ #include #include -static fs_t *root_fs = NULL; static list_t fs_type_list = {0}; static char *path_trim(const char *name) @@ -85,7 +84,7 @@ static int next_level(const char *path, dirent_t *dent, dirent_t **out_dirent) return -ENOENT; } -int vfs_openat(dirent_t *in, const char *name, dirent_t **out) +int vfs_openat(struct mount *mnt, dirent_t *in, const char *name, dirent_t **out) { if (!in || !name || !out) return -EINVAL; @@ -94,6 +93,7 @@ int vfs_openat(dirent_t *in, const char *name, dirent_t **out) if (!path_optr) return -ENOMEM; + struct mount *cur_mount = mnt; dirent_t *current_dir = in; dirent_inc_ref(current_dir); @@ -118,6 +118,19 @@ int vfs_openat(dirent_t *in, const char *name, dirent_t **out) dirent_dec_ref(current_dir); current_dir = next_dir; + + struct mount *child = mount_find_child(cur_mount, current_dir); + if (child) { + dirent_dec_ref(current_dir); + cur_mount = child; + current_dir = child->fs->root_dirent; + if (!current_dir) { + ret = -ENOENT; + goto out; + } + dirent_inc_ref(current_dir); + } + cur_path += res; while (*cur_path == '/') @@ -140,10 +153,11 @@ int vfs_open(const char *abs_path, dirent_t **out) if (!abs_path || !out) return -EINVAL; - if (!root_fs->root_dirent) + struct mount *root = vfs_get_root_mount(); + if (!root || !root->fs || !root->fs->root_dirent) return -ENOENT; - return vfs_openat(root_fs->root_dirent, abs_path, out); + return vfs_openat(root, root->fs->root_dirent, abs_path, out); } void vfs_close(dirent_t *dent) @@ -284,16 +298,6 @@ int vfs_unlink(dirent_t *parent, const char *name) return res; } -int vfs_mount(const char *mount_point, fs_t *fs_to_mount) -{ - if (!mount_point || !strcmp(mount_point, "/")) { - root_fs = fs_to_mount; - return 0; - } - - return -ENOSYS; -} - void vfs_register(struct fs_driver *fs) { struct fs_driver *ins = kalloc(sizeof(*fs), ALLOC_KERN); @@ -308,8 +312,5 @@ void vfs_register(struct fs_driver *fs) void vfs_init(void) { inode_init(); - root_fs = kalloc(sizeof(fs_t), ALLOC_KERN); - memset(root_fs, 0, sizeof(*root_fs)); - list_create(&fs_type_list); } diff --git a/include/fs/vfs.h b/include/fs/vfs.h index d6add85..e8a2062 100644 --- a/include/fs/vfs.h +++ b/include/fs/vfs.h @@ -3,6 +3,7 @@ #define _VFS_H_ #include +#include #include #include @@ -81,19 +82,32 @@ struct _fs { struct dir_context { void *cookie; - bool init; + bool init; +}; + +struct mount { + struct mount *parent; + list_t children; + list_item_t *child_link; + fs_t *fs; + dirent_t *mountpoint; + u64 flags; }; void vfs_init(void); void vfs_register(struct fs_driver *fs); int vfs_open(const char *abs_path, dirent_t **out); -int vfs_openat(dirent_t *in, const char *name, dirent_t **out); +int vfs_openat(struct mount *mnt, dirent_t *in, const char *name, dirent_t **out); void vfs_close(dirent_t *dent); int vfs_readdir(dirent_t *dir, struct dir_context *ctx, dirent_t **out); int vfs_creat(dirent_t *parent, const char *name, u64 mode, int flags, dirent_t **out); int vfs_unlink(dirent_t *parent, const char *name); -int vfs_mount(const char *mount_point, fs_t *fs_to_mount); +int vfs_mount(const char *path, fs_t *fs, u64 flags); +int vfs_umount(const char *path); +fs_t *vfs_get_root(void); +struct mount *vfs_get_root_mount(void); +struct mount *mount_find_child(struct mount *parent, dirent_t *dent); ssize_t vfs_write(inode_t *ino, char *buf, size_t size, size_t offset); ssize_t vfs_read(inode_t *ino, char *buf, size_t size, size_t offset); From 474468ef047940fdc896c5ecadcb76582e70e0fe Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Fri, 10 Jul 2026 18:00:19 -0400 Subject: [PATCH 05/18] block: move block from drivers to kernel Signed-off-by: Michael Shaw --- include/{drivers => kernel}/block.h | 0 {drivers => kernel}/block.c | 3 +-- 2 files changed, 1 insertion(+), 2 deletions(-) rename include/{drivers => kernel}/block.h (100%) rename {drivers => kernel}/block.c (92%) diff --git a/include/drivers/block.h b/include/kernel/block.h similarity index 100% rename from include/drivers/block.h rename to include/kernel/block.h diff --git a/drivers/block.c b/kernel/block.c similarity index 92% rename from drivers/block.c rename to kernel/block.c index cc958da..401b8de 100644 --- a/drivers/block.c +++ b/kernel/block.c @@ -1,7 +1,6 @@ /* SPDX-License-Identifier: GPL-2.0-only */ #include - -#include +#include ssize_t block_read(struct block_device *dev, char *buf, size_t offset, size_t n) { From b5a6c0bc97295af52281da1f7810572f331e64f4 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Sat, 11 Jul 2026 20:11:43 -0400 Subject: [PATCH 06/18] Makefile: create initramfs.cpio Signed-off-by: Michael Shaw --- Makefile | 1 + scripts/Makefile.limine | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 2eb8485..5e1ccf7 100644 --- a/Makefile +++ b/Makefile @@ -90,6 +90,7 @@ $(KERNEL): $(KOBJ) linker.ld .PHONY: clean clean: $(Q)$(MAKE) -f scripts/Makefile.build _clean obj=. + $(Q)rm -rf $(INITRAMFS) $(Q)rm -rf $(IMG) $(Q)rm -rf iso_root $(Q)rm -rf os_root diff --git a/scripts/Makefile.limine b/scripts/Makefile.limine index 5ae0859..309ef77 100644 --- a/scripts/Makefile.limine +++ b/scripts/Makefile.limine @@ -1,5 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-only # Makefile stuff for all the booting and bootloader related stuff +# +INITRAMFS := initramfs.cpio $(IMG): limine/limine $(KERNEL) rm -rf $(IMG) @@ -32,8 +34,13 @@ limine/limine: LDFLAGS="$(HOST_LDFLAGS)" \ LIBS="$(HOST_LIBS)" +$(INITRAMFS): initramfs/ + $(Q)echo " CPIO $@" + $(Q)mkdir -p initramfs/ + $(Q)cd initramfs/ && find . | cpio -oH newc > ../$(INITRAMFS) + .PHONY += run -run: edk2-ovmf $(IMG) +run: edk2-ovmf $(INITRAMFS) $(IMG) qemu-system-$(ARCH) \ -M q35 \ -drive if=pflash,unit=0,format=raw,file=edk2-ovmf/ovmf-code-$(ARCH).fd,readonly=on \ From bba8fc7476e428abe70160e818efa6406a0fbdf7 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Sat, 11 Jul 2026 23:54:15 -0400 Subject: [PATCH 07/18] amd64: serial: register tty in serial driver Signed-off-by: Michael Shaw --- amd64/amd64_init.c | 2 +- amd64/drivers/serial.c | 26 +++++++++++++++++++++----- amd64/include/drivers/serial.h | 2 +- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/amd64/amd64_init.c b/amd64/amd64_init.c index 76b7f99..fd8a3e6 100644 --- a/amd64/amd64_init.c +++ b/amd64/amd64_init.c @@ -28,7 +28,7 @@ void arch_early_init() cr4 |= (1 << 9) | (1 << 10); cr4_write(cr4); - tty_early.serial_ops = &tty_early_ops; + tty_early.serial_ops = tty_early_ops; tty_early.priv = (void *)COM1; tty_printf = &tty_early; diff --git a/amd64/drivers/serial.c b/amd64/drivers/serial.c index 6507804..38dd216 100644 --- a/amd64/drivers/serial.c +++ b/amd64/drivers/serial.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: GPL-2.0-only */ #include #include +#include #include #include @@ -30,11 +31,6 @@ void init_serial_port(int port) outb(port | 4, 0x0F); /* Enter normal operation */ } -void *arch_serial_init() -{ - return (void *)COM1; -} - void arch_serial_write(struct tty *tty, char c) { /* Wait for line to be clear */ @@ -54,3 +50,23 @@ char arch_serial_read(struct tty *tty) return inb(port); } + +int serial_port_tty_init(struct tty **out) +{ + init_serial_port(COM1); + + *out = kalloc(sizeof(struct tty), ALLOC_KERN); + + if (!(*out)) + return -ENOMEM; + + (*out)[0].priv = (void *)COM1; + (*out)[0].serial_ops.serial_getchar = arch_serial_read; + (*out)[0].serial_ops.serial_putchar = arch_serial_write; + + dev_t com1 = device_create((*out + 0), DEV_CLASS_TTY, "ttyS0"); + + (*out)[0].dev = com1; + + return 1; +} diff --git a/amd64/include/drivers/serial.h b/amd64/include/drivers/serial.h index a9061d8..0bf3184 100644 --- a/amd64/include/drivers/serial.h +++ b/amd64/include/drivers/serial.h @@ -7,6 +7,6 @@ #include -void *arch_serial_init(); +int serial_port_tty_init(struct tty **out); #endif /* _SERIAL_H_ */ From f177a8e65d22c90ca7258067c017690c5200d8fd Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Sat, 11 Jul 2026 23:54:48 -0400 Subject: [PATCH 08/18] tty: make ops struct not a pointer Signed-off-by: Michael Shaw --- drivers/tty/tty.c | 6 +++--- include/drivers/tty.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/tty/tty.c b/drivers/tty/tty.c index 0f8fd2c..1c3d21e 100644 --- a/drivers/tty/tty.c +++ b/drivers/tty/tty.c @@ -17,19 +17,19 @@ struct tty *tty_create(const char *name, void *priv) tty->dev = tty_device; tty->priv = priv; - tty->serial_ops = &arch_serial_ops; + tty->serial_ops = arch_serial_ops; return tty; } void tty_putchar(struct tty *tty, char c) { - tty->serial_ops->serial_putchar(tty, c); + tty->serial_ops.serial_putchar(tty, c); } char tty_getchar(struct tty *tty) { - return tty->serial_ops->serial_getchar(tty); + return tty->serial_ops.serial_getchar(tty); } void tty_write(struct tty *tty, const char *s) diff --git a/include/drivers/tty.h b/include/drivers/tty.h index b50758f..33f74c4 100644 --- a/include/drivers/tty.h +++ b/include/drivers/tty.h @@ -12,7 +12,7 @@ struct tty_serial_ops { struct tty { dev_t dev; void *priv; - struct tty_serial_ops *serial_ops; + struct tty_serial_ops serial_ops; }; void tty_layer_init(); From 1dde87423d98b3443b35b5b8b6138559b195c36e Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Sat, 11 Jul 2026 23:55:21 -0400 Subject: [PATCH 09/18] string: fix NULL pointer dereferences Signed-off-by: Michael Shaw --- lib/string.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/string.c b/lib/string.c index 1f33ef9..a6d9517 100644 --- a/lib/string.c +++ b/lib/string.c @@ -225,6 +225,8 @@ char *dirname(const char *path) if (!path) return strdup("."); char *copy = strdup(path); + if (!copy) + return NULL; char *res = udirname(copy); char *ret = strdup(res); kfree(copy); @@ -236,6 +238,8 @@ char *basename(const char *path) if (!path) return strdup("."); char *copy = strdup(path); + if (!copy) + return NULL; char *res = ubasename(copy); char *ret = strdup(res); kfree(copy); From 6c2613ab9638498f1dc1a980dc27414555572ef6 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Sat, 11 Jul 2026 23:56:04 -0400 Subject: [PATCH 10/18] buddy: add bounds check Signed-off-by: Michael Shaw --- mem/buddy.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mem/buddy.c b/mem/buddy.c index 3a5f014..c9c6e9c 100644 --- a/mem/buddy.c +++ b/mem/buddy.c @@ -138,6 +138,8 @@ static void init_freelist(struct buddy_zone *bzone) while (remaining >= PAGE_SIZE) { size_t order = buddy_order(remaining); + if (order > bzone->max_order) + order = bzone->max_order; cur->order = order; cur->zone = bzone; From 7833400893510fd4d280908a60bab7856f75c51f Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Sat, 11 Jul 2026 23:56:26 -0400 Subject: [PATCH 11/18] kfree: check if pointer NULL Signed-off-by: Michael Shaw --- mem/alloc.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mem/alloc.c b/mem/alloc.c index 3b88050..f2ded45 100644 --- a/mem/alloc.c +++ b/mem/alloc.c @@ -88,6 +88,9 @@ void *kzalloc(size_t size, u64 flags) void kfree(void *ptr) { + if (!ptr) + return; + rbnode_t *node = rbt_search(&kalloc_tree, (u64)ptr); if (!node) From 4b17525d334b4f2ee07ac971a89835caca7673e4 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Sat, 11 Jul 2026 23:57:47 -0400 Subject: [PATCH 12/18] devfs: add devfs Signed-off-by: Michael Shaw --- fs/vfs/devfs.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++ include/fs/devfs.h | 15 ++++++++ 2 files changed, 108 insertions(+) create mode 100644 fs/vfs/devfs.c create mode 100644 include/fs/devfs.h diff --git a/fs/vfs/devfs.c b/fs/vfs/devfs.c new file mode 100644 index 0000000..50f61d5 --- /dev/null +++ b/fs/vfs/devfs.c @@ -0,0 +1,93 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include +#include +#include + +#include +#include +#include +#include + +#include + +static fs_t *f_devfs = NULL; + +static ssize_t i_read_chr(inode_t *inode, char *buf, size_t size, size_t offset) +{ + *buf = tty_getchar(inode->data); + return 1; +} + +static ssize_t i_write_chr(inode_t *inode, char *buf, size_t size, size_t offset) +{ + for (size_t i = offset; i < size + offset; i++) + tty_putchar(inode->data, buf[offset + i]); + + return size; +} + +static ssize_t i_read_blk(inode_t *inode, char *buf, size_t size, size_t offset) +{ + return block_read(inode->data, buf, offset, size); +} + +static ssize_t i_write_blk(inode_t *inode, char *buf, size_t size, size_t offset) +{ + return block_write(inode->data, buf, offset, size); +} + +static int devfs_insert_device(const char *name, u64 mode, dirent_t **out) +{ + return vfs_creat(f_devfs->root_dirent, name, mode, 0, out); +} + +int devfs_insert_device_chr(const char *name, struct tty *device, dirent_t **out) +{ + int res = devfs_insert_device(name, S_IFCHR, out); + + if (res < 0) + return res; + + inode_t *ino = (*out)->inode; + write_lock(&ino->rwlock); + + ino->data = device; + ino->i_ops.write = i_write_chr; + ino->i_ops.read = i_read_chr; + + write_unlock(&ino->rwlock); + + return res; +} + +int devfs_insert_device_blk(const char *name, struct block_device *device, dirent_t **out) +{ + int res = devfs_insert_device(name, S_IFBLK, out); + + if (res < 0) + return res; + + inode_t *ino = (*out)->inode; + write_lock(&ino->rwlock); + + ino->data = device; + ino->i_ops.write = i_write_blk; + ino->i_ops.read = i_read_blk; + + write_unlock(&ino->rwlock); + + return res; +} + +void devfs_init(void) +{ + int res = ramfs_create(0, &f_devfs); + if (res < 0) + panic("failed to create devfs: %s\n", strerror(-res)); + + res = vfs_mount("/dev", f_devfs, 0); + if (res < 0) + panic("failed to mount /dev: %s\n", strerror(-res)); + + krdy("devfs ready\n"); +} diff --git a/include/fs/devfs.h b/include/fs/devfs.h new file mode 100644 index 0000000..7b18283 --- /dev/null +++ b/include/fs/devfs.h @@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _DEVFS_H_ +#define _DEVFS_H_ + +#include + +#include + +#include + +int devfs_insert_device_chr(const char *name, struct tty *device, dirent_t **out); +int devfs_insert_device_blk(const char *name, struct block_device *device, dirent_t **out); +void devfs_init(); + +#endif /* _DEVFS_H_ */ From df3a785e4356af40fe3699e4a33d9d831d652e4f Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Sat, 11 Jul 2026 23:58:17 -0400 Subject: [PATCH 13/18] device: add devfs Signed-off-by: Michael Shaw --- kernel/Makefile | 1 + kernel/device.c | 30 +++++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/kernel/Makefile b/kernel/Makefile index b8039ae..af3538b 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -1,2 +1,3 @@ # SPDX-License-Identifier: GPL-2.0-only obj-y += device.o +obj-y += block.o diff --git a/kernel/device.c b/kernel/device.c index 1d7ff74..b3fc145 100644 --- a/kernel/device.c +++ b/kernel/device.c @@ -1,6 +1,12 @@ /* SPDX-License-Identifier: GPL-2.0-only */ +#include #include +#include #include +#include + +#include + #include #include #include @@ -16,9 +22,11 @@ static mem_cache_t *dev_info_cache = NULL; static rbtree_t device_tree = {0}; +static u64 device_ticker = 0; + dev_t device_create(void *private, dev_class_t class, const char *name) { - dev_t key = rbt_next_key(&device_tree); + dev_t key = atomic_xadd(&device_ticker, 1); rbnode_t *node = rbt_insert(&device_tree, key); size_t name_len = strlen(name); @@ -31,6 +39,26 @@ dev_t device_create(void *private, dev_class_t class, const char *name) node->value = (vaddr_t)info; + dirent_t *devfile = NULL; + + if (class == DEV_CLASS_BLK) { + int res = devfs_insert_device_blk(name, private, &devfile); + + if (res < 0) { + kerr("failed to insert blk device node: %s\n", strerror(-res)); + return res; + } + } else if (class == DEV_CLASS_TTY) { + int res = devfs_insert_device_chr(name, private, &devfile); + + if (res < 0) { + kerr("failed to insert chr device node: %s\n", strerror(-res)); + return res; + } else { + krdy("inserted chrdev node %s: value %d\n", name, res); + } + } + return key; } From 402761ca8054cda9efba81e2938daa5ac5d98d2a Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Sat, 11 Jul 2026 23:59:12 -0400 Subject: [PATCH 14/18] initcpio: add missing argument to vfs_mount Signed-off-by: Michael Shaw --- fs/vfs/initcpio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs/vfs/initcpio.c b/fs/vfs/initcpio.c index 6444f80..f6ff845 100644 --- a/fs/vfs/initcpio.c +++ b/fs/vfs/initcpio.c @@ -150,7 +150,7 @@ int cpio_read_and_init(const char *file, size_t len, fs_t **out) if (res < 0) return res; - res = vfs_mount(NULL, initramfs); + res = vfs_mount(NULL, initramfs, 0); if (res < 0) return res; From 05ccdb3d0e01d6609f787278bbefaaa273f5cc16 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Sun, 12 Jul 2026 00:00:47 -0400 Subject: [PATCH 15/18] ramfs: manually init, do not use initcall Signed-off-by: Michael Shaw --- fs/vfs/ramfs.c | 1 - include/fs/ramfs.h | 2 ++ 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/fs/vfs/ramfs.c b/fs/vfs/ramfs.c index cd62dfa..61433ad 100644 --- a/fs/vfs/ramfs.c +++ b/fs/vfs/ramfs.c @@ -243,4 +243,3 @@ void ramfs_init(void) vfs_register(&ramfs_type); } -DRIVER_ENTRY(ramfs_init); diff --git a/include/fs/ramfs.h b/include/fs/ramfs.h index ba28b58..5fea5ff 100644 --- a/include/fs/ramfs.h +++ b/include/fs/ramfs.h @@ -9,4 +9,6 @@ int ramfs_create(u64 flags, fs_t **out); int ramfs_umount(fs_t *fs); +void ramfs_init(void); + #endif /* _RAMFS_H_ */ From 1e4b96e6fb90fba30a93312a399729e46edfdd92 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Sun, 12 Jul 2026 00:01:23 -0400 Subject: [PATCH 16/18] device: add DEV_CLASS_BLK Signed-off-by: Michael Shaw --- include/kernel/device.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/kernel/device.h b/include/kernel/device.h index 60a78d8..ce7c1e0 100644 --- a/include/kernel/device.h +++ b/include/kernel/device.h @@ -5,8 +5,9 @@ #include #define DEV_CLASS_TTY 1 +#define DEV_CLASS_BLK 2 -typedef u64 dev_t; +typedef i64 dev_t; typedef u64 dev_class_t; From 57d3962d5c9613d83aecb9d59ac11840018c20e0 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Sun, 12 Jul 2026 00:01:50 -0400 Subject: [PATCH 17/18] fs: vfs: Makefile: add mount.o and devfs.o Signed-off-by: Michael Shaw --- fs/vfs/Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/fs/vfs/Makefile b/fs/vfs/Makefile index 7da549d..c84ae09 100644 --- a/fs/vfs/Makefile +++ b/fs/vfs/Makefile @@ -1,5 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-only obj-y += vfs.o +obj-y += mount.o obj-y += inode.o obj-y += ramfs.o +obj-y += devfs.o obj-y += initcpio.o From 7c7d052286f6dc5454e7b08b5b1a58d28a2d5a78 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Sun, 12 Jul 2026 00:14:46 -0400 Subject: [PATCH 18/18] main: add tty layer to kmain Signed-off-by: Michael Shaw --- init/main.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/init/main.c b/init/main.c index a45cf2e..80c855a 100644 --- a/init/main.c +++ b/init/main.c @@ -68,6 +68,14 @@ static void kmain() /* start the kernel */ devfs_init(); device_layer_init(); + tty_layer_init(); + + /* init main fallback tty */ + struct tty *main_tty = NULL; + serial_port_tty_init(&main_tty); + + tty_printf = main_tty; + do_initcalls(); /* do nothing, for now :) */