2015-09-28 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / ada / init.c
blob5754fae361979a3c54ae3585ca29a5b72967d16b
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * I N I T *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2015, Free Software Foundation, Inc. *
10 * *
11 * GNAT is free software; you can redistribute it and/or modify it under *
12 * terms of the GNU General Public License as published by the Free Soft- *
13 * ware Foundation; either version 3, or (at your option) any later ver- *
14 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
15 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
16 * or FITNESS FOR A PARTICULAR PURPOSE. *
17 * *
18 * As a special exception under Section 7 of GPL version 3, you are granted *
19 * additional permissions described in the GCC Runtime Library Exception, *
20 * version 3.1, as published by the Free Software Foundation. *
21 * *
22 * You should have received a copy of the GNU General Public License and *
23 * a copy of the GCC Runtime Library Exception along with this program; *
24 * see the files COPYING3 and COPYING.RUNTIME respectively. If not, see *
25 * <http://www.gnu.org/licenses/>. *
26 * *
27 * GNAT was originally developed by the GNAT team at New York University. *
28 * Extensive contributions were provided by Ada Core Technologies Inc. *
29 * *
30 ****************************************************************************/
32 /* This unit contains initialization circuits that are system dependent.
33 A major part of the functionality involves stack overflow checking.
34 The GCC backend generates probe instructions to test for stack overflow.
35 For details on the exact approach used to generate these probes, see the
36 "Using and Porting GCC" manual, in particular the "Stack Checking" section
37 and the subsection "Specifying How Stack Checking is Done". The handlers
38 installed by this file are used to catch the resulting signals that come
39 from these probes failing (i.e. touching protected pages). */
41 /* This file should be kept synchronized with s-init.ads, s-init.adb and the
42 s-init-*.adb variants. All these files implement the required functionality
43 for different targets. */
45 /* The following include is here to meet the published VxWorks requirement
46 that the __vxworks header appear before any other include. */
47 #ifdef __vxworks
48 #include "vxWorks.h"
49 #endif
51 #ifdef __ANDROID__
52 #undef __linux__
53 #endif
55 #ifdef IN_RTS
56 #include "tconfig.h"
57 #include "tsystem.h"
58 #include <sys/stat.h>
60 /* We don't have libiberty, so use malloc. */
61 #define xmalloc(S) malloc (S)
62 #else
63 #include "config.h"
64 #include "system.h"
65 #endif
67 #include "adaint.h"
68 #include "raise.h"
70 #ifdef __cplusplus
71 extern "C" {
72 #endif
74 extern void __gnat_raise_program_error (const char *, int);
76 /* Addresses of exception data blocks for predefined exceptions. Tasking_Error
77 is not used in this unit, and the abort signal is only used on IRIX.
78 ??? Revisit this part since IRIX is no longer supported. */
79 extern struct Exception_Data constraint_error;
80 extern struct Exception_Data numeric_error;
81 extern struct Exception_Data program_error;
82 extern struct Exception_Data storage_error;
84 /* For the Cert run time we use the regular raise exception routine because
85 Raise_From_Signal_Handler is not available. */
86 #ifdef CERT
87 #define Raise_From_Signal_Handler \
88 __gnat_raise_exception
89 extern void Raise_From_Signal_Handler (struct Exception_Data *, const char *);
90 #else
91 #define Raise_From_Signal_Handler \
92 ada__exceptions__raise_from_signal_handler
93 extern void Raise_From_Signal_Handler (struct Exception_Data *, const char *);
94 #endif
96 /* Global values computed by the binder. */
97 int __gl_main_priority = -1;
98 int __gl_main_cpu = -1;
99 int __gl_time_slice_val = -1;
100 char __gl_wc_encoding = 'n';
101 char __gl_locking_policy = ' ';
102 char __gl_queuing_policy = ' ';
103 char __gl_task_dispatching_policy = ' ';
104 char *__gl_priority_specific_dispatching = 0;
105 int __gl_num_specific_dispatching = 0;
106 char *__gl_interrupt_states = 0;
107 int __gl_num_interrupt_states = 0;
108 int __gl_unreserve_all_interrupts = 0;
109 int __gl_exception_tracebacks = 0;
110 int __gl_detect_blocking = 0;
111 int __gl_default_stack_size = -1;
112 int __gl_leap_seconds_support = 0;
113 int __gl_canonical_streams = 0;
115 /* This value is not used anymore, but kept for bootstrapping purpose. */
116 int __gl_zero_cost_exceptions = 0;
118 /* Indication of whether synchronous signal handler has already been
119 installed by a previous call to adainit. */
120 int __gnat_handler_installed = 0;
122 #ifndef IN_RTS
123 int __gnat_inside_elab_final_code = 0;
124 /* ??? This variable is obsolete since 2001-08-29 but is kept to allow
125 bootstrap from old GNAT versions (< 3.15). */
126 #endif
128 /* HAVE_GNAT_INIT_FLOAT must be set on every targets where a __gnat_init_float
129 is defined. If this is not set then a void implementation will be defined
130 at the end of this unit. */
131 #undef HAVE_GNAT_INIT_FLOAT
133 /******************************/
134 /* __gnat_get_interrupt_state */
135 /******************************/
137 char __gnat_get_interrupt_state (int);
139 /* This routine is called from the runtime as needed to determine the state
140 of an interrupt, as set by an Interrupt_State pragma appearing anywhere
141 in the current partition. The input argument is the interrupt number,
142 and the result is one of the following:
144 'n' this interrupt not set by any Interrupt_State pragma
145 'u' Interrupt_State pragma set state to User
146 'r' Interrupt_State pragma set state to Runtime
147 's' Interrupt_State pragma set state to System */
149 char
150 __gnat_get_interrupt_state (int intrup)
152 if (intrup >= __gl_num_interrupt_states)
153 return 'n';
154 else
155 return __gl_interrupt_states [intrup];
158 /***********************************/
159 /* __gnat_get_specific_dispatching */
160 /***********************************/
162 char __gnat_get_specific_dispatching (int);
164 /* This routine is called from the runtime as needed to determine the
165 priority specific dispatching policy, as set by a
166 Priority_Specific_Dispatching pragma appearing anywhere in the current
167 partition. The input argument is the priority number, and the result
168 is the upper case first character of the policy name, e.g. 'F' for
169 FIFO_Within_Priorities. A space ' ' is returned if no
170 Priority_Specific_Dispatching pragma is used in the partition. */
172 char
173 __gnat_get_specific_dispatching (int priority)
175 if (__gl_num_specific_dispatching == 0)
176 return ' ';
177 else if (priority >= __gl_num_specific_dispatching)
178 return 'F';
179 else
180 return __gl_priority_specific_dispatching [priority];
183 #ifndef IN_RTS
185 /**********************/
186 /* __gnat_set_globals */
187 /**********************/
189 /* This routine is kept for bootstrapping purposes, since the binder generated
190 file now sets the __gl_* variables directly. */
192 void
193 __gnat_set_globals (void)
197 #endif
199 /***************/
200 /* AIX Section */
201 /***************/
203 #if defined (_AIX)
205 #include <signal.h>
206 #include <sys/time.h>
208 /* Some versions of AIX don't define SA_NODEFER. */
210 #ifndef SA_NODEFER
211 #define SA_NODEFER 0
212 #endif /* SA_NODEFER */
214 /* Versions of AIX before 4.3 don't have nanosleep but provide
215 nsleep instead. */
217 #ifndef _AIXVERSION_430
219 extern int nanosleep (struct timestruc_t *, struct timestruc_t *);
222 nanosleep (struct timestruc_t *Rqtp, struct timestruc_t *Rmtp)
224 return nsleep (Rqtp, Rmtp);
227 #endif /* _AIXVERSION_430 */
229 static void
230 __gnat_error_handler (int sig,
231 siginfo_t *si ATTRIBUTE_UNUSED,
232 void *ucontext ATTRIBUTE_UNUSED)
234 struct Exception_Data *exception;
235 const char *msg;
237 switch (sig)
239 case SIGSEGV:
240 /* FIXME: we need to detect the case of a *real* SIGSEGV. */
241 exception = &storage_error;
242 msg = "stack overflow or erroneous memory access";
243 break;
245 case SIGBUS:
246 exception = &constraint_error;
247 msg = "SIGBUS";
248 break;
250 case SIGFPE:
251 exception = &constraint_error;
252 msg = "SIGFPE";
253 break;
255 default:
256 exception = &program_error;
257 msg = "unhandled signal";
260 Raise_From_Signal_Handler (exception, msg);
263 void
264 __gnat_install_handler (void)
266 struct sigaction act;
268 /* Set up signal handler to map synchronous signals to appropriate
269 exceptions. Make sure that the handler isn't interrupted by another
270 signal that might cause a scheduling event! */
272 act.sa_flags = SA_NODEFER | SA_RESTART | SA_SIGINFO;
273 act.sa_sigaction = __gnat_error_handler;
274 sigemptyset (&act.sa_mask);
276 /* Do not install handlers if interrupt state is "System". */
277 if (__gnat_get_interrupt_state (SIGABRT) != 's')
278 sigaction (SIGABRT, &act, NULL);
279 if (__gnat_get_interrupt_state (SIGFPE) != 's')
280 sigaction (SIGFPE, &act, NULL);
281 if (__gnat_get_interrupt_state (SIGILL) != 's')
282 sigaction (SIGILL, &act, NULL);
283 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
284 sigaction (SIGSEGV, &act, NULL);
285 if (__gnat_get_interrupt_state (SIGBUS) != 's')
286 sigaction (SIGBUS, &act, NULL);
288 __gnat_handler_installed = 1;
291 /*****************/
292 /* HP-UX section */
293 /*****************/
295 #elif defined (__hpux__)
297 #include <signal.h>
298 #include <sys/ucontext.h>
300 #if defined (IN_RTS) && defined (__ia64__)
302 #include <sys/uc_access.h>
304 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
306 void
307 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED, void *ucontext)
309 ucontext_t *uc = (ucontext_t *) ucontext;
310 uint64_t ip;
312 /* Adjust on itanium, as GetIPInfo is not supported. */
313 __uc_get_ip (uc, &ip);
314 __uc_set_ip (uc, ip + 1);
316 #endif /* IN_RTS && __ia64__ */
318 /* Tasking and Non-tasking signal handler. Map SIGnal to Ada exception
319 propagation after the required low level adjustments. */
321 static void
322 __gnat_error_handler (int sig,
323 siginfo_t *si ATTRIBUTE_UNUSED,
324 void *ucontext ATTRIBUTE_UNUSED)
326 struct Exception_Data *exception;
327 const char *msg;
329 __gnat_adjust_context_for_raise (sig, ucontext);
331 switch (sig)
333 case SIGSEGV:
334 /* FIXME: we need to detect the case of a *real* SIGSEGV. */
335 exception = &storage_error;
336 msg = "stack overflow or erroneous memory access";
337 break;
339 case SIGBUS:
340 exception = &constraint_error;
341 msg = "SIGBUS";
342 break;
344 case SIGFPE:
345 exception = &constraint_error;
346 msg = "SIGFPE";
347 break;
349 default:
350 exception = &program_error;
351 msg = "unhandled signal";
354 Raise_From_Signal_Handler (exception, msg);
357 /* This must be in keeping with System.OS_Interface.Alternate_Stack_Size. */
358 #if defined (__hppa__)
359 char __gnat_alternate_stack[16 * 1024]; /* 2 * SIGSTKSZ */
360 #else
361 char __gnat_alternate_stack[128 * 1024]; /* MINSIGSTKSZ */
362 #endif
364 void
365 __gnat_install_handler (void)
367 struct sigaction act;
369 /* Set up signal handler to map synchronous signals to appropriate
370 exceptions. Make sure that the handler isn't interrupted by another
371 signal that might cause a scheduling event! Also setup an alternate
372 stack region for the handler execution so that stack overflows can be
373 handled properly, avoiding a SEGV generation from stack usage by the
374 handler itself. */
376 stack_t stack;
377 stack.ss_sp = __gnat_alternate_stack;
378 stack.ss_size = sizeof (__gnat_alternate_stack);
379 stack.ss_flags = 0;
380 sigaltstack (&stack, NULL);
382 act.sa_sigaction = __gnat_error_handler;
383 act.sa_flags = SA_NODEFER | SA_RESTART | SA_SIGINFO;
384 sigemptyset (&act.sa_mask);
386 /* Do not install handlers if interrupt state is "System". */
387 if (__gnat_get_interrupt_state (SIGABRT) != 's')
388 sigaction (SIGABRT, &act, NULL);
389 if (__gnat_get_interrupt_state (SIGFPE) != 's')
390 sigaction (SIGFPE, &act, NULL);
391 if (__gnat_get_interrupt_state (SIGILL) != 's')
392 sigaction (SIGILL, &act, NULL);
393 if (__gnat_get_interrupt_state (SIGBUS) != 's')
394 sigaction (SIGBUS, &act, NULL);
395 act.sa_flags |= SA_ONSTACK;
396 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
397 sigaction (SIGSEGV, &act, NULL);
399 __gnat_handler_installed = 1;
402 /*********************/
403 /* GNU/Linux Section */
404 /*********************/
406 #elif defined (__linux__)
408 #include <signal.h>
410 #define __USE_GNU 1 /* required to get REG_EIP/RIP from glibc's ucontext.h */
411 #include <sys/ucontext.h>
413 /* GNU/Linux, which uses glibc, does not define NULL in included
414 header files. */
416 #if !defined (NULL)
417 #define NULL ((void *) 0)
418 #endif
420 #if defined (MaRTE)
422 /* MaRTE OS provides its own version of sigaction, sigfillset, and
423 sigemptyset (overriding these symbol names). We want to make sure that
424 the versions provided by the underlying C library are used here (these
425 versions are renamed by MaRTE to linux_sigaction, fake_linux_sigfillset,
426 and fake_linux_sigemptyset, respectively). The MaRTE library will not
427 always be present (it will not be linked if no tasking constructs are
428 used), so we use the weak symbol mechanism to point always to the symbols
429 defined within the C library. */
431 #pragma weak linux_sigaction
432 int linux_sigaction (int signum, const struct sigaction *act,
433 struct sigaction *oldact)
435 return sigaction (signum, act, oldact);
437 #define sigaction(signum, act, oldact) linux_sigaction (signum, act, oldact)
439 #pragma weak fake_linux_sigfillset
440 void fake_linux_sigfillset (sigset_t *set)
442 sigfillset (set);
444 #define sigfillset(set) fake_linux_sigfillset (set)
446 #pragma weak fake_linux_sigemptyset
447 void fake_linux_sigemptyset (sigset_t *set)
449 sigemptyset (set);
451 #define sigemptyset(set) fake_linux_sigemptyset (set)
453 #endif
455 #if defined (__i386__) || defined (__x86_64__) || defined (__ia64__) \
456 || defined (__ARMEL__)
458 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
460 void
461 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED, void *ucontext)
463 mcontext_t *mcontext = &((ucontext_t *) ucontext)->uc_mcontext;
465 /* On the i386 and x86-64 architectures, stack checking is performed by
466 means of probes with moving stack pointer, that is to say the probed
467 address is always the value of the stack pointer. Upon hitting the
468 guard page, the stack pointer therefore points to an inaccessible
469 address and an alternate signal stack is needed to run the handler.
470 But there is an additional twist: on these architectures, the EH
471 return code writes the address of the handler at the target CFA's
472 value on the stack before doing the jump. As a consequence, if
473 there is an active handler in the frame whose stack has overflowed,
474 the stack pointer must nevertheless point to an accessible address
475 by the time the EH return is executed.
477 We therefore adjust the saved value of the stack pointer by the size
478 of one page + a small dope of 4 words, in order to make sure that it
479 points to an accessible address in case it's used as the target CFA.
480 The stack checking code guarantees that this address is unused by the
481 time this happens. */
483 #if defined (__i386__)
484 unsigned long *pc = (unsigned long *)mcontext->gregs[REG_EIP];
485 /* The pattern is "orl $0x0,(%esp)" for a probe in 32-bit mode. */
486 if (signo == SIGSEGV && pc && *pc == 0x00240c83)
487 mcontext->gregs[REG_ESP] += 4096 + 4 * sizeof (unsigned long);
488 #elif defined (__x86_64__)
489 unsigned long long *pc = (unsigned long long *)mcontext->gregs[REG_RIP];
490 if (signo == SIGSEGV && pc
491 /* The pattern is "orq $0x0,(%rsp)" for a probe in 64-bit mode. */
492 && ((*pc & 0xffffffffffLL) == 0x00240c8348LL
493 /* The pattern may also be "orl $0x0,(%esp)" for a probe in
494 x32 mode. */
495 || (*pc & 0xffffffffLL) == 0x00240c83LL))
496 mcontext->gregs[REG_RSP] += 4096 + 4 * sizeof (unsigned long);
497 #elif defined (__ia64__)
498 /* ??? The IA-64 unwinder doesn't compensate for signals. */
499 mcontext->sc_ip++;
500 #elif defined (__ARMEL__)
501 /* ARM Bump has to be an even number because of odd/even architecture. */
502 mcontext->arm_pc+=2;
503 #endif
506 #endif
508 static void
509 __gnat_error_handler (int sig, siginfo_t *si ATTRIBUTE_UNUSED, void *ucontext)
511 struct Exception_Data *exception;
512 const char *msg;
514 /* Adjusting is required for every fault context, so adjust for this one
515 now, before we possibly trigger a recursive fault below. */
516 __gnat_adjust_context_for_raise (sig, ucontext);
518 switch (sig)
520 case SIGSEGV:
521 /* Here we would like a discrimination test to see whether the page
522 before the faulting address is accessible. Unfortunately, Linux
523 seems to have no way of giving us the faulting address.
525 In old versions of init.c, we had a test of the page before the
526 stack pointer:
528 ((volatile char *)
529 ((long) si->esp_at_signal & - getpagesize ()))[getpagesize ()];
531 but that's wrong since it tests the stack pointer location and the
532 stack probing code may not move it until all probes succeed.
534 For now we simply do not attempt any discrimination at all. Note
535 that this is quite acceptable, since a "real" SIGSEGV can only
536 occur as the result of an erroneous program. */
537 exception = &storage_error;
538 msg = "stack overflow or erroneous memory access";
539 break;
541 case SIGBUS:
542 exception = &storage_error;
543 msg = "SIGBUS: possible stack overflow";
544 break;
546 case SIGFPE:
547 exception = &constraint_error;
548 msg = "SIGFPE";
549 break;
551 default:
552 exception = &program_error;
553 msg = "unhandled signal";
556 Raise_From_Signal_Handler (exception, msg);
559 #ifndef __ia64__
560 #define HAVE_GNAT_ALTERNATE_STACK 1
561 /* This must be in keeping with System.OS_Interface.Alternate_Stack_Size.
562 It must be larger than MINSIGSTKSZ and hopefully near 2 * SIGSTKSZ. */
563 # if 16 * 1024 < MINSIGSTKSZ
564 # error "__gnat_alternate_stack too small"
565 # endif
566 char __gnat_alternate_stack[16 * 1024];
567 #endif
569 #ifdef __XENO__
570 #include <sys/mman.h>
571 #include <native/task.h>
573 RT_TASK main_task;
574 #endif
576 void
577 __gnat_install_handler (void)
579 struct sigaction act;
581 #ifdef __XENO__
582 int prio;
584 if (__gl_main_priority == -1)
585 prio = 49;
586 else
587 prio = __gl_main_priority;
589 /* Avoid memory swapping for this program */
591 mlockall (MCL_CURRENT|MCL_FUTURE);
593 /* Turn the current Linux task into a native Xenomai task */
595 rt_task_shadow (&main_task, "environment_task", prio, T_FPU);
596 #endif
598 /* Set up signal handler to map synchronous signals to appropriate
599 exceptions. Make sure that the handler isn't interrupted by another
600 signal that might cause a scheduling event! Also setup an alternate
601 stack region for the handler execution so that stack overflows can be
602 handled properly, avoiding a SEGV generation from stack usage by the
603 handler itself. */
605 act.sa_sigaction = __gnat_error_handler;
606 act.sa_flags = SA_NODEFER | SA_RESTART | SA_SIGINFO;
607 sigemptyset (&act.sa_mask);
609 /* Do not install handlers if interrupt state is "System". */
610 if (__gnat_get_interrupt_state (SIGABRT) != 's')
611 sigaction (SIGABRT, &act, NULL);
612 if (__gnat_get_interrupt_state (SIGFPE) != 's')
613 sigaction (SIGFPE, &act, NULL);
614 if (__gnat_get_interrupt_state (SIGILL) != 's')
615 sigaction (SIGILL, &act, NULL);
616 if (__gnat_get_interrupt_state (SIGBUS) != 's')
617 sigaction (SIGBUS, &act, NULL);
618 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
620 #ifdef HAVE_GNAT_ALTERNATE_STACK
621 /* Setup an alternate stack region for the handler execution so that
622 stack overflows can be handled properly, avoiding a SEGV generation
623 from stack usage by the handler itself. */
624 stack_t stack;
626 stack.ss_sp = __gnat_alternate_stack;
627 stack.ss_size = sizeof (__gnat_alternate_stack);
628 stack.ss_flags = 0;
629 sigaltstack (&stack, NULL);
631 act.sa_flags |= SA_ONSTACK;
632 #endif
633 sigaction (SIGSEGV, &act, NULL);
636 __gnat_handler_installed = 1;
639 /*******************/
640 /* LynxOS Section */
641 /*******************/
643 #elif defined (__Lynx__)
645 #include <signal.h>
646 #include <unistd.h>
648 static void
649 __gnat_error_handler (int sig)
651 struct Exception_Data *exception;
652 const char *msg;
654 switch(sig)
656 case SIGFPE:
657 exception = &constraint_error;
658 msg = "SIGFPE";
659 break;
660 case SIGILL:
661 exception = &constraint_error;
662 msg = "SIGILL";
663 break;
664 case SIGSEGV:
665 exception = &storage_error;
666 msg = "stack overflow or erroneous memory access";
667 break;
668 case SIGBUS:
669 exception = &constraint_error;
670 msg = "SIGBUS";
671 break;
672 default:
673 exception = &program_error;
674 msg = "unhandled signal";
677 Raise_From_Signal_Handler (exception, msg);
680 void
681 __gnat_install_handler(void)
683 struct sigaction act;
685 act.sa_handler = __gnat_error_handler;
686 act.sa_flags = 0x0;
687 sigemptyset (&act.sa_mask);
689 /* Do not install handlers if interrupt state is "System". */
690 if (__gnat_get_interrupt_state (SIGFPE) != 's')
691 sigaction (SIGFPE, &act, NULL);
692 if (__gnat_get_interrupt_state (SIGILL) != 's')
693 sigaction (SIGILL, &act, NULL);
694 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
695 sigaction (SIGSEGV, &act, NULL);
696 if (__gnat_get_interrupt_state (SIGBUS) != 's')
697 sigaction (SIGBUS, &act, NULL);
699 __gnat_handler_installed = 1;
702 /*******************/
703 /* Solaris Section */
704 /*******************/
706 #elif defined (__sun__) && !defined (__vxworks)
708 #include <signal.h>
709 #include <siginfo.h>
710 #include <sys/ucontext.h>
711 #include <sys/regset.h>
713 static void
714 __gnat_error_handler (int sig, siginfo_t *si, void *ucontext ATTRIBUTE_UNUSED)
716 struct Exception_Data *exception;
717 static int recurse = 0;
718 const char *msg;
720 switch (sig)
722 case SIGSEGV:
723 /* If the problem was permissions, this is a constraint error.
724 Likewise if the failing address isn't maximally aligned or if
725 we've recursed.
727 ??? Using a static variable here isn't task-safe, but it's
728 much too hard to do anything else and we're just determining
729 which exception to raise. */
730 if (si->si_code == SEGV_ACCERR
731 || (long) si->si_addr == 0
732 || (((long) si->si_addr) & 3) != 0
733 || recurse)
735 exception = &constraint_error;
736 msg = "SIGSEGV";
738 else
740 /* See if the page before the faulting page is accessible. Do that
741 by trying to access it. We'd like to simply try to access
742 4096 + the faulting address, but it's not guaranteed to be
743 the actual address, just to be on the same page. */
744 recurse++;
745 ((volatile char *)
746 ((long) si->si_addr & - getpagesize ()))[getpagesize ()];
747 exception = &storage_error;
748 msg = "stack overflow or erroneous memory access";
750 break;
752 case SIGBUS:
753 exception = &program_error;
754 msg = "SIGBUS";
755 break;
757 case SIGFPE:
758 exception = &constraint_error;
759 msg = "SIGFPE";
760 break;
762 default:
763 exception = &program_error;
764 msg = "unhandled signal";
767 recurse = 0;
768 Raise_From_Signal_Handler (exception, msg);
771 void
772 __gnat_install_handler (void)
774 struct sigaction act;
776 /* Set up signal handler to map synchronous signals to appropriate
777 exceptions. Make sure that the handler isn't interrupted by another
778 signal that might cause a scheduling event! */
780 act.sa_sigaction = __gnat_error_handler;
781 act.sa_flags = SA_NODEFER | SA_RESTART | SA_SIGINFO;
782 sigemptyset (&act.sa_mask);
784 /* Do not install handlers if interrupt state is "System". */
785 if (__gnat_get_interrupt_state (SIGABRT) != 's')
786 sigaction (SIGABRT, &act, NULL);
787 if (__gnat_get_interrupt_state (SIGFPE) != 's')
788 sigaction (SIGFPE, &act, NULL);
789 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
790 sigaction (SIGSEGV, &act, NULL);
791 if (__gnat_get_interrupt_state (SIGBUS) != 's')
792 sigaction (SIGBUS, &act, NULL);
794 __gnat_handler_installed = 1;
797 /***************/
798 /* VMS Section */
799 /***************/
801 #elif defined (VMS)
803 /* Routine called from binder to override default feature values. */
804 void __gnat_set_features (void);
805 int __gnat_features_set = 0;
806 void (*__gnat_ctrl_c_handler) (void) = 0;
808 #ifdef __IA64
809 #define lib_get_curr_invo_context LIB$I64_GET_CURR_INVO_CONTEXT
810 #define lib_get_prev_invo_context LIB$I64_GET_PREV_INVO_CONTEXT
811 #define lib_get_invo_handle LIB$I64_GET_INVO_HANDLE
812 #else
813 #define lib_get_curr_invo_context LIB$GET_CURR_INVO_CONTEXT
814 #define lib_get_prev_invo_context LIB$GET_PREV_INVO_CONTEXT
815 #define lib_get_invo_handle LIB$GET_INVO_HANDLE
816 #endif
818 /* Masks for facility identification. */
819 #define FAC_MASK 0x0fff0000
820 #define DECADA_M_FACILITY 0x00310000
822 /* Define macro symbols for the VMS conditions that become Ada exceptions.
823 It would be better to just include <ssdef.h> */
825 #define SS$_CONTINUE 1
826 #define SS$_ACCVIO 12
827 #define SS$_HPARITH 1284
828 #define SS$_INTDIV 1156
829 #define SS$_STKOVF 1364
830 #define SS$_CONTROLC 1617
831 #define SS$_RESIGNAL 2328
833 #define MTH$_FLOOVEMAT 1475268 /* Some ACVC_21 CXA tests */
835 /* The following codes must be resignalled, and not handled here. */
837 /* These codes are in standard message libraries. */
838 extern int C$_SIGKILL;
839 extern int C$_SIGINT;
840 extern int SS$_DEBUG;
841 extern int LIB$_KEYNOTFOU;
842 extern int LIB$_ACTIMAGE;
844 /* These codes are non standard, which is to say the author is
845 not sure if they are defined in the standard message libraries
846 so keep them as macros for now. */
847 #define RDB$_STREAM_EOF 20480426
848 #define FDL$_UNPRIKW 11829410
849 #define CMA$_EXIT_THREAD 4227492
851 struct cond_sigargs
853 unsigned int sigarg;
854 unsigned int sigargval;
857 struct cond_subtests
859 unsigned int num;
860 const struct cond_sigargs sigargs[];
863 struct cond_except
865 unsigned int cond;
866 const struct Exception_Data *except;
867 unsigned int needs_adjust; /* 1 = adjust PC, 0 = no adjust */
868 const struct cond_subtests *subtests;
871 struct descriptor_s
873 unsigned short len, mbz;
874 __char_ptr32 adr;
877 /* Conditions that don't have an Ada exception counterpart must raise
878 Non_Ada_Error. Since this is defined in s-auxdec, it should only be
879 referenced by user programs, not the compiler or tools. Hence the
880 #ifdef IN_RTS. */
882 #ifdef IN_RTS
884 #define Status_Error ada__io_exceptions__status_error
885 extern struct Exception_Data Status_Error;
887 #define Mode_Error ada__io_exceptions__mode_error
888 extern struct Exception_Data Mode_Error;
890 #define Name_Error ada__io_exceptions__name_error
891 extern struct Exception_Data Name_Error;
893 #define Use_Error ada__io_exceptions__use_error
894 extern struct Exception_Data Use_Error;
896 #define Device_Error ada__io_exceptions__device_error
897 extern struct Exception_Data Device_Error;
899 #define End_Error ada__io_exceptions__end_error
900 extern struct Exception_Data End_Error;
902 #define Data_Error ada__io_exceptions__data_error
903 extern struct Exception_Data Data_Error;
905 #define Layout_Error ada__io_exceptions__layout_error
906 extern struct Exception_Data Layout_Error;
908 #define Non_Ada_Error system__aux_dec__non_ada_error
909 extern struct Exception_Data Non_Ada_Error;
911 #define Coded_Exception system__vms_exception_table__coded_exception
912 extern struct Exception_Data *Coded_Exception (void *);
914 #define Base_Code_In system__vms_exception_table__base_code_in
915 extern void *Base_Code_In (void *);
917 /* DEC Ada exceptions are not defined in a header file, so they
918 must be declared. */
920 #define ADA$_ALREADY_OPEN 0x0031a594
921 #define ADA$_CONSTRAINT_ERRO 0x00318324
922 #define ADA$_DATA_ERROR 0x003192c4
923 #define ADA$_DEVICE_ERROR 0x003195e4
924 #define ADA$_END_ERROR 0x00319904
925 #define ADA$_FAC_MODE_MISMAT 0x0031a8b3
926 #define ADA$_IOSYSFAILED 0x0031af04
927 #define ADA$_KEYSIZERR 0x0031aa3c
928 #define ADA$_KEY_MISMATCH 0x0031a8e3
929 #define ADA$_LAYOUT_ERROR 0x00319c24
930 #define ADA$_LINEXCMRS 0x0031a8f3
931 #define ADA$_MAXLINEXC 0x0031a8eb
932 #define ADA$_MODE_ERROR 0x00319f44
933 #define ADA$_MRN_MISMATCH 0x0031a8db
934 #define ADA$_MRS_MISMATCH 0x0031a8d3
935 #define ADA$_NAME_ERROR 0x0031a264
936 #define ADA$_NOT_OPEN 0x0031a58c
937 #define ADA$_ORG_MISMATCH 0x0031a8bb
938 #define ADA$_PROGRAM_ERROR 0x00318964
939 #define ADA$_RAT_MISMATCH 0x0031a8cb
940 #define ADA$_RFM_MISMATCH 0x0031a8c3
941 #define ADA$_STAOVF 0x00318cac
942 #define ADA$_STATUS_ERROR 0x0031a584
943 #define ADA$_STORAGE_ERROR 0x00318c84
944 #define ADA$_UNSUPPORTED 0x0031a8ab
945 #define ADA$_USE_ERROR 0x0031a8a4
947 /* DEC Ada specific conditions. */
948 static const struct cond_except dec_ada_cond_except_table [] =
950 {ADA$_PROGRAM_ERROR, &program_error, 0, 0},
951 {ADA$_USE_ERROR, &Use_Error, 0, 0},
952 {ADA$_KEYSIZERR, &program_error, 0, 0},
953 {ADA$_STAOVF, &storage_error, 0, 0},
954 {ADA$_CONSTRAINT_ERRO, &constraint_error, 0, 0},
955 {ADA$_IOSYSFAILED, &Device_Error, 0, 0},
956 {ADA$_LAYOUT_ERROR, &Layout_Error, 0, 0},
957 {ADA$_STORAGE_ERROR, &storage_error, 0, 0},
958 {ADA$_DATA_ERROR, &Data_Error, 0, 0},
959 {ADA$_DEVICE_ERROR, &Device_Error, 0, 0},
960 {ADA$_END_ERROR, &End_Error, 0, 0},
961 {ADA$_MODE_ERROR, &Mode_Error, 0, 0},
962 {ADA$_NAME_ERROR, &Name_Error, 0, 0},
963 {ADA$_STATUS_ERROR, &Status_Error, 0, 0},
964 {ADA$_NOT_OPEN, &Use_Error, 0, 0},
965 {ADA$_ALREADY_OPEN, &Use_Error, 0, 0},
966 {ADA$_USE_ERROR, &Use_Error, 0, 0},
967 {ADA$_UNSUPPORTED, &Use_Error, 0, 0},
968 {ADA$_FAC_MODE_MISMAT, &Use_Error, 0, 0},
969 {ADA$_ORG_MISMATCH, &Use_Error, 0, 0},
970 {ADA$_RFM_MISMATCH, &Use_Error, 0, 0},
971 {ADA$_RAT_MISMATCH, &Use_Error, 0, 0},
972 {ADA$_MRS_MISMATCH, &Use_Error, 0, 0},
973 {ADA$_MRN_MISMATCH, &Use_Error, 0, 0},
974 {ADA$_KEY_MISMATCH, &Use_Error, 0, 0},
975 {ADA$_MAXLINEXC, &constraint_error, 0, 0},
976 {ADA$_LINEXCMRS, &constraint_error, 0, 0},
978 #if 0
979 /* Already handled by a pragma Import_Exception
980 in Aux_IO_Exceptions */
981 {ADA$_LOCK_ERROR, &Lock_Error, 0, 0},
982 {ADA$_EXISTENCE_ERROR, &Existence_Error, 0, 0},
983 {ADA$_KEY_ERROR, &Key_Error, 0, 0},
984 #endif
986 {0, 0, 0, 0}
989 #endif /* IN_RTS */
991 /* Non-DEC Ada specific conditions that map to Ada exceptions. */
993 /* Subtest for ACCVIO Constraint_Error, kept for compatibility,
994 in hindsight should have just made ACCVIO == Storage_Error. */
995 #define ACCVIO_VIRTUAL_ADDR 3
996 static const struct cond_subtests accvio_c_e =
997 {1, /* number of subtests below */
999 { ACCVIO_VIRTUAL_ADDR, 0 }
1003 /* Macro flag to adjust PC which gets off by one for some conditions,
1004 not sure if this is reliably true, PC could be off by more for
1005 HPARITH for example, unless a trapb is inserted. */
1006 #define NEEDS_ADJUST 1
1008 static const struct cond_except system_cond_except_table [] =
1010 {MTH$_FLOOVEMAT, &constraint_error, 0, 0},
1011 {SS$_INTDIV, &constraint_error, 0, 0},
1012 {SS$_HPARITH, &constraint_error, NEEDS_ADJUST, 0},
1013 {SS$_ACCVIO, &constraint_error, NEEDS_ADJUST, &accvio_c_e},
1014 {SS$_ACCVIO, &storage_error, NEEDS_ADJUST, 0},
1015 {SS$_STKOVF, &storage_error, NEEDS_ADJUST, 0},
1016 {0, 0, 0, 0}
1019 /* To deal with VMS conditions and their mapping to Ada exceptions,
1020 the __gnat_error_handler routine below is installed as an exception
1021 vector having precedence over DEC frame handlers. Some conditions
1022 still need to be handled by such handlers, however, in which case
1023 __gnat_error_handler needs to return SS$_RESIGNAL. Consider for
1024 instance the use of a third party library compiled with DECAda and
1025 performing its own exception handling internally.
1027 To allow some user-level flexibility, which conditions should be
1028 resignaled is controlled by a predicate function, provided with the
1029 condition value and returning a boolean indication stating whether
1030 this condition should be resignaled or not.
1032 That predicate function is called indirectly, via a function pointer,
1033 by __gnat_error_handler, and changing that pointer is allowed to the
1034 user code by way of the __gnat_set_resignal_predicate interface.
1036 The user level function may then implement what it likes, including
1037 for instance the maintenance of a dynamic data structure if the set
1038 of to be resignalled conditions has to change over the program's
1039 lifetime.
1041 ??? This is not a perfect solution to deal with the possible
1042 interactions between the GNAT and the DECAda exception handling
1043 models and better (more general) schemes are studied. This is so
1044 just provided as a convenient workaround in the meantime, and
1045 should be use with caution since the implementation has been kept
1046 very simple. */
1048 typedef int resignal_predicate (int code);
1050 static const int * const cond_resignal_table [] =
1052 &C$_SIGKILL,
1053 (int *)CMA$_EXIT_THREAD,
1054 &SS$_DEBUG,
1055 &LIB$_KEYNOTFOU,
1056 &LIB$_ACTIMAGE,
1057 (int *) RDB$_STREAM_EOF,
1058 (int *) FDL$_UNPRIKW,
1062 static const int facility_resignal_table [] =
1064 0x1380000, /* RDB */
1065 0x2220000, /* SQL */
1069 /* Default GNAT predicate for resignaling conditions. */
1071 static int
1072 __gnat_default_resignal_p (int code)
1074 int i, iexcept;
1076 for (i = 0; facility_resignal_table [i]; i++)
1077 if ((code & FAC_MASK) == facility_resignal_table [i])
1078 return 1;
1080 for (i = 0, iexcept = 0;
1081 cond_resignal_table [i]
1082 && !(iexcept = LIB$MATCH_COND (&code, &cond_resignal_table [i]));
1083 i++);
1085 return iexcept;
1088 /* Static pointer to predicate that the __gnat_error_handler exception
1089 vector invokes to determine if it should resignal a condition. */
1091 static resignal_predicate *__gnat_resignal_p = __gnat_default_resignal_p;
1093 /* User interface to change the predicate pointer to PREDICATE. Reset to
1094 the default if PREDICATE is null. */
1096 void
1097 __gnat_set_resignal_predicate (resignal_predicate *predicate)
1099 if (predicate == NULL)
1100 __gnat_resignal_p = __gnat_default_resignal_p;
1101 else
1102 __gnat_resignal_p = predicate;
1105 /* Should match System.Parameters.Default_Exception_Msg_Max_Length. */
1106 #define Default_Exception_Msg_Max_Length 512
1108 /* Action routine for SYS$PUTMSG. There may be multiple
1109 conditions, each with text to be appended to MESSAGE
1110 and separated by line termination. */
1111 static int
1112 copy_msg (struct descriptor_s *msgdesc, char *message)
1114 int len = strlen (message);
1115 int copy_len;
1117 /* Check for buffer overflow and skip. */
1118 if (len > 0 && len <= Default_Exception_Msg_Max_Length - 3)
1120 strcat (message, "\r\n");
1121 len += 2;
1124 /* Check for buffer overflow and truncate if necessary. */
1125 copy_len = (len + msgdesc->len <= Default_Exception_Msg_Max_Length - 1 ?
1126 msgdesc->len :
1127 Default_Exception_Msg_Max_Length - 1 - len);
1128 strncpy (&message [len], msgdesc->adr, copy_len);
1129 message [len + copy_len] = 0;
1131 return 0;
1134 /* Scan TABLE for a match for the condition contained in SIGARGS,
1135 and return the entry, or the empty entry if no match found. */
1136 static const struct cond_except *
1137 scan_conditions ( int *sigargs, const struct cond_except *table [])
1139 int i;
1140 struct cond_except entry;
1142 /* Scan the exception condition table for a match and fetch
1143 the associated GNAT exception pointer. */
1144 for (i = 0; (*table) [i].cond; i++)
1146 unsigned int match = LIB$MATCH_COND (&sigargs [1], &(*table) [i].cond);
1147 const struct cond_subtests *subtests = (*table) [i].subtests;
1149 if (match)
1151 if (!subtests)
1153 return &(*table) [i];
1155 else
1157 unsigned int ii;
1158 int num = (*subtests).num;
1160 /* Perform subtests to differentiate exception. */
1161 for (ii = 0; ii < num; ii++)
1163 unsigned int arg = (*subtests).sigargs [ii].sigarg;
1164 unsigned int argval = (*subtests).sigargs [ii].sigargval;
1166 if (sigargs [arg] != argval)
1168 num = 0;
1169 break;
1173 /* All subtests passed. */
1174 if (num == (*subtests).num)
1175 return &(*table) [i];
1180 /* No match, return the null terminating entry. */
1181 return &(*table) [i];
1184 /* __gnat_handle_vms_condtition is both a frame based handler
1185 for the runtime, and an exception vector for the compiler. */
1186 long
1187 __gnat_handle_vms_condition (int *sigargs, void *mechargs)
1189 struct Exception_Data *exception = 0;
1190 unsigned int needs_adjust = 0;
1191 void *base_code;
1192 struct descriptor_s gnat_facility = {4, 0, "GNAT"};
1193 char message [Default_Exception_Msg_Max_Length];
1195 const char *msg = "";
1197 /* Check for conditions to resignal which aren't effected by pragma
1198 Import_Exception. */
1199 if (__gnat_resignal_p (sigargs [1]))
1200 return SS$_RESIGNAL;
1201 #ifndef IN_RTS
1202 /* toplev.c handles this for compiler. */
1203 if (sigargs [1] == SS$_HPARITH)
1204 return SS$_RESIGNAL;
1205 #endif
1207 #ifdef IN_RTS
1208 /* See if it's an imported exception. Beware that registered exceptions
1209 are bound to their base code, with the severity bits masked off. */
1210 base_code = Base_Code_In ((void *) sigargs[1]);
1211 exception = Coded_Exception (base_code);
1212 #endif
1214 if (exception == 0)
1215 #ifdef IN_RTS
1217 int i;
1218 struct cond_except cond;
1219 const struct cond_except *cond_table;
1220 const struct cond_except *cond_tables [] = {dec_ada_cond_except_table,
1221 system_cond_except_table,
1223 unsigned int ctrlc = SS$_CONTROLC;
1224 unsigned int *sigint = &C$_SIGINT;
1225 int ctrlc_match = LIB$MATCH_COND (&sigargs [1], &ctrlc);
1226 int sigint_match = LIB$MATCH_COND (&sigargs [1], &sigint);
1228 extern int SYS$DCLAST (void (*astadr)(), unsigned long long astprm,
1229 unsigned int acmode);
1231 /* If SS$_CONTROLC has been imported as an exception, it will take
1232 priority over a Ctrl/C handler. See above. SIGINT has a
1233 different condition value due to it's DECCCRTL roots and it's
1234 the condition that gets raised for a "kill -INT". */
1235 if ((ctrlc_match || sigint_match) && __gnat_ctrl_c_handler)
1237 SYS$DCLAST (__gnat_ctrl_c_handler, 0, 0);
1238 return SS$_CONTINUE;
1241 i = 0;
1242 while ((cond_table = cond_tables[i++]) && !exception)
1244 cond = *scan_conditions (sigargs, &cond_table);
1245 exception = (struct Exception_Data *) cond.except;
1248 if (exception)
1249 needs_adjust = cond.needs_adjust;
1250 else
1251 /* User programs expect Non_Ada_Error to be raised if no match,
1252 reference DEC Ada test CXCONDHAN. */
1253 exception = &Non_Ada_Error;
1255 #else
1257 /* Pretty much everything is just a program error in the compiler */
1258 exception = &program_error;
1260 #endif
1262 message[0] = 0;
1263 /* Subtract PC & PSL fields as per ABI for SYS$PUTMSG. */
1264 sigargs[0] -= 2;
1266 extern int SYS$PUTMSG (void *, int (*)(), void *, unsigned long long);
1268 /* If it was a DEC Ada specific condtiion, make it GNAT otherwise
1269 keep the old facility. */
1270 if (sigargs [1] & FAC_MASK == DECADA_M_FACILITY)
1271 SYS$PUTMSG (sigargs, copy_msg, &gnat_facility,
1272 (unsigned long long ) message);
1273 else
1274 SYS$PUTMSG (sigargs, copy_msg, 0,
1275 (unsigned long long ) message);
1277 /* Add back PC & PSL fields as per ABI for SYS$PUTMSG. */
1278 sigargs[0] += 2;
1279 msg = message;
1281 if (needs_adjust)
1282 __gnat_adjust_context_for_raise (sigargs [1], (void *)mechargs);
1284 Raise_From_Signal_Handler (exception, msg);
1287 #if defined (IN_RTS) && defined (__IA64)
1288 /* Called only from adasigio.b32. This is a band aid to avoid going
1289 through the VMS signal handling code which results in a 0x8000 per
1290 handled exception memory leak in P2 space (see VMS source listing
1291 sys/lis/exception.lis) due to the allocation of working space that
1292 is expected to be deallocated upon return from the condition handler,
1293 which doesn't return in GNAT compiled code. */
1294 void
1295 GNAT$STOP (int *sigargs)
1297 /* Note that there are no mechargs. We rely on the fact that condtions
1298 raised from DEClib I/O do not require an "adjust". Also the count
1299 will be off by 2, since LIB$STOP didn't get a chance to add the
1300 PC and PSL fields, so we bump it so PUTMSG comes out right. */
1301 sigargs [0] += 2;
1302 __gnat_handle_vms_condition (sigargs, 0);
1304 #endif
1306 void
1307 __gnat_install_handler (void)
1309 long prvhnd ATTRIBUTE_UNUSED;
1311 #if !defined (IN_RTS)
1312 extern int SYS$SETEXV (unsigned int vector, int (*addres)(),
1313 unsigned int accmode, void *(*(prvhnd)));
1314 SYS$SETEXV (1, __gnat_handle_vms_condition, 3, &prvhnd);
1315 #endif
1317 __gnat_handler_installed = 1;
1320 /* __gnat_adjust_context_for_raise for Alpha - see comments along with the
1321 default version later in this file. */
1323 #if defined (IN_RTS) && defined (__alpha__)
1325 #include <vms/chfctxdef.h>
1326 #include <vms/chfdef.h>
1328 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
1330 void
1331 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED, void *ucontext)
1333 if (signo == SS$_HPARITH)
1335 /* Sub one to the address of the instruction signaling the condition,
1336 located in the sigargs array. */
1338 CHF$MECH_ARRAY * mechargs = (CHF$MECH_ARRAY *) ucontext;
1339 CHF$SIGNAL_ARRAY * sigargs
1340 = (CHF$SIGNAL_ARRAY *) mechargs->chf$q_mch_sig_addr;
1342 int vcount = sigargs->chf$is_sig_args;
1343 int * pc_slot = & (&sigargs->chf$l_sig_name)[vcount-2];
1345 (*pc_slot)--;
1349 #endif
1351 /* __gnat_adjust_context_for_raise for ia64. */
1353 #if defined (IN_RTS) && defined (__IA64)
1355 #include <vms/chfctxdef.h>
1356 #include <vms/chfdef.h>
1358 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
1360 typedef unsigned long long u64;
1362 void
1363 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED, void *ucontext)
1365 /* Add one to the address of the instruction signaling the condition,
1366 located in the 64bits sigargs array. */
1368 CHF$MECH_ARRAY * mechargs = (CHF$MECH_ARRAY *) ucontext;
1370 CHF64$SIGNAL_ARRAY *chfsig64
1371 = (CHF64$SIGNAL_ARRAY *) mechargs->chf$ph_mch_sig64_addr;
1373 u64 * post_sigarray
1374 = (u64 *)chfsig64 + 1 + chfsig64->chf64$l_sig_args;
1376 u64 * ih_pc_loc = post_sigarray - 2;
1378 (*ih_pc_loc) ++;
1381 #endif
1383 /* Easier interface for LIB$GET_LOGICAL: put the equivalence of NAME into BUF,
1384 always NUL terminated. In case of error or if the result is longer than
1385 LEN (length of BUF) an empty string is written info BUF. */
1387 static void
1388 __gnat_vms_get_logical (const char *name, char *buf, int len)
1390 struct descriptor_s name_desc, result_desc;
1391 int status;
1392 unsigned short rlen;
1394 /* Build the descriptor for NAME. */
1395 name_desc.len = strlen (name);
1396 name_desc.mbz = 0;
1397 name_desc.adr = (char *)name;
1399 /* Build the descriptor for the result. */
1400 result_desc.len = len;
1401 result_desc.mbz = 0;
1402 result_desc.adr = buf;
1404 status = LIB$GET_LOGICAL (&name_desc, &result_desc, &rlen);
1406 if ((status & 1) == 1 && rlen < len)
1407 buf[rlen] = 0;
1408 else
1409 buf[0] = 0;
1412 /* Size of a page on ia64 and alpha VMS. */
1413 #define VMS_PAGESIZE 8192
1415 /* User mode. */
1416 #define PSL__C_USER 3
1418 /* No access. */
1419 #define PRT__C_NA 0
1421 /* Descending region. */
1422 #define VA__M_DESCEND 1
1424 /* Get by virtual address. */
1425 #define VA___REGSUM_BY_VA 1
1427 /* Memory region summary. */
1428 struct regsum
1430 unsigned long long q_region_id;
1431 unsigned int l_flags;
1432 unsigned int l_region_protection;
1433 void *pq_start_va;
1434 unsigned long long q_region_size;
1435 void *pq_first_free_va;
1438 extern int SYS$GET_REGION_INFO (unsigned int, unsigned long long *,
1439 void *, void *, unsigned int,
1440 void *, unsigned int *);
1441 extern int SYS$EXPREG_64 (unsigned long long *, unsigned long long,
1442 unsigned int, unsigned int, void **,
1443 unsigned long long *);
1444 extern int SYS$SETPRT_64 (void *, unsigned long long, unsigned int,
1445 unsigned int, void **, unsigned long long *,
1446 unsigned int *);
1448 /* Add a guard page in the memory region containing ADDR at ADDR +/- SIZE.
1449 (The sign depends on the kind of the memory region). */
1451 static int
1452 __gnat_set_stack_guard_page (void *addr, unsigned long size)
1454 int status;
1455 void *ret_va;
1456 unsigned long long ret_len;
1457 unsigned int ret_prot;
1458 void *start_va;
1459 unsigned long long length;
1460 unsigned int retlen;
1461 struct regsum buffer;
1463 /* Get the region for ADDR. */
1464 status = SYS$GET_REGION_INFO
1465 (VA___REGSUM_BY_VA, NULL, addr, NULL, sizeof (buffer), &buffer, &retlen);
1467 if ((status & 1) != 1)
1468 return -1;
1470 /* Extend the region. */
1471 status = SYS$EXPREG_64 (&buffer.q_region_id,
1472 size, 0, 0, &start_va, &length);
1474 if ((status & 1) != 1)
1475 return -1;
1477 /* Create a guard page. */
1478 if (!(buffer.l_flags & VA__M_DESCEND))
1479 start_va = (void *)((unsigned long long)start_va + length - VMS_PAGESIZE);
1481 status = SYS$SETPRT_64 (start_va, VMS_PAGESIZE, PSL__C_USER, PRT__C_NA,
1482 &ret_va, &ret_len, &ret_prot);
1484 if ((status & 1) != 1)
1485 return -1;
1486 return 0;
1489 /* Read logicals to limit the stack(s) size. */
1491 static void
1492 __gnat_set_stack_limit (void)
1494 #ifdef __ia64__
1495 void *sp;
1496 unsigned long size;
1497 char value[16];
1498 char *e;
1500 /* The main stack. */
1501 __gnat_vms_get_logical ("GNAT_STACK_SIZE", value, sizeof (value));
1502 size = strtoul (value, &e, 0);
1503 if (e > value && *e == 0)
1505 asm ("mov %0=sp" : "=r" (sp));
1506 __gnat_set_stack_guard_page (sp, size * 1024);
1509 /* The register stack. */
1510 __gnat_vms_get_logical ("GNAT_RBS_SIZE", value, sizeof (value));
1511 size = strtoul (value, &e, 0);
1512 if (e > value && *e == 0)
1514 asm ("mov %0=ar.bsp" : "=r" (sp));
1515 __gnat_set_stack_guard_page (sp, size * 1024);
1517 #endif
1520 #ifdef IN_RTS
1521 extern int SYS$IEEE_SET_FP_CONTROL (void *, void *, void *);
1522 #define K_TRUE 1
1523 #define __int64 long long
1524 #define __NEW_STARLET
1525 #include <vms/ieeedef.h>
1526 #endif
1528 /* Feature logical name and global variable address pair.
1529 If we ever add another feature logical to this list, the
1530 feature struct will need to be enhanced to take into account
1531 possible values for *gl_addr. */
1532 struct feature {
1533 const char *name;
1534 int *gl_addr;
1537 /* Default values for GNAT features set by environment or binder. */
1538 int __gl_heap_size = 64;
1540 /* Default float format is 'I' meaning IEEE. If gnatbind detetcts that a
1541 VAX Float format is specified, it will set this global variable to 'V'.
1542 Subsequently __gnat_set_features will test the variable and if set for
1543 VAX Float will call a Starlet function to enable trapping for invalid
1544 operation, drivide by zero, and overflow. This will prevent the VMS runtime
1545 (specifically OTS$CHECK_FP_MODE) from complaining about inconsistent
1546 floating point settings in a mixed language program. Ideally the setting
1547 would be determined at link time based on setttings in the object files,
1548 however the VMS linker seems to take the setting from the first object
1549 in the link, e.g. pcrt0.o which is float representation neutral. */
1550 char __gl_float_format = 'I';
1552 /* Array feature logical names and global variable addresses. */
1553 static const struct feature features[] =
1555 {"GNAT$NO_MALLOC_64", &__gl_heap_size},
1556 {0, 0}
1559 void
1560 __gnat_set_features (void)
1562 int i;
1563 char buff[16];
1564 #ifdef IN_RTS
1565 IEEE clrmsk, setmsk, prvmsk;
1567 clrmsk.ieee$q_flags = 0LL;
1568 setmsk.ieee$q_flags = 0LL;
1569 #endif
1571 /* Loop through features array and test name for enable/disable. */
1572 for (i = 0; features[i].name; i++)
1574 __gnat_vms_get_logical (features[i].name, buff, sizeof (buff));
1576 if (strcmp (buff, "ENABLE") == 0
1577 || strcmp (buff, "TRUE") == 0
1578 || strcmp (buff, "1") == 0)
1579 *features[i].gl_addr = 32;
1580 else if (strcmp (buff, "DISABLE") == 0
1581 || strcmp (buff, "FALSE") == 0
1582 || strcmp (buff, "0") == 0)
1583 *features[i].gl_addr = 64;
1586 /* Features to artificially limit the stack size. */
1587 __gnat_set_stack_limit ();
1589 #ifdef IN_RTS
1590 if (__gl_float_format == 'V')
1592 setmsk.ieee$v_trap_enable_inv = K_TRUE;
1593 setmsk.ieee$v_trap_enable_dze = K_TRUE;
1594 setmsk.ieee$v_trap_enable_ovf = K_TRUE;
1595 SYS$IEEE_SET_FP_CONTROL (&clrmsk, &setmsk, &prvmsk);
1597 #endif
1599 __gnat_features_set = 1;
1602 /* Return true if the VMS version is 7.x. */
1604 extern unsigned int LIB$GETSYI (int *, ...);
1606 #define SYI$_VERSION 0x1000
1609 __gnat_is_vms_v7 (void)
1611 struct descriptor_s desc;
1612 char version[8];
1613 int status;
1614 int code = SYI$_VERSION;
1616 desc.len = sizeof (version);
1617 desc.mbz = 0;
1618 desc.adr = version;
1620 status = LIB$GETSYI (&code, 0, &desc);
1621 if ((status & 1) == 1 && version[1] == '7' && version[2] == '.')
1622 return 1;
1623 else
1624 return 0;
1627 /*******************/
1628 /* FreeBSD Section */
1629 /*******************/
1631 #elif defined (__FreeBSD__) || defined (__DragonFly__)
1633 #include <signal.h>
1634 #include <sys/ucontext.h>
1635 #include <unistd.h>
1637 static void
1638 __gnat_error_handler (int sig,
1639 siginfo_t *si ATTRIBUTE_UNUSED,
1640 void *ucontext ATTRIBUTE_UNUSED)
1642 struct Exception_Data *exception;
1643 const char *msg;
1645 switch (sig)
1647 case SIGFPE:
1648 exception = &constraint_error;
1649 msg = "SIGFPE";
1650 break;
1652 case SIGILL:
1653 exception = &constraint_error;
1654 msg = "SIGILL";
1655 break;
1657 case SIGSEGV:
1658 exception = &storage_error;
1659 msg = "stack overflow or erroneous memory access";
1660 break;
1662 case SIGBUS:
1663 exception = &storage_error;
1664 msg = "SIGBUS: possible stack overflow";
1665 break;
1667 default:
1668 exception = &program_error;
1669 msg = "unhandled signal";
1672 Raise_From_Signal_Handler (exception, msg);
1675 void
1676 __gnat_install_handler (void)
1678 struct sigaction act;
1680 /* Set up signal handler to map synchronous signals to appropriate
1681 exceptions. Make sure that the handler isn't interrupted by another
1682 signal that might cause a scheduling event! */
1684 act.sa_sigaction
1685 = (void (*)(int, struct __siginfo *, void*)) __gnat_error_handler;
1686 act.sa_flags = SA_NODEFER | SA_RESTART | SA_SIGINFO;
1687 (void) sigemptyset (&act.sa_mask);
1689 (void) sigaction (SIGILL, &act, NULL);
1690 (void) sigaction (SIGFPE, &act, NULL);
1691 (void) sigaction (SIGSEGV, &act, NULL);
1692 (void) sigaction (SIGBUS, &act, NULL);
1694 __gnat_handler_installed = 1;
1697 /*************************************/
1698 /* VxWorks Section (including Vx653) */
1699 /*************************************/
1701 #elif defined(__vxworks)
1703 #include <signal.h>
1704 #include <taskLib.h>
1705 #if defined (__i386__) && !defined (VTHREADS)
1706 #include <sysLib.h>
1707 #endif
1709 #ifndef __RTP__
1710 #include <intLib.h>
1711 #include <iv.h>
1712 #endif
1714 #if defined (ARMEL) && (_WRS_VXWORKS_MAJOR == 6)
1715 #include <vmLib.h>
1716 #endif
1718 #ifdef VTHREADS
1719 #include "private/vThreadsP.h"
1720 #endif
1722 #ifndef __RTP__
1724 /* Directly vectored Interrupt routines are not supported when using RTPs. */
1726 extern int __gnat_inum_to_ivec (int);
1728 /* This is needed by the GNAT run time to handle Vxworks interrupts. */
1730 __gnat_inum_to_ivec (int num)
1732 return (int) INUM_TO_IVEC (num);
1734 #endif
1736 #if !defined(__alpha_vxworks) && ((_WRS_VXWORKS_MAJOR != 6) && (_WRS_VXWORKS_MAJOR != 7)) && !defined(__RTP__)
1738 /* getpid is used by s-parint.adb, but is not defined by VxWorks, except
1739 on Alpha VxWorks and VxWorks 6.x (including RTPs). */
1741 extern long getpid (void);
1743 long
1744 getpid (void)
1746 return taskIdSelf ();
1748 #endif
1750 /* VxWorks 653 vThreads expects the field excCnt to be zeroed when a signal is.
1751 handled. The VxWorks version of longjmp does this; GCC's builtin_longjmp
1752 doesn't. */
1753 void
1754 __gnat_clear_exception_count (void)
1756 #ifdef VTHREADS
1757 WIND_TCB *currentTask = (WIND_TCB *) taskIdSelf();
1759 currentTask->vThreads.excCnt = 0;
1760 #endif
1763 /* Handle different SIGnal to exception mappings in different VxWorks
1764 versions. */
1765 void
1766 __gnat_map_signal (int sig, siginfo_t *si ATTRIBUTE_UNUSED,
1767 void *sc ATTRIBUTE_UNUSED)
1769 struct Exception_Data *exception;
1770 const char *msg;
1772 switch (sig)
1774 case SIGFPE:
1775 exception = &constraint_error;
1776 msg = "SIGFPE";
1777 break;
1778 #ifdef VTHREADS
1779 #ifdef __VXWORKSMILS__
1780 case SIGILL:
1781 exception = &storage_error;
1782 msg = "SIGILL: possible stack overflow";
1783 break;
1784 case SIGSEGV:
1785 exception = &storage_error;
1786 msg = "SIGSEGV";
1787 break;
1788 case SIGBUS:
1789 exception = &program_error;
1790 msg = "SIGBUS";
1791 break;
1792 #else
1793 case SIGILL:
1794 exception = &constraint_error;
1795 msg = "Floating point exception or SIGILL";
1796 break;
1797 case SIGSEGV:
1798 exception = &storage_error;
1799 msg = "SIGSEGV";
1800 break;
1801 case SIGBUS:
1802 exception = &storage_error;
1803 msg = "SIGBUS: possible stack overflow";
1804 break;
1805 #endif
1806 #elif (_WRS_VXWORKS_MAJOR >= 6)
1807 case SIGILL:
1808 exception = &constraint_error;
1809 msg = "SIGILL";
1810 break;
1811 #ifdef __RTP__
1812 /* In RTP mode a SIGSEGV is most likely due to a stack overflow,
1813 since stack checking uses the probing mechanism. */
1814 case SIGSEGV:
1815 exception = &storage_error;
1816 msg = "SIGSEGV: possible stack overflow";
1817 break;
1818 case SIGBUS:
1819 exception = &program_error;
1820 msg = "SIGBUS";
1821 break;
1822 #else
1823 /* VxWorks 6 kernel mode with probing. SIGBUS for guard page hit */
1824 case SIGSEGV:
1825 exception = &storage_error;
1826 msg = "SIGSEGV";
1827 break;
1828 case SIGBUS:
1829 exception = &storage_error;
1830 msg = "SIGBUS: possible stack overflow";
1831 break;
1832 #endif
1833 #else
1834 /* VxWorks 5: a SIGILL is most likely due to a stack overflow,
1835 since stack checking uses the stack limit mechanism. */
1836 case SIGILL:
1837 exception = &storage_error;
1838 msg = "SIGILL: possible stack overflow";
1839 break;
1840 case SIGSEGV:
1841 exception = &storage_error;
1842 msg = "SIGSEGV";
1843 break;
1844 case SIGBUS:
1845 exception = &program_error;
1846 msg = "SIGBUS";
1847 break;
1848 #endif
1849 default:
1850 exception = &program_error;
1851 msg = "unhandled signal";
1854 /* On ARM VxWorks 6.x, the guard page is left un-armed by the kernel
1855 after being violated, so subsequent violations aren't detected.
1856 so we retrieve the address of the guard page from the TCB and compare it
1857 with the page that is violated (pREG 12 in the context) and re-arm that
1858 page if there's a match. Additionally we're are assured this is a
1859 genuine stack overflow condition and and set the message and exception
1860 to that effect. */
1861 #if defined (ARMEL) && (_WRS_VXWORKS_MAJOR == 6)
1863 /* We re-arm the guard page by marking it invalid */
1865 #define PAGE_SIZE 4096
1866 #define REG_IP 12
1868 if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL)
1870 TASK_ID tid = taskIdSelf ();
1871 WIND_TCB *pTcb = taskTcb (tid);
1872 unsigned long violated_page
1873 = ((struct sigcontext *) sc)->sc_pregs->r[REG_IP] & ~(PAGE_SIZE - 1);
1875 if ((unsigned long) (pTcb->pStackEnd - PAGE_SIZE) == violated_page)
1877 vmStateSet (NULL, violated_page,
1878 PAGE_SIZE, VM_STATE_MASK_VALID, VM_STATE_VALID_NOT);
1879 exception = &storage_error;
1881 switch (sig)
1883 case SIGSEGV:
1884 msg = "SIGSEGV: stack overflow";
1885 break;
1886 case SIGBUS:
1887 msg = "SIGBUS: stack overflow";
1888 break;
1889 case SIGILL:
1890 msg = "SIGILL: stack overflow";
1891 break;
1895 #endif /* defined (ARMEL) && (_WRS_VXWORKS_MAJOR == 6) */
1897 __gnat_clear_exception_count ();
1898 Raise_From_Signal_Handler (exception, msg);
1901 #if defined (__i386__) && !defined (VTHREADS)
1902 extern void
1903 __gnat_vxsim_error_handler (int sig, siginfo_t *si, void *sc);
1905 static int is_vxsim = 0;
1906 #endif
1908 /* Tasking and Non-tasking signal handler. Map SIGnal to Ada exception
1909 propagation after the required low level adjustments. */
1911 static void
1912 __gnat_error_handler (int sig, siginfo_t *si, void *sc)
1914 sigset_t mask;
1916 /* VxWorks will always mask out the signal during the signal handler and
1917 will reenable it on a longjmp. GNAT does not generate a longjmp to
1918 return from a signal handler so the signal will still be masked unless
1919 we unmask it. */
1920 sigprocmask (SIG_SETMASK, NULL, &mask);
1921 sigdelset (&mask, sig);
1922 sigprocmask (SIG_SETMASK, &mask, NULL);
1924 #if defined (__ARMEL__) || defined (__PPC__) || defined (__i386__)
1925 /* On certain targets, kernel mode, we process signals through a Call Frame
1926 Info trampoline, voiding the need for myriads of fallback_frame_state
1927 variants in the ZCX runtime. We have no simple way to distinguish ZCX
1928 from SJLJ here, so we do this for SJLJ as well even though this is not
1929 necessary. This only incurs a few extra instructions and a tiny
1930 amount of extra stack usage. */
1932 #if defined (__i386__) && !defined (VTHREADS)
1933 /* On x86, the vxsim signal context is subtly different and is processeed
1934 by a handler compiled especially for vxsim. */
1936 if (is_vxsim)
1937 __gnat_vxsim_error_handler (sig, si, sc);
1938 #endif
1940 #include "sigtramp.h"
1942 __gnat_sigtramp (sig, (void *)si, (void *)sc,
1943 (__sigtramphandler_t *)&__gnat_map_signal);
1945 #else
1946 __gnat_map_signal (sig, si, sc);
1947 #endif
1950 #if defined(__leon__) && defined(_WRS_KERNEL)
1951 /* For LEON VxWorks we need to install a trap handler for stack overflow */
1953 extern void excEnt (void);
1954 /* VxWorks exception handler entry */
1956 struct trap_entry {
1957 unsigned long inst_first;
1958 unsigned long inst_second;
1959 unsigned long inst_third;
1960 unsigned long inst_fourth;
1962 /* Four instructions representing entries in the trap table */
1964 struct trap_entry *trap_0_entry;
1965 /* We will set the location of the entry for software trap 0 in the trap
1966 table. */
1967 #endif
1969 void
1970 __gnat_install_handler (void)
1972 struct sigaction act;
1973 char *model ATTRIBUTE_UNUSED;
1975 /* Setup signal handler to map synchronous signals to appropriate
1976 exceptions. Make sure that the handler isn't interrupted by another
1977 signal that might cause a scheduling event! */
1979 act.sa_sigaction = __gnat_error_handler;
1980 act.sa_flags = SA_SIGINFO | SA_ONSTACK;
1981 sigemptyset (&act.sa_mask);
1983 /* For VxWorks, install all signal handlers, since pragma Interrupt_State
1984 applies to vectored hardware interrupts, not signals. */
1985 sigaction (SIGFPE, &act, NULL);
1986 sigaction (SIGILL, &act, NULL);
1987 sigaction (SIGSEGV, &act, NULL);
1988 sigaction (SIGBUS, &act, NULL);
1990 #if defined(__leon__) && defined(_WRS_KERNEL)
1991 /* Specific to the LEON VxWorks kernel run-time library */
1993 /* For stack checking the compiler triggers a software trap 0 (ta 0) in
1994 case of overflow (we use the stack limit mechanism). We need to install
1995 the trap handler here for this software trap (the OS does not handle
1996 it) as if it were a data_access_exception (trap 9). We do the same as
1997 if we put in the trap table a VXSPARC_BAD_TRAP(9). Software trap 0 is
1998 located at vector 0x80, and each entry takes 4 words. */
2000 trap_0_entry = (struct trap_entry *)(intVecBaseGet () + 0x80 * 4);
2002 /* mov 0x9, %l7 */
2004 trap_0_entry->inst_first = 0xae102000 + 9;
2006 /* sethi %hi(excEnt), %l6 */
2008 /* The 22 most significant bits of excEnt are obtained shifting 10 times
2009 to the right. */
2011 trap_0_entry->inst_second = 0x2d000000 + ((unsigned long)excEnt >> 10);
2013 /* jmp %l6+%lo(excEnt) */
2015 /* The 10 least significant bits of excEnt are obtained by masking */
2017 trap_0_entry->inst_third = 0x81c5a000 + ((unsigned long)excEnt & 0x3ff);
2019 /* rd %psr, %l0 */
2021 trap_0_entry->inst_fourth = 0xa1480000;
2022 #endif
2024 #if defined (__i386__) && !defined (VTHREADS)
2025 /* By experiment, found that sysModel () returns the following string
2026 prefix for vxsim when running on Linux and Windows. */
2027 model = sysModel ();
2028 if ((strncmp (model, "Linux", 5) == 0)
2029 || (strncmp (model, "Windows", 7) == 0))
2030 is_vxsim = 1;
2031 #endif
2033 __gnat_handler_installed = 1;
2036 #define HAVE_GNAT_INIT_FLOAT
2038 void
2039 __gnat_init_float (void)
2041 /* Disable overflow/underflow exceptions on the PPC processor, needed
2042 to get correct Ada semantics. Note that for AE653 vThreads, the HW
2043 overflow settings are an OS configuration issue. The instructions
2044 below have no effect. */
2045 #if defined (_ARCH_PPC) && !defined (_SOFT_FLOAT) && (!defined (VTHREADS) || defined (__VXWORKSMILS__))
2046 #if defined (__SPE__)
2048 /* For e500v2, do nothing and leave the responsibility to install the
2049 handler and enable the exceptions to the BSP. */
2051 #else
2052 asm ("mtfsb0 25");
2053 asm ("mtfsb0 26");
2054 #endif
2055 #endif
2057 #if defined (__i386__) && !defined (VTHREADS)
2058 /* This is used to properly initialize the FPU on an x86 for each
2059 process thread. */
2060 asm ("finit");
2061 #endif
2063 /* Similarly for SPARC64. Achieved by masking bits in the Trap Enable Mask
2064 field of the Floating-point Status Register (see the SPARC Architecture
2065 Manual Version 9, p 48). */
2066 #if defined (sparc64)
2068 #define FSR_TEM_NVM (1 << 27) /* Invalid operand */
2069 #define FSR_TEM_OFM (1 << 26) /* Overflow */
2070 #define FSR_TEM_UFM (1 << 25) /* Underflow */
2071 #define FSR_TEM_DZM (1 << 24) /* Division by Zero */
2072 #define FSR_TEM_NXM (1 << 23) /* Inexact result */
2074 unsigned int fsr;
2076 __asm__("st %%fsr, %0" : "=m" (fsr));
2077 fsr &= ~(FSR_TEM_OFM | FSR_TEM_UFM);
2078 __asm__("ld %0, %%fsr" : : "m" (fsr));
2080 #endif
2083 /* This subprogram is called by System.Task_Primitives.Operations.Enter_Task
2084 (if not null) when a new task is created. It is initialized by
2085 System.Stack_Checking.Operations.Initialize_Stack_Limit.
2086 The use of a hook avoids to drag stack checking subprograms if stack
2087 checking is not used. */
2088 void (*__gnat_set_stack_limit_hook)(void) = (void (*)(void))0;
2090 /******************/
2091 /* NetBSD Section */
2092 /******************/
2094 #elif defined(__NetBSD__)
2096 #include <signal.h>
2097 #include <unistd.h>
2099 static void
2100 __gnat_error_handler (int sig)
2102 struct Exception_Data *exception;
2103 const char *msg;
2105 switch(sig)
2107 case SIGFPE:
2108 exception = &constraint_error;
2109 msg = "SIGFPE";
2110 break;
2111 case SIGILL:
2112 exception = &constraint_error;
2113 msg = "SIGILL";
2114 break;
2115 case SIGSEGV:
2116 exception = &storage_error;
2117 msg = "stack overflow or erroneous memory access";
2118 break;
2119 case SIGBUS:
2120 exception = &constraint_error;
2121 msg = "SIGBUS";
2122 break;
2123 default:
2124 exception = &program_error;
2125 msg = "unhandled signal";
2128 Raise_From_Signal_Handler (exception, msg);
2131 void
2132 __gnat_install_handler(void)
2134 struct sigaction act;
2136 act.sa_handler = __gnat_error_handler;
2137 act.sa_flags = SA_NODEFER | SA_RESTART;
2138 sigemptyset (&act.sa_mask);
2140 /* Do not install handlers if interrupt state is "System". */
2141 if (__gnat_get_interrupt_state (SIGFPE) != 's')
2142 sigaction (SIGFPE, &act, NULL);
2143 if (__gnat_get_interrupt_state (SIGILL) != 's')
2144 sigaction (SIGILL, &act, NULL);
2145 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
2146 sigaction (SIGSEGV, &act, NULL);
2147 if (__gnat_get_interrupt_state (SIGBUS) != 's')
2148 sigaction (SIGBUS, &act, NULL);
2150 __gnat_handler_installed = 1;
2153 /*******************/
2154 /* OpenBSD Section */
2155 /*******************/
2157 #elif defined(__OpenBSD__)
2159 #include <signal.h>
2160 #include <unistd.h>
2162 static void
2163 __gnat_error_handler (int sig)
2165 struct Exception_Data *exception;
2166 const char *msg;
2168 switch(sig)
2170 case SIGFPE:
2171 exception = &constraint_error;
2172 msg = "SIGFPE";
2173 break;
2174 case SIGILL:
2175 exception = &constraint_error;
2176 msg = "SIGILL";
2177 break;
2178 case SIGSEGV:
2179 exception = &storage_error;
2180 msg = "stack overflow or erroneous memory access";
2181 break;
2182 case SIGBUS:
2183 exception = &constraint_error;
2184 msg = "SIGBUS";
2185 break;
2186 default:
2187 exception = &program_error;
2188 msg = "unhandled signal";
2191 Raise_From_Signal_Handler (exception, msg);
2194 void
2195 __gnat_install_handler(void)
2197 struct sigaction act;
2199 act.sa_handler = __gnat_error_handler;
2200 act.sa_flags = SA_NODEFER | SA_RESTART;
2201 sigemptyset (&act.sa_mask);
2203 /* Do not install handlers if interrupt state is "System" */
2204 if (__gnat_get_interrupt_state (SIGFPE) != 's')
2205 sigaction (SIGFPE, &act, NULL);
2206 if (__gnat_get_interrupt_state (SIGILL) != 's')
2207 sigaction (SIGILL, &act, NULL);
2208 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
2209 sigaction (SIGSEGV, &act, NULL);
2210 if (__gnat_get_interrupt_state (SIGBUS) != 's')
2211 sigaction (SIGBUS, &act, NULL);
2213 __gnat_handler_installed = 1;
2216 /******************/
2217 /* Darwin Section */
2218 /******************/
2220 #elif defined(__APPLE__)
2222 #include <signal.h>
2223 #include <stdlib.h>
2224 #include <sys/syscall.h>
2225 #include <sys/sysctl.h>
2227 /* This must be in keeping with System.OS_Interface.Alternate_Stack_Size. */
2228 char __gnat_alternate_stack[32 * 1024]; /* 1 * MINSIGSTKSZ */
2230 /* Defined in xnu unix_signal.c.
2231 Tell the kernel to re-use alt stack when delivering a signal. */
2232 #define UC_RESET_ALT_STACK 0x80000000
2234 #ifndef __arm__
2235 #include <mach/mach_vm.h>
2236 #include <mach/mach_init.h>
2237 #include <mach/vm_statistics.h>
2238 #endif
2240 /* Return true if ADDR is within a stack guard area. */
2241 static int
2242 __gnat_is_stack_guard (mach_vm_address_t addr)
2244 #ifndef __arm__
2245 kern_return_t kret;
2246 vm_region_submap_info_data_64_t info;
2247 mach_vm_address_t start;
2248 mach_vm_size_t size;
2249 natural_t depth;
2250 mach_msg_type_number_t count;
2252 count = VM_REGION_SUBMAP_INFO_COUNT_64;
2253 start = addr;
2254 size = -1;
2255 depth = 9999;
2256 kret = mach_vm_region_recurse (mach_task_self (), &start, &size, &depth,
2257 (vm_region_recurse_info_t) &info, &count);
2258 if (kret == KERN_SUCCESS
2259 && addr >= start && addr < (start + size)
2260 && info.protection == VM_PROT_NONE
2261 && info.user_tag == VM_MEMORY_STACK)
2262 return 1;
2263 return 0;
2264 #else
2265 /* Pagezero for arm. */
2266 return addr >= 4096;
2267 #endif
2270 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
2272 #if defined (__x86_64__)
2273 static int
2274 __darwin_major_version (void)
2276 static int cache = -1;
2277 if (cache < 0)
2279 int mib[2] = {CTL_KERN, KERN_OSRELEASE};
2280 size_t len;
2282 /* Find out how big the buffer needs to be (and set cache to 0
2283 on failure). */
2284 if (sysctl (mib, 2, NULL, &len, NULL, 0) == 0)
2286 char release[len];
2287 sysctl (mib, 2, release, &len, NULL, 0);
2288 /* Darwin releases are of the form L.M.N where L is the major
2289 version, so strtol will return L. */
2290 cache = (int) strtol (release, NULL, 10);
2292 else
2294 cache = 0;
2297 return cache;
2299 #endif
2301 void
2302 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED,
2303 void *ucontext ATTRIBUTE_UNUSED)
2305 #if defined (__x86_64__)
2306 if (__darwin_major_version () < 12)
2308 /* Work around radar #10302855, where the unwinders (libunwind or
2309 libgcc_s depending on the system revision) and the DWARF unwind
2310 data for sigtramp have different ideas about register numbering,
2311 causing rbx and rdx to be transposed. */
2312 ucontext_t *uc = (ucontext_t *)ucontext;
2313 unsigned long t = uc->uc_mcontext->__ss.__rbx;
2315 uc->uc_mcontext->__ss.__rbx = uc->uc_mcontext->__ss.__rdx;
2316 uc->uc_mcontext->__ss.__rdx = t;
2318 #endif
2321 static void
2322 __gnat_error_handler (int sig, siginfo_t *si, void *ucontext)
2324 struct Exception_Data *exception;
2325 const char *msg;
2327 __gnat_adjust_context_for_raise (sig, ucontext);
2329 switch (sig)
2331 case SIGSEGV:
2332 case SIGBUS:
2333 if (__gnat_is_stack_guard ((unsigned long)si->si_addr))
2335 exception = &storage_error;
2336 msg = "stack overflow";
2338 else
2340 exception = &constraint_error;
2341 msg = "erroneous memory access";
2343 /* Reset the use of alt stack, so that the alt stack will be used
2344 for the next signal delivery.
2345 The stack can't be used in case of stack checking. */
2346 syscall (SYS_sigreturn, NULL, UC_RESET_ALT_STACK);
2347 break;
2349 case SIGFPE:
2350 exception = &constraint_error;
2351 msg = "SIGFPE";
2352 break;
2354 default:
2355 exception = &program_error;
2356 msg = "unhandled signal";
2359 Raise_From_Signal_Handler (exception, msg);
2362 void
2363 __gnat_install_handler (void)
2365 struct sigaction act;
2367 /* Set up signal handler to map synchronous signals to appropriate
2368 exceptions. Make sure that the handler isn't interrupted by another
2369 signal that might cause a scheduling event! Also setup an alternate
2370 stack region for the handler execution so that stack overflows can be
2371 handled properly, avoiding a SEGV generation from stack usage by the
2372 handler itself (and it is required by Darwin). */
2374 stack_t stack;
2375 stack.ss_sp = __gnat_alternate_stack;
2376 stack.ss_size = sizeof (__gnat_alternate_stack);
2377 stack.ss_flags = 0;
2378 sigaltstack (&stack, NULL);
2380 act.sa_flags = SA_NODEFER | SA_RESTART | SA_SIGINFO;
2381 act.sa_sigaction = __gnat_error_handler;
2382 sigemptyset (&act.sa_mask);
2384 /* Do not install handlers if interrupt state is "System". */
2385 if (__gnat_get_interrupt_state (SIGABRT) != 's')
2386 sigaction (SIGABRT, &act, NULL);
2387 if (__gnat_get_interrupt_state (SIGFPE) != 's')
2388 sigaction (SIGFPE, &act, NULL);
2389 if (__gnat_get_interrupt_state (SIGILL) != 's')
2390 sigaction (SIGILL, &act, NULL);
2392 act.sa_flags |= SA_ONSTACK;
2393 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
2394 sigaction (SIGSEGV, &act, NULL);
2395 if (__gnat_get_interrupt_state (SIGBUS) != 's')
2396 sigaction (SIGBUS, &act, NULL);
2398 __gnat_handler_installed = 1;
2401 #elif defined(__ANDROID__)
2403 /*******************/
2404 /* Android Section */
2405 /*******************/
2407 #include <signal.h>
2408 #include "sigtramp.h"
2410 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
2412 void
2413 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED, void *ucontext)
2415 mcontext_t *mcontext = &((ucontext_t *) ucontext)->uc_mcontext;
2417 /* ARM Bump has to be an even number because of odd/even architecture. */
2418 ((mcontext_t *) mcontext)->arm_pc += 2;
2421 static void
2422 __gnat_map_signal (int sig,
2423 siginfo_t *si ATTRIBUTE_UNUSED,
2424 void *ucontext ATTRIBUTE_UNUSED)
2426 struct Exception_Data *exception;
2427 const char *msg;
2429 switch (sig)
2431 case SIGSEGV:
2432 exception = &storage_error;
2433 msg = "stack overflow or erroneous memory access";
2434 break;
2436 case SIGBUS:
2437 exception = &constraint_error;
2438 msg = "SIGBUS";
2439 break;
2441 case SIGFPE:
2442 exception = &constraint_error;
2443 msg = "SIGFPE";
2444 break;
2446 default:
2447 exception = &program_error;
2448 msg = "unhandled signal";
2451 Raise_From_Signal_Handler (exception, msg);
2454 static void
2455 __gnat_error_handler (int sig,
2456 siginfo_t *si ATTRIBUTE_UNUSED,
2457 void *ucontext ATTRIBUTE_UNUSED)
2459 __gnat_adjust_context_for_raise (sig, ucontext);
2461 __gnat_sigtramp (sig, (void *) si, (void *) ucontext,
2462 (__sigtramphandler_t *)&__gnat_map_signal);
2465 /* This must be in keeping with System.OS_Interface.Alternate_Stack_Size. */
2466 char __gnat_alternate_stack[16 * 1024];
2468 void
2469 __gnat_install_handler (void)
2471 struct sigaction act;
2473 /* Set up signal handler to map synchronous signals to appropriate
2474 exceptions. Make sure that the handler isn't interrupted by another
2475 signal that might cause a scheduling event! Also setup an alternate
2476 stack region for the handler execution so that stack overflows can be
2477 handled properly, avoiding a SEGV generation from stack usage by the
2478 handler itself. */
2480 stack_t stack;
2481 stack.ss_sp = __gnat_alternate_stack;
2482 stack.ss_size = sizeof (__gnat_alternate_stack);
2483 stack.ss_flags = 0;
2484 sigaltstack (&stack, NULL);
2486 act.sa_sigaction = __gnat_error_handler;
2487 act.sa_flags = SA_NODEFER | SA_RESTART | SA_SIGINFO;
2488 sigemptyset (&act.sa_mask);
2490 sigaction (SIGABRT, &act, NULL);
2491 sigaction (SIGFPE, &act, NULL);
2492 sigaction (SIGILL, &act, NULL);
2493 sigaction (SIGBUS, &act, NULL);
2494 act.sa_flags |= SA_ONSTACK;
2495 sigaction (SIGSEGV, &act, NULL);
2497 __gnat_handler_installed = 1;
2500 #else
2502 /* For all other versions of GNAT, the handler does nothing. */
2504 /*******************/
2505 /* Default Section */
2506 /*******************/
2508 void
2509 __gnat_install_handler (void)
2511 __gnat_handler_installed = 1;
2514 #endif
2516 /*********************/
2517 /* __gnat_init_float */
2518 /*********************/
2520 /* This routine is called as each process thread is created, for possible
2521 initialization of the FP processor. This version is used under INTERIX
2522 and WIN32. */
2524 #if defined (_WIN32) || defined (__INTERIX) \
2525 || defined (__Lynx__) || defined(__NetBSD__) || defined(__FreeBSD__) \
2526 || defined (__OpenBSD__) || defined (__DragonFly__)
2528 #define HAVE_GNAT_INIT_FLOAT
2530 void
2531 __gnat_init_float (void)
2533 #if defined (__i386__) || defined (__x86_64__)
2535 /* This is used to properly initialize the FPU on an x86 for each
2536 process thread. */
2538 asm ("finit");
2540 #endif /* Defined __i386__ */
2542 #endif
2544 #ifndef HAVE_GNAT_INIT_FLOAT
2546 /* All targets without a specific __gnat_init_float will use an empty one. */
2547 void
2548 __gnat_init_float (void)
2551 #endif
2553 /***********************************/
2554 /* __gnat_adjust_context_for_raise */
2555 /***********************************/
2557 #ifndef HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
2559 /* All targets without a specific version will use an empty one. */
2561 /* Given UCONTEXT a pointer to a context structure received by a signal
2562 handler for SIGNO, perform the necessary adjustments to let the handler
2563 raise an exception. Calls to this routine are not conditioned by the
2564 propagation scheme in use. */
2566 void
2567 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED,
2568 void *ucontext ATTRIBUTE_UNUSED)
2570 /* We used to compensate here for the raised from call vs raised from signal
2571 exception discrepancy with the GCC ZCX scheme, but this now can be dealt
2572 with generically in the unwinder (see GCC PR other/26208). This however
2573 requires the use of the _Unwind_GetIPInfo routine in raise-gcc.c, which
2574 is predicated on the definition of HAVE_GETIPINFO at compile time. Only
2575 the VMS ports still do the compensation described in the few lines below.
2577 *** Call vs signal exception discrepancy with GCC ZCX scheme ***
2579 The GCC unwinder expects to be dealing with call return addresses, since
2580 this is the "nominal" case of what we retrieve while unwinding a regular
2581 call chain.
2583 To evaluate if a handler applies at some point identified by a return
2584 address, the propagation engine needs to determine what region the
2585 corresponding call instruction pertains to. Because the return address
2586 may not be attached to the same region as the call, the unwinder always
2587 subtracts "some" amount from a return address to search the region
2588 tables, amount chosen to ensure that the resulting address is inside the
2589 call instruction.
2591 When we raise an exception from a signal handler, e.g. to transform a
2592 SIGSEGV into Storage_Error, things need to appear as if the signal
2593 handler had been "called" by the instruction which triggered the signal,
2594 so that exception handlers that apply there are considered. What the
2595 unwinder will retrieve as the return address from the signal handler is
2596 what it will find as the faulting instruction address in the signal
2597 context pushed by the kernel. Leaving this address untouched looses, if
2598 the triggering instruction happens to be the very first of a region, as
2599 the later adjustments performed by the unwinder would yield an address
2600 outside that region. We need to compensate for the unwinder adjustments
2601 at some point, and this is what this routine is expected to do.
2603 signo is passed because on some targets for some signals the PC in
2604 context points to the instruction after the faulting one, in which case
2605 the unwinder adjustment is still desired. */
2608 #endif
2610 #ifdef __cplusplus
2612 #endif