From d1b1c8d9119229dbeed06cd252917e54a1cb77f6 Mon Sep 17 00:00:00 2001 From: Lunaixsky Date: Sat, 22 Jun 2024 15:16:00 +0100 Subject: [PATCH] Decoupling Architectural-specific Code (#35) * add headers for generic arch implementation * restructure the arch-dependent file, better separation * populate generic architecture for templating * more cleaning and refactoring to keep things stay decoupled * allow architectural specific code to override klib functions. * merge lib/ into klibc for uniformity * add x86 specific crc32 implementation, require SSE4 extension * rename isr_param to more general hart_state * isolate: direct reference of memeber inside hart_state * isolate the hstate dumping part of trace.h. * general clean up * decouple gdbstub from architectual code. * fix the trace_log missing component name * add reg_t to shadow the explicit typed register value * rename hart_change_execution to hart_flow_redirect --- lunaix-os/arch/README.md | 57 +++++ lunaix-os/arch/generic/arch.c | 8 + lunaix-os/arch/generic/hart.c | 18 ++ lunaix-os/arch/generic/includes/sys/abi.h | 58 +++++ lunaix-os/arch/generic/includes/sys/cpu.h | 71 ++++++ .../arch/generic/includes/sys/failsafe.h | 23 ++ lunaix-os/arch/generic/includes/sys/gdbstub.h | 29 +++ lunaix-os/arch/generic/includes/sys/hart.h | 54 +++++ .../arch/generic/includes/sys/mm/memory.h | 14 ++ .../arch/generic/includes/sys/mm/mempart.h | 60 ++++++ .../arch/generic/includes/sys/mm/mm_defs.h | 34 +++ .../arch/generic/includes/sys/mm/physical.h | 14 ++ lunaix-os/arch/generic/includes/sys/mm/tlb.h | 99 +++++++++ .../arch/generic/includes/sys/muldiv64.h | 15 ++ lunaix-os/arch/generic/includes/sys/pci_hba.h | 31 +++ lunaix-os/arch/generic/includes/sys/port_io.h | 89 ++++++++ lunaix-os/arch/generic/includes/sys/trace.h | 11 + lunaix-os/arch/generic/mm/fault.c | 7 + lunaix-os/arch/generic/mm/mm.c | 20 ++ lunaix-os/arch/i386/README | 32 --- lunaix-os/arch/i386/arch.c | 16 +- lunaix-os/arch/i386/exceptions/i386_isrm.c | 50 ++++- lunaix-os/arch/i386/exceptions/interrupt.S | 14 +- lunaix-os/arch/i386/exceptions/interrupts.c | 30 +-- .../arch/i386/exceptions/intr_routines.c | 34 +-- lunaix-os/arch/i386/gdbstub.c | 77 +++++++ lunaix-os/arch/i386/hal/apic.c | 8 +- .../{hal/timer => arch/i386/hal}/apic_timer.c | 10 +- .../{includes => arch/i386}/hal/apic_timer.h | 0 lunaix-os/arch/i386/hal/ioapic.c | 8 +- .../{hal/rtc => arch/i386/hal}/mc146818a.c | 12 +- lunaix-os/arch/i386/hal/pci_hba.c | 32 --- .../{hal/char => arch/i386/hal}/ps2kbd.c | 12 +- lunaix-os/{hal/rng => arch/i386/hal}/rngx86.c | 0 lunaix-os/arch/i386/hal/x86_intc.c | 16 -- lunaix-os/arch/i386/{pcontext.c => hart.c} | 22 +- lunaix-os/arch/i386/includes/sys/abi.h | 42 ++-- lunaix-os/arch/i386/includes/sys/apic.h | 4 +- lunaix-os/arch/i386/includes/sys/cpu.h | 18 +- lunaix-os/arch/i386/includes/sys/gdbstub.h | 44 ++++ lunaix-os/arch/i386/includes/sys/hart.h | 106 +++++++++ .../arch/i386/includes/sys/interrupt.S.inc | 10 +- lunaix-os/arch/i386/includes/sys/interrupts.h | 55 ----- lunaix-os/arch/i386/includes/sys/ioapic.h | 4 +- lunaix-os/arch/i386/includes/sys/mm/memory.h | 6 +- lunaix-os/arch/i386/includes/sys/mm/mempart.h | 32 ++- .../arch/i386/includes/sys/mm/physical.h | 1 - lunaix-os/arch/i386/includes/sys/pci_hba.h | 20 +- lunaix-os/arch/i386/includes/sys/vectors.h | 3 - lunaix-os/arch/i386/includes/sys/x86_isa.h | 29 +++ lunaix-os/arch/i386/klib/fast_crc.c | 24 +++ lunaix-os/arch/i386/klib/fast_str.c | 26 +++ lunaix-os/arch/i386/mm/fault.c | 4 +- lunaix-os/arch/i386/mm/gdt.c | 2 +- lunaix-os/arch/i386/syscall.S | 2 +- lunaix-os/arch/i386/trace.c | 49 +++++ lunaix-os/hal/ahci/ahci.c | 2 +- lunaix-os/hal/ahci/io_event.c | 8 +- lunaix-os/hal/char/uart/16550_pmio.c | 7 +- lunaix-os/hal/intc.c | 24 --- lunaix-os/hal/pci.c | 36 ++++ lunaix-os/hal/timer/timer_device.c | 2 +- lunaix-os/includes/hal/ahci/ahci.h | 4 +- lunaix-os/includes/hal/hwtimer.h | 2 +- lunaix-os/includes/hal/intc.h | 53 ----- lunaix-os/includes/hal/pci.h | 6 +- lunaix-os/includes/hal/rtc/mc146818a.h | 13 -- lunaix-os/includes/{lib => klibc}/crc.h | 1 + lunaix-os/includes/{lib => klibc}/hash.h | 0 lunaix-os/includes/klibc/ia_utils.h | 6 + lunaix-os/includes/klibc/stdlib.h | 11 - lunaix-os/includes/lunaix/ds/hashtable.h | 2 +- lunaix-os/includes/lunaix/ds/hstr.h | 2 +- .../includes/lunaix/{ => generic}/isrm.h | 25 ++- .../includes/lunaix/generic/trace_arch.h | 15 ++ .../lunaix/{pcontext.h => hart_state.h} | 21 +- lunaix-os/includes/lunaix/mm/fault.h | 4 +- lunaix-os/includes/lunaix/process.h | 10 +- lunaix-os/includes/lunaix/signal.h | 8 +- lunaix-os/includes/lunaix/timer.h | 2 +- lunaix-os/includes/lunaix/trace.h | 8 +- lunaix-os/includes/lunaix/types.h | 3 +- lunaix-os/includes/sdbg/gdbstub.h | 12 +- lunaix-os/includes/sdbg/lsdbg.h | 4 +- lunaix-os/kernel/block/blkpart_gpt.c | 2 +- lunaix-os/kernel/block/block.c | 2 +- lunaix-os/kernel/debug/failsafe.c | 16 +- lunaix-os/kernel/debug/gdbstub.c | 204 ++---------------- lunaix-os/kernel/debug/sdbg.c | 92 -------- lunaix-os/kernel/debug/trace.c | 49 +++-- lunaix-os/kernel/device/devdb.c | 2 +- lunaix-os/kernel/exe/exec.c | 6 +- lunaix-os/kernel/kinit.c | 8 +- lunaix-os/kernel/mm/fault.c | 12 +- lunaix-os/kernel/process/fork.c | 2 +- lunaix-os/kernel/process/sched.c | 11 +- lunaix-os/kernel/process/signal.c | 18 +- lunaix-os/kernel/process/thread.c | 18 +- lunaix-os/kernel/spike.c | 2 +- lunaix-os/kernel/time/timer.c | 2 +- lunaix-os/libs/crc.c | 7 +- lunaix-os/libs/hash.c | 5 +- lunaix-os/libs/klibc/{stdlib => }/itoa.c | 6 +- lunaix-os/libs/klibc/ksprintf.c | 2 +- lunaix-os/libs/klibc/string/mem.c | 28 ++- lunaix-os/libs/klibc/string/strchr.c | 2 +- lunaix-os/libs/klibc/string/strcmp.c | 3 +- lunaix-os/libs/klibc/string/strcpy.c | 5 +- lunaix-os/libs/klibc/string/strlen.c | 5 +- lunaix-os/libs/klibc/string/trim.c | 5 +- 110 files changed, 1543 insertions(+), 828 deletions(-) create mode 100644 lunaix-os/arch/README.md create mode 100644 lunaix-os/arch/generic/arch.c create mode 100644 lunaix-os/arch/generic/hart.c create mode 100644 lunaix-os/arch/generic/includes/sys/abi.h create mode 100644 lunaix-os/arch/generic/includes/sys/cpu.h create mode 100644 lunaix-os/arch/generic/includes/sys/failsafe.h create mode 100644 lunaix-os/arch/generic/includes/sys/gdbstub.h create mode 100644 lunaix-os/arch/generic/includes/sys/hart.h create mode 100644 lunaix-os/arch/generic/includes/sys/mm/memory.h create mode 100644 lunaix-os/arch/generic/includes/sys/mm/mempart.h create mode 100644 lunaix-os/arch/generic/includes/sys/mm/mm_defs.h create mode 100644 lunaix-os/arch/generic/includes/sys/mm/physical.h create mode 100644 lunaix-os/arch/generic/includes/sys/mm/tlb.h create mode 100644 lunaix-os/arch/generic/includes/sys/muldiv64.h create mode 100644 lunaix-os/arch/generic/includes/sys/pci_hba.h create mode 100644 lunaix-os/arch/generic/includes/sys/port_io.h create mode 100644 lunaix-os/arch/generic/includes/sys/trace.h create mode 100644 lunaix-os/arch/generic/mm/fault.c create mode 100644 lunaix-os/arch/generic/mm/mm.c delete mode 100644 lunaix-os/arch/i386/README create mode 100644 lunaix-os/arch/i386/gdbstub.c rename lunaix-os/{hal/timer => arch/i386/hal}/apic_timer.c (95%) rename lunaix-os/{includes => arch/i386}/hal/apic_timer.h (100%) rename lunaix-os/{hal/rtc => arch/i386/hal}/mc146818a.c (95%) delete mode 100644 lunaix-os/arch/i386/hal/pci_hba.c rename lunaix-os/{hal/char => arch/i386/hal}/ps2kbd.c (98%) rename lunaix-os/{hal/rng => arch/i386/hal}/rngx86.c (100%) delete mode 100644 lunaix-os/arch/i386/hal/x86_intc.c rename lunaix-os/arch/i386/{pcontext.c => hart.c} (65%) create mode 100644 lunaix-os/arch/i386/includes/sys/gdbstub.h create mode 100644 lunaix-os/arch/i386/includes/sys/hart.h delete mode 100644 lunaix-os/arch/i386/includes/sys/interrupts.h create mode 100644 lunaix-os/arch/i386/klib/fast_crc.c create mode 100644 lunaix-os/arch/i386/klib/fast_str.c create mode 100644 lunaix-os/arch/i386/trace.c delete mode 100644 lunaix-os/hal/intc.c delete mode 100644 lunaix-os/includes/hal/intc.h delete mode 100644 lunaix-os/includes/hal/rtc/mc146818a.h rename lunaix-os/includes/{lib => klibc}/crc.h (99%) rename lunaix-os/includes/{lib => klibc}/hash.h (100%) create mode 100644 lunaix-os/includes/klibc/ia_utils.h delete mode 100644 lunaix-os/includes/klibc/stdlib.h rename lunaix-os/includes/lunaix/{ => generic}/isrm.h (73%) create mode 100644 lunaix-os/includes/lunaix/generic/trace_arch.h rename lunaix-os/includes/lunaix/{pcontext.h => hart_state.h} (53%) delete mode 100644 lunaix-os/kernel/debug/sdbg.c rename lunaix-os/libs/klibc/{stdlib => }/itoa.c (94%) diff --git a/lunaix-os/arch/README.md b/lunaix-os/arch/README.md new file mode 100644 index 0000000..0a2ead5 --- /dev/null +++ b/lunaix-os/arch/README.md @@ -0,0 +1,57 @@ +# Porting Lunaix to Other ISAs + +This document briefly describe how to add support for other ISA + +## Adding Implementations + +Lunaix provide bunch of headers that **MUST** be implemented. + ++ Follow the structure and derive the template header files according to + `arch/generic/includes`. + ! You **MUST** copy the header file and add your own + declaration & other stuffs. + ! You **MUST NOT** remove or modify the data structure, + function signature or any declaration that already in the template header + files unless stated explicitly. + ! Read the comment carefully, it may contains implementation recommendation + which is vital to the overall correctness. + ++ Add implementation to function signature defined in header files under + `includes/lunaix/generic` + ++ Add implementation of syscall dispatching (Reference: `arhc/i386/syscall.S`) + ++ Add implementation of interrupt handler dispatching (Reference: + `arhc/i386/exceptions/intrhnds.S`) + ++ Add implementation of context switching, signal handling. (Reference: + `arhc/i386/exceptions/interrupt.S`) + **TODO: make this procedure more standalone** + +## Preparing the Flows + +When system boot up, Lunaix start the execution from `start_`, and then the +control flow will transfer to the `kinit.c::kernel_bootstrap`. A boot hand-over +context `struct boot_handoff*` must passed to the bootstrap procedure. + +Before jumping to `kernel_bootstrap`, one + ++ Must constructure a proper `struct boot_handoff` context. ++ Must remap the kernel to architecturally favoured address range. + **TODO: the algorithm for doing kernel remapping should be arch-agnostic** ++ Must perform any essential CPU statet initialization, for example, setting up + address translation scheme, enable/disable ISA extensions, probing CPU/SOC + features. + +## Alternative Implementation + +Most functions in `klibc` can be override by an architectural port. To achieve +this, one just need to add definition of such function. This allow port to +provide better performance on these library functions by exploiting the +architectural specific feature. + +All non-static function from these header can be overriden: + ++ `klibc/string.h` ++ `klibc/crc.h` ++ `klibc/hash.h` diff --git a/lunaix-os/arch/generic/arch.c b/lunaix-os/arch/generic/arch.c new file mode 100644 index 0000000..ac0f064 --- /dev/null +++ b/lunaix-os/arch/generic/arch.c @@ -0,0 +1,8 @@ +#include +#include + +struct hwtimer* +select_platform_timer() +{ + fail("unimplemented"); +} \ No newline at end of file diff --git a/lunaix-os/arch/generic/hart.c b/lunaix-os/arch/generic/hart.c new file mode 100644 index 0000000..9c74bca --- /dev/null +++ b/lunaix-os/arch/generic/hart.c @@ -0,0 +1,18 @@ +#include +#include +#include +#include + +bool +install_hart_transition(ptr_t vm_mnt, struct hart_transition* tctx) +{ + return false; +} + +void +hart_prepare_transition(struct hart_transition* tctx, + ptr_t kstack_tp, ptr_t ustack_pt, + ptr_t entry, bool to_user) +{ + fail("unimplemented"); +} \ No newline at end of file diff --git a/lunaix-os/arch/generic/includes/sys/abi.h b/lunaix-os/arch/generic/includes/sys/abi.h new file mode 100644 index 0000000..bef0f5d --- /dev/null +++ b/lunaix-os/arch/generic/includes/sys/abi.h @@ -0,0 +1,58 @@ +#ifndef __LUNAIX_ARCH_ABI_H +#define __LUNAIX_ARCH_ABI_H + +#include + +#define stack_alignment 0 + +#ifndef __ASM__ +#define align_stack(ptr) ((ptr) & stack_alignment) +#define store_retval(retval) (void) + +#define store_retval_to(th, retval) (void) + +static inline void must_inline noret +j_usr(ptr_t sp, ptr_t pc) +{ + +} + + +static inline void must_inline noret +switch_context() { + unreachable; +} + +#define push_arg1(stack_ptr, arg) (void) + +#define push_arg2(stack_ptr, arg1, arg2) \ + { } + +#define push_arg3(stack_ptr, arg1, arg2, arg3) \ + { } + +#define push_arg4(stack_ptr, arg1, arg2, arg3, arg4) \ + { } + + +static inline ptr_t must_inline +abi_get_callframe() +{ + return 0; +} + +static inline ptr_t +abi_get_retaddr() +{ + return 0; +} + +static inline ptr_t +abi_get_retaddrat(ptr_t fp) +{ + return 0; +} + +#endif +#endif /* __LUNAIX_ABI_H */ + diff --git a/lunaix-os/arch/generic/includes/sys/cpu.h b/lunaix-os/arch/generic/includes/sys/cpu.h new file mode 100644 index 0000000..952a3de --- /dev/null +++ b/lunaix-os/arch/generic/includes/sys/cpu.h @@ -0,0 +1,71 @@ +#ifndef __LUNAIX_CPU_H +#define __LUNAIX_CPU_H + +#include + +/** + * @brief Get processor ID string + * + * @param id_out + */ +void +cpu_get_id(char* id_out); + +void +cpu_trap_sched(); + +void +cpu_trap_panic(char* message); + + +/** + * @brief Load current processor state + * + * @return ptr_t + */ +ptr_t +cpu_ldstate(); + +/** + * @brief Load current processor config + * + * @return ptr_t + */ +ptr_t +cpu_ldconfig(); + +/** + * @brief Change current processor state + * + * @return ptr_t + */ +void +cpu_chconfig(ptr_t val); + +/** + * @brief Change current virtual memory space + * + * @return ptr_t + */ +void +cpu_chvmspace(ptr_t val); + +void +cpu_enable_interrupt(); + +void +cpu_disable_interrupt(); + +void +cpu_wait(); + +/** + * @brief Read exeception address + * + * @return ptr_t + */ +ptr_t +cpu_ldeaddr(); + + +#endif /* __LUNAIX_CPU_H */ diff --git a/lunaix-os/arch/generic/includes/sys/failsafe.h b/lunaix-os/arch/generic/includes/sys/failsafe.h new file mode 100644 index 0000000..26020cd --- /dev/null +++ b/lunaix-os/arch/generic/includes/sys/failsafe.h @@ -0,0 +1,23 @@ +#ifndef __LUNAIX_FAILSAFE_H +#define __LUNAIX_FAILSAFE_H + +#define STACK_SANITY 0xbeefc0de + +#ifndef __ASM__ + +#include + +static inline bool +check_bootstack_sanity() +{ + return true; +} + +static inline void must_inline noret +failsafe_diagnostic() { + unreachable; +} + +#endif + +#endif /* __LUNAIX_FAILSAFE_H */ diff --git a/lunaix-os/arch/generic/includes/sys/gdbstub.h b/lunaix-os/arch/generic/includes/sys/gdbstub.h new file mode 100644 index 0000000..70f0df9 --- /dev/null +++ b/lunaix-os/arch/generic/includes/sys/gdbstub.h @@ -0,0 +1,29 @@ +#ifndef __LUNAIX_ARCH_GDBSTUB_ARCH_H +#define __LUNAIX_ARCH_GDBSTUB_ARCH_H + +#include "sys/hart.h" + +enum GDB_REGISTER +{ + // TODO add your registers + GDB_CPU_NUM_REGISTERS +}; + +struct gdb_state; + +void +arch_gdbstub_setup_state(struct gdb_state* state, struct hart_state* hstate); + +void +arch_gdbstub_save_regs(struct gdb_state* state, struct hart_state* hstate); + +void +arch_gdbstub_restore_regs(struct gdb_state* state, struct hart_state* hstate); + +int +gdb_sys_continue(struct gdb_state* state); + +int +gdb_sys_step(struct gdb_state* state); + +#endif /* __LUNAIX_ARCH_GDBSTUB_ARCH_H */ \ No newline at end of file diff --git a/lunaix-os/arch/generic/includes/sys/hart.h b/lunaix-os/arch/generic/includes/sys/hart.h new file mode 100644 index 0000000..340a0ee --- /dev/null +++ b/lunaix-os/arch/generic/includes/sys/hart.h @@ -0,0 +1,54 @@ +#ifndef __LUNAIX_ARCH_HART_H +#define __LUNAIX_ARCH_HART_H + +#ifndef __ASM__ +#include +#include + +struct exec_param; + +struct regcontext +{ + +} compact; + +struct hart_state +{ + +} compact; + +struct exec_param +{ + struct hart_state* parent_state; +} compact; + +void +hart_flow_redirect(struct hart_state* state, ptr_t pc, ptr_t sp); + +ptr_t +hart_pc(struct hart_state* state); + +ptr_t +hart_sp(struct hart_state* state); + +bool +kernel_context(struct hart_state* hstate); + +ptr_t +hart_stack_frame(struct hart_state* hstate); + +int +hart_vector_stamp(struct hart_state* hstate); + +unsigned int +hart_ecause(struct hart_state* hstate); + +struct hart_state* +hart_parent_state(struct hart_state* hstate); + +void +hart_push_state(struct hart_state* p_hstate, struct hart_state* hstate); + +#endif + +#endif /* __LUNAIX_ARCH_HART_H */ diff --git a/lunaix-os/arch/generic/includes/sys/mm/memory.h b/lunaix-os/arch/generic/includes/sys/mm/memory.h new file mode 100644 index 0000000..2bb4d65 --- /dev/null +++ b/lunaix-os/arch/generic/includes/sys/mm/memory.h @@ -0,0 +1,14 @@ +#ifndef __LUNAIX_ARCH_MEMORY_H +#define __LUNAIX_ARCH_MEMORY_H + +#include +#include + +static inline pte_attr_t +translate_vmr_prot(unsigned int vmr_prot) +{ + return 0; +} + + +#endif /* __LUNAIX_ARCH_MEMORY_H */ diff --git a/lunaix-os/arch/generic/includes/sys/mm/mempart.h b/lunaix-os/arch/generic/includes/sys/mm/mempart.h new file mode 100644 index 0000000..28d7ba6 --- /dev/null +++ b/lunaix-os/arch/generic/includes/sys/mm/mempart.h @@ -0,0 +1,60 @@ +#ifndef __LUNAIX_ARCH_MEMPART_H +#define __LUNAIX_ARCH_MEMPART_H + +#define MEM_PAGE 0x1000UL +#define MEM_1M 0x100000UL +#define MEM_4M 0x400000UL +#define MEM_HUGE 0x400000UL +#define MEM_1G 0x40000000UL + +#define END_POINT(name) (name + name##_SIZE - 1) + +#define KSTACK_AREA 0 +#define KSTACK_AREA_SIZE 0 +#define KSTACK_AREA_END END_POINT(KSTACK_AREA) + +#define USR_EXEC 0 +#define USR_EXEC_SIZE 0 +#define USR_EXEC_END END_POINT(USR_EXEC) + +#define USR_MMAP 0 +#define USR_MMAP_SIZE 0 +#define USR_MMAP_END END_POINT(USR_MMAP) + +#define USR_STACK 0 +#define USR_STACK_SIZE 0 +#define USR_STACK_END END_POINT(USR_STACK) + +#define KERNEL_IMG 0 +#define KERNEL_IMG_SIZE 0 +#define KERNEL_IMG_END END_POINT(KERNEL_IMG) + +#define PG_MOUNT_1 0 +#define PG_MOUNT_1_SIZE 0 +#define PG_MOUNT_1_END END_POINT(PG_MOUNT_1) + +#define PG_MOUNT_2 0 +#define PG_MOUNT_2_SIZE 0 +#define PG_MOUNT_2_END END_POINT(PG_MOUNT_2) + +#define PG_MOUNT_3 0 +#define PG_MOUNT_3_SIZE 0 +#define PG_MOUNT_3_END END_POINT(PG_MOUNT_3) + +#define PG_MOUNT_4 0 +#define PG_MOUNT_4_SIZE 0 +#define PG_MOUNT_4_END END_POINT(PG_MOUNT_4) + +#define PG_MOUNT_VAR 0 +#define PG_MOUNT_VAR_SIZE 0 +#define PG_MOUNT_VAR_END END_POINT(PG_MOUNT_VAR) + +#define VMAP 0 +#define VMAP_SIZE 0 +#define VMAP_END END_POINT(VMAP) + +#define VMS_MOUNT_1 0 +#define VMS_MOUNT_1_SIZE 0 +#define VMS_MOUNT_1_END END_POINT(VMS_MOUNT_1) + +#endif diff --git a/lunaix-os/arch/generic/includes/sys/mm/mm_defs.h b/lunaix-os/arch/generic/includes/sys/mm/mm_defs.h new file mode 100644 index 0000000..79ce14c --- /dev/null +++ b/lunaix-os/arch/generic/includes/sys/mm/mm_defs.h @@ -0,0 +1,34 @@ +#ifndef __LUNAIX_MM_DEFS_H +#define __LUNAIX_MM_DEFS_H + + +#include "mempart.h" +#include "pagetable.h" + +#define KSTACK_PAGES 3 +#define KSTACK_SIZE (KSTACK_PAGES * MEM_PAGE) + +/* + Regardless architecture we need to draw the line very carefully, and must + take the size of VM into account. In general, we aims to achieve + "sufficiently large" of memory for kernel + + In terms of x86_32: + * #768~1022 PTEs of PD (0x00000000c0000000, ~1GiB) + + In light of upcomming x86_64 support (for Level 4&5 Paging): + * #510 entry of PML4 (0x0000ff0000000000, ~512GiB) + * #510 entry of PML5 (0x01fe000000000000, ~256TiB) +*/ +// Where the kernel getting re-mapped. +#define KERNEL_RESIDENT 0x0 + +// Pages reserved for kernel image +#define KEXEC_RSVD 16 + +#define kernel_addr(addr) (addr) + +#define to_kphysical(k_va) ((ptr_t)(k_va)) +#define to_kvirtual(k_pa) ((ptr_t)(k_pa)) + +#endif /* __LUNAIX_MM_DEFS_H */ diff --git a/lunaix-os/arch/generic/includes/sys/mm/physical.h b/lunaix-os/arch/generic/includes/sys/mm/physical.h new file mode 100644 index 0000000..2a2cc43 --- /dev/null +++ b/lunaix-os/arch/generic/includes/sys/mm/physical.h @@ -0,0 +1,14 @@ +#ifndef __LUNAIX_ARCH_PHYSICAL_H +#define __LUNAIX_ARCH_PHYSICAL_H + +#include +#include "mm_defs.h" + +#define PPLIST_STARTVM VMAP + +struct ppage_arch +{ + +}; + +#endif /* __LUNAIX_ARCH_PHYSICAL_H */ \ No newline at end of file diff --git a/lunaix-os/arch/generic/includes/sys/mm/tlb.h b/lunaix-os/arch/generic/includes/sys/mm/tlb.h new file mode 100644 index 0000000..49cc08b --- /dev/null +++ b/lunaix-os/arch/generic/includes/sys/mm/tlb.h @@ -0,0 +1,99 @@ +#ifndef __LUNAIX_ARCH_TLB_H +#define __LUNAIX_ARCH_TLB_H + +#include +#include +#include + +/** + * @brief Invalidate entries of all address spaces + * + * @param asid + * @param addr + * @param npages + */ +void +tlb_flush_range(ptr_t addr, unsigned int npages); + +/** + * @brief Invalidate entries of an address space identified + * by ASID + * + * @param asid + * @param addr + * @param npages + */ +void +tlb_flush_asid_range(unsigned int asid, ptr_t addr, unsigned int npages); + +/** + * @brief Invalidate an entry of kernel address spaces + * + * @param asid + * @param addr + * @param npages + */ +void +tlb_flush_kernel(ptr_t addr); + +/** + * @brief Invalidate entries of kernel address spaces + * + * @param asid + * @param addr + * @param npages + */ +void +tlb_flush_kernel_ranged(ptr_t addr, unsigned int npages); + +/** + * @brief Invalidate an entry within a process memory space + * + * @param asid + * @param addr + * @param npages + */ +void +tlb_flush_mm(struct proc_mm* mm, ptr_t addr); + +/** + * @brief Invalidate entries within a process memory space + * + * @param asid + * @param addr + * @param npages + */ +void +tlb_flush_mm_range(struct proc_mm* mm, ptr_t addr, unsigned int npages); + +/** + * @brief Invalidate an entry within a vm region + * + * @param asid + * @param addr + * @param npages + */ +void +tlb_flush_vmr(struct mm_region* vmr, ptr_t va); + +/** + * @brief Invalidate all entries within a vm region + * + * @param asid + * @param addr + * @param npages + */ +void +tlb_flush_vmr_all(struct mm_region* vmr); + +/** + * @brief Invalidate entries within a vm region + * + * @param asid + * @param addr + * @param npages + */ +void +tlb_flush_vmr_range(struct mm_region* vmr, ptr_t addr, unsigned int npages); + +#endif /* __LUNAIX_VMTLB_H */ diff --git a/lunaix-os/arch/generic/includes/sys/muldiv64.h b/lunaix-os/arch/generic/includes/sys/muldiv64.h new file mode 100644 index 0000000..2f4f1fd --- /dev/null +++ b/lunaix-os/arch/generic/includes/sys/muldiv64.h @@ -0,0 +1,15 @@ +#ifndef __LUNAIX_ARCH_MULDIV64_H +#define __LUNAIX_ARCH_MULDIV64_H + + +#include +#include + +u64_t +udiv64(u64_t n, unsigned int base); + +unsigned int +umod64(u64_t n, unsigned int base); + + +#endif /* __LUNAIX_ARCH_MULDIV64_H */ diff --git a/lunaix-os/arch/generic/includes/sys/pci_hba.h b/lunaix-os/arch/generic/includes/sys/pci_hba.h new file mode 100644 index 0000000..f3747bf --- /dev/null +++ b/lunaix-os/arch/generic/includes/sys/pci_hba.h @@ -0,0 +1,31 @@ +#ifndef __LUNAIX_ARCH_PCI_HBA_H +#define __LUNAIX_ARCH_PCI_HBA_H + +#include +#include + +#define PCI_MSI_BASE 0 + +static inline pci_reg_t +pci_read_cspace(ptr_t base, int offset) +{ + return 0; +} + +static inline void +pci_write_cspace(ptr_t base, int offset, pci_reg_t data) +{ + return; +} + +static inline u16_t +pci_config_msi_data(int vector) { + return vector; +} + +static inline ptr_t +pci_get_msi_base() { + return 0; +} + +#endif /* __LUNAIX_ARCH_PCI_HBA_H */ diff --git a/lunaix-os/arch/generic/includes/sys/port_io.h b/lunaix-os/arch/generic/includes/sys/port_io.h new file mode 100644 index 0000000..09f3adf --- /dev/null +++ b/lunaix-os/arch/generic/includes/sys/port_io.h @@ -0,0 +1,89 @@ +#ifndef __LUNAIX_ARCH_PORT_IO_H +#define __LUNAIX_ARCH_PORT_IO_H + +#include + +static inline u8_t +port_rdbyte(int port) +{ + return 0; +} + +static inline void +port_rdbytes(int port, void* addr, int cnt) +{ + return; +} + +static inline u16_t +port_rdword(int port) +{ + return 0; +} + +static inline void +port_rdwords(int port, void* addr, int cnt) +{ + asm volatile("cld\n" + "repne\n" + "insw" + : "=D"(addr), "=c"(cnt) + : "d"(port), "0"(addr), "1"(cnt) + : "memory", "cc"); +} + +static inline u32_t +port_rddword(int port) +{ + return 0; +} + +static inline void +port_rddwords(int port, void* addr, int cnt) +{ + return; +} + +static inline void +port_wrbyte(int port, u8_t data) +{ + return; +} + +static inline void +port_wrbytes(int port, const void* addr, int cnt) +{ + return; +} + +static inline void +port_wrword(int port, u16_t data) +{ + return; +} + +static inline void +port_wrwords(int port, const void* addr, int cnt) +{ + return; +} + +static inline void +port_wrdwords(int port, const void* addr, int cnt) +{ + return; +} + +static inline void +port_wrdword(int port, u32_t data) +{ + return; +} + +static inline void +port_delay(int counter) +{ + return; +} + +#endif /* __LUNAIX_ARCH_PORT_IO_H */ diff --git a/lunaix-os/arch/generic/includes/sys/trace.h b/lunaix-os/arch/generic/includes/sys/trace.h new file mode 100644 index 0000000..17a39d7 --- /dev/null +++ b/lunaix-os/arch/generic/includes/sys/trace.h @@ -0,0 +1,11 @@ +#ifndef __LUNAIX_ARCH_TRACE_H +#define __LUNAIX_ARCH_TRACE_H + +#include + +static inline bool +arch_valid_fp(ptr_t ptr) { + return false; +} + +#endif /* __LUNAIX_ARCH_TRACE_H */ diff --git a/lunaix-os/arch/generic/mm/fault.c b/lunaix-os/arch/generic/mm/fault.c new file mode 100644 index 0000000..dd45ee9 --- /dev/null +++ b/lunaix-os/arch/generic/mm/fault.c @@ -0,0 +1,7 @@ +#include + +bool +__arch_prepare_fault_context(struct fault_context* fault) +{ + return false; +} \ No newline at end of file diff --git a/lunaix-os/arch/generic/mm/mm.c b/lunaix-os/arch/generic/mm/mm.c new file mode 100644 index 0000000..7f361b0 --- /dev/null +++ b/lunaix-os/arch/generic/mm/mm.c @@ -0,0 +1,20 @@ +#include +#include + +struct leaflet* +dup_leaflet(struct leaflet* leaflet) +{ + fail("unimplemented"); +} + +void +pmm_arch_init_pool(struct pmem* memory) +{ + fail("unimplemented"); +} + +ptr_t +pmm_arch_init_remap(struct pmem* memory, struct boot_handoff* bctx) +{ + fail("unimplemented"); +} \ No newline at end of file diff --git a/lunaix-os/arch/i386/README b/lunaix-os/arch/i386/README deleted file mode 100644 index e87a8c0..0000000 --- a/lunaix-os/arch/i386/README +++ /dev/null @@ -1,32 +0,0 @@ -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/arch/i386/arch.c b/lunaix-os/arch/i386/arch.c index 3de802d..5ad6cd7 100644 --- a/lunaix-os/arch/i386/arch.c +++ b/lunaix-os/arch/i386/arch.c @@ -1,14 +1,12 @@ -#include -#include - -#include #include -#include +#include #include -#include -#include +#include "sys/i386_intr.h" +#include "sys/hart.h" + +#include "hal/apic_timer.h" void exception_init() @@ -19,7 +17,7 @@ exception_init() } extern void -syscall_hndlr(const isr_param* param); +syscall_hndlr(const struct hart_state* hstate); void arch_preinit() @@ -30,7 +28,7 @@ arch_preinit() } struct hwtimer* -hwtimer_choose() +select_platform_timer() { struct hwtimer* timer; diff --git a/lunaix-os/arch/i386/exceptions/i386_isrm.c b/lunaix-os/arch/i386/exceptions/i386_isrm.c index f1facf9..89ff616 100644 --- a/lunaix-os/arch/i386/exceptions/i386_isrm.c +++ b/lunaix-os/arch/i386/exceptions/i386_isrm.c @@ -1,7 +1,10 @@ -#include +#include #include +#include -#include +#include "sys/x86_isa.h" +#include "sys/ioapic.h" +#include "sys/apic.h" /* total: 256 ivs @@ -14,8 +17,10 @@ static char iv_bmp[(IV_EX_END - IV_BASE_END) / 8]; static isr_cb handlers[TOTAL_IV]; static ptr_t ivhand_payload[TOTAL_IV]; +static struct x86_intc arch_intc_ctx; + extern void -intr_routine_fallback(const isr_param* param); +intr_routine_fallback(const struct hart_state* state); void isrm_init() @@ -95,7 +100,7 @@ isrm_bindirq(int irq, isr_cb irq_handler) } // fixed, edge trigged, polarity=high - intc_irq_attach(irq, iv, 0, IRQ_DEFAULT); + isrm_irq_attach(irq, iv, 0, IRQ_DEFAULT); return iv; } @@ -121,9 +126,9 @@ isrm_get(int iv) } ptr_t -isrm_get_payload(const isr_param* param) +isrm_get_payload(const struct hart_state* state) { - int iv = param->execp->vector; + int iv = state->execp->vector; assert(iv < 256); return ivhand_payload[iv]; @@ -135,4 +140,35 @@ isrm_set_payload(int iv, ptr_t payload) assert(iv < 256); ivhand_payload[iv] = payload; -} \ No newline at end of file +} + +void +isrm_irq_attach(int irq, int iv, cpu_t dest, u32_t flags) +{ + arch_intc_ctx.irq_attach(&arch_intc_ctx, irq, iv, dest, flags); +} + +void +isrm_notify_eoi(cpu_t id, int iv) +{ + arch_intc_ctx.notify_eoi(&arch_intc_ctx, id, iv); +} + +void +isrm_notify_eos(cpu_t id) +{ + isrm_notify_eoi(id, LUNAIX_SCHED); +} + + +static void +__intc_init() +{ + apic_init(); + ioapic_init(); + + arch_intc_ctx.name = "i386_apic"; + arch_intc_ctx.irq_attach = ioapic_irq_remap; + arch_intc_ctx.notify_eoi = apic_on_eoi; +} +owloysius_fetch_init(__intc_init, on_earlyboot); \ No newline at end of file diff --git a/lunaix-os/arch/i386/exceptions/interrupt.S b/lunaix-os/arch/i386/exceptions/interrupt.S index e6656c9..8d82d10 100644 --- a/lunaix-os/arch/i386/exceptions/interrupt.S +++ b/lunaix-os/arch/i386/exceptions/interrupt.S @@ -1,5 +1,5 @@ #define __ASM__ -#include +#include #include #include @@ -120,7 +120,7 @@ # fxrstor (%eax) 1: - popl %eax # discard isr_param::depth + popl %eax # discard struct hart_state::depth popl %eax popl %ebx popl %ecx @@ -140,7 +140,7 @@ movl current_thread, %eax # nested intr: restore saved context - popl thread_intr_ctx(%eax) + popl thread_hstate(%eax) addl $8, %esp @@ -195,13 +195,13 @@ 由于这中间没有进行地址空间的交换,所以第二次跳转使用的是同一个内核栈,而之前默认tss.esp0的值是永远指向最顶部 这样一来就有可能会覆盖更早的上下文信息(比如嵌套的信号捕获函数) */ - movl thread_intr_ctx(%ebx), %ecx # __current->intr_ctx + movl thread_hstate(%ebx), %ecx # __current->hstate movl %ecx, (tss_esp0_off + _tss) jmp handle_signal 1: - movl thread_intr_ctx(%ebx), %eax + movl thread_hstate(%ebx), %eax jmp soft_iret .type handle_signal, @function @@ -210,13 +210,13 @@ # 注意1:任何对proc_sig的布局改动,都须及时的保证这里的一致性! # 注意2:handle_signal在调用之前,须确保proc_sig已经写入用户栈! # arg1 in %eax: addr of proc_sig structure in user stack - movl psig_saved_ictx(%eax), %ebx # %ebx = &proc_sig->saved_ictx + movl psig_saved_hstate(%eax), %ebx # %ebx = &proc_sig->saved_hstate pushl $UDATA_SEG pushl %eax # esp movl iexecp(%ebx), %ebx - pushl exeflags(%ebx) # proc_sig->saved_ictx->execp->eflags + pushl exeflags(%ebx) # proc_sig->saved_hstate->execp->eflags pushl $UCODE_SEG # cs pushl psig_sigact(%eax) # %eip = proc_sig->sigact diff --git a/lunaix-os/arch/i386/exceptions/interrupts.c b/lunaix-os/arch/i386/exceptions/interrupts.c index 8e6fa20..d7eced3 100644 --- a/lunaix-os/arch/i386/exceptions/interrupts.c +++ b/lunaix-os/arch/i386/exceptions/interrupts.c @@ -1,11 +1,11 @@ #include #include -#include -#include +#include +#include "sys/x86_isa.h" -#include +#include "sys/x86_isa.h" -#include +#include #include #include #include @@ -14,30 +14,30 @@ LOG_MODULE("INTR") static inline void -update_thread_context(isr_param* param) +update_thread_context(struct hart_state* state) { if (!current_thread) { return; } - isr_param* ppctx = current_thread->intr_ctx; - param->execp->saved_prev_ctx = ppctx; - current_thread->intr_ctx = param; + struct hart_state* parent = current_thread->hstate; + hart_push_state(parent, state); + current_thread->hstate = state; - if (ppctx) { - param->depth = ppctx->depth + 1; + if (parent) { + state->depth = parent->depth + 1; } } void -intr_handler(isr_param* param) +intr_handler(struct hart_state* state) { - update_thread_context(param); + update_thread_context(state); - volatile struct exec_param* execp = param->execp; + volatile struct exec_param* execp = state->execp; if (execp->vector <= 255) { isr_cb subscriber = isrm_get(execp->vector); - subscriber(param); + subscriber(state); goto done; } @@ -50,7 +50,7 @@ intr_handler(isr_param* param) done: if (execp->vector > IV_BASE_END) { - intc_notify_eoi(0, execp->vector); + isrm_notify_eoi(0, execp->vector); } return; diff --git a/lunaix-os/arch/i386/exceptions/intr_routines.c b/lunaix-os/arch/i386/exceptions/intr_routines.c index b93e679..ddd06a8 100644 --- a/lunaix-os/arch/i386/exceptions/intr_routines.c +++ b/lunaix-os/arch/i386/exceptions/intr_routines.c @@ -1,6 +1,6 @@ -#include +#include -#include +#include #include #include #include @@ -10,60 +10,60 @@ #include -#include +#include "sys/apic.h" #include LOG_MODULE("INTR") extern void -intr_routine_page_fault(const isr_param* param); +intr_routine_page_fault(const struct hart_state* state); extern u32_t debug_resv; void -__print_panic_msg(const char* msg, const isr_param* param) +__print_panic_msg(const char* msg, const struct hart_state* state) { ERROR("panic: %s", msg); failsafe_diagnostic(); } void -intr_routine_divide_zero(const isr_param* param) +intr_routine_divide_zero(const struct hart_state* state) { - __print_panic_msg("div zero", param); + __print_panic_msg("div zero", state); } void -intr_routine_general_protection(const isr_param* param) +intr_routine_general_protection(const struct hart_state* state) { - __print_panic_msg("general protection", param); + __print_panic_msg("general protection", state); } void -intr_routine_sys_panic(const isr_param* param) +intr_routine_sys_panic(const struct hart_state* state) { - __print_panic_msg((char*)(param->registers.edi), param); + __print_panic_msg((char*)(state->registers.edi), state); } void -intr_routine_fallback(const isr_param* param) +intr_routine_fallback(const struct hart_state* state) { - __print_panic_msg("unknown interrupt", param); + __print_panic_msg("unknown interrupt", state); } /** * @brief ISR for Spurious interrupt * - * @param isr_param passed by CPU + * @param struct hart_state passed by CPU */ void -intr_routine_apic_spi(const isr_param* param) +intr_routine_apic_spi(const struct hart_state* state) { // FUTURE: do nothing for now } void -intr_routine_apic_error(const isr_param* param) +intr_routine_apic_error(const struct hart_state* state) { u32_t error_reg = apic_read_reg(APIC_ESR); char buf[32]; @@ -73,7 +73,7 @@ intr_routine_apic_error(const isr_param* param) } void -intr_routine_sched(const isr_param* param) +intr_routine_sched(const struct hart_state* state) { schedule(); } diff --git a/lunaix-os/arch/i386/gdbstub.c b/lunaix-os/arch/i386/gdbstub.c new file mode 100644 index 0000000..78c3165 --- /dev/null +++ b/lunaix-os/arch/i386/gdbstub.c @@ -0,0 +1,77 @@ +#include +#include + +void +arch_gdbstub_setup_state(struct gdb_state* state, struct hart_state* hstate) +{ + /* Translate vector to signal */ + switch (hstate->execp->vector) { + case 1: + state->signum = 5; + break; + case 3: + state->signum = 5; + break; + default: + state->signum = 7; + } +} + +void +arch_gdbstub_save_regs(struct gdb_state* state, struct hart_state* hstate) +{ + /* Load Registers */ + state->registers[GDB_CPU_I386_REG_EAX] = hstate->registers.eax; + state->registers[GDB_CPU_I386_REG_ECX] = hstate->registers.ecx; + state->registers[GDB_CPU_I386_REG_EDX] = hstate->registers.edx; + state->registers[GDB_CPU_I386_REG_EBX] = hstate->registers.ebx; + state->registers[GDB_CPU_I386_REG_ESP] = hstate->esp; + state->registers[GDB_CPU_I386_REG_EBP] = hstate->registers.ebp; + state->registers[GDB_CPU_I386_REG_ESI] = hstate->registers.esi; + state->registers[GDB_CPU_I386_REG_EDI] = hstate->registers.edi; + state->registers[GDB_CPU_I386_REG_PC] = hstate->execp->eip; + state->registers[GDB_CPU_I386_REG_CS] = hstate->execp->cs; + state->registers[GDB_CPU_I386_REG_PS] = hstate->execp->eflags; + state->registers[GDB_CPU_I386_REG_SS] = hstate->execp->ss; + state->registers[GDB_CPU_I386_REG_DS] = hstate->registers.ds; + state->registers[GDB_CPU_I386_REG_ES] = hstate->registers.es; + state->registers[GDB_CPU_I386_REG_FS] = hstate->registers.fs; + state->registers[GDB_CPU_I386_REG_GS] = hstate->registers.gs; +} + +void +arch_gdbstub_restore_regs(struct gdb_state* state, struct hart_state* hstate) +{ + /* Restore Registers */ + hstate->registers.eax = state->registers[GDB_CPU_I386_REG_EAX]; + hstate->registers.ecx = state->registers[GDB_CPU_I386_REG_ECX]; + hstate->registers.edx = state->registers[GDB_CPU_I386_REG_EDX]; + hstate->registers.ebx = state->registers[GDB_CPU_I386_REG_EBX]; + hstate->esp = state->registers[GDB_CPU_I386_REG_ESP]; + hstate->registers.ebp = state->registers[GDB_CPU_I386_REG_EBP]; + hstate->registers.esi = state->registers[GDB_CPU_I386_REG_ESI]; + hstate->registers.edi = state->registers[GDB_CPU_I386_REG_EDI]; + hstate->execp->eip = state->registers[GDB_CPU_I386_REG_PC]; + hstate->execp->cs = state->registers[GDB_CPU_I386_REG_CS]; + hstate->execp->eflags = state->registers[GDB_CPU_I386_REG_PS]; + hstate->execp->ss = state->registers[GDB_CPU_I386_REG_SS]; + hstate->registers.ds = state->registers[GDB_CPU_I386_REG_DS]; + hstate->registers.es = state->registers[GDB_CPU_I386_REG_ES]; + hstate->registers.fs = state->registers[GDB_CPU_I386_REG_FS]; + hstate->registers.gs = state->registers[GDB_CPU_I386_REG_GS]; +} + + +int +gdb_sys_continue(struct gdb_state* state) +{ + state->registers[GDB_CPU_I386_REG_PS] &= ~(1 << 8); + return 0; +} + +int +gdb_sys_step(struct gdb_state* state) +{ + state->registers[GDB_CPU_I386_REG_PS] |= 1 << 8; + return 0; +} \ No newline at end of file diff --git a/lunaix-os/arch/i386/hal/apic.c b/lunaix-os/arch/i386/hal/apic.c index 458bf03..4e2501f 100644 --- a/lunaix-os/arch/i386/hal/apic.c +++ b/lunaix-os/arch/i386/hal/apic.c @@ -9,11 +9,11 @@ * */ -#include +#include "sys/x86_isa.h" #include -#include -#include +#include "sys/apic.h" +#include #include #include @@ -102,7 +102,7 @@ apic_setup_lvts() } void -apic_on_eoi(struct intc_context* intc_ctx, cpu_t cpu, int iv) +apic_on_eoi(struct x86_intc* intc_ctx, cpu_t cpu, int iv) { // for all external interrupts except the spurious interrupt // this is required by Intel Manual Vol.3A, section 10.8.1 & 10.8.5 diff --git a/lunaix-os/hal/timer/apic_timer.c b/lunaix-os/arch/i386/hal/apic_timer.c similarity index 95% rename from lunaix-os/hal/timer/apic_timer.c rename to lunaix-os/arch/i386/hal/apic_timer.c index 97f9744..9ea0be6 100644 --- a/lunaix-os/hal/timer/apic_timer.c +++ b/lunaix-os/arch/i386/hal/apic_timer.c @@ -1,13 +1,13 @@ -#include +#include "apic_timer.h" #include #include #include -#include +#include #include #include -#include +#include "sys/apic.h" LOG_MODULE("APIC_TIMER") @@ -23,13 +23,13 @@ static volatile ticks_t systicks = 0; static timer_tick_cb tick_cb = NULL; static void -temp_intr_routine_apic_timer(const isr_param* param) +temp_intr_routine_apic_timer(const struct hart_state* state) { apic_timer_done = 1; } static void -apic_timer_tick_isr(const isr_param* param) +apic_timer_tick_isr(const struct hart_state* state) { systicks++; diff --git a/lunaix-os/includes/hal/apic_timer.h b/lunaix-os/arch/i386/hal/apic_timer.h similarity index 100% rename from lunaix-os/includes/hal/apic_timer.h rename to lunaix-os/arch/i386/hal/apic_timer.h diff --git a/lunaix-os/arch/i386/hal/ioapic.c b/lunaix-os/arch/i386/hal/ioapic.c index 7e7800d..83ce6a0 100644 --- a/lunaix-os/arch/i386/hal/ioapic.c +++ b/lunaix-os/arch/i386/hal/ioapic.c @@ -1,10 +1,10 @@ #include -#include #include -#include -#include +#include +#include "sys/ioapic.h" +#include "sys/x86_isa.h" #define IOAPIC_IOREGSEL 0x00 #define IOAPIC_IOWIN 0x10 @@ -56,7 +56,7 @@ ioapic_read(u8_t sel) } void -ioapic_irq_remap(struct intc_context*, int irq, int iv, cpu_t dest, u32_t flags) +ioapic_irq_remap(struct x86_intc* intc, int irq, int iv, cpu_t dest, u32_t flags) { /* FIXME move it to HAL level. since every platform might have their own diff --git a/lunaix-os/hal/rtc/mc146818a.c b/lunaix-os/arch/i386/hal/mc146818a.c similarity index 95% rename from lunaix-os/hal/rtc/mc146818a.c rename to lunaix-os/arch/i386/hal/mc146818a.c index cb1ae33..96b95e1 100644 --- a/lunaix-os/hal/rtc/mc146818a.c +++ b/lunaix-os/arch/i386/hal/mc146818a.c @@ -10,12 +10,12 @@ * */ -#include +#include #include #include -#include +#include -#include +#include #include @@ -54,6 +54,8 @@ #define RTC_FREQUENCY_1024HZ 0b110 #define RTC_DIVIDER_33KHZ (0b010 << 4) +#define PC_AT_IRQ_RTC 8 + struct mc146818 { struct hwrtc* rtc_context; @@ -143,9 +145,9 @@ mc146818_check_support(struct hwrtc* rtc) } static void -__rtc_tick(const isr_param* param) +__rtc_tick(const struct hart_state* hstate) { - struct mc146818* state = (struct mc146818*)isrm_get_payload(param); + struct mc146818* state = (struct mc146818*)isrm_get_payload(hstate); state->tick_counts++; diff --git a/lunaix-os/arch/i386/hal/pci_hba.c b/lunaix-os/arch/i386/hal/pci_hba.c deleted file mode 100644 index 60e3db8..0000000 --- a/lunaix-os/arch/i386/hal/pci_hba.c +++ /dev/null @@ -1,32 +0,0 @@ -#include -#include - -void -pci_setup_msi(struct pci_device* device, int vector) -{ - // Dest: APIC#0, Physical Destination, No redirection - u32_t msi_addr = (__APIC_BASE_PADDR); - - // Edge trigger, Fixed delivery - u32_t msi_data = vector; - - pci_write_cspace( - device->cspace_base, PCI_MSI_ADDR(device->msi_loc), msi_addr); - - pci_reg_t reg1 = pci_read_cspace(device->cspace_base, device->msi_loc); - pci_reg_t msg_ctl = reg1 >> 16; - - int offset = !!(msg_ctl & MSI_CAP_64BIT) * 4; - pci_write_cspace(device->cspace_base, - PCI_MSI_DATA(device->msi_loc, offset), - msi_data & 0xffff); - - if ((msg_ctl & MSI_CAP_MASK)) { - pci_write_cspace( - device->cspace_base, PCI_MSI_MASK(device->msi_loc, offset), 0); - } - - // manipulate the MSI_CTRL to allow device using MSI to request service. - reg1 = (reg1 & 0xff8fffff) | 0x10000; - pci_write_cspace(device->cspace_base, device->msi_loc, reg1); -} \ No newline at end of file diff --git a/lunaix-os/hal/char/ps2kbd.c b/lunaix-os/arch/i386/hal/ps2kbd.c similarity index 98% rename from lunaix-os/hal/char/ps2kbd.c rename to lunaix-os/arch/i386/hal/ps2kbd.c index 7b14048..f0d3ba4 100644 --- a/lunaix-os/hal/char/ps2kbd.c +++ b/lunaix-os/arch/i386/hal/ps2kbd.c @@ -1,13 +1,13 @@ #include #include #include -#include +#include #include #include #include -#include +#include -#include +#include "sys/x86_isa.h" #include @@ -56,6 +56,8 @@ #define PS2_NO_ARG 0xff00 +#define PC_AT_IRQ_KBD 1 + struct ps2_cmd { char cmd; @@ -187,7 +189,7 @@ static struct input_device* kbd_idev; // #define KBD_DBGLOG static void -intr_ps2_kbd_handler(const isr_param* param); +intr_ps2_kbd_handler(const struct hart_state* hstate); static u8_t ps2_issue_cmd_wretry(char cmd, u16_t arg); @@ -399,7 +401,7 @@ kbd_buffer_key_event(kbd_keycode_t key, u8_t scancode, kbd_kstate_t state) } static void -intr_ps2_kbd_handler(const isr_param* param) +intr_ps2_kbd_handler(const struct hart_state* hstate) { // This is important! Don't believe me? try comment it out and run on Bochs! diff --git a/lunaix-os/hal/rng/rngx86.c b/lunaix-os/arch/i386/hal/rngx86.c similarity index 100% rename from lunaix-os/hal/rng/rngx86.c rename to lunaix-os/arch/i386/hal/rngx86.c diff --git a/lunaix-os/arch/i386/hal/x86_intc.c b/lunaix-os/arch/i386/hal/x86_intc.c deleted file mode 100644 index 948eeca..0000000 --- a/lunaix-os/arch/i386/hal/x86_intc.c +++ /dev/null @@ -1,16 +0,0 @@ -#include -#include -#include - -extern struct intc_context arch_intc_ctx; - -void -intc_init() -{ - apic_init(); - ioapic_init(); - - arch_intc_ctx.name = "i386_apic"; - arch_intc_ctx.irq_attach = ioapic_irq_remap; - arch_intc_ctx.notify_eoi = apic_on_eoi; -} diff --git a/lunaix-os/arch/i386/pcontext.c b/lunaix-os/arch/i386/hart.c similarity index 65% rename from lunaix-os/arch/i386/pcontext.c rename to lunaix-os/arch/i386/hart.c index 6b56b41..09bd2b3 100644 --- a/lunaix-os/arch/i386/pcontext.c +++ b/lunaix-os/arch/i386/hart.c @@ -1,5 +1,5 @@ #include -#include +#include #include #include @@ -11,38 +11,38 @@ volatile struct x86_tss _tss = { .link = 0, .ss0 = KDATA_SEG }; bool -inject_transfer_context(ptr_t vm_mnt, struct transfer_context* tctx) +install_hart_transition(ptr_t vm_mnt, struct hart_transition* ht) { pte_t pte; - if (!vmm_lookupat(vm_mnt, tctx->inject, &pte)) { + if (!vmm_lookupat(vm_mnt, ht->inject, &pte)) { return false; } mount_page(PG_MOUNT_4, pte_paddr(pte)); - ptr_t mount_inject = PG_MOUNT_4 + va_offset(tctx->inject); - memcpy((void*)mount_inject, &tctx->transfer, sizeof(tctx->transfer)); + ptr_t mount_inject = PG_MOUNT_4 + va_offset(ht->inject); + memcpy((void*)mount_inject, &ht->transfer, sizeof(ht->transfer)); unmount_page(PG_MOUNT_4); return true; } void -thread_setup_trasnfer(struct transfer_context* tctx, +hart_prepare_transition(struct hart_transition* ht, ptr_t kstack_tp, ptr_t ustack_pt, ptr_t entry, bool to_user) { - ptr_t offset = (ptr_t)&tctx->transfer.eret - (ptr_t)&tctx->transfer; - tctx->inject = align_stack(kstack_tp - sizeof(tctx->transfer)); + ptr_t offset = (ptr_t)&ht->transfer.eret - (ptr_t)&ht->transfer; + ht->inject = align_stack(kstack_tp - sizeof(ht->transfer)); - tctx->transfer.isr = (isr_param){ + ht->transfer.state = (struct hart_state){ .registers = { .ds = KDATA_SEG, .es = KDATA_SEG, .fs = KDATA_SEG, .gs = KDATA_SEG }, - .execp = (struct exec_param*)(tctx->inject + offset) + .execp = (struct exec_param*)(ht->inject + offset) }; int code_seg = KCODE_SEG, data_seg = KDATA_SEG; @@ -52,7 +52,7 @@ thread_setup_trasnfer(struct transfer_context* tctx, mstate |= 0x200; // enable interrupt } - tctx->transfer.eret = (struct exec_param) { + ht->transfer.eret = (struct exec_param) { .cs = code_seg, .eip = entry, .ss = data_seg, .esp = align_stack(ustack_pt), .eflags = mstate diff --git a/lunaix-os/arch/i386/includes/sys/abi.h b/lunaix-os/arch/i386/includes/sys/abi.h index 05c2897..d847251 100644 --- a/lunaix-os/arch/i386/includes/sys/abi.h +++ b/lunaix-os/arch/i386/includes/sys/abi.h @@ -1,36 +1,34 @@ #ifndef __LUNAIX_I386ABI_H #define __LUNAIX_I386ABI_H -#include +#include "sys/x86_isa.h" #define stack_alignment 0xfffffff0 #ifndef __ASM__ #define align_stack(ptr) ((ptr) & stack_alignment) -#define store_retval(retval) current_thread->intr_ctx->registers.eax = (retval) +#define store_retval(retval) current_thread->hstate->registers.eax = (retval) -#define store_retval_to(th, retval) (th)->intr_ctx->registers.eax = (retval) +#define store_retval_to(th, retval) (th)->hstate->registers.eax = (retval) -#define eret_target(th) (th)->intr_ctx->execp->eip -#define eret_stack(th) (th)->intr_ctx->execp->esp -#define intr_ivec(th) (th)->intr_ctx->execp->vector -#define intr_ierr(th) (th)->intr_ctx->execp->err_code - -#define j_usr(sp, pc) \ - asm volatile("movw %0, %%ax\n" \ - "movw %%ax, %%es\n" \ - "movw %%ax, %%ds\n" \ - "movw %%ax, %%fs\n" \ - "movw %%ax, %%gs\n" \ - "pushl %0\n" \ - "pushl %1\n" \ - "pushl %2\n" \ - "pushl %3\n" \ - "retf" ::"i"(UDATA_SEG), \ - "r"(sp), \ - "i"(UCODE_SEG), \ - "r"(pc) \ +static inline void must_inline +j_usr(ptr_t sp, ptr_t pc) +{ + asm volatile("movw %0, %%ax\n" + "movw %%ax, %%es\n" + "movw %%ax, %%ds\n" + "movw %%ax, %%fs\n" + "movw %%ax, %%gs\n" + "pushl %0\n" + "pushl %1\n" + "pushl %2\n" + "pushl %3\n" + "retf" ::"i"(UDATA_SEG), + "r"(sp), + "i"(UCODE_SEG), + "r"(pc) : "eax", "memory"); +} static inline void must_inline noret diff --git a/lunaix-os/arch/i386/includes/sys/apic.h b/lunaix-os/arch/i386/includes/sys/apic.h index 5583190..a74049b 100644 --- a/lunaix-os/arch/i386/includes/sys/apic.h +++ b/lunaix-os/arch/i386/includes/sys/apic.h @@ -1,7 +1,7 @@ #ifndef __LUNAIX_APIC_H #define __LUNAIX_APIC_H -#include +#include "sys/x86_isa.h" #include #define __APIC_BASE_PADDR 0xFEE00000 @@ -76,6 +76,6 @@ void apic_init(); void -apic_on_eoi(struct intc_context* intc_ctx, cpu_t cpu, int iv); +apic_on_eoi(struct x86_intc* intc_ctx, cpu_t cpu, int iv); #endif /* __LUNAIX_APIC_H */ diff --git a/lunaix-os/arch/i386/includes/sys/cpu.h b/lunaix-os/arch/i386/includes/sys/cpu.h index d8b8a23..ca7080a 100644 --- a/lunaix-os/arch/i386/includes/sys/cpu.h +++ b/lunaix-os/arch/i386/includes/sys/cpu.h @@ -21,9 +21,9 @@ cpu_trap_panic(char* message); /** * @brief Load current processor state * - * @return u32_t + * @return reg_t */ -static inline u32_t +static inline reg_t cpu_ldstate() { ptr_t val; @@ -36,12 +36,12 @@ cpu_ldstate() /** * @brief Load current processor config * - * @return u32_t + * @return reg_t */ -static inline u32_t +static inline reg_t cpu_ldconfig() { - ptr_t val; + reg_t val; asm volatile("movl %%cr0,%0" : "=r"(val)); return val; } @@ -49,10 +49,10 @@ cpu_ldconfig() /** * @brief Change current processor state * - * @return u32_t + * @return reg_t */ static inline void -cpu_chconfig(u32_t val) +cpu_chconfig(reg_t val) { asm("mov %0, %%cr0" ::"r"(val)); } @@ -60,10 +60,10 @@ cpu_chconfig(u32_t val) /** * @brief Change current virtual memory space * - * @return u32_t + * @return reg_t */ static inline void -cpu_chvmspace(u32_t val) +cpu_chvmspace(reg_t val) { asm("mov %0, %%cr3" ::"r"(val)); } diff --git a/lunaix-os/arch/i386/includes/sys/gdbstub.h b/lunaix-os/arch/i386/includes/sys/gdbstub.h new file mode 100644 index 0000000..8d98cef --- /dev/null +++ b/lunaix-os/arch/i386/includes/sys/gdbstub.h @@ -0,0 +1,44 @@ +#ifndef __LUNAIX_ARCH_GDBSTUB_ARCH_H +#define __LUNAIX_ARCH_GDBSTUB_ARCH_H + +#include "sys/hart.h" + +enum GDB_REGISTER +{ + GDB_CPU_I386_REG_EAX = 0, + GDB_CPU_I386_REG_ECX = 1, + GDB_CPU_I386_REG_EDX = 2, + GDB_CPU_I386_REG_EBX = 3, + GDB_CPU_I386_REG_ESP = 4, + GDB_CPU_I386_REG_EBP = 5, + GDB_CPU_I386_REG_ESI = 6, + GDB_CPU_I386_REG_EDI = 7, + GDB_CPU_I386_REG_PC = 8, + GDB_CPU_I386_REG_PS = 9, + GDB_CPU_I386_REG_CS = 10, + GDB_CPU_I386_REG_SS = 11, + GDB_CPU_I386_REG_DS = 12, + GDB_CPU_I386_REG_ES = 13, + GDB_CPU_I386_REG_FS = 14, + GDB_CPU_I386_REG_GS = 15, + GDB_CPU_NUM_REGISTERS +}; + +struct gdb_state; + +void +arch_gdbstub_setup_state(struct gdb_state* state, struct hart_state* hstate); + +void +arch_gdbstub_save_regs(struct gdb_state* state, struct hart_state* hstate); + +void +arch_gdbstub_restore_regs(struct gdb_state* state, struct hart_state* hstate); + +int +gdb_sys_continue(struct gdb_state* state); + +int +gdb_sys_step(struct gdb_state* state); + +#endif /* __LUNAIX_ARCH_GDBSTUB_ARCH_H */ diff --git a/lunaix-os/arch/i386/includes/sys/hart.h b/lunaix-os/arch/i386/includes/sys/hart.h new file mode 100644 index 0000000..9a6381a --- /dev/null +++ b/lunaix-os/arch/i386/includes/sys/hart.h @@ -0,0 +1,106 @@ +#ifndef __LUNAIX_ARCH_HART_H +#define __LUNAIX_ARCH_HART_H + +#include "vectors.h" + +#ifndef __ASM__ +#include +#include + +struct exec_param; + +struct regcontext +{ + reg_t eax; + reg_t ebx; + reg_t ecx; + reg_t edx; + reg_t edi; + reg_t ebp; + reg_t esi; + reg_t ds; + reg_t es; + reg_t fs; + reg_t gs; +} compact; + +struct hart_state +{ + unsigned int depth; + struct regcontext registers; + union + { + reg_t esp; + volatile struct exec_param* execp; + }; +} compact; + +struct exec_param +{ + struct hart_state* parent_state; + reg_t vector; + reg_t err_code; + reg_t eip; + reg_t cs; + reg_t eflags; + reg_t esp; + reg_t ss; +} compact; + + +static inline void +hart_flow_redirect(struct hart_state* hstate, ptr_t pc, ptr_t sp) +{ + hstate->execp->eip = pc; + hstate->execp->esp = sp; +} + +static inline ptr_t +hart_pc(struct hart_state* hstate) +{ + return hstate->execp->eip; +} + +static inline ptr_t +hart_sp(struct hart_state* hstate) +{ + return hstate->execp->esp; +} + +static inline bool +kernel_context(struct hart_state* hstate) +{ + return !((hstate->execp->cs) & 0b11); +} + +static inline ptr_t +hart_stack_frame(struct hart_state* hstate) +{ + return hstate->registers.ebp; +} + +static inline int +hart_vector_stamp(struct hart_state* hstate) { + return hstate->execp->vector; +} + +static inline unsigned int +hart_ecause(struct hart_state* hstate) { + return hstate->execp->err_code; +} + +static inline struct hart_state* +hart_parent_state(struct hart_state* hstate) +{ + return hstate->execp->parent_state; +} + +static inline void +hart_push_state(struct hart_state* p_hstate, struct hart_state* hstate) +{ + hstate->execp->parent_state = p_hstate; +} + +#endif + +#endif /* __LUNAIX_ARCH_HART_H */ diff --git a/lunaix-os/arch/i386/includes/sys/interrupt.S.inc b/lunaix-os/arch/i386/includes/sys/interrupt.S.inc index 055439e..1d698fa 100644 --- a/lunaix-os/arch/i386/includes/sys/interrupt.S.inc +++ b/lunaix-os/arch/i386/includes/sys/interrupt.S.inc @@ -30,8 +30,8 @@ igs: iexecp: iesp: .struct iesp + regsize -isave_prev: - .struct isave_prev + regsize +isave_parent: + .struct isave_parent + regsize ivec: .struct ivec + regsize iecode: @@ -67,8 +67,8 @@ exuss: /* struct layout: critical section of struct proc_info */ .struct 0 -thread_intr_ctx: - .struct thread_intr_ctx + regsize +thread_hstate: + .struct thread_hstate + regsize thread_ustack_top: /* struct layout: proc_sig */ @@ -79,4 +79,4 @@ psig_sigact: .struct psig_sigact + regsize psig_sighand: .struct psig_sighand + regsize -psig_saved_ictx: \ No newline at end of file +psig_saved_hstate: \ No newline at end of file diff --git a/lunaix-os/arch/i386/includes/sys/interrupts.h b/lunaix-os/arch/i386/includes/sys/interrupts.h deleted file mode 100644 index 540da4c..0000000 --- a/lunaix-os/arch/i386/includes/sys/interrupts.h +++ /dev/null @@ -1,55 +0,0 @@ -#ifndef __LUNAIX_INTERRUPTS_H -#define __LUNAIX_INTERRUPTS_H - -#include "vectors.h" - -#ifndef __ASM__ -#include -#include - -struct exec_param; - -struct regcontext -{ - u32_t eax; - u32_t ebx; - u32_t ecx; - u32_t edx; - u32_t edi; - u32_t ebp; - u32_t esi; - u32_t ds; - u32_t es; - u32_t fs; - u32_t gs; -} compact; - -struct pcontext -{ - unsigned int depth; - struct regcontext registers; - union - { - u32_t esp; - volatile struct exec_param* execp; - }; -} compact; - -struct exec_param -{ - struct pcontext* saved_prev_ctx; - u32_t vector; - u32_t err_code; - u32_t eip; - u32_t cs; - u32_t eflags; - u32_t esp; - u32_t ss; -} compact; - -#define saved_fp(isrm) ((isrm)->registers.ebp) -#define kernel_context(isrm) (!(((isrm)->execp->cs) & 0b11)) - -#endif - -#endif /* __LUNAIX_INTERRUPTS_H */ diff --git a/lunaix-os/arch/i386/includes/sys/ioapic.h b/lunaix-os/arch/i386/includes/sys/ioapic.h index 0c97528..46916d9 100644 --- a/lunaix-os/arch/i386/includes/sys/ioapic.h +++ b/lunaix-os/arch/i386/includes/sys/ioapic.h @@ -1,13 +1,13 @@ #ifndef __LUNAIX_IOAPIC_H #define __LUNAIX_IOAPIC_H -#include +#include "sys/x86_isa.h" void ioapic_init(); void -ioapic_irq_remap(struct intc_context*, +ioapic_irq_remap(struct x86_intc*, int irq, int iv, cpu_t dest, diff --git a/lunaix-os/arch/i386/includes/sys/mm/memory.h b/lunaix-os/arch/i386/includes/sys/mm/memory.h index 62bab8d..a89d90a 100644 --- a/lunaix-os/arch/i386/includes/sys/mm/memory.h +++ b/lunaix-os/arch/i386/includes/sys/mm/memory.h @@ -1,5 +1,5 @@ -#ifndef __LUNAIX_MEMORY_H -#define __LUNAIX_MEMORY_H +#ifndef __LUNAIX_ARCH_MEMORY_H +#define __LUNAIX_ARCH_MEMORY_H #include #include @@ -24,4 +24,4 @@ translate_vmr_prot(unsigned int vmr_prot) } -#endif /* __LUNAIX_MEMORY_H */ +#endif /* __LUNAIX_ARCH_MEMORY_H */ diff --git a/lunaix-os/arch/i386/includes/sys/mm/mempart.h b/lunaix-os/arch/i386/includes/sys/mm/mempart.h index b604265..aac7f9c 100644 --- a/lunaix-os/arch/i386/includes/sys/mm/mempart.h +++ b/lunaix-os/arch/i386/includes/sys/mm/mempart.h @@ -1,7 +1,5 @@ #ifndef __LUNAIX_MEMPART_H #define __LUNAIX_MEMPART_H -/* Physical Adress Space Partition */ -/* Generated from mempart.h.j2. Do NOT modify */ #define MEM_PAGE 0x1000UL #define MEM_1M 0x100000UL @@ -9,56 +7,54 @@ #define MEM_HUGE 0x400000UL #define MEM_1G 0x40000000UL +#define END_POINT(name) (name + name##_SIZE - 1) + #define KSTACK_AREA 0x100000UL #define KSTACK_AREA_SIZE 0x300000UL -#define KSTACK_AREA_END 0x3ffff0UL +#define KSTACK_AREA_END END_POINT(KSTACK_AREA) #define USR_EXEC 0x400000UL #define USR_EXEC_SIZE 0x20000000UL -#define USR_EXEC_END 0x203fffffUL +#define USR_EXEC_END END_POINT(USR_EXEC) #define USR_MMAP 0x20400000UL #define USR_MMAP_SIZE 0x9fbc0000UL -#define USR_MMAP_END 0xbffbffffUL +#define USR_MMAP_END END_POINT(USR_MMAP) #define USR_STACK 0xbffc0000UL #define USR_STACK_SIZE 0x40000UL -#define USR_STACK_END 0xbffffff0UL +#define USR_STACK_END END_POINT(USR_STACK) #define KERNEL_IMG 0xc0000000UL #define KERNEL_IMG_SIZE 0x4000000UL -#define KERNEL_IMG_END 0xc3ffffffUL +#define KERNEL_IMG_END END_POINT(KERNEL_IMG) #define PG_MOUNT_1 0xc4000000UL #define PG_MOUNT_1_SIZE 0x1000UL -#define PG_MOUNT_1_END 0xc4000fffUL +#define PG_MOUNT_1_END END_POINT(PG_MOUNT_1) #define PG_MOUNT_2 0xc4001000UL #define PG_MOUNT_2_SIZE 0x1000UL -#define PG_MOUNT_2_END 0xc4001fffUL +#define PG_MOUNT_2_END END_POINT(PG_MOUNT_2) #define PG_MOUNT_3 0xc4002000UL #define PG_MOUNT_3_SIZE 0x1000UL -#define PG_MOUNT_3_END 0xc4002fffUL +#define PG_MOUNT_3_END END_POINT(PG_MOUNT_3) #define PG_MOUNT_4 0xc4003000UL #define PG_MOUNT_4_SIZE 0x1000UL -#define PG_MOUNT_4_END 0xc4003fffUL +#define PG_MOUNT_4_END END_POINT(PG_MOUNT_4) #define PG_MOUNT_VAR 0xc4004000UL #define PG_MOUNT_VAR_SIZE 0x3fc000UL -#define PG_MOUNT_VAR_END 0xc43fffffUL +#define PG_MOUNT_VAR_END END_POINT(PG_MOUNT_VAR) #define VMAP 0xc4400000UL #define VMAP_SIZE 0x3b400000UL -#define VMAP_END 0xff7fffffUL +#define VMAP_END END_POINT(VMAP) #define VMS_MOUNT_1 0xff800000UL #define VMS_MOUNT_1_SIZE 0x400000UL -#define VMS_MOUNT_1_END 0xffbfffffUL - -#define PD_REF 0xffc00000UL -#define PD_REF_SIZE 0x400000UL -#define PD_REF_END 0xffffffffUL +#define VMS_MOUNT_1_END END_POINT(VMS_MOUNT_1) #endif \ No newline at end of file diff --git a/lunaix-os/arch/i386/includes/sys/mm/physical.h b/lunaix-os/arch/i386/includes/sys/mm/physical.h index 88f59ad..a6528bd 100644 --- a/lunaix-os/arch/i386/includes/sys/mm/physical.h +++ b/lunaix-os/arch/i386/includes/sys/mm/physical.h @@ -4,7 +4,6 @@ #include #include "mm_defs.h" -#define MAX_GROUP_PAGE_SIZE ( 0x8000 ) #define PPLIST_STARTVM VMAP struct ppage_arch diff --git a/lunaix-os/arch/i386/includes/sys/pci_hba.h b/lunaix-os/arch/i386/includes/sys/pci_hba.h index 0546dca..74d9c03 100644 --- a/lunaix-os/arch/i386/includes/sys/pci_hba.h +++ b/lunaix-os/arch/i386/includes/sys/pci_hba.h @@ -23,15 +23,15 @@ pci_write_cspace(ptr_t base, int offset, pci_reg_t data) port_wrdword(PCI_CONFIG_DATA, data); } -/** - * @brief 配置并启用设备MSI支持。 - * 参阅:PCI LB Spec. (Rev 3) Section 6.8 & 6.8.1 - * 以及:Intel Manual, Vol 3, Section 10.11 - * - * @param device PCI device - * @param vector interrupt vector. - */ -void -pci_setup_msi(struct pci_device* device, int vector); +static inline u16_t +pci_config_msi_data(int vector) { + return vector; +} + +static inline ptr_t +pci_get_msi_base() { + return 0xFEE00000; +} + #endif /* __LUNAIX_PCI_HBA_H */ diff --git a/lunaix-os/arch/i386/includes/sys/vectors.h b/lunaix-os/arch/i386/includes/sys/vectors.h index f352148..c055b46 100644 --- a/lunaix-os/arch/i386/includes/sys/vectors.h +++ b/lunaix-os/arch/i386/includes/sys/vectors.h @@ -48,9 +48,6 @@ #define APIC_SPIV_IV 252 #define APIC_TIMER_IV 253 -#define PC_AT_IRQ_RTC 8 -#define PC_AT_IRQ_KBD 1 - // clang-format on #endif /* __LUNAIX_VECTORS_H */ diff --git a/lunaix-os/arch/i386/includes/sys/x86_isa.h b/lunaix-os/arch/i386/includes/sys/x86_isa.h index 0c53d9d..7db8807 100644 --- a/lunaix-os/arch/i386/includes/sys/x86_isa.h +++ b/lunaix-os/arch/i386/includes/sys/x86_isa.h @@ -11,6 +11,20 @@ #ifndef __ASM__ #include + +#define IRQ_TRIG_EDGE 0b0 +#define IRQ_TRIG_LEVEL 0b1 + +#define IRQ_TYPE_FIXED (0b01 << 1) +#define IRQ_TYPE_NMI (0b11 << 1) +#define IRQ_TYPE (0b11 << 1) + +#define IRQ_VE_HI (0b1 << 3) +#define IRQ_VE_LO (0b0 << 3) + +#define IRQ_DEFAULT (IRQ_TRIG_EDGE | IRQ_TYPE_FIXED | IRQ_VE_HI) + + struct x86_tss { u32_t link; @@ -20,6 +34,21 @@ struct x86_tss } __attribute__((packed)); void tss_update_esp(u32_t esp0); + +struct x86_intc +{ + char* name; + void* data; + + void (*irq_attach)(struct x86_intc*, + int irq, + int iv, + cpu_t dest, + u32_t flags); + void (*notify_eoi)(struct x86_intc*, cpu_t id, int iv); +}; + + #endif #endif /* __LUNAIX_I386_ASM_H */ diff --git a/lunaix-os/arch/i386/klib/fast_crc.c b/lunaix-os/arch/i386/klib/fast_crc.c new file mode 100644 index 0000000..2384390 --- /dev/null +++ b/lunaix-os/arch/i386/klib/fast_crc.c @@ -0,0 +1,24 @@ +#include +#include + +#ifdef CONFIG_X86_SSE4 +unsigned int +crc32b(unsigned char* data, unsigned int size) +{ + unsigned int ret; + asm volatile( + "xorl %%ebx, %%ebx\n" + "xorl %%eax, %%eax\n" + "1:\n" + "crc32 (%%edx, %%ebx, 1), %%eax\n" + "incl %%ebx\n" + "cmpl %%ebx, %%ecx\n" + "jne 1b\n" + : "=a"(ret) + : "d"((ptr_t)data), + "c"(size) + : + ); + return ret; +} +#endif \ No newline at end of file diff --git a/lunaix-os/arch/i386/klib/fast_str.c b/lunaix-os/arch/i386/klib/fast_str.c new file mode 100644 index 0000000..46dcf28 --- /dev/null +++ b/lunaix-os/arch/i386/klib/fast_str.c @@ -0,0 +1,26 @@ +#include + +void* +memcpy(void* dest, const void* src, unsigned long num) +{ + if (!num) + return dest; + + asm volatile("movl %1, %%edi\n" + "rep movsb\n" ::"S"(src), + "r"(dest), + "c"(num) + : "edi", "memory"); + return dest; +} + +void* +memset(void* ptr, int value, unsigned long num) +{ + asm volatile("movl %1, %%edi\n" + "rep stosb\n" ::"c"(num), + "r"(ptr), + "a"(value) + : "edi", "memory"); + return ptr; +} \ No newline at end of file diff --git a/lunaix-os/arch/i386/mm/fault.c b/lunaix-os/arch/i386/mm/fault.c index 44f01a6..4177147 100644 --- a/lunaix-os/arch/i386/mm/fault.c +++ b/lunaix-os/arch/i386/mm/fault.c @@ -1,14 +1,14 @@ #include #include #include -#include +#include #include bool __arch_prepare_fault_context(struct fault_context* fault) { - isr_param* ictx = fault->ictx; + struct hart_state* ictx = fault->hstate; ptr_t ptr = cpu_ldeaddr(); if (!ptr) { diff --git a/lunaix-os/arch/i386/mm/gdt.c b/lunaix-os/arch/i386/mm/gdt.c index 5d4ffe8..10b6bec 100644 --- a/lunaix-os/arch/i386/mm/gdt.c +++ b/lunaix-os/arch/i386/mm/gdt.c @@ -1,5 +1,5 @@ #include -#include +#include "sys/x86_isa.h" #define SD_TYPE(x) (x << 8) #define SD_CODE_DATA(x) (x << 12) diff --git a/lunaix-os/arch/i386/syscall.S b/lunaix-os/arch/i386/syscall.S index bfbf740..4682df4 100644 --- a/lunaix-os/arch/i386/syscall.S +++ b/lunaix-os/arch/i386/syscall.S @@ -83,7 +83,7 @@ syscall_hndlr: pushl %ebp movl %esp, %ebp - movl 8(%esp), %ebx // isr_param* + movl 8(%esp), %ebx // struct hart_state* addl $4, %ebx movl (%ebx), %eax /* eax: call code as well as the return value from syscall */ diff --git a/lunaix-os/arch/i386/trace.c b/lunaix-os/arch/i386/trace.c new file mode 100644 index 0000000..71c3229 --- /dev/null +++ b/lunaix-os/arch/i386/trace.c @@ -0,0 +1,49 @@ +#include + +void +trace_print_transistion_short(struct hart_state* hstate) +{ + trace_log(" trigger: iv=%d, ecause=%p", + hart_vector_stamp(hstate), + hart_ecause(hstate)); +} + +void +trace_print_transition_full(struct hart_state* hstate) +{ + trace_log("hart state transition"); + trace_log(" vector=%d, ecause=0x%x", + hart_vector_stamp(hstate), + hart_ecause(hstate)); + trace_log(" eflags=0x%x", hstate->execp->eflags); + trace_log(" sp=%p, [seg_sel=0x%04x]", + hstate->execp->esp, + hstate->execp->esp); + trace_log(" ip=%p, seg_sel=0x%04x", + hstate->execp->eip, + hstate->execp->cs); +} + +void +trace_dump_state(struct hart_state* hstate) +{ + struct regcontext* rh = &hstate->registers; + struct exec_param* ep = hstate->execp; + trace_log("hart state dump (depth=%d)", hstate->depth); + trace_log(" eax=0x%08x, ebx=0x%08x, ecx=0x%08x", + rh->eax, rh->ebx, rh->ecx); + trace_log(" edx=0x%08x, ebp=0x%08x", + rh->edx, rh->ebp); + trace_log(" ds=0x%04x, edi=0x%08x", + rh->ds, rh->edi); + trace_log(" es=0x%04x, esi=0x%08x", + rh->es, rh->esi); + trace_log(" fs=0x%04x, gs=0x%x", + rh->fs, rh->gs); + trace_log(" cs=0x%04x, ip=0x%08x", + ep->cs, ep->eip); + trace_log(" [ss=0x%04x],sp=0x%08x", + ep->ss, ep->eip); + trace_log(" eflags=0x%08x", + ep->eflags); +} \ No newline at end of file diff --git a/lunaix-os/hal/ahci/ahci.c b/lunaix-os/hal/ahci/ahci.c index 11e1621..bd84bf8 100644 --- a/lunaix-os/hal/ahci/ahci.c +++ b/lunaix-os/hal/ahci/ahci.c @@ -19,7 +19,7 @@ #include #include -#include +#include #include #include #include diff --git a/lunaix-os/hal/ahci/io_event.c b/lunaix-os/hal/ahci/io_event.c index aec209e..4e77f98 100644 --- a/lunaix-os/hal/ahci/io_event.c +++ b/lunaix-os/hal/ahci/io_event.c @@ -1,21 +1,21 @@ #include #include -#include +#include #include #include LOG_MODULE("io_evt") void -ahci_hba_isr(const isr_param* param) +ahci_hba_isr(const struct hart_state* hstate) { struct ahci_hba* hba; struct ahci_driver *pos, *n; - struct llist_header* ahcis = (struct llist_header*)isrm_get_payload(param); + struct llist_header* ahcis = (struct llist_header*)isrm_get_payload(hstate); llist_for_each(pos, n, ahcis, ahci_drvs) { - if (pos->id == (int)param->execp->vector) { + if (pos->id == hart_vector_stamp(hstate)) { hba = &pos->hba; goto proceed; } diff --git a/lunaix-os/hal/char/uart/16550_pmio.c b/lunaix-os/hal/char/uart/16550_pmio.c index 7bd99da..62d403a 100644 --- a/lunaix-os/hal/char/uart/16550_pmio.c +++ b/lunaix-os/hal/char/uart/16550_pmio.c @@ -9,7 +9,7 @@ * */ #include -#include +#include #include @@ -36,9 +36,10 @@ com_regwrite(struct uart16550* uart, ptr_t regoff, u32_t val) } static void -com_irq_handler(const isr_param* param) +com_irq_handler(const struct hart_state* hstate) { - uart_general_irq_handler(param->execp->vector, &com_ports); + int vector = hart_vector_stamp(hstate); + uart_general_irq_handler(vector, &com_ports); } static int diff --git a/lunaix-os/hal/intc.c b/lunaix-os/hal/intc.c deleted file mode 100644 index ae08909..0000000 --- a/lunaix-os/hal/intc.c +++ /dev/null @@ -1,24 +0,0 @@ -#include -#include - -#include - -struct intc_context arch_intc_ctx; - -void -intc_irq_attach(int irq, int iv, cpu_t dest, u32_t flags) -{ - arch_intc_ctx.irq_attach(&arch_intc_ctx, irq, iv, dest, flags); -} - -void -intc_notify_eoi(cpu_t id, int iv) -{ - arch_intc_ctx.notify_eoi(&arch_intc_ctx, id, iv); -} - -void -intc_notify_eos(cpu_t id) -{ - intc_notify_eoi(id, LUNAIX_SCHED); -} \ No newline at end of file diff --git a/lunaix-os/hal/pci.c b/lunaix-os/hal/pci.c index 8a5048b..af7a256 100644 --- a/lunaix-os/hal/pci.c +++ b/lunaix-os/hal/pci.c @@ -214,6 +214,42 @@ pci_probe_bar_info(struct pci_device* device) } } +void +pci_setup_msi(struct pci_device* device, int vector) +{ + // PCI LB Spec. (Rev 3) Section 6.8 & 6.8.1 + + ptr_t msi_addr = pci_get_msi_base(); + u32_t msi_data = pci_config_msi_data(vector); + + pci_reg_t reg1 = pci_read_cspace(device->cspace_base, device->msi_loc); + pci_reg_t msg_ctl = reg1 >> 16; + int offset_cap64 = !!(msg_ctl & MSI_CAP_64BIT) * 4; + + pci_write_cspace(device->cspace_base, + PCI_MSI_ADDR_LO(device->msi_loc), + msi_addr); + + if (offset_cap64) { + pci_write_cspace(device->cspace_base, + PCI_MSI_ADDR_HI(device->msi_loc), + (u64_t)msi_addr >> 32); + } + + pci_write_cspace(device->cspace_base, + PCI_MSI_DATA(device->msi_loc, offset_cap64), + msi_data & 0xffff); + + if ((msg_ctl & MSI_CAP_MASK)) { + pci_write_cspace( + device->cspace_base, PCI_MSI_MASK(device->msi_loc, offset_cap64), 0); + } + + // manipulate the MSI_CTRL to allow device using MSI to request service. + reg1 = (reg1 & 0xff8fffff) | 0x10000; + pci_write_cspace(device->cspace_base, device->msi_loc, reg1); +} + void pci_probe_msi_info(struct pci_device* device) { diff --git a/lunaix-os/hal/timer/timer_device.c b/lunaix-os/hal/timer/timer_device.c index 3e47fac..1db8147 100644 --- a/lunaix-os/hal/timer/timer_device.c +++ b/lunaix-os/hal/timer/timer_device.c @@ -51,7 +51,7 @@ __hwtimer_ioctl(struct device* dev, u32_t req, va_list args) void hwtimer_init(u32_t hertz, void* tick_callback) { - struct hwtimer* hwt_ctx = hwtimer_choose(); + struct hwtimer* hwt_ctx = select_platform_timer(); hwt_ctx->init(hwt_ctx, hertz, tick_callback); hwt_ctx->running_freq = hertz; diff --git a/lunaix-os/includes/hal/ahci/ahci.h b/lunaix-os/includes/hal/ahci/ahci.h index d4db5c1..a9d96c7 100644 --- a/lunaix-os/includes/hal/ahci/ahci.h +++ b/lunaix-os/includes/hal/ahci/ahci.h @@ -2,7 +2,7 @@ #define __LUNAIX_AHCI_H #include "hba.h" -#include +#include /* * Macro naming rule: @@ -59,6 +59,6 @@ struct ahci_driver* ahci_driver_init(struct ahci_driver_param* param); void -ahci_hba_isr(const isr_param* param); +ahci_hba_isr(const struct hart_state* hstate); #endif /* __LUNAIX_AHCI_H */ diff --git a/lunaix-os/includes/hal/hwtimer.h b/lunaix-os/includes/hal/hwtimer.h index 3a55d6d..2faa507 100644 --- a/lunaix-os/includes/hal/hwtimer.h +++ b/lunaix-os/includes/hal/hwtimer.h @@ -26,7 +26,7 @@ void hwtimer_init(u32_t hertz, void* tick_callback); struct hwtimer* -hwtimer_choose(); +select_platform_timer(); ticks_t hwtimer_base_frequency(); diff --git a/lunaix-os/includes/hal/intc.h b/lunaix-os/includes/hal/intc.h deleted file mode 100644 index 6a2828e..0000000 --- a/lunaix-os/includes/hal/intc.h +++ /dev/null @@ -1,53 +0,0 @@ -#ifndef __LUNAIX_INTC_H -#define __LUNAIX_INTC_H - -#include - -#define IRQ_TRIG_EDGE 0b0 -#define IRQ_TRIG_LEVEL 0b1 - -#define IRQ_TYPE_FIXED (0b01 << 1) -#define IRQ_TYPE_NMI (0b11 << 1) -#define IRQ_TYPE (0b11 << 1) - -#define IRQ_VE_HI (0b1 << 3) -#define IRQ_VE_LO (0b0 << 3) - -#define IRQ_DEFAULT (IRQ_TRIG_EDGE | IRQ_TYPE_FIXED | IRQ_VE_HI) - -struct intc_context -{ - char* name; - void* data; - - void (*irq_attach)(struct intc_context*, - int irq, - int iv, - cpu_t dest, - u32_t flags); - void (*notify_eoi)(struct intc_context*, cpu_t id, int iv); -}; - -void -intc_init(); - -void -intc_irq_attach(int irq, int iv, cpu_t dest, u32_t flags); - -/** - * @brief Notify end of interrupt event - * - * @param id - */ -void -intc_notify_eoi(cpu_t id, int iv); - -/** - * @brief Notify end of scheduling event - * - * @param id - */ -void -intc_notify_eos(cpu_t id); - -#endif /* __LUNAIX_INTC_H */ diff --git a/lunaix-os/includes/hal/pci.h b/lunaix-os/includes/hal/pci.h index adc270f..da45090 100644 --- a/lunaix-os/includes/hal/pci.h +++ b/lunaix-os/includes/hal/pci.h @@ -39,7 +39,8 @@ #define PCI_BAR_ADDR_MM(x) ((x) & ~0xf) #define PCI_BAR_ADDR_IO(x) ((x) & ~0x3) -#define PCI_MSI_ADDR(msi_base) ((msi_base) + 4) +#define PCI_MSI_ADDR_LO(msi_base) ((msi_base) + 4) +#define PCI_MSI_ADDR_HI(msi_base) ((msi_base) + 8) #define PCI_MSI_DATA(msi_base, offset) ((msi_base) + 8 + offset) #define PCI_MSI_MASK(msi_base, offset) ((msi_base) + 0xc + offset) @@ -168,6 +169,9 @@ pci_bind_instance(struct pci_device* pcidev, void* devobj); void pci_probe_bar_info(struct pci_device* device); +void +pci_setup_msi(struct pci_device* device, int vector); + void pci_probe_msi_info(struct pci_device* device); diff --git a/lunaix-os/includes/hal/rtc/mc146818a.h b/lunaix-os/includes/hal/rtc/mc146818a.h deleted file mode 100644 index 3e81ff1..0000000 --- a/lunaix-os/includes/hal/rtc/mc146818a.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef __LUNAIX_MC146818A_H -#define __LUNAIX_MC146818A_H - -/* - FIXME the drivers should go into ldga -*/ - -#include - -struct hwrtc* -mc146818a_rtc_context(); - -#endif /* __LUNAIX_MC146818A_H */ diff --git a/lunaix-os/includes/lib/crc.h b/lunaix-os/includes/klibc/crc.h similarity index 99% rename from lunaix-os/includes/lib/crc.h rename to lunaix-os/includes/klibc/crc.h index 4e9c4db..9072134 100644 --- a/lunaix-os/includes/lib/crc.h +++ b/lunaix-os/includes/klibc/crc.h @@ -1,5 +1,6 @@ #ifndef __LUNAIX_CRC_H #define __LUNAIX_CRC_H + unsigned int crc32b(unsigned char* data, unsigned int size); diff --git a/lunaix-os/includes/lib/hash.h b/lunaix-os/includes/klibc/hash.h similarity index 100% rename from lunaix-os/includes/lib/hash.h rename to lunaix-os/includes/klibc/hash.h diff --git a/lunaix-os/includes/klibc/ia_utils.h b/lunaix-os/includes/klibc/ia_utils.h new file mode 100644 index 0000000..df7704f --- /dev/null +++ b/lunaix-os/includes/klibc/ia_utils.h @@ -0,0 +1,6 @@ +#ifndef __LUNAIX_IAUTILS_H +#define __LUNAIX_IAUTILS_H + +char* itoa(int value, char* str, int base); + +#endif /* __LUNAIX_IAUTILS_H */ diff --git a/lunaix-os/includes/klibc/stdlib.h b/lunaix-os/includes/klibc/stdlib.h deleted file mode 100644 index a6c83a9..0000000 --- a/lunaix-os/includes/klibc/stdlib.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef __LUNAIX_STDLIB_H -#define __LUNAIX_STDLIB_H - -#ifdef __LUNAIX_LIBC -char* __uitoa_internal(unsigned int value, char* str, int base, unsigned int* size); -char* __itoa_internal(int value, char* str, int base, unsigned int* size); -#endif - -char* itoa(int value, char* str, int base); - -#endif /* __LUNAIX_STDLIB_H */ diff --git a/lunaix-os/includes/lunaix/ds/hashtable.h b/lunaix-os/includes/lunaix/ds/hashtable.h index 41ca36b..3a38af8 100644 --- a/lunaix-os/includes/lunaix/ds/hashtable.h +++ b/lunaix-os/includes/lunaix/ds/hashtable.h @@ -13,7 +13,7 @@ #ifndef __LUNAIX_HASHTABLE_H #define __LUNAIX_HASHTABLE_H -#include +#include #include struct hbucket diff --git a/lunaix-os/includes/lunaix/ds/hstr.h b/lunaix-os/includes/lunaix/ds/hstr.h index d954103..6d2b0ee 100644 --- a/lunaix-os/includes/lunaix/ds/hstr.h +++ b/lunaix-os/includes/lunaix/ds/hstr.h @@ -1,7 +1,7 @@ #ifndef __LUNAIX_HSTR_H #define __LUNAIX_HSTR_H -#include +#include #define HSTR_FULL_HASH 32 diff --git a/lunaix-os/includes/lunaix/isrm.h b/lunaix-os/includes/lunaix/generic/isrm.h similarity index 73% rename from lunaix-os/includes/lunaix/isrm.h rename to lunaix-os/includes/lunaix/generic/isrm.h index 9d22279..7e319b5 100644 --- a/lunaix-os/includes/lunaix/isrm.h +++ b/lunaix-os/includes/lunaix/generic/isrm.h @@ -12,9 +12,9 @@ #define __LUNAIX_ISRM_H #include -#include +#include -typedef void (*isr_cb)(const isr_param*); +typedef void (*isr_cb)(const struct hart_state*); void isrm_init(); @@ -70,9 +70,28 @@ isr_cb isrm_get(int iv); ptr_t -isrm_get_payload(const isr_param*); +isrm_get_payload(const struct hart_state*); void isrm_set_payload(int iv, ptr_t); +void +isrm_irq_attach(int irq, int iv, cpu_t dest, u32_t flags); + +/** + * @brief Notify end of interrupt event + * + * @param id + */ +void +isrm_notify_eoi(cpu_t id, int iv); + +/** + * @brief Notify end of scheduling event + * + * @param id + */ +void +isrm_notify_eos(cpu_t id); + #endif /* __LUNAIX_ISRM_H */ diff --git a/lunaix-os/includes/lunaix/generic/trace_arch.h b/lunaix-os/includes/lunaix/generic/trace_arch.h new file mode 100644 index 0000000..384ac7c --- /dev/null +++ b/lunaix-os/includes/lunaix/generic/trace_arch.h @@ -0,0 +1,15 @@ +#ifndef __LUNAIX_TRACE_ARCH_H +#define __LUNAIX_TRACE_ARCH_H + +#include + +void +trace_print_transistion_short(struct hart_state* hstate); + +void +trace_print_transition_full(struct hart_state* hstate); + +void +trace_dump_state(struct hart_state* hstate); + +#endif /* __LUNAIX_TRACE_ARCH_H */ diff --git a/lunaix-os/includes/lunaix/pcontext.h b/lunaix-os/includes/lunaix/hart_state.h similarity index 53% rename from lunaix-os/includes/lunaix/pcontext.h rename to lunaix-os/includes/lunaix/hart_state.h index 77e7614..a9172bd 100644 --- a/lunaix-os/includes/lunaix/pcontext.h +++ b/lunaix-os/includes/lunaix/hart_state.h @@ -3,42 +3,41 @@ struct exec_param; struct regcontext; -struct pcontext; -typedef struct pcontext isr_param; +struct hart_state; #include -#include +#include -struct transfer_context +struct hart_transition { ptr_t inject; struct { - struct pcontext isr; + struct hart_state state; struct exec_param eret; } compact transfer; }; bool -inject_transfer_context(ptr_t vm_mnt, struct transfer_context* tctx); +install_hart_transition(ptr_t vm_mnt, struct hart_transition* tctx); void -thread_setup_trasnfer(struct transfer_context* tctx, +hart_prepare_transition(struct hart_transition* tctx, ptr_t kstack_tp, ptr_t ustack_pt, ptr_t entry, bool to_user); static inline void -thread_create_user_transfer(struct transfer_context* tctx, +hart_user_transfer(struct hart_transition* tctx, ptr_t kstack_tp, ptr_t ustack_pt, ptr_t entry) { - thread_setup_trasnfer(tctx, kstack_tp, ustack_pt, entry, true); + hart_prepare_transition(tctx, kstack_tp, ustack_pt, entry, true); } static inline void -thread_create_kernel_transfer(struct transfer_context* tctx, +hart_kernel_transfer(struct hart_transition* tctx, ptr_t kstack_tp, ptr_t entry) { - thread_setup_trasnfer(tctx, kstack_tp, 0, entry, false); + hart_prepare_transition(tctx, kstack_tp, 0, entry, false); } #endif /* __LUNAIX_CONTEXT_H */ diff --git a/lunaix-os/includes/lunaix/mm/fault.h b/lunaix-os/includes/lunaix/mm/fault.h index 3367841..057ef17 100644 --- a/lunaix-os/includes/lunaix/mm/fault.h +++ b/lunaix-os/includes/lunaix/mm/fault.h @@ -4,14 +4,14 @@ #include #include #include -#include +#include #define RESOLVE_OK ( 0b000001 ) #define NO_PREALLOC ( 0b000010 ) struct fault_context { - isr_param* ictx; + struct hart_state* hstate; struct { diff --git a/lunaix-os/includes/lunaix/process.h b/lunaix-os/includes/lunaix/process.h index ba73de0..fe33fa2 100644 --- a/lunaix-os/includes/lunaix/process.h +++ b/lunaix-os/includes/lunaix/process.h @@ -12,7 +12,7 @@ #include #include #include -#include +#include #include @@ -60,7 +60,7 @@ struct proc_sig int sig_num; void* sigact; void* sighand; - isr_param* saved_ictx; + struct hart_state* saved_hstate; } __attribute__((packed)); @@ -81,7 +81,7 @@ struct thread */ struct { - isr_param* intr_ctx; + struct hart_state* hstate; ptr_t ustack_top; }; // *critical section @@ -133,7 +133,7 @@ struct proc_info }; struct proc_mm* mm; - struct sigregister* sigreg; + struct sigregistry* sigreg; struct v_fdtable* fdtable; struct v_dnode* cwd; struct { @@ -351,7 +351,7 @@ thread_release_mem(struct thread* thread); static inline struct sigact* active_signal(struct thread* thread) { struct sigctx* sigctx = &thread->sigctx; - struct sigregister* sigreg = thread->process->sigreg; + struct sigregistry* sigreg = thread->process->sigreg; return sigreg->signals[sigctx->sig_active]; } diff --git a/lunaix-os/includes/lunaix/signal.h b/lunaix-os/includes/lunaix/signal.h index 2623412..e983846 100644 --- a/lunaix-os/includes/lunaix/signal.h +++ b/lunaix-os/includes/lunaix/signal.h @@ -35,7 +35,7 @@ struct sigact pid_t sender; }; -struct sigregister { +struct sigregistry { struct sigact* signals[_SIG_NUM]; }; @@ -54,15 +54,15 @@ void signal_dup_context(struct sigctx* dest_ctx); void -signal_dup_registers(struct sigregister* dest_reg); +signal_dup_registry(struct sigregistry* dest_reg); void signal_reset_context(struct sigctx* sigctx); void -signal_reset_register(struct sigregister* sigreg); +signal_reset_registry(struct sigregistry* sigreg); void -signal_free_registers(struct sigregister* sigreg); +signal_free_registry(struct sigregistry* sigreg); #endif /* __LUNAIX_SIGNAL_H */ diff --git a/lunaix-os/includes/lunaix/timer.h b/lunaix-os/includes/lunaix/timer.h index d64d628..08718ad 100644 --- a/lunaix-os/includes/lunaix/timer.h +++ b/lunaix-os/includes/lunaix/timer.h @@ -3,7 +3,7 @@ #include #include -#include +#include #define SYS_TIMER_FREQUENCY_HZ 1000 diff --git a/lunaix-os/includes/lunaix/trace.h b/lunaix-os/includes/lunaix/trace.h index 9409c36..c89a8b3 100644 --- a/lunaix-os/includes/lunaix/trace.h +++ b/lunaix-os/includes/lunaix/trace.h @@ -2,7 +2,8 @@ #define __LUNAIX_TRACE_H #include -#include +#include +#include struct ksym_entry { @@ -77,7 +78,7 @@ trace_printstack_of(ptr_t fp); * @param isrm */ void -trace_printstack_isr(const isr_param* isrm); +trace_printstack_isr(const struct hart_state* hstate); /** * @brief Print the stack trace starting from caller's frame pointer. @@ -86,4 +87,7 @@ trace_printstack_isr(const isr_param* isrm); void trace_printstack(); +void +trace_log(const char* fmt, ...); + #endif /* __LUNAIX_TRACE_H */ diff --git a/lunaix-os/includes/lunaix/types.h b/lunaix-os/includes/lunaix/types.h index d4273d8..9aff5ab 100644 --- a/lunaix-os/includes/lunaix/types.h +++ b/lunaix-os/includes/lunaix/types.h @@ -16,11 +16,10 @@ typedef unsigned short u16_t; typedef unsigned int u32_t; typedef unsigned long long u64_t; typedef unsigned long ptr_t; +typedef unsigned long reg_t; typedef int pid_t; typedef signed long ssize_t; -// typedef unsigned long size_t; -// typedef unsigned long off_t; typedef unsigned int cpu_t; diff --git a/lunaix-os/includes/sdbg/gdbstub.h b/lunaix-os/includes/sdbg/gdbstub.h index d5b386d..f036d50 100644 --- a/lunaix-os/includes/sdbg/gdbstub.h +++ b/lunaix-os/includes/sdbg/gdbstub.h @@ -1,9 +1,17 @@ #ifndef __LUNAIX_GDBSTUB_H #define __LUNAIX_GDBSTUB_H -#include +#include +#include + +struct gdb_state +{ + int signum; + struct serial_dev* sdev; + reg_t registers[GDB_CPU_NUM_REGISTERS]; +}; void -gdbstub_loop(isr_param* param); +gdbstub_loop(struct hart_state* hstate); #endif /* __LUNAIX_GDBSTUB_H */ diff --git a/lunaix-os/includes/sdbg/lsdbg.h b/lunaix-os/includes/sdbg/lsdbg.h index 730011d..1864f1f 100644 --- a/lunaix-os/includes/sdbg/lsdbg.h +++ b/lunaix-os/includes/sdbg/lsdbg.h @@ -1,7 +1,7 @@ #ifndef __LUNAIX_LSDBG_H #define __LUNAIX_LSDBG_H -#include +#include #define SDBG_CLNT_HI 0x10 #define SDBG_CLNT_QUIT 0xff @@ -18,6 +18,6 @@ #define SDBG_STATE_WAIT_BRK 2 void -lunaix_sdbg_loop(isr_param* param); +lunaix_sdbg_loop(struct hart_state* hstate); #endif /* __LUNAIX_LSDBG_H */ diff --git a/lunaix-os/kernel/block/blkpart_gpt.c b/lunaix-os/kernel/block/blkpart_gpt.c index c989456..e9770ce 100644 --- a/lunaix-os/kernel/block/blkpart_gpt.c +++ b/lunaix-os/kernel/block/blkpart_gpt.c @@ -7,7 +7,7 @@ #include -#include +#include #define GPT_BLKSIZE 512 #define LBA2OFF(lba) ((lba) * GPT_BLKSIZE) diff --git a/lunaix-os/kernel/block/block.c b/lunaix-os/kernel/block/block.c index 8ca5117..b620b2f 100644 --- a/lunaix-os/kernel/block/block.c +++ b/lunaix-os/kernel/block/block.c @@ -3,7 +3,7 @@ #include -#include +#include #include #include diff --git a/lunaix-os/kernel/debug/failsafe.c b/lunaix-os/kernel/debug/failsafe.c index 5fdbc5d..7c1ecff 100644 --- a/lunaix-os/kernel/debug/failsafe.c +++ b/lunaix-os/kernel/debug/failsafe.c @@ -9,16 +9,26 @@ LOG_MODULE("NMM") void do_failsafe_unrecoverable(ptr_t frame_link, ptr_t stack_link) { + ERROR("------- [cut here] ------- \n"); ERROR("diagnositic mode"); - ERROR("check: init stack: %s", check_bootstack_sanity() ? "ok" : "smashing"); // TODO ...check other invariants + if (current_thread && current_thread->hstate) + { + struct hart_state* hstate = current_thread->hstate; + + trace_print_transition_full(hstate); + ERROR("++++++"); - ERROR("non recoverable: Nightmare Moon arrival."); + trace_dump_state(hstate); + ERROR("++++++"); + } trace_printstack(); - + ERROR("++++++"); + + ERROR("non recoverable: Nightmare Moon arrival."); spin(); } \ No newline at end of file diff --git a/lunaix-os/kernel/debug/gdbstub.c b/lunaix-os/kernel/debug/gdbstub.c index 963e5ff..c88ab8d 100644 --- a/lunaix-os/kernel/debug/gdbstub.c +++ b/lunaix-os/kernel/debug/gdbstub.c @@ -35,54 +35,10 @@ #include #include #include -#include - -/***************************************************************************** - * Types - ****************************************************************************/ -#ifndef GDBSTUB_DONT_DEFINE_STDINT_TYPES -typedef unsigned char u8_t; -typedef unsigned short u16_t; -typedef unsigned long uint32_t; -#endif - -typedef unsigned int address; -typedef unsigned int reg; - -enum GDB_REGISTER -{ - GDB_CPU_I386_REG_EAX = 0, - GDB_CPU_I386_REG_ECX = 1, - GDB_CPU_I386_REG_EDX = 2, - GDB_CPU_I386_REG_EBX = 3, - GDB_CPU_I386_REG_ESP = 4, - GDB_CPU_I386_REG_EBP = 5, - GDB_CPU_I386_REG_ESI = 6, - GDB_CPU_I386_REG_EDI = 7, - GDB_CPU_I386_REG_PC = 8, - GDB_CPU_I386_REG_PS = 9, - GDB_CPU_I386_REG_CS = 10, - GDB_CPU_I386_REG_SS = 11, - GDB_CPU_I386_REG_DS = 12, - GDB_CPU_I386_REG_ES = 13, - GDB_CPU_I386_REG_FS = 14, - GDB_CPU_I386_REG_GS = 15, - GDB_CPU_NUM_REGISTERS = 16 -}; - -struct gdb_state -{ - int signum; - struct serial_dev* sdev; - reg registers[GDB_CPU_NUM_REGISTERS]; -}; - -/***************************************************************************** - * - * GDB Remote Serial Protocol - * - ****************************************************************************/ +#include +#include +#include /***************************************************************************** * Macros @@ -90,14 +46,8 @@ struct gdb_state #define GDB_PRINT(...) -#define COM_PORT SERIAL_COM1 - #define GDB_EOF (-1) -#ifndef NULL -#define NULL ((void*)0) -#endif - #ifndef GDB_ASSERT #define GDB_ASSERT(x) \ do { \ @@ -119,13 +69,10 @@ gdb_sys_getc(struct gdb_state* state); int gdb_sys_putchar(struct gdb_state* state, int ch); int -gdb_sys_mem_readb(struct gdb_state* state, address addr, char* val); -int -gdb_sys_mem_writeb(struct gdb_state* state, address addr, char val); -int -gdb_sys_continue(struct gdb_state* state); +gdb_sys_mem_readb(struct gdb_state* state, ptr_t addr, char* val); int -gdb_sys_step(struct gdb_state* state); +gdb_sys_mem_writeb(struct gdb_state* state, ptr_t addr, char val); + /***************************************************************************** * Types @@ -159,9 +106,7 @@ gdb_read(struct gdb_state* state, unsigned int buf_len, unsigned int len); -/* String processing helper functions */ -static int -gdb_strlen(const char* ch); + static char gdb_get_digit(int val); static int @@ -228,14 +173,14 @@ static int gdb_mem_read(struct gdb_state* state, char* buf, unsigned int buf_len, - address addr, + ptr_t addr, unsigned int len, gdb_enc_func enc); static int gdb_mem_write(struct gdb_state* state, const char* buf, unsigned int buf_len, - address addr, + ptr_t addr, unsigned int len, gdb_dec_func dec); static int @@ -247,22 +192,6 @@ gdb_step(struct gdb_state* state); * String Processing Helper Functions ****************************************************************************/ -/* - * Get null-terminated string length. - */ -static int -gdb_strlen(const char* ch) -{ - int len; - - len = 0; - while (*ch++) { - len += 1; - } - - return len; -} - /* * Get integer value for a string representation. * @@ -703,7 +632,7 @@ static int gdb_mem_read(struct gdb_state* state, char* buf, unsigned int buf_len, - address addr, + ptr_t addr, unsigned int len, gdb_enc_func enc) { @@ -733,7 +662,7 @@ static int gdb_mem_write(struct gdb_state* state, const char* buf, unsigned int buf_len, - address addr, + ptr_t addr, unsigned int len, gdb_dec_func dec) { @@ -811,7 +740,7 @@ gdb_send_conmsg_packet(struct gdb_state* state, } buf[0] = 'O'; - status = gdb_enc_hex(&buf[1], buf_len - 1, msg, gdb_strlen(msg)); + status = gdb_enc_hex(&buf[1], buf_len - 1, msg, strlen(msg)); if (status == GDB_EOF) { return GDB_EOF; } @@ -925,7 +854,7 @@ gdb_read(struct gdb_state* state, int gdb_main(struct gdb_state* state) { - address addr; + ptr_t addr; char pkt_buf[256]; int status; unsigned int length; @@ -1162,36 +1091,6 @@ gdb_main(struct gdb_state* state) return 0; } -/***************************************************************************** - * Types - ****************************************************************************/ - -struct gdb_idtr -{ - u16_t len; - uint32_t offset; -} __attribute__((packed)); - -struct gdb_idt_gate -{ - u16_t offset_low; - u16_t segment; - u16_t flags; - u16_t offset_high; -} __attribute__((packed)); - -/***************************************************************************** - * Prototypes - ****************************************************************************/ -#define gdb_x86_io_write_8(port, val) port_wrbyte(port, val) -#define gdb_x86_io_read_8(port) port_rdbyte(port) - -#define gdb_x86_serial_getc() serial_rx_byte(COM_PORT) -#define gdb_x86_serial_putchar(ch) serial_tx_byte(COM_PORT, ch) - -#ifdef __STRICT_ANSI__ -#define asm __asm__ -#endif static struct gdb_state gdb_state; static volatile int start_debugging = 0; @@ -1200,57 +1099,14 @@ static volatile int start_debugging = 0; * Debug interrupt handler. */ void -gdbstub_loop(isr_param* param) +gdbstub_loop(struct hart_state* hstate) { - /* Translate vector to signal */ - switch (param->execp->vector) { - case 1: - gdb_state.signum = 5; - break; - case 3: - gdb_state.signum = 5; - break; - default: - gdb_state.signum = 7; - } - - /* Load Registers */ - gdb_state.registers[GDB_CPU_I386_REG_EAX] = param->registers.eax; - gdb_state.registers[GDB_CPU_I386_REG_ECX] = param->registers.ecx; - gdb_state.registers[GDB_CPU_I386_REG_EDX] = param->registers.edx; - gdb_state.registers[GDB_CPU_I386_REG_EBX] = param->registers.ebx; - gdb_state.registers[GDB_CPU_I386_REG_ESP] = param->esp; - gdb_state.registers[GDB_CPU_I386_REG_EBP] = param->registers.ebp; - gdb_state.registers[GDB_CPU_I386_REG_ESI] = param->registers.esi; - gdb_state.registers[GDB_CPU_I386_REG_EDI] = param->registers.edi; - gdb_state.registers[GDB_CPU_I386_REG_PC] = param->execp->eip; - gdb_state.registers[GDB_CPU_I386_REG_CS] = param->execp->cs; - gdb_state.registers[GDB_CPU_I386_REG_PS] = param->execp->eflags; - gdb_state.registers[GDB_CPU_I386_REG_SS] = param->execp->ss; - gdb_state.registers[GDB_CPU_I386_REG_DS] = param->registers.ds; - gdb_state.registers[GDB_CPU_I386_REG_ES] = param->registers.es; - gdb_state.registers[GDB_CPU_I386_REG_FS] = param->registers.fs; - gdb_state.registers[GDB_CPU_I386_REG_GS] = param->registers.gs; - + arch_gdbstub_setup_state(&gdb_state, hstate); + arch_gdbstub_save_regs(&gdb_state, hstate); + gdb_main(&gdb_state); - /* Restore Registers */ - param->registers.eax = gdb_state.registers[GDB_CPU_I386_REG_EAX]; - param->registers.ecx = gdb_state.registers[GDB_CPU_I386_REG_ECX]; - param->registers.edx = gdb_state.registers[GDB_CPU_I386_REG_EDX]; - param->registers.ebx = gdb_state.registers[GDB_CPU_I386_REG_EBX]; - param->esp = gdb_state.registers[GDB_CPU_I386_REG_ESP]; - param->registers.ebp = gdb_state.registers[GDB_CPU_I386_REG_EBP]; - param->registers.esi = gdb_state.registers[GDB_CPU_I386_REG_ESI]; - param->registers.edi = gdb_state.registers[GDB_CPU_I386_REG_EDI]; - param->execp->eip = gdb_state.registers[GDB_CPU_I386_REG_PC]; - param->execp->cs = gdb_state.registers[GDB_CPU_I386_REG_CS]; - param->execp->eflags = gdb_state.registers[GDB_CPU_I386_REG_PS]; - param->execp->ss = gdb_state.registers[GDB_CPU_I386_REG_SS]; - param->registers.ds = gdb_state.registers[GDB_CPU_I386_REG_DS]; - param->registers.es = gdb_state.registers[GDB_CPU_I386_REG_ES]; - param->registers.fs = gdb_state.registers[GDB_CPU_I386_REG_FS]; - param->registers.gs = gdb_state.registers[GDB_CPU_I386_REG_GS]; + arch_gdbstub_restore_regs(&gdb_state, hstate); } /***************************************************************************** @@ -1282,7 +1138,7 @@ gdb_sys_getc(struct gdb_state* state) * Read one byte from memory. */ int -gdb_sys_mem_readb(struct gdb_state* state, address addr, char* val) +gdb_sys_mem_readb(struct gdb_state* state, ptr_t addr, char* val) { *val = *(volatile char*)addr; return 0; @@ -1292,28 +1148,8 @@ gdb_sys_mem_readb(struct gdb_state* state, address addr, char* val) * Write one byte to memory. */ int -gdb_sys_mem_writeb(struct gdb_state* state, address addr, char val) +gdb_sys_mem_writeb(struct gdb_state* state, ptr_t addr, char val) { *(volatile char*)addr = val; return 0; } - -/* - * Continue program execution. - */ -int -gdb_sys_continue(struct gdb_state* state) -{ - gdb_state.registers[GDB_CPU_I386_REG_PS] &= ~(1 << 8); - return 0; -} - -/* - * Single step the next instruction. - */ -int -gdb_sys_step(struct gdb_state* state) -{ - gdb_state.registers[GDB_CPU_I386_REG_PS] |= 1 << 8; - return 0; -} \ No newline at end of file diff --git a/lunaix-os/kernel/debug/sdbg.c b/lunaix-os/kernel/debug/sdbg.c deleted file mode 100644 index edfecea..0000000 --- a/lunaix-os/kernel/debug/sdbg.c +++ /dev/null @@ -1,92 +0,0 @@ -// FIXME Re-design needed!! - -#include -#include -#include -#include -#include -#include - -#include - -// #define USE_LSDBG_BACKEND - -LOG_MODULE("SDBG") - -volatile int debug_mode = 0; - -// begin: @cmc -#define DBG_START 0x636d6340UL - -// begin: @yay -#define DBG_END 0x79617940UL - -static int -sdbg_serial_callback(struct serial_dev* sdev) -{ - u32_t dbg_sig = *(u32_t*)sdev->rw.buf; - - if (dbg_sig == DBG_START) { - debug_mode = 1; - } else if (dbg_sig == DBG_END) { - debug_mode = 0; - } - - // Debugger should be run later - // TODO implement a defer execution mechanism (i.e., soft interrupt) - - return SERIAL_AGAIN; -} - -void -sdbg_imm(const isr_param* param) -{ - struct exec_param* execp = param->execp; - DEBUG("Quick debug mode\n"); - DEBUG("cs=%p eip=%p eax=%p ebx=%p\n", - execp->cs, - execp->eip, - param->registers.eax, - param->registers.ebx); - DEBUG("ecx=%p edx=%p edi=%p esi=%p\n", - param->registers.ecx, - param->registers.edx, - param->registers.edi, - param->registers.esi); - DEBUG("u.esp=%p k.esp=%p ebp=%p ps=%p\n", - param->esp, - execp->esp, - param->registers.ebp, - execp->eflags); - DEBUG("ss=%p ds=%p es=%p fs=%p gs=%p\n", - execp->ss, - param->registers.ds, - param->registers.es, - param->registers.fs, - param->registers.gs); - while (1) - ; -} - -static char buf[4]; - -static void -__sdbg_breakpoint(const isr_param* param) -{ - gdbstub_loop(param); -} - -void -sdbg_init() -{ - struct serial_dev* sdev = serial_get_avilable(); - - if (!sdev) { - ERROR("no serial port available\n"); - return; - } - - kprintf("listening: %s\n", sdev->dev->name.value); - - serial_rwbuf_async(sdev, buf, 4, sdbg_serial_callback, SERIAL_RW_RX); -} \ No newline at end of file diff --git a/lunaix-os/kernel/debug/trace.c b/lunaix-os/kernel/debug/trace.c index 0a70de3..66e5505 100644 --- a/lunaix-os/kernel/debug/trace.c +++ b/lunaix-os/kernel/debug/trace.c @@ -19,6 +19,17 @@ extern struct ksyms __lunaix_ksymtable[]; static struct trace_context trace_ctx; +void +trace_log(const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + + kprintf_m("TRACE", fmt, args); + + va_end(args); +} + void trace_modksyms_init(struct boot_handoff* bhctx) { @@ -111,9 +122,9 @@ static inline void trace_print_code_entry(ptr_t sym_pc, ptr_t inst_pc, char* sym) { if (sym_pc) { - DEBUG("%s+%p", sym, inst_pc - sym_pc); + trace_log("%s+%p", sym, inst_pc - sym_pc); } else { - DEBUG("%s [%p]", sym, sym_pc); + trace_log("%s [%p]", sym, sym_pc); } } @@ -126,7 +137,7 @@ trace_printstack_of(ptr_t fp) int n = trace_walkback(tbs, fp, NB_TRACEBACK, &fp); if (fp) { - DEBUG("..."); + trace_log("..."); } for (int i = 0; i < n; i++) { @@ -139,7 +150,7 @@ void trace_printstack() { if (current_thread) { - trace_printstack_isr(current_thread->intr_ctx); + trace_printstack_isr(current_thread->hstate); } else { trace_printstack_of(abi_get_callframe()); @@ -147,29 +158,27 @@ trace_printstack() } static void -trace_printswctx(const isr_param* p, bool from_usr, bool to_usr) +trace_printswctx(const struct hart_state* hstate, bool from_usr, bool to_usr) { - struct ksym_entry* sym = trace_sym_lookup(p->execp->eip); + struct ksym_entry* sym = trace_sym_lookup(hstate->execp->eip); - DEBUG("^^^^^ --- %s", to_usr ? "user" : "kernel"); - DEBUG(" interrupted on #%d, ecode=%p", - p->execp->vector, - p->execp->err_code); - DEBUG("vvvvv --- %s", from_usr ? "user" : "kernel"); + trace_log("^^^^^ --- %s", to_usr ? "user" : "kernel"); + trace_print_transistion_short(hstate); + trace_log("vvvvv --- %s", from_usr ? "user" : "kernel"); - ptr_t sym_pc = sym ? sym->pc : p->execp->eip; - trace_print_code_entry(sym_pc, p->execp->eip, ksym_getstr(sym)); + ptr_t sym_pc = sym ? sym->pc : hart_pc(hstate); + trace_print_code_entry(sym_pc, hart_pc(hstate), ksym_getstr(sym)); } void -trace_printstack_isr(const isr_param* isrm) +trace_printstack_isr(const struct hart_state* hstate) { - isr_param* p = isrm; + struct hart_state* p = hstate; ptr_t fp = abi_get_callframe(); int prev_usrctx = 0; - DEBUG("stack trace (pid=%d)\n", __current->pid); + trace_log("stack trace (pid=%d)\n", __current->pid); trace_printstack_of(fp); @@ -184,9 +193,9 @@ trace_printstack_isr(const isr_param* isrm) trace_printswctx(p, false, true); } - fp = saved_fp(p); + fp = hart_stack_frame(p); if (!valid_fp(fp)) { - DEBUG("??? invalid frame: %p", fp); + trace_log("??? invalid frame: %p", fp); break; } @@ -194,8 +203,8 @@ trace_printstack_isr(const isr_param* isrm) prev_usrctx = !kernel_context(p); - p = p->execp->saved_prev_ctx; + p = hart_parent_state(p); } - DEBUG("----- [trace end] -----\n"); + trace_log("----- [trace end] -----\n"); } \ No newline at end of file diff --git a/lunaix-os/kernel/device/devdb.c b/lunaix-os/kernel/device/devdb.c index eb9e612..09b6b5c 100644 --- a/lunaix-os/kernel/device/devdb.c +++ b/lunaix-os/kernel/device/devdb.c @@ -2,7 +2,7 @@ #include #include -#include +#include #include diff --git a/lunaix-os/kernel/exe/exec.c b/lunaix-os/kernel/exe/exec.c index 813d5c3..0f6e4ec 100644 --- a/lunaix-os/kernel/exe/exec.c +++ b/lunaix-os/kernel/exe/exec.c @@ -263,13 +263,13 @@ __DEFINE_LXSYSCALL3(int, // we will jump to new entry point (_u_start) upon syscall's // return so execve 'will not return' from the perspective of it's invoker - eret_target(current_thread) = container.exe.entry; - eret_stack(current_thread) = container.stack_top; + hart_flow_redirect(current_thread->hstate, + container.exe.entry, container.stack_top); // these become meaningless once execved! current_thread->ustack_top = 0; signal_reset_context(¤t_thread->sigctx); - signal_reset_register(__current->sigreg); + signal_reset_registry(__current->sigreg); done: // set return value diff --git a/lunaix-os/kernel/kinit.c b/lunaix-os/kernel/kinit.c index 528a738..c74f8b3 100644 --- a/lunaix-os/kernel/kinit.c +++ b/lunaix-os/kernel/kinit.c @@ -16,10 +16,9 @@ #include #include #include -#include +#include #include -#include #include #include @@ -59,12 +58,9 @@ kernel_bootstrap(struct boot_handoff* bhctx) device_scan_drivers(); - invoke_init_function(on_earlyboot); - device_sysconf_load(); - /* Get intc online, this is the cornerstone when initing devices */ - intc_init(); + invoke_init_function(on_earlyboot); clock_init(); timer_init(); diff --git a/lunaix-os/kernel/mm/fault.c b/lunaix-os/kernel/mm/fault.c index f55e489..e840b76 100644 --- a/lunaix-os/kernel/mm/fault.c +++ b/lunaix-os/kernel/mm/fault.c @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include #include @@ -108,7 +108,7 @@ __prepare_fault_context(struct fault_context* fault) fault->resolving = pte_mkloaded(fault->resolving); fault->kernel_vmfault = kernel_vmfault; - fault->kernel_access = kernel_context(fault->ictx); + fault->kernel_access = kernel_context(fault->hstate); return true; } @@ -265,7 +265,7 @@ __fail_to_resolve(struct fault_context* fault) failsafe_diagnostic(); } - trace_printstack_isr(fault->ictx); + trace_printstack_isr(fault->hstate); thread_setsignal(current_thread, _SIGSEGV); @@ -316,15 +316,15 @@ done: } void -intr_routine_page_fault(const isr_param* param) +intr_routine_page_fault(const struct hart_state* hstate) { - if (param->depth > 10) { + if (hstate->depth > 10) { // Too many nested fault! we must messed up something // XXX should we failed silently? spin(); } - struct fault_context fault = { .ictx = param }; + struct fault_context fault = { .hstate = hstate }; if (!__prepare_fault_context(&fault)) { __fail_to_resolve(&fault); diff --git a/lunaix-os/kernel/process/fork.c b/lunaix-os/kernel/process/fork.c index c106650..6dbe737 100644 --- a/lunaix-os/kernel/process/fork.c +++ b/lunaix-os/kernel/process/fork.c @@ -106,7 +106,7 @@ dup_active_thread(ptr_t vm_mnt, struct proc_info* duped_pcb) return NULL; } - th->intr_ctx = current_thread->intr_ctx; + th->hstate = current_thread->hstate; th->kstack = current_thread->kstack; signal_dup_context(&th->sigctx); diff --git a/lunaix-os/kernel/process/sched.c b/lunaix-os/kernel/process/sched.c index bdd9736..91cf61b 100644 --- a/lunaix-os/kernel/process/sched.c +++ b/lunaix-os/kernel/process/sched.c @@ -1,7 +1,6 @@ #include #include -#include #include #include @@ -18,9 +17,11 @@ #include #include #include -#include +#include #include +#include + #include struct thread empty_thread_obj; @@ -207,7 +208,7 @@ schedule() sched_ctx.procs_index = to_check->process->pid; done: - intc_notify_eos(0); + isrm_notify_eos(0); run(to_check); fail("unexpected return from scheduler"); @@ -389,7 +390,7 @@ alloc_process() proc->created = clock_systime(); proc->pgid = proc->pid; - proc->sigreg = vzalloc(sizeof(struct sigregister)); + proc->sigreg = vzalloc(sizeof(struct sigregistry)); proc->fdtable = vzalloc(sizeof(struct v_fdtable)); proc->mm = procvm_create(proc); @@ -509,7 +510,7 @@ delete_process(struct proc_info* proc) vfree(proc->fdtable); - signal_free_registers(proc->sigreg); + signal_free_registry(proc->sigreg); procvm_mount(mm); diff --git a/lunaix-os/kernel/process/signal.c b/lunaix-os/kernel/process/signal.c index 9f5357e..e40526e 100644 --- a/lunaix-os/kernel/process/signal.c +++ b/lunaix-os/kernel/process/signal.c @@ -41,7 +41,7 @@ signal_dispatch() return 0; } - struct sigregister* sigreg = __current->sigreg; + struct sigregistry* sigreg = __current->sigreg; struct sigctx* psig = ¤t_thread->sigctx; struct sigact* prev_working = active_signal(current_thread); sigset_t mask = psig->sig_mask | (prev_working ? prev_working->sa_mask : 0); @@ -78,7 +78,7 @@ signal_dispatch() sigframe->sigact = action->sa_actor; sigframe->sighand = action->sa_handler; - sigframe->saved_ictx = current_thread->intr_ctx; + sigframe->saved_hstate = current_thread->hstate; sigactive_push(current_thread, sig_selected); @@ -196,9 +196,9 @@ signal_dup_context(struct sigctx* dest_ctx) } void -signal_dup_registers(struct sigregister* dest_reg) +signal_dup_registry(struct sigregistry* dest_reg) { - struct sigregister* oldreg = __current->sigreg; + struct sigregistry* oldreg = __current->sigreg; for (int i = 0; i < _SIG_NUM; i++) { struct sigact* oldact = oldreg->signals[i]; if (!oldact) { @@ -218,7 +218,7 @@ signal_reset_context(struct sigctx* sigctx) { } void -signal_reset_register(struct sigregister* sigreg) { +signal_reset_registry(struct sigregistry* sigreg) { for (int i = 0; i < _SIG_NUM; i++) { struct sigact* act = sigreg->signals[i]; if (act) { @@ -229,8 +229,8 @@ signal_reset_register(struct sigregister* sigreg) { } void -signal_free_registers(struct sigregister* sigreg) { - signal_reset_register(sigreg); +signal_free_registry(struct sigregistry* sigreg) { + signal_reset_registry(sigreg); vfree(sigreg); } @@ -267,14 +267,14 @@ __DEFINE_LXSYSCALL1(int, sigreturn, struct proc_sig, *sig_ctx) schedule(); } - current_thread->intr_ctx = sig_ctx->saved_ictx; + current_thread->hstate = sig_ctx->saved_hstate; if (proc_terminated(__current)) { __current->exit_code |= PEXITSIG; } else if (sigset_test(CORE, sig_ctx->sig_num)) { signal_terminate(sig_ctx->sig_num); } - ptr_t ictx = (ptr_t)current_thread->intr_ctx; + ptr_t ictx = (ptr_t)current_thread->hstate; /* Ensure our restored context is within kernel stack diff --git a/lunaix-os/kernel/process/thread.c b/lunaix-os/kernel/process/thread.c index 4ad4653..9d93566 100644 --- a/lunaix-os/kernel/process/thread.c +++ b/lunaix-os/kernel/process/thread.c @@ -14,7 +14,8 @@ LOG_MODULE("THREAD") static ptr_t -__alloc_user_thread_stack(struct proc_info* proc, struct mm_region** stack_region, ptr_t vm_mnt) +__alloc_user_thread_stack(struct proc_info* proc, + struct mm_region** stack_region, ptr_t vm_mnt) { ptr_t th_stack_top = (proc->thread_count + 1) * USR_STACK_SIZE; th_stack_top = ROUNDUP(USR_STACK_END - th_stack_top, MEM_PAGE); @@ -28,8 +29,9 @@ __alloc_user_thread_stack(struct proc_info* proc, struct mm_region** stack_regio .flags = MAP_ANON | MAP_PRIVATE, .type = REGION_TYPE_STACK }; - int errno = mmap_user((void**)&th_stack_top, &vmr, th_stack_top, NULL, ¶m); - + int errno; + + errno = mmap_user((void**)&th_stack_top, &vmr, th_stack_top, NULL, ¶m); if (errno) { WARN("failed to create user thread stack: %d", errno); return 0; @@ -144,22 +146,22 @@ start_thread(struct thread* th, ptr_t entry) assert(mm->vm_mnt); - struct transfer_context transfer; + struct hart_transition transition; if (!kernel_addr(entry)) { assert(th->ustack); ptr_t ustack_top = align_stack(th->ustack->end - 1); ustack_top -= 16; // pre_allocate a 16 byte for inject parameter - thread_create_user_transfer(&transfer, th->kstack, ustack_top, entry); + hart_user_transfer(&transition, th->kstack, ustack_top, entry); th->ustack_top = ustack_top; } else { - thread_create_kernel_transfer(&transfer, th->kstack, entry); + hart_kernel_transfer(&transition, th->kstack, entry); } - inject_transfer_context(mm->vm_mnt, &transfer); - th->intr_ctx = (isr_param*)transfer.inject; + install_hart_transition(mm->vm_mnt, &transition); + th->hstate = (struct hart_state*)transition.inject; commit_thread(th); } diff --git a/lunaix-os/kernel/spike.c b/lunaix-os/kernel/spike.c index 4c1bb03..86eaf94 100644 --- a/lunaix-os/kernel/spike.c +++ b/lunaix-os/kernel/spike.c @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include #include diff --git a/lunaix-os/kernel/time/timer.c b/lunaix-os/kernel/time/timer.c index 407bd9a..fe8f5d3 100644 --- a/lunaix-os/kernel/time/timer.c +++ b/lunaix-os/kernel/time/timer.c @@ -16,7 +16,7 @@ #include #include #include -#include +#include #include diff --git a/lunaix-os/libs/crc.c b/lunaix-os/libs/crc.c index 06ebbac..71fa03e 100644 --- a/lunaix-os/libs/crc.c +++ b/lunaix-os/libs/crc.c @@ -1,7 +1,8 @@ -#include +#include +#include // crc32 lookup table. (https://web.mit.edu/freebsd/head/sys/libkern/crc32.c) -const unsigned int crc32_tab[] = { +static const unsigned int crc32_tab[] = { 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, @@ -54,7 +55,7 @@ const unsigned int crc32_tab[] = { * @param size * @return unsigned int */ -unsigned int +unsigned int weak crc32b(unsigned char* data, unsigned int size) { unsigned int crc = (unsigned int)-1, i = 0; diff --git a/lunaix-os/libs/hash.c b/lunaix-os/libs/hash.c index 41fa4b1..b4e5c6f 100644 --- a/lunaix-os/libs/hash.c +++ b/lunaix-os/libs/hash.c @@ -1,4 +1,5 @@ -#include +#include +#include /** * @brief Simple string hash function @@ -8,7 +9,7 @@ * @param str * @return unsigned int */ -u32_t +u32_t weak strhash_32(const char* str, u32_t truncate_to) { if (!str) diff --git a/lunaix-os/libs/klibc/stdlib/itoa.c b/lunaix-os/libs/klibc/itoa.c similarity index 94% rename from lunaix-os/libs/klibc/stdlib/itoa.c rename to lunaix-os/libs/klibc/itoa.c index 24b9827..cbffd52 100644 --- a/lunaix-os/libs/klibc/stdlib/itoa.c +++ b/lunaix-os/libs/klibc/itoa.c @@ -1,10 +1,10 @@ #define __LUNAIX_LIBC -#include +#include #include char base_char[] = "0123456789abcdefghijklmnopqrstuvwxyz"; -char* +static char* __uitoa_internal(unsigned int value, char* str, int base, unsigned int* size) { unsigned int ptr = 0; @@ -26,7 +26,7 @@ __uitoa_internal(unsigned int value, char* str, int base, unsigned int* size) return str; } -char* +static char* __itoa_internal(int value, char* str, int base, unsigned int* size) { if (value < 0 && base == 10) { diff --git a/lunaix-os/libs/klibc/ksprintf.c b/lunaix-os/libs/klibc/ksprintf.c index a3a54d1..856e55f 100644 --- a/lunaix-os/libs/klibc/ksprintf.c +++ b/lunaix-os/libs/klibc/ksprintf.c @@ -1,5 +1,5 @@ #define __LUNAIX_LIBC -#include +#include #include #include #include diff --git a/lunaix-os/libs/klibc/string/mem.c b/lunaix-os/libs/klibc/string/mem.c index 0856358..b347b36 100755 --- a/lunaix-os/libs/klibc/string/mem.c +++ b/lunaix-os/libs/klibc/string/mem.c @@ -1,20 +1,17 @@ #include #include -void* +void* weak memcpy(void* dest, const void* src, unsigned long num) { - if (!num) - return dest; - asm volatile("movl %1, %%edi\n" - "rep movsb\n" ::"S"(src), - "r"(dest), - "c"(num) - : "edi", "memory"); + for (size_t i = 0; i < num; i++) { + ((u8_t*)dest)[i] = ((u8_t*)src)[i]; + } + return dest; } -void* +void* weak memmove(void* dest, const void* src, unsigned long num) { u8_t* dest_ptr = (u8_t*)dest; @@ -31,18 +28,17 @@ memmove(void* dest, const void* src, unsigned long num) return dest; } -void* +void* weak memset(void* ptr, int value, unsigned long num) { - asm volatile("movl %1, %%edi\n" - "rep stosb\n" ::"c"(num), - "r"(ptr), - "a"(value) - : "edi", "memory"); + for (size_t i = 0; i < num; i++) { + ((u8_t*)ptr)[i] = 0; + } + return ptr; } -int +int weak memcmp(const void* ptr1, const void* ptr2, unsigned long num) { u8_t* p1 = (u8_t*)ptr1; diff --git a/lunaix-os/libs/klibc/string/strchr.c b/lunaix-os/libs/klibc/string/strchr.c index 2d80c71..3fdd7c3 100644 --- a/lunaix-os/libs/klibc/string/strchr.c +++ b/lunaix-os/libs/klibc/string/strchr.c @@ -1,7 +1,7 @@ #include #include -const char* +const char* weak strchr(const char* str, int character) { char c = (char)character; diff --git a/lunaix-os/libs/klibc/string/strcmp.c b/lunaix-os/libs/klibc/string/strcmp.c index 736bb91..88759ce 100644 --- a/lunaix-os/libs/klibc/string/strcmp.c +++ b/lunaix-os/libs/klibc/string/strcmp.c @@ -1,6 +1,7 @@ #include +#include -int +int weak streq(const char* a, const char* b) { while (*a == *b) { diff --git a/lunaix-os/libs/klibc/string/strcpy.c b/lunaix-os/libs/klibc/string/strcpy.c index 86032d9..04c85c9 100644 --- a/lunaix-os/libs/klibc/string/strcpy.c +++ b/lunaix-os/libs/klibc/string/strcpy.c @@ -1,6 +1,7 @@ #include +#include -char* +char* weak strcpy(char* dest, const char* src) { char c; @@ -13,7 +14,7 @@ strcpy(char* dest, const char* src) return &dest[i]; } -char* +char* weak strncpy(char* dest, const char* src, unsigned long n) { char c; diff --git a/lunaix-os/libs/klibc/string/strlen.c b/lunaix-os/libs/klibc/string/strlen.c index 7a9c538..a1aec5a 100644 --- a/lunaix-os/libs/klibc/string/strlen.c +++ b/lunaix-os/libs/klibc/string/strlen.c @@ -1,6 +1,7 @@ #include +#include -unsigned long +unsigned long weak strlen(const char* str) { unsigned long len = 0; @@ -9,7 +10,7 @@ strlen(const char* str) return len; } -unsigned long +unsigned long weak strnlen(const char* str, unsigned long max_len) { unsigned long len = 0; diff --git a/lunaix-os/libs/klibc/string/trim.c b/lunaix-os/libs/klibc/string/trim.c index abd6508..3c3d11a 100644 --- a/lunaix-os/libs/klibc/string/trim.c +++ b/lunaix-os/libs/klibc/string/trim.c @@ -1,9 +1,10 @@ #include +#include #define WS_CHAR(c) \ (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\v' || c == '\r') -void +void weak strrtrim(char* str) { unsigned long l = strlen(str); @@ -18,7 +19,7 @@ strrtrim(char* str) str[l + 1] = '\0'; } -char* +char* weak strltrim_safe(char* str) { unsigned long l = 0; -- 2.27.0