1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** ThreadX Component                                                     */
17 /**                                                                       */
18 /**   Initialize                                                          */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 #define TX_SOURCE_CODE
24 #define TX_THREAD_SMP_SOURCE_CODE
25 
26 
27 /* Include necessary system files.  */
28 
29 #include "tx_api.h"
30 #include "tx_initialize.h"
31 #include "tx_thread.h"
32 #include "tx_timer.h"
33 
34 
35 /* Define any port-specific scheduling data structures.  */
36 
37 TX_PORT_SPECIFIC_DATA
38 
39 
40 #ifdef TX_SAFETY_CRITICAL
41 TX_SAFETY_CRITICAL_EXCEPTION_HANDLER
42 #endif
43 
44 
45 /**************************************************************************/
46 /*                                                                        */
47 /*  FUNCTION                                               RELEASE        */
48 /*                                                                        */
49 /*    _tx_initialize_kernel_enter                        PORTABLE SMP     */
50 /*                                                           6.3.0        */
51 /*  AUTHOR                                                                */
52 /*                                                                        */
53 /*    William E. Lamie, Microsoft Corporation                             */
54 /*                                                                        */
55 /*  DESCRIPTION                                                           */
56 /*                                                                        */
57 /*    This function is the first ThreadX function called during           */
58 /*    initialization.  It is called from the application's "main()"       */
59 /*    function.  It is important to note that this routine never          */
60 /*    returns.  The processing of this function is relatively simple:     */
61 /*    it calls several ThreadX initialization functions (if needed),      */
62 /*    calls the application define function, and then invokes the         */
63 /*    scheduler.                                                          */
64 /*                                                                        */
65 /*  INPUT                                                                 */
66 /*                                                                        */
67 /*    None                                                                */
68 /*                                                                        */
69 /*  OUTPUT                                                                */
70 /*                                                                        */
71 /*    None                                                                */
72 /*                                                                        */
73 /*  CALLS                                                                 */
74 /*                                                                        */
75 /*    _tx_thread_smp_high_level_initialize  SMP initialization            */
76 /*    _tx_thread_smp_current_state_set  Set system state for all cores    */
77 /*    _tx_initialize_low_level          Low-level initialization          */
78 /*    _tx_initialize_high_level         High-level initialization         */
79 /*    tx_application_define             Application define function       */
80 /*    _tx_thread_scheduler              ThreadX scheduling loop           */
81 /*                                                                        */
82 /*  CALLED BY                                                             */
83 /*                                                                        */
84 /*    main                              Application main program          */
85 /*                                                                        */
86 /*  RELEASE HISTORY                                                       */
87 /*                                                                        */
88 /*    DATE              NAME                      DESCRIPTION             */
89 /*                                                                        */
90 /*  09-30-2020      William E. Lamie        Initial Version 6.1           */
91 /*  10-31-2023      Xiuwen Cai              Modified comment(s),          */
92 /*                                            added random generator      */
93 /*                                            initialization,             */
94 /*                                            resulting in version 6.3.0  */
95 /*                                                                        */
96 /**************************************************************************/
_tx_initialize_kernel_enter(VOID)97 VOID  _tx_initialize_kernel_enter(VOID)
98 {
99 
100 ULONG   other_core_status, i;
101 
102 
103     /* Determine if the compiler has pre-initialized ThreadX.  */
104     if (_tx_thread_system_state[0] != TX_INITIALIZE_ALMOST_DONE)
105     {
106 
107         /* No, the initialization still needs to take place.  */
108 
109         /* Ensure that the system state variable is set to indicate
110            initialization is in progress.  Note that this variable is
111            later used to represent interrupt nesting.  */
112         _tx_thread_smp_current_state_set(TX_INITIALIZE_IN_PROGRESS);
113 
114         /* Call any port specific preprocessing.  */
115         TX_PORT_SPECIFIC_PRE_INITIALIZATION
116 
117         /* Invoke the low-level initialization to handle all processor specific
118            initialization issues.  */
119         _tx_initialize_low_level();
120 
121         /* Call the high-level SMP  Initialization.  */
122         _tx_thread_smp_high_level_initialize();
123 
124         /* Invoke the high-level initialization to exercise all of the
125            ThreadX components and the application's initialization
126            function.  */
127         _tx_initialize_high_level();
128 
129         /* Call any port specific post-processing.  */
130         TX_PORT_SPECIFIC_POST_INITIALIZATION
131     }
132 
133     /* Optional processing extension.  */
134     TX_INITIALIZE_KERNEL_ENTER_EXTENSION
135 
136     /* Ensure that the system state variable is set to indicate
137        initialization is in progress.  Note that this variable is
138        later used to represent interrupt nesting.  */
139     _tx_thread_system_state[0] =  TX_INITIALIZE_IN_PROGRESS;
140 
141     /* Optional random number generator initialization.  */
142     TX_INITIALIZE_RANDOM_GENERATOR_INITIALIZATION
143 
144     /* Call the application provided initialization function.  Pass the
145        first available memory address to it.  */
146     tx_application_define(_tx_initialize_unused_memory);
147 
148     /* Call any port specific pre-scheduler processing.  */
149     TX_PORT_SPECIFIC_PRE_SCHEDULER_INITIALIZATION
150 
151     /* Now wait for all cores to become ready for scheduling.  */
152     do
153     {
154 
155         /* Release the other cores from initialization.  */
156         _tx_thread_smp_release_cores_flag =  TX_TRUE;
157 
158         /* Add all the status together...  Other cores must clear their system
159            state before they they are released.  */
160         other_core_status =  ((ULONG) 0);
161 
162 #ifndef TX_THREAD_SMP_DYNAMIC_CORE_MAX
163         for (i = ((ULONG) 1); i < ((ULONG) TX_THREAD_SMP_MAX_CORES); i++)
164 #else
165         for (i = ((ULONG) 1); i < _tx_thread_smp_max_cores; i++)
166 #endif
167         {
168 
169 
170             /* Call port-specific memory synchronization primitive.  */
171             TX_PORT_SPECIFIC_MEMORY_SYNCHRONIZATION
172 
173             /* Add the states of each subsequent core.  */
174             other_core_status =  other_core_status + _tx_thread_system_state[i];
175         }
176 
177     } while (other_core_status != ((ULONG) 0));
178 
179     /* Set the system state in preparation for entering the thread
180        scheduler.  */
181     _tx_thread_system_state[0] =  TX_INITIALIZE_IS_FINISHED;
182 
183     /* Call port-specific memory synchronization primitive.  */
184     TX_PORT_SPECIFIC_MEMORY_SYNCHRONIZATION
185 
186     /* Enter the scheduling loop to start executing threads!  */
187     _tx_thread_schedule();
188 
189 #ifdef TX_SAFETY_CRITICAL
190 
191     /* If we ever get here, raise safety critical exception.  */
192     TX_SAFETY_CRITICAL_EXCEPTION(__FILE__, __LINE__, 0);
193 #endif
194 }
195 
196