diff --git a/.gitignore b/.gitignore index 89bfacc..dcbb8c9 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,3 @@ edk2-ovmf/ initramfs/ Makefile.local *.ko -*.gen.* diff --git a/Makefile b/Makefile index 24c7b1d..c96fa3b 100644 --- a/Makefile +++ b/Makefile @@ -58,8 +58,7 @@ ifeq ($(ARCH),x86_64) KDIRS += amd64/ endif -KOBJ_CORE := $(foreach d,$(KDIRS),$(d)/built-in.o) -KOBJ := $(KOBJ_CORE) kernel/module/built-in.o +KOBJ := $(foreach d,$(KDIRS),$(d)/built-in.o) obj-y := $(KDIRS) @@ -91,33 +90,7 @@ all: $(KERNEL) modules __build_all: $(Q)$(MAKE) --no-print-directory -f scripts/Makefile.build _all obj=. -$(KOBJ_CORE): __build_all - -SYMTAB_C := kernel/module/symtab.gen.c - -$(SYMTAB_C): $(KOBJ_CORE) - $(Q)echo " GEN $@" - $(Q){ \ - echo '#include '; \ - echo 'struct ksym { const char *name; void *addr; };'; \ - nm --defined-only $^ 2>/dev/null | \ - awk '$$2 ~ /^[TDRB]$$/ && $$3 ~ /^[a-zA-Z_][a-zA-Z0-9_]*$$/ && \ - $$3 != "kernel_symtab" && $$3 != "kernel_symtab_count" && $$3 != "driver_load" {print "extern char " $$3 ";"}' | \ - sort -u; \ - echo ''; \ - echo 'struct ksym kernel_symtab[] = {'; \ - nm --defined-only $^ 2>/dev/null | \ - awk '$$2 ~ /^[TDRB]$$/ && $$3 ~ /^[a-zA-Z_][a-zA-Z0-9_]*$$/ && \ - $$3 != "kernel_symtab" && $$3 != "kernel_symtab_count" && $$3 != "driver_load" {print " {\"" $$3 "\", (void *)&" $$3 "},"}' | \ - sort -u; \ - echo ' {NULL, NULL}};'; \ - echo 'size_t kernel_symtab_count = '$$(nm --defined-only $^ 2>/dev/null | \ - awk '$$2 ~ /^[TDRB]$$/ && $$3 ~ /^[a-zA-Z_][a-zA-Z0-9_]*$$/ && \ - $$3 != "kernel_symtab" && $$3 != "kernel_symtab_count" && $$3 != "driver_load" && $$3 != "_init"' | wc -l)';'; \ - } > $@ - -kernel/module/built-in.o: $(SYMTAB_C) - $(Q)$(MAKE) --no-print-directory -f scripts/Makefile.build _all obj=kernel/module +$(KOBJ): __build_all $(KERNEL): $(KOBJ) linker.ld $(Q)$(MAKE) --no-print-directory -C usr.bin @@ -138,12 +111,11 @@ modules_install: modules $(Q)echo -n > initramfs/modules/modules.alias $(Q)for ko in initramfs/modules/*.ko; do \ [ -f "$$ko" ] || continue; \ - modname=$$(basename "$$ko" .ko); \ readelf -p .modinfo "$$ko" 2>/dev/null | \ sed -n 's/^\s*\[.*\]\s*//p' | \ while IFS= read -r line; do \ case "$$line" in alias=*) \ - echo "$${line#alias=} $$modname" >> initramfs/modules/modules.alias ;; \ + echo "$${line#alias=} $$ko" >> initramfs/modules/modules.alias ;; \ esac; \ done; \ done diff --git a/amd64/arch/entry.S b/amd64/arch/entry.S index 53fcde4..de27da7 100644 --- a/amd64/arch/entry.S +++ b/amd64/arch/entry.S @@ -138,28 +138,9 @@ _timer_handler: .global sched_restore_state sched_restore_state: - mov rsp, [rdi + 8] /* load RSP from thread->kstack_ptr */ - pop_all - ret - -.global thread_wait -thread_wait: - push_all - mov rax, qword ptr gs:[24] /* cur_thread */ - mov [rax + 8], rsp /* save rsp to kstack_ptr */ - mov qword ptr [rax + 32], 1 /* state = PROC_STATE_WAITING */ - call sched_add_thread /* re-add to run queue */ - call schedule - /* not reached */ - -.global thread_block -thread_block: - push_all - mov rax, qword ptr gs:[24] /* cur_thread */ - mov [rax + 8], rsp /* save rsp to kstack_ptr */ - mov qword ptr [rax + 32], 3 /* state = PROC_STATE_BLOCKED */ - call schedule - /* not reached */ + mov rsp, [rdi + 8] /* load RSP from thread->kstack_ptr */ + pop_all + ret .global _syscall_enter _syscall_enter: diff --git a/amd64/include/arch/proc.h b/amd64/include/arch/proc.h index dbe4640..54a352b 100644 --- a/amd64/include/arch/proc.h +++ b/amd64/include/arch/proc.h @@ -46,7 +46,4 @@ void enter_userspace_init(struct thread *thread, uaddr_t entry); struct pt_regs *current_pt_regs(); u32 cpu_id(); -void thread_wait(); -void thread_block(); - #endif /* _ARCH_PROC_H_ */ diff --git a/amd64/include/kernel/mtx.h b/amd64/include/kernel/mtx.h deleted file mode 100644 index 43adaa3..0000000 --- a/amd64/include/kernel/mtx.h +++ /dev/null @@ -1,10 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-only */ -#ifndef _MTX_H_ -#define _MTX_H_ - -#include - -void mtx_lock(mtx_t *l); -void mtx_unlock(mtx_t *l); - -#endif /* _MTX_H_ */ diff --git a/amd64/include/kernel/spinlock.h b/amd64/include/kernel/spinlock.h index a41d463..06ad9c2 100644 --- a/amd64/include/kernel/spinlock.h +++ b/amd64/include/kernel/spinlock.h @@ -8,7 +8,6 @@ #define CLEANUP_SLOCK CLEANUP(cleanup_spinlock) typedef u32 spinlock_t; -typedef spinlock_t mtx_t; void spinlock_acquire(spinlock_t *lock); void spinlock_release(spinlock_t *lock); diff --git a/amd64/kernel/spinlock.c b/amd64/kernel/spinlock.c index e6f1e28..809380b 100644 --- a/amd64/kernel/spinlock.c +++ b/amd64/kernel/spinlock.c @@ -2,7 +2,6 @@ #include #include #include -#include static uint64_t spinlock_attempt_acquire(spinlock_t *lock) { @@ -25,14 +24,3 @@ void cleanup_spinlock(spinlock_t **lock_ptr) if (lock_ptr && *lock_ptr) spinlock_release(*lock_ptr); } - -void mtx_lock(mtx_t *l) -{ - while (spinlock_attempt_acquire(l)) - thread_wait(); -} - -void mtx_unlock(mtx_t *l) -{ - spinlock_release(l); -} diff --git a/drivers/Makefile b/drivers/Makefile index 25f80aa..820fc9c 100644 --- a/drivers/Makefile +++ b/drivers/Makefile @@ -1,5 +1,5 @@ # SPDX-License-Identifier: GPL-2.0-only obj-y += acpi/ obj-y += pci/ +obj-y += ahci.o obj-y += term/ -obj-m += ahci.o diff --git a/drivers/ahci.c b/drivers/ahci.c index da92459..058a111 100644 --- a/drivers/ahci.c +++ b/drivers/ahci.c @@ -392,8 +392,11 @@ static int ahci_init_device(struct pci_device *device) void ahci_init() { + list_t dev_list; + list_create(&dev_list); + driver_register("ahci", DRIVER_PCI, ahci_init_device, NULL, pci_driver_class(1, 6, 1)); + krdy("AHCI ready\n"); } DRIVER_ENTRY_PRIORITY(ahci_init, 2); -DRIVER_ALIAS("pci:010601"); diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index 1e4ab2e..9b5a448 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -57,8 +57,6 @@ size_t pci_find_device(u16 major, u16 minor, list_t *out) { size_t n = 0; - list_lock(out); - list_item_t *item = pci_devices.head; while (item) { @@ -71,22 +69,9 @@ size_t pci_find_device(u16 major, u16 minor, list_t *out) item = item->next; } - list_unlock(out); - return n; } -static void device_init(struct pci_device *pdev) -{ - u32 driverclass = pci_driver_class(pdev->class_major, pdev->class_minor, pdev->prog_if); - struct driver *driver = driver_find(DRIVER_PCI, driverclass); - - if (driver && driver->init_device) { - pci_init_t f = driver->init_device; - f(pdev); - } -} - static void host_probe_devices(struct pci_host *host) { for (int bus = 0; bus < 256; bus++) { @@ -140,16 +125,21 @@ static void host_probe_devices(struct pci_host *host) if ((bars[2] & 0x6) == 0x4) pdev->bar2 |= ((u64)bars[3] << 32); - list_lock(&pci_devices); list_insert(&pci_devices, pdev); - list_unlock(&pci_devices); if (func == 0) { uint8_t header = (pci_config_read32(host, bus, dev, 0, 0x0C) >> 16) & 0xFF; if (!(header & 0x80)) break; } - device_init(pdev); + /* call the driver */ + u32 driverclass = pci_driver_class(pdev->class_major, pdev->class_minor, pdev->prog_if); + struct driver *driver = driver_find(DRIVER_PCI, driverclass); + + if (driver && driver->init_device) { + pci_init_t f = driver->init_device; + f(pdev); + } kdebug("PCI device: vendor=%04x, id=%x, major=%x, minor=%x\n", pdev->vendor, pdev->device_id, pdev->class_major, pdev->class_minor); diff --git a/fs/vfs/inode.c b/fs/vfs/inode.c index 4069eab..051bd75 100644 --- a/fs/vfs/inode.c +++ b/fs/vfs/inode.c @@ -30,8 +30,6 @@ int dirent_populate_children(dirent_t *dir) return n; } - list_lock(&inode->dirents); - for (size_t i = 0; i < n; i++) { dirent_t *dent = dents + i; dirent_t *insert = dirent_dup(dent); @@ -47,7 +45,6 @@ int dirent_populate_children(dirent_t *dir) } } - list_unlock(&inode->dirents); write_unlock(&inode->rwlock); kfree(dents); return 0; @@ -65,7 +62,6 @@ cleanup: item = next; } } - list_unlock(&inode->dirents); write_unlock(&inode->rwlock); kfree(dents); return -ENOMEM; @@ -125,8 +121,6 @@ inode_t *inode_create(void) void inode_free(inode_t *inode) { - list_lock(&inode->dirents); - while (inode->dirents.head) { list_item_t *item = inode->dirents.head; dirent_t *dent = item->ptr; @@ -138,8 +132,6 @@ void inode_free(inode_t *inode) } } - list_unlock(&inode->dirents); - kfree(inode->data); slab_free(inode_slab, inode); } diff --git a/fs/vfs/mount.c b/fs/vfs/mount.c index 418b395..b3e64fe 100644 --- a/fs/vfs/mount.c +++ b/fs/vfs/mount.c @@ -128,9 +128,7 @@ int vfs_mount(const char *path, fs_t *fs, u64 flags) list_create(&m->children); - list_lock(&parent->children); m->child_link = list_insert(&parent->children, m); - list_unlock(&parent->children); if (!m->child_link) { dirent_dec_ref(mp); kfree(m); @@ -183,9 +181,7 @@ int vfs_umount(const char *path) return -EBUSY; } - list_lock(target->child_link->list); list_remove(target->child_link); - list_unlock(target->child_link->list); dirent_dec_ref(target->mountpoint); kfree(target); diff --git a/fs/vfs/ramfs.c b/fs/vfs/ramfs.c index e2d4759..eaed320 100644 --- a/fs/vfs/ramfs.c +++ b/fs/vfs/ramfs.c @@ -139,13 +139,10 @@ static int link(inode_t *parent_dir, inode_t *child, const char *name, dirent_t new->inode = child; - list_lock(&parent_dir->dirents); if (!list_insert(&parent_dir->dirents, new)) { - list_unlock(&parent_dir->dirents); dirent_free(new); return -ENOMEM; } - list_unlock(&parent_dir->dirents); *out = new; return 0; @@ -156,21 +153,17 @@ static int unlink(inode_t *parent_dir, const char *name) if (!parent_dir || !name) return -EINVAL; - list_lock(&parent_dir->dirents); - list_item_t *item = parent_dir->dirents.head; while (item) { dirent_t *dent = item->ptr; if (!strcmp(dent->name, name)) { list_remove(item); - list_unlock(&parent_dir->dirents); dirent_dec_ref(dent); return 0; } item = item->next; } - list_unlock(&parent_dir->dirents); return -ENOENT; } diff --git a/fs/vfs/vfs.c b/fs/vfs/vfs.c index 194f6b6..de6e00e 100644 --- a/fs/vfs/vfs.c +++ b/fs/vfs/vfs.c @@ -313,13 +313,8 @@ void vfs_register(struct fs_driver *fs) return; memcpy(ins, fs, sizeof(*fs)); - list_lock(&fs_type_list); - if (!list_insert(&fs_type_list, ins)) { - list_unlock(&fs_type_list); + if (!list_insert(&fs_type_list, ins)) kfree(ins); - } else { - list_unlock(&fs_type_list); - } } void vfs_init(void) diff --git a/include/kernel/driver.h b/include/kernel/driver.h index bafa244..45ade23 100644 --- a/include/kernel/driver.h +++ b/include/kernel/driver.h @@ -3,47 +3,23 @@ #define _DRIVER_H_ #include -#include -#include #include #define DRIVER_ENTRY(fn) static void (*__initcall_##fn)(void) __attribute((used, section(".initcall"))) = fn #define DRIVER_ENTRY_PRIORITY(fn, pr) static void (*__initcall_##fn)(void) __attribute((used, section(".initcall." #pr))) = fn -#define DRIVER_EXIT(fn) static void (*__exitcall_##fn)(void) __attribute((used, section(".exitcall"))) = fn - -#define DRIVER_ALIAS(_alias) \ - static const char __driver_alias_##__LINE__[] \ - __attribute__((used, section(".modinfo"), aligned(1))) = "alias=" _alias #define DRIVER_PCI 1 typedef int (*pci_init_t)(struct pci_device *dev); -struct ksym { - const char *name; - void *addr; -}; - struct driver { char name[32]; u32 type; void *init_device; void *deinit_device; - void (*exit)(void); - void *base; - size_t size; - - u32 n_users; }; -struct driver *driver_register(const char *name, u32 type, void *init, void *deinit, u32 class); +int driver_register(const char *name, u32 type, void *init, void *deinit, u32 class); struct driver *driver_find(u32 type, u32 class); -int driver_load(const char *name); -int driver_unload(const char *name); - -extern list_t pending_drivers; - -extern struct ksym kernel_symtab[]; -extern size_t kernel_symtab_count; #endif /* _DRIVER_H_ */ diff --git a/include/kernel/proc.h b/include/kernel/proc.h index a3e285a..17b50df 100644 --- a/include/kernel/proc.h +++ b/include/kernel/proc.h @@ -13,7 +13,6 @@ #define PROC_STATE_RUNNING 0 #define PROC_STATE_WAITING 1 #define PROC_STATE_DNS 2 /* do not schedule */ -#define PROC_STATE_BLOCKED 3 typedef u64 pid_t; typedef u64 tid_t; diff --git a/include/lib/list.h b/include/lib/list.h index d8fbaee..66d5bca 100644 --- a/include/lib/list.h +++ b/include/lib/list.h @@ -17,15 +17,13 @@ typedef struct _list_item { typedef struct _list_head { struct _list_item *head; struct _list_item *tail; - mtx_t lock; + spinlock_t lock; } list_t; void list_create(list_t *list); -void list_destroy(list_t *list); +void list_destroy(list_t * list); list_item_t *list_insert(list_t *list, void *item); void list_remove(list_item_t *item); -void list_lock(list_t *list); -void list_unlock(list_t *list); void list_init(); #endif /* _LIST_H_ */ diff --git a/include/lib/string.h b/include/lib/string.h index 52238d4..44b5b60 100644 --- a/include/lib/string.h +++ b/include/lib/string.h @@ -15,7 +15,6 @@ void *memcpy_user(struct proc *proc, void *dest, const void *src, size_t num); int memcmp(const void *aa, const void *bb, size_t num); int strcmp(const char *a, const char *b); int strncmp(const char *a, const char *b, size_t n); -char *strchr(const char *s, int c); void *memset(void *str, int c, size_t n); char *strcpy(char *dest, const char *src); char *strncpy(char *dest, const char *src, size_t n); diff --git a/init/main.c b/init/main.c index 2961e8c..76ad5ff 100644 --- a/init/main.c +++ b/init/main.c @@ -14,7 +14,6 @@ #include #include #include -#include #include diff --git a/kernel/driver.c b/kernel/driver.c index 62b42c2..714f7dd 100644 --- a/kernel/driver.c +++ b/kernel/driver.c @@ -3,45 +3,13 @@ #include #include #include -#include static rbtree_t driver_registry; -static char *modules_alias_data; -static size_t modules_alias_len; -list_t pending_drivers = {0}; - -struct driver *driver_register(const char *name, u32 type, void *init, void *deinit, u32 class) +int driver_register(const char *name, u32 type, void *init, void *deinit, u32 class) { - if (!pending_drivers.head && !pending_drivers.tail) - list_create(&pending_drivers); - - struct driver *driver = NULL; - - list_lock(&pending_drivers); - - list_item_t *item = pending_drivers.head; - while (item) { - struct driver *d = item->ptr; - if (strcmp(d->name, name) == 0) { - driver = d; - list_remove(item); - break; - } - item = item->next; - } - - list_unlock(&pending_drivers); - - if (!driver) { - driver = kzalloc(sizeof(struct driver), ALLOC_KERN); - strncpy(driver->name, name, sizeof(driver->name) - 1); - driver->exit = NULL; - driver->base = NULL; - driver->size = 0; - driver->n_users = 0; - } - + struct driver *driver = kzalloc(sizeof(struct driver), ALLOC_KERN); + strncpy(driver->name, name, sizeof(driver->name) - 1); driver->type = type; driver->init_device = init; driver->deinit_device = deinit; @@ -50,57 +18,12 @@ struct driver *driver_register(const char *name, u32 type, void *init, void *dei key |= class; rbnode_t *node = rbt_insert(&driver_registry, key); - if (!node) { - kfree(driver); - return NULL; - } + if (!node) + return -ENOMEM; + node->ptr = driver; - return driver; -} - -static const char *alias_lookup(u32 type, u32 class) -{ - if (!modules_alias_data) { - dirent_t *dent; - if (vfs_open("/modules/modules.alias", &dent) < 0) - return NULL; - - modules_alias_len = dent->inode->file_size; - modules_alias_data = kalloc(modules_alias_len + 1, ALLOC_KERN); - if (!modules_alias_data) - return NULL; - - vfs_read(dent->inode, modules_alias_data, modules_alias_len, 0); - modules_alias_data[modules_alias_len] = '\0'; - } - - char want[64]; - if (type == DRIVER_PCI) - snprintf(want, sizeof(want), "pci:%06x ", class); - else - return NULL; - - size_t want_len = strlen(want); - char *p = modules_alias_data; - - while (p && p < modules_alias_data + modules_alias_len) { - if (strncmp(p, want, want_len) == 0) { - char *nl = strchr(p + want_len, '\n'); - size_t name_len = nl ? (size_t)(nl - p - want_len) : strlen(p + want_len); - char *modname = kalloc(name_len + 1, ALLOC_KERN); - if (modname) { - memcpy(modname, p + want_len, name_len); - modname[name_len] = '\0'; - } - return modname; - } - - char *nl = strchr(p, '\n'); - p = nl ? nl + 1 : NULL; - } - - return NULL; + return 0; } struct driver *driver_find(u32 type, u32 class) @@ -109,13 +32,6 @@ struct driver *driver_find(u32 type, u32 class) key |= class; rbnode_t *node = rbt_search(&driver_registry, key); - if (!node) { - const char *modname = alias_lookup(type, class); - if (modname && driver_load(modname) == 0) { - node = rbt_search(&driver_registry, key); - } - } - if (!node) return NULL; diff --git a/kernel/module/Makefile b/kernel/module/Makefile deleted file mode 100644 index 9937513..0000000 --- a/kernel/module/Makefile +++ /dev/null @@ -1,3 +0,0 @@ -# SPDX-License-Identifier: GPL-2.0-only -obj-y += modload.o -obj-y += symtab.gen.o diff --git a/kernel/module/modload.c b/kernel/module/modload.c deleted file mode 100644 index dc0cda4..0000000 --- a/kernel/module/modload.c +++ /dev/null @@ -1,306 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-only */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "../proc/elf.h" - -struct sec_info { - int loadable; - size_t file_off; - size_t size; - size_t align; - size_t load_off; -}; - -static const char elf_sig[4] = {0x7f, 'E', 'L', 'F'}; -extern char __kernel_end[]; -static vaddr_t mod_next_vaddr; - -static void *alloc_module(size_t size) -{ - if (!mod_next_vaddr) - mod_next_vaddr = ALIGN_UP((vaddr_t)__kernel_end, PAGE_SIZE); - - size_t npages = ALIGN_UP(size, PAGE_SIZE) / PAGE_SIZE; - vaddr_t base = ALIGN_UP(mod_next_vaddr, PAGE_SIZE); - mod_next_vaddr = base + npages * PAGE_SIZE; - - for (size_t i = 0; i < npages; i++) { - void *v = buddy_alloc(0); - if (!v) - return NULL; - page_map(kpgd, TO_PHYS(v), base + i * PAGE_SIZE, PAGE_SIZE, PAGE_RW, NULL); - } - - tlb_flush(); - return (void *)base; -} - -static void *sym_lookup(const char *name) -{ - for (size_t i = 0; i < kernel_symtab_count; i++) - if (strcmp(kernel_symtab[i].name, name) == 0) - return kernel_symtab[i].addr; - return NULL; -} - -static int str_prefix(const char *str, const char *pre) -{ - return strncmp(str, pre, strlen(pre)) == 0; -} - -static int elf_validate(const struct ehdr *ehdr, size_t fsize) -{ - if (fsize < sizeof(struct ehdr)) - return -1; - if (memcmp(elf_sig, ehdr->e_ident, 4)) - return -1; - if (ehdr->e_type != ET_REL || ehdr->e_machine != EM_X86_64) - return -1; - if (ehdr->e_shentsize != sizeof(struct shdr) || ehdr->e_shnum == 0) - return -1; - return 0; -} - -static int find_symtab(const struct shdr *shdr, size_t n, int *syndx, int *stndx) -{ - *syndx = -1; - *stndx = -1; - for (size_t i = 0; i < n; i++) - if (shdr[i].sh_type == SHT_SYMTAB) { - *syndx = i; - *stndx = shdr[i].sh_link; - } - return (*syndx < 0 || *stndx < 0) ? -1 : 0; -} - -static size_t layout_sections(const struct shdr *shdr, size_t n, struct sec_info *secs) -{ - size_t total = 0; - for (size_t i = 0; i < n; i++) { - secs[i].loadable = 0; - if (!(shdr[i].sh_flags & SHF_ALLOC)) - continue; - secs[i].loadable = 1; - secs[i].file_off = shdr[i].sh_offset; - secs[i].size = shdr[i].sh_size; - secs[i].align = shdr[i].sh_addralign; - if (secs[i].align < 16) - secs[i].align = 16; - secs[i].load_off = ALIGN_UP(total, secs[i].align); - total = secs[i].load_off + secs[i].size; - } - return ALIGN_UP(total, PAGE_SIZE); -} - -static int load_sections(const struct shdr *shdr, struct sec_info *secs, size_t n, const char *file, size_t fsize, u8 *base) -{ - for (size_t i = 0; i < n; i++) { - if (!secs[i].loadable) - continue; - u8 *dst = base + secs[i].load_off; - if (shdr[i].sh_type == SHT_NOBITS) { - memset(dst, 0, secs[i].size); - } else { - if (secs[i].file_off + secs[i].size > fsize) - return -1; - memcpy(dst, file + secs[i].file_off, secs[i].size); - } - } - return 0; -} - -static int -resolve_symbols(const struct sym *symtab, size_t n_sym, const char *strtab, struct sec_info *secs, size_t n_sec, u8 *base, u64 *out) -{ - for (size_t i = 0; i < n_sym; i++) { - const struct sym *s = &symtab[i]; - if (s->st_shndx == SHN_UNDEF) { - if (s->st_name == 0) { - out[i] = 0; - continue; - } - void *addr = sym_lookup(strtab + s->st_name); - if (!addr) - return -1; - out[i] = (u64)addr; - } else if (s->st_shndx < n_sec && secs[s->st_shndx].loadable) { - out[i] = (u64)(base + secs[s->st_shndx].load_off) + s->st_value; - } else { - out[i] = s->st_value; - } - } - return 0; -} - -static int apply_reloc(const struct shdr *shdr, struct sec_info *secs, size_t n_sec, const char *file, u8 *base, const u64 *sym_val) -{ - for (size_t i = 0; i < n_sec; i++) { - if (shdr[i].sh_type != SHT_RELA) - continue; - int tgt = shdr[i].sh_info; - if (tgt < 0 || (size_t)tgt >= n_sec || !secs[tgt].loadable) - continue; - - const struct rela *r = (const struct rela *)(file + shdr[i].sh_offset); - size_t nr = shdr[i].sh_size / sizeof(struct rela); - u8 *tgt_base = base + secs[tgt].load_off; - - for (size_t j = 0; j < nr; j++) { - u64 sv = sym_val[ELF64_R_SYM(r[j].r_info)]; - u64 ty = ELF64_R_TYPE(r[j].r_info); - u64 off = r[j].r_offset; - u64 ad = r[j].r_addend; - u64 p = (u64)(tgt_base + off); - - switch (ty) { - case R_X86_64_NONE: - break; - case R_X86_64_64: - *(u64 *)(tgt_base + off) = sv + ad; - break; - case R_X86_64_PC32: - case R_X86_64_PLT32: - *(u32 *)(tgt_base + off) = (u32)(sv + ad - p); - break; - case R_X86_64_32S: - *(u32 *)(tgt_base + off) = (u32)(sv + ad); - break; - default: - return -1; - } - } - } - return 0; -} - -static void call_initcalls(const struct shdr *shdr, struct sec_info *secs, size_t n_sec, const char *shstrtab, u8 *base) -{ - for (size_t i = 0; i < n_sec; i++) { - if (!secs[i].loadable) - continue; - if (!str_prefix(shstrtab + shdr[i].sh_name, ".initcall")) - continue; - - void (**fn)(void) = (void (**)(void))(base + secs[i].load_off); - size_t nf = secs[i].size / sizeof(void (*)(void)); - for (size_t j = 0; j < nf; j++) - if (fn[j]) - fn[j](); - } -} - -static int elf_load(const char *name, const char *file, size_t fsize) -{ - const struct ehdr *ehdr = (const struct ehdr *)file; - - if (elf_validate(ehdr, fsize)) - return -ENOEXEC; - - size_t n_sec = ehdr->e_shnum; - const struct shdr *shdr = (const struct shdr *)(file + ehdr->e_shoff); - - int syndx, stndx; - if (find_symtab(shdr, n_sec, &syndx, &stndx)) - return -ENOEXEC; - - const struct sym *symtab = (const struct sym *)(file + shdr[syndx].sh_offset); - size_t n_sym = shdr[syndx].sh_size / sizeof(struct sym); - const char *strtab = file + shdr[stndx].sh_offset; - const char *shstrtab = file + shdr[ehdr->e_shstrndx].sh_offset; - - struct sec_info *secs = kalloc(n_sec * sizeof(struct sec_info), ALLOC_KERN); - if (!secs) - return -ENOMEM; - - size_t total = layout_sections(shdr, n_sec, secs); - - u8 *base = alloc_module(total); - if (!base) { - kfree(secs); - return -ENOMEM; - } - - if (load_sections(shdr, secs, n_sec, file, fsize, base)) - goto err; - - u64 *sym_val = kalloc(n_sym * sizeof(u64), ALLOC_KERN); - if (!sym_val) - goto err; - - if (resolve_symbols(symtab, n_sym, strtab, secs, n_sec, base, sym_val)) - goto err_sym; - - if (apply_reloc(shdr, secs, n_sec, file, base, sym_val)) - goto err_sym; - - void (*exit)(void) = NULL; - for (size_t i = 0; i < n_sec; i++) { - if (!secs[i].loadable) - continue; - if (!str_prefix(shstrtab + shdr[i].sh_name, ".exitcall")) - continue; - void (**fn)(void) = (void (**)(void))(base + secs[i].load_off); - if (fn[0]) - exit = fn[0]; - break; - } - - if (exit || 1) { - struct driver *d = kzalloc(sizeof(struct driver), ALLOC_KERN); - if (d) { - strncpy(d->name, name, sizeof(d->name) - 1); - d->exit = exit; - d->base = base; - d->size = total; - d->n_users = 1; - list_lock(&pending_drivers); - list_insert(&pending_drivers, d); - list_unlock(&pending_drivers); - } - } - - call_initcalls(shdr, secs, n_sec, shstrtab, base); - - kfree(sym_val); - kfree(secs); - return 0; - -err_sym: - kfree(sym_val); -err: - kfree(secs); - return -ENOEXEC; -} - -int driver_load(const char *name) -{ - char path[128]; - snprintf(path, sizeof(path), "/modules/%s.ko", name); - - dirent_t *dent; - if (vfs_open(path, &dent) < 0) - return -ENOENT; - - char *buf = kalloc(dent->inode->file_size, ALLOC_KERN); - if (!buf) - return -ENOMEM; - - vfs_read(dent->inode, buf, dent->inode->file_size, 0); - - int ret = elf_load(name, buf, dent->inode->file_size); - - kfree(buf); - return ret; -} - -int driver_unload(const char *name) -{ - return -ENOSYS; -} diff --git a/kernel/proc/elf.h b/kernel/proc/elf.h index 9e7e50f..3d63c8d 100644 --- a/kernel/proc/elf.h +++ b/kernel/proc/elf.h @@ -4,54 +4,22 @@ #include -#define ET_NONE 0 -#define ET_REL 1 -#define ET_EXEC 2 +#define ET_NONE 0 /* No file type */ +#define ET_EXEC 2 /* Executable file */ -#define PT_NULL 0 -#define PT_LOAD 1 -#define PT_DYNAMIC 2 -#define PT_INTERP 3 +#define PT_NULL 0 /* Program header table entry unused */ +#define PT_LOAD 1 /* Loadable program segment */ +#define PT_DYNAMIC 2 /* Dynamic linking information */ +#define PT_INTERP 3 /* Program interpreter */ -#define PF_X (1 << 0) -#define PF_W (1 << 1) -#define PF_R (1 << 2) +#define PF_X (1 << 0) /* Segment is executable */ +#define PF_W (1 << 1) /* Segment is writable */ +#define PF_R (1 << 2) /* Segment is readable */ #define EM_X86_64 62 + #define EV_CURRENT 1 -#define SHT_NULL 0 -#define SHT_PROGBITS 1 -#define SHT_SYMTAB 2 -#define SHT_STRTAB 3 -#define SHT_RELA 4 -#define SHT_NOBITS 8 - -#define SHF_WRITE (1 << 0) -#define SHF_ALLOC (1 << 1) -#define SHF_EXECINSTR (1 << 2) - -#define STB_LOCAL 0 -#define STB_GLOBAL 1 -#define STB_WEAK 2 -#define STT_NOTYPE 0 -#define STT_OBJECT 1 -#define STT_FUNC 2 -#define STT_SECTION 3 - -#define ELF64_ST_BIND(i) ((i) >> 4) -#define ELF64_ST_TYPE(i) ((i) & 0xf) - -#define R_X86_64_NONE 0 -#define R_X86_64_64 1 -#define R_X86_64_PC32 2 -#define R_X86_64_PLT32 4 -#define R_X86_64_32S 11 - -#define ELF64_R_SYM(i) ((i) >> 32) -#define ELF64_R_TYPE(i) ((i) & 0xffffffff) -#define SHN_UNDEF 0 - typedef u16 Elf64_Half; typedef u32 Elf64_Word; typedef u64 Elf64_Xword; @@ -59,59 +27,31 @@ typedef u64 Elf64_Addr; typedef u64 Elf64_Off; struct ehdr { - u8 e_ident[16]; - Elf64_Half e_type; - Elf64_Half e_machine; - Elf64_Word e_version; - Elf64_Addr e_entry; - Elf64_Off e_phoff; - Elf64_Off e_shoff; - Elf64_Word e_flags; - Elf64_Half e_ehsize; - Elf64_Half e_phentsize; - Elf64_Half e_phnum; - Elf64_Half e_shentsize; - Elf64_Half e_shnum; - Elf64_Half e_shstrndx; + u8 e_ident[16]; /* Magic number and other info */ + Elf64_Half e_type; /* Object file type */ + Elf64_Half e_machine; /* Architecture */ + Elf64_Word e_version; /* Object file version */ + Elf64_Addr e_entry; /* Entry point virtual address */ + Elf64_Off e_phoff; /* Program header table file offset */ + Elf64_Off e_shoff; /* Section header table file offset */ + Elf64_Word e_flags; /* Processor-specific flags */ + Elf64_Half e_ehsize; /* ELF header size in bytes */ + Elf64_Half e_phentsize; /* Program header table entry size */ + Elf64_Half e_phnum; /* Program header table entry count */ + Elf64_Half e_shentsize; /* Section header table entry size */ + Elf64_Half e_shnum; /* Section header table entry count */ + Elf64_Half e_shstrndx; /* Section header string table index */ } PACKED; struct phdr { - Elf64_Word p_type; - Elf64_Word p_flags; - Elf64_Off p_offset; - Elf64_Addr p_vaddr; - Elf64_Addr p_paddr; - Elf64_Xword p_filesz; - Elf64_Xword p_memsz; - Elf64_Xword p_align; -} PACKED; - -struct shdr { - Elf64_Word sh_name; - Elf64_Word sh_type; - Elf64_Xword sh_flags; - Elf64_Addr sh_addr; - Elf64_Off sh_offset; - Elf64_Xword sh_size; - Elf64_Word sh_link; - Elf64_Word sh_info; - Elf64_Xword sh_addralign; - Elf64_Xword sh_entsize; -} PACKED; - -struct sym { - Elf64_Word st_name; - unsigned char st_info; - unsigned char st_other; - Elf64_Half st_shndx; - Elf64_Addr st_value; - Elf64_Xword st_size; -} PACKED; - -struct rela { - Elf64_Addr r_offset; - Elf64_Xword r_info; - Elf64_Xword r_addend; + Elf64_Word p_type; /* Segment type */ + Elf64_Word p_flags; /* Segment flags */ + Elf64_Off p_offset; /* Segment file offset */ + Elf64_Addr p_vaddr; /* Segment virtual address */ + Elf64_Addr p_paddr; /* Segment physical address */ + Elf64_Xword p_filesz; /* Segment size in file */ + Elf64_Xword p_memsz; /* Segment size in memory */ + Elf64_Xword p_align; /* Segment alignment */ } PACKED; int elf_get_headers(const char *file, struct ehdr **out_ehdr, struct phdr **out_phdrs, uaddr_t *entry); diff --git a/kernel/tty/tee.c b/kernel/tty/tee.c index edf51e8..967a662 100644 --- a/kernel/tty/tee.c +++ b/kernel/tty/tee.c @@ -33,9 +33,7 @@ struct tty *tty_tee_create(const char *name) struct tty *ret = tty_create(name, list); if (!ret) { - list_lock(list); list_destroy(list); - list_unlock(list); kfree(list); return NULL; } @@ -51,9 +49,7 @@ void tty_tee_add(struct tty *tee, struct tty *child) return; list_t *list = tee->priv; - list_lock(list); list_insert(list, child); - list_unlock(list); } void tty_tee_remove(struct tty *tee, struct tty *child) @@ -62,18 +58,13 @@ void tty_tee_remove(struct tty *tee, struct tty *child) if (!list) return; - list_lock(list); - list_item_t *item = list->head; while (item) { if (item->ptr == child) { list_remove(item); - list_unlock(list); return; } item = item->next; } - - list_unlock(list); } diff --git a/lib/list.c b/lib/list.c index 2e830cf..ac51975 100644 --- a/lib/list.c +++ b/lib/list.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-2.0-only */ -#include +#include #include #include #include @@ -15,7 +15,9 @@ void list_create(list_t *list) void list_destroy(list_t *list) { + spinlock_acquire(&list->lock); list_item_t *item = list->head; + while (item) { list_item_t *next = item->next; slab_free(list_cache, item); @@ -23,14 +25,17 @@ void list_destroy(list_t *list) } list->head = NULL; list->tail = NULL; + spinlock_release(&list->lock); } list_item_t *list_insert(list_t *list, void *item) { + spinlock_acquire(&list->lock); list_item_t *ret = slab_alloc(list_cache); - if (!ret) + if (!ret) { + spinlock_release(&list->lock); return NULL; - + } ret->ptr = item; ret->list = list; ret->next = NULL; @@ -45,6 +50,8 @@ list_item_t *list_insert(list_t *list, void *item) } list->tail = ret; + spinlock_release(&list->lock); + return ret; } @@ -52,6 +59,7 @@ void list_remove(list_item_t *item) { list_t *list = item->list; + spinlock_acquire(&list->lock); if (item == list->head) list->head = item->next; if (item == list->tail) @@ -59,22 +67,14 @@ void list_remove(list_item_t *item) if (item->next) item->next->prev = item->prev; + if (item->prev) item->prev->next = item->next; + spinlock_release(&list->lock); slab_free(list_cache, item); } -void list_lock(list_t *list) -{ - mtx_lock(&list->lock); -} - -void list_unlock(list_t *list) -{ - mtx_unlock(&list->lock); -} - void list_init() { list_cache = slab_create("list_cache", sizeof(list_item_t), 64 * KB, 0x8); diff --git a/scripts/Makefile.limine b/scripts/Makefile.limine index 0425ddf..9dafe0c 100644 --- a/scripts/Makefile.limine +++ b/scripts/Makefile.limine @@ -33,7 +33,7 @@ limine/limine: LDFLAGS="$(HOST_LDFLAGS)" \ LIBS="$(HOST_LIBS)" -$(INITRAMFS): initramfs/ modules_install +$(INITRAMFS): initramfs/ $(Q)echo " CPIO $@" $(Q)mkdir -p initramfs/dev $(Q)cd initramfs/ && find . | cpio -oH newc > ../$(INITRAMFS)