1 | /* |
---|
2 | * Thread Handler |
---|
3 | * |
---|
4 | * |
---|
5 | * COPYRIGHT (c) 1989-2011. |
---|
6 | * On-Line Applications Research Corporation (OAR). |
---|
7 | * |
---|
8 | * The license and distribution terms for this file may be |
---|
9 | * found in the file LICENSE in this distribution or at |
---|
10 | * http://www.rtems.com/license/LICENSE. |
---|
11 | * |
---|
12 | * $Id$ |
---|
13 | */ |
---|
14 | |
---|
15 | #if HAVE_CONFIG_H |
---|
16 | #include "config.h" |
---|
17 | #endif |
---|
18 | |
---|
19 | #include <rtems/system.h> |
---|
20 | #include <rtems/score/apiext.h> |
---|
21 | #include <rtems/score/context.h> |
---|
22 | #include <rtems/score/interr.h> |
---|
23 | #include <rtems/score/isr.h> |
---|
24 | #include <rtems/score/object.h> |
---|
25 | #include <rtems/score/priority.h> |
---|
26 | #include <rtems/score/states.h> |
---|
27 | #include <rtems/score/sysstate.h> |
---|
28 | #include <rtems/score/thread.h> |
---|
29 | #include <rtems/score/threadq.h> |
---|
30 | #include <rtems/score/userext.h> |
---|
31 | #include <rtems/score/wkspace.h> |
---|
32 | #if defined(RTEMS_SMP) |
---|
33 | #include <rtems/score/smp.h> |
---|
34 | #endif |
---|
35 | |
---|
36 | |
---|
37 | /* |
---|
38 | * Conditional magic to determine what style of C++ constructor |
---|
39 | * initialization this target and compiler version uses. |
---|
40 | */ |
---|
41 | #if defined(__AVR__) |
---|
42 | #undef __USE_INIT_FINI__ |
---|
43 | #endif |
---|
44 | |
---|
45 | #if defined(__USE_INIT_FINI__) |
---|
46 | #if defined(__M32R__) |
---|
47 | #define INIT_NAME __init |
---|
48 | #elif defined(__ARM_EABI__) |
---|
49 | #define INIT_NAME __libc_init_array |
---|
50 | #else |
---|
51 | #define INIT_NAME _init |
---|
52 | #endif |
---|
53 | |
---|
54 | extern void INIT_NAME(void); |
---|
55 | #define EXECUTE_GLOBAL_CONSTRUCTORS |
---|
56 | #endif |
---|
57 | |
---|
58 | #if defined(__USE__MAIN__) |
---|
59 | extern void _main(void); |
---|
60 | #define INIT_NAME __main |
---|
61 | #define EXECUTE_GLOBAL_CONSTRUCTORS |
---|
62 | #endif |
---|
63 | |
---|
64 | /* |
---|
65 | * _Thread_Handler |
---|
66 | * |
---|
67 | * This routine is the "primal" entry point for all threads. |
---|
68 | * _Context_Initialize() dummies up the thread's initial context |
---|
69 | * to cause the first Context_Switch() to jump to _Thread_Handler(). |
---|
70 | * |
---|
71 | * This routine is the default thread exitted error handler. It is |
---|
72 | * returned to when a thread exits. The configured fatal error handler |
---|
73 | * is invoked to process the exit. |
---|
74 | * |
---|
75 | * NOTE: |
---|
76 | * |
---|
77 | * On entry, it is assumed all interrupts are blocked and that this |
---|
78 | * routine needs to set the initial isr level. This may or may not |
---|
79 | * actually be needed by the context switch routine and as a result |
---|
80 | * interrupts may already be at there proper level. Either way, |
---|
81 | * setting the initial isr level properly here is safe. |
---|
82 | * |
---|
83 | * Input parameters: NONE |
---|
84 | * |
---|
85 | * Output parameters: NONE |
---|
86 | */ |
---|
87 | void _Thread_Handler( void ) |
---|
88 | { |
---|
89 | ISR_Level level; |
---|
90 | Thread_Control *executing; |
---|
91 | #if defined(EXECUTE_GLOBAL_CONSTRUCTORS) |
---|
92 | static char doneConstructors; |
---|
93 | char doneCons; |
---|
94 | #endif |
---|
95 | |
---|
96 | executing = _Thread_Executing; |
---|
97 | |
---|
98 | /* |
---|
99 | * Some CPUs need to tinker with the call frame or registers when the |
---|
100 | * thread actually begins to execute for the first time. This is a |
---|
101 | * hook point where the port gets a shot at doing whatever it requires. |
---|
102 | */ |
---|
103 | _Context_Initialization_at_thread_begin(); |
---|
104 | |
---|
105 | /* |
---|
106 | * have to put level into a register for those cpu's that use |
---|
107 | * inline asm here |
---|
108 | */ |
---|
109 | level = executing->Start.isr_level; |
---|
110 | _ISR_Set_level(level); |
---|
111 | |
---|
112 | #if defined(EXECUTE_GLOBAL_CONSTRUCTORS) |
---|
113 | doneCons = doneConstructors; |
---|
114 | doneConstructors = 1; |
---|
115 | #endif |
---|
116 | |
---|
117 | #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE ) |
---|
118 | #if ( CPU_USE_DEFERRED_FP_SWITCH == TRUE ) |
---|
119 | if ( (executing->fp_context != NULL) && |
---|
120 | !_Thread_Is_allocated_fp( executing ) ) { |
---|
121 | if ( _Thread_Allocated_fp != NULL ) |
---|
122 | _Context_Save_fp( &_Thread_Allocated_fp->fp_context ); |
---|
123 | _Thread_Allocated_fp = executing; |
---|
124 | } |
---|
125 | #endif |
---|
126 | #endif |
---|
127 | |
---|
128 | /* |
---|
129 | * Take care that 'begin' extensions get to complete before |
---|
130 | * 'switch' extensions can run. This means must keep dispatch |
---|
131 | * disabled until all 'begin' extensions complete. |
---|
132 | */ |
---|
133 | _User_extensions_Thread_begin( executing ); |
---|
134 | |
---|
135 | /* |
---|
136 | * At this point, the dispatch disable level BETTER be 1. |
---|
137 | */ |
---|
138 | _Thread_Enable_dispatch(); |
---|
139 | |
---|
140 | #if defined(EXECUTE_GLOBAL_CONSTRUCTORS) |
---|
141 | /* |
---|
142 | * _init could be a weak symbol and we SHOULD test it but it isn't |
---|
143 | * in any configuration I know of and it generates a warning on every |
---|
144 | * RTEMS target configuration. --joel (12 May 2007) |
---|
145 | */ |
---|
146 | if (!doneCons) /* && (volatile void *)_init) */ { |
---|
147 | INIT_NAME (); |
---|
148 | |
---|
149 | #if defined(RTEMS_SMP) |
---|
150 | _Thread_Disable_dispatch(); |
---|
151 | _SMP_Request_other_cores_to_perform_first_context_switch(); |
---|
152 | _Thread_Enable_dispatch(); |
---|
153 | #endif |
---|
154 | } |
---|
155 | #endif |
---|
156 | |
---|
157 | if ( executing->Start.prototype == THREAD_START_NUMERIC ) { |
---|
158 | executing->Wait.return_argument = |
---|
159 | (*(Thread_Entry_numeric) executing->Start.entry_point)( |
---|
160 | executing->Start.numeric_argument |
---|
161 | ); |
---|
162 | } |
---|
163 | #if defined(RTEMS_POSIX_API) |
---|
164 | else if ( executing->Start.prototype == THREAD_START_POINTER ) { |
---|
165 | executing->Wait.return_argument = |
---|
166 | (*(Thread_Entry_pointer) executing->Start.entry_point)( |
---|
167 | executing->Start.pointer_argument |
---|
168 | ); |
---|
169 | } |
---|
170 | #endif |
---|
171 | #if defined(FUNCTIONALITY_NOT_CURRENTLY_USED_BY_ANY_API) |
---|
172 | else if ( executing->Start.prototype == THREAD_START_BOTH_POINTER_FIRST ) { |
---|
173 | executing->Wait.return_argument = |
---|
174 | (*(Thread_Entry_both_pointer_first) executing->Start.entry_point)( |
---|
175 | executing->Start.pointer_argument, |
---|
176 | executing->Start.numeric_argument |
---|
177 | ); |
---|
178 | } |
---|
179 | else if ( executing->Start.prototype == THREAD_START_BOTH_NUMERIC_FIRST ) { |
---|
180 | executing->Wait.return_argument = |
---|
181 | (*(Thread_Entry_both_numeric_first) executing->Start.entry_point)( |
---|
182 | executing->Start.numeric_argument, |
---|
183 | executing->Start.pointer_argument |
---|
184 | ); |
---|
185 | } |
---|
186 | #endif |
---|
187 | |
---|
188 | /* |
---|
189 | * In the switch above, the return code from the user thread body |
---|
190 | * was placed in return_argument. This assumed that if it returned |
---|
191 | * anything (which is not supporting in all APIs), then it would be |
---|
192 | * able to fit in a (void *). |
---|
193 | */ |
---|
194 | |
---|
195 | _User_extensions_Thread_exitted( executing ); |
---|
196 | |
---|
197 | _Internal_error_Occurred( |
---|
198 | INTERNAL_ERROR_CORE, |
---|
199 | true, |
---|
200 | INTERNAL_ERROR_THREAD_EXITTED |
---|
201 | ); |
---|
202 | } |
---|