calling.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 ALLOC_PT_GPREGS_ON_STACK
  85. addq $-(15*8), %rsp
  86. .endm
  87. .macro SAVE_C_REGS_HELPER offset=0 rax=1 rcx=1 r8910=1 r11=1
  88. .if \r11
  89. movq %r11, 6*8+\offset(%rsp)
  90. .endif
  91. .if \r8910
  92. movq %r10, 7*8+\offset(%rsp)
  93. movq %r9, 8*8+\offset(%rsp)
  94. movq %r8, 9*8+\offset(%rsp)
  95. .endif
  96. .if \rax
  97. movq %rax, 10*8+\offset(%rsp)
  98. .endif
  99. .if \rcx
  100. movq %rcx, 11*8+\offset(%rsp)
  101. .endif
  102. movq %rdx, 12*8+\offset(%rsp)
  103. movq %rsi, 13*8+\offset(%rsp)
  104. movq %rdi, 14*8+\offset(%rsp)
  105. UNWIND_HINT_REGS offset=\offset extra=0
  106. .endm
  107. .macro SAVE_C_REGS offset=0
  108. SAVE_C_REGS_HELPER \offset, 1, 1, 1, 1
  109. .endm
  110. .macro SAVE_C_REGS_EXCEPT_RAX_RCX offset=0
  111. SAVE_C_REGS_HELPER \offset, 0, 0, 1, 1
  112. .endm
  113. .macro SAVE_C_REGS_EXCEPT_R891011
  114. SAVE_C_REGS_HELPER 0, 1, 1, 0, 0
  115. .endm
  116. .macro SAVE_C_REGS_EXCEPT_RCX_R891011
  117. SAVE_C_REGS_HELPER 0, 1, 0, 0, 0
  118. .endm
  119. .macro SAVE_C_REGS_EXCEPT_RAX_RCX_R11
  120. SAVE_C_REGS_HELPER 0, 0, 0, 1, 0
  121. .endm
  122. .macro SAVE_EXTRA_REGS offset=0
  123. movq %r15, 0*8+\offset(%rsp)
  124. movq %r14, 1*8+\offset(%rsp)
  125. movq %r13, 2*8+\offset(%rsp)
  126. movq %r12, 3*8+\offset(%rsp)
  127. movq %rbp, 4*8+\offset(%rsp)
  128. movq %rbx, 5*8+\offset(%rsp)
  129. UNWIND_HINT_REGS offset=\offset
  130. .endm
  131. .macro POP_EXTRA_REGS
  132. popq %r15
  133. popq %r14
  134. popq %r13
  135. popq %r12
  136. popq %rbp
  137. popq %rbx
  138. .endm
  139. .macro POP_C_REGS
  140. popq %r11
  141. popq %r10
  142. popq %r9
  143. popq %r8
  144. popq %rax
  145. popq %rcx
  146. popq %rdx
  147. popq %rsi
  148. popq %rdi
  149. .endm
  150. .macro icebp
  151. .byte 0xf1
  152. .endm
  153. /*
  154. * This is a sneaky trick to help the unwinder find pt_regs on the stack. The
  155. * frame pointer is replaced with an encoded pointer to pt_regs. The encoding
  156. * is just setting the LSB, which makes it an invalid stack address and is also
  157. * a signal to the unwinder that it's a pt_regs pointer in disguise.
  158. *
  159. * NOTE: This macro must be used *after* SAVE_EXTRA_REGS because it corrupts
  160. * the original rbp.
  161. */
  162. .macro ENCODE_FRAME_POINTER ptregs_offset=0
  163. #ifdef CONFIG_FRAME_POINTER
  164. .if \ptregs_offset
  165. leaq \ptregs_offset(%rsp), %rbp
  166. .else
  167. mov %rsp, %rbp
  168. .endif
  169. orq $0x1, %rbp
  170. #endif
  171. .endm
  172. #ifdef CONFIG_PAGE_TABLE_ISOLATION
  173. /*
  174. * PAGE_TABLE_ISOLATION PGDs are 8k. Flip bit 12 to switch between the two
  175. * halves:
  176. */
  177. #define PTI_USER_PGTABLE_BIT PAGE_SHIFT
  178. #define PTI_USER_PGTABLE_MASK (1 << PTI_USER_PGTABLE_BIT)
  179. #define PTI_USER_PCID_BIT X86_CR3_PTI_PCID_USER_BIT
  180. #define PTI_USER_PCID_MASK (1 << PTI_USER_PCID_BIT)
  181. #define PTI_USER_PGTABLE_AND_PCID_MASK (PTI_USER_PCID_MASK | PTI_USER_PGTABLE_MASK)
  182. .macro SET_NOFLUSH_BIT reg:req
  183. bts $X86_CR3_PCID_NOFLUSH_BIT, \reg
  184. .endm
  185. .macro ADJUST_KERNEL_CR3 reg:req
  186. ALTERNATIVE "", "SET_NOFLUSH_BIT \reg", X86_FEATURE_PCID
  187. /* Clear PCID and "PAGE_TABLE_ISOLATION bit", point CR3 at kernel pagetables: */
  188. andq $(~PTI_USER_PGTABLE_AND_PCID_MASK), \reg
  189. .endm
  190. .macro SWITCH_TO_KERNEL_CR3 scratch_reg:req
  191. ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI
  192. mov %cr3, \scratch_reg
  193. ADJUST_KERNEL_CR3 \scratch_reg
  194. mov \scratch_reg, %cr3
  195. .Lend_\@:
  196. .endm
  197. #define THIS_CPU_user_pcid_flush_mask \
  198. PER_CPU_VAR(cpu_tlbstate) + TLB_STATE_user_pcid_flush_mask
  199. .macro SWITCH_TO_USER_CR3_NOSTACK scratch_reg:req scratch_reg2:req
  200. ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI
  201. mov %cr3, \scratch_reg
  202. ALTERNATIVE "jmp .Lwrcr3_\@", "", X86_FEATURE_PCID
  203. /*
  204. * Test if the ASID needs a flush.
  205. */
  206. movq \scratch_reg, \scratch_reg2
  207. andq $(0x7FF), \scratch_reg /* mask ASID */
  208. bt \scratch_reg, THIS_CPU_user_pcid_flush_mask
  209. jnc .Lnoflush_\@
  210. /* Flush needed, clear the bit */
  211. btr \scratch_reg, THIS_CPU_user_pcid_flush_mask
  212. movq \scratch_reg2, \scratch_reg
  213. jmp .Lwrcr3_pcid_\@
  214. .Lnoflush_\@:
  215. movq \scratch_reg2, \scratch_reg
  216. SET_NOFLUSH_BIT \scratch_reg
  217. .Lwrcr3_pcid_\@:
  218. /* Flip the ASID to the user version */
  219. orq $(PTI_USER_PCID_MASK), \scratch_reg
  220. .Lwrcr3_\@:
  221. /* Flip the PGD to the user version */
  222. orq $(PTI_USER_PGTABLE_MASK), \scratch_reg
  223. mov \scratch_reg, %cr3
  224. .Lend_\@:
  225. .endm
  226. .macro SWITCH_TO_USER_CR3_STACK scratch_reg:req
  227. pushq %rax
  228. SWITCH_TO_USER_CR3_NOSTACK scratch_reg=\scratch_reg scratch_reg2=%rax
  229. popq %rax
  230. .endm
  231. .macro SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg:req save_reg:req
  232. ALTERNATIVE "jmp .Ldone_\@", "", X86_FEATURE_PTI
  233. movq %cr3, \scratch_reg
  234. movq \scratch_reg, \save_reg
  235. /*
  236. * Test the user pagetable bit. If set, then the user page tables
  237. * are active. If clear CR3 already has the kernel page table
  238. * active.
  239. */
  240. bt $PTI_USER_PGTABLE_BIT, \scratch_reg
  241. jnc .Ldone_\@
  242. ADJUST_KERNEL_CR3 \scratch_reg
  243. movq \scratch_reg, %cr3
  244. .Ldone_\@:
  245. .endm
  246. .macro RESTORE_CR3 scratch_reg:req save_reg:req
  247. ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI
  248. ALTERNATIVE "jmp .Lwrcr3_\@", "", X86_FEATURE_PCID
  249. /*
  250. * KERNEL pages can always resume with NOFLUSH as we do
  251. * explicit flushes.
  252. */
  253. bt $PTI_USER_PGTABLE_BIT, \save_reg
  254. jnc .Lnoflush_\@
  255. /*
  256. * Check if there's a pending flush for the user ASID we're
  257. * about to set.
  258. */
  259. movq \save_reg, \scratch_reg
  260. andq $(0x7FF), \scratch_reg
  261. bt \scratch_reg, THIS_CPU_user_pcid_flush_mask
  262. jnc .Lnoflush_\@
  263. btr \scratch_reg, THIS_CPU_user_pcid_flush_mask
  264. jmp .Lwrcr3_\@
  265. .Lnoflush_\@:
  266. SET_NOFLUSH_BIT \save_reg
  267. .Lwrcr3_\@:
  268. /*
  269. * The CR3 write could be avoided when not changing its value,
  270. * but would require a CR3 read *and* a scratch register.
  271. */
  272. movq \save_reg, %cr3
  273. .Lend_\@:
  274. .endm
  275. #else /* CONFIG_PAGE_TABLE_ISOLATION=n: */
  276. .macro SWITCH_TO_KERNEL_CR3 scratch_reg:req
  277. .endm
  278. .macro SWITCH_TO_USER_CR3_NOSTACK scratch_reg:req scratch_reg2:req
  279. .endm
  280. .macro SWITCH_TO_USER_CR3_STACK scratch_reg:req
  281. .endm
  282. .macro SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg:req save_reg:req
  283. .endm
  284. .macro RESTORE_CR3 scratch_reg:req save_reg:req
  285. .endm
  286. #endif
  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 HAVE_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