source: rtems/cpukit/score/cpu/unix/cpu.c @ ddf142d

4.104.114.84.95
Last change on this file since ddf142d was ddf142d, checked in by Joel Sherrill <joel.sherrill@…>, on Mar 24, 1998 at 8:53:48 PM

Added _XOPEN_SOURCE definition to allow use of UNIX IPC.

  • 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  unsigned32  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
247  sigprocmask(SIG_BLOCK, 0, &old_mask);
248
249  if (memcmp((void *)&posix_empty_mask, (void *)&old_mask, sizeof(sigset_t)))
250      return 1;
251
252  return 0;
253}
254
255/*  _CPU_Initialize
256 *
257 *  This routine performs processor dependent initialization.
258 *
259 *  INPUT PARAMETERS:
260 *    cpu_table       - CPU table to initialize
261 *    thread_dispatch - address of disptaching routine
262 */
263
264
265void _CPU_Initialize(
266  rtems_cpu_table  *cpu_table,
267  void            (*thread_dispatch)      /* ignored on this CPU */
268)
269{
270  /*
271   *  The thread_dispatch argument is the address of the entry point
272   *  for the routine called at the end of an ISR once it has been
273   *  decided a context switch is necessary.  On some compilation
274   *  systems it is difficult to call a high-level language routine
275   *  from assembly.  This allows us to trick these systems.
276   *
277   *  If you encounter this problem save the entry point in a CPU
278   *  dependent variable.
279   */
280
281  _CPU_Thread_dispatch_pointer = thread_dispatch;
282
283  /*
284   * XXX; If there is not an easy way to initialize the FP context
285   *      during Context_Initialize, then it is usually easier to
286   *      save an "uninitialized" FP context here and copy it to
287   *      the task's during Context_Initialize.
288   */
289
290  /* XXX: FP context initialization support */
291
292  _CPU_Table = *cpu_table;
293
294  _CPU_ISR_From_CPU_Init();
295
296  _CPU_Sync_io_Init();
297
298  _CPU_Context_From_CPU_Init();
299
300}
301
302/*PAGE
303 *
304 *  _CPU_ISR_install_raw_handler
305 */
306
307void _CPU_ISR_install_raw_handler(
308  unsigned32  vector,
309  proc_ptr    new_handler,
310  proc_ptr   *old_handler
311)
312{
313  _CPU_Fatal_halt( 0xdeaddead );
314}
315
316/*PAGE
317 *
318 *  _CPU_ISR_install_vector
319 *
320 *  This kernel routine installs the RTEMS handler for the
321 *  specified vector.
322 *
323 *  Input parameters:
324 *    vector      - interrupt vector number
325 *    old_handler - former ISR for this vector number
326 *    new_handler - replacement ISR for this vector number
327 *
328 *  Output parameters:  NONE
329 *
330 */
331
332
333void _CPU_ISR_install_vector(
334  unsigned32  vector,
335  proc_ptr    new_handler,
336  proc_ptr   *old_handler
337)
338{
339   *old_handler = _ISR_Vector_table[ vector ];
340
341   /*
342    *  If the interrupt vector table is a table of pointer to isr entry
343    *  points, then we need to install the appropriate RTEMS interrupt
344    *  handler for this vector number.
345    */
346
347   /*
348    *  We put the actual user ISR address in '_ISR_vector_table'.  This will
349    *  be used by the _CPU_ISR_Handler so the user gets control.
350    */
351
352    _ISR_Vector_table[ vector ] = new_handler;
353}
354
355/*PAGE
356 *
357 *  _CPU_Install_interrupt_stack
358 */
359
360void _CPU_Install_interrupt_stack( void )
361{
362}
363
364/*PAGE
365 *
366 *  _CPU_Thread_Idle_body
367 *
368 *  Stop until we get a signal which is the logically the same thing
369 *  entering low-power or sleep mode on a real processor and waiting for
370 *  an interrupt.  This significantly reduces the consumption of host
371 *  CPU cycles which is again similar to low power mode.
372 */
373
374void _CPU_Thread_Idle_body( void )
375{
376#if CPU_SYNC_IO
377  extern void _Thread_Dispatch(void);
378  int fd;
379#endif
380
381  while (1) {
382#ifdef RTEMS_DEBUG
383    /* interrupts had better be enabled at this point! */
384    if (_CPU_ISR_Get_level() != 0)
385       abort();
386#endif
387
388    /*
389     *  Block on a select statement, the CPU interface added allow the
390     *  user to add new descriptors which are to be blocked on
391     */
392
393#if CPU_SYNC_IO
394    if (sync_io_nfds) {
395      int result;
396
397      result = select(sync_io_nfds,
398                 &sync_io_readfds,
399                 &sync_io_writefds,
400                 &sync_io_exceptfds,
401                 NULL);
402
403      if ((result < 0) && (errno != EINTR))
404        _CPU_Fatal_error(0x200);       /* FIXME : what number should go here !! */
405
406      for (fd = 0; fd < sync_io_nfds; fd++) {
407        boolean read = FD_ISSET(fd, &sync_io_readfds);
408        boolean write = FD_ISSET(fd, &sync_io_writefds);
409        boolean except = FD_ISSET(fd, &sync_io_exceptfds);
410
411        if (_CPU_Sync_io_handlers[fd] && (read || write || except))
412          _CPU_Sync_io_handlers[fd](fd, read, write, except);
413
414        _Thread_Dispatch();
415      }
416    } else
417      pause();
418#else
419    pause();
420#endif
421
422  }
423
424}
425
426/*PAGE
427 *
428 *  _CPU_Context_Initialize
429 */
430
431void _CPU_Context_Initialize(
432  Context_Control  *_the_context,
433  unsigned32       *_stack_base,
434  unsigned32        _size,
435  unsigned32        _new_level,
436  void             *_entry_point,
437  boolean           _is_fp
438)
439{
440  unsigned32  *addr;
441  unsigned32   jmp_addr;
442  unsigned32   _stack_low;   /* lowest "stack aligned" address */
443  unsigned32   _stack_high;  /* highest "stack aligned" address */
444  unsigned32   _the_size;
445
446  jmp_addr = (unsigned32) _entry_point;
447
448  /*
449   *  On CPUs with stacks which grow down, we build the stack
450   *  based on the _stack_high address.  On CPUs with stacks which
451   *  grow up, we build the stack based on the _stack_low address.
452   */
453
454  _stack_low = (unsigned32)(_stack_base) + CPU_STACK_ALIGNMENT - 1;
455  _stack_low &= ~(CPU_STACK_ALIGNMENT - 1);
456
457  _stack_high = (unsigned32)(_stack_base) + _size;
458  _stack_high &= ~(CPU_STACK_ALIGNMENT - 1);
459
460  if (_stack_high > _stack_low)
461    _the_size = _stack_high - _stack_low;
462  else
463    _the_size = _stack_low - _stack_high;
464
465  /*
466   * Slam our jmp_buf template into the context we are creating
467   */
468
469  if ( _new_level == 0 )
470      *_the_context = *(Context_Control *)
471                         &_CPU_Context_Default_with_ISRs_enabled;
472  else
473      *_the_context = *(Context_Control *)
474                         &_CPU_Context_Default_with_ISRs_disabled;
475
476  addr = (unsigned32 *)_the_context;
477
478#if defined(__hppa__)
479  *(addr + RP_OFF) = jmp_addr;
480  *(addr + SP_OFF) = (unsigned32)(_stack_low + CPU_FRAME_SIZE);
481
482  /*
483   * See if we are using shared libraries by checking
484   * bit 30 in 24 off of newp. If bit 30 is set then
485   * we are using shared libraries and the jump address
486   * points to the pointer, so we put that into rp instead.
487   */
488
489  if (jmp_addr & 0x40000000) {
490    jmp_addr &= 0xfffffffc;
491     *(addr + RP_OFF) = *(unsigned32 *)jmp_addr;
492  }
493#elif defined(__sparc__)
494
495  /*
496   *  See /usr/include/sys/stack.h in Solaris 2.3 for a nice
497   *  diagram of the stack.
498   */
499
500  asm ("ta  0x03");            /* flush registers */
501
502  *(addr + RP_OFF) = jmp_addr + ADDR_ADJ_OFFSET;
503  *(addr + SP_OFF) = (unsigned32)(_stack_high - CPU_FRAME_SIZE);
504  *(addr + FP_OFF) = (unsigned32)(_stack_high);
505
506#elif defined(__i386__)
507
508    /*
509     *  This information was gathered by disassembling setjmp().
510     */
511
512    {
513      unsigned32 stack_ptr;
514
515      stack_ptr = _stack_high - CPU_FRAME_SIZE;
516
517      *(addr + EBX_OFF) = 0xFEEDFEED;
518      *(addr + ESI_OFF) = 0xDEADDEAD;
519      *(addr + EDI_OFF) = 0xDEAFDEAF;
520      *(addr + EBP_OFF) = stack_ptr;
521      *(addr + ESP_OFF) = stack_ptr;
522      *(addr + RET_OFF) = jmp_addr;
523
524      addr = (unsigned32 *) stack_ptr;
525
526      addr[ 0 ] = jmp_addr;
527      addr[ 1 ] = (unsigned32) stack_ptr;
528      addr[ 2 ] = (unsigned32) stack_ptr;
529    }
530
531#else
532#error "UNKNOWN CPU!!!"
533#endif
534
535}
536
537/*PAGE
538 *
539 *  _CPU_Context_restore
540 */
541
542void _CPU_Context_restore(
543  Context_Control  *next
544)
545{
546  Context_Control_overlay *nextp = (Context_Control_overlay *)next;
547
548  _CPU_ISR_Enable(nextp->isr_level);
549  longjmp( nextp->regs, 0 );
550}
551
552/*PAGE
553 *
554 *  _CPU_Context_switch
555 */
556
557static void do_jump(
558  Context_Control_overlay *currentp,
559  Context_Control_overlay *nextp
560);
561
562void _CPU_Context_switch(
563  Context_Control  *current,
564  Context_Control  *next
565)
566{
567  Context_Control_overlay *currentp = (Context_Control_overlay *)current;
568  Context_Control_overlay *nextp = (Context_Control_overlay *)next;
569#if 0
570  int status;
571#endif
572
573  currentp->isr_level = _CPU_ISR_Disable_support();
574
575  do_jump( currentp, nextp );
576
577#if 0
578  if (sigsetjmp(currentp->regs, 1) == 0) {    /* Save the current context */
579     siglongjmp(nextp->regs, 0);           /* Switch to the new context */
580     _Internal_error_Occurred(
581         INTERNAL_ERROR_CORE,
582         TRUE,
583         status
584       );
585  }
586#endif
587
588#ifdef RTEMS_DEBUG
589    if (_CPU_ISR_Get_level() == 0)
590       abort();
591#endif
592
593  _CPU_ISR_Enable(currentp->isr_level);
594}
595
596static void do_jump(
597  Context_Control_overlay *currentp,
598  Context_Control_overlay *nextp
599)
600{
601  int status;
602
603  if (setjmp(currentp->regs) == 0) {    /* Save the current context */
604     longjmp(nextp->regs, 0);           /* Switch to the new context */
605     _Internal_error_Occurred(
606         INTERNAL_ERROR_CORE,
607         TRUE,
608         status
609       );
610  }
611}
612
613/*PAGE
614 *
615 *  _CPU_Save_float_context
616 */
617
618void _CPU_Save_float_context(
619  Context_Control_fp *fp_context
620)
621{
622}
623
624/*PAGE
625 *
626 *  _CPU_Restore_float_context
627 */
628
629void _CPU_Restore_float_context(
630  Context_Control_fp *fp_context
631)
632{
633}
634
635/*PAGE
636 *
637 *  _CPU_ISR_Disable_support
638 */
639
640unsigned32 _CPU_ISR_Disable_support(void)
641{
642  int status;
643  sigset_t  old_mask;
644
645  status = sigprocmask(SIG_BLOCK, &_CPU_Signal_mask, &old_mask);
646  if ( status )
647    _Internal_error_Occurred(
648      INTERNAL_ERROR_CORE,
649      TRUE,
650      status
651    );
652
653  if (memcmp((void *)&posix_empty_mask, (void *)&old_mask, sizeof(sigset_t)))
654    return 1;
655
656  return 0;
657}
658
659/*PAGE
660 *
661 *  _CPU_ISR_Enable
662 */
663
664void _CPU_ISR_Enable(
665  unsigned32 level
666)
667{
668  int status;
669
670  if (level == 0)
671    status = sigprocmask(SIG_UNBLOCK, &_CPU_Signal_mask, 0);
672  else
673    status = sigprocmask(SIG_BLOCK, &_CPU_Signal_mask, 0);
674
675  if ( status )
676    _Internal_error_Occurred(
677      INTERNAL_ERROR_CORE,
678      TRUE,
679      status
680    );
681}
682
683/*PAGE
684 *
685 *  _CPU_ISR_Handler
686 *
687 *  External interrupt handler.
688 *  This is installed as a UNIX signal handler.
689 *  It vectors out to specific user interrupt handlers.
690 */
691
692void _CPU_ISR_Handler(int vector)
693{
694  extern void        _Thread_Dispatch(void);
695  extern unsigned32  _Thread_Dispatch_disable_level;
696  extern boolean     _Context_Switch_necessary;
697
698  if (_ISR_Nest_level++ == 0) {
699      /* switch to interrupt stack */
700  }
701
702  _Thread_Dispatch_disable_level++;
703
704  if (_ISR_Vector_table[vector]) {
705     _ISR_Vector_table[vector](vector);
706  } else {
707     _CPU_Stray_signal(vector);
708  }
709
710  if (_ISR_Nest_level-- == 0) {
711      /* switch back to original stack */
712  }
713
714  _Thread_Dispatch_disable_level--;
715
716  if (_Thread_Dispatch_disable_level == 0 &&
717      (_Context_Switch_necessary || _ISR_Signals_to_thread_executing)) {
718      _ISR_Signals_to_thread_executing = FALSE;
719      _CPU_ISR_Enable(0);
720      _Thread_Dispatch();
721  }
722}
723
724/*PAGE
725 *
726 *  _CPU_Stray_signal
727 */
728
729void _CPU_Stray_signal(int sig_num)
730{
731  char buffer[ 4 ];
732
733  /*
734   * print "stray" msg about ones which that might mean something
735   * Avoid using the stdio section of the library.
736   * The following is generally safe.
737   */
738
739  switch (sig_num)
740  {
741      case SIGCLD:
742          break;
743
744      default:
745      {
746        /*
747         *  We avoid using the stdio section of the library.
748         *  The following is generally safe
749         */
750
751        int digit;
752        int number = sig_num;
753        int len = 0;
754
755        digit = number / 100;
756        number %= 100;
757        if (digit) buffer[len++] = '0' + digit;
758
759        digit = number / 10;
760        number %= 10;
761        if (digit || len) buffer[len++] = '0' + digit;
762
763        digit = number;
764        buffer[len++] = '0' + digit;
765
766        buffer[ len++ ] = '\n';
767
768        write( 2, "Stray signal ", 13 );
769        write( 2, buffer, len );
770
771      }
772  }
773
774  /*
775   * If it was a "fatal" signal, then exit here
776   * If app code has installed a hander for one of these, then
777   * we won't call _CPU_Stray_signal, so this is ok.
778   */
779
780  switch (sig_num) {
781      case SIGINT:
782      case SIGHUP:
783      case SIGQUIT:
784      case SIGILL:
785#ifdef SIGEMT
786      case SIGEMT:
787#endif
788      case SIGKILL:
789      case SIGBUS:
790      case SIGSEGV:
791      case SIGTERM:
792      case SIGIOT:
793        _CPU_Fatal_error(0x100 + sig_num);
794  }
795}
796
797/*PAGE
798 *
799 *  _CPU_Fatal_error
800 */
801
802void _CPU_Fatal_error(unsigned32 error)
803{
804  setitimer(ITIMER_REAL, 0, 0);
805
806  if ( error ) {
807#ifdef RTEMS_DEBUG
808    abort();
809#endif
810    if (getenv("RTEMS_DEBUG"))
811      abort();
812  }
813
814  _exit(error);
815}
816
817/*
818 *  Special Purpose Routines to hide the use of UNIX system calls.
819 */
820
821int _CPU_Set_sync_io_handler(
822  int fd,
823  boolean read,
824  boolean write,
825  boolean except,
826  rtems_sync_io_handler handler
827)
828{
829  if ((fd < FD_SETSIZE) && (_CPU_Sync_io_handlers[fd] == NULL)) {
830    if (read)
831      FD_SET(fd, &sync_io_readfds);
832    else
833      FD_CLR(fd, &sync_io_readfds);
834    if (write)
835      FD_SET(fd, &sync_io_writefds);
836    else
837      FD_CLR(fd, &sync_io_writefds);
838    if (except)
839      FD_SET(fd, &sync_io_exceptfds);
840    else
841      FD_CLR(fd, &sync_io_exceptfds);
842    _CPU_Sync_io_handlers[fd] = handler;
843    if ((fd + 1) > sync_io_nfds)
844      sync_io_nfds = fd + 1;
845    return 0;
846  }
847  return -1;
848}
849
850int _CPU_Clear_sync_io_handler(
851  int fd
852)
853{
854  if ((fd < FD_SETSIZE) && _CPU_Sync_io_handlers[fd]) {
855    FD_CLR(fd, &sync_io_readfds);
856    FD_CLR(fd, &sync_io_writefds);
857    FD_CLR(fd, &sync_io_exceptfds);
858    _CPU_Sync_io_handlers[fd] = NULL;
859    sync_io_nfds = 0;
860    for (fd = 0; fd < FD_SETSIZE; fd++)
861      if (FD_ISSET(fd, &sync_io_readfds) ||
862          FD_ISSET(fd, &sync_io_writefds) ||
863          FD_ISSET(fd, &sync_io_exceptfds))
864        sync_io_nfds = fd;
865    return 0;
866  }
867  return -1;
868}
869
870int _CPU_Get_clock_vector( void )
871{
872  return SIGALRM;
873}
874
875void _CPU_Start_clock(
876  int microseconds
877)
878{
879  struct itimerval  new;
880
881  new.it_value.tv_sec = 0;
882  new.it_value.tv_usec = microseconds;
883  new.it_interval.tv_sec = 0;
884  new.it_interval.tv_usec = microseconds;
885
886  setitimer(ITIMER_REAL, &new, 0);
887}
888
889void _CPU_Stop_clock( void )
890{
891  struct itimerval  new;
892  struct sigaction  act;
893
894  /*
895   * Set the SIGALRM signal to ignore any last
896   * signals that might come in while we are
897   * disarming the timer and removing the interrupt
898   * vector.
899   */
900
901  (void) memset(&act, 0, sizeof(act));
902  act.sa_handler = SIG_IGN;
903
904  sigaction(SIGALRM, &act, 0);
905
906  (void) memset(&new, 0, sizeof(new));
907  setitimer(ITIMER_REAL, &new, 0);
908}
909
910int  _CPU_SHM_Semid;
911extern       void fix_syscall_errno( void );
912
913void _CPU_SHM_Init(
914  unsigned32   maximum_nodes,
915  boolean      is_master_node,
916  void       **shm_address,
917  unsigned32  *shm_length
918)
919{
920  int          i;
921  int          shmid;
922  char        *shm_addr;
923  key_t        shm_key;
924  key_t        sem_key;
925  int          status = 0;  /* to avoid unitialized warnings */
926  int          shm_size;
927
928  if (getenv("RTEMS_SHM_KEY"))
929    shm_key = strtol(getenv("RTEMS_SHM_KEY"), 0, 0);
930  else
931#ifdef RTEMS_SHM_KEY
932    shm_key = RTEMS_SHM_KEY;
933#else
934    shm_key = 0xa000;
935#endif
936
937    if (getenv("RTEMS_SHM_SIZE"))
938      shm_size = strtol(getenv("RTEMS_SHM_SIZE"), 0, 0);
939    else
940#ifdef RTEMS_SHM_SIZE
941      shm_size = RTEMS_SHM_SIZE;
942#else
943      shm_size = 64 * 1024;
944#endif
945
946    if (getenv("RTEMS_SHM_SEMAPHORE_KEY"))
947      sem_key = strtol(getenv("RTEMS_SHM_SEMAPHORE_KEY"), 0, 0);
948    else
949#ifdef RTEMS_SHM_SEMAPHORE_KEY
950      sem_key = RTEMS_SHM_SEMAPHORE_KEY;
951#else
952      sem_key = 0xa001;
953#endif
954
955    shmid = shmget(shm_key, shm_size, IPC_CREAT | 0660);
956    if ( shmid == -1 ) {
957      fix_syscall_errno(); /* in case of newlib */
958      perror( "shmget" );
959      _CPU_Fatal_halt( 0xdead0001 );
960    }
961
962    shm_addr = shmat(shmid, (char *)0, SHM_RND);
963    if ( shm_addr == (void *)-1 ) {
964      fix_syscall_errno(); /* in case of newlib */
965      perror( "shmat" );
966      _CPU_Fatal_halt( 0xdead0002 );
967    }
968
969    _CPU_SHM_Semid = semget(sem_key, maximum_nodes + 1, IPC_CREAT | 0660);
970    if ( _CPU_SHM_Semid == -1 ) {
971      fix_syscall_errno(); /* in case of newlib */
972      perror( "semget" );
973      _CPU_Fatal_halt( 0xdead0003 );
974    }
975
976    if ( is_master_node ) {
977      for ( i=0 ; i <= maximum_nodes ; i++ ) {
978#if defined(solaris2)
979        union semun {
980          int val;
981          struct semid_ds *buf;
982          ushort *array;
983        } help;
984
985        help.val = 1;
986        status = semctl( _CPU_SHM_Semid, i, SETVAL, help );
987#elif defined(__linux__)
988        union semun help;
989
990        help.val = 1;
991        status = semctl( _CPU_SHM_Semid, i, SETVAL, help );
992#elif defined(hpux)
993        status = semctl( _CPU_SHM_Semid, i, SETVAL, 1 );
994#else
995#error "Not a supported unix variant"
996#endif
997
998        fix_syscall_errno(); /* in case of newlib */
999        if ( status == -1 ) {
1000          _CPU_Fatal_halt( 0xdead0004 );
1001        }
1002      }
1003    }
1004
1005  *shm_address = shm_addr;
1006  *shm_length = shm_size;
1007
1008}
1009
1010int _CPU_Get_pid( void )
1011{
1012  return getpid();
1013}
1014
1015/*
1016 * Define this to use signals for MPCI shared memory driver.
1017 * If undefined, the shared memory driver will poll from the
1018 * clock interrupt.
1019 * Ref: ../shmsupp/getcfg.c
1020 *
1021 * BEWARE:: many UN*X kernels and debuggers become severely confused when
1022 *          debugging programs which use signals.  The problem is *much*
1023 *          worse when using multiple signals, since ptrace(2) tends to
1024 *          drop all signals except 1 in the case of multiples.
1025 *          On hpux9, this problem was so bad, we couldn't use interrupts
1026 *          with the shared memory driver if we ever hoped to debug
1027 *          RTEMS programs.
1028 *          Maybe systems that use /proc don't have this problem...
1029 */
1030
1031
1032int _CPU_SHM_Get_vector( void )
1033{
1034#ifdef CPU_USE_SHM_INTERRUPTS
1035  return SIGUSR1;
1036#else
1037  return 0;
1038#endif
1039}
1040
1041void _CPU_SHM_Send_interrupt(
1042  int pid,
1043  int vector
1044)
1045{
1046  kill((pid_t) pid, vector);
1047}
1048
1049void _CPU_SHM_Lock(
1050  int semaphore
1051)
1052{
1053  struct sembuf sb;
1054
1055  sb.sem_num = semaphore;
1056  sb.sem_op  = -1;
1057  sb.sem_flg = 0;
1058
1059  while (1) {
1060    int status = -1;
1061
1062    status = semop(_CPU_SHM_Semid, &sb, 1);
1063    if ( status >= 0 )
1064      break;
1065    if ( status == -1 ) {
1066       fix_syscall_errno();    /* in case of newlib */
1067        if (errno == EINTR)
1068            continue;
1069        perror("shm lock");
1070        _CPU_Fatal_halt( 0xdead0005 );
1071    }
1072  }
1073
1074}
1075
1076void _CPU_SHM_Unlock(
1077  int semaphore
1078)
1079{
1080  struct sembuf  sb;
1081  int            status;
1082
1083  sb.sem_num = semaphore;
1084  sb.sem_op  = 1;
1085  sb.sem_flg = 0;
1086
1087  while (1) {
1088    status = semop(_CPU_SHM_Semid, &sb, 1);
1089    if ( status >= 0 )
1090      break;
1091
1092    if ( status == -1 ) {
1093      fix_syscall_errno();    /* in case of newlib */
1094      if (errno == EINTR)
1095          continue;
1096      perror("shm unlock");
1097      _CPU_Fatal_halt( 0xdead0006 );
1098    }
1099  }
1100
1101}
Note: See TracBrowser for help on using the repository browser.