source: rtems/cpukit/score/cpu/unix/cpu.c @ 9a6994b

4.104.114.84.95
Last change on this file since 9a6994b was 9a6994b, checked in by Joel Sherrill <joel.sherrill@…>, on Jun 18, 1998 at 3:22:35 PM

Added freebsd support from Dario Alcocer <alcocer@…>.

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