xref: /qemu/linux-user/arm/signal.c (revision 709098fd)
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 struct target_sigcontext {
26     abi_ulong trap_no;
27     abi_ulong error_code;
28     abi_ulong oldmask;
29     abi_ulong arm_r0;
30     abi_ulong arm_r1;
31     abi_ulong arm_r2;
32     abi_ulong arm_r3;
33     abi_ulong arm_r4;
34     abi_ulong arm_r5;
35     abi_ulong arm_r6;
36     abi_ulong arm_r7;
37     abi_ulong arm_r8;
38     abi_ulong arm_r9;
39     abi_ulong arm_r10;
40     abi_ulong arm_fp;
41     abi_ulong arm_ip;
42     abi_ulong arm_sp;
43     abi_ulong arm_lr;
44     abi_ulong arm_pc;
45     abi_ulong arm_cpsr;
46     abi_ulong fault_address;
47 };
48 
49 struct target_ucontext_v1 {
50     abi_ulong tuc_flags;
51     abi_ulong tuc_link;
52     target_stack_t tuc_stack;
53     struct target_sigcontext tuc_mcontext;
54     target_sigset_t  tuc_sigmask;       /* mask last for extensibility */
55 };
56 
57 struct target_ucontext_v2 {
58     abi_ulong tuc_flags;
59     abi_ulong tuc_link;
60     target_stack_t tuc_stack;
61     struct target_sigcontext tuc_mcontext;
62     target_sigset_t  tuc_sigmask;       /* mask last for extensibility */
63     char __unused[128 - sizeof(target_sigset_t)];
64     abi_ulong tuc_regspace[128] __attribute__((__aligned__(8)));
65 };
66 
67 struct target_user_vfp {
68     uint64_t fpregs[32];
69     abi_ulong fpscr;
70 };
71 
72 struct target_user_vfp_exc {
73     abi_ulong fpexc;
74     abi_ulong fpinst;
75     abi_ulong fpinst2;
76 };
77 
78 struct target_vfp_sigframe {
79     abi_ulong magic;
80     abi_ulong size;
81     struct target_user_vfp ufp;
82     struct target_user_vfp_exc ufp_exc;
83 } __attribute__((__aligned__(8)));
84 
85 struct target_iwmmxt_sigframe {
86     abi_ulong magic;
87     abi_ulong size;
88     uint64_t regs[16];
89     /* Note that not all the coprocessor control registers are stored here */
90     uint32_t wcssf;
91     uint32_t wcasf;
92     uint32_t wcgr0;
93     uint32_t wcgr1;
94     uint32_t wcgr2;
95     uint32_t wcgr3;
96 } __attribute__((__aligned__(8)));
97 
98 #define TARGET_VFP_MAGIC 0x56465001
99 #define TARGET_IWMMXT_MAGIC 0x12ef842a
100 
101 struct sigframe_v1
102 {
103     struct target_sigcontext sc;
104     abi_ulong extramask[TARGET_NSIG_WORDS-1];
105     abi_ulong retcode[4];
106 };
107 
108 struct sigframe_v2
109 {
110     struct target_ucontext_v2 uc;
111     abi_ulong retcode[4];
112 };
113 
114 struct rt_sigframe_v1
115 {
116     abi_ulong pinfo;
117     abi_ulong puc;
118     struct target_siginfo info;
119     struct target_ucontext_v1 uc;
120     abi_ulong retcode[4];
121 };
122 
123 struct rt_sigframe_v2
124 {
125     struct target_siginfo info;
126     struct target_ucontext_v2 uc;
127     abi_ulong retcode[4];
128 };
129 
130 /*
131  * For ARM syscalls, we encode the syscall number into the instruction.
132  */
133 #define SWI_SYS_SIGRETURN       (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
134 #define SWI_SYS_RT_SIGRETURN    (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
135 
136 /*
137  * For Thumb syscalls, we pass the syscall number via r7.  We therefore
138  * need two 16-bit instructions.
139  */
140 #define SWI_THUMB_SIGRETURN     (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
141 #define SWI_THUMB_RT_SIGRETURN  (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
142 
143 static const abi_ulong retcodes[4] = {
144         SWI_SYS_SIGRETURN,      SWI_THUMB_SIGRETURN,
145         SWI_SYS_RT_SIGRETURN,   SWI_THUMB_RT_SIGRETURN
146 };
147 
148 /*
149  * Stub needed to make sure the FD register (r9) contains the right
150  * value.
151  */
152 static const unsigned long sigreturn_fdpic_codes[3] = {
153     0xe59fc004, /* ldr r12, [pc, #4] to read function descriptor */
154     0xe59c9004, /* ldr r9, [r12, #4] to setup GOT */
155     0xe59cf000  /* ldr pc, [r12] to jump into restorer */
156 };
157 
158 static const unsigned long sigreturn_fdpic_thumb_codes[3] = {
159     0xc008f8df, /* ldr r12, [pc, #8] to read function descriptor */
160     0x9004f8dc, /* ldr r9, [r12, #4] to setup GOT */
161     0xf000f8dc  /* ldr pc, [r12] to jump into restorer */
162 };
163 
164 static inline int valid_user_regs(CPUARMState *regs)
165 {
166     return 1;
167 }
168 
169 static void
170 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
171                  CPUARMState *env, abi_ulong mask)
172 {
173     __put_user(env->regs[0], &sc->arm_r0);
174     __put_user(env->regs[1], &sc->arm_r1);
175     __put_user(env->regs[2], &sc->arm_r2);
176     __put_user(env->regs[3], &sc->arm_r3);
177     __put_user(env->regs[4], &sc->arm_r4);
178     __put_user(env->regs[5], &sc->arm_r5);
179     __put_user(env->regs[6], &sc->arm_r6);
180     __put_user(env->regs[7], &sc->arm_r7);
181     __put_user(env->regs[8], &sc->arm_r8);
182     __put_user(env->regs[9], &sc->arm_r9);
183     __put_user(env->regs[10], &sc->arm_r10);
184     __put_user(env->regs[11], &sc->arm_fp);
185     __put_user(env->regs[12], &sc->arm_ip);
186     __put_user(env->regs[13], &sc->arm_sp);
187     __put_user(env->regs[14], &sc->arm_lr);
188     __put_user(env->regs[15], &sc->arm_pc);
189     __put_user(cpsr_read(env), &sc->arm_cpsr);
190 
191     __put_user(/* current->thread.trap_no */ 0, &sc->trap_no);
192     __put_user(/* current->thread.error_code */ 0, &sc->error_code);
193     __put_user(/* current->thread.address */ 0, &sc->fault_address);
194     __put_user(mask, &sc->oldmask);
195 }
196 
197 static inline abi_ulong
198 get_sigframe(struct target_sigaction *ka, CPUARMState *regs, int framesize)
199 {
200     unsigned long sp;
201 
202     sp = target_sigsp(get_sp_from_cpustate(regs), ka);
203     /*
204      * ATPCS B01 mandates 8-byte alignment
205      */
206     return (sp - framesize) & ~7;
207 }
208 
209 static int
210 setup_return(CPUARMState *env, struct target_sigaction *ka,
211              abi_ulong *rc, abi_ulong frame_addr, int usig, abi_ulong rc_addr)
212 {
213     abi_ulong handler = 0;
214     abi_ulong handler_fdpic_GOT = 0;
215     abi_ulong retcode;
216 
217     int thumb;
218     int is_fdpic = info_is_fdpic(((TaskState *)thread_cpu->opaque)->info);
219 
220     if (is_fdpic) {
221         /* In FDPIC mode, ka->_sa_handler points to a function
222          * descriptor (FD). The first word contains the address of the
223          * handler. The second word contains the value of the PIC
224          * register (r9).  */
225         abi_ulong funcdesc_ptr = ka->_sa_handler;
226         if (get_user_ual(handler, funcdesc_ptr)
227             || get_user_ual(handler_fdpic_GOT, funcdesc_ptr + 4)) {
228             return 1;
229         }
230     } else {
231         handler = ka->_sa_handler;
232     }
233 
234     thumb = handler & 1;
235 
236     uint32_t cpsr = cpsr_read(env);
237 
238     cpsr &= ~CPSR_IT;
239     if (thumb) {
240         cpsr |= CPSR_T;
241     } else {
242         cpsr &= ~CPSR_T;
243     }
244     if (env->cp15.sctlr_el[1] & SCTLR_E0E) {
245         cpsr |= CPSR_E;
246     } else {
247         cpsr &= ~CPSR_E;
248     }
249 
250     if (ka->sa_flags & TARGET_SA_RESTORER) {
251         if (is_fdpic) {
252             /* For FDPIC we ensure that the restorer is called with a
253              * correct r9 value.  For that we need to write code on
254              * the stack that sets r9 and jumps back to restorer
255              * value.
256              */
257             if (thumb) {
258                 __put_user(sigreturn_fdpic_thumb_codes[0], rc);
259                 __put_user(sigreturn_fdpic_thumb_codes[1], rc + 1);
260                 __put_user(sigreturn_fdpic_thumb_codes[2], rc + 2);
261                 __put_user((abi_ulong)ka->sa_restorer, rc + 3);
262             } else {
263                 __put_user(sigreturn_fdpic_codes[0], rc);
264                 __put_user(sigreturn_fdpic_codes[1], rc + 1);
265                 __put_user(sigreturn_fdpic_codes[2], rc + 2);
266                 __put_user((abi_ulong)ka->sa_restorer, rc + 3);
267             }
268 
269             retcode = rc_addr + thumb;
270         } else {
271             retcode = ka->sa_restorer;
272         }
273     } else {
274         unsigned int idx = thumb;
275 
276         if (ka->sa_flags & TARGET_SA_SIGINFO) {
277             idx += 2;
278         }
279 
280         __put_user(retcodes[idx], rc);
281 
282         retcode = rc_addr + thumb;
283     }
284 
285     env->regs[0] = usig;
286     if (is_fdpic) {
287         env->regs[9] = handler_fdpic_GOT;
288     }
289     env->regs[13] = frame_addr;
290     env->regs[14] = retcode;
291     env->regs[15] = handler & (thumb ? ~1 : ~3);
292     cpsr_write(env, cpsr, CPSR_IT | CPSR_T | CPSR_E, CPSRWriteByInstr);
293 
294     return 0;
295 }
296 
297 static abi_ulong *setup_sigframe_v2_vfp(abi_ulong *regspace, CPUARMState *env)
298 {
299     int i;
300     struct target_vfp_sigframe *vfpframe;
301     vfpframe = (struct target_vfp_sigframe *)regspace;
302     __put_user(TARGET_VFP_MAGIC, &vfpframe->magic);
303     __put_user(sizeof(*vfpframe), &vfpframe->size);
304     for (i = 0; i < 32; i++) {
305         __put_user(*aa32_vfp_dreg(env, i), &vfpframe->ufp.fpregs[i]);
306     }
307     __put_user(vfp_get_fpscr(env), &vfpframe->ufp.fpscr);
308     __put_user(env->vfp.xregs[ARM_VFP_FPEXC], &vfpframe->ufp_exc.fpexc);
309     __put_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
310     __put_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
311     return (abi_ulong*)(vfpframe+1);
312 }
313 
314 static abi_ulong *setup_sigframe_v2_iwmmxt(abi_ulong *regspace,
315                                            CPUARMState *env)
316 {
317     int i;
318     struct target_iwmmxt_sigframe *iwmmxtframe;
319     iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
320     __put_user(TARGET_IWMMXT_MAGIC, &iwmmxtframe->magic);
321     __put_user(sizeof(*iwmmxtframe), &iwmmxtframe->size);
322     for (i = 0; i < 16; i++) {
323         __put_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
324     }
325     __put_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
326     __put_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
327     __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
328     __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
329     __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
330     __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
331     return (abi_ulong*)(iwmmxtframe+1);
332 }
333 
334 static void setup_sigframe_v2(struct target_ucontext_v2 *uc,
335                               target_sigset_t *set, CPUARMState *env)
336 {
337     struct target_sigaltstack stack;
338     int i;
339     abi_ulong *regspace;
340 
341     /* Clear all the bits of the ucontext we don't use.  */
342     memset(uc, 0, offsetof(struct target_ucontext_v2, tuc_mcontext));
343 
344     memset(&stack, 0, sizeof(stack));
345     target_save_altstack(&stack, env);
346     memcpy(&uc->tuc_stack, &stack, sizeof(stack));
347 
348     setup_sigcontext(&uc->tuc_mcontext, env, set->sig[0]);
349     /* Save coprocessor signal frame.  */
350     regspace = uc->tuc_regspace;
351     if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
352         regspace = setup_sigframe_v2_vfp(regspace, env);
353     }
354     if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
355         regspace = setup_sigframe_v2_iwmmxt(regspace, env);
356     }
357 
358     /* Write terminating magic word */
359     __put_user(0, regspace);
360 
361     for(i = 0; i < TARGET_NSIG_WORDS; i++) {
362         __put_user(set->sig[i], &uc->tuc_sigmask.sig[i]);
363     }
364 }
365 
366 /* compare linux/arch/arm/kernel/signal.c:setup_frame() */
367 static void setup_frame_v1(int usig, struct target_sigaction *ka,
368                            target_sigset_t *set, CPUARMState *regs)
369 {
370     struct sigframe_v1 *frame;
371     abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
372     int i;
373 
374     trace_user_setup_frame(regs, frame_addr);
375     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
376         goto sigsegv;
377     }
378 
379     setup_sigcontext(&frame->sc, regs, set->sig[0]);
380 
381     for(i = 1; i < TARGET_NSIG_WORDS; i++) {
382         __put_user(set->sig[i], &frame->extramask[i - 1]);
383     }
384 
385     if (setup_return(regs, ka, frame->retcode, frame_addr, usig,
386                      frame_addr + offsetof(struct sigframe_v1, retcode))) {
387         goto sigsegv;
388     }
389 
390     unlock_user_struct(frame, frame_addr, 1);
391     return;
392 sigsegv:
393     unlock_user_struct(frame, frame_addr, 1);
394     force_sigsegv(usig);
395 }
396 
397 static void setup_frame_v2(int usig, struct target_sigaction *ka,
398                            target_sigset_t *set, CPUARMState *regs)
399 {
400     struct sigframe_v2 *frame;
401     abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
402 
403     trace_user_setup_frame(regs, frame_addr);
404     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
405         goto sigsegv;
406     }
407 
408     setup_sigframe_v2(&frame->uc, set, regs);
409 
410     if (setup_return(regs, ka, frame->retcode, frame_addr, usig,
411                      frame_addr + offsetof(struct sigframe_v2, retcode))) {
412         goto sigsegv;
413     }
414 
415     unlock_user_struct(frame, frame_addr, 1);
416     return;
417 sigsegv:
418     unlock_user_struct(frame, frame_addr, 1);
419     force_sigsegv(usig);
420 }
421 
422 void setup_frame(int usig, struct target_sigaction *ka,
423                  target_sigset_t *set, CPUARMState *regs)
424 {
425     if (get_osversion() >= 0x020612) {
426         setup_frame_v2(usig, ka, set, regs);
427     } else {
428         setup_frame_v1(usig, ka, set, regs);
429     }
430 }
431 
432 /* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
433 static void setup_rt_frame_v1(int usig, struct target_sigaction *ka,
434                               target_siginfo_t *info,
435                               target_sigset_t *set, CPUARMState *env)
436 {
437     struct rt_sigframe_v1 *frame;
438     abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
439     struct target_sigaltstack stack;
440     int i;
441     abi_ulong info_addr, uc_addr;
442 
443     trace_user_setup_rt_frame(env, frame_addr);
444     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
445         goto sigsegv;
446     }
447 
448     info_addr = frame_addr + offsetof(struct rt_sigframe_v1, info);
449     __put_user(info_addr, &frame->pinfo);
450     uc_addr = frame_addr + offsetof(struct rt_sigframe_v1, uc);
451     __put_user(uc_addr, &frame->puc);
452     tswap_siginfo(&frame->info, info);
453 
454     /* Clear all the bits of the ucontext we don't use.  */
455     memset(&frame->uc, 0, offsetof(struct target_ucontext_v1, tuc_mcontext));
456 
457     memset(&stack, 0, sizeof(stack));
458     target_save_altstack(&stack, env);
459     memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
460 
461     setup_sigcontext(&frame->uc.tuc_mcontext, env, set->sig[0]);
462     for(i = 0; i < TARGET_NSIG_WORDS; i++) {
463         __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
464     }
465 
466     if (setup_return(env, ka, frame->retcode, frame_addr, usig,
467                      frame_addr + offsetof(struct rt_sigframe_v1, retcode))) {
468         goto sigsegv;
469     }
470 
471     env->regs[1] = info_addr;
472     env->regs[2] = uc_addr;
473 
474     unlock_user_struct(frame, frame_addr, 1);
475     return;
476 sigsegv:
477     unlock_user_struct(frame, frame_addr, 1);
478     force_sigsegv(usig);
479 }
480 
481 static void setup_rt_frame_v2(int usig, struct target_sigaction *ka,
482                               target_siginfo_t *info,
483                               target_sigset_t *set, CPUARMState *env)
484 {
485     struct rt_sigframe_v2 *frame;
486     abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
487     abi_ulong info_addr, uc_addr;
488 
489     trace_user_setup_rt_frame(env, frame_addr);
490     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
491         goto sigsegv;
492     }
493 
494     info_addr = frame_addr + offsetof(struct rt_sigframe_v2, info);
495     uc_addr = frame_addr + offsetof(struct rt_sigframe_v2, uc);
496     tswap_siginfo(&frame->info, info);
497 
498     setup_sigframe_v2(&frame->uc, set, env);
499 
500     if (setup_return(env, ka, frame->retcode, frame_addr, usig,
501                      frame_addr + offsetof(struct rt_sigframe_v2, retcode))) {
502         goto sigsegv;
503     }
504 
505     env->regs[1] = info_addr;
506     env->regs[2] = uc_addr;
507 
508     unlock_user_struct(frame, frame_addr, 1);
509     return;
510 sigsegv:
511     unlock_user_struct(frame, frame_addr, 1);
512     force_sigsegv(usig);
513 }
514 
515 void setup_rt_frame(int usig, struct target_sigaction *ka,
516                     target_siginfo_t *info,
517                     target_sigset_t *set, CPUARMState *env)
518 {
519     if (get_osversion() >= 0x020612) {
520         setup_rt_frame_v2(usig, ka, info, set, env);
521     } else {
522         setup_rt_frame_v1(usig, ka, info, set, env);
523     }
524 }
525 
526 static int
527 restore_sigcontext(CPUARMState *env, struct target_sigcontext *sc)
528 {
529     int err = 0;
530     uint32_t cpsr;
531 
532     __get_user(env->regs[0], &sc->arm_r0);
533     __get_user(env->regs[1], &sc->arm_r1);
534     __get_user(env->regs[2], &sc->arm_r2);
535     __get_user(env->regs[3], &sc->arm_r3);
536     __get_user(env->regs[4], &sc->arm_r4);
537     __get_user(env->regs[5], &sc->arm_r5);
538     __get_user(env->regs[6], &sc->arm_r6);
539     __get_user(env->regs[7], &sc->arm_r7);
540     __get_user(env->regs[8], &sc->arm_r8);
541     __get_user(env->regs[9], &sc->arm_r9);
542     __get_user(env->regs[10], &sc->arm_r10);
543     __get_user(env->regs[11], &sc->arm_fp);
544     __get_user(env->regs[12], &sc->arm_ip);
545     __get_user(env->regs[13], &sc->arm_sp);
546     __get_user(env->regs[14], &sc->arm_lr);
547     __get_user(env->regs[15], &sc->arm_pc);
548     __get_user(cpsr, &sc->arm_cpsr);
549     cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC, CPSRWriteByInstr);
550 
551     err |= !valid_user_regs(env);
552 
553     return err;
554 }
555 
556 static long do_sigreturn_v1(CPUARMState *env)
557 {
558     abi_ulong frame_addr;
559     struct sigframe_v1 *frame = NULL;
560     target_sigset_t set;
561     sigset_t host_set;
562     int i;
563 
564     /*
565      * Since we stacked the signal on a 64-bit boundary,
566      * then 'sp' should be word aligned here.  If it's
567      * not, then the user is trying to mess with us.
568      */
569     frame_addr = env->regs[13];
570     trace_user_do_sigreturn(env, frame_addr);
571     if (frame_addr & 7) {
572         goto badframe;
573     }
574 
575     if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
576         goto badframe;
577     }
578 
579     __get_user(set.sig[0], &frame->sc.oldmask);
580     for(i = 1; i < TARGET_NSIG_WORDS; i++) {
581         __get_user(set.sig[i], &frame->extramask[i - 1]);
582     }
583 
584     target_to_host_sigset_internal(&host_set, &set);
585     set_sigmask(&host_set);
586 
587     if (restore_sigcontext(env, &frame->sc)) {
588         goto badframe;
589     }
590 
591 #if 0
592     /* Send SIGTRAP if we're single-stepping */
593     if (ptrace_cancel_bpt(current))
594         send_sig(SIGTRAP, current, 1);
595 #endif
596     unlock_user_struct(frame, frame_addr, 0);
597     return -TARGET_QEMU_ESIGRETURN;
598 
599 badframe:
600     force_sig(TARGET_SIGSEGV);
601     return -TARGET_QEMU_ESIGRETURN;
602 }
603 
604 static abi_ulong *restore_sigframe_v2_vfp(CPUARMState *env, abi_ulong *regspace)
605 {
606     int i;
607     abi_ulong magic, sz;
608     uint32_t fpscr, fpexc;
609     struct target_vfp_sigframe *vfpframe;
610     vfpframe = (struct target_vfp_sigframe *)regspace;
611 
612     __get_user(magic, &vfpframe->magic);
613     __get_user(sz, &vfpframe->size);
614     if (magic != TARGET_VFP_MAGIC || sz != sizeof(*vfpframe)) {
615         return 0;
616     }
617     for (i = 0; i < 32; i++) {
618         __get_user(*aa32_vfp_dreg(env, i), &vfpframe->ufp.fpregs[i]);
619     }
620     __get_user(fpscr, &vfpframe->ufp.fpscr);
621     vfp_set_fpscr(env, fpscr);
622     __get_user(fpexc, &vfpframe->ufp_exc.fpexc);
623     /* Sanitise FPEXC: ensure VFP is enabled, FPINST2 is invalid
624      * and the exception flag is cleared
625      */
626     fpexc |= (1 << 30);
627     fpexc &= ~((1 << 31) | (1 << 28));
628     env->vfp.xregs[ARM_VFP_FPEXC] = fpexc;
629     __get_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
630     __get_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
631     return (abi_ulong*)(vfpframe + 1);
632 }
633 
634 static abi_ulong *restore_sigframe_v2_iwmmxt(CPUARMState *env,
635                                              abi_ulong *regspace)
636 {
637     int i;
638     abi_ulong magic, sz;
639     struct target_iwmmxt_sigframe *iwmmxtframe;
640     iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
641 
642     __get_user(magic, &iwmmxtframe->magic);
643     __get_user(sz, &iwmmxtframe->size);
644     if (magic != TARGET_IWMMXT_MAGIC || sz != sizeof(*iwmmxtframe)) {
645         return 0;
646     }
647     for (i = 0; i < 16; i++) {
648         __get_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
649     }
650     __get_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
651     __get_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
652     __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
653     __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
654     __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
655     __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
656     return (abi_ulong*)(iwmmxtframe + 1);
657 }
658 
659 static int do_sigframe_return_v2(CPUARMState *env,
660                                  target_ulong context_addr,
661                                  struct target_ucontext_v2 *uc)
662 {
663     sigset_t host_set;
664     abi_ulong *regspace;
665 
666     target_to_host_sigset(&host_set, &uc->tuc_sigmask);
667     set_sigmask(&host_set);
668 
669     if (restore_sigcontext(env, &uc->tuc_mcontext))
670         return 1;
671 
672     /* Restore coprocessor signal frame */
673     regspace = uc->tuc_regspace;
674     if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
675         regspace = restore_sigframe_v2_vfp(env, regspace);
676         if (!regspace) {
677             return 1;
678         }
679     }
680     if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
681         regspace = restore_sigframe_v2_iwmmxt(env, regspace);
682         if (!regspace) {
683             return 1;
684         }
685     }
686 
687     target_restore_altstack(&uc->tuc_stack, env);
688 
689 #if 0
690     /* Send SIGTRAP if we're single-stepping */
691     if (ptrace_cancel_bpt(current))
692         send_sig(SIGTRAP, current, 1);
693 #endif
694 
695     return 0;
696 }
697 
698 static long do_sigreturn_v2(CPUARMState *env)
699 {
700     abi_ulong frame_addr;
701     struct sigframe_v2 *frame = NULL;
702 
703     /*
704      * Since we stacked the signal on a 64-bit boundary,
705      * then 'sp' should be word aligned here.  If it's
706      * not, then the user is trying to mess with us.
707      */
708     frame_addr = env->regs[13];
709     trace_user_do_sigreturn(env, frame_addr);
710     if (frame_addr & 7) {
711         goto badframe;
712     }
713 
714     if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
715         goto badframe;
716     }
717 
718     if (do_sigframe_return_v2(env,
719                               frame_addr
720                               + offsetof(struct sigframe_v2, uc),
721                               &frame->uc)) {
722         goto badframe;
723     }
724 
725     unlock_user_struct(frame, frame_addr, 0);
726     return -TARGET_QEMU_ESIGRETURN;
727 
728 badframe:
729     unlock_user_struct(frame, frame_addr, 0);
730     force_sig(TARGET_SIGSEGV);
731     return -TARGET_QEMU_ESIGRETURN;
732 }
733 
734 long do_sigreturn(CPUARMState *env)
735 {
736     if (get_osversion() >= 0x020612) {
737         return do_sigreturn_v2(env);
738     } else {
739         return do_sigreturn_v1(env);
740     }
741 }
742 
743 static long do_rt_sigreturn_v1(CPUARMState *env)
744 {
745     abi_ulong frame_addr;
746     struct rt_sigframe_v1 *frame = NULL;
747     sigset_t host_set;
748 
749     /*
750      * Since we stacked the signal on a 64-bit boundary,
751      * then 'sp' should be word aligned here.  If it's
752      * not, then the user is trying to mess with us.
753      */
754     frame_addr = env->regs[13];
755     trace_user_do_rt_sigreturn(env, frame_addr);
756     if (frame_addr & 7) {
757         goto badframe;
758     }
759 
760     if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
761         goto badframe;
762     }
763 
764     target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
765     set_sigmask(&host_set);
766 
767     if (restore_sigcontext(env, &frame->uc.tuc_mcontext)) {
768         goto badframe;
769     }
770 
771     target_restore_altstack(&frame->uc.tuc_stack, env);
772 
773 #if 0
774     /* Send SIGTRAP if we're single-stepping */
775     if (ptrace_cancel_bpt(current))
776         send_sig(SIGTRAP, current, 1);
777 #endif
778     unlock_user_struct(frame, frame_addr, 0);
779     return -TARGET_QEMU_ESIGRETURN;
780 
781 badframe:
782     unlock_user_struct(frame, frame_addr, 0);
783     force_sig(TARGET_SIGSEGV);
784     return -TARGET_QEMU_ESIGRETURN;
785 }
786 
787 static long do_rt_sigreturn_v2(CPUARMState *env)
788 {
789     abi_ulong frame_addr;
790     struct rt_sigframe_v2 *frame = NULL;
791 
792     /*
793      * Since we stacked the signal on a 64-bit boundary,
794      * then 'sp' should be word aligned here.  If it's
795      * not, then the user is trying to mess with us.
796      */
797     frame_addr = env->regs[13];
798     trace_user_do_rt_sigreturn(env, frame_addr);
799     if (frame_addr & 7) {
800         goto badframe;
801     }
802 
803     if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
804         goto badframe;
805     }
806 
807     if (do_sigframe_return_v2(env,
808                               frame_addr
809                               + offsetof(struct rt_sigframe_v2, uc),
810                               &frame->uc)) {
811         goto badframe;
812     }
813 
814     unlock_user_struct(frame, frame_addr, 0);
815     return -TARGET_QEMU_ESIGRETURN;
816 
817 badframe:
818     unlock_user_struct(frame, frame_addr, 0);
819     force_sig(TARGET_SIGSEGV);
820     return -TARGET_QEMU_ESIGRETURN;
821 }
822 
823 long do_rt_sigreturn(CPUARMState *env)
824 {
825     if (get_osversion() >= 0x020612) {
826         return do_rt_sigreturn_v2(env);
827     } else {
828         return do_rt_sigreturn_v1(env);
829     }
830 }
831