Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions .github/workflows/module_qemu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ name: module_qemu
# Portions of this file were generated with AI assistance.
#
# Builds the module execution harness in test/tx/cmake/module_qemu for each
# Cortex-M module port it covers and EXECUTES every image on QEMU's MPS2
# board for that core, asserting each image's self-reported result.
# Arm module port it covers and executes each image on the corresponding
# QEMU machine, asserting its self-reported result.
#
# Why this exists: until this workflow landed, no Cortex-M module port ran in
# CI. gcc_check compiles the module manager sources once per port and links
Expand All @@ -17,7 +17,7 @@ name: module_qemu
# misbehaving module are only exercised by executing them, and r52_fvp does
# that for the Cortex-R52 alone.
#
# What it covers, per port:
# What it covers:
#
# - cortex_m3 on mps2-an385 and cortex_m4 on mps2-an386, the latter built
# for its FPU with the hard-float ABI;
Expand All @@ -26,8 +26,10 @@ name: module_qemu
# - a protected module faulting on a read of kernel memory, reported to the
# application and terminated -- which is also the proof that QEMU's MPU is
# enforcing at all;
# - the same module built privileged and unprotected, whose identical read
# succeeds.
# - the same module built privileged and unprotected on Cortex-M, whose
# identical read succeeds;
# - the protected lifecycle on Cortex-A7 on virt with GICv2 and the
# architected timer.
#
# QEMU comes from the Ubuntu archive. The images report through semihosting
# and end the simulation themselves, so a hang shows up as the runner's
Expand All @@ -43,6 +45,7 @@ on:
- ".github/workflows/module_qemu.yml"
- "common/**"
- "common_modules/**"
- "ports_module/cortex_a7/**"
- "ports_module/cortex_m3/**"
- "ports_module/cortex_m4/**"
- "test/tx/cmake/module_qemu/**"
Expand All @@ -55,6 +58,7 @@ on:
- ".github/workflows/module_qemu.yml"
- "common/**"
- "common_modules/**"
- "ports_module/cortex_a7/**"
- "ports_module/cortex_m3/**"
- "ports_module/cortex_m4/**"
- "test/tx/cmake/module_qemu/**"
Expand Down Expand Up @@ -121,7 +125,7 @@ jobs:
arm-none-eabi-gcc --version | head -1

status=0
for port in cortex_m3 cortex_m4; do
for port in cortex_m3 cortex_m4 cortex_a7; do
for pointer_context in OFF ON; do
config="$port (pointer context $pointer_context)"
build="build/module_qemu_${port}_$pointer_context"
Expand Down
61 changes: 42 additions & 19 deletions test/tx/cmake/module_qemu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
# Module execution harness.
#
# Builds a module manager image per test, each embedding the modules it loads,
# for one Cortex-M module port, and runs every image on QEMU's MPS2 board for
# that core, asserting the result each image reports over semihosting.
# for one Arm module port, and runs every image on QEMU, asserting the result
# each image reports over semihosting.
#
# Why this exists: the host tests under test/tx/module_manager compile a few
# module manager sources against stubs, and nothing else in CI runs a Cortex-M
Expand All @@ -37,15 +37,15 @@
# cmake --build build/module_qemu_m3
# ctest --test-dir build/module_qemu_m3 --output-on-failure
#
# MODULE_QEMU_PORT is cortex_m3 (QEMU mps2-an385) or cortex_m4 (mps2-an386,
# built for its FPU with the hard-float ABI).
# MODULE_QEMU_PORT is cortex_m3 (mps2-an385), cortex_m4 (mps2-an386, hard-float),
# or cortex_a7 (virt with GICv2).

cmake_minimum_required(VERSION 3.20 FATAL_ERROR)

project(threadx_module_qemu LANGUAGES C ASM)

set(MODULE_QEMU_PORT "cortex_m3" CACHE STRING "Cortex-M module port to build and run")
set_property(CACHE MODULE_QEMU_PORT PROPERTY STRINGS cortex_m3 cortex_m4)
set(MODULE_QEMU_PORT "cortex_m3" CACHE STRING "Arm module port to build and run")
set_property(CACHE MODULE_QEMU_PORT PROPERTY STRINGS cortex_m3 cortex_m4 cortex_a7)
option(MODULE_QEMU_POINTER_CONTEXT "Exercise the pointer-context module APIs" OFF)

if(MODULE_QEMU_PORT STREQUAL "cortex_m3")
Expand All @@ -54,15 +54,36 @@ if(MODULE_QEMU_PORT STREQUAL "cortex_m3")
elseif(MODULE_QEMU_PORT STREQUAL "cortex_m4")
set(PORT_CPU_FLAGS -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard)
set(QEMU_MACHINE mps2-an386)
elseif(MODULE_QEMU_PORT STREQUAL "cortex_a7")
set(PORT_CPU_FLAGS -mcpu=cortex-a7 -marm -mfloat-abi=soft)
set(QEMU_MACHINE virt,gic-version=2)
set(QEMU_CPU_OPTION --cpu=cortex-a7)
# Reserve room for the unoptimized test's nested kernel calls.
add_compile_definitions(HARNESS_CORTEX_A7 TXM_MODULE_KERNEL_STACK_SIZE=1024)
else()
message(FATAL_ERROR "MODULE_QEMU_PORT must be cortex_m3 or cortex_m4, not '${MODULE_QEMU_PORT}'")
message(FATAL_ERROR "Unsupported MODULE_QEMU_PORT: '${MODULE_QEMU_PORT}'")
endif()

set(REPO_ROOT ${CMAKE_CURRENT_LIST_DIR}/../../../..)
set(PORT_DIR ${REPO_ROOT}/ports_module/${MODULE_QEMU_PORT}/gnu)
set(HARNESS_SRC ${CMAKE_CURRENT_LIST_DIR}/src)
set(HARNESS_TESTS ${CMAKE_CURRENT_LIST_DIR}/tests)

if(MODULE_QEMU_PORT STREQUAL "cortex_a7")
set(MANAGER_STARTUP ${HARNESS_SRC}/cortex_a7_startup.S)
set(MANAGER_LINKER ${HARNESS_SRC}/cortex_a7_manager.ld)
set(MODULE_SETUP ${PORT_DIR}/example_build/gcc_setup.S)
# The port's linker script leaves .dynamic below the module preamble.
set(MODULE_OBJCOPY_OPTIONS -R .dynamic)
set(PROTECTED_PROPERTIES 0x02000003)
else()
set(MANAGER_STARTUP ${HARNESS_SRC}/startup.S ${HARNESS_SRC}/tx_initialize_low_level.S)
set(MANAGER_LINKER ${HARNESS_SRC}/manager.ld)
set(MODULE_SETUP ${PORT_DIR}/example_build/gcc_setup.s)
set(PROTECTED_PROPERTIES 0x02000007)
endif()
add_compile_definitions(HARNESS_PROTECTED_PROPERTIES=${PROTECTED_PROPERTIES})

message(STATUS "Module port: ${MODULE_QEMU_PORT}, QEMU machine: ${QEMU_MACHINE}")

add_compile_options(${PORT_CPU_FLAGS} -g -O0 -ffunction-sections -fdata-sections)
Expand Down Expand Up @@ -133,7 +154,7 @@ target_compile_options(txm_module_lib PRIVATE -fpic ${MODULE_PIC_FLAGS})
function(module_qemu_add_module name source properties)
add_executable(${name}.elf
${HARNESS_SRC}/module_preamble.S
${PORT_DIR}/example_build/gcc_setup.s
${MODULE_SETUP}
${source})
target_include_directories(${name}.elf PRIVATE ${HARNESS_SRC})
target_compile_definitions(${name}.elf PRIVATE TEST_MODULE_PROPERTIES=${properties})
Expand All @@ -154,7 +175,7 @@ function(module_qemu_add_module name source properties)
set(bin ${CMAKE_CURRENT_BINARY_DIR}/${name}.bin)
add_custom_command(
OUTPUT ${bin}
COMMAND ${CMAKE_OBJCOPY} -O binary $<TARGET_FILE:${name}.elf> ${bin}
COMMAND ${CMAKE_OBJCOPY} -O binary ${MODULE_OBJCOPY_OPTIONS} $<TARGET_FILE:${name}.elf> ${bin}
DEPENDS ${name}.elf
COMMENT "Converting ${name} to a raw module image"
VERBATIM)
Expand All @@ -170,9 +191,7 @@ endfunction()

# GNU compiler ID in bits 31-24; bit 0 user mode, bit 1 memory protection,
# bit 2 shared memory access.
module_qemu_add_module(module_services_protected ${HARNESS_TESTS}/module_services.c 0x02000007)
module_qemu_add_module(module_services_privileged ${HARNESS_TESTS}/module_services.c 0x02000000)
module_qemu_add_module(module_stack_frame ${HARNESS_TESTS}/module_stack_frame.c 0x02000007)
module_qemu_add_module(module_services_protected ${HARNESS_TESTS}/module_services.c ${PROTECTED_PROPERTIES})

# ---------------------------------------------------------------------------
# The manager images, one per test, and the CTest entries that run them.
Expand All @@ -191,23 +210,22 @@ function(module_qemu_add_test name test_source)
endforeach()

add_executable(${name}.elf
${HARNESS_SRC}/startup.S
${HARNESS_SRC}/tx_initialize_low_level.S
${MANAGER_STARTUP}
${HARNESS_SRC}/harness.c
${HARNESS_TESTS}/harness_checks.c
${test_source}
${blob_sources})
target_include_directories(${name}.elf PRIVATE ${HARNESS_SRC} ${HARNESS_TESTS})
target_link_libraries(${name}.elf PRIVATE threadx_module)
target_link_options(${name}.elf PRIVATE
-T${HARNESS_SRC}/manager.ld
-T${MANAGER_LINKER}
-nostartfiles
--specs=nano.specs
--specs=nosys.specs
-Wl,--gc-sections
-Wl,--no-warn-rwx-segments
-Wl,-Map=${name}.map)
set_target_properties(${name}.elf PROPERTIES LINK_DEPENDS ${HARNESS_SRC}/manager.ld)
set_target_properties(${name}.elf PROPERTIES LINK_DEPENDS ${MANAGER_LINKER})
foreach(blob IN LISTS blobs)
add_dependencies(${name}.elf ${blob}_bin)
endforeach()
Expand All @@ -217,14 +235,19 @@ function(module_qemu_add_test name test_source)
COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_LIST_DIR}/run_qemu_test.py
--qemu ${QEMU_SYSTEM_ARM}
--machine ${QEMU_MACHINE}
${QEMU_CPU_OPTION}
--elf $<TARGET_FILE:${name}.elf>)
endif()
endfunction()

module_qemu_add_test(test_module_lifecycle ${HARNESS_TESTS}/test_module_lifecycle.c module_services_protected)
module_qemu_add_test(test_module_fault ${HARNESS_TESTS}/test_module_fault.c module_services_protected)
module_qemu_add_test(test_module_privileged ${HARNESS_TESTS}/test_module_privileged.c module_services_privileged)
module_qemu_add_test(test_module_stack_frame ${HARNESS_TESTS}/test_module_stack_frame.c module_stack_frame)
if(NOT MODULE_QEMU_PORT STREQUAL "cortex_a7")
module_qemu_add_module(module_services_privileged ${HARNESS_TESTS}/module_services.c 0x02000000)
module_qemu_add_module(module_stack_frame ${HARNESS_TESTS}/module_stack_frame.c ${PROTECTED_PROPERTIES})
module_qemu_add_test(test_module_fault ${HARNESS_TESTS}/test_module_fault.c module_services_protected)
module_qemu_add_test(test_module_privileged ${HARNESS_TESTS}/test_module_privileged.c module_services_privileged)
module_qemu_add_test(test_module_stack_frame ${HARNESS_TESTS}/test_module_stack_frame.c module_stack_frame)
endif()

if(NOT (Python3_FOUND AND QEMU_SYSTEM_ARM))
message(STATUS "qemu-system-arm or Python 3 not found; the images build but no tests are registered.")
Expand Down
8 changes: 6 additions & 2 deletions test/tx/cmake/module_qemu/run_qemu_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,19 @@ def as_text(stream):
return stream


def run(qemu, machine, elf, timeout):
def run(qemu, machine, elf, timeout, cpu=None):
command = [
qemu,
"-M", machine,
"-nodefaults",
"-nographic",
"-monitor", "none",
"-serial", "none",
"-semihosting-config", "enable=on,target=native",
"-kernel", elf,
]
if cpu is not None:
command.extend(["-cpu", cpu])

print("Running:", " ".join(command), flush=True)

Expand Down Expand Up @@ -95,10 +98,11 @@ def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--qemu", required=True, help="qemu-system-arm executable")
parser.add_argument("--machine", required=True, help="QEMU machine, e.g. mps2-an385")
parser.add_argument("--cpu", help="QEMU CPU when the board has no fixed model")
parser.add_argument("--elf", required=True, help="image to run")
parser.add_argument("--timeout", type=int, default=60, help="seconds before a hang is declared")
args = parser.parse_args()
return run(args.qemu, args.machine, args.elf, args.timeout)
return run(args.qemu, args.machine, args.elf, args.timeout, args.cpu)


if __name__ == "__main__":
Expand Down
39 changes: 39 additions & 0 deletions test/tx/cmake/module_qemu/src/cortex_a7_manager.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* Copyright (c) 2026 Eclipse ThreadX contributors
* SPDX-License-Identifier: MIT
*/

/* QEMU loads the manager ELF directly into virt RAM. The shared module page
starts just beyond this region at 0x40200000. */
MEMORY
{
RAM (rwx) : ORIGIN = 0x40010000, LENGTH = 0x001f0000
}

ENTRY(reset_handler)

SECTIONS
{
.vectors : { KEEP(*(.vectors)) } > RAM
.text : { *(.text .text.*) *(.rodata .rodata.*) } > RAM
.ARM.exidx : { *(.ARM.exidx*) } > RAM
.data : { *(.data .data.*) } > RAM

.bss (NOLOAD) : ALIGN(4)
{
__bss_start__ = .;
*(.bss .bss.*)
*(COMMON)
. = ALIGN(4);
__bss_end__ = .;
} > RAM

.stack (NOLOAD) : ALIGN(8)
{
. = . + 4096;
__stack_end__ = .;
. = . + 2048;
__irq_stack_end__ = .;
} > RAM

__RAM_segment_used_end__ = .;
}
118 changes: 118 additions & 0 deletions test/tx/cmake/module_qemu/src/cortex_a7_startup.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/***************************************************************************
* 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
**************************************************************************/

.syntax unified
.arm

GIC_DIST = 0x08000000
GIC_CPU = 0x08010000
GIC_CTLR = 0x000
GICD_ISENABLER0 = 0x100
GICC_PMR = 0x004
GICC_IAR = 0x00c
GICC_EOIR = 0x010
TIMER_INTID = 30
TICKS_PER_SECOND = 100
SVC_MODE = 0x13
IRQ_MODE = 0x12

.section .vectors, "ax"
.balign 32
vectors:
b reset_handler
b unexpected_exception
b __tx_swi_interrupt
b unexpected_exception
b unexpected_exception
b unexpected_exception
b _tx_thread_context_save
b unexpected_exception

.text
.balign 4

/* Enter ThreadX with aligned SVC and IRQ stacks, vectors, and cleared BSS. */
.global reset_handler
.type reset_handler, %function
reset_handler:
cpsid if, #IRQ_MODE
ldr sp, =__irq_stack_end__
cps #SVC_MODE
ldr sp, =__stack_end__
ldr r0, =vectors
mcr p15, 0, r0, c12, c0, 0 @ VBAR
isb
ldr r0, =__bss_start__
ldr r1, =__bss_end__
mov r2, #0
1:
cmp r0, r1
strlo r2, [r0], #4
blo 1b
bl main

/* Report a failure from an exception mode with no C stack. */
unexpected_exception:
cpsid if, #SVC_MODE
b harness_unexpected_exception

/* Enable the physical timer PPI on the single test core. */
.global _tx_initialize_low_level
.type _tx_initialize_low_level, %function
_tx_initialize_low_level:
ldr r0, =_tx_initialize_unused_memory
ldr r1, =__RAM_segment_used_end__
str r1, [r0]
ldr r0, =_tx_thread_system_stack_ptr
ldr r1, =__irq_stack_end__
str r1, [r0]

ldr r0, =GIC_DIST
mov r1, #(1 << TIMER_INTID)
str r1, [r0, #GICD_ISENABLER0]
mov r1, #1
str r1, [r0, #GIC_CTLR]
ldr r0, =GIC_CPU
mov r2, #0xff
str r2, [r0, #GICC_PMR]
str r1, [r0, #GIC_CTLR]
mcr p15, 0, r1, c14, c2, 1 @ CNTP_CTL
b reload_timer

/* Rearm the level-triggered timer before acknowledging end of interrupt. */
.global __tx_irq_processing_return
.type __tx_irq_processing_return, %function
__tx_irq_processing_return:
push {r4, r5}
ldr r5, =GIC_CPU
ldr r4, [r5, #GICC_IAR]
ubfx r0, r4, #0, #10
cmp r0, #TIMER_INTID
bne unexpected_exception
bl reload_timer
dsb sy
str r4, [r5, #GICC_EOIR]
bl _tx_timer_interrupt
pop {r4, r5}
b _tx_thread_context_restore

/* Set the next 100 Hz tick relative to the current timer count. */
reload_timer:
mrc p15, 0, r0, c14, c0, 0 @ CNTFRQ
mov r1, #TICKS_PER_SECOND
udiv r0, r0, r1
mcr p15, 0, r0, c14, c2, 0 @ CNTP_TVAL
isb
bx lr
Loading