source: rtems/cpukit/score/cpu/unix/cpu.c @ 5e34bf4

4.104.114.84.95
Last change on this file since 5e34bf4 was 5e34bf4, checked in by Joel Sherrill <joel.sherrill@…>, on 03/31/98 at 14:19:27

Added "sigemptyset()" call to insure that the memcmp() would work.
It appears that the new glibc does not clear all the bits of the signal
set with a sigprocmask.

  • Property mode set to 100644
File size: 23.0 KB
RevLine 
[ac7d5ef0]1/*
[3652ad35]2 *  UNIX Simulator Dependent Source
[ac7d5ef0]3 *
[e71ce071]4 *  COPYRIGHT (c) 1994,95 by Division Incorporated
[ac7d5ef0]5 *
[98e4ebf5]6 *  The license and distribution terms for this file may be
7 *  found in the file LICENSE in this distribution or at
[e71ce071]8 *  http://www.OARcorp.com/rtems/license.html.
[ac7d5ef0]9 *
10 *  $Id$
11 */
12
13#include <rtems/system.h>
[5e9b32b]14#include <rtems/score/isr.h>
15#include <rtems/score/interr.h>
[ac7d5ef0]16
[f2545552]17#if defined(__linux__)
[ddf142d]18#define _XOPEN_SOURCE
[37f4c2d]19#define MALLOC_0_RETURNS_NULL
20#endif
[d1193c7]21
[1c964ffa]22#include <sys/types.h>
23#include <sys/times.h>
[ac7d5ef0]24#include <stdio.h>
25#include <stdlib.h>
[37f4c2d]26#include <setjmp.h>
[ac7d5ef0]27#include <signal.h>
28#include <time.h>
[10aed1e3]29#include <sys/time.h>
[37f4c2d]30#include <errno.h>
31#include <unistd.h>
32#include <sys/ipc.h>
33#include <sys/shm.h>
34#include <sys/sem.h>
[cc4c1fe4]35#include <string.h>   /* memset */
[ac7d5ef0]36
[637df35]37#ifndef SA_RESTART
38#define SA_RESTART 0
39#endif
[ac7d5ef0]40
[37f4c2d]41typedef struct {
42  jmp_buf   regs;
[a30359bb]43  int  isr_level;
[37f4c2d]44} Context_Control_overlay;
45
[637df35]46void  _CPU_Signal_initialize(void);
47void  _CPU_Stray_signal(int);
48void  _CPU_ISR_Handler(int);
[ac7d5ef0]49
[d196e48]50static sigset_t         _CPU_Signal_mask;
[d1193c7]51static Context_Control_overlay
[855edec]52          _CPU_Context_Default_with_ISRs_enabled CPU_STRUCTURE_ALIGNMENT;
[d1193c7]53static Context_Control_overlay
[855edec]54          _CPU_Context_Default_with_ISRs_disabled CPU_STRUCTURE_ALIGNMENT;
[ac7d5ef0]55
[0a6fb22]56/*
57 * Sync IO support, an entry for each fd that can be set
58 */
59
60void  _CPU_Sync_io_Init();
61
62static rtems_sync_io_handler _CPU_Sync_io_handlers[FD_SETSIZE];
63static int sync_io_nfds;
64static fd_set sync_io_readfds;
65static fd_set sync_io_writefds;
66static fd_set sync_io_exceptfds;
67
[ac7d5ef0]68/*
69 * Which cpu are we? Used by libcpu and libbsp.
70 */
71
72int cpu_number;
73
[637df35]74/*PAGE
75 *
76 *  _CPU_ISR_From_CPU_Init
77 */
78
[e7e016f]79sigset_t  posix_empty_mask;
80
[637df35]81void _CPU_ISR_From_CPU_Init()
82{
83  unsigned32        i;
84  proc_ptr          old_handler;
85
[e7e016f]86  /*
87   * Generate an empty mask to be used by disable_support
88   */
[637df35]89
[e7e016f]90  sigemptyset(&posix_empty_mask);
[c64e4ed4]91
[637df35]92  /*
93   * Block all the signals except SIGTRAP for the debugger
[d196e48]94   * and fatal error signals.
[637df35]95   */
96
97  (void) sigfillset(&_CPU_Signal_mask);
98  (void) sigdelset(&_CPU_Signal_mask, SIGTRAP);
99  (void) sigdelset(&_CPU_Signal_mask, SIGABRT);
100  (void) sigdelset(&_CPU_Signal_mask, SIGIOT);
101  (void) sigdelset(&_CPU_Signal_mask, SIGCONT);
[d196e48]102  (void) sigdelset(&_CPU_Signal_mask, SIGSEGV);
103  (void) sigdelset(&_CPU_Signal_mask, SIGBUS);
104  (void) sigdelset(&_CPU_Signal_mask, SIGFPE);
[637df35]105
[e7e016f]106  _CPU_ISR_Enable(1);
[637df35]107
108  /*
109   * Set the handler for all signals to be signal_handler
110   * which will then vector out to the correct handler
111   * for whichever signal actually happened. Initially
112   * set the vectors to the stray signal handler.
113   */
114
115  for (i = 0; i < CPU_INTERRUPT_NUMBER_OF_VECTORS; i++)
116      (void)_CPU_ISR_install_vector(i, _CPU_Stray_signal, &old_handler);
117
118  _CPU_Signal_initialize();
119}
120
121void _CPU_Signal_initialize( void )
122{
123  struct sigaction  act;
124  sigset_t          mask;
[d1193c7]125
[637df35]126  /* mark them all active except for TraceTrap  and Abort */
[d1193c7]127
[d196e48]128  mask = _CPU_Signal_mask;
[637df35]129  sigprocmask(SIG_UNBLOCK, &mask, 0);
[d1193c7]130
[637df35]131  act.sa_handler = _CPU_ISR_Handler;
132  act.sa_mask = mask;
133  act.sa_flags = SA_RESTART;
[d1193c7]134
[637df35]135  sigaction(SIGHUP, &act, 0);
136  sigaction(SIGINT, &act, 0);
137  sigaction(SIGQUIT, &act, 0);
138  sigaction(SIGILL, &act, 0);
[10aed1e3]139#ifdef SIGEMT
[637df35]140  sigaction(SIGEMT, &act, 0);
[10aed1e3]141#endif
[637df35]142  sigaction(SIGFPE, &act, 0);
143  sigaction(SIGKILL, &act, 0);
144  sigaction(SIGBUS, &act, 0);
145  sigaction(SIGSEGV, &act, 0);
[10aed1e3]146#ifdef SIGSYS
[637df35]147  sigaction(SIGSYS, &act, 0);
[10aed1e3]148#endif
[637df35]149  sigaction(SIGPIPE, &act, 0);
150  sigaction(SIGALRM, &act, 0);
151  sigaction(SIGTERM, &act, 0);
152  sigaction(SIGUSR1, &act, 0);
153  sigaction(SIGUSR2, &act, 0);
154  sigaction(SIGCHLD, &act, 0);
155  sigaction(SIGCLD, &act, 0);
156  sigaction(SIGPWR, &act, 0);
157  sigaction(SIGVTALRM, &act, 0);
158  sigaction(SIGPROF, &act, 0);
159  sigaction(SIGIO, &act, 0);
160  sigaction(SIGWINCH, &act, 0);
161  sigaction(SIGSTOP, &act, 0);
162  sigaction(SIGTTIN, &act, 0);
163  sigaction(SIGTTOU, &act, 0);
164  sigaction(SIGURG, &act, 0);
[e7e016f]165#ifdef SIGLOST
[637df35]166    sigaction(SIGLOST, &act, 0);
167#endif
168}
169
170/*PAGE
171 *
172 *  _CPU_Context_From_CPU_Init
173 */
174
175void _CPU_Context_From_CPU_Init()
176{
177
[3ec7bfc]178#if defined(__hppa__) && defined(RTEMS_UNIXLIB_SETJMP)
[637df35]179    /*
180     * HACK - set the _SYSTEM_ID to 0x20c so that setjmp/longjmp
181     * will handle the full 32 floating point registers.
182     */
183
184    {
185      extern unsigned32 _SYSTEM_ID;
186
187      _SYSTEM_ID = 0x20c;
188    }
189#endif
190
191  /*
192   *  get default values to use in _CPU_Context_Initialize()
193   */
194
[d1193c7]195
[cc4c1fe4]196  (void) memset(
197    &_CPU_Context_Default_with_ISRs_enabled,
198    0,
199    sizeof(Context_Control)
200  );
201  (void) memset(
202    &_CPU_Context_Default_with_ISRs_disabled,
203    0,
204    sizeof(Context_Control)
205  );
206
[637df35]207  _CPU_ISR_Set_level( 0 );
[3652ad35]208  _CPU_Context_switch(
[d196e48]209    (Context_Control *) &_CPU_Context_Default_with_ISRs_enabled,
210    (Context_Control *) &_CPU_Context_Default_with_ISRs_enabled
[637df35]211  );
[d1193c7]212
[637df35]213  _CPU_ISR_Set_level( 1 );
[3652ad35]214  _CPU_Context_switch(
[d196e48]215    (Context_Control *) &_CPU_Context_Default_with_ISRs_disabled,
216    (Context_Control *) &_CPU_Context_Default_with_ISRs_disabled
[637df35]217  );
218}
219
[0a6fb22]220/*PAGE
221 *
222 *  _CPU_Sync_io_Init
223 */
224
225void _CPU_Sync_io_Init()
226{
227  int fd;
228
229  for (fd = 0; fd < FD_SETSIZE; fd++)
230    _CPU_Sync_io_handlers[fd] = NULL;
231
232  sync_io_nfds = 0;
233  FD_ZERO(&sync_io_readfds);
234  FD_ZERO(&sync_io_writefds);
235  FD_ZERO(&sync_io_exceptfds);
236}
237
[3a4ae6c]238/*PAGE
239 *
240 *  _CPU_ISR_Get_level
241 */
242
243unsigned32 _CPU_ISR_Get_level( void )
244{
[d196e48]245  sigset_t old_mask;
[5e34bf4]246  sigset_t new_mask;
[d1193c7]247
[5e34bf4]248#if defined(__linux__)
249  sigemptyset( &old_mask );
250#endif
[d196e48]251  sigprocmask(SIG_BLOCK, 0, &old_mask);
[d1193c7]252
[d196e48]253  if (memcmp((void *)&posix_empty_mask, (void *)&old_mask, sizeof(sigset_t)))
254      return 1;
[d1193c7]255
[d196e48]256  return 0;
[3a4ae6c]257}
258
[ac7d5ef0]259/*  _CPU_Initialize
260 *
261 *  This routine performs processor dependent initialization.
262 *
263 *  INPUT PARAMETERS:
264 *    cpu_table       - CPU table to initialize
265 *    thread_dispatch - address of disptaching routine
266 */
267
268
269void _CPU_Initialize(
270  rtems_cpu_table  *cpu_table,
[3a4ae6c]271  void            (*thread_dispatch)      /* ignored on this CPU */
[ac7d5ef0]272)
273{
274  /*
275   *  The thread_dispatch argument is the address of the entry point
276   *  for the routine called at the end of an ISR once it has been
277   *  decided a context switch is necessary.  On some compilation
278   *  systems it is difficult to call a high-level language routine
279   *  from assembly.  This allows us to trick these systems.
280   *
281   *  If you encounter this problem save the entry point in a CPU
282   *  dependent variable.
283   */
284
285  _CPU_Thread_dispatch_pointer = thread_dispatch;
286
287  /*
288   * XXX; If there is not an easy way to initialize the FP context
289   *      during Context_Initialize, then it is usually easier to
290   *      save an "uninitialized" FP context here and copy it to
291   *      the task's during Context_Initialize.
292   */
293
294  /* XXX: FP context initialization support */
295
296  _CPU_Table = *cpu_table;
297
[637df35]298  _CPU_ISR_From_CPU_Init();
[ac7d5ef0]299
[0a6fb22]300  _CPU_Sync_io_Init();
301
[637df35]302  _CPU_Context_From_CPU_Init();
[ac7d5ef0]303
[637df35]304}
[ac7d5ef0]305
[637df35]306/*PAGE
307 *
308 *  _CPU_ISR_install_raw_handler
309 */
[ac7d5ef0]310
[637df35]311void _CPU_ISR_install_raw_handler(
312  unsigned32  vector,
313  proc_ptr    new_handler,
314  proc_ptr   *old_handler
315)
316{
317  _CPU_Fatal_halt( 0xdeaddead );
[ac7d5ef0]318}
319
[637df35]320/*PAGE
321 *
322 *  _CPU_ISR_install_vector
[ac7d5ef0]323 *
324 *  This kernel routine installs the RTEMS handler for the
325 *  specified vector.
326 *
327 *  Input parameters:
328 *    vector      - interrupt vector number
329 *    old_handler - former ISR for this vector number
330 *    new_handler - replacement ISR for this vector number
331 *
332 *  Output parameters:  NONE
333 *
334 */
335
336
337void _CPU_ISR_install_vector(
338  unsigned32  vector,
339  proc_ptr    new_handler,
340  proc_ptr   *old_handler
341)
342{
343   *old_handler = _ISR_Vector_table[ vector ];
344
345   /*
346    *  If the interrupt vector table is a table of pointer to isr entry
347    *  points, then we need to install the appropriate RTEMS interrupt
348    *  handler for this vector number.
349    */
350
351   /*
352    *  We put the actual user ISR address in '_ISR_vector_table'.  This will
[637df35]353    *  be used by the _CPU_ISR_Handler so the user gets control.
[ac7d5ef0]354    */
355
356    _ISR_Vector_table[ vector ] = new_handler;
357}
358
359/*PAGE
360 *
361 *  _CPU_Install_interrupt_stack
362 */
363
364void _CPU_Install_interrupt_stack( void )
365{
366}
367
368/*PAGE
369 *
[75f09e5]370 *  _CPU_Thread_Idle_body
[ac7d5ef0]371 *
[d1193c7]372 *  Stop until we get a signal which is the logically the same thing
[9700578]373 *  entering low-power or sleep mode on a real processor and waiting for
374 *  an interrupt.  This significantly reduces the consumption of host
375 *  CPU cycles which is again similar to low power mode.
[ac7d5ef0]376 */
377
[75f09e5]378void _CPU_Thread_Idle_body( void )
[ac7d5ef0]379{
[0a6fb22]380#if CPU_SYNC_IO
381  extern void _Thread_Dispatch(void);
382  int fd;
383#endif
384
[d196e48]385  while (1) {
386#ifdef RTEMS_DEBUG
387    /* interrupts had better be enabled at this point! */
388    if (_CPU_ISR_Get_level() != 0)
389       abort();
390#endif
[0a6fb22]391
392    /*
393     *  Block on a select statement, the CPU interface added allow the
394     *  user to add new descriptors which are to be blocked on
395     */
396
397#if CPU_SYNC_IO
398    if (sync_io_nfds) {
399      int result;
400
401      result = select(sync_io_nfds,
402                 &sync_io_readfds,
403                 &sync_io_writefds,
404                 &sync_io_exceptfds,
405                 NULL);
406
407      if ((result < 0) && (errno != EINTR))
408        _CPU_Fatal_error(0x200);       /* FIXME : what number should go here !! */
409
410      for (fd = 0; fd < sync_io_nfds; fd++) {
411        boolean read = FD_ISSET(fd, &sync_io_readfds);
412        boolean write = FD_ISSET(fd, &sync_io_writefds);
413        boolean except = FD_ISSET(fd, &sync_io_exceptfds);
414
415        if (_CPU_Sync_io_handlers[fd] && (read || write || except))
416          _CPU_Sync_io_handlers[fd](fd, read, write, except);
417
418        _Thread_Dispatch();
419      }
420    } else
421      pause();
422#else
[637df35]423    pause();
[0a6fb22]424#endif
425
[d196e48]426  }
427
[ac7d5ef0]428}
429
[637df35]430/*PAGE
[d1193c7]431 *
[637df35]432 *  _CPU_Context_Initialize
433 */
434
[ac7d5ef0]435void _CPU_Context_Initialize(
436  Context_Control  *_the_context,
437  unsigned32       *_stack_base,
438  unsigned32        _size,
439  unsigned32        _new_level,
[9700578]440  void             *_entry_point,
441  boolean           _is_fp
[ac7d5ef0]442)
443{
[637df35]444  unsigned32  *addr;
445  unsigned32   jmp_addr;
446  unsigned32   _stack_low;   /* lowest "stack aligned" address */
447  unsigned32   _stack_high;  /* highest "stack aligned" address */
448  unsigned32   _the_size;
[ac7d5ef0]449
[637df35]450  jmp_addr = (unsigned32) _entry_point;
[ac7d5ef0]451
[637df35]452  /*
453   *  On CPUs with stacks which grow down, we build the stack
[d1193c7]454   *  based on the _stack_high address.  On CPUs with stacks which
455   *  grow up, we build the stack based on the _stack_low address.
[637df35]456   */
[88d594a]457
[cc4c1fe4]458  _stack_low = (unsigned32)(_stack_base) + CPU_STACK_ALIGNMENT - 1;
[637df35]459  _stack_low &= ~(CPU_STACK_ALIGNMENT - 1);
[88d594a]460
[cc4c1fe4]461  _stack_high = (unsigned32)(_stack_base) + _size;
[637df35]462  _stack_high &= ~(CPU_STACK_ALIGNMENT - 1);
[ac7d5ef0]463
[cc4c1fe4]464  if (_stack_high > _stack_low)
465    _the_size = _stack_high - _stack_low;
466  else
467    _the_size = _stack_low - _stack_high;
[ac7d5ef0]468
[637df35]469  /*
470   * Slam our jmp_buf template into the context we are creating
471   */
[ac7d5ef0]472
[637df35]473  if ( _new_level == 0 )
[d196e48]474      *_the_context = *(Context_Control *)
475                         &_CPU_Context_Default_with_ISRs_enabled;
[637df35]476  else
[d196e48]477      *_the_context = *(Context_Control *)
478                         &_CPU_Context_Default_with_ISRs_disabled;
[d1193c7]479
[637df35]480  addr = (unsigned32 *)_the_context;
[ac7d5ef0]481
[3ec7bfc]482#if defined(__hppa__)
[637df35]483  *(addr + RP_OFF) = jmp_addr;
484  *(addr + SP_OFF) = (unsigned32)(_stack_low + CPU_FRAME_SIZE);
[ac7d5ef0]485
[637df35]486  /*
487   * See if we are using shared libraries by checking
488   * bit 30 in 24 off of newp. If bit 30 is set then
489   * we are using shared libraries and the jump address
[cc4c1fe4]490   * points to the pointer, so we put that into rp instead.
[637df35]491   */
[ac7d5ef0]492
[637df35]493  if (jmp_addr & 0x40000000) {
494    jmp_addr &= 0xfffffffc;
[cc4c1fe4]495     *(addr + RP_OFF) = *(unsigned32 *)jmp_addr;
[637df35]496  }
[3ec7bfc]497#elif defined(__sparc__)
[ac7d5ef0]498
[637df35]499  /*
500   *  See /usr/include/sys/stack.h in Solaris 2.3 for a nice
501   *  diagram of the stack.
502   */
[ac7d5ef0]503
[637df35]504  asm ("ta  0x03");            /* flush registers */
[ac7d5ef0]505
[637df35]506  *(addr + RP_OFF) = jmp_addr + ADDR_ADJ_OFFSET;
507  *(addr + SP_OFF) = (unsigned32)(_stack_high - CPU_FRAME_SIZE);
508  *(addr + FP_OFF) = (unsigned32)(_stack_high);
[8044533]509
[3ec7bfc]510#elif defined(__i386__)
[d1193c7]511
[8044533]512    /*
513     *  This information was gathered by disassembling setjmp().
514     */
[10aed1e3]515
516    {
517      unsigned32 stack_ptr;
518
519      stack_ptr = _stack_high - CPU_FRAME_SIZE;
520
521      *(addr + EBX_OFF) = 0xFEEDFEED;
522      *(addr + ESI_OFF) = 0xDEADDEAD;
523      *(addr + EDI_OFF) = 0xDEAFDEAF;
524      *(addr + EBP_OFF) = stack_ptr;
525      *(addr + ESP_OFF) = stack_ptr;
526      *(addr + RET_OFF) = jmp_addr;
[d1193c7]527
[10aed1e3]528      addr = (unsigned32 *) stack_ptr;
[d1193c7]529
[10aed1e3]530      addr[ 0 ] = jmp_addr;
531      addr[ 1 ] = (unsigned32) stack_ptr;
532      addr[ 2 ] = (unsigned32) stack_ptr;
533    }
[8044533]534
[ac7d5ef0]535#else
536#error "UNKNOWN CPU!!!"
537#endif
538
539}
540
[637df35]541/*PAGE
542 *
543 *  _CPU_Context_restore
544 */
545
[ac7d5ef0]546void _CPU_Context_restore(
547  Context_Control  *next
548)
549{
[37f4c2d]550  Context_Control_overlay *nextp = (Context_Control_overlay *)next;
551
[d196e48]552  _CPU_ISR_Enable(nextp->isr_level);
[37f4c2d]553  longjmp( nextp->regs, 0 );
[ac7d5ef0]554}
555
[637df35]556/*PAGE
557 *
558 *  _CPU_Context_switch
559 */
560
[d196e48]561static void do_jump(
562  Context_Control_overlay *currentp,
563  Context_Control_overlay *nextp
564);
565
[ac7d5ef0]566void _CPU_Context_switch(
567  Context_Control  *current,
568  Context_Control  *next
569)
570{
[37f4c2d]571  Context_Control_overlay *currentp = (Context_Control_overlay *)current;
572  Context_Control_overlay *nextp = (Context_Control_overlay *)next;
[d196e48]573#if 0
[3652ad35]574  int status;
[d196e48]575#endif
[d1193c7]576
[d196e48]577  currentp->isr_level = _CPU_ISR_Disable_support();
[d1193c7]578
[d196e48]579  do_jump( currentp, nextp );
[3652ad35]580
[d196e48]581#if 0
582  if (sigsetjmp(currentp->regs, 1) == 0) {    /* Save the current context */
583     siglongjmp(nextp->regs, 0);           /* Switch to the new context */
584     _Internal_error_Occurred(
585         INTERNAL_ERROR_CORE,
586         TRUE,
587         status
588       );
589  }
590#endif
[d1193c7]591
[d196e48]592#ifdef RTEMS_DEBUG
593    if (_CPU_ISR_Get_level() == 0)
594       abort();
595#endif
[d1193c7]596
[d196e48]597  _CPU_ISR_Enable(currentp->isr_level);
598}
[d1193c7]599
600static void do_jump(
[d196e48]601  Context_Control_overlay *currentp,
[d1193c7]602  Context_Control_overlay *nextp
[d196e48]603)
604{
605  int status;
[ac7d5ef0]606
[37f4c2d]607  if (setjmp(currentp->regs) == 0) {    /* Save the current context */
608     longjmp(nextp->regs, 0);           /* Switch to the new context */
[d196e48]609     _Internal_error_Occurred(
[3652ad35]610         INTERNAL_ERROR_CORE,
611         TRUE,
612         status
613       );
[637df35]614  }
[ac7d5ef0]615}
[d196e48]616
[637df35]617/*PAGE
618 *
619 *  _CPU_Save_float_context
620 */
[ac7d5ef0]621
622void _CPU_Save_float_context(
623  Context_Control_fp *fp_context
624)
625{
626}
627
[637df35]628/*PAGE
629 *
630 *  _CPU_Restore_float_context
631 */
632
[ac7d5ef0]633void _CPU_Restore_float_context(
634  Context_Control_fp *fp_context
635)
636{
637}
638
[637df35]639/*PAGE
640 *
641 *  _CPU_ISR_Disable_support
642 */
[ac7d5ef0]643
[637df35]644unsigned32 _CPU_ISR_Disable_support(void)
[ac7d5ef0]645{
[3652ad35]646  int status;
[637df35]647  sigset_t  old_mask;
[ac7d5ef0]648
[3652ad35]649  status = sigprocmask(SIG_BLOCK, &_CPU_Signal_mask, &old_mask);
650  if ( status )
651    _Internal_error_Occurred(
652      INTERNAL_ERROR_CORE,
653      TRUE,
654      status
655    );
[ac7d5ef0]656
[3652ad35]657  if (memcmp((void *)&posix_empty_mask, (void *)&old_mask, sizeof(sigset_t)))
[637df35]658    return 1;
[ac7d5ef0]659
[637df35]660  return 0;
[ac7d5ef0]661}
662
[637df35]663/*PAGE
664 *
665 *  _CPU_ISR_Enable
666 */
[ac7d5ef0]667
[637df35]668void _CPU_ISR_Enable(
669  unsigned32 level
670)
[ac7d5ef0]671{
[3652ad35]672  int status;
673
[637df35]674  if (level == 0)
[3652ad35]675    status = sigprocmask(SIG_UNBLOCK, &_CPU_Signal_mask, 0);
[637df35]676  else
[3652ad35]677    status = sigprocmask(SIG_BLOCK, &_CPU_Signal_mask, 0);
678
679  if ( status )
680    _Internal_error_Occurred(
681      INTERNAL_ERROR_CORE,
682      TRUE,
683      status
684    );
[ac7d5ef0]685}
686
[637df35]687/*PAGE
[ac7d5ef0]688 *
[637df35]689 *  _CPU_ISR_Handler
690 *
691 *  External interrupt handler.
692 *  This is installed as a UNIX signal handler.
693 *  It vectors out to specific user interrupt handlers.
[ac7d5ef0]694 */
695
[637df35]696void _CPU_ISR_Handler(int vector)
[ac7d5ef0]697{
[637df35]698  extern void        _Thread_Dispatch(void);
699  extern unsigned32  _Thread_Dispatch_disable_level;
700  extern boolean     _Context_Switch_necessary;
[ac7d5ef0]701
[637df35]702  if (_ISR_Nest_level++ == 0) {
703      /* switch to interrupt stack */
704  }
[ac7d5ef0]705
[637df35]706  _Thread_Dispatch_disable_level++;
[ac7d5ef0]707
[637df35]708  if (_ISR_Vector_table[vector]) {
709     _ISR_Vector_table[vector](vector);
710  } else {
711     _CPU_Stray_signal(vector);
712  }
[ac7d5ef0]713
[637df35]714  if (_ISR_Nest_level-- == 0) {
715      /* switch back to original stack */
716  }
[ac7d5ef0]717
[637df35]718  _Thread_Dispatch_disable_level--;
[ac7d5ef0]719
[637df35]720  if (_Thread_Dispatch_disable_level == 0 &&
721      (_Context_Switch_necessary || _ISR_Signals_to_thread_executing)) {
[8a38f3b]722      _ISR_Signals_to_thread_executing = FALSE;
[637df35]723      _CPU_ISR_Enable(0);
724      _Thread_Dispatch();
725  }
[ac7d5ef0]726}
727
[637df35]728/*PAGE
729 *
730 *  _CPU_Stray_signal
731 */
[ac7d5ef0]732
[637df35]733void _CPU_Stray_signal(int sig_num)
[ac7d5ef0]734{
[c64e4ed4]735  char buffer[ 4 ];
[d1193c7]736
[c64e4ed4]737  /*
738   * print "stray" msg about ones which that might mean something
739   * Avoid using the stdio section of the library.
740   * The following is generally safe.
[637df35]741   */
[d1193c7]742
[c64e4ed4]743  switch (sig_num)
744  {
745      case SIGCLD:
746          break;
[d1193c7]747
[c64e4ed4]748      default:
749      {
[cc4c1fe4]750        /*
751         *  We avoid using the stdio section of the library.
752         *  The following is generally safe
753         */
[d1193c7]754
[cc4c1fe4]755        int digit;
756        int number = sig_num;
757        int len = 0;
758
759        digit = number / 100;
760        number %= 100;
761        if (digit) buffer[len++] = '0' + digit;
762
763        digit = number / 10;
764        number %= 10;
765        if (digit || len) buffer[len++] = '0' + digit;
766
767        digit = number;
768        buffer[len++] = '0' + digit;
[d1193c7]769
[cc4c1fe4]770        buffer[ len++ ] = '\n';
[d1193c7]771
[cc4c1fe4]772        write( 2, "Stray signal ", 13 );
773        write( 2, buffer, len );
774
[c64e4ed4]775      }
776  }
[d1193c7]777
[637df35]778  /*
779   * If it was a "fatal" signal, then exit here
780   * If app code has installed a hander for one of these, then
781   * we won't call _CPU_Stray_signal, so this is ok.
782   */
[d1193c7]783
[637df35]784  switch (sig_num) {
785      case SIGINT:
786      case SIGHUP:
787      case SIGQUIT:
788      case SIGILL:
[10aed1e3]789#ifdef SIGEMT
[637df35]790      case SIGEMT:
[10aed1e3]791#endif
[637df35]792      case SIGKILL:
793      case SIGBUS:
794      case SIGSEGV:
795      case SIGTERM:
[d196e48]796      case SIGIOT:
[cc4c1fe4]797        _CPU_Fatal_error(0x100 + sig_num);
[637df35]798  }
[ac7d5ef0]799}
800
[637df35]801/*PAGE
802 *
803 *  _CPU_Fatal_error
804 */
[ac7d5ef0]805
[637df35]806void _CPU_Fatal_error(unsigned32 error)
[ac7d5ef0]807{
[637df35]808  setitimer(ITIMER_REAL, 0, 0);
[ac7d5ef0]809
[e7e016f]810  if ( error ) {
811#ifdef RTEMS_DEBUG
812    abort();
813#endif
814    if (getenv("RTEMS_DEBUG"))
815      abort();
816  }
817
[637df35]818  _exit(error);
[ac7d5ef0]819}
820
[37f4c2d]821/*
822 *  Special Purpose Routines to hide the use of UNIX system calls.
823 */
824
[0a6fb22]825int _CPU_Set_sync_io_handler(
826  int fd,
827  boolean read,
828  boolean write,
829  boolean except,
830  rtems_sync_io_handler handler
831)
832{
833  if ((fd < FD_SETSIZE) && (_CPU_Sync_io_handlers[fd] == NULL)) {
834    if (read)
835      FD_SET(fd, &sync_io_readfds);
836    else
837      FD_CLR(fd, &sync_io_readfds);
838    if (write)
839      FD_SET(fd, &sync_io_writefds);
840    else
841      FD_CLR(fd, &sync_io_writefds);
842    if (except)
843      FD_SET(fd, &sync_io_exceptfds);
844    else
845      FD_CLR(fd, &sync_io_exceptfds);
846    _CPU_Sync_io_handlers[fd] = handler;
847    if ((fd + 1) > sync_io_nfds)
848      sync_io_nfds = fd + 1;
849    return 0;
850  }
851  return -1;
852}
853
854int _CPU_Clear_sync_io_handler(
855  int fd
856)
857{
858  if ((fd < FD_SETSIZE) && _CPU_Sync_io_handlers[fd]) {
859    FD_CLR(fd, &sync_io_readfds);
860    FD_CLR(fd, &sync_io_writefds);
861    FD_CLR(fd, &sync_io_exceptfds);
862    _CPU_Sync_io_handlers[fd] = NULL;
863    sync_io_nfds = 0;
864    for (fd = 0; fd < FD_SETSIZE; fd++)
865      if (FD_ISSET(fd, &sync_io_readfds) ||
866          FD_ISSET(fd, &sync_io_writefds) ||
867          FD_ISSET(fd, &sync_io_exceptfds))
868        sync_io_nfds = fd;
869    return 0;
870  }
871  return -1;
872}
873
[37f4c2d]874int _CPU_Get_clock_vector( void )
875{
876  return SIGALRM;
877}
878
[d1193c7]879void _CPU_Start_clock(
[37f4c2d]880  int microseconds
881)
882{
883  struct itimerval  new;
884
885  new.it_value.tv_sec = 0;
886  new.it_value.tv_usec = microseconds;
887  new.it_interval.tv_sec = 0;
888  new.it_interval.tv_usec = microseconds;
889
890  setitimer(ITIMER_REAL, &new, 0);
891}
892
893void _CPU_Stop_clock( void )
894{
895  struct itimerval  new;
896  struct sigaction  act;
[d1193c7]897
[37f4c2d]898  /*
899   * Set the SIGALRM signal to ignore any last
900   * signals that might come in while we are
901   * disarming the timer and removing the interrupt
902   * vector.
903   */
[d1193c7]904
[cc4c1fe4]905  (void) memset(&act, 0, sizeof(act));
[37f4c2d]906  act.sa_handler = SIG_IGN;
[d1193c7]907
[cc4c1fe4]908  sigaction(SIGALRM, &act, 0);
[d1193c7]909
[cc4c1fe4]910  (void) memset(&new, 0, sizeof(new));
[37f4c2d]911  setitimer(ITIMER_REAL, &new, 0);
912}
913
914int  _CPU_SHM_Semid;
915extern       void fix_syscall_errno( void );
916
[d1193c7]917void _CPU_SHM_Init(
[37f4c2d]918  unsigned32   maximum_nodes,
919  boolean      is_master_node,
920  void       **shm_address,
921  unsigned32  *shm_length
922)
923{
924  int          i;
925  int          shmid;
926  char        *shm_addr;
927  key_t        shm_key;
928  key_t        sem_key;
[d6ba279]929  int          status = 0;  /* to avoid unitialized warnings */
[37f4c2d]930  int          shm_size;
[d1193c7]931
[37f4c2d]932  if (getenv("RTEMS_SHM_KEY"))
933    shm_key = strtol(getenv("RTEMS_SHM_KEY"), 0, 0);
934  else
935#ifdef RTEMS_SHM_KEY
936    shm_key = RTEMS_SHM_KEY;
937#else
938    shm_key = 0xa000;
939#endif
[d1193c7]940
[37f4c2d]941    if (getenv("RTEMS_SHM_SIZE"))
942      shm_size = strtol(getenv("RTEMS_SHM_SIZE"), 0, 0);
943    else
944#ifdef RTEMS_SHM_SIZE
945      shm_size = RTEMS_SHM_SIZE;
946#else
947      shm_size = 64 * 1024;
948#endif
[d1193c7]949
[37f4c2d]950    if (getenv("RTEMS_SHM_SEMAPHORE_KEY"))
951      sem_key = strtol(getenv("RTEMS_SHM_SEMAPHORE_KEY"), 0, 0);
952    else
953#ifdef RTEMS_SHM_SEMAPHORE_KEY
954      sem_key = RTEMS_SHM_SEMAPHORE_KEY;
955#else
956      sem_key = 0xa001;
957#endif
[d1193c7]958
[37f4c2d]959    shmid = shmget(shm_key, shm_size, IPC_CREAT | 0660);
960    if ( shmid == -1 ) {
961      fix_syscall_errno(); /* in case of newlib */
962      perror( "shmget" );
963      _CPU_Fatal_halt( 0xdead0001 );
964    }
[d1193c7]965
[37f4c2d]966    shm_addr = shmat(shmid, (char *)0, SHM_RND);
967    if ( shm_addr == (void *)-1 ) {
968      fix_syscall_errno(); /* in case of newlib */
969      perror( "shmat" );
970      _CPU_Fatal_halt( 0xdead0002 );
971    }
[d1193c7]972
[37f4c2d]973    _CPU_SHM_Semid = semget(sem_key, maximum_nodes + 1, IPC_CREAT | 0660);
974    if ( _CPU_SHM_Semid == -1 ) {
975      fix_syscall_errno(); /* in case of newlib */
976      perror( "semget" );
977      _CPU_Fatal_halt( 0xdead0003 );
978    }
[d1193c7]979
[37f4c2d]980    if ( is_master_node ) {
981      for ( i=0 ; i <= maximum_nodes ; i++ ) {
982#if defined(solaris2)
983        union semun {
984          int val;
985          struct semid_ds *buf;
986          ushort *array;
987        } help;
[d1193c7]988
[37f4c2d]989        help.val = 1;
990        status = semctl( _CPU_SHM_Semid, i, SETVAL, help );
[3a85d03d]991#elif defined(__linux__)
992        union semun help;
993
994        help.val = 1;
995        status = semctl( _CPU_SHM_Semid, i, SETVAL, help );
996#elif defined(hpux)
[37f4c2d]997        status = semctl( _CPU_SHM_Semid, i, SETVAL, 1 );
[f2545552]998#else
999#error "Not a supported unix variant"
[37f4c2d]1000#endif
[d1193c7]1001
[37f4c2d]1002        fix_syscall_errno(); /* in case of newlib */
1003        if ( status == -1 ) {
1004          _CPU_Fatal_halt( 0xdead0004 );
1005        }
1006      }
1007    }
[d1193c7]1008
[37f4c2d]1009  *shm_address = shm_addr;
1010  *shm_length = shm_size;
1011
1012}
1013
1014int _CPU_Get_pid( void )
1015{
1016  return getpid();
1017}
1018
1019/*
1020 * Define this to use signals for MPCI shared memory driver.
1021 * If undefined, the shared memory driver will poll from the
1022 * clock interrupt.
1023 * Ref: ../shmsupp/getcfg.c
1024 *
1025 * BEWARE:: many UN*X kernels and debuggers become severely confused when
1026 *          debugging programs which use signals.  The problem is *much*
1027 *          worse when using multiple signals, since ptrace(2) tends to
1028 *          drop all signals except 1 in the case of multiples.
1029 *          On hpux9, this problem was so bad, we couldn't use interrupts
1030 *          with the shared memory driver if we ever hoped to debug
1031 *          RTEMS programs.
1032 *          Maybe systems that use /proc don't have this problem...
1033 */
[d1193c7]1034
1035
[37f4c2d]1036int _CPU_SHM_Get_vector( void )
1037{
1038#ifdef CPU_USE_SHM_INTERRUPTS
1039  return SIGUSR1;
1040#else
1041  return 0;
1042#endif
1043}
1044
1045void _CPU_SHM_Send_interrupt(
1046  int pid,
1047  int vector
1048)
1049{
1050  kill((pid_t) pid, vector);
1051}
1052
[d1193c7]1053void _CPU_SHM_Lock(
[37f4c2d]1054  int semaphore
1055)
1056{
[0a6fb22]1057  struct sembuf sb;
[d1193c7]1058
[37f4c2d]1059  sb.sem_num = semaphore;
1060  sb.sem_op  = -1;
1061  sb.sem_flg = 0;
[d1193c7]1062
[37f4c2d]1063  while (1) {
[0a6fb22]1064    int status = -1;
1065
[37f4c2d]1066    status = semop(_CPU_SHM_Semid, &sb, 1);
1067    if ( status >= 0 )
1068      break;
1069    if ( status == -1 ) {
1070       fix_syscall_errno();    /* in case of newlib */
1071        if (errno == EINTR)
1072            continue;
1073        perror("shm lock");
1074        _CPU_Fatal_halt( 0xdead0005 );
1075    }
1076  }
1077
1078}
1079
1080void _CPU_SHM_Unlock(
1081  int semaphore
1082)
1083{
1084  struct sembuf  sb;
1085  int            status;
[d1193c7]1086
[37f4c2d]1087  sb.sem_num = semaphore;
1088  sb.sem_op  = 1;
1089  sb.sem_flg = 0;
[d1193c7]1090
[37f4c2d]1091  while (1) {
1092    status = semop(_CPU_SHM_Semid, &sb, 1);
1093    if ( status >= 0 )
1094      break;
[d1193c7]1095
[37f4c2d]1096    if ( status == -1 ) {
1097      fix_syscall_errno();    /* in case of newlib */
1098      if (errno == EINTR)
1099          continue;
1100      perror("shm unlock");
1101      _CPU_Fatal_halt( 0xdead0006 );
1102    }
1103  }
1104
1105}
Note: See TracBrowser for help on using the repository browser.