xref: /openbmc/qemu/linux-user/riscv/signal.c (revision f14eced5)
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 /* 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.
30 
31    The code below is qemu re-implementation of arch/riscv/kernel/signal.c */
32 
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 */
39 
40 struct target_ucontext {
41     abi_ulong uc_flags;
42     abi_ptr 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);
47 };
48 
49 struct target_rt_sigframe {
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     sp &= ~0xf;
68 
69     return sp;
70 }
71 
72 static void setup_sigcontext(struct target_sigcontext *sc, CPURISCVState *env)
73 {
74     int i;
75 
76     __put_user(env->pc, &sc->pc);
77 
78     for (i = 1; i < 32; i++) {
79         __put_user(env->gpr[i], &sc->gpr[i - 1]);
80     }
81     for (i = 0; i < 32; i++) {
82         __put_user(env->fpr[i], &sc->fpr[i]);
83     }
84 
85     uint32_t fcsr = riscv_csr_read(env, CSR_FCSR);
86     __put_user(fcsr, &sc->fcsr);
87 }
88 
89 static void setup_ucontext(struct target_ucontext *uc,
90                            CPURISCVState *env, target_sigset_t *set)
91 {
92     __put_user(0,    &(uc->uc_flags));
93     __put_user(0,    &(uc->uc_link));
94 
95     target_save_altstack(&uc->uc_stack, env);
96 
97     int i;
98     for (i = 0; i < TARGET_NSIG_WORDS; i++) {
99         __put_user(set->sig[i], &(uc->uc_sigmask.sig[i]));
100     }
101 
102     setup_sigcontext(&uc->uc_mcontext, env);
103 }
104 
105 void setup_rt_frame(int sig, struct target_sigaction *ka,
106                     target_siginfo_t *info,
107                     target_sigset_t *set, CPURISCVState *env)
108 {
109     abi_ulong frame_addr;
110     struct target_rt_sigframe *frame;
111 
112     frame_addr = get_sigframe(ka, env, sizeof(*frame));
113     trace_user_setup_rt_frame(env, frame_addr);
114 
115     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
116         goto badframe;
117     }
118 
119     setup_ucontext(&frame->uc, env, set);
120     tswap_siginfo(&frame->info, info);
121 
122     env->pc = ka->_sa_handler;
123     env->gpr[xSP] = frame_addr;
124     env->gpr[xA0] = sig;
125     env->gpr[xA1] = frame_addr + offsetof(struct target_rt_sigframe, info);
126     env->gpr[xA2] = frame_addr + offsetof(struct target_rt_sigframe, uc);
127     env->gpr[xRA] = default_rt_sigreturn;
128 
129     return;
130 
131 badframe:
132     unlock_user_struct(frame, frame_addr, 1);
133     if (sig == TARGET_SIGSEGV) {
134         ka->_sa_handler = TARGET_SIG_DFL;
135     }
136     force_sig(TARGET_SIGSEGV);
137 }
138 
139 static void restore_sigcontext(CPURISCVState *env, struct target_sigcontext *sc)
140 {
141     int i;
142 
143     __get_user(env->pc, &sc->pc);
144 
145     for (i = 1; i < 32; ++i) {
146         __get_user(env->gpr[i], &sc->gpr[i - 1]);
147     }
148     for (i = 0; i < 32; ++i) {
149         __get_user(env->fpr[i], &sc->fpr[i]);
150     }
151 
152     uint32_t fcsr;
153     __get_user(fcsr, &sc->fcsr);
154     riscv_csr_write(env, CSR_FCSR, fcsr);
155 }
156 
157 static void restore_ucontext(CPURISCVState *env, struct target_ucontext *uc)
158 {
159     sigset_t blocked;
160     target_sigset_t target_set;
161     int i;
162 
163     target_sigemptyset(&target_set);
164     for (i = 0; i < TARGET_NSIG_WORDS; i++) {
165         __get_user(target_set.sig[i], &(uc->uc_sigmask.sig[i]));
166     }
167 
168     target_to_host_sigset_internal(&blocked, &target_set);
169     set_sigmask(&blocked);
170 
171     restore_sigcontext(env, &uc->uc_mcontext);
172 }
173 
174 long do_rt_sigreturn(CPURISCVState *env)
175 {
176     struct target_rt_sigframe *frame;
177     abi_ulong frame_addr;
178 
179     frame_addr = env->gpr[xSP];
180     trace_user_do_sigreturn(env, frame_addr);
181     if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
182         goto badframe;
183     }
184 
185     restore_ucontext(env, &frame->uc);
186     target_restore_altstack(&frame->uc.uc_stack, env);
187 
188     unlock_user_struct(frame, frame_addr, 0);
189     return -QEMU_ESIGRETURN;
190 
191 badframe:
192     unlock_user_struct(frame, frame_addr, 0);
193     force_sig(TARGET_SIGSEGV);
194     return 0;
195 }
196 
197 void setup_sigtramp(abi_ulong sigtramp_page)
198 {
199     uint32_t *tramp = lock_user(VERIFY_WRITE, sigtramp_page, 8, 0);
200     assert(tramp != NULL);
201 
202     __put_user(0x08b00893, tramp + 0);  /* li a7, 139 = __NR_rt_sigreturn */
203     __put_user(0x00000073, tramp + 1);  /* ecall */
204 
205     default_rt_sigreturn = sigtramp_page;
206     unlock_user(tramp, sigtramp_page, 8);
207 }
208