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