Ingo Molnar | 0c2bd5a | 2008-01-30 13:32:49 +0100 | [diff] [blame] | 1 | /* |
Ingo Molnar | 063f891 | 2009-02-03 18:02:36 +0100 | [diff] [blame] | 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 | |
| 10 | ( rsp is obviously invariant across normal function calls. (gcc can 'merge' |
| 11 | functions when it sees tail-call optimization possibilities) rflags is |
| 12 | clobbered. Leftover arguments are passed over the stack frame.) |
| 13 | |
| 14 | [*] In the frame-pointers case rbp is fixed to the stack frame. |
| 15 | |
| 16 | [**] for struct return values wider than 64 bits the return convention is a |
| 17 | bit more complex: up to 128 bits width we return small structures |
| 18 | straight in rax, rdx. For structures larger than that (3 words or |
| 19 | larger) the caller puts a pointer to an on-stack return struct |
| 20 | [allocated in the caller's stack frame] into the first argument - i.e. |
| 21 | into rdi. All other arguments shift up by one in this case. |
| 22 | Fortunately this case is rare in the kernel. |
| 23 | |
| 24 | For 32-bit we have the following conventions - kernel is built with |
| 25 | -mregparm=3 and -freg-struct-return: |
| 26 | |
| 27 | x86 function calling convention, 32-bit: |
| 28 | ---------------------------------------- |
| 29 | arguments | callee-saved | extra caller-saved | return |
| 30 | [callee-clobbered] | | [callee-clobbered] | |
| 31 | ------------------------------------------------------------------------- |
| 32 | eax edx ecx | ebx edi esi ebp [*] | <none> | eax, edx [**] |
| 33 | |
| 34 | ( here too esp is obviously invariant across normal function calls. eflags |
| 35 | is clobbered. Leftover arguments are passed over the stack frame. ) |
| 36 | |
| 37 | [*] In the frame-pointers case ebp is fixed to the stack frame. |
| 38 | |
| 39 | [**] We build with -freg-struct-return, which on 32-bit means similar |
| 40 | semantics as on 64-bit: edx can be used for a second return value |
| 41 | (i.e. covering integer and structure sizes up to 64 bits) - after that |
| 42 | it gets more complex and more expensive: 3-word or larger struct returns |
| 43 | get done in the caller's frame and the pointer to the return struct goes |
| 44 | into regparm0, i.e. eax - the other arguments shift up and the |
| 45 | function's register parameters degenerate to regparm=2 in essence. |
| 46 | |
| 47 | */ |
| 48 | |
| 49 | |
| 50 | /* |
Jan Beulich | 3234282 | 2010-10-19 14:52:26 +0100 | [diff] [blame^] | 51 | * 64-bit system call stack frame layout defines and helpers, for |
| 52 | * assembly code (note that the seemingly unnecessary parentheses |
| 53 | * are to prevent cpp from inserting spaces in expressions that get |
| 54 | * passed to macros): |
Ingo Molnar | 0c2bd5a | 2008-01-30 13:32:49 +0100 | [diff] [blame] | 55 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Jan Beulich | 3234282 | 2010-10-19 14:52:26 +0100 | [diff] [blame^] | 57 | #define R15 (0) |
| 58 | #define R14 (8) |
| 59 | #define R13 (16) |
| 60 | #define R12 (24) |
| 61 | #define RBP (32) |
| 62 | #define RBX (40) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Ingo Molnar | 063f891 | 2009-02-03 18:02:36 +0100 | [diff] [blame] | 64 | /* arguments: interrupts/non tracing syscalls only save up to here: */ |
Jan Beulich | 3234282 | 2010-10-19 14:52:26 +0100 | [diff] [blame^] | 65 | #define R11 (48) |
| 66 | #define R10 (56) |
| 67 | #define R9 (64) |
| 68 | #define R8 (72) |
| 69 | #define RAX (80) |
| 70 | #define RCX (88) |
| 71 | #define RDX (96) |
| 72 | #define RSI (104) |
| 73 | #define RDI (112) |
| 74 | #define ORIG_RAX (120) /* + error_code */ |
Ingo Molnar | 0c2bd5a | 2008-01-30 13:32:49 +0100 | [diff] [blame] | 75 | /* end of arguments */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
Ingo Molnar | 063f891 | 2009-02-03 18:02:36 +0100 | [diff] [blame] | 77 | /* cpu exception frame or undefined in case of fast syscall: */ |
Jan Beulich | 3234282 | 2010-10-19 14:52:26 +0100 | [diff] [blame^] | 78 | #define RIP (128) |
| 79 | #define CS (136) |
| 80 | #define EFLAGS (144) |
| 81 | #define RSP (152) |
| 82 | #define SS (160) |
Ingo Molnar | 0c2bd5a | 2008-01-30 13:32:49 +0100 | [diff] [blame] | 83 | |
| 84 | #define ARGOFFSET R11 |
| 85 | #define SWFRAME ORIG_RAX |
| 86 | |
| 87 | .macro SAVE_ARGS addskip=0, norcx=0, nor891011=0 |
| 88 | subq $9*8+\addskip, %rsp |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | CFI_ADJUST_CFA_OFFSET 9*8+\addskip |
Ingo Molnar | 0c2bd5a | 2008-01-30 13:32:49 +0100 | [diff] [blame] | 90 | movq %rdi, 8*8(%rsp) |
| 91 | CFI_REL_OFFSET rdi, 8*8 |
| 92 | movq %rsi, 7*8(%rsp) |
| 93 | CFI_REL_OFFSET rsi, 7*8 |
| 94 | movq %rdx, 6*8(%rsp) |
| 95 | CFI_REL_OFFSET rdx, 6*8 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | .if \norcx |
| 97 | .else |
Ingo Molnar | 0c2bd5a | 2008-01-30 13:32:49 +0100 | [diff] [blame] | 98 | movq %rcx, 5*8(%rsp) |
| 99 | CFI_REL_OFFSET rcx, 5*8 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | .endif |
Ingo Molnar | 0c2bd5a | 2008-01-30 13:32:49 +0100 | [diff] [blame] | 101 | movq %rax, 4*8(%rsp) |
| 102 | CFI_REL_OFFSET rax, 4*8 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | .if \nor891011 |
| 104 | .else |
Ingo Molnar | 0c2bd5a | 2008-01-30 13:32:49 +0100 | [diff] [blame] | 105 | movq %r8, 3*8(%rsp) |
| 106 | CFI_REL_OFFSET r8, 3*8 |
| 107 | movq %r9, 2*8(%rsp) |
| 108 | CFI_REL_OFFSET r9, 2*8 |
| 109 | movq %r10, 1*8(%rsp) |
| 110 | CFI_REL_OFFSET r10, 1*8 |
| 111 | movq %r11, (%rsp) |
| 112 | CFI_REL_OFFSET r11, 0*8 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | .endif |
| 114 | .endm |
| 115 | |
Jan Beulich | 3234282 | 2010-10-19 14:52:26 +0100 | [diff] [blame^] | 116 | #define ARG_SKIP (9*8) |
Ingo Molnar | 0c2bd5a | 2008-01-30 13:32:49 +0100 | [diff] [blame] | 117 | |
| 118 | .macro RESTORE_ARGS skiprax=0, addskip=0, skiprcx=0, skipr11=0, \ |
| 119 | skipr8910=0, skiprdx=0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | .if \skipr11 |
| 121 | .else |
Ingo Molnar | 0c2bd5a | 2008-01-30 13:32:49 +0100 | [diff] [blame] | 122 | movq (%rsp), %r11 |
Jan Beulich | 7effaa8 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 123 | CFI_RESTORE r11 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | .endif |
| 125 | .if \skipr8910 |
| 126 | .else |
Ingo Molnar | 0c2bd5a | 2008-01-30 13:32:49 +0100 | [diff] [blame] | 127 | movq 1*8(%rsp), %r10 |
Jan Beulich | 7effaa8 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 128 | CFI_RESTORE r10 |
Ingo Molnar | 0c2bd5a | 2008-01-30 13:32:49 +0100 | [diff] [blame] | 129 | movq 2*8(%rsp), %r9 |
Jan Beulich | 7effaa8 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 130 | CFI_RESTORE r9 |
Ingo Molnar | 0c2bd5a | 2008-01-30 13:32:49 +0100 | [diff] [blame] | 131 | movq 3*8(%rsp), %r8 |
Jan Beulich | 7effaa8 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 132 | CFI_RESTORE r8 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | .endif |
| 134 | .if \skiprax |
| 135 | .else |
Ingo Molnar | 0c2bd5a | 2008-01-30 13:32:49 +0100 | [diff] [blame] | 136 | movq 4*8(%rsp), %rax |
Jan Beulich | 7effaa8 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 137 | CFI_RESTORE rax |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | .endif |
| 139 | .if \skiprcx |
| 140 | .else |
Ingo Molnar | 0c2bd5a | 2008-01-30 13:32:49 +0100 | [diff] [blame] | 141 | movq 5*8(%rsp), %rcx |
Jan Beulich | 7effaa8 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 142 | CFI_RESTORE rcx |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | .endif |
| 144 | .if \skiprdx |
| 145 | .else |
Ingo Molnar | 0c2bd5a | 2008-01-30 13:32:49 +0100 | [diff] [blame] | 146 | movq 6*8(%rsp), %rdx |
Jan Beulich | 7effaa8 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 147 | CFI_RESTORE rdx |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | .endif |
Ingo Molnar | 0c2bd5a | 2008-01-30 13:32:49 +0100 | [diff] [blame] | 149 | movq 7*8(%rsp), %rsi |
Jan Beulich | 7effaa8 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 150 | CFI_RESTORE rsi |
Ingo Molnar | 0c2bd5a | 2008-01-30 13:32:49 +0100 | [diff] [blame] | 151 | movq 8*8(%rsp), %rdi |
Jan Beulich | 7effaa8 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 152 | CFI_RESTORE rdi |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | .if ARG_SKIP+\addskip > 0 |
Ingo Molnar | 0c2bd5a | 2008-01-30 13:32:49 +0100 | [diff] [blame] | 154 | addq $ARG_SKIP+\addskip, %rsp |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | CFI_ADJUST_CFA_OFFSET -(ARG_SKIP+\addskip) |
| 156 | .endif |
Ingo Molnar | 0c2bd5a | 2008-01-30 13:32:49 +0100 | [diff] [blame] | 157 | .endm |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
Roland McGrath | d4d6715 | 2008-07-09 02:38:07 -0700 | [diff] [blame] | 159 | .macro LOAD_ARGS offset, skiprax=0 |
Ingo Molnar | 0c2bd5a | 2008-01-30 13:32:49 +0100 | [diff] [blame] | 160 | movq \offset(%rsp), %r11 |
| 161 | movq \offset+8(%rsp), %r10 |
| 162 | movq \offset+16(%rsp), %r9 |
| 163 | movq \offset+24(%rsp), %r8 |
| 164 | movq \offset+40(%rsp), %rcx |
| 165 | movq \offset+48(%rsp), %rdx |
| 166 | movq \offset+56(%rsp), %rsi |
| 167 | movq \offset+64(%rsp), %rdi |
Roland McGrath | d4d6715 | 2008-07-09 02:38:07 -0700 | [diff] [blame] | 168 | .if \skiprax |
| 169 | .else |
Ingo Molnar | 0c2bd5a | 2008-01-30 13:32:49 +0100 | [diff] [blame] | 170 | movq \offset+72(%rsp), %rax |
Roland McGrath | d4d6715 | 2008-07-09 02:38:07 -0700 | [diff] [blame] | 171 | .endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | .endm |
Ingo Molnar | 0c2bd5a | 2008-01-30 13:32:49 +0100 | [diff] [blame] | 173 | |
Jan Beulich | 3234282 | 2010-10-19 14:52:26 +0100 | [diff] [blame^] | 174 | #define REST_SKIP (6*8) |
Ingo Molnar | 0c2bd5a | 2008-01-30 13:32:49 +0100 | [diff] [blame] | 175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | .macro SAVE_REST |
Ingo Molnar | 0c2bd5a | 2008-01-30 13:32:49 +0100 | [diff] [blame] | 177 | subq $REST_SKIP, %rsp |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | CFI_ADJUST_CFA_OFFSET REST_SKIP |
Ingo Molnar | 0c2bd5a | 2008-01-30 13:32:49 +0100 | [diff] [blame] | 179 | movq %rbx, 5*8(%rsp) |
| 180 | CFI_REL_OFFSET rbx, 5*8 |
| 181 | movq %rbp, 4*8(%rsp) |
| 182 | CFI_REL_OFFSET rbp, 4*8 |
| 183 | movq %r12, 3*8(%rsp) |
| 184 | CFI_REL_OFFSET r12, 3*8 |
| 185 | movq %r13, 2*8(%rsp) |
| 186 | CFI_REL_OFFSET r13, 2*8 |
| 187 | movq %r14, 1*8(%rsp) |
| 188 | CFI_REL_OFFSET r14, 1*8 |
| 189 | movq %r15, (%rsp) |
| 190 | CFI_REL_OFFSET r15, 0*8 |
| 191 | .endm |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | |
| 193 | .macro RESTORE_REST |
Ingo Molnar | 0c2bd5a | 2008-01-30 13:32:49 +0100 | [diff] [blame] | 194 | movq (%rsp), %r15 |
Jan Beulich | 7effaa8 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 195 | CFI_RESTORE r15 |
Ingo Molnar | 0c2bd5a | 2008-01-30 13:32:49 +0100 | [diff] [blame] | 196 | movq 1*8(%rsp), %r14 |
Jan Beulich | 7effaa8 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 197 | CFI_RESTORE r14 |
Ingo Molnar | 0c2bd5a | 2008-01-30 13:32:49 +0100 | [diff] [blame] | 198 | movq 2*8(%rsp), %r13 |
Jan Beulich | 7effaa8 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 199 | CFI_RESTORE r13 |
Ingo Molnar | 0c2bd5a | 2008-01-30 13:32:49 +0100 | [diff] [blame] | 200 | movq 3*8(%rsp), %r12 |
Jan Beulich | 7effaa8 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 201 | CFI_RESTORE r12 |
Ingo Molnar | 0c2bd5a | 2008-01-30 13:32:49 +0100 | [diff] [blame] | 202 | movq 4*8(%rsp), %rbp |
Jan Beulich | 7effaa8 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 203 | CFI_RESTORE rbp |
Ingo Molnar | 0c2bd5a | 2008-01-30 13:32:49 +0100 | [diff] [blame] | 204 | movq 5*8(%rsp), %rbx |
Jan Beulich | 7effaa8 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 205 | CFI_RESTORE rbx |
Ingo Molnar | 0c2bd5a | 2008-01-30 13:32:49 +0100 | [diff] [blame] | 206 | addq $REST_SKIP, %rsp |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | CFI_ADJUST_CFA_OFFSET -(REST_SKIP) |
| 208 | .endm |
Ingo Molnar | 0c2bd5a | 2008-01-30 13:32:49 +0100 | [diff] [blame] | 209 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | .macro SAVE_ALL |
| 211 | SAVE_ARGS |
| 212 | SAVE_REST |
| 213 | .endm |
Ingo Molnar | 0c2bd5a | 2008-01-30 13:32:49 +0100 | [diff] [blame] | 214 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | .macro RESTORE_ALL addskip=0 |
| 216 | RESTORE_REST |
Ingo Molnar | 0c2bd5a | 2008-01-30 13:32:49 +0100 | [diff] [blame] | 217 | RESTORE_ARGS 0, \addskip |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | .endm |
| 219 | |
| 220 | .macro icebp |
| 221 | .byte 0xf1 |
| 222 | .endm |