From aa2843fdbdd9b5cc579d198fd1a9ec874642706e Mon Sep 17 00:00:00 2001 From: Minep Date: Sun, 20 Aug 2023 01:27:12 +0100 Subject: [PATCH] fix: corrected time conversion on alarm/sleep syscall fix: makefile does not respond to source file change feat: makefile respond to header change chore: add description on arch-specific code chore: clean up --- lunaix-os/.gitignore | 3 ++- lunaix-os/arch/i386/README | 32 +++++++++++++++++++++++++++++ lunaix-os/flags.h | 8 +------- lunaix-os/hal/timer/hwtimer.c | 12 +++++------ lunaix-os/includes/hal/hwtimer.h | 2 ++ lunaix-os/includes/lunaix/process.h | 8 ++++++++ lunaix-os/kernel.mk | 6 ++++-- lunaix-os/kernel/proc0.c | 14 ++++++------- lunaix-os/kernel/process/sched.c | 7 ++----- lunaix-os/kernel/time/clock.c | 2 +- lunaix-os/makefile | 1 - lunaix-os/makeinc/toolchain.mkinc | 2 +- 12 files changed, 66 insertions(+), 31 deletions(-) create mode 100644 lunaix-os/arch/i386/README diff --git a/lunaix-os/.gitignore b/lunaix-os/.gitignore index 32bb844..b48c604 100644 --- a/lunaix-os/.gitignore +++ b/lunaix-os/.gitignore @@ -10,4 +10,5 @@ draft/ iso_inspect/ unused/ -**.o \ No newline at end of file +**.o +**.d \ No newline at end of file diff --git a/lunaix-os/arch/i386/README b/lunaix-os/arch/i386/README new file mode 100644 index 0000000..e87a8c0 --- /dev/null +++ b/lunaix-os/arch/i386/README @@ -0,0 +1,32 @@ +Lunaix kernel arch specific ABI +====== + +This document provides a checklist if one wants to add support for novel architecture + +(It is far from complete, as the refactoring is going on) + +Implementation checklist: + [ ] An entry point that recieve control from second stage bootloader. + [ ] Interrupt vectoring must be done before invoking kernel_bootstrap. + [ ] Prepare the boot_handoff state struct, according to system info provided + by upstream bootloader + [ ] Invoke the kernel_bootstrap, pass the boot_handoff as it's only argument + This will transfer the control to Lunaix kernel. + [ ] A syscall handler, syscall table, and the syscall vectoring + [ ] A interrupt handler, must take care of context save/restore, signal handling + [ ] A system virtual memory map + [ ] Implement the following abstractions + +Referenced headers + * /includes/sys/abi.h + * /includes/sys/interrupt.h + * /includes/sys/port_io.h + * /includes/sys/pci_hba.h + * /includes/mm/mempart.h + * /includes/cpu.h + +Referenced functions + * includes/hal/hwtimer.h:hwtimer_choose + * includes/hal/hwrtc.h:hwrtc_choose + * includes/hal/intc.h:intc_init + * includes/lunaix/process.h:proc_init_transfer \ No newline at end of file diff --git a/lunaix-os/flags.h b/lunaix-os/flags.h index 61d12c9..7f9835f 100644 --- a/lunaix-os/flags.h +++ b/lunaix-os/flags.h @@ -1,7 +1,7 @@ #ifndef __LUNAIX_FLAGS_H #define __LUNAIX_FLAGS_H -#ifdef __ARCH_IA32 +#if __ARCH__ == i386 #define PLATFORM_TARGET "x86_32" #else #define PLATFORM_TARGET "unknown" @@ -9,12 +9,6 @@ #define LUNAIX_VER "0.0.1-dev" -/* - Uncomment below to force LunaixOS use kernel page table when context switch - to kernel space NOTE: This will make the kernel global. -*/ -// #define USE_KERNEL_PG - /* Uncomment below to disable all assertion */ diff --git a/lunaix-os/hal/timer/hwtimer.c b/lunaix-os/hal/timer/hwtimer.c index 9c864c7..bd6477a 100644 --- a/lunaix-os/hal/timer/hwtimer.c +++ b/lunaix-os/hal/timer/hwtimer.c @@ -1,7 +1,7 @@ #include #include -struct hwtimer_context* sys_hwtctx; +struct hwtimer_context* current_timer; void hwtimer_init(u32_t hertz, void* tick_callback) @@ -11,19 +11,19 @@ hwtimer_init(u32_t hertz, void* tick_callback) hwt_ctx->init(hwt_ctx, hertz, tick_callback); hwt_ctx->running_freq = hertz; - sys_hwtctx = hwt_ctx; + current_timer = hwt_ctx; } ticks_t hwtimer_base_frequency() { - return sys_hwtctx->base_freq; + return current_timer->base_freq; } ticks_t hwtimer_current_systicks() { - return sys_hwtctx->systicks(); + return current_timer->systicks(); } ticks_t @@ -31,10 +31,10 @@ hwtimer_to_ticks(u32_t value, int unit) { // in case system frequency is less than 1000Hz if (unit != TIME_MS) { - return sys_hwtctx->running_freq * unit * value; + return current_timer->running_freq * unit * value; } - ticks_t freq_ms = sys_hwtctx->running_freq / 1000; + ticks_t freq_ms = current_timer->running_freq / 1000; return freq_ms * value; } \ No newline at end of file diff --git a/lunaix-os/includes/hal/hwtimer.h b/lunaix-os/includes/hal/hwtimer.h index efa72d8..bf0595d 100644 --- a/lunaix-os/includes/hal/hwtimer.h +++ b/lunaix-os/includes/hal/hwtimer.h @@ -18,6 +18,8 @@ struct hwtimer_context ticks_t running_freq; }; +extern struct hwtimer_context* current_timer; + void hwtimer_init(u32_t hertz, void* tick_callback); diff --git a/lunaix-os/includes/lunaix/process.h b/lunaix-os/includes/lunaix/process.h index 6ef1ff0..10dc5ec 100644 --- a/lunaix-os/includes/lunaix/process.h +++ b/lunaix-os/includes/lunaix/process.h @@ -198,6 +198,14 @@ proc_clear_signal(struct proc_info* proc); // enable interrupt upon transfer #define TRANSFER_IE 1 +/** + * @brief Setup process initial context, used to initiate first switch + * + * @param proc + * @param stop + * @param target + * @param flags + */ void proc_init_transfer(struct proc_info* proc, ptr_t stop, ptr_t target, int flags); diff --git a/lunaix-os/kernel.mk b/lunaix-os/kernel.mk index c844703..ba15fa3 100644 --- a/lunaix-os/kernel.mk +++ b/lunaix-os/kernel.mk @@ -20,6 +20,7 @@ kbin := $(BUILD_NAME) ksrc_files := $(foreach f, $(ksrc_dirs), $(shell find $(f) -name "*.[cS]")) ksrc_objs := $(addsuffix .o,$(ksrc_files)) +ksrc_deps := $(addsuffix .d,$(ksrc_files)) kinc_opts := $(addprefix -I,$(kinc_dirs)) @@ -34,7 +35,7 @@ CFLAGS += -include flags.h $(call status_,CC,$<) @$(CC) $(CFLAGS) $(kinc_opts) -c $< -o $@ -$(kbin): $(ksrc_objs) $(kbin_dir) +$(kbin): $(kbin_dir) $(ksrc_files) $(ksrc_objs) $(call status_,LD,$@) @$(CC) -T link/linker.ld -o $(kbin) $(ksrc_objs) $(LDFLAGS) @@ -45,4 +46,5 @@ $(kbin_dir)/modksyms: $(kbin) all: $(kbin) $(kbin_dir)/modksyms clean: - @rm -f $(ksrc_objs) \ No newline at end of file + @rm -f $(ksrc_objs) + @rm -f $(ksrc_deps) \ No newline at end of file diff --git a/lunaix-os/kernel/proc0.c b/lunaix-os/kernel/proc0.c index 64dbcd9..c30feec 100644 --- a/lunaix-os/kernel/proc0.c +++ b/lunaix-os/kernel/proc0.c @@ -20,8 +20,6 @@ #include -#include -#include #include #include @@ -78,8 +76,8 @@ void __proc0() { /* - * We must defer boot code/data cleaning after we successfully escape that - * area + * We must defer boot code/data cleaning to here, after we successfully + * escape that area */ boot_cleanup(); @@ -104,15 +102,17 @@ init_platform() twifs_register_plugins(); - /* we must start probing pci after all drivers are registered! */ + /* + * all device registering and loading must defered to here! + * due to limited stack size and partial scheduling context + */ pci_load_devices(); // debugger serial_init(); sdbg_init(); - // FIXME ps2 kbd is x86 PC specific, not here. - // peripherals & chipset features + // FIXME ps2 kbd is a device, must not be here ps2_kbd_init(); // console diff --git a/lunaix-os/kernel/process/sched.c b/lunaix-os/kernel/process/sched.c index 651ab0e..82c73c0 100644 --- a/lunaix-os/kernel/process/sched.c +++ b/lunaix-os/kernel/process/sched.c @@ -196,7 +196,7 @@ __DEFINE_LXSYSCALL1(unsigned int, sleep, unsigned int, seconds) } struct proc_info* root_proc = sched_ctx._procs[0]; - __current->sleep.wakeup_time = clock_systime() + seconds * 1000; + __current->sleep.wakeup_time = clock_systime() + seconds; if (llist_empty(&__current->sleep.sleepers)) { llist_append(&root_proc->sleep.sleepers, &__current->sleep.sleepers); @@ -210,15 +210,12 @@ __DEFINE_LXSYSCALL1(unsigned int, sleep, unsigned int, seconds) return 0; } -// FIXME issue with alarm, paused parent process never got wake up, check what -// has been fucked up by refactoring. - __DEFINE_LXSYSCALL1(unsigned int, alarm, unsigned int, seconds) { time_t prev_ddl = __current->sleep.alarm_time; time_t now = clock_systime(); - __current->sleep.alarm_time = seconds ? now + seconds * 1000 : 0; + __current->sleep.alarm_time = seconds ? now + seconds : 0; struct proc_info* root_proc = sched_ctx._procs[0]; if (llist_empty(&__current->sleep.sleepers)) { diff --git a/lunaix-os/kernel/time/clock.c b/lunaix-os/kernel/time/clock.c index 726e333..2dc4e81 100644 --- a/lunaix-os/kernel/time/clock.c +++ b/lunaix-os/kernel/time/clock.c @@ -69,7 +69,7 @@ time_t clock_systime() { ticks_t t = hwtimer_current_systicks(); - return t / hwtimer_base_frequency(); + return t / current_timer->running_freq; } void diff --git a/lunaix-os/makefile b/lunaix-os/makefile index e8deacf..7513516 100644 --- a/lunaix-os/makefile +++ b/lunaix-os/makefile @@ -5,7 +5,6 @@ include $(mkinc_dir)/toolchain.mkinc include $(mkinc_dir)/qemu.mkinc include $(mkinc_dir)/utils.mkinc - ARCH ?= i386 export ARCH diff --git a/lunaix-os/makeinc/toolchain.mkinc b/lunaix-os/makeinc/toolchain.mkinc index b629d6f..70ef37b 100644 --- a/lunaix-os/makeinc/toolchain.mkinc +++ b/lunaix-os/makeinc/toolchain.mkinc @@ -23,7 +23,7 @@ OFLAGS := -fno-gcse\ -fno-inline-small-functions \ -fno-indirect-inlining -CFLAGS := -std=gnu99 -ffreestanding $(OFLAGS) $(W) $(ARCH_OPT) +CFLAGS := -std=gnu99 -ffreestanding -MMD $(OFLAGS) $(W) $(ARCH_OPT) ifeq ($(BUILD_MODE),debug) O = -Og -- 2.27.0