vbos/kernel/module/modload.c
Michael Shaw da53fa3871
modload: fix include
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-08-12 21:31:44 -04:00

306 lines
7.3 KiB
C

/* 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 "../thread/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;
}