1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
5 * S I G T R A M P - T A R G E T *
7 * Asm Implementation Include File *
9 * Copyright (C) 2011-2015, Free Software Foundation, Inc. *
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. *
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. *
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 *
27 * GNAT was originally developed by the GNAT team at New York University. *
28 * Extensive contributions were provided by Ada Core Technologies Inc. *
30 ****************************************************************************/
32 /***************************************************************
33 * VxWorks target specific part of the __gnat_sigtramp service *
34 ***************************************************************/
36 /* Note: This target specific part is kept in a separate file to avoid
37 duplication of its code for the vxworks and vxworks-vxsim asm
38 implementation files. */
40 /* ---------------------------
41 -- And now the asm stubs --
42 ---------------------------
44 They all have a common structure with blocks of asm sequences queued one
45 after the others. Typically:
55 asm code to establish frame, setup the cfa reg value,
56 call the real signal handler, ...
61 /*--------------------------------
62 -- Misc constants and helpers --
63 -------------------------------- */
65 /* asm string construction helpers. */
67 #define STR(TEXT) #TEXT
68 /* stringify expanded TEXT, surrounding it with double quotes. */
71 /* stringify E, which will resolve as text but may contain macros
72 still to be expanded. */
74 /* asm (TEXT) outputs <tab>TEXT. These facilitate the output of
80 #define TCR(S) TAB(CR(S))
82 /* REGNO constants, dwarf column numbers for registers of interest. */
90 #define REGNO_GR(N) (N)
92 #define REGNO_PC 67 /* ARG_POINTER_REGNUM */
94 #define FUNCTION "@function"
96 #elif defined (__ARMEL__)
98 #define REGNO_G_REG_OFFSET(N) (N)
100 #define REGNO_PC_OFFSET 15 /* PC_REGNUM */
102 #define FUNCTION "%function"
106 /* These are the cfi colunm numbers */
116 #define REGNO_EFLAGS 9
117 #define REGNO_SET_PC 8 /* aka %eip */
119 #define FUNCTION "@function"
121 /* Mapping of CFI Column, Gcc Regno, Signal context offset for 32bit
137 There is no unique numbering for the x86 architecture. It's parameterized
138 by DWARF_FRAME_REGNUM, which is DBX_REGISTER_NUMBER except for Windows, and
139 the latter depends on the platform.
142 #elif defined (__x86_64__)
144 /* These are the cfi colunm numbers */
162 #define REGNO_SET_PC 16 /* aka %rip */
163 #define REGNO_EFLAGS 49
166 #define FUNCTION "@function"
170 #endif /* REGNO constants */
173 /*------------------------------
174 -- Stub construction blocks --
175 ------------------------------ */
179 Only non-volatile registers are suitable for a CFA base. These are the
180 only ones we can expect to be able retrieve from the unwinding context
181 while walking up the chain, saved by at least the bottom-most exception
182 propagation services. We set a non-volatile register to the value we
183 need in the stub body that follows. */
185 #if defined (__PPC__)
187 /* Use r15 for PPC. Note that r14 is inappropriate here, even though it
188 is non-volatile according to the ABI, because GCC uses it as an extra
189 SCRATCH on SPE targets. */
193 #elif defined (__ARMEL__)
195 /* Use r8 for ARM. Any of r4-r8 should work. */
203 #elif defined (__x86_64__)
210 #endif /* CFA setup block */
212 #define CFI_DEF_CFA \
213 CR(".cfi_def_cfa " S(CFA_REG) ", 0")
215 /* Register location blocks
216 ------------------------
217 Rules to find registers of interest from the CFA. This should comprise
218 all the non-volatile registers relevant to the interrupted context.
220 Note that we include r1 in this set, unlike the libgcc unwinding
221 fallbacks. This is useful for fallbacks to allow the use of r1 in CFI
222 expressions and the absence of rule for r1 gets compensated by using the
223 target CFA instead. We don't need the expression facility here and
224 setup a fake CFA to allow very simple offset expressions, so having a
225 rule for r1 is the proper thing to do. We for sure have observed
226 crashes in some cases without it. */
228 #if defined (__PPC__)
230 #define COMMON_CFI(REG) \
231 ".cfi_offset " S(REGNO_##REG) "," S(REG_SET_##REG)
233 #define CFI_COMMON_REGS \
234 CR("# CFI for common registers\n") \
235 TCR(COMMON_CFI(GR(0))) \
236 TCR(COMMON_CFI(GR(1))) \
237 TCR(COMMON_CFI(GR(2))) \
238 TCR(COMMON_CFI(GR(3))) \
239 TCR(COMMON_CFI(GR(4))) \
240 TCR(COMMON_CFI(GR(5))) \
241 TCR(COMMON_CFI(GR(6))) \
242 TCR(COMMON_CFI(GR(7))) \
243 TCR(COMMON_CFI(GR(8))) \
244 TCR(COMMON_CFI(GR(9))) \
245 TCR(COMMON_CFI(GR(10))) \
246 TCR(COMMON_CFI(GR(11))) \
247 TCR(COMMON_CFI(GR(12))) \
248 TCR(COMMON_CFI(GR(13))) \
249 TCR(COMMON_CFI(GR(14))) \
250 TCR(COMMON_CFI(GR(15))) \
251 TCR(COMMON_CFI(GR(16))) \
252 TCR(COMMON_CFI(GR(17))) \
253 TCR(COMMON_CFI(GR(18))) \
254 TCR(COMMON_CFI(GR(19))) \
255 TCR(COMMON_CFI(GR(20))) \
256 TCR(COMMON_CFI(GR(21))) \
257 TCR(COMMON_CFI(GR(22))) \
258 TCR(COMMON_CFI(GR(23))) \
259 TCR(COMMON_CFI(GR(24))) \
260 TCR(COMMON_CFI(GR(25))) \
261 TCR(COMMON_CFI(GR(26))) \
262 TCR(COMMON_CFI(GR(27))) \
263 TCR(COMMON_CFI(GR(28))) \
264 TCR(COMMON_CFI(GR(29))) \
265 TCR(COMMON_CFI(GR(30))) \
266 TCR(COMMON_CFI(GR(31))) \
267 TCR(COMMON_CFI(LR)) \
268 TCR(COMMON_CFI(CR)) \
269 TCR(COMMON_CFI(CTR)) \
270 TCR(COMMON_CFI(XER)) \
271 TCR(COMMON_CFI(PC)) \
272 TCR(".cfi_return_column " S(REGNO_PC))
274 /* Trampoline body block
275 --------------------- */
277 #define SIGTRAMP_BODY \
279 TCR("# Allocate frame and save the non-volatile") \
280 TCR("# registers we're going to modify") \
281 TCR("stwu %r1,-16(%r1)") \
283 TCR("stw %r0,20(%r1)") \
284 TCR("stw %r" S(CFA_REG) ",8(%r1)") \
286 TCR("# Setup CFA_REG = context, which we'll retrieve as our CFA value") \
287 TCR("mr %r" S(CFA_REG) ", %r7") \
289 TCR("# Call the real handler. The signo, siginfo and sigcontext") \
290 TCR("# arguments are the same as those we received in r3, r4 and r5") \
294 TCR("# Restore our callee-saved items, release our frame and return") \
295 TCR("lwz %r" S(CFA_REG) ",8(%r1)") \
296 TCR("lwz %r0,20(%r1)") \
299 TCR("addi %r1,%r1,16") \
302 #elif defined (__ARMEL__)
304 #define COMMON_CFI(REG) \
305 ".cfi_offset " S(REGNO_##REG) "," S(REG_SET_##REG)
307 #define CFI_COMMON_REGS \
308 CR("# CFI for common registers\n") \
309 TCR(COMMON_CFI(G_REG_OFFSET(0))) \
310 TCR(COMMON_CFI(G_REG_OFFSET(1))) \
311 TCR(COMMON_CFI(G_REG_OFFSET(2))) \
312 TCR(COMMON_CFI(G_REG_OFFSET(3))) \
313 TCR(COMMON_CFI(G_REG_OFFSET(4))) \
314 TCR(COMMON_CFI(G_REG_OFFSET(5))) \
315 TCR(COMMON_CFI(G_REG_OFFSET(6))) \
316 TCR(COMMON_CFI(G_REG_OFFSET(7))) \
317 TCR(COMMON_CFI(G_REG_OFFSET(8))) \
318 TCR(COMMON_CFI(G_REG_OFFSET(9))) \
319 TCR(COMMON_CFI(G_REG_OFFSET(10))) \
320 TCR(COMMON_CFI(G_REG_OFFSET(11))) \
321 TCR(COMMON_CFI(G_REG_OFFSET(12))) \
322 TCR(COMMON_CFI(G_REG_OFFSET(13))) \
323 TCR(COMMON_CFI(G_REG_OFFSET(14))) \
324 TCR(COMMON_CFI(PC_OFFSET)) \
325 TCR(".cfi_return_column " S(REGNO_PC_OFFSET))
327 /* Trampoline body block
328 --------------------- */
330 #define SIGTRAMP_BODY \
332 TCR("# Allocate frame and save the non-volatile") \
333 TCR("# registers we're going to modify") \
335 TCR("stmfd sp!, {r"S(CFA_REG)", fp, ip, lr, pc}") \
336 TCR("# Setup CFA_REG = context, which we'll retrieve as our CFA value") \
337 TCR("ldr r"S(CFA_REG)", [ip]") \
339 TCR("# Call the real handler. The signo, siginfo and sigcontext") \
340 TCR("# arguments are the same as those we received in r0, r1 and r2") \
341 TCR("sub fp, ip, #4") \
343 TCR("# Restore our callee-saved items, release our frame and return") \
344 TCR("ldmfd sp, {r"S(CFA_REG)", fp, sp, pc}")
348 #if CPU == SIMNT || CPU == SIMPENTIUM || CPU == SIMLINUX
349 #define COMMON_CFI(REG) \
350 ".cfi_offset " S(REGNO_##REG) "," S(REG_SET_##REG)
352 #define COMMON_CFI(REG) \
353 ".cfi_offset " S(REGNO_##REG) "," S(REG_##REG)
356 #define PC_CFI(REG) \
357 ".cfi_offset " S(REGNO_##REG) "," S(REG_##REG)
359 #define CFI_COMMON_REGS \
360 CR("# CFI for common registers\n") \
361 TCR(COMMON_CFI(EDI)) \
362 TCR(COMMON_CFI(ESI)) \
363 TCR(COMMON_CFI(EBP)) \
364 TCR(COMMON_CFI(ESP)) \
365 TCR(COMMON_CFI(EBX)) \
366 TCR(COMMON_CFI(EDX)) \
367 TCR(COMMON_CFI(ECX)) \
368 TCR(COMMON_CFI(EAX)) \
369 TCR(COMMON_CFI(EFLAGS)) \
370 TCR(PC_CFI(SET_PC)) \
371 TCR(".cfi_return_column " S(REGNO_SET_PC))
373 /* Trampoline body block
374 --------------------- */
376 #define SIGTRAMP_BODY \
378 TCR("# Allocate frame and save the non-volatile") \
379 TCR("# registers we're going to modify") \
381 TCR("movl %esp, %ebp") \
383 TCR("subl $24, %esp") \
384 TCR("# Setup CFA_REG = context, which we'll retrieve as our CFA value") \
385 TCR("movl 24(%ebp), %edi") \
386 TCR("# Call the real handler. The signo, siginfo and sigcontext") \
387 TCR("# arguments are the same as those we received") \
388 TCR("movl 16(%ebp), %eax") \
389 TCR("movl %eax, 8(%esp)") \
390 TCR("movl 12(%ebp), %eax") \
391 TCR("movl %eax, 4(%esp)") \
392 TCR("movl 8(%ebp), %eax") \
393 TCR("movl %eax, (%esp)") \
394 TCR("call *20(%ebp)") \
395 TCR("# Restore our callee-saved items, release our frame and return") \
400 #elif defined (__x86_64__)
402 #define COMMON_CFI(REG) \
403 ".cfi_offset " S(REGNO_##REG) "," S(REG_##REG)
404 #define PC_CFI(REG) \
405 ".cfi_offset " S(REGNO_##REG) "," S(REG_##REG)
407 #define CFI_COMMON_REGS \
408 CR("# CFI for common registers\n") \
409 TCR(COMMON_CFI(R15)) \
410 TCR(COMMON_CFI(R14)) \
411 TCR(COMMON_CFI(R13)) \
412 TCR(COMMON_CFI(R12)) \
413 TCR(COMMON_CFI(R11)) \
414 TCR(COMMON_CFI(R10)) \
415 TCR(COMMON_CFI(R9)) \
416 TCR(COMMON_CFI(R8)) \
417 TCR(COMMON_CFI(RDI)) \
418 TCR(COMMON_CFI(RSI)) \
419 TCR(COMMON_CFI(RBP)) \
420 TCR(COMMON_CFI(RSP)) \
421 TCR(COMMON_CFI(RBX)) \
422 TCR(COMMON_CFI(RDX)) \
423 TCR(COMMON_CFI(RCX)) \
424 TCR(COMMON_CFI(RAX)) \
425 TCR(COMMON_CFI(EFLAGS)) \
426 TCR(COMMON_CFI(SET_PC)) \
427 TCR(COMMON_CFI(FS)) \
428 TCR(".cfi_return_column " S(REGNO_SET_PC))
430 /* Trampoline body block
431 --------------------- */
433 #define SIGTRAMP_BODY \
435 TCR("# Allocate frame and save the non-volatile") \
436 TCR("# registers we're going to modify") \
437 TCR("subq $8, %rsp") \
438 TCR("# Setup CFA_REG = context, which we'll retrieve as our CFA value") \
439 TCR("movq %r8, %r15") \
440 TCR("# Call the real handler. The signo, siginfo and sigcontext") \
441 TCR("# arguments are the same as those we received") \
443 TCR("# This part should never be executed") \
444 TCR("addq $8, %rsp") \
449 #endif /* CFI_COMMON_REGS and SIGTRAMP_BODY */
451 /* Symbol definition block
452 ----------------------- */
454 #define SIGTRAMP_START(SYM) \
455 CR("# " S(SYM) " cfi trampoline") \
456 TCR(".type " S(SYM) ", "FUNCTION) \
459 TCR(".cfi_startproc") \
460 TCR(".cfi_signal_frame")
462 /* Symbol termination block
463 ------------------------ */
465 #define SIGTRAMP_END(SYM) \
467 TCR(".size " S(SYM) ", .-" S(SYM))
469 /*----------------------------
470 -- And now, the real code --
471 ---------------------------- */
473 /* Text section start. The compiler isn't aware of that switch. */