xref: /openbmc/qemu/linux-user/sh4/signal.c (revision 30b6852c)
1 /*
2  *  Emulation of Linux signals
3  *
4  *  Copyright (c) 2003 Fabrice Bellard
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19 #include "qemu/osdep.h"
20 #include "qemu.h"
21 #include "user-internals.h"
22 #include "signal-common.h"
23 #include "linux-user/trace.h"
24 
25 /*
26  * code and data structures from linux kernel:
27  * include/asm-sh/sigcontext.h
28  * arch/sh/kernel/signal.c
29  */
30 
31 struct target_sigcontext {
32     target_ulong  oldmask;
33 
34     /* CPU registers */
35     target_ulong  sc_gregs[16];
36     target_ulong  sc_pc;
37     target_ulong  sc_pr;
38     target_ulong  sc_sr;
39     target_ulong  sc_gbr;
40     target_ulong  sc_mach;
41     target_ulong  sc_macl;
42 
43     /* FPU registers */
44     target_ulong  sc_fpregs[16];
45     target_ulong  sc_xfpregs[16];
46     unsigned int sc_fpscr;
47     unsigned int sc_fpul;
48     unsigned int sc_ownedfp;
49 };
50 
51 struct target_sigframe
52 {
53     struct target_sigcontext sc;
54     target_ulong extramask[TARGET_NSIG_WORDS-1];
55 };
56 
57 
58 struct target_ucontext {
59     target_ulong tuc_flags;
60     struct target_ucontext *tuc_link;
61     target_stack_t tuc_stack;
62     struct target_sigcontext tuc_mcontext;
63     target_sigset_t tuc_sigmask;        /* mask last for extensibility */
64 };
65 
66 struct target_rt_sigframe
67 {
68     struct target_siginfo info;
69     struct target_ucontext uc;
70 };
71 
72 
73 #define MOVW(n)  (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
74 #define TRAP_NOARG 0xc310         /* Syscall w/no args (NR in R3) SH3/4 */
75 
76 static abi_ulong get_sigframe(struct target_sigaction *ka,
77                               unsigned long sp, size_t frame_size)
78 {
79     sp = target_sigsp(sp, ka);
80 
81     return (sp - frame_size) & -8ul;
82 }
83 
84 /*
85  * Notice when we're in the middle of a gUSA region and reset.
86  * Note that this will only occur when #CF_PARALLEL is unset, as we
87  * will translate such sequences differently in a parallel context.
88  */
89 static void unwind_gusa(CPUSH4State *regs)
90 {
91     /* If the stack pointer is sufficiently negative, and we haven't
92        completed the sequence, then reset to the entry to the region.  */
93     /* ??? The SH4 kernel checks for and address above 0xC0000000.
94        However, the page mappings in qemu linux-user aren't as restricted
95        and we wind up with the normal stack mapped above 0xF0000000.
96        That said, there is no reason why the kernel should be allowing
97        a gUSA region that spans 1GB.  Use a tighter check here, for what
98        can actually be enabled by the immediate move.  */
99     if (regs->gregs[15] >= -128u && regs->pc < regs->gregs[0]) {
100         /* Reset the PC to before the gUSA region, as computed from
101            R0 = region end, SP = -(region size), plus one more for the
102            insn that actually initializes SP to the region size.  */
103         regs->pc = regs->gregs[0] + regs->gregs[15] - 2;
104 
105         /* Reset the SP to the saved version in R1.  */
106         regs->gregs[15] = regs->gregs[1];
107     }
108 }
109 
110 static void setup_sigcontext(struct target_sigcontext *sc,
111                              CPUSH4State *regs, unsigned long mask)
112 {
113     int i;
114 
115 #define COPY(x)         __put_user(regs->x, &sc->sc_##x)
116     COPY(gregs[0]); COPY(gregs[1]);
117     COPY(gregs[2]); COPY(gregs[3]);
118     COPY(gregs[4]); COPY(gregs[5]);
119     COPY(gregs[6]); COPY(gregs[7]);
120     COPY(gregs[8]); COPY(gregs[9]);
121     COPY(gregs[10]); COPY(gregs[11]);
122     COPY(gregs[12]); COPY(gregs[13]);
123     COPY(gregs[14]); COPY(gregs[15]);
124     COPY(gbr); COPY(mach);
125     COPY(macl); COPY(pr);
126     COPY(sr); COPY(pc);
127 #undef COPY
128 
129     for (i=0; i<16; i++) {
130         __put_user(regs->fregs[i], &sc->sc_fpregs[i]);
131     }
132     __put_user(regs->fpscr, &sc->sc_fpscr);
133     __put_user(regs->fpul, &sc->sc_fpul);
134 
135     /* non-iBCS2 extensions.. */
136     __put_user(mask, &sc->oldmask);
137 }
138 
139 static void restore_sigcontext(CPUSH4State *regs, struct target_sigcontext *sc)
140 {
141     int i;
142 
143 #define COPY(x)         __get_user(regs->x, &sc->sc_##x)
144     COPY(gregs[0]); COPY(gregs[1]);
145     COPY(gregs[2]); COPY(gregs[3]);
146     COPY(gregs[4]); COPY(gregs[5]);
147     COPY(gregs[6]); COPY(gregs[7]);
148     COPY(gregs[8]); COPY(gregs[9]);
149     COPY(gregs[10]); COPY(gregs[11]);
150     COPY(gregs[12]); COPY(gregs[13]);
151     COPY(gregs[14]); COPY(gregs[15]);
152     COPY(gbr); COPY(mach);
153     COPY(macl); COPY(pr);
154     COPY(sr); COPY(pc);
155 #undef COPY
156 
157     for (i=0; i<16; i++) {
158         __get_user(regs->fregs[i], &sc->sc_fpregs[i]);
159     }
160     __get_user(regs->fpscr, &sc->sc_fpscr);
161     __get_user(regs->fpul, &sc->sc_fpul);
162 
163     regs->tra = -1;         /* disable syscall checks */
164     regs->flags &= ~(DELAY_SLOT_MASK | GUSA_MASK);
165 }
166 
167 void setup_frame(int sig, struct target_sigaction *ka,
168                  target_sigset_t *set, CPUSH4State *regs)
169 {
170     struct target_sigframe *frame;
171     abi_ulong frame_addr;
172     int i;
173 
174     unwind_gusa(regs);
175 
176     frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
177     trace_user_setup_frame(regs, frame_addr);
178     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
179         goto give_sigsegv;
180     }
181 
182     setup_sigcontext(&frame->sc, regs, set->sig[0]);
183 
184     for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
185         __put_user(set->sig[i + 1], &frame->extramask[i]);
186     }
187 
188     /* Set up to return from userspace.  If provided, use a stub
189        already in userspace.  */
190     if (ka->sa_flags & TARGET_SA_RESTORER) {
191         regs->pr = ka->sa_restorer;
192     } else {
193         regs->pr = default_sigreturn;
194     }
195 
196     /* Set up registers for signal handler */
197     regs->gregs[15] = frame_addr;
198     regs->gregs[4] = sig; /* Arg for signal handler */
199     regs->gregs[5] = 0;
200     regs->gregs[6] = frame_addr += offsetof(typeof(*frame), sc);
201     regs->pc = (unsigned long) ka->_sa_handler;
202     regs->flags &= ~(DELAY_SLOT_MASK | GUSA_MASK);
203 
204     unlock_user_struct(frame, frame_addr, 1);
205     return;
206 
207 give_sigsegv:
208     unlock_user_struct(frame, frame_addr, 1);
209     force_sigsegv(sig);
210 }
211 
212 void setup_rt_frame(int sig, struct target_sigaction *ka,
213                     target_siginfo_t *info,
214                     target_sigset_t *set, CPUSH4State *regs)
215 {
216     struct target_rt_sigframe *frame;
217     abi_ulong frame_addr;
218     int i;
219 
220     unwind_gusa(regs);
221 
222     frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
223     trace_user_setup_rt_frame(regs, frame_addr);
224     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
225         goto give_sigsegv;
226     }
227 
228     tswap_siginfo(&frame->info, info);
229 
230     /* Create the ucontext.  */
231     __put_user(0, &frame->uc.tuc_flags);
232     __put_user(0, (unsigned long *)&frame->uc.tuc_link);
233     target_save_altstack(&frame->uc.tuc_stack, regs);
234     setup_sigcontext(&frame->uc.tuc_mcontext,
235                      regs, set->sig[0]);
236     for(i = 0; i < TARGET_NSIG_WORDS; i++) {
237         __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
238     }
239 
240     /* Set up to return from userspace.  If provided, use a stub
241        already in userspace.  */
242     if (ka->sa_flags & TARGET_SA_RESTORER) {
243         regs->pr = ka->sa_restorer;
244     } else {
245         regs->pr = default_rt_sigreturn;
246     }
247 
248     /* Set up registers for signal handler */
249     regs->gregs[15] = frame_addr;
250     regs->gregs[4] = sig; /* Arg for signal handler */
251     regs->gregs[5] = frame_addr + offsetof(typeof(*frame), info);
252     regs->gregs[6] = frame_addr + offsetof(typeof(*frame), uc);
253     regs->pc = (unsigned long) ka->_sa_handler;
254     regs->flags &= ~(DELAY_SLOT_MASK | GUSA_MASK);
255 
256     unlock_user_struct(frame, frame_addr, 1);
257     return;
258 
259 give_sigsegv:
260     unlock_user_struct(frame, frame_addr, 1);
261     force_sigsegv(sig);
262 }
263 
264 long do_sigreturn(CPUSH4State *regs)
265 {
266     struct target_sigframe *frame;
267     abi_ulong frame_addr;
268     sigset_t blocked;
269     target_sigset_t target_set;
270     int i;
271 
272     frame_addr = regs->gregs[15];
273     trace_user_do_sigreturn(regs, frame_addr);
274     if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
275         goto badframe;
276     }
277 
278     __get_user(target_set.sig[0], &frame->sc.oldmask);
279     for(i = 1; i < TARGET_NSIG_WORDS; i++) {
280         __get_user(target_set.sig[i], &frame->extramask[i - 1]);
281     }
282 
283     target_to_host_sigset_internal(&blocked, &target_set);
284     set_sigmask(&blocked);
285 
286     restore_sigcontext(regs, &frame->sc);
287 
288     unlock_user_struct(frame, frame_addr, 0);
289     return -TARGET_QEMU_ESIGRETURN;
290 
291 badframe:
292     unlock_user_struct(frame, frame_addr, 0);
293     force_sig(TARGET_SIGSEGV);
294     return -TARGET_QEMU_ESIGRETURN;
295 }
296 
297 long do_rt_sigreturn(CPUSH4State *regs)
298 {
299     struct target_rt_sigframe *frame;
300     abi_ulong frame_addr;
301     sigset_t blocked;
302 
303     frame_addr = regs->gregs[15];
304     trace_user_do_rt_sigreturn(regs, frame_addr);
305     if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
306         goto badframe;
307     }
308 
309     target_to_host_sigset(&blocked, &frame->uc.tuc_sigmask);
310     set_sigmask(&blocked);
311 
312     restore_sigcontext(regs, &frame->uc.tuc_mcontext);
313     target_restore_altstack(&frame->uc.tuc_stack, regs);
314 
315     unlock_user_struct(frame, frame_addr, 0);
316     return -TARGET_QEMU_ESIGRETURN;
317 
318 badframe:
319     unlock_user_struct(frame, frame_addr, 0);
320     force_sig(TARGET_SIGSEGV);
321     return -TARGET_QEMU_ESIGRETURN;
322 }
323 
324 void setup_sigtramp(abi_ulong sigtramp_page)
325 {
326     uint16_t *tramp = lock_user(VERIFY_WRITE, sigtramp_page, 2 * 6, 0);
327     assert(tramp != NULL);
328 
329     default_sigreturn = sigtramp_page;
330     __put_user(MOVW(2), &tramp[0]);
331     __put_user(TRAP_NOARG, &tramp[1]);
332     __put_user(TARGET_NR_sigreturn, &tramp[2]);
333 
334     default_rt_sigreturn = sigtramp_page + 6;
335     __put_user(MOVW(2), &tramp[3]);
336     __put_user(TRAP_NOARG, &tramp[4]);
337     __put_user(TARGET_NR_rt_sigreturn, &tramp[5]);
338 
339     unlock_user(tramp, sigtramp_page, 2 * 6);
340 }
341