entry_64_compat.S 8.8 KB

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