entry_64_compat.S 10 KB

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