1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 1992-2012, 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). */
45 /* This file should be kept synchronized with 2sinit.ads, 2sinit.adb,
46 s-init-ae653-cert.adb and s-init-xi-sparc.adb. All these files implement
47 the required functionality for different targets. */
49 /* The following include is here to meet the published VxWorks requirement
50 that the __vxworks header appear before any other include. */
64 /* We don't have libiberty, so use malloc. */
65 #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 */
229 /* Version of AIX before 5.3 don't have pthread_condattr_setclock:
230 * supply it as a weak symbol here so that if linking on a 5.3 or newer
231 * machine, we get the real one.
234 #ifndef _AIXVERSION_530
235 #pragma weak pthread_condattr_setclock
237 pthread_condattr_setclock (pthread_condattr_t
*attr
, clockid_t cl
) {
243 __gnat_error_handler (int sig
,
244 siginfo_t
*si ATTRIBUTE_UNUSED
,
245 void *ucontext ATTRIBUTE_UNUSED
)
247 struct Exception_Data
*exception
;
253 /* FIXME: we need to detect the case of a *real* SIGSEGV. */
254 exception
= &storage_error
;
255 msg
= "stack overflow or erroneous memory access";
259 exception
= &constraint_error
;
264 exception
= &constraint_error
;
269 exception
= &program_error
;
270 msg
= "unhandled signal";
273 Raise_From_Signal_Handler (exception
, msg
);
277 __gnat_install_handler (void)
279 struct sigaction act
;
281 /* Set up signal handler to map synchronous signals to appropriate
282 exceptions. Make sure that the handler isn't interrupted by another
283 signal that might cause a scheduling event! */
285 act
.sa_flags
= SA_NODEFER
| SA_RESTART
| SA_SIGINFO
;
286 act
.sa_sigaction
= __gnat_error_handler
;
287 sigemptyset (&act
.sa_mask
);
289 /* Do not install handlers if interrupt state is "System". */
290 if (__gnat_get_interrupt_state (SIGABRT
) != 's')
291 sigaction (SIGABRT
, &act
, NULL
);
292 if (__gnat_get_interrupt_state (SIGFPE
) != 's')
293 sigaction (SIGFPE
, &act
, NULL
);
294 if (__gnat_get_interrupt_state (SIGILL
) != 's')
295 sigaction (SIGILL
, &act
, NULL
);
296 if (__gnat_get_interrupt_state (SIGSEGV
) != 's')
297 sigaction (SIGSEGV
, &act
, NULL
);
298 if (__gnat_get_interrupt_state (SIGBUS
) != 's')
299 sigaction (SIGBUS
, &act
, NULL
);
301 __gnat_handler_installed
= 1;
308 #elif defined (__hpux__)
311 #include <sys/ucontext.h>
313 #if defined (IN_RTS) && defined (__ia64__)
315 #include <sys/uc_access.h>
317 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
320 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED
, void *ucontext
)
322 ucontext_t
*uc
= (ucontext_t
*) ucontext
;
325 /* Adjust on itanium, as GetIPInfo is not supported. */
326 __uc_get_ip (uc
, &ip
);
327 __uc_set_ip (uc
, ip
+ 1);
329 #endif /* IN_RTS && __ia64__ */
331 /* Tasking and Non-tasking signal handler. Map SIGnal to Ada exception
332 propagation after the required low level adjustments. */
335 __gnat_error_handler (int sig
,
336 siginfo_t
*si ATTRIBUTE_UNUSED
,
337 void *ucontext ATTRIBUTE_UNUSED
)
339 struct Exception_Data
*exception
;
342 __gnat_adjust_context_for_raise (sig
, ucontext
);
347 /* FIXME: we need to detect the case of a *real* SIGSEGV. */
348 exception
= &storage_error
;
349 msg
= "stack overflow or erroneous memory access";
353 exception
= &constraint_error
;
358 exception
= &constraint_error
;
363 exception
= &program_error
;
364 msg
= "unhandled signal";
367 Raise_From_Signal_Handler (exception
, msg
);
370 /* This must be in keeping with System.OS_Interface.Alternate_Stack_Size. */
371 #if defined (__hppa__)
372 char __gnat_alternate_stack
[16 * 1024]; /* 2 * SIGSTKSZ */
374 char __gnat_alternate_stack
[128 * 1024]; /* MINSIGSTKSZ */
378 __gnat_install_handler (void)
380 struct sigaction act
;
382 /* Set up signal handler to map synchronous signals to appropriate
383 exceptions. Make sure that the handler isn't interrupted by another
384 signal that might cause a scheduling event! Also setup an alternate
385 stack region for the handler execution so that stack overflows can be
386 handled properly, avoiding a SEGV generation from stack usage by the
390 stack
.ss_sp
= __gnat_alternate_stack
;
391 stack
.ss_size
= sizeof (__gnat_alternate_stack
);
393 sigaltstack (&stack
, NULL
);
395 act
.sa_sigaction
= __gnat_error_handler
;
396 act
.sa_flags
= SA_NODEFER
| SA_RESTART
| SA_SIGINFO
;
397 sigemptyset (&act
.sa_mask
);
399 /* Do not install handlers if interrupt state is "System". */
400 if (__gnat_get_interrupt_state (SIGABRT
) != 's')
401 sigaction (SIGABRT
, &act
, NULL
);
402 if (__gnat_get_interrupt_state (SIGFPE
) != 's')
403 sigaction (SIGFPE
, &act
, NULL
);
404 if (__gnat_get_interrupt_state (SIGILL
) != 's')
405 sigaction (SIGILL
, &act
, NULL
);
406 if (__gnat_get_interrupt_state (SIGBUS
) != 's')
407 sigaction (SIGBUS
, &act
, NULL
);
408 act
.sa_flags
|= SA_ONSTACK
;
409 if (__gnat_get_interrupt_state (SIGSEGV
) != 's')
410 sigaction (SIGSEGV
, &act
, NULL
);
412 __gnat_handler_installed
= 1;
415 /*********************/
416 /* GNU/Linux Section */
417 /*********************/
419 #elif defined (linux)
423 #define __USE_GNU 1 /* required to get REG_EIP/RIP from glibc's ucontext.h */
424 #include <sys/ucontext.h>
426 /* GNU/Linux, which uses glibc, does not define NULL in included
430 #define NULL ((void *) 0)
435 /* MaRTE OS provides its own version of sigaction, sigfillset, and
436 sigemptyset (overriding these symbol names). We want to make sure that
437 the versions provided by the underlying C library are used here (these
438 versions are renamed by MaRTE to linux_sigaction, fake_linux_sigfillset,
439 and fake_linux_sigemptyset, respectively). The MaRTE library will not
440 always be present (it will not be linked if no tasking constructs are
441 used), so we use the weak symbol mechanism to point always to the symbols
442 defined within the C library. */
444 #pragma weak linux_sigaction
445 int linux_sigaction (int signum
, const struct sigaction
*act
,
446 struct sigaction
*oldact
) {
447 return sigaction (signum
, act
, oldact
);
449 #define sigaction(signum, act, oldact) linux_sigaction (signum, act, oldact)
451 #pragma weak fake_linux_sigfillset
452 void fake_linux_sigfillset (sigset_t
*set
) {
455 #define sigfillset(set) fake_linux_sigfillset (set)
457 #pragma weak fake_linux_sigemptyset
458 void fake_linux_sigemptyset (sigset_t
*set
) {
461 #define sigemptyset(set) fake_linux_sigemptyset (set)
465 #if defined (i386) || defined (__x86_64__) || defined (__ia64__)
467 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
470 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED
, void *ucontext
)
472 mcontext_t
*mcontext
= &((ucontext_t
*) ucontext
)->uc_mcontext
;
474 /* On the i386 and x86-64 architectures, stack checking is performed by
475 means of probes with moving stack pointer, that is to say the probed
476 address is always the value of the stack pointer. Upon hitting the
477 guard page, the stack pointer therefore points to an inaccessible
478 address and an alternate signal stack is needed to run the handler.
479 But there is an additional twist: on these architectures, the EH
480 return code writes the address of the handler at the target CFA's
481 value on the stack before doing the jump. As a consequence, if
482 there is an active handler in the frame whose stack has overflowed,
483 the stack pointer must nevertheless point to an accessible address
484 by the time the EH return is executed.
486 We therefore adjust the saved value of the stack pointer by the size
487 of one page + a small dope of 4 words, in order to make sure that it
488 points to an accessible address in case it's used as the target CFA.
489 The stack checking code guarantees that this address is unused by the
490 time this happens. */
493 unsigned long *pc
= (unsigned long *)mcontext
->gregs
[REG_EIP
];
494 /* The pattern is "orl $0x0,(%esp)" for a probe in 32-bit mode. */
495 if (signo
== SIGSEGV
&& pc
&& *pc
== 0x00240c83)
496 mcontext
->gregs
[REG_ESP
] += 4096 + 4 * sizeof (unsigned long);
497 #elif defined (__x86_64__)
498 unsigned long long *pc
= (unsigned long long *)mcontext
->gregs
[REG_RIP
];
499 if (signo
== SIGSEGV
&& pc
500 /* The pattern is "orq $0x0,(%rsp)" for a probe in 64-bit mode. */
501 && ((*pc
& 0xffffffffffLL
) == 0x00240c8348LL
502 /* The pattern may also be "orl $0x0,(%esp)" for a probe in
504 || (*pc
& 0xffffffffLL
) == 0x00240c83LL
))
505 mcontext
->gregs
[REG_RSP
] += 4096 + 4 * sizeof (unsigned long);
506 #elif defined (__ia64__)
507 /* ??? The IA-64 unwinder doesn't compensate for signals. */
515 __gnat_error_handler (int sig
, siginfo_t
*si ATTRIBUTE_UNUSED
, void *ucontext
)
517 struct Exception_Data
*exception
;
520 /* Adjusting is required for every fault context, so adjust for this one
521 now, before we possibly trigger a recursive fault below. */
522 __gnat_adjust_context_for_raise (sig
, ucontext
);
527 /* Here we would like a discrimination test to see whether the page
528 before the faulting address is accessible. Unfortunately, Linux
529 seems to have no way of giving us the faulting address.
531 In old versions of init.c, we had a test of the page before the
535 ((long) si->esp_at_signal & - getpagesize ()))[getpagesize ()];
537 but that's wrong since it tests the stack pointer location and the
538 stack probing code may not move it until all probes succeed.
540 For now we simply do not attempt any discrimination at all. Note
541 that this is quite acceptable, since a "real" SIGSEGV can only
542 occur as the result of an erroneous program. */
543 exception
= &storage_error
;
544 msg
= "stack overflow or erroneous memory access";
548 exception
= &storage_error
;
549 msg
= "SIGBUS: possible stack overflow";
553 exception
= &constraint_error
;
558 exception
= &program_error
;
559 msg
= "unhandled signal";
562 Raise_From_Signal_Handler (exception
, msg
);
565 #if defined (i386) || defined (__x86_64__) || defined (__powerpc__)
566 /* This must be in keeping with System.OS_Interface.Alternate_Stack_Size. */
567 char __gnat_alternate_stack
[16 * 1024]; /* 2 * SIGSTKSZ */
571 #include <sys/mman.h>
572 #include <native/task.h>
578 __gnat_install_handler (void)
580 struct sigaction act
;
585 if (__gl_main_priority
== -1)
588 prio
= __gl_main_priority
;
590 /* Avoid memory swapping for this program */
592 mlockall (MCL_CURRENT
|MCL_FUTURE
);
594 /* Turn the current Linux task into a native Xenomai task */
596 rt_task_shadow(&main_task
, "environment_task", prio
, T_FPU
);
599 /* Set up signal handler to map synchronous signals to appropriate
600 exceptions. Make sure that the handler isn't interrupted by another
601 signal that might cause a scheduling event! Also setup an alternate
602 stack region for the handler execution so that stack overflows can be
603 handled properly, avoiding a SEGV generation from stack usage by the
606 #if defined (i386) || defined (__x86_64__) || defined (__powerpc__)
608 stack
.ss_sp
= __gnat_alternate_stack
;
609 stack
.ss_size
= sizeof (__gnat_alternate_stack
);
611 sigaltstack (&stack
, NULL
);
614 act
.sa_sigaction
= __gnat_error_handler
;
615 act
.sa_flags
= SA_NODEFER
| SA_RESTART
| SA_SIGINFO
;
616 sigemptyset (&act
.sa_mask
);
618 /* Do not install handlers if interrupt state is "System". */
619 if (__gnat_get_interrupt_state (SIGABRT
) != 's')
620 sigaction (SIGABRT
, &act
, NULL
);
621 if (__gnat_get_interrupt_state (SIGFPE
) != 's')
622 sigaction (SIGFPE
, &act
, NULL
);
623 if (__gnat_get_interrupt_state (SIGILL
) != 's')
624 sigaction (SIGILL
, &act
, NULL
);
625 if (__gnat_get_interrupt_state (SIGBUS
) != 's')
626 sigaction (SIGBUS
, &act
, NULL
);
627 #if defined (i386) || defined (__x86_64__) || defined (__powerpc__)
628 act
.sa_flags
|= SA_ONSTACK
;
630 if (__gnat_get_interrupt_state (SIGSEGV
) != 's')
631 sigaction (SIGSEGV
, &act
, NULL
);
633 __gnat_handler_installed
= 1;
636 /*******************/
638 /*******************/
640 #elif defined (__Lynx__)
646 __gnat_error_handler (int sig
)
648 struct Exception_Data
*exception
;
654 exception
= &constraint_error
;
658 exception
= &constraint_error
;
662 exception
= &storage_error
;
663 msg
= "stack overflow or erroneous memory access";
666 exception
= &constraint_error
;
670 exception
= &program_error
;
671 msg
= "unhandled signal";
674 Raise_From_Signal_Handler(exception
, msg
);
678 __gnat_install_handler(void)
680 struct sigaction act
;
682 act
.sa_handler
= __gnat_error_handler
;
684 sigemptyset (&act
.sa_mask
);
686 /* Do not install handlers if interrupt state is "System". */
687 if (__gnat_get_interrupt_state (SIGFPE
) != 's')
688 sigaction (SIGFPE
, &act
, NULL
);
689 if (__gnat_get_interrupt_state (SIGILL
) != 's')
690 sigaction (SIGILL
, &act
, NULL
);
691 if (__gnat_get_interrupt_state (SIGSEGV
) != 's')
692 sigaction (SIGSEGV
, &act
, NULL
);
693 if (__gnat_get_interrupt_state (SIGBUS
) != 's')
694 sigaction (SIGBUS
, &act
, NULL
);
696 __gnat_handler_installed
= 1;
699 /*******************/
700 /* Solaris Section */
701 /*******************/
703 #elif defined (sun) && defined (__SVR4) && !defined (__vxworks)
707 #include <sys/ucontext.h>
708 #include <sys/regset.h>
710 /* The code below is common to SPARC and x86. Beware of the delay slot
711 differences for signal context adjustments. */
713 #if defined (__sparc)
714 #define RETURN_ADDR_OFFSET 8
716 #define RETURN_ADDR_OFFSET 0
720 __gnat_error_handler (int sig
, siginfo_t
*si
, void *ucontext ATTRIBUTE_UNUSED
)
722 struct Exception_Data
*exception
;
723 static int recurse
= 0;
729 /* If the problem was permissions, this is a constraint error.
730 Likewise if the failing address isn't maximally aligned or if
733 ??? Using a static variable here isn't task-safe, but it's
734 much too hard to do anything else and we're just determining
735 which exception to raise. */
736 if (si
->si_code
== SEGV_ACCERR
737 || (long) si
->si_addr
== 0
738 || (((long) si
->si_addr
) & 3) != 0
741 exception
= &constraint_error
;
746 /* See if the page before the faulting page is accessible. Do that
747 by trying to access it. We'd like to simply try to access
748 4096 + the faulting address, but it's not guaranteed to be
749 the actual address, just to be on the same page. */
752 ((long) si
->si_addr
& - getpagesize ()))[getpagesize ()];
753 exception
= &storage_error
;
754 msg
= "stack overflow or erroneous memory access";
759 exception
= &program_error
;
764 exception
= &constraint_error
;
769 exception
= &program_error
;
770 msg
= "unhandled signal";
774 Raise_From_Signal_Handler (exception
, msg
);
778 __gnat_install_handler (void)
780 struct sigaction act
;
782 /* Set up signal handler to map synchronous signals to appropriate
783 exceptions. Make sure that the handler isn't interrupted by another
784 signal that might cause a scheduling event! */
786 act
.sa_sigaction
= __gnat_error_handler
;
787 act
.sa_flags
= SA_NODEFER
| SA_RESTART
| SA_SIGINFO
;
788 sigemptyset (&act
.sa_mask
);
790 /* Do not install handlers if interrupt state is "System". */
791 if (__gnat_get_interrupt_state (SIGABRT
) != 's')
792 sigaction (SIGABRT
, &act
, NULL
);
793 if (__gnat_get_interrupt_state (SIGFPE
) != 's')
794 sigaction (SIGFPE
, &act
, NULL
);
795 if (__gnat_get_interrupt_state (SIGSEGV
) != 's')
796 sigaction (SIGSEGV
, &act
, NULL
);
797 if (__gnat_get_interrupt_state (SIGBUS
) != 's')
798 sigaction (SIGBUS
, &act
, NULL
);
800 __gnat_handler_installed
= 1;
809 /* Routine called from binder to override default feature values. */
810 void __gnat_set_features (void);
811 int __gnat_features_set
= 0;
814 #define lib_get_curr_invo_context LIB$I64_GET_CURR_INVO_CONTEXT
815 #define lib_get_prev_invo_context LIB$I64_GET_PREV_INVO_CONTEXT
816 #define lib_get_invo_handle LIB$I64_GET_INVO_HANDLE
818 #define lib_get_curr_invo_context LIB$GET_CURR_INVO_CONTEXT
819 #define lib_get_prev_invo_context LIB$GET_PREV_INVO_CONTEXT
820 #define lib_get_invo_handle LIB$GET_INVO_HANDLE
823 /* Define macro symbols for the VMS conditions that become Ada exceptions.
824 Most of these are also defined in the header file ssdef.h which has not
825 yet been converted to be recognized by GNU C. */
827 /* Defining these as macros, as opposed to external addresses, allows
828 them to be used in a case statement below. */
829 #define SS$_ACCVIO 12
830 #define SS$_HPARITH 1284
831 #define SS$_STKOVF 1364
832 #define SS$_RESIGNAL 2328
834 /* These codes are in standard message libraries. */
835 extern int C$_SIGKILL
;
836 extern int SS$_DEBUG
;
837 extern int LIB$_KEYNOTFOU
;
838 extern int LIB$_ACTIMAGE
;
839 #define CMA$_EXIT_THREAD 4227492
840 #define MTH$_FLOOVEMAT 1475268 /* Some ACVC_21 CXA tests */
841 #define SS$_INTDIV 1156
843 /* These codes are non standard, which is to say the author is
844 not sure if they are defined in the standard message libraries
845 so keep them as macros for now. */
846 #define RDB$_STREAM_EOF 20480426
847 #define FDL$_UNPRIKW 11829410
851 const struct Exception_Data
*except
;
854 struct descriptor_s
{
855 unsigned short len
, mbz
;
859 /* Conditions that don't have an Ada exception counterpart must raise
860 Non_Ada_Error. Since this is defined in s-auxdec, it should only be
861 referenced by user programs, not the compiler or tools. Hence the
866 #define Status_Error ada__io_exceptions__status_error
867 extern struct Exception_Data Status_Error
;
869 #define Mode_Error ada__io_exceptions__mode_error
870 extern struct Exception_Data Mode_Error
;
872 #define Name_Error ada__io_exceptions__name_error
873 extern struct Exception_Data Name_Error
;
875 #define Use_Error ada__io_exceptions__use_error
876 extern struct Exception_Data Use_Error
;
878 #define Device_Error ada__io_exceptions__device_error
879 extern struct Exception_Data Device_Error
;
881 #define End_Error ada__io_exceptions__end_error
882 extern struct Exception_Data End_Error
;
884 #define Data_Error ada__io_exceptions__data_error
885 extern struct Exception_Data Data_Error
;
887 #define Layout_Error ada__io_exceptions__layout_error
888 extern struct Exception_Data Layout_Error
;
890 #define Non_Ada_Error system__aux_dec__non_ada_error
891 extern struct Exception_Data Non_Ada_Error
;
893 #define Coded_Exception system__vms_exception_table__coded_exception
894 extern struct Exception_Data
*Coded_Exception (Exception_Code
);
896 #define Base_Code_In system__vms_exception_table__base_code_in
897 extern Exception_Code
Base_Code_In (Exception_Code
);
899 /* DEC Ada exceptions are not defined in a header file, so they
902 #define ADA$_ALREADY_OPEN 0x0031a594
903 #define ADA$_CONSTRAINT_ERRO 0x00318324
904 #define ADA$_DATA_ERROR 0x003192c4
905 #define ADA$_DEVICE_ERROR 0x003195e4
906 #define ADA$_END_ERROR 0x00319904
907 #define ADA$_FAC_MODE_MISMAT 0x0031a8b3
908 #define ADA$_IOSYSFAILED 0x0031af04
909 #define ADA$_KEYSIZERR 0x0031aa3c
910 #define ADA$_KEY_MISMATCH 0x0031a8e3
911 #define ADA$_LAYOUT_ERROR 0x00319c24
912 #define ADA$_LINEXCMRS 0x0031a8f3
913 #define ADA$_MAXLINEXC 0x0031a8eb
914 #define ADA$_MODE_ERROR 0x00319f44
915 #define ADA$_MRN_MISMATCH 0x0031a8db
916 #define ADA$_MRS_MISMATCH 0x0031a8d3
917 #define ADA$_NAME_ERROR 0x0031a264
918 #define ADA$_NOT_OPEN 0x0031a58c
919 #define ADA$_ORG_MISMATCH 0x0031a8bb
920 #define ADA$_PROGRAM_ERROR 0x00318964
921 #define ADA$_RAT_MISMATCH 0x0031a8cb
922 #define ADA$_RFM_MISMATCH 0x0031a8c3
923 #define ADA$_STAOVF 0x00318cac
924 #define ADA$_STATUS_ERROR 0x0031a584
925 #define ADA$_STORAGE_ERROR 0x00318c84
926 #define ADA$_UNSUPPORTED 0x0031a8ab
927 #define ADA$_USE_ERROR 0x0031a8a4
929 /* DEC Ada specific conditions. */
930 static const struct cond_except dec_ada_cond_except_table
[] = {
931 {ADA$_PROGRAM_ERROR
, &program_error
},
932 {ADA$_USE_ERROR
, &Use_Error
},
933 {ADA$_KEYSIZERR
, &program_error
},
934 {ADA$_STAOVF
, &storage_error
},
935 {ADA$_CONSTRAINT_ERRO
, &constraint_error
},
936 {ADA$_IOSYSFAILED
, &Device_Error
},
937 {ADA$_LAYOUT_ERROR
, &Layout_Error
},
938 {ADA$_STORAGE_ERROR
, &storage_error
},
939 {ADA$_DATA_ERROR
, &Data_Error
},
940 {ADA$_DEVICE_ERROR
, &Device_Error
},
941 {ADA$_END_ERROR
, &End_Error
},
942 {ADA$_MODE_ERROR
, &Mode_Error
},
943 {ADA$_NAME_ERROR
, &Name_Error
},
944 {ADA$_STATUS_ERROR
, &Status_Error
},
945 {ADA$_NOT_OPEN
, &Use_Error
},
946 {ADA$_ALREADY_OPEN
, &Use_Error
},
947 {ADA$_USE_ERROR
, &Use_Error
},
948 {ADA$_UNSUPPORTED
, &Use_Error
},
949 {ADA$_FAC_MODE_MISMAT
, &Use_Error
},
950 {ADA$_ORG_MISMATCH
, &Use_Error
},
951 {ADA$_RFM_MISMATCH
, &Use_Error
},
952 {ADA$_RAT_MISMATCH
, &Use_Error
},
953 {ADA$_MRS_MISMATCH
, &Use_Error
},
954 {ADA$_MRN_MISMATCH
, &Use_Error
},
955 {ADA$_KEY_MISMATCH
, &Use_Error
},
956 {ADA$_MAXLINEXC
, &constraint_error
},
957 {ADA$_LINEXCMRS
, &constraint_error
},
960 /* Already handled by a pragma Import_Exception
961 in Aux_IO_Exceptions */
962 {ADA$_LOCK_ERROR
, &Lock_Error
},
963 {ADA$_EXISTENCE_ERROR
, &Existence_Error
},
964 {ADA$_KEY_ERROR
, &Key_Error
},
972 /* Non-DEC Ada specific conditions. We could probably also put
973 SS$_HPARITH here and possibly SS$_ACCVIO, SS$_STKOVF. */
974 static const struct cond_except cond_except_table
[] = {
975 {MTH$_FLOOVEMAT
, &constraint_error
},
976 {SS$_INTDIV
, &constraint_error
},
980 /* To deal with VMS conditions and their mapping to Ada exceptions,
981 the __gnat_error_handler routine below is installed as an exception
982 vector having precedence over DEC frame handlers. Some conditions
983 still need to be handled by such handlers, however, in which case
984 __gnat_error_handler needs to return SS$_RESIGNAL. Consider for
985 instance the use of a third party library compiled with DECAda and
986 performing its own exception handling internally.
988 To allow some user-level flexibility, which conditions should be
989 resignaled is controlled by a predicate function, provided with the
990 condition value and returning a boolean indication stating whether
991 this condition should be resignaled or not.
993 That predicate function is called indirectly, via a function pointer,
994 by __gnat_error_handler, and changing that pointer is allowed to the
995 user code by way of the __gnat_set_resignal_predicate interface.
997 The user level function may then implement what it likes, including
998 for instance the maintenance of a dynamic data structure if the set
999 of to be resignalled conditions has to change over the program's
1002 ??? This is not a perfect solution to deal with the possible
1003 interactions between the GNAT and the DECAda exception handling
1004 models and better (more general) schemes are studied. This is so
1005 just provided as a convenient workaround in the meantime, and
1006 should be use with caution since the implementation has been kept
1010 resignal_predicate (int code
);
1012 static const int * const cond_resignal_table
[] = {
1014 (int *)CMA$_EXIT_THREAD
,
1018 (int *) RDB$_STREAM_EOF
,
1019 (int *) FDL$_UNPRIKW
,
1023 static const int facility_resignal_table
[] = {
1024 0x1380000, /* RDB */
1025 0x2220000, /* SQL */
1029 /* Default GNAT predicate for resignaling conditions. */
1032 __gnat_default_resignal_p (int code
)
1036 for (i
= 0; facility_resignal_table
[i
]; i
++)
1037 if ((code
& 0xfff0000) == facility_resignal_table
[i
])
1040 for (i
= 0, iexcept
= 0;
1041 cond_resignal_table
[i
]
1042 && !(iexcept
= LIB$
MATCH_COND (&code
, &cond_resignal_table
[i
]));
1048 /* Static pointer to predicate that the __gnat_error_handler exception
1049 vector invokes to determine if it should resignal a condition. */
1051 static resignal_predicate
*__gnat_resignal_p
= __gnat_default_resignal_p
;
1053 /* User interface to change the predicate pointer to PREDICATE. Reset to
1054 the default if PREDICATE is null. */
1057 __gnat_set_resignal_predicate (resignal_predicate
*predicate
)
1059 if (predicate
== NULL
)
1060 __gnat_resignal_p
= __gnat_default_resignal_p
;
1062 __gnat_resignal_p
= predicate
;
1065 /* Should match System.Parameters.Default_Exception_Msg_Max_Length. */
1066 #define Default_Exception_Msg_Max_Length 512
1068 /* Action routine for SYS$PUTMSG. There may be multiple
1069 conditions, each with text to be appended to MESSAGE
1070 and separated by line termination. */
1073 copy_msg (struct descriptor_s
*msgdesc
, char *message
)
1075 int len
= strlen (message
);
1078 /* Check for buffer overflow and skip. */
1079 if (len
> 0 && len
<= Default_Exception_Msg_Max_Length
- 3)
1081 strcat (message
, "\r\n");
1085 /* Check for buffer overflow and truncate if necessary. */
1086 copy_len
= (len
+ msgdesc
->len
<= Default_Exception_Msg_Max_Length
- 1 ?
1088 Default_Exception_Msg_Max_Length
- 1 - len
);
1089 strncpy (&message
[len
], msgdesc
->adr
, copy_len
);
1090 message
[len
+ copy_len
] = 0;
1096 __gnat_handle_vms_condition (int *sigargs
, void *mechargs
)
1098 struct Exception_Data
*exception
= 0;
1099 Exception_Code base_code
;
1100 struct descriptor_s gnat_facility
= {4, 0, "GNAT"};
1101 char message
[Default_Exception_Msg_Max_Length
];
1103 const char *msg
= "";
1105 /* Check for conditions to resignal which aren't effected by pragma
1106 Import_Exception. */
1107 if (__gnat_resignal_p (sigargs
[1]))
1108 return SS$_RESIGNAL
;
1111 /* See if it's an imported exception. Beware that registered exceptions
1112 are bound to their base code, with the severity bits masked off. */
1113 base_code
= Base_Code_In ((Exception_Code
) sigargs
[1]);
1114 exception
= Coded_Exception (base_code
);
1120 /* Subtract PC & PSL fields which messes with PUTMSG. */
1122 SYS$
PUTMSG (sigargs
, copy_msg
, &gnat_facility
, message
);
1126 exception
->Name_Length
= 19;
1127 /* ??? The full name really should be get SYS$GETMSG returns. */
1128 exception
->Full_Name
= "IMPORTED_EXCEPTION";
1129 exception
->Import_Code
= base_code
;
1132 /* Do not adjust the program counter as already points to the next
1133 instruction (just after the call to LIB$STOP). */
1134 Raise_From_Signal_Handler (exception
, msg
);
1143 if (sigargs
[3] == 0)
1145 exception
= &constraint_error
;
1146 msg
= "access zero";
1150 exception
= &storage_error
;
1151 msg
= "stack overflow or erroneous memory access";
1153 __gnat_adjust_context_for_raise (SS$_ACCVIO
, (void *)mechargs
);
1157 exception
= &storage_error
;
1158 msg
= "stack overflow";
1159 __gnat_adjust_context_for_raise (SS$_STKOVF
, (void *)mechargs
);
1164 return SS$_RESIGNAL
; /* toplev.c handles for compiler */
1166 exception
= &constraint_error
;
1167 msg
= "arithmetic error";
1168 __gnat_adjust_context_for_raise (SS$_HPARITH
, (void *)mechargs
);
1177 /* Scan the DEC Ada exception condition table for a match and fetch
1178 the associated GNAT exception pointer. */
1180 dec_ada_cond_except_table
[i
].cond
&&
1181 !LIB$
MATCH_COND (&sigargs
[1],
1182 &dec_ada_cond_except_table
[i
].cond
);
1184 exception
= (struct Exception_Data
*)
1185 dec_ada_cond_except_table
[i
].except
;
1189 /* Scan the VMS standard condition table for a match and fetch
1190 the associated GNAT exception pointer. */
1192 cond_except_table
[i
].cond
&&
1193 !LIB$
MATCH_COND (&sigargs
[1], &cond_except_table
[i
].cond
);
1195 exception
= (struct Exception_Data
*)
1196 cond_except_table
[i
].except
;
1199 /* User programs expect Non_Ada_Error to be raised, reference
1200 DEC Ada test CXCONDHAN. */
1201 exception
= &Non_Ada_Error
;
1205 exception
= &program_error
;
1208 /* Subtract PC & PSL fields which messes with PUTMSG. */
1210 SYS$
PUTMSG (sigargs
, copy_msg
, &gnat_facility
, message
);
1216 Raise_From_Signal_Handler (exception
, msg
);
1220 __gnat_install_handler (void)
1222 long prvhnd ATTRIBUTE_UNUSED
;
1224 #if !defined (IN_RTS)
1225 SYS$
SETEXV (1, __gnat_handle_vms_condition
, 3, &prvhnd
);
1228 __gnat_handler_installed
= 1;
1231 /* __gnat_adjust_context_for_raise for Alpha - see comments along with the
1232 default version later in this file. */
1234 #if defined (IN_RTS) && defined (__alpha__)
1236 #include <vms/chfctxdef.h>
1237 #include <vms/chfdef.h>
1239 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
1242 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED
, void *ucontext
)
1244 if (signo
== SS$_HPARITH
)
1246 /* Sub one to the address of the instruction signaling the condition,
1247 located in the sigargs array. */
1249 CHF$MECH_ARRAY
* mechargs
= (CHF$MECH_ARRAY
*) ucontext
;
1250 CHF$SIGNAL_ARRAY
* sigargs
1251 = (CHF$SIGNAL_ARRAY
*) mechargs
->chf$q_mch_sig_addr
;
1253 int vcount
= sigargs
->chf$is_sig_args
;
1254 int * pc_slot
= & (&sigargs
->chf$l_sig_name
)[vcount
-2];
1262 /* __gnat_adjust_context_for_raise for ia64. */
1264 #if defined (IN_RTS) && defined (__IA64)
1266 #include <vms/chfctxdef.h>
1267 #include <vms/chfdef.h>
1269 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
1271 typedef unsigned long long u64
;
1274 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED
, void *ucontext
)
1276 /* Add one to the address of the instruction signaling the condition,
1277 located in the 64bits sigargs array. */
1279 CHF$MECH_ARRAY
* mechargs
= (CHF$MECH_ARRAY
*) ucontext
;
1281 CHF64$SIGNAL_ARRAY
*chfsig64
1282 = (CHF64$SIGNAL_ARRAY
*) mechargs
->chf$ph_mch_sig64_addr
;
1285 = (u64
*)chfsig64
+ 1 + chfsig64
->chf64$l_sig_args
;
1287 u64
* ih_pc_loc
= post_sigarray
- 2;
1294 /* Easier interface for LIB$GET_LOGICAL: put the equivalence of NAME into BUF,
1295 always NUL terminated. In case of error or if the result is longer than
1296 LEN (length of BUF) an empty string is written info BUF. */
1299 __gnat_vms_get_logical (const char *name
, char *buf
, int len
)
1301 struct descriptor_s name_desc
, result_desc
;
1303 unsigned short rlen
;
1305 /* Build the descriptor for NAME. */
1306 name_desc
.len
= strlen (name
);
1308 name_desc
.adr
= (char *)name
;
1310 /* Build the descriptor for the result. */
1311 result_desc
.len
= len
;
1312 result_desc
.mbz
= 0;
1313 result_desc
.adr
= buf
;
1315 status
= LIB$
GET_LOGICAL (&name_desc
, &result_desc
, &rlen
);
1317 if ((status
& 1) == 1 && rlen
< len
)
1323 /* Size of a page on ia64 and alpha VMS. */
1324 #define VMS_PAGESIZE 8192
1327 #define PSL__C_USER 3
1332 /* Descending region. */
1333 #define VA__M_DESCEND 1
1335 /* Get by virtual address. */
1336 #define VA___REGSUM_BY_VA 1
1338 /* Memory region summary. */
1341 unsigned long long q_region_id
;
1342 unsigned int l_flags
;
1343 unsigned int l_region_protection
;
1345 unsigned long long q_region_size
;
1346 void *pq_first_free_va
;
1349 extern int SYS$
GET_REGION_INFO (unsigned int, unsigned long long *,
1350 void *, void *, unsigned int,
1351 void *, unsigned int *);
1352 extern int SYS$
EXPREG_64 (unsigned long long *, unsigned long long,
1353 unsigned int, unsigned int, void **,
1354 unsigned long long *);
1355 extern int SYS$
SETPRT_64 (void *, unsigned long long, unsigned int,
1356 unsigned int, void **, unsigned long long *,
1358 extern int SYS$
PUTMSG (void *, int (*)(), void *, unsigned long long);
1360 /* Add a guard page in the memory region containing ADDR at ADDR +/- SIZE.
1361 (The sign depends on the kind of the memory region). */
1364 __gnat_set_stack_guard_page (void *addr
, unsigned long size
)
1368 unsigned long long ret_len
;
1369 unsigned int ret_prot
;
1371 unsigned long long length
;
1372 unsigned int retlen
;
1373 struct regsum buffer
;
1375 /* Get the region for ADDR. */
1376 status
= SYS$GET_REGION_INFO
1377 (VA___REGSUM_BY_VA
, NULL
, addr
, NULL
, sizeof (buffer
), &buffer
, &retlen
);
1379 if ((status
& 1) != 1)
1382 /* Extend the region. */
1383 status
= SYS$
EXPREG_64 (&buffer
.q_region_id
,
1384 size
, 0, 0, &start_va
, &length
);
1386 if ((status
& 1) != 1)
1389 /* Create a guard page. */
1390 if (!(buffer
.l_flags
& VA__M_DESCEND
))
1391 start_va
= (void *)((unsigned long long)start_va
+ length
- VMS_PAGESIZE
);
1393 status
= SYS$
SETPRT_64 (start_va
, VMS_PAGESIZE
, PSL__C_USER
, PRT__C_NA
,
1394 &ret_va
, &ret_len
, &ret_prot
);
1396 if ((status
& 1) != 1)
1401 /* Read logicals to limit the stack(s) size. */
1404 __gnat_set_stack_limit (void)
1412 /* The main stack. */
1413 __gnat_vms_get_logical ("GNAT_STACK_SIZE", value
, sizeof (value
));
1414 size
= strtoul (value
, &e
, 0);
1415 if (e
> value
&& *e
== 0)
1417 asm ("mov %0=sp" : "=r" (sp
));
1418 __gnat_set_stack_guard_page (sp
, size
* 1024);
1421 /* The register stack. */
1422 __gnat_vms_get_logical ("GNAT_RBS_SIZE", value
, sizeof (value
));
1423 size
= strtoul (value
, &e
, 0);
1424 if (e
> value
&& *e
== 0)
1426 asm ("mov %0=ar.bsp" : "=r" (sp
));
1427 __gnat_set_stack_guard_page (sp
, size
* 1024);
1432 /* Feature logical name and global variable address pair.
1433 If we ever add another feature logical to this list, the
1434 feature struct will need to be enhanced to take into account
1435 possible values for *gl_addr. */
1441 /* Default values for GNAT features set by environment. */
1442 int __gl_heap_size
= 64;
1444 /* Array feature logical names and global variable addresses. */
1445 static const struct feature features
[] = {
1446 {"GNAT$NO_MALLOC_64", &__gl_heap_size
},
1451 __gnat_set_features (void)
1456 /* Loop through features array and test name for enable/disable. */
1457 for (i
= 0; features
[i
].name
; i
++)
1459 __gnat_vms_get_logical (features
[i
].name
, buff
, sizeof (buff
));
1461 if (strcmp (buff
, "ENABLE") == 0
1462 || strcmp (buff
, "TRUE") == 0
1463 || strcmp (buff
, "1") == 0)
1464 *features
[i
].gl_addr
= 32;
1465 else if (strcmp (buff
, "DISABLE") == 0
1466 || strcmp (buff
, "FALSE") == 0
1467 || strcmp (buff
, "0") == 0)
1468 *features
[i
].gl_addr
= 64;
1471 /* Features to artificially limit the stack size. */
1472 __gnat_set_stack_limit ();
1474 __gnat_features_set
= 1;
1477 /* Return true if the VMS version is 7.x. */
1479 extern unsigned int LIB$
GETSYI (int *, ...);
1481 #define SYI$_VERSION 0x1000
1484 __gnat_is_vms_v7 (void)
1486 struct descriptor_s desc
;
1489 int code
= SYI$_VERSION
;
1491 desc
.len
= sizeof (version
);
1495 status
= LIB$
GETSYI (&code
, 0, &desc
);
1496 if ((status
& 1) == 1 && version
[1] == '7' && version
[2] == '.')
1502 /*******************/
1503 /* FreeBSD Section */
1504 /*******************/
1506 #elif defined (__FreeBSD__)
1509 #include <sys/ucontext.h>
1513 __gnat_error_handler (int sig
,
1514 siginfo_t
*si ATTRIBUTE_UNUSED
,
1515 void *ucontext ATTRIBUTE_UNUSED
)
1517 struct Exception_Data
*exception
;
1523 exception
= &constraint_error
;
1528 exception
= &constraint_error
;
1533 exception
= &storage_error
;
1534 msg
= "stack overflow or erroneous memory access";
1538 exception
= &storage_error
;
1539 msg
= "SIGBUS: possible stack overflow";
1543 exception
= &program_error
;
1544 msg
= "unhandled signal";
1547 Raise_From_Signal_Handler (exception
, msg
);
1551 __gnat_install_handler ()
1553 struct sigaction act
;
1555 /* Set up signal handler to map synchronous signals to appropriate
1556 exceptions. Make sure that the handler isn't interrupted by another
1557 signal that might cause a scheduling event! */
1560 = (void (*)(int, struct __siginfo
*, void*)) __gnat_error_handler
;
1561 act
.sa_flags
= SA_NODEFER
| SA_RESTART
| SA_SIGINFO
;
1562 (void) sigemptyset (&act
.sa_mask
);
1564 (void) sigaction (SIGILL
, &act
, NULL
);
1565 (void) sigaction (SIGFPE
, &act
, NULL
);
1566 (void) sigaction (SIGSEGV
, &act
, NULL
);
1567 (void) sigaction (SIGBUS
, &act
, NULL
);
1569 __gnat_handler_installed
= 1;
1572 /*******************/
1573 /* VxWorks Section */
1574 /*******************/
1576 #elif defined(__vxworks)
1579 #include <taskLib.h>
1587 #include "private/vThreadsP.h"
1590 void __gnat_error_handler (int, void *, struct sigcontext
*);
1594 /* Directly vectored Interrupt routines are not supported when using RTPs. */
1596 extern int __gnat_inum_to_ivec (int);
1598 /* This is needed by the GNAT run time to handle Vxworks interrupts. */
1600 __gnat_inum_to_ivec (int num
)
1602 return INUM_TO_IVEC (num
);
1606 #if !defined(__alpha_vxworks) && (_WRS_VXWORKS_MAJOR != 6) && !defined(__RTP__)
1608 /* getpid is used by s-parint.adb, but is not defined by VxWorks, except
1609 on Alpha VxWorks and VxWorks 6.x (including RTPs). */
1611 extern long getpid (void);
1616 return taskIdSelf ();
1620 /* VxWorks 653 vThreads expects the field excCnt to be zeroed when a signal is.
1621 handled. The VxWorks version of longjmp does this; GCC's builtin_longjmp
1624 __gnat_clear_exception_count (void)
1627 WIND_TCB
*currentTask
= (WIND_TCB
*) taskIdSelf();
1629 currentTask
->vThreads
.excCnt
= 0;
1633 /* Handle different SIGnal to exception mappings in different VxWorks
1636 __gnat_map_signal (int sig
, void *si ATTRIBUTE_UNUSED
,
1637 struct sigcontext
*sc ATTRIBUTE_UNUSED
)
1639 struct Exception_Data
*exception
;
1645 exception
= &constraint_error
;
1649 #ifdef __VXWORKSMILS__
1651 exception
= &storage_error
;
1652 msg
= "SIGILL: possible stack overflow";
1655 exception
= &storage_error
;
1659 exception
= &program_error
;
1664 exception
= &constraint_error
;
1665 msg
= "Floating point exception or SIGILL";
1668 exception
= &storage_error
;
1672 exception
= &storage_error
;
1673 msg
= "SIGBUS: possible stack overflow";
1676 #elif (_WRS_VXWORKS_MAJOR == 6)
1678 exception
= &constraint_error
;
1682 /* In RTP mode a SIGSEGV is most likely due to a stack overflow,
1683 since stack checking uses the probing mechanism. */
1685 exception
= &storage_error
;
1686 msg
= "SIGSEGV: possible stack overflow";
1689 exception
= &program_error
;
1693 /* VxWorks 6 kernel mode with probing. SIGBUS for guard page hit */
1695 exception
= &storage_error
;
1699 exception
= &storage_error
;
1700 msg
= "SIGBUS: possible stack overflow";
1704 /* VxWorks 5: a SIGILL is most likely due to a stack overflow,
1705 since stack checking uses the stack limit mechanism. */
1707 exception
= &storage_error
;
1708 msg
= "SIGILL: possible stack overflow";
1711 exception
= &storage_error
;
1715 exception
= &program_error
;
1720 exception
= &program_error
;
1721 msg
= "unhandled signal";
1724 __gnat_clear_exception_count ();
1725 Raise_From_Signal_Handler (exception
, msg
);
1728 /* Tasking and Non-tasking signal handler. Map SIGnal to Ada exception
1729 propagation after the required low level adjustments. */
1732 __gnat_error_handler (int sig
, void *si
, struct sigcontext
*sc
)
1736 /* VxWorks will always mask out the signal during the signal handler and
1737 will reenable it on a longjmp. GNAT does not generate a longjmp to
1738 return from a signal handler so the signal will still be masked unless
1740 sigprocmask (SIG_SETMASK
, NULL
, &mask
);
1741 sigdelset (&mask
, sig
);
1742 sigprocmask (SIG_SETMASK
, &mask
, NULL
);
1744 #if defined (__PPC__) && defined(_WRS_KERNEL)
1745 /* On PowerPC, kernel mode, we process signals through a Call Frame Info
1746 trampoline, voiding the need for myriads of fallback_frame_state
1747 variants in the ZCX runtime. We have no simple way to distinguish ZCX
1748 from SJLJ here, so we do this for SJLJ as well even though this is not
1749 necessary. This only incurs a few extra instructions and a tiny
1750 amount of extra stack usage. */
1752 #include "sigtramp.h"
1754 __gnat_sigtramp (sig
, (void *)si
, (void *)sc
,
1755 (sighandler_t
*)&__gnat_map_signal
);
1758 __gnat_map_signal (sig
, si
, sc
);
1763 __gnat_install_handler (void)
1765 struct sigaction act
;
1767 /* Setup signal handler to map synchronous signals to appropriate
1768 exceptions. Make sure that the handler isn't interrupted by another
1769 signal that might cause a scheduling event! */
1771 act
.sa_handler
= __gnat_error_handler
;
1772 act
.sa_flags
= SA_SIGINFO
| SA_ONSTACK
;
1773 sigemptyset (&act
.sa_mask
);
1775 /* For VxWorks, install all signal handlers, since pragma Interrupt_State
1776 applies to vectored hardware interrupts, not signals. */
1777 sigaction (SIGFPE
, &act
, NULL
);
1778 sigaction (SIGILL
, &act
, NULL
);
1779 sigaction (SIGSEGV
, &act
, NULL
);
1780 sigaction (SIGBUS
, &act
, NULL
);
1782 __gnat_handler_installed
= 1;
1785 #define HAVE_GNAT_INIT_FLOAT
1788 __gnat_init_float (void)
1790 /* Disable overflow/underflow exceptions on the PPC processor, needed
1791 to get correct Ada semantics. Note that for AE653 vThreads, the HW
1792 overflow settings are an OS configuration issue. The instructions
1793 below have no effect. */
1794 #if defined (_ARCH_PPC) && !defined (_SOFT_FLOAT) && (!defined (VTHREADS) || defined (__VXWORKSMILS__))
1795 #if defined (__SPE__)
1797 const unsigned long spefscr_mask
= 0xfffffff3;
1798 unsigned long spefscr
;
1799 asm ("mfspr %0, 512" : "=r" (spefscr
));
1800 spefscr
= spefscr
& spefscr_mask
;
1801 asm ("mtspr 512, %0\n\tisync" : : "r" (spefscr
));
1809 #if (defined (__i386__) || defined (i386)) && !defined (VTHREADS)
1810 /* This is used to properly initialize the FPU on an x86 for each
1815 /* Similarly for SPARC64. Achieved by masking bits in the Trap Enable Mask
1816 field of the Floating-point Status Register (see the SPARC Architecture
1817 Manual Version 9, p 48). */
1818 #if defined (sparc64)
1820 #define FSR_TEM_NVM (1 << 27) /* Invalid operand */
1821 #define FSR_TEM_OFM (1 << 26) /* Overflow */
1822 #define FSR_TEM_UFM (1 << 25) /* Underflow */
1823 #define FSR_TEM_DZM (1 << 24) /* Division by Zero */
1824 #define FSR_TEM_NXM (1 << 23) /* Inexact result */
1828 __asm__("st %%fsr, %0" : "=m" (fsr
));
1829 fsr
&= ~(FSR_TEM_OFM
| FSR_TEM_UFM
);
1830 __asm__("ld %0, %%fsr" : : "m" (fsr
));
1835 /* This subprogram is called by System.Task_Primitives.Operations.Enter_Task
1836 (if not null) when a new task is created. It is initialized by
1837 System.Stack_Checking.Operations.Initialize_Stack_Limit.
1838 The use of a hook avoids to drag stack checking subprograms if stack
1839 checking is not used. */
1840 void (*__gnat_set_stack_limit_hook
)(void) = (void (*)(void))0;
1842 /******************/
1843 /* NetBSD Section */
1844 /******************/
1846 #elif defined(__NetBSD__)
1852 __gnat_error_handler (int sig
)
1854 struct Exception_Data
*exception
;
1860 exception
= &constraint_error
;
1864 exception
= &constraint_error
;
1868 exception
= &storage_error
;
1869 msg
= "stack overflow or erroneous memory access";
1872 exception
= &constraint_error
;
1876 exception
= &program_error
;
1877 msg
= "unhandled signal";
1880 Raise_From_Signal_Handler(exception
, msg
);
1884 __gnat_install_handler(void)
1886 struct sigaction act
;
1888 act
.sa_handler
= __gnat_error_handler
;
1889 act
.sa_flags
= SA_NODEFER
| SA_RESTART
;
1890 sigemptyset (&act
.sa_mask
);
1892 /* Do not install handlers if interrupt state is "System". */
1893 if (__gnat_get_interrupt_state (SIGFPE
) != 's')
1894 sigaction (SIGFPE
, &act
, NULL
);
1895 if (__gnat_get_interrupt_state (SIGILL
) != 's')
1896 sigaction (SIGILL
, &act
, NULL
);
1897 if (__gnat_get_interrupt_state (SIGSEGV
) != 's')
1898 sigaction (SIGSEGV
, &act
, NULL
);
1899 if (__gnat_get_interrupt_state (SIGBUS
) != 's')
1900 sigaction (SIGBUS
, &act
, NULL
);
1902 __gnat_handler_installed
= 1;
1905 /*******************/
1906 /* OpenBSD Section */
1907 /*******************/
1909 #elif defined(__OpenBSD__)
1915 __gnat_error_handler (int sig
)
1917 struct Exception_Data
*exception
;
1923 exception
= &constraint_error
;
1927 exception
= &constraint_error
;
1931 exception
= &storage_error
;
1932 msg
= "stack overflow or erroneous memory access";
1935 exception
= &constraint_error
;
1939 exception
= &program_error
;
1940 msg
= "unhandled signal";
1943 Raise_From_Signal_Handler(exception
, msg
);
1947 __gnat_install_handler(void)
1949 struct sigaction act
;
1951 act
.sa_handler
= __gnat_error_handler
;
1952 act
.sa_flags
= SA_NODEFER
| SA_RESTART
;
1953 sigemptyset (&act
.sa_mask
);
1955 /* Do not install handlers if interrupt state is "System" */
1956 if (__gnat_get_interrupt_state (SIGFPE
) != 's')
1957 sigaction (SIGFPE
, &act
, NULL
);
1958 if (__gnat_get_interrupt_state (SIGILL
) != 's')
1959 sigaction (SIGILL
, &act
, NULL
);
1960 if (__gnat_get_interrupt_state (SIGSEGV
) != 's')
1961 sigaction (SIGSEGV
, &act
, NULL
);
1962 if (__gnat_get_interrupt_state (SIGBUS
) != 's')
1963 sigaction (SIGBUS
, &act
, NULL
);
1965 __gnat_handler_installed
= 1;
1968 /******************/
1969 /* Darwin Section */
1970 /******************/
1972 #elif defined(__APPLE__)
1975 #include <sys/syscall.h>
1976 #include <mach/mach_vm.h>
1977 #include <mach/mach_init.h>
1978 #include <mach/vm_statistics.h>
1980 /* This must be in keeping with System.OS_Interface.Alternate_Stack_Size. */
1981 char __gnat_alternate_stack
[32 * 1024]; /* 1 * MINSIGSTKSZ */
1983 /* Defined in xnu unix_signal.c.
1984 Tell the kernel to re-use alt stack when delivering a signal. */
1985 #define UC_RESET_ALT_STACK 0x80000000
1987 /* Return true if ADDR is within a stack guard area. */
1989 __gnat_is_stack_guard (mach_vm_address_t addr
)
1992 vm_region_submap_info_data_64_t info
;
1993 mach_vm_address_t start
;
1994 mach_vm_size_t size
;
1996 mach_msg_type_number_t count
;
1998 count
= VM_REGION_SUBMAP_INFO_COUNT_64
;
2002 kret
= mach_vm_region_recurse (mach_task_self (), &start
, &size
, &depth
,
2003 (vm_region_recurse_info_t
) &info
, &count
);
2004 if (kret
== KERN_SUCCESS
2005 && addr
>= start
&& addr
< (start
+ size
)
2006 && info
.protection
== VM_PROT_NONE
2007 && info
.user_tag
== VM_MEMORY_STACK
)
2012 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
2015 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED
,
2016 void *ucontext ATTRIBUTE_UNUSED
)
2018 #if defined (__x86_64__)
2019 /* Work around radar #10302855/pr50678, where the unwinders (libunwind or
2020 libgcc_s depending on the system revision) and the DWARF unwind data for
2021 the sigtramp have different ideas about register numbering (causing rbx
2022 and rdx to be transposed).. */
2023 ucontext_t
*uc
= (ucontext_t
*)ucontext
;
2024 unsigned long t
= uc
->uc_mcontext
->__ss
.__rbx
;
2026 uc
->uc_mcontext
->__ss
.__rbx
= uc
->uc_mcontext
->__ss
.__rdx
;
2027 uc
->uc_mcontext
->__ss
.__rdx
= t
;
2032 __gnat_error_handler (int sig
, siginfo_t
*si
, void *ucontext
)
2034 struct Exception_Data
*exception
;
2037 __gnat_adjust_context_for_raise (sig
, ucontext
);
2043 if (__gnat_is_stack_guard ((unsigned long)si
->si_addr
))
2045 exception
= &storage_error
;
2046 msg
= "stack overflow";
2050 exception
= &constraint_error
;
2051 msg
= "erroneous memory access";
2053 /* Reset the use of alt stack, so that the alt stack will be used
2054 for the next signal delivery.
2055 The stack can't be used in case of stack checking. */
2056 syscall (SYS_sigreturn
, NULL
, UC_RESET_ALT_STACK
);
2060 exception
= &constraint_error
;
2065 exception
= &program_error
;
2066 msg
= "unhandled signal";
2069 Raise_From_Signal_Handler (exception
, msg
);
2073 __gnat_install_handler (void)
2075 struct sigaction act
;
2077 /* Set up signal handler to map synchronous signals to appropriate
2078 exceptions. Make sure that the handler isn't interrupted by another
2079 signal that might cause a scheduling event! Also setup an alternate
2080 stack region for the handler execution so that stack overflows can be
2081 handled properly, avoiding a SEGV generation from stack usage by the
2082 handler itself (and it is required by Darwin). */
2085 stack
.ss_sp
= __gnat_alternate_stack
;
2086 stack
.ss_size
= sizeof (__gnat_alternate_stack
);
2088 sigaltstack (&stack
, NULL
);
2090 act
.sa_flags
= SA_NODEFER
| SA_RESTART
| SA_SIGINFO
;
2091 act
.sa_sigaction
= __gnat_error_handler
;
2092 sigemptyset (&act
.sa_mask
);
2094 /* Do not install handlers if interrupt state is "System". */
2095 if (__gnat_get_interrupt_state (SIGABRT
) != 's')
2096 sigaction (SIGABRT
, &act
, NULL
);
2097 if (__gnat_get_interrupt_state (SIGFPE
) != 's')
2098 sigaction (SIGFPE
, &act
, NULL
);
2099 if (__gnat_get_interrupt_state (SIGILL
) != 's')
2100 sigaction (SIGILL
, &act
, NULL
);
2102 act
.sa_flags
|= SA_ONSTACK
;
2103 if (__gnat_get_interrupt_state (SIGSEGV
) != 's')
2104 sigaction (SIGSEGV
, &act
, NULL
);
2105 if (__gnat_get_interrupt_state (SIGBUS
) != 's')
2106 sigaction (SIGBUS
, &act
, NULL
);
2108 __gnat_handler_installed
= 1;
2113 /* For all other versions of GNAT, the handler does nothing. */
2115 /*******************/
2116 /* Default Section */
2117 /*******************/
2120 __gnat_install_handler (void)
2122 __gnat_handler_installed
= 1;
2127 /*********************/
2128 /* __gnat_init_float */
2129 /*********************/
2131 /* This routine is called as each process thread is created, for possible
2132 initialization of the FP processor. This version is used under INTERIX
2135 #if defined (_WIN32) || defined (__INTERIX) \
2136 || defined (__Lynx__) || defined(__NetBSD__) || defined(__FreeBSD__) \
2137 || defined (__OpenBSD__)
2139 #define HAVE_GNAT_INIT_FLOAT
2142 __gnat_init_float (void)
2144 #if defined (__i386__) || defined (i386) || defined (__x86_64)
2146 /* This is used to properly initialize the FPU on an x86 for each
2151 #endif /* Defined __i386__ */
2155 #ifndef HAVE_GNAT_INIT_FLOAT
2157 /* All targets without a specific __gnat_init_float will use an empty one. */
2159 __gnat_init_float (void)
2164 /***********************************/
2165 /* __gnat_adjust_context_for_raise */
2166 /***********************************/
2168 #ifndef HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
2170 /* All targets without a specific version will use an empty one. */
2172 /* Given UCONTEXT a pointer to a context structure received by a signal
2173 handler for SIGNO, perform the necessary adjustments to let the handler
2174 raise an exception. Calls to this routine are not conditioned by the
2175 propagation scheme in use. */
2178 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED
,
2179 void *ucontext ATTRIBUTE_UNUSED
)
2181 /* We used to compensate here for the raised from call vs raised from signal
2182 exception discrepancy with the GCC ZCX scheme, but this now can be dealt
2183 with generically in the unwinder (see GCC PR other/26208). This however
2184 requires the use of the _Unwind_GetIPInfo routine in raise-gcc.c, which
2185 is predicated on the definition of HAVE_GETIPINFO at compile time. Only
2186 the VMS ports still do the compensation described in the few lines below.
2188 *** Call vs signal exception discrepancy with GCC ZCX scheme ***
2190 The GCC unwinder expects to be dealing with call return addresses, since
2191 this is the "nominal" case of what we retrieve while unwinding a regular
2194 To evaluate if a handler applies at some point identified by a return
2195 address, the propagation engine needs to determine what region the
2196 corresponding call instruction pertains to. Because the return address
2197 may not be attached to the same region as the call, the unwinder always
2198 subtracts "some" amount from a return address to search the region
2199 tables, amount chosen to ensure that the resulting address is inside the
2202 When we raise an exception from a signal handler, e.g. to transform a
2203 SIGSEGV into Storage_Error, things need to appear as if the signal
2204 handler had been "called" by the instruction which triggered the signal,
2205 so that exception handlers that apply there are considered. What the
2206 unwinder will retrieve as the return address from the signal handler is
2207 what it will find as the faulting instruction address in the signal
2208 context pushed by the kernel. Leaving this address untouched looses, if
2209 the triggering instruction happens to be the very first of a region, as
2210 the later adjustments performed by the unwinder would yield an address
2211 outside that region. We need to compensate for the unwinder adjustments
2212 at some point, and this is what this routine is expected to do.
2214 signo is passed because on some targets for some signals the PC in
2215 context points to the instruction after the faulting one, in which case
2216 the unwinder adjustment is still desired. */