entry_64_compat.S 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Compatibility mode system call entry point for x86-64.
  4. *
  5. * Copyright 2000-2002 Andi Kleen, SuSE Labs.
  6. */
  7. #include "calling.h"
  8. #include <asm/asm-offsets.h>
  9. #include <asm/current.h>
  10. #include <asm/errno.h>
  11. #include <asm/ia32_unistd.h>
  12. #include <asm/thread_info.h>
  13. #include <asm/segment.h>
  14. #include <asm/irqflags.h>
  15. #include <asm/asm.h>
  16. #include <asm/smap.h>
  17. #include <linux/linkage.h>
  18. #include <linux/err.h>
  19. .section .entry.text, "ax"
  20. /*
  21. * 32-bit SYSENTER entry.
  22. *
  23. * 32-bit system calls through the vDSO's __kernel_vsyscall enter here
  24. * on 64-bit kernels running on Intel CPUs.
  25. *
  26. * The SYSENTER instruction, in principle, should *only* occur in the
  27. * vDSO. In practice, a small number of Android devices were shipped
  28. * with a copy of Bionic that inlined a SYSENTER instruction. This
  29. * never happened in any of Google's Bionic versions -- it only happened
  30. * in a narrow range of Intel-provided versions.
  31. *
  32. * SYSENTER loads SS, RSP, CS, and RIP from previously programmed MSRs.
  33. * IF and VM in RFLAGS are cleared (IOW: interrupts are off).
  34. * SYSENTER does not save anything on the stack,
  35. * and does not save old RIP (!!!), RSP, or RFLAGS.
  36. *
  37. * Arguments:
  38. * eax system call number
  39. * ebx arg1
  40. * ecx arg2
  41. * edx arg3
  42. * esi arg4
  43. * edi arg5
  44. * ebp user stack
  45. * 0(%ebp) arg6
  46. */
  47. ENTRY(entry_SYSENTER_compat)
  48. /* Interrupts are off on entry. */
  49. SWAPGS
  50. /* We are about to clobber %rsp anyway, clobbering here is OK */
  51. SWITCH_TO_KERNEL_CR3 scratch_reg=%rsp
  52. movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
  53. /*
  54. * User tracing code (ptrace or signal handlers) might assume that
  55. * the saved RAX contains a 32-bit number when we're invoking a 32-bit
  56. * syscall. Just in case the high bits are nonzero, zero-extend
  57. * the syscall number. (This could almost certainly be deleted
  58. * with no ill effects.)
  59. */
  60. movl %eax, %eax
  61. /* Construct struct pt_regs on stack */
  62. pushq $__USER32_DS /* pt_regs->ss */
  63. pushq %rbp /* pt_regs->sp (stashed in bp) */
  64. /*
  65. * Push flags. This is nasty. First, interrupts are currently
  66. * off, but we need pt_regs->flags to have IF set. Second, even
  67. * if TF was set when SYSENTER started, it's clear by now. We fix
  68. * that later using TIF_SINGLESTEP.
  69. */
  70. pushfq /* pt_regs->flags (except IF = 0) */
  71. orl $X86_EFLAGS_IF, (%rsp) /* Fix saved flags */
  72. pushq $__USER32_CS /* pt_regs->cs */
  73. pushq $0 /* pt_regs->ip = 0 (placeholder) */
  74. pushq %rax /* pt_regs->orig_ax */
  75. pushq %rdi /* pt_regs->di */
  76. pushq %rsi /* pt_regs->si */
  77. pushq %rdx /* pt_regs->dx */
  78. pushq %rcx /* pt_regs->cx */
  79. pushq $-ENOSYS /* pt_regs->ax */
  80. pushq $0 /* pt_regs->r8 = 0 */
  81. xorl %r8d, %r8d /* nospec r8 */
  82. pushq $0 /* pt_regs->r9 = 0 */
  83. xorl %r9d, %r9d /* nospec r9 */
  84. pushq $0 /* pt_regs->r10 = 0 */
  85. xorl %r10d, %r10d /* nospec r10 */
  86. pushq $0 /* pt_regs->r11 = 0 */
  87. xorl %r11d, %r11d /* nospec r11 */
  88. pushq %rbx /* pt_regs->rbx */
  89. xorl %ebx, %ebx /* nospec rbx */
  90. pushq %rbp /* pt_regs->rbp (will be overwritten) */
  91. xorl %ebp, %ebp /* nospec rbp */
  92. pushq $0 /* pt_regs->r12 = 0 */
  93. xorl %r12d, %r12d /* nospec r12 */
  94. pushq $0 /* pt_regs->r13 = 0 */
  95. xorl %r13d, %r13d /* nospec r13 */
  96. pushq $0 /* pt_regs->r14 = 0 */
  97. xorl %r14d, %r14d /* nospec r14 */
  98. pushq $0 /* pt_regs->r15 = 0 */
  99. xorl %r15d, %r15d /* nospec r15 */
  100. cld
  101. /*
  102. * SYSENTER doesn't filter flags, so we need to clear NT and AC
  103. * ourselves. To save a few cycles, we can check whether
  104. * either was set instead of doing an unconditional popfq.
  105. * This needs to happen before enabling interrupts so that
  106. * we don't get preempted with NT set.
  107. *
  108. * If TF is set, we will single-step all the way to here -- do_debug
  109. * will ignore all the traps. (Yes, this is slow, but so is
  110. * single-stepping in general. This allows us to avoid having
  111. * a more complicated code to handle the case where a user program
  112. * forces us to single-step through the SYSENTER entry code.)
  113. *
  114. * NB.: .Lsysenter_fix_flags is a label with the code under it moved
  115. * out-of-line as an optimization: NT is unlikely to be set in the
  116. * majority of the cases and instead of polluting the I$ unnecessarily,
  117. * we're keeping that code behind a branch which will predict as
  118. * not-taken and therefore its instructions won't be fetched.
  119. */
  120. testl $X86_EFLAGS_NT|X86_EFLAGS_AC|X86_EFLAGS_TF, EFLAGS(%rsp)
  121. jnz .Lsysenter_fix_flags
  122. .Lsysenter_flags_fixed:
  123. /*
  124. * User mode is traced as though IRQs are on, and SYSENTER
  125. * turned them off.
  126. */
  127. TRACE_IRQS_OFF
  128. movq %rsp, %rdi
  129. call do_fast_syscall_32
  130. /* XEN PV guests always use IRET path */
  131. ALTERNATIVE "testl %eax, %eax; jz .Lsyscall_32_done", \
  132. "jmp .Lsyscall_32_done", X86_FEATURE_XENPV
  133. jmp sysret32_from_system_call
  134. .Lsysenter_fix_flags:
  135. pushq $X86_EFLAGS_FIXED
  136. popfq
  137. jmp .Lsysenter_flags_fixed
  138. GLOBAL(__end_entry_SYSENTER_compat)
  139. ENDPROC(entry_SYSENTER_compat)
  140. /*
  141. * 32-bit SYSCALL entry.
  142. *
  143. * 32-bit system calls through the vDSO's __kernel_vsyscall enter here
  144. * on 64-bit kernels running on AMD CPUs.
  145. *
  146. * The SYSCALL instruction, in principle, should *only* occur in the
  147. * vDSO. In practice, it appears that this really is the case.
  148. * As evidence:
  149. *
  150. * - The calling convention for SYSCALL has changed several times without
  151. * anyone noticing.
  152. *
  153. * - Prior to the in-kernel X86_BUG_SYSRET_SS_ATTRS fixup, anything
  154. * user task that did SYSCALL without immediately reloading SS
  155. * would randomly crash.
  156. *
  157. * - Most programmers do not directly target AMD CPUs, and the 32-bit
  158. * SYSCALL instruction does not exist on Intel CPUs. Even on AMD
  159. * CPUs, Linux disables the SYSCALL instruction on 32-bit kernels
  160. * because the SYSCALL instruction in legacy/native 32-bit mode (as
  161. * opposed to compat mode) is sufficiently poorly designed as to be
  162. * essentially unusable.
  163. *
  164. * 32-bit SYSCALL saves RIP to RCX, clears RFLAGS.RF, then saves
  165. * RFLAGS to R11, then loads new SS, CS, and RIP from previously
  166. * programmed MSRs. RFLAGS gets masked by a value from another MSR
  167. * (so CLD and CLAC are not needed). SYSCALL does not save anything on
  168. * the stack and does not change RSP.
  169. *
  170. * Note: RFLAGS saving+masking-with-MSR happens only in Long mode
  171. * (in legacy 32-bit mode, IF, RF and VM bits are cleared and that's it).
  172. * Don't get confused: RFLAGS saving+masking depends on Long Mode Active bit
  173. * (EFER.LMA=1), NOT on bitness of userspace where SYSCALL executes
  174. * or target CS descriptor's L bit (SYSCALL does not read segment descriptors).
  175. *
  176. * Arguments:
  177. * eax system call number
  178. * ecx return address
  179. * ebx arg1
  180. * ebp arg2 (note: not saved in the stack frame, should not be touched)
  181. * edx arg3
  182. * esi arg4
  183. * edi arg5
  184. * esp user stack
  185. * 0(%esp) arg6
  186. */
  187. ENTRY(entry_SYSCALL_compat)
  188. /* Interrupts are off on entry. */
  189. swapgs
  190. /* Stash user ESP */
  191. movl %esp, %r8d
  192. /* Use %rsp as scratch reg. User ESP is stashed in r8 */
  193. SWITCH_TO_KERNEL_CR3 scratch_reg=%rsp
  194. /* Switch to the kernel stack */
  195. movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
  196. /* Construct struct pt_regs on stack */
  197. pushq $__USER32_DS /* pt_regs->ss */
  198. pushq %r8 /* pt_regs->sp */
  199. pushq %r11 /* pt_regs->flags */
  200. pushq $__USER32_CS /* pt_regs->cs */
  201. pushq %rcx /* pt_regs->ip */
  202. GLOBAL(entry_SYSCALL_compat_after_hwframe)
  203. movl %eax, %eax /* discard orig_ax high bits */
  204. pushq %rax /* pt_regs->orig_ax */
  205. pushq %rdi /* pt_regs->di */
  206. pushq %rsi /* pt_regs->si */
  207. xorl %esi, %esi /* nospec si */
  208. pushq %rdx /* pt_regs->dx */
  209. xorl %edx, %edx /* nospec dx */
  210. pushq %rbp /* pt_regs->cx (stashed in bp) */
  211. xorl %ecx, %ecx /* nospec cx */
  212. pushq $-ENOSYS /* pt_regs->ax */
  213. pushq $0 /* pt_regs->r8 = 0 */
  214. xorl %r8d, %r8d /* nospec r8 */
  215. pushq $0 /* pt_regs->r9 = 0 */
  216. xorl %r9d, %r9d /* nospec r9 */
  217. pushq $0 /* pt_regs->r10 = 0 */
  218. xorl %r10d, %r10d /* nospec r10 */
  219. pushq $0 /* pt_regs->r11 = 0 */
  220. xorl %r11d, %r11d /* nospec r11 */
  221. pushq %rbx /* pt_regs->rbx */
  222. xorl %ebx, %ebx /* nospec rbx */
  223. pushq %rbp /* pt_regs->rbp (will be overwritten) */
  224. xorl %ebp, %ebp /* nospec rbp */
  225. pushq $0 /* pt_regs->r12 = 0 */
  226. xorl %r12d, %r12d /* nospec r12 */
  227. pushq $0 /* pt_regs->r13 = 0 */
  228. xorl %r13d, %r13d /* nospec r13 */
  229. pushq $0 /* pt_regs->r14 = 0 */
  230. xorl %r14d, %r14d /* nospec r14 */
  231. pushq $0 /* pt_regs->r15 = 0 */
  232. xorl %r15d, %r15d /* nospec r15 */
  233. /*
  234. * User mode is traced as though IRQs are on, and SYSENTER
  235. * turned them off.
  236. */
  237. TRACE_IRQS_OFF
  238. movq %rsp, %rdi
  239. call do_fast_syscall_32
  240. /* XEN PV guests always use IRET path */
  241. ALTERNATIVE "testl %eax, %eax; jz .Lsyscall_32_done", \
  242. "jmp .Lsyscall_32_done", X86_FEATURE_XENPV
  243. /* Opportunistic SYSRET */
  244. sysret32_from_system_call:
  245. /*
  246. * We are not going to return to userspace from the trampoline
  247. * stack. So let's erase the thread stack right now.
  248. */
  249. STACKLEAK_ERASE
  250. TRACE_IRQS_ON /* User mode traces as IRQs on. */
  251. movq RBX(%rsp), %rbx /* pt_regs->rbx */
  252. movq RBP(%rsp), %rbp /* pt_regs->rbp */
  253. movq EFLAGS(%rsp), %r11 /* pt_regs->flags (in r11) */
  254. movq RIP(%rsp), %rcx /* pt_regs->ip (in rcx) */
  255. addq $RAX, %rsp /* Skip r8-r15 */
  256. popq %rax /* pt_regs->rax */
  257. popq %rdx /* Skip pt_regs->cx */
  258. popq %rdx /* pt_regs->dx */
  259. popq %rsi /* pt_regs->si */
  260. popq %rdi /* pt_regs->di */
  261. /*
  262. * USERGS_SYSRET32 does:
  263. * GSBASE = user's GS base
  264. * EIP = ECX
  265. * RFLAGS = R11
  266. * CS = __USER32_CS
  267. * SS = __USER_DS
  268. *
  269. * ECX will not match pt_regs->cx, but we're returning to a vDSO
  270. * trampoline that will fix up RCX, so this is okay.
  271. *
  272. * R12-R15 are callee-saved, so they contain whatever was in them
  273. * when the system call started, which is already known to user
  274. * code. We zero R8-R10 to avoid info leaks.
  275. */
  276. movq RSP-ORIG_RAX(%rsp), %rsp
  277. /*
  278. * The original userspace %rsp (RSP-ORIG_RAX(%rsp)) is stored
  279. * on the process stack which is not mapped to userspace and
  280. * not readable after we SWITCH_TO_USER_CR3. Delay the CR3
  281. * switch until after after the last reference to the process
  282. * stack.
  283. *
  284. * %r8/%r9 are zeroed before the sysret, thus safe to clobber.
  285. */
  286. SWITCH_TO_USER_CR3_NOSTACK scratch_reg=%r8 scratch_reg2=%r9
  287. xorl %r8d, %r8d
  288. xorl %r9d, %r9d
  289. xorl %r10d, %r10d
  290. swapgs
  291. sysretl
  292. END(entry_SYSCALL_compat)
  293. /*
  294. * 32-bit legacy system call entry.
  295. *
  296. * 32-bit x86 Linux system calls traditionally used the INT $0x80
  297. * instruction. INT $0x80 lands here.
  298. *
  299. * This entry point can be used by 32-bit and 64-bit programs to perform
  300. * 32-bit system calls. Instances of INT $0x80 can be found inline in
  301. * various programs and libraries. It is also used by the vDSO's
  302. * __kernel_vsyscall fallback for hardware that doesn't support a faster
  303. * entry method. Restarted 32-bit system calls also fall back to INT
  304. * $0x80 regardless of what instruction was originally used to do the
  305. * system call.
  306. *
  307. * This is considered a slow path. It is not used by most libc
  308. * implementations on modern hardware except during process startup.
  309. *
  310. * Arguments:
  311. * eax system call number
  312. * ebx arg1
  313. * ecx arg2
  314. * edx arg3
  315. * esi arg4
  316. * edi arg5
  317. * ebp arg6
  318. */
  319. ENTRY(entry_INT80_compat)
  320. /*
  321. * Interrupts are off on entry.
  322. */
  323. ASM_CLAC /* Do this early to minimize exposure */
  324. SWAPGS
  325. /*
  326. * User tracing code (ptrace or signal handlers) might assume that
  327. * the saved RAX contains a 32-bit number when we're invoking a 32-bit
  328. * syscall. Just in case the high bits are nonzero, zero-extend
  329. * the syscall number. (This could almost certainly be deleted
  330. * with no ill effects.)
  331. */
  332. movl %eax, %eax
  333. /* switch to thread stack expects orig_ax and rdi to be pushed */
  334. pushq %rax /* pt_regs->orig_ax */
  335. pushq %rdi /* pt_regs->di */
  336. /* Need to switch before accessing the thread stack. */
  337. SWITCH_TO_KERNEL_CR3 scratch_reg=%rdi
  338. movq %rsp, %rdi
  339. movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
  340. pushq 6*8(%rdi) /* regs->ss */
  341. pushq 5*8(%rdi) /* regs->rsp */
  342. pushq 4*8(%rdi) /* regs->eflags */
  343. pushq 3*8(%rdi) /* regs->cs */
  344. pushq 2*8(%rdi) /* regs->ip */
  345. pushq 1*8(%rdi) /* regs->orig_ax */
  346. pushq (%rdi) /* pt_regs->di */
  347. pushq %rsi /* pt_regs->si */
  348. xorl %esi, %esi /* nospec si */
  349. pushq %rdx /* pt_regs->dx */
  350. xorl %edx, %edx /* nospec dx */
  351. pushq %rcx /* pt_regs->cx */
  352. xorl %ecx, %ecx /* nospec cx */
  353. pushq $-ENOSYS /* pt_regs->ax */
  354. pushq %r8 /* pt_regs->r8 */
  355. xorl %r8d, %r8d /* nospec r8 */
  356. pushq %r9 /* pt_regs->r9 */
  357. xorl %r9d, %r9d /* nospec r9 */
  358. pushq %r10 /* pt_regs->r10*/
  359. xorl %r10d, %r10d /* nospec r10 */
  360. pushq %r11 /* pt_regs->r11 */
  361. xorl %r11d, %r11d /* nospec r11 */
  362. pushq %rbx /* pt_regs->rbx */
  363. xorl %ebx, %ebx /* nospec rbx */
  364. pushq %rbp /* pt_regs->rbp */
  365. xorl %ebp, %ebp /* nospec rbp */
  366. pushq %r12 /* pt_regs->r12 */
  367. xorl %r12d, %r12d /* nospec r12 */
  368. pushq %r13 /* pt_regs->r13 */
  369. xorl %r13d, %r13d /* nospec r13 */
  370. pushq %r14 /* pt_regs->r14 */
  371. xorl %r14d, %r14d /* nospec r14 */
  372. pushq %r15 /* pt_regs->r15 */
  373. xorl %r15d, %r15d /* nospec r15 */
  374. cld
  375. /*
  376. * User mode is traced as though IRQs are on, and the interrupt
  377. * gate turned them off.
  378. */
  379. TRACE_IRQS_OFF
  380. movq %rsp, %rdi
  381. call do_int80_syscall_32
  382. .Lsyscall_32_done:
  383. /* Go back to user mode. */
  384. TRACE_IRQS_ON
  385. jmp swapgs_restore_regs_and_return_to_usermode
  386. END(entry_INT80_compat)