summaryrefslogtreecommitdiff
path: root/test/tx/cmake/module_qemu/tests/test_module_lifecycle.c
blob: 8c241e79302ca17ec6310abe0d0e7b093346b49c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/***************************************************************************
 * 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: protected module lifecycle                */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

/* A user-mode, memory-protected module is loaded, granted the shared area,
   started, left to exercise the kernel through its dispatcher, stopped and
   unloaded -- twice, so that the second cycle shows the first gave back what
   it took.  The module must run unprivileged and must not fault.  */

#include "harness_checks.h"

#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[];

static TXM_MODULE_INSTANCE      test_module;


VOID  harness_test(VOID)
{
ULONG   cycle;
ULONG   available_before;

    harness_print("Module lifecycle: protected module, load/start/stop/unload twice\n");

    available_before = harness_module_pool_available();

    for (cycle = 1UL; cycle <= LIFECYCLE_CYCLES; cycle++)
    {
        harness_value("cycle", cycle);

        harness_shared_clear();
        MODULE_SHARED[MODULE_IN_CYCLE] = cycle;

        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("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);

        harness_check_services(cycle);
        harness_expect("module ran unprivileged",
                       MODULE_SHARED[MODULE_OUT_CONTROL] & CONTROL_NPRIV, CONTROL_NPRIV);
        harness_expect("no memory fault", harness_fault_count, 0UL);

        harness_expect("module stop", (ULONG) txm_module_manager_stop(&test_module), (ULONG) TX_SUCCESS);
        harness_expect("module unload", (ULONG) txm_module_manager_unload(&test_module), (ULONG) TX_SUCCESS);
        harness_expect("module memory returned", harness_module_pool_available(), available_before);
    }
}