calling.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. #include <asm/ptrace-abi.h>
  10. #include <asm/msr.h>
  11. #include <asm/nospec-branch.h>
  12. /*
  13. x86 function call convention, 64-bit:
  14. -------------------------------------
  15. arguments | callee-saved | extra caller-saved | return
  16. [callee-clobbered] | | [callee-clobbered] |
  17. ---------------------------------------------------------------------------
  18. rdi rsi rdx rcx r8-9 | rbx rbp [*] r12-15 | r10-11 | rax, rdx [**]
  19. ( rsp is obviously invariant across normal function calls. (gcc can 'merge'
  20. functions when it sees tail-call optimization possibilities) rflags is
  21. clobbered. Leftover arguments are passed over the stack frame.)
  22. [*] In the frame-pointers case rbp is fixed to the stack frame.
  23. [**] for struct return values wider than 64 bits the return convention is a
  24. bit more complex: up to 128 bits width we return small structures
  25. straight in rax, rdx. For structures larger than that (3 words or
  26. larger) the caller puts a pointer to an on-stack return struct
  27. [allocated in the caller's stack frame] into the first argument - i.e.
  28. into rdi. All other arguments shift up by one in this case.
  29. Fortunately this case is rare in the kernel.
  30. For 32-bit we have the following conventions - kernel is built with
  31. -mregparm=3 and -freg-struct-return:
  32. x86 function calling convention, 32-bit:
  33. ----------------------------------------
  34. arguments | callee-saved | extra caller-saved | return
  35. [callee-clobbered] | | [callee-clobbered] |
  36. -------------------------------------------------------------------------
  37. eax edx ecx | ebx edi esi ebp [*] | <none> | eax, edx [**]
  38. ( here too esp is obviously invariant across normal function calls. eflags
  39. is clobbered. Leftover arguments are passed over the stack frame. )
  40. [*] In the frame-pointers case ebp is fixed to the stack frame.
  41. [**] We build with -freg-struct-return, which on 32-bit means similar
  42. semantics as on 64-bit: edx can be used for a second return value
  43. (i.e. covering integer and structure sizes up to 64 bits) - after that
  44. it gets more complex and more expensive: 3-word or larger struct returns
  45. get done in the caller's frame and the pointer to the return struct goes
  46. into regparm0, i.e. eax - the other arguments shift up and the
  47. function's register parameters degenerate to regparm=2 in essence.
  48. */
  49. #ifdef CONFIG_X86_64
  50. /*
  51. * 64-bit system call stack frame layout defines and helpers,
  52. * for assembly code:
  53. */
  54. .macro PUSH_REGS rdx=%rdx rcx=%rcx rax=%rax save_ret=0 unwind_hint=1
  55. .if \save_ret
  56. pushq %rsi /* pt_regs->si */
  57. movq 8(%rsp), %rsi /* temporarily store the return address in %rsi */
  58. movq %rdi, 8(%rsp) /* pt_regs->di (overwriting original return address) */
  59. /* We just clobbered the return address - use the IRET frame for unwinding: */
  60. UNWIND_HINT_IRET_REGS offset=3*8
  61. .else
  62. pushq %rdi /* pt_regs->di */
  63. pushq %rsi /* pt_regs->si */
  64. .endif
  65. pushq \rdx /* pt_regs->dx */
  66. pushq \rcx /* pt_regs->cx */
  67. pushq \rax /* pt_regs->ax */
  68. pushq %r8 /* pt_regs->r8 */
  69. pushq %r9 /* pt_regs->r9 */
  70. pushq %r10 /* pt_regs->r10 */
  71. pushq %r11 /* pt_regs->r11 */
  72. pushq %rbx /* pt_regs->rbx */
  73. pushq %rbp /* pt_regs->rbp */
  74. pushq %r12 /* pt_regs->r12 */
  75. pushq %r13 /* pt_regs->r13 */
  76. pushq %r14 /* pt_regs->r14 */
  77. pushq %r15 /* pt_regs->r15 */
  78. .if \unwind_hint
  79. UNWIND_HINT_REGS
  80. .endif
  81. .if \save_ret
  82. pushq %rsi /* return address on top of stack */
  83. .endif
  84. .endm
  85. .macro CLEAR_REGS clear_bp=1
  86. /*
  87. * Sanitize registers of values that a speculation attack might
  88. * otherwise want to exploit. The lower registers are likely clobbered
  89. * well before they could be put to use in a speculative execution
  90. * gadget.
  91. */
  92. xorl %esi, %esi /* nospec si */
  93. xorl %edx, %edx /* nospec dx */
  94. xorl %ecx, %ecx /* nospec cx */
  95. xorl %r8d, %r8d /* nospec r8 */
  96. xorl %r9d, %r9d /* nospec r9 */
  97. xorl %r10d, %r10d /* nospec r10 */
  98. xorl %r11d, %r11d /* nospec r11 */
  99. xorl %ebx, %ebx /* nospec rbx */
  100. .if \clear_bp
  101. xorl %ebp, %ebp /* nospec rbp */
  102. .endif
  103. xorl %r12d, %r12d /* nospec r12 */
  104. xorl %r13d, %r13d /* nospec r13 */
  105. xorl %r14d, %r14d /* nospec r14 */
  106. xorl %r15d, %r15d /* nospec r15 */
  107. .endm
  108. .macro PUSH_AND_CLEAR_REGS rdx=%rdx rcx=%rcx rax=%rax save_ret=0 clear_bp=1 unwind_hint=1
  109. PUSH_REGS rdx=\rdx, rcx=\rcx, rax=\rax, save_ret=\save_ret unwind_hint=\unwind_hint
  110. CLEAR_REGS clear_bp=\clear_bp
  111. .endm
  112. .macro POP_REGS pop_rdi=1
  113. popq %r15
  114. popq %r14
  115. popq %r13
  116. popq %r12
  117. popq %rbp
  118. popq %rbx
  119. popq %r11
  120. popq %r10
  121. popq %r9
  122. popq %r8
  123. popq %rax
  124. popq %rcx
  125. popq %rdx
  126. popq %rsi
  127. .if \pop_rdi
  128. popq %rdi
  129. .endif
  130. .endm
  131. #ifdef CONFIG_MITIGATION_PAGE_TABLE_ISOLATION
  132. /*
  133. * MITIGATION_PAGE_TABLE_ISOLATION PGDs are 8k. Flip bit 12 to switch between the two
  134. * halves:
  135. */
  136. #define PTI_USER_PGTABLE_BIT PAGE_SHIFT
  137. #define PTI_USER_PGTABLE_MASK (1 << PTI_USER_PGTABLE_BIT)
  138. #define PTI_USER_PCID_BIT X86_CR3_PTI_PCID_USER_BIT
  139. #define PTI_USER_PCID_MASK (1 << PTI_USER_PCID_BIT)
  140. #define PTI_USER_PGTABLE_AND_PCID_MASK (PTI_USER_PCID_MASK | PTI_USER_PGTABLE_MASK)
  141. .macro SET_NOFLUSH_BIT reg:req
  142. bts $X86_CR3_PCID_NOFLUSH_BIT, \reg
  143. .endm
  144. .macro ADJUST_KERNEL_CR3 reg:req
  145. ALTERNATIVE "", "SET_NOFLUSH_BIT \reg", X86_FEATURE_PCID
  146. /* Clear PCID and "MITIGATION_PAGE_TABLE_ISOLATION bit", point CR3 at kernel pagetables: */
  147. andq $(~PTI_USER_PGTABLE_AND_PCID_MASK), \reg
  148. .endm
  149. .macro SWITCH_TO_KERNEL_CR3 scratch_reg:req
  150. ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI
  151. mov %cr3, \scratch_reg
  152. ADJUST_KERNEL_CR3 \scratch_reg
  153. mov \scratch_reg, %cr3
  154. .Lend_\@:
  155. .endm
  156. #define THIS_CPU_user_pcid_flush_mask \
  157. PER_CPU_VAR(cpu_tlbstate + TLB_STATE_user_pcid_flush_mask)
  158. .macro SWITCH_TO_USER_CR3 scratch_reg:req scratch_reg2:req
  159. mov %cr3, \scratch_reg
  160. ALTERNATIVE "jmp .Lwrcr3_\@", "", X86_FEATURE_PCID
  161. /*
  162. * Test if the ASID needs a flush.
  163. */
  164. movq \scratch_reg, \scratch_reg2
  165. andq $(0x7FF), \scratch_reg /* mask ASID */
  166. bt \scratch_reg, THIS_CPU_user_pcid_flush_mask
  167. jnc .Lnoflush_\@
  168. /* Flush needed, clear the bit */
  169. btr \scratch_reg, THIS_CPU_user_pcid_flush_mask
  170. movq \scratch_reg2, \scratch_reg
  171. jmp .Lwrcr3_pcid_\@
  172. .Lnoflush_\@:
  173. movq \scratch_reg2, \scratch_reg
  174. SET_NOFLUSH_BIT \scratch_reg
  175. .Lwrcr3_pcid_\@:
  176. /* Flip the ASID to the user version */
  177. orq $(PTI_USER_PCID_MASK), \scratch_reg
  178. .Lwrcr3_\@:
  179. /* Flip the PGD to the user version */
  180. orq $(PTI_USER_PGTABLE_MASK), \scratch_reg
  181. mov \scratch_reg, %cr3
  182. .endm
  183. .macro SWITCH_TO_USER_CR3_NOSTACK scratch_reg:req scratch_reg2:req
  184. ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI
  185. SWITCH_TO_USER_CR3 \scratch_reg \scratch_reg2
  186. .Lend_\@:
  187. .endm
  188. .macro SWITCH_TO_USER_CR3_STACK scratch_reg:req
  189. ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI
  190. pushq %rax
  191. SWITCH_TO_USER_CR3 scratch_reg=\scratch_reg scratch_reg2=%rax
  192. popq %rax
  193. .Lend_\@:
  194. .endm
  195. .macro SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg:req save_reg:req
  196. ALTERNATIVE "jmp .Ldone_\@", "", X86_FEATURE_PTI
  197. movq %cr3, \scratch_reg
  198. movq \scratch_reg, \save_reg
  199. /*
  200. * Test the user pagetable bit. If set, then the user page tables
  201. * are active. If clear CR3 already has the kernel page table
  202. * active.
  203. */
  204. bt $PTI_USER_PGTABLE_BIT, \scratch_reg
  205. jnc .Ldone_\@
  206. ADJUST_KERNEL_CR3 \scratch_reg
  207. movq \scratch_reg, %cr3
  208. .Ldone_\@:
  209. .endm
  210. /* Restore CR3 from a kernel context. May restore a user CR3 value. */
  211. .macro PARANOID_RESTORE_CR3 scratch_reg:req save_reg:req
  212. ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI
  213. /*
  214. * If CR3 contained the kernel page tables at the paranoid exception
  215. * entry, then there is nothing to restore as CR3 is not modified while
  216. * handling the exception.
  217. */
  218. bt $PTI_USER_PGTABLE_BIT, \save_reg
  219. jnc .Lend_\@
  220. ALTERNATIVE "jmp .Lwrcr3_\@", "", X86_FEATURE_PCID
  221. /*
  222. * Check if there's a pending flush for the user ASID we're
  223. * about to set.
  224. */
  225. movq \save_reg, \scratch_reg
  226. andq $(0x7FF), \scratch_reg
  227. btr \scratch_reg, THIS_CPU_user_pcid_flush_mask
  228. jc .Lwrcr3_\@
  229. SET_NOFLUSH_BIT \save_reg
  230. .Lwrcr3_\@:
  231. movq \save_reg, %cr3
  232. .Lend_\@:
  233. .endm
  234. #else /* CONFIG_MITIGATION_PAGE_TABLE_ISOLATION=n: */
  235. .macro SWITCH_TO_KERNEL_CR3 scratch_reg:req
  236. .endm
  237. .macro SWITCH_TO_USER_CR3_NOSTACK scratch_reg:req scratch_reg2:req
  238. .endm
  239. .macro SWITCH_TO_USER_CR3_STACK scratch_reg:req
  240. .endm
  241. .macro SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg:req save_reg:req
  242. .endm
  243. .macro PARANOID_RESTORE_CR3 scratch_reg:req save_reg:req
  244. .endm
  245. #endif
  246. /*
  247. * IBRS kernel mitigation for Spectre_v2.
  248. *
  249. * Assumes full context is established (PUSH_REGS, CR3 and GS) and it clobbers
  250. * the regs it uses (AX, CX, DX). Must be called before the first RET
  251. * instruction (NOTE! UNTRAIN_RET includes a RET instruction)
  252. *
  253. * The optional argument is used to save/restore the current value,
  254. * which is used on the paranoid paths.
  255. *
  256. * Assumes x86_spec_ctrl_{base,current} to have SPEC_CTRL_IBRS set.
  257. */
  258. .macro IBRS_ENTER save_reg
  259. #ifdef CONFIG_MITIGATION_IBRS_ENTRY
  260. ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_KERNEL_IBRS
  261. movl $MSR_IA32_SPEC_CTRL, %ecx
  262. .ifnb \save_reg
  263. rdmsr
  264. shl $32, %rdx
  265. or %rdx, %rax
  266. mov %rax, \save_reg
  267. test $SPEC_CTRL_IBRS, %eax
  268. jz .Ldo_wrmsr_\@
  269. lfence
  270. jmp .Lend_\@
  271. .Ldo_wrmsr_\@:
  272. .endif
  273. movq PER_CPU_VAR(x86_spec_ctrl_current), %rdx
  274. movl %edx, %eax
  275. shr $32, %rdx
  276. wrmsr
  277. .Lend_\@:
  278. #endif
  279. .endm
  280. /*
  281. * Similar to IBRS_ENTER, requires KERNEL GS,CR3 and clobbers (AX, CX, DX)
  282. * regs. Must be called after the last RET.
  283. */
  284. .macro IBRS_EXIT save_reg
  285. #ifdef CONFIG_MITIGATION_IBRS_ENTRY
  286. ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_KERNEL_IBRS
  287. movl $MSR_IA32_SPEC_CTRL, %ecx
  288. .ifnb \save_reg
  289. mov \save_reg, %rdx
  290. .else
  291. movq PER_CPU_VAR(x86_spec_ctrl_current), %rdx
  292. andl $(~SPEC_CTRL_IBRS), %edx
  293. .endif
  294. movl %edx, %eax
  295. shr $32, %rdx
  296. wrmsr
  297. .Lend_\@:
  298. #endif
  299. .endm
  300. /*
  301. * Mitigate Spectre v1 for conditional swapgs code paths.
  302. *
  303. * FENCE_SWAPGS_USER_ENTRY is used in the user entry swapgs code path, to
  304. * prevent a speculative swapgs when coming from kernel space.
  305. *
  306. * FENCE_SWAPGS_KERNEL_ENTRY is used in the kernel entry non-swapgs code path,
  307. * to prevent the swapgs from getting speculatively skipped when coming from
  308. * user space.
  309. */
  310. .macro FENCE_SWAPGS_USER_ENTRY
  311. ALTERNATIVE "", "lfence", X86_FEATURE_FENCE_SWAPGS_USER
  312. .endm
  313. .macro FENCE_SWAPGS_KERNEL_ENTRY
  314. ALTERNATIVE "", "lfence", X86_FEATURE_FENCE_SWAPGS_KERNEL
  315. .endm
  316. .macro STACKLEAK_ERASE_NOCLOBBER
  317. #ifdef CONFIG_GCC_PLUGIN_STACKLEAK
  318. PUSH_AND_CLEAR_REGS
  319. call stackleak_erase
  320. POP_REGS
  321. #endif
  322. .endm
  323. .macro SAVE_AND_SET_GSBASE scratch_reg:req save_reg:req
  324. rdgsbase \save_reg
  325. GET_PERCPU_BASE \scratch_reg
  326. wrgsbase \scratch_reg
  327. .endm
  328. #else /* CONFIG_X86_64 */
  329. # undef UNWIND_HINT_IRET_REGS
  330. # define UNWIND_HINT_IRET_REGS
  331. #endif /* !CONFIG_X86_64 */
  332. .macro STACKLEAK_ERASE
  333. #ifdef CONFIG_GCC_PLUGIN_STACKLEAK
  334. call stackleak_erase
  335. #endif
  336. .endm
  337. #ifdef CONFIG_SMP
  338. /*
  339. * CPU/node NR is loaded from the limit (size) field of a special segment
  340. * descriptor entry in GDT.
  341. */
  342. .macro LOAD_CPU_AND_NODE_SEG_LIMIT reg:req
  343. movq $__CPUNODE_SEG, \reg
  344. lsl \reg, \reg
  345. .endm
  346. /*
  347. * Fetch the per-CPU GSBASE value for this processor and put it in @reg.
  348. * We normally use %gs for accessing per-CPU data, but we are setting up
  349. * %gs here and obviously can not use %gs itself to access per-CPU data.
  350. *
  351. * Do not use RDPID, because KVM loads guest's TSC_AUX on vm-entry and
  352. * may not restore the host's value until the CPU returns to userspace.
  353. * Thus the kernel would consume a guest's TSC_AUX if an NMI arrives
  354. * while running KVM's run loop.
  355. */
  356. .macro GET_PERCPU_BASE reg:req
  357. LOAD_CPU_AND_NODE_SEG_LIMIT \reg
  358. andq $VDSO_CPUNODE_MASK, \reg
  359. movq __per_cpu_offset(, \reg, 8), \reg
  360. .endm
  361. #else
  362. .macro GET_PERCPU_BASE reg:req
  363. movq pcpu_unit_offsets(%rip), \reg
  364. .endm
  365. #endif /* CONFIG_SMP */
  366. #ifdef CONFIG_X86_64
  367. /* rdi: arg1 ... normal C conventions. rax is saved/restored. */
  368. .macro THUNK name, func
  369. SYM_FUNC_START(\name)
  370. pushq %rbp
  371. movq %rsp, %rbp
  372. pushq %rdi
  373. pushq %rsi
  374. pushq %rdx
  375. pushq %rcx
  376. pushq %rax
  377. pushq %r8
  378. pushq %r9
  379. pushq %r10
  380. pushq %r11
  381. call \func
  382. popq %r11
  383. popq %r10
  384. popq %r9
  385. popq %r8
  386. popq %rax
  387. popq %rcx
  388. popq %rdx
  389. popq %rsi
  390. popq %rdi
  391. popq %rbp
  392. RET
  393. SYM_FUNC_END(\name)
  394. _ASM_NOKPROBE(\name)
  395. .endm
  396. #else /* CONFIG_X86_32 */
  397. /* put return address in eax (arg1) */
  398. .macro THUNK name, func, put_ret_addr_in_eax=0
  399. SYM_CODE_START_NOALIGN(\name)
  400. pushl %eax
  401. pushl %ecx
  402. pushl %edx
  403. .if \put_ret_addr_in_eax
  404. /* Place EIP in the arg1 */
  405. movl 3*4(%esp), %eax
  406. .endif
  407. call \func
  408. popl %edx
  409. popl %ecx
  410. popl %eax
  411. RET
  412. _ASM_NOKPROBE(\name)
  413. SYM_CODE_END(\name)
  414. .endm
  415. #endif