blob: 963b35b81bcc5dc3a9cde29168ade5c1ad1fa79d [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)
Alexander van Heukelume0a5a5d2008-07-22 18:14:16 +0200278 CFI_ADJUST_CFA_OFFSET 8
Andi Kleen658fdbe2006-09-26 10:52:41 +0200279 popf # reset kernel eflags
Alexander van Heukelume0a5a5d2008-07-22 18:14:16 +0200280 CFI_ADJUST_CFA_OFFSET -8
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
Roland McGrath5cbf1562008-06-24 01:13:31 -0700495 .globl int_with_check
Jan Beulichbcddc012006-12-07 02:14:02 +0100496int_ret_from_sys_call:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100497 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700498 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 testl $3,CS-ARGOFFSET(%rsp)
500 je retint_restore_args
501 movl $_TIF_ALLWORK_MASK,%edi
502 /* edi: mask to check */
503int_with_check:
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200504 LOCKDEP_SYS_EXIT_IRQ
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 GET_THREAD_INFO(%rcx)
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300506 movl TI_flags(%rcx),%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 andl %edi,%edx
508 jnz int_careful
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300509 andl $~TS_COMPAT,TI_status(%rcx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 jmp retint_swapgs
511
512 /* Either reschedule or signal or syscall exit tracking needed. */
513 /* First do a reschedule test. */
514 /* edx: work, edi: workmask */
515int_careful:
516 bt $TIF_NEED_RESCHED,%edx
517 jnc int_very_careful
Ingo Molnar2601e642006-07-03 00:24:45 -0700518 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100519 ENABLE_INTERRUPTS(CLBR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 pushq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200521 CFI_ADJUST_CFA_OFFSET 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 call schedule
523 popq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200524 CFI_ADJUST_CFA_OFFSET -8
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100525 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700526 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 jmp int_with_check
528
529 /* handle signals and tracing -- both require a full stack frame */
530int_very_careful:
Ingo Molnar2601e642006-07-03 00:24:45 -0700531 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100532 ENABLE_INTERRUPTS(CLBR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 SAVE_REST
534 /* Check for syscall exit trace */
Roland McGrathd4d67152008-07-09 02:38:07 -0700535 testl $_TIF_WORK_SYSCALL_EXIT,%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 jz int_signal
537 pushq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200538 CFI_ADJUST_CFA_OFFSET 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 leaq 8(%rsp),%rdi # &ptregs -> arg1
540 call syscall_trace_leave
541 popq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200542 CFI_ADJUST_CFA_OFFSET -8
Roland McGrathd4d67152008-07-09 02:38:07 -0700543 andl $~(_TIF_WORK_SYSCALL_EXIT|_TIF_SYSCALL_EMU),%edi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 jmp int_restore_rest
545
546int_signal:
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100547 testl $_TIF_DO_NOTIFY_MASK,%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 jz 1f
549 movq %rsp,%rdi # &ptregs -> arg1
550 xorl %esi,%esi # oldset -> arg2
551 call do_notify_resume
Roland McGratheca91e72008-07-10 14:50:39 -07005521: movl $_TIF_WORK_MASK,%edi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553int_restore_rest:
554 RESTORE_REST
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100555 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700556 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 jmp int_with_check
558 CFI_ENDPROC
Jan Beulichbcddc012006-12-07 02:14:02 +0100559END(system_call)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561/*
562 * Certain special system calls that need to save a complete full stack frame.
563 */
564
565 .macro PTREGSCALL label,func,arg
566 .globl \label
567\label:
568 leaq \func(%rip),%rax
569 leaq -ARGOFFSET+8(%rsp),\arg /* 8 for return address */
570 jmp ptregscall_common
Jan Beulich4b787e02006-06-26 13:56:55 +0200571END(\label)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 .endm
573
Jan Beulich7effaa82005-09-12 18:49:24 +0200574 CFI_STARTPROC
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 PTREGSCALL stub_clone, sys_clone, %r8
577 PTREGSCALL stub_fork, sys_fork, %rdi
578 PTREGSCALL stub_vfork, sys_vfork, %rdi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 PTREGSCALL stub_sigaltstack, sys_sigaltstack, %rdx
580 PTREGSCALL stub_iopl, sys_iopl, %rsi
581
582ENTRY(ptregscall_common)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 popq %r11
Jan Beulich7effaa82005-09-12 18:49:24 +0200584 CFI_ADJUST_CFA_OFFSET -8
585 CFI_REGISTER rip, r11
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 SAVE_REST
587 movq %r11, %r15
Jan Beulich7effaa82005-09-12 18:49:24 +0200588 CFI_REGISTER rip, r15
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 FIXUP_TOP_OF_STACK %r11
590 call *%rax
591 RESTORE_TOP_OF_STACK %r11
592 movq %r15, %r11
Jan Beulich7effaa82005-09-12 18:49:24 +0200593 CFI_REGISTER rip, r11
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 RESTORE_REST
595 pushq %r11
Jan Beulich7effaa82005-09-12 18:49:24 +0200596 CFI_ADJUST_CFA_OFFSET 8
597 CFI_REL_OFFSET rip, 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 ret
599 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200600END(ptregscall_common)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602ENTRY(stub_execve)
603 CFI_STARTPROC
604 popq %r11
Jan Beulich7effaa82005-09-12 18:49:24 +0200605 CFI_ADJUST_CFA_OFFSET -8
606 CFI_REGISTER rip, r11
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 SAVE_REST
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 FIXUP_TOP_OF_STACK %r11
Ingo Molnar5d119b22008-02-26 12:55:57 +0100609 movq %rsp, %rcx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 call sys_execve
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 RESTORE_TOP_OF_STACK %r11
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 movq %rax,RAX(%rsp)
613 RESTORE_REST
614 jmp int_ret_from_sys_call
615 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200616END(stub_execve)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618/*
619 * sigreturn is special because it needs to restore all registers on return.
620 * This cannot be done with SYSRET, so use the IRET return path instead.
621 */
622ENTRY(stub_rt_sigreturn)
623 CFI_STARTPROC
Jan Beulich7effaa82005-09-12 18:49:24 +0200624 addq $8, %rsp
625 CFI_ADJUST_CFA_OFFSET -8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 SAVE_REST
627 movq %rsp,%rdi
628 FIXUP_TOP_OF_STACK %r11
629 call sys_rt_sigreturn
630 movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer
631 RESTORE_REST
632 jmp int_ret_from_sys_call
633 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200634END(stub_rt_sigreturn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Jan Beulich7effaa82005-09-12 18:49:24 +0200636/*
637 * initial frame state for interrupts and exceptions
638 */
639 .macro _frame ref
640 CFI_STARTPROC simple
Jan Beulichadf14232006-09-26 10:52:41 +0200641 CFI_SIGNAL_FRAME
Jan Beulich7effaa82005-09-12 18:49:24 +0200642 CFI_DEF_CFA rsp,SS+8-\ref
643 /*CFI_REL_OFFSET ss,SS-\ref*/
644 CFI_REL_OFFSET rsp,RSP-\ref
645 /*CFI_REL_OFFSET rflags,EFLAGS-\ref*/
646 /*CFI_REL_OFFSET cs,CS-\ref*/
647 CFI_REL_OFFSET rip,RIP-\ref
648 .endm
649
650/* initial frame state for interrupts (and exceptions without error code) */
651#define INTR_FRAME _frame RIP
652/* initial frame state for exceptions with error code (and interrupts with
653 vector already pushed) */
654#define XCPT_FRAME _frame ORIG_RAX
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656/*
657 * Interrupt entry/exit.
658 *
659 * Interrupt entry points save only callee clobbered registers in fast path.
660 *
661 * Entry runs with interrupts off.
662 */
663
664/* 0(%rsp): interrupt number */
665 .macro interrupt func
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 cld
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 SAVE_ARGS
668 leaq -ARGOFFSET(%rsp),%rdi # arg1 for handler
Jan Beulich1de9c3f2006-06-26 13:57:35 +0200669 pushq %rbp
Glauber Costa097a0782008-08-14 17:33:12 -0300670 /*
671 * Save rbp twice: One is for marking the stack frame, as usual, and the
672 * other, to fill pt_regs properly. This is because bx comes right
673 * before the last saved register in that structure, and not bp. If the
674 * base pointer were in the place bx is today, this would not be needed.
675 */
676 movq %rbp, -8(%rsp)
Jan Beulich1de9c3f2006-06-26 13:57:35 +0200677 CFI_ADJUST_CFA_OFFSET 8
678 CFI_REL_OFFSET rbp, 0
679 movq %rsp,%rbp
680 CFI_DEF_CFA_REGISTER rbp
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 testl $3,CS(%rdi)
682 je 1f
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100683 SWAPGS
Andi Kleen96e54042006-09-26 10:52:39 +0200684 /* irqcount is used to check if a CPU is already on an interrupt
685 stack or not. While this is essentially redundant with preempt_count
686 it is a little cheaper to use a separate counter in the PDA
687 (short of moving irq_enter into assembly, which would be too
688 much work) */
6891: incl %gs:pda_irqcount
Jan Beulich1de9c3f2006-06-26 13:57:35 +0200690 cmoveq %gs:pda_irqstackptr,%rsp
Andi Kleen26995002006-08-02 22:37:28 +0200691 push %rbp # backlink for old unwinder
Ingo Molnar2601e642006-07-03 00:24:45 -0700692 /*
693 * We entered an interrupt context - irqs are off:
694 */
695 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 call \func
697 .endm
698
699ENTRY(common_interrupt)
Jan Beulich7effaa82005-09-12 18:49:24 +0200700 XCPT_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 interrupt do_IRQ
702 /* 0(%rsp): oldrsp-ARGOFFSET */
Jan Beulich7effaa82005-09-12 18:49:24 +0200703ret_from_intr:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100704 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700705 TRACE_IRQS_OFF
Andi Kleen3829ee62005-07-28 21:15:48 -0700706 decl %gs:pda_irqcount
Jan Beulich1de9c3f2006-06-26 13:57:35 +0200707 leaveq
Jan Beulich7effaa82005-09-12 18:49:24 +0200708 CFI_DEF_CFA_REGISTER rsp
Jan Beulich1de9c3f2006-06-26 13:57:35 +0200709 CFI_ADJUST_CFA_OFFSET -8
Jan Beulich7effaa82005-09-12 18:49:24 +0200710exit_intr:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 GET_THREAD_INFO(%rcx)
712 testl $3,CS-ARGOFFSET(%rsp)
713 je retint_kernel
714
715 /* Interrupt came from user space */
716 /*
717 * Has a correct top of stack, but a partial stack frame
718 * %rcx: thread info. Interrupts off.
719 */
720retint_with_reschedule:
721 movl $_TIF_WORK_MASK,%edi
Jan Beulich7effaa82005-09-12 18:49:24 +0200722retint_check:
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200723 LOCKDEP_SYS_EXIT_IRQ
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300724 movl TI_flags(%rcx),%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 andl %edi,%edx
Jan Beulich7effaa82005-09-12 18:49:24 +0200726 CFI_REMEMBER_STATE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 jnz retint_careful
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200728
729retint_swapgs: /* return to user-space */
Ingo Molnar2601e642006-07-03 00:24:45 -0700730 /*
731 * The iretq could re-enable interrupts:
732 */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100733 DISABLE_INTERRUPTS(CLBR_ANY)
Ingo Molnar2601e642006-07-03 00:24:45 -0700734 TRACE_IRQS_IRETQ
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100735 SWAPGS
Ingo Molnar2601e642006-07-03 00:24:45 -0700736 jmp restore_args
737
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200738retint_restore_args: /* return to kernel space */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100739 DISABLE_INTERRUPTS(CLBR_ANY)
Ingo Molnar2601e642006-07-03 00:24:45 -0700740 /*
741 * The iretq could re-enable interrupts:
742 */
743 TRACE_IRQS_IRETQ
744restore_args:
Ingo Molnar3701d8632008-02-09 23:24:08 +0100745 RESTORE_ARGS 0,8,0
746
Adrian Bunkf7f3d792008-02-13 23:29:53 +0200747irq_return:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100748 INTERRUPT_RETURN
Ingo Molnar3701d8632008-02-09 23:24:08 +0100749
750 .section __ex_table, "a"
751 .quad irq_return, bad_iret
752 .previous
753
754#ifdef CONFIG_PARAVIRT
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100755ENTRY(native_iret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 iretq
757
758 .section __ex_table,"a"
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100759 .quad native_iret, bad_iret
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 .previous
Ingo Molnar3701d8632008-02-09 23:24:08 +0100761#endif
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 .section .fixup,"ax"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764bad_iret:
Roland McGrath3aa4b372008-02-06 22:39:43 +0100765 /*
766 * The iret traps when the %cs or %ss being restored is bogus.
767 * We've lost the original trap vector and error code.
768 * #GPF is the most likely one to get for an invalid selector.
769 * So pretend we completed the iret and took the #GPF in user mode.
770 *
771 * We are now running with the kernel GS after exception recovery.
772 * But error_entry expects us to have user GS to match the user %cs,
773 * so swap back.
774 */
775 pushq $0
776
777 SWAPGS
778 jmp general_protection
779
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100780 .previous
781
Jan Beulich7effaa82005-09-12 18:49:24 +0200782 /* edi: workmask, edx: work */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783retint_careful:
Jan Beulich7effaa82005-09-12 18:49:24 +0200784 CFI_RESTORE_STATE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 bt $TIF_NEED_RESCHED,%edx
786 jnc retint_signal
Ingo Molnar2601e642006-07-03 00:24:45 -0700787 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100788 ENABLE_INTERRUPTS(CLBR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 pushq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200790 CFI_ADJUST_CFA_OFFSET 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 call schedule
792 popq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200793 CFI_ADJUST_CFA_OFFSET -8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 GET_THREAD_INFO(%rcx)
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100795 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700796 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 jmp retint_check
798
799retint_signal:
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100800 testl $_TIF_DO_NOTIFY_MASK,%edx
Andi Kleen10ffdbb2005-05-16 21:53:19 -0700801 jz retint_swapgs
Ingo Molnar2601e642006-07-03 00:24:45 -0700802 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100803 ENABLE_INTERRUPTS(CLBR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 SAVE_REST
805 movq $-1,ORIG_RAX(%rsp)
Andi Kleen3829ee62005-07-28 21:15:48 -0700806 xorl %esi,%esi # oldset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 movq %rsp,%rdi # &pt_regs
808 call do_notify_resume
809 RESTORE_REST
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100810 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700811 TRACE_IRQS_OFF
Andi Kleenbe9e6872005-05-01 08:58:51 -0700812 GET_THREAD_INFO(%rcx)
Roland McGratheca91e72008-07-10 14:50:39 -0700813 jmp retint_with_reschedule
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815#ifdef CONFIG_PREEMPT
816 /* Returning to kernel space. Check if we need preemption */
817 /* rcx: threadinfo. interrupts off. */
Andi Kleenb06baba2006-09-26 10:52:29 +0200818ENTRY(retint_kernel)
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300819 cmpl $0,TI_preempt_count(%rcx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 jnz retint_restore_args
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300821 bt $TIF_NEED_RESCHED,TI_flags(%rcx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 jnc retint_restore_args
823 bt $9,EFLAGS-ARGOFFSET(%rsp) /* interrupts off? */
824 jnc retint_restore_args
825 call preempt_schedule_irq
826 jmp exit_intr
827#endif
Jan Beulich4b787e02006-06-26 13:56:55 +0200828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200830END(common_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832/*
833 * APIC interrupts.
834 */
835 .macro apicinterrupt num,func
Jan Beulich7effaa82005-09-12 18:49:24 +0200836 INTR_FRAME
Rusty Russell19eadf92006-06-27 02:53:44 -0700837 pushq $~(\num)
Jan Beulich7effaa82005-09-12 18:49:24 +0200838 CFI_ADJUST_CFA_OFFSET 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 interrupt \func
840 jmp ret_from_intr
841 CFI_ENDPROC
842 .endm
843
844ENTRY(thermal_interrupt)
845 apicinterrupt THERMAL_APIC_VECTOR,smp_thermal_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200846END(thermal_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Jacob Shin89b831e2005-11-05 17:25:53 +0100848ENTRY(threshold_interrupt)
849 apicinterrupt THRESHOLD_APIC_VECTOR,mce_threshold_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200850END(threshold_interrupt)
Jacob Shin89b831e2005-11-05 17:25:53 +0100851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852#ifdef CONFIG_SMP
853ENTRY(reschedule_interrupt)
854 apicinterrupt RESCHEDULE_VECTOR,smp_reschedule_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200855END(reschedule_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Andi Kleene5bc8b62005-09-12 18:49:24 +0200857 .macro INVALIDATE_ENTRY num
858ENTRY(invalidate_interrupt\num)
859 apicinterrupt INVALIDATE_TLB_VECTOR_START+\num,smp_invalidate_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200860END(invalidate_interrupt\num)
Andi Kleene5bc8b62005-09-12 18:49:24 +0200861 .endm
862
863 INVALIDATE_ENTRY 0
864 INVALIDATE_ENTRY 1
865 INVALIDATE_ENTRY 2
866 INVALIDATE_ENTRY 3
867 INVALIDATE_ENTRY 4
868 INVALIDATE_ENTRY 5
869 INVALIDATE_ENTRY 6
870 INVALIDATE_ENTRY 7
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872ENTRY(call_function_interrupt)
873 apicinterrupt CALL_FUNCTION_VECTOR,smp_call_function_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200874END(call_function_interrupt)
Jens Axboe3b16cf82008-06-26 11:21:54 +0200875ENTRY(call_function_single_interrupt)
876 apicinterrupt CALL_FUNCTION_SINGLE_VECTOR,smp_call_function_single_interrupt
877END(call_function_single_interrupt)
Eric W. Biederman61014292007-02-23 04:40:58 -0700878ENTRY(irq_move_cleanup_interrupt)
879 apicinterrupt IRQ_MOVE_CLEANUP_VECTOR,smp_irq_move_cleanup_interrupt
880END(irq_move_cleanup_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881#endif
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883ENTRY(apic_timer_interrupt)
884 apicinterrupt LOCAL_TIMER_VECTOR,smp_apic_timer_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200885END(apic_timer_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Cliff Wickman18129242008-06-02 08:56:14 -0500887ENTRY(uv_bau_message_intr1)
888 apicinterrupt 220,uv_bau_message_interrupt
889END(uv_bau_message_intr1)
890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891ENTRY(error_interrupt)
892 apicinterrupt ERROR_APIC_VECTOR,smp_error_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200893END(error_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895ENTRY(spurious_interrupt)
896 apicinterrupt SPURIOUS_APIC_VECTOR,smp_spurious_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200897END(spurious_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899/*
900 * Exception entry points.
901 */
902 .macro zeroentry sym
Jan Beulich7effaa82005-09-12 18:49:24 +0200903 INTR_FRAME
Jeremy Fitzhardingefab58422008-06-25 00:19:31 -0400904 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 pushq $0 /* push error code/oldrax */
Jan Beulich7effaa82005-09-12 18:49:24 +0200906 CFI_ADJUST_CFA_OFFSET 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 pushq %rax /* push real oldrax to the rdi slot */
Jan Beulich7effaa82005-09-12 18:49:24 +0200908 CFI_ADJUST_CFA_OFFSET 8
Jan Beulich37550902007-05-02 19:27:05 +0200909 CFI_REL_OFFSET rax,0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 leaq \sym(%rip),%rax
911 jmp error_entry
Jan Beulich7effaa82005-09-12 18:49:24 +0200912 CFI_ENDPROC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 .endm
914
915 .macro errorentry sym
Jan Beulich7effaa82005-09-12 18:49:24 +0200916 XCPT_FRAME
Jeremy Fitzhardingefab58422008-06-25 00:19:31 -0400917 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 pushq %rax
Jan Beulich7effaa82005-09-12 18:49:24 +0200919 CFI_ADJUST_CFA_OFFSET 8
Jan Beulich37550902007-05-02 19:27:05 +0200920 CFI_REL_OFFSET rax,0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 leaq \sym(%rip),%rax
922 jmp error_entry
Jan Beulich7effaa82005-09-12 18:49:24 +0200923 CFI_ENDPROC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 .endm
925
926 /* error code is on the stack already */
927 /* handle NMI like exceptions that can happen everywhere */
Ingo Molnar2601e642006-07-03 00:24:45 -0700928 .macro paranoidentry sym, ist=0, irqtrace=1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 SAVE_ALL
930 cld
931 movl $1,%ebx
932 movl $MSR_GS_BASE,%ecx
933 rdmsr
934 testl %edx,%edx
935 js 1f
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100936 SWAPGS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 xorl %ebx,%ebx
Jan Beulichb556b352006-01-11 22:43:00 +01009381:
939 .if \ist
940 movq %gs:pda_data_offset, %rbp
941 .endif
942 movq %rsp,%rdi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 movq ORIG_RAX(%rsp),%rsi
944 movq $-1,ORIG_RAX(%rsp)
Jan Beulichb556b352006-01-11 22:43:00 +0100945 .if \ist
Andi Kleen5f8efbb2006-01-16 01:56:39 +0100946 subq $EXCEPTION_STKSZ, per_cpu__init_tss + TSS_ist + (\ist - 1) * 8(%rbp)
Jan Beulichb556b352006-01-11 22:43:00 +0100947 .endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 call \sym
Jan Beulichb556b352006-01-11 22:43:00 +0100949 .if \ist
Andi Kleen5f8efbb2006-01-16 01:56:39 +0100950 addq $EXCEPTION_STKSZ, per_cpu__init_tss + TSS_ist + (\ist - 1) * 8(%rbp)
Jan Beulichb556b352006-01-11 22:43:00 +0100951 .endif
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100952 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700953 .if \irqtrace
954 TRACE_IRQS_OFF
955 .endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 .endm
Ingo Molnar2601e642006-07-03 00:24:45 -0700957
958 /*
959 * "Paranoid" exit path from exception stack.
960 * Paranoid because this is used by NMIs and cannot take
961 * any kernel state for granted.
962 * We don't do kernel preemption checks here, because only
963 * NMI should be common and it does not enable IRQs and
964 * cannot get reschedule ticks.
965 *
966 * "trace" is 0 for the NMI handler only, because irq-tracing
967 * is fundamentally NMI-unsafe. (we cannot change the soft and
968 * hard flags at once, atomically)
969 */
970 .macro paranoidexit trace=1
971 /* ebx: no swapgs flag */
972paranoid_exit\trace:
973 testl %ebx,%ebx /* swapgs needed? */
974 jnz paranoid_restore\trace
975 testl $3,CS(%rsp)
976 jnz paranoid_userspace\trace
977paranoid_swapgs\trace:
Andi Kleen7a0a2df2006-09-26 10:52:37 +0200978 .if \trace
Ingo Molnar2601e642006-07-03 00:24:45 -0700979 TRACE_IRQS_IRETQ 0
Andi Kleen7a0a2df2006-09-26 10:52:37 +0200980 .endif
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100981 SWAPGS_UNSAFE_STACK
Ingo Molnar2601e642006-07-03 00:24:45 -0700982paranoid_restore\trace:
983 RESTORE_ALL 8
Ingo Molnar3701d8632008-02-09 23:24:08 +0100984 jmp irq_return
Ingo Molnar2601e642006-07-03 00:24:45 -0700985paranoid_userspace\trace:
986 GET_THREAD_INFO(%rcx)
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300987 movl TI_flags(%rcx),%ebx
Ingo Molnar2601e642006-07-03 00:24:45 -0700988 andl $_TIF_WORK_MASK,%ebx
989 jz paranoid_swapgs\trace
990 movq %rsp,%rdi /* &pt_regs */
991 call sync_regs
992 movq %rax,%rsp /* switch stack for scheduling */
993 testl $_TIF_NEED_RESCHED,%ebx
994 jnz paranoid_schedule\trace
995 movl %ebx,%edx /* arg3: thread flags */
996 .if \trace
997 TRACE_IRQS_ON
998 .endif
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100999 ENABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -07001000 xorl %esi,%esi /* arg2: oldset */
1001 movq %rsp,%rdi /* arg1: &pt_regs */
1002 call do_notify_resume
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001003 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -07001004 .if \trace
1005 TRACE_IRQS_OFF
1006 .endif
1007 jmp paranoid_userspace\trace
1008paranoid_schedule\trace:
1009 .if \trace
1010 TRACE_IRQS_ON
1011 .endif
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001012 ENABLE_INTERRUPTS(CLBR_ANY)
Ingo Molnar2601e642006-07-03 00:24:45 -07001013 call schedule
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001014 DISABLE_INTERRUPTS(CLBR_ANY)
Ingo Molnar2601e642006-07-03 00:24:45 -07001015 .if \trace
1016 TRACE_IRQS_OFF
1017 .endif
1018 jmp paranoid_userspace\trace
1019 CFI_ENDPROC
1020 .endm
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022/*
1023 * Exception entry point. This expects an error code/orig_rax on the stack
1024 * and the exception handler in %rax.
1025 */
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001026KPROBE_ENTRY(error_entry)
Jan Beulich7effaa82005-09-12 18:49:24 +02001027 _frame RDI
Jan Beulich37550902007-05-02 19:27:05 +02001028 CFI_REL_OFFSET rax,0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 /* rdi slot contains rax, oldrax contains error code */
1030 cld
1031 subq $14*8,%rsp
1032 CFI_ADJUST_CFA_OFFSET (14*8)
1033 movq %rsi,13*8(%rsp)
1034 CFI_REL_OFFSET rsi,RSI
1035 movq 14*8(%rsp),%rsi /* load rax from rdi slot */
Jan Beulich37550902007-05-02 19:27:05 +02001036 CFI_REGISTER rax,rsi
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 movq %rdx,12*8(%rsp)
1038 CFI_REL_OFFSET rdx,RDX
1039 movq %rcx,11*8(%rsp)
1040 CFI_REL_OFFSET rcx,RCX
1041 movq %rsi,10*8(%rsp) /* store rax */
1042 CFI_REL_OFFSET rax,RAX
1043 movq %r8, 9*8(%rsp)
1044 CFI_REL_OFFSET r8,R8
1045 movq %r9, 8*8(%rsp)
1046 CFI_REL_OFFSET r9,R9
1047 movq %r10,7*8(%rsp)
1048 CFI_REL_OFFSET r10,R10
1049 movq %r11,6*8(%rsp)
1050 CFI_REL_OFFSET r11,R11
1051 movq %rbx,5*8(%rsp)
1052 CFI_REL_OFFSET rbx,RBX
1053 movq %rbp,4*8(%rsp)
1054 CFI_REL_OFFSET rbp,RBP
1055 movq %r12,3*8(%rsp)
1056 CFI_REL_OFFSET r12,R12
1057 movq %r13,2*8(%rsp)
1058 CFI_REL_OFFSET r13,R13
1059 movq %r14,1*8(%rsp)
1060 CFI_REL_OFFSET r14,R14
1061 movq %r15,(%rsp)
1062 CFI_REL_OFFSET r15,R15
1063 xorl %ebx,%ebx
1064 testl $3,CS(%rsp)
1065 je error_kernelspace
1066error_swapgs:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001067 SWAPGS
Alexander van Heukelum6b11d4e2008-09-26 14:03:02 +02001068error_sti:
1069 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 movq %rdi,RDI(%rsp)
Jan Beulich37550902007-05-02 19:27:05 +02001071 CFI_REL_OFFSET rdi,RDI
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 movq %rsp,%rdi
1073 movq ORIG_RAX(%rsp),%rsi /* get error code */
1074 movq $-1,ORIG_RAX(%rsp)
1075 call *%rax
Peter Zijlstra10cd7062007-10-11 22:11:12 +02001076 /* ebx: no swapgs flag (1: don't need swapgs, 0: need it) */
1077error_exit:
1078 movl %ebx,%eax
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 RESTORE_REST
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001080 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -07001081 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 GET_THREAD_INFO(%rcx)
1083 testl %eax,%eax
1084 jne retint_kernel
Peter Zijlstra10cd7062007-10-11 22:11:12 +02001085 LOCKDEP_SYS_EXIT_IRQ
Glauber Costa26ccb8a2008-06-24 11:19:35 -03001086 movl TI_flags(%rcx),%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 movl $_TIF_WORK_MASK,%edi
1088 andl %edi,%edx
1089 jnz retint_careful
Peter Zijlstra10cd7062007-10-11 22:11:12 +02001090 jmp retint_swapgs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 CFI_ENDPROC
1092
1093error_kernelspace:
1094 incl %ebx
1095 /* There are two places in the kernel that can potentially fault with
1096 usergs. Handle them here. The exception handlers after
1097 iret run with kernel gs again, so don't set the user space flag.
1098 B stepping K8s sometimes report an truncated RIP for IRET
1099 exceptions returning to compat mode. Check for these here too. */
Vegard Nossum9d8ad5d2008-06-27 17:22:17 +02001100 leaq irq_return(%rip),%rcx
1101 cmpq %rcx,RIP(%rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 je error_swapgs
Vegard Nossum9d8ad5d2008-06-27 17:22:17 +02001103 movl %ecx,%ecx /* zero extend */
1104 cmpq %rcx,RIP(%rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 je error_swapgs
1106 cmpq $gs_change,RIP(%rsp)
1107 je error_swapgs
1108 jmp error_sti
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001109KPROBE_END(error_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 /* Reload gs selector with exception handling */
1112 /* edi: new selector */
Jeremy Fitzhardinge9f9d4892008-06-25 00:19:32 -04001113ENTRY(native_load_gs_index)
Jan Beulich7effaa82005-09-12 18:49:24 +02001114 CFI_STARTPROC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 pushf
Jan Beulich7effaa82005-09-12 18:49:24 +02001116 CFI_ADJUST_CFA_OFFSET 8
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001117 DISABLE_INTERRUPTS(CLBR_ANY | ~(CLBR_RDI))
1118 SWAPGS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119gs_change:
1120 movl %edi,%gs
11212: mfence /* workaround */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001122 SWAPGS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 popf
Jan Beulich7effaa82005-09-12 18:49:24 +02001124 CFI_ADJUST_CFA_OFFSET -8
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 ret
Jan Beulich7effaa82005-09-12 18:49:24 +02001126 CFI_ENDPROC
Jeremy Fitzhardinge9f9d4892008-06-25 00:19:32 -04001127ENDPROC(native_load_gs_index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129 .section __ex_table,"a"
1130 .align 8
1131 .quad gs_change,bad_gs
1132 .previous
1133 .section .fixup,"ax"
1134 /* running with kernelgs */
1135bad_gs:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001136 SWAPGS /* switch back to user gs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 xorl %eax,%eax
1138 movl %eax,%gs
1139 jmp 2b
1140 .previous
1141
1142/*
1143 * Create a kernel thread.
1144 *
1145 * C extern interface:
1146 * extern long kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
1147 *
1148 * asm input arguments:
1149 * rdi: fn, rsi: arg, rdx: flags
1150 */
1151ENTRY(kernel_thread)
1152 CFI_STARTPROC
1153 FAKE_STACK_FRAME $child_rip
1154 SAVE_ALL
1155
1156 # rdi: flags, rsi: usp, rdx: will be &pt_regs
1157 movq %rdx,%rdi
1158 orq kernel_thread_flags(%rip),%rdi
1159 movq $-1, %rsi
1160 movq %rsp, %rdx
1161
1162 xorl %r8d,%r8d
1163 xorl %r9d,%r9d
1164
1165 # clone now
1166 call do_fork
1167 movq %rax,RAX(%rsp)
1168 xorl %edi,%edi
1169
1170 /*
1171 * It isn't worth to check for reschedule here,
1172 * so internally to the x86_64 port you can rely on kernel_thread()
1173 * not to reschedule the child before returning, this avoids the need
1174 * of hacks for example to fork off the per-CPU idle tasks.
1175 * [Hopefully no generic code relies on the reschedule -AK]
1176 */
1177 RESTORE_ALL
1178 UNFAKE_STACK_FRAME
1179 ret
1180 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001181ENDPROC(kernel_thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183child_rip:
Andi Kleenc05991e2006-08-30 19:37:08 +02001184 pushq $0 # fake return address
1185 CFI_STARTPROC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 /*
1187 * Here we are in the child and the registers are set as they were
1188 * at kernel_thread() invocation in the parent.
1189 */
1190 movq %rdi, %rax
1191 movq %rsi, %rdi
1192 call *%rax
1193 # exit
Andrey Mirkin1c5b5cf2007-10-17 18:04:33 +02001194 mov %eax, %edi
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 call do_exit
Andi Kleenc05991e2006-08-30 19:37:08 +02001196 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001197ENDPROC(child_rip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199/*
1200 * execve(). This function needs to use IRET, not SYSRET, to set up all state properly.
1201 *
1202 * C extern interface:
1203 * extern long execve(char *name, char **argv, char **envp)
1204 *
1205 * asm input arguments:
1206 * rdi: name, rsi: argv, rdx: envp
1207 *
1208 * We want to fallback into:
Ingo Molnar5d119b22008-02-26 12:55:57 +01001209 * extern long sys_execve(char *name, char **argv,char **envp, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 *
1211 * do_sys_execve asm fallback arguments:
Ingo Molnar5d119b22008-02-26 12:55:57 +01001212 * rdi: name, rsi: argv, rdx: envp, rcx: fake frame on the stack
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 */
Arnd Bergmann3db03b42006-10-02 02:18:31 -07001214ENTRY(kernel_execve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 CFI_STARTPROC
1216 FAKE_STACK_FRAME $0
1217 SAVE_ALL
Ingo Molnar5d119b22008-02-26 12:55:57 +01001218 movq %rsp,%rcx
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 call sys_execve
1220 movq %rax, RAX(%rsp)
1221 RESTORE_REST
1222 testq %rax,%rax
1223 je int_ret_from_sys_call
1224 RESTORE_ARGS
1225 UNFAKE_STACK_FRAME
1226 ret
1227 CFI_ENDPROC
Arnd Bergmann3db03b42006-10-02 02:18:31 -07001228ENDPROC(kernel_execve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001230KPROBE_ENTRY(page_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 errorentry do_page_fault
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001232KPROBE_END(page_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234ENTRY(coprocessor_error)
1235 zeroentry do_coprocessor_error
Jan Beulich4b787e02006-06-26 13:56:55 +02001236END(coprocessor_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238ENTRY(simd_coprocessor_error)
1239 zeroentry do_simd_coprocessor_error
Jan Beulich4b787e02006-06-26 13:56:55 +02001240END(simd_coprocessor_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242ENTRY(device_not_available)
1243 zeroentry math_state_restore
Jan Beulich4b787e02006-06-26 13:56:55 +02001244END(device_not_available)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246 /* runs on exception stack */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001247KPROBE_ENTRY(debug)
Jan Beulich7effaa82005-09-12 18:49:24 +02001248 INTR_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001249 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 pushq $0
1251 CFI_ADJUST_CFA_OFFSET 8
Andi Kleen5f8efbb2006-01-16 01:56:39 +01001252 paranoidentry do_debug, DEBUG_STACK
Ingo Molnar2601e642006-07-03 00:24:45 -07001253 paranoidexit
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001254KPROBE_END(debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256 /* runs on exception stack */
Andi Kleeneddb6fb2006-02-03 21:50:41 +01001257KPROBE_ENTRY(nmi)
Jan Beulich7effaa82005-09-12 18:49:24 +02001258 INTR_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001259 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 pushq $-1
Jan Beulich7effaa82005-09-12 18:49:24 +02001261 CFI_ADJUST_CFA_OFFSET 8
Ingo Molnar2601e642006-07-03 00:24:45 -07001262 paranoidentry do_nmi, 0, 0
1263#ifdef CONFIG_TRACE_IRQFLAGS
1264 paranoidexit 0
1265#else
1266 jmp paranoid_exit1
1267 CFI_ENDPROC
1268#endif
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001269KPROBE_END(nmi)
Andi Kleen6fefb0d2005-04-16 15:25:03 -07001270
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001271KPROBE_ENTRY(int3)
Jan Beulichb556b352006-01-11 22:43:00 +01001272 INTR_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001273 PARAVIRT_ADJUST_EXCEPTION_FRAME
Jan Beulichb556b352006-01-11 22:43:00 +01001274 pushq $0
1275 CFI_ADJUST_CFA_OFFSET 8
Andi Kleen5f8efbb2006-01-16 01:56:39 +01001276 paranoidentry do_int3, DEBUG_STACK
Ingo Molnar2601e642006-07-03 00:24:45 -07001277 jmp paranoid_exit1
Jan Beulichb556b352006-01-11 22:43:00 +01001278 CFI_ENDPROC
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001279KPROBE_END(int3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281ENTRY(overflow)
1282 zeroentry do_overflow
Jan Beulich4b787e02006-06-26 13:56:55 +02001283END(overflow)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285ENTRY(bounds)
1286 zeroentry do_bounds
Jan Beulich4b787e02006-06-26 13:56:55 +02001287END(bounds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289ENTRY(invalid_op)
1290 zeroentry do_invalid_op
Jan Beulich4b787e02006-06-26 13:56:55 +02001291END(invalid_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293ENTRY(coprocessor_segment_overrun)
1294 zeroentry do_coprocessor_segment_overrun
Jan Beulich4b787e02006-06-26 13:56:55 +02001295END(coprocessor_segment_overrun)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 /* runs on exception stack */
1298ENTRY(double_fault)
Jan Beulich7effaa82005-09-12 18:49:24 +02001299 XCPT_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001300 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 paranoidentry do_double_fault
Ingo Molnar2601e642006-07-03 00:24:45 -07001302 jmp paranoid_exit1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001304END(double_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306ENTRY(invalid_TSS)
1307 errorentry do_invalid_TSS
Jan Beulich4b787e02006-06-26 13:56:55 +02001308END(invalid_TSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310ENTRY(segment_not_present)
1311 errorentry do_segment_not_present
Jan Beulich4b787e02006-06-26 13:56:55 +02001312END(segment_not_present)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314 /* runs on exception stack */
1315ENTRY(stack_segment)
Jan Beulich7effaa82005-09-12 18:49:24 +02001316 XCPT_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001317 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 paranoidentry do_stack_segment
Ingo Molnar2601e642006-07-03 00:24:45 -07001319 jmp paranoid_exit1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001321END(stack_segment)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001323KPROBE_ENTRY(general_protection)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 errorentry do_general_protection
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001325KPROBE_END(general_protection)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327ENTRY(alignment_check)
1328 errorentry do_alignment_check
Jan Beulich4b787e02006-06-26 13:56:55 +02001329END(alignment_check)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331ENTRY(divide_error)
1332 zeroentry do_divide_error
Jan Beulich4b787e02006-06-26 13:56:55 +02001333END(divide_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335ENTRY(spurious_interrupt_bug)
1336 zeroentry do_spurious_interrupt_bug
Jan Beulich4b787e02006-06-26 13:56:55 +02001337END(spurious_interrupt_bug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339#ifdef CONFIG_X86_MCE
1340 /* runs on exception stack */
1341ENTRY(machine_check)
Jan Beulich7effaa82005-09-12 18:49:24 +02001342 INTR_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001343 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 pushq $0
1345 CFI_ADJUST_CFA_OFFSET 8
1346 paranoidentry do_machine_check
Ingo Molnar2601e642006-07-03 00:24:45 -07001347 jmp paranoid_exit1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001349END(machine_check)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350#endif
1351
Andi Kleen26995002006-08-02 22:37:28 +02001352/* Call softirq on interrupt stack. Interrupts are off. */
Andi Kleened6b6762005-07-28 21:15:49 -07001353ENTRY(call_softirq)
Jan Beulich7effaa82005-09-12 18:49:24 +02001354 CFI_STARTPROC
Andi Kleen26995002006-08-02 22:37:28 +02001355 push %rbp
1356 CFI_ADJUST_CFA_OFFSET 8
1357 CFI_REL_OFFSET rbp,0
1358 mov %rsp,%rbp
1359 CFI_DEF_CFA_REGISTER rbp
Andi Kleened6b6762005-07-28 21:15:49 -07001360 incl %gs:pda_irqcount
Andi Kleen26995002006-08-02 22:37:28 +02001361 cmove %gs:pda_irqstackptr,%rsp
1362 push %rbp # backlink for old unwinder
Andi Kleened6b6762005-07-28 21:15:49 -07001363 call __do_softirq
Andi Kleen26995002006-08-02 22:37:28 +02001364 leaveq
Jan Beulich7effaa82005-09-12 18:49:24 +02001365 CFI_DEF_CFA_REGISTER rsp
Andi Kleen26995002006-08-02 22:37:28 +02001366 CFI_ADJUST_CFA_OFFSET -8
Andi Kleened6b6762005-07-28 21:15:49 -07001367 decl %gs:pda_irqcount
Andi Kleened6b6762005-07-28 21:15:49 -07001368 ret
Jan Beulich7effaa82005-09-12 18:49:24 +02001369 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001370ENDPROC(call_softirq)
Andi Kleen75154f42007-06-23 02:29:25 +02001371
1372KPROBE_ENTRY(ignore_sysret)
1373 CFI_STARTPROC
1374 mov $-ENOSYS,%eax
1375 sysret
1376 CFI_ENDPROC
1377ENDPROC(ignore_sysret)
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001378
1379#ifdef CONFIG_XEN
1380ENTRY(xen_hypervisor_callback)
1381 zeroentry xen_do_hypervisor_callback
1382END(xen_hypervisor_callback)
1383
1384/*
1385# A note on the "critical region" in our callback handler.
1386# We want to avoid stacking callback handlers due to events occurring
1387# during handling of the last event. To do this, we keep events disabled
1388# until we've done all processing. HOWEVER, we must enable events before
1389# popping the stack frame (can't be done atomically) and so it would still
1390# be possible to get enough handler activations to overflow the stack.
1391# Although unlikely, bugs of that kind are hard to track down, so we'd
1392# like to avoid the possibility.
1393# So, on entry to the handler we detect whether we interrupted an
1394# existing activation in its critical region -- if so, we pop the current
1395# activation and restart the handler using the previous one.
1396*/
1397ENTRY(xen_do_hypervisor_callback) # do_hypervisor_callback(struct *pt_regs)
1398 CFI_STARTPROC
1399/* Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
1400 see the correct pointer to the pt_regs */
1401 movq %rdi, %rsp # we don't return, adjust the stack frame
1402 CFI_ENDPROC
1403 CFI_DEFAULT_STACK
140411: incl %gs:pda_irqcount
1405 movq %rsp,%rbp
1406 CFI_DEF_CFA_REGISTER rbp
1407 cmovzq %gs:pda_irqstackptr,%rsp
1408 pushq %rbp # backlink for old unwinder
1409 call xen_evtchn_do_upcall
1410 popq %rsp
1411 CFI_DEF_CFA_REGISTER rsp
1412 decl %gs:pda_irqcount
1413 jmp error_exit
1414 CFI_ENDPROC
1415END(do_hypervisor_callback)
1416
1417/*
1418# Hypervisor uses this for application faults while it executes.
1419# We get here for two reasons:
1420# 1. Fault while reloading DS, ES, FS or GS
1421# 2. Fault while executing IRET
1422# Category 1 we do not need to fix up as Xen has already reloaded all segment
1423# registers that could be reloaded and zeroed the others.
1424# Category 2 we fix up by killing the current process. We cannot use the
1425# normal Linux return path in this case because if we use the IRET hypercall
1426# to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1427# We distinguish between categories by comparing each saved segment register
1428# with its current contents: any discrepancy means we in category 1.
1429*/
1430ENTRY(xen_failsafe_callback)
Jeremy Fitzhardinge4a5c3e72008-07-08 15:07:09 -07001431 framesz = (RIP-0x30) /* workaround buggy gas */
1432 _frame framesz
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001433 CFI_REL_OFFSET rcx, 0
1434 CFI_REL_OFFSET r11, 8
1435 movw %ds,%cx
1436 cmpw %cx,0x10(%rsp)
1437 CFI_REMEMBER_STATE
1438 jne 1f
1439 movw %es,%cx
1440 cmpw %cx,0x18(%rsp)
1441 jne 1f
1442 movw %fs,%cx
1443 cmpw %cx,0x20(%rsp)
1444 jne 1f
1445 movw %gs,%cx
1446 cmpw %cx,0x28(%rsp)
1447 jne 1f
1448 /* All segments match their saved values => Category 2 (Bad IRET). */
1449 movq (%rsp),%rcx
1450 CFI_RESTORE rcx
1451 movq 8(%rsp),%r11
1452 CFI_RESTORE r11
1453 addq $0x30,%rsp
1454 CFI_ADJUST_CFA_OFFSET -0x30
Jeremy Fitzhardinge4a5c3e72008-07-08 15:07:09 -07001455 pushq $0
1456 CFI_ADJUST_CFA_OFFSET 8
1457 pushq %r11
1458 CFI_ADJUST_CFA_OFFSET 8
1459 pushq %rcx
1460 CFI_ADJUST_CFA_OFFSET 8
1461 jmp general_protection
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001462 CFI_RESTORE_STATE
14631: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
1464 movq (%rsp),%rcx
1465 CFI_RESTORE rcx
1466 movq 8(%rsp),%r11
1467 CFI_RESTORE r11
1468 addq $0x30,%rsp
1469 CFI_ADJUST_CFA_OFFSET -0x30
1470 pushq $0
1471 CFI_ADJUST_CFA_OFFSET 8
1472 SAVE_ALL
1473 jmp error_exit
1474 CFI_ENDPROC
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001475END(xen_failsafe_callback)
1476
1477#endif /* CONFIG_XEN */