Merge remote-tracking branch 'remotes/vivier2/tags/linux-user-for-6.2-pull-request...
[qemu.git] / linux-user / riscv / signal.c
blobf7f33bc90aed79ac7ed9a368742d2f7ae9ee0399
1 /*
2 * Emulation of Linux signals
4 * Copyright (c) 2003 Fabrice Bellard
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.
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.
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/>.
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"
25 /* Signal handler invocation must be transparent for the code being
26 interrupted. Complete CPU (hart) state is saved on entry and restored
27 before returning from the handler. Process sigmask is also saved to block
28 signals while the handler is running. The handler gets its own stack,
29 which also doubles as storage for the CPU state and sigmask.
31 The code below is qemu re-implementation of arch/riscv/kernel/signal.c */
33 struct target_sigcontext {
34 abi_long pc;
35 abi_long gpr[31]; /* x0 is not present, so all offsets must be -1 */
36 uint64_t fpr[32];
37 uint32_t fcsr;
38 }; /* cf. riscv-linux:arch/riscv/include/uapi/asm/ptrace.h */
40 struct target_ucontext {
41 unsigned long uc_flags;
42 struct target_ucontext *uc_link;
43 target_stack_t uc_stack;
44 target_sigset_t uc_sigmask;
45 uint8_t __unused[1024 / 8 - sizeof(target_sigset_t)];
46 struct target_sigcontext uc_mcontext QEMU_ALIGNED(16);
49 struct target_rt_sigframe {
50 uint32_t tramp[2]; /* not in kernel, which uses VDSO instead */
51 struct target_siginfo info;
52 struct target_ucontext uc;
55 static abi_ulong get_sigframe(struct target_sigaction *ka,
56 CPURISCVState *regs, size_t framesize)
58 abi_ulong sp = get_sp_from_cpustate(regs);
60 /* If we are on the alternate signal stack and would overflow it, don't.
61 Return an always-bogus address instead so we will die with SIGSEGV. */
62 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize))) {
63 return -1L;
66 /* This is the X/Open sanctioned signal stack switching. */
67 sp = target_sigsp(sp, ka) - framesize;
69 /* XXX: kernel aligns with 0xf ? */
70 sp &= ~3UL; /* align sp on 4-byte boundary */
72 return sp;
75 static void setup_sigcontext(struct target_sigcontext *sc, CPURISCVState *env)
77 int i;
79 __put_user(env->pc, &sc->pc);
81 for (i = 1; i < 32; i++) {
82 __put_user(env->gpr[i], &sc->gpr[i - 1]);
84 for (i = 0; i < 32; i++) {
85 __put_user(env->fpr[i], &sc->fpr[i]);
88 uint32_t fcsr = riscv_csr_read(env, CSR_FCSR);
89 __put_user(fcsr, &sc->fcsr);
92 static void setup_ucontext(struct target_ucontext *uc,
93 CPURISCVState *env, target_sigset_t *set)
95 __put_user(0, &(uc->uc_flags));
96 __put_user(0, &(uc->uc_link));
98 target_save_altstack(&uc->uc_stack, env);
100 int i;
101 for (i = 0; i < TARGET_NSIG_WORDS; i++) {
102 __put_user(set->sig[i], &(uc->uc_sigmask.sig[i]));
105 setup_sigcontext(&uc->uc_mcontext, env);
108 static inline void install_sigtramp(uint32_t *tramp)
110 __put_user(0x08b00893, tramp + 0); /* li a7, 139 = __NR_rt_sigreturn */
111 __put_user(0x00000073, tramp + 1); /* ecall */
114 void setup_rt_frame(int sig, struct target_sigaction *ka,
115 target_siginfo_t *info,
116 target_sigset_t *set, CPURISCVState *env)
118 abi_ulong frame_addr;
119 struct target_rt_sigframe *frame;
121 frame_addr = get_sigframe(ka, env, sizeof(*frame));
122 trace_user_setup_rt_frame(env, frame_addr);
124 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
125 goto badframe;
128 setup_ucontext(&frame->uc, env, set);
129 tswap_siginfo(&frame->info, info);
130 install_sigtramp(frame->tramp);
132 env->pc = ka->_sa_handler;
133 env->gpr[xSP] = frame_addr;
134 env->gpr[xA0] = sig;
135 env->gpr[xA1] = frame_addr + offsetof(struct target_rt_sigframe, info);
136 env->gpr[xA2] = frame_addr + offsetof(struct target_rt_sigframe, uc);
137 env->gpr[xRA] = frame_addr + offsetof(struct target_rt_sigframe, tramp);
139 return;
141 badframe:
142 unlock_user_struct(frame, frame_addr, 1);
143 if (sig == TARGET_SIGSEGV) {
144 ka->_sa_handler = TARGET_SIG_DFL;
146 force_sig(TARGET_SIGSEGV);
149 static void restore_sigcontext(CPURISCVState *env, struct target_sigcontext *sc)
151 int i;
153 __get_user(env->pc, &sc->pc);
155 for (i = 1; i < 32; ++i) {
156 __get_user(env->gpr[i], &sc->gpr[i - 1]);
158 for (i = 0; i < 32; ++i) {
159 __get_user(env->fpr[i], &sc->fpr[i]);
162 uint32_t fcsr;
163 __get_user(fcsr, &sc->fcsr);
164 riscv_csr_write(env, CSR_FCSR, fcsr);
167 static void restore_ucontext(CPURISCVState *env, struct target_ucontext *uc)
169 sigset_t blocked;
170 target_sigset_t target_set;
171 int i;
173 target_sigemptyset(&target_set);
174 for (i = 0; i < TARGET_NSIG_WORDS; i++) {
175 __get_user(target_set.sig[i], &(uc->uc_sigmask.sig[i]));
178 target_to_host_sigset_internal(&blocked, &target_set);
179 set_sigmask(&blocked);
181 restore_sigcontext(env, &uc->uc_mcontext);
184 long do_rt_sigreturn(CPURISCVState *env)
186 struct target_rt_sigframe *frame;
187 abi_ulong frame_addr;
189 frame_addr = env->gpr[xSP];
190 trace_user_do_sigreturn(env, frame_addr);
191 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
192 goto badframe;
195 restore_ucontext(env, &frame->uc);
196 target_restore_altstack(&frame->uc.uc_stack, env);
198 unlock_user_struct(frame, frame_addr, 0);
199 return -TARGET_QEMU_ESIGRETURN;
201 badframe:
202 unlock_user_struct(frame, frame_addr, 0);
203 force_sig(TARGET_SIGSEGV);
204 return 0;