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