1/***************************************************************************
2 * Copyright (c) 2024 Microsoft Corporation
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the MIT License which is available at
6 * https://opensource.org/licenses/MIT.
7 *
8 * SPDX-License-Identifier: MIT
9 **************************************************************************/
10
11
12/**************************************************************************/
13/**************************************************************************/
14/**                                                                       */
15/** ThreadX Component                                                     */
16/**                                                                       */
17/**   Thread                                                              */
18/**                                                                       */
19/**************************************************************************/
20/**************************************************************************/
21
22#ifdef TX_INCLUDE_USER_DEFINE_FILE
23#include "tx_user.h"
24#endif
25
26/**************************************************************************/
27/*                                                                        */
28/*  FUNCTION                                               RELEASE        */
29/*                                                                        */
30/*    _tx_thread_stack_build                            Cortex-Mxx/GNU    */
31/*                                                           6.2.1        */
32/*  AUTHOR                                                                */
33/*                                                                        */
34/*    Scott Larson, Microsoft Corporation                                 */
35/*                                                                        */
36/*  DESCRIPTION                                                           */
37/*                                                                        */
38/*    This function builds a stack frame on the supplied thread's stack.  */
39/*    The stack frame results in a fake interrupt return to the supplied  */
40/*    function pointer.                                                   */
41/*                                                                        */
42/*  INPUT                                                                 */
43/*                                                                        */
44/*    thread_ptr                            Pointer to thread control blk */
45/*    function_ptr                          Pointer to return function    */
46/*                                                                        */
47/*  OUTPUT                                                                */
48/*                                                                        */
49/*    None                                                                */
50/*                                                                        */
51/*  CALLS                                                                 */
52/*                                                                        */
53/*    None                                                                */
54/*                                                                        */
55/*  CALLED BY                                                             */
56/*                                                                        */
57/*    _tx_thread_create                     Create thread service         */
58/*                                                                        */
59/*  RELEASE HISTORY                                                       */
60/*                                                                        */
61/*    DATE              NAME                      DESCRIPTION             */
62/*                                                                        */
63/*  09-30-2020      Scott Larson            Initial Version 6.1           */
64/*  03-08-2023      Scott Larson            Include tx_user.h,            */
65/*                                            resulting in version 6.2.1  */
66/*                                                                        */
67/**************************************************************************/
68// VOID   _tx_thread_stack_build(TX_THREAD *thread_ptr, VOID (*function_ptr)(VOID))
69// {
70    .section .text
71    .balign 4
72    .syntax unified
73    .eabi_attribute Tag_ABI_align_preserved, 1
74    .global  _tx_thread_stack_build
75    .thumb_func
76.type _tx_thread_stack_build, function
77_tx_thread_stack_build:
78    /* Build a fake interrupt frame.  The form of the fake interrupt stack
79       on the Cortex-M should look like the following after it is built:
80
81       Stack Top:
82                       LR          Interrupted LR (LR at time of PENDSV)
83                       r4          Initial value for r4
84                       r5          Initial value for r5
85                       r6          Initial value for r6
86                       r7          Initial value for r7
87                       r8          Initial value for r8
88                       r9          Initial value for r9
89                       r10         Initial value for r10
90                       r11         Initial value for r11
91                       r0          Initial value for r0    (Hardware stack starts here!!)
92                       r1          Initial value for r1
93                       r2          Initial value for r2
94                       r3          Initial value for r3
95                       r12         Initial value for r12
96                       lr          Initial value for lr
97                       pc          Initial value for pc
98                       xPSR        Initial value for xPSR
99
100    Stack Bottom: (higher memory address)  */
101
102    LDR     r2, [r0, #16]                           // Pickup end of stack area
103    BIC     r2, r2, #0x7                            // Align frame for 8-byte alignment
104    SUB     r2, r2, #68                             // Subtract frame size
105#ifdef TX_SINGLE_MODE_SECURE
106    LDR     r3, =0xFFFFFFFD                         // Build initial LR value for secure mode
107#else
108    LDR     r3, =0xFFFFFFBC                         // Build initial LR value to return to non-secure PSP
109#endif
110    STR     r3, [r2, #0]                            // Save on the stack
111
112    /* Actually build the stack frame.  */
113
114    MOV     r3, #0                                  // Build initial register value
115    STR     r3, [r2, #4]                            // Store initial r4
116    STR     r3, [r2, #8]                            // Store initial r5
117    STR     r3, [r2, #12]                           // Store initial r6
118    STR     r3, [r2, #16]                           // Store initial r7
119    STR     r3, [r2, #20]                           // Store initial r8
120    STR     r3, [r2, #24]                           // Store initial r9
121    STR     r3, [r2, #28]                           // Store initial r10
122    STR     r3, [r2, #32]                           // Store initial r11
123
124    /* Hardware stack follows.  */
125
126    STR     r3, [r2, #36]                           // Store initial r0
127    STR     r3, [r2, #40]                           // Store initial r1
128    STR     r3, [r2, #44]                           // Store initial r2
129    STR     r3, [r2, #48]                           // Store initial r3
130    STR     r3, [r2, #52]                           // Store initial r12
131    MOV     r3, #0xFFFFFFFF                         // Poison EXC_RETURN value
132    STR     r3, [r2, #56]                           // Store initial lr
133    STR     r1, [r2, #60]                           // Store initial pc
134    MOV     r3, #0x01000000                         // Only T-bit need be set
135    STR     r3, [r2, #64]                           // Store initial xPSR
136
137    /* Setup stack pointer.  */
138    // thread_ptr -> tx_thread_stack_ptr =  r2;
139
140    STR     r2, [r0, #8]                            // Save stack pointer in thread's
141                                                    //   control block
142    BX      lr                                      // Return to caller
143// }
144    .end
145