blob: 4a16bf31c78353452514e526d66169555ddaa5c4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
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.
14 *
15 * Normal syscalls and interrupts don't save a full stack frame, this is
16 * only done for syscall tracing, signals or fork/exec et.al.
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.
21 * - partial stack frame: partially saved registers upto R11.
22 * - full stack frame: Like partial stack frame, but all register saved.
Andi Kleen2e91a172006-09-26 10:52:29 +020023 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 */
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/linkage.h>
41#include <asm/segment.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/cache.h>
43#include <asm/errno.h>
44#include <asm/dwarf2.h>
45#include <asm/calling.h>
Sam Ravnborge2d5df92005-09-09 21:28:48 +020046#include <asm/asm-offsets.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/msr.h>
48#include <asm/unistd.h>
49#include <asm/thread_info.h>
50#include <asm/hw_irq.h>
Andi Kleen5f8efbb2006-01-16 01:56:39 +010051#include <asm/page.h>
Ingo Molnar2601e642006-07-03 00:24:45 -070052#include <asm/irqflags.h>
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +010053#include <asm/paravirt.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053054#include <asm/ftrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Roland McGrath86a1c342008-06-23 15:37:04 -070056/* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 .code64
63
Steven Rostedt606576c2008-10-06 19:06:12 -040064#ifdef CONFIG_FUNCTION_TRACER
Steven Rostedtd61f82d2008-05-12 21:20:43 +020065#ifdef CONFIG_DYNAMIC_FTRACE
66ENTRY(mcount)
Steven Rostedtd61f82d2008-05-12 21:20:43 +020067 retq
68END(mcount)
69
70ENTRY(ftrace_caller)
71
72 /* taken from glibc */
73 subq $0x38, %rsp
74 movq %rax, (%rsp)
75 movq %rcx, 8(%rsp)
76 movq %rdx, 16(%rsp)
77 movq %rsi, 24(%rsp)
78 movq %rdi, 32(%rsp)
79 movq %r8, 40(%rsp)
80 movq %r9, 48(%rsp)
81
82 movq 0x38(%rsp), %rdi
83 movq 8(%rbp), %rsi
Abhishek Sagar395a59d2008-06-21 23:47:27 +053084 subq $MCOUNT_INSN_SIZE, %rdi
Steven Rostedtd61f82d2008-05-12 21:20:43 +020085
86.globl ftrace_call
87ftrace_call:
88 call ftrace_stub
89
90 movq 48(%rsp), %r9
91 movq 40(%rsp), %r8
92 movq 32(%rsp), %rdi
93 movq 24(%rsp), %rsi
94 movq 16(%rsp), %rdx
95 movq 8(%rsp), %rcx
96 movq (%rsp), %rax
97 addq $0x38, %rsp
98
99.globl ftrace_stub
100ftrace_stub:
101 retq
102END(ftrace_caller)
103
104#else /* ! CONFIG_DYNAMIC_FTRACE */
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200105ENTRY(mcount)
106 cmpq $ftrace_stub, ftrace_trace_function
107 jnz trace
108.globl ftrace_stub
109ftrace_stub:
110 retq
111
112trace:
113 /* taken from glibc */
114 subq $0x38, %rsp
115 movq %rax, (%rsp)
116 movq %rcx, 8(%rsp)
117 movq %rdx, 16(%rsp)
118 movq %rsi, 24(%rsp)
119 movq %rdi, 32(%rsp)
120 movq %r8, 40(%rsp)
121 movq %r9, 48(%rsp)
122
123 movq 0x38(%rsp), %rdi
124 movq 8(%rbp), %rsi
Abhishek Sagar395a59d2008-06-21 23:47:27 +0530125 subq $MCOUNT_INSN_SIZE, %rdi
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200126
127 call *ftrace_trace_function
128
129 movq 48(%rsp), %r9
130 movq 40(%rsp), %r8
131 movq 32(%rsp), %rdi
132 movq 24(%rsp), %rsi
133 movq 16(%rsp), %rdx
134 movq 8(%rsp), %rcx
135 movq (%rsp), %rax
136 addq $0x38, %rsp
137
138 jmp ftrace_stub
139END(mcount)
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200140#endif /* CONFIG_DYNAMIC_FTRACE */
Steven Rostedt606576c2008-10-06 19:06:12 -0400141#endif /* CONFIG_FUNCTION_TRACER */
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200142
Andi Kleendc37db42005-04-16 15:25:05 -0700143#ifndef CONFIG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144#define retint_kernel retint_restore_args
145#endif
Ingo Molnar2601e642006-07-03 00:24:45 -0700146
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100147#ifdef CONFIG_PARAVIRT
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -0400148ENTRY(native_usergs_sysret64)
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100149 swapgs
150 sysretq
151#endif /* CONFIG_PARAVIRT */
152
Ingo Molnar2601e642006-07-03 00:24:45 -0700153
154.macro TRACE_IRQS_IRETQ offset=ARGOFFSET
155#ifdef CONFIG_TRACE_IRQFLAGS
156 bt $9,EFLAGS-\offset(%rsp) /* interrupts off? */
157 jnc 1f
158 TRACE_IRQS_ON
1591:
160#endif
161.endm
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/*
164 * C code is not supposed to know about undefined top of stack. Every time
165 * a C function with an pt_regs argument is called from the SYSCALL based
166 * fast path FIXUP_TOP_OF_STACK is needed.
167 * RESTORE_TOP_OF_STACK syncs the syscall state after any possible ptregs
168 * manipulation.
169 */
170
171 /* %rsp:at FRAMEEND */
172 .macro FIXUP_TOP_OF_STACK tmp
173 movq %gs:pda_oldrsp,\tmp
174 movq \tmp,RSP(%rsp)
175 movq $__USER_DS,SS(%rsp)
176 movq $__USER_CS,CS(%rsp)
177 movq $-1,RCX(%rsp)
178 movq R11(%rsp),\tmp /* get eflags */
179 movq \tmp,EFLAGS(%rsp)
180 .endm
181
182 .macro RESTORE_TOP_OF_STACK tmp,offset=0
183 movq RSP-\offset(%rsp),\tmp
184 movq \tmp,%gs:pda_oldrsp
185 movq EFLAGS-\offset(%rsp),\tmp
186 movq \tmp,R11-\offset(%rsp)
187 .endm
188
189 .macro FAKE_STACK_FRAME child_rip
190 /* push in order ss, rsp, eflags, cs, rip */
Andi Kleen3829ee62005-07-28 21:15:48 -0700191 xorl %eax, %eax
Jeremy Fitzhardingee04e0a62008-06-25 00:19:25 -0400192 pushq $__KERNEL_DS /* ss */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 CFI_ADJUST_CFA_OFFSET 8
Jan Beulich7effaa82005-09-12 18:49:24 +0200194 /*CFI_REL_OFFSET ss,0*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 pushq %rax /* rsp */
196 CFI_ADJUST_CFA_OFFSET 8
Jan Beulich7effaa82005-09-12 18:49:24 +0200197 CFI_REL_OFFSET rsp,0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 pushq $(1<<9) /* eflags - interrupts on */
199 CFI_ADJUST_CFA_OFFSET 8
Jan Beulich7effaa82005-09-12 18:49:24 +0200200 /*CFI_REL_OFFSET rflags,0*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 pushq $__KERNEL_CS /* cs */
202 CFI_ADJUST_CFA_OFFSET 8
Jan Beulich7effaa82005-09-12 18:49:24 +0200203 /*CFI_REL_OFFSET cs,0*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 pushq \child_rip /* rip */
205 CFI_ADJUST_CFA_OFFSET 8
Jan Beulich7effaa82005-09-12 18:49:24 +0200206 CFI_REL_OFFSET rip,0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 pushq %rax /* orig rax */
208 CFI_ADJUST_CFA_OFFSET 8
209 .endm
210
211 .macro UNFAKE_STACK_FRAME
212 addq $8*6, %rsp
213 CFI_ADJUST_CFA_OFFSET -(6*8)
214 .endm
215
Jan Beulich7effaa82005-09-12 18:49:24 +0200216 .macro CFI_DEFAULT_STACK start=1
217 .if \start
218 CFI_STARTPROC simple
Jan Beulichadf14232006-09-26 10:52:41 +0200219 CFI_SIGNAL_FRAME
Jan Beulich7effaa82005-09-12 18:49:24 +0200220 CFI_DEF_CFA rsp,SS+8
221 .else
222 CFI_DEF_CFA_OFFSET SS+8
223 .endif
224 CFI_REL_OFFSET r15,R15
225 CFI_REL_OFFSET r14,R14
226 CFI_REL_OFFSET r13,R13
227 CFI_REL_OFFSET r12,R12
228 CFI_REL_OFFSET rbp,RBP
229 CFI_REL_OFFSET rbx,RBX
230 CFI_REL_OFFSET r11,R11
231 CFI_REL_OFFSET r10,R10
232 CFI_REL_OFFSET r9,R9
233 CFI_REL_OFFSET r8,R8
234 CFI_REL_OFFSET rax,RAX
235 CFI_REL_OFFSET rcx,RCX
236 CFI_REL_OFFSET rdx,RDX
237 CFI_REL_OFFSET rsi,RSI
238 CFI_REL_OFFSET rdi,RDI
239 CFI_REL_OFFSET rip,RIP
240 /*CFI_REL_OFFSET cs,CS*/
241 /*CFI_REL_OFFSET rflags,EFLAGS*/
242 CFI_REL_OFFSET rsp,RSP
243 /*CFI_REL_OFFSET ss,SS*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 .endm
245/*
246 * A newly forked process directly context switches into this.
247 */
248/* rdi: prev */
249ENTRY(ret_from_fork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 CFI_DEFAULT_STACK
Andi Kleen658fdbe2006-09-26 10:52:41 +0200251 push kernel_eflags(%rip)
Alexander van Heukelume0a5a5d2008-07-22 18:14:16 +0200252 CFI_ADJUST_CFA_OFFSET 8
Andi Kleen658fdbe2006-09-26 10:52:41 +0200253 popf # reset kernel eflags
Alexander van Heukelume0a5a5d2008-07-22 18:14:16 +0200254 CFI_ADJUST_CFA_OFFSET -8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 call schedule_tail
256 GET_THREAD_INFO(%rcx)
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300257 testl $(_TIF_SYSCALL_TRACE|_TIF_SYSCALL_AUDIT),TI_flags(%rcx)
Jan Beulich1cbd8b32008-10-30 10:45:36 +0000258 CFI_REMEMBER_STATE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 jnz rff_trace
260rff_action:
261 RESTORE_REST
262 testl $3,CS-ARGOFFSET(%rsp) # from kernel_thread?
263 je int_ret_from_sys_call
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300264 testl $_TIF_IA32,TI_flags(%rcx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 jnz int_ret_from_sys_call
266 RESTORE_TOP_OF_STACK %rdi,ARGOFFSET
267 jmp ret_from_sys_call
Jan Beulich1cbd8b32008-10-30 10:45:36 +0000268 CFI_RESTORE_STATE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269rff_trace:
270 movq %rsp,%rdi
271 call syscall_trace_leave
272 GET_THREAD_INFO(%rcx)
273 jmp rff_action
274 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200275END(ret_from_fork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277/*
278 * System call entry. Upto 6 arguments in registers are supported.
279 *
280 * SYSCALL does not save anything on the stack and does not change the
281 * stack pointer.
282 */
283
284/*
285 * Register setup:
286 * rax system call number
287 * rdi arg0
288 * rcx return address for syscall/sysret, C arg3
289 * rsi arg1
290 * rdx arg2
291 * r10 arg3 (--> moved to rcx for C)
292 * r8 arg4
293 * r9 arg5
294 * r11 eflags for syscall/sysret, temporary for C
295 * r12-r15,rbp,rbx saved by C code, not touched.
296 *
297 * Interrupts are off on entry.
298 * Only called from user space.
299 *
300 * XXX if we had a free scratch register we could save the RSP into the stack frame
301 * and report it properly in ps. Unfortunately we haven't.
Andi Kleen7bf36bb2006-04-07 19:50:00 +0200302 *
303 * When user can change the frames always force IRET. That is because
304 * it deals with uncanonical addresses better. SYSRET has trouble
305 * with them due to bugs in both AMD and Intel CPUs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 */
307
308ENTRY(system_call)
Jan Beulich7effaa82005-09-12 18:49:24 +0200309 CFI_STARTPROC simple
Jan Beulichadf14232006-09-26 10:52:41 +0200310 CFI_SIGNAL_FRAME
Jan Beulichdffead42006-06-26 13:57:38 +0200311 CFI_DEF_CFA rsp,PDA_STACKOFFSET
Jan Beulich7effaa82005-09-12 18:49:24 +0200312 CFI_REGISTER rip,rcx
313 /*CFI_REGISTER rflags,r11*/
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100314 SWAPGS_UNSAFE_STACK
315 /*
316 * A hypervisor implementation might want to use a label
317 * after the swapgs, so that it can do the swapgs
318 * for the guest and jump here on syscall.
319 */
320ENTRY(system_call_after_swapgs)
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 movq %rsp,%gs:pda_oldrsp
323 movq %gs:pda_kernelstack,%rsp
Ingo Molnar2601e642006-07-03 00:24:45 -0700324 /*
325 * No need to follow this irqs off/on section - it's straight
326 * and short:
327 */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100328 ENABLE_INTERRUPTS(CLBR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 SAVE_ARGS 8,1
330 movq %rax,ORIG_RAX-ARGOFFSET(%rsp)
Jan Beulich7effaa82005-09-12 18:49:24 +0200331 movq %rcx,RIP-ARGOFFSET(%rsp)
332 CFI_REL_OFFSET rip,RIP-ARGOFFSET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 GET_THREAD_INFO(%rcx)
Roland McGrathd4d67152008-07-09 02:38:07 -0700334 testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags(%rcx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 jnz tracesys
Roland McGrath86a1c342008-06-23 15:37:04 -0700336system_call_fastpath:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 cmpq $__NR_syscall_max,%rax
338 ja badsys
339 movq %r10,%rcx
340 call *sys_call_table(,%rax,8) # XXX: rip relative
341 movq %rax,RAX-ARGOFFSET(%rsp)
342/*
343 * Syscall return path ending with SYSRET (fast path)
344 * Has incomplete stack frame and undefined top of stack.
345 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346ret_from_sys_call:
Andi Kleen11b854b2005-04-16 15:25:02 -0700347 movl $_TIF_ALLWORK_MASK,%edi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 /* edi: flagmask */
349sysret_check:
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200350 LOCKDEP_SYS_EXIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 GET_THREAD_INFO(%rcx)
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100352 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700353 TRACE_IRQS_OFF
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300354 movl TI_flags(%rcx),%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 andl %edi,%edx
356 jnz sysret_careful
Jan Beulichbcddc012006-12-07 02:14:02 +0100357 CFI_REMEMBER_STATE
Ingo Molnar2601e642006-07-03 00:24:45 -0700358 /*
359 * sysretq will re-enable interrupts:
360 */
361 TRACE_IRQS_ON
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 movq RIP-ARGOFFSET(%rsp),%rcx
Jan Beulich7effaa82005-09-12 18:49:24 +0200363 CFI_REGISTER rip,rcx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 RESTORE_ARGS 0,-ARG_SKIP,1
Jan Beulich7effaa82005-09-12 18:49:24 +0200365 /*CFI_REGISTER rflags,r11*/
Jeremy Fitzhardingec7245da2008-06-25 00:19:27 -0400366 movq %gs:pda_oldrsp, %rsp
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -0400367 USERGS_SYSRET64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Jan Beulichbcddc012006-12-07 02:14:02 +0100369 CFI_RESTORE_STATE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 /* Handle reschedules */
371 /* edx: work, edi: workmask */
372sysret_careful:
373 bt $TIF_NEED_RESCHED,%edx
374 jnc sysret_signal
Ingo Molnar2601e642006-07-03 00:24:45 -0700375 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100376 ENABLE_INTERRUPTS(CLBR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 pushq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200378 CFI_ADJUST_CFA_OFFSET 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 call schedule
380 popq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200381 CFI_ADJUST_CFA_OFFSET -8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 jmp sysret_check
383
384 /* Handle a signal */
385sysret_signal:
Ingo Molnar2601e642006-07-03 00:24:45 -0700386 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100387 ENABLE_INTERRUPTS(CLBR_NONE)
Roland McGrath86a1c342008-06-23 15:37:04 -0700388#ifdef CONFIG_AUDITSYSCALL
389 bt $TIF_SYSCALL_AUDIT,%edx
390 jc sysret_audit
391#endif
Andi Kleen10ffdbb2005-05-16 21:53:19 -0700392 /* edx: work flags (arg3) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 leaq do_notify_resume(%rip),%rax
394 leaq -ARGOFFSET(%rsp),%rdi # &pt_regs -> arg1
395 xorl %esi,%esi # oldset -> arg2
396 call ptregscall_common
Roland McGrath15e8f342008-06-23 20:41:12 -0700397 movl $_TIF_WORK_MASK,%edi
Andi Kleen7bf36bb2006-04-07 19:50:00 +0200398 /* Use IRET because user could have changed frame. This
399 works because ptregscall_common has called FIXUP_TOP_OF_STACK. */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100400 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700401 TRACE_IRQS_OFF
Andi Kleen7bf36bb2006-04-07 19:50:00 +0200402 jmp int_with_check
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Jan Beulich7effaa82005-09-12 18:49:24 +0200404badsys:
405 movq $-ENOSYS,RAX-ARGOFFSET(%rsp)
406 jmp ret_from_sys_call
407
Roland McGrath86a1c342008-06-23 15:37:04 -0700408#ifdef CONFIG_AUDITSYSCALL
409 /*
410 * Fast path for syscall audit without full syscall trace.
411 * We just call audit_syscall_entry() directly, and then
412 * jump back to the normal fast path.
413 */
414auditsys:
415 movq %r10,%r9 /* 6th arg: 4th syscall arg */
416 movq %rdx,%r8 /* 5th arg: 3rd syscall arg */
417 movq %rsi,%rcx /* 4th arg: 2nd syscall arg */
418 movq %rdi,%rdx /* 3rd arg: 1st syscall arg */
419 movq %rax,%rsi /* 2nd arg: syscall number */
420 movl $AUDIT_ARCH_X86_64,%edi /* 1st arg: audit arch */
421 call audit_syscall_entry
422 LOAD_ARGS 0 /* reload call-clobbered registers */
423 jmp system_call_fastpath
424
425 /*
426 * Return fast path for syscall audit. Call audit_syscall_exit()
427 * directly and then jump back to the fast path with TIF_SYSCALL_AUDIT
428 * masked off.
429 */
430sysret_audit:
431 movq %rax,%rsi /* second arg, syscall return value */
432 cmpq $0,%rax /* is it < 0? */
433 setl %al /* 1 if so, 0 if not */
434 movzbl %al,%edi /* zero-extend that into %edi */
435 inc %edi /* first arg, 0->1(AUDITSC_SUCCESS), 1->2(AUDITSC_FAILURE) */
436 call audit_syscall_exit
437 movl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT),%edi
438 jmp sysret_check
439#endif /* CONFIG_AUDITSYSCALL */
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 /* Do syscall tracing */
442tracesys:
Roland McGrath86a1c342008-06-23 15:37:04 -0700443#ifdef CONFIG_AUDITSYSCALL
444 testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT),TI_flags(%rcx)
445 jz auditsys
446#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 SAVE_REST
Roland McGratha31f8dd2008-03-16 21:59:11 -0700448 movq $-ENOSYS,RAX(%rsp) /* ptrace can change this for a bad syscall */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 FIXUP_TOP_OF_STACK %rdi
450 movq %rsp,%rdi
451 call syscall_trace_enter
Roland McGrathd4d67152008-07-09 02:38:07 -0700452 /*
453 * Reload arg registers from stack in case ptrace changed them.
454 * We don't reload %rax because syscall_trace_enter() returned
455 * the value it wants us to use in the table lookup.
456 */
457 LOAD_ARGS ARGOFFSET, 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 RESTORE_REST
459 cmpq $__NR_syscall_max,%rax
Roland McGratha31f8dd2008-03-16 21:59:11 -0700460 ja int_ret_from_sys_call /* RAX(%rsp) set to -ENOSYS above */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 movq %r10,%rcx /* fixup for C */
462 call *sys_call_table(,%rax,8)
Roland McGratha31f8dd2008-03-16 21:59:11 -0700463 movq %rax,RAX-ARGOFFSET(%rsp)
Andi Kleen7bf36bb2006-04-07 19:50:00 +0200464 /* Use IRET because user could have changed frame */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466/*
467 * Syscall return path ending with IRET.
468 * Has correct top of stack, but partial stack frame.
Jan Beulichbcddc012006-12-07 02:14:02 +0100469 */
470 .globl int_ret_from_sys_call
Roland McGrath5cbf1562008-06-24 01:13:31 -0700471 .globl int_with_check
Jan Beulichbcddc012006-12-07 02:14:02 +0100472int_ret_from_sys_call:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100473 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700474 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 testl $3,CS-ARGOFFSET(%rsp)
476 je retint_restore_args
477 movl $_TIF_ALLWORK_MASK,%edi
478 /* edi: mask to check */
479int_with_check:
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200480 LOCKDEP_SYS_EXIT_IRQ
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 GET_THREAD_INFO(%rcx)
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300482 movl TI_flags(%rcx),%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 andl %edi,%edx
484 jnz int_careful
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300485 andl $~TS_COMPAT,TI_status(%rcx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 jmp retint_swapgs
487
488 /* Either reschedule or signal or syscall exit tracking needed. */
489 /* First do a reschedule test. */
490 /* edx: work, edi: workmask */
491int_careful:
492 bt $TIF_NEED_RESCHED,%edx
493 jnc int_very_careful
Ingo Molnar2601e642006-07-03 00:24:45 -0700494 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100495 ENABLE_INTERRUPTS(CLBR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 pushq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200497 CFI_ADJUST_CFA_OFFSET 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 call schedule
499 popq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200500 CFI_ADJUST_CFA_OFFSET -8
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100501 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700502 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 jmp int_with_check
504
505 /* handle signals and tracing -- both require a full stack frame */
506int_very_careful:
Ingo Molnar2601e642006-07-03 00:24:45 -0700507 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100508 ENABLE_INTERRUPTS(CLBR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 SAVE_REST
510 /* Check for syscall exit trace */
Roland McGrathd4d67152008-07-09 02:38:07 -0700511 testl $_TIF_WORK_SYSCALL_EXIT,%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 jz int_signal
513 pushq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200514 CFI_ADJUST_CFA_OFFSET 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 leaq 8(%rsp),%rdi # &ptregs -> arg1
516 call syscall_trace_leave
517 popq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200518 CFI_ADJUST_CFA_OFFSET -8
Roland McGrathd4d67152008-07-09 02:38:07 -0700519 andl $~(_TIF_WORK_SYSCALL_EXIT|_TIF_SYSCALL_EMU),%edi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 jmp int_restore_rest
521
522int_signal:
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100523 testl $_TIF_DO_NOTIFY_MASK,%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 jz 1f
525 movq %rsp,%rdi # &ptregs -> arg1
526 xorl %esi,%esi # oldset -> arg2
527 call do_notify_resume
Roland McGratheca91e72008-07-10 14:50:39 -07005281: movl $_TIF_WORK_MASK,%edi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529int_restore_rest:
530 RESTORE_REST
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100531 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700532 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 jmp int_with_check
534 CFI_ENDPROC
Jan Beulichbcddc012006-12-07 02:14:02 +0100535END(system_call)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537/*
538 * Certain special system calls that need to save a complete full stack frame.
539 */
540
541 .macro PTREGSCALL label,func,arg
542 .globl \label
543\label:
544 leaq \func(%rip),%rax
545 leaq -ARGOFFSET+8(%rsp),\arg /* 8 for return address */
546 jmp ptregscall_common
Jan Beulich4b787e02006-06-26 13:56:55 +0200547END(\label)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 .endm
549
Jan Beulich7effaa82005-09-12 18:49:24 +0200550 CFI_STARTPROC
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 PTREGSCALL stub_clone, sys_clone, %r8
553 PTREGSCALL stub_fork, sys_fork, %rdi
554 PTREGSCALL stub_vfork, sys_vfork, %rdi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 PTREGSCALL stub_sigaltstack, sys_sigaltstack, %rdx
556 PTREGSCALL stub_iopl, sys_iopl, %rsi
557
558ENTRY(ptregscall_common)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 popq %r11
Jan Beulich7effaa82005-09-12 18:49:24 +0200560 CFI_ADJUST_CFA_OFFSET -8
561 CFI_REGISTER rip, r11
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 SAVE_REST
563 movq %r11, %r15
Jan Beulich7effaa82005-09-12 18:49:24 +0200564 CFI_REGISTER rip, r15
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 FIXUP_TOP_OF_STACK %r11
566 call *%rax
567 RESTORE_TOP_OF_STACK %r11
568 movq %r15, %r11
Jan Beulich7effaa82005-09-12 18:49:24 +0200569 CFI_REGISTER rip, r11
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 RESTORE_REST
571 pushq %r11
Jan Beulich7effaa82005-09-12 18:49:24 +0200572 CFI_ADJUST_CFA_OFFSET 8
573 CFI_REL_OFFSET rip, 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 ret
575 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200576END(ptregscall_common)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578ENTRY(stub_execve)
579 CFI_STARTPROC
580 popq %r11
Jan Beulich7effaa82005-09-12 18:49:24 +0200581 CFI_ADJUST_CFA_OFFSET -8
582 CFI_REGISTER rip, r11
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 SAVE_REST
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 FIXUP_TOP_OF_STACK %r11
Ingo Molnar5d119b22008-02-26 12:55:57 +0100585 movq %rsp, %rcx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 call sys_execve
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 RESTORE_TOP_OF_STACK %r11
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 movq %rax,RAX(%rsp)
589 RESTORE_REST
590 jmp int_ret_from_sys_call
591 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200592END(stub_execve)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594/*
595 * sigreturn is special because it needs to restore all registers on return.
596 * This cannot be done with SYSRET, so use the IRET return path instead.
597 */
598ENTRY(stub_rt_sigreturn)
599 CFI_STARTPROC
Jan Beulich7effaa82005-09-12 18:49:24 +0200600 addq $8, %rsp
601 CFI_ADJUST_CFA_OFFSET -8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 SAVE_REST
603 movq %rsp,%rdi
604 FIXUP_TOP_OF_STACK %r11
605 call sys_rt_sigreturn
606 movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer
607 RESTORE_REST
608 jmp int_ret_from_sys_call
609 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200610END(stub_rt_sigreturn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Jan Beulich7effaa82005-09-12 18:49:24 +0200612/*
613 * initial frame state for interrupts and exceptions
614 */
615 .macro _frame ref
616 CFI_STARTPROC simple
Jan Beulichadf14232006-09-26 10:52:41 +0200617 CFI_SIGNAL_FRAME
Jan Beulich7effaa82005-09-12 18:49:24 +0200618 CFI_DEF_CFA rsp,SS+8-\ref
619 /*CFI_REL_OFFSET ss,SS-\ref*/
620 CFI_REL_OFFSET rsp,RSP-\ref
621 /*CFI_REL_OFFSET rflags,EFLAGS-\ref*/
622 /*CFI_REL_OFFSET cs,CS-\ref*/
623 CFI_REL_OFFSET rip,RIP-\ref
624 .endm
625
626/* initial frame state for interrupts (and exceptions without error code) */
627#define INTR_FRAME _frame RIP
628/* initial frame state for exceptions with error code (and interrupts with
629 vector already pushed) */
630#define XCPT_FRAME _frame ORIG_RAX
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632/*
633 * Interrupt entry/exit.
634 *
635 * Interrupt entry points save only callee clobbered registers in fast path.
636 *
637 * Entry runs with interrupts off.
638 */
639
640/* 0(%rsp): interrupt number */
641 .macro interrupt func
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 cld
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 SAVE_ARGS
644 leaq -ARGOFFSET(%rsp),%rdi # arg1 for handler
Jan Beulich1de9c3f2006-06-26 13:57:35 +0200645 pushq %rbp
Glauber Costa097a0782008-08-14 17:33:12 -0300646 /*
647 * Save rbp twice: One is for marking the stack frame, as usual, and the
648 * other, to fill pt_regs properly. This is because bx comes right
649 * before the last saved register in that structure, and not bp. If the
650 * base pointer were in the place bx is today, this would not be needed.
651 */
652 movq %rbp, -8(%rsp)
Jan Beulich1de9c3f2006-06-26 13:57:35 +0200653 CFI_ADJUST_CFA_OFFSET 8
654 CFI_REL_OFFSET rbp, 0
655 movq %rsp,%rbp
656 CFI_DEF_CFA_REGISTER rbp
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 testl $3,CS(%rdi)
658 je 1f
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100659 SWAPGS
Andi Kleen96e54042006-09-26 10:52:39 +0200660 /* irqcount is used to check if a CPU is already on an interrupt
661 stack or not. While this is essentially redundant with preempt_count
662 it is a little cheaper to use a separate counter in the PDA
663 (short of moving irq_enter into assembly, which would be too
664 much work) */
6651: incl %gs:pda_irqcount
Jan Beulich1de9c3f2006-06-26 13:57:35 +0200666 cmoveq %gs:pda_irqstackptr,%rsp
Andi Kleen26995002006-08-02 22:37:28 +0200667 push %rbp # backlink for old unwinder
Ingo Molnar2601e642006-07-03 00:24:45 -0700668 /*
669 * We entered an interrupt context - irqs are off:
670 */
671 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 call \func
673 .endm
674
675ENTRY(common_interrupt)
Jan Beulich7effaa82005-09-12 18:49:24 +0200676 XCPT_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 interrupt do_IRQ
678 /* 0(%rsp): oldrsp-ARGOFFSET */
Jan Beulich7effaa82005-09-12 18:49:24 +0200679ret_from_intr:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100680 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700681 TRACE_IRQS_OFF
Andi Kleen3829ee62005-07-28 21:15:48 -0700682 decl %gs:pda_irqcount
Jan Beulich1de9c3f2006-06-26 13:57:35 +0200683 leaveq
Jan Beulich7effaa82005-09-12 18:49:24 +0200684 CFI_DEF_CFA_REGISTER rsp
Jan Beulich1de9c3f2006-06-26 13:57:35 +0200685 CFI_ADJUST_CFA_OFFSET -8
Jan Beulich7effaa82005-09-12 18:49:24 +0200686exit_intr:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 GET_THREAD_INFO(%rcx)
688 testl $3,CS-ARGOFFSET(%rsp)
689 je retint_kernel
690
691 /* Interrupt came from user space */
692 /*
693 * Has a correct top of stack, but a partial stack frame
694 * %rcx: thread info. Interrupts off.
695 */
696retint_with_reschedule:
697 movl $_TIF_WORK_MASK,%edi
Jan Beulich7effaa82005-09-12 18:49:24 +0200698retint_check:
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200699 LOCKDEP_SYS_EXIT_IRQ
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300700 movl TI_flags(%rcx),%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 andl %edi,%edx
Jan Beulich7effaa82005-09-12 18:49:24 +0200702 CFI_REMEMBER_STATE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 jnz retint_careful
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200704
705retint_swapgs: /* return to user-space */
Ingo Molnar2601e642006-07-03 00:24:45 -0700706 /*
707 * The iretq could re-enable interrupts:
708 */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100709 DISABLE_INTERRUPTS(CLBR_ANY)
Ingo Molnar2601e642006-07-03 00:24:45 -0700710 TRACE_IRQS_IRETQ
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100711 SWAPGS
Ingo Molnar2601e642006-07-03 00:24:45 -0700712 jmp restore_args
713
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200714retint_restore_args: /* return to kernel space */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100715 DISABLE_INTERRUPTS(CLBR_ANY)
Ingo Molnar2601e642006-07-03 00:24:45 -0700716 /*
717 * The iretq could re-enable interrupts:
718 */
719 TRACE_IRQS_IRETQ
720restore_args:
Ingo Molnar3701d8632008-02-09 23:24:08 +0100721 RESTORE_ARGS 0,8,0
722
Adrian Bunkf7f3d792008-02-13 23:29:53 +0200723irq_return:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100724 INTERRUPT_RETURN
Ingo Molnar3701d8632008-02-09 23:24:08 +0100725
726 .section __ex_table, "a"
727 .quad irq_return, bad_iret
728 .previous
729
730#ifdef CONFIG_PARAVIRT
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100731ENTRY(native_iret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 iretq
733
734 .section __ex_table,"a"
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100735 .quad native_iret, bad_iret
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 .previous
Ingo Molnar3701d8632008-02-09 23:24:08 +0100737#endif
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 .section .fixup,"ax"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740bad_iret:
Roland McGrath3aa4b372008-02-06 22:39:43 +0100741 /*
742 * The iret traps when the %cs or %ss being restored is bogus.
743 * We've lost the original trap vector and error code.
744 * #GPF is the most likely one to get for an invalid selector.
745 * So pretend we completed the iret and took the #GPF in user mode.
746 *
747 * We are now running with the kernel GS after exception recovery.
748 * But error_entry expects us to have user GS to match the user %cs,
749 * so swap back.
750 */
751 pushq $0
752
753 SWAPGS
754 jmp general_protection
755
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100756 .previous
757
Jan Beulich7effaa82005-09-12 18:49:24 +0200758 /* edi: workmask, edx: work */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759retint_careful:
Jan Beulich7effaa82005-09-12 18:49:24 +0200760 CFI_RESTORE_STATE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 bt $TIF_NEED_RESCHED,%edx
762 jnc retint_signal
Ingo Molnar2601e642006-07-03 00:24:45 -0700763 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100764 ENABLE_INTERRUPTS(CLBR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 pushq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200766 CFI_ADJUST_CFA_OFFSET 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 call schedule
768 popq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200769 CFI_ADJUST_CFA_OFFSET -8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 GET_THREAD_INFO(%rcx)
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100771 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700772 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 jmp retint_check
774
775retint_signal:
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100776 testl $_TIF_DO_NOTIFY_MASK,%edx
Andi Kleen10ffdbb2005-05-16 21:53:19 -0700777 jz retint_swapgs
Ingo Molnar2601e642006-07-03 00:24:45 -0700778 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100779 ENABLE_INTERRUPTS(CLBR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 SAVE_REST
781 movq $-1,ORIG_RAX(%rsp)
Andi Kleen3829ee62005-07-28 21:15:48 -0700782 xorl %esi,%esi # oldset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 movq %rsp,%rdi # &pt_regs
784 call do_notify_resume
785 RESTORE_REST
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100786 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700787 TRACE_IRQS_OFF
Andi Kleenbe9e6872005-05-01 08:58:51 -0700788 GET_THREAD_INFO(%rcx)
Roland McGratheca91e72008-07-10 14:50:39 -0700789 jmp retint_with_reschedule
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791#ifdef CONFIG_PREEMPT
792 /* Returning to kernel space. Check if we need preemption */
793 /* rcx: threadinfo. interrupts off. */
Andi Kleenb06baba2006-09-26 10:52:29 +0200794ENTRY(retint_kernel)
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300795 cmpl $0,TI_preempt_count(%rcx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 jnz retint_restore_args
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300797 bt $TIF_NEED_RESCHED,TI_flags(%rcx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 jnc retint_restore_args
799 bt $9,EFLAGS-ARGOFFSET(%rsp) /* interrupts off? */
800 jnc retint_restore_args
801 call preempt_schedule_irq
802 jmp exit_intr
803#endif
Jan Beulich4b787e02006-06-26 13:56:55 +0200804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200806END(common_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808/*
809 * APIC interrupts.
810 */
811 .macro apicinterrupt num,func
Jan Beulich7effaa82005-09-12 18:49:24 +0200812 INTR_FRAME
Rusty Russell19eadf92006-06-27 02:53:44 -0700813 pushq $~(\num)
Jan Beulich7effaa82005-09-12 18:49:24 +0200814 CFI_ADJUST_CFA_OFFSET 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 interrupt \func
816 jmp ret_from_intr
817 CFI_ENDPROC
818 .endm
819
820ENTRY(thermal_interrupt)
821 apicinterrupt THERMAL_APIC_VECTOR,smp_thermal_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200822END(thermal_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Jacob Shin89b831e2005-11-05 17:25:53 +0100824ENTRY(threshold_interrupt)
825 apicinterrupt THRESHOLD_APIC_VECTOR,mce_threshold_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200826END(threshold_interrupt)
Jacob Shin89b831e2005-11-05 17:25:53 +0100827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828#ifdef CONFIG_SMP
829ENTRY(reschedule_interrupt)
830 apicinterrupt RESCHEDULE_VECTOR,smp_reschedule_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200831END(reschedule_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Andi Kleene5bc8b62005-09-12 18:49:24 +0200833 .macro INVALIDATE_ENTRY num
834ENTRY(invalidate_interrupt\num)
835 apicinterrupt INVALIDATE_TLB_VECTOR_START+\num,smp_invalidate_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200836END(invalidate_interrupt\num)
Andi Kleene5bc8b62005-09-12 18:49:24 +0200837 .endm
838
839 INVALIDATE_ENTRY 0
840 INVALIDATE_ENTRY 1
841 INVALIDATE_ENTRY 2
842 INVALIDATE_ENTRY 3
843 INVALIDATE_ENTRY 4
844 INVALIDATE_ENTRY 5
845 INVALIDATE_ENTRY 6
846 INVALIDATE_ENTRY 7
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848ENTRY(call_function_interrupt)
849 apicinterrupt CALL_FUNCTION_VECTOR,smp_call_function_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200850END(call_function_interrupt)
Jens Axboe3b16cf82008-06-26 11:21:54 +0200851ENTRY(call_function_single_interrupt)
852 apicinterrupt CALL_FUNCTION_SINGLE_VECTOR,smp_call_function_single_interrupt
853END(call_function_single_interrupt)
Eric W. Biederman61014292007-02-23 04:40:58 -0700854ENTRY(irq_move_cleanup_interrupt)
855 apicinterrupt IRQ_MOVE_CLEANUP_VECTOR,smp_irq_move_cleanup_interrupt
856END(irq_move_cleanup_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857#endif
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859ENTRY(apic_timer_interrupt)
860 apicinterrupt LOCAL_TIMER_VECTOR,smp_apic_timer_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200861END(apic_timer_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Cliff Wickman18129242008-06-02 08:56:14 -0500863ENTRY(uv_bau_message_intr1)
864 apicinterrupt 220,uv_bau_message_interrupt
865END(uv_bau_message_intr1)
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867ENTRY(error_interrupt)
868 apicinterrupt ERROR_APIC_VECTOR,smp_error_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200869END(error_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871ENTRY(spurious_interrupt)
872 apicinterrupt SPURIOUS_APIC_VECTOR,smp_spurious_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200873END(spurious_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875/*
876 * Exception entry points.
877 */
878 .macro zeroentry sym
Jan Beulich7effaa82005-09-12 18:49:24 +0200879 INTR_FRAME
Jeremy Fitzhardingefab58422008-06-25 00:19:31 -0400880 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 pushq $0 /* push error code/oldrax */
Jan Beulich7effaa82005-09-12 18:49:24 +0200882 CFI_ADJUST_CFA_OFFSET 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 pushq %rax /* push real oldrax to the rdi slot */
Jan Beulich7effaa82005-09-12 18:49:24 +0200884 CFI_ADJUST_CFA_OFFSET 8
Jan Beulich37550902007-05-02 19:27:05 +0200885 CFI_REL_OFFSET rax,0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 leaq \sym(%rip),%rax
887 jmp error_entry
Jan Beulich7effaa82005-09-12 18:49:24 +0200888 CFI_ENDPROC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 .endm
890
891 .macro errorentry sym
Jan Beulich7effaa82005-09-12 18:49:24 +0200892 XCPT_FRAME
Jeremy Fitzhardingefab58422008-06-25 00:19:31 -0400893 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 pushq %rax
Jan Beulich7effaa82005-09-12 18:49:24 +0200895 CFI_ADJUST_CFA_OFFSET 8
Jan Beulich37550902007-05-02 19:27:05 +0200896 CFI_REL_OFFSET rax,0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 leaq \sym(%rip),%rax
898 jmp error_entry
Jan Beulich7effaa82005-09-12 18:49:24 +0200899 CFI_ENDPROC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 .endm
901
902 /* error code is on the stack already */
903 /* handle NMI like exceptions that can happen everywhere */
Ingo Molnar2601e642006-07-03 00:24:45 -0700904 .macro paranoidentry sym, ist=0, irqtrace=1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 SAVE_ALL
906 cld
907 movl $1,%ebx
908 movl $MSR_GS_BASE,%ecx
909 rdmsr
910 testl %edx,%edx
911 js 1f
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100912 SWAPGS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 xorl %ebx,%ebx
Jan Beulichb556b352006-01-11 22:43:00 +01009141:
915 .if \ist
916 movq %gs:pda_data_offset, %rbp
917 .endif
Alexander van Heukelum7e61a792008-09-26 14:03:03 +0200918 .if \irqtrace
919 TRACE_IRQS_OFF
920 .endif
Jan Beulichb556b352006-01-11 22:43:00 +0100921 movq %rsp,%rdi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 movq ORIG_RAX(%rsp),%rsi
923 movq $-1,ORIG_RAX(%rsp)
Jan Beulichb556b352006-01-11 22:43:00 +0100924 .if \ist
Andi Kleen5f8efbb2006-01-16 01:56:39 +0100925 subq $EXCEPTION_STKSZ, per_cpu__init_tss + TSS_ist + (\ist - 1) * 8(%rbp)
Jan Beulichb556b352006-01-11 22:43:00 +0100926 .endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 call \sym
Jan Beulichb556b352006-01-11 22:43:00 +0100928 .if \ist
Andi Kleen5f8efbb2006-01-16 01:56:39 +0100929 addq $EXCEPTION_STKSZ, per_cpu__init_tss + TSS_ist + (\ist - 1) * 8(%rbp)
Jan Beulichb556b352006-01-11 22:43:00 +0100930 .endif
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100931 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700932 .if \irqtrace
933 TRACE_IRQS_OFF
934 .endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 .endm
Ingo Molnar2601e642006-07-03 00:24:45 -0700936
937 /*
938 * "Paranoid" exit path from exception stack.
939 * Paranoid because this is used by NMIs and cannot take
940 * any kernel state for granted.
941 * We don't do kernel preemption checks here, because only
942 * NMI should be common and it does not enable IRQs and
943 * cannot get reschedule ticks.
944 *
945 * "trace" is 0 for the NMI handler only, because irq-tracing
946 * is fundamentally NMI-unsafe. (we cannot change the soft and
947 * hard flags at once, atomically)
948 */
949 .macro paranoidexit trace=1
950 /* ebx: no swapgs flag */
951paranoid_exit\trace:
952 testl %ebx,%ebx /* swapgs needed? */
953 jnz paranoid_restore\trace
954 testl $3,CS(%rsp)
955 jnz paranoid_userspace\trace
956paranoid_swapgs\trace:
Andi Kleen7a0a2df2006-09-26 10:52:37 +0200957 .if \trace
Ingo Molnar2601e642006-07-03 00:24:45 -0700958 TRACE_IRQS_IRETQ 0
Andi Kleen7a0a2df2006-09-26 10:52:37 +0200959 .endif
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100960 SWAPGS_UNSAFE_STACK
Ingo Molnar2601e642006-07-03 00:24:45 -0700961paranoid_restore\trace:
962 RESTORE_ALL 8
Ingo Molnar3701d8632008-02-09 23:24:08 +0100963 jmp irq_return
Ingo Molnar2601e642006-07-03 00:24:45 -0700964paranoid_userspace\trace:
965 GET_THREAD_INFO(%rcx)
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300966 movl TI_flags(%rcx),%ebx
Ingo Molnar2601e642006-07-03 00:24:45 -0700967 andl $_TIF_WORK_MASK,%ebx
968 jz paranoid_swapgs\trace
969 movq %rsp,%rdi /* &pt_regs */
970 call sync_regs
971 movq %rax,%rsp /* switch stack for scheduling */
972 testl $_TIF_NEED_RESCHED,%ebx
973 jnz paranoid_schedule\trace
974 movl %ebx,%edx /* arg3: thread flags */
975 .if \trace
976 TRACE_IRQS_ON
977 .endif
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100978 ENABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700979 xorl %esi,%esi /* arg2: oldset */
980 movq %rsp,%rdi /* arg1: &pt_regs */
981 call do_notify_resume
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100982 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700983 .if \trace
984 TRACE_IRQS_OFF
985 .endif
986 jmp paranoid_userspace\trace
987paranoid_schedule\trace:
988 .if \trace
989 TRACE_IRQS_ON
990 .endif
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100991 ENABLE_INTERRUPTS(CLBR_ANY)
Ingo Molnar2601e642006-07-03 00:24:45 -0700992 call schedule
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100993 DISABLE_INTERRUPTS(CLBR_ANY)
Ingo Molnar2601e642006-07-03 00:24:45 -0700994 .if \trace
995 TRACE_IRQS_OFF
996 .endif
997 jmp paranoid_userspace\trace
998 CFI_ENDPROC
999 .endm
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001/*
1002 * Exception entry point. This expects an error code/orig_rax on the stack
1003 * and the exception handler in %rax.
1004 */
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001005KPROBE_ENTRY(error_entry)
Jan Beulich7effaa82005-09-12 18:49:24 +02001006 _frame RDI
Jan Beulich37550902007-05-02 19:27:05 +02001007 CFI_REL_OFFSET rax,0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 /* rdi slot contains rax, oldrax contains error code */
1009 cld
1010 subq $14*8,%rsp
1011 CFI_ADJUST_CFA_OFFSET (14*8)
1012 movq %rsi,13*8(%rsp)
1013 CFI_REL_OFFSET rsi,RSI
1014 movq 14*8(%rsp),%rsi /* load rax from rdi slot */
Jan Beulich37550902007-05-02 19:27:05 +02001015 CFI_REGISTER rax,rsi
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 movq %rdx,12*8(%rsp)
1017 CFI_REL_OFFSET rdx,RDX
1018 movq %rcx,11*8(%rsp)
1019 CFI_REL_OFFSET rcx,RCX
1020 movq %rsi,10*8(%rsp) /* store rax */
1021 CFI_REL_OFFSET rax,RAX
1022 movq %r8, 9*8(%rsp)
1023 CFI_REL_OFFSET r8,R8
1024 movq %r9, 8*8(%rsp)
1025 CFI_REL_OFFSET r9,R9
1026 movq %r10,7*8(%rsp)
1027 CFI_REL_OFFSET r10,R10
1028 movq %r11,6*8(%rsp)
1029 CFI_REL_OFFSET r11,R11
1030 movq %rbx,5*8(%rsp)
1031 CFI_REL_OFFSET rbx,RBX
1032 movq %rbp,4*8(%rsp)
1033 CFI_REL_OFFSET rbp,RBP
1034 movq %r12,3*8(%rsp)
1035 CFI_REL_OFFSET r12,R12
1036 movq %r13,2*8(%rsp)
1037 CFI_REL_OFFSET r13,R13
1038 movq %r14,1*8(%rsp)
1039 CFI_REL_OFFSET r14,R14
1040 movq %r15,(%rsp)
1041 CFI_REL_OFFSET r15,R15
1042 xorl %ebx,%ebx
1043 testl $3,CS(%rsp)
1044 je error_kernelspace
1045error_swapgs:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001046 SWAPGS
Alexander van Heukelum6b11d4e2008-09-26 14:03:02 +02001047error_sti:
1048 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 movq %rdi,RDI(%rsp)
Jan Beulich37550902007-05-02 19:27:05 +02001050 CFI_REL_OFFSET rdi,RDI
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 movq %rsp,%rdi
1052 movq ORIG_RAX(%rsp),%rsi /* get error code */
1053 movq $-1,ORIG_RAX(%rsp)
1054 call *%rax
Peter Zijlstra10cd7062007-10-11 22:11:12 +02001055 /* ebx: no swapgs flag (1: don't need swapgs, 0: need it) */
1056error_exit:
1057 movl %ebx,%eax
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 RESTORE_REST
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001059 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -07001060 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 GET_THREAD_INFO(%rcx)
1062 testl %eax,%eax
1063 jne retint_kernel
Peter Zijlstra10cd7062007-10-11 22:11:12 +02001064 LOCKDEP_SYS_EXIT_IRQ
Glauber Costa26ccb8a2008-06-24 11:19:35 -03001065 movl TI_flags(%rcx),%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 movl $_TIF_WORK_MASK,%edi
1067 andl %edi,%edx
1068 jnz retint_careful
Peter Zijlstra10cd7062007-10-11 22:11:12 +02001069 jmp retint_swapgs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 CFI_ENDPROC
1071
1072error_kernelspace:
1073 incl %ebx
1074 /* There are two places in the kernel that can potentially fault with
1075 usergs. Handle them here. The exception handlers after
1076 iret run with kernel gs again, so don't set the user space flag.
1077 B stepping K8s sometimes report an truncated RIP for IRET
1078 exceptions returning to compat mode. Check for these here too. */
Vegard Nossum9d8ad5d2008-06-27 17:22:17 +02001079 leaq irq_return(%rip),%rcx
1080 cmpq %rcx,RIP(%rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 je error_swapgs
Vegard Nossum9d8ad5d2008-06-27 17:22:17 +02001082 movl %ecx,%ecx /* zero extend */
1083 cmpq %rcx,RIP(%rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 je error_swapgs
1085 cmpq $gs_change,RIP(%rsp)
1086 je error_swapgs
1087 jmp error_sti
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001088KPROBE_END(error_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090 /* Reload gs selector with exception handling */
1091 /* edi: new selector */
Jeremy Fitzhardinge9f9d4892008-06-25 00:19:32 -04001092ENTRY(native_load_gs_index)
Jan Beulich7effaa82005-09-12 18:49:24 +02001093 CFI_STARTPROC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 pushf
Jan Beulich7effaa82005-09-12 18:49:24 +02001095 CFI_ADJUST_CFA_OFFSET 8
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001096 DISABLE_INTERRUPTS(CLBR_ANY | ~(CLBR_RDI))
1097 SWAPGS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098gs_change:
1099 movl %edi,%gs
11002: mfence /* workaround */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001101 SWAPGS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 popf
Jan Beulich7effaa82005-09-12 18:49:24 +02001103 CFI_ADJUST_CFA_OFFSET -8
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 ret
Jan Beulich7effaa82005-09-12 18:49:24 +02001105 CFI_ENDPROC
Jeremy Fitzhardinge9f9d4892008-06-25 00:19:32 -04001106ENDPROC(native_load_gs_index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 .section __ex_table,"a"
1109 .align 8
1110 .quad gs_change,bad_gs
1111 .previous
1112 .section .fixup,"ax"
1113 /* running with kernelgs */
1114bad_gs:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001115 SWAPGS /* switch back to user gs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 xorl %eax,%eax
1117 movl %eax,%gs
1118 jmp 2b
1119 .previous
1120
1121/*
1122 * Create a kernel thread.
1123 *
1124 * C extern interface:
1125 * extern long kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
1126 *
1127 * asm input arguments:
1128 * rdi: fn, rsi: arg, rdx: flags
1129 */
1130ENTRY(kernel_thread)
1131 CFI_STARTPROC
1132 FAKE_STACK_FRAME $child_rip
1133 SAVE_ALL
1134
1135 # rdi: flags, rsi: usp, rdx: will be &pt_regs
1136 movq %rdx,%rdi
1137 orq kernel_thread_flags(%rip),%rdi
1138 movq $-1, %rsi
1139 movq %rsp, %rdx
1140
1141 xorl %r8d,%r8d
1142 xorl %r9d,%r9d
1143
1144 # clone now
1145 call do_fork
1146 movq %rax,RAX(%rsp)
1147 xorl %edi,%edi
1148
1149 /*
1150 * It isn't worth to check for reschedule here,
1151 * so internally to the x86_64 port you can rely on kernel_thread()
1152 * not to reschedule the child before returning, this avoids the need
1153 * of hacks for example to fork off the per-CPU idle tasks.
1154 * [Hopefully no generic code relies on the reschedule -AK]
1155 */
1156 RESTORE_ALL
1157 UNFAKE_STACK_FRAME
1158 ret
1159 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001160ENDPROC(kernel_thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162child_rip:
Andi Kleenc05991e2006-08-30 19:37:08 +02001163 pushq $0 # fake return address
1164 CFI_STARTPROC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 /*
1166 * Here we are in the child and the registers are set as they were
1167 * at kernel_thread() invocation in the parent.
1168 */
1169 movq %rdi, %rax
1170 movq %rsi, %rdi
1171 call *%rax
1172 # exit
Andrey Mirkin1c5b5cf2007-10-17 18:04:33 +02001173 mov %eax, %edi
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 call do_exit
jia zhang5f5db592008-11-23 22:47:10 +08001175 ud2 # padding for call trace
Andi Kleenc05991e2006-08-30 19:37:08 +02001176 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001177ENDPROC(child_rip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179/*
1180 * execve(). This function needs to use IRET, not SYSRET, to set up all state properly.
1181 *
1182 * C extern interface:
1183 * extern long execve(char *name, char **argv, char **envp)
1184 *
1185 * asm input arguments:
1186 * rdi: name, rsi: argv, rdx: envp
1187 *
1188 * We want to fallback into:
Ingo Molnar5d119b22008-02-26 12:55:57 +01001189 * extern long sys_execve(char *name, char **argv,char **envp, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 *
1191 * do_sys_execve asm fallback arguments:
Ingo Molnar5d119b22008-02-26 12:55:57 +01001192 * rdi: name, rsi: argv, rdx: envp, rcx: fake frame on the stack
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 */
Arnd Bergmann3db03b42006-10-02 02:18:31 -07001194ENTRY(kernel_execve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 CFI_STARTPROC
1196 FAKE_STACK_FRAME $0
1197 SAVE_ALL
Ingo Molnar5d119b22008-02-26 12:55:57 +01001198 movq %rsp,%rcx
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 call sys_execve
1200 movq %rax, RAX(%rsp)
1201 RESTORE_REST
1202 testq %rax,%rax
1203 je int_ret_from_sys_call
1204 RESTORE_ARGS
1205 UNFAKE_STACK_FRAME
1206 ret
1207 CFI_ENDPROC
Arnd Bergmann3db03b42006-10-02 02:18:31 -07001208ENDPROC(kernel_execve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001210KPROBE_ENTRY(page_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 errorentry do_page_fault
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001212KPROBE_END(page_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214ENTRY(coprocessor_error)
1215 zeroentry do_coprocessor_error
Jan Beulich4b787e02006-06-26 13:56:55 +02001216END(coprocessor_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218ENTRY(simd_coprocessor_error)
1219 zeroentry do_simd_coprocessor_error
Jan Beulich4b787e02006-06-26 13:56:55 +02001220END(simd_coprocessor_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222ENTRY(device_not_available)
Alexander van Heukelume407d6202008-09-30 18:41:36 +02001223 zeroentry do_device_not_available
Jan Beulich4b787e02006-06-26 13:56:55 +02001224END(device_not_available)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 /* runs on exception stack */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001227KPROBE_ENTRY(debug)
Jan Beulich7effaa82005-09-12 18:49:24 +02001228 INTR_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001229 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 pushq $0
1231 CFI_ADJUST_CFA_OFFSET 8
Andi Kleen5f8efbb2006-01-16 01:56:39 +01001232 paranoidentry do_debug, DEBUG_STACK
Ingo Molnar2601e642006-07-03 00:24:45 -07001233 paranoidexit
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001234KPROBE_END(debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 /* runs on exception stack */
Andi Kleeneddb6fb2006-02-03 21:50:41 +01001237KPROBE_ENTRY(nmi)
Jan Beulich7effaa82005-09-12 18:49:24 +02001238 INTR_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001239 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 pushq $-1
Jan Beulich7effaa82005-09-12 18:49:24 +02001241 CFI_ADJUST_CFA_OFFSET 8
Ingo Molnar2601e642006-07-03 00:24:45 -07001242 paranoidentry do_nmi, 0, 0
1243#ifdef CONFIG_TRACE_IRQFLAGS
1244 paranoidexit 0
1245#else
1246 jmp paranoid_exit1
1247 CFI_ENDPROC
1248#endif
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001249KPROBE_END(nmi)
Andi Kleen6fefb0d2005-04-16 15:25:03 -07001250
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001251KPROBE_ENTRY(int3)
Jan Beulichb556b352006-01-11 22:43:00 +01001252 INTR_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001253 PARAVIRT_ADJUST_EXCEPTION_FRAME
Jan Beulichb556b352006-01-11 22:43:00 +01001254 pushq $0
1255 CFI_ADJUST_CFA_OFFSET 8
Andi Kleen5f8efbb2006-01-16 01:56:39 +01001256 paranoidentry do_int3, DEBUG_STACK
Ingo Molnar2601e642006-07-03 00:24:45 -07001257 jmp paranoid_exit1
Jan Beulichb556b352006-01-11 22:43:00 +01001258 CFI_ENDPROC
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001259KPROBE_END(int3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261ENTRY(overflow)
1262 zeroentry do_overflow
Jan Beulich4b787e02006-06-26 13:56:55 +02001263END(overflow)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265ENTRY(bounds)
1266 zeroentry do_bounds
Jan Beulich4b787e02006-06-26 13:56:55 +02001267END(bounds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269ENTRY(invalid_op)
1270 zeroentry do_invalid_op
Jan Beulich4b787e02006-06-26 13:56:55 +02001271END(invalid_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273ENTRY(coprocessor_segment_overrun)
1274 zeroentry do_coprocessor_segment_overrun
Jan Beulich4b787e02006-06-26 13:56:55 +02001275END(coprocessor_segment_overrun)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 /* runs on exception stack */
1278ENTRY(double_fault)
Jan Beulich7effaa82005-09-12 18:49:24 +02001279 XCPT_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001280 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 paranoidentry do_double_fault
Ingo Molnar2601e642006-07-03 00:24:45 -07001282 jmp paranoid_exit1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001284END(double_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286ENTRY(invalid_TSS)
1287 errorentry do_invalid_TSS
Jan Beulich4b787e02006-06-26 13:56:55 +02001288END(invalid_TSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290ENTRY(segment_not_present)
1291 errorentry do_segment_not_present
Jan Beulich4b787e02006-06-26 13:56:55 +02001292END(segment_not_present)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 /* runs on exception stack */
1295ENTRY(stack_segment)
Jan Beulich7effaa82005-09-12 18:49:24 +02001296 XCPT_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001297 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 paranoidentry do_stack_segment
Ingo Molnar2601e642006-07-03 00:24:45 -07001299 jmp paranoid_exit1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001301END(stack_segment)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001303KPROBE_ENTRY(general_protection)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 errorentry do_general_protection
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001305KPROBE_END(general_protection)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307ENTRY(alignment_check)
1308 errorentry do_alignment_check
Jan Beulich4b787e02006-06-26 13:56:55 +02001309END(alignment_check)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311ENTRY(divide_error)
1312 zeroentry do_divide_error
Jan Beulich4b787e02006-06-26 13:56:55 +02001313END(divide_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315ENTRY(spurious_interrupt_bug)
1316 zeroentry do_spurious_interrupt_bug
Jan Beulich4b787e02006-06-26 13:56:55 +02001317END(spurious_interrupt_bug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319#ifdef CONFIG_X86_MCE
1320 /* runs on exception stack */
1321ENTRY(machine_check)
Jan Beulich7effaa82005-09-12 18:49:24 +02001322 INTR_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001323 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 pushq $0
1325 CFI_ADJUST_CFA_OFFSET 8
1326 paranoidentry do_machine_check
Ingo Molnar2601e642006-07-03 00:24:45 -07001327 jmp paranoid_exit1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001329END(machine_check)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330#endif
1331
Andi Kleen26995002006-08-02 22:37:28 +02001332/* Call softirq on interrupt stack. Interrupts are off. */
Andi Kleened6b6762005-07-28 21:15:49 -07001333ENTRY(call_softirq)
Jan Beulich7effaa82005-09-12 18:49:24 +02001334 CFI_STARTPROC
Andi Kleen26995002006-08-02 22:37:28 +02001335 push %rbp
1336 CFI_ADJUST_CFA_OFFSET 8
1337 CFI_REL_OFFSET rbp,0
1338 mov %rsp,%rbp
1339 CFI_DEF_CFA_REGISTER rbp
Andi Kleened6b6762005-07-28 21:15:49 -07001340 incl %gs:pda_irqcount
Andi Kleen26995002006-08-02 22:37:28 +02001341 cmove %gs:pda_irqstackptr,%rsp
1342 push %rbp # backlink for old unwinder
Andi Kleened6b6762005-07-28 21:15:49 -07001343 call __do_softirq
Andi Kleen26995002006-08-02 22:37:28 +02001344 leaveq
Jan Beulich7effaa82005-09-12 18:49:24 +02001345 CFI_DEF_CFA_REGISTER rsp
Andi Kleen26995002006-08-02 22:37:28 +02001346 CFI_ADJUST_CFA_OFFSET -8
Andi Kleened6b6762005-07-28 21:15:49 -07001347 decl %gs:pda_irqcount
Andi Kleened6b6762005-07-28 21:15:49 -07001348 ret
Jan Beulich7effaa82005-09-12 18:49:24 +02001349 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001350ENDPROC(call_softirq)
Andi Kleen75154f42007-06-23 02:29:25 +02001351
1352KPROBE_ENTRY(ignore_sysret)
1353 CFI_STARTPROC
1354 mov $-ENOSYS,%eax
1355 sysret
1356 CFI_ENDPROC
1357ENDPROC(ignore_sysret)
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001358
1359#ifdef CONFIG_XEN
1360ENTRY(xen_hypervisor_callback)
1361 zeroentry xen_do_hypervisor_callback
1362END(xen_hypervisor_callback)
1363
1364/*
1365# A note on the "critical region" in our callback handler.
1366# We want to avoid stacking callback handlers due to events occurring
1367# during handling of the last event. To do this, we keep events disabled
1368# until we've done all processing. HOWEVER, we must enable events before
1369# popping the stack frame (can't be done atomically) and so it would still
1370# be possible to get enough handler activations to overflow the stack.
1371# Although unlikely, bugs of that kind are hard to track down, so we'd
1372# like to avoid the possibility.
1373# So, on entry to the handler we detect whether we interrupted an
1374# existing activation in its critical region -- if so, we pop the current
1375# activation and restart the handler using the previous one.
1376*/
1377ENTRY(xen_do_hypervisor_callback) # do_hypervisor_callback(struct *pt_regs)
1378 CFI_STARTPROC
1379/* Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
1380 see the correct pointer to the pt_regs */
1381 movq %rdi, %rsp # we don't return, adjust the stack frame
1382 CFI_ENDPROC
1383 CFI_DEFAULT_STACK
138411: incl %gs:pda_irqcount
1385 movq %rsp,%rbp
1386 CFI_DEF_CFA_REGISTER rbp
1387 cmovzq %gs:pda_irqstackptr,%rsp
1388 pushq %rbp # backlink for old unwinder
1389 call xen_evtchn_do_upcall
1390 popq %rsp
1391 CFI_DEF_CFA_REGISTER rsp
1392 decl %gs:pda_irqcount
1393 jmp error_exit
1394 CFI_ENDPROC
1395END(do_hypervisor_callback)
1396
1397/*
1398# Hypervisor uses this for application faults while it executes.
1399# We get here for two reasons:
1400# 1. Fault while reloading DS, ES, FS or GS
1401# 2. Fault while executing IRET
1402# Category 1 we do not need to fix up as Xen has already reloaded all segment
1403# registers that could be reloaded and zeroed the others.
1404# Category 2 we fix up by killing the current process. We cannot use the
1405# normal Linux return path in this case because if we use the IRET hypercall
1406# to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1407# We distinguish between categories by comparing each saved segment register
1408# with its current contents: any discrepancy means we in category 1.
1409*/
1410ENTRY(xen_failsafe_callback)
Jeremy Fitzhardinge4a5c3e72008-07-08 15:07:09 -07001411 framesz = (RIP-0x30) /* workaround buggy gas */
1412 _frame framesz
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001413 CFI_REL_OFFSET rcx, 0
1414 CFI_REL_OFFSET r11, 8
1415 movw %ds,%cx
1416 cmpw %cx,0x10(%rsp)
1417 CFI_REMEMBER_STATE
1418 jne 1f
1419 movw %es,%cx
1420 cmpw %cx,0x18(%rsp)
1421 jne 1f
1422 movw %fs,%cx
1423 cmpw %cx,0x20(%rsp)
1424 jne 1f
1425 movw %gs,%cx
1426 cmpw %cx,0x28(%rsp)
1427 jne 1f
1428 /* All segments match their saved values => Category 2 (Bad IRET). */
1429 movq (%rsp),%rcx
1430 CFI_RESTORE rcx
1431 movq 8(%rsp),%r11
1432 CFI_RESTORE r11
1433 addq $0x30,%rsp
1434 CFI_ADJUST_CFA_OFFSET -0x30
Jeremy Fitzhardinge4a5c3e72008-07-08 15:07:09 -07001435 pushq $0
1436 CFI_ADJUST_CFA_OFFSET 8
1437 pushq %r11
1438 CFI_ADJUST_CFA_OFFSET 8
1439 pushq %rcx
1440 CFI_ADJUST_CFA_OFFSET 8
1441 jmp general_protection
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001442 CFI_RESTORE_STATE
14431: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
1444 movq (%rsp),%rcx
1445 CFI_RESTORE rcx
1446 movq 8(%rsp),%r11
1447 CFI_RESTORE r11
1448 addq $0x30,%rsp
1449 CFI_ADJUST_CFA_OFFSET -0x30
1450 pushq $0
1451 CFI_ADJUST_CFA_OFFSET 8
1452 SAVE_ALL
1453 jmp error_exit
1454 CFI_ENDPROC
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001455END(xen_failsafe_callback)
1456
1457#endif /* CONFIG_XEN */