PR target/60039
[official-gcc.git] / gcc / ada / init.c
blobc3824ab7ef30b945ebf75e660c7716f1cf870b7e
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * I N I T *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2013, 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 #if defined (i386) || defined (__x86_64__) || defined (__powerpc__)
560 /* This must be in keeping with System.OS_Interface.Alternate_Stack_Size. */
561 char __gnat_alternate_stack[16 * 1024]; /* 2 * SIGSTKSZ */
562 #endif
564 #ifdef __XENO__
565 #include <sys/mman.h>
566 #include <native/task.h>
568 RT_TASK main_task;
569 #endif
571 void
572 __gnat_install_handler (void)
574 struct sigaction act;
576 #ifdef __XENO__
577 int prio;
579 if (__gl_main_priority == -1)
580 prio = 49;
581 else
582 prio = __gl_main_priority;
584 /* Avoid memory swapping for this program */
586 mlockall (MCL_CURRENT|MCL_FUTURE);
588 /* Turn the current Linux task into a native Xenomai task */
590 rt_task_shadow (&main_task, "environment_task", prio, T_FPU);
591 #endif
593 /* Set up signal handler to map synchronous signals to appropriate
594 exceptions. Make sure that the handler isn't interrupted by another
595 signal that might cause a scheduling event! Also setup an alternate
596 stack region for the handler execution so that stack overflows can be
597 handled properly, avoiding a SEGV generation from stack usage by the
598 handler itself. */
600 act.sa_sigaction = __gnat_error_handler;
601 act.sa_flags = SA_NODEFER | SA_RESTART | SA_SIGINFO;
602 sigemptyset (&act.sa_mask);
604 /* Do not install handlers if interrupt state is "System". */
605 if (__gnat_get_interrupt_state (SIGABRT) != 's')
606 sigaction (SIGABRT, &act, NULL);
607 if (__gnat_get_interrupt_state (SIGFPE) != 's')
608 sigaction (SIGFPE, &act, NULL);
609 if (__gnat_get_interrupt_state (SIGILL) != 's')
610 sigaction (SIGILL, &act, NULL);
611 if (__gnat_get_interrupt_state (SIGBUS) != 's')
612 sigaction (SIGBUS, &act, NULL);
613 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
615 #if defined (i386) || defined (__x86_64__) || defined (__powerpc__)
616 /* Setup an alternate stack region for the handler execution so that
617 stack overflows can be handled properly, avoiding a SEGV generation
618 from stack usage by the handler itself. */
619 stack_t stack;
621 stack.ss_sp = __gnat_alternate_stack;
622 stack.ss_size = sizeof (__gnat_alternate_stack);
623 stack.ss_flags = 0;
624 sigaltstack (&stack, NULL);
626 act.sa_flags |= SA_ONSTACK;
627 #endif
628 sigaction (SIGSEGV, &act, NULL);
631 __gnat_handler_installed = 1;
634 /*******************/
635 /* LynxOS Section */
636 /*******************/
638 #elif defined (__Lynx__)
640 #include <signal.h>
641 #include <unistd.h>
643 static void
644 __gnat_error_handler (int sig)
646 struct Exception_Data *exception;
647 const char *msg;
649 switch(sig)
651 case SIGFPE:
652 exception = &constraint_error;
653 msg = "SIGFPE";
654 break;
655 case SIGILL:
656 exception = &constraint_error;
657 msg = "SIGILL";
658 break;
659 case SIGSEGV:
660 exception = &storage_error;
661 msg = "stack overflow or erroneous memory access";
662 break;
663 case SIGBUS:
664 exception = &constraint_error;
665 msg = "SIGBUS";
666 break;
667 default:
668 exception = &program_error;
669 msg = "unhandled signal";
672 Raise_From_Signal_Handler(exception, msg);
675 void
676 __gnat_install_handler(void)
678 struct sigaction act;
680 act.sa_handler = __gnat_error_handler;
681 act.sa_flags = 0x0;
682 sigemptyset (&act.sa_mask);
684 /* Do not install handlers if interrupt state is "System". */
685 if (__gnat_get_interrupt_state (SIGFPE) != 's')
686 sigaction (SIGFPE, &act, NULL);
687 if (__gnat_get_interrupt_state (SIGILL) != 's')
688 sigaction (SIGILL, &act, NULL);
689 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
690 sigaction (SIGSEGV, &act, NULL);
691 if (__gnat_get_interrupt_state (SIGBUS) != 's')
692 sigaction (SIGBUS, &act, NULL);
694 __gnat_handler_installed = 1;
697 /*******************/
698 /* Solaris Section */
699 /*******************/
701 #elif defined (sun) && defined (__SVR4) && !defined (__vxworks)
703 #include <signal.h>
704 #include <siginfo.h>
705 #include <sys/ucontext.h>
706 #include <sys/regset.h>
708 static void
709 __gnat_error_handler (int sig, siginfo_t *si, void *ucontext ATTRIBUTE_UNUSED)
711 struct Exception_Data *exception;
712 static int recurse = 0;
713 const char *msg;
715 switch (sig)
717 case SIGSEGV:
718 /* If the problem was permissions, this is a constraint error.
719 Likewise if the failing address isn't maximally aligned or if
720 we've recursed.
722 ??? Using a static variable here isn't task-safe, but it's
723 much too hard to do anything else and we're just determining
724 which exception to raise. */
725 if (si->si_code == SEGV_ACCERR
726 || (long) si->si_addr == 0
727 || (((long) si->si_addr) & 3) != 0
728 || recurse)
730 exception = &constraint_error;
731 msg = "SIGSEGV";
733 else
735 /* See if the page before the faulting page is accessible. Do that
736 by trying to access it. We'd like to simply try to access
737 4096 + the faulting address, but it's not guaranteed to be
738 the actual address, just to be on the same page. */
739 recurse++;
740 ((volatile char *)
741 ((long) si->si_addr & - getpagesize ()))[getpagesize ()];
742 exception = &storage_error;
743 msg = "stack overflow or erroneous memory access";
745 break;
747 case SIGBUS:
748 exception = &program_error;
749 msg = "SIGBUS";
750 break;
752 case SIGFPE:
753 exception = &constraint_error;
754 msg = "SIGFPE";
755 break;
757 default:
758 exception = &program_error;
759 msg = "unhandled signal";
762 recurse = 0;
763 Raise_From_Signal_Handler (exception, msg);
766 void
767 __gnat_install_handler (void)
769 struct sigaction act;
771 /* Set up signal handler to map synchronous signals to appropriate
772 exceptions. Make sure that the handler isn't interrupted by another
773 signal that might cause a scheduling event! */
775 act.sa_sigaction = __gnat_error_handler;
776 act.sa_flags = SA_NODEFER | SA_RESTART | SA_SIGINFO;
777 sigemptyset (&act.sa_mask);
779 /* Do not install handlers if interrupt state is "System". */
780 if (__gnat_get_interrupt_state (SIGABRT) != 's')
781 sigaction (SIGABRT, &act, NULL);
782 if (__gnat_get_interrupt_state (SIGFPE) != 's')
783 sigaction (SIGFPE, &act, NULL);
784 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
785 sigaction (SIGSEGV, &act, NULL);
786 if (__gnat_get_interrupt_state (SIGBUS) != 's')
787 sigaction (SIGBUS, &act, NULL);
789 __gnat_handler_installed = 1;
792 /***************/
793 /* VMS Section */
794 /***************/
796 #elif defined (VMS)
798 /* Routine called from binder to override default feature values. */
799 void __gnat_set_features (void);
800 int __gnat_features_set = 0;
801 void (*__gnat_ctrl_c_handler) (void) = 0;
803 #ifdef __IA64
804 #define lib_get_curr_invo_context LIB$I64_GET_CURR_INVO_CONTEXT
805 #define lib_get_prev_invo_context LIB$I64_GET_PREV_INVO_CONTEXT
806 #define lib_get_invo_handle LIB$I64_GET_INVO_HANDLE
807 #else
808 #define lib_get_curr_invo_context LIB$GET_CURR_INVO_CONTEXT
809 #define lib_get_prev_invo_context LIB$GET_PREV_INVO_CONTEXT
810 #define lib_get_invo_handle LIB$GET_INVO_HANDLE
811 #endif
813 /* Masks for facility identification. */
814 #define FAC_MASK 0x0fff0000
815 #define DECADA_M_FACILITY 0x00310000
817 /* Define macro symbols for the VMS conditions that become Ada exceptions.
818 It would be better to just include <ssdef.h> */
820 #define SS$_CONTINUE 1
821 #define SS$_ACCVIO 12
822 #define SS$_HPARITH 1284
823 #define SS$_INTDIV 1156
824 #define SS$_STKOVF 1364
825 #define SS$_CONTROLC 1617
826 #define SS$_RESIGNAL 2328
828 #define MTH$_FLOOVEMAT 1475268 /* Some ACVC_21 CXA tests */
830 /* The following codes must be resignalled, and not handled here. */
832 /* These codes are in standard message libraries. */
833 extern int C$_SIGKILL;
834 extern int C$_SIGINT;
835 extern int SS$_DEBUG;
836 extern int LIB$_KEYNOTFOU;
837 extern int LIB$_ACTIMAGE;
839 /* These codes are non standard, which is to say the author is
840 not sure if they are defined in the standard message libraries
841 so keep them as macros for now. */
842 #define RDB$_STREAM_EOF 20480426
843 #define FDL$_UNPRIKW 11829410
844 #define CMA$_EXIT_THREAD 4227492
846 struct cond_sigargs
848 unsigned int sigarg;
849 unsigned int sigargval;
852 struct cond_subtests
854 unsigned int num;
855 const struct cond_sigargs sigargs[];
858 struct cond_except
860 unsigned int cond;
861 const struct Exception_Data *except;
862 unsigned int needs_adjust; /* 1 = adjust PC, 0 = no adjust */
863 const struct cond_subtests *subtests;
866 struct descriptor_s
868 unsigned short len, mbz;
869 __char_ptr32 adr;
872 /* Conditions that don't have an Ada exception counterpart must raise
873 Non_Ada_Error. Since this is defined in s-auxdec, it should only be
874 referenced by user programs, not the compiler or tools. Hence the
875 #ifdef IN_RTS. */
877 #ifdef IN_RTS
879 #define Status_Error ada__io_exceptions__status_error
880 extern struct Exception_Data Status_Error;
882 #define Mode_Error ada__io_exceptions__mode_error
883 extern struct Exception_Data Mode_Error;
885 #define Name_Error ada__io_exceptions__name_error
886 extern struct Exception_Data Name_Error;
888 #define Use_Error ada__io_exceptions__use_error
889 extern struct Exception_Data Use_Error;
891 #define Device_Error ada__io_exceptions__device_error
892 extern struct Exception_Data Device_Error;
894 #define End_Error ada__io_exceptions__end_error
895 extern struct Exception_Data End_Error;
897 #define Data_Error ada__io_exceptions__data_error
898 extern struct Exception_Data Data_Error;
900 #define Layout_Error ada__io_exceptions__layout_error
901 extern struct Exception_Data Layout_Error;
903 #define Non_Ada_Error system__aux_dec__non_ada_error
904 extern struct Exception_Data Non_Ada_Error;
906 #define Coded_Exception system__vms_exception_table__coded_exception
907 extern struct Exception_Data *Coded_Exception (void *);
909 #define Base_Code_In system__vms_exception_table__base_code_in
910 extern void *Base_Code_In (void *);
912 /* DEC Ada exceptions are not defined in a header file, so they
913 must be declared. */
915 #define ADA$_ALREADY_OPEN 0x0031a594
916 #define ADA$_CONSTRAINT_ERRO 0x00318324
917 #define ADA$_DATA_ERROR 0x003192c4
918 #define ADA$_DEVICE_ERROR 0x003195e4
919 #define ADA$_END_ERROR 0x00319904
920 #define ADA$_FAC_MODE_MISMAT 0x0031a8b3
921 #define ADA$_IOSYSFAILED 0x0031af04
922 #define ADA$_KEYSIZERR 0x0031aa3c
923 #define ADA$_KEY_MISMATCH 0x0031a8e3
924 #define ADA$_LAYOUT_ERROR 0x00319c24
925 #define ADA$_LINEXCMRS 0x0031a8f3
926 #define ADA$_MAXLINEXC 0x0031a8eb
927 #define ADA$_MODE_ERROR 0x00319f44
928 #define ADA$_MRN_MISMATCH 0x0031a8db
929 #define ADA$_MRS_MISMATCH 0x0031a8d3
930 #define ADA$_NAME_ERROR 0x0031a264
931 #define ADA$_NOT_OPEN 0x0031a58c
932 #define ADA$_ORG_MISMATCH 0x0031a8bb
933 #define ADA$_PROGRAM_ERROR 0x00318964
934 #define ADA$_RAT_MISMATCH 0x0031a8cb
935 #define ADA$_RFM_MISMATCH 0x0031a8c3
936 #define ADA$_STAOVF 0x00318cac
937 #define ADA$_STATUS_ERROR 0x0031a584
938 #define ADA$_STORAGE_ERROR 0x00318c84
939 #define ADA$_UNSUPPORTED 0x0031a8ab
940 #define ADA$_USE_ERROR 0x0031a8a4
942 /* DEC Ada specific conditions. */
943 static const struct cond_except dec_ada_cond_except_table [] =
945 {ADA$_PROGRAM_ERROR, &program_error, 0, 0},
946 {ADA$_USE_ERROR, &Use_Error, 0, 0},
947 {ADA$_KEYSIZERR, &program_error, 0, 0},
948 {ADA$_STAOVF, &storage_error, 0, 0},
949 {ADA$_CONSTRAINT_ERRO, &constraint_error, 0, 0},
950 {ADA$_IOSYSFAILED, &Device_Error, 0, 0},
951 {ADA$_LAYOUT_ERROR, &Layout_Error, 0, 0},
952 {ADA$_STORAGE_ERROR, &storage_error, 0, 0},
953 {ADA$_DATA_ERROR, &Data_Error, 0, 0},
954 {ADA$_DEVICE_ERROR, &Device_Error, 0, 0},
955 {ADA$_END_ERROR, &End_Error, 0, 0},
956 {ADA$_MODE_ERROR, &Mode_Error, 0, 0},
957 {ADA$_NAME_ERROR, &Name_Error, 0, 0},
958 {ADA$_STATUS_ERROR, &Status_Error, 0, 0},
959 {ADA$_NOT_OPEN, &Use_Error, 0, 0},
960 {ADA$_ALREADY_OPEN, &Use_Error, 0, 0},
961 {ADA$_USE_ERROR, &Use_Error, 0, 0},
962 {ADA$_UNSUPPORTED, &Use_Error, 0, 0},
963 {ADA$_FAC_MODE_MISMAT, &Use_Error, 0, 0},
964 {ADA$_ORG_MISMATCH, &Use_Error, 0, 0},
965 {ADA$_RFM_MISMATCH, &Use_Error, 0, 0},
966 {ADA$_RAT_MISMATCH, &Use_Error, 0, 0},
967 {ADA$_MRS_MISMATCH, &Use_Error, 0, 0},
968 {ADA$_MRN_MISMATCH, &Use_Error, 0, 0},
969 {ADA$_KEY_MISMATCH, &Use_Error, 0, 0},
970 {ADA$_MAXLINEXC, &constraint_error, 0, 0},
971 {ADA$_LINEXCMRS, &constraint_error, 0, 0},
973 #if 0
974 /* Already handled by a pragma Import_Exception
975 in Aux_IO_Exceptions */
976 {ADA$_LOCK_ERROR, &Lock_Error, 0, 0},
977 {ADA$_EXISTENCE_ERROR, &Existence_Error, 0, 0},
978 {ADA$_KEY_ERROR, &Key_Error, 0, 0},
979 #endif
981 {0, 0, 0, 0}
984 #endif /* IN_RTS */
986 /* Non-DEC Ada specific conditions that map to Ada exceptions. */
988 /* Subtest for ACCVIO Constraint_Error, kept for compatibility,
989 in hindsight should have just made ACCVIO == Storage_Error. */
990 #define ACCVIO_VIRTUAL_ADDR 3
991 static const struct cond_subtests accvio_c_e =
992 {1, /* number of subtests below */
994 { ACCVIO_VIRTUAL_ADDR, 0 }
998 /* Macro flag to adjust PC which gets off by one for some conditions,
999 not sure if this is reliably true, PC could be off by more for
1000 HPARITH for example, unless a trapb is inserted. */
1001 #define NEEDS_ADJUST 1
1003 static const struct cond_except system_cond_except_table [] =
1005 {MTH$_FLOOVEMAT, &constraint_error, 0, 0},
1006 {SS$_INTDIV, &constraint_error, 0, 0},
1007 {SS$_HPARITH, &constraint_error, NEEDS_ADJUST, 0},
1008 {SS$_ACCVIO, &constraint_error, NEEDS_ADJUST, &accvio_c_e},
1009 {SS$_ACCVIO, &storage_error, NEEDS_ADJUST, 0},
1010 {SS$_STKOVF, &storage_error, NEEDS_ADJUST, 0},
1011 {0, 0, 0, 0}
1014 /* To deal with VMS conditions and their mapping to Ada exceptions,
1015 the __gnat_error_handler routine below is installed as an exception
1016 vector having precedence over DEC frame handlers. Some conditions
1017 still need to be handled by such handlers, however, in which case
1018 __gnat_error_handler needs to return SS$_RESIGNAL. Consider for
1019 instance the use of a third party library compiled with DECAda and
1020 performing its own exception handling internally.
1022 To allow some user-level flexibility, which conditions should be
1023 resignaled is controlled by a predicate function, provided with the
1024 condition value and returning a boolean indication stating whether
1025 this condition should be resignaled or not.
1027 That predicate function is called indirectly, via a function pointer,
1028 by __gnat_error_handler, and changing that pointer is allowed to the
1029 user code by way of the __gnat_set_resignal_predicate interface.
1031 The user level function may then implement what it likes, including
1032 for instance the maintenance of a dynamic data structure if the set
1033 of to be resignalled conditions has to change over the program's
1034 lifetime.
1036 ??? This is not a perfect solution to deal with the possible
1037 interactions between the GNAT and the DECAda exception handling
1038 models and better (more general) schemes are studied. This is so
1039 just provided as a convenient workaround in the meantime, and
1040 should be use with caution since the implementation has been kept
1041 very simple. */
1043 typedef int resignal_predicate (int code);
1045 static const int * const cond_resignal_table [] =
1047 &C$_SIGKILL,
1048 (int *)CMA$_EXIT_THREAD,
1049 &SS$_DEBUG,
1050 &LIB$_KEYNOTFOU,
1051 &LIB$_ACTIMAGE,
1052 (int *) RDB$_STREAM_EOF,
1053 (int *) FDL$_UNPRIKW,
1057 static const int facility_resignal_table [] =
1059 0x1380000, /* RDB */
1060 0x2220000, /* SQL */
1064 /* Default GNAT predicate for resignaling conditions. */
1066 static int
1067 __gnat_default_resignal_p (int code)
1069 int i, iexcept;
1071 for (i = 0; facility_resignal_table [i]; i++)
1072 if ((code & FAC_MASK) == facility_resignal_table [i])
1073 return 1;
1075 for (i = 0, iexcept = 0;
1076 cond_resignal_table [i]
1077 && !(iexcept = LIB$MATCH_COND (&code, &cond_resignal_table [i]));
1078 i++);
1080 return iexcept;
1083 /* Static pointer to predicate that the __gnat_error_handler exception
1084 vector invokes to determine if it should resignal a condition. */
1086 static resignal_predicate *__gnat_resignal_p = __gnat_default_resignal_p;
1088 /* User interface to change the predicate pointer to PREDICATE. Reset to
1089 the default if PREDICATE is null. */
1091 void
1092 __gnat_set_resignal_predicate (resignal_predicate *predicate)
1094 if (predicate == NULL)
1095 __gnat_resignal_p = __gnat_default_resignal_p;
1096 else
1097 __gnat_resignal_p = predicate;
1100 /* Should match System.Parameters.Default_Exception_Msg_Max_Length. */
1101 #define Default_Exception_Msg_Max_Length 512
1103 /* Action routine for SYS$PUTMSG. There may be multiple
1104 conditions, each with text to be appended to MESSAGE
1105 and separated by line termination. */
1106 static int
1107 copy_msg (struct descriptor_s *msgdesc, char *message)
1109 int len = strlen (message);
1110 int copy_len;
1112 /* Check for buffer overflow and skip. */
1113 if (len > 0 && len <= Default_Exception_Msg_Max_Length - 3)
1115 strcat (message, "\r\n");
1116 len += 2;
1119 /* Check for buffer overflow and truncate if necessary. */
1120 copy_len = (len + msgdesc->len <= Default_Exception_Msg_Max_Length - 1 ?
1121 msgdesc->len :
1122 Default_Exception_Msg_Max_Length - 1 - len);
1123 strncpy (&message [len], msgdesc->adr, copy_len);
1124 message [len + copy_len] = 0;
1126 return 0;
1129 /* Scan TABLE for a match for the condition contained in SIGARGS,
1130 and return the entry, or the empty entry if no match found. */
1131 static const struct cond_except *
1132 scan_conditions ( int *sigargs, const struct cond_except *table [])
1134 int i;
1135 struct cond_except entry;
1137 /* Scan the exception condition table for a match and fetch
1138 the associated GNAT exception pointer. */
1139 for (i = 0; (*table) [i].cond; i++)
1141 unsigned int match = LIB$MATCH_COND (&sigargs [1], &(*table) [i].cond);
1142 const struct cond_subtests *subtests = (*table) [i].subtests;
1144 if (match)
1146 if (!subtests)
1148 return &(*table) [i];
1150 else
1152 unsigned int ii;
1153 int num = (*subtests).num;
1155 /* Perform subtests to differentiate exception. */
1156 for (ii = 0; ii < num; ii++)
1158 unsigned int arg = (*subtests).sigargs [ii].sigarg;
1159 unsigned int argval = (*subtests).sigargs [ii].sigargval;
1161 if (sigargs [arg] != argval)
1163 num = 0;
1164 break;
1168 /* All subtests passed. */
1169 if (num == (*subtests).num)
1170 return &(*table) [i];
1175 /* No match, return the null terminating entry. */
1176 return &(*table) [i];
1179 /* __gnat_handle_vms_condtition is both a frame based handler
1180 for the runtime, and an exception vector for the compiler. */
1181 long
1182 __gnat_handle_vms_condition (int *sigargs, void *mechargs)
1184 struct Exception_Data *exception = 0;
1185 unsigned int needs_adjust = 0;
1186 void *base_code;
1187 struct descriptor_s gnat_facility = {4, 0, "GNAT"};
1188 char message [Default_Exception_Msg_Max_Length];
1190 const char *msg = "";
1192 /* Check for conditions to resignal which aren't effected by pragma
1193 Import_Exception. */
1194 if (__gnat_resignal_p (sigargs [1]))
1195 return SS$_RESIGNAL;
1196 #ifndef IN_RTS
1197 /* toplev.c handles this for compiler. */
1198 if (sigargs [1] == SS$_HPARITH)
1199 return SS$_RESIGNAL;
1200 #endif
1202 #ifdef IN_RTS
1203 /* See if it's an imported exception. Beware that registered exceptions
1204 are bound to their base code, with the severity bits masked off. */
1205 base_code = Base_Code_In ((void *) sigargs[1]);
1206 exception = Coded_Exception (base_code);
1207 #endif
1209 if (exception == 0)
1210 #ifdef IN_RTS
1212 int i;
1213 struct cond_except cond;
1214 const struct cond_except *cond_table;
1215 const struct cond_except *cond_tables [] = {dec_ada_cond_except_table,
1216 system_cond_except_table,
1218 unsigned int ctrlc = SS$_CONTROLC;
1219 unsigned int *sigint = &C$_SIGINT;
1220 int ctrlc_match = LIB$MATCH_COND (&sigargs [1], &ctrlc);
1221 int sigint_match = LIB$MATCH_COND (&sigargs [1], &sigint);
1223 extern int SYS$DCLAST (void (*astadr)(), unsigned long long astprm,
1224 unsigned int acmode);
1226 /* If SS$_CONTROLC has been imported as an exception, it will take
1227 priority over a a Ctrl/C handler. See above. SIGINT has a
1228 different condition value due to it's DECCCRTL roots and it's
1229 the condition that gets raised for a "kill -INT". */
1230 if ((ctrlc_match || sigint_match) && __gnat_ctrl_c_handler)
1232 SYS$DCLAST (__gnat_ctrl_c_handler, 0, 0);
1233 return SS$_CONTINUE;
1236 i = 0;
1237 while ((cond_table = cond_tables[i++]) && !exception)
1239 cond = *scan_conditions (sigargs, &cond_table);
1240 exception = (struct Exception_Data *) cond.except;
1243 if (exception)
1244 needs_adjust = cond.needs_adjust;
1245 else
1246 /* User programs expect Non_Ada_Error to be raised if no match,
1247 reference DEC Ada test CXCONDHAN. */
1248 exception = &Non_Ada_Error;
1250 #else
1252 /* Pretty much everything is just a program error in the compiler */
1253 exception = &program_error;
1255 #endif
1257 message[0] = 0;
1258 /* Subtract PC & PSL fields as per ABI for SYS$PUTMSG. */
1259 sigargs[0] -= 2;
1261 extern int SYS$PUTMSG (void *, int (*)(), void *, unsigned long long);
1263 /* If it was a DEC Ada specific condtiion, make it GNAT otherwise
1264 keep the old facility. */
1265 if (sigargs [1] & FAC_MASK == DECADA_M_FACILITY)
1266 SYS$PUTMSG (sigargs, copy_msg, &gnat_facility,
1267 (unsigned long long ) message);
1268 else
1269 SYS$PUTMSG (sigargs, copy_msg, 0,
1270 (unsigned long long ) message);
1272 /* Add back PC & PSL fields as per ABI for SYS$PUTMSG. */
1273 sigargs[0] += 2;
1274 msg = message;
1276 if (needs_adjust)
1277 __gnat_adjust_context_for_raise (sigargs [1], (void *)mechargs);
1279 Raise_From_Signal_Handler (exception, msg);
1282 #if defined (IN_RTS) && defined (__IA64)
1283 /* Called only from adasigio.b32. This is a band aid to avoid going
1284 through the VMS signal handling code which results in a 0x8000 per
1285 handled exception memory leak in P2 space (see VMS source listing
1286 sys/lis/exception.lis) due to the allocation of working space that
1287 is expected to be deallocated upon return from the condition handler,
1288 which doesn't return in GNAT compiled code. */
1289 void
1290 GNAT$STOP (int *sigargs)
1292 /* Note that there are no mechargs. We rely on the fact that condtions
1293 raised from DEClib I/O do not require an "adjust". Also the count
1294 will be off by 2, since LIB$STOP didn't get a chance to add the
1295 PC and PSL fields, so we bump it so PUTMSG comes out right. */
1296 sigargs [0] += 2;
1297 __gnat_handle_vms_condition (sigargs, 0);
1299 #endif
1301 void
1302 __gnat_install_handler (void)
1304 long prvhnd ATTRIBUTE_UNUSED;
1306 #if !defined (IN_RTS)
1307 extern int SYS$SETEXV (unsigned int vector, int (*addres)(),
1308 unsigned int accmode, void *(*(prvhnd)));
1309 SYS$SETEXV (1, __gnat_handle_vms_condition, 3, &prvhnd);
1310 #endif
1312 __gnat_handler_installed = 1;
1315 /* __gnat_adjust_context_for_raise for Alpha - see comments along with the
1316 default version later in this file. */
1318 #if defined (IN_RTS) && defined (__alpha__)
1320 #include <vms/chfctxdef.h>
1321 #include <vms/chfdef.h>
1323 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
1325 void
1326 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED, void *ucontext)
1328 if (signo == SS$_HPARITH)
1330 /* Sub one to the address of the instruction signaling the condition,
1331 located in the sigargs array. */
1333 CHF$MECH_ARRAY * mechargs = (CHF$MECH_ARRAY *) ucontext;
1334 CHF$SIGNAL_ARRAY * sigargs
1335 = (CHF$SIGNAL_ARRAY *) mechargs->chf$q_mch_sig_addr;
1337 int vcount = sigargs->chf$is_sig_args;
1338 int * pc_slot = & (&sigargs->chf$l_sig_name)[vcount-2];
1340 (*pc_slot)--;
1344 #endif
1346 /* __gnat_adjust_context_for_raise for ia64. */
1348 #if defined (IN_RTS) && defined (__IA64)
1350 #include <vms/chfctxdef.h>
1351 #include <vms/chfdef.h>
1353 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
1355 typedef unsigned long long u64;
1357 void
1358 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED, void *ucontext)
1360 /* Add one to the address of the instruction signaling the condition,
1361 located in the 64bits sigargs array. */
1363 CHF$MECH_ARRAY * mechargs = (CHF$MECH_ARRAY *) ucontext;
1365 CHF64$SIGNAL_ARRAY *chfsig64
1366 = (CHF64$SIGNAL_ARRAY *) mechargs->chf$ph_mch_sig64_addr;
1368 u64 * post_sigarray
1369 = (u64 *)chfsig64 + 1 + chfsig64->chf64$l_sig_args;
1371 u64 * ih_pc_loc = post_sigarray - 2;
1373 (*ih_pc_loc) ++;
1376 #endif
1378 /* Easier interface for LIB$GET_LOGICAL: put the equivalence of NAME into BUF,
1379 always NUL terminated. In case of error or if the result is longer than
1380 LEN (length of BUF) an empty string is written info BUF. */
1382 static void
1383 __gnat_vms_get_logical (const char *name, char *buf, int len)
1385 struct descriptor_s name_desc, result_desc;
1386 int status;
1387 unsigned short rlen;
1389 /* Build the descriptor for NAME. */
1390 name_desc.len = strlen (name);
1391 name_desc.mbz = 0;
1392 name_desc.adr = (char *)name;
1394 /* Build the descriptor for the result. */
1395 result_desc.len = len;
1396 result_desc.mbz = 0;
1397 result_desc.adr = buf;
1399 status = LIB$GET_LOGICAL (&name_desc, &result_desc, &rlen);
1401 if ((status & 1) == 1 && rlen < len)
1402 buf[rlen] = 0;
1403 else
1404 buf[0] = 0;
1407 /* Size of a page on ia64 and alpha VMS. */
1408 #define VMS_PAGESIZE 8192
1410 /* User mode. */
1411 #define PSL__C_USER 3
1413 /* No access. */
1414 #define PRT__C_NA 0
1416 /* Descending region. */
1417 #define VA__M_DESCEND 1
1419 /* Get by virtual address. */
1420 #define VA___REGSUM_BY_VA 1
1422 /* Memory region summary. */
1423 struct regsum
1425 unsigned long long q_region_id;
1426 unsigned int l_flags;
1427 unsigned int l_region_protection;
1428 void *pq_start_va;
1429 unsigned long long q_region_size;
1430 void *pq_first_free_va;
1433 extern int SYS$GET_REGION_INFO (unsigned int, unsigned long long *,
1434 void *, void *, unsigned int,
1435 void *, unsigned int *);
1436 extern int SYS$EXPREG_64 (unsigned long long *, unsigned long long,
1437 unsigned int, unsigned int, void **,
1438 unsigned long long *);
1439 extern int SYS$SETPRT_64 (void *, unsigned long long, unsigned int,
1440 unsigned int, void **, unsigned long long *,
1441 unsigned int *);
1443 /* Add a guard page in the memory region containing ADDR at ADDR +/- SIZE.
1444 (The sign depends on the kind of the memory region). */
1446 static int
1447 __gnat_set_stack_guard_page (void *addr, unsigned long size)
1449 int status;
1450 void *ret_va;
1451 unsigned long long ret_len;
1452 unsigned int ret_prot;
1453 void *start_va;
1454 unsigned long long length;
1455 unsigned int retlen;
1456 struct regsum buffer;
1458 /* Get the region for ADDR. */
1459 status = SYS$GET_REGION_INFO
1460 (VA___REGSUM_BY_VA, NULL, addr, NULL, sizeof (buffer), &buffer, &retlen);
1462 if ((status & 1) != 1)
1463 return -1;
1465 /* Extend the region. */
1466 status = SYS$EXPREG_64 (&buffer.q_region_id,
1467 size, 0, 0, &start_va, &length);
1469 if ((status & 1) != 1)
1470 return -1;
1472 /* Create a guard page. */
1473 if (!(buffer.l_flags & VA__M_DESCEND))
1474 start_va = (void *)((unsigned long long)start_va + length - VMS_PAGESIZE);
1476 status = SYS$SETPRT_64 (start_va, VMS_PAGESIZE, PSL__C_USER, PRT__C_NA,
1477 &ret_va, &ret_len, &ret_prot);
1479 if ((status & 1) != 1)
1480 return -1;
1481 return 0;
1484 /* Read logicals to limit the stack(s) size. */
1486 static void
1487 __gnat_set_stack_limit (void)
1489 #ifdef __ia64__
1490 void *sp;
1491 unsigned long size;
1492 char value[16];
1493 char *e;
1495 /* The main stack. */
1496 __gnat_vms_get_logical ("GNAT_STACK_SIZE", value, sizeof (value));
1497 size = strtoul (value, &e, 0);
1498 if (e > value && *e == 0)
1500 asm ("mov %0=sp" : "=r" (sp));
1501 __gnat_set_stack_guard_page (sp, size * 1024);
1504 /* The register stack. */
1505 __gnat_vms_get_logical ("GNAT_RBS_SIZE", value, sizeof (value));
1506 size = strtoul (value, &e, 0);
1507 if (e > value && *e == 0)
1509 asm ("mov %0=ar.bsp" : "=r" (sp));
1510 __gnat_set_stack_guard_page (sp, size * 1024);
1512 #endif
1515 #ifdef IN_RTS
1516 extern int SYS$IEEE_SET_FP_CONTROL (void *, void *, void *);
1517 #define K_TRUE 1
1518 #define __int64 long long
1519 #define __NEW_STARLET
1520 #include <vms/ieeedef.h>
1521 #endif
1523 /* Feature logical name and global variable address pair.
1524 If we ever add another feature logical to this list, the
1525 feature struct will need to be enhanced to take into account
1526 possible values for *gl_addr. */
1527 struct feature {
1528 const char *name;
1529 int *gl_addr;
1532 /* Default values for GNAT features set by environment or binder. */
1533 int __gl_heap_size = 64;
1535 /* Default float format is 'I' meaning IEEE. If gnatbind detetcts that a
1536 VAX Float format is specified, it will set this global variable to 'V'.
1537 Subsequently __gnat_set_features will test the variable and if set for
1538 VAX Float will call a Starlet function to enable trapping for invalid
1539 operation, drivide by zero, and overflow. This will prevent the VMS runtime
1540 (specifically OTS$CHECK_FP_MODE) from complaining about inconsistent
1541 floating point settings in a mixed language program. Ideally the setting
1542 would be determined at link time based on setttings in the object files,
1543 however the VMS linker seems to take the setting from the first object
1544 in the link, e.g. pcrt0.o which is float representation neutral. */
1545 char __gl_float_format = 'I';
1547 /* Array feature logical names and global variable addresses. */
1548 static const struct feature features[] =
1550 {"GNAT$NO_MALLOC_64", &__gl_heap_size},
1551 {0, 0}
1554 void
1555 __gnat_set_features (void)
1557 int i;
1558 char buff[16];
1559 #ifdef IN_RTS
1560 IEEE clrmsk, setmsk, prvmsk;
1562 clrmsk.ieee$q_flags = 0LL;
1563 setmsk.ieee$q_flags = 0LL;
1564 #endif
1566 /* Loop through features array and test name for enable/disable. */
1567 for (i = 0; features[i].name; i++)
1569 __gnat_vms_get_logical (features[i].name, buff, sizeof (buff));
1571 if (strcmp (buff, "ENABLE") == 0
1572 || strcmp (buff, "TRUE") == 0
1573 || strcmp (buff, "1") == 0)
1574 *features[i].gl_addr = 32;
1575 else if (strcmp (buff, "DISABLE") == 0
1576 || strcmp (buff, "FALSE") == 0
1577 || strcmp (buff, "0") == 0)
1578 *features[i].gl_addr = 64;
1581 /* Features to artificially limit the stack size. */
1582 __gnat_set_stack_limit ();
1584 #ifdef IN_RTS
1585 if (__gl_float_format == 'V')
1587 setmsk.ieee$v_trap_enable_inv = K_TRUE;
1588 setmsk.ieee$v_trap_enable_dze = K_TRUE;
1589 setmsk.ieee$v_trap_enable_ovf = K_TRUE;
1590 SYS$IEEE_SET_FP_CONTROL (&clrmsk, &setmsk, &prvmsk);
1592 #endif
1594 __gnat_features_set = 1;
1597 /* Return true if the VMS version is 7.x. */
1599 extern unsigned int LIB$GETSYI (int *, ...);
1601 #define SYI$_VERSION 0x1000
1604 __gnat_is_vms_v7 (void)
1606 struct descriptor_s desc;
1607 char version[8];
1608 int status;
1609 int code = SYI$_VERSION;
1611 desc.len = sizeof (version);
1612 desc.mbz = 0;
1613 desc.adr = version;
1615 status = LIB$GETSYI (&code, 0, &desc);
1616 if ((status & 1) == 1 && version[1] == '7' && version[2] == '.')
1617 return 1;
1618 else
1619 return 0;
1622 /*******************/
1623 /* FreeBSD Section */
1624 /*******************/
1626 #elif defined (__FreeBSD__)
1628 #include <signal.h>
1629 #include <sys/ucontext.h>
1630 #include <unistd.h>
1632 static void
1633 __gnat_error_handler (int sig,
1634 siginfo_t *si ATTRIBUTE_UNUSED,
1635 void *ucontext ATTRIBUTE_UNUSED)
1637 struct Exception_Data *exception;
1638 const char *msg;
1640 switch (sig)
1642 case SIGFPE:
1643 exception = &constraint_error;
1644 msg = "SIGFPE";
1645 break;
1647 case SIGILL:
1648 exception = &constraint_error;
1649 msg = "SIGILL";
1650 break;
1652 case SIGSEGV:
1653 exception = &storage_error;
1654 msg = "stack overflow or erroneous memory access";
1655 break;
1657 case SIGBUS:
1658 exception = &storage_error;
1659 msg = "SIGBUS: possible stack overflow";
1660 break;
1662 default:
1663 exception = &program_error;
1664 msg = "unhandled signal";
1667 Raise_From_Signal_Handler (exception, msg);
1670 void
1671 __gnat_install_handler ()
1673 struct sigaction act;
1675 /* Set up signal handler to map synchronous signals to appropriate
1676 exceptions. Make sure that the handler isn't interrupted by another
1677 signal that might cause a scheduling event! */
1679 act.sa_sigaction
1680 = (void (*)(int, struct __siginfo *, void*)) __gnat_error_handler;
1681 act.sa_flags = SA_NODEFER | SA_RESTART | SA_SIGINFO;
1682 (void) sigemptyset (&act.sa_mask);
1684 (void) sigaction (SIGILL, &act, NULL);
1685 (void) sigaction (SIGFPE, &act, NULL);
1686 (void) sigaction (SIGSEGV, &act, NULL);
1687 (void) sigaction (SIGBUS, &act, NULL);
1689 __gnat_handler_installed = 1;
1692 /*******************/
1693 /* VxWorks Section */
1694 /*******************/
1696 #elif defined(__vxworks)
1698 #include <signal.h>
1699 #include <taskLib.h>
1701 #ifndef __RTP__
1702 #include <intLib.h>
1703 #include <iv.h>
1704 #endif
1706 #if defined (ARMEL) && (_WRS_VXWORKS_MAJOR == 6)
1707 #include <vmLib.h>
1708 #endif
1710 #ifdef VTHREADS
1711 #include "private/vThreadsP.h"
1712 #endif
1714 #ifndef __RTP__
1716 /* Directly vectored Interrupt routines are not supported when using RTPs. */
1718 extern int __gnat_inum_to_ivec (int);
1720 /* This is needed by the GNAT run time to handle Vxworks interrupts. */
1722 __gnat_inum_to_ivec (int num)
1724 return (int) INUM_TO_IVEC (num);
1726 #endif
1728 #if !defined(__alpha_vxworks) && (_WRS_VXWORKS_MAJOR != 6) && !defined(__RTP__)
1730 /* getpid is used by s-parint.adb, but is not defined by VxWorks, except
1731 on Alpha VxWorks and VxWorks 6.x (including RTPs). */
1733 extern long getpid (void);
1735 long
1736 getpid (void)
1738 return taskIdSelf ();
1740 #endif
1742 /* VxWorks 653 vThreads expects the field excCnt to be zeroed when a signal is.
1743 handled. The VxWorks version of longjmp does this; GCC's builtin_longjmp
1744 doesn't. */
1745 void
1746 __gnat_clear_exception_count (void)
1748 #ifdef VTHREADS
1749 WIND_TCB *currentTask = (WIND_TCB *) taskIdSelf();
1751 currentTask->vThreads.excCnt = 0;
1752 #endif
1755 /* Handle different SIGnal to exception mappings in different VxWorks
1756 versions. */
1757 static void
1758 __gnat_map_signal (int sig, siginfo_t *si ATTRIBUTE_UNUSED,
1759 void *sc ATTRIBUTE_UNUSED)
1761 struct Exception_Data *exception;
1762 const char *msg;
1764 switch (sig)
1766 case SIGFPE:
1767 exception = &constraint_error;
1768 msg = "SIGFPE";
1769 break;
1770 #ifdef VTHREADS
1771 #ifdef __VXWORKSMILS__
1772 case SIGILL:
1773 exception = &storage_error;
1774 msg = "SIGILL: possible stack overflow";
1775 break;
1776 case SIGSEGV:
1777 exception = &storage_error;
1778 msg = "SIGSEGV";
1779 break;
1780 case SIGBUS:
1781 exception = &program_error;
1782 msg = "SIGBUS";
1783 break;
1784 #else
1785 case SIGILL:
1786 exception = &constraint_error;
1787 msg = "Floating point exception or SIGILL";
1788 break;
1789 case SIGSEGV:
1790 exception = &storage_error;
1791 msg = "SIGSEGV";
1792 break;
1793 case SIGBUS:
1794 exception = &storage_error;
1795 msg = "SIGBUS: possible stack overflow";
1796 break;
1797 #endif
1798 #elif (_WRS_VXWORKS_MAJOR == 6)
1799 case SIGILL:
1800 exception = &constraint_error;
1801 msg = "SIGILL";
1802 break;
1803 #ifdef __RTP__
1804 /* In RTP mode a SIGSEGV is most likely due to a stack overflow,
1805 since stack checking uses the probing mechanism. */
1806 case SIGSEGV:
1807 exception = &storage_error;
1808 msg = "SIGSEGV: possible stack overflow";
1809 break;
1810 case SIGBUS:
1811 exception = &program_error;
1812 msg = "SIGBUS";
1813 break;
1814 #else
1815 /* VxWorks 6 kernel mode with probing. SIGBUS for guard page hit */
1816 case SIGSEGV:
1817 exception = &storage_error;
1818 msg = "SIGSEGV";
1819 break;
1820 case SIGBUS:
1821 exception = &storage_error;
1822 msg = "SIGBUS: possible stack overflow";
1823 break;
1824 #endif
1825 #else
1826 /* VxWorks 5: a SIGILL is most likely due to a stack overflow,
1827 since stack checking uses the stack limit mechanism. */
1828 case SIGILL:
1829 exception = &storage_error;
1830 msg = "SIGILL: possible stack overflow";
1831 break;
1832 case SIGSEGV:
1833 exception = &storage_error;
1834 msg = "SIGSEGV";
1835 break;
1836 case SIGBUS:
1837 exception = &program_error;
1838 msg = "SIGBUS";
1839 break;
1840 #endif
1841 default:
1842 exception = &program_error;
1843 msg = "unhandled signal";
1846 /* On ARM VxWorks 6.x, the guard page is left un-armed by the kernel
1847 after being violated, so subsequent violations aren't detected.
1848 so we retrieve the address of the guard page from the TCB and compare it
1849 with the page that is violated (pREG 12 in the context) and re-arm that
1850 page if there's a match. Additionally we're are assured this is a
1851 genuine stack overflow condition and and set the message and exception
1852 to that effect. */
1853 #if defined (ARMEL) && (_WRS_VXWORKS_MAJOR == 6)
1855 /* We re-arm the guard page by marking it invalid */
1857 #define PAGE_SIZE 4096
1858 #define REG_IP 12
1860 if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL)
1862 TASK_ID tid = taskIdSelf ();
1863 WIND_TCB *pTcb = taskTcb (tid);
1864 unsigned long violated_page
1865 = ((struct sigcontext *) sc)->sc_pregs->r[REG_IP] & ~(PAGE_SIZE - 1);
1867 if ((unsigned long) (pTcb->pStackEnd - PAGE_SIZE) == violated_page)
1869 vmStateSet (NULL, violated_page,
1870 PAGE_SIZE, VM_STATE_MASK_VALID, VM_STATE_VALID_NOT);
1871 exception = &storage_error;
1873 switch (sig)
1875 case SIGSEGV:
1876 msg = "SIGSEGV: stack overflow";
1877 break;
1878 case SIGBUS:
1879 msg = "SIGBUS: stack overflow";
1880 break;
1881 case SIGILL:
1882 msg = "SIGILL: stack overflow";
1883 break;
1887 #endif /* defined (ARMEL) && (_WRS_VXWORKS_MAJOR == 6) */
1889 __gnat_clear_exception_count ();
1890 Raise_From_Signal_Handler (exception, msg);
1893 /* Tasking and Non-tasking signal handler. Map SIGnal to Ada exception
1894 propagation after the required low level adjustments. */
1896 static void
1897 __gnat_error_handler (int sig, siginfo_t *si, void *sc)
1899 sigset_t mask;
1901 /* VxWorks will always mask out the signal during the signal handler and
1902 will reenable it on a longjmp. GNAT does not generate a longjmp to
1903 return from a signal handler so the signal will still be masked unless
1904 we unmask it. */
1905 sigprocmask (SIG_SETMASK, NULL, &mask);
1906 sigdelset (&mask, sig);
1907 sigprocmask (SIG_SETMASK, &mask, NULL);
1909 #if (defined (__ARMEL__) || defined (__PPC__)) && defined(_WRS_KERNEL)
1910 /* On PowerPC, kernel mode, we process signals through a Call Frame Info
1911 trampoline, voiding the need for myriads of fallback_frame_state
1912 variants in the ZCX runtime. We have no simple way to distinguish ZCX
1913 from SJLJ here, so we do this for SJLJ as well even though this is not
1914 necessary. This only incurs a few extra instructions and a tiny
1915 amount of extra stack usage. */
1917 #include "sigtramp.h"
1919 __gnat_sigtramp (sig, (void *)si, (void *)sc,
1920 (sighandler_t *)&__gnat_map_signal);
1922 #else
1923 __gnat_map_signal (sig, si, sc);
1924 #endif
1927 #if defined(__leon__) && defined(_WRS_KERNEL)
1928 /* For LEON VxWorks we need to install a trap handler for stack overflow */
1930 extern void excEnt (void);
1931 /* VxWorks exception handler entry */
1933 struct trap_entry {
1934 unsigned long inst_first;
1935 unsigned long inst_second;
1936 unsigned long inst_third;
1937 unsigned long inst_fourth;
1939 /* Four instructions representing entries in the trap table */
1941 struct trap_entry *trap_0_entry;
1942 /* We will set the location of the entry for software trap 0 in the trap
1943 table. */
1944 #endif
1946 void
1947 __gnat_install_handler (void)
1949 struct sigaction act;
1951 /* Setup signal handler to map synchronous signals to appropriate
1952 exceptions. Make sure that the handler isn't interrupted by another
1953 signal that might cause a scheduling event! */
1955 act.sa_sigaction = __gnat_error_handler;
1956 act.sa_flags = SA_SIGINFO | SA_ONSTACK;
1957 sigemptyset (&act.sa_mask);
1959 /* For VxWorks, install all signal handlers, since pragma Interrupt_State
1960 applies to vectored hardware interrupts, not signals. */
1961 sigaction (SIGFPE, &act, NULL);
1962 sigaction (SIGILL, &act, NULL);
1963 sigaction (SIGSEGV, &act, NULL);
1964 sigaction (SIGBUS, &act, NULL);
1966 #if defined(__leon__) && defined(_WRS_KERNEL)
1967 /* Specific to the LEON VxWorks kernel run-time library */
1969 /* For stack checking the compiler triggers a software trap 0 (ta 0) in
1970 case of overflow (we use the stack limit mechanism). We need to install
1971 the trap handler here for this software trap (the OS does not handle
1972 it) as if it were a data_access_exception (trap 9). We do the same as
1973 if we put in the trap table a VXSPARC_BAD_TRAP(9). Software trap 0 is
1974 located at vector 0x80, and each entry takes 4 words. */
1976 trap_0_entry = (struct trap_entry *)(intVecBaseGet () + 0x80 * 4);
1978 /* mov 0x9, %l7 */
1980 trap_0_entry->inst_first = 0xae102000 + 9;
1982 /* sethi %hi(excEnt), %l6 */
1984 /* The 22 most significant bits of excEnt are obtained shifting 10 times
1985 to the right. */
1987 trap_0_entry->inst_second = 0x2d000000 + ((unsigned long)excEnt >> 10);
1989 /* jmp %l6+%lo(excEnt) */
1991 /* The 10 least significant bits of excEnt are obtained by masking */
1993 trap_0_entry->inst_third = 0x81c5a000 + ((unsigned long)excEnt & 0x3ff);
1995 /* rd %psr, %l0 */
1997 trap_0_entry->inst_fourth = 0xa1480000;
1998 #endif
2000 __gnat_handler_installed = 1;
2003 #define HAVE_GNAT_INIT_FLOAT
2005 void
2006 __gnat_init_float (void)
2008 /* Disable overflow/underflow exceptions on the PPC processor, needed
2009 to get correct Ada semantics. Note that for AE653 vThreads, the HW
2010 overflow settings are an OS configuration issue. The instructions
2011 below have no effect. */
2012 #if defined (_ARCH_PPC) && !defined (_SOFT_FLOAT) && (!defined (VTHREADS) || defined (__VXWORKSMILS__))
2013 #if defined (__SPE__)
2015 const unsigned long spefscr_mask = 0xfffffff3;
2016 unsigned long spefscr;
2017 asm ("mfspr %0, 512" : "=r" (spefscr));
2018 spefscr = spefscr & spefscr_mask;
2019 asm ("mtspr 512, %0\n\tisync" : : "r" (spefscr));
2021 #else
2022 asm ("mtfsb0 25");
2023 asm ("mtfsb0 26");
2024 #endif
2025 #endif
2027 #if (defined (__i386__) || defined (i386)) && !defined (VTHREADS)
2028 /* This is used to properly initialize the FPU on an x86 for each
2029 process thread. */
2030 asm ("finit");
2031 #endif
2033 /* Similarly for SPARC64. Achieved by masking bits in the Trap Enable Mask
2034 field of the Floating-point Status Register (see the SPARC Architecture
2035 Manual Version 9, p 48). */
2036 #if defined (sparc64)
2038 #define FSR_TEM_NVM (1 << 27) /* Invalid operand */
2039 #define FSR_TEM_OFM (1 << 26) /* Overflow */
2040 #define FSR_TEM_UFM (1 << 25) /* Underflow */
2041 #define FSR_TEM_DZM (1 << 24) /* Division by Zero */
2042 #define FSR_TEM_NXM (1 << 23) /* Inexact result */
2044 unsigned int fsr;
2046 __asm__("st %%fsr, %0" : "=m" (fsr));
2047 fsr &= ~(FSR_TEM_OFM | FSR_TEM_UFM);
2048 __asm__("ld %0, %%fsr" : : "m" (fsr));
2050 #endif
2053 /* This subprogram is called by System.Task_Primitives.Operations.Enter_Task
2054 (if not null) when a new task is created. It is initialized by
2055 System.Stack_Checking.Operations.Initialize_Stack_Limit.
2056 The use of a hook avoids to drag stack checking subprograms if stack
2057 checking is not used. */
2058 void (*__gnat_set_stack_limit_hook)(void) = (void (*)(void))0;
2060 /******************/
2061 /* NetBSD Section */
2062 /******************/
2064 #elif defined(__NetBSD__)
2066 #include <signal.h>
2067 #include <unistd.h>
2069 static void
2070 __gnat_error_handler (int sig)
2072 struct Exception_Data *exception;
2073 const char *msg;
2075 switch(sig)
2077 case SIGFPE:
2078 exception = &constraint_error;
2079 msg = "SIGFPE";
2080 break;
2081 case SIGILL:
2082 exception = &constraint_error;
2083 msg = "SIGILL";
2084 break;
2085 case SIGSEGV:
2086 exception = &storage_error;
2087 msg = "stack overflow or erroneous memory access";
2088 break;
2089 case SIGBUS:
2090 exception = &constraint_error;
2091 msg = "SIGBUS";
2092 break;
2093 default:
2094 exception = &program_error;
2095 msg = "unhandled signal";
2098 Raise_From_Signal_Handler(exception, msg);
2101 void
2102 __gnat_install_handler(void)
2104 struct sigaction act;
2106 act.sa_handler = __gnat_error_handler;
2107 act.sa_flags = SA_NODEFER | SA_RESTART;
2108 sigemptyset (&act.sa_mask);
2110 /* Do not install handlers if interrupt state is "System". */
2111 if (__gnat_get_interrupt_state (SIGFPE) != 's')
2112 sigaction (SIGFPE, &act, NULL);
2113 if (__gnat_get_interrupt_state (SIGILL) != 's')
2114 sigaction (SIGILL, &act, NULL);
2115 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
2116 sigaction (SIGSEGV, &act, NULL);
2117 if (__gnat_get_interrupt_state (SIGBUS) != 's')
2118 sigaction (SIGBUS, &act, NULL);
2120 __gnat_handler_installed = 1;
2123 /*******************/
2124 /* OpenBSD Section */
2125 /*******************/
2127 #elif defined(__OpenBSD__)
2129 #include <signal.h>
2130 #include <unistd.h>
2132 static void
2133 __gnat_error_handler (int sig)
2135 struct Exception_Data *exception;
2136 const char *msg;
2138 switch(sig)
2140 case SIGFPE:
2141 exception = &constraint_error;
2142 msg = "SIGFPE";
2143 break;
2144 case SIGILL:
2145 exception = &constraint_error;
2146 msg = "SIGILL";
2147 break;
2148 case SIGSEGV:
2149 exception = &storage_error;
2150 msg = "stack overflow or erroneous memory access";
2151 break;
2152 case SIGBUS:
2153 exception = &constraint_error;
2154 msg = "SIGBUS";
2155 break;
2156 default:
2157 exception = &program_error;
2158 msg = "unhandled signal";
2161 Raise_From_Signal_Handler(exception, msg);
2164 void
2165 __gnat_install_handler(void)
2167 struct sigaction act;
2169 act.sa_handler = __gnat_error_handler;
2170 act.sa_flags = SA_NODEFER | SA_RESTART;
2171 sigemptyset (&act.sa_mask);
2173 /* Do not install handlers if interrupt state is "System" */
2174 if (__gnat_get_interrupt_state (SIGFPE) != 's')
2175 sigaction (SIGFPE, &act, NULL);
2176 if (__gnat_get_interrupt_state (SIGILL) != 's')
2177 sigaction (SIGILL, &act, NULL);
2178 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
2179 sigaction (SIGSEGV, &act, NULL);
2180 if (__gnat_get_interrupt_state (SIGBUS) != 's')
2181 sigaction (SIGBUS, &act, NULL);
2183 __gnat_handler_installed = 1;
2186 /******************/
2187 /* Darwin Section */
2188 /******************/
2190 #elif defined(__APPLE__)
2192 #include <signal.h>
2193 #include <stdlib.h>
2194 #include <sys/syscall.h>
2195 #include <sys/sysctl.h>
2196 #include <mach/mach_vm.h>
2197 #include <mach/mach_init.h>
2198 #include <mach/vm_statistics.h>
2200 /* This must be in keeping with System.OS_Interface.Alternate_Stack_Size. */
2201 char __gnat_alternate_stack[32 * 1024]; /* 1 * MINSIGSTKSZ */
2203 /* Defined in xnu unix_signal.c.
2204 Tell the kernel to re-use alt stack when delivering a signal. */
2205 #define UC_RESET_ALT_STACK 0x80000000
2207 /* Return true if ADDR is within a stack guard area. */
2208 static int
2209 __gnat_is_stack_guard (mach_vm_address_t addr)
2211 kern_return_t kret;
2212 vm_region_submap_info_data_64_t info;
2213 mach_vm_address_t start;
2214 mach_vm_size_t size;
2215 natural_t depth;
2216 mach_msg_type_number_t count;
2218 count = VM_REGION_SUBMAP_INFO_COUNT_64;
2219 start = addr;
2220 size = -1;
2221 depth = 9999;
2222 kret = mach_vm_region_recurse (mach_task_self (), &start, &size, &depth,
2223 (vm_region_recurse_info_t) &info, &count);
2224 if (kret == KERN_SUCCESS
2225 && addr >= start && addr < (start + size)
2226 && info.protection == VM_PROT_NONE
2227 && info.user_tag == VM_MEMORY_STACK)
2228 return 1;
2229 return 0;
2232 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
2234 #if defined (__x86_64__)
2235 static int
2236 __darwin_major_version (void)
2238 static int cache = -1;
2239 if (cache < 0)
2241 int mib[2] = {CTL_KERN, KERN_OSRELEASE};
2242 size_t len;
2244 /* Find out how big the buffer needs to be (and set cache to 0
2245 on failure). */
2246 if (sysctl (mib, 2, NULL, &len, NULL, 0) == 0)
2248 char release[len];
2249 sysctl (mib, 2, release, &len, NULL, 0);
2250 /* Darwin releases are of the form L.M.N where L is the major
2251 version, so strtol will return L. */
2252 cache = (int) strtol (release, NULL, 10);
2254 else
2256 cache = 0;
2259 return cache;
2261 #endif
2263 void
2264 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED,
2265 void *ucontext ATTRIBUTE_UNUSED)
2267 #if defined (__x86_64__)
2268 if (__darwin_major_version () < 12)
2270 /* Work around radar #10302855, where the unwinders (libunwind or
2271 libgcc_s depending on the system revision) and the DWARF unwind
2272 data for sigtramp have different ideas about register numbering,
2273 causing rbx and rdx to be transposed. */
2274 ucontext_t *uc = (ucontext_t *)ucontext;
2275 unsigned long t = uc->uc_mcontext->__ss.__rbx;
2277 uc->uc_mcontext->__ss.__rbx = uc->uc_mcontext->__ss.__rdx;
2278 uc->uc_mcontext->__ss.__rdx = t;
2280 #endif
2283 static void
2284 __gnat_error_handler (int sig, siginfo_t *si, void *ucontext)
2286 struct Exception_Data *exception;
2287 const char *msg;
2289 __gnat_adjust_context_for_raise (sig, ucontext);
2291 switch (sig)
2293 case SIGSEGV:
2294 case SIGBUS:
2295 if (__gnat_is_stack_guard ((unsigned long)si->si_addr))
2297 exception = &storage_error;
2298 msg = "stack overflow";
2300 else
2302 exception = &constraint_error;
2303 msg = "erroneous memory access";
2305 /* Reset the use of alt stack, so that the alt stack will be used
2306 for the next signal delivery.
2307 The stack can't be used in case of stack checking. */
2308 syscall (SYS_sigreturn, NULL, UC_RESET_ALT_STACK);
2309 break;
2311 case SIGFPE:
2312 exception = &constraint_error;
2313 msg = "SIGFPE";
2314 break;
2316 default:
2317 exception = &program_error;
2318 msg = "unhandled signal";
2321 Raise_From_Signal_Handler (exception, msg);
2324 void
2325 __gnat_install_handler (void)
2327 struct sigaction act;
2329 /* Set up signal handler to map synchronous signals to appropriate
2330 exceptions. Make sure that the handler isn't interrupted by another
2331 signal that might cause a scheduling event! Also setup an alternate
2332 stack region for the handler execution so that stack overflows can be
2333 handled properly, avoiding a SEGV generation from stack usage by the
2334 handler itself (and it is required by Darwin). */
2336 stack_t stack;
2337 stack.ss_sp = __gnat_alternate_stack;
2338 stack.ss_size = sizeof (__gnat_alternate_stack);
2339 stack.ss_flags = 0;
2340 sigaltstack (&stack, NULL);
2342 act.sa_flags = SA_NODEFER | SA_RESTART | SA_SIGINFO;
2343 act.sa_sigaction = __gnat_error_handler;
2344 sigemptyset (&act.sa_mask);
2346 /* Do not install handlers if interrupt state is "System". */
2347 if (__gnat_get_interrupt_state (SIGABRT) != 's')
2348 sigaction (SIGABRT, &act, NULL);
2349 if (__gnat_get_interrupt_state (SIGFPE) != 's')
2350 sigaction (SIGFPE, &act, NULL);
2351 if (__gnat_get_interrupt_state (SIGILL) != 's')
2352 sigaction (SIGILL, &act, NULL);
2354 act.sa_flags |= SA_ONSTACK;
2355 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
2356 sigaction (SIGSEGV, &act, NULL);
2357 if (__gnat_get_interrupt_state (SIGBUS) != 's')
2358 sigaction (SIGBUS, &act, NULL);
2360 __gnat_handler_installed = 1;
2363 #elif defined(__ANDROID__)
2365 /*******************/
2366 /* Android Section */
2367 /*******************/
2369 #include <signal.h>
2370 #include <stdlib.h>
2372 static void
2373 __gnat_error_handler (int sig,
2374 siginfo_t *si ATTRIBUTE_UNUSED,
2375 void *ucontext ATTRIBUTE_UNUSED)
2377 struct Exception_Data *exception;
2378 const char *msg;
2380 switch (sig)
2382 case SIGSEGV:
2383 exception = &storage_error;
2384 msg = "stack overflow or erroneous memory access";
2385 break;
2387 case SIGBUS:
2388 exception = &constraint_error;
2389 msg = "SIGBUS";
2390 break;
2392 case SIGFPE:
2393 exception = &constraint_error;
2394 msg = "SIGFPE";
2395 break;
2397 default:
2398 exception = &program_error;
2399 msg = "unhandled signal";
2402 Raise_From_Signal_Handler (exception, msg);
2405 /* This must be in keeping with System.OS_Interface.Alternate_Stack_Size. */
2406 char __gnat_alternate_stack[16 * 1024];
2408 void
2409 __gnat_install_handler (void)
2411 struct sigaction act;
2413 /* Set up signal handler to map synchronous signals to appropriate
2414 exceptions. Make sure that the handler isn't interrupted by another
2415 signal that might cause a scheduling event! Also setup an alternate
2416 stack region for the handler execution so that stack overflows can be
2417 handled properly, avoiding a SEGV generation from stack usage by the
2418 handler itself. */
2420 stack_t stack;
2421 stack.ss_sp = __gnat_alternate_stack;
2422 stack.ss_size = sizeof (__gnat_alternate_stack);
2423 stack.ss_flags = 0;
2424 sigaltstack (&stack, NULL);
2426 act.sa_sigaction = __gnat_error_handler;
2427 act.sa_flags = SA_NODEFER | SA_RESTART | SA_SIGINFO;
2428 sigemptyset (&act.sa_mask);
2430 sigaction (SIGABRT, &act, NULL);
2431 sigaction (SIGFPE, &act, NULL);
2432 sigaction (SIGILL, &act, NULL);
2433 sigaction (SIGBUS, &act, NULL);
2434 act.sa_flags |= SA_ONSTACK;
2435 sigaction (SIGSEGV, &act, NULL);
2437 __gnat_handler_installed = 1;
2440 #else
2442 /* For all other versions of GNAT, the handler does nothing. */
2444 /*******************/
2445 /* Default Section */
2446 /*******************/
2448 void
2449 __gnat_install_handler (void)
2451 __gnat_handler_installed = 1;
2454 #endif
2456 /*********************/
2457 /* __gnat_init_float */
2458 /*********************/
2460 /* This routine is called as each process thread is created, for possible
2461 initialization of the FP processor. This version is used under INTERIX
2462 and WIN32. */
2464 #if defined (_WIN32) || defined (__INTERIX) \
2465 || defined (__Lynx__) || defined(__NetBSD__) || defined(__FreeBSD__) \
2466 || defined (__OpenBSD__)
2468 #define HAVE_GNAT_INIT_FLOAT
2470 void
2471 __gnat_init_float (void)
2473 #if defined (__i386__) || defined (i386) || defined (__x86_64)
2475 /* This is used to properly initialize the FPU on an x86 for each
2476 process thread. */
2478 asm ("finit");
2480 #endif /* Defined __i386__ */
2482 #endif
2484 #ifndef HAVE_GNAT_INIT_FLOAT
2486 /* All targets without a specific __gnat_init_float will use an empty one. */
2487 void
2488 __gnat_init_float (void)
2491 #endif
2493 /***********************************/
2494 /* __gnat_adjust_context_for_raise */
2495 /***********************************/
2497 #ifndef HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
2499 /* All targets without a specific version will use an empty one. */
2501 /* Given UCONTEXT a pointer to a context structure received by a signal
2502 handler for SIGNO, perform the necessary adjustments to let the handler
2503 raise an exception. Calls to this routine are not conditioned by the
2504 propagation scheme in use. */
2506 void
2507 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED,
2508 void *ucontext ATTRIBUTE_UNUSED)
2510 /* We used to compensate here for the raised from call vs raised from signal
2511 exception discrepancy with the GCC ZCX scheme, but this now can be dealt
2512 with generically in the unwinder (see GCC PR other/26208). This however
2513 requires the use of the _Unwind_GetIPInfo routine in raise-gcc.c, which
2514 is predicated on the definition of HAVE_GETIPINFO at compile time. Only
2515 the VMS ports still do the compensation described in the few lines below.
2517 *** Call vs signal exception discrepancy with GCC ZCX scheme ***
2519 The GCC unwinder expects to be dealing with call return addresses, since
2520 this is the "nominal" case of what we retrieve while unwinding a regular
2521 call chain.
2523 To evaluate if a handler applies at some point identified by a return
2524 address, the propagation engine needs to determine what region the
2525 corresponding call instruction pertains to. Because the return address
2526 may not be attached to the same region as the call, the unwinder always
2527 subtracts "some" amount from a return address to search the region
2528 tables, amount chosen to ensure that the resulting address is inside the
2529 call instruction.
2531 When we raise an exception from a signal handler, e.g. to transform a
2532 SIGSEGV into Storage_Error, things need to appear as if the signal
2533 handler had been "called" by the instruction which triggered the signal,
2534 so that exception handlers that apply there are considered. What the
2535 unwinder will retrieve as the return address from the signal handler is
2536 what it will find as the faulting instruction address in the signal
2537 context pushed by the kernel. Leaving this address untouched looses, if
2538 the triggering instruction happens to be the very first of a region, as
2539 the later adjustments performed by the unwinder would yield an address
2540 outside that region. We need to compensate for the unwinder adjustments
2541 at some point, and this is what this routine is expected to do.
2543 signo is passed because on some targets for some signals the PC in
2544 context points to the instruction after the faulting one, in which case
2545 the unwinder adjustment is still desired. */
2548 #endif
2550 #ifdef __cplusplus
2552 #endif