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