From e077d322f4e58d794804e142c2a1be1a4664e7e1 Mon Sep 17 00:00:00 2001 From: kamkow1 Date: Sun, 4 Jan 2026 01:11:31 +0100 Subject: [PATCH] Rewrite init app in C, introduce MSL (MOP3 System Library) --- kernel/.clang-format => .clang-format | 0 Makefile | 9 +++-- amd64/link.ld | 4 ++ aux/devel.sh | 1 + generic/flags.mk | 5 ++- .../LICENSE => include/c_hdrs_LICENSE | 0 .../c_headers/README => include/c_hdrs_README | 0 {kernel/c_headers/include => include}/float.h | 0 .../c_headers/include => include}/iso646.h | 0 .../c_headers/include => include}/limits.h | 0 .../defs.h => include/m/syscall_defs.h | 6 +-- .../c_headers/include => include}/stdalign.h | 0 .../c_headers/include => include}/stdarg.h | 0 .../c_headers/include => include}/stdatomic.h | 0 .../c_headers/include => include}/stdbool.h | 0 .../c_headers/include => include}/stddef.h | 0 .../c_headers/include => include}/stdint.h | 0 .../include => include}/stdnoreturn.h | 0 init/Makefile | 2 +- init/init.S | 12 ------ init/init.c | 9 +++++ kernel/amd64/apic.c | 4 +- kernel/amd64/bootmain.c | 5 --- kernel/amd64/hpet.c | 38 +++++++------------ kernel/amd64/hpet.h | 1 - kernel/amd64/init.c | 2 - kernel/amd64/intr.c | 10 ----- kernel/amd64/intr_stub.S | 6 --- kernel/amd64/sched1.c | 2 +- kernel/amd64/smp.c | 34 +++++++++-------- kernel/amd64/smp.h | 1 + kernel/amd64/syscall.c | 4 +- kernel/amd64/syscallentry.S | 19 +--------- kernel/generic/flags.mk | 2 +- kernel/sys/debug.h | 6 +-- kernel/syscall/syscall.c | 2 +- libmsl/Makefile | 28 ++++++++++++++ libmsl/amd64/.gitignore | 1 + libmsl/amd64/_start.S | 8 ++++ libmsl/amd64/src.mk | 6 +++ libmsl/amd64/syscall.c | 23 +++++++++++ libmsl/amd64/syscall.h | 8 ++++ libmsl/init/.gitignore | 1 + libmsl/init/__premain.c | 22 +++++++++++ libmsl/init/src.mk | 3 ++ libmsl/m/.gitignore | 1 + libmsl/m/proc.c | 10 +++++ libmsl/m/proc.h | 8 ++++ libmsl/m/src.mk | 3 ++ libmsl/m/syscall.h | 11 ++++++ libmsl/src.mk | 3 ++ apps.mk => make/apps.mk | 0 dist.mk => make/dist.mk | 0 docs.mk => make/docs.mk | 0 kernel.mk => make/kernel.mk | 0 make/libc.mk | 10 +++++ user.mk => make/user.mk | 4 +- 57 files changed, 214 insertions(+), 120 deletions(-) rename kernel/.clang-format => .clang-format (100%) rename kernel/c_headers/LICENSE => include/c_hdrs_LICENSE (100%) rename kernel/c_headers/README => include/c_hdrs_README (100%) rename {kernel/c_headers/include => include}/float.h (100%) rename {kernel/c_headers/include => include}/iso646.h (100%) rename {kernel/c_headers/include => include}/limits.h (100%) rename kernel/syscall/defs.h => include/m/syscall_defs.h (54%) rename {kernel/c_headers/include => include}/stdalign.h (100%) rename {kernel/c_headers/include => include}/stdarg.h (100%) rename {kernel/c_headers/include => include}/stdatomic.h (100%) rename {kernel/c_headers/include => include}/stdbool.h (100%) rename {kernel/c_headers/include => include}/stddef.h (100%) rename {kernel/c_headers/include => include}/stdint.h (100%) rename {kernel/c_headers/include => include}/stdnoreturn.h (100%) delete mode 100644 init/init.S create mode 100644 init/init.c create mode 100644 libmsl/Makefile create mode 100644 libmsl/amd64/.gitignore create mode 100644 libmsl/amd64/_start.S create mode 100644 libmsl/amd64/src.mk create mode 100644 libmsl/amd64/syscall.c create mode 100644 libmsl/amd64/syscall.h create mode 100644 libmsl/init/.gitignore create mode 100644 libmsl/init/__premain.c create mode 100644 libmsl/init/src.mk create mode 100644 libmsl/m/.gitignore create mode 100644 libmsl/m/proc.c create mode 100644 libmsl/m/proc.h create mode 100644 libmsl/m/src.mk create mode 100644 libmsl/m/syscall.h create mode 100644 libmsl/src.mk rename apps.mk => make/apps.mk (100%) rename dist.mk => make/dist.mk (100%) rename docs.mk => make/docs.mk (100%) rename kernel.mk => make/kernel.mk (100%) create mode 100644 make/libc.mk rename user.mk => make/user.mk (81%) diff --git a/kernel/.clang-format b/.clang-format similarity index 100% rename from kernel/.clang-format rename to .clang-format diff --git a/Makefile b/Makefile index 6532dab..2f213f7 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ platform ?= amd64 -include apps.mk -include kernel.mk -include dist.mk -include docs.mk +include make/apps.mk +include make/kernel.mk +include make/dist.mk +include make/docs.mk +include make/libc.mk diff --git a/amd64/link.ld b/amd64/link.ld index c426fc5..657fd53 100644 --- a/amd64/link.ld +++ b/amd64/link.ld @@ -31,10 +31,14 @@ SECTIONS { *(.data .data.*) } :data + __bss_start = .; + .bss : { *(.bss .bss.*) } :data + __bss_end = .; + /DISCARD/ : { *(.eh_frame*) *(.note .note.*) diff --git a/aux/devel.sh b/aux/devel.sh index 6e81ad2..88ba747 100755 --- a/aux/devel.sh +++ b/aux/devel.sh @@ -8,6 +8,7 @@ else make -B all_kernel fi +make -B all_libmsl make -B all_apps make -B all_dist ./aux/limine_iso_amd64.sh diff --git a/generic/flags.mk b/generic/flags.mk index 89aa98c..ae363e1 100644 --- a/generic/flags.mk +++ b/generic/flags.mk @@ -5,8 +5,9 @@ cflags += -nostdinc \ -std=c11 \ -pedantic \ -Wall \ - -Wextra \ - -mcmodel=kernel + -Wextra + +cflags += -isystem ../include ldflags += -ffreestanding \ -nostdlib \ diff --git a/kernel/c_headers/LICENSE b/include/c_hdrs_LICENSE similarity index 100% rename from kernel/c_headers/LICENSE rename to include/c_hdrs_LICENSE diff --git a/kernel/c_headers/README b/include/c_hdrs_README similarity index 100% rename from kernel/c_headers/README rename to include/c_hdrs_README diff --git a/kernel/c_headers/include/float.h b/include/float.h similarity index 100% rename from kernel/c_headers/include/float.h rename to include/float.h diff --git a/kernel/c_headers/include/iso646.h b/include/iso646.h similarity index 100% rename from kernel/c_headers/include/iso646.h rename to include/iso646.h diff --git a/kernel/c_headers/include/limits.h b/include/limits.h similarity index 100% rename from kernel/c_headers/include/limits.h rename to include/limits.h diff --git a/kernel/syscall/defs.h b/include/m/syscall_defs.h similarity index 54% rename from kernel/syscall/defs.h rename to include/m/syscall_defs.h index 3f3701c..f47ac06 100644 --- a/kernel/syscall/defs.h +++ b/include/m/syscall_defs.h @@ -1,5 +1,5 @@ -#ifndef _KERNEL_SYSCALL_DEFS_H -#define _KERNEL_SYSCALL_DEFS_H +#ifndef _M_SYSCALL_DEFS_H +#define _M_SYSCALL_DEFS_H #define SYS_PROC_QUIT 1 #define SYS_PROC_TEST 2 @@ -7,4 +7,4 @@ #define SR_OK 0 #define SR_SYSCALL_NOT_FOUND 1 -#endif // _KERNEL_SYSCALL_DEFS_H +#endif // _M_SYSCALL_DEFS_H diff --git a/kernel/c_headers/include/stdalign.h b/include/stdalign.h similarity index 100% rename from kernel/c_headers/include/stdalign.h rename to include/stdalign.h diff --git a/kernel/c_headers/include/stdarg.h b/include/stdarg.h similarity index 100% rename from kernel/c_headers/include/stdarg.h rename to include/stdarg.h diff --git a/kernel/c_headers/include/stdatomic.h b/include/stdatomic.h similarity index 100% rename from kernel/c_headers/include/stdatomic.h rename to include/stdatomic.h diff --git a/kernel/c_headers/include/stdbool.h b/include/stdbool.h similarity index 100% rename from kernel/c_headers/include/stdbool.h rename to include/stdbool.h diff --git a/kernel/c_headers/include/stddef.h b/include/stddef.h similarity index 100% rename from kernel/c_headers/include/stddef.h rename to include/stddef.h diff --git a/kernel/c_headers/include/stdint.h b/include/stdint.h similarity index 100% rename from kernel/c_headers/include/stdint.h rename to include/stdint.h diff --git a/kernel/c_headers/include/stdnoreturn.h b/include/stdnoreturn.h similarity index 100% rename from kernel/c_headers/include/stdnoreturn.h rename to include/stdnoreturn.h diff --git a/init/Makefile b/init/Makefile index fdfe1e2..d16094b 100644 --- a/init/Makefile +++ b/init/Makefile @@ -1 +1 @@ -include ../user.mk +include ../make/user.mk diff --git a/init/init.S b/init/init.S deleted file mode 100644 index ddfff1d..0000000 --- a/init/init.S +++ /dev/null @@ -1,12 +0,0 @@ -.global _start -_start: - pushq $123 - addq $8, %rsp - - movq $2, %rax - syscall - - movq $1, %rax - syscall - - jmp _start diff --git a/init/init.c b/init/init.c new file mode 100644 index 0000000..9bbdca6 --- /dev/null +++ b/init/init.c @@ -0,0 +1,9 @@ +#include +#include + +void app_main (void) { + m_proc_test (); + m_proc_test (); + m_proc_test (); + m_proc_test (); +} diff --git a/kernel/amd64/apic.c b/kernel/amd64/apic.c index 8e5a551..593c58c 100644 --- a/kernel/amd64/apic.c +++ b/kernel/amd64/apic.c @@ -235,7 +235,7 @@ void amd64_lapic_tick (uint32_t tick) { amd64_lapic_write (LAPIC_TIMICT, tick); * @return amount of ticsk in a given period */ static uint32_t amd64_lapic_calibrate (uint32_t us) { - amd64_lapic_write (LAPIC_DCR, 0x03); + amd64_lapic_write (LAPIC_DCR, 0x0B); amd64_lapic_write (LAPIC_LVTTR, SCHED_PREEMPT_TIMER | (1 << 16)); @@ -256,7 +256,7 @@ static uint32_t amd64_lapic_calibrate (uint32_t us) { * Initial tick count */ static void amd64_lapic_start (uint32_t ticks) { - amd64_lapic_write (LAPIC_DCR, 0x03); + amd64_lapic_write (LAPIC_DCR, 0x0B); amd64_lapic_write (LAPIC_LVTTR, SCHED_PREEMPT_TIMER | (1 << 17)); diff --git a/kernel/amd64/bootmain.c b/kernel/amd64/bootmain.c index bc2bb16..967eb1e 100644 --- a/kernel/amd64/bootmain.c +++ b/kernel/amd64/bootmain.c @@ -35,7 +35,6 @@ ALIGNED (16) static uint8_t uacpi_memory_buffer[UACPI_MEMORY_BUFFER_MAX]; */ void bootmain (void) { struct cpu* bsp_cpu = cpu_make (); - amd64_thiscpu_set_init (); amd64_init (bsp_cpu, false); syscall_init (); @@ -52,10 +51,6 @@ void bootmain (void) { smp_init (); - /* busy wait for cpus to come online */ - for (volatile int i = 0; i < INT_MAX; i++) - ; - mm_init2 (); proc_init (); diff --git a/kernel/amd64/hpet.c b/kernel/amd64/hpet.c index e879896..a3a7ad7 100644 --- a/kernel/amd64/hpet.c +++ b/kernel/amd64/hpet.c @@ -28,8 +28,8 @@ static bool hpet_32bits = 1; /// Physical address for HPET MMIO static uintptr_t hpet_paddr; -/// HPET nanoseconds for conversion -static uint64_t hpet_clock_nano; +/// HPET period in femtoseconds +static uint64_t hpet_period_fs; /// Lock, which protects concurrent access. See \ref amd64/smp.c static spin_lock_t hpet_lock = SPIN_LOCK_INIT; @@ -54,32 +54,22 @@ static void amd64_hpet_write (uint32_t reg, uint64_t value) { /// Read current value of \ref HPET_MCVR register. static uint64_t amd64_hpet_timestamp (void) { return amd64_hpet_read (HPET_MCVR); } -/** - * @brief Get current HPET timestamp in nanoseconds - * - * @param lock - * if true, hold \ref hpet_lock - */ -uint64_t amd64_hpet_current_nano (bool lock) { - if (lock) - spin_lock (&hpet_lock); - - uint64_t t = amd64_hpet_timestamp () * hpet_clock_nano; - - if (lock) - spin_unlock (&hpet_lock); - - return t; -} - /// Sleep for a given amount of microseconds. This time can last longer due to \ref hpet_lock being held. void amd64_hpet_sleep_micro (uint64_t us) { spin_lock (&hpet_lock); uint64_t start = amd64_hpet_timestamp (); - uint64_t conv = us * 1000; - while (((amd64_hpet_timestamp () - start) * hpet_clock_nano) < conv) + uint64_t target_fs = us * 1000000000ULL; + + for (;;) { + uint64_t current = amd64_hpet_timestamp (); + uint64_t dt = current - start; + + if ((dt * hpet_period_fs) >= target_fs) + break; + __asm__ volatile ("pause" ::: "memory"); + } spin_unlock (&hpet_lock); } @@ -114,7 +104,5 @@ void amd64_hpet_init (void) { gcidr = (((uint64_t)high << 32) | low); } - uint64_t period_fs = (gcidr >> 32); - - hpet_clock_nano = period_fs / 1000000; + hpet_period_fs = (gcidr >> 32); } diff --git a/kernel/amd64/hpet.h b/kernel/amd64/hpet.h index 48e5920..6841998 100644 --- a/kernel/amd64/hpet.h +++ b/kernel/amd64/hpet.h @@ -3,7 +3,6 @@ #include -uint64_t amd64_hpet_current_nano (bool lock); void amd64_hpet_sleep_micro (uint64_t us); void amd64_hpet_init (void); diff --git a/kernel/amd64/init.c b/kernel/amd64/init.c index 539f4f7..0384c34 100644 --- a/kernel/amd64/init.c +++ b/kernel/amd64/init.c @@ -65,8 +65,6 @@ static void amd64_gdt_init (struct cpu* cpu) { "movw %%ax, %%ds\n" "movw %%ax, %%es\n" "movw %%ax, %%ss\n" - "movw %%ax, %%fs\n" - "movw %%ax, %%gs\n" : : [kcode] "i"(GDT_KCODE), [kdata] "i"(GDT_KDATA) : "rax", "memory"); diff --git a/kernel/amd64/intr.c b/kernel/amd64/intr.c index e9036fb..ad7bc75 100644 --- a/kernel/amd64/intr.c +++ b/kernel/amd64/intr.c @@ -175,8 +175,6 @@ static void amd64_intr_exception (struct saved_regs* regs) { void amd64_intr_handler (void* stack_ptr) { struct saved_regs* regs = stack_ptr; - amd64_load_kernel_cr3 (); - if (regs->trap <= 31) { amd64_intr_exception (regs); } else { @@ -219,10 +217,6 @@ static void amd64_irq_restore_flags (uint64_t rflags) { /// Save current interrupt state void irq_save (void) { - /* before smp init. */ - if (thiscpu == NULL) - return; - int prev = atomic_fetch_add_explicit (&thiscpu->irq_ctx.nesting, 1, memory_order_acq_rel); if (prev == 0) thiscpu->irq_ctx.rflags = amd64_irq_save_flags (); @@ -230,10 +224,6 @@ void irq_save (void) { /// Restore interrupt state void irq_restore (void) { - /* before smp init. */ - if (thiscpu == NULL) - return; - int prev = atomic_fetch_sub_explicit (&thiscpu->irq_ctx.nesting, 1, memory_order_acq_rel); if (prev == 1) amd64_irq_restore_flags (thiscpu->irq_ctx.rflags); diff --git a/kernel/amd64/intr_stub.S b/kernel/amd64/intr_stub.S index 239d68e..1dd0f50 100644 --- a/kernel/amd64/intr_stub.S +++ b/kernel/amd64/intr_stub.S @@ -22,9 +22,6 @@ ;\ movq %rsp, %rdi; \ ;\ - movq %cr3, %rax; \ - pushq %rax; \ - ;\ movq %rsp, %rbp; \ ;\ subq $8, %rsp; \ @@ -34,9 +31,6 @@ ;\ movq %rbp, %rsp; \ ;\ - popq %rax; \ - movq %rax, %cr3; \ - ;\ pop_regs; \ addq $16, %rsp; \ ;\ diff --git a/kernel/amd64/sched1.c b/kernel/amd64/sched1.c index f04dc94..0ef09d0 100644 --- a/kernel/amd64/sched1.c +++ b/kernel/amd64/sched1.c @@ -9,6 +9,6 @@ void do_sched (struct proc* proc) { thiscpu->tss.rsp0 = proc->pdata.kernel_stack; thiscpu->syscall_kernel_stack = proc->pdata.kernel_stack; - amd64_wrmsr (MSR_GS_BASE, (uint64_t)proc->pdata.gs_base); + amd64_do_sched ((void*)&proc->pdata.regs, (void*)proc->pd.cr3_paddr); } diff --git a/kernel/amd64/smp.c b/kernel/amd64/smp.c index acdbe80..a5fc84a 100644 --- a/kernel/amd64/smp.c +++ b/kernel/amd64/smp.c @@ -13,38 +13,31 @@ #include /// Cpu ID counter -static uint32_t cpu_counter = 0; -/// Lock for \ref cpu_counter -static spin_lock_t cpu_counter_lock = SPIN_LOCK_INIT; +static atomic_uint cpu_counter = 0; /// The CPUs static struct cpu cpus[CPUS_MAX]; -static bool thiscpu_init = false; - -void amd64_thiscpu_set_init (void) { thiscpu_init = true; } +static atomic_int cpu_init_count; /// Allocate a CPU structure struct cpu* cpu_make (void) { - spin_lock (&cpu_counter_lock); - int id = cpu_counter++; - spin_unlock (&cpu_counter_lock); + int id = atomic_fetch_add (&cpu_counter, 1); struct cpu* cpu = &cpus[id]; memset (cpu, 0, sizeof (*cpu)); cpu->lock = SPIN_LOCK_INIT; cpu->id = id; + cpu->self = cpu; - amd64_wrmsr (MSR_SHADOW_GS_BASE, (uint64_t)cpu); + amd64_wrmsr (MSR_GS_BASE, (uint64_t)cpu); return cpu; } struct cpu* cpu_get (void) { - if (!thiscpu_init) - return NULL; - - return (struct cpu*)amd64_rdmsr (MSR_SHADOW_GS_BASE); + struct cpu* ptr = (struct cpu*)amd64_rdmsr (MSR_GS_BASE); + return ptr; } /// Bootstrap code for non-BSP CPUs @@ -56,27 +49,36 @@ static void amd64_smp_bootstrap (struct limine_mp_info* mp_info) { amd64_init (cpu, true); /* gdt + idt */ syscall_init (); - thiscpu->lapic_ticks = amd64_lapic_init (2500); + thiscpu->lapic_ticks = amd64_lapic_init (10000); amd64_lapic_tick (thiscpu->lapic_ticks); DEBUG ("CPU %u is online!\n", thiscpu->id); __asm__ volatile ("sti"); + atomic_fetch_sub (&cpu_init_count, 1); + for (;;) ; } /// Initialize SMP subsystem for AMD64. Start AP CPUs void smp_init (void) { - thiscpu->lapic_ticks = amd64_lapic_init (2500); + thiscpu->lapic_ticks = amd64_lapic_init (10000); struct limine_mp_response* mp = limine_mp_request.response; + cpu_init_count = mp->cpu_count - 1; /* Don't include BSP */ + for (size_t i = 0; i < mp->cpu_count; i++) { if (mp->cpus[i]->lapic_id != thiscpu->id) { DEBUG ("Trying CPU %u\n", mp->cpus[i]->lapic_id); mp->cpus[i]->goto_address = &amd64_smp_bootstrap; } } + + while (atomic_load (&cpu_init_count) > 0) + ; + + DEBUG ("All CPUs are online\n"); } diff --git a/kernel/amd64/smp.h b/kernel/amd64/smp.h index 33a3318..47c13d1 100644 --- a/kernel/amd64/smp.h +++ b/kernel/amd64/smp.h @@ -13,6 +13,7 @@ struct cpu { /* for syscall instruction */ uintptr_t syscall_user_stack; uintptr_t syscall_kernel_stack; + struct cpu* self; volatile uint8_t kernel_stack[KSTACK_SIZE] ALIGNED (16); volatile uint8_t except_stack[KSTACK_SIZE] ALIGNED (16); volatile uint8_t irq_stack[KSTACK_SIZE] ALIGNED (16); diff --git a/kernel/amd64/syscall.c b/kernel/amd64/syscall.c index 9588cd0..137cf75 100644 --- a/kernel/amd64/syscall.c +++ b/kernel/amd64/syscall.c @@ -6,7 +6,7 @@ #include #include #include -#include +#include #include extern void amd64_syscall_entry (void); @@ -14,8 +14,6 @@ extern void amd64_syscall_entry (void); int amd64_syscall_dispatch (void* stack_ptr) { struct saved_regs* regs = stack_ptr; - amd64_load_kernel_cr3 (); - int syscall_num = regs->rax; syscall_handler_func_t func = syscall_find_handler (syscall_num); diff --git a/kernel/amd64/syscallentry.S b/kernel/amd64/syscallentry.S index 1d2b1f3..f643b39 100644 --- a/kernel/amd64/syscallentry.S +++ b/kernel/amd64/syscallentry.S @@ -5,7 +5,6 @@ .global amd64_syscall_entry amd64_syscall_entry: cli - swapgs movq %rsp, %gs:0 movq %gs:8, %rsp @@ -20,15 +19,10 @@ amd64_syscall_entry: push_regs - swapgs - cld movq %rsp, %rdi - movq %cr3, %rax - pushq %rax - movq %rsp, %rbp subq $8, %rsp @@ -38,20 +32,9 @@ amd64_syscall_entry: movq %rbp, %rsp - popq %rax - movq %rax, %cr3 - pop_regs - swapgs - - addq $16, %rsp - popq %rcx - addq $8, %rsp - popq %r11 - addq $16, %rsp + addq $56, %rsp movq %gs:0, %rsp - swapgs - sysretq diff --git a/kernel/generic/flags.mk b/kernel/generic/flags.mk index 8dd83bf..97edd25 100644 --- a/kernel/generic/flags.mk +++ b/kernel/generic/flags.mk @@ -8,7 +8,7 @@ cflags += -nostdinc \ -Wextra \ -mcmodel=kernel -cflags += -isystem . -isystem c_headers/include +cflags += -isystem . -isystem ../include cflags += -DPRINTF_INCLUDE_CONFIG_H=1 \ -D_ALLOC_SKIP_DEFINE diff --git a/kernel/sys/debug.h b/kernel/sys/debug.h index 9144e7f..f4b4625 100644 --- a/kernel/sys/debug.h +++ b/kernel/sys/debug.h @@ -7,11 +7,7 @@ void debugprintf (const char* fmt, ...); #define DEBUG(fmt, ...) \ do { \ - if (thiscpu != NULL) { \ - debugprintf ("(CPU %u) %s:%d: " fmt, thiscpu->id, __func__, __LINE__, ##__VA_ARGS__); \ - } else { \ - debugprintf ("%s:%d: " fmt, __func__, __LINE__, ##__VA_ARGS__); \ - } \ + debugprintf ("(CPU %u) %s:%d: " fmt, thiscpu->id, __func__, __LINE__, ##__VA_ARGS__); \ } while (0) #endif // _KERNEL_SYS_DEBUG_H diff --git a/kernel/syscall/syscall.c b/kernel/syscall/syscall.c index a05ae6f..75b0437 100644 --- a/kernel/syscall/syscall.c +++ b/kernel/syscall/syscall.c @@ -2,7 +2,7 @@ #include #include #include -#include +#include #include #define DEFINE_SYSCALL(name) \ diff --git a/libmsl/Makefile b/libmsl/Makefile new file mode 100644 index 0000000..e7a71eb --- /dev/null +++ b/libmsl/Makefile @@ -0,0 +1,28 @@ +cc := clang +o := +c := +cflags := -isystem . +buildtype ?= release + +include src.mk +include ../generic/flags.mk +include ../$(platform)/flags.mk + +all: build/libmsl.a + +build/libmsl.a: $(o) + llvm-ar rcs $@ $^ + +%.o: %.c + $(cc) -c -o $@ $(cflags) $< + +%.o: %.S + $(cc) -c -o $@ $(cflags) $< + +clean: + rm -f $(o) build/libmsl.a + +format: + clang-format -i $$(git ls-files '*.c' '*.h') + +.PHONY: all clean format diff --git a/libmsl/amd64/.gitignore b/libmsl/amd64/.gitignore new file mode 100644 index 0000000..5761abc --- /dev/null +++ b/libmsl/amd64/.gitignore @@ -0,0 +1 @@ +*.o diff --git a/libmsl/amd64/_start.S b/libmsl/amd64/_start.S new file mode 100644 index 0000000..f3ffee1 --- /dev/null +++ b/libmsl/amd64/_start.S @@ -0,0 +1,8 @@ +.global _start +_start: + xorq %rbp, %rbp + movq %rsp, %rbp + andq $-16, %rsp + subq $8, %rsp + + callq __premain diff --git a/libmsl/amd64/src.mk b/libmsl/amd64/src.mk new file mode 100644 index 0000000..900ee73 --- /dev/null +++ b/libmsl/amd64/src.mk @@ -0,0 +1,6 @@ +c += amd64/syscall.c + +S += amd64/_start.S + +o += amd64/_start.o \ + amd64/syscall.o diff --git a/libmsl/amd64/syscall.c b/libmsl/amd64/syscall.c new file mode 100644 index 0000000..798e689 --- /dev/null +++ b/libmsl/amd64/syscall.c @@ -0,0 +1,23 @@ +#include +#include +#include + +int msl_amd64_syscall (int syscall_num, uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4, uintptr_t a5, uintptr_t a6) { + uint64_t result; + __asm__ volatile ( + "movq %1, %%rax\n" + "movq %2, %%rdi\n" + "movq %3, %%rsi\n" + "movq %4, %%rdx\n" + "movq %5, %%r10\n" + "movq %6, %%r8\n" + "movq %7, %%r9\n" + "syscall\n" + "movq %%rax, %0\n" + : "=r"(result) + : "r"((uint64_t)syscall_num), "r"(a1), "r"(a2), "r"(a3), "r"(a4), "r"(a5), "r"(a6) + : "memory", "cc", "rcx", "r11" + ); + return (int)result; +} + diff --git a/libmsl/amd64/syscall.h b/libmsl/amd64/syscall.h new file mode 100644 index 0000000..01243aa --- /dev/null +++ b/libmsl/amd64/syscall.h @@ -0,0 +1,8 @@ +#ifndef _LIBMSL_AMD64_SYSCALL_H +#define _LIBMSL_AMD64_SYSCALL_H + +#include + +int msl_amd64_syscall (int syscall_num, uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4, uintptr_t a5, uintptr_t a6); + +#endif // _LIBMSL_AMD64_SYSCALL_H diff --git a/libmsl/init/.gitignore b/libmsl/init/.gitignore new file mode 100644 index 0000000..5761abc --- /dev/null +++ b/libmsl/init/.gitignore @@ -0,0 +1 @@ +*.o diff --git a/libmsl/init/__premain.c b/libmsl/init/__premain.c new file mode 100644 index 0000000..fa36727 --- /dev/null +++ b/libmsl/init/__premain.c @@ -0,0 +1,22 @@ +#include +#include + +extern volatile uint8_t __bss_start[]; +extern volatile uint8_t __bss_end[]; + +extern void app_main (void); + +static void msl_clear_bss (void) { + uint8_t *p = (uint8_t*)__bss_start; + while (p < __bss_end) { + *p++ = 0; + } +} + +void __premain (void) { + msl_clear_bss (); + + app_main (); + + m_proc_quit (); +} diff --git a/libmsl/init/src.mk b/libmsl/init/src.mk new file mode 100644 index 0000000..e6c74ca --- /dev/null +++ b/libmsl/init/src.mk @@ -0,0 +1,3 @@ +c += init/__premain.c + +o += init/__premain.o diff --git a/libmsl/m/.gitignore b/libmsl/m/.gitignore new file mode 100644 index 0000000..5761abc --- /dev/null +++ b/libmsl/m/.gitignore @@ -0,0 +1 @@ +*.o diff --git a/libmsl/m/proc.c b/libmsl/m/proc.c new file mode 100644 index 0000000..53b9515 --- /dev/null +++ b/libmsl/m/proc.c @@ -0,0 +1,10 @@ +#include +#include + +int m_proc_quit (void) { + return m_syscall (SYS_PROC_QUIT, 0, 0, 0, 0, 0, 0); +} + +int m_proc_test (void) { + return m_syscall (SYS_PROC_TEST, 0, 0, 0, 0, 0, 0); +} diff --git a/libmsl/m/proc.h b/libmsl/m/proc.h new file mode 100644 index 0000000..1bb6667 --- /dev/null +++ b/libmsl/m/proc.h @@ -0,0 +1,8 @@ +#ifndef _LIBMSL_M_PROC_H +#define _LIBMSL_M_PROC_H + +int m_proc_quit (void); + +int m_proc_test (void); + +#endif // _LIBMSL_M_PROC_H diff --git a/libmsl/m/src.mk b/libmsl/m/src.mk new file mode 100644 index 0000000..9bf763a --- /dev/null +++ b/libmsl/m/src.mk @@ -0,0 +1,3 @@ +c += m/proc.c + +o += m/proc.o diff --git a/libmsl/m/syscall.h b/libmsl/m/syscall.h new file mode 100644 index 0000000..8bbd4e8 --- /dev/null +++ b/libmsl/m/syscall.h @@ -0,0 +1,11 @@ +#ifndef _LIBMSL_M_SYSCALL_H +#define _LIBMSL_M_SYSCALL_H + +#include + +#if defined(__x86_64__) + #include + #define m_syscall msl_amd64_syscall +#endif + +#endif // _LIBMSL_M_SYSCALL_H diff --git a/libmsl/src.mk b/libmsl/src.mk new file mode 100644 index 0000000..b61f45d --- /dev/null +++ b/libmsl/src.mk @@ -0,0 +1,3 @@ +include $(platform)/src.mk +include init/src.mk +include m/src.mk diff --git a/apps.mk b/make/apps.mk similarity index 100% rename from apps.mk rename to make/apps.mk diff --git a/dist.mk b/make/dist.mk similarity index 100% rename from dist.mk rename to make/dist.mk diff --git a/docs.mk b/make/docs.mk similarity index 100% rename from docs.mk rename to make/docs.mk diff --git a/kernel.mk b/make/kernel.mk similarity index 100% rename from kernel.mk rename to make/kernel.mk diff --git a/make/libc.mk b/make/libc.mk new file mode 100644 index 0000000..093017b --- /dev/null +++ b/make/libc.mk @@ -0,0 +1,10 @@ +all_libmsl: + make -C libmsl platform=$(platform) all + +clean_libmsl: + make -C libmsl platform=$(platform) clean + +format_libmsl: + make -C libmsl platform=$(platform) format + +.PHONY: all_libmsl clean_libmsl diff --git a/user.mk b/make/user.mk similarity index 81% rename from user.mk rename to make/user.mk index e017274..0c9b9ab 100644 --- a/user.mk +++ b/make/user.mk @@ -1,8 +1,8 @@ cc := clang o := c := -ldflags := -cflags := +ldflags := -L ../libmsl/build -l:libmsl.a +cflags := -isystem ../libmsl include src.mk include app.mk