1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 1992-2015, 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 s-init.ads, s-init.adb and the
42 s-init-*.adb variants. All these files implement the required functionality
43 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. */
49 #include "version.h" /* for _WRS_VXWORKS_MAJOR */
61 /* We don't have libiberty, so use malloc. */
62 #define xmalloc(S) malloc (S)
75 extern void __gnat_raise_program_error (const char *, int);
77 /* Addresses of exception data blocks for predefined exceptions. Tasking_Error
78 is not used in this unit, and the abort signal is only used on IRIX.
79 ??? Revisit this part since IRIX is no longer supported. */
80 extern struct Exception_Data constraint_error
;
81 extern struct Exception_Data numeric_error
;
82 extern struct Exception_Data program_error
;
83 extern struct Exception_Data storage_error
;
85 /* For the Cert run time we use the regular raise exception routine because
86 Raise_From_Signal_Handler is not available. */
88 #define Raise_From_Signal_Handler \
89 __gnat_raise_exception
90 extern void Raise_From_Signal_Handler (struct Exception_Data
*, const char *);
92 #define Raise_From_Signal_Handler \
93 ada__exceptions__raise_from_signal_handler
94 extern void Raise_From_Signal_Handler (struct Exception_Data
*, const char *);
97 /* Global values computed by the binder. Note that these variables are
98 declared here, not in the binder file, to avoid having unresolved
99 references in the shared libgnat. */
100 int __gl_main_priority
= -1;
101 int __gl_main_cpu
= -1;
102 int __gl_time_slice_val
= -1;
103 char __gl_wc_encoding
= 'n';
104 char __gl_locking_policy
= ' ';
105 char __gl_queuing_policy
= ' ';
106 char __gl_task_dispatching_policy
= ' ';
107 char *__gl_priority_specific_dispatching
= 0;
108 int __gl_num_specific_dispatching
= 0;
109 char *__gl_interrupt_states
= 0;
110 int __gl_num_interrupt_states
= 0;
111 int __gl_unreserve_all_interrupts
= 0;
112 int __gl_exception_tracebacks
= 0;
113 int __gl_exception_tracebacks_symbolic
= 0;
114 int __gl_detect_blocking
= 0;
115 int __gl_default_stack_size
= -1;
116 int __gl_leap_seconds_support
= 0;
117 int __gl_canonical_streams
= 0;
118 char *__gl_bind_env_addr
= NULL
;
120 /* This value is not used anymore, but kept for bootstrapping purpose. */
121 int __gl_zero_cost_exceptions
= 0;
123 /* Indication of whether synchronous signal handler has already been
124 installed by a previous call to adainit. */
125 int __gnat_handler_installed
= 0;
128 int __gnat_inside_elab_final_code
= 0;
129 /* ??? This variable is obsolete since 2001-08-29 but is kept to allow
130 bootstrap from old GNAT versions (< 3.15). */
133 /* HAVE_GNAT_INIT_FLOAT must be set on every targets where a __gnat_init_float
134 is defined. If this is not set then a void implementation will be defined
135 at the end of this unit. */
136 #undef HAVE_GNAT_INIT_FLOAT
138 /******************************/
139 /* __gnat_get_interrupt_state */
140 /******************************/
142 char __gnat_get_interrupt_state (int);
144 /* This routine is called from the runtime as needed to determine the state
145 of an interrupt, as set by an Interrupt_State pragma appearing anywhere
146 in the current partition. The input argument is the interrupt number,
147 and the result is one of the following:
149 'n' this interrupt not set by any Interrupt_State pragma
150 'u' Interrupt_State pragma set state to User
151 'r' Interrupt_State pragma set state to Runtime
152 's' Interrupt_State pragma set state to System */
155 __gnat_get_interrupt_state (int intrup
)
157 if (intrup
>= __gl_num_interrupt_states
)
160 return __gl_interrupt_states
[intrup
];
163 /***********************************/
164 /* __gnat_get_specific_dispatching */
165 /***********************************/
167 char __gnat_get_specific_dispatching (int);
169 /* This routine is called from the runtime as needed to determine the
170 priority specific dispatching policy, as set by a
171 Priority_Specific_Dispatching pragma appearing anywhere in the current
172 partition. The input argument is the priority number, and the result
173 is the upper case first character of the policy name, e.g. 'F' for
174 FIFO_Within_Priorities. A space ' ' is returned if no
175 Priority_Specific_Dispatching pragma is used in the partition. */
178 __gnat_get_specific_dispatching (int priority
)
180 if (__gl_num_specific_dispatching
== 0)
182 else if (priority
>= __gl_num_specific_dispatching
)
185 return __gl_priority_specific_dispatching
[priority
];
190 /**********************/
191 /* __gnat_set_globals */
192 /**********************/
194 /* This routine is kept for bootstrapping purposes, since the binder generated
195 file now sets the __gl_* variables directly. */
198 __gnat_set_globals (void)
211 #include <sys/time.h>
213 /* Some versions of AIX don't define SA_NODEFER. */
217 #endif /* SA_NODEFER */
219 /* Versions of AIX before 4.3 don't have nanosleep but provide
222 #ifndef _AIXVERSION_430
224 extern int nanosleep (struct timestruc_t
*, struct timestruc_t
*);
227 nanosleep (struct timestruc_t
*Rqtp
, struct timestruc_t
*Rmtp
)
229 return nsleep (Rqtp
, Rmtp
);
232 #endif /* _AIXVERSION_430 */
235 __gnat_error_handler (int sig
,
236 siginfo_t
*si ATTRIBUTE_UNUSED
,
237 void *ucontext ATTRIBUTE_UNUSED
)
239 struct Exception_Data
*exception
;
245 /* FIXME: we need to detect the case of a *real* SIGSEGV. */
246 exception
= &storage_error
;
247 msg
= "stack overflow or erroneous memory access";
251 exception
= &constraint_error
;
256 exception
= &constraint_error
;
261 exception
= &program_error
;
262 msg
= "unhandled signal";
265 Raise_From_Signal_Handler (exception
, msg
);
269 __gnat_install_handler (void)
271 struct sigaction act
;
273 /* Set up signal handler to map synchronous signals to appropriate
274 exceptions. Make sure that the handler isn't interrupted by another
275 signal that might cause a scheduling event! */
277 act
.sa_flags
= SA_NODEFER
| SA_RESTART
| SA_SIGINFO
;
278 act
.sa_sigaction
= __gnat_error_handler
;
279 sigemptyset (&act
.sa_mask
);
281 /* Do not install handlers if interrupt state is "System". */
282 if (__gnat_get_interrupt_state (SIGABRT
) != 's')
283 sigaction (SIGABRT
, &act
, NULL
);
284 if (__gnat_get_interrupt_state (SIGFPE
) != 's')
285 sigaction (SIGFPE
, &act
, NULL
);
286 if (__gnat_get_interrupt_state (SIGILL
) != 's')
287 sigaction (SIGILL
, &act
, NULL
);
288 if (__gnat_get_interrupt_state (SIGSEGV
) != 's')
289 sigaction (SIGSEGV
, &act
, NULL
);
290 if (__gnat_get_interrupt_state (SIGBUS
) != 's')
291 sigaction (SIGBUS
, &act
, NULL
);
293 __gnat_handler_installed
= 1;
300 #elif defined (__hpux__)
303 #include <sys/ucontext.h>
305 #if defined (IN_RTS) && defined (__ia64__)
307 #include <sys/uc_access.h>
309 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
312 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED
, void *ucontext
)
314 ucontext_t
*uc
= (ucontext_t
*) ucontext
;
317 /* Adjust on itanium, as GetIPInfo is not supported. */
318 __uc_get_ip (uc
, &ip
);
319 __uc_set_ip (uc
, ip
+ 1);
321 #endif /* IN_RTS && __ia64__ */
323 /* Tasking and Non-tasking signal handler. Map SIGnal to Ada exception
324 propagation after the required low level adjustments. */
327 __gnat_error_handler (int sig
, siginfo_t
*si ATTRIBUTE_UNUSED
, void *ucontext
)
329 struct Exception_Data
*exception
;
332 __gnat_adjust_context_for_raise (sig
, ucontext
);
337 /* FIXME: we need to detect the case of a *real* SIGSEGV. */
338 exception
= &storage_error
;
339 msg
= "stack overflow or erroneous memory access";
343 exception
= &constraint_error
;
348 exception
= &constraint_error
;
353 exception
= &program_error
;
354 msg
= "unhandled signal";
357 Raise_From_Signal_Handler (exception
, msg
);
360 /* This must be in keeping with System.OS_Interface.Alternate_Stack_Size. */
361 #if defined (__hppa__)
362 char __gnat_alternate_stack
[16 * 1024]; /* 2 * SIGSTKSZ */
364 char __gnat_alternate_stack
[128 * 1024]; /* MINSIGSTKSZ */
368 __gnat_install_handler (void)
370 struct sigaction act
;
372 /* Set up signal handler to map synchronous signals to appropriate
373 exceptions. Make sure that the handler isn't interrupted by another
374 signal that might cause a scheduling event! Also setup an alternate
375 stack region for the handler execution so that stack overflows can be
376 handled properly, avoiding a SEGV generation from stack usage by the
380 stack
.ss_sp
= __gnat_alternate_stack
;
381 stack
.ss_size
= sizeof (__gnat_alternate_stack
);
383 sigaltstack (&stack
, NULL
);
385 act
.sa_sigaction
= __gnat_error_handler
;
386 act
.sa_flags
= SA_NODEFER
| SA_RESTART
| SA_SIGINFO
;
387 sigemptyset (&act
.sa_mask
);
389 /* Do not install handlers if interrupt state is "System". */
390 if (__gnat_get_interrupt_state (SIGABRT
) != 's')
391 sigaction (SIGABRT
, &act
, NULL
);
392 if (__gnat_get_interrupt_state (SIGFPE
) != 's')
393 sigaction (SIGFPE
, &act
, NULL
);
394 if (__gnat_get_interrupt_state (SIGILL
) != 's')
395 sigaction (SIGILL
, &act
, NULL
);
396 if (__gnat_get_interrupt_state (SIGBUS
) != 's')
397 sigaction (SIGBUS
, &act
, NULL
);
398 act
.sa_flags
|= SA_ONSTACK
;
399 if (__gnat_get_interrupt_state (SIGSEGV
) != 's')
400 sigaction (SIGSEGV
, &act
, NULL
);
402 __gnat_handler_installed
= 1;
405 /*********************/
406 /* GNU/Linux Section */
407 /*********************/
409 #elif defined (__linux__)
413 #define __USE_GNU 1 /* required to get REG_EIP/RIP from glibc's ucontext.h */
414 #include <sys/ucontext.h>
416 /* GNU/Linux, which uses glibc, does not define NULL in included
420 #define NULL ((void *) 0)
425 /* MaRTE OS provides its own version of sigaction, sigfillset, and
426 sigemptyset (overriding these symbol names). We want to make sure that
427 the versions provided by the underlying C library are used here (these
428 versions are renamed by MaRTE to linux_sigaction, fake_linux_sigfillset,
429 and fake_linux_sigemptyset, respectively). The MaRTE library will not
430 always be present (it will not be linked if no tasking constructs are
431 used), so we use the weak symbol mechanism to point always to the symbols
432 defined within the C library. */
434 #pragma weak linux_sigaction
435 int linux_sigaction (int signum
, const struct sigaction
*act
,
436 struct sigaction
*oldact
)
438 return sigaction (signum
, act
, oldact
);
440 #define sigaction(signum, act, oldact) linux_sigaction (signum, act, oldact)
442 #pragma weak fake_linux_sigfillset
443 void fake_linux_sigfillset (sigset_t
*set
)
447 #define sigfillset(set) fake_linux_sigfillset (set)
449 #pragma weak fake_linux_sigemptyset
450 void fake_linux_sigemptyset (sigset_t
*set
)
454 #define sigemptyset(set) fake_linux_sigemptyset (set)
458 #if defined (__i386__) || defined (__x86_64__) || defined (__ia64__) \
459 || defined (__ARMEL__)
461 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
464 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED
, void *ucontext
)
466 mcontext_t
*mcontext
= &((ucontext_t
*) ucontext
)->uc_mcontext
;
468 /* On the i386 and x86-64 architectures, stack checking is performed by
469 means of probes with moving stack pointer, that is to say the probed
470 address is always the value of the stack pointer. Upon hitting the
471 guard page, the stack pointer therefore points to an inaccessible
472 address and an alternate signal stack is needed to run the handler.
473 But there is an additional twist: on these architectures, the EH
474 return code writes the address of the handler at the target CFA's
475 value on the stack before doing the jump. As a consequence, if
476 there is an active handler in the frame whose stack has overflowed,
477 the stack pointer must nevertheless point to an accessible address
478 by the time the EH return is executed.
480 We therefore adjust the saved value of the stack pointer by the size
481 of one page + a small dope of 4 words, in order to make sure that it
482 points to an accessible address in case it's used as the target CFA.
483 The stack checking code guarantees that this address is unused by the
484 time this happens. */
486 #if defined (__i386__)
487 unsigned long *pc
= (unsigned long *)mcontext
->gregs
[REG_EIP
];
488 /* The pattern is "orl $0x0,(%esp)" for a probe in 32-bit mode. */
489 if (signo
== SIGSEGV
&& pc
&& *pc
== 0x00240c83)
490 mcontext
->gregs
[REG_ESP
] += 4096 + 4 * sizeof (unsigned long);
491 #elif defined (__x86_64__)
492 unsigned long long *pc
= (unsigned long long *)mcontext
->gregs
[REG_RIP
];
493 if (signo
== SIGSEGV
&& pc
494 /* The pattern is "orq $0x0,(%rsp)" for a probe in 64-bit mode. */
495 && ((*pc
& 0xffffffffffLL
) == 0x00240c8348LL
496 /* The pattern may also be "orl $0x0,(%esp)" for a probe in
498 || (*pc
& 0xffffffffLL
) == 0x00240c83LL
))
499 mcontext
->gregs
[REG_RSP
] += 4096 + 4 * sizeof (unsigned long);
500 #elif defined (__ia64__)
501 /* ??? The IA-64 unwinder doesn't compensate for signals. */
503 #elif defined (__ARMEL__)
504 /* ARM Bump has to be an even number because of odd/even architecture. */
512 __gnat_error_handler (int sig
, siginfo_t
*si ATTRIBUTE_UNUSED
, void *ucontext
)
514 struct Exception_Data
*exception
;
517 /* Adjusting is required for every fault context, so adjust for this one
518 now, before we possibly trigger a recursive fault below. */
519 __gnat_adjust_context_for_raise (sig
, ucontext
);
524 /* Here we would like a discrimination test to see whether the page
525 before the faulting address is accessible. Unfortunately, Linux
526 seems to have no way of giving us the faulting address.
528 In old versions of init.c, we had a test of the page before the
532 ((long) si->esp_at_signal & - getpagesize ()))[getpagesize ()];
534 but that's wrong since it tests the stack pointer location and the
535 stack probing code may not move it until all probes succeed.
537 For now we simply do not attempt any discrimination at all. Note
538 that this is quite acceptable, since a "real" SIGSEGV can only
539 occur as the result of an erroneous program. */
540 exception
= &storage_error
;
541 msg
= "stack overflow or erroneous memory access";
545 exception
= &storage_error
;
546 msg
= "SIGBUS: possible stack overflow";
550 exception
= &constraint_error
;
555 exception
= &program_error
;
556 msg
= "unhandled signal";
559 Raise_From_Signal_Handler (exception
, msg
);
563 #define HAVE_GNAT_ALTERNATE_STACK 1
564 /* This must be in keeping with System.OS_Interface.Alternate_Stack_Size.
565 It must be larger than MINSIGSTKSZ and hopefully near 2 * SIGSTKSZ. */
566 # if 16 * 1024 < MINSIGSTKSZ
567 # error "__gnat_alternate_stack too small"
569 char __gnat_alternate_stack
[16 * 1024];
573 #include <sys/mman.h>
574 #include <native/task.h>
580 __gnat_install_handler (void)
582 struct sigaction act
;
587 if (__gl_main_priority
== -1)
590 prio
= __gl_main_priority
;
592 /* Avoid memory swapping for this program */
594 mlockall (MCL_CURRENT
|MCL_FUTURE
);
596 /* Turn the current Linux task into a native Xenomai task */
598 rt_task_shadow (&main_task
, "environment_task", prio
, T_FPU
);
601 /* Set up signal handler to map synchronous signals to appropriate
602 exceptions. Make sure that the handler isn't interrupted by another
603 signal that might cause a scheduling event! Also setup an alternate
604 stack region for the handler execution so that stack overflows can be
605 handled properly, avoiding a SEGV generation from stack usage by the
608 act
.sa_sigaction
= __gnat_error_handler
;
609 act
.sa_flags
= SA_NODEFER
| SA_RESTART
| SA_SIGINFO
;
610 sigemptyset (&act
.sa_mask
);
612 /* Do not install handlers if interrupt state is "System". */
613 if (__gnat_get_interrupt_state (SIGABRT
) != 's')
614 sigaction (SIGABRT
, &act
, NULL
);
615 if (__gnat_get_interrupt_state (SIGFPE
) != 's')
616 sigaction (SIGFPE
, &act
, NULL
);
617 if (__gnat_get_interrupt_state (SIGILL
) != 's')
618 sigaction (SIGILL
, &act
, NULL
);
619 if (__gnat_get_interrupt_state (SIGBUS
) != 's')
620 sigaction (SIGBUS
, &act
, NULL
);
621 if (__gnat_get_interrupt_state (SIGSEGV
) != 's')
623 #ifdef HAVE_GNAT_ALTERNATE_STACK
624 /* Setup an alternate stack region for the handler execution so that
625 stack overflows can be handled properly, avoiding a SEGV generation
626 from stack usage by the handler itself. */
629 stack
.ss_sp
= __gnat_alternate_stack
;
630 stack
.ss_size
= sizeof (__gnat_alternate_stack
);
632 sigaltstack (&stack
, NULL
);
634 act
.sa_flags
|= SA_ONSTACK
;
636 sigaction (SIGSEGV
, &act
, NULL
);
639 __gnat_handler_installed
= 1;
642 /*******************/
644 /*******************/
646 #elif defined (__Lynx__)
652 __gnat_error_handler (int sig
)
654 struct Exception_Data
*exception
;
660 exception
= &constraint_error
;
664 exception
= &constraint_error
;
668 exception
= &storage_error
;
669 msg
= "stack overflow or erroneous memory access";
672 exception
= &constraint_error
;
676 exception
= &program_error
;
677 msg
= "unhandled signal";
680 Raise_From_Signal_Handler (exception
, msg
);
684 __gnat_install_handler (void)
686 struct sigaction act
;
688 act
.sa_handler
= __gnat_error_handler
;
690 sigemptyset (&act
.sa_mask
);
692 /* Do not install handlers if interrupt state is "System". */
693 if (__gnat_get_interrupt_state (SIGFPE
) != 's')
694 sigaction (SIGFPE
, &act
, NULL
);
695 if (__gnat_get_interrupt_state (SIGILL
) != 's')
696 sigaction (SIGILL
, &act
, NULL
);
697 if (__gnat_get_interrupt_state (SIGSEGV
) != 's')
698 sigaction (SIGSEGV
, &act
, NULL
);
699 if (__gnat_get_interrupt_state (SIGBUS
) != 's')
700 sigaction (SIGBUS
, &act
, NULL
);
702 __gnat_handler_installed
= 1;
705 /*******************/
706 /* Solaris Section */
707 /*******************/
709 #elif defined (__sun__) && !defined (__vxworks)
713 #include <sys/ucontext.h>
714 #include <sys/regset.h>
717 __gnat_error_handler (int sig
, siginfo_t
*si
, void *ucontext ATTRIBUTE_UNUSED
)
719 struct Exception_Data
*exception
;
720 static int recurse
= 0;
726 /* If the problem was permissions, this is a constraint error.
727 Likewise if the failing address isn't maximally aligned or if
730 ??? Using a static variable here isn't task-safe, but it's
731 much too hard to do anything else and we're just determining
732 which exception to raise. */
733 if (si
->si_code
== SEGV_ACCERR
734 || (long) si
->si_addr
== 0
735 || (((long) si
->si_addr
) & 3) != 0
738 exception
= &constraint_error
;
743 /* See if the page before the faulting page is accessible. Do that
744 by trying to access it. We'd like to simply try to access
745 4096 + the faulting address, but it's not guaranteed to be
746 the actual address, just to be on the same page. */
749 ((long) si
->si_addr
& - getpagesize ()))[getpagesize ()];
750 exception
= &storage_error
;
751 msg
= "stack overflow or erroneous memory access";
756 exception
= &program_error
;
761 exception
= &constraint_error
;
766 exception
= &program_error
;
767 msg
= "unhandled signal";
771 Raise_From_Signal_Handler (exception
, msg
);
775 __gnat_install_handler (void)
777 struct sigaction act
;
779 /* Set up signal handler to map synchronous signals to appropriate
780 exceptions. Make sure that the handler isn't interrupted by another
781 signal that might cause a scheduling event! */
783 act
.sa_sigaction
= __gnat_error_handler
;
784 act
.sa_flags
= SA_NODEFER
| SA_RESTART
| SA_SIGINFO
;
785 sigemptyset (&act
.sa_mask
);
787 /* Do not install handlers if interrupt state is "System". */
788 if (__gnat_get_interrupt_state (SIGABRT
) != 's')
789 sigaction (SIGABRT
, &act
, NULL
);
790 if (__gnat_get_interrupt_state (SIGFPE
) != 's')
791 sigaction (SIGFPE
, &act
, NULL
);
792 if (__gnat_get_interrupt_state (SIGSEGV
) != 's')
793 sigaction (SIGSEGV
, &act
, NULL
);
794 if (__gnat_get_interrupt_state (SIGBUS
) != 's')
795 sigaction (SIGBUS
, &act
, NULL
);
797 __gnat_handler_installed
= 1;
806 /* Routine called from binder to override default feature values. */
807 void __gnat_set_features (void);
808 int __gnat_features_set
= 0;
809 void (*__gnat_ctrl_c_handler
) (void) = 0;
812 #define lib_get_curr_invo_context LIB$I64_GET_CURR_INVO_CONTEXT
813 #define lib_get_prev_invo_context LIB$I64_GET_PREV_INVO_CONTEXT
814 #define lib_get_invo_handle LIB$I64_GET_INVO_HANDLE
816 #define lib_get_curr_invo_context LIB$GET_CURR_INVO_CONTEXT
817 #define lib_get_prev_invo_context LIB$GET_PREV_INVO_CONTEXT
818 #define lib_get_invo_handle LIB$GET_INVO_HANDLE
821 /* Masks for facility identification. */
822 #define FAC_MASK 0x0fff0000
823 #define DECADA_M_FACILITY 0x00310000
825 /* Define macro symbols for the VMS conditions that become Ada exceptions.
826 It would be better to just include <ssdef.h> */
828 #define SS$_CONTINUE 1
829 #define SS$_ACCVIO 12
830 #define SS$_HPARITH 1284
831 #define SS$_INTDIV 1156
832 #define SS$_STKOVF 1364
833 #define SS$_CONTROLC 1617
834 #define SS$_RESIGNAL 2328
836 #define MTH$_FLOOVEMAT 1475268 /* Some ACVC_21 CXA tests */
838 /* The following codes must be resignalled, and not handled here. */
840 /* These codes are in standard message libraries. */
841 extern int C$_SIGKILL
;
842 extern int C$_SIGINT
;
843 extern int SS$_DEBUG
;
844 extern int LIB$_KEYNOTFOU
;
845 extern int LIB$_ACTIMAGE
;
847 /* These codes are non standard, which is to say the author is
848 not sure if they are defined in the standard message libraries
849 so keep them as macros for now. */
850 #define RDB$_STREAM_EOF 20480426
851 #define FDL$_UNPRIKW 11829410
852 #define CMA$_EXIT_THREAD 4227492
857 unsigned int sigargval
;
863 const struct cond_sigargs sigargs
[];
869 const struct Exception_Data
*except
;
870 unsigned int needs_adjust
; /* 1 = adjust PC, 0 = no adjust */
871 const struct cond_subtests
*subtests
;
876 unsigned short len
, mbz
;
880 /* Conditions that don't have an Ada exception counterpart must raise
881 Non_Ada_Error. Since this is defined in s-auxdec, it should only be
882 referenced by user programs, not the compiler or tools. Hence the
887 #define Status_Error ada__io_exceptions__status_error
888 extern struct Exception_Data Status_Error
;
890 #define Mode_Error ada__io_exceptions__mode_error
891 extern struct Exception_Data Mode_Error
;
893 #define Name_Error ada__io_exceptions__name_error
894 extern struct Exception_Data Name_Error
;
896 #define Use_Error ada__io_exceptions__use_error
897 extern struct Exception_Data Use_Error
;
899 #define Device_Error ada__io_exceptions__device_error
900 extern struct Exception_Data Device_Error
;
902 #define End_Error ada__io_exceptions__end_error
903 extern struct Exception_Data End_Error
;
905 #define Data_Error ada__io_exceptions__data_error
906 extern struct Exception_Data Data_Error
;
908 #define Layout_Error ada__io_exceptions__layout_error
909 extern struct Exception_Data Layout_Error
;
911 #define Non_Ada_Error system__aux_dec__non_ada_error
912 extern struct Exception_Data Non_Ada_Error
;
914 #define Coded_Exception system__vms_exception_table__coded_exception
915 extern struct Exception_Data
*Coded_Exception (void *);
917 #define Base_Code_In system__vms_exception_table__base_code_in
918 extern void *Base_Code_In (void *);
920 /* DEC Ada exceptions are not defined in a header file, so they
923 #define ADA$_ALREADY_OPEN 0x0031a594
924 #define ADA$_CONSTRAINT_ERRO 0x00318324
925 #define ADA$_DATA_ERROR 0x003192c4
926 #define ADA$_DEVICE_ERROR 0x003195e4
927 #define ADA$_END_ERROR 0x00319904
928 #define ADA$_FAC_MODE_MISMAT 0x0031a8b3
929 #define ADA$_IOSYSFAILED 0x0031af04
930 #define ADA$_KEYSIZERR 0x0031aa3c
931 #define ADA$_KEY_MISMATCH 0x0031a8e3
932 #define ADA$_LAYOUT_ERROR 0x00319c24
933 #define ADA$_LINEXCMRS 0x0031a8f3
934 #define ADA$_MAXLINEXC 0x0031a8eb
935 #define ADA$_MODE_ERROR 0x00319f44
936 #define ADA$_MRN_MISMATCH 0x0031a8db
937 #define ADA$_MRS_MISMATCH 0x0031a8d3
938 #define ADA$_NAME_ERROR 0x0031a264
939 #define ADA$_NOT_OPEN 0x0031a58c
940 #define ADA$_ORG_MISMATCH 0x0031a8bb
941 #define ADA$_PROGRAM_ERROR 0x00318964
942 #define ADA$_RAT_MISMATCH 0x0031a8cb
943 #define ADA$_RFM_MISMATCH 0x0031a8c3
944 #define ADA$_STAOVF 0x00318cac
945 #define ADA$_STATUS_ERROR 0x0031a584
946 #define ADA$_STORAGE_ERROR 0x00318c84
947 #define ADA$_UNSUPPORTED 0x0031a8ab
948 #define ADA$_USE_ERROR 0x0031a8a4
950 /* DEC Ada specific conditions. */
951 static const struct cond_except dec_ada_cond_except_table
[] =
953 {ADA$_PROGRAM_ERROR
, &program_error
, 0, 0},
954 {ADA$_USE_ERROR
, &Use_Error
, 0, 0},
955 {ADA$_KEYSIZERR
, &program_error
, 0, 0},
956 {ADA$_STAOVF
, &storage_error
, 0, 0},
957 {ADA$_CONSTRAINT_ERRO
, &constraint_error
, 0, 0},
958 {ADA$_IOSYSFAILED
, &Device_Error
, 0, 0},
959 {ADA$_LAYOUT_ERROR
, &Layout_Error
, 0, 0},
960 {ADA$_STORAGE_ERROR
, &storage_error
, 0, 0},
961 {ADA$_DATA_ERROR
, &Data_Error
, 0, 0},
962 {ADA$_DEVICE_ERROR
, &Device_Error
, 0, 0},
963 {ADA$_END_ERROR
, &End_Error
, 0, 0},
964 {ADA$_MODE_ERROR
, &Mode_Error
, 0, 0},
965 {ADA$_NAME_ERROR
, &Name_Error
, 0, 0},
966 {ADA$_STATUS_ERROR
, &Status_Error
, 0, 0},
967 {ADA$_NOT_OPEN
, &Use_Error
, 0, 0},
968 {ADA$_ALREADY_OPEN
, &Use_Error
, 0, 0},
969 {ADA$_USE_ERROR
, &Use_Error
, 0, 0},
970 {ADA$_UNSUPPORTED
, &Use_Error
, 0, 0},
971 {ADA$_FAC_MODE_MISMAT
, &Use_Error
, 0, 0},
972 {ADA$_ORG_MISMATCH
, &Use_Error
, 0, 0},
973 {ADA$_RFM_MISMATCH
, &Use_Error
, 0, 0},
974 {ADA$_RAT_MISMATCH
, &Use_Error
, 0, 0},
975 {ADA$_MRS_MISMATCH
, &Use_Error
, 0, 0},
976 {ADA$_MRN_MISMATCH
, &Use_Error
, 0, 0},
977 {ADA$_KEY_MISMATCH
, &Use_Error
, 0, 0},
978 {ADA$_MAXLINEXC
, &constraint_error
, 0, 0},
979 {ADA$_LINEXCMRS
, &constraint_error
, 0, 0},
982 /* Already handled by a pragma Import_Exception
983 in Aux_IO_Exceptions */
984 {ADA$_LOCK_ERROR
, &Lock_Error
, 0, 0},
985 {ADA$_EXISTENCE_ERROR
, &Existence_Error
, 0, 0},
986 {ADA$_KEY_ERROR
, &Key_Error
, 0, 0},
994 /* Non-DEC Ada specific conditions that map to Ada exceptions. */
996 /* Subtest for ACCVIO Constraint_Error, kept for compatibility,
997 in hindsight should have just made ACCVIO == Storage_Error. */
998 #define ACCVIO_VIRTUAL_ADDR 3
999 static const struct cond_subtests accvio_c_e
=
1000 {1, /* number of subtests below */
1002 { ACCVIO_VIRTUAL_ADDR
, 0 }
1006 /* Macro flag to adjust PC which gets off by one for some conditions,
1007 not sure if this is reliably true, PC could be off by more for
1008 HPARITH for example, unless a trapb is inserted. */
1009 #define NEEDS_ADJUST 1
1011 static const struct cond_except system_cond_except_table
[] =
1013 {MTH$_FLOOVEMAT
, &constraint_error
, 0, 0},
1014 {SS$_INTDIV
, &constraint_error
, 0, 0},
1015 {SS$_HPARITH
, &constraint_error
, NEEDS_ADJUST
, 0},
1016 {SS$_ACCVIO
, &constraint_error
, NEEDS_ADJUST
, &accvio_c_e
},
1017 {SS$_ACCVIO
, &storage_error
, NEEDS_ADJUST
, 0},
1018 {SS$_STKOVF
, &storage_error
, NEEDS_ADJUST
, 0},
1022 /* To deal with VMS conditions and their mapping to Ada exceptions,
1023 the __gnat_error_handler routine below is installed as an exception
1024 vector having precedence over DEC frame handlers. Some conditions
1025 still need to be handled by such handlers, however, in which case
1026 __gnat_error_handler needs to return SS$_RESIGNAL. Consider for
1027 instance the use of a third party library compiled with DECAda and
1028 performing its own exception handling internally.
1030 To allow some user-level flexibility, which conditions should be
1031 resignaled is controlled by a predicate function, provided with the
1032 condition value and returning a boolean indication stating whether
1033 this condition should be resignaled or not.
1035 That predicate function is called indirectly, via a function pointer,
1036 by __gnat_error_handler, and changing that pointer is allowed to the
1037 user code by way of the __gnat_set_resignal_predicate interface.
1039 The user level function may then implement what it likes, including
1040 for instance the maintenance of a dynamic data structure if the set
1041 of to be resignalled conditions has to change over the program's
1044 ??? This is not a perfect solution to deal with the possible
1045 interactions between the GNAT and the DECAda exception handling
1046 models and better (more general) schemes are studied. This is so
1047 just provided as a convenient workaround in the meantime, and
1048 should be use with caution since the implementation has been kept
1051 typedef int resignal_predicate (int code
);
1053 static const int * const cond_resignal_table
[] =
1056 (int *)CMA$_EXIT_THREAD
,
1060 (int *) RDB$_STREAM_EOF
,
1061 (int *) FDL$_UNPRIKW
,
1065 static const int facility_resignal_table
[] =
1067 0x1380000, /* RDB */
1068 0x2220000, /* SQL */
1072 /* Default GNAT predicate for resignaling conditions. */
1075 __gnat_default_resignal_p (int code
)
1079 for (i
= 0; facility_resignal_table
[i
]; i
++)
1080 if ((code
& FAC_MASK
) == facility_resignal_table
[i
])
1083 for (i
= 0, iexcept
= 0;
1084 cond_resignal_table
[i
]
1085 && !(iexcept
= LIB$
MATCH_COND (&code
, &cond_resignal_table
[i
]));
1091 /* Static pointer to predicate that the __gnat_error_handler exception
1092 vector invokes to determine if it should resignal a condition. */
1094 static resignal_predicate
*__gnat_resignal_p
= __gnat_default_resignal_p
;
1096 /* User interface to change the predicate pointer to PREDICATE. Reset to
1097 the default if PREDICATE is null. */
1100 __gnat_set_resignal_predicate (resignal_predicate
*predicate
)
1102 if (predicate
== NULL
)
1103 __gnat_resignal_p
= __gnat_default_resignal_p
;
1105 __gnat_resignal_p
= predicate
;
1108 /* Should match System.Parameters.Default_Exception_Msg_Max_Length. */
1109 #define Default_Exception_Msg_Max_Length 512
1111 /* Action routine for SYS$PUTMSG. There may be multiple
1112 conditions, each with text to be appended to MESSAGE
1113 and separated by line termination. */
1115 copy_msg (struct descriptor_s
*msgdesc
, char *message
)
1117 int len
= strlen (message
);
1120 /* Check for buffer overflow and skip. */
1121 if (len
> 0 && len
<= Default_Exception_Msg_Max_Length
- 3)
1123 strcat (message
, "\r\n");
1127 /* Check for buffer overflow and truncate if necessary. */
1128 copy_len
= (len
+ msgdesc
->len
<= Default_Exception_Msg_Max_Length
- 1 ?
1130 Default_Exception_Msg_Max_Length
- 1 - len
);
1131 strncpy (&message
[len
], msgdesc
->adr
, copy_len
);
1132 message
[len
+ copy_len
] = 0;
1137 /* Scan TABLE for a match for the condition contained in SIGARGS,
1138 and return the entry, or the empty entry if no match found. */
1139 static const struct cond_except
*
1140 scan_conditions ( int *sigargs
, const struct cond_except
*table
[])
1143 struct cond_except entry
;
1145 /* Scan the exception condition table for a match and fetch
1146 the associated GNAT exception pointer. */
1147 for (i
= 0; (*table
) [i
].cond
; i
++)
1149 unsigned int match
= LIB$
MATCH_COND (&sigargs
[1], &(*table
) [i
].cond
);
1150 const struct cond_subtests
*subtests
= (*table
) [i
].subtests
;
1156 return &(*table
) [i
];
1161 int num
= (*subtests
).num
;
1163 /* Perform subtests to differentiate exception. */
1164 for (ii
= 0; ii
< num
; ii
++)
1166 unsigned int arg
= (*subtests
).sigargs
[ii
].sigarg
;
1167 unsigned int argval
= (*subtests
).sigargs
[ii
].sigargval
;
1169 if (sigargs
[arg
] != argval
)
1176 /* All subtests passed. */
1177 if (num
== (*subtests
).num
)
1178 return &(*table
) [i
];
1183 /* No match, return the null terminating entry. */
1184 return &(*table
) [i
];
1187 /* __gnat_handle_vms_condtition is both a frame based handler
1188 for the runtime, and an exception vector for the compiler. */
1190 __gnat_handle_vms_condition (int *sigargs
, void *mechargs
)
1192 struct Exception_Data
*exception
= 0;
1193 unsigned int needs_adjust
= 0;
1195 struct descriptor_s gnat_facility
= {4, 0, "GNAT"};
1196 char message
[Default_Exception_Msg_Max_Length
];
1198 const char *msg
= "";
1200 /* Check for conditions to resignal which aren't effected by pragma
1201 Import_Exception. */
1202 if (__gnat_resignal_p (sigargs
[1]))
1203 return SS$_RESIGNAL
;
1205 /* toplev.c handles this for compiler. */
1206 if (sigargs
[1] == SS$_HPARITH
)
1207 return SS$_RESIGNAL
;
1211 /* See if it's an imported exception. Beware that registered exceptions
1212 are bound to their base code, with the severity bits masked off. */
1213 base_code
= Base_Code_In ((void *) sigargs
[1]);
1214 exception
= Coded_Exception (base_code
);
1221 struct cond_except cond
;
1222 const struct cond_except
*cond_table
;
1223 const struct cond_except
*cond_tables
[] = {dec_ada_cond_except_table
,
1224 system_cond_except_table
,
1226 unsigned int ctrlc
= SS$_CONTROLC
;
1227 unsigned int *sigint
= &C$_SIGINT
;
1228 int ctrlc_match
= LIB$
MATCH_COND (&sigargs
[1], &ctrlc
);
1229 int sigint_match
= LIB$
MATCH_COND (&sigargs
[1], &sigint
);
1231 extern int SYS$
DCLAST (void (*astadr
)(), unsigned long long astprm
,
1232 unsigned int acmode
);
1234 /* If SS$_CONTROLC has been imported as an exception, it will take
1235 priority over a Ctrl/C handler. See above. SIGINT has a
1236 different condition value due to it's DECCCRTL roots and it's
1237 the condition that gets raised for a "kill -INT". */
1238 if ((ctrlc_match
|| sigint_match
) && __gnat_ctrl_c_handler
)
1240 SYS$
DCLAST (__gnat_ctrl_c_handler
, 0, 0);
1241 return SS$_CONTINUE
;
1245 while ((cond_table
= cond_tables
[i
++]) && !exception
)
1247 cond
= *scan_conditions (sigargs
, &cond_table
);
1248 exception
= (struct Exception_Data
*) cond
.except
;
1252 needs_adjust
= cond
.needs_adjust
;
1254 /* User programs expect Non_Ada_Error to be raised if no match,
1255 reference DEC Ada test CXCONDHAN. */
1256 exception
= &Non_Ada_Error
;
1260 /* Pretty much everything is just a program error in the compiler */
1261 exception
= &program_error
;
1266 /* Subtract PC & PSL fields as per ABI for SYS$PUTMSG. */
1269 extern int SYS$
PUTMSG (void *, int (*)(), void *, unsigned long long);
1271 /* If it was a DEC Ada specific condtiion, make it GNAT otherwise
1272 keep the old facility. */
1273 if (sigargs
[1] & FAC_MASK
== DECADA_M_FACILITY
)
1274 SYS$
PUTMSG (sigargs
, copy_msg
, &gnat_facility
,
1275 (unsigned long long ) message
);
1277 SYS$
PUTMSG (sigargs
, copy_msg
, 0,
1278 (unsigned long long ) message
);
1280 /* Add back PC & PSL fields as per ABI for SYS$PUTMSG. */
1285 __gnat_adjust_context_for_raise (sigargs
[1], (void *)mechargs
);
1287 Raise_From_Signal_Handler (exception
, msg
);
1290 #if defined (IN_RTS) && defined (__IA64)
1291 /* Called only from adasigio.b32. This is a band aid to avoid going
1292 through the VMS signal handling code which results in a 0x8000 per
1293 handled exception memory leak in P2 space (see VMS source listing
1294 sys/lis/exception.lis) due to the allocation of working space that
1295 is expected to be deallocated upon return from the condition handler,
1296 which doesn't return in GNAT compiled code. */
1298 GNAT$
STOP (int *sigargs
)
1300 /* Note that there are no mechargs. We rely on the fact that condtions
1301 raised from DEClib I/O do not require an "adjust". Also the count
1302 will be off by 2, since LIB$STOP didn't get a chance to add the
1303 PC and PSL fields, so we bump it so PUTMSG comes out right. */
1305 __gnat_handle_vms_condition (sigargs
, 0);
1310 __gnat_install_handler (void)
1312 long prvhnd ATTRIBUTE_UNUSED
;
1314 #if !defined (IN_RTS)
1315 extern int SYS$
SETEXV (unsigned int vector
, int (*addres
)(),
1316 unsigned int accmode
, void *(*(prvhnd
)));
1317 SYS$
SETEXV (1, __gnat_handle_vms_condition
, 3, &prvhnd
);
1320 __gnat_handler_installed
= 1;
1323 /* __gnat_adjust_context_for_raise for Alpha - see comments along with the
1324 default version later in this file. */
1326 #if defined (IN_RTS) && defined (__alpha__)
1328 #include <vms/chfctxdef.h>
1329 #include <vms/chfdef.h>
1331 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
1334 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED
, void *ucontext
)
1336 if (signo
== SS$_HPARITH
)
1338 /* Sub one to the address of the instruction signaling the condition,
1339 located in the sigargs array. */
1341 CHF$MECH_ARRAY
* mechargs
= (CHF$MECH_ARRAY
*) ucontext
;
1342 CHF$SIGNAL_ARRAY
* sigargs
1343 = (CHF$SIGNAL_ARRAY
*) mechargs
->chf$q_mch_sig_addr
;
1345 int vcount
= sigargs
->chf$is_sig_args
;
1346 int * pc_slot
= & (&sigargs
->chf$l_sig_name
)[vcount
-2];
1354 /* __gnat_adjust_context_for_raise for ia64. */
1356 #if defined (IN_RTS) && defined (__IA64)
1358 #include <vms/chfctxdef.h>
1359 #include <vms/chfdef.h>
1361 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
1363 typedef unsigned long long u64
;
1366 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED
, void *ucontext
)
1368 /* Add one to the address of the instruction signaling the condition,
1369 located in the 64bits sigargs array. */
1371 CHF$MECH_ARRAY
* mechargs
= (CHF$MECH_ARRAY
*) ucontext
;
1373 CHF64$SIGNAL_ARRAY
*chfsig64
1374 = (CHF64$SIGNAL_ARRAY
*) mechargs
->chf$ph_mch_sig64_addr
;
1377 = (u64
*)chfsig64
+ 1 + chfsig64
->chf64$l_sig_args
;
1379 u64
* ih_pc_loc
= post_sigarray
- 2;
1386 /* Easier interface for LIB$GET_LOGICAL: put the equivalence of NAME into BUF,
1387 always NUL terminated. In case of error or if the result is longer than
1388 LEN (length of BUF) an empty string is written info BUF. */
1391 __gnat_vms_get_logical (const char *name
, char *buf
, int len
)
1393 struct descriptor_s name_desc
, result_desc
;
1395 unsigned short rlen
;
1397 /* Build the descriptor for NAME. */
1398 name_desc
.len
= strlen (name
);
1400 name_desc
.adr
= (char *)name
;
1402 /* Build the descriptor for the result. */
1403 result_desc
.len
= len
;
1404 result_desc
.mbz
= 0;
1405 result_desc
.adr
= buf
;
1407 status
= LIB$
GET_LOGICAL (&name_desc
, &result_desc
, &rlen
);
1409 if ((status
& 1) == 1 && rlen
< len
)
1415 /* Size of a page on ia64 and alpha VMS. */
1416 #define VMS_PAGESIZE 8192
1419 #define PSL__C_USER 3
1424 /* Descending region. */
1425 #define VA__M_DESCEND 1
1427 /* Get by virtual address. */
1428 #define VA___REGSUM_BY_VA 1
1430 /* Memory region summary. */
1433 unsigned long long q_region_id
;
1434 unsigned int l_flags
;
1435 unsigned int l_region_protection
;
1437 unsigned long long q_region_size
;
1438 void *pq_first_free_va
;
1441 extern int SYS$
GET_REGION_INFO (unsigned int, unsigned long long *,
1442 void *, void *, unsigned int,
1443 void *, unsigned int *);
1444 extern int SYS$
EXPREG_64 (unsigned long long *, unsigned long long,
1445 unsigned int, unsigned int, void **,
1446 unsigned long long *);
1447 extern int SYS$
SETPRT_64 (void *, unsigned long long, unsigned int,
1448 unsigned int, void **, unsigned long long *,
1451 /* Add a guard page in the memory region containing ADDR at ADDR +/- SIZE.
1452 (The sign depends on the kind of the memory region). */
1455 __gnat_set_stack_guard_page (void *addr
, unsigned long size
)
1459 unsigned long long ret_len
;
1460 unsigned int ret_prot
;
1462 unsigned long long length
;
1463 unsigned int retlen
;
1464 struct regsum buffer
;
1466 /* Get the region for ADDR. */
1467 status
= SYS$GET_REGION_INFO
1468 (VA___REGSUM_BY_VA
, NULL
, addr
, NULL
, sizeof (buffer
), &buffer
, &retlen
);
1470 if ((status
& 1) != 1)
1473 /* Extend the region. */
1474 status
= SYS$
EXPREG_64 (&buffer
.q_region_id
,
1475 size
, 0, 0, &start_va
, &length
);
1477 if ((status
& 1) != 1)
1480 /* Create a guard page. */
1481 if (!(buffer
.l_flags
& VA__M_DESCEND
))
1482 start_va
= (void *)((unsigned long long)start_va
+ length
- VMS_PAGESIZE
);
1484 status
= SYS$
SETPRT_64 (start_va
, VMS_PAGESIZE
, PSL__C_USER
, PRT__C_NA
,
1485 &ret_va
, &ret_len
, &ret_prot
);
1487 if ((status
& 1) != 1)
1492 /* Read logicals to limit the stack(s) size. */
1495 __gnat_set_stack_limit (void)
1503 /* The main stack. */
1504 __gnat_vms_get_logical ("GNAT_STACK_SIZE", value
, sizeof (value
));
1505 size
= strtoul (value
, &e
, 0);
1506 if (e
> value
&& *e
== 0)
1508 asm ("mov %0=sp" : "=r" (sp
));
1509 __gnat_set_stack_guard_page (sp
, size
* 1024);
1512 /* The register stack. */
1513 __gnat_vms_get_logical ("GNAT_RBS_SIZE", value
, sizeof (value
));
1514 size
= strtoul (value
, &e
, 0);
1515 if (e
> value
&& *e
== 0)
1517 asm ("mov %0=ar.bsp" : "=r" (sp
));
1518 __gnat_set_stack_guard_page (sp
, size
* 1024);
1524 extern int SYS$
IEEE_SET_FP_CONTROL (void *, void *, void *);
1526 #define __int64 long long
1527 #define __NEW_STARLET
1528 #include <vms/ieeedef.h>
1531 /* Feature logical name and global variable address pair.
1532 If we ever add another feature logical to this list, the
1533 feature struct will need to be enhanced to take into account
1534 possible values for *gl_addr. */
1540 /* Default values for GNAT features set by environment or binder. */
1541 int __gl_heap_size
= 64;
1543 /* Default float format is 'I' meaning IEEE. If gnatbind detetcts that a
1544 VAX Float format is specified, it will set this global variable to 'V'.
1545 Subsequently __gnat_set_features will test the variable and if set for
1546 VAX Float will call a Starlet function to enable trapping for invalid
1547 operation, drivide by zero, and overflow. This will prevent the VMS runtime
1548 (specifically OTS$CHECK_FP_MODE) from complaining about inconsistent
1549 floating point settings in a mixed language program. Ideally the setting
1550 would be determined at link time based on setttings in the object files,
1551 however the VMS linker seems to take the setting from the first object
1552 in the link, e.g. pcrt0.o which is float representation neutral. */
1553 char __gl_float_format
= 'I';
1555 /* Array feature logical names and global variable addresses. */
1556 static const struct feature features
[] =
1558 {"GNAT$NO_MALLOC_64", &__gl_heap_size
},
1563 __gnat_set_features (void)
1568 IEEE clrmsk
, setmsk
, prvmsk
;
1570 clrmsk
.ieee$q_flags
= 0LL;
1571 setmsk
.ieee$q_flags
= 0LL;
1574 /* Loop through features array and test name for enable/disable. */
1575 for (i
= 0; features
[i
].name
; i
++)
1577 __gnat_vms_get_logical (features
[i
].name
, buff
, sizeof (buff
));
1579 if (strcmp (buff
, "ENABLE") == 0
1580 || strcmp (buff
, "TRUE") == 0
1581 || strcmp (buff
, "1") == 0)
1582 *features
[i
].gl_addr
= 32;
1583 else if (strcmp (buff
, "DISABLE") == 0
1584 || strcmp (buff
, "FALSE") == 0
1585 || strcmp (buff
, "0") == 0)
1586 *features
[i
].gl_addr
= 64;
1589 /* Features to artificially limit the stack size. */
1590 __gnat_set_stack_limit ();
1593 if (__gl_float_format
== 'V')
1595 setmsk
.ieee$v_trap_enable_inv
= K_TRUE
;
1596 setmsk
.ieee$v_trap_enable_dze
= K_TRUE
;
1597 setmsk
.ieee$v_trap_enable_ovf
= K_TRUE
;
1598 SYS$
IEEE_SET_FP_CONTROL (&clrmsk
, &setmsk
, &prvmsk
);
1602 __gnat_features_set
= 1;
1605 /* Return true if the VMS version is 7.x. */
1607 extern unsigned int LIB$
GETSYI (int *, ...);
1609 #define SYI$_VERSION 0x1000
1612 __gnat_is_vms_v7 (void)
1614 struct descriptor_s desc
;
1617 int code
= SYI$_VERSION
;
1619 desc
.len
= sizeof (version
);
1623 status
= LIB$
GETSYI (&code
, 0, &desc
);
1624 if ((status
& 1) == 1 && version
[1] == '7' && version
[2] == '.')
1630 /*******************/
1631 /* FreeBSD Section */
1632 /*******************/
1634 #elif defined (__FreeBSD__) || defined (__DragonFly__)
1637 #include <sys/ucontext.h>
1641 __gnat_error_handler (int sig
,
1642 siginfo_t
*si ATTRIBUTE_UNUSED
,
1643 void *ucontext ATTRIBUTE_UNUSED
)
1645 struct Exception_Data
*exception
;
1651 exception
= &constraint_error
;
1656 exception
= &constraint_error
;
1661 exception
= &storage_error
;
1662 msg
= "stack overflow or erroneous memory access";
1666 exception
= &storage_error
;
1667 msg
= "SIGBUS: possible stack overflow";
1671 exception
= &program_error
;
1672 msg
= "unhandled signal";
1675 Raise_From_Signal_Handler (exception
, msg
);
1679 __gnat_install_handler (void)
1681 struct sigaction act
;
1683 /* Set up signal handler to map synchronous signals to appropriate
1684 exceptions. Make sure that the handler isn't interrupted by another
1685 signal that might cause a scheduling event! */
1688 = (void (*)(int, struct __siginfo
*, void*)) __gnat_error_handler
;
1689 act
.sa_flags
= SA_NODEFER
| SA_RESTART
| SA_SIGINFO
;
1690 (void) sigemptyset (&act
.sa_mask
);
1692 (void) sigaction (SIGILL
, &act
, NULL
);
1693 (void) sigaction (SIGFPE
, &act
, NULL
);
1694 (void) sigaction (SIGSEGV
, &act
, NULL
);
1695 (void) sigaction (SIGBUS
, &act
, NULL
);
1697 __gnat_handler_installed
= 1;
1700 /*************************************/
1701 /* VxWorks Section (including Vx653) */
1702 /*************************************/
1704 #elif defined(__vxworks)
1707 #include <taskLib.h>
1708 #if defined (__i386__) && !defined (VTHREADS)
1717 #if defined (ARMEL) && (_WRS_VXWORKS_MAJOR == 6) && !defined(__RTP__)
1722 #include "private/vThreadsP.h"
1727 /* Directly vectored Interrupt routines are not supported when using RTPs. */
1729 extern int __gnat_inum_to_ivec (int);
1731 /* This is needed by the GNAT run time to handle Vxworks interrupts. */
1733 __gnat_inum_to_ivec (int num
)
1735 return (int) ((long) INUM_TO_IVEC ((long) num
));
1739 #if !defined(__alpha_vxworks) && ((_WRS_VXWORKS_MAJOR != 6) && (_WRS_VXWORKS_MAJOR != 7)) && !defined(__RTP__)
1741 /* getpid is used by s-parint.adb, but is not defined by VxWorks, except
1742 on Alpha VxWorks and VxWorks 6.x (including RTPs). */
1744 extern long getpid (void);
1749 return taskIdSelf ();
1753 /* VxWorks 653 vThreads expects the field excCnt to be zeroed when a signal is.
1754 handled. The VxWorks version of longjmp does this; GCC's builtin_longjmp
1757 __gnat_clear_exception_count (void)
1760 WIND_TCB
*currentTask
= (WIND_TCB
*) taskIdSelf();
1762 currentTask
->vThreads
.excCnt
= 0;
1766 /* Handle different SIGnal to exception mappings in different VxWorks
1769 __gnat_map_signal (int sig
, siginfo_t
*si ATTRIBUTE_UNUSED
,
1770 void *sc ATTRIBUTE_UNUSED
)
1772 struct Exception_Data
*exception
;
1778 exception
= &constraint_error
;
1782 #ifdef __VXWORKSMILS__
1784 exception
= &storage_error
;
1785 msg
= "SIGILL: possible stack overflow";
1788 exception
= &storage_error
;
1792 exception
= &program_error
;
1797 exception
= &constraint_error
;
1798 msg
= "Floating point exception or SIGILL";
1801 exception
= &storage_error
;
1805 exception
= &storage_error
;
1806 msg
= "SIGBUS: possible stack overflow";
1809 #elif (_WRS_VXWORKS_MAJOR >= 6)
1811 exception
= &constraint_error
;
1815 /* In RTP mode a SIGSEGV is most likely due to a stack overflow,
1816 since stack checking uses the probing mechanism. */
1818 exception
= &storage_error
;
1819 msg
= "SIGSEGV: possible stack overflow";
1822 exception
= &program_error
;
1826 /* VxWorks 6 kernel mode with probing. SIGBUS for guard page hit */
1828 exception
= &storage_error
;
1832 exception
= &storage_error
;
1833 msg
= "SIGBUS: possible stack overflow";
1837 /* VxWorks 5: a SIGILL is most likely due to a stack overflow,
1838 since stack checking uses the stack limit mechanism. */
1840 exception
= &storage_error
;
1841 msg
= "SIGILL: possible stack overflow";
1844 exception
= &storage_error
;
1848 exception
= &program_error
;
1853 exception
= &program_error
;
1854 msg
= "unhandled signal";
1857 /* On ARM VxWorks 6.x, the guard page is left un-armed by the kernel
1858 after being violated, so subsequent violations aren't detected.
1859 so we retrieve the address of the guard page from the TCB and compare it
1860 with the page that is violated (pREG 12 in the context) and re-arm that
1861 page if there's a match. Additionally we're are assured this is a
1862 genuine stack overflow condition and and set the message and exception
1864 #if defined (ARMEL) && (_WRS_VXWORKS_MAJOR == 6) && !defined(__RTP__)
1866 /* We re-arm the guard page by marking it invalid */
1868 #define PAGE_SIZE 4096
1871 if (sig
== SIGSEGV
|| sig
== SIGBUS
|| sig
== SIGILL
)
1873 TASK_ID tid
= taskIdSelf ();
1874 WIND_TCB
*pTcb
= taskTcb (tid
);
1875 unsigned long violated_page
1876 = ((struct sigcontext
*) sc
)->sc_pregs
->r
[REG_IP
] & ~(PAGE_SIZE
- 1);
1878 if ((unsigned long) (pTcb
->pStackEnd
- PAGE_SIZE
) == violated_page
)
1880 vmStateSet (NULL
, violated_page
,
1881 PAGE_SIZE
, VM_STATE_MASK_VALID
, VM_STATE_VALID_NOT
);
1882 exception
= &storage_error
;
1887 msg
= "SIGSEGV: stack overflow";
1890 msg
= "SIGBUS: stack overflow";
1893 msg
= "SIGILL: stack overflow";
1898 #endif /* defined (ARMEL) && (_WRS_VXWORKS_MAJOR == 6) && !defined(__RTP__) */
1900 __gnat_clear_exception_count ();
1901 Raise_From_Signal_Handler (exception
, msg
);
1904 #if defined (__i386__) && !defined (VTHREADS) && _WRS_VXWORKS_MAJOR < 7
1907 __gnat_vxsim_error_handler (int sig
, siginfo_t
*si
, void *sc
);
1909 static int is_vxsim
= 0;
1912 #if defined (ARMEL) && (_WRS_VXWORKS_MAJOR >= 7)
1914 /* ARM-vx7 case with arm unwinding exceptions */
1915 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
1917 #include <arch/../regs.h>
1923 #include <ucontext.h>
1924 #endif /* __RTP__ */
1927 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED
,
1928 void *sc ATTRIBUTE_UNUSED
)
1930 /* In case of ARM exceptions, the registers context have the PC pointing
1931 to the instruction that raised the signal. However the unwinder expects
1932 the instruction to be in the range ]PC,PC+1]. */
1935 mcontext_t
*mcontext
= &((ucontext_t
*) sc
)->uc_mcontext
;
1936 pc_addr
= (uintptr_t*)&mcontext
->regs
.pc
;
1938 struct sigcontext
* sctx
= (struct sigcontext
*) sc
;
1939 pc_addr
= (uintptr_t*)&sctx
->sc_pregs
->pc
;
1941 /* ARM Bump has to be an even number because of odd/even architecture. */
1944 #endif /* ARMEL && _WRS_VXWORKS_MAJOR >= 7 */
1946 /* Tasking and Non-tasking signal handler. Map SIGnal to Ada exception
1947 propagation after the required low level adjustments. */
1950 __gnat_error_handler (int sig
, siginfo_t
*si
, void *sc
)
1954 /* VxWorks on e500v2 clears the SPE bit of the MSR when entering CPU
1955 exception state. To allow the handler and exception to work properly
1956 when they contain SPE instructions, we need to set it back before doing
1958 This mechanism is only need in kernel mode. */
1959 #if !(defined (__RTP__) || defined (CERT)) && ((CPU == PPCE500V2) || (CPU == PPC85XX))
1960 register unsigned msr
;
1961 /* Read the MSR value */
1962 asm volatile ("mfmsr %0" : "=r" (msr
));
1963 /* Force the SPE bit */
1966 asm volatile ("mtmsr %0" : : "r" (msr
));
1969 /* VxWorks will always mask out the signal during the signal handler and
1970 will reenable it on a longjmp. GNAT does not generate a longjmp to
1971 return from a signal handler so the signal will still be masked unless
1973 sigprocmask (SIG_SETMASK
, NULL
, &mask
);
1974 sigdelset (&mask
, sig
);
1975 sigprocmask (SIG_SETMASK
, &mask
, NULL
);
1977 #if defined (__ARMEL__) || defined (__PPC__) || defined (__i386__) || defined (__x86_64__)
1978 /* On certain targets, kernel mode, we process signals through a Call Frame
1979 Info trampoline, voiding the need for myriads of fallback_frame_state
1980 variants in the ZCX runtime. We have no simple way to distinguish ZCX
1981 from SJLJ here, so we do this for SJLJ as well even though this is not
1982 necessary. This only incurs a few extra instructions and a tiny
1983 amount of extra stack usage. */
1985 #ifdef HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
1986 /* We need to sometimes to adjust the PC in case of signals so that it
1987 doesn't reference the exception that actually raised the signal but the
1988 instruction before it. */
1989 __gnat_adjust_context_for_raise (sig
, sc
);
1992 #if defined (__i386__) && !defined (VTHREADS) && (__WRS_VXWORKS_MAJOR < 7)
1993 /* On x86, the vxsim signal context is subtly different and is processeed
1994 by a handler compiled especially for vxsim.
1995 Vxsim is not supported anymore on our vxworks-7 port. */
1998 __gnat_vxsim_error_handler (sig
, si
, sc
);
2001 # include "sigtramp.h"
2003 __gnat_sigtramp (sig
, (void *)si
, (void *)sc
,
2004 (__sigtramphandler_t
*)&__gnat_map_signal
);
2007 __gnat_map_signal (sig
, si
, sc
);
2011 #if defined(__leon__) && defined(_WRS_KERNEL)
2012 /* For LEON VxWorks we need to install a trap handler for stack overflow */
2014 extern void excEnt (void);
2015 /* VxWorks exception handler entry */
2018 unsigned long inst_first
;
2019 unsigned long inst_second
;
2020 unsigned long inst_third
;
2021 unsigned long inst_fourth
;
2023 /* Four instructions representing entries in the trap table */
2025 struct trap_entry
*trap_0_entry
;
2026 /* We will set the location of the entry for software trap 0 in the trap
2031 __gnat_install_handler (void)
2033 struct sigaction act
;
2034 char *model ATTRIBUTE_UNUSED
;
2036 /* Setup signal handler to map synchronous signals to appropriate
2037 exceptions. Make sure that the handler isn't interrupted by another
2038 signal that might cause a scheduling event! */
2040 act
.sa_sigaction
= __gnat_error_handler
;
2041 act
.sa_flags
= SA_SIGINFO
| SA_ONSTACK
;
2042 sigemptyset (&act
.sa_mask
);
2044 /* For VxWorks, install all signal handlers, since pragma Interrupt_State
2045 applies to vectored hardware interrupts, not signals. */
2046 sigaction (SIGFPE
, &act
, NULL
);
2047 sigaction (SIGILL
, &act
, NULL
);
2048 sigaction (SIGSEGV
, &act
, NULL
);
2049 sigaction (SIGBUS
, &act
, NULL
);
2051 #if defined(__leon__) && defined(_WRS_KERNEL)
2052 /* Specific to the LEON VxWorks kernel run-time library */
2054 /* For stack checking the compiler triggers a software trap 0 (ta 0) in
2055 case of overflow (we use the stack limit mechanism). We need to install
2056 the trap handler here for this software trap (the OS does not handle
2057 it) as if it were a data_access_exception (trap 9). We do the same as
2058 if we put in the trap table a VXSPARC_BAD_TRAP(9). Software trap 0 is
2059 located at vector 0x80, and each entry takes 4 words. */
2061 trap_0_entry
= (struct trap_entry
*)(intVecBaseGet () + 0x80 * 4);
2065 trap_0_entry
->inst_first
= 0xae102000 + 9;
2067 /* sethi %hi(excEnt), %l6 */
2069 /* The 22 most significant bits of excEnt are obtained shifting 10 times
2072 trap_0_entry
->inst_second
= 0x2d000000 + ((unsigned long)excEnt
>> 10);
2074 /* jmp %l6+%lo(excEnt) */
2076 /* The 10 least significant bits of excEnt are obtained by masking */
2078 trap_0_entry
->inst_third
= 0x81c5a000 + ((unsigned long)excEnt
& 0x3ff);
2082 trap_0_entry
->inst_fourth
= 0xa1480000;
2085 #if defined (__i386__) && !defined (VTHREADS) && _WRS_VXWORKS_MAJOR != 7
2086 /* By experiment, found that sysModel () returns the following string
2087 prefix for vxsim when running on Linux and Windows. */
2088 model
= sysModel ();
2089 if ((strncmp (model
, "Linux", 5) == 0)
2090 || (strncmp (model
, "Windows", 7) == 0))
2094 __gnat_handler_installed
= 1;
2097 #define HAVE_GNAT_INIT_FLOAT
2100 __gnat_init_float (void)
2102 /* Disable overflow/underflow exceptions on the PPC processor, needed
2103 to get correct Ada semantics. Note that for AE653 vThreads, the HW
2104 overflow settings are an OS configuration issue. The instructions
2105 below have no effect. */
2106 #if defined (_ARCH_PPC) && !defined (_SOFT_FLOAT) && (!defined (VTHREADS) || defined (__VXWORKSMILS__))
2107 #if defined (__SPE__)
2109 /* For e500v2, do nothing and leave the responsibility to install the
2110 handler and enable the exceptions to the BSP. */
2118 #if (defined (__i386__) && !defined (VTHREADS))
2119 /* This is used to properly initialize the FPU on an x86 for each
2120 process thread. Is this needed for x86_64 ??? */
2124 /* Similarly for SPARC64. Achieved by masking bits in the Trap Enable Mask
2125 field of the Floating-point Status Register (see the SPARC Architecture
2126 Manual Version 9, p 48). */
2127 #if defined (sparc64)
2129 #define FSR_TEM_NVM (1 << 27) /* Invalid operand */
2130 #define FSR_TEM_OFM (1 << 26) /* Overflow */
2131 #define FSR_TEM_UFM (1 << 25) /* Underflow */
2132 #define FSR_TEM_DZM (1 << 24) /* Division by Zero */
2133 #define FSR_TEM_NXM (1 << 23) /* Inexact result */
2137 __asm__("st %%fsr, %0" : "=m" (fsr
));
2138 fsr
&= ~(FSR_TEM_OFM
| FSR_TEM_UFM
);
2139 __asm__("ld %0, %%fsr" : : "m" (fsr
));
2144 /* This subprogram is called by System.Task_Primitives.Operations.Enter_Task
2145 (if not null) when a new task is created. It is initialized by
2146 System.Stack_Checking.Operations.Initialize_Stack_Limit.
2147 The use of a hook avoids to drag stack checking subprograms if stack
2148 checking is not used. */
2149 void (*__gnat_set_stack_limit_hook
)(void) = (void (*)(void))0;
2151 /******************/
2152 /* NetBSD Section */
2153 /******************/
2155 #elif defined(__NetBSD__)
2161 __gnat_error_handler (int sig
)
2163 struct Exception_Data
*exception
;
2169 exception
= &constraint_error
;
2173 exception
= &constraint_error
;
2177 exception
= &storage_error
;
2178 msg
= "stack overflow or erroneous memory access";
2181 exception
= &constraint_error
;
2185 exception
= &program_error
;
2186 msg
= "unhandled signal";
2189 Raise_From_Signal_Handler (exception
, msg
);
2193 __gnat_install_handler (void)
2195 struct sigaction act
;
2197 act
.sa_handler
= __gnat_error_handler
;
2198 act
.sa_flags
= SA_NODEFER
| SA_RESTART
;
2199 sigemptyset (&act
.sa_mask
);
2201 /* Do not install handlers if interrupt state is "System". */
2202 if (__gnat_get_interrupt_state (SIGFPE
) != 's')
2203 sigaction (SIGFPE
, &act
, NULL
);
2204 if (__gnat_get_interrupt_state (SIGILL
) != 's')
2205 sigaction (SIGILL
, &act
, NULL
);
2206 if (__gnat_get_interrupt_state (SIGSEGV
) != 's')
2207 sigaction (SIGSEGV
, &act
, NULL
);
2208 if (__gnat_get_interrupt_state (SIGBUS
) != 's')
2209 sigaction (SIGBUS
, &act
, NULL
);
2211 __gnat_handler_installed
= 1;
2214 /*******************/
2215 /* OpenBSD Section */
2216 /*******************/
2218 #elif defined(__OpenBSD__)
2224 __gnat_error_handler (int sig
)
2226 struct Exception_Data
*exception
;
2232 exception
= &constraint_error
;
2236 exception
= &constraint_error
;
2240 exception
= &storage_error
;
2241 msg
= "stack overflow or erroneous memory access";
2244 exception
= &constraint_error
;
2248 exception
= &program_error
;
2249 msg
= "unhandled signal";
2252 Raise_From_Signal_Handler (exception
, msg
);
2256 __gnat_install_handler (void)
2258 struct sigaction act
;
2260 act
.sa_handler
= __gnat_error_handler
;
2261 act
.sa_flags
= SA_NODEFER
| SA_RESTART
;
2262 sigemptyset (&act
.sa_mask
);
2264 /* Do not install handlers if interrupt state is "System" */
2265 if (__gnat_get_interrupt_state (SIGFPE
) != 's')
2266 sigaction (SIGFPE
, &act
, NULL
);
2267 if (__gnat_get_interrupt_state (SIGILL
) != 's')
2268 sigaction (SIGILL
, &act
, NULL
);
2269 if (__gnat_get_interrupt_state (SIGSEGV
) != 's')
2270 sigaction (SIGSEGV
, &act
, NULL
);
2271 if (__gnat_get_interrupt_state (SIGBUS
) != 's')
2272 sigaction (SIGBUS
, &act
, NULL
);
2274 __gnat_handler_installed
= 1;
2277 /******************/
2278 /* Darwin Section */
2279 /******************/
2281 #elif defined(__APPLE__)
2283 #include <TargetConditionals.h>
2286 #include <sys/syscall.h>
2287 #include <sys/sysctl.h>
2289 /* This must be in keeping with System.OS_Interface.Alternate_Stack_Size. */
2290 char __gnat_alternate_stack
[32 * 1024]; /* 1 * MINSIGSTKSZ */
2292 /* Defined in xnu unix_signal.c.
2293 Tell the kernel to re-use alt stack when delivering a signal. */
2294 #define UC_RESET_ALT_STACK 0x80000000
2296 #if !(defined (__arm__) || defined (__arm64__) || TARGET_IPHONE_SIMULATOR)
2297 #include <mach/mach_vm.h>
2298 #include <mach/mach_init.h>
2299 #include <mach/vm_statistics.h>
2303 #include <sys/ucontext.h>
2304 #include "sigtramp.h"
2307 /* Return true if ADDR is within a stack guard area. */
2309 __gnat_is_stack_guard (mach_vm_address_t addr
)
2311 #if !(defined (__arm__) || defined (__arm64__) || TARGET_IPHONE_SIMULATOR)
2313 vm_region_submap_info_data_64_t info
;
2314 mach_vm_address_t start
;
2315 mach_vm_size_t size
;
2317 mach_msg_type_number_t count
;
2319 count
= VM_REGION_SUBMAP_INFO_COUNT_64
;
2323 kret
= mach_vm_region_recurse (mach_task_self (), &start
, &size
, &depth
,
2324 (vm_region_recurse_info_t
) &info
, &count
);
2325 if (kret
== KERN_SUCCESS
2326 && addr
>= start
&& addr
< (start
+ size
)
2327 && info
.protection
== VM_PROT_NONE
2328 && info
.user_tag
== VM_MEMORY_STACK
)
2332 /* Pagezero for arm. */
2333 return addr
>= 4096;
2337 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
2339 #if defined (__x86_64__)
2341 __darwin_major_version (void)
2343 static int cache
= -1;
2346 int mib
[2] = {CTL_KERN
, KERN_OSRELEASE
};
2349 /* Find out how big the buffer needs to be (and set cache to 0
2351 if (sysctl (mib
, 2, NULL
, &len
, NULL
, 0) == 0)
2354 sysctl (mib
, 2, release
, &len
, NULL
, 0);
2355 /* Darwin releases are of the form L.M.N where L is the major
2356 version, so strtol will return L. */
2357 cache
= (int) strtol (release
, NULL
, 10);
2369 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED
,
2370 void *ucontext ATTRIBUTE_UNUSED
)
2372 #if defined (__x86_64__)
2373 if (__darwin_major_version () < 12)
2375 /* Work around radar #10302855, where the unwinders (libunwind or
2376 libgcc_s depending on the system revision) and the DWARF unwind
2377 data for sigtramp have different ideas about register numbering,
2378 causing rbx and rdx to be transposed. */
2379 ucontext_t
*uc
= (ucontext_t
*)ucontext
;
2380 unsigned long t
= uc
->uc_mcontext
->__ss
.__rbx
;
2382 uc
->uc_mcontext
->__ss
.__rbx
= uc
->uc_mcontext
->__ss
.__rdx
;
2383 uc
->uc_mcontext
->__ss
.__rdx
= t
;
2385 #elif defined(__arm64__)
2386 /* Even though the CFI is marked as a signal frame, we need this. */
2387 ucontext_t
*uc
= (ucontext_t
*)ucontext
;
2388 uc
->uc_mcontext
->__ss
.__pc
++;
2393 __gnat_map_signal (int sig
, siginfo_t
*si
, void *mcontext ATTRIBUTE_UNUSED
)
2395 struct Exception_Data
*exception
;
2402 if (__gnat_is_stack_guard ((unsigned long)si
->si_addr
))
2405 /* ??? This is a kludge to make stack checking work. The problem is
2406 that the trampoline doesn't restore LR and, consequently, doesn't
2407 make it possible to unwind past an interrupted frame which hasn"t
2408 saved LR on the stack yet. Therefore, for probes in the prologue
2409 (32-bit probes as opposed to standard 64-bit probes), we make the
2410 unwinder skip the not-yet-established frame altogether. */
2411 mcontext_t mc
= (mcontext_t
)mcontext
;
2412 if (!(*(unsigned int *)(mc
->__ss
.__pc
-1) & ((unsigned int)1 << 30)))
2413 mc
->__ss
.__pc
= mc
->__ss
.__lr
;
2415 exception
= &storage_error
;
2416 msg
= "stack overflow";
2420 exception
= &constraint_error
;
2421 msg
= "erroneous memory access";
2424 /* Reset the use of alt stack, so that the alt stack will be used
2425 for the next signal delivery.
2426 The stack can't be used in case of stack checking. */
2427 syscall (SYS_sigreturn
, NULL
, UC_RESET_ALT_STACK
);
2431 exception
= &constraint_error
;
2436 exception
= &program_error
;
2437 msg
= "unhandled signal";
2440 Raise_From_Signal_Handler (exception
, msg
);
2444 __gnat_error_handler (int sig
, siginfo_t
*si
, void *ucontext
)
2446 __gnat_adjust_context_for_raise (sig
, ucontext
);
2448 /* The Darwin libc comes with a signal trampoline, except for ARM64. */
2450 __gnat_sigtramp (sig
, (void *)si
, ucontext
,
2451 (__sigtramphandler_t
*)&__gnat_map_signal
);
2453 __gnat_map_signal (sig
, si
, ucontext
);
2458 __gnat_install_handler (void)
2460 struct sigaction act
;
2462 /* Set up signal handler to map synchronous signals to appropriate
2463 exceptions. Make sure that the handler isn't interrupted by another
2464 signal that might cause a scheduling event! Also setup an alternate
2465 stack region for the handler execution so that stack overflows can be
2466 handled properly, avoiding a SEGV generation from stack usage by the
2467 handler itself (and it is required by Darwin). */
2470 stack
.ss_sp
= __gnat_alternate_stack
;
2471 stack
.ss_size
= sizeof (__gnat_alternate_stack
);
2473 sigaltstack (&stack
, NULL
);
2475 act
.sa_flags
= SA_NODEFER
| SA_RESTART
| SA_SIGINFO
;
2476 act
.sa_sigaction
= __gnat_error_handler
;
2477 sigemptyset (&act
.sa_mask
);
2479 /* Do not install handlers if interrupt state is "System". */
2480 if (__gnat_get_interrupt_state (SIGABRT
) != 's')
2481 sigaction (SIGABRT
, &act
, NULL
);
2482 if (__gnat_get_interrupt_state (SIGFPE
) != 's')
2483 sigaction (SIGFPE
, &act
, NULL
);
2484 if (__gnat_get_interrupt_state (SIGILL
) != 's')
2485 sigaction (SIGILL
, &act
, NULL
);
2487 act
.sa_flags
|= SA_ONSTACK
;
2488 if (__gnat_get_interrupt_state (SIGSEGV
) != 's')
2489 sigaction (SIGSEGV
, &act
, NULL
);
2490 if (__gnat_get_interrupt_state (SIGBUS
) != 's')
2491 sigaction (SIGBUS
, &act
, NULL
);
2493 __gnat_handler_installed
= 1;
2496 #elif defined(__ANDROID__)
2498 /*******************/
2499 /* Android Section */
2500 /*******************/
2503 #include <sys/ucontext.h>
2504 #include "sigtramp.h"
2506 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
2509 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED
, void *ucontext
)
2511 mcontext_t
*mcontext
= &((ucontext_t
*) ucontext
)->uc_mcontext
;
2513 /* ARM Bump has to be an even number because of odd/even architecture. */
2514 ((mcontext_t
*) mcontext
)->arm_pc
+= 2;
2518 __gnat_map_signal (int sig
,
2519 siginfo_t
*si ATTRIBUTE_UNUSED
,
2520 void *mcontext ATTRIBUTE_UNUSED
)
2522 struct Exception_Data
*exception
;
2528 exception
= &storage_error
;
2529 msg
= "stack overflow or erroneous memory access";
2533 exception
= &constraint_error
;
2538 exception
= &constraint_error
;
2543 exception
= &program_error
;
2544 msg
= "unhandled signal";
2547 Raise_From_Signal_Handler (exception
, msg
);
2551 __gnat_error_handler (int sig
, siginfo_t
*si
, void *ucontext
)
2553 __gnat_adjust_context_for_raise (sig
, ucontext
);
2555 __gnat_sigtramp (sig
, (void *) si
, (void *) ucontext
,
2556 (__sigtramphandler_t
*)&__gnat_map_signal
);
2559 /* This must be in keeping with System.OS_Interface.Alternate_Stack_Size. */
2560 char __gnat_alternate_stack
[16 * 1024];
2563 __gnat_install_handler (void)
2565 struct sigaction act
;
2567 /* Set up signal handler to map synchronous signals to appropriate
2568 exceptions. Make sure that the handler isn't interrupted by another
2569 signal that might cause a scheduling event! Also setup an alternate
2570 stack region for the handler execution so that stack overflows can be
2571 handled properly, avoiding a SEGV generation from stack usage by the
2575 stack
.ss_sp
= __gnat_alternate_stack
;
2576 stack
.ss_size
= sizeof (__gnat_alternate_stack
);
2578 sigaltstack (&stack
, NULL
);
2580 act
.sa_sigaction
= __gnat_error_handler
;
2581 act
.sa_flags
= SA_NODEFER
| SA_RESTART
| SA_SIGINFO
;
2582 sigemptyset (&act
.sa_mask
);
2584 sigaction (SIGABRT
, &act
, NULL
);
2585 sigaction (SIGFPE
, &act
, NULL
);
2586 sigaction (SIGILL
, &act
, NULL
);
2587 sigaction (SIGBUS
, &act
, NULL
);
2588 act
.sa_flags
|= SA_ONSTACK
;
2589 sigaction (SIGSEGV
, &act
, NULL
);
2591 __gnat_handler_installed
= 1;
2596 /* For all other versions of GNAT, the handler does nothing. */
2598 /*******************/
2599 /* Default Section */
2600 /*******************/
2603 __gnat_install_handler (void)
2605 __gnat_handler_installed
= 1;
2610 /*********************/
2611 /* __gnat_init_float */
2612 /*********************/
2614 /* This routine is called as each process thread is created, for possible
2615 initialization of the FP processor. This version is used under INTERIX
2618 #if defined (_WIN32) || defined (__INTERIX) \
2619 || defined (__Lynx__) || defined(__NetBSD__) || defined(__FreeBSD__) \
2620 || defined (__OpenBSD__) || defined (__DragonFly__)
2622 #define HAVE_GNAT_INIT_FLOAT
2625 __gnat_init_float (void)
2627 #if defined (__i386__) || defined (__x86_64__)
2629 /* This is used to properly initialize the FPU on an x86 for each
2634 #endif /* Defined __i386__ */
2638 #ifndef HAVE_GNAT_INIT_FLOAT
2640 /* All targets without a specific __gnat_init_float will use an empty one. */
2642 __gnat_init_float (void)
2647 /***********************************/
2648 /* __gnat_adjust_context_for_raise */
2649 /***********************************/
2651 #ifndef HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
2653 /* All targets without a specific version will use an empty one. */
2655 /* Given UCONTEXT a pointer to a context structure received by a signal
2656 handler for SIGNO, perform the necessary adjustments to let the handler
2657 raise an exception. Calls to this routine are not conditioned by the
2658 propagation scheme in use. */
2661 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED
,
2662 void *ucontext ATTRIBUTE_UNUSED
)
2664 /* We used to compensate here for the raised from call vs raised from signal
2665 exception discrepancy with the GCC ZCX scheme, but this now can be dealt
2666 with generically in the unwinder (see GCC PR other/26208). This however
2667 requires the use of the _Unwind_GetIPInfo routine in raise-gcc.c, which
2668 is predicated on the definition of HAVE_GETIPINFO at compile time. Only
2669 the VMS ports still do the compensation described in the few lines below.
2671 *** Call vs signal exception discrepancy with GCC ZCX scheme ***
2673 The GCC unwinder expects to be dealing with call return addresses, since
2674 this is the "nominal" case of what we retrieve while unwinding a regular
2677 To evaluate if a handler applies at some point identified by a return
2678 address, the propagation engine needs to determine what region the
2679 corresponding call instruction pertains to. Because the return address
2680 may not be attached to the same region as the call, the unwinder always
2681 subtracts "some" amount from a return address to search the region
2682 tables, amount chosen to ensure that the resulting address is inside the
2685 When we raise an exception from a signal handler, e.g. to transform a
2686 SIGSEGV into Storage_Error, things need to appear as if the signal
2687 handler had been "called" by the instruction which triggered the signal,
2688 so that exception handlers that apply there are considered. What the
2689 unwinder will retrieve as the return address from the signal handler is
2690 what it will find as the faulting instruction address in the signal
2691 context pushed by the kernel. Leaving this address untouched looses, if
2692 the triggering instruction happens to be the very first of a region, as
2693 the later adjustments performed by the unwinder would yield an address
2694 outside that region. We need to compensate for the unwinder adjustments
2695 at some point, and this is what this routine is expected to do.
2697 signo is passed because on some targets for some signals the PC in
2698 context points to the instruction after the faulting one, in which case
2699 the unwinder adjustment is still desired. */