blob: cf3a0b2d00599adb2a79fefa991c0d8223059fba [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
670 CFI_ADJUST_CFA_OFFSET 8
671 CFI_REL_OFFSET rbp, 0
672 movq %rsp,%rbp
673 CFI_DEF_CFA_REGISTER rbp
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 testl $3,CS(%rdi)
675 je 1f
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100676 SWAPGS
Andi Kleen96e54042006-09-26 10:52:39 +0200677 /* irqcount is used to check if a CPU is already on an interrupt
678 stack or not. While this is essentially redundant with preempt_count
679 it is a little cheaper to use a separate counter in the PDA
680 (short of moving irq_enter into assembly, which would be too
681 much work) */
6821: incl %gs:pda_irqcount
Jan Beulich1de9c3f2006-06-26 13:57:35 +0200683 cmoveq %gs:pda_irqstackptr,%rsp
Andi Kleen26995002006-08-02 22:37:28 +0200684 push %rbp # backlink for old unwinder
Ingo Molnar2601e642006-07-03 00:24:45 -0700685 /*
686 * We entered an interrupt context - irqs are off:
687 */
688 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 call \func
690 .endm
691
692ENTRY(common_interrupt)
Jan Beulich7effaa82005-09-12 18:49:24 +0200693 XCPT_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 interrupt do_IRQ
695 /* 0(%rsp): oldrsp-ARGOFFSET */
Jan Beulich7effaa82005-09-12 18:49:24 +0200696ret_from_intr:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100697 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700698 TRACE_IRQS_OFF
Andi Kleen3829ee62005-07-28 21:15:48 -0700699 decl %gs:pda_irqcount
Jan Beulich1de9c3f2006-06-26 13:57:35 +0200700 leaveq
Jan Beulich7effaa82005-09-12 18:49:24 +0200701 CFI_DEF_CFA_REGISTER rsp
Jan Beulich1de9c3f2006-06-26 13:57:35 +0200702 CFI_ADJUST_CFA_OFFSET -8
Jan Beulich7effaa82005-09-12 18:49:24 +0200703exit_intr:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 GET_THREAD_INFO(%rcx)
705 testl $3,CS-ARGOFFSET(%rsp)
706 je retint_kernel
707
708 /* Interrupt came from user space */
709 /*
710 * Has a correct top of stack, but a partial stack frame
711 * %rcx: thread info. Interrupts off.
712 */
713retint_with_reschedule:
714 movl $_TIF_WORK_MASK,%edi
Jan Beulich7effaa82005-09-12 18:49:24 +0200715retint_check:
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200716 LOCKDEP_SYS_EXIT_IRQ
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300717 movl TI_flags(%rcx),%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 andl %edi,%edx
Jan Beulich7effaa82005-09-12 18:49:24 +0200719 CFI_REMEMBER_STATE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 jnz retint_careful
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200721
722retint_swapgs: /* return to user-space */
Ingo Molnar2601e642006-07-03 00:24:45 -0700723 /*
724 * The iretq could re-enable interrupts:
725 */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100726 DISABLE_INTERRUPTS(CLBR_ANY)
Ingo Molnar2601e642006-07-03 00:24:45 -0700727 TRACE_IRQS_IRETQ
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100728 SWAPGS
Ingo Molnar2601e642006-07-03 00:24:45 -0700729 jmp restore_args
730
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200731retint_restore_args: /* return to kernel space */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100732 DISABLE_INTERRUPTS(CLBR_ANY)
Ingo Molnar2601e642006-07-03 00:24:45 -0700733 /*
734 * The iretq could re-enable interrupts:
735 */
736 TRACE_IRQS_IRETQ
737restore_args:
Ingo Molnar3701d8632008-02-09 23:24:08 +0100738 RESTORE_ARGS 0,8,0
739
Adrian Bunkf7f3d792008-02-13 23:29:53 +0200740irq_return:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100741 INTERRUPT_RETURN
Ingo Molnar3701d8632008-02-09 23:24:08 +0100742
743 .section __ex_table, "a"
744 .quad irq_return, bad_iret
745 .previous
746
747#ifdef CONFIG_PARAVIRT
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100748ENTRY(native_iret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 iretq
750
751 .section __ex_table,"a"
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100752 .quad native_iret, bad_iret
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 .previous
Ingo Molnar3701d8632008-02-09 23:24:08 +0100754#endif
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 .section .fixup,"ax"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757bad_iret:
Roland McGrath3aa4b372008-02-06 22:39:43 +0100758 /*
759 * The iret traps when the %cs or %ss being restored is bogus.
760 * We've lost the original trap vector and error code.
761 * #GPF is the most likely one to get for an invalid selector.
762 * So pretend we completed the iret and took the #GPF in user mode.
763 *
764 * We are now running with the kernel GS after exception recovery.
765 * But error_entry expects us to have user GS to match the user %cs,
766 * so swap back.
767 */
768 pushq $0
769
770 SWAPGS
771 jmp general_protection
772
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100773 .previous
774
Jan Beulich7effaa82005-09-12 18:49:24 +0200775 /* edi: workmask, edx: work */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776retint_careful:
Jan Beulich7effaa82005-09-12 18:49:24 +0200777 CFI_RESTORE_STATE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 bt $TIF_NEED_RESCHED,%edx
779 jnc retint_signal
Ingo Molnar2601e642006-07-03 00:24:45 -0700780 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100781 ENABLE_INTERRUPTS(CLBR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 pushq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200783 CFI_ADJUST_CFA_OFFSET 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 call schedule
785 popq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200786 CFI_ADJUST_CFA_OFFSET -8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 GET_THREAD_INFO(%rcx)
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100788 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700789 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 jmp retint_check
791
792retint_signal:
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100793 testl $_TIF_DO_NOTIFY_MASK,%edx
Andi Kleen10ffdbb2005-05-16 21:53:19 -0700794 jz retint_swapgs
Ingo Molnar2601e642006-07-03 00:24:45 -0700795 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100796 ENABLE_INTERRUPTS(CLBR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 SAVE_REST
798 movq $-1,ORIG_RAX(%rsp)
Andi Kleen3829ee62005-07-28 21:15:48 -0700799 xorl %esi,%esi # oldset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 movq %rsp,%rdi # &pt_regs
801 call do_notify_resume
802 RESTORE_REST
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100803 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700804 TRACE_IRQS_OFF
Andi Kleenbe9e6872005-05-01 08:58:51 -0700805 GET_THREAD_INFO(%rcx)
Roland McGratheca91e72008-07-10 14:50:39 -0700806 jmp retint_with_reschedule
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808#ifdef CONFIG_PREEMPT
809 /* Returning to kernel space. Check if we need preemption */
810 /* rcx: threadinfo. interrupts off. */
Andi Kleenb06baba2006-09-26 10:52:29 +0200811ENTRY(retint_kernel)
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300812 cmpl $0,TI_preempt_count(%rcx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 jnz retint_restore_args
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300814 bt $TIF_NEED_RESCHED,TI_flags(%rcx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 jnc retint_restore_args
816 bt $9,EFLAGS-ARGOFFSET(%rsp) /* interrupts off? */
817 jnc retint_restore_args
818 call preempt_schedule_irq
819 jmp exit_intr
820#endif
Jan Beulich4b787e02006-06-26 13:56:55 +0200821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200823END(common_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825/*
826 * APIC interrupts.
827 */
828 .macro apicinterrupt num,func
Jan Beulich7effaa82005-09-12 18:49:24 +0200829 INTR_FRAME
Rusty Russell19eadf92006-06-27 02:53:44 -0700830 pushq $~(\num)
Jan Beulich7effaa82005-09-12 18:49:24 +0200831 CFI_ADJUST_CFA_OFFSET 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 interrupt \func
833 jmp ret_from_intr
834 CFI_ENDPROC
835 .endm
836
837ENTRY(thermal_interrupt)
838 apicinterrupt THERMAL_APIC_VECTOR,smp_thermal_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200839END(thermal_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Jacob Shin89b831e2005-11-05 17:25:53 +0100841ENTRY(threshold_interrupt)
842 apicinterrupt THRESHOLD_APIC_VECTOR,mce_threshold_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200843END(threshold_interrupt)
Jacob Shin89b831e2005-11-05 17:25:53 +0100844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845#ifdef CONFIG_SMP
846ENTRY(reschedule_interrupt)
847 apicinterrupt RESCHEDULE_VECTOR,smp_reschedule_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200848END(reschedule_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Andi Kleene5bc8b62005-09-12 18:49:24 +0200850 .macro INVALIDATE_ENTRY num
851ENTRY(invalidate_interrupt\num)
852 apicinterrupt INVALIDATE_TLB_VECTOR_START+\num,smp_invalidate_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200853END(invalidate_interrupt\num)
Andi Kleene5bc8b62005-09-12 18:49:24 +0200854 .endm
855
856 INVALIDATE_ENTRY 0
857 INVALIDATE_ENTRY 1
858 INVALIDATE_ENTRY 2
859 INVALIDATE_ENTRY 3
860 INVALIDATE_ENTRY 4
861 INVALIDATE_ENTRY 5
862 INVALIDATE_ENTRY 6
863 INVALIDATE_ENTRY 7
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865ENTRY(call_function_interrupt)
866 apicinterrupt CALL_FUNCTION_VECTOR,smp_call_function_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200867END(call_function_interrupt)
Jens Axboe3b16cf82008-06-26 11:21:54 +0200868ENTRY(call_function_single_interrupt)
869 apicinterrupt CALL_FUNCTION_SINGLE_VECTOR,smp_call_function_single_interrupt
870END(call_function_single_interrupt)
Eric W. Biederman61014292007-02-23 04:40:58 -0700871ENTRY(irq_move_cleanup_interrupt)
872 apicinterrupt IRQ_MOVE_CLEANUP_VECTOR,smp_irq_move_cleanup_interrupt
873END(irq_move_cleanup_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874#endif
875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876ENTRY(apic_timer_interrupt)
877 apicinterrupt LOCAL_TIMER_VECTOR,smp_apic_timer_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200878END(apic_timer_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Cliff Wickman18129242008-06-02 08:56:14 -0500880ENTRY(uv_bau_message_intr1)
881 apicinterrupt 220,uv_bau_message_interrupt
882END(uv_bau_message_intr1)
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884ENTRY(error_interrupt)
885 apicinterrupt ERROR_APIC_VECTOR,smp_error_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200886END(error_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888ENTRY(spurious_interrupt)
889 apicinterrupt SPURIOUS_APIC_VECTOR,smp_spurious_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200890END(spurious_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892/*
893 * Exception entry points.
894 */
895 .macro zeroentry sym
Jan Beulich7effaa82005-09-12 18:49:24 +0200896 INTR_FRAME
Jeremy Fitzhardingefab58422008-06-25 00:19:31 -0400897 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 pushq $0 /* push error code/oldrax */
Jan Beulich7effaa82005-09-12 18:49:24 +0200899 CFI_ADJUST_CFA_OFFSET 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 pushq %rax /* push real oldrax to the rdi slot */
Jan Beulich7effaa82005-09-12 18:49:24 +0200901 CFI_ADJUST_CFA_OFFSET 8
Jan Beulich37550902007-05-02 19:27:05 +0200902 CFI_REL_OFFSET rax,0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 leaq \sym(%rip),%rax
904 jmp error_entry
Jan Beulich7effaa82005-09-12 18:49:24 +0200905 CFI_ENDPROC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 .endm
907
908 .macro errorentry sym
Jan Beulich7effaa82005-09-12 18:49:24 +0200909 XCPT_FRAME
Jeremy Fitzhardingefab58422008-06-25 00:19:31 -0400910 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 pushq %rax
Jan Beulich7effaa82005-09-12 18:49:24 +0200912 CFI_ADJUST_CFA_OFFSET 8
Jan Beulich37550902007-05-02 19:27:05 +0200913 CFI_REL_OFFSET rax,0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 leaq \sym(%rip),%rax
915 jmp error_entry
Jan Beulich7effaa82005-09-12 18:49:24 +0200916 CFI_ENDPROC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 .endm
918
919 /* error code is on the stack already */
920 /* handle NMI like exceptions that can happen everywhere */
Ingo Molnar2601e642006-07-03 00:24:45 -0700921 .macro paranoidentry sym, ist=0, irqtrace=1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 SAVE_ALL
923 cld
924 movl $1,%ebx
925 movl $MSR_GS_BASE,%ecx
926 rdmsr
927 testl %edx,%edx
928 js 1f
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100929 SWAPGS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 xorl %ebx,%ebx
Jan Beulichb556b352006-01-11 22:43:00 +01009311:
932 .if \ist
933 movq %gs:pda_data_offset, %rbp
934 .endif
935 movq %rsp,%rdi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 movq ORIG_RAX(%rsp),%rsi
937 movq $-1,ORIG_RAX(%rsp)
Jan Beulichb556b352006-01-11 22:43:00 +0100938 .if \ist
Andi Kleen5f8efbb2006-01-16 01:56:39 +0100939 subq $EXCEPTION_STKSZ, per_cpu__init_tss + TSS_ist + (\ist - 1) * 8(%rbp)
Jan Beulichb556b352006-01-11 22:43:00 +0100940 .endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 call \sym
Jan Beulichb556b352006-01-11 22:43:00 +0100942 .if \ist
Andi Kleen5f8efbb2006-01-16 01:56:39 +0100943 addq $EXCEPTION_STKSZ, per_cpu__init_tss + TSS_ist + (\ist - 1) * 8(%rbp)
Jan Beulichb556b352006-01-11 22:43:00 +0100944 .endif
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100945 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700946 .if \irqtrace
947 TRACE_IRQS_OFF
948 .endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 .endm
Ingo Molnar2601e642006-07-03 00:24:45 -0700950
951 /*
952 * "Paranoid" exit path from exception stack.
953 * Paranoid because this is used by NMIs and cannot take
954 * any kernel state for granted.
955 * We don't do kernel preemption checks here, because only
956 * NMI should be common and it does not enable IRQs and
957 * cannot get reschedule ticks.
958 *
959 * "trace" is 0 for the NMI handler only, because irq-tracing
960 * is fundamentally NMI-unsafe. (we cannot change the soft and
961 * hard flags at once, atomically)
962 */
963 .macro paranoidexit trace=1
964 /* ebx: no swapgs flag */
965paranoid_exit\trace:
966 testl %ebx,%ebx /* swapgs needed? */
967 jnz paranoid_restore\trace
968 testl $3,CS(%rsp)
969 jnz paranoid_userspace\trace
970paranoid_swapgs\trace:
Andi Kleen7a0a2df2006-09-26 10:52:37 +0200971 .if \trace
Ingo Molnar2601e642006-07-03 00:24:45 -0700972 TRACE_IRQS_IRETQ 0
Andi Kleen7a0a2df2006-09-26 10:52:37 +0200973 .endif
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100974 SWAPGS_UNSAFE_STACK
Ingo Molnar2601e642006-07-03 00:24:45 -0700975paranoid_restore\trace:
976 RESTORE_ALL 8
Ingo Molnar3701d8632008-02-09 23:24:08 +0100977 jmp irq_return
Ingo Molnar2601e642006-07-03 00:24:45 -0700978paranoid_userspace\trace:
979 GET_THREAD_INFO(%rcx)
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300980 movl TI_flags(%rcx),%ebx
Ingo Molnar2601e642006-07-03 00:24:45 -0700981 andl $_TIF_WORK_MASK,%ebx
982 jz paranoid_swapgs\trace
983 movq %rsp,%rdi /* &pt_regs */
984 call sync_regs
985 movq %rax,%rsp /* switch stack for scheduling */
986 testl $_TIF_NEED_RESCHED,%ebx
987 jnz paranoid_schedule\trace
988 movl %ebx,%edx /* arg3: thread flags */
989 .if \trace
990 TRACE_IRQS_ON
991 .endif
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100992 ENABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700993 xorl %esi,%esi /* arg2: oldset */
994 movq %rsp,%rdi /* arg1: &pt_regs */
995 call do_notify_resume
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100996 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700997 .if \trace
998 TRACE_IRQS_OFF
999 .endif
1000 jmp paranoid_userspace\trace
1001paranoid_schedule\trace:
1002 .if \trace
1003 TRACE_IRQS_ON
1004 .endif
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001005 ENABLE_INTERRUPTS(CLBR_ANY)
Ingo Molnar2601e642006-07-03 00:24:45 -07001006 call schedule
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001007 DISABLE_INTERRUPTS(CLBR_ANY)
Ingo Molnar2601e642006-07-03 00:24:45 -07001008 .if \trace
1009 TRACE_IRQS_OFF
1010 .endif
1011 jmp paranoid_userspace\trace
1012 CFI_ENDPROC
1013 .endm
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015/*
1016 * Exception entry point. This expects an error code/orig_rax on the stack
1017 * and the exception handler in %rax.
1018 */
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001019KPROBE_ENTRY(error_entry)
Jan Beulich7effaa82005-09-12 18:49:24 +02001020 _frame RDI
Jan Beulich37550902007-05-02 19:27:05 +02001021 CFI_REL_OFFSET rax,0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 /* rdi slot contains rax, oldrax contains error code */
1023 cld
1024 subq $14*8,%rsp
1025 CFI_ADJUST_CFA_OFFSET (14*8)
1026 movq %rsi,13*8(%rsp)
1027 CFI_REL_OFFSET rsi,RSI
1028 movq 14*8(%rsp),%rsi /* load rax from rdi slot */
Jan Beulich37550902007-05-02 19:27:05 +02001029 CFI_REGISTER rax,rsi
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 movq %rdx,12*8(%rsp)
1031 CFI_REL_OFFSET rdx,RDX
1032 movq %rcx,11*8(%rsp)
1033 CFI_REL_OFFSET rcx,RCX
1034 movq %rsi,10*8(%rsp) /* store rax */
1035 CFI_REL_OFFSET rax,RAX
1036 movq %r8, 9*8(%rsp)
1037 CFI_REL_OFFSET r8,R8
1038 movq %r9, 8*8(%rsp)
1039 CFI_REL_OFFSET r9,R9
1040 movq %r10,7*8(%rsp)
1041 CFI_REL_OFFSET r10,R10
1042 movq %r11,6*8(%rsp)
1043 CFI_REL_OFFSET r11,R11
1044 movq %rbx,5*8(%rsp)
1045 CFI_REL_OFFSET rbx,RBX
1046 movq %rbp,4*8(%rsp)
1047 CFI_REL_OFFSET rbp,RBP
1048 movq %r12,3*8(%rsp)
1049 CFI_REL_OFFSET r12,R12
1050 movq %r13,2*8(%rsp)
1051 CFI_REL_OFFSET r13,R13
1052 movq %r14,1*8(%rsp)
1053 CFI_REL_OFFSET r14,R14
1054 movq %r15,(%rsp)
1055 CFI_REL_OFFSET r15,R15
1056 xorl %ebx,%ebx
1057 testl $3,CS(%rsp)
1058 je error_kernelspace
1059error_swapgs:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001060 SWAPGS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061error_sti:
1062 movq %rdi,RDI(%rsp)
Jan Beulich37550902007-05-02 19:27:05 +02001063 CFI_REL_OFFSET rdi,RDI
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 movq %rsp,%rdi
1065 movq ORIG_RAX(%rsp),%rsi /* get error code */
1066 movq $-1,ORIG_RAX(%rsp)
1067 call *%rax
Peter Zijlstra10cd7062007-10-11 22:11:12 +02001068 /* ebx: no swapgs flag (1: don't need swapgs, 0: need it) */
1069error_exit:
1070 movl %ebx,%eax
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 RESTORE_REST
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001072 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -07001073 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 GET_THREAD_INFO(%rcx)
1075 testl %eax,%eax
1076 jne retint_kernel
Peter Zijlstra10cd7062007-10-11 22:11:12 +02001077 LOCKDEP_SYS_EXIT_IRQ
Glauber Costa26ccb8a2008-06-24 11:19:35 -03001078 movl TI_flags(%rcx),%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 movl $_TIF_WORK_MASK,%edi
1080 andl %edi,%edx
1081 jnz retint_careful
Peter Zijlstra10cd7062007-10-11 22:11:12 +02001082 jmp retint_swapgs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 CFI_ENDPROC
1084
1085error_kernelspace:
1086 incl %ebx
1087 /* There are two places in the kernel that can potentially fault with
1088 usergs. Handle them here. The exception handlers after
1089 iret run with kernel gs again, so don't set the user space flag.
1090 B stepping K8s sometimes report an truncated RIP for IRET
1091 exceptions returning to compat mode. Check for these here too. */
Vegard Nossum9d8ad5d2008-06-27 17:22:17 +02001092 leaq irq_return(%rip),%rcx
1093 cmpq %rcx,RIP(%rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 je error_swapgs
Vegard Nossum9d8ad5d2008-06-27 17:22:17 +02001095 movl %ecx,%ecx /* zero extend */
1096 cmpq %rcx,RIP(%rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 je error_swapgs
1098 cmpq $gs_change,RIP(%rsp)
1099 je error_swapgs
1100 jmp error_sti
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001101KPROBE_END(error_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 /* Reload gs selector with exception handling */
1104 /* edi: new selector */
Jeremy Fitzhardinge9f9d4892008-06-25 00:19:32 -04001105ENTRY(native_load_gs_index)
Jan Beulich7effaa82005-09-12 18:49:24 +02001106 CFI_STARTPROC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 pushf
Jan Beulich7effaa82005-09-12 18:49:24 +02001108 CFI_ADJUST_CFA_OFFSET 8
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001109 DISABLE_INTERRUPTS(CLBR_ANY | ~(CLBR_RDI))
1110 SWAPGS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111gs_change:
1112 movl %edi,%gs
11132: mfence /* workaround */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001114 SWAPGS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 popf
Jan Beulich7effaa82005-09-12 18:49:24 +02001116 CFI_ADJUST_CFA_OFFSET -8
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 ret
Jan Beulich7effaa82005-09-12 18:49:24 +02001118 CFI_ENDPROC
Jeremy Fitzhardinge9f9d4892008-06-25 00:19:32 -04001119ENDPROC(native_load_gs_index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 .section __ex_table,"a"
1122 .align 8
1123 .quad gs_change,bad_gs
1124 .previous
1125 .section .fixup,"ax"
1126 /* running with kernelgs */
1127bad_gs:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001128 SWAPGS /* switch back to user gs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 xorl %eax,%eax
1130 movl %eax,%gs
1131 jmp 2b
1132 .previous
1133
1134/*
1135 * Create a kernel thread.
1136 *
1137 * C extern interface:
1138 * extern long kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
1139 *
1140 * asm input arguments:
1141 * rdi: fn, rsi: arg, rdx: flags
1142 */
1143ENTRY(kernel_thread)
1144 CFI_STARTPROC
1145 FAKE_STACK_FRAME $child_rip
1146 SAVE_ALL
1147
1148 # rdi: flags, rsi: usp, rdx: will be &pt_regs
1149 movq %rdx,%rdi
1150 orq kernel_thread_flags(%rip),%rdi
1151 movq $-1, %rsi
1152 movq %rsp, %rdx
1153
1154 xorl %r8d,%r8d
1155 xorl %r9d,%r9d
1156
1157 # clone now
1158 call do_fork
1159 movq %rax,RAX(%rsp)
1160 xorl %edi,%edi
1161
1162 /*
1163 * It isn't worth to check for reschedule here,
1164 * so internally to the x86_64 port you can rely on kernel_thread()
1165 * not to reschedule the child before returning, this avoids the need
1166 * of hacks for example to fork off the per-CPU idle tasks.
1167 * [Hopefully no generic code relies on the reschedule -AK]
1168 */
1169 RESTORE_ALL
1170 UNFAKE_STACK_FRAME
1171 ret
1172 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001173ENDPROC(kernel_thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175child_rip:
Andi Kleenc05991e2006-08-30 19:37:08 +02001176 pushq $0 # fake return address
1177 CFI_STARTPROC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 /*
1179 * Here we are in the child and the registers are set as they were
1180 * at kernel_thread() invocation in the parent.
1181 */
1182 movq %rdi, %rax
1183 movq %rsi, %rdi
1184 call *%rax
1185 # exit
Andrey Mirkin1c5b5cf2007-10-17 18:04:33 +02001186 mov %eax, %edi
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 call do_exit
Andi Kleenc05991e2006-08-30 19:37:08 +02001188 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001189ENDPROC(child_rip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191/*
1192 * execve(). This function needs to use IRET, not SYSRET, to set up all state properly.
1193 *
1194 * C extern interface:
1195 * extern long execve(char *name, char **argv, char **envp)
1196 *
1197 * asm input arguments:
1198 * rdi: name, rsi: argv, rdx: envp
1199 *
1200 * We want to fallback into:
Ingo Molnar5d119b22008-02-26 12:55:57 +01001201 * extern long sys_execve(char *name, char **argv,char **envp, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 *
1203 * do_sys_execve asm fallback arguments:
Ingo Molnar5d119b22008-02-26 12:55:57 +01001204 * rdi: name, rsi: argv, rdx: envp, rcx: fake frame on the stack
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 */
Arnd Bergmann3db03b42006-10-02 02:18:31 -07001206ENTRY(kernel_execve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 CFI_STARTPROC
1208 FAKE_STACK_FRAME $0
1209 SAVE_ALL
Ingo Molnar5d119b22008-02-26 12:55:57 +01001210 movq %rsp,%rcx
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 call sys_execve
1212 movq %rax, RAX(%rsp)
1213 RESTORE_REST
1214 testq %rax,%rax
1215 je int_ret_from_sys_call
1216 RESTORE_ARGS
1217 UNFAKE_STACK_FRAME
1218 ret
1219 CFI_ENDPROC
Arnd Bergmann3db03b42006-10-02 02:18:31 -07001220ENDPROC(kernel_execve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001222KPROBE_ENTRY(page_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 errorentry do_page_fault
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001224KPROBE_END(page_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226ENTRY(coprocessor_error)
1227 zeroentry do_coprocessor_error
Jan Beulich4b787e02006-06-26 13:56:55 +02001228END(coprocessor_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230ENTRY(simd_coprocessor_error)
1231 zeroentry do_simd_coprocessor_error
Jan Beulich4b787e02006-06-26 13:56:55 +02001232END(simd_coprocessor_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234ENTRY(device_not_available)
1235 zeroentry math_state_restore
Jan Beulich4b787e02006-06-26 13:56:55 +02001236END(device_not_available)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 /* runs on exception stack */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001239KPROBE_ENTRY(debug)
Jan Beulich7effaa82005-09-12 18:49:24 +02001240 INTR_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001241 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 pushq $0
1243 CFI_ADJUST_CFA_OFFSET 8
Andi Kleen5f8efbb2006-01-16 01:56:39 +01001244 paranoidentry do_debug, DEBUG_STACK
Ingo Molnar2601e642006-07-03 00:24:45 -07001245 paranoidexit
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001246KPROBE_END(debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 /* runs on exception stack */
Andi Kleeneddb6fb2006-02-03 21:50:41 +01001249KPROBE_ENTRY(nmi)
Jan Beulich7effaa82005-09-12 18:49:24 +02001250 INTR_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001251 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 pushq $-1
Jan Beulich7effaa82005-09-12 18:49:24 +02001253 CFI_ADJUST_CFA_OFFSET 8
Ingo Molnar2601e642006-07-03 00:24:45 -07001254 paranoidentry do_nmi, 0, 0
1255#ifdef CONFIG_TRACE_IRQFLAGS
1256 paranoidexit 0
1257#else
1258 jmp paranoid_exit1
1259 CFI_ENDPROC
1260#endif
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001261KPROBE_END(nmi)
Andi Kleen6fefb0d2005-04-16 15:25:03 -07001262
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001263KPROBE_ENTRY(int3)
Jan Beulichb556b352006-01-11 22:43:00 +01001264 INTR_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001265 PARAVIRT_ADJUST_EXCEPTION_FRAME
Jan Beulichb556b352006-01-11 22:43:00 +01001266 pushq $0
1267 CFI_ADJUST_CFA_OFFSET 8
Andi Kleen5f8efbb2006-01-16 01:56:39 +01001268 paranoidentry do_int3, DEBUG_STACK
Ingo Molnar2601e642006-07-03 00:24:45 -07001269 jmp paranoid_exit1
Jan Beulichb556b352006-01-11 22:43:00 +01001270 CFI_ENDPROC
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001271KPROBE_END(int3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273ENTRY(overflow)
1274 zeroentry do_overflow
Jan Beulich4b787e02006-06-26 13:56:55 +02001275END(overflow)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277ENTRY(bounds)
1278 zeroentry do_bounds
Jan Beulich4b787e02006-06-26 13:56:55 +02001279END(bounds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281ENTRY(invalid_op)
1282 zeroentry do_invalid_op
Jan Beulich4b787e02006-06-26 13:56:55 +02001283END(invalid_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285ENTRY(coprocessor_segment_overrun)
1286 zeroentry do_coprocessor_segment_overrun
Jan Beulich4b787e02006-06-26 13:56:55 +02001287END(coprocessor_segment_overrun)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 /* runs on exception stack */
1290ENTRY(double_fault)
Jan Beulich7effaa82005-09-12 18:49:24 +02001291 XCPT_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001292 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 paranoidentry do_double_fault
Ingo Molnar2601e642006-07-03 00:24:45 -07001294 jmp paranoid_exit1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001296END(double_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298ENTRY(invalid_TSS)
1299 errorentry do_invalid_TSS
Jan Beulich4b787e02006-06-26 13:56:55 +02001300END(invalid_TSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302ENTRY(segment_not_present)
1303 errorentry do_segment_not_present
Jan Beulich4b787e02006-06-26 13:56:55 +02001304END(segment_not_present)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 /* runs on exception stack */
1307ENTRY(stack_segment)
Jan Beulich7effaa82005-09-12 18:49:24 +02001308 XCPT_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001309 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 paranoidentry do_stack_segment
Ingo Molnar2601e642006-07-03 00:24:45 -07001311 jmp paranoid_exit1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001313END(stack_segment)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001315KPROBE_ENTRY(general_protection)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 errorentry do_general_protection
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001317KPROBE_END(general_protection)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319ENTRY(alignment_check)
1320 errorentry do_alignment_check
Jan Beulich4b787e02006-06-26 13:56:55 +02001321END(alignment_check)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323ENTRY(divide_error)
1324 zeroentry do_divide_error
Jan Beulich4b787e02006-06-26 13:56:55 +02001325END(divide_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327ENTRY(spurious_interrupt_bug)
1328 zeroentry do_spurious_interrupt_bug
Jan Beulich4b787e02006-06-26 13:56:55 +02001329END(spurious_interrupt_bug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331#ifdef CONFIG_X86_MCE
1332 /* runs on exception stack */
1333ENTRY(machine_check)
Jan Beulich7effaa82005-09-12 18:49:24 +02001334 INTR_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001335 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 pushq $0
1337 CFI_ADJUST_CFA_OFFSET 8
1338 paranoidentry do_machine_check
Ingo Molnar2601e642006-07-03 00:24:45 -07001339 jmp paranoid_exit1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001341END(machine_check)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342#endif
1343
Andi Kleen26995002006-08-02 22:37:28 +02001344/* Call softirq on interrupt stack. Interrupts are off. */
Andi Kleened6b6762005-07-28 21:15:49 -07001345ENTRY(call_softirq)
Jan Beulich7effaa82005-09-12 18:49:24 +02001346 CFI_STARTPROC
Andi Kleen26995002006-08-02 22:37:28 +02001347 push %rbp
1348 CFI_ADJUST_CFA_OFFSET 8
1349 CFI_REL_OFFSET rbp,0
1350 mov %rsp,%rbp
1351 CFI_DEF_CFA_REGISTER rbp
Andi Kleened6b6762005-07-28 21:15:49 -07001352 incl %gs:pda_irqcount
Andi Kleen26995002006-08-02 22:37:28 +02001353 cmove %gs:pda_irqstackptr,%rsp
1354 push %rbp # backlink for old unwinder
Andi Kleened6b6762005-07-28 21:15:49 -07001355 call __do_softirq
Andi Kleen26995002006-08-02 22:37:28 +02001356 leaveq
Jan Beulich7effaa82005-09-12 18:49:24 +02001357 CFI_DEF_CFA_REGISTER rsp
Andi Kleen26995002006-08-02 22:37:28 +02001358 CFI_ADJUST_CFA_OFFSET -8
Andi Kleened6b6762005-07-28 21:15:49 -07001359 decl %gs:pda_irqcount
Andi Kleened6b6762005-07-28 21:15:49 -07001360 ret
Jan Beulich7effaa82005-09-12 18:49:24 +02001361 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001362ENDPROC(call_softirq)
Andi Kleen75154f42007-06-23 02:29:25 +02001363
1364KPROBE_ENTRY(ignore_sysret)
1365 CFI_STARTPROC
1366 mov $-ENOSYS,%eax
1367 sysret
1368 CFI_ENDPROC
1369ENDPROC(ignore_sysret)
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001370
1371#ifdef CONFIG_XEN
1372ENTRY(xen_hypervisor_callback)
1373 zeroentry xen_do_hypervisor_callback
1374END(xen_hypervisor_callback)
1375
1376/*
1377# A note on the "critical region" in our callback handler.
1378# We want to avoid stacking callback handlers due to events occurring
1379# during handling of the last event. To do this, we keep events disabled
1380# until we've done all processing. HOWEVER, we must enable events before
1381# popping the stack frame (can't be done atomically) and so it would still
1382# be possible to get enough handler activations to overflow the stack.
1383# Although unlikely, bugs of that kind are hard to track down, so we'd
1384# like to avoid the possibility.
1385# So, on entry to the handler we detect whether we interrupted an
1386# existing activation in its critical region -- if so, we pop the current
1387# activation and restart the handler using the previous one.
1388*/
1389ENTRY(xen_do_hypervisor_callback) # do_hypervisor_callback(struct *pt_regs)
1390 CFI_STARTPROC
1391/* Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
1392 see the correct pointer to the pt_regs */
1393 movq %rdi, %rsp # we don't return, adjust the stack frame
1394 CFI_ENDPROC
1395 CFI_DEFAULT_STACK
139611: incl %gs:pda_irqcount
1397 movq %rsp,%rbp
1398 CFI_DEF_CFA_REGISTER rbp
1399 cmovzq %gs:pda_irqstackptr,%rsp
1400 pushq %rbp # backlink for old unwinder
1401 call xen_evtchn_do_upcall
1402 popq %rsp
1403 CFI_DEF_CFA_REGISTER rsp
1404 decl %gs:pda_irqcount
1405 jmp error_exit
1406 CFI_ENDPROC
1407END(do_hypervisor_callback)
1408
1409/*
1410# Hypervisor uses this for application faults while it executes.
1411# We get here for two reasons:
1412# 1. Fault while reloading DS, ES, FS or GS
1413# 2. Fault while executing IRET
1414# Category 1 we do not need to fix up as Xen has already reloaded all segment
1415# registers that could be reloaded and zeroed the others.
1416# Category 2 we fix up by killing the current process. We cannot use the
1417# normal Linux return path in this case because if we use the IRET hypercall
1418# to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1419# We distinguish between categories by comparing each saved segment register
1420# with its current contents: any discrepancy means we in category 1.
1421*/
1422ENTRY(xen_failsafe_callback)
Jeremy Fitzhardinge4a5c3e72008-07-08 15:07:09 -07001423 framesz = (RIP-0x30) /* workaround buggy gas */
1424 _frame framesz
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001425 CFI_REL_OFFSET rcx, 0
1426 CFI_REL_OFFSET r11, 8
1427 movw %ds,%cx
1428 cmpw %cx,0x10(%rsp)
1429 CFI_REMEMBER_STATE
1430 jne 1f
1431 movw %es,%cx
1432 cmpw %cx,0x18(%rsp)
1433 jne 1f
1434 movw %fs,%cx
1435 cmpw %cx,0x20(%rsp)
1436 jne 1f
1437 movw %gs,%cx
1438 cmpw %cx,0x28(%rsp)
1439 jne 1f
1440 /* All segments match their saved values => Category 2 (Bad IRET). */
1441 movq (%rsp),%rcx
1442 CFI_RESTORE rcx
1443 movq 8(%rsp),%r11
1444 CFI_RESTORE r11
1445 addq $0x30,%rsp
1446 CFI_ADJUST_CFA_OFFSET -0x30
Jeremy Fitzhardinge4a5c3e72008-07-08 15:07:09 -07001447 pushq $0
1448 CFI_ADJUST_CFA_OFFSET 8
1449 pushq %r11
1450 CFI_ADJUST_CFA_OFFSET 8
1451 pushq %rcx
1452 CFI_ADJUST_CFA_OFFSET 8
1453 jmp general_protection
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001454 CFI_RESTORE_STATE
14551: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
1456 movq (%rsp),%rcx
1457 CFI_RESTORE rcx
1458 movq 8(%rsp),%r11
1459 CFI_RESTORE r11
1460 addq $0x30,%rsp
1461 CFI_ADJUST_CFA_OFFSET -0x30
1462 pushq $0
1463 CFI_ADJUST_CFA_OFFSET 8
1464 SAVE_ALL
1465 jmp error_exit
1466 CFI_ENDPROC
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001467END(xen_failsafe_callback)
1468
1469#endif /* CONFIG_XEN */