calling.h 6.2 KB

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