source: rtems/c/src/exec/score/cpu/unix/cpu.c @ ce691c51

4.104.114.84.95
Last change on this file since ce691c51 was 4381e50, checked in by Joel Sherrill <joel.sherrill@…>, on 04/14/98 at 22:44:49

Ralf Corsepius suggested a way to get rid of UNIX compiler files and use gcc-target-default.cfg

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