calling.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 "dwarf2.h"
  39. /*
  40. * 64-bit system call stack frame layout defines and helpers, for
  41. * assembly code (note that the seemingly unnecessary parentheses
  42. * are to prevent cpp from inserting spaces in expressions that get
  43. * passed to macros):
  44. */
  45. #define R15 (0)
  46. #define R14 (8)
  47. #define R13 (16)
  48. #define R12 (24)
  49. #define RBP (32)
  50. #define RBX (40)
  51. /* arguments: interrupts/non tracing syscalls only save up to here: */
  52. #define R11 (48)
  53. #define R10 (56)
  54. #define R9 (64)
  55. #define R8 (72)
  56. #define RAX (80)
  57. #define RCX (88)
  58. #define RDX (96)
  59. #define RSI (104)
  60. #define RDI (112)
  61. #define ORIG_RAX (120) /* + error_code */
  62. /* end of arguments */
  63. /* cpu exception frame or undefined in case of fast syscall: */
  64. #define RIP (128)
  65. #define CS (136)
  66. #define EFLAGS (144)
  67. #define RSP (152)
  68. #define SS (160)
  69. #define ARGOFFSET R11
  70. #define SWFRAME ORIG_RAX
  71. .macro SAVE_ARGS addskip=0, save_rcx=1, save_r891011=1
  72. subq $9*8+\addskip, %rsp
  73. CFI_ADJUST_CFA_OFFSET 9*8+\addskip
  74. movq_cfi rdi, 8*8
  75. movq_cfi rsi, 7*8
  76. movq_cfi rdx, 6*8
  77. .if \save_rcx
  78. movq_cfi rcx, 5*8
  79. .endif
  80. movq_cfi rax, 4*8
  81. .if \save_r891011
  82. movq_cfi r8, 3*8
  83. movq_cfi r9, 2*8
  84. movq_cfi r10, 1*8
  85. movq_cfi r11, 0*8
  86. .endif
  87. .endm
  88. #define ARG_SKIP (9*8)
  89. .macro RESTORE_ARGS rstor_rax=1, addskip=0, rstor_rcx=1, rstor_r11=1, \
  90. rstor_r8910=1, rstor_rdx=1
  91. .if \rstor_r11
  92. movq_cfi_restore 0*8, r11
  93. .endif
  94. .if \rstor_r8910
  95. movq_cfi_restore 1*8, r10
  96. movq_cfi_restore 2*8, r9
  97. movq_cfi_restore 3*8, r8
  98. .endif
  99. .if \rstor_rax
  100. movq_cfi_restore 4*8, rax
  101. .endif
  102. .if \rstor_rcx
  103. movq_cfi_restore 5*8, rcx
  104. .endif
  105. .if \rstor_rdx
  106. movq_cfi_restore 6*8, rdx
  107. .endif
  108. movq_cfi_restore 7*8, rsi
  109. movq_cfi_restore 8*8, rdi
  110. .if ARG_SKIP+\addskip > 0
  111. addq $ARG_SKIP+\addskip, %rsp
  112. CFI_ADJUST_CFA_OFFSET -(ARG_SKIP+\addskip)
  113. .endif
  114. .endm
  115. .macro LOAD_ARGS offset, skiprax=0
  116. movq \offset(%rsp), %r11
  117. movq \offset+8(%rsp), %r10
  118. movq \offset+16(%rsp), %r9
  119. movq \offset+24(%rsp), %r8
  120. movq \offset+40(%rsp), %rcx
  121. movq \offset+48(%rsp), %rdx
  122. movq \offset+56(%rsp), %rsi
  123. movq \offset+64(%rsp), %rdi
  124. .if \skiprax
  125. .else
  126. movq \offset+72(%rsp), %rax
  127. .endif
  128. .endm
  129. #define REST_SKIP (6*8)
  130. .macro SAVE_REST
  131. subq $REST_SKIP, %rsp
  132. CFI_ADJUST_CFA_OFFSET REST_SKIP
  133. movq_cfi rbx, 5*8
  134. movq_cfi rbp, 4*8
  135. movq_cfi r12, 3*8
  136. movq_cfi r13, 2*8
  137. movq_cfi r14, 1*8
  138. movq_cfi r15, 0*8
  139. .endm
  140. .macro RESTORE_REST
  141. movq_cfi_restore 0*8, r15
  142. movq_cfi_restore 1*8, r14
  143. movq_cfi_restore 2*8, r13
  144. movq_cfi_restore 3*8, r12
  145. movq_cfi_restore 4*8, rbp
  146. movq_cfi_restore 5*8, rbx
  147. addq $REST_SKIP, %rsp
  148. CFI_ADJUST_CFA_OFFSET -(REST_SKIP)
  149. .endm
  150. .macro SAVE_ALL
  151. SAVE_ARGS
  152. SAVE_REST
  153. .endm
  154. .macro RESTORE_ALL addskip=0
  155. RESTORE_REST
  156. RESTORE_ARGS 1, \addskip
  157. .endm
  158. .macro icebp
  159. .byte 0xf1
  160. .endm