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-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 /**********************************************************
33 * PowerPC-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_LR 65
128 #define REGNO_CTR 66
129 #define REGNO_CR 70
130 #define REGNO_XER 76
131 #define REGNO_GR(N) (N)
132
133 #define REGNO_PC 67 /* ARG_POINTER_REGNUM */
134
135 /* asm string construction helpers. */
136
137 #define STR(TEXT) #TEXT
138 /* stringify expanded TEXT, surrounding it with double quotes. */
139
140 #define S(E) STR(E)
141 /* stringify E, which will resolve as text but may contain macros
142 still to be expanded. */
143
144 /* asm (TEXT) outputs <tab>TEXT. These facilitate the output of
145 multine contents: */
146 #define TAB(S) "\t" S
147 #define CR(S) S "\n"
148
149 #undef TCR
150 #define TCR(S) TAB(CR(S))
151
152 /*------------------------------
153 -- Stub construction blocks --
154 ------------------------------ */
155
156 /* CFA setup block
157 ---------------
158 Only non-volatile registers are suitable for a CFA base. These are the
159 only ones we can expect to be able retrieve from the unwinding context
160 while walking up the chain, saved by at least the bottom-most exception
161 propagation services. We use r15 here and set it to the value we need
162 in stub body that follows. Note that r14 is inappropriate here, even
163 though it is non-volatile according to the ABI, because GCC uses it as
164 an extra SCRATCH on SPE targets. */
165
166 #define CFA_REG 15
167
168 #define CFI_DEF_CFA \
169 CR(".cfi_def_cfa " S(CFA_REG) ", 0")
170
171 /* Register location blocks
172 ------------------------
173 Rules to find registers of interest from the CFA. This should comprise
174 all the non-volatile registers relevant to the interrupted context.
175
176 Note that we include r1 in this set, unlike the libgcc unwinding
177 fallbacks. This is useful for fallbacks to allow the use of r1 in CFI
178 expressions and the absence of rule for r1 gets compensated by using the
179 target CFA instead. We don't need the expression facility here and
180 setup a fake CFA to allow very simple offset expressions, so having a
181 rule for r1 is the proper thing to do. We for sure have observed
182 crashes in some cases without it. */
183
184 #define COMMON_CFI(REG) \
185 ".cfi_offset " S(REGNO_##REG) "," S(REG_SET_##REG)
186
187 #define CFI_COMMON_REGS \
188 CR("# CFI for common registers\n") \
189 TCR(COMMON_CFI(GR(0))) \
190 TCR(COMMON_CFI(GR(1))) \
191 TCR(COMMON_CFI(GR(2))) \
192 TCR(COMMON_CFI(GR(3))) \
193 TCR(COMMON_CFI(GR(4))) \
194 TCR(COMMON_CFI(GR(5))) \
195 TCR(COMMON_CFI(GR(6))) \
196 TCR(COMMON_CFI(GR(7))) \
197 TCR(COMMON_CFI(GR(8))) \
198 TCR(COMMON_CFI(GR(9))) \
199 TCR(COMMON_CFI(GR(10))) \
200 TCR(COMMON_CFI(GR(11))) \
201 TCR(COMMON_CFI(GR(12))) \
202 TCR(COMMON_CFI(GR(13))) \
203 TCR(COMMON_CFI(GR(14))) \
204 TCR(COMMON_CFI(GR(15))) \
205 TCR(COMMON_CFI(GR(16))) \
206 TCR(COMMON_CFI(GR(17))) \
207 TCR(COMMON_CFI(GR(18))) \
208 TCR(COMMON_CFI(GR(19))) \
209 TCR(COMMON_CFI(GR(20))) \
210 TCR(COMMON_CFI(GR(21))) \
211 TCR(COMMON_CFI(GR(22))) \
212 TCR(COMMON_CFI(GR(23))) \
213 TCR(COMMON_CFI(GR(24))) \
214 TCR(COMMON_CFI(GR(25))) \
215 TCR(COMMON_CFI(GR(26))) \
216 TCR(COMMON_CFI(GR(27))) \
217 TCR(COMMON_CFI(GR(28))) \
218 TCR(COMMON_CFI(GR(29))) \
219 TCR(COMMON_CFI(GR(30))) \
220 TCR(COMMON_CFI(GR(31))) \
221 TCR(COMMON_CFI(LR)) \
222 TCR(COMMON_CFI(CR)) \
223 TCR(COMMON_CFI(CTR)) \
224 TCR(COMMON_CFI(XER)) \
225 TCR(COMMON_CFI(PC)) \
226 TCR(".cfi_return_column " S(REGNO_PC))
227
228 /* Trampoline body block
229 --------------------- */
230
231 #define SIGTRAMP_BODY \
232 CR("") \
233 TCR("# Allocate frame and save the non-volatile") \
234 TCR("# registers we're going to modify") \
235 TCR("stwu %r1,-16(%r1)") \
236 TCR("mflr %r0") \
237 TCR("stw %r0,20(%r1)") \
238 TCR("stw %r" S(CFA_REG) ",8(%r1)") \
239 TCR("") \
240 TCR("# Setup CFA_REG = sc_pregs, that we'll retrieve as our CFA value") \
241 TCR("mr %r" S(CFA_REG) ", %r7") \
242 TCR("") \
243 TCR("# Call the real handler. The signo, siginfo and sigcontext") \
244 TCR("# arguments are the same as those we received in r3, r4 and r5") \
245 TCR("mtctr %r6") \
246 TCR("bctrl") \
247 TCR("") \
248 TCR("# Restore our callee-saved items, release our frame and return") \
249 TCR("lwz %r" S(CFA_REG) ",8(%r1)") \
250 TCR("lwz %r0,20(%r1)") \
251 TCR("mtlr %r0") \
252 TCR("") \
253 TCR("addi %r1,%r1,16") \
254 TCR("blr")
255
256 /* Symbol definition block
257 ----------------------- */
258
259 #define SIGTRAMP_START(SYM) \
260 CR("# " S(SYM) " cfi trampoline") \
261 TCR(".type " S(SYM) ", @function") \
262 CR("") \
263 CR(S(SYM) ":") \
264 TCR(".cfi_startproc") \
265 TCR(".cfi_signal_frame")
266
267 /* Symbol termination block
268 ------------------------ */
269
270 #define SIGTRAMP_END(SYM) \
271 CR(".cfi_endproc") \
272 TCR(".size " S(SYM) ", .-" S(SYM))
273
274 /*----------------------------
275 -- And now, the real code --
276 ---------------------------- */
277
278 /* Text section start. The compiler isn't aware of that switch. */
279
280 asm (".text\n"
281 TCR(".align 2"));
282
283 /* sigtramp stub for common registers. */
284
285 #define TRAMP_COMMON __gnat_sigtramp_common
286
287 asm (SIGTRAMP_START(TRAMP_COMMON));
288 asm (CFI_DEF_CFA);
289 asm (CFI_COMMON_REGS);
290 asm (SIGTRAMP_BODY);
291 asm (SIGTRAMP_END(TRAMP_COMMON));
292
293
294