diff --git a/.gdbinit b/.gdbinit index 8c13264..1a40063 100644 --- a/.gdbinit +++ b/.gdbinit @@ -2,3 +2,4 @@ set pagination off set confirm off file kernel.elf target remote :1234 +add-symbol-file initramfs/bin/utest diff --git a/Makefile b/Makefile index 9deb99a..284adf6 100644 --- a/Makefile +++ b/Makefile @@ -54,6 +54,7 @@ KDIRS := mem/ lib/ drivers/ kernel/ init/ fs/ ifeq ($(ARCH),x86_64) CFLAGS += -Iamd64/include + CFLAGS += -Iamd64 KDIRS += amd64/ endif @@ -61,17 +62,21 @@ KOBJ := $(foreach d,$(KDIRS),$(d)/built-in.o) obj-y := $(KDIRS) -QEMUFLAGS := -m 16G +QEMUFLAGS := -m 4G + +MODULE_CFLAGS := -DMODULE export Q export V export obj-y +export obj-m export CC export LD export QEMUFLAGS export CFLAGS export LDFLAGS export INITRAMFS +export MODULE_CFLAGS PHONY: default default: @@ -94,6 +99,10 @@ $(KERNEL): $(KOBJ) linker.ld $(Q)echo " LD $@" $(Q)$(LD) $(KOBJ) $(LDFLAGS) -T linker.ld -o $@ +.PHONY: modules +modules: + $(Q)$(MAKE) --no-print-directory -f scripts/Makefile.build _modules obj=. + .PHONY: clean clean: $(Q)$(MAKE) --no-print-directory -f scripts/Makefile.build _clean obj=. diff --git a/amd64/amd64_init.c b/amd64/amd64_init.c index 553970c..56478fa 100644 --- a/amd64/amd64_init.c +++ b/amd64/amd64_init.c @@ -49,6 +49,7 @@ void arch_init() gdt_init(); idt_init(); + wrmsr(IA32_EFER, rdmsr(IA32_EFER) | 1); /* SCE */ wrmsr(IA32_STAR, ((u64)GDT_SEGMENT_CODE_RING0 << 48) | ((u64)GDT_SEGMENT_CODE_RING3 << 32)); wrmsr(IA32_LSTAR, (u64)_syscall_enter); wrmsr(IA32_FMASK, 0x3F7FD5); @@ -60,4 +61,6 @@ void arch_ap_startup() struct cpu_local *this_local = &cpu_local_arr[id]; wrmsr(IA32_GS_BASE, (u64)this_local); + + tss_init(id, this_local); } diff --git a/amd64/arch/reg.h b/amd64/arch/reg.h index 75996e4..a6df2ae 100644 --- a/amd64/arch/reg.h +++ b/amd64/arch/reg.h @@ -12,6 +12,6 @@ void cr4_write(u64 cr4); u64 cr4_read(); void lidt(); void lgdt(); -void ltr(); +void ltr(u16 index); #endif /* _REG_H_ */ diff --git a/amd64/descriptor.h b/amd64/descriptor.h index 361727a..587d91c 100644 --- a/amd64/descriptor.h +++ b/amd64/descriptor.h @@ -12,6 +12,7 @@ #define GDT_SEGMENT_DATA_RING0 0x30 #define GDT_SEGMENT_CODE_RING3 0x38 #define GDT_SEGMENT_DATA_RING3 0x40 +#define GDT_SEGMENT_TSS 0x48 #define GATE_INTR 0x8E #define GATE_TRAP 0x8F @@ -21,6 +22,8 @@ #include +struct cpu_local; + struct gdt_desc { uint16_t limit; uint16_t base; @@ -87,5 +90,6 @@ extern struct irq_desc __idt[64]; void gdt_init(); void idt_init(); +void tss_init(int cpu_id, struct cpu_local *local); #endif /* _DESCRIPTOR_H_ */ diff --git a/amd64/exception.c b/amd64/exception.c index 319cf90..30a8c42 100644 --- a/amd64/exception.c +++ b/amd64/exception.c @@ -40,9 +40,8 @@ static const char *exception_names[] = { void exception_handler(u64 no, u64 err) { if (no == 0x0e) { - if (kernel_init_ready) - panic("(Page fault) addr: 0x%16x err: %x\n", cr2_read(), err); + panic("(Page fault) addr: 0x%16x err: %x\n", cr2_read(), err); } - if (!kernel_init_ready) - panic("(Exception) %s(%02xh)\n", exception_names[no], no); + + panic("(Exception) %s(%02xh) err: %x\n", exception_names[no], no, err); } diff --git a/amd64/gdt.c b/amd64/gdt.c index 5566240..638c2b8 100644 --- a/amd64/gdt.c +++ b/amd64/gdt.c @@ -1,9 +1,14 @@ /* SPDX-License-Identifier: GPL-2.0-only */ +#include #include +#include +#include #include "descriptor.h" #include "arch/reg.h" +#define TSS_STACK_SIZE 0x4000 + static size_t gdt_index = 0; void gdt_insert(uint8_t offset, uint8_t access, uint8_t flags) @@ -28,8 +33,8 @@ void gdt_init() gdt_insert(4, GDT_ACCESS_DATA_RING0, 0xC0); gdt_insert(5, GDT_ACCESS_CODE_RING0, 0xA0); gdt_insert(6, GDT_ACCESS_DATA_RING0, 0xC0); - gdt_insert(7, GDT_ACCESS_DATA_RING3, 0xC0); - gdt_insert(8, GDT_ACCESS_CODE_RING3, 0xA0); + gdt_insert(7, GDT_ACCESS_CODE_RING3, 0xC0); + gdt_insert(8, GDT_ACCESS_DATA_RING3, 0xA0); __gdtr.size = (sizeof(__gdt) - 1); __gdtr.offset = (uint64_t)__gdt; @@ -38,3 +43,32 @@ void gdt_init() krdy("GDT initialized\n"); } + +void tss_init(int cpu_id, struct cpu_local *local) +{ + int gdt_idx = 9 + cpu_id * 2; + uint16_t sel = gdt_idx * 8; + + char *tss_stack = kalloc(TSS_STACK_SIZE, ALLOC_KERN); + if (!tss_stack) + panic("failed to alloc tss stack for cpu %d\n", cpu_id); + + struct tss *tss = &local->tss; + + memset(tss, 0, sizeof(*tss)); + tss->rsp0 = (u64)tss_stack + TSS_STACK_SIZE; + + uint64_t addr = (uint64_t)tss; + struct gdt_desc_sys *desc = (struct gdt_desc_sys *)&__gdt[gdt_idx]; + + desc->limit = sizeof(struct tss) - 1; + desc->base = addr & 0xFFFF; + desc->base_2 = (addr >> 16) & 0xFF; + desc->base_3 = (addr >> 24) & 0xFF; + desc->base_4 = addr >> 32; + desc->access = 0x89; + desc->flags = 0x00; + desc->reserved = 0; + + ltr(sel); +} diff --git a/amd64/include/arch/msr.h b/amd64/include/arch/msr.h index ae81a36..bbdbd23 100644 --- a/amd64/include/arch/msr.h +++ b/amd64/include/arch/msr.h @@ -4,6 +4,7 @@ #include +#define IA32_EFER 0xC0000080 #define IA32_STAR 0xC0000081 #define IA32_LSTAR 0xC0000082 #define IA32_FMASK 0xC0000084 diff --git a/amd64/include/arch/proc.h b/amd64/include/arch/proc.h index fa964f3..600ed67 100644 --- a/amd64/include/arch/proc.h +++ b/amd64/include/arch/proc.h @@ -2,6 +2,7 @@ #ifndef _ARCH_PROC_H_ #define _ARCH_PROC_H_ +#include #include #include @@ -11,6 +12,7 @@ struct cpu_local { vaddr_t kernel_rsp; vaddr_t user_rsp; u32 cpu_id; + struct tss tss; } PACKED; struct pt_regs { diff --git a/amd64/kernel/proc.c b/amd64/kernel/proc.c index 4bb4742..e2ac5b6 100644 --- a/amd64/kernel/proc.c +++ b/amd64/kernel/proc.c @@ -36,6 +36,10 @@ void enter_userspace_init(struct thread *thread, uaddr_t entry) tls->cur_thread = thread; + void *kstack = kalloc(PAGE_SIZE, ALLOC_KERN); + tls->kernel_rsp = (vaddr_t)kstack + PAGE_SIZE; + tls->tss.rsp0 = tls->kernel_rsp; + pgd_load(thread->proc->pgd); thread->state = PROC_STATE_RUNNING; diff --git a/fs/vfs/uapi.c b/fs/vfs/uapi.c index f3031d8..e64881d 100644 --- a/fs/vfs/uapi.c +++ b/fs/vfs/uapi.c @@ -84,25 +84,24 @@ static int sys_open(const char *path, int flags) struct file *fd = proc->fdt + i; if (!fd->dirent) { memset(fd, 0, sizeof(struct file)); - fd->dirent = f; - fd->flags = flags; - fd->refcount = 1; fdno = i; + break; } } if (fdno == -1) { size_t osize = proc->fd_array_size; - size_t newsize = MIN(osize * 2, 32); + size_t newsize = MAX(osize * 2, 32); void *new = krealloc(proc->fdt, newsize, ALLOC_KERN); if (!new) { vfs_close(f); return -ENOMEM; } + proc->fd_array_size = newsize; proc->fdt = new; - memset(proc->fdt + osize, 0, osize); + memset(proc->fdt + osize, 0, newsize - osize); struct file *fd = proc->fdt + osize; fd->dirent = f; fd->flags = flags; diff --git a/include/kernel/proc.h b/include/kernel/proc.h index b1d1eed..915b80f 100644 --- a/include/kernel/proc.h +++ b/include/kernel/proc.h @@ -10,8 +10,6 @@ #include -#include "elf.h" - #define PROC_STATE_RUNNING 0 #define PROC_STATE_WAITING 1 #define PROC_STATE_DNS 2 /* do not schedule */ diff --git a/init/main.c b/init/main.c index 4e99b6c..1ae6b16 100644 --- a/init/main.c +++ b/init/main.c @@ -34,7 +34,7 @@ #include #include -#define P_STACK_SIZE 0x8000 +#define P_STACK_SIZE 0x10000 typedef void (*initcall_t)(void); @@ -85,7 +85,10 @@ static void kmain() struct tty *tee = tty_tee_create("broadcast"); tty_tee_add(tee, serial_tty); - tty_tee_add(tee, visual_tty); + + if (visual_tty) + tty_tee_add(tee, visual_tty); + tty_printf = tee; do_initcalls(); diff --git a/kernel/proc/proc.c b/kernel/proc/proc.c index 7747500..18922dc 100644 --- a/kernel/proc/proc.c +++ b/kernel/proc/proc.c @@ -99,7 +99,7 @@ struct proc *proc_create(struct proc *parent) return NULL; } - proc_mmap(ret, TO_PHYS(ustack), r_mt->stack_addr - r_mt->stack_size, r_mt->stack_size, PAGE_RW); + proc_mmap(ret, TO_PHYS(ustack), r_mt->stack_addr - r_mt->stack_size, r_mt->stack_size, PAGE_RW | PAGE_USER); ret->main_thread = r_mt; diff --git a/kernel/tty/tee.c b/kernel/tty/tee.c index 703d890..967a662 100644 --- a/kernel/tty/tee.c +++ b/kernel/tty/tee.c @@ -45,6 +45,9 @@ struct tty *tty_tee_create(const char *name) void tty_tee_add(struct tty *tee, struct tty *child) { + if (!child) + return; + list_t *list = tee->priv; list_insert(list, child); } diff --git a/limine.conf b/limine.conf index de07032..5e8440e 100644 --- a/limine.conf +++ b/limine.conf @@ -6,4 +6,4 @@ timeout: 1 protocol: limine kernel_path: boot():/kernel.elf module_path: boot():/initramfs.cpio - kaslr: yes + kaslr: no diff --git a/mem/alloc.c b/mem/alloc.c index f2ded45..46187ec 100644 --- a/mem/alloc.c +++ b/mem/alloc.c @@ -126,7 +126,7 @@ void *krealloc(void *old, size_t size, u64 flags) rbnode_t *node = rbt_search(&kalloc_tree, (u64)old); if (!node) - return NULL; + return kalloc(size, flags); struct alloc_entry *entry = (void *)node->value; @@ -150,7 +150,8 @@ void kalloc_init() char name[256] = {0}; snprintf(name, 255, "kalloc_%d", slab_item_size); - *slab = slab_create(name, slab_item_size, slab_size, 64); + size_t alignment = slab_item_size >= PAGE_SIZE ? PAGE_SIZE : 64; + *slab = slab_create(name, slab_item_size, slab_size, alignment); } kalloc_map_slab = slab_create("kalloc_map_slab", sizeof(struct alloc_entry), 64 * KB, 0x8); diff --git a/mem/mem.c b/mem/mem.c index aad9cce..bca2704 100644 --- a/mem/mem.c +++ b/mem/mem.c @@ -5,6 +5,9 @@ static struct mem_zone zones[128]; static size_t n_zones; +extern char __kernel_start[]; +extern char __kernel_end[]; + static const char *zone_type_strings[] = {"USABLE", "RESERVED", "ACPI_RECLAIMABLE", @@ -77,6 +80,28 @@ static void mem_init_zones() last_usable = usable; } + + if (kern_addr_res && kern_addr_res->physical_base) { + vaddr_t kbase = (vaddr_t)TO_VIRT(kern_addr_res->physical_base); + vaddr_t kbase_pad = kbase > 0x100000 ? kbase - 0x100000 : 0; + vaddr_t kend = kbase + ((vaddr_t)__kernel_end - (vaddr_t)__kernel_start); + for (size_t j = 0; j < n_zones; j++) { + vaddr_t zbase = zones[j].virt_base; + vaddr_t zend = zbase + zones[j].len; + if (zbase < kend && zend > kbase_pad) { + if (zend <= kend) + zones[j].len = kbase_pad > zbase ? kbase_pad - zbase : 0; + else { + zones[n_zones].virt_base = kend; + zones[n_zones].len = zend - kend; + n_zones++; + zones[j].len = kbase_pad > zbase ? kbase_pad - zbase : 0; + } + break; + } + } + } + #ifdef KDEBUG kdebug("|"); for (int i = 0; i < grid_width - 17; i++) { diff --git a/mem/page.c b/mem/page.c index 07a1253..80827fc 100644 --- a/mem/page.c +++ b/mem/page.c @@ -105,7 +105,7 @@ static void map_hhdm_zones(pgd_t *pgd_table) } hhdm_end = top_paddr; - device_base = ALIGN_UP(hhdm_end + PAGE_SIZE, PAGE_SIZE); + device_base = ALIGN_UP((u64)TO_VIRT(hhdm_end) + PAGE_SIZE, PAGE_SIZE); for (paddr_t p = 0; p < top_paddr; p += 0x200000) { vaddr_t v = (vaddr_t)TO_VIRT(p); diff --git a/scripts/Makefile.build b/scripts/Makefile.build index 4d1c76b..3a716e6 100644 --- a/scripts/Makefile.build +++ b/scripts/Makefile.build @@ -11,6 +11,7 @@ export obj-y := +obj-m := subdir-y := include $(obj)/Makefile @@ -20,7 +21,14 @@ real-objs-y := subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y))) real-objs-y := $(addprefix $(obj)/, $(filter-out %/, $(obj-y))) subdir-objs-y := $(foreach d,$(subdir-y),$(obj)/$(d)/built-in.o) -deps := $(real-objs-y:.o=.d) + +subdir-m := $(patsubst %/,%,$(filter %/, $(obj-m))) +mod-names := $(basename $(filter-out %/, $(obj-m))) +mod-targets := $(addprefix $(obj)/, $(addsuffix .ko, $(mod-names))) +mod-objs := $(foreach m,$(mod-names), \ + $(addprefix $(obj)/, $(or $($(m)-objs),$($(m)-y),$(m).o))) + +deps := $(real-objs-y:.o=.d) $(mod-objs:.o=.d) ifeq ($(obj),.) _all: $(subdir-objs-y) @@ -40,15 +48,36 @@ $(obj)/built-in.o: $(real-objs-y) $(subdir-objs-y) $(Q)echo " LD $@" $(Q)$(LD) -r -o $@ $(real-objs-y) $(foreach d,$(subdir-y),$(obj)/$(d)/built-in.o) +# --- Module support --- + +define mod_rule +$(obj)/$(1).ko: $(addprefix $(obj)/, $(or $($(1)-objs),$($(1)-y),$(1).o)) + $$(Q)echo " MODLD $$@" + $$(Q)$$(LD) -r -o $$@ $$^ +endef + +$(foreach m,$(mod-names),$(eval $(call mod_rule,$(m)))) + +define mod_cflags +$(addprefix $(obj)/, $(or $($(1)-objs),$($(1)-y),$(1).o)): CFLAGS += $(MODULE_CFLAGS) +endef + +$(foreach m,$(mod-names),$(eval $(call mod_cflags,$(m)))) + +.PHONY: _modules +_modules: $(mod-targets) $(subdir-m) .PHONY: _clean -_clean: $(subdir-y) +_clean: $(subdir-y) $(subdir-m) $(Q)echo " CLEAN $(obj)" - @rm -rf $(obj)/built-in.o $(real-objs-y) $(deps) - + @rm -rf $(obj)/built-in.o $(real-objs-y) $(deps) $(mod-objs) $(mod-targets) + $(subdir-y): FORCE $(Q)$(MAKE) --no-print-directory -f scripts/Makefile.build $(MAKECMDGOALS) obj=$(obj)/$@ +$(subdir-m): FORCE + $(Q)$(MAKE) --no-print-directory -f scripts/Makefile.build $(if $(filter _clean,$(MAKECMDGOALS)),_clean,_modules) obj=$(obj)/$@ + $(obj)/%/built-in.o: FORCE $(Q)$(MAKE) --no-print-directory -f scripts/Makefile.build obj=$(patsubst %/,%,$(dir $@)) diff --git a/scripts/Makefile.limine b/scripts/Makefile.limine index 742ae32..d43db93 100644 --- a/scripts/Makefile.limine +++ b/scripts/Makefile.limine @@ -41,11 +41,13 @@ $(INITRAMFS): initramfs/ .PHONY += run run: edk2-ovmf $(INITRAMFS) $(IMG) qemu-system-$(ARCH) \ + -no-reboot \ -M q35 \ -drive if=pflash,unit=0,format=raw,file=edk2-ovmf/ovmf-code-$(ARCH).fd,readonly=on \ -accel kvm \ -hda $(IMG) \ -hdb test_drive.hdd \ + -nographic \ -serial mon:stdio \ $(QEMUFLAGS) @@ -57,6 +59,7 @@ debug: edk2-ovmf $(IMG) -drive if=pflash,unit=0,format=raw,file=edk2-ovmf/ovmf-code-$(ARCH).fd,readonly=on \ -hda $(IMG) \ -hdb test_drive.hdd \ + -nographic \ -serial mon:stdio \ $(QEMUFLAGS) diff --git a/usr.bin/utest/utest.c b/usr.bin/utest/utest.c index 61a134b..78e4a28 100644 --- a/usr.bin/utest/utest.c +++ b/usr.bin/utest/utest.c @@ -21,14 +21,15 @@ size_t strlen(const char *c) void _start() { - int fd = open("/dev/ttyS0", 0); + int fd = open("/dev/broadcast", 0); if (fd < 0) - goto loop; + while (1) + ; char msg[] = "Hello, userspace!\n"; write(fd, msg, strlen(msg)); + close(fd); -loop: while (1) - continue; + ; }