xref: /openbmc/qemu/linux-user/aarch64/signal.c (revision d3b4f717)
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     uint64_t fault_address;
27     /* AArch64 registers */
28     uint64_t regs[31];
29     uint64_t sp;
30     uint64_t pc;
31     uint64_t pstate;
32     /* 4K reserved for FP/SIMD state and future expansion */
33     char __reserved[4096] __attribute__((__aligned__(16)));
34 };
35 
36 struct target_ucontext {
37     abi_ulong tuc_flags;
38     abi_ulong tuc_link;
39     target_stack_t tuc_stack;
40     target_sigset_t tuc_sigmask;
41     /* glibc uses a 1024-bit sigset_t */
42     char __unused[1024 / 8 - sizeof(target_sigset_t)];
43     /* last for future expansion */
44     struct target_sigcontext tuc_mcontext;
45 };
46 
47 /*
48  * Header to be used at the beginning of structures extending the user
49  * context. Such structures must be placed after the rt_sigframe on the stack
50  * and be 16-byte aligned. The last structure must be a dummy one with the
51  * magic and size set to 0.
52  */
53 struct target_aarch64_ctx {
54     uint32_t magic;
55     uint32_t size;
56 };
57 
58 #define TARGET_FPSIMD_MAGIC 0x46508001
59 
60 struct target_fpsimd_context {
61     struct target_aarch64_ctx head;
62     uint32_t fpsr;
63     uint32_t fpcr;
64     uint64_t vregs[32 * 2]; /* really uint128_t vregs[32] */
65 };
66 
67 #define TARGET_EXTRA_MAGIC  0x45585401
68 
69 struct target_extra_context {
70     struct target_aarch64_ctx head;
71     uint64_t datap; /* 16-byte aligned pointer to extra space cast to __u64 */
72     uint32_t size; /* size in bytes of the extra space */
73     uint32_t reserved[3];
74 };
75 
76 #define TARGET_SVE_MAGIC    0x53564501
77 
78 struct target_sve_context {
79     struct target_aarch64_ctx head;
80     uint16_t vl;
81     uint16_t flags;
82     uint16_t reserved[2];
83     /* The actual SVE data immediately follows.  It is laid out
84      * according to TARGET_SVE_SIG_{Z,P}REG_OFFSET, based off of
85      * the original struct pointer.
86      */
87 };
88 
89 #define TARGET_SVE_VQ_BYTES  16
90 
91 #define TARGET_SVE_SIG_ZREG_SIZE(VQ)  ((VQ) * TARGET_SVE_VQ_BYTES)
92 #define TARGET_SVE_SIG_PREG_SIZE(VQ)  ((VQ) * (TARGET_SVE_VQ_BYTES / 8))
93 
94 #define TARGET_SVE_SIG_REGS_OFFSET \
95     QEMU_ALIGN_UP(sizeof(struct target_sve_context), TARGET_SVE_VQ_BYTES)
96 #define TARGET_SVE_SIG_ZREG_OFFSET(VQ, N) \
97     (TARGET_SVE_SIG_REGS_OFFSET + TARGET_SVE_SIG_ZREG_SIZE(VQ) * (N))
98 #define TARGET_SVE_SIG_PREG_OFFSET(VQ, N) \
99     (TARGET_SVE_SIG_ZREG_OFFSET(VQ, 32) + TARGET_SVE_SIG_PREG_SIZE(VQ) * (N))
100 #define TARGET_SVE_SIG_FFR_OFFSET(VQ) \
101     (TARGET_SVE_SIG_PREG_OFFSET(VQ, 16))
102 #define TARGET_SVE_SIG_CONTEXT_SIZE(VQ) \
103     (TARGET_SVE_SIG_PREG_OFFSET(VQ, 17))
104 
105 #define TARGET_SVE_SIG_FLAG_SM  1
106 
107 struct target_rt_sigframe {
108     struct target_siginfo info;
109     struct target_ucontext uc;
110 };
111 
112 struct target_rt_frame_record {
113     uint64_t fp;
114     uint64_t lr;
115 };
116 
117 static void target_setup_general_frame(struct target_rt_sigframe *sf,
118                                        CPUARMState *env, target_sigset_t *set)
119 {
120     int i;
121 
122     __put_user(0, &sf->uc.tuc_flags);
123     __put_user(0, &sf->uc.tuc_link);
124 
125     target_save_altstack(&sf->uc.tuc_stack, env);
126 
127     for (i = 0; i < 31; i++) {
128         __put_user(env->xregs[i], &sf->uc.tuc_mcontext.regs[i]);
129     }
130     __put_user(env->xregs[31], &sf->uc.tuc_mcontext.sp);
131     __put_user(env->pc, &sf->uc.tuc_mcontext.pc);
132     __put_user(pstate_read(env), &sf->uc.tuc_mcontext.pstate);
133 
134     __put_user(env->exception.vaddress, &sf->uc.tuc_mcontext.fault_address);
135 
136     for (i = 0; i < TARGET_NSIG_WORDS; i++) {
137         __put_user(set->sig[i], &sf->uc.tuc_sigmask.sig[i]);
138     }
139 }
140 
141 static void target_setup_fpsimd_record(struct target_fpsimd_context *fpsimd,
142                                        CPUARMState *env)
143 {
144     int i;
145 
146     __put_user(TARGET_FPSIMD_MAGIC, &fpsimd->head.magic);
147     __put_user(sizeof(struct target_fpsimd_context), &fpsimd->head.size);
148     __put_user(vfp_get_fpsr(env), &fpsimd->fpsr);
149     __put_user(vfp_get_fpcr(env), &fpsimd->fpcr);
150 
151     for (i = 0; i < 32; i++) {
152         uint64_t *q = aa64_vfp_qreg(env, i);
153 #if TARGET_BIG_ENDIAN
154         __put_user(q[0], &fpsimd->vregs[i * 2 + 1]);
155         __put_user(q[1], &fpsimd->vregs[i * 2]);
156 #else
157         __put_user(q[0], &fpsimd->vregs[i * 2]);
158         __put_user(q[1], &fpsimd->vregs[i * 2 + 1]);
159 #endif
160     }
161 }
162 
163 static void target_setup_extra_record(struct target_extra_context *extra,
164                                       uint64_t datap, uint32_t extra_size)
165 {
166     __put_user(TARGET_EXTRA_MAGIC, &extra->head.magic);
167     __put_user(sizeof(struct target_extra_context), &extra->head.size);
168     __put_user(datap, &extra->datap);
169     __put_user(extra_size, &extra->size);
170 }
171 
172 static void target_setup_end_record(struct target_aarch64_ctx *end)
173 {
174     __put_user(0, &end->magic);
175     __put_user(0, &end->size);
176 }
177 
178 static void target_setup_sve_record(struct target_sve_context *sve,
179                                     CPUARMState *env, int vq, int size)
180 {
181     int i, j;
182 
183     memset(sve, 0, sizeof(*sve));
184     __put_user(TARGET_SVE_MAGIC, &sve->head.magic);
185     __put_user(size, &sve->head.size);
186     __put_user(vq * TARGET_SVE_VQ_BYTES, &sve->vl);
187     if (FIELD_EX64(env->svcr, SVCR, SM)) {
188         __put_user(TARGET_SVE_SIG_FLAG_SM, &sve->flags);
189     }
190 
191     /* Note that SVE regs are stored as a byte stream, with each byte element
192      * at a subsequent address.  This corresponds to a little-endian store
193      * of our 64-bit hunks.
194      */
195     for (i = 0; i < 32; ++i) {
196         uint64_t *z = (void *)sve + TARGET_SVE_SIG_ZREG_OFFSET(vq, i);
197         for (j = 0; j < vq * 2; ++j) {
198             __put_user_e(env->vfp.zregs[i].d[j], z + j, le);
199         }
200     }
201     for (i = 0; i <= 16; ++i) {
202         uint16_t *p = (void *)sve + TARGET_SVE_SIG_PREG_OFFSET(vq, i);
203         for (j = 0; j < vq; ++j) {
204             uint64_t r = env->vfp.pregs[i].p[j >> 2];
205             __put_user_e(r >> ((j & 3) * 16), p + j, le);
206         }
207     }
208 }
209 
210 static void target_restore_general_frame(CPUARMState *env,
211                                          struct target_rt_sigframe *sf)
212 {
213     sigset_t set;
214     uint64_t pstate;
215     int i;
216 
217     target_to_host_sigset(&set, &sf->uc.tuc_sigmask);
218     set_sigmask(&set);
219 
220     for (i = 0; i < 31; i++) {
221         __get_user(env->xregs[i], &sf->uc.tuc_mcontext.regs[i]);
222     }
223 
224     __get_user(env->xregs[31], &sf->uc.tuc_mcontext.sp);
225     __get_user(env->pc, &sf->uc.tuc_mcontext.pc);
226     __get_user(pstate, &sf->uc.tuc_mcontext.pstate);
227     pstate_write(env, pstate);
228 }
229 
230 static void target_restore_fpsimd_record(CPUARMState *env,
231                                          struct target_fpsimd_context *fpsimd)
232 {
233     uint32_t fpsr, fpcr;
234     int i;
235 
236     __get_user(fpsr, &fpsimd->fpsr);
237     vfp_set_fpsr(env, fpsr);
238     __get_user(fpcr, &fpsimd->fpcr);
239     vfp_set_fpcr(env, fpcr);
240 
241     for (i = 0; i < 32; i++) {
242         uint64_t *q = aa64_vfp_qreg(env, i);
243 #if TARGET_BIG_ENDIAN
244         __get_user(q[0], &fpsimd->vregs[i * 2 + 1]);
245         __get_user(q[1], &fpsimd->vregs[i * 2]);
246 #else
247         __get_user(q[0], &fpsimd->vregs[i * 2]);
248         __get_user(q[1], &fpsimd->vregs[i * 2 + 1]);
249 #endif
250     }
251 }
252 
253 static bool target_restore_sve_record(CPUARMState *env,
254                                       struct target_sve_context *sve,
255                                       int size)
256 {
257     int i, j, vl, vq;
258 
259     if (!cpu_isar_feature(aa64_sve, env_archcpu(env))) {
260         return false;
261     }
262 
263     __get_user(vl, &sve->vl);
264     vq = sve_vq(env);
265 
266     /* Reject mismatched VL. */
267     if (vl != vq * TARGET_SVE_VQ_BYTES) {
268         return false;
269     }
270 
271     /* Accept empty record -- used to clear PSTATE.SM. */
272     if (size <= sizeof(*sve)) {
273         return true;
274     }
275 
276     /* Reject non-empty but incomplete record. */
277     if (size < TARGET_SVE_SIG_CONTEXT_SIZE(vq)) {
278         return false;
279     }
280 
281     /*
282      * Note that SVE regs are stored as a byte stream, with each byte element
283      * at a subsequent address.  This corresponds to a little-endian load
284      * of our 64-bit hunks.
285      */
286     for (i = 0; i < 32; ++i) {
287         uint64_t *z = (void *)sve + TARGET_SVE_SIG_ZREG_OFFSET(vq, i);
288         for (j = 0; j < vq * 2; ++j) {
289             __get_user_e(env->vfp.zregs[i].d[j], z + j, le);
290         }
291     }
292     for (i = 0; i <= 16; ++i) {
293         uint16_t *p = (void *)sve + TARGET_SVE_SIG_PREG_OFFSET(vq, i);
294         for (j = 0; j < vq; ++j) {
295             uint16_t r;
296             __get_user_e(r, p + j, le);
297             if (j & 3) {
298                 env->vfp.pregs[i].p[j >> 2] |= (uint64_t)r << ((j & 3) * 16);
299             } else {
300                 env->vfp.pregs[i].p[j >> 2] = r;
301             }
302         }
303     }
304     return true;
305 }
306 
307 static int target_restore_sigframe(CPUARMState *env,
308                                    struct target_rt_sigframe *sf)
309 {
310     struct target_aarch64_ctx *ctx, *extra = NULL;
311     struct target_fpsimd_context *fpsimd = NULL;
312     struct target_sve_context *sve = NULL;
313     uint64_t extra_datap = 0;
314     bool used_extra = false;
315     int sve_size = 0;
316 
317     target_restore_general_frame(env, sf);
318 
319     ctx = (struct target_aarch64_ctx *)sf->uc.tuc_mcontext.__reserved;
320     while (ctx) {
321         uint32_t magic, size, extra_size;
322 
323         __get_user(magic, &ctx->magic);
324         __get_user(size, &ctx->size);
325         switch (magic) {
326         case 0:
327             if (size != 0) {
328                 goto err;
329             }
330             if (used_extra) {
331                 ctx = NULL;
332             } else {
333                 ctx = extra;
334                 used_extra = true;
335             }
336             continue;
337 
338         case TARGET_FPSIMD_MAGIC:
339             if (fpsimd || size != sizeof(struct target_fpsimd_context)) {
340                 goto err;
341             }
342             fpsimd = (struct target_fpsimd_context *)ctx;
343             break;
344 
345         case TARGET_SVE_MAGIC:
346             if (sve || size < sizeof(struct target_sve_context)) {
347                 goto err;
348             }
349             sve = (struct target_sve_context *)ctx;
350             sve_size = size;
351             break;
352 
353         case TARGET_EXTRA_MAGIC:
354             if (extra || size != sizeof(struct target_extra_context)) {
355                 goto err;
356             }
357             __get_user(extra_datap,
358                        &((struct target_extra_context *)ctx)->datap);
359             __get_user(extra_size,
360                        &((struct target_extra_context *)ctx)->size);
361             extra = lock_user(VERIFY_READ, extra_datap, extra_size, 0);
362             if (!extra) {
363                 return 1;
364             }
365             break;
366 
367         default:
368             /* Unknown record -- we certainly didn't generate it.
369              * Did we in fact get out of sync?
370              */
371             goto err;
372         }
373         ctx = (void *)ctx + size;
374     }
375 
376     /* Require FPSIMD always.  */
377     if (fpsimd) {
378         target_restore_fpsimd_record(env, fpsimd);
379     } else {
380         goto err;
381     }
382 
383     /* SVE data, if present, overwrites FPSIMD data.  */
384     if (sve && !target_restore_sve_record(env, sve, sve_size)) {
385         goto err;
386     }
387     unlock_user(extra, extra_datap, 0);
388     return 0;
389 
390  err:
391     unlock_user(extra, extra_datap, 0);
392     return 1;
393 }
394 
395 static abi_ulong get_sigframe(struct target_sigaction *ka,
396                               CPUARMState *env, int size)
397 {
398     abi_ulong sp;
399 
400     sp = target_sigsp(get_sp_from_cpustate(env), ka);
401 
402     sp = (sp - size) & ~15;
403 
404     return sp;
405 }
406 
407 typedef struct {
408     int total_size;
409     int extra_base;
410     int extra_size;
411     int std_end_ofs;
412     int extra_ofs;
413     int extra_end_ofs;
414 } target_sigframe_layout;
415 
416 static int alloc_sigframe_space(int this_size, target_sigframe_layout *l)
417 {
418     /* Make sure there will always be space for the end marker.  */
419     const int std_size = sizeof(struct target_rt_sigframe)
420                          - sizeof(struct target_aarch64_ctx);
421     int this_loc = l->total_size;
422 
423     if (l->extra_base) {
424         /* Once we have begun an extra space, all allocations go there.  */
425         l->extra_size += this_size;
426     } else if (this_size + this_loc > std_size) {
427         /* This allocation does not fit in the standard space.  */
428         /* Allocate the extra record.  */
429         l->extra_ofs = this_loc;
430         l->total_size += sizeof(struct target_extra_context);
431 
432         /* Allocate the standard end record.  */
433         l->std_end_ofs = l->total_size;
434         l->total_size += sizeof(struct target_aarch64_ctx);
435 
436         /* Allocate the requested record.  */
437         l->extra_base = this_loc = l->total_size;
438         l->extra_size = this_size;
439     }
440     l->total_size += this_size;
441 
442     return this_loc;
443 }
444 
445 static void target_setup_frame(int usig, struct target_sigaction *ka,
446                                target_siginfo_t *info, target_sigset_t *set,
447                                CPUARMState *env)
448 {
449     target_sigframe_layout layout = {
450         /* Begin with the size pointing to the reserved space.  */
451         .total_size = offsetof(struct target_rt_sigframe,
452                                uc.tuc_mcontext.__reserved),
453     };
454     int fpsimd_ofs, fr_ofs, sve_ofs = 0, vq = 0, sve_size = 0;
455     struct target_rt_sigframe *frame;
456     struct target_rt_frame_record *fr;
457     abi_ulong frame_addr, return_addr;
458 
459     /* FPSIMD record is always in the standard space.  */
460     fpsimd_ofs = alloc_sigframe_space(sizeof(struct target_fpsimd_context),
461                                       &layout);
462 
463     /* SVE state needs saving only if it exists.  */
464     if (cpu_isar_feature(aa64_sve, env_archcpu(env))) {
465         vq = sve_vq(env);
466         sve_size = QEMU_ALIGN_UP(TARGET_SVE_SIG_CONTEXT_SIZE(vq), 16);
467         sve_ofs = alloc_sigframe_space(sve_size, &layout);
468     }
469 
470     if (layout.extra_ofs) {
471         /* Reserve space for the extra end marker.  The standard end marker
472          * will have been allocated when we allocated the extra record.
473          */
474         layout.extra_end_ofs
475             = alloc_sigframe_space(sizeof(struct target_aarch64_ctx), &layout);
476     } else {
477         /* Reserve space for the standard end marker.
478          * Do not use alloc_sigframe_space because we cheat
479          * std_size therein to reserve space for this.
480          */
481         layout.std_end_ofs = layout.total_size;
482         layout.total_size += sizeof(struct target_aarch64_ctx);
483     }
484 
485     /* We must always provide at least the standard 4K reserved space,
486      * even if we don't use all of it (this is part of the ABI)
487      */
488     layout.total_size = MAX(layout.total_size,
489                             sizeof(struct target_rt_sigframe));
490 
491     /*
492      * Reserve space for the standard frame unwind pair: fp, lr.
493      * Despite the name this is not a "real" record within the frame.
494      */
495     fr_ofs = layout.total_size;
496     layout.total_size += sizeof(struct target_rt_frame_record);
497 
498     frame_addr = get_sigframe(ka, env, layout.total_size);
499     trace_user_setup_frame(env, frame_addr);
500     frame = lock_user(VERIFY_WRITE, frame_addr, layout.total_size, 0);
501     if (!frame) {
502         goto give_sigsegv;
503     }
504 
505     target_setup_general_frame(frame, env, set);
506     target_setup_fpsimd_record((void *)frame + fpsimd_ofs, env);
507     target_setup_end_record((void *)frame + layout.std_end_ofs);
508     if (layout.extra_ofs) {
509         target_setup_extra_record((void *)frame + layout.extra_ofs,
510                                   frame_addr + layout.extra_base,
511                                   layout.extra_size);
512         target_setup_end_record((void *)frame + layout.extra_end_ofs);
513     }
514     if (sve_ofs) {
515         target_setup_sve_record((void *)frame + sve_ofs, env, vq, sve_size);
516     }
517 
518     /* Set up the stack frame for unwinding.  */
519     fr = (void *)frame + fr_ofs;
520     __put_user(env->xregs[29], &fr->fp);
521     __put_user(env->xregs[30], &fr->lr);
522 
523     if (ka->sa_flags & TARGET_SA_RESTORER) {
524         return_addr = ka->sa_restorer;
525     } else {
526         return_addr = default_rt_sigreturn;
527     }
528     env->xregs[0] = usig;
529     env->xregs[29] = frame_addr + fr_ofs;
530     env->xregs[30] = return_addr;
531     env->xregs[31] = frame_addr;
532     env->pc = ka->_sa_handler;
533 
534     /* Invoke the signal handler as if by indirect call.  */
535     if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
536         env->btype = 2;
537     }
538 
539     if (info) {
540         tswap_siginfo(&frame->info, info);
541         env->xregs[1] = frame_addr + offsetof(struct target_rt_sigframe, info);
542         env->xregs[2] = frame_addr + offsetof(struct target_rt_sigframe, uc);
543     }
544 
545     unlock_user(frame, frame_addr, layout.total_size);
546     return;
547 
548  give_sigsegv:
549     unlock_user(frame, frame_addr, layout.total_size);
550     force_sigsegv(usig);
551 }
552 
553 void setup_rt_frame(int sig, struct target_sigaction *ka,
554                     target_siginfo_t *info, target_sigset_t *set,
555                     CPUARMState *env)
556 {
557     target_setup_frame(sig, ka, info, set, env);
558 }
559 
560 void setup_frame(int sig, struct target_sigaction *ka,
561                  target_sigset_t *set, CPUARMState *env)
562 {
563     target_setup_frame(sig, ka, 0, set, env);
564 }
565 
566 long do_rt_sigreturn(CPUARMState *env)
567 {
568     struct target_rt_sigframe *frame = NULL;
569     abi_ulong frame_addr = env->xregs[31];
570 
571     trace_user_do_rt_sigreturn(env, frame_addr);
572     if (frame_addr & 15) {
573         goto badframe;
574     }
575 
576     if  (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
577         goto badframe;
578     }
579 
580     if (target_restore_sigframe(env, frame)) {
581         goto badframe;
582     }
583 
584     target_restore_altstack(&frame->uc.tuc_stack, env);
585 
586     unlock_user_struct(frame, frame_addr, 0);
587     return -QEMU_ESIGRETURN;
588 
589  badframe:
590     unlock_user_struct(frame, frame_addr, 0);
591     force_sig(TARGET_SIGSEGV);
592     return -QEMU_ESIGRETURN;
593 }
594 
595 long do_sigreturn(CPUARMState *env)
596 {
597     return do_rt_sigreturn(env);
598 }
599 
600 void setup_sigtramp(abi_ulong sigtramp_page)
601 {
602     uint32_t *tramp = lock_user(VERIFY_WRITE, sigtramp_page, 8, 0);
603     assert(tramp != NULL);
604 
605     /*
606      * mov x8,#__NR_rt_sigreturn; svc #0
607      * Since these are instructions they need to be put as little-endian
608      * regardless of target default or current CPU endianness.
609      */
610     __put_user_e(0xd2801168, &tramp[0], le);
611     __put_user_e(0xd4000001, &tramp[1], le);
612 
613     default_rt_sigreturn = sigtramp_page;
614     unlock_user(tramp, sigtramp_page, 8);
615 }
616