summaryrefslogtreecommitdiff
path: root/test/tx/cmake/module_qemu/tests/module_services.c
diff options
context:
space:
mode:
authorFrédéric Desbiens <[email protected]>2026-10-23 13:06:05 -0400
committerGitHub <[email protected]>2026-10-23 13:06:05 -0400
commit4c2179186c5348175bbce481cfafbc3d13f54a61 (patch)
treeece1b62db161d3aaf6b64dd67896beea5f2024d0 /test/tx/cmake/module_qemu/tests/module_services.c
parent3a77bc385c6cac82c71e1c36d6817e0860d12526 (diff)
Added a QEMU module execution harness for the Cortex-M3 and M4 ports (#821)dev
No Cortex-M module port ran in CI. gcc_check compiles each port's module manager sources and links nothing that loads a module, and the host tests under test/tx/module_manager compile a few portable sources against stubs. A module's start-up, its kernel calls through the supervisor call, the MPU programming on context switches and the fault path that terminates a misbehaving module were exercised only on the Cortex-R52 FVP. test/tx/cmake/module_qemu is a stand-alone CMake project, like test/tx/cmake/riscv, that builds the kernel and Module Manager with a module port's headers, builds test modules position-independent with the port's module linker script, embeds their raw images in manager images, and registers each image with CTest. A Python runner starts it on QEMU's MPS2 board for the core, mps2-an385 for cortex_m3 and mps2-an386 for cortex_m4 built for its FPU with the hard-float ABI, and judges the result line the image prints over semihosting and the exit status it sets. Three images per port: a protected module loaded, started, exercising queues, semaphores, a worker thread and the timer, stopped and unloaded twice with its memory returned; the same module faulting on a read of kernel memory, reported to the fault notification with its module, thread and address and terminated, then stopped and unloaded; and the module built privileged and unprotected, whose identical read succeeds. The fault image is also the check that QEMU's MPU enforces at all. The new module_qemu workflow runs both ports on changes to common/, common_modules/, the two module ports and the harness, installing QEMU from the Ubuntu archive through the bounded apt helpers in scripts/tx_ci_common.sh. Both ports pass 3/3 on QEMU 8.2.2 with Arm GNU 14.3.rel1, using the workflow's commands, with zero compiler warnings; configure, build and run take 9 seconds for both. The AI disclosure check passes. The workflow itself has not run on GitHub yet. Assisted-by: Claude Code (Opus 5.5) <[email protected]>
Diffstat (limited to 'test/tx/cmake/module_qemu/tests/module_services.c')
-rw-r--r--test/tx/cmake/module_qemu/tests/module_services.c133
1 files changed, 133 insertions, 0 deletions
diff --git a/test/tx/cmake/module_qemu/tests/module_services.c b/test/tx/cmake/module_qemu/tests/module_services.c
new file mode 100644
index 00000000..e2fcce44
--- /dev/null
+++ b/test/tx/cmake/module_qemu/tests/module_services.c
@@ -0,0 +1,133 @@
+/***************************************************************************
+ * 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 execution harness: the test module */
+/** */
+/**************************************************************************/
+/**************************************************************************/
+
+/* One module, built twice: once user mode with memory protection and once
+ privileged without it, from the same source; only the preamble's property
+ word differs.
+
+ The start thread exercises the kernel through the module's dispatcher: it
+ allocates and creates a queue, a semaphore and a worker thread, hands the
+ worker a message, waits for the worker to signal back, sleeps on the timer,
+ and records what it saw in the area the manager shared with it. Then, if
+ the manager supplied the address of a word in kernel memory, it reads that
+ word. A protected module must fault on the read and never record its
+ result; a privileged one reads it. The done word is written last, so a
+ module that faults never sets it.
+
+ MISRA C:2023 deviations, confined to this test module: Rule 1.2 and
+ Directive 4.3 (inline assembly to read CONTROL), and Rule 11.4 (the shared
+ area and the kernel word are integer addresses supplied at run time). */
+
+#define TXM_MODULE
+#include "txm_module.h"
+#include "module_shared.h"
+
+#define WORKER_STACK_SIZE ((ULONG) 1024UL)
+#define SLEEP_TICKS ((ULONG) 5UL)
+#define WAIT_TICKS ((ULONG) 100UL)
+
+static TX_THREAD *worker_thread;
+static TX_QUEUE *message_queue;
+static TX_SEMAPHORE *done_semaphore;
+static ULONG worker_stack[WORKER_STACK_SIZE / sizeof(ULONG)];
+static ULONG queue_storage[4];
+
+VOID module_start(ULONG id);
+
+
+/* Keep the first failing status rather than the last. */
+static VOID record_status(ULONG word, UINT status)
+{
+ if ((MODULE_SHARED[word] == (ULONG) TX_SUCCESS) && (status != TX_SUCCESS))
+ {
+ MODULE_SHARED[word] = (ULONG) status;
+ }
+}
+
+
+static VOID worker_entry(ULONG thread_input)
+{
+ULONG message;
+
+ (VOID) thread_input;
+
+ for (;;)
+ {
+ message = 0UL;
+ if (tx_queue_receive(message_queue, &message, TX_WAIT_FOREVER) == TX_SUCCESS)
+ {
+ MODULE_SHARED[MODULE_OUT_QUEUE_VALUE] = message;
+ MODULE_SHARED[MODULE_OUT_WORKER_RUNS] = MODULE_SHARED[MODULE_OUT_WORKER_RUNS] + 1UL;
+ (VOID) tx_semaphore_put(done_semaphore);
+ }
+ }
+}
+
+
+VOID module_start(ULONG id)
+{
+ULONG message;
+ULONG start_time;
+ULONG control;
+ULONG kernel_word;
+
+ (VOID) id;
+
+ MODULE_SHARED[MODULE_OUT_STARTED] = MODULE_STARTED_MAGIC;
+ MODULE_SHARED[MODULE_OUT_CYCLE_SEEN] = MODULE_SHARED[MODULE_IN_CYCLE];
+
+ __asm__ volatile ("mrs %0, control" : "=r"(control));
+ MODULE_SHARED[MODULE_OUT_CONTROL] = control;
+
+ record_status(MODULE_OUT_OBJECT_STATUS, txm_module_object_allocate((VOID **) &message_queue, sizeof(TX_QUEUE)));
+ record_status(MODULE_OUT_OBJECT_STATUS, txm_module_object_allocate((VOID **) &done_semaphore, sizeof(TX_SEMAPHORE)));
+ record_status(MODULE_OUT_OBJECT_STATUS, txm_module_object_allocate((VOID **) &worker_thread, sizeof(TX_THREAD)));
+
+ record_status(MODULE_OUT_CREATE_STATUS, tx_queue_create(message_queue, "module queue", TX_1_ULONG,
+ queue_storage, sizeof(queue_storage)));
+ record_status(MODULE_OUT_CREATE_STATUS, tx_semaphore_create(done_semaphore, "module semaphore", 0UL));
+ record_status(MODULE_OUT_CREATE_STATUS, tx_thread_create(worker_thread, "module worker", worker_entry, 0UL,
+ worker_stack, sizeof(worker_stack),
+ 2U, 2U, TX_NO_TIME_SLICE, TX_AUTO_START));
+
+ message = MODULE_QUEUE_MESSAGE;
+ record_status(MODULE_OUT_CREATE_STATUS, tx_queue_send(message_queue, &message, TX_NO_WAIT));
+ MODULE_SHARED[MODULE_OUT_SEMAPHORE_STATUS] = (ULONG) tx_semaphore_get(done_semaphore, WAIT_TICKS);
+
+ start_time = tx_time_get();
+ (VOID) tx_thread_sleep(SLEEP_TICKS);
+ MODULE_SHARED[MODULE_OUT_SLEEP_TICKS] = tx_time_get() - start_time;
+
+ kernel_word = MODULE_SHARED[MODULE_IN_KERNEL_WORD];
+ if (kernel_word != 0UL)
+ {
+ MODULE_SHARED[MODULE_OUT_KERNEL_READ_TRY] = 1UL;
+ MODULE_SHARED[MODULE_OUT_KERNEL_VALUE] = *((volatile ULONG *) kernel_word);
+ MODULE_SHARED[MODULE_OUT_KERNEL_READ_DONE] = 1UL;
+ }
+
+ MODULE_SHARED[MODULE_DONE] = MODULE_DONE_MAGIC;
+}