summaryrefslogtreecommitdiff
path: root/test/tx/cmake/riscv/regression/threadx_riscv_new_thread_fpu_state_test.c
blob: b388ad62130ebd8bae352b39670254973370b283 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/***************************************************************************/
/* 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 Opus 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                                */
/***************************************************************************/

/* This test is designed to verify that a thread created after another
   thread has used the floating point registers starts with a clean
   floating point state.

   The interrupt stack frame reserves slot 29 for mstatus, and the context
   restore path uses the FS field of that slot to decide whether the saved
   floating point registers have to be recovered.  When the stack builder
   leaves the slot untouched, the decision is made on whatever the thread
   stack happened to contain, so the new thread can silently inherit the
   floating point registers of the thread that ran before it.

   Thread 0 fills every floating point register with a non-zero value and
   then creates thread 1.  The test checks that the stack builder wrote the
   mstatus slot at all, that the slot describes a live floating point state,
   and that thread 1 observes all floating point registers as zero once it
   preempts thread 0.

   An unwritten slot still holds the stack fill pattern, so it is detected
   by comparing the slot against a stack word the builder never touches.  */

#include   <stdio.h>
#include   "tx_api.h"


/* The mstatus slot of the interrupt stack frame.  */

#define FRAME_MSTATUS_INDEX     29


#if defined(__riscv_float_abi_double)

#define FP_SUPPORTED            1
#define FP_LOAD                 "fld"
#define FP_STORE                "fsd"
#define FP_WORD_SIZE            "8"
typedef double                  FP_WORD;

#elif defined(__riscv_float_abi_single)

#define FP_SUPPORTED            1
#define FP_LOAD                 "flw"
#define FP_STORE                "fsw"
#define FP_WORD_SIZE            "4"
typedef float                   FP_WORD;

#else

#define FP_SUPPORTED            0

#endif


#if FP_SUPPORTED

/* Walk f0-f31 in register number order so that the index is both the
   register number and the slot in the state buffers.  */

#define FP_REG_LIST(op)                                                       \
    op(ft0,   0) op(ft1,   1) op(ft2,   2) op(ft3,   3)                       \
    op(ft4,   4) op(ft5,   5) op(ft6,   6) op(ft7,   7)                       \
    op(fs0,   8) op(fs1,   9) op(fa0,  10) op(fa1,  11)                       \
    op(fa2,  12) op(fa3,  13) op(fa4,  14) op(fa5,  15)                       \
    op(fa6,  16) op(fa7,  17) op(fs2,  18) op(fs3,  19)                       \
    op(fs4,  20) op(fs5,  21) op(fs6,  22) op(fs7,  23)                       \
    op(fs8,  24) op(fs9,  25) op(fs10, 26) op(fs11, 27)                       \
    op(ft8,  28) op(ft9,  29) op(ft10, 30) op(ft11, 31)

#define FP_LOAD_ONE(reg, index)     FP_LOAD  " " #reg ", " #index "*" FP_WORD_SIZE "(%0)\n"
#define FP_STORE_ONE(reg, index)    FP_STORE " " #reg ", " #index "*" FP_WORD_SIZE "(%0)\n"
#define FP_CLOBBER_ONE(reg, index)  #reg,

#define FP_REGISTERS            32

static FP_WORD          dirty_pattern[FP_REGISTERS];
static FP_WORD          thread_1_fp_state[FP_REGISTERS];
static unsigned long    thread_1_fp_dirty =  0;

#endif


static TX_THREAD        thread_0;
static TX_THREAD        thread_1;

/* Give thread 1 a private stack so it can be cleared before the thread is
   created.  */

static unsigned long    thread_1_stack[TEST_STACK_SIZE_PRINTF/sizeof(unsigned long)];

static unsigned long    thread_1_executed =  0;


/* Define thread prototypes.  */

static void    thread_0_entry(ULONG thread_input);
static void    thread_1_entry(ULONG thread_input);


/* Prototype for test control return.  */
void  test_control_return(UINT status);


/* Define what the initial system looks like.  */

#ifdef CTEST
void test_application_define(void *first_unused_memory)
#else
void    threadx_riscv_new_thread_fpu_state_application_define(void *first_unused_memory)
#endif
{

UINT    status;
CHAR    *pointer;


    /* Put first available memory address into a character pointer.  */
    pointer =  (CHAR *) first_unused_memory;

    status =  tx_thread_create(&thread_0, "thread 0", thread_0_entry, 0,
            pointer, TEST_STACK_SIZE_PRINTF,
            16, 16, TX_NO_TIME_SLICE, TX_AUTO_START);

    /* Check for status.  */
    if (status != TX_SUCCESS)
    {

        printf("Running RISC-V New Thread FPU State Test............................ ERROR #1\n");
        test_control_return(1);
    }
}



/* Define the test threads.  */

static void    thread_0_entry(ULONG thread_input)
{

UINT            status;
unsigned long   *frame;
unsigned long   stack_fill;
unsigned long   frame_fs;
#if FP_SUPPORTED
UINT            i;
#endif


    /* Inform user.  */
    printf("Running RISC-V New Thread FPU State Test............................ ");

#if FP_SUPPORTED

    /* Build a pattern that is non-zero in every register.  */
    for (i = 0; i < FP_REGISTERS; i++)
    {

        dirty_pattern[i] =  (FP_WORD) (i + 1);
    }

    /* Make the floating point state of this thread dirty.  */
    __asm__ volatile (FP_REG_LIST(FP_LOAD_ONE)
                      :
                      : "r" (dirty_pattern)
                      : FP_REG_LIST(FP_CLOBBER_ONE) "memory");
#endif

    /* Create thread 1 without starting it, so the built frame can be
       inspected before the thread is scheduled.  */
    status =  tx_thread_create(&thread_1, "thread 1", thread_1_entry, 0,
            thread_1_stack, sizeof(thread_1_stack),
            15, 15, TX_NO_TIME_SLICE, TX_DONT_START);

    /* Check for status.  */
    if (status != TX_SUCCESS)
    {

        printf("ERROR #2\n");
        test_control_return(1);
    }

    /* The bottom of the stack is never part of the frame, so it still holds
       the fill pattern that thread creation left behind.  */
    frame =       (unsigned long *) thread_1.tx_thread_stack_ptr;
    stack_fill =  thread_1_stack[0];

    /* The stack builder must have written the mstatus slot.  */
    if (frame[FRAME_MSTATUS_INDEX] == stack_fill)
    {

        printf("ERROR #3\n");
        test_control_return(1);
    }

    /* The built frame must describe a live floating point state.  */
    frame_fs =  (frame[FRAME_MSTATUS_INDEX] >> 13) & 0x3UL;

    if (frame_fs == 0)
    {

        printf("ERROR #4\n");
        test_control_return(1);
    }

    /* Let thread 1 preempt and run to completion.  */
    status =  tx_thread_resume(&thread_1);

    /* Check for status.  */
    if ((status != TX_SUCCESS) || (thread_1_executed != 1))
    {

        printf("ERROR #5\n");
        test_control_return(1);
    }

#if FP_SUPPORTED

    /* Thread 1 must not have inherited the floating point registers of
       thread 0.  */
    if (thread_1_fp_dirty)
    {

        printf("ERROR #6\n");
        test_control_return(1);
    }
#endif

    /* Successful test.  */
    printf("SUCCESS!\n");
    test_control_return(0);
}


static void    thread_1_entry(ULONG thread_input)
{

#if FP_SUPPORTED

const unsigned char     *state_bytes;
UINT                    i;


    /* Capture the floating point registers before anything else can use
       them.  */
    __asm__ volatile (FP_REG_LIST(FP_STORE_ONE)
                      :
                      : "r" (thread_1_fp_state)
                      : "memory");

    state_bytes =  (const unsigned char *) thread_1_fp_state;

    for (i = 0; i < (UINT) sizeof(thread_1_fp_state); i++)
    {

        if (state_bytes[i] != 0)
        {

            thread_1_fp_dirty =  1;
            break;
        }
    }
#endif

    thread_1_executed =  1;
}