entry_64_compat.S 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. #ifdef CONFIG_PARAVIRT
  22. ENTRY(native_usergs_sysret32)
  23. swapgs
  24. sysretl
  25. ENDPROC(native_usergs_sysret32)
  26. #endif
  27. /*
  28. * 32-bit SYSENTER instruction entry.
  29. *
  30. * SYSENTER loads ss, rsp, cs, and rip from previously programmed MSRs.
  31. * IF and VM in rflags are cleared (IOW: interrupts are off).
  32. * SYSENTER does not save anything on the stack,
  33. * and does not save old rip (!!!) and rflags.
  34. *
  35. * Arguments:
  36. * eax system call number
  37. * ebx arg1
  38. * ecx arg2
  39. * edx arg3
  40. * esi arg4
  41. * edi arg5
  42. * ebp user stack
  43. * 0(%ebp) arg6
  44. *
  45. * This is purely a fast path. For anything complicated we use the int 0x80
  46. * path below. We set up a complete hardware stack frame to share code
  47. * with the int 0x80 path.
  48. */
  49. ENTRY(entry_SYSENTER_compat)
  50. /* Interrupts are off on entry. */
  51. SWAPGS_UNSAFE_STACK
  52. SWITCH_KERNEL_CR3_NO_STACK
  53. movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
  54. /*
  55. * User tracing code (ptrace or signal handlers) might assume that
  56. * the saved RAX contains a 32-bit number when we're invoking a 32-bit
  57. * syscall. Just in case the high bits are nonzero, zero-extend
  58. * the syscall number. (This could almost certainly be deleted
  59. * with no ill effects.)
  60. */
  61. movl %eax, %eax
  62. /* Construct struct pt_regs on stack */
  63. pushq $__USER32_DS /* pt_regs->ss */
  64. pushq %rbp /* pt_regs->sp (stashed in bp) */
  65. /*
  66. * Push flags. This is nasty. First, interrupts are currently
  67. * off, but we need pt_regs->flags to have IF set. Second, even
  68. * if TF was set when SYSENTER started, it's clear by now. We fix
  69. * that later using TIF_SINGLESTEP.
  70. */
  71. pushfq /* pt_regs->flags (except IF = 0) */
  72. orl $X86_EFLAGS_IF, (%rsp) /* Fix saved flags */
  73. ASM_CLAC /* Clear AC after saving FLAGS */
  74. pushq $__USER32_CS /* pt_regs->cs */
  75. pushq $0 /* pt_regs->ip = 0 (placeholder) */
  76. pushq %rax /* pt_regs->orig_ax */
  77. pushq %rdi /* pt_regs->di */
  78. pushq %rsi /* pt_regs->si */
  79. pushq %rdx /* pt_regs->dx */
  80. pushq %rcx /* pt_regs->cx */
  81. pushq $-ENOSYS /* pt_regs->ax */
  82. pushq $0 /* pt_regs->r8 = 0 */
  83. xorq %r8, %r8 /* nospec r8 */
  84. pushq $0 /* pt_regs->r9 = 0 */
  85. xorq %r9, %r9 /* nospec r9 */
  86. pushq $0 /* pt_regs->r10 = 0 */
  87. xorq %r10, %r10 /* nospec r10 */
  88. pushq $0 /* pt_regs->r11 = 0 */
  89. xorq %r11, %r11 /* nospec r11 */
  90. pushq %rbx /* pt_regs->rbx */
  91. xorl %ebx, %ebx /* nospec rbx */
  92. pushq %rbp /* pt_regs->rbp (will be overwritten) */
  93. xorl %ebp, %ebp /* nospec rbp */
  94. pushq $0 /* pt_regs->r12 = 0 */
  95. xorq %r12, %r12 /* nospec r12 */
  96. pushq $0 /* pt_regs->r13 = 0 */
  97. xorq %r13, %r13 /* nospec r13 */
  98. pushq $0 /* pt_regs->r14 = 0 */
  99. xorq %r14, %r14 /* nospec r14 */
  100. pushq $0 /* pt_regs->r15 = 0 */
  101. xorq %r15, %r15 /* nospec r15 */
  102. cld
  103. /*
  104. * Sysenter doesn't filter flags, so we need to clear NT
  105. * ourselves. To save a few cycles, we can check whether
  106. * NT was set instead of doing an unconditional popfq.
  107. * This needs to happen before enabling interrupts so that
  108. * we don't get preempted with NT set.
  109. *
  110. * NB.: sysenter_fix_flags is a label with the code under it moved
  111. * out-of-line as an optimization: NT is unlikely to be set in the
  112. * majority of the cases and instead of polluting the I$ unnecessarily,
  113. * we're keeping that code behind a branch which will predict as
  114. * not-taken and therefore its instructions won't be fetched.
  115. */
  116. testl $X86_EFLAGS_NT, EFLAGS(%rsp)
  117. jnz sysenter_fix_flags
  118. sysenter_flags_fixed:
  119. /*
  120. * User mode is traced as though IRQs are on, and SYSENTER
  121. * turned them off.
  122. */
  123. TRACE_IRQS_OFF
  124. movq %rsp, %rdi
  125. call do_fast_syscall_32
  126. /* XEN PV guests always use IRET path */
  127. ALTERNATIVE "testl %eax, %eax; jz .Lsyscall_32_done", \
  128. "jmp .Lsyscall_32_done", X86_FEATURE_XENPV
  129. jmp sysret32_from_system_call
  130. sysenter_fix_flags:
  131. pushq $X86_EFLAGS_FIXED
  132. popfq
  133. jmp sysenter_flags_fixed
  134. ENDPROC(entry_SYSENTER_compat)
  135. /*
  136. * 32-bit SYSCALL instruction entry.
  137. *
  138. * 32-bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11,
  139. * then loads new ss, cs, and rip from previously programmed MSRs.
  140. * rflags gets masked by a value from another MSR (so CLD and CLAC
  141. * are not needed). SYSCALL does not save anything on the stack
  142. * and does not change rsp.
  143. *
  144. * Note: rflags saving+masking-with-MSR happens only in Long mode
  145. * (in legacy 32-bit mode, IF, RF and VM bits are cleared and that's it).
  146. * Don't get confused: rflags saving+masking depends on Long Mode Active bit
  147. * (EFER.LMA=1), NOT on bitness of userspace where SYSCALL executes
  148. * or target CS descriptor's L bit (SYSCALL does not read segment descriptors).
  149. *
  150. * Arguments:
  151. * eax system call number
  152. * ecx return address
  153. * ebx arg1
  154. * ebp arg2 (note: not saved in the stack frame, should not be touched)
  155. * edx arg3
  156. * esi arg4
  157. * edi arg5
  158. * esp user stack
  159. * 0(%esp) arg6
  160. */
  161. ENTRY(entry_SYSCALL_compat)
  162. /* Interrupts are off on entry. */
  163. SWAPGS_UNSAFE_STACK
  164. SWITCH_KERNEL_CR3_NO_STACK
  165. /* Stash user ESP and switch to the kernel stack. */
  166. movl %esp, %r8d
  167. movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
  168. /* Zero-extending 32-bit regs, do not remove */
  169. movl %eax, %eax
  170. /* Construct struct pt_regs on stack */
  171. pushq $__USER32_DS /* pt_regs->ss */
  172. pushq %r8 /* pt_regs->sp */
  173. pushq %r11 /* pt_regs->flags */
  174. pushq $__USER32_CS /* pt_regs->cs */
  175. pushq %rcx /* pt_regs->ip */
  176. pushq %rax /* pt_regs->orig_ax */
  177. pushq %rdi /* pt_regs->di */
  178. pushq %rsi /* pt_regs->si */
  179. pushq %rdx /* pt_regs->dx */
  180. pushq %rbp /* pt_regs->cx (stashed in bp) */
  181. pushq $-ENOSYS /* pt_regs->ax */
  182. pushq $0 /* pt_regs->r8 = 0 */
  183. xorq %r8, %r8 /* nospec r8 */
  184. pushq $0 /* pt_regs->r9 = 0 */
  185. xorq %r9, %r9 /* nospec r9 */
  186. pushq $0 /* pt_regs->r10 = 0 */
  187. xorq %r10, %r10 /* nospec r10 */
  188. pushq $0 /* pt_regs->r11 = 0 */
  189. xorq %r11, %r11 /* nospec r11 */
  190. pushq %rbx /* pt_regs->rbx */
  191. xorl %ebx, %ebx /* nospec rbx */
  192. pushq %rbp /* pt_regs->rbp (will be overwritten) */
  193. xorl %ebp, %ebp /* nospec rbp */
  194. pushq $0 /* pt_regs->r12 = 0 */
  195. xorq %r12, %r12 /* nospec r12 */
  196. pushq $0 /* pt_regs->r13 = 0 */
  197. xorq %r13, %r13 /* nospec r13 */
  198. pushq $0 /* pt_regs->r14 = 0 */
  199. xorq %r14, %r14 /* nospec r14 */
  200. pushq $0 /* pt_regs->r15 = 0 */
  201. xorq %r15, %r15 /* nospec r15 */
  202. /*
  203. * User mode is traced as though IRQs are on, and SYSENTER
  204. * turned them off.
  205. */
  206. TRACE_IRQS_OFF
  207. movq %rsp, %rdi
  208. call do_fast_syscall_32
  209. /* XEN PV guests always use IRET path */
  210. ALTERNATIVE "testl %eax, %eax; jz .Lsyscall_32_done", \
  211. "jmp .Lsyscall_32_done", X86_FEATURE_XENPV
  212. /* Opportunistic SYSRET */
  213. sysret32_from_system_call:
  214. TRACE_IRQS_ON /* User mode traces as IRQs on. */
  215. SWITCH_USER_CR3
  216. movq RBX(%rsp), %rbx /* pt_regs->rbx */
  217. movq RBP(%rsp), %rbp /* pt_regs->rbp */
  218. movq EFLAGS(%rsp), %r11 /* pt_regs->flags (in r11) */
  219. movq RIP(%rsp), %rcx /* pt_regs->ip (in rcx) */
  220. addq $RAX, %rsp /* Skip r8-r15 */
  221. popq %rax /* pt_regs->rax */
  222. popq %rdx /* Skip pt_regs->cx */
  223. popq %rdx /* pt_regs->dx */
  224. popq %rsi /* pt_regs->si */
  225. popq %rdi /* pt_regs->di */
  226. /*
  227. * USERGS_SYSRET32 does:
  228. * GSBASE = user's GS base
  229. * EIP = ECX
  230. * RFLAGS = R11
  231. * CS = __USER32_CS
  232. * SS = __USER_DS
  233. *
  234. * ECX will not match pt_regs->cx, but we're returning to a vDSO
  235. * trampoline that will fix up RCX, so this is okay.
  236. *
  237. * R12-R15 are callee-saved, so they contain whatever was in them
  238. * when the system call started, which is already known to user
  239. * code. We zero R8-R10 to avoid info leaks.
  240. */
  241. xorq %r8, %r8
  242. xorq %r9, %r9
  243. xorq %r10, %r10
  244. movq RSP-ORIG_RAX(%rsp), %rsp
  245. USERGS_SYSRET32
  246. END(entry_SYSCALL_compat)
  247. /*
  248. * Emulated IA32 system calls via int 0x80.
  249. *
  250. * Arguments:
  251. * eax system call number
  252. * ebx arg1
  253. * ecx arg2
  254. * edx arg3
  255. * esi arg4
  256. * edi arg5
  257. * ebp arg6 (note: not saved in the stack frame, should not be touched)
  258. *
  259. * Notes:
  260. * Uses the same stack frame as the x86-64 version.
  261. * All registers except eax must be saved (but ptrace may violate that).
  262. * Arguments are zero extended. For system calls that want sign extension and
  263. * take long arguments a wrapper is needed. Most calls can just be called
  264. * directly.
  265. * Assumes it is only called from user space and entered with interrupts off.
  266. */
  267. ENTRY(entry_INT80_compat)
  268. /*
  269. * Interrupts are off on entry.
  270. */
  271. PARAVIRT_ADJUST_EXCEPTION_FRAME
  272. ASM_CLAC /* Do this early to minimize exposure */
  273. SWAPGS
  274. SWITCH_KERNEL_CR3_NO_STACK
  275. /*
  276. * User tracing code (ptrace or signal handlers) might assume that
  277. * the saved RAX contains a 32-bit number when we're invoking a 32-bit
  278. * syscall. Just in case the high bits are nonzero, zero-extend
  279. * the syscall number. (This could almost certainly be deleted
  280. * with no ill effects.)
  281. */
  282. movl %eax, %eax
  283. /* Construct struct pt_regs on stack (iret frame is already on stack) */
  284. pushq %rax /* pt_regs->orig_ax */
  285. pushq %rdi /* pt_regs->di */
  286. pushq %rsi /* pt_regs->si */
  287. pushq %rdx /* pt_regs->dx */
  288. pushq %rcx /* pt_regs->cx */
  289. pushq $-ENOSYS /* pt_regs->ax */
  290. pushq $0 /* pt_regs->r8 = 0 */
  291. xorq %r8, %r8 /* nospec r8 */
  292. pushq $0 /* pt_regs->r9 = 0 */
  293. xorq %r9, %r9 /* nospec r9 */
  294. pushq $0 /* pt_regs->r10 = 0 */
  295. xorq %r10, %r10 /* nospec r10 */
  296. pushq $0 /* pt_regs->r11 = 0 */
  297. xorq %r11, %r11 /* nospec r11 */
  298. pushq %rbx /* pt_regs->rbx */
  299. xorl %ebx, %ebx /* nospec rbx */
  300. pushq %rbp /* pt_regs->rbp */
  301. xorl %ebp, %ebp /* nospec rbp */
  302. pushq %r12 /* pt_regs->r12 */
  303. xorq %r12, %r12 /* nospec r12 */
  304. pushq %r13 /* pt_regs->r13 */
  305. xorq %r13, %r13 /* nospec r13 */
  306. pushq %r14 /* pt_regs->r14 */
  307. xorq %r14, %r14 /* nospec r14 */
  308. pushq %r15 /* pt_regs->r15 */
  309. xorq %r15, %r15 /* nospec r15 */
  310. cld
  311. /*
  312. * User mode is traced as though IRQs are on, and the interrupt
  313. * gate turned them off.
  314. */
  315. TRACE_IRQS_OFF
  316. movq %rsp, %rdi
  317. call do_syscall_32_irqs_off
  318. .Lsyscall_32_done:
  319. /* Go back to user mode. */
  320. TRACE_IRQS_ON
  321. SWITCH_USER_CR3
  322. SWAPGS
  323. jmp restore_regs_and_iret
  324. END(entry_INT80_compat)
  325. ALIGN
  326. GLOBAL(stub32_clone)
  327. /*
  328. * The 32-bit clone ABI is: clone(..., int tls_val, int *child_tidptr).
  329. * The 64-bit clone ABI is: clone(..., int *child_tidptr, int tls_val).
  330. *
  331. * The native 64-bit kernel's sys_clone() implements the latter,
  332. * so we need to swap arguments here before calling it:
  333. */
  334. xchg %r8, %rcx
  335. jmp sys_clone