]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/x86/kernel/entry_64.S
x86: include ENTRY/END in entry handlers in entry_64.S
[mirror_ubuntu-bionic-kernel.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 */
322648d1
AH
925.macro apicinterrupt num sym do_sym
926ENTRY(\sym)
7effaa88 927 INTR_FRAME
19eadf98 928 pushq $~(\num)
7effaa88 929 CFI_ADJUST_CFA_OFFSET 8
322648d1 930 interrupt \do_sym
1da177e4
LT
931 jmp ret_from_intr
932 CFI_ENDPROC
322648d1
AH
933END(\sym)
934.endm
1da177e4 935
322648d1
AH
936#ifdef CONFIG_SMP
937apicinterrupt IRQ_MOVE_CLEANUP_VECTOR \
938 irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt
939#endif
1da177e4 940
322648d1
AH
941apicinterrupt 220 \
942 uv_bau_message_intr1 uv_bau_message_interrupt
943apicinterrupt LOCAL_TIMER_VECTOR \
944 apic_timer_interrupt smp_apic_timer_interrupt
89b831ef 945
0bd7b798 946#ifdef CONFIG_SMP
322648d1
AH
947apicinterrupt INVALIDATE_TLB_VECTOR_START+0 \
948 invalidate_interrupt0 smp_invalidate_interrupt
949apicinterrupt INVALIDATE_TLB_VECTOR_START+1 \
950 invalidate_interrupt1 smp_invalidate_interrupt
951apicinterrupt INVALIDATE_TLB_VECTOR_START+2 \
952 invalidate_interrupt2 smp_invalidate_interrupt
953apicinterrupt INVALIDATE_TLB_VECTOR_START+3 \
954 invalidate_interrupt3 smp_invalidate_interrupt
955apicinterrupt INVALIDATE_TLB_VECTOR_START+4 \
956 invalidate_interrupt4 smp_invalidate_interrupt
957apicinterrupt INVALIDATE_TLB_VECTOR_START+5 \
958 invalidate_interrupt5 smp_invalidate_interrupt
959apicinterrupt INVALIDATE_TLB_VECTOR_START+6 \
960 invalidate_interrupt6 smp_invalidate_interrupt
961apicinterrupt INVALIDATE_TLB_VECTOR_START+7 \
962 invalidate_interrupt7 smp_invalidate_interrupt
1da177e4
LT
963#endif
964
322648d1
AH
965apicinterrupt THRESHOLD_APIC_VECTOR \
966 threshold_interrupt mce_threshold_interrupt
967apicinterrupt THERMAL_APIC_VECTOR \
968 thermal_interrupt smp_thermal_interrupt
1812924b 969
322648d1
AH
970#ifdef CONFIG_SMP
971apicinterrupt CALL_FUNCTION_SINGLE_VECTOR \
972 call_function_single_interrupt smp_call_function_single_interrupt
973apicinterrupt CALL_FUNCTION_VECTOR \
974 call_function_interrupt smp_call_function_interrupt
975apicinterrupt RESCHEDULE_VECTOR \
976 reschedule_interrupt smp_reschedule_interrupt
977#endif
1da177e4 978
322648d1
AH
979apicinterrupt ERROR_APIC_VECTOR \
980 error_interrupt smp_error_interrupt
981apicinterrupt SPURIOUS_APIC_VECTOR \
982 spurious_interrupt smp_spurious_interrupt
0bd7b798 983
1da177e4
LT
984/*
985 * Exception entry points.
0bd7b798 986 */
322648d1
AH
987.macro zeroentry sym do_sym
988ENTRY(\sym)
7effaa88 989 INTR_FRAME
fab58420 990 PARAVIRT_ADJUST_EXCEPTION_FRAME
14ae22ba 991 pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */
d99015b1
AH
992 subq $15*8,%rsp
993 CFI_ADJUST_CFA_OFFSET 15*8
994 call error_entry
dcd072e2 995 DEFAULT_FRAME 0
d99015b1
AH
996 movq %rsp,%rdi /* pt_regs pointer */
997 xorl %esi,%esi /* no error code */
322648d1 998 call \do_sym
d99015b1 999 jmp error_exit /* %ebx: no swapgs flag */
7effaa88 1000 CFI_ENDPROC
322648d1
AH
1001END(\sym)
1002.endm
1da177e4 1003
322648d1
AH
1004.macro paranoidzeroentry sym do_sym
1005KPROBE_ENTRY(\sym)
b8b1d08b
AH
1006 INTR_FRAME
1007 PARAVIRT_ADJUST_EXCEPTION_FRAME
1008 pushq $-1 /* ORIG_RAX: no syscall to restart */
1009 CFI_ADJUST_CFA_OFFSET 8
1010 subq $15*8, %rsp
1011 call save_paranoid
1012 TRACE_IRQS_OFF
1013 movq %rsp,%rdi /* pt_regs pointer */
1014 xorl %esi,%esi /* no error code */
322648d1 1015 call \do_sym
b8b1d08b
AH
1016 jmp paranoid_exit /* %ebx: no swapgs flag */
1017 CFI_ENDPROC
322648d1
AH
1018KPROBE_END(\sym)
1019.endm
b8b1d08b 1020
322648d1
AH
1021.macro paranoidzeroentry_ist sym do_sym ist
1022KPROBE_ENTRY(\sym)
b8b1d08b
AH
1023 INTR_FRAME
1024 PARAVIRT_ADJUST_EXCEPTION_FRAME
1025 pushq $-1 /* ORIG_RAX: no syscall to restart */
1026 CFI_ADJUST_CFA_OFFSET 8
1027 subq $15*8, %rsp
1028 call save_paranoid
1029 TRACE_IRQS_OFF
1030 movq %rsp,%rdi /* pt_regs pointer */
1031 xorl %esi,%esi /* no error code */
1032 movq %gs:pda_data_offset, %rbp
1033 subq $EXCEPTION_STKSZ, per_cpu__init_tss + TSS_ist + (\ist - 1) * 8(%rbp)
322648d1 1034 call \do_sym
b8b1d08b
AH
1035 addq $EXCEPTION_STKSZ, per_cpu__init_tss + TSS_ist + (\ist - 1) * 8(%rbp)
1036 jmp paranoid_exit /* %ebx: no swapgs flag */
1037 CFI_ENDPROC
322648d1
AH
1038KPROBE_END(\sym)
1039.endm
b8b1d08b 1040
322648d1
AH
1041.macro errorentry sym do_sym entry=0
1042.if \entry
1043KPROBE_ENTRY(\sym)
1044.else
1045ENTRY(\sym)
1046.endif
7effaa88 1047 XCPT_FRAME
fab58420 1048 PARAVIRT_ADJUST_EXCEPTION_FRAME
d99015b1
AH
1049 subq $15*8,%rsp
1050 CFI_ADJUST_CFA_OFFSET 15*8
1051 call error_entry
dcd072e2 1052 DEFAULT_FRAME 0
d99015b1
AH
1053 movq %rsp,%rdi /* pt_regs pointer */
1054 movq ORIG_RAX(%rsp),%rsi /* get error code */
1055 movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */
322648d1 1056 call \do_sym
d99015b1 1057 jmp error_exit /* %ebx: no swapgs flag */
7effaa88 1058 CFI_ENDPROC
322648d1
AH
1059.if \entry
1060KPROBE_END(\sym)
1061.else
1062END(\sym)
1063.endif
1064.endm
1da177e4
LT
1065
1066 /* error code is on the stack already */
322648d1
AH
1067.macro paranoiderrorentry sym do_sym entry=1
1068.if \entry
1069KPROBE_ENTRY(\sym)
1070.else
1071ENTRY(\sym)
1072.endif
b8b1d08b
AH
1073 XCPT_FRAME
1074 PARAVIRT_ADJUST_EXCEPTION_FRAME
1075 subq $15*8,%rsp
e2f6bc25
AH
1076 CFI_ADJUST_CFA_OFFSET 15*8
1077 call save_paranoid
1078 DEFAULT_FRAME 0
7e61a793 1079 TRACE_IRQS_OFF
b8b1d08b
AH
1080 movq %rsp,%rdi /* pt_regs pointer */
1081 movq ORIG_RAX(%rsp),%rsi /* get error code */
1082 movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */
322648d1 1083 call \do_sym
b8b1d08b
AH
1084 jmp paranoid_exit /* %ebx: no swapgs flag */
1085 CFI_ENDPROC
322648d1
AH
1086.if \entry
1087KPROBE_END(\sym)
1088.else
1089END(\sym)
1090.endif
1091.endm
1092
1093zeroentry divide_error do_divide_error
1094paranoidzeroentry_ist debug do_debug DEBUG_STACK
1095paranoidzeroentry_ist int3 do_int3 DEBUG_STACK
1096zeroentry overflow do_overflow
1097zeroentry bounds do_bounds
1098zeroentry invalid_op do_invalid_op
1099zeroentry device_not_available do_device_not_available
1100paranoiderrorentry double_fault do_double_fault 0
1101zeroentry coprocessor_segment_overrun do_coprocessor_segment_overrun
1102errorentry invalid_TSS do_invalid_TSS
1103errorentry segment_not_present do_segment_not_present
1104paranoiderrorentry stack_segment do_stack_segment
1105errorentry general_protection do_general_protection 1
1106errorentry page_fault do_page_fault 1
1107zeroentry spurious_interrupt_bug do_spurious_interrupt_bug
1108zeroentry coprocessor_error do_coprocessor_error
1109errorentry alignment_check do_alignment_check
1110#ifdef CONFIG_X86_MCE
1111paranoidzeroentry machine_check do_machine_check
1112#endif
1113zeroentry simd_coprocessor_error do_simd_coprocessor_error
2601e64d
IM
1114
1115 /*
1116 * "Paranoid" exit path from exception stack.
1117 * Paranoid because this is used by NMIs and cannot take
1118 * any kernel state for granted.
1119 * We don't do kernel preemption checks here, because only
1120 * NMI should be common and it does not enable IRQs and
1121 * cannot get reschedule ticks.
1122 *
1123 * "trace" is 0 for the NMI handler only, because irq-tracing
1124 * is fundamentally NMI-unsafe. (we cannot change the soft and
1125 * hard flags at once, atomically)
1126 */
e2f6bc25 1127
2601e64d 1128 /* ebx: no swapgs flag */
e2f6bc25
AH
1129KPROBE_ENTRY(paranoid_exit)
1130 INTR_FRAME
b8b1d08b
AH
1131 DISABLE_INTERRUPTS(CLBR_NONE)
1132 TRACE_IRQS_OFF
2601e64d 1133 testl %ebx,%ebx /* swapgs needed? */
e2f6bc25 1134 jnz paranoid_restore
2601e64d 1135 testl $3,CS(%rsp)
e2f6bc25
AH
1136 jnz paranoid_userspace
1137paranoid_swapgs:
2601e64d 1138 TRACE_IRQS_IRETQ 0
72fe4858 1139 SWAPGS_UNSAFE_STACK
e2f6bc25 1140paranoid_restore:
2601e64d 1141 RESTORE_ALL 8
3701d863 1142 jmp irq_return
e2f6bc25 1143paranoid_userspace:
2601e64d 1144 GET_THREAD_INFO(%rcx)
26ccb8a7 1145 movl TI_flags(%rcx),%ebx
2601e64d 1146 andl $_TIF_WORK_MASK,%ebx
e2f6bc25 1147 jz paranoid_swapgs
2601e64d
IM
1148 movq %rsp,%rdi /* &pt_regs */
1149 call sync_regs
1150 movq %rax,%rsp /* switch stack for scheduling */
1151 testl $_TIF_NEED_RESCHED,%ebx
e2f6bc25 1152 jnz paranoid_schedule
2601e64d 1153 movl %ebx,%edx /* arg3: thread flags */
2601e64d 1154 TRACE_IRQS_ON
72fe4858 1155 ENABLE_INTERRUPTS(CLBR_NONE)
2601e64d
IM
1156 xorl %esi,%esi /* arg2: oldset */
1157 movq %rsp,%rdi /* arg1: &pt_regs */
1158 call do_notify_resume
72fe4858 1159 DISABLE_INTERRUPTS(CLBR_NONE)
2601e64d 1160 TRACE_IRQS_OFF
e2f6bc25
AH
1161 jmp paranoid_userspace
1162paranoid_schedule:
2601e64d 1163 TRACE_IRQS_ON
72fe4858 1164 ENABLE_INTERRUPTS(CLBR_ANY)
2601e64d 1165 call schedule
72fe4858 1166 DISABLE_INTERRUPTS(CLBR_ANY)
2601e64d 1167 TRACE_IRQS_OFF
e2f6bc25 1168 jmp paranoid_userspace
2601e64d 1169 CFI_ENDPROC
e2f6bc25 1170END(paranoid_exit)
2601e64d 1171
1da177e4 1172/*
d99015b1
AH
1173 * Exception entry point. This expects an error code/orig_rax on the stack.
1174 * returns in "no swapgs flag" in %ebx.
0bd7b798 1175 */
d28c4393 1176KPROBE_ENTRY(error_entry)
dcd072e2 1177 XCPT_FRAME
d99015b1
AH
1178 CFI_ADJUST_CFA_OFFSET 15*8
1179 /* oldrax contains error code */
0bd7b798 1180 cld
14ae22ba
IM
1181 movq_cfi rdi, RDI+8
1182 movq_cfi rsi, RSI+8
1183 movq_cfi rdx, RDX+8
1184 movq_cfi rcx, RCX+8
1185 movq_cfi rax, RAX+8
1186 movq_cfi r8, R8+8
1187 movq_cfi r9, R9+8
1188 movq_cfi r10, R10+8
1189 movq_cfi r11, R11+8
1190 movq_cfi rbx, RBX+8
1191 movq_cfi rbp, RBP+8
1192 movq_cfi r12, R12+8
1193 movq_cfi r13, R13+8
1194 movq_cfi r14, R14+8
1195 movq_cfi r15, R15+8
0bd7b798 1196 xorl %ebx,%ebx
d99015b1
AH
1197 testl $3,CS+8(%rsp)
1198 je error_kernelspace
0bd7b798 1199error_swapgs:
72fe4858 1200 SWAPGS
6b11d4ef
AH
1201error_sti:
1202 TRACE_IRQS_OFF
d99015b1
AH
1203 ret
1204 CFI_ENDPROC
1205
1206/*
1207 * There are two places in the kernel that can potentially fault with
1208 * usergs. Handle them here. The exception handlers after iret run with
1209 * kernel gs again, so don't set the user space flag. B stepping K8s
1210 * sometimes report an truncated RIP for IRET exceptions returning to
1211 * compat mode. Check for these here too.
1212 */
1213error_kernelspace:
1214 incl %ebx
1215 leaq irq_return(%rip),%rcx
1216 cmpq %rcx,RIP+8(%rsp)
1217 je error_swapgs
1218 movl %ecx,%ecx /* zero extend */
1219 cmpq %rcx,RIP+8(%rsp)
1220 je error_swapgs
1221 cmpq $gs_change,RIP+8(%rsp)
1222 je error_swapgs
1223 jmp error_sti
1224KPROBE_END(error_entry)
1225
1226
1227/* ebx: no swapgs flag (1: don't need swapgs, 0: need it) */
1228KPROBE_ENTRY(error_exit)
dcd072e2 1229 DEFAULT_FRAME
10cd706d 1230 movl %ebx,%eax
1da177e4 1231 RESTORE_REST
72fe4858 1232 DISABLE_INTERRUPTS(CLBR_NONE)
2601e64d 1233 TRACE_IRQS_OFF
0bd7b798 1234 GET_THREAD_INFO(%rcx)
1da177e4 1235 testl %eax,%eax
d99015b1 1236 jne retint_kernel
10cd706d 1237 LOCKDEP_SYS_EXIT_IRQ
d99015b1
AH
1238 movl TI_flags(%rcx),%edx
1239 movl $_TIF_WORK_MASK,%edi
1240 andl %edi,%edx
1241 jnz retint_careful
10cd706d 1242 jmp retint_swapgs
1da177e4 1243 CFI_ENDPROC
d99015b1 1244KPROBE_END(error_exit)
0bd7b798 1245
1da177e4 1246 /* Reload gs selector with exception handling */
0bd7b798 1247 /* edi: new selector */
9f9d489a 1248ENTRY(native_load_gs_index)
7effaa88 1249 CFI_STARTPROC
1da177e4 1250 pushf
7effaa88 1251 CFI_ADJUST_CFA_OFFSET 8
72fe4858
GOC
1252 DISABLE_INTERRUPTS(CLBR_ANY | ~(CLBR_RDI))
1253 SWAPGS
0bd7b798
AH
1254gs_change:
1255 movl %edi,%gs
1da177e4 12562: mfence /* workaround */
72fe4858 1257 SWAPGS
1da177e4 1258 popf
7effaa88 1259 CFI_ADJUST_CFA_OFFSET -8
1da177e4 1260 ret
7effaa88 1261 CFI_ENDPROC
9f9d489a 1262ENDPROC(native_load_gs_index)
0bd7b798 1263
1da177e4
LT
1264 .section __ex_table,"a"
1265 .align 8
1266 .quad gs_change,bad_gs
1267 .previous
1268 .section .fixup,"ax"
1269 /* running with kernelgs */
0bd7b798 1270bad_gs:
72fe4858 1271 SWAPGS /* switch back to user gs */
1da177e4
LT
1272 xorl %eax,%eax
1273 movl %eax,%gs
1274 jmp 2b
0bd7b798
AH
1275 .previous
1276
1da177e4
LT
1277/*
1278 * Create a kernel thread.
1279 *
1280 * C extern interface:
1281 * extern long kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
1282 *
1283 * asm input arguments:
1284 * rdi: fn, rsi: arg, rdx: flags
1285 */
1286ENTRY(kernel_thread)
1287 CFI_STARTPROC
1288 FAKE_STACK_FRAME $child_rip
1289 SAVE_ALL
1290
1291 # rdi: flags, rsi: usp, rdx: will be &pt_regs
1292 movq %rdx,%rdi
1293 orq kernel_thread_flags(%rip),%rdi
1294 movq $-1, %rsi
1295 movq %rsp, %rdx
1296
1297 xorl %r8d,%r8d
1298 xorl %r9d,%r9d
0bd7b798 1299
1da177e4
LT
1300 # clone now
1301 call do_fork
1302 movq %rax,RAX(%rsp)
1303 xorl %edi,%edi
1304
1305 /*
1306 * It isn't worth to check for reschedule here,
1307 * so internally to the x86_64 port you can rely on kernel_thread()
1308 * not to reschedule the child before returning, this avoids the need
1309 * of hacks for example to fork off the per-CPU idle tasks.
0bd7b798 1310 * [Hopefully no generic code relies on the reschedule -AK]
1da177e4
LT
1311 */
1312 RESTORE_ALL
1313 UNFAKE_STACK_FRAME
1314 ret
1315 CFI_ENDPROC
4b787e0b 1316ENDPROC(kernel_thread)
0bd7b798 1317
1da177e4 1318child_rip:
c05991ed
AK
1319 pushq $0 # fake return address
1320 CFI_STARTPROC
1da177e4
LT
1321 /*
1322 * Here we are in the child and the registers are set as they were
1323 * at kernel_thread() invocation in the parent.
1324 */
1325 movq %rdi, %rax
1326 movq %rsi, %rdi
1327 call *%rax
1328 # exit
1c5b5cfd 1329 mov %eax, %edi
1da177e4 1330 call do_exit
c05991ed 1331 CFI_ENDPROC
4b787e0b 1332ENDPROC(child_rip)
1da177e4
LT
1333
1334/*
1335 * execve(). This function needs to use IRET, not SYSRET, to set up all state properly.
1336 *
1337 * C extern interface:
1338 * extern long execve(char *name, char **argv, char **envp)
1339 *
1340 * asm input arguments:
1341 * rdi: name, rsi: argv, rdx: envp
1342 *
1343 * We want to fallback into:
5d119b2c 1344 * extern long sys_execve(char *name, char **argv,char **envp, struct pt_regs *regs)
1da177e4
LT
1345 *
1346 * do_sys_execve asm fallback arguments:
5d119b2c 1347 * rdi: name, rsi: argv, rdx: envp, rcx: fake frame on the stack
1da177e4 1348 */
3db03b4a 1349ENTRY(kernel_execve)
1da177e4
LT
1350 CFI_STARTPROC
1351 FAKE_STACK_FRAME $0
0bd7b798 1352 SAVE_ALL
5d119b2c 1353 movq %rsp,%rcx
1da177e4 1354 call sys_execve
0bd7b798 1355 movq %rax, RAX(%rsp)
1da177e4
LT
1356 RESTORE_REST
1357 testq %rax,%rax
1358 je int_ret_from_sys_call
1359 RESTORE_ARGS
1360 UNFAKE_STACK_FRAME
1361 ret
1362 CFI_ENDPROC
3db03b4a 1363ENDPROC(kernel_execve)
1da177e4 1364
1da177e4 1365
1da177e4 1366
0bd7b798 1367 /* runs on exception stack */
eddb6fb9 1368KPROBE_ENTRY(nmi)
7effaa88 1369 INTR_FRAME
09402947 1370 PARAVIRT_ADJUST_EXCEPTION_FRAME
e2f6bc25
AH
1371 pushq_cfi $-1
1372 subq $15*8, %rsp
1373 CFI_ADJUST_CFA_OFFSET 15*8
1374 call save_paranoid
1375 DEFAULT_FRAME 0
1376 /* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */
1377 movq %rsp,%rdi
b8b1d08b 1378 movq $-1,%rsi
e2f6bc25 1379 call do_nmi
2601e64d 1380#ifdef CONFIG_TRACE_IRQFLAGS
e2f6bc25
AH
1381 /* paranoidexit; without TRACE_IRQS_OFF */
1382 /* ebx: no swapgs flag */
b8b1d08b 1383 DISABLE_INTERRUPTS(CLBR_NONE)
e2f6bc25
AH
1384 testl %ebx,%ebx /* swapgs needed? */
1385 jnz nmi_restore
1386 testl $3,CS(%rsp)
1387 jnz nmi_userspace
1388nmi_swapgs:
1389 SWAPGS_UNSAFE_STACK
1390nmi_restore:
1391 RESTORE_ALL 8
1392 jmp irq_return
1393nmi_userspace:
1394 GET_THREAD_INFO(%rcx)
1395 movl TI_flags(%rcx),%ebx
1396 andl $_TIF_WORK_MASK,%ebx
1397 jz nmi_swapgs
1398 movq %rsp,%rdi /* &pt_regs */
1399 call sync_regs
1400 movq %rax,%rsp /* switch stack for scheduling */
1401 testl $_TIF_NEED_RESCHED,%ebx
1402 jnz nmi_schedule
1403 movl %ebx,%edx /* arg3: thread flags */
1404 ENABLE_INTERRUPTS(CLBR_NONE)
1405 xorl %esi,%esi /* arg2: oldset */
1406 movq %rsp,%rdi /* arg1: &pt_regs */
1407 call do_notify_resume
1408 DISABLE_INTERRUPTS(CLBR_NONE)
1409 jmp nmi_userspace
1410nmi_schedule:
1411 ENABLE_INTERRUPTS(CLBR_ANY)
1412 call schedule
1413 DISABLE_INTERRUPTS(CLBR_ANY)
1414 jmp nmi_userspace
1415 CFI_ENDPROC
2601e64d 1416#else
e2f6bc25 1417 jmp paranoid_exit
2601e64d
IM
1418 CFI_ENDPROC
1419#endif
d28c4393 1420KPROBE_END(nmi)
6fefb0d1 1421
2699500b 1422/* Call softirq on interrupt stack. Interrupts are off. */
ed6b676c 1423ENTRY(call_softirq)
7effaa88 1424 CFI_STARTPROC
2699500b
AK
1425 push %rbp
1426 CFI_ADJUST_CFA_OFFSET 8
1427 CFI_REL_OFFSET rbp,0
1428 mov %rsp,%rbp
1429 CFI_DEF_CFA_REGISTER rbp
ed6b676c 1430 incl %gs:pda_irqcount
2699500b
AK
1431 cmove %gs:pda_irqstackptr,%rsp
1432 push %rbp # backlink for old unwinder
ed6b676c 1433 call __do_softirq
2699500b 1434 leaveq
7effaa88 1435 CFI_DEF_CFA_REGISTER rsp
2699500b 1436 CFI_ADJUST_CFA_OFFSET -8
ed6b676c 1437 decl %gs:pda_irqcount
ed6b676c 1438 ret
7effaa88 1439 CFI_ENDPROC
4b787e0b 1440ENDPROC(call_softirq)
75154f40
AK
1441
1442KPROBE_ENTRY(ignore_sysret)
1443 CFI_STARTPROC
1444 mov $-ENOSYS,%eax
1445 sysret
1446 CFI_ENDPROC
1447ENDPROC(ignore_sysret)
3d75e1b8
JF
1448
1449#ifdef CONFIG_XEN
322648d1 1450zeroentry xen_hypervisor_callback xen_do_hypervisor_callback
3d75e1b8
JF
1451
1452/*
1453# A note on the "critical region" in our callback handler.
1454# We want to avoid stacking callback handlers due to events occurring
1455# during handling of the last event. To do this, we keep events disabled
1456# until we've done all processing. HOWEVER, we must enable events before
1457# popping the stack frame (can't be done atomically) and so it would still
1458# be possible to get enough handler activations to overflow the stack.
1459# Although unlikely, bugs of that kind are hard to track down, so we'd
1460# like to avoid the possibility.
1461# So, on entry to the handler we detect whether we interrupted an
1462# existing activation in its critical region -- if so, we pop the current
1463# activation and restart the handler using the previous one.
1464*/
1465ENTRY(xen_do_hypervisor_callback) # do_hypervisor_callback(struct *pt_regs)
1466 CFI_STARTPROC
1467/* Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
1468 see the correct pointer to the pt_regs */
1469 movq %rdi, %rsp # we don't return, adjust the stack frame
1470 CFI_ENDPROC
dcd072e2 1471 DEFAULT_FRAME
3d75e1b8
JF
147211: incl %gs:pda_irqcount
1473 movq %rsp,%rbp
1474 CFI_DEF_CFA_REGISTER rbp
1475 cmovzq %gs:pda_irqstackptr,%rsp
1476 pushq %rbp # backlink for old unwinder
1477 call xen_evtchn_do_upcall
1478 popq %rsp
1479 CFI_DEF_CFA_REGISTER rsp
1480 decl %gs:pda_irqcount
1481 jmp error_exit
1482 CFI_ENDPROC
1483END(do_hypervisor_callback)
1484
1485/*
1486# Hypervisor uses this for application faults while it executes.
1487# We get here for two reasons:
1488# 1. Fault while reloading DS, ES, FS or GS
1489# 2. Fault while executing IRET
1490# Category 1 we do not need to fix up as Xen has already reloaded all segment
1491# registers that could be reloaded and zeroed the others.
1492# Category 2 we fix up by killing the current process. We cannot use the
1493# normal Linux return path in this case because if we use the IRET hypercall
1494# to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1495# We distinguish between categories by comparing each saved segment register
1496# with its current contents: any discrepancy means we in category 1.
1497*/
1498ENTRY(xen_failsafe_callback)
dcd072e2
AH
1499 INTR_FRAME 1 (6*8)
1500 /*CFI_REL_OFFSET gs,GS*/
1501 /*CFI_REL_OFFSET fs,FS*/
1502 /*CFI_REL_OFFSET es,ES*/
1503 /*CFI_REL_OFFSET ds,DS*/
1504 CFI_REL_OFFSET r11,8
1505 CFI_REL_OFFSET rcx,0
3d75e1b8
JF
1506 movw %ds,%cx
1507 cmpw %cx,0x10(%rsp)
1508 CFI_REMEMBER_STATE
1509 jne 1f
1510 movw %es,%cx
1511 cmpw %cx,0x18(%rsp)
1512 jne 1f
1513 movw %fs,%cx
1514 cmpw %cx,0x20(%rsp)
1515 jne 1f
1516 movw %gs,%cx
1517 cmpw %cx,0x28(%rsp)
1518 jne 1f
1519 /* All segments match their saved values => Category 2 (Bad IRET). */
1520 movq (%rsp),%rcx
1521 CFI_RESTORE rcx
1522 movq 8(%rsp),%r11
1523 CFI_RESTORE r11
1524 addq $0x30,%rsp
1525 CFI_ADJUST_CFA_OFFSET -0x30
14ae22ba
IM
1526 pushq_cfi $0 /* RIP */
1527 pushq_cfi %r11
1528 pushq_cfi %rcx
4a5c3e77 1529 jmp general_protection
3d75e1b8
JF
1530 CFI_RESTORE_STATE
15311: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
1532 movq (%rsp),%rcx
1533 CFI_RESTORE rcx
1534 movq 8(%rsp),%r11
1535 CFI_RESTORE r11
1536 addq $0x30,%rsp
1537 CFI_ADJUST_CFA_OFFSET -0x30
14ae22ba 1538 pushq_cfi $0
3d75e1b8
JF
1539 SAVE_ALL
1540 jmp error_exit
1541 CFI_ENDPROC
3d75e1b8
JF
1542END(xen_failsafe_callback)
1543
1544#endif /* CONFIG_XEN */