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