blob: 92c5e18340db16564b7ca6fde314bdb3970966c9 [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.
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +010014 *
15 * Normal syscalls and interrupts don't save a full stack frame, this is
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * only done for syscall tracing, signals or fork/exec et.al.
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +010017 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 * - partial stack frame: partially saved registers upto R11.
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +010022 * - 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
Alexander van Heukelumdcd072e2008-11-20 14:40:11 +010063/*
64 * Some macro's to hide the most frequently occuring CFI annotations.
65 */
Ingo Molnar14ae22b2008-11-21 15:20:47 +010066 .macro pushq_cfi reg
Alexander van Heukelumdcd072e2008-11-20 14:40:11 +010067 pushq \reg
68 CFI_ADJUST_CFA_OFFSET 8
69 .endm
70
Ingo Molnar14ae22b2008-11-21 15:20:47 +010071 .macro popq_cfi reg
Alexander van Heukelumdcd072e2008-11-20 14:40:11 +010072 popq \reg
73 CFI_ADJUST_CFA_OFFSET -8
74 .endm
75
Ingo Molnar14ae22b2008-11-21 15:20:47 +010076 .macro movq_cfi reg offset=0
Alexander van Heukelumdcd072e2008-11-20 14:40:11 +010077 movq %\reg, \offset(%rsp)
78 CFI_REL_OFFSET \reg, \offset
79 .endm
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Steven Rostedt606576c2008-10-06 19:06:12 -040081#ifdef CONFIG_FUNCTION_TRACER
Steven Rostedtd61f82d2008-05-12 21:20:43 +020082#ifdef CONFIG_DYNAMIC_FTRACE
83ENTRY(mcount)
Steven Rostedtd61f82d2008-05-12 21:20:43 +020084 retq
85END(mcount)
86
87ENTRY(ftrace_caller)
88
89 /* taken from glibc */
90 subq $0x38, %rsp
91 movq %rax, (%rsp)
92 movq %rcx, 8(%rsp)
93 movq %rdx, 16(%rsp)
94 movq %rsi, 24(%rsp)
95 movq %rdi, 32(%rsp)
96 movq %r8, 40(%rsp)
97 movq %r9, 48(%rsp)
98
99 movq 0x38(%rsp), %rdi
100 movq 8(%rbp), %rsi
Abhishek Sagar395a59d2008-06-21 23:47:27 +0530101 subq $MCOUNT_INSN_SIZE, %rdi
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200102
103.globl ftrace_call
104ftrace_call:
105 call ftrace_stub
106
107 movq 48(%rsp), %r9
108 movq 40(%rsp), %r8
109 movq 32(%rsp), %rdi
110 movq 24(%rsp), %rsi
111 movq 16(%rsp), %rdx
112 movq 8(%rsp), %rcx
113 movq (%rsp), %rax
114 addq $0x38, %rsp
115
116.globl ftrace_stub
117ftrace_stub:
118 retq
119END(ftrace_caller)
120
121#else /* ! CONFIG_DYNAMIC_FTRACE */
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200122ENTRY(mcount)
123 cmpq $ftrace_stub, ftrace_trace_function
124 jnz trace
125.globl ftrace_stub
126ftrace_stub:
127 retq
128
129trace:
130 /* taken from glibc */
131 subq $0x38, %rsp
132 movq %rax, (%rsp)
133 movq %rcx, 8(%rsp)
134 movq %rdx, 16(%rsp)
135 movq %rsi, 24(%rsp)
136 movq %rdi, 32(%rsp)
137 movq %r8, 40(%rsp)
138 movq %r9, 48(%rsp)
139
140 movq 0x38(%rsp), %rdi
141 movq 8(%rbp), %rsi
Abhishek Sagar395a59d2008-06-21 23:47:27 +0530142 subq $MCOUNT_INSN_SIZE, %rdi
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200143
144 call *ftrace_trace_function
145
146 movq 48(%rsp), %r9
147 movq 40(%rsp), %r8
148 movq 32(%rsp), %rdi
149 movq 24(%rsp), %rsi
150 movq 16(%rsp), %rdx
151 movq 8(%rsp), %rcx
152 movq (%rsp), %rax
153 addq $0x38, %rsp
154
155 jmp ftrace_stub
156END(mcount)
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200157#endif /* CONFIG_DYNAMIC_FTRACE */
Steven Rostedt606576c2008-10-06 19:06:12 -0400158#endif /* CONFIG_FUNCTION_TRACER */
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200159
Andi Kleendc37db42005-04-16 15:25:05 -0700160#ifndef CONFIG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161#define retint_kernel retint_restore_args
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100162#endif
Ingo Molnar2601e642006-07-03 00:24:45 -0700163
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100164#ifdef CONFIG_PARAVIRT
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -0400165ENTRY(native_usergs_sysret64)
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100166 swapgs
167 sysretq
168#endif /* CONFIG_PARAVIRT */
169
Ingo Molnar2601e642006-07-03 00:24:45 -0700170
171.macro TRACE_IRQS_IRETQ offset=ARGOFFSET
172#ifdef CONFIG_TRACE_IRQFLAGS
173 bt $9,EFLAGS-\offset(%rsp) /* interrupts off? */
174 jnc 1f
175 TRACE_IRQS_ON
1761:
177#endif
178.endm
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180/*
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100181 * C code is not supposed to know about undefined top of stack. Every time
182 * a C function with an pt_regs argument is called from the SYSCALL based
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * fast path FIXUP_TOP_OF_STACK is needed.
184 * RESTORE_TOP_OF_STACK syncs the syscall state after any possible ptregs
185 * manipulation.
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100186 */
187
188 /* %rsp:at FRAMEEND */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 .macro FIXUP_TOP_OF_STACK tmp
190 movq %gs:pda_oldrsp,\tmp
191 movq \tmp,RSP(%rsp)
192 movq $__USER_DS,SS(%rsp)
193 movq $__USER_CS,CS(%rsp)
194 movq $-1,RCX(%rsp)
195 movq R11(%rsp),\tmp /* get eflags */
196 movq \tmp,EFLAGS(%rsp)
197 .endm
198
199 .macro RESTORE_TOP_OF_STACK tmp,offset=0
200 movq RSP-\offset(%rsp),\tmp
201 movq \tmp,%gs:pda_oldrsp
202 movq EFLAGS-\offset(%rsp),\tmp
203 movq \tmp,R11-\offset(%rsp)
204 .endm
205
206 .macro FAKE_STACK_FRAME child_rip
207 /* push in order ss, rsp, eflags, cs, rip */
Andi Kleen3829ee62005-07-28 21:15:48 -0700208 xorl %eax, %eax
Jeremy Fitzhardingee04e0a62008-06-25 00:19:25 -0400209 pushq $__KERNEL_DS /* ss */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 CFI_ADJUST_CFA_OFFSET 8
Jan Beulich7effaa82005-09-12 18:49:24 +0200211 /*CFI_REL_OFFSET ss,0*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 pushq %rax /* rsp */
213 CFI_ADJUST_CFA_OFFSET 8
Jan Beulich7effaa82005-09-12 18:49:24 +0200214 CFI_REL_OFFSET rsp,0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 pushq $(1<<9) /* eflags - interrupts on */
216 CFI_ADJUST_CFA_OFFSET 8
Jan Beulich7effaa82005-09-12 18:49:24 +0200217 /*CFI_REL_OFFSET rflags,0*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 pushq $__KERNEL_CS /* cs */
219 CFI_ADJUST_CFA_OFFSET 8
Jan Beulich7effaa82005-09-12 18:49:24 +0200220 /*CFI_REL_OFFSET cs,0*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 pushq \child_rip /* rip */
222 CFI_ADJUST_CFA_OFFSET 8
Jan Beulich7effaa82005-09-12 18:49:24 +0200223 CFI_REL_OFFSET rip,0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 pushq %rax /* orig rax */
225 CFI_ADJUST_CFA_OFFSET 8
226 .endm
227
228 .macro UNFAKE_STACK_FRAME
229 addq $8*6, %rsp
230 CFI_ADJUST_CFA_OFFSET -(6*8)
231 .endm
232
Alexander van Heukelumd99015b2008-11-19 01:18:11 +0100233/*
Alexander van Heukelumdcd072e2008-11-20 14:40:11 +0100234 * initial frame state for interrupts (and exceptions without error code)
Alexander van Heukelumd99015b2008-11-19 01:18:11 +0100235 */
Alexander van Heukelumdcd072e2008-11-20 14:40:11 +0100236 .macro EMPTY_FRAME start=1 offset=0
237 .if \start
Alexander van Heukelumd99015b2008-11-19 01:18:11 +0100238 CFI_STARTPROC simple
239 CFI_SIGNAL_FRAME
Alexander van Heukelumdcd072e2008-11-20 14:40:11 +0100240 CFI_DEF_CFA rsp,8+\offset
241 .else
242 CFI_DEF_CFA_OFFSET 8+\offset
243 .endif
Alexander van Heukelumd99015b2008-11-19 01:18:11 +0100244 .endm
245
246/*
247 * initial frame state for interrupts (and exceptions without error code)
248 */
Alexander van Heukelumdcd072e2008-11-20 14:40:11 +0100249 .macro INTR_FRAME start=1 offset=0
Ingo Molnare8a0e272008-11-21 15:11:32 +0100250 EMPTY_FRAME \start, SS+8+\offset-RIP
251 /*CFI_REL_OFFSET ss, SS+\offset-RIP*/
252 CFI_REL_OFFSET rsp, RSP+\offset-RIP
253 /*CFI_REL_OFFSET rflags, EFLAGS+\offset-RIP*/
254 /*CFI_REL_OFFSET cs, CS+\offset-RIP*/
255 CFI_REL_OFFSET rip, RIP+\offset-RIP
Alexander van Heukelumdcd072e2008-11-20 14:40:11 +0100256 .endm
257
Alexander van Heukelumd99015b2008-11-19 01:18:11 +0100258/*
259 * initial frame state for exceptions with error code (and interrupts
260 * with vector already pushed)
261 */
Alexander van Heukelumdcd072e2008-11-20 14:40:11 +0100262 .macro XCPT_FRAME start=1 offset=0
Ingo Molnare8a0e272008-11-21 15:11:32 +0100263 INTR_FRAME \start, RIP+\offset-ORIG_RAX
Alexander van Heukelumdcd072e2008-11-20 14:40:11 +0100264 /*CFI_REL_OFFSET orig_rax, ORIG_RAX-ORIG_RAX*/
265 .endm
266
267/*
268 * frame that enables calling into C.
269 */
270 .macro PARTIAL_FRAME start=1 offset=0
Ingo Molnare8a0e272008-11-21 15:11:32 +0100271 XCPT_FRAME \start, ORIG_RAX+\offset-ARGOFFSET
272 CFI_REL_OFFSET rdi, RDI+\offset-ARGOFFSET
273 CFI_REL_OFFSET rsi, RSI+\offset-ARGOFFSET
274 CFI_REL_OFFSET rdx, RDX+\offset-ARGOFFSET
275 CFI_REL_OFFSET rcx, RCX+\offset-ARGOFFSET
276 CFI_REL_OFFSET rax, RAX+\offset-ARGOFFSET
277 CFI_REL_OFFSET r8, R8+\offset-ARGOFFSET
278 CFI_REL_OFFSET r9, R9+\offset-ARGOFFSET
279 CFI_REL_OFFSET r10, R10+\offset-ARGOFFSET
280 CFI_REL_OFFSET r11, R11+\offset-ARGOFFSET
Alexander van Heukelumdcd072e2008-11-20 14:40:11 +0100281 .endm
282
283/*
284 * frame that enables passing a complete pt_regs to a C function.
285 */
286 .macro DEFAULT_FRAME start=1 offset=0
Ingo Molnare8a0e272008-11-21 15:11:32 +0100287 PARTIAL_FRAME \start, R11+\offset-R15
Alexander van Heukelumdcd072e2008-11-20 14:40:11 +0100288 CFI_REL_OFFSET rbx, RBX+\offset
289 CFI_REL_OFFSET rbp, RBP+\offset
290 CFI_REL_OFFSET r12, R12+\offset
291 CFI_REL_OFFSET r13, R13+\offset
292 CFI_REL_OFFSET r14, R14+\offset
293 CFI_REL_OFFSET r15, R15+\offset
294 .endm
Alexander van Heukelumd99015b2008-11-19 01:18:11 +0100295
296/* save partial stack frame */
297ENTRY(save_args)
298 XCPT_FRAME
299 cld
Ingo Molnar14ae22b2008-11-21 15:20:47 +0100300 movq_cfi rdi, RDI+16-ARGOFFSET
301 movq_cfi rsi, RSI+16-ARGOFFSET
302 movq_cfi rdx, RDX+16-ARGOFFSET
303 movq_cfi rcx, RCX+16-ARGOFFSET
304 movq_cfi rax, RAX+16-ARGOFFSET
305 movq_cfi r8, R8+16-ARGOFFSET
306 movq_cfi r9, R9+16-ARGOFFSET
307 movq_cfi r10, R10+16-ARGOFFSET
308 movq_cfi r11, R11+16-ARGOFFSET
309
Alexander van Heukelumd99015b2008-11-19 01:18:11 +0100310 leaq -ARGOFFSET+16(%rsp),%rdi /* arg1 for handler */
Ingo Molnar14ae22b2008-11-21 15:20:47 +0100311 movq_cfi rbp, 8 /* push %rbp */
Alexander van Heukelumd99015b2008-11-19 01:18:11 +0100312 leaq 8(%rsp), %rbp /* mov %rsp, %ebp */
313 testl $3, CS(%rdi)
314 je 1f
315 SWAPGS
316 /*
317 * irqcount is used to check if a CPU is already on an interrupt stack
318 * or not. While this is essentially redundant with preempt_count it is
319 * a little cheaper to use a separate counter in the PDA (short of
320 * moving irq_enter into assembly, which would be too much work)
321 */
3221: incl %gs:pda_irqcount
323 jne 2f
Ingo Molnar14ae22b2008-11-21 15:20:47 +0100324 popq_cfi %rax /* move return address... */
Alexander van Heukelumd99015b2008-11-19 01:18:11 +0100325 mov %gs:pda_irqstackptr,%rsp
Alexander van Heukelumdcd072e2008-11-20 14:40:11 +0100326 EMPTY_FRAME 0
Ingo Molnar14ae22b2008-11-21 15:20:47 +0100327 pushq_cfi %rax /* ... to the new stack */
Alexander van Heukelumd99015b2008-11-19 01:18:11 +0100328 /*
329 * We entered an interrupt context - irqs are off:
330 */
3312: TRACE_IRQS_OFF
332 ret
333 CFI_ENDPROC
334END(save_args)
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336/*
337 * A newly forked process directly context switches into this.
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100338 */
339/* rdi: prev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340ENTRY(ret_from_fork)
Alexander van Heukelumdcd072e2008-11-20 14:40:11 +0100341 DEFAULT_FRAME
Andi Kleen658fdbe2006-09-26 10:52:41 +0200342 push kernel_eflags(%rip)
Alexander van Heukelume0a5a5d2008-07-22 18:14:16 +0200343 CFI_ADJUST_CFA_OFFSET 8
Andi Kleen658fdbe2006-09-26 10:52:41 +0200344 popf # reset kernel eflags
Alexander van Heukelume0a5a5d2008-07-22 18:14:16 +0200345 CFI_ADJUST_CFA_OFFSET -8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 call schedule_tail
347 GET_THREAD_INFO(%rcx)
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300348 testl $(_TIF_SYSCALL_TRACE|_TIF_SYSCALL_AUDIT),TI_flags(%rcx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 jnz rff_trace
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100350rff_action:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 RESTORE_REST
352 testl $3,CS-ARGOFFSET(%rsp) # from kernel_thread?
353 je int_ret_from_sys_call
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300354 testl $_TIF_IA32,TI_flags(%rcx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 jnz int_ret_from_sys_call
356 RESTORE_TOP_OF_STACK %rdi,ARGOFFSET
357 jmp ret_from_sys_call
358rff_trace:
359 movq %rsp,%rdi
360 call syscall_trace_leave
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100361 GET_THREAD_INFO(%rcx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 jmp rff_action
363 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200364END(ret_from_fork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366/*
367 * System call entry. Upto 6 arguments in registers are supported.
368 *
369 * SYSCALL does not save anything on the stack and does not change the
370 * stack pointer.
371 */
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373/*
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100374 * Register setup:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 * rax system call number
376 * rdi arg0
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100377 * rcx return address for syscall/sysret, C arg3
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 * rsi arg1
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100379 * rdx arg2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 * r10 arg3 (--> moved to rcx for C)
381 * r8 arg4
382 * r9 arg5
383 * r11 eflags for syscall/sysret, temporary for C
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100384 * r12-r15,rbp,rbx saved by C code, not touched.
385 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 * Interrupts are off on entry.
387 * Only called from user space.
388 *
389 * XXX if we had a free scratch register we could save the RSP into the stack frame
390 * and report it properly in ps. Unfortunately we haven't.
Andi Kleen7bf36bb2006-04-07 19:50:00 +0200391 *
392 * When user can change the frames always force IRET. That is because
393 * it deals with uncanonical addresses better. SYSRET has trouble
394 * with them due to bugs in both AMD and Intel CPUs.
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100395 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397ENTRY(system_call)
Jan Beulich7effaa82005-09-12 18:49:24 +0200398 CFI_STARTPROC simple
Jan Beulichadf14232006-09-26 10:52:41 +0200399 CFI_SIGNAL_FRAME
Jan Beulichdffead42006-06-26 13:57:38 +0200400 CFI_DEF_CFA rsp,PDA_STACKOFFSET
Jan Beulich7effaa82005-09-12 18:49:24 +0200401 CFI_REGISTER rip,rcx
402 /*CFI_REGISTER rflags,r11*/
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100403 SWAPGS_UNSAFE_STACK
404 /*
405 * A hypervisor implementation might want to use a label
406 * after the swapgs, so that it can do the swapgs
407 * for the guest and jump here on syscall.
408 */
409ENTRY(system_call_after_swapgs)
410
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100411 movq %rsp,%gs:pda_oldrsp
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 movq %gs:pda_kernelstack,%rsp
Ingo Molnar2601e642006-07-03 00:24:45 -0700413 /*
414 * No need to follow this irqs off/on section - it's straight
415 * and short:
416 */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100417 ENABLE_INTERRUPTS(CLBR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 SAVE_ARGS 8,1
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100419 movq %rax,ORIG_RAX-ARGOFFSET(%rsp)
Jan Beulich7effaa82005-09-12 18:49:24 +0200420 movq %rcx,RIP-ARGOFFSET(%rsp)
421 CFI_REL_OFFSET rip,RIP-ARGOFFSET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 GET_THREAD_INFO(%rcx)
Roland McGrathd4d67152008-07-09 02:38:07 -0700423 testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags(%rcx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 jnz tracesys
Roland McGrath86a1c342008-06-23 15:37:04 -0700425system_call_fastpath:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 cmpq $__NR_syscall_max,%rax
427 ja badsys
428 movq %r10,%rcx
429 call *sys_call_table(,%rax,8) # XXX: rip relative
430 movq %rax,RAX-ARGOFFSET(%rsp)
431/*
432 * Syscall return path ending with SYSRET (fast path)
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100433 * Has incomplete stack frame and undefined top of stack.
434 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435ret_from_sys_call:
Andi Kleen11b854b2005-04-16 15:25:02 -0700436 movl $_TIF_ALLWORK_MASK,%edi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 /* edi: flagmask */
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100438sysret_check:
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200439 LOCKDEP_SYS_EXIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 GET_THREAD_INFO(%rcx)
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100441 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700442 TRACE_IRQS_OFF
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300443 movl TI_flags(%rcx),%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 andl %edi,%edx
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100445 jnz sysret_careful
Jan Beulichbcddc012006-12-07 02:14:02 +0100446 CFI_REMEMBER_STATE
Ingo Molnar2601e642006-07-03 00:24:45 -0700447 /*
448 * sysretq will re-enable interrupts:
449 */
450 TRACE_IRQS_ON
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 movq RIP-ARGOFFSET(%rsp),%rcx
Jan Beulich7effaa82005-09-12 18:49:24 +0200452 CFI_REGISTER rip,rcx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 RESTORE_ARGS 0,-ARG_SKIP,1
Jan Beulich7effaa82005-09-12 18:49:24 +0200454 /*CFI_REGISTER rflags,r11*/
Jeremy Fitzhardingec7245da2008-06-25 00:19:27 -0400455 movq %gs:pda_oldrsp, %rsp
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -0400456 USERGS_SYSRET64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Jan Beulichbcddc012006-12-07 02:14:02 +0100458 CFI_RESTORE_STATE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 /* Handle reschedules */
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100460 /* edx: work, edi: workmask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461sysret_careful:
462 bt $TIF_NEED_RESCHED,%edx
463 jnc sysret_signal
Ingo Molnar2601e642006-07-03 00:24:45 -0700464 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100465 ENABLE_INTERRUPTS(CLBR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 pushq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200467 CFI_ADJUST_CFA_OFFSET 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 call schedule
469 popq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200470 CFI_ADJUST_CFA_OFFSET -8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 jmp sysret_check
472
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100473 /* Handle a signal */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474sysret_signal:
Ingo Molnar2601e642006-07-03 00:24:45 -0700475 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100476 ENABLE_INTERRUPTS(CLBR_NONE)
Roland McGrath86a1c342008-06-23 15:37:04 -0700477#ifdef CONFIG_AUDITSYSCALL
478 bt $TIF_SYSCALL_AUDIT,%edx
479 jc sysret_audit
480#endif
Andi Kleen10ffdbb2005-05-16 21:53:19 -0700481 /* edx: work flags (arg3) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 leaq do_notify_resume(%rip),%rax
483 leaq -ARGOFFSET(%rsp),%rdi # &pt_regs -> arg1
484 xorl %esi,%esi # oldset -> arg2
485 call ptregscall_common
Roland McGrath15e8f342008-06-23 20:41:12 -0700486 movl $_TIF_WORK_MASK,%edi
Andi Kleen7bf36bb2006-04-07 19:50:00 +0200487 /* Use IRET because user could have changed frame. This
488 works because ptregscall_common has called FIXUP_TOP_OF_STACK. */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100489 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700490 TRACE_IRQS_OFF
Andi Kleen7bf36bb2006-04-07 19:50:00 +0200491 jmp int_with_check
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100492
Jan Beulich7effaa82005-09-12 18:49:24 +0200493badsys:
494 movq $-ENOSYS,RAX-ARGOFFSET(%rsp)
495 jmp ret_from_sys_call
496
Roland McGrath86a1c342008-06-23 15:37:04 -0700497#ifdef CONFIG_AUDITSYSCALL
498 /*
499 * Fast path for syscall audit without full syscall trace.
500 * We just call audit_syscall_entry() directly, and then
501 * jump back to the normal fast path.
502 */
503auditsys:
504 movq %r10,%r9 /* 6th arg: 4th syscall arg */
505 movq %rdx,%r8 /* 5th arg: 3rd syscall arg */
506 movq %rsi,%rcx /* 4th arg: 2nd syscall arg */
507 movq %rdi,%rdx /* 3rd arg: 1st syscall arg */
508 movq %rax,%rsi /* 2nd arg: syscall number */
509 movl $AUDIT_ARCH_X86_64,%edi /* 1st arg: audit arch */
510 call audit_syscall_entry
511 LOAD_ARGS 0 /* reload call-clobbered registers */
512 jmp system_call_fastpath
513
514 /*
515 * Return fast path for syscall audit. Call audit_syscall_exit()
516 * directly and then jump back to the fast path with TIF_SYSCALL_AUDIT
517 * masked off.
518 */
519sysret_audit:
520 movq %rax,%rsi /* second arg, syscall return value */
521 cmpq $0,%rax /* is it < 0? */
522 setl %al /* 1 if so, 0 if not */
523 movzbl %al,%edi /* zero-extend that into %edi */
524 inc %edi /* first arg, 0->1(AUDITSC_SUCCESS), 1->2(AUDITSC_FAILURE) */
525 call audit_syscall_exit
526 movl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT),%edi
527 jmp sysret_check
528#endif /* CONFIG_AUDITSYSCALL */
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 /* Do syscall tracing */
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100531tracesys:
Roland McGrath86a1c342008-06-23 15:37:04 -0700532#ifdef CONFIG_AUDITSYSCALL
533 testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT),TI_flags(%rcx)
534 jz auditsys
535#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 SAVE_REST
Roland McGratha31f8dd2008-03-16 21:59:11 -0700537 movq $-ENOSYS,RAX(%rsp) /* ptrace can change this for a bad syscall */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 FIXUP_TOP_OF_STACK %rdi
539 movq %rsp,%rdi
540 call syscall_trace_enter
Roland McGrathd4d67152008-07-09 02:38:07 -0700541 /*
542 * Reload arg registers from stack in case ptrace changed them.
543 * We don't reload %rax because syscall_trace_enter() returned
544 * the value it wants us to use in the table lookup.
545 */
546 LOAD_ARGS ARGOFFSET, 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 RESTORE_REST
548 cmpq $__NR_syscall_max,%rax
Roland McGratha31f8dd2008-03-16 21:59:11 -0700549 ja int_ret_from_sys_call /* RAX(%rsp) set to -ENOSYS above */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 movq %r10,%rcx /* fixup for C */
551 call *sys_call_table(,%rax,8)
Roland McGratha31f8dd2008-03-16 21:59:11 -0700552 movq %rax,RAX-ARGOFFSET(%rsp)
Andi Kleen7bf36bb2006-04-07 19:50:00 +0200553 /* Use IRET because user could have changed frame */
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100554
555/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 * Syscall return path ending with IRET.
557 * Has correct top of stack, but partial stack frame.
Jan Beulichbcddc012006-12-07 02:14:02 +0100558 */
559 .globl int_ret_from_sys_call
Roland McGrath5cbf1562008-06-24 01:13:31 -0700560 .globl int_with_check
Jan Beulichbcddc012006-12-07 02:14:02 +0100561int_ret_from_sys_call:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100562 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700563 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 testl $3,CS-ARGOFFSET(%rsp)
565 je retint_restore_args
566 movl $_TIF_ALLWORK_MASK,%edi
567 /* edi: mask to check */
568int_with_check:
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200569 LOCKDEP_SYS_EXIT_IRQ
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 GET_THREAD_INFO(%rcx)
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300571 movl TI_flags(%rcx),%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 andl %edi,%edx
573 jnz int_careful
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300574 andl $~TS_COMPAT,TI_status(%rcx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 jmp retint_swapgs
576
577 /* Either reschedule or signal or syscall exit tracking needed. */
578 /* First do a reschedule test. */
579 /* edx: work, edi: workmask */
580int_careful:
581 bt $TIF_NEED_RESCHED,%edx
582 jnc int_very_careful
Ingo Molnar2601e642006-07-03 00:24:45 -0700583 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100584 ENABLE_INTERRUPTS(CLBR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 pushq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200586 CFI_ADJUST_CFA_OFFSET 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 call schedule
588 popq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200589 CFI_ADJUST_CFA_OFFSET -8
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100590 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700591 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 jmp int_with_check
593
594 /* handle signals and tracing -- both require a full stack frame */
595int_very_careful:
Ingo Molnar2601e642006-07-03 00:24:45 -0700596 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100597 ENABLE_INTERRUPTS(CLBR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 SAVE_REST
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100599 /* Check for syscall exit trace */
Roland McGrathd4d67152008-07-09 02:38:07 -0700600 testl $_TIF_WORK_SYSCALL_EXIT,%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 jz int_signal
602 pushq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200603 CFI_ADJUST_CFA_OFFSET 8
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100604 leaq 8(%rsp),%rdi # &ptregs -> arg1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 call syscall_trace_leave
606 popq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200607 CFI_ADJUST_CFA_OFFSET -8
Roland McGrathd4d67152008-07-09 02:38:07 -0700608 andl $~(_TIF_WORK_SYSCALL_EXIT|_TIF_SYSCALL_EMU),%edi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 jmp int_restore_rest
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611int_signal:
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100612 testl $_TIF_DO_NOTIFY_MASK,%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 jz 1f
614 movq %rsp,%rdi # &ptregs -> arg1
615 xorl %esi,%esi # oldset -> arg2
616 call do_notify_resume
Roland McGratheca91e72008-07-10 14:50:39 -07006171: movl $_TIF_WORK_MASK,%edi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618int_restore_rest:
619 RESTORE_REST
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100620 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700621 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 jmp int_with_check
623 CFI_ENDPROC
Jan Beulichbcddc012006-12-07 02:14:02 +0100624END(system_call)
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100625
626/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 * Certain special system calls that need to save a complete full stack frame.
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100628 */
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 .macro PTREGSCALL label,func,arg
631 .globl \label
632\label:
633 leaq \func(%rip),%rax
634 leaq -ARGOFFSET+8(%rsp),\arg /* 8 for return address */
635 jmp ptregscall_common
Jan Beulich4b787e02006-06-26 13:56:55 +0200636END(\label)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 .endm
638
Jan Beulich7effaa82005-09-12 18:49:24 +0200639 CFI_STARTPROC
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 PTREGSCALL stub_clone, sys_clone, %r8
642 PTREGSCALL stub_fork, sys_fork, %rdi
643 PTREGSCALL stub_vfork, sys_vfork, %rdi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 PTREGSCALL stub_sigaltstack, sys_sigaltstack, %rdx
645 PTREGSCALL stub_iopl, sys_iopl, %rsi
646
647ENTRY(ptregscall_common)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 popq %r11
Jan Beulich7effaa82005-09-12 18:49:24 +0200649 CFI_ADJUST_CFA_OFFSET -8
650 CFI_REGISTER rip, r11
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 SAVE_REST
652 movq %r11, %r15
Jan Beulich7effaa82005-09-12 18:49:24 +0200653 CFI_REGISTER rip, r15
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 FIXUP_TOP_OF_STACK %r11
655 call *%rax
656 RESTORE_TOP_OF_STACK %r11
657 movq %r15, %r11
Jan Beulich7effaa82005-09-12 18:49:24 +0200658 CFI_REGISTER rip, r11
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 RESTORE_REST
660 pushq %r11
Jan Beulich7effaa82005-09-12 18:49:24 +0200661 CFI_ADJUST_CFA_OFFSET 8
662 CFI_REL_OFFSET rip, 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 ret
664 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200665END(ptregscall_common)
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667ENTRY(stub_execve)
668 CFI_STARTPROC
669 popq %r11
Jan Beulich7effaa82005-09-12 18:49:24 +0200670 CFI_ADJUST_CFA_OFFSET -8
671 CFI_REGISTER rip, r11
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 SAVE_REST
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 FIXUP_TOP_OF_STACK %r11
Ingo Molnar5d119b22008-02-26 12:55:57 +0100674 movq %rsp, %rcx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 call sys_execve
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 RESTORE_TOP_OF_STACK %r11
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 movq %rax,RAX(%rsp)
678 RESTORE_REST
679 jmp int_ret_from_sys_call
680 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200681END(stub_execve)
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683/*
684 * sigreturn is special because it needs to restore all registers on return.
685 * This cannot be done with SYSRET, so use the IRET return path instead.
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100686 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687ENTRY(stub_rt_sigreturn)
688 CFI_STARTPROC
Jan Beulich7effaa82005-09-12 18:49:24 +0200689 addq $8, %rsp
690 CFI_ADJUST_CFA_OFFSET -8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 SAVE_REST
692 movq %rsp,%rdi
693 FIXUP_TOP_OF_STACK %r11
694 call sys_rt_sigreturn
695 movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer
696 RESTORE_REST
697 jmp int_ret_from_sys_call
698 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200699END(stub_rt_sigreturn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Jan Beulich7effaa82005-09-12 18:49:24 +0200701/*
H. Peter Anvin939b7872008-11-11 13:51:52 -0800702 * Build the entry stubs and pointer table with some assembler magic.
703 * We pack 7 stubs into a single 32-byte chunk, which will fit in a
704 * single cache line on all modern x86 implementations.
705 */
706 .section .init.rodata,"a"
707ENTRY(interrupt)
708 .text
709 .p2align 5
710 .p2align CONFIG_X86_L1_CACHE_SHIFT
711ENTRY(irq_entries_start)
712 INTR_FRAME
713vector=FIRST_EXTERNAL_VECTOR
714.rept (NR_VECTORS-FIRST_EXTERNAL_VECTOR+6)/7
715 .balign 32
716 .rept 7
717 .if vector < NR_VECTORS
H. Peter Anvin86655962008-11-12 10:27:35 -0800718 .if vector <> FIRST_EXTERNAL_VECTOR
H. Peter Anvin939b7872008-11-11 13:51:52 -0800719 CFI_ADJUST_CFA_OFFSET -8
720 .endif
7211: pushq $(~vector+0x80) /* Note: always in signed byte range */
722 CFI_ADJUST_CFA_OFFSET 8
H. Peter Anvin86655962008-11-12 10:27:35 -0800723 .if ((vector-FIRST_EXTERNAL_VECTOR)%7) <> 6
H. Peter Anvin939b7872008-11-11 13:51:52 -0800724 jmp 2f
725 .endif
726 .previous
727 .quad 1b
728 .text
729vector=vector+1
730 .endif
731 .endr
7322: jmp common_interrupt
733.endr
734 CFI_ENDPROC
735END(irq_entries_start)
736
737.previous
738END(interrupt)
739.previous
740
Alexander van Heukelumd99015b2008-11-19 01:18:11 +0100741/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 * Interrupt entry/exit.
743 *
744 * Interrupt entry points save only callee clobbered registers in fast path.
Alexander van Heukelumd99015b2008-11-19 01:18:11 +0100745 *
746 * Entry runs with interrupts off.
747 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Alexander van Heukelum722024d2008-11-13 13:50:20 +0100749/* 0(%rsp): ~(interrupt number) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 .macro interrupt func
Alexander van Heukelumd99015b2008-11-19 01:18:11 +0100751 subq $10*8, %rsp
752 CFI_ADJUST_CFA_OFFSET 10*8
753 call save_args
Alexander van Heukelumdcd072e2008-11-20 14:40:11 +0100754 PARTIAL_FRAME 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 call \func
756 .endm
757
Alexander van Heukelum722024d2008-11-13 13:50:20 +0100758 /*
759 * The interrupt stubs push (~vector+0x80) onto the stack and
760 * then jump to common_interrupt.
761 */
H. Peter Anvin939b7872008-11-11 13:51:52 -0800762 .p2align CONFIG_X86_L1_CACHE_SHIFT
763common_interrupt:
Jan Beulich7effaa82005-09-12 18:49:24 +0200764 XCPT_FRAME
Alexander van Heukelum722024d2008-11-13 13:50:20 +0100765 addq $-0x80,(%rsp) /* Adjust vector to [-256,-1] range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 interrupt do_IRQ
767 /* 0(%rsp): oldrsp-ARGOFFSET */
Jan Beulich7effaa82005-09-12 18:49:24 +0200768ret_from_intr:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100769 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700770 TRACE_IRQS_OFF
Andi Kleen3829ee62005-07-28 21:15:48 -0700771 decl %gs:pda_irqcount
Jan Beulich1de9c3f2006-06-26 13:57:35 +0200772 leaveq
Jan Beulich7effaa82005-09-12 18:49:24 +0200773 CFI_DEF_CFA_REGISTER rsp
Jan Beulich1de9c3f2006-06-26 13:57:35 +0200774 CFI_ADJUST_CFA_OFFSET -8
Jan Beulich7effaa82005-09-12 18:49:24 +0200775exit_intr:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 GET_THREAD_INFO(%rcx)
777 testl $3,CS-ARGOFFSET(%rsp)
778 je retint_kernel
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 /* Interrupt came from user space */
781 /*
782 * Has a correct top of stack, but a partial stack frame
783 * %rcx: thread info. Interrupts off.
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100784 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785retint_with_reschedule:
786 movl $_TIF_WORK_MASK,%edi
Jan Beulich7effaa82005-09-12 18:49:24 +0200787retint_check:
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200788 LOCKDEP_SYS_EXIT_IRQ
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300789 movl TI_flags(%rcx),%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 andl %edi,%edx
Jan Beulich7effaa82005-09-12 18:49:24 +0200791 CFI_REMEMBER_STATE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 jnz retint_careful
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200793
794retint_swapgs: /* return to user-space */
Ingo Molnar2601e642006-07-03 00:24:45 -0700795 /*
796 * The iretq could re-enable interrupts:
797 */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100798 DISABLE_INTERRUPTS(CLBR_ANY)
Ingo Molnar2601e642006-07-03 00:24:45 -0700799 TRACE_IRQS_IRETQ
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100800 SWAPGS
Ingo Molnar2601e642006-07-03 00:24:45 -0700801 jmp restore_args
802
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200803retint_restore_args: /* return to kernel space */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100804 DISABLE_INTERRUPTS(CLBR_ANY)
Ingo Molnar2601e642006-07-03 00:24:45 -0700805 /*
806 * The iretq could re-enable interrupts:
807 */
808 TRACE_IRQS_IRETQ
809restore_args:
Ingo Molnar3701d8632008-02-09 23:24:08 +0100810 RESTORE_ARGS 0,8,0
811
Adrian Bunkf7f3d792008-02-13 23:29:53 +0200812irq_return:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100813 INTERRUPT_RETURN
Ingo Molnar3701d8632008-02-09 23:24:08 +0100814
815 .section __ex_table, "a"
816 .quad irq_return, bad_iret
817 .previous
818
819#ifdef CONFIG_PARAVIRT
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100820ENTRY(native_iret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 iretq
822
823 .section __ex_table,"a"
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100824 .quad native_iret, bad_iret
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 .previous
Ingo Molnar3701d8632008-02-09 23:24:08 +0100826#endif
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 .section .fixup,"ax"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829bad_iret:
Roland McGrath3aa4b372008-02-06 22:39:43 +0100830 /*
831 * The iret traps when the %cs or %ss being restored is bogus.
832 * We've lost the original trap vector and error code.
833 * #GPF is the most likely one to get for an invalid selector.
834 * So pretend we completed the iret and took the #GPF in user mode.
835 *
836 * We are now running with the kernel GS after exception recovery.
837 * But error_entry expects us to have user GS to match the user %cs,
838 * so swap back.
839 */
840 pushq $0
841
842 SWAPGS
843 jmp general_protection
844
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100845 .previous
846
Jan Beulich7effaa82005-09-12 18:49:24 +0200847 /* edi: workmask, edx: work */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848retint_careful:
Jan Beulich7effaa82005-09-12 18:49:24 +0200849 CFI_RESTORE_STATE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 bt $TIF_NEED_RESCHED,%edx
851 jnc retint_signal
Ingo Molnar2601e642006-07-03 00:24:45 -0700852 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100853 ENABLE_INTERRUPTS(CLBR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 pushq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200855 CFI_ADJUST_CFA_OFFSET 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 call schedule
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100857 popq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200858 CFI_ADJUST_CFA_OFFSET -8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 GET_THREAD_INFO(%rcx)
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100860 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700861 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 jmp retint_check
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864retint_signal:
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100865 testl $_TIF_DO_NOTIFY_MASK,%edx
Andi Kleen10ffdbb2005-05-16 21:53:19 -0700866 jz retint_swapgs
Ingo Molnar2601e642006-07-03 00:24:45 -0700867 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100868 ENABLE_INTERRUPTS(CLBR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 SAVE_REST
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100870 movq $-1,ORIG_RAX(%rsp)
Andi Kleen3829ee62005-07-28 21:15:48 -0700871 xorl %esi,%esi # oldset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 movq %rsp,%rdi # &pt_regs
873 call do_notify_resume
874 RESTORE_REST
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100875 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700876 TRACE_IRQS_OFF
Andi Kleenbe9e6872005-05-01 08:58:51 -0700877 GET_THREAD_INFO(%rcx)
Roland McGratheca91e72008-07-10 14:50:39 -0700878 jmp retint_with_reschedule
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880#ifdef CONFIG_PREEMPT
881 /* Returning to kernel space. Check if we need preemption */
882 /* rcx: threadinfo. interrupts off. */
Andi Kleenb06baba2006-09-26 10:52:29 +0200883ENTRY(retint_kernel)
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300884 cmpl $0,TI_preempt_count(%rcx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 jnz retint_restore_args
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300886 bt $TIF_NEED_RESCHED,TI_flags(%rcx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 jnc retint_restore_args
888 bt $9,EFLAGS-ARGOFFSET(%rsp) /* interrupts off? */
889 jnc retint_restore_args
890 call preempt_schedule_irq
891 jmp exit_intr
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100892#endif
Jan Beulich4b787e02006-06-26 13:56:55 +0200893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200895END(common_interrupt)
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897/*
898 * APIC interrupts.
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100899 */
Alexander van Heukelumd99015b2008-11-19 01:18:11 +0100900 .p2align 5
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 .macro apicinterrupt num,func
Jan Beulich7effaa82005-09-12 18:49:24 +0200903 INTR_FRAME
Rusty Russell19eadf92006-06-27 02:53:44 -0700904 pushq $~(\num)
Jan Beulich7effaa82005-09-12 18:49:24 +0200905 CFI_ADJUST_CFA_OFFSET 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 interrupt \func
907 jmp ret_from_intr
908 CFI_ENDPROC
909 .endm
910
911ENTRY(thermal_interrupt)
912 apicinterrupt THERMAL_APIC_VECTOR,smp_thermal_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200913END(thermal_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Jacob Shin89b831e2005-11-05 17:25:53 +0100915ENTRY(threshold_interrupt)
916 apicinterrupt THRESHOLD_APIC_VECTOR,mce_threshold_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200917END(threshold_interrupt)
Jacob Shin89b831e2005-11-05 17:25:53 +0100918
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100919#ifdef CONFIG_SMP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920ENTRY(reschedule_interrupt)
921 apicinterrupt RESCHEDULE_VECTOR,smp_reschedule_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200922END(reschedule_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Andi Kleene5bc8b62005-09-12 18:49:24 +0200924 .macro INVALIDATE_ENTRY num
925ENTRY(invalidate_interrupt\num)
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100926 apicinterrupt INVALIDATE_TLB_VECTOR_START+\num,smp_invalidate_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200927END(invalidate_interrupt\num)
Andi Kleene5bc8b62005-09-12 18:49:24 +0200928 .endm
929
930 INVALIDATE_ENTRY 0
931 INVALIDATE_ENTRY 1
932 INVALIDATE_ENTRY 2
933 INVALIDATE_ENTRY 3
934 INVALIDATE_ENTRY 4
935 INVALIDATE_ENTRY 5
936 INVALIDATE_ENTRY 6
937 INVALIDATE_ENTRY 7
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939ENTRY(call_function_interrupt)
940 apicinterrupt CALL_FUNCTION_VECTOR,smp_call_function_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200941END(call_function_interrupt)
Jens Axboe3b16cf82008-06-26 11:21:54 +0200942ENTRY(call_function_single_interrupt)
943 apicinterrupt CALL_FUNCTION_SINGLE_VECTOR,smp_call_function_single_interrupt
944END(call_function_single_interrupt)
Eric W. Biederman61014292007-02-23 04:40:58 -0700945ENTRY(irq_move_cleanup_interrupt)
946 apicinterrupt IRQ_MOVE_CLEANUP_VECTOR,smp_irq_move_cleanup_interrupt
947END(irq_move_cleanup_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948#endif
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950ENTRY(apic_timer_interrupt)
951 apicinterrupt LOCAL_TIMER_VECTOR,smp_apic_timer_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200952END(apic_timer_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Cliff Wickman18129242008-06-02 08:56:14 -0500954ENTRY(uv_bau_message_intr1)
955 apicinterrupt 220,uv_bau_message_interrupt
956END(uv_bau_message_intr1)
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958ENTRY(error_interrupt)
959 apicinterrupt ERROR_APIC_VECTOR,smp_error_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200960END(error_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962ENTRY(spurious_interrupt)
963 apicinterrupt SPURIOUS_APIC_VECTOR,smp_spurious_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200964END(spurious_interrupt)
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966/*
967 * Exception entry points.
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100968 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 .macro zeroentry sym
Jan Beulich7effaa82005-09-12 18:49:24 +0200970 INTR_FRAME
Jeremy Fitzhardingefab58422008-06-25 00:19:31 -0400971 PARAVIRT_ADJUST_EXCEPTION_FRAME
Ingo Molnar14ae22b2008-11-21 15:20:47 +0100972 pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */
Alexander van Heukelumd99015b2008-11-19 01:18:11 +0100973 subq $15*8,%rsp
974 CFI_ADJUST_CFA_OFFSET 15*8
975 call error_entry
Alexander van Heukelumdcd072e2008-11-20 14:40:11 +0100976 DEFAULT_FRAME 0
Alexander van Heukelumd99015b2008-11-19 01:18:11 +0100977 movq %rsp,%rdi /* pt_regs pointer */
978 xorl %esi,%esi /* no error code */
979 call \sym
980 jmp error_exit /* %ebx: no swapgs flag */
Jan Beulich7effaa82005-09-12 18:49:24 +0200981 CFI_ENDPROC
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +0100982 .endm
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 .macro errorentry sym
Jan Beulich7effaa82005-09-12 18:49:24 +0200985 XCPT_FRAME
Jeremy Fitzhardingefab58422008-06-25 00:19:31 -0400986 PARAVIRT_ADJUST_EXCEPTION_FRAME
Alexander van Heukelumd99015b2008-11-19 01:18:11 +0100987 subq $15*8,%rsp
988 CFI_ADJUST_CFA_OFFSET 15*8
989 call error_entry
Alexander van Heukelumdcd072e2008-11-20 14:40:11 +0100990 DEFAULT_FRAME 0
Alexander van Heukelumd99015b2008-11-19 01:18:11 +0100991 movq %rsp,%rdi /* pt_regs pointer */
992 movq ORIG_RAX(%rsp),%rsi /* get error code */
993 movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */
994 call \sym
995 jmp error_exit /* %ebx: no swapgs flag */
Jan Beulich7effaa82005-09-12 18:49:24 +0200996 CFI_ENDPROC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 .endm
998
999 /* error code is on the stack already */
1000 /* handle NMI like exceptions that can happen everywhere */
Ingo Molnar2601e642006-07-03 00:24:45 -07001001 .macro paranoidentry sym, ist=0, irqtrace=1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 SAVE_ALL
1003 cld
1004 movl $1,%ebx
1005 movl $MSR_GS_BASE,%ecx
1006 rdmsr
1007 testl %edx,%edx
1008 js 1f
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001009 SWAPGS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 xorl %ebx,%ebx
Jan Beulichb556b352006-01-11 22:43:00 +010010111:
1012 .if \ist
1013 movq %gs:pda_data_offset, %rbp
1014 .endif
Alexander van Heukelum7e61a792008-09-26 14:03:03 +02001015 .if \irqtrace
1016 TRACE_IRQS_OFF
1017 .endif
Jan Beulichb556b352006-01-11 22:43:00 +01001018 movq %rsp,%rdi
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 movq ORIG_RAX(%rsp),%rsi
1020 movq $-1,ORIG_RAX(%rsp)
Jan Beulichb556b352006-01-11 22:43:00 +01001021 .if \ist
Andi Kleen5f8efbb2006-01-16 01:56:39 +01001022 subq $EXCEPTION_STKSZ, per_cpu__init_tss + TSS_ist + (\ist - 1) * 8(%rbp)
Jan Beulichb556b352006-01-11 22:43:00 +01001023 .endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 call \sym
Jan Beulichb556b352006-01-11 22:43:00 +01001025 .if \ist
Andi Kleen5f8efbb2006-01-16 01:56:39 +01001026 addq $EXCEPTION_STKSZ, per_cpu__init_tss + TSS_ist + (\ist - 1) * 8(%rbp)
Jan Beulichb556b352006-01-11 22:43:00 +01001027 .endif
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001028 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -07001029 .if \irqtrace
1030 TRACE_IRQS_OFF
1031 .endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 .endm
Ingo Molnar2601e642006-07-03 00:24:45 -07001033
1034 /*
1035 * "Paranoid" exit path from exception stack.
1036 * Paranoid because this is used by NMIs and cannot take
1037 * any kernel state for granted.
1038 * We don't do kernel preemption checks here, because only
1039 * NMI should be common and it does not enable IRQs and
1040 * cannot get reschedule ticks.
1041 *
1042 * "trace" is 0 for the NMI handler only, because irq-tracing
1043 * is fundamentally NMI-unsafe. (we cannot change the soft and
1044 * hard flags at once, atomically)
1045 */
1046 .macro paranoidexit trace=1
1047 /* ebx: no swapgs flag */
1048paranoid_exit\trace:
1049 testl %ebx,%ebx /* swapgs needed? */
1050 jnz paranoid_restore\trace
1051 testl $3,CS(%rsp)
1052 jnz paranoid_userspace\trace
1053paranoid_swapgs\trace:
Andi Kleen7a0a2df2006-09-26 10:52:37 +02001054 .if \trace
Ingo Molnar2601e642006-07-03 00:24:45 -07001055 TRACE_IRQS_IRETQ 0
Andi Kleen7a0a2df2006-09-26 10:52:37 +02001056 .endif
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001057 SWAPGS_UNSAFE_STACK
Ingo Molnar2601e642006-07-03 00:24:45 -07001058paranoid_restore\trace:
1059 RESTORE_ALL 8
Ingo Molnar3701d8632008-02-09 23:24:08 +01001060 jmp irq_return
Ingo Molnar2601e642006-07-03 00:24:45 -07001061paranoid_userspace\trace:
1062 GET_THREAD_INFO(%rcx)
Glauber Costa26ccb8a2008-06-24 11:19:35 -03001063 movl TI_flags(%rcx),%ebx
Ingo Molnar2601e642006-07-03 00:24:45 -07001064 andl $_TIF_WORK_MASK,%ebx
1065 jz paranoid_swapgs\trace
1066 movq %rsp,%rdi /* &pt_regs */
1067 call sync_regs
1068 movq %rax,%rsp /* switch stack for scheduling */
1069 testl $_TIF_NEED_RESCHED,%ebx
1070 jnz paranoid_schedule\trace
1071 movl %ebx,%edx /* arg3: thread flags */
1072 .if \trace
1073 TRACE_IRQS_ON
1074 .endif
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001075 ENABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -07001076 xorl %esi,%esi /* arg2: oldset */
1077 movq %rsp,%rdi /* arg1: &pt_regs */
1078 call do_notify_resume
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001079 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -07001080 .if \trace
1081 TRACE_IRQS_OFF
1082 .endif
1083 jmp paranoid_userspace\trace
1084paranoid_schedule\trace:
1085 .if \trace
1086 TRACE_IRQS_ON
1087 .endif
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001088 ENABLE_INTERRUPTS(CLBR_ANY)
Ingo Molnar2601e642006-07-03 00:24:45 -07001089 call schedule
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001090 DISABLE_INTERRUPTS(CLBR_ANY)
Ingo Molnar2601e642006-07-03 00:24:45 -07001091 .if \trace
1092 TRACE_IRQS_OFF
1093 .endif
1094 jmp paranoid_userspace\trace
1095 CFI_ENDPROC
1096 .endm
1097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098/*
Alexander van Heukelumd99015b2008-11-19 01:18:11 +01001099 * Exception entry point. This expects an error code/orig_rax on the stack.
1100 * returns in "no swapgs flag" in %ebx.
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +01001101 */
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001102KPROBE_ENTRY(error_entry)
Alexander van Heukelumdcd072e2008-11-20 14:40:11 +01001103 XCPT_FRAME
Alexander van Heukelumd99015b2008-11-19 01:18:11 +01001104 CFI_ADJUST_CFA_OFFSET 15*8
1105 /* oldrax contains error code */
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +01001106 cld
Ingo Molnar14ae22b2008-11-21 15:20:47 +01001107 movq_cfi rdi, RDI+8
1108 movq_cfi rsi, RSI+8
1109 movq_cfi rdx, RDX+8
1110 movq_cfi rcx, RCX+8
1111 movq_cfi rax, RAX+8
1112 movq_cfi r8, R8+8
1113 movq_cfi r9, R9+8
1114 movq_cfi r10, R10+8
1115 movq_cfi r11, R11+8
1116 movq_cfi rbx, RBX+8
1117 movq_cfi rbp, RBP+8
1118 movq_cfi r12, R12+8
1119 movq_cfi r13, R13+8
1120 movq_cfi r14, R14+8
1121 movq_cfi r15, R15+8
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +01001122 xorl %ebx,%ebx
Alexander van Heukelumd99015b2008-11-19 01:18:11 +01001123 testl $3,CS+8(%rsp)
1124 je error_kernelspace
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +01001125error_swapgs:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001126 SWAPGS
Alexander van Heukelum6b11d4e2008-09-26 14:03:02 +02001127error_sti:
1128 TRACE_IRQS_OFF
Alexander van Heukelumd99015b2008-11-19 01:18:11 +01001129 ret
1130 CFI_ENDPROC
1131
1132/*
1133 * There are two places in the kernel that can potentially fault with
1134 * usergs. Handle them here. The exception handlers after iret run with
1135 * kernel gs again, so don't set the user space flag. B stepping K8s
1136 * sometimes report an truncated RIP for IRET exceptions returning to
1137 * compat mode. Check for these here too.
1138 */
1139error_kernelspace:
1140 incl %ebx
1141 leaq irq_return(%rip),%rcx
1142 cmpq %rcx,RIP+8(%rsp)
1143 je error_swapgs
1144 movl %ecx,%ecx /* zero extend */
1145 cmpq %rcx,RIP+8(%rsp)
1146 je error_swapgs
1147 cmpq $gs_change,RIP+8(%rsp)
1148 je error_swapgs
1149 jmp error_sti
1150KPROBE_END(error_entry)
1151
1152
1153/* ebx: no swapgs flag (1: don't need swapgs, 0: need it) */
1154KPROBE_ENTRY(error_exit)
Alexander van Heukelumdcd072e2008-11-20 14:40:11 +01001155 DEFAULT_FRAME
Peter Zijlstra10cd7062007-10-11 22:11:12 +02001156 movl %ebx,%eax
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 RESTORE_REST
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001158 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -07001159 TRACE_IRQS_OFF
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +01001160 GET_THREAD_INFO(%rcx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 testl %eax,%eax
Alexander van Heukelumd99015b2008-11-19 01:18:11 +01001162 jne retint_kernel
Peter Zijlstra10cd7062007-10-11 22:11:12 +02001163 LOCKDEP_SYS_EXIT_IRQ
Alexander van Heukelumd99015b2008-11-19 01:18:11 +01001164 movl TI_flags(%rcx),%edx
1165 movl $_TIF_WORK_MASK,%edi
1166 andl %edi,%edx
1167 jnz retint_careful
Peter Zijlstra10cd7062007-10-11 22:11:12 +02001168 jmp retint_swapgs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 CFI_ENDPROC
Alexander van Heukelumd99015b2008-11-19 01:18:11 +01001170KPROBE_END(error_exit)
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +01001171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 /* Reload gs selector with exception handling */
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +01001173 /* edi: new selector */
Jeremy Fitzhardinge9f9d4892008-06-25 00:19:32 -04001174ENTRY(native_load_gs_index)
Jan Beulich7effaa82005-09-12 18:49:24 +02001175 CFI_STARTPROC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 pushf
Jan Beulich7effaa82005-09-12 18:49:24 +02001177 CFI_ADJUST_CFA_OFFSET 8
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001178 DISABLE_INTERRUPTS(CLBR_ANY | ~(CLBR_RDI))
1179 SWAPGS
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +01001180gs_change:
1181 movl %edi,%gs
Linus Torvalds1da177e2005-04-16 15:20:36 -070011822: mfence /* workaround */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001183 SWAPGS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 popf
Jan Beulich7effaa82005-09-12 18:49:24 +02001185 CFI_ADJUST_CFA_OFFSET -8
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 ret
Jan Beulich7effaa82005-09-12 18:49:24 +02001187 CFI_ENDPROC
Jeremy Fitzhardinge9f9d4892008-06-25 00:19:32 -04001188ENDPROC(native_load_gs_index)
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +01001189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 .section __ex_table,"a"
1191 .align 8
1192 .quad gs_change,bad_gs
1193 .previous
1194 .section .fixup,"ax"
1195 /* running with kernelgs */
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +01001196bad_gs:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001197 SWAPGS /* switch back to user gs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 xorl %eax,%eax
1199 movl %eax,%gs
1200 jmp 2b
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +01001201 .previous
1202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203/*
1204 * Create a kernel thread.
1205 *
1206 * C extern interface:
1207 * extern long kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
1208 *
1209 * asm input arguments:
1210 * rdi: fn, rsi: arg, rdx: flags
1211 */
1212ENTRY(kernel_thread)
1213 CFI_STARTPROC
1214 FAKE_STACK_FRAME $child_rip
1215 SAVE_ALL
1216
1217 # rdi: flags, rsi: usp, rdx: will be &pt_regs
1218 movq %rdx,%rdi
1219 orq kernel_thread_flags(%rip),%rdi
1220 movq $-1, %rsi
1221 movq %rsp, %rdx
1222
1223 xorl %r8d,%r8d
1224 xorl %r9d,%r9d
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +01001225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 # clone now
1227 call do_fork
1228 movq %rax,RAX(%rsp)
1229 xorl %edi,%edi
1230
1231 /*
1232 * It isn't worth to check for reschedule here,
1233 * so internally to the x86_64 port you can rely on kernel_thread()
1234 * not to reschedule the child before returning, this avoids the need
1235 * of hacks for example to fork off the per-CPU idle tasks.
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +01001236 * [Hopefully no generic code relies on the reschedule -AK]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 */
1238 RESTORE_ALL
1239 UNFAKE_STACK_FRAME
1240 ret
1241 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001242ENDPROC(kernel_thread)
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +01001243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244child_rip:
Andi Kleenc05991e2006-08-30 19:37:08 +02001245 pushq $0 # fake return address
1246 CFI_STARTPROC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 /*
1248 * Here we are in the child and the registers are set as they were
1249 * at kernel_thread() invocation in the parent.
1250 */
1251 movq %rdi, %rax
1252 movq %rsi, %rdi
1253 call *%rax
1254 # exit
Andrey Mirkin1c5b5cf2007-10-17 18:04:33 +02001255 mov %eax, %edi
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 call do_exit
Andi Kleenc05991e2006-08-30 19:37:08 +02001257 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001258ENDPROC(child_rip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260/*
1261 * execve(). This function needs to use IRET, not SYSRET, to set up all state properly.
1262 *
1263 * C extern interface:
1264 * extern long execve(char *name, char **argv, char **envp)
1265 *
1266 * asm input arguments:
1267 * rdi: name, rsi: argv, rdx: envp
1268 *
1269 * We want to fallback into:
Ingo Molnar5d119b22008-02-26 12:55:57 +01001270 * extern long sys_execve(char *name, char **argv,char **envp, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 *
1272 * do_sys_execve asm fallback arguments:
Ingo Molnar5d119b22008-02-26 12:55:57 +01001273 * rdi: name, rsi: argv, rdx: envp, rcx: fake frame on the stack
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 */
Arnd Bergmann3db03b42006-10-02 02:18:31 -07001275ENTRY(kernel_execve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 CFI_STARTPROC
1277 FAKE_STACK_FRAME $0
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +01001278 SAVE_ALL
Ingo Molnar5d119b22008-02-26 12:55:57 +01001279 movq %rsp,%rcx
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 call sys_execve
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +01001281 movq %rax, RAX(%rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 RESTORE_REST
1283 testq %rax,%rax
1284 je int_ret_from_sys_call
1285 RESTORE_ARGS
1286 UNFAKE_STACK_FRAME
1287 ret
1288 CFI_ENDPROC
Arnd Bergmann3db03b42006-10-02 02:18:31 -07001289ENDPROC(kernel_execve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001291KPROBE_ENTRY(page_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 errorentry do_page_fault
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001293KPROBE_END(page_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295ENTRY(coprocessor_error)
1296 zeroentry do_coprocessor_error
Jan Beulich4b787e02006-06-26 13:56:55 +02001297END(coprocessor_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299ENTRY(simd_coprocessor_error)
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +01001300 zeroentry do_simd_coprocessor_error
Jan Beulich4b787e02006-06-26 13:56:55 +02001301END(simd_coprocessor_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303ENTRY(device_not_available)
Alexander van Heukelume407d6202008-09-30 18:41:36 +02001304 zeroentry do_device_not_available
Jan Beulich4b787e02006-06-26 13:56:55 +02001305END(device_not_available)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 /* runs on exception stack */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001308KPROBE_ENTRY(debug)
Jan Beulich7effaa82005-09-12 18:49:24 +02001309 INTR_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001310 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 pushq $0
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +01001312 CFI_ADJUST_CFA_OFFSET 8
Andi Kleen5f8efbb2006-01-16 01:56:39 +01001313 paranoidentry do_debug, DEBUG_STACK
Ingo Molnar2601e642006-07-03 00:24:45 -07001314 paranoidexit
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001315KPROBE_END(debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +01001317 /* runs on exception stack */
Andi Kleeneddb6fb2006-02-03 21:50:41 +01001318KPROBE_ENTRY(nmi)
Jan Beulich7effaa82005-09-12 18:49:24 +02001319 INTR_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001320 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 pushq $-1
Jan Beulich7effaa82005-09-12 18:49:24 +02001322 CFI_ADJUST_CFA_OFFSET 8
Ingo Molnar2601e642006-07-03 00:24:45 -07001323 paranoidentry do_nmi, 0, 0
1324#ifdef CONFIG_TRACE_IRQFLAGS
1325 paranoidexit 0
1326#else
1327 jmp paranoid_exit1
1328 CFI_ENDPROC
1329#endif
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001330KPROBE_END(nmi)
Andi Kleen6fefb0d2005-04-16 15:25:03 -07001331
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001332KPROBE_ENTRY(int3)
Jan Beulichb556b352006-01-11 22:43:00 +01001333 INTR_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001334 PARAVIRT_ADJUST_EXCEPTION_FRAME
Jan Beulichb556b352006-01-11 22:43:00 +01001335 pushq $0
1336 CFI_ADJUST_CFA_OFFSET 8
Andi Kleen5f8efbb2006-01-16 01:56:39 +01001337 paranoidentry do_int3, DEBUG_STACK
Ingo Molnar2601e642006-07-03 00:24:45 -07001338 jmp paranoid_exit1
Jan Beulichb556b352006-01-11 22:43:00 +01001339 CFI_ENDPROC
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001340KPROBE_END(int3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342ENTRY(overflow)
1343 zeroentry do_overflow
Jan Beulich4b787e02006-06-26 13:56:55 +02001344END(overflow)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346ENTRY(bounds)
1347 zeroentry do_bounds
Jan Beulich4b787e02006-06-26 13:56:55 +02001348END(bounds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350ENTRY(invalid_op)
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +01001351 zeroentry do_invalid_op
Jan Beulich4b787e02006-06-26 13:56:55 +02001352END(invalid_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354ENTRY(coprocessor_segment_overrun)
1355 zeroentry do_coprocessor_segment_overrun
Jan Beulich4b787e02006-06-26 13:56:55 +02001356END(coprocessor_segment_overrun)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 /* runs on exception stack */
1359ENTRY(double_fault)
Jan Beulich7effaa82005-09-12 18:49:24 +02001360 XCPT_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001361 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 paranoidentry do_double_fault
Ingo Molnar2601e642006-07-03 00:24:45 -07001363 jmp paranoid_exit1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001365END(double_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367ENTRY(invalid_TSS)
1368 errorentry do_invalid_TSS
Jan Beulich4b787e02006-06-26 13:56:55 +02001369END(invalid_TSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371ENTRY(segment_not_present)
1372 errorentry do_segment_not_present
Jan Beulich4b787e02006-06-26 13:56:55 +02001373END(segment_not_present)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
1375 /* runs on exception stack */
1376ENTRY(stack_segment)
Jan Beulich7effaa82005-09-12 18:49:24 +02001377 XCPT_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001378 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 paranoidentry do_stack_segment
Ingo Molnar2601e642006-07-03 00:24:45 -07001380 jmp paranoid_exit1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001382END(stack_segment)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001384KPROBE_ENTRY(general_protection)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 errorentry do_general_protection
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001386KPROBE_END(general_protection)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388ENTRY(alignment_check)
1389 errorentry do_alignment_check
Jan Beulich4b787e02006-06-26 13:56:55 +02001390END(alignment_check)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392ENTRY(divide_error)
1393 zeroentry do_divide_error
Jan Beulich4b787e02006-06-26 13:56:55 +02001394END(divide_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396ENTRY(spurious_interrupt_bug)
1397 zeroentry do_spurious_interrupt_bug
Jan Beulich4b787e02006-06-26 13:56:55 +02001398END(spurious_interrupt_bug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400#ifdef CONFIG_X86_MCE
1401 /* runs on exception stack */
1402ENTRY(machine_check)
Jan Beulich7effaa82005-09-12 18:49:24 +02001403 INTR_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001404 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 pushq $0
Alexander van Heukelum0bd7b792008-11-16 15:29:00 +01001406 CFI_ADJUST_CFA_OFFSET 8
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 paranoidentry do_machine_check
Ingo Molnar2601e642006-07-03 00:24:45 -07001408 jmp paranoid_exit1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001410END(machine_check)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411#endif
1412
Andi Kleen26995002006-08-02 22:37:28 +02001413/* Call softirq on interrupt stack. Interrupts are off. */
Andi Kleened6b6762005-07-28 21:15:49 -07001414ENTRY(call_softirq)
Jan Beulich7effaa82005-09-12 18:49:24 +02001415 CFI_STARTPROC
Andi Kleen26995002006-08-02 22:37:28 +02001416 push %rbp
1417 CFI_ADJUST_CFA_OFFSET 8
1418 CFI_REL_OFFSET rbp,0
1419 mov %rsp,%rbp
1420 CFI_DEF_CFA_REGISTER rbp
Andi Kleened6b6762005-07-28 21:15:49 -07001421 incl %gs:pda_irqcount
Andi Kleen26995002006-08-02 22:37:28 +02001422 cmove %gs:pda_irqstackptr,%rsp
1423 push %rbp # backlink for old unwinder
Andi Kleened6b6762005-07-28 21:15:49 -07001424 call __do_softirq
Andi Kleen26995002006-08-02 22:37:28 +02001425 leaveq
Jan Beulich7effaa82005-09-12 18:49:24 +02001426 CFI_DEF_CFA_REGISTER rsp
Andi Kleen26995002006-08-02 22:37:28 +02001427 CFI_ADJUST_CFA_OFFSET -8
Andi Kleened6b6762005-07-28 21:15:49 -07001428 decl %gs:pda_irqcount
Andi Kleened6b6762005-07-28 21:15:49 -07001429 ret
Jan Beulich7effaa82005-09-12 18:49:24 +02001430 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001431ENDPROC(call_softirq)
Andi Kleen75154f42007-06-23 02:29:25 +02001432
1433KPROBE_ENTRY(ignore_sysret)
1434 CFI_STARTPROC
1435 mov $-ENOSYS,%eax
1436 sysret
1437 CFI_ENDPROC
1438ENDPROC(ignore_sysret)
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001439
1440#ifdef CONFIG_XEN
1441ENTRY(xen_hypervisor_callback)
1442 zeroentry xen_do_hypervisor_callback
1443END(xen_hypervisor_callback)
1444
1445/*
1446# A note on the "critical region" in our callback handler.
1447# We want to avoid stacking callback handlers due to events occurring
1448# during handling of the last event. To do this, we keep events disabled
1449# until we've done all processing. HOWEVER, we must enable events before
1450# popping the stack frame (can't be done atomically) and so it would still
1451# be possible to get enough handler activations to overflow the stack.
1452# Although unlikely, bugs of that kind are hard to track down, so we'd
1453# like to avoid the possibility.
1454# So, on entry to the handler we detect whether we interrupted an
1455# existing activation in its critical region -- if so, we pop the current
1456# activation and restart the handler using the previous one.
1457*/
1458ENTRY(xen_do_hypervisor_callback) # do_hypervisor_callback(struct *pt_regs)
1459 CFI_STARTPROC
1460/* Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
1461 see the correct pointer to the pt_regs */
1462 movq %rdi, %rsp # we don't return, adjust the stack frame
1463 CFI_ENDPROC
Alexander van Heukelumdcd072e2008-11-20 14:40:11 +01001464 DEFAULT_FRAME
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -0700146511: incl %gs:pda_irqcount
1466 movq %rsp,%rbp
1467 CFI_DEF_CFA_REGISTER rbp
1468 cmovzq %gs:pda_irqstackptr,%rsp
1469 pushq %rbp # backlink for old unwinder
1470 call xen_evtchn_do_upcall
1471 popq %rsp
1472 CFI_DEF_CFA_REGISTER rsp
1473 decl %gs:pda_irqcount
1474 jmp error_exit
1475 CFI_ENDPROC
1476END(do_hypervisor_callback)
1477
1478/*
1479# Hypervisor uses this for application faults while it executes.
1480# We get here for two reasons:
1481# 1. Fault while reloading DS, ES, FS or GS
1482# 2. Fault while executing IRET
1483# Category 1 we do not need to fix up as Xen has already reloaded all segment
1484# registers that could be reloaded and zeroed the others.
1485# Category 2 we fix up by killing the current process. We cannot use the
1486# normal Linux return path in this case because if we use the IRET hypercall
1487# to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1488# We distinguish between categories by comparing each saved segment register
1489# with its current contents: any discrepancy means we in category 1.
1490*/
1491ENTRY(xen_failsafe_callback)
Alexander van Heukelumdcd072e2008-11-20 14:40:11 +01001492 INTR_FRAME 1 (6*8)
1493 /*CFI_REL_OFFSET gs,GS*/
1494 /*CFI_REL_OFFSET fs,FS*/
1495 /*CFI_REL_OFFSET es,ES*/
1496 /*CFI_REL_OFFSET ds,DS*/
1497 CFI_REL_OFFSET r11,8
1498 CFI_REL_OFFSET rcx,0
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001499 movw %ds,%cx
1500 cmpw %cx,0x10(%rsp)
1501 CFI_REMEMBER_STATE
1502 jne 1f
1503 movw %es,%cx
1504 cmpw %cx,0x18(%rsp)
1505 jne 1f
1506 movw %fs,%cx
1507 cmpw %cx,0x20(%rsp)
1508 jne 1f
1509 movw %gs,%cx
1510 cmpw %cx,0x28(%rsp)
1511 jne 1f
1512 /* All segments match their saved values => Category 2 (Bad IRET). */
1513 movq (%rsp),%rcx
1514 CFI_RESTORE rcx
1515 movq 8(%rsp),%r11
1516 CFI_RESTORE r11
1517 addq $0x30,%rsp
1518 CFI_ADJUST_CFA_OFFSET -0x30
Ingo Molnar14ae22b2008-11-21 15:20:47 +01001519 pushq_cfi $0 /* RIP */
1520 pushq_cfi %r11
1521 pushq_cfi %rcx
Jeremy Fitzhardinge4a5c3e72008-07-08 15:07:09 -07001522 jmp general_protection
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001523 CFI_RESTORE_STATE
15241: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
1525 movq (%rsp),%rcx
1526 CFI_RESTORE rcx
1527 movq 8(%rsp),%r11
1528 CFI_RESTORE r11
1529 addq $0x30,%rsp
1530 CFI_ADJUST_CFA_OFFSET -0x30
Ingo Molnar14ae22b2008-11-21 15:20:47 +01001531 pushq_cfi $0
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001532 SAVE_ALL
1533 jmp error_exit
1534 CFI_ENDPROC
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001535END(xen_failsafe_callback)
1536
1537#endif /* CONFIG_XEN */