From: Tulio A M Mendes Date: Thu, 5 Feb 2026 22:39:46 +0000 (-0300) Subject: hal: add cpu helpers (idle/irqs/stack/as) X-Git-Url: https://projects.tadryanom.me/?a=commitdiff_plain;h=a5dcb987d1afc5f45cfc45178a4ee8f05b22ec1c;p=AdrOS.git hal: add cpu helpers (idle/irqs/stack/as) --- diff --git a/include/hal/cpu.h b/include/hal/cpu.h new file mode 100644 index 0000000..776ac6a --- /dev/null +++ b/include/hal/cpu.h @@ -0,0 +1,14 @@ +#ifndef HAL_CPU_H +#define HAL_CPU_H + +#include + +uintptr_t hal_cpu_get_stack_pointer(void); +uintptr_t hal_cpu_get_address_space(void); + +void hal_cpu_set_kernel_stack(uintptr_t sp_top); + +void hal_cpu_enable_interrupts(void); +void hal_cpu_idle(void); + +#endif diff --git a/src/hal/arm/cpu.c b/src/hal/arm/cpu.c new file mode 100644 index 0000000..69f6989 --- /dev/null +++ b/src/hal/arm/cpu.c @@ -0,0 +1,35 @@ +#include "hal/cpu.h" + +#if defined(__aarch64__) + +uintptr_t hal_cpu_get_stack_pointer(void) { + uintptr_t sp; + __asm__ volatile("mov %0, sp" : "=r"(sp)); + return sp; +} + +uintptr_t hal_cpu_get_address_space(void) { + return 0; +} + +void hal_cpu_set_kernel_stack(uintptr_t sp_top) { + (void)sp_top; +} + +void hal_cpu_enable_interrupts(void) { + __asm__ volatile("msr daifclr, #2" ::: "memory"); +} + +void hal_cpu_idle(void) { + __asm__ volatile("wfi" ::: "memory"); +} + +#else + +uintptr_t hal_cpu_get_stack_pointer(void) { return 0; } +uintptr_t hal_cpu_get_address_space(void) { return 0; } +void hal_cpu_set_kernel_stack(uintptr_t sp_top) { (void)sp_top; } +void hal_cpu_enable_interrupts(void) { } +void hal_cpu_idle(void) { } + +#endif diff --git a/src/hal/mips/cpu.c b/src/hal/mips/cpu.c new file mode 100644 index 0000000..15b5e2a --- /dev/null +++ b/src/hal/mips/cpu.c @@ -0,0 +1,34 @@ +#include "hal/cpu.h" + +#if defined(__mips__) + +uintptr_t hal_cpu_get_stack_pointer(void) { + uintptr_t sp; + __asm__ volatile("move %0, $sp" : "=r"(sp)); + return sp; +} + +uintptr_t hal_cpu_get_address_space(void) { + return 0; +} + +void hal_cpu_set_kernel_stack(uintptr_t sp_top) { + (void)sp_top; +} + +void hal_cpu_enable_interrupts(void) { +} + +void hal_cpu_idle(void) { + __asm__ volatile("wait" ::: "memory"); +} + +#else + +uintptr_t hal_cpu_get_stack_pointer(void) { return 0; } +uintptr_t hal_cpu_get_address_space(void) { return 0; } +void hal_cpu_set_kernel_stack(uintptr_t sp_top) { (void)sp_top; } +void hal_cpu_enable_interrupts(void) { } +void hal_cpu_idle(void) { } + +#endif diff --git a/src/hal/riscv/cpu.c b/src/hal/riscv/cpu.c new file mode 100644 index 0000000..646f910 --- /dev/null +++ b/src/hal/riscv/cpu.c @@ -0,0 +1,35 @@ +#include "hal/cpu.h" + +#if defined(__riscv) + +uintptr_t hal_cpu_get_stack_pointer(void) { + uintptr_t sp; + __asm__ volatile("mv %0, sp" : "=r"(sp)); + return sp; +} + +uintptr_t hal_cpu_get_address_space(void) { + return 0; +} + +void hal_cpu_set_kernel_stack(uintptr_t sp_top) { + (void)sp_top; +} + +void hal_cpu_enable_interrupts(void) { + __asm__ volatile("csrsi mstatus, 0x8" ::: "memory"); +} + +void hal_cpu_idle(void) { + __asm__ volatile("wfi" ::: "memory"); +} + +#else + +uintptr_t hal_cpu_get_stack_pointer(void) { return 0; } +uintptr_t hal_cpu_get_address_space(void) { return 0; } +void hal_cpu_set_kernel_stack(uintptr_t sp_top) { (void)sp_top; } +void hal_cpu_enable_interrupts(void) { } +void hal_cpu_idle(void) { } + +#endif diff --git a/src/hal/x86/cpu.c b/src/hal/x86/cpu.c new file mode 100644 index 0000000..4c46c9c --- /dev/null +++ b/src/hal/x86/cpu.c @@ -0,0 +1,59 @@ +#include "hal/cpu.h" + +#include "gdt.h" + +#if defined(__i386__) || defined(__x86_64__) + +uintptr_t hal_cpu_get_stack_pointer(void) { + uintptr_t sp; +#if defined(__x86_64__) + __asm__ volatile("mov %%rsp, %0" : "=r"(sp)); +#else + __asm__ volatile("mov %%esp, %0" : "=r"(sp)); +#endif + return sp; +} + +uintptr_t hal_cpu_get_address_space(void) { + uintptr_t as; +#if defined(__x86_64__) + __asm__ volatile("mov %%cr3, %0" : "=r"(as)); +#else + __asm__ volatile("mov %%cr3, %0" : "=r"(as)); +#endif + return as; +} + +void hal_cpu_set_kernel_stack(uintptr_t sp_top) { + tss_set_kernel_stack(sp_top); +} + +void hal_cpu_enable_interrupts(void) { + __asm__ volatile("sti"); +} + +void hal_cpu_idle(void) { + __asm__ volatile("hlt"); +} + +#else + +uintptr_t hal_cpu_get_stack_pointer(void) { + return 0; +} + +uintptr_t hal_cpu_get_address_space(void) { + return 0; +} + +void hal_cpu_set_kernel_stack(uintptr_t sp_top) { + (void)sp_top; +} + +void hal_cpu_enable_interrupts(void) { +} + +void hal_cpu_idle(void) { +} + +#endif