xref: /openbmc/qemu/linux-user/arm/signal.c (revision a9f495b9)
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 #include "vdso-asmoffset.h"
25 
26 struct target_sigcontext {
27     abi_ulong trap_no;
28     abi_ulong error_code;
29     abi_ulong oldmask;
30     abi_ulong arm_r0;
31     abi_ulong arm_r1;
32     abi_ulong arm_r2;
33     abi_ulong arm_r3;
34     abi_ulong arm_r4;
35     abi_ulong arm_r5;
36     abi_ulong arm_r6;
37     abi_ulong arm_r7;
38     abi_ulong arm_r8;
39     abi_ulong arm_r9;
40     abi_ulong arm_r10;
41     abi_ulong arm_fp;
42     abi_ulong arm_ip;
43     abi_ulong arm_sp;
44     abi_ulong arm_lr;
45     abi_ulong arm_pc;
46     abi_ulong arm_cpsr;
47     abi_ulong fault_address;
48 };
49 
50 struct target_ucontext {
51     abi_ulong tuc_flags;
52     abi_ulong tuc_link;
53     target_stack_t tuc_stack;
54     struct target_sigcontext tuc_mcontext;
55     target_sigset_t  tuc_sigmask;       /* mask last for extensibility */
56     char __unused[128 - sizeof(target_sigset_t)];
57     abi_ulong tuc_regspace[128] __attribute__((__aligned__(8)));
58 };
59 
60 struct target_user_vfp {
61     uint64_t fpregs[32];
62     abi_ulong fpscr;
63 };
64 
65 struct target_user_vfp_exc {
66     abi_ulong fpexc;
67     abi_ulong fpinst;
68     abi_ulong fpinst2;
69 };
70 
71 struct target_vfp_sigframe {
72     abi_ulong magic;
73     abi_ulong size;
74     struct target_user_vfp ufp;
75     struct target_user_vfp_exc ufp_exc;
76 } __attribute__((__aligned__(8)));
77 
78 struct target_iwmmxt_sigframe {
79     abi_ulong magic;
80     abi_ulong size;
81     uint64_t regs[16];
82     /* Note that not all the coprocessor control registers are stored here */
83     uint32_t wcssf;
84     uint32_t wcasf;
85     uint32_t wcgr0;
86     uint32_t wcgr1;
87     uint32_t wcgr2;
88     uint32_t wcgr3;
89 } __attribute__((__aligned__(8)));
90 
91 #define TARGET_VFP_MAGIC 0x56465001
92 #define TARGET_IWMMXT_MAGIC 0x12ef842a
93 
94 struct sigframe
95 {
96     struct target_ucontext uc;
97     abi_ulong retcode[4];
98 };
99 
100 struct rt_sigframe
101 {
102     struct target_siginfo info;
103     struct sigframe sig;
104 };
105 
106 QEMU_BUILD_BUG_ON(offsetof(struct sigframe, retcode[3])
107                   != SIGFRAME_RC3_OFFSET);
108 QEMU_BUILD_BUG_ON(offsetof(struct rt_sigframe, sig.retcode[3])
109                   != RT_SIGFRAME_RC3_OFFSET);
110 
111 static abi_ptr sigreturn_fdpic_tramp;
112 
113 /*
114  * Up to 3 words of 'retcode' in the sigframe are code,
115  * with retcode[3] being used by fdpic for the function descriptor.
116  * This code is not actually executed, but is retained for ABI compat.
117  *
118  * We will create a table of 8 retcode variants in the sigtramp page.
119  * Let each table entry use 3 words.
120  */
121 #define RETCODE_WORDS  3
122 #define RETCODE_BYTES  (RETCODE_WORDS * 4)
123 
124 static inline int valid_user_regs(CPUARMState *regs)
125 {
126     return 1;
127 }
128 
129 static void
130 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
131                  CPUARMState *env, abi_ulong mask)
132 {
133     __put_user(env->regs[0], &sc->arm_r0);
134     __put_user(env->regs[1], &sc->arm_r1);
135     __put_user(env->regs[2], &sc->arm_r2);
136     __put_user(env->regs[3], &sc->arm_r3);
137     __put_user(env->regs[4], &sc->arm_r4);
138     __put_user(env->regs[5], &sc->arm_r5);
139     __put_user(env->regs[6], &sc->arm_r6);
140     __put_user(env->regs[7], &sc->arm_r7);
141     __put_user(env->regs[8], &sc->arm_r8);
142     __put_user(env->regs[9], &sc->arm_r9);
143     __put_user(env->regs[10], &sc->arm_r10);
144     __put_user(env->regs[11], &sc->arm_fp);
145     __put_user(env->regs[12], &sc->arm_ip);
146     __put_user(env->regs[13], &sc->arm_sp);
147     __put_user(env->regs[14], &sc->arm_lr);
148     __put_user(env->regs[15], &sc->arm_pc);
149     __put_user(cpsr_read(env), &sc->arm_cpsr);
150 
151     __put_user(/* current->thread.trap_no */ 0, &sc->trap_no);
152     __put_user(/* current->thread.error_code */ 0, &sc->error_code);
153     __put_user(/* current->thread.address */ 0, &sc->fault_address);
154     __put_user(mask, &sc->oldmask);
155 }
156 
157 static inline abi_ulong
158 get_sigframe(struct target_sigaction *ka, CPUARMState *regs, int framesize)
159 {
160     unsigned long sp;
161 
162     sp = target_sigsp(get_sp_from_cpustate(regs), ka);
163     /*
164      * ATPCS B01 mandates 8-byte alignment
165      */
166     return (sp - framesize) & ~7;
167 }
168 
169 static void write_arm_sigreturn(uint32_t *rc, int syscall);
170 static void write_arm_fdpic_sigreturn(uint32_t *rc, int ofs);
171 
172 static int
173 setup_return(CPUARMState *env, struct target_sigaction *ka, int usig,
174              struct sigframe *frame, abi_ulong sp_addr)
175 {
176     abi_ulong handler = 0;
177     abi_ulong handler_fdpic_GOT = 0;
178     abi_ulong retcode;
179     bool is_fdpic = info_is_fdpic(((TaskState *)thread_cpu->opaque)->info);
180     bool is_rt = ka->sa_flags & TARGET_SA_SIGINFO;
181     bool thumb;
182 
183     if (is_fdpic) {
184         /* In FDPIC mode, ka->_sa_handler points to a function
185          * descriptor (FD). The first word contains the address of the
186          * handler. The second word contains the value of the PIC
187          * register (r9).  */
188         abi_ulong funcdesc_ptr = ka->_sa_handler;
189         if (get_user_ual(handler, funcdesc_ptr)
190             || get_user_ual(handler_fdpic_GOT, funcdesc_ptr + 4)) {
191             return 1;
192         }
193     } else {
194         handler = ka->_sa_handler;
195     }
196     thumb = handler & 1;
197 
198     uint32_t cpsr = cpsr_read(env);
199 
200     cpsr &= ~CPSR_IT;
201     if (thumb) {
202         cpsr |= CPSR_T;
203     } else {
204         cpsr &= ~CPSR_T;
205     }
206     if (env->cp15.sctlr_el[1] & SCTLR_E0E) {
207         cpsr |= CPSR_E;
208     } else {
209         cpsr &= ~CPSR_E;
210     }
211 
212     /* Our vdso default_sigreturn label is a table of entry points. */
213     retcode = default_sigreturn + (is_fdpic * 2 + is_rt) * 8;
214 
215     /*
216      * Put the sigreturn code on the stack no matter which return
217      * mechanism we use in order to remain ABI compliant.
218      * Because this is about ABI, always use the A32 instructions,
219      * despite the fact that our actual vdso trampoline is T16.
220      */
221     if (is_fdpic) {
222         write_arm_fdpic_sigreturn(frame->retcode,
223                                   is_rt ? RT_SIGFRAME_RC3_OFFSET
224                                         : SIGFRAME_RC3_OFFSET);
225     } else {
226         write_arm_sigreturn(frame->retcode,
227                             is_rt ? TARGET_NR_rt_sigreturn
228                                   : TARGET_NR_sigreturn);
229     }
230 
231     if (ka->sa_flags & TARGET_SA_RESTORER) {
232         if (is_fdpic) {
233             /* Place the function descriptor in slot 3. */
234             __put_user((abi_ulong)ka->sa_restorer, &frame->retcode[3]);
235         } else {
236             retcode = ka->sa_restorer;
237         }
238     }
239 
240     env->regs[0] = usig;
241     if (is_fdpic) {
242         env->regs[9] = handler_fdpic_GOT;
243     }
244     env->regs[13] = sp_addr;
245     env->regs[14] = retcode;
246     env->regs[15] = handler & (thumb ? ~1 : ~3);
247     cpsr_write(env, cpsr, CPSR_IT | CPSR_T | CPSR_E, CPSRWriteByInstr);
248 
249     return 0;
250 }
251 
252 static abi_ulong *setup_sigframe_vfp(abi_ulong *regspace, CPUARMState *env)
253 {
254     int i;
255     struct target_vfp_sigframe *vfpframe;
256     vfpframe = (struct target_vfp_sigframe *)regspace;
257     __put_user(TARGET_VFP_MAGIC, &vfpframe->magic);
258     __put_user(sizeof(*vfpframe), &vfpframe->size);
259     for (i = 0; i < 32; i++) {
260         __put_user(*aa32_vfp_dreg(env, i), &vfpframe->ufp.fpregs[i]);
261     }
262     __put_user(vfp_get_fpscr(env), &vfpframe->ufp.fpscr);
263     __put_user(env->vfp.xregs[ARM_VFP_FPEXC], &vfpframe->ufp_exc.fpexc);
264     __put_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
265     __put_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
266     return (abi_ulong*)(vfpframe+1);
267 }
268 
269 static abi_ulong *setup_sigframe_iwmmxt(abi_ulong *regspace, CPUARMState *env)
270 {
271     int i;
272     struct target_iwmmxt_sigframe *iwmmxtframe;
273     iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
274     __put_user(TARGET_IWMMXT_MAGIC, &iwmmxtframe->magic);
275     __put_user(sizeof(*iwmmxtframe), &iwmmxtframe->size);
276     for (i = 0; i < 16; i++) {
277         __put_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
278     }
279     __put_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
280     __put_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
281     __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
282     __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
283     __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
284     __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
285     return (abi_ulong*)(iwmmxtframe+1);
286 }
287 
288 static void setup_sigframe(struct target_ucontext *uc,
289                            target_sigset_t *set, CPUARMState *env)
290 {
291     struct target_sigaltstack stack;
292     int i;
293     abi_ulong *regspace;
294 
295     /* Clear all the bits of the ucontext we don't use.  */
296     memset(uc, 0, offsetof(struct target_ucontext, tuc_mcontext));
297 
298     memset(&stack, 0, sizeof(stack));
299     target_save_altstack(&stack, env);
300     memcpy(&uc->tuc_stack, &stack, sizeof(stack));
301 
302     setup_sigcontext(&uc->tuc_mcontext, env, set->sig[0]);
303     /* Save coprocessor signal frame.  */
304     regspace = uc->tuc_regspace;
305     if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
306         regspace = setup_sigframe_vfp(regspace, env);
307     }
308     if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
309         regspace = setup_sigframe_iwmmxt(regspace, env);
310     }
311 
312     /* Write terminating magic word */
313     __put_user(0, regspace);
314 
315     for(i = 0; i < TARGET_NSIG_WORDS; i++) {
316         __put_user(set->sig[i], &uc->tuc_sigmask.sig[i]);
317     }
318 }
319 
320 void setup_frame(int usig, struct target_sigaction *ka,
321                  target_sigset_t *set, CPUARMState *regs)
322 {
323     struct sigframe *frame;
324     abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
325 
326     trace_user_setup_frame(regs, frame_addr);
327     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
328         goto sigsegv;
329     }
330 
331     setup_sigframe(&frame->uc, set, regs);
332 
333     if (setup_return(regs, ka, usig, frame, frame_addr)) {
334         goto sigsegv;
335     }
336 
337     unlock_user_struct(frame, frame_addr, 1);
338     return;
339 sigsegv:
340     unlock_user_struct(frame, frame_addr, 1);
341     force_sigsegv(usig);
342 }
343 
344 void setup_rt_frame(int usig, struct target_sigaction *ka,
345                     target_siginfo_t *info,
346                     target_sigset_t *set, CPUARMState *env)
347 {
348     struct rt_sigframe *frame;
349     abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
350     abi_ulong info_addr, uc_addr;
351 
352     trace_user_setup_rt_frame(env, frame_addr);
353     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
354         goto sigsegv;
355     }
356 
357     info_addr = frame_addr + offsetof(struct rt_sigframe, info);
358     uc_addr = frame_addr + offsetof(struct rt_sigframe, sig.uc);
359     tswap_siginfo(&frame->info, info);
360 
361     setup_sigframe(&frame->sig.uc, set, env);
362 
363     if (setup_return(env, ka, usig, &frame->sig, frame_addr)) {
364         goto sigsegv;
365     }
366 
367     env->regs[1] = info_addr;
368     env->regs[2] = uc_addr;
369 
370     unlock_user_struct(frame, frame_addr, 1);
371     return;
372 sigsegv:
373     unlock_user_struct(frame, frame_addr, 1);
374     force_sigsegv(usig);
375 }
376 
377 static int
378 restore_sigcontext(CPUARMState *env, struct target_sigcontext *sc)
379 {
380     int err = 0;
381     uint32_t cpsr;
382 
383     __get_user(env->regs[0], &sc->arm_r0);
384     __get_user(env->regs[1], &sc->arm_r1);
385     __get_user(env->regs[2], &sc->arm_r2);
386     __get_user(env->regs[3], &sc->arm_r3);
387     __get_user(env->regs[4], &sc->arm_r4);
388     __get_user(env->regs[5], &sc->arm_r5);
389     __get_user(env->regs[6], &sc->arm_r6);
390     __get_user(env->regs[7], &sc->arm_r7);
391     __get_user(env->regs[8], &sc->arm_r8);
392     __get_user(env->regs[9], &sc->arm_r9);
393     __get_user(env->regs[10], &sc->arm_r10);
394     __get_user(env->regs[11], &sc->arm_fp);
395     __get_user(env->regs[12], &sc->arm_ip);
396     __get_user(env->regs[13], &sc->arm_sp);
397     __get_user(env->regs[14], &sc->arm_lr);
398     __get_user(env->regs[15], &sc->arm_pc);
399     __get_user(cpsr, &sc->arm_cpsr);
400     cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC, CPSRWriteByInstr);
401 
402     err |= !valid_user_regs(env);
403 
404     return err;
405 }
406 
407 static abi_ulong *restore_sigframe_vfp(CPUARMState *env, abi_ulong *regspace)
408 {
409     int i;
410     abi_ulong magic, sz;
411     uint32_t fpscr, fpexc;
412     struct target_vfp_sigframe *vfpframe;
413     vfpframe = (struct target_vfp_sigframe *)regspace;
414 
415     __get_user(magic, &vfpframe->magic);
416     __get_user(sz, &vfpframe->size);
417     if (magic != TARGET_VFP_MAGIC || sz != sizeof(*vfpframe)) {
418         return 0;
419     }
420     for (i = 0; i < 32; i++) {
421         __get_user(*aa32_vfp_dreg(env, i), &vfpframe->ufp.fpregs[i]);
422     }
423     __get_user(fpscr, &vfpframe->ufp.fpscr);
424     vfp_set_fpscr(env, fpscr);
425     __get_user(fpexc, &vfpframe->ufp_exc.fpexc);
426     /* Sanitise FPEXC: ensure VFP is enabled, FPINST2 is invalid
427      * and the exception flag is cleared
428      */
429     fpexc |= (1 << 30);
430     fpexc &= ~((1 << 31) | (1 << 28));
431     env->vfp.xregs[ARM_VFP_FPEXC] = fpexc;
432     __get_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
433     __get_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
434     return (abi_ulong*)(vfpframe + 1);
435 }
436 
437 static abi_ulong *restore_sigframe_iwmmxt(CPUARMState *env,
438                                           abi_ulong *regspace)
439 {
440     int i;
441     abi_ulong magic, sz;
442     struct target_iwmmxt_sigframe *iwmmxtframe;
443     iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
444 
445     __get_user(magic, &iwmmxtframe->magic);
446     __get_user(sz, &iwmmxtframe->size);
447     if (magic != TARGET_IWMMXT_MAGIC || sz != sizeof(*iwmmxtframe)) {
448         return 0;
449     }
450     for (i = 0; i < 16; i++) {
451         __get_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
452     }
453     __get_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
454     __get_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
455     __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
456     __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
457     __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
458     __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
459     return (abi_ulong*)(iwmmxtframe + 1);
460 }
461 
462 static int do_sigframe_return(CPUARMState *env,
463                               target_ulong context_addr,
464                               struct target_ucontext *uc)
465 {
466     sigset_t host_set;
467     abi_ulong *regspace;
468 
469     target_to_host_sigset(&host_set, &uc->tuc_sigmask);
470     set_sigmask(&host_set);
471 
472     if (restore_sigcontext(env, &uc->tuc_mcontext)) {
473         return 1;
474     }
475 
476     /* Restore coprocessor signal frame */
477     regspace = uc->tuc_regspace;
478     if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
479         regspace = restore_sigframe_vfp(env, regspace);
480         if (!regspace) {
481             return 1;
482         }
483     }
484     if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
485         regspace = restore_sigframe_iwmmxt(env, regspace);
486         if (!regspace) {
487             return 1;
488         }
489     }
490 
491     target_restore_altstack(&uc->tuc_stack, env);
492 
493 #if 0
494     /* Send SIGTRAP if we're single-stepping */
495     if (ptrace_cancel_bpt(current))
496         send_sig(SIGTRAP, current, 1);
497 #endif
498 
499     return 0;
500 }
501 
502 long do_sigreturn(CPUARMState *env)
503 {
504     abi_ulong frame_addr;
505     struct sigframe *frame = NULL;
506 
507     /*
508      * Since we stacked the signal on a 64-bit boundary,
509      * then 'sp' should be word aligned here.  If it's
510      * not, then the user is trying to mess with us.
511      */
512     frame_addr = env->regs[13];
513     trace_user_do_sigreturn(env, frame_addr);
514     if (frame_addr & 7) {
515         goto badframe;
516     }
517 
518     if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
519         goto badframe;
520     }
521 
522     if (do_sigframe_return(env,
523                            frame_addr + offsetof(struct sigframe, uc),
524                            &frame->uc)) {
525         goto badframe;
526     }
527 
528     unlock_user_struct(frame, frame_addr, 0);
529     return -QEMU_ESIGRETURN;
530 
531 badframe:
532     unlock_user_struct(frame, frame_addr, 0);
533     force_sig(TARGET_SIGSEGV);
534     return -QEMU_ESIGRETURN;
535 }
536 
537 long do_rt_sigreturn(CPUARMState *env)
538 {
539     abi_ulong frame_addr;
540     struct rt_sigframe *frame = NULL;
541 
542     /*
543      * Since we stacked the signal on a 64-bit boundary,
544      * then 'sp' should be word aligned here.  If it's
545      * not, then the user is trying to mess with us.
546      */
547     frame_addr = env->regs[13];
548     trace_user_do_rt_sigreturn(env, frame_addr);
549     if (frame_addr & 7) {
550         goto badframe;
551     }
552 
553     if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
554         goto badframe;
555     }
556 
557     if (do_sigframe_return(env,
558                            frame_addr + offsetof(struct rt_sigframe, sig.uc),
559                            &frame->sig.uc)) {
560         goto badframe;
561     }
562 
563     unlock_user_struct(frame, frame_addr, 0);
564     return -QEMU_ESIGRETURN;
565 
566 badframe:
567     unlock_user_struct(frame, frame_addr, 0);
568     force_sig(TARGET_SIGSEGV);
569     return -QEMU_ESIGRETURN;
570 }
571 
572 /*
573  * EABI syscalls pass the number via r7.
574  * Note that the kernel still adds the OABI syscall number to the trap,
575  * presumably for backward ABI compatibility with unwinders.
576  */
577 #define ARM_MOV_R7_IMM(X)       (0xe3a07000 | (X))
578 #define ARM_SWI_SYS(X)          (0xef000000 | (X) | ARM_SYSCALL_BASE)
579 
580 #define THUMB_MOVS_R7_IMM(X)    (0x2700 | (X))
581 #define THUMB_SWI_SYS           0xdf00
582 
583 static void write_arm_sigreturn(uint32_t *rc, int syscall)
584 {
585     __put_user(ARM_MOV_R7_IMM(syscall), rc);
586     __put_user(ARM_SWI_SYS(syscall), rc + 1);
587     /* Wrote 8 of 12 bytes */
588 }
589 
590 static void write_thm_sigreturn(uint32_t *rc, int syscall)
591 {
592     __put_user(THUMB_SWI_SYS << 16 | THUMB_MOVS_R7_IMM(syscall), rc);
593     /* Wrote 4 of 12 bytes */
594 }
595 
596 /*
597  * Stub needed to make sure the FD register (r9) contains the right value.
598  * Use the same instruction sequence as the kernel.
599  */
600 static void write_arm_fdpic_sigreturn(uint32_t *rc, int ofs)
601 {
602     assert(ofs <= 0xfff);
603     __put_user(0xe59d3000 | ofs, rc + 0);   /* ldr r3, [sp, #ofs] */
604     __put_user(0xe8930908, rc + 1);         /* ldm r3, { r3, r9 } */
605     __put_user(0xe12fff13, rc + 2);         /* bx  r3 */
606     /* Wrote 12 of 12 bytes */
607 }
608 
609 static void write_thm_fdpic_sigreturn(void *vrc, int ofs)
610 {
611     uint16_t *rc = vrc;
612 
613     assert((ofs & ~0x3fc) == 0);
614     __put_user(0x9b00 | (ofs >> 2), rc + 0);      /* ldr r3, [sp, #ofs] */
615     __put_user(0xcb0c, rc + 1);                   /* ldm r3, { r2, r3 } */
616     __put_user(0x4699, rc + 2);                   /* mov r9, r3 */
617     __put_user(0x4710, rc + 3);                   /* bx  r2 */
618     /* Wrote 8 of 12 bytes */
619 }
620 
621 void setup_sigtramp(abi_ulong sigtramp_page)
622 {
623     uint32_t total_size = 8 * RETCODE_BYTES;
624     uint32_t *tramp = lock_user(VERIFY_WRITE, sigtramp_page, total_size, 0);
625 
626     assert(tramp != NULL);
627 
628     default_sigreturn = sigtramp_page;
629     write_arm_sigreturn(&tramp[0 * RETCODE_WORDS], TARGET_NR_sigreturn);
630     write_thm_sigreturn(&tramp[1 * RETCODE_WORDS], TARGET_NR_sigreturn);
631     write_arm_sigreturn(&tramp[2 * RETCODE_WORDS], TARGET_NR_rt_sigreturn);
632     write_thm_sigreturn(&tramp[3 * RETCODE_WORDS], TARGET_NR_rt_sigreturn);
633 
634     sigreturn_fdpic_tramp = sigtramp_page + 4 * RETCODE_BYTES;
635     write_arm_fdpic_sigreturn(tramp + 4 * RETCODE_WORDS,
636                               offsetof(struct sigframe, retcode[3]));
637     write_thm_fdpic_sigreturn(tramp + 5 * RETCODE_WORDS,
638                                 offsetof(struct sigframe, retcode[3]));
639     write_arm_fdpic_sigreturn(tramp + 6 * RETCODE_WORDS,
640                               offsetof(struct rt_sigframe, sig.retcode[3]));
641     write_thm_fdpic_sigreturn(tramp + 7 * RETCODE_WORDS,
642                               offsetof(struct rt_sigframe, sig.retcode[3]));
643 
644     unlock_user(tramp, sigtramp_page, total_size);
645 }
646