diff --git a/.gdbinit b/.gdbinit index 1a40063..8c13264 100644 --- a/.gdbinit +++ b/.gdbinit @@ -2,4 +2,3 @@ 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 284adf6..9deb99a 100644 --- a/Makefile +++ b/Makefile @@ -54,7 +54,6 @@ KDIRS := mem/ lib/ drivers/ kernel/ init/ fs/ ifeq ($(ARCH),x86_64) CFLAGS += -Iamd64/include - CFLAGS += -Iamd64 KDIRS += amd64/ endif @@ -62,21 +61,17 @@ KOBJ := $(foreach d,$(KDIRS),$(d)/built-in.o) obj-y := $(KDIRS) -QEMUFLAGS := -m 4G - -MODULE_CFLAGS := -DMODULE +QEMUFLAGS := -m 16G 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: @@ -99,10 +94,6 @@ $(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 56478fa..553970c 100644 --- a/amd64/amd64_init.c +++ b/amd64/amd64_init.c @@ -49,7 +49,6 @@ 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); @@ -61,6 +60,4 @@ 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 a6df2ae..75996e4 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(u16 index); +void ltr(); #endif /* _REG_H_ */ diff --git a/amd64/descriptor.h b/amd64/descriptor.h index 587d91c..361727a 100644 --- a/amd64/descriptor.h +++ b/amd64/descriptor.h @@ -12,7 +12,6 @@ #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 @@ -22,8 +21,6 @@ #include -struct cpu_local; - struct gdt_desc { uint16_t limit; uint16_t base; @@ -90,6 +87,5 @@ 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 30a8c42..319cf90 100644 --- a/amd64/exception.c +++ b/amd64/exception.c @@ -40,8 +40,9 @@ static const char *exception_names[] = { void exception_handler(u64 no, u64 err) { if (no == 0x0e) { - panic("(Page fault) addr: 0x%16x err: %x\n", cr2_read(), err); + if (kernel_init_ready) + panic("(Page fault) addr: 0x%16x err: %x\n", cr2_read(), err); } - - panic("(Exception) %s(%02xh) err: %x\n", exception_names[no], no, err); + if (!kernel_init_ready) + panic("(Exception) %s(%02xh)\n", exception_names[no], no); } diff --git a/amd64/gdt.c b/amd64/gdt.c index 638c2b8..5566240 100644 --- a/amd64/gdt.c +++ b/amd64/gdt.c @@ -1,14 +1,9 @@ /* 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) @@ -33,8 +28,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_CODE_RING3, 0xC0); - gdt_insert(8, GDT_ACCESS_DATA_RING3, 0xA0); + gdt_insert(7, GDT_ACCESS_DATA_RING3, 0xC0); + gdt_insert(8, GDT_ACCESS_CODE_RING3, 0xA0); __gdtr.size = (sizeof(__gdt) - 1); __gdtr.offset = (uint64_t)__gdt; @@ -43,32 +38,3 @@ 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 bbdbd23..ae81a36 100644 --- a/amd64/include/arch/msr.h +++ b/amd64/include/arch/msr.h @@ -4,7 +4,6 @@ #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 600ed67..fa964f3 100644 --- a/amd64/include/arch/proc.h +++ b/amd64/include/arch/proc.h @@ -2,7 +2,6 @@ #ifndef _ARCH_PROC_H_ #define _ARCH_PROC_H_ -#include #include #include @@ -12,7 +11,6 @@ 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 e2ac5b6..4bb4742 100644 --- a/amd64/kernel/proc.c +++ b/amd64/kernel/proc.c @@ -36,10 +36,6 @@ 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 e64881d..f3031d8 100644 --- a/fs/vfs/uapi.c +++ b/fs/vfs/uapi.c @@ -84,24 +84,25 @@ 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 = MAX(osize * 2, 32); + size_t newsize = MIN(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, newsize - osize); + memset(proc->fdt + osize, 0, 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 915b80f..b1d1eed 100644 --- a/include/kernel/proc.h +++ b/include/kernel/proc.h @@ -10,6 +10,8 @@ #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 1ae6b16..4e99b6c 100644 --- a/init/main.c +++ b/init/main.c @@ -34,7 +34,7 @@ #include #include -#define P_STACK_SIZE 0x10000 +#define P_STACK_SIZE 0x8000 typedef void (*initcall_t)(void); @@ -85,10 +85,7 @@ static void kmain() struct tty *tee = tty_tee_create("broadcast"); tty_tee_add(tee, serial_tty); - - if (visual_tty) - tty_tee_add(tee, 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 18922dc..7747500 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 | PAGE_USER); + proc_mmap(ret, TO_PHYS(ustack), r_mt->stack_addr - r_mt->stack_size, r_mt->stack_size, PAGE_RW); ret->main_thread = r_mt; diff --git a/kernel/tty/tee.c b/kernel/tty/tee.c index 967a662..703d890 100644 --- a/kernel/tty/tee.c +++ b/kernel/tty/tee.c @@ -45,9 +45,6 @@ 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 5e8440e..de07032 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: no + kaslr: yes diff --git a/mem/alloc.c b/mem/alloc.c index 46187ec..f2ded45 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 kalloc(size, flags); + return NULL; struct alloc_entry *entry = (void *)node->value; @@ -150,8 +150,7 @@ void kalloc_init() char name[256] = {0}; snprintf(name, 255, "kalloc_%d", slab_item_size); - size_t alignment = slab_item_size >= PAGE_SIZE ? PAGE_SIZE : 64; - *slab = slab_create(name, slab_item_size, slab_size, alignment); + *slab = slab_create(name, slab_item_size, slab_size, 64); } 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 bca2704..aad9cce 100644 --- a/mem/mem.c +++ b/mem/mem.c @@ -5,9 +5,6 @@ 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", @@ -80,28 +77,6 @@ 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 80827fc..07a1253 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((u64)TO_VIRT(hhdm_end) + PAGE_SIZE, PAGE_SIZE); + device_base = ALIGN_UP(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 3a716e6..4d1c76b 100644 --- a/scripts/Makefile.build +++ b/scripts/Makefile.build @@ -11,7 +11,6 @@ export obj-y := -obj-m := subdir-y := include $(obj)/Makefile @@ -21,14 +20,7 @@ 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) - -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) +deps := $(real-objs-y:.o=.d) ifeq ($(obj),.) _all: $(subdir-objs-y) @@ -48,36 +40,15 @@ $(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) $(subdir-m) +_clean: $(subdir-y) $(Q)echo " CLEAN $(obj)" - @rm -rf $(obj)/built-in.o $(real-objs-y) $(deps) $(mod-objs) $(mod-targets) - + @rm -rf $(obj)/built-in.o $(real-objs-y) $(deps) + $(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 d43db93..742ae32 100644 --- a/scripts/Makefile.limine +++ b/scripts/Makefile.limine @@ -41,13 +41,11 @@ $(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) @@ -59,7 +57,6 @@ 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 78e4a28..61a134b 100644 --- a/usr.bin/utest/utest.c +++ b/usr.bin/utest/utest.c @@ -21,15 +21,14 @@ size_t strlen(const char *c) void _start() { - int fd = open("/dev/broadcast", 0); + int fd = open("/dev/ttyS0", 0); if (fd < 0) - while (1) - ; + goto loop; char msg[] = "Hello, userspace!\n"; write(fd, msg, strlen(msg)); - close(fd); +loop: while (1) - ; + continue; }