calling.h 4.9 KB

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