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