GNU Linux-libre 4.4.289-gnu1
[releases.git] / arch / x86 / entry / entry_64_compat.S
1 /*
2  * Compatibility mode system call entry point for x86-64.
3  *
4  * Copyright 2000-2002 Andi Kleen, SuSE Labs.
5  */
6 #include "calling.h"
7 #include <asm/asm-offsets.h>
8 #include <asm/current.h>
9 #include <asm/errno.h>
10 #include <asm/ia32_unistd.h>
11 #include <asm/thread_info.h>
12 #include <asm/segment.h>
13 #include <asm/irqflags.h>
14 #include <asm/asm.h>
15 #include <asm/smap.h>
16 #include <asm/pgtable_types.h>
17 #include <asm/kaiser.h>
18 #include <linux/linkage.h>
19 #include <linux/err.h>
20
21         .section .entry.text, "ax"
22
23 #ifdef CONFIG_PARAVIRT
24 ENTRY(native_usergs_sysret32)
25         swapgs
26         sysretl
27 ENDPROC(native_usergs_sysret32)
28 #endif
29
30 /*
31  * 32-bit SYSENTER instruction entry.
32  *
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.
37  *
38  * Arguments:
39  * eax  system call number
40  * ebx  arg1
41  * ecx  arg2
42  * edx  arg3
43  * esi  arg4
44  * edi  arg5
45  * ebp  user stack
46  * 0(%ebp) arg6
47  *
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.
51  */
52 ENTRY(entry_SYSENTER_compat)
53         /* Interrupts are off on entry. */
54         SWAPGS_UNSAFE_STACK
55         SWITCH_KERNEL_CR3_NO_STACK
56         movq    PER_CPU_VAR(cpu_current_top_of_stack), %rsp
57
58         /*
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.)
64          */
65         movl    %eax, %eax
66
67         /* Construct struct pt_regs on stack */
68         pushq   $__USER32_DS            /* pt_regs->ss */
69         pushq   %rbp                    /* pt_regs->sp (stashed in bp) */
70
71         /*
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.
76          */
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 */
80
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 */
109         cld
110
111         /*
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.
117          *
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.
123          */
124         testl   $X86_EFLAGS_NT, EFLAGS(%rsp)
125         jnz     sysenter_fix_flags
126 sysenter_flags_fixed:
127
128         /*
129          * User mode is traced as though IRQs are on, and SYSENTER
130          * turned them off.
131          */
132         TRACE_IRQS_OFF
133
134         movq    %rsp, %rdi
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
140
141 sysenter_fix_flags:
142         pushq   $X86_EFLAGS_FIXED
143         popfq
144         jmp     sysenter_flags_fixed
145 ENDPROC(entry_SYSENTER_compat)
146
147 /*
148  * 32-bit SYSCALL instruction entry.
149  *
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.
155  *
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).
161  *
162  * Arguments:
163  * eax  system call number
164  * ecx  return address
165  * ebx  arg1
166  * ebp  arg2    (note: not saved in the stack frame, should not be touched)
167  * edx  arg3
168  * esi  arg4
169  * edi  arg5
170  * esp  user stack
171  * 0(%esp) arg6
172  */
173 ENTRY(entry_SYSCALL_compat)
174         /* Interrupts are off on entry. */
175         SWAPGS_UNSAFE_STACK
176         SWITCH_KERNEL_CR3_NO_STACK
177
178         /* Stash user ESP and switch to the kernel stack. */
179         movl    %esp, %r8d
180         movq    PER_CPU_VAR(cpu_current_top_of_stack), %rsp
181
182         /* Zero-extending 32-bit regs, do not remove */
183         movl    %eax, %eax
184
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 */
217
218         /*
219          * User mode is traced as though IRQs are on, and SYSENTER
220          * turned them off.
221          */
222         TRACE_IRQS_OFF
223
224         movq    %rsp, %rdi
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
229
230         /* Opportunistic SYSRET */
231 sysret32_from_system_call:
232         TRACE_IRQS_ON                   /* User mode traces as IRQs on. */
233         SWITCH_USER_CR3
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 */
244
245         /*
246          * USERGS_SYSRET32 does:
247          *  GSBASE = user's GS base
248          *  EIP = ECX
249          *  RFLAGS = R11
250          *  CS = __USER32_CS
251          *  SS = __USER_DS
252          *
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.
255          *
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.
259          */
260         xorq    %r8, %r8
261         xorq    %r9, %r9
262         xorq    %r10, %r10
263         movq    RSP-ORIG_RAX(%rsp), %rsp
264         USERGS_SYSRET32
265 END(entry_SYSCALL_compat)
266
267 /*
268  * Emulated IA32 system calls via int 0x80.
269  *
270  * Arguments:
271  * eax  system call number
272  * ebx  arg1
273  * ecx  arg2
274  * edx  arg3
275  * esi  arg4
276  * edi  arg5
277  * ebp  arg6    (note: not saved in the stack frame, should not be touched)
278  *
279  * Notes:
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
284  * directly.
285  * Assumes it is only called from user space and entered with interrupts off.
286  */
287
288 ENTRY(entry_INT80_compat)
289         /*
290          * Interrupts are off on entry.
291          */
292         PARAVIRT_ADJUST_EXCEPTION_FRAME
293         ASM_CLAC                        /* Do this early to minimize exposure */
294         SWAPGS
295         SWITCH_KERNEL_CR3_NO_STACK
296
297         /*
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.)
303          */
304         movl    %eax, %eax
305
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 */
333         cld
334
335         /*
336          * User mode is traced as though IRQs are on, and the interrupt
337          * gate turned them off.
338          */
339         TRACE_IRQS_OFF
340
341         movq    %rsp, %rdi
342         call    do_syscall_32_irqs_off
343 .Lsyscall_32_done:
344
345         /* Go back to user mode. */
346         TRACE_IRQS_ON
347         SWITCH_USER_CR3
348         SWAPGS
349         jmp     restore_regs_and_iret
350 END(entry_INT80_compat)
351
352         ALIGN
353 GLOBAL(stub32_clone)
354         /*
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).
357          *
358          * The native 64-bit kernel's sys_clone() implements the latter,
359          * so we need to swap arguments here before calling it:
360          */
361         xchg    %r8, %rcx
362         jmp     sys_clone