| 1 |
/**************************************************************************** |
| 2 |
* * |
| 3 |
* GNAT COMPILER COMPONENTS * |
| 4 |
* * |
| 5 |
* S I G T R A M P * |
| 6 |
* * |
| 7 |
* Asm Implementation File * |
| 8 |
* * |
| 9 |
* Copyright (C) 2011-2025, 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 |
* In particular, you can freely distribute your programs built with the * |
| 23 |
* GNAT Pro compiler, including any required library run-time units, using * |
| 24 |
* any licensing terms of your choosing. See the AdaCore Software License * |
| 25 |
* for full details. * |
| 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 |
/************************************************** |
| 33 |
* ARM-QNX version of the __gnat_sigtramp service * |
| 34 |
**************************************************/ |
| 35 |
|
| 36 |
#include <signal.h> |
| 37 |
#include <ucontext.h> |
| 38 |
|
| 39 |
#include "sigtramp.h" |
| 40 |
/* See sigtramp.h for a general explanation of functionality. */ |
| 41 |
|
| 42 |
/* ------------------------------------------- |
| 43 |
-- Prototypes for our internal asm stubs -- |
| 44 |
------------------------------------------- |
| 45 |
|
| 46 |
Eventhough our symbols will remain local, the prototype claims "extern" |
| 47 |
and not "static" to prevent compiler complaints about a symbol used but |
| 48 |
never defined. */ |
| 49 |
|
| 50 |
/* sigtramp stub providing ARM unwinding info for common registers. */ |
| 51 |
|
| 52 |
extern void __gnat_sigtramp_common |
| 53 |
(int signo, void *siginfo, void *sigcontext, |
| 54 |
__sigtramphandler_t * handler, void * sc_pregs); |
| 55 |
|
| 56 |
/* ------------------------------------- |
| 57 |
-- Common interface implementation -- |
| 58 |
------------------------------------- |
| 59 |
|
| 60 |
We enforce optimization to minimize the overhead of the extra layer. */ |
| 61 |
|
| 62 |
void __gnat_sigtramp (int signo, void *si, void *sc, |
| 63 |
__sigtramphandler_t * handler) |
| 64 |
__attribute__((optimize(2))); |
| 65 |
|
| 66 |
void __gnat_sigtramp (int signo, void *si, void *sc, |
| 67 |
__sigtramphandler_t * handler) |
| 68 |
{ |
| 69 |
mcontext_t *mcontext = &((ucontext_t *) sc)->uc_mcontext; |
| 70 |
|
| 71 |
/* Pass MCONTEXT in the fifth position so that the assembly code can find |
| 72 |
it at the same stack location as SC_PREGS. */ |
| 73 |
__gnat_sigtramp_common (signo, si, mcontext, handler, &mcontext->cpu); |
| 74 |
} |
| 75 |
|
| 76 |
/* asm string construction helpers. */ |
| 77 |
|
| 78 |
#define STR(TEXT) #TEXT |
| 79 |
/* stringify expanded TEXT, surrounding it with double quotes. */ |
| 80 |
|
| 81 |
#define S(E) STR(E) |
| 82 |
/* stringify E, which will resolve as text but may contain macros |
| 83 |
still to be expanded. */ |
| 84 |
|
| 85 |
/* asm (TEXT) outputs <tab>TEXT. These facilitate the output of |
| 86 |
multiline contents: */ |
| 87 |
#define TAB(S) "\t" S |
| 88 |
#define CR(S) S "\n" |
| 89 |
|
| 90 |
#undef TCR |
| 91 |
#define TCR(S) TAB(CR(S)) |
| 92 |
|
| 93 |
/* Trampoline body block |
| 94 |
--------------------- */ |
| 95 |
|
| 96 |
/* The 5 arguments passed to __gnat_sigtramp_common are located in: |
| 97 |
- r0-r2: arguments to pass on to the actual handler |
| 98 |
- r3: the actual handler |
| 99 |
- sp: the address of the reg set to restore |
| 100 |
All we have to do then is to instruct the unwinder to restore the registers |
| 101 |
from the value in VSP. Unwinder instructions are executed backwards, so we |
| 102 |
1- instruct to pop r2 from the VSP (.save {r2}) |
| 103 |
2- move the VSP to the address pointed to by r2 (.movsp r2) |
| 104 |
3- restore all registers from there. (.save {r0-r15}) |
| 105 |
Once the unwinding instructions are set, we just need to call the handler |
| 106 |
as r0-r2 are already properly set. |
| 107 |
*/ |
| 108 |
#define SIGTRAMP_BODY \ |
| 109 |
CR("") \ |
| 110 |
TCR(".save {r0-r15}") \ |
| 111 |
TCR(".movsp r2") \ |
| 112 |
TCR(".save {r2}") \ |
| 113 |
TCR("blx r3") \ |
| 114 |
TCR("# No return here.") |
| 115 |
|
| 116 |
/* Symbol definition block |
| 117 |
----------------------- */ |
| 118 |
|
| 119 |
#define SIGTRAMP_START(SYM) \ |
| 120 |
CR("# " S(SYM) " unwind trampoline") \ |
| 121 |
TCR(".type " S(SYM) ", %function") \ |
| 122 |
CR("") \ |
| 123 |
CR(S(SYM) ":") \ |
| 124 |
TCR(".fnstart") |
| 125 |
|
| 126 |
/* Symbol termination block |
| 127 |
------------------------ */ |
| 128 |
|
| 129 |
#define SIGTRAMP_END(SYM) \ |
| 130 |
CR(".fnend") \ |
| 131 |
TCR(".size " S(SYM) ", .-" S(SYM)) |
| 132 |
|
| 133 |
/*---------------------------- |
| 134 |
-- And now, the real code -- |
| 135 |
---------------------------- */ |
| 136 |
|
| 137 |
/* Text section start. The compiler isn't aware of that switch. */ |
| 138 |
|
| 139 |
asm (".text\n" |
| 140 |
TCR(".align 2")); |
| 141 |
|
| 142 |
/* sigtramp stub for common registers. */ |
| 143 |
|
| 144 |
#define TRAMP_COMMON __gnat_sigtramp_common |
| 145 |
|
| 146 |
asm (SIGTRAMP_START(TRAMP_COMMON)); |
| 147 |
asm (SIGTRAMP_BODY); |
| 148 |
asm (SIGTRAMP_END(TRAMP_COMMON)); |