blob: 0fa67503391257d89d1b55016c07e97fba7fa667 [file] [log] [blame]
Ingo Molnar0c2bd5a2008-01-30 13:32:49 +01001/*
Ingo Molnar063f8912009-02-03 18:02:36 +01002
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
24For 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
David Howellsa1ce3922012-10-02 18:01:25 +010049#include <asm/dwarf2.h>
Ingo Molnar063f8912009-02-03 18:02:36 +010050
51/*
Tao Guo1b2b23d2012-09-26 04:28:22 -040052 * 64-bit system call stack frame layout defines and helpers,
53 * for assembly code:
Ingo Molnar0c2bd5a2008-01-30 13:32:49 +010054 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Tao Guo1b2b23d2012-09-26 04:28:22 -040056#define R15 0
57#define R14 8
58#define R13 16
59#define R12 24
60#define RBP 32
61#define RBX 40
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Ingo Molnar063f8912009-02-03 18:02:36 +010063/* arguments: interrupts/non tracing syscalls only save up to here: */
Tao Guo1b2b23d2012-09-26 04:28:22 -040064#define R11 48
65#define R10 56
66#define R9 64
67#define R8 72
68#define RAX 80
69#define RCX 88
70#define RDX 96
71#define RSI 104
72#define RDI 112
73#define ORIG_RAX 120 /* + error_code */
Ingo Molnar0c2bd5a2008-01-30 13:32:49 +010074/* end of arguments */
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Ingo Molnar063f8912009-02-03 18:02:36 +010076/* cpu exception frame or undefined in case of fast syscall: */
Tao Guo1b2b23d2012-09-26 04:28:22 -040077#define RIP 128
78#define CS 136
79#define EFLAGS 144
80#define RSP 152
81#define SS 160
Ingo Molnar0c2bd5a2008-01-30 13:32:49 +010082
83#define ARGOFFSET R11
84#define SWFRAME ORIG_RAX
85
Borislav Petkovcac0e0a2011-05-31 22:21:52 +020086 .macro SAVE_ARGS addskip=0, save_rcx=1, save_r891011=1
Ingo Molnar0c2bd5a2008-01-30 13:32:49 +010087 subq $9*8+\addskip, %rsp
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 CFI_ADJUST_CFA_OFFSET 9*8+\addskip
Borislav Petkova268fcf2011-05-31 22:21:51 +020089 movq_cfi rdi, 8*8
90 movq_cfi rsi, 7*8
91 movq_cfi rdx, 6*8
92
Borislav Petkovcac0e0a2011-05-31 22:21:52 +020093 .if \save_rcx
Borislav Petkova268fcf2011-05-31 22:21:51 +020094 movq_cfi rcx, 5*8
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 .endif
Borislav Petkova268fcf2011-05-31 22:21:51 +020096
97 movq_cfi rax, 4*8
98
Borislav Petkovcac0e0a2011-05-31 22:21:52 +020099 .if \save_r891011
Borislav Petkova268fcf2011-05-31 22:21:51 +0200100 movq_cfi r8, 3*8
101 movq_cfi r9, 2*8
102 movq_cfi r10, 1*8
103 movq_cfi r11, 0*8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 .endif
Borislav Petkova268fcf2011-05-31 22:21:51 +0200105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 .endm
107
Jan Beulich32342822010-10-19 14:52:26 +0100108#define ARG_SKIP (9*8)
Ingo Molnar0c2bd5a2008-01-30 13:32:49 +0100109
Borislav Petkov838feb42011-05-31 22:21:53 +0200110 .macro RESTORE_ARGS rstor_rax=1, addskip=0, rstor_rcx=1, rstor_r11=1, \
111 rstor_r8910=1, rstor_rdx=1
112 .if \rstor_r11
Borislav Petkova268fcf2011-05-31 22:21:51 +0200113 movq_cfi_restore 0*8, r11
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 .endif
Borislav Petkova268fcf2011-05-31 22:21:51 +0200115
Borislav Petkov838feb42011-05-31 22:21:53 +0200116 .if \rstor_r8910
Borislav Petkova268fcf2011-05-31 22:21:51 +0200117 movq_cfi_restore 1*8, r10
118 movq_cfi_restore 2*8, r9
119 movq_cfi_restore 3*8, r8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 .endif
Borislav Petkova268fcf2011-05-31 22:21:51 +0200121
Borislav Petkov838feb42011-05-31 22:21:53 +0200122 .if \rstor_rax
Borislav Petkova268fcf2011-05-31 22:21:51 +0200123 movq_cfi_restore 4*8, rax
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 .endif
Borislav Petkova268fcf2011-05-31 22:21:51 +0200125
Borislav Petkov838feb42011-05-31 22:21:53 +0200126 .if \rstor_rcx
Borislav Petkova268fcf2011-05-31 22:21:51 +0200127 movq_cfi_restore 5*8, rcx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 .endif
Borislav Petkova268fcf2011-05-31 22:21:51 +0200129
Borislav Petkov838feb42011-05-31 22:21:53 +0200130 .if \rstor_rdx
Borislav Petkova268fcf2011-05-31 22:21:51 +0200131 movq_cfi_restore 6*8, rdx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 .endif
Borislav Petkova268fcf2011-05-31 22:21:51 +0200133
134 movq_cfi_restore 7*8, rsi
135 movq_cfi_restore 8*8, rdi
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 .if ARG_SKIP+\addskip > 0
Ingo Molnar0c2bd5a2008-01-30 13:32:49 +0100138 addq $ARG_SKIP+\addskip, %rsp
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 CFI_ADJUST_CFA_OFFSET -(ARG_SKIP+\addskip)
140 .endif
Ingo Molnar0c2bd5a2008-01-30 13:32:49 +0100141 .endm
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Roland McGrathd4d67152008-07-09 02:38:07 -0700143 .macro LOAD_ARGS offset, skiprax=0
Ingo Molnar0c2bd5a2008-01-30 13:32:49 +0100144 movq \offset(%rsp), %r11
145 movq \offset+8(%rsp), %r10
146 movq \offset+16(%rsp), %r9
147 movq \offset+24(%rsp), %r8
148 movq \offset+40(%rsp), %rcx
149 movq \offset+48(%rsp), %rdx
150 movq \offset+56(%rsp), %rsi
151 movq \offset+64(%rsp), %rdi
Roland McGrathd4d67152008-07-09 02:38:07 -0700152 .if \skiprax
153 .else
Ingo Molnar0c2bd5a2008-01-30 13:32:49 +0100154 movq \offset+72(%rsp), %rax
Roland McGrathd4d67152008-07-09 02:38:07 -0700155 .endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 .endm
Ingo Molnar0c2bd5a2008-01-30 13:32:49 +0100157
Jan Beulich32342822010-10-19 14:52:26 +0100158#define REST_SKIP (6*8)
Ingo Molnar0c2bd5a2008-01-30 13:32:49 +0100159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 .macro SAVE_REST
Ingo Molnar0c2bd5a2008-01-30 13:32:49 +0100161 subq $REST_SKIP, %rsp
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 CFI_ADJUST_CFA_OFFSET REST_SKIP
Borislav Petkova268fcf2011-05-31 22:21:51 +0200163 movq_cfi rbx, 5*8
164 movq_cfi rbp, 4*8
165 movq_cfi r12, 3*8
166 movq_cfi r13, 2*8
167 movq_cfi r14, 1*8
168 movq_cfi r15, 0*8
Ingo Molnar0c2bd5a2008-01-30 13:32:49 +0100169 .endm
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 .macro RESTORE_REST
Borislav Petkova268fcf2011-05-31 22:21:51 +0200172 movq_cfi_restore 0*8, r15
173 movq_cfi_restore 1*8, r14
174 movq_cfi_restore 2*8, r13
175 movq_cfi_restore 3*8, r12
176 movq_cfi_restore 4*8, rbp
177 movq_cfi_restore 5*8, rbx
Ingo Molnar0c2bd5a2008-01-30 13:32:49 +0100178 addq $REST_SKIP, %rsp
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 CFI_ADJUST_CFA_OFFSET -(REST_SKIP)
180 .endm
Ingo Molnar0c2bd5a2008-01-30 13:32:49 +0100181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 .macro SAVE_ALL
183 SAVE_ARGS
184 SAVE_REST
185 .endm
Ingo Molnar0c2bd5a2008-01-30 13:32:49 +0100186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 .macro RESTORE_ALL addskip=0
188 RESTORE_REST
Borislav Petkov838feb42011-05-31 22:21:53 +0200189 RESTORE_ARGS 1, \addskip
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 .endm
191
192 .macro icebp
193 .byte 0xf1
194 .endm