diff --git a/port/threadx_smp/CMakeLists.txt b/port/threadx_smp/CMakeLists.txt index c784a5c..6c83e29 100644 --- a/port/threadx_smp/CMakeLists.txt +++ b/port/threadx_smp/CMakeLists.txt @@ -14,6 +14,7 @@ set(THREADX_SMP_CUSTOM_INC set(THREADX_SMP_CUSTOM_SRC src/trap_entry.S src/tx_initialize_low_level.c + src/tx_port_offset_check.c src/tx_thread_context_restore.S src/tx_thread_context_save.S src/tx_thread_interrupt_control.S diff --git a/port/threadx_smp/cmake/BuildAsmOffsets.cmake b/port/threadx_smp/cmake/BuildAsmOffsets.cmake deleted file mode 100644 index 83d1ffa..0000000 --- a/port/threadx_smp/cmake/BuildAsmOffsets.cmake +++ /dev/null @@ -1,41 +0,0 @@ -if(NOT DEFINED COMPILER) - message(FATAL_ERROR "COMPILER is required") -endif() - -if(NOT DEFINED SOURCE) - message(FATAL_ERROR "SOURCE is required") -endif() - -if(NOT DEFINED ASM_OUTPUT) - message(FATAL_ERROR "ASM_OUTPUT is required") -endif() - -if(NOT DEFINED HEADER_OUTPUT) - message(FATAL_ERROR "HEADER_OUTPUT is required") -endif() - -if(NOT DEFINED GENERATE_SCRIPT) - message(FATAL_ERROR "GENERATE_SCRIPT is required") -endif() - -execute_process( - COMMAND "${COMPILER}" ${COMPILER_ARG1} ${CFLAG_ARGS} ${INCLUDE_ARGS} ${DEFINE_ARGS} -S -o "${ASM_OUTPUT}" "${SOURCE}" - RESULT_VARIABLE compile_result - OUTPUT_VARIABLE compile_stdout - ERROR_VARIABLE compile_stderr -) - -if(NOT compile_result EQUAL 0) - message(FATAL_ERROR "failed to compile asm offsets source\n${compile_stdout}${compile_stderr}") -endif() - -execute_process( - COMMAND "${CMAKE_COMMAND}" -DINPUT="${ASM_OUTPUT}" -DOUTPUT="${HEADER_OUTPUT}" -P "${GENERATE_SCRIPT}" - RESULT_VARIABLE generate_result - OUTPUT_VARIABLE generate_stdout - ERROR_VARIABLE generate_stderr -) - -if(NOT generate_result EQUAL 0) - message(FATAL_ERROR "failed to generate asm offsets header\n${generate_stdout}${generate_stderr}") -endif() diff --git a/port/threadx_smp/cmake/GenerateAsmOffsets.cmake b/port/threadx_smp/cmake/GenerateAsmOffsets.cmake deleted file mode 100644 index 4b598e3..0000000 --- a/port/threadx_smp/cmake/GenerateAsmOffsets.cmake +++ /dev/null @@ -1,22 +0,0 @@ -if(NOT DEFINED INPUT) - message(FATAL_ERROR "INPUT is required") -endif() - -if(NOT DEFINED OUTPUT) - message(FATAL_ERROR "OUTPUT is required") -endif() - -file(STRINGS "${INPUT}" OFFSET_LINES REGEX "->") - -set(OFFSET_CONTENT "/* Generated by GenerateAsmOffsets.cmake. */\n") - -foreach(LINE IN LISTS OFFSET_LINES) - string(REGEX MATCH "-->([A-Za-z0-9_]+)[^0-9-]*(-?[0-9]+)" _ "${LINE}") - if(NOT CMAKE_MATCH_1) - continue() - endif() - - string(APPEND OFFSET_CONTENT "#define ${CMAKE_MATCH_1} ${CMAKE_MATCH_2}\n") -endforeach() - -file(WRITE "${OUTPUT}" "${OFFSET_CONTENT}") diff --git a/port/threadx_smp/inc/tx_port.h b/port/threadx_smp/inc/tx_port.h index a0729c2..8e86c6d 100644 --- a/port/threadx_smp/inc/tx_port.h +++ b/port/threadx_smp/inc/tx_port.h @@ -54,6 +54,16 @@ #ifndef TX_PORT_H #define TX_PORT_H +/* Assembly uses this byte displacement for atomic accesses to + TX_THREAD.tx_thread_smp_core_control. Changes to ULONG, the control block, + or thread and timer extensions can move the field. Recheck the layout with + the target compiler when changing it. See tx_port_offset_check.c. */ +#if __riscv_xlen == 64 +#define TX_THREAD_SMP_LOCK_READY_BIT_OFFSET 280 +#else +#define TX_THREAD_SMP_LOCK_READY_BIT_OFFSET 152 +#endif + #ifdef __ASSEMBLER__ #if __riscv_xlen == 64 @@ -64,7 +74,6 @@ #define AMOSWAP_AQ amoswap.d.aq #define AMOSWAP_RL amoswap.d.rl #define LOG_REGBYTES 3 -#define TX_THREAD_SMP_LOCK_READY_BIT_OFFSET 312 // This changes if thread or timer internal extensions are used #else #define SLL32 sll #define STORE sw @@ -73,7 +82,6 @@ #define AMOSWAP_AQ amoswap.w.aq #define AMOSWAP_RL amoswap.w.rl #define LOG_REGBYTES 2 -#define TX_THREAD_SMP_LOCK_READY_BIT_OFFSET 168 // This changes if thread or timer internal extensions are used #endif #define REGBYTES (1 << LOG_REGBYTES) diff --git a/port/threadx_smp/src/tx_port_offset_check.c b/port/threadx_smp/src/tx_port_offset_check.c new file mode 100644 index 0000000..b70cc88 --- /dev/null +++ b/port/threadx_smp/src/tx_port_offset_check.c @@ -0,0 +1,13 @@ +/* SPDX-License-Identifier: MIT */ + +#include + +#include "tx_api.h" + +/* Use C99 typedefs so stale assembly layout assumptions fail the build. */ +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) == __riscv_xlen / 8 && + TX_THREAD_SMP_LOCK_READY_BIT_OFFSET % (__riscv_xlen / 8) == 0 ? 1 : -1];