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

4.104.114.84.95
Last change on this file since d196e48 was d196e48, checked in by Joel Sherrill <joel.sherrill@…>, on 05/23/96 at 15:34:54

updates from Tony Bennett.

Broke the setjmp/longjmp pair in the context switch into a separate routine
so no code depended on local variables surviving the jump.

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