xref: /openbmc/qemu/linux-user/loongarch64/signal.c (revision ba49d760eb04630e7b15f423ebecf6c871b8f77b)
19d5cd658SSong Gao /* SPDX-License-Identifier: GPL-2.0-or-later */
29d5cd658SSong Gao /*
39d5cd658SSong Gao  * LoongArch emulation of Linux signals
49d5cd658SSong Gao  *
59d5cd658SSong Gao  * Copyright (c) 2021 Loongson Technology Corporation Limited
69d5cd658SSong Gao  */
79d5cd658SSong Gao 
89d5cd658SSong Gao #include "qemu/osdep.h"
99d5cd658SSong Gao #include "qemu.h"
109d5cd658SSong Gao #include "user-internals.h"
119d5cd658SSong Gao #include "signal-common.h"
129d5cd658SSong Gao #include "linux-user/trace.h"
139d5cd658SSong Gao #include "target/loongarch/internals.h"
14008a3b16SSong Gao #include "target/loongarch/vec.h"
1500cc2934SRichard Henderson #include "vdso-asmoffset.h"
169d5cd658SSong Gao 
179d5cd658SSong Gao /* FP context was used */
189d5cd658SSong Gao #define SC_USED_FP              (1 << 0)
199d5cd658SSong Gao 
209d5cd658SSong Gao struct target_sigcontext {
217c0ea81cSSong Gao     abi_ulong sc_pc;
227c0ea81cSSong Gao     abi_ulong sc_regs[32];
237c0ea81cSSong Gao     abi_uint  sc_flags;
247c0ea81cSSong Gao     abi_ulong sc_extcontext[0]   QEMU_ALIGNED(16);
259d5cd658SSong Gao };
269d5cd658SSong Gao 
2700cc2934SRichard Henderson QEMU_BUILD_BUG_ON(sizeof(struct target_sigcontext) != sizeof_sigcontext);
2800cc2934SRichard Henderson QEMU_BUILD_BUG_ON(offsetof(struct target_sigcontext, sc_pc)
2900cc2934SRichard Henderson                   != offsetof_sigcontext_pc);
3000cc2934SRichard Henderson QEMU_BUILD_BUG_ON(offsetof(struct target_sigcontext, sc_regs)
3100cc2934SRichard Henderson                   != offsetof_sigcontext_gr);
329d5cd658SSong Gao 
339d5cd658SSong Gao #define FPU_CTX_MAGIC           0x46505501
349d5cd658SSong Gao #define FPU_CTX_ALIGN           8
359d5cd658SSong Gao struct target_fpu_context {
367c0ea81cSSong Gao     abi_ulong regs[32];
377c0ea81cSSong Gao     abi_ulong fcc;
387c0ea81cSSong Gao     abi_uint  fcsr;
399d5cd658SSong Gao } QEMU_ALIGNED(FPU_CTX_ALIGN);
409d5cd658SSong Gao 
4100cc2934SRichard Henderson QEMU_BUILD_BUG_ON(offsetof(struct target_fpu_context, regs)
4200cc2934SRichard Henderson                   != offsetof_fpucontext_fr);
4300cc2934SRichard Henderson 
4490ea967dSSong Gao #define LSX_CTX_MAGIC           0x53580001
4590ea967dSSong Gao #define LSX_CTX_ALIGN           16
4690ea967dSSong Gao struct target_lsx_context {
4790ea967dSSong Gao     abi_ulong regs[2 * 32];
4890ea967dSSong Gao     abi_ulong fcc;
4990ea967dSSong Gao     abi_uint  fcsr;
5090ea967dSSong Gao } QEMU_ALIGNED(LSX_CTX_ALIGN);
5190ea967dSSong Gao 
52f7077737SSong Gao #define LASX_CTX_MAGIC          0x41535801
53f7077737SSong Gao #define LASX_CTX_ALIGN          32
54f7077737SSong Gao struct target_lasx_context {
55f7077737SSong Gao     abi_ulong regs[4 * 32];
56f7077737SSong Gao     abi_ulong fcc;
57f7077737SSong Gao     abi_uint  fcsr;
58f7077737SSong Gao } QEMU_ALIGNED(LASX_CTX_ALIGN);
59f7077737SSong Gao 
609d5cd658SSong Gao #define CONTEXT_INFO_ALIGN      16
619d5cd658SSong Gao struct target_sctx_info {
627c0ea81cSSong Gao     abi_uint  magic;
637c0ea81cSSong Gao     abi_uint  size;
647c0ea81cSSong Gao     abi_ulong padding;
659d5cd658SSong Gao } QEMU_ALIGNED(CONTEXT_INFO_ALIGN);
669d5cd658SSong Gao 
6700cc2934SRichard Henderson QEMU_BUILD_BUG_ON(sizeof(struct target_sctx_info) != sizeof_sctx_info);
6800cc2934SRichard Henderson 
699d5cd658SSong Gao struct target_ucontext {
709d5cd658SSong Gao     abi_ulong tuc_flags;
719d5cd658SSong Gao     abi_ptr tuc_link;
729d5cd658SSong Gao     target_stack_t tuc_stack;
739d5cd658SSong Gao     target_sigset_t tuc_sigmask;
749d5cd658SSong Gao     uint8_t __unused[1024 / 8 - sizeof(target_sigset_t)];
759d5cd658SSong Gao     struct target_sigcontext tuc_mcontext;
769d5cd658SSong Gao };
779d5cd658SSong Gao 
789d5cd658SSong Gao struct target_rt_sigframe {
799d5cd658SSong Gao     struct target_siginfo        rs_info;
809d5cd658SSong Gao     struct target_ucontext       rs_uc;
819d5cd658SSong Gao };
829d5cd658SSong Gao 
8300cc2934SRichard Henderson QEMU_BUILD_BUG_ON(sizeof(struct target_rt_sigframe)
8400cc2934SRichard Henderson                   != sizeof_rt_sigframe);
8500cc2934SRichard Henderson QEMU_BUILD_BUG_ON(offsetof(struct target_rt_sigframe, rs_uc.tuc_mcontext)
8600cc2934SRichard Henderson                   != offsetof_sigcontext);
8700cc2934SRichard Henderson 
889d5cd658SSong Gao /*
899d5cd658SSong Gao  * These two structures are not present in guest memory, are private
909d5cd658SSong Gao  * to the signal implementation, but are largely copied from the
919d5cd658SSong Gao  * kernel's signal implementation.
929d5cd658SSong Gao  */
939d5cd658SSong Gao struct ctx_layout {
949d5cd658SSong Gao     void *haddr;
959d5cd658SSong Gao     abi_ptr gaddr;
969d5cd658SSong Gao     unsigned int size;
979d5cd658SSong Gao };
989d5cd658SSong Gao 
999d5cd658SSong Gao struct extctx_layout {
10090ea967dSSong Gao     unsigned long size;
1019d5cd658SSong Gao     unsigned int flags;
1029d5cd658SSong Gao     struct ctx_layout fpu;
10390ea967dSSong Gao     struct ctx_layout lsx;
104f7077737SSong Gao     struct ctx_layout lasx;
1059d5cd658SSong Gao     struct ctx_layout end;
1069d5cd658SSong Gao };
1079d5cd658SSong Gao 
extframe_alloc(struct extctx_layout * extctx,struct ctx_layout * sctx,unsigned size,unsigned align,abi_ptr orig_sp)1089d5cd658SSong Gao static abi_ptr extframe_alloc(struct extctx_layout *extctx,
1099d5cd658SSong Gao                               struct ctx_layout *sctx, unsigned size,
1109d5cd658SSong Gao                               unsigned align, abi_ptr orig_sp)
1119d5cd658SSong Gao {
1129d5cd658SSong Gao     abi_ptr sp = orig_sp;
1139d5cd658SSong Gao 
1149d5cd658SSong Gao     sp -= sizeof(struct target_sctx_info) + size;
1159d5cd658SSong Gao     align = MAX(align, CONTEXT_INFO_ALIGN);
1169d5cd658SSong Gao     sp = ROUND_DOWN(sp, align);
1179d5cd658SSong Gao     sctx->gaddr = sp;
1189d5cd658SSong Gao 
1199d5cd658SSong Gao     size = orig_sp - sp;
1209d5cd658SSong Gao     sctx->size = size;
1219d5cd658SSong Gao     extctx->size += size;
1229d5cd658SSong Gao 
1239d5cd658SSong Gao     return sp;
1249d5cd658SSong Gao }
1259d5cd658SSong Gao 
setup_extcontext(CPULoongArchState * env,struct extctx_layout * extctx,abi_ptr sp)12690ea967dSSong Gao static abi_ptr setup_extcontext(CPULoongArchState *env,
12790ea967dSSong Gao                                 struct extctx_layout *extctx, abi_ptr sp)
1289d5cd658SSong Gao {
1299d5cd658SSong Gao     memset(extctx, 0, sizeof(struct extctx_layout));
1309d5cd658SSong Gao 
1319d5cd658SSong Gao     /* Grow down, alloc "end" context info first. */
1329d5cd658SSong Gao     sp = extframe_alloc(extctx, &extctx->end, 0, CONTEXT_INFO_ALIGN, sp);
1339d5cd658SSong Gao 
1349d5cd658SSong Gao     /* For qemu, there is no lazy fp context switch, so fp always present. */
1359d5cd658SSong Gao     extctx->flags = SC_USED_FP;
13690ea967dSSong Gao 
137f7077737SSong Gao     if (FIELD_EX64(env->CSR_EUEN, CSR_EUEN, ASXE)) {
138f7077737SSong Gao         sp = extframe_alloc(extctx, &extctx->lasx,
139f7077737SSong Gao                         sizeof(struct target_lasx_context), LASX_CTX_ALIGN, sp);
140f7077737SSong Gao     } else if (FIELD_EX64(env->CSR_EUEN, CSR_EUEN, SXE)) {
14190ea967dSSong Gao         sp = extframe_alloc(extctx, &extctx->lsx,
14290ea967dSSong Gao                         sizeof(struct target_lsx_context), LSX_CTX_ALIGN, sp);
14390ea967dSSong Gao     } else {
1449d5cd658SSong Gao         sp = extframe_alloc(extctx, &extctx->fpu,
145420756c2SSong Gao                         sizeof(struct target_fpu_context), FPU_CTX_ALIGN, sp);
14690ea967dSSong Gao     }
1479d5cd658SSong Gao 
1489d5cd658SSong Gao     return sp;
1499d5cd658SSong Gao }
1509d5cd658SSong Gao 
setup_sigframe(CPULoongArchState * env,struct target_sigcontext * sc,struct extctx_layout * extctx)1519d5cd658SSong Gao static void setup_sigframe(CPULoongArchState *env,
1529d5cd658SSong Gao                            struct target_sigcontext *sc,
1539d5cd658SSong Gao                            struct extctx_layout *extctx)
1549d5cd658SSong Gao {
1559d5cd658SSong Gao     struct target_sctx_info *info;
1569d5cd658SSong Gao     int i;
1579d5cd658SSong Gao 
1589d5cd658SSong Gao     __put_user(extctx->flags, &sc->sc_flags);
1599d5cd658SSong Gao     __put_user(env->pc, &sc->sc_pc);
1609d5cd658SSong Gao     __put_user(0, &sc->sc_regs[0]);
1619d5cd658SSong Gao     for (i = 1; i < 32; ++i) {
1629d5cd658SSong Gao         __put_user(env->gpr[i], &sc->sc_regs[i]);
1639d5cd658SSong Gao     }
1649d5cd658SSong Gao 
1659d5cd658SSong Gao     /*
16690ea967dSSong Gao      * Set extension context
1679d5cd658SSong Gao      */
16890ea967dSSong Gao 
169f7077737SSong Gao     if (FIELD_EX64(env->CSR_EUEN, CSR_EUEN, ASXE)) {
170f7077737SSong Gao         struct target_lasx_context *lasx_ctx;
171f7077737SSong Gao         info = extctx->lasx.haddr;
172f7077737SSong Gao 
173f7077737SSong Gao         __put_user(LASX_CTX_MAGIC, &info->magic);
174f7077737SSong Gao         __put_user(extctx->lasx.size, &info->size);
175f7077737SSong Gao 
176f7077737SSong Gao         lasx_ctx = (struct target_lasx_context *)(info + 1);
177f7077737SSong Gao 
178f7077737SSong Gao         for (i = 0; i < 32; ++i) {
179f7077737SSong Gao             __put_user(env->fpr[i].vreg.UD(0), &lasx_ctx->regs[4 * i]);
180f7077737SSong Gao             __put_user(env->fpr[i].vreg.UD(1), &lasx_ctx->regs[4 * i + 1]);
181f7077737SSong Gao             __put_user(env->fpr[i].vreg.UD(2), &lasx_ctx->regs[4 * i + 2]);
182f7077737SSong Gao             __put_user(env->fpr[i].vreg.UD(3), &lasx_ctx->regs[4 * i + 3]);
183f7077737SSong Gao         }
184f7077737SSong Gao         __put_user(read_fcc(env), &lasx_ctx->fcc);
185f7077737SSong Gao         __put_user(env->fcsr0, &lasx_ctx->fcsr);
186f7077737SSong Gao     } else if (FIELD_EX64(env->CSR_EUEN, CSR_EUEN, SXE)) {
18790ea967dSSong Gao         struct target_lsx_context *lsx_ctx;
18890ea967dSSong Gao         info = extctx->lsx.haddr;
18990ea967dSSong Gao 
19090ea967dSSong Gao         __put_user(LSX_CTX_MAGIC, &info->magic);
19190ea967dSSong Gao         __put_user(extctx->lsx.size, &info->size);
19290ea967dSSong Gao 
19390ea967dSSong Gao         lsx_ctx = (struct target_lsx_context *)(info + 1);
19490ea967dSSong Gao 
19590ea967dSSong Gao         for (i = 0; i < 32; ++i) {
19690ea967dSSong Gao             __put_user(env->fpr[i].vreg.UD(0), &lsx_ctx->regs[2 * i]);
19790ea967dSSong Gao             __put_user(env->fpr[i].vreg.UD(1), &lsx_ctx->regs[2 * i + 1]);
19890ea967dSSong Gao         }
19990ea967dSSong Gao         __put_user(read_fcc(env), &lsx_ctx->fcc);
20090ea967dSSong Gao         __put_user(env->fcsr0, &lsx_ctx->fcsr);
20190ea967dSSong Gao     } else {
20290ea967dSSong Gao         struct target_fpu_context *fpu_ctx;
2039d5cd658SSong Gao         info = extctx->fpu.haddr;
20490ea967dSSong Gao 
2059d5cd658SSong Gao         __put_user(FPU_CTX_MAGIC, &info->magic);
2069d5cd658SSong Gao         __put_user(extctx->fpu.size, &info->size);
2079d5cd658SSong Gao 
2089d5cd658SSong Gao         fpu_ctx = (struct target_fpu_context *)(info + 1);
20990ea967dSSong Gao 
2109d5cd658SSong Gao         for (i = 0; i < 32; ++i) {
21190ea967dSSong Gao             __put_user(env->fpr[i].vreg.UD(0), &fpu_ctx->regs[i]);
2129d5cd658SSong Gao         }
2132f149c75SSong Gao         __put_user(read_fcc(env), &fpu_ctx->fcc);
2149d5cd658SSong Gao         __put_user(env->fcsr0, &fpu_ctx->fcsr);
21590ea967dSSong Gao     }
2169d5cd658SSong Gao 
2179d5cd658SSong Gao     /*
2189d5cd658SSong Gao      * Set end context
2199d5cd658SSong Gao      */
2209d5cd658SSong Gao     info = extctx->end.haddr;
2219d5cd658SSong Gao     __put_user(0, &info->magic);
22201714edaSSong Gao     __put_user(0, &info->size);
2239d5cd658SSong Gao }
2249d5cd658SSong Gao 
parse_extcontext(struct extctx_layout * extctx,abi_ptr frame)2259d5cd658SSong Gao static bool parse_extcontext(struct extctx_layout *extctx, abi_ptr frame)
2269d5cd658SSong Gao {
2279d5cd658SSong Gao     memset(extctx, 0, sizeof(*extctx));
2289d5cd658SSong Gao 
2299d5cd658SSong Gao     while (1) {
2307c0ea81cSSong Gao         abi_uint magic, size;
2319d5cd658SSong Gao 
2329d5cd658SSong Gao         if (get_user_u32(magic, frame) || get_user_u32(size, frame + 4)) {
2339d5cd658SSong Gao             return false;
2349d5cd658SSong Gao         }
2359d5cd658SSong Gao 
2369d5cd658SSong Gao         switch (magic) {
2379d5cd658SSong Gao         case 0: /* END */
2389d5cd658SSong Gao             extctx->end.gaddr = frame;
2399d5cd658SSong Gao             extctx->end.size = size;
2409d5cd658SSong Gao             extctx->size += size;
2419d5cd658SSong Gao             return true;
2429d5cd658SSong Gao 
2439d5cd658SSong Gao         case FPU_CTX_MAGIC:
2449d5cd658SSong Gao             if (size < (sizeof(struct target_sctx_info) +
2459d5cd658SSong Gao                         sizeof(struct target_fpu_context))) {
2469d5cd658SSong Gao                 return false;
2479d5cd658SSong Gao             }
2489d5cd658SSong Gao             extctx->fpu.gaddr = frame;
2499d5cd658SSong Gao             extctx->fpu.size = size;
2509d5cd658SSong Gao             extctx->size += size;
2519d5cd658SSong Gao             break;
25290ea967dSSong Gao         case LSX_CTX_MAGIC:
25390ea967dSSong Gao             if (size < (sizeof(struct target_sctx_info) +
25490ea967dSSong Gao                         sizeof(struct target_lsx_context))) {
25590ea967dSSong Gao                 return false;
25690ea967dSSong Gao             }
25790ea967dSSong Gao             extctx->lsx.gaddr = frame;
25890ea967dSSong Gao             extctx->lsx.size = size;
25990ea967dSSong Gao             extctx->size += size;
26090ea967dSSong Gao             break;
261f7077737SSong Gao         case LASX_CTX_MAGIC:
262f7077737SSong Gao             if (size < (sizeof(struct target_sctx_info) +
263f7077737SSong Gao                         sizeof(struct target_lasx_context))) {
264f7077737SSong Gao                 return false;
265f7077737SSong Gao             }
266f7077737SSong Gao             extctx->lasx.gaddr = frame;
267f7077737SSong Gao             extctx->lasx.size = size;
268f7077737SSong Gao             extctx->size += size;
269f7077737SSong Gao             break;
2709d5cd658SSong Gao         default:
2719d5cd658SSong Gao             return false;
2729d5cd658SSong Gao         }
2739d5cd658SSong Gao 
2749d5cd658SSong Gao         frame += size;
2759d5cd658SSong Gao     }
2769d5cd658SSong Gao }
2779d5cd658SSong Gao 
restore_sigframe(CPULoongArchState * env,struct target_sigcontext * sc,struct extctx_layout * extctx)2789d5cd658SSong Gao static void restore_sigframe(CPULoongArchState *env,
2799d5cd658SSong Gao                              struct target_sigcontext *sc,
2809d5cd658SSong Gao                              struct extctx_layout *extctx)
2819d5cd658SSong Gao {
2829d5cd658SSong Gao     int i;
28390ea967dSSong Gao     abi_ulong fcc;
2849d5cd658SSong Gao 
2859d5cd658SSong Gao     __get_user(env->pc, &sc->sc_pc);
2869d5cd658SSong Gao     for (i = 1; i < 32; ++i) {
2879d5cd658SSong Gao         __get_user(env->gpr[i], &sc->sc_regs[i]);
2889d5cd658SSong Gao     }
2899d5cd658SSong Gao 
290f7077737SSong Gao     if (extctx->lasx.haddr) {
291f7077737SSong Gao         struct target_lasx_context *lasx_ctx =
292f7077737SSong Gao             extctx->lasx.haddr + sizeof(struct target_sctx_info);
293f7077737SSong Gao 
294f7077737SSong Gao         for (i = 0; i < 32; ++i) {
295f7077737SSong Gao             __get_user(env->fpr[i].vreg.UD(0), &lasx_ctx->regs[4 * i]);
296f7077737SSong Gao             __get_user(env->fpr[i].vreg.UD(1), &lasx_ctx->regs[4 * i + 1]);
297f7077737SSong Gao             __get_user(env->fpr[i].vreg.UD(2), &lasx_ctx->regs[4 * i + 2]);
298f7077737SSong Gao             __get_user(env->fpr[i].vreg.UD(3), &lasx_ctx->regs[4 * i + 3]);
299f7077737SSong Gao         }
300f7077737SSong Gao         __get_user(fcc, &lasx_ctx->fcc);
301f7077737SSong Gao         write_fcc(env, fcc);
302f7077737SSong Gao         __get_user(env->fcsr0, &lasx_ctx->fcsr);
303f7077737SSong Gao         restore_fp_status(env);
304f7077737SSong Gao     } else if (extctx->lsx.haddr) {
30590ea967dSSong Gao         struct target_lsx_context *lsx_ctx =
30690ea967dSSong Gao             extctx->lsx.haddr + sizeof(struct target_sctx_info);
3079d5cd658SSong Gao 
3089d5cd658SSong Gao         for (i = 0; i < 32; ++i) {
30990ea967dSSong Gao             __get_user(env->fpr[i].vreg.UD(0), &lsx_ctx->regs[2 * i]);
31090ea967dSSong Gao             __get_user(env->fpr[i].vreg.UD(1), &lsx_ctx->regs[2 * i + 1]);
31190ea967dSSong Gao         }
31290ea967dSSong Gao         __get_user(fcc, &lsx_ctx->fcc);
31390ea967dSSong Gao         write_fcc(env, fcc);
31490ea967dSSong Gao         __get_user(env->fcsr0, &lsx_ctx->fcsr);
31590ea967dSSong Gao         restore_fp_status(env);
31690ea967dSSong Gao     } else if (extctx->fpu.haddr) {
31790ea967dSSong Gao         struct target_fpu_context *fpu_ctx =
31890ea967dSSong Gao             extctx->fpu.haddr + sizeof(struct target_sctx_info);
31990ea967dSSong Gao 
32090ea967dSSong Gao         for (i = 0; i < 32; ++i) {
32190ea967dSSong Gao             __get_user(env->fpr[i].vreg.UD(0), &fpu_ctx->regs[i]);
3229d5cd658SSong Gao         }
3239d5cd658SSong Gao         __get_user(fcc, &fpu_ctx->fcc);
3242f149c75SSong Gao         write_fcc(env, fcc);
3259d5cd658SSong Gao         __get_user(env->fcsr0, &fpu_ctx->fcsr);
3269d5cd658SSong Gao         restore_fp_status(env);
3279d5cd658SSong Gao     }
3289d5cd658SSong Gao }
3299d5cd658SSong Gao 
3309d5cd658SSong Gao /*
3319d5cd658SSong Gao  * Determine which stack to use.
3329d5cd658SSong Gao  */
get_sigframe(struct target_sigaction * ka,CPULoongArchState * env,struct extctx_layout * extctx)3339d5cd658SSong Gao static abi_ptr get_sigframe(struct target_sigaction *ka,
3349d5cd658SSong Gao                             CPULoongArchState *env,
3359d5cd658SSong Gao                             struct extctx_layout *extctx)
3369d5cd658SSong Gao {
3379d5cd658SSong Gao     abi_ulong sp;
3389d5cd658SSong Gao 
3399d5cd658SSong Gao     sp = target_sigsp(get_sp_from_cpustate(env), ka);
3409d5cd658SSong Gao     sp = ROUND_DOWN(sp, 16);
34190ea967dSSong Gao     sp = setup_extcontext(env, extctx, sp);
3429d5cd658SSong Gao     sp -= sizeof(struct target_rt_sigframe);
3439d5cd658SSong Gao 
3449d5cd658SSong Gao     assert(QEMU_IS_ALIGNED(sp, 16));
3459d5cd658SSong Gao 
3469d5cd658SSong Gao     return sp;
3479d5cd658SSong Gao }
3489d5cd658SSong Gao 
setup_rt_frame(int sig,struct target_sigaction * ka,target_siginfo_t * info,target_sigset_t * set,CPULoongArchState * env)3499d5cd658SSong Gao void setup_rt_frame(int sig, struct target_sigaction *ka,
3509d5cd658SSong Gao                     target_siginfo_t *info,
3519d5cd658SSong Gao                     target_sigset_t *set, CPULoongArchState *env)
3529d5cd658SSong Gao {
3539d5cd658SSong Gao     struct target_rt_sigframe *frame;
3549d5cd658SSong Gao     struct extctx_layout extctx;
3559d5cd658SSong Gao     abi_ptr frame_addr;
3569d5cd658SSong Gao     int i;
3579d5cd658SSong Gao 
3589d5cd658SSong Gao     frame_addr = get_sigframe(ka, env, &extctx);
3599d5cd658SSong Gao     trace_user_setup_rt_frame(env, frame_addr);
3609d5cd658SSong Gao 
3619d5cd658SSong Gao     frame = lock_user(VERIFY_WRITE, frame_addr,
3629d5cd658SSong Gao                       sizeof(*frame) + extctx.size, 0);
3639d5cd658SSong Gao     if (!frame) {
3649d5cd658SSong Gao         force_sigsegv(sig);
3659d5cd658SSong Gao         return;
3669d5cd658SSong Gao     }
36790ea967dSSong Gao 
368f7077737SSong Gao     if (FIELD_EX64(env->CSR_EUEN, CSR_EUEN, ASXE)) {
369f7077737SSong Gao         extctx.lasx.haddr = (void *)frame + (extctx.lasx.gaddr - frame_addr);
370f7077737SSong Gao         extctx.end.haddr = (void *)frame + (extctx.end.gaddr - frame_addr);
371f7077737SSong Gao     } else if (FIELD_EX64(env->CSR_EUEN, CSR_EUEN, SXE)) {
37290ea967dSSong Gao         extctx.lsx.haddr = (void *)frame + (extctx.lsx.gaddr - frame_addr);
37390ea967dSSong Gao         extctx.end.haddr = (void *)frame + (extctx.end.gaddr - frame_addr);
37490ea967dSSong Gao     } else {
3759d5cd658SSong Gao         extctx.fpu.haddr = (void *)frame + (extctx.fpu.gaddr - frame_addr);
3769d5cd658SSong Gao         extctx.end.haddr = (void *)frame + (extctx.end.gaddr - frame_addr);
37790ea967dSSong Gao     }
3789d5cd658SSong Gao 
379*4d6d8a05SGustavo Romero     frame->rs_info = *info;
3809d5cd658SSong Gao 
3819d5cd658SSong Gao     __put_user(0, &frame->rs_uc.tuc_flags);
3829d5cd658SSong Gao     __put_user(0, &frame->rs_uc.tuc_link);
3839d5cd658SSong Gao     target_save_altstack(&frame->rs_uc.tuc_stack, env);
3849d5cd658SSong Gao 
3859d5cd658SSong Gao     setup_sigframe(env, &frame->rs_uc.tuc_mcontext, &extctx);
3869d5cd658SSong Gao 
3879d5cd658SSong Gao     for (i = 0; i < TARGET_NSIG_WORDS; i++) {
3889d5cd658SSong Gao         __put_user(set->sig[i], &frame->rs_uc.tuc_sigmask.sig[i]);
3899d5cd658SSong Gao     }
3909d5cd658SSong Gao 
3919d5cd658SSong Gao     env->gpr[4] = sig;
3929d5cd658SSong Gao     env->gpr[5] = frame_addr + offsetof(struct target_rt_sigframe, rs_info);
3939d5cd658SSong Gao     env->gpr[6] = frame_addr + offsetof(struct target_rt_sigframe, rs_uc);
3949d5cd658SSong Gao     env->gpr[3] = frame_addr;
3959d5cd658SSong Gao     env->gpr[1] = default_rt_sigreturn;
3969d5cd658SSong Gao 
3979d5cd658SSong Gao     env->pc = ka->_sa_handler;
3989d5cd658SSong Gao     unlock_user(frame, frame_addr, sizeof(*frame) + extctx.size);
3999d5cd658SSong Gao }
4009d5cd658SSong Gao 
do_rt_sigreturn(CPULoongArchState * env)4019d5cd658SSong Gao long do_rt_sigreturn(CPULoongArchState *env)
4029d5cd658SSong Gao {
4039d5cd658SSong Gao     struct target_rt_sigframe *frame;
4049d5cd658SSong Gao     struct extctx_layout extctx;
4059d5cd658SSong Gao     abi_ulong frame_addr;
4069d5cd658SSong Gao     sigset_t blocked;
4079d5cd658SSong Gao 
4089d5cd658SSong Gao     frame_addr = env->gpr[3];
4099d5cd658SSong Gao     trace_user_do_rt_sigreturn(env, frame_addr);
4109d5cd658SSong Gao 
4119d5cd658SSong Gao     if (!parse_extcontext(&extctx, frame_addr + sizeof(*frame))) {
4129d5cd658SSong Gao         goto badframe;
4139d5cd658SSong Gao     }
4149d5cd658SSong Gao 
4159d5cd658SSong Gao     frame = lock_user(VERIFY_READ, frame_addr,
4169d5cd658SSong Gao                       sizeof(*frame) + extctx.size, 1);
4179d5cd658SSong Gao     if (!frame) {
4189d5cd658SSong Gao         goto badframe;
4199d5cd658SSong Gao     }
42090ea967dSSong Gao 
421f7077737SSong Gao     if (extctx.lasx.gaddr) {
422f7077737SSong Gao         extctx.lasx.haddr = (void *)frame + (extctx.lasx.gaddr - frame_addr);
423f7077737SSong Gao     } else if (extctx.lsx.gaddr) {
42490ea967dSSong Gao         extctx.lsx.haddr = (void *)frame + (extctx.lsx.gaddr - frame_addr);
42590ea967dSSong Gao     } else if (extctx.fpu.gaddr) {
4269d5cd658SSong Gao         extctx.fpu.haddr = (void *)frame + (extctx.fpu.gaddr - frame_addr);
4279d5cd658SSong Gao     }
4289d5cd658SSong Gao 
4299d5cd658SSong Gao     target_to_host_sigset(&blocked, &frame->rs_uc.tuc_sigmask);
4309d5cd658SSong Gao     set_sigmask(&blocked);
4319d5cd658SSong Gao 
4329d5cd658SSong Gao     restore_sigframe(env, &frame->rs_uc.tuc_mcontext, &extctx);
4339d5cd658SSong Gao 
4349d5cd658SSong Gao     target_restore_altstack(&frame->rs_uc.tuc_stack, env);
4359d5cd658SSong Gao 
4369d5cd658SSong Gao     unlock_user(frame, frame_addr, 0);
4379d5cd658SSong Gao     return -QEMU_ESIGRETURN;
4389d5cd658SSong Gao 
4399d5cd658SSong Gao  badframe:
4409d5cd658SSong Gao     force_sig(TARGET_SIGSEGV);
4419d5cd658SSong Gao     return -QEMU_ESIGRETURN;
4429d5cd658SSong Gao }
4439d5cd658SSong Gao 
setup_sigtramp(abi_ulong sigtramp_page)4449d5cd658SSong Gao void setup_sigtramp(abi_ulong sigtramp_page)
4459d5cd658SSong Gao {
4469d5cd658SSong Gao     uint32_t *tramp = lock_user(VERIFY_WRITE, sigtramp_page, 8, 0);
4479d5cd658SSong Gao     assert(tramp != NULL);
4489d5cd658SSong Gao 
4499d5cd658SSong Gao     __put_user(0x03822c0b, tramp + 0);  /* ori     a7, zero, 0x8b */
4509d5cd658SSong Gao     __put_user(0x002b0000, tramp + 1);  /* syscall 0 */
4519d5cd658SSong Gao 
4529d5cd658SSong Gao     default_rt_sigreturn = sigtramp_page;
4539d5cd658SSong Gao     unlock_user(tramp, sigtramp_page, 8);
4549d5cd658SSong Gao }
455