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