From 7c2ea3fe6b18a48e6ac53db572601541c5dfda32 Mon Sep 17 00:00:00 2001 From: Dev10-sys Date: Sat, 21 Nov 2026 14:53:33 +0530 Subject: [PATCH 1/2] Dropped the SMP Linux port memory sync test coverage macro In _tx_initialize_kernel_enter (common_smp/src/tx_initialize_kernel_enter.c), the kernel waits for secondary cores to become ready with a loop: do { ... } while (other_core_status != ((ULONG) 0)); On the SMP Linux port, virtual cores are simulated rather than backed by hardware secondary cores. TX_PORT_SPECIFIC_PRE_SCHEDULER_INITIALIZATION (tx_port.h:427) zeroes _tx_thread_system_state[1 .. TX_THREAD_SMP_MAX_CORES-1] on core 0 before the loop begins, causing the loop condition to evaluate to false on the first pass. To force both outcomes of the loop-back branch in coverage reports, the port's TX_REGRESSION_TEST block defined TX_PORT_SPECIFIC_MEMORY_SYNCHRONIZATION to fold core 0's own _tx_thread_system_state[0] into other_core_status and clear it. This artificially forced a second loop pass, misrepresenting the loop-back branch as covered when the port cannot exercise it. Dropped the TX_PORT_SPECIFIC_MEMORY_SYNCHRONIZATION macro from the TX_REGRESSION_TEST block in ports_smp/linux/gnu/inc/tx_port.h, falling back to its default empty definition in common_smp/inc/tx_api.h. Line 167 remains 100% line-covered while the coverage report accurately reflects that the loop-back edge is unreached on this simulated port. Verified with the full SMP regression suite: all 118 ctest cases pass cleanly. Fixes: #768 Signed-off-by: Dev10-sys --- ports_smp/linux/gnu/inc/tx_port.h | 4 ---- 1 file changed, 4 deletions(-) diff --git a/ports_smp/linux/gnu/inc/tx_port.h b/ports_smp/linux/gnu/inc/tx_port.h index 2a2e7ee42..843d264f8 100644 --- a/ports_smp/linux/gnu/inc/tx_port.h +++ b/ports_smp/linux/gnu/inc/tx_port.h @@ -194,10 +194,6 @@ extern UINT priority_change_extension_selection; /* Define extension macros for automated coverage tests. */ -#define TX_PORT_SPECIFIC_MEMORY_SYNCHRONIZATION other_core_status = other_core_status + _tx_thread_system_state[0]; \ - _tx_thread_system_state[0] = 0; - - #define TX_BYTE_ALLOCATE_EXTENSION if (threadx_byte_allocate_loop_test == ((TEST_FLAG) 1)) \ { \ pool_ptr -> tx_byte_pool_owner = TX_NULL; \ From 8071380aa2684a8c23019a3134e87c1bdfd12efe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Desbiens?= Date: Tue, 1 Dec 2026 14:11:37 -0500 Subject: [PATCH 2/2] Covered the SMP core-ready wait on Cortex-A53 QEMU The Linux SMP port clears secondary core states before the kernel's core-ready wait, leaving the loop-back outcome unexercised. Boot two Cortex-A53 CPUs in QEMU and exercise the production startup handshake. Require both CPUs to run a ThreadX thread and gcov to report both wait outcomes in a dedicated CI job. The QEMU test recorded 609,628 loop-backs and one exit. The coverage check rejected a report with a missing outcome, and GCC 14 passed 118/118 Linux SMP tests on the PR head. Assisted-by: Codex (GPT-6.2-Sol) --- .github/workflows/smp_startup_qemu.yml | 81 +++++++++++ test/smp/cmake/startup_qemu/CMakeLists.txt | 32 +++++ test/smp/cmake/startup_qemu/README.md | 21 +++ test/smp/cmake/startup_qemu/memory.ld | 36 +++++ test/smp/cmake/startup_qemu/run.cmake | 29 ++++ test/smp/cmake/startup_qemu/startup.S | 131 +++++++++++++++++ test/smp/cmake/startup_qemu/startup.c | 159 +++++++++++++++++++++ 7 files changed, 489 insertions(+) create mode 100644 .github/workflows/smp_startup_qemu.yml create mode 100644 test/smp/cmake/startup_qemu/CMakeLists.txt create mode 100644 test/smp/cmake/startup_qemu/README.md create mode 100644 test/smp/cmake/startup_qemu/memory.ld create mode 100644 test/smp/cmake/startup_qemu/run.cmake create mode 100644 test/smp/cmake/startup_qemu/startup.S create mode 100644 test/smp/cmake/startup_qemu/startup.c diff --git a/.github/workflows/smp_startup_qemu.yml b/.github/workflows/smp_startup_qemu.yml new file mode 100644 index 000000000..024550629 --- /dev/null +++ b/.github/workflows/smp_startup_qemu.yml @@ -0,0 +1,81 @@ +name: smp_startup_qemu + +on: + push: + branches: [ master, dev ] + paths: + - ".github/workflows/smp_startup_qemu.yml" + - "CMakeLists.txt" + - "cmake/aarch64-none-elf.cmake" + - "cmake/cortex_a53.cmake" + - "common_smp/**" + - "ports_smp/cortex_a53_smp/gnu/**" + - "scripts/tx_ci_common.sh" + - "test/smp/cmake/startup_qemu/**" + pull_request: + branches: [ master, dev ] + paths: + - ".github/workflows/smp_startup_qemu.yml" + - "CMakeLists.txt" + - "cmake/aarch64-none-elf.cmake" + - "cmake/cortex_a53.cmake" + - "common_smp/**" + - "ports_smp/cortex_a53_smp/gnu/**" + - "scripts/tx_ci_common.sh" + - "test/smp/cmake/startup_qemu/**" + +permissions: + contents: read + +jobs: + smp_startup_qemu: + runs-on: ubuntu-24.04 + timeout-minutes: 15 + + env: + GCC_VERSION: 14.3.rel1 + + steps: + - name: Check out the repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + + # Share the AArch64 toolchain cache with gcc_check.yml. + - name: Cache the AArch64 Arm GNU toolchain + id: cache-arm64 + uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: toolchain64 + key: arm-gnu-toolchain-${{ env.GCC_VERSION }}-x86_64-aarch64-none-elf + + - name: Install the AArch64 Arm GNU toolchain + if: steps.cache-arm64.outputs.cache-hit != 'true' + run: | + set -eu + base="https://developer.arm.com/-/media/Files/downloads/gnu/$GCC_VERSION/binrel" + archive="arm-gnu-toolchain-$GCC_VERSION-x86_64-aarch64-none-elf.tar.xz" + mkdir -p toolchain64 + cd toolchain64 + curl -fsSLO "$base/$archive" + curl -fsSLO "$base/$archive.sha256asc" + sha256sum -c "$archive.sha256asc" + tar xf "$archive" + rm -f "$archive" + + - name: Install QEMU + run: | + set -eu + . scripts/tx_ci_common.sh + packages="qemu-system-arm" + command -v ninja >/dev/null 2>&1 || packages="$packages ninja-build" + retry sudo "${TIMEOUT[@]}" apt-get "${APT_OPTIONS[@]}" update || true + # shellcheck disable=SC2086 + retry sudo "${TIMEOUT[@]}" apt-get "${APT_OPTIONS[@]}" install -y --no-install-recommends $packages + + - name: Build and run the SMP startup test + run: | + set -eu + export PATH="$GITHUB_WORKSPACE/toolchain64/arm-gnu-toolchain-$GCC_VERSION-x86_64-aarch64-none-elf/bin:$PATH" + cmake -S test/smp/cmake/startup_qemu -B build/smp_startup_qemu -G Ninja \ + -DCMAKE_TOOLCHAIN_FILE="$PWD/cmake/cortex_a53.cmake" + cmake --build build/smp_startup_qemu + ctest --test-dir build/smp_startup_qemu --verbose --no-tests=error diff --git a/test/smp/cmake/startup_qemu/CMakeLists.txt b/test/smp/cmake/startup_qemu/CMakeLists.txt new file mode 100644 index 000000000..29b82cffd --- /dev/null +++ b/test/smp/cmake/startup_qemu/CMakeLists.txt @@ -0,0 +1,32 @@ +# Copyright (c) 2026 Eclipse ThreadX contributors +# SPDX-License-Identifier: MIT + +cmake_minimum_required(VERSION 3.18) +project(threadx_smp_startup_qemu LANGUAGES C ASM) + +set(CMAKE_C_STANDARD 99) +set(CMAKE_C_STANDARD_REQUIRED ON) +set(CMAKE_C_EXTENSIONS OFF) + +find_program(QEMU NAMES qemu-system-aarch64 REQUIRED) +find_program(GCOV NAMES aarch64-none-elf-gcov REQUIRED) + +get_filename_component(THREADX_SOURCE "${CMAKE_CURRENT_LIST_DIR}/../../../.." ABSOLUTE) +set(THREADX_SMP ON) +add_subdirectory("${THREADX_SOURCE}" threadx) +target_compile_definitions(threadx PUBLIC TX_THREAD_SMP_MAX_CORES=2 TX_THREAD_SMP_CORE_MASK=3 EL1) +set_property(SOURCE "${THREADX_SOURCE}/common_smp/src/tx_initialize_kernel_enter.c" + TARGET_DIRECTORY threadx APPEND PROPERTY COMPILE_OPTIONS + -O0 -fprofile-arcs -ftest-coverage -fprofile-info-section) + +add_executable(smp_startup startup.S startup.c) +target_link_options(smp_startup PRIVATE -nostartfiles "-T${CMAKE_CURRENT_LIST_DIR}/memory.ld") +set_property(TARGET smp_startup APPEND PROPERTY LINK_DEPENDS "${CMAKE_CURRENT_LIST_DIR}/memory.ld") +target_link_libraries(smp_startup PRIVATE threadx gcov) + +enable_testing() +add_test(NAME smp_startup_qemu COMMAND "${CMAKE_COMMAND}" + "-DQEMU=${QEMU}" "-DGCOV=${GCOV}" "-DIMAGE=$" + "-DGCOV_UNIT=${CMAKE_CURRENT_BINARY_DIR}/threadx/CMakeFiles/threadx.dir/common_smp/src/tx_initialize_kernel_enter.c" + -P "${CMAKE_CURRENT_LIST_DIR}/run.cmake") +set_tests_properties(smp_startup_qemu PROPERTIES TIMEOUT 30) diff --git a/test/smp/cmake/startup_qemu/README.md b/test/smp/cmake/startup_qemu/README.md new file mode 100644 index 000000000..e97daa981 --- /dev/null +++ b/test/smp/cmake/startup_qemu/README.md @@ -0,0 +1,21 @@ +# Cortex-A53 SMP startup test + +The test boots two Cortex-A53 cores on QEMU `virt`. Core 1 waits five virtual +milliseconds after release, then enters the port's secondary startup routine +and clears its own system state. The test requires both cores to run a thread +and gcov to report both outcomes of `_tx_initialize_kernel_enter`'s core-ready +wait condition. + +With `aarch64-none-elf-gcc`, `aarch64-none-elf-gcov`, and `qemu-system-aarch64` +on `PATH`, run from the repository root. + +```sh +cmake -S test/smp/cmake/startup_qemu -B build/smp_startup_qemu -G Ninja \ + -DCMAKE_TOOLCHAIN_FILE="$PWD/cmake/cortex_a53.cmake" +cmake --build build/smp_startup_qemu +ctest --test-dir build/smp_startup_qemu --verbose --no-tests=error +``` + +`startup.c` deviates from MISRA C:2023 Rule 11.5 in the gcov callbacks. The GCC +callback interface passes the file handle through `void *`. The callbacks +recover the pointer to the original `long` object. diff --git a/test/smp/cmake/startup_qemu/memory.ld b/test/smp/cmake/startup_qemu/memory.ld new file mode 100644 index 000000000..a6cd9c942 --- /dev/null +++ b/test/smp/cmake/startup_qemu/memory.ld @@ -0,0 +1,36 @@ +/* Copyright (c) 2026 Eclipse ThreadX contributors */ +/* SPDX-License-Identifier: MIT */ + +ENTRY(_start) +PHDRS +{ + code PT_LOAD FLAGS(5); + data PT_LOAD FLAGS(6); +} +SECTIONS +{ + . = 0x40200000; + .text : { KEEP(*(.text.start)) *(.text .text.*) } :code + .rodata : { *(.rodata .rodata.*) } :code + .gcov_info : + { + __gcov_info_start = .; + KEEP(*(.gcov_info)) + } :data + .data : { *(.data .data.*) } :data + .bss (NOLOAD) : + { + . = ALIGN(16); + __bss_start = .; + *(.bss .bss.*) *(COMMON) + . = ALIGN(16); + __bss_end = .; + . += 0x4000; + __primary_stack = .; + . += 0x4000; + __secondary_stack = .; + __top_of_ram = .; + PROVIDE(end = .); + } :data +} +ASSERT(SIZEOF(.gcov_info) == 8, "Expected one instrumented translation unit") diff --git a/test/smp/cmake/startup_qemu/run.cmake b/test/smp/cmake/startup_qemu/run.cmake new file mode 100644 index 000000000..8d6890a9f --- /dev/null +++ b/test/smp/cmake/startup_qemu/run.cmake @@ -0,0 +1,29 @@ +# Copyright (c) 2026 Eclipse ThreadX contributors +# SPDX-License-Identifier: MIT + +file(REMOVE "${GCOV_UNIT}.gcda") +execute_process( + COMMAND "${QEMU}" -M virt,gic-version=3 -cpu cortex-a53 -smp 2 + -accel tcg,thread=single -icount shift=0,sleep=off -m 128M -nographic -nic none + -semihosting-config enable=on,target=native -kernel "${IMAGE}" + RESULT_VARIABLE qemu_status OUTPUT_VARIABLE qemu_output ERROR_VARIABLE qemu_output + TIMEOUT 20) +if(NOT qemu_status STREQUAL "0" OR NOT EXISTS "${GCOV_UNIT}.gcda") + message(FATAL_ERROR "SMP startup failed (${qemu_status})\n${qemu_output}") +endif() +message(STATUS "${qemu_output}") + +execute_process( + COMMAND "${GCOV}" -b -c -t "${GCOV_UNIT}.gcno" + RESULT_VARIABLE gcov_status OUTPUT_VARIABLE coverage ERROR_VARIABLE gcov_error) +if(NOT gcov_status STREQUAL "0") + message(FATAL_ERROR "Could not read SMP startup coverage\n${gcov_error}") +endif() + +string(REGEX MATCH + "while \\(other_core_status[^\n]*\nbranch +0 taken ([0-9]+)[^\n]*\nbranch +1 taken ([0-9]+)" + wait_branches "${coverage}") +if(NOT wait_branches OR CMAKE_MATCH_1 LESS 1 OR CMAKE_MATCH_2 LESS 1) + message(FATAL_ERROR "Expected both SMP startup wait outcomes\n${wait_branches}") +endif() +message(STATUS "SMP startup wait branch counts ${CMAKE_MATCH_1} and ${CMAKE_MATCH_2}") diff --git a/test/smp/cmake/startup_qemu/startup.S b/test/smp/cmake/startup_qemu/startup.S new file mode 100644 index 000000000..0422cf4c5 --- /dev/null +++ b/test/smp/cmake/startup_qemu/startup.S @@ -0,0 +1,131 @@ +/*************************************************************************** + * 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 Codex (GPT-6.2-Sol). + * 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 + **************************************************************************/ + + .section .text.start,"ax" + + // Zero the shared state before starting the primary core. + .global _start +_start: + msr daifset, #15 + msr spsel, #0 + ldr x0, =__primary_stack + mov sp, x0 + ldr x0, =__bss_start + ldr x1, =__bss_end +1: + cmp x0, x1 + b.hs 2f + stp xzr, xzr, [x0], #16 + b 1b +2: + bl set_up_core + b primary_start + + // Give core 0 five virtual milliseconds to exercise the wait loop. +secondary_start: + msr daifset, #15 + msr spsel, #0 + ldr x0, =__secondary_stack + mov sp, x0 + bl set_up_core + ldr x0, =_tx_thread_smp_release_cores_flag +1: + ldr w1, [x0] + cbz w1, 1b + mrs x0, cntfrq_el0 + mov x1, #200 + udiv x0, x0, x1 + mrs x1, cntpct_el0 +2: + mrs x2, cntpct_el0 + sub x2, x2, x1 + cmp x2, x0 + b.lo 2b + b _tx_thread_smp_initialize_wait + + // Set up exception handling and a shared identity mapping for virt RAM. +set_up_core: + mov x1, sp + sub x1, x1, #0x2000 + msr spsel, #1 + mov sp, x1 + msr spsel, #0 + ldr x0, =vectors + msr vbar_el1, x0 + mrs x0, cpacr_el1 + orr x0, x0, #(3 << 20) + msr cpacr_el1, x0 + mov x0, #0xff00 + msr mair_el1, x0 + ldr x0, =0x280993519 + msr tcr_el1, x0 + ldr x0, =level_one_table + msr ttbr0_el1, x0 + isb + mrs x0, sctlr_el1 + bic x0, x0, #2 + orr x0, x0, #1 + msr sctlr_el1, x0 + isb + ret + + // Start core 1 through QEMU virt's PSCI HVC conduit. + .global start_secondary_core +start_secondary_core: + ldr x0, =0xc4000003 + mov x1, #1 + ldr x2, =secondary_start + mov x3, #0 + hvc #0 + ret + + // Return when each core has entered a ThreadX thread. + .global wait_for_both_cores +wait_for_both_cores: + ldr x1, =started_cores + add x2, x1, w0, uxtw #2 + mov w3, #1 + stlr w3, [x2] + eor w0, w0, #1 + add x2, x1, w0, uxtw #2 +1: + ldar w3, [x2] + cbz w3, 1b + ret + + // Call the Arm semihosting service with arguments in x0 and x1. + .global semihost_call +semihost_call: + hlt #0xf000 + ret + + .balign 2048 +vectors: + .rept 16 + b startup_fault + .balign 128 + .endr + + .section .rodata.boot,"a" + .balign 4096 +level_one_table: + .quad 0x00000401 + .quad 0x40000705 + .zero 4080 + + .bss + .balign 4 +started_cores: + .zero 8 diff --git a/test/smp/cmake/startup_qemu/startup.c b/test/smp/cmake/startup_qemu/startup.c new file mode 100644 index 000000000..fa19d7ca3 --- /dev/null +++ b/test/smp/cmake/startup_qemu/startup.c @@ -0,0 +1,159 @@ +/*************************************************************************** + * 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 Codex (GPT-6.2-Sol). + * 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 + **************************************************************************/ + +#include +#include +#include + +#include "tx_api.h" +#include "tx_thread.h" + +#define SEMIHOST_OPEN 0x01L +#define SEMIHOST_CLOSE 0x02L +#define SEMIHOST_WRITE0 0x04L +#define SEMIHOST_WRITE 0x05L +#define SEMIHOST_EXIT_EXTENDED 0x20L +#define SEMIHOST_EXIT_REASON 0x20026L +#define SEMIHOST_BINARY_WRITE 5UL + +/* Arm semihosting SYS_OPEN argument block. */ +struct semihost_open_args +{ + const char *name; + unsigned long mode; + size_t length; +}; + +/* Arm semihosting SYS_WRITE argument block. */ +struct semihost_write_args +{ + long file; + const void *data; + size_t length; +}; + +extern long semihost_call(long operation, const void *argument); +extern long start_secondary_core(void); +extern void wait_for_both_cores(UINT core); +extern const struct gcov_info *const __gcov_info_start; + +void primary_start(void); +void startup_fault(void); + +static TX_THREAD threads[2]; +static uint64_t stacks[2][512]; +static CHAR startup_thread_name[] = "SMP startup"; + +/* Report the test result to QEMU and terminate the guest. */ +static void exit_test(long status, const char *message) +{ + const long args[] = { SEMIHOST_EXIT_REASON, status }; + + (void) semihost_call(SEMIHOST_WRITE0, message); + (void) semihost_call(SEMIHOST_EXIT_EXTENDED, args); + for (;;) + { + } +} + +/* Fail promptly if an exception occurs during startup. */ +void startup_fault(void) +{ + exit_test(1L, "Unexpected exception during SMP startup\n"); +} + +/* Open the data file named by the instrumented translation unit. */ +static void open_coverage_file(const char *name, void *arg) +{ + const struct semihost_open_args args = { name, SEMIHOST_BINARY_WRITE, strlen(name) }; + long *file = arg; + + *file = semihost_call(SEMIHOST_OPEN, &args); + if (*file == -1L) + { + exit_test(1L, "Could not open coverage file\n"); + } +} + +/* Write a block of gcov data through the QEMU host. */ +static void write_coverage(const void *data, unsigned int size, void *arg) +{ + const long *file = arg; + const struct semihost_write_args args = { *file, data, size }; + + if (semihost_call(SEMIHOST_WRITE, &args) != 0L) + { + exit_test(1L, "Could not write coverage file\n"); + } +} + +/* Arc counters can be exported directly without allocation. */ +static void *allocate_coverage(unsigned int size, void *arg) +{ + (void) size; + (void) arg; + exit_test(1L, "Unexpected allocation during arc export\n"); + return NULL; +} + +/* Confirm that both cores scheduled a thread, then export coverage from core 0. */ +static void startup_thread(ULONG index) +{ + const UINT core = _tx_thread_smp_core_get(); + long file = -1L; + + (void) index; + wait_for_both_cores(core); + if (core == 0U) + { + __gcov_info_to_gcda(__gcov_info_start, open_coverage_file, write_coverage, + allocate_coverage, &file); + if (semihost_call(SEMIHOST_CLOSE, &file) != 0L) + { + exit_test(1L, "Could not close coverage file\n"); + } + exit_test(0L, "Both Cortex-A53 cores ran a ThreadX thread\n"); + } + for (;;) + { + } +} + +/* Provide one ready thread per core so the test reaches both schedulers. */ +void tx_application_define(void *unused) +{ + ULONG index; + + (void) unused; + for (index = 0U; index < 2U; index++) + { + if (tx_thread_create(&threads[index], startup_thread_name, startup_thread, index, + stacks[index], sizeof(stacks[index]), 1U, 1U, + TX_NO_TIME_SLICE, TX_AUTO_START) != TX_SUCCESS) + { + exit_test(1L, "Could not create a startup thread\n"); + } + } +} + +/* Start the second CPU before entering the normal ThreadX initialization. */ +void primary_start(void) +{ + if (start_secondary_core() != 0L) + { + exit_test(1L, "PSCI CPU_ON failed\n"); + } + tx_kernel_enter(); +}