2015-01-06 Ed Schonberg <schonberg@adacore.com>
[official-gcc.git] / gcc / ada / init.c
blob30f2da4462b95fa050397573bfe4f255c725eb2e
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * I N I T *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2014, 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 2sinit.ads, 2sinit.adb,
42 s-init-ae653-cert.adb and s-init-xi-sparc.adb. All these files implement
43 the required functionality 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 (__SVR4) && !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 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__)
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 ()
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 */
1699 /*******************/
1701 #elif defined(__vxworks)
1703 #include <signal.h>
1704 #include <taskLib.h>
1706 #ifndef __RTP__
1707 #include <intLib.h>
1708 #include <iv.h>
1709 #endif
1711 #if defined (ARMEL) && (_WRS_VXWORKS_MAJOR == 6)
1712 #include <vmLib.h>
1713 #endif
1715 #ifdef VTHREADS
1716 #include "private/vThreadsP.h"
1717 #endif
1719 #ifndef __RTP__
1721 /* Directly vectored Interrupt routines are not supported when using RTPs. */
1723 extern int __gnat_inum_to_ivec (int);
1725 /* This is needed by the GNAT run time to handle Vxworks interrupts. */
1727 __gnat_inum_to_ivec (int num)
1729 return (int) INUM_TO_IVEC (num);
1731 #endif
1733 #if !defined(__alpha_vxworks) && ((_WRS_VXWORKS_MAJOR != 6) && (_WRS_VXWORKS_MAJOR != 7)) && !defined(__RTP__)
1735 /* getpid is used by s-parint.adb, but is not defined by VxWorks, except
1736 on Alpha VxWorks and VxWorks 6.x (including RTPs). */
1738 extern long getpid (void);
1740 long
1741 getpid (void)
1743 return taskIdSelf ();
1745 #endif
1747 /* VxWorks 653 vThreads expects the field excCnt to be zeroed when a signal is.
1748 handled. The VxWorks version of longjmp does this; GCC's builtin_longjmp
1749 doesn't. */
1750 void
1751 __gnat_clear_exception_count (void)
1753 #ifdef VTHREADS
1754 WIND_TCB *currentTask = (WIND_TCB *) taskIdSelf();
1756 currentTask->vThreads.excCnt = 0;
1757 #endif
1760 /* Handle different SIGnal to exception mappings in different VxWorks
1761 versions. */
1762 static void
1763 __gnat_map_signal (int sig, siginfo_t *si ATTRIBUTE_UNUSED,
1764 void *sc ATTRIBUTE_UNUSED)
1766 struct Exception_Data *exception;
1767 const char *msg;
1769 switch (sig)
1771 case SIGFPE:
1772 exception = &constraint_error;
1773 msg = "SIGFPE";
1774 break;
1775 #ifdef VTHREADS
1776 #ifdef __VXWORKSMILS__
1777 case SIGILL:
1778 exception = &storage_error;
1779 msg = "SIGILL: possible stack overflow";
1780 break;
1781 case SIGSEGV:
1782 exception = &storage_error;
1783 msg = "SIGSEGV";
1784 break;
1785 case SIGBUS:
1786 exception = &program_error;
1787 msg = "SIGBUS";
1788 break;
1789 #else
1790 case SIGILL:
1791 exception = &constraint_error;
1792 msg = "Floating point exception or SIGILL";
1793 break;
1794 case SIGSEGV:
1795 exception = &storage_error;
1796 msg = "SIGSEGV";
1797 break;
1798 case SIGBUS:
1799 exception = &storage_error;
1800 msg = "SIGBUS: possible stack overflow";
1801 break;
1802 #endif
1803 #elif (_WRS_VXWORKS_MAJOR >= 6)
1804 case SIGILL:
1805 exception = &constraint_error;
1806 msg = "SIGILL";
1807 break;
1808 #ifdef __RTP__
1809 /* In RTP mode a SIGSEGV is most likely due to a stack overflow,
1810 since stack checking uses the probing mechanism. */
1811 case SIGSEGV:
1812 exception = &storage_error;
1813 msg = "SIGSEGV: possible stack overflow";
1814 break;
1815 case SIGBUS:
1816 exception = &program_error;
1817 msg = "SIGBUS";
1818 break;
1819 #else
1820 /* VxWorks 6 kernel mode with probing. SIGBUS for guard page hit */
1821 case SIGSEGV:
1822 exception = &storage_error;
1823 msg = "SIGSEGV";
1824 break;
1825 case SIGBUS:
1826 exception = &storage_error;
1827 msg = "SIGBUS: possible stack overflow";
1828 break;
1829 #endif
1830 #else
1831 /* VxWorks 5: a SIGILL is most likely due to a stack overflow,
1832 since stack checking uses the stack limit mechanism. */
1833 case SIGILL:
1834 exception = &storage_error;
1835 msg = "SIGILL: possible stack overflow";
1836 break;
1837 case SIGSEGV:
1838 exception = &storage_error;
1839 msg = "SIGSEGV";
1840 break;
1841 case SIGBUS:
1842 exception = &program_error;
1843 msg = "SIGBUS";
1844 break;
1845 #endif
1846 default:
1847 exception = &program_error;
1848 msg = "unhandled signal";
1851 /* On ARM VxWorks 6.x, the guard page is left un-armed by the kernel
1852 after being violated, so subsequent violations aren't detected.
1853 so we retrieve the address of the guard page from the TCB and compare it
1854 with the page that is violated (pREG 12 in the context) and re-arm that
1855 page if there's a match. Additionally we're are assured this is a
1856 genuine stack overflow condition and and set the message and exception
1857 to that effect. */
1858 #if defined (ARMEL) && (_WRS_VXWORKS_MAJOR == 6)
1860 /* We re-arm the guard page by marking it invalid */
1862 #define PAGE_SIZE 4096
1863 #define REG_IP 12
1865 if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL)
1867 TASK_ID tid = taskIdSelf ();
1868 WIND_TCB *pTcb = taskTcb (tid);
1869 unsigned long violated_page
1870 = ((struct sigcontext *) sc)->sc_pregs->r[REG_IP] & ~(PAGE_SIZE - 1);
1872 if ((unsigned long) (pTcb->pStackEnd - PAGE_SIZE) == violated_page)
1874 vmStateSet (NULL, violated_page,
1875 PAGE_SIZE, VM_STATE_MASK_VALID, VM_STATE_VALID_NOT);
1876 exception = &storage_error;
1878 switch (sig)
1880 case SIGSEGV:
1881 msg = "SIGSEGV: stack overflow";
1882 break;
1883 case SIGBUS:
1884 msg = "SIGBUS: stack overflow";
1885 break;
1886 case SIGILL:
1887 msg = "SIGILL: stack overflow";
1888 break;
1892 #endif /* defined (ARMEL) && (_WRS_VXWORKS_MAJOR == 6) */
1894 __gnat_clear_exception_count ();
1895 Raise_From_Signal_Handler (exception, msg);
1898 /* Tasking and Non-tasking signal handler. Map SIGnal to Ada exception
1899 propagation after the required low level adjustments. */
1901 static void
1902 __gnat_error_handler (int sig, siginfo_t *si, void *sc)
1904 sigset_t mask;
1906 /* VxWorks will always mask out the signal during the signal handler and
1907 will reenable it on a longjmp. GNAT does not generate a longjmp to
1908 return from a signal handler so the signal will still be masked unless
1909 we unmask it. */
1910 sigprocmask (SIG_SETMASK, NULL, &mask);
1911 sigdelset (&mask, sig);
1912 sigprocmask (SIG_SETMASK, &mask, NULL);
1914 #if defined (__ARMEL__) || defined (__PPC__)
1915 /* On PowerPC, kernel mode, we process signals through a Call Frame Info
1916 trampoline, voiding the need for myriads of fallback_frame_state
1917 variants in the ZCX runtime. We have no simple way to distinguish ZCX
1918 from SJLJ here, so we do this for SJLJ as well even though this is not
1919 necessary. This only incurs a few extra instructions and a tiny
1920 amount of extra stack usage. */
1922 #include "sigtramp.h"
1924 __gnat_sigtramp (sig, (void *)si, (void *)sc,
1925 (__sigtramphandler_t *)&__gnat_map_signal);
1927 #else
1928 __gnat_map_signal (sig, si, sc);
1929 #endif
1932 #if defined(__leon__) && defined(_WRS_KERNEL)
1933 /* For LEON VxWorks we need to install a trap handler for stack overflow */
1935 extern void excEnt (void);
1936 /* VxWorks exception handler entry */
1938 struct trap_entry {
1939 unsigned long inst_first;
1940 unsigned long inst_second;
1941 unsigned long inst_third;
1942 unsigned long inst_fourth;
1944 /* Four instructions representing entries in the trap table */
1946 struct trap_entry *trap_0_entry;
1947 /* We will set the location of the entry for software trap 0 in the trap
1948 table. */
1949 #endif
1951 void
1952 __gnat_install_handler (void)
1954 struct sigaction act;
1956 /* Setup signal handler to map synchronous signals to appropriate
1957 exceptions. Make sure that the handler isn't interrupted by another
1958 signal that might cause a scheduling event! */
1960 act.sa_sigaction = __gnat_error_handler;
1961 act.sa_flags = SA_SIGINFO | SA_ONSTACK;
1962 sigemptyset (&act.sa_mask);
1964 /* For VxWorks, install all signal handlers, since pragma Interrupt_State
1965 applies to vectored hardware interrupts, not signals. */
1966 sigaction (SIGFPE, &act, NULL);
1967 sigaction (SIGILL, &act, NULL);
1968 sigaction (SIGSEGV, &act, NULL);
1969 sigaction (SIGBUS, &act, NULL);
1971 #if defined(__leon__) && defined(_WRS_KERNEL)
1972 /* Specific to the LEON VxWorks kernel run-time library */
1974 /* For stack checking the compiler triggers a software trap 0 (ta 0) in
1975 case of overflow (we use the stack limit mechanism). We need to install
1976 the trap handler here for this software trap (the OS does not handle
1977 it) as if it were a data_access_exception (trap 9). We do the same as
1978 if we put in the trap table a VXSPARC_BAD_TRAP(9). Software trap 0 is
1979 located at vector 0x80, and each entry takes 4 words. */
1981 trap_0_entry = (struct trap_entry *)(intVecBaseGet () + 0x80 * 4);
1983 /* mov 0x9, %l7 */
1985 trap_0_entry->inst_first = 0xae102000 + 9;
1987 /* sethi %hi(excEnt), %l6 */
1989 /* The 22 most significant bits of excEnt are obtained shifting 10 times
1990 to the right. */
1992 trap_0_entry->inst_second = 0x2d000000 + ((unsigned long)excEnt >> 10);
1994 /* jmp %l6+%lo(excEnt) */
1996 /* The 10 least significant bits of excEnt are obtained by masking */
1998 trap_0_entry->inst_third = 0x81c5a000 + ((unsigned long)excEnt & 0x3ff);
2000 /* rd %psr, %l0 */
2002 trap_0_entry->inst_fourth = 0xa1480000;
2003 #endif
2005 __gnat_handler_installed = 1;
2008 #define HAVE_GNAT_INIT_FLOAT
2010 void
2011 __gnat_init_float (void)
2013 /* Disable overflow/underflow exceptions on the PPC processor, needed
2014 to get correct Ada semantics. Note that for AE653 vThreads, the HW
2015 overflow settings are an OS configuration issue. The instructions
2016 below have no effect. */
2017 #if defined (_ARCH_PPC) && !defined (_SOFT_FLOAT) && (!defined (VTHREADS) || defined (__VXWORKSMILS__))
2018 #if defined (__SPE__)
2020 const unsigned long spefscr_mask = 0xfffffff3;
2021 unsigned long spefscr;
2022 asm ("mfspr %0, 512" : "=r" (spefscr));
2023 spefscr = spefscr & spefscr_mask;
2024 asm ("mtspr 512, %0\n\tisync" : : "r" (spefscr));
2026 #else
2027 asm ("mtfsb0 25");
2028 asm ("mtfsb0 26");
2029 #endif
2030 #endif
2032 #if (defined (__i386__) || defined (i386)) && !defined (VTHREADS)
2033 /* This is used to properly initialize the FPU on an x86 for each
2034 process thread. */
2035 asm ("finit");
2036 #endif
2038 /* Similarly for SPARC64. Achieved by masking bits in the Trap Enable Mask
2039 field of the Floating-point Status Register (see the SPARC Architecture
2040 Manual Version 9, p 48). */
2041 #if defined (sparc64)
2043 #define FSR_TEM_NVM (1 << 27) /* Invalid operand */
2044 #define FSR_TEM_OFM (1 << 26) /* Overflow */
2045 #define FSR_TEM_UFM (1 << 25) /* Underflow */
2046 #define FSR_TEM_DZM (1 << 24) /* Division by Zero */
2047 #define FSR_TEM_NXM (1 << 23) /* Inexact result */
2049 unsigned int fsr;
2051 __asm__("st %%fsr, %0" : "=m" (fsr));
2052 fsr &= ~(FSR_TEM_OFM | FSR_TEM_UFM);
2053 __asm__("ld %0, %%fsr" : : "m" (fsr));
2055 #endif
2058 /* This subprogram is called by System.Task_Primitives.Operations.Enter_Task
2059 (if not null) when a new task is created. It is initialized by
2060 System.Stack_Checking.Operations.Initialize_Stack_Limit.
2061 The use of a hook avoids to drag stack checking subprograms if stack
2062 checking is not used. */
2063 void (*__gnat_set_stack_limit_hook)(void) = (void (*)(void))0;
2065 /******************/
2066 /* NetBSD Section */
2067 /******************/
2069 #elif defined(__NetBSD__)
2071 #include <signal.h>
2072 #include <unistd.h>
2074 static void
2075 __gnat_error_handler (int sig)
2077 struct Exception_Data *exception;
2078 const char *msg;
2080 switch(sig)
2082 case SIGFPE:
2083 exception = &constraint_error;
2084 msg = "SIGFPE";
2085 break;
2086 case SIGILL:
2087 exception = &constraint_error;
2088 msg = "SIGILL";
2089 break;
2090 case SIGSEGV:
2091 exception = &storage_error;
2092 msg = "stack overflow or erroneous memory access";
2093 break;
2094 case SIGBUS:
2095 exception = &constraint_error;
2096 msg = "SIGBUS";
2097 break;
2098 default:
2099 exception = &program_error;
2100 msg = "unhandled signal";
2103 Raise_From_Signal_Handler(exception, msg);
2106 void
2107 __gnat_install_handler(void)
2109 struct sigaction act;
2111 act.sa_handler = __gnat_error_handler;
2112 act.sa_flags = SA_NODEFER | SA_RESTART;
2113 sigemptyset (&act.sa_mask);
2115 /* Do not install handlers if interrupt state is "System". */
2116 if (__gnat_get_interrupt_state (SIGFPE) != 's')
2117 sigaction (SIGFPE, &act, NULL);
2118 if (__gnat_get_interrupt_state (SIGILL) != 's')
2119 sigaction (SIGILL, &act, NULL);
2120 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
2121 sigaction (SIGSEGV, &act, NULL);
2122 if (__gnat_get_interrupt_state (SIGBUS) != 's')
2123 sigaction (SIGBUS, &act, NULL);
2125 __gnat_handler_installed = 1;
2128 /*******************/
2129 /* OpenBSD Section */
2130 /*******************/
2132 #elif defined(__OpenBSD__)
2134 #include <signal.h>
2135 #include <unistd.h>
2137 static void
2138 __gnat_error_handler (int sig)
2140 struct Exception_Data *exception;
2141 const char *msg;
2143 switch(sig)
2145 case SIGFPE:
2146 exception = &constraint_error;
2147 msg = "SIGFPE";
2148 break;
2149 case SIGILL:
2150 exception = &constraint_error;
2151 msg = "SIGILL";
2152 break;
2153 case SIGSEGV:
2154 exception = &storage_error;
2155 msg = "stack overflow or erroneous memory access";
2156 break;
2157 case SIGBUS:
2158 exception = &constraint_error;
2159 msg = "SIGBUS";
2160 break;
2161 default:
2162 exception = &program_error;
2163 msg = "unhandled signal";
2166 Raise_From_Signal_Handler(exception, msg);
2169 void
2170 __gnat_install_handler(void)
2172 struct sigaction act;
2174 act.sa_handler = __gnat_error_handler;
2175 act.sa_flags = SA_NODEFER | SA_RESTART;
2176 sigemptyset (&act.sa_mask);
2178 /* Do not install handlers if interrupt state is "System" */
2179 if (__gnat_get_interrupt_state (SIGFPE) != 's')
2180 sigaction (SIGFPE, &act, NULL);
2181 if (__gnat_get_interrupt_state (SIGILL) != 's')
2182 sigaction (SIGILL, &act, NULL);
2183 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
2184 sigaction (SIGSEGV, &act, NULL);
2185 if (__gnat_get_interrupt_state (SIGBUS) != 's')
2186 sigaction (SIGBUS, &act, NULL);
2188 __gnat_handler_installed = 1;
2191 /******************/
2192 /* Darwin Section */
2193 /******************/
2195 #elif defined(__APPLE__)
2197 #include <signal.h>
2198 #include <stdlib.h>
2199 #include <sys/syscall.h>
2200 #include <sys/sysctl.h>
2202 /* This must be in keeping with System.OS_Interface.Alternate_Stack_Size. */
2203 char __gnat_alternate_stack[32 * 1024]; /* 1 * MINSIGSTKSZ */
2205 /* Defined in xnu unix_signal.c.
2206 Tell the kernel to re-use alt stack when delivering a signal. */
2207 #define UC_RESET_ALT_STACK 0x80000000
2209 #ifndef __arm__
2210 #include <mach/mach_vm.h>
2211 #include <mach/mach_init.h>
2212 #include <mach/vm_statistics.h>
2213 #endif
2215 /* Return true if ADDR is within a stack guard area. */
2216 static int
2217 __gnat_is_stack_guard (mach_vm_address_t addr)
2219 #ifndef __arm__
2220 kern_return_t kret;
2221 vm_region_submap_info_data_64_t info;
2222 mach_vm_address_t start;
2223 mach_vm_size_t size;
2224 natural_t depth;
2225 mach_msg_type_number_t count;
2227 count = VM_REGION_SUBMAP_INFO_COUNT_64;
2228 start = addr;
2229 size = -1;
2230 depth = 9999;
2231 kret = mach_vm_region_recurse (mach_task_self (), &start, &size, &depth,
2232 (vm_region_recurse_info_t) &info, &count);
2233 if (kret == KERN_SUCCESS
2234 && addr >= start && addr < (start + size)
2235 && info.protection == VM_PROT_NONE
2236 && info.user_tag == VM_MEMORY_STACK)
2237 return 1;
2238 return 0;
2239 #else
2240 /* Pagezero for arm. */
2241 return addr >= 4096;
2242 #endif
2245 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
2247 #if defined (__x86_64__)
2248 static int
2249 __darwin_major_version (void)
2251 static int cache = -1;
2252 if (cache < 0)
2254 int mib[2] = {CTL_KERN, KERN_OSRELEASE};
2255 size_t len;
2257 /* Find out how big the buffer needs to be (and set cache to 0
2258 on failure). */
2259 if (sysctl (mib, 2, NULL, &len, NULL, 0) == 0)
2261 char release[len];
2262 sysctl (mib, 2, release, &len, NULL, 0);
2263 /* Darwin releases are of the form L.M.N where L is the major
2264 version, so strtol will return L. */
2265 cache = (int) strtol (release, NULL, 10);
2267 else
2269 cache = 0;
2272 return cache;
2274 #endif
2276 void
2277 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED,
2278 void *ucontext ATTRIBUTE_UNUSED)
2280 #if defined (__x86_64__)
2281 if (__darwin_major_version () < 12)
2283 /* Work around radar #10302855, where the unwinders (libunwind or
2284 libgcc_s depending on the system revision) and the DWARF unwind
2285 data for sigtramp have different ideas about register numbering,
2286 causing rbx and rdx to be transposed. */
2287 ucontext_t *uc = (ucontext_t *)ucontext;
2288 unsigned long t = uc->uc_mcontext->__ss.__rbx;
2290 uc->uc_mcontext->__ss.__rbx = uc->uc_mcontext->__ss.__rdx;
2291 uc->uc_mcontext->__ss.__rdx = t;
2293 #endif
2296 static void
2297 __gnat_error_handler (int sig, siginfo_t *si, void *ucontext)
2299 struct Exception_Data *exception;
2300 const char *msg;
2302 __gnat_adjust_context_for_raise (sig, ucontext);
2304 switch (sig)
2306 case SIGSEGV:
2307 case SIGBUS:
2308 if (__gnat_is_stack_guard ((unsigned long)si->si_addr))
2310 exception = &storage_error;
2311 msg = "stack overflow";
2313 else
2315 exception = &constraint_error;
2316 msg = "erroneous memory access";
2318 /* Reset the use of alt stack, so that the alt stack will be used
2319 for the next signal delivery.
2320 The stack can't be used in case of stack checking. */
2321 syscall (SYS_sigreturn, NULL, UC_RESET_ALT_STACK);
2322 break;
2324 case SIGFPE:
2325 exception = &constraint_error;
2326 msg = "SIGFPE";
2327 break;
2329 default:
2330 exception = &program_error;
2331 msg = "unhandled signal";
2334 Raise_From_Signal_Handler (exception, msg);
2337 void
2338 __gnat_install_handler (void)
2340 struct sigaction act;
2342 /* Set up signal handler to map synchronous signals to appropriate
2343 exceptions. Make sure that the handler isn't interrupted by another
2344 signal that might cause a scheduling event! Also setup an alternate
2345 stack region for the handler execution so that stack overflows can be
2346 handled properly, avoiding a SEGV generation from stack usage by the
2347 handler itself (and it is required by Darwin). */
2349 stack_t stack;
2350 stack.ss_sp = __gnat_alternate_stack;
2351 stack.ss_size = sizeof (__gnat_alternate_stack);
2352 stack.ss_flags = 0;
2353 sigaltstack (&stack, NULL);
2355 act.sa_flags = SA_NODEFER | SA_RESTART | SA_SIGINFO;
2356 act.sa_sigaction = __gnat_error_handler;
2357 sigemptyset (&act.sa_mask);
2359 /* Do not install handlers if interrupt state is "System". */
2360 if (__gnat_get_interrupt_state (SIGABRT) != 's')
2361 sigaction (SIGABRT, &act, NULL);
2362 if (__gnat_get_interrupt_state (SIGFPE) != 's')
2363 sigaction (SIGFPE, &act, NULL);
2364 if (__gnat_get_interrupt_state (SIGILL) != 's')
2365 sigaction (SIGILL, &act, NULL);
2367 act.sa_flags |= SA_ONSTACK;
2368 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
2369 sigaction (SIGSEGV, &act, NULL);
2370 if (__gnat_get_interrupt_state (SIGBUS) != 's')
2371 sigaction (SIGBUS, &act, NULL);
2373 __gnat_handler_installed = 1;
2376 #elif defined(__ANDROID__)
2378 /*******************/
2379 /* Android Section */
2380 /*******************/
2382 #include <signal.h>
2383 #include "sigtramp.h"
2385 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
2387 void
2388 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED, void *ucontext)
2390 mcontext_t *mcontext = &((ucontext_t *) ucontext)->uc_mcontext;
2392 /* ARM Bump has to be an even number because of odd/even architecture. */
2393 ((mcontext_t *) mcontext)->arm_pc += 2;
2396 static void
2397 __gnat_map_signal (int sig,
2398 siginfo_t *si ATTRIBUTE_UNUSED,
2399 void *ucontext ATTRIBUTE_UNUSED)
2401 struct Exception_Data *exception;
2402 const char *msg;
2404 switch (sig)
2406 case SIGSEGV:
2407 exception = &storage_error;
2408 msg = "stack overflow or erroneous memory access";
2409 break;
2411 case SIGBUS:
2412 exception = &constraint_error;
2413 msg = "SIGBUS";
2414 break;
2416 case SIGFPE:
2417 exception = &constraint_error;
2418 msg = "SIGFPE";
2419 break;
2421 default:
2422 exception = &program_error;
2423 msg = "unhandled signal";
2426 Raise_From_Signal_Handler (exception, msg);
2429 static void
2430 __gnat_error_handler (int sig,
2431 siginfo_t *si ATTRIBUTE_UNUSED,
2432 void *ucontext ATTRIBUTE_UNUSED)
2434 __gnat_adjust_context_for_raise (sig, ucontext);
2436 __gnat_sigtramp (sig, (void *) si, (void *) ucontext,
2437 (__sigtramphandler_t *)&__gnat_map_signal);
2440 /* This must be in keeping with System.OS_Interface.Alternate_Stack_Size. */
2441 char __gnat_alternate_stack[16 * 1024];
2443 void
2444 __gnat_install_handler (void)
2446 struct sigaction act;
2448 /* Set up signal handler to map synchronous signals to appropriate
2449 exceptions. Make sure that the handler isn't interrupted by another
2450 signal that might cause a scheduling event! Also setup an alternate
2451 stack region for the handler execution so that stack overflows can be
2452 handled properly, avoiding a SEGV generation from stack usage by the
2453 handler itself. */
2455 stack_t stack;
2456 stack.ss_sp = __gnat_alternate_stack;
2457 stack.ss_size = sizeof (__gnat_alternate_stack);
2458 stack.ss_flags = 0;
2459 sigaltstack (&stack, NULL);
2461 act.sa_sigaction = __gnat_error_handler;
2462 act.sa_flags = SA_NODEFER | SA_RESTART | SA_SIGINFO;
2463 sigemptyset (&act.sa_mask);
2465 sigaction (SIGABRT, &act, NULL);
2466 sigaction (SIGFPE, &act, NULL);
2467 sigaction (SIGILL, &act, NULL);
2468 sigaction (SIGBUS, &act, NULL);
2469 act.sa_flags |= SA_ONSTACK;
2470 sigaction (SIGSEGV, &act, NULL);
2472 __gnat_handler_installed = 1;
2475 #else
2477 /* For all other versions of GNAT, the handler does nothing. */
2479 /*******************/
2480 /* Default Section */
2481 /*******************/
2483 void
2484 __gnat_install_handler (void)
2486 __gnat_handler_installed = 1;
2489 #endif
2491 /*********************/
2492 /* __gnat_init_float */
2493 /*********************/
2495 /* This routine is called as each process thread is created, for possible
2496 initialization of the FP processor. This version is used under INTERIX
2497 and WIN32. */
2499 #if defined (_WIN32) || defined (__INTERIX) \
2500 || defined (__Lynx__) || defined(__NetBSD__) || defined(__FreeBSD__) \
2501 || defined (__OpenBSD__)
2503 #define HAVE_GNAT_INIT_FLOAT
2505 void
2506 __gnat_init_float (void)
2508 #if defined (__i386__) || defined (i386) || defined (__x86_64)
2510 /* This is used to properly initialize the FPU on an x86 for each
2511 process thread. */
2513 asm ("finit");
2515 #endif /* Defined __i386__ */
2517 #endif
2519 #ifndef HAVE_GNAT_INIT_FLOAT
2521 /* All targets without a specific __gnat_init_float will use an empty one. */
2522 void
2523 __gnat_init_float (void)
2526 #endif
2528 /***********************************/
2529 /* __gnat_adjust_context_for_raise */
2530 /***********************************/
2532 #ifndef HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
2534 /* All targets without a specific version will use an empty one. */
2536 /* Given UCONTEXT a pointer to a context structure received by a signal
2537 handler for SIGNO, perform the necessary adjustments to let the handler
2538 raise an exception. Calls to this routine are not conditioned by the
2539 propagation scheme in use. */
2541 void
2542 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED,
2543 void *ucontext ATTRIBUTE_UNUSED)
2545 /* We used to compensate here for the raised from call vs raised from signal
2546 exception discrepancy with the GCC ZCX scheme, but this now can be dealt
2547 with generically in the unwinder (see GCC PR other/26208). This however
2548 requires the use of the _Unwind_GetIPInfo routine in raise-gcc.c, which
2549 is predicated on the definition of HAVE_GETIPINFO at compile time. Only
2550 the VMS ports still do the compensation described in the few lines below.
2552 *** Call vs signal exception discrepancy with GCC ZCX scheme ***
2554 The GCC unwinder expects to be dealing with call return addresses, since
2555 this is the "nominal" case of what we retrieve while unwinding a regular
2556 call chain.
2558 To evaluate if a handler applies at some point identified by a return
2559 address, the propagation engine needs to determine what region the
2560 corresponding call instruction pertains to. Because the return address
2561 may not be attached to the same region as the call, the unwinder always
2562 subtracts "some" amount from a return address to search the region
2563 tables, amount chosen to ensure that the resulting address is inside the
2564 call instruction.
2566 When we raise an exception from a signal handler, e.g. to transform a
2567 SIGSEGV into Storage_Error, things need to appear as if the signal
2568 handler had been "called" by the instruction which triggered the signal,
2569 so that exception handlers that apply there are considered. What the
2570 unwinder will retrieve as the return address from the signal handler is
2571 what it will find as the faulting instruction address in the signal
2572 context pushed by the kernel. Leaving this address untouched looses, if
2573 the triggering instruction happens to be the very first of a region, as
2574 the later adjustments performed by the unwinder would yield an address
2575 outside that region. We need to compensate for the unwinder adjustments
2576 at some point, and this is what this routine is expected to do.
2578 signo is passed because on some targets for some signals the PC in
2579 context points to the instruction after the faulting one, in which case
2580 the unwinder adjustment is still desired. */
2583 #endif
2585 #ifdef __cplusplus
2587 #endif