blob: 54e0bbdccb994d9b1b87747158487e4817ccde81 [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
Steven Rostedt606576c2008-10-06 19:06:12 -040064#ifdef CONFIG_FUNCTION_TRACER
Steven Rostedtd61f82d2008-05-12 21:20:43 +020065#ifdef CONFIG_DYNAMIC_FTRACE
66ENTRY(mcount)
Steven Rostedtd61f82d2008-05-12 21:20:43 +020067 retq
68END(mcount)
69
70ENTRY(ftrace_caller)
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050071 cmpl $0, function_trace_stop
72 jne ftrace_stub
Steven Rostedtd61f82d2008-05-12 21:20:43 +020073
74 /* taken from glibc */
75 subq $0x38, %rsp
76 movq %rax, (%rsp)
77 movq %rcx, 8(%rsp)
78 movq %rdx, 16(%rsp)
79 movq %rsi, 24(%rsp)
80 movq %rdi, 32(%rsp)
81 movq %r8, 40(%rsp)
82 movq %r9, 48(%rsp)
83
84 movq 0x38(%rsp), %rdi
85 movq 8(%rbp), %rsi
Abhishek Sagar395a59d2008-06-21 23:47:27 +053086 subq $MCOUNT_INSN_SIZE, %rdi
Steven Rostedtd61f82d2008-05-12 21:20:43 +020087
88.globl ftrace_call
89ftrace_call:
90 call ftrace_stub
91
92 movq 48(%rsp), %r9
93 movq 40(%rsp), %r8
94 movq 32(%rsp), %rdi
95 movq 24(%rsp), %rsi
96 movq 16(%rsp), %rdx
97 movq 8(%rsp), %rcx
98 movq (%rsp), %rax
99 addq $0x38, %rsp
100
Frederic Weisbecker48d68b22008-12-02 00:20:39 +0100101#ifdef CONFIG_FUNCTION_GRAPH_TRACER
102.globl ftrace_graph_call
103ftrace_graph_call:
104 jmp ftrace_stub
105#endif
106
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200107.globl ftrace_stub
108ftrace_stub:
109 retq
110END(ftrace_caller)
111
112#else /* ! CONFIG_DYNAMIC_FTRACE */
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200113ENTRY(mcount)
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500114 cmpl $0, function_trace_stop
115 jne ftrace_stub
116
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200117 cmpq $ftrace_stub, ftrace_trace_function
118 jnz trace
Frederic Weisbecker48d68b22008-12-02 00:20:39 +0100119
120#ifdef CONFIG_FUNCTION_GRAPH_TRACER
121 cmpq $ftrace_stub, ftrace_graph_return
122 jnz ftrace_graph_caller
Steven Rostedte49dc192008-12-02 23:50:05 -0500123
124 cmpq $ftrace_graph_entry_stub, ftrace_graph_entry
125 jnz ftrace_graph_caller
Frederic Weisbecker48d68b22008-12-02 00:20:39 +0100126#endif
127
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200128.globl ftrace_stub
129ftrace_stub:
130 retq
131
132trace:
133 /* taken from glibc */
134 subq $0x38, %rsp
135 movq %rax, (%rsp)
136 movq %rcx, 8(%rsp)
137 movq %rdx, 16(%rsp)
138 movq %rsi, 24(%rsp)
139 movq %rdi, 32(%rsp)
140 movq %r8, 40(%rsp)
141 movq %r9, 48(%rsp)
142
143 movq 0x38(%rsp), %rdi
144 movq 8(%rbp), %rsi
Abhishek Sagar395a59d2008-06-21 23:47:27 +0530145 subq $MCOUNT_INSN_SIZE, %rdi
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200146
147 call *ftrace_trace_function
148
149 movq 48(%rsp), %r9
150 movq 40(%rsp), %r8
151 movq 32(%rsp), %rdi
152 movq 24(%rsp), %rsi
153 movq 16(%rsp), %rdx
154 movq 8(%rsp), %rcx
155 movq (%rsp), %rax
156 addq $0x38, %rsp
157
158 jmp ftrace_stub
159END(mcount)
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200160#endif /* CONFIG_DYNAMIC_FTRACE */
Steven Rostedt606576c2008-10-06 19:06:12 -0400161#endif /* CONFIG_FUNCTION_TRACER */
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200162
Frederic Weisbecker48d68b22008-12-02 00:20:39 +0100163#ifdef CONFIG_FUNCTION_GRAPH_TRACER
164ENTRY(ftrace_graph_caller)
165 cmpl $0, function_trace_stop
166 jne ftrace_stub
167
168 subq $0x38, %rsp
169 movq %rax, (%rsp)
170 movq %rcx, 8(%rsp)
171 movq %rdx, 16(%rsp)
172 movq %rsi, 24(%rsp)
173 movq %rdi, 32(%rsp)
174 movq %r8, 40(%rsp)
175 movq %r9, 48(%rsp)
176
177 leaq 8(%rbp), %rdi
178 movq 0x38(%rsp), %rsi
Steven Rostedtbb4304c2008-12-02 15:34:09 -0500179 subq $MCOUNT_INSN_SIZE, %rsi
Frederic Weisbecker48d68b22008-12-02 00:20:39 +0100180
181 call prepare_ftrace_return
182
183 movq 48(%rsp), %r9
184 movq 40(%rsp), %r8
185 movq 32(%rsp), %rdi
186 movq 24(%rsp), %rsi
187 movq 16(%rsp), %rdx
188 movq 8(%rsp), %rcx
189 movq (%rsp), %rax
190 addq $0x38, %rsp
191 retq
192END(ftrace_graph_caller)
193
194
195.globl return_to_handler
196return_to_handler:
197 subq $80, %rsp
198
199 movq %rax, (%rsp)
200 movq %rcx, 8(%rsp)
201 movq %rdx, 16(%rsp)
202 movq %rsi, 24(%rsp)
203 movq %rdi, 32(%rsp)
204 movq %r8, 40(%rsp)
205 movq %r9, 48(%rsp)
206 movq %r10, 56(%rsp)
207 movq %r11, 64(%rsp)
208
209 call ftrace_return_to_handler
210
211 movq %rax, 72(%rsp)
212 movq 64(%rsp), %r11
213 movq 56(%rsp), %r10
214 movq 48(%rsp), %r9
215 movq 40(%rsp), %r8
216 movq 32(%rsp), %rdi
217 movq 24(%rsp), %rsi
218 movq 16(%rsp), %rdx
219 movq 8(%rsp), %rcx
220 movq (%rsp), %rax
221 addq $72, %rsp
222 retq
223#endif
224
225
Andi Kleendc37db42005-04-16 15:25:05 -0700226#ifndef CONFIG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227#define retint_kernel retint_restore_args
228#endif
Ingo Molnar2601e642006-07-03 00:24:45 -0700229
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100230#ifdef CONFIG_PARAVIRT
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -0400231ENTRY(native_usergs_sysret64)
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100232 swapgs
233 sysretq
234#endif /* CONFIG_PARAVIRT */
235
Ingo Molnar2601e642006-07-03 00:24:45 -0700236
237.macro TRACE_IRQS_IRETQ offset=ARGOFFSET
238#ifdef CONFIG_TRACE_IRQFLAGS
239 bt $9,EFLAGS-\offset(%rsp) /* interrupts off? */
240 jnc 1f
241 TRACE_IRQS_ON
2421:
243#endif
244.endm
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246/*
247 * C code is not supposed to know about undefined top of stack. Every time
248 * a C function with an pt_regs argument is called from the SYSCALL based
249 * fast path FIXUP_TOP_OF_STACK is needed.
250 * RESTORE_TOP_OF_STACK syncs the syscall state after any possible ptregs
251 * manipulation.
252 */
253
254 /* %rsp:at FRAMEEND */
255 .macro FIXUP_TOP_OF_STACK tmp
256 movq %gs:pda_oldrsp,\tmp
257 movq \tmp,RSP(%rsp)
258 movq $__USER_DS,SS(%rsp)
259 movq $__USER_CS,CS(%rsp)
260 movq $-1,RCX(%rsp)
261 movq R11(%rsp),\tmp /* get eflags */
262 movq \tmp,EFLAGS(%rsp)
263 .endm
264
265 .macro RESTORE_TOP_OF_STACK tmp,offset=0
266 movq RSP-\offset(%rsp),\tmp
267 movq \tmp,%gs:pda_oldrsp
268 movq EFLAGS-\offset(%rsp),\tmp
269 movq \tmp,R11-\offset(%rsp)
270 .endm
271
272 .macro FAKE_STACK_FRAME child_rip
273 /* push in order ss, rsp, eflags, cs, rip */
Andi Kleen3829ee62005-07-28 21:15:48 -0700274 xorl %eax, %eax
Jeremy Fitzhardingee04e0a62008-06-25 00:19:25 -0400275 pushq $__KERNEL_DS /* ss */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 CFI_ADJUST_CFA_OFFSET 8
Jan Beulich7effaa82005-09-12 18:49:24 +0200277 /*CFI_REL_OFFSET ss,0*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 pushq %rax /* rsp */
279 CFI_ADJUST_CFA_OFFSET 8
Jan Beulich7effaa82005-09-12 18:49:24 +0200280 CFI_REL_OFFSET rsp,0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 pushq $(1<<9) /* eflags - interrupts on */
282 CFI_ADJUST_CFA_OFFSET 8
Jan Beulich7effaa82005-09-12 18:49:24 +0200283 /*CFI_REL_OFFSET rflags,0*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 pushq $__KERNEL_CS /* cs */
285 CFI_ADJUST_CFA_OFFSET 8
Jan Beulich7effaa82005-09-12 18:49:24 +0200286 /*CFI_REL_OFFSET cs,0*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 pushq \child_rip /* rip */
288 CFI_ADJUST_CFA_OFFSET 8
Jan Beulich7effaa82005-09-12 18:49:24 +0200289 CFI_REL_OFFSET rip,0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 pushq %rax /* orig rax */
291 CFI_ADJUST_CFA_OFFSET 8
292 .endm
293
294 .macro UNFAKE_STACK_FRAME
295 addq $8*6, %rsp
296 CFI_ADJUST_CFA_OFFSET -(6*8)
297 .endm
298
Jan Beulich7effaa82005-09-12 18:49:24 +0200299 .macro CFI_DEFAULT_STACK start=1
300 .if \start
301 CFI_STARTPROC simple
Jan Beulichadf14232006-09-26 10:52:41 +0200302 CFI_SIGNAL_FRAME
Jan Beulich7effaa82005-09-12 18:49:24 +0200303 CFI_DEF_CFA rsp,SS+8
304 .else
305 CFI_DEF_CFA_OFFSET SS+8
306 .endif
307 CFI_REL_OFFSET r15,R15
308 CFI_REL_OFFSET r14,R14
309 CFI_REL_OFFSET r13,R13
310 CFI_REL_OFFSET r12,R12
311 CFI_REL_OFFSET rbp,RBP
312 CFI_REL_OFFSET rbx,RBX
313 CFI_REL_OFFSET r11,R11
314 CFI_REL_OFFSET r10,R10
315 CFI_REL_OFFSET r9,R9
316 CFI_REL_OFFSET r8,R8
317 CFI_REL_OFFSET rax,RAX
318 CFI_REL_OFFSET rcx,RCX
319 CFI_REL_OFFSET rdx,RDX
320 CFI_REL_OFFSET rsi,RSI
321 CFI_REL_OFFSET rdi,RDI
322 CFI_REL_OFFSET rip,RIP
323 /*CFI_REL_OFFSET cs,CS*/
324 /*CFI_REL_OFFSET rflags,EFLAGS*/
325 CFI_REL_OFFSET rsp,RSP
326 /*CFI_REL_OFFSET ss,SS*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 .endm
328/*
329 * A newly forked process directly context switches into this.
330 */
331/* rdi: prev */
332ENTRY(ret_from_fork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 CFI_DEFAULT_STACK
Andi Kleen658fdbe2006-09-26 10:52:41 +0200334 push kernel_eflags(%rip)
Alexander van Heukelume0a5a5d2008-07-22 18:14:16 +0200335 CFI_ADJUST_CFA_OFFSET 8
Andi Kleen658fdbe2006-09-26 10:52:41 +0200336 popf # reset kernel eflags
Alexander van Heukelume0a5a5d2008-07-22 18:14:16 +0200337 CFI_ADJUST_CFA_OFFSET -8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 call schedule_tail
339 GET_THREAD_INFO(%rcx)
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300340 testl $(_TIF_SYSCALL_TRACE|_TIF_SYSCALL_AUDIT),TI_flags(%rcx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 jnz rff_trace
342rff_action:
343 RESTORE_REST
344 testl $3,CS-ARGOFFSET(%rsp) # from kernel_thread?
345 je int_ret_from_sys_call
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300346 testl $_TIF_IA32,TI_flags(%rcx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 jnz int_ret_from_sys_call
348 RESTORE_TOP_OF_STACK %rdi,ARGOFFSET
349 jmp ret_from_sys_call
350rff_trace:
351 movq %rsp,%rdi
352 call syscall_trace_leave
353 GET_THREAD_INFO(%rcx)
354 jmp rff_action
355 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200356END(ret_from_fork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358/*
359 * System call entry. Upto 6 arguments in registers are supported.
360 *
361 * SYSCALL does not save anything on the stack and does not change the
362 * stack pointer.
363 */
364
365/*
366 * Register setup:
367 * rax system call number
368 * rdi arg0
369 * rcx return address for syscall/sysret, C arg3
370 * rsi arg1
371 * rdx arg2
372 * r10 arg3 (--> moved to rcx for C)
373 * r8 arg4
374 * r9 arg5
375 * r11 eflags for syscall/sysret, temporary for C
376 * r12-r15,rbp,rbx saved by C code, not touched.
377 *
378 * Interrupts are off on entry.
379 * Only called from user space.
380 *
381 * XXX if we had a free scratch register we could save the RSP into the stack frame
382 * and report it properly in ps. Unfortunately we haven't.
Andi Kleen7bf36bb2006-04-07 19:50:00 +0200383 *
384 * When user can change the frames always force IRET. That is because
385 * it deals with uncanonical addresses better. SYSRET has trouble
386 * with them due to bugs in both AMD and Intel CPUs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 */
388
389ENTRY(system_call)
Jan Beulich7effaa82005-09-12 18:49:24 +0200390 CFI_STARTPROC simple
Jan Beulichadf14232006-09-26 10:52:41 +0200391 CFI_SIGNAL_FRAME
Jan Beulichdffead42006-06-26 13:57:38 +0200392 CFI_DEF_CFA rsp,PDA_STACKOFFSET
Jan Beulich7effaa82005-09-12 18:49:24 +0200393 CFI_REGISTER rip,rcx
394 /*CFI_REGISTER rflags,r11*/
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100395 SWAPGS_UNSAFE_STACK
396 /*
397 * A hypervisor implementation might want to use a label
398 * after the swapgs, so that it can do the swapgs
399 * for the guest and jump here on syscall.
400 */
401ENTRY(system_call_after_swapgs)
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 movq %rsp,%gs:pda_oldrsp
404 movq %gs:pda_kernelstack,%rsp
Ingo Molnar2601e642006-07-03 00:24:45 -0700405 /*
406 * No need to follow this irqs off/on section - it's straight
407 * and short:
408 */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100409 ENABLE_INTERRUPTS(CLBR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 SAVE_ARGS 8,1
411 movq %rax,ORIG_RAX-ARGOFFSET(%rsp)
Jan Beulich7effaa82005-09-12 18:49:24 +0200412 movq %rcx,RIP-ARGOFFSET(%rsp)
413 CFI_REL_OFFSET rip,RIP-ARGOFFSET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 GET_THREAD_INFO(%rcx)
Roland McGrathd4d67152008-07-09 02:38:07 -0700415 testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags(%rcx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 jnz tracesys
Roland McGrath86a1c342008-06-23 15:37:04 -0700417system_call_fastpath:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 cmpq $__NR_syscall_max,%rax
419 ja badsys
420 movq %r10,%rcx
421 call *sys_call_table(,%rax,8) # XXX: rip relative
422 movq %rax,RAX-ARGOFFSET(%rsp)
423/*
424 * Syscall return path ending with SYSRET (fast path)
425 * Has incomplete stack frame and undefined top of stack.
426 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427ret_from_sys_call:
Andi Kleen11b854b2005-04-16 15:25:02 -0700428 movl $_TIF_ALLWORK_MASK,%edi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 /* edi: flagmask */
430sysret_check:
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200431 LOCKDEP_SYS_EXIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 GET_THREAD_INFO(%rcx)
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100433 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700434 TRACE_IRQS_OFF
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300435 movl TI_flags(%rcx),%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 andl %edi,%edx
437 jnz sysret_careful
Jan Beulichbcddc012006-12-07 02:14:02 +0100438 CFI_REMEMBER_STATE
Ingo Molnar2601e642006-07-03 00:24:45 -0700439 /*
440 * sysretq will re-enable interrupts:
441 */
442 TRACE_IRQS_ON
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 movq RIP-ARGOFFSET(%rsp),%rcx
Jan Beulich7effaa82005-09-12 18:49:24 +0200444 CFI_REGISTER rip,rcx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 RESTORE_ARGS 0,-ARG_SKIP,1
Jan Beulich7effaa82005-09-12 18:49:24 +0200446 /*CFI_REGISTER rflags,r11*/
Jeremy Fitzhardingec7245da2008-06-25 00:19:27 -0400447 movq %gs:pda_oldrsp, %rsp
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -0400448 USERGS_SYSRET64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Jan Beulichbcddc012006-12-07 02:14:02 +0100450 CFI_RESTORE_STATE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 /* Handle reschedules */
452 /* edx: work, edi: workmask */
453sysret_careful:
454 bt $TIF_NEED_RESCHED,%edx
455 jnc sysret_signal
Ingo Molnar2601e642006-07-03 00:24:45 -0700456 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100457 ENABLE_INTERRUPTS(CLBR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 pushq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200459 CFI_ADJUST_CFA_OFFSET 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 call schedule
461 popq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200462 CFI_ADJUST_CFA_OFFSET -8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 jmp sysret_check
464
465 /* Handle a signal */
466sysret_signal:
Ingo Molnar2601e642006-07-03 00:24:45 -0700467 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100468 ENABLE_INTERRUPTS(CLBR_NONE)
Roland McGrath86a1c342008-06-23 15:37:04 -0700469#ifdef CONFIG_AUDITSYSCALL
470 bt $TIF_SYSCALL_AUDIT,%edx
471 jc sysret_audit
472#endif
Andi Kleen10ffdbb2005-05-16 21:53:19 -0700473 /* edx: work flags (arg3) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 leaq do_notify_resume(%rip),%rax
475 leaq -ARGOFFSET(%rsp),%rdi # &pt_regs -> arg1
476 xorl %esi,%esi # oldset -> arg2
477 call ptregscall_common
Roland McGrath15e8f342008-06-23 20:41:12 -0700478 movl $_TIF_WORK_MASK,%edi
Andi Kleen7bf36bb2006-04-07 19:50:00 +0200479 /* Use IRET because user could have changed frame. This
480 works because ptregscall_common has called FIXUP_TOP_OF_STACK. */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100481 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700482 TRACE_IRQS_OFF
Andi Kleen7bf36bb2006-04-07 19:50:00 +0200483 jmp int_with_check
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Jan Beulich7effaa82005-09-12 18:49:24 +0200485badsys:
486 movq $-ENOSYS,RAX-ARGOFFSET(%rsp)
487 jmp ret_from_sys_call
488
Roland McGrath86a1c342008-06-23 15:37:04 -0700489#ifdef CONFIG_AUDITSYSCALL
490 /*
491 * Fast path for syscall audit without full syscall trace.
492 * We just call audit_syscall_entry() directly, and then
493 * jump back to the normal fast path.
494 */
495auditsys:
496 movq %r10,%r9 /* 6th arg: 4th syscall arg */
497 movq %rdx,%r8 /* 5th arg: 3rd syscall arg */
498 movq %rsi,%rcx /* 4th arg: 2nd syscall arg */
499 movq %rdi,%rdx /* 3rd arg: 1st syscall arg */
500 movq %rax,%rsi /* 2nd arg: syscall number */
501 movl $AUDIT_ARCH_X86_64,%edi /* 1st arg: audit arch */
502 call audit_syscall_entry
503 LOAD_ARGS 0 /* reload call-clobbered registers */
504 jmp system_call_fastpath
505
506 /*
507 * Return fast path for syscall audit. Call audit_syscall_exit()
508 * directly and then jump back to the fast path with TIF_SYSCALL_AUDIT
509 * masked off.
510 */
511sysret_audit:
512 movq %rax,%rsi /* second arg, syscall return value */
513 cmpq $0,%rax /* is it < 0? */
514 setl %al /* 1 if so, 0 if not */
515 movzbl %al,%edi /* zero-extend that into %edi */
516 inc %edi /* first arg, 0->1(AUDITSC_SUCCESS), 1->2(AUDITSC_FAILURE) */
517 call audit_syscall_exit
518 movl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT),%edi
519 jmp sysret_check
520#endif /* CONFIG_AUDITSYSCALL */
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 /* Do syscall tracing */
523tracesys:
Roland McGrath86a1c342008-06-23 15:37:04 -0700524#ifdef CONFIG_AUDITSYSCALL
525 testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT),TI_flags(%rcx)
526 jz auditsys
527#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 SAVE_REST
Roland McGratha31f8dd2008-03-16 21:59:11 -0700529 movq $-ENOSYS,RAX(%rsp) /* ptrace can change this for a bad syscall */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 FIXUP_TOP_OF_STACK %rdi
531 movq %rsp,%rdi
532 call syscall_trace_enter
Roland McGrathd4d67152008-07-09 02:38:07 -0700533 /*
534 * Reload arg registers from stack in case ptrace changed them.
535 * We don't reload %rax because syscall_trace_enter() returned
536 * the value it wants us to use in the table lookup.
537 */
538 LOAD_ARGS ARGOFFSET, 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 RESTORE_REST
540 cmpq $__NR_syscall_max,%rax
Roland McGratha31f8dd2008-03-16 21:59:11 -0700541 ja int_ret_from_sys_call /* RAX(%rsp) set to -ENOSYS above */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 movq %r10,%rcx /* fixup for C */
543 call *sys_call_table(,%rax,8)
Roland McGratha31f8dd2008-03-16 21:59:11 -0700544 movq %rax,RAX-ARGOFFSET(%rsp)
Andi Kleen7bf36bb2006-04-07 19:50:00 +0200545 /* Use IRET because user could have changed frame */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547/*
548 * Syscall return path ending with IRET.
549 * Has correct top of stack, but partial stack frame.
Jan Beulichbcddc012006-12-07 02:14:02 +0100550 */
551 .globl int_ret_from_sys_call
Roland McGrath5cbf1562008-06-24 01:13:31 -0700552 .globl int_with_check
Jan Beulichbcddc012006-12-07 02:14:02 +0100553int_ret_from_sys_call:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100554 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700555 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 testl $3,CS-ARGOFFSET(%rsp)
557 je retint_restore_args
558 movl $_TIF_ALLWORK_MASK,%edi
559 /* edi: mask to check */
560int_with_check:
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200561 LOCKDEP_SYS_EXIT_IRQ
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 GET_THREAD_INFO(%rcx)
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300563 movl TI_flags(%rcx),%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 andl %edi,%edx
565 jnz int_careful
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300566 andl $~TS_COMPAT,TI_status(%rcx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 jmp retint_swapgs
568
569 /* Either reschedule or signal or syscall exit tracking needed. */
570 /* First do a reschedule test. */
571 /* edx: work, edi: workmask */
572int_careful:
573 bt $TIF_NEED_RESCHED,%edx
574 jnc int_very_careful
Ingo Molnar2601e642006-07-03 00:24:45 -0700575 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100576 ENABLE_INTERRUPTS(CLBR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 pushq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200578 CFI_ADJUST_CFA_OFFSET 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 call schedule
580 popq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200581 CFI_ADJUST_CFA_OFFSET -8
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100582 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700583 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 jmp int_with_check
585
586 /* handle signals and tracing -- both require a full stack frame */
587int_very_careful:
Ingo Molnar2601e642006-07-03 00:24:45 -0700588 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100589 ENABLE_INTERRUPTS(CLBR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 SAVE_REST
591 /* Check for syscall exit trace */
Roland McGrathd4d67152008-07-09 02:38:07 -0700592 testl $_TIF_WORK_SYSCALL_EXIT,%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 jz int_signal
594 pushq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200595 CFI_ADJUST_CFA_OFFSET 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 leaq 8(%rsp),%rdi # &ptregs -> arg1
597 call syscall_trace_leave
598 popq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200599 CFI_ADJUST_CFA_OFFSET -8
Roland McGrathd4d67152008-07-09 02:38:07 -0700600 andl $~(_TIF_WORK_SYSCALL_EXIT|_TIF_SYSCALL_EMU),%edi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 jmp int_restore_rest
602
603int_signal:
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100604 testl $_TIF_DO_NOTIFY_MASK,%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 jz 1f
606 movq %rsp,%rdi # &ptregs -> arg1
607 xorl %esi,%esi # oldset -> arg2
608 call do_notify_resume
Roland McGratheca91e72008-07-10 14:50:39 -07006091: movl $_TIF_WORK_MASK,%edi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610int_restore_rest:
611 RESTORE_REST
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100612 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700613 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 jmp int_with_check
615 CFI_ENDPROC
Jan Beulichbcddc012006-12-07 02:14:02 +0100616END(system_call)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618/*
619 * Certain special system calls that need to save a complete full stack frame.
620 */
621
622 .macro PTREGSCALL label,func,arg
623 .globl \label
624\label:
625 leaq \func(%rip),%rax
626 leaq -ARGOFFSET+8(%rsp),\arg /* 8 for return address */
627 jmp ptregscall_common
Jan Beulich4b787e02006-06-26 13:56:55 +0200628END(\label)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 .endm
630
Jan Beulich7effaa82005-09-12 18:49:24 +0200631 CFI_STARTPROC
632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 PTREGSCALL stub_clone, sys_clone, %r8
634 PTREGSCALL stub_fork, sys_fork, %rdi
635 PTREGSCALL stub_vfork, sys_vfork, %rdi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 PTREGSCALL stub_sigaltstack, sys_sigaltstack, %rdx
637 PTREGSCALL stub_iopl, sys_iopl, %rsi
638
639ENTRY(ptregscall_common)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 popq %r11
Jan Beulich7effaa82005-09-12 18:49:24 +0200641 CFI_ADJUST_CFA_OFFSET -8
642 CFI_REGISTER rip, r11
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 SAVE_REST
644 movq %r11, %r15
Jan Beulich7effaa82005-09-12 18:49:24 +0200645 CFI_REGISTER rip, r15
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 FIXUP_TOP_OF_STACK %r11
647 call *%rax
648 RESTORE_TOP_OF_STACK %r11
649 movq %r15, %r11
Jan Beulich7effaa82005-09-12 18:49:24 +0200650 CFI_REGISTER rip, r11
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 RESTORE_REST
652 pushq %r11
Jan Beulich7effaa82005-09-12 18:49:24 +0200653 CFI_ADJUST_CFA_OFFSET 8
654 CFI_REL_OFFSET rip, 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 ret
656 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200657END(ptregscall_common)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659ENTRY(stub_execve)
660 CFI_STARTPROC
661 popq %r11
Jan Beulich7effaa82005-09-12 18:49:24 +0200662 CFI_ADJUST_CFA_OFFSET -8
663 CFI_REGISTER rip, r11
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 SAVE_REST
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 FIXUP_TOP_OF_STACK %r11
Ingo Molnar5d119b22008-02-26 12:55:57 +0100666 movq %rsp, %rcx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 call sys_execve
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 RESTORE_TOP_OF_STACK %r11
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 movq %rax,RAX(%rsp)
670 RESTORE_REST
671 jmp int_ret_from_sys_call
672 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200673END(stub_execve)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675/*
676 * sigreturn is special because it needs to restore all registers on return.
677 * This cannot be done with SYSRET, so use the IRET return path instead.
678 */
679ENTRY(stub_rt_sigreturn)
680 CFI_STARTPROC
Jan Beulich7effaa82005-09-12 18:49:24 +0200681 addq $8, %rsp
682 CFI_ADJUST_CFA_OFFSET -8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 SAVE_REST
684 movq %rsp,%rdi
685 FIXUP_TOP_OF_STACK %r11
686 call sys_rt_sigreturn
687 movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer
688 RESTORE_REST
689 jmp int_ret_from_sys_call
690 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200691END(stub_rt_sigreturn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Jan Beulich7effaa82005-09-12 18:49:24 +0200693/*
694 * initial frame state for interrupts and exceptions
695 */
696 .macro _frame ref
697 CFI_STARTPROC simple
Jan Beulichadf14232006-09-26 10:52:41 +0200698 CFI_SIGNAL_FRAME
Jan Beulich7effaa82005-09-12 18:49:24 +0200699 CFI_DEF_CFA rsp,SS+8-\ref
700 /*CFI_REL_OFFSET ss,SS-\ref*/
701 CFI_REL_OFFSET rsp,RSP-\ref
702 /*CFI_REL_OFFSET rflags,EFLAGS-\ref*/
703 /*CFI_REL_OFFSET cs,CS-\ref*/
704 CFI_REL_OFFSET rip,RIP-\ref
705 .endm
706
707/* initial frame state for interrupts (and exceptions without error code) */
708#define INTR_FRAME _frame RIP
709/* initial frame state for exceptions with error code (and interrupts with
710 vector already pushed) */
711#define XCPT_FRAME _frame ORIG_RAX
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713/*
714 * Interrupt entry/exit.
715 *
716 * Interrupt entry points save only callee clobbered registers in fast path.
717 *
718 * Entry runs with interrupts off.
719 */
720
721/* 0(%rsp): interrupt number */
722 .macro interrupt func
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 cld
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 SAVE_ARGS
725 leaq -ARGOFFSET(%rsp),%rdi # arg1 for handler
Jan Beulich1de9c3f2006-06-26 13:57:35 +0200726 pushq %rbp
Glauber Costa097a0782008-08-14 17:33:12 -0300727 /*
728 * Save rbp twice: One is for marking the stack frame, as usual, and the
729 * other, to fill pt_regs properly. This is because bx comes right
730 * before the last saved register in that structure, and not bp. If the
731 * base pointer were in the place bx is today, this would not be needed.
732 */
733 movq %rbp, -8(%rsp)
Jan Beulich1de9c3f2006-06-26 13:57:35 +0200734 CFI_ADJUST_CFA_OFFSET 8
735 CFI_REL_OFFSET rbp, 0
736 movq %rsp,%rbp
737 CFI_DEF_CFA_REGISTER rbp
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 testl $3,CS(%rdi)
739 je 1f
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100740 SWAPGS
Andi Kleen96e54042006-09-26 10:52:39 +0200741 /* irqcount is used to check if a CPU is already on an interrupt
742 stack or not. While this is essentially redundant with preempt_count
743 it is a little cheaper to use a separate counter in the PDA
744 (short of moving irq_enter into assembly, which would be too
745 much work) */
7461: incl %gs:pda_irqcount
Jan Beulich1de9c3f2006-06-26 13:57:35 +0200747 cmoveq %gs:pda_irqstackptr,%rsp
Andi Kleen26995002006-08-02 22:37:28 +0200748 push %rbp # backlink for old unwinder
Ingo Molnar2601e642006-07-03 00:24:45 -0700749 /*
750 * We entered an interrupt context - irqs are off:
751 */
752 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 call \func
754 .endm
755
756ENTRY(common_interrupt)
Jan Beulich7effaa82005-09-12 18:49:24 +0200757 XCPT_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 interrupt do_IRQ
759 /* 0(%rsp): oldrsp-ARGOFFSET */
Jan Beulich7effaa82005-09-12 18:49:24 +0200760ret_from_intr:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100761 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700762 TRACE_IRQS_OFF
Andi Kleen3829ee62005-07-28 21:15:48 -0700763 decl %gs:pda_irqcount
Jan Beulich1de9c3f2006-06-26 13:57:35 +0200764 leaveq
Jan Beulich7effaa82005-09-12 18:49:24 +0200765 CFI_DEF_CFA_REGISTER rsp
Jan Beulich1de9c3f2006-06-26 13:57:35 +0200766 CFI_ADJUST_CFA_OFFSET -8
Jan Beulich7effaa82005-09-12 18:49:24 +0200767exit_intr:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 GET_THREAD_INFO(%rcx)
769 testl $3,CS-ARGOFFSET(%rsp)
770 je retint_kernel
771
772 /* Interrupt came from user space */
773 /*
774 * Has a correct top of stack, but a partial stack frame
775 * %rcx: thread info. Interrupts off.
776 */
777retint_with_reschedule:
778 movl $_TIF_WORK_MASK,%edi
Jan Beulich7effaa82005-09-12 18:49:24 +0200779retint_check:
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200780 LOCKDEP_SYS_EXIT_IRQ
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300781 movl TI_flags(%rcx),%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 andl %edi,%edx
Jan Beulich7effaa82005-09-12 18:49:24 +0200783 CFI_REMEMBER_STATE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 jnz retint_careful
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200785
786retint_swapgs: /* return to user-space */
Ingo Molnar2601e642006-07-03 00:24:45 -0700787 /*
788 * The iretq could re-enable interrupts:
789 */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100790 DISABLE_INTERRUPTS(CLBR_ANY)
Ingo Molnar2601e642006-07-03 00:24:45 -0700791 TRACE_IRQS_IRETQ
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100792 SWAPGS
Ingo Molnar2601e642006-07-03 00:24:45 -0700793 jmp restore_args
794
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200795retint_restore_args: /* return to kernel space */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100796 DISABLE_INTERRUPTS(CLBR_ANY)
Ingo Molnar2601e642006-07-03 00:24:45 -0700797 /*
798 * The iretq could re-enable interrupts:
799 */
800 TRACE_IRQS_IRETQ
801restore_args:
Ingo Molnar3701d8632008-02-09 23:24:08 +0100802 RESTORE_ARGS 0,8,0
803
Adrian Bunkf7f3d792008-02-13 23:29:53 +0200804irq_return:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100805 INTERRUPT_RETURN
Ingo Molnar3701d8632008-02-09 23:24:08 +0100806
807 .section __ex_table, "a"
808 .quad irq_return, bad_iret
809 .previous
810
811#ifdef CONFIG_PARAVIRT
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100812ENTRY(native_iret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 iretq
814
815 .section __ex_table,"a"
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100816 .quad native_iret, bad_iret
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 .previous
Ingo Molnar3701d8632008-02-09 23:24:08 +0100818#endif
819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 .section .fixup,"ax"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821bad_iret:
Roland McGrath3aa4b372008-02-06 22:39:43 +0100822 /*
823 * The iret traps when the %cs or %ss being restored is bogus.
824 * We've lost the original trap vector and error code.
825 * #GPF is the most likely one to get for an invalid selector.
826 * So pretend we completed the iret and took the #GPF in user mode.
827 *
828 * We are now running with the kernel GS after exception recovery.
829 * But error_entry expects us to have user GS to match the user %cs,
830 * so swap back.
831 */
832 pushq $0
833
834 SWAPGS
835 jmp general_protection
836
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100837 .previous
838
Jan Beulich7effaa82005-09-12 18:49:24 +0200839 /* edi: workmask, edx: work */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840retint_careful:
Jan Beulich7effaa82005-09-12 18:49:24 +0200841 CFI_RESTORE_STATE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 bt $TIF_NEED_RESCHED,%edx
843 jnc retint_signal
Ingo Molnar2601e642006-07-03 00:24:45 -0700844 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100845 ENABLE_INTERRUPTS(CLBR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 pushq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200847 CFI_ADJUST_CFA_OFFSET 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 call schedule
849 popq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200850 CFI_ADJUST_CFA_OFFSET -8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 GET_THREAD_INFO(%rcx)
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100852 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700853 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 jmp retint_check
855
856retint_signal:
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100857 testl $_TIF_DO_NOTIFY_MASK,%edx
Andi Kleen10ffdbb2005-05-16 21:53:19 -0700858 jz retint_swapgs
Ingo Molnar2601e642006-07-03 00:24:45 -0700859 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100860 ENABLE_INTERRUPTS(CLBR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 SAVE_REST
862 movq $-1,ORIG_RAX(%rsp)
Andi Kleen3829ee62005-07-28 21:15:48 -0700863 xorl %esi,%esi # oldset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 movq %rsp,%rdi # &pt_regs
865 call do_notify_resume
866 RESTORE_REST
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100867 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700868 TRACE_IRQS_OFF
Andi Kleenbe9e6872005-05-01 08:58:51 -0700869 GET_THREAD_INFO(%rcx)
Roland McGratheca91e72008-07-10 14:50:39 -0700870 jmp retint_with_reschedule
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872#ifdef CONFIG_PREEMPT
873 /* Returning to kernel space. Check if we need preemption */
874 /* rcx: threadinfo. interrupts off. */
Andi Kleenb06baba2006-09-26 10:52:29 +0200875ENTRY(retint_kernel)
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300876 cmpl $0,TI_preempt_count(%rcx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 jnz retint_restore_args
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300878 bt $TIF_NEED_RESCHED,TI_flags(%rcx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 jnc retint_restore_args
880 bt $9,EFLAGS-ARGOFFSET(%rsp) /* interrupts off? */
881 jnc retint_restore_args
882 call preempt_schedule_irq
883 jmp exit_intr
884#endif
Jan Beulich4b787e02006-06-26 13:56:55 +0200885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200887END(common_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889/*
890 * APIC interrupts.
891 */
892 .macro apicinterrupt num,func
Jan Beulich7effaa82005-09-12 18:49:24 +0200893 INTR_FRAME
Rusty Russell19eadf92006-06-27 02:53:44 -0700894 pushq $~(\num)
Jan Beulich7effaa82005-09-12 18:49:24 +0200895 CFI_ADJUST_CFA_OFFSET 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 interrupt \func
897 jmp ret_from_intr
898 CFI_ENDPROC
899 .endm
900
901ENTRY(thermal_interrupt)
902 apicinterrupt THERMAL_APIC_VECTOR,smp_thermal_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200903END(thermal_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Jacob Shin89b831e2005-11-05 17:25:53 +0100905ENTRY(threshold_interrupt)
906 apicinterrupt THRESHOLD_APIC_VECTOR,mce_threshold_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200907END(threshold_interrupt)
Jacob Shin89b831e2005-11-05 17:25:53 +0100908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909#ifdef CONFIG_SMP
910ENTRY(reschedule_interrupt)
911 apicinterrupt RESCHEDULE_VECTOR,smp_reschedule_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200912END(reschedule_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Andi Kleene5bc8b62005-09-12 18:49:24 +0200914 .macro INVALIDATE_ENTRY num
915ENTRY(invalidate_interrupt\num)
916 apicinterrupt INVALIDATE_TLB_VECTOR_START+\num,smp_invalidate_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200917END(invalidate_interrupt\num)
Andi Kleene5bc8b62005-09-12 18:49:24 +0200918 .endm
919
920 INVALIDATE_ENTRY 0
921 INVALIDATE_ENTRY 1
922 INVALIDATE_ENTRY 2
923 INVALIDATE_ENTRY 3
924 INVALIDATE_ENTRY 4
925 INVALIDATE_ENTRY 5
926 INVALIDATE_ENTRY 6
927 INVALIDATE_ENTRY 7
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929ENTRY(call_function_interrupt)
930 apicinterrupt CALL_FUNCTION_VECTOR,smp_call_function_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200931END(call_function_interrupt)
Jens Axboe3b16cf82008-06-26 11:21:54 +0200932ENTRY(call_function_single_interrupt)
933 apicinterrupt CALL_FUNCTION_SINGLE_VECTOR,smp_call_function_single_interrupt
934END(call_function_single_interrupt)
Eric W. Biederman61014292007-02-23 04:40:58 -0700935ENTRY(irq_move_cleanup_interrupt)
936 apicinterrupt IRQ_MOVE_CLEANUP_VECTOR,smp_irq_move_cleanup_interrupt
937END(irq_move_cleanup_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938#endif
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940ENTRY(apic_timer_interrupt)
941 apicinterrupt LOCAL_TIMER_VECTOR,smp_apic_timer_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200942END(apic_timer_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Cliff Wickman18129242008-06-02 08:56:14 -0500944ENTRY(uv_bau_message_intr1)
945 apicinterrupt 220,uv_bau_message_interrupt
946END(uv_bau_message_intr1)
947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948ENTRY(error_interrupt)
949 apicinterrupt ERROR_APIC_VECTOR,smp_error_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200950END(error_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952ENTRY(spurious_interrupt)
953 apicinterrupt SPURIOUS_APIC_VECTOR,smp_spurious_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200954END(spurious_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956/*
957 * Exception entry points.
958 */
959 .macro zeroentry sym
Jan Beulich7effaa82005-09-12 18:49:24 +0200960 INTR_FRAME
Jeremy Fitzhardingefab58422008-06-25 00:19:31 -0400961 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 pushq $0 /* push error code/oldrax */
Jan Beulich7effaa82005-09-12 18:49:24 +0200963 CFI_ADJUST_CFA_OFFSET 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 pushq %rax /* push real oldrax to the rdi slot */
Jan Beulich7effaa82005-09-12 18:49:24 +0200965 CFI_ADJUST_CFA_OFFSET 8
Jan Beulich37550902007-05-02 19:27:05 +0200966 CFI_REL_OFFSET rax,0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 leaq \sym(%rip),%rax
968 jmp error_entry
Jan Beulich7effaa82005-09-12 18:49:24 +0200969 CFI_ENDPROC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 .endm
971
972 .macro errorentry sym
Jan Beulich7effaa82005-09-12 18:49:24 +0200973 XCPT_FRAME
Jeremy Fitzhardingefab58422008-06-25 00:19:31 -0400974 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 pushq %rax
Jan Beulich7effaa82005-09-12 18:49:24 +0200976 CFI_ADJUST_CFA_OFFSET 8
Jan Beulich37550902007-05-02 19:27:05 +0200977 CFI_REL_OFFSET rax,0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 leaq \sym(%rip),%rax
979 jmp error_entry
Jan Beulich7effaa82005-09-12 18:49:24 +0200980 CFI_ENDPROC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 .endm
982
983 /* error code is on the stack already */
984 /* handle NMI like exceptions that can happen everywhere */
Ingo Molnar2601e642006-07-03 00:24:45 -0700985 .macro paranoidentry sym, ist=0, irqtrace=1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 SAVE_ALL
987 cld
988 movl $1,%ebx
989 movl $MSR_GS_BASE,%ecx
990 rdmsr
991 testl %edx,%edx
992 js 1f
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100993 SWAPGS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 xorl %ebx,%ebx
Jan Beulichb556b352006-01-11 22:43:00 +01009951:
996 .if \ist
997 movq %gs:pda_data_offset, %rbp
998 .endif
Alexander van Heukelum7e61a792008-09-26 14:03:03 +0200999 .if \irqtrace
1000 TRACE_IRQS_OFF
1001 .endif
Jan Beulichb556b352006-01-11 22:43:00 +01001002 movq %rsp,%rdi
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 movq ORIG_RAX(%rsp),%rsi
1004 movq $-1,ORIG_RAX(%rsp)
Jan Beulichb556b352006-01-11 22:43:00 +01001005 .if \ist
Andi Kleen5f8efbb2006-01-16 01:56:39 +01001006 subq $EXCEPTION_STKSZ, per_cpu__init_tss + TSS_ist + (\ist - 1) * 8(%rbp)
Jan Beulichb556b352006-01-11 22:43:00 +01001007 .endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 call \sym
Jan Beulichb556b352006-01-11 22:43:00 +01001009 .if \ist
Andi Kleen5f8efbb2006-01-16 01:56:39 +01001010 addq $EXCEPTION_STKSZ, per_cpu__init_tss + TSS_ist + (\ist - 1) * 8(%rbp)
Jan Beulichb556b352006-01-11 22:43:00 +01001011 .endif
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001012 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -07001013 .if \irqtrace
1014 TRACE_IRQS_OFF
1015 .endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 .endm
Ingo Molnar2601e642006-07-03 00:24:45 -07001017
1018 /*
1019 * "Paranoid" exit path from exception stack.
1020 * Paranoid because this is used by NMIs and cannot take
1021 * any kernel state for granted.
1022 * We don't do kernel preemption checks here, because only
1023 * NMI should be common and it does not enable IRQs and
1024 * cannot get reschedule ticks.
1025 *
1026 * "trace" is 0 for the NMI handler only, because irq-tracing
1027 * is fundamentally NMI-unsafe. (we cannot change the soft and
1028 * hard flags at once, atomically)
1029 */
1030 .macro paranoidexit trace=1
1031 /* ebx: no swapgs flag */
1032paranoid_exit\trace:
1033 testl %ebx,%ebx /* swapgs needed? */
1034 jnz paranoid_restore\trace
1035 testl $3,CS(%rsp)
1036 jnz paranoid_userspace\trace
1037paranoid_swapgs\trace:
Andi Kleen7a0a2df2006-09-26 10:52:37 +02001038 .if \trace
Ingo Molnar2601e642006-07-03 00:24:45 -07001039 TRACE_IRQS_IRETQ 0
Andi Kleen7a0a2df2006-09-26 10:52:37 +02001040 .endif
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001041 SWAPGS_UNSAFE_STACK
Ingo Molnar2601e642006-07-03 00:24:45 -07001042paranoid_restore\trace:
1043 RESTORE_ALL 8
Ingo Molnar3701d8632008-02-09 23:24:08 +01001044 jmp irq_return
Ingo Molnar2601e642006-07-03 00:24:45 -07001045paranoid_userspace\trace:
1046 GET_THREAD_INFO(%rcx)
Glauber Costa26ccb8a2008-06-24 11:19:35 -03001047 movl TI_flags(%rcx),%ebx
Ingo Molnar2601e642006-07-03 00:24:45 -07001048 andl $_TIF_WORK_MASK,%ebx
1049 jz paranoid_swapgs\trace
1050 movq %rsp,%rdi /* &pt_regs */
1051 call sync_regs
1052 movq %rax,%rsp /* switch stack for scheduling */
1053 testl $_TIF_NEED_RESCHED,%ebx
1054 jnz paranoid_schedule\trace
1055 movl %ebx,%edx /* arg3: thread flags */
1056 .if \trace
1057 TRACE_IRQS_ON
1058 .endif
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001059 ENABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -07001060 xorl %esi,%esi /* arg2: oldset */
1061 movq %rsp,%rdi /* arg1: &pt_regs */
1062 call do_notify_resume
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001063 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -07001064 .if \trace
1065 TRACE_IRQS_OFF
1066 .endif
1067 jmp paranoid_userspace\trace
1068paranoid_schedule\trace:
1069 .if \trace
1070 TRACE_IRQS_ON
1071 .endif
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001072 ENABLE_INTERRUPTS(CLBR_ANY)
Ingo Molnar2601e642006-07-03 00:24:45 -07001073 call schedule
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001074 DISABLE_INTERRUPTS(CLBR_ANY)
Ingo Molnar2601e642006-07-03 00:24:45 -07001075 .if \trace
1076 TRACE_IRQS_OFF
1077 .endif
1078 jmp paranoid_userspace\trace
1079 CFI_ENDPROC
1080 .endm
1081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082/*
1083 * Exception entry point. This expects an error code/orig_rax on the stack
1084 * and the exception handler in %rax.
1085 */
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001086KPROBE_ENTRY(error_entry)
Jan Beulich7effaa82005-09-12 18:49:24 +02001087 _frame RDI
Jan Beulich37550902007-05-02 19:27:05 +02001088 CFI_REL_OFFSET rax,0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 /* rdi slot contains rax, oldrax contains error code */
1090 cld
1091 subq $14*8,%rsp
1092 CFI_ADJUST_CFA_OFFSET (14*8)
1093 movq %rsi,13*8(%rsp)
1094 CFI_REL_OFFSET rsi,RSI
1095 movq 14*8(%rsp),%rsi /* load rax from rdi slot */
Jan Beulich37550902007-05-02 19:27:05 +02001096 CFI_REGISTER rax,rsi
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 movq %rdx,12*8(%rsp)
1098 CFI_REL_OFFSET rdx,RDX
1099 movq %rcx,11*8(%rsp)
1100 CFI_REL_OFFSET rcx,RCX
1101 movq %rsi,10*8(%rsp) /* store rax */
1102 CFI_REL_OFFSET rax,RAX
1103 movq %r8, 9*8(%rsp)
1104 CFI_REL_OFFSET r8,R8
1105 movq %r9, 8*8(%rsp)
1106 CFI_REL_OFFSET r9,R9
1107 movq %r10,7*8(%rsp)
1108 CFI_REL_OFFSET r10,R10
1109 movq %r11,6*8(%rsp)
1110 CFI_REL_OFFSET r11,R11
1111 movq %rbx,5*8(%rsp)
1112 CFI_REL_OFFSET rbx,RBX
1113 movq %rbp,4*8(%rsp)
1114 CFI_REL_OFFSET rbp,RBP
1115 movq %r12,3*8(%rsp)
1116 CFI_REL_OFFSET r12,R12
1117 movq %r13,2*8(%rsp)
1118 CFI_REL_OFFSET r13,R13
1119 movq %r14,1*8(%rsp)
1120 CFI_REL_OFFSET r14,R14
1121 movq %r15,(%rsp)
1122 CFI_REL_OFFSET r15,R15
1123 xorl %ebx,%ebx
1124 testl $3,CS(%rsp)
1125 je error_kernelspace
1126error_swapgs:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001127 SWAPGS
Alexander van Heukelum6b11d4e2008-09-26 14:03:02 +02001128error_sti:
1129 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 movq %rdi,RDI(%rsp)
Jan Beulich37550902007-05-02 19:27:05 +02001131 CFI_REL_OFFSET rdi,RDI
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 movq %rsp,%rdi
1133 movq ORIG_RAX(%rsp),%rsi /* get error code */
1134 movq $-1,ORIG_RAX(%rsp)
1135 call *%rax
Peter Zijlstra10cd7062007-10-11 22:11:12 +02001136 /* ebx: no swapgs flag (1: don't need swapgs, 0: need it) */
1137error_exit:
1138 movl %ebx,%eax
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 RESTORE_REST
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001140 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -07001141 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 GET_THREAD_INFO(%rcx)
1143 testl %eax,%eax
1144 jne retint_kernel
Peter Zijlstra10cd7062007-10-11 22:11:12 +02001145 LOCKDEP_SYS_EXIT_IRQ
Glauber Costa26ccb8a2008-06-24 11:19:35 -03001146 movl TI_flags(%rcx),%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 movl $_TIF_WORK_MASK,%edi
1148 andl %edi,%edx
1149 jnz retint_careful
Peter Zijlstra10cd7062007-10-11 22:11:12 +02001150 jmp retint_swapgs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 CFI_ENDPROC
1152
1153error_kernelspace:
1154 incl %ebx
1155 /* There are two places in the kernel that can potentially fault with
1156 usergs. Handle them here. The exception handlers after
1157 iret run with kernel gs again, so don't set the user space flag.
1158 B stepping K8s sometimes report an truncated RIP for IRET
1159 exceptions returning to compat mode. Check for these here too. */
Vegard Nossum9d8ad5d2008-06-27 17:22:17 +02001160 leaq irq_return(%rip),%rcx
1161 cmpq %rcx,RIP(%rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 je error_swapgs
Vegard Nossum9d8ad5d2008-06-27 17:22:17 +02001163 movl %ecx,%ecx /* zero extend */
1164 cmpq %rcx,RIP(%rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 je error_swapgs
1166 cmpq $gs_change,RIP(%rsp)
1167 je error_swapgs
1168 jmp error_sti
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001169KPROBE_END(error_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 /* Reload gs selector with exception handling */
1172 /* edi: new selector */
Jeremy Fitzhardinge9f9d4892008-06-25 00:19:32 -04001173ENTRY(native_load_gs_index)
Jan Beulich7effaa82005-09-12 18:49:24 +02001174 CFI_STARTPROC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 pushf
Jan Beulich7effaa82005-09-12 18:49:24 +02001176 CFI_ADJUST_CFA_OFFSET 8
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001177 DISABLE_INTERRUPTS(CLBR_ANY | ~(CLBR_RDI))
1178 SWAPGS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179gs_change:
1180 movl %edi,%gs
11812: mfence /* workaround */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001182 SWAPGS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 popf
Jan Beulich7effaa82005-09-12 18:49:24 +02001184 CFI_ADJUST_CFA_OFFSET -8
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 ret
Jan Beulich7effaa82005-09-12 18:49:24 +02001186 CFI_ENDPROC
Jeremy Fitzhardinge9f9d4892008-06-25 00:19:32 -04001187ENDPROC(native_load_gs_index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 .section __ex_table,"a"
1190 .align 8
1191 .quad gs_change,bad_gs
1192 .previous
1193 .section .fixup,"ax"
1194 /* running with kernelgs */
1195bad_gs:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001196 SWAPGS /* switch back to user gs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 xorl %eax,%eax
1198 movl %eax,%gs
1199 jmp 2b
1200 .previous
1201
1202/*
1203 * Create a kernel thread.
1204 *
1205 * C extern interface:
1206 * extern long kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
1207 *
1208 * asm input arguments:
1209 * rdi: fn, rsi: arg, rdx: flags
1210 */
1211ENTRY(kernel_thread)
1212 CFI_STARTPROC
1213 FAKE_STACK_FRAME $child_rip
1214 SAVE_ALL
1215
1216 # rdi: flags, rsi: usp, rdx: will be &pt_regs
1217 movq %rdx,%rdi
1218 orq kernel_thread_flags(%rip),%rdi
1219 movq $-1, %rsi
1220 movq %rsp, %rdx
1221
1222 xorl %r8d,%r8d
1223 xorl %r9d,%r9d
1224
1225 # clone now
1226 call do_fork
1227 movq %rax,RAX(%rsp)
1228 xorl %edi,%edi
1229
1230 /*
1231 * It isn't worth to check for reschedule here,
1232 * so internally to the x86_64 port you can rely on kernel_thread()
1233 * not to reschedule the child before returning, this avoids the need
1234 * of hacks for example to fork off the per-CPU idle tasks.
1235 * [Hopefully no generic code relies on the reschedule -AK]
1236 */
1237 RESTORE_ALL
1238 UNFAKE_STACK_FRAME
1239 ret
1240 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001241ENDPROC(kernel_thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243child_rip:
Andi Kleenc05991e2006-08-30 19:37:08 +02001244 pushq $0 # fake return address
1245 CFI_STARTPROC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 /*
1247 * Here we are in the child and the registers are set as they were
1248 * at kernel_thread() invocation in the parent.
1249 */
1250 movq %rdi, %rax
1251 movq %rsi, %rdi
1252 call *%rax
1253 # exit
Andrey Mirkin1c5b5cf2007-10-17 18:04:33 +02001254 mov %eax, %edi
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 call do_exit
Andi Kleenc05991e2006-08-30 19:37:08 +02001256 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001257ENDPROC(child_rip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259/*
1260 * execve(). This function needs to use IRET, not SYSRET, to set up all state properly.
1261 *
1262 * C extern interface:
1263 * extern long execve(char *name, char **argv, char **envp)
1264 *
1265 * asm input arguments:
1266 * rdi: name, rsi: argv, rdx: envp
1267 *
1268 * We want to fallback into:
Ingo Molnar5d119b22008-02-26 12:55:57 +01001269 * extern long sys_execve(char *name, char **argv,char **envp, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 *
1271 * do_sys_execve asm fallback arguments:
Ingo Molnar5d119b22008-02-26 12:55:57 +01001272 * rdi: name, rsi: argv, rdx: envp, rcx: fake frame on the stack
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 */
Arnd Bergmann3db03b42006-10-02 02:18:31 -07001274ENTRY(kernel_execve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 CFI_STARTPROC
1276 FAKE_STACK_FRAME $0
1277 SAVE_ALL
Ingo Molnar5d119b22008-02-26 12:55:57 +01001278 movq %rsp,%rcx
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 call sys_execve
1280 movq %rax, RAX(%rsp)
1281 RESTORE_REST
1282 testq %rax,%rax
1283 je int_ret_from_sys_call
1284 RESTORE_ARGS
1285 UNFAKE_STACK_FRAME
1286 ret
1287 CFI_ENDPROC
Arnd Bergmann3db03b42006-10-02 02:18:31 -07001288ENDPROC(kernel_execve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001290KPROBE_ENTRY(page_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 errorentry do_page_fault
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001292KPROBE_END(page_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294ENTRY(coprocessor_error)
1295 zeroentry do_coprocessor_error
Jan Beulich4b787e02006-06-26 13:56:55 +02001296END(coprocessor_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298ENTRY(simd_coprocessor_error)
1299 zeroentry do_simd_coprocessor_error
Jan Beulich4b787e02006-06-26 13:56:55 +02001300END(simd_coprocessor_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302ENTRY(device_not_available)
Alexander van Heukelume407d6202008-09-30 18:41:36 +02001303 zeroentry do_device_not_available
Jan Beulich4b787e02006-06-26 13:56:55 +02001304END(device_not_available)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 /* runs on exception stack */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001307KPROBE_ENTRY(debug)
Jan Beulich7effaa82005-09-12 18:49:24 +02001308 INTR_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001309 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 pushq $0
1311 CFI_ADJUST_CFA_OFFSET 8
Andi Kleen5f8efbb2006-01-16 01:56:39 +01001312 paranoidentry do_debug, DEBUG_STACK
Ingo Molnar2601e642006-07-03 00:24:45 -07001313 paranoidexit
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001314KPROBE_END(debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 /* runs on exception stack */
Andi Kleeneddb6fb2006-02-03 21:50:41 +01001317KPROBE_ENTRY(nmi)
Jan Beulich7effaa82005-09-12 18:49:24 +02001318 INTR_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001319 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 pushq $-1
Jan Beulich7effaa82005-09-12 18:49:24 +02001321 CFI_ADJUST_CFA_OFFSET 8
Ingo Molnar2601e642006-07-03 00:24:45 -07001322 paranoidentry do_nmi, 0, 0
1323#ifdef CONFIG_TRACE_IRQFLAGS
1324 paranoidexit 0
1325#else
1326 jmp paranoid_exit1
1327 CFI_ENDPROC
1328#endif
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001329KPROBE_END(nmi)
Andi Kleen6fefb0d2005-04-16 15:25:03 -07001330
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001331KPROBE_ENTRY(int3)
Jan Beulichb556b352006-01-11 22:43:00 +01001332 INTR_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001333 PARAVIRT_ADJUST_EXCEPTION_FRAME
Jan Beulichb556b352006-01-11 22:43:00 +01001334 pushq $0
1335 CFI_ADJUST_CFA_OFFSET 8
Andi Kleen5f8efbb2006-01-16 01:56:39 +01001336 paranoidentry do_int3, DEBUG_STACK
Ingo Molnar2601e642006-07-03 00:24:45 -07001337 jmp paranoid_exit1
Jan Beulichb556b352006-01-11 22:43:00 +01001338 CFI_ENDPROC
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001339KPROBE_END(int3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341ENTRY(overflow)
1342 zeroentry do_overflow
Jan Beulich4b787e02006-06-26 13:56:55 +02001343END(overflow)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345ENTRY(bounds)
1346 zeroentry do_bounds
Jan Beulich4b787e02006-06-26 13:56:55 +02001347END(bounds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349ENTRY(invalid_op)
1350 zeroentry do_invalid_op
Jan Beulich4b787e02006-06-26 13:56:55 +02001351END(invalid_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353ENTRY(coprocessor_segment_overrun)
1354 zeroentry do_coprocessor_segment_overrun
Jan Beulich4b787e02006-06-26 13:56:55 +02001355END(coprocessor_segment_overrun)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 /* runs on exception stack */
1358ENTRY(double_fault)
Jan Beulich7effaa82005-09-12 18:49:24 +02001359 XCPT_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001360 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 paranoidentry do_double_fault
Ingo Molnar2601e642006-07-03 00:24:45 -07001362 jmp paranoid_exit1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001364END(double_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366ENTRY(invalid_TSS)
1367 errorentry do_invalid_TSS
Jan Beulich4b787e02006-06-26 13:56:55 +02001368END(invalid_TSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370ENTRY(segment_not_present)
1371 errorentry do_segment_not_present
Jan Beulich4b787e02006-06-26 13:56:55 +02001372END(segment_not_present)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 /* runs on exception stack */
1375ENTRY(stack_segment)
Jan Beulich7effaa82005-09-12 18:49:24 +02001376 XCPT_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001377 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 paranoidentry do_stack_segment
Ingo Molnar2601e642006-07-03 00:24:45 -07001379 jmp paranoid_exit1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001381END(stack_segment)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001383KPROBE_ENTRY(general_protection)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 errorentry do_general_protection
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001385KPROBE_END(general_protection)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387ENTRY(alignment_check)
1388 errorentry do_alignment_check
Jan Beulich4b787e02006-06-26 13:56:55 +02001389END(alignment_check)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391ENTRY(divide_error)
1392 zeroentry do_divide_error
Jan Beulich4b787e02006-06-26 13:56:55 +02001393END(divide_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395ENTRY(spurious_interrupt_bug)
1396 zeroentry do_spurious_interrupt_bug
Jan Beulich4b787e02006-06-26 13:56:55 +02001397END(spurious_interrupt_bug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399#ifdef CONFIG_X86_MCE
1400 /* runs on exception stack */
1401ENTRY(machine_check)
Jan Beulich7effaa82005-09-12 18:49:24 +02001402 INTR_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001403 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 pushq $0
1405 CFI_ADJUST_CFA_OFFSET 8
1406 paranoidentry do_machine_check
Ingo Molnar2601e642006-07-03 00:24:45 -07001407 jmp paranoid_exit1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001409END(machine_check)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410#endif
1411
Andi Kleen26995002006-08-02 22:37:28 +02001412/* Call softirq on interrupt stack. Interrupts are off. */
Andi Kleened6b6762005-07-28 21:15:49 -07001413ENTRY(call_softirq)
Jan Beulich7effaa82005-09-12 18:49:24 +02001414 CFI_STARTPROC
Andi Kleen26995002006-08-02 22:37:28 +02001415 push %rbp
1416 CFI_ADJUST_CFA_OFFSET 8
1417 CFI_REL_OFFSET rbp,0
1418 mov %rsp,%rbp
1419 CFI_DEF_CFA_REGISTER rbp
Andi Kleened6b6762005-07-28 21:15:49 -07001420 incl %gs:pda_irqcount
Andi Kleen26995002006-08-02 22:37:28 +02001421 cmove %gs:pda_irqstackptr,%rsp
1422 push %rbp # backlink for old unwinder
Andi Kleened6b6762005-07-28 21:15:49 -07001423 call __do_softirq
Andi Kleen26995002006-08-02 22:37:28 +02001424 leaveq
Jan Beulich7effaa82005-09-12 18:49:24 +02001425 CFI_DEF_CFA_REGISTER rsp
Andi Kleen26995002006-08-02 22:37:28 +02001426 CFI_ADJUST_CFA_OFFSET -8
Andi Kleened6b6762005-07-28 21:15:49 -07001427 decl %gs:pda_irqcount
Andi Kleened6b6762005-07-28 21:15:49 -07001428 ret
Jan Beulich7effaa82005-09-12 18:49:24 +02001429 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001430ENDPROC(call_softirq)
Andi Kleen75154f42007-06-23 02:29:25 +02001431
1432KPROBE_ENTRY(ignore_sysret)
1433 CFI_STARTPROC
1434 mov $-ENOSYS,%eax
1435 sysret
1436 CFI_ENDPROC
1437ENDPROC(ignore_sysret)
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001438
1439#ifdef CONFIG_XEN
1440ENTRY(xen_hypervisor_callback)
1441 zeroentry xen_do_hypervisor_callback
1442END(xen_hypervisor_callback)
1443
1444/*
1445# A note on the "critical region" in our callback handler.
1446# We want to avoid stacking callback handlers due to events occurring
1447# during handling of the last event. To do this, we keep events disabled
1448# until we've done all processing. HOWEVER, we must enable events before
1449# popping the stack frame (can't be done atomically) and so it would still
1450# be possible to get enough handler activations to overflow the stack.
1451# Although unlikely, bugs of that kind are hard to track down, so we'd
1452# like to avoid the possibility.
1453# So, on entry to the handler we detect whether we interrupted an
1454# existing activation in its critical region -- if so, we pop the current
1455# activation and restart the handler using the previous one.
1456*/
1457ENTRY(xen_do_hypervisor_callback) # do_hypervisor_callback(struct *pt_regs)
1458 CFI_STARTPROC
1459/* Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
1460 see the correct pointer to the pt_regs */
1461 movq %rdi, %rsp # we don't return, adjust the stack frame
1462 CFI_ENDPROC
1463 CFI_DEFAULT_STACK
146411: incl %gs:pda_irqcount
1465 movq %rsp,%rbp
1466 CFI_DEF_CFA_REGISTER rbp
1467 cmovzq %gs:pda_irqstackptr,%rsp
1468 pushq %rbp # backlink for old unwinder
1469 call xen_evtchn_do_upcall
1470 popq %rsp
1471 CFI_DEF_CFA_REGISTER rsp
1472 decl %gs:pda_irqcount
1473 jmp error_exit
1474 CFI_ENDPROC
1475END(do_hypervisor_callback)
1476
1477/*
1478# Hypervisor uses this for application faults while it executes.
1479# We get here for two reasons:
1480# 1. Fault while reloading DS, ES, FS or GS
1481# 2. Fault while executing IRET
1482# Category 1 we do not need to fix up as Xen has already reloaded all segment
1483# registers that could be reloaded and zeroed the others.
1484# Category 2 we fix up by killing the current process. We cannot use the
1485# normal Linux return path in this case because if we use the IRET hypercall
1486# to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1487# We distinguish between categories by comparing each saved segment register
1488# with its current contents: any discrepancy means we in category 1.
1489*/
1490ENTRY(xen_failsafe_callback)
Jeremy Fitzhardinge4a5c3e72008-07-08 15:07:09 -07001491 framesz = (RIP-0x30) /* workaround buggy gas */
1492 _frame framesz
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001493 CFI_REL_OFFSET rcx, 0
1494 CFI_REL_OFFSET r11, 8
1495 movw %ds,%cx
1496 cmpw %cx,0x10(%rsp)
1497 CFI_REMEMBER_STATE
1498 jne 1f
1499 movw %es,%cx
1500 cmpw %cx,0x18(%rsp)
1501 jne 1f
1502 movw %fs,%cx
1503 cmpw %cx,0x20(%rsp)
1504 jne 1f
1505 movw %gs,%cx
1506 cmpw %cx,0x28(%rsp)
1507 jne 1f
1508 /* All segments match their saved values => Category 2 (Bad IRET). */
1509 movq (%rsp),%rcx
1510 CFI_RESTORE rcx
1511 movq 8(%rsp),%r11
1512 CFI_RESTORE r11
1513 addq $0x30,%rsp
1514 CFI_ADJUST_CFA_OFFSET -0x30
Jeremy Fitzhardinge4a5c3e72008-07-08 15:07:09 -07001515 pushq $0
1516 CFI_ADJUST_CFA_OFFSET 8
1517 pushq %r11
1518 CFI_ADJUST_CFA_OFFSET 8
1519 pushq %rcx
1520 CFI_ADJUST_CFA_OFFSET 8
1521 jmp general_protection
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001522 CFI_RESTORE_STATE
15231: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
1524 movq (%rsp),%rcx
1525 CFI_RESTORE rcx
1526 movq 8(%rsp),%r11
1527 CFI_RESTORE r11
1528 addq $0x30,%rsp
1529 CFI_ADJUST_CFA_OFFSET -0x30
1530 pushq $0
1531 CFI_ADJUST_CFA_OFFSET 8
1532 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 */