2 * Compatibility mode system call entry point for x86-64.
4 * Copyright 2000-2002 Andi Kleen, SuSE Labs.
8 #include <asm/asm-offsets.h>
9 #include <asm/current.h>
10 #include <asm/errno.h>
11 #include <asm/ia32_unistd.h>
12 #include <asm/thread_info.h>
13 #include <asm/segment.h>
14 #include <asm/irqflags.h>
17 #include <linux/linkage.h>
18 #include <linux/err.h>
20 /* Avoid __ASSEMBLER__'ifying <linux/audit.h> just for this. */
21 #include <linux/elf-em.h>
22 #define AUDIT_ARCH_I386 (EM_386|__AUDIT_ARCH_LE)
23 #define __AUDIT_ARCH_LE 0x40000000
25 #ifndef CONFIG_AUDITSYSCALL
26 #define sysexit_audit ia32_ret_from_sys_call
27 #define sysretl_audit ia32_ret_from_sys_call
30 .section .entry.text, "ax"
32 #ifdef CONFIG_PARAVIRT
33 ENTRY(native_usergs_sysret32)
36 ENDPROC(native_usergs_sysret32)
40 * 32bit SYSENTER instruction entry.
42 * SYSENTER loads ss, rsp, cs, and rip from previously programmed MSRs.
43 * IF and VM in rflags are cleared (IOW: interrupts are off).
44 * SYSENTER does not save anything on the stack,
45 * and does not save old rip (!!!) and rflags.
48 * eax system call number
57 * This is purely a fast path. For anything complicated we use the int 0x80
58 * path below. We set up a complete hardware stack frame to share code
59 * with the int 0x80 path.
61 ENTRY(ia32_sysenter_target)
63 * Interrupts are off on entry.
64 * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
65 * it is too small to ever cause noticeable irq latency.
68 movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
69 ENABLE_INTERRUPTS(CLBR_NONE)
71 /* Zero-extending 32-bit regs, do not remove */
75 movl ASM_THREAD_INFO(TI_sysenter_return, %rsp, 0), %r10d
77 /* Construct struct pt_regs on stack */
78 pushq $__USER32_DS /* pt_regs->ss */
79 pushq %rbp /* pt_regs->sp */
80 pushfq /* pt_regs->flags */
81 pushq $__USER32_CS /* pt_regs->cs */
82 pushq %r10 /* pt_regs->ip = thread_info->sysenter_return */
83 pushq %rax /* pt_regs->orig_ax */
84 pushq %rdi /* pt_regs->di */
85 pushq %rsi /* pt_regs->si */
86 pushq %rdx /* pt_regs->dx */
87 pushq %rcx /* pt_regs->cx */
88 pushq $-ENOSYS /* pt_regs->ax */
90 sub $(10*8),%rsp /* pt_regs->r8-11,bp,bx,r12-15 not saved */
93 * no need to do an access_ok check here because rbp has been
98 _ASM_EXTABLE(1b,ia32_badarg)
102 * Sysenter doesn't filter flags, so we need to clear NT
103 * ourselves. To save a few cycles, we can check whether
104 * NT was set instead of doing an unconditional popfq.
106 testl $X86_EFLAGS_NT,EFLAGS(%rsp)
107 jnz sysenter_fix_flags
108 sysenter_flags_fixed:
110 orl $TS_COMPAT, ASM_THREAD_INFO(TI_status, %rsp, SIZEOF_PTREGS)
111 testl $_TIF_WORK_SYSCALL_ENTRY, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
112 jnz sysenter_tracesys
115 /* 32bit syscall -> 64bit C ABI argument conversion */
116 movl %edi,%r8d /* arg5 */
117 movl %ebp,%r9d /* arg6 */
118 xchg %ecx,%esi /* rsi:arg2, rcx:arg4 */
119 movl %ebx,%edi /* arg1 */
120 movl %edx,%edx /* arg3 (zero extension) */
122 cmpq $(IA32_NR_syscalls-1),%rax
124 call *ia32_sys_call_table(,%rax,8)
127 DISABLE_INTERRUPTS(CLBR_NONE)
129 testl $_TIF_ALLWORK_MASK, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
131 sysexit_from_sys_call:
133 * NB: SYSEXIT is not obviously safe for 64-bit kernels -- an
134 * NMI between STI and SYSEXIT has poorly specified behavior,
135 * and and NMI followed by an IRQ with usergs is fatal. So
136 * we just pretend we're using SYSEXIT but we really use
139 * This code path is still called 'sysexit' because it pairs
140 * with 'sysenter' and it uses the SYSENTER calling convention.
142 andl $~TS_COMPAT,ASM_THREAD_INFO(TI_status, %rsp, SIZEOF_PTREGS)
143 movl RIP(%rsp),%ecx /* User %eip */
145 xorl %edx,%edx /* avoid info leaks */
149 movl EFLAGS(%rsp),%r11d /* User eflags */
153 * SYSRETL works even on Intel CPUs. Use it in preference to SYSEXIT,
154 * since it avoids a dicey window with interrupts enabled.
159 * USERGS_SYSRET32 does:
160 * gsbase = user's gs base
166 * The prologue set RIP(%rsp) to VDSO32_SYSENTER_RETURN, which does:
172 * Therefore, we invoke SYSRETL with EDX and R8-R10 zeroed to
173 * avoid info leaks. R11 ends up with VDSO32_SYSENTER_RETURN's
174 * address (already known to user code), and R12-R15 are
175 * callee-saved and therefore don't contain any interesting
180 #ifdef CONFIG_AUDITSYSCALL
181 .macro auditsys_entry_common
182 movl %esi,%r8d /* 5th arg: 4th syscall arg */
183 movl %ecx,%r9d /*swap with edx*/
184 movl %edx,%ecx /* 4th arg: 3rd syscall arg */
185 movl %r9d,%edx /* 3rd arg: 2nd syscall arg */
186 movl %ebx,%esi /* 2nd arg: 1st syscall arg */
187 movl %eax,%edi /* 1st arg: syscall number */
188 call __audit_syscall_entry
189 movl ORIG_RAX(%rsp),%eax /* reload syscall number */
190 movl %ebx,%edi /* reload 1st syscall arg */
191 movl RCX(%rsp),%esi /* reload 2nd syscall arg */
192 movl RDX(%rsp),%edx /* reload 3rd syscall arg */
193 movl RSI(%rsp),%ecx /* reload 4th syscall arg */
194 movl RDI(%rsp),%r8d /* reload 5th syscall arg */
197 .macro auditsys_exit exit
198 testl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT), ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
199 jnz ia32_ret_from_sys_call
201 ENABLE_INTERRUPTS(CLBR_NONE)
202 movl %eax,%esi /* second arg, syscall return value */
203 cmpl $-MAX_ERRNO,%eax /* is it an error ? */
205 movslq %eax, %rsi /* if error sign extend to 64 bits */
206 1: setbe %al /* 1 if error, 0 if not */
207 movzbl %al,%edi /* zero-extend that into %edi */
208 call __audit_syscall_exit
209 movq RAX(%rsp),%rax /* reload syscall return value */
210 movl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT),%edi
211 DISABLE_INTERRUPTS(CLBR_NONE)
213 testl %edi, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
215 xorl %eax, %eax /* do not leak kernel information */
224 auditsys_entry_common
225 movl %ebp,%r9d /* reload 6th syscall arg */
226 jmp sysenter_dispatch
229 auditsys_exit sysexit_from_sys_call
233 pushq $(X86_EFLAGS_IF|X86_EFLAGS_FIXED)
235 jmp sysenter_flags_fixed
238 #ifdef CONFIG_AUDITSYSCALL
239 testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT), ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
243 xorl %eax, %eax /* do not leak kernel information */
248 movq %rsp,%rdi /* &pt_regs -> arg1 */
249 call syscall_trace_enter
251 /* Reload arg registers from stack. (see sysenter_tracesys) */
256 movl %eax, %eax /* zero extension */
260 ENDPROC(ia32_sysenter_target)
263 * 32bit SYSCALL instruction entry.
265 * 32bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11,
266 * then loads new ss, cs, and rip from previously programmed MSRs.
267 * rflags gets masked by a value from another MSR (so CLD and CLAC
268 * are not needed). SYSCALL does not save anything on the stack
269 * and does not change rsp.
271 * Note: rflags saving+masking-with-MSR happens only in Long mode
272 * (in legacy 32bit mode, IF, RF and VM bits are cleared and that's it).
273 * Don't get confused: rflags saving+masking depends on Long Mode Active bit
274 * (EFER.LMA=1), NOT on bitness of userspace where SYSCALL executes
275 * or target CS descriptor's L bit (SYSCALL does not read segment descriptors).
278 * eax system call number
281 * ebp arg2 (note: not saved in the stack frame, should not be touched)
288 * This is purely a fast path. For anything complicated we use the int 0x80
289 * path below. We set up a complete hardware stack frame to share code
290 * with the int 0x80 path.
292 ENTRY(ia32_cstar_target)
294 * Interrupts are off on entry.
295 * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
296 * it is too small to ever cause noticeable irq latency.
300 movq PER_CPU_VAR(cpu_current_top_of_stack),%rsp
301 ENABLE_INTERRUPTS(CLBR_NONE)
303 /* Zero-extending 32-bit regs, do not remove */
306 /* Construct struct pt_regs on stack */
307 pushq $__USER32_DS /* pt_regs->ss */
308 pushq %r8 /* pt_regs->sp */
309 pushq %r11 /* pt_regs->flags */
310 pushq $__USER32_CS /* pt_regs->cs */
311 pushq %rcx /* pt_regs->ip */
312 pushq %rax /* pt_regs->orig_ax */
313 pushq %rdi /* pt_regs->di */
314 pushq %rsi /* pt_regs->si */
315 pushq %rdx /* pt_regs->dx */
316 pushq %rbp /* pt_regs->cx */
318 pushq $-ENOSYS /* pt_regs->ax */
319 sub $(10*8),%rsp /* pt_regs->r8-11,bp,bx,r12-15 not saved */
322 * no need to do an access_ok check here because r8 has been
323 * 32bit zero extended
327 _ASM_EXTABLE(1b,ia32_badarg)
329 orl $TS_COMPAT, ASM_THREAD_INFO(TI_status, %rsp, SIZEOF_PTREGS)
330 testl $_TIF_WORK_SYSCALL_ENTRY, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
334 /* 32bit syscall -> 64bit C ABI argument conversion */
335 movl %edi,%r8d /* arg5 */
336 /* r9 already loaded */ /* arg6 */
337 xchg %ecx,%esi /* rsi:arg2, rcx:arg4 */
338 movl %ebx,%edi /* arg1 */
339 movl %edx,%edx /* arg3 (zero extension) */
341 cmpq $(IA32_NR_syscalls-1),%rax
343 call *ia32_sys_call_table(,%rax,8)
346 DISABLE_INTERRUPTS(CLBR_NONE)
348 testl $_TIF_ALLWORK_MASK, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
350 sysretl_from_sys_call:
351 andl $~TS_COMPAT, ASM_THREAD_INFO(TI_status, %rsp, SIZEOF_PTREGS)
354 movl EFLAGS(%rsp),%r11d
361 * 64bit->32bit SYSRET restores eip from ecx,
362 * eflags from r11 (but RF and VM bits are forced to 0),
363 * cs and ss are loaded from MSRs.
364 * (Note: 32bit->32bit SYSRET is different: since r11
365 * does not exist, it merely sets eflags.IF=1).
367 * NB: On AMD CPUs with the X86_BUG_SYSRET_SS_ATTRS bug, the ss
368 * descriptor is not reinitialized. This means that we must
369 * avoid SYSRET with SS == NULL, which could happen if we schedule,
370 * exit the kernel, and re-enter using an interrupt vector. (All
371 * interrupt entries on x86_64 set SS to NULL.) We prevent that
372 * from happening by reloading SS in __switch_to.
376 #ifdef CONFIG_AUDITSYSCALL
378 movl %r9d,R9(%rsp) /* register to be clobbered by call */
379 auditsys_entry_common
380 movl R9(%rsp),%r9d /* reload 6th syscall arg */
384 auditsys_exit sysretl_from_sys_call
388 #ifdef CONFIG_AUDITSYSCALL
389 testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT), ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
394 xorl %eax, %eax /* do not leak kernel information */
399 movq %rsp,%rdi /* &pt_regs -> arg1 */
400 call syscall_trace_enter
403 /* Reload arg registers from stack. (see sysenter_tracesys) */
408 movl %eax, %eax /* zero extension */
413 END(ia32_cstar_target)
420 ia32_ret_from_sys_call:
421 xorl %eax, %eax /* do not leak kernel information */
426 jmp int_ret_from_sys_call
429 * Emulated IA32 system calls via int 0x80.
432 * eax system call number
438 * ebp arg6 (note: not saved in the stack frame, should not be touched)
441 * Uses the same stack frame as the x86-64 version.
442 * All registers except eax must be saved (but ptrace may violate that).
443 * Arguments are zero extended. For system calls that want sign extension and
444 * take long arguments a wrapper is needed. Most calls can just be called
446 * Assumes it is only called from user space and entered with interrupts off.
451 * Interrupts are off on entry.
452 * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
453 * it is too small to ever cause noticeable irq latency.
455 PARAVIRT_ADJUST_EXCEPTION_FRAME
457 ENABLE_INTERRUPTS(CLBR_NONE)
459 /* Zero-extending 32-bit regs, do not remove */
462 /* Construct struct pt_regs on stack (iret frame is already on stack) */
463 pushq %rax /* pt_regs->orig_ax */
464 pushq %rdi /* pt_regs->di */
465 pushq %rsi /* pt_regs->si */
466 pushq %rdx /* pt_regs->dx */
467 pushq %rcx /* pt_regs->cx */
468 pushq $-ENOSYS /* pt_regs->ax */
469 pushq $0 /* pt_regs->r8 */
470 pushq $0 /* pt_regs->r9 */
471 pushq $0 /* pt_regs->r10 */
472 pushq $0 /* pt_regs->r11 */
474 sub $(6*8),%rsp /* pt_regs->bp,bx,r12-15 not saved */
476 orl $TS_COMPAT, ASM_THREAD_INFO(TI_status, %rsp, SIZEOF_PTREGS)
477 testl $_TIF_WORK_SYSCALL_ENTRY, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
480 /* 32bit syscall -> 64bit C ABI argument conversion */
481 movl %edi,%r8d /* arg5 */
482 movl %ebp,%r9d /* arg6 */
483 xchg %ecx,%esi /* rsi:arg2, rcx:arg4 */
484 movl %ebx,%edi /* arg1 */
485 movl %edx,%edx /* arg3 (zero extension) */
486 cmpq $(IA32_NR_syscalls-1),%rax
488 call *ia32_sys_call_table(,%rax,8) # xxx: rip relative
492 jmp int_ret_from_sys_call
496 movq %rsp,%rdi /* &pt_regs -> arg1 */
497 call syscall_trace_enter
499 * Reload arg registers from stack in case ptrace changed them.
500 * Don't reload %eax because syscall_trace_enter() returned
501 * the %rax value we should see. But do truncate it to 32 bits.
502 * If it's -1 to make us punt the syscall, then (u32)-1 is still
503 * an appropriately invalid value.
509 movl %eax, %eax /* zero extension */
514 .macro PTREGSCALL label, func
517 leaq \func(%rip),%rax
518 jmp ia32_ptregs_common
521 PTREGSCALL stub32_rt_sigreturn, sys32_rt_sigreturn
522 PTREGSCALL stub32_sigreturn, sys32_sigreturn
523 PTREGSCALL stub32_fork, sys_fork
524 PTREGSCALL stub32_vfork, sys_vfork
528 leaq sys_clone(%rip),%rax
530 jmp ia32_ptregs_common
538 END(ia32_ptregs_common)