diff --git a/common_modules/module_manager/src/txm_module_manager_thread_create.c b/common_modules/module_manager/src/txm_module_manager_thread_create.c index 24a7c1dbb..180441a40 100644 --- a/common_modules/module_manager/src/txm_module_manager_thread_create.c +++ b/common_modules/module_manager/src/txm_module_manager_thread_create.c @@ -376,11 +376,9 @@ ULONG i; thread_ptr -> tx_thread_module_kernel_stack_size = (ULONG) ((ALIGN_TYPE) (thread_ptr -> tx_thread_module_kernel_stack_end) - (ALIGN_TYPE) (thread_ptr -> tx_thread_module_kernel_stack_start)); } -#if TXM_MODULE_MEMORY_PROTECTION /* Place the stack parameters into the thread's control block. */ thread_ptr -> tx_thread_module_stack_start = stack_start; thread_ptr -> tx_thread_module_stack_size = stack_size; -#endif /* Place the supplied parameters into the thread's control block. */ thread_ptr -> tx_thread_name = name_ptr; @@ -424,9 +422,7 @@ ULONG i; temp_ptr = TX_VOID_TO_UCHAR_POINTER_CONVERT(stack_start); temp_ptr = (TX_UCHAR_POINTER_ADD(temp_ptr, (stack_size - ((ULONG) 1)))); thread_ptr -> tx_thread_stack_end = TX_UCHAR_TO_VOID_POINTER_CONVERT(temp_ptr); -#if TXM_MODULE_MEMORY_PROTECTION thread_ptr -> tx_thread_module_stack_end = thread_ptr -> tx_thread_stack_end; -#endif /* TXM_MODULE_MEMORY_PROTECTION */ #ifndef TX_DISABLE_PREEMPTION_THRESHOLD diff --git a/test/tx/cmake/module_manager/CMakeLists.txt b/test/tx/cmake/module_manager/CMakeLists.txt index 6989a2a0f..4c44597ee 100644 --- a/test/tx/cmake/module_manager/CMakeLists.txt +++ b/test/tx/cmake/module_manager/CMakeLists.txt @@ -394,6 +394,41 @@ target_compile_options( add_test(${CMAKE_BUILD_TYPE}::threadx_module_manager_load_bounds_test threadx_module_manager_load_bounds_test) +# This test runs thread create and thread reset against the Cortex-A7 module port, +# once as shipped and once with the port's MMU support compiled out, which a second +# force-included header models. Neither calls a dispatcher. +foreach(reset_stack_variant "" "_no_mmu") + set(reset_stack_test threadx_module_manager_thread_reset_stack${reset_stack_variant}_test) + + add_executable( + ${reset_stack_test} + ${SOURCE_DIR}/threadx_module_manager_thread_reset_stack_test.c + ${module_manager_dir}/src/txm_module_manager_thread_create.c + ${module_manager_dir}/src/txm_module_manager_thread_reset.c) + + target_include_directories( + ${reset_stack_test} + PRIVATE ${SOURCE_DIR} + ${REPO_ROOT}/common/inc + ${REPO_ROOT}/common_modules/inc + ${module_manager_dir}/inc + ${cortex_a7_module_dir}/inc) + + if(reset_stack_variant STREQUAL "_no_mmu") + set(reset_stack_port_header threadx_module_manager_cortex_a7_no_mmu_port.h) + else() + set(reset_stack_port_header threadx_module_manager_host_test_port.h) + endif() + + target_compile_options( + ${reset_stack_test} + PRIVATE -Wall + -Wextra + -include ${SOURCE_DIR}/${reset_stack_port_header}) + + add_test(${CMAKE_BUILD_TYPE}::${reset_stack_test} ${reset_stack_test}) +endforeach() + # This test checks the module options the Cortex-A35 module ports accept, so it is # compiled once against each of their four headers rather than against a stand-in # port. The SMP ports take the SMP kernel's headers. These ports are AArch64, whose diff --git a/test/tx/module_manager/threadx_module_manager_cortex_a7_no_mmu_port.h b/test/tx/module_manager/threadx_module_manager_cortex_a7_no_mmu_port.h new file mode 100644 index 000000000..8d2cda702 --- /dev/null +++ b/test/tx/module_manager/threadx_module_manager_cortex_a7_no_mmu_port.h @@ -0,0 +1,48 @@ +/*************************************************************************** + * 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. + * + * AI Disclosure: This file was largely AI-generated by Claude Code (Opus 5.5). + * The AI-generated portions may be considered public domain (CC0-1.0) + * and not subject to the project's licence. The human contributor has + * reviewed and verified that the code is correct. + * + * SPDX-License-Identifier: MIT and CC0-1.0 + **************************************************************************/ + + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Test */ +/** */ +/** Module Manager Cortex-A7 port without MMU support */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + +/* The Cortex-A7 module port compiles its MMU support out when the line defining + TXM_MODULE_MEMORY_PROTECTION_ENABLED is removed from txm_module_port.h, and + TXM_MODULE_MEMORY_PROTECTION is then zero. That is the only definition in the + port's headers that depends on the option. + + This header is force-included after the host port shim, ahead of every + translation unit of a test that models such a build. It brings the port's module + header in first and replaces the two definitions with what the port defines + without the option, so every source under test sees the port as an integrator + who removed that line would build it. */ + +#ifndef THREADX_MODULE_MANAGER_CORTEX_A7_NO_MMU_PORT_H +#define THREADX_MODULE_MANAGER_CORTEX_A7_NO_MMU_PORT_H + +#include "threadx_module_manager_host_test_port.h" +#include "txm_module.h" + +#undef TXM_MODULE_MEMORY_PROTECTION_ENABLED +#undef TXM_MODULE_MEMORY_PROTECTION +#define TXM_MODULE_MEMORY_PROTECTION 0x00000000 + +#endif diff --git a/test/tx/module_manager/threadx_module_manager_thread_reset_stack_test.c b/test/tx/module_manager/threadx_module_manager_thread_reset_stack_test.c new file mode 100644 index 000000000..9d9fb7a99 --- /dev/null +++ b/test/tx/module_manager/threadx_module_manager_thread_reset_stack_test.c @@ -0,0 +1,271 @@ +/*************************************************************************** + * 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. + * + * AI Disclosure: This file was largely AI-generated by Claude Code (Opus 5.5). + * The AI-generated portions may be considered public domain (CC0-1.0) + * and not subject to the project's licence. The human contributor has + * reviewed and verified that the code is correct. + * + * SPDX-License-Identifier: MIT and CC0-1.0 + **************************************************************************/ + + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Test */ +/** */ +/** Module thread reset stack frame */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + +/* Thread create records a module thread's stack in the module stack fields of its + control block. Reset rebuilds the thread's entry frame from those fields, so they + must describe the stack the thread was created with whatever the port's memory + protection configuration. + + This test runs the real create and reset services against the Cortex-A7 module + port's headers and records the stack each entry frame is built on. It is built + twice: with the port as shipped, and with the port's MMU support compiled out, + where TXM_MODULE_MEMORY_PROTECTION is zero. */ + +#include + +#include "tx_api.h" +#include "tx_thread.h" +#include "tx_trace.h" +#include "tx_timer.h" +#include "txm_module.h" + + +/* Define the stand-in interrupt lock the port shim counts through. */ + +unsigned int test_interrupt_disable_depth; +unsigned int test_interrupt_disable_max_depth; +unsigned int test_interrupt_restore_underflows; + + +/* Define the kernel state the services under test read and update. */ + +TX_THREAD *_tx_thread_current_ptr; +TX_THREAD *_tx_thread_execute_ptr; +TX_THREAD *_tx_thread_created_ptr; +ULONG _tx_thread_created_count; +volatile UINT _tx_thread_preempt_disable; +volatile ULONG _tx_thread_system_state; +TX_THREAD _tx_timer_thread; + +#ifdef TX_ENABLE_EVENT_TRACE + +/* Define the trace state the trace inserts refer to. The inserts do nothing while + the buffer pointer is null. */ + +TX_TRACE_HEADER *_tx_trace_header_ptr; +TX_TRACE_BUFFER_ENTRY *_tx_trace_buffer_start_ptr; +TX_TRACE_BUFFER_ENTRY *_tx_trace_buffer_end_ptr; +TX_TRACE_BUFFER_ENTRY *_tx_trace_buffer_current_ptr; +ULONG _tx_trace_event_enable_bits; +ULONG _tx_trace_simulated_time; +VOID (*_tx_trace_full_notify_function)(VOID *buffer); + +/* Ignore object registration, which this test does not observe. */ +VOID _tx_trace_object_register(UCHAR object_type, VOID *object_ptr, CHAR *object_name, ULONG parameter_1, ULONG parameter_2) +{ + (VOID) object_type; + (VOID) object_ptr; + (VOID) object_name; + (VOID) parameter_1; + (VOID) parameter_2; +} + +#endif + + +#define TEST_STACK_BYTES 1024U + +/* Define the module thread stacks. */ + +static union +{ + ALIGN_TYPE test_stack_alignment; + UCHAR test_stack_bytes[TEST_STACK_BYTES]; +} test_stack, test_other_stack; + +static TX_THREAD test_thread; +static TX_THREAD test_overlap_thread; +static TX_THREAD test_other_thread; +static TXM_MODULE_INSTANCE test_module; + +static UINT test_failures; +static ULONG test_checks; +static ULONG test_stack_builds; +static VOID *test_frame_stack_start; +static VOID *test_frame_stack_end; +static ULONG test_frame_stack_size; + + +/* Record the outcome of one expectation. */ +static VOID test_expect(const char *description, ULONG actual, ULONG expected) +{ + test_checks++; + + if (actual != expected) + { + printf("FAIL: %s (expected %lu, got %lu)\n", description, (unsigned long) expected, (unsigned long) actual); + test_failures++; + } +} + + +/* Record the stack an entry frame is built on, which the port builds below + tx_thread_stack_end. */ +VOID _txm_module_manager_thread_stack_build(TX_THREAD *thread_ptr, VOID (*shell_function)(TX_THREAD *, TXM_MODULE_INSTANCE *)) +{ + (VOID) shell_function; + + test_stack_builds++; + test_frame_stack_start = thread_ptr -> tx_thread_stack_start; + test_frame_stack_end = thread_ptr -> tx_thread_stack_end; + test_frame_stack_size = thread_ptr -> tx_thread_stack_size; +} + + +/* Stand in for the kernel services thread create refers to. The threads here are + created without user mode and without starting, so only the preemption check + runs. */ +VOID _tx_thread_system_preempt_check(VOID) +{ +} + +VOID _tx_thread_system_resume(TX_THREAD *thread_ptr) +{ + (VOID) thread_ptr; + printf("FAIL: a thread created without starting was resumed\n"); + test_failures++; +} + +VOID _tx_thread_timeout(ULONG timeout_input) +{ + (VOID) timeout_input; +} + +UINT _txm_module_manager_object_allocate(VOID **object_ptr_ptr, ULONG object_size, TXM_MODULE_INSTANCE *module_instance) +{ + (VOID) object_ptr_ptr; + (VOID) object_size; + (VOID) module_instance; + printf("FAIL: a kernel stack was allocated for a thread without user mode\n"); + test_failures++; + return(TX_NO_MEMORY); +} + +ALIGN_TYPE _txm_module_manager_kernel_dispatch(ULONG kernel_request, ALIGN_TYPE param_0, ALIGN_TYPE param_1, ALIGN_TYPE param_2) +{ + (VOID) kernel_request; + (VOID) param_0; + (VOID) param_1; + (VOID) param_2; + return(0); +} + +ALIGN_TYPE _txm_module_manager_user_mode_entry(ULONG kernel_request, ALIGN_TYPE param_1, ALIGN_TYPE param_2, ALIGN_TYPE param_3) +{ + (VOID) kernel_request; + (VOID) param_1; + (VOID) param_2; + (VOID) param_3; + return(0); +} + + +/* The module's shell and thread entry functions. Neither runs. */ +static VOID test_shell_entry(TX_THREAD *thread_ptr, TXM_MODULE_INSTANCE *module_instance) +{ + (VOID) thread_ptr; + (VOID) module_instance; +} + +static VOID test_thread_entry(ULONG id) +{ + (VOID) id; +} + + +/* Create a module thread without user mode on the given stack, without starting it. */ +static UINT test_create(TX_THREAD *thread_ptr, UCHAR *stack_start) +{ + return(_txm_module_manager_thread_create(thread_ptr, "module thread", test_shell_entry, + test_thread_entry, 0UL, stack_start, TEST_STACK_BYTES, + 1U, 1U, TX_NO_TIME_SLICE, TX_DONT_START, + (UINT) sizeof(TX_THREAD), &test_module)); +} + + +int main(void) +{ +UINT status; +VOID *created_stack_start; +VOID *created_stack_end; +ULONG created_stack_size; + + /* A module loaded without memory protection or user mode. */ + test_module.txm_module_instance_property_flags = 0UL; + test_module.txm_module_instance_shell_entry_function = test_shell_entry; + + status = test_create(&test_thread, test_stack.test_stack_bytes); + + test_expect("a module thread without user mode is created", (ULONG) status, (ULONG) TX_SUCCESS); + test_expect("...building its entry frame once", test_stack_builds, 1UL); + test_expect("...on the stack it was given", + (ULONG) ((test_frame_stack_start >= (VOID *) test_stack.test_stack_bytes) && + (test_frame_stack_end > test_frame_stack_start) && + (test_frame_stack_end < (VOID *) &test_stack.test_stack_bytes[TEST_STACK_BYTES])), + (ULONG) TX_TRUE); + + created_stack_start = test_frame_stack_start; + created_stack_end = test_frame_stack_end; + created_stack_size = test_frame_stack_size; + + /* A completed thread is rebuilt on the stack it was created with. */ + test_thread.tx_thread_state = TX_COMPLETED; + test_stack_builds = 0UL; + test_frame_stack_end = TX_NULL; + + status = _txm_module_manager_thread_reset(&test_thread); + + test_expect("a completed module thread is reset", (ULONG) status, (ULONG) TX_SUCCESS); + test_expect("...building its entry frame once", test_stack_builds, 1UL); + test_expect("...below the end of its own stack", + (ULONG) (ALIGN_TYPE) test_frame_stack_end, (ULONG) (ALIGN_TYPE) created_stack_end); + test_expect("...from the start of its own stack", + (ULONG) (ALIGN_TYPE) test_frame_stack_start, (ULONG) (ALIGN_TYPE) created_stack_start); + test_expect("...with its own stack size", test_frame_stack_size, created_stack_size); + + /* Thread create refuses a stack that overlaps a module thread's stack. */ + status = test_create(&test_overlap_thread, &test_stack.test_stack_bytes[TEST_STACK_BYTES / 2U]); + + test_expect("a stack overlapping a module thread's stack is refused", (ULONG) status, (ULONG) TX_PTR_ERROR); + test_expect("...leaving one thread created", _tx_thread_created_count, 1UL); + + status = test_create(&test_other_thread, test_other_stack.test_stack_bytes); + + test_expect("a separate stack is accepted", (ULONG) status, (ULONG) TX_SUCCESS); + test_expect("...leaving two threads created", _tx_thread_created_count, 2UL); + + test_expect("the interrupt lock is balanced", (ULONG) test_interrupt_disable_depth, 0UL); + test_expect("...and never released more than taken", (ULONG) test_interrupt_restore_underflows, 0UL); + + if (test_failures == 0U) + { + printf("SUCCESS! %lu expectations\n", (unsigned long) test_checks); + return(0); + } + + printf("ERROR: %u expectation(s) failed of %lu\n", test_failures, (unsigned long) test_checks); + return(1); +}