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) 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 * 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-VxWorks version of the __gnat_sigtramp service *
34 ******************************************************/
35
36 #include "sigtramp.h"
37 /* See sigtramp.h for a general explanation of functionality. */
38
39 #include <vxWorks.h>
40 #include <arch/../regs.h>
41 #include <sigLib.h>
42
43 /* ----------------------
44 -- General comments --
45 ----------------------
46
47 Stubs are generated from toplevel asms and .cfi directives, much simpler
48 to use and check for correctness than manual encodings of CFI byte
49 sequences. The general idea is to establish CFA as sigcontext->sc_pregs
50 and state where to find the registers as offsets from there.
51
52 As of today, we support a single stub, providing CFI info for common
53 registers (GPRs, LR, ...). We might need variants with support for floating
54 point or altivec registers as well at some point.
55
56 Checking which variant should apply and getting at sc_pregs is simpler
57 to express in C (we can't use offsetof in toplevel asms and hardcoding
58 constants is not workable with the flurry of VxWorks variants), so this
59 is the choice for our toplevel interface.
60
61 Note that the registers we "restore" here are those to which we have
62 direct access through the system sigcontext structure, which includes
63 only a partial set of the non-volatiles ABI-wise. */
64
65 /* -----------------------------------------
66 -- Protypes for our internal asm stubs --
67 -----------------------------------------
68
69 SC_PREGS is always expected to be SIGCONTEXT->sc_pregs. Eventhough our
70 symbols will remain local, the prototype claims "extern" and not
71 "static" to prevent compiler complaints about a symbol used but never
72 defined. */
73
74 /* sigtramp stub providing CFI info for common registers. */
75
76 extern void __gnat_sigtramp_common
77 (int signo, void *siginfo, void *sigcontext,
78 sighandler_t * handler, void * sc_pregs);
79
80
81 /* -------------------------------------
82 -- Common interface implementation --
83 -------------------------------------
84
85 We enforce optimization to minimize the overhead of the extra layer. */
86
87 void __gnat_sigtramp (int signo, void *si, void *sc,
88 sighandler_t * handler)
89 __attribute__((optimize(2)));
90
__gnat_sigtramp(int signo,void * si,void * sc,sighandler_t * handler)91 void __gnat_sigtramp (int signo, void *si, void *sc,
92 sighandler_t * handler)
93 {
94 struct sigcontext * sctx = (struct sigcontext *) sc;
95
96 __gnat_sigtramp_common (signo, si, sctx, handler, sctx->sc_pregs);
97 }
98
99
100 /* ---------------------------
101 -- And now the asm stubs --
102 ---------------------------
103
104 They all have a common structure with blocks of asm sequences queued one
105 after the others. Typically:
106
107 SYMBOL_START
108
109 CFI_DIRECTIVES
110 CFI_DEF_CFA,
111 CFI_COMMON_REGISTERS,
112 ...
113
114 STUB_BODY
115 asm code to establish frame, setup the cfa reg value,
116 call the real signal handler, ...
117
118 SYMBOL_END
119 */
120
121 /*--------------------------------
122 -- Misc constants and helpers --
123 -------------------------------- */
124
125 /* REGNO constants, dwarf column numbers for registers of interest. */
126
127 #define REGNO_G_REG_OFFSET(N) (N)
128
129 #define REGNO_PC_OFFSET 15 /* PC_REGNUM */
130
131 /* asm string construction helpers. */
132
133 #define STR(TEXT) #TEXT
134 /* stringify expanded TEXT, surrounding it with double quotes. */
135
136 #define S(E) STR(E)
137 /* stringify E, which will resolve as text but may contain macros
138 still to be expanded. */
139
140 /* asm (TEXT) outputs <tab>TEXT. These facilitate the output of
141 multine contents: */
142 #define TAB(S) "\t" S
143 #define CR(S) S "\n"
144
145 #undef TCR
146 #define TCR(S) TAB(CR(S))
147
148 /*------------------------------
149 -- Stub construction blocks --
150 ------------------------------ */
151
152 /* CFA setup block
153 ---------------
154 Only non-volatile registers are suitable for a CFA base. These are the
155 only ones we can expect to be able retrieve from the unwinding context
156 while walking up the chain, saved by at least the bottom-most exception
157 propagation services. We use r8 here and set it to the value we need
158 in stub body that follows. Any of r4-r8 should work. */
159
160 #define CFA_REG 8
161
162 #define CFI_DEF_CFA \
163 CR(".cfi_def_cfa " S(CFA_REG) ", 0")
164
165 /* Register location blocks
166 ------------------------
167 Rules to find registers of interest from the CFA. This should comprise
168 all the non-volatile registers relevant to the interrupted context. */
169
170 #define COMMON_CFI(REG) \
171 ".cfi_offset " S(REGNO_##REG) "," S(REG_SET_##REG)
172
173 #define CFI_COMMON_REGS \
174 CR("# CFI for common registers\n") \
175 TCR(COMMON_CFI(G_REG_OFFSET(0))) \
176 TCR(COMMON_CFI(G_REG_OFFSET(1))) \
177 TCR(COMMON_CFI(G_REG_OFFSET(2))) \
178 TCR(COMMON_CFI(G_REG_OFFSET(3))) \
179 TCR(COMMON_CFI(G_REG_OFFSET(4))) \
180 TCR(COMMON_CFI(G_REG_OFFSET(5))) \
181 TCR(COMMON_CFI(G_REG_OFFSET(6))) \
182 TCR(COMMON_CFI(G_REG_OFFSET(7))) \
183 TCR(COMMON_CFI(G_REG_OFFSET(8))) \
184 TCR(COMMON_CFI(G_REG_OFFSET(9))) \
185 TCR(COMMON_CFI(G_REG_OFFSET(10))) \
186 TCR(COMMON_CFI(G_REG_OFFSET(11))) \
187 TCR(COMMON_CFI(G_REG_OFFSET(12))) \
188 TCR(COMMON_CFI(G_REG_OFFSET(13))) \
189 TCR(COMMON_CFI(G_REG_OFFSET(14))) \
190 TCR(COMMON_CFI(PC_OFFSET)) \
191 TCR(".cfi_return_column " S(REGNO_PC_OFFSET))
192
193 /* Trampoline body block
194 --------------------- */
195
196 #define SIGTRAMP_BODY \
197 CR("") \
198 TCR("# Allocate frame and save the non-volatile") \
199 TCR("# registers we're going to modify") \
200 TCR("mov ip, sp") \
201 TCR("stmfd sp!, {r"S(CFA_REG)", fp, ip, lr, pc}") \
202 TCR("# Setup CFA_REG = sc_pregs, that we'll retrieve as our CFA value") \
203 TCR("ldr r"S(CFA_REG)", [ip]") \
204 TCR("") \
205 TCR("# Call the real handler. The signo, siginfo and sigcontext") \
206 TCR("# arguments are the same as those we received in r0, r1 and r2") \
207 TCR("sub fp, ip, #4") \
208 TCR("blx r3") \
209 TCR("# Restore our callee-saved items, release our frame and return") \
210 TCR("ldmfd sp, {r"S(CFA_REG)", fp, sp, pc}")
211
212
213 /* Symbol definition block
214 ----------------------- */
215
216 #define SIGTRAMP_START(SYM) \
217 CR("# " S(SYM) " cfi trampoline") \
218 TCR(".type " S(SYM) ", %function") \
219 CR("") \
220 CR(S(SYM) ":") \
221 TCR(".cfi_startproc") \
222 TCR(".cfi_signal_frame")
223
224 /* Symbol termination block
225 ------------------------ */
226
227 #define SIGTRAMP_END(SYM) \
228 CR(".cfi_endproc") \
229 TCR(".size " S(SYM) ", .-" S(SYM))
230
231 /*----------------------------
232 -- And now, the real code --
233 ---------------------------- */
234
235 /* Text section start. The compiler isn't aware of that switch. */
236
237 asm (".text\n"
238 TCR(".align 2"));
239
240 /* sigtramp stub for common registers. */
241
242 #define TRAMP_COMMON __gnat_sigtramp_common
243
244 asm (SIGTRAMP_START(TRAMP_COMMON));
245 asm (CFI_DEF_CFA);
246 asm (CFI_COMMON_REGS);
247 asm (SIGTRAMP_BODY);
248 asm (SIGTRAMP_END(TRAMP_COMMON));
249
250
251