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) 2014, 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-Android version of the __gnat_sigtramp service *
34 ******************************************************/
35
36#include "sigtramp.h"
37/* See sigtramp.h for a general explanation of functionality.  */
38
39/* ----------------------
40   -- General comments --
41   ----------------------
42
43   Stubs are generated from toplevel asms,
44   The general idea is to establish CFA as the sigcontext
45   and state where to find the registers as offsets from there.
46
47   We support stubs for VxWorks and Android, providing unwind info for
48   common registers. We might need variants with support for floating
49   point or altivec registers as well at some point.
50
51   For Android it would be simpler to write this in Asm since there's only
52   one variant, but to keep it looking like the VxWorks stubs,
53   C is the choice for our toplevel interface.
54
55   Note that the registers we "restore" here are those to which we have
56   direct access through the system sigcontext structure, which includes
57   only a partial set of the non-volatiles ABI-wise.  */
58
59/* -----------------------------------------
60   -- Protypes for our internal asm stubs --
61   -----------------------------------------
62
63   The registers are expected to be at SIGCONTEXT + 12 (reference the
64   sicontext structure in asm/sigcontext.h which describes the first
65   3 * 4byte fields.)  Even though our symbols will remain local, the
66   prototype claims "extern" and not "static" to prevent compiler complaints
67   about a symbol used but never defined.  */
68
69/* sigtramp stub providing unwind info for common registers.  */
70
71extern void __gnat_sigtramp_common
72  (int signo, void *siginfo, void *sigcontext,
73   __sigtramphandler_t * handler);
74
75void __gnat_sigtramp (int signo, void *si, void *sc,
76                      __sigtramphandler_t * handler)
77     __attribute__((optimize(2)));
78
79void __gnat_sigtramp (int signo, void *si, void *ucontext,
80                      __sigtramphandler_t * handler)
81{
82  struct sigcontext *mcontext = &((ucontext_t *) ucontext)->uc_mcontext;
83
84  __gnat_sigtramp_common (signo, si, mcontext, handler);
85}
86
87/* asm string construction helpers.  */
88
89#define STR(TEXT) #TEXT
90/* stringify expanded TEXT, surrounding it with double quotes.  */
91
92#define S(E) STR(E)
93/* stringify E, which will resolve as text but may contain macros
94   still to be expanded.  */
95
96/* asm (TEXT) outputs <tab>TEXT. These facilitate the output of
97   multiline contents:  */
98#define TAB(S) "\t" S
99#define CR(S)  S "\n"
100
101#undef TCR
102#define TCR(S) TAB(CR(S))
103
104/* Trampoline body block
105   ---------------------  */
106
107#define SIGTRAMP_BODY \
108CR("") \
109TCR("# Allocate frame and also save r2 which is the argument register") \
110TCR("# containing the sigcontext, so that we can restore it during") \
111TCR("# unwinding and thereby load the rest of the desired context.") \
112TCR("stmfd	sp!, {r2, r3, lr}") \
113TCR("# The unwinder undo's these operations in reverse order so starting") \
114TCR("# from bottom, restore r2 from the current vsp location, move r2 into") \
115TCR("# the vsp, add 12 bytes to get the start of the register save area") \
116TCR("# then restore the 15 general purpose registers of the frame which") \
117TCR("# raised the signal.") \
118TCR(".save {r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, r14, r15}") \
119TCR(".pad #12") \
120TCR(".movsp r2") \
121TCR(".save {r2}") \
122TCR("# Call the real handler. The signo, siginfo and sigcontext") \
123TCR("# arguments are the same as those we received in r0, r1 and r2.") \
124TCR("blx	r3") \
125TCR("# Restore our callee-saved items, release our frame and return") \
126TCR("# (should never get here!).") \
127TCR("ldmfd	sp, {r2, r3, pc}")
128
129/* Symbol definition block
130   -----------------------  */
131
132#define SIGTRAMP_START(SYM) \
133CR("# " S(SYM) " unwind trampoline") \
134TCR(".type " S(SYM) ", %function") \
135CR("") \
136CR(S(SYM) ":") \
137TCR(".fnstart")
138
139/* Symbol termination block
140   ------------------------  */
141
142#define SIGTRAMP_END(SYM) \
143CR(".fnend") \
144TCR(".size " S(SYM) ", .-" S(SYM))
145
146/*----------------------------
147  -- And now, the real code --
148  ---------------------------- */
149
150/* Text section start.  The compiler isn't aware of that switch.  */
151
152asm (".text\n"
153     TCR(".align 2"));
154
155/* sigtramp stub for common registers.  */
156
157#define TRAMP_COMMON __gnat_sigtramp_common
158
159asm (SIGTRAMP_START(TRAMP_COMMON));
160asm (SIGTRAMP_BODY);
161asm (SIGTRAMP_END(TRAMP_COMMON));
162