1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 1992-2013, Free Software Foundation, Inc. *
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. *
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. *
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/>. *
27 * GNAT was originally developed by the GNAT team at New York University. *
28 * Extensive contributions were provided by Ada Core Technologies Inc. *
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. */
60 /* We don't have libiberty, so use malloc. */
61 #define xmalloc(S) malloc (S)
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. */
87 #define Raise_From_Signal_Handler \
88 __gnat_raise_exception
89 extern void Raise_From_Signal_Handler (struct Exception_Data
*, const char *);
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 *);
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;
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). */
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 */
150 __gnat_get_interrupt_state (int intrup
)
152 if (intrup
>= __gl_num_interrupt_states
)
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. */
173 __gnat_get_specific_dispatching (int priority
)
175 if (__gl_num_specific_dispatching
== 0)
177 else if (priority
>= __gl_num_specific_dispatching
)
180 return __gl_priority_specific_dispatching
[priority
];
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. */
193 __gnat_set_globals (void)
206 #include <sys/time.h>
208 /* Some versions of AIX don't define SA_NODEFER. */
212 #endif /* SA_NODEFER */
214 /* Versions of AIX before 4.3 don't have nanosleep but provide
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 */
230 __gnat_error_handler (int sig
,
231 siginfo_t
*si ATTRIBUTE_UNUSED
,
232 void *ucontext ATTRIBUTE_UNUSED
)
234 struct Exception_Data
*exception
;
240 /* FIXME: we need to detect the case of a *real* SIGSEGV. */
241 exception
= &storage_error
;
242 msg
= "stack overflow or erroneous memory access";
246 exception
= &constraint_error
;
251 exception
= &constraint_error
;
256 exception
= &program_error
;
257 msg
= "unhandled signal";
260 Raise_From_Signal_Handler (exception
, msg
);
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;
295 #elif defined (__hpux__)
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
307 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED
, void *ucontext
)
309 ucontext_t
*uc
= (ucontext_t
*) ucontext
;
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. */
322 __gnat_error_handler (int sig
,
323 siginfo_t
*si ATTRIBUTE_UNUSED
,
324 void *ucontext ATTRIBUTE_UNUSED
)
326 struct Exception_Data
*exception
;
329 __gnat_adjust_context_for_raise (sig
, ucontext
);
334 /* FIXME: we need to detect the case of a *real* SIGSEGV. */
335 exception
= &storage_error
;
336 msg
= "stack overflow or erroneous memory access";
340 exception
= &constraint_error
;
345 exception
= &constraint_error
;
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 */
361 char __gnat_alternate_stack
[128 * 1024]; /* MINSIGSTKSZ */
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
377 stack
.ss_sp
= __gnat_alternate_stack
;
378 stack
.ss_size
= sizeof (__gnat_alternate_stack
);
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)
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
417 #define NULL ((void *) 0)
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
) {
434 return sigaction (signum
, act
, oldact
);
436 #define sigaction(signum, act, oldact) linux_sigaction (signum, act, oldact)
438 #pragma weak fake_linux_sigfillset
439 void fake_linux_sigfillset (sigset_t
*set
) {
442 #define sigfillset(set) fake_linux_sigfillset (set)
444 #pragma weak fake_linux_sigemptyset
445 void fake_linux_sigemptyset (sigset_t
*set
) {
448 #define sigemptyset(set) fake_linux_sigemptyset (set)
452 #if defined (i386) || defined (__x86_64__) || defined (__ia64__)
454 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
457 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED
, void *ucontext
)
459 mcontext_t
*mcontext
= &((ucontext_t
*) ucontext
)->uc_mcontext
;
461 /* On the i386 and x86-64 architectures, stack checking is performed by
462 means of probes with moving stack pointer, that is to say the probed
463 address is always the value of the stack pointer. Upon hitting the
464 guard page, the stack pointer therefore points to an inaccessible
465 address and an alternate signal stack is needed to run the handler.
466 But there is an additional twist: on these architectures, the EH
467 return code writes the address of the handler at the target CFA's
468 value on the stack before doing the jump. As a consequence, if
469 there is an active handler in the frame whose stack has overflowed,
470 the stack pointer must nevertheless point to an accessible address
471 by the time the EH return is executed.
473 We therefore adjust the saved value of the stack pointer by the size
474 of one page + a small dope of 4 words, in order to make sure that it
475 points to an accessible address in case it's used as the target CFA.
476 The stack checking code guarantees that this address is unused by the
477 time this happens. */
480 unsigned long *pc
= (unsigned long *)mcontext
->gregs
[REG_EIP
];
481 /* The pattern is "orl $0x0,(%esp)" for a probe in 32-bit mode. */
482 if (signo
== SIGSEGV
&& pc
&& *pc
== 0x00240c83)
483 mcontext
->gregs
[REG_ESP
] += 4096 + 4 * sizeof (unsigned long);
484 #elif defined (__x86_64__)
485 unsigned long long *pc
= (unsigned long long *)mcontext
->gregs
[REG_RIP
];
486 if (signo
== SIGSEGV
&& pc
487 /* The pattern is "orq $0x0,(%rsp)" for a probe in 64-bit mode. */
488 && ((*pc
& 0xffffffffffLL
) == 0x00240c8348LL
489 /* The pattern may also be "orl $0x0,(%esp)" for a probe in
491 || (*pc
& 0xffffffffLL
) == 0x00240c83LL
))
492 mcontext
->gregs
[REG_RSP
] += 4096 + 4 * sizeof (unsigned long);
493 #elif defined (__ia64__)
494 /* ??? The IA-64 unwinder doesn't compensate for signals. */
502 __gnat_error_handler (int sig
, siginfo_t
*si ATTRIBUTE_UNUSED
, void *ucontext
)
504 struct Exception_Data
*exception
;
507 /* Adjusting is required for every fault context, so adjust for this one
508 now, before we possibly trigger a recursive fault below. */
509 __gnat_adjust_context_for_raise (sig
, ucontext
);
514 /* Here we would like a discrimination test to see whether the page
515 before the faulting address is accessible. Unfortunately, Linux
516 seems to have no way of giving us the faulting address.
518 In old versions of init.c, we had a test of the page before the
522 ((long) si->esp_at_signal & - getpagesize ()))[getpagesize ()];
524 but that's wrong since it tests the stack pointer location and the
525 stack probing code may not move it until all probes succeed.
527 For now we simply do not attempt any discrimination at all. Note
528 that this is quite acceptable, since a "real" SIGSEGV can only
529 occur as the result of an erroneous program. */
530 exception
= &storage_error
;
531 msg
= "stack overflow or erroneous memory access";
535 exception
= &storage_error
;
536 msg
= "SIGBUS: possible stack overflow";
540 exception
= &constraint_error
;
545 exception
= &program_error
;
546 msg
= "unhandled signal";
549 Raise_From_Signal_Handler (exception
, msg
);
552 #if defined (i386) || defined (__x86_64__) || defined (__powerpc__)
553 /* This must be in keeping with System.OS_Interface.Alternate_Stack_Size. */
554 char __gnat_alternate_stack
[16 * 1024]; /* 2 * SIGSTKSZ */
558 #include <sys/mman.h>
559 #include <native/task.h>
565 __gnat_install_handler (void)
567 struct sigaction act
;
572 if (__gl_main_priority
== -1)
575 prio
= __gl_main_priority
;
577 /* Avoid memory swapping for this program */
579 mlockall (MCL_CURRENT
|MCL_FUTURE
);
581 /* Turn the current Linux task into a native Xenomai task */
583 rt_task_shadow(&main_task
, "environment_task", prio
, T_FPU
);
586 /* Set up signal handler to map synchronous signals to appropriate
587 exceptions. Make sure that the handler isn't interrupted by another
588 signal that might cause a scheduling event! Also setup an alternate
589 stack region for the handler execution so that stack overflows can be
590 handled properly, avoiding a SEGV generation from stack usage by the
593 act
.sa_sigaction
= __gnat_error_handler
;
594 act
.sa_flags
= SA_NODEFER
| SA_RESTART
| SA_SIGINFO
;
595 sigemptyset (&act
.sa_mask
);
597 /* Do not install handlers if interrupt state is "System". */
598 if (__gnat_get_interrupt_state (SIGABRT
) != 's')
599 sigaction (SIGABRT
, &act
, NULL
);
600 if (__gnat_get_interrupt_state (SIGFPE
) != 's')
601 sigaction (SIGFPE
, &act
, NULL
);
602 if (__gnat_get_interrupt_state (SIGILL
) != 's')
603 sigaction (SIGILL
, &act
, NULL
);
604 if (__gnat_get_interrupt_state (SIGBUS
) != 's')
605 sigaction (SIGBUS
, &act
, NULL
);
606 if (__gnat_get_interrupt_state (SIGSEGV
) != 's')
608 #if defined (i386) || defined (__x86_64__) || defined (__powerpc__)
609 /* Setup an alternate stack region for the handler execution so that
610 stack overflows can be handled properly, avoiding a SEGV generation
611 from stack usage by the handler itself. */
614 stack
.ss_sp
= __gnat_alternate_stack
;
615 stack
.ss_size
= sizeof (__gnat_alternate_stack
);
617 sigaltstack (&stack
, NULL
);
619 act
.sa_flags
|= SA_ONSTACK
;
621 sigaction (SIGSEGV
, &act
, NULL
);
624 __gnat_handler_installed
= 1;
627 /*******************/
629 /*******************/
631 #elif defined (__Lynx__)
637 __gnat_error_handler (int sig
)
639 struct Exception_Data
*exception
;
645 exception
= &constraint_error
;
649 exception
= &constraint_error
;
653 exception
= &storage_error
;
654 msg
= "stack overflow or erroneous memory access";
657 exception
= &constraint_error
;
661 exception
= &program_error
;
662 msg
= "unhandled signal";
665 Raise_From_Signal_Handler(exception
, msg
);
669 __gnat_install_handler(void)
671 struct sigaction act
;
673 act
.sa_handler
= __gnat_error_handler
;
675 sigemptyset (&act
.sa_mask
);
677 /* Do not install handlers if interrupt state is "System". */
678 if (__gnat_get_interrupt_state (SIGFPE
) != 's')
679 sigaction (SIGFPE
, &act
, NULL
);
680 if (__gnat_get_interrupt_state (SIGILL
) != 's')
681 sigaction (SIGILL
, &act
, NULL
);
682 if (__gnat_get_interrupt_state (SIGSEGV
) != 's')
683 sigaction (SIGSEGV
, &act
, NULL
);
684 if (__gnat_get_interrupt_state (SIGBUS
) != 's')
685 sigaction (SIGBUS
, &act
, NULL
);
687 __gnat_handler_installed
= 1;
690 /*******************/
691 /* Solaris Section */
692 /*******************/
694 #elif defined (sun) && defined (__SVR4) && !defined (__vxworks)
698 #include <sys/ucontext.h>
699 #include <sys/regset.h>
702 __gnat_error_handler (int sig
, siginfo_t
*si
, void *ucontext ATTRIBUTE_UNUSED
)
704 struct Exception_Data
*exception
;
705 static int recurse
= 0;
711 /* If the problem was permissions, this is a constraint error.
712 Likewise if the failing address isn't maximally aligned or if
715 ??? Using a static variable here isn't task-safe, but it's
716 much too hard to do anything else and we're just determining
717 which exception to raise. */
718 if (si
->si_code
== SEGV_ACCERR
719 || (long) si
->si_addr
== 0
720 || (((long) si
->si_addr
) & 3) != 0
723 exception
= &constraint_error
;
728 /* See if the page before the faulting page is accessible. Do that
729 by trying to access it. We'd like to simply try to access
730 4096 + the faulting address, but it's not guaranteed to be
731 the actual address, just to be on the same page. */
734 ((long) si
->si_addr
& - getpagesize ()))[getpagesize ()];
735 exception
= &storage_error
;
736 msg
= "stack overflow or erroneous memory access";
741 exception
= &program_error
;
746 exception
= &constraint_error
;
751 exception
= &program_error
;
752 msg
= "unhandled signal";
756 Raise_From_Signal_Handler (exception
, msg
);
760 __gnat_install_handler (void)
762 struct sigaction act
;
764 /* Set up signal handler to map synchronous signals to appropriate
765 exceptions. Make sure that the handler isn't interrupted by another
766 signal that might cause a scheduling event! */
768 act
.sa_sigaction
= __gnat_error_handler
;
769 act
.sa_flags
= SA_NODEFER
| SA_RESTART
| SA_SIGINFO
;
770 sigemptyset (&act
.sa_mask
);
772 /* Do not install handlers if interrupt state is "System". */
773 if (__gnat_get_interrupt_state (SIGABRT
) != 's')
774 sigaction (SIGABRT
, &act
, NULL
);
775 if (__gnat_get_interrupt_state (SIGFPE
) != 's')
776 sigaction (SIGFPE
, &act
, NULL
);
777 if (__gnat_get_interrupt_state (SIGSEGV
) != 's')
778 sigaction (SIGSEGV
, &act
, NULL
);
779 if (__gnat_get_interrupt_state (SIGBUS
) != 's')
780 sigaction (SIGBUS
, &act
, NULL
);
782 __gnat_handler_installed
= 1;
791 /* Routine called from binder to override default feature values. */
792 void __gnat_set_features (void);
793 int __gnat_features_set
= 0;
794 void (*__gnat_ctrl_c_handler
) (void) = 0;
797 #define lib_get_curr_invo_context LIB$I64_GET_CURR_INVO_CONTEXT
798 #define lib_get_prev_invo_context LIB$I64_GET_PREV_INVO_CONTEXT
799 #define lib_get_invo_handle LIB$I64_GET_INVO_HANDLE
801 #define lib_get_curr_invo_context LIB$GET_CURR_INVO_CONTEXT
802 #define lib_get_prev_invo_context LIB$GET_PREV_INVO_CONTEXT
803 #define lib_get_invo_handle LIB$GET_INVO_HANDLE
806 /* Masks for facility identification. */
807 #define FAC_MASK 0x0fff0000
808 #define DECADA_M_FACILITY 0x00310000
810 /* Define macro symbols for the VMS conditions that become Ada exceptions.
811 It would be better to just include <ssdef.h> */
813 #define SS$_CONTINUE 1
814 #define SS$_ACCVIO 12
815 #define SS$_HPARITH 1284
816 #define SS$_INTDIV 1156
817 #define SS$_STKOVF 1364
818 #define SS$_CONTROLC 1617
819 #define SS$_RESIGNAL 2328
821 #define MTH$_FLOOVEMAT 1475268 /* Some ACVC_21 CXA tests */
823 /* The following codes must be resignalled, and not handled here. */
825 /* These codes are in standard message libraries. */
826 extern int C$_SIGKILL
;
827 extern int C$_SIGINT
;
828 extern int SS$_DEBUG
;
829 extern int LIB$_KEYNOTFOU
;
830 extern int LIB$_ACTIMAGE
;
832 /* These codes are non standard, which is to say the author is
833 not sure if they are defined in the standard message libraries
834 so keep them as macros for now. */
835 #define RDB$_STREAM_EOF 20480426
836 #define FDL$_UNPRIKW 11829410
837 #define CMA$_EXIT_THREAD 4227492
842 unsigned int sigargval
;
848 const struct cond_sigargs sigargs
[];
854 const struct Exception_Data
*except
;
855 unsigned int needs_adjust
; /* 1 = adjust PC, 0 = no adjust */
856 const struct cond_subtests
*subtests
;
861 unsigned short len
, mbz
;
865 /* Conditions that don't have an Ada exception counterpart must raise
866 Non_Ada_Error. Since this is defined in s-auxdec, it should only be
867 referenced by user programs, not the compiler or tools. Hence the
872 #define Status_Error ada__io_exceptions__status_error
873 extern struct Exception_Data Status_Error
;
875 #define Mode_Error ada__io_exceptions__mode_error
876 extern struct Exception_Data Mode_Error
;
878 #define Name_Error ada__io_exceptions__name_error
879 extern struct Exception_Data Name_Error
;
881 #define Use_Error ada__io_exceptions__use_error
882 extern struct Exception_Data Use_Error
;
884 #define Device_Error ada__io_exceptions__device_error
885 extern struct Exception_Data Device_Error
;
887 #define End_Error ada__io_exceptions__end_error
888 extern struct Exception_Data End_Error
;
890 #define Data_Error ada__io_exceptions__data_error
891 extern struct Exception_Data Data_Error
;
893 #define Layout_Error ada__io_exceptions__layout_error
894 extern struct Exception_Data Layout_Error
;
896 #define Non_Ada_Error system__aux_dec__non_ada_error
897 extern struct Exception_Data Non_Ada_Error
;
899 #define Coded_Exception system__vms_exception_table__coded_exception
900 extern struct Exception_Data
*Coded_Exception (Exception_Code
);
902 #define Base_Code_In system__vms_exception_table__base_code_in
903 extern Exception_Code
Base_Code_In (Exception_Code
);
905 /* DEC Ada exceptions are not defined in a header file, so they
908 #define ADA$_ALREADY_OPEN 0x0031a594
909 #define ADA$_CONSTRAINT_ERRO 0x00318324
910 #define ADA$_DATA_ERROR 0x003192c4
911 #define ADA$_DEVICE_ERROR 0x003195e4
912 #define ADA$_END_ERROR 0x00319904
913 #define ADA$_FAC_MODE_MISMAT 0x0031a8b3
914 #define ADA$_IOSYSFAILED 0x0031af04
915 #define ADA$_KEYSIZERR 0x0031aa3c
916 #define ADA$_KEY_MISMATCH 0x0031a8e3
917 #define ADA$_LAYOUT_ERROR 0x00319c24
918 #define ADA$_LINEXCMRS 0x0031a8f3
919 #define ADA$_MAXLINEXC 0x0031a8eb
920 #define ADA$_MODE_ERROR 0x00319f44
921 #define ADA$_MRN_MISMATCH 0x0031a8db
922 #define ADA$_MRS_MISMATCH 0x0031a8d3
923 #define ADA$_NAME_ERROR 0x0031a264
924 #define ADA$_NOT_OPEN 0x0031a58c
925 #define ADA$_ORG_MISMATCH 0x0031a8bb
926 #define ADA$_PROGRAM_ERROR 0x00318964
927 #define ADA$_RAT_MISMATCH 0x0031a8cb
928 #define ADA$_RFM_MISMATCH 0x0031a8c3
929 #define ADA$_STAOVF 0x00318cac
930 #define ADA$_STATUS_ERROR 0x0031a584
931 #define ADA$_STORAGE_ERROR 0x00318c84
932 #define ADA$_UNSUPPORTED 0x0031a8ab
933 #define ADA$_USE_ERROR 0x0031a8a4
935 /* DEC Ada specific conditions. */
936 static const struct cond_except dec_ada_cond_except_table
[] =
938 {ADA$_PROGRAM_ERROR
, &program_error
, 0, 0},
939 {ADA$_USE_ERROR
, &Use_Error
, 0, 0},
940 {ADA$_KEYSIZERR
, &program_error
, 0, 0},
941 {ADA$_STAOVF
, &storage_error
, 0, 0},
942 {ADA$_CONSTRAINT_ERRO
, &constraint_error
, 0, 0},
943 {ADA$_IOSYSFAILED
, &Device_Error
, 0, 0},
944 {ADA$_LAYOUT_ERROR
, &Layout_Error
, 0, 0},
945 {ADA$_STORAGE_ERROR
, &storage_error
, 0, 0},
946 {ADA$_DATA_ERROR
, &Data_Error
, 0, 0},
947 {ADA$_DEVICE_ERROR
, &Device_Error
, 0, 0},
948 {ADA$_END_ERROR
, &End_Error
, 0, 0},
949 {ADA$_MODE_ERROR
, &Mode_Error
, 0, 0},
950 {ADA$_NAME_ERROR
, &Name_Error
, 0, 0},
951 {ADA$_STATUS_ERROR
, &Status_Error
, 0, 0},
952 {ADA$_NOT_OPEN
, &Use_Error
, 0, 0},
953 {ADA$_ALREADY_OPEN
, &Use_Error
, 0, 0},
954 {ADA$_USE_ERROR
, &Use_Error
, 0, 0},
955 {ADA$_UNSUPPORTED
, &Use_Error
, 0, 0},
956 {ADA$_FAC_MODE_MISMAT
, &Use_Error
, 0, 0},
957 {ADA$_ORG_MISMATCH
, &Use_Error
, 0, 0},
958 {ADA$_RFM_MISMATCH
, &Use_Error
, 0, 0},
959 {ADA$_RAT_MISMATCH
, &Use_Error
, 0, 0},
960 {ADA$_MRS_MISMATCH
, &Use_Error
, 0, 0},
961 {ADA$_MRN_MISMATCH
, &Use_Error
, 0, 0},
962 {ADA$_KEY_MISMATCH
, &Use_Error
, 0, 0},
963 {ADA$_MAXLINEXC
, &constraint_error
, 0, 0},
964 {ADA$_LINEXCMRS
, &constraint_error
, 0, 0},
967 /* Already handled by a pragma Import_Exception
968 in Aux_IO_Exceptions */
969 {ADA$_LOCK_ERROR
, &Lock_Error
, 0, 0},
970 {ADA$_EXISTENCE_ERROR
, &Existence_Error
, 0, 0},
971 {ADA$_KEY_ERROR
, &Key_Error
, 0, 0},
979 /* Non-DEC Ada specific conditions that map to Ada exceptions. */
981 /* Subtest for ACCVIO Constraint_Error, kept for compatibility,
982 in hindsight should have just made ACCVIO == Storage_Error. */
983 #define ACCVIO_VIRTUAL_ADDR 3
984 static const struct cond_subtests accvio_c_e
=
985 {1, /* number of subtests below */
987 { ACCVIO_VIRTUAL_ADDR
, 0 }
991 /* Macro flag to adjust PC which gets off by one for some conditions,
992 not sure if this is reliably true, PC could be off by more for
993 HPARITH for example, unless a trapb is inserted. */
994 #define NEEDS_ADJUST 1
996 static const struct cond_except system_cond_except_table
[] =
998 {MTH$_FLOOVEMAT
, &constraint_error
, 0, 0},
999 {SS$_INTDIV
, &constraint_error
, 0, 0},
1000 {SS$_HPARITH
, &constraint_error
, NEEDS_ADJUST
, 0},
1001 {SS$_ACCVIO
, &constraint_error
, NEEDS_ADJUST
, &accvio_c_e
},
1002 {SS$_ACCVIO
, &storage_error
, NEEDS_ADJUST
, 0},
1003 {SS$_STKOVF
, &storage_error
, NEEDS_ADJUST
, 0},
1007 /* To deal with VMS conditions and their mapping to Ada exceptions,
1008 the __gnat_error_handler routine below is installed as an exception
1009 vector having precedence over DEC frame handlers. Some conditions
1010 still need to be handled by such handlers, however, in which case
1011 __gnat_error_handler needs to return SS$_RESIGNAL. Consider for
1012 instance the use of a third party library compiled with DECAda and
1013 performing its own exception handling internally.
1015 To allow some user-level flexibility, which conditions should be
1016 resignaled is controlled by a predicate function, provided with the
1017 condition value and returning a boolean indication stating whether
1018 this condition should be resignaled or not.
1020 That predicate function is called indirectly, via a function pointer,
1021 by __gnat_error_handler, and changing that pointer is allowed to the
1022 user code by way of the __gnat_set_resignal_predicate interface.
1024 The user level function may then implement what it likes, including
1025 for instance the maintenance of a dynamic data structure if the set
1026 of to be resignalled conditions has to change over the program's
1029 ??? This is not a perfect solution to deal with the possible
1030 interactions between the GNAT and the DECAda exception handling
1031 models and better (more general) schemes are studied. This is so
1032 just provided as a convenient workaround in the meantime, and
1033 should be use with caution since the implementation has been kept
1037 resignal_predicate (int code
);
1039 static const int * const cond_resignal_table
[] =
1042 (int *)CMA$_EXIT_THREAD
,
1046 (int *) RDB$_STREAM_EOF
,
1047 (int *) FDL$_UNPRIKW
,
1051 static const int facility_resignal_table
[] =
1053 0x1380000, /* RDB */
1054 0x2220000, /* SQL */
1058 /* Default GNAT predicate for resignaling conditions. */
1061 __gnat_default_resignal_p (int code
)
1065 for (i
= 0; facility_resignal_table
[i
]; i
++)
1066 if ((code
& FAC_MASK
) == facility_resignal_table
[i
])
1069 for (i
= 0, iexcept
= 0;
1070 cond_resignal_table
[i
]
1071 && !(iexcept
= LIB$
MATCH_COND (&code
, &cond_resignal_table
[i
]));
1077 /* Static pointer to predicate that the __gnat_error_handler exception
1078 vector invokes to determine if it should resignal a condition. */
1080 static resignal_predicate
*__gnat_resignal_p
= __gnat_default_resignal_p
;
1082 /* User interface to change the predicate pointer to PREDICATE. Reset to
1083 the default if PREDICATE is null. */
1086 __gnat_set_resignal_predicate (resignal_predicate
*predicate
)
1088 if (predicate
== NULL
)
1089 __gnat_resignal_p
= __gnat_default_resignal_p
;
1091 __gnat_resignal_p
= predicate
;
1094 /* Should match System.Parameters.Default_Exception_Msg_Max_Length. */
1095 #define Default_Exception_Msg_Max_Length 512
1097 /* Action routine for SYS$PUTMSG. There may be multiple
1098 conditions, each with text to be appended to MESSAGE
1099 and separated by line termination. */
1101 copy_msg (struct descriptor_s
*msgdesc
, char *message
)
1103 int len
= strlen (message
);
1106 /* Check for buffer overflow and skip. */
1107 if (len
> 0 && len
<= Default_Exception_Msg_Max_Length
- 3)
1109 strcat (message
, "\r\n");
1113 /* Check for buffer overflow and truncate if necessary. */
1114 copy_len
= (len
+ msgdesc
->len
<= Default_Exception_Msg_Max_Length
- 1 ?
1116 Default_Exception_Msg_Max_Length
- 1 - len
);
1117 strncpy (&message
[len
], msgdesc
->adr
, copy_len
);
1118 message
[len
+ copy_len
] = 0;
1123 /* Scan TABLE for a match for the condition contained in SIGARGS,
1124 and return the entry, or the empty entry if no match found. */
1125 static const struct cond_except
*
1126 scan_conditions ( int *sigargs
, const struct cond_except
*table
[])
1129 struct cond_except entry
;
1131 /* Scan the exception condition table for a match and fetch
1132 the associated GNAT exception pointer. */
1133 for (i
= 0; (*table
) [i
].cond
; i
++)
1135 unsigned int match
= LIB$
MATCH_COND (&sigargs
[1], &(*table
) [i
].cond
);
1136 const struct cond_subtests
*subtests
= (*table
) [i
].subtests
;
1142 return &(*table
) [i
];
1147 int num
= (*subtests
).num
;
1149 /* Perform subtests to differentiate exception. */
1150 for (ii
= 0; ii
< num
; ii
++)
1152 unsigned int arg
= (*subtests
).sigargs
[ii
].sigarg
;
1153 unsigned int argval
= (*subtests
).sigargs
[ii
].sigargval
;
1155 if (sigargs
[arg
] != argval
)
1162 /* All subtests passed. */
1163 if (num
== (*subtests
).num
)
1164 return &(*table
) [i
];
1169 /* No match, return the null terminating entry. */
1170 return &(*table
) [i
];
1173 /* __gnat_handle_vms_condtition is both a frame based handler
1174 for the runtime, and an exception vector for the compiler. */
1176 __gnat_handle_vms_condition (int *sigargs
, void *mechargs
)
1178 struct Exception_Data
*exception
= 0;
1179 unsigned int needs_adjust
= 0;
1180 Exception_Code base_code
;
1181 struct descriptor_s gnat_facility
= {4, 0, "GNAT"};
1182 char message
[Default_Exception_Msg_Max_Length
];
1184 const char *msg
= "";
1186 /* Check for conditions to resignal which aren't effected by pragma
1187 Import_Exception. */
1188 if (__gnat_resignal_p (sigargs
[1]))
1189 return SS$_RESIGNAL
;
1191 /* toplev.c handles this for compiler. */
1192 if (sigargs
[1] == SS$_HPARITH
)
1193 return SS$_RESIGNAL
;
1197 /* See if it's an imported exception. Beware that registered exceptions
1198 are bound to their base code, with the severity bits masked off. */
1199 base_code
= Base_Code_In ((Exception_Code
) sigargs
[1]);
1200 exception
= Coded_Exception (base_code
);
1207 struct cond_except cond
;
1208 const struct cond_except
*cond_table
;
1209 const struct cond_except
*cond_tables
[] = {dec_ada_cond_except_table
,
1210 system_cond_except_table
,
1212 unsigned int ctrlc
= SS$_CONTROLC
;
1213 unsigned int *sigint
= &C$_SIGINT
;
1214 int ctrlc_match
= LIB$
MATCH_COND (&sigargs
[1], &ctrlc
);
1215 int sigint_match
= LIB$
MATCH_COND (&sigargs
[1], &sigint
);
1217 extern int SYS$
DCLAST (void (*astadr
)(), unsigned long long astprm
,
1218 unsigned int acmode
);
1220 /* If SS$_CONTROLC has been imported as an exception, it will take
1221 priority over a a Ctrl/C handler. See above. SIGINT has a
1222 different condition value due to it's DECCCRTL roots and it's
1223 the condition that gets raised for a "kill -INT". */
1224 if ((ctrlc_match
|| sigint_match
) && __gnat_ctrl_c_handler
)
1226 SYS$
DCLAST (__gnat_ctrl_c_handler
, 0, 0);
1227 return SS$_CONTINUE
;
1231 while ((cond_table
= cond_tables
[i
++]) && !exception
)
1233 cond
= *scan_conditions (sigargs
, &cond_table
);
1234 exception
= (struct Exception_Data
*) cond
.except
;
1238 needs_adjust
= cond
.needs_adjust
;
1240 /* User programs expect Non_Ada_Error to be raised if no match,
1241 reference DEC Ada test CXCONDHAN. */
1242 exception
= &Non_Ada_Error
;
1246 /* Pretty much everything is just a program error in the compiler */
1247 exception
= &program_error
;
1252 /* Subtract PC & PSL fields as per ABI for SYS$PUTMSG. */
1255 extern int SYS$
PUTMSG (void *, int (*)(), void *, unsigned long long);
1257 /* If it was a DEC Ada specific condtiion, make it GNAT otherwise
1258 keep the old facility. */
1259 if (sigargs
[1] & FAC_MASK
== DECADA_M_FACILITY
)
1260 SYS$
PUTMSG (sigargs
, copy_msg
, &gnat_facility
,
1261 (unsigned long long ) message
);
1263 SYS$
PUTMSG (sigargs
, copy_msg
, 0,
1264 (unsigned long long ) message
);
1266 /* Add back PC & PSL fields as per ABI for SYS$PUTMSG. */
1271 __gnat_adjust_context_for_raise (sigargs
[1], (void *)mechargs
);
1273 Raise_From_Signal_Handler (exception
, msg
);
1276 #if defined (IN_RTS) && defined (__IA64)
1277 /* Called only from adasigio.b32. This is a band aid to avoid going
1278 through the VMS signal handling code which results in a 0x8000 per
1279 handled exception memory leak in P2 space (see VMS source listing
1280 sys/lis/exception.lis) due to the allocation of working space that
1281 is expected to be deallocated upon return from the condition handler,
1282 which doesn't return in GNAT compiled code. */
1284 GNAT$
STOP (int *sigargs
)
1286 /* Note that there are no mechargs. We rely on the fact that condtions
1287 raised from DEClib I/O do not require an "adjust". Also the count
1288 will be off by 2, since LIB$STOP didn't get a chance to add the
1289 PC and PSL fields, so we bump it so PUTMSG comes out right. */
1291 __gnat_handle_vms_condition (sigargs
, 0);
1296 __gnat_install_handler (void)
1298 long prvhnd ATTRIBUTE_UNUSED
;
1300 #if !defined (IN_RTS)
1301 extern int SYS$
SETEXV (unsigned int vector
, int (*addres
)(),
1302 unsigned int accmode
, void *(*(prvhnd
)));
1303 SYS$
SETEXV (1, __gnat_handle_vms_condition
, 3, &prvhnd
);
1306 __gnat_handler_installed
= 1;
1309 /* __gnat_adjust_context_for_raise for Alpha - see comments along with the
1310 default version later in this file. */
1312 #if defined (IN_RTS) && defined (__alpha__)
1314 #include <vms/chfctxdef.h>
1315 #include <vms/chfdef.h>
1317 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
1320 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED
, void *ucontext
)
1322 if (signo
== SS$_HPARITH
)
1324 /* Sub one to the address of the instruction signaling the condition,
1325 located in the sigargs array. */
1327 CHF$MECH_ARRAY
* mechargs
= (CHF$MECH_ARRAY
*) ucontext
;
1328 CHF$SIGNAL_ARRAY
* sigargs
1329 = (CHF$SIGNAL_ARRAY
*) mechargs
->chf$q_mch_sig_addr
;
1331 int vcount
= sigargs
->chf$is_sig_args
;
1332 int * pc_slot
= & (&sigargs
->chf$l_sig_name
)[vcount
-2];
1340 /* __gnat_adjust_context_for_raise for ia64. */
1342 #if defined (IN_RTS) && defined (__IA64)
1344 #include <vms/chfctxdef.h>
1345 #include <vms/chfdef.h>
1347 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
1349 typedef unsigned long long u64
;
1352 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED
, void *ucontext
)
1354 /* Add one to the address of the instruction signaling the condition,
1355 located in the 64bits sigargs array. */
1357 CHF$MECH_ARRAY
* mechargs
= (CHF$MECH_ARRAY
*) ucontext
;
1359 CHF64$SIGNAL_ARRAY
*chfsig64
1360 = (CHF64$SIGNAL_ARRAY
*) mechargs
->chf$ph_mch_sig64_addr
;
1363 = (u64
*)chfsig64
+ 1 + chfsig64
->chf64$l_sig_args
;
1365 u64
* ih_pc_loc
= post_sigarray
- 2;
1372 /* Easier interface for LIB$GET_LOGICAL: put the equivalence of NAME into BUF,
1373 always NUL terminated. In case of error or if the result is longer than
1374 LEN (length of BUF) an empty string is written info BUF. */
1377 __gnat_vms_get_logical (const char *name
, char *buf
, int len
)
1379 struct descriptor_s name_desc
, result_desc
;
1381 unsigned short rlen
;
1383 /* Build the descriptor for NAME. */
1384 name_desc
.len
= strlen (name
);
1386 name_desc
.adr
= (char *)name
;
1388 /* Build the descriptor for the result. */
1389 result_desc
.len
= len
;
1390 result_desc
.mbz
= 0;
1391 result_desc
.adr
= buf
;
1393 status
= LIB$
GET_LOGICAL (&name_desc
, &result_desc
, &rlen
);
1395 if ((status
& 1) == 1 && rlen
< len
)
1401 /* Size of a page on ia64 and alpha VMS. */
1402 #define VMS_PAGESIZE 8192
1405 #define PSL__C_USER 3
1410 /* Descending region. */
1411 #define VA__M_DESCEND 1
1413 /* Get by virtual address. */
1414 #define VA___REGSUM_BY_VA 1
1416 /* Memory region summary. */
1419 unsigned long long q_region_id
;
1420 unsigned int l_flags
;
1421 unsigned int l_region_protection
;
1423 unsigned long long q_region_size
;
1424 void *pq_first_free_va
;
1427 extern int SYS$
GET_REGION_INFO (unsigned int, unsigned long long *,
1428 void *, void *, unsigned int,
1429 void *, unsigned int *);
1430 extern int SYS$
EXPREG_64 (unsigned long long *, unsigned long long,
1431 unsigned int, unsigned int, void **,
1432 unsigned long long *);
1433 extern int SYS$
SETPRT_64 (void *, unsigned long long, unsigned int,
1434 unsigned int, void **, unsigned long long *,
1437 /* Add a guard page in the memory region containing ADDR at ADDR +/- SIZE.
1438 (The sign depends on the kind of the memory region). */
1441 __gnat_set_stack_guard_page (void *addr
, unsigned long size
)
1445 unsigned long long ret_len
;
1446 unsigned int ret_prot
;
1448 unsigned long long length
;
1449 unsigned int retlen
;
1450 struct regsum buffer
;
1452 /* Get the region for ADDR. */
1453 status
= SYS$GET_REGION_INFO
1454 (VA___REGSUM_BY_VA
, NULL
, addr
, NULL
, sizeof (buffer
), &buffer
, &retlen
);
1456 if ((status
& 1) != 1)
1459 /* Extend the region. */
1460 status
= SYS$
EXPREG_64 (&buffer
.q_region_id
,
1461 size
, 0, 0, &start_va
, &length
);
1463 if ((status
& 1) != 1)
1466 /* Create a guard page. */
1467 if (!(buffer
.l_flags
& VA__M_DESCEND
))
1468 start_va
= (void *)((unsigned long long)start_va
+ length
- VMS_PAGESIZE
);
1470 status
= SYS$
SETPRT_64 (start_va
, VMS_PAGESIZE
, PSL__C_USER
, PRT__C_NA
,
1471 &ret_va
, &ret_len
, &ret_prot
);
1473 if ((status
& 1) != 1)
1478 /* Read logicals to limit the stack(s) size. */
1481 __gnat_set_stack_limit (void)
1489 /* The main stack. */
1490 __gnat_vms_get_logical ("GNAT_STACK_SIZE", value
, sizeof (value
));
1491 size
= strtoul (value
, &e
, 0);
1492 if (e
> value
&& *e
== 0)
1494 asm ("mov %0=sp" : "=r" (sp
));
1495 __gnat_set_stack_guard_page (sp
, size
* 1024);
1498 /* The register stack. */
1499 __gnat_vms_get_logical ("GNAT_RBS_SIZE", value
, sizeof (value
));
1500 size
= strtoul (value
, &e
, 0);
1501 if (e
> value
&& *e
== 0)
1503 asm ("mov %0=ar.bsp" : "=r" (sp
));
1504 __gnat_set_stack_guard_page (sp
, size
* 1024);
1509 /* Feature logical name and global variable address pair.
1510 If we ever add another feature logical to this list, the
1511 feature struct will need to be enhanced to take into account
1512 possible values for *gl_addr. */
1518 /* Default values for GNAT features set by environment. */
1519 int __gl_heap_size
= 64;
1521 /* Array feature logical names and global variable addresses. */
1522 static const struct feature features
[] =
1524 {"GNAT$NO_MALLOC_64", &__gl_heap_size
},
1529 __gnat_set_features (void)
1534 /* Loop through features array and test name for enable/disable. */
1535 for (i
= 0; features
[i
].name
; i
++)
1537 __gnat_vms_get_logical (features
[i
].name
, buff
, sizeof (buff
));
1539 if (strcmp (buff
, "ENABLE") == 0
1540 || strcmp (buff
, "TRUE") == 0
1541 || strcmp (buff
, "1") == 0)
1542 *features
[i
].gl_addr
= 32;
1543 else if (strcmp (buff
, "DISABLE") == 0
1544 || strcmp (buff
, "FALSE") == 0
1545 || strcmp (buff
, "0") == 0)
1546 *features
[i
].gl_addr
= 64;
1549 /* Features to artificially limit the stack size. */
1550 __gnat_set_stack_limit ();
1552 __gnat_features_set
= 1;
1555 /* Return true if the VMS version is 7.x. */
1557 extern unsigned int LIB$
GETSYI (int *, ...);
1559 #define SYI$_VERSION 0x1000
1562 __gnat_is_vms_v7 (void)
1564 struct descriptor_s desc
;
1567 int code
= SYI$_VERSION
;
1569 desc
.len
= sizeof (version
);
1573 status
= LIB$
GETSYI (&code
, 0, &desc
);
1574 if ((status
& 1) == 1 && version
[1] == '7' && version
[2] == '.')
1580 /*******************/
1581 /* FreeBSD Section */
1582 /*******************/
1584 #elif defined (__FreeBSD__)
1587 #include <sys/ucontext.h>
1591 __gnat_error_handler (int sig
,
1592 siginfo_t
*si ATTRIBUTE_UNUSED
,
1593 void *ucontext ATTRIBUTE_UNUSED
)
1595 struct Exception_Data
*exception
;
1601 exception
= &constraint_error
;
1606 exception
= &constraint_error
;
1611 exception
= &storage_error
;
1612 msg
= "stack overflow or erroneous memory access";
1616 exception
= &storage_error
;
1617 msg
= "SIGBUS: possible stack overflow";
1621 exception
= &program_error
;
1622 msg
= "unhandled signal";
1625 Raise_From_Signal_Handler (exception
, msg
);
1629 __gnat_install_handler ()
1631 struct sigaction act
;
1633 /* Set up signal handler to map synchronous signals to appropriate
1634 exceptions. Make sure that the handler isn't interrupted by another
1635 signal that might cause a scheduling event! */
1638 = (void (*)(int, struct __siginfo
*, void*)) __gnat_error_handler
;
1639 act
.sa_flags
= SA_NODEFER
| SA_RESTART
| SA_SIGINFO
;
1640 (void) sigemptyset (&act
.sa_mask
);
1642 (void) sigaction (SIGILL
, &act
, NULL
);
1643 (void) sigaction (SIGFPE
, &act
, NULL
);
1644 (void) sigaction (SIGSEGV
, &act
, NULL
);
1645 (void) sigaction (SIGBUS
, &act
, NULL
);
1647 __gnat_handler_installed
= 1;
1650 /*******************/
1651 /* VxWorks Section */
1652 /*******************/
1654 #elif defined(__vxworks)
1657 #include <taskLib.h>
1665 #include "private/vThreadsP.h"
1668 void __gnat_error_handler (int, void *, struct sigcontext
*);
1672 /* Directly vectored Interrupt routines are not supported when using RTPs. */
1674 extern int __gnat_inum_to_ivec (int);
1676 /* This is needed by the GNAT run time to handle Vxworks interrupts. */
1678 __gnat_inum_to_ivec (int num
)
1680 return INUM_TO_IVEC (num
);
1684 #if !defined(__alpha_vxworks) && (_WRS_VXWORKS_MAJOR != 6) && !defined(__RTP__)
1686 /* getpid is used by s-parint.adb, but is not defined by VxWorks, except
1687 on Alpha VxWorks and VxWorks 6.x (including RTPs). */
1689 extern long getpid (void);
1694 return taskIdSelf ();
1698 /* VxWorks 653 vThreads expects the field excCnt to be zeroed when a signal is.
1699 handled. The VxWorks version of longjmp does this; GCC's builtin_longjmp
1702 __gnat_clear_exception_count (void)
1705 WIND_TCB
*currentTask
= (WIND_TCB
*) taskIdSelf();
1707 currentTask
->vThreads
.excCnt
= 0;
1711 /* Handle different SIGnal to exception mappings in different VxWorks
1714 __gnat_map_signal (int sig
, void *si ATTRIBUTE_UNUSED
,
1715 struct sigcontext
*sc ATTRIBUTE_UNUSED
)
1717 struct Exception_Data
*exception
;
1723 exception
= &constraint_error
;
1727 #ifdef __VXWORKSMILS__
1729 exception
= &storage_error
;
1730 msg
= "SIGILL: possible stack overflow";
1733 exception
= &storage_error
;
1737 exception
= &program_error
;
1742 exception
= &constraint_error
;
1743 msg
= "Floating point exception or SIGILL";
1746 exception
= &storage_error
;
1750 exception
= &storage_error
;
1751 msg
= "SIGBUS: possible stack overflow";
1754 #elif (_WRS_VXWORKS_MAJOR == 6)
1756 exception
= &constraint_error
;
1760 /* In RTP mode a SIGSEGV is most likely due to a stack overflow,
1761 since stack checking uses the probing mechanism. */
1763 exception
= &storage_error
;
1764 msg
= "SIGSEGV: possible stack overflow";
1767 exception
= &program_error
;
1771 /* VxWorks 6 kernel mode with probing. SIGBUS for guard page hit */
1773 exception
= &storage_error
;
1777 exception
= &storage_error
;
1778 msg
= "SIGBUS: possible stack overflow";
1782 /* VxWorks 5: a SIGILL is most likely due to a stack overflow,
1783 since stack checking uses the stack limit mechanism. */
1785 exception
= &storage_error
;
1786 msg
= "SIGILL: possible stack overflow";
1789 exception
= &storage_error
;
1793 exception
= &program_error
;
1798 exception
= &program_error
;
1799 msg
= "unhandled signal";
1802 __gnat_clear_exception_count ();
1803 Raise_From_Signal_Handler (exception
, msg
);
1806 /* Tasking and Non-tasking signal handler. Map SIGnal to Ada exception
1807 propagation after the required low level adjustments. */
1810 __gnat_error_handler (int sig
, void *si
, struct sigcontext
*sc
)
1814 /* VxWorks will always mask out the signal during the signal handler and
1815 will reenable it on a longjmp. GNAT does not generate a longjmp to
1816 return from a signal handler so the signal will still be masked unless
1818 sigprocmask (SIG_SETMASK
, NULL
, &mask
);
1819 sigdelset (&mask
, sig
);
1820 sigprocmask (SIG_SETMASK
, &mask
, NULL
);
1822 #if defined (__PPC__) && defined(_WRS_KERNEL)
1823 /* On PowerPC, kernel mode, we process signals through a Call Frame Info
1824 trampoline, voiding the need for myriads of fallback_frame_state
1825 variants in the ZCX runtime. We have no simple way to distinguish ZCX
1826 from SJLJ here, so we do this for SJLJ as well even though this is not
1827 necessary. This only incurs a few extra instructions and a tiny
1828 amount of extra stack usage. */
1830 #include "sigtramp.h"
1832 __gnat_sigtramp (sig
, (void *)si
, (void *)sc
,
1833 (sighandler_t
*)&__gnat_map_signal
);
1836 __gnat_map_signal (sig
, si
, sc
);
1840 #if defined(__leon__) && defined(_WRS_KERNEL)
1841 /* For LEON VxWorks we need to install a trap handler for stack overflow */
1843 extern void excEnt (void);
1844 /* VxWorks exception handler entry */
1847 unsigned long inst_first
;
1848 unsigned long inst_second
;
1849 unsigned long inst_third
;
1850 unsigned long inst_fourth
;
1852 /* Four instructions representing entries in the trap table */
1854 struct trap_entry
*trap_0_entry
;
1855 /* We will set the location of the entry for software trap 0 in the trap
1860 __gnat_install_handler (void)
1862 struct sigaction act
;
1864 /* Setup signal handler to map synchronous signals to appropriate
1865 exceptions. Make sure that the handler isn't interrupted by another
1866 signal that might cause a scheduling event! */
1868 act
.sa_handler
= __gnat_error_handler
;
1869 act
.sa_flags
= SA_SIGINFO
| SA_ONSTACK
;
1870 sigemptyset (&act
.sa_mask
);
1872 /* For VxWorks, install all signal handlers, since pragma Interrupt_State
1873 applies to vectored hardware interrupts, not signals. */
1874 sigaction (SIGFPE
, &act
, NULL
);
1875 sigaction (SIGILL
, &act
, NULL
);
1876 sigaction (SIGSEGV
, &act
, NULL
);
1877 sigaction (SIGBUS
, &act
, NULL
);
1879 #if defined(__leon__) && defined(_WRS_KERNEL)
1880 /* Specific to the LEON VxWorks kernel run-time library */
1882 /* For stack checking the compiler triggers a software trap 0 (ta 0) in
1883 case of overflow (we use the stack limit mechanism). We need to install
1884 the trap handler here for this software trap (the OS does not handle
1885 it) as if it were a data_access_exception (trap 9). We do the same as
1886 if we put in the trap table a VXSPARC_BAD_TRAP(9). Software trap 0 is
1887 located at vector 0x80, and each entry takes 4 words. */
1889 trap_0_entry
= (struct trap_entry
*)(intVecBaseGet () + 0x80 * 4);
1893 trap_0_entry
->inst_first
= 0xae102000 + 9;
1895 /* sethi %hi(excEnt), %l6 */
1897 /* The 22 most significant bits of excEnt are obtained shifting 10 times
1900 trap_0_entry
->inst_second
= 0x2d000000 + ((unsigned long)excEnt
>> 10);
1902 /* jmp %l6+%lo(excEnt) */
1904 /* The 10 least significant bits of excEnt are obtained by masking */
1906 trap_0_entry
->inst_third
= 0x81c5a000 + ((unsigned long)excEnt
& 0x3ff);
1910 trap_0_entry
->inst_fourth
= 0xa1480000;
1913 __gnat_handler_installed
= 1;
1916 #define HAVE_GNAT_INIT_FLOAT
1919 __gnat_init_float (void)
1921 /* Disable overflow/underflow exceptions on the PPC processor, needed
1922 to get correct Ada semantics. Note that for AE653 vThreads, the HW
1923 overflow settings are an OS configuration issue. The instructions
1924 below have no effect. */
1925 #if defined (_ARCH_PPC) && !defined (_SOFT_FLOAT) && (!defined (VTHREADS) || defined (__VXWORKSMILS__))
1926 #if defined (__SPE__)
1928 const unsigned long spefscr_mask
= 0xfffffff3;
1929 unsigned long spefscr
;
1930 asm ("mfspr %0, 512" : "=r" (spefscr
));
1931 spefscr
= spefscr
& spefscr_mask
;
1932 asm ("mtspr 512, %0\n\tisync" : : "r" (spefscr
));
1940 #if (defined (__i386__) || defined (i386)) && !defined (VTHREADS)
1941 /* This is used to properly initialize the FPU on an x86 for each
1946 /* Similarly for SPARC64. Achieved by masking bits in the Trap Enable Mask
1947 field of the Floating-point Status Register (see the SPARC Architecture
1948 Manual Version 9, p 48). */
1949 #if defined (sparc64)
1951 #define FSR_TEM_NVM (1 << 27) /* Invalid operand */
1952 #define FSR_TEM_OFM (1 << 26) /* Overflow */
1953 #define FSR_TEM_UFM (1 << 25) /* Underflow */
1954 #define FSR_TEM_DZM (1 << 24) /* Division by Zero */
1955 #define FSR_TEM_NXM (1 << 23) /* Inexact result */
1959 __asm__("st %%fsr, %0" : "=m" (fsr
));
1960 fsr
&= ~(FSR_TEM_OFM
| FSR_TEM_UFM
);
1961 __asm__("ld %0, %%fsr" : : "m" (fsr
));
1966 /* This subprogram is called by System.Task_Primitives.Operations.Enter_Task
1967 (if not null) when a new task is created. It is initialized by
1968 System.Stack_Checking.Operations.Initialize_Stack_Limit.
1969 The use of a hook avoids to drag stack checking subprograms if stack
1970 checking is not used. */
1971 void (*__gnat_set_stack_limit_hook
)(void) = (void (*)(void))0;
1973 /******************/
1974 /* NetBSD Section */
1975 /******************/
1977 #elif defined(__NetBSD__)
1983 __gnat_error_handler (int sig
)
1985 struct Exception_Data
*exception
;
1991 exception
= &constraint_error
;
1995 exception
= &constraint_error
;
1999 exception
= &storage_error
;
2000 msg
= "stack overflow or erroneous memory access";
2003 exception
= &constraint_error
;
2007 exception
= &program_error
;
2008 msg
= "unhandled signal";
2011 Raise_From_Signal_Handler(exception
, msg
);
2015 __gnat_install_handler(void)
2017 struct sigaction act
;
2019 act
.sa_handler
= __gnat_error_handler
;
2020 act
.sa_flags
= SA_NODEFER
| SA_RESTART
;
2021 sigemptyset (&act
.sa_mask
);
2023 /* Do not install handlers if interrupt state is "System". */
2024 if (__gnat_get_interrupt_state (SIGFPE
) != 's')
2025 sigaction (SIGFPE
, &act
, NULL
);
2026 if (__gnat_get_interrupt_state (SIGILL
) != 's')
2027 sigaction (SIGILL
, &act
, NULL
);
2028 if (__gnat_get_interrupt_state (SIGSEGV
) != 's')
2029 sigaction (SIGSEGV
, &act
, NULL
);
2030 if (__gnat_get_interrupt_state (SIGBUS
) != 's')
2031 sigaction (SIGBUS
, &act
, NULL
);
2033 __gnat_handler_installed
= 1;
2036 /*******************/
2037 /* OpenBSD Section */
2038 /*******************/
2040 #elif defined(__OpenBSD__)
2046 __gnat_error_handler (int sig
)
2048 struct Exception_Data
*exception
;
2054 exception
= &constraint_error
;
2058 exception
= &constraint_error
;
2062 exception
= &storage_error
;
2063 msg
= "stack overflow or erroneous memory access";
2066 exception
= &constraint_error
;
2070 exception
= &program_error
;
2071 msg
= "unhandled signal";
2074 Raise_From_Signal_Handler(exception
, msg
);
2078 __gnat_install_handler(void)
2080 struct sigaction act
;
2082 act
.sa_handler
= __gnat_error_handler
;
2083 act
.sa_flags
= SA_NODEFER
| SA_RESTART
;
2084 sigemptyset (&act
.sa_mask
);
2086 /* Do not install handlers if interrupt state is "System" */
2087 if (__gnat_get_interrupt_state (SIGFPE
) != 's')
2088 sigaction (SIGFPE
, &act
, NULL
);
2089 if (__gnat_get_interrupt_state (SIGILL
) != 's')
2090 sigaction (SIGILL
, &act
, NULL
);
2091 if (__gnat_get_interrupt_state (SIGSEGV
) != 's')
2092 sigaction (SIGSEGV
, &act
, NULL
);
2093 if (__gnat_get_interrupt_state (SIGBUS
) != 's')
2094 sigaction (SIGBUS
, &act
, NULL
);
2096 __gnat_handler_installed
= 1;
2099 /******************/
2100 /* Darwin Section */
2101 /******************/
2103 #elif defined(__APPLE__)
2107 #include <sys/syscall.h>
2108 #include <sys/sysctl.h>
2109 #include <mach/mach_vm.h>
2110 #include <mach/mach_init.h>
2111 #include <mach/vm_statistics.h>
2113 /* This must be in keeping with System.OS_Interface.Alternate_Stack_Size. */
2114 char __gnat_alternate_stack
[32 * 1024]; /* 1 * MINSIGSTKSZ */
2116 /* Defined in xnu unix_signal.c.
2117 Tell the kernel to re-use alt stack when delivering a signal. */
2118 #define UC_RESET_ALT_STACK 0x80000000
2120 /* Return true if ADDR is within a stack guard area. */
2122 __gnat_is_stack_guard (mach_vm_address_t addr
)
2125 vm_region_submap_info_data_64_t info
;
2126 mach_vm_address_t start
;
2127 mach_vm_size_t size
;
2129 mach_msg_type_number_t count
;
2131 count
= VM_REGION_SUBMAP_INFO_COUNT_64
;
2135 kret
= mach_vm_region_recurse (mach_task_self (), &start
, &size
, &depth
,
2136 (vm_region_recurse_info_t
) &info
, &count
);
2137 if (kret
== KERN_SUCCESS
2138 && addr
>= start
&& addr
< (start
+ size
)
2139 && info
.protection
== VM_PROT_NONE
2140 && info
.user_tag
== VM_MEMORY_STACK
)
2145 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
2147 #if defined (__x86_64__)
2149 __darwin_major_version (void)
2151 static int cache
= -1;
2154 int mib
[2] = {CTL_KERN
, KERN_OSRELEASE
};
2157 /* Find out how big the buffer needs to be (and set cache to 0
2159 if (sysctl (mib
, 2, NULL
, &len
, NULL
, 0) == 0)
2162 sysctl (mib
, 2, release
, &len
, NULL
, 0);
2163 /* Darwin releases are of the form L.M.N where L is the major
2164 version, so strtol will return L. */
2165 cache
= (int) strtol (release
, NULL
, 10);
2177 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED
,
2178 void *ucontext ATTRIBUTE_UNUSED
)
2180 #if defined (__x86_64__)
2181 if (__darwin_major_version () < 12)
2183 /* Work around radar #10302855, where the unwinders (libunwind or
2184 libgcc_s depending on the system revision) and the DWARF unwind
2185 data for sigtramp have different ideas about register numbering,
2186 causing rbx and rdx to be transposed. */
2187 ucontext_t
*uc
= (ucontext_t
*)ucontext
;
2188 unsigned long t
= uc
->uc_mcontext
->__ss
.__rbx
;
2190 uc
->uc_mcontext
->__ss
.__rbx
= uc
->uc_mcontext
->__ss
.__rdx
;
2191 uc
->uc_mcontext
->__ss
.__rdx
= t
;
2197 __gnat_error_handler (int sig
, siginfo_t
*si
, void *ucontext
)
2199 struct Exception_Data
*exception
;
2202 __gnat_adjust_context_for_raise (sig
, ucontext
);
2208 if (__gnat_is_stack_guard ((unsigned long)si
->si_addr
))
2210 exception
= &storage_error
;
2211 msg
= "stack overflow";
2215 exception
= &constraint_error
;
2216 msg
= "erroneous memory access";
2218 /* Reset the use of alt stack, so that the alt stack will be used
2219 for the next signal delivery.
2220 The stack can't be used in case of stack checking. */
2221 syscall (SYS_sigreturn
, NULL
, UC_RESET_ALT_STACK
);
2225 exception
= &constraint_error
;
2230 exception
= &program_error
;
2231 msg
= "unhandled signal";
2234 Raise_From_Signal_Handler (exception
, msg
);
2238 __gnat_install_handler (void)
2240 struct sigaction act
;
2242 /* Set up signal handler to map synchronous signals to appropriate
2243 exceptions. Make sure that the handler isn't interrupted by another
2244 signal that might cause a scheduling event! Also setup an alternate
2245 stack region for the handler execution so that stack overflows can be
2246 handled properly, avoiding a SEGV generation from stack usage by the
2247 handler itself (and it is required by Darwin). */
2250 stack
.ss_sp
= __gnat_alternate_stack
;
2251 stack
.ss_size
= sizeof (__gnat_alternate_stack
);
2253 sigaltstack (&stack
, NULL
);
2255 act
.sa_flags
= SA_NODEFER
| SA_RESTART
| SA_SIGINFO
;
2256 act
.sa_sigaction
= __gnat_error_handler
;
2257 sigemptyset (&act
.sa_mask
);
2259 /* Do not install handlers if interrupt state is "System". */
2260 if (__gnat_get_interrupt_state (SIGABRT
) != 's')
2261 sigaction (SIGABRT
, &act
, NULL
);
2262 if (__gnat_get_interrupt_state (SIGFPE
) != 's')
2263 sigaction (SIGFPE
, &act
, NULL
);
2264 if (__gnat_get_interrupt_state (SIGILL
) != 's')
2265 sigaction (SIGILL
, &act
, NULL
);
2267 act
.sa_flags
|= SA_ONSTACK
;
2268 if (__gnat_get_interrupt_state (SIGSEGV
) != 's')
2269 sigaction (SIGSEGV
, &act
, NULL
);
2270 if (__gnat_get_interrupt_state (SIGBUS
) != 's')
2271 sigaction (SIGBUS
, &act
, NULL
);
2273 __gnat_handler_installed
= 1;
2278 /* For all other versions of GNAT, the handler does nothing. */
2280 /*******************/
2281 /* Default Section */
2282 /*******************/
2285 __gnat_install_handler (void)
2287 __gnat_handler_installed
= 1;
2292 /*********************/
2293 /* __gnat_init_float */
2294 /*********************/
2296 /* This routine is called as each process thread is created, for possible
2297 initialization of the FP processor. This version is used under INTERIX
2300 #if defined (_WIN32) || defined (__INTERIX) \
2301 || defined (__Lynx__) || defined(__NetBSD__) || defined(__FreeBSD__) \
2302 || defined (__OpenBSD__)
2304 #define HAVE_GNAT_INIT_FLOAT
2307 __gnat_init_float (void)
2309 #if defined (__i386__) || defined (i386) || defined (__x86_64)
2311 /* This is used to properly initialize the FPU on an x86 for each
2316 #endif /* Defined __i386__ */
2320 #ifndef HAVE_GNAT_INIT_FLOAT
2322 /* All targets without a specific __gnat_init_float will use an empty one. */
2324 __gnat_init_float (void)
2329 /***********************************/
2330 /* __gnat_adjust_context_for_raise */
2331 /***********************************/
2333 #ifndef HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
2335 /* All targets without a specific version will use an empty one. */
2337 /* Given UCONTEXT a pointer to a context structure received by a signal
2338 handler for SIGNO, perform the necessary adjustments to let the handler
2339 raise an exception. Calls to this routine are not conditioned by the
2340 propagation scheme in use. */
2343 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED
,
2344 void *ucontext ATTRIBUTE_UNUSED
)
2346 /* We used to compensate here for the raised from call vs raised from signal
2347 exception discrepancy with the GCC ZCX scheme, but this now can be dealt
2348 with generically in the unwinder (see GCC PR other/26208). This however
2349 requires the use of the _Unwind_GetIPInfo routine in raise-gcc.c, which
2350 is predicated on the definition of HAVE_GETIPINFO at compile time. Only
2351 the VMS ports still do the compensation described in the few lines below.
2353 *** Call vs signal exception discrepancy with GCC ZCX scheme ***
2355 The GCC unwinder expects to be dealing with call return addresses, since
2356 this is the "nominal" case of what we retrieve while unwinding a regular
2359 To evaluate if a handler applies at some point identified by a return
2360 address, the propagation engine needs to determine what region the
2361 corresponding call instruction pertains to. Because the return address
2362 may not be attached to the same region as the call, the unwinder always
2363 subtracts "some" amount from a return address to search the region
2364 tables, amount chosen to ensure that the resulting address is inside the
2367 When we raise an exception from a signal handler, e.g. to transform a
2368 SIGSEGV into Storage_Error, things need to appear as if the signal
2369 handler had been "called" by the instruction which triggered the signal,
2370 so that exception handlers that apply there are considered. What the
2371 unwinder will retrieve as the return address from the signal handler is
2372 what it will find as the faulting instruction address in the signal
2373 context pushed by the kernel. Leaving this address untouched looses, if
2374 the triggering instruction happens to be the very first of a region, as
2375 the later adjustments performed by the unwinder would yield an address
2376 outside that region. We need to compensate for the unwinder adjustments
2377 at some point, and this is what this routine is expected to do.
2379 signo is passed because on some targets for some signals the PC in
2380 context points to the instruction after the faulting one, in which case
2381 the unwinder adjustment is still desired. */