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