]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/init.c
2013-10-13 Thomas Quinot <quinot@adacore.com>
[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 void __gnat_error_handler (int, void *, struct sigcontext *);
1669
1670 #ifndef __RTP__
1671
1672 /* Directly vectored Interrupt routines are not supported when using RTPs. */
1673
1674 extern int __gnat_inum_to_ivec (int);
1675
1676 /* This is needed by the GNAT run time to handle Vxworks interrupts. */
1677 int
1678 __gnat_inum_to_ivec (int num)
1679 {
1680 return INUM_TO_IVEC (num);
1681 }
1682 #endif
1683
1684 #if !defined(__alpha_vxworks) && (_WRS_VXWORKS_MAJOR != 6) && !defined(__RTP__)
1685
1686 /* getpid is used by s-parint.adb, but is not defined by VxWorks, except
1687 on Alpha VxWorks and VxWorks 6.x (including RTPs). */
1688
1689 extern long getpid (void);
1690
1691 long
1692 getpid (void)
1693 {
1694 return taskIdSelf ();
1695 }
1696 #endif
1697
1698 /* VxWorks 653 vThreads expects the field excCnt to be zeroed when a signal is.
1699 handled. The VxWorks version of longjmp does this; GCC's builtin_longjmp
1700 doesn't. */
1701 void
1702 __gnat_clear_exception_count (void)
1703 {
1704 #ifdef VTHREADS
1705 WIND_TCB *currentTask = (WIND_TCB *) taskIdSelf();
1706
1707 currentTask->vThreads.excCnt = 0;
1708 #endif
1709 }
1710
1711 /* Handle different SIGnal to exception mappings in different VxWorks
1712 versions. */
1713 static void
1714 __gnat_map_signal (int sig, void *si ATTRIBUTE_UNUSED,
1715 struct sigcontext *sc ATTRIBUTE_UNUSED)
1716 {
1717 struct Exception_Data *exception;
1718 const char *msg;
1719
1720 switch (sig)
1721 {
1722 case SIGFPE:
1723 exception = &constraint_error;
1724 msg = "SIGFPE";
1725 break;
1726 #ifdef VTHREADS
1727 #ifdef __VXWORKSMILS__
1728 case SIGILL:
1729 exception = &storage_error;
1730 msg = "SIGILL: possible stack overflow";
1731 break;
1732 case SIGSEGV:
1733 exception = &storage_error;
1734 msg = "SIGSEGV";
1735 break;
1736 case SIGBUS:
1737 exception = &program_error;
1738 msg = "SIGBUS";
1739 break;
1740 #else
1741 case SIGILL:
1742 exception = &constraint_error;
1743 msg = "Floating point exception or SIGILL";
1744 break;
1745 case SIGSEGV:
1746 exception = &storage_error;
1747 msg = "SIGSEGV";
1748 break;
1749 case SIGBUS:
1750 exception = &storage_error;
1751 msg = "SIGBUS: possible stack overflow";
1752 break;
1753 #endif
1754 #elif (_WRS_VXWORKS_MAJOR == 6)
1755 case SIGILL:
1756 exception = &constraint_error;
1757 msg = "SIGILL";
1758 break;
1759 #ifdef __RTP__
1760 /* In RTP mode a SIGSEGV is most likely due to a stack overflow,
1761 since stack checking uses the probing mechanism. */
1762 case SIGSEGV:
1763 exception = &storage_error;
1764 msg = "SIGSEGV: possible stack overflow";
1765 break;
1766 case SIGBUS:
1767 exception = &program_error;
1768 msg = "SIGBUS";
1769 break;
1770 #else
1771 /* VxWorks 6 kernel mode with probing. SIGBUS for guard page hit */
1772 case SIGSEGV:
1773 exception = &storage_error;
1774 msg = "SIGSEGV";
1775 break;
1776 case SIGBUS:
1777 exception = &storage_error;
1778 msg = "SIGBUS: possible stack overflow";
1779 break;
1780 #endif
1781 #else
1782 /* VxWorks 5: a SIGILL is most likely due to a stack overflow,
1783 since stack checking uses the stack limit mechanism. */
1784 case SIGILL:
1785 exception = &storage_error;
1786 msg = "SIGILL: possible stack overflow";
1787 break;
1788 case SIGSEGV:
1789 exception = &storage_error;
1790 msg = "SIGSEGV";
1791 break;
1792 case SIGBUS:
1793 exception = &program_error;
1794 msg = "SIGBUS";
1795 break;
1796 #endif
1797 default:
1798 exception = &program_error;
1799 msg = "unhandled signal";
1800 }
1801
1802 __gnat_clear_exception_count ();
1803 Raise_From_Signal_Handler (exception, msg);
1804 }
1805
1806 /* Tasking and Non-tasking signal handler. Map SIGnal to Ada exception
1807 propagation after the required low level adjustments. */
1808
1809 void
1810 __gnat_error_handler (int sig, void *si, struct sigcontext *sc)
1811 {
1812 sigset_t mask;
1813
1814 /* VxWorks will always mask out the signal during the signal handler and
1815 will reenable it on a longjmp. GNAT does not generate a longjmp to
1816 return from a signal handler so the signal will still be masked unless
1817 we unmask it. */
1818 sigprocmask (SIG_SETMASK, NULL, &mask);
1819 sigdelset (&mask, sig);
1820 sigprocmask (SIG_SETMASK, &mask, NULL);
1821
1822 #if defined (__PPC__) && defined(_WRS_KERNEL)
1823 /* On PowerPC, kernel mode, we process signals through a Call Frame Info
1824 trampoline, voiding the need for myriads of fallback_frame_state
1825 variants in the ZCX runtime. We have no simple way to distinguish ZCX
1826 from SJLJ here, so we do this for SJLJ as well even though this is not
1827 necessary. This only incurs a few extra instructions and a tiny
1828 amount of extra stack usage. */
1829
1830 #include "sigtramp.h"
1831
1832 __gnat_sigtramp (sig, (void *)si, (void *)sc,
1833 (sighandler_t *)&__gnat_map_signal);
1834
1835 #else
1836 __gnat_map_signal (sig, si, sc);
1837 #endif
1838 }
1839
1840 #if defined(__leon__) && defined(_WRS_KERNEL)
1841 /* For LEON VxWorks we need to install a trap handler for stack overflow */
1842
1843 extern void excEnt (void);
1844 /* VxWorks exception handler entry */
1845
1846 struct trap_entry {
1847 unsigned long inst_first;
1848 unsigned long inst_second;
1849 unsigned long inst_third;
1850 unsigned long inst_fourth;
1851 };
1852 /* Four instructions representing entries in the trap table */
1853
1854 struct trap_entry *trap_0_entry;
1855 /* We will set the location of the entry for software trap 0 in the trap
1856 table. */
1857 #endif
1858
1859 void
1860 __gnat_install_handler (void)
1861 {
1862 struct sigaction act;
1863
1864 /* Setup signal handler to map synchronous signals to appropriate
1865 exceptions. Make sure that the handler isn't interrupted by another
1866 signal that might cause a scheduling event! */
1867
1868 act.sa_handler = __gnat_error_handler;
1869 act.sa_flags = SA_SIGINFO | SA_ONSTACK;
1870 sigemptyset (&act.sa_mask);
1871
1872 /* For VxWorks, install all signal handlers, since pragma Interrupt_State
1873 applies to vectored hardware interrupts, not signals. */
1874 sigaction (SIGFPE, &act, NULL);
1875 sigaction (SIGILL, &act, NULL);
1876 sigaction (SIGSEGV, &act, NULL);
1877 sigaction (SIGBUS, &act, NULL);
1878
1879 #if defined(__leon__) && defined(_WRS_KERNEL)
1880 /* Specific to the LEON VxWorks kernel run-time library */
1881
1882 /* For stack checking the compiler triggers a software trap 0 (ta 0) in
1883 case of overflow (we use the stack limit mechanism). We need to install
1884 the trap handler here for this software trap (the OS does not handle
1885 it) as if it were a data_access_exception (trap 9). We do the same as
1886 if we put in the trap table a VXSPARC_BAD_TRAP(9). Software trap 0 is
1887 located at vector 0x80, and each entry takes 4 words. */
1888
1889 trap_0_entry = (struct trap_entry *)(intVecBaseGet () + 0x80 * 4);
1890
1891 /* mov 0x9, %l7 */
1892
1893 trap_0_entry->inst_first = 0xae102000 + 9;
1894
1895 /* sethi %hi(excEnt), %l6 */
1896
1897 /* The 22 most significant bits of excEnt are obtained shifting 10 times
1898 to the right. */
1899
1900 trap_0_entry->inst_second = 0x2d000000 + ((unsigned long)excEnt >> 10);
1901
1902 /* jmp %l6+%lo(excEnt) */
1903
1904 /* The 10 least significant bits of excEnt are obtained by masking */
1905
1906 trap_0_entry->inst_third = 0x81c5a000 + ((unsigned long)excEnt & 0x3ff);
1907
1908 /* rd %psr, %l0 */
1909
1910 trap_0_entry->inst_fourth = 0xa1480000;
1911 #endif
1912
1913 __gnat_handler_installed = 1;
1914 }
1915
1916 #define HAVE_GNAT_INIT_FLOAT
1917
1918 void
1919 __gnat_init_float (void)
1920 {
1921 /* Disable overflow/underflow exceptions on the PPC processor, needed
1922 to get correct Ada semantics. Note that for AE653 vThreads, the HW
1923 overflow settings are an OS configuration issue. The instructions
1924 below have no effect. */
1925 #if defined (_ARCH_PPC) && !defined (_SOFT_FLOAT) && (!defined (VTHREADS) || defined (__VXWORKSMILS__))
1926 #if defined (__SPE__)
1927 {
1928 const unsigned long spefscr_mask = 0xfffffff3;
1929 unsigned long spefscr;
1930 asm ("mfspr %0, 512" : "=r" (spefscr));
1931 spefscr = spefscr & spefscr_mask;
1932 asm ("mtspr 512, %0\n\tisync" : : "r" (spefscr));
1933 }
1934 #else
1935 asm ("mtfsb0 25");
1936 asm ("mtfsb0 26");
1937 #endif
1938 #endif
1939
1940 #if (defined (__i386__) || defined (i386)) && !defined (VTHREADS)
1941 /* This is used to properly initialize the FPU on an x86 for each
1942 process thread. */
1943 asm ("finit");
1944 #endif
1945
1946 /* Similarly for SPARC64. Achieved by masking bits in the Trap Enable Mask
1947 field of the Floating-point Status Register (see the SPARC Architecture
1948 Manual Version 9, p 48). */
1949 #if defined (sparc64)
1950
1951 #define FSR_TEM_NVM (1 << 27) /* Invalid operand */
1952 #define FSR_TEM_OFM (1 << 26) /* Overflow */
1953 #define FSR_TEM_UFM (1 << 25) /* Underflow */
1954 #define FSR_TEM_DZM (1 << 24) /* Division by Zero */
1955 #define FSR_TEM_NXM (1 << 23) /* Inexact result */
1956 {
1957 unsigned int fsr;
1958
1959 __asm__("st %%fsr, %0" : "=m" (fsr));
1960 fsr &= ~(FSR_TEM_OFM | FSR_TEM_UFM);
1961 __asm__("ld %0, %%fsr" : : "m" (fsr));
1962 }
1963 #endif
1964 }
1965
1966 /* This subprogram is called by System.Task_Primitives.Operations.Enter_Task
1967 (if not null) when a new task is created. It is initialized by
1968 System.Stack_Checking.Operations.Initialize_Stack_Limit.
1969 The use of a hook avoids to drag stack checking subprograms if stack
1970 checking is not used. */
1971 void (*__gnat_set_stack_limit_hook)(void) = (void (*)(void))0;
1972
1973 /******************/
1974 /* NetBSD Section */
1975 /******************/
1976
1977 #elif defined(__NetBSD__)
1978
1979 #include <signal.h>
1980 #include <unistd.h>
1981
1982 static void
1983 __gnat_error_handler (int sig)
1984 {
1985 struct Exception_Data *exception;
1986 const char *msg;
1987
1988 switch(sig)
1989 {
1990 case SIGFPE:
1991 exception = &constraint_error;
1992 msg = "SIGFPE";
1993 break;
1994 case SIGILL:
1995 exception = &constraint_error;
1996 msg = "SIGILL";
1997 break;
1998 case SIGSEGV:
1999 exception = &storage_error;
2000 msg = "stack overflow or erroneous memory access";
2001 break;
2002 case SIGBUS:
2003 exception = &constraint_error;
2004 msg = "SIGBUS";
2005 break;
2006 default:
2007 exception = &program_error;
2008 msg = "unhandled signal";
2009 }
2010
2011 Raise_From_Signal_Handler(exception, msg);
2012 }
2013
2014 void
2015 __gnat_install_handler(void)
2016 {
2017 struct sigaction act;
2018
2019 act.sa_handler = __gnat_error_handler;
2020 act.sa_flags = SA_NODEFER | SA_RESTART;
2021 sigemptyset (&act.sa_mask);
2022
2023 /* Do not install handlers if interrupt state is "System". */
2024 if (__gnat_get_interrupt_state (SIGFPE) != 's')
2025 sigaction (SIGFPE, &act, NULL);
2026 if (__gnat_get_interrupt_state (SIGILL) != 's')
2027 sigaction (SIGILL, &act, NULL);
2028 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
2029 sigaction (SIGSEGV, &act, NULL);
2030 if (__gnat_get_interrupt_state (SIGBUS) != 's')
2031 sigaction (SIGBUS, &act, NULL);
2032
2033 __gnat_handler_installed = 1;
2034 }
2035
2036 /*******************/
2037 /* OpenBSD Section */
2038 /*******************/
2039
2040 #elif defined(__OpenBSD__)
2041
2042 #include <signal.h>
2043 #include <unistd.h>
2044
2045 static void
2046 __gnat_error_handler (int sig)
2047 {
2048 struct Exception_Data *exception;
2049 const char *msg;
2050
2051 switch(sig)
2052 {
2053 case SIGFPE:
2054 exception = &constraint_error;
2055 msg = "SIGFPE";
2056 break;
2057 case SIGILL:
2058 exception = &constraint_error;
2059 msg = "SIGILL";
2060 break;
2061 case SIGSEGV:
2062 exception = &storage_error;
2063 msg = "stack overflow or erroneous memory access";
2064 break;
2065 case SIGBUS:
2066 exception = &constraint_error;
2067 msg = "SIGBUS";
2068 break;
2069 default:
2070 exception = &program_error;
2071 msg = "unhandled signal";
2072 }
2073
2074 Raise_From_Signal_Handler(exception, msg);
2075 }
2076
2077 void
2078 __gnat_install_handler(void)
2079 {
2080 struct sigaction act;
2081
2082 act.sa_handler = __gnat_error_handler;
2083 act.sa_flags = SA_NODEFER | SA_RESTART;
2084 sigemptyset (&act.sa_mask);
2085
2086 /* Do not install handlers if interrupt state is "System" */
2087 if (__gnat_get_interrupt_state (SIGFPE) != 's')
2088 sigaction (SIGFPE, &act, NULL);
2089 if (__gnat_get_interrupt_state (SIGILL) != 's')
2090 sigaction (SIGILL, &act, NULL);
2091 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
2092 sigaction (SIGSEGV, &act, NULL);
2093 if (__gnat_get_interrupt_state (SIGBUS) != 's')
2094 sigaction (SIGBUS, &act, NULL);
2095
2096 __gnat_handler_installed = 1;
2097 }
2098
2099 /******************/
2100 /* Darwin Section */
2101 /******************/
2102
2103 #elif defined(__APPLE__)
2104
2105 #include <signal.h>
2106 #include <stdlib.h>
2107 #include <sys/syscall.h>
2108 #include <sys/sysctl.h>
2109 #include <mach/mach_vm.h>
2110 #include <mach/mach_init.h>
2111 #include <mach/vm_statistics.h>
2112
2113 /* This must be in keeping with System.OS_Interface.Alternate_Stack_Size. */
2114 char __gnat_alternate_stack[32 * 1024]; /* 1 * MINSIGSTKSZ */
2115
2116 /* Defined in xnu unix_signal.c.
2117 Tell the kernel to re-use alt stack when delivering a signal. */
2118 #define UC_RESET_ALT_STACK 0x80000000
2119
2120 /* Return true if ADDR is within a stack guard area. */
2121 static int
2122 __gnat_is_stack_guard (mach_vm_address_t addr)
2123 {
2124 kern_return_t kret;
2125 vm_region_submap_info_data_64_t info;
2126 mach_vm_address_t start;
2127 mach_vm_size_t size;
2128 natural_t depth;
2129 mach_msg_type_number_t count;
2130
2131 count = VM_REGION_SUBMAP_INFO_COUNT_64;
2132 start = addr;
2133 size = -1;
2134 depth = 9999;
2135 kret = mach_vm_region_recurse (mach_task_self (), &start, &size, &depth,
2136 (vm_region_recurse_info_t) &info, &count);
2137 if (kret == KERN_SUCCESS
2138 && addr >= start && addr < (start + size)
2139 && info.protection == VM_PROT_NONE
2140 && info.user_tag == VM_MEMORY_STACK)
2141 return 1;
2142 return 0;
2143 }
2144
2145 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
2146
2147 #if defined (__x86_64__)
2148 static int
2149 __darwin_major_version (void)
2150 {
2151 static int cache = -1;
2152 if (cache < 0)
2153 {
2154 int mib[2] = {CTL_KERN, KERN_OSRELEASE};
2155 size_t len;
2156
2157 /* Find out how big the buffer needs to be (and set cache to 0
2158 on failure). */
2159 if (sysctl (mib, 2, NULL, &len, NULL, 0) == 0)
2160 {
2161 char release[len];
2162 sysctl (mib, 2, release, &len, NULL, 0);
2163 /* Darwin releases are of the form L.M.N where L is the major
2164 version, so strtol will return L. */
2165 cache = (int) strtol (release, NULL, 10);
2166 }
2167 else
2168 {
2169 cache = 0;
2170 }
2171 }
2172 return cache;
2173 }
2174 #endif
2175
2176 void
2177 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED,
2178 void *ucontext ATTRIBUTE_UNUSED)
2179 {
2180 #if defined (__x86_64__)
2181 if (__darwin_major_version () < 12)
2182 {
2183 /* Work around radar #10302855, where the unwinders (libunwind or
2184 libgcc_s depending on the system revision) and the DWARF unwind
2185 data for sigtramp have different ideas about register numbering,
2186 causing rbx and rdx to be transposed. */
2187 ucontext_t *uc = (ucontext_t *)ucontext;
2188 unsigned long t = uc->uc_mcontext->__ss.__rbx;
2189
2190 uc->uc_mcontext->__ss.__rbx = uc->uc_mcontext->__ss.__rdx;
2191 uc->uc_mcontext->__ss.__rdx = t;
2192 }
2193 #endif
2194 }
2195
2196 static void
2197 __gnat_error_handler (int sig, siginfo_t *si, void *ucontext)
2198 {
2199 struct Exception_Data *exception;
2200 const char *msg;
2201
2202 __gnat_adjust_context_for_raise (sig, ucontext);
2203
2204 switch (sig)
2205 {
2206 case SIGSEGV:
2207 case SIGBUS:
2208 if (__gnat_is_stack_guard ((unsigned long)si->si_addr))
2209 {
2210 exception = &storage_error;
2211 msg = "stack overflow";
2212 }
2213 else
2214 {
2215 exception = &constraint_error;
2216 msg = "erroneous memory access";
2217 }
2218 /* Reset the use of alt stack, so that the alt stack will be used
2219 for the next signal delivery.
2220 The stack can't be used in case of stack checking. */
2221 syscall (SYS_sigreturn, NULL, UC_RESET_ALT_STACK);
2222 break;
2223
2224 case SIGFPE:
2225 exception = &constraint_error;
2226 msg = "SIGFPE";
2227 break;
2228
2229 default:
2230 exception = &program_error;
2231 msg = "unhandled signal";
2232 }
2233
2234 Raise_From_Signal_Handler (exception, msg);
2235 }
2236
2237 void
2238 __gnat_install_handler (void)
2239 {
2240 struct sigaction act;
2241
2242 /* Set up signal handler to map synchronous signals to appropriate
2243 exceptions. Make sure that the handler isn't interrupted by another
2244 signal that might cause a scheduling event! Also setup an alternate
2245 stack region for the handler execution so that stack overflows can be
2246 handled properly, avoiding a SEGV generation from stack usage by the
2247 handler itself (and it is required by Darwin). */
2248
2249 stack_t stack;
2250 stack.ss_sp = __gnat_alternate_stack;
2251 stack.ss_size = sizeof (__gnat_alternate_stack);
2252 stack.ss_flags = 0;
2253 sigaltstack (&stack, NULL);
2254
2255 act.sa_flags = SA_NODEFER | SA_RESTART | SA_SIGINFO;
2256 act.sa_sigaction = __gnat_error_handler;
2257 sigemptyset (&act.sa_mask);
2258
2259 /* Do not install handlers if interrupt state is "System". */
2260 if (__gnat_get_interrupt_state (SIGABRT) != 's')
2261 sigaction (SIGABRT, &act, NULL);
2262 if (__gnat_get_interrupt_state (SIGFPE) != 's')
2263 sigaction (SIGFPE, &act, NULL);
2264 if (__gnat_get_interrupt_state (SIGILL) != 's')
2265 sigaction (SIGILL, &act, NULL);
2266
2267 act.sa_flags |= SA_ONSTACK;
2268 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
2269 sigaction (SIGSEGV, &act, NULL);
2270 if (__gnat_get_interrupt_state (SIGBUS) != 's')
2271 sigaction (SIGBUS, &act, NULL);
2272
2273 __gnat_handler_installed = 1;
2274 }
2275
2276 #else
2277
2278 /* For all other versions of GNAT, the handler does nothing. */
2279
2280 /*******************/
2281 /* Default Section */
2282 /*******************/
2283
2284 void
2285 __gnat_install_handler (void)
2286 {
2287 __gnat_handler_installed = 1;
2288 }
2289
2290 #endif
2291
2292 /*********************/
2293 /* __gnat_init_float */
2294 /*********************/
2295
2296 /* This routine is called as each process thread is created, for possible
2297 initialization of the FP processor. This version is used under INTERIX
2298 and WIN32. */
2299
2300 #if defined (_WIN32) || defined (__INTERIX) \
2301 || defined (__Lynx__) || defined(__NetBSD__) || defined(__FreeBSD__) \
2302 || defined (__OpenBSD__)
2303
2304 #define HAVE_GNAT_INIT_FLOAT
2305
2306 void
2307 __gnat_init_float (void)
2308 {
2309 #if defined (__i386__) || defined (i386) || defined (__x86_64)
2310
2311 /* This is used to properly initialize the FPU on an x86 for each
2312 process thread. */
2313
2314 asm ("finit");
2315
2316 #endif /* Defined __i386__ */
2317 }
2318 #endif
2319
2320 #ifndef HAVE_GNAT_INIT_FLOAT
2321
2322 /* All targets without a specific __gnat_init_float will use an empty one. */
2323 void
2324 __gnat_init_float (void)
2325 {
2326 }
2327 #endif
2328
2329 /***********************************/
2330 /* __gnat_adjust_context_for_raise */
2331 /***********************************/
2332
2333 #ifndef HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
2334
2335 /* All targets without a specific version will use an empty one. */
2336
2337 /* Given UCONTEXT a pointer to a context structure received by a signal
2338 handler for SIGNO, perform the necessary adjustments to let the handler
2339 raise an exception. Calls to this routine are not conditioned by the
2340 propagation scheme in use. */
2341
2342 void
2343 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED,
2344 void *ucontext ATTRIBUTE_UNUSED)
2345 {
2346 /* We used to compensate here for the raised from call vs raised from signal
2347 exception discrepancy with the GCC ZCX scheme, but this now can be dealt
2348 with generically in the unwinder (see GCC PR other/26208). This however
2349 requires the use of the _Unwind_GetIPInfo routine in raise-gcc.c, which
2350 is predicated on the definition of HAVE_GETIPINFO at compile time. Only
2351 the VMS ports still do the compensation described in the few lines below.
2352
2353 *** Call vs signal exception discrepancy with GCC ZCX scheme ***
2354
2355 The GCC unwinder expects to be dealing with call return addresses, since
2356 this is the "nominal" case of what we retrieve while unwinding a regular
2357 call chain.
2358
2359 To evaluate if a handler applies at some point identified by a return
2360 address, the propagation engine needs to determine what region the
2361 corresponding call instruction pertains to. Because the return address
2362 may not be attached to the same region as the call, the unwinder always
2363 subtracts "some" amount from a return address to search the region
2364 tables, amount chosen to ensure that the resulting address is inside the
2365 call instruction.
2366
2367 When we raise an exception from a signal handler, e.g. to transform a
2368 SIGSEGV into Storage_Error, things need to appear as if the signal
2369 handler had been "called" by the instruction which triggered the signal,
2370 so that exception handlers that apply there are considered. What the
2371 unwinder will retrieve as the return address from the signal handler is
2372 what it will find as the faulting instruction address in the signal
2373 context pushed by the kernel. Leaving this address untouched looses, if
2374 the triggering instruction happens to be the very first of a region, as
2375 the later adjustments performed by the unwinder would yield an address
2376 outside that region. We need to compensate for the unwinder adjustments
2377 at some point, and this is what this routine is expected to do.
2378
2379 signo is passed because on some targets for some signals the PC in
2380 context points to the instruction after the faulting one, in which case
2381 the unwinder adjustment is still desired. */
2382 }
2383
2384 #endif
2385
2386 #ifdef __cplusplus
2387 }
2388 #endif