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

4.104.114.84.95
Last change on this file since d8cfebb was d8cfebb, checked in by Joel Sherrill <joel.sherrill@…>, on 06/21/07 at 18:33:22

2007-06-21 Joel Sherrill <joel.sherrill@…>

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