Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/arch/x86_64/entry.S | |
3 | * | |
4 | * Copyright (C) 1991, 1992 Linus Torvalds | |
5 | * Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs | |
6 | * Copyright (C) 2000 Pavel Machek <pavel@suse.cz> | |
1da177e4 LT |
7 | */ |
8 | ||
9 | /* | |
10 | * entry.S contains the system-call and fault low-level handling routines. | |
11 | * | |
12 | * NOTE: This code handles signal-recognition, which happens every time | |
13 | * after an interrupt and after each system call. | |
0bd7b798 AH |
14 | * |
15 | * Normal syscalls and interrupts don't save a full stack frame, this is | |
1da177e4 | 16 | * only done for syscall tracing, signals or fork/exec et.al. |
0bd7b798 AH |
17 | * |
18 | * A note on terminology: | |
19 | * - top of stack: Architecture defined interrupt frame from SS to RIP | |
20 | * at the top of the kernel process stack. | |
1da177e4 | 21 | * - partial stack frame: partially saved registers upto R11. |
0bd7b798 | 22 | * - full stack frame: Like partial stack frame, but all register saved. |
2e91a17b AK |
23 | * |
24 | * Some macro usage: | |
25 | * - CFI macros are used to generate dwarf2 unwind information for better | |
26 | * backtraces. They don't change any code. | |
27 | * - SAVE_ALL/RESTORE_ALL - Save/restore all registers | |
28 | * - SAVE_ARGS/RESTORE_ARGS - Save/restore registers that C functions modify. | |
29 | * There are unfortunately lots of special cases where some registers | |
30 | * not touched. The macro is a big mess that should be cleaned up. | |
31 | * - SAVE_REST/RESTORE_REST - Handle the registers not saved by SAVE_ARGS. | |
32 | * Gives a full stack frame. | |
33 | * - ENTRY/END Define functions in the symbol table. | |
34 | * - FIXUP_TOP_OF_STACK/RESTORE_TOP_OF_STACK - Fix up the hardware stack | |
35 | * frame that is otherwise undefined after a SYSCALL | |
36 | * - TRACE_IRQ_* - Trace hard interrupt state for lock debugging. | |
37 | * - errorentry/paranoidentry/zeroentry - Define exception entry points. | |
1da177e4 LT |
38 | */ |
39 | ||
1da177e4 LT |
40 | #include <linux/linkage.h> |
41 | #include <asm/segment.h> | |
1da177e4 LT |
42 | #include <asm/cache.h> |
43 | #include <asm/errno.h> | |
44 | #include <asm/dwarf2.h> | |
45 | #include <asm/calling.h> | |
e2d5df93 | 46 | #include <asm/asm-offsets.h> |
1da177e4 LT |
47 | #include <asm/msr.h> |
48 | #include <asm/unistd.h> | |
49 | #include <asm/thread_info.h> | |
50 | #include <asm/hw_irq.h> | |
5f8efbb9 | 51 | #include <asm/page.h> |
2601e64d | 52 | #include <asm/irqflags.h> |
72fe4858 | 53 | #include <asm/paravirt.h> |
395a59d0 | 54 | #include <asm/ftrace.h> |
9939ddaf | 55 | #include <asm/percpu.h> |
1da177e4 | 56 | |
86a1c34a RM |
57 | /* Avoid __ASSEMBLER__'ifying <linux/audit.h> just for this. */ |
58 | #include <linux/elf-em.h> | |
59 | #define AUDIT_ARCH_X86_64 (EM_X86_64|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE) | |
60 | #define __AUDIT_ARCH_64BIT 0x80000000 | |
61 | #define __AUDIT_ARCH_LE 0x40000000 | |
62 | ||
1da177e4 | 63 | .code64 |
606576ce | 64 | #ifdef CONFIG_FUNCTION_TRACER |
d61f82d0 SR |
65 | #ifdef CONFIG_DYNAMIC_FTRACE |
66 | ENTRY(mcount) | |
d61f82d0 SR |
67 | retq |
68 | END(mcount) | |
69 | ||
70 | ENTRY(ftrace_caller) | |
60a7ecf4 SR |
71 | cmpl $0, function_trace_stop |
72 | jne ftrace_stub | |
d61f82d0 | 73 | |
d680fe44 | 74 | MCOUNT_SAVE_FRAME |
d61f82d0 SR |
75 | |
76 | movq 0x38(%rsp), %rdi | |
77 | movq 8(%rbp), %rsi | |
395a59d0 | 78 | subq $MCOUNT_INSN_SIZE, %rdi |
d61f82d0 SR |
79 | |
80 | .globl ftrace_call | |
81 | ftrace_call: | |
82 | call ftrace_stub | |
83 | ||
d680fe44 | 84 | MCOUNT_RESTORE_FRAME |
d61f82d0 | 85 | |
48d68b20 FW |
86 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
87 | .globl ftrace_graph_call | |
88 | ftrace_graph_call: | |
89 | jmp ftrace_stub | |
90 | #endif | |
d61f82d0 SR |
91 | |
92 | .globl ftrace_stub | |
93 | ftrace_stub: | |
94 | retq | |
95 | END(ftrace_caller) | |
96 | ||
97 | #else /* ! CONFIG_DYNAMIC_FTRACE */ | |
16444a8a | 98 | ENTRY(mcount) |
60a7ecf4 SR |
99 | cmpl $0, function_trace_stop |
100 | jne ftrace_stub | |
101 | ||
16444a8a ACM |
102 | cmpq $ftrace_stub, ftrace_trace_function |
103 | jnz trace | |
48d68b20 FW |
104 | |
105 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER | |
106 | cmpq $ftrace_stub, ftrace_graph_return | |
107 | jnz ftrace_graph_caller | |
e49dc19c SR |
108 | |
109 | cmpq $ftrace_graph_entry_stub, ftrace_graph_entry | |
110 | jnz ftrace_graph_caller | |
48d68b20 FW |
111 | #endif |
112 | ||
16444a8a ACM |
113 | .globl ftrace_stub |
114 | ftrace_stub: | |
115 | retq | |
116 | ||
117 | trace: | |
d680fe44 | 118 | MCOUNT_SAVE_FRAME |
16444a8a ACM |
119 | |
120 | movq 0x38(%rsp), %rdi | |
121 | movq 8(%rbp), %rsi | |
395a59d0 | 122 | subq $MCOUNT_INSN_SIZE, %rdi |
16444a8a ACM |
123 | |
124 | call *ftrace_trace_function | |
125 | ||
d680fe44 | 126 | MCOUNT_RESTORE_FRAME |
16444a8a ACM |
127 | |
128 | jmp ftrace_stub | |
129 | END(mcount) | |
d61f82d0 | 130 | #endif /* CONFIG_DYNAMIC_FTRACE */ |
606576ce | 131 | #endif /* CONFIG_FUNCTION_TRACER */ |
16444a8a | 132 | |
48d68b20 FW |
133 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
134 | ENTRY(ftrace_graph_caller) | |
135 | cmpl $0, function_trace_stop | |
136 | jne ftrace_stub | |
137 | ||
d680fe44 | 138 | MCOUNT_SAVE_FRAME |
48d68b20 FW |
139 | |
140 | leaq 8(%rbp), %rdi | |
141 | movq 0x38(%rsp), %rsi | |
bb4304c7 | 142 | subq $MCOUNT_INSN_SIZE, %rsi |
48d68b20 FW |
143 | |
144 | call prepare_ftrace_return | |
145 | ||
d680fe44 CG |
146 | MCOUNT_RESTORE_FRAME |
147 | ||
48d68b20 FW |
148 | retq |
149 | END(ftrace_graph_caller) | |
150 | ||
151 | ||
152 | .globl return_to_handler | |
153 | return_to_handler: | |
154 | subq $80, %rsp | |
155 | ||
16444a8a ACM |
156 | movq %rax, (%rsp) |
157 | movq %rcx, 8(%rsp) | |
158 | movq %rdx, 16(%rsp) | |
159 | movq %rsi, 24(%rsp) | |
160 | movq %rdi, 32(%rsp) | |
161 | movq %r8, 40(%rsp) | |
162 | movq %r9, 48(%rsp) | |
48d68b20 FW |
163 | movq %r10, 56(%rsp) |
164 | movq %r11, 64(%rsp) | |
16444a8a | 165 | |
48d68b20 | 166 | call ftrace_return_to_handler |
16444a8a | 167 | |
48d68b20 FW |
168 | movq %rax, 72(%rsp) |
169 | movq 64(%rsp), %r11 | |
170 | movq 56(%rsp), %r10 | |
16444a8a ACM |
171 | movq 48(%rsp), %r9 |
172 | movq 40(%rsp), %r8 | |
173 | movq 32(%rsp), %rdi | |
174 | movq 24(%rsp), %rsi | |
175 | movq 16(%rsp), %rdx | |
176 | movq 8(%rsp), %rcx | |
177 | movq (%rsp), %rax | |
48d68b20 FW |
178 | addq $72, %rsp |
179 | retq | |
180 | #endif | |
16444a8a | 181 | |
16444a8a | 182 | |
dc37db4d | 183 | #ifndef CONFIG_PREEMPT |
1da177e4 | 184 | #define retint_kernel retint_restore_args |
0bd7b798 | 185 | #endif |
2601e64d | 186 | |
72fe4858 | 187 | #ifdef CONFIG_PARAVIRT |
2be29982 | 188 | ENTRY(native_usergs_sysret64) |
72fe4858 GOC |
189 | swapgs |
190 | sysretq | |
b3baaa13 | 191 | ENDPROC(native_usergs_sysret64) |
72fe4858 GOC |
192 | #endif /* CONFIG_PARAVIRT */ |
193 | ||
2601e64d IM |
194 | |
195 | .macro TRACE_IRQS_IRETQ offset=ARGOFFSET | |
196 | #ifdef CONFIG_TRACE_IRQFLAGS | |
197 | bt $9,EFLAGS-\offset(%rsp) /* interrupts off? */ | |
198 | jnc 1f | |
199 | TRACE_IRQS_ON | |
200 | 1: | |
201 | #endif | |
202 | .endm | |
203 | ||
1da177e4 | 204 | /* |
0bd7b798 AH |
205 | * C code is not supposed to know about undefined top of stack. Every time |
206 | * a C function with an pt_regs argument is called from the SYSCALL based | |
1da177e4 LT |
207 | * fast path FIXUP_TOP_OF_STACK is needed. |
208 | * RESTORE_TOP_OF_STACK syncs the syscall state after any possible ptregs | |
209 | * manipulation. | |
0bd7b798 AH |
210 | */ |
211 | ||
212 | /* %rsp:at FRAMEEND */ | |
c002a1e6 | 213 | .macro FIXUP_TOP_OF_STACK tmp offset=0 |
3d1e42a7 | 214 | movq PER_CPU_VAR(old_rsp),\tmp |
c002a1e6 AH |
215 | movq \tmp,RSP+\offset(%rsp) |
216 | movq $__USER_DS,SS+\offset(%rsp) | |
217 | movq $__USER_CS,CS+\offset(%rsp) | |
218 | movq $-1,RCX+\offset(%rsp) | |
219 | movq R11+\offset(%rsp),\tmp /* get eflags */ | |
220 | movq \tmp,EFLAGS+\offset(%rsp) | |
1da177e4 LT |
221 | .endm |
222 | ||
c002a1e6 AH |
223 | .macro RESTORE_TOP_OF_STACK tmp offset=0 |
224 | movq RSP+\offset(%rsp),\tmp | |
3d1e42a7 | 225 | movq \tmp,PER_CPU_VAR(old_rsp) |
c002a1e6 AH |
226 | movq EFLAGS+\offset(%rsp),\tmp |
227 | movq \tmp,R11+\offset(%rsp) | |
1da177e4 LT |
228 | .endm |
229 | ||
230 | .macro FAKE_STACK_FRAME child_rip | |
231 | /* push in order ss, rsp, eflags, cs, rip */ | |
3829ee6b | 232 | xorl %eax, %eax |
e04e0a63 | 233 | pushq $__KERNEL_DS /* ss */ |
1da177e4 | 234 | CFI_ADJUST_CFA_OFFSET 8 |
7effaa88 | 235 | /*CFI_REL_OFFSET ss,0*/ |
1da177e4 LT |
236 | pushq %rax /* rsp */ |
237 | CFI_ADJUST_CFA_OFFSET 8 | |
7effaa88 | 238 | CFI_REL_OFFSET rsp,0 |
33454539 | 239 | pushq $X86_EFLAGS_IF /* eflags - interrupts on */ |
1da177e4 | 240 | CFI_ADJUST_CFA_OFFSET 8 |
7effaa88 | 241 | /*CFI_REL_OFFSET rflags,0*/ |
1da177e4 LT |
242 | pushq $__KERNEL_CS /* cs */ |
243 | CFI_ADJUST_CFA_OFFSET 8 | |
7effaa88 | 244 | /*CFI_REL_OFFSET cs,0*/ |
1da177e4 LT |
245 | pushq \child_rip /* rip */ |
246 | CFI_ADJUST_CFA_OFFSET 8 | |
7effaa88 | 247 | CFI_REL_OFFSET rip,0 |
1da177e4 LT |
248 | pushq %rax /* orig rax */ |
249 | CFI_ADJUST_CFA_OFFSET 8 | |
250 | .endm | |
251 | ||
252 | .macro UNFAKE_STACK_FRAME | |
253 | addq $8*6, %rsp | |
254 | CFI_ADJUST_CFA_OFFSET -(6*8) | |
255 | .endm | |
256 | ||
dcd072e2 AH |
257 | /* |
258 | * initial frame state for interrupts (and exceptions without error code) | |
259 | */ | |
260 | .macro EMPTY_FRAME start=1 offset=0 | |
7effaa88 | 261 | .if \start |
dcd072e2 | 262 | CFI_STARTPROC simple |
adf14236 | 263 | CFI_SIGNAL_FRAME |
dcd072e2 | 264 | CFI_DEF_CFA rsp,8+\offset |
7effaa88 | 265 | .else |
dcd072e2 | 266 | CFI_DEF_CFA_OFFSET 8+\offset |
7effaa88 | 267 | .endif |
1da177e4 | 268 | .endm |
d99015b1 AH |
269 | |
270 | /* | |
dcd072e2 | 271 | * initial frame state for interrupts (and exceptions without error code) |
d99015b1 | 272 | */ |
dcd072e2 | 273 | .macro INTR_FRAME start=1 offset=0 |
e8a0e276 IM |
274 | EMPTY_FRAME \start, SS+8+\offset-RIP |
275 | /*CFI_REL_OFFSET ss, SS+\offset-RIP*/ | |
276 | CFI_REL_OFFSET rsp, RSP+\offset-RIP | |
277 | /*CFI_REL_OFFSET rflags, EFLAGS+\offset-RIP*/ | |
278 | /*CFI_REL_OFFSET cs, CS+\offset-RIP*/ | |
279 | CFI_REL_OFFSET rip, RIP+\offset-RIP | |
d99015b1 AH |
280 | .endm |
281 | ||
d99015b1 AH |
282 | /* |
283 | * initial frame state for exceptions with error code (and interrupts | |
284 | * with vector already pushed) | |
285 | */ | |
dcd072e2 | 286 | .macro XCPT_FRAME start=1 offset=0 |
e8a0e276 | 287 | INTR_FRAME \start, RIP+\offset-ORIG_RAX |
dcd072e2 AH |
288 | /*CFI_REL_OFFSET orig_rax, ORIG_RAX-ORIG_RAX*/ |
289 | .endm | |
290 | ||
291 | /* | |
292 | * frame that enables calling into C. | |
293 | */ | |
294 | .macro PARTIAL_FRAME start=1 offset=0 | |
e8a0e276 IM |
295 | XCPT_FRAME \start, ORIG_RAX+\offset-ARGOFFSET |
296 | CFI_REL_OFFSET rdi, RDI+\offset-ARGOFFSET | |
297 | CFI_REL_OFFSET rsi, RSI+\offset-ARGOFFSET | |
298 | CFI_REL_OFFSET rdx, RDX+\offset-ARGOFFSET | |
299 | CFI_REL_OFFSET rcx, RCX+\offset-ARGOFFSET | |
300 | CFI_REL_OFFSET rax, RAX+\offset-ARGOFFSET | |
301 | CFI_REL_OFFSET r8, R8+\offset-ARGOFFSET | |
302 | CFI_REL_OFFSET r9, R9+\offset-ARGOFFSET | |
303 | CFI_REL_OFFSET r10, R10+\offset-ARGOFFSET | |
304 | CFI_REL_OFFSET r11, R11+\offset-ARGOFFSET | |
dcd072e2 AH |
305 | .endm |
306 | ||
307 | /* | |
308 | * frame that enables passing a complete pt_regs to a C function. | |
309 | */ | |
310 | .macro DEFAULT_FRAME start=1 offset=0 | |
e8a0e276 | 311 | PARTIAL_FRAME \start, R11+\offset-R15 |
dcd072e2 AH |
312 | CFI_REL_OFFSET rbx, RBX+\offset |
313 | CFI_REL_OFFSET rbp, RBP+\offset | |
314 | CFI_REL_OFFSET r12, R12+\offset | |
315 | CFI_REL_OFFSET r13, R13+\offset | |
316 | CFI_REL_OFFSET r14, R14+\offset | |
317 | CFI_REL_OFFSET r15, R15+\offset | |
318 | .endm | |
d99015b1 AH |
319 | |
320 | /* save partial stack frame */ | |
321 | ENTRY(save_args) | |
322 | XCPT_FRAME | |
323 | cld | |
14ae22ba IM |
324 | movq_cfi rdi, RDI+16-ARGOFFSET |
325 | movq_cfi rsi, RSI+16-ARGOFFSET | |
326 | movq_cfi rdx, RDX+16-ARGOFFSET | |
327 | movq_cfi rcx, RCX+16-ARGOFFSET | |
328 | movq_cfi rax, RAX+16-ARGOFFSET | |
329 | movq_cfi r8, R8+16-ARGOFFSET | |
330 | movq_cfi r9, R9+16-ARGOFFSET | |
331 | movq_cfi r10, R10+16-ARGOFFSET | |
332 | movq_cfi r11, R11+16-ARGOFFSET | |
333 | ||
d99015b1 | 334 | leaq -ARGOFFSET+16(%rsp),%rdi /* arg1 for handler */ |
14ae22ba | 335 | movq_cfi rbp, 8 /* push %rbp */ |
d99015b1 AH |
336 | leaq 8(%rsp), %rbp /* mov %rsp, %ebp */ |
337 | testl $3, CS(%rdi) | |
338 | je 1f | |
339 | SWAPGS | |
340 | /* | |
56895530 | 341 | * irq_count is used to check if a CPU is already on an interrupt stack |
d99015b1 AH |
342 | * or not. While this is essentially redundant with preempt_count it is |
343 | * a little cheaper to use a separate counter in the PDA (short of | |
344 | * moving irq_enter into assembly, which would be too much work) | |
345 | */ | |
56895530 | 346 | 1: incl PER_CPU_VAR(irq_count) |
d99015b1 | 347 | jne 2f |
14ae22ba | 348 | popq_cfi %rax /* move return address... */ |
26f80bd6 | 349 | mov PER_CPU_VAR(irq_stack_ptr),%rsp |
dcd072e2 | 350 | EMPTY_FRAME 0 |
a67798cd | 351 | pushq_cfi %rbp /* backlink for unwinder */ |
14ae22ba | 352 | pushq_cfi %rax /* ... to the new stack */ |
d99015b1 AH |
353 | /* |
354 | * We entered an interrupt context - irqs are off: | |
355 | */ | |
356 | 2: TRACE_IRQS_OFF | |
357 | ret | |
358 | CFI_ENDPROC | |
359 | END(save_args) | |
360 | ||
c002a1e6 AH |
361 | ENTRY(save_rest) |
362 | PARTIAL_FRAME 1 REST_SKIP+8 | |
363 | movq 5*8+16(%rsp), %r11 /* save return address */ | |
364 | movq_cfi rbx, RBX+16 | |
365 | movq_cfi rbp, RBP+16 | |
366 | movq_cfi r12, R12+16 | |
367 | movq_cfi r13, R13+16 | |
368 | movq_cfi r14, R14+16 | |
369 | movq_cfi r15, R15+16 | |
370 | movq %r11, 8(%rsp) /* return address */ | |
371 | FIXUP_TOP_OF_STACK %r11, 16 | |
372 | ret | |
373 | CFI_ENDPROC | |
374 | END(save_rest) | |
375 | ||
e2f6bc25 AH |
376 | /* save complete stack frame */ |
377 | ENTRY(save_paranoid) | |
378 | XCPT_FRAME 1 RDI+8 | |
379 | cld | |
380 | movq_cfi rdi, RDI+8 | |
381 | movq_cfi rsi, RSI+8 | |
382 | movq_cfi rdx, RDX+8 | |
383 | movq_cfi rcx, RCX+8 | |
384 | movq_cfi rax, RAX+8 | |
385 | movq_cfi r8, R8+8 | |
386 | movq_cfi r9, R9+8 | |
387 | movq_cfi r10, R10+8 | |
388 | movq_cfi r11, R11+8 | |
389 | movq_cfi rbx, RBX+8 | |
390 | movq_cfi rbp, RBP+8 | |
391 | movq_cfi r12, R12+8 | |
392 | movq_cfi r13, R13+8 | |
393 | movq_cfi r14, R14+8 | |
394 | movq_cfi r15, R15+8 | |
395 | movl $1,%ebx | |
396 | movl $MSR_GS_BASE,%ecx | |
397 | rdmsr | |
398 | testl %edx,%edx | |
399 | js 1f /* negative -> in kernel */ | |
400 | SWAPGS | |
401 | xorl %ebx,%ebx | |
402 | 1: ret | |
403 | CFI_ENDPROC | |
404 | END(save_paranoid) | |
405 | ||
1da177e4 | 406 | /* |
5b3eec0c IM |
407 | * A newly forked process directly context switches into this address. |
408 | * | |
409 | * rdi: prev task we switched from | |
0bd7b798 | 410 | */ |
1da177e4 | 411 | ENTRY(ret_from_fork) |
dcd072e2 | 412 | DEFAULT_FRAME |
5b3eec0c | 413 | |
7106a5ab BL |
414 | LOCK ; btr $TIF_FORK,TI_flags(%r8) |
415 | ||
658fdbef | 416 | push kernel_eflags(%rip) |
e0a5a5d9 | 417 | CFI_ADJUST_CFA_OFFSET 8 |
5b3eec0c | 418 | popf # reset kernel eflags |
e0a5a5d9 | 419 | CFI_ADJUST_CFA_OFFSET -8 |
5b3eec0c IM |
420 | |
421 | call schedule_tail # rdi: 'prev' task parameter | |
422 | ||
1da177e4 | 423 | GET_THREAD_INFO(%rcx) |
5b3eec0c | 424 | |
1cbd8b3f | 425 | CFI_REMEMBER_STATE |
1da177e4 | 426 | RESTORE_REST |
5b3eec0c IM |
427 | |
428 | testl $3, CS-ARGOFFSET(%rsp) # from kernel_thread? | |
1da177e4 | 429 | je int_ret_from_sys_call |
5b3eec0c IM |
430 | |
431 | testl $_TIF_IA32, TI_flags(%rcx) # 32-bit compat task needs IRET | |
1da177e4 | 432 | jnz int_ret_from_sys_call |
5b3eec0c | 433 | |
c002a1e6 | 434 | RESTORE_TOP_OF_STACK %rdi, -ARGOFFSET |
5b3eec0c IM |
435 | jmp ret_from_sys_call # go to the SYSRET fastpath |
436 | ||
1cbd8b3f | 437 | CFI_RESTORE_STATE |
1da177e4 | 438 | CFI_ENDPROC |
4b787e0b | 439 | END(ret_from_fork) |
1da177e4 LT |
440 | |
441 | /* | |
442 | * System call entry. Upto 6 arguments in registers are supported. | |
443 | * | |
444 | * SYSCALL does not save anything on the stack and does not change the | |
445 | * stack pointer. | |
446 | */ | |
0bd7b798 | 447 | |
1da177e4 | 448 | /* |
0bd7b798 | 449 | * Register setup: |
1da177e4 LT |
450 | * rax system call number |
451 | * rdi arg0 | |
0bd7b798 | 452 | * rcx return address for syscall/sysret, C arg3 |
1da177e4 | 453 | * rsi arg1 |
0bd7b798 | 454 | * rdx arg2 |
1da177e4 LT |
455 | * r10 arg3 (--> moved to rcx for C) |
456 | * r8 arg4 | |
457 | * r9 arg5 | |
458 | * r11 eflags for syscall/sysret, temporary for C | |
0bd7b798 AH |
459 | * r12-r15,rbp,rbx saved by C code, not touched. |
460 | * | |
1da177e4 LT |
461 | * Interrupts are off on entry. |
462 | * Only called from user space. | |
463 | * | |
464 | * XXX if we had a free scratch register we could save the RSP into the stack frame | |
465 | * and report it properly in ps. Unfortunately we haven't. | |
7bf36bbc AK |
466 | * |
467 | * When user can change the frames always force IRET. That is because | |
468 | * it deals with uncanonical addresses better. SYSRET has trouble | |
469 | * with them due to bugs in both AMD and Intel CPUs. | |
0bd7b798 | 470 | */ |
1da177e4 LT |
471 | |
472 | ENTRY(system_call) | |
7effaa88 | 473 | CFI_STARTPROC simple |
adf14236 | 474 | CFI_SIGNAL_FRAME |
9af45651 | 475 | CFI_DEF_CFA rsp,KERNEL_STACK_OFFSET |
7effaa88 JB |
476 | CFI_REGISTER rip,rcx |
477 | /*CFI_REGISTER rflags,r11*/ | |
72fe4858 GOC |
478 | SWAPGS_UNSAFE_STACK |
479 | /* | |
480 | * A hypervisor implementation might want to use a label | |
481 | * after the swapgs, so that it can do the swapgs | |
482 | * for the guest and jump here on syscall. | |
483 | */ | |
484 | ENTRY(system_call_after_swapgs) | |
485 | ||
3d1e42a7 | 486 | movq %rsp,PER_CPU_VAR(old_rsp) |
9af45651 | 487 | movq PER_CPU_VAR(kernel_stack),%rsp |
2601e64d IM |
488 | /* |
489 | * No need to follow this irqs off/on section - it's straight | |
490 | * and short: | |
491 | */ | |
72fe4858 | 492 | ENABLE_INTERRUPTS(CLBR_NONE) |
1da177e4 | 493 | SAVE_ARGS 8,1 |
0bd7b798 | 494 | movq %rax,ORIG_RAX-ARGOFFSET(%rsp) |
7effaa88 JB |
495 | movq %rcx,RIP-ARGOFFSET(%rsp) |
496 | CFI_REL_OFFSET rip,RIP-ARGOFFSET | |
1da177e4 | 497 | GET_THREAD_INFO(%rcx) |
d4d67150 | 498 | testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags(%rcx) |
1da177e4 | 499 | jnz tracesys |
86a1c34a | 500 | system_call_fastpath: |
1da177e4 LT |
501 | cmpq $__NR_syscall_max,%rax |
502 | ja badsys | |
503 | movq %r10,%rcx | |
504 | call *sys_call_table(,%rax,8) # XXX: rip relative | |
505 | movq %rax,RAX-ARGOFFSET(%rsp) | |
506 | /* | |
507 | * Syscall return path ending with SYSRET (fast path) | |
0bd7b798 AH |
508 | * Has incomplete stack frame and undefined top of stack. |
509 | */ | |
1da177e4 | 510 | ret_from_sys_call: |
11b854b2 | 511 | movl $_TIF_ALLWORK_MASK,%edi |
1da177e4 | 512 | /* edi: flagmask */ |
0bd7b798 | 513 | sysret_check: |
10cd706d | 514 | LOCKDEP_SYS_EXIT |
1da177e4 | 515 | GET_THREAD_INFO(%rcx) |
72fe4858 | 516 | DISABLE_INTERRUPTS(CLBR_NONE) |
2601e64d | 517 | TRACE_IRQS_OFF |
26ccb8a7 | 518 | movl TI_flags(%rcx),%edx |
1da177e4 | 519 | andl %edi,%edx |
0bd7b798 | 520 | jnz sysret_careful |
bcddc015 | 521 | CFI_REMEMBER_STATE |
2601e64d IM |
522 | /* |
523 | * sysretq will re-enable interrupts: | |
524 | */ | |
525 | TRACE_IRQS_ON | |
1da177e4 | 526 | movq RIP-ARGOFFSET(%rsp),%rcx |
7effaa88 | 527 | CFI_REGISTER rip,rcx |
1da177e4 | 528 | RESTORE_ARGS 0,-ARG_SKIP,1 |
7effaa88 | 529 | /*CFI_REGISTER rflags,r11*/ |
3d1e42a7 | 530 | movq PER_CPU_VAR(old_rsp), %rsp |
2be29982 | 531 | USERGS_SYSRET64 |
1da177e4 | 532 | |
bcddc015 | 533 | CFI_RESTORE_STATE |
1da177e4 | 534 | /* Handle reschedules */ |
0bd7b798 | 535 | /* edx: work, edi: workmask */ |
1da177e4 LT |
536 | sysret_careful: |
537 | bt $TIF_NEED_RESCHED,%edx | |
538 | jnc sysret_signal | |
2601e64d | 539 | TRACE_IRQS_ON |
72fe4858 | 540 | ENABLE_INTERRUPTS(CLBR_NONE) |
1da177e4 | 541 | pushq %rdi |
7effaa88 | 542 | CFI_ADJUST_CFA_OFFSET 8 |
1da177e4 LT |
543 | call schedule |
544 | popq %rdi | |
7effaa88 | 545 | CFI_ADJUST_CFA_OFFSET -8 |
1da177e4 LT |
546 | jmp sysret_check |
547 | ||
0bd7b798 | 548 | /* Handle a signal */ |
1da177e4 | 549 | sysret_signal: |
2601e64d | 550 | TRACE_IRQS_ON |
72fe4858 | 551 | ENABLE_INTERRUPTS(CLBR_NONE) |
86a1c34a RM |
552 | #ifdef CONFIG_AUDITSYSCALL |
553 | bt $TIF_SYSCALL_AUDIT,%edx | |
554 | jc sysret_audit | |
555 | #endif | |
10ffdbb8 | 556 | /* edx: work flags (arg3) */ |
1da177e4 LT |
557 | leaq -ARGOFFSET(%rsp),%rdi # &pt_regs -> arg1 |
558 | xorl %esi,%esi # oldset -> arg2 | |
c8108411 AH |
559 | SAVE_REST |
560 | FIXUP_TOP_OF_STACK %r11 | |
561 | call do_notify_resume | |
562 | RESTORE_TOP_OF_STACK %r11 | |
563 | RESTORE_REST | |
15e8f348 | 564 | movl $_TIF_WORK_MASK,%edi |
7bf36bbc AK |
565 | /* Use IRET because user could have changed frame. This |
566 | works because ptregscall_common has called FIXUP_TOP_OF_STACK. */ | |
72fe4858 | 567 | DISABLE_INTERRUPTS(CLBR_NONE) |
2601e64d | 568 | TRACE_IRQS_OFF |
7bf36bbc | 569 | jmp int_with_check |
0bd7b798 | 570 | |
7effaa88 JB |
571 | badsys: |
572 | movq $-ENOSYS,RAX-ARGOFFSET(%rsp) | |
573 | jmp ret_from_sys_call | |
574 | ||
86a1c34a RM |
575 | #ifdef CONFIG_AUDITSYSCALL |
576 | /* | |
577 | * Fast path for syscall audit without full syscall trace. | |
578 | * We just call audit_syscall_entry() directly, and then | |
579 | * jump back to the normal fast path. | |
580 | */ | |
581 | auditsys: | |
582 | movq %r10,%r9 /* 6th arg: 4th syscall arg */ | |
583 | movq %rdx,%r8 /* 5th arg: 3rd syscall arg */ | |
584 | movq %rsi,%rcx /* 4th arg: 2nd syscall arg */ | |
585 | movq %rdi,%rdx /* 3rd arg: 1st syscall arg */ | |
586 | movq %rax,%rsi /* 2nd arg: syscall number */ | |
587 | movl $AUDIT_ARCH_X86_64,%edi /* 1st arg: audit arch */ | |
588 | call audit_syscall_entry | |
589 | LOAD_ARGS 0 /* reload call-clobbered registers */ | |
590 | jmp system_call_fastpath | |
591 | ||
592 | /* | |
593 | * Return fast path for syscall audit. Call audit_syscall_exit() | |
594 | * directly and then jump back to the fast path with TIF_SYSCALL_AUDIT | |
595 | * masked off. | |
596 | */ | |
597 | sysret_audit: | |
598 | movq %rax,%rsi /* second arg, syscall return value */ | |
599 | cmpq $0,%rax /* is it < 0? */ | |
600 | setl %al /* 1 if so, 0 if not */ | |
601 | movzbl %al,%edi /* zero-extend that into %edi */ | |
602 | inc %edi /* first arg, 0->1(AUDITSC_SUCCESS), 1->2(AUDITSC_FAILURE) */ | |
603 | call audit_syscall_exit | |
604 | movl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT),%edi | |
605 | jmp sysret_check | |
606 | #endif /* CONFIG_AUDITSYSCALL */ | |
607 | ||
1da177e4 | 608 | /* Do syscall tracing */ |
0bd7b798 | 609 | tracesys: |
86a1c34a RM |
610 | #ifdef CONFIG_AUDITSYSCALL |
611 | testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT),TI_flags(%rcx) | |
612 | jz auditsys | |
613 | #endif | |
1da177e4 | 614 | SAVE_REST |
a31f8dd7 | 615 | movq $-ENOSYS,RAX(%rsp) /* ptrace can change this for a bad syscall */ |
1da177e4 LT |
616 | FIXUP_TOP_OF_STACK %rdi |
617 | movq %rsp,%rdi | |
618 | call syscall_trace_enter | |
d4d67150 RM |
619 | /* |
620 | * Reload arg registers from stack in case ptrace changed them. | |
621 | * We don't reload %rax because syscall_trace_enter() returned | |
622 | * the value it wants us to use in the table lookup. | |
623 | */ | |
624 | LOAD_ARGS ARGOFFSET, 1 | |
1da177e4 LT |
625 | RESTORE_REST |
626 | cmpq $__NR_syscall_max,%rax | |
a31f8dd7 | 627 | ja int_ret_from_sys_call /* RAX(%rsp) set to -ENOSYS above */ |
1da177e4 LT |
628 | movq %r10,%rcx /* fixup for C */ |
629 | call *sys_call_table(,%rax,8) | |
a31f8dd7 | 630 | movq %rax,RAX-ARGOFFSET(%rsp) |
7bf36bbc | 631 | /* Use IRET because user could have changed frame */ |
0bd7b798 AH |
632 | |
633 | /* | |
1da177e4 LT |
634 | * Syscall return path ending with IRET. |
635 | * Has correct top of stack, but partial stack frame. | |
bcddc015 JB |
636 | */ |
637 | .globl int_ret_from_sys_call | |
5cbf1565 | 638 | .globl int_with_check |
bcddc015 | 639 | int_ret_from_sys_call: |
72fe4858 | 640 | DISABLE_INTERRUPTS(CLBR_NONE) |
2601e64d | 641 | TRACE_IRQS_OFF |
1da177e4 LT |
642 | testl $3,CS-ARGOFFSET(%rsp) |
643 | je retint_restore_args | |
644 | movl $_TIF_ALLWORK_MASK,%edi | |
645 | /* edi: mask to check */ | |
646 | int_with_check: | |
10cd706d | 647 | LOCKDEP_SYS_EXIT_IRQ |
1da177e4 | 648 | GET_THREAD_INFO(%rcx) |
26ccb8a7 | 649 | movl TI_flags(%rcx),%edx |
1da177e4 LT |
650 | andl %edi,%edx |
651 | jnz int_careful | |
26ccb8a7 | 652 | andl $~TS_COMPAT,TI_status(%rcx) |
1da177e4 LT |
653 | jmp retint_swapgs |
654 | ||
655 | /* Either reschedule or signal or syscall exit tracking needed. */ | |
656 | /* First do a reschedule test. */ | |
657 | /* edx: work, edi: workmask */ | |
658 | int_careful: | |
659 | bt $TIF_NEED_RESCHED,%edx | |
660 | jnc int_very_careful | |
2601e64d | 661 | TRACE_IRQS_ON |
72fe4858 | 662 | ENABLE_INTERRUPTS(CLBR_NONE) |
1da177e4 | 663 | pushq %rdi |
7effaa88 | 664 | CFI_ADJUST_CFA_OFFSET 8 |
1da177e4 LT |
665 | call schedule |
666 | popq %rdi | |
7effaa88 | 667 | CFI_ADJUST_CFA_OFFSET -8 |
72fe4858 | 668 | DISABLE_INTERRUPTS(CLBR_NONE) |
2601e64d | 669 | TRACE_IRQS_OFF |
1da177e4 LT |
670 | jmp int_with_check |
671 | ||
672 | /* handle signals and tracing -- both require a full stack frame */ | |
673 | int_very_careful: | |
2601e64d | 674 | TRACE_IRQS_ON |
72fe4858 | 675 | ENABLE_INTERRUPTS(CLBR_NONE) |
1da177e4 | 676 | SAVE_REST |
0bd7b798 | 677 | /* Check for syscall exit trace */ |
d4d67150 | 678 | testl $_TIF_WORK_SYSCALL_EXIT,%edx |
1da177e4 LT |
679 | jz int_signal |
680 | pushq %rdi | |
7effaa88 | 681 | CFI_ADJUST_CFA_OFFSET 8 |
0bd7b798 | 682 | leaq 8(%rsp),%rdi # &ptregs -> arg1 |
1da177e4 LT |
683 | call syscall_trace_leave |
684 | popq %rdi | |
7effaa88 | 685 | CFI_ADJUST_CFA_OFFSET -8 |
d4d67150 | 686 | andl $~(_TIF_WORK_SYSCALL_EXIT|_TIF_SYSCALL_EMU),%edi |
1da177e4 | 687 | jmp int_restore_rest |
0bd7b798 | 688 | |
1da177e4 | 689 | int_signal: |
8f4d37ec | 690 | testl $_TIF_DO_NOTIFY_MASK,%edx |
1da177e4 LT |
691 | jz 1f |
692 | movq %rsp,%rdi # &ptregs -> arg1 | |
693 | xorl %esi,%esi # oldset -> arg2 | |
694 | call do_notify_resume | |
eca91e78 | 695 | 1: movl $_TIF_WORK_MASK,%edi |
1da177e4 LT |
696 | int_restore_rest: |
697 | RESTORE_REST | |
72fe4858 | 698 | DISABLE_INTERRUPTS(CLBR_NONE) |
2601e64d | 699 | TRACE_IRQS_OFF |
1da177e4 LT |
700 | jmp int_with_check |
701 | CFI_ENDPROC | |
bcddc015 | 702 | END(system_call) |
0bd7b798 AH |
703 | |
704 | /* | |
1da177e4 | 705 | * Certain special system calls that need to save a complete full stack frame. |
0bd7b798 | 706 | */ |
1da177e4 | 707 | .macro PTREGSCALL label,func,arg |
c002a1e6 AH |
708 | ENTRY(\label) |
709 | PARTIAL_FRAME 1 8 /* offset 8: return address */ | |
710 | subq $REST_SKIP, %rsp | |
711 | CFI_ADJUST_CFA_OFFSET REST_SKIP | |
712 | call save_rest | |
713 | DEFAULT_FRAME 0 8 /* offset 8: return address */ | |
714 | leaq 8(%rsp), \arg /* pt_regs pointer */ | |
715 | call \func | |
716 | jmp ptregscall_common | |
717 | CFI_ENDPROC | |
4b787e0b | 718 | END(\label) |
1da177e4 LT |
719 | .endm |
720 | ||
721 | PTREGSCALL stub_clone, sys_clone, %r8 | |
722 | PTREGSCALL stub_fork, sys_fork, %rdi | |
723 | PTREGSCALL stub_vfork, sys_vfork, %rdi | |
1da177e4 LT |
724 | PTREGSCALL stub_sigaltstack, sys_sigaltstack, %rdx |
725 | PTREGSCALL stub_iopl, sys_iopl, %rsi | |
726 | ||
727 | ENTRY(ptregscall_common) | |
c002a1e6 AH |
728 | DEFAULT_FRAME 1 8 /* offset 8: return address */ |
729 | RESTORE_TOP_OF_STACK %r11, 8 | |
730 | movq_cfi_restore R15+8, r15 | |
731 | movq_cfi_restore R14+8, r14 | |
732 | movq_cfi_restore R13+8, r13 | |
733 | movq_cfi_restore R12+8, r12 | |
734 | movq_cfi_restore RBP+8, rbp | |
735 | movq_cfi_restore RBX+8, rbx | |
736 | ret $REST_SKIP /* pop extended registers */ | |
1da177e4 | 737 | CFI_ENDPROC |
4b787e0b | 738 | END(ptregscall_common) |
0bd7b798 | 739 | |
1da177e4 LT |
740 | ENTRY(stub_execve) |
741 | CFI_STARTPROC | |
742 | popq %r11 | |
7effaa88 JB |
743 | CFI_ADJUST_CFA_OFFSET -8 |
744 | CFI_REGISTER rip, r11 | |
1da177e4 | 745 | SAVE_REST |
1da177e4 | 746 | FIXUP_TOP_OF_STACK %r11 |
5d119b2c | 747 | movq %rsp, %rcx |
1da177e4 | 748 | call sys_execve |
1da177e4 | 749 | RESTORE_TOP_OF_STACK %r11 |
1da177e4 LT |
750 | movq %rax,RAX(%rsp) |
751 | RESTORE_REST | |
752 | jmp int_ret_from_sys_call | |
753 | CFI_ENDPROC | |
4b787e0b | 754 | END(stub_execve) |
0bd7b798 | 755 | |
1da177e4 LT |
756 | /* |
757 | * sigreturn is special because it needs to restore all registers on return. | |
758 | * This cannot be done with SYSRET, so use the IRET return path instead. | |
0bd7b798 | 759 | */ |
1da177e4 LT |
760 | ENTRY(stub_rt_sigreturn) |
761 | CFI_STARTPROC | |
7effaa88 JB |
762 | addq $8, %rsp |
763 | CFI_ADJUST_CFA_OFFSET -8 | |
1da177e4 LT |
764 | SAVE_REST |
765 | movq %rsp,%rdi | |
766 | FIXUP_TOP_OF_STACK %r11 | |
767 | call sys_rt_sigreturn | |
768 | movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer | |
769 | RESTORE_REST | |
770 | jmp int_ret_from_sys_call | |
771 | CFI_ENDPROC | |
4b787e0b | 772 | END(stub_rt_sigreturn) |
1da177e4 | 773 | |
939b7871 PA |
774 | /* |
775 | * Build the entry stubs and pointer table with some assembler magic. | |
776 | * We pack 7 stubs into a single 32-byte chunk, which will fit in a | |
777 | * single cache line on all modern x86 implementations. | |
778 | */ | |
779 | .section .init.rodata,"a" | |
780 | ENTRY(interrupt) | |
781 | .text | |
782 | .p2align 5 | |
783 | .p2align CONFIG_X86_L1_CACHE_SHIFT | |
784 | ENTRY(irq_entries_start) | |
785 | INTR_FRAME | |
786 | vector=FIRST_EXTERNAL_VECTOR | |
787 | .rept (NR_VECTORS-FIRST_EXTERNAL_VECTOR+6)/7 | |
788 | .balign 32 | |
789 | .rept 7 | |
790 | .if vector < NR_VECTORS | |
8665596e | 791 | .if vector <> FIRST_EXTERNAL_VECTOR |
939b7871 PA |
792 | CFI_ADJUST_CFA_OFFSET -8 |
793 | .endif | |
794 | 1: pushq $(~vector+0x80) /* Note: always in signed byte range */ | |
795 | CFI_ADJUST_CFA_OFFSET 8 | |
8665596e | 796 | .if ((vector-FIRST_EXTERNAL_VECTOR)%7) <> 6 |
939b7871 PA |
797 | jmp 2f |
798 | .endif | |
799 | .previous | |
800 | .quad 1b | |
801 | .text | |
802 | vector=vector+1 | |
803 | .endif | |
804 | .endr | |
805 | 2: jmp common_interrupt | |
806 | .endr | |
807 | CFI_ENDPROC | |
808 | END(irq_entries_start) | |
809 | ||
810 | .previous | |
811 | END(interrupt) | |
812 | .previous | |
813 | ||
d99015b1 | 814 | /* |
1da177e4 LT |
815 | * Interrupt entry/exit. |
816 | * | |
817 | * Interrupt entry points save only callee clobbered registers in fast path. | |
d99015b1 AH |
818 | * |
819 | * Entry runs with interrupts off. | |
820 | */ | |
1da177e4 | 821 | |
722024db | 822 | /* 0(%rsp): ~(interrupt number) */ |
1da177e4 | 823 | .macro interrupt func |
d99015b1 AH |
824 | subq $10*8, %rsp |
825 | CFI_ADJUST_CFA_OFFSET 10*8 | |
826 | call save_args | |
dcd072e2 | 827 | PARTIAL_FRAME 0 |
1da177e4 LT |
828 | call \func |
829 | .endm | |
830 | ||
722024db AH |
831 | /* |
832 | * The interrupt stubs push (~vector+0x80) onto the stack and | |
833 | * then jump to common_interrupt. | |
834 | */ | |
939b7871 PA |
835 | .p2align CONFIG_X86_L1_CACHE_SHIFT |
836 | common_interrupt: | |
7effaa88 | 837 | XCPT_FRAME |
722024db | 838 | addq $-0x80,(%rsp) /* Adjust vector to [-256,-1] range */ |
1da177e4 | 839 | interrupt do_IRQ |
3d1e42a7 | 840 | /* 0(%rsp): old_rsp-ARGOFFSET */ |
7effaa88 | 841 | ret_from_intr: |
72fe4858 | 842 | DISABLE_INTERRUPTS(CLBR_NONE) |
2601e64d | 843 | TRACE_IRQS_OFF |
56895530 | 844 | decl PER_CPU_VAR(irq_count) |
1de9c3f6 | 845 | leaveq |
7effaa88 | 846 | CFI_DEF_CFA_REGISTER rsp |
1de9c3f6 | 847 | CFI_ADJUST_CFA_OFFSET -8 |
7effaa88 | 848 | exit_intr: |
1da177e4 LT |
849 | GET_THREAD_INFO(%rcx) |
850 | testl $3,CS-ARGOFFSET(%rsp) | |
851 | je retint_kernel | |
0bd7b798 | 852 | |
1da177e4 LT |
853 | /* Interrupt came from user space */ |
854 | /* | |
855 | * Has a correct top of stack, but a partial stack frame | |
856 | * %rcx: thread info. Interrupts off. | |
0bd7b798 | 857 | */ |
1da177e4 LT |
858 | retint_with_reschedule: |
859 | movl $_TIF_WORK_MASK,%edi | |
7effaa88 | 860 | retint_check: |
10cd706d | 861 | LOCKDEP_SYS_EXIT_IRQ |
26ccb8a7 | 862 | movl TI_flags(%rcx),%edx |
1da177e4 | 863 | andl %edi,%edx |
7effaa88 | 864 | CFI_REMEMBER_STATE |
1da177e4 | 865 | jnz retint_careful |
10cd706d PZ |
866 | |
867 | retint_swapgs: /* return to user-space */ | |
2601e64d IM |
868 | /* |
869 | * The iretq could re-enable interrupts: | |
870 | */ | |
72fe4858 | 871 | DISABLE_INTERRUPTS(CLBR_ANY) |
2601e64d | 872 | TRACE_IRQS_IRETQ |
72fe4858 | 873 | SWAPGS |
2601e64d IM |
874 | jmp restore_args |
875 | ||
10cd706d | 876 | retint_restore_args: /* return to kernel space */ |
72fe4858 | 877 | DISABLE_INTERRUPTS(CLBR_ANY) |
2601e64d IM |
878 | /* |
879 | * The iretq could re-enable interrupts: | |
880 | */ | |
881 | TRACE_IRQS_IRETQ | |
882 | restore_args: | |
3701d863 IM |
883 | RESTORE_ARGS 0,8,0 |
884 | ||
f7f3d791 | 885 | irq_return: |
72fe4858 | 886 | INTERRUPT_RETURN |
3701d863 IM |
887 | |
888 | .section __ex_table, "a" | |
889 | .quad irq_return, bad_iret | |
890 | .previous | |
891 | ||
892 | #ifdef CONFIG_PARAVIRT | |
72fe4858 | 893 | ENTRY(native_iret) |
1da177e4 LT |
894 | iretq |
895 | ||
896 | .section __ex_table,"a" | |
72fe4858 | 897 | .quad native_iret, bad_iret |
1da177e4 | 898 | .previous |
3701d863 IM |
899 | #endif |
900 | ||
1da177e4 | 901 | .section .fixup,"ax" |
1da177e4 | 902 | bad_iret: |
3aa4b37d RM |
903 | /* |
904 | * The iret traps when the %cs or %ss being restored is bogus. | |
905 | * We've lost the original trap vector and error code. | |
906 | * #GPF is the most likely one to get for an invalid selector. | |
907 | * So pretend we completed the iret and took the #GPF in user mode. | |
908 | * | |
909 | * We are now running with the kernel GS after exception recovery. | |
910 | * But error_entry expects us to have user GS to match the user %cs, | |
911 | * so swap back. | |
912 | */ | |
913 | pushq $0 | |
914 | ||
915 | SWAPGS | |
916 | jmp general_protection | |
917 | ||
72fe4858 GOC |
918 | .previous |
919 | ||
7effaa88 | 920 | /* edi: workmask, edx: work */ |
1da177e4 | 921 | retint_careful: |
7effaa88 | 922 | CFI_RESTORE_STATE |
1da177e4 LT |
923 | bt $TIF_NEED_RESCHED,%edx |
924 | jnc retint_signal | |
2601e64d | 925 | TRACE_IRQS_ON |
72fe4858 | 926 | ENABLE_INTERRUPTS(CLBR_NONE) |
1da177e4 | 927 | pushq %rdi |
7effaa88 | 928 | CFI_ADJUST_CFA_OFFSET 8 |
1da177e4 | 929 | call schedule |
0bd7b798 | 930 | popq %rdi |
7effaa88 | 931 | CFI_ADJUST_CFA_OFFSET -8 |
1da177e4 | 932 | GET_THREAD_INFO(%rcx) |
72fe4858 | 933 | DISABLE_INTERRUPTS(CLBR_NONE) |
2601e64d | 934 | TRACE_IRQS_OFF |
1da177e4 | 935 | jmp retint_check |
0bd7b798 | 936 | |
1da177e4 | 937 | retint_signal: |
8f4d37ec | 938 | testl $_TIF_DO_NOTIFY_MASK,%edx |
10ffdbb8 | 939 | jz retint_swapgs |
2601e64d | 940 | TRACE_IRQS_ON |
72fe4858 | 941 | ENABLE_INTERRUPTS(CLBR_NONE) |
1da177e4 | 942 | SAVE_REST |
0bd7b798 | 943 | movq $-1,ORIG_RAX(%rsp) |
3829ee6b | 944 | xorl %esi,%esi # oldset |
1da177e4 LT |
945 | movq %rsp,%rdi # &pt_regs |
946 | call do_notify_resume | |
947 | RESTORE_REST | |
72fe4858 | 948 | DISABLE_INTERRUPTS(CLBR_NONE) |
2601e64d | 949 | TRACE_IRQS_OFF |
be9e6870 | 950 | GET_THREAD_INFO(%rcx) |
eca91e78 | 951 | jmp retint_with_reschedule |
1da177e4 LT |
952 | |
953 | #ifdef CONFIG_PREEMPT | |
954 | /* Returning to kernel space. Check if we need preemption */ | |
955 | /* rcx: threadinfo. interrupts off. */ | |
b06babac | 956 | ENTRY(retint_kernel) |
26ccb8a7 | 957 | cmpl $0,TI_preempt_count(%rcx) |
1da177e4 | 958 | jnz retint_restore_args |
26ccb8a7 | 959 | bt $TIF_NEED_RESCHED,TI_flags(%rcx) |
1da177e4 LT |
960 | jnc retint_restore_args |
961 | bt $9,EFLAGS-ARGOFFSET(%rsp) /* interrupts off? */ | |
962 | jnc retint_restore_args | |
963 | call preempt_schedule_irq | |
964 | jmp exit_intr | |
0bd7b798 | 965 | #endif |
4b787e0b | 966 | |
1da177e4 | 967 | CFI_ENDPROC |
4b787e0b | 968 | END(common_interrupt) |
0bd7b798 | 969 | |
1da177e4 LT |
970 | /* |
971 | * APIC interrupts. | |
0bd7b798 | 972 | */ |
322648d1 AH |
973 | .macro apicinterrupt num sym do_sym |
974 | ENTRY(\sym) | |
7effaa88 | 975 | INTR_FRAME |
19eadf98 | 976 | pushq $~(\num) |
7effaa88 | 977 | CFI_ADJUST_CFA_OFFSET 8 |
322648d1 | 978 | interrupt \do_sym |
1da177e4 LT |
979 | jmp ret_from_intr |
980 | CFI_ENDPROC | |
322648d1 AH |
981 | END(\sym) |
982 | .endm | |
1da177e4 | 983 | |
322648d1 AH |
984 | #ifdef CONFIG_SMP |
985 | apicinterrupt IRQ_MOVE_CLEANUP_VECTOR \ | |
986 | irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt | |
987 | #endif | |
1da177e4 | 988 | |
03b48632 | 989 | #ifdef CONFIG_X86_UV |
5ae3a139 | 990 | apicinterrupt UV_BAU_MESSAGE \ |
322648d1 | 991 | uv_bau_message_intr1 uv_bau_message_interrupt |
03b48632 | 992 | #endif |
322648d1 AH |
993 | apicinterrupt LOCAL_TIMER_VECTOR \ |
994 | apic_timer_interrupt smp_apic_timer_interrupt | |
89b831ef | 995 | |
0bd7b798 | 996 | #ifdef CONFIG_SMP |
322648d1 AH |
997 | apicinterrupt INVALIDATE_TLB_VECTOR_START+0 \ |
998 | invalidate_interrupt0 smp_invalidate_interrupt | |
999 | apicinterrupt INVALIDATE_TLB_VECTOR_START+1 \ | |
1000 | invalidate_interrupt1 smp_invalidate_interrupt | |
1001 | apicinterrupt INVALIDATE_TLB_VECTOR_START+2 \ | |
1002 | invalidate_interrupt2 smp_invalidate_interrupt | |
1003 | apicinterrupt INVALIDATE_TLB_VECTOR_START+3 \ | |
1004 | invalidate_interrupt3 smp_invalidate_interrupt | |
1005 | apicinterrupt INVALIDATE_TLB_VECTOR_START+4 \ | |
1006 | invalidate_interrupt4 smp_invalidate_interrupt | |
1007 | apicinterrupt INVALIDATE_TLB_VECTOR_START+5 \ | |
1008 | invalidate_interrupt5 smp_invalidate_interrupt | |
1009 | apicinterrupt INVALIDATE_TLB_VECTOR_START+6 \ | |
1010 | invalidate_interrupt6 smp_invalidate_interrupt | |
1011 | apicinterrupt INVALIDATE_TLB_VECTOR_START+7 \ | |
1012 | invalidate_interrupt7 smp_invalidate_interrupt | |
1da177e4 LT |
1013 | #endif |
1014 | ||
322648d1 AH |
1015 | apicinterrupt THRESHOLD_APIC_VECTOR \ |
1016 | threshold_interrupt mce_threshold_interrupt | |
1017 | apicinterrupt THERMAL_APIC_VECTOR \ | |
1018 | thermal_interrupt smp_thermal_interrupt | |
1812924b | 1019 | |
322648d1 AH |
1020 | #ifdef CONFIG_SMP |
1021 | apicinterrupt CALL_FUNCTION_SINGLE_VECTOR \ | |
1022 | call_function_single_interrupt smp_call_function_single_interrupt | |
1023 | apicinterrupt CALL_FUNCTION_VECTOR \ | |
1024 | call_function_interrupt smp_call_function_interrupt | |
1025 | apicinterrupt RESCHEDULE_VECTOR \ | |
1026 | reschedule_interrupt smp_reschedule_interrupt | |
1027 | #endif | |
1da177e4 | 1028 | |
322648d1 AH |
1029 | apicinterrupt ERROR_APIC_VECTOR \ |
1030 | error_interrupt smp_error_interrupt | |
1031 | apicinterrupt SPURIOUS_APIC_VECTOR \ | |
1032 | spurious_interrupt smp_spurious_interrupt | |
0bd7b798 | 1033 | |
1da177e4 LT |
1034 | /* |
1035 | * Exception entry points. | |
0bd7b798 | 1036 | */ |
322648d1 AH |
1037 | .macro zeroentry sym do_sym |
1038 | ENTRY(\sym) | |
7effaa88 | 1039 | INTR_FRAME |
fab58420 | 1040 | PARAVIRT_ADJUST_EXCEPTION_FRAME |
14ae22ba | 1041 | pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */ |
d99015b1 AH |
1042 | subq $15*8,%rsp |
1043 | CFI_ADJUST_CFA_OFFSET 15*8 | |
1044 | call error_entry | |
dcd072e2 | 1045 | DEFAULT_FRAME 0 |
d99015b1 AH |
1046 | movq %rsp,%rdi /* pt_regs pointer */ |
1047 | xorl %esi,%esi /* no error code */ | |
322648d1 | 1048 | call \do_sym |
d99015b1 | 1049 | jmp error_exit /* %ebx: no swapgs flag */ |
7effaa88 | 1050 | CFI_ENDPROC |
322648d1 AH |
1051 | END(\sym) |
1052 | .endm | |
1da177e4 | 1053 | |
322648d1 | 1054 | .macro paranoidzeroentry sym do_sym |
ddeb8f21 | 1055 | ENTRY(\sym) |
b8b1d08b AH |
1056 | INTR_FRAME |
1057 | PARAVIRT_ADJUST_EXCEPTION_FRAME | |
1058 | pushq $-1 /* ORIG_RAX: no syscall to restart */ | |
1059 | CFI_ADJUST_CFA_OFFSET 8 | |
1060 | subq $15*8, %rsp | |
1061 | call save_paranoid | |
1062 | TRACE_IRQS_OFF | |
1063 | movq %rsp,%rdi /* pt_regs pointer */ | |
1064 | xorl %esi,%esi /* no error code */ | |
322648d1 | 1065 | call \do_sym |
b8b1d08b AH |
1066 | jmp paranoid_exit /* %ebx: no swapgs flag */ |
1067 | CFI_ENDPROC | |
ddeb8f21 | 1068 | END(\sym) |
322648d1 | 1069 | .endm |
b8b1d08b | 1070 | |
322648d1 | 1071 | .macro paranoidzeroentry_ist sym do_sym ist |
ddeb8f21 | 1072 | ENTRY(\sym) |
9f1e87ea | 1073 | INTR_FRAME |
b8b1d08b AH |
1074 | PARAVIRT_ADJUST_EXCEPTION_FRAME |
1075 | pushq $-1 /* ORIG_RAX: no syscall to restart */ | |
1076 | CFI_ADJUST_CFA_OFFSET 8 | |
1077 | subq $15*8, %rsp | |
1078 | call save_paranoid | |
1079 | TRACE_IRQS_OFF | |
1080 | movq %rsp,%rdi /* pt_regs pointer */ | |
1081 | xorl %esi,%esi /* no error code */ | |
9939ddaf TH |
1082 | PER_CPU(init_tss, %rbp) |
1083 | subq $EXCEPTION_STKSZ, TSS_ist + (\ist - 1) * 8(%rbp) | |
322648d1 | 1084 | call \do_sym |
9939ddaf | 1085 | addq $EXCEPTION_STKSZ, TSS_ist + (\ist - 1) * 8(%rbp) |
b8b1d08b AH |
1086 | jmp paranoid_exit /* %ebx: no swapgs flag */ |
1087 | CFI_ENDPROC | |
ddeb8f21 | 1088 | END(\sym) |
322648d1 | 1089 | .endm |
b8b1d08b | 1090 | |
ddeb8f21 | 1091 | .macro errorentry sym do_sym |
322648d1 | 1092 | ENTRY(\sym) |
7effaa88 | 1093 | XCPT_FRAME |
fab58420 | 1094 | PARAVIRT_ADJUST_EXCEPTION_FRAME |
d99015b1 AH |
1095 | subq $15*8,%rsp |
1096 | CFI_ADJUST_CFA_OFFSET 15*8 | |
1097 | call error_entry | |
dcd072e2 | 1098 | DEFAULT_FRAME 0 |
d99015b1 AH |
1099 | movq %rsp,%rdi /* pt_regs pointer */ |
1100 | movq ORIG_RAX(%rsp),%rsi /* get error code */ | |
1101 | movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */ | |
322648d1 | 1102 | call \do_sym |
d99015b1 | 1103 | jmp error_exit /* %ebx: no swapgs flag */ |
7effaa88 | 1104 | CFI_ENDPROC |
322648d1 | 1105 | END(\sym) |
322648d1 | 1106 | .endm |
1da177e4 LT |
1107 | |
1108 | /* error code is on the stack already */ | |
ddeb8f21 | 1109 | .macro paranoiderrorentry sym do_sym |
322648d1 | 1110 | ENTRY(\sym) |
b8b1d08b AH |
1111 | XCPT_FRAME |
1112 | PARAVIRT_ADJUST_EXCEPTION_FRAME | |
1113 | subq $15*8,%rsp | |
e2f6bc25 AH |
1114 | CFI_ADJUST_CFA_OFFSET 15*8 |
1115 | call save_paranoid | |
1116 | DEFAULT_FRAME 0 | |
7e61a793 | 1117 | TRACE_IRQS_OFF |
b8b1d08b AH |
1118 | movq %rsp,%rdi /* pt_regs pointer */ |
1119 | movq ORIG_RAX(%rsp),%rsi /* get error code */ | |
1120 | movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */ | |
322648d1 | 1121 | call \do_sym |
b8b1d08b AH |
1122 | jmp paranoid_exit /* %ebx: no swapgs flag */ |
1123 | CFI_ENDPROC | |
322648d1 | 1124 | END(\sym) |
322648d1 AH |
1125 | .endm |
1126 | ||
1127 | zeroentry divide_error do_divide_error | |
322648d1 AH |
1128 | zeroentry overflow do_overflow |
1129 | zeroentry bounds do_bounds | |
1130 | zeroentry invalid_op do_invalid_op | |
1131 | zeroentry device_not_available do_device_not_available | |
ddeb8f21 | 1132 | paranoiderrorentry double_fault do_double_fault |
322648d1 AH |
1133 | zeroentry coprocessor_segment_overrun do_coprocessor_segment_overrun |
1134 | errorentry invalid_TSS do_invalid_TSS | |
1135 | errorentry segment_not_present do_segment_not_present | |
322648d1 AH |
1136 | zeroentry spurious_interrupt_bug do_spurious_interrupt_bug |
1137 | zeroentry coprocessor_error do_coprocessor_error | |
1138 | errorentry alignment_check do_alignment_check | |
322648d1 | 1139 | zeroentry simd_coprocessor_error do_simd_coprocessor_error |
2601e64d | 1140 | |
9f1e87ea CG |
1141 | /* Reload gs selector with exception handling */ |
1142 | /* edi: new selector */ | |
9f9d489a | 1143 | ENTRY(native_load_gs_index) |
7effaa88 | 1144 | CFI_STARTPROC |
1da177e4 | 1145 | pushf |
7effaa88 | 1146 | CFI_ADJUST_CFA_OFFSET 8 |
b8aa287f | 1147 | DISABLE_INTERRUPTS(CLBR_ANY & ~CLBR_RDI) |
9f1e87ea | 1148 | SWAPGS |
0bd7b798 | 1149 | gs_change: |
9f1e87ea | 1150 | movl %edi,%gs |
1da177e4 | 1151 | 2: mfence /* workaround */ |
72fe4858 | 1152 | SWAPGS |
9f1e87ea | 1153 | popf |
7effaa88 | 1154 | CFI_ADJUST_CFA_OFFSET -8 |
9f1e87ea | 1155 | ret |
7effaa88 | 1156 | CFI_ENDPROC |
6efdcfaf | 1157 | END(native_load_gs_index) |
0bd7b798 | 1158 | |
9f1e87ea CG |
1159 | .section __ex_table,"a" |
1160 | .align 8 | |
1161 | .quad gs_change,bad_gs | |
1162 | .previous | |
1163 | .section .fixup,"ax" | |
1da177e4 | 1164 | /* running with kernelgs */ |
0bd7b798 | 1165 | bad_gs: |
72fe4858 | 1166 | SWAPGS /* switch back to user gs */ |
1da177e4 | 1167 | xorl %eax,%eax |
9f1e87ea CG |
1168 | movl %eax,%gs |
1169 | jmp 2b | |
1170 | .previous | |
0bd7b798 | 1171 | |
1da177e4 LT |
1172 | /* |
1173 | * Create a kernel thread. | |
1174 | * | |
1175 | * C extern interface: | |
1176 | * extern long kernel_thread(int (*fn)(void *), void * arg, unsigned long flags) | |
1177 | * | |
1178 | * asm input arguments: | |
1179 | * rdi: fn, rsi: arg, rdx: flags | |
1180 | */ | |
1181 | ENTRY(kernel_thread) | |
1182 | CFI_STARTPROC | |
1183 | FAKE_STACK_FRAME $child_rip | |
1184 | SAVE_ALL | |
1185 | ||
1186 | # rdi: flags, rsi: usp, rdx: will be &pt_regs | |
1187 | movq %rdx,%rdi | |
1188 | orq kernel_thread_flags(%rip),%rdi | |
1189 | movq $-1, %rsi | |
1190 | movq %rsp, %rdx | |
1191 | ||
1192 | xorl %r8d,%r8d | |
1193 | xorl %r9d,%r9d | |
0bd7b798 | 1194 | |
1da177e4 LT |
1195 | # clone now |
1196 | call do_fork | |
1197 | movq %rax,RAX(%rsp) | |
1198 | xorl %edi,%edi | |
1199 | ||
1200 | /* | |
1201 | * It isn't worth to check for reschedule here, | |
1202 | * so internally to the x86_64 port you can rely on kernel_thread() | |
1203 | * not to reschedule the child before returning, this avoids the need | |
1204 | * of hacks for example to fork off the per-CPU idle tasks. | |
9f1e87ea | 1205 | * [Hopefully no generic code relies on the reschedule -AK] |
1da177e4 LT |
1206 | */ |
1207 | RESTORE_ALL | |
1208 | UNFAKE_STACK_FRAME | |
1209 | ret | |
1210 | CFI_ENDPROC | |
6efdcfaf | 1211 | END(kernel_thread) |
0bd7b798 | 1212 | |
c2c631e3 | 1213 | ENTRY(child_rip) |
c05991ed AK |
1214 | pushq $0 # fake return address |
1215 | CFI_STARTPROC | |
1da177e4 LT |
1216 | /* |
1217 | * Here we are in the child and the registers are set as they were | |
1218 | * at kernel_thread() invocation in the parent. | |
1219 | */ | |
1220 | movq %rdi, %rax | |
1221 | movq %rsi, %rdi | |
1222 | call *%rax | |
1223 | # exit | |
1c5b5cfd | 1224 | mov %eax, %edi |
1da177e4 | 1225 | call do_exit |
5f5db591 | 1226 | ud2 # padding for call trace |
c05991ed | 1227 | CFI_ENDPROC |
6efdcfaf | 1228 | END(child_rip) |
1da177e4 LT |
1229 | |
1230 | /* | |
1231 | * execve(). This function needs to use IRET, not SYSRET, to set up all state properly. | |
1232 | * | |
1233 | * C extern interface: | |
1234 | * extern long execve(char *name, char **argv, char **envp) | |
1235 | * | |
1236 | * asm input arguments: | |
1237 | * rdi: name, rsi: argv, rdx: envp | |
1238 | * | |
1239 | * We want to fallback into: | |
5d119b2c | 1240 | * extern long sys_execve(char *name, char **argv,char **envp, struct pt_regs *regs) |
1da177e4 LT |
1241 | * |
1242 | * do_sys_execve asm fallback arguments: | |
5d119b2c | 1243 | * rdi: name, rsi: argv, rdx: envp, rcx: fake frame on the stack |
1da177e4 | 1244 | */ |
3db03b4a | 1245 | ENTRY(kernel_execve) |
1da177e4 LT |
1246 | CFI_STARTPROC |
1247 | FAKE_STACK_FRAME $0 | |
0bd7b798 | 1248 | SAVE_ALL |
5d119b2c | 1249 | movq %rsp,%rcx |
1da177e4 | 1250 | call sys_execve |
0bd7b798 | 1251 | movq %rax, RAX(%rsp) |
1da177e4 LT |
1252 | RESTORE_REST |
1253 | testq %rax,%rax | |
1254 | je int_ret_from_sys_call | |
1255 | RESTORE_ARGS | |
1256 | UNFAKE_STACK_FRAME | |
1257 | ret | |
1258 | CFI_ENDPROC | |
6efdcfaf | 1259 | END(kernel_execve) |
1da177e4 | 1260 | |
2699500b | 1261 | /* Call softirq on interrupt stack. Interrupts are off. */ |
ed6b676c | 1262 | ENTRY(call_softirq) |
7effaa88 | 1263 | CFI_STARTPROC |
2699500b AK |
1264 | push %rbp |
1265 | CFI_ADJUST_CFA_OFFSET 8 | |
1266 | CFI_REL_OFFSET rbp,0 | |
1267 | mov %rsp,%rbp | |
1268 | CFI_DEF_CFA_REGISTER rbp | |
56895530 | 1269 | incl PER_CPU_VAR(irq_count) |
26f80bd6 | 1270 | cmove PER_CPU_VAR(irq_stack_ptr),%rsp |
2699500b | 1271 | push %rbp # backlink for old unwinder |
ed6b676c | 1272 | call __do_softirq |
2699500b | 1273 | leaveq |
7effaa88 | 1274 | CFI_DEF_CFA_REGISTER rsp |
2699500b | 1275 | CFI_ADJUST_CFA_OFFSET -8 |
56895530 | 1276 | decl PER_CPU_VAR(irq_count) |
ed6b676c | 1277 | ret |
7effaa88 | 1278 | CFI_ENDPROC |
6efdcfaf | 1279 | END(call_softirq) |
75154f40 | 1280 | |
3d75e1b8 | 1281 | #ifdef CONFIG_XEN |
322648d1 | 1282 | zeroentry xen_hypervisor_callback xen_do_hypervisor_callback |
3d75e1b8 JF |
1283 | |
1284 | /* | |
9f1e87ea CG |
1285 | * A note on the "critical region" in our callback handler. |
1286 | * We want to avoid stacking callback handlers due to events occurring | |
1287 | * during handling of the last event. To do this, we keep events disabled | |
1288 | * until we've done all processing. HOWEVER, we must enable events before | |
1289 | * popping the stack frame (can't be done atomically) and so it would still | |
1290 | * be possible to get enough handler activations to overflow the stack. | |
1291 | * Although unlikely, bugs of that kind are hard to track down, so we'd | |
1292 | * like to avoid the possibility. | |
1293 | * So, on entry to the handler we detect whether we interrupted an | |
1294 | * existing activation in its critical region -- if so, we pop the current | |
1295 | * activation and restart the handler using the previous one. | |
1296 | */ | |
3d75e1b8 JF |
1297 | ENTRY(xen_do_hypervisor_callback) # do_hypervisor_callback(struct *pt_regs) |
1298 | CFI_STARTPROC | |
9f1e87ea CG |
1299 | /* |
1300 | * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will | |
1301 | * see the correct pointer to the pt_regs | |
1302 | */ | |
3d75e1b8 JF |
1303 | movq %rdi, %rsp # we don't return, adjust the stack frame |
1304 | CFI_ENDPROC | |
dcd072e2 | 1305 | DEFAULT_FRAME |
56895530 | 1306 | 11: incl PER_CPU_VAR(irq_count) |
3d75e1b8 JF |
1307 | movq %rsp,%rbp |
1308 | CFI_DEF_CFA_REGISTER rbp | |
26f80bd6 | 1309 | cmovzq PER_CPU_VAR(irq_stack_ptr),%rsp |
3d75e1b8 JF |
1310 | pushq %rbp # backlink for old unwinder |
1311 | call xen_evtchn_do_upcall | |
1312 | popq %rsp | |
1313 | CFI_DEF_CFA_REGISTER rsp | |
56895530 | 1314 | decl PER_CPU_VAR(irq_count) |
3d75e1b8 JF |
1315 | jmp error_exit |
1316 | CFI_ENDPROC | |
1317 | END(do_hypervisor_callback) | |
1318 | ||
1319 | /* | |
9f1e87ea CG |
1320 | * Hypervisor uses this for application faults while it executes. |
1321 | * We get here for two reasons: | |
1322 | * 1. Fault while reloading DS, ES, FS or GS | |
1323 | * 2. Fault while executing IRET | |
1324 | * Category 1 we do not need to fix up as Xen has already reloaded all segment | |
1325 | * registers that could be reloaded and zeroed the others. | |
1326 | * Category 2 we fix up by killing the current process. We cannot use the | |
1327 | * normal Linux return path in this case because if we use the IRET hypercall | |
1328 | * to pop the stack frame we end up in an infinite loop of failsafe callbacks. | |
1329 | * We distinguish between categories by comparing each saved segment register | |
1330 | * with its current contents: any discrepancy means we in category 1. | |
1331 | */ | |
3d75e1b8 | 1332 | ENTRY(xen_failsafe_callback) |
dcd072e2 AH |
1333 | INTR_FRAME 1 (6*8) |
1334 | /*CFI_REL_OFFSET gs,GS*/ | |
1335 | /*CFI_REL_OFFSET fs,FS*/ | |
1336 | /*CFI_REL_OFFSET es,ES*/ | |
1337 | /*CFI_REL_OFFSET ds,DS*/ | |
1338 | CFI_REL_OFFSET r11,8 | |
1339 | CFI_REL_OFFSET rcx,0 | |
3d75e1b8 JF |
1340 | movw %ds,%cx |
1341 | cmpw %cx,0x10(%rsp) | |
1342 | CFI_REMEMBER_STATE | |
1343 | jne 1f | |
1344 | movw %es,%cx | |
1345 | cmpw %cx,0x18(%rsp) | |
1346 | jne 1f | |
1347 | movw %fs,%cx | |
1348 | cmpw %cx,0x20(%rsp) | |
1349 | jne 1f | |
1350 | movw %gs,%cx | |
1351 | cmpw %cx,0x28(%rsp) | |
1352 | jne 1f | |
1353 | /* All segments match their saved values => Category 2 (Bad IRET). */ | |
1354 | movq (%rsp),%rcx | |
1355 | CFI_RESTORE rcx | |
1356 | movq 8(%rsp),%r11 | |
1357 | CFI_RESTORE r11 | |
1358 | addq $0x30,%rsp | |
1359 | CFI_ADJUST_CFA_OFFSET -0x30 | |
14ae22ba IM |
1360 | pushq_cfi $0 /* RIP */ |
1361 | pushq_cfi %r11 | |
1362 | pushq_cfi %rcx | |
4a5c3e77 | 1363 | jmp general_protection |
3d75e1b8 JF |
1364 | CFI_RESTORE_STATE |
1365 | 1: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */ | |
1366 | movq (%rsp),%rcx | |
1367 | CFI_RESTORE rcx | |
1368 | movq 8(%rsp),%r11 | |
1369 | CFI_RESTORE r11 | |
1370 | addq $0x30,%rsp | |
1371 | CFI_ADJUST_CFA_OFFSET -0x30 | |
14ae22ba | 1372 | pushq_cfi $0 |
3d75e1b8 JF |
1373 | SAVE_ALL |
1374 | jmp error_exit | |
1375 | CFI_ENDPROC | |
3d75e1b8 JF |
1376 | END(xen_failsafe_callback) |
1377 | ||
1378 | #endif /* CONFIG_XEN */ | |
ddeb8f21 AH |
1379 | |
1380 | /* | |
1381 | * Some functions should be protected against kprobes | |
1382 | */ | |
1383 | .pushsection .kprobes.text, "ax" | |
1384 | ||
1385 | paranoidzeroentry_ist debug do_debug DEBUG_STACK | |
1386 | paranoidzeroentry_ist int3 do_int3 DEBUG_STACK | |
1387 | paranoiderrorentry stack_segment do_stack_segment | |
1388 | errorentry general_protection do_general_protection | |
1389 | errorentry page_fault do_page_fault | |
1390 | #ifdef CONFIG_X86_MCE | |
1391 | paranoidzeroentry machine_check do_machine_check | |
1392 | #endif | |
1393 | ||
1394 | /* | |
9f1e87ea CG |
1395 | * "Paranoid" exit path from exception stack. |
1396 | * Paranoid because this is used by NMIs and cannot take | |
ddeb8f21 AH |
1397 | * any kernel state for granted. |
1398 | * We don't do kernel preemption checks here, because only | |
1399 | * NMI should be common and it does not enable IRQs and | |
1400 | * cannot get reschedule ticks. | |
1401 | * | |
1402 | * "trace" is 0 for the NMI handler only, because irq-tracing | |
1403 | * is fundamentally NMI-unsafe. (we cannot change the soft and | |
1404 | * hard flags at once, atomically) | |
1405 | */ | |
1406 | ||
1407 | /* ebx: no swapgs flag */ | |
1408 | ENTRY(paranoid_exit) | |
1409 | INTR_FRAME | |
1410 | DISABLE_INTERRUPTS(CLBR_NONE) | |
1411 | TRACE_IRQS_OFF | |
1412 | testl %ebx,%ebx /* swapgs needed? */ | |
1413 | jnz paranoid_restore | |
1414 | testl $3,CS(%rsp) | |
1415 | jnz paranoid_userspace | |
1416 | paranoid_swapgs: | |
1417 | TRACE_IRQS_IRETQ 0 | |
1418 | SWAPGS_UNSAFE_STACK | |
1419 | paranoid_restore: | |
1420 | RESTORE_ALL 8 | |
1421 | jmp irq_return | |
1422 | paranoid_userspace: | |
1423 | GET_THREAD_INFO(%rcx) | |
1424 | movl TI_flags(%rcx),%ebx | |
1425 | andl $_TIF_WORK_MASK,%ebx | |
1426 | jz paranoid_swapgs | |
1427 | movq %rsp,%rdi /* &pt_regs */ | |
1428 | call sync_regs | |
1429 | movq %rax,%rsp /* switch stack for scheduling */ | |
1430 | testl $_TIF_NEED_RESCHED,%ebx | |
1431 | jnz paranoid_schedule | |
1432 | movl %ebx,%edx /* arg3: thread flags */ | |
1433 | TRACE_IRQS_ON | |
1434 | ENABLE_INTERRUPTS(CLBR_NONE) | |
1435 | xorl %esi,%esi /* arg2: oldset */ | |
1436 | movq %rsp,%rdi /* arg1: &pt_regs */ | |
1437 | call do_notify_resume | |
1438 | DISABLE_INTERRUPTS(CLBR_NONE) | |
1439 | TRACE_IRQS_OFF | |
1440 | jmp paranoid_userspace | |
1441 | paranoid_schedule: | |
1442 | TRACE_IRQS_ON | |
1443 | ENABLE_INTERRUPTS(CLBR_ANY) | |
1444 | call schedule | |
1445 | DISABLE_INTERRUPTS(CLBR_ANY) | |
1446 | TRACE_IRQS_OFF | |
1447 | jmp paranoid_userspace | |
1448 | CFI_ENDPROC | |
1449 | END(paranoid_exit) | |
1450 | ||
1451 | /* | |
1452 | * Exception entry point. This expects an error code/orig_rax on the stack. | |
1453 | * returns in "no swapgs flag" in %ebx. | |
1454 | */ | |
1455 | ENTRY(error_entry) | |
1456 | XCPT_FRAME | |
1457 | CFI_ADJUST_CFA_OFFSET 15*8 | |
1458 | /* oldrax contains error code */ | |
1459 | cld | |
1460 | movq_cfi rdi, RDI+8 | |
1461 | movq_cfi rsi, RSI+8 | |
1462 | movq_cfi rdx, RDX+8 | |
1463 | movq_cfi rcx, RCX+8 | |
1464 | movq_cfi rax, RAX+8 | |
1465 | movq_cfi r8, R8+8 | |
1466 | movq_cfi r9, R9+8 | |
1467 | movq_cfi r10, R10+8 | |
1468 | movq_cfi r11, R11+8 | |
1469 | movq_cfi rbx, RBX+8 | |
1470 | movq_cfi rbp, RBP+8 | |
1471 | movq_cfi r12, R12+8 | |
1472 | movq_cfi r13, R13+8 | |
1473 | movq_cfi r14, R14+8 | |
1474 | movq_cfi r15, R15+8 | |
1475 | xorl %ebx,%ebx | |
1476 | testl $3,CS+8(%rsp) | |
1477 | je error_kernelspace | |
1478 | error_swapgs: | |
1479 | SWAPGS | |
1480 | error_sti: | |
1481 | TRACE_IRQS_OFF | |
1482 | ret | |
1483 | CFI_ENDPROC | |
1484 | ||
1485 | /* | |
1486 | * There are two places in the kernel that can potentially fault with | |
1487 | * usergs. Handle them here. The exception handlers after iret run with | |
1488 | * kernel gs again, so don't set the user space flag. B stepping K8s | |
1489 | * sometimes report an truncated RIP for IRET exceptions returning to | |
1490 | * compat mode. Check for these here too. | |
1491 | */ | |
1492 | error_kernelspace: | |
1493 | incl %ebx | |
1494 | leaq irq_return(%rip),%rcx | |
1495 | cmpq %rcx,RIP+8(%rsp) | |
1496 | je error_swapgs | |
1497 | movl %ecx,%ecx /* zero extend */ | |
1498 | cmpq %rcx,RIP+8(%rsp) | |
1499 | je error_swapgs | |
1500 | cmpq $gs_change,RIP+8(%rsp) | |
9f1e87ea | 1501 | je error_swapgs |
ddeb8f21 AH |
1502 | jmp error_sti |
1503 | END(error_entry) | |
1504 | ||
1505 | ||
1506 | /* ebx: no swapgs flag (1: don't need swapgs, 0: need it) */ | |
1507 | ENTRY(error_exit) | |
1508 | DEFAULT_FRAME | |
1509 | movl %ebx,%eax | |
1510 | RESTORE_REST | |
1511 | DISABLE_INTERRUPTS(CLBR_NONE) | |
1512 | TRACE_IRQS_OFF | |
1513 | GET_THREAD_INFO(%rcx) | |
1514 | testl %eax,%eax | |
1515 | jne retint_kernel | |
1516 | LOCKDEP_SYS_EXIT_IRQ | |
1517 | movl TI_flags(%rcx),%edx | |
1518 | movl $_TIF_WORK_MASK,%edi | |
1519 | andl %edi,%edx | |
1520 | jnz retint_careful | |
1521 | jmp retint_swapgs | |
1522 | CFI_ENDPROC | |
1523 | END(error_exit) | |
1524 | ||
1525 | ||
1526 | /* runs on exception stack */ | |
1527 | ENTRY(nmi) | |
1528 | INTR_FRAME | |
1529 | PARAVIRT_ADJUST_EXCEPTION_FRAME | |
1530 | pushq_cfi $-1 | |
1531 | subq $15*8, %rsp | |
1532 | CFI_ADJUST_CFA_OFFSET 15*8 | |
1533 | call save_paranoid | |
1534 | DEFAULT_FRAME 0 | |
1535 | /* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */ | |
1536 | movq %rsp,%rdi | |
1537 | movq $-1,%rsi | |
1538 | call do_nmi | |
1539 | #ifdef CONFIG_TRACE_IRQFLAGS | |
1540 | /* paranoidexit; without TRACE_IRQS_OFF */ | |
1541 | /* ebx: no swapgs flag */ | |
1542 | DISABLE_INTERRUPTS(CLBR_NONE) | |
1543 | testl %ebx,%ebx /* swapgs needed? */ | |
1544 | jnz nmi_restore | |
1545 | testl $3,CS(%rsp) | |
1546 | jnz nmi_userspace | |
1547 | nmi_swapgs: | |
1548 | SWAPGS_UNSAFE_STACK | |
1549 | nmi_restore: | |
1550 | RESTORE_ALL 8 | |
1551 | jmp irq_return | |
1552 | nmi_userspace: | |
1553 | GET_THREAD_INFO(%rcx) | |
1554 | movl TI_flags(%rcx),%ebx | |
1555 | andl $_TIF_WORK_MASK,%ebx | |
1556 | jz nmi_swapgs | |
1557 | movq %rsp,%rdi /* &pt_regs */ | |
1558 | call sync_regs | |
1559 | movq %rax,%rsp /* switch stack for scheduling */ | |
1560 | testl $_TIF_NEED_RESCHED,%ebx | |
1561 | jnz nmi_schedule | |
1562 | movl %ebx,%edx /* arg3: thread flags */ | |
1563 | ENABLE_INTERRUPTS(CLBR_NONE) | |
1564 | xorl %esi,%esi /* arg2: oldset */ | |
1565 | movq %rsp,%rdi /* arg1: &pt_regs */ | |
1566 | call do_notify_resume | |
1567 | DISABLE_INTERRUPTS(CLBR_NONE) | |
1568 | jmp nmi_userspace | |
1569 | nmi_schedule: | |
1570 | ENABLE_INTERRUPTS(CLBR_ANY) | |
1571 | call schedule | |
1572 | DISABLE_INTERRUPTS(CLBR_ANY) | |
1573 | jmp nmi_userspace | |
1574 | CFI_ENDPROC | |
1575 | #else | |
1576 | jmp paranoid_exit | |
9f1e87ea | 1577 | CFI_ENDPROC |
ddeb8f21 AH |
1578 | #endif |
1579 | END(nmi) | |
1580 | ||
1581 | ENTRY(ignore_sysret) | |
1582 | CFI_STARTPROC | |
1583 | mov $-ENOSYS,%eax | |
1584 | sysret | |
1585 | CFI_ENDPROC | |
1586 | END(ignore_sysret) | |
1587 | ||
1588 | /* | |
1589 | * End of kprobes section | |
1590 | */ | |
1591 | .popsection |