diff --git a/.github/workflows/module_qemu.yml b/.github/workflows/module_qemu.yml index bc153dc39..dc90b7a06 100644 --- a/.github/workflows/module_qemu.yml +++ b/.github/workflows/module_qemu.yml @@ -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 @@ -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; @@ -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 @@ -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/**" @@ -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/**" @@ -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" diff --git a/test/tx/cmake/module_qemu/CMakeLists.txt b/test/tx/cmake/module_qemu/CMakeLists.txt index a69889af4..4f17345cb 100644 --- a/test/tx/cmake/module_qemu/CMakeLists.txt +++ b/test/tx/cmake/module_qemu/CMakeLists.txt @@ -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 @@ -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") @@ -54,8 +54,14 @@ 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}/../../../..) @@ -63,6 +69,21 @@ 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) @@ -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}) @@ -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 $ ${bin} + COMMAND ${CMAKE_OBJCOPY} -O binary ${MODULE_OBJCOPY_OPTIONS} $ ${bin} DEPENDS ${name}.elf COMMENT "Converting ${name} to a raw module image" VERBATIM) @@ -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. @@ -191,8 +210,7 @@ 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} @@ -200,14 +218,14 @@ function(module_qemu_add_test name test_source) 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() @@ -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 $) 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.") diff --git a/test/tx/cmake/module_qemu/run_qemu_test.py b/test/tx/cmake/module_qemu/run_qemu_test.py index b2533c5d2..46f56e0e7 100755 --- a/test/tx/cmake/module_qemu/run_qemu_test.py +++ b/test/tx/cmake/module_qemu/run_qemu_test.py @@ -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) @@ -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__": diff --git a/test/tx/cmake/module_qemu/src/cortex_a7_manager.ld b/test/tx/cmake/module_qemu/src/cortex_a7_manager.ld new file mode 100644 index 000000000..3d8d755f4 --- /dev/null +++ b/test/tx/cmake/module_qemu/src/cortex_a7_manager.ld @@ -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__ = .; +} diff --git a/test/tx/cmake/module_qemu/src/cortex_a7_startup.S b/test/tx/cmake/module_qemu/src/cortex_a7_startup.S new file mode 100644 index 000000000..63c42b40c --- /dev/null +++ b/test/tx/cmake/module_qemu/src/cortex_a7_startup.S @@ -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 diff --git a/test/tx/cmake/module_qemu/src/harness.c b/test/tx/cmake/module_qemu/src/harness.c index 1285de133..c594b867f 100644 --- a/test/tx/cmake/module_qemu/src/harness.c +++ b/test/tx/cmake/module_qemu/src/harness.c @@ -73,7 +73,11 @@ static ULONG harness_semihost(ULONG operation, ULONG parameter) register ULONG r0 __asm__("r0") = operation; register ULONG r1 __asm__("r1") = parameter; +#ifdef HARNESS_CORTEX_A7 + __asm__ volatile ("svc 0x123456" : "+r"(r0) : "r"(r1) : "memory"); +#else __asm__ volatile ("bkpt 0xab" : "+r"(r0) : "r"(r1) : "memory"); +#endif return(r0); } @@ -172,8 +176,13 @@ VOID harness_fault_notify(TX_THREAD *thread_ptr, TXM_MODULE_INSTANCE *module_in { harness_fault_thread = thread_ptr; harness_fault_module = module_instance; +#ifdef HARNESS_CORTEX_A7 + harness_fault_address = _txm_module_manager_memory_fault_info.txm_module_manager_memory_fault_info_dfar; + harness_fault_status = _txm_module_manager_memory_fault_info.txm_module_manager_memory_fault_info_dfsr; +#else harness_fault_address = _txm_module_manager_memory_fault_info.txm_module_manager_memory_fault_info_mmfar; harness_fault_status = _txm_module_manager_memory_fault_info.txm_module_manager_memory_fault_info_cfsr; +#endif harness_fault_count++; } @@ -191,8 +200,14 @@ ULONG index; UINT harness_shared_grant(TXM_MODULE_INSTANCE *module_instance) { +#ifdef HARNESS_CORTEX_A7 + /* Execute-never, user read/write, and normal non-cacheable memory. */ + return(txm_module_manager_external_memory_enable(module_instance, (VOID *) MODULE_SHARED_BASE, + MODULE_SHARED_SIZE, 0x39U)); +#else return(txm_module_manager_external_memory_enable(module_instance, (VOID *) MODULE_SHARED_BASE, MODULE_SHARED_SIZE, TXM_MODULE_MANAGER_SHARED_ATTRIBUTE_WRITE)); +#endif } @@ -232,6 +247,10 @@ static VOID harness_thread_entry(ULONG thread_input) harness_expect("txm_module_manager_memory_fault_notify", (ULONG) txm_module_manager_memory_fault_notify(harness_fault_notify), (ULONG) TX_SUCCESS); +#ifdef HARNESS_CORTEX_A7 + harness_expect("txm_module_manager_mm_initialize", + (ULONG) txm_module_manager_mm_initialize(), (ULONG) TX_SUCCESS); +#endif harness_test(); harness_finish(); diff --git a/test/tx/cmake/module_qemu/src/module_shared.h b/test/tx/cmake/module_qemu/src/module_shared.h index 58e763356..83f6f5e67 100644 --- a/test/tx/cmake/module_qemu/src/module_shared.h +++ b/test/tx/cmake/module_qemu/src/module_shared.h @@ -27,15 +27,19 @@ /* A module reports what it did through a small area of RAM that the manager grants it with txm_module_manager_external_memory_enable. The area is at a fixed address, outside everything the manager image links, so that both - images can name it without either knowing the other's layout. Its base and - size meet the Cortex-M MPU's rule that a region is a power of two in size - and aligned to that size. */ + images can name it without either knowing the other's layout. Its base and + size match the Cortex-A7 MMU page or the Cortex-M MPU region. */ #ifndef MODULE_SHARED_H #define MODULE_SHARED_H +#ifdef HARNESS_CORTEX_A7 +#define MODULE_SHARED_BASE ((ULONG) 0x40200000UL) +#define MODULE_SHARED_SIZE ((ULONG) 4096UL) +#else #define MODULE_SHARED_BASE ((ULONG) 0x20100000UL) #define MODULE_SHARED_SIZE ((ULONG) 256UL) +#endif #define MODULE_SHARED_WORDS (MODULE_SHARED_SIZE / ((ULONG) sizeof(ULONG))) #define MODULE_SHARED ((volatile ULONG *) MODULE_SHARED_BASE) @@ -58,7 +62,7 @@ #define MODULE_OUT_KERNEL_READ_TRY ((ULONG) 16UL) /* Kernel word read about to happen */ #define MODULE_OUT_KERNEL_READ_DONE ((ULONG) 17UL) /* Kernel word read completed */ #define MODULE_OUT_KERNEL_VALUE ((ULONG) 18UL) /* Value read from the kernel word */ -#define MODULE_OUT_CONTROL ((ULONG) 19UL) /* CONTROL as seen by the start thread */ +#define MODULE_OUT_CONTROL ((ULONG) 19UL) /* Mode as seen by the start thread */ #define MODULE_OUT_POINTER_STATUS ((ULONG) 20UL) /* First pointer API error */ #define MODULE_OUT_POINTER_THREADS ((ULONG) 21UL) /* Thread entries with intact context */ #define MODULE_OUT_POINTER_TIMERS ((ULONG) 22UL) /* Pointer callbacks with intact context */ diff --git a/test/tx/cmake/module_qemu/tests/module_services.c b/test/tx/cmake/module_qemu/tests/module_services.c index 63961a056..49fa4d8eb 100644 --- a/test/tx/cmake/module_qemu/tests/module_services.c +++ b/test/tx/cmake/module_qemu/tests/module_services.c @@ -38,7 +38,7 @@ 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 + Directive 4.3 (inline assembly to read CPU mode), and Rule 11.4 (the shared area and the kernel word are integer addresses supplied at run time). */ #define TXM_MODULE @@ -64,6 +64,20 @@ static ULONG callback_context; VOID module_start(ULONG id); +/* Report unprivileged mode in bit zero, as in Cortex-M CONTROL. */ +static ULONG module_control(VOID) +{ +ULONG control; + +#ifdef HARNESS_CORTEX_A7 + __asm__ volatile ("mrs %0, cpsr" : "=r"(control)); + control = ((control & 0x1fUL) == 0x10UL) ? 1UL : 0UL; +#else + __asm__ volatile ("mrs %0, control" : "=r"(control)); +#endif + return(control); +} + /* Keep the first failing status rather than the last. */ static VOID record_status(ULONG word, UINT status) @@ -78,13 +92,12 @@ static VOID record_status(ULONG word, UINT status) static VOID worker_entry(ULONG thread_input) { ULONG message; -ULONG control; - __asm__ volatile ("mrs %0, control" : "=r"(control)); message = 0UL; if (tx_queue_receive(message_queue, &message, WAIT_TICKS) == TX_SUCCESS) { - if ((thread_input == MODULE_QUEUE_MESSAGE) && (control == MODULE_SHARED[MODULE_OUT_CONTROL])) + /* Check the mode restored after the blocking kernel service. */ + if ((thread_input == MODULE_QUEUE_MESSAGE) && (module_control() == MODULE_SHARED[MODULE_OUT_CONTROL])) { MODULE_SHARED[MODULE_OUT_QUEUE_VALUE] = message; MODULE_SHARED[MODULE_OUT_WORKER_RUNS]++; @@ -109,7 +122,7 @@ static VOID pointer_timer_entry(VOID *context) { ULONG control; - __asm__ volatile ("mrs %0, control" : "=r"(control)); + control = module_control(); if ((context == &callback_context) && (control == MODULE_SHARED[MODULE_OUT_CONTROL])) { MODULE_SHARED[MODULE_OUT_POINTER_TIMERS]++; @@ -198,7 +211,7 @@ ULONG kernel_word; 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)); + control = module_control(); MODULE_SHARED[MODULE_OUT_CONTROL] = control; record_status(MODULE_OUT_OBJECT_STATUS, txm_module_object_allocate((VOID **) &message_queue, sizeof(TX_QUEUE))); diff --git a/test/tx/cmake/module_qemu/tests/test_module_lifecycle.c b/test/tx/cmake/module_qemu/tests/test_module_lifecycle.c index 130b7497d..ed419762c 100644 --- a/test/tx/cmake/module_qemu/tests/test_module_lifecycle.c +++ b/test/tx/cmake/module_qemu/tests/test_module_lifecycle.c @@ -33,7 +33,6 @@ #define LIFECYCLE_CYCLES ((ULONG) 2UL) #define MODULE_WAIT_TICKS ((ULONG) 500UL) -#define PROTECTED_PROPERTIES ((ULONG) 0x02000007UL) /* GNU; user mode, memory protection, shared memory */ extern UCHAR module_services_protected[]; @@ -109,7 +108,8 @@ ULONG available_before; harness_expect("memory load", (ULONG) txm_module_manager_memory_load(&test_module, "services protected", (VOID *) module_services_protected), (ULONG) TX_SUCCESS); - harness_expect("module properties", test_module.txm_module_instance_property_flags, PROTECTED_PROPERTIES); + harness_expect("module properties", test_module.txm_module_instance_property_flags, + (ULONG) HARNESS_PROTECTED_PROPERTIES); harness_expect("shared area grant", (ULONG) harness_shared_grant(&test_module), (ULONG) TX_SUCCESS); harness_expect("module start", (ULONG) txm_module_manager_start(&test_module), (ULONG) TX_SUCCESS); harness_expect("module finished", (ULONG) harness_wait_for_module(MODULE_WAIT_TICKS), (ULONG) TX_TRUE);