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