1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * common.c - C code for kernel entry and exit 4 * Copyright (c) 2015 Andrew Lutomirski 5 * 6 * Based on asm and ptrace code by many authors. The code here originated 7 * in ptrace.c and signal.c. 8 */ 9 10 #include <linux/kernel.h> 11 #include <linux/sched.h> 12 #include <linux/sched/task_stack.h> 13 #include <linux/entry-common.h> 14 #include <linux/mm.h> 15 #include <linux/smp.h> 16 #include <linux/errno.h> 17 #include <linux/ptrace.h> 18 #include <linux/export.h> 19 #include <linux/nospec.h> 20 #include <linux/syscalls.h> 21 #include <linux/uaccess.h> 22 23 #ifdef CONFIG_XEN_PV 24 #include <xen/xen-ops.h> 25 #include <xen/events.h> 26 #endif 27 28 #include <asm/apic.h> 29 #include <asm/desc.h> 30 #include <asm/traps.h> 31 #include <asm/vdso.h> 32 #include <asm/cpufeature.h> 33 #include <asm/fpu/api.h> 34 #include <asm/nospec-branch.h> 35 #include <asm/io_bitmap.h> 36 #include <asm/syscall.h> 37 #include <asm/irq_stack.h> 38 39 #ifdef CONFIG_X86_64 40 41 static __always_inline bool do_syscall_x64(struct pt_regs *regs, int nr) 42 { 43 /* 44 * Convert negative numbers to very high and thus out of range 45 * numbers for comparisons. 46 */ 47 unsigned int unr = nr; 48 49 if (likely(unr < NR_syscalls)) { 50 unr = array_index_nospec(unr, NR_syscalls); 51 regs->ax = x64_sys_call(regs, unr); 52 return true; 53 } 54 return false; 55 } 56 57 static __always_inline bool do_syscall_x32(struct pt_regs *regs, int nr) 58 { 59 /* 60 * Adjust the starting offset of the table, and convert numbers 61 * < __X32_SYSCALL_BIT to very high and thus out of range 62 * numbers for comparisons. 63 */ 64 unsigned int xnr = nr - __X32_SYSCALL_BIT; 65 66 if (IS_ENABLED(CONFIG_X86_X32_ABI) && likely(xnr < X32_NR_syscalls)) { 67 xnr = array_index_nospec(xnr, X32_NR_syscalls); 68 regs->ax = x32_sys_call(regs, xnr); 69 return true; 70 } 71 return false; 72 } 73 74 __visible noinstr void do_syscall_64(struct pt_regs *regs, int nr) 75 { 76 add_random_kstack_offset(); 77 nr = syscall_enter_from_user_mode(regs, nr); 78 79 instrumentation_begin(); 80 81 if (!do_syscall_x64(regs, nr) && !do_syscall_x32(regs, nr) && nr != -1) { 82 /* Invalid system call, but still a system call. */ 83 regs->ax = __x64_sys_ni_syscall(regs); 84 } 85 86 instrumentation_end(); 87 syscall_exit_to_user_mode(regs); 88 } 89 #endif 90 91 #if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION) 92 static __always_inline int syscall_32_enter(struct pt_regs *regs) 93 { 94 if (IS_ENABLED(CONFIG_IA32_EMULATION)) 95 current_thread_info()->status |= TS_COMPAT; 96 97 return (int)regs->orig_ax; 98 } 99 100 #ifdef CONFIG_IA32_EMULATION 101 bool __ia32_enabled __ro_after_init = true; 102 #endif 103 104 /* 105 * Invoke a 32-bit syscall. Called with IRQs on in CONTEXT_KERNEL. 106 */ 107 static __always_inline void do_syscall_32_irqs_on(struct pt_regs *regs, int nr) 108 { 109 /* 110 * Convert negative numbers to very high and thus out of range 111 * numbers for comparisons. 112 */ 113 unsigned int unr = nr; 114 115 if (likely(unr < IA32_NR_syscalls)) { 116 unr = array_index_nospec(unr, IA32_NR_syscalls); 117 regs->ax = ia32_sys_call(regs, unr); 118 } else if (nr != -1) { 119 regs->ax = __ia32_sys_ni_syscall(regs); 120 } 121 } 122 123 #ifdef CONFIG_IA32_EMULATION 124 static __always_inline bool int80_is_external(void) 125 { 126 const unsigned int offs = (0x80 / 32) * 0x10; 127 const u32 bit = BIT(0x80 % 32); 128 129 /* The local APIC on XENPV guests is fake */ 130 if (cpu_feature_enabled(X86_FEATURE_XENPV)) 131 return false; 132 133 /* 134 * If vector 0x80 is set in the APIC ISR then this is an external 135 * interrupt. Either from broken hardware or injected by a VMM. 136 * 137 * Note: In guest mode this is only valid for secure guests where 138 * the secure module fully controls the vAPIC exposed to the guest. 139 */ 140 return apic_read(APIC_ISR + offs) & bit; 141 } 142 143 /** 144 * do_int80_emulation - 32-bit legacy syscall C entry from asm 145 * 146 * This entry point can be used by 32-bit and 64-bit programs to perform 147 * 32-bit system calls. Instances of INT $0x80 can be found inline in 148 * various programs and libraries. It is also used by the vDSO's 149 * __kernel_vsyscall fallback for hardware that doesn't support a faster 150 * entry method. Restarted 32-bit system calls also fall back to INT 151 * $0x80 regardless of what instruction was originally used to do the 152 * system call. 153 * 154 * This is considered a slow path. It is not used by most libc 155 * implementations on modern hardware except during process startup. 156 * 157 * The arguments for the INT $0x80 based syscall are on stack in the 158 * pt_regs structure: 159 * eax: system call number 160 * ebx, ecx, edx, esi, edi, ebp: arg1 - arg 6 161 */ 162 __visible noinstr void do_int80_emulation(struct pt_regs *regs) 163 { 164 int nr; 165 166 /* Kernel does not use INT $0x80! */ 167 if (unlikely(!user_mode(regs))) { 168 irqentry_enter(regs); 169 instrumentation_begin(); 170 panic("Unexpected external interrupt 0x80\n"); 171 } 172 173 /* 174 * Establish kernel context for instrumentation, including for 175 * int80_is_external() below which calls into the APIC driver. 176 * Identical for soft and external interrupts. 177 */ 178 enter_from_user_mode(regs); 179 180 instrumentation_begin(); 181 add_random_kstack_offset(); 182 183 /* Validate that this is a soft interrupt to the extent possible */ 184 if (unlikely(int80_is_external())) 185 panic("Unexpected external interrupt 0x80\n"); 186 187 /* 188 * The low level idtentry code pushed -1 into regs::orig_ax 189 * and regs::ax contains the syscall number. 190 * 191 * User tracing code (ptrace or signal handlers) might assume 192 * that the regs::orig_ax contains a 32-bit number on invoking 193 * a 32-bit syscall. 194 * 195 * Establish the syscall convention by saving the 32bit truncated 196 * syscall number in regs::orig_ax and by invalidating regs::ax. 197 */ 198 regs->orig_ax = regs->ax & GENMASK(31, 0); 199 regs->ax = -ENOSYS; 200 201 nr = syscall_32_enter(regs); 202 203 local_irq_enable(); 204 nr = syscall_enter_from_user_mode_work(regs, nr); 205 do_syscall_32_irqs_on(regs, nr); 206 207 instrumentation_end(); 208 syscall_exit_to_user_mode(regs); 209 } 210 #else /* CONFIG_IA32_EMULATION */ 211 212 /* Handles int $0x80 on a 32bit kernel */ 213 __visible noinstr void do_int80_syscall_32(struct pt_regs *regs) 214 { 215 int nr = syscall_32_enter(regs); 216 217 add_random_kstack_offset(); 218 /* 219 * Subtlety here: if ptrace pokes something larger than 2^31-1 into 220 * orig_ax, the int return value truncates it. This matches 221 * the semantics of syscall_get_nr(). 222 */ 223 nr = syscall_enter_from_user_mode(regs, nr); 224 instrumentation_begin(); 225 226 do_syscall_32_irqs_on(regs, nr); 227 228 instrumentation_end(); 229 syscall_exit_to_user_mode(regs); 230 } 231 #endif /* !CONFIG_IA32_EMULATION */ 232 233 static noinstr bool __do_fast_syscall_32(struct pt_regs *regs) 234 { 235 int nr = syscall_32_enter(regs); 236 int res; 237 238 add_random_kstack_offset(); 239 /* 240 * This cannot use syscall_enter_from_user_mode() as it has to 241 * fetch EBP before invoking any of the syscall entry work 242 * functions. 243 */ 244 syscall_enter_from_user_mode_prepare(regs); 245 246 instrumentation_begin(); 247 /* Fetch EBP from where the vDSO stashed it. */ 248 if (IS_ENABLED(CONFIG_X86_64)) { 249 /* 250 * Micro-optimization: the pointer we're following is 251 * explicitly 32 bits, so it can't be out of range. 252 */ 253 res = __get_user(*(u32 *)®s->bp, 254 (u32 __user __force *)(unsigned long)(u32)regs->sp); 255 } else { 256 res = get_user(*(u32 *)®s->bp, 257 (u32 __user __force *)(unsigned long)(u32)regs->sp); 258 } 259 260 if (res) { 261 /* User code screwed up. */ 262 regs->ax = -EFAULT; 263 264 local_irq_disable(); 265 instrumentation_end(); 266 irqentry_exit_to_user_mode(regs); 267 return false; 268 } 269 270 nr = syscall_enter_from_user_mode_work(regs, nr); 271 272 /* Now this is just like a normal syscall. */ 273 do_syscall_32_irqs_on(regs, nr); 274 275 instrumentation_end(); 276 syscall_exit_to_user_mode(regs); 277 return true; 278 } 279 280 /* Returns 0 to return using IRET or 1 to return using SYSEXIT/SYSRETL. */ 281 __visible noinstr long do_fast_syscall_32(struct pt_regs *regs) 282 { 283 /* 284 * Called using the internal vDSO SYSENTER/SYSCALL32 calling 285 * convention. Adjust regs so it looks like we entered using int80. 286 */ 287 unsigned long landing_pad = (unsigned long)current->mm->context.vdso + 288 vdso_image_32.sym_int80_landing_pad; 289 290 /* 291 * SYSENTER loses EIP, and even SYSCALL32 needs us to skip forward 292 * so that 'regs->ip -= 2' lands back on an int $0x80 instruction. 293 * Fix it up. 294 */ 295 regs->ip = landing_pad; 296 297 /* Invoke the syscall. If it failed, keep it simple: use IRET. */ 298 if (!__do_fast_syscall_32(regs)) 299 return 0; 300 301 #ifdef CONFIG_X86_64 302 /* 303 * Opportunistic SYSRETL: if possible, try to return using SYSRETL. 304 * SYSRETL is available on all 64-bit CPUs, so we don't need to 305 * bother with SYSEXIT. 306 * 307 * Unlike 64-bit opportunistic SYSRET, we can't check that CX == IP, 308 * because the ECX fixup above will ensure that this is essentially 309 * never the case. 310 */ 311 return regs->cs == __USER32_CS && regs->ss == __USER_DS && 312 regs->ip == landing_pad && 313 (regs->flags & (X86_EFLAGS_RF | X86_EFLAGS_TF)) == 0; 314 #else 315 /* 316 * Opportunistic SYSEXIT: if possible, try to return using SYSEXIT. 317 * 318 * Unlike 64-bit opportunistic SYSRET, we can't check that CX == IP, 319 * because the ECX fixup above will ensure that this is essentially 320 * never the case. 321 * 322 * We don't allow syscalls at all from VM86 mode, but we still 323 * need to check VM, because we might be returning from sys_vm86. 324 */ 325 return static_cpu_has(X86_FEATURE_SEP) && 326 regs->cs == __USER_CS && regs->ss == __USER_DS && 327 regs->ip == landing_pad && 328 (regs->flags & (X86_EFLAGS_RF | X86_EFLAGS_TF | X86_EFLAGS_VM)) == 0; 329 #endif 330 } 331 332 /* Returns 0 to return using IRET or 1 to return using SYSEXIT/SYSRETL. */ 333 __visible noinstr long do_SYSENTER_32(struct pt_regs *regs) 334 { 335 /* SYSENTER loses RSP, but the vDSO saved it in RBP. */ 336 regs->sp = regs->bp; 337 338 /* SYSENTER clobbers EFLAGS.IF. Assume it was set in usermode. */ 339 regs->flags |= X86_EFLAGS_IF; 340 341 return do_fast_syscall_32(regs); 342 } 343 #endif 344 345 SYSCALL_DEFINE0(ni_syscall) 346 { 347 return -ENOSYS; 348 } 349 350 #ifdef CONFIG_XEN_PV 351 #ifndef CONFIG_PREEMPTION 352 /* 353 * Some hypercalls issued by the toolstack can take many 10s of 354 * seconds. Allow tasks running hypercalls via the privcmd driver to 355 * be voluntarily preempted even if full kernel preemption is 356 * disabled. 357 * 358 * Such preemptible hypercalls are bracketed by 359 * xen_preemptible_hcall_begin() and xen_preemptible_hcall_end() 360 * calls. 361 */ 362 DEFINE_PER_CPU(bool, xen_in_preemptible_hcall); 363 EXPORT_SYMBOL_GPL(xen_in_preemptible_hcall); 364 365 /* 366 * In case of scheduling the flag must be cleared and restored after 367 * returning from schedule as the task might move to a different CPU. 368 */ 369 static __always_inline bool get_and_clear_inhcall(void) 370 { 371 bool inhcall = __this_cpu_read(xen_in_preemptible_hcall); 372 373 __this_cpu_write(xen_in_preemptible_hcall, false); 374 return inhcall; 375 } 376 377 static __always_inline void restore_inhcall(bool inhcall) 378 { 379 __this_cpu_write(xen_in_preemptible_hcall, inhcall); 380 } 381 #else 382 static __always_inline bool get_and_clear_inhcall(void) { return false; } 383 static __always_inline void restore_inhcall(bool inhcall) { } 384 #endif 385 386 static void __xen_pv_evtchn_do_upcall(struct pt_regs *regs) 387 { 388 struct pt_regs *old_regs = set_irq_regs(regs); 389 390 inc_irq_stat(irq_hv_callback_count); 391 392 xen_evtchn_do_upcall(); 393 394 set_irq_regs(old_regs); 395 } 396 397 __visible noinstr void xen_pv_evtchn_do_upcall(struct pt_regs *regs) 398 { 399 irqentry_state_t state = irqentry_enter(regs); 400 bool inhcall; 401 402 instrumentation_begin(); 403 run_sysvec_on_irqstack_cond(__xen_pv_evtchn_do_upcall, regs); 404 405 inhcall = get_and_clear_inhcall(); 406 if (inhcall && !WARN_ON_ONCE(state.exit_rcu)) { 407 irqentry_exit_cond_resched(); 408 instrumentation_end(); 409 restore_inhcall(inhcall); 410 } else { 411 instrumentation_end(); 412 irqentry_exit(regs, state); 413 } 414 } 415 #endif /* CONFIG_XEN_PV */ 416