calling.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. .if \save_ret
  86. pushq %rsi /* pt_regs->si */
  87. movq 8(%rsp), %rsi /* temporarily store the return address in %rsi */
  88. movq %rdi, 8(%rsp) /* pt_regs->di (overwriting original return address) */
  89. .else
  90. pushq %rdi /* pt_regs->di */
  91. pushq %rsi /* pt_regs->si */
  92. .endif
  93. pushq \rdx /* pt_regs->dx */
  94. pushq %rcx /* pt_regs->cx */
  95. pushq \rax /* pt_regs->ax */
  96. pushq %r8 /* pt_regs->r8 */
  97. pushq %r9 /* pt_regs->r9 */
  98. pushq %r10 /* pt_regs->r10 */
  99. pushq %r11 /* pt_regs->r11 */
  100. pushq %rbx /* pt_regs->rbx */
  101. pushq %rbp /* pt_regs->rbp */
  102. pushq %r12 /* pt_regs->r12 */
  103. pushq %r13 /* pt_regs->r13 */
  104. pushq %r14 /* pt_regs->r14 */
  105. pushq %r15 /* pt_regs->r15 */
  106. UNWIND_HINT_REGS
  107. .if \save_ret
  108. pushq %rsi /* return address on top of stack */
  109. .endif
  110. /*
  111. * Sanitize registers of values that a speculation attack might
  112. * otherwise want to exploit. The lower registers are likely clobbered
  113. * well before they could be put to use in a speculative execution
  114. * gadget.
  115. */
  116. xorl %edx, %edx /* nospec dx */
  117. xorl %ecx, %ecx /* nospec cx */
  118. xorl %r8d, %r8d /* nospec r8 */
  119. xorl %r9d, %r9d /* nospec r9 */
  120. xorl %r10d, %r10d /* nospec r10 */
  121. xorl %r11d, %r11d /* nospec r11 */
  122. xorl %ebx, %ebx /* nospec rbx */
  123. xorl %ebp, %ebp /* nospec rbp */
  124. xorl %r12d, %r12d /* nospec r12 */
  125. xorl %r13d, %r13d /* nospec r13 */
  126. xorl %r14d, %r14d /* nospec r14 */
  127. xorl %r15d, %r15d /* nospec r15 */
  128. .endm
  129. .macro POP_REGS pop_rdi=1 skip_r11rcx=0
  130. popq %r15
  131. popq %r14
  132. popq %r13
  133. popq %r12
  134. popq %rbp
  135. popq %rbx
  136. .if \skip_r11rcx
  137. popq %rsi
  138. .else
  139. popq %r11
  140. .endif
  141. popq %r10
  142. popq %r9
  143. popq %r8
  144. popq %rax
  145. .if \skip_r11rcx
  146. popq %rsi
  147. .else
  148. popq %rcx
  149. .endif
  150. popq %rdx
  151. popq %rsi
  152. .if \pop_rdi
  153. popq %rdi
  154. .endif
  155. .endm
  156. #ifdef CONFIG_PAGE_TABLE_ISOLATION
  157. /*
  158. * PAGE_TABLE_ISOLATION PGDs are 8k. Flip bit 12 to switch between the two
  159. * halves:
  160. */
  161. #define PTI_USER_PGTABLE_BIT PAGE_SHIFT
  162. #define PTI_USER_PGTABLE_MASK (1 << PTI_USER_PGTABLE_BIT)
  163. #define PTI_USER_PCID_BIT X86_CR3_PTI_PCID_USER_BIT
  164. #define PTI_USER_PCID_MASK (1 << PTI_USER_PCID_BIT)
  165. #define PTI_USER_PGTABLE_AND_PCID_MASK (PTI_USER_PCID_MASK | PTI_USER_PGTABLE_MASK)
  166. .macro SET_NOFLUSH_BIT reg:req
  167. bts $X86_CR3_PCID_NOFLUSH_BIT, \reg
  168. .endm
  169. .macro ADJUST_KERNEL_CR3 reg:req
  170. ALTERNATIVE "", "SET_NOFLUSH_BIT \reg", X86_FEATURE_PCID
  171. /* Clear PCID and "PAGE_TABLE_ISOLATION bit", point CR3 at kernel pagetables: */
  172. andq $(~PTI_USER_PGTABLE_AND_PCID_MASK), \reg
  173. .endm
  174. .macro SWITCH_TO_KERNEL_CR3 scratch_reg:req
  175. ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI
  176. mov %cr3, \scratch_reg
  177. ADJUST_KERNEL_CR3 \scratch_reg
  178. mov \scratch_reg, %cr3
  179. .Lend_\@:
  180. .endm
  181. #define THIS_CPU_user_pcid_flush_mask \
  182. PER_CPU_VAR(cpu_tlbstate) + TLB_STATE_user_pcid_flush_mask
  183. .macro SWITCH_TO_USER_CR3_NOSTACK scratch_reg:req scratch_reg2:req
  184. ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI
  185. mov %cr3, \scratch_reg
  186. ALTERNATIVE "jmp .Lwrcr3_\@", "", X86_FEATURE_PCID
  187. /*
  188. * Test if the ASID needs a flush.
  189. */
  190. movq \scratch_reg, \scratch_reg2
  191. andq $(0x7FF), \scratch_reg /* mask ASID */
  192. bt \scratch_reg, THIS_CPU_user_pcid_flush_mask
  193. jnc .Lnoflush_\@
  194. /* Flush needed, clear the bit */
  195. btr \scratch_reg, THIS_CPU_user_pcid_flush_mask
  196. movq \scratch_reg2, \scratch_reg
  197. jmp .Lwrcr3_pcid_\@
  198. .Lnoflush_\@:
  199. movq \scratch_reg2, \scratch_reg
  200. SET_NOFLUSH_BIT \scratch_reg
  201. .Lwrcr3_pcid_\@:
  202. /* Flip the ASID to the user version */
  203. orq $(PTI_USER_PCID_MASK), \scratch_reg
  204. .Lwrcr3_\@:
  205. /* Flip the PGD to the user version */
  206. orq $(PTI_USER_PGTABLE_MASK), \scratch_reg
  207. mov \scratch_reg, %cr3
  208. .Lend_\@:
  209. .endm
  210. .macro SWITCH_TO_USER_CR3_STACK scratch_reg:req
  211. pushq %rax
  212. SWITCH_TO_USER_CR3_NOSTACK scratch_reg=\scratch_reg scratch_reg2=%rax
  213. popq %rax
  214. .endm
  215. .macro SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg:req save_reg:req
  216. ALTERNATIVE "jmp .Ldone_\@", "", X86_FEATURE_PTI
  217. movq %cr3, \scratch_reg
  218. movq \scratch_reg, \save_reg
  219. /*
  220. * Test the user pagetable bit. If set, then the user page tables
  221. * are active. If clear CR3 already has the kernel page table
  222. * active.
  223. */
  224. bt $PTI_USER_PGTABLE_BIT, \scratch_reg
  225. jnc .Ldone_\@
  226. ADJUST_KERNEL_CR3 \scratch_reg
  227. movq \scratch_reg, %cr3
  228. .Ldone_\@:
  229. .endm
  230. .macro RESTORE_CR3 scratch_reg:req save_reg:req
  231. ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI
  232. ALTERNATIVE "jmp .Lwrcr3_\@", "", X86_FEATURE_PCID
  233. /*
  234. * KERNEL pages can always resume with NOFLUSH as we do
  235. * explicit flushes.
  236. */
  237. bt $PTI_USER_PGTABLE_BIT, \save_reg
  238. jnc .Lnoflush_\@
  239. /*
  240. * Check if there's a pending flush for the user ASID we're
  241. * about to set.
  242. */
  243. movq \save_reg, \scratch_reg
  244. andq $(0x7FF), \scratch_reg
  245. bt \scratch_reg, THIS_CPU_user_pcid_flush_mask
  246. jnc .Lnoflush_\@
  247. btr \scratch_reg, THIS_CPU_user_pcid_flush_mask
  248. jmp .Lwrcr3_\@
  249. .Lnoflush_\@:
  250. SET_NOFLUSH_BIT \save_reg
  251. .Lwrcr3_\@:
  252. /*
  253. * The CR3 write could be avoided when not changing its value,
  254. * but would require a CR3 read *and* a scratch register.
  255. */
  256. movq \save_reg, %cr3
  257. .Lend_\@:
  258. .endm
  259. #else /* CONFIG_PAGE_TABLE_ISOLATION=n: */
  260. .macro SWITCH_TO_KERNEL_CR3 scratch_reg:req
  261. .endm
  262. .macro SWITCH_TO_USER_CR3_NOSTACK scratch_reg:req scratch_reg2:req
  263. .endm
  264. .macro SWITCH_TO_USER_CR3_STACK scratch_reg:req
  265. .endm
  266. .macro SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg:req save_reg:req
  267. .endm
  268. .macro RESTORE_CR3 scratch_reg:req save_reg:req
  269. .endm
  270. #endif
  271. /*
  272. * Mitigate Spectre v1 for conditional swapgs code paths.
  273. *
  274. * FENCE_SWAPGS_USER_ENTRY is used in the user entry swapgs code path, to
  275. * prevent a speculative swapgs when coming from kernel space.
  276. *
  277. * FENCE_SWAPGS_KERNEL_ENTRY is used in the kernel entry non-swapgs code path,
  278. * to prevent the swapgs from getting speculatively skipped when coming from
  279. * user space.
  280. */
  281. .macro FENCE_SWAPGS_USER_ENTRY
  282. ALTERNATIVE "", "lfence", X86_FEATURE_FENCE_SWAPGS_USER
  283. .endm
  284. .macro FENCE_SWAPGS_KERNEL_ENTRY
  285. ALTERNATIVE "", "lfence", X86_FEATURE_FENCE_SWAPGS_KERNEL
  286. .endm
  287. #endif /* CONFIG_X86_64 */
  288. /*
  289. * This does 'call enter_from_user_mode' unless we can avoid it based on
  290. * kernel config or using the static jump infrastructure.
  291. */
  292. .macro CALL_enter_from_user_mode
  293. #ifdef CONFIG_CONTEXT_TRACKING
  294. #ifdef CONFIG_JUMP_LABEL
  295. STATIC_JUMP_IF_FALSE .Lafter_call_\@, context_tracking_enabled, def=0
  296. #endif
  297. call enter_from_user_mode
  298. .Lafter_call_\@:
  299. #endif
  300. .endm