2 * Compatibility mode system call entry point for x86-64.
4 * Copyright 2000-2002 Andi Kleen, SuSE Labs.
7 #include <asm/asm-offsets.h>
8 #include <asm/current.h>
10 #include <asm/ia32_unistd.h>
11 #include <asm/thread_info.h>
12 #include <asm/segment.h>
13 #include <asm/irqflags.h>
16 #include <asm/pgtable_types.h>
17 #include <asm/kaiser.h>
18 #include <linux/linkage.h>
19 #include <linux/err.h>
21 .section .entry.text, "ax"
23 #ifdef CONFIG_PARAVIRT
24 ENTRY(native_usergs_sysret32)
27 ENDPROC(native_usergs_sysret32)
31 * 32-bit SYSENTER instruction entry.
33 * SYSENTER loads ss, rsp, cs, and rip from previously programmed MSRs.
34 * IF and VM in rflags are cleared (IOW: interrupts are off).
35 * SYSENTER does not save anything on the stack,
36 * and does not save old rip (!!!) and rflags.
39 * eax system call number
48 * This is purely a fast path. For anything complicated we use the int 0x80
49 * path below. We set up a complete hardware stack frame to share code
50 * with the int 0x80 path.
52 ENTRY(entry_SYSENTER_compat)
53 /* Interrupts are off on entry. */
55 SWITCH_KERNEL_CR3_NO_STACK
56 movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
59 * User tracing code (ptrace or signal handlers) might assume that
60 * the saved RAX contains a 32-bit number when we're invoking a 32-bit
61 * syscall. Just in case the high bits are nonzero, zero-extend
62 * the syscall number. (This could almost certainly be deleted
63 * with no ill effects.)
67 /* Construct struct pt_regs on stack */
68 pushq $__USER32_DS /* pt_regs->ss */
69 pushq %rbp /* pt_regs->sp (stashed in bp) */
72 * Push flags. This is nasty. First, interrupts are currently
73 * off, but we need pt_regs->flags to have IF set. Second, even
74 * if TF was set when SYSENTER started, it's clear by now. We fix
75 * that later using TIF_SINGLESTEP.
77 pushfq /* pt_regs->flags (except IF = 0) */
78 orl $X86_EFLAGS_IF, (%rsp) /* Fix saved flags */
79 ASM_CLAC /* Clear AC after saving FLAGS */
81 pushq $__USER32_CS /* pt_regs->cs */
82 pushq $0 /* pt_regs->ip = 0 (placeholder) */
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 */
89 pushq $0 /* pt_regs->r8 = 0 */
90 xorq %r8, %r8 /* nospec r8 */
91 pushq $0 /* pt_regs->r9 = 0 */
92 xorq %r9, %r9 /* nospec r9 */
93 pushq $0 /* pt_regs->r10 = 0 */
94 xorq %r10, %r10 /* nospec r10 */
95 pushq $0 /* pt_regs->r11 = 0 */
96 xorq %r11, %r11 /* nospec r11 */
97 pushq %rbx /* pt_regs->rbx */
98 xorl %ebx, %ebx /* nospec rbx */
99 pushq %rbp /* pt_regs->rbp (will be overwritten) */
100 xorl %ebp, %ebp /* nospec rbp */
101 pushq $0 /* pt_regs->r12 = 0 */
102 xorq %r12, %r12 /* nospec r12 */
103 pushq $0 /* pt_regs->r13 = 0 */
104 xorq %r13, %r13 /* nospec r13 */
105 pushq $0 /* pt_regs->r14 = 0 */
106 xorq %r14, %r14 /* nospec r14 */
107 pushq $0 /* pt_regs->r15 = 0 */
108 xorq %r15, %r15 /* nospec r15 */
112 * Sysenter doesn't filter flags, so we need to clear NT
113 * ourselves. To save a few cycles, we can check whether
114 * NT was set instead of doing an unconditional popfq.
115 * This needs to happen before enabling interrupts so that
116 * we don't get preempted with NT set.
118 * NB.: sysenter_fix_flags is a label with the code under it moved
119 * out-of-line as an optimization: NT is unlikely to be set in the
120 * majority of the cases and instead of polluting the I$ unnecessarily,
121 * we're keeping that code behind a branch which will predict as
122 * not-taken and therefore its instructions won't be fetched.
124 testl $X86_EFLAGS_NT, EFLAGS(%rsp)
125 jnz sysenter_fix_flags
126 sysenter_flags_fixed:
129 * User mode is traced as though IRQs are on, and SYSENTER
135 call do_fast_syscall_32
136 /* XEN PV guests always use IRET path */
137 ALTERNATIVE "testl %eax, %eax; jz .Lsyscall_32_done", \
138 "jmp .Lsyscall_32_done", X86_FEATURE_XENPV
139 jmp sysret32_from_system_call
142 pushq $X86_EFLAGS_FIXED
144 jmp sysenter_flags_fixed
145 ENDPROC(entry_SYSENTER_compat)
148 * 32-bit SYSCALL instruction entry.
150 * 32-bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11,
151 * then loads new ss, cs, and rip from previously programmed MSRs.
152 * rflags gets masked by a value from another MSR (so CLD and CLAC
153 * are not needed). SYSCALL does not save anything on the stack
154 * and does not change rsp.
156 * Note: rflags saving+masking-with-MSR happens only in Long mode
157 * (in legacy 32-bit mode, IF, RF and VM bits are cleared and that's it).
158 * Don't get confused: rflags saving+masking depends on Long Mode Active bit
159 * (EFER.LMA=1), NOT on bitness of userspace where SYSCALL executes
160 * or target CS descriptor's L bit (SYSCALL does not read segment descriptors).
163 * eax system call number
166 * ebp arg2 (note: not saved in the stack frame, should not be touched)
173 ENTRY(entry_SYSCALL_compat)
174 /* Interrupts are off on entry. */
176 SWITCH_KERNEL_CR3_NO_STACK
178 /* Stash user ESP and switch to the kernel stack. */
180 movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
182 /* Zero-extending 32-bit regs, do not remove */
185 /* Construct struct pt_regs on stack */
186 pushq $__USER32_DS /* pt_regs->ss */
187 pushq %r8 /* pt_regs->sp */
188 pushq %r11 /* pt_regs->flags */
189 pushq $__USER32_CS /* pt_regs->cs */
190 pushq %rcx /* pt_regs->ip */
191 pushq %rax /* pt_regs->orig_ax */
192 pushq %rdi /* pt_regs->di */
193 pushq %rsi /* pt_regs->si */
194 pushq %rdx /* pt_regs->dx */
195 pushq %rbp /* pt_regs->cx (stashed in bp) */
196 pushq $-ENOSYS /* pt_regs->ax */
197 pushq $0 /* pt_regs->r8 = 0 */
198 xorq %r8, %r8 /* nospec r8 */
199 pushq $0 /* pt_regs->r9 = 0 */
200 xorq %r9, %r9 /* nospec r9 */
201 pushq $0 /* pt_regs->r10 = 0 */
202 xorq %r10, %r10 /* nospec r10 */
203 pushq $0 /* pt_regs->r11 = 0 */
204 xorq %r11, %r11 /* nospec r11 */
205 pushq %rbx /* pt_regs->rbx */
206 xorl %ebx, %ebx /* nospec rbx */
207 pushq %rbp /* pt_regs->rbp (will be overwritten) */
208 xorl %ebp, %ebp /* nospec rbp */
209 pushq $0 /* pt_regs->r12 = 0 */
210 xorq %r12, %r12 /* nospec r12 */
211 pushq $0 /* pt_regs->r13 = 0 */
212 xorq %r13, %r13 /* nospec r13 */
213 pushq $0 /* pt_regs->r14 = 0 */
214 xorq %r14, %r14 /* nospec r14 */
215 pushq $0 /* pt_regs->r15 = 0 */
216 xorq %r15, %r15 /* nospec r15 */
219 * User mode is traced as though IRQs are on, and SYSENTER
225 call do_fast_syscall_32
226 /* XEN PV guests always use IRET path */
227 ALTERNATIVE "testl %eax, %eax; jz .Lsyscall_32_done", \
228 "jmp .Lsyscall_32_done", X86_FEATURE_XENPV
230 /* Opportunistic SYSRET */
231 sysret32_from_system_call:
232 TRACE_IRQS_ON /* User mode traces as IRQs on. */
234 movq RBX(%rsp), %rbx /* pt_regs->rbx */
235 movq RBP(%rsp), %rbp /* pt_regs->rbp */
236 movq EFLAGS(%rsp), %r11 /* pt_regs->flags (in r11) */
237 movq RIP(%rsp), %rcx /* pt_regs->ip (in rcx) */
238 addq $RAX, %rsp /* Skip r8-r15 */
239 popq %rax /* pt_regs->rax */
240 popq %rdx /* Skip pt_regs->cx */
241 popq %rdx /* pt_regs->dx */
242 popq %rsi /* pt_regs->si */
243 popq %rdi /* pt_regs->di */
246 * USERGS_SYSRET32 does:
247 * GSBASE = user's GS base
253 * ECX will not match pt_regs->cx, but we're returning to a vDSO
254 * trampoline that will fix up RCX, so this is okay.
256 * R12-R15 are callee-saved, so they contain whatever was in them
257 * when the system call started, which is already known to user
258 * code. We zero R8-R10 to avoid info leaks.
263 movq RSP-ORIG_RAX(%rsp), %rsp
265 END(entry_SYSCALL_compat)
268 * Emulated IA32 system calls via int 0x80.
271 * eax system call number
277 * ebp arg6 (note: not saved in the stack frame, should not be touched)
280 * Uses the same stack frame as the x86-64 version.
281 * All registers except eax must be saved (but ptrace may violate that).
282 * Arguments are zero extended. For system calls that want sign extension and
283 * take long arguments a wrapper is needed. Most calls can just be called
285 * Assumes it is only called from user space and entered with interrupts off.
288 ENTRY(entry_INT80_compat)
290 * Interrupts are off on entry.
292 PARAVIRT_ADJUST_EXCEPTION_FRAME
293 ASM_CLAC /* Do this early to minimize exposure */
295 SWITCH_KERNEL_CR3_NO_STACK
298 * User tracing code (ptrace or signal handlers) might assume that
299 * the saved RAX contains a 32-bit number when we're invoking a 32-bit
300 * syscall. Just in case the high bits are nonzero, zero-extend
301 * the syscall number. (This could almost certainly be deleted
302 * with no ill effects.)
306 /* Construct struct pt_regs on stack (iret frame is already on stack) */
307 pushq %rax /* pt_regs->orig_ax */
308 pushq %rdi /* pt_regs->di */
309 pushq %rsi /* pt_regs->si */
310 pushq %rdx /* pt_regs->dx */
311 pushq %rcx /* pt_regs->cx */
312 pushq $-ENOSYS /* pt_regs->ax */
313 pushq $0 /* pt_regs->r8 = 0 */
314 xorq %r8, %r8 /* nospec r8 */
315 pushq $0 /* pt_regs->r9 = 0 */
316 xorq %r9, %r9 /* nospec r9 */
317 pushq $0 /* pt_regs->r10 = 0 */
318 xorq %r10, %r10 /* nospec r10 */
319 pushq $0 /* pt_regs->r11 = 0 */
320 xorq %r11, %r11 /* nospec r11 */
321 pushq %rbx /* pt_regs->rbx */
322 xorl %ebx, %ebx /* nospec rbx */
323 pushq %rbp /* pt_regs->rbp */
324 xorl %ebp, %ebp /* nospec rbp */
325 pushq %r12 /* pt_regs->r12 */
326 xorq %r12, %r12 /* nospec r12 */
327 pushq %r13 /* pt_regs->r13 */
328 xorq %r13, %r13 /* nospec r13 */
329 pushq %r14 /* pt_regs->r14 */
330 xorq %r14, %r14 /* nospec r14 */
331 pushq %r15 /* pt_regs->r15 */
332 xorq %r15, %r15 /* nospec r15 */
336 * User mode is traced as though IRQs are on, and the interrupt
337 * gate turned them off.
342 call do_syscall_32_irqs_off
345 /* Go back to user mode. */
349 jmp restore_regs_and_iret
350 END(entry_INT80_compat)
355 * The 32-bit clone ABI is: clone(..., int tls_val, int *child_tidptr).
356 * The 64-bit clone ABI is: clone(..., int *child_tidptr, int tls_val).
358 * The native 64-bit kernel's sys_clone() implements the latter,
359 * so we need to swap arguments here before calling it: