Daily bump.
[official-gcc.git] / gcc / ada / init.c
blob0637b781ad1bb8ef2b5eb0a172acd7c4a085c8ee
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * I N I T *
6 * *
7 * $Revision: 1.7 $
8 * *
9 * C Implementation File *
10 * *
11 * Copyright (C) 1992-2001 Free Software Foundation, Inc. *
12 * *
13 * GNAT is free software; you can redistribute it and/or modify it under *
14 * terms of the GNU General Public License as published by the Free Soft- *
15 * ware Foundation; either version 2, or (at your option) any later ver- *
16 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
17 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
19 * for more details. You should have received a copy of the GNU General *
20 * Public License distributed with GNAT; see file COPYING. If not, write *
21 * to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, *
22 * MA 02111-1307, USA. *
23 * *
24 * As a special exception, if you link this file with other files to *
25 * produce an executable, this file does not by itself cause the resulting *
26 * executable to be covered by the GNU General Public License. This except- *
27 * ion does not however invalidate any other reasons why the executable *
28 * file might be covered by the GNU Public License. *
29 * *
30 * GNAT was originally developed by the GNAT team at New York University. *
31 * It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). *
32 * *
33 ****************************************************************************/
35 /* This unit contains initialization circuits that are system dependent. A
36 major part of the functionality involved involves stack overflow checking.
37 The GCC backend generates probe instructions to test for stack overflow.
38 For details on the exact approach used to generate these probes, see the
39 "Using and Porting GCC" manual, in particular the "Stack Checking" section
40 and the subsection "Specifying How Stack Checking is Done". The handlers
41 installed by this file are used to handle resulting signals that come
42 from these probes failing (i.e. touching protected pages) */
44 /* The following include is here to meet the published VxWorks requirement
45 that the __vxworks header appear before any other include. */
46 #ifdef __vxworks
47 #include "vxWorks.h"
48 #endif
50 #ifdef IN_RTS
51 #include "tconfig.h"
52 #include "tsystem.h"
53 #include <sys/stat.h>
54 #else
55 #include "config.h"
56 #include "system.h"
57 #endif
59 #include "adaint.h"
60 #include "raise.h"
62 extern void __gnat_raise_program_error (const char *, int);
64 /* Addresses of exception data blocks for predefined exceptions. */
65 extern struct Exception_Data constraint_error;
66 extern struct Exception_Data numeric_error;
67 extern struct Exception_Data program_error;
68 extern struct Exception_Data storage_error;
69 extern struct Exception_Data tasking_error;
70 extern struct Exception_Data _abort_signal;
72 #define Lock_Task system__soft_links__lock_task
73 extern void (*Lock_Task) PARAMS ((void));
75 #define Unlock_Task system__soft_links__unlock_task
76 extern void (*Unlock_Task) PARAMS ((void));
78 #define Get_Machine_State_Addr \
79 system__soft_links__get_machine_state_addr
80 extern struct Machine_State *(*Get_Machine_State_Addr) PARAMS ((void));
82 #define Check_Abort_Status \
83 system__soft_links__check_abort_status
84 extern int (*Check_Abort_Status) PARAMS ((void));
86 #define Raise_From_Signal_Handler \
87 ada__exceptions__raise_from_signal_handler
88 extern void Raise_From_Signal_Handler PARAMS ((struct Exception_Data *,
89 const char *));
91 #define Propagate_Signal_Exception \
92 __gnat_propagate_sig_exc
93 extern void Propagate_Signal_Exception
94 PARAMS ((struct Machine_State *, struct Exception_Data *, const char *));
96 /* Copies of global values computed by the binder */
97 int __gl_main_priority = -1;
98 int __gl_time_slice_val = -1;
99 char __gl_wc_encoding = 'n';
100 char __gl_locking_policy = ' ';
101 char __gl_queuing_policy = ' ';
102 char __gl_task_dispatching_policy = ' ';
103 int __gl_unreserve_all_interrupts = 0;
104 int __gl_exception_tracebacks = 0;
106 /* Indication of whether synchronous signal handler has already been
107 installed by a previous call to adainit */
108 int __gnat_handler_installed = 0;
110 /* HAVE_GNAT_INIT_FLOAT must be set on every targets where a __gnat_init_float
111 is defined. If this is not set them a void implementation will be defined
112 at the end of this unit. */
113 #undef HAVE_GNAT_INIT_FLOAT
115 /**********************/
116 /* __gnat_set_globals */
117 /**********************/
119 /* This routine is called from the binder generated main program. It copies
120 the values for global quantities computed by the binder into the following
121 global locations. The reason that we go through this copy, rather than just
122 define the global locations in the binder generated file, is that they are
123 referenced from the runtime, which may be in a shared library, and the
124 binder file is not in the shared library. Global references across library
125 boundaries like this are not handled correctly in all systems. */
127 void
128 __gnat_set_globals (main_priority, time_slice_val, wc_encoding, locking_policy,
129 queuing_policy, task_dispatching_policy, adafinal_ptr,
130 unreserve_all_interrupts, exception_tracebacks)
131 int main_priority;
132 int time_slice_val;
133 int wc_encoding;
134 int locking_policy, queuing_policy, task_dispatching_policy;
135 void (*adafinal_ptr) PARAMS ((void)) ATTRIBUTE_UNUSED;
136 int unreserve_all_interrupts, exception_tracebacks;
138 static int already_called = 0;
140 /* If this procedure has been already called once, check that the
141 arguments in this call are consistent with the ones in the previous
142 calls. Otherwise, raise a Program_Error exception.
144 We do not check for consistency of the wide character encoding
145 method. This default affects only Wide_Text_IO where no explicit
146 coding method is given, and there is no particular reason to let
147 this default be affected by the source representation of a library
148 in any case.
150 The value of main_priority is meaningful only when we are invoked
151 from the main program elaboration routine of an Ada application.
152 Checking the consistency of this parameter should therefore not be
153 done. Since it is assured that the main program elaboration will
154 always invoke this procedure before any library elaboration
155 routine, only the value of main_priority during the first call
156 should be taken into account and all the subsequent ones should be
157 ignored. Note that the case where the main program is not written
158 in Ada is also properly handled, since the default value will then
159 be used for this parameter.
161 For identical reasons, the consistency of time_slice_val should not
162 be checked. */
164 if (already_called)
166 if (__gl_locking_policy != locking_policy ||
167 __gl_queuing_policy != queuing_policy ||
168 __gl_task_dispatching_policy != task_dispatching_policy ||
169 __gl_unreserve_all_interrupts != unreserve_all_interrupts ||
170 __gl_exception_tracebacks != exception_tracebacks)
172 __gnat_raise_program_error (__FILE__, __LINE__);
174 return;
176 already_called = 1;
178 __gl_main_priority = main_priority;
179 __gl_time_slice_val = time_slice_val;
180 __gl_wc_encoding = wc_encoding;
181 __gl_locking_policy = locking_policy;
182 __gl_queuing_policy = queuing_policy;
183 __gl_task_dispatching_policy = task_dispatching_policy;
184 __gl_unreserve_all_interrupts = unreserve_all_interrupts;
185 __gl_exception_tracebacks = exception_tracebacks;
188 /*********************/
189 /* __gnat_initialize */
190 /*********************/
192 /* __gnat_initialize is called at the start of execution of an Ada program
193 (the call is generated by the binder). The standard routine does nothing
194 at all; the intention is that this be replaced by system specific
195 code where initialization is required. */
197 /***********************************/
198 /* __gnat_initialize (AIX version) */
199 /***********************************/
201 #if defined (_AIX)
203 /* AiX doesn't have SA_NODEFER */
205 #define SA_NODEFER 0
207 #include <sys/time.h>
209 /* AiX doesn't have nanosleep, but provides nsleep instead */
211 extern int nanosleep PARAMS ((struct timestruc_t *, struct timestruc_t *));
212 static void __gnat_error_handler PARAMS ((int));
215 nanosleep (Rqtp, Rmtp)
216 struct timestruc_t *Rqtp, *Rmtp;
218 return nsleep (Rqtp, Rmtp);
221 #include <signal.h>
223 static void
224 __gnat_error_handler (sig)
225 int sig;
227 struct Exception_Data *exception;
228 const char *msg;
230 switch (sig)
232 case SIGSEGV:
233 /* FIXME: we need to detect the case of a *real* SIGSEGV */
234 exception = &storage_error;
235 msg = "stack overflow or erroneous memory access";
236 break;
238 case SIGBUS:
239 exception = &constraint_error;
240 msg = "SIGBUS";
241 break;
243 case SIGFPE:
244 exception = &constraint_error;
245 msg = "SIGFPE";
246 break;
248 default:
249 exception = &program_error;
250 msg = "unhandled signal";
253 Raise_From_Signal_Handler (exception, msg);
256 void
257 __gnat_install_handler ()
259 struct sigaction act;
261 /* Set up signal handler to map synchronous signals to appropriate
262 exceptions. Make sure that the handler isn't interrupted by another
263 signal that might cause a scheduling event! */
265 act.sa_handler = __gnat_error_handler;
266 act.sa_flags = SA_NODEFER | SA_RESTART;
267 (void) sigemptyset (&act.sa_mask);
269 (void) sigaction (SIGABRT, &act, NULL);
270 (void) sigaction (SIGFPE, &act, NULL);
272 if (__gl_unreserve_all_interrupts == 0)
274 (void) sigaction (SIGILL, &act, NULL);
275 (void) sigaction (SIGSEGV, &act, NULL);
276 (void) sigaction (SIGBUS, &act, NULL);
278 __gnat_handler_installed = 1;
281 void
282 __gnat_initialize ()
286 /****************************************/
287 /* __gnat_initialize (Dec Unix version) */
288 /****************************************/
290 #elif defined(__alpha__) && defined(__osf__) && ! defined(__alpha_vxworks)
292 /* Note: it seems that __osf__ is defined for the Alpha VXWorks case. Not
293 clear that this is reasonable, but in any case we have to be sure to
294 exclude this case in the above test. */
296 #include <signal.h>
297 #include <sys/siginfo.h>
299 static void __gnat_error_handler PARAMS ((int, siginfo_t *,
300 struct sigcontext *));
301 extern char *__gnat_get_code_loc PARAMS ((struct sigcontext *));
302 extern void __gnat_enter_handler PARAMS ((struct sigcontext *, char *));
303 extern size_t __gnat_machine_state_length PARAMS ((void));
305 extern long exc_lookup_gp PARAMS ((char *));
306 extern void exc_resume PARAMS ((struct sigcontext *));
308 static void
309 __gnat_error_handler (sig, sip, context)
310 int sig;
311 siginfo_t *sip;
312 struct sigcontext *context;
314 struct Exception_Data *exception;
315 static int recurse = 0;
316 struct sigcontext *mstate;
317 const char *msg;
319 /* If this was an explicit signal from a "kill", just resignal it. */
320 if (SI_FROMUSER (sip))
322 signal (sig, SIG_DFL);
323 kill (getpid(), sig);
326 /* Otherwise, treat it as something we handle. */
327 switch (sig)
329 case SIGSEGV:
330 /* If the problem was permissions, this is a constraint error.
331 Likewise if the failing address isn't maximally aligned or if
332 we've recursed.
334 ??? Using a static variable here isn't task-safe, but it's
335 much too hard to do anything else and we're just determining
336 which exception to raise. */
337 if (sip->si_code == SEGV_ACCERR
338 || (((long) sip->si_addr) & 3) != 0
339 || recurse)
341 exception = &constraint_error;
342 msg = "SIGSEGV";
344 else
346 /* See if the page before the faulting page is accessible. Do that
347 by trying to access it. We'd like to simply try to access
348 4096 + the faulting address, but it's not guaranteed to be
349 the actual address, just to be on the same page. */
350 recurse++;
351 ((volatile char *)
352 ((long) sip->si_addr & - getpagesize ()))[getpagesize ()];
353 msg = "stack overflow (or erroneous memory access)";
354 exception = &storage_error;
356 break;
358 case SIGBUS:
359 exception = &program_error;
360 msg = "SIGBUS";
361 break;
363 case SIGFPE:
364 exception = &constraint_error;
365 msg = "SIGFPE";
366 break;
368 default:
369 exception = &program_error;
370 msg = "unhandled signal";
373 recurse = 0;
374 mstate = (struct sigcontext *) (*Get_Machine_State_Addr) ();
375 if (mstate != 0)
376 *mstate = *context;
378 Raise_From_Signal_Handler (exception, (char *) msg);
381 void
382 __gnat_install_handler ()
384 struct sigaction act;
386 /* Setup signal handler to map synchronous signals to appropriate
387 exceptions. Make sure that the handler isn't interrupted by another
388 signal that might cause a scheduling event! */
390 act.sa_handler = (void (*) PARAMS ((int))) __gnat_error_handler;
391 act.sa_flags = SA_ONSTACK | SA_RESTART | SA_NODEFER | SA_SIGINFO;
392 (void) sigemptyset (&act.sa_mask);
394 (void) sigaction (SIGABRT, &act, NULL);
395 (void) sigaction (SIGFPE, &act, NULL);
397 if (__gl_unreserve_all_interrupts == 0)
399 (void) sigaction (SIGILL, &act, NULL);
400 (void) sigaction (SIGSEGV, &act, NULL);
401 (void) sigaction (SIGBUS, &act, NULL);
404 __gnat_handler_installed = 1;
407 void
408 __gnat_initialize ()
412 /* Routines called by 5amastop.adb. */
414 #define SC_GP 29
416 char *
417 __gnat_get_code_loc (context)
418 struct sigcontext *context;
420 return (char *) context->sc_pc;
423 void
424 __gnat_enter_handler (context, pc)
425 struct sigcontext *context;
426 char *pc;
428 context->sc_pc = (long) pc;
429 context->sc_regs[SC_GP] = exc_lookup_gp (pc);
430 exc_resume (context);
433 size_t
434 __gnat_machine_state_length ()
436 return sizeof (struct sigcontext);
439 /***********************************/
440 /* __gnat_initialize (HPUX version) */
441 /***********************************/
443 #elif defined (hpux)
445 #include <signal.h>
447 static void __gnat_error_handler PARAMS ((int));
449 static void
450 __gnat_error_handler (sig)
451 int sig;
453 struct Exception_Data *exception;
454 char *msg;
456 switch (sig)
458 case SIGSEGV:
459 /* FIXME: we need to detect the case of a *real* SIGSEGV */
460 exception = &storage_error;
461 msg = "stack overflow or erroneous memory access";
462 break;
464 case SIGBUS:
465 exception = &constraint_error;
466 msg = "SIGBUS";
467 break;
469 case SIGFPE:
470 exception = &constraint_error;
471 msg = "SIGFPE";
472 break;
474 default:
475 exception = &program_error;
476 msg = "unhandled signal";
479 Raise_From_Signal_Handler (exception, msg);
482 void
483 __gnat_install_handler ()
485 struct sigaction act;
487 /* Set up signal handler to map synchronous signals to appropriate
488 exceptions. Make sure that the handler isn't interrupted by another
489 signal that might cause a scheduling event! Also setup an alternate
490 stack region for the handler execution so that stack overflows can be
491 handled properly, avoiding a SEGV generation from stack usage by the
492 handler itself. */
494 static char handler_stack [SIGSTKSZ];
496 stack_t stack;
498 stack.ss_sp = handler_stack;
499 stack.ss_size = SIGSTKSZ;
500 stack.ss_flags = 0;
502 (void) sigaltstack (&stack, NULL);
504 act.sa_handler = __gnat_error_handler;
505 act.sa_flags = SA_NODEFER | SA_RESTART | SA_ONSTACK;
506 (void) sigemptyset (&act.sa_mask);
508 (void) sigaction (SIGABRT, &act, NULL);
509 (void) sigaction (SIGFPE, &act, NULL);
511 if (__gl_unreserve_all_interrupts == 0)
513 (void) sigaction (SIGILL, &act, NULL);
514 (void) sigaction (SIGSEGV, &act, NULL);
515 (void) sigaction (SIGBUS, &act, NULL);
517 __gnat_handler_installed = 1;
520 void
521 __gnat_initialize ()
525 /*************************************/
526 /* __gnat_initialize (GNU/Linux version) */
527 /*************************************/
529 #elif defined (linux) && defined (i386) && !defined (__RT__)
531 #include <signal.h>
532 #include <asm/sigcontext.h>
534 /* GNU/Linux, which uses glibc, does not define NULL in included
535 header files */
537 #if !defined (NULL)
538 #define NULL ((void *) 0)
539 #endif
541 struct Machine_State
543 unsigned long eip;
544 unsigned long ebx;
545 unsigned long esp;
546 unsigned long ebp;
547 unsigned long esi;
548 unsigned long edi;
551 static void __gnat_error_handler PARAMS ((int));
553 static void
554 __gnat_error_handler (sig)
555 int sig;
557 struct Exception_Data *exception;
558 const char *msg;
559 static int recurse = 0;
561 struct sigcontext *info
562 = (struct sigcontext *) (((char *) &sig) + sizeof (int));
564 /* The Linux kernel does not document how to get the machine state in a
565 signal handler, but in fact the necessary data is in a sigcontext_struct
566 value that is on the stack immediately above the signal number
567 parameter, and the above messing accesses this value on the stack. */
569 struct Machine_State *mstate;
571 switch (sig)
573 case SIGSEGV:
574 /* If the problem was permissions, this is a constraint error.
575 Likewise if the failing address isn't maximally aligned or if
576 we've recursed.
578 ??? Using a static variable here isn't task-safe, but it's
579 much too hard to do anything else and we're just determining
580 which exception to raise. */
581 if (recurse)
583 exception = &constraint_error;
584 msg = "SIGSEGV";
586 else
588 /* Here we would like a discrimination test to see whether the
589 page before the faulting address is accessible. Unfortunately
590 Linux seems to have no way of giving us the faulting address.
592 In versions of a-init.c before 1.95, we had a test of the page
593 before the stack pointer using:
595 recurse++;
596 ((volatile char *)
597 ((long) info->esp_at_signal & - getpagesize ()))[getpagesize ()];
599 but that's wrong, since it tests the stack pointer location, and
600 the current stack probe code does not move the stack pointer
601 until all probes succeed.
603 For now we simply do not attempt any discrimination at all. Note
604 that this is quite acceptable, since a "real" SIGSEGV can only
605 occur as the result of an erroneous program */
607 msg = "stack overflow (or erroneous memory access)";
608 exception = &storage_error;
610 break;
612 case SIGBUS:
613 exception = &constraint_error;
614 msg = "SIGBUS";
615 break;
617 case SIGFPE:
618 exception = &constraint_error;
619 msg = "SIGFPE";
620 break;
622 default:
623 exception = &program_error;
624 msg = "unhandled signal";
627 mstate = (*Get_Machine_State_Addr)();
628 if (mstate)
630 mstate->eip = info->eip;
631 mstate->ebx = info->ebx;
632 mstate->esp = info->esp_at_signal;
633 mstate->ebp = info->ebp;
634 mstate->esi = info->esi;
635 mstate->edi = info->edi;
638 recurse = 0;
639 Raise_From_Signal_Handler (exception, msg);
642 void
643 __gnat_install_handler ()
645 struct sigaction act;
647 /* Set up signal handler to map synchronous signals to appropriate
648 exceptions. Make sure that the handler isn't interrupted by another
649 signal that might cause a scheduling event! */
651 act.sa_handler = __gnat_error_handler;
652 act.sa_flags = SA_NODEFER | SA_RESTART;
653 (void) sigemptyset (&act.sa_mask);
655 (void) sigaction (SIGABRT, &act, NULL);
656 (void) sigaction (SIGFPE, &act, NULL);
658 if (__gl_unreserve_all_interrupts == 0)
660 (void) sigaction (SIGILL, &act, NULL);
661 (void) sigaction (SIGSEGV, &act, NULL);
662 (void) sigaction (SIGBUS, &act, NULL);
664 __gnat_handler_installed = 1;
667 void
668 __gnat_initialize ()
672 /******************************************/
673 /* __gnat_initialize (NT-mingw32 version) */
674 /******************************************/
676 #elif defined (__MINGW32__)
677 #include <windows.h>
679 static LONG __gnat_error_handler PARAMS ((PEXCEPTION_POINTERS));
681 /* __gnat_initialize (mingw32). */
683 static LONG
684 __gnat_error_handler (info)
685 PEXCEPTION_POINTERS info;
687 static int recurse;
688 struct Exception_Data *exception;
689 char *msg;
691 switch (info->ExceptionRecord->ExceptionCode)
693 case EXCEPTION_ACCESS_VIOLATION:
694 /* If the failing address isn't maximally-aligned or if we've
695 recursed, this is a program error. */
696 if ((info->ExceptionRecord->ExceptionInformation[1] & 3) != 0
697 || recurse)
699 exception = &program_error;
700 msg = "EXCEPTION_ACCESS_VIOLATION";
702 else
704 /* See if the page before the faulting page is accessible. Do that
705 by trying to access it. */
706 recurse++;
707 * ((volatile char *) (info->ExceptionRecord->ExceptionInformation[1]
708 + 4096));
709 exception = &storage_error;
710 msg = "stack overflow (or erroneous memory access)";
712 break;
714 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
715 exception = &constraint_error;
716 msg = "EXCEPTION_ARRAY_BOUNDS_EXCEEDED";
717 break;
719 case EXCEPTION_DATATYPE_MISALIGNMENT:
720 exception = &constraint_error;
721 msg = "EXCEPTION_DATATYPE_MISALIGNMENT";
722 break;
724 case EXCEPTION_FLT_DENORMAL_OPERAND:
725 exception = &constraint_error;
726 msg = "EXCEPTION_FLT_DENORMAL_OPERAND";
727 break;
729 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
730 exception = &constraint_error;
731 msg = "EXCEPTION_FLT_DENORMAL_OPERAND";
732 break;
734 case EXCEPTION_FLT_INVALID_OPERATION:
735 exception = &constraint_error;
736 msg = "EXCEPTION_FLT_INVALID_OPERATION";
737 break;
739 case EXCEPTION_FLT_OVERFLOW:
740 exception = &constraint_error;
741 msg = "EXCEPTION_FLT_OVERFLOW";
742 break;
744 case EXCEPTION_FLT_STACK_CHECK:
745 exception = &program_error;
746 msg = "EXCEPTION_FLT_STACK_CHECK";
747 break;
749 case EXCEPTION_FLT_UNDERFLOW:
750 exception = &constraint_error;
751 msg = "EXCEPTION_FLT_UNDERFLOW";
752 break;
754 case EXCEPTION_INT_DIVIDE_BY_ZERO:
755 exception = &constraint_error;
756 msg = "EXCEPTION_INT_DIVIDE_BY_ZERO";
757 break;
759 case EXCEPTION_INT_OVERFLOW:
760 exception = &constraint_error;
761 msg = "EXCEPTION_INT_OVERFLOW";
762 break;
764 case EXCEPTION_INVALID_DISPOSITION:
765 exception = &program_error;
766 msg = "EXCEPTION_INVALID_DISPOSITION";
767 break;
769 case EXCEPTION_NONCONTINUABLE_EXCEPTION:
770 exception = &program_error;
771 msg = "EXCEPTION_NONCONTINUABLE_EXCEPTION";
772 break;
774 case EXCEPTION_PRIV_INSTRUCTION:
775 exception = &program_error;
776 msg = "EXCEPTION_PRIV_INSTRUCTION";
777 break;
779 case EXCEPTION_SINGLE_STEP:
780 exception = &program_error;
781 msg = "EXCEPTION_SINGLE_STEP";
782 break;
784 case EXCEPTION_STACK_OVERFLOW:
785 exception = &storage_error;
786 msg = "EXCEPTION_STACK_OVERFLOW";
787 break;
789 default:
790 exception = &program_error;
791 msg = "unhandled signal";
794 recurse = 0;
795 Raise_From_Signal_Handler (exception, msg);
798 void
799 __gnat_install_handler ()
801 SetUnhandledExceptionFilter (__gnat_error_handler);
802 __gnat_handler_installed = 1;
805 void
806 __gnat_initialize ()
809 /* Initialize floating-point coprocessor. This call is needed because
810 the MS libraries default to 64-bit precision instead of 80-bit
811 precision, and we require the full precision for proper operation,
812 given that we have set Max_Digits etc with this in mind */
814 __gnat_init_float ();
816 /* initialize a lock for a process handle list - see a-adaint.c for the
817 implementation of __gnat_portable_no_block_spawn, __gnat_portable_wait */
818 __gnat_plist_init();
821 /**************************************/
822 /* __gnat_initialize (Interix version) */
823 /**************************************/
825 #elif defined (__INTERIX)
827 #include <signal.h>
829 static void __gnat_error_handler PARAMS ((int));
831 static void
832 __gnat_error_handler (sig)
833 int sig;
835 struct Exception_Data *exception;
836 char *msg;
838 switch (sig)
840 case SIGSEGV:
841 exception = &storage_error;
842 msg = "stack overflow or erroneous memory access";
843 break;
845 case SIGBUS:
846 exception = &constraint_error;
847 msg = "SIGBUS";
848 break;
850 case SIGFPE:
851 exception = &constraint_error;
852 msg = "SIGFPE";
853 break;
855 default:
856 exception = &program_error;
857 msg = "unhandled signal";
860 Raise_From_Signal_Handler (exception, msg);
863 void
864 __gnat_install_handler ()
866 struct sigaction act;
868 /* Set up signal handler to map synchronous signals to appropriate
869 exceptions. Make sure that the handler isn't interrupted by another
870 signal that might cause a scheduling event! */
872 act.sa_handler = __gnat_error_handler;
873 act.sa_flags = 0;
874 (void) sigemptyset (&act.sa_mask);
876 /* Handlers for signals besides SIGSEGV cause c974013 to hang */
877 /* (void) sigaction (SIGILL, &act, NULL); */
878 /* (void) sigaction (SIGABRT, &act, NULL); */
879 /* (void) sigaction (SIGFPE, &act, NULL); */
880 /* (void) sigaction (SIGBUS, &act, NULL); */
881 if (__gl_unreserve_all_interrupts == 0)
883 (void) sigaction (SIGSEGV, &act, NULL);
885 __gnat_handler_installed = 1;
888 void
889 __gnat_initialize ()
891 __gnat_init_float ();
894 /**************************************/
895 /* __gnat_initialize (LynxOS version) */
896 /**************************************/
898 #elif defined (__Lynx__)
900 void
901 __gnat_initialize ()
903 __gnat_init_float ();
906 /*********************************/
907 /* __gnat_install_handler (Lynx) */
908 /*********************************/
910 void
911 __gnat_install_handler ()
913 __gnat_handler_installed = 1;
916 /****************************/
917 /* __gnat_initialize (OS/2) */
918 /****************************/
920 #elif defined (__EMX__) /* OS/2 dependent initialization */
922 void
923 __gnat_initialize ()
927 /*********************************/
928 /* __gnat_install_handler (OS/2) */
929 /*********************************/
931 void
932 __gnat_install_handler ()
934 __gnat_handler_installed = 1;
937 /***********************************/
938 /* __gnat_initialize (SGI version) */
939 /***********************************/
941 #elif defined (sgi)
943 #include <signal.h>
944 #include <siginfo.h>
946 #ifndef NULL
947 #define NULL 0
948 #endif
950 #define SIGADAABORT 48
951 #define SIGNAL_STACK_SIZE 4096
952 #define SIGNAL_STACK_ALIGNMENT 64
954 struct Machine_State
956 sigcontext_t context;
959 static void __gnat_error_handler PARAMS ((int, int, sigcontext_t *));
961 static void
962 __gnat_error_handler (sig, code, sc)
963 int sig;
964 int code;
965 sigcontext_t *sc;
967 struct Machine_State *mstate;
968 struct Exception_Data *exception;
969 char *msg;
971 int i;
973 switch (sig)
975 case SIGSEGV:
976 if (code == EFAULT)
978 exception = &program_error;
979 msg = "SIGSEGV: (Invalid virtual address)";
981 else if (code == ENXIO)
983 exception = &program_error;
984 msg = "SIGSEGV: (Read beyond mapped object)";
986 else if (code == ENOSPC)
988 exception = &program_error; /* ??? storage_error ??? */
989 msg = "SIGSEGV: (Autogrow for file failed)";
991 else if (code == EACCES)
993 /* ??? Re-add smarts to further verify that we launched
994 the stack into a guard page, not an attempt to
995 write to .text or something */
996 exception = &storage_error;
997 msg = "SIGSEGV: (stack overflow or erroneous memory access)";
999 else
1001 /* Just in case the OS guys did it to us again. Sometimes
1002 they fail to document all of the valid codes that are
1003 passed to signal handlers, just in case someone depends
1004 on knowing all the codes */
1005 exception = &program_error;
1006 msg = "SIGSEGV: (Undocumented reason)";
1008 break;
1010 case SIGBUS:
1011 /* Map all bus errors to Program_Error. */
1012 exception = &program_error;
1013 msg = "SIGBUS";
1014 break;
1016 case SIGFPE:
1017 /* Map all fpe errors to Constraint_Error. */
1018 exception = &constraint_error;
1019 msg = "SIGFPE";
1020 break;
1022 case SIGADAABORT:
1023 if ((*Check_Abort_Status) ())
1025 exception = &_abort_signal;
1026 msg = "";
1028 else
1029 return;
1031 break;
1033 default:
1034 /* Everything else is a Program_Error. */
1035 exception = &program_error;
1036 msg = "unhandled signal";
1039 mstate = (*Get_Machine_State_Addr)();
1040 if (mstate != 0)
1041 memcpy ((void *) mstate, (const void *) sc, sizeof (sigcontext_t));
1043 Raise_From_Signal_Handler (exception, msg);
1047 void
1048 __gnat_install_handler ()
1050 stack_t ss;
1051 struct sigaction act;
1053 /* Setup signal handler to map synchronous signals to appropriate
1054 exceptions. Make sure that the handler isn't interrupted by another
1055 signal that might cause a scheduling event! */
1057 act.sa_handler = __gnat_error_handler;
1058 act.sa_flags = SA_NODEFER + SA_RESTART;
1059 (void) sigfillset (&act.sa_mask);
1060 (void) sigemptyset (&act.sa_mask);
1062 (void) sigaction (SIGABRT, &act, NULL);
1063 (void) sigaction (SIGFPE, &act, NULL);
1065 if (__gl_unreserve_all_interrupts == 0)
1067 (void) sigaction (SIGILL, &act, NULL);
1068 (void) sigaction (SIGSEGV, &act, NULL);
1069 (void) sigaction (SIGBUS, &act, NULL);
1071 (void) sigaction (SIGADAABORT, &act, NULL);
1072 __gnat_handler_installed = 1;
1075 void
1076 __gnat_initialize ()
1080 /*************************************************/
1081 /* __gnat_initialize (Solaris and SunOS version) */
1082 /*************************************************/
1084 #elif defined (sun) && defined (__SVR4) && !defined (__vxworks)
1086 #include <signal.h>
1087 #include <siginfo.h>
1089 static void __gnat_error_handler PARAMS ((int, siginfo_t *));
1091 static void
1092 __gnat_error_handler (sig, sip)
1093 int sig;
1094 siginfo_t *sip;
1096 struct Exception_Data *exception;
1097 static int recurse = 0;
1098 char *msg;
1100 /* If this was an explicit signal from a "kill", just resignal it. */
1101 if (SI_FROMUSER (sip))
1103 signal (sig, SIG_DFL);
1104 kill (getpid(), sig);
1107 /* Otherwise, treat it as something we handle. */
1108 switch (sig)
1110 case SIGSEGV:
1111 /* If the problem was permissions, this is a constraint error.
1112 Likewise if the failing address isn't maximally aligned or if
1113 we've recursed.
1115 ??? Using a static variable here isn't task-safe, but it's
1116 much too hard to do anything else and we're just determining
1117 which exception to raise. */
1118 if (sip->si_code == SEGV_ACCERR
1119 || (((long) sip->si_addr) & 3) != 0
1120 || recurse)
1122 exception = &constraint_error;
1123 msg = "SIGSEGV";
1125 else
1127 /* See if the page before the faulting page is accessible. Do that
1128 by trying to access it. We'd like to simply try to access
1129 4096 + the faulting address, but it's not guaranteed to be
1130 the actual address, just to be on the same page. */
1131 recurse++;
1132 ((volatile char *)
1133 ((long) sip->si_addr & - getpagesize ()))[getpagesize ()];
1134 exception = &storage_error;
1135 msg = "stack overflow (or erroneous memory access)";
1137 break;
1139 case SIGBUS:
1140 exception = &program_error;
1141 msg = "SIGBUS";
1142 break;
1144 case SIGFPE:
1145 exception = &constraint_error;
1146 msg = "SIGFPE";
1147 break;
1149 default:
1150 exception = &program_error;
1151 msg = "unhandled signal";
1154 recurse = 0;
1156 Raise_From_Signal_Handler (exception, msg);
1159 void
1160 __gnat_install_handler ()
1162 struct sigaction act;
1164 /* Set up signal handler to map synchronous signals to appropriate
1165 exceptions. Make sure that the handler isn't interrupted by another
1166 signal that might cause a scheduling event! */
1168 act.sa_handler = __gnat_error_handler;
1169 act.sa_flags = SA_NODEFER | SA_RESTART | SA_SIGINFO;
1170 (void) sigemptyset (&act.sa_mask);
1172 (void) sigaction (SIGABRT, &act, NULL);
1174 if (__gl_unreserve_all_interrupts == 0)
1176 (void) sigaction (SIGFPE, &act, NULL);
1177 (void) sigaction (SIGSEGV, &act, NULL);
1178 (void) sigaction (SIGBUS, &act, NULL);
1180 __gnat_handler_installed = 1;
1183 void
1184 __gnat_initialize ()
1188 /***********************************/
1189 /* __gnat_initialize (SNI version) */
1190 /***********************************/
1192 #elif defined (__sni__)
1194 /* SNI needs special defines and includes */
1196 #define _XOPEN_SOURCE
1197 #define _POSIX_SOURCE
1198 #include <signal.h>
1200 extern size_t __gnat_getpagesize PARAMS ((void));
1201 static void __gnat_error_handler PARAMS ((int));
1203 /* The run time needs this function which is a #define in SNI */
1205 size_t
1206 __gnat_getpagesize ()
1208 return getpagesize ();
1211 static void
1212 __gnat_error_handler (sig)
1213 int sig;
1215 struct Exception_Data *exception;
1216 char *msg;
1218 switch (sig)
1220 case SIGSEGV:
1221 /* FIXME: we need to detect the case of a *real* SIGSEGV */
1222 exception = &storage_error;
1223 msg = "stack overflow or erroneous memory access";
1224 break;
1226 case SIGBUS:
1227 exception = &constraint_error;
1228 msg = "SIGBUS";
1229 break;
1231 case SIGFPE:
1232 exception = &constraint_error;
1233 msg = "SIGFPE";
1234 break;
1236 default:
1237 exception = &program_error;
1238 msg = "unhandled signal";
1241 Raise_From_Signal_Handler (exception, msg);
1244 void
1245 __gnat_install_handler ()
1247 struct sigaction act;
1249 /* Set up signal handler to map synchronous signals to appropriate
1250 exceptions. Make sure that the handler isn't interrupted by another
1251 signal that might cause a scheduling event! */
1253 act.sa_handler = __gnat_error_handler;
1254 act.sa_flags = SA_NODEFER | SA_RESTART;
1255 (void) sigemptyset (&act.sa_mask);
1257 (void) sigaction (SIGABRT, &act, NULL);
1258 (void) sigaction (SIGFPE, &act, NULL);
1260 if (__gl_unreserve_all_interrupts == 0)
1262 (void) sigaction (SIGILL, &act, NULL);
1263 (void) sigaction (SIGSEGV, &act, NULL);
1264 (void) sigaction (SIGBUS, &act, NULL);
1266 __gnat_handler_installed = 1;
1269 void
1270 __gnat_initialize ()
1274 /***********************************/
1275 /* __gnat_initialize (VMS version) */
1276 /***********************************/
1278 #elif defined (VMS)
1280 /* The prehandler actually gets control first on a condition. It swaps the
1281 stack pointer and calls the handler (__gnat_error_handler). */
1282 extern long __gnat_error_prehandler ();
1284 extern char *__gnat_error_prehandler_stack; /* Alternate signal stack */
1286 /* Conditions that don't have an Ada exception counterpart must raise
1287 Non_Ada_Error. Since this is defined in s-auxdec, it should only be
1288 referenced by user programs, not the compiler or tools. Hence the
1289 #ifdef IN_RTS. */
1291 #ifdef IN_RTS
1292 #define Non_Ada_Error system__aux_dec__non_ada_error
1293 extern struct Exception_Data Non_Ada_Error;
1295 #define Coded_Exception system__vms_exception_table__coded_exception
1296 extern struct Exception_Data *Coded_Exception (int);
1297 #endif
1299 /* Define macro symbols for the VMS conditions that become Ada exceptions.
1300 Most of these are also defined in the header file ssdef.h which has not
1301 yet been converted to be recoginized by Gnu C. Some, which couldn't be
1302 located, are assigned names based on the DEC test suite tests which
1303 raise them. */
1305 #define SS$_ACCVIO 12
1306 #define SS$_DEBUG 1132
1307 #define SS$_INTDIV 1156
1308 #define SS$_HPARITH 1284
1309 #define SS$_STKOVF 1364
1310 #define SS$_RESIGNAL 2328
1311 #define MTH$_FLOOVEMAT 1475268 /* Some ACVC_21 CXA tests */
1312 #define SS$_CE24VRU 3253636 /* Write to unopened file */
1313 #define SS$_C980VTE 3246436 /* AST requests time slice */
1314 #define CMA$_EXIT_THREAD 4227492
1315 #define CMA$_EXCCOPLOS 4228108
1316 #define CMA$_ALERTED 4227460
1318 struct descriptor_s {unsigned short len, mbz; char *adr; };
1320 static long __gnat_error_handler PARAMS ((int *, void *));
1322 static long
1323 __gnat_error_handler (sigargs, mechargs)
1324 int *sigargs;
1325 void *mechargs;
1327 struct Exception_Data *exception = 0;
1328 char *msg = "";
1329 char message [256];
1330 long prvhnd;
1331 struct descriptor_s msgdesc;
1332 int msg_flag = 0x000f; /* 1 bit for each of the four message parts */
1333 unsigned short outlen;
1334 char curr_icb [544];
1335 long curr_invo_handle;
1336 long *mstate;
1338 /* Resignaled conditions aren't effected by by pragma Import_Exception */
1340 switch (sigargs[1])
1343 case CMA$_EXIT_THREAD:
1344 return SS$_RESIGNAL;
1346 case SS$_DEBUG: /* Gdb attach, resignal to merge activate gdbstub. */
1347 return SS$_RESIGNAL;
1349 case 1409786: /* Nickerson bug #33 ??? */
1350 return SS$_RESIGNAL;
1352 case 1381050: /* Nickerson bug #33 ??? */
1353 return SS$_RESIGNAL;
1355 case 11829410: /* Resignalled as Use_Error for CE10VRC */
1356 return SS$_RESIGNAL;
1360 #ifdef IN_RTS
1361 /* See if it's an imported exception. Mask off severity bits. */
1362 exception = Coded_Exception (sigargs [1] & 0xfffffff8);
1363 if (exception)
1365 msgdesc.len = 256;
1366 msgdesc.mbz = 0;
1367 msgdesc.adr = message;
1368 SYS$GETMSG (sigargs[1], &outlen, &msgdesc, msg_flag, 0);
1369 message [outlen] = 0;
1370 msg = message;
1372 exception->Name_Length = 19;
1373 /* The full name really should be get sys$getmsg returns. ??? */
1374 exception->Full_Name = "IMPORTED_EXCEPTION";
1375 exception->Import_Code = sigargs [1] & 0xfffffff8;
1377 #endif
1379 if (exception == 0)
1380 switch (sigargs[1])
1382 case SS$_ACCVIO:
1383 if (sigargs[3] == 0)
1385 exception = &constraint_error;
1386 msg = "access zero";
1388 else
1390 exception = &storage_error;
1391 msg = "stack overflow (or erroneous memory access)";
1393 break;
1395 case SS$_STKOVF:
1396 exception = &storage_error;
1397 msg = "stack overflow";
1398 break;
1400 case SS$_INTDIV:
1401 exception = &constraint_error;
1402 msg = "division by zero";
1403 break;
1405 case SS$_HPARITH:
1406 #ifndef IN_RTS
1407 return SS$_RESIGNAL; /* toplev.c handles for compiler */
1408 #else
1410 exception = &constraint_error;
1411 msg = "arithmetic error";
1413 #endif
1414 break;
1416 case MTH$_FLOOVEMAT:
1417 exception = &constraint_error;
1418 msg = "floating overflow in math library";
1419 break;
1421 case SS$_CE24VRU:
1422 exception = &constraint_error;
1423 msg = "";
1424 break;
1426 case SS$_C980VTE:
1427 exception = &program_error;
1428 msg = "";
1429 break;
1431 default:
1432 #ifndef IN_RTS
1433 exception = &program_error;
1434 #else
1435 /* User programs expect Non_Ada_Error to be raised, reference
1436 DEC Ada test CXCONDHAN. */
1437 exception = &Non_Ada_Error;
1438 #endif
1439 msgdesc.len = 256;
1440 msgdesc.mbz = 0;
1441 msgdesc.adr = message;
1442 SYS$GETMSG (sigargs[1], &outlen, &msgdesc, msg_flag, 0);
1443 message [outlen] = 0;
1444 msg = message;
1445 break;
1448 mstate = (long *) (*Get_Machine_State_Addr) ();
1449 if (mstate != 0)
1451 LIB$GET_CURR_INVO_CONTEXT (&curr_icb);
1452 LIB$GET_PREV_INVO_CONTEXT (&curr_icb);
1453 LIB$GET_PREV_INVO_CONTEXT (&curr_icb);
1454 curr_invo_handle = LIB$GET_INVO_HANDLE (&curr_icb);
1455 *mstate = curr_invo_handle;
1457 Raise_From_Signal_Handler (exception, msg);
1460 void
1461 __gnat_install_handler ()
1463 long prvhnd;
1464 char *c;
1466 c = (char *) malloc (2049);
1468 __gnat_error_prehandler_stack = &c[2048];
1470 /* __gnat_error_prehandler is an assembly function. */
1471 SYS$SETEXV (1, __gnat_error_prehandler, 3, &prvhnd);
1472 __gnat_handler_installed = 1;
1475 void
1476 __gnat_initialize()
1480 /***************************************/
1481 /* __gnat_initialize (VXWorks version) */
1482 /***************************************/
1484 #elif defined(__vxworks)
1486 #include <signal.h>
1487 #include <taskLib.h>
1488 #include <intLib.h>
1489 #include <iv.h>
1491 static void __gnat_init_handler PARAMS ((int));
1492 extern int __gnat_inum_to_ivec PARAMS ((int));
1493 static void __gnat_error_handler PARAMS ((int, int, struct sigcontext *));
1495 static void
1496 __gnat_int_handler (interr)
1497 int interr;
1499 /* Note that we should use something like Raise_From_Int_Handler here, but
1500 for now Raise_From_Signal_Handler will do the job. ??? */
1502 Raise_From_Signal_Handler (&storage_error, "stack overflow");
1505 /* Used for stack-checking on VxWorks. Must be task-local in
1506 tasking programs */
1508 void *__gnat_stack_limit = NULL;
1510 #ifndef __alpha_vxworks
1512 /* getpid is used by s-parint.adb, but is not defined by VxWorks, except
1513 on Alpha VxWorks */
1515 extern long getpid PARAMS ((void));
1517 long
1518 getpid ()
1520 return taskIdSelf ();
1522 #endif
1524 /* This is needed by the GNAT run time to handle Vxworks interrupts */
1526 __gnat_inum_to_ivec (num)
1527 int num;
1529 return INUM_TO_IVEC (num);
1532 static void
1533 __gnat_error_handler (sig, code, sc)
1534 int sig;
1535 int code;
1536 struct sigcontext *sc;
1538 struct Exception_Data *exception;
1539 sigset_t mask;
1540 int result;
1541 char *msg;
1543 /* VxWorks will always mask out the signal during the signal handler and
1544 will reenable it on a longjmp. GNAT does not generate a longjmp to
1545 return from a signal handler so the signal will still be masked unless
1546 we unmask it. */
1547 (void) sigprocmask (SIG_SETMASK, NULL, &mask);
1548 sigdelset (&mask, sig);
1549 (void) sigprocmask (SIG_SETMASK, &mask, NULL);
1551 /* VxWorks will suspend the task when it gets a hardware exception. We
1552 take the liberty of resuming the task for the application. */
1553 if (taskIsSuspended (taskIdSelf ()) != 0)
1554 (void) taskResume (taskIdSelf ());
1556 switch (sig)
1558 case SIGFPE:
1559 exception = &constraint_error;
1560 msg = "SIGFPE";
1561 break;
1562 case SIGILL:
1563 exception = &constraint_error;
1564 msg = "SIGILL";
1565 break;
1566 case SIGSEGV:
1567 exception = &program_error;
1568 msg = "SIGSEGV";
1569 break;
1570 case SIGBUS:
1571 exception = &program_error;
1572 msg = "SIGBUS";
1573 break;
1574 default:
1575 exception = &program_error;
1576 msg = "unhandled signal";
1579 Raise_From_Signal_Handler (exception, msg);
1582 void
1583 __gnat_install_handler ()
1585 struct sigaction act;
1587 /* Setup signal handler to map synchronous signals to appropriate
1588 exceptions. Make sure that the handler isn't interrupted by another
1589 signal that might cause a scheduling event! */
1591 act.sa_handler = __gnat_error_handler;
1592 act.sa_flags = SA_SIGINFO | SA_ONSTACK;
1593 (void) sigemptyset (&act.sa_mask);
1595 (void) sigaction (SIGFPE, &act, NULL);
1597 if (__gl_unreserve_all_interrupts == 0)
1599 (void) sigaction (SIGILL, &act, NULL);
1600 (void) sigaction (SIGSEGV, &act, NULL);
1601 (void) sigaction (SIGBUS, &act, NULL);
1603 __gnat_handler_installed = 1;
1606 #define HAVE_GNAT_INIT_FLOAT
1608 void
1609 __gnat_init_float ()
1611 #if defined (_ARCH_PPC) && !defined (_SOFT_FLOAT)
1612 /* Disable overflow/underflow exceptions on the PPC processor, this is needed
1613 to get correct Ada semantic */
1614 asm ("mtfsb0 25");
1615 asm ("mtfsb0 26");
1616 #endif
1619 void
1620 __gnat_initialize ()
1622 TASK_DESC pTaskDesc;
1624 if (taskInfoGet (taskIdSelf (), &pTaskDesc) != OK)
1625 printErr ("Cannot get task info");
1627 __gnat_stack_limit = (void *) pTaskDesc.td_pStackLimit;
1629 __gnat_init_float ();
1631 #ifdef __mips_vxworks
1632 #if 0
1633 /* For now remove this handler, since it is causing interferences with gdb */
1635 /* Connect the overflow trap directly to the __gnat_int_handler routine
1636 as it is not converted to a signal by VxWorks. */
1638 intConnect (INUM_TO_IVEC (IV_TRAP_VEC), &__gnat_int_handler, IV_TRAP_VEC);
1639 #endif
1640 #endif
1643 #else
1645 /* For all other versions of GNAT, the initialize routine and handler
1646 installation do nothing */
1648 /***************************************/
1649 /* __gnat_initialize (default version) */
1650 /***************************************/
1652 void
1653 __gnat_initialize ()
1657 /********************************************/
1658 /* __gnat_install_handler (default version) */
1659 /********************************************/
1661 void
1662 __gnat_install_handler ()
1664 __gnat_handler_installed = 1;
1667 #endif
1669 /*********************/
1670 /* __gnat_init_float */
1671 /*********************/
1673 /* This routine is called as each process thread is created, for possible
1674 initialization of the FP processor. This version is used under INTERIX,
1675 WIN32 and could be used under OS/2 */
1677 #if defined (_WIN32) || defined (__INTERIX) || defined (__EMX__) \
1678 || defined (__Lynx__)
1680 #define HAVE_GNAT_INIT_FLOAT
1682 void
1683 __gnat_init_float ()
1685 #if defined (__i386__) || defined (i386)
1687 /* This is used to properly initialize the FPU on an x86 for each
1688 process thread. */
1690 asm ("finit");
1692 #endif /* Defined __i386__ */
1694 #endif
1696 #ifndef HAVE_GNAT_INIT_FLOAT
1698 /* All targets without a specific __gnat_init_float will use an empty one */
1699 void
1700 __gnat_init_float ()
1703 #endif