calling.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. #include <asm/dwarf2.h>
  39. #ifdef CONFIG_X86_64
  40. /*
  41. * 64-bit system call stack frame layout defines and helpers,
  42. * for assembly code:
  43. */
  44. #define R15 0
  45. #define R14 8
  46. #define R13 16
  47. #define R12 24
  48. #define RBP 32
  49. #define RBX 40
  50. /* arguments: interrupts/non tracing syscalls only save up to here: */
  51. #define R11 48
  52. #define R10 56
  53. #define R9 64
  54. #define R8 72
  55. #define RAX 80
  56. #define RCX 88
  57. #define RDX 96
  58. #define RSI 104
  59. #define RDI 112
  60. #define ORIG_RAX 120 /* + error_code */
  61. /* end of arguments */
  62. /* cpu exception frame or undefined in case of fast syscall: */
  63. #define RIP 128
  64. #define CS 136
  65. #define EFLAGS 144
  66. #define RSP 152
  67. #define SS 160
  68. #define ARGOFFSET R11
  69. #define SWFRAME ORIG_RAX
  70. .macro SAVE_ARGS addskip=0, save_rcx=1, save_r891011=1
  71. subq $9*8+\addskip, %rsp
  72. CFI_ADJUST_CFA_OFFSET 9*8+\addskip
  73. movq_cfi rdi, 8*8
  74. movq_cfi rsi, 7*8
  75. movq_cfi rdx, 6*8
  76. .if \save_rcx
  77. movq_cfi rcx, 5*8
  78. .endif
  79. movq_cfi rax, 4*8
  80. .if \save_r891011
  81. movq_cfi r8, 3*8
  82. movq_cfi r9, 2*8
  83. movq_cfi r10, 1*8
  84. movq_cfi r11, 0*8
  85. .endif
  86. .endm
  87. #define ARG_SKIP (9*8)
  88. .macro RESTORE_ARGS rstor_rax=1, addskip=0, rstor_rcx=1, rstor_r11=1, \
  89. rstor_r8910=1, rstor_rdx=1
  90. .if \rstor_r11
  91. movq_cfi_restore 0*8, r11
  92. .endif
  93. .if \rstor_r8910
  94. movq_cfi_restore 1*8, r10
  95. movq_cfi_restore 2*8, r9
  96. movq_cfi_restore 3*8, r8
  97. .endif
  98. .if \rstor_rax
  99. movq_cfi_restore 4*8, rax
  100. .endif
  101. .if \rstor_rcx
  102. movq_cfi_restore 5*8, rcx
  103. .endif
  104. .if \rstor_rdx
  105. movq_cfi_restore 6*8, rdx
  106. .endif
  107. movq_cfi_restore 7*8, rsi
  108. movq_cfi_restore 8*8, rdi
  109. .if ARG_SKIP+\addskip > 0
  110. addq $ARG_SKIP+\addskip, %rsp
  111. CFI_ADJUST_CFA_OFFSET -(ARG_SKIP+\addskip)
  112. .endif
  113. .endm
  114. .macro LOAD_ARGS offset, skiprax=0
  115. movq \offset(%rsp), %r11
  116. movq \offset+8(%rsp), %r10
  117. movq \offset+16(%rsp), %r9
  118. movq \offset+24(%rsp), %r8
  119. movq \offset+40(%rsp), %rcx
  120. movq \offset+48(%rsp), %rdx
  121. movq \offset+56(%rsp), %rsi
  122. movq \offset+64(%rsp), %rdi
  123. .if \skiprax
  124. .else
  125. movq \offset+72(%rsp), %rax
  126. .endif
  127. .endm
  128. #define REST_SKIP (6*8)
  129. .macro SAVE_REST
  130. subq $REST_SKIP, %rsp
  131. CFI_ADJUST_CFA_OFFSET REST_SKIP
  132. movq_cfi rbx, 5*8
  133. movq_cfi rbp, 4*8
  134. movq_cfi r12, 3*8
  135. movq_cfi r13, 2*8
  136. movq_cfi r14, 1*8
  137. movq_cfi r15, 0*8
  138. .endm
  139. .macro RESTORE_REST
  140. movq_cfi_restore 0*8, r15
  141. movq_cfi_restore 1*8, r14
  142. movq_cfi_restore 2*8, r13
  143. movq_cfi_restore 3*8, r12
  144. movq_cfi_restore 4*8, rbp
  145. movq_cfi_restore 5*8, rbx
  146. addq $REST_SKIP, %rsp
  147. CFI_ADJUST_CFA_OFFSET -(REST_SKIP)
  148. .endm
  149. .macro SAVE_ALL
  150. SAVE_ARGS
  151. SAVE_REST
  152. .endm
  153. .macro RESTORE_ALL addskip=0
  154. RESTORE_REST
  155. RESTORE_ARGS 1, \addskip
  156. .endm
  157. .macro icebp
  158. .byte 0xf1
  159. .endm
  160. #else /* CONFIG_X86_64 */
  161. /*
  162. * For 32bit only simplified versions of SAVE_ALL/RESTORE_ALL. These
  163. * are different from the entry_32.S versions in not changing the segment
  164. * registers. So only suitable for in kernel use, not when transitioning
  165. * from or to user space. The resulting stack frame is not a standard
  166. * pt_regs frame. The main use case is calling C code from assembler
  167. * when all the registers need to be preserved.
  168. */
  169. .macro SAVE_ALL
  170. pushl_cfi %eax
  171. CFI_REL_OFFSET eax, 0
  172. pushl_cfi %ebp
  173. CFI_REL_OFFSET ebp, 0
  174. pushl_cfi %edi
  175. CFI_REL_OFFSET edi, 0
  176. pushl_cfi %esi
  177. CFI_REL_OFFSET esi, 0
  178. pushl_cfi %edx
  179. CFI_REL_OFFSET edx, 0
  180. pushl_cfi %ecx
  181. CFI_REL_OFFSET ecx, 0
  182. pushl_cfi %ebx
  183. CFI_REL_OFFSET ebx, 0
  184. .endm
  185. .macro RESTORE_ALL
  186. popl_cfi %ebx
  187. CFI_RESTORE ebx
  188. popl_cfi %ecx
  189. CFI_RESTORE ecx
  190. popl_cfi %edx
  191. CFI_RESTORE edx
  192. popl_cfi %esi
  193. CFI_RESTORE esi
  194. popl_cfi %edi
  195. CFI_RESTORE edi
  196. popl_cfi %ebp
  197. CFI_RESTORE ebp
  198. popl_cfi %eax
  199. CFI_RESTORE eax
  200. .endm
  201. #endif /* CONFIG_X86_64 */