xref: /openbmc/qemu/linux-user/sparc/signal.c (revision 71cda6e9128d3f47634ebc8cda7125d5039e43ac)
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 "signal-common.h"
22 #include "linux-user/trace.h"
23 
24 /* A Sparc register window */
25 struct target_reg_window {
26     abi_ulong locals[8];
27     abi_ulong ins[8];
28 };
29 
30 /* A Sparc stack frame. */
31 struct target_stackf {
32     /*
33      * Since qemu does not reference fp or callers_pc directly,
34      * it's simpler to treat fp and callers_pc as elements of ins[],
35      * and then bundle locals[] and ins[] into reg_window.
36      */
37     struct target_reg_window win;
38     /*
39      * Similarly, bundle structptr and xxargs into xargs[].
40      * This portion of the struct is part of the function call abi,
41      * and belongs to the callee for spilling argument registers.
42      */
43     abi_ulong xargs[8];
44 };
45 
46 struct target_siginfo_fpu {
47     /* It is more convenient for qemu to move doubles, not singles. */
48     uint64_t si_double_regs[16];
49     uint32_t si_fsr;
50     uint32_t si_fpqdepth;
51     struct {
52         uint32_t insn_addr;
53         uint32_t insn;
54     } si_fpqueue [16];
55 };
56 
57 struct target_signal_frame {
58     struct target_stackf ss;
59     struct target_pt_regs regs;
60     uint32_t si_mask;
61     abi_ulong fpu_save;
62     uint32_t insns[2] QEMU_ALIGNED(8);
63     abi_ulong extramask[TARGET_NSIG_WORDS - 1];
64     abi_ulong extra_size; /* Should be 0 */
65 };
66 
67 static abi_ulong get_sigframe(struct target_sigaction *sa,
68                               CPUSPARCState *env,
69                               size_t framesize)
70 {
71     abi_ulong sp = get_sp_from_cpustate(env);
72 
73     /*
74      * If we are on the alternate signal stack and would overflow it, don't.
75      * Return an always-bogus address instead so we will die with SIGSEGV.
76      */
77     if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize))) {
78         return -1;
79     }
80 
81     /* This is the X/Open sanctioned signal stack switching.  */
82     sp = target_sigsp(sp, sa) - framesize;
83 
84     /*
85      * Always align the stack frame.  This handles two cases.  First,
86      * sigaltstack need not be mindful of platform specific stack
87      * alignment.  Second, if we took this signal because the stack
88      * is not aligned properly, we'd like to take the signal cleanly
89      * and report that.
90      */
91     sp &= ~15UL;
92 
93     return sp;
94 }
95 
96 static void save_pt_regs(struct target_pt_regs *regs, CPUSPARCState *env)
97 {
98     int i;
99 
100 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
101     __put_user(sparc64_tstate(env), &regs->tstate);
102     /* TODO: magic should contain PT_REG_MAGIC + %tt. */
103     __put_user(0, &regs->magic);
104 #else
105     __put_user(cpu_get_psr(env), &regs->psr);
106 #endif
107 
108     __put_user(env->pc, &regs->pc);
109     __put_user(env->npc, &regs->npc);
110     __put_user(env->y, &regs->y);
111 
112     for (i = 0; i < 8; i++) {
113         __put_user(env->gregs[i], &regs->u_regs[i]);
114     }
115     for (i = 0; i < 8; i++) {
116         __put_user(env->regwptr[WREG_O0 + i], &regs->u_regs[i + 8]);
117     }
118 }
119 
120 static void restore_pt_regs(struct target_pt_regs *regs, CPUSPARCState *env)
121 {
122     int i;
123 
124 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
125     /* User can only change condition codes and %asi in %tstate. */
126     uint64_t tstate;
127     __get_user(tstate, &regs->tstate);
128     cpu_put_ccr(env, tstate >> 32);
129     env->asi = extract64(tstate, 24, 8);
130 #else
131     /*
132      * User can only change condition codes and FPU enabling in %psr.
133      * But don't bother with FPU enabling, since a real kernel would
134      * just re-enable the FPU upon the next fpu trap.
135      */
136     uint32_t psr;
137     __get_user(psr, &regs->psr);
138     env->psr = (psr & PSR_ICC) | (env->psr & ~PSR_ICC);
139 #endif
140 
141     /* Note that pc and npc are handled in the caller. */
142 
143     __get_user(env->y, &regs->y);
144 
145     for (i = 0; i < 8; i++) {
146         __get_user(env->gregs[i], &regs->u_regs[i]);
147     }
148     for (i = 0; i < 8; i++) {
149         __get_user(env->regwptr[WREG_O0 + i], &regs->u_regs[i + 8]);
150     }
151 }
152 
153 static void save_reg_win(struct target_reg_window *win, CPUSPARCState *env)
154 {
155     int i;
156 
157     for (i = 0; i < 8; i++) {
158         __put_user(env->regwptr[i + WREG_L0], &win->locals[i]);
159     }
160     for (i = 0; i < 8; i++) {
161         __put_user(env->regwptr[i + WREG_I0], &win->ins[i]);
162     }
163 }
164 
165 static void save_fpu(struct target_siginfo_fpu *fpu, CPUSPARCState *env)
166 {
167     int i;
168 
169     for (i = 0; i < 16; ++i) {
170         __put_user(env->fpr[i].ll, &fpu->si_double_regs[i]);
171     }
172     __put_user(env->fsr, &fpu->si_fsr);
173     __put_user(0, &fpu->si_fpqdepth);
174 }
175 
176 static void restore_fpu(struct target_siginfo_fpu *fpu, CPUSPARCState *env)
177 {
178     int i;
179 
180     for (i = 0; i < 16; ++i) {
181         __get_user(env->fpr[i].ll, &fpu->si_double_regs[i]);
182     }
183     __get_user(env->fsr, &fpu->si_fsr);
184 }
185 
186 void setup_frame(int sig, struct target_sigaction *ka,
187                  target_sigset_t *set, CPUSPARCState *env)
188 {
189     abi_ulong sf_addr;
190     struct target_signal_frame *sf;
191     size_t sf_size = sizeof(*sf) + sizeof(struct target_siginfo_fpu);
192     int i;
193 
194     /* 1. Make sure everything is clean */
195 
196     sf_addr = get_sigframe(ka, env, sf_size);
197     trace_user_setup_frame(env, sf_addr);
198 
199     sf = lock_user(VERIFY_WRITE, sf_addr, sf_size, 0);
200     if (!sf) {
201         goto sigsegv;
202     }
203 
204     /* 2. Save the current process state */
205     save_pt_regs(&sf->regs, env);
206     __put_user(0, &sf->extra_size);
207 
208     save_fpu((struct target_siginfo_fpu *)(sf + 1), env);
209     __put_user(sf_addr + sizeof(*sf), &sf->fpu_save);
210 
211     __put_user(set->sig[0], &sf->si_mask);
212     for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
213         __put_user(set->sig[i + 1], &sf->extramask[i]);
214     }
215 
216     save_reg_win(&sf->ss.win, env);
217 
218     /* 3. signal handler back-trampoline and parameters */
219     env->regwptr[WREG_SP] = sf_addr;
220     env->regwptr[WREG_O0] = sig;
221     env->regwptr[WREG_O1] = sf_addr +
222             offsetof(struct target_signal_frame, regs);
223     env->regwptr[WREG_O2] = sf_addr +
224             offsetof(struct target_signal_frame, regs);
225 
226     /* 4. signal handler */
227     env->pc = ka->_sa_handler;
228     env->npc = (env->pc + 4);
229     /* 5. return to kernel instructions */
230     if (ka->ka_restorer) {
231         env->regwptr[WREG_O7] = ka->ka_restorer;
232     } else {
233         uint32_t val32;
234 
235         env->regwptr[WREG_O7] = sf_addr +
236                 offsetof(struct target_signal_frame, insns) - 2 * 4;
237 
238         /* mov __NR_sigreturn, %g1 */
239         val32 = 0x821020d8;
240         __put_user(val32, &sf->insns[0]);
241 
242         /* t 0x10 */
243         val32 = 0x91d02010;
244         __put_user(val32, &sf->insns[1]);
245     }
246     unlock_user(sf, sf_addr, sf_size);
247     return;
248 #if 0
249 sigill_and_return:
250     force_sig(TARGET_SIGILL);
251 #endif
252 sigsegv:
253     unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
254     force_sigsegv(sig);
255 }
256 
257 void setup_rt_frame(int sig, struct target_sigaction *ka,
258                     target_siginfo_t *info,
259                     target_sigset_t *set, CPUSPARCState *env)
260 {
261     qemu_log_mask(LOG_UNIMP, "setup_rt_frame: not implemented\n");
262 }
263 
264 long do_sigreturn(CPUSPARCState *env)
265 {
266     abi_ulong sf_addr;
267     struct target_signal_frame *sf;
268     abi_ulong pc, npc, ptr;
269     target_sigset_t set;
270     sigset_t host_set;
271     int i;
272 
273     sf_addr = env->regwptr[WREG_SP];
274     trace_user_do_sigreturn(env, sf_addr);
275     if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1)) {
276         goto segv_and_exit;
277     }
278 
279     /* 1. Make sure we are not getting garbage from the user */
280 
281     if (sf_addr & 3)
282         goto segv_and_exit;
283 
284     __get_user(pc,  &sf->regs.pc);
285     __get_user(npc, &sf->regs.npc);
286 
287     if ((pc | npc) & 3) {
288         goto segv_and_exit;
289     }
290 
291     /* 2. Restore the state */
292     restore_pt_regs(&sf->regs, env);
293     env->pc = pc;
294     env->npc = npc;
295 
296     __get_user(ptr, &sf->fpu_save);
297     if (ptr) {
298         struct target_siginfo_fpu *fpu;
299         if ((ptr & 3) || !lock_user_struct(VERIFY_READ, fpu, ptr, 1)) {
300             goto segv_and_exit;
301         }
302         restore_fpu(fpu, env);
303         unlock_user_struct(fpu, ptr, 0);
304     }
305 
306     __get_user(set.sig[0], &sf->si_mask);
307     for (i = 1; i < TARGET_NSIG_WORDS; i++) {
308         __get_user(set.sig[i], &sf->extramask[i - 1]);
309     }
310 
311     target_to_host_sigset_internal(&host_set, &set);
312     set_sigmask(&host_set);
313 
314     unlock_user_struct(sf, sf_addr, 0);
315     return -TARGET_QEMU_ESIGRETURN;
316 
317 segv_and_exit:
318     unlock_user_struct(sf, sf_addr, 0);
319     force_sig(TARGET_SIGSEGV);
320     return -TARGET_QEMU_ESIGRETURN;
321 }
322 
323 long do_rt_sigreturn(CPUSPARCState *env)
324 {
325     trace_user_do_rt_sigreturn(env, 0);
326     qemu_log_mask(LOG_UNIMP, "do_rt_sigreturn: not implemented\n");
327     return -TARGET_ENOSYS;
328 }
329 
330 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
331 #define SPARC_MC_TSTATE 0
332 #define SPARC_MC_PC 1
333 #define SPARC_MC_NPC 2
334 #define SPARC_MC_Y 3
335 #define SPARC_MC_G1 4
336 #define SPARC_MC_G2 5
337 #define SPARC_MC_G3 6
338 #define SPARC_MC_G4 7
339 #define SPARC_MC_G5 8
340 #define SPARC_MC_G6 9
341 #define SPARC_MC_G7 10
342 #define SPARC_MC_O0 11
343 #define SPARC_MC_O1 12
344 #define SPARC_MC_O2 13
345 #define SPARC_MC_O3 14
346 #define SPARC_MC_O4 15
347 #define SPARC_MC_O5 16
348 #define SPARC_MC_O6 17
349 #define SPARC_MC_O7 18
350 #define SPARC_MC_NGREG 19
351 
352 typedef abi_ulong target_mc_greg_t;
353 typedef target_mc_greg_t target_mc_gregset_t[SPARC_MC_NGREG];
354 
355 struct target_mc_fq {
356     abi_ulong mcfq_addr;
357     uint32_t mcfq_insn;
358 };
359 
360 /*
361  * Note the manual 16-alignment; the kernel gets this because it
362  * includes a "long double qregs[16]" in the mcpu_fregs union,
363  * which we can't do.
364  */
365 struct target_mc_fpu {
366     union {
367         uint32_t sregs[32];
368         uint64_t dregs[32];
369         //uint128_t qregs[16];
370     } mcfpu_fregs;
371     abi_ulong mcfpu_fsr;
372     abi_ulong mcfpu_fprs;
373     abi_ulong mcfpu_gsr;
374     abi_ulong mcfpu_fq;
375     unsigned char mcfpu_qcnt;
376     unsigned char mcfpu_qentsz;
377     unsigned char mcfpu_enab;
378 } __attribute__((aligned(16)));
379 typedef struct target_mc_fpu target_mc_fpu_t;
380 
381 typedef struct {
382     target_mc_gregset_t mc_gregs;
383     target_mc_greg_t mc_fp;
384     target_mc_greg_t mc_i7;
385     target_mc_fpu_t mc_fpregs;
386 } target_mcontext_t;
387 
388 struct target_ucontext {
389     abi_ulong tuc_link;
390     abi_ulong tuc_flags;
391     target_sigset_t tuc_sigmask;
392     target_mcontext_t tuc_mcontext;
393 };
394 
395 /* {set, get}context() needed for 64-bit SparcLinux userland. */
396 void sparc64_set_context(CPUSPARCState *env)
397 {
398     abi_ulong ucp_addr;
399     struct target_ucontext *ucp;
400     target_mc_gregset_t *grp;
401     target_mc_fpu_t *fpup;
402     abi_ulong pc, npc, tstate;
403     unsigned int i;
404     unsigned char fenab;
405 
406     ucp_addr = env->regwptr[WREG_O0];
407     if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1)) {
408         goto do_sigsegv;
409     }
410     grp  = &ucp->tuc_mcontext.mc_gregs;
411     __get_user(pc, &((*grp)[SPARC_MC_PC]));
412     __get_user(npc, &((*grp)[SPARC_MC_NPC]));
413     if ((pc | npc) & 3) {
414         goto do_sigsegv;
415     }
416     if (env->regwptr[WREG_O1]) {
417         target_sigset_t target_set;
418         sigset_t set;
419 
420         if (TARGET_NSIG_WORDS == 1) {
421             __get_user(target_set.sig[0], &ucp->tuc_sigmask.sig[0]);
422         } else {
423             abi_ulong *src, *dst;
424             src = ucp->tuc_sigmask.sig;
425             dst = target_set.sig;
426             for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
427                 __get_user(*dst, src);
428             }
429         }
430         target_to_host_sigset_internal(&set, &target_set);
431         set_sigmask(&set);
432     }
433     env->pc = pc;
434     env->npc = npc;
435     __get_user(env->y, &((*grp)[SPARC_MC_Y]));
436     __get_user(tstate, &((*grp)[SPARC_MC_TSTATE]));
437     /* Honour TSTATE_ASI, TSTATE_ICC and TSTATE_XCC only */
438     env->asi = (tstate >> 24) & 0xff;
439     cpu_put_ccr(env, (tstate >> 32) & 0xff);
440     __get_user(env->gregs[1], (&(*grp)[SPARC_MC_G1]));
441     __get_user(env->gregs[2], (&(*grp)[SPARC_MC_G2]));
442     __get_user(env->gregs[3], (&(*grp)[SPARC_MC_G3]));
443     __get_user(env->gregs[4], (&(*grp)[SPARC_MC_G4]));
444     __get_user(env->gregs[5], (&(*grp)[SPARC_MC_G5]));
445     __get_user(env->gregs[6], (&(*grp)[SPARC_MC_G6]));
446     /* Skip g7 as that's the thread register in userspace */
447 
448     /*
449      * Note that unlike the kernel, we didn't need to mess with the
450      * guest register window state to save it into a pt_regs to run
451      * the kernel. So for us the guest's O regs are still in WREG_O*
452      * (unlike the kernel which has put them in UREG_I* in a pt_regs)
453      * and the fp and i7 are still in WREG_I6 and WREG_I7 and don't
454      * need to be written back to userspace memory.
455      */
456     __get_user(env->regwptr[WREG_O0], (&(*grp)[SPARC_MC_O0]));
457     __get_user(env->regwptr[WREG_O1], (&(*grp)[SPARC_MC_O1]));
458     __get_user(env->regwptr[WREG_O2], (&(*grp)[SPARC_MC_O2]));
459     __get_user(env->regwptr[WREG_O3], (&(*grp)[SPARC_MC_O3]));
460     __get_user(env->regwptr[WREG_O4], (&(*grp)[SPARC_MC_O4]));
461     __get_user(env->regwptr[WREG_O5], (&(*grp)[SPARC_MC_O5]));
462     __get_user(env->regwptr[WREG_O6], (&(*grp)[SPARC_MC_O6]));
463     __get_user(env->regwptr[WREG_O7], (&(*grp)[SPARC_MC_O7]));
464 
465     __get_user(env->regwptr[WREG_FP], &(ucp->tuc_mcontext.mc_fp));
466     __get_user(env->regwptr[WREG_I7], &(ucp->tuc_mcontext.mc_i7));
467 
468     fpup = &ucp->tuc_mcontext.mc_fpregs;
469 
470     __get_user(fenab, &(fpup->mcfpu_enab));
471     if (fenab) {
472         abi_ulong fprs;
473 
474         /*
475          * We use the FPRS from the guest only in deciding whether
476          * to restore the upper, lower, or both banks of the FPU regs.
477          * The kernel here writes the FPU register data into the
478          * process's current_thread_info state and unconditionally
479          * clears FPRS and TSTATE_PEF: this disables the FPU so that the
480          * next FPU-disabled trap will copy the data out of
481          * current_thread_info and into the real FPU registers.
482          * QEMU doesn't need to handle lazy-FPU-state-restoring like that,
483          * so we always load the data directly into the FPU registers
484          * and leave FPRS and TSTATE_PEF alone (so the FPU stays enabled).
485          * Note that because we (and the kernel) always write zeroes for
486          * the fenab and fprs in sparc64_get_context() none of this code
487          * will execute unless the guest manually constructed or changed
488          * the context structure.
489          */
490         __get_user(fprs, &(fpup->mcfpu_fprs));
491         if (fprs & FPRS_DL) {
492             for (i = 0; i < 16; i++) {
493                 __get_user(env->fpr[i].ll, &(fpup->mcfpu_fregs.dregs[i]));
494             }
495         }
496         if (fprs & FPRS_DU) {
497             for (i = 16; i < 32; i++) {
498                 __get_user(env->fpr[i].ll, &(fpup->mcfpu_fregs.dregs[i]));
499             }
500         }
501         __get_user(env->fsr, &(fpup->mcfpu_fsr));
502         __get_user(env->gsr, &(fpup->mcfpu_gsr));
503     }
504     unlock_user_struct(ucp, ucp_addr, 0);
505     return;
506 do_sigsegv:
507     unlock_user_struct(ucp, ucp_addr, 0);
508     force_sig(TARGET_SIGSEGV);
509 }
510 
511 void sparc64_get_context(CPUSPARCState *env)
512 {
513     abi_ulong ucp_addr;
514     struct target_ucontext *ucp;
515     target_mc_gregset_t *grp;
516     target_mcontext_t *mcp;
517     int err;
518     unsigned int i;
519     target_sigset_t target_set;
520     sigset_t set;
521 
522     ucp_addr = env->regwptr[WREG_O0];
523     if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0)) {
524         goto do_sigsegv;
525     }
526 
527     memset(ucp, 0, sizeof(*ucp));
528 
529     mcp = &ucp->tuc_mcontext;
530     grp = &mcp->mc_gregs;
531 
532     /* Skip over the trap instruction, first. */
533     env->pc = env->npc;
534     env->npc += 4;
535 
536     /* If we're only reading the signal mask then do_sigprocmask()
537      * is guaranteed not to fail, which is important because we don't
538      * have any way to signal a failure or restart this operation since
539      * this is not a normal syscall.
540      */
541     err = do_sigprocmask(0, NULL, &set);
542     assert(err == 0);
543     host_to_target_sigset_internal(&target_set, &set);
544     if (TARGET_NSIG_WORDS == 1) {
545         __put_user(target_set.sig[0],
546                    (abi_ulong *)&ucp->tuc_sigmask);
547     } else {
548         abi_ulong *src, *dst;
549         src = target_set.sig;
550         dst = ucp->tuc_sigmask.sig;
551         for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
552             __put_user(*src, dst);
553         }
554     }
555 
556     __put_user(sparc64_tstate(env), &((*grp)[SPARC_MC_TSTATE]));
557     __put_user(env->pc, &((*grp)[SPARC_MC_PC]));
558     __put_user(env->npc, &((*grp)[SPARC_MC_NPC]));
559     __put_user(env->y, &((*grp)[SPARC_MC_Y]));
560     __put_user(env->gregs[1], &((*grp)[SPARC_MC_G1]));
561     __put_user(env->gregs[2], &((*grp)[SPARC_MC_G2]));
562     __put_user(env->gregs[3], &((*grp)[SPARC_MC_G3]));
563     __put_user(env->gregs[4], &((*grp)[SPARC_MC_G4]));
564     __put_user(env->gregs[5], &((*grp)[SPARC_MC_G5]));
565     __put_user(env->gregs[6], &((*grp)[SPARC_MC_G6]));
566     __put_user(env->gregs[7], &((*grp)[SPARC_MC_G7]));
567 
568     /*
569      * Note that unlike the kernel, we didn't need to mess with the
570      * guest register window state to save it into a pt_regs to run
571      * the kernel. So for us the guest's O regs are still in WREG_O*
572      * (unlike the kernel which has put them in UREG_I* in a pt_regs)
573      * and the fp and i7 are still in WREG_I6 and WREG_I7 and don't
574      * need to be fished out of userspace memory.
575      */
576     __put_user(env->regwptr[WREG_O0], &((*grp)[SPARC_MC_O0]));
577     __put_user(env->regwptr[WREG_O1], &((*grp)[SPARC_MC_O1]));
578     __put_user(env->regwptr[WREG_O2], &((*grp)[SPARC_MC_O2]));
579     __put_user(env->regwptr[WREG_O3], &((*grp)[SPARC_MC_O3]));
580     __put_user(env->regwptr[WREG_O4], &((*grp)[SPARC_MC_O4]));
581     __put_user(env->regwptr[WREG_O5], &((*grp)[SPARC_MC_O5]));
582     __put_user(env->regwptr[WREG_O6], &((*grp)[SPARC_MC_O6]));
583     __put_user(env->regwptr[WREG_O7], &((*grp)[SPARC_MC_O7]));
584 
585     __put_user(env->regwptr[WREG_FP], &(mcp->mc_fp));
586     __put_user(env->regwptr[WREG_I7], &(mcp->mc_i7));
587 
588     /*
589      * We don't write out the FPU state. This matches the kernel's
590      * implementation (which has the code for doing this but
591      * hidden behind an "if (fenab)" where fenab is always 0).
592      */
593 
594     unlock_user_struct(ucp, ucp_addr, 1);
595     return;
596 do_sigsegv:
597     unlock_user_struct(ucp, ucp_addr, 1);
598     force_sig(TARGET_SIGSEGV);
599 }
600 #endif
601