/* SPDX-License-Identifier: MIT */ #include #include #include "aclint_ipi.h" #include "board.h" #include "riscv-csr.h" #include "riscv-traps.h" #include "tx_api.h" #include "tx_thread.h" static TX_THREAD controller; static TX_THREAD worker; static ULONG controller_stack[1024]; static ULONG worker_stack[1024]; static TX_SEMAPHORE worker_finished; static volatile UINT worker_waiting_on_hart_1; static volatile UINT request_nested_irq; static volatile UINT observed_nested_irq; /* Disable fault injection in the regression-configured kernel. */ TEST_FLAG test_initialize_flag; #ifndef TX_TIMER_PROCESS_IN_ISR TEST_FLAG threadx_delete_timer_thread; void delete_timer_thread(void) { } #endif UINT vp_fp_context_sleep(void); UINT vp_fp_context_migrate(volatile UINT *waiting); static void check(UINT pass, const char *message) { if (!pass) { puts(message); exit(1); } } static void check_software_interrupt(void) { UINT hart = tx_thread_smp_core_get(); set_aclint_msip(aclint, hart, 0); if (hart == 0 && request_nested_irq) { observed_nested_irq = _tx_thread_smp_current_state_get() == 2; request_nested_irq = 0; } } /* Called by the board timer ISR after it has acknowledged the timer. */ void test_interrupt_dispatch(void) { if (tx_thread_smp_core_get() == 0 && request_nested_irq) { send_ipi(0); csr_set_bits_mstatus(MSTATUS_MIE_BIT_MASK); while (request_nested_irq) { } csr_clr_bits_mstatus(MSTATUS_MIE_BIT_MASK); } } static void run_worker(ULONG input) { (void)input; check(tx_thread_smp_core_get() == 1, "Worker did not start on hart 1"); check(vp_fp_context_migrate(&worker_waiting_on_hart_1) == TX_SUCCESS, "FP context was corrupted during interrupt-driven migration"); check(tx_thread_smp_core_get() == 2, "Worker did not migrate to hart 2"); check(tx_semaphore_put(&worker_finished) == TX_SUCCESS, "Worker completion signal failed"); } static void run_controller(ULONG input) { (void)input; request_nested_irq = 1; check(vp_fp_context_sleep() == TX_SUCCESS, "Sleep failed or corrupted the solicited FP context"); check(observed_nested_irq, "Software interrupt did not nest inside the timer interrupt"); check(tx_thread_resume(&worker) == TX_SUCCESS, "Unable to start worker"); for (UINT tick = 0; tick < 50 && !worker_waiting_on_hart_1; ++tick) { check(tx_thread_sleep(1) == TX_SUCCESS, "Sleep while waiting for worker failed"); } check(worker_waiting_on_hart_1, "Worker never started on hart 1"); check(tx_thread_smp_core_exclude(&worker, TX_THREAD_SMP_CORE_MASK & ~(1UL << 2)) == TX_SUCCESS, "Unable to migrate worker to hart 2"); check(tx_semaphore_get(&worker_finished, 50) == TX_SUCCESS, "Worker did not finish after migration"); puts("SMP context test passed"); exit(0); } void tx_application_define(void *memory) { (void)memory; check(register_irq_handler(RISCV_INT_MSI, check_software_interrupt), "Unable to install software IRQ handler"); check(tx_semaphore_create(&worker_finished, "worker finished", 0) == TX_SUCCESS, "Unable to create worker completion semaphore"); check(tx_thread_create(&worker, "worker", run_worker, 0, worker_stack, sizeof(worker_stack), 5, 5, TX_NO_TIME_SLICE, TX_DONT_START) == TX_SUCCESS, "Unable to create worker"); check(tx_thread_smp_core_exclude(&worker, TX_THREAD_SMP_CORE_MASK & ~(1UL << 1)) == TX_SUCCESS, "Unable to assign worker to hart 1"); check(tx_thread_create(&controller, "controller", run_controller, 0, controller_stack, sizeof(controller_stack), 5, 5, TX_NO_TIME_SLICE, TX_AUTO_START) == TX_SUCCESS, "Unable to create controller"); } int main(void) { tx_kernel_enter(); }