[ac7d5ef0] | 1 | |
---|
| 2 | /* |
---|
| 3 | * COPYRIGHT (c) 1994 by Division Incorporated |
---|
| 4 | * |
---|
[98e4ebf5] | 5 | * The license and distribution terms for this file may be |
---|
| 6 | * found in the file LICENSE in this distribution or at |
---|
[e71ce071] | 7 | * http://www.OARcorp.com/rtems/license.html. |
---|
[ac7d5ef0] | 8 | * |
---|
| 9 | * Description: |
---|
| 10 | * Implementation of hooks for the CYGNUS newlib libc |
---|
| 11 | * These hooks set things up so that: |
---|
| 12 | * '_REENT' is switched at task switch time. |
---|
| 13 | * |
---|
| 14 | * |
---|
| 15 | * TODO: |
---|
| 16 | * |
---|
| 17 | * NOTE: |
---|
| 18 | * |
---|
[3235ad9] | 19 | * $Id$ |
---|
[ac7d5ef0] | 20 | * |
---|
| 21 | */ |
---|
| 22 | |
---|
[1a8fde6c] | 23 | #define __RTEMS_VIOLATE_KERNEL_VISIBILITY__ |
---|
[ac7d5ef0] | 24 | #include <rtems.h> |
---|
[dcec5a4] | 25 | |
---|
| 26 | #if defined(RTEMS_NEWLIB) |
---|
[ac7d5ef0] | 27 | #include <libcsupport.h> |
---|
| 28 | #include <stdlib.h> /* for free() */ |
---|
| 29 | #include <string.h> /* for memset() */ |
---|
| 30 | |
---|
| 31 | #include <sys/reent.h> /* for extern of _REENT (aka _impure_ptr) */ |
---|
[8d7a1d76] | 32 | #include <errno.h> |
---|
[ac7d5ef0] | 33 | |
---|
[1f94ed6b] | 34 | /* |
---|
| 35 | * NOTE: |
---|
| 36 | * There is some problem with doing this on the hpux version |
---|
| 37 | * of the UNIX simulator (symptom is printf core dumps), so |
---|
| 38 | * we just don't for now. |
---|
| 39 | * Not sure if this is a problem with hpux, newlib, or something else. |
---|
| 40 | */ |
---|
| 41 | |
---|
| 42 | #if defined(RTEMS_UNIX) && !defined(hpux) |
---|
| 43 | #define NEED_SETVBUF |
---|
| 44 | #endif |
---|
| 45 | |
---|
| 46 | #ifdef NEED_SETVBUF |
---|
| 47 | #include <stdio.h> |
---|
[eaa442fc] | 48 | #endif |
---|
| 49 | |
---|
[ac7d5ef0] | 50 | #include "internal.h" |
---|
| 51 | |
---|
| 52 | #define LIBC_NOTEPAD RTEMS_NOTEPAD_LAST |
---|
| 53 | |
---|
| 54 | |
---|
| 55 | int libc_reentrant; /* do we think we are reentrant? */ |
---|
[5e9a594] | 56 | struct _reent libc_global_reent = _REENT_INIT(libc_global_reent); |
---|
[ac7d5ef0] | 57 | |
---|
| 58 | /* |
---|
| 59 | * CYGNUS newlib routine that does atexit() processing and flushes |
---|
| 60 | * stdio streams |
---|
| 61 | * undocumented |
---|
| 62 | */ |
---|
| 63 | |
---|
| 64 | extern void _wrapup_reent(struct _reent *); |
---|
| 65 | extern void _reclaim_reent(struct _reent *); |
---|
| 66 | |
---|
| 67 | void |
---|
| 68 | libc_wrapup(void) |
---|
| 69 | { |
---|
[e39e6f7] | 70 | /* |
---|
| 71 | * In case RTEMS is already down, don't do this. It could be |
---|
| 72 | * dangerous. |
---|
| 73 | */ |
---|
| 74 | |
---|
| 75 | if (!_System_state_Is_up(_System_state_Get())) |
---|
| 76 | return; |
---|
| 77 | |
---|
[ac7d5ef0] | 78 | _wrapup_reent(0); |
---|
| 79 | if (_REENT != &libc_global_reent) |
---|
| 80 | { |
---|
| 81 | _wrapup_reent(&libc_global_reent); |
---|
| 82 | #if 0 |
---|
| 83 | /* don't reclaim this one, just in case we do printfs */ |
---|
| 84 | /* on our way out to ROM */ |
---|
| 85 | _reclaim_reent(&libc_global_reent); |
---|
| 86 | #endif |
---|
| 87 | _REENT = &libc_global_reent; |
---|
| 88 | } |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | |
---|
[3a4ae6c] | 92 | rtems_boolean |
---|
[ac7d5ef0] | 93 | libc_create_hook(rtems_tcb *current_task, |
---|
| 94 | rtems_tcb *creating_task) |
---|
| 95 | { |
---|
| 96 | MY_task_set_note(creating_task, LIBC_NOTEPAD, 0); |
---|
[3a4ae6c] | 97 | return TRUE; |
---|
[ac7d5ef0] | 98 | } |
---|
| 99 | |
---|
| 100 | /* |
---|
[8cbbe312] | 101 | * Called for all user TASKS (system tasks are MPCI Receive Server and IDLE) |
---|
[ac7d5ef0] | 102 | */ |
---|
| 103 | |
---|
| 104 | rtems_extension |
---|
| 105 | libc_start_hook(rtems_tcb *current_task, |
---|
| 106 | rtems_tcb *starting_task) |
---|
| 107 | { |
---|
| 108 | struct _reent *ptr; |
---|
| 109 | |
---|
| 110 | /* NOTE: our malloc is reentrant without a reent ptr since |
---|
| 111 | * it is based on region manager |
---|
| 112 | */ |
---|
| 113 | |
---|
[5e9a594] | 114 | ptr = (struct _reent *) calloc(1, sizeof(struct _reent)); |
---|
[ac7d5ef0] | 115 | |
---|
[5e9a594] | 116 | if (!ptr) |
---|
| 117 | rtems_fatal_error_occurred(RTEMS_NO_MEMORY); |
---|
| 118 | |
---|
| 119 | #ifdef __GNUC__ |
---|
[ac7d5ef0] | 120 | /* GCC extension: structure constants */ |
---|
| 121 | *ptr = (struct _reent) _REENT_INIT((*ptr)); |
---|
[5e9a594] | 122 | #else |
---|
| 123 | /* |
---|
| 124 | * Warning: THIS IS VERY DEPENDENT ON NEWLIB!!! WRITTEN FOR 1.7.0 |
---|
| 125 | */ |
---|
| 126 | ptr->_errno=0; |
---|
| 127 | ptr->_stdin=&ptr->__sf[0]; |
---|
| 128 | ptr->_stdout=&ptr->__sf[1]; |
---|
| 129 | ptr->_stderr=&ptr->__sf[2]; |
---|
| 130 | ptr->_scanpoint=0; |
---|
| 131 | ptr->_asctime[0]=0; |
---|
| 132 | ptr->_next=1; |
---|
| 133 | ptr->__sdidinit=0; |
---|
| 134 | #endif |
---|
[ac7d5ef0] | 135 | |
---|
| 136 | MY_task_set_note(starting_task, LIBC_NOTEPAD, (rtems_unsigned32) ptr); |
---|
| 137 | } |
---|
| 138 | |
---|
[eaa442fc] | 139 | /* |
---|
[8cbbe312] | 140 | * Called for all user TASKS (system tasks are MPCI Receive Server and IDLE) |
---|
[eaa442fc] | 141 | */ |
---|
| 142 | |
---|
[8b2ecf85] | 143 | #ifdef NEED_SETVBUF |
---|
[eaa442fc] | 144 | rtems_extension |
---|
| 145 | libc_begin_hook(rtems_tcb *current_task) |
---|
| 146 | { |
---|
| 147 | setvbuf( stdout, NULL, _IOLBF, BUFSIZ ); |
---|
| 148 | } |
---|
| 149 | #endif |
---|
| 150 | |
---|
[ac7d5ef0] | 151 | rtems_extension |
---|
| 152 | libc_switch_hook(rtems_tcb *current_task, |
---|
| 153 | rtems_tcb *heir_task) |
---|
| 154 | { |
---|
| 155 | rtems_unsigned32 impure_value; |
---|
| 156 | |
---|
| 157 | /* XXX We can't use rtems_task_set_note() here since SYSI task has a |
---|
| 158 | * tid of 0, which is treated specially (optimized, actually) |
---|
| 159 | * by rtems_task_set_note |
---|
[b3ac6a8d] | 160 | * |
---|
| 161 | * NOTE: The above comment is no longer true and we need to use |
---|
| 162 | * the extension data areas added about the same time. |
---|
[ac7d5ef0] | 163 | */ |
---|
| 164 | |
---|
[b3ac6a8d] | 165 | /* |
---|
| 166 | * Don't touch the outgoing task if it has been deleted. |
---|
| 167 | */ |
---|
| 168 | |
---|
| 169 | if ( !_States_Is_transient( current_task->current_state ) ) { |
---|
| 170 | impure_value = (rtems_unsigned32) _REENT; |
---|
| 171 | MY_task_set_note(current_task, LIBC_NOTEPAD, impure_value); |
---|
| 172 | } |
---|
[ac7d5ef0] | 173 | |
---|
| 174 | _REENT = (struct _reent *) MY_task_get_note(heir_task, LIBC_NOTEPAD); |
---|
| 175 | |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | /* |
---|
| 179 | * Function: libc_delete_hook |
---|
| 180 | * Created: 94/12/10 |
---|
| 181 | * |
---|
| 182 | * Description: |
---|
| 183 | * Called when a task is deleted. |
---|
| 184 | * Must restore the new lib reentrancy state for the new current |
---|
| 185 | * task. |
---|
| 186 | * |
---|
| 187 | * Parameters: |
---|
| 188 | * |
---|
| 189 | * |
---|
| 190 | * Returns: |
---|
| 191 | * |
---|
| 192 | * |
---|
| 193 | * Side Effects: |
---|
| 194 | * |
---|
| 195 | * Notes: |
---|
| 196 | * |
---|
| 197 | * |
---|
| 198 | * Deficiencies/ToDo: |
---|
| 199 | * |
---|
| 200 | * |
---|
| 201 | */ |
---|
| 202 | rtems_extension |
---|
| 203 | libc_delete_hook(rtems_tcb *current_task, |
---|
| 204 | rtems_tcb *deleted_task) |
---|
| 205 | { |
---|
| 206 | struct _reent *ptr; |
---|
| 207 | |
---|
| 208 | /* |
---|
| 209 | * The reentrancy structure was allocated by newlib using malloc() |
---|
| 210 | */ |
---|
| 211 | |
---|
| 212 | if (current_task == deleted_task) |
---|
| 213 | { |
---|
[1f94ed6b] | 214 | ptr = _REENT; |
---|
[ac7d5ef0] | 215 | } |
---|
| 216 | else |
---|
| 217 | { |
---|
[1f94ed6b] | 218 | ptr = (struct _reent *) MY_task_get_note(deleted_task, LIBC_NOTEPAD); |
---|
[ac7d5ef0] | 219 | } |
---|
| 220 | |
---|
[b3ac6a8d] | 221 | /* if (ptr) */ |
---|
| 222 | if (ptr && ptr != &libc_global_reent) |
---|
[ac7d5ef0] | 223 | { |
---|
[1f94ed6b] | 224 | _wrapup_reent(ptr); |
---|
| 225 | _reclaim_reent(ptr); |
---|
| 226 | free(ptr); |
---|
[ac7d5ef0] | 227 | } |
---|
| 228 | |
---|
| 229 | MY_task_set_note(deleted_task, LIBC_NOTEPAD, 0); |
---|
| 230 | |
---|
| 231 | /* |
---|
| 232 | * Require the switch back to another task to install its own |
---|
| 233 | */ |
---|
| 234 | |
---|
| 235 | if (current_task == deleted_task) |
---|
| 236 | { |
---|
[1f94ed6b] | 237 | _REENT = 0; |
---|
[ac7d5ef0] | 238 | } |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | /* |
---|
| 242 | * Function: libc_init |
---|
| 243 | * Created: 94/12/10 |
---|
| 244 | * |
---|
| 245 | * Description: |
---|
| 246 | * Init libc for CYGNUS newlib |
---|
| 247 | * Set up _REENT to use our global libc_global_reent. |
---|
| 248 | * (newlib provides a global of its own, but we prefer our |
---|
| 249 | * own name for it) |
---|
| 250 | * |
---|
| 251 | * If reentrancy is desired (which it should be), then |
---|
| 252 | * we install the task extension hooks to maintain the |
---|
| 253 | * newlib reentrancy global variable _REENT on task |
---|
| 254 | * create, delete, switch, exit, etc. |
---|
| 255 | * |
---|
| 256 | * Parameters: |
---|
| 257 | * reentrant non-zero if reentrant library desired. |
---|
| 258 | * |
---|
| 259 | * Returns: |
---|
| 260 | * |
---|
| 261 | * Side Effects: |
---|
| 262 | * installs libc extensions if reentrant. |
---|
| 263 | * |
---|
| 264 | * Notes: |
---|
| 265 | * |
---|
| 266 | * |
---|
| 267 | * Deficiencies/ToDo: |
---|
| 268 | * |
---|
| 269 | */ |
---|
| 270 | |
---|
| 271 | void |
---|
| 272 | libc_init(int reentrant) |
---|
| 273 | { |
---|
| 274 | rtems_extensions_table libc_extension; |
---|
| 275 | rtems_id extension_id; |
---|
| 276 | rtems_status_code rc; |
---|
| 277 | |
---|
| 278 | _REENT = &libc_global_reent; |
---|
| 279 | |
---|
| 280 | if (reentrant) |
---|
| 281 | { |
---|
| 282 | memset(&libc_extension, 0, sizeof(libc_extension)); |
---|
| 283 | |
---|
[3a4ae6c] | 284 | libc_extension.thread_create = libc_create_hook; |
---|
| 285 | libc_extension.thread_start = libc_start_hook; |
---|
[8b2ecf85] | 286 | #ifdef NEED_SETVBUF |
---|
[eaa442fc] | 287 | libc_extension.thread_begin = libc_begin_hook; |
---|
| 288 | #endif |
---|
[3a4ae6c] | 289 | libc_extension.thread_switch = libc_switch_hook; |
---|
| 290 | libc_extension.thread_delete = libc_delete_hook; |
---|
[ac7d5ef0] | 291 | |
---|
| 292 | rc = rtems_extension_create(rtems_build_name('L', 'I', 'B', 'C'), |
---|
| 293 | &libc_extension, &extension_id); |
---|
| 294 | if (rc != RTEMS_SUCCESSFUL) |
---|
| 295 | rtems_fatal_error_occurred(rc); |
---|
| 296 | |
---|
| 297 | libc_reentrant = reentrant; |
---|
| 298 | } |
---|
| 299 | } |
---|
| 300 | |
---|
[4bab8e2d] | 301 | #if 0 |
---|
[8d7a1d76] | 302 | /* |
---|
| 303 | * Routines required by the gnat runtime. |
---|
| 304 | */ |
---|
| 305 | |
---|
| 306 | int get_errno() |
---|
| 307 | { |
---|
| 308 | return errno; |
---|
| 309 | } |
---|
[4bab8e2d] | 310 | #endif |
---|
[ac7d5ef0] | 311 | |
---|
| 312 | /* |
---|
| 313 | * Function: _exit |
---|
| 314 | * Created: 94/12/10 |
---|
| 315 | * |
---|
| 316 | * Description: |
---|
| 317 | * Called from exit() after it does atexit() processing and stdio fflush's |
---|
| 318 | * |
---|
| 319 | * called from bottom of exit() to really delete the task. |
---|
| 320 | * If we are using reentrant libc, then let the delete extension |
---|
| 321 | * do all the work, otherwise if a shutdown is in progress, |
---|
| 322 | * then just do it. |
---|
| 323 | * |
---|
| 324 | * Parameters: |
---|
| 325 | * exit status |
---|
| 326 | * |
---|
| 327 | * Returns: |
---|
| 328 | * does not return |
---|
| 329 | * |
---|
| 330 | * Side Effects: |
---|
| 331 | * |
---|
| 332 | * Notes: |
---|
| 333 | * |
---|
| 334 | * |
---|
| 335 | * Deficiencies/ToDo: |
---|
| 336 | * |
---|
| 337 | * |
---|
| 338 | */ |
---|
| 339 | |
---|
[dcec5a4] | 340 | #include <stdio.h> |
---|
| 341 | |
---|
[aea06ed] | 342 | /* #if !defined(RTEMS_UNIX) && !defined(__GO32__) && !defined(_AM29K) */ |
---|
| 343 | #if !defined(RTEMS_UNIX) && !defined(_AM29K) |
---|
[7150f00f] | 344 | #if !defined(pc386) |
---|
[ac7d5ef0] | 345 | void _exit(int status) |
---|
| 346 | { |
---|
[f00d7ad] | 347 | libc_wrapup(); /* Why? XXX */ |
---|
[ac7d5ef0] | 348 | rtems_shutdown_executive(status); |
---|
| 349 | } |
---|
[7150f00f] | 350 | #endif |
---|
[da4b6e8] | 351 | |
---|
| 352 | #else |
---|
| 353 | |
---|
| 354 | void exit(int status) |
---|
| 355 | { |
---|
| 356 | libc_wrapup(); |
---|
| 357 | rtems_shutdown_executive(status); |
---|
| 358 | } |
---|
[ac7d5ef0] | 359 | #endif |
---|
| 360 | |
---|
[b169590e] | 361 | |
---|
| 362 | /* |
---|
[685f4d65] | 363 | * These are directly supported (and completely correct) in the posix api. |
---|
[b169590e] | 364 | */ |
---|
| 365 | |
---|
| 366 | pid_t __getpid(void) |
---|
| 367 | { |
---|
[b1459dc] | 368 | return getpid(); |
---|
[b169590e] | 369 | } |
---|
[8d7a1d76] | 370 | |
---|
[aea06ed] | 371 | /* #if !defined(RTEMS_POSIX_API) || defined(__GO32__) */ |
---|
| 372 | #if !defined(RTEMS_POSIX_API) |
---|
[8d7a1d76] | 373 | pid_t getpid(void) |
---|
| 374 | { |
---|
[b1459dc] | 375 | return (0); |
---|
[8d7a1d76] | 376 | } |
---|
| 377 | #endif |
---|
| 378 | |
---|
[aea06ed] | 379 | /* #if !defined(RTEMS_POSIX_API) || defined(__GO32__) */ |
---|
| 380 | #if !defined(RTEMS_POSIX_API) |
---|
[8d7a1d76] | 381 | int kill( pid_t pid, int sig ) |
---|
| 382 | { |
---|
| 383 | return 0; |
---|
| 384 | } |
---|
| 385 | #endif |
---|
[b169590e] | 386 | |
---|
| 387 | int __kill( pid_t pid, int sig ) |
---|
| 388 | { |
---|
| 389 | return 0; |
---|
| 390 | } |
---|
| 391 | |
---|
[4bab8e2d] | 392 | #if !defined(RTEMS_POSIX_API) |
---|
[685f4d65] | 393 | unsigned int sleep( |
---|
| 394 | unsigned int seconds |
---|
| 395 | ) |
---|
| 396 | { |
---|
| 397 | rtems_status_code status; |
---|
| 398 | rtems_interval ticks_per_second; |
---|
| 399 | rtems_interval ticks; |
---|
| 400 | |
---|
| 401 | status = rtems_clock_get( |
---|
| 402 | RTEMS_CLOCK_GET_TICKS_PER_SECOND, |
---|
| 403 | &ticks_per_second |
---|
| 404 | ); |
---|
| 405 | |
---|
| 406 | ticks = seconds * ticks_per_second; |
---|
| 407 | |
---|
| 408 | status = rtems_task_wake_after( ticks ); |
---|
| 409 | |
---|
| 410 | /* |
---|
| 411 | * Returns the "unslept" amount of time. In RTEMS signals are not |
---|
| 412 | * interruptable, so tasks really sleep all of the requested time. |
---|
| 413 | */ |
---|
| 414 | |
---|
| 415 | return 0; |
---|
| 416 | } |
---|
[4bab8e2d] | 417 | #endif |
---|
[685f4d65] | 418 | |
---|
[b169590e] | 419 | |
---|
[ac7d5ef0] | 420 | #endif |
---|