xref: /openbmc/qemu/linux-user/riscv/signal.c (revision 0ed93f4c)
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 "signal-common.h"
22 #include "linux-user/trace.h"
23 
24 /* Signal handler invocation must be transparent for the code being
25    interrupted. Complete CPU (hart) state is saved on entry and restored
26    before returning from the handler. Process sigmask is also saved to block
27    signals while the handler is running. The handler gets its own stack,
28    which also doubles as storage for the CPU state and sigmask.
29 
30    The code below is qemu re-implementation of arch/riscv/kernel/signal.c */
31 
32 struct target_sigcontext {
33     abi_long pc;
34     abi_long gpr[31]; /* x0 is not present, so all offsets must be -1 */
35     uint64_t fpr[32];
36     uint32_t fcsr;
37 }; /* cf. riscv-linux:arch/riscv/include/uapi/asm/ptrace.h */
38 
39 struct target_ucontext {
40     unsigned long uc_flags;
41     struct target_ucontext *uc_link;
42     target_stack_t uc_stack;
43     target_sigset_t uc_sigmask;
44     uint8_t   __unused[1024 / 8 - sizeof(target_sigset_t)];
45     struct target_sigcontext uc_mcontext QEMU_ALIGNED(16);
46 };
47 
48 struct target_rt_sigframe {
49     uint32_t tramp[2]; /* not in kernel, which uses VDSO instead */
50     struct target_siginfo info;
51     struct target_ucontext uc;
52 };
53 
54 static abi_ulong get_sigframe(struct target_sigaction *ka,
55                               CPURISCVState *regs, size_t framesize)
56 {
57     abi_ulong sp = get_sp_from_cpustate(regs);
58 
59     /* If we are on the alternate signal stack and would overflow it, don't.
60        Return an always-bogus address instead so we will die with SIGSEGV. */
61     if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize))) {
62         return -1L;
63     }
64 
65     /* This is the X/Open sanctioned signal stack switching.  */
66     sp = target_sigsp(sp, ka) - framesize;
67 
68     /* XXX: kernel aligns with 0xf ? */
69     sp &= ~3UL; /* align sp on 4-byte boundary */
70 
71     return sp;
72 }
73 
74 static void setup_sigcontext(struct target_sigcontext *sc, CPURISCVState *env)
75 {
76     int i;
77 
78     __put_user(env->pc, &sc->pc);
79 
80     for (i = 1; i < 32; i++) {
81         __put_user(env->gpr[i], &sc->gpr[i - 1]);
82     }
83     for (i = 0; i < 32; i++) {
84         __put_user(env->fpr[i], &sc->fpr[i]);
85     }
86 
87     uint32_t fcsr = riscv_csr_read(env, CSR_FCSR);
88     __put_user(fcsr, &sc->fcsr);
89 }
90 
91 static void setup_ucontext(struct target_ucontext *uc,
92                            CPURISCVState *env, target_sigset_t *set)
93 {
94     __put_user(0,    &(uc->uc_flags));
95     __put_user(0,    &(uc->uc_link));
96 
97     target_save_altstack(&uc->uc_stack, env);
98 
99     int i;
100     for (i = 0; i < TARGET_NSIG_WORDS; i++) {
101         __put_user(set->sig[i], &(uc->uc_sigmask.sig[i]));
102     }
103 
104     setup_sigcontext(&uc->uc_mcontext, env);
105 }
106 
107 static inline void install_sigtramp(uint32_t *tramp)
108 {
109     __put_user(0x08b00893, tramp + 0);  /* li a7, 139 = __NR_rt_sigreturn */
110     __put_user(0x00000073, tramp + 1);  /* ecall */
111 }
112 
113 void 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);
128     tswap_siginfo(&frame->info, info);
129     install_sigtramp(frame->tramp);
130 
131     env->pc = ka->_sa_handler;
132     env->gpr[xSP] = frame_addr;
133     env->gpr[xA0] = sig;
134     env->gpr[xA1] = frame_addr + offsetof(struct target_rt_sigframe, info);
135     env->gpr[xA2] = frame_addr + offsetof(struct target_rt_sigframe, uc);
136     env->gpr[xRA] = frame_addr + offsetof(struct target_rt_sigframe, tramp);
137 
138     return;
139 
140 badframe:
141     unlock_user_struct(frame, frame_addr, 1);
142     if (sig == TARGET_SIGSEGV) {
143         ka->_sa_handler = TARGET_SIG_DFL;
144     }
145     force_sig(TARGET_SIGSEGV);
146 }
147 
148 static void restore_sigcontext(CPURISCVState *env, struct target_sigcontext *sc)
149 {
150     int i;
151 
152     __get_user(env->pc, &sc->pc);
153 
154     for (i = 1; i < 32; ++i) {
155         __get_user(env->gpr[i], &sc->gpr[i - 1]);
156     }
157     for (i = 0; i < 32; ++i) {
158         __get_user(env->fpr[i], &sc->fpr[i]);
159     }
160 
161     uint32_t fcsr;
162     __get_user(fcsr, &sc->fcsr);
163     riscv_csr_write(env, CSR_FCSR, fcsr);
164 }
165 
166 static void restore_ucontext(CPURISCVState *env, struct target_ucontext *uc)
167 {
168     sigset_t blocked;
169     target_sigset_t target_set;
170     int i;
171 
172     target_sigemptyset(&target_set);
173     for (i = 0; i < TARGET_NSIG_WORDS; i++) {
174         __get_user(target_set.sig[i], &(uc->uc_sigmask.sig[i]));
175     }
176 
177     target_to_host_sigset_internal(&blocked, &target_set);
178     set_sigmask(&blocked);
179 
180     restore_sigcontext(env, &uc->uc_mcontext);
181 }
182 
183 long do_rt_sigreturn(CPURISCVState *env)
184 {
185     struct target_rt_sigframe *frame;
186     abi_ulong frame_addr;
187 
188     frame_addr = env->gpr[xSP];
189     trace_user_do_sigreturn(env, frame_addr);
190     if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
191         goto badframe;
192     }
193 
194     restore_ucontext(env, &frame->uc);
195 
196     if (do_sigaltstack(frame_addr + offsetof(struct target_rt_sigframe,
197             uc.uc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT) {
198         goto badframe;
199     }
200 
201     unlock_user_struct(frame, frame_addr, 0);
202     return -TARGET_QEMU_ESIGRETURN;
203 
204 badframe:
205     unlock_user_struct(frame, frame_addr, 0);
206     force_sig(TARGET_SIGSEGV);
207     return 0;
208 }
209