source: rtems/cpukit/score/cpu/unix/cpu.c @ 58933e6

4.104.114.84.95
Last change on this file since 58933e6 was 5e34bf4, checked in by Joel Sherrill <joel.sherrill@…>, on Mar 31, 1998 at 2:19:27 PM

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
Line 
1/*
2 *  UNIX Simulator Dependent Source
3 *
4 *  COPYRIGHT (c) 1994,95 by Division Incorporated
5 *
6 *  The license and distribution terms for this file may be
7 *  found in the file LICENSE in this distribution or at
8 *  http://www.OARcorp.com/rtems/license.html.
9 *
10 *  $Id$
11 */
12
13#include <rtems/system.h>
14#include <rtems/score/isr.h>
15#include <rtems/score/interr.h>
16
17#if defined(__linux__)
18#define _XOPEN_SOURCE
19#define MALLOC_0_RETURNS_NULL
20#endif
21
22#include <sys/types.h>
23#include <sys/times.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <setjmp.h>
27#include <signal.h>
28#include <time.h>
29#include <sys/time.h>
30#include <errno.h>
31#include <unistd.h>
32#include <sys/ipc.h>
33#include <sys/shm.h>
34#include <sys/sem.h>
35#include <string.h>   /* memset */
36
37#ifndef SA_RESTART
38#define SA_RESTART 0
39#endif
40
41typedef struct {
42  jmp_buf   regs;
43  int  isr_level;
44} Context_Control_overlay;
45
46void  _CPU_Signal_initialize(void);
47void  _CPU_Stray_signal(int);
48void  _CPU_ISR_Handler(int);
49
50static sigset_t         _CPU_Signal_mask;
51static Context_Control_overlay
52          _CPU_Context_Default_with_ISRs_enabled CPU_STRUCTURE_ALIGNMENT;
53static Context_Control_overlay
54          _CPU_Context_Default_with_ISRs_disabled CPU_STRUCTURE_ALIGNMENT;
55
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
68/*
69 * Which cpu are we? Used by libcpu and libbsp.
70 */
71
72int cpu_number;
73
74/*PAGE
75 *
76 *  _CPU_ISR_From_CPU_Init
77 */
78
79sigset_t  posix_empty_mask;
80
81void _CPU_ISR_From_CPU_Init()
82{
83  unsigned32        i;
84  proc_ptr          old_handler;
85
86  /*
87   * Generate an empty mask to be used by disable_support
88   */
89
90  sigemptyset(&posix_empty_mask);
91
92  /*
93   * Block all the signals except SIGTRAP for the debugger
94   * and fatal error signals.
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);
102  (void) sigdelset(&_CPU_Signal_mask, SIGSEGV);
103  (void) sigdelset(&_CPU_Signal_mask, SIGBUS);
104  (void) sigdelset(&_CPU_Signal_mask, SIGFPE);
105
106  _CPU_ISR_Enable(1);
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;
125
126  /* mark them all active except for TraceTrap  and Abort */
127
128  mask = _CPU_Signal_mask;
129  sigprocmask(SIG_UNBLOCK, &mask, 0);
130
131  act.sa_handler = _CPU_ISR_Handler;
132  act.sa_mask = mask;
133  act.sa_flags = SA_RESTART;
134
135  sigaction(SIGHUP, &act, 0);
136  sigaction(SIGINT, &act, 0);
137  sigaction(SIGQUIT, &act, 0);
138  sigaction(SIGILL, &act, 0);
139#ifdef SIGEMT
140  sigaction(SIGEMT, &act, 0);
141#endif
142  sigaction(SIGFPE, &act, 0);
143  sigaction(SIGKILL, &act, 0);
144  sigaction(SIGBUS, &act, 0);
145  sigaction(SIGSEGV, &act, 0);
146#ifdef SIGSYS
147  sigaction(SIGSYS, &act, 0);
148#endif
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);
165#ifdef SIGLOST
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
178#if defined(__hppa__) && defined(RTEMS_UNIXLIB_SETJMP)
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
195
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
207  _CPU_ISR_Set_level( 0 );
208  _CPU_Context_switch(
209    (Context_Control *) &_CPU_Context_Default_with_ISRs_enabled,
210    (Context_Control *) &_CPU_Context_Default_with_ISRs_enabled
211  );
212
213  _CPU_ISR_Set_level( 1 );
214  _CPU_Context_switch(
215    (Context_Control *) &_CPU_Context_Default_with_ISRs_disabled,
216    (Context_Control *) &_CPU_Context_Default_with_ISRs_disabled
217  );
218}
219
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
238/*PAGE
239 *
240 *  _CPU_ISR_Get_level
241 */
242
243unsigned32 _CPU_ISR_Get_level( void )
244{
245  sigset_t old_mask;
246  sigset_t new_mask;
247
248#if defined(__linux__)
249  sigemptyset( &old_mask );
250#endif
251  sigprocmask(SIG_BLOCK, 0, &old_mask);
252
253  if (memcmp((void *)&posix_empty_mask, (void *)&old_mask, sizeof(sigset_t)))
254      return 1;
255
256  return 0;
257}
258
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,
271  void            (*thread_dispatch)      /* ignored on this CPU */
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
298  _CPU_ISR_From_CPU_Init();
299
300  _CPU_Sync_io_Init();
301
302  _CPU_Context_From_CPU_Init();
303
304}
305
306/*PAGE
307 *
308 *  _CPU_ISR_install_raw_handler
309 */
310
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 );
318}
319
320/*PAGE
321 *
322 *  _CPU_ISR_install_vector
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
353    *  be used by the _CPU_ISR_Handler so the user gets control.
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 *
370 *  _CPU_Thread_Idle_body
371 *
372 *  Stop until we get a signal which is the logically the same thing
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.
376 */
377
378void _CPU_Thread_Idle_body( void )
379{
380#if CPU_SYNC_IO
381  extern void _Thread_Dispatch(void);
382  int fd;
383#endif
384
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
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
423    pause();
424#endif
425
426  }
427
428}
429
430/*PAGE
431 *
432 *  _CPU_Context_Initialize
433 */
434
435void _CPU_Context_Initialize(
436  Context_Control  *_the_context,
437  unsigned32       *_stack_base,
438  unsigned32        _size,
439  unsigned32        _new_level,
440  void             *_entry_point,
441  boolean           _is_fp
442)
443{
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;
449
450  jmp_addr = (unsigned32) _entry_point;
451
452  /*
453   *  On CPUs with stacks which grow down, we build the stack
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.
456   */
457
458  _stack_low = (unsigned32)(_stack_base) + CPU_STACK_ALIGNMENT - 1;
459  _stack_low &= ~(CPU_STACK_ALIGNMENT - 1);
460
461  _stack_high = (unsigned32)(_stack_base) + _size;
462  _stack_high &= ~(CPU_STACK_ALIGNMENT - 1);
463
464  if (_stack_high > _stack_low)
465    _the_size = _stack_high - _stack_low;
466  else
467    _the_size = _stack_low - _stack_high;
468
469  /*
470   * Slam our jmp_buf template into the context we are creating
471   */
472
473  if ( _new_level == 0 )
474      *_the_context = *(Context_Control *)
475                         &_CPU_Context_Default_with_ISRs_enabled;
476  else
477      *_the_context = *(Context_Control *)
478                         &_CPU_Context_Default_with_ISRs_disabled;
479
480  addr = (unsigned32 *)_the_context;
481
482#if defined(__hppa__)
483  *(addr + RP_OFF) = jmp_addr;
484  *(addr + SP_OFF) = (unsigned32)(_stack_low + CPU_FRAME_SIZE);
485
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
490   * points to the pointer, so we put that into rp instead.
491   */
492
493  if (jmp_addr & 0x40000000) {
494    jmp_addr &= 0xfffffffc;
495     *(addr + RP_OFF) = *(unsigned32 *)jmp_addr;
496  }
497#elif defined(__sparc__)
498
499  /*
500   *  See /usr/include/sys/stack.h in Solaris 2.3 for a nice
501   *  diagram of the stack.
502   */
503
504  asm ("ta  0x03");            /* flush registers */
505
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);
509
510#elif defined(__i386__)
511
512    /*
513     *  This information was gathered by disassembling setjmp().
514     */
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;
527
528      addr = (unsigned32 *) stack_ptr;
529
530      addr[ 0 ] = jmp_addr;
531      addr[ 1 ] = (unsigned32) stack_ptr;
532      addr[ 2 ] = (unsigned32) stack_ptr;
533    }
534
535#else
536#error "UNKNOWN CPU!!!"
537#endif
538
539}
540
541/*PAGE
542 *
543 *  _CPU_Context_restore
544 */
545
546void _CPU_Context_restore(
547  Context_Control  *next
548)
549{
550  Context_Control_overlay *nextp = (Context_Control_overlay *)next;
551
552  _CPU_ISR_Enable(nextp->isr_level);
553  longjmp( nextp->regs, 0 );
554}
555
556/*PAGE
557 *
558 *  _CPU_Context_switch
559 */
560
561static void do_jump(
562  Context_Control_overlay *currentp,
563  Context_Control_overlay *nextp
564);
565
566void _CPU_Context_switch(
567  Context_Control  *current,
568  Context_Control  *next
569)
570{
571  Context_Control_overlay *currentp = (Context_Control_overlay *)current;
572  Context_Control_overlay *nextp = (Context_Control_overlay *)next;
573#if 0
574  int status;
575#endif
576
577  currentp->isr_level = _CPU_ISR_Disable_support();
578
579  do_jump( currentp, nextp );
580
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
591
592#ifdef RTEMS_DEBUG
593    if (_CPU_ISR_Get_level() == 0)
594       abort();
595#endif
596
597  _CPU_ISR_Enable(currentp->isr_level);
598}
599
600static void do_jump(
601  Context_Control_overlay *currentp,
602  Context_Control_overlay *nextp
603)
604{
605  int status;
606
607  if (setjmp(currentp->regs) == 0) {    /* Save the current context */
608     longjmp(nextp->regs, 0);           /* Switch to the new context */
609     _Internal_error_Occurred(
610         INTERNAL_ERROR_CORE,
611         TRUE,
612         status
613       );
614  }
615}
616
617/*PAGE
618 *
619 *  _CPU_Save_float_context
620 */
621
622void _CPU_Save_float_context(
623  Context_Control_fp *fp_context
624)
625{
626}
627
628/*PAGE
629 *
630 *  _CPU_Restore_float_context
631 */
632
633void _CPU_Restore_float_context(
634  Context_Control_fp *fp_context
635)
636{
637}
638
639/*PAGE
640 *
641 *  _CPU_ISR_Disable_support
642 */
643
644unsigned32 _CPU_ISR_Disable_support(void)
645{
646  int status;
647  sigset_t  old_mask;
648
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    );
656
657  if (memcmp((void *)&posix_empty_mask, (void *)&old_mask, sizeof(sigset_t)))
658    return 1;
659
660  return 0;
661}
662
663/*PAGE
664 *
665 *  _CPU_ISR_Enable
666 */
667
668void _CPU_ISR_Enable(
669  unsigned32 level
670)
671{
672  int status;
673
674  if (level == 0)
675    status = sigprocmask(SIG_UNBLOCK, &_CPU_Signal_mask, 0);
676  else
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    );
685}
686
687/*PAGE
688 *
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.
694 */
695
696void _CPU_ISR_Handler(int vector)
697{
698  extern void        _Thread_Dispatch(void);
699  extern unsigned32  _Thread_Dispatch_disable_level;
700  extern boolean     _Context_Switch_necessary;
701
702  if (_ISR_Nest_level++ == 0) {
703      /* switch to interrupt stack */
704  }
705
706  _Thread_Dispatch_disable_level++;
707
708  if (_ISR_Vector_table[vector]) {
709     _ISR_Vector_table[vector](vector);
710  } else {
711     _CPU_Stray_signal(vector);
712  }
713
714  if (_ISR_Nest_level-- == 0) {
715      /* switch back to original stack */
716  }
717
718  _Thread_Dispatch_disable_level--;
719
720  if (_Thread_Dispatch_disable_level == 0 &&
721      (_Context_Switch_necessary || _ISR_Signals_to_thread_executing)) {
722      _ISR_Signals_to_thread_executing = FALSE;
723      _CPU_ISR_Enable(0);
724      _Thread_Dispatch();
725  }
726}
727
728/*PAGE
729 *
730 *  _CPU_Stray_signal
731 */
732
733void _CPU_Stray_signal(int sig_num)
734{
735  char buffer[ 4 ];
736
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.
741   */
742
743  switch (sig_num)
744  {
745      case SIGCLD:
746          break;
747
748      default:
749      {
750        /*
751         *  We avoid using the stdio section of the library.
752         *  The following is generally safe
753         */
754
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;
769
770        buffer[ len++ ] = '\n';
771
772        write( 2, "Stray signal ", 13 );
773        write( 2, buffer, len );
774
775      }
776  }
777
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   */
783
784  switch (sig_num) {
785      case SIGINT:
786      case SIGHUP:
787      case SIGQUIT:
788      case SIGILL:
789#ifdef SIGEMT
790      case SIGEMT:
791#endif
792      case SIGKILL:
793      case SIGBUS:
794      case SIGSEGV:
795      case SIGTERM:
796      case SIGIOT:
797        _CPU_Fatal_error(0x100 + sig_num);
798  }
799}
800
801/*PAGE
802 *
803 *  _CPU_Fatal_error
804 */
805
806void _CPU_Fatal_error(unsigned32 error)
807{
808  setitimer(ITIMER_REAL, 0, 0);
809
810  if ( error ) {
811#ifdef RTEMS_DEBUG
812    abort();
813#endif
814    if (getenv("RTEMS_DEBUG"))
815      abort();
816  }
817
818  _exit(error);
819}
820
821/*
822 *  Special Purpose Routines to hide the use of UNIX system calls.
823 */
824
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
874int _CPU_Get_clock_vector( void )
875{
876  return SIGALRM;
877}
878
879void _CPU_Start_clock(
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;
897
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   */
904
905  (void) memset(&act, 0, sizeof(act));
906  act.sa_handler = SIG_IGN;
907
908  sigaction(SIGALRM, &act, 0);
909
910  (void) memset(&new, 0, sizeof(new));
911  setitimer(ITIMER_REAL, &new, 0);
912}
913
914int  _CPU_SHM_Semid;
915extern       void fix_syscall_errno( void );
916
917void _CPU_SHM_Init(
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;
929  int          status = 0;  /* to avoid unitialized warnings */
930  int          shm_size;
931
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
940
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
949
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
958
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    }
965
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    }
972
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    }
979
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;
988
989        help.val = 1;
990        status = semctl( _CPU_SHM_Semid, i, SETVAL, help );
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)
997        status = semctl( _CPU_SHM_Semid, i, SETVAL, 1 );
998#else
999#error "Not a supported unix variant"
1000#endif
1001
1002        fix_syscall_errno(); /* in case of newlib */
1003        if ( status == -1 ) {
1004          _CPU_Fatal_halt( 0xdead0004 );
1005        }
1006      }
1007    }
1008
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 */
1034
1035
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
1053void _CPU_SHM_Lock(
1054  int semaphore
1055)
1056{
1057  struct sembuf sb;
1058
1059  sb.sem_num = semaphore;
1060  sb.sem_op  = -1;
1061  sb.sem_flg = 0;
1062
1063  while (1) {
1064    int status = -1;
1065
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;
1086
1087  sb.sem_num = semaphore;
1088  sb.sem_op  = 1;
1089  sb.sem_flg = 0;
1090
1091  while (1) {
1092    status = semop(_CPU_SHM_Semid, &sb, 1);
1093    if ( status >= 0 )
1094      break;
1095
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.