xref: /linux/arch/alpha/kernel/signal.c (revision 12d5fc96ebbb3b948861805b32a4c3a96105ccd9)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  linux/arch/alpha/kernel/signal.c
4  *
5  *  Copyright (C) 1995  Linus Torvalds
6  *
7  *  1997-11-02  Modified for POSIX.1b signals by Richard Henderson
8  */
9 
10 #include <linux/sched/signal.h>
11 #include <linux/sched/task_stack.h>
12 #include <linux/kernel.h>
13 #include <linux/signal.h>
14 #include <linux/errno.h>
15 #include <linux/wait.h>
16 #include <linux/ptrace.h>
17 #include <linux/unistd.h>
18 #include <linux/mm.h>
19 #include <linux/smp.h>
20 #include <linux/stddef.h>
21 #include <linux/tty.h>
22 #include <linux/binfmts.h>
23 #include <linux/bitops.h>
24 #include <linux/syscalls.h>
25 #include <linux/resume_user_mode.h>
26 
27 #include <linux/uaccess.h>
28 #include <asm/sigcontext.h>
29 #include <asm/ucontext.h>
30 #include <linux/entry-common.h>
31 #include "proto.h"
32 
33 #define DEBUG_SIG 0
34 
35 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
36 
37 asmlinkage void ret_from_sys_call(void);
38 
39 /*
40  * The OSF/1 sigprocmask calling sequence is different from the
41  * C sigprocmask() sequence..
42  */
43 SYSCALL_DEFINE2(osf_sigprocmask, int, how, unsigned long, newmask)
44 {
45 	sigset_t oldmask;
46 	sigset_t mask;
47 	unsigned long res;
48 
49 	siginitset(&mask, newmask & _BLOCKABLE);
50 	res = sigprocmask(how, &mask, &oldmask);
51 	if (!res) {
52 		force_successful_syscall_return();
53 		res = oldmask.sig[0];
54 	}
55 	return res;
56 }
57 
58 SYSCALL_DEFINE3(osf_sigaction, int, sig,
59 		const struct osf_sigaction __user *, act,
60 		struct osf_sigaction __user *, oact)
61 {
62 	struct k_sigaction new_ka, old_ka;
63 	int ret;
64 
65 	if (act) {
66 		old_sigset_t mask;
67 		scoped_user_read_access(act, Efault) {
68 			unsafe_get_user(new_ka.sa.sa_handler, &act->sa_handler, Efault);
69 			unsafe_get_user(new_ka.sa.sa_flags, &act->sa_flags, Efault);
70 			unsafe_get_user(mask, &act->sa_mask, Efault);
71 		}
72 		siginitset(&new_ka.sa.sa_mask, mask);
73 		new_ka.ka_restorer = NULL;
74 	}
75 
76 	ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
77 
78 	if (!ret && oact) {
79 		scoped_user_write_access(oact, Efault) {
80 			unsafe_put_user(old_ka.sa.sa_handler, &oact->sa_handler, Efault);
81 			unsafe_put_user(old_ka.sa.sa_flags, &oact->sa_flags, Efault);
82 			unsafe_put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask, Efault);
83 		}
84 	}
85 
86 	return ret;
87 
88 Efault:
89 	return -EFAULT;
90 }
91 
92 SYSCALL_DEFINE5(rt_sigaction, int, sig, const struct sigaction __user *, act,
93 		struct sigaction __user *, oact,
94 		size_t, sigsetsize, void __user *, restorer)
95 {
96 	struct k_sigaction new_ka, old_ka;
97 	int ret;
98 
99 	/* XXX: Don't preclude handling different sized sigset_t's.  */
100 	if (sigsetsize != sizeof(sigset_t))
101 		return -EINVAL;
102 
103 	if (act) {
104 		new_ka.ka_restorer = restorer;
105 		if (copy_from_user(&new_ka.sa, act, sizeof(*act)))
106 			return -EFAULT;
107 	}
108 
109 	ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
110 
111 	if (!ret && oact) {
112 		if (copy_to_user(oact, &old_ka.sa, sizeof(*oact)))
113 			return -EFAULT;
114 	}
115 
116 	return ret;
117 }
118 
119 /*
120  * Do a signal return; undo the signal stack.
121  */
122 
123 #if _NSIG_WORDS > 1
124 # error "Non SA_SIGINFO frame needs rearranging"
125 #endif
126 
127 struct sigframe
128 {
129 	struct sigcontext sc;
130 	unsigned int retcode[3];
131 };
132 
133 struct rt_sigframe
134 {
135 	struct siginfo info;
136 	struct ucontext uc;
137 	unsigned int retcode[3];
138 };
139 
140 /* If this changes, userland unwinders that Know Things about our signal
141    frame will break.  Do not undertake lightly.  It also implies an ABI
142    change wrt the size of siginfo_t, which may cause some pain.  */
143 extern char compile_time_assert
144         [offsetof(struct rt_sigframe, uc.uc_mcontext) == 176 ? 1 : -1];
145 
146 #define INSN_MOV_R30_R16	0x47fe0410
147 #define INSN_LDI_R0		0x201f0000
148 #define INSN_CALLSYS		0x00000083
149 
150 static long
151 restore_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs)
152 {
153 	unsigned long usp;
154 	struct switch_stack *sw = (struct switch_stack *)regs - 1;
155 
156 	current->restart_block.fn = do_no_restart_syscall;
157 	current_thread_info()->status |= TS_SAVED_FP | TS_RESTORE_FP;
158 
159 	sw->r26 = (unsigned long) ret_from_sys_call;
160 
161 	scoped_user_read_access(sc, Efault) {
162 		unsafe_get_user(regs->pc, &sc->sc_pc, Efault);
163 		unsafe_get_user(regs->r0, sc->sc_regs+0, Efault);
164 		unsafe_get_user(regs->r1, sc->sc_regs+1, Efault);
165 		unsafe_get_user(regs->r2, sc->sc_regs+2, Efault);
166 		unsafe_get_user(regs->r3, sc->sc_regs+3, Efault);
167 		unsafe_get_user(regs->r4, sc->sc_regs+4, Efault);
168 		unsafe_get_user(regs->r5, sc->sc_regs+5, Efault);
169 		unsafe_get_user(regs->r6, sc->sc_regs+6, Efault);
170 		unsafe_get_user(regs->r7, sc->sc_regs+7, Efault);
171 		unsafe_get_user(regs->r8, sc->sc_regs+8, Efault);
172 		unsafe_get_user(sw->r9, sc->sc_regs+9, Efault);
173 		unsafe_get_user(sw->r10, sc->sc_regs+10, Efault);
174 		unsafe_get_user(sw->r11, sc->sc_regs+11, Efault);
175 		unsafe_get_user(sw->r12, sc->sc_regs+12, Efault);
176 		unsafe_get_user(sw->r13, sc->sc_regs+13, Efault);
177 		unsafe_get_user(sw->r14, sc->sc_regs+14, Efault);
178 		unsafe_get_user(sw->r15, sc->sc_regs+15, Efault);
179 		unsafe_get_user(regs->r16, sc->sc_regs+16, Efault);
180 		unsafe_get_user(regs->r17, sc->sc_regs+17, Efault);
181 		unsafe_get_user(regs->r18, sc->sc_regs+18, Efault);
182 		unsafe_get_user(regs->r19, sc->sc_regs+19, Efault);
183 		unsafe_get_user(regs->r20, sc->sc_regs+20, Efault);
184 		unsafe_get_user(regs->r21, sc->sc_regs+21, Efault);
185 		unsafe_get_user(regs->r22, sc->sc_regs+22, Efault);
186 		unsafe_get_user(regs->r23, sc->sc_regs+23, Efault);
187 		unsafe_get_user(regs->r24, sc->sc_regs+24, Efault);
188 		unsafe_get_user(regs->r25, sc->sc_regs+25, Efault);
189 		unsafe_get_user(regs->r26, sc->sc_regs+26, Efault);
190 		unsafe_get_user(regs->r27, sc->sc_regs+27, Efault);
191 		unsafe_get_user(regs->r28, sc->sc_regs+28, Efault);
192 		unsafe_get_user(regs->gp, sc->sc_regs+29, Efault);
193 		unsafe_get_user(usp, sc->sc_regs+30, Efault);
194 		unsafe_copy_from_user(current_thread_info()->fp,
195 				      sc->sc_fpregs, 31 * 8, Efault);
196 		unsafe_get_user(current_thread_info()->fp[31], &sc->sc_fpcr, Efault);
197 	}
198 	wrusp(usp);
199 	return 0;
200 
201 Efault:
202 	return -EFAULT;
203 }
204 
205 /* Note that this syscall is also used by setcontext(3) to install
206    a given sigcontext.  This because it's impossible to set *all*
207    registers and transfer control from userland.  */
208 
209 asmlinkage void
210 do_sigreturn(struct sigcontext __user *sc)
211 {
212 	struct pt_regs *regs = current_pt_regs();
213 	sigset_t set;
214 
215 	scoped_user_read_access(sc, give_sigsegv)
216 		unsafe_get_user(set.sig[0], &sc->sc_mask, give_sigsegv);
217 
218 	set_current_blocked(&set);
219 
220 	if (restore_sigcontext(sc, regs))
221 		goto give_sigsegv;
222 
223 	/* Send SIGTRAP if we're single-stepping: */
224 	if (ptrace_cancel_bpt (current)) {
225 		send_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *) regs->pc,
226 			       current);
227 	}
228 	return;
229 
230 give_sigsegv:
231 	force_sig(SIGSEGV);
232 }
233 
234 asmlinkage void
235 do_rt_sigreturn(struct rt_sigframe __user *frame)
236 {
237 	struct pt_regs *regs = current_pt_regs();
238 	sigset_t set;
239 
240 	/* Verify that it's a good ucontext_t before using it */
241 	if (!access_ok(&frame->uc, sizeof(frame->uc)))
242 		goto give_sigsegv;
243 	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
244 		goto give_sigsegv;
245 
246 	set_current_blocked(&set);
247 
248 	if (restore_sigcontext(&frame->uc.uc_mcontext, regs))
249 		goto give_sigsegv;
250 
251 	/* Send SIGTRAP if we're single-stepping: */
252 	if (ptrace_cancel_bpt (current)) {
253 		send_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *) regs->pc,
254 			       current);
255 	}
256 	return;
257 
258 give_sigsegv:
259 	force_sig(SIGSEGV);
260 }
261 
262 
263 /*
264  * Set up a signal frame.
265  */
266 
267 static inline void __user *
268 get_sigframe(struct ksignal *ksig, unsigned long sp, size_t frame_size)
269 {
270 	return (void __user *)((sigsp(sp, ksig) - frame_size) & -32ul);
271 }
272 
273 static long
274 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
275 		 unsigned long mask, unsigned long sp)
276 {
277 	struct switch_stack *sw = (struct switch_stack *)regs - 1;
278 	long onstack = on_sig_stack((unsigned long)sc);
279 
280 	scoped_user_write_access(sc, Efault) {
281 		unsafe_put_user(onstack, &sc->sc_onstack, Efault);
282 		unsafe_put_user(mask, &sc->sc_mask, Efault);
283 		unsafe_put_user(regs->pc, &sc->sc_pc, Efault);
284 		unsafe_put_user(8, &sc->sc_ps, Efault);
285 
286 		unsafe_put_user(regs->r0, sc->sc_regs+0, Efault);
287 		unsafe_put_user(regs->r1, sc->sc_regs+1, Efault);
288 		unsafe_put_user(regs->r2, sc->sc_regs+2, Efault);
289 		unsafe_put_user(regs->r3, sc->sc_regs+3, Efault);
290 		unsafe_put_user(regs->r4, sc->sc_regs+4, Efault);
291 		unsafe_put_user(regs->r5, sc->sc_regs+5, Efault);
292 		unsafe_put_user(regs->r6, sc->sc_regs+6, Efault);
293 		unsafe_put_user(regs->r7, sc->sc_regs+7, Efault);
294 		unsafe_put_user(regs->r8, sc->sc_regs+8, Efault);
295 		unsafe_put_user(sw->r9, sc->sc_regs+9, Efault);
296 		unsafe_put_user(sw->r10, sc->sc_regs+10, Efault);
297 		unsafe_put_user(sw->r11, sc->sc_regs+11, Efault);
298 		unsafe_put_user(sw->r12, sc->sc_regs+12, Efault);
299 		unsafe_put_user(sw->r13, sc->sc_regs+13, Efault);
300 		unsafe_put_user(sw->r14, sc->sc_regs+14, Efault);
301 		unsafe_put_user(sw->r15, sc->sc_regs+15, Efault);
302 		unsafe_put_user(regs->r16, sc->sc_regs+16, Efault);
303 		unsafe_put_user(regs->r17, sc->sc_regs+17, Efault);
304 		unsafe_put_user(regs->r18, sc->sc_regs+18, Efault);
305 		unsafe_put_user(regs->r19, sc->sc_regs+19, Efault);
306 		unsafe_put_user(regs->r20, sc->sc_regs+20, Efault);
307 		unsafe_put_user(regs->r21, sc->sc_regs+21, Efault);
308 		unsafe_put_user(regs->r22, sc->sc_regs+22, Efault);
309 		unsafe_put_user(regs->r23, sc->sc_regs+23, Efault);
310 		unsafe_put_user(regs->r24, sc->sc_regs+24, Efault);
311 		unsafe_put_user(regs->r25, sc->sc_regs+25, Efault);
312 		unsafe_put_user(regs->r26, sc->sc_regs+26, Efault);
313 		unsafe_put_user(regs->r27, sc->sc_regs+27, Efault);
314 		unsafe_put_user(regs->r28, sc->sc_regs+28, Efault);
315 		unsafe_put_user(regs->gp, sc->sc_regs+29, Efault);
316 		unsafe_put_user(sp, sc->sc_regs+30, Efault);
317 		unsafe_put_user(0, sc->sc_regs+31, Efault);
318 
319 		unsafe_copy_to_user(sc->sc_fpregs,
320 				    current_thread_info()->fp, 31 * 8, Efault);
321 		unsafe_put_user(0, sc->sc_fpregs+31, Efault);
322 		unsafe_put_user(current_thread_info()->fp[31], &sc->sc_fpcr, Efault);
323 
324 		unsafe_put_user(regs->trap_a0, &sc->sc_traparg_a0, Efault);
325 		unsafe_put_user(regs->trap_a1, &sc->sc_traparg_a1, Efault);
326 		unsafe_put_user(regs->trap_a2, &sc->sc_traparg_a2, Efault);
327 	}
328 	return 0;
329 
330 Efault:
331 	return -EFAULT;
332 }
333 
334 static int
335 setup_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
336 {
337 	unsigned long oldsp, r26;
338 	struct sigframe __user *frame;
339 
340 	oldsp = rdusp();
341 	frame = get_sigframe(ksig, oldsp, sizeof(*frame));
342 	if (!access_ok(frame, sizeof(*frame)))
343 		return -EFAULT;
344 
345 	if (setup_sigcontext(&frame->sc, regs, set->sig[0], oldsp))
346 		return -EFAULT;
347 
348 	/* Set up to return from userspace.  If provided, use a stub
349 	   already in userspace.  */
350 	r26 = (unsigned long) ksig->ka.ka_restorer;
351 	if (!r26) {
352 		scoped_user_write_access(frame, Efault) {
353 			unsafe_put_user(INSN_MOV_R30_R16, frame->retcode+0, Efault);
354 			unsafe_put_user(INSN_LDI_R0+__NR_sigreturn, frame->retcode+1, Efault);
355 			unsafe_put_user(INSN_CALLSYS, frame->retcode+2, Efault);
356 		}
357 		imb();
358 		r26 = (unsigned long) frame->retcode;
359 	}
360 
361 	/* "Return" to the handler */
362 	regs->r26 = r26;
363 	regs->r27 = regs->pc = (unsigned long) ksig->ka.sa.sa_handler;
364 	regs->r16 = ksig->sig;			/* a0: signal number */
365 	regs->r17 = 0;				/* a1: exception code */
366 	regs->r18 = (unsigned long) &frame->sc;	/* a2: sigcontext pointer */
367 	wrusp((unsigned long) frame);
368 
369 #if DEBUG_SIG
370 	printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
371 		current->comm, current->pid, frame, regs->pc, regs->r26);
372 #endif
373 	return 0;
374 
375 Efault:
376 	return -EFAULT;
377 }
378 
379 static int
380 setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
381 {
382 	unsigned long oldsp, r26, err = 0;
383 	struct rt_sigframe __user *frame;
384 
385 	oldsp = rdusp();
386 	frame = get_sigframe(ksig, oldsp, sizeof(*frame));
387 	if (!access_ok(frame, sizeof(*frame)))
388 		return -EFAULT;
389 
390 	err |= copy_siginfo_to_user(&frame->info, &ksig->info);
391 
392 	/* Create the ucontext.  */
393 	scoped_user_write_access(frame, Efault) {
394 		unsafe_put_user(0, &frame->uc.uc_flags, Efault);
395 		unsafe_put_user(0, &frame->uc.uc_link, Efault);
396 		unsafe_put_user(set->sig[0], &frame->uc.uc_osf_sigmask, Efault);
397 	}
398 	err |= __save_altstack(&frame->uc.uc_stack, oldsp);
399 	err |= setup_sigcontext(&frame->uc.uc_mcontext, regs,
400 				set->sig[0], oldsp);
401 	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
402 	if (err)
403 		return -EFAULT;
404 
405 	/* Set up to return from userspace.  If provided, use a stub
406 	   already in userspace.  */
407 	r26 = (unsigned long) ksig->ka.ka_restorer;
408 	if (!r26) {
409 		scoped_user_write_access(frame, Efault) {
410 			unsafe_put_user(INSN_MOV_R30_R16, frame->retcode+0, Efault);
411 			unsafe_put_user(INSN_LDI_R0+__NR_rt_sigreturn,
412 					frame->retcode+1, Efault);
413 			unsafe_put_user(INSN_CALLSYS, frame->retcode+2, Efault);
414 		}
415 		imb();
416 		r26 = (unsigned long) frame->retcode;
417 	}
418 
419 	/* "Return" to the handler */
420 	regs->r26 = r26;
421 	regs->r27 = regs->pc = (unsigned long) ksig->ka.sa.sa_handler;
422 	regs->r16 = ksig->sig;			  /* a0: signal number */
423 	regs->r17 = (unsigned long) &frame->info; /* a1: siginfo pointer */
424 	regs->r18 = (unsigned long) &frame->uc;	  /* a2: ucontext pointer */
425 	wrusp((unsigned long) frame);
426 
427 #if DEBUG_SIG
428 	printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
429 		current->comm, current->pid, frame, regs->pc, regs->r26);
430 #endif
431 
432 	return 0;
433 
434 Efault:
435 	return -EFAULT;
436 }
437 
438 
439 /*
440  * OK, we're invoking a handler.
441  */
442 static inline void
443 handle_signal(struct ksignal *ksig, struct pt_regs *regs)
444 {
445 	sigset_t *oldset = sigmask_to_save();
446 	int ret;
447 
448 	if (ksig->ka.sa.sa_flags & SA_SIGINFO)
449 		ret = setup_rt_frame(ksig, oldset, regs);
450 	else
451 		ret = setup_frame(ksig, oldset, regs);
452 
453 	signal_setup_done(ret, ksig, 0);
454 }
455 
456 static inline void
457 syscall_restart(unsigned long r0, unsigned long r19,
458 		struct pt_regs *regs, struct k_sigaction *ka)
459 {
460 	switch (regs->r0) {
461 	case ERESTARTSYS:
462 		if (!(ka->sa.sa_flags & SA_RESTART)) {
463 		case ERESTARTNOHAND:
464 			regs->r0 = EINTR;
465 			break;
466 		}
467 		fallthrough;
468 	case ERESTARTNOINTR:
469 		regs->r0 = r0;	/* reset v0 and a3 and replay syscall */
470 		regs->r1 = r0;
471 		regs->r19 = r19;
472 		regs->pc -= 4;
473 		break;
474 	case ERESTART_RESTARTBLOCK:
475 		regs->r0 = EINTR;
476 		break;
477 	}
478 }
479 
480 
481 /*
482  * Note that 'init' is a special process: it doesn't get signals it doesn't
483  * want to handle. Thus you cannot kill init even with a SIGKILL even by
484  * mistake.
485  *
486  * Note that we go through the signals twice: once to check the signals that
487  * the kernel can handle, and then we build all the user-level signal handling
488  * stack-frames in one go after that.
489  *
490  * "r0" and "r19" are the registers we need to restore for system call
491  * restart. "r0" is also used as an indicator whether we can restart at
492  * all (if we get here from anything but a syscall return, it will be 0)
493  */
494 void
495 do_signal(struct pt_regs *regs, unsigned long r0, unsigned long r19)
496 {
497 	unsigned long single_stepping = ptrace_cancel_bpt(current);
498 	struct ksignal ksig;
499 
500 	/* This lets the debugger run, ... */
501 	if (get_signal(&ksig)) {
502 		/* ... so re-check the single stepping. */
503 		single_stepping |= ptrace_cancel_bpt(current);
504 		/* Whee!  Actually deliver the signal.  */
505 		if (r0)
506 			syscall_restart(r0, r19, regs, &ksig.ka);
507 		handle_signal(&ksig, regs);
508 	} else {
509 		single_stepping |= ptrace_cancel_bpt(current);
510 		if (r0) {
511 			switch (regs->r0) {
512 			case ERESTARTNOHAND:
513 			case ERESTARTSYS:
514 			case ERESTARTNOINTR:
515 				/* Reset v0 and a3 and replay syscall.  */
516 				regs->r0 = r0;
517 				regs->r1 = r0;
518 				regs->r19 = r19;
519 				regs->pc -= 4;
520 				break;
521 			case ERESTART_RESTARTBLOCK:
522 				/* Set v0 to the restart_syscall and replay */
523 				regs->r0 = __NR_restart_syscall;
524 				regs->r1 = __NR_restart_syscall;
525 				regs->pc -= 4;
526 				break;
527 			}
528 		}
529 		restore_saved_sigmask();
530 	}
531 	if (single_stepping)
532 		ptrace_set_bpt(current);	/* re-set breakpoint */
533 }
534 
535 asmlinkage void alpha_exit_to_user_mode(struct pt_regs *regs)
536 {
537 	local_irq_disable();
538 	irqentry_exit_to_user_mode_prepare(regs);
539 	exit_to_user_mode();
540 }
541 
542 /*
543  * Syscall return reaches here after Alpha-specific r0/a3 result encoding.
544  * Delegate syscall-exit work and final exit-to-user handling to generic
545  * entry code; low-level PAL restore remains in assembly.
546  */
547 asmlinkage void alpha_syscall_exit_to_user_mode(struct pt_regs *regs)
548 {
549 	syscall_exit_to_user_mode(regs);
550 }
551 
552 void arch_do_signal_or_restart(struct pt_regs *regs)
553 {
554 	struct thread_info *ti = current_thread_info();
555 
556 	preempt_disable();
557 	save_fpu();
558 	preempt_enable();
559 	do_signal(regs, ti->syscall_saved_nr, ti->syscall_saved_r19);
560 }
561 
562 asmlinkage unsigned long
563 alpha_syscall_enter_select(struct pt_regs *regs, long syscall)
564 {
565 	struct thread_info *ti = current_thread_info();
566 	unsigned long saved_r0 = regs->r0;
567 	unsigned long saved_r19 = regs->r19;
568 	unsigned long fn = (unsigned long)sys_ni_syscall;
569 	long nr = syscall;
570 	bool permit;
571 
572 	ti->syscall_meta = 0;
573 	ti->syscall_saved_nr = syscall;
574 
575 	if (!(ti->status & TS_SAVED_FP)) {
576 		ti->status |= TS_SAVED_FP;
577 		__save_fpu();
578 	}
579 
580 	permit = syscall_enter_from_user_mode_randomize_stack(regs, &nr);
581 
582 	/*
583 	 * Alpha cannot preset the full -ENOSYS return state before generic
584 	 * entry handling, because r19 is still syscall argument 4 on the
585 	 * normal dispatch path. So provide the default -ENOSYS result only
586 	 * here, after we know no real syscall will be invoked, and only if
587 	 * entry work did not already change the return state.
588 	 */
589 	if (unlikely(!permit || (unsigned long)nr >= NR_syscalls)) {
590 		if (regs->r0 == saved_r0 && regs->r19 == saved_r19)
591 			syscall_set_return_value(current, regs, -ENOSYS, 0);
592 
593 		ti->syscall_meta = ALPHA_SYSCALL_META_SKIP;
594 		return fn;	/* ignored by asm when SKIP is set */
595 	}
596 
597 	instrumentation_begin();
598 	{
599 		unsigned long index;
600 
601 		index = array_index_nospec((unsigned long)nr, NR_syscalls);
602 		fn = (unsigned long)sys_call_table[index];
603 	}
604 	instrumentation_end();
605 
606 	return fn;
607 }
608 
609 asmlinkage noinstr void
610 alpha_finish_syscall_to_user_mode(struct pt_regs *regs, long ret)
611 {
612 	struct thread_info *ti = current_thread_info();
613 	unsigned long meta = ti->syscall_meta;
614 
615 	ti->syscall_meta = 0;
616 	ti->syscall_saved_r19 = regs->r19;
617 
618 	instrumentation_begin();
619 
620 	if (meta & ALPHA_SYSCALL_META_SKIP) {
621 	/*
622 	 * Skip-dispatch path: ptrace/seccomp may already have installed
623 	 * the return state in r0/r19. Preserve it unless the syscall was
624 	 * skipped with no explicit return value.
625 	 *
626 	 * Generic PTRACE_SET_SYSCALL_INFO changes only the syscall-number
627 	 * shadow, so r1 == -1 while r0 still contains the original Alpha
628 	 * syscall number. Legacy PTRACE_POKEUSR based skipping can leave
629 	 * r0 == -1 with a3/r19 still indicating success. Both represent
630 	 * an unhandled skipped syscall and should become ENOSYS/a3=1.
631 	 */
632 		if (regs->r1 == (unsigned long)-1 &&
633 		   (regs->r0 == ti->syscall_saved_nr ||
634 		    regs->r0 == (unsigned long)-1)) {
635 			regs->r0 = ENOSYS;
636 			regs->r19 = 1;
637 		}
638 
639 		instrumentation_end();
640 
641 		syscall_exit_to_user_mode(regs);
642 		return;
643 	}
644 
645 	/*
646 	 * Some successful syscalls, notably legacy ptrace PEEK requests,
647 	 * return arbitrary data in r0.  That data may have the bit pattern
648 	 * of a negative errno, so do not infer failure from ret < 0 when
649 	 * arch code explicitly requested a successful Alpha return.
650 	 */
651 	if (meta & ALPHA_SYSCALL_META_FORCE_SUCCESS) {
652 		regs->r0 = ret;
653 		regs->r19 = 0;
654 	} else if (ret < 0) {
655 		regs->r0 = -ret;
656 		regs->r19 = 1;
657 	} else {
658 		regs->r0 = ret;
659 		regs->r19 = 0;
660 	}
661 
662 	instrumentation_end();
663 	syscall_exit_to_user_mode(regs);
664 }
665