xref: /openbmc/qemu/linux-user/sparc/signal.c (revision 44a5f861718caeb6f7b1ac7a6c279d32fc84041a)
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 typedef struct {
47     abi_ulong  si_float_regs[32];
48     unsigned   long si_fsr;
49     unsigned   long si_fpqdepth;
50     struct {
51         unsigned long *insn_addr;
52         unsigned long insn;
53     } si_fpqueue [16];
54 } qemu_siginfo_fpu_t;
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     qemu_siginfo_fpu_t fpu_state;
66 };
67 
68 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
69                                      CPUSPARCState *env,
70                                      unsigned long framesize)
71 {
72     abi_ulong sp = get_sp_from_cpustate(env);
73 
74     /*
75      * If we are on the alternate signal stack and would overflow it, don't.
76      * Return an always-bogus address instead so we will die with SIGSEGV.
77          */
78     if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize))) {
79             return -1;
80     }
81 
82     /* This is the X/Open sanctioned signal stack switching.  */
83     sp = target_sigsp(sp, sa) - framesize;
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 #define NF_ALIGNEDSZ  (((sizeof(struct target_signal_frame) + 7) & (~7)))
166 
167 void setup_frame(int sig, struct target_sigaction *ka,
168                  target_sigset_t *set, CPUSPARCState *env)
169 {
170     abi_ulong sf_addr;
171     struct target_signal_frame *sf;
172     int sigframe_size, i;
173 
174     /* 1. Make sure everything is clean */
175     //synchronize_user_stack();
176 
177     sigframe_size = NF_ALIGNEDSZ;
178     sf_addr = get_sigframe(ka, env, sigframe_size);
179     trace_user_setup_frame(env, sf_addr);
180 
181     sf = lock_user(VERIFY_WRITE, sf_addr,
182                    sizeof(struct target_signal_frame), 0);
183     if (!sf) {
184         goto sigsegv;
185     }
186     /* 2. Save the current process state */
187     save_pt_regs(&sf->regs, env);
188     __put_user(0, &sf->extra_size);
189 
190     //save_fpu_state(regs, &sf->fpu_state);
191     //__put_user(&sf->fpu_state, &sf->fpu_save);
192 
193     __put_user(set->sig[0], &sf->si_mask);
194     for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
195         __put_user(set->sig[i + 1], &sf->extramask[i]);
196     }
197 
198     save_reg_win(&sf->ss.win, env);
199 
200     /* 3. signal handler back-trampoline and parameters */
201     env->regwptr[WREG_SP] = sf_addr;
202     env->regwptr[WREG_O0] = sig;
203     env->regwptr[WREG_O1] = sf_addr +
204             offsetof(struct target_signal_frame, regs);
205     env->regwptr[WREG_O2] = sf_addr +
206             offsetof(struct target_signal_frame, regs);
207 
208     /* 4. signal handler */
209     env->pc = ka->_sa_handler;
210     env->npc = (env->pc + 4);
211     /* 5. return to kernel instructions */
212     if (ka->ka_restorer) {
213         env->regwptr[WREG_O7] = ka->ka_restorer;
214     } else {
215         uint32_t val32;
216 
217         env->regwptr[WREG_O7] = sf_addr +
218                 offsetof(struct target_signal_frame, insns) - 2 * 4;
219 
220         /* mov __NR_sigreturn, %g1 */
221         val32 = 0x821020d8;
222         __put_user(val32, &sf->insns[0]);
223 
224         /* t 0x10 */
225         val32 = 0x91d02010;
226         __put_user(val32, &sf->insns[1]);
227     }
228     unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
229     return;
230 #if 0
231 sigill_and_return:
232     force_sig(TARGET_SIGILL);
233 #endif
234 sigsegv:
235     unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
236     force_sigsegv(sig);
237 }
238 
239 void setup_rt_frame(int sig, struct target_sigaction *ka,
240                     target_siginfo_t *info,
241                     target_sigset_t *set, CPUSPARCState *env)
242 {
243     qemu_log_mask(LOG_UNIMP, "setup_rt_frame: not implemented\n");
244 }
245 
246 long do_sigreturn(CPUSPARCState *env)
247 {
248     abi_ulong sf_addr;
249     struct target_signal_frame *sf;
250     abi_ulong pc, npc;
251     target_sigset_t set;
252     sigset_t host_set;
253     int i;
254 
255     sf_addr = env->regwptr[WREG_SP];
256     trace_user_do_sigreturn(env, sf_addr);
257     if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1)) {
258         goto segv_and_exit;
259     }
260 
261     /* 1. Make sure we are not getting garbage from the user */
262 
263     if (sf_addr & 3)
264         goto segv_and_exit;
265 
266     __get_user(pc,  &sf->regs.pc);
267     __get_user(npc, &sf->regs.npc);
268 
269     if ((pc | npc) & 3) {
270         goto segv_and_exit;
271     }
272 
273     /* 2. Restore the state */
274     restore_pt_regs(&sf->regs, env);
275     env->pc = pc;
276     env->npc = npc;
277 
278     /* FIXME: implement FPU save/restore:
279      * __get_user(fpu_save, &sf->fpu_save);
280      * if (fpu_save) {
281      *     if (restore_fpu_state(env, fpu_save)) {
282      *         goto segv_and_exit;
283      *     }
284      * }
285      */
286 
287     __get_user(set.sig[0], &sf->si_mask);
288     for (i = 1; i < TARGET_NSIG_WORDS; i++) {
289         __get_user(set.sig[i], &sf->extramask[i - 1]);
290     }
291 
292     target_to_host_sigset_internal(&host_set, &set);
293     set_sigmask(&host_set);
294 
295     unlock_user_struct(sf, sf_addr, 0);
296     return -TARGET_QEMU_ESIGRETURN;
297 
298 segv_and_exit:
299     unlock_user_struct(sf, sf_addr, 0);
300     force_sig(TARGET_SIGSEGV);
301     return -TARGET_QEMU_ESIGRETURN;
302 }
303 
304 long do_rt_sigreturn(CPUSPARCState *env)
305 {
306     trace_user_do_rt_sigreturn(env, 0);
307     qemu_log_mask(LOG_UNIMP, "do_rt_sigreturn: not implemented\n");
308     return -TARGET_ENOSYS;
309 }
310 
311 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
312 #define SPARC_MC_TSTATE 0
313 #define SPARC_MC_PC 1
314 #define SPARC_MC_NPC 2
315 #define SPARC_MC_Y 3
316 #define SPARC_MC_G1 4
317 #define SPARC_MC_G2 5
318 #define SPARC_MC_G3 6
319 #define SPARC_MC_G4 7
320 #define SPARC_MC_G5 8
321 #define SPARC_MC_G6 9
322 #define SPARC_MC_G7 10
323 #define SPARC_MC_O0 11
324 #define SPARC_MC_O1 12
325 #define SPARC_MC_O2 13
326 #define SPARC_MC_O3 14
327 #define SPARC_MC_O4 15
328 #define SPARC_MC_O5 16
329 #define SPARC_MC_O6 17
330 #define SPARC_MC_O7 18
331 #define SPARC_MC_NGREG 19
332 
333 typedef abi_ulong target_mc_greg_t;
334 typedef target_mc_greg_t target_mc_gregset_t[SPARC_MC_NGREG];
335 
336 struct target_mc_fq {
337     abi_ulong mcfq_addr;
338     uint32_t mcfq_insn;
339 };
340 
341 /*
342  * Note the manual 16-alignment; the kernel gets this because it
343  * includes a "long double qregs[16]" in the mcpu_fregs union,
344  * which we can't do.
345  */
346 struct target_mc_fpu {
347     union {
348         uint32_t sregs[32];
349         uint64_t dregs[32];
350         //uint128_t qregs[16];
351     } mcfpu_fregs;
352     abi_ulong mcfpu_fsr;
353     abi_ulong mcfpu_fprs;
354     abi_ulong mcfpu_gsr;
355     abi_ulong mcfpu_fq;
356     unsigned char mcfpu_qcnt;
357     unsigned char mcfpu_qentsz;
358     unsigned char mcfpu_enab;
359 } __attribute__((aligned(16)));
360 typedef struct target_mc_fpu target_mc_fpu_t;
361 
362 typedef struct {
363     target_mc_gregset_t mc_gregs;
364     target_mc_greg_t mc_fp;
365     target_mc_greg_t mc_i7;
366     target_mc_fpu_t mc_fpregs;
367 } target_mcontext_t;
368 
369 struct target_ucontext {
370     abi_ulong tuc_link;
371     abi_ulong tuc_flags;
372     target_sigset_t tuc_sigmask;
373     target_mcontext_t tuc_mcontext;
374 };
375 
376 /* {set, get}context() needed for 64-bit SparcLinux userland. */
377 void sparc64_set_context(CPUSPARCState *env)
378 {
379     abi_ulong ucp_addr;
380     struct target_ucontext *ucp;
381     target_mc_gregset_t *grp;
382     target_mc_fpu_t *fpup;
383     abi_ulong pc, npc, tstate;
384     unsigned int i;
385     unsigned char fenab;
386 
387     ucp_addr = env->regwptr[WREG_O0];
388     if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1)) {
389         goto do_sigsegv;
390     }
391     grp  = &ucp->tuc_mcontext.mc_gregs;
392     __get_user(pc, &((*grp)[SPARC_MC_PC]));
393     __get_user(npc, &((*grp)[SPARC_MC_NPC]));
394     if ((pc | npc) & 3) {
395         goto do_sigsegv;
396     }
397     if (env->regwptr[WREG_O1]) {
398         target_sigset_t target_set;
399         sigset_t set;
400 
401         if (TARGET_NSIG_WORDS == 1) {
402             __get_user(target_set.sig[0], &ucp->tuc_sigmask.sig[0]);
403         } else {
404             abi_ulong *src, *dst;
405             src = ucp->tuc_sigmask.sig;
406             dst = target_set.sig;
407             for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
408                 __get_user(*dst, src);
409             }
410         }
411         target_to_host_sigset_internal(&set, &target_set);
412         set_sigmask(&set);
413     }
414     env->pc = pc;
415     env->npc = npc;
416     __get_user(env->y, &((*grp)[SPARC_MC_Y]));
417     __get_user(tstate, &((*grp)[SPARC_MC_TSTATE]));
418     /* Honour TSTATE_ASI, TSTATE_ICC and TSTATE_XCC only */
419     env->asi = (tstate >> 24) & 0xff;
420     cpu_put_ccr(env, (tstate >> 32) & 0xff);
421     __get_user(env->gregs[1], (&(*grp)[SPARC_MC_G1]));
422     __get_user(env->gregs[2], (&(*grp)[SPARC_MC_G2]));
423     __get_user(env->gregs[3], (&(*grp)[SPARC_MC_G3]));
424     __get_user(env->gregs[4], (&(*grp)[SPARC_MC_G4]));
425     __get_user(env->gregs[5], (&(*grp)[SPARC_MC_G5]));
426     __get_user(env->gregs[6], (&(*grp)[SPARC_MC_G6]));
427     /* Skip g7 as that's the thread register in userspace */
428 
429     /*
430      * Note that unlike the kernel, we didn't need to mess with the
431      * guest register window state to save it into a pt_regs to run
432      * the kernel. So for us the guest's O regs are still in WREG_O*
433      * (unlike the kernel which has put them in UREG_I* in a pt_regs)
434      * and the fp and i7 are still in WREG_I6 and WREG_I7 and don't
435      * need to be written back to userspace memory.
436      */
437     __get_user(env->regwptr[WREG_O0], (&(*grp)[SPARC_MC_O0]));
438     __get_user(env->regwptr[WREG_O1], (&(*grp)[SPARC_MC_O1]));
439     __get_user(env->regwptr[WREG_O2], (&(*grp)[SPARC_MC_O2]));
440     __get_user(env->regwptr[WREG_O3], (&(*grp)[SPARC_MC_O3]));
441     __get_user(env->regwptr[WREG_O4], (&(*grp)[SPARC_MC_O4]));
442     __get_user(env->regwptr[WREG_O5], (&(*grp)[SPARC_MC_O5]));
443     __get_user(env->regwptr[WREG_O6], (&(*grp)[SPARC_MC_O6]));
444     __get_user(env->regwptr[WREG_O7], (&(*grp)[SPARC_MC_O7]));
445 
446     __get_user(env->regwptr[WREG_FP], &(ucp->tuc_mcontext.mc_fp));
447     __get_user(env->regwptr[WREG_I7], &(ucp->tuc_mcontext.mc_i7));
448 
449     fpup = &ucp->tuc_mcontext.mc_fpregs;
450 
451     __get_user(fenab, &(fpup->mcfpu_enab));
452     if (fenab) {
453         abi_ulong fprs;
454 
455         /*
456          * We use the FPRS from the guest only in deciding whether
457          * to restore the upper, lower, or both banks of the FPU regs.
458          * The kernel here writes the FPU register data into the
459          * process's current_thread_info state and unconditionally
460          * clears FPRS and TSTATE_PEF: this disables the FPU so that the
461          * next FPU-disabled trap will copy the data out of
462          * current_thread_info and into the real FPU registers.
463          * QEMU doesn't need to handle lazy-FPU-state-restoring like that,
464          * so we always load the data directly into the FPU registers
465          * and leave FPRS and TSTATE_PEF alone (so the FPU stays enabled).
466          * Note that because we (and the kernel) always write zeroes for
467          * the fenab and fprs in sparc64_get_context() none of this code
468          * will execute unless the guest manually constructed or changed
469          * the context structure.
470          */
471         __get_user(fprs, &(fpup->mcfpu_fprs));
472         if (fprs & FPRS_DL) {
473             for (i = 0; i < 16; i++) {
474                 __get_user(env->fpr[i].ll, &(fpup->mcfpu_fregs.dregs[i]));
475             }
476         }
477         if (fprs & FPRS_DU) {
478             for (i = 16; i < 32; i++) {
479                 __get_user(env->fpr[i].ll, &(fpup->mcfpu_fregs.dregs[i]));
480             }
481         }
482         __get_user(env->fsr, &(fpup->mcfpu_fsr));
483         __get_user(env->gsr, &(fpup->mcfpu_gsr));
484     }
485     unlock_user_struct(ucp, ucp_addr, 0);
486     return;
487 do_sigsegv:
488     unlock_user_struct(ucp, ucp_addr, 0);
489     force_sig(TARGET_SIGSEGV);
490 }
491 
492 void sparc64_get_context(CPUSPARCState *env)
493 {
494     abi_ulong ucp_addr;
495     struct target_ucontext *ucp;
496     target_mc_gregset_t *grp;
497     target_mcontext_t *mcp;
498     int err;
499     unsigned int i;
500     target_sigset_t target_set;
501     sigset_t set;
502 
503     ucp_addr = env->regwptr[WREG_O0];
504     if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0)) {
505         goto do_sigsegv;
506     }
507 
508     memset(ucp, 0, sizeof(*ucp));
509 
510     mcp = &ucp->tuc_mcontext;
511     grp = &mcp->mc_gregs;
512 
513     /* Skip over the trap instruction, first. */
514     env->pc = env->npc;
515     env->npc += 4;
516 
517     /* If we're only reading the signal mask then do_sigprocmask()
518      * is guaranteed not to fail, which is important because we don't
519      * have any way to signal a failure or restart this operation since
520      * this is not a normal syscall.
521      */
522     err = do_sigprocmask(0, NULL, &set);
523     assert(err == 0);
524     host_to_target_sigset_internal(&target_set, &set);
525     if (TARGET_NSIG_WORDS == 1) {
526         __put_user(target_set.sig[0],
527                    (abi_ulong *)&ucp->tuc_sigmask);
528     } else {
529         abi_ulong *src, *dst;
530         src = target_set.sig;
531         dst = ucp->tuc_sigmask.sig;
532         for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
533             __put_user(*src, dst);
534         }
535     }
536 
537     __put_user(sparc64_tstate(env), &((*grp)[SPARC_MC_TSTATE]));
538     __put_user(env->pc, &((*grp)[SPARC_MC_PC]));
539     __put_user(env->npc, &((*grp)[SPARC_MC_NPC]));
540     __put_user(env->y, &((*grp)[SPARC_MC_Y]));
541     __put_user(env->gregs[1], &((*grp)[SPARC_MC_G1]));
542     __put_user(env->gregs[2], &((*grp)[SPARC_MC_G2]));
543     __put_user(env->gregs[3], &((*grp)[SPARC_MC_G3]));
544     __put_user(env->gregs[4], &((*grp)[SPARC_MC_G4]));
545     __put_user(env->gregs[5], &((*grp)[SPARC_MC_G5]));
546     __put_user(env->gregs[6], &((*grp)[SPARC_MC_G6]));
547     __put_user(env->gregs[7], &((*grp)[SPARC_MC_G7]));
548 
549     /*
550      * Note that unlike the kernel, we didn't need to mess with the
551      * guest register window state to save it into a pt_regs to run
552      * the kernel. So for us the guest's O regs are still in WREG_O*
553      * (unlike the kernel which has put them in UREG_I* in a pt_regs)
554      * and the fp and i7 are still in WREG_I6 and WREG_I7 and don't
555      * need to be fished out of userspace memory.
556      */
557     __put_user(env->regwptr[WREG_O0], &((*grp)[SPARC_MC_O0]));
558     __put_user(env->regwptr[WREG_O1], &((*grp)[SPARC_MC_O1]));
559     __put_user(env->regwptr[WREG_O2], &((*grp)[SPARC_MC_O2]));
560     __put_user(env->regwptr[WREG_O3], &((*grp)[SPARC_MC_O3]));
561     __put_user(env->regwptr[WREG_O4], &((*grp)[SPARC_MC_O4]));
562     __put_user(env->regwptr[WREG_O5], &((*grp)[SPARC_MC_O5]));
563     __put_user(env->regwptr[WREG_O6], &((*grp)[SPARC_MC_O6]));
564     __put_user(env->regwptr[WREG_O7], &((*grp)[SPARC_MC_O7]));
565 
566     __put_user(env->regwptr[WREG_FP], &(mcp->mc_fp));
567     __put_user(env->regwptr[WREG_I7], &(mcp->mc_i7));
568 
569     /*
570      * We don't write out the FPU state. This matches the kernel's
571      * implementation (which has the code for doing this but
572      * hidden behind an "if (fenab)" where fenab is always 0).
573      */
574 
575     unlock_user_struct(ucp, ucp_addr, 1);
576     return;
577 do_sigsegv:
578     unlock_user_struct(ucp, ucp_addr, 1);
579     force_sig(TARGET_SIGSEGV);
580 }
581 #endif
582