diff --git a/Makefile b/Makefile index 672235e..5648fe1 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 diff --git a/amd64/amd64_init.c b/amd64/amd64_init.c index 80c7ce7..56478fa 100644 --- a/amd64/amd64_init.c +++ b/amd64/amd64_init.c @@ -61,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/gdt.c b/amd64/gdt.c index 3461ec9..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) @@ -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/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 e9637b4..e2ac5b6 100644 --- a/amd64/kernel/proc.c +++ b/amd64/kernel/proc.c @@ -38,6 +38,7 @@ void enter_userspace_init(struct thread *thread, uaddr_t entry) 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); 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..d59ca73 100644 --- a/mem/alloc.c +++ b/mem/alloc.c @@ -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/scripts/Makefile.limine b/scripts/Makefile.limine index 742ae32..d49b804 100644 --- a/scripts/Makefile.limine +++ b/scripts/Makefile.limine @@ -41,22 +41,26 @@ $(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) .PHONY += debug debug: edk2-ovmf $(IMG) qemu-system-$(ARCH) \ + -no-reboot \ -S -s \ -M q35 \ -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)