calling.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #include <linux/jump_label.h>
  3. #include <asm/unwind_hints.h>
  4. #include <asm/cpufeatures.h>
  5. #include <asm/page_types.h>
  6. #include <asm/percpu.h>
  7. #include <asm/asm-offsets.h>
  8. #include <asm/processor-flags.h>
  9. /*
  10. x86 function call convention, 64-bit:
  11. -------------------------------------
  12. arguments | callee-saved | extra caller-saved | return
  13. [callee-clobbered] | | [callee-clobbered] |
  14. ---------------------------------------------------------------------------
  15. rdi rsi rdx rcx r8-9 | rbx rbp [*] r12-15 | r10-11 | rax, rdx [**]
  16. ( rsp is obviously invariant across normal function calls. (gcc can 'merge'
  17. functions when it sees tail-call optimization possibilities) rflags is
  18. clobbered. Leftover arguments are passed over the stack frame.)
  19. [*] In the frame-pointers case rbp is fixed to the stack frame.
  20. [**] for struct return values wider than 64 bits the return convention is a
  21. bit more complex: up to 128 bits width we return small structures
  22. straight in rax, rdx. For structures larger than that (3 words or
  23. larger) the caller puts a pointer to an on-stack return struct
  24. [allocated in the caller's stack frame] into the first argument - i.e.
  25. into rdi. All other arguments shift up by one in this case.
  26. Fortunately this case is rare in the kernel.
  27. For 32-bit we have the following conventions - kernel is built with
  28. -mregparm=3 and -freg-struct-return:
  29. x86 function calling convention, 32-bit:
  30. ----------------------------------------
  31. arguments | callee-saved | extra caller-saved | return
  32. [callee-clobbered] | | [callee-clobbered] |
  33. -------------------------------------------------------------------------
  34. eax edx ecx | ebx edi esi ebp [*] | <none> | eax, edx [**]
  35. ( here too esp is obviously invariant across normal function calls. eflags
  36. is clobbered. Leftover arguments are passed over the stack frame. )
  37. [*] In the frame-pointers case ebp is fixed to the stack frame.
  38. [**] We build with -freg-struct-return, which on 32-bit means similar
  39. semantics as on 64-bit: edx can be used for a second return value
  40. (i.e. covering integer and structure sizes up to 64 bits) - after that
  41. it gets more complex and more expensive: 3-word or larger struct returns
  42. get done in the caller's frame and the pointer to the return struct goes
  43. into regparm0, i.e. eax - the other arguments shift up and the
  44. function's register parameters degenerate to regparm=2 in essence.
  45. */
  46. #ifdef CONFIG_X86_64
  47. /*
  48. * 64-bit system call stack frame layout defines and helpers,
  49. * for assembly code:
  50. */
  51. /* The layout forms the "struct pt_regs" on the stack: */
  52. /*
  53. * C ABI says these regs are callee-preserved. They aren't saved on kernel entry
  54. * unless syscall needs a complete, fully filled "struct pt_regs".
  55. */
  56. #define R15 0*8
  57. #define R14 1*8
  58. #define R13 2*8
  59. #define R12 3*8
  60. #define RBP 4*8
  61. #define RBX 5*8
  62. /* These regs are callee-clobbered. Always saved on kernel entry. */
  63. #define R11 6*8
  64. #define R10 7*8
  65. #define R9 8*8
  66. #define R8 9*8
  67. #define RAX 10*8
  68. #define RCX 11*8
  69. #define RDX 12*8
  70. #define RSI 13*8
  71. #define RDI 14*8
  72. /*
  73. * On syscall entry, this is syscall#. On CPU exception, this is error code.
  74. * On hw interrupt, it's IRQ number:
  75. */
  76. #define ORIG_RAX 15*8
  77. /* Return frame for iretq */
  78. #define RIP 16*8
  79. #define CS 17*8
  80. #define EFLAGS 18*8
  81. #define RSP 19*8
  82. #define SS 20*8
  83. #define SIZEOF_PTREGS 21*8
  84. .macro PUSH_AND_CLEAR_REGS rdx=%rdx rax=%rax save_ret=0
  85. /*
  86. * Push registers and sanitize registers of values that a
  87. * speculation attack might otherwise want to exploit. The
  88. * lower registers are likely clobbered well before they
  89. * could be put to use in a speculative execution gadget.
  90. * Interleave XOR with PUSH for better uop scheduling:
  91. */
  92. .if \save_ret
  93. pushq %rsi /* pt_regs->si */
  94. movq 8(%rsp), %rsi /* temporarily store the return address in %rsi */
  95. movq %rdi, 8(%rsp) /* pt_regs->di (overwriting original return address) */
  96. .else
  97. pushq %rdi /* pt_regs->di */
  98. pushq %rsi /* pt_regs->si */
  99. .endif
  100. pushq \rdx /* pt_regs->dx */
  101. pushq %rcx /* pt_regs->cx */
  102. pushq \rax /* pt_regs->ax */
  103. pushq %r8 /* pt_regs->r8 */
  104. xorl %r8d, %r8d /* nospec r8 */
  105. pushq %r9 /* pt_regs->r9 */
  106. xorl %r9d, %r9d /* nospec r9 */
  107. pushq %r10 /* pt_regs->r10 */
  108. xorl %r10d, %r10d /* nospec r10 */
  109. pushq %r11 /* pt_regs->r11 */
  110. xorl %r11d, %r11d /* nospec r11*/
  111. pushq %rbx /* pt_regs->rbx */
  112. xorl %ebx, %ebx /* nospec rbx*/
  113. pushq %rbp /* pt_regs->rbp */
  114. xorl %ebp, %ebp /* nospec rbp*/
  115. pushq %r12 /* pt_regs->r12 */
  116. xorl %r12d, %r12d /* nospec r12*/
  117. pushq %r13 /* pt_regs->r13 */
  118. xorl %r13d, %r13d /* nospec r13*/
  119. pushq %r14 /* pt_regs->r14 */
  120. xorl %r14d, %r14d /* nospec r14*/
  121. pushq %r15 /* pt_regs->r15 */
  122. xorl %r15d, %r15d /* nospec r15*/
  123. UNWIND_HINT_REGS
  124. .if \save_ret
  125. pushq %rsi /* return address on top of stack */
  126. .endif
  127. .endm
  128. .macro POP_REGS pop_rdi=1 skip_r11rcx=0
  129. popq %r15
  130. popq %r14
  131. popq %r13
  132. popq %r12
  133. popq %rbp
  134. popq %rbx
  135. .if \skip_r11rcx
  136. popq %rsi
  137. .else
  138. popq %r11
  139. .endif
  140. popq %r10
  141. popq %r9
  142. popq %r8
  143. popq %rax
  144. .if \skip_r11rcx
  145. popq %rsi
  146. .else
  147. popq %rcx
  148. .endif
  149. popq %rdx
  150. popq %rsi
  151. .if \pop_rdi
  152. popq %rdi
  153. .endif
  154. .endm
  155. /*
  156. * This is a sneaky trick to help the unwinder find pt_regs on the stack. The
  157. * frame pointer is replaced with an encoded pointer to pt_regs. The encoding
  158. * is just setting the LSB, which makes it an invalid stack address and is also
  159. * a signal to the unwinder that it's a pt_regs pointer in disguise.
  160. *
  161. * NOTE: This macro must be used *after* PUSH_AND_CLEAR_REGS because it corrupts
  162. * the original rbp.
  163. */
  164. .macro ENCODE_FRAME_POINTER ptregs_offset=0
  165. #ifdef CONFIG_FRAME_POINTER
  166. leaq 1+\ptregs_offset(%rsp), %rbp
  167. #endif
  168. .endm
  169. #ifdef CONFIG_PAGE_TABLE_ISOLATION
  170. /*
  171. * PAGE_TABLE_ISOLATION PGDs are 8k. Flip bit 12 to switch between the two
  172. * halves:
  173. */
  174. #define PTI_USER_PGTABLE_BIT PAGE_SHIFT
  175. #define PTI_USER_PGTABLE_MASK (1 << PTI_USER_PGTABLE_BIT)
  176. #define PTI_USER_PCID_BIT X86_CR3_PTI_PCID_USER_BIT
  177. #define PTI_USER_PCID_MASK (1 << PTI_USER_PCID_BIT)
  178. #define PTI_USER_PGTABLE_AND_PCID_MASK (PTI_USER_PCID_MASK | PTI_USER_PGTABLE_MASK)
  179. .macro SET_NOFLUSH_BIT reg:req
  180. bts $X86_CR3_PCID_NOFLUSH_BIT, \reg
  181. .endm
  182. .macro ADJUST_KERNEL_CR3 reg:req
  183. ALTERNATIVE "", "SET_NOFLUSH_BIT \reg", X86_FEATURE_PCID
  184. /* Clear PCID and "PAGE_TABLE_ISOLATION bit", point CR3 at kernel pagetables: */
  185. andq $(~PTI_USER_PGTABLE_AND_PCID_MASK), \reg
  186. .endm
  187. .macro SWITCH_TO_KERNEL_CR3 scratch_reg:req
  188. ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI
  189. mov %cr3, \scratch_reg
  190. ADJUST_KERNEL_CR3 \scratch_reg
  191. mov \scratch_reg, %cr3
  192. .Lend_\@:
  193. .endm
  194. #define THIS_CPU_user_pcid_flush_mask \
  195. PER_CPU_VAR(cpu_tlbstate) + TLB_STATE_user_pcid_flush_mask
  196. .macro SWITCH_TO_USER_CR3_NOSTACK scratch_reg:req scratch_reg2:req
  197. ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI
  198. mov %cr3, \scratch_reg
  199. ALTERNATIVE "jmp .Lwrcr3_\@", "", X86_FEATURE_PCID
  200. /*
  201. * Test if the ASID needs a flush.
  202. */
  203. movq \scratch_reg, \scratch_reg2
  204. andq $(0x7FF), \scratch_reg /* mask ASID */
  205. bt \scratch_reg, THIS_CPU_user_pcid_flush_mask
  206. jnc .Lnoflush_\@
  207. /* Flush needed, clear the bit */
  208. btr \scratch_reg, THIS_CPU_user_pcid_flush_mask
  209. movq \scratch_reg2, \scratch_reg
  210. jmp .Lwrcr3_pcid_\@
  211. .Lnoflush_\@:
  212. movq \scratch_reg2, \scratch_reg
  213. SET_NOFLUSH_BIT \scratch_reg
  214. .Lwrcr3_pcid_\@:
  215. /* Flip the ASID to the user version */
  216. orq $(PTI_USER_PCID_MASK), \scratch_reg
  217. .Lwrcr3_\@:
  218. /* Flip the PGD to the user version */
  219. orq $(PTI_USER_PGTABLE_MASK), \scratch_reg
  220. mov \scratch_reg, %cr3
  221. .Lend_\@:
  222. .endm
  223. .macro SWITCH_TO_USER_CR3_STACK scratch_reg:req
  224. pushq %rax
  225. SWITCH_TO_USER_CR3_NOSTACK scratch_reg=\scratch_reg scratch_reg2=%rax
  226. popq %rax
  227. .endm
  228. .macro SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg:req save_reg:req
  229. ALTERNATIVE "jmp .Ldone_\@", "", X86_FEATURE_PTI
  230. movq %cr3, \scratch_reg
  231. movq \scratch_reg, \save_reg
  232. /*
  233. * Test the user pagetable bit. If set, then the user page tables
  234. * are active. If clear CR3 already has the kernel page table
  235. * active.
  236. */
  237. bt $PTI_USER_PGTABLE_BIT, \scratch_reg
  238. jnc .Ldone_\@
  239. ADJUST_KERNEL_CR3 \scratch_reg
  240. movq \scratch_reg, %cr3
  241. .Ldone_\@:
  242. .endm
  243. .macro RESTORE_CR3 scratch_reg:req save_reg:req
  244. ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI
  245. ALTERNATIVE "jmp .Lwrcr3_\@", "", X86_FEATURE_PCID
  246. /*
  247. * KERNEL pages can always resume with NOFLUSH as we do
  248. * explicit flushes.
  249. */
  250. bt $PTI_USER_PGTABLE_BIT, \save_reg
  251. jnc .Lnoflush_\@
  252. /*
  253. * Check if there's a pending flush for the user ASID we're
  254. * about to set.
  255. */
  256. movq \save_reg, \scratch_reg
  257. andq $(0x7FF), \scratch_reg
  258. bt \scratch_reg, THIS_CPU_user_pcid_flush_mask
  259. jnc .Lnoflush_\@
  260. btr \scratch_reg, THIS_CPU_user_pcid_flush_mask
  261. jmp .Lwrcr3_\@
  262. .Lnoflush_\@:
  263. SET_NOFLUSH_BIT \save_reg
  264. .Lwrcr3_\@:
  265. /*
  266. * The CR3 write could be avoided when not changing its value,
  267. * but would require a CR3 read *and* a scratch register.
  268. */
  269. movq \save_reg, %cr3
  270. .Lend_\@:
  271. .endm
  272. #else /* CONFIG_PAGE_TABLE_ISOLATION=n: */
  273. .macro SWITCH_TO_KERNEL_CR3 scratch_reg:req
  274. .endm
  275. .macro SWITCH_TO_USER_CR3_NOSTACK scratch_reg:req scratch_reg2:req
  276. .endm
  277. .macro SWITCH_TO_USER_CR3_STACK scratch_reg:req
  278. .endm
  279. .macro SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg:req save_reg:req
  280. .endm
  281. .macro RESTORE_CR3 scratch_reg:req save_reg:req
  282. .endm
  283. #endif
  284. #endif /* CONFIG_X86_64 */
  285. /*
  286. * This does 'call enter_from_user_mode' unless we can avoid it based on
  287. * kernel config or using the static jump infrastructure.
  288. */
  289. .macro CALL_enter_from_user_mode
  290. #ifdef CONFIG_CONTEXT_TRACKING
  291. #ifdef HAVE_JUMP_LABEL
  292. STATIC_JUMP_IF_FALSE .Lafter_call_\@, context_tracking_enabled, def=0
  293. #endif
  294. call enter_from_user_mode
  295. .Lafter_call_\@:
  296. #endif
  297. .endm