xref: /linux/arch/x86/entry/syscall_64.c (revision 889dd44491b37c890b1b4ef40876cf19032ca897)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* 64-bit system call dispatch */
3 
4 #include <linux/linkage.h>
5 #include <linux/sys.h>
6 #include <linux/cache.h>
7 #include <linux/syscalls.h>
8 #include <linux/entry-common.h>
9 #include <linux/nospec.h>
10 #include <asm/syscall.h>
11 
12 #define __SYSCALL(nr, sym) extern long __x64_##sym(const struct pt_regs *);
13 #define __SYSCALL_NORETURN(nr, sym) extern long __noreturn __x64_##sym(const struct pt_regs *);
14 #include <asm/syscalls_64.h>
15 #ifdef CONFIG_X86_X32_ABI
16 #include <asm/syscalls_x32.h>
17 #endif
18 #undef  __SYSCALL
19 
20 #undef  __SYSCALL_NORETURN
21 #define __SYSCALL_NORETURN __SYSCALL
22 
23 /*
24  * The sys_call_table[] is no longer used for system calls, but
25  * kernel/trace/trace_syscalls.c still wants to know the system
26  * call address.
27  */
28 #define __SYSCALL(nr, sym) __x64_##sym,
29 const sys_call_ptr_t sys_call_table[] = {
30 #include <asm/syscalls_64.h>
31 };
32 #undef  __SYSCALL
33 
34 #define __SYSCALL(nr, sym) case nr: return __x64_##sym(regs);
35 
36 /* The unsigned int @nr argument is intentional as it creates denser code */
37 static noinline long x64_sys_call(const struct pt_regs *regs, unsigned int nr)
38 {
39 	switch (nr) {
40 	#include <asm/syscalls_64.h>
41 	default: return __x64_sys_ni_syscall(regs);
42 	}
43 }
44 
45 static noinline long x32_sys_call(const struct pt_regs *regs, unsigned int nr)
46 {
47 #ifdef CONFIG_X86_X32_ABI
48 	switch (nr) {
49 	#include <asm/syscalls_x32.h>
50 	default: return __x64_sys_ni_syscall(regs);
51 	}
52 #else
53 	return -ENOSYS;
54 #endif
55 }
56 
57 static __always_inline bool do_syscall_x64(struct pt_regs *regs, unsigned long nr)
58 {
59 	if (likely(nr < NR_syscalls)) {
60 		nr = array_index_nospec(nr, NR_syscalls);
61 		regs->ax = x64_sys_call(regs, (unsigned int)nr);
62 		return true;
63 	}
64 	return false;
65 }
66 
67 static __always_inline bool do_syscall_x32(struct pt_regs *regs, unsigned long nr)
68 {
69 	if (!IS_ENABLED(CONFIG_X86_X32_ABI))
70 		return false;
71 
72 	/* Adjust the starting offset of the table */
73 	nr -= __X32_SYSCALL_BIT;
74 
75 	if (likely(nr < X32_NR_syscalls)) {
76 		nr = array_index_nospec(nr, X32_NR_syscalls);
77 		regs->ax = x32_sys_call(regs, (unsigned int)nr);
78 		return true;
79 	}
80 	return false;
81 }
82 
83 /* Returns true to return using SYSRET, or false to use IRET */
84 __visible noinstr bool do_syscall_64(struct pt_regs *regs, long nr)
85 {
86 	if (likely(syscall_enter_from_user_mode_randomize_stack(regs, &nr))) {
87 		instrumentation_begin();
88 
89 		if (unlikely(!do_syscall_x64(regs, nr))) {
90 			/*
91 			 * The special treatment of syscall number -1 is a
92 			 * historical bogosity as the original code used -1 to
93 			 * indicate syscall abort, which is not distinguishable
94 			 * from a syscall which is invoked with syscall number
95 			 * -1. The historic behavior is to keep the return value
96 			 * in regs->ax unmodified in that case, which means it's
97 			 * either the preset value -ENOSYS or a value which was
98 			 * written by a tracer. If the syscall number is invalid
99 			 * but not -1 then overwrite any tracer modification.
100 			 */
101 			if (!do_syscall_x32(regs, nr) && nr != -1L)
102 				regs->ax = -ENOSYS;
103 		}
104 
105 		instrumentation_end();
106 	}
107 	syscall_exit_to_user_mode(regs);
108 
109 	/*
110 	 * Check that the register state is valid for using SYSRET to exit
111 	 * to userspace.  Otherwise use the slower but fully capable IRET
112 	 * exit path.
113 	 */
114 
115 	/* XEN PV guests always use the IRET path */
116 	if (cpu_feature_enabled(X86_FEATURE_XENPV))
117 		return false;
118 
119 	/* SYSRET requires RCX == RIP and R11 == EFLAGS */
120 	if (unlikely(regs->cx != regs->ip || regs->r11 != regs->flags))
121 		return false;
122 
123 	/* CS and SS must match the values set in MSR_STAR */
124 	if (unlikely(regs->cs != __USER_CS || regs->ss != __USER_DS))
125 		return false;
126 
127 	/*
128 	 * On Intel CPUs, SYSRET with non-canonical RCX/RIP will #GP
129 	 * in kernel space.  This essentially lets the user take over
130 	 * the kernel, since userspace controls RSP.
131 	 *
132 	 * TASK_SIZE_MAX covers all user-accessible addresses other than
133 	 * the deprecated vsyscall page.
134 	 */
135 	if (unlikely(regs->ip >= TASK_SIZE_MAX))
136 		return false;
137 
138 	/*
139 	 * SYSRET cannot restore RF.  It can restore TF, but unlike IRET,
140 	 * restoring TF results in a trap from userspace immediately after
141 	 * SYSRET.
142 	 */
143 	if (unlikely(regs->flags & (X86_EFLAGS_RF | X86_EFLAGS_TF)))
144 		return false;
145 
146 	/* Use SYSRET to exit to userspace */
147 	return true;
148 }
149