d5057c8ea3d0824093e03e334a0c5e224f9d1aba
[gcc.git] / gcc / ada / init.c
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * I N I T *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2013, Free Software Foundation, Inc. *
10 * *
11 * GNAT is free software; you can redistribute it and/or modify it under *
12 * terms of the GNU General Public License as published by the Free Soft- *
13 * ware Foundation; either version 3, or (at your option) any later ver- *
14 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
15 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
16 * or FITNESS FOR A PARTICULAR PURPOSE. *
17 * *
18 * As a special exception under Section 7 of GPL version 3, you are granted *
19 * additional permissions described in the GCC Runtime Library Exception, *
20 * version 3.1, as published by the Free Software Foundation. *
21 * *
22 * You should have received a copy of the GNU General Public License and *
23 * a copy of the GCC Runtime Library Exception along with this program; *
24 * see the files COPYING3 and COPYING.RUNTIME respectively. If not, see *
25 * <http://www.gnu.org/licenses/>. *
26 * *
27 * GNAT was originally developed by the GNAT team at New York University. *
28 * Extensive contributions were provided by Ada Core Technologies Inc. *
29 * *
30 ****************************************************************************/
31
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). */
40
41 /* This file should be kept synchronized with 2sinit.ads, 2sinit.adb,
42 s-init-ae653-cert.adb and s-init-xi-sparc.adb. All these files implement
43 the required functionality for different targets. */
44
45 /* The following include is here to meet the published VxWorks requirement
46 that the __vxworks header appear before any other include. */
47 #ifdef __vxworks
48 #include "vxWorks.h"
49 #endif
50
51 #ifdef __ANDROID__
52 #undef linux
53 #endif
54
55 #ifdef IN_RTS
56 #include "tconfig.h"
57 #include "tsystem.h"
58 #include <sys/stat.h>
59
60 /* We don't have libiberty, so use malloc. */
61 #define xmalloc(S) malloc (S)
62 #else
63 #include "config.h"
64 #include "system.h"
65 #endif
66
67 #include "adaint.h"
68 #include "raise.h"
69
70 #ifdef __cplusplus
71 extern "C" {
72 #endif
73
74 extern void __gnat_raise_program_error (const char *, int);
75
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;
83
84 /* For the Cert run time we use the regular raise exception routine because
85 Raise_From_Signal_Handler is not available. */
86 #ifdef CERT
87 #define Raise_From_Signal_Handler \
88 __gnat_raise_exception
89 extern void Raise_From_Signal_Handler (struct Exception_Data *, const char *);
90 #else
91 #define Raise_From_Signal_Handler \
92 ada__exceptions__raise_from_signal_handler
93 extern void Raise_From_Signal_Handler (struct Exception_Data *, const char *);
94 #endif
95
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;
114
115 /* This value is not used anymore, but kept for bootstrapping purpose. */
116 int __gl_zero_cost_exceptions = 0;
117
118 /* Indication of whether synchronous signal handler has already been
119 installed by a previous call to adainit. */
120 int __gnat_handler_installed = 0;
121
122 #ifndef IN_RTS
123 int __gnat_inside_elab_final_code = 0;
124 /* ??? This variable is obsolete since 2001-08-29 but is kept to allow
125 bootstrap from old GNAT versions (< 3.15). */
126 #endif
127
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
132
133 /******************************/
134 /* __gnat_get_interrupt_state */
135 /******************************/
136
137 char __gnat_get_interrupt_state (int);
138
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:
143
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 */
148
149 char
150 __gnat_get_interrupt_state (int intrup)
151 {
152 if (intrup >= __gl_num_interrupt_states)
153 return 'n';
154 else
155 return __gl_interrupt_states [intrup];
156 }
157
158 /***********************************/
159 /* __gnat_get_specific_dispatching */
160 /***********************************/
161
162 char __gnat_get_specific_dispatching (int);
163
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. */
171
172 char
173 __gnat_get_specific_dispatching (int priority)
174 {
175 if (__gl_num_specific_dispatching == 0)
176 return ' ';
177 else if (priority >= __gl_num_specific_dispatching)
178 return 'F';
179 else
180 return __gl_priority_specific_dispatching [priority];
181 }
182
183 #ifndef IN_RTS
184
185 /**********************/
186 /* __gnat_set_globals */
187 /**********************/
188
189 /* This routine is kept for bootstrapping purposes, since the binder generated
190 file now sets the __gl_* variables directly. */
191
192 void
193 __gnat_set_globals (void)
194 {
195 }
196
197 #endif
198
199 /***************/
200 /* AIX Section */
201 /***************/
202
203 #if defined (_AIX)
204
205 #include <signal.h>
206 #include <sys/time.h>
207
208 /* Some versions of AIX don't define SA_NODEFER. */
209
210 #ifndef SA_NODEFER
211 #define SA_NODEFER 0
212 #endif /* SA_NODEFER */
213
214 /* Versions of AIX before 4.3 don't have nanosleep but provide
215 nsleep instead. */
216
217 #ifndef _AIXVERSION_430
218
219 extern int nanosleep (struct timestruc_t *, struct timestruc_t *);
220
221 int
222 nanosleep (struct timestruc_t *Rqtp, struct timestruc_t *Rmtp)
223 {
224 return nsleep (Rqtp, Rmtp);
225 }
226
227 #endif /* _AIXVERSION_430 */
228
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.
232 */
233
234 #ifndef _AIXVERSION_530
235 #pragma weak pthread_condattr_setclock
236 int
237 pthread_condattr_setclock (pthread_condattr_t *attr, clockid_t cl) {
238 return 0;
239 }
240 #endif
241
242 static void
243 __gnat_error_handler (int sig,
244 siginfo_t *si ATTRIBUTE_UNUSED,
245 void *ucontext ATTRIBUTE_UNUSED)
246 {
247 struct Exception_Data *exception;
248 const char *msg;
249
250 switch (sig)
251 {
252 case SIGSEGV:
253 /* FIXME: we need to detect the case of a *real* SIGSEGV. */
254 exception = &storage_error;
255 msg = "stack overflow or erroneous memory access";
256 break;
257
258 case SIGBUS:
259 exception = &constraint_error;
260 msg = "SIGBUS";
261 break;
262
263 case SIGFPE:
264 exception = &constraint_error;
265 msg = "SIGFPE";
266 break;
267
268 default:
269 exception = &program_error;
270 msg = "unhandled signal";
271 }
272
273 Raise_From_Signal_Handler (exception, msg);
274 }
275
276 void
277 __gnat_install_handler (void)
278 {
279 struct sigaction act;
280
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! */
284
285 act.sa_flags = SA_NODEFER | SA_RESTART | SA_SIGINFO;
286 act.sa_sigaction = __gnat_error_handler;
287 sigemptyset (&act.sa_mask);
288
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);
300
301 __gnat_handler_installed = 1;
302 }
303
304 /*****************/
305 /* HP-UX section */
306 /*****************/
307
308 #elif defined (__hpux__)
309
310 #include <signal.h>
311 #include <sys/ucontext.h>
312
313 #if defined (IN_RTS) && defined (__ia64__)
314
315 #include <sys/uc_access.h>
316
317 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
318
319 void
320 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED, void *ucontext)
321 {
322 ucontext_t *uc = (ucontext_t *) ucontext;
323 uint64_t ip;
324
325 /* Adjust on itanium, as GetIPInfo is not supported. */
326 __uc_get_ip (uc, &ip);
327 __uc_set_ip (uc, ip + 1);
328 }
329 #endif /* IN_RTS && __ia64__ */
330
331 /* Tasking and Non-tasking signal handler. Map SIGnal to Ada exception
332 propagation after the required low level adjustments. */
333
334 static void
335 __gnat_error_handler (int sig,
336 siginfo_t *si ATTRIBUTE_UNUSED,
337 void *ucontext ATTRIBUTE_UNUSED)
338 {
339 struct Exception_Data *exception;
340 const char *msg;
341
342 __gnat_adjust_context_for_raise (sig, ucontext);
343
344 switch (sig)
345 {
346 case SIGSEGV:
347 /* FIXME: we need to detect the case of a *real* SIGSEGV. */
348 exception = &storage_error;
349 msg = "stack overflow or erroneous memory access";
350 break;
351
352 case SIGBUS:
353 exception = &constraint_error;
354 msg = "SIGBUS";
355 break;
356
357 case SIGFPE:
358 exception = &constraint_error;
359 msg = "SIGFPE";
360 break;
361
362 default:
363 exception = &program_error;
364 msg = "unhandled signal";
365 }
366
367 Raise_From_Signal_Handler (exception, msg);
368 }
369
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 */
373 #else
374 char __gnat_alternate_stack[128 * 1024]; /* MINSIGSTKSZ */
375 #endif
376
377 void
378 __gnat_install_handler (void)
379 {
380 struct sigaction act;
381
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
387 handler itself. */
388
389 stack_t stack;
390 stack.ss_sp = __gnat_alternate_stack;
391 stack.ss_size = sizeof (__gnat_alternate_stack);
392 stack.ss_flags = 0;
393 sigaltstack (&stack, NULL);
394
395 act.sa_sigaction = __gnat_error_handler;
396 act.sa_flags = SA_NODEFER | SA_RESTART | SA_SIGINFO;
397 sigemptyset (&act.sa_mask);
398
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);
411
412 __gnat_handler_installed = 1;
413 }
414
415 /*********************/
416 /* GNU/Linux Section */
417 /*********************/
418
419 #elif defined (linux)
420
421 #include <signal.h>
422
423 #define __USE_GNU 1 /* required to get REG_EIP/RIP from glibc's ucontext.h */
424 #include <sys/ucontext.h>
425
426 /* GNU/Linux, which uses glibc, does not define NULL in included
427 header files. */
428
429 #if !defined (NULL)
430 #define NULL ((void *) 0)
431 #endif
432
433 #if defined (MaRTE)
434
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. */
443
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);
448 }
449 #define sigaction(signum, act, oldact) linux_sigaction (signum, act, oldact)
450
451 #pragma weak fake_linux_sigfillset
452 void fake_linux_sigfillset (sigset_t *set) {
453 sigfillset (set);
454 }
455 #define sigfillset(set) fake_linux_sigfillset (set)
456
457 #pragma weak fake_linux_sigemptyset
458 void fake_linux_sigemptyset (sigset_t *set) {
459 sigemptyset (set);
460 }
461 #define sigemptyset(set) fake_linux_sigemptyset (set)
462
463 #endif
464
465 #if defined (i386) || defined (__x86_64__) || defined (__ia64__)
466
467 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
468
469 void
470 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED, void *ucontext)
471 {
472 mcontext_t *mcontext = &((ucontext_t *) ucontext)->uc_mcontext;
473
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.
485
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. */
491
492 #if defined (i386)
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
503 x32 mode. */
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. */
508 mcontext->sc_ip++;
509 #endif
510 }
511
512 #endif
513
514 static void
515 __gnat_error_handler (int sig, siginfo_t *si ATTRIBUTE_UNUSED, void *ucontext)
516 {
517 struct Exception_Data *exception;
518 const char *msg;
519
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);
523
524 switch (sig)
525 {
526 case SIGSEGV:
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.
530
531 In old versions of init.c, we had a test of the page before the
532 stack pointer:
533
534 ((volatile char *)
535 ((long) si->esp_at_signal & - getpagesize ()))[getpagesize ()];
536
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.
539
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";
545 break;
546
547 case SIGBUS:
548 exception = &storage_error;
549 msg = "SIGBUS: possible stack overflow";
550 break;
551
552 case SIGFPE:
553 exception = &constraint_error;
554 msg = "SIGFPE";
555 break;
556
557 default:
558 exception = &program_error;
559 msg = "unhandled signal";
560 }
561
562 Raise_From_Signal_Handler (exception, msg);
563 }
564
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 */
568 #endif
569
570 #ifdef __XENO__
571 #include <sys/mman.h>
572 #include <native/task.h>
573
574 RT_TASK main_task;
575 #endif
576
577 void
578 __gnat_install_handler (void)
579 {
580 struct sigaction act;
581
582 #ifdef __XENO__
583 int prio;
584
585 if (__gl_main_priority == -1)
586 prio = 49;
587 else
588 prio = __gl_main_priority;
589
590 /* Avoid memory swapping for this program */
591
592 mlockall (MCL_CURRENT|MCL_FUTURE);
593
594 /* Turn the current Linux task into a native Xenomai task */
595
596 rt_task_shadow(&main_task, "environment_task", prio, T_FPU);
597 #endif
598
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
604 handler itself. */
605
606 act.sa_sigaction = __gnat_error_handler;
607 act.sa_flags = SA_NODEFER | SA_RESTART | SA_SIGINFO;
608 sigemptyset (&act.sa_mask);
609
610 /* Do not install handlers if interrupt state is "System". */
611 if (__gnat_get_interrupt_state (SIGABRT) != 's')
612 sigaction (SIGABRT, &act, NULL);
613 if (__gnat_get_interrupt_state (SIGFPE) != 's')
614 sigaction (SIGFPE, &act, NULL);
615 if (__gnat_get_interrupt_state (SIGILL) != 's')
616 sigaction (SIGILL, &act, NULL);
617 if (__gnat_get_interrupt_state (SIGBUS) != 's')
618 sigaction (SIGBUS, &act, NULL);
619 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
620 {
621 #if defined (i386) || defined (__x86_64__) || defined (__powerpc__)
622 /* Setup an alternate stack region for the handler execution so that
623 stack overflows can be handled properly, avoiding a SEGV generation
624 from stack usage by the handler itself. */
625 stack_t stack;
626
627 stack.ss_sp = __gnat_alternate_stack;
628 stack.ss_size = sizeof (__gnat_alternate_stack);
629 stack.ss_flags = 0;
630 sigaltstack (&stack, NULL);
631
632 act.sa_flags |= SA_ONSTACK;
633 #endif
634 sigaction (SIGSEGV, &act, NULL);
635 }
636
637 __gnat_handler_installed = 1;
638 }
639
640 /*******************/
641 /* LynxOS Section */
642 /*******************/
643
644 #elif defined (__Lynx__)
645
646 #include <signal.h>
647 #include <unistd.h>
648
649 static void
650 __gnat_error_handler (int sig)
651 {
652 struct Exception_Data *exception;
653 const char *msg;
654
655 switch(sig)
656 {
657 case SIGFPE:
658 exception = &constraint_error;
659 msg = "SIGFPE";
660 break;
661 case SIGILL:
662 exception = &constraint_error;
663 msg = "SIGILL";
664 break;
665 case SIGSEGV:
666 exception = &storage_error;
667 msg = "stack overflow or erroneous memory access";
668 break;
669 case SIGBUS:
670 exception = &constraint_error;
671 msg = "SIGBUS";
672 break;
673 default:
674 exception = &program_error;
675 msg = "unhandled signal";
676 }
677
678 Raise_From_Signal_Handler(exception, msg);
679 }
680
681 void
682 __gnat_install_handler(void)
683 {
684 struct sigaction act;
685
686 act.sa_handler = __gnat_error_handler;
687 act.sa_flags = 0x0;
688 sigemptyset (&act.sa_mask);
689
690 /* Do not install handlers if interrupt state is "System". */
691 if (__gnat_get_interrupt_state (SIGFPE) != 's')
692 sigaction (SIGFPE, &act, NULL);
693 if (__gnat_get_interrupt_state (SIGILL) != 's')
694 sigaction (SIGILL, &act, NULL);
695 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
696 sigaction (SIGSEGV, &act, NULL);
697 if (__gnat_get_interrupt_state (SIGBUS) != 's')
698 sigaction (SIGBUS, &act, NULL);
699
700 __gnat_handler_installed = 1;
701 }
702
703 /*******************/
704 /* Solaris Section */
705 /*******************/
706
707 #elif defined (sun) && defined (__SVR4) && !defined (__vxworks)
708
709 #include <signal.h>
710 #include <siginfo.h>
711 #include <sys/ucontext.h>
712 #include <sys/regset.h>
713
714 static void
715 __gnat_error_handler (int sig, siginfo_t *si, void *ucontext ATTRIBUTE_UNUSED)
716 {
717 struct Exception_Data *exception;
718 static int recurse = 0;
719 const char *msg;
720
721 switch (sig)
722 {
723 case SIGSEGV:
724 /* If the problem was permissions, this is a constraint error.
725 Likewise if the failing address isn't maximally aligned or if
726 we've recursed.
727
728 ??? Using a static variable here isn't task-safe, but it's
729 much too hard to do anything else and we're just determining
730 which exception to raise. */
731 if (si->si_code == SEGV_ACCERR
732 || (long) si->si_addr == 0
733 || (((long) si->si_addr) & 3) != 0
734 || recurse)
735 {
736 exception = &constraint_error;
737 msg = "SIGSEGV";
738 }
739 else
740 {
741 /* See if the page before the faulting page is accessible. Do that
742 by trying to access it. We'd like to simply try to access
743 4096 + the faulting address, but it's not guaranteed to be
744 the actual address, just to be on the same page. */
745 recurse++;
746 ((volatile char *)
747 ((long) si->si_addr & - getpagesize ()))[getpagesize ()];
748 exception = &storage_error;
749 msg = "stack overflow or erroneous memory access";
750 }
751 break;
752
753 case SIGBUS:
754 exception = &program_error;
755 msg = "SIGBUS";
756 break;
757
758 case SIGFPE:
759 exception = &constraint_error;
760 msg = "SIGFPE";
761 break;
762
763 default:
764 exception = &program_error;
765 msg = "unhandled signal";
766 }
767
768 recurse = 0;
769 Raise_From_Signal_Handler (exception, msg);
770 }
771
772 void
773 __gnat_install_handler (void)
774 {
775 struct sigaction act;
776
777 /* Set up signal handler to map synchronous signals to appropriate
778 exceptions. Make sure that the handler isn't interrupted by another
779 signal that might cause a scheduling event! */
780
781 act.sa_sigaction = __gnat_error_handler;
782 act.sa_flags = SA_NODEFER | SA_RESTART | SA_SIGINFO;
783 sigemptyset (&act.sa_mask);
784
785 /* Do not install handlers if interrupt state is "System". */
786 if (__gnat_get_interrupt_state (SIGABRT) != 's')
787 sigaction (SIGABRT, &act, NULL);
788 if (__gnat_get_interrupt_state (SIGFPE) != 's')
789 sigaction (SIGFPE, &act, NULL);
790 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
791 sigaction (SIGSEGV, &act, NULL);
792 if (__gnat_get_interrupt_state (SIGBUS) != 's')
793 sigaction (SIGBUS, &act, NULL);
794
795 __gnat_handler_installed = 1;
796 }
797
798 /***************/
799 /* VMS Section */
800 /***************/
801
802 #elif defined (VMS)
803
804 /* Routine called from binder to override default feature values. */
805 void __gnat_set_features (void);
806 int __gnat_features_set = 0;
807
808 #ifdef __IA64
809 #define lib_get_curr_invo_context LIB$I64_GET_CURR_INVO_CONTEXT
810 #define lib_get_prev_invo_context LIB$I64_GET_PREV_INVO_CONTEXT
811 #define lib_get_invo_handle LIB$I64_GET_INVO_HANDLE
812 #else
813 #define lib_get_curr_invo_context LIB$GET_CURR_INVO_CONTEXT
814 #define lib_get_prev_invo_context LIB$GET_PREV_INVO_CONTEXT
815 #define lib_get_invo_handle LIB$GET_INVO_HANDLE
816 #endif
817
818 /* Define macro symbols for the VMS conditions that become Ada exceptions.
819 It would be better to just include <ssdef.h> */
820
821 #define SS$_ACCVIO 12
822 #define SS$_HPARITH 1284
823 #define SS$_INTDIV 1156
824 #define SS$_STKOVF 1364
825 #define SS$_RESIGNAL 2328
826
827 #define MTH$_FLOOVEMAT 1475268 /* Some ACVC_21 CXA tests */
828
829 /* The following codes must be resignalled, and not handled here. */
830
831 /* These codes are in standard message libraries. */
832 extern int C$_SIGKILL;
833 extern int SS$_DEBUG;
834 extern int LIB$_KEYNOTFOU;
835 extern int LIB$_ACTIMAGE;
836
837 /* These codes are non standard, which is to say the author is
838 not sure if they are defined in the standard message libraries
839 so keep them as macros for now. */
840 #define RDB$_STREAM_EOF 20480426
841 #define FDL$_UNPRIKW 11829410
842 #define CMA$_EXIT_THREAD 4227492
843
844 struct cond_sigargs {
845 unsigned int sigarg;
846 unsigned int sigargval;
847 };
848
849 struct cond_subtests {
850 unsigned int num;
851 const struct cond_sigargs sigargs[];
852 };
853
854 struct cond_except {
855 unsigned int cond;
856 const struct Exception_Data *except;
857 unsigned int needs_adjust; /* 1 = adjust PC, 0 = no adjust */
858 const struct cond_subtests *subtests;
859 };
860
861 struct descriptor_s {
862 unsigned short len, mbz;
863 __char_ptr32 adr;
864 };
865
866 /* Conditions that don't have an Ada exception counterpart must raise
867 Non_Ada_Error. Since this is defined in s-auxdec, it should only be
868 referenced by user programs, not the compiler or tools. Hence the
869 #ifdef IN_RTS. */
870
871 #ifdef IN_RTS
872
873 #define Status_Error ada__io_exceptions__status_error
874 extern struct Exception_Data Status_Error;
875
876 #define Mode_Error ada__io_exceptions__mode_error
877 extern struct Exception_Data Mode_Error;
878
879 #define Name_Error ada__io_exceptions__name_error
880 extern struct Exception_Data Name_Error;
881
882 #define Use_Error ada__io_exceptions__use_error
883 extern struct Exception_Data Use_Error;
884
885 #define Device_Error ada__io_exceptions__device_error
886 extern struct Exception_Data Device_Error;
887
888 #define End_Error ada__io_exceptions__end_error
889 extern struct Exception_Data End_Error;
890
891 #define Data_Error ada__io_exceptions__data_error
892 extern struct Exception_Data Data_Error;
893
894 #define Layout_Error ada__io_exceptions__layout_error
895 extern struct Exception_Data Layout_Error;
896
897 #define Non_Ada_Error system__aux_dec__non_ada_error
898 extern struct Exception_Data Non_Ada_Error;
899
900 #define Coded_Exception system__vms_exception_table__coded_exception
901 extern struct Exception_Data *Coded_Exception (Exception_Code);
902
903 #define Base_Code_In system__vms_exception_table__base_code_in
904 extern Exception_Code Base_Code_In (Exception_Code);
905
906 /* DEC Ada exceptions are not defined in a header file, so they
907 must be declared. */
908
909 #define FAC_MASK 0x0fff0000
910 #define MSG_MASK 0x0000fff8
911 #define DECADA_M_FACILITY 0x00310000
912
913 #define ADA$_ALREADY_OPEN 0x0031a594
914 #define ADA$_CONSTRAINT_ERRO 0x00318324
915 #define ADA$_DATA_ERROR 0x003192c4
916 #define ADA$_DEVICE_ERROR 0x003195e4
917 #define ADA$_END_ERROR 0x00319904
918 #define ADA$_FAC_MODE_MISMAT 0x0031a8b3
919 #define ADA$_IOSYSFAILED 0x0031af04
920 #define ADA$_KEYSIZERR 0x0031aa3c
921 #define ADA$_KEY_MISMATCH 0x0031a8e3
922 #define ADA$_LAYOUT_ERROR 0x00319c24
923 #define ADA$_LINEXCMRS 0x0031a8f3
924 #define ADA$_MAXLINEXC 0x0031a8eb
925 #define ADA$_MODE_ERROR 0x00319f44
926 #define ADA$_MRN_MISMATCH 0x0031a8db
927 #define ADA$_MRS_MISMATCH 0x0031a8d3
928 #define ADA$_NAME_ERROR 0x0031a264
929 #define ADA$_NOT_OPEN 0x0031a58c
930 #define ADA$_ORG_MISMATCH 0x0031a8bb
931 #define ADA$_PROGRAM_ERROR 0x00318964
932 #define ADA$_RAT_MISMATCH 0x0031a8cb
933 #define ADA$_RFM_MISMATCH 0x0031a8c3
934 #define ADA$_STAOVF 0x00318cac
935 #define ADA$_STATUS_ERROR 0x0031a584
936 #define ADA$_STORAGE_ERROR 0x00318c84
937 #define ADA$_UNSUPPORTED 0x0031a8ab
938 #define ADA$_USE_ERROR 0x0031a8a4
939
940 /* DEC Ada specific conditions. */
941 static const struct cond_except dec_ada_cond_except_table [] = {
942 {ADA$_PROGRAM_ERROR, &program_error, 0, 0},
943 {ADA$_USE_ERROR, &Use_Error, 0, 0},
944 {ADA$_KEYSIZERR, &program_error, 0, 0},
945 {ADA$_STAOVF, &storage_error, 0, 0},
946 {ADA$_CONSTRAINT_ERRO, &constraint_error, 0, 0},
947 {ADA$_IOSYSFAILED, &Device_Error, 0, 0},
948 {ADA$_LAYOUT_ERROR, &Layout_Error, 0, 0},
949 {ADA$_STORAGE_ERROR, &storage_error, 0, 0},
950 {ADA$_DATA_ERROR, &Data_Error, 0, 0},
951 {ADA$_DEVICE_ERROR, &Device_Error, 0, 0},
952 {ADA$_END_ERROR, &End_Error, 0, 0},
953 {ADA$_MODE_ERROR, &Mode_Error, 0, 0},
954 {ADA$_NAME_ERROR, &Name_Error, 0, 0},
955 {ADA$_STATUS_ERROR, &Status_Error, 0, 0},
956 {ADA$_NOT_OPEN, &Use_Error, 0, 0},
957 {ADA$_ALREADY_OPEN, &Use_Error, 0, 0},
958 {ADA$_USE_ERROR, &Use_Error, 0, 0},
959 {ADA$_UNSUPPORTED, &Use_Error, 0, 0},
960 {ADA$_FAC_MODE_MISMAT, &Use_Error, 0, 0},
961 {ADA$_ORG_MISMATCH, &Use_Error, 0, 0},
962 {ADA$_RFM_MISMATCH, &Use_Error, 0, 0},
963 {ADA$_RAT_MISMATCH, &Use_Error, 0, 0},
964 {ADA$_MRS_MISMATCH, &Use_Error, 0, 0},
965 {ADA$_MRN_MISMATCH, &Use_Error, 0, 0},
966 {ADA$_KEY_MISMATCH, &Use_Error, 0, 0},
967 {ADA$_MAXLINEXC, &constraint_error, 0, 0},
968 {ADA$_LINEXCMRS, &constraint_error, 0, 0},
969
970 #if 0
971 /* Already handled by a pragma Import_Exception
972 in Aux_IO_Exceptions */
973 {ADA$_LOCK_ERROR, &Lock_Error, 0, 0},
974 {ADA$_EXISTENCE_ERROR, &Existence_Error, 0, 0},
975 {ADA$_KEY_ERROR, &Key_Error, 0, 0},
976 #endif
977
978 {0, 0, 0, 0}
979 };
980
981 #endif /* IN_RTS */
982
983 /* Non-DEC Ada specific conditions that map to Ada exceptions. */
984
985 /* Subtest for ACCVIO Constraint_Error, kept for compatibility,
986 in hindsight should have just made ACCVIO == Storage_Error. */
987 #define ACCVIO_VIRTUAL_ADDR 3
988 static const struct cond_subtests accvio_c_e =
989 {1, /* number of subtests below */
990 {
991 {ACCVIO_VIRTUAL_ADDR, 0}
992 }
993 };
994
995 /* Macro flag to adjust PC which gets off by one for some conditions,
996 not sure if this is reliably true, PC could be off by more for
997 HPARITH for example, unless a trapb is inserted. */
998 #define NEEDS_ADJUST 1
999
1000 static const struct cond_except system_cond_except_table [] = {
1001 {MTH$_FLOOVEMAT, &constraint_error, 0, 0},
1002 {SS$_INTDIV, &constraint_error, 0, 0},
1003 {SS$_HPARITH, &constraint_error, NEEDS_ADJUST, 0},
1004 {SS$_ACCVIO, &constraint_error, NEEDS_ADJUST, &accvio_c_e},
1005 {SS$_ACCVIO, &storage_error, NEEDS_ADJUST, 0},
1006 {SS$_STKOVF, &storage_error, NEEDS_ADJUST, 0},
1007 {0, 0, 0, 0}
1008 };
1009
1010 /* To deal with VMS conditions and their mapping to Ada exceptions,
1011 the __gnat_error_handler routine below is installed as an exception
1012 vector having precedence over DEC frame handlers. Some conditions
1013 still need to be handled by such handlers, however, in which case
1014 __gnat_error_handler needs to return SS$_RESIGNAL. Consider for
1015 instance the use of a third party library compiled with DECAda and
1016 performing its own exception handling internally.
1017
1018 To allow some user-level flexibility, which conditions should be
1019 resignaled is controlled by a predicate function, provided with the
1020 condition value and returning a boolean indication stating whether
1021 this condition should be resignaled or not.
1022
1023 That predicate function is called indirectly, via a function pointer,
1024 by __gnat_error_handler, and changing that pointer is allowed to the
1025 user code by way of the __gnat_set_resignal_predicate interface.
1026
1027 The user level function may then implement what it likes, including
1028 for instance the maintenance of a dynamic data structure if the set
1029 of to be resignalled conditions has to change over the program's
1030 lifetime.
1031
1032 ??? This is not a perfect solution to deal with the possible
1033 interactions between the GNAT and the DECAda exception handling
1034 models and better (more general) schemes are studied. This is so
1035 just provided as a convenient workaround in the meantime, and
1036 should be use with caution since the implementation has been kept
1037 very simple. */
1038
1039 typedef int
1040 resignal_predicate (int code);
1041
1042 static const int * const cond_resignal_table [] = {
1043 &C$_SIGKILL,
1044 (int *)CMA$_EXIT_THREAD,
1045 &SS$_DEBUG,
1046 &LIB$_KEYNOTFOU,
1047 &LIB$_ACTIMAGE,
1048 (int *) RDB$_STREAM_EOF,
1049 (int *) FDL$_UNPRIKW,
1050 0
1051 };
1052
1053 static const int facility_resignal_table [] = {
1054 0x1380000, /* RDB */
1055 0x2220000, /* SQL */
1056 0
1057 };
1058
1059 /* Default GNAT predicate for resignaling conditions. */
1060
1061 static int
1062 __gnat_default_resignal_p (int code)
1063 {
1064 int i, iexcept;
1065
1066 for (i = 0; facility_resignal_table [i]; i++)
1067 if ((code & FAC_MASK) == facility_resignal_table [i])
1068 return 1;
1069
1070 for (i = 0, iexcept = 0;
1071 cond_resignal_table [i]
1072 && !(iexcept = LIB$MATCH_COND (&code, &cond_resignal_table [i]));
1073 i++);
1074
1075 return iexcept;
1076 }
1077
1078 /* Static pointer to predicate that the __gnat_error_handler exception
1079 vector invokes to determine if it should resignal a condition. */
1080
1081 static resignal_predicate *__gnat_resignal_p = __gnat_default_resignal_p;
1082
1083 /* User interface to change the predicate pointer to PREDICATE. Reset to
1084 the default if PREDICATE is null. */
1085
1086 void
1087 __gnat_set_resignal_predicate (resignal_predicate *predicate)
1088 {
1089 if (predicate == NULL)
1090 __gnat_resignal_p = __gnat_default_resignal_p;
1091 else
1092 __gnat_resignal_p = predicate;
1093 }
1094
1095 /* Should match System.Parameters.Default_Exception_Msg_Max_Length. */
1096 #define Default_Exception_Msg_Max_Length 512
1097
1098 /* Action routine for SYS$PUTMSG. There may be multiple
1099 conditions, each with text to be appended to MESSAGE
1100 and separated by line termination. */
1101
1102 static int
1103 copy_msg (struct descriptor_s *msgdesc, char *message)
1104 {
1105 int len = strlen (message);
1106 int copy_len;
1107
1108 /* Check for buffer overflow and skip. */
1109 if (len > 0 && len <= Default_Exception_Msg_Max_Length - 3)
1110 {
1111 strcat (message, "\r\n");
1112 len += 2;
1113 }
1114
1115 /* Check for buffer overflow and truncate if necessary. */
1116 copy_len = (len + msgdesc->len <= Default_Exception_Msg_Max_Length - 1 ?
1117 msgdesc->len :
1118 Default_Exception_Msg_Max_Length - 1 - len);
1119 strncpy (&message [len], msgdesc->adr, copy_len);
1120 message [len + copy_len] = 0;
1121
1122 return 0;
1123 }
1124
1125 /* Scan TABLE for a match for the condition contained in SIGARGS,
1126 and return the entry, or the empty entry if no match found. */
1127
1128 static const struct cond_except *
1129 scan_conditions ( int *sigargs, const struct cond_except *table [])
1130 {
1131 int i;
1132 struct cond_except entry;
1133
1134 /* Scan the exception condition table for a match and fetch
1135 the associated GNAT exception pointer. */
1136 for (i = 0; (*table) [i].cond; i++)
1137 {
1138 unsigned int match = LIB$MATCH_COND (&sigargs [1], &(*table) [i].cond);
1139 const struct cond_subtests *subtests = (*table) [i].subtests;
1140
1141 if (match)
1142 {
1143 if (!subtests)
1144 {
1145 return &(*table) [i];
1146 }
1147 else
1148 {
1149 unsigned int ii;
1150 int num = (*subtests).num;
1151
1152 /* Perform subtests to differentiate exception. */
1153 for (ii = 0; ii < num; ii++)
1154 {
1155 unsigned int arg = (*subtests).sigargs [ii].sigarg;
1156 unsigned int argval = (*subtests).sigargs [ii].sigargval;
1157
1158 if (sigargs [arg] != argval)
1159 {
1160 num = 0;
1161 break;
1162 }
1163 }
1164
1165 /* All subtests passed. */
1166 if (num == (*subtests).num)
1167 return &(*table) [i];
1168 }
1169 }
1170 }
1171
1172 /* No match, return the null terminating entry. */
1173 return &(*table) [i];
1174 }
1175
1176 long
1177 __gnat_handle_vms_condition (int *sigargs, void *mechargs)
1178 {
1179 struct Exception_Data *exception = 0;
1180 unsigned int needs_adjust = 0;
1181 Exception_Code base_code;
1182 struct descriptor_s gnat_facility = {4, 0, "GNAT"};
1183 char message [Default_Exception_Msg_Max_Length];
1184
1185 const char *msg = "";
1186
1187 /* Check for conditions to resignal which aren't effected by pragma
1188 Import_Exception. */
1189 if (__gnat_resignal_p (sigargs [1]))
1190 return SS$_RESIGNAL;
1191 #ifndef IN_RTS
1192 /* toplev.c handles this for compiler. */
1193 if (sigargs [1] == SS$_HPARITH)
1194 return SS$_RESIGNAL;
1195 #endif
1196
1197 #ifdef IN_RTS
1198 /* See if it's an imported exception. Beware that registered exceptions
1199 are bound to their base code, with the severity bits masked off. */
1200 base_code = Base_Code_In ((Exception_Code) sigargs[1]);
1201 exception = Coded_Exception (base_code);
1202 #endif
1203
1204 if (exception == 0)
1205 #ifdef IN_RTS
1206 {
1207 int i;
1208 struct cond_except cond;
1209 const struct cond_except *cond_table;
1210 const struct cond_except *cond_tables [] = {dec_ada_cond_except_table,
1211 system_cond_except_table,
1212 0};
1213
1214 i = 0;
1215 while ((cond_table = cond_tables[i++]) && !exception)
1216 {
1217 cond = *scan_conditions (sigargs, &cond_table);
1218 exception = (struct Exception_Data *) cond.except;
1219 }
1220
1221 if (exception)
1222 needs_adjust = cond.needs_adjust;
1223 else
1224 /* User programs expect Non_Ada_Error to be raised if no match,
1225 reference DEC Ada test CXCONDHAN. */
1226 exception = &Non_Ada_Error;
1227 }
1228 #else
1229 {
1230 /* Pretty much everything is just a program error in the compiler */
1231 exception = &program_error;
1232 }
1233 #endif
1234
1235 message[0] = 0;
1236 /* Subtract PC & PSL fields as per ABI for SYS$PUTMSG. */
1237 sigargs[0] -= 2;
1238
1239 /* If it was a DEC Ada specific condtiion, make it GNAT otherwise
1240 keep the old facility. */
1241 if (sigargs [1] & FAC_MASK == DECADA_M_FACILITY)
1242 SYS$PUTMSG (sigargs, copy_msg, &gnat_facility, message);
1243 else
1244 SYS$PUTMSG (sigargs, copy_msg, 0, message);
1245
1246 /* Add back PC & PSL fields as per ABI for SYS$PUTMSG. */
1247 sigargs[0] += 2;
1248 msg = message;
1249
1250 if (needs_adjust)
1251 __gnat_adjust_context_for_raise (sigargs [1], (void *)mechargs);
1252
1253 Raise_From_Signal_Handler (exception, msg);
1254 }
1255
1256 void
1257 __gnat_install_handler (void)
1258 {
1259 long prvhnd ATTRIBUTE_UNUSED;
1260
1261 #if !defined (IN_RTS)
1262 SYS$SETEXV (1, __gnat_handle_vms_condition, 3, &prvhnd);
1263 #endif
1264
1265 __gnat_handler_installed = 1;
1266 }
1267
1268 /* __gnat_adjust_context_for_raise for Alpha - see comments along with the
1269 default version later in this file. */
1270
1271 #if defined (IN_RTS) && defined (__alpha__)
1272
1273 #include <vms/chfctxdef.h>
1274 #include <vms/chfdef.h>
1275
1276 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
1277
1278 void
1279 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED, void *ucontext)
1280 {
1281 if (signo == SS$_HPARITH)
1282 {
1283 /* Sub one to the address of the instruction signaling the condition,
1284 located in the sigargs array. */
1285
1286 CHF$MECH_ARRAY * mechargs = (CHF$MECH_ARRAY *) ucontext;
1287 CHF$SIGNAL_ARRAY * sigargs
1288 = (CHF$SIGNAL_ARRAY *) mechargs->chf$q_mch_sig_addr;
1289
1290 int vcount = sigargs->chf$is_sig_args;
1291 int * pc_slot = & (&sigargs->chf$l_sig_name)[vcount-2];
1292
1293 (*pc_slot)--;
1294 }
1295 }
1296
1297 #endif
1298
1299 /* __gnat_adjust_context_for_raise for ia64. */
1300
1301 #if defined (IN_RTS) && defined (__IA64)
1302
1303 #include <vms/chfctxdef.h>
1304 #include <vms/chfdef.h>
1305
1306 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
1307
1308 typedef unsigned long long u64;
1309
1310 void
1311 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED, void *ucontext)
1312 {
1313 /* Add one to the address of the instruction signaling the condition,
1314 located in the 64bits sigargs array. */
1315
1316 CHF$MECH_ARRAY * mechargs = (CHF$MECH_ARRAY *) ucontext;
1317
1318 CHF64$SIGNAL_ARRAY *chfsig64
1319 = (CHF64$SIGNAL_ARRAY *) mechargs->chf$ph_mch_sig64_addr;
1320
1321 u64 * post_sigarray
1322 = (u64 *)chfsig64 + 1 + chfsig64->chf64$l_sig_args;
1323
1324 u64 * ih_pc_loc = post_sigarray - 2;
1325
1326 (*ih_pc_loc) ++;
1327 }
1328
1329 #endif
1330
1331 /* Easier interface for LIB$GET_LOGICAL: put the equivalence of NAME into BUF,
1332 always NUL terminated. In case of error or if the result is longer than
1333 LEN (length of BUF) an empty string is written info BUF. */
1334
1335 static void
1336 __gnat_vms_get_logical (const char *name, char *buf, int len)
1337 {
1338 struct descriptor_s name_desc, result_desc;
1339 int status;
1340 unsigned short rlen;
1341
1342 /* Build the descriptor for NAME. */
1343 name_desc.len = strlen (name);
1344 name_desc.mbz = 0;
1345 name_desc.adr = (char *)name;
1346
1347 /* Build the descriptor for the result. */
1348 result_desc.len = len;
1349 result_desc.mbz = 0;
1350 result_desc.adr = buf;
1351
1352 status = LIB$GET_LOGICAL (&name_desc, &result_desc, &rlen);
1353
1354 if ((status & 1) == 1 && rlen < len)
1355 buf[rlen] = 0;
1356 else
1357 buf[0] = 0;
1358 }
1359
1360 /* Size of a page on ia64 and alpha VMS. */
1361 #define VMS_PAGESIZE 8192
1362
1363 /* User mode. */
1364 #define PSL__C_USER 3
1365
1366 /* No access. */
1367 #define PRT__C_NA 0
1368
1369 /* Descending region. */
1370 #define VA__M_DESCEND 1
1371
1372 /* Get by virtual address. */
1373 #define VA___REGSUM_BY_VA 1
1374
1375 /* Memory region summary. */
1376 struct regsum
1377 {
1378 unsigned long long q_region_id;
1379 unsigned int l_flags;
1380 unsigned int l_region_protection;
1381 void *pq_start_va;
1382 unsigned long long q_region_size;
1383 void *pq_first_free_va;
1384 };
1385
1386 extern int SYS$GET_REGION_INFO (unsigned int, unsigned long long *,
1387 void *, void *, unsigned int,
1388 void *, unsigned int *);
1389 extern int SYS$EXPREG_64 (unsigned long long *, unsigned long long,
1390 unsigned int, unsigned int, void **,
1391 unsigned long long *);
1392 extern int SYS$SETPRT_64 (void *, unsigned long long, unsigned int,
1393 unsigned int, void **, unsigned long long *,
1394 unsigned int *);
1395 extern int SYS$PUTMSG (void *, int (*)(), void *, unsigned long long);
1396
1397 /* Add a guard page in the memory region containing ADDR at ADDR +/- SIZE.
1398 (The sign depends on the kind of the memory region). */
1399
1400 static int
1401 __gnat_set_stack_guard_page (void *addr, unsigned long size)
1402 {
1403 int status;
1404 void *ret_va;
1405 unsigned long long ret_len;
1406 unsigned int ret_prot;
1407 void *start_va;
1408 unsigned long long length;
1409 unsigned int retlen;
1410 struct regsum buffer;
1411
1412 /* Get the region for ADDR. */
1413 status = SYS$GET_REGION_INFO
1414 (VA___REGSUM_BY_VA, NULL, addr, NULL, sizeof (buffer), &buffer, &retlen);
1415
1416 if ((status & 1) != 1)
1417 return -1;
1418
1419 /* Extend the region. */
1420 status = SYS$EXPREG_64 (&buffer.q_region_id,
1421 size, 0, 0, &start_va, &length);
1422
1423 if ((status & 1) != 1)
1424 return -1;
1425
1426 /* Create a guard page. */
1427 if (!(buffer.l_flags & VA__M_DESCEND))
1428 start_va = (void *)((unsigned long long)start_va + length - VMS_PAGESIZE);
1429
1430 status = SYS$SETPRT_64 (start_va, VMS_PAGESIZE, PSL__C_USER, PRT__C_NA,
1431 &ret_va, &ret_len, &ret_prot);
1432
1433 if ((status & 1) != 1)
1434 return -1;
1435 return 0;
1436 }
1437
1438 /* Read logicals to limit the stack(s) size. */
1439
1440 static void
1441 __gnat_set_stack_limit (void)
1442 {
1443 #ifdef __ia64__
1444 void *sp;
1445 unsigned long size;
1446 char value[16];
1447 char *e;
1448
1449 /* The main stack. */
1450 __gnat_vms_get_logical ("GNAT_STACK_SIZE", value, sizeof (value));
1451 size = strtoul (value, &e, 0);
1452 if (e > value && *e == 0)
1453 {
1454 asm ("mov %0=sp" : "=r" (sp));
1455 __gnat_set_stack_guard_page (sp, size * 1024);
1456 }
1457
1458 /* The register stack. */
1459 __gnat_vms_get_logical ("GNAT_RBS_SIZE", value, sizeof (value));
1460 size = strtoul (value, &e, 0);
1461 if (e > value && *e == 0)
1462 {
1463 asm ("mov %0=ar.bsp" : "=r" (sp));
1464 __gnat_set_stack_guard_page (sp, size * 1024);
1465 }
1466 #endif
1467 }
1468
1469 /* Feature logical name and global variable address pair.
1470 If we ever add another feature logical to this list, the
1471 feature struct will need to be enhanced to take into account
1472 possible values for *gl_addr. */
1473 struct feature {
1474 const char *name;
1475 int *gl_addr;
1476 };
1477
1478 /* Default values for GNAT features set by environment. */
1479 int __gl_heap_size = 64;
1480
1481 /* Array feature logical names and global variable addresses. */
1482 static const struct feature features[] = {
1483 {"GNAT$NO_MALLOC_64", &__gl_heap_size},
1484 {0, 0}
1485 };
1486
1487 void
1488 __gnat_set_features (void)
1489 {
1490 int i;
1491 char buff[16];
1492
1493 /* Loop through features array and test name for enable/disable. */
1494 for (i = 0; features[i].name; i++)
1495 {
1496 __gnat_vms_get_logical (features[i].name, buff, sizeof (buff));
1497
1498 if (strcmp (buff, "ENABLE") == 0
1499 || strcmp (buff, "TRUE") == 0
1500 || strcmp (buff, "1") == 0)
1501 *features[i].gl_addr = 32;
1502 else if (strcmp (buff, "DISABLE") == 0
1503 || strcmp (buff, "FALSE") == 0
1504 || strcmp (buff, "0") == 0)
1505 *features[i].gl_addr = 64;
1506 }
1507
1508 /* Features to artificially limit the stack size. */
1509 __gnat_set_stack_limit ();
1510
1511 __gnat_features_set = 1;
1512 }
1513
1514 /* Return true if the VMS version is 7.x. */
1515
1516 extern unsigned int LIB$GETSYI (int *, ...);
1517
1518 #define SYI$_VERSION 0x1000
1519
1520 int
1521 __gnat_is_vms_v7 (void)
1522 {
1523 struct descriptor_s desc;
1524 char version[8];
1525 int status;
1526 int code = SYI$_VERSION;
1527
1528 desc.len = sizeof (version);
1529 desc.mbz = 0;
1530 desc.adr = version;
1531
1532 status = LIB$GETSYI (&code, 0, &desc);
1533 if ((status & 1) == 1 && version[1] == '7' && version[2] == '.')
1534 return 1;
1535 else
1536 return 0;
1537 }
1538
1539 /*******************/
1540 /* FreeBSD Section */
1541 /*******************/
1542
1543 #elif defined (__FreeBSD__)
1544
1545 #include <signal.h>
1546 #include <sys/ucontext.h>
1547 #include <unistd.h>
1548
1549 static void
1550 __gnat_error_handler (int sig,
1551 siginfo_t *si ATTRIBUTE_UNUSED,
1552 void *ucontext ATTRIBUTE_UNUSED)
1553 {
1554 struct Exception_Data *exception;
1555 const char *msg;
1556
1557 switch (sig)
1558 {
1559 case SIGFPE:
1560 exception = &constraint_error;
1561 msg = "SIGFPE";
1562 break;
1563
1564 case SIGILL:
1565 exception = &constraint_error;
1566 msg = "SIGILL";
1567 break;
1568
1569 case SIGSEGV:
1570 exception = &storage_error;
1571 msg = "stack overflow or erroneous memory access";
1572 break;
1573
1574 case SIGBUS:
1575 exception = &storage_error;
1576 msg = "SIGBUS: possible stack overflow";
1577 break;
1578
1579 default:
1580 exception = &program_error;
1581 msg = "unhandled signal";
1582 }
1583
1584 Raise_From_Signal_Handler (exception, msg);
1585 }
1586
1587 void
1588 __gnat_install_handler ()
1589 {
1590 struct sigaction act;
1591
1592 /* Set up signal handler to map synchronous signals to appropriate
1593 exceptions. Make sure that the handler isn't interrupted by another
1594 signal that might cause a scheduling event! */
1595
1596 act.sa_sigaction
1597 = (void (*)(int, struct __siginfo *, void*)) __gnat_error_handler;
1598 act.sa_flags = SA_NODEFER | SA_RESTART | SA_SIGINFO;
1599 (void) sigemptyset (&act.sa_mask);
1600
1601 (void) sigaction (SIGILL, &act, NULL);
1602 (void) sigaction (SIGFPE, &act, NULL);
1603 (void) sigaction (SIGSEGV, &act, NULL);
1604 (void) sigaction (SIGBUS, &act, NULL);
1605
1606 __gnat_handler_installed = 1;
1607 }
1608
1609 /*******************/
1610 /* VxWorks Section */
1611 /*******************/
1612
1613 #elif defined(__vxworks)
1614
1615 #include <signal.h>
1616 #include <taskLib.h>
1617
1618 #ifndef __RTP__
1619 #include <intLib.h>
1620 #include <iv.h>
1621 #endif
1622
1623 #ifdef VTHREADS
1624 #include "private/vThreadsP.h"
1625 #endif
1626
1627 void __gnat_error_handler (int, void *, struct sigcontext *);
1628
1629 #ifndef __RTP__
1630
1631 /* Directly vectored Interrupt routines are not supported when using RTPs. */
1632
1633 extern int __gnat_inum_to_ivec (int);
1634
1635 /* This is needed by the GNAT run time to handle Vxworks interrupts. */
1636 int
1637 __gnat_inum_to_ivec (int num)
1638 {
1639 return INUM_TO_IVEC (num);
1640 }
1641 #endif
1642
1643 #if !defined(__alpha_vxworks) && (_WRS_VXWORKS_MAJOR != 6) && !defined(__RTP__)
1644
1645 /* getpid is used by s-parint.adb, but is not defined by VxWorks, except
1646 on Alpha VxWorks and VxWorks 6.x (including RTPs). */
1647
1648 extern long getpid (void);
1649
1650 long
1651 getpid (void)
1652 {
1653 return taskIdSelf ();
1654 }
1655 #endif
1656
1657 /* VxWorks 653 vThreads expects the field excCnt to be zeroed when a signal is.
1658 handled. The VxWorks version of longjmp does this; GCC's builtin_longjmp
1659 doesn't. */
1660 void
1661 __gnat_clear_exception_count (void)
1662 {
1663 #ifdef VTHREADS
1664 WIND_TCB *currentTask = (WIND_TCB *) taskIdSelf();
1665
1666 currentTask->vThreads.excCnt = 0;
1667 #endif
1668 }
1669
1670 /* Handle different SIGnal to exception mappings in different VxWorks
1671 versions. */
1672 static void
1673 __gnat_map_signal (int sig, void *si ATTRIBUTE_UNUSED,
1674 struct sigcontext *sc ATTRIBUTE_UNUSED)
1675 {
1676 struct Exception_Data *exception;
1677 const char *msg;
1678
1679 switch (sig)
1680 {
1681 case SIGFPE:
1682 exception = &constraint_error;
1683 msg = "SIGFPE";
1684 break;
1685 #ifdef VTHREADS
1686 #ifdef __VXWORKSMILS__
1687 case SIGILL:
1688 exception = &storage_error;
1689 msg = "SIGILL: possible stack overflow";
1690 break;
1691 case SIGSEGV:
1692 exception = &storage_error;
1693 msg = "SIGSEGV";
1694 break;
1695 case SIGBUS:
1696 exception = &program_error;
1697 msg = "SIGBUS";
1698 break;
1699 #else
1700 case SIGILL:
1701 exception = &constraint_error;
1702 msg = "Floating point exception or SIGILL";
1703 break;
1704 case SIGSEGV:
1705 exception = &storage_error;
1706 msg = "SIGSEGV";
1707 break;
1708 case SIGBUS:
1709 exception = &storage_error;
1710 msg = "SIGBUS: possible stack overflow";
1711 break;
1712 #endif
1713 #elif (_WRS_VXWORKS_MAJOR == 6)
1714 case SIGILL:
1715 exception = &constraint_error;
1716 msg = "SIGILL";
1717 break;
1718 #ifdef __RTP__
1719 /* In RTP mode a SIGSEGV is most likely due to a stack overflow,
1720 since stack checking uses the probing mechanism. */
1721 case SIGSEGV:
1722 exception = &storage_error;
1723 msg = "SIGSEGV: possible stack overflow";
1724 break;
1725 case SIGBUS:
1726 exception = &program_error;
1727 msg = "SIGBUS";
1728 break;
1729 #else
1730 /* VxWorks 6 kernel mode with probing. SIGBUS for guard page hit */
1731 case SIGSEGV:
1732 exception = &storage_error;
1733 msg = "SIGSEGV";
1734 break;
1735 case SIGBUS:
1736 exception = &storage_error;
1737 msg = "SIGBUS: possible stack overflow";
1738 break;
1739 #endif
1740 #else
1741 /* VxWorks 5: a SIGILL is most likely due to a stack overflow,
1742 since stack checking uses the stack limit mechanism. */
1743 case SIGILL:
1744 exception = &storage_error;
1745 msg = "SIGILL: possible stack overflow";
1746 break;
1747 case SIGSEGV:
1748 exception = &storage_error;
1749 msg = "SIGSEGV";
1750 break;
1751 case SIGBUS:
1752 exception = &program_error;
1753 msg = "SIGBUS";
1754 break;
1755 #endif
1756 default:
1757 exception = &program_error;
1758 msg = "unhandled signal";
1759 }
1760
1761 __gnat_clear_exception_count ();
1762 Raise_From_Signal_Handler (exception, msg);
1763 }
1764
1765 /* Tasking and Non-tasking signal handler. Map SIGnal to Ada exception
1766 propagation after the required low level adjustments. */
1767
1768 void
1769 __gnat_error_handler (int sig, void *si, struct sigcontext *sc)
1770 {
1771 sigset_t mask;
1772
1773 /* VxWorks will always mask out the signal during the signal handler and
1774 will reenable it on a longjmp. GNAT does not generate a longjmp to
1775 return from a signal handler so the signal will still be masked unless
1776 we unmask it. */
1777 sigprocmask (SIG_SETMASK, NULL, &mask);
1778 sigdelset (&mask, sig);
1779 sigprocmask (SIG_SETMASK, &mask, NULL);
1780
1781 #if defined (__PPC__) && defined(_WRS_KERNEL)
1782 /* On PowerPC, kernel mode, we process signals through a Call Frame Info
1783 trampoline, voiding the need for myriads of fallback_frame_state
1784 variants in the ZCX runtime. We have no simple way to distinguish ZCX
1785 from SJLJ here, so we do this for SJLJ as well even though this is not
1786 necessary. This only incurs a few extra instructions and a tiny
1787 amount of extra stack usage. */
1788
1789 #include "sigtramp.h"
1790
1791 __gnat_sigtramp (sig, (void *)si, (void *)sc,
1792 (sighandler_t *)&__gnat_map_signal);
1793
1794 #else
1795 __gnat_map_signal (sig, si, sc);
1796 #endif
1797 }
1798
1799 #if defined(__leon__) && defined(_WRS_KERNEL)
1800 /* For LEON VxWorks we need to install a trap handler for stack overflow */
1801
1802 extern void excEnt (void);
1803 /* VxWorks exception handler entry */
1804
1805 struct trap_entry {
1806 unsigned long inst_first;
1807 unsigned long inst_second;
1808 unsigned long inst_third;
1809 unsigned long inst_fourth;
1810 };
1811 /* Four instructions representing entries in the trap table */
1812
1813 struct trap_entry *trap_0_entry;
1814 /* We will set the location of the entry for software trap 0 in the trap
1815 table. */
1816 #endif
1817
1818 void
1819 __gnat_install_handler (void)
1820 {
1821 struct sigaction act;
1822
1823 /* Setup signal handler to map synchronous signals to appropriate
1824 exceptions. Make sure that the handler isn't interrupted by another
1825 signal that might cause a scheduling event! */
1826
1827 act.sa_handler = __gnat_error_handler;
1828 act.sa_flags = SA_SIGINFO | SA_ONSTACK;
1829 sigemptyset (&act.sa_mask);
1830
1831 /* For VxWorks, install all signal handlers, since pragma Interrupt_State
1832 applies to vectored hardware interrupts, not signals. */
1833 sigaction (SIGFPE, &act, NULL);
1834 sigaction (SIGILL, &act, NULL);
1835 sigaction (SIGSEGV, &act, NULL);
1836 sigaction (SIGBUS, &act, NULL);
1837
1838 #if defined(__leon__) && defined(_WRS_KERNEL)
1839 /* Specific to the LEON VxWorks kernel run-time library */
1840
1841 /* For stack checking the compiler triggers a software trap 0 (ta 0) in
1842 case of overflow (we use the stack limit mechanism). We need to install
1843 the trap handler here for this software trap (the OS does not handle
1844 it) as if it were a data_access_exception (trap 9). We do the same as
1845 if we put in the trap table a VXSPARC_BAD_TRAP(9). Software trap 0 is
1846 located at vector 0x80, and each entry takes 4 words. */
1847
1848 trap_0_entry = (struct trap_entry *)(intVecBaseGet () + 0x80 * 4);
1849
1850 /* mov 0x9, %l7 */
1851
1852 trap_0_entry->inst_first = 0xae102000 + 9;
1853
1854 /* sethi %hi(excEnt), %l6 */
1855
1856 /* The 22 most significant bits of excEnt are obtained shifting 10 times
1857 to the right. */
1858
1859 trap_0_entry->inst_second = 0x2d000000 + ((unsigned long)excEnt >> 10);
1860
1861 /* jmp %l6+%lo(excEnt) */
1862
1863 /* The 10 least significant bits of excEnt are obtained by masking */
1864
1865 trap_0_entry->inst_third = 0x81c5a000 + ((unsigned long)excEnt & 0x3ff);
1866
1867 /* rd %psr, %l0 */
1868
1869 trap_0_entry->inst_fourth = 0xa1480000;
1870 #endif
1871
1872 __gnat_handler_installed = 1;
1873 }
1874
1875 #define HAVE_GNAT_INIT_FLOAT
1876
1877 void
1878 __gnat_init_float (void)
1879 {
1880 /* Disable overflow/underflow exceptions on the PPC processor, needed
1881 to get correct Ada semantics. Note that for AE653 vThreads, the HW
1882 overflow settings are an OS configuration issue. The instructions
1883 below have no effect. */
1884 #if defined (_ARCH_PPC) && !defined (_SOFT_FLOAT) && (!defined (VTHREADS) || defined (__VXWORKSMILS__))
1885 #if defined (__SPE__)
1886 {
1887 const unsigned long spefscr_mask = 0xfffffff3;
1888 unsigned long spefscr;
1889 asm ("mfspr %0, 512" : "=r" (spefscr));
1890 spefscr = spefscr & spefscr_mask;
1891 asm ("mtspr 512, %0\n\tisync" : : "r" (spefscr));
1892 }
1893 #else
1894 asm ("mtfsb0 25");
1895 asm ("mtfsb0 26");
1896 #endif
1897 #endif
1898
1899 #if (defined (__i386__) || defined (i386)) && !defined (VTHREADS)
1900 /* This is used to properly initialize the FPU on an x86 for each
1901 process thread. */
1902 asm ("finit");
1903 #endif
1904
1905 /* Similarly for SPARC64. Achieved by masking bits in the Trap Enable Mask
1906 field of the Floating-point Status Register (see the SPARC Architecture
1907 Manual Version 9, p 48). */
1908 #if defined (sparc64)
1909
1910 #define FSR_TEM_NVM (1 << 27) /* Invalid operand */
1911 #define FSR_TEM_OFM (1 << 26) /* Overflow */
1912 #define FSR_TEM_UFM (1 << 25) /* Underflow */
1913 #define FSR_TEM_DZM (1 << 24) /* Division by Zero */
1914 #define FSR_TEM_NXM (1 << 23) /* Inexact result */
1915 {
1916 unsigned int fsr;
1917
1918 __asm__("st %%fsr, %0" : "=m" (fsr));
1919 fsr &= ~(FSR_TEM_OFM | FSR_TEM_UFM);
1920 __asm__("ld %0, %%fsr" : : "m" (fsr));
1921 }
1922 #endif
1923 }
1924
1925 /* This subprogram is called by System.Task_Primitives.Operations.Enter_Task
1926 (if not null) when a new task is created. It is initialized by
1927 System.Stack_Checking.Operations.Initialize_Stack_Limit.
1928 The use of a hook avoids to drag stack checking subprograms if stack
1929 checking is not used. */
1930 void (*__gnat_set_stack_limit_hook)(void) = (void (*)(void))0;
1931
1932 /******************/
1933 /* NetBSD Section */
1934 /******************/
1935
1936 #elif defined(__NetBSD__)
1937
1938 #include <signal.h>
1939 #include <unistd.h>
1940
1941 static void
1942 __gnat_error_handler (int sig)
1943 {
1944 struct Exception_Data *exception;
1945 const char *msg;
1946
1947 switch(sig)
1948 {
1949 case SIGFPE:
1950 exception = &constraint_error;
1951 msg = "SIGFPE";
1952 break;
1953 case SIGILL:
1954 exception = &constraint_error;
1955 msg = "SIGILL";
1956 break;
1957 case SIGSEGV:
1958 exception = &storage_error;
1959 msg = "stack overflow or erroneous memory access";
1960 break;
1961 case SIGBUS:
1962 exception = &constraint_error;
1963 msg = "SIGBUS";
1964 break;
1965 default:
1966 exception = &program_error;
1967 msg = "unhandled signal";
1968 }
1969
1970 Raise_From_Signal_Handler(exception, msg);
1971 }
1972
1973 void
1974 __gnat_install_handler(void)
1975 {
1976 struct sigaction act;
1977
1978 act.sa_handler = __gnat_error_handler;
1979 act.sa_flags = SA_NODEFER | SA_RESTART;
1980 sigemptyset (&act.sa_mask);
1981
1982 /* Do not install handlers if interrupt state is "System". */
1983 if (__gnat_get_interrupt_state (SIGFPE) != 's')
1984 sigaction (SIGFPE, &act, NULL);
1985 if (__gnat_get_interrupt_state (SIGILL) != 's')
1986 sigaction (SIGILL, &act, NULL);
1987 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
1988 sigaction (SIGSEGV, &act, NULL);
1989 if (__gnat_get_interrupt_state (SIGBUS) != 's')
1990 sigaction (SIGBUS, &act, NULL);
1991
1992 __gnat_handler_installed = 1;
1993 }
1994
1995 /*******************/
1996 /* OpenBSD Section */
1997 /*******************/
1998
1999 #elif defined(__OpenBSD__)
2000
2001 #include <signal.h>
2002 #include <unistd.h>
2003
2004 static void
2005 __gnat_error_handler (int sig)
2006 {
2007 struct Exception_Data *exception;
2008 const char *msg;
2009
2010 switch(sig)
2011 {
2012 case SIGFPE:
2013 exception = &constraint_error;
2014 msg = "SIGFPE";
2015 break;
2016 case SIGILL:
2017 exception = &constraint_error;
2018 msg = "SIGILL";
2019 break;
2020 case SIGSEGV:
2021 exception = &storage_error;
2022 msg = "stack overflow or erroneous memory access";
2023 break;
2024 case SIGBUS:
2025 exception = &constraint_error;
2026 msg = "SIGBUS";
2027 break;
2028 default:
2029 exception = &program_error;
2030 msg = "unhandled signal";
2031 }
2032
2033 Raise_From_Signal_Handler(exception, msg);
2034 }
2035
2036 void
2037 __gnat_install_handler(void)
2038 {
2039 struct sigaction act;
2040
2041 act.sa_handler = __gnat_error_handler;
2042 act.sa_flags = SA_NODEFER | SA_RESTART;
2043 sigemptyset (&act.sa_mask);
2044
2045 /* Do not install handlers if interrupt state is "System" */
2046 if (__gnat_get_interrupt_state (SIGFPE) != 's')
2047 sigaction (SIGFPE, &act, NULL);
2048 if (__gnat_get_interrupt_state (SIGILL) != 's')
2049 sigaction (SIGILL, &act, NULL);
2050 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
2051 sigaction (SIGSEGV, &act, NULL);
2052 if (__gnat_get_interrupt_state (SIGBUS) != 's')
2053 sigaction (SIGBUS, &act, NULL);
2054
2055 __gnat_handler_installed = 1;
2056 }
2057
2058 /******************/
2059 /* Darwin Section */
2060 /******************/
2061
2062 #elif defined(__APPLE__)
2063
2064 #include <signal.h>
2065 #include <stdlib.h>
2066 #include <sys/syscall.h>
2067 #include <sys/sysctl.h>
2068 #include <mach/mach_vm.h>
2069 #include <mach/mach_init.h>
2070 #include <mach/vm_statistics.h>
2071
2072 /* This must be in keeping with System.OS_Interface.Alternate_Stack_Size. */
2073 char __gnat_alternate_stack[32 * 1024]; /* 1 * MINSIGSTKSZ */
2074
2075 /* Defined in xnu unix_signal.c.
2076 Tell the kernel to re-use alt stack when delivering a signal. */
2077 #define UC_RESET_ALT_STACK 0x80000000
2078
2079 /* Return true if ADDR is within a stack guard area. */
2080 static int
2081 __gnat_is_stack_guard (mach_vm_address_t addr)
2082 {
2083 kern_return_t kret;
2084 vm_region_submap_info_data_64_t info;
2085 mach_vm_address_t start;
2086 mach_vm_size_t size;
2087 natural_t depth;
2088 mach_msg_type_number_t count;
2089
2090 count = VM_REGION_SUBMAP_INFO_COUNT_64;
2091 start = addr;
2092 size = -1;
2093 depth = 9999;
2094 kret = mach_vm_region_recurse (mach_task_self (), &start, &size, &depth,
2095 (vm_region_recurse_info_t) &info, &count);
2096 if (kret == KERN_SUCCESS
2097 && addr >= start && addr < (start + size)
2098 && info.protection == VM_PROT_NONE
2099 && info.user_tag == VM_MEMORY_STACK)
2100 return 1;
2101 return 0;
2102 }
2103
2104 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
2105
2106 #if defined (__x86_64__)
2107 static int
2108 __darwin_major_version (void)
2109 {
2110 static int cache = -1;
2111 if (cache < 0)
2112 {
2113 int mib[2] = {CTL_KERN, KERN_OSRELEASE};
2114 size_t len;
2115
2116 /* Find out how big the buffer needs to be (and set cache to 0
2117 on failure). */
2118 if (sysctl (mib, 2, NULL, &len, NULL, 0) == 0)
2119 {
2120 char release[len];
2121 sysctl (mib, 2, release, &len, NULL, 0);
2122 /* Darwin releases are of the form L.M.N where L is the major
2123 version, so strtol will return L. */
2124 cache = (int) strtol (release, NULL, 10);
2125 }
2126 else
2127 {
2128 cache = 0;
2129 }
2130 }
2131 return cache;
2132 }
2133 #endif
2134
2135 void
2136 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED,
2137 void *ucontext ATTRIBUTE_UNUSED)
2138 {
2139 #if defined (__x86_64__)
2140 if (__darwin_major_version () < 12)
2141 {
2142 /* Work around radar #10302855, where the unwinders (libunwind or
2143 libgcc_s depending on the system revision) and the DWARF unwind
2144 data for sigtramp have different ideas about register numbering,
2145 causing rbx and rdx to be transposed. */
2146 ucontext_t *uc = (ucontext_t *)ucontext;
2147 unsigned long t = uc->uc_mcontext->__ss.__rbx;
2148
2149 uc->uc_mcontext->__ss.__rbx = uc->uc_mcontext->__ss.__rdx;
2150 uc->uc_mcontext->__ss.__rdx = t;
2151 }
2152 #endif
2153 }
2154
2155 static void
2156 __gnat_error_handler (int sig, siginfo_t *si, void *ucontext)
2157 {
2158 struct Exception_Data *exception;
2159 const char *msg;
2160
2161 __gnat_adjust_context_for_raise (sig, ucontext);
2162
2163 switch (sig)
2164 {
2165 case SIGSEGV:
2166 case SIGBUS:
2167 if (__gnat_is_stack_guard ((unsigned long)si->si_addr))
2168 {
2169 exception = &storage_error;
2170 msg = "stack overflow";
2171 }
2172 else
2173 {
2174 exception = &constraint_error;
2175 msg = "erroneous memory access";
2176 }
2177 /* Reset the use of alt stack, so that the alt stack will be used
2178 for the next signal delivery.
2179 The stack can't be used in case of stack checking. */
2180 syscall (SYS_sigreturn, NULL, UC_RESET_ALT_STACK);
2181 break;
2182
2183 case SIGFPE:
2184 exception = &constraint_error;
2185 msg = "SIGFPE";
2186 break;
2187
2188 default:
2189 exception = &program_error;
2190 msg = "unhandled signal";
2191 }
2192
2193 Raise_From_Signal_Handler (exception, msg);
2194 }
2195
2196 void
2197 __gnat_install_handler (void)
2198 {
2199 struct sigaction act;
2200
2201 /* Set up signal handler to map synchronous signals to appropriate
2202 exceptions. Make sure that the handler isn't interrupted by another
2203 signal that might cause a scheduling event! Also setup an alternate
2204 stack region for the handler execution so that stack overflows can be
2205 handled properly, avoiding a SEGV generation from stack usage by the
2206 handler itself (and it is required by Darwin). */
2207
2208 stack_t stack;
2209 stack.ss_sp = __gnat_alternate_stack;
2210 stack.ss_size = sizeof (__gnat_alternate_stack);
2211 stack.ss_flags = 0;
2212 sigaltstack (&stack, NULL);
2213
2214 act.sa_flags = SA_NODEFER | SA_RESTART | SA_SIGINFO;
2215 act.sa_sigaction = __gnat_error_handler;
2216 sigemptyset (&act.sa_mask);
2217
2218 /* Do not install handlers if interrupt state is "System". */
2219 if (__gnat_get_interrupt_state (SIGABRT) != 's')
2220 sigaction (SIGABRT, &act, NULL);
2221 if (__gnat_get_interrupt_state (SIGFPE) != 's')
2222 sigaction (SIGFPE, &act, NULL);
2223 if (__gnat_get_interrupt_state (SIGILL) != 's')
2224 sigaction (SIGILL, &act, NULL);
2225
2226 act.sa_flags |= SA_ONSTACK;
2227 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
2228 sigaction (SIGSEGV, &act, NULL);
2229 if (__gnat_get_interrupt_state (SIGBUS) != 's')
2230 sigaction (SIGBUS, &act, NULL);
2231
2232 __gnat_handler_installed = 1;
2233 }
2234
2235 #else
2236
2237 /* For all other versions of GNAT, the handler does nothing. */
2238
2239 /*******************/
2240 /* Default Section */
2241 /*******************/
2242
2243 void
2244 __gnat_install_handler (void)
2245 {
2246 __gnat_handler_installed = 1;
2247 }
2248
2249 #endif
2250
2251 /*********************/
2252 /* __gnat_init_float */
2253 /*********************/
2254
2255 /* This routine is called as each process thread is created, for possible
2256 initialization of the FP processor. This version is used under INTERIX
2257 and WIN32. */
2258
2259 #if defined (_WIN32) || defined (__INTERIX) \
2260 || defined (__Lynx__) || defined(__NetBSD__) || defined(__FreeBSD__) \
2261 || defined (__OpenBSD__)
2262
2263 #define HAVE_GNAT_INIT_FLOAT
2264
2265 void
2266 __gnat_init_float (void)
2267 {
2268 #if defined (__i386__) || defined (i386) || defined (__x86_64)
2269
2270 /* This is used to properly initialize the FPU on an x86 for each
2271 process thread. */
2272
2273 asm ("finit");
2274
2275 #endif /* Defined __i386__ */
2276 }
2277 #endif
2278
2279 #ifndef HAVE_GNAT_INIT_FLOAT
2280
2281 /* All targets without a specific __gnat_init_float will use an empty one. */
2282 void
2283 __gnat_init_float (void)
2284 {
2285 }
2286 #endif
2287
2288 /***********************************/
2289 /* __gnat_adjust_context_for_raise */
2290 /***********************************/
2291
2292 #ifndef HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
2293
2294 /* All targets without a specific version will use an empty one. */
2295
2296 /* Given UCONTEXT a pointer to a context structure received by a signal
2297 handler for SIGNO, perform the necessary adjustments to let the handler
2298 raise an exception. Calls to this routine are not conditioned by the
2299 propagation scheme in use. */
2300
2301 void
2302 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED,
2303 void *ucontext ATTRIBUTE_UNUSED)
2304 {
2305 /* We used to compensate here for the raised from call vs raised from signal
2306 exception discrepancy with the GCC ZCX scheme, but this now can be dealt
2307 with generically in the unwinder (see GCC PR other/26208). This however
2308 requires the use of the _Unwind_GetIPInfo routine in raise-gcc.c, which
2309 is predicated on the definition of HAVE_GETIPINFO at compile time. Only
2310 the VMS ports still do the compensation described in the few lines below.
2311
2312 *** Call vs signal exception discrepancy with GCC ZCX scheme ***
2313
2314 The GCC unwinder expects to be dealing with call return addresses, since
2315 this is the "nominal" case of what we retrieve while unwinding a regular
2316 call chain.
2317
2318 To evaluate if a handler applies at some point identified by a return
2319 address, the propagation engine needs to determine what region the
2320 corresponding call instruction pertains to. Because the return address
2321 may not be attached to the same region as the call, the unwinder always
2322 subtracts "some" amount from a return address to search the region
2323 tables, amount chosen to ensure that the resulting address is inside the
2324 call instruction.
2325
2326 When we raise an exception from a signal handler, e.g. to transform a
2327 SIGSEGV into Storage_Error, things need to appear as if the signal
2328 handler had been "called" by the instruction which triggered the signal,
2329 so that exception handlers that apply there are considered. What the
2330 unwinder will retrieve as the return address from the signal handler is
2331 what it will find as the faulting instruction address in the signal
2332 context pushed by the kernel. Leaving this address untouched looses, if
2333 the triggering instruction happens to be the very first of a region, as
2334 the later adjustments performed by the unwinder would yield an address
2335 outside that region. We need to compensate for the unwinder adjustments
2336 at some point, and this is what this routine is expected to do.
2337
2338 signo is passed because on some targets for some signals the PC in
2339 context points to the instruction after the faulting one, in which case
2340 the unwinder adjustment is still desired. */
2341 }
2342
2343 #endif
2344
2345 #ifdef __cplusplus
2346 }
2347 #endif