Loading...
1/*
2 * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
3 * Chen Liqin <liqin.chen@sunplusct.com>
4 * Lennox Wu <lennox.wu@sunplusct.com>
5 * Copyright (C) 2012 Regents of the University of California
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see the file COPYING, or write
19 * to the Free Software Foundation, Inc.,
20 */
21
22#include <linux/signal.h>
23#include <linux/uaccess.h>
24#include <linux/syscalls.h>
25#include <linux/tracehook.h>
26#include <linux/linkage.h>
27
28#include <asm/ucontext.h>
29#include <asm/vdso.h>
30#include <asm/switch_to.h>
31#include <asm/csr.h>
32
33#define DEBUG_SIG 0
34
35struct rt_sigframe {
36 struct siginfo info;
37 struct ucontext uc;
38};
39
40static long restore_d_state(struct pt_regs *regs,
41 struct __riscv_d_ext_state __user *state)
42{
43 long err;
44 err = __copy_from_user(¤t->thread.fstate, state, sizeof(*state));
45 if (likely(!err))
46 fstate_restore(current, regs);
47 return err;
48}
49
50static long save_d_state(struct pt_regs *regs,
51 struct __riscv_d_ext_state __user *state)
52{
53 fstate_save(current, regs);
54 return __copy_to_user(state, ¤t->thread.fstate, sizeof(*state));
55}
56
57static long restore_sigcontext(struct pt_regs *regs,
58 struct sigcontext __user *sc)
59{
60 long err;
61 size_t i;
62 /* sc_regs is structured the same as the start of pt_regs */
63 err = __copy_from_user(regs, &sc->sc_regs, sizeof(sc->sc_regs));
64 if (unlikely(err))
65 return err;
66 /* Restore the floating-point state. */
67 err = restore_d_state(regs, &sc->sc_fpregs.d);
68 if (unlikely(err))
69 return err;
70 /* We support no other extension state at this time. */
71 for (i = 0; i < ARRAY_SIZE(sc->sc_fpregs.q.reserved); i++) {
72 u32 value;
73 err = __get_user(value, &sc->sc_fpregs.q.reserved[i]);
74 if (unlikely(err))
75 break;
76 if (value != 0)
77 return -EINVAL;
78 }
79 return err;
80}
81
82SYSCALL_DEFINE0(rt_sigreturn)
83{
84 struct pt_regs *regs = current_pt_regs();
85 struct rt_sigframe __user *frame;
86 struct task_struct *task;
87 sigset_t set;
88
89 /* Always make any pending restarted system calls return -EINTR */
90 current->restart_block.fn = do_no_restart_syscall;
91
92 frame = (struct rt_sigframe __user *)regs->sp;
93
94 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
95 goto badframe;
96
97 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
98 goto badframe;
99
100 set_current_blocked(&set);
101
102 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
103 goto badframe;
104
105 if (restore_altstack(&frame->uc.uc_stack))
106 goto badframe;
107
108 return regs->a0;
109
110badframe:
111 task = current;
112 if (show_unhandled_signals) {
113 pr_info_ratelimited(
114 "%s[%d]: bad frame in %s: frame=%p pc=%p sp=%p\n",
115 task->comm, task_pid_nr(task), __func__,
116 frame, (void *)regs->sepc, (void *)regs->sp);
117 }
118 force_sig(SIGSEGV, task);
119 return 0;
120}
121
122static long setup_sigcontext(struct rt_sigframe __user *frame,
123 struct pt_regs *regs)
124{
125 struct sigcontext __user *sc = &frame->uc.uc_mcontext;
126 long err;
127 size_t i;
128 /* sc_regs is structured the same as the start of pt_regs */
129 err = __copy_to_user(&sc->sc_regs, regs, sizeof(sc->sc_regs));
130 /* Save the floating-point state. */
131 err |= save_d_state(regs, &sc->sc_fpregs.d);
132 /* We support no other extension state at this time. */
133 for (i = 0; i < ARRAY_SIZE(sc->sc_fpregs.q.reserved); i++)
134 err |= __put_user(0, &sc->sc_fpregs.q.reserved[i]);
135 return err;
136}
137
138static inline void __user *get_sigframe(struct ksignal *ksig,
139 struct pt_regs *regs, size_t framesize)
140{
141 unsigned long sp;
142 /* Default to using normal stack */
143 sp = regs->sp;
144
145 /*
146 * If we are on the alternate signal stack and would overflow it, don't.
147 * Return an always-bogus address instead so we will die with SIGSEGV.
148 */
149 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
150 return (void __user __force *)(-1UL);
151
152 /* This is the X/Open sanctioned signal stack switching. */
153 sp = sigsp(sp, ksig) - framesize;
154
155 /* Align the stack frame. */
156 sp &= ~0xfUL;
157
158 return (void __user *)sp;
159}
160
161
162static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
163 struct pt_regs *regs)
164{
165 struct rt_sigframe __user *frame;
166 long err = 0;
167
168 frame = get_sigframe(ksig, regs, sizeof(*frame));
169 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
170 return -EFAULT;
171
172 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
173
174 /* Create the ucontext. */
175 err |= __put_user(0, &frame->uc.uc_flags);
176 err |= __put_user(NULL, &frame->uc.uc_link);
177 err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
178 err |= setup_sigcontext(frame, regs);
179 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
180 if (err)
181 return -EFAULT;
182
183 /* Set up to return from userspace. */
184 regs->ra = (unsigned long)VDSO_SYMBOL(
185 current->mm->context.vdso, rt_sigreturn);
186
187 /*
188 * Set up registers for signal handler.
189 * Registers that we don't modify keep the value they had from
190 * user-space at the time we took the signal.
191 * We always pass siginfo and mcontext, regardless of SA_SIGINFO,
192 * since some things rely on this (e.g. glibc's debug/segfault.c).
193 */
194 regs->sepc = (unsigned long)ksig->ka.sa.sa_handler;
195 regs->sp = (unsigned long)frame;
196 regs->a0 = ksig->sig; /* a0: signal number */
197 regs->a1 = (unsigned long)(&frame->info); /* a1: siginfo pointer */
198 regs->a2 = (unsigned long)(&frame->uc); /* a2: ucontext pointer */
199
200#if DEBUG_SIG
201 pr_info("SIG deliver (%s:%d): sig=%d pc=%p ra=%p sp=%p\n",
202 current->comm, task_pid_nr(current), ksig->sig,
203 (void *)regs->sepc, (void *)regs->ra, frame);
204#endif
205
206 return 0;
207}
208
209static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
210{
211 sigset_t *oldset = sigmask_to_save();
212 int ret;
213
214 /* Are we from a system call? */
215 if (regs->scause == EXC_SYSCALL) {
216 /* If so, check system call restarting.. */
217 switch (regs->a0) {
218 case -ERESTART_RESTARTBLOCK:
219 case -ERESTARTNOHAND:
220 regs->a0 = -EINTR;
221 break;
222
223 case -ERESTARTSYS:
224 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
225 regs->a0 = -EINTR;
226 break;
227 }
228 /* fallthrough */
229 case -ERESTARTNOINTR:
230 regs->a0 = regs->orig_a0;
231 regs->sepc -= 0x4;
232 break;
233 }
234 }
235
236 /* Set up the stack frame */
237 ret = setup_rt_frame(ksig, oldset, regs);
238
239 signal_setup_done(ret, ksig, 0);
240}
241
242static void do_signal(struct pt_regs *regs)
243{
244 struct ksignal ksig;
245
246 if (get_signal(&ksig)) {
247 /* Actually deliver the signal */
248 handle_signal(&ksig, regs);
249 return;
250 }
251
252 /* Did we come from a system call? */
253 if (regs->scause == EXC_SYSCALL) {
254 /* Restart the system call - no handlers present */
255 switch (regs->a0) {
256 case -ERESTARTNOHAND:
257 case -ERESTARTSYS:
258 case -ERESTARTNOINTR:
259 regs->a0 = regs->orig_a0;
260 regs->sepc -= 0x4;
261 break;
262 case -ERESTART_RESTARTBLOCK:
263 regs->a0 = regs->orig_a0;
264 regs->a7 = __NR_restart_syscall;
265 regs->sepc -= 0x4;
266 break;
267 }
268 }
269
270 /*
271 * If there is no signal to deliver, we just put the saved
272 * sigmask back.
273 */
274 restore_saved_sigmask();
275}
276
277/*
278 * notification of userspace execution resumption
279 * - triggered by the _TIF_WORK_MASK flags
280 */
281asmlinkage void do_notify_resume(struct pt_regs *regs,
282 unsigned long thread_info_flags)
283{
284 /* Handle pending signal delivery */
285 if (thread_info_flags & _TIF_SIGPENDING)
286 do_signal(regs);
287
288 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
289 clear_thread_flag(TIF_NOTIFY_RESUME);
290 tracehook_notify_resume(regs);
291 }
292}
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
4 * Chen Liqin <liqin.chen@sunplusct.com>
5 * Lennox Wu <lennox.wu@sunplusct.com>
6 * Copyright (C) 2012 Regents of the University of California
7 */
8
9#include <linux/compat.h>
10#include <linux/signal.h>
11#include <linux/uaccess.h>
12#include <linux/syscalls.h>
13#include <linux/resume_user_mode.h>
14#include <linux/linkage.h>
15#include <linux/entry-common.h>
16
17#include <asm/ucontext.h>
18#include <asm/vdso.h>
19#include <asm/signal.h>
20#include <asm/signal32.h>
21#include <asm/switch_to.h>
22#include <asm/vector.h>
23#include <asm/csr.h>
24#include <asm/cacheflush.h>
25
26unsigned long signal_minsigstksz __ro_after_init;
27
28extern u32 __user_rt_sigreturn[2];
29static size_t riscv_v_sc_size __ro_after_init;
30
31#define DEBUG_SIG 0
32
33struct rt_sigframe {
34 struct siginfo info;
35 struct ucontext uc;
36#ifndef CONFIG_MMU
37 u32 sigreturn_code[2];
38#endif
39};
40
41#ifdef CONFIG_FPU
42static long restore_fp_state(struct pt_regs *regs,
43 union __riscv_fp_state __user *sc_fpregs)
44{
45 long err;
46 struct __riscv_d_ext_state __user *state = &sc_fpregs->d;
47
48 err = __copy_from_user(¤t->thread.fstate, state, sizeof(*state));
49 if (unlikely(err))
50 return err;
51
52 fstate_restore(current, regs);
53 return 0;
54}
55
56static long save_fp_state(struct pt_regs *regs,
57 union __riscv_fp_state __user *sc_fpregs)
58{
59 long err;
60 struct __riscv_d_ext_state __user *state = &sc_fpregs->d;
61
62 fstate_save(current, regs);
63 err = __copy_to_user(state, ¤t->thread.fstate, sizeof(*state));
64 return err;
65}
66#else
67#define save_fp_state(task, regs) (0)
68#define restore_fp_state(task, regs) (0)
69#endif
70
71#ifdef CONFIG_RISCV_ISA_V
72
73static long save_v_state(struct pt_regs *regs, void __user **sc_vec)
74{
75 struct __riscv_ctx_hdr __user *hdr;
76 struct __sc_riscv_v_state __user *state;
77 void __user *datap;
78 long err;
79
80 hdr = *sc_vec;
81 /* Place state to the user's signal context space after the hdr */
82 state = (struct __sc_riscv_v_state __user *)(hdr + 1);
83 /* Point datap right after the end of __sc_riscv_v_state */
84 datap = state + 1;
85
86 /* datap is designed to be 16 byte aligned for better performance */
87 WARN_ON(!IS_ALIGNED((unsigned long)datap, 16));
88
89 get_cpu_vector_context();
90 riscv_v_vstate_save(¤t->thread.vstate, regs);
91 put_cpu_vector_context();
92
93 /* Copy everything of vstate but datap. */
94 err = __copy_to_user(&state->v_state, ¤t->thread.vstate,
95 offsetof(struct __riscv_v_ext_state, datap));
96 /* Copy the pointer datap itself. */
97 err |= __put_user((__force void *)datap, &state->v_state.datap);
98 /* Copy the whole vector content to user space datap. */
99 err |= __copy_to_user(datap, current->thread.vstate.datap, riscv_v_vsize);
100 /* Copy magic to the user space after saving all vector conetext */
101 err |= __put_user(RISCV_V_MAGIC, &hdr->magic);
102 err |= __put_user(riscv_v_sc_size, &hdr->size);
103 if (unlikely(err))
104 return err;
105
106 /* Only progress the sv_vec if everything has done successfully */
107 *sc_vec += riscv_v_sc_size;
108 return 0;
109}
110
111/*
112 * Restore Vector extension context from the user's signal frame. This function
113 * assumes a valid extension header. So magic and size checking must be done by
114 * the caller.
115 */
116static long __restore_v_state(struct pt_regs *regs, void __user *sc_vec)
117{
118 long err;
119 struct __sc_riscv_v_state __user *state = sc_vec;
120 void __user *datap;
121
122 /*
123 * Mark the vstate as clean prior performing the actual copy,
124 * to avoid getting the vstate incorrectly clobbered by the
125 * discarded vector state.
126 */
127 riscv_v_vstate_set_restore(current, regs);
128
129 /* Copy everything of __sc_riscv_v_state except datap. */
130 err = __copy_from_user(¤t->thread.vstate, &state->v_state,
131 offsetof(struct __riscv_v_ext_state, datap));
132 if (unlikely(err))
133 return err;
134
135 /* Copy the pointer datap itself. */
136 err = __get_user(datap, &state->v_state.datap);
137 if (unlikely(err))
138 return err;
139 /*
140 * Copy the whole vector content from user space datap. Use
141 * copy_from_user to prevent information leak.
142 */
143 return copy_from_user(current->thread.vstate.datap, datap, riscv_v_vsize);
144}
145#else
146#define save_v_state(task, regs) (0)
147#define __restore_v_state(task, regs) (0)
148#endif
149
150static long restore_sigcontext(struct pt_regs *regs,
151 struct sigcontext __user *sc)
152{
153 void __user *sc_ext_ptr = &sc->sc_extdesc.hdr;
154 __u32 rsvd;
155 long err;
156 /* sc_regs is structured the same as the start of pt_regs */
157 err = __copy_from_user(regs, &sc->sc_regs, sizeof(sc->sc_regs));
158 if (unlikely(err))
159 return err;
160
161 /* Restore the floating-point state. */
162 if (has_fpu()) {
163 err = restore_fp_state(regs, &sc->sc_fpregs);
164 if (unlikely(err))
165 return err;
166 }
167
168 /* Check the reserved word before extensions parsing */
169 err = __get_user(rsvd, &sc->sc_extdesc.reserved);
170 if (unlikely(err))
171 return err;
172 if (unlikely(rsvd))
173 return -EINVAL;
174
175 while (!err) {
176 __u32 magic, size;
177 struct __riscv_ctx_hdr __user *head = sc_ext_ptr;
178
179 err |= __get_user(magic, &head->magic);
180 err |= __get_user(size, &head->size);
181 if (unlikely(err))
182 return err;
183
184 sc_ext_ptr += sizeof(*head);
185 switch (magic) {
186 case END_MAGIC:
187 if (size != END_HDR_SIZE)
188 return -EINVAL;
189
190 return 0;
191 case RISCV_V_MAGIC:
192 if (!has_vector() || !riscv_v_vstate_query(regs) ||
193 size != riscv_v_sc_size)
194 return -EINVAL;
195
196 err = __restore_v_state(regs, sc_ext_ptr);
197 break;
198 default:
199 return -EINVAL;
200 }
201 sc_ext_ptr = (void __user *)head + size;
202 }
203 return err;
204}
205
206static size_t get_rt_frame_size(bool cal_all)
207{
208 struct rt_sigframe __user *frame;
209 size_t frame_size;
210 size_t total_context_size = 0;
211
212 frame_size = sizeof(*frame);
213
214 if (has_vector()) {
215 if (cal_all || riscv_v_vstate_query(task_pt_regs(current)))
216 total_context_size += riscv_v_sc_size;
217 }
218
219 frame_size += total_context_size;
220
221 frame_size = round_up(frame_size, 16);
222 return frame_size;
223}
224
225SYSCALL_DEFINE0(rt_sigreturn)
226{
227 struct pt_regs *regs = current_pt_regs();
228 struct rt_sigframe __user *frame;
229 struct task_struct *task;
230 sigset_t set;
231 size_t frame_size = get_rt_frame_size(false);
232
233 /* Always make any pending restarted system calls return -EINTR */
234 current->restart_block.fn = do_no_restart_syscall;
235
236 frame = (struct rt_sigframe __user *)regs->sp;
237
238 if (!access_ok(frame, frame_size))
239 goto badframe;
240
241 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
242 goto badframe;
243
244 set_current_blocked(&set);
245
246 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
247 goto badframe;
248
249 if (restore_altstack(&frame->uc.uc_stack))
250 goto badframe;
251
252 regs->cause = -1UL;
253
254 return regs->a0;
255
256badframe:
257 task = current;
258 if (show_unhandled_signals) {
259 pr_info_ratelimited(
260 "%s[%d]: bad frame in %s: frame=%p pc=%p sp=%p\n",
261 task->comm, task_pid_nr(task), __func__,
262 frame, (void *)regs->epc, (void *)regs->sp);
263 }
264 force_sig(SIGSEGV);
265 return 0;
266}
267
268static long setup_sigcontext(struct rt_sigframe __user *frame,
269 struct pt_regs *regs)
270{
271 struct sigcontext __user *sc = &frame->uc.uc_mcontext;
272 struct __riscv_ctx_hdr __user *sc_ext_ptr = &sc->sc_extdesc.hdr;
273 long err;
274
275 /* sc_regs is structured the same as the start of pt_regs */
276 err = __copy_to_user(&sc->sc_regs, regs, sizeof(sc->sc_regs));
277 /* Save the floating-point state. */
278 if (has_fpu())
279 err |= save_fp_state(regs, &sc->sc_fpregs);
280 /* Save the vector state. */
281 if (has_vector() && riscv_v_vstate_query(regs))
282 err |= save_v_state(regs, (void __user **)&sc_ext_ptr);
283 /* Write zero to fp-reserved space and check it on restore_sigcontext */
284 err |= __put_user(0, &sc->sc_extdesc.reserved);
285 /* And put END __riscv_ctx_hdr at the end. */
286 err |= __put_user(END_MAGIC, &sc_ext_ptr->magic);
287 err |= __put_user(END_HDR_SIZE, &sc_ext_ptr->size);
288
289 return err;
290}
291
292static inline void __user *get_sigframe(struct ksignal *ksig,
293 struct pt_regs *regs, size_t framesize)
294{
295 unsigned long sp;
296 /* Default to using normal stack */
297 sp = regs->sp;
298
299 /*
300 * If we are on the alternate signal stack and would overflow it, don't.
301 * Return an always-bogus address instead so we will die with SIGSEGV.
302 */
303 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
304 return (void __user __force *)(-1UL);
305
306 /* This is the X/Open sanctioned signal stack switching. */
307 sp = sigsp(sp, ksig) - framesize;
308
309 /* Align the stack frame. */
310 sp &= ~0xfUL;
311
312 return (void __user *)sp;
313}
314
315static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
316 struct pt_regs *regs)
317{
318 struct rt_sigframe __user *frame;
319 long err = 0;
320 unsigned long __maybe_unused addr;
321 size_t frame_size = get_rt_frame_size(false);
322
323 frame = get_sigframe(ksig, regs, frame_size);
324 if (!access_ok(frame, frame_size))
325 return -EFAULT;
326
327 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
328
329 /* Create the ucontext. */
330 err |= __put_user(0, &frame->uc.uc_flags);
331 err |= __put_user(NULL, &frame->uc.uc_link);
332 err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
333 err |= setup_sigcontext(frame, regs);
334 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
335 if (err)
336 return -EFAULT;
337
338 /* Set up to return from userspace. */
339#ifdef CONFIG_MMU
340 regs->ra = (unsigned long)VDSO_SYMBOL(
341 current->mm->context.vdso, rt_sigreturn);
342#else
343 /*
344 * For the nommu case we don't have a VDSO. Instead we push two
345 * instructions to call the rt_sigreturn syscall onto the user stack.
346 */
347 if (copy_to_user(&frame->sigreturn_code, __user_rt_sigreturn,
348 sizeof(frame->sigreturn_code)))
349 return -EFAULT;
350
351 addr = (unsigned long)&frame->sigreturn_code;
352 /* Make sure the two instructions are pushed to icache. */
353 flush_icache_range(addr, addr + sizeof(frame->sigreturn_code));
354
355 regs->ra = addr;
356#endif /* CONFIG_MMU */
357
358 /*
359 * Set up registers for signal handler.
360 * Registers that we don't modify keep the value they had from
361 * user-space at the time we took the signal.
362 * We always pass siginfo and mcontext, regardless of SA_SIGINFO,
363 * since some things rely on this (e.g. glibc's debug/segfault.c).
364 */
365 regs->epc = (unsigned long)ksig->ka.sa.sa_handler;
366 regs->sp = (unsigned long)frame;
367 regs->a0 = ksig->sig; /* a0: signal number */
368 regs->a1 = (unsigned long)(&frame->info); /* a1: siginfo pointer */
369 regs->a2 = (unsigned long)(&frame->uc); /* a2: ucontext pointer */
370
371#if DEBUG_SIG
372 pr_info("SIG deliver (%s:%d): sig=%d pc=%p ra=%p sp=%p\n",
373 current->comm, task_pid_nr(current), ksig->sig,
374 (void *)regs->epc, (void *)regs->ra, frame);
375#endif
376
377 return 0;
378}
379
380static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
381{
382 sigset_t *oldset = sigmask_to_save();
383 int ret;
384
385 rseq_signal_deliver(ksig, regs);
386
387 /* Set up the stack frame */
388 if (is_compat_task())
389 ret = compat_setup_rt_frame(ksig, oldset, regs);
390 else
391 ret = setup_rt_frame(ksig, oldset, regs);
392
393 signal_setup_done(ret, ksig, 0);
394}
395
396void arch_do_signal_or_restart(struct pt_regs *regs)
397{
398 unsigned long continue_addr = 0, restart_addr = 0;
399 int retval = 0;
400 struct ksignal ksig;
401 bool syscall = (regs->cause == EXC_SYSCALL);
402
403 /* If we were from a system call, check for system call restarting */
404 if (syscall) {
405 continue_addr = regs->epc;
406 restart_addr = continue_addr - 4;
407 retval = regs->a0;
408
409 /* Avoid additional syscall restarting via ret_from_exception */
410 regs->cause = -1UL;
411
412 /*
413 * Prepare for system call restart. We do this here so that a
414 * debugger will see the already changed PC.
415 */
416 switch (retval) {
417 case -ERESTARTNOHAND:
418 case -ERESTARTSYS:
419 case -ERESTARTNOINTR:
420 case -ERESTART_RESTARTBLOCK:
421 regs->a0 = regs->orig_a0;
422 regs->epc = restart_addr;
423 break;
424 }
425 }
426
427 /*
428 * Get the signal to deliver. When running under ptrace, at this point
429 * the debugger may change all of our registers.
430 */
431 if (get_signal(&ksig)) {
432 /*
433 * Depending on the signal settings, we may need to revert the
434 * decision to restart the system call, but skip this if a
435 * debugger has chosen to restart at a different PC.
436 */
437 if (regs->epc == restart_addr &&
438 (retval == -ERESTARTNOHAND ||
439 retval == -ERESTART_RESTARTBLOCK ||
440 (retval == -ERESTARTSYS &&
441 !(ksig.ka.sa.sa_flags & SA_RESTART)))) {
442 regs->a0 = -EINTR;
443 regs->epc = continue_addr;
444 }
445
446 /* Actually deliver the signal */
447 handle_signal(&ksig, regs);
448 return;
449 }
450
451 /*
452 * Handle restarting a different system call. As above, if a debugger
453 * has chosen to restart at a different PC, ignore the restart.
454 */
455 if (syscall && regs->epc == restart_addr && retval == -ERESTART_RESTARTBLOCK)
456 regs->a7 = __NR_restart_syscall;
457
458 /*
459 * If there is no signal to deliver, we just put the saved
460 * sigmask back.
461 */
462 restore_saved_sigmask();
463}
464
465void init_rt_signal_env(void);
466void __init init_rt_signal_env(void)
467{
468 riscv_v_sc_size = sizeof(struct __riscv_ctx_hdr) +
469 sizeof(struct __sc_riscv_v_state) + riscv_v_vsize;
470 /*
471 * Determine the stack space required for guaranteed signal delivery.
472 * The signal_minsigstksz will be populated into the AT_MINSIGSTKSZ entry
473 * in the auxiliary array at process startup.
474 */
475 signal_minsigstksz = get_rt_frame_size(true);
476}
477
478#ifdef CONFIG_DYNAMIC_SIGFRAME
479bool sigaltstack_size_valid(size_t ss_size)
480{
481 return ss_size > get_rt_frame_size(false);
482}
483#endif /* CONFIG_DYNAMIC_SIGFRAME */