blob: 977c8ea660288612a2de3b1dcd24d1e47d26523c [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
Alexander van Heukelum7e61a792008-09-26 14:03:03 +0200942 .if \irqtrace
943 TRACE_IRQS_OFF
944 .endif
Jan Beulichb556b352006-01-11 22:43:00 +0100945 movq %rsp,%rdi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 movq ORIG_RAX(%rsp),%rsi
947 movq $-1,ORIG_RAX(%rsp)
Jan Beulichb556b352006-01-11 22:43:00 +0100948 .if \ist
Andi Kleen5f8efbb2006-01-16 01:56:39 +0100949 subq $EXCEPTION_STKSZ, per_cpu__init_tss + TSS_ist + (\ist - 1) * 8(%rbp)
Jan Beulichb556b352006-01-11 22:43:00 +0100950 .endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 call \sym
Jan Beulichb556b352006-01-11 22:43:00 +0100952 .if \ist
Andi Kleen5f8efbb2006-01-16 01:56:39 +0100953 addq $EXCEPTION_STKSZ, per_cpu__init_tss + TSS_ist + (\ist - 1) * 8(%rbp)
Jan Beulichb556b352006-01-11 22:43:00 +0100954 .endif
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100955 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700956 .if \irqtrace
957 TRACE_IRQS_OFF
958 .endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 .endm
Ingo Molnar2601e642006-07-03 00:24:45 -0700960
961 /*
962 * "Paranoid" exit path from exception stack.
963 * Paranoid because this is used by NMIs and cannot take
964 * any kernel state for granted.
965 * We don't do kernel preemption checks here, because only
966 * NMI should be common and it does not enable IRQs and
967 * cannot get reschedule ticks.
968 *
969 * "trace" is 0 for the NMI handler only, because irq-tracing
970 * is fundamentally NMI-unsafe. (we cannot change the soft and
971 * hard flags at once, atomically)
972 */
973 .macro paranoidexit trace=1
974 /* ebx: no swapgs flag */
975paranoid_exit\trace:
976 testl %ebx,%ebx /* swapgs needed? */
977 jnz paranoid_restore\trace
978 testl $3,CS(%rsp)
979 jnz paranoid_userspace\trace
980paranoid_swapgs\trace:
Andi Kleen7a0a2df2006-09-26 10:52:37 +0200981 .if \trace
Ingo Molnar2601e642006-07-03 00:24:45 -0700982 TRACE_IRQS_IRETQ 0
Andi Kleen7a0a2df2006-09-26 10:52:37 +0200983 .endif
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100984 SWAPGS_UNSAFE_STACK
Ingo Molnar2601e642006-07-03 00:24:45 -0700985paranoid_restore\trace:
986 RESTORE_ALL 8
Ingo Molnar3701d8632008-02-09 23:24:08 +0100987 jmp irq_return
Ingo Molnar2601e642006-07-03 00:24:45 -0700988paranoid_userspace\trace:
989 GET_THREAD_INFO(%rcx)
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300990 movl TI_flags(%rcx),%ebx
Ingo Molnar2601e642006-07-03 00:24:45 -0700991 andl $_TIF_WORK_MASK,%ebx
992 jz paranoid_swapgs\trace
993 movq %rsp,%rdi /* &pt_regs */
994 call sync_regs
995 movq %rax,%rsp /* switch stack for scheduling */
996 testl $_TIF_NEED_RESCHED,%ebx
997 jnz paranoid_schedule\trace
998 movl %ebx,%edx /* arg3: thread flags */
999 .if \trace
1000 TRACE_IRQS_ON
1001 .endif
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001002 ENABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -07001003 xorl %esi,%esi /* arg2: oldset */
1004 movq %rsp,%rdi /* arg1: &pt_regs */
1005 call do_notify_resume
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001006 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -07001007 .if \trace
1008 TRACE_IRQS_OFF
1009 .endif
1010 jmp paranoid_userspace\trace
1011paranoid_schedule\trace:
1012 .if \trace
1013 TRACE_IRQS_ON
1014 .endif
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001015 ENABLE_INTERRUPTS(CLBR_ANY)
Ingo Molnar2601e642006-07-03 00:24:45 -07001016 call schedule
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001017 DISABLE_INTERRUPTS(CLBR_ANY)
Ingo Molnar2601e642006-07-03 00:24:45 -07001018 .if \trace
1019 TRACE_IRQS_OFF
1020 .endif
1021 jmp paranoid_userspace\trace
1022 CFI_ENDPROC
1023 .endm
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025/*
1026 * Exception entry point. This expects an error code/orig_rax on the stack
1027 * and the exception handler in %rax.
1028 */
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001029KPROBE_ENTRY(error_entry)
Jan Beulich7effaa82005-09-12 18:49:24 +02001030 _frame RDI
Jan Beulich37550902007-05-02 19:27:05 +02001031 CFI_REL_OFFSET rax,0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 /* rdi slot contains rax, oldrax contains error code */
1033 cld
1034 subq $14*8,%rsp
1035 CFI_ADJUST_CFA_OFFSET (14*8)
1036 movq %rsi,13*8(%rsp)
1037 CFI_REL_OFFSET rsi,RSI
1038 movq 14*8(%rsp),%rsi /* load rax from rdi slot */
Jan Beulich37550902007-05-02 19:27:05 +02001039 CFI_REGISTER rax,rsi
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 movq %rdx,12*8(%rsp)
1041 CFI_REL_OFFSET rdx,RDX
1042 movq %rcx,11*8(%rsp)
1043 CFI_REL_OFFSET rcx,RCX
1044 movq %rsi,10*8(%rsp) /* store rax */
1045 CFI_REL_OFFSET rax,RAX
1046 movq %r8, 9*8(%rsp)
1047 CFI_REL_OFFSET r8,R8
1048 movq %r9, 8*8(%rsp)
1049 CFI_REL_OFFSET r9,R9
1050 movq %r10,7*8(%rsp)
1051 CFI_REL_OFFSET r10,R10
1052 movq %r11,6*8(%rsp)
1053 CFI_REL_OFFSET r11,R11
1054 movq %rbx,5*8(%rsp)
1055 CFI_REL_OFFSET rbx,RBX
1056 movq %rbp,4*8(%rsp)
1057 CFI_REL_OFFSET rbp,RBP
1058 movq %r12,3*8(%rsp)
1059 CFI_REL_OFFSET r12,R12
1060 movq %r13,2*8(%rsp)
1061 CFI_REL_OFFSET r13,R13
1062 movq %r14,1*8(%rsp)
1063 CFI_REL_OFFSET r14,R14
1064 movq %r15,(%rsp)
1065 CFI_REL_OFFSET r15,R15
1066 xorl %ebx,%ebx
1067 testl $3,CS(%rsp)
1068 je error_kernelspace
1069error_swapgs:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001070 SWAPGS
Alexander van Heukelum6b11d4e2008-09-26 14:03:02 +02001071error_sti:
1072 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 movq %rdi,RDI(%rsp)
Jan Beulich37550902007-05-02 19:27:05 +02001074 CFI_REL_OFFSET rdi,RDI
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 movq %rsp,%rdi
1076 movq ORIG_RAX(%rsp),%rsi /* get error code */
1077 movq $-1,ORIG_RAX(%rsp)
1078 call *%rax
Peter Zijlstra10cd7062007-10-11 22:11:12 +02001079 /* ebx: no swapgs flag (1: don't need swapgs, 0: need it) */
1080error_exit:
1081 movl %ebx,%eax
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 RESTORE_REST
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001083 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -07001084 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 GET_THREAD_INFO(%rcx)
1086 testl %eax,%eax
1087 jne retint_kernel
Peter Zijlstra10cd7062007-10-11 22:11:12 +02001088 LOCKDEP_SYS_EXIT_IRQ
Glauber Costa26ccb8a2008-06-24 11:19:35 -03001089 movl TI_flags(%rcx),%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 movl $_TIF_WORK_MASK,%edi
1091 andl %edi,%edx
1092 jnz retint_careful
Peter Zijlstra10cd7062007-10-11 22:11:12 +02001093 jmp retint_swapgs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 CFI_ENDPROC
1095
1096error_kernelspace:
1097 incl %ebx
1098 /* There are two places in the kernel that can potentially fault with
1099 usergs. Handle them here. The exception handlers after
1100 iret run with kernel gs again, so don't set the user space flag.
1101 B stepping K8s sometimes report an truncated RIP for IRET
1102 exceptions returning to compat mode. Check for these here too. */
Vegard Nossum9d8ad5d2008-06-27 17:22:17 +02001103 leaq irq_return(%rip),%rcx
1104 cmpq %rcx,RIP(%rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 je error_swapgs
Vegard Nossum9d8ad5d2008-06-27 17:22:17 +02001106 movl %ecx,%ecx /* zero extend */
1107 cmpq %rcx,RIP(%rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 je error_swapgs
1109 cmpq $gs_change,RIP(%rsp)
1110 je error_swapgs
1111 jmp error_sti
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001112KPROBE_END(error_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 /* Reload gs selector with exception handling */
1115 /* edi: new selector */
Jeremy Fitzhardinge9f9d4892008-06-25 00:19:32 -04001116ENTRY(native_load_gs_index)
Jan Beulich7effaa82005-09-12 18:49:24 +02001117 CFI_STARTPROC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 pushf
Jan Beulich7effaa82005-09-12 18:49:24 +02001119 CFI_ADJUST_CFA_OFFSET 8
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001120 DISABLE_INTERRUPTS(CLBR_ANY | ~(CLBR_RDI))
1121 SWAPGS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122gs_change:
1123 movl %edi,%gs
11242: mfence /* workaround */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001125 SWAPGS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 popf
Jan Beulich7effaa82005-09-12 18:49:24 +02001127 CFI_ADJUST_CFA_OFFSET -8
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 ret
Jan Beulich7effaa82005-09-12 18:49:24 +02001129 CFI_ENDPROC
Jeremy Fitzhardinge9f9d4892008-06-25 00:19:32 -04001130ENDPROC(native_load_gs_index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 .section __ex_table,"a"
1133 .align 8
1134 .quad gs_change,bad_gs
1135 .previous
1136 .section .fixup,"ax"
1137 /* running with kernelgs */
1138bad_gs:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001139 SWAPGS /* switch back to user gs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 xorl %eax,%eax
1141 movl %eax,%gs
1142 jmp 2b
1143 .previous
1144
1145/*
1146 * Create a kernel thread.
1147 *
1148 * C extern interface:
1149 * extern long kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
1150 *
1151 * asm input arguments:
1152 * rdi: fn, rsi: arg, rdx: flags
1153 */
1154ENTRY(kernel_thread)
1155 CFI_STARTPROC
1156 FAKE_STACK_FRAME $child_rip
1157 SAVE_ALL
1158
1159 # rdi: flags, rsi: usp, rdx: will be &pt_regs
1160 movq %rdx,%rdi
1161 orq kernel_thread_flags(%rip),%rdi
1162 movq $-1, %rsi
1163 movq %rsp, %rdx
1164
1165 xorl %r8d,%r8d
1166 xorl %r9d,%r9d
1167
1168 # clone now
1169 call do_fork
1170 movq %rax,RAX(%rsp)
1171 xorl %edi,%edi
1172
1173 /*
1174 * It isn't worth to check for reschedule here,
1175 * so internally to the x86_64 port you can rely on kernel_thread()
1176 * not to reschedule the child before returning, this avoids the need
1177 * of hacks for example to fork off the per-CPU idle tasks.
1178 * [Hopefully no generic code relies on the reschedule -AK]
1179 */
1180 RESTORE_ALL
1181 UNFAKE_STACK_FRAME
1182 ret
1183 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001184ENDPROC(kernel_thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186child_rip:
Andi Kleenc05991e2006-08-30 19:37:08 +02001187 pushq $0 # fake return address
1188 CFI_STARTPROC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 /*
1190 * Here we are in the child and the registers are set as they were
1191 * at kernel_thread() invocation in the parent.
1192 */
1193 movq %rdi, %rax
1194 movq %rsi, %rdi
1195 call *%rax
1196 # exit
Andrey Mirkin1c5b5cf2007-10-17 18:04:33 +02001197 mov %eax, %edi
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 call do_exit
Andi Kleenc05991e2006-08-30 19:37:08 +02001199 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001200ENDPROC(child_rip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202/*
1203 * execve(). This function needs to use IRET, not SYSRET, to set up all state properly.
1204 *
1205 * C extern interface:
1206 * extern long execve(char *name, char **argv, char **envp)
1207 *
1208 * asm input arguments:
1209 * rdi: name, rsi: argv, rdx: envp
1210 *
1211 * We want to fallback into:
Ingo Molnar5d119b22008-02-26 12:55:57 +01001212 * extern long sys_execve(char *name, char **argv,char **envp, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 *
1214 * do_sys_execve asm fallback arguments:
Ingo Molnar5d119b22008-02-26 12:55:57 +01001215 * rdi: name, rsi: argv, rdx: envp, rcx: fake frame on the stack
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 */
Arnd Bergmann3db03b42006-10-02 02:18:31 -07001217ENTRY(kernel_execve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 CFI_STARTPROC
1219 FAKE_STACK_FRAME $0
1220 SAVE_ALL
Ingo Molnar5d119b22008-02-26 12:55:57 +01001221 movq %rsp,%rcx
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 call sys_execve
1223 movq %rax, RAX(%rsp)
1224 RESTORE_REST
1225 testq %rax,%rax
1226 je int_ret_from_sys_call
1227 RESTORE_ARGS
1228 UNFAKE_STACK_FRAME
1229 ret
1230 CFI_ENDPROC
Arnd Bergmann3db03b42006-10-02 02:18:31 -07001231ENDPROC(kernel_execve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001233KPROBE_ENTRY(page_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 errorentry do_page_fault
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001235KPROBE_END(page_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237ENTRY(coprocessor_error)
1238 zeroentry do_coprocessor_error
Jan Beulich4b787e02006-06-26 13:56:55 +02001239END(coprocessor_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241ENTRY(simd_coprocessor_error)
1242 zeroentry do_simd_coprocessor_error
Jan Beulich4b787e02006-06-26 13:56:55 +02001243END(simd_coprocessor_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245ENTRY(device_not_available)
1246 zeroentry math_state_restore
Jan Beulich4b787e02006-06-26 13:56:55 +02001247END(device_not_available)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 /* runs on exception stack */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001250KPROBE_ENTRY(debug)
Jan Beulich7effaa82005-09-12 18:49:24 +02001251 INTR_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001252 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 pushq $0
1254 CFI_ADJUST_CFA_OFFSET 8
Andi Kleen5f8efbb2006-01-16 01:56:39 +01001255 paranoidentry do_debug, DEBUG_STACK
Ingo Molnar2601e642006-07-03 00:24:45 -07001256 paranoidexit
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001257KPROBE_END(debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 /* runs on exception stack */
Andi Kleeneddb6fb2006-02-03 21:50:41 +01001260KPROBE_ENTRY(nmi)
Jan Beulich7effaa82005-09-12 18:49:24 +02001261 INTR_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001262 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 pushq $-1
Jan Beulich7effaa82005-09-12 18:49:24 +02001264 CFI_ADJUST_CFA_OFFSET 8
Ingo Molnar2601e642006-07-03 00:24:45 -07001265 paranoidentry do_nmi, 0, 0
1266#ifdef CONFIG_TRACE_IRQFLAGS
1267 paranoidexit 0
1268#else
1269 jmp paranoid_exit1
1270 CFI_ENDPROC
1271#endif
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001272KPROBE_END(nmi)
Andi Kleen6fefb0d2005-04-16 15:25:03 -07001273
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001274KPROBE_ENTRY(int3)
Jan Beulichb556b352006-01-11 22:43:00 +01001275 INTR_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001276 PARAVIRT_ADJUST_EXCEPTION_FRAME
Jan Beulichb556b352006-01-11 22:43:00 +01001277 pushq $0
1278 CFI_ADJUST_CFA_OFFSET 8
Andi Kleen5f8efbb2006-01-16 01:56:39 +01001279 paranoidentry do_int3, DEBUG_STACK
Ingo Molnar2601e642006-07-03 00:24:45 -07001280 jmp paranoid_exit1
Jan Beulichb556b352006-01-11 22:43:00 +01001281 CFI_ENDPROC
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001282KPROBE_END(int3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284ENTRY(overflow)
1285 zeroentry do_overflow
Jan Beulich4b787e02006-06-26 13:56:55 +02001286END(overflow)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288ENTRY(bounds)
1289 zeroentry do_bounds
Jan Beulich4b787e02006-06-26 13:56:55 +02001290END(bounds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292ENTRY(invalid_op)
1293 zeroentry do_invalid_op
Jan Beulich4b787e02006-06-26 13:56:55 +02001294END(invalid_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296ENTRY(coprocessor_segment_overrun)
1297 zeroentry do_coprocessor_segment_overrun
Jan Beulich4b787e02006-06-26 13:56:55 +02001298END(coprocessor_segment_overrun)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 /* runs on exception stack */
1301ENTRY(double_fault)
Jan Beulich7effaa82005-09-12 18:49:24 +02001302 XCPT_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001303 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 paranoidentry do_double_fault
Ingo Molnar2601e642006-07-03 00:24:45 -07001305 jmp paranoid_exit1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001307END(double_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309ENTRY(invalid_TSS)
1310 errorentry do_invalid_TSS
Jan Beulich4b787e02006-06-26 13:56:55 +02001311END(invalid_TSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313ENTRY(segment_not_present)
1314 errorentry do_segment_not_present
Jan Beulich4b787e02006-06-26 13:56:55 +02001315END(segment_not_present)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 /* runs on exception stack */
1318ENTRY(stack_segment)
Jan Beulich7effaa82005-09-12 18:49:24 +02001319 XCPT_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001320 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 paranoidentry do_stack_segment
Ingo Molnar2601e642006-07-03 00:24:45 -07001322 jmp paranoid_exit1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001324END(stack_segment)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001326KPROBE_ENTRY(general_protection)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 errorentry do_general_protection
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001328KPROBE_END(general_protection)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330ENTRY(alignment_check)
1331 errorentry do_alignment_check
Jan Beulich4b787e02006-06-26 13:56:55 +02001332END(alignment_check)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334ENTRY(divide_error)
1335 zeroentry do_divide_error
Jan Beulich4b787e02006-06-26 13:56:55 +02001336END(divide_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338ENTRY(spurious_interrupt_bug)
1339 zeroentry do_spurious_interrupt_bug
Jan Beulich4b787e02006-06-26 13:56:55 +02001340END(spurious_interrupt_bug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342#ifdef CONFIG_X86_MCE
1343 /* runs on exception stack */
1344ENTRY(machine_check)
Jan Beulich7effaa82005-09-12 18:49:24 +02001345 INTR_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001346 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 pushq $0
1348 CFI_ADJUST_CFA_OFFSET 8
1349 paranoidentry do_machine_check
Ingo Molnar2601e642006-07-03 00:24:45 -07001350 jmp paranoid_exit1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001352END(machine_check)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353#endif
1354
Andi Kleen26995002006-08-02 22:37:28 +02001355/* Call softirq on interrupt stack. Interrupts are off. */
Andi Kleened6b6762005-07-28 21:15:49 -07001356ENTRY(call_softirq)
Jan Beulich7effaa82005-09-12 18:49:24 +02001357 CFI_STARTPROC
Andi Kleen26995002006-08-02 22:37:28 +02001358 push %rbp
1359 CFI_ADJUST_CFA_OFFSET 8
1360 CFI_REL_OFFSET rbp,0
1361 mov %rsp,%rbp
1362 CFI_DEF_CFA_REGISTER rbp
Andi Kleened6b6762005-07-28 21:15:49 -07001363 incl %gs:pda_irqcount
Andi Kleen26995002006-08-02 22:37:28 +02001364 cmove %gs:pda_irqstackptr,%rsp
1365 push %rbp # backlink for old unwinder
Andi Kleened6b6762005-07-28 21:15:49 -07001366 call __do_softirq
Andi Kleen26995002006-08-02 22:37:28 +02001367 leaveq
Jan Beulich7effaa82005-09-12 18:49:24 +02001368 CFI_DEF_CFA_REGISTER rsp
Andi Kleen26995002006-08-02 22:37:28 +02001369 CFI_ADJUST_CFA_OFFSET -8
Andi Kleened6b6762005-07-28 21:15:49 -07001370 decl %gs:pda_irqcount
Andi Kleened6b6762005-07-28 21:15:49 -07001371 ret
Jan Beulich7effaa82005-09-12 18:49:24 +02001372 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001373ENDPROC(call_softirq)
Andi Kleen75154f42007-06-23 02:29:25 +02001374
1375KPROBE_ENTRY(ignore_sysret)
1376 CFI_STARTPROC
1377 mov $-ENOSYS,%eax
1378 sysret
1379 CFI_ENDPROC
1380ENDPROC(ignore_sysret)
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001381
1382#ifdef CONFIG_XEN
1383ENTRY(xen_hypervisor_callback)
1384 zeroentry xen_do_hypervisor_callback
1385END(xen_hypervisor_callback)
1386
1387/*
1388# A note on the "critical region" in our callback handler.
1389# We want to avoid stacking callback handlers due to events occurring
1390# during handling of the last event. To do this, we keep events disabled
1391# until we've done all processing. HOWEVER, we must enable events before
1392# popping the stack frame (can't be done atomically) and so it would still
1393# be possible to get enough handler activations to overflow the stack.
1394# Although unlikely, bugs of that kind are hard to track down, so we'd
1395# like to avoid the possibility.
1396# So, on entry to the handler we detect whether we interrupted an
1397# existing activation in its critical region -- if so, we pop the current
1398# activation and restart the handler using the previous one.
1399*/
1400ENTRY(xen_do_hypervisor_callback) # do_hypervisor_callback(struct *pt_regs)
1401 CFI_STARTPROC
1402/* Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
1403 see the correct pointer to the pt_regs */
1404 movq %rdi, %rsp # we don't return, adjust the stack frame
1405 CFI_ENDPROC
1406 CFI_DEFAULT_STACK
140711: incl %gs:pda_irqcount
1408 movq %rsp,%rbp
1409 CFI_DEF_CFA_REGISTER rbp
1410 cmovzq %gs:pda_irqstackptr,%rsp
1411 pushq %rbp # backlink for old unwinder
1412 call xen_evtchn_do_upcall
1413 popq %rsp
1414 CFI_DEF_CFA_REGISTER rsp
1415 decl %gs:pda_irqcount
1416 jmp error_exit
1417 CFI_ENDPROC
1418END(do_hypervisor_callback)
1419
1420/*
1421# Hypervisor uses this for application faults while it executes.
1422# We get here for two reasons:
1423# 1. Fault while reloading DS, ES, FS or GS
1424# 2. Fault while executing IRET
1425# Category 1 we do not need to fix up as Xen has already reloaded all segment
1426# registers that could be reloaded and zeroed the others.
1427# Category 2 we fix up by killing the current process. We cannot use the
1428# normal Linux return path in this case because if we use the IRET hypercall
1429# to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1430# We distinguish between categories by comparing each saved segment register
1431# with its current contents: any discrepancy means we in category 1.
1432*/
1433ENTRY(xen_failsafe_callback)
Jeremy Fitzhardinge4a5c3e72008-07-08 15:07:09 -07001434 framesz = (RIP-0x30) /* workaround buggy gas */
1435 _frame framesz
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001436 CFI_REL_OFFSET rcx, 0
1437 CFI_REL_OFFSET r11, 8
1438 movw %ds,%cx
1439 cmpw %cx,0x10(%rsp)
1440 CFI_REMEMBER_STATE
1441 jne 1f
1442 movw %es,%cx
1443 cmpw %cx,0x18(%rsp)
1444 jne 1f
1445 movw %fs,%cx
1446 cmpw %cx,0x20(%rsp)
1447 jne 1f
1448 movw %gs,%cx
1449 cmpw %cx,0x28(%rsp)
1450 jne 1f
1451 /* All segments match their saved values => Category 2 (Bad IRET). */
1452 movq (%rsp),%rcx
1453 CFI_RESTORE rcx
1454 movq 8(%rsp),%r11
1455 CFI_RESTORE r11
1456 addq $0x30,%rsp
1457 CFI_ADJUST_CFA_OFFSET -0x30
Jeremy Fitzhardinge4a5c3e72008-07-08 15:07:09 -07001458 pushq $0
1459 CFI_ADJUST_CFA_OFFSET 8
1460 pushq %r11
1461 CFI_ADJUST_CFA_OFFSET 8
1462 pushq %rcx
1463 CFI_ADJUST_CFA_OFFSET 8
1464 jmp general_protection
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001465 CFI_RESTORE_STATE
14661: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
1467 movq (%rsp),%rcx
1468 CFI_RESTORE rcx
1469 movq 8(%rsp),%r11
1470 CFI_RESTORE r11
1471 addq $0x30,%rsp
1472 CFI_ADJUST_CFA_OFFSET -0x30
1473 pushq $0
1474 CFI_ADJUST_CFA_OFFSET 8
1475 SAVE_ALL
1476 jmp error_exit
1477 CFI_ENDPROC
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001478END(xen_failsafe_callback)
1479
1480#endif /* CONFIG_XEN */