• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/signal.h>
10 #include <linux/uaccess.h>
11 #include <linux/syscalls.h>
12 #include <linux/tracehook.h>
13 #include <linux/linkage.h>
14 
15 #include <asm/ucontext.h>
16 #include <asm/vdso.h>
17 #include <asm/switch_to.h>
18 #include <asm/csr.h>
19 #include <asm/cacheflush.h>
20 
21 extern u32 __user_rt_sigreturn[2];
22 
23 #define DEBUG_SIG 0
24 
25 struct rt_sigframe {
26 	struct siginfo info;
27 	struct ucontext uc;
28 #ifndef CONFIG_MMU
29 	u32 sigreturn_code[2];
30 #endif
31 };
32 
33 #ifdef CONFIG_FPU
restore_fp_state(struct pt_regs * regs,union __riscv_fp_state __user * sc_fpregs)34 static long restore_fp_state(struct pt_regs *regs,
35 			     union __riscv_fp_state __user *sc_fpregs)
36 {
37 	long err;
38 	struct __riscv_d_ext_state __user *state = &sc_fpregs->d;
39 	size_t i;
40 
41 	err = __copy_from_user(&current->thread.fstate, state, sizeof(*state));
42 	if (unlikely(err))
43 		return err;
44 
45 	fstate_restore(current, regs);
46 
47 	/* We support no other extension state at this time. */
48 	for (i = 0; i < ARRAY_SIZE(sc_fpregs->q.reserved); i++) {
49 		u32 value;
50 
51 		err = __get_user(value, &sc_fpregs->q.reserved[i]);
52 		if (unlikely(err))
53 			break;
54 		if (value != 0)
55 			return -EINVAL;
56 	}
57 
58 	return err;
59 }
60 
save_fp_state(struct pt_regs * regs,union __riscv_fp_state __user * sc_fpregs)61 static long save_fp_state(struct pt_regs *regs,
62 			  union __riscv_fp_state __user *sc_fpregs)
63 {
64 	long err;
65 	struct __riscv_d_ext_state __user *state = &sc_fpregs->d;
66 	size_t i;
67 
68 	fstate_save(current, regs);
69 	err = __copy_to_user(state, &current->thread.fstate, sizeof(*state));
70 	if (unlikely(err))
71 		return err;
72 
73 	/* We support no other extension state at this time. */
74 	for (i = 0; i < ARRAY_SIZE(sc_fpregs->q.reserved); i++) {
75 		err = __put_user(0, &sc_fpregs->q.reserved[i]);
76 		if (unlikely(err))
77 			break;
78 	}
79 
80 	return err;
81 }
82 #else
83 #define save_fp_state(task, regs) (0)
84 #define restore_fp_state(task, regs) (0)
85 #endif
86 
restore_sigcontext(struct pt_regs * regs,struct sigcontext __user * sc)87 static long restore_sigcontext(struct pt_regs *regs,
88 	struct sigcontext __user *sc)
89 {
90 	long err;
91 	/* sc_regs is structured the same as the start of pt_regs */
92 	err = __copy_from_user(regs, &sc->sc_regs, sizeof(sc->sc_regs));
93 	/* Restore the floating-point state. */
94 	if (has_fpu)
95 		err |= restore_fp_state(regs, &sc->sc_fpregs);
96 	return err;
97 }
98 
SYSCALL_DEFINE0(rt_sigreturn)99 SYSCALL_DEFINE0(rt_sigreturn)
100 {
101 	struct pt_regs *regs = current_pt_regs();
102 	struct rt_sigframe __user *frame;
103 	struct task_struct *task;
104 	sigset_t set;
105 
106 	/* Always make any pending restarted system calls return -EINTR */
107 	current->restart_block.fn = do_no_restart_syscall;
108 
109 	frame = (struct rt_sigframe __user *)regs->sp;
110 
111 	if (!access_ok(frame, sizeof(*frame)))
112 		goto badframe;
113 
114 	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
115 		goto badframe;
116 
117 	set_current_blocked(&set);
118 
119 	if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
120 		goto badframe;
121 
122 	if (restore_altstack(&frame->uc.uc_stack))
123 		goto badframe;
124 
125 	regs->cause = -1UL;
126 
127 	return regs->a0;
128 
129 badframe:
130 	task = current;
131 	if (show_unhandled_signals) {
132 		pr_info_ratelimited(
133 			"%s[%d]: bad frame in %s: frame=%p pc=%p sp=%p\n",
134 			task->comm, task_pid_nr(task), __func__,
135 			frame, (void *)regs->epc, (void *)regs->sp);
136 	}
137 	force_sig(SIGSEGV);
138 	return 0;
139 }
140 
setup_sigcontext(struct rt_sigframe __user * frame,struct pt_regs * regs)141 static long setup_sigcontext(struct rt_sigframe __user *frame,
142 	struct pt_regs *regs)
143 {
144 	struct sigcontext __user *sc = &frame->uc.uc_mcontext;
145 	long err;
146 	/* sc_regs is structured the same as the start of pt_regs */
147 	err = __copy_to_user(&sc->sc_regs, regs, sizeof(sc->sc_regs));
148 	/* Save the floating-point state. */
149 	if (has_fpu)
150 		err |= save_fp_state(regs, &sc->sc_fpregs);
151 	return err;
152 }
153 
get_sigframe(struct ksignal * ksig,struct pt_regs * regs,size_t framesize)154 static inline void __user *get_sigframe(struct ksignal *ksig,
155 	struct pt_regs *regs, size_t framesize)
156 {
157 	unsigned long sp;
158 	/* Default to using normal stack */
159 	sp = regs->sp;
160 
161 	/*
162 	 * If we are on the alternate signal stack and would overflow it, don't.
163 	 * Return an always-bogus address instead so we will die with SIGSEGV.
164 	 */
165 	if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
166 		return (void __user __force *)(-1UL);
167 
168 	/* This is the X/Open sanctioned signal stack switching. */
169 	sp = sigsp(sp, ksig) - framesize;
170 
171 	/* Align the stack frame. */
172 	sp &= ~0xfUL;
173 
174 	return (void __user *)sp;
175 }
176 
setup_rt_frame(struct ksignal * ksig,sigset_t * set,struct pt_regs * regs)177 static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
178 	struct pt_regs *regs)
179 {
180 	struct rt_sigframe __user *frame;
181 	long err = 0;
182 	unsigned long __maybe_unused addr;
183 
184 	frame = get_sigframe(ksig, regs, sizeof(*frame));
185 	if (!access_ok(frame, sizeof(*frame)))
186 		return -EFAULT;
187 
188 	err |= copy_siginfo_to_user(&frame->info, &ksig->info);
189 
190 	/* Create the ucontext. */
191 	err |= __put_user(0, &frame->uc.uc_flags);
192 	err |= __put_user(NULL, &frame->uc.uc_link);
193 	err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
194 	err |= setup_sigcontext(frame, regs);
195 	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
196 	if (err)
197 		return -EFAULT;
198 
199 	/* Set up to return from userspace. */
200 #ifdef CONFIG_MMU
201 	regs->ra = (unsigned long)VDSO_SYMBOL(
202 		current->mm->context.vdso, rt_sigreturn);
203 #else
204 	/*
205 	 * For the nommu case we don't have a VDSO.  Instead we push two
206 	 * instructions to call the rt_sigreturn syscall onto the user stack.
207 	 */
208 	if (copy_to_user(&frame->sigreturn_code, __user_rt_sigreturn,
209 			 sizeof(frame->sigreturn_code)))
210 		return -EFAULT;
211 
212 	addr = (unsigned long)&frame->sigreturn_code;
213 	/* Make sure the two instructions are pushed to icache. */
214 	flush_icache_range(addr, addr + sizeof(frame->sigreturn_code));
215 
216 	regs->ra = addr;
217 #endif /* CONFIG_MMU */
218 
219 	/*
220 	 * Set up registers for signal handler.
221 	 * Registers that we don't modify keep the value they had from
222 	 * user-space at the time we took the signal.
223 	 * We always pass siginfo and mcontext, regardless of SA_SIGINFO,
224 	 * since some things rely on this (e.g. glibc's debug/segfault.c).
225 	 */
226 	regs->epc = (unsigned long)ksig->ka.sa.sa_handler;
227 	regs->sp = (unsigned long)frame;
228 	regs->a0 = ksig->sig;                     /* a0: signal number */
229 	regs->a1 = (unsigned long)(&frame->info); /* a1: siginfo pointer */
230 	regs->a2 = (unsigned long)(&frame->uc);   /* a2: ucontext pointer */
231 
232 #if DEBUG_SIG
233 	pr_info("SIG deliver (%s:%d): sig=%d pc=%p ra=%p sp=%p\n",
234 		current->comm, task_pid_nr(current), ksig->sig,
235 		(void *)regs->epc, (void *)regs->ra, frame);
236 #endif
237 
238 	return 0;
239 }
240 
handle_signal(struct ksignal * ksig,struct pt_regs * regs)241 static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
242 {
243 	sigset_t *oldset = sigmask_to_save();
244 	int ret;
245 
246 	/* Are we from a system call? */
247 	if (regs->cause == EXC_SYSCALL) {
248 		/* Avoid additional syscall restarting via ret_from_exception */
249 		regs->cause = -1UL;
250 		/* If so, check system call restarting.. */
251 		switch (regs->a0) {
252 		case -ERESTART_RESTARTBLOCK:
253 		case -ERESTARTNOHAND:
254 			regs->a0 = -EINTR;
255 			break;
256 
257 		case -ERESTARTSYS:
258 			if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
259 				regs->a0 = -EINTR;
260 				break;
261 			}
262 			fallthrough;
263 		case -ERESTARTNOINTR:
264                         regs->a0 = regs->orig_a0;
265 			regs->epc -= 0x4;
266 			break;
267 		}
268 	}
269 
270 	/* Set up the stack frame */
271 	ret = setup_rt_frame(ksig, oldset, regs);
272 
273 	signal_setup_done(ret, ksig, 0);
274 }
275 
do_signal(struct pt_regs * regs)276 static void do_signal(struct pt_regs *regs)
277 {
278 	struct ksignal ksig;
279 
280 	if (get_signal(&ksig)) {
281 		/* Actually deliver the signal */
282 		handle_signal(&ksig, regs);
283 		return;
284 	}
285 
286 	/* Did we come from a system call? */
287 	if (regs->cause == EXC_SYSCALL) {
288 		/* Avoid additional syscall restarting via ret_from_exception */
289 		regs->cause = -1UL;
290 
291 		/* Restart the system call - no handlers present */
292 		switch (regs->a0) {
293 		case -ERESTARTNOHAND:
294 		case -ERESTARTSYS:
295 		case -ERESTARTNOINTR:
296                         regs->a0 = regs->orig_a0;
297 			regs->epc -= 0x4;
298 			break;
299 		case -ERESTART_RESTARTBLOCK:
300                         regs->a0 = regs->orig_a0;
301 			regs->a7 = __NR_restart_syscall;
302 			regs->epc -= 0x4;
303 			break;
304 		}
305 	}
306 
307 	/*
308 	 * If there is no signal to deliver, we just put the saved
309 	 * sigmask back.
310 	 */
311 	restore_saved_sigmask();
312 }
313 
314 /*
315  * notification of userspace execution resumption
316  * - triggered by the _TIF_WORK_MASK flags
317  */
do_notify_resume(struct pt_regs * regs,unsigned long thread_info_flags)318 asmlinkage __visible void do_notify_resume(struct pt_regs *regs,
319 					   unsigned long thread_info_flags)
320 {
321 	/* Handle pending signal delivery */
322 	if (thread_info_flags & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL))
323 		do_signal(regs);
324 
325 	if (thread_info_flags & _TIF_NOTIFY_RESUME)
326 		tracehook_notify_resume(regs);
327 }
328