calling.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #include <linux/jump_label.h>
  2. /*
  3. x86 function call convention, 64-bit:
  4. -------------------------------------
  5. arguments | callee-saved | extra caller-saved | return
  6. [callee-clobbered] | | [callee-clobbered] |
  7. ---------------------------------------------------------------------------
  8. rdi rsi rdx rcx r8-9 | rbx rbp [*] r12-15 | r10-11 | rax, rdx [**]
  9. ( rsp is obviously invariant across normal function calls. (gcc can 'merge'
  10. functions when it sees tail-call optimization possibilities) rflags is
  11. clobbered. Leftover arguments are passed over the stack frame.)
  12. [*] In the frame-pointers case rbp is fixed to the stack frame.
  13. [**] for struct return values wider than 64 bits the return convention is a
  14. bit more complex: up to 128 bits width we return small structures
  15. straight in rax, rdx. For structures larger than that (3 words or
  16. larger) the caller puts a pointer to an on-stack return struct
  17. [allocated in the caller's stack frame] into the first argument - i.e.
  18. into rdi. All other arguments shift up by one in this case.
  19. Fortunately this case is rare in the kernel.
  20. For 32-bit we have the following conventions - kernel is built with
  21. -mregparm=3 and -freg-struct-return:
  22. x86 function calling convention, 32-bit:
  23. ----------------------------------------
  24. arguments | callee-saved | extra caller-saved | return
  25. [callee-clobbered] | | [callee-clobbered] |
  26. -------------------------------------------------------------------------
  27. eax edx ecx | ebx edi esi ebp [*] | <none> | eax, edx [**]
  28. ( here too esp is obviously invariant across normal function calls. eflags
  29. is clobbered. Leftover arguments are passed over the stack frame. )
  30. [*] In the frame-pointers case ebp is fixed to the stack frame.
  31. [**] We build with -freg-struct-return, which on 32-bit means similar
  32. semantics as on 64-bit: edx can be used for a second return value
  33. (i.e. covering integer and structure sizes up to 64 bits) - after that
  34. it gets more complex and more expensive: 3-word or larger struct returns
  35. get done in the caller's frame and the pointer to the return struct goes
  36. into regparm0, i.e. eax - the other arguments shift up and the
  37. function's register parameters degenerate to regparm=2 in essence.
  38. */
  39. #ifdef CONFIG_X86_64
  40. /*
  41. * 64-bit system call stack frame layout defines and helpers,
  42. * for assembly code:
  43. */
  44. /* The layout forms the "struct pt_regs" on the stack: */
  45. /*
  46. * C ABI says these regs are callee-preserved. They aren't saved on kernel entry
  47. * unless syscall needs a complete, fully filled "struct pt_regs".
  48. */
  49. #define R15 0*8
  50. #define R14 1*8
  51. #define R13 2*8
  52. #define R12 3*8
  53. #define RBP 4*8
  54. #define RBX 5*8
  55. /* These regs are callee-clobbered. Always saved on kernel entry. */
  56. #define R11 6*8
  57. #define R10 7*8
  58. #define R9 8*8
  59. #define R8 9*8
  60. #define RAX 10*8
  61. #define RCX 11*8
  62. #define RDX 12*8
  63. #define RSI 13*8
  64. #define RDI 14*8
  65. /*
  66. * On syscall entry, this is syscall#. On CPU exception, this is error code.
  67. * On hw interrupt, it's IRQ number:
  68. */
  69. #define ORIG_RAX 15*8
  70. /* Return frame for iretq */
  71. #define RIP 16*8
  72. #define CS 17*8
  73. #define EFLAGS 18*8
  74. #define RSP 19*8
  75. #define SS 20*8
  76. #define SIZEOF_PTREGS 21*8
  77. .macro ALLOC_PT_GPREGS_ON_STACK addskip=0
  78. addq $-(15*8+\addskip), %rsp
  79. .endm
  80. .macro SAVE_C_REGS_HELPER offset=0 rax=1 rcx=1 r8910=1 r11=1
  81. .if \r11
  82. movq %r11, 6*8+\offset(%rsp)
  83. .endif
  84. .if \r8910
  85. movq %r10, 7*8+\offset(%rsp)
  86. movq %r9, 8*8+\offset(%rsp)
  87. movq %r8, 9*8+\offset(%rsp)
  88. .endif
  89. .if \rax
  90. movq %rax, 10*8+\offset(%rsp)
  91. .endif
  92. .if \rcx
  93. movq %rcx, 11*8+\offset(%rsp)
  94. .endif
  95. movq %rdx, 12*8+\offset(%rsp)
  96. movq %rsi, 13*8+\offset(%rsp)
  97. movq %rdi, 14*8+\offset(%rsp)
  98. .endm
  99. .macro SAVE_C_REGS offset=0
  100. SAVE_C_REGS_HELPER \offset, 1, 1, 1, 1
  101. .endm
  102. .macro SAVE_C_REGS_EXCEPT_RAX_RCX offset=0
  103. SAVE_C_REGS_HELPER \offset, 0, 0, 1, 1
  104. .endm
  105. .macro SAVE_C_REGS_EXCEPT_R891011
  106. SAVE_C_REGS_HELPER 0, 1, 1, 0, 0
  107. .endm
  108. .macro SAVE_C_REGS_EXCEPT_RCX_R891011
  109. SAVE_C_REGS_HELPER 0, 1, 0, 0, 0
  110. .endm
  111. .macro SAVE_C_REGS_EXCEPT_RAX_RCX_R11
  112. SAVE_C_REGS_HELPER 0, 0, 0, 1, 0
  113. .endm
  114. .macro SAVE_EXTRA_REGS offset=0
  115. movq %r15, 0*8+\offset(%rsp)
  116. movq %r14, 1*8+\offset(%rsp)
  117. movq %r13, 2*8+\offset(%rsp)
  118. movq %r12, 3*8+\offset(%rsp)
  119. movq %rbp, 4*8+\offset(%rsp)
  120. movq %rbx, 5*8+\offset(%rsp)
  121. .endm
  122. .macro RESTORE_EXTRA_REGS offset=0
  123. movq 0*8+\offset(%rsp), %r15
  124. movq 1*8+\offset(%rsp), %r14
  125. movq 2*8+\offset(%rsp), %r13
  126. movq 3*8+\offset(%rsp), %r12
  127. movq 4*8+\offset(%rsp), %rbp
  128. movq 5*8+\offset(%rsp), %rbx
  129. .endm
  130. .macro ZERO_EXTRA_REGS
  131. xorl %r15d, %r15d
  132. xorl %r14d, %r14d
  133. xorl %r13d, %r13d
  134. xorl %r12d, %r12d
  135. xorl %ebp, %ebp
  136. xorl %ebx, %ebx
  137. .endm
  138. .macro RESTORE_C_REGS_HELPER rstor_rax=1, rstor_rcx=1, rstor_r11=1, rstor_r8910=1, rstor_rdx=1
  139. .if \rstor_r11
  140. movq 6*8(%rsp), %r11
  141. .endif
  142. .if \rstor_r8910
  143. movq 7*8(%rsp), %r10
  144. movq 8*8(%rsp), %r9
  145. movq 9*8(%rsp), %r8
  146. .endif
  147. .if \rstor_rax
  148. movq 10*8(%rsp), %rax
  149. .endif
  150. .if \rstor_rcx
  151. movq 11*8(%rsp), %rcx
  152. .endif
  153. .if \rstor_rdx
  154. movq 12*8(%rsp), %rdx
  155. .endif
  156. movq 13*8(%rsp), %rsi
  157. movq 14*8(%rsp), %rdi
  158. .endm
  159. .macro RESTORE_C_REGS
  160. RESTORE_C_REGS_HELPER 1,1,1,1,1
  161. .endm
  162. .macro RESTORE_C_REGS_EXCEPT_RAX
  163. RESTORE_C_REGS_HELPER 0,1,1,1,1
  164. .endm
  165. .macro RESTORE_C_REGS_EXCEPT_RCX
  166. RESTORE_C_REGS_HELPER 1,0,1,1,1
  167. .endm
  168. .macro RESTORE_C_REGS_EXCEPT_R11
  169. RESTORE_C_REGS_HELPER 1,1,0,1,1
  170. .endm
  171. .macro RESTORE_C_REGS_EXCEPT_RCX_R11
  172. RESTORE_C_REGS_HELPER 1,0,0,1,1
  173. .endm
  174. .macro REMOVE_PT_GPREGS_FROM_STACK addskip=0
  175. subq $-(15*8+\addskip), %rsp
  176. .endm
  177. .macro icebp
  178. .byte 0xf1
  179. .endm
  180. #endif /* CONFIG_X86_64 */
  181. /*
  182. * This does 'call enter_from_user_mode' unless we can avoid it based on
  183. * kernel config or using the static jump infrastructure.
  184. */
  185. .macro CALL_enter_from_user_mode
  186. #ifdef CONFIG_CONTEXT_TRACKING
  187. #ifdef HAVE_JUMP_LABEL
  188. STATIC_JUMP_IF_FALSE .Lafter_call_\@, context_tracking_enabled, def=0
  189. #endif
  190. call enter_from_user_mode
  191. .Lafter_call_\@:
  192. #endif
  193. .endm