blob: 2aa0526ac30e9f8c7936c31f62959aedc39b01ff [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
123#endif
124
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200125.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
Frederic Weisbecker48d68b22008-12-02 00:20:39 +0100160#ifdef CONFIG_FUNCTION_GRAPH_TRACER
161ENTRY(ftrace_graph_caller)
162 cmpl $0, function_trace_stop
163 jne ftrace_stub
164
165 subq $0x38, %rsp
166 movq %rax, (%rsp)
167 movq %rcx, 8(%rsp)
168 movq %rdx, 16(%rsp)
169 movq %rsi, 24(%rsp)
170 movq %rdi, 32(%rsp)
171 movq %r8, 40(%rsp)
172 movq %r9, 48(%rsp)
173
174 leaq 8(%rbp), %rdi
175 movq 0x38(%rsp), %rsi
176
177 call prepare_ftrace_return
178
179 movq 48(%rsp), %r9
180 movq 40(%rsp), %r8
181 movq 32(%rsp), %rdi
182 movq 24(%rsp), %rsi
183 movq 16(%rsp), %rdx
184 movq 8(%rsp), %rcx
185 movq (%rsp), %rax
186 addq $0x38, %rsp
187 retq
188END(ftrace_graph_caller)
189
190
191.globl return_to_handler
192return_to_handler:
193 subq $80, %rsp
194
195 movq %rax, (%rsp)
196 movq %rcx, 8(%rsp)
197 movq %rdx, 16(%rsp)
198 movq %rsi, 24(%rsp)
199 movq %rdi, 32(%rsp)
200 movq %r8, 40(%rsp)
201 movq %r9, 48(%rsp)
202 movq %r10, 56(%rsp)
203 movq %r11, 64(%rsp)
204
205 call ftrace_return_to_handler
206
207 movq %rax, 72(%rsp)
208 movq 64(%rsp), %r11
209 movq 56(%rsp), %r10
210 movq 48(%rsp), %r9
211 movq 40(%rsp), %r8
212 movq 32(%rsp), %rdi
213 movq 24(%rsp), %rsi
214 movq 16(%rsp), %rdx
215 movq 8(%rsp), %rcx
216 movq (%rsp), %rax
217 addq $72, %rsp
218 retq
219#endif
220
221
Andi Kleendc37db42005-04-16 15:25:05 -0700222#ifndef CONFIG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223#define retint_kernel retint_restore_args
224#endif
Ingo Molnar2601e642006-07-03 00:24:45 -0700225
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100226#ifdef CONFIG_PARAVIRT
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -0400227ENTRY(native_usergs_sysret64)
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100228 swapgs
229 sysretq
230#endif /* CONFIG_PARAVIRT */
231
Ingo Molnar2601e642006-07-03 00:24:45 -0700232
233.macro TRACE_IRQS_IRETQ offset=ARGOFFSET
234#ifdef CONFIG_TRACE_IRQFLAGS
235 bt $9,EFLAGS-\offset(%rsp) /* interrupts off? */
236 jnc 1f
237 TRACE_IRQS_ON
2381:
239#endif
240.endm
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242/*
243 * C code is not supposed to know about undefined top of stack. Every time
244 * a C function with an pt_regs argument is called from the SYSCALL based
245 * fast path FIXUP_TOP_OF_STACK is needed.
246 * RESTORE_TOP_OF_STACK syncs the syscall state after any possible ptregs
247 * manipulation.
248 */
249
250 /* %rsp:at FRAMEEND */
251 .macro FIXUP_TOP_OF_STACK tmp
252 movq %gs:pda_oldrsp,\tmp
253 movq \tmp,RSP(%rsp)
254 movq $__USER_DS,SS(%rsp)
255 movq $__USER_CS,CS(%rsp)
256 movq $-1,RCX(%rsp)
257 movq R11(%rsp),\tmp /* get eflags */
258 movq \tmp,EFLAGS(%rsp)
259 .endm
260
261 .macro RESTORE_TOP_OF_STACK tmp,offset=0
262 movq RSP-\offset(%rsp),\tmp
263 movq \tmp,%gs:pda_oldrsp
264 movq EFLAGS-\offset(%rsp),\tmp
265 movq \tmp,R11-\offset(%rsp)
266 .endm
267
268 .macro FAKE_STACK_FRAME child_rip
269 /* push in order ss, rsp, eflags, cs, rip */
Andi Kleen3829ee62005-07-28 21:15:48 -0700270 xorl %eax, %eax
Jeremy Fitzhardingee04e0a62008-06-25 00:19:25 -0400271 pushq $__KERNEL_DS /* ss */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 CFI_ADJUST_CFA_OFFSET 8
Jan Beulich7effaa82005-09-12 18:49:24 +0200273 /*CFI_REL_OFFSET ss,0*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 pushq %rax /* rsp */
275 CFI_ADJUST_CFA_OFFSET 8
Jan Beulich7effaa82005-09-12 18:49:24 +0200276 CFI_REL_OFFSET rsp,0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 pushq $(1<<9) /* eflags - interrupts on */
278 CFI_ADJUST_CFA_OFFSET 8
Jan Beulich7effaa82005-09-12 18:49:24 +0200279 /*CFI_REL_OFFSET rflags,0*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 pushq $__KERNEL_CS /* cs */
281 CFI_ADJUST_CFA_OFFSET 8
Jan Beulich7effaa82005-09-12 18:49:24 +0200282 /*CFI_REL_OFFSET cs,0*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 pushq \child_rip /* rip */
284 CFI_ADJUST_CFA_OFFSET 8
Jan Beulich7effaa82005-09-12 18:49:24 +0200285 CFI_REL_OFFSET rip,0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 pushq %rax /* orig rax */
287 CFI_ADJUST_CFA_OFFSET 8
288 .endm
289
290 .macro UNFAKE_STACK_FRAME
291 addq $8*6, %rsp
292 CFI_ADJUST_CFA_OFFSET -(6*8)
293 .endm
294
Jan Beulich7effaa82005-09-12 18:49:24 +0200295 .macro CFI_DEFAULT_STACK start=1
296 .if \start
297 CFI_STARTPROC simple
Jan Beulichadf14232006-09-26 10:52:41 +0200298 CFI_SIGNAL_FRAME
Jan Beulich7effaa82005-09-12 18:49:24 +0200299 CFI_DEF_CFA rsp,SS+8
300 .else
301 CFI_DEF_CFA_OFFSET SS+8
302 .endif
303 CFI_REL_OFFSET r15,R15
304 CFI_REL_OFFSET r14,R14
305 CFI_REL_OFFSET r13,R13
306 CFI_REL_OFFSET r12,R12
307 CFI_REL_OFFSET rbp,RBP
308 CFI_REL_OFFSET rbx,RBX
309 CFI_REL_OFFSET r11,R11
310 CFI_REL_OFFSET r10,R10
311 CFI_REL_OFFSET r9,R9
312 CFI_REL_OFFSET r8,R8
313 CFI_REL_OFFSET rax,RAX
314 CFI_REL_OFFSET rcx,RCX
315 CFI_REL_OFFSET rdx,RDX
316 CFI_REL_OFFSET rsi,RSI
317 CFI_REL_OFFSET rdi,RDI
318 CFI_REL_OFFSET rip,RIP
319 /*CFI_REL_OFFSET cs,CS*/
320 /*CFI_REL_OFFSET rflags,EFLAGS*/
321 CFI_REL_OFFSET rsp,RSP
322 /*CFI_REL_OFFSET ss,SS*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 .endm
324/*
325 * A newly forked process directly context switches into this.
326 */
327/* rdi: prev */
328ENTRY(ret_from_fork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 CFI_DEFAULT_STACK
Andi Kleen658fdbe2006-09-26 10:52:41 +0200330 push kernel_eflags(%rip)
Alexander van Heukelume0a5a5d2008-07-22 18:14:16 +0200331 CFI_ADJUST_CFA_OFFSET 8
Andi Kleen658fdbe2006-09-26 10:52:41 +0200332 popf # reset kernel eflags
Alexander van Heukelume0a5a5d2008-07-22 18:14:16 +0200333 CFI_ADJUST_CFA_OFFSET -8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 call schedule_tail
335 GET_THREAD_INFO(%rcx)
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300336 testl $(_TIF_SYSCALL_TRACE|_TIF_SYSCALL_AUDIT),TI_flags(%rcx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 jnz rff_trace
338rff_action:
339 RESTORE_REST
340 testl $3,CS-ARGOFFSET(%rsp) # from kernel_thread?
341 je int_ret_from_sys_call
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300342 testl $_TIF_IA32,TI_flags(%rcx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 jnz int_ret_from_sys_call
344 RESTORE_TOP_OF_STACK %rdi,ARGOFFSET
345 jmp ret_from_sys_call
346rff_trace:
347 movq %rsp,%rdi
348 call syscall_trace_leave
349 GET_THREAD_INFO(%rcx)
350 jmp rff_action
351 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200352END(ret_from_fork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354/*
355 * System call entry. Upto 6 arguments in registers are supported.
356 *
357 * SYSCALL does not save anything on the stack and does not change the
358 * stack pointer.
359 */
360
361/*
362 * Register setup:
363 * rax system call number
364 * rdi arg0
365 * rcx return address for syscall/sysret, C arg3
366 * rsi arg1
367 * rdx arg2
368 * r10 arg3 (--> moved to rcx for C)
369 * r8 arg4
370 * r9 arg5
371 * r11 eflags for syscall/sysret, temporary for C
372 * r12-r15,rbp,rbx saved by C code, not touched.
373 *
374 * Interrupts are off on entry.
375 * Only called from user space.
376 *
377 * XXX if we had a free scratch register we could save the RSP into the stack frame
378 * and report it properly in ps. Unfortunately we haven't.
Andi Kleen7bf36bb2006-04-07 19:50:00 +0200379 *
380 * When user can change the frames always force IRET. That is because
381 * it deals with uncanonical addresses better. SYSRET has trouble
382 * with them due to bugs in both AMD and Intel CPUs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 */
384
385ENTRY(system_call)
Jan Beulich7effaa82005-09-12 18:49:24 +0200386 CFI_STARTPROC simple
Jan Beulichadf14232006-09-26 10:52:41 +0200387 CFI_SIGNAL_FRAME
Jan Beulichdffead42006-06-26 13:57:38 +0200388 CFI_DEF_CFA rsp,PDA_STACKOFFSET
Jan Beulich7effaa82005-09-12 18:49:24 +0200389 CFI_REGISTER rip,rcx
390 /*CFI_REGISTER rflags,r11*/
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100391 SWAPGS_UNSAFE_STACK
392 /*
393 * A hypervisor implementation might want to use a label
394 * after the swapgs, so that it can do the swapgs
395 * for the guest and jump here on syscall.
396 */
397ENTRY(system_call_after_swapgs)
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 movq %rsp,%gs:pda_oldrsp
400 movq %gs:pda_kernelstack,%rsp
Ingo Molnar2601e642006-07-03 00:24:45 -0700401 /*
402 * No need to follow this irqs off/on section - it's straight
403 * and short:
404 */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100405 ENABLE_INTERRUPTS(CLBR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 SAVE_ARGS 8,1
407 movq %rax,ORIG_RAX-ARGOFFSET(%rsp)
Jan Beulich7effaa82005-09-12 18:49:24 +0200408 movq %rcx,RIP-ARGOFFSET(%rsp)
409 CFI_REL_OFFSET rip,RIP-ARGOFFSET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 GET_THREAD_INFO(%rcx)
Roland McGrathd4d67152008-07-09 02:38:07 -0700411 testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags(%rcx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 jnz tracesys
Roland McGrath86a1c342008-06-23 15:37:04 -0700413system_call_fastpath:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 cmpq $__NR_syscall_max,%rax
415 ja badsys
416 movq %r10,%rcx
417 call *sys_call_table(,%rax,8) # XXX: rip relative
418 movq %rax,RAX-ARGOFFSET(%rsp)
419/*
420 * Syscall return path ending with SYSRET (fast path)
421 * Has incomplete stack frame and undefined top of stack.
422 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423ret_from_sys_call:
Andi Kleen11b854b2005-04-16 15:25:02 -0700424 movl $_TIF_ALLWORK_MASK,%edi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 /* edi: flagmask */
426sysret_check:
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200427 LOCKDEP_SYS_EXIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 GET_THREAD_INFO(%rcx)
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100429 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700430 TRACE_IRQS_OFF
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300431 movl TI_flags(%rcx),%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 andl %edi,%edx
433 jnz sysret_careful
Jan Beulichbcddc012006-12-07 02:14:02 +0100434 CFI_REMEMBER_STATE
Ingo Molnar2601e642006-07-03 00:24:45 -0700435 /*
436 * sysretq will re-enable interrupts:
437 */
438 TRACE_IRQS_ON
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 movq RIP-ARGOFFSET(%rsp),%rcx
Jan Beulich7effaa82005-09-12 18:49:24 +0200440 CFI_REGISTER rip,rcx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 RESTORE_ARGS 0,-ARG_SKIP,1
Jan Beulich7effaa82005-09-12 18:49:24 +0200442 /*CFI_REGISTER rflags,r11*/
Jeremy Fitzhardingec7245da2008-06-25 00:19:27 -0400443 movq %gs:pda_oldrsp, %rsp
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -0400444 USERGS_SYSRET64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Jan Beulichbcddc012006-12-07 02:14:02 +0100446 CFI_RESTORE_STATE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 /* Handle reschedules */
448 /* edx: work, edi: workmask */
449sysret_careful:
450 bt $TIF_NEED_RESCHED,%edx
451 jnc sysret_signal
Ingo Molnar2601e642006-07-03 00:24:45 -0700452 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100453 ENABLE_INTERRUPTS(CLBR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 pushq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200455 CFI_ADJUST_CFA_OFFSET 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 call schedule
457 popq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200458 CFI_ADJUST_CFA_OFFSET -8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 jmp sysret_check
460
461 /* Handle a signal */
462sysret_signal:
Ingo Molnar2601e642006-07-03 00:24:45 -0700463 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100464 ENABLE_INTERRUPTS(CLBR_NONE)
Roland McGrath86a1c342008-06-23 15:37:04 -0700465#ifdef CONFIG_AUDITSYSCALL
466 bt $TIF_SYSCALL_AUDIT,%edx
467 jc sysret_audit
468#endif
Andi Kleen10ffdbb2005-05-16 21:53:19 -0700469 /* edx: work flags (arg3) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 leaq do_notify_resume(%rip),%rax
471 leaq -ARGOFFSET(%rsp),%rdi # &pt_regs -> arg1
472 xorl %esi,%esi # oldset -> arg2
473 call ptregscall_common
Roland McGrath15e8f342008-06-23 20:41:12 -0700474 movl $_TIF_WORK_MASK,%edi
Andi Kleen7bf36bb2006-04-07 19:50:00 +0200475 /* Use IRET because user could have changed frame. This
476 works because ptregscall_common has called FIXUP_TOP_OF_STACK. */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100477 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700478 TRACE_IRQS_OFF
Andi Kleen7bf36bb2006-04-07 19:50:00 +0200479 jmp int_with_check
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Jan Beulich7effaa82005-09-12 18:49:24 +0200481badsys:
482 movq $-ENOSYS,RAX-ARGOFFSET(%rsp)
483 jmp ret_from_sys_call
484
Roland McGrath86a1c342008-06-23 15:37:04 -0700485#ifdef CONFIG_AUDITSYSCALL
486 /*
487 * Fast path for syscall audit without full syscall trace.
488 * We just call audit_syscall_entry() directly, and then
489 * jump back to the normal fast path.
490 */
491auditsys:
492 movq %r10,%r9 /* 6th arg: 4th syscall arg */
493 movq %rdx,%r8 /* 5th arg: 3rd syscall arg */
494 movq %rsi,%rcx /* 4th arg: 2nd syscall arg */
495 movq %rdi,%rdx /* 3rd arg: 1st syscall arg */
496 movq %rax,%rsi /* 2nd arg: syscall number */
497 movl $AUDIT_ARCH_X86_64,%edi /* 1st arg: audit arch */
498 call audit_syscall_entry
499 LOAD_ARGS 0 /* reload call-clobbered registers */
500 jmp system_call_fastpath
501
502 /*
503 * Return fast path for syscall audit. Call audit_syscall_exit()
504 * directly and then jump back to the fast path with TIF_SYSCALL_AUDIT
505 * masked off.
506 */
507sysret_audit:
508 movq %rax,%rsi /* second arg, syscall return value */
509 cmpq $0,%rax /* is it < 0? */
510 setl %al /* 1 if so, 0 if not */
511 movzbl %al,%edi /* zero-extend that into %edi */
512 inc %edi /* first arg, 0->1(AUDITSC_SUCCESS), 1->2(AUDITSC_FAILURE) */
513 call audit_syscall_exit
514 movl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT),%edi
515 jmp sysret_check
516#endif /* CONFIG_AUDITSYSCALL */
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 /* Do syscall tracing */
519tracesys:
Roland McGrath86a1c342008-06-23 15:37:04 -0700520#ifdef CONFIG_AUDITSYSCALL
521 testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT),TI_flags(%rcx)
522 jz auditsys
523#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 SAVE_REST
Roland McGratha31f8dd2008-03-16 21:59:11 -0700525 movq $-ENOSYS,RAX(%rsp) /* ptrace can change this for a bad syscall */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 FIXUP_TOP_OF_STACK %rdi
527 movq %rsp,%rdi
528 call syscall_trace_enter
Roland McGrathd4d67152008-07-09 02:38:07 -0700529 /*
530 * Reload arg registers from stack in case ptrace changed them.
531 * We don't reload %rax because syscall_trace_enter() returned
532 * the value it wants us to use in the table lookup.
533 */
534 LOAD_ARGS ARGOFFSET, 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 RESTORE_REST
536 cmpq $__NR_syscall_max,%rax
Roland McGratha31f8dd2008-03-16 21:59:11 -0700537 ja int_ret_from_sys_call /* RAX(%rsp) set to -ENOSYS above */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 movq %r10,%rcx /* fixup for C */
539 call *sys_call_table(,%rax,8)
Roland McGratha31f8dd2008-03-16 21:59:11 -0700540 movq %rax,RAX-ARGOFFSET(%rsp)
Andi Kleen7bf36bb2006-04-07 19:50:00 +0200541 /* Use IRET because user could have changed frame */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543/*
544 * Syscall return path ending with IRET.
545 * Has correct top of stack, but partial stack frame.
Jan Beulichbcddc012006-12-07 02:14:02 +0100546 */
547 .globl int_ret_from_sys_call
Roland McGrath5cbf1562008-06-24 01:13:31 -0700548 .globl int_with_check
Jan Beulichbcddc012006-12-07 02:14:02 +0100549int_ret_from_sys_call:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100550 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700551 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 testl $3,CS-ARGOFFSET(%rsp)
553 je retint_restore_args
554 movl $_TIF_ALLWORK_MASK,%edi
555 /* edi: mask to check */
556int_with_check:
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200557 LOCKDEP_SYS_EXIT_IRQ
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 GET_THREAD_INFO(%rcx)
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300559 movl TI_flags(%rcx),%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 andl %edi,%edx
561 jnz int_careful
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300562 andl $~TS_COMPAT,TI_status(%rcx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 jmp retint_swapgs
564
565 /* Either reschedule or signal or syscall exit tracking needed. */
566 /* First do a reschedule test. */
567 /* edx: work, edi: workmask */
568int_careful:
569 bt $TIF_NEED_RESCHED,%edx
570 jnc int_very_careful
Ingo Molnar2601e642006-07-03 00:24:45 -0700571 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100572 ENABLE_INTERRUPTS(CLBR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 pushq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200574 CFI_ADJUST_CFA_OFFSET 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 call schedule
576 popq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200577 CFI_ADJUST_CFA_OFFSET -8
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100578 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700579 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 jmp int_with_check
581
582 /* handle signals and tracing -- both require a full stack frame */
583int_very_careful:
Ingo Molnar2601e642006-07-03 00:24:45 -0700584 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100585 ENABLE_INTERRUPTS(CLBR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 SAVE_REST
587 /* Check for syscall exit trace */
Roland McGrathd4d67152008-07-09 02:38:07 -0700588 testl $_TIF_WORK_SYSCALL_EXIT,%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 jz int_signal
590 pushq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200591 CFI_ADJUST_CFA_OFFSET 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 leaq 8(%rsp),%rdi # &ptregs -> arg1
593 call syscall_trace_leave
594 popq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200595 CFI_ADJUST_CFA_OFFSET -8
Roland McGrathd4d67152008-07-09 02:38:07 -0700596 andl $~(_TIF_WORK_SYSCALL_EXIT|_TIF_SYSCALL_EMU),%edi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 jmp int_restore_rest
598
599int_signal:
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100600 testl $_TIF_DO_NOTIFY_MASK,%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 jz 1f
602 movq %rsp,%rdi # &ptregs -> arg1
603 xorl %esi,%esi # oldset -> arg2
604 call do_notify_resume
Roland McGratheca91e72008-07-10 14:50:39 -07006051: movl $_TIF_WORK_MASK,%edi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606int_restore_rest:
607 RESTORE_REST
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100608 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700609 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 jmp int_with_check
611 CFI_ENDPROC
Jan Beulichbcddc012006-12-07 02:14:02 +0100612END(system_call)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614/*
615 * Certain special system calls that need to save a complete full stack frame.
616 */
617
618 .macro PTREGSCALL label,func,arg
619 .globl \label
620\label:
621 leaq \func(%rip),%rax
622 leaq -ARGOFFSET+8(%rsp),\arg /* 8 for return address */
623 jmp ptregscall_common
Jan Beulich4b787e02006-06-26 13:56:55 +0200624END(\label)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 .endm
626
Jan Beulich7effaa82005-09-12 18:49:24 +0200627 CFI_STARTPROC
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 PTREGSCALL stub_clone, sys_clone, %r8
630 PTREGSCALL stub_fork, sys_fork, %rdi
631 PTREGSCALL stub_vfork, sys_vfork, %rdi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 PTREGSCALL stub_sigaltstack, sys_sigaltstack, %rdx
633 PTREGSCALL stub_iopl, sys_iopl, %rsi
634
635ENTRY(ptregscall_common)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 popq %r11
Jan Beulich7effaa82005-09-12 18:49:24 +0200637 CFI_ADJUST_CFA_OFFSET -8
638 CFI_REGISTER rip, r11
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 SAVE_REST
640 movq %r11, %r15
Jan Beulich7effaa82005-09-12 18:49:24 +0200641 CFI_REGISTER rip, r15
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 FIXUP_TOP_OF_STACK %r11
643 call *%rax
644 RESTORE_TOP_OF_STACK %r11
645 movq %r15, %r11
Jan Beulich7effaa82005-09-12 18:49:24 +0200646 CFI_REGISTER rip, r11
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 RESTORE_REST
648 pushq %r11
Jan Beulich7effaa82005-09-12 18:49:24 +0200649 CFI_ADJUST_CFA_OFFSET 8
650 CFI_REL_OFFSET rip, 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 ret
652 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200653END(ptregscall_common)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655ENTRY(stub_execve)
656 CFI_STARTPROC
657 popq %r11
Jan Beulich7effaa82005-09-12 18:49:24 +0200658 CFI_ADJUST_CFA_OFFSET -8
659 CFI_REGISTER rip, r11
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 SAVE_REST
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 FIXUP_TOP_OF_STACK %r11
Ingo Molnar5d119b22008-02-26 12:55:57 +0100662 movq %rsp, %rcx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 call sys_execve
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 RESTORE_TOP_OF_STACK %r11
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 movq %rax,RAX(%rsp)
666 RESTORE_REST
667 jmp int_ret_from_sys_call
668 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200669END(stub_execve)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671/*
672 * sigreturn is special because it needs to restore all registers on return.
673 * This cannot be done with SYSRET, so use the IRET return path instead.
674 */
675ENTRY(stub_rt_sigreturn)
676 CFI_STARTPROC
Jan Beulich7effaa82005-09-12 18:49:24 +0200677 addq $8, %rsp
678 CFI_ADJUST_CFA_OFFSET -8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 SAVE_REST
680 movq %rsp,%rdi
681 FIXUP_TOP_OF_STACK %r11
682 call sys_rt_sigreturn
683 movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer
684 RESTORE_REST
685 jmp int_ret_from_sys_call
686 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200687END(stub_rt_sigreturn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Jan Beulich7effaa82005-09-12 18:49:24 +0200689/*
690 * initial frame state for interrupts and exceptions
691 */
692 .macro _frame ref
693 CFI_STARTPROC simple
Jan Beulichadf14232006-09-26 10:52:41 +0200694 CFI_SIGNAL_FRAME
Jan Beulich7effaa82005-09-12 18:49:24 +0200695 CFI_DEF_CFA rsp,SS+8-\ref
696 /*CFI_REL_OFFSET ss,SS-\ref*/
697 CFI_REL_OFFSET rsp,RSP-\ref
698 /*CFI_REL_OFFSET rflags,EFLAGS-\ref*/
699 /*CFI_REL_OFFSET cs,CS-\ref*/
700 CFI_REL_OFFSET rip,RIP-\ref
701 .endm
702
703/* initial frame state for interrupts (and exceptions without error code) */
704#define INTR_FRAME _frame RIP
705/* initial frame state for exceptions with error code (and interrupts with
706 vector already pushed) */
707#define XCPT_FRAME _frame ORIG_RAX
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709/*
710 * Interrupt entry/exit.
711 *
712 * Interrupt entry points save only callee clobbered registers in fast path.
713 *
714 * Entry runs with interrupts off.
715 */
716
717/* 0(%rsp): interrupt number */
718 .macro interrupt func
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 cld
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 SAVE_ARGS
721 leaq -ARGOFFSET(%rsp),%rdi # arg1 for handler
Jan Beulich1de9c3f2006-06-26 13:57:35 +0200722 pushq %rbp
Glauber Costa097a0782008-08-14 17:33:12 -0300723 /*
724 * Save rbp twice: One is for marking the stack frame, as usual, and the
725 * other, to fill pt_regs properly. This is because bx comes right
726 * before the last saved register in that structure, and not bp. If the
727 * base pointer were in the place bx is today, this would not be needed.
728 */
729 movq %rbp, -8(%rsp)
Jan Beulich1de9c3f2006-06-26 13:57:35 +0200730 CFI_ADJUST_CFA_OFFSET 8
731 CFI_REL_OFFSET rbp, 0
732 movq %rsp,%rbp
733 CFI_DEF_CFA_REGISTER rbp
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 testl $3,CS(%rdi)
735 je 1f
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100736 SWAPGS
Andi Kleen96e54042006-09-26 10:52:39 +0200737 /* irqcount is used to check if a CPU is already on an interrupt
738 stack or not. While this is essentially redundant with preempt_count
739 it is a little cheaper to use a separate counter in the PDA
740 (short of moving irq_enter into assembly, which would be too
741 much work) */
7421: incl %gs:pda_irqcount
Jan Beulich1de9c3f2006-06-26 13:57:35 +0200743 cmoveq %gs:pda_irqstackptr,%rsp
Andi Kleen26995002006-08-02 22:37:28 +0200744 push %rbp # backlink for old unwinder
Ingo Molnar2601e642006-07-03 00:24:45 -0700745 /*
746 * We entered an interrupt context - irqs are off:
747 */
748 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 call \func
750 .endm
751
752ENTRY(common_interrupt)
Jan Beulich7effaa82005-09-12 18:49:24 +0200753 XCPT_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 interrupt do_IRQ
755 /* 0(%rsp): oldrsp-ARGOFFSET */
Jan Beulich7effaa82005-09-12 18:49:24 +0200756ret_from_intr:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100757 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700758 TRACE_IRQS_OFF
Andi Kleen3829ee62005-07-28 21:15:48 -0700759 decl %gs:pda_irqcount
Jan Beulich1de9c3f2006-06-26 13:57:35 +0200760 leaveq
Jan Beulich7effaa82005-09-12 18:49:24 +0200761 CFI_DEF_CFA_REGISTER rsp
Jan Beulich1de9c3f2006-06-26 13:57:35 +0200762 CFI_ADJUST_CFA_OFFSET -8
Jan Beulich7effaa82005-09-12 18:49:24 +0200763exit_intr:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 GET_THREAD_INFO(%rcx)
765 testl $3,CS-ARGOFFSET(%rsp)
766 je retint_kernel
767
768 /* Interrupt came from user space */
769 /*
770 * Has a correct top of stack, but a partial stack frame
771 * %rcx: thread info. Interrupts off.
772 */
773retint_with_reschedule:
774 movl $_TIF_WORK_MASK,%edi
Jan Beulich7effaa82005-09-12 18:49:24 +0200775retint_check:
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200776 LOCKDEP_SYS_EXIT_IRQ
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300777 movl TI_flags(%rcx),%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 andl %edi,%edx
Jan Beulich7effaa82005-09-12 18:49:24 +0200779 CFI_REMEMBER_STATE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 jnz retint_careful
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200781
782retint_swapgs: /* return to user-space */
Ingo Molnar2601e642006-07-03 00:24:45 -0700783 /*
784 * The iretq could re-enable interrupts:
785 */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100786 DISABLE_INTERRUPTS(CLBR_ANY)
Ingo Molnar2601e642006-07-03 00:24:45 -0700787 TRACE_IRQS_IRETQ
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100788 SWAPGS
Ingo Molnar2601e642006-07-03 00:24:45 -0700789 jmp restore_args
790
Peter Zijlstra10cd7062007-10-11 22:11:12 +0200791retint_restore_args: /* return to kernel space */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100792 DISABLE_INTERRUPTS(CLBR_ANY)
Ingo Molnar2601e642006-07-03 00:24:45 -0700793 /*
794 * The iretq could re-enable interrupts:
795 */
796 TRACE_IRQS_IRETQ
797restore_args:
Ingo Molnar3701d8632008-02-09 23:24:08 +0100798 RESTORE_ARGS 0,8,0
799
Adrian Bunkf7f3d792008-02-13 23:29:53 +0200800irq_return:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100801 INTERRUPT_RETURN
Ingo Molnar3701d8632008-02-09 23:24:08 +0100802
803 .section __ex_table, "a"
804 .quad irq_return, bad_iret
805 .previous
806
807#ifdef CONFIG_PARAVIRT
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100808ENTRY(native_iret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 iretq
810
811 .section __ex_table,"a"
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100812 .quad native_iret, bad_iret
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 .previous
Ingo Molnar3701d8632008-02-09 23:24:08 +0100814#endif
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 .section .fixup,"ax"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817bad_iret:
Roland McGrath3aa4b372008-02-06 22:39:43 +0100818 /*
819 * The iret traps when the %cs or %ss being restored is bogus.
820 * We've lost the original trap vector and error code.
821 * #GPF is the most likely one to get for an invalid selector.
822 * So pretend we completed the iret and took the #GPF in user mode.
823 *
824 * We are now running with the kernel GS after exception recovery.
825 * But error_entry expects us to have user GS to match the user %cs,
826 * so swap back.
827 */
828 pushq $0
829
830 SWAPGS
831 jmp general_protection
832
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100833 .previous
834
Jan Beulich7effaa82005-09-12 18:49:24 +0200835 /* edi: workmask, edx: work */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836retint_careful:
Jan Beulich7effaa82005-09-12 18:49:24 +0200837 CFI_RESTORE_STATE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 bt $TIF_NEED_RESCHED,%edx
839 jnc retint_signal
Ingo Molnar2601e642006-07-03 00:24:45 -0700840 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100841 ENABLE_INTERRUPTS(CLBR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 pushq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200843 CFI_ADJUST_CFA_OFFSET 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 call schedule
845 popq %rdi
Jan Beulich7effaa82005-09-12 18:49:24 +0200846 CFI_ADJUST_CFA_OFFSET -8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 GET_THREAD_INFO(%rcx)
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100848 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700849 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 jmp retint_check
851
852retint_signal:
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100853 testl $_TIF_DO_NOTIFY_MASK,%edx
Andi Kleen10ffdbb2005-05-16 21:53:19 -0700854 jz retint_swapgs
Ingo Molnar2601e642006-07-03 00:24:45 -0700855 TRACE_IRQS_ON
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100856 ENABLE_INTERRUPTS(CLBR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 SAVE_REST
858 movq $-1,ORIG_RAX(%rsp)
Andi Kleen3829ee62005-07-28 21:15:48 -0700859 xorl %esi,%esi # oldset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 movq %rsp,%rdi # &pt_regs
861 call do_notify_resume
862 RESTORE_REST
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100863 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700864 TRACE_IRQS_OFF
Andi Kleenbe9e6872005-05-01 08:58:51 -0700865 GET_THREAD_INFO(%rcx)
Roland McGratheca91e72008-07-10 14:50:39 -0700866 jmp retint_with_reschedule
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868#ifdef CONFIG_PREEMPT
869 /* Returning to kernel space. Check if we need preemption */
870 /* rcx: threadinfo. interrupts off. */
Andi Kleenb06baba2006-09-26 10:52:29 +0200871ENTRY(retint_kernel)
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300872 cmpl $0,TI_preempt_count(%rcx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 jnz retint_restore_args
Glauber Costa26ccb8a2008-06-24 11:19:35 -0300874 bt $TIF_NEED_RESCHED,TI_flags(%rcx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 jnc retint_restore_args
876 bt $9,EFLAGS-ARGOFFSET(%rsp) /* interrupts off? */
877 jnc retint_restore_args
878 call preempt_schedule_irq
879 jmp exit_intr
880#endif
Jan Beulich4b787e02006-06-26 13:56:55 +0200881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200883END(common_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885/*
886 * APIC interrupts.
887 */
888 .macro apicinterrupt num,func
Jan Beulich7effaa82005-09-12 18:49:24 +0200889 INTR_FRAME
Rusty Russell19eadf92006-06-27 02:53:44 -0700890 pushq $~(\num)
Jan Beulich7effaa82005-09-12 18:49:24 +0200891 CFI_ADJUST_CFA_OFFSET 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 interrupt \func
893 jmp ret_from_intr
894 CFI_ENDPROC
895 .endm
896
897ENTRY(thermal_interrupt)
898 apicinterrupt THERMAL_APIC_VECTOR,smp_thermal_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200899END(thermal_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Jacob Shin89b831e2005-11-05 17:25:53 +0100901ENTRY(threshold_interrupt)
902 apicinterrupt THRESHOLD_APIC_VECTOR,mce_threshold_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200903END(threshold_interrupt)
Jacob Shin89b831e2005-11-05 17:25:53 +0100904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905#ifdef CONFIG_SMP
906ENTRY(reschedule_interrupt)
907 apicinterrupt RESCHEDULE_VECTOR,smp_reschedule_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200908END(reschedule_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Andi Kleene5bc8b62005-09-12 18:49:24 +0200910 .macro INVALIDATE_ENTRY num
911ENTRY(invalidate_interrupt\num)
912 apicinterrupt INVALIDATE_TLB_VECTOR_START+\num,smp_invalidate_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200913END(invalidate_interrupt\num)
Andi Kleene5bc8b62005-09-12 18:49:24 +0200914 .endm
915
916 INVALIDATE_ENTRY 0
917 INVALIDATE_ENTRY 1
918 INVALIDATE_ENTRY 2
919 INVALIDATE_ENTRY 3
920 INVALIDATE_ENTRY 4
921 INVALIDATE_ENTRY 5
922 INVALIDATE_ENTRY 6
923 INVALIDATE_ENTRY 7
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925ENTRY(call_function_interrupt)
926 apicinterrupt CALL_FUNCTION_VECTOR,smp_call_function_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200927END(call_function_interrupt)
Jens Axboe3b16cf82008-06-26 11:21:54 +0200928ENTRY(call_function_single_interrupt)
929 apicinterrupt CALL_FUNCTION_SINGLE_VECTOR,smp_call_function_single_interrupt
930END(call_function_single_interrupt)
Eric W. Biederman61014292007-02-23 04:40:58 -0700931ENTRY(irq_move_cleanup_interrupt)
932 apicinterrupt IRQ_MOVE_CLEANUP_VECTOR,smp_irq_move_cleanup_interrupt
933END(irq_move_cleanup_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934#endif
935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936ENTRY(apic_timer_interrupt)
937 apicinterrupt LOCAL_TIMER_VECTOR,smp_apic_timer_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200938END(apic_timer_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Cliff Wickman18129242008-06-02 08:56:14 -0500940ENTRY(uv_bau_message_intr1)
941 apicinterrupt 220,uv_bau_message_interrupt
942END(uv_bau_message_intr1)
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944ENTRY(error_interrupt)
945 apicinterrupt ERROR_APIC_VECTOR,smp_error_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200946END(error_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948ENTRY(spurious_interrupt)
949 apicinterrupt SPURIOUS_APIC_VECTOR,smp_spurious_interrupt
Jan Beulich4b787e02006-06-26 13:56:55 +0200950END(spurious_interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952/*
953 * Exception entry points.
954 */
955 .macro zeroentry sym
Jan Beulich7effaa82005-09-12 18:49:24 +0200956 INTR_FRAME
Jeremy Fitzhardingefab58422008-06-25 00:19:31 -0400957 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 pushq $0 /* push error code/oldrax */
Jan Beulich7effaa82005-09-12 18:49:24 +0200959 CFI_ADJUST_CFA_OFFSET 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 pushq %rax /* push real oldrax to the rdi slot */
Jan Beulich7effaa82005-09-12 18:49:24 +0200961 CFI_ADJUST_CFA_OFFSET 8
Jan Beulich37550902007-05-02 19:27:05 +0200962 CFI_REL_OFFSET rax,0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 leaq \sym(%rip),%rax
964 jmp error_entry
Jan Beulich7effaa82005-09-12 18:49:24 +0200965 CFI_ENDPROC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 .endm
967
968 .macro errorentry sym
Jan Beulich7effaa82005-09-12 18:49:24 +0200969 XCPT_FRAME
Jeremy Fitzhardingefab58422008-06-25 00:19:31 -0400970 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 pushq %rax
Jan Beulich7effaa82005-09-12 18:49:24 +0200972 CFI_ADJUST_CFA_OFFSET 8
Jan Beulich37550902007-05-02 19:27:05 +0200973 CFI_REL_OFFSET rax,0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 leaq \sym(%rip),%rax
975 jmp error_entry
Jan Beulich7effaa82005-09-12 18:49:24 +0200976 CFI_ENDPROC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 .endm
978
979 /* error code is on the stack already */
980 /* handle NMI like exceptions that can happen everywhere */
Ingo Molnar2601e642006-07-03 00:24:45 -0700981 .macro paranoidentry sym, ist=0, irqtrace=1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 SAVE_ALL
983 cld
984 movl $1,%ebx
985 movl $MSR_GS_BASE,%ecx
986 rdmsr
987 testl %edx,%edx
988 js 1f
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +0100989 SWAPGS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 xorl %ebx,%ebx
Jan Beulichb556b352006-01-11 22:43:00 +01009911:
992 .if \ist
993 movq %gs:pda_data_offset, %rbp
994 .endif
Alexander van Heukelum7e61a792008-09-26 14:03:03 +0200995 .if \irqtrace
996 TRACE_IRQS_OFF
997 .endif
Jan Beulichb556b352006-01-11 22:43:00 +0100998 movq %rsp,%rdi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 movq ORIG_RAX(%rsp),%rsi
1000 movq $-1,ORIG_RAX(%rsp)
Jan Beulichb556b352006-01-11 22:43:00 +01001001 .if \ist
Andi Kleen5f8efbb2006-01-16 01:56:39 +01001002 subq $EXCEPTION_STKSZ, per_cpu__init_tss + TSS_ist + (\ist - 1) * 8(%rbp)
Jan Beulichb556b352006-01-11 22:43:00 +01001003 .endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 call \sym
Jan Beulichb556b352006-01-11 22:43:00 +01001005 .if \ist
Andi Kleen5f8efbb2006-01-16 01:56:39 +01001006 addq $EXCEPTION_STKSZ, per_cpu__init_tss + TSS_ist + (\ist - 1) * 8(%rbp)
Jan Beulichb556b352006-01-11 22:43:00 +01001007 .endif
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001008 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -07001009 .if \irqtrace
1010 TRACE_IRQS_OFF
1011 .endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 .endm
Ingo Molnar2601e642006-07-03 00:24:45 -07001013
1014 /*
1015 * "Paranoid" exit path from exception stack.
1016 * Paranoid because this is used by NMIs and cannot take
1017 * any kernel state for granted.
1018 * We don't do kernel preemption checks here, because only
1019 * NMI should be common and it does not enable IRQs and
1020 * cannot get reschedule ticks.
1021 *
1022 * "trace" is 0 for the NMI handler only, because irq-tracing
1023 * is fundamentally NMI-unsafe. (we cannot change the soft and
1024 * hard flags at once, atomically)
1025 */
1026 .macro paranoidexit trace=1
1027 /* ebx: no swapgs flag */
1028paranoid_exit\trace:
1029 testl %ebx,%ebx /* swapgs needed? */
1030 jnz paranoid_restore\trace
1031 testl $3,CS(%rsp)
1032 jnz paranoid_userspace\trace
1033paranoid_swapgs\trace:
Andi Kleen7a0a2df2006-09-26 10:52:37 +02001034 .if \trace
Ingo Molnar2601e642006-07-03 00:24:45 -07001035 TRACE_IRQS_IRETQ 0
Andi Kleen7a0a2df2006-09-26 10:52:37 +02001036 .endif
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001037 SWAPGS_UNSAFE_STACK
Ingo Molnar2601e642006-07-03 00:24:45 -07001038paranoid_restore\trace:
1039 RESTORE_ALL 8
Ingo Molnar3701d8632008-02-09 23:24:08 +01001040 jmp irq_return
Ingo Molnar2601e642006-07-03 00:24:45 -07001041paranoid_userspace\trace:
1042 GET_THREAD_INFO(%rcx)
Glauber Costa26ccb8a2008-06-24 11:19:35 -03001043 movl TI_flags(%rcx),%ebx
Ingo Molnar2601e642006-07-03 00:24:45 -07001044 andl $_TIF_WORK_MASK,%ebx
1045 jz paranoid_swapgs\trace
1046 movq %rsp,%rdi /* &pt_regs */
1047 call sync_regs
1048 movq %rax,%rsp /* switch stack for scheduling */
1049 testl $_TIF_NEED_RESCHED,%ebx
1050 jnz paranoid_schedule\trace
1051 movl %ebx,%edx /* arg3: thread flags */
1052 .if \trace
1053 TRACE_IRQS_ON
1054 .endif
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001055 ENABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -07001056 xorl %esi,%esi /* arg2: oldset */
1057 movq %rsp,%rdi /* arg1: &pt_regs */
1058 call do_notify_resume
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001059 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -07001060 .if \trace
1061 TRACE_IRQS_OFF
1062 .endif
1063 jmp paranoid_userspace\trace
1064paranoid_schedule\trace:
1065 .if \trace
1066 TRACE_IRQS_ON
1067 .endif
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001068 ENABLE_INTERRUPTS(CLBR_ANY)
Ingo Molnar2601e642006-07-03 00:24:45 -07001069 call schedule
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001070 DISABLE_INTERRUPTS(CLBR_ANY)
Ingo Molnar2601e642006-07-03 00:24:45 -07001071 .if \trace
1072 TRACE_IRQS_OFF
1073 .endif
1074 jmp paranoid_userspace\trace
1075 CFI_ENDPROC
1076 .endm
1077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078/*
1079 * Exception entry point. This expects an error code/orig_rax on the stack
1080 * and the exception handler in %rax.
1081 */
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001082KPROBE_ENTRY(error_entry)
Jan Beulich7effaa82005-09-12 18:49:24 +02001083 _frame RDI
Jan Beulich37550902007-05-02 19:27:05 +02001084 CFI_REL_OFFSET rax,0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 /* rdi slot contains rax, oldrax contains error code */
1086 cld
1087 subq $14*8,%rsp
1088 CFI_ADJUST_CFA_OFFSET (14*8)
1089 movq %rsi,13*8(%rsp)
1090 CFI_REL_OFFSET rsi,RSI
1091 movq 14*8(%rsp),%rsi /* load rax from rdi slot */
Jan Beulich37550902007-05-02 19:27:05 +02001092 CFI_REGISTER rax,rsi
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 movq %rdx,12*8(%rsp)
1094 CFI_REL_OFFSET rdx,RDX
1095 movq %rcx,11*8(%rsp)
1096 CFI_REL_OFFSET rcx,RCX
1097 movq %rsi,10*8(%rsp) /* store rax */
1098 CFI_REL_OFFSET rax,RAX
1099 movq %r8, 9*8(%rsp)
1100 CFI_REL_OFFSET r8,R8
1101 movq %r9, 8*8(%rsp)
1102 CFI_REL_OFFSET r9,R9
1103 movq %r10,7*8(%rsp)
1104 CFI_REL_OFFSET r10,R10
1105 movq %r11,6*8(%rsp)
1106 CFI_REL_OFFSET r11,R11
1107 movq %rbx,5*8(%rsp)
1108 CFI_REL_OFFSET rbx,RBX
1109 movq %rbp,4*8(%rsp)
1110 CFI_REL_OFFSET rbp,RBP
1111 movq %r12,3*8(%rsp)
1112 CFI_REL_OFFSET r12,R12
1113 movq %r13,2*8(%rsp)
1114 CFI_REL_OFFSET r13,R13
1115 movq %r14,1*8(%rsp)
1116 CFI_REL_OFFSET r14,R14
1117 movq %r15,(%rsp)
1118 CFI_REL_OFFSET r15,R15
1119 xorl %ebx,%ebx
1120 testl $3,CS(%rsp)
1121 je error_kernelspace
1122error_swapgs:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001123 SWAPGS
Alexander van Heukelum6b11d4e2008-09-26 14:03:02 +02001124error_sti:
1125 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 movq %rdi,RDI(%rsp)
Jan Beulich37550902007-05-02 19:27:05 +02001127 CFI_REL_OFFSET rdi,RDI
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 movq %rsp,%rdi
1129 movq ORIG_RAX(%rsp),%rsi /* get error code */
1130 movq $-1,ORIG_RAX(%rsp)
1131 call *%rax
Peter Zijlstra10cd7062007-10-11 22:11:12 +02001132 /* ebx: no swapgs flag (1: don't need swapgs, 0: need it) */
1133error_exit:
1134 movl %ebx,%eax
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 RESTORE_REST
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001136 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -07001137 TRACE_IRQS_OFF
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 GET_THREAD_INFO(%rcx)
1139 testl %eax,%eax
1140 jne retint_kernel
Peter Zijlstra10cd7062007-10-11 22:11:12 +02001141 LOCKDEP_SYS_EXIT_IRQ
Glauber Costa26ccb8a2008-06-24 11:19:35 -03001142 movl TI_flags(%rcx),%edx
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 movl $_TIF_WORK_MASK,%edi
1144 andl %edi,%edx
1145 jnz retint_careful
Peter Zijlstra10cd7062007-10-11 22:11:12 +02001146 jmp retint_swapgs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 CFI_ENDPROC
1148
1149error_kernelspace:
1150 incl %ebx
1151 /* There are two places in the kernel that can potentially fault with
1152 usergs. Handle them here. The exception handlers after
1153 iret run with kernel gs again, so don't set the user space flag.
1154 B stepping K8s sometimes report an truncated RIP for IRET
1155 exceptions returning to compat mode. Check for these here too. */
Vegard Nossum9d8ad5d2008-06-27 17:22:17 +02001156 leaq irq_return(%rip),%rcx
1157 cmpq %rcx,RIP(%rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 je error_swapgs
Vegard Nossum9d8ad5d2008-06-27 17:22:17 +02001159 movl %ecx,%ecx /* zero extend */
1160 cmpq %rcx,RIP(%rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 je error_swapgs
1162 cmpq $gs_change,RIP(%rsp)
1163 je error_swapgs
1164 jmp error_sti
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001165KPROBE_END(error_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 /* Reload gs selector with exception handling */
1168 /* edi: new selector */
Jeremy Fitzhardinge9f9d4892008-06-25 00:19:32 -04001169ENTRY(native_load_gs_index)
Jan Beulich7effaa82005-09-12 18:49:24 +02001170 CFI_STARTPROC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 pushf
Jan Beulich7effaa82005-09-12 18:49:24 +02001172 CFI_ADJUST_CFA_OFFSET 8
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001173 DISABLE_INTERRUPTS(CLBR_ANY | ~(CLBR_RDI))
1174 SWAPGS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175gs_change:
1176 movl %edi,%gs
11772: mfence /* workaround */
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001178 SWAPGS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 popf
Jan Beulich7effaa82005-09-12 18:49:24 +02001180 CFI_ADJUST_CFA_OFFSET -8
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 ret
Jan Beulich7effaa82005-09-12 18:49:24 +02001182 CFI_ENDPROC
Jeremy Fitzhardinge9f9d4892008-06-25 00:19:32 -04001183ENDPROC(native_load_gs_index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 .section __ex_table,"a"
1186 .align 8
1187 .quad gs_change,bad_gs
1188 .previous
1189 .section .fixup,"ax"
1190 /* running with kernelgs */
1191bad_gs:
Glauber de Oliveira Costa72fe4852008-01-30 13:32:08 +01001192 SWAPGS /* switch back to user gs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 xorl %eax,%eax
1194 movl %eax,%gs
1195 jmp 2b
1196 .previous
1197
1198/*
1199 * Create a kernel thread.
1200 *
1201 * C extern interface:
1202 * extern long kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
1203 *
1204 * asm input arguments:
1205 * rdi: fn, rsi: arg, rdx: flags
1206 */
1207ENTRY(kernel_thread)
1208 CFI_STARTPROC
1209 FAKE_STACK_FRAME $child_rip
1210 SAVE_ALL
1211
1212 # rdi: flags, rsi: usp, rdx: will be &pt_regs
1213 movq %rdx,%rdi
1214 orq kernel_thread_flags(%rip),%rdi
1215 movq $-1, %rsi
1216 movq %rsp, %rdx
1217
1218 xorl %r8d,%r8d
1219 xorl %r9d,%r9d
1220
1221 # clone now
1222 call do_fork
1223 movq %rax,RAX(%rsp)
1224 xorl %edi,%edi
1225
1226 /*
1227 * It isn't worth to check for reschedule here,
1228 * so internally to the x86_64 port you can rely on kernel_thread()
1229 * not to reschedule the child before returning, this avoids the need
1230 * of hacks for example to fork off the per-CPU idle tasks.
1231 * [Hopefully no generic code relies on the reschedule -AK]
1232 */
1233 RESTORE_ALL
1234 UNFAKE_STACK_FRAME
1235 ret
1236 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001237ENDPROC(kernel_thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239child_rip:
Andi Kleenc05991e2006-08-30 19:37:08 +02001240 pushq $0 # fake return address
1241 CFI_STARTPROC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 /*
1243 * Here we are in the child and the registers are set as they were
1244 * at kernel_thread() invocation in the parent.
1245 */
1246 movq %rdi, %rax
1247 movq %rsi, %rdi
1248 call *%rax
1249 # exit
Andrey Mirkin1c5b5cf2007-10-17 18:04:33 +02001250 mov %eax, %edi
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 call do_exit
Andi Kleenc05991e2006-08-30 19:37:08 +02001252 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001253ENDPROC(child_rip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255/*
1256 * execve(). This function needs to use IRET, not SYSRET, to set up all state properly.
1257 *
1258 * C extern interface:
1259 * extern long execve(char *name, char **argv, char **envp)
1260 *
1261 * asm input arguments:
1262 * rdi: name, rsi: argv, rdx: envp
1263 *
1264 * We want to fallback into:
Ingo Molnar5d119b22008-02-26 12:55:57 +01001265 * extern long sys_execve(char *name, char **argv,char **envp, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 *
1267 * do_sys_execve asm fallback arguments:
Ingo Molnar5d119b22008-02-26 12:55:57 +01001268 * rdi: name, rsi: argv, rdx: envp, rcx: fake frame on the stack
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 */
Arnd Bergmann3db03b42006-10-02 02:18:31 -07001270ENTRY(kernel_execve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 CFI_STARTPROC
1272 FAKE_STACK_FRAME $0
1273 SAVE_ALL
Ingo Molnar5d119b22008-02-26 12:55:57 +01001274 movq %rsp,%rcx
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 call sys_execve
1276 movq %rax, RAX(%rsp)
1277 RESTORE_REST
1278 testq %rax,%rax
1279 je int_ret_from_sys_call
1280 RESTORE_ARGS
1281 UNFAKE_STACK_FRAME
1282 ret
1283 CFI_ENDPROC
Arnd Bergmann3db03b42006-10-02 02:18:31 -07001284ENDPROC(kernel_execve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001286KPROBE_ENTRY(page_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 errorentry do_page_fault
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001288KPROBE_END(page_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290ENTRY(coprocessor_error)
1291 zeroentry do_coprocessor_error
Jan Beulich4b787e02006-06-26 13:56:55 +02001292END(coprocessor_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294ENTRY(simd_coprocessor_error)
1295 zeroentry do_simd_coprocessor_error
Jan Beulich4b787e02006-06-26 13:56:55 +02001296END(simd_coprocessor_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298ENTRY(device_not_available)
Alexander van Heukelume407d6202008-09-30 18:41:36 +02001299 zeroentry do_device_not_available
Jan Beulich4b787e02006-06-26 13:56:55 +02001300END(device_not_available)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 /* runs on exception stack */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001303KPROBE_ENTRY(debug)
Jan Beulich7effaa82005-09-12 18:49:24 +02001304 INTR_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001305 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 pushq $0
1307 CFI_ADJUST_CFA_OFFSET 8
Andi Kleen5f8efbb2006-01-16 01:56:39 +01001308 paranoidentry do_debug, DEBUG_STACK
Ingo Molnar2601e642006-07-03 00:24:45 -07001309 paranoidexit
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001310KPROBE_END(debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312 /* runs on exception stack */
Andi Kleeneddb6fb2006-02-03 21:50:41 +01001313KPROBE_ENTRY(nmi)
Jan Beulich7effaa82005-09-12 18:49:24 +02001314 INTR_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001315 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 pushq $-1
Jan Beulich7effaa82005-09-12 18:49:24 +02001317 CFI_ADJUST_CFA_OFFSET 8
Ingo Molnar2601e642006-07-03 00:24:45 -07001318 paranoidentry do_nmi, 0, 0
1319#ifdef CONFIG_TRACE_IRQFLAGS
1320 paranoidexit 0
1321#else
1322 jmp paranoid_exit1
1323 CFI_ENDPROC
1324#endif
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001325KPROBE_END(nmi)
Andi Kleen6fefb0d2005-04-16 15:25:03 -07001326
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001327KPROBE_ENTRY(int3)
Jan Beulichb556b352006-01-11 22:43:00 +01001328 INTR_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001329 PARAVIRT_ADJUST_EXCEPTION_FRAME
Jan Beulichb556b352006-01-11 22:43:00 +01001330 pushq $0
1331 CFI_ADJUST_CFA_OFFSET 8
Andi Kleen5f8efbb2006-01-16 01:56:39 +01001332 paranoidentry do_int3, DEBUG_STACK
Ingo Molnar2601e642006-07-03 00:24:45 -07001333 jmp paranoid_exit1
Jan Beulichb556b352006-01-11 22:43:00 +01001334 CFI_ENDPROC
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001335KPROBE_END(int3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337ENTRY(overflow)
1338 zeroentry do_overflow
Jan Beulich4b787e02006-06-26 13:56:55 +02001339END(overflow)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341ENTRY(bounds)
1342 zeroentry do_bounds
Jan Beulich4b787e02006-06-26 13:56:55 +02001343END(bounds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345ENTRY(invalid_op)
1346 zeroentry do_invalid_op
Jan Beulich4b787e02006-06-26 13:56:55 +02001347END(invalid_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349ENTRY(coprocessor_segment_overrun)
1350 zeroentry do_coprocessor_segment_overrun
Jan Beulich4b787e02006-06-26 13:56:55 +02001351END(coprocessor_segment_overrun)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 /* runs on exception stack */
1354ENTRY(double_fault)
Jan Beulich7effaa82005-09-12 18:49:24 +02001355 XCPT_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001356 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 paranoidentry do_double_fault
Ingo Molnar2601e642006-07-03 00:24:45 -07001358 jmp paranoid_exit1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001360END(double_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362ENTRY(invalid_TSS)
1363 errorentry do_invalid_TSS
Jan Beulich4b787e02006-06-26 13:56:55 +02001364END(invalid_TSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366ENTRY(segment_not_present)
1367 errorentry do_segment_not_present
Jan Beulich4b787e02006-06-26 13:56:55 +02001368END(segment_not_present)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 /* runs on exception stack */
1371ENTRY(stack_segment)
Jan Beulich7effaa82005-09-12 18:49:24 +02001372 XCPT_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001373 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 paranoidentry do_stack_segment
Ingo Molnar2601e642006-07-03 00:24:45 -07001375 jmp paranoid_exit1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001377END(stack_segment)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001379KPROBE_ENTRY(general_protection)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 errorentry do_general_protection
Prasanna S.Pd28c4392006-09-26 10:52:34 +02001381KPROBE_END(general_protection)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383ENTRY(alignment_check)
1384 errorentry do_alignment_check
Jan Beulich4b787e02006-06-26 13:56:55 +02001385END(alignment_check)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387ENTRY(divide_error)
1388 zeroentry do_divide_error
Jan Beulich4b787e02006-06-26 13:56:55 +02001389END(divide_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391ENTRY(spurious_interrupt_bug)
1392 zeroentry do_spurious_interrupt_bug
Jan Beulich4b787e02006-06-26 13:56:55 +02001393END(spurious_interrupt_bug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395#ifdef CONFIG_X86_MCE
1396 /* runs on exception stack */
1397ENTRY(machine_check)
Jan Beulich7effaa82005-09-12 18:49:24 +02001398 INTR_FRAME
Jeremy Fitzhardinge09402942008-07-12 02:22:12 -07001399 PARAVIRT_ADJUST_EXCEPTION_FRAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 pushq $0
1401 CFI_ADJUST_CFA_OFFSET 8
1402 paranoidentry do_machine_check
Ingo Molnar2601e642006-07-03 00:24:45 -07001403 jmp paranoid_exit1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001405END(machine_check)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406#endif
1407
Andi Kleen26995002006-08-02 22:37:28 +02001408/* Call softirq on interrupt stack. Interrupts are off. */
Andi Kleened6b6762005-07-28 21:15:49 -07001409ENTRY(call_softirq)
Jan Beulich7effaa82005-09-12 18:49:24 +02001410 CFI_STARTPROC
Andi Kleen26995002006-08-02 22:37:28 +02001411 push %rbp
1412 CFI_ADJUST_CFA_OFFSET 8
1413 CFI_REL_OFFSET rbp,0
1414 mov %rsp,%rbp
1415 CFI_DEF_CFA_REGISTER rbp
Andi Kleened6b6762005-07-28 21:15:49 -07001416 incl %gs:pda_irqcount
Andi Kleen26995002006-08-02 22:37:28 +02001417 cmove %gs:pda_irqstackptr,%rsp
1418 push %rbp # backlink for old unwinder
Andi Kleened6b6762005-07-28 21:15:49 -07001419 call __do_softirq
Andi Kleen26995002006-08-02 22:37:28 +02001420 leaveq
Jan Beulich7effaa82005-09-12 18:49:24 +02001421 CFI_DEF_CFA_REGISTER rsp
Andi Kleen26995002006-08-02 22:37:28 +02001422 CFI_ADJUST_CFA_OFFSET -8
Andi Kleened6b6762005-07-28 21:15:49 -07001423 decl %gs:pda_irqcount
Andi Kleened6b6762005-07-28 21:15:49 -07001424 ret
Jan Beulich7effaa82005-09-12 18:49:24 +02001425 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +02001426ENDPROC(call_softirq)
Andi Kleen75154f42007-06-23 02:29:25 +02001427
1428KPROBE_ENTRY(ignore_sysret)
1429 CFI_STARTPROC
1430 mov $-ENOSYS,%eax
1431 sysret
1432 CFI_ENDPROC
1433ENDPROC(ignore_sysret)
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001434
1435#ifdef CONFIG_XEN
1436ENTRY(xen_hypervisor_callback)
1437 zeroentry xen_do_hypervisor_callback
1438END(xen_hypervisor_callback)
1439
1440/*
1441# A note on the "critical region" in our callback handler.
1442# We want to avoid stacking callback handlers due to events occurring
1443# during handling of the last event. To do this, we keep events disabled
1444# until we've done all processing. HOWEVER, we must enable events before
1445# popping the stack frame (can't be done atomically) and so it would still
1446# be possible to get enough handler activations to overflow the stack.
1447# Although unlikely, bugs of that kind are hard to track down, so we'd
1448# like to avoid the possibility.
1449# So, on entry to the handler we detect whether we interrupted an
1450# existing activation in its critical region -- if so, we pop the current
1451# activation and restart the handler using the previous one.
1452*/
1453ENTRY(xen_do_hypervisor_callback) # do_hypervisor_callback(struct *pt_regs)
1454 CFI_STARTPROC
1455/* Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
1456 see the correct pointer to the pt_regs */
1457 movq %rdi, %rsp # we don't return, adjust the stack frame
1458 CFI_ENDPROC
1459 CFI_DEFAULT_STACK
146011: incl %gs:pda_irqcount
1461 movq %rsp,%rbp
1462 CFI_DEF_CFA_REGISTER rbp
1463 cmovzq %gs:pda_irqstackptr,%rsp
1464 pushq %rbp # backlink for old unwinder
1465 call xen_evtchn_do_upcall
1466 popq %rsp
1467 CFI_DEF_CFA_REGISTER rsp
1468 decl %gs:pda_irqcount
1469 jmp error_exit
1470 CFI_ENDPROC
1471END(do_hypervisor_callback)
1472
1473/*
1474# Hypervisor uses this for application faults while it executes.
1475# We get here for two reasons:
1476# 1. Fault while reloading DS, ES, FS or GS
1477# 2. Fault while executing IRET
1478# Category 1 we do not need to fix up as Xen has already reloaded all segment
1479# registers that could be reloaded and zeroed the others.
1480# Category 2 we fix up by killing the current process. We cannot use the
1481# normal Linux return path in this case because if we use the IRET hypercall
1482# to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1483# We distinguish between categories by comparing each saved segment register
1484# with its current contents: any discrepancy means we in category 1.
1485*/
1486ENTRY(xen_failsafe_callback)
Jeremy Fitzhardinge4a5c3e72008-07-08 15:07:09 -07001487 framesz = (RIP-0x30) /* workaround buggy gas */
1488 _frame framesz
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001489 CFI_REL_OFFSET rcx, 0
1490 CFI_REL_OFFSET r11, 8
1491 movw %ds,%cx
1492 cmpw %cx,0x10(%rsp)
1493 CFI_REMEMBER_STATE
1494 jne 1f
1495 movw %es,%cx
1496 cmpw %cx,0x18(%rsp)
1497 jne 1f
1498 movw %fs,%cx
1499 cmpw %cx,0x20(%rsp)
1500 jne 1f
1501 movw %gs,%cx
1502 cmpw %cx,0x28(%rsp)
1503 jne 1f
1504 /* All segments match their saved values => Category 2 (Bad IRET). */
1505 movq (%rsp),%rcx
1506 CFI_RESTORE rcx
1507 movq 8(%rsp),%r11
1508 CFI_RESTORE r11
1509 addq $0x30,%rsp
1510 CFI_ADJUST_CFA_OFFSET -0x30
Jeremy Fitzhardinge4a5c3e72008-07-08 15:07:09 -07001511 pushq $0
1512 CFI_ADJUST_CFA_OFFSET 8
1513 pushq %r11
1514 CFI_ADJUST_CFA_OFFSET 8
1515 pushq %rcx
1516 CFI_ADJUST_CFA_OFFSET 8
1517 jmp general_protection
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001518 CFI_RESTORE_STATE
15191: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
1520 movq (%rsp),%rcx
1521 CFI_RESTORE rcx
1522 movq 8(%rsp),%r11
1523 CFI_RESTORE r11
1524 addq $0x30,%rsp
1525 CFI_ADJUST_CFA_OFFSET -0x30
1526 pushq $0
1527 CFI_ADJUST_CFA_OFFSET 8
1528 SAVE_ALL
1529 jmp error_exit
1530 CFI_ENDPROC
Jeremy Fitzhardinge3d75e1b2008-07-08 15:06:49 -07001531END(xen_failsafe_callback)
1532
1533#endif /* CONFIG_XEN */