blob: db7d34a89d2ef7a56fbe4ab686f021835cdd3c94 [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
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020064#ifdef CONFIG_FTRACE
Steven Rostedtd61f82d2008-05-12 21:20:43 +020065#ifdef CONFIG_DYNAMIC_FTRACE
66ENTRY(mcount)
67
68 subq $0x38, %rsp
69 movq %rax, (%rsp)
70 movq %rcx, 8(%rsp)
71 movq %rdx, 16(%rsp)
72 movq %rsi, 24(%rsp)
73 movq %rdi, 32(%rsp)
74 movq %r8, 40(%rsp)
75 movq %r9, 48(%rsp)
76
77 movq 0x38(%rsp), %rdi
Abhishek Sagar395a59d2008-06-21 23:47:27 +053078 subq $MCOUNT_INSN_SIZE, %rdi
Steven Rostedtd61f82d2008-05-12 21:20:43 +020079
80.globl mcount_call
81mcount_call:
82 call ftrace_stub
83
84 movq 48(%rsp), %r9
85 movq 40(%rsp), %r8
86 movq 32(%rsp), %rdi
87 movq 24(%rsp), %rsi
88 movq 16(%rsp), %rdx
89 movq 8(%rsp), %rcx
90 movq (%rsp), %rax
91 addq $0x38, %rsp
92
93 retq
94END(mcount)
95
96ENTRY(ftrace_caller)
97
98 /* taken from glibc */
99 subq $0x38, %rsp
100 movq %rax, (%rsp)
101 movq %rcx, 8(%rsp)
102 movq %rdx, 16(%rsp)
103 movq %rsi, 24(%rsp)
104 movq %rdi, 32(%rsp)
105 movq %r8, 40(%rsp)
106 movq %r9, 48(%rsp)
107
108 movq 0x38(%rsp), %rdi
109 movq 8(%rbp), %rsi
Abhishek Sagar395a59d2008-06-21 23:47:27 +0530110 subq $MCOUNT_INSN_SIZE, %rdi
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200111
112.globl ftrace_call
113ftrace_call:
114 call ftrace_stub
115
116 movq 48(%rsp), %r9
117 movq 40(%rsp), %r8
118 movq 32(%rsp), %rdi
119 movq 24(%rsp), %rsi
120 movq 16(%rsp), %rdx
121 movq 8(%rsp), %rcx
122 movq (%rsp), %rax
123 addq $0x38, %rsp
124
125.globl ftrace_stub
126ftrace_stub:
127 retq
128END(ftrace_caller)
129
130#else /* ! CONFIG_DYNAMIC_FTRACE */
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200131ENTRY(mcount)
132 cmpq $ftrace_stub, ftrace_trace_function
133 jnz trace
134.globl ftrace_stub
135ftrace_stub:
136 retq
137
138trace:
139 /* taken from glibc */
140 subq $0x38, %rsp
141 movq %rax, (%rsp)
142 movq %rcx, 8(%rsp)
143 movq %rdx, 16(%rsp)
144 movq %rsi, 24(%rsp)
145 movq %rdi, 32(%rsp)
146 movq %r8, 40(%rsp)
147 movq %r9, 48(%rsp)
148
149 movq 0x38(%rsp), %rdi
150 movq 8(%rbp), %rsi
Abhishek Sagar395a59d2008-06-21 23:47:27 +0530151 subq $MCOUNT_INSN_SIZE, %rdi
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200152
153 call *ftrace_trace_function
154
155 movq 48(%rsp), %r9
156 movq 40(%rsp), %r8
157 movq 32(%rsp), %rdi
158 movq 24(%rsp), %rsi
159 movq 16(%rsp), %rdx
160 movq 8(%rsp), %rcx
161 movq (%rsp), %rax
162 addq $0x38, %rsp
163
164 jmp ftrace_stub
165END(mcount)
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200166#endif /* CONFIG_DYNAMIC_FTRACE */
167#endif /* CONFIG_FTRACE */
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200168
Andi Kleendc37db42005-04-16 15:25:05 -0700169#ifndef CONFIG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170#define retint_kernel retint_restore_args
171#endif
Ingo Molnar2601e642006-07-03 00:24:45 -0700172
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100173#ifdef CONFIG_PARAVIRT
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -0400174ENTRY(native_usergs_sysret64)
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100175 swapgs
176 sysretq
177#endif /* CONFIG_PARAVIRT */
178
Ingo Molnar2601e642006-07-03 00:24:45 -0700179
180.macro TRACE_IRQS_IRETQ offset=ARGOFFSET
181#ifdef CONFIG_TRACE_IRQFLAGS
182 bt $9,EFLAGS-\offset(%rsp) /* interrupts off? */
183 jnc 1f
184 TRACE_IRQS_ON
1851:
186#endif
187.endm
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/*
190 * C code is not supposed to know about undefined top of stack. Every time
191 * a C function with an pt_regs argument is called from the SYSCALL based
192 * fast path FIXUP_TOP_OF_STACK is needed.
193 * RESTORE_TOP_OF_STACK syncs the syscall state after any possible ptregs
194 * manipulation.
195 */
196
197 /* %rsp:at FRAMEEND */
198 .macro FIXUP_TOP_OF_STACK tmp
199 movq %gs:pda_oldrsp,\tmp
200 movq \tmp,RSP(%rsp)
201 movq $__USER_DS,SS(%rsp)
202 movq $__USER_CS,CS(%rsp)
203 movq $-1,RCX(%rsp)
204 movq R11(%rsp),\tmp /* get eflags */
205 movq \tmp,EFLAGS(%rsp)
206 .endm
207
208 .macro RESTORE_TOP_OF_STACK tmp,offset=0
209 movq RSP-\offset(%rsp),\tmp
210 movq \tmp,%gs:pda_oldrsp
211 movq EFLAGS-\offset(%rsp),\tmp
212 movq \tmp,R11-\offset(%rsp)
213 .endm
214
215 .macro FAKE_STACK_FRAME child_rip
216 /* push in order ss, rsp, eflags, cs, rip */
Andi Kleen3829ee62005-07-28 21:15:48 -0700217 xorl %eax, %eax
Jeremy Fitzhardingee04e0a62008-06-25 00:19:25 -0400218 pushq $__KERNEL_DS /* ss */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 CFI_ADJUST_CFA_OFFSET 8
Jan Beulich7effaa82005-09-12 18:49:24 +0200220 /*CFI_REL_OFFSET ss,0*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 pushq %rax /* rsp */
222 CFI_ADJUST_CFA_OFFSET 8
Jan Beulich7effaa82005-09-12 18:49:24 +0200223 CFI_REL_OFFSET rsp,0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 pushq $(1<<9) /* eflags - interrupts on */
225 CFI_ADJUST_CFA_OFFSET 8
Jan Beulich7effaa82005-09-12 18:49:24 +0200226 /*CFI_REL_OFFSET rflags,0*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 pushq $__KERNEL_CS /* cs */
228 CFI_ADJUST_CFA_OFFSET 8
Jan Beulich7effaa82005-09-12 18:49:24 +0200229 /*CFI_REL_OFFSET cs,0*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 pushq \child_rip /* rip */
231 CFI_ADJUST_CFA_OFFSET 8
Jan Beulich7effaa82005-09-12 18:49:24 +0200232 CFI_REL_OFFSET rip,0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 pushq %rax /* orig rax */
234 CFI_ADJUST_CFA_OFFSET 8
235 .endm
236
237 .macro UNFAKE_STACK_FRAME
238 addq $8*6, %rsp
239 CFI_ADJUST_CFA_OFFSET -(6*8)
240 .endm
241
Jan Beulich7effaa82005-09-12 18:49:24 +0200242 .macro CFI_DEFAULT_STACK start=1
243 .if \start
244 CFI_STARTPROC simple
Jan Beulichadf14232006-09-26 10:52:41 +0200245 CFI_SIGNAL_FRAME
Jan Beulich7effaa82005-09-12 18:49:24 +0200246 CFI_DEF_CFA rsp,SS+8
247 .else
248 CFI_DEF_CFA_OFFSET SS+8
249 .endif
250 CFI_REL_OFFSET r15,R15
251 CFI_REL_OFFSET r14,R14
252 CFI_REL_OFFSET r13,R13
253 CFI_REL_OFFSET r12,R12
254 CFI_REL_OFFSET rbp,RBP
255 CFI_REL_OFFSET rbx,RBX
256 CFI_REL_OFFSET r11,R11
257 CFI_REL_OFFSET r10,R10
258 CFI_REL_OFFSET r9,R9
259 CFI_REL_OFFSET r8,R8
260 CFI_REL_OFFSET rax,RAX
261 CFI_REL_OFFSET rcx,RCX
262 CFI_REL_OFFSET rdx,RDX
263 CFI_REL_OFFSET rsi,RSI
264 CFI_REL_OFFSET rdi,RDI
265 CFI_REL_OFFSET rip,RIP
266 /*CFI_REL_OFFSET cs,CS*/
267 /*CFI_REL_OFFSET rflags,EFLAGS*/
268 CFI_REL_OFFSET rsp,RSP
269 /*CFI_REL_OFFSET ss,SS*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 .endm
271/*
272 * A newly forked process directly context switches into this.
273 */
274/* rdi: prev */
275ENTRY(ret_from_fork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 CFI_DEFAULT_STACK
Andi Kleen658fdbe2006-09-26 10:52:41 +0200277 push kernel_eflags(%rip)
278 CFI_ADJUST_CFA_OFFSET 4
279 popf # reset kernel eflags
280 CFI_ADJUST_CFA_OFFSET -4
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 call schedule_tail
282 GET_THREAD_INFO(%rcx)
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300283 testl $(_TIF_SYSCALL_TRACE|_TIF_SYSCALL_AUDIT),TI_flags(%rcx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 jnz rff_trace
285rff_action:
286 RESTORE_REST
287 testl $3,CS-ARGOFFSET(%rsp) # from kernel_thread?
288 je int_ret_from_sys_call
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300289 testl $_TIF_IA32,TI_flags(%rcx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 jnz int_ret_from_sys_call
291 RESTORE_TOP_OF_STACK %rdi,ARGOFFSET
292 jmp ret_from_sys_call
293rff_trace:
294 movq %rsp,%rdi
295 call syscall_trace_leave
296 GET_THREAD_INFO(%rcx)
297 jmp rff_action
298 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200299END(ret_from_fork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301/*
302 * System call entry. Upto 6 arguments in registers are supported.
303 *
304 * SYSCALL does not save anything on the stack and does not change the
305 * stack pointer.
306 */
307
308/*
309 * Register setup:
310 * rax system call number
311 * rdi arg0
312 * rcx return address for syscall/sysret, C arg3
313 * rsi arg1
314 * rdx arg2
315 * r10 arg3 (--> moved to rcx for C)
316 * r8 arg4
317 * r9 arg5
318 * r11 eflags for syscall/sysret, temporary for C
319 * r12-r15,rbp,rbx saved by C code, not touched.
320 *
321 * Interrupts are off on entry.
322 * Only called from user space.
323 *
324 * XXX if we had a free scratch register we could save the RSP into the stack frame
325 * and report it properly in ps. Unfortunately we haven't.
Andi Kleen7bf36bb2006-04-07 19:50:00 +0200326 *
327 * When user can change the frames always force IRET. That is because
328 * it deals with uncanonical addresses better. SYSRET has trouble
329 * with them due to bugs in both AMD and Intel CPUs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 */
331
332ENTRY(system_call)
Jan Beulich7effaa82005-09-12 18:49:24 +0200333 CFI_STARTPROC simple
Jan Beulichadf14232006-09-26 10:52:41 +0200334 CFI_SIGNAL_FRAME
Jan Beulichdffead42006-06-26 13:57:38 +0200335 CFI_DEF_CFA rsp,PDA_STACKOFFSET
Jan Beulich7effaa82005-09-12 18:49:24 +0200336 CFI_REGISTER rip,rcx
337 /*CFI_REGISTER rflags,r11*/
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100338 SWAPGS_UNSAFE_STACK
339 /*
340 * A hypervisor implementation might want to use a label
341 * after the swapgs, so that it can do the swapgs
342 * for the guest and jump here on syscall.
343 */
344ENTRY(system_call_after_swapgs)
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 movq %rsp,%gs:pda_oldrsp
347 movq %gs:pda_kernelstack,%rsp
Ingo Molnar2601e642006-07-03 00:24:45 -0700348 /*
349 * No need to follow this irqs off/on section - it's straight
350 * and short:
351 */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100352 ENABLE_INTERRUPTS(CLBR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 SAVE_ARGS 8,1
354 movq %rax,ORIG_RAX-ARGOFFSET(%rsp)
Jan Beulich7effaa82005-09-12 18:49:24 +0200355 movq %rcx,RIP-ARGOFFSET(%rsp)
356 CFI_REL_OFFSET rip,RIP-ARGOFFSET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 GET_THREAD_INFO(%rcx)
Roland McGrathd4d67152008-07-09 02:38:07 -0700358 testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags(%rcx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 jnz tracesys
Roland McGrath86a1c342008-06-23 15:37:04 -0700360system_call_fastpath:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 cmpq $__NR_syscall_max,%rax
362 ja badsys
363 movq %r10,%rcx
364 call *sys_call_table(,%rax,8) # XXX: rip relative
365 movq %rax,RAX-ARGOFFSET(%rsp)
366/*
367 * Syscall return path ending with SYSRET (fast path)
368 * Has incomplete stack frame and undefined top of stack.
369 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370ret_from_sys_call:
Andi Kleen11b854b2005-04-16 15:25:02 -0700371 movl $_TIF_ALLWORK_MASK,%edi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 /* edi: flagmask */
373sysret_check:
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200374 LOCKDEP_SYS_EXIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 GET_THREAD_INFO(%rcx)
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100376 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700377 TRACE_IRQS_OFF
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300378 movl TI_flags(%rcx),%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 andl %edi,%edx
380 jnz sysret_careful
Jan Beulichbcddc012006-12-07 02:14:02 +0100381 CFI_REMEMBER_STATE
Ingo Molnar2601e642006-07-03 00:24:45 -0700382 /*
383 * sysretq will re-enable interrupts:
384 */
385 TRACE_IRQS_ON
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 movq RIP-ARGOFFSET(%rsp),%rcx
Jan Beulich7effaa82005-09-12 18:49:24 +0200387 CFI_REGISTER rip,rcx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 RESTORE_ARGS 0,-ARG_SKIP,1
Jan Beulich7effaa82005-09-12 18:49:24 +0200389 /*CFI_REGISTER rflags,r11*/
Jeremy Fitzhardingec7245da2008-06-25 00:19:27 -0400390 movq %gs:pda_oldrsp, %rsp
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -0400391 USERGS_SYSRET64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Jan Beulichbcddc012006-12-07 02:14:02 +0100393 CFI_RESTORE_STATE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 /* Handle reschedules */
395 /* edx: work, edi: workmask */
396sysret_careful:
397 bt $TIF_NEED_RESCHED,%edx
398 jnc sysret_signal
Ingo Molnar2601e642006-07-03 00:24:45 -0700399 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100400 ENABLE_INTERRUPTS(CLBR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 pushq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200402 CFI_ADJUST_CFA_OFFSET 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 call schedule
404 popq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200405 CFI_ADJUST_CFA_OFFSET -8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 jmp sysret_check
407
408 /* Handle a signal */
409sysret_signal:
Ingo Molnar2601e642006-07-03 00:24:45 -0700410 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100411 ENABLE_INTERRUPTS(CLBR_NONE)
Roland McGrath86a1c342008-06-23 15:37:04 -0700412#ifdef CONFIG_AUDITSYSCALL
413 bt $TIF_SYSCALL_AUDIT,%edx
414 jc sysret_audit
415#endif
Andi Kleen10ffdbb2005-05-16 21:53:19 -0700416 /* edx: work flags (arg3) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 leaq do_notify_resume(%rip),%rax
418 leaq -ARGOFFSET(%rsp),%rdi # &pt_regs -> arg1
419 xorl %esi,%esi # oldset -> arg2
420 call ptregscall_common
Roland McGrath15e8f342008-06-23 20:41:12 -0700421 movl $_TIF_WORK_MASK,%edi
Andi Kleen7bf36bb2006-04-07 19:50:00 +0200422 /* Use IRET because user could have changed frame. This
423 works because ptregscall_common has called FIXUP_TOP_OF_STACK. */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100424 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700425 TRACE_IRQS_OFF
Andi Kleen7bf36bb2006-04-07 19:50:00 +0200426 jmp int_with_check
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Jan Beulich7effaa82005-09-12 18:49:24 +0200428badsys:
429 movq $-ENOSYS,RAX-ARGOFFSET(%rsp)
430 jmp ret_from_sys_call
431
Roland McGrath86a1c342008-06-23 15:37:04 -0700432#ifdef CONFIG_AUDITSYSCALL
433 /*
434 * Fast path for syscall audit without full syscall trace.
435 * We just call audit_syscall_entry() directly, and then
436 * jump back to the normal fast path.
437 */
438auditsys:
439 movq %r10,%r9 /* 6th arg: 4th syscall arg */
440 movq %rdx,%r8 /* 5th arg: 3rd syscall arg */
441 movq %rsi,%rcx /* 4th arg: 2nd syscall arg */
442 movq %rdi,%rdx /* 3rd arg: 1st syscall arg */
443 movq %rax,%rsi /* 2nd arg: syscall number */
444 movl $AUDIT_ARCH_X86_64,%edi /* 1st arg: audit arch */
445 call audit_syscall_entry
446 LOAD_ARGS 0 /* reload call-clobbered registers */
447 jmp system_call_fastpath
448
449 /*
450 * Return fast path for syscall audit. Call audit_syscall_exit()
451 * directly and then jump back to the fast path with TIF_SYSCALL_AUDIT
452 * masked off.
453 */
454sysret_audit:
455 movq %rax,%rsi /* second arg, syscall return value */
456 cmpq $0,%rax /* is it < 0? */
457 setl %al /* 1 if so, 0 if not */
458 movzbl %al,%edi /* zero-extend that into %edi */
459 inc %edi /* first arg, 0->1(AUDITSC_SUCCESS), 1->2(AUDITSC_FAILURE) */
460 call audit_syscall_exit
461 movl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT),%edi
462 jmp sysret_check
463#endif /* CONFIG_AUDITSYSCALL */
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 /* Do syscall tracing */
466tracesys:
Roland McGrath86a1c342008-06-23 15:37:04 -0700467#ifdef CONFIG_AUDITSYSCALL
468 testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT),TI_flags(%rcx)
469 jz auditsys
470#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 SAVE_REST
Roland McGratha31f8dd2008-03-16 21:59:11 -0700472 movq $-ENOSYS,RAX(%rsp) /* ptrace can change this for a bad syscall */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 FIXUP_TOP_OF_STACK %rdi
474 movq %rsp,%rdi
475 call syscall_trace_enter
Roland McGrathd4d67152008-07-09 02:38:07 -0700476 /*
477 * Reload arg registers from stack in case ptrace changed them.
478 * We don't reload %rax because syscall_trace_enter() returned
479 * the value it wants us to use in the table lookup.
480 */
481 LOAD_ARGS ARGOFFSET, 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 RESTORE_REST
483 cmpq $__NR_syscall_max,%rax
Roland McGratha31f8dd2008-03-16 21:59:11 -0700484 ja int_ret_from_sys_call /* RAX(%rsp) set to -ENOSYS above */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 movq %r10,%rcx /* fixup for C */
486 call *sys_call_table(,%rax,8)
Roland McGratha31f8dd2008-03-16 21:59:11 -0700487 movq %rax,RAX-ARGOFFSET(%rsp)
Andi Kleen7bf36bb2006-04-07 19:50:00 +0200488 /* Use IRET because user could have changed frame */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490/*
491 * Syscall return path ending with IRET.
492 * Has correct top of stack, but partial stack frame.
Jan Beulichbcddc012006-12-07 02:14:02 +0100493 */
494 .globl int_ret_from_sys_call
495int_ret_from_sys_call:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100496 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700497 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 testl $3,CS-ARGOFFSET(%rsp)
499 je retint_restore_args
500 movl $_TIF_ALLWORK_MASK,%edi
501 /* edi: mask to check */
502int_with_check:
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200503 LOCKDEP_SYS_EXIT_IRQ
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 GET_THREAD_INFO(%rcx)
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300505 movl TI_flags(%rcx),%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 andl %edi,%edx
507 jnz int_careful
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300508 andl $~TS_COMPAT,TI_status(%rcx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 jmp retint_swapgs
510
511 /* Either reschedule or signal or syscall exit tracking needed. */
512 /* First do a reschedule test. */
513 /* edx: work, edi: workmask */
514int_careful:
515 bt $TIF_NEED_RESCHED,%edx
516 jnc int_very_careful
Ingo Molnar2601e642006-07-03 00:24:45 -0700517 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100518 ENABLE_INTERRUPTS(CLBR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 pushq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200520 CFI_ADJUST_CFA_OFFSET 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 call schedule
522 popq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200523 CFI_ADJUST_CFA_OFFSET -8
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100524 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700525 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 jmp int_with_check
527
528 /* handle signals and tracing -- both require a full stack frame */
529int_very_careful:
Ingo Molnar2601e642006-07-03 00:24:45 -0700530 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100531 ENABLE_INTERRUPTS(CLBR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 SAVE_REST
533 /* Check for syscall exit trace */
Roland McGrathd4d67152008-07-09 02:38:07 -0700534 testl $_TIF_WORK_SYSCALL_EXIT,%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 jz int_signal
536 pushq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200537 CFI_ADJUST_CFA_OFFSET 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 leaq 8(%rsp),%rdi # &ptregs -> arg1
539 call syscall_trace_leave
540 popq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200541 CFI_ADJUST_CFA_OFFSET -8
Roland McGrathd4d67152008-07-09 02:38:07 -0700542 andl $~(_TIF_WORK_SYSCALL_EXIT|_TIF_SYSCALL_EMU),%edi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 jmp int_restore_rest
544
545int_signal:
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100546 testl $_TIF_DO_NOTIFY_MASK,%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 jz 1f
548 movq %rsp,%rdi # &ptregs -> arg1
549 xorl %esi,%esi # oldset -> arg2
550 call do_notify_resume
Roland McGratheca91e72008-07-10 14:50:39 -07005511: movl $_TIF_WORK_MASK,%edi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552int_restore_rest:
553 RESTORE_REST
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100554 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700555 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 jmp int_with_check
557 CFI_ENDPROC
Jan Beulichbcddc012006-12-07 02:14:02 +0100558END(system_call)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560/*
561 * Certain special system calls that need to save a complete full stack frame.
562 */
563
564 .macro PTREGSCALL label,func,arg
565 .globl \label
566\label:
567 leaq \func(%rip),%rax
568 leaq -ARGOFFSET+8(%rsp),\arg /* 8 for return address */
569 jmp ptregscall_common
Jan Beulich4b787e02006-06-26 13:56:55 +0200570END(\label)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 .endm
572
Jan Beulich7effaa82005-09-12 18:49:24 +0200573 CFI_STARTPROC
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 PTREGSCALL stub_clone, sys_clone, %r8
576 PTREGSCALL stub_fork, sys_fork, %rdi
577 PTREGSCALL stub_vfork, sys_vfork, %rdi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 PTREGSCALL stub_sigaltstack, sys_sigaltstack, %rdx
579 PTREGSCALL stub_iopl, sys_iopl, %rsi
580
581ENTRY(ptregscall_common)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 popq %r11
Jan Beulich7effaa82005-09-12 18:49:24 +0200583 CFI_ADJUST_CFA_OFFSET -8
584 CFI_REGISTER rip, r11
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 SAVE_REST
586 movq %r11, %r15
Jan Beulich7effaa82005-09-12 18:49:24 +0200587 CFI_REGISTER rip, r15
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 FIXUP_TOP_OF_STACK %r11
589 call *%rax
590 RESTORE_TOP_OF_STACK %r11
591 movq %r15, %r11
Jan Beulich7effaa82005-09-12 18:49:24 +0200592 CFI_REGISTER rip, r11
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 RESTORE_REST
594 pushq %r11
Jan Beulich7effaa82005-09-12 18:49:24 +0200595 CFI_ADJUST_CFA_OFFSET 8
596 CFI_REL_OFFSET rip, 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 ret
598 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200599END(ptregscall_common)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601ENTRY(stub_execve)
602 CFI_STARTPROC
603 popq %r11
Jan Beulich7effaa82005-09-12 18:49:24 +0200604 CFI_ADJUST_CFA_OFFSET -8
605 CFI_REGISTER rip, r11
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 SAVE_REST
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 FIXUP_TOP_OF_STACK %r11
Ingo Molnar5d119b22008-02-26 12:55:57 +0100608 movq %rsp, %rcx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 call sys_execve
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 RESTORE_TOP_OF_STACK %r11
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 movq %rax,RAX(%rsp)
612 RESTORE_REST
613 jmp int_ret_from_sys_call
614 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200615END(stub_execve)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617/*
618 * sigreturn is special because it needs to restore all registers on return.
619 * This cannot be done with SYSRET, so use the IRET return path instead.
620 */
621ENTRY(stub_rt_sigreturn)
622 CFI_STARTPROC
Jan Beulich7effaa82005-09-12 18:49:24 +0200623 addq $8, %rsp
624 CFI_ADJUST_CFA_OFFSET -8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 SAVE_REST
626 movq %rsp,%rdi
627 FIXUP_TOP_OF_STACK %r11
628 call sys_rt_sigreturn
629 movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer
630 RESTORE_REST
631 jmp int_ret_from_sys_call
632 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200633END(stub_rt_sigreturn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Jan Beulich7effaa82005-09-12 18:49:24 +0200635/*
636 * initial frame state for interrupts and exceptions
637 */
638 .macro _frame ref
639 CFI_STARTPROC simple
Jan Beulichadf14232006-09-26 10:52:41 +0200640 CFI_SIGNAL_FRAME
Jan Beulich7effaa82005-09-12 18:49:24 +0200641 CFI_DEF_CFA rsp,SS+8-\ref
642 /*CFI_REL_OFFSET ss,SS-\ref*/
643 CFI_REL_OFFSET rsp,RSP-\ref
644 /*CFI_REL_OFFSET rflags,EFLAGS-\ref*/
645 /*CFI_REL_OFFSET cs,CS-\ref*/
646 CFI_REL_OFFSET rip,RIP-\ref
647 .endm
648
649/* initial frame state for interrupts (and exceptions without error code) */
650#define INTR_FRAME _frame RIP
651/* initial frame state for exceptions with error code (and interrupts with
652 vector already pushed) */
653#define XCPT_FRAME _frame ORIG_RAX
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655/*
656 * Interrupt entry/exit.
657 *
658 * Interrupt entry points save only callee clobbered registers in fast path.
659 *
660 * Entry runs with interrupts off.
661 */
662
663/* 0(%rsp): interrupt number */
664 .macro interrupt func
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 cld
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 SAVE_ARGS
667 leaq -ARGOFFSET(%rsp),%rdi # arg1 for handler
Jan Beulich1de9c3f2006-06-26 13:57:35 +0200668 pushq %rbp
669 CFI_ADJUST_CFA_OFFSET 8
670 CFI_REL_OFFSET rbp, 0
671 movq %rsp,%rbp
672 CFI_DEF_CFA_REGISTER rbp
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 testl $3,CS(%rdi)
674 je 1f
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100675 SWAPGS
Andi Kleen96e54042006-09-26 10:52:39 +0200676 /* irqcount is used to check if a CPU is already on an interrupt
677 stack or not. While this is essentially redundant with preempt_count
678 it is a little cheaper to use a separate counter in the PDA
679 (short of moving irq_enter into assembly, which would be too
680 much work) */
6811: incl %gs:pda_irqcount
Jan Beulich1de9c3f2006-06-26 13:57:35 +0200682 cmoveq %gs:pda_irqstackptr,%rsp
Andi Kleen26995002006-08-02 22:37:28 +0200683 push %rbp # backlink for old unwinder
Ingo Molnar2601e642006-07-03 00:24:45 -0700684 /*
685 * We entered an interrupt context - irqs are off:
686 */
687 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 call \func
689 .endm
690
691ENTRY(common_interrupt)
Jan Beulich7effaa82005-09-12 18:49:24 +0200692 XCPT_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 interrupt do_IRQ
694 /* 0(%rsp): oldrsp-ARGOFFSET */
Jan Beulich7effaa82005-09-12 18:49:24 +0200695ret_from_intr:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100696 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700697 TRACE_IRQS_OFF
Andi Kleen3829ee62005-07-28 21:15:48 -0700698 decl %gs:pda_irqcount
Jan Beulich1de9c3f2006-06-26 13:57:35 +0200699 leaveq
Jan Beulich7effaa82005-09-12 18:49:24 +0200700 CFI_DEF_CFA_REGISTER rsp
Jan Beulich1de9c3f2006-06-26 13:57:35 +0200701 CFI_ADJUST_CFA_OFFSET -8
Jan Beulich7effaa82005-09-12 18:49:24 +0200702exit_intr:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 GET_THREAD_INFO(%rcx)
704 testl $3,CS-ARGOFFSET(%rsp)
705 je retint_kernel
706
707 /* Interrupt came from user space */
708 /*
709 * Has a correct top of stack, but a partial stack frame
710 * %rcx: thread info. Interrupts off.
711 */
712retint_with_reschedule:
713 movl $_TIF_WORK_MASK,%edi
Jan Beulich7effaa82005-09-12 18:49:24 +0200714retint_check:
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200715 LOCKDEP_SYS_EXIT_IRQ
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300716 movl TI_flags(%rcx),%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 andl %edi,%edx
Jan Beulich7effaa82005-09-12 18:49:24 +0200718 CFI_REMEMBER_STATE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 jnz retint_careful
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200720
721retint_swapgs: /* return to user-space */
Ingo Molnar2601e642006-07-03 00:24:45 -0700722 /*
723 * The iretq could re-enable interrupts:
724 */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100725 DISABLE_INTERRUPTS(CLBR_ANY)
Ingo Molnar2601e642006-07-03 00:24:45 -0700726 TRACE_IRQS_IRETQ
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100727 SWAPGS
Ingo Molnar2601e642006-07-03 00:24:45 -0700728 jmp restore_args
729
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200730retint_restore_args: /* return to kernel space */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100731 DISABLE_INTERRUPTS(CLBR_ANY)
Ingo Molnar2601e642006-07-03 00:24:45 -0700732 /*
733 * The iretq could re-enable interrupts:
734 */
735 TRACE_IRQS_IRETQ
736restore_args:
Ingo Molnar3701d8632008-02-09 23:24:08 +0100737 RESTORE_ARGS 0,8,0
738
Adrian Bunkf7f3d792008-02-13 23:29:53 +0200739irq_return:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100740 INTERRUPT_RETURN
Ingo Molnar3701d8632008-02-09 23:24:08 +0100741
742 .section __ex_table, "a"
743 .quad irq_return, bad_iret
744 .previous
745
746#ifdef CONFIG_PARAVIRT
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100747ENTRY(native_iret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 iretq
749
750 .section __ex_table,"a"
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100751 .quad native_iret, bad_iret
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 .previous
Ingo Molnar3701d8632008-02-09 23:24:08 +0100753#endif
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 .section .fixup,"ax"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756bad_iret:
Roland McGrath3aa4b372008-02-06 22:39:43 +0100757 /*
758 * The iret traps when the %cs or %ss being restored is bogus.
759 * We've lost the original trap vector and error code.
760 * #GPF is the most likely one to get for an invalid selector.
761 * So pretend we completed the iret and took the #GPF in user mode.
762 *
763 * We are now running with the kernel GS after exception recovery.
764 * But error_entry expects us to have user GS to match the user %cs,
765 * so swap back.
766 */
767 pushq $0
768
769 SWAPGS
770 jmp general_protection
771
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100772 .previous
773
Jan Beulich7effaa82005-09-12 18:49:24 +0200774 /* edi: workmask, edx: work */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775retint_careful:
Jan Beulich7effaa82005-09-12 18:49:24 +0200776 CFI_RESTORE_STATE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 bt $TIF_NEED_RESCHED,%edx
778 jnc retint_signal
Ingo Molnar2601e642006-07-03 00:24:45 -0700779 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100780 ENABLE_INTERRUPTS(CLBR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 pushq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200782 CFI_ADJUST_CFA_OFFSET 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 call schedule
784 popq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200785 CFI_ADJUST_CFA_OFFSET -8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 GET_THREAD_INFO(%rcx)
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100787 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700788 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 jmp retint_check
790
791retint_signal:
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100792 testl $_TIF_DO_NOTIFY_MASK,%edx
Andi Kleen10ffdbb2005-05-16 21:53:19 -0700793 jz retint_swapgs
Ingo Molnar2601e642006-07-03 00:24:45 -0700794 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100795 ENABLE_INTERRUPTS(CLBR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 SAVE_REST
797 movq $-1,ORIG_RAX(%rsp)
Andi Kleen3829ee62005-07-28 21:15:48 -0700798 xorl %esi,%esi # oldset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 movq %rsp,%rdi # &pt_regs
800 call do_notify_resume
801 RESTORE_REST
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100802 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700803 TRACE_IRQS_OFF
Andi Kleenbe9e6872005-05-01 08:58:51 -0700804 GET_THREAD_INFO(%rcx)
Roland McGratheca91e72008-07-10 14:50:39 -0700805 jmp retint_with_reschedule
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807#ifdef CONFIG_PREEMPT
808 /* Returning to kernel space. Check if we need preemption */
809 /* rcx: threadinfo. interrupts off. */
Andi Kleenb06baba2006-09-26 10:52:29 +0200810ENTRY(retint_kernel)
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300811 cmpl $0,TI_preempt_count(%rcx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 jnz retint_restore_args
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300813 bt $TIF_NEED_RESCHED,TI_flags(%rcx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 jnc retint_restore_args
815 bt $9,EFLAGS-ARGOFFSET(%rsp) /* interrupts off? */
816 jnc retint_restore_args
817 call preempt_schedule_irq
818 jmp exit_intr
819#endif
Jan Beulich4b787e02006-06-26 13:56:55 +0200820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200822END(common_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824/*
825 * APIC interrupts.
826 */
827 .macro apicinterrupt num,func
Jan Beulich7effaa82005-09-12 18:49:24 +0200828 INTR_FRAME
Rusty Russell19eadf92006-06-27 02:53:44 -0700829 pushq $~(\num)
Jan Beulich7effaa82005-09-12 18:49:24 +0200830 CFI_ADJUST_CFA_OFFSET 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 interrupt \func
832 jmp ret_from_intr
833 CFI_ENDPROC
834 .endm
835
836ENTRY(thermal_interrupt)
837 apicinterrupt THERMAL_APIC_VECTOR,smp_thermal_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200838END(thermal_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Jacob Shin89b831e2005-11-05 17:25:53 +0100840ENTRY(threshold_interrupt)
841 apicinterrupt THRESHOLD_APIC_VECTOR,mce_threshold_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200842END(threshold_interrupt)
Jacob Shin89b831e2005-11-05 17:25:53 +0100843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844#ifdef CONFIG_SMP
845ENTRY(reschedule_interrupt)
846 apicinterrupt RESCHEDULE_VECTOR,smp_reschedule_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200847END(reschedule_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Andi Kleene5bc8b62005-09-12 18:49:24 +0200849 .macro INVALIDATE_ENTRY num
850ENTRY(invalidate_interrupt\num)
851 apicinterrupt INVALIDATE_TLB_VECTOR_START+\num,smp_invalidate_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200852END(invalidate_interrupt\num)
Andi Kleene5bc8b62005-09-12 18:49:24 +0200853 .endm
854
855 INVALIDATE_ENTRY 0
856 INVALIDATE_ENTRY 1
857 INVALIDATE_ENTRY 2
858 INVALIDATE_ENTRY 3
859 INVALIDATE_ENTRY 4
860 INVALIDATE_ENTRY 5
861 INVALIDATE_ENTRY 6
862 INVALIDATE_ENTRY 7
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864ENTRY(call_function_interrupt)
865 apicinterrupt CALL_FUNCTION_VECTOR,smp_call_function_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200866END(call_function_interrupt)
Jens Axboe3b16cf82008-06-26 11:21:54 +0200867ENTRY(call_function_single_interrupt)
868 apicinterrupt CALL_FUNCTION_SINGLE_VECTOR,smp_call_function_single_interrupt
869END(call_function_single_interrupt)
Eric W. Biederman61014292007-02-23 04:40:58 -0700870ENTRY(irq_move_cleanup_interrupt)
871 apicinterrupt IRQ_MOVE_CLEANUP_VECTOR,smp_irq_move_cleanup_interrupt
872END(irq_move_cleanup_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873#endif
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875ENTRY(apic_timer_interrupt)
876 apicinterrupt LOCAL_TIMER_VECTOR,smp_apic_timer_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200877END(apic_timer_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Cliff Wickman18129242008-06-02 08:56:14 -0500879ENTRY(uv_bau_message_intr1)
880 apicinterrupt 220,uv_bau_message_interrupt
881END(uv_bau_message_intr1)
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883ENTRY(error_interrupt)
884 apicinterrupt ERROR_APIC_VECTOR,smp_error_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200885END(error_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887ENTRY(spurious_interrupt)
888 apicinterrupt SPURIOUS_APIC_VECTOR,smp_spurious_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200889END(spurious_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891/*
892 * Exception entry points.
893 */
894 .macro zeroentry sym
Jan Beulich7effaa82005-09-12 18:49:24 +0200895 INTR_FRAME
Jeremy Fitzhardingefab58422008-06-25 00:19:31 -0400896 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 pushq $0 /* push error code/oldrax */
Jan Beulich7effaa82005-09-12 18:49:24 +0200898 CFI_ADJUST_CFA_OFFSET 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 pushq %rax /* push real oldrax to the rdi slot */
Jan Beulich7effaa82005-09-12 18:49:24 +0200900 CFI_ADJUST_CFA_OFFSET 8
Jan Beulich37550902007-05-02 19:27:05 +0200901 CFI_REL_OFFSET rax,0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 leaq \sym(%rip),%rax
903 jmp error_entry
Jan Beulich7effaa82005-09-12 18:49:24 +0200904 CFI_ENDPROC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 .endm
906
907 .macro errorentry sym
Jan Beulich7effaa82005-09-12 18:49:24 +0200908 XCPT_FRAME
Jeremy Fitzhardingefab58422008-06-25 00:19:31 -0400909 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 pushq %rax
Jan Beulich7effaa82005-09-12 18:49:24 +0200911 CFI_ADJUST_CFA_OFFSET 8
Jan Beulich37550902007-05-02 19:27:05 +0200912 CFI_REL_OFFSET rax,0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 leaq \sym(%rip),%rax
914 jmp error_entry
Jan Beulich7effaa82005-09-12 18:49:24 +0200915 CFI_ENDPROC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 .endm
917
918 /* error code is on the stack already */
919 /* handle NMI like exceptions that can happen everywhere */
Ingo Molnar2601e642006-07-03 00:24:45 -0700920 .macro paranoidentry sym, ist=0, irqtrace=1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 SAVE_ALL
922 cld
923 movl $1,%ebx
924 movl $MSR_GS_BASE,%ecx
925 rdmsr
926 testl %edx,%edx
927 js 1f
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100928 SWAPGS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 xorl %ebx,%ebx
Jan Beulichb556b352006-01-11 22:43:00 +01009301:
931 .if \ist
932 movq %gs:pda_data_offset, %rbp
933 .endif
934 movq %rsp,%rdi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 movq ORIG_RAX(%rsp),%rsi
936 movq $-1,ORIG_RAX(%rsp)
Jan Beulichb556b352006-01-11 22:43:00 +0100937 .if \ist
Andi Kleen5f8efbb2006-01-16 01:56:39 +0100938 subq $EXCEPTION_STKSZ, per_cpu__init_tss + TSS_ist + (\ist - 1) * 8(%rbp)
Jan Beulichb556b352006-01-11 22:43:00 +0100939 .endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 call \sym
Jan Beulichb556b352006-01-11 22:43:00 +0100941 .if \ist
Andi Kleen5f8efbb2006-01-16 01:56:39 +0100942 addq $EXCEPTION_STKSZ, per_cpu__init_tss + TSS_ist + (\ist - 1) * 8(%rbp)
Jan Beulichb556b352006-01-11 22:43:00 +0100943 .endif
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100944 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700945 .if \irqtrace
946 TRACE_IRQS_OFF
947 .endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 .endm
Ingo Molnar2601e642006-07-03 00:24:45 -0700949
950 /*
951 * "Paranoid" exit path from exception stack.
952 * Paranoid because this is used by NMIs and cannot take
953 * any kernel state for granted.
954 * We don't do kernel preemption checks here, because only
955 * NMI should be common and it does not enable IRQs and
956 * cannot get reschedule ticks.
957 *
958 * "trace" is 0 for the NMI handler only, because irq-tracing
959 * is fundamentally NMI-unsafe. (we cannot change the soft and
960 * hard flags at once, atomically)
961 */
962 .macro paranoidexit trace=1
963 /* ebx: no swapgs flag */
964paranoid_exit\trace:
965 testl %ebx,%ebx /* swapgs needed? */
966 jnz paranoid_restore\trace
967 testl $3,CS(%rsp)
968 jnz paranoid_userspace\trace
969paranoid_swapgs\trace:
Andi Kleen7a0a2df2006-09-26 10:52:37 +0200970 .if \trace
Ingo Molnar2601e642006-07-03 00:24:45 -0700971 TRACE_IRQS_IRETQ 0
Andi Kleen7a0a2df2006-09-26 10:52:37 +0200972 .endif
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100973 SWAPGS_UNSAFE_STACK
Ingo Molnar2601e642006-07-03 00:24:45 -0700974paranoid_restore\trace:
975 RESTORE_ALL 8
Ingo Molnar3701d8632008-02-09 23:24:08 +0100976 jmp irq_return
Ingo Molnar2601e642006-07-03 00:24:45 -0700977paranoid_userspace\trace:
978 GET_THREAD_INFO(%rcx)
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300979 movl TI_flags(%rcx),%ebx
Ingo Molnar2601e642006-07-03 00:24:45 -0700980 andl $_TIF_WORK_MASK,%ebx
981 jz paranoid_swapgs\trace
982 movq %rsp,%rdi /* &pt_regs */
983 call sync_regs
984 movq %rax,%rsp /* switch stack for scheduling */
985 testl $_TIF_NEED_RESCHED,%ebx
986 jnz paranoid_schedule\trace
987 movl %ebx,%edx /* arg3: thread flags */
988 .if \trace
989 TRACE_IRQS_ON
990 .endif
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100991 ENABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700992 xorl %esi,%esi /* arg2: oldset */
993 movq %rsp,%rdi /* arg1: &pt_regs */
994 call do_notify_resume
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100995 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700996 .if \trace
997 TRACE_IRQS_OFF
998 .endif
999 jmp paranoid_userspace\trace
1000paranoid_schedule\trace:
1001 .if \trace
1002 TRACE_IRQS_ON
1003 .endif
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001004 ENABLE_INTERRUPTS(CLBR_ANY)
Ingo Molnar2601e642006-07-03 00:24:45 -07001005 call schedule
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001006 DISABLE_INTERRUPTS(CLBR_ANY)
Ingo Molnar2601e642006-07-03 00:24:45 -07001007 .if \trace
1008 TRACE_IRQS_OFF
1009 .endif
1010 jmp paranoid_userspace\trace
1011 CFI_ENDPROC
1012 .endm
1013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014/*
1015 * Exception entry point. This expects an error code/orig_rax on the stack
1016 * and the exception handler in %rax.
1017 */
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001018KPROBE_ENTRY(error_entry)
Jan Beulich7effaa82005-09-12 18:49:24 +02001019 _frame RDI
Jan Beulich37550902007-05-02 19:27:05 +02001020 CFI_REL_OFFSET rax,0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 /* rdi slot contains rax, oldrax contains error code */
1022 cld
1023 subq $14*8,%rsp
1024 CFI_ADJUST_CFA_OFFSET (14*8)
1025 movq %rsi,13*8(%rsp)
1026 CFI_REL_OFFSET rsi,RSI
1027 movq 14*8(%rsp),%rsi /* load rax from rdi slot */
Jan Beulich37550902007-05-02 19:27:05 +02001028 CFI_REGISTER rax,rsi
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 movq %rdx,12*8(%rsp)
1030 CFI_REL_OFFSET rdx,RDX
1031 movq %rcx,11*8(%rsp)
1032 CFI_REL_OFFSET rcx,RCX
1033 movq %rsi,10*8(%rsp) /* store rax */
1034 CFI_REL_OFFSET rax,RAX
1035 movq %r8, 9*8(%rsp)
1036 CFI_REL_OFFSET r8,R8
1037 movq %r9, 8*8(%rsp)
1038 CFI_REL_OFFSET r9,R9
1039 movq %r10,7*8(%rsp)
1040 CFI_REL_OFFSET r10,R10
1041 movq %r11,6*8(%rsp)
1042 CFI_REL_OFFSET r11,R11
1043 movq %rbx,5*8(%rsp)
1044 CFI_REL_OFFSET rbx,RBX
1045 movq %rbp,4*8(%rsp)
1046 CFI_REL_OFFSET rbp,RBP
1047 movq %r12,3*8(%rsp)
1048 CFI_REL_OFFSET r12,R12
1049 movq %r13,2*8(%rsp)
1050 CFI_REL_OFFSET r13,R13
1051 movq %r14,1*8(%rsp)
1052 CFI_REL_OFFSET r14,R14
1053 movq %r15,(%rsp)
1054 CFI_REL_OFFSET r15,R15
1055 xorl %ebx,%ebx
1056 testl $3,CS(%rsp)
1057 je error_kernelspace
1058error_swapgs:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001059 SWAPGS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060error_sti:
1061 movq %rdi,RDI(%rsp)
Jan Beulich37550902007-05-02 19:27:05 +02001062 CFI_REL_OFFSET rdi,RDI
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 movq %rsp,%rdi
1064 movq ORIG_RAX(%rsp),%rsi /* get error code */
1065 movq $-1,ORIG_RAX(%rsp)
1066 call *%rax
Peter Zijlstra10cd7062007-10-11 22:11:12 +02001067 /* ebx: no swapgs flag (1: don't need swapgs, 0: need it) */
1068error_exit:
1069 movl %ebx,%eax
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 RESTORE_REST
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001071 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -07001072 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 GET_THREAD_INFO(%rcx)
1074 testl %eax,%eax
1075 jne retint_kernel
Peter Zijlstra10cd7062007-10-11 22:11:12 +02001076 LOCKDEP_SYS_EXIT_IRQ
Glauber Costa26ccb8a2008-06-24 11:19:35 -03001077 movl TI_flags(%rcx),%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 movl $_TIF_WORK_MASK,%edi
1079 andl %edi,%edx
1080 jnz retint_careful
Peter Zijlstra10cd7062007-10-11 22:11:12 +02001081 jmp retint_swapgs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 CFI_ENDPROC
1083
1084error_kernelspace:
1085 incl %ebx
1086 /* There are two places in the kernel that can potentially fault with
1087 usergs. Handle them here. The exception handlers after
1088 iret run with kernel gs again, so don't set the user space flag.
1089 B stepping K8s sometimes report an truncated RIP for IRET
1090 exceptions returning to compat mode. Check for these here too. */
Vegard Nossum9d8ad5d2008-06-27 17:22:17 +02001091 leaq irq_return(%rip),%rcx
1092 cmpq %rcx,RIP(%rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 je error_swapgs
Vegard Nossum9d8ad5d2008-06-27 17:22:17 +02001094 movl %ecx,%ecx /* zero extend */
1095 cmpq %rcx,RIP(%rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 je error_swapgs
1097 cmpq $gs_change,RIP(%rsp)
1098 je error_swapgs
1099 jmp error_sti
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001100KPROBE_END(error_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 /* Reload gs selector with exception handling */
1103 /* edi: new selector */
Jeremy Fitzhardinge9f9d4892008-06-25 00:19:32 -04001104ENTRY(native_load_gs_index)
Jan Beulich7effaa82005-09-12 18:49:24 +02001105 CFI_STARTPROC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 pushf
Jan Beulich7effaa82005-09-12 18:49:24 +02001107 CFI_ADJUST_CFA_OFFSET 8
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001108 DISABLE_INTERRUPTS(CLBR_ANY | ~(CLBR_RDI))
1109 SWAPGS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110gs_change:
1111 movl %edi,%gs
11122: mfence /* workaround */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001113 SWAPGS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 popf
Jan Beulich7effaa82005-09-12 18:49:24 +02001115 CFI_ADJUST_CFA_OFFSET -8
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 ret
Jan Beulich7effaa82005-09-12 18:49:24 +02001117 CFI_ENDPROC
Jeremy Fitzhardinge9f9d4892008-06-25 00:19:32 -04001118ENDPROC(native_load_gs_index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 .section __ex_table,"a"
1121 .align 8
1122 .quad gs_change,bad_gs
1123 .previous
1124 .section .fixup,"ax"
1125 /* running with kernelgs */
1126bad_gs:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001127 SWAPGS /* switch back to user gs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 xorl %eax,%eax
1129 movl %eax,%gs
1130 jmp 2b
1131 .previous
1132
1133/*
1134 * Create a kernel thread.
1135 *
1136 * C extern interface:
1137 * extern long kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
1138 *
1139 * asm input arguments:
1140 * rdi: fn, rsi: arg, rdx: flags
1141 */
1142ENTRY(kernel_thread)
1143 CFI_STARTPROC
1144 FAKE_STACK_FRAME $child_rip
1145 SAVE_ALL
1146
1147 # rdi: flags, rsi: usp, rdx: will be &pt_regs
1148 movq %rdx,%rdi
1149 orq kernel_thread_flags(%rip),%rdi
1150 movq $-1, %rsi
1151 movq %rsp, %rdx
1152
1153 xorl %r8d,%r8d
1154 xorl %r9d,%r9d
1155
1156 # clone now
1157 call do_fork
1158 movq %rax,RAX(%rsp)
1159 xorl %edi,%edi
1160
1161 /*
1162 * It isn't worth to check for reschedule here,
1163 * so internally to the x86_64 port you can rely on kernel_thread()
1164 * not to reschedule the child before returning, this avoids the need
1165 * of hacks for example to fork off the per-CPU idle tasks.
1166 * [Hopefully no generic code relies on the reschedule -AK]
1167 */
1168 RESTORE_ALL
1169 UNFAKE_STACK_FRAME
1170 ret
1171 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001172ENDPROC(kernel_thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174child_rip:
Andi Kleenc05991e2006-08-30 19:37:08 +02001175 pushq $0 # fake return address
1176 CFI_STARTPROC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 /*
1178 * Here we are in the child and the registers are set as they were
1179 * at kernel_thread() invocation in the parent.
1180 */
1181 movq %rdi, %rax
1182 movq %rsi, %rdi
1183 call *%rax
1184 # exit
Andrey Mirkin1c5b5cf2007-10-17 18:04:33 +02001185 mov %eax, %edi
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 call do_exit
Andi Kleenc05991e2006-08-30 19:37:08 +02001187 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001188ENDPROC(child_rip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190/*
1191 * execve(). This function needs to use IRET, not SYSRET, to set up all state properly.
1192 *
1193 * C extern interface:
1194 * extern long execve(char *name, char **argv, char **envp)
1195 *
1196 * asm input arguments:
1197 * rdi: name, rsi: argv, rdx: envp
1198 *
1199 * We want to fallback into:
Ingo Molnar5d119b22008-02-26 12:55:57 +01001200 * extern long sys_execve(char *name, char **argv,char **envp, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 *
1202 * do_sys_execve asm fallback arguments:
Ingo Molnar5d119b22008-02-26 12:55:57 +01001203 * rdi: name, rsi: argv, rdx: envp, rcx: fake frame on the stack
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 */
Arnd Bergmann3db03b42006-10-02 02:18:31 -07001205ENTRY(kernel_execve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 CFI_STARTPROC
1207 FAKE_STACK_FRAME $0
1208 SAVE_ALL
Ingo Molnar5d119b22008-02-26 12:55:57 +01001209 movq %rsp,%rcx
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 call sys_execve
1211 movq %rax, RAX(%rsp)
1212 RESTORE_REST
1213 testq %rax,%rax
1214 je int_ret_from_sys_call
1215 RESTORE_ARGS
1216 UNFAKE_STACK_FRAME
1217 ret
1218 CFI_ENDPROC
Arnd Bergmann3db03b42006-10-02 02:18:31 -07001219ENDPROC(kernel_execve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001221KPROBE_ENTRY(page_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 errorentry do_page_fault
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001223KPROBE_END(page_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225ENTRY(coprocessor_error)
1226 zeroentry do_coprocessor_error
Jan Beulich4b787e02006-06-26 13:56:55 +02001227END(coprocessor_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229ENTRY(simd_coprocessor_error)
1230 zeroentry do_simd_coprocessor_error
Jan Beulich4b787e02006-06-26 13:56:55 +02001231END(simd_coprocessor_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233ENTRY(device_not_available)
1234 zeroentry math_state_restore
Jan Beulich4b787e02006-06-26 13:56:55 +02001235END(device_not_available)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 /* runs on exception stack */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001238KPROBE_ENTRY(debug)
Jan Beulich7effaa82005-09-12 18:49:24 +02001239 INTR_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001240 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 pushq $0
1242 CFI_ADJUST_CFA_OFFSET 8
Andi Kleen5f8efbb2006-01-16 01:56:39 +01001243 paranoidentry do_debug, DEBUG_STACK
Ingo Molnar2601e642006-07-03 00:24:45 -07001244 paranoidexit
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001245KPROBE_END(debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 /* runs on exception stack */
Andi Kleeneddb6fb2006-02-03 21:50:41 +01001248KPROBE_ENTRY(nmi)
Jan Beulich7effaa82005-09-12 18:49:24 +02001249 INTR_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001250 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 pushq $-1
Jan Beulich7effaa82005-09-12 18:49:24 +02001252 CFI_ADJUST_CFA_OFFSET 8
Ingo Molnar2601e642006-07-03 00:24:45 -07001253 paranoidentry do_nmi, 0, 0
1254#ifdef CONFIG_TRACE_IRQFLAGS
1255 paranoidexit 0
1256#else
1257 jmp paranoid_exit1
1258 CFI_ENDPROC
1259#endif
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001260KPROBE_END(nmi)
Andi Kleen6fefb0d2005-04-16 15:25:03 -07001261
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001262KPROBE_ENTRY(int3)
Jan Beulichb556b352006-01-11 22:43:00 +01001263 INTR_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001264 PARAVIRT_ADJUST_EXCEPTION_FRAME
Jan Beulichb556b352006-01-11 22:43:00 +01001265 pushq $0
1266 CFI_ADJUST_CFA_OFFSET 8
Andi Kleen5f8efbb2006-01-16 01:56:39 +01001267 paranoidentry do_int3, DEBUG_STACK
Ingo Molnar2601e642006-07-03 00:24:45 -07001268 jmp paranoid_exit1
Jan Beulichb556b352006-01-11 22:43:00 +01001269 CFI_ENDPROC
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001270KPROBE_END(int3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272ENTRY(overflow)
1273 zeroentry do_overflow
Jan Beulich4b787e02006-06-26 13:56:55 +02001274END(overflow)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276ENTRY(bounds)
1277 zeroentry do_bounds
Jan Beulich4b787e02006-06-26 13:56:55 +02001278END(bounds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280ENTRY(invalid_op)
1281 zeroentry do_invalid_op
Jan Beulich4b787e02006-06-26 13:56:55 +02001282END(invalid_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284ENTRY(coprocessor_segment_overrun)
1285 zeroentry do_coprocessor_segment_overrun
Jan Beulich4b787e02006-06-26 13:56:55 +02001286END(coprocessor_segment_overrun)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 /* runs on exception stack */
1289ENTRY(double_fault)
Jan Beulich7effaa82005-09-12 18:49:24 +02001290 XCPT_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001291 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 paranoidentry do_double_fault
Ingo Molnar2601e642006-07-03 00:24:45 -07001293 jmp paranoid_exit1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001295END(double_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297ENTRY(invalid_TSS)
1298 errorentry do_invalid_TSS
Jan Beulich4b787e02006-06-26 13:56:55 +02001299END(invalid_TSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301ENTRY(segment_not_present)
1302 errorentry do_segment_not_present
Jan Beulich4b787e02006-06-26 13:56:55 +02001303END(segment_not_present)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 /* runs on exception stack */
1306ENTRY(stack_segment)
Jan Beulich7effaa82005-09-12 18:49:24 +02001307 XCPT_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001308 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 paranoidentry do_stack_segment
Ingo Molnar2601e642006-07-03 00:24:45 -07001310 jmp paranoid_exit1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001312END(stack_segment)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001314KPROBE_ENTRY(general_protection)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 errorentry do_general_protection
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001316KPROBE_END(general_protection)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318ENTRY(alignment_check)
1319 errorentry do_alignment_check
Jan Beulich4b787e02006-06-26 13:56:55 +02001320END(alignment_check)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322ENTRY(divide_error)
1323 zeroentry do_divide_error
Jan Beulich4b787e02006-06-26 13:56:55 +02001324END(divide_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326ENTRY(spurious_interrupt_bug)
1327 zeroentry do_spurious_interrupt_bug
Jan Beulich4b787e02006-06-26 13:56:55 +02001328END(spurious_interrupt_bug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330#ifdef CONFIG_X86_MCE
1331 /* runs on exception stack */
1332ENTRY(machine_check)
Jan Beulich7effaa82005-09-12 18:49:24 +02001333 INTR_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001334 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 pushq $0
1336 CFI_ADJUST_CFA_OFFSET 8
1337 paranoidentry do_machine_check
Ingo Molnar2601e642006-07-03 00:24:45 -07001338 jmp paranoid_exit1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001340END(machine_check)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341#endif
1342
Andi Kleen26995002006-08-02 22:37:28 +02001343/* Call softirq on interrupt stack. Interrupts are off. */
Andi Kleened6b6762005-07-28 21:15:49 -07001344ENTRY(call_softirq)
Jan Beulich7effaa82005-09-12 18:49:24 +02001345 CFI_STARTPROC
Andi Kleen26995002006-08-02 22:37:28 +02001346 push %rbp
1347 CFI_ADJUST_CFA_OFFSET 8
1348 CFI_REL_OFFSET rbp,0
1349 mov %rsp,%rbp
1350 CFI_DEF_CFA_REGISTER rbp
Andi Kleened6b6762005-07-28 21:15:49 -07001351 incl %gs:pda_irqcount
Andi Kleen26995002006-08-02 22:37:28 +02001352 cmove %gs:pda_irqstackptr,%rsp
1353 push %rbp # backlink for old unwinder
Andi Kleened6b6762005-07-28 21:15:49 -07001354 call __do_softirq
Andi Kleen26995002006-08-02 22:37:28 +02001355 leaveq
Jan Beulich7effaa82005-09-12 18:49:24 +02001356 CFI_DEF_CFA_REGISTER rsp
Andi Kleen26995002006-08-02 22:37:28 +02001357 CFI_ADJUST_CFA_OFFSET -8
Andi Kleened6b6762005-07-28 21:15:49 -07001358 decl %gs:pda_irqcount
Andi Kleened6b6762005-07-28 21:15:49 -07001359 ret
Jan Beulich7effaa82005-09-12 18:49:24 +02001360 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001361ENDPROC(call_softirq)
Andi Kleen75154f42007-06-23 02:29:25 +02001362
1363KPROBE_ENTRY(ignore_sysret)
1364 CFI_STARTPROC
1365 mov $-ENOSYS,%eax
1366 sysret
1367 CFI_ENDPROC
1368ENDPROC(ignore_sysret)
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001369
1370#ifdef CONFIG_XEN
1371ENTRY(xen_hypervisor_callback)
1372 zeroentry xen_do_hypervisor_callback
1373END(xen_hypervisor_callback)
1374
1375/*
1376# A note on the "critical region" in our callback handler.
1377# We want to avoid stacking callback handlers due to events occurring
1378# during handling of the last event. To do this, we keep events disabled
1379# until we've done all processing. HOWEVER, we must enable events before
1380# popping the stack frame (can't be done atomically) and so it would still
1381# be possible to get enough handler activations to overflow the stack.
1382# Although unlikely, bugs of that kind are hard to track down, so we'd
1383# like to avoid the possibility.
1384# So, on entry to the handler we detect whether we interrupted an
1385# existing activation in its critical region -- if so, we pop the current
1386# activation and restart the handler using the previous one.
1387*/
1388ENTRY(xen_do_hypervisor_callback) # do_hypervisor_callback(struct *pt_regs)
1389 CFI_STARTPROC
1390/* Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
1391 see the correct pointer to the pt_regs */
1392 movq %rdi, %rsp # we don't return, adjust the stack frame
1393 CFI_ENDPROC
1394 CFI_DEFAULT_STACK
139511: incl %gs:pda_irqcount
1396 movq %rsp,%rbp
1397 CFI_DEF_CFA_REGISTER rbp
1398 cmovzq %gs:pda_irqstackptr,%rsp
1399 pushq %rbp # backlink for old unwinder
1400 call xen_evtchn_do_upcall
1401 popq %rsp
1402 CFI_DEF_CFA_REGISTER rsp
1403 decl %gs:pda_irqcount
1404 jmp error_exit
1405 CFI_ENDPROC
1406END(do_hypervisor_callback)
1407
1408/*
1409# Hypervisor uses this for application faults while it executes.
1410# We get here for two reasons:
1411# 1. Fault while reloading DS, ES, FS or GS
1412# 2. Fault while executing IRET
1413# Category 1 we do not need to fix up as Xen has already reloaded all segment
1414# registers that could be reloaded and zeroed the others.
1415# Category 2 we fix up by killing the current process. We cannot use the
1416# normal Linux return path in this case because if we use the IRET hypercall
1417# to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1418# We distinguish between categories by comparing each saved segment register
1419# with its current contents: any discrepancy means we in category 1.
1420*/
1421ENTRY(xen_failsafe_callback)
Jeremy Fitzhardinge4a5c3e72008-07-08 15:07:09 -07001422 framesz = (RIP-0x30) /* workaround buggy gas */
1423 _frame framesz
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001424 CFI_REL_OFFSET rcx, 0
1425 CFI_REL_OFFSET r11, 8
1426 movw %ds,%cx
1427 cmpw %cx,0x10(%rsp)
1428 CFI_REMEMBER_STATE
1429 jne 1f
1430 movw %es,%cx
1431 cmpw %cx,0x18(%rsp)
1432 jne 1f
1433 movw %fs,%cx
1434 cmpw %cx,0x20(%rsp)
1435 jne 1f
1436 movw %gs,%cx
1437 cmpw %cx,0x28(%rsp)
1438 jne 1f
1439 /* All segments match their saved values => Category 2 (Bad IRET). */
1440 movq (%rsp),%rcx
1441 CFI_RESTORE rcx
1442 movq 8(%rsp),%r11
1443 CFI_RESTORE r11
1444 addq $0x30,%rsp
1445 CFI_ADJUST_CFA_OFFSET -0x30
Jeremy Fitzhardinge4a5c3e72008-07-08 15:07:09 -07001446 pushq $0
1447 CFI_ADJUST_CFA_OFFSET 8
1448 pushq %r11
1449 CFI_ADJUST_CFA_OFFSET 8
1450 pushq %rcx
1451 CFI_ADJUST_CFA_OFFSET 8
1452 jmp general_protection
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001453 CFI_RESTORE_STATE
14541: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
1455 movq (%rsp),%rcx
1456 CFI_RESTORE rcx
1457 movq 8(%rsp),%r11
1458 CFI_RESTORE r11
1459 addq $0x30,%rsp
1460 CFI_ADJUST_CFA_OFFSET -0x30
1461 pushq $0
1462 CFI_ADJUST_CFA_OFFSET 8
1463 SAVE_ALL
1464 jmp error_exit
1465 CFI_ENDPROC
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001466END(xen_failsafe_callback)
1467
1468#endif /* CONFIG_XEN */