entry_64_compat.S 11 KB

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