]> git.proxmox.com Git - mirror_qemu.git/blame - linux-user/riscv/signal.c
Merge tag 'pull-maintainer-may24-160524-2' of https://gitlab.com/stsquad/qemu into...
[mirror_qemu.git] / linux-user / riscv / signal.c
CommitLineData
befb7447
LV
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 */
9c3221c1
LV
19#include "qemu/osdep.h"
20#include "qemu.h"
3b249d26 21#include "user-internals.h"
9c3221c1
LV
22#include "signal-common.h"
23#include "linux-user/trace.h"
468c1bb5 24#include "vdso-asmoffset.h"
9c3221c1
LV
25
26/* Signal handler invocation must be transparent for the code being
27 interrupted. Complete CPU (hart) state is saved on entry and restored
28 before returning from the handler. Process sigmask is also saved to block
29 signals while the handler is running. The handler gets its own stack,
30 which also doubles as storage for the CPU state and sigmask.
31
32 The code below is qemu re-implementation of arch/riscv/kernel/signal.c */
33
34struct target_sigcontext {
35 abi_long pc;
36 abi_long gpr[31]; /* x0 is not present, so all offsets must be -1 */
37 uint64_t fpr[32];
38 uint32_t fcsr;
39}; /* cf. riscv-linux:arch/riscv/include/uapi/asm/ptrace.h */
40
468c1bb5
RH
41QEMU_BUILD_BUG_ON(offsetof(struct target_sigcontext, fpr) != offsetof_freg0);
42
9c3221c1 43struct target_ucontext {
ae7d4d62
LZ
44 abi_ulong uc_flags;
45 abi_ptr uc_link;
9c3221c1 46 target_stack_t uc_stack;
9c3221c1 47 target_sigset_t uc_sigmask;
64ce00a6
LZ
48 uint8_t __unused[1024 / 8 - sizeof(target_sigset_t)];
49 struct target_sigcontext uc_mcontext QEMU_ALIGNED(16);
9c3221c1
LV
50};
51
52struct target_rt_sigframe {
9c3221c1
LV
53 struct target_siginfo info;
54 struct target_ucontext uc;
55};
56
468c1bb5
RH
57QEMU_BUILD_BUG_ON(sizeof(struct target_rt_sigframe)
58 != sizeof_rt_sigframe);
59QEMU_BUILD_BUG_ON(offsetof(struct target_rt_sigframe, uc.uc_mcontext)
60 != offsetof_uc_mcontext);
61
9c3221c1
LV
62static abi_ulong get_sigframe(struct target_sigaction *ka,
63 CPURISCVState *regs, size_t framesize)
64{
465e237b 65 abi_ulong sp = get_sp_from_cpustate(regs);
9c3221c1
LV
66
67 /* If we are on the alternate signal stack and would overflow it, don't.
68 Return an always-bogus address instead so we will die with SIGSEGV. */
465e237b 69 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize))) {
9c3221c1
LV
70 return -1L;
71 }
72
465e237b
LV
73 /* This is the X/Open sanctioned signal stack switching. */
74 sp = target_sigsp(sp, ka) - framesize;
1eaa6342 75 sp &= ~0xf;
465e237b 76
9c3221c1
LV
77 return sp;
78}
79
80static void setup_sigcontext(struct target_sigcontext *sc, CPURISCVState *env)
81{
82 int i;
83
84 __put_user(env->pc, &sc->pc);
85
86 for (i = 1; i < 32; i++) {
87 __put_user(env->gpr[i], &sc->gpr[i - 1]);
88 }
89 for (i = 0; i < 32; i++) {
90 __put_user(env->fpr[i], &sc->fpr[i]);
91 }
92
fb738839 93 uint32_t fcsr = riscv_csr_read(env, CSR_FCSR);
9c3221c1
LV
94 __put_user(fcsr, &sc->fcsr);
95}
96
97static void setup_ucontext(struct target_ucontext *uc,
98 CPURISCVState *env, target_sigset_t *set)
99{
9c3221c1
LV
100 __put_user(0, &(uc->uc_flags));
101 __put_user(0, &(uc->uc_link));
102
465e237b 103 target_save_altstack(&uc->uc_stack, env);
9c3221c1
LV
104
105 int i;
106 for (i = 0; i < TARGET_NSIG_WORDS; i++) {
107 __put_user(set->sig[i], &(uc->uc_sigmask.sig[i]));
108 }
109
110 setup_sigcontext(&uc->uc_mcontext, env);
111}
112
9c3221c1
LV
113void setup_rt_frame(int sig, struct target_sigaction *ka,
114 target_siginfo_t *info,
115 target_sigset_t *set, CPURISCVState *env)
116{
117 abi_ulong frame_addr;
118 struct target_rt_sigframe *frame;
119
120 frame_addr = get_sigframe(ka, env, sizeof(*frame));
121 trace_user_setup_rt_frame(env, frame_addr);
122
123 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
124 goto badframe;
125 }
126
127 setup_ucontext(&frame->uc, env, set);
4d6d8a05 128 frame->info = *info;
9c3221c1
LV
129
130 env->pc = ka->_sa_handler;
131 env->gpr[xSP] = frame_addr;
132 env->gpr[xA0] = sig;
133 env->gpr[xA1] = frame_addr + offsetof(struct target_rt_sigframe, info);
134 env->gpr[xA2] = frame_addr + offsetof(struct target_rt_sigframe, uc);
3c62b5d2 135 env->gpr[xRA] = default_rt_sigreturn;
9c3221c1
LV
136
137 return;
138
139badframe:
140 unlock_user_struct(frame, frame_addr, 1);
141 if (sig == TARGET_SIGSEGV) {
142 ka->_sa_handler = TARGET_SIG_DFL;
143 }
144 force_sig(TARGET_SIGSEGV);
145}
146
147static void restore_sigcontext(CPURISCVState *env, struct target_sigcontext *sc)
148{
149 int i;
150
151 __get_user(env->pc, &sc->pc);
152
153 for (i = 1; i < 32; ++i) {
154 __get_user(env->gpr[i], &sc->gpr[i - 1]);
155 }
156 for (i = 0; i < 32; ++i) {
157 __get_user(env->fpr[i], &sc->fpr[i]);
158 }
159
160 uint32_t fcsr;
161 __get_user(fcsr, &sc->fcsr);
fb738839 162 riscv_csr_write(env, CSR_FCSR, fcsr);
9c3221c1
LV
163}
164
165static void restore_ucontext(CPURISCVState *env, struct target_ucontext *uc)
166{
167 sigset_t blocked;
168 target_sigset_t target_set;
169 int i;
170
171 target_sigemptyset(&target_set);
172 for (i = 0; i < TARGET_NSIG_WORDS; i++) {
173 __get_user(target_set.sig[i], &(uc->uc_sigmask.sig[i]));
174 }
175
176 target_to_host_sigset_internal(&blocked, &target_set);
177 set_sigmask(&blocked);
178
179 restore_sigcontext(env, &uc->uc_mcontext);
180}
181
182long do_rt_sigreturn(CPURISCVState *env)
183{
184 struct target_rt_sigframe *frame;
185 abi_ulong frame_addr;
186
187 frame_addr = env->gpr[xSP];
188 trace_user_do_sigreturn(env, frame_addr);
189 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
190 goto badframe;
191 }
192
193 restore_ucontext(env, &frame->uc);
ddc3e74d 194 target_restore_altstack(&frame->uc.uc_stack, env);
9c3221c1
LV
195
196 unlock_user_struct(frame, frame_addr, 0);
57a0c938 197 return -QEMU_ESIGRETURN;
9c3221c1
LV
198
199badframe:
200 unlock_user_struct(frame, frame_addr, 0);
201 force_sig(TARGET_SIGSEGV);
202 return 0;
203}
3c62b5d2
RH
204
205void setup_sigtramp(abi_ulong sigtramp_page)
206{
207 uint32_t *tramp = lock_user(VERIFY_WRITE, sigtramp_page, 8, 0);
208 assert(tramp != NULL);
209
210 __put_user(0x08b00893, tramp + 0); /* li a7, 139 = __NR_rt_sigreturn */
211 __put_user(0x00000073, tramp + 1); /* ecall */
212
213 default_rt_sigreturn = sigtramp_page;
214 unlock_user(tramp, sigtramp_page, 8);
215}