Compare commits

...
Author SHA1 Message Date
cb2c3e1053
ahci: load AHCI as module
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-08-12 16:06:58 -04:00
e8effb194e
list: switch lock to mtx
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-08-12 15:38:24 -04:00
9c50a1df92
kernel: add thread_wait, thread_block and mtx
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-08-12 14:59:34 -04:00
071a5651e0
list: change locking discipline
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-08-12 14:50:47 -04:00
2cf76fd225
main: remove testmod
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-08-12 14:50:40 -04:00
8d147a14c4
driver: track number of users on driver
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-08-12 14:44:56 -04:00
3b83b2f741
ahci: set to modular
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-08-12 14:41:15 -04:00
6f201ed370
driver: add module teardown to driver struct
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-08-12 14:39:57 -04:00
3abfd32fbb
module: add exitcall function
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-08-12 12:15:59 -04:00
e8de5a33af
kernel: add loadable module system
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-08-12 11:27:46 -04:00
acae0cac63
gitignore: fix .gen.* rule
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-08-12 10:49:12 -04:00
b7097b0854
gitignore: ignore generated files
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-08-12 00:23:13 -04:00
26 changed files with 672 additions and 76 deletions

1
.gitignore vendored
View file

@ -14,3 +14,4 @@ edk2-ovmf/
initramfs/
Makefile.local
*.ko
*.gen.*

View file

@ -58,7 +58,8 @@ ifeq ($(ARCH),x86_64)
KDIRS += amd64/
endif
KOBJ := $(foreach d,$(KDIRS),$(d)/built-in.o)
KOBJ_CORE := $(foreach d,$(KDIRS),$(d)/built-in.o)
KOBJ := $(KOBJ_CORE) kernel/module/built-in.o
obj-y := $(KDIRS)
@ -90,7 +91,33 @@ all: $(KERNEL) modules
__build_all:
$(Q)$(MAKE) --no-print-directory -f scripts/Makefile.build _all obj=.
$(KOBJ): __build_all
$(KOBJ_CORE): __build_all
SYMTAB_C := kernel/module/symtab.gen.c
$(SYMTAB_C): $(KOBJ_CORE)
$(Q)echo " GEN $@"
$(Q){ \
echo '#include <stddef.h>'; \
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
$(KERNEL): $(KOBJ) linker.ld
$(Q)$(MAKE) --no-print-directory -C usr.bin
@ -111,11 +138,12 @@ 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=} $$ko" >> initramfs/modules/modules.alias ;; \
echo "$${line#alias=} $$modname" >> initramfs/modules/modules.alias ;; \
esac; \
done; \
done

View file

@ -142,6 +142,25 @@ sched_restore_state:
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 */
.global _syscall_enter
_syscall_enter:
swapgs

View file

@ -46,4 +46,7 @@ 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_ */

View file

@ -0,0 +1,10 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef _MTX_H_
#define _MTX_H_
#include <kernel/spinlock.h>
void mtx_lock(mtx_t *l);
void mtx_unlock(mtx_t *l);
#endif /* _MTX_H_ */

View file

@ -8,6 +8,7 @@
#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);

View file

@ -2,6 +2,7 @@
#include <kernel/common.h>
#include <kernel/spinlock.h>
#include <kernel/atomic.h>
#include <arch/proc.h>
static uint64_t spinlock_attempt_acquire(spinlock_t *lock)
{
@ -24,3 +25,14 @@ 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);
}

View file

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

View file

@ -392,11 +392,8 @@ 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");

View file

@ -57,6 +57,8 @@ 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) {
@ -69,9 +71,22 @@ 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++) {
@ -125,21 +140,16 @@ 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;
}
/* 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);
}
device_init(pdev);
kdebug("PCI device: vendor=%04x, id=%x, major=%x, minor=%x\n", pdev->vendor, pdev->device_id,
pdev->class_major, pdev->class_minor);

View file

@ -30,6 +30,8 @@ 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);
@ -45,6 +47,7 @@ int dirent_populate_children(dirent_t *dir)
}
}
list_unlock(&inode->dirents);
write_unlock(&inode->rwlock);
kfree(dents);
return 0;
@ -62,6 +65,7 @@ cleanup:
item = next;
}
}
list_unlock(&inode->dirents);
write_unlock(&inode->rwlock);
kfree(dents);
return -ENOMEM;
@ -121,6 +125,8 @@ 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;
@ -132,6 +138,8 @@ void inode_free(inode_t *inode)
}
}
list_unlock(&inode->dirents);
kfree(inode->data);
slab_free(inode_slab, inode);
}

View file

@ -128,7 +128,9 @@ 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);
@ -181,7 +183,9 @@ 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);

View file

@ -139,10 +139,13 @@ 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;
@ -153,17 +156,21 @@ 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;
}

View file

@ -313,8 +313,13 @@ void vfs_register(struct fs_driver *fs)
return;
memcpy(ins, fs, sizeof(*fs));
if (!list_insert(&fs_type_list, ins))
list_lock(&fs_type_list);
if (!list_insert(&fs_type_list, ins)) {
list_unlock(&fs_type_list);
kfree(ins);
} else {
list_unlock(&fs_type_list);
}
}
void vfs_init(void)

View file

@ -3,23 +3,47 @@
#define _DRIVER_H_
#include <kernel/common.h>
#include <kernel/spinlock.h>
#include <lib/list.h>
#include <drivers/pci.h>
#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;
};
int driver_register(const char *name, u32 type, void *init, void *deinit, u32 class);
struct driver *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_ */

View file

@ -13,6 +13,7 @@
#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;

View file

@ -17,13 +17,15 @@ typedef struct _list_item {
typedef struct _list_head {
struct _list_item *head;
struct _list_item *tail;
spinlock_t lock;
mtx_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_ */

View file

@ -15,6 +15,7 @@ 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);

View file

@ -14,6 +14,7 @@
#include <kernel/page.h>
#include <kernel/slab.h>
#include <kernel/proc.h>
#include <kernel/driver.h>
#include <arch/init.h>

View file

@ -3,13 +3,45 @@
#include <kernel/slab.h>
#include <lib/string.h>
#include <lib/rbtree.h>
#include <fs/vfs.h>
static rbtree_t driver_registry;
static char *modules_alias_data;
static size_t modules_alias_len;
int driver_register(const char *name, u32 type, void *init, void *deinit, u32 class)
list_t pending_drivers = {0};
struct driver *driver_register(const char *name, u32 type, void *init, void *deinit, u32 class)
{
struct driver *driver = kzalloc(sizeof(struct driver), ALLOC_KERN);
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;
}
driver->type = type;
driver->init_device = init;
driver->deinit_device = deinit;
@ -18,12 +50,57 @@ int driver_register(const char *name, u32 type, void *init, void *deinit, u32 cl
key |= class;
rbnode_t *node = rbt_insert(&driver_registry, key);
if (!node)
return -ENOMEM;
if (!node) {
kfree(driver);
return NULL;
}
node->ptr = driver;
return 0;
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;
}
struct driver *driver_find(u32 type, u32 class)
@ -32,6 +109,13 @@ 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;

3
kernel/module/Makefile Normal file
View file

@ -0,0 +1,3 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-y += modload.o
obj-y += symtab.gen.o

306
kernel/module/modload.c Normal file
View file

@ -0,0 +1,306 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/common.h>
#include <kernel/slab.h>
#include <kernel/mem.h>
#include <kernel/buddy.h>
#include <kernel/page.h>
#include <kernel/driver.h>
#include <arch/page.h>
#include <lib/string.h>
#include <fs/vfs.h>
#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;
}

View file

@ -4,22 +4,54 @@
#include <kernel/common.h>
#define ET_NONE 0 /* No file type */
#define ET_EXEC 2 /* Executable file */
#define ET_NONE 0
#define ET_REL 1
#define ET_EXEC 2
#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 PT_NULL 0
#define PT_LOAD 1
#define PT_DYNAMIC 2
#define PT_INTERP 3
#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 PF_X (1 << 0)
#define PF_W (1 << 1)
#define PF_R (1 << 2)
#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;
@ -27,31 +59,59 @@ typedef u64 Elf64_Addr;
typedef u64 Elf64_Off;
struct ehdr {
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 */
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;
} PACKED;
struct phdr {
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 */
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;
} PACKED;
int elf_get_headers(const char *file, struct ehdr **out_ehdr, struct phdr **out_phdrs, uaddr_t *entry);

View file

@ -33,7 +33,9 @@ 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;
}
@ -49,7 +51,9 @@ 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)
@ -58,13 +62,18 @@ 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);
}

View file

@ -1,5 +1,5 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/spinlock.h>
#include <kernel/mtx.h>
#include <kernel/types.h>
#include <kernel/slab.h>
#include <lib/list.h>
@ -15,9 +15,7 @@ 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);
@ -25,17 +23,14 @@ 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) {
spinlock_release(&list->lock);
if (!ret)
return NULL;
}
ret->ptr = item;
ret->list = list;
ret->next = NULL;
@ -50,8 +45,6 @@ list_item_t *list_insert(list_t *list, void *item)
}
list->tail = ret;
spinlock_release(&list->lock);
return ret;
}
@ -59,7 +52,6 @@ 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)
@ -67,14 +59,22 @@ 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);

View file

@ -33,7 +33,7 @@ limine/limine:
LDFLAGS="$(HOST_LDFLAGS)" \
LIBS="$(HOST_LIBS)"
$(INITRAMFS): initramfs/
$(INITRAMFS): initramfs/ modules_install
$(Q)echo " CPIO $@"
$(Q)mkdir -p initramfs/dev
$(Q)cd initramfs/ && find . | cpio -oH newc > ../$(INITRAMFS)