diff --git a/Makefile b/Makefile index c96fa3b..f91fbeb 100644 --- a/Makefile +++ b/Makefile @@ -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 '; \ + 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 diff --git a/include/kernel/driver.h b/include/kernel/driver.h index 45ade23..01e68c2 100644 --- a/include/kernel/driver.h +++ b/include/kernel/driver.h @@ -12,6 +12,11 @@ typedef int (*pci_init_t)(struct pci_device *dev); +struct ksym { + const char *name; + void *addr; +}; + struct driver { char name[32]; u32 type; @@ -21,5 +26,9 @@ struct driver { 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); + +extern struct ksym kernel_symtab[]; +extern size_t kernel_symtab_count; #endif /* _DRIVER_H_ */ diff --git a/init/main.c b/init/main.c index 76ad5ff..2961e8c 100644 --- a/init/main.c +++ b/init/main.c @@ -14,6 +14,7 @@ #include #include #include +#include #include diff --git a/kernel/module/Makefile b/kernel/module/Makefile new file mode 100644 index 0000000..9937513 --- /dev/null +++ b/kernel/module/Makefile @@ -0,0 +1,3 @@ +# 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 new file mode 100644 index 0000000..165eab2 --- /dev/null +++ b/kernel/module/modload.c @@ -0,0 +1,278 @@ +/* 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) +{ + while (*pre) + if (*str++ != *pre++) + return 0; + return 1; +} + +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 *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; + + 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(buf, dent->inode->file_size); + + kfree(buf); + return ret; +} diff --git a/kernel/proc/elf.h b/kernel/proc/elf.h index 3d63c8d..9e7e50f 100644 --- a/kernel/proc/elf.h +++ b/kernel/proc/elf.h @@ -4,22 +4,54 @@ #include -#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); diff --git a/scripts/Makefile.limine b/scripts/Makefile.limine index 9dafe0c..0425ddf 100644 --- a/scripts/Makefile.limine +++ b/scripts/Makefile.limine @@ -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)