source: rtems/cpukit/score/cpu/unix/cpu.c @ 3c87adba

4.104.114.95
Last change on this file since 3c87adba was 3c87adba, checked in by Joel Sherrill <joel.sherrill@…>, on 07/31/08 at 14:55:56

2008-07-31 Joel Sherrill <joel.sherrill@…>

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