xref: /openbmc/linux/arch/arm64/kernel/syscall.c (revision f91ca89e924eb287915522664a31afc71a49c05b)
14141c857SMark Rutland // SPDX-License-Identifier: GPL-2.0
24141c857SMark Rutland 
3f37099b6SMark Rutland #include <linux/compiler.h>
4f37099b6SMark Rutland #include <linux/context_tracking.h>
54141c857SMark Rutland #include <linux/errno.h>
64141c857SMark Rutland #include <linux/nospec.h>
74141c857SMark Rutland #include <linux/ptrace.h>
870918779SKees Cook #include <linux/randomize_kstack.h>
94141c857SMark Rutland #include <linux/syscalls.h>
104141c857SMark Rutland 
11969f5ea6SWill Deacon #include <asm/debug-monitors.h>
12b6d8878dSMark Rutland #include <asm/exception.h>
133b714275SMark Rutland #include <asm/fpsimd.h>
144141c857SMark Rutland #include <asm/syscall.h>
15f37099b6SMark Rutland #include <asm/thread_info.h>
163b714275SMark Rutland #include <asm/unistd.h>
174141c857SMark Rutland 
1853290432SWill Deacon long compat_arm_syscall(struct pt_regs *regs, int scno);
194378a7d4SMark Rutland long sys_ni_syscall(void);
204378a7d4SMark Rutland 
do_ni_syscall(struct pt_regs * regs,int scno)2153290432SWill Deacon static long do_ni_syscall(struct pt_regs *regs, int scno)
224141c857SMark Rutland {
234141c857SMark Rutland #ifdef CONFIG_COMPAT
244141c857SMark Rutland 	long ret;
254141c857SMark Rutland 	if (is_compat_task()) {
2653290432SWill Deacon 		ret = compat_arm_syscall(regs, scno);
274141c857SMark Rutland 		if (ret != -ENOSYS)
284141c857SMark Rutland 			return ret;
294141c857SMark Rutland 	}
304141c857SMark Rutland #endif
314141c857SMark Rutland 
324141c857SMark Rutland 	return sys_ni_syscall();
334141c857SMark Rutland }
344141c857SMark Rutland 
__invoke_syscall(struct pt_regs * regs,syscall_fn_t syscall_fn)354141c857SMark Rutland static long __invoke_syscall(struct pt_regs *regs, syscall_fn_t syscall_fn)
364141c857SMark Rutland {
374378a7d4SMark Rutland 	return syscall_fn(regs);
384141c857SMark Rutland }
394141c857SMark Rutland 
invoke_syscall(struct pt_regs * regs,unsigned int scno,unsigned int sc_nr,const syscall_fn_t syscall_table[])40f37099b6SMark Rutland static void invoke_syscall(struct pt_regs *regs, unsigned int scno,
414141c857SMark Rutland 			   unsigned int sc_nr,
424141c857SMark Rutland 			   const syscall_fn_t syscall_table[])
434141c857SMark Rutland {
444141c857SMark Rutland 	long ret;
454141c857SMark Rutland 
4670918779SKees Cook 	add_random_kstack_offset();
4770918779SKees Cook 
484141c857SMark Rutland 	if (scno < sc_nr) {
494141c857SMark Rutland 		syscall_fn_t syscall_fn;
504141c857SMark Rutland 		syscall_fn = syscall_table[array_index_nospec(scno, sc_nr)];
514141c857SMark Rutland 		ret = __invoke_syscall(regs, syscall_fn);
524141c857SMark Rutland 	} else {
5353290432SWill Deacon 		ret = do_ni_syscall(regs, scno);
544141c857SMark Rutland 	}
554141c857SMark Rutland 
56e30e8d46SMark Rutland 	syscall_set_return_value(current, regs, 0, ret);
5770918779SKees Cook 
5870918779SKees Cook 	/*
59*591c0650SKees Cook 	 * This value will get limited by KSTACK_OFFSET_MAX(), which is 10
60*591c0650SKees Cook 	 * bits. The actual entropy will be further reduced by the compiler
61*591c0650SKees Cook 	 * when applying stack alignment constraints: the AAPCS mandates a
62*591c0650SKees Cook 	 * 16-byte aligned SP at function boundaries, which will remove the
63*591c0650SKees Cook 	 * 4 low bits from any entropy chosen here.
6470918779SKees Cook 	 *
65*591c0650SKees Cook 	 * The resulting 6 bits of entropy is seen in SP[9:4].
6670918779SKees Cook 	 */
67*591c0650SKees Cook 	choose_random_kstack_offset(get_random_u16());
684141c857SMark Rutland }
69f37099b6SMark Rutland 
has_syscall_work(unsigned long flags)70f37099b6SMark Rutland static inline bool has_syscall_work(unsigned long flags)
71f37099b6SMark Rutland {
72f37099b6SMark Rutland 	return unlikely(flags & _TIF_SYSCALL_WORK);
73f37099b6SMark Rutland }
74f37099b6SMark Rutland 
el0_svc_common(struct pt_regs * regs,int scno,int sc_nr,const syscall_fn_t syscall_table[])753b714275SMark Rutland static void el0_svc_common(struct pt_regs *regs, int scno, int sc_nr,
76f37099b6SMark Rutland 			   const syscall_fn_t syscall_table[])
77f37099b6SMark Rutland {
78342b3808SMark Rutland 	unsigned long flags = read_thread_flags();
79f37099b6SMark Rutland 
80f37099b6SMark Rutland 	regs->orig_x0 = regs->regs[0];
81f37099b6SMark Rutland 	regs->syscallno = scno;
82f37099b6SMark Rutland 
838ef8f360SDave Martin 	/*
848ef8f360SDave Martin 	 * BTI note:
858ef8f360SDave Martin 	 * The architecture does not guarantee that SPSR.BTYPE is zero
868ef8f360SDave Martin 	 * on taking an SVC, so we could return to userspace with a
878ef8f360SDave Martin 	 * non-zero BTYPE after the syscall.
888ef8f360SDave Martin 	 *
898ef8f360SDave Martin 	 * This shouldn't matter except when userspace is explicitly
908ef8f360SDave Martin 	 * doing something stupid, such as setting PROT_BTI on a page
918ef8f360SDave Martin 	 * that lacks conforming BTI/PACIxSP instructions, falling
928ef8f360SDave Martin 	 * through from one executable page to another with differing
938ef8f360SDave Martin 	 * PROT_BTI, or messing with BTYPE via ptrace: in such cases,
948ef8f360SDave Martin 	 * userspace should not be surprised if a SIGILL occurs on
958ef8f360SDave Martin 	 * syscall return.
968ef8f360SDave Martin 	 *
978ef8f360SDave Martin 	 * So, don't touch regs->pstate & PSR_BTYPE_MASK here.
988ef8f360SDave Martin 	 * (Similarly for HVC and SMC elsewhere.)
998ef8f360SDave Martin 	 */
1008ef8f360SDave Martin 
101739003c6SPeter Collingbourne 	if (flags & _TIF_MTE_ASYNC_FAULT) {
102637ec831SVincenzo Frascino 		/*
103637ec831SVincenzo Frascino 		 * Process the asynchronous tag check fault before the actual
104637ec831SVincenzo Frascino 		 * syscall. do_notify_resume() will send a signal to userspace
105637ec831SVincenzo Frascino 		 * before the syscall is restarted.
106637ec831SVincenzo Frascino 		 */
107e30e8d46SMark Rutland 		syscall_set_return_value(current, regs, -ERESTARTNOINTR, 0);
108637ec831SVincenzo Frascino 		return;
109637ec831SVincenzo Frascino 	}
110637ec831SVincenzo Frascino 
111f37099b6SMark Rutland 	if (has_syscall_work(flags)) {
112139dbe5dSWill Deacon 		/*
113139dbe5dSWill Deacon 		 * The de-facto standard way to skip a system call using ptrace
114139dbe5dSWill Deacon 		 * is to set the system call to -1 (NO_SYSCALL) and set x0 to a
115139dbe5dSWill Deacon 		 * suitable error code for consumption by userspace. However,
116139dbe5dSWill Deacon 		 * this cannot be distinguished from a user-issued syscall(-1)
117139dbe5dSWill Deacon 		 * and so we must set x0 to -ENOSYS here in case the tracer doesn't
118139dbe5dSWill Deacon 		 * issue the skip and we fall into trace_exit with x0 preserved.
119139dbe5dSWill Deacon 		 *
120139dbe5dSWill Deacon 		 * This is slightly odd because it also means that if a tracer
121139dbe5dSWill Deacon 		 * sets the system call number to -1 but does not initialise x0,
122139dbe5dSWill Deacon 		 * then x0 will be preserved for all system calls apart from a
123139dbe5dSWill Deacon 		 * user-issued syscall(-1). However, requesting a skip and not
124139dbe5dSWill Deacon 		 * setting the return value is unlikely to do anything sensible
125139dbe5dSWill Deacon 		 * anyway.
126139dbe5dSWill Deacon 		 */
127f37099b6SMark Rutland 		if (scno == NO_SYSCALL)
128e30e8d46SMark Rutland 			syscall_set_return_value(current, regs, -ENOSYS, 0);
129f37099b6SMark Rutland 		scno = syscall_trace_enter(regs);
130f37099b6SMark Rutland 		if (scno == NO_SYSCALL)
131f37099b6SMark Rutland 			goto trace_exit;
132f37099b6SMark Rutland 	}
133f37099b6SMark Rutland 
134f37099b6SMark Rutland 	invoke_syscall(regs, scno, sc_nr, syscall_table);
135f37099b6SMark Rutland 
136f37099b6SMark Rutland 	/*
137f37099b6SMark Rutland 	 * The tracing status may have changed under our feet, so we have to
138f37099b6SMark Rutland 	 * check again. However, if we were tracing entry, then we always trace
139f37099b6SMark Rutland 	 * exit regardless, as the old entry assembly did.
140f37099b6SMark Rutland 	 */
141f37099b6SMark Rutland 	if (!has_syscall_work(flags) && !IS_ENABLED(CONFIG_DEBUG_RSEQ)) {
142342b3808SMark Rutland 		flags = read_thread_flags();
143df068247SMark Rutland 		if (!has_syscall_work(flags) && !(flags & _TIF_SINGLESTEP))
144f37099b6SMark Rutland 			return;
145f37099b6SMark Rutland 	}
146f37099b6SMark Rutland 
147f37099b6SMark Rutland trace_exit:
148f37099b6SMark Rutland 	syscall_trace_exit(regs);
149f37099b6SMark Rutland }
1503b714275SMark Rutland 
do_el0_svc(struct pt_regs * regs)1517a2c0944SMark Rutland void do_el0_svc(struct pt_regs *regs)
1523b714275SMark Rutland {
1533b714275SMark Rutland 	el0_svc_common(regs, regs->regs[8], __NR_syscalls, sys_call_table);
1543b714275SMark Rutland }
1553b714275SMark Rutland 
1563b714275SMark Rutland #ifdef CONFIG_COMPAT
do_el0_svc_compat(struct pt_regs * regs)1577a2c0944SMark Rutland void do_el0_svc_compat(struct pt_regs *regs)
1583b714275SMark Rutland {
1593b714275SMark Rutland 	el0_svc_common(regs, regs->regs[7], __NR_compat_syscalls,
1603b714275SMark Rutland 		       compat_sys_call_table);
1613b714275SMark Rutland }
1623b714275SMark Rutland #endif
163