From bc6063e588893487c7cacd3bc8c2b9eb814fadd9 Mon Sep 17 00:00:00 2001 From: Eyck-Alexander Jentzsch Date: Thu, 26 Nov 2026 18:52:48 +0100 Subject: [PATCH 1/6] Fixes SMP regression tests on bare-metal RISC-V Limits the teardown watchdog to host ports, preserves the cleanup walk guard, and restores polling of state shared with threads and interrupts. Resets the delayed-suspension calibration counter before each sample. --- test/smp/regression/testcontrol.c | 40 ++++++++++++++++++- .../threadx_event_flag_isr_set_clear_test.c | 2 +- .../threadx_event_flag_isr_wait_abort_test.c | 2 +- .../threadx_smp_preemption_threshold_test.c | 2 +- ..._smp_resume_suspend_ascending_order_test.c | 2 +- ...smp_resume_suspend_descending_order_test.c | 2 +- .../threadx_thread_delayed_suspension_test.c | 20 +++++----- ...ad_multi_level_preemption_threshold_test.c | 2 +- 8 files changed, 55 insertions(+), 17 deletions(-) diff --git a/test/smp/regression/testcontrol.c b/test/smp/regression/testcontrol.c index c964dc03a..9cd84f11f 100644 --- a/test/smp/regression/testcontrol.c +++ b/test/smp/regression/testcontrol.c @@ -31,7 +31,7 @@ #include #include #include -#else +#elif defined(__linux__) #include #include #include @@ -1392,9 +1392,12 @@ TX_THREAD *thread_ptr; puts stdout in line-buffered mode so the surrounding output survives too. --------------------------------------------------------------------------- */ +#define TEST_TEARDOWN_WALK_LIMIT 100000 /* Cap on the unbounded walk in cleanup. */ + +#if defined(_WIN32) || defined(__linux__) + #define TEST_TEARDOWN_TIMEOUT_DEFAULT 60 /* Seconds allowed for teardown. */ #define TEST_TEARDOWN_POLL_MS 250 /* Watchdog poll interval. */ -#define TEST_TEARDOWN_WALK_LIMIT 100000 /* Cap on the unbounded walk in cleanup. */ static const char * volatile test_teardown_stage_name = "test body"; static volatile UINT test_teardown_armed = TX_FALSE; @@ -1668,6 +1671,39 @@ static void test_teardown_stand_down(void) test_teardown_stage("test body"); } +#else + +/* Bare-metal test runners use their external timeout for hung tests. */ +static void test_teardown_watchdog_start(void) +{ +} + +static void test_teardown_stage(const char *stage) +{ + (void) stage; +} + +static void test_teardown_arm(const char *stage) +{ + (void) stage; +} + +static void test_teardown_stand_down(void) +{ +} + +static void test_teardown_abort(const char *reason) +{ + fprintf(stderr, "ThreadX test teardown failed: %s\n", reason); +#ifdef EXTERNAL_EXIT + external_exit(99); +#else + exit(99); +#endif +} + +#endif + /* Define the test control thread. This thread is responsible for dispatching all of the tests in the ThreadX test suite. */ diff --git a/test/smp/regression/threadx_event_flag_isr_set_clear_test.c b/test/smp/regression/threadx_event_flag_isr_set_clear_test.c index 1dd95dc9a..bf8812586 100644 --- a/test/smp/regression/threadx_event_flag_isr_set_clear_test.c +++ b/test/smp/regression/threadx_event_flag_isr_set_clear_test.c @@ -39,7 +39,7 @@ static unsigned long timer_0_counter = 0; static TX_TIMER timer_0; static unsigned long event_flags_set_counter = 0; -static unsigned long condition_count = 0; +static volatile unsigned long condition_count = 0; static TX_EVENT_FLAGS_GROUP event_flags_0; diff --git a/test/smp/regression/threadx_event_flag_isr_wait_abort_test.c b/test/smp/regression/threadx_event_flag_isr_wait_abort_test.c index 3c170fd4a..26f8af54b 100644 --- a/test/smp/regression/threadx_event_flag_isr_wait_abort_test.c +++ b/test/smp/regression/threadx_event_flag_isr_wait_abort_test.c @@ -39,7 +39,7 @@ static unsigned long timer_0_counter = 0; static TX_TIMER timer_0; static unsigned long event_flags_wait_abort_counter = 0; -static unsigned long condition_count = 0; +static volatile unsigned long condition_count = 0; static TX_EVENT_FLAGS_GROUP event_flags_0; diff --git a/test/smp/regression/threadx_smp_preemption_threshold_test.c b/test/smp/regression/threadx_smp_preemption_threshold_test.c index 47935034d..5d9f8e7c5 100644 --- a/test/smp/regression/threadx_smp_preemption_threshold_test.c +++ b/test/smp/regression/threadx_smp_preemption_threshold_test.c @@ -28,7 +28,7 @@ static TX_THREAD thread_25; static TX_THREAD thread_27_pt24; static TX_THREAD thread_31; -static ULONG thread_run_counter[10]; +static volatile ULONG thread_run_counter[10]; static unsigned long error = 0; diff --git a/test/smp/regression/threadx_smp_resume_suspend_ascending_order_test.c b/test/smp/regression/threadx_smp_resume_suspend_ascending_order_test.c index 91a66bfeb..815135ad9 100644 --- a/test/smp/regression/threadx_smp_resume_suspend_ascending_order_test.c +++ b/test/smp/regression/threadx_smp_resume_suspend_ascending_order_test.c @@ -50,7 +50,7 @@ static TX_THREAD thread_29; static TX_THREAD thread_30; static TX_THREAD thread_31; -static ULONG thread_run_counter[32]; +static volatile ULONG thread_run_counter[32]; static unsigned long error = 0; diff --git a/test/smp/regression/threadx_smp_resume_suspend_descending_order_test.c b/test/smp/regression/threadx_smp_resume_suspend_descending_order_test.c index e4d202a53..756f648b5 100644 --- a/test/smp/regression/threadx_smp_resume_suspend_descending_order_test.c +++ b/test/smp/regression/threadx_smp_resume_suspend_descending_order_test.c @@ -50,7 +50,7 @@ static TX_THREAD thread_29; static TX_THREAD thread_30; static TX_THREAD thread_31; -static ULONG thread_run_counter[32]; +static volatile ULONG thread_run_counter[32]; static unsigned long error = 0; diff --git a/test/smp/regression/threadx_thread_delayed_suspension_test.c b/test/smp/regression/threadx_thread_delayed_suspension_test.c index 17a89d038..34c1f9b5f 100644 --- a/test/smp/regression/threadx_thread_delayed_suspension_test.c +++ b/test/smp/regression/threadx_thread_delayed_suspension_test.c @@ -55,20 +55,20 @@ void test_control_return(UINT status); extern VOID (*test_isr_dispatch)(void); static void thread_2_entry(ULONG thread_input); -static UINT delayed_suspend_set; +static volatile UINT delayed_suspend_set; -static ULONG thread_2_counter; -static ULONG thread_2_counter_capture; +static volatile ULONG thread_2_counter; +static volatile ULONG thread_2_counter_capture; -static ULONG min_loop_count; -static ULONG max_loop_count; -static ULONG loop_count; +static volatile ULONG min_loop_count; +static volatile ULONG max_loop_count; +static volatile ULONG loop_count; static volatile ULONG count; static volatile ULONG destination = 0; static ULONG start_time; -static ULONG lower_bound; -static ULONG upper_bound; -static ULONG current_iterations; +static volatile ULONG lower_bound; +static volatile ULONG upper_bound; +static volatile ULONG current_iterations; #ifdef DEBUG_1 static ULONG last_loop_count; #endif @@ -373,6 +373,8 @@ ULONG i; /* Sleep to get a fresh time. */ tx_thread_sleep(1); + /* Exclude counts from the previous calibration interval. */ + loop_count = 0; start_time = _tx_timer_system_clock; do { diff --git a/test/smp/regression/threadx_thread_multi_level_preemption_threshold_test.c b/test/smp/regression/threadx_thread_multi_level_preemption_threshold_test.c index 1eb8f6850..468065259 100644 --- a/test/smp/regression/threadx_thread_multi_level_preemption_threshold_test.c +++ b/test/smp/regression/threadx_thread_multi_level_preemption_threshold_test.c @@ -74,7 +74,7 @@ static TX_TIMER timer_0; /* Define counters. */ -static unsigned long timer_0_counter = 0; +static volatile unsigned long timer_0_counter = 0; static unsigned long thread_1_0_counter = 0; static unsigned long thread_2_1_counter = 0; static unsigned long thread_3_2_counter = 0; From 6e2f3a4aed6d85b29dcfd36e24708382980759e1 Mon Sep 17 00:00:00 2001 From: Eyck-Alexander Jentzsch Date: Fri, 27 Nov 2026 10:50:01 +0100 Subject: [PATCH 2/6] Copied the GNU RISC-V context sources for the SMP ports Preserve the single-core sources from 227586c72edba83c9c876528ef0b2ecc9afdfa37 as the reference for the following SMP changes. --- .../gnu/src/tx_thread_context_restore.S | 367 +++++++++++ .../gnu/src/tx_thread_context_save.S | 249 ++++++++ .../gnu/src/tx_thread_interrupt_control.S | 88 +++ .../risc-v32_smp/gnu/src/tx_thread_schedule.S | 273 ++++++++ .../gnu/src/tx_thread_stack_build.S | 171 +++++ .../gnu/src/tx_thread_system_return.S | 159 +++++ .../gnu/src/tx_thread_context_restore.S | 594 ++++++++++++++++++ .../gnu/src/tx_thread_context_save.S | 417 ++++++++++++ .../gnu/src/tx_thread_interrupt_control.S | 85 +++ .../risc-v64_smp/gnu/src/tx_thread_schedule.S | 439 +++++++++++++ .../gnu/src/tx_thread_stack_build.S | 266 ++++++++ .../gnu/src/tx_thread_system_return.S | 212 +++++++ 12 files changed, 3320 insertions(+) create mode 100644 ports_smp/risc-v32_smp/gnu/src/tx_thread_context_restore.S create mode 100644 ports_smp/risc-v32_smp/gnu/src/tx_thread_context_save.S create mode 100644 ports_smp/risc-v32_smp/gnu/src/tx_thread_interrupt_control.S create mode 100644 ports_smp/risc-v32_smp/gnu/src/tx_thread_schedule.S create mode 100644 ports_smp/risc-v32_smp/gnu/src/tx_thread_stack_build.S create mode 100644 ports_smp/risc-v32_smp/gnu/src/tx_thread_system_return.S create mode 100644 ports_smp/risc-v64_smp/gnu/src/tx_thread_context_restore.S create mode 100644 ports_smp/risc-v64_smp/gnu/src/tx_thread_context_save.S create mode 100644 ports_smp/risc-v64_smp/gnu/src/tx_thread_interrupt_control.S create mode 100644 ports_smp/risc-v64_smp/gnu/src/tx_thread_schedule.S create mode 100644 ports_smp/risc-v64_smp/gnu/src/tx_thread_stack_build.S create mode 100644 ports_smp/risc-v64_smp/gnu/src/tx_thread_system_return.S diff --git a/ports_smp/risc-v32_smp/gnu/src/tx_thread_context_restore.S b/ports_smp/risc-v32_smp/gnu/src/tx_thread_context_restore.S new file mode 100644 index 000000000..c48546433 --- /dev/null +++ b/ports_smp/risc-v32_smp/gnu/src/tx_thread_context_restore.S @@ -0,0 +1,367 @@ +/*************************************************************************** + * Copyright (c) 2025 10xEngineers + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Component */ +/** */ +/** Thread */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + + .section .text + +/* Trap frame layout (stack grows down, all totals mod 16 = 0): + FP build (double ABI): integer slots 0-31 at i*4 (0-127; slot 31 is pad), + f0-f31 as doubles at 128 + i*8 (128-383), fcsr at 384, pad 388-399. + SOFT_FLOAT build: integer slots 0-31 at i*4 only. */ +#include "tx_port.h" + .equ TX_TRAP_FRAME_SIZE, TX_RISCV_TRAP_FRAME_SIZE + +/**************************************************************************/ +/* */ +/* FUNCTION RELEASE */ +/* */ +/* _tx_thread_context_restore RISC-V32/GNU */ +/* 6.4.x */ +/* AUTHOR */ +/* */ +/* Akif Ejaz, 10xEngineers */ +/* Wei-Chen Lai, National Cheng Kung University */ +/* */ +/* DESCRIPTION */ +/* */ +/* This function restores the interrupt context if it is processing a */ +/* nested interrupt. If not, it returns to the interrupt thread if no */ +/* preemption is necessary. Otherwise, if preemption is necessary or */ +/* if no thread was running, the function returns to the scheduler. */ +/* */ +/* INPUT */ +/* */ +/* None */ +/* */ +/* OUTPUT */ +/* */ +/* None */ +/* */ +/* CALLS */ +/* */ +/* _tx_thread_schedule Thread scheduling routine */ +/* */ +/* CALLED BY */ +/* */ +/* ISRs Interrupt Service Routines */ +/* */ +/**************************************************************************/ +/* VOID _tx_thread_context_restore(VOID) +{ */ + .global _tx_thread_context_restore +_tx_thread_context_restore: + + /* Lockout interrupts. */ + + csrci mstatus, 0x08 // Disable interrupts (MIE bit 3) + +#ifdef TX_ENABLE_EXECUTION_CHANGE_NOTIFY + call _tx_execution_isr_exit // Call the ISR execution exit function +#endif + + /* Determine if interrupts are nested. */ + /* if (--_tx_thread_system_state) + { */ + + la t0, _tx_thread_system_state // Pickup addr of nested interrupt count + lw t1, 0(t0) // Pickup nested interrupt count + addi t1, t1, -1 // Decrement the nested interrupt counter + sw t1, 0(t0) // Store new nested count + beqz t1, _tx_thread_not_nested_restore // If 0, not nested restore + + /* Interrupts are nested. */ + + /* Just recover the saved registers and return to the point of + interrupt. */ + + /* Recover floating point registers only if saved mstatus.FS was not Off. */ +#if defined(__riscv_float_abi_double) + lw t1, 29*4(sp) // Pickup saved mstatus + srli t1, t1, 13 + andi t1, t1, 0x3 + beqz t1, _tx_thread_skip_fp_restore // Skip if FS was Off + fld f0, 16*8(sp) // Recover ft0 + fld f1, 17*8(sp) // Recover ft1 + fld f2, 18*8(sp) // Recover ft2 + fld f3, 19*8(sp) // Recover ft3 + fld f4, 20*8(sp) // Recover ft4 + fld f5, 21*8(sp) // Recover ft5 + fld f6, 22*8(sp) // Recover ft6 + fld f7, 23*8(sp) // Recover ft7 + fld f10, 26*8(sp) // Recover fa0 + fld f11, 27*8(sp) // Recover fa1 + fld f12, 28*8(sp) // Recover fa2 + fld f13, 29*8(sp) // Recover fa3 + fld f14, 30*8(sp) // Recover fa4 + fld f15, 31*8(sp) // Recover fa5 + fld f16, 32*8(sp) // Recover fa6 + fld f17, 33*8(sp) // Recover fa7 + fld f28, 44*8(sp) // Recover ft8 + fld f29, 45*8(sp) // Recover ft9 + fld f30, 46*8(sp) // Recover ft10 + fld f31, 47*8(sp) // Recover ft11 + lw t0, 96*4(sp) // Recover fcsr + csrw fcsr, t0 // Restore fcsr +#endif +_tx_thread_skip_fp_restore: + + /* Recover standard registers. */ + + /* Restore registers, + Skip global pointer because that does not change. + Also skip the saved registers since they have been restored by any function we called, + except s0 since we use it ourselves. */ + + lw t0, 30*4(sp) // Recover mepc + csrw mepc, t0 // Setup mepc + + /* Compose mstatus via read/modify/write to avoid clobbering unrelated bits. + Set MPIE and restore MPP to Machine, preserve other fields. */ + + csrr t1, mstatus + + /* Clear MPP/MPIE/MIE bits in t1 then set desired values. */ + + li t2, 0x1888 // MPP(0x1800) | MPIE(0x80) | MIE(0x08) + li t3, 0x1880 // Set MPP=Machine (0x1800) + MPIE=1 (0x80); mret sets MIE=MPIE + + /* Construct new mstatus in t1: clear mask bits, set MPP/MPIE and optionally FP bit, + preserve everything except the bits we will modify. */ + + li t4, ~0x1888 // Clear mask for MPP/MPIE/MIE + and t1, t1, t4 + or t1, t1, t3 + +#if defined(__riscv_float_abi_double) + li t0, 0x2000 // Set FS bits (bits 14:13 to 01) for FP state + or t1, t1, t0 +#endif + csrw mstatus, t1 // Update mstatus safely + + lw ra, 28*4(sp) // Recover return address + lw t0, 19*4(sp) // Recover t0 + lw t1, 18*4(sp) // Recover t1 + lw t2, 17*4(sp) // Recover t2 + lw s0, 12*4(sp) // Recover s0 + lw a0, 27*4(sp) // Recover a0 + lw a1, 26*4(sp) // Recover a1 + lw a2, 25*4(sp) // Recover a2 + lw a3, 24*4(sp) // Recover a3 + lw a4, 23*4(sp) // Recover a4 + lw a5, 22*4(sp) // Recover a5 + lw a6, 21*4(sp) // Recover a6 + lw a7, 20*4(sp) // Recover a7 + lw t3, 16*4(sp) // Recover t3 + lw t4, 15*4(sp) // Recover t4 + lw t5, 14*4(sp) // Recover t5 + lw t6, 13*4(sp) // Recover t6 + + addi sp, sp, TX_TRAP_FRAME_SIZE // Recover stack frame + mret // Return to point of interrupt + + /* } */ +_tx_thread_not_nested_restore: + /* Determine if a thread was interrupted and no preemption is required. */ + /* else if (((_tx_thread_current_ptr) && (_tx_thread_current_ptr == _tx_thread_execute_ptr) + || (_tx_thread_preempt_disable)) + { */ + + la t0, _tx_thread_current_ptr // Pickup current thread pointer address + lw t1, 0(t0) // Pickup current thread pointer + + beqz t1, _tx_thread_idle_system_restore // If NULL, idle system restore + + + la t0, _tx_thread_preempt_disable // Pickup preempt disable flag address + lw t2, 0(t0) // Pickup preempt disable flag (UINT) + + bnez t2, _tx_thread_no_preempt_restore // If set, restore interrupted thread + + + la t0, _tx_thread_execute_ptr // Pickup thread execute pointer address + lw t2, 0(t0) // Pickup thread execute pointer + + bne t1, t2, _tx_thread_preempt_restore // If higher-priority thread is ready, preempt + + +_tx_thread_no_preempt_restore: + /* Restore interrupted thread or ISR. */ + + /* Pickup the saved stack pointer. */ + /* sp = _tx_thread_current_ptr -> tx_thread_stack_ptr; */ + + lw sp, 8(t1) // Switch back to thread's stack + + /* Recover floating point registers only if saved mstatus.FS was not Off. */ +#if defined(__riscv_float_abi_double) + lw t3, 29*4(sp) // Pickup saved mstatus + srli t3, t3, 13 + andi t3, t3, 0x3 + beqz t3, _tx_thread_no_preempt_skip_fp_restore // Skip if FS was Off + fld f0, 16*8(sp) // Recover ft0 + fld f1, 17*8(sp) // Recover ft1 + fld f2, 18*8(sp) // Recover ft2 + fld f3, 19*8(sp) // Recover ft3 + fld f4, 20*8(sp) // Recover ft4 + fld f5, 21*8(sp) // Recover ft5 + fld f6, 22*8(sp) // Recover ft6 + fld f7, 23*8(sp) // Recover ft7 + fld f10, 26*8(sp) // Recover fa0 + fld f11, 27*8(sp) // Recover fa1 + fld f12, 28*8(sp) // Recover fa2 + fld f13, 29*8(sp) // Recover fa3 + fld f14, 30*8(sp) // Recover fa4 + fld f15, 31*8(sp) // Recover fa5 + fld f16, 32*8(sp) // Recover fa6 + fld f17, 33*8(sp) // Recover fa7 + fld f28, 44*8(sp) // Recover ft8 + fld f29, 45*8(sp) // Recover ft9 + fld f30, 46*8(sp) // Recover ft10 + fld f31, 47*8(sp) // Recover ft11 + lw t0, 96*4(sp) // Recover fcsr + csrw fcsr, t0 // Restore fcsr +#endif +_tx_thread_no_preempt_skip_fp_restore: + + /* Recover the saved context and return to the point of interrupt. */ + + /* Recover standard registers. */ + /* Restore registers, + Skip global pointer because that does not change */ + + lw t0, 30*4(sp) // Recover mepc + csrw mepc, t0 // Setup mepc + + /* Compose mstatus via read/modify/write to avoid clobbering unrelated bits. */ + + csrr t1, mstatus + li t2, 0x1888 // MPP(0x1800) | MPIE(0x80) | MIE(0x08) + li t3, 0x1880 // Set MPP=Machine (0x1800) + MPIE=1 (0x80); mret sets MIE=MPIE + li t4, ~0x1888 // Clear mask for MPP/MPIE/MIE + and t1, t1, t4 + or t1, t1, t3 + +#if defined(__riscv_float_abi_double) + li t0, 0x2000 // Set FS bits for FP state + or t1, t1, t0 +#endif + csrw mstatus, t1 // Update mstatus safely + + lw ra, 28*4(sp) // Recover return address + lw t0, 19*4(sp) // Recover t0 + lw t1, 18*4(sp) // Recover t1 + lw t2, 17*4(sp) // Recover t2 + lw s0, 12*4(sp) // Recover s0 + lw a0, 27*4(sp) // Recover a0 + lw a1, 26*4(sp) // Recover a1 + lw a2, 25*4(sp) // Recover a2 + lw a3, 24*4(sp) // Recover a3 + lw a4, 23*4(sp) // Recover a4 + lw a5, 22*4(sp) // Recover a5 + lw a6, 21*4(sp) // Recover a6 + lw a7, 20*4(sp) // Recover a7 + lw t3, 16*4(sp) // Recover t3 + lw t4, 15*4(sp) // Recover t4 + lw t5, 14*4(sp) // Recover t5 + lw t6, 13*4(sp) // Recover t6 + + addi sp, sp, TX_TRAP_FRAME_SIZE // Recover stack frame + mret // Return to point of interrupt + + /* } + else + { */ +_tx_thread_preempt_restore: + /* Instead of directly activating the thread again, ensure we save the + entire stack frame by saving the remaining registers. */ + + lw t0, 8(t1) // Pickup thread's stack pointer + ori t3, zero, 1 // Build interrupt stack type + sw t3, 0(t0) // Store stack type + + /* Store floating point preserved registers only if saved mstatus.FS was not Off. */ +#if defined(__riscv_float_abi_double) + lw t3, 29*4(t0) // Pickup saved mstatus + srli t3, t3, 13 + andi t3, t3, 0x3 + beqz t3, _tx_thread_preempt_skip_fp_restore // Skip if FS was Off + fsd f8, 192(t0) // Store fs0 + fsd f9, 200(t0) // Store fs1 + fsd f18, 272(t0) // Store fs2 + fsd f19, 280(t0) // Store fs3 + fsd f20, 288(t0) // Store fs4 + fsd f21, 296(t0) // Store fs5 + fsd f22, 304(t0) // Store fs6 + fsd f23, 312(t0) // Store fs7 + fsd f24, 320(t0) // Store fs8 + fsd f25, 328(t0) // Store fs9 + fsd f26, 336(t0) // Store fs10 + fsd f27, 344(t0) // Store fs11 +#endif +_tx_thread_preempt_skip_fp_restore: + + /* Store standard preserved registers. */ + + sw x9, 11*4(t0) // Store s1 + sw x18, 10*4(t0) // Store s2 + sw x19, 9*4(t0) // Store s3 + sw x20, 8*4(t0) // Store s4 + sw x21, 7*4(t0) // Store s5 + sw x22, 6*4(t0) // Store s6 + sw x23, 5*4(t0) // Store s7 + sw x24, 4*4(t0) // Store s8 + sw x25, 3*4(t0) // Store s9 + sw x26, 2*4(t0) // Store s10 + sw x27, 1*4(t0) // Store s11 + // Note: s0 is already stored! + + /* Save the remaining time-slice and disable it. */ + /* if (_tx_timer_time_slice) + { */ + + la t0, _tx_timer_time_slice // Pickup time slice variable address + lw t2, 0(t0) // Pickup time slice + beqz t2, _tx_thread_dont_save_ts // If 0, skip time slice processing + + /* _tx_thread_current_ptr -> tx_thread_time_slice = _tx_timer_time_slice + _tx_timer_time_slice = 0; */ + + sw t2, 24(t1) // Save current time slice + sw x0, 0(t0) // Clear global time slice + + + /* } */ +_tx_thread_dont_save_ts: + /* Clear the current task pointer. */ + /* _tx_thread_current_ptr = TX_NULL; */ + + /* Return to the scheduler. */ + /* _tx_thread_schedule(); */ + + la t0, _tx_thread_current_ptr // Pickup current thread pointer address + sw x0, 0(t0) // Clear current thread pointer + + /* } */ + +_tx_thread_idle_system_restore: + /* Just return back to the scheduler! */ + j _tx_thread_schedule // Return to scheduler + +/* } */ diff --git a/ports_smp/risc-v32_smp/gnu/src/tx_thread_context_save.S b/ports_smp/risc-v32_smp/gnu/src/tx_thread_context_save.S new file mode 100644 index 000000000..e93d5870d --- /dev/null +++ b/ports_smp/risc-v32_smp/gnu/src/tx_thread_context_save.S @@ -0,0 +1,249 @@ +/*************************************************************************** + * Copyright (c) 2025 10xEngineers + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +// Portions of this file were generated with AI assistance. + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Component */ +/** */ +/** Thread */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + + .section .text + +#include "tx_port.h" + .equ TX_TRAP_FRAME_SIZE, TX_RISCV_TRAP_FRAME_SIZE + +/**************************************************************************/ +/* */ +/* FUNCTION RELEASE */ +/* */ +/* _tx_thread_context_save RISC-V32/GNU */ +/* 6.4.x */ +/* AUTHOR */ +/* */ +/* Akif Ejaz, 10xEngineers */ +/* Wei-Chen Lai, National Cheng Kung University */ +/* */ +/* DESCRIPTION */ +/* */ +/* This function saves the context of an executing thread in the */ +/* beginning of interrupt processing. The function also ensures that */ +/* the system stack is used upon return to the calling ISR. */ +/* */ +/* INPUT */ +/* */ +/* None */ +/* */ +/* OUTPUT */ +/* */ +/* None */ +/* */ +/* CALLS */ +/* */ +/* None */ +/* */ +/* CALLED BY */ +/* */ +/* ISRs */ +/* */ +/**************************************************************************/ +/* VOID _tx_thread_context_save(VOID) +{ */ + .global _tx_thread_context_save +_tx_thread_context_save: + + /* Upon entry to this routine, RA/x1 has been saved on the stack + and the stack has been already allocated for the entire context: + addi sp, sp, -TX_TRAP_FRAME_SIZE (400 with FP, 128 without) + sw ra, 28*4(sp) + */ + + sw t0, 19*4(sp) // Store t0 + sw t1, 18*4(sp) // Store t1 + + /* Check for a nested interrupt. */ + /* if (_tx_thread_system_state++) + { */ + + la t0, _tx_thread_system_state // Pickup addr of system state var + lw t1, 0(t0) // Pickup system state + addi t1, t1, 1 // Increment system state + sw t1, 0(t0) // Store system state + li t0, 1 + bgt t1, t0, _tx_thread_nested_save // If it's more than 1, nested interrupt + + /* First level interrupt, save the rest of the scratch registers and + check for a thread to preempt. */ + + sw t2, 17*4(sp) // Store t2 + sw s0, 12*4(sp) // Store s0 + sw a0, 27*4(sp) // Store a0 + sw a1, 26*4(sp) // Store a1 + sw a2, 25*4(sp) // Store a2 + sw a3, 24*4(sp) // Store a3 + sw a4, 23*4(sp) // Store a4 + sw a5, 22*4(sp) // Store a5 + sw a6, 21*4(sp) // Store a6 + sw a7, 20*4(sp) // Store a7 + sw t3, 16*4(sp) // Store t3 + sw t4, 15*4(sp) // Store t4 + sw t5, 14*4(sp) // Store t5 + sw t6, 13*4(sp) // Store t6 + + /* Save mstatus and skip FP state if FS is Off. */ + csrr t0, mstatus + sw t0, 29*4(sp) +#if defined(__riscv_float_abi_double) + srli t1, t0, 13 + andi t1, t1, 0x3 + beqz t1, _tx_thread_skip_fpu_save + + /* Save floating point registers (doubles at 128 + i*8). */ + fsd f0, 16*8(sp) // Store ft0 + fsd f1, 17*8(sp) // Store ft1 + fsd f2, 18*8(sp) // Store ft2 + fsd f3, 19*8(sp) // Store ft3 + fsd f4, 20*8(sp) // Store ft4 + fsd f5, 21*8(sp) // Store ft5 + fsd f6, 22*8(sp) // Store ft6 + fsd f7, 23*8(sp) // Store ft7 + fsd f10, 26*8(sp) // Store fa0 + fsd f11, 27*8(sp) // Store fa1 + fsd f12, 28*8(sp) // Store fa2 + fsd f13, 29*8(sp) // Store fa3 + fsd f14, 30*8(sp) // Store fa4 + fsd f15, 31*8(sp) // Store fa5 + fsd f16, 32*8(sp) // Store fa6 + fsd f17, 33*8(sp) // Store fa7 + fsd f28, 44*8(sp) // Store ft8 + fsd f29, 45*8(sp) // Store ft9 + fsd f30, 46*8(sp) // Store ft10 + fsd f31, 47*8(sp) // Store ft11 + csrr t0, fcsr + sw t0, 96*4(sp) // Store fcsr +#endif +_tx_thread_skip_fpu_save: + + csrr t0, mepc + sw t0, 30*4(sp) // Save it on the stack + + la t1, _tx_thread_current_ptr // Pickup address of current thread ptr + lw t2, 0(t1) // Pickup current thread pointer + beqz t2, _tx_thread_idle_system_save // If NULL, idle system was interrupted + + /* Save the current thread's stack pointer and switch to the system stack. */ + /* _tx_thread_current_ptr -> tx_thread_stack_ptr = sp; + sp = _tx_thread_system_stack_ptr; */ + + sw sp, 8(t2) // Save stack pointer + la t0, _tx_thread_system_stack_ptr + lw sp, 0(t0) // Switch to system stack + + /* Call the ISR execution exit function if enabled. */ +#ifdef TX_ENABLE_EXECUTION_CHANGE_NOTIFY + addi sp, sp, -16 // Reserve space to save the return address + sw ra, 0(sp) // Save the return address + call _tx_execution_isr_enter // Call the ISR execution enter function + lw ra, 0(sp) // Recover the return address + addi sp, sp, 16 // Recover the reserved stack space +#endif + + ret // Return to ISR + +_tx_thread_nested_save: + + /* Nested interrupt! Just save the scratch registers and return to the ISR. */ + + sw t2, 17*4(sp) // Store t2 + sw s0, 12*4(sp) // Store s0 + sw a0, 27*4(sp) // Store a0 + sw a1, 26*4(sp) // Store a1 + sw a2, 25*4(sp) // Store a2 + sw a3, 24*4(sp) // Store a3 + sw a4, 23*4(sp) // Store a4 + sw a5, 22*4(sp) // Store a5 + sw a6, 21*4(sp) // Store a6 + sw a7, 20*4(sp) // Store a7 + sw t3, 16*4(sp) // Store t3 + sw t4, 15*4(sp) // Store t4 + sw t5, 14*4(sp) // Store t5 + sw t6, 13*4(sp) // Store t6 + + /* Save mstatus and skip FP state if FS is Off. */ + csrr t0, mstatus + sw t0, 29*4(sp) +#if defined(__riscv_float_abi_double) + srli t1, t0, 13 + andi t1, t1, 0x3 + beqz t1, _tx_thread_skip_nested_fpu_save + + /* Save floating point registers (doubles at 128 + i*8). */ + fsd f0, 16*8(sp) // Store ft0 + fsd f1, 17*8(sp) // Store ft1 + fsd f2, 18*8(sp) // Store ft2 + fsd f3, 19*8(sp) // Store ft3 + fsd f4, 20*8(sp) // Store ft4 + fsd f5, 21*8(sp) // Store ft5 + fsd f6, 22*8(sp) // Store ft6 + fsd f7, 23*8(sp) // Store ft7 + fsd f10, 26*8(sp) // Store fa0 + fsd f11, 27*8(sp) // Store fa1 + fsd f12, 28*8(sp) // Store fa2 + fsd f13, 29*8(sp) // Store fa3 + fsd f14, 30*8(sp) // Store fa4 + fsd f15, 31*8(sp) // Store fa5 + fsd f16, 32*8(sp) // Store fa6 + fsd f17, 33*8(sp) // Store fa7 + fsd f28, 44*8(sp) // Store ft8 + fsd f29, 45*8(sp) // Store ft9 + fsd f30, 46*8(sp) // Store ft10 + fsd f31, 47*8(sp) // Store ft11 + csrr t0, fcsr + sw t0, 96*4(sp) // Store fcsr +#endif +_tx_thread_skip_nested_fpu_save: + + csrr t0, mepc + sw t0, 30*4(sp) // Save it on stack + + /* Call the ISR execution exit function if enabled. */ +#ifdef TX_ENABLE_EXECUTION_CHANGE_NOTIFY + addi sp, sp, -16 // Reserve space to save the return address + sw ra, 0(sp) // Save the return address + call _tx_execution_isr_enter // Call the ISR execution enter function + lw ra, 0(sp) // Recover the return address + addi sp, sp, 16 // Recover the reserved stack space +#endif + + ret // Return to ISR + +_tx_thread_idle_system_save: + + +#ifdef TX_ENABLE_EXECUTION_CHANGE_NOTIFY + addi sp, sp, -16 // Reserve space to save the return address + sw ra, 0(sp) // Save the return address + call _tx_execution_isr_enter // Call the ISR execution enter function + lw ra, 0(sp) // Recover the return address + addi sp, sp, 16 // Recover the reserved stack space +#endif + + /* Interrupt occurred in the scheduling loop. */ + + /* } +} */ + addi sp, sp, TX_TRAP_FRAME_SIZE // Recover the reserved stack space + ret // Return to calling ISR diff --git a/ports_smp/risc-v32_smp/gnu/src/tx_thread_interrupt_control.S b/ports_smp/risc-v32_smp/gnu/src/tx_thread_interrupt_control.S new file mode 100644 index 000000000..aab2955b5 --- /dev/null +++ b/ports_smp/risc-v32_smp/gnu/src/tx_thread_interrupt_control.S @@ -0,0 +1,88 @@ +/*************************************************************************** + * Copyright (c) 2025 10xEngineers + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Component */ +/** */ +/** Thread */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + + + .section .text +/**************************************************************************/ +/* */ +/* FUNCTION RELEASE */ +/* */ +/* _tx_thread_interrupt_control RISC-V32/GNU */ +/* 6.4.x */ +/* AUTHOR */ +/* */ +/* Akif Ejaz, 10xEngineers */ +/* */ +/* DESCRIPTION */ +/* */ +/* This function is responsible for changing the interrupt lockout */ +/* posture of the system. */ +/* */ +/* INPUT */ +/* */ +/* new_posture New interrupt lockout posture */ +/* */ +/* OUTPUT */ +/* */ +/* old_posture Old interrupt lockout posture */ +/* */ +/* CALLS */ +/* */ +/* None */ +/* */ +/* CALLED BY */ +/* */ +/* Application Code */ +/* */ +/**************************************************************************/ +/* UINT _tx_thread_interrupt_control(UINT new_posture) +{ */ + .global _tx_thread_interrupt_control +_tx_thread_interrupt_control: + + /* Pickup current interrupt posture. */ + + csrr a1, mstatus // Pickup mstatus + andi a1, a1, 0x08 // Mask out all but MIE + + /* Check for the new posture. */ + + beqz a0, _tx_thread_interrupt_disable // If 0, disable interrupts + + /* Enable interrupts. */ + + csrsi mstatus, 0x08 // Enable interrupts (MIE bit 3) + j _tx_thread_interrupt_control_exit // Return to caller + +_tx_thread_interrupt_disable: + + /* Disable interrupts. */ + + csrci mstatus, 0x08 // Disable interrupts (MIE bit 3) + +_tx_thread_interrupt_control_exit: + + /* Return the old interrupt posture. */ + + mv a0, a1 // Setup return value + ret // Return to caller + +/* } */ diff --git a/ports_smp/risc-v32_smp/gnu/src/tx_thread_schedule.S b/ports_smp/risc-v32_smp/gnu/src/tx_thread_schedule.S new file mode 100644 index 000000000..7d2c41917 --- /dev/null +++ b/ports_smp/risc-v32_smp/gnu/src/tx_thread_schedule.S @@ -0,0 +1,273 @@ +/*************************************************************************** + * Copyright (c) 2025 10xEngineers + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Component */ +/** */ +/** Thread */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + + + .section .text + +#include "tx_port.h" + .equ TX_TRAP_FRAME_SIZE, TX_RISCV_TRAP_FRAME_SIZE + .equ TX_SOL_FRAME_SIZE, TX_RISCV_SOL_FRAME_SIZE + +/**************************************************************************/ +/* */ +/* FUNCTION RELEASE */ +/* */ +/* _tx_thread_schedule RISC-V32/GNU */ +/* 6.4.x */ +/* AUTHOR */ +/* */ +/* Akif Ejaz, 10xEngineers */ +/* */ +/* DESCRIPTION */ +/* */ +/* This function waits for a thread control block pointer to appear in */ +/* the _tx_thread_execute_ptr variable. Once a thread pointer appears */ +/* in the variable, the corresponding thread is resumed. */ +/* */ +/* INPUT */ +/* */ +/* None */ +/* */ +/* OUTPUT */ +/* */ +/* None */ +/* */ +/* CALLS */ +/* */ +/* None */ +/* */ +/* CALLED BY */ +/* */ +/* _tx_initialize_kernel_enter ThreadX entry function */ +/* _tx_thread_system_return Return to system from thread */ +/* _tx_thread_context_restore Restore thread's context */ +/* */ +/**************************************************************************/ +/* VOID _tx_thread_schedule(VOID) +{ */ + .global _tx_thread_schedule +_tx_thread_schedule: + + /* Enable interrupts. */ + + csrsi mstatus, 0x08 // Enable interrupts (MIE bit 3) + + /* Wait for a thread to execute. */ + /* do + { */ +_tx_thread_schedule_loop: + + la t0, _tx_thread_execute_ptr // Pickup address of execute ptr + lw t1, 0(t0) // Pickup execute pointer + bnez t1, _tx_thread_ready_to_run // If non-NULL, a thread is ready to run + +#ifndef TX_NO_WFI + wfi // Wait for interrupt +#endif + j _tx_thread_schedule_loop // Check again + + /* } + while (_tx_thread_execute_ptr == TX_NULL); */ + +_tx_thread_ready_to_run: + + /* At this point, t1 contains the pointer to the thread to execute. + Lockout interrupts. */ + + csrci mstatus, 0x08 // Disable interrupts (MIE bit 3) + + /* Check _tx_thread_execute_ptr again, in case an interrupt occurred + between the check and the disable. */ + + lw t1, 0(t0) // Pickup execute pointer + beqz t1, _tx_thread_schedule_loop // If NULL, go back to wait loop + + /* Yes! We have a thread to execute. */ + /* _tx_thread_current_ptr = _tx_thread_execute_ptr; */ + + la t0, _tx_thread_current_ptr // Pickup address of current thread + sw t1, 0(t0) // Setup current thread pointer + + /* Increment the run count for this thread. */ + /* _tx_thread_current_ptr -> tx_thread_run_count++; */ + + lw t2, 4(t1) // Pickup run count + addi t2, t2, 1 // Increment run count + sw t2, 4(t1) // Store run count + + /* Setup time-slice values. */ + /* _tx_timer_time_slice = _tx_thread_current_ptr -> tx_thread_time_slice; */ + + lw t2, 24(t1) // Pickup thread time-slice + la t3, _tx_timer_time_slice // Pickup address of time-slice + sw t2, 0(t3) // Setup time-slice + + /* Call the thread execution enter function if enabled. */ +#ifdef TX_ENABLE_EXECUTION_CHANGE_NOTIFY + + call _tx_execution_thread_enter // Call the thread execution enter function +#endif + + /* Switch to the thread's stack. */ + /* sp = _tx_thread_current_ptr -> tx_thread_stack_ptr; */ + + lw sp, 8(t1) // Switch to thread stack + + /* Determine the type of stack frame. */ + /* if (*sp) + { */ + + lw t0, 0*4(sp) // Pickup stack type + beqz t0, _tx_thread_solicited_return // If 0, solicited return + + /* Recover floating point registers only if saved mstatus.FS was not Off. */ +#if defined(__riscv_float_abi_double) + lw t1, 29*4(sp) // Pickup saved mstatus + srli t1, t1, 13 + andi t1, t1, 0x3 + beqz t1, _tx_thread_schedule_skip_fp_restore // Skip if FS was Off + fld f0, 16*8(sp) // Recover ft0 + fld f1, 17*8(sp) // Recover ft1 + fld f2, 18*8(sp) // Recover ft2 + fld f3, 19*8(sp) // Recover ft3 + fld f4, 20*8(sp) // Recover ft4 + fld f5, 21*8(sp) // Recover ft5 + fld f6, 22*8(sp) // Recover ft6 + fld f7, 23*8(sp) // Recover ft7 + fld f8, 24*8(sp) // Recover fs0 + fld f9, 25*8(sp) // Recover fs1 + fld f10, 26*8(sp) // Recover fa0 + fld f11, 27*8(sp) // Recover fa1 + fld f12, 28*8(sp) // Recover fa2 + fld f13, 29*8(sp) // Recover fa3 + fld f14, 30*8(sp) // Recover fa4 + fld f15, 31*8(sp) // Recover fa5 + fld f16, 32*8(sp) // Recover fa6 + fld f17, 33*8(sp) // Recover fa7 + fld f18, 34*8(sp) // Recover fs2 + fld f19, 35*8(sp) // Recover fs3 + fld f20, 36*8(sp) // Recover fs4 + fld f21, 37*8(sp) // Recover fs5 + fld f22, 38*8(sp) // Recover fs6 + fld f23, 39*8(sp) // Recover fs7 + fld f24, 40*8(sp) // Recover fs8 + fld f25, 41*8(sp) // Recover fs9 + fld f26, 42*8(sp) // Recover fs10 + fld f27, 43*8(sp) // Recover fs11 + fld f28, 44*8(sp) // Recover ft8 + fld f29, 45*8(sp) // Recover ft9 + fld f30, 46*8(sp) // Recover ft10 + fld f31, 47*8(sp) // Recover ft11 + lw t0, 96*4(sp) // Recover fcsr + csrw fcsr, t0 // Restore fcsr +_tx_thread_schedule_skip_fp_restore: +#endif + + /* Recover standard registers. */ + lw t0, 30*4(sp) // Recover mepc + csrw mepc, t0 // Setup mepc + + li t0, 0x1880 // Prepare mstatus: MPP=Machine(0x1800) | MPIE(0x80) +#if defined(__riscv_float_abi_double) + li t1, 0x2000 // Set FS bits for FP state + or t0, t0, t1 +#endif + csrw mstatus, t0 // Set mstatus + + lw ra, 28*4(sp) // Recover return address + lw t0, 19*4(sp) // Recover t0 + lw t1, 18*4(sp) // Recover t1 + lw t2, 17*4(sp) // Recover t2 + lw s0, 12*4(sp) // Recover s0 + lw x9, 11*4(sp) // Recover s1 + lw a0, 27*4(sp) // Recover a0 + lw a1, 26*4(sp) // Recover a1 + lw a2, 25*4(sp) // Recover a2 + lw a3, 24*4(sp) // Recover a3 + lw a4, 23*4(sp) // Recover a4 + lw a5, 22*4(sp) // Recover a5 + lw a6, 21*4(sp) // Recover a6 + lw a7, 20*4(sp) // Recover a7 + lw t3, 16*4(sp) // Recover t3 + lw t4, 15*4(sp) // Recover t4 + lw t5, 14*4(sp) // Recover t5 + lw t6, 13*4(sp) // Recover t6 + lw x18, 10*4(sp) // Recover s2 + lw x19, 9*4(sp) // Recover s3 + lw x20, 8*4(sp) // Recover s4 + lw x21, 7*4(sp) // Recover s5 + lw x22, 6*4(sp) // Recover s6 + lw x23, 5*4(sp) // Recover s7 + lw x24, 4*4(sp) // Recover s8 + lw x25, 3*4(sp) // Recover s9 + lw x26, 2*4(sp) // Recover s10 + lw x27, 1*4(sp) // Recover s11 + + addi sp, sp, TX_TRAP_FRAME_SIZE // Recover stack frame + mret // Return to thread + +_tx_thread_solicited_return: + + /* Recover floating point registers only if saved mstatus.FS was not Off. */ +#if defined(__riscv_float_abi_double) + lw t1, 14*4(sp) // Pickup saved mstatus + srli t1, t1, 13 + andi t1, t1, 0x3 + beqz t1, _tx_thread_schedule_solicited_skip_fp_restore // Skip if FS was Off + fld f8, 8*8(sp) // Recover fs0 + fld f9, 9*8(sp) // Recover fs1 + fld f18, 10*8(sp) // Recover fs2 + fld f19, 11*8(sp) // Recover fs3 + fld f20, 12*8(sp) // Recover fs4 + fld f21, 13*8(sp) // Recover fs5 + fld f22, 14*8(sp) // Recover fs6 + fld f23, 15*8(sp) // Recover fs7 + fld f24, 16*8(sp) // Recover fs8 + fld f25, 17*8(sp) // Recover fs9 + fld f26, 18*8(sp) // Recover fs10 + fld f27, 19*8(sp) // Recover fs11 + lw t0, 40*4(sp) // Recover fcsr + csrw fcsr, t0 // Restore fcsr +_tx_thread_schedule_solicited_skip_fp_restore: +#endif + + /* Recover standard registers. */ + lw t0, 14*4(sp) // Reload saved mstatus (t0 may hold fcsr if FP was restored) + csrw mstatus, t0 // Restore mstatus + + lw ra, 13*4(sp) // Recover return address + lw s0, 12*4(sp) // Recover s0 + lw s1, 11*4(sp) // Recover s1 + lw x18, 10*4(sp) // Recover s2 + lw x19, 9*4(sp) // Recover s3 + lw x20, 8*4(sp) // Recover s4 + lw x21, 7*4(sp) // Recover s5 + lw x22, 6*4(sp) // Recover s6 + lw x23, 5*4(sp) // Recover s7 + lw x24, 4*4(sp) // Recover s8 + lw x25, 3*4(sp) // Recover s9 + lw x26, 2*4(sp) // Recover s10 + lw x27, 1*4(sp) // Recover s11 + + addi sp, sp, TX_SOL_FRAME_SIZE // Recover stack frame + ret // Return to thread + +/* } */ diff --git a/ports_smp/risc-v32_smp/gnu/src/tx_thread_stack_build.S b/ports_smp/risc-v32_smp/gnu/src/tx_thread_stack_build.S new file mode 100644 index 000000000..3f7ae5d6e --- /dev/null +++ b/ports_smp/risc-v32_smp/gnu/src/tx_thread_stack_build.S @@ -0,0 +1,171 @@ +/*************************************************************************** + * Copyright (c) 2025 10xEngineers + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Component */ +/** */ +/** Thread */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + + + .section .text + +#include "tx_port.h" + .equ TX_TRAP_FRAME_SIZE, TX_RISCV_TRAP_FRAME_SIZE + +/**************************************************************************/ +/* */ +/* FUNCTION RELEASE */ +/* */ +/* _tx_thread_stack_build RISC-V32/GNU */ +/* 6.4.x */ +/* AUTHOR */ +/* */ +/* Akif Ejaz, 10xEngineers */ +/* */ +/* DESCRIPTION */ +/* */ +/* This function builds a stack frame on the supplied thread's stack. */ +/* The stack frame results in a fake interrupt return to the supplied */ +/* function pointer. */ +/* */ +/* INPUT */ +/* */ +/* thread_ptr Pointer to thread control blk */ +/* function_ptr Pointer to return function */ +/* */ +/* OUTPUT */ +/* */ +/* None */ +/* */ +/* CALLS */ +/* */ +/* None */ +/* */ +/* CALLED BY */ +/* */ +/* _tx_thread_create Create thread service */ +/* */ +/**************************************************************************/ +/* VOID _tx_thread_stack_build(TX_THREAD *thread_ptr, VOID (*function_ptr)(VOID)) +{ */ + .global _tx_thread_stack_build +_tx_thread_stack_build: + + /* Build a fake interrupt frame. The form of the fake interrupt stack + on the RISC-V should look like the following after it is built: + Reg Index + Stack Top: 1 0 Interrupt stack frame type + x27 1 Initial s11 + x26 2 Initial s10 + x25 3 Initial s9 + x24 4 Initial s8 + x23 5 Initial s7 + x22 6 Initial s6 + x21 7 Initial s5 + x20 8 Initial s4 + x19 9 Initial s3 + x18 10 Initial s2 + x9 11 Initial s1 + x8 12 Initial s0 + x31 13 Initial t6 + x30 14 Initial t5 + x29 15 Initial t4 + x28 16 Initial t3 + x7 17 Initial t2 + x6 18 Initial t1 + x5 19 Initial t0 + x17 20 Initial a7 + x16 21 Initial a6 + x15 22 Initial a5 + x14 23 Initial a4 + x13 24 Initial a3 + x12 25 Initial a2 + x11 26 Initial a1 + x10 27 Initial a0 + x1 28 Initial ra + mstatus 29 Initial mstatus seed + mepc 30 Initial mepc + -- 31 pad (0) + If floating point support (double ABI), byte offsets from + the frame base: + f0-f31 128 + i*8 Initial FP registers (all 0) + fcsr 384 Initial fcsr (0) + pad 388-399 (0) + + Stack Bottom: (higher memory address) */ + + lw t0, 16(a0) // Pickup end of stack area + li t1, ~15 // Build 16-byte alignment mask + and t0, t0, t1 // Make sure 16-byte alignment + + /* Actually build the stack frame. */ + + addi t0, t0, -TX_TRAP_FRAME_SIZE // Allocate space for the stack frame + li t1, 1 // Build stack type + sw t1, 0*4(t0) // Place stack type on the top + sw zero, 1*4(t0) // Initial s11 + sw zero, 2*4(t0) // Initial s10 + sw zero, 3*4(t0) // Initial s9 + sw zero, 4*4(t0) // Initial s8 + sw zero, 5*4(t0) // Initial s7 + sw zero, 6*4(t0) // Initial s6 + sw zero, 7*4(t0) // Initial s5 + sw zero, 8*4(t0) // Initial s4 + sw zero, 9*4(t0) // Initial s3 + sw zero, 10*4(t0) // Initial s2 + sw zero, 11*4(t0) // Initial s1 + sw zero, 12*4(t0) // Initial s0 + sw zero, 13*4(t0) // Initial t6 + sw zero, 14*4(t0) // Initial t5 + sw zero, 15*4(t0) // Initial t4 + sw zero, 16*4(t0) // Initial t3 + sw zero, 17*4(t0) // Initial t2 + sw zero, 18*4(t0) // Initial t1 + sw zero, 19*4(t0) // Initial t0 + sw zero, 20*4(t0) // Initial a7 + sw zero, 21*4(t0) // Initial a6 + sw zero, 22*4(t0) // Initial a5 + sw zero, 23*4(t0) // Initial a4 + sw zero, 24*4(t0) // Initial a3 + sw zero, 25*4(t0) // Initial a2 + sw zero, 26*4(t0) // Initial a1 + sw zero, 27*4(t0) // Initial a0 + sw zero, 28*4(t0) // Initial ra + sw a1, 30*4(t0) // Initial mepc (thread entry point) + sw zero, 31*4(t0) // Pad word (0) +#if defined(__riscv_float_abi_double) + li t1, 0x6000 // mstatus seed: FS = Dirty (bits 14:13 = 11) + sw t1, 29*4(t0) // so the first dispatch restores the clean FP state + + /* Zero the whole FP area: f0-f31 double slots (128-383), fcsr (384) + and the pad words up to the frame end. Threads start with clean + FP registers and fcsr = 0. */ + addi t1, t0, 32*4 // First FP-area word (offset 128) + addi t2, t0, TX_TRAP_FRAME_SIZE // One past the frame end (offset 400) +_tx_thread_stack_build_fp_zero: + sw zero, 0(t1) // Clear FP-area word + addi t1, t1, 4 + bne t1, t2, _tx_thread_stack_build_fp_zero +#else + sw zero, 29*4(t0) // Initial mstatus slot (0, no FP gate) +#endif + + /* Setup stack pointer. */ + /* thread_ptr -> tx_thread_stack_ptr = t0; */ + + sw t0, 8(a0) // Save stack pointer in thread's + ret // control block and return +/* } */ diff --git a/ports_smp/risc-v32_smp/gnu/src/tx_thread_system_return.S b/ports_smp/risc-v32_smp/gnu/src/tx_thread_system_return.S new file mode 100644 index 000000000..2e49bc3fe --- /dev/null +++ b/ports_smp/risc-v32_smp/gnu/src/tx_thread_system_return.S @@ -0,0 +1,159 @@ +/*************************************************************************** + * Copyright (c) 2025 10xEngineers + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Component */ +/** */ +/** Thread */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + + + .section .text + +#include "tx_port.h" + .equ TX_SOL_FRAME_SIZE, TX_RISCV_SOL_FRAME_SIZE + +/**************************************************************************/ +/* */ +/* FUNCTION RELEASE */ +/* */ +/* _tx_thread_system_return RISC-V32/GNU */ +/* 6.4.x */ +/* AUTHOR */ +/* */ +/* Akif Ejaz, 10xEngineers */ +/* */ +/* DESCRIPTION */ +/* */ +/* This function is target processor specific. It is used to transfer */ +/* control from a thread back to the system. Only a minimal context */ +/* is saved since the compiler assumes temp registers are going to get */ +/* slicked by a function call anyway. */ +/* */ +/* INPUT */ +/* */ +/* None */ +/* */ +/* OUTPUT */ +/* */ +/* None */ +/* */ +/* CALLS */ +/* */ +/* _tx_thread_schedule Thread scheduling loop */ +/* */ +/* CALLED BY */ +/* */ +/* ThreadX components */ +/* */ +/**************************************************************************/ +/* VOID _tx_thread_system_return(VOID) +{ */ + .global _tx_thread_system_return +_tx_thread_system_return: + + /* Save minimal context on the stack. */ + /* sp -= sizeof(stack_frame); */ + + addi sp, sp, -TX_SOL_FRAME_SIZE // Allocate the solicited stack frame + + sw zero, 0*4(sp) // Solicited stack type (store early so slot is always written) + + /* Read and save mstatus first; use it to guard FP register save. */ + csrr t0, mstatus // Pickup current mstatus + sw t0, 14*4(sp) // Save mstatus + + /* Store floating point preserved registers — only if FS != Off. */ +#if defined(__riscv_float_abi_double) + srli t1, t0, 13 // Shift FS field to bits [1:0] + andi t1, t1, 0x3 // Isolate FS bits + beqz t1, _tx_thread_system_return_skip_fp // Skip FP save if FS == Off + fsd f8, 8*8(sp) // Store fs0 + fsd f9, 9*8(sp) // Store fs1 + fsd f18, 10*8(sp) // Store fs2 + fsd f19, 11*8(sp) // Store fs3 + fsd f20, 12*8(sp) // Store fs4 + fsd f21, 13*8(sp) // Store fs5 + fsd f22, 14*8(sp) // Store fs6 + fsd f23, 15*8(sp) // Store fs7 + fsd f24, 16*8(sp) // Store fs8 + fsd f25, 17*8(sp) // Store fs9 + fsd f26, 18*8(sp) // Store fs10 + fsd f27, 19*8(sp) // Store fs11 + csrr t0, fcsr + sw t0, 40*4(sp) // Store fcsr +_tx_thread_system_return_skip_fp: +#endif + + sw ra, 13*4(sp) // Save return address + sw s0, 12*4(sp) // Save s0 + sw s1, 11*4(sp) // Save s1 + sw s2, 10*4(sp) // Save s2 + sw s3, 9*4(sp) // Save s3 + sw s4, 8*4(sp) // Save s4 + sw s5, 7*4(sp) // Save s5 + sw s6, 6*4(sp) // Save s6 + sw s7, 5*4(sp) // Save s7 + sw s8, 4*4(sp) // Save s8 + sw s9, 3*4(sp) // Save s9 + sw s10, 2*4(sp) // Save s10 + sw s11, 1*4(sp) // Save s11 + + /* Lockout interrupts. will be enabled in _tx_thread_schedule */ + + csrci mstatus, 0x08 // Disable interrupts (MIE bit 3) + +#ifdef TX_ENABLE_EXECUTION_CHANGE_NOTIFY + + call _tx_execution_thread_exit // Call the thread execution exit function +#endif + + la t0, _tx_thread_current_ptr // Pickup address of pointer + lw t1, 0(t0) // Pickup current thread pointer + la t2, _tx_thread_system_stack_ptr // Pickup stack pointer address + + /* Save current stack and switch to system stack. */ + /* _tx_thread_current_ptr -> tx_thread_stack_ptr = SP; + SP = _tx_thread_system_stack_ptr; */ + + sw sp, 8(t1) // Save stack pointer + lw sp, 0(t2) // Switch to system stack + + /* Determine if the time-slice is active. */ + /* if (_tx_timer_time_slice) + { */ + + la t4, _tx_timer_time_slice // Pickup time slice variable addr + lw t3, 0(t4) // Pickup time slice value + la t2, _tx_thread_schedule // Pickup address of scheduling loop + beqz t3, _tx_thread_dont_save_ts // If no time-slice, don't save it + + /* Save time-slice for the thread and clear the current time-slice. */ + /* _tx_thread_current_ptr -> tx_thread_time_slice = _tx_timer_time_slice; + _tx_timer_time_slice = 0; */ + + sw t3, 24(t1) // Save current time-slice for thread + sw zero, 0(t4) // Clear time-slice variable + + /* } */ +_tx_thread_dont_save_ts: + + /* Clear the current thread pointer. */ + /* _tx_thread_current_ptr = TX_NULL; */ + + sw x0, 0(t0) // Clear current thread pointer + jr t2 // Return to thread scheduler + +/* } */ diff --git a/ports_smp/risc-v64_smp/gnu/src/tx_thread_context_restore.S b/ports_smp/risc-v64_smp/gnu/src/tx_thread_context_restore.S new file mode 100644 index 000000000..f5a68d2ec --- /dev/null +++ b/ports_smp/risc-v64_smp/gnu/src/tx_thread_context_restore.S @@ -0,0 +1,594 @@ +/*************************************************************************** + * Copyright (c) 2024 Microsoft Corporation + * Copyright (c) 2026-present Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Component */ +/** */ +/** Thread */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + + + .section .text +/**************************************************************************/ +/* */ +/* FUNCTION RELEASE */ +/* */ +/* _tx_thread_context_restore RISC-V64/GNU */ +/* 6.2.1 */ +/* AUTHOR */ +/* */ +/* Scott Larson, Microsoft Corporation */ +/* */ +/* DESCRIPTION */ +/* */ +/* This function restores the interrupt context if it is processing a */ +/* nested interrupt. If not, it returns to the interrupt thread if no */ +/* preemption is necessary. Otherwise, if preemption is necessary or */ +/* if no thread was running, the function returns to the scheduler. */ +/* */ +/* INPUT */ +/* */ +/* None */ +/* */ +/* OUTPUT */ +/* */ +/* None */ +/* */ +/* CALLS */ +/* */ +/* _tx_thread_schedule Thread scheduling routine */ +/* */ +/* CALLED BY */ +/* */ +/* ISRs Interrupt Service Routines */ +/* */ +/**************************************************************************/ +/* VOID _tx_thread_context_restore(VOID) +{ */ +#include "tx_port.h" + .equ TX_TRAP_FRAME_SIZE, TX_RISCV_TRAP_FRAME_SIZE + .global _tx_thread_context_restore +_tx_thread_context_restore: + + /* Lockout interrupts. */ + +#ifdef TX_RISCV_SMODE + csrci sstatus, 0x02 // Disable interrupts (SIE bit 1) +#else + csrci mstatus, 0x08 // Disable interrupts (MIE bit 3) +#endif + +#ifdef TX_ENABLE_EXECUTION_CHANGE_NOTIFY + call _tx_execution_isr_exit // Call the ISR execution exit function +#endif + + /* Determine if interrupts are nested. */ + /* if (--_tx_thread_system_state) + { */ + + la t0, _tx_thread_system_state // Pickup addr of nested interrupt count + lw t1, 0(t0) // Pickup nested interrupt count + addi t1, t1, -1 // Decrement the nested interrupt counter + sw t1, 0(t0) // Store new nested count + beqz t1, _tx_thread_not_nested_restore // If 0, not nested restore + + /* Interrupts are nested. */ + + /* Just recover the saved registers and return to the point of + interrupt. */ + + /* Recover the FP state only when the saved mstatus.FS was not Off. The + save side skips the FP stores for a thread that has not touched the + unit, so those slots hold nothing worth loading back. */ +#if (__riscv_flen == 32) || (__riscv_flen == 64) + ld t1, 29*8(sp) // Pickup saved mstatus + srli t1, t1, 13 + andi t1, t1, 0x3 + beqz t1, _tx_thread_skip_fp_restore // Skip if FS was Off +#endif + + /* Recover floating point registers. */ +#if (__riscv_flen == 32) + flw f0, 31*8(sp) // Recover ft0 + flw f1, 32*8(sp) // Recover ft1 + flw f2, 33*8(sp) // Recover ft2 + flw f3, 34*8(sp) // Recover ft3 + flw f4, 35*8(sp) // Recover ft4 + flw f5, 36*8(sp) // Recover ft5 + flw f6, 37*8(sp) // Recover ft6 + flw f7, 38*8(sp) // Recover ft7 + flw f10,41*8(sp) // Recover fa0 + flw f11,42*8(sp) // Recover fa1 + flw f12,43*8(sp) // Recover fa2 + flw f13,44*8(sp) // Recover fa3 + flw f14,45*8(sp) // Recover fa4 + flw f15,46*8(sp) // Recover fa5 + flw f16,47*8(sp) // Recover fa6 + flw f17,48*8(sp) // Recover fa7 + flw f28,59*8(sp) // Recover ft8 + flw f29,60*8(sp) // Recover ft9 + flw f30,61*8(sp) // Recover ft10 + flw f31,62*8(sp) // Recover ft11 + ld t0, 63*8(sp) // Recover fcsr + csrw fcsr, t0 // Restore fcsr +#elif (__riscv_flen == 64) + fld f0, 31*8(sp) // Recover ft0 + fld f1, 32*8(sp) // Recover ft1 + fld f2, 33*8(sp) // Recover ft2 + fld f3, 34*8(sp) // Recover ft3 + fld f4, 35*8(sp) // Recover ft4 + fld f5, 36*8(sp) // Recover ft5 + fld f6, 37*8(sp) // Recover ft6 + fld f7, 38*8(sp) // Recover ft7 +#if defined(__riscv_float_abi_single) + fld f8, 39*8(sp) // Recover fs0 + fld f9, 40*8(sp) // Recover fs1 + fld f18, 49*8(sp) // Recover fs2 + fld f19, 50*8(sp) // Recover fs3 + fld f20, 51*8(sp) // Recover fs4 + fld f21, 52*8(sp) // Recover fs5 + fld f22, 53*8(sp) // Recover fs6 + fld f23, 54*8(sp) // Recover fs7 + fld f24, 55*8(sp) // Recover fs8 + fld f25, 56*8(sp) // Recover fs9 + fld f26, 57*8(sp) // Recover fs10 + fld f27, 58*8(sp) // Recover fs11 +#endif + fld f10,41*8(sp) // Recover fa0 + fld f11,42*8(sp) // Recover fa1 + fld f12,43*8(sp) // Recover fa2 + fld f13,44*8(sp) // Recover fa3 + fld f14,45*8(sp) // Recover fa4 + fld f15,46*8(sp) // Recover fa5 + fld f16,47*8(sp) // Recover fa6 + fld f17,48*8(sp) // Recover fa7 + fld f28,59*8(sp) // Recover ft8 + fld f29,60*8(sp) // Recover ft9 + fld f30,61*8(sp) // Recover ft10 + fld f31,62*8(sp) // Recover ft11 + ld t0, 63*8(sp) // Recover fcsr + csrw fcsr, t0 +#endif +_tx_thread_skip_fp_restore: + +#if defined(__riscv_vector) + /* Recover vector registers v0-v31 */ +#if defined(__riscv_float_abi_single) || defined(__riscv_float_abi_double) + addi t1, sp, 64*8 +#else + addi t1, sp, 31*8 +#endif + addi t2, t1, 4*8 + vsetvli t3, zero, e8, m8, ta, ma + vle8.v v0, (t2) // Recover v0 ~ v7 + add t2, t2, t3 + vle8.v v8, (t2) // Recover v8 ~ v15 + add t2, t2, t3 + vle8.v v16, (t2) // Recover v16 ~ v23 + add t2, t2, t3 + vle8.v v24, (t2) // Recover v24 ~ v31 + add t2, t2, t3 + + /* Recover vector CSRs */ + ld t2, 0*8(t1) + ld t3, 1*8(t1) + ld t4, 2*8(t1) + vsetvl zero, t4, t3 + csrw vstart, t2 + ld t4, 3*8(t1) + csrw vcsr, t4 +#endif + + /* Recover standard registers. */ + + /* Restore registers, + Skip global pointer because that does not change. + Also skip the saved registers since they have been restored by any function we called, + except s0 since we use it ourselves. */ + + ld t0, 30*8(sp) // Recover exception PC +#ifdef TX_RISCV_SMODE + csrw sepc, t0 // Setup sepc + + /* Compose sstatus via read/modify/write to avoid clobbering unrelated bits. + Set SPIE and restore SPP to Supervisor, preserve other fields. */ + + csrr t1, sstatus + li t4, ~0x122 // Clear mask for SPP/SPIE/SIE + and t1, t1, t4 + li t3, 0x120 // Set SPP=Supervisor(0x100) + SPIE(0x20) so sret re-enables SIE + or t1, t1, t3 + +#if defined(__riscv_float_abi_single) || defined(__riscv_float_abi_double) + li t0, 0x6000 // Set FS=Dirty (bits 14:13) for FP state + or t1, t1, t0 +#endif +#if defined(__riscv_vector) + li t0, 0x0600 + or t1, t1, t0 +#endif + csrw sstatus, t1 // Update sstatus safely +#else + csrw mepc, t0 // Setup mepc + + /* Compose mstatus via read/modify/write to avoid clobbering unrelated bits. + Set MPIE and restore MPP to Machine, preserve other fields. */ + + csrr t1, mstatus + li t2, 0x1880 // Set MPP(0x1800) | MPIE(0x80) + + or t1, t1, t2 // MPP and MPIE are now set. All other bits are preserved + andi t1, t1, ~0x8 // Clear MIE, Hardware will restore it from MPIE + +#if defined(__riscv_float_abi_single) || defined(__riscv_float_abi_double) + li t0, 0x6000 + or t1, t1, t0 +#endif +#if defined(__riscv_vector) + li t0, 0x0600 + or t1, t1, t0 +#endif + csrw mstatus, t1 // Update mstatus safely +#endif + + ld ra, 28*8(sp) // Recover return address + ld t0, 19*8(sp) // Recover t0 + ld t1, 18*8(sp) // Recover t1 + ld t2, 17*8(sp) // Recover t2 + ld s0, 12*8(sp) // Recover s0 + ld a0, 27*8(sp) // Recover a0 + ld a1, 26*8(sp) // Recover a1 + ld a2, 25*8(sp) // Recover a2 + ld a3, 24*8(sp) // Recover a3 + ld a4, 23*8(sp) // Recover a4 + ld a5, 22*8(sp) // Recover a5 + ld a6, 21*8(sp) // Recover a6 + ld a7, 20*8(sp) // Recover a7 + ld t3, 16*8(sp) // Recover t3 + ld t4, 15*8(sp) // Recover t4 + ld t5, 14*8(sp) // Recover t5 + ld t6, 13*8(sp) // Recover t6 + + addi sp, sp, TX_TRAP_FRAME_SIZE // Recover stack frame - with floating point enabled (65*8 data + 8 pad) + +#if defined(__riscv_vector) + addi t0, sp, -TX_TRAP_FRAME_SIZE + csrr t1, vlenb // Get vector register byte length + slli t1, t1, 5 // Multiply by 32 (number of vector registers) + addi t1, t1, 4*8 // Add vector CSR space: vstart, vtype, vl, vcsr + add sp, sp, t1 // Recover vector stack frame + + ld t1, 18*8(t0) // Recover t1 + ld t0, 19*8(t0) // Recover t0 +#endif +#ifdef TX_RISCV_SMODE + sret // Return to point of interrupt +#else + mret // Return to point of interrupt +#endif + + /* } */ +_tx_thread_not_nested_restore: + /* Determine if a thread was interrupted and no preemption is required. */ + /* else if (((_tx_thread_current_ptr) && (_tx_thread_current_ptr == _tx_thread_execute_ptr) + || (_tx_thread_preempt_disable)) + { */ + + la t0, _tx_thread_current_ptr // Pickup current thread pointer address + ld t1, 0(t0) // Pickup current thread pointer + beqz t1, _tx_thread_idle_system_restore // If NULL, idle system restore + + la t0, _tx_thread_preempt_disable // Pickup preempt disable flag address + lw t2, 0(t0) // Pickup preempt disable flag (UINT) + bnez t2, _tx_thread_no_preempt_restore // If set (nonzero), restore interrupted thread + + la t0, _tx_thread_execute_ptr // Pickup thread execute pointer address + ld t2, 0(t0) // Pickup thread execute pointer + bne t1, t2, _tx_thread_preempt_restore // If higher-priority thread is ready, preempt + + +_tx_thread_no_preempt_restore: + /* Restore interrupted thread or ISR. */ + + /* Pickup the saved stack pointer. */ + /* sp = _tx_thread_current_ptr -> tx_thread_stack_ptr; */ + + ld sp, 8(t1) // Switch back to thread's stack + + /* Recover the FP state only when the saved mstatus.FS was not Off. The + save side skips the FP stores for a thread that has not touched the + unit, so those slots hold nothing worth loading back. */ +#if (__riscv_flen == 32) || (__riscv_flen == 64) + ld t3, 29*8(sp) // Pickup saved mstatus + srli t3, t3, 13 + andi t3, t3, 0x3 + beqz t3, _tx_thread_no_preempt_skip_fp_restore // Skip if FS was Off +#endif + + /* Recover floating point registers. */ +#if (__riscv_flen == 32) + flw f0, 31*8(sp) // Recover ft0 + flw f1, 32*8(sp) // Recover ft1 + flw f2, 33*8(sp) // Recover ft2 + flw f3, 34*8(sp) // Recover ft3 + flw f4, 35*8(sp) // Recover ft4 + flw f5, 36*8(sp) // Recover ft5 + flw f6, 37*8(sp) // Recover ft6 + flw f7, 38*8(sp) // Recover ft7 + flw f10,41*8(sp) // Recover fa0 + flw f11,42*8(sp) // Recover fa1 + flw f12,43*8(sp) // Recover fa2 + flw f13,44*8(sp) // Recover fa3 + flw f14,45*8(sp) // Recover fa4 + flw f15,46*8(sp) // Recover fa5 + flw f16,47*8(sp) // Recover fa6 + flw f17,48*8(sp) // Recover fa7 + flw f28,59*8(sp) // Recover ft8 + flw f29,60*8(sp) // Recover ft9 + flw f30,61*8(sp) // Recover ft10 + flw f31,62*8(sp) // Recover ft11 + ld t0, 63*8(sp) // Recover fcsr + csrw fcsr, t0 // Restore fcsr +#elif (__riscv_flen == 64) + fld f0, 31*8(sp) // Recover ft0 + fld f1, 32*8(sp) // Recover ft1 + fld f2, 33*8(sp) // Recover ft2 + fld f3, 34*8(sp) // Recover ft3 + fld f4, 35*8(sp) // Recover ft4 + fld f5, 36*8(sp) // Recover ft5 + fld f6, 37*8(sp) // Recover ft6 + fld f7, 38*8(sp) // Recover ft7 +#if defined(__riscv_float_abi_single) + fld f8, 39*8(sp) // Recover fs0 + fld f9, 40*8(sp) // Recover fs1 + fld f18, 49*8(sp) // Recover fs2 + fld f19, 50*8(sp) // Recover fs3 + fld f20, 51*8(sp) // Recover fs4 + fld f21, 52*8(sp) // Recover fs5 + fld f22, 53*8(sp) // Recover fs6 + fld f23, 54*8(sp) // Recover fs7 + fld f24, 55*8(sp) // Recover fs8 + fld f25, 56*8(sp) // Recover fs9 + fld f26, 57*8(sp) // Recover fs10 + fld f27, 58*8(sp) // Recover fs11 +#endif + fld f10,41*8(sp) // Recover fa0 + fld f11,42*8(sp) // Recover fa1 + fld f12,43*8(sp) // Recover fa2 + fld f13,44*8(sp) // Recover fa3 + fld f14,45*8(sp) // Recover fa4 + fld f15,46*8(sp) // Recover fa5 + fld f16,47*8(sp) // Recover fa6 + fld f17,48*8(sp) // Recover fa7 + fld f28,59*8(sp) // Recover ft8 + fld f29,60*8(sp) // Recover ft9 + fld f30,61*8(sp) // Recover ft10 + fld f31,62*8(sp) // Recover ft11 + ld t0, 63*8(sp) // Recover fcsr + csrw fcsr, t0 // Restore fcsr +#endif +_tx_thread_no_preempt_skip_fp_restore: + +#if defined(__riscv_vector) + /* Recover vector registers v0-v31 */ +#if defined(__riscv_float_abi_single) || defined(__riscv_float_abi_double) + addi t1, sp, 64*8 +#else + addi t1, sp, 31*8 +#endif + addi t2, t1, 4*8 + vsetvli t3, zero, e8, m8, ta, ma + vle8.v v0, (t2) // Recover v0 ~ v7 + add t2, t2, t3 + vle8.v v8, (t2) // Recover v8 ~ v15 + add t2, t2, t3 + vle8.v v16, (t2) // Recover v16 ~ v23 + add t2, t2, t3 + vle8.v v24, (t2) // Recover v24 ~ v31 + add t2, t2, t3 + + /* Recover vector CSRs */ + ld t2, 0*8(t1) + ld t3, 1*8(t1) + ld t4, 2*8(t1) + vsetvl zero, t4, t3 + csrw vstart, t2 + ld t4, 3*8(t1) + csrw vcsr, t4 +#endif + + /* Recover the saved context and return to the point of interrupt. */ + + /* Recover standard registers. */ + /* Restore registers, + Skip global pointer because that does not change */ + + ld t0, 30*8(sp) // Recover exception PC +#ifdef TX_RISCV_SMODE + csrw sepc, t0 // Setup sepc + + /* Compose sstatus via read/modify/write to avoid clobbering unrelated bits. */ + + csrr t1, sstatus + li t4, ~0x122 // Clear mask for SPP/SPIE/SIE + and t1, t1, t4 + li t3, 0x120 // Set SPP=Supervisor(0x100) + SPIE(0x20) so sret re-enables SIE + or t1, t1, t3 + +#if defined(__riscv_float_abi_single) || defined(__riscv_float_abi_double) + li t0, 0x6000 // Set FS=Dirty (bits 14:13) for FP state + or t1, t1, t0 +#endif +#if defined(__riscv_vector) + li t0, 0x0600 // Set VS bits (bits 10:9 to 11, Dirty) for restored vector state + or t1, t1, t0 +#endif + csrw sstatus, t1 // Update sstatus safely +#else + csrw mepc, t0 // Setup mepc + + + /* Compose mstatus via read/modify/write to avoid clobbering unrelated bits. + Set MPIE and restore MPP to Machine, preserve other fields. */ + + csrr t1, mstatus + li t2, 0x1880 // Set MPP(0x1800) | MPIE(0x80) + + or t1, t1, t2 // MPP and MPIE are now set. All other bits are preserved + andi t1, t1, ~0x8 // Clear MIE, Hardware will restore it from MPIE + +#if defined(__riscv_float_abi_single) || defined(__riscv_float_abi_double) + li t0, 0x6000 + or t1, t1, t0 +#endif +#if defined(__riscv_vector) + li t0, 0x0600 + or t1, t1, t0 +#endif + csrw mstatus, t1 // Update mstatus safely +#endif + + ld ra, 28*8(sp) // Recover return address + ld t0, 19*8(sp) // Recover t0 + ld t1, 18*8(sp) // Recover t1 + ld t2, 17*8(sp) // Recover t2 + ld s0, 12*8(sp) // Recover s0 + ld a0, 27*8(sp) // Recover a0 + ld a1, 26*8(sp) // Recover a1 + ld a2, 25*8(sp) // Recover a2 + ld a3, 24*8(sp) // Recover a3 + ld a4, 23*8(sp) // Recover a4 + ld a5, 22*8(sp) // Recover a5 + ld a6, 21*8(sp) // Recover a6 + ld a7, 20*8(sp) // Recover a7 + ld t3, 16*8(sp) // Recover t3 + ld t4, 15*8(sp) // Recover t4 + ld t5, 14*8(sp) // Recover t5 + ld t6, 13*8(sp) // Recover t6 + + addi sp, sp, TX_TRAP_FRAME_SIZE // Recover stack frame + +#if defined(__riscv_vector) + addi t0, sp, -TX_TRAP_FRAME_SIZE + csrr t1, vlenb // Get vector register byte length + slli t1, t1, 5 // Multiply by 32 (number of vector registers) + addi t1, t1, 4*8 // Add vector CSR space: vstart, vtype, vl, vcsr + add sp, sp, t1 // Recover vector stack frame + + ld t1, 18*8(t0) // Recover t1 + ld t0, 19*8(t0) // Recover t0 +#endif +#ifdef TX_RISCV_SMODE + sret // Return to point of interrupt +#else + mret // Return to point of interrupt +#endif + + /* } + else + { */ +_tx_thread_preempt_restore: + /* Instead of directly activating the thread again, ensure we save the + entire stack frame by saving the remaining registers. */ + + ld t0, 8(t1) // Pickup thread's stack pointer + ori t3, zero, 1 // Build interrupt stack type + sd t3, 0(t0) // Store stack type + + /* Store floating point preserved registers. LP64F saved the complete + FLEN=64 state before the C interrupt handler ran. */ +#if !defined(__riscv_float_abi_single) || (__riscv_flen != 64) +#if (__riscv_flen == 32) + fsw f8, 39*8(t0) // Store fs0 + fsw f9, 40*8(t0) // Store fs1 + fsw f18, 49*8(t0) // Store fs2 + fsw f19, 50*8(t0) // Store fs3 + fsw f20, 51*8(t0) // Store fs4 + fsw f21, 52*8(t0) // Store fs5 + fsw f22, 53*8(t0) // Store fs6 + fsw f23, 54*8(t0) // Store fs7 + fsw f24, 55*8(t0) // Store fs8 + fsw f25, 56*8(t0) // Store fs9 + fsw f26, 57*8(t0) // Store fs10 + fsw f27, 58*8(t0) // Store fs11 +#elif (__riscv_flen == 64) + fsd f8, 39*8(t0) // Store fs0 + fsd f9, 40*8(t0) // Store fs1 + fsd f18, 49*8(t0) // Store fs2 + fsd f19, 50*8(t0) // Store fs3 + fsd f20, 51*8(t0) // Store fs4 + fsd f21, 52*8(t0) // Store fs5 + fsd f22, 53*8(t0) // Store fs6 + fsd f23, 54*8(t0) // Store fs7 + fsd f24, 55*8(t0) // Store fs8 + fsd f25, 56*8(t0) // Store fs9 + fsd f26, 57*8(t0) // Store fs10 + fsd f27, 58*8(t0) // Store fs11 +#endif +#endif + + /* No vector save here. _tx_thread_context_save stored v0-v31 and the + vector CSRs before the C handler ran. The psABI treats v0-v31, vl and + vtype as caller-saved, so the handler may have changed them; a second + store here would overwrite the good copy with the handler's state. */ + + /* Store standard preserved registers. */ + + sd x9, 11*8(t0) // Store s1 + sd x18, 10*8(t0) // Store s2 + sd x19, 9*8(t0) // Store s3 + sd x20, 8*8(t0) // Store s4 + sd x21, 7*8(t0) // Store s5 + sd x22, 6*8(t0) // Store s6 + sd x23, 5*8(t0) // Store s7 + sd x24, 4*8(t0) // Store s8 + sd x25, 3*8(t0) // Store s9 + sd x26, 2*8(t0) // Store s10 + sd x27, 1*8(t0) // Store s11 + // Note: s0 is already stored! + + /* Save the remaining time-slice and disable it. */ + /* if (_tx_timer_time_slice) + { */ + + la t0, _tx_timer_time_slice // Pickup time slice variable address + lw t2, 0(t0) // Pickup time slice + beqz t2, _tx_thread_dont_save_ts // If 0, skip time slice processing + + /* _tx_thread_current_ptr -> tx_thread_time_slice = _tx_timer_time_slice + _tx_timer_time_slice = 0; */ + + sw t2, 36(t1) // Save current time slice + sw x0, 0(t0) // Clear global time slice + + + /* } */ +_tx_thread_dont_save_ts: + /* Clear the current task pointer. */ + /* _tx_thread_current_ptr = TX_NULL; */ + + /* Return to the scheduler. */ + /* _tx_thread_schedule(); */ + + la t0, _tx_thread_current_ptr // Pickup current thread pointer address + sd x0, 0(t0) // Clear current thread pointer*/ + /* } */ + +_tx_thread_idle_system_restore: + /* Just return back to the scheduler! */ + j _tx_thread_schedule // Return to scheduler + +/* } */ diff --git a/ports_smp/risc-v64_smp/gnu/src/tx_thread_context_save.S b/ports_smp/risc-v64_smp/gnu/src/tx_thread_context_save.S new file mode 100644 index 000000000..1924bd6ee --- /dev/null +++ b/ports_smp/risc-v64_smp/gnu/src/tx_thread_context_save.S @@ -0,0 +1,417 @@ +/*************************************************************************** + * Copyright (c) 2024 Microsoft Corporation + * Copyright (c) 2026-present Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +// Portions of this file were generated with AI assistance. + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Component */ +/** */ +/** Thread */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + + .section .text +#include "tx_port.h" + .equ TX_TRAP_FRAME_SIZE, TX_RISCV_TRAP_FRAME_SIZE +/**************************************************************************/ +/* */ +/* FUNCTION RELEASE */ +/* */ +/* _tx_thread_context_save RISC-V64/GNU */ +/* 6.2.1 */ +/* AUTHOR */ +/* */ +/* Scott Larson, Microsoft Corporation */ +/* Wei-Chen Lai, National Cheng Kung University */ +/* */ +/* DESCRIPTION */ +/* */ +/* This function saves the context of an executing thread in the */ +/* beginning of interrupt processing. The function also ensures that */ +/* the system stack is used upon return to the calling ISR. */ +/* */ +/* INPUT */ +/* */ +/* None */ +/* */ +/* OUTPUT */ +/* */ +/* None */ +/* */ +/* CALLS */ +/* */ +/* None */ +/* */ +/* CALLED BY */ +/* */ +/* ISRs */ +/* */ +/**************************************************************************/ +/* VOID _tx_thread_context_save(VOID) +{ */ + .global _tx_thread_context_save +_tx_thread_context_save: + + /* Upon entry to this routine, it is assumed that interrupts are locked + out and the interrupt stack frame has been allocated and ra has + been saved on the stack. */ + + sd t0, 19*8(sp) // First store t0 and t1 + sd t1, 18*8(sp) + + la t0, _tx_thread_system_state // Pickup address of system state + lw t1, 0(t0) // Pickup system state + + /* Check for a nested interrupt condition. */ + /* if (_tx_thread_system_state++) + { */ + beqz t1, _tx_thread_not_nested_save // If 0, first interrupt condition + addi t1, t1, 1 // Increment the interrupt counter + sw t1, 0(t0) // Store the interrupt counter + + /* Nested interrupt condition. + Save the rest of the scratch registers on the stack and return to the + calling ISR. */ + + sd t2, 17*8(sp) // Store t2 + sd s0, 12*8(sp) // Store s0 + sd a0, 27*8(sp) // Store a0 + sd a1, 26*8(sp) // Store a1 + sd a2, 25*8(sp) // Store a2 + sd a3, 24*8(sp) // Store a3 + sd a4, 23*8(sp) // Store a4 + sd a5, 22*8(sp) // Store a5 + sd a6, 21*8(sp) // Store a6 + sd a7, 20*8(sp) // Store a7 + sd t3, 16*8(sp) // Store t3 + sd t4, 15*8(sp) // Store t4 + sd t5, 14*8(sp) // Store t5 + sd t6, 13*8(sp) // Store t6 +#ifdef TX_RISCV_SMODE + csrr t0, sepc // Load exception program counter +#else + csrr t0, mepc // Load exception program counter +#endif + sd t0, 30*8(sp) // Save it on the stack + + /* Save mstatus and skip FP state if FS is Off. */ + csrr t0, mstatus + sd t0, 29*8(sp) // Save mstatus +#if defined(__riscv_float_abi_single) || defined(__riscv_float_abi_double) + srli t1, t0, 13 + andi t1, t1, 0x3 + beqz t1, _tx_thread_skip_nested_fpu_save // Skip if FS was Off +#endif + + /* Save floating point scratch registers if floating point is enabled. */ +#if (__riscv_flen == 32) + fsw f0, 31*8(sp) // Store ft0 + fsw f1, 32*8(sp) // Store ft1 + fsw f2, 33*8(sp) // Store ft2 + fsw f3, 34*8(sp) // Store ft3 + fsw f4, 35*8(sp) // Store ft4 + fsw f5, 36*8(sp) // Store ft5 + fsw f6, 37*8(sp) // Store ft6 + fsw f7, 38*8(sp) // Store ft7 + fsw f10,41*8(sp) // Store fa0 + fsw f11,42*8(sp) // Store fa1 + fsw f12,43*8(sp) // Store fa2 + fsw f13,44*8(sp) // Store fa3 + fsw f14,45*8(sp) // Store fa4 + fsw f15,46*8(sp) // Store fa5 + fsw f16,47*8(sp) // Store fa6 + fsw f17,48*8(sp) // Store fa7 + fsw f28,59*8(sp) // Store ft8 + fsw f29,60*8(sp) // Store ft9 + fsw f30,61*8(sp) // Store ft10 + fsw f31,62*8(sp) // Store ft11 + csrr t0, fcsr + sd t0, 63*8(sp) // Store fcsr +#elif (__riscv_flen == 64) + fsd f0, 31*8(sp) // Store ft0 + fsd f1, 32*8(sp) // Store ft1 + fsd f2, 33*8(sp) // Store ft2 + fsd f3, 34*8(sp) // Store ft3 + fsd f4, 35*8(sp) // Store ft4 + fsd f5, 36*8(sp) // Store ft5 + fsd f6, 37*8(sp) // Store ft6 + fsd f7, 38*8(sp) // Store ft7 +#if defined(__riscv_float_abi_single) + fsd f8, 39*8(sp) // Store fs0 + fsd f9, 40*8(sp) // Store fs1 + fsd f18, 49*8(sp) // Store fs2 + fsd f19, 50*8(sp) // Store fs3 + fsd f20, 51*8(sp) // Store fs4 + fsd f21, 52*8(sp) // Store fs5 + fsd f22, 53*8(sp) // Store fs6 + fsd f23, 54*8(sp) // Store fs7 + fsd f24, 55*8(sp) // Store fs8 + fsd f25, 56*8(sp) // Store fs9 + fsd f26, 57*8(sp) // Store fs10 + fsd f27, 58*8(sp) // Store fs11 +#endif + fsd f10,41*8(sp) // Store fa0 + fsd f11,42*8(sp) // Store fa1 + fsd f12,43*8(sp) // Store fa2 + fsd f13,44*8(sp) // Store fa3 + fsd f14,45*8(sp) // Store fa4 + fsd f15,46*8(sp) // Store fa5 + fsd f16,47*8(sp) // Store fa6 + fsd f17,48*8(sp) // Store fa7 + fsd f28,59*8(sp) // Store ft8 + fsd f29,60*8(sp) // Store ft9 + fsd f30,61*8(sp) // Store ft10 + fsd f31,62*8(sp) // Store ft11 + csrr t0, fcsr + sd t0, 63*8(sp) // Store fcsr +#endif +_tx_thread_skip_nested_fpu_save: + +#if defined(__riscv_vector) + /* Store vector registers and CSRs */ +#if defined(__riscv_float_abi_single) || defined(__riscv_float_abi_double) + addi t1, sp, 64*8 +#else + addi t1, sp, 31*8 +#endif + /* Store vector CSRs */ + csrr t2, vstart // Store vstart + sd t2, 0*8(t1) + csrr t2, vtype // Store vtype + sd t2, 1*8(t1) + csrr t2, vl // Store vl + sd t2, 2*8(t1) + csrr t2, vcsr // Store vcsr + sd t2, 3*8(t1) + + /* Store vector registers v0-v31 */ + addi t2, t1, 4*8 + vsetvli t3, zero, e8, m8, ta, ma + vse8.v v0, 0(t2) // Store v0 ~ v7 + add t2, t2, t3 + vse8.v v8, 0(t2) // Store v8 ~ v15 + add t2, t2, t3 + vse8.v v16, 0(t2) // Store v16 ~ v23 + add t2, t2, t3 + vse8.v v24, 0(t2) // Store v24 ~ v31 + add t2, t2, t3 +#endif + +#ifdef TX_ENABLE_EXECUTION_CHANGE_NOTIFY + addi sp, sp, -16 // Reserve space to save the return address + sd ra, 0(sp) // Save the return address + call _tx_execution_isr_enter // Call the ISR execution enter function + ld ra, 0(sp) // Recover the return address + addi sp, sp, 16 // Recover the reserved stack space +#endif + + ret // Return to calling ISR + +_tx_thread_not_nested_save: + /* } */ + + /* Otherwise, not nested, check to see if a thread was running. */ + /* else if (_tx_thread_current_ptr) + { */ + addi t1, t1, 1 // Increment the interrupt counter + sw t1, 0(t0) // Store the interrupt counter + + /* Not nested: Find the user thread that was running and load our SP */ + + la t0, _tx_thread_current_ptr // Pickup current thread pointer address + ld t0, 0(t0) // Pickup current thread pointer + beqz t0, _tx_thread_idle_system_save // If NULL, idle system was interrupted + + /* Save the standard scratch registers. */ + + sd t2, 17*8(sp) // Store t2 + sd s0, 12*8(sp) // Store s0 + sd a0, 27*8(sp) // Store a0 + sd a1, 26*8(sp) // Store a1 + sd a2, 25*8(sp) // Store a2 + sd a3, 24*8(sp) // Store a3 + sd a4, 23*8(sp) // Store a4 + sd a5, 22*8(sp) // Store a5 + sd a6, 21*8(sp) // Store a6 + sd a7, 20*8(sp) // Store a7 + sd t3, 16*8(sp) // Store t3 + sd t4, 15*8(sp) // Store t4 + sd t5, 14*8(sp) // Store t5 + sd t6, 13*8(sp) // Store t6 + +#ifdef TX_RISCV_SMODE + csrr t1, sepc // Load exception program counter +#else + csrr t1, mepc // Load exception program counter +#endif + sd t1, 30*8(sp) // Save it on the stack + + /* Save mstatus and skip FP state if FS is Off. */ + csrr t1, mstatus + sd t1, 29*8(sp) // Save mstatus +#if defined(__riscv_float_abi_single) || defined(__riscv_float_abi_double) + srli t2, t1, 13 + andi t2, t2, 0x3 + beqz t2, _tx_thread_skip_fpu_save // Skip if FS was Off +#endif + + /* Save floating point scratch registers if floating point is enabled. */ +#if (__riscv_flen == 32) + fsw f0, 31*8(sp) // Store ft0 + fsw f1, 32*8(sp) // Store ft1 + fsw f2, 33*8(sp) // Store ft2 + fsw f3, 34*8(sp) // Store ft3 + fsw f4, 35*8(sp) // Store ft4 + fsw f5, 36*8(sp) // Store ft5 + fsw f6, 37*8(sp) // Store ft6 + fsw f7, 38*8(sp) // Store ft7 + fsw f10,41*8(sp) // Store fa0 + fsw f11,42*8(sp) // Store fa1 + fsw f12,43*8(sp) // Store fa2 + fsw f13,44*8(sp) // Store fa3 + fsw f14,45*8(sp) // Store fa4 + fsw f15,46*8(sp) // Store fa5 + fsw f16,47*8(sp) // Store fa6 + fsw f17,48*8(sp) // Store fa7 + fsw f28,59*8(sp) // Store ft8 + fsw f29,60*8(sp) // Store ft9 + fsw f30,61*8(sp) // Store ft10 + fsw f31,62*8(sp) // Store ft11 + csrr t0, fcsr + sd t0, 63*8(sp) // Store fcsr +#elif (__riscv_flen == 64) + fsd f0, 31*8(sp) // Store ft0 + fsd f1, 32*8(sp) // Store ft1 + fsd f2, 33*8(sp) // Store ft2 + fsd f3, 34*8(sp) // Store ft3 + fsd f4, 35*8(sp) // Store ft4 + fsd f5, 36*8(sp) // Store ft5 + fsd f6, 37*8(sp) // Store ft6 + fsd f7, 38*8(sp) // Store ft7 +#if defined(__riscv_float_abi_single) + fsd f8, 39*8(sp) // Store fs0 + fsd f9, 40*8(sp) // Store fs1 + fsd f18, 49*8(sp) // Store fs2 + fsd f19, 50*8(sp) // Store fs3 + fsd f20, 51*8(sp) // Store fs4 + fsd f21, 52*8(sp) // Store fs5 + fsd f22, 53*8(sp) // Store fs6 + fsd f23, 54*8(sp) // Store fs7 + fsd f24, 55*8(sp) // Store fs8 + fsd f25, 56*8(sp) // Store fs9 + fsd f26, 57*8(sp) // Store fs10 + fsd f27, 58*8(sp) // Store fs11 +#endif + fsd f10,41*8(sp) // Store fa0 + fsd f11,42*8(sp) // Store fa1 + fsd f12,43*8(sp) // Store fa2 + fsd f13,44*8(sp) // Store fa3 + fsd f14,45*8(sp) // Store fa4 + fsd f15,46*8(sp) // Store fa5 + fsd f16,47*8(sp) // Store fa6 + fsd f17,48*8(sp) // Store fa7 + fsd f28,59*8(sp) // Store ft8 + fsd f29,60*8(sp) // Store ft9 + fsd f30,61*8(sp) // Store ft10 + fsd f31,62*8(sp) // Store ft11 + csrr t0, fcsr + sd t0, 63*8(sp) // Store fcsr +#endif +_tx_thread_skip_fpu_save: + +#if defined(__riscv_vector) + /* Store vector registers and CSRs */ +#if defined(__riscv_float_abi_single) || defined(__riscv_float_abi_double) + addi t1, sp, 64*8 +#else + addi t1, sp, 31*8 +#endif + /* Store vector CSRs */ + csrr t2, vstart // Store vstart + sd t2, 0*8(t1) + csrr t2, vtype // Store vtype + sd t2, 1*8(t1) + csrr t2, vl // Store vl + sd t2, 2*8(t1) + csrr t2, vcsr // Store vcsr + sd t2, 3*8(t1) + + /* Store vector registers v0-v31 */ + addi t2, t1, 4*8 + vsetvli t3, zero, e8, m8, ta, ma + vse8.v v0, 0(t2) // Store v0 ~ v7 + add t2, t2, t3 + vse8.v v8, 0(t2) // Store v8 ~ v15 + add t2, t2, t3 + vse8.v v16, 0(t2) // Store v16 ~ v23 + add t2, t2, t3 + vse8.v v24, 0(t2) // Store v24 ~ v31 + add t2, t2, t3 +#endif + + /* Save the current stack pointer in the thread's control block. */ + /* _tx_thread_current_ptr -> tx_thread_stack_ptr = sp; */ + + /* Switch to the system stack. */ + /* sp = _tx_thread_system_stack_ptr; */ + + la t1, _tx_thread_current_ptr // Pickup current thread pointer address + ld t1, 0(t1) // Pickup current thread pointer + sd sp, 8(t1) // Save stack pointer + +#ifdef TX_ENABLE_EXECUTION_CHANGE_NOTIFY + /* _tx_execution_isr_enter is called with thread stack pointer */ + addi sp, sp, -16 // Reserve space to save the return address + sd ra, 0(sp) // Save the return address + call _tx_execution_isr_enter // Call the ISR execution enter function + ld ra, 0(sp) // Recover the return address + addi sp, sp, 16 // Recover the reserved stack space +#endif + + la t0, _tx_thread_system_stack_ptr // Pickup system stack pointer address + ld sp, 0(t0) // Switch to system stack + ret // Return to calling ISR + + /* } + else + { */ + +_tx_thread_idle_system_save: + + +#ifdef TX_ENABLE_EXECUTION_CHANGE_NOTIFY + addi sp, sp, -16 // Reserve space to save the return address + sd ra, 0(sp) // Save the return address + call _tx_execution_isr_enter // Call the ISR execution enter function + ld ra, 0(sp) // Recover the return address + addi sp, sp, 16 // Recover the reserved stack space +#endif + + /* Interrupt occurred in the scheduling loop. */ + + /* } +} */ + addi sp, sp, TX_TRAP_FRAME_SIZE // Recover stack frame - with floating point enabled (65*8 data + 8 pad) + +#if defined(__riscv_vector) + addi t0, sp, -TX_TRAP_FRAME_SIZE + csrr t1, vlenb // Get vector register byte length + slli t1, t1, 5 // Multiply by 32 (number of vector registers) + addi t1, t1, 4*8 // Add vector CSR space: vstart, vtype, vl, vcsr + add sp, sp, t1 // Recover vector stack frame + + ld t1, 18*8(t0) // Recover t1 + ld t0, 19*8(t0) // Recover t0 +#endif + ret // Return to calling ISR diff --git a/ports_smp/risc-v64_smp/gnu/src/tx_thread_interrupt_control.S b/ports_smp/risc-v64_smp/gnu/src/tx_thread_interrupt_control.S new file mode 100644 index 000000000..d791e5089 --- /dev/null +++ b/ports_smp/risc-v64_smp/gnu/src/tx_thread_interrupt_control.S @@ -0,0 +1,85 @@ +/*************************************************************************** + * Copyright (c) 2024 Microsoft Corporation + * Copyright (c) 2026-present Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Component */ +/** */ +/** Thread */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + + + .section .text +/**************************************************************************/ +/* */ +/* FUNCTION RELEASE */ +/* */ +/* _tx_thread_interrupt_control RISC-V64/GNU */ +/* 6.5.1.202602a */ +/* AUTHOR */ +/* */ +/* Scott Larson, Microsoft Corporation */ +/* */ +/* DESCRIPTION */ +/* */ +/* This function is responsible for changing the interrupt lockout */ +/* posture of the system. */ +/* */ +/* INPUT */ +/* */ +/* new_posture New interrupt lockout posture */ +/* */ +/* OUTPUT */ +/* */ +/* old_posture Old interrupt lockout posture */ +/* */ +/* CALLS */ +/* */ +/* None */ +/* */ +/* CALLED BY */ +/* */ +/* Application Code */ +/* */ +/**************************************************************************/ +/* UINT _tx_thread_interrupt_control(UINT new_posture) +{ */ + .global _tx_thread_interrupt_control +_tx_thread_interrupt_control: + /* Pickup current interrupt lockout posture. */ + +#ifdef TX_RISCV_SMODE + li t1, 0x02 // Build SIE bit mask (bit 1) + andi a0, a0, 0x02 // Mask incoming to only SIE bit + bnez a0, 1f // If set, enable interrupts + csrrc t0, sstatus, t1 // Atomically clear SIE, read old sstatus + j 2f +1: + csrrs t0, sstatus, t1 // Atomically set SIE, read old sstatus +2: + andi a0, t0, 0x02 // Return original SIE bit +#else + li t1, 0x08 // Build MIE bit mask (bit 3) + andi a0, a0, 0x08 // Mask incoming to only MIE bit + bnez a0, 1f // If set, enable interrupts + csrrc t0, mstatus, t1 // Atomically clear MIE, read old mstatus + j 2f +1: + csrrs t0, mstatus, t1 // Atomically set MIE, read old mstatus +2: + andi a0, t0, 0x08 // Return original MIE bit +#endif + ret +/* } */ diff --git a/ports_smp/risc-v64_smp/gnu/src/tx_thread_schedule.S b/ports_smp/risc-v64_smp/gnu/src/tx_thread_schedule.S new file mode 100644 index 000000000..262207265 --- /dev/null +++ b/ports_smp/risc-v64_smp/gnu/src/tx_thread_schedule.S @@ -0,0 +1,439 @@ +/*************************************************************************** + * Copyright (c) 2024 Microsoft Corporation + * Copyright (c) 2026-present Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Component */ +/** */ +/** Thread */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + + .section .text +/**************************************************************************/ +/* */ +/* FUNCTION RELEASE */ +/* */ +/* _tx_thread_schedule RISC-V64/GNU */ +/* 6.2.1 */ +/* AUTHOR */ +/* */ +/* Scott Larson, Microsoft Corporation */ +/* */ +/* DESCRIPTION */ +/* */ +/* This function waits for a thread control block pointer to appear in */ +/* the _tx_thread_execute_ptr variable. Once a thread pointer appears */ +/* in the variable, the corresponding thread is resumed. */ +/* */ +/* INPUT */ +/* */ +/* None */ +/* */ +/* OUTPUT */ +/* */ +/* None */ +/* */ +/* CALLS */ +/* */ +/* None */ +/* */ +/* CALLED BY */ +/* */ +/* _tx_initialize_kernel_enter ThreadX entry function */ +/* _tx_thread_system_return Return to system from thread */ +/* _tx_thread_context_restore Restore thread's context */ +/* */ +/**************************************************************************/ +/* VOID _tx_thread_schedule(VOID) +{ */ +#include "tx_port.h" + .equ TX_TRAP_FRAME_SIZE, TX_RISCV_TRAP_FRAME_SIZE + .equ TX_SOL_FRAME_SIZE, TX_RISCV_SOL_FRAME_SIZE + .global _tx_thread_schedule +_tx_thread_schedule: + + /* Enable interrupts. */ +#ifdef TX_RISCV_SMODE + csrsi sstatus, 0x02 // Enable interrupts (SIE bit 1) +#else + csrsi mstatus, 0x08 // Enable interrupts (MIE bit 3) +#endif + + /* Wait for a thread to execute. */ + /* do + { */ + + la t0, _tx_thread_execute_ptr // Pickup address of execute ptr +_tx_thread_schedule_loop: + ld t1, 0(t0) // Pickup next thread to execute + +/* TX_USE_WFI_IDLE Configuration: + When defined, the scheduler enters WFI (Wait-For-Interrupt) mode when + no threads are ready, reducing power consumption. The core will wake + on any enabled interrupt. This is recommended for battery-powered or + low-power applications. Define TX_USE_WFI_IDLE in tx_user.h or via + compiler flags to enable this feature. */ +#ifdef TX_USE_WFI_IDLE + beqz t1, 1f + j 2f +1: wfi + j _tx_thread_schedule_loop +2: + beqz t1, _tx_thread_schedule_loop // Fallback: If still NULL, loop +#else + beqz t1, _tx_thread_schedule_loop // If NULL, wait for thread to execute +#endif + + /* } + while(_tx_thread_execute_ptr == NULL); */ + + /* Yes! We have a thread to execute. Lockout interrupts and + transfer control to it. */ +#ifdef TX_RISCV_SMODE + csrci sstatus, 0x02 // Lockout interrupts +#else + csrci mstatus, 0x08 // Lockout interrupts +#endif + + /* Setup the current thread pointer. */ + /* _tx_thread_current_ptr = _tx_thread_execute_ptr; */ + + la t0, _tx_thread_current_ptr // Pickup current thread pointer address + sd t1, 0(t0) // Set current thread pointer + + /* Increment the run count for this thread. */ + /* _tx_thread_current_ptr -> tx_thread_run_count++; */ + + lw t2, 4(t1) // Pickup run count + lw t3, 36(t1) // Pickup time slice value + addi t2, t2, 1 // Increment run count + sw t2, 4(t1) // Store new run count + + /* Setup time-slice, if present. */ + /* _tx_timer_time_slice = _tx_thread_current_ptr -> tx_thread_time_slice; */ + + la t2, _tx_timer_time_slice // Pickup time-slice variable address + + /* Switch to the thread's stack. */ + /* SP = _tx_thread_execute_ptr -> tx_thread_stack_ptr; */ + + ld sp, 8(t1) // Switch to thread's stack + sw t3, 0(t2) // Store new time-slice + +#ifdef TX_ENABLE_EXECUTION_CHANGE_NOTIFY + + call _tx_execution_thread_enter // Call the thread execution enter function +#endif + + /* Determine if an interrupt frame or a synchronous task suspension frame + is present. */ + + ld t2, 0(sp) // Pickup stack type + beqz t2, _tx_thread_synch_return // If 0, solicited thread return + + /* Determine if floating point registers need to be recovered. This is an + interrupt frame, so _tx_thread_context_save built it and may have + skipped the FP stores when mstatus.FS was Off. Read back the same slot + it wrote rather than reloading whatever those slots happen to hold. */ +#if (__riscv_flen == 32) || (__riscv_flen == 64) + ld t1, 29*8(sp) // Pickup saved mstatus + srli t1, t1, 13 + andi t1, t1, 0x3 + beqz t1, _tx_thread_schedule_skip_fp_restore // Skip if FS was Off +#endif + +#if (__riscv_flen == 32) + flw f0, 31*8(sp) // Recover ft0 + flw f1, 32*8(sp) // Recover ft1 + flw f2, 33*8(sp) // Recover ft2 + flw f3, 34*8(sp) // Recover ft3 + flw f4, 35*8(sp) // Recover ft4 + flw f5, 36*8(sp) // Recover ft5 + flw f6, 37*8(sp) // Recover ft6 + flw f7, 38*8(sp) // Recover ft7 + flw f8, 39*8(sp) // Recover fs0 + flw f9, 40*8(sp) // Recover fs1 + flw f10,41*8(sp) // Recover fa0 + flw f11,42*8(sp) // Recover fa1 + flw f12,43*8(sp) // Recover fa2 + flw f13,44*8(sp) // Recover fa3 + flw f14,45*8(sp) // Recover fa4 + flw f15,46*8(sp) // Recover fa5 + flw f16,47*8(sp) // Recover fa6 + flw f17,48*8(sp) // Recover fa7 + flw f18,49*8(sp) // Recover fs2 + flw f19,50*8(sp) // Recover fs3 + flw f20,51*8(sp) // Recover fs4 + flw f21,52*8(sp) // Recover fs5 + flw f22,53*8(sp) // Recover fs6 + flw f23,54*8(sp) // Recover fs7 + flw f24,55*8(sp) // Recover fs8 + flw f25,56*8(sp) // Recover fs9 + flw f26,57*8(sp) // Recover fs10 + flw f27,58*8(sp) // Recover fs11 + flw f28,59*8(sp) // Recover ft8 + flw f29,60*8(sp) // Recover ft9 + flw f30,61*8(sp) // Recover ft10 + flw f31,62*8(sp) // Recover ft11 + ld t0, 63*8(sp) // Recover fcsr + csrw fcsr, t0 // Restore fcsr +#elif (__riscv_flen == 64) + fld f0, 31*8(sp) // Recover ft0 + fld f1, 32*8(sp) // Recover ft1 + fld f2, 33*8(sp) // Recover ft2 + fld f3, 34*8(sp) // Recover ft3 + fld f4, 35*8(sp) // Recover ft4 + fld f5, 36*8(sp) // Recover ft5 + fld f6, 37*8(sp) // Recover ft6 + fld f7, 38*8(sp) // Recover ft7 + fld f8, 39*8(sp) // Recover fs0 + fld f9, 40*8(sp) // Recover fs1 + fld f10,41*8(sp) // Recover fa0 + fld f11,42*8(sp) // Recover fa1 + fld f12,43*8(sp) // Recover fa2 + fld f13,44*8(sp) // Recover fa3 + fld f14,45*8(sp) // Recover fa4 + fld f15,46*8(sp) // Recover fa5 + fld f16,47*8(sp) // Recover fa6 + fld f17,48*8(sp) // Recover fa7 + fld f18,49*8(sp) // Recover fs2 + fld f19,50*8(sp) // Recover fs3 + fld f20,51*8(sp) // Recover fs4 + fld f21,52*8(sp) // Recover fs5 + fld f22,53*8(sp) // Recover fs6 + fld f23,54*8(sp) // Recover fs7 + fld f24,55*8(sp) // Recover fs8 + fld f25,56*8(sp) // Recover fs9 + fld f26,57*8(sp) // Recover fs10 + fld f27,58*8(sp) // Recover fs11 + fld f28,59*8(sp) // Recover ft8 + fld f29,60*8(sp) // Recover ft9 + fld f30,61*8(sp) // Recover ft10 + fld f31,62*8(sp) // Recover ft11 + ld t0, 63*8(sp) // Recover fcsr + csrw fcsr, t0 // Restore fcsr +#endif +_tx_thread_schedule_skip_fp_restore: + +#if defined(__riscv_vector) + /* Recover vector registers v0-v31 */ +#if defined(__riscv_float_abi_single) || defined(__riscv_float_abi_double) + addi t1, sp, 64*8 +#else + addi t1, sp, 31*8 +#endif + addi t2, t1, 4*8 + vsetvli t3, zero, e8, m8, ta, ma + vle8.v v0, (t2) // Recover v0 ~ v7 + add t2, t2, t3 + vle8.v v8, (t2) // Recover v8 ~ v15 + add t2, t2, t3 + vle8.v v16, (t2) // Recover v16 ~ v23 + add t2, t2, t3 + vle8.v v24, (t2) // Recover v24 ~ v31 + add t2, t2, t3 + + /* Recover vector CSRs */ + ld t2, 0*8(t1) + ld t3, 1*8(t1) + ld t4, 2*8(t1) + vsetvl zero, t4, t3 + csrw vstart, t2 + ld t4, 3*8(t1) + csrw vcsr, t4 +#endif + + /* Recover standard registers. */ + + ld t0, 30*8(sp) // Recover mepc/sepc +#ifdef TX_RISCV_SMODE + csrw sepc, t0 // Store sepc + + /* Read/modify/write sstatus to preserve SUM, MXR, UXL, FS, etc. + Only touch SPP, SPIE, SIE — the bits SRET consumes. */ + csrr t0, sstatus + li t1, ~0x122 // Clear mask: SIE(1) | SPIE(5) | SPP(8) + and t0, t0, t1 + li t1, 0x120 // Set SPP=Supervisor(0x100) | SPIE(0x20) + or t0, t0, t1 +#if defined(__riscv_float_abi_single) || defined(__riscv_float_abi_double) + li t1, 0x6000 // Set FS=Dirty (bits 14:13) + or t0, t0, t1 +#endif +#if defined(__riscv_vector) + li t1, 0x0600 + or t0, t0, t1 +#endif + csrw sstatus, t0 // Update sstatus safely +#else + csrw mepc, t0 // Store mepc + + /* Read/modify/write mstatus — same principle as S-mode path. */ + csrr t0, mstatus + li t1, ~0x1888 // Clear mask: MIE(3) | MPIE(7) | MPP(11:12) + and t0, t0, t1 + li t1, 0x1880 // Set MPP=Machine(0x1800) | MPIE(0x80) + or t0, t0, t1 +#if defined(__riscv_float_abi_single) || defined(__riscv_float_abi_double) + li t1, 0x6000 // Set FS=Dirty (bits 14:13) + or t0, t0, t1 +#endif +#if defined(__riscv_vector) + li t1, 0x0600 + or t0, t0, t1 +#endif + csrw mstatus, t0 // Set mstatus +#endif + + ld ra, 28*8(sp) // Recover return address + ld t0, 19*8(sp) // Recover t0 + ld t1, 18*8(sp) // Recover t1 + ld t2, 17*8(sp) // Recover t2 + ld s0, 12*8(sp) // Recover s0 + ld s1, 11*8(sp) // Recover s1 + ld a0, 27*8(sp) // Recover a0 + ld a1, 26*8(sp) // Recover a1 + ld a2, 25*8(sp) // Recover a2 + ld a3, 24*8(sp) // Recover a3 + ld a4, 23*8(sp) // Recover a4 + ld a5, 22*8(sp) // Recover a5 + ld a6, 21*8(sp) // Recover a6 + ld a7, 20*8(sp) // Recover a7 + ld s2, 10*8(sp) // Recover s2 + ld s3, 9*8(sp) // Recover s3 + ld s4, 8*8(sp) // Recover s4 + ld s5, 7*8(sp) // Recover s5 + ld s6, 6*8(sp) // Recover s6 + ld s7, 5*8(sp) // Recover s7 + ld s8, 4*8(sp) // Recover s8 + ld s9, 3*8(sp) // Recover s9 + ld s10, 2*8(sp) // Recover s10 + ld s11, 1*8(sp) // Recover s11 + ld t3, 16*8(sp) // Recover t3 + ld t4, 15*8(sp) // Recover t4 + ld t5, 14*8(sp) // Recover t5 + ld t6, 13*8(sp) // Recover t6 + + addi sp, sp, TX_TRAP_FRAME_SIZE // Recover stack frame - with floating point registers (65*8 data + 8 pad) +#if defined(__riscv_vector) + addi t0, sp, -TX_TRAP_FRAME_SIZE + csrr t1, vlenb // Get vector register byte length + slli t1, t1, 5 // Multiply by 32 (number of vector registers) + addi t1, t1, 4*8 // Add vector CSR space: vstart, vtype, vl, vcsr + add sp, sp, t1 // Recover vector stack frame + + ld t1, 18*8(t0) // Recover t1 + ld t0, 19*8(t0) // Recover t0 +#endif +#ifdef TX_RISCV_SMODE + sret // Return to point of interrupt +#else + mret // Return to point of interrupt +#endif + +_tx_thread_synch_return: + +#if (__riscv_flen == 32) + flw f8, 15*8(sp) // Recover fs0 + flw f9, 16*8(sp) // Recover fs1 + flw f18,17*8(sp) // Recover fs2 + flw f19,18*8(sp) // Recover fs3 + flw f20,19*8(sp) // Recover fs4 + flw f21,20*8(sp) // Recover fs5 + flw f22,21*8(sp) // Recover fs6 + flw f23,22*8(sp) // Recover fs7 + flw f24,23*8(sp) // Recover fs8 + flw f25,24*8(sp) // Recover fs9 + flw f26,25*8(sp) // Recover fs10 + flw f27,26*8(sp) // Recover fs11 + ld t0, 27*8(sp) // Recover fcsr + csrw fcsr, t0 // +#elif (__riscv_flen == 64) + fld f8, 15*8(sp) // Recover fs0 + fld f9, 16*8(sp) // Recover fs1 + fld f18,17*8(sp) // Recover fs2 + fld f19,18*8(sp) // Recover fs3 + fld f20,19*8(sp) // Recover fs4 + fld f21,20*8(sp) // Recover fs5 + fld f22,21*8(sp) // Recover fs6 + fld f23,22*8(sp) // Recover fs7 + fld f24,23*8(sp) // Recover fs8 + fld f25,24*8(sp) // Recover fs9 + fld f26,25*8(sp) // Recover fs10 + fld f27,26*8(sp) // Recover fs11 + ld t0, 27*8(sp) // Recover fcsr + csrw fcsr, t0 // +#endif + +#if defined(__riscv_vector) + /* Recover vector registers v0-v31 */ +#if defined(__riscv_float_abi_single) || defined(__riscv_float_abi_double) + addi t1, sp, 28*8 +#else + addi t1, sp, 15*8 +#endif + addi t2, t1, 4*8 + vsetvli t3, zero, e8, m8, ta, ma + vle8.v v0, (t2) // Recover v0 ~ v7 + add t2, t2, t3 + vle8.v v8, (t2) // Recover v8 ~ v15 + add t2, t2, t3 + vle8.v v16, (t2) // Recover v16 ~ v23 + add t2, t2, t3 + vle8.v v24, (t2) // Recover v24 ~ v31 + add t2, t2, t3 + + /* Recover vector CSRs */ + ld t2, 0*8(t1) + ld t3, 1*8(t1) + ld t4, 2*8(t1) + vsetvl zero, t4, t3 + csrw vstart, t2 + ld t4, 3*8(t1) + csrw vcsr, t4 +#endif + + /* Recover standard preserved registers. */ + /* Recover standard registers. */ + + ld ra, 13*8(sp) // Recover RA + ld s0, 12*8(sp) // Recover s0 + ld s1, 11*8(sp) // Recover s1 + ld s2, 10*8(sp) // Recover s2 + ld s3, 9*8(sp) // Recover s3 + ld s4, 8*8(sp) // Recover s4 + ld s5, 7*8(sp) // Recover s5 + ld s6, 6*8(sp) // Recover s6 + ld s7, 5*8(sp) // Recover s7 + ld s8, 4*8(sp) // Recover s8 + ld s9, 3*8(sp) // Recover s9 + ld s10, 2*8(sp) // Recover s10 + ld s11, 1*8(sp) // Recover s11 + ld t0, 14*8(sp) // Recover status register +#ifdef TX_RISCV_SMODE + csrw sstatus, t0 // Store sstatus, enables interrupt +#else + csrw mstatus, t0 // Store mstatus, enables interrupt +#endif + addi sp, sp, TX_SOL_FRAME_SIZE // Recover stack frame (29*8 data + 8 pad) +#if defined(__riscv_vector) + csrr t1, vlenb // Get vector register byte length + slli t1, t1, 5 // Multiply by 32 (number of vector registers) + addi t1, t1, 4*8 // Add vector CSR space: vstart, vtype, vl, vcsr + add sp, sp, t1 // Recover vector stack frame +#endif + ret // Return to thread + +/* } */ diff --git a/ports_smp/risc-v64_smp/gnu/src/tx_thread_stack_build.S b/ports_smp/risc-v64_smp/gnu/src/tx_thread_stack_build.S new file mode 100644 index 000000000..47e5c995f --- /dev/null +++ b/ports_smp/risc-v64_smp/gnu/src/tx_thread_stack_build.S @@ -0,0 +1,266 @@ +/*************************************************************************** + * Copyright (c) 2024 Microsoft Corporation + * Copyright (c) 2026-present Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Component */ +/** */ +/** Thread */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + + .section .text +#include "tx_port.h" + .equ TX_TRAP_FRAME_SIZE, TX_RISCV_TRAP_FRAME_SIZE +/**************************************************************************/ +/* */ +/* FUNCTION RELEASE */ +/* */ +/* _tx_thread_stack_build RISC-V64/GNU */ +/* 6.2.1 */ +/* AUTHOR */ +/* */ +/* Scott Larson, Microsoft Corporation */ +/* Wei-Chen Lai, National Cheng Kung University */ +/* */ +/* DESCRIPTION */ +/* */ +/* This function builds a stack frame on the supplied thread's stack. */ +/* The stack frame results in a fake interrupt return to the supplied */ +/* function pointer. */ +/* */ +/* INPUT */ +/* */ +/* thread_ptr Pointer to thread control blk */ +/* function_ptr Pointer to return function */ +/* */ +/* OUTPUT */ +/* */ +/* None */ +/* */ +/* CALLS */ +/* */ +/* None */ +/* */ +/* CALLED BY */ +/* */ +/* _tx_thread_create Create thread service */ +/* */ +/**************************************************************************/ +/* VOID _tx_thread_stack_build(TX_THREAD *thread_ptr, VOID (*function_ptr)(VOID)) +{ */ + .global _tx_thread_stack_build +_tx_thread_stack_build: + + /* Build a fake interrupt frame. The form of the fake interrupt stack + on the RISC-V should look like the following after it is built: + Reg Index + Stack Top: 1 0 Interrupt stack frame type + x27 1 Initial s11 + x26 2 Initial s10 + x25 3 Initial s9 + x24 4 Initial s8 + x23 5 Initial s7 + x22 6 Initial s6 + x21 7 Initial s5 + x20 8 Initial s4 + x19 9 Initial s3 + x18 10 Initial s2 + x9 11 Initial s1 + x8 12 Initial s0 + x31 13 Initial t6 + x30 14 Initial t5 + x29 15 Initial t4 + x28 16 Initial t3 + x7 17 Initial t2 + x6 18 Initial t1 + x5 19 Initial t0 + x17 20 Initial a7 + x16 21 Initial a6 + x15 22 Initial a5 + x14 23 Initial a4 + x13 24 Initial a3 + x12 25 Initial a2 + x11 26 Initial a1 + x10 27 Initial a0 + x1 28 Initial ra + mstatus 29 Initial mstatus + mepc 30 Initial mepc +If floating point support: + f0 31 Inital ft0 + f1 32 Inital ft1 + f2 33 Inital ft2 + f3 34 Inital ft3 + f4 35 Inital ft4 + f5 36 Inital ft5 + f6 37 Inital ft6 + f7 38 Inital ft7 + f8 39 Inital fs0 + f9 40 Inital fs1 + f10 41 Inital fa0 + f11 42 Inital fa1 + f12 43 Inital fa2 + f13 44 Inital fa3 + f14 45 Inital fa4 + f15 46 Inital fa5 + f16 47 Inital fa6 + f17 48 Inital fa7 + f18 49 Inital fs2 + f19 50 Inital fs3 + f20 51 Inital fs4 + f21 52 Inital fs5 + f22 53 Inital fs6 + f23 54 Inital fs7 + f24 55 Inital fs8 + f25 56 Inital fs9 + f26 57 Inital fs10 + f27 58 Inital fs11 + f28 59 Inital ft8 + f29 60 Inital ft9 + f30 61 Inital ft10 + f31 62 Inital ft11 + fscr 63 Inital fscr +If vector extension support: + vstart 64 Initial vstart + vtype 65 Initial vtype + vl 66 Initial vl + vcsr 67 Initial vcsr + v0 68 Initial v0 + v1 69 Initial v1 + ... + v31 99 Initial v31 + + With floating point enabled, 8 pad bytes sit at the top of the frame + (bytes 520-527) so the 528-byte total keeps sp 16-byte aligned. + + Stack Bottom: (higher memory address) */ + + ld t0, 24(a0) // Pickup end of stack area + li t1, ~15 // Build 16-byte alignment mask + and t0, t0, t1 // Make sure 16-byte alignment + + /* Actually build the stack frame. */ + + addi t0, t0, -TX_TRAP_FRAME_SIZE + +#if defined(__riscv_vector) + /* Vector extension support: calculate space based on vlenb */ + csrr t4, vlenb // Get vector register byte length + slli t4, t4, 5 // Multiply by 32 (number of vector registers) + addi t4, t4, 4*8 // Add vector CSR space: vstart, vtype, vl, vcsr + sub t0, t0, t4 // Allocate vector space for the stack frame +#else + li t4, 0 +#endif + + li t1, 1 // Build stack type + sd t1, 0*8(t0) // Place stack type on the top + sd zero, 1*8(t0) // Initial s11 + sd zero, 2*8(t0) // Initial s10 + sd zero, 3*8(t0) // Initial s9 + sd zero, 4*8(t0) // Initial s8 + sd zero, 5*8(t0) // Initial s7 + sd zero, 6*8(t0) // Initial s6 + sd zero, 7*8(t0) // Initial s5 + sd zero, 8*8(t0) // Initial s4 + sd zero, 9*8(t0) // Initial s3 + sd zero, 10*8(t0) // Initial s2 + sd zero, 11*8(t0) // Initial s1 + sd zero, 12*8(t0) // Initial s0 + sd zero, 13*8(t0) // Initial t6 + sd zero, 14*8(t0) // Initial t5 + sd zero, 15*8(t0) // Initial t4 + sd zero, 16*8(t0) // Initial t3 + sd zero, 17*8(t0) // Initial t2 + sd zero, 18*8(t0) // Initial t1 + sd zero, 19*8(t0) // Initial t0 + sd zero, 20*8(t0) // Initial a7 + sd zero, 21*8(t0) // Initial a6 + sd zero, 22*8(t0) // Initial a5 + sd zero, 23*8(t0) // Initial a4 + sd zero, 24*8(t0) // Initial a3 + sd zero, 25*8(t0) // Initial a2 + sd zero, 26*8(t0) // Initial a1 + sd zero, 27*8(t0) // Initial a0 + sd zero, 28*8(t0) // Initial ra + li t1, 0x2000 // mstatus.FS = Initial + sd t1, 29*8(t0) // Initial mstatus + sd a1, 30*8(t0) // Initial mepc/sepc (thread entry point) +#if defined(__riscv_float_abi_single) || defined(__riscv_float_abi_double) + sd zero, 31*8(t0) // Initial ft0 + sd zero, 32*8(t0) // Initial ft1 + sd zero, 33*8(t0) // Initial ft2 + sd zero, 34*8(t0) // Initial ft3 + sd zero, 35*8(t0) // Initial ft4 + sd zero, 36*8(t0) // Initial ft5 + sd zero, 37*8(t0) // Initial ft6 + sd zero, 38*8(t0) // Initial ft7 + sd zero, 39*8(t0) // Initial fs0 + sd zero, 40*8(t0) // Initial fs1 + sd zero, 41*8(t0) // Initial fa0 + sd zero, 42*8(t0) // Initial fa1 + sd zero, 43*8(t0) // Initial fa2 + sd zero, 44*8(t0) // Initial fa3 + sd zero, 45*8(t0) // Initial fa4 + sd zero, 46*8(t0) // Initial fa5 + sd zero, 47*8(t0) // Initial fa6 + sd zero, 48*8(t0) // Initial fa7 + sd zero, 49*8(t0) // Initial fs2 + sd zero, 50*8(t0) // Initial fs3 + sd zero, 51*8(t0) // Initial fs4 + sd zero, 52*8(t0) // Initial fs5 + sd zero, 53*8(t0) // Initial fs6 + sd zero, 54*8(t0) // Initial fs7 + sd zero, 55*8(t0) // Initial fs8 + sd zero, 56*8(t0) // Initial fs9 + sd zero, 57*8(t0) // Initial fs10 + sd zero, 58*8(t0) // Initial fs11 + sd zero, 59*8(t0) // Initial ft8 + sd zero, 60*8(t0) // Initial ft9 + sd zero, 61*8(t0) // Initial ft10 + sd zero, 62*8(t0) // Initial ft11 + sd zero, 63*8(t0) // Initial fcsr (0: no sticky flags, round-to-nearest) +#endif + +#if defined(__riscv_vector) + /* Clear vector register space */ +#if defined(__riscv_float_abi_single) || defined(__riscv_float_abi_double) + addi t2, t0, 64*8 // t2 = start of vector registers +#else + addi t2, t0, 31*8 // t2 = start of vector registers +#endif + add t3, t2, t4 // t3 = end of vector registers + +vector_clear_loop: + beq t2, t3, vector_clear_done // Done if reached end + sd zero, 0(t2) // Clear 8 bytes + addi t2, t2, 8 // Move to next 8 bytes + j vector_clear_loop + +vector_clear_done: +#endif + + add t2, t0, t4 +#if defined(__riscv_float_abi_single) || defined(__riscv_float_abi_double) + sd zero, 64*8(t2) // Reserved word (0) +#else + sd zero, 31*8(t2) // Reserved word (0) +#endif + + /* Setup stack pointer. */ + /* thread_ptr -> tx_thread_stack_ptr = t0; */ + + sd t0, 8(a0) // Save stack pointer in thread's + ret // control block and return +/* } */ diff --git a/ports_smp/risc-v64_smp/gnu/src/tx_thread_system_return.S b/ports_smp/risc-v64_smp/gnu/src/tx_thread_system_return.S new file mode 100644 index 000000000..2f4daeede --- /dev/null +++ b/ports_smp/risc-v64_smp/gnu/src/tx_thread_system_return.S @@ -0,0 +1,212 @@ +/*************************************************************************** + * Copyright (c) 2024 Microsoft Corporation + * Copyright (c) 2026-present Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Component */ +/** */ +/** Thread */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + + .section .text +/**************************************************************************/ +/* */ +/* FUNCTION RELEASE */ +/* */ +/* _tx_thread_system_return RISC-V64/GNU */ +/* 6.2.1 */ +/* AUTHOR */ +/* */ +/* Scott Larson, Microsoft Corporation */ +/* */ +/* DESCRIPTION */ +/* */ +/* This function is target processor specific. It is used to transfer */ +/* control from a thread back to the system. Only a minimal context */ +/* is saved since the compiler assumes temp registers are going to get */ +/* slicked by a function call anyway. */ +/* */ +/* INPUT */ +/* */ +/* None */ +/* */ +/* OUTPUT */ +/* */ +/* None */ +/* */ +/* CALLS */ +/* */ +/* _tx_thread_schedule Thread scheduling loop */ +/* */ +/* CALLED BY */ +/* */ +/* ThreadX components */ +/* */ +/**************************************************************************/ +/* VOID _tx_thread_system_return(VOID) +{ */ +#include "tx_port.h" + .equ TX_SOL_FRAME_SIZE, TX_RISCV_SOL_FRAME_SIZE + .global _tx_thread_system_return +_tx_thread_system_return: + + /* Save minimal context on the stack. */ + /* sp -= sizeof(stack_frame); */ + + addi sp, sp, -TX_SOL_FRAME_SIZE // Allocate space on the stack +#if defined(__riscv_vector) + csrr t1, vlenb // Get vector register byte length + slli t1, t1, 5 // Multiply by 32 (number of vector registers) + addi t1, t1, 4*8 // Add vector CSR space: vstart, vtype, vl, vcsr + sub sp, sp, t1 // Allocate vector space on the stack +#endif + + /* Store floating point preserved registers. */ +#if (__riscv_flen == 32) + fsw f8, 15*8(sp) // Store fs0 + fsw f9, 16*8(sp) // Store fs1 + fsw f18, 17*8(sp) // Store fs2 + fsw f19, 18*8(sp) // Store fs3 + fsw f20, 19*8(sp) // Store fs4 + fsw f21, 20*8(sp) // Store fs5 + fsw f22, 21*8(sp) // Store fs6 + fsw f23, 22*8(sp) // Store fs7 + fsw f24, 23*8(sp) // Store fs8 + fsw f25, 24*8(sp) // Store fs9 + fsw f26, 25*8(sp) // Store fs10 + fsw f27, 26*8(sp) // Store fs11 + csrr t0, fcsr + sd t0, 27*8(sp) // Store fcsr +#elif (__riscv_flen == 64) + fsd f8, 15*8(sp) // Store fs0 + fsd f9, 16*8(sp) // Store fs1 + fsd f18, 17*8(sp) // Store fs2 + fsd f19, 18*8(sp) // Store fs3 + fsd f20, 19*8(sp) // Store fs4 + fsd f21, 20*8(sp) // Store fs5 + fsd f22, 21*8(sp) // Store fs6 + fsd f23, 22*8(sp) // Store fs7 + fsd f24, 23*8(sp) // Store fs8 + fsd f25, 24*8(sp) // Store fs9 + fsd f26, 25*8(sp) // Store fs10 + fsd f27, 26*8(sp) // Store fs11 + csrr t0, fcsr + sd t0, 27*8(sp) // Store fcsr +#endif + +#if defined(__riscv_vector) + /* Store vector registers and CSRs */ +#if defined(__riscv_float_abi_single) || defined(__riscv_float_abi_double) + addi t1, sp, 28*8 +#else + addi t1, sp, 15*8 +#endif + /* Store vector CSRs */ + csrr t2, vstart // Store vstart + sd t2, 0*8(t1) + csrr t2, vtype // Store vtype + sd t2, 1*8(t1) + csrr t2, vl // Store vl + sd t2, 2*8(t1) + csrr t2, vcsr // Store vcsr + sd t2, 3*8(t1) + + /* Store vector registers v0-v31 */ + addi t2, t1, 4*8 + vsetvli t3, zero, e8, m8, ta, ma + vse8.v v0, 0(t2) // Store v0 ~ v7 + add t2, t2, t3 + vse8.v v8, 0(t2) // Store v8 ~ v15 + add t2, t2, t3 + vse8.v v16, 0(t2) // Store v16 ~ v23 + add t2, t2, t3 + vse8.v v24, 0(t2) // Store v24 ~ v31 + add t2, t2, t3 +#endif + + sd zero, 0(sp) // Solicited stack type + sd ra, 13*8(sp) // Save return address + sd s0, 12*8(sp) // Save s0 + sd s1, 11*8(sp) // Save s1 + sd s2, 10*8(sp) // Save s2 + sd s3, 9*8(sp) // Save s3 + sd s4, 8*8(sp) // Save s4 + sd s5, 7*8(sp) // Save s5 + sd s6, 6*8(sp) // Save s6 + sd s7, 5*8(sp) // Save s7 + sd s8, 4*8(sp) // Save s8 + sd s9, 3*8(sp) // Save s9 + sd s10, 2*8(sp) // Save s10 + sd s11, 1*8(sp) // Save s11 +#ifdef TX_RISCV_SMODE + csrr t0, sstatus // Pickup sstatus + sd t0, 14*8(sp) // Save sstatus + + + /* Lockout interrupts. will be enabled in _tx_thread_schedule */ + + csrci sstatus, 0x02 // Disable interrupts (SIE bit 1) +#else + csrr t0, mstatus // Pickup mstatus + sd t0, 14*8(sp) // Save mstatus + + + /* Lockout interrupts. will be enabled in _tx_thread_schedule */ + + csrci mstatus, 0x08 // Disable interrupts (MIE bit 3) +#endif + +#ifdef TX_ENABLE_EXECUTION_CHANGE_NOTIFY + + call _tx_execution_thread_exit // Call the thread execution exit function +#endif + + la t0, _tx_thread_current_ptr // Pickup address of pointer + ld t1, 0(t0) // Pickup current thread pointer + la t2, _tx_thread_system_stack_ptr // Pickup stack pointer address + + /* Save current stack and switch to system stack. */ + /* _tx_thread_current_ptr -> tx_thread_stack_ptr = SP; + SP = _tx_thread_system_stack_ptr; */ + + sd sp, 8(t1) // Save stack pointer + ld sp, 0(t2) // Switch to system stack + + /* Determine if the time-slice is active. */ + /* if (_tx_timer_time_slice) + { */ + + la t4, _tx_timer_time_slice // Pickup time slice variable addr + lw t3, 0(t4) // Pickup time slice value + la t2, _tx_thread_schedule // Pickup address of scheduling loop + beqz t3, _tx_thread_dont_save_ts // If no time-slice, don't save it + + /* Save time-slice for the thread and clear the current time-slice. */ + /* _tx_thread_current_ptr -> tx_thread_time_slice = _tx_timer_time_slice; + _tx_timer_time_slice = 0; */ + + sw t3, 36(t1) // Save current time-slice for thread + sw zero, 0(t4) // Clear time-slice variable + + /* } */ +_tx_thread_dont_save_ts: + + /* Clear the current thread pointer. */ + /* _tx_thread_current_ptr = TX_NULL; */ + + sd x0, 0(t0) // Clear current thread pointer + jr t2 // Return to thread scheduler + +/* } */ From 86e76f11560af49db71e82b7686de28247dd1307 Mon Sep 17 00:00:00 2001 From: Eyck-Alexander Jentzsch Date: Fri, 27 Nov 2026 11:07:48 +0100 Subject: [PATCH 3/6] Added machine-mode GNU RISC-V SMP ports Adapt the corresponding single-core context code to per-hart state and atomic context publication. Keep board startup, timer setup, and IPI routing in BSP hooks. Check the C layout at build time and use the upstream RV64 timer pointer extension with a 32-bit ULONG. Verified both Debug SMP suites and both hard-float context tests on HERE VP. Assisted-by: Codex (GPT-6) --- ports_smp/risc-v-common/readme_threadx.txt | 37 ++ .../risc-v-common/tx_port_offset_check.c | 30 ++ ports_smp/risc-v-common/tx_port_riscv_smp.h | 376 ++++++++++++++++++ .../risc-v-common/tx_thread_smp_core_get.S | 16 + .../tx_thread_smp_current_state_get.S | 20 + .../tx_thread_smp_low_level_initialize.S | 16 + .../risc-v-common/tx_thread_smp_protect.S | 49 +++ .../risc-v-common/tx_thread_smp_time_get.c | 17 + .../risc-v-common/tx_thread_smp_unprotect.S | 48 +++ ports_smp/risc-v-common/tx_timer_interrupt.c | 129 ++++++ ports_smp/risc-v32_smp/gnu/CMakeLists.txt | 25 ++ ports_smp/risc-v32_smp/gnu/inc/tx_port.h | 108 +++++ .../gnu/src/tx_thread_context_restore.S | 74 ++-- .../gnu/src/tx_thread_context_save.S | 16 +- .../gnu/src/tx_thread_interrupt_control.S | 2 +- .../risc-v32_smp/gnu/src/tx_thread_schedule.S | 93 +++-- .../src/tx_thread_smp_current_thread_get.S | 25 ++ .../gnu/src/tx_thread_smp_initialize_wait.S | 50 +++ .../gnu/src/tx_thread_stack_build.S | 15 +- .../gnu/src/tx_thread_system_return.S | 75 ++-- ports_smp/risc-v64_smp/gnu/CMakeLists.txt | 25 ++ ports_smp/risc-v64_smp/gnu/inc/tx_port.h | 122 ++++++ .../gnu/src/tx_thread_context_restore.S | 219 +++------- .../gnu/src/tx_thread_context_save.S | 95 +---- .../gnu/src/tx_thread_interrupt_control.S | 17 +- .../risc-v64_smp/gnu/src/tx_thread_schedule.S | 207 +++------- .../src/tx_thread_smp_current_thread_get.S | 25 ++ .../gnu/src/tx_thread_smp_initialize_wait.S | 50 +++ .../gnu/src/tx_thread_stack_build.S | 47 +-- .../gnu/src/tx_thread_system_return.S | 131 +++--- 30 files changed, 1498 insertions(+), 661 deletions(-) create mode 100644 ports_smp/risc-v-common/readme_threadx.txt create mode 100644 ports_smp/risc-v-common/tx_port_offset_check.c create mode 100644 ports_smp/risc-v-common/tx_port_riscv_smp.h create mode 100644 ports_smp/risc-v-common/tx_thread_smp_core_get.S create mode 100644 ports_smp/risc-v-common/tx_thread_smp_current_state_get.S create mode 100644 ports_smp/risc-v-common/tx_thread_smp_low_level_initialize.S create mode 100644 ports_smp/risc-v-common/tx_thread_smp_protect.S create mode 100644 ports_smp/risc-v-common/tx_thread_smp_time_get.c create mode 100644 ports_smp/risc-v-common/tx_thread_smp_unprotect.S create mode 100644 ports_smp/risc-v-common/tx_timer_interrupt.c create mode 100644 ports_smp/risc-v32_smp/gnu/CMakeLists.txt create mode 100644 ports_smp/risc-v32_smp/gnu/inc/tx_port.h create mode 100644 ports_smp/risc-v32_smp/gnu/src/tx_thread_smp_current_thread_get.S create mode 100644 ports_smp/risc-v32_smp/gnu/src/tx_thread_smp_initialize_wait.S create mode 100644 ports_smp/risc-v64_smp/gnu/CMakeLists.txt create mode 100644 ports_smp/risc-v64_smp/gnu/inc/tx_port.h create mode 100644 ports_smp/risc-v64_smp/gnu/src/tx_thread_smp_current_thread_get.S create mode 100644 ports_smp/risc-v64_smp/gnu/src/tx_thread_smp_initialize_wait.S diff --git a/ports_smp/risc-v-common/readme_threadx.txt b/ports_smp/risc-v-common/readme_threadx.txt new file mode 100644 index 000000000..3b4d4a1f5 --- /dev/null +++ b/ports_smp/risc-v-common/readme_threadx.txt @@ -0,0 +1,37 @@ +Eclipse ThreadX SMP on RISC-V GNU + +Use THREADX_SMP=ON with THREADX_ARCH=risc-v32 or risc-v64 and +THREADX_TOOLCHAIN=gnu. The ports run in machine mode and require the A and +Zicsr extensions. RV32 supports ILP32 without hardware FP and ILP32D. RV64 +supports LP64 without hardware FP, LP64F, and LP64D. The ports use a 32-bit +ULONG on both architectures and preserve native-width pointers. + +The context sources derive from the corresponding single-core GNU ports at +227586c72edba83c9c876528ef0b2ecc9afdfa37. SMP adds per-hart kernel state, +context publication and claiming, and software interrupt wakeups. + +Board integration + +Start TX_THREAD_SMP_MAX_CORES contiguous machine-mode harts beginning at +hart 0. Provide a 16-byte aligned system stack for every hart. Hart 0 enters +ThreadX normally. Secondary harts call _tx_thread_smp_initialize_wait after +initializing their stacks. The port calls _tx_riscv_smp_hart_initialize on +each secondary hart after synchronization, with interrupts disabled. Supply +this board routine to install the local trap vector and configure the local +interrupt controller. + +Supply _tx_initialize_low_level for hart 0. Set _tx_initialize_unused_memory +and its _tx_thread_system_stack_ptr slot, initialize the board, and install +the trap vector. Make one hart-0 hardware tick call _tx_timer_interrupt per +configured ThreadX tick. Supply _tx_thread_smp_core_preempt(UINT core) to +trigger an IPI and clear the IPI in the target hart's handler. + +The trap entry reserves TX_RISCV_TRAP_FRAME_SIZE bytes and saves the +interrupted ra in slot 28 at the native register width. It calls +_tx_thread_context_save, runs the board handler, then transfers control to +_tx_thread_context_restore. Keep the stack 16-byte aligned at each call. + +The ready-token offset in each tx_port.h is a byte displacement for an +assembly atomic operation. After changing the control block, preceding +members, or thread and timer extensions, remeasure with the target compiler. +The port's C99 offset checks are built with the kernel and reject a mismatch. diff --git a/ports_smp/risc-v-common/tx_port_offset_check.c b/ports_smp/risc-v-common/tx_port_offset_check.c new file mode 100644 index 000000000..786c7c44a --- /dev/null +++ b/ports_smp/risc-v-common/tx_port_offset_check.c @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2026 Eclipse ThreadX contributors + * Portions of this file were generated with AI assistance. + * SPDX-License-Identifier: MIT + */ + +#include + +#include "tx_api.h" + +/* Keep hard-coded assembly displacements tied to the actual target C layout. */ +typedef char tx_port_assert_ready_token_offset[ + offsetof(TX_THREAD, tx_thread_smp_core_control) == TX_THREAD_SMP_LOCK_READY_BIT_OFFSET ? 1 : -1]; +typedef char tx_port_assert_ready_token_atomic_width_and_alignment[ + sizeof(((TX_THREAD *) 0)->tx_thread_smp_core_control) == 4 && + TX_THREAD_SMP_LOCK_READY_BIT_OFFSET % 4 == 0 ? 1 : -1]; +typedef char tx_port_assert_stack_ptr_offset[ + offsetof(TX_THREAD, tx_thread_stack_ptr) == TX_RISCV_TCB_STACK_PTR ? 1 : -1]; +typedef char tx_port_assert_stack_end_offset[ + offsetof(TX_THREAD, tx_thread_stack_end) == TX_RISCV_TCB_STACK_END ? 1 : -1]; +typedef char tx_port_assert_run_count_offset[ + offsetof(TX_THREAD, tx_thread_run_count) == TX_RISCV_TCB_RUN_COUNT ? 1 : -1]; +typedef char tx_port_assert_time_slice_offset[ + offsetof(TX_THREAD, tx_thread_time_slice) == TX_RISCV_TCB_TIME_SLICE ? 1 : -1]; +typedef char tx_port_assert_smp_protection_layout[ + offsetof(TX_THREAD_SMP_PROTECT, tx_thread_smp_protect_in_force) == TX_RISCV_SMP_PROTECT_IN_FORCE && + offsetof(TX_THREAD_SMP_PROTECT, tx_thread_smp_protect_core) == TX_RISCV_SMP_PROTECT_CORE && + offsetof(TX_THREAD_SMP_PROTECT, tx_thread_smp_protect_count) == TX_RISCV_SMP_PROTECT_COUNT ? 1 : -1]; +typedef char tx_port_assert_array_strides[ + sizeof(ULONG) == 4 && sizeof(UINT) == 4 && sizeof(VOID *) == __riscv_xlen / 8 ? 1 : -1]; diff --git a/ports_smp/risc-v-common/tx_port_riscv_smp.h b/ports_smp/risc-v-common/tx_port_riscv_smp.h new file mode 100644 index 000000000..b1ab751dd --- /dev/null +++ b/ports_smp/risc-v-common/tx_port_riscv_smp.h @@ -0,0 +1,376 @@ +/*************************************************************************** + * Copyright (c) 2024 Microsoft Corporation + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +/* + * Copyright (c) 2026 Eclipse ThreadX contributors + * Portions of this file were generated with AI assistance. + * SPDX-License-Identifier: MIT + */ + +#ifndef TX_PORT_RISCV_SMP_H +#define TX_PORT_RISCV_SMP_H + +#ifndef __riscv_atomic +#error "The RISC-V SMP port requires the A extension." +#endif + +#define TX_RISCV_TCB_RUN_COUNT 4 +#define TX_RISCV_TCB_STACK_PTR 8 +#define TX_RISCV_SMP_PROTECT_IN_FORCE 0 +#define TX_RISCV_SMP_PROTECT_CORE 4 +#define TX_RISCV_SMP_PROTECT_COUNT 8 + +#ifndef __ASSEMBLER__ + +/************* Define ThreadX SMP constants. *************/ + +#define TX_DISABLE_INLINE + +/* Define the ThreadX SMP maximum number of cores. */ + +#ifndef TX_THREAD_SMP_MAX_CORES +#define TX_THREAD_SMP_MAX_CORES 4 +#endif + +/* The board starts contiguous harts with IDs from zero to MAX_CORES - 1. */ +#if TX_THREAD_SMP_MAX_CORES < 1 || TX_THREAD_SMP_MAX_CORES > 32 +#error "TX_THREAD_SMP_MAX_CORES must be between 1 and 32" +#endif + +#ifndef TX_THREAD_SMP_CORE_MASK +#define TX_THREAD_SMP_CORE_MASK (0xFFFFFFFFUL >> (32 - TX_THREAD_SMP_MAX_CORES)) +#endif + +#define INLINE_DECLARE inline + +/* The board supplies _tx_thread_smp_core_preempt() to send an IPI. */ +#define TX_THREAD_SMP_INTER_CORE_INTERRUPT +#ifndef TX_THREAD_SMP_WAKEUP_LOGIC +#define TX_THREAD_SMP_WAKEUP_LOGIC +#define TX_THREAD_SMP_WAKEUP(i) _tx_thread_smp_core_preempt(i) +#else +#include "tx_thread_smp_core_wakeup.h" +#endif + +/* Ensure that the in-line resume/suspend define is not allowed. */ + +#ifdef TX_INLINE_THREAD_RESUME_SUSPEND +#undef TX_INLINE_THREAD_RESUME_SUSPEND +#endif + +/************* End ThreadX SMP constants. *************/ + +/* Define compiler library include files. */ + +#include +#include + +/* Define ThreadX basic types for this port. */ + +#define VOID void +typedef char CHAR; +typedef unsigned char UCHAR; +typedef int INT; +typedef unsigned int UINT; +typedef int LONG; +typedef unsigned int ULONG; +typedef unsigned long long ULONG64; +typedef short SHORT; +typedef unsigned short USHORT; +#define ULONG64_DEFINED + +#if __riscv_xlen == 64 +typedef unsigned long long ALIGN_TYPE; +#define ALIGN_TYPE_DEFINED + +/* Preserve a full thread pointer while the timeout callback takes ULONG. */ +#define TX_TIMER_INTERNAL_EXTENSION VOID *tx_timer_internal_extension_ptr; +#define TX_TIMER_EXTENSION_PTR_DEFINED +#define TX_THREAD_CREATE_TIMEOUT_SETUP(t) \ + (t)->tx_thread_timer.tx_timer_internal_timeout_function = &_tx_thread_timeout; \ + (t)->tx_thread_timer.tx_timer_internal_timeout_param = 0; \ + (t)->tx_thread_timer.tx_timer_internal_extension_ptr = (VOID *) (t); +#define TX_THREAD_TIMEOUT_POINTER_SETUP(t) \ + TX_PARAMETER_NOT_USED(timeout_input); \ + (t) = (TX_THREAD *) _tx_timer_expired_timer_ptr->tx_timer_internal_extension_ptr; +#endif + +/* Define the priority levels for ThreadX. Legal values range + from 32 to 1024 and MUST be evenly divisible by 32. */ + +#ifndef TX_MAX_PRIORITIES +#define TX_MAX_PRIORITIES 32 +#endif + +/* Define the minimum stack for a ThreadX thread on this processor. If the size supplied during + thread creation is less than this value, the thread create call will return an error. */ + +#ifndef TX_MINIMUM_STACK +#define TX_MINIMUM_STACK 1024 /* Minimum stack size for this port */ +#endif + +/* Define the system timer thread's default stack size and priority. These are only applicable + if TX_TIMER_PROCESS_IN_ISR is not defined. */ + +#ifndef TX_TIMER_THREAD_STACK_SIZE +#define TX_TIMER_THREAD_STACK_SIZE 1024 /* Default timer thread stack size */ +#endif + +#ifndef TX_TIMER_THREAD_PRIORITY +#define TX_TIMER_THREAD_PRIORITY 0 /* Default timer thread priority */ +#endif + +/* Define various constants for the ThreadX RISC-V port. */ + +#define TX_INT_DISABLE 0x00000000 /* Disable interrupts value */ +#define TX_INT_ENABLE 0x00000008 /* Enable interrupt value */ + +/* Define the clock source for trace event entry time stamp. The following two item are port specific. + For example, if the time source is at the address 0x0a800024 and is 16-bits in size, the clock + source constants would be: + +#define TX_TRACE_TIME_SOURCE *((ULONG *) 0x0a800024) +#define TX_TRACE_TIME_MASK 0x0000FFFFUL + +*/ + +#ifndef TX_TRACE_TIME_SOURCE +#define TX_TRACE_TIME_SOURCE ++_tx_trace_simulated_time +#endif +#ifndef TX_TRACE_TIME_MASK +#define TX_TRACE_TIME_MASK 0xFFFFFFFFUL +#endif + +/* Define the port specific options for the _tx_build_options variable. This variable indicates + how the ThreadX library was built. */ + +#define TX_PORT_SPECIFIC_BUILD_OPTIONS 0 + +/* Define the in-line initialization constant so that modules with in-line + initialization capabilities can prevent their initialization from being + a function call. */ + +#define TX_INLINE_INITIALIZATION + +/* Determine whether or not stack checking is enabled. By default, ThreadX stack checking is + disabled. When the following is defined, ThreadX thread stack checking is enabled. If stack + checking is enabled (TX_ENABLE_STACK_CHECKING is defined), the TX_DISABLE_STACK_FILLING + define is negated, thereby forcing the stack fill which is necessary for the stack checking + logic. */ + +#ifdef TX_ENABLE_STACK_CHECKING +#undef TX_DISABLE_STACK_FILLING +#endif + +/* Define the TX_THREAD control block extensions for this port. The main reason + for the multiple macros is so that backward compatibility can be maintained with + existing ThreadX kernel awareness modules. */ + +#define TX_THREAD_EXTENSION_0 +#define TX_THREAD_EXTENSION_1 +#define TX_THREAD_EXTENSION_2 +#define TX_THREAD_EXTENSION_3 + +/* Define the port extensions of the remaining ThreadX objects. */ + +#define TX_BLOCK_POOL_EXTENSION +#define TX_BYTE_POOL_EXTENSION +#define TX_EVENT_FLAGS_GROUP_EXTENSION +#define TX_MUTEX_EXTENSION +#define TX_QUEUE_EXTENSION +#define TX_SEMAPHORE_EXTENSION +#define TX_TIMER_EXTENSION + +/* Define the user extension field of the thread control block. Nothing + additional is needed for this port so it is defined as white space. */ + +#ifndef TX_THREAD_USER_EXTENSION +#define TX_THREAD_USER_EXTENSION +#endif + +/* Define the macros for processing extensions in tx_thread_create, tx_thread_delete, + tx_thread_shell_entry, and tx_thread_terminate. */ + +#define TX_THREAD_CREATE_EXTENSION(thread_ptr) +#define TX_THREAD_DELETE_EXTENSION(thread_ptr) +#define TX_THREAD_COMPLETED_EXTENSION(thread_ptr) +#define TX_THREAD_TERMINATED_EXTENSION(thread_ptr) + +/* Define automated regression test extensions. These are required for the + ThreadX regression tests. */ + +typedef unsigned int TEST_FLAG; +extern TEST_FLAG threadx_byte_allocate_loop_test; +extern TEST_FLAG threadx_byte_release_loop_test; +extern TEST_FLAG threadx_mutex_suspension_put_test; +extern TEST_FLAG threadx_mutex_suspension_priority_test; +#ifndef TX_TIMER_PROCESS_IN_ISR +extern TEST_FLAG threadx_delete_timer_thread; +#endif + +extern void abort_and_resume_byte_allocating_thread(void); +extern void abort_all_threads_suspended_on_mutex(void); +extern void suspend_lowest_priority(void); +#ifndef TX_TIMER_PROCESS_IN_ISR +extern void delete_timer_thread(void); +#endif +extern TEST_FLAG test_stack_analyze_flag; +extern TEST_FLAG test_initialize_flag; +extern TEST_FLAG test_forced_mutex_timeout; + +#ifdef TX_REGRESSION_TEST + +/* Define extension macros for automated coverage tests. */ + +#define TX_BYTE_ALLOCATE_EXTENSION \ + if (threadx_byte_allocate_loop_test == ((TEST_FLAG)1)) \ + { \ + pool_ptr->tx_byte_pool_owner = TX_NULL; \ + threadx_byte_allocate_loop_test = ((TEST_FLAG)0); \ + } + +#define TX_BYTE_RELEASE_EXTENSION \ + if (threadx_byte_release_loop_test == ((TEST_FLAG)1)) \ + { \ + threadx_byte_release_loop_test = ((TEST_FLAG)0); \ + abort_and_resume_byte_allocating_thread(); \ + } + +#define TX_MUTEX_PUT_EXTENSION_1 \ + if (threadx_mutex_suspension_put_test == ((TEST_FLAG)1)) \ + { \ + threadx_mutex_suspension_put_test = ((TEST_FLAG)0); \ + abort_all_threads_suspended_on_mutex(); \ + } + +#define TX_MUTEX_PUT_EXTENSION_2 \ + if (test_forced_mutex_timeout == ((TEST_FLAG)1)) \ + { \ + test_forced_mutex_timeout = ((TEST_FLAG)0); \ + _tx_thread_wait_abort(mutex_ptr->tx_mutex_suspension_list); \ + } + +#define TX_MUTEX_PRIORITY_CHANGE_EXTENSION \ + if (threadx_mutex_suspension_priority_test == ((TEST_FLAG)1)) \ + { \ + threadx_mutex_suspension_priority_test = ((TEST_FLAG)0); \ + suspend_lowest_priority(); \ + } + +#ifndef TX_TIMER_PROCESS_IN_ISR + +#define TX_TIMER_INITIALIZE_EXTENSION(a) \ + if (threadx_delete_timer_thread == ((TEST_FLAG)1)) \ + { \ + threadx_delete_timer_thread = ((TEST_FLAG)0); \ + delete_timer_thread(); \ + (a) = ((UINT)1); \ + } + +#endif + +#define TX_THREAD_STACK_ANALYZE_EXTENSION \ + if (test_stack_analyze_flag == ((TEST_FLAG)1)) \ + { \ + thread_ptr->tx_thread_id = ((TEST_FLAG)0); \ + test_stack_analyze_flag = ((TEST_FLAG)0); \ + } \ + else if (test_stack_analyze_flag == ((TEST_FLAG)2)) \ + { \ + stack_ptr = thread_ptr->tx_thread_stack_start; \ + test_stack_analyze_flag = ((TEST_FLAG)0); \ + } \ + else if (test_stack_analyze_flag == ((TEST_FLAG)3)) \ + { \ + *stack_ptr = TX_STACK_FILL; \ + test_stack_analyze_flag = ((TEST_FLAG)0); \ + } \ + else \ + { \ + test_stack_analyze_flag = ((TEST_FLAG)0); \ + } + +#define TX_INITIALIZE_KERNEL_ENTER_EXTENSION \ + if (test_initialize_flag == ((TEST_FLAG)1)) \ + { \ + test_initialize_flag = ((TEST_FLAG)0); \ + return; \ + } + +#endif + +/* Define the ThreadX object creation extensions for the remaining objects. */ + +#define TX_BLOCK_POOL_CREATE_EXTENSION(pool_ptr) +#define TX_BYTE_POOL_CREATE_EXTENSION(pool_ptr) +#define TX_EVENT_FLAGS_GROUP_CREATE_EXTENSION(group_ptr) +#define TX_MUTEX_CREATE_EXTENSION(mutex_ptr) +#define TX_QUEUE_CREATE_EXTENSION(queue_ptr) +#define TX_SEMAPHORE_CREATE_EXTENSION(semaphore_ptr) +#define TX_TIMER_CREATE_EXTENSION(timer_ptr) + +/* Define the ThreadX object deletion extensions for the remaining objects. */ + +#define TX_BLOCK_POOL_DELETE_EXTENSION(pool_ptr) +#define TX_BYTE_POOL_DELETE_EXTENSION(pool_ptr) +#define TX_EVENT_FLAGS_GROUP_DELETE_EXTENSION(group_ptr) +#define TX_MUTEX_DELETE_EXTENSION(mutex_ptr) +#define TX_QUEUE_DELETE_EXTENSION(queue_ptr) +#define TX_SEMAPHORE_DELETE_EXTENSION(semaphore_ptr) +#define TX_TIMER_DELETE_EXTENSION(timer_ptr) + +/************* Define ThreadX SMP data types and function prototypes. *************/ + +struct TX_THREAD_STRUCT; + +/* Define the ThreadX SMP protection structure. */ + +typedef struct TX_THREAD_SMP_PROTECT_STRUCT +{ + ULONG tx_thread_smp_protect_in_force; + ULONG tx_thread_smp_protect_core; + ULONG tx_thread_smp_protect_count; + ULONG tx_thread_smp_protect_pad_0; + ULONG tx_thread_smp_protect_pad_1; + ULONG tx_thread_smp_protect_pad_2; + ULONG tx_thread_smp_protect_pad_3; +} TX_THREAD_SMP_PROTECT; + +/* Define ThreadX interrupt lockout and restore macros for protection on + access of critical kernel information. The restore interrupt macro must + restore the interrupt posture of the running thread prior to the value + present prior to the disable macro. In most cases, the save area macro + is used to define a local function save area for the disable and restore + macros. */ + +#define TX_INTERRUPT_SAVE_AREA ULONG tx_saved_posture; + +#define TX_DISABLE tx_saved_posture = _tx_thread_smp_protect(); +#define TX_RESTORE _tx_thread_smp_unprotect(tx_saved_posture); + +/************* End ThreadX SMP data type and function prototype definitions. *************/ + +/* Define the interrupt lockout macros for each ThreadX object. */ + +#define TX_BLOCK_POOL_DISABLE TX_DISABLE +#define TX_BYTE_POOL_DISABLE TX_DISABLE +#define TX_EVENT_FLAGS_GROUP_DISABLE TX_DISABLE +#define TX_MUTEX_DISABLE TX_DISABLE +#define TX_QUEUE_DISABLE TX_DISABLE +#define TX_SEMAPHORE_DISABLE TX_DISABLE + +#ifdef TX_THREAD_INIT +CHAR _tx_version_id[] = "Eclipse ThreadX RISC-V/GNU SMP"; +#else +extern CHAR _tx_version_id[]; +#endif + +#endif /* __ASSEMBLER__ */ +#endif /* TX_PORT_RISCV_SMP_H */ diff --git a/ports_smp/risc-v-common/tx_thread_smp_core_get.S b/ports_smp/risc-v-common/tx_thread_smp_core_get.S new file mode 100644 index 000000000..8c18625ec --- /dev/null +++ b/ports_smp/risc-v-common/tx_thread_smp_core_get.S @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2026 Eclipse ThreadX contributors + * Portions of this file were generated with AI assistance. + * SPDX-License-Identifier: MIT + */ + +#include "tx_port.h" + + .section .text + .align 2 + + .global _tx_thread_smp_core_get + .type _tx_thread_smp_core_get, @function +_tx_thread_smp_core_get: + csrr a0, mhartid // Pickup the current hart ID + ret diff --git a/ports_smp/risc-v-common/tx_thread_smp_current_state_get.S b/ports_smp/risc-v-common/tx_thread_smp_current_state_get.S new file mode 100644 index 000000000..f76d1737f --- /dev/null +++ b/ports_smp/risc-v-common/tx_thread_smp_current_state_get.S @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2026 Eclipse ThreadX contributors + * Portions of this file were generated with AI assistance. + * SPDX-License-Identifier: MIT + */ + +#include "tx_port.h" + + .section .text + .align 2 + + .global _tx_thread_smp_current_state_get + .type _tx_thread_smp_current_state_get, @function +_tx_thread_smp_current_state_get: + csrr t0, mhartid // Pickup current hart ID + la t1, _tx_thread_system_state // Base of per-hart system-state array + slli t0, t0, 2 // Build offset into array + add t1, t1, t0 // Select this hart's slot + lw a0, 0(t1) // Return current system state + ret diff --git a/ports_smp/risc-v-common/tx_thread_smp_low_level_initialize.S b/ports_smp/risc-v-common/tx_thread_smp_low_level_initialize.S new file mode 100644 index 000000000..0bc0e7f3c --- /dev/null +++ b/ports_smp/risc-v-common/tx_thread_smp_low_level_initialize.S @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2026 Eclipse ThreadX contributors + * Portions of this file were generated with AI assistance. + * SPDX-License-Identifier: MIT + */ + +#include "tx_port.h" + + .section .text + .align 2 + + .global _tx_thread_smp_low_level_initialize + .type _tx_thread_smp_low_level_initialize, @function +_tx_thread_smp_low_level_initialize: + /* The board starts the fixed set of secondary harts. */ + ret diff --git a/ports_smp/risc-v-common/tx_thread_smp_protect.S b/ports_smp/risc-v-common/tx_thread_smp_protect.S new file mode 100644 index 000000000..79dddcc98 --- /dev/null +++ b/ports_smp/risc-v-common/tx_thread_smp_protect.S @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2026 Eclipse ThreadX contributors + * Portions of this file were generated with AI assistance. + * SPDX-License-Identifier: MIT + */ + +#include "tx_port.h" + + .section .text + .align 2 + + .global _tx_thread_smp_protect + .type _tx_thread_smp_protect, @function +_tx_thread_smp_protect: + + /* Disable interrupts so we don't get preempted. */ + csrrci a0, mstatus, 0x08 // Save and disable MIE + andi a0, a0, 0x08 // Return only the interrupt posture + + /* Pickup the hart ID. */ + csrr t2, mhartid // Pickup the current hart ID + + /* Build address to protection structure. */ + la t1, _tx_thread_smp_protection + + /* If this hart already owns protection, just nest the count. */ + lw t3, TX_RISCV_SMP_PROTECT_CORE(t1) // Pickup owning hart + beq t3, t2, _owned // Already owned by this hart + + /* Try to get the protection. */ + lw t4, 0(t1) // Pickup protection flag + bnez t4, _protection_busy // If set, protection is busy + li t4, 1 // Build lock value + amoswap.w.aq t5, t4, (t1) // Attempt to get protection + bnez t5, _protection_busy // If old value != 0, retry + + fence rw, rw // Ensure lock acquisition is visible + sw t2, TX_RISCV_SMP_PROTECT_CORE(t1) // Save owning hart + +_owned: + lw t5, TX_RISCV_SMP_PROTECT_COUNT(t1) // Pickup ownership count + addi t5, t5, 1 // Increment ownership count + sw t5, TX_RISCV_SMP_PROTECT_COUNT(t1) // Store ownership count + fence rw, rw // Publish owner/count before return + ret + +_protection_busy: + csrs mstatus, a0 // Restore interrupts + j _tx_thread_smp_protect // Restart the protection attempt diff --git a/ports_smp/risc-v-common/tx_thread_smp_time_get.c b/ports_smp/risc-v-common/tx_thread_smp_time_get.c new file mode 100644 index 000000000..15ca0cffe --- /dev/null +++ b/ports_smp/risc-v-common/tx_thread_smp_time_get.c @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2026 Eclipse ThreadX contributors + * Portions of this file were generated with AI assistance. + * SPDX-License-Identifier: MIT + */ + +#include "tx_api.h" + +#ifdef TX_THREAD_SMP_DEBUG_ENABLE +ULONG _tx_thread_smp_time_get(VOID) +{ +ULONG timestamp; + + __asm__ volatile ("rdtime %0" : "=r" (timestamp)); + return timestamp; +} +#endif diff --git a/ports_smp/risc-v-common/tx_thread_smp_unprotect.S b/ports_smp/risc-v-common/tx_thread_smp_unprotect.S new file mode 100644 index 000000000..d9c912478 --- /dev/null +++ b/ports_smp/risc-v-common/tx_thread_smp_unprotect.S @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2026 Eclipse ThreadX contributors + * Portions of this file were generated with AI assistance. + * SPDX-License-Identifier: MIT + */ + +#include "tx_port.h" + + .section .text + .align 2 + + .global _tx_thread_smp_unprotect + .type _tx_thread_smp_unprotect, @function +_tx_thread_smp_unprotect: + + /* Lockout interrupts while protection state is updated. */ + csrci mstatus, 0x08 // Lockout interrupts + + /* Pickup the current hart ID. */ + csrr t1, mhartid // Pickup hart ID + + /* Build address of protection structure. */ + la t2, _tx_thread_smp_protection + + /* Only the owning hart may release the protection. */ + lw t3, TX_RISCV_SMP_PROTECT_CORE(t2) // Pickup owning hart + bne t1, t3, _still_protected // Not owner, skip release + + /* Pickup and decrement the protection count. */ + lw t3, TX_RISCV_SMP_PROTECT_COUNT(t2) // Pickup protection count + beqz t3, _still_protected // Already cleared + addi t3, t3, -1 // Decrement protection count + sw t3, TX_RISCV_SMP_PROTECT_COUNT(t2) // Store new count + bnez t3, _still_protected // Still nested, stay protected + + /* If preemption is disabled, keep protection in force. */ + la t4, _tx_thread_preempt_disable + lw t5, 0(t4) // Pickup preempt disable + bnez t5, _still_protected // Skip protection release + + /* Release the protection. */ + li t3, -1 // Invalid owner value + sw t3, TX_RISCV_SMP_PROTECT_CORE(t2) // Mark owning hart invalid + amoswap.w.rl x0, x0, (t2) // Release protection flag + +_still_protected: + csrs mstatus, a0 // Restore interrupt posture + ret diff --git a/ports_smp/risc-v-common/tx_timer_interrupt.c b/ports_smp/risc-v-common/tx_timer_interrupt.c new file mode 100644 index 000000000..52cfbc06d --- /dev/null +++ b/ports_smp/risc-v-common/tx_timer_interrupt.c @@ -0,0 +1,129 @@ +/*************************************************************************** + * Copyright (c) 2024 Microsoft Corporation + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Component */ +/** */ +/** Timer */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + +#define TX_SOURCE_CODE +#define TX_THREAD_SMP_SOURCE_CODE + +/* Include necessary system files. */ + +#include "tx_api.h" +#include "tx_thread.h" +#include "tx_timer.h" + +/**************************************************************************/ +/* */ +/* FUNCTION RELEASE */ +/* */ +/* _tx_timer_interrupt RISC-V64/GNU */ +/* 6.2.1 */ +/* AUTHOR */ +/* */ +/* Scott Larson, Microsoft Corporation */ +/* */ +/* DESCRIPTION */ +/* */ +/* This function processes the hardware timer interrupt. This */ +/* processing includes incrementing the system clock and checking for */ +/* time slice and/or timer expiration. If either is found, the */ +/* interrupt context save/restore functions are called along with the */ +/* expiration functions. */ +/* */ +/* INPUT */ +/* */ +/* None */ +/* */ +/* OUTPUT */ +/* */ +/* None */ +/* */ +/* CALLS */ +/* */ +/* _tx_timer_expiration_process Timer expiration processing */ +/* _tx_thread_time_slice Time slice interrupted thread */ +/* */ +/* CALLED BY */ +/* */ +/* interrupt vector */ +/* */ +/* RELEASE HISTORY */ +/* */ +/* DATE NAME DESCRIPTION */ +/* */ +/* 03-08-2023 Scott Larson Initial Version 6.2.1 */ +/* */ +/**************************************************************************/ +VOID _tx_timer_interrupt(VOID) +{ +ULONG saved_posture; + + + /* Only core 0 advances the global timer wheel. */ + if (TX_SMP_CORE_ID != ((UINT) 0)) + { + return; + } + + /* Get the protection. */ + saved_posture = _tx_thread_smp_protect(); + + /* Indicate timer interrupt processing is active. */ + _tx_timer_interrupt_active++; + + /* Increment system clock. */ + _tx_timer_system_clock++; + + /* Test for timer expiration. */ + if (*_tx_timer_current_ptr) + { + + /* Set expiration flag. */ + _tx_timer_expired = TX_TRUE; + } + else + { + + /* No timer expired, increment the timer pointer. */ + _tx_timer_current_ptr++; + + /* Check for wrap-around. */ + if (_tx_timer_current_ptr == _tx_timer_list_end) + { + + /* Wrap to beginning of list. */ + _tx_timer_current_ptr = _tx_timer_list_start; + } + } + + /* Did a timer expire? */ + if (_tx_timer_expired) + { + /* Process timer expiration. */ + _tx_timer_expiration_process(); + } + + /* Process time-slice expiration for all cores. */ + _tx_thread_time_slice(); + + /* Timer interrupt processing is no longer active. */ + _tx_timer_interrupt_active--; + + /* Release the protection. */ + _tx_thread_smp_unprotect(saved_posture); +} diff --git a/ports_smp/risc-v32_smp/gnu/CMakeLists.txt b/ports_smp/risc-v32_smp/gnu/CMakeLists.txt new file mode 100644 index 000000000..0f2fdca4d --- /dev/null +++ b/ports_smp/risc-v32_smp/gnu/CMakeLists.txt @@ -0,0 +1,25 @@ +# Copyright (c) 2026 Eclipse ThreadX contributors +# Portions of this file were generated with AI assistance. +# SPDX-License-Identifier: MIT + +set(TX_RISCV_SMP_COMMON ${CMAKE_CURRENT_LIST_DIR}/../../risc-v-common) + +target_sources(${PROJECT_NAME} PRIVATE + src/tx_thread_context_restore.S + src/tx_thread_context_save.S + src/tx_thread_interrupt_control.S + src/tx_thread_schedule.S + src/tx_thread_smp_current_thread_get.S + src/tx_thread_smp_initialize_wait.S + src/tx_thread_stack_build.S + src/tx_thread_system_return.S + ${TX_RISCV_SMP_COMMON}/tx_port_offset_check.c + ${TX_RISCV_SMP_COMMON}/tx_thread_smp_core_get.S + ${TX_RISCV_SMP_COMMON}/tx_thread_smp_current_state_get.S + ${TX_RISCV_SMP_COMMON}/tx_thread_smp_low_level_initialize.S + ${TX_RISCV_SMP_COMMON}/tx_thread_smp_protect.S + ${TX_RISCV_SMP_COMMON}/tx_thread_smp_time_get.c + ${TX_RISCV_SMP_COMMON}/tx_thread_smp_unprotect.S + ${TX_RISCV_SMP_COMMON}/tx_timer_interrupt.c +) +target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/inc) diff --git a/ports_smp/risc-v32_smp/gnu/inc/tx_port.h b/ports_smp/risc-v32_smp/gnu/inc/tx_port.h new file mode 100644 index 000000000..93b88c34d --- /dev/null +++ b/ports_smp/risc-v32_smp/gnu/inc/tx_port.h @@ -0,0 +1,108 @@ +/*************************************************************************** + * Copyright (c) 2025 10xEngineers + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Component */ +/** */ +/** Port Specific */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + + +/**************************************************************************/ +/* */ +/* PORT SPECIFIC C INFORMATION RELEASE */ +/* */ +/* tx_port.h RISC-V32/GNU */ +/* 6.5.1.202602a */ +/* */ +/* AUTHOR */ +/* */ +/* Akif Ejaz, 10xEngineers */ +/* */ +/* DESCRIPTION */ +/* */ +/* This file contains data type definitions that make the ThreadX */ +/* real-time kernel function identically on a variety of different */ +/* processor architectures. For example, the size or number of bits */ +/* in an "int" data type vary between microprocessor architectures and */ +/* even C compilers for the same microprocessor. ThreadX does not */ +/* directly use native C data types. Instead, ThreadX creates its */ +/* own special types that can be mapped to actual data types by this */ +/* file to guarantee consistency in the interface and functionality. */ +/* */ +/**************************************************************************/ + +// Portions of this file were generated with AI assistance. +#ifndef TX_PORT_H +#define TX_PORT_H + +#ifdef __riscv_vector +#error "The ThreadX RISC-V32 GNU port does not save or restore vector state. Build without the V extension." +#endif + +#ifdef __riscv_32e +#error "The ThreadX RISC-V32 GNU port requires the RV32I register set. RV32E is not supported." +#endif + +#ifdef __riscv_float_abi_single +#error "The ThreadX RISC-V32 GNU port does not support ILP32F. Use ILP32D or a soft-float ABI." +#endif + +#ifdef __riscv_float_abi_quad +#error "The ThreadX RISC-V32 GNU port does not support ILP32Q." +#endif + +#if defined(__riscv_flen) && !defined(__riscv_float_abi_double) +#error "The ThreadX RISC-V32 GNU port requires ILP32D when the ISA enables floating-point state." +#endif + +#if defined(__riscv_float_abi_double) && (!defined(__riscv_flen) || (__riscv_flen != 64)) +#error "The ThreadX RISC-V32 GNU ILP32D port requires FLEN=64." +#endif + +/* Publish the interrupt frame contract to GNU BSP assembly files. */ +#if defined(__riscv_float_abi_double) +#define TX_RISCV_TRAP_FRAME_SIZE 400 +#else +#define TX_RISCV_TRAP_FRAME_SIZE 128 +#endif +#define TX_RISCV_TRAP_CALL_FRAME_SIZE 16 + +/* Solicited (cooperative) frame built by _tx_thread_system_return: fs0-fs11 + as doubles plus fcsr with FP, callee-saved integer state without. */ +#if defined(__riscv_float_abi_double) +#define TX_RISCV_SOL_FRAME_SIZE 176 +#else +#define TX_RISCV_SOL_FRAME_SIZE 128 +#endif + +#ifdef TX_RISCV_SMODE +#error "The RISC-V SMP port requires machine mode." +#endif + +#ifdef TX_INCLUDE_USER_DEFINE_FILE +#include "tx_user.h" +#endif + +/* Assembly atomically updates this byte displacement in TX_THREAD. Recheck + it when ULONG, preceding members, or thread and timer extensions change. + tx_port_offset_check.c verifies it against the target C layout. */ +#define TX_THREAD_SMP_LOCK_READY_BIT_OFFSET 152 +#define TX_RISCV_TCB_STACK_END 16 +#define TX_RISCV_TCB_TIME_SLICE 24 + +#include "../../../risc-v-common/tx_port_riscv_smp.h" + +#endif /* TX_PORT_H */ diff --git a/ports_smp/risc-v32_smp/gnu/src/tx_thread_context_restore.S b/ports_smp/risc-v32_smp/gnu/src/tx_thread_context_restore.S index c48546433..90121f010 100644 --- a/ports_smp/risc-v32_smp/gnu/src/tx_thread_context_restore.S +++ b/ports_smp/risc-v32_smp/gnu/src/tx_thread_context_restore.S @@ -1,12 +1,15 @@ /*************************************************************************** * Copyright (c) 2025 10xEngineers + * Copyright (c) 2026 Eclipse ThreadX contributors * * This program and the accompanying materials are made available under the * terms of the MIT License which is available at * https://opensource.org/licenses/MIT. * * SPDX-License-Identifier: MIT + **************************************************************************/ +/* Portions of this file were generated with AI assistance. */ /**************************************************************************/ @@ -32,7 +35,7 @@ /* */ /* FUNCTION RELEASE */ /* */ -/* _tx_thread_context_restore RISC-V32/GNU */ +/* _tx_thread_context_restore RISC-V32/GNU SMP */ /* 6.4.x */ /* AUTHOR */ /* */ @@ -81,6 +84,9 @@ _tx_thread_context_restore: { */ la t0, _tx_thread_system_state // Pickup addr of nested interrupt count + csrr t1, mhartid + slli t1, t1, 2 + add t0, t0, t1 lw t1, 0(t0) // Pickup nested interrupt count addi t1, t1, -1 // Decrement the nested interrupt counter sw t1, 0(t0) // Store new nested count @@ -178,28 +184,27 @@ _tx_thread_skip_fp_restore: /* } */ _tx_thread_not_nested_restore: - /* Determine if a thread was interrupted and no preemption is required. */ - /* else if (((_tx_thread_current_ptr) && (_tx_thread_current_ptr == _tx_thread_execute_ptr) - || (_tx_thread_preempt_disable)) - { */ - - la t0, _tx_thread_current_ptr // Pickup current thread pointer address - lw t1, 0(t0) // Pickup current thread pointer - - beqz t1, _tx_thread_idle_system_restore // If NULL, idle system restore - - - la t0, _tx_thread_preempt_disable // Pickup preempt disable flag address - lw t2, 0(t0) // Pickup preempt disable flag (UINT) - - bnez t2, _tx_thread_no_preempt_restore // If set, restore interrupted thread - - - la t0, _tx_thread_execute_ptr // Pickup thread execute pointer address - lw t2, 0(t0) // Pickup thread execute pointer - - bne t1, t2, _tx_thread_preempt_restore // If higher-priority thread is ready, preempt + /* The preemption-disable count belongs to the hart holding protection. */ + csrr t4, mhartid + slli t5, t4, 2 + la t0, _tx_thread_current_ptr + add t0, t0, t5 + lw t1, 0(t0) + beqz t1, _tx_thread_idle_system_restore + + la t0, _tx_thread_execute_ptr + add t0, t0, t5 + lw t2, 0(t0) + beq t1, t2, _tx_thread_no_preempt_restore + + la t0, _tx_thread_smp_protection + lw t2, TX_RISCV_SMP_PROTECT_CORE(t0) + bne t2, t4, _tx_thread_preempt_restore + la t0, _tx_thread_preempt_disable + lw t2, 0(t0) + bnez t2, _tx_thread_no_preempt_restore + j _tx_thread_preempt_restore _tx_thread_no_preempt_restore: /* Restore interrupted thread or ISR. */ @@ -207,7 +212,7 @@ _tx_thread_no_preempt_restore: /* Pickup the saved stack pointer. */ /* sp = _tx_thread_current_ptr -> tx_thread_stack_ptr; */ - lw sp, 8(t1) // Switch back to thread's stack + lw sp, TX_RISCV_TCB_STACK_PTR(t1) // Switch back to thread's stack /* Recover floating point registers only if saved mstatus.FS was not Off. */ #if defined(__riscv_float_abi_double) @@ -292,7 +297,7 @@ _tx_thread_preempt_restore: /* Instead of directly activating the thread again, ensure we save the entire stack frame by saving the remaining registers. */ - lw t0, 8(t1) // Pickup thread's stack pointer + lw t0, TX_RISCV_TCB_STACK_PTR(t1) // Pickup thread's stack pointer ori t3, zero, 1 // Build interrupt stack type sw t3, 0(t0) // Store stack type @@ -337,28 +342,27 @@ _tx_thread_preempt_skip_fp_restore: { */ la t0, _tx_timer_time_slice // Pickup time slice variable address + slli t5, t4, 2 + add t0, t0, t5 lw t2, 0(t0) // Pickup time slice beqz t2, _tx_thread_dont_save_ts // If 0, skip time slice processing /* _tx_thread_current_ptr -> tx_thread_time_slice = _tx_timer_time_slice _tx_timer_time_slice = 0; */ - sw t2, 24(t1) // Save current time slice + sw t2, TX_RISCV_TCB_TIME_SLICE(t1) // Save current time slice sw x0, 0(t0) // Clear global time slice /* } */ _tx_thread_dont_save_ts: - /* Clear the current task pointer. */ - /* _tx_thread_current_ptr = TX_NULL; */ - - /* Return to the scheduler. */ - /* _tx_thread_schedule(); */ - - la t0, _tx_thread_current_ptr // Pickup current thread pointer address - sw x0, 0(t0) // Clear current thread pointer - - /* } */ + la t0, _tx_thread_current_ptr + slli t5, t4, 2 + add t0, t0, t5 + sw zero, 0(t0) + addi t0, t1, TX_THREAD_SMP_LOCK_READY_BIT_OFFSET + li t2, 1 + amoswap.w.rl zero, t2, (t0) _tx_thread_idle_system_restore: /* Just return back to the scheduler! */ diff --git a/ports_smp/risc-v32_smp/gnu/src/tx_thread_context_save.S b/ports_smp/risc-v32_smp/gnu/src/tx_thread_context_save.S index e93d5870d..a681e5c63 100644 --- a/ports_smp/risc-v32_smp/gnu/src/tx_thread_context_save.S +++ b/ports_smp/risc-v32_smp/gnu/src/tx_thread_context_save.S @@ -1,13 +1,16 @@ /*************************************************************************** * Copyright (c) 2025 10xEngineers * Copyright (c) 2026 Eclipse ThreadX contributors + * Copyright (c) 2026 Eclipse ThreadX contributors * * This program and the accompanying materials are made available under the * terms of the MIT License which is available at * https://opensource.org/licenses/MIT. * * SPDX-License-Identifier: MIT + **************************************************************************/ +/* Portions of this file were generated with AI assistance. */ // Portions of this file were generated with AI assistance. @@ -30,7 +33,7 @@ /* */ /* FUNCTION RELEASE */ /* */ -/* _tx_thread_context_save RISC-V32/GNU */ +/* _tx_thread_context_save RISC-V32/GNU SMP */ /* 6.4.x */ /* AUTHOR */ /* */ @@ -79,6 +82,9 @@ _tx_thread_context_save: { */ la t0, _tx_thread_system_state // Pickup addr of system state var + csrr t1, mhartid + slli t1, t1, 2 + add t0, t0, t1 lw t1, 0(t0) // Pickup system state addi t1, t1, 1 // Increment system state sw t1, 0(t0) // Store system state @@ -141,6 +147,9 @@ _tx_thread_skip_fpu_save: sw t0, 30*4(sp) // Save it on the stack la t1, _tx_thread_current_ptr // Pickup address of current thread ptr + csrr t0, mhartid + slli t0, t0, 2 + add t1, t1, t0 lw t2, 0(t1) // Pickup current thread pointer beqz t2, _tx_thread_idle_system_save // If NULL, idle system was interrupted @@ -148,8 +157,11 @@ _tx_thread_skip_fpu_save: /* _tx_thread_current_ptr -> tx_thread_stack_ptr = sp; sp = _tx_thread_system_stack_ptr; */ - sw sp, 8(t2) // Save stack pointer + sw sp, TX_RISCV_TCB_STACK_PTR(t2) // Save stack pointer la t0, _tx_thread_system_stack_ptr + csrr t1, mhartid + slli t1, t1, 2 + add t0, t0, t1 lw sp, 0(t0) // Switch to system stack /* Call the ISR execution exit function if enabled. */ diff --git a/ports_smp/risc-v32_smp/gnu/src/tx_thread_interrupt_control.S b/ports_smp/risc-v32_smp/gnu/src/tx_thread_interrupt_control.S index aab2955b5..6c6880d2b 100644 --- a/ports_smp/risc-v32_smp/gnu/src/tx_thread_interrupt_control.S +++ b/ports_smp/risc-v32_smp/gnu/src/tx_thread_interrupt_control.S @@ -25,7 +25,7 @@ /* */ /* FUNCTION RELEASE */ /* */ -/* _tx_thread_interrupt_control RISC-V32/GNU */ +/* _tx_thread_interrupt_control RISC-V32/GNU SMP */ /* 6.4.x */ /* AUTHOR */ /* */ diff --git a/ports_smp/risc-v32_smp/gnu/src/tx_thread_schedule.S b/ports_smp/risc-v32_smp/gnu/src/tx_thread_schedule.S index 7d2c41917..36fb7bd8a 100644 --- a/ports_smp/risc-v32_smp/gnu/src/tx_thread_schedule.S +++ b/ports_smp/risc-v32_smp/gnu/src/tx_thread_schedule.S @@ -1,12 +1,15 @@ /*************************************************************************** * Copyright (c) 2025 10xEngineers + * Copyright (c) 2026 Eclipse ThreadX contributors * * This program and the accompanying materials are made available under the * terms of the MIT License which is available at * https://opensource.org/licenses/MIT. * * SPDX-License-Identifier: MIT + **************************************************************************/ +/* Portions of this file were generated with AI assistance. */ /**************************************************************************/ @@ -30,7 +33,7 @@ /* */ /* FUNCTION RELEASE */ /* */ -/* _tx_thread_schedule RISC-V32/GNU */ +/* _tx_thread_schedule RISC-V32/GNU SMP */ /* 6.4.x */ /* AUTHOR */ /* */ @@ -65,59 +68,60 @@ { */ .global _tx_thread_schedule _tx_thread_schedule: + csrr t4, mhartid + slli t5, t4, 2 + la t0, _tx_thread_execute_ptr + add t0, t0, t5 + la a0, _tx_thread_current_ptr + add a0, a0, t5 - /* Enable interrupts. */ - - csrsi mstatus, 0x08 // Enable interrupts (MIE bit 3) - - /* Wait for a thread to execute. */ - /* do - { */ _tx_thread_schedule_loop: - - la t0, _tx_thread_execute_ptr // Pickup address of execute ptr - lw t1, 0(t0) // Pickup execute pointer - bnez t1, _tx_thread_ready_to_run // If non-NULL, a thread is ready to run - -#ifndef TX_NO_WFI - wfi // Wait for interrupt + csrci mstatus, 0x08 + lw t1, 0(t0) + bnez t1, _tx_thread_schedule_thread +#ifdef TX_ENABLE_WFI + /* A pending IPI wakes WFI while MIE is clear. Service it afterwards. */ + wfi #endif - j _tx_thread_schedule_loop // Check again - - /* } - while (_tx_thread_execute_ptr == TX_NULL); */ - -_tx_thread_ready_to_run: - - /* At this point, t1 contains the pointer to the thread to execute. - Lockout interrupts. */ - - csrci mstatus, 0x08 // Disable interrupts (MIE bit 3) - - /* Check _tx_thread_execute_ptr again, in case an interrupt occurred - between the check and the disable. */ - - lw t1, 0(t0) // Pickup execute pointer - beqz t1, _tx_thread_schedule_loop // If NULL, go back to wait loop - - /* Yes! We have a thread to execute. */ - /* _tx_thread_current_ptr = _tx_thread_execute_ptr; */ - - la t0, _tx_thread_current_ptr // Pickup address of current thread - sw t1, 0(t0) // Setup current thread pointer + csrsi mstatus, 0x08 + j _tx_thread_schedule_loop + +_tx_thread_schedule_thread: + /* Claim the thread before publishing that it runs on this hart. */ + addi t2, t1, TX_THREAD_SMP_LOCK_READY_BIT_OFFSET + amoswap.w.aq t3, zero, (t2) + beqz t3, _tx_thread_schedule_retry + +scheduler_found_thread: + sw t1, 0(a0) + fence rw, rw + lw t6, 0(t0) + beq t1, t6, _tx_thread_execute_unchanged + + /* The execute slot changed while the thread was being claimed. */ + sw zero, 0(a0) + li t3, 1 + amoswap.w.rl zero, t3, (t2) +_tx_thread_schedule_retry: + csrsi mstatus, 0x08 + j _tx_thread_schedule_loop + +_tx_thread_execute_unchanged: /* Increment the run count for this thread. */ /* _tx_thread_current_ptr -> tx_thread_run_count++; */ - lw t2, 4(t1) // Pickup run count + lw t2, TX_RISCV_TCB_RUN_COUNT(t1) // Pickup run count addi t2, t2, 1 // Increment run count - sw t2, 4(t1) // Store run count + sw t2, TX_RISCV_TCB_RUN_COUNT(t1) // Store run count /* Setup time-slice values. */ /* _tx_timer_time_slice = _tx_thread_current_ptr -> tx_thread_time_slice; */ - lw t2, 24(t1) // Pickup thread time-slice + lw t2, TX_RISCV_TCB_TIME_SLICE(t1) // Pickup thread time-slice la t3, _tx_timer_time_slice // Pickup address of time-slice + slli t5, t4, 2 + add t3, t3, t5 sw t2, 0(t3) // Setup time-slice /* Call the thread execution enter function if enabled. */ @@ -129,7 +133,12 @@ _tx_thread_ready_to_run: /* Switch to the thread's stack. */ /* sp = _tx_thread_current_ptr -> tx_thread_stack_ptr; */ - lw sp, 8(t1) // Switch to thread stack + la t0, _tx_thread_current_ptr + csrr t1, mhartid + slli t1, t1, 2 + add t0, t0, t1 + lw t1, 0(t0) + lw sp, TX_RISCV_TCB_STACK_PTR(t1) // Switch to thread stack /* Determine the type of stack frame. */ /* if (*sp) diff --git a/ports_smp/risc-v32_smp/gnu/src/tx_thread_smp_current_thread_get.S b/ports_smp/risc-v32_smp/gnu/src/tx_thread_smp_current_thread_get.S new file mode 100644 index 000000000..0d324f335 --- /dev/null +++ b/ports_smp/risc-v32_smp/gnu/src/tx_thread_smp_current_thread_get.S @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2026 Eclipse ThreadX contributors + * Portions of this file were generated with AI assistance. + * SPDX-License-Identifier: MIT + */ + +#include "tx_port.h" + + .section .text + .align 2 + + .global _tx_thread_smp_current_thread_get + .type _tx_thread_smp_current_thread_get, @function +_tx_thread_smp_current_thread_get: + csrr t0, mstatus // Pickup current interrupt posture + csrci mstatus, 0x08 // Lockout interrupts + + csrr t1, mhartid // Pickup current hart ID + la t2, _tx_thread_current_ptr // Base of current-thread pointer array + slli t1, t1, 2 // Build per-hart byte offset + add t2, t2, t1 // Select this hart's slot + lw a0, 0(t2) // Pickup current thread pointer + + csrw mstatus, t0 // Restore interrupt posture + ret diff --git a/ports_smp/risc-v32_smp/gnu/src/tx_thread_smp_initialize_wait.S b/ports_smp/risc-v32_smp/gnu/src/tx_thread_smp_initialize_wait.S new file mode 100644 index 000000000..d45d7d717 --- /dev/null +++ b/ports_smp/risc-v32_smp/gnu/src/tx_thread_smp_initialize_wait.S @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2026 Eclipse ThreadX contributors + * Portions of this file were generated with AI assistance. + * SPDX-License-Identifier: MIT + */ + +#include "tx_port.h" + + .section .text + .align 2 + .global _tx_thread_smp_initialize_wait + .type _tx_thread_smp_initialize_wait, @function +_tx_thread_smp_initialize_wait: + csrci mstatus, 0x08 + csrr t0, mhartid + beqz t0, .Linitialize_done + + /* Wait until primary initialization has recorded this hart. */ + slli t1, t0, 2 + la t4, _tx_thread_system_state + add t5, t4, t1 + li t3, 0xF0F0F0F0 +.Lwait_for_initialize: + lw t6, 0(t5) + bne t6, t3, .Lwait_for_initialize + + slli t2, t0, 2 + la t3, _tx_thread_system_stack_ptr + add t3, t3, t2 + sw sp, 0(t3) + + la t3, _tx_thread_smp_release_cores_flag +.Lwait_for_release: + lw t6, 0(t3) + beqz t6, .Lwait_for_release + fence r, rw + + fence rw, w + sw zero, 0(t5) + +.Lwait_for_primary: + lw t6, 0(t4) + bnez t6, .Lwait_for_primary + fence r, rw + + call _tx_riscv_smp_hart_initialize + j _tx_thread_schedule + +.Linitialize_done: + ret diff --git a/ports_smp/risc-v32_smp/gnu/src/tx_thread_stack_build.S b/ports_smp/risc-v32_smp/gnu/src/tx_thread_stack_build.S index 3f7ae5d6e..524647781 100644 --- a/ports_smp/risc-v32_smp/gnu/src/tx_thread_stack_build.S +++ b/ports_smp/risc-v32_smp/gnu/src/tx_thread_stack_build.S @@ -1,12 +1,15 @@ /*************************************************************************** * Copyright (c) 2025 10xEngineers + * Copyright (c) 2026 Eclipse ThreadX contributors * * This program and the accompanying materials are made available under the * terms of the MIT License which is available at * https://opensource.org/licenses/MIT. * * SPDX-License-Identifier: MIT + **************************************************************************/ +/* Portions of this file were generated with AI assistance. */ /**************************************************************************/ @@ -29,7 +32,7 @@ /* */ /* FUNCTION RELEASE */ /* */ -/* _tx_thread_stack_build RISC-V32/GNU */ +/* _tx_thread_stack_build RISC-V32/GNU SMP */ /* 6.4.x */ /* AUTHOR */ /* */ @@ -96,10 +99,10 @@ _tx_thread_stack_build: x11 26 Initial a1 x10 27 Initial a0 x1 28 Initial ra - mstatus 29 Initial mstatus seed + mstatus 29 Initial mstatus seed mepc 30 Initial mepc -- 31 pad (0) - If floating point support (double ABI), byte offsets from + If floating point support (double ABI), byte offsets from the frame base: f0-f31 128 + i*8 Initial FP registers (all 0) fcsr 384 Initial fcsr (0) @@ -107,7 +110,7 @@ _tx_thread_stack_build: Stack Bottom: (higher memory address) */ - lw t0, 16(a0) // Pickup end of stack area + lw t0, TX_RISCV_TCB_STACK_END(a0) // Pickup end of stack area li t1, ~15 // Build 16-byte alignment mask and t0, t0, t1 // Make sure 16-byte alignment @@ -166,6 +169,8 @@ _tx_thread_stack_build_fp_zero: /* Setup stack pointer. */ /* thread_ptr -> tx_thread_stack_ptr = t0; */ - sw t0, 8(a0) // Save stack pointer in thread's + sw t0, TX_RISCV_TCB_STACK_PTR(a0) // Save stack pointer in thread's + li t1, 1 + sw t1, TX_THREAD_SMP_LOCK_READY_BIT_OFFSET(a0) ret // control block and return /* } */ diff --git a/ports_smp/risc-v32_smp/gnu/src/tx_thread_system_return.S b/ports_smp/risc-v32_smp/gnu/src/tx_thread_system_return.S index 2e49bc3fe..15ee8d53f 100644 --- a/ports_smp/risc-v32_smp/gnu/src/tx_thread_system_return.S +++ b/ports_smp/risc-v32_smp/gnu/src/tx_thread_system_return.S @@ -1,12 +1,15 @@ /*************************************************************************** * Copyright (c) 2025 10xEngineers + * Copyright (c) 2026 Eclipse ThreadX contributors * * This program and the accompanying materials are made available under the * terms of the MIT License which is available at * https://opensource.org/licenses/MIT. * * SPDX-License-Identifier: MIT + **************************************************************************/ +/* Portions of this file were generated with AI assistance. */ /**************************************************************************/ @@ -29,7 +32,7 @@ /* */ /* FUNCTION RELEASE */ /* */ -/* _tx_thread_system_return RISC-V32/GNU */ +/* _tx_thread_system_return RISC-V32/GNU SMP */ /* 6.4.x */ /* AUTHOR */ /* */ @@ -120,40 +123,40 @@ _tx_thread_system_return_skip_fp: call _tx_execution_thread_exit // Call the thread execution exit function #endif - la t0, _tx_thread_current_ptr // Pickup address of pointer - lw t1, 0(t0) // Pickup current thread pointer - la t2, _tx_thread_system_stack_ptr // Pickup stack pointer address - - /* Save current stack and switch to system stack. */ - /* _tx_thread_current_ptr -> tx_thread_stack_ptr = SP; - SP = _tx_thread_system_stack_ptr; */ - - sw sp, 8(t1) // Save stack pointer - lw sp, 0(t2) // Switch to system stack - - /* Determine if the time-slice is active. */ - /* if (_tx_timer_time_slice) - { */ - - la t4, _tx_timer_time_slice // Pickup time slice variable addr - lw t3, 0(t4) // Pickup time slice value - la t2, _tx_thread_schedule // Pickup address of scheduling loop - beqz t3, _tx_thread_dont_save_ts // If no time-slice, don't save it + csrr t5, mhartid + slli t6, t5, 2 + la t0, _tx_thread_current_ptr + add t0, t0, t6 + lw t1, 0(t0) + sw sp, TX_RISCV_TCB_STACK_PTR(t1) + + la t2, _tx_thread_system_stack_ptr + add t2, t2, t6 + lw sp, 0(t2) + + slli t6, t5, 2 + la t4, _tx_timer_time_slice + add t4, t4, t6 + lw t3, 0(t4) + beqz t3, _tx_thread_dont_save_ts + sw t3, TX_RISCV_TCB_TIME_SLICE(t1) + sw zero, 0(t4) - /* Save time-slice for the thread and clear the current time-slice. */ - /* _tx_thread_current_ptr -> tx_thread_time_slice = _tx_timer_time_slice; - _tx_timer_time_slice = 0; */ - - sw t3, 24(t1) // Save current time-slice for thread - sw zero, 0(t4) // Clear time-slice variable - - /* } */ _tx_thread_dont_save_ts: - - /* Clear the current thread pointer. */ - /* _tx_thread_current_ptr = TX_NULL; */ - - sw x0, 0(t0) // Clear current thread pointer - jr t2 // Return to thread scheduler - -/* } */ + /* Publish the saved context before another hart can claim the thread. */ + sw zero, 0(t0) + addi t3, t1, TX_THREAD_SMP_LOCK_READY_BIT_OFFSET + li t4, 1 + amoswap.w.rl zero, t4, (t3) + + /* The kernel enters system return with SMP protection held. */ + la t3, _tx_thread_preempt_disable + sw zero, 0(t3) + la t3, _tx_thread_smp_protection + sw zero, TX_RISCV_SMP_PROTECT_COUNT(t3) + li t4, -1 + sw t4, TX_RISCV_SMP_PROTECT_CORE(t3) + amoswap.w.rl zero, zero, (t3) + j _tx_thread_schedule + +/* } */ diff --git a/ports_smp/risc-v64_smp/gnu/CMakeLists.txt b/ports_smp/risc-v64_smp/gnu/CMakeLists.txt new file mode 100644 index 000000000..0f2fdca4d --- /dev/null +++ b/ports_smp/risc-v64_smp/gnu/CMakeLists.txt @@ -0,0 +1,25 @@ +# Copyright (c) 2026 Eclipse ThreadX contributors +# Portions of this file were generated with AI assistance. +# SPDX-License-Identifier: MIT + +set(TX_RISCV_SMP_COMMON ${CMAKE_CURRENT_LIST_DIR}/../../risc-v-common) + +target_sources(${PROJECT_NAME} PRIVATE + src/tx_thread_context_restore.S + src/tx_thread_context_save.S + src/tx_thread_interrupt_control.S + src/tx_thread_schedule.S + src/tx_thread_smp_current_thread_get.S + src/tx_thread_smp_initialize_wait.S + src/tx_thread_stack_build.S + src/tx_thread_system_return.S + ${TX_RISCV_SMP_COMMON}/tx_port_offset_check.c + ${TX_RISCV_SMP_COMMON}/tx_thread_smp_core_get.S + ${TX_RISCV_SMP_COMMON}/tx_thread_smp_current_state_get.S + ${TX_RISCV_SMP_COMMON}/tx_thread_smp_low_level_initialize.S + ${TX_RISCV_SMP_COMMON}/tx_thread_smp_protect.S + ${TX_RISCV_SMP_COMMON}/tx_thread_smp_time_get.c + ${TX_RISCV_SMP_COMMON}/tx_thread_smp_unprotect.S + ${TX_RISCV_SMP_COMMON}/tx_timer_interrupt.c +) +target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/inc) diff --git a/ports_smp/risc-v64_smp/gnu/inc/tx_port.h b/ports_smp/risc-v64_smp/gnu/inc/tx_port.h new file mode 100644 index 000000000..3270c3315 --- /dev/null +++ b/ports_smp/risc-v64_smp/gnu/inc/tx_port.h @@ -0,0 +1,122 @@ +/*************************************************************************** + * Copyright (c) 2024 Microsoft Corporation + * Copyright (c) 2026-present Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +// Portions of this file were generated with AI assistance. + + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Component */ +/** */ +/** Port Specific */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + + +/**************************************************************************/ +/* */ +/* PORT SPECIFIC C INFORMATION RELEASE */ +/* */ +/* tx_port.h RISC-V64/GNU */ +/* 6.5.1.202602a */ +/* */ +/* AUTHOR */ +/* */ +/* Scott Larson, Microsoft Corporation */ +/* */ +/* DESCRIPTION */ +/* */ +/* This file contains data type definitions that make the ThreadX */ +/* real-time kernel function identically on a variety of different */ +/* processor architectures. For example, the size or number of bits */ +/* in an "int" data type vary between microprocessor architectures and */ +/* even C compilers for the same microprocessor. ThreadX does not */ +/* directly use native C data types. Instead, ThreadX creates its */ +/* own special types that can be mapped to actual data types by this */ +/* file to guarantee consistency in the interface and functionality. */ +/* */ +/**************************************************************************/ + +// Portions of this file were generated with AI assistance. +#ifndef TX_PORT_H +#define TX_PORT_H + +#ifdef __riscv_vector +#error "The RISC-V SMP port supports integer and floating-point context." +#endif + +#ifdef __riscv_float_abi_quad +#error "The ThreadX RISC-V64 port does not support the LP64Q ABI." +#endif + +#if defined(__riscv_flen) && !defined(__riscv_float_abi_single) && !defined(__riscv_float_abi_double) +#error "The ThreadX RISC-V64 port does not preserve FP state for a soft-float ABI. Remove F and D ISA extensions or use LP64F or LP64D." +#endif + +#if defined(__riscv_float_abi_single) && (!defined(__riscv_flen) || (__riscv_flen < 32)) +#error "The ThreadX RISC-V64 LP64F port requires FLEN>=32." +#endif + +#if defined(__riscv_float_abi_double) && (!defined(__riscv_flen) || (__riscv_flen < 64)) +#error "The ThreadX RISC-V64 LP64D port requires FLEN>=64." +#endif + +#if defined(__riscv_flen) && (__riscv_flen != 32) && (__riscv_flen != 64) +#error "The ThreadX RISC-V64 port supports only FLEN=32 or FLEN=64." +#endif + +/* Every port .S file uses CSR instructions, so the ISA string must include + Zicsr. Catch its absence here rather than in a wall of assembler + "unrecognized opcode" errors. */ +#if !defined(__riscv_zicsr) +#error "The ThreadX RISC-V64 port requires the Zicsr extension. Add it to the ISA string, for example rv64imac_zicsr." +#endif + +/* Publish the interrupt frame contract to GNU BSP assembly files. */ +#if defined(__riscv_float_abi_single) || defined(__riscv_float_abi_double) +#define TX_RISCV_TRAP_FRAME_SIZE 528 +#else +#define TX_RISCV_TRAP_FRAME_SIZE 256 +#endif + +/* Bytes a trap entry reserves around a call into C. The RISC-V psABI + requires sp to stay 16-byte aligned at a call boundary, so this is 16 + rather than one register slot. */ +#define TX_RISCV_TRAP_CALL_FRAME_SIZE 16 + + +#if defined(__riscv_float_abi_single) || defined(__riscv_float_abi_double) +#define TX_RISCV_SOL_FRAME_SIZE 240 +#else +#define TX_RISCV_SOL_FRAME_SIZE 128 +#endif + + +#ifdef TX_RISCV_SMODE +#error "The RISC-V SMP port requires machine mode." +#endif + +#ifdef TX_INCLUDE_USER_DEFINE_FILE +#include "tx_user.h" +#endif + +/* Assembly atomically updates this byte displacement in TX_THREAD. Recheck + it when ULONG, preceding members, or thread and timer extensions change. + tx_port_offset_check.c verifies it against the target C layout. */ +#define TX_THREAD_SMP_LOCK_READY_BIT_OFFSET 260 +#define TX_RISCV_TCB_STACK_END 24 +#define TX_RISCV_TCB_TIME_SLICE 36 + +#include "../../../risc-v-common/tx_port_riscv_smp.h" + +#endif /* TX_PORT_H */ diff --git a/ports_smp/risc-v64_smp/gnu/src/tx_thread_context_restore.S b/ports_smp/risc-v64_smp/gnu/src/tx_thread_context_restore.S index f5a68d2ec..933d97bde 100644 --- a/ports_smp/risc-v64_smp/gnu/src/tx_thread_context_restore.S +++ b/ports_smp/risc-v64_smp/gnu/src/tx_thread_context_restore.S @@ -1,5 +1,6 @@ /*************************************************************************** * Copyright (c) 2024 Microsoft Corporation + * Copyright (c) 2026 Eclipse ThreadX contributors * Copyright (c) 2026-present Eclipse ThreadX contributors * * This program and the accompanying materials are made available under the @@ -7,7 +8,9 @@ * https://opensource.org/licenses/MIT. * * SPDX-License-Identifier: MIT + **************************************************************************/ +/* Portions of this file were generated with AI assistance. */ /**************************************************************************/ @@ -26,7 +29,7 @@ /* */ /* FUNCTION RELEASE */ /* */ -/* _tx_thread_context_restore RISC-V64/GNU */ +/* _tx_thread_context_restore RISC-V64/GNU SMP */ /* 6.2.1 */ /* AUTHOR */ /* */ @@ -65,11 +68,7 @@ _tx_thread_context_restore: /* Lockout interrupts. */ -#ifdef TX_RISCV_SMODE - csrci sstatus, 0x02 // Disable interrupts (SIE bit 1) -#else csrci mstatus, 0x08 // Disable interrupts (MIE bit 3) -#endif #ifdef TX_ENABLE_EXECUTION_CHANGE_NOTIFY call _tx_execution_isr_exit // Call the ISR execution exit function @@ -80,6 +79,9 @@ _tx_thread_context_restore: { */ la t0, _tx_thread_system_state // Pickup addr of nested interrupt count + csrr t1, mhartid + slli t1, t1, 2 + add t0, t0, t1 lw t1, 0(t0) // Pickup nested interrupt count addi t1, t1, -1 // Decrement the nested interrupt counter sw t1, 0(t0) // Store new nested count @@ -164,33 +166,6 @@ _tx_thread_context_restore: #endif _tx_thread_skip_fp_restore: -#if defined(__riscv_vector) - /* Recover vector registers v0-v31 */ -#if defined(__riscv_float_abi_single) || defined(__riscv_float_abi_double) - addi t1, sp, 64*8 -#else - addi t1, sp, 31*8 -#endif - addi t2, t1, 4*8 - vsetvli t3, zero, e8, m8, ta, ma - vle8.v v0, (t2) // Recover v0 ~ v7 - add t2, t2, t3 - vle8.v v8, (t2) // Recover v8 ~ v15 - add t2, t2, t3 - vle8.v v16, (t2) // Recover v16 ~ v23 - add t2, t2, t3 - vle8.v v24, (t2) // Recover v24 ~ v31 - add t2, t2, t3 - - /* Recover vector CSRs */ - ld t2, 0*8(t1) - ld t3, 1*8(t1) - ld t4, 2*8(t1) - vsetvl zero, t4, t3 - csrw vstart, t2 - ld t4, 3*8(t1) - csrw vcsr, t4 -#endif /* Recover standard registers. */ @@ -200,28 +175,6 @@ _tx_thread_skip_fp_restore: except s0 since we use it ourselves. */ ld t0, 30*8(sp) // Recover exception PC -#ifdef TX_RISCV_SMODE - csrw sepc, t0 // Setup sepc - - /* Compose sstatus via read/modify/write to avoid clobbering unrelated bits. - Set SPIE and restore SPP to Supervisor, preserve other fields. */ - - csrr t1, sstatus - li t4, ~0x122 // Clear mask for SPP/SPIE/SIE - and t1, t1, t4 - li t3, 0x120 // Set SPP=Supervisor(0x100) + SPIE(0x20) so sret re-enables SIE - or t1, t1, t3 - -#if defined(__riscv_float_abi_single) || defined(__riscv_float_abi_double) - li t0, 0x6000 // Set FS=Dirty (bits 14:13) for FP state - or t1, t1, t0 -#endif -#if defined(__riscv_vector) - li t0, 0x0600 - or t1, t1, t0 -#endif - csrw sstatus, t1 // Update sstatus safely -#else csrw mepc, t0 // Setup mepc /* Compose mstatus via read/modify/write to avoid clobbering unrelated bits. @@ -236,13 +189,8 @@ _tx_thread_skip_fp_restore: #if defined(__riscv_float_abi_single) || defined(__riscv_float_abi_double) li t0, 0x6000 or t1, t1, t0 -#endif -#if defined(__riscv_vector) - li t0, 0x0600 - or t1, t1, t0 #endif csrw mstatus, t1 // Update mstatus safely -#endif ld ra, 28*8(sp) // Recover return address ld t0, 19*8(sp) // Recover t0 @@ -264,41 +212,31 @@ _tx_thread_skip_fp_restore: addi sp, sp, TX_TRAP_FRAME_SIZE // Recover stack frame - with floating point enabled (65*8 data + 8 pad) -#if defined(__riscv_vector) - addi t0, sp, -TX_TRAP_FRAME_SIZE - csrr t1, vlenb // Get vector register byte length - slli t1, t1, 5 // Multiply by 32 (number of vector registers) - addi t1, t1, 4*8 // Add vector CSR space: vstart, vtype, vl, vcsr - add sp, sp, t1 // Recover vector stack frame - - ld t1, 18*8(t0) // Recover t1 - ld t0, 19*8(t0) // Recover t0 -#endif -#ifdef TX_RISCV_SMODE - sret // Return to point of interrupt -#else mret // Return to point of interrupt -#endif /* } */ _tx_thread_not_nested_restore: - /* Determine if a thread was interrupted and no preemption is required. */ - /* else if (((_tx_thread_current_ptr) && (_tx_thread_current_ptr == _tx_thread_execute_ptr) - || (_tx_thread_preempt_disable)) - { */ - - la t0, _tx_thread_current_ptr // Pickup current thread pointer address - ld t1, 0(t0) // Pickup current thread pointer - beqz t1, _tx_thread_idle_system_restore // If NULL, idle system restore - - la t0, _tx_thread_preempt_disable // Pickup preempt disable flag address - lw t2, 0(t0) // Pickup preempt disable flag (UINT) - bnez t2, _tx_thread_no_preempt_restore // If set (nonzero), restore interrupted thread - - la t0, _tx_thread_execute_ptr // Pickup thread execute pointer address - ld t2, 0(t0) // Pickup thread execute pointer - bne t1, t2, _tx_thread_preempt_restore // If higher-priority thread is ready, preempt + /* The preemption-disable count belongs to the hart holding protection. */ + csrr t4, mhartid + slli t5, t4, 3 + la t0, _tx_thread_current_ptr + add t0, t0, t5 + ld t1, 0(t0) + beqz t1, _tx_thread_idle_system_restore + + la t0, _tx_thread_execute_ptr + add t0, t0, t5 + ld t2, 0(t0) + beq t1, t2, _tx_thread_no_preempt_restore + + la t0, _tx_thread_smp_protection + lw t2, TX_RISCV_SMP_PROTECT_CORE(t0) + bne t2, t4, _tx_thread_preempt_restore + la t0, _tx_thread_preempt_disable + lw t2, 0(t0) + bnez t2, _tx_thread_no_preempt_restore + j _tx_thread_preempt_restore _tx_thread_no_preempt_restore: /* Restore interrupted thread or ISR. */ @@ -306,7 +244,7 @@ _tx_thread_no_preempt_restore: /* Pickup the saved stack pointer. */ /* sp = _tx_thread_current_ptr -> tx_thread_stack_ptr; */ - ld sp, 8(t1) // Switch back to thread's stack + ld sp, TX_RISCV_TCB_STACK_PTR(t1) // Switch back to thread's stack /* Recover the FP state only when the saved mstatus.FS was not Off. The save side skips the FP stores for a thread that has not touched the @@ -382,33 +320,6 @@ _tx_thread_no_preempt_restore: #endif _tx_thread_no_preempt_skip_fp_restore: -#if defined(__riscv_vector) - /* Recover vector registers v0-v31 */ -#if defined(__riscv_float_abi_single) || defined(__riscv_float_abi_double) - addi t1, sp, 64*8 -#else - addi t1, sp, 31*8 -#endif - addi t2, t1, 4*8 - vsetvli t3, zero, e8, m8, ta, ma - vle8.v v0, (t2) // Recover v0 ~ v7 - add t2, t2, t3 - vle8.v v8, (t2) // Recover v8 ~ v15 - add t2, t2, t3 - vle8.v v16, (t2) // Recover v16 ~ v23 - add t2, t2, t3 - vle8.v v24, (t2) // Recover v24 ~ v31 - add t2, t2, t3 - - /* Recover vector CSRs */ - ld t2, 0*8(t1) - ld t3, 1*8(t1) - ld t4, 2*8(t1) - vsetvl zero, t4, t3 - csrw vstart, t2 - ld t4, 3*8(t1) - csrw vcsr, t4 -#endif /* Recover the saved context and return to the point of interrupt. */ @@ -417,27 +328,6 @@ _tx_thread_no_preempt_skip_fp_restore: Skip global pointer because that does not change */ ld t0, 30*8(sp) // Recover exception PC -#ifdef TX_RISCV_SMODE - csrw sepc, t0 // Setup sepc - - /* Compose sstatus via read/modify/write to avoid clobbering unrelated bits. */ - - csrr t1, sstatus - li t4, ~0x122 // Clear mask for SPP/SPIE/SIE - and t1, t1, t4 - li t3, 0x120 // Set SPP=Supervisor(0x100) + SPIE(0x20) so sret re-enables SIE - or t1, t1, t3 - -#if defined(__riscv_float_abi_single) || defined(__riscv_float_abi_double) - li t0, 0x6000 // Set FS=Dirty (bits 14:13) for FP state - or t1, t1, t0 -#endif -#if defined(__riscv_vector) - li t0, 0x0600 // Set VS bits (bits 10:9 to 11, Dirty) for restored vector state - or t1, t1, t0 -#endif - csrw sstatus, t1 // Update sstatus safely -#else csrw mepc, t0 // Setup mepc @@ -453,13 +343,8 @@ _tx_thread_no_preempt_skip_fp_restore: #if defined(__riscv_float_abi_single) || defined(__riscv_float_abi_double) li t0, 0x6000 or t1, t1, t0 -#endif -#if defined(__riscv_vector) - li t0, 0x0600 - or t1, t1, t0 #endif csrw mstatus, t1 // Update mstatus safely -#endif ld ra, 28*8(sp) // Recover return address ld t0, 19*8(sp) // Recover t0 @@ -481,21 +366,7 @@ _tx_thread_no_preempt_skip_fp_restore: addi sp, sp, TX_TRAP_FRAME_SIZE // Recover stack frame -#if defined(__riscv_vector) - addi t0, sp, -TX_TRAP_FRAME_SIZE - csrr t1, vlenb // Get vector register byte length - slli t1, t1, 5 // Multiply by 32 (number of vector registers) - addi t1, t1, 4*8 // Add vector CSR space: vstart, vtype, vl, vcsr - add sp, sp, t1 // Recover vector stack frame - - ld t1, 18*8(t0) // Recover t1 - ld t0, 19*8(t0) // Recover t0 -#endif -#ifdef TX_RISCV_SMODE - sret // Return to point of interrupt -#else mret // Return to point of interrupt -#endif /* } else @@ -504,13 +375,19 @@ _tx_thread_preempt_restore: /* Instead of directly activating the thread again, ensure we save the entire stack frame by saving the remaining registers. */ - ld t0, 8(t1) // Pickup thread's stack pointer + ld t0, TX_RISCV_TCB_STACK_PTR(t1) // Pickup thread's stack pointer ori t3, zero, 1 // Build interrupt stack type sd t3, 0(t0) // Store stack type /* Store floating point preserved registers. LP64F saved the complete FLEN=64 state before the C interrupt handler ran. */ #if !defined(__riscv_float_abi_single) || (__riscv_flen != 64) +#if (__riscv_flen == 32) || (__riscv_flen == 64) + ld t2, 29*8(t0) + srli t2, t2, 13 + andi t2, t2, 0x3 + beqz t2, _tx_thread_preempt_skip_fp_save +#endif #if (__riscv_flen == 32) fsw f8, 39*8(t0) // Store fs0 fsw f9, 40*8(t0) // Store fs1 @@ -540,11 +417,7 @@ _tx_thread_preempt_restore: #endif #endif - /* No vector save here. _tx_thread_context_save stored v0-v31 and the - vector CSRs before the C handler ran. The psABI treats v0-v31, vl and - vtype as caller-saved, so the handler may have changed them; a second - store here would overwrite the good copy with the handler's state. */ - +_tx_thread_preempt_skip_fp_save: /* Store standard preserved registers. */ sd x9, 11*8(t0) // Store s1 @@ -565,27 +438,27 @@ _tx_thread_preempt_restore: { */ la t0, _tx_timer_time_slice // Pickup time slice variable address + slli t5, t4, 2 + add t0, t0, t5 lw t2, 0(t0) // Pickup time slice beqz t2, _tx_thread_dont_save_ts // If 0, skip time slice processing /* _tx_thread_current_ptr -> tx_thread_time_slice = _tx_timer_time_slice _tx_timer_time_slice = 0; */ - sw t2, 36(t1) // Save current time slice + sw t2, TX_RISCV_TCB_TIME_SLICE(t1) // Save current time slice sw x0, 0(t0) // Clear global time slice /* } */ _tx_thread_dont_save_ts: - /* Clear the current task pointer. */ - /* _tx_thread_current_ptr = TX_NULL; */ - - /* Return to the scheduler. */ - /* _tx_thread_schedule(); */ - - la t0, _tx_thread_current_ptr // Pickup current thread pointer address - sd x0, 0(t0) // Clear current thread pointer*/ - /* } */ + la t0, _tx_thread_current_ptr + slli t5, t4, 3 + add t0, t0, t5 + sd zero, 0(t0) + addi t0, t1, TX_THREAD_SMP_LOCK_READY_BIT_OFFSET + li t2, 1 + amoswap.w.rl zero, t2, (t0) _tx_thread_idle_system_restore: /* Just return back to the scheduler! */ diff --git a/ports_smp/risc-v64_smp/gnu/src/tx_thread_context_save.S b/ports_smp/risc-v64_smp/gnu/src/tx_thread_context_save.S index 1924bd6ee..43a1f604b 100644 --- a/ports_smp/risc-v64_smp/gnu/src/tx_thread_context_save.S +++ b/ports_smp/risc-v64_smp/gnu/src/tx_thread_context_save.S @@ -1,5 +1,6 @@ /*************************************************************************** * Copyright (c) 2024 Microsoft Corporation + * Copyright (c) 2026 Eclipse ThreadX contributors * Copyright (c) 2026-present Eclipse ThreadX contributors * * This program and the accompanying materials are made available under the @@ -7,7 +8,9 @@ * https://opensource.org/licenses/MIT. * * SPDX-License-Identifier: MIT + **************************************************************************/ +/* Portions of this file were generated with AI assistance. */ // Portions of this file were generated with AI assistance. @@ -28,7 +31,7 @@ /* */ /* FUNCTION RELEASE */ /* */ -/* _tx_thread_context_save RISC-V64/GNU */ +/* _tx_thread_context_save RISC-V64/GNU SMP */ /* 6.2.1 */ /* AUTHOR */ /* */ @@ -71,6 +74,9 @@ _tx_thread_context_save: sd t1, 18*8(sp) la t0, _tx_thread_system_state // Pickup address of system state + csrr t1, mhartid + slli t1, t1, 2 + add t0, t0, t1 lw t1, 0(t0) // Pickup system state /* Check for a nested interrupt condition. */ @@ -98,11 +104,7 @@ _tx_thread_context_save: sd t4, 15*8(sp) // Store t4 sd t5, 14*8(sp) // Store t5 sd t6, 13*8(sp) // Store t6 -#ifdef TX_RISCV_SMODE - csrr t0, sepc // Load exception program counter -#else csrr t0, mepc // Load exception program counter -#endif sd t0, 30*8(sp) // Save it on the stack /* Save mstatus and skip FP state if FS is Off. */ @@ -178,35 +180,6 @@ _tx_thread_context_save: #endif _tx_thread_skip_nested_fpu_save: -#if defined(__riscv_vector) - /* Store vector registers and CSRs */ -#if defined(__riscv_float_abi_single) || defined(__riscv_float_abi_double) - addi t1, sp, 64*8 -#else - addi t1, sp, 31*8 -#endif - /* Store vector CSRs */ - csrr t2, vstart // Store vstart - sd t2, 0*8(t1) - csrr t2, vtype // Store vtype - sd t2, 1*8(t1) - csrr t2, vl // Store vl - sd t2, 2*8(t1) - csrr t2, vcsr // Store vcsr - sd t2, 3*8(t1) - - /* Store vector registers v0-v31 */ - addi t2, t1, 4*8 - vsetvli t3, zero, e8, m8, ta, ma - vse8.v v0, 0(t2) // Store v0 ~ v7 - add t2, t2, t3 - vse8.v v8, 0(t2) // Store v8 ~ v15 - add t2, t2, t3 - vse8.v v16, 0(t2) // Store v16 ~ v23 - add t2, t2, t3 - vse8.v v24, 0(t2) // Store v24 ~ v31 - add t2, t2, t3 -#endif #ifdef TX_ENABLE_EXECUTION_CHANGE_NOTIFY addi sp, sp, -16 // Reserve space to save the return address @@ -230,6 +203,9 @@ _tx_thread_not_nested_save: /* Not nested: Find the user thread that was running and load our SP */ la t0, _tx_thread_current_ptr // Pickup current thread pointer address + csrr t1, mhartid + slli t1, t1, 3 + add t0, t0, t1 ld t0, 0(t0) // Pickup current thread pointer beqz t0, _tx_thread_idle_system_save // If NULL, idle system was interrupted @@ -250,11 +226,7 @@ _tx_thread_not_nested_save: sd t5, 14*8(sp) // Store t5 sd t6, 13*8(sp) // Store t6 -#ifdef TX_RISCV_SMODE - csrr t1, sepc // Load exception program counter -#else csrr t1, mepc // Load exception program counter -#endif sd t1, 30*8(sp) // Save it on the stack /* Save mstatus and skip FP state if FS is Off. */ @@ -330,35 +302,6 @@ _tx_thread_not_nested_save: #endif _tx_thread_skip_fpu_save: -#if defined(__riscv_vector) - /* Store vector registers and CSRs */ -#if defined(__riscv_float_abi_single) || defined(__riscv_float_abi_double) - addi t1, sp, 64*8 -#else - addi t1, sp, 31*8 -#endif - /* Store vector CSRs */ - csrr t2, vstart // Store vstart - sd t2, 0*8(t1) - csrr t2, vtype // Store vtype - sd t2, 1*8(t1) - csrr t2, vl // Store vl - sd t2, 2*8(t1) - csrr t2, vcsr // Store vcsr - sd t2, 3*8(t1) - - /* Store vector registers v0-v31 */ - addi t2, t1, 4*8 - vsetvli t3, zero, e8, m8, ta, ma - vse8.v v0, 0(t2) // Store v0 ~ v7 - add t2, t2, t3 - vse8.v v8, 0(t2) // Store v8 ~ v15 - add t2, t2, t3 - vse8.v v16, 0(t2) // Store v16 ~ v23 - add t2, t2, t3 - vse8.v v24, 0(t2) // Store v24 ~ v31 - add t2, t2, t3 -#endif /* Save the current stack pointer in the thread's control block. */ /* _tx_thread_current_ptr -> tx_thread_stack_ptr = sp; */ @@ -367,8 +310,11 @@ _tx_thread_skip_fpu_save: /* sp = _tx_thread_system_stack_ptr; */ la t1, _tx_thread_current_ptr // Pickup current thread pointer address + csrr t0, mhartid + slli t0, t0, 3 + add t1, t1, t0 ld t1, 0(t1) // Pickup current thread pointer - sd sp, 8(t1) // Save stack pointer + sd sp, TX_RISCV_TCB_STACK_PTR(t1) // Save stack pointer #ifdef TX_ENABLE_EXECUTION_CHANGE_NOTIFY /* _tx_execution_isr_enter is called with thread stack pointer */ @@ -380,6 +326,9 @@ _tx_thread_skip_fpu_save: #endif la t0, _tx_thread_system_stack_ptr // Pickup system stack pointer address + csrr t1, mhartid + slli t1, t1, 3 + add t0, t0, t1 ld sp, 0(t0) // Switch to system stack ret // Return to calling ISR @@ -404,14 +353,4 @@ _tx_thread_idle_system_save: } */ addi sp, sp, TX_TRAP_FRAME_SIZE // Recover stack frame - with floating point enabled (65*8 data + 8 pad) -#if defined(__riscv_vector) - addi t0, sp, -TX_TRAP_FRAME_SIZE - csrr t1, vlenb // Get vector register byte length - slli t1, t1, 5 // Multiply by 32 (number of vector registers) - addi t1, t1, 4*8 // Add vector CSR space: vstart, vtype, vl, vcsr - add sp, sp, t1 // Recover vector stack frame - - ld t1, 18*8(t0) // Recover t1 - ld t0, 19*8(t0) // Recover t0 -#endif ret // Return to calling ISR diff --git a/ports_smp/risc-v64_smp/gnu/src/tx_thread_interrupt_control.S b/ports_smp/risc-v64_smp/gnu/src/tx_thread_interrupt_control.S index d791e5089..3c8c15dc6 100644 --- a/ports_smp/risc-v64_smp/gnu/src/tx_thread_interrupt_control.S +++ b/ports_smp/risc-v64_smp/gnu/src/tx_thread_interrupt_control.S @@ -1,5 +1,6 @@ /*************************************************************************** * Copyright (c) 2024 Microsoft Corporation + * Copyright (c) 2026 Eclipse ThreadX contributors * Copyright (c) 2026-present Eclipse ThreadX contributors * * This program and the accompanying materials are made available under the @@ -7,7 +8,9 @@ * https://opensource.org/licenses/MIT. * * SPDX-License-Identifier: MIT + **************************************************************************/ +/* Portions of this file were generated with AI assistance. */ /**************************************************************************/ @@ -26,7 +29,7 @@ /* */ /* FUNCTION RELEASE */ /* */ -/* _tx_thread_interrupt_control RISC-V64/GNU */ +/* _tx_thread_interrupt_control RISC-V64/GNU SMP */ /* 6.5.1.202602a */ /* AUTHOR */ /* */ @@ -60,17 +63,6 @@ _tx_thread_interrupt_control: /* Pickup current interrupt lockout posture. */ -#ifdef TX_RISCV_SMODE - li t1, 0x02 // Build SIE bit mask (bit 1) - andi a0, a0, 0x02 // Mask incoming to only SIE bit - bnez a0, 1f // If set, enable interrupts - csrrc t0, sstatus, t1 // Atomically clear SIE, read old sstatus - j 2f -1: - csrrs t0, sstatus, t1 // Atomically set SIE, read old sstatus -2: - andi a0, t0, 0x02 // Return original SIE bit -#else li t1, 0x08 // Build MIE bit mask (bit 3) andi a0, a0, 0x08 // Mask incoming to only MIE bit bnez a0, 1f // If set, enable interrupts @@ -80,6 +72,5 @@ _tx_thread_interrupt_control: csrrs t0, mstatus, t1 // Atomically set MIE, read old mstatus 2: andi a0, t0, 0x08 // Return original MIE bit -#endif ret /* } */ diff --git a/ports_smp/risc-v64_smp/gnu/src/tx_thread_schedule.S b/ports_smp/risc-v64_smp/gnu/src/tx_thread_schedule.S index 262207265..e1e3dc28d 100644 --- a/ports_smp/risc-v64_smp/gnu/src/tx_thread_schedule.S +++ b/ports_smp/risc-v64_smp/gnu/src/tx_thread_schedule.S @@ -1,5 +1,6 @@ /*************************************************************************** * Copyright (c) 2024 Microsoft Corporation + * Copyright (c) 2026 Eclipse ThreadX contributors * Copyright (c) 2026-present Eclipse ThreadX contributors * * This program and the accompanying materials are made available under the @@ -7,7 +8,9 @@ * https://opensource.org/licenses/MIT. * * SPDX-License-Identifier: MIT + **************************************************************************/ +/* Portions of this file were generated with AI assistance. */ /**************************************************************************/ @@ -25,7 +28,7 @@ /* */ /* FUNCTION RELEASE */ /* */ -/* _tx_thread_schedule RISC-V64/GNU */ +/* _tx_thread_schedule RISC-V64/GNU SMP */ /* 6.2.1 */ /* AUTHOR */ /* */ @@ -63,73 +66,65 @@ .equ TX_SOL_FRAME_SIZE, TX_RISCV_SOL_FRAME_SIZE .global _tx_thread_schedule _tx_thread_schedule: + csrr t4, mhartid + slli t5, t4, 3 + la t0, _tx_thread_execute_ptr + add t0, t0, t5 + la a0, _tx_thread_current_ptr + add a0, a0, t5 - /* Enable interrupts. */ -#ifdef TX_RISCV_SMODE - csrsi sstatus, 0x02 // Enable interrupts (SIE bit 1) -#else - csrsi mstatus, 0x08 // Enable interrupts (MIE bit 3) -#endif - - /* Wait for a thread to execute. */ - /* do - { */ - - la t0, _tx_thread_execute_ptr // Pickup address of execute ptr _tx_thread_schedule_loop: - ld t1, 0(t0) // Pickup next thread to execute - -/* TX_USE_WFI_IDLE Configuration: - When defined, the scheduler enters WFI (Wait-For-Interrupt) mode when - no threads are ready, reducing power consumption. The core will wake - on any enabled interrupt. This is recommended for battery-powered or - low-power applications. Define TX_USE_WFI_IDLE in tx_user.h or via - compiler flags to enable this feature. */ -#ifdef TX_USE_WFI_IDLE - beqz t1, 1f - j 2f -1: wfi - j _tx_thread_schedule_loop -2: - beqz t1, _tx_thread_schedule_loop // Fallback: If still NULL, loop -#else - beqz t1, _tx_thread_schedule_loop // If NULL, wait for thread to execute + csrci mstatus, 0x08 + ld t1, 0(t0) + bnez t1, _tx_thread_schedule_thread +#ifdef TX_ENABLE_WFI + /* A pending IPI wakes WFI while MIE is clear. Service it afterwards. */ + wfi #endif - - /* } - while(_tx_thread_execute_ptr == NULL); */ - - /* Yes! We have a thread to execute. Lockout interrupts and - transfer control to it. */ -#ifdef TX_RISCV_SMODE - csrci sstatus, 0x02 // Lockout interrupts -#else - csrci mstatus, 0x08 // Lockout interrupts -#endif - - /* Setup the current thread pointer. */ - /* _tx_thread_current_ptr = _tx_thread_execute_ptr; */ - - la t0, _tx_thread_current_ptr // Pickup current thread pointer address - sd t1, 0(t0) // Set current thread pointer + csrsi mstatus, 0x08 + j _tx_thread_schedule_loop + +_tx_thread_schedule_thread: + /* Claim the thread before publishing that it runs on this hart. */ + addi t2, t1, TX_THREAD_SMP_LOCK_READY_BIT_OFFSET + amoswap.w.aq t3, zero, (t2) + beqz t3, _tx_thread_schedule_retry + +scheduler_found_thread: + sd t1, 0(a0) + fence rw, rw + ld t6, 0(t0) + beq t1, t6, _tx_thread_execute_unchanged + + /* The execute slot changed while the thread was being claimed. */ + sd zero, 0(a0) + li t3, 1 + amoswap.w.rl zero, t3, (t2) +_tx_thread_schedule_retry: + csrsi mstatus, 0x08 + j _tx_thread_schedule_loop + +_tx_thread_execute_unchanged: /* Increment the run count for this thread. */ /* _tx_thread_current_ptr -> tx_thread_run_count++; */ - lw t2, 4(t1) // Pickup run count - lw t3, 36(t1) // Pickup time slice value + lw t2, TX_RISCV_TCB_RUN_COUNT(t1) // Pickup run count + lw t3, TX_RISCV_TCB_TIME_SLICE(t1) // Pickup time slice value addi t2, t2, 1 // Increment run count - sw t2, 4(t1) // Store new run count + sw t2, TX_RISCV_TCB_RUN_COUNT(t1) // Store new run count /* Setup time-slice, if present. */ /* _tx_timer_time_slice = _tx_thread_current_ptr -> tx_thread_time_slice; */ la t2, _tx_timer_time_slice // Pickup time-slice variable address + slli t5, t4, 2 + add t2, t2, t5 /* Switch to the thread's stack. */ /* SP = _tx_thread_execute_ptr -> tx_thread_stack_ptr; */ - ld sp, 8(t1) // Switch to thread's stack + ld sp, TX_RISCV_TCB_STACK_PTR(t1) // Switch to thread's stack sw t3, 0(t2) // Store new time-slice #ifdef TX_ENABLE_EXECUTION_CHANGE_NOTIFY @@ -227,57 +222,10 @@ _tx_thread_schedule_loop: #endif _tx_thread_schedule_skip_fp_restore: -#if defined(__riscv_vector) - /* Recover vector registers v0-v31 */ -#if defined(__riscv_float_abi_single) || defined(__riscv_float_abi_double) - addi t1, sp, 64*8 -#else - addi t1, sp, 31*8 -#endif - addi t2, t1, 4*8 - vsetvli t3, zero, e8, m8, ta, ma - vle8.v v0, (t2) // Recover v0 ~ v7 - add t2, t2, t3 - vle8.v v8, (t2) // Recover v8 ~ v15 - add t2, t2, t3 - vle8.v v16, (t2) // Recover v16 ~ v23 - add t2, t2, t3 - vle8.v v24, (t2) // Recover v24 ~ v31 - add t2, t2, t3 - - /* Recover vector CSRs */ - ld t2, 0*8(t1) - ld t3, 1*8(t1) - ld t4, 2*8(t1) - vsetvl zero, t4, t3 - csrw vstart, t2 - ld t4, 3*8(t1) - csrw vcsr, t4 -#endif /* Recover standard registers. */ ld t0, 30*8(sp) // Recover mepc/sepc -#ifdef TX_RISCV_SMODE - csrw sepc, t0 // Store sepc - - /* Read/modify/write sstatus to preserve SUM, MXR, UXL, FS, etc. - Only touch SPP, SPIE, SIE — the bits SRET consumes. */ - csrr t0, sstatus - li t1, ~0x122 // Clear mask: SIE(1) | SPIE(5) | SPP(8) - and t0, t0, t1 - li t1, 0x120 // Set SPP=Supervisor(0x100) | SPIE(0x20) - or t0, t0, t1 -#if defined(__riscv_float_abi_single) || defined(__riscv_float_abi_double) - li t1, 0x6000 // Set FS=Dirty (bits 14:13) - or t0, t0, t1 -#endif -#if defined(__riscv_vector) - li t1, 0x0600 - or t0, t0, t1 -#endif - csrw sstatus, t0 // Update sstatus safely -#else csrw mepc, t0 // Store mepc /* Read/modify/write mstatus — same principle as S-mode path. */ @@ -289,13 +237,8 @@ _tx_thread_schedule_skip_fp_restore: #if defined(__riscv_float_abi_single) || defined(__riscv_float_abi_double) li t1, 0x6000 // Set FS=Dirty (bits 14:13) or t0, t0, t1 -#endif -#if defined(__riscv_vector) - li t1, 0x0600 - or t0, t0, t1 #endif csrw mstatus, t0 // Set mstatus -#endif ld ra, 28*8(sp) // Recover return address ld t0, 19*8(sp) // Recover t0 @@ -327,24 +270,16 @@ _tx_thread_schedule_skip_fp_restore: ld t6, 13*8(sp) // Recover t6 addi sp, sp, TX_TRAP_FRAME_SIZE // Recover stack frame - with floating point registers (65*8 data + 8 pad) -#if defined(__riscv_vector) - addi t0, sp, -TX_TRAP_FRAME_SIZE - csrr t1, vlenb // Get vector register byte length - slli t1, t1, 5 // Multiply by 32 (number of vector registers) - addi t1, t1, 4*8 // Add vector CSR space: vstart, vtype, vl, vcsr - add sp, sp, t1 // Recover vector stack frame - - ld t1, 18*8(t0) // Recover t1 - ld t0, 19*8(t0) // Recover t0 -#endif -#ifdef TX_RISCV_SMODE - sret // Return to point of interrupt -#else mret // Return to point of interrupt -#endif _tx_thread_synch_return: +#if (__riscv_flen == 32) || (__riscv_flen == 64) + ld t0, 14*8(sp) + srli t0, t0, 13 + andi t0, t0, 0x3 + beqz t0, _tx_thread_solicited_skip_fp_restore +#endif #if (__riscv_flen == 32) flw f8, 15*8(sp) // Recover fs0 flw f9, 16*8(sp) // Recover fs1 @@ -377,37 +312,11 @@ _tx_thread_synch_return: csrw fcsr, t0 // #endif -#if defined(__riscv_vector) - /* Recover vector registers v0-v31 */ -#if defined(__riscv_float_abi_single) || defined(__riscv_float_abi_double) - addi t1, sp, 28*8 -#else - addi t1, sp, 15*8 -#endif - addi t2, t1, 4*8 - vsetvli t3, zero, e8, m8, ta, ma - vle8.v v0, (t2) // Recover v0 ~ v7 - add t2, t2, t3 - vle8.v v8, (t2) // Recover v8 ~ v15 - add t2, t2, t3 - vle8.v v16, (t2) // Recover v16 ~ v23 - add t2, t2, t3 - vle8.v v24, (t2) // Recover v24 ~ v31 - add t2, t2, t3 - - /* Recover vector CSRs */ - ld t2, 0*8(t1) - ld t3, 1*8(t1) - ld t4, 2*8(t1) - vsetvl zero, t4, t3 - csrw vstart, t2 - ld t4, 3*8(t1) - csrw vcsr, t4 -#endif /* Recover standard preserved registers. */ /* Recover standard registers. */ +_tx_thread_solicited_skip_fp_restore: ld ra, 13*8(sp) // Recover RA ld s0, 12*8(sp) // Recover s0 ld s1, 11*8(sp) // Recover s1 @@ -422,18 +331,8 @@ _tx_thread_synch_return: ld s10, 2*8(sp) // Recover s10 ld s11, 1*8(sp) // Recover s11 ld t0, 14*8(sp) // Recover status register -#ifdef TX_RISCV_SMODE - csrw sstatus, t0 // Store sstatus, enables interrupt -#else csrw mstatus, t0 // Store mstatus, enables interrupt -#endif addi sp, sp, TX_SOL_FRAME_SIZE // Recover stack frame (29*8 data + 8 pad) -#if defined(__riscv_vector) - csrr t1, vlenb // Get vector register byte length - slli t1, t1, 5 // Multiply by 32 (number of vector registers) - addi t1, t1, 4*8 // Add vector CSR space: vstart, vtype, vl, vcsr - add sp, sp, t1 // Recover vector stack frame -#endif ret // Return to thread /* } */ diff --git a/ports_smp/risc-v64_smp/gnu/src/tx_thread_smp_current_thread_get.S b/ports_smp/risc-v64_smp/gnu/src/tx_thread_smp_current_thread_get.S new file mode 100644 index 000000000..e6e85fba1 --- /dev/null +++ b/ports_smp/risc-v64_smp/gnu/src/tx_thread_smp_current_thread_get.S @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2026 Eclipse ThreadX contributors + * Portions of this file were generated with AI assistance. + * SPDX-License-Identifier: MIT + */ + +#include "tx_port.h" + + .section .text + .align 2 + + .global _tx_thread_smp_current_thread_get + .type _tx_thread_smp_current_thread_get, @function +_tx_thread_smp_current_thread_get: + csrr t0, mstatus // Pickup current interrupt posture + csrci mstatus, 0x08 // Lockout interrupts + + csrr t1, mhartid // Pickup current hart ID + la t2, _tx_thread_current_ptr // Base of current-thread pointer array + slli t1, t1, 3 // Build per-hart byte offset + add t2, t2, t1 // Select this hart's slot + ld a0, 0(t2) // Pickup current thread pointer + + csrw mstatus, t0 // Restore interrupt posture + ret diff --git a/ports_smp/risc-v64_smp/gnu/src/tx_thread_smp_initialize_wait.S b/ports_smp/risc-v64_smp/gnu/src/tx_thread_smp_initialize_wait.S new file mode 100644 index 000000000..364715bc8 --- /dev/null +++ b/ports_smp/risc-v64_smp/gnu/src/tx_thread_smp_initialize_wait.S @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2026 Eclipse ThreadX contributors + * Portions of this file were generated with AI assistance. + * SPDX-License-Identifier: MIT + */ + +#include "tx_port.h" + + .section .text + .align 2 + .global _tx_thread_smp_initialize_wait + .type _tx_thread_smp_initialize_wait, @function +_tx_thread_smp_initialize_wait: + csrci mstatus, 0x08 + csrr t0, mhartid + beqz t0, .Linitialize_done + + /* Wait until primary initialization has recorded this hart. */ + slli t1, t0, 2 + la t4, _tx_thread_system_state + add t5, t4, t1 + li t3, 0xF0F0F0F0 +.Lwait_for_initialize: + lwu t6, 0(t5) + bne t6, t3, .Lwait_for_initialize + + slli t2, t0, 3 + la t3, _tx_thread_system_stack_ptr + add t3, t3, t2 + sd sp, 0(t3) + + la t3, _tx_thread_smp_release_cores_flag +.Lwait_for_release: + lw t6, 0(t3) + beqz t6, .Lwait_for_release + fence r, rw + + fence rw, w + sw zero, 0(t5) + +.Lwait_for_primary: + lw t6, 0(t4) + bnez t6, .Lwait_for_primary + fence r, rw + + call _tx_riscv_smp_hart_initialize + j _tx_thread_schedule + +.Linitialize_done: + ret diff --git a/ports_smp/risc-v64_smp/gnu/src/tx_thread_stack_build.S b/ports_smp/risc-v64_smp/gnu/src/tx_thread_stack_build.S index 47e5c995f..6529bbbdb 100644 --- a/ports_smp/risc-v64_smp/gnu/src/tx_thread_stack_build.S +++ b/ports_smp/risc-v64_smp/gnu/src/tx_thread_stack_build.S @@ -1,5 +1,6 @@ /*************************************************************************** * Copyright (c) 2024 Microsoft Corporation + * Copyright (c) 2026 Eclipse ThreadX contributors * Copyright (c) 2026-present Eclipse ThreadX contributors * * This program and the accompanying materials are made available under the @@ -7,7 +8,9 @@ * https://opensource.org/licenses/MIT. * * SPDX-License-Identifier: MIT + **************************************************************************/ +/* Portions of this file were generated with AI assistance. */ /**************************************************************************/ @@ -27,7 +30,7 @@ /* */ /* FUNCTION RELEASE */ /* */ -/* _tx_thread_stack_build RISC-V64/GNU */ +/* _tx_thread_stack_build RISC-V64/GNU SMP */ /* 6.2.1 */ /* AUTHOR */ /* */ @@ -130,23 +133,14 @@ If floating point support: f29 60 Inital ft9 f30 61 Inital ft10 f31 62 Inital ft11 - fscr 63 Inital fscr -If vector extension support: - vstart 64 Initial vstart - vtype 65 Initial vtype - vl 66 Initial vl - vcsr 67 Initial vcsr - v0 68 Initial v0 - v1 69 Initial v1 - ... - v31 99 Initial v31 + fcsr 63 Initial fcsr With floating point enabled, 8 pad bytes sit at the top of the frame (bytes 520-527) so the 528-byte total keeps sp 16-byte aligned. Stack Bottom: (higher memory address) */ - ld t0, 24(a0) // Pickup end of stack area + ld t0, TX_RISCV_TCB_STACK_END(a0) // Pickup end of stack area li t1, ~15 // Build 16-byte alignment mask and t0, t0, t1 // Make sure 16-byte alignment @@ -154,15 +148,7 @@ If vector extension support: addi t0, t0, -TX_TRAP_FRAME_SIZE -#if defined(__riscv_vector) - /* Vector extension support: calculate space based on vlenb */ - csrr t4, vlenb // Get vector register byte length - slli t4, t4, 5 // Multiply by 32 (number of vector registers) - addi t4, t4, 4*8 // Add vector CSR space: vstart, vtype, vl, vcsr - sub t0, t0, t4 // Allocate vector space for the stack frame -#else li t4, 0 -#endif li t1, 1 // Build stack type sd t1, 0*8(t0) // Place stack type on the top @@ -233,23 +219,6 @@ If vector extension support: sd zero, 63*8(t0) // Initial fcsr (0: no sticky flags, round-to-nearest) #endif -#if defined(__riscv_vector) - /* Clear vector register space */ -#if defined(__riscv_float_abi_single) || defined(__riscv_float_abi_double) - addi t2, t0, 64*8 // t2 = start of vector registers -#else - addi t2, t0, 31*8 // t2 = start of vector registers -#endif - add t3, t2, t4 // t3 = end of vector registers - -vector_clear_loop: - beq t2, t3, vector_clear_done // Done if reached end - sd zero, 0(t2) // Clear 8 bytes - addi t2, t2, 8 // Move to next 8 bytes - j vector_clear_loop - -vector_clear_done: -#endif add t2, t0, t4 #if defined(__riscv_float_abi_single) || defined(__riscv_float_abi_double) @@ -261,6 +230,8 @@ vector_clear_done: /* Setup stack pointer. */ /* thread_ptr -> tx_thread_stack_ptr = t0; */ - sd t0, 8(a0) // Save stack pointer in thread's + sd t0, TX_RISCV_TCB_STACK_PTR(a0) // Save stack pointer in thread's + li t1, 1 + sw t1, TX_THREAD_SMP_LOCK_READY_BIT_OFFSET(a0) ret // control block and return /* } */ diff --git a/ports_smp/risc-v64_smp/gnu/src/tx_thread_system_return.S b/ports_smp/risc-v64_smp/gnu/src/tx_thread_system_return.S index 2f4daeede..dad1e9b0f 100644 --- a/ports_smp/risc-v64_smp/gnu/src/tx_thread_system_return.S +++ b/ports_smp/risc-v64_smp/gnu/src/tx_thread_system_return.S @@ -1,5 +1,6 @@ /*************************************************************************** * Copyright (c) 2024 Microsoft Corporation + * Copyright (c) 2026 Eclipse ThreadX contributors * Copyright (c) 2026-present Eclipse ThreadX contributors * * This program and the accompanying materials are made available under the @@ -7,7 +8,9 @@ * https://opensource.org/licenses/MIT. * * SPDX-License-Identifier: MIT + **************************************************************************/ +/* Portions of this file were generated with AI assistance. */ /**************************************************************************/ @@ -25,7 +28,7 @@ /* */ /* FUNCTION RELEASE */ /* */ -/* _tx_thread_system_return RISC-V64/GNU */ +/* _tx_thread_system_return RISC-V64/GNU SMP */ /* 6.2.1 */ /* AUTHOR */ /* */ @@ -66,11 +69,13 @@ _tx_thread_system_return: /* sp -= sizeof(stack_frame); */ addi sp, sp, -TX_SOL_FRAME_SIZE // Allocate space on the stack -#if defined(__riscv_vector) - csrr t1, vlenb // Get vector register byte length - slli t1, t1, 5 // Multiply by 32 (number of vector registers) - addi t1, t1, 4*8 // Add vector CSR space: vstart, vtype, vl, vcsr - sub sp, sp, t1 // Allocate vector space on the stack + + csrr t0, mstatus + sd t0, 14*8(sp) +#if (__riscv_flen == 32) || (__riscv_flen == 64) + srli t1, t0, 13 + andi t1, t1, 0x3 + beqz t1, _tx_thread_system_return_skip_fp #endif /* Store floating point preserved registers. */ @@ -106,36 +111,8 @@ _tx_thread_system_return: sd t0, 27*8(sp) // Store fcsr #endif -#if defined(__riscv_vector) - /* Store vector registers and CSRs */ -#if defined(__riscv_float_abi_single) || defined(__riscv_float_abi_double) - addi t1, sp, 28*8 -#else - addi t1, sp, 15*8 -#endif - /* Store vector CSRs */ - csrr t2, vstart // Store vstart - sd t2, 0*8(t1) - csrr t2, vtype // Store vtype - sd t2, 1*8(t1) - csrr t2, vl // Store vl - sd t2, 2*8(t1) - csrr t2, vcsr // Store vcsr - sd t2, 3*8(t1) - - /* Store vector registers v0-v31 */ - addi t2, t1, 4*8 - vsetvli t3, zero, e8, m8, ta, ma - vse8.v v0, 0(t2) // Store v0 ~ v7 - add t2, t2, t3 - vse8.v v8, 0(t2) // Store v8 ~ v15 - add t2, t2, t3 - vse8.v v16, 0(t2) // Store v16 ~ v23 - add t2, t2, t3 - vse8.v v24, 0(t2) // Store v24 ~ v31 - add t2, t2, t3 -#endif +_tx_thread_system_return_skip_fp: sd zero, 0(sp) // Solicited stack type sd ra, 13*8(sp) // Save return address sd s0, 12*8(sp) // Save s0 @@ -150,63 +127,49 @@ _tx_thread_system_return: sd s9, 3*8(sp) // Save s9 sd s10, 2*8(sp) // Save s10 sd s11, 1*8(sp) // Save s11 -#ifdef TX_RISCV_SMODE - csrr t0, sstatus // Pickup sstatus - sd t0, 14*8(sp) // Save sstatus - - - /* Lockout interrupts. will be enabled in _tx_thread_schedule */ - - csrci sstatus, 0x02 // Disable interrupts (SIE bit 1) -#else - csrr t0, mstatus // Pickup mstatus - sd t0, 14*8(sp) // Save mstatus - - /* Lockout interrupts. will be enabled in _tx_thread_schedule */ csrci mstatus, 0x08 // Disable interrupts (MIE bit 3) -#endif #ifdef TX_ENABLE_EXECUTION_CHANGE_NOTIFY call _tx_execution_thread_exit // Call the thread execution exit function #endif - la t0, _tx_thread_current_ptr // Pickup address of pointer - ld t1, 0(t0) // Pickup current thread pointer - la t2, _tx_thread_system_stack_ptr // Pickup stack pointer address - - /* Save current stack and switch to system stack. */ - /* _tx_thread_current_ptr -> tx_thread_stack_ptr = SP; - SP = _tx_thread_system_stack_ptr; */ - - sd sp, 8(t1) // Save stack pointer - ld sp, 0(t2) // Switch to system stack - - /* Determine if the time-slice is active. */ - /* if (_tx_timer_time_slice) - { */ - - la t4, _tx_timer_time_slice // Pickup time slice variable addr - lw t3, 0(t4) // Pickup time slice value - la t2, _tx_thread_schedule // Pickup address of scheduling loop - beqz t3, _tx_thread_dont_save_ts // If no time-slice, don't save it + csrr t5, mhartid + slli t6, t5, 3 + la t0, _tx_thread_current_ptr + add t0, t0, t6 + ld t1, 0(t0) + sd sp, TX_RISCV_TCB_STACK_PTR(t1) + + la t2, _tx_thread_system_stack_ptr + add t2, t2, t6 + ld sp, 0(t2) + + slli t6, t5, 2 + la t4, _tx_timer_time_slice + add t4, t4, t6 + lw t3, 0(t4) + beqz t3, _tx_thread_dont_save_ts + sw t3, TX_RISCV_TCB_TIME_SLICE(t1) + sw zero, 0(t4) - /* Save time-slice for the thread and clear the current time-slice. */ - /* _tx_thread_current_ptr -> tx_thread_time_slice = _tx_timer_time_slice; - _tx_timer_time_slice = 0; */ - - sw t3, 36(t1) // Save current time-slice for thread - sw zero, 0(t4) // Clear time-slice variable - - /* } */ _tx_thread_dont_save_ts: - - /* Clear the current thread pointer. */ - /* _tx_thread_current_ptr = TX_NULL; */ - - sd x0, 0(t0) // Clear current thread pointer - jr t2 // Return to thread scheduler - -/* } */ + /* Publish the saved context before another hart can claim the thread. */ + sd zero, 0(t0) + addi t3, t1, TX_THREAD_SMP_LOCK_READY_BIT_OFFSET + li t4, 1 + amoswap.w.rl zero, t4, (t3) + + /* The kernel enters system return with SMP protection held. */ + la t3, _tx_thread_preempt_disable + sw zero, 0(t3) + la t3, _tx_thread_smp_protection + sw zero, TX_RISCV_SMP_PROTECT_COUNT(t3) + li t4, -1 + sw t4, TX_RISCV_SMP_PROTECT_CORE(t3) + amoswap.w.rl zero, zero, (t3) + j _tx_thread_schedule + +/* } */ From fa06c6f137293c98f7a48a927256f5a052be93b8 Mon Sep 17 00:00:00 2001 From: Eyck-Alexander Jentzsch Date: Fri, 27 Nov 2026 11:21:57 +0100 Subject: [PATCH 4/6] Matched the upstream RV32 scalar aliases Keep the GNU single-core LONG and ULONG type aliases for RV32 SMP. The RV64 port keeps int-width aliases and native-width pointers. Assisted-by: Codex (GPT-6) --- ports_smp/risc-v-common/tx_port_riscv_smp.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ports_smp/risc-v-common/tx_port_riscv_smp.h b/ports_smp/risc-v-common/tx_port_riscv_smp.h index b1ab751dd..f3cfa0280 100644 --- a/ports_smp/risc-v-common/tx_port_riscv_smp.h +++ b/ports_smp/risc-v-common/tx_port_riscv_smp.h @@ -79,8 +79,13 @@ typedef char CHAR; typedef unsigned char UCHAR; typedef int INT; typedef unsigned int UINT; +#if __riscv_xlen == 64 typedef int LONG; typedef unsigned int ULONG; +#else +typedef long LONG; +typedef unsigned long ULONG; +#endif typedef unsigned long long ULONG64; typedef short SHORT; typedef unsigned short USHORT; From a096f4db9c8b9b928bb30c8522d079de302e7466 Mon Sep 17 00:00:00 2001 From: Eyck-Alexander Jentzsch Date: Thu, 3 Dec 2026 15:57:13 +0100 Subject: [PATCH 5/6] cleans up a bit, adds better readme --- ports_smp/risc-v-common/readme_threadx.txt | 62 ++++++++++++++------- ports_smp/risc-v-common/tx_port_riscv_smp.h | 7 +++ 2 files changed, 48 insertions(+), 21 deletions(-) diff --git a/ports_smp/risc-v-common/readme_threadx.txt b/ports_smp/risc-v-common/readme_threadx.txt index 3b4d4a1f5..43d949a85 100644 --- a/ports_smp/risc-v-common/readme_threadx.txt +++ b/ports_smp/risc-v-common/readme_threadx.txt @@ -6,30 +6,50 @@ Zicsr extensions. RV32 supports ILP32 without hardware FP and ILP32D. RV64 supports LP64 without hardware FP, LP64F, and LP64D. The ports use a 32-bit ULONG on both architectures and preserve native-width pointers. -The context sources derive from the corresponding single-core GNU ports at -227586c72edba83c9c876528ef0b2ecc9afdfa37. SMP adds per-hart kernel state, -context publication and claiming, and software interrupt wakeups. +Place kernel data and thread and system stacks in shared, coherent memory +that supports word atomic operations. An SMP debug build +(TX_THREAD_SMP_DEBUG_ENABLE) also needs a working rdtime instruction. + +The context sources derive from the corresponding single-core GNU ports. +SMP adds per-hart kernel state, context publication and claiming, and +software interrupt wakeups. Board integration +----------------- + +The port uses mhartid directly as the ThreadX core index. It requires exactly +TX_THREAD_SMP_MAX_CORES participating harts, numbered consecutively from 0, +with hart 0 as the primary. Keep all other harts out of ThreadX. A missing +participating hart can stall initialization. Set the core count consistently +in the kernel and BSP builds. + +Provide a separate, 16-byte-aligned system stack for each participating hart. +Hart 0 calls tx_kernel_enter(). Secondary harts call +_tx_thread_smp_initialize_wait() after the BSP has made C runtime initialization +visible to them. + +Provide _tx_initialize_low_level() to initialize hart 0 and set +_tx_initialize_unused_memory and _tx_thread_system_stack_ptr[0]. Provide +_tx_riscv_smp_hart_initialize() to initialize local trap and interrupt handling +on secondary harts. The port calls it before scheduling begins with global +machine interrupts masked. Leave them masked on return. + +Reuse the corresponding single-core GNU machine-mode trap entry. Take the +interrupt frame size from TX_RISCV_TRAP_FRAME_SIZE in the selected port's +tx_port.h. + +Timer and interprocessor interrupts +----------------------------------- + +On hart 0, acknowledge or rearm the timer and call _tx_timer_interrupt() at +TX_TIMER_TICKS_PER_SECOND. This advances the global tick and updates time +slices for all cores. -Start TX_THREAD_SMP_MAX_CORES contiguous machine-mode harts beginning at -hart 0. Provide a 16-byte aligned system stack for every hart. Hart 0 enters -ThreadX normally. Secondary harts call _tx_thread_smp_initialize_wait after -initializing their stacks. The port calls _tx_riscv_smp_hart_initialize on -each secondary hart after synchronization, with interrupts disabled. Supply -this board routine to install the local trap vector and configure the local -interrupt controller. - -Supply _tx_initialize_low_level for hart 0. Set _tx_initialize_unused_memory -and its _tx_thread_system_stack_ptr slot, initialize the board, and install -the trap vector. Make one hart-0 hardware tick call _tx_timer_interrupt per -configured ThreadX tick. Supply _tx_thread_smp_core_preempt(UINT core) to -trigger an IPI and clear the IPI in the target hart's handler. - -The trap entry reserves TX_RISCV_TRAP_FRAME_SIZE bytes and saves the -interrupted ra in slot 28 at the native register width. It calls -_tx_thread_context_save, runs the board handler, then transfers control to -_tx_thread_context_restore. Keep the stack 16-byte aligned at each call. +Provide _tx_thread_smp_core_preempt(UINT core) to send a reschedule interrupt +to the target hart. Ensure scheduler updates are visible before delivering the +interrupt. The target must acknowledge the interrupt before checking scheduler +state during context restore. With TX_ENABLE_WFI, the interrupt must wake an +idle hart. The ready-token offset in each tx_port.h is a byte displacement for an assembly atomic operation. After changing the control block, preceding diff --git a/ports_smp/risc-v-common/tx_port_riscv_smp.h b/ports_smp/risc-v-common/tx_port_riscv_smp.h index f3cfa0280..5c6cc3948 100644 --- a/ports_smp/risc-v-common/tx_port_riscv_smp.h +++ b/ports_smp/risc-v-common/tx_port_riscv_smp.h @@ -164,6 +164,13 @@ typedef unsigned long long ALIGN_TYPE; #define TX_INLINE_INITIALIZATION +/* Publish kernel and application initialization before both startup stores. + The final store is control-dependent on the secondary acknowledgements. + Board overrides of this hook must preserve the release ordering. */ +#ifndef TX_PORT_SPECIFIC_PRE_SCHEDULER_INITIALIZATION +#define TX_PORT_SPECIFIC_PRE_SCHEDULER_INITIALIZATION __asm__ volatile ("fence rw, w" ::: "memory"); +#endif + /* Determine whether or not stack checking is enabled. By default, ThreadX stack checking is disabled. When the following is defined, ThreadX thread stack checking is enabled. If stack checking is enabled (TX_ENABLE_STACK_CHECKING is defined), the TX_DISABLE_STACK_FILLING From 3a8817513687ba5b256c537a74d7af58ff16e3c1 Mon Sep 17 00:00:00 2001 From: Eyck-Alexander Jentzsch Date: Fri, 4 Dec 2026 14:31:15 +0100 Subject: [PATCH 6/6] adds contribution notice --- ports_smp/risc-v-common/readme_threadx.txt | 3 +++ ports_smp/risc-v-common/tx_port_offset_check.c | 2 ++ ports_smp/risc-v-common/tx_port_riscv_smp.h | 2 ++ ports_smp/risc-v-common/tx_thread_smp_core_get.S | 2 ++ ports_smp/risc-v-common/tx_thread_smp_current_state_get.S | 2 ++ ports_smp/risc-v-common/tx_thread_smp_low_level_initialize.S | 2 ++ ports_smp/risc-v-common/tx_thread_smp_protect.S | 2 ++ ports_smp/risc-v-common/tx_thread_smp_time_get.c | 2 ++ ports_smp/risc-v-common/tx_thread_smp_unprotect.S | 2 ++ ports_smp/risc-v-common/tx_timer_interrupt.c | 1 + ports_smp/risc-v32_smp/gnu/CMakeLists.txt | 2 ++ ports_smp/risc-v32_smp/gnu/inc/tx_port.h | 1 + ports_smp/risc-v32_smp/gnu/src/tx_thread_context_restore.S | 1 + ports_smp/risc-v32_smp/gnu/src/tx_thread_context_save.S | 1 + ports_smp/risc-v32_smp/gnu/src/tx_thread_interrupt_control.S | 1 + ports_smp/risc-v32_smp/gnu/src/tx_thread_schedule.S | 1 + .../risc-v32_smp/gnu/src/tx_thread_smp_current_thread_get.S | 2 ++ ports_smp/risc-v32_smp/gnu/src/tx_thread_smp_initialize_wait.S | 2 ++ ports_smp/risc-v32_smp/gnu/src/tx_thread_stack_build.S | 1 + ports_smp/risc-v32_smp/gnu/src/tx_thread_system_return.S | 1 + ports_smp/risc-v64_smp/gnu/CMakeLists.txt | 2 ++ ports_smp/risc-v64_smp/gnu/inc/tx_port.h | 1 + ports_smp/risc-v64_smp/gnu/src/tx_thread_context_restore.S | 1 + ports_smp/risc-v64_smp/gnu/src/tx_thread_context_save.S | 1 + ports_smp/risc-v64_smp/gnu/src/tx_thread_interrupt_control.S | 1 + ports_smp/risc-v64_smp/gnu/src/tx_thread_schedule.S | 1 + .../risc-v64_smp/gnu/src/tx_thread_smp_current_thread_get.S | 2 ++ ports_smp/risc-v64_smp/gnu/src/tx_thread_smp_initialize_wait.S | 2 ++ ports_smp/risc-v64_smp/gnu/src/tx_thread_stack_build.S | 1 + ports_smp/risc-v64_smp/gnu/src/tx_thread_system_return.S | 1 + 30 files changed, 46 insertions(+) diff --git a/ports_smp/risc-v-common/readme_threadx.txt b/ports_smp/risc-v-common/readme_threadx.txt index 43d949a85..367d48313 100644 --- a/ports_smp/risc-v-common/readme_threadx.txt +++ b/ports_smp/risc-v-common/readme_threadx.txt @@ -1,5 +1,8 @@ Eclipse ThreadX SMP on RISC-V GNU +RISC-V SMP contributions by +Eyck-Alexander Jentzsch, MINRES Technologies GmbH + Use THREADX_SMP=ON with THREADX_ARCH=risc-v32 or risc-v64 and THREADX_TOOLCHAIN=gnu. The ports run in machine mode and require the A and Zicsr extensions. RV32 supports ILP32 without hardware FP and ILP32D. RV64 diff --git a/ports_smp/risc-v-common/tx_port_offset_check.c b/ports_smp/risc-v-common/tx_port_offset_check.c index 786c7c44a..17c7b96c2 100644 --- a/ports_smp/risc-v-common/tx_port_offset_check.c +++ b/ports_smp/risc-v-common/tx_port_offset_check.c @@ -1,5 +1,7 @@ /* * Copyright (c) 2026 Eclipse ThreadX contributors + * RISC-V SMP contributions by + * Eyck-Alexander Jentzsch, MINRES Technologies GmbH * Portions of this file were generated with AI assistance. * SPDX-License-Identifier: MIT */ diff --git a/ports_smp/risc-v-common/tx_port_riscv_smp.h b/ports_smp/risc-v-common/tx_port_riscv_smp.h index 5c6cc3948..6f68d223a 100644 --- a/ports_smp/risc-v-common/tx_port_riscv_smp.h +++ b/ports_smp/risc-v-common/tx_port_riscv_smp.h @@ -10,6 +10,8 @@ /* * Copyright (c) 2026 Eclipse ThreadX contributors + * RISC-V SMP contributions by + * Eyck-Alexander Jentzsch, MINRES Technologies GmbH * Portions of this file were generated with AI assistance. * SPDX-License-Identifier: MIT */ diff --git a/ports_smp/risc-v-common/tx_thread_smp_core_get.S b/ports_smp/risc-v-common/tx_thread_smp_core_get.S index 8c18625ec..ebc77c018 100644 --- a/ports_smp/risc-v-common/tx_thread_smp_core_get.S +++ b/ports_smp/risc-v-common/tx_thread_smp_core_get.S @@ -1,5 +1,7 @@ /* * Copyright (c) 2026 Eclipse ThreadX contributors + * RISC-V SMP contributions by + * Eyck-Alexander Jentzsch, MINRES Technologies GmbH * Portions of this file were generated with AI assistance. * SPDX-License-Identifier: MIT */ diff --git a/ports_smp/risc-v-common/tx_thread_smp_current_state_get.S b/ports_smp/risc-v-common/tx_thread_smp_current_state_get.S index f76d1737f..85adb8c9b 100644 --- a/ports_smp/risc-v-common/tx_thread_smp_current_state_get.S +++ b/ports_smp/risc-v-common/tx_thread_smp_current_state_get.S @@ -1,5 +1,7 @@ /* * Copyright (c) 2026 Eclipse ThreadX contributors + * RISC-V SMP contributions by + * Eyck-Alexander Jentzsch, MINRES Technologies GmbH * Portions of this file were generated with AI assistance. * SPDX-License-Identifier: MIT */ diff --git a/ports_smp/risc-v-common/tx_thread_smp_low_level_initialize.S b/ports_smp/risc-v-common/tx_thread_smp_low_level_initialize.S index 0bc0e7f3c..3df074c6d 100644 --- a/ports_smp/risc-v-common/tx_thread_smp_low_level_initialize.S +++ b/ports_smp/risc-v-common/tx_thread_smp_low_level_initialize.S @@ -1,5 +1,7 @@ /* * Copyright (c) 2026 Eclipse ThreadX contributors + * RISC-V SMP contributions by + * Eyck-Alexander Jentzsch, MINRES Technologies GmbH * Portions of this file were generated with AI assistance. * SPDX-License-Identifier: MIT */ diff --git a/ports_smp/risc-v-common/tx_thread_smp_protect.S b/ports_smp/risc-v-common/tx_thread_smp_protect.S index 79dddcc98..87af1a4bf 100644 --- a/ports_smp/risc-v-common/tx_thread_smp_protect.S +++ b/ports_smp/risc-v-common/tx_thread_smp_protect.S @@ -1,5 +1,7 @@ /* * Copyright (c) 2026 Eclipse ThreadX contributors + * RISC-V SMP contributions by + * Eyck-Alexander Jentzsch, MINRES Technologies GmbH * Portions of this file were generated with AI assistance. * SPDX-License-Identifier: MIT */ diff --git a/ports_smp/risc-v-common/tx_thread_smp_time_get.c b/ports_smp/risc-v-common/tx_thread_smp_time_get.c index 15ca0cffe..dc10a732a 100644 --- a/ports_smp/risc-v-common/tx_thread_smp_time_get.c +++ b/ports_smp/risc-v-common/tx_thread_smp_time_get.c @@ -1,5 +1,7 @@ /* * Copyright (c) 2026 Eclipse ThreadX contributors + * RISC-V SMP contributions by + * Eyck-Alexander Jentzsch, MINRES Technologies GmbH * Portions of this file were generated with AI assistance. * SPDX-License-Identifier: MIT */ diff --git a/ports_smp/risc-v-common/tx_thread_smp_unprotect.S b/ports_smp/risc-v-common/tx_thread_smp_unprotect.S index d9c912478..02754448e 100644 --- a/ports_smp/risc-v-common/tx_thread_smp_unprotect.S +++ b/ports_smp/risc-v-common/tx_thread_smp_unprotect.S @@ -1,5 +1,7 @@ /* * Copyright (c) 2026 Eclipse ThreadX contributors + * RISC-V SMP contributions by + * Eyck-Alexander Jentzsch, MINRES Technologies GmbH * Portions of this file were generated with AI assistance. * SPDX-License-Identifier: MIT */ diff --git a/ports_smp/risc-v-common/tx_timer_interrupt.c b/ports_smp/risc-v-common/tx_timer_interrupt.c index 52cfbc06d..fe8d3f6b1 100644 --- a/ports_smp/risc-v-common/tx_timer_interrupt.c +++ b/ports_smp/risc-v-common/tx_timer_interrupt.c @@ -36,6 +36,7 @@ /* AUTHOR */ /* */ /* Scott Larson, Microsoft Corporation */ +/* Eyck-Alexander Jentzsch, MINRES Technologies GmbH */ /* */ /* DESCRIPTION */ /* */ diff --git a/ports_smp/risc-v32_smp/gnu/CMakeLists.txt b/ports_smp/risc-v32_smp/gnu/CMakeLists.txt index 0f2fdca4d..900591f7f 100644 --- a/ports_smp/risc-v32_smp/gnu/CMakeLists.txt +++ b/ports_smp/risc-v32_smp/gnu/CMakeLists.txt @@ -1,4 +1,6 @@ # Copyright (c) 2026 Eclipse ThreadX contributors +# RISC-V SMP contributions by +# Eyck-Alexander Jentzsch, MINRES Technologies GmbH # Portions of this file were generated with AI assistance. # SPDX-License-Identifier: MIT diff --git a/ports_smp/risc-v32_smp/gnu/inc/tx_port.h b/ports_smp/risc-v32_smp/gnu/inc/tx_port.h index 93b88c34d..8b337bb68 100644 --- a/ports_smp/risc-v32_smp/gnu/inc/tx_port.h +++ b/ports_smp/risc-v32_smp/gnu/inc/tx_port.h @@ -30,6 +30,7 @@ /* AUTHOR */ /* */ /* Akif Ejaz, 10xEngineers */ +/* Eyck-Alexander Jentzsch, MINRES Technologies GmbH */ /* */ /* DESCRIPTION */ /* */ diff --git a/ports_smp/risc-v32_smp/gnu/src/tx_thread_context_restore.S b/ports_smp/risc-v32_smp/gnu/src/tx_thread_context_restore.S index 90121f010..6116834a2 100644 --- a/ports_smp/risc-v32_smp/gnu/src/tx_thread_context_restore.S +++ b/ports_smp/risc-v32_smp/gnu/src/tx_thread_context_restore.S @@ -41,6 +41,7 @@ /* */ /* Akif Ejaz, 10xEngineers */ /* Wei-Chen Lai, National Cheng Kung University */ +/* Eyck-Alexander Jentzsch, MINRES Technologies GmbH */ /* */ /* DESCRIPTION */ /* */ diff --git a/ports_smp/risc-v32_smp/gnu/src/tx_thread_context_save.S b/ports_smp/risc-v32_smp/gnu/src/tx_thread_context_save.S index a681e5c63..54bca30c0 100644 --- a/ports_smp/risc-v32_smp/gnu/src/tx_thread_context_save.S +++ b/ports_smp/risc-v32_smp/gnu/src/tx_thread_context_save.S @@ -39,6 +39,7 @@ /* */ /* Akif Ejaz, 10xEngineers */ /* Wei-Chen Lai, National Cheng Kung University */ +/* Eyck-Alexander Jentzsch, MINRES Technologies GmbH */ /* */ /* DESCRIPTION */ /* */ diff --git a/ports_smp/risc-v32_smp/gnu/src/tx_thread_interrupt_control.S b/ports_smp/risc-v32_smp/gnu/src/tx_thread_interrupt_control.S index 6c6880d2b..2786a8d2b 100644 --- a/ports_smp/risc-v32_smp/gnu/src/tx_thread_interrupt_control.S +++ b/ports_smp/risc-v32_smp/gnu/src/tx_thread_interrupt_control.S @@ -30,6 +30,7 @@ /* AUTHOR */ /* */ /* Akif Ejaz, 10xEngineers */ +/* Eyck-Alexander Jentzsch, MINRES Technologies GmbH */ /* */ /* DESCRIPTION */ /* */ diff --git a/ports_smp/risc-v32_smp/gnu/src/tx_thread_schedule.S b/ports_smp/risc-v32_smp/gnu/src/tx_thread_schedule.S index 36fb7bd8a..bbc8e467e 100644 --- a/ports_smp/risc-v32_smp/gnu/src/tx_thread_schedule.S +++ b/ports_smp/risc-v32_smp/gnu/src/tx_thread_schedule.S @@ -38,6 +38,7 @@ /* AUTHOR */ /* */ /* Akif Ejaz, 10xEngineers */ +/* Eyck-Alexander Jentzsch, MINRES Technologies GmbH */ /* */ /* DESCRIPTION */ /* */ diff --git a/ports_smp/risc-v32_smp/gnu/src/tx_thread_smp_current_thread_get.S b/ports_smp/risc-v32_smp/gnu/src/tx_thread_smp_current_thread_get.S index 0d324f335..a166f6d9d 100644 --- a/ports_smp/risc-v32_smp/gnu/src/tx_thread_smp_current_thread_get.S +++ b/ports_smp/risc-v32_smp/gnu/src/tx_thread_smp_current_thread_get.S @@ -1,5 +1,7 @@ /* * Copyright (c) 2026 Eclipse ThreadX contributors + * RISC-V SMP contributions by + * Eyck-Alexander Jentzsch, MINRES Technologies GmbH * Portions of this file were generated with AI assistance. * SPDX-License-Identifier: MIT */ diff --git a/ports_smp/risc-v32_smp/gnu/src/tx_thread_smp_initialize_wait.S b/ports_smp/risc-v32_smp/gnu/src/tx_thread_smp_initialize_wait.S index d45d7d717..c9f13bafb 100644 --- a/ports_smp/risc-v32_smp/gnu/src/tx_thread_smp_initialize_wait.S +++ b/ports_smp/risc-v32_smp/gnu/src/tx_thread_smp_initialize_wait.S @@ -1,5 +1,7 @@ /* * Copyright (c) 2026 Eclipse ThreadX contributors + * RISC-V SMP contributions by + * Eyck-Alexander Jentzsch, MINRES Technologies GmbH * Portions of this file were generated with AI assistance. * SPDX-License-Identifier: MIT */ diff --git a/ports_smp/risc-v32_smp/gnu/src/tx_thread_stack_build.S b/ports_smp/risc-v32_smp/gnu/src/tx_thread_stack_build.S index 524647781..b9c3ed18d 100644 --- a/ports_smp/risc-v32_smp/gnu/src/tx_thread_stack_build.S +++ b/ports_smp/risc-v32_smp/gnu/src/tx_thread_stack_build.S @@ -37,6 +37,7 @@ /* AUTHOR */ /* */ /* Akif Ejaz, 10xEngineers */ +/* Eyck-Alexander Jentzsch, MINRES Technologies GmbH */ /* */ /* DESCRIPTION */ /* */ diff --git a/ports_smp/risc-v32_smp/gnu/src/tx_thread_system_return.S b/ports_smp/risc-v32_smp/gnu/src/tx_thread_system_return.S index 15ee8d53f..65bec6e1f 100644 --- a/ports_smp/risc-v32_smp/gnu/src/tx_thread_system_return.S +++ b/ports_smp/risc-v32_smp/gnu/src/tx_thread_system_return.S @@ -37,6 +37,7 @@ /* AUTHOR */ /* */ /* Akif Ejaz, 10xEngineers */ +/* Eyck-Alexander Jentzsch, MINRES Technologies GmbH */ /* */ /* DESCRIPTION */ /* */ diff --git a/ports_smp/risc-v64_smp/gnu/CMakeLists.txt b/ports_smp/risc-v64_smp/gnu/CMakeLists.txt index 0f2fdca4d..900591f7f 100644 --- a/ports_smp/risc-v64_smp/gnu/CMakeLists.txt +++ b/ports_smp/risc-v64_smp/gnu/CMakeLists.txt @@ -1,4 +1,6 @@ # Copyright (c) 2026 Eclipse ThreadX contributors +# RISC-V SMP contributions by +# Eyck-Alexander Jentzsch, MINRES Technologies GmbH # Portions of this file were generated with AI assistance. # SPDX-License-Identifier: MIT diff --git a/ports_smp/risc-v64_smp/gnu/inc/tx_port.h b/ports_smp/risc-v64_smp/gnu/inc/tx_port.h index 3270c3315..be99cc11f 100644 --- a/ports_smp/risc-v64_smp/gnu/inc/tx_port.h +++ b/ports_smp/risc-v64_smp/gnu/inc/tx_port.h @@ -33,6 +33,7 @@ /* AUTHOR */ /* */ /* Scott Larson, Microsoft Corporation */ +/* Eyck-Alexander Jentzsch, MINRES Technologies GmbH */ /* */ /* DESCRIPTION */ /* */ diff --git a/ports_smp/risc-v64_smp/gnu/src/tx_thread_context_restore.S b/ports_smp/risc-v64_smp/gnu/src/tx_thread_context_restore.S index 933d97bde..252991e12 100644 --- a/ports_smp/risc-v64_smp/gnu/src/tx_thread_context_restore.S +++ b/ports_smp/risc-v64_smp/gnu/src/tx_thread_context_restore.S @@ -34,6 +34,7 @@ /* AUTHOR */ /* */ /* Scott Larson, Microsoft Corporation */ +/* Eyck-Alexander Jentzsch, MINRES Technologies GmbH */ /* */ /* DESCRIPTION */ /* */ diff --git a/ports_smp/risc-v64_smp/gnu/src/tx_thread_context_save.S b/ports_smp/risc-v64_smp/gnu/src/tx_thread_context_save.S index 43a1f604b..1482b10bf 100644 --- a/ports_smp/risc-v64_smp/gnu/src/tx_thread_context_save.S +++ b/ports_smp/risc-v64_smp/gnu/src/tx_thread_context_save.S @@ -37,6 +37,7 @@ /* */ /* Scott Larson, Microsoft Corporation */ /* Wei-Chen Lai, National Cheng Kung University */ +/* Eyck-Alexander Jentzsch, MINRES Technologies GmbH */ /* */ /* DESCRIPTION */ /* */ diff --git a/ports_smp/risc-v64_smp/gnu/src/tx_thread_interrupt_control.S b/ports_smp/risc-v64_smp/gnu/src/tx_thread_interrupt_control.S index 3c8c15dc6..8a26788a5 100644 --- a/ports_smp/risc-v64_smp/gnu/src/tx_thread_interrupt_control.S +++ b/ports_smp/risc-v64_smp/gnu/src/tx_thread_interrupt_control.S @@ -34,6 +34,7 @@ /* AUTHOR */ /* */ /* Scott Larson, Microsoft Corporation */ +/* Eyck-Alexander Jentzsch, MINRES Technologies GmbH */ /* */ /* DESCRIPTION */ /* */ diff --git a/ports_smp/risc-v64_smp/gnu/src/tx_thread_schedule.S b/ports_smp/risc-v64_smp/gnu/src/tx_thread_schedule.S index e1e3dc28d..2ec6367d7 100644 --- a/ports_smp/risc-v64_smp/gnu/src/tx_thread_schedule.S +++ b/ports_smp/risc-v64_smp/gnu/src/tx_thread_schedule.S @@ -33,6 +33,7 @@ /* AUTHOR */ /* */ /* Scott Larson, Microsoft Corporation */ +/* Eyck-Alexander Jentzsch, MINRES Technologies GmbH */ /* */ /* DESCRIPTION */ /* */ diff --git a/ports_smp/risc-v64_smp/gnu/src/tx_thread_smp_current_thread_get.S b/ports_smp/risc-v64_smp/gnu/src/tx_thread_smp_current_thread_get.S index e6e85fba1..001756916 100644 --- a/ports_smp/risc-v64_smp/gnu/src/tx_thread_smp_current_thread_get.S +++ b/ports_smp/risc-v64_smp/gnu/src/tx_thread_smp_current_thread_get.S @@ -1,5 +1,7 @@ /* * Copyright (c) 2026 Eclipse ThreadX contributors + * RISC-V SMP contributions by + * Eyck-Alexander Jentzsch, MINRES Technologies GmbH * Portions of this file were generated with AI assistance. * SPDX-License-Identifier: MIT */ diff --git a/ports_smp/risc-v64_smp/gnu/src/tx_thread_smp_initialize_wait.S b/ports_smp/risc-v64_smp/gnu/src/tx_thread_smp_initialize_wait.S index 364715bc8..c6a6faaad 100644 --- a/ports_smp/risc-v64_smp/gnu/src/tx_thread_smp_initialize_wait.S +++ b/ports_smp/risc-v64_smp/gnu/src/tx_thread_smp_initialize_wait.S @@ -1,5 +1,7 @@ /* * Copyright (c) 2026 Eclipse ThreadX contributors + * RISC-V SMP contributions by + * Eyck-Alexander Jentzsch, MINRES Technologies GmbH * Portions of this file were generated with AI assistance. * SPDX-License-Identifier: MIT */ diff --git a/ports_smp/risc-v64_smp/gnu/src/tx_thread_stack_build.S b/ports_smp/risc-v64_smp/gnu/src/tx_thread_stack_build.S index 6529bbbdb..0979f7cd6 100644 --- a/ports_smp/risc-v64_smp/gnu/src/tx_thread_stack_build.S +++ b/ports_smp/risc-v64_smp/gnu/src/tx_thread_stack_build.S @@ -36,6 +36,7 @@ /* */ /* Scott Larson, Microsoft Corporation */ /* Wei-Chen Lai, National Cheng Kung University */ +/* Eyck-Alexander Jentzsch, MINRES Technologies GmbH */ /* */ /* DESCRIPTION */ /* */ diff --git a/ports_smp/risc-v64_smp/gnu/src/tx_thread_system_return.S b/ports_smp/risc-v64_smp/gnu/src/tx_thread_system_return.S index dad1e9b0f..0d3bc5a04 100644 --- a/ports_smp/risc-v64_smp/gnu/src/tx_thread_system_return.S +++ b/ports_smp/risc-v64_smp/gnu/src/tx_thread_system_return.S @@ -33,6 +33,7 @@ /* AUTHOR */ /* */ /* Scott Larson, Microsoft Corporation */ +/* Eyck-Alexander Jentzsch, MINRES Technologies GmbH */ /* */ /* DESCRIPTION */ /* */