PR target/58115
[official-gcc.git] / gcc / ada / init.c
blob7f8b3a3e58c53711a1c29a465be16611eaec2c88
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__)
457 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
459 void
460 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED, void *ucontext)
462 mcontext_t *mcontext = &((ucontext_t *) ucontext)->uc_mcontext;
464 /* On the i386 and x86-64 architectures, stack checking is performed by
465 means of probes with moving stack pointer, that is to say the probed
466 address is always the value of the stack pointer. Upon hitting the
467 guard page, the stack pointer therefore points to an inaccessible
468 address and an alternate signal stack is needed to run the handler.
469 But there is an additional twist: on these architectures, the EH
470 return code writes the address of the handler at the target CFA's
471 value on the stack before doing the jump. As a consequence, if
472 there is an active handler in the frame whose stack has overflowed,
473 the stack pointer must nevertheless point to an accessible address
474 by the time the EH return is executed.
476 We therefore adjust the saved value of the stack pointer by the size
477 of one page + a small dope of 4 words, in order to make sure that it
478 points to an accessible address in case it's used as the target CFA.
479 The stack checking code guarantees that this address is unused by the
480 time this happens. */
482 #if defined (i386)
483 unsigned long *pc = (unsigned long *)mcontext->gregs[REG_EIP];
484 /* The pattern is "orl $0x0,(%esp)" for a probe in 32-bit mode. */
485 if (signo == SIGSEGV && pc && *pc == 0x00240c83)
486 mcontext->gregs[REG_ESP] += 4096 + 4 * sizeof (unsigned long);
487 #elif defined (__x86_64__)
488 unsigned long long *pc = (unsigned long long *)mcontext->gregs[REG_RIP];
489 if (signo == SIGSEGV && pc
490 /* The pattern is "orq $0x0,(%rsp)" for a probe in 64-bit mode. */
491 && ((*pc & 0xffffffffffLL) == 0x00240c8348LL
492 /* The pattern may also be "orl $0x0,(%esp)" for a probe in
493 x32 mode. */
494 || (*pc & 0xffffffffLL) == 0x00240c83LL))
495 mcontext->gregs[REG_RSP] += 4096 + 4 * sizeof (unsigned long);
496 #elif defined (__ia64__)
497 /* ??? The IA-64 unwinder doesn't compensate for signals. */
498 mcontext->sc_ip++;
499 #endif
502 #endif
504 static void
505 __gnat_error_handler (int sig, siginfo_t *si ATTRIBUTE_UNUSED, void *ucontext)
507 struct Exception_Data *exception;
508 const char *msg;
510 /* Adjusting is required for every fault context, so adjust for this one
511 now, before we possibly trigger a recursive fault below. */
512 __gnat_adjust_context_for_raise (sig, ucontext);
514 switch (sig)
516 case SIGSEGV:
517 /* Here we would like a discrimination test to see whether the page
518 before the faulting address is accessible. Unfortunately, Linux
519 seems to have no way of giving us the faulting address.
521 In old versions of init.c, we had a test of the page before the
522 stack pointer:
524 ((volatile char *)
525 ((long) si->esp_at_signal & - getpagesize ()))[getpagesize ()];
527 but that's wrong since it tests the stack pointer location and the
528 stack probing code may not move it until all probes succeed.
530 For now we simply do not attempt any discrimination at all. Note
531 that this is quite acceptable, since a "real" SIGSEGV can only
532 occur as the result of an erroneous program. */
533 exception = &storage_error;
534 msg = "stack overflow or erroneous memory access";
535 break;
537 case SIGBUS:
538 exception = &storage_error;
539 msg = "SIGBUS: possible stack overflow";
540 break;
542 case SIGFPE:
543 exception = &constraint_error;
544 msg = "SIGFPE";
545 break;
547 default:
548 exception = &program_error;
549 msg = "unhandled signal";
552 Raise_From_Signal_Handler (exception, msg);
555 #if defined (i386) || defined (__x86_64__) || defined (__powerpc__)
556 /* This must be in keeping with System.OS_Interface.Alternate_Stack_Size. */
557 char __gnat_alternate_stack[16 * 1024]; /* 2 * SIGSTKSZ */
558 #endif
560 #ifdef __XENO__
561 #include <sys/mman.h>
562 #include <native/task.h>
564 RT_TASK main_task;
565 #endif
567 void
568 __gnat_install_handler (void)
570 struct sigaction act;
572 #ifdef __XENO__
573 int prio;
575 if (__gl_main_priority == -1)
576 prio = 49;
577 else
578 prio = __gl_main_priority;
580 /* Avoid memory swapping for this program */
582 mlockall (MCL_CURRENT|MCL_FUTURE);
584 /* Turn the current Linux task into a native Xenomai task */
586 rt_task_shadow (&main_task, "environment_task", prio, T_FPU);
587 #endif
589 /* Set up signal handler to map synchronous signals to appropriate
590 exceptions. Make sure that the handler isn't interrupted by another
591 signal that might cause a scheduling event! Also setup an alternate
592 stack region for the handler execution so that stack overflows can be
593 handled properly, avoiding a SEGV generation from stack usage by the
594 handler itself. */
596 act.sa_sigaction = __gnat_error_handler;
597 act.sa_flags = SA_NODEFER | SA_RESTART | SA_SIGINFO;
598 sigemptyset (&act.sa_mask);
600 /* Do not install handlers if interrupt state is "System". */
601 if (__gnat_get_interrupt_state (SIGABRT) != 's')
602 sigaction (SIGABRT, &act, NULL);
603 if (__gnat_get_interrupt_state (SIGFPE) != 's')
604 sigaction (SIGFPE, &act, NULL);
605 if (__gnat_get_interrupt_state (SIGILL) != 's')
606 sigaction (SIGILL, &act, NULL);
607 if (__gnat_get_interrupt_state (SIGBUS) != 's')
608 sigaction (SIGBUS, &act, NULL);
609 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
611 #if defined (i386) || defined (__x86_64__) || defined (__powerpc__)
612 /* Setup an alternate stack region for the handler execution so that
613 stack overflows can be handled properly, avoiding a SEGV generation
614 from stack usage by the handler itself. */
615 stack_t stack;
617 stack.ss_sp = __gnat_alternate_stack;
618 stack.ss_size = sizeof (__gnat_alternate_stack);
619 stack.ss_flags = 0;
620 sigaltstack (&stack, NULL);
622 act.sa_flags |= SA_ONSTACK;
623 #endif
624 sigaction (SIGSEGV, &act, NULL);
627 __gnat_handler_installed = 1;
630 /*******************/
631 /* LynxOS Section */
632 /*******************/
634 #elif defined (__Lynx__)
636 #include <signal.h>
637 #include <unistd.h>
639 static void
640 __gnat_error_handler (int sig)
642 struct Exception_Data *exception;
643 const char *msg;
645 switch(sig)
647 case SIGFPE:
648 exception = &constraint_error;
649 msg = "SIGFPE";
650 break;
651 case SIGILL:
652 exception = &constraint_error;
653 msg = "SIGILL";
654 break;
655 case SIGSEGV:
656 exception = &storage_error;
657 msg = "stack overflow or erroneous memory access";
658 break;
659 case SIGBUS:
660 exception = &constraint_error;
661 msg = "SIGBUS";
662 break;
663 default:
664 exception = &program_error;
665 msg = "unhandled signal";
668 Raise_From_Signal_Handler(exception, msg);
671 void
672 __gnat_install_handler(void)
674 struct sigaction act;
676 act.sa_handler = __gnat_error_handler;
677 act.sa_flags = 0x0;
678 sigemptyset (&act.sa_mask);
680 /* Do not install handlers if interrupt state is "System". */
681 if (__gnat_get_interrupt_state (SIGFPE) != 's')
682 sigaction (SIGFPE, &act, NULL);
683 if (__gnat_get_interrupt_state (SIGILL) != 's')
684 sigaction (SIGILL, &act, NULL);
685 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
686 sigaction (SIGSEGV, &act, NULL);
687 if (__gnat_get_interrupt_state (SIGBUS) != 's')
688 sigaction (SIGBUS, &act, NULL);
690 __gnat_handler_installed = 1;
693 /*******************/
694 /* Solaris Section */
695 /*******************/
697 #elif defined (sun) && defined (__SVR4) && !defined (__vxworks)
699 #include <signal.h>
700 #include <siginfo.h>
701 #include <sys/ucontext.h>
702 #include <sys/regset.h>
704 static void
705 __gnat_error_handler (int sig, siginfo_t *si, void *ucontext ATTRIBUTE_UNUSED)
707 struct Exception_Data *exception;
708 static int recurse = 0;
709 const char *msg;
711 switch (sig)
713 case SIGSEGV:
714 /* If the problem was permissions, this is a constraint error.
715 Likewise if the failing address isn't maximally aligned or if
716 we've recursed.
718 ??? Using a static variable here isn't task-safe, but it's
719 much too hard to do anything else and we're just determining
720 which exception to raise. */
721 if (si->si_code == SEGV_ACCERR
722 || (long) si->si_addr == 0
723 || (((long) si->si_addr) & 3) != 0
724 || recurse)
726 exception = &constraint_error;
727 msg = "SIGSEGV";
729 else
731 /* See if the page before the faulting page is accessible. Do that
732 by trying to access it. We'd like to simply try to access
733 4096 + the faulting address, but it's not guaranteed to be
734 the actual address, just to be on the same page. */
735 recurse++;
736 ((volatile char *)
737 ((long) si->si_addr & - getpagesize ()))[getpagesize ()];
738 exception = &storage_error;
739 msg = "stack overflow or erroneous memory access";
741 break;
743 case SIGBUS:
744 exception = &program_error;
745 msg = "SIGBUS";
746 break;
748 case SIGFPE:
749 exception = &constraint_error;
750 msg = "SIGFPE";
751 break;
753 default:
754 exception = &program_error;
755 msg = "unhandled signal";
758 recurse = 0;
759 Raise_From_Signal_Handler (exception, msg);
762 void
763 __gnat_install_handler (void)
765 struct sigaction act;
767 /* Set up signal handler to map synchronous signals to appropriate
768 exceptions. Make sure that the handler isn't interrupted by another
769 signal that might cause a scheduling event! */
771 act.sa_sigaction = __gnat_error_handler;
772 act.sa_flags = SA_NODEFER | SA_RESTART | SA_SIGINFO;
773 sigemptyset (&act.sa_mask);
775 /* Do not install handlers if interrupt state is "System". */
776 if (__gnat_get_interrupt_state (SIGABRT) != 's')
777 sigaction (SIGABRT, &act, NULL);
778 if (__gnat_get_interrupt_state (SIGFPE) != 's')
779 sigaction (SIGFPE, &act, NULL);
780 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
781 sigaction (SIGSEGV, &act, NULL);
782 if (__gnat_get_interrupt_state (SIGBUS) != 's')
783 sigaction (SIGBUS, &act, NULL);
785 __gnat_handler_installed = 1;
788 /***************/
789 /* VMS Section */
790 /***************/
792 #elif defined (VMS)
794 /* Routine called from binder to override default feature values. */
795 void __gnat_set_features (void);
796 int __gnat_features_set = 0;
797 void (*__gnat_ctrl_c_handler) (void) = 0;
799 #ifdef __IA64
800 #define lib_get_curr_invo_context LIB$I64_GET_CURR_INVO_CONTEXT
801 #define lib_get_prev_invo_context LIB$I64_GET_PREV_INVO_CONTEXT
802 #define lib_get_invo_handle LIB$I64_GET_INVO_HANDLE
803 #else
804 #define lib_get_curr_invo_context LIB$GET_CURR_INVO_CONTEXT
805 #define lib_get_prev_invo_context LIB$GET_PREV_INVO_CONTEXT
806 #define lib_get_invo_handle LIB$GET_INVO_HANDLE
807 #endif
809 /* Masks for facility identification. */
810 #define FAC_MASK 0x0fff0000
811 #define DECADA_M_FACILITY 0x00310000
813 /* Define macro symbols for the VMS conditions that become Ada exceptions.
814 It would be better to just include <ssdef.h> */
816 #define SS$_CONTINUE 1
817 #define SS$_ACCVIO 12
818 #define SS$_HPARITH 1284
819 #define SS$_INTDIV 1156
820 #define SS$_STKOVF 1364
821 #define SS$_CONTROLC 1617
822 #define SS$_RESIGNAL 2328
824 #define MTH$_FLOOVEMAT 1475268 /* Some ACVC_21 CXA tests */
826 /* The following codes must be resignalled, and not handled here. */
828 /* These codes are in standard message libraries. */
829 extern int C$_SIGKILL;
830 extern int C$_SIGINT;
831 extern int SS$_DEBUG;
832 extern int LIB$_KEYNOTFOU;
833 extern int LIB$_ACTIMAGE;
835 /* These codes are non standard, which is to say the author is
836 not sure if they are defined in the standard message libraries
837 so keep them as macros for now. */
838 #define RDB$_STREAM_EOF 20480426
839 #define FDL$_UNPRIKW 11829410
840 #define CMA$_EXIT_THREAD 4227492
842 struct cond_sigargs
844 unsigned int sigarg;
845 unsigned int sigargval;
848 struct cond_subtests
850 unsigned int num;
851 const struct cond_sigargs sigargs[];
854 struct cond_except
856 unsigned int cond;
857 const struct Exception_Data *except;
858 unsigned int needs_adjust; /* 1 = adjust PC, 0 = no adjust */
859 const struct cond_subtests *subtests;
862 struct descriptor_s
864 unsigned short len, mbz;
865 __char_ptr32 adr;
868 /* Conditions that don't have an Ada exception counterpart must raise
869 Non_Ada_Error. Since this is defined in s-auxdec, it should only be
870 referenced by user programs, not the compiler or tools. Hence the
871 #ifdef IN_RTS. */
873 #ifdef IN_RTS
875 #define Status_Error ada__io_exceptions__status_error
876 extern struct Exception_Data Status_Error;
878 #define Mode_Error ada__io_exceptions__mode_error
879 extern struct Exception_Data Mode_Error;
881 #define Name_Error ada__io_exceptions__name_error
882 extern struct Exception_Data Name_Error;
884 #define Use_Error ada__io_exceptions__use_error
885 extern struct Exception_Data Use_Error;
887 #define Device_Error ada__io_exceptions__device_error
888 extern struct Exception_Data Device_Error;
890 #define End_Error ada__io_exceptions__end_error
891 extern struct Exception_Data End_Error;
893 #define Data_Error ada__io_exceptions__data_error
894 extern struct Exception_Data Data_Error;
896 #define Layout_Error ada__io_exceptions__layout_error
897 extern struct Exception_Data Layout_Error;
899 #define Non_Ada_Error system__aux_dec__non_ada_error
900 extern struct Exception_Data Non_Ada_Error;
902 #define Coded_Exception system__vms_exception_table__coded_exception
903 extern struct Exception_Data *Coded_Exception (void *);
905 #define Base_Code_In system__vms_exception_table__base_code_in
906 extern void *Base_Code_In (void *);
908 /* DEC Ada exceptions are not defined in a header file, so they
909 must be declared. */
911 #define ADA$_ALREADY_OPEN 0x0031a594
912 #define ADA$_CONSTRAINT_ERRO 0x00318324
913 #define ADA$_DATA_ERROR 0x003192c4
914 #define ADA$_DEVICE_ERROR 0x003195e4
915 #define ADA$_END_ERROR 0x00319904
916 #define ADA$_FAC_MODE_MISMAT 0x0031a8b3
917 #define ADA$_IOSYSFAILED 0x0031af04
918 #define ADA$_KEYSIZERR 0x0031aa3c
919 #define ADA$_KEY_MISMATCH 0x0031a8e3
920 #define ADA$_LAYOUT_ERROR 0x00319c24
921 #define ADA$_LINEXCMRS 0x0031a8f3
922 #define ADA$_MAXLINEXC 0x0031a8eb
923 #define ADA$_MODE_ERROR 0x00319f44
924 #define ADA$_MRN_MISMATCH 0x0031a8db
925 #define ADA$_MRS_MISMATCH 0x0031a8d3
926 #define ADA$_NAME_ERROR 0x0031a264
927 #define ADA$_NOT_OPEN 0x0031a58c
928 #define ADA$_ORG_MISMATCH 0x0031a8bb
929 #define ADA$_PROGRAM_ERROR 0x00318964
930 #define ADA$_RAT_MISMATCH 0x0031a8cb
931 #define ADA$_RFM_MISMATCH 0x0031a8c3
932 #define ADA$_STAOVF 0x00318cac
933 #define ADA$_STATUS_ERROR 0x0031a584
934 #define ADA$_STORAGE_ERROR 0x00318c84
935 #define ADA$_UNSUPPORTED 0x0031a8ab
936 #define ADA$_USE_ERROR 0x0031a8a4
938 /* DEC Ada specific conditions. */
939 static const struct cond_except dec_ada_cond_except_table [] =
941 {ADA$_PROGRAM_ERROR, &program_error, 0, 0},
942 {ADA$_USE_ERROR, &Use_Error, 0, 0},
943 {ADA$_KEYSIZERR, &program_error, 0, 0},
944 {ADA$_STAOVF, &storage_error, 0, 0},
945 {ADA$_CONSTRAINT_ERRO, &constraint_error, 0, 0},
946 {ADA$_IOSYSFAILED, &Device_Error, 0, 0},
947 {ADA$_LAYOUT_ERROR, &Layout_Error, 0, 0},
948 {ADA$_STORAGE_ERROR, &storage_error, 0, 0},
949 {ADA$_DATA_ERROR, &Data_Error, 0, 0},
950 {ADA$_DEVICE_ERROR, &Device_Error, 0, 0},
951 {ADA$_END_ERROR, &End_Error, 0, 0},
952 {ADA$_MODE_ERROR, &Mode_Error, 0, 0},
953 {ADA$_NAME_ERROR, &Name_Error, 0, 0},
954 {ADA$_STATUS_ERROR, &Status_Error, 0, 0},
955 {ADA$_NOT_OPEN, &Use_Error, 0, 0},
956 {ADA$_ALREADY_OPEN, &Use_Error, 0, 0},
957 {ADA$_USE_ERROR, &Use_Error, 0, 0},
958 {ADA$_UNSUPPORTED, &Use_Error, 0, 0},
959 {ADA$_FAC_MODE_MISMAT, &Use_Error, 0, 0},
960 {ADA$_ORG_MISMATCH, &Use_Error, 0, 0},
961 {ADA$_RFM_MISMATCH, &Use_Error, 0, 0},
962 {ADA$_RAT_MISMATCH, &Use_Error, 0, 0},
963 {ADA$_MRS_MISMATCH, &Use_Error, 0, 0},
964 {ADA$_MRN_MISMATCH, &Use_Error, 0, 0},
965 {ADA$_KEY_MISMATCH, &Use_Error, 0, 0},
966 {ADA$_MAXLINEXC, &constraint_error, 0, 0},
967 {ADA$_LINEXCMRS, &constraint_error, 0, 0},
969 #if 0
970 /* Already handled by a pragma Import_Exception
971 in Aux_IO_Exceptions */
972 {ADA$_LOCK_ERROR, &Lock_Error, 0, 0},
973 {ADA$_EXISTENCE_ERROR, &Existence_Error, 0, 0},
974 {ADA$_KEY_ERROR, &Key_Error, 0, 0},
975 #endif
977 {0, 0, 0, 0}
980 #endif /* IN_RTS */
982 /* Non-DEC Ada specific conditions that map to Ada exceptions. */
984 /* Subtest for ACCVIO Constraint_Error, kept for compatibility,
985 in hindsight should have just made ACCVIO == Storage_Error. */
986 #define ACCVIO_VIRTUAL_ADDR 3
987 static const struct cond_subtests accvio_c_e =
988 {1, /* number of subtests below */
990 { ACCVIO_VIRTUAL_ADDR, 0 }
994 /* Macro flag to adjust PC which gets off by one for some conditions,
995 not sure if this is reliably true, PC could be off by more for
996 HPARITH for example, unless a trapb is inserted. */
997 #define NEEDS_ADJUST 1
999 static const struct cond_except system_cond_except_table [] =
1001 {MTH$_FLOOVEMAT, &constraint_error, 0, 0},
1002 {SS$_INTDIV, &constraint_error, 0, 0},
1003 {SS$_HPARITH, &constraint_error, NEEDS_ADJUST, 0},
1004 {SS$_ACCVIO, &constraint_error, NEEDS_ADJUST, &accvio_c_e},
1005 {SS$_ACCVIO, &storage_error, NEEDS_ADJUST, 0},
1006 {SS$_STKOVF, &storage_error, NEEDS_ADJUST, 0},
1007 {0, 0, 0, 0}
1010 /* To deal with VMS conditions and their mapping to Ada exceptions,
1011 the __gnat_error_handler routine below is installed as an exception
1012 vector having precedence over DEC frame handlers. Some conditions
1013 still need to be handled by such handlers, however, in which case
1014 __gnat_error_handler needs to return SS$_RESIGNAL. Consider for
1015 instance the use of a third party library compiled with DECAda and
1016 performing its own exception handling internally.
1018 To allow some user-level flexibility, which conditions should be
1019 resignaled is controlled by a predicate function, provided with the
1020 condition value and returning a boolean indication stating whether
1021 this condition should be resignaled or not.
1023 That predicate function is called indirectly, via a function pointer,
1024 by __gnat_error_handler, and changing that pointer is allowed to the
1025 user code by way of the __gnat_set_resignal_predicate interface.
1027 The user level function may then implement what it likes, including
1028 for instance the maintenance of a dynamic data structure if the set
1029 of to be resignalled conditions has to change over the program's
1030 lifetime.
1032 ??? This is not a perfect solution to deal with the possible
1033 interactions between the GNAT and the DECAda exception handling
1034 models and better (more general) schemes are studied. This is so
1035 just provided as a convenient workaround in the meantime, and
1036 should be use with caution since the implementation has been kept
1037 very simple. */
1039 typedef int resignal_predicate (int code);
1041 static const int * const cond_resignal_table [] =
1043 &C$_SIGKILL,
1044 (int *)CMA$_EXIT_THREAD,
1045 &SS$_DEBUG,
1046 &LIB$_KEYNOTFOU,
1047 &LIB$_ACTIMAGE,
1048 (int *) RDB$_STREAM_EOF,
1049 (int *) FDL$_UNPRIKW,
1053 static const int facility_resignal_table [] =
1055 0x1380000, /* RDB */
1056 0x2220000, /* SQL */
1060 /* Default GNAT predicate for resignaling conditions. */
1062 static int
1063 __gnat_default_resignal_p (int code)
1065 int i, iexcept;
1067 for (i = 0; facility_resignal_table [i]; i++)
1068 if ((code & FAC_MASK) == facility_resignal_table [i])
1069 return 1;
1071 for (i = 0, iexcept = 0;
1072 cond_resignal_table [i]
1073 && !(iexcept = LIB$MATCH_COND (&code, &cond_resignal_table [i]));
1074 i++);
1076 return iexcept;
1079 /* Static pointer to predicate that the __gnat_error_handler exception
1080 vector invokes to determine if it should resignal a condition. */
1082 static resignal_predicate *__gnat_resignal_p = __gnat_default_resignal_p;
1084 /* User interface to change the predicate pointer to PREDICATE. Reset to
1085 the default if PREDICATE is null. */
1087 void
1088 __gnat_set_resignal_predicate (resignal_predicate *predicate)
1090 if (predicate == NULL)
1091 __gnat_resignal_p = __gnat_default_resignal_p;
1092 else
1093 __gnat_resignal_p = predicate;
1096 /* Should match System.Parameters.Default_Exception_Msg_Max_Length. */
1097 #define Default_Exception_Msg_Max_Length 512
1099 /* Action routine for SYS$PUTMSG. There may be multiple
1100 conditions, each with text to be appended to MESSAGE
1101 and separated by line termination. */
1102 static int
1103 copy_msg (struct descriptor_s *msgdesc, char *message)
1105 int len = strlen (message);
1106 int copy_len;
1108 /* Check for buffer overflow and skip. */
1109 if (len > 0 && len <= Default_Exception_Msg_Max_Length - 3)
1111 strcat (message, "\r\n");
1112 len += 2;
1115 /* Check for buffer overflow and truncate if necessary. */
1116 copy_len = (len + msgdesc->len <= Default_Exception_Msg_Max_Length - 1 ?
1117 msgdesc->len :
1118 Default_Exception_Msg_Max_Length - 1 - len);
1119 strncpy (&message [len], msgdesc->adr, copy_len);
1120 message [len + copy_len] = 0;
1122 return 0;
1125 /* Scan TABLE for a match for the condition contained in SIGARGS,
1126 and return the entry, or the empty entry if no match found. */
1127 static const struct cond_except *
1128 scan_conditions ( int *sigargs, const struct cond_except *table [])
1130 int i;
1131 struct cond_except entry;
1133 /* Scan the exception condition table for a match and fetch
1134 the associated GNAT exception pointer. */
1135 for (i = 0; (*table) [i].cond; i++)
1137 unsigned int match = LIB$MATCH_COND (&sigargs [1], &(*table) [i].cond);
1138 const struct cond_subtests *subtests = (*table) [i].subtests;
1140 if (match)
1142 if (!subtests)
1144 return &(*table) [i];
1146 else
1148 unsigned int ii;
1149 int num = (*subtests).num;
1151 /* Perform subtests to differentiate exception. */
1152 for (ii = 0; ii < num; ii++)
1154 unsigned int arg = (*subtests).sigargs [ii].sigarg;
1155 unsigned int argval = (*subtests).sigargs [ii].sigargval;
1157 if (sigargs [arg] != argval)
1159 num = 0;
1160 break;
1164 /* All subtests passed. */
1165 if (num == (*subtests).num)
1166 return &(*table) [i];
1171 /* No match, return the null terminating entry. */
1172 return &(*table) [i];
1175 /* __gnat_handle_vms_condtition is both a frame based handler
1176 for the runtime, and an exception vector for the compiler. */
1177 long
1178 __gnat_handle_vms_condition (int *sigargs, void *mechargs)
1180 struct Exception_Data *exception = 0;
1181 unsigned int needs_adjust = 0;
1182 void *base_code;
1183 struct descriptor_s gnat_facility = {4, 0, "GNAT"};
1184 char message [Default_Exception_Msg_Max_Length];
1186 const char *msg = "";
1188 /* Check for conditions to resignal which aren't effected by pragma
1189 Import_Exception. */
1190 if (__gnat_resignal_p (sigargs [1]))
1191 return SS$_RESIGNAL;
1192 #ifndef IN_RTS
1193 /* toplev.c handles this for compiler. */
1194 if (sigargs [1] == SS$_HPARITH)
1195 return SS$_RESIGNAL;
1196 #endif
1198 #ifdef IN_RTS
1199 /* See if it's an imported exception. Beware that registered exceptions
1200 are bound to their base code, with the severity bits masked off. */
1201 base_code = Base_Code_In ((void *) sigargs[1]);
1202 exception = Coded_Exception (base_code);
1203 #endif
1205 if (exception == 0)
1206 #ifdef IN_RTS
1208 int i;
1209 struct cond_except cond;
1210 const struct cond_except *cond_table;
1211 const struct cond_except *cond_tables [] = {dec_ada_cond_except_table,
1212 system_cond_except_table,
1214 unsigned int ctrlc = SS$_CONTROLC;
1215 unsigned int *sigint = &C$_SIGINT;
1216 int ctrlc_match = LIB$MATCH_COND (&sigargs [1], &ctrlc);
1217 int sigint_match = LIB$MATCH_COND (&sigargs [1], &sigint);
1219 extern int SYS$DCLAST (void (*astadr)(), unsigned long long astprm,
1220 unsigned int acmode);
1222 /* If SS$_CONTROLC has been imported as an exception, it will take
1223 priority over a a Ctrl/C handler. See above. SIGINT has a
1224 different condition value due to it's DECCCRTL roots and it's
1225 the condition that gets raised for a "kill -INT". */
1226 if ((ctrlc_match || sigint_match) && __gnat_ctrl_c_handler)
1228 SYS$DCLAST (__gnat_ctrl_c_handler, 0, 0);
1229 return SS$_CONTINUE;
1232 i = 0;
1233 while ((cond_table = cond_tables[i++]) && !exception)
1235 cond = *scan_conditions (sigargs, &cond_table);
1236 exception = (struct Exception_Data *) cond.except;
1239 if (exception)
1240 needs_adjust = cond.needs_adjust;
1241 else
1242 /* User programs expect Non_Ada_Error to be raised if no match,
1243 reference DEC Ada test CXCONDHAN. */
1244 exception = &Non_Ada_Error;
1246 #else
1248 /* Pretty much everything is just a program error in the compiler */
1249 exception = &program_error;
1251 #endif
1253 message[0] = 0;
1254 /* Subtract PC & PSL fields as per ABI for SYS$PUTMSG. */
1255 sigargs[0] -= 2;
1257 extern int SYS$PUTMSG (void *, int (*)(), void *, unsigned long long);
1259 /* If it was a DEC Ada specific condtiion, make it GNAT otherwise
1260 keep the old facility. */
1261 if (sigargs [1] & FAC_MASK == DECADA_M_FACILITY)
1262 SYS$PUTMSG (sigargs, copy_msg, &gnat_facility,
1263 (unsigned long long ) message);
1264 else
1265 SYS$PUTMSG (sigargs, copy_msg, 0,
1266 (unsigned long long ) message);
1268 /* Add back PC & PSL fields as per ABI for SYS$PUTMSG. */
1269 sigargs[0] += 2;
1270 msg = message;
1272 if (needs_adjust)
1273 __gnat_adjust_context_for_raise (sigargs [1], (void *)mechargs);
1275 Raise_From_Signal_Handler (exception, msg);
1278 #if defined (IN_RTS) && defined (__IA64)
1279 /* Called only from adasigio.b32. This is a band aid to avoid going
1280 through the VMS signal handling code which results in a 0x8000 per
1281 handled exception memory leak in P2 space (see VMS source listing
1282 sys/lis/exception.lis) due to the allocation of working space that
1283 is expected to be deallocated upon return from the condition handler,
1284 which doesn't return in GNAT compiled code. */
1285 void
1286 GNAT$STOP (int *sigargs)
1288 /* Note that there are no mechargs. We rely on the fact that condtions
1289 raised from DEClib I/O do not require an "adjust". Also the count
1290 will be off by 2, since LIB$STOP didn't get a chance to add the
1291 PC and PSL fields, so we bump it so PUTMSG comes out right. */
1292 sigargs [0] += 2;
1293 __gnat_handle_vms_condition (sigargs, 0);
1295 #endif
1297 void
1298 __gnat_install_handler (void)
1300 long prvhnd ATTRIBUTE_UNUSED;
1302 #if !defined (IN_RTS)
1303 extern int SYS$SETEXV (unsigned int vector, int (*addres)(),
1304 unsigned int accmode, void *(*(prvhnd)));
1305 SYS$SETEXV (1, __gnat_handle_vms_condition, 3, &prvhnd);
1306 #endif
1308 __gnat_handler_installed = 1;
1311 /* __gnat_adjust_context_for_raise for Alpha - see comments along with the
1312 default version later in this file. */
1314 #if defined (IN_RTS) && defined (__alpha__)
1316 #include <vms/chfctxdef.h>
1317 #include <vms/chfdef.h>
1319 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
1321 void
1322 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED, void *ucontext)
1324 if (signo == SS$_HPARITH)
1326 /* Sub one to the address of the instruction signaling the condition,
1327 located in the sigargs array. */
1329 CHF$MECH_ARRAY * mechargs = (CHF$MECH_ARRAY *) ucontext;
1330 CHF$SIGNAL_ARRAY * sigargs
1331 = (CHF$SIGNAL_ARRAY *) mechargs->chf$q_mch_sig_addr;
1333 int vcount = sigargs->chf$is_sig_args;
1334 int * pc_slot = & (&sigargs->chf$l_sig_name)[vcount-2];
1336 (*pc_slot)--;
1340 #endif
1342 /* __gnat_adjust_context_for_raise for ia64. */
1344 #if defined (IN_RTS) && defined (__IA64)
1346 #include <vms/chfctxdef.h>
1347 #include <vms/chfdef.h>
1349 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
1351 typedef unsigned long long u64;
1353 void
1354 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED, void *ucontext)
1356 /* Add one to the address of the instruction signaling the condition,
1357 located in the 64bits sigargs array. */
1359 CHF$MECH_ARRAY * mechargs = (CHF$MECH_ARRAY *) ucontext;
1361 CHF64$SIGNAL_ARRAY *chfsig64
1362 = (CHF64$SIGNAL_ARRAY *) mechargs->chf$ph_mch_sig64_addr;
1364 u64 * post_sigarray
1365 = (u64 *)chfsig64 + 1 + chfsig64->chf64$l_sig_args;
1367 u64 * ih_pc_loc = post_sigarray - 2;
1369 (*ih_pc_loc) ++;
1372 #endif
1374 /* Easier interface for LIB$GET_LOGICAL: put the equivalence of NAME into BUF,
1375 always NUL terminated. In case of error or if the result is longer than
1376 LEN (length of BUF) an empty string is written info BUF. */
1378 static void
1379 __gnat_vms_get_logical (const char *name, char *buf, int len)
1381 struct descriptor_s name_desc, result_desc;
1382 int status;
1383 unsigned short rlen;
1385 /* Build the descriptor for NAME. */
1386 name_desc.len = strlen (name);
1387 name_desc.mbz = 0;
1388 name_desc.adr = (char *)name;
1390 /* Build the descriptor for the result. */
1391 result_desc.len = len;
1392 result_desc.mbz = 0;
1393 result_desc.adr = buf;
1395 status = LIB$GET_LOGICAL (&name_desc, &result_desc, &rlen);
1397 if ((status & 1) == 1 && rlen < len)
1398 buf[rlen] = 0;
1399 else
1400 buf[0] = 0;
1403 /* Size of a page on ia64 and alpha VMS. */
1404 #define VMS_PAGESIZE 8192
1406 /* User mode. */
1407 #define PSL__C_USER 3
1409 /* No access. */
1410 #define PRT__C_NA 0
1412 /* Descending region. */
1413 #define VA__M_DESCEND 1
1415 /* Get by virtual address. */
1416 #define VA___REGSUM_BY_VA 1
1418 /* Memory region summary. */
1419 struct regsum
1421 unsigned long long q_region_id;
1422 unsigned int l_flags;
1423 unsigned int l_region_protection;
1424 void *pq_start_va;
1425 unsigned long long q_region_size;
1426 void *pq_first_free_va;
1429 extern int SYS$GET_REGION_INFO (unsigned int, unsigned long long *,
1430 void *, void *, unsigned int,
1431 void *, unsigned int *);
1432 extern int SYS$EXPREG_64 (unsigned long long *, unsigned long long,
1433 unsigned int, unsigned int, void **,
1434 unsigned long long *);
1435 extern int SYS$SETPRT_64 (void *, unsigned long long, unsigned int,
1436 unsigned int, void **, unsigned long long *,
1437 unsigned int *);
1439 /* Add a guard page in the memory region containing ADDR at ADDR +/- SIZE.
1440 (The sign depends on the kind of the memory region). */
1442 static int
1443 __gnat_set_stack_guard_page (void *addr, unsigned long size)
1445 int status;
1446 void *ret_va;
1447 unsigned long long ret_len;
1448 unsigned int ret_prot;
1449 void *start_va;
1450 unsigned long long length;
1451 unsigned int retlen;
1452 struct regsum buffer;
1454 /* Get the region for ADDR. */
1455 status = SYS$GET_REGION_INFO
1456 (VA___REGSUM_BY_VA, NULL, addr, NULL, sizeof (buffer), &buffer, &retlen);
1458 if ((status & 1) != 1)
1459 return -1;
1461 /* Extend the region. */
1462 status = SYS$EXPREG_64 (&buffer.q_region_id,
1463 size, 0, 0, &start_va, &length);
1465 if ((status & 1) != 1)
1466 return -1;
1468 /* Create a guard page. */
1469 if (!(buffer.l_flags & VA__M_DESCEND))
1470 start_va = (void *)((unsigned long long)start_va + length - VMS_PAGESIZE);
1472 status = SYS$SETPRT_64 (start_va, VMS_PAGESIZE, PSL__C_USER, PRT__C_NA,
1473 &ret_va, &ret_len, &ret_prot);
1475 if ((status & 1) != 1)
1476 return -1;
1477 return 0;
1480 /* Read logicals to limit the stack(s) size. */
1482 static void
1483 __gnat_set_stack_limit (void)
1485 #ifdef __ia64__
1486 void *sp;
1487 unsigned long size;
1488 char value[16];
1489 char *e;
1491 /* The main stack. */
1492 __gnat_vms_get_logical ("GNAT_STACK_SIZE", value, sizeof (value));
1493 size = strtoul (value, &e, 0);
1494 if (e > value && *e == 0)
1496 asm ("mov %0=sp" : "=r" (sp));
1497 __gnat_set_stack_guard_page (sp, size * 1024);
1500 /* The register stack. */
1501 __gnat_vms_get_logical ("GNAT_RBS_SIZE", value, sizeof (value));
1502 size = strtoul (value, &e, 0);
1503 if (e > value && *e == 0)
1505 asm ("mov %0=ar.bsp" : "=r" (sp));
1506 __gnat_set_stack_guard_page (sp, size * 1024);
1508 #endif
1511 /* Feature logical name and global variable address pair.
1512 If we ever add another feature logical to this list, the
1513 feature struct will need to be enhanced to take into account
1514 possible values for *gl_addr. */
1515 struct feature {
1516 const char *name;
1517 int *gl_addr;
1520 /* Default values for GNAT features set by environment. */
1521 int __gl_heap_size = 64;
1523 /* Array feature logical names and global variable addresses. */
1524 static const struct feature features[] =
1526 {"GNAT$NO_MALLOC_64", &__gl_heap_size},
1527 {0, 0}
1530 void
1531 __gnat_set_features (void)
1533 int i;
1534 char buff[16];
1536 /* Loop through features array and test name for enable/disable. */
1537 for (i = 0; features[i].name; i++)
1539 __gnat_vms_get_logical (features[i].name, buff, sizeof (buff));
1541 if (strcmp (buff, "ENABLE") == 0
1542 || strcmp (buff, "TRUE") == 0
1543 || strcmp (buff, "1") == 0)
1544 *features[i].gl_addr = 32;
1545 else if (strcmp (buff, "DISABLE") == 0
1546 || strcmp (buff, "FALSE") == 0
1547 || strcmp (buff, "0") == 0)
1548 *features[i].gl_addr = 64;
1551 /* Features to artificially limit the stack size. */
1552 __gnat_set_stack_limit ();
1554 __gnat_features_set = 1;
1557 /* Return true if the VMS version is 7.x. */
1559 extern unsigned int LIB$GETSYI (int *, ...);
1561 #define SYI$_VERSION 0x1000
1564 __gnat_is_vms_v7 (void)
1566 struct descriptor_s desc;
1567 char version[8];
1568 int status;
1569 int code = SYI$_VERSION;
1571 desc.len = sizeof (version);
1572 desc.mbz = 0;
1573 desc.adr = version;
1575 status = LIB$GETSYI (&code, 0, &desc);
1576 if ((status & 1) == 1 && version[1] == '7' && version[2] == '.')
1577 return 1;
1578 else
1579 return 0;
1582 /*******************/
1583 /* FreeBSD Section */
1584 /*******************/
1586 #elif defined (__FreeBSD__)
1588 #include <signal.h>
1589 #include <sys/ucontext.h>
1590 #include <unistd.h>
1592 static void
1593 __gnat_error_handler (int sig,
1594 siginfo_t *si ATTRIBUTE_UNUSED,
1595 void *ucontext ATTRIBUTE_UNUSED)
1597 struct Exception_Data *exception;
1598 const char *msg;
1600 switch (sig)
1602 case SIGFPE:
1603 exception = &constraint_error;
1604 msg = "SIGFPE";
1605 break;
1607 case SIGILL:
1608 exception = &constraint_error;
1609 msg = "SIGILL";
1610 break;
1612 case SIGSEGV:
1613 exception = &storage_error;
1614 msg = "stack overflow or erroneous memory access";
1615 break;
1617 case SIGBUS:
1618 exception = &storage_error;
1619 msg = "SIGBUS: possible stack overflow";
1620 break;
1622 default:
1623 exception = &program_error;
1624 msg = "unhandled signal";
1627 Raise_From_Signal_Handler (exception, msg);
1630 void
1631 __gnat_install_handler ()
1633 struct sigaction act;
1635 /* Set up signal handler to map synchronous signals to appropriate
1636 exceptions. Make sure that the handler isn't interrupted by another
1637 signal that might cause a scheduling event! */
1639 act.sa_sigaction
1640 = (void (*)(int, struct __siginfo *, void*)) __gnat_error_handler;
1641 act.sa_flags = SA_NODEFER | SA_RESTART | SA_SIGINFO;
1642 (void) sigemptyset (&act.sa_mask);
1644 (void) sigaction (SIGILL, &act, NULL);
1645 (void) sigaction (SIGFPE, &act, NULL);
1646 (void) sigaction (SIGSEGV, &act, NULL);
1647 (void) sigaction (SIGBUS, &act, NULL);
1649 __gnat_handler_installed = 1;
1652 /*******************/
1653 /* VxWorks Section */
1654 /*******************/
1656 #elif defined(__vxworks)
1658 #include <signal.h>
1659 #include <taskLib.h>
1661 #ifndef __RTP__
1662 #include <intLib.h>
1663 #include <iv.h>
1664 #endif
1666 #if defined (ARMEL) && (_WRS_VXWORKS_MAJOR == 6)
1667 #include <vmLib.h>
1668 #endif
1670 #ifdef VTHREADS
1671 #include "private/vThreadsP.h"
1672 #endif
1674 #ifndef __RTP__
1676 /* Directly vectored Interrupt routines are not supported when using RTPs. */
1678 extern int __gnat_inum_to_ivec (int);
1680 /* This is needed by the GNAT run time to handle Vxworks interrupts. */
1682 __gnat_inum_to_ivec (int num)
1684 return (int) INUM_TO_IVEC (num);
1686 #endif
1688 #if !defined(__alpha_vxworks) && (_WRS_VXWORKS_MAJOR != 6) && !defined(__RTP__)
1690 /* getpid is used by s-parint.adb, but is not defined by VxWorks, except
1691 on Alpha VxWorks and VxWorks 6.x (including RTPs). */
1693 extern long getpid (void);
1695 long
1696 getpid (void)
1698 return taskIdSelf ();
1700 #endif
1702 /* VxWorks 653 vThreads expects the field excCnt to be zeroed when a signal is.
1703 handled. The VxWorks version of longjmp does this; GCC's builtin_longjmp
1704 doesn't. */
1705 void
1706 __gnat_clear_exception_count (void)
1708 #ifdef VTHREADS
1709 WIND_TCB *currentTask = (WIND_TCB *) taskIdSelf();
1711 currentTask->vThreads.excCnt = 0;
1712 #endif
1715 /* Handle different SIGnal to exception mappings in different VxWorks
1716 versions. */
1717 static void
1718 __gnat_map_signal (int sig, siginfo_t *si ATTRIBUTE_UNUSED,
1719 void *sc ATTRIBUTE_UNUSED)
1721 struct Exception_Data *exception;
1722 const char *msg;
1724 switch (sig)
1726 case SIGFPE:
1727 exception = &constraint_error;
1728 msg = "SIGFPE";
1729 break;
1730 #ifdef VTHREADS
1731 #ifdef __VXWORKSMILS__
1732 case SIGILL:
1733 exception = &storage_error;
1734 msg = "SIGILL: possible stack overflow";
1735 break;
1736 case SIGSEGV:
1737 exception = &storage_error;
1738 msg = "SIGSEGV";
1739 break;
1740 case SIGBUS:
1741 exception = &program_error;
1742 msg = "SIGBUS";
1743 break;
1744 #else
1745 case SIGILL:
1746 exception = &constraint_error;
1747 msg = "Floating point exception or SIGILL";
1748 break;
1749 case SIGSEGV:
1750 exception = &storage_error;
1751 msg = "SIGSEGV";
1752 break;
1753 case SIGBUS:
1754 exception = &storage_error;
1755 msg = "SIGBUS: possible stack overflow";
1756 break;
1757 #endif
1758 #elif (_WRS_VXWORKS_MAJOR == 6)
1759 case SIGILL:
1760 exception = &constraint_error;
1761 msg = "SIGILL";
1762 break;
1763 #ifdef __RTP__
1764 /* In RTP mode a SIGSEGV is most likely due to a stack overflow,
1765 since stack checking uses the probing mechanism. */
1766 case SIGSEGV:
1767 exception = &storage_error;
1768 msg = "SIGSEGV: possible stack overflow";
1769 break;
1770 case SIGBUS:
1771 exception = &program_error;
1772 msg = "SIGBUS";
1773 break;
1774 #else
1775 /* VxWorks 6 kernel mode with probing. SIGBUS for guard page hit */
1776 case SIGSEGV:
1777 exception = &storage_error;
1778 msg = "SIGSEGV";
1779 break;
1780 case SIGBUS:
1781 exception = &storage_error;
1782 msg = "SIGBUS: possible stack overflow";
1783 break;
1784 #endif
1785 #else
1786 /* VxWorks 5: a SIGILL is most likely due to a stack overflow,
1787 since stack checking uses the stack limit mechanism. */
1788 case SIGILL:
1789 exception = &storage_error;
1790 msg = "SIGILL: possible stack overflow";
1791 break;
1792 case SIGSEGV:
1793 exception = &storage_error;
1794 msg = "SIGSEGV";
1795 break;
1796 case SIGBUS:
1797 exception = &program_error;
1798 msg = "SIGBUS";
1799 break;
1800 #endif
1801 default:
1802 exception = &program_error;
1803 msg = "unhandled signal";
1806 /* On ARM VxWorks 6.x, the guard page is left un-armed by the kernel
1807 after being violated, so subsequent violations aren't detected.
1808 so we retrieve the address of the guard page from the TCB and compare it
1809 with the page that is violated (pREG 12 in the context) and re-arm that
1810 page if there's a match. Additionally we're are assured this is a
1811 genuine stack overflow condition and and set the message and exception
1812 to that effect. */
1813 #if defined (ARMEL) && (_WRS_VXWORKS_MAJOR == 6)
1815 /* We re-arm the guard page by marking it invalid */
1817 #define PAGE_SIZE 4096
1818 #define REG_IP 12
1820 if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL)
1822 TASK_ID tid = taskIdSelf ();
1823 WIND_TCB *pTcb = taskTcb (tid);
1824 unsigned long violated_page
1825 = ((struct sigcontext *) sc)->sc_pregs->r[REG_IP] & ~(PAGE_SIZE - 1);
1827 if ((unsigned long) (pTcb->pStackEnd - PAGE_SIZE) == violated_page)
1829 vmStateSet (NULL, violated_page,
1830 PAGE_SIZE, VM_STATE_MASK_VALID, VM_STATE_VALID_NOT);
1831 exception = &storage_error;
1833 switch (sig)
1835 case SIGSEGV:
1836 msg = "SIGSEGV: stack overflow";
1837 break;
1838 case SIGBUS:
1839 msg = "SIGBUS: stack overflow";
1840 break;
1841 case SIGILL:
1842 msg = "SIGILL: stack overflow";
1843 break;
1847 #endif /* defined (ARMEL) && (_WRS_VXWORKS_MAJOR == 6) */
1849 __gnat_clear_exception_count ();
1850 Raise_From_Signal_Handler (exception, msg);
1853 /* Tasking and Non-tasking signal handler. Map SIGnal to Ada exception
1854 propagation after the required low level adjustments. */
1856 static void
1857 __gnat_error_handler (int sig, siginfo_t *si, void *sc)
1859 sigset_t mask;
1861 /* VxWorks will always mask out the signal during the signal handler and
1862 will reenable it on a longjmp. GNAT does not generate a longjmp to
1863 return from a signal handler so the signal will still be masked unless
1864 we unmask it. */
1865 sigprocmask (SIG_SETMASK, NULL, &mask);
1866 sigdelset (&mask, sig);
1867 sigprocmask (SIG_SETMASK, &mask, NULL);
1869 #if defined (__PPC__) && defined(_WRS_KERNEL)
1870 /* On PowerPC, kernel mode, we process signals through a Call Frame Info
1871 trampoline, voiding the need for myriads of fallback_frame_state
1872 variants in the ZCX runtime. We have no simple way to distinguish ZCX
1873 from SJLJ here, so we do this for SJLJ as well even though this is not
1874 necessary. This only incurs a few extra instructions and a tiny
1875 amount of extra stack usage. */
1877 #include "sigtramp.h"
1879 __gnat_sigtramp (sig, (void *)si, (void *)sc,
1880 (sighandler_t *)&__gnat_map_signal);
1882 #else
1883 __gnat_map_signal (sig, si, sc);
1884 #endif
1887 #if defined(__leon__) && defined(_WRS_KERNEL)
1888 /* For LEON VxWorks we need to install a trap handler for stack overflow */
1890 extern void excEnt (void);
1891 /* VxWorks exception handler entry */
1893 struct trap_entry {
1894 unsigned long inst_first;
1895 unsigned long inst_second;
1896 unsigned long inst_third;
1897 unsigned long inst_fourth;
1899 /* Four instructions representing entries in the trap table */
1901 struct trap_entry *trap_0_entry;
1902 /* We will set the location of the entry for software trap 0 in the trap
1903 table. */
1904 #endif
1906 void
1907 __gnat_install_handler (void)
1909 struct sigaction act;
1911 /* Setup signal handler to map synchronous signals to appropriate
1912 exceptions. Make sure that the handler isn't interrupted by another
1913 signal that might cause a scheduling event! */
1915 act.sa_sigaction = __gnat_error_handler;
1916 act.sa_flags = SA_SIGINFO | SA_ONSTACK;
1917 sigemptyset (&act.sa_mask);
1919 /* For VxWorks, install all signal handlers, since pragma Interrupt_State
1920 applies to vectored hardware interrupts, not signals. */
1921 sigaction (SIGFPE, &act, NULL);
1922 sigaction (SIGILL, &act, NULL);
1923 sigaction (SIGSEGV, &act, NULL);
1924 sigaction (SIGBUS, &act, NULL);
1926 #if defined(__leon__) && defined(_WRS_KERNEL)
1927 /* Specific to the LEON VxWorks kernel run-time library */
1929 /* For stack checking the compiler triggers a software trap 0 (ta 0) in
1930 case of overflow (we use the stack limit mechanism). We need to install
1931 the trap handler here for this software trap (the OS does not handle
1932 it) as if it were a data_access_exception (trap 9). We do the same as
1933 if we put in the trap table a VXSPARC_BAD_TRAP(9). Software trap 0 is
1934 located at vector 0x80, and each entry takes 4 words. */
1936 trap_0_entry = (struct trap_entry *)(intVecBaseGet () + 0x80 * 4);
1938 /* mov 0x9, %l7 */
1940 trap_0_entry->inst_first = 0xae102000 + 9;
1942 /* sethi %hi(excEnt), %l6 */
1944 /* The 22 most significant bits of excEnt are obtained shifting 10 times
1945 to the right. */
1947 trap_0_entry->inst_second = 0x2d000000 + ((unsigned long)excEnt >> 10);
1949 /* jmp %l6+%lo(excEnt) */
1951 /* The 10 least significant bits of excEnt are obtained by masking */
1953 trap_0_entry->inst_third = 0x81c5a000 + ((unsigned long)excEnt & 0x3ff);
1955 /* rd %psr, %l0 */
1957 trap_0_entry->inst_fourth = 0xa1480000;
1958 #endif
1960 __gnat_handler_installed = 1;
1963 #define HAVE_GNAT_INIT_FLOAT
1965 void
1966 __gnat_init_float (void)
1968 /* Disable overflow/underflow exceptions on the PPC processor, needed
1969 to get correct Ada semantics. Note that for AE653 vThreads, the HW
1970 overflow settings are an OS configuration issue. The instructions
1971 below have no effect. */
1972 #if defined (_ARCH_PPC) && !defined (_SOFT_FLOAT) && (!defined (VTHREADS) || defined (__VXWORKSMILS__))
1973 #if defined (__SPE__)
1975 const unsigned long spefscr_mask = 0xfffffff3;
1976 unsigned long spefscr;
1977 asm ("mfspr %0, 512" : "=r" (spefscr));
1978 spefscr = spefscr & spefscr_mask;
1979 asm ("mtspr 512, %0\n\tisync" : : "r" (spefscr));
1981 #else
1982 asm ("mtfsb0 25");
1983 asm ("mtfsb0 26");
1984 #endif
1985 #endif
1987 #if (defined (__i386__) || defined (i386)) && !defined (VTHREADS)
1988 /* This is used to properly initialize the FPU on an x86 for each
1989 process thread. */
1990 asm ("finit");
1991 #endif
1993 /* Similarly for SPARC64. Achieved by masking bits in the Trap Enable Mask
1994 field of the Floating-point Status Register (see the SPARC Architecture
1995 Manual Version 9, p 48). */
1996 #if defined (sparc64)
1998 #define FSR_TEM_NVM (1 << 27) /* Invalid operand */
1999 #define FSR_TEM_OFM (1 << 26) /* Overflow */
2000 #define FSR_TEM_UFM (1 << 25) /* Underflow */
2001 #define FSR_TEM_DZM (1 << 24) /* Division by Zero */
2002 #define FSR_TEM_NXM (1 << 23) /* Inexact result */
2004 unsigned int fsr;
2006 __asm__("st %%fsr, %0" : "=m" (fsr));
2007 fsr &= ~(FSR_TEM_OFM | FSR_TEM_UFM);
2008 __asm__("ld %0, %%fsr" : : "m" (fsr));
2010 #endif
2013 /* This subprogram is called by System.Task_Primitives.Operations.Enter_Task
2014 (if not null) when a new task is created. It is initialized by
2015 System.Stack_Checking.Operations.Initialize_Stack_Limit.
2016 The use of a hook avoids to drag stack checking subprograms if stack
2017 checking is not used. */
2018 void (*__gnat_set_stack_limit_hook)(void) = (void (*)(void))0;
2020 /******************/
2021 /* NetBSD Section */
2022 /******************/
2024 #elif defined(__NetBSD__)
2026 #include <signal.h>
2027 #include <unistd.h>
2029 static void
2030 __gnat_error_handler (int sig)
2032 struct Exception_Data *exception;
2033 const char *msg;
2035 switch(sig)
2037 case SIGFPE:
2038 exception = &constraint_error;
2039 msg = "SIGFPE";
2040 break;
2041 case SIGILL:
2042 exception = &constraint_error;
2043 msg = "SIGILL";
2044 break;
2045 case SIGSEGV:
2046 exception = &storage_error;
2047 msg = "stack overflow or erroneous memory access";
2048 break;
2049 case SIGBUS:
2050 exception = &constraint_error;
2051 msg = "SIGBUS";
2052 break;
2053 default:
2054 exception = &program_error;
2055 msg = "unhandled signal";
2058 Raise_From_Signal_Handler(exception, msg);
2061 void
2062 __gnat_install_handler(void)
2064 struct sigaction act;
2066 act.sa_handler = __gnat_error_handler;
2067 act.sa_flags = SA_NODEFER | SA_RESTART;
2068 sigemptyset (&act.sa_mask);
2070 /* Do not install handlers if interrupt state is "System". */
2071 if (__gnat_get_interrupt_state (SIGFPE) != 's')
2072 sigaction (SIGFPE, &act, NULL);
2073 if (__gnat_get_interrupt_state (SIGILL) != 's')
2074 sigaction (SIGILL, &act, NULL);
2075 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
2076 sigaction (SIGSEGV, &act, NULL);
2077 if (__gnat_get_interrupt_state (SIGBUS) != 's')
2078 sigaction (SIGBUS, &act, NULL);
2080 __gnat_handler_installed = 1;
2083 /*******************/
2084 /* OpenBSD Section */
2085 /*******************/
2087 #elif defined(__OpenBSD__)
2089 #include <signal.h>
2090 #include <unistd.h>
2092 static void
2093 __gnat_error_handler (int sig)
2095 struct Exception_Data *exception;
2096 const char *msg;
2098 switch(sig)
2100 case SIGFPE:
2101 exception = &constraint_error;
2102 msg = "SIGFPE";
2103 break;
2104 case SIGILL:
2105 exception = &constraint_error;
2106 msg = "SIGILL";
2107 break;
2108 case SIGSEGV:
2109 exception = &storage_error;
2110 msg = "stack overflow or erroneous memory access";
2111 break;
2112 case SIGBUS:
2113 exception = &constraint_error;
2114 msg = "SIGBUS";
2115 break;
2116 default:
2117 exception = &program_error;
2118 msg = "unhandled signal";
2121 Raise_From_Signal_Handler(exception, msg);
2124 void
2125 __gnat_install_handler(void)
2127 struct sigaction act;
2129 act.sa_handler = __gnat_error_handler;
2130 act.sa_flags = SA_NODEFER | SA_RESTART;
2131 sigemptyset (&act.sa_mask);
2133 /* Do not install handlers if interrupt state is "System" */
2134 if (__gnat_get_interrupt_state (SIGFPE) != 's')
2135 sigaction (SIGFPE, &act, NULL);
2136 if (__gnat_get_interrupt_state (SIGILL) != 's')
2137 sigaction (SIGILL, &act, NULL);
2138 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
2139 sigaction (SIGSEGV, &act, NULL);
2140 if (__gnat_get_interrupt_state (SIGBUS) != 's')
2141 sigaction (SIGBUS, &act, NULL);
2143 __gnat_handler_installed = 1;
2146 /******************/
2147 /* Darwin Section */
2148 /******************/
2150 #elif defined(__APPLE__)
2152 #include <signal.h>
2153 #include <stdlib.h>
2154 #include <sys/syscall.h>
2155 #include <sys/sysctl.h>
2156 #include <mach/mach_vm.h>
2157 #include <mach/mach_init.h>
2158 #include <mach/vm_statistics.h>
2160 /* This must be in keeping with System.OS_Interface.Alternate_Stack_Size. */
2161 char __gnat_alternate_stack[32 * 1024]; /* 1 * MINSIGSTKSZ */
2163 /* Defined in xnu unix_signal.c.
2164 Tell the kernel to re-use alt stack when delivering a signal. */
2165 #define UC_RESET_ALT_STACK 0x80000000
2167 /* Return true if ADDR is within a stack guard area. */
2168 static int
2169 __gnat_is_stack_guard (mach_vm_address_t addr)
2171 kern_return_t kret;
2172 vm_region_submap_info_data_64_t info;
2173 mach_vm_address_t start;
2174 mach_vm_size_t size;
2175 natural_t depth;
2176 mach_msg_type_number_t count;
2178 count = VM_REGION_SUBMAP_INFO_COUNT_64;
2179 start = addr;
2180 size = -1;
2181 depth = 9999;
2182 kret = mach_vm_region_recurse (mach_task_self (), &start, &size, &depth,
2183 (vm_region_recurse_info_t) &info, &count);
2184 if (kret == KERN_SUCCESS
2185 && addr >= start && addr < (start + size)
2186 && info.protection == VM_PROT_NONE
2187 && info.user_tag == VM_MEMORY_STACK)
2188 return 1;
2189 return 0;
2192 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
2194 #if defined (__x86_64__)
2195 static int
2196 __darwin_major_version (void)
2198 static int cache = -1;
2199 if (cache < 0)
2201 int mib[2] = {CTL_KERN, KERN_OSRELEASE};
2202 size_t len;
2204 /* Find out how big the buffer needs to be (and set cache to 0
2205 on failure). */
2206 if (sysctl (mib, 2, NULL, &len, NULL, 0) == 0)
2208 char release[len];
2209 sysctl (mib, 2, release, &len, NULL, 0);
2210 /* Darwin releases are of the form L.M.N where L is the major
2211 version, so strtol will return L. */
2212 cache = (int) strtol (release, NULL, 10);
2214 else
2216 cache = 0;
2219 return cache;
2221 #endif
2223 void
2224 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED,
2225 void *ucontext ATTRIBUTE_UNUSED)
2227 #if defined (__x86_64__)
2228 if (__darwin_major_version () < 12)
2230 /* Work around radar #10302855, where the unwinders (libunwind or
2231 libgcc_s depending on the system revision) and the DWARF unwind
2232 data for sigtramp have different ideas about register numbering,
2233 causing rbx and rdx to be transposed. */
2234 ucontext_t *uc = (ucontext_t *)ucontext;
2235 unsigned long t = uc->uc_mcontext->__ss.__rbx;
2237 uc->uc_mcontext->__ss.__rbx = uc->uc_mcontext->__ss.__rdx;
2238 uc->uc_mcontext->__ss.__rdx = t;
2240 #endif
2243 static void
2244 __gnat_error_handler (int sig, siginfo_t *si, void *ucontext)
2246 struct Exception_Data *exception;
2247 const char *msg;
2249 __gnat_adjust_context_for_raise (sig, ucontext);
2251 switch (sig)
2253 case SIGSEGV:
2254 case SIGBUS:
2255 if (__gnat_is_stack_guard ((unsigned long)si->si_addr))
2257 exception = &storage_error;
2258 msg = "stack overflow";
2260 else
2262 exception = &constraint_error;
2263 msg = "erroneous memory access";
2265 /* Reset the use of alt stack, so that the alt stack will be used
2266 for the next signal delivery.
2267 The stack can't be used in case of stack checking. */
2268 syscall (SYS_sigreturn, NULL, UC_RESET_ALT_STACK);
2269 break;
2271 case SIGFPE:
2272 exception = &constraint_error;
2273 msg = "SIGFPE";
2274 break;
2276 default:
2277 exception = &program_error;
2278 msg = "unhandled signal";
2281 Raise_From_Signal_Handler (exception, msg);
2284 void
2285 __gnat_install_handler (void)
2287 struct sigaction act;
2289 /* Set up signal handler to map synchronous signals to appropriate
2290 exceptions. Make sure that the handler isn't interrupted by another
2291 signal that might cause a scheduling event! Also setup an alternate
2292 stack region for the handler execution so that stack overflows can be
2293 handled properly, avoiding a SEGV generation from stack usage by the
2294 handler itself (and it is required by Darwin). */
2296 stack_t stack;
2297 stack.ss_sp = __gnat_alternate_stack;
2298 stack.ss_size = sizeof (__gnat_alternate_stack);
2299 stack.ss_flags = 0;
2300 sigaltstack (&stack, NULL);
2302 act.sa_flags = SA_NODEFER | SA_RESTART | SA_SIGINFO;
2303 act.sa_sigaction = __gnat_error_handler;
2304 sigemptyset (&act.sa_mask);
2306 /* Do not install handlers if interrupt state is "System". */
2307 if (__gnat_get_interrupt_state (SIGABRT) != 's')
2308 sigaction (SIGABRT, &act, NULL);
2309 if (__gnat_get_interrupt_state (SIGFPE) != 's')
2310 sigaction (SIGFPE, &act, NULL);
2311 if (__gnat_get_interrupt_state (SIGILL) != 's')
2312 sigaction (SIGILL, &act, NULL);
2314 act.sa_flags |= SA_ONSTACK;
2315 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
2316 sigaction (SIGSEGV, &act, NULL);
2317 if (__gnat_get_interrupt_state (SIGBUS) != 's')
2318 sigaction (SIGBUS, &act, NULL);
2320 __gnat_handler_installed = 1;
2323 #else
2325 /* For all other versions of GNAT, the handler does nothing. */
2327 /*******************/
2328 /* Default Section */
2329 /*******************/
2331 void
2332 __gnat_install_handler (void)
2334 __gnat_handler_installed = 1;
2337 #endif
2339 /*********************/
2340 /* __gnat_init_float */
2341 /*********************/
2343 /* This routine is called as each process thread is created, for possible
2344 initialization of the FP processor. This version is used under INTERIX
2345 and WIN32. */
2347 #if defined (_WIN32) || defined (__INTERIX) \
2348 || defined (__Lynx__) || defined(__NetBSD__) || defined(__FreeBSD__) \
2349 || defined (__OpenBSD__)
2351 #define HAVE_GNAT_INIT_FLOAT
2353 void
2354 __gnat_init_float (void)
2356 #if defined (__i386__) || defined (i386) || defined (__x86_64)
2358 /* This is used to properly initialize the FPU on an x86 for each
2359 process thread. */
2361 asm ("finit");
2363 #endif /* Defined __i386__ */
2365 #endif
2367 #ifndef HAVE_GNAT_INIT_FLOAT
2369 /* All targets without a specific __gnat_init_float will use an empty one. */
2370 void
2371 __gnat_init_float (void)
2374 #endif
2376 /***********************************/
2377 /* __gnat_adjust_context_for_raise */
2378 /***********************************/
2380 #ifndef HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
2382 /* All targets without a specific version will use an empty one. */
2384 /* Given UCONTEXT a pointer to a context structure received by a signal
2385 handler for SIGNO, perform the necessary adjustments to let the handler
2386 raise an exception. Calls to this routine are not conditioned by the
2387 propagation scheme in use. */
2389 void
2390 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED,
2391 void *ucontext ATTRIBUTE_UNUSED)
2393 /* We used to compensate here for the raised from call vs raised from signal
2394 exception discrepancy with the GCC ZCX scheme, but this now can be dealt
2395 with generically in the unwinder (see GCC PR other/26208). This however
2396 requires the use of the _Unwind_GetIPInfo routine in raise-gcc.c, which
2397 is predicated on the definition of HAVE_GETIPINFO at compile time. Only
2398 the VMS ports still do the compensation described in the few lines below.
2400 *** Call vs signal exception discrepancy with GCC ZCX scheme ***
2402 The GCC unwinder expects to be dealing with call return addresses, since
2403 this is the "nominal" case of what we retrieve while unwinding a regular
2404 call chain.
2406 To evaluate if a handler applies at some point identified by a return
2407 address, the propagation engine needs to determine what region the
2408 corresponding call instruction pertains to. Because the return address
2409 may not be attached to the same region as the call, the unwinder always
2410 subtracts "some" amount from a return address to search the region
2411 tables, amount chosen to ensure that the resulting address is inside the
2412 call instruction.
2414 When we raise an exception from a signal handler, e.g. to transform a
2415 SIGSEGV into Storage_Error, things need to appear as if the signal
2416 handler had been "called" by the instruction which triggered the signal,
2417 so that exception handlers that apply there are considered. What the
2418 unwinder will retrieve as the return address from the signal handler is
2419 what it will find as the faulting instruction address in the signal
2420 context pushed by the kernel. Leaving this address untouched looses, if
2421 the triggering instruction happens to be the very first of a region, as
2422 the later adjustments performed by the unwinder would yield an address
2423 outside that region. We need to compensate for the unwinder adjustments
2424 at some point, and this is what this routine is expected to do.
2426 signo is passed because on some targets for some signals the PC in
2427 context points to the instruction after the faulting one, in which case
2428 the unwinder adjustment is still desired. */
2431 #endif
2433 #ifdef __cplusplus
2435 #endif