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