diff --git a/Makefile b/Makefile index e49598b..e2e315c 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,15 @@ .PHONY: clean prepare cleanall iso base kernel user test +ARCH ?= x86_64 + kernel: - make -C kernel ROOT=$(PWD) all + make -C kernel ARCH=$(ARCH) ROOT=$(PWD) all user: - make -C user ROOT=$(PWD) all + make -C user ARCH=$(ARCH) ROOT=$(PWD) all + +ulib: + make -C ulib ARCH=$(ARCH) ROOT=$(PWD) all prepare: if [ ! -d limine ]; then \ @@ -18,8 +23,9 @@ cleanall: rm -rf limine clean: - make -C kernel ROOT=$(PWD) clean - make -C user ROOT=$(PWD) clean + make -C kernel ARCH=$(ARCH) ROOT=$(PWD) clean + make -C user ARCH=$(ARCH) ROOT=$(PWD) clean + make -C ulib ARCH=$(ARCH) ROOT=$(PWD) clean rm -f mop2.iso base.img base: diff --git a/kernel/Makefile b/kernel/Makefile index b7173bc..7bedae7 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -2,14 +2,14 @@ include $(ROOT)/mk/grabsrc.mk .PHONY: all clean -ARCH ?= x86_64 PUTCHAR_ ?= fb CFLAGS := -ffreestanding -Wall -Wextra -g -fcommon -nostdinc CFLAGS += -I. \ -I$(ROOT)/limine \ - -I./std/include \ + -I$(ROOT)/std/include \ + -I./std \ -I./flanterm/src \ -DPRINTF_INCLUDE_CONFIG_H=1 \ -DLFS_NO_ASSERT \ diff --git a/kernel/arch/x86_64/x86_64.mk b/kernel/arch/x86_64/x86_64.mk index bec49ac..7eb27bd 100644 --- a/kernel/arch/x86_64/x86_64.mk +++ b/kernel/arch/x86_64/x86_64.mk @@ -1,5 +1,4 @@ -CC := x86_64-elf-gcc -LD := x86_64-elf-ld +include $(ROOT)/mk/arch/toolchain-x86_64.mk CFLAGS += -m64 \ -fPIE \ diff --git a/kernel/hal/x86_64/cpu.c b/kernel/hal/x86_64/cpu.c index 29b1425..33a9e65 100644 --- a/kernel/hal/x86_64/cpu.c +++ b/kernel/hal/x86_64/cpu.c @@ -6,8 +6,6 @@ #include "hal/hal.h" #include "bootinfo/bootinfo.h" -LocalCpuData HAL_CPUS[HAL_CPUS_MAX]; - uint64_t hal_cpu_rdmsr(uint32_t id) { uint32_t lo, hi; asm volatile("rdmsr" : "=a"(lo), "=d"(hi) : "c"(id)); @@ -21,8 +19,3 @@ uint64_t hal_cpu_wrmsr(uint32_t id, uint64_t val) { return val; } -void hal_cpu_init(uint32_t cpu) { - void *addr = pmm_alloc(16) + (16 * HAL_PAGE_SIZE); - HAL_CPUS[cpu].syscall_kstack = (uint64_t *)VIRT(addr); - HAL_CPUS[cpu].kcr3 = hal_vmm_current_cr3(); -} diff --git a/kernel/hal/x86_64/cpu.h b/kernel/hal/x86_64/cpu.h index efb7504..ee7cf74 100644 --- a/kernel/hal/x86_64/cpu.h +++ b/kernel/hal/x86_64/cpu.h @@ -13,24 +13,13 @@ #define HAL_CPU_STAR 0xC0000081 #define HAL_CPU_LSTAR 0xC0000082 #define HAL_CPU_CSTAR 0xC0000083 -#define HAL_CPU_SFMASK 0xC0000084 +#define HAL_CPU_FMASK 0xC0000084 #define HAL_CPU_FSBASE 0xC0000100 #define HAL_CPU_GSBASE 0xC0000101 #define HAL_CPU_KGSBASE 0xC0000102 -typedef struct { - uint64_t *syscall_kstack; - uint64_t *syscall_ustack; - PgTable *kcr3; - PgTable *pcr3; - IntrStackFrame *frame; -} PACKED LocalCpuData; - uint64_t hal_cpu_rdmsr(uint32_t id); uint64_t hal_cpu_wrmsr(uint32_t id, uint64_t val); -void hal_cpu_init(uint32_t cpu); - -extern LocalCpuData HAL_CPUS[HAL_CPUS_MAX]; #endif // HAL_CPU_H_ diff --git a/kernel/hal/x86_64/hal.c b/kernel/hal/x86_64/hal.c index 45c028a..76503a0 100644 --- a/kernel/hal/x86_64/hal.c +++ b/kernel/hal/x86_64/hal.c @@ -9,7 +9,6 @@ #include "pic.h" #include "apic.h" #include "pit.h" -#include "syscall.h" void hal_init(void) { if (!serial_init()) { @@ -33,7 +32,6 @@ void hal_init_withmalloc(void) { pit_init(); ioapic_setentry(IOAPIC, acpi_remapirq(0x00), INTR_TIMER); hal_intr_disable(); - hal_syscall_init(); } void hal_wait(uint32_t ms) { diff --git a/kernel/hal/x86_64/intr.c b/kernel/hal/x86_64/intr.c index f60e45e..29df735 100644 --- a/kernel/hal/x86_64/intr.c +++ b/kernel/hal/x86_64/intr.c @@ -11,6 +11,8 @@ #include "apic.h" #include "pit.h" #include "proc/proc.h" +#include "syscall/syscall.h" +#include "errors.h" void hal_intr_disable(void) { asm volatile("cli"); @@ -122,6 +124,9 @@ void intr_init(void) { MKINTR(45, 0); MKINTR(46, 0); MKINTR(47, 0); + + extern void intr_vec128(void); + idt_setentry(0x80, (uint64_t)&intr_vec128, 0, 0xEE); idt_init(); } @@ -149,14 +154,33 @@ void intr_dumpframe(IntrStackFrame *frame) { ); } +void hal_syscalldispatch(IntrStackFrame *frame) { + uint64_t sysnum = frame->regs.rax; + if (sysnum < SYSCALLS_MAX) { + SyscallFn fn = SYSCALL_TABLE[sysnum]; + if (fn == NULL) { + frame->regs.rax = E_BADSYSCALL; + return; + } + int32_t ret = fn(frame->regs.rdi, frame->regs.rsi, frame->regs.rdx, + frame->regs.r10, frame->regs.r8, frame->regs.r9); + + if (sysnum == SYS_QUITPROC) { + proc_sched((void *)frame); + } + frame->regs.rax = *(uint64_t *)&ret; + } +} + void intr_handleintr(IntrStackFrame *frame) { if (frame->trapnum <= 31) { + kprintf("ERROR %s, 0x%lX\n", exceptions[frame->trapnum], frame->errnum); + intr_dumpframe(frame); if (frame->trapnum == 14 && frame->errnum & 0x4) { + kprintf("killed pid %ld %s\n", PROCS.current->pid, PROCS.current->name); proc_killself(); proc_sched((void *)frame); } - kprintf("ERROR %s, 0x%lX\n", exceptions[frame->trapnum], frame->errnum); - intr_dumpframe(frame); hal_hang(); } else if (frame->trapnum >= 32 && frame->trapnum <= 47) { if (frame->trapnum == INTR_TIMER) { @@ -167,6 +191,8 @@ void intr_handleintr(IntrStackFrame *frame) { lapic_write(LAPIC_EOI, 0x00); proc_sched((void *)frame); + } else if (frame->trapnum == 0x80) { + hal_syscalldispatch(frame); } } diff --git a/kernel/hal/x86_64/intr0.S b/kernel/hal/x86_64/intr0.S index 2b98fcc..bcada08 100644 --- a/kernel/hal/x86_64/intr0.S +++ b/kernel/hal/x86_64/intr0.S @@ -50,6 +50,7 @@ .global intr_vec45 .global intr_vec46 .global intr_vec47 +.global intr_vec128 .macro _vecintr_errorcode_present_save num pushq $\num @@ -215,3 +216,6 @@ intr_vec46: intr_vec47: _vecintr_plain_save 47 _vecintr_bodygen +intr_vec128: + _vecintr_plain_save 128 + _vecintr_bodygen diff --git a/kernel/hal/x86_64/syscall.c b/kernel/hal/x86_64/syscall.c deleted file mode 100644 index 7a18f62..0000000 --- a/kernel/hal/x86_64/syscall.c +++ /dev/null @@ -1,43 +0,0 @@ -#include -#include "syscall.h" -#include "cpu.h" -#include "intr.h" -#include "syscall/syscall.h" -#include "proc/proc.h" -#include "kprintf.h" -#include "errors.h" - -extern void hal_enable_syscallentry(void); -extern void hal_syscallentry(void); - -void hal_syscalldispatch(IntrStackFrame *frame) { - uint64_t sysnum = frame->regs.rax; - if (sysnum < SYSCALLS_MAX) { - SyscallFn fn = SYSCALL_TABLE[sysnum]; - if (fn == NULL) { - frame->regs.rax = E_BADSYSCALL; - return; - } - int32_t ret = fn(frame->regs.rdi, frame->regs.rsi, frame->regs.rdx, - frame->regs.r10, frame->regs.r8, frame->regs.r9); - - if (sysnum == SYS_QUITPROC) { - proc_sched((void *)frame); - } - frame->regs.rax = *(uint64_t *)&ret; - } -} - -void hal_syscall_init(void) { - hal_cpu_init(0); - LocalCpuData *lcd = &HAL_CPUS[0]; - - hal_cpu_wrmsr(HAL_CPU_EFER, hal_cpu_rdmsr(HAL_CPU_EFER) | 0x1); - hal_enable_syscallentry(); - - hal_cpu_wrmsr(HAL_CPU_GSBASE, (uint64_t)lcd); - hal_cpu_wrmsr(HAL_CPU_KGSBASE, (uint64_t)lcd); - hal_cpu_wrmsr(HAL_CPU_SFMASK, (uint64_t)0); - - hal_cpu_wrmsr(HAL_CPU_LSTAR, (uint64_t)&hal_syscallentry); -} diff --git a/kernel/hal/x86_64/syscall.h b/kernel/hal/x86_64/syscall.h deleted file mode 100644 index d408203..0000000 --- a/kernel/hal/x86_64/syscall.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef HAL_SYSCALL_H_ -#define HAL_SYSCALL_H_ - -void hal_syscall_init(void); - -#endif // HAL_SYSCALL_H_ diff --git a/kernel/hal/x86_64/syscall0.S b/kernel/hal/x86_64/syscall0.S deleted file mode 100644 index 8c44aad..0000000 --- a/kernel/hal/x86_64/syscall0.S +++ /dev/null @@ -1,48 +0,0 @@ -#include "regs.S" - -.extern hal_syscalldispatch - -.global hal_enable_syscallentry -hal_enable_syscallentry: - movq $0xC0000080, %rcx - rdmsr - or $0x1, %eax - wrmsr - movq $0xC0000081, %rcx - rdmsr - movl $0x180008, %edx - wrmsr - ret - -.global hal_syscallentry -hal_syscallentry: - cli - swapgs - - movq %rsp, %gs:0x8 - movq %gs:0x0, %rsp - - pushq $0x23 - pushq %gs:0x8 - - swapgs - - pushq %r11 - pushq $0x1b - pushq %rcx - - _push_regs - - mov %rsp, %rdi - mov $0x0, %rbp - - call hal_syscalldispatch - cli - - _pop_regs - - popq %rcx - add $0x8, %rsp - popq %r11 - popq %rsp - sysret diff --git a/kernel/proc/proc.c b/kernel/proc/proc.c index bf2814f..c0081ab 100644 --- a/kernel/proc/proc.c +++ b/kernel/proc/proc.c @@ -155,7 +155,7 @@ Proc *proc_spawnuser(char *mountpoint, char *path) { ElfAuxval aux = proc_load_elf_segs(proc, data); proc->platformdata.trapframe.ss = 0x20 | 0x3; - proc->platformdata.trapframe.rsp = (uint64_t)VIRT(sp); + proc->platformdata.trapframe.rsp = (uint64_t)sp; proc->platformdata.trapframe.rflags = 0x202; proc->platformdata.trapframe.cs = 0x18 | 0x3; proc->platformdata.trapframe.rip = aux.entry; @@ -231,13 +231,10 @@ void proc_sched(void *cpustate) { IntrStackFrame *frame = cpustate; PROCS.current->platformdata.trapframe = *frame; - PROCS.current->platformdata.fsbase = hal_cpu_rdmsr(HAL_CPU_FSBASE); PROCS.current = proc_nextready(); PROCS.current->state = PROC_RUNNING; - hal_cpu_wrmsr(HAL_CPU_FSBASE, PROCS.current->platformdata.fsbase); - HAL_CPUS[0].syscall_kstack = VIRT(PROCS.current->platformdata.kstack); hal_switchproc(&PROCS.current->platformdata.trapframe, (void *)PROCS.current->platformdata.cr3); } @@ -282,8 +279,8 @@ void proc_init(void) { proc_register(idle); PROCS.current = idle; - proc_register(proc_spawnkern(&proc_status, "status")); - proc_register(proc_spawnuser("base", "/bin/hello")); + /* proc_register(proc_spawnkern(&proc_status, "status")); */ + proc_register(proc_spawnuser("base", "/bin/init")); hal_switchproc(&PROCS.current->platformdata.trapframe, (void *)PROCS.current->platformdata.cr3); } diff --git a/kernel/proc/proc.h b/kernel/proc/proc.h index 4b54d19..1245896 100644 --- a/kernel/proc/proc.h +++ b/kernel/proc/proc.h @@ -14,7 +14,6 @@ #define PROC_MAX 0x100 // max amount of processes typedef struct { - uint64_t fsbase; IntrStackFrame trapframe; uint8_t *kstack; PgTable *cr3; diff --git a/kernel/std/include/stdlib.h b/kernel/std/stdlib.h similarity index 100% rename from kernel/std/include/stdlib.h rename to kernel/std/stdlib.h diff --git a/kernel/std/include/string.h b/kernel/std/string.h similarity index 100% rename from kernel/std/include/string.h rename to kernel/std/string.h diff --git a/kernel/syscall/syscall.c b/kernel/syscall/syscall.c index 5501298..cc01f2f 100644 --- a/kernel/syscall/syscall.c +++ b/kernel/syscall/syscall.c @@ -5,14 +5,9 @@ #include "kprintf.h" #include "proc/proc.h" -int32_t SYSCALL2(sys_debugprint, string1, len) { - char *buf = dlmalloc(len); - if (buf == NULL) { - return E_NOMEMORY; - } - ksnprintf(buf, len, "%s", string1); - kprintf("%s\n", buf); - dlfree(buf); +int32_t SYSCALL1(sys_debugprint, string) { + char *p = (char *)string; + kprintf("%s\n", p); return E_OK; } diff --git a/mk/arch/toolchain-x86_64.mk b/mk/arch/toolchain-x86_64.mk new file mode 100644 index 0000000..91e60ca --- /dev/null +++ b/mk/arch/toolchain-x86_64.mk @@ -0,0 +1,3 @@ +CC := x86_64-elf-gcc +LD := x86_64-elf-ld +AR := x86_64-elf-ar diff --git a/mk/user/x86_64.mk b/mk/user/x86_64.mk new file mode 100644 index 0000000..8379193 --- /dev/null +++ b/mk/user/x86_64.mk @@ -0,0 +1,13 @@ +CFLAGS += -m64 \ + -fPIE \ + -mno-80387 \ + -mno-mmx \ + -mno-sse \ + -nostartfiles \ + -nostdlib \ + -mno-sse2 \ + -mno-red-zone \ + -fno-stack-protector \ + -fno-stack-check \ + -fno-builtin \ + -Os \ diff --git a/scripts/devel.sh b/scripts/devel.sh index 35a5138..20f0006 100755 --- a/scripts/devel.sh +++ b/scripts/devel.sh @@ -1,3 +1,3 @@ #!/bin/sh -make -B kernel && make -B user && make base && make iso +make -B kernel && make -B ulib && make -B user && make base && make iso diff --git a/kernel/std/LICENSE b/std/LICENSE similarity index 100% rename from kernel/std/LICENSE rename to std/LICENSE diff --git a/kernel/std/README b/std/README similarity index 100% rename from kernel/std/README rename to std/README diff --git a/kernel/std/include/float.h b/std/include/float.h similarity index 100% rename from kernel/std/include/float.h rename to std/include/float.h diff --git a/kernel/std/include/inttypes.h b/std/include/inttypes.h similarity index 100% rename from kernel/std/include/inttypes.h rename to std/include/inttypes.h diff --git a/kernel/std/include/iso646.h b/std/include/iso646.h similarity index 100% rename from kernel/std/include/iso646.h rename to std/include/iso646.h diff --git a/kernel/std/include/limits.h b/std/include/limits.h similarity index 100% rename from kernel/std/include/limits.h rename to std/include/limits.h diff --git a/kernel/std/include/stdalign.h b/std/include/stdalign.h similarity index 100% rename from kernel/std/include/stdalign.h rename to std/include/stdalign.h diff --git a/kernel/std/include/stdarg.h b/std/include/stdarg.h similarity index 100% rename from kernel/std/include/stdarg.h rename to std/include/stdarg.h diff --git a/kernel/std/include/stdatomic.h b/std/include/stdatomic.h similarity index 96% rename from kernel/std/include/stdatomic.h rename to std/include/stdatomic.h index 2ee79a8..3170f38 100644 --- a/kernel/std/include/stdatomic.h +++ b/std/include/stdatomic.h @@ -20,6 +20,6 @@ #define atomic_compare_exchange_strong(p, old, new) \ __atomic_compare_exchange_n(p, old, new, false, memory_order_relaxed, memory_order_relaxed) -#define atomic_bool volatile bool +#define atomic_bool _Atomic(bool) #endif diff --git a/kernel/std/include/stdbool.h b/std/include/stdbool.h similarity index 100% rename from kernel/std/include/stdbool.h rename to std/include/stdbool.h diff --git a/kernel/std/include/stddef.h b/std/include/stddef.h similarity index 100% rename from kernel/std/include/stddef.h rename to std/include/stddef.h diff --git a/kernel/std/include/stdint.h b/std/include/stdint.h similarity index 100% rename from kernel/std/include/stdint.h rename to std/include/stdint.h diff --git a/kernel/std/include/stdnoreturn.h b/std/include/stdnoreturn.h similarity index 100% rename from kernel/std/include/stdnoreturn.h rename to std/include/stdnoreturn.h diff --git a/ulib/.gitignore b/ulib/.gitignore new file mode 100644 index 0000000..5761abc --- /dev/null +++ b/ulib/.gitignore @@ -0,0 +1 @@ +*.o diff --git a/ulib/Makefile b/ulib/Makefile new file mode 100644 index 0000000..b8c7573 --- /dev/null +++ b/ulib/Makefile @@ -0,0 +1,33 @@ +include $(ROOT)/mk/grabsrc.mk +include $(ROOT)/mk/arch/toolchain-$(ARCH).mk +include $(ROOT)/mk/user/$(ARCH).mk + +.PHONY: all clean + +SRCFILES := $(call GRABSRC, \ + . \ + syscall \ + string \ + system \ + ) + +CFLAGS += -isystem $(ROOT)/ulib -isystem $(ROOT)/std/include + +ASFILES := $(call GET_ASFILES, $(SRCFILES)) +CFILES := $(call GET_CFILES, $(SRCFILES)) +OBJ := $(call GET_OBJ, $(SRCFILES)) + +%.o: %.S + $(CC) $(CFLAGS) -c $< -o $@ + +%.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ + +libulib.a: $(OBJ) + $(AR) rcs libulib.a $(OBJ) + +all: libulib.a + +clean: + rm -f $(OBJ) + rm -f libulib.a diff --git a/ulib/_premain.c b/ulib/_premain.c new file mode 100644 index 0000000..4420903 --- /dev/null +++ b/ulib/_premain.c @@ -0,0 +1,22 @@ +#include +#include + +extern void main(void); + +extern uint8_t _bss_start[]; +extern uint8_t _bss_end[]; + +void bss_clear(void) { + uint8_t *p = _bss_start; + while (p != _bss_end) { + *p = 0; + p++; + } +} + +// ulib initialization goes here +void _premain(void) { + bss_clear(); + main(); +} + diff --git a/ulib/_start.S b/ulib/_start.S new file mode 100644 index 0000000..c09c7eb --- /dev/null +++ b/ulib/_start.S @@ -0,0 +1,10 @@ +#include + +.extern _premain + +.global _start +_start: + call _premain + + mov $SYS_QUITPROC, %rax + int $0x80 diff --git a/ulib/libulib.a b/ulib/libulib.a new file mode 100644 index 0000000..365d31a Binary files /dev/null and b/ulib/libulib.a differ diff --git a/ulib/string/string.c b/ulib/string/string.c new file mode 100644 index 0000000..84b5f3c --- /dev/null +++ b/ulib/string/string.c @@ -0,0 +1,11 @@ +#include +#include + +size_t string_len(const char *s) { + size_t l = 0; + while (*s != '\0') { + l++; + s++; + } + return l; +} diff --git a/ulib/string/string.h b/ulib/string/string.h new file mode 100644 index 0000000..f2cbcff --- /dev/null +++ b/ulib/string/string.h @@ -0,0 +1,8 @@ +#ifndef ULIB_STRING_STRING_H_ +#define ULIB_STRING_STRING_H_ + +#include + +size_t string_len(const char *s); + +#endif // ULIB_STRING_STRING_H_ diff --git a/ulib/syscall/syscall.c b/ulib/syscall/syscall.c new file mode 100644 index 0000000..cd75d4e --- /dev/null +++ b/ulib/syscall/syscall.c @@ -0,0 +1,27 @@ +#include + +uint64_t syscall(uint64_t num, uint64_t arg1, uint64_t arg2, + uint64_t arg3, uint64_t arg4, uint64_t arg5, uint64_t arg6) { + uint64_t ret = 0; + asm volatile ( + "mov %[SYSNUM], %%rax\n" + "mov %[ARG1], %%rdi\n" + "mov %[ARG2], %%rsi\n" + "mov %[ARG3], %%rdx\n" + "mov %[ARG4], %%r10\n" + "mov %[ARG5], %%r8\n" + "mov %[ARG6], %%r9\n" + "int $0x80\n" + "mov %%rax, %[RESULT]\n" + : [RESULT]"=r"(ret) + : [SYSNUM]"r"(num), + [ARG1]"r"(arg1), + [ARG2]"r"(arg2), + [ARG3]"r"(arg3), + [ARG4]"r"(arg4), + [ARG5]"r"(arg5), + [ARG6]"r"(arg6) + : "%rax", "%rdi", "%rsi", "%rdx", "%r10", "%r8", "%r9", "memory" + ); + return ret; +} diff --git a/ulib/syscall/syscall.h b/ulib/syscall/syscall.h new file mode 100644 index 0000000..1c4ccdd --- /dev/null +++ b/ulib/syscall/syscall.h @@ -0,0 +1,14 @@ +#ifndef ULIB_SYSCALL_SYSCALL_H_ +#define ULIB_SYSCALL_SYSCALL_H_ + +#define SYS_DEBUGPRINT 1 +#define SYS_QUITPROC 2 + +#if !defined(__ASSEMBLER__) + +uint64_t syscall(uint64_t num, uint64_t arg1, uint64_t arg2, + uint64_t arg3, uint64_t arg4, uint64_t arg5, uint64_t arg6); + +#endif // ! __ASSEMBLER__ + +#endif // ULIB_SYSCALL_SYSCALL_H_ diff --git a/ulib/system/system.c b/ulib/system/system.c new file mode 100644 index 0000000..7d44bcf --- /dev/null +++ b/ulib/system/system.c @@ -0,0 +1,9 @@ +#include +#include +#include + + +void sys_debugprint(const char *string) { + syscall(SYS_DEBUGPRINT, (uint64_t)string, 0, 0, 0, 0, 0); +} + diff --git a/ulib/system/system.h b/ulib/system/system.h new file mode 100644 index 0000000..30149b0 --- /dev/null +++ b/ulib/system/system.h @@ -0,0 +1,6 @@ +#ifndef ULIB_SYSTEM_SYSTEM_H_ +#define ULIB_SYSTEM_SYSTEM_H_ + +void sys_debugprint(const char *string); + +#endif // ULIB_SYSTEM_SYSTEM_H_ diff --git a/user/Makefile.inc b/user/Makefile.inc index 8d751cb..35f41bd 100644 --- a/user/Makefile.inc +++ b/user/Makefile.inc @@ -1,6 +1,5 @@ -ARCH ?= x86_64 - -CFLAGS := -ffreestanding -Wall -Wextra -g -fcommon -nostdinc +CFLAGS := -ffreestanding -Wall -Wextra -g -fcommon -nostdinc \ + -isystem $(ROOT)/std/include -isystem $(ROOT)/ulib CURRENT_DIR := $(dir $(abspath $(lastword $(MAKEFILE_LIST)))) diff --git a/user/arch/x86_64/link.ld b/user/arch/x86_64/link.ld index a8e5fff..353c4fe 100644 --- a/user/arch/x86_64/link.ld +++ b/user/arch/x86_64/link.ld @@ -1,17 +1,17 @@ ENTRY(_start) SECTIONS { - . = 0x0000000000000000; - - .rodata ALIGN(4K): - { - *(.rodata .rodata.*) - } + /* . = 0x0000000000000000; */ .text ALIGN(4K): { *(.text .text.*) } + + .rodata (READONLY): ALIGN(4K) + { + *(.rodata .rodata.*) + } .data ALIGN(4K): { @@ -20,6 +20,8 @@ SECTIONS { .bss ALIGN(4K): { + _bss_start = .; *(.bss .bss.*) + _bss_end = .; } } diff --git a/user/arch/x86_64/x86_64.mk b/user/arch/x86_64/x86_64.mk index bec49ac..ceee098 100644 --- a/user/arch/x86_64/x86_64.mk +++ b/user/arch/x86_64/x86_64.mk @@ -1,18 +1,5 @@ -CC := x86_64-elf-gcc -LD := x86_64-elf-ld - -CFLAGS += -m64 \ - -fPIE \ - -mno-80387 \ - -mno-mmx \ - -mno-sse \ - -nostartfiles \ - -nostdlib \ - -mno-sse2 \ - -mno-red-zone \ - -fno-stack-protector \ - -fno-stack-check \ - -Os \ +include $(ROOT)/mk/user/x86_64.mk +include $(ROOT)/mk/arch/toolchain-x86_64.mk LDFLAGS += -m elf_x86_64 \ -pie \ diff --git a/user/hello/Makefile b/user/hello/Makefile index ccd4b6d..645d364 100644 --- a/user/hello/Makefile +++ b/user/hello/Makefile @@ -14,9 +14,9 @@ OBJ := $(call GET_OBJ, $(SRCFILES)) all: $(TARGET) -hello: $(OBJ) +$(TARGET): $(OBJ) $(LD) $^ $(LDFLAGS) -o $@ - echo $(realpath hello) >> $(FILES) + echo $$(realpath $(TARGET)) >> $(FILES) clean: rm -f $(OBJ) $(TARGET) diff --git a/user/hello/hello.s b/user/hello/hello.s index 0abbbc5..ee947fe 100644 --- a/user/hello/hello.s +++ b/user/hello/hello.s @@ -11,8 +11,7 @@ _start: movq $1, %rax lea STRING(%rip), %rdi lea STRING_LEN(%rip), %rsi - syscall + int $0x80 movq $2, %rax - syscall - + int $0x80 diff --git a/user/init/.gitignore b/user/init/.gitignore new file mode 100644 index 0000000..808d629 --- /dev/null +++ b/user/init/.gitignore @@ -0,0 +1,2 @@ +*.o +init diff --git a/user/init/Makefile b/user/init/Makefile new file mode 100644 index 0000000..a8ff1bc --- /dev/null +++ b/user/init/Makefile @@ -0,0 +1,24 @@ +include $(ROOT)/mk/grabsrc.mk +include ../Makefile.inc + +.PHONY: all clean + +TARGET := init + +LDFLAGS += -L$(ROOT)/ulib -l:libulib.a + +SRCFILES := $(call GRABSRC, .) +CFILES := $(call GET_CFILES, $(SRCFILES)) +OBJ := $(call GET_OBJ, $(SRCFILES)) + +%.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ + +all: $(TARGET) + +$(TARGET): $(OBJ) + $(LD) $^ $(LDFLAGS) -o $@ + echo $$(realpath $(TARGET)) >> $(FILES) + +clean: + rm -f $(OBJ) $(TARGET) diff --git a/user/init/main.c b/user/init/main.c new file mode 100644 index 0000000..205d20b --- /dev/null +++ b/user/init/main.c @@ -0,0 +1,6 @@ +#include + +void main(void) { + sys_debugprint("Hello world from userspace in C"); +} +