entry_64_compat.S 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * Compatibility mode system call entry point for x86-64.
  3. *
  4. * Copyright 2000-2002 Andi Kleen, SuSE Labs.
  5. */
  6. #include "calling.h"
  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/pgtable_types.h>
  17. #include <asm/kaiser.h>
  18. #include <linux/linkage.h>
  19. #include <linux/err.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. ENTRY(entry_SYSENTER_compat)
  49. /* Interrupts are off on entry. */
  50. SWAPGS_UNSAFE_STACK
  51. SWITCH_KERNEL_CR3_NO_STACK
  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. xorq %r8, %r8 /* nospec r8 */
  82. pushq $0 /* pt_regs->r9 = 0 */
  83. xorq %r9, %r9 /* nospec r9 */
  84. pushq $0 /* pt_regs->r10 = 0 */
  85. xorq %r10, %r10 /* nospec r10 */
  86. pushq $0 /* pt_regs->r11 = 0 */
  87. xorq %r11, %r11 /* 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. xorq %r12, %r12 /* nospec r12 */
  94. pushq $0 /* pt_regs->r13 = 0 */
  95. xorq %r13, %r13 /* nospec r13 */
  96. pushq $0 /* pt_regs->r14 = 0 */
  97. xorq %r14, %r14 /* nospec r14 */
  98. pushq $0 /* pt_regs->r15 = 0 */
  99. xorq %r15, %r15 /* 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_UNSAFE_STACK
  190. SWITCH_KERNEL_CR3_NO_STACK
  191. /* Stash user ESP and switch to the kernel stack. */
  192. movl %esp, %r8d
  193. movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
  194. /* Zero-extending 32-bit regs, do not remove */
  195. movl %eax, %eax
  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. pushq %rax /* pt_regs->orig_ax */
  203. pushq %rdi /* pt_regs->di */
  204. pushq %rsi /* pt_regs->si */
  205. pushq %rdx /* pt_regs->dx */
  206. pushq %rbp /* pt_regs->cx (stashed in bp) */
  207. pushq $-ENOSYS /* pt_regs->ax */
  208. pushq $0 /* pt_regs->r8 = 0 */
  209. xorq %r8, %r8 /* nospec r8 */
  210. pushq $0 /* pt_regs->r9 = 0 */
  211. xorq %r9, %r9 /* nospec r9 */
  212. pushq $0 /* pt_regs->r10 = 0 */
  213. xorq %r10, %r10 /* nospec r10 */
  214. pushq $0 /* pt_regs->r11 = 0 */
  215. xorq %r11, %r11 /* nospec r11 */
  216. pushq %rbx /* pt_regs->rbx */
  217. xorl %ebx, %ebx /* nospec rbx */
  218. pushq %rbp /* pt_regs->rbp (will be overwritten) */
  219. xorl %ebp, %ebp /* nospec rbp */
  220. pushq $0 /* pt_regs->r12 = 0 */
  221. xorq %r12, %r12 /* nospec r12 */
  222. pushq $0 /* pt_regs->r13 = 0 */
  223. xorq %r13, %r13 /* nospec r13 */
  224. pushq $0 /* pt_regs->r14 = 0 */
  225. xorq %r14, %r14 /* nospec r14 */
  226. pushq $0 /* pt_regs->r15 = 0 */
  227. xorq %r15, %r15 /* nospec r15 */
  228. /*
  229. * User mode is traced as though IRQs are on, and SYSENTER
  230. * turned them off.
  231. */
  232. TRACE_IRQS_OFF
  233. movq %rsp, %rdi
  234. call do_fast_syscall_32
  235. /* XEN PV guests always use IRET path */
  236. ALTERNATIVE "testl %eax, %eax; jz .Lsyscall_32_done", \
  237. "jmp .Lsyscall_32_done", X86_FEATURE_XENPV
  238. /* Opportunistic SYSRET */
  239. sysret32_from_system_call:
  240. TRACE_IRQS_ON /* User mode traces as IRQs on. */
  241. movq RBX(%rsp), %rbx /* pt_regs->rbx */
  242. movq RBP(%rsp), %rbp /* pt_regs->rbp */
  243. movq EFLAGS(%rsp), %r11 /* pt_regs->flags (in r11) */
  244. movq RIP(%rsp), %rcx /* pt_regs->ip (in rcx) */
  245. addq $RAX, %rsp /* Skip r8-r15 */
  246. popq %rax /* pt_regs->rax */
  247. popq %rdx /* Skip pt_regs->cx */
  248. popq %rdx /* pt_regs->dx */
  249. popq %rsi /* pt_regs->si */
  250. popq %rdi /* pt_regs->di */
  251. /*
  252. * USERGS_SYSRET32 does:
  253. * GSBASE = user's GS base
  254. * EIP = ECX
  255. * RFLAGS = R11
  256. * CS = __USER32_CS
  257. * SS = __USER_DS
  258. *
  259. * ECX will not match pt_regs->cx, but we're returning to a vDSO
  260. * trampoline that will fix up RCX, so this is okay.
  261. *
  262. * R12-R15 are callee-saved, so they contain whatever was in them
  263. * when the system call started, which is already known to user
  264. * code. We zero R8-R10 to avoid info leaks.
  265. */
  266. xorq %r8, %r8
  267. xorq %r9, %r9
  268. xorq %r10, %r10
  269. SWITCH_USER_CR3
  270. movq RSP-ORIG_RAX(%rsp), %rsp
  271. swapgs
  272. sysretl
  273. END(entry_SYSCALL_compat)
  274. /*
  275. * 32-bit legacy system call entry.
  276. *
  277. * 32-bit x86 Linux system calls traditionally used the INT $0x80
  278. * instruction. INT $0x80 lands here.
  279. *
  280. * This entry point can be used by 32-bit and 64-bit programs to perform
  281. * 32-bit system calls. Instances of INT $0x80 can be found inline in
  282. * various programs and libraries. It is also used by the vDSO's
  283. * __kernel_vsyscall fallback for hardware that doesn't support a faster
  284. * entry method. Restarted 32-bit system calls also fall back to INT
  285. * $0x80 regardless of what instruction was originally used to do the
  286. * system call.
  287. *
  288. * This is considered a slow path. It is not used by most libc
  289. * implementations on modern hardware except during process startup.
  290. *
  291. * Arguments:
  292. * eax system call number
  293. * ebx arg1
  294. * ecx arg2
  295. * edx arg3
  296. * esi arg4
  297. * edi arg5
  298. * ebp arg6
  299. */
  300. ENTRY(entry_INT80_compat)
  301. /*
  302. * Interrupts are off on entry.
  303. */
  304. PARAVIRT_ADJUST_EXCEPTION_FRAME
  305. ASM_CLAC /* Do this early to minimize exposure */
  306. SWAPGS
  307. SWITCH_KERNEL_CR3_NO_STACK
  308. /*
  309. * User tracing code (ptrace or signal handlers) might assume that
  310. * the saved RAX contains a 32-bit number when we're invoking a 32-bit
  311. * syscall. Just in case the high bits are nonzero, zero-extend
  312. * the syscall number. (This could almost certainly be deleted
  313. * with no ill effects.)
  314. */
  315. movl %eax, %eax
  316. /* Construct struct pt_regs on stack (iret frame is already on stack) */
  317. pushq %rax /* pt_regs->orig_ax */
  318. pushq %rdi /* pt_regs->di */
  319. pushq %rsi /* pt_regs->si */
  320. pushq %rdx /* pt_regs->dx */
  321. pushq %rcx /* pt_regs->cx */
  322. pushq $-ENOSYS /* pt_regs->ax */
  323. pushq $0 /* pt_regs->r8 = 0 */
  324. xorq %r8, %r8 /* nospec r8 */
  325. pushq $0 /* pt_regs->r9 = 0 */
  326. xorq %r9, %r9 /* nospec r9 */
  327. pushq $0 /* pt_regs->r10 = 0 */
  328. xorq %r10, %r10 /* nospec r10 */
  329. pushq $0 /* pt_regs->r11 = 0 */
  330. xorq %r11, %r11 /* nospec r11 */
  331. pushq %rbx /* pt_regs->rbx */
  332. xorl %ebx, %ebx /* nospec rbx */
  333. pushq %rbp /* pt_regs->rbp */
  334. xorl %ebp, %ebp /* nospec rbp */
  335. pushq %r12 /* pt_regs->r12 */
  336. xorq %r12, %r12 /* nospec r12 */
  337. pushq %r13 /* pt_regs->r13 */
  338. xorq %r13, %r13 /* nospec r13 */
  339. pushq %r14 /* pt_regs->r14 */
  340. xorq %r14, %r14 /* nospec r14 */
  341. pushq %r15 /* pt_regs->r15 */
  342. xorq %r15, %r15 /* nospec r15 */
  343. cld
  344. /*
  345. * User mode is traced as though IRQs are on, and the interrupt
  346. * gate turned them off.
  347. */
  348. TRACE_IRQS_OFF
  349. movq %rsp, %rdi
  350. call do_int80_syscall_32
  351. .Lsyscall_32_done:
  352. /* Go back to user mode. */
  353. TRACE_IRQS_ON
  354. SWITCH_USER_CR3
  355. SWAPGS
  356. jmp restore_regs_and_iret
  357. END(entry_INT80_compat)
  358. ALIGN
  359. GLOBAL(stub32_clone)
  360. /*
  361. * The 32-bit clone ABI is: clone(..., int tls_val, int *child_tidptr).
  362. * The 64-bit clone ABI is: clone(..., int *child_tidptr, int tls_val).
  363. *
  364. * The native 64-bit kernel's sys_clone() implements the latter,
  365. * so we need to swap arguments here before calling it:
  366. */
  367. xchg %r8, %rcx
  368. jmp sys_clone