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