xref: /qemu/linux-user/mips/signal.c (revision 4f7a0a4c866fde385e65395f870d042c217d3c8b)
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 # if defined(TARGET_ABI_MIPSO32)
26 struct target_sigcontext {
27     uint32_t   sc_regmask;     /* Unused */
28     uint32_t   sc_status;
29     uint64_t   sc_pc;
30     uint64_t   sc_regs[32];
31     uint64_t   sc_fpregs[32];
32     uint32_t   sc_ownedfp;     /* Unused */
33     uint32_t   sc_fpc_csr;
34     uint32_t   sc_fpc_eir;     /* Unused */
35     uint32_t   sc_used_math;
36     uint32_t   sc_dsp;         /* dsp status, was sc_ssflags */
37     uint32_t   pad0;
38     uint64_t   sc_mdhi;
39     uint64_t   sc_mdlo;
40     target_ulong   sc_hi1;         /* Was sc_cause */
41     target_ulong   sc_lo1;         /* Was sc_badvaddr */
42     target_ulong   sc_hi2;         /* Was sc_sigset[4] */
43     target_ulong   sc_lo2;
44     target_ulong   sc_hi3;
45     target_ulong   sc_lo3;
46 };
47 # else /* N32 || N64 */
48 struct target_sigcontext {
49     uint64_t sc_regs[32];
50     uint64_t sc_fpregs[32];
51     uint64_t sc_mdhi;
52     uint64_t sc_hi1;
53     uint64_t sc_hi2;
54     uint64_t sc_hi3;
55     uint64_t sc_mdlo;
56     uint64_t sc_lo1;
57     uint64_t sc_lo2;
58     uint64_t sc_lo3;
59     uint64_t sc_pc;
60     uint32_t sc_fpc_csr;
61     uint32_t sc_used_math;
62     uint32_t sc_dsp;
63     uint32_t sc_reserved;
64 };
65 # endif /* O32 */
66 
67 struct sigframe {
68     uint32_t sf_ass[4];                 /* argument save space for o32 */
69     uint32_t sf_code[2];                        /* signal trampoline */
70     struct target_sigcontext sf_sc;
71     target_sigset_t sf_mask;
72 };
73 
74 struct target_ucontext {
75     abi_ulong tuc_flags;
76     abi_ulong tuc_link;
77     target_stack_t tuc_stack;
78     struct target_sigcontext tuc_mcontext;
79     target_sigset_t tuc_sigmask;
80 };
81 
82 struct target_rt_sigframe {
83     uint32_t rs_ass[4];               /* argument save space for o32 */
84     uint32_t rs_code[2];              /* signal trampoline */
85     struct target_siginfo rs_info;
86     struct target_ucontext rs_uc;
87 };
88 
89 /* Install trampoline to jump back from signal handler */
90 static void install_sigtramp(uint32_t *tramp, unsigned int syscall)
91 {
92     /*
93      * Set up the return code ...
94      *
95      *         li      v0, __NR__foo_sigreturn
96      *         syscall
97      */
98 
99     __put_user(0x24020000 + syscall, tramp + 0);
100     __put_user(0x0000000c          , tramp + 1);
101 }
102 
103 static inline void setup_sigcontext(CPUMIPSState *regs,
104                                     struct target_sigcontext *sc)
105 {
106     int i;
107 
108     __put_user(exception_resume_pc(regs), &sc->sc_pc);
109     regs->hflags &= ~MIPS_HFLAG_BMASK;
110 
111     __put_user(0, &sc->sc_regs[0]);
112     for (i = 1; i < 32; ++i) {
113         __put_user(regs->active_tc.gpr[i], &sc->sc_regs[i]);
114     }
115 
116     __put_user(regs->active_tc.HI[0], &sc->sc_mdhi);
117     __put_user(regs->active_tc.LO[0], &sc->sc_mdlo);
118 
119     /* Rather than checking for dsp existence, always copy.  The storage
120        would just be garbage otherwise.  */
121     __put_user(regs->active_tc.HI[1], &sc->sc_hi1);
122     __put_user(regs->active_tc.HI[2], &sc->sc_hi2);
123     __put_user(regs->active_tc.HI[3], &sc->sc_hi3);
124     __put_user(regs->active_tc.LO[1], &sc->sc_lo1);
125     __put_user(regs->active_tc.LO[2], &sc->sc_lo2);
126     __put_user(regs->active_tc.LO[3], &sc->sc_lo3);
127     {
128         uint32_t dsp = cpu_rddsp(0x3ff, regs);
129         __put_user(dsp, &sc->sc_dsp);
130     }
131 
132     __put_user(1, &sc->sc_used_math);
133 
134     for (i = 0; i < 32; ++i) {
135         __put_user(regs->active_fpu.fpr[i].d, &sc->sc_fpregs[i]);
136     }
137 }
138 
139 static inline void
140 restore_sigcontext(CPUMIPSState *regs, struct target_sigcontext *sc)
141 {
142     int i;
143 
144     __get_user(regs->CP0_EPC, &sc->sc_pc);
145 
146     __get_user(regs->active_tc.HI[0], &sc->sc_mdhi);
147     __get_user(regs->active_tc.LO[0], &sc->sc_mdlo);
148 
149     for (i = 1; i < 32; ++i) {
150         __get_user(regs->active_tc.gpr[i], &sc->sc_regs[i]);
151     }
152 
153     __get_user(regs->active_tc.HI[1], &sc->sc_hi1);
154     __get_user(regs->active_tc.HI[2], &sc->sc_hi2);
155     __get_user(regs->active_tc.HI[3], &sc->sc_hi3);
156     __get_user(regs->active_tc.LO[1], &sc->sc_lo1);
157     __get_user(regs->active_tc.LO[2], &sc->sc_lo2);
158     __get_user(regs->active_tc.LO[3], &sc->sc_lo3);
159     {
160         uint32_t dsp;
161         __get_user(dsp, &sc->sc_dsp);
162         cpu_wrdsp(dsp, 0x3ff, regs);
163     }
164 
165     for (i = 0; i < 32; ++i) {
166         __get_user(regs->active_fpu.fpr[i].d, &sc->sc_fpregs[i]);
167     }
168 }
169 
170 /*
171  * Determine which stack to use..
172  */
173 static inline abi_ulong
174 get_sigframe(struct target_sigaction *ka, CPUMIPSState *regs, size_t frame_size)
175 {
176     unsigned long sp;
177 
178     /*
179      * FPU emulator may have its own trampoline active just
180      * above the user stack, 16-bytes before the next lowest
181      * 16 byte boundary.  Try to avoid trashing it.
182      */
183     sp = target_sigsp(get_sp_from_cpustate(regs) - 32, ka);
184 
185     return (sp - frame_size) & ~7;
186 }
187 
188 static void mips_set_hflags_isa_mode_from_pc(CPUMIPSState *env)
189 {
190     if (env->insn_flags & (ASE_MIPS16 | ASE_MICROMIPS)) {
191         env->hflags &= ~MIPS_HFLAG_M16;
192         env->hflags |= (env->active_tc.PC & 1) << MIPS_HFLAG_M16_SHIFT;
193         env->active_tc.PC &= ~(target_ulong) 1;
194     }
195 }
196 
197 # if defined(TARGET_ABI_MIPSO32)
198 /* compare linux/arch/mips/kernel/signal.c:setup_frame() */
199 void setup_frame(int sig, struct target_sigaction * ka,
200                  target_sigset_t *set, CPUMIPSState *regs)
201 {
202     struct sigframe *frame;
203     abi_ulong frame_addr;
204     int i;
205 
206     frame_addr = get_sigframe(ka, regs, sizeof(*frame));
207     trace_user_setup_frame(regs, frame_addr);
208     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
209         goto give_sigsegv;
210     }
211 
212     install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
213 
214     setup_sigcontext(regs, &frame->sf_sc);
215 
216     for(i = 0; i < TARGET_NSIG_WORDS; i++) {
217         __put_user(set->sig[i], &frame->sf_mask.sig[i]);
218     }
219 
220     /*
221     * Arguments to signal handler:
222     *
223     *   a0 = signal number
224     *   a1 = 0 (should be cause)
225     *   a2 = pointer to struct sigcontext
226     *
227     * $25 and PC point to the signal handler, $29 points to the
228     * struct sigframe.
229     */
230     regs->active_tc.gpr[ 4] = sig;
231     regs->active_tc.gpr[ 5] = 0;
232     regs->active_tc.gpr[ 6] = frame_addr + offsetof(struct sigframe, sf_sc);
233     regs->active_tc.gpr[29] = frame_addr;
234     regs->active_tc.gpr[31] = frame_addr + offsetof(struct sigframe, sf_code);
235     /* The original kernel code sets CP0_EPC to the handler
236     * since it returns to userland using eret
237     * we cannot do this here, and we must set PC directly */
238     regs->active_tc.PC = regs->active_tc.gpr[25] = ka->_sa_handler;
239     mips_set_hflags_isa_mode_from_pc(regs);
240     unlock_user_struct(frame, frame_addr, 1);
241     return;
242 
243 give_sigsegv:
244     force_sigsegv(sig);
245 }
246 
247 long do_sigreturn(CPUMIPSState *regs)
248 {
249     struct sigframe *frame;
250     abi_ulong frame_addr;
251     sigset_t blocked;
252     target_sigset_t target_set;
253     int i;
254 
255     frame_addr = regs->active_tc.gpr[29];
256     trace_user_do_sigreturn(regs, frame_addr);
257     if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
258         goto badframe;
259 
260     for(i = 0; i < TARGET_NSIG_WORDS; i++) {
261         __get_user(target_set.sig[i], &frame->sf_mask.sig[i]);
262     }
263 
264     target_to_host_sigset_internal(&blocked, &target_set);
265     set_sigmask(&blocked);
266 
267     restore_sigcontext(regs, &frame->sf_sc);
268 
269 #if 0
270     /*
271      * Don't let your children do this ...
272      */
273     __asm__ __volatile__(
274         "move\t$29, %0\n\t"
275         "j\tsyscall_exit"
276         :/* no outputs */
277         :"r" (&regs));
278     /* Unreached */
279 #endif
280 
281     regs->active_tc.PC = regs->CP0_EPC;
282     mips_set_hflags_isa_mode_from_pc(regs);
283     /* I am not sure this is right, but it seems to work
284     * maybe a problem with nested signals ? */
285     regs->CP0_EPC = 0;
286     return -TARGET_QEMU_ESIGRETURN;
287 
288 badframe:
289     force_sig(TARGET_SIGSEGV);
290     return -TARGET_QEMU_ESIGRETURN;
291 }
292 # endif /* O32 */
293 
294 void setup_rt_frame(int sig, struct target_sigaction *ka,
295                     target_siginfo_t *info,
296                     target_sigset_t *set, CPUMIPSState *env)
297 {
298     struct target_rt_sigframe *frame;
299     abi_ulong frame_addr;
300     int i;
301 
302     frame_addr = get_sigframe(ka, env, sizeof(*frame));
303     trace_user_setup_rt_frame(env, frame_addr);
304     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
305         goto give_sigsegv;
306     }
307 
308     install_sigtramp(frame->rs_code, TARGET_NR_rt_sigreturn);
309 
310     tswap_siginfo(&frame->rs_info, info);
311 
312     __put_user(0, &frame->rs_uc.tuc_flags);
313     __put_user(0, &frame->rs_uc.tuc_link);
314     target_save_altstack(&frame->rs_uc.tuc_stack, env);
315 
316     setup_sigcontext(env, &frame->rs_uc.tuc_mcontext);
317 
318     for(i = 0; i < TARGET_NSIG_WORDS; i++) {
319         __put_user(set->sig[i], &frame->rs_uc.tuc_sigmask.sig[i]);
320     }
321 
322     /*
323     * Arguments to signal handler:
324     *
325     *   a0 = signal number
326     *   a1 = pointer to siginfo_t
327     *   a2 = pointer to ucontext_t
328     *
329     * $25 and PC point to the signal handler, $29 points to the
330     * struct sigframe.
331     */
332     env->active_tc.gpr[ 4] = sig;
333     env->active_tc.gpr[ 5] = frame_addr
334                              + offsetof(struct target_rt_sigframe, rs_info);
335     env->active_tc.gpr[ 6] = frame_addr
336                              + offsetof(struct target_rt_sigframe, rs_uc);
337     env->active_tc.gpr[29] = frame_addr;
338     env->active_tc.gpr[31] = frame_addr
339                              + offsetof(struct target_rt_sigframe, rs_code);
340     /* The original kernel code sets CP0_EPC to the handler
341     * since it returns to userland using eret
342     * we cannot do this here, and we must set PC directly */
343     env->active_tc.PC = env->active_tc.gpr[25] = ka->_sa_handler;
344     mips_set_hflags_isa_mode_from_pc(env);
345     unlock_user_struct(frame, frame_addr, 1);
346     return;
347 
348 give_sigsegv:
349     unlock_user_struct(frame, frame_addr, 1);
350     force_sigsegv(sig);
351 }
352 
353 long do_rt_sigreturn(CPUMIPSState *env)
354 {
355     struct target_rt_sigframe *frame;
356     abi_ulong frame_addr;
357     sigset_t blocked;
358 
359     frame_addr = env->active_tc.gpr[29];
360     trace_user_do_rt_sigreturn(env, frame_addr);
361     if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
362         goto badframe;
363     }
364 
365     target_to_host_sigset(&blocked, &frame->rs_uc.tuc_sigmask);
366     set_sigmask(&blocked);
367 
368     restore_sigcontext(env, &frame->rs_uc.tuc_mcontext);
369     target_restore_altstack(&frame->rs_uc.tuc_stack, env);
370 
371     env->active_tc.PC = env->CP0_EPC;
372     mips_set_hflags_isa_mode_from_pc(env);
373     /* I am not sure this is right, but it seems to work
374     * maybe a problem with nested signals ? */
375     env->CP0_EPC = 0;
376     return -TARGET_QEMU_ESIGRETURN;
377 
378 badframe:
379     force_sig(TARGET_SIGSEGV);
380     return -TARGET_QEMU_ESIGRETURN;
381 }
382