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