entry_64_compat.S 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 <asm/asm-offsets.h>
  8. #include <asm/current.h>
  9. #include <asm/errno.h>
  10. #include <asm/thread_info.h>
  11. #include <asm/segment.h>
  12. #include <asm/irqflags.h>
  13. #include <asm/asm.h>
  14. #include <asm/smap.h>
  15. #include <asm/nospec-branch.h>
  16. #include <linux/linkage.h>
  17. #include <linux/err.h>
  18. #include "calling.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. SYM_CODE_START(entry_SYSENTER_compat)
  48. UNWIND_HINT_ENTRY
  49. ENDBR
  50. /* Interrupts are off on entry. */
  51. swapgs
  52. pushq %rax
  53. SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
  54. popq %rax
  55. movq PER_CPU_VAR(pcpu_hot + X86_top_of_stack), %rsp
  56. /* Construct struct pt_regs on stack */
  57. pushq $__USER_DS /* pt_regs->ss */
  58. pushq $0 /* pt_regs->sp = 0 (placeholder) */
  59. /*
  60. * Push flags. This is nasty. First, interrupts are currently
  61. * off, but we need pt_regs->flags to have IF set. Second, if TS
  62. * was set in usermode, it's still set, and we're singlestepping
  63. * through this code. do_SYSENTER_32() will fix up IF.
  64. */
  65. pushfq /* pt_regs->flags (except IF = 0) */
  66. pushq $__USER32_CS /* pt_regs->cs */
  67. pushq $0 /* pt_regs->ip = 0 (placeholder) */
  68. SYM_INNER_LABEL(entry_SYSENTER_compat_after_hwframe, SYM_L_GLOBAL)
  69. /*
  70. * User tracing code (ptrace or signal handlers) might assume that
  71. * the saved RAX contains a 32-bit number when we're invoking a 32-bit
  72. * syscall. Just in case the high bits are nonzero, zero-extend
  73. * the syscall number. (This could almost certainly be deleted
  74. * with no ill effects.)
  75. */
  76. movl %eax, %eax
  77. pushq %rax /* pt_regs->orig_ax */
  78. PUSH_AND_CLEAR_REGS rax=$-ENOSYS
  79. UNWIND_HINT_REGS
  80. cld
  81. /*
  82. * SYSENTER doesn't filter flags, so we need to clear NT and AC
  83. * ourselves. To save a few cycles, we can check whether
  84. * either was set instead of doing an unconditional popfq.
  85. * This needs to happen before enabling interrupts so that
  86. * we don't get preempted with NT set.
  87. *
  88. * If TF is set, we will single-step all the way to here -- do_debug
  89. * will ignore all the traps. (Yes, this is slow, but so is
  90. * single-stepping in general. This allows us to avoid having
  91. * a more complicated code to handle the case where a user program
  92. * forces us to single-step through the SYSENTER entry code.)
  93. *
  94. * NB.: .Lsysenter_fix_flags is a label with the code under it moved
  95. * out-of-line as an optimization: NT is unlikely to be set in the
  96. * majority of the cases and instead of polluting the I$ unnecessarily,
  97. * we're keeping that code behind a branch which will predict as
  98. * not-taken and therefore its instructions won't be fetched.
  99. */
  100. testl $X86_EFLAGS_NT|X86_EFLAGS_AC|X86_EFLAGS_TF, EFLAGS(%rsp)
  101. jnz .Lsysenter_fix_flags
  102. .Lsysenter_flags_fixed:
  103. /*
  104. * CPU bugs mitigations mechanisms can call other functions. They
  105. * should be invoked after making sure TF is cleared because
  106. * single-step is ignored only for instructions inside the
  107. * entry_SYSENTER_compat function.
  108. */
  109. IBRS_ENTER
  110. UNTRAIN_RET
  111. CLEAR_BRANCH_HISTORY
  112. movq %rsp, %rdi
  113. call do_SYSENTER_32
  114. jmp sysret32_from_system_call
  115. .Lsysenter_fix_flags:
  116. pushq $X86_EFLAGS_FIXED
  117. popfq
  118. jmp .Lsysenter_flags_fixed
  119. SYM_INNER_LABEL(__end_entry_SYSENTER_compat, SYM_L_GLOBAL)
  120. SYM_CODE_END(entry_SYSENTER_compat)
  121. /*
  122. * 32-bit SYSCALL entry.
  123. *
  124. * 32-bit system calls through the vDSO's __kernel_vsyscall enter here
  125. * on 64-bit kernels running on AMD CPUs.
  126. *
  127. * The SYSCALL instruction, in principle, should *only* occur in the
  128. * vDSO. In practice, it appears that this really is the case.
  129. * As evidence:
  130. *
  131. * - The calling convention for SYSCALL has changed several times without
  132. * anyone noticing.
  133. *
  134. * - Prior to the in-kernel X86_BUG_SYSRET_SS_ATTRS fixup, anything
  135. * user task that did SYSCALL without immediately reloading SS
  136. * would randomly crash.
  137. *
  138. * - Most programmers do not directly target AMD CPUs, and the 32-bit
  139. * SYSCALL instruction does not exist on Intel CPUs. Even on AMD
  140. * CPUs, Linux disables the SYSCALL instruction on 32-bit kernels
  141. * because the SYSCALL instruction in legacy/native 32-bit mode (as
  142. * opposed to compat mode) is sufficiently poorly designed as to be
  143. * essentially unusable.
  144. *
  145. * 32-bit SYSCALL saves RIP to RCX, clears RFLAGS.RF, then saves
  146. * RFLAGS to R11, then loads new SS, CS, and RIP from previously
  147. * programmed MSRs. RFLAGS gets masked by a value from another MSR
  148. * (so CLD and CLAC are not needed). SYSCALL does not save anything on
  149. * the stack and does not change RSP.
  150. *
  151. * Note: RFLAGS saving+masking-with-MSR happens only in Long mode
  152. * (in legacy 32-bit mode, IF, RF and VM bits are cleared and that's it).
  153. * Don't get confused: RFLAGS saving+masking depends on Long Mode Active bit
  154. * (EFER.LMA=1), NOT on bitness of userspace where SYSCALL executes
  155. * or target CS descriptor's L bit (SYSCALL does not read segment descriptors).
  156. *
  157. * Arguments:
  158. * eax system call number
  159. * ecx return address
  160. * ebx arg1
  161. * ebp arg2 (note: not saved in the stack frame, should not be touched)
  162. * edx arg3
  163. * esi arg4
  164. * edi arg5
  165. * esp user stack
  166. * 0(%esp) arg6
  167. */
  168. SYM_CODE_START(entry_SYSCALL_compat)
  169. UNWIND_HINT_ENTRY
  170. ENDBR
  171. /* Interrupts are off on entry. */
  172. swapgs
  173. /* Stash user ESP */
  174. movl %esp, %r8d
  175. /* Use %rsp as scratch reg. User ESP is stashed in r8 */
  176. SWITCH_TO_KERNEL_CR3 scratch_reg=%rsp
  177. /* Switch to the kernel stack */
  178. movq PER_CPU_VAR(pcpu_hot + X86_top_of_stack), %rsp
  179. SYM_INNER_LABEL(entry_SYSCALL_compat_safe_stack, SYM_L_GLOBAL)
  180. ANNOTATE_NOENDBR
  181. /* Construct struct pt_regs on stack */
  182. pushq $__USER_DS /* pt_regs->ss */
  183. pushq %r8 /* pt_regs->sp */
  184. pushq %r11 /* pt_regs->flags */
  185. pushq $__USER32_CS /* pt_regs->cs */
  186. pushq %rcx /* pt_regs->ip */
  187. SYM_INNER_LABEL(entry_SYSCALL_compat_after_hwframe, SYM_L_GLOBAL)
  188. movl %eax, %eax /* discard orig_ax high bits */
  189. pushq %rax /* pt_regs->orig_ax */
  190. PUSH_AND_CLEAR_REGS rcx=%rbp rax=$-ENOSYS
  191. UNWIND_HINT_REGS
  192. IBRS_ENTER
  193. UNTRAIN_RET
  194. CLEAR_BRANCH_HISTORY
  195. movq %rsp, %rdi
  196. call do_fast_syscall_32
  197. sysret32_from_system_call:
  198. /* XEN PV guests always use IRET path */
  199. ALTERNATIVE "testb %al, %al; jz swapgs_restore_regs_and_return_to_usermode", \
  200. "jmp swapgs_restore_regs_and_return_to_usermode", X86_FEATURE_XENPV
  201. /*
  202. * Opportunistic SYSRET
  203. *
  204. * We are not going to return to userspace from the trampoline
  205. * stack. So let's erase the thread stack right now.
  206. */
  207. STACKLEAK_ERASE
  208. IBRS_EXIT
  209. movq RBX(%rsp), %rbx /* pt_regs->rbx */
  210. movq RBP(%rsp), %rbp /* pt_regs->rbp */
  211. movq EFLAGS(%rsp), %r11 /* pt_regs->flags (in r11) */
  212. movq RIP(%rsp), %rcx /* pt_regs->ip (in rcx) */
  213. addq $RAX, %rsp /* Skip r8-r15 */
  214. popq %rax /* pt_regs->rax */
  215. popq %rdx /* Skip pt_regs->cx */
  216. popq %rdx /* pt_regs->dx */
  217. popq %rsi /* pt_regs->si */
  218. popq %rdi /* pt_regs->di */
  219. /*
  220. * USERGS_SYSRET32 does:
  221. * GSBASE = user's GS base
  222. * EIP = ECX
  223. * RFLAGS = R11
  224. * CS = __USER32_CS
  225. * SS = __USER_DS
  226. *
  227. * ECX will not match pt_regs->cx, but we're returning to a vDSO
  228. * trampoline that will fix up RCX, so this is okay.
  229. *
  230. * R12-R15 are callee-saved, so they contain whatever was in them
  231. * when the system call started, which is already known to user
  232. * code. We zero R8-R10 to avoid info leaks.
  233. */
  234. movq RSP-ORIG_RAX(%rsp), %rsp
  235. SYM_INNER_LABEL(entry_SYSRETL_compat_unsafe_stack, SYM_L_GLOBAL)
  236. ANNOTATE_NOENDBR
  237. /*
  238. * The original userspace %rsp (RSP-ORIG_RAX(%rsp)) is stored
  239. * on the process stack which is not mapped to userspace and
  240. * not readable after we SWITCH_TO_USER_CR3. Delay the CR3
  241. * switch until after after the last reference to the process
  242. * stack.
  243. *
  244. * %r8/%r9 are zeroed before the sysret, thus safe to clobber.
  245. */
  246. SWITCH_TO_USER_CR3_NOSTACK scratch_reg=%r8 scratch_reg2=%r9
  247. xorl %r8d, %r8d
  248. xorl %r9d, %r9d
  249. xorl %r10d, %r10d
  250. swapgs
  251. CLEAR_CPU_BUFFERS
  252. sysretl
  253. SYM_INNER_LABEL(entry_SYSRETL_compat_end, SYM_L_GLOBAL)
  254. ANNOTATE_NOENDBR
  255. int3
  256. SYM_CODE_END(entry_SYSCALL_compat)
  257. /*
  258. * int 0x80 is used by 32 bit mode as a system call entry. Normally idt entries
  259. * point to C routines, however since this is a system call interface the branch
  260. * history needs to be scrubbed to protect against BHI attacks, and that
  261. * scrubbing needs to take place in assembly code prior to entering any C
  262. * routines.
  263. */
  264. SYM_CODE_START(int80_emulation)
  265. ANNOTATE_NOENDBR
  266. UNWIND_HINT_FUNC
  267. CLEAR_BRANCH_HISTORY
  268. jmp do_int80_emulation
  269. SYM_CODE_END(int80_emulation)