From 3b99d1f5884f2c20d187b0802bb350f4647e4bc2 Mon Sep 17 00:00:00 2001 From: Tulio A M Mendes Date: Thu, 5 Feb 2026 19:39:46 -0300 Subject: [PATCH] hal: add cpu helpers (idle/irqs/stack/as) --- include/hal/cpu.h | 23 +++++++++++++++ src/hal/arm/cpu.c | 44 +++++++++++++++++++++++++++++ src/hal/mips/cpu.c | 43 ++++++++++++++++++++++++++++ src/hal/riscv/cpu.c | 44 +++++++++++++++++++++++++++++ src/hal/x86/cpu.c | 68 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 222 insertions(+) create mode 100644 include/hal/cpu.h create mode 100644 src/hal/arm/cpu.c create mode 100644 src/hal/mips/cpu.c create mode 100644 src/hal/riscv/cpu.c create mode 100644 src/hal/x86/cpu.c diff --git a/include/hal/cpu.h b/include/hal/cpu.h new file mode 100644 index 00000000..ed0abd0a --- /dev/null +++ b/include/hal/cpu.h @@ -0,0 +1,23 @@ +// SPDX-License-Identifier: BSD-3-Clause +/* + * Copyright (c) 2018, Tulio A M Mendes + * All rights reserved. + * See LICENSE for details. + * + * Source: https://github.com/tadryanom/AdrOS + */ + +#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 00000000..ea1e57d4 --- /dev/null +++ b/src/hal/arm/cpu.c @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: BSD-3-Clause +/* + * Copyright (c) 2018, Tulio A M Mendes + * All rights reserved. + * See LICENSE for details. + * + * Source: https://github.com/tadryanom/AdrOS + */ + +#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 00000000..0ddfe00e --- /dev/null +++ b/src/hal/mips/cpu.c @@ -0,0 +1,43 @@ +// SPDX-License-Identifier: BSD-3-Clause +/* + * Copyright (c) 2018, Tulio A M Mendes + * All rights reserved. + * See LICENSE for details. + * + * Source: https://github.com/tadryanom/AdrOS + */ + +#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 00000000..dd6d9380 --- /dev/null +++ b/src/hal/riscv/cpu.c @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: BSD-3-Clause +/* + * Copyright (c) 2018, Tulio A M Mendes + * All rights reserved. + * See LICENSE for details. + * + * Source: https://github.com/tadryanom/AdrOS + */ + +#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 00000000..bc7d3902 --- /dev/null +++ b/src/hal/x86/cpu.c @@ -0,0 +1,68 @@ +// SPDX-License-Identifier: BSD-3-Clause +/* + * Copyright (c) 2018, Tulio A M Mendes + * All rights reserved. + * See LICENSE for details. + * + * Source: https://github.com/tadryanom/AdrOS + */ + +#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 -- 2.43.0