blob: 06a8966415f90e3d8109627b4b62460832110ecf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Ingo Molnar54ad7262015-06-05 13:02:28 +02002 * Compatibility mode system call entry point for x86-64.
3 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Copyright 2000-2002 Andi Kleen, SuSE Labs.
Ingo Molnar54ad7262015-06-05 13:02:28 +02005 */
Ingo Molnard36f9472015-06-03 18:29:26 +02006#include "calling.h"
Sam Ravnborge2d5df92005-09-09 21:28:48 +02007#include <asm/asm-offsets.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <asm/current.h>
9#include <asm/errno.h>
Ingo Molnar54ad7262015-06-05 13:02:28 +020010#include <asm/ia32_unistd.h>
11#include <asm/thread_info.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <asm/segment.h>
Ingo Molnar2601e642006-07-03 00:24:45 -070013#include <asm/irqflags.h>
H. Peter Anvin1ce6f862012-04-20 12:19:50 -070014#include <asm/asm.h>
H. Peter Anvin63bcff22012-09-21 12:43:12 -070015#include <asm/smap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/linkage.h>
Eric Parisd7e75282012-01-03 14:23:06 -050017#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Roland McGrath5cbf1562008-06-24 01:13:31 -070019/* Avoid __ASSEMBLER__'ifying <linux/audit.h> just for this. */
20#include <linux/elf-em.h>
21#define AUDIT_ARCH_I386 (EM_386|__AUDIT_ARCH_LE)
Ingo Molnar54ad7262015-06-05 13:02:28 +020022#define __AUDIT_ARCH_LE 0x40000000
Roland McGrath5cbf1562008-06-24 01:13:31 -070023
24#ifndef CONFIG_AUDITSYSCALL
Andy Lutomirski8f7f06b2015-07-07 10:55:28 -070025# define sysexit_audit ia32_ret_from_sys_call_irqs_off
26# define sysretl_audit ia32_ret_from_sys_call_irqs_off
Roland McGrath5cbf1562008-06-24 01:13:31 -070027#endif
28
Jiri Olsaea714542011-03-07 19:10:39 +010029 .section .entry.text, "ax"
30
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -040031#ifdef CONFIG_PARAVIRT
32ENTRY(native_usergs_sysret32)
33 swapgs
34 sysretl
35ENDPROC(native_usergs_sysret32)
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -040036#endif
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038/*
Ingo Molnar54ad7262015-06-05 13:02:28 +020039 * 32-bit SYSENTER instruction entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 *
Denys Vlasenkob87cf632015-02-26 14:40:32 -080041 * SYSENTER loads ss, rsp, cs, and rip from previously programmed MSRs.
42 * IF and VM in rflags are cleared (IOW: interrupts are off).
43 * SYSENTER does not save anything on the stack,
44 * and does not save old rip (!!!) and rflags.
45 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * Arguments:
Denys Vlasenkob87cf632015-02-26 14:40:32 -080047 * eax system call number
48 * ebx arg1
49 * ecx arg2
50 * edx arg3
51 * esi arg4
52 * edi arg5
53 * ebp user stack
54 * 0(%ebp) arg6
55 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 * This is purely a fast path. For anything complicated we use the int 0x80
Denys Vlasenkob87cf632015-02-26 14:40:32 -080057 * path below. We set up a complete hardware stack frame to share code
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 * with the int 0x80 path.
Denys Vlasenkob87cf632015-02-26 14:40:32 -080059 */
Ingo Molnar4c8cd0c2015-06-08 08:33:56 +020060ENTRY(entry_SYSENTER_compat)
Andy Lutomirskib611acf2015-10-05 17:47:55 -070061 /* Interrupts are off on entry. */
Jeremy Fitzhardinge457da702008-06-26 07:28:51 -070062 SWAPGS_UNSAFE_STACK
Denys Vlasenko3a232082015-04-24 17:31:35 +020063 movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
Denys Vlasenkoa232e3d2015-03-27 11:36:20 +010064
Denys Vlasenko4ee8ec12015-03-27 11:36:21 +010065 /* Zero-extending 32-bit regs, do not remove */
66 movl %ebp, %ebp
67 movl %eax, %eax
68
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +020069 movl ASM_THREAD_INFO(TI_sysenter_return, %rsp, 0), %r10d
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +020070
71 /* Construct struct pt_regs on stack */
Ingo Molnar131484c2015-05-28 12:21:47 +020072 pushq $__USER32_DS /* pt_regs->ss */
73 pushq %rbp /* pt_regs->sp */
Andy Lutomirskib611acf2015-10-05 17:47:55 -070074
75 /*
76 * Push flags. This is nasty. First, interrupts are currently
77 * off, but we need pt_regs->flags to have IF set. Second, even
78 * if TF was set when SYSENTER started, it's clear by now. We fix
79 * that later using TIF_SINGLESTEP.
80 */
81 pushfq /* pt_regs->flags (except IF = 0) */
82 orl $X86_EFLAGS_IF, (%rsp) /* Fix saved flags */
83
Ingo Molnar131484c2015-05-28 12:21:47 +020084 pushq $__USER32_CS /* pt_regs->cs */
Ingo Molnar54ad7262015-06-05 13:02:28 +020085 pushq %r10 /* pt_regs->ip = thread_info->sysenter_return */
Ingo Molnar131484c2015-05-28 12:21:47 +020086 pushq %rax /* pt_regs->orig_ax */
87 pushq %rdi /* pt_regs->di */
88 pushq %rsi /* pt_regs->si */
89 pushq %rdx /* pt_regs->dx */
90 pushq %rcx /* pt_regs->cx */
91 pushq $-ENOSYS /* pt_regs->ax */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 cld
Ingo Molnar54ad7262015-06-05 13:02:28 +020093 sub $(10*8), %rsp /* pt_regs->r8-11, bp, bx, r12-15 not saved */
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +020094
Denys Vlasenkob87cf632015-02-26 14:40:32 -080095 /*
Andy Lutomirski8c7aa692014-10-01 11:49:04 -070096 * Sysenter doesn't filter flags, so we need to clear NT
97 * ourselves. To save a few cycles, we can check whether
98 * NT was set instead of doing an unconditional popfq.
Andy Lutomirskib611acf2015-10-05 17:47:55 -070099 * This needs to happen before enabling interrupts so that
100 * we don't get preempted with NT set.
Andy Lutomirski8c7aa692014-10-01 11:49:04 -0700101 */
Ingo Molnar54ad7262015-06-05 13:02:28 +0200102 testl $X86_EFLAGS_NT, EFLAGS(%rsp)
103 jnz sysenter_fix_flags
Andy Lutomirski8c7aa692014-10-01 11:49:04 -0700104sysenter_flags_fixed:
105
Andy Lutomirskie62a2542015-10-05 17:48:02 -0700106 /* Temporary: SYSENTER is disabled. */
107#ifdef CONFIG_CONTEXT_TRACKING
108 call enter_from_user_mode
109#endif
110 ENABLE_INTERRUPTS(CLBR_NONE)
111 movl $11, %edi
112 call do_exit
113
Andy Lutomirskidd27f992015-10-05 17:47:53 -0700114 /*
Andy Lutomirskib611acf2015-10-05 17:47:55 -0700115 * Re-enable interrupts. IRQ tracing already thinks that IRQs are
116 * on (since we treat user mode as having IRQs on), and the
117 * prologue above is too short for it to be worth adding a
118 * tracing round trip.
119 */
120 ENABLE_INTERRUPTS(CLBR_NONE)
121
122 /*
Andy Lutomirskidd27f992015-10-05 17:47:53 -0700123 * No need to do an access_ok() check here because RBP has been
124 * 32-bit zero extended:
125 */
126 ASM_STAC
1271: movl (%rbp), %ebp
128 _ASM_EXTABLE(1b, ia32_badarg)
129 ASM_CLAC
130
Denys Vlasenko9b47feb2015-06-08 22:35:33 +0200131 orl $TS_COMPAT, ASM_THREAD_INFO(TI_status, %rsp, SIZEOF_PTREGS)
132 testl $_TIF_WORK_SYSCALL_ENTRY, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Ingo Molnar54ad7262015-06-05 13:02:28 +0200133 jnz sysenter_tracesys
Ingo Molnar131484c2015-05-28 12:21:47 +0200134
Roland McGrathd4d67152008-07-09 02:38:07 -0700135sysenter_do_call:
Ingo Molnar54ad7262015-06-05 13:02:28 +0200136 /* 32-bit syscall -> 64-bit C ABI argument conversion */
137 movl %edi, %r8d /* arg5 */
138 movl %ebp, %r9d /* arg6 */
139 xchg %ecx, %esi /* rsi:arg2, rcx:arg4 */
140 movl %ebx, %edi /* arg1 */
141 movl %edx, %edx /* arg3 (zero extension) */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700142sysenter_dispatch:
Ingo Molnar54ad7262015-06-05 13:02:28 +0200143 cmpq $(IA32_NR_syscalls-1), %rax
Denys Vlasenko3f5159a2015-04-21 18:03:14 +0200144 ja 1f
Ingo Molnar54ad7262015-06-05 13:02:28 +0200145 call *ia32_sys_call_table(, %rax, 8)
146 movq %rax, RAX(%rsp)
Denys Vlasenko3f5159a2015-04-21 18:03:14 +02001471:
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -0400148 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700149 TRACE_IRQS_OFF
Ingo Molnardca5b522015-03-24 19:44:42 +0100150 testl $_TIF_ALLWORK_MASK, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Roland McGrath5cbf1562008-06-24 01:13:31 -0700151 jnz sysexit_audit
152sysexit_from_sys_call:
Andy Lutomirski4214a162015-04-02 17:12:12 -0700153 /*
154 * NB: SYSEXIT is not obviously safe for 64-bit kernels -- an
155 * NMI between STI and SYSEXIT has poorly specified behavior,
156 * and and NMI followed by an IRQ with usergs is fatal. So
157 * we just pretend we're using SYSEXIT but we really use
158 * SYSRETL instead.
159 *
160 * This code path is still called 'sysexit' because it pairs
161 * with 'sysenter' and it uses the SYSENTER calling convention.
162 */
Denys Vlasenko9b47feb2015-06-08 22:35:33 +0200163 andl $~TS_COMPAT, ASM_THREAD_INFO(TI_status, %rsp, SIZEOF_PTREGS)
Ingo Molnar54ad7262015-06-05 13:02:28 +0200164 movl RIP(%rsp), %ecx /* User %eip */
Linus Torvaldscd88ec22015-08-13 16:19:44 -0700165 movq RAX(%rsp), %rax
Denys Vlasenkoc73e36b2015-07-03 22:19:02 +0200166 movl RSI(%rsp), %esi
167 movl RDI(%rsp), %edi
Ingo Molnar54ad7262015-06-05 13:02:28 +0200168 xorl %edx, %edx /* Do not leak kernel information */
169 xorq %r8, %r8
170 xorq %r9, %r9
171 xorq %r10, %r10
172 movl EFLAGS(%rsp), %r11d /* User eflags */
Ingo Molnar2601e642006-07-03 00:24:45 -0700173 TRACE_IRQS_ON
Andy Lutomirski4214a162015-04-02 17:12:12 -0700174
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800175 /*
Andy Lutomirski4214a162015-04-02 17:12:12 -0700176 * SYSRETL works even on Intel CPUs. Use it in preference to SYSEXIT,
177 * since it avoids a dicey window with interrupts enabled.
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800178 */
Ingo Molnar54ad7262015-06-05 13:02:28 +0200179 movl RSP(%rsp), %esp
Andy Lutomirski4214a162015-04-02 17:12:12 -0700180
181 /*
182 * USERGS_SYSRET32 does:
183 * gsbase = user's gs base
184 * eip = ecx
185 * rflags = r11
186 * cs = __USER32_CS
187 * ss = __USER_DS
188 *
189 * The prologue set RIP(%rsp) to VDSO32_SYSENTER_RETURN, which does:
190 *
191 * pop %ebp
192 * pop %edx
193 * pop %ecx
194 *
195 * Therefore, we invoke SYSRETL with EDX and R8-R10 zeroed to
196 * avoid info leaks. R11 ends up with VDSO32_SYSENTER_RETURN's
197 * address (already known to user code), and R12-R15 are
198 * callee-saved and therefore don't contain any interesting
199 * kernel data.
200 */
201 USERGS_SYSRET32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Roland McGrath5cbf1562008-06-24 01:13:31 -0700203#ifdef CONFIG_AUDITSYSCALL
204 .macro auditsys_entry_common
Denys Vlasenko1536bb42015-06-09 20:54:08 +0200205 /*
Denys Vlasenkoa92fde252015-06-09 20:54:09 +0200206 * At this point, registers hold syscall args in the 32-bit syscall ABI:
207 * EAX is syscall number, the 6 args are in EBX,ECX,EDX,ESI,EDI,EBP.
208 *
209 * We want to pass them to __audit_syscall_entry(), which is a 64-bit
210 * C function with 5 parameters, so shuffle them to match what
211 * the function expects: RDI,RSI,RDX,RCX,R8.
212 */
213 movl %esi, %r8d /* arg5 (R8 ) <= 4th syscall arg (ESI) */
214 xchg %ecx, %edx /* arg4 (RCX) <= 3rd syscall arg (EDX) */
215 /* arg3 (RDX) <= 2nd syscall arg (ECX) */
216 movl %ebx, %esi /* arg2 (RSI) <= 1st syscall arg (EBX) */
217 movl %eax, %edi /* arg1 (RDI) <= syscall number (EAX) */
218 call __audit_syscall_entry
219
220 /*
221 * We are going to jump back to the syscall dispatch code.
222 * Prepare syscall args as required by the 64-bit C ABI.
223 * Registers clobbered by __audit_syscall_entry() are
224 * loaded from pt_regs on stack:
Denys Vlasenko1536bb42015-06-09 20:54:08 +0200225 */
226 movl ORIG_RAX(%rsp), %eax /* syscall number */
227 movl %ebx, %edi /* arg1 */
228 movl RCX(%rsp), %esi /* arg2 */
229 movl RDX(%rsp), %edx /* arg3 */
230 movl RSI(%rsp), %ecx /* arg4 */
231 movl RDI(%rsp), %r8d /* arg5 */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700232 .endm
233
Jan Beulich81766742009-10-26 15:20:29 +0000234 .macro auditsys_exit exit
Roland McGrath5cbf1562008-06-24 01:13:31 -0700235 TRACE_IRQS_ON
Jan Beulich40a1ef92013-01-30 07:55:53 +0000236 ENABLE_INTERRUPTS(CLBR_NONE)
Andy Lutomirski29ea1b22015-07-03 12:44:28 -0700237 testl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT), ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
238 jnz ia32_ret_from_sys_call
Ingo Molnar54ad7262015-06-05 13:02:28 +0200239 movl %eax, %esi /* second arg, syscall return value */
240 cmpl $-MAX_ERRNO, %eax /* is it an error ? */
241 jbe 1f
242 movslq %eax, %rsi /* if error sign extend to 64 bits */
2431: setbe %al /* 1 if error, 0 if not */
244 movzbl %al, %edi /* zero-extend that into %edi */
245 call __audit_syscall_exit
Ingo Molnar54ad7262015-06-05 13:02:28 +0200246 movl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT), %edi
Jan Beulich40a1ef92013-01-30 07:55:53 +0000247 DISABLE_INTERRUPTS(CLBR_NONE)
Roland McGrath5cbf1562008-06-24 01:13:31 -0700248 TRACE_IRQS_OFF
Ingo Molnar54ad7262015-06-05 13:02:28 +0200249 testl %edi, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
250 jz \exit
251 xorl %eax, %eax /* Do not leak kernel information */
Denys Vlasenkoef0cd5d2015-06-02 21:04:01 +0200252 movq %rax, R11(%rsp)
253 movq %rax, R10(%rsp)
254 movq %rax, R9(%rsp)
255 movq %rax, R8(%rsp)
Andy Lutomirski29ea1b22015-07-03 12:44:28 -0700256 jmp int_ret_from_sys_call_irqs_off
Roland McGrath5cbf1562008-06-24 01:13:31 -0700257 .endm
258
259sysenter_auditsys:
Roland McGrath5cbf1562008-06-24 01:13:31 -0700260 auditsys_entry_common
Denys Vlasenkoc0c33222015-07-24 14:16:43 +0200261 movl %ebp, %r9d /* reload 6th syscall arg */
Ingo Molnar54ad7262015-06-05 13:02:28 +0200262 jmp sysenter_dispatch
Roland McGrath5cbf1562008-06-24 01:13:31 -0700263
264sysexit_audit:
265 auditsys_exit sysexit_from_sys_call
266#endif
267
Andy Lutomirski8c7aa692014-10-01 11:49:04 -0700268sysenter_fix_flags:
Andy Lutomirskib611acf2015-10-05 17:47:55 -0700269 pushq $X86_EFLAGS_FIXED
Ingo Molnar131484c2015-05-28 12:21:47 +0200270 popfq
Ingo Molnar54ad7262015-06-05 13:02:28 +0200271 jmp sysenter_flags_fixed
Andy Lutomirski8c7aa692014-10-01 11:49:04 -0700272
Roland McGrath5cbf1562008-06-24 01:13:31 -0700273sysenter_tracesys:
Roland McGrath5cbf1562008-06-24 01:13:31 -0700274#ifdef CONFIG_AUDITSYSCALL
Ingo Molnardca5b522015-03-24 19:44:42 +0100275 testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT), ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Roland McGrath5cbf1562008-06-24 01:13:31 -0700276 jz sysenter_auditsys
277#endif
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800278 SAVE_EXTRA_REGS
Ingo Molnar54ad7262015-06-05 13:02:28 +0200279 xorl %eax, %eax /* Do not leak kernel information */
Denys Vlasenkoef0cd5d2015-06-02 21:04:01 +0200280 movq %rax, R11(%rsp)
281 movq %rax, R10(%rsp)
282 movq %rax, R9(%rsp)
283 movq %rax, R8(%rsp)
Ingo Molnar54ad7262015-06-05 13:02:28 +0200284 movq %rsp, %rdi /* &pt_regs -> arg1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 call syscall_trace_enter
Denys Vlasenko73cbf682015-06-02 21:04:02 +0200286
287 /* Reload arg registers from stack. (see sysenter_tracesys) */
288 movl RCX(%rsp), %ecx
289 movl RDX(%rsp), %edx
290 movl RSI(%rsp), %esi
291 movl RDI(%rsp), %edi
Ingo Molnar54ad7262015-06-05 13:02:28 +0200292 movl %eax, %eax /* zero extension */
Denys Vlasenko73cbf682015-06-02 21:04:02 +0200293
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800294 RESTORE_EXTRA_REGS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 jmp sysenter_do_call
Ingo Molnar4c8cd0c2015-06-08 08:33:56 +0200296ENDPROC(entry_SYSENTER_compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298/*
Ingo Molnar54ad7262015-06-05 13:02:28 +0200299 * 32-bit SYSCALL instruction entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 *
Ingo Molnar54ad7262015-06-05 13:02:28 +0200301 * 32-bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11,
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800302 * then loads new ss, cs, and rip from previously programmed MSRs.
303 * rflags gets masked by a value from another MSR (so CLD and CLAC
304 * are not needed). SYSCALL does not save anything on the stack
305 * and does not change rsp.
306 *
307 * Note: rflags saving+masking-with-MSR happens only in Long mode
Ingo Molnar54ad7262015-06-05 13:02:28 +0200308 * (in legacy 32-bit mode, IF, RF and VM bits are cleared and that's it).
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800309 * Don't get confused: rflags saving+masking depends on Long Mode Active bit
310 * (EFER.LMA=1), NOT on bitness of userspace where SYSCALL executes
311 * or target CS descriptor's L bit (SYSCALL does not read segment descriptors).
312 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 * Arguments:
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800314 * eax system call number
315 * ecx return address
316 * ebx arg1
317 * ebp arg2 (note: not saved in the stack frame, should not be touched)
318 * edx arg3
319 * esi arg4
320 * edi arg5
321 * esp user stack
322 * 0(%esp) arg6
323 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 * This is purely a fast path. For anything complicated we use the int 0x80
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800325 * path below. We set up a complete hardware stack frame to share code
326 * with the int 0x80 path.
327 */
Ingo Molnar2cd23552015-06-08 08:28:07 +0200328ENTRY(entry_SYSCALL_compat)
Denys Vlasenkoa232e3d2015-03-27 11:36:20 +0100329 /*
330 * Interrupts are off on entry.
331 * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
332 * it is too small to ever cause noticeable irq latency.
333 */
Jeremy Fitzhardinge457da702008-06-26 07:28:51 -0700334 SWAPGS_UNSAFE_STACK
Andy Lutomirskie62a2542015-10-05 17:48:02 -0700335
336 /* Temporary: SYSCALL32 is disabled. */
337 movl $-ENOSYS, %eax
338 USERGS_SYSRET32
339
Ingo Molnar54ad7262015-06-05 13:02:28 +0200340 movl %esp, %r8d
341 movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -0400342 ENABLE_INTERRUPTS(CLBR_NONE)
Denys Vlasenkoa232e3d2015-03-27 11:36:20 +0100343
Denys Vlasenko4ee8ec12015-03-27 11:36:21 +0100344 /* Zero-extending 32-bit regs, do not remove */
Ingo Molnar54ad7262015-06-05 13:02:28 +0200345 movl %eax, %eax
Denys Vlasenko4ee8ec12015-03-27 11:36:21 +0100346
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +0200347 /* Construct struct pt_regs on stack */
Ingo Molnar131484c2015-05-28 12:21:47 +0200348 pushq $__USER32_DS /* pt_regs->ss */
349 pushq %r8 /* pt_regs->sp */
350 pushq %r11 /* pt_regs->flags */
351 pushq $__USER32_CS /* pt_regs->cs */
352 pushq %rcx /* pt_regs->ip */
353 pushq %rax /* pt_regs->orig_ax */
354 pushq %rdi /* pt_regs->di */
355 pushq %rsi /* pt_regs->si */
356 pushq %rdx /* pt_regs->dx */
357 pushq %rbp /* pt_regs->cx */
Ingo Molnar54ad7262015-06-05 13:02:28 +0200358 movl %ebp, %ecx
Ingo Molnar131484c2015-05-28 12:21:47 +0200359 pushq $-ENOSYS /* pt_regs->ax */
Ingo Molnar54ad7262015-06-05 13:02:28 +0200360 sub $(10*8), %rsp /* pt_regs->r8-11, bp, bx, r12-15 not saved */
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +0200361
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800362 /*
Ingo Molnar54ad7262015-06-05 13:02:28 +0200363 * No need to do an access_ok check here because r8 has been
364 * 32-bit zero extended:
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800365 */
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700366 ASM_STAC
Denys Vlasenkoc0c33222015-07-24 14:16:43 +02003671: movl (%r8), %r9d
Ingo Molnar54ad7262015-06-05 13:02:28 +0200368 _ASM_EXTABLE(1b, ia32_badarg)
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700369 ASM_CLAC
Denys Vlasenko9b47feb2015-06-08 22:35:33 +0200370 orl $TS_COMPAT, ASM_THREAD_INFO(TI_status, %rsp, SIZEOF_PTREGS)
371 testl $_TIF_WORK_SYSCALL_ENTRY, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
372 jnz cstar_tracesys
Ingo Molnar131484c2015-05-28 12:21:47 +0200373
Jan Beulich295286a2008-08-29 13:21:11 +0100374cstar_do_call:
Ingo Molnar54ad7262015-06-05 13:02:28 +0200375 /* 32-bit syscall -> 64-bit C ABI argument conversion */
376 movl %edi, %r8d /* arg5 */
Denys Vlasenkoc0c33222015-07-24 14:16:43 +0200377 /* r9 already loaded */ /* arg6 */
Ingo Molnar54ad7262015-06-05 13:02:28 +0200378 xchg %ecx, %esi /* rsi:arg2, rcx:arg4 */
379 movl %ebx, %edi /* arg1 */
380 movl %edx, %edx /* arg3 (zero extension) */
381
Roland McGrath5cbf1562008-06-24 01:13:31 -0700382cstar_dispatch:
Ingo Molnar54ad7262015-06-05 13:02:28 +0200383 cmpq $(IA32_NR_syscalls-1), %rax
Denys Vlasenko3f5159a2015-04-21 18:03:14 +0200384 ja 1f
Ingo Molnar54ad7262015-06-05 13:02:28 +0200385
386 call *ia32_sys_call_table(, %rax, 8)
387 movq %rax, RAX(%rsp)
Denys Vlasenko3f5159a2015-04-21 18:03:14 +02003881:
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -0400389 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700390 TRACE_IRQS_OFF
Ingo Molnar54ad7262015-06-05 13:02:28 +0200391 testl $_TIF_ALLWORK_MASK, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
392 jnz sysretl_audit
393
Roland McGrath5cbf1562008-06-24 01:13:31 -0700394sysretl_from_sys_call:
Ingo Molnar54ad7262015-06-05 13:02:28 +0200395 andl $~TS_COMPAT, ASM_THREAD_INFO(TI_status, %rsp, SIZEOF_PTREGS)
Denys Vlasenkoc73e36b2015-07-03 22:19:02 +0200396 movl RDX(%rsp), %edx
397 movl RSI(%rsp), %esi
398 movl RDI(%rsp), %edi
Ingo Molnar54ad7262015-06-05 13:02:28 +0200399 movl RIP(%rsp), %ecx
400 movl EFLAGS(%rsp), %r11d
Linus Torvaldscd88ec22015-08-13 16:19:44 -0700401 movq RAX(%rsp), %rax
Ingo Molnar54ad7262015-06-05 13:02:28 +0200402 xorq %r10, %r10
403 xorq %r9, %r9
404 xorq %r8, %r8
Ingo Molnar2601e642006-07-03 00:24:45 -0700405 TRACE_IRQS_ON
Ingo Molnar54ad7262015-06-05 13:02:28 +0200406 movl RSP(%rsp), %esp
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800407 /*
Ingo Molnar54ad7262015-06-05 13:02:28 +0200408 * 64-bit->32-bit SYSRET restores eip from ecx,
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800409 * eflags from r11 (but RF and VM bits are forced to 0),
410 * cs and ss are loaded from MSRs.
Ingo Molnar54ad7262015-06-05 13:02:28 +0200411 * (Note: 32-bit->32-bit SYSRET is different: since r11
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800412 * does not exist, it merely sets eflags.IF=1).
Andy Lutomirski61f01dd2015-04-26 16:47:59 -0700413 *
414 * NB: On AMD CPUs with the X86_BUG_SYSRET_SS_ATTRS bug, the ss
415 * descriptor is not reinitialized. This means that we must
416 * avoid SYSRET with SS == NULL, which could happen if we schedule,
417 * exit the kernel, and re-enter using an interrupt vector. (All
418 * interrupt entries on x86_64 set SS to NULL.) We prevent that
419 * from happening by reloading SS in __switch_to.
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800420 */
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -0400421 USERGS_SYSRET32
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800422
Roland McGrath5cbf1562008-06-24 01:13:31 -0700423#ifdef CONFIG_AUDITSYSCALL
424cstar_auditsys:
Denys Vlasenkoc0c33222015-07-24 14:16:43 +0200425 movl %r9d, R9(%rsp) /* register to be clobbered by call */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700426 auditsys_entry_common
Denys Vlasenkoc0c33222015-07-24 14:16:43 +0200427 movl R9(%rsp), %r9d /* reload 6th syscall arg */
Ingo Molnar54ad7262015-06-05 13:02:28 +0200428 jmp cstar_dispatch
Roland McGrath5cbf1562008-06-24 01:13:31 -0700429
430sysretl_audit:
Jan Beulich81766742009-10-26 15:20:29 +0000431 auditsys_exit sysretl_from_sys_call
Roland McGrath5cbf1562008-06-24 01:13:31 -0700432#endif
433
434cstar_tracesys:
435#ifdef CONFIG_AUDITSYSCALL
Ingo Molnar54ad7262015-06-05 13:02:28 +0200436 testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT), ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
437 jz cstar_auditsys
Roland McGrath5cbf1562008-06-24 01:13:31 -0700438#endif
Denys Vlasenkoc0c33222015-07-24 14:16:43 +0200439 xchgl %r9d, %ebp
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800440 SAVE_EXTRA_REGS
Ingo Molnar54ad7262015-06-05 13:02:28 +0200441 xorl %eax, %eax /* Do not leak kernel information */
Denys Vlasenkoef0cd5d2015-06-02 21:04:01 +0200442 movq %rax, R11(%rsp)
443 movq %rax, R10(%rsp)
Denys Vlasenkoc0c33222015-07-24 14:16:43 +0200444 movq %r9, R9(%rsp)
Denys Vlasenkoef0cd5d2015-06-02 21:04:01 +0200445 movq %rax, R8(%rsp)
Ingo Molnar54ad7262015-06-05 13:02:28 +0200446 movq %rsp, %rdi /* &pt_regs -> arg1 */
Denys Vlasenko53e9acc2015-06-03 14:56:09 +0200447 call syscall_trace_enter
Denys Vlasenkoc0c33222015-07-24 14:16:43 +0200448 movl R9(%rsp), %r9d
Denys Vlasenko73cbf682015-06-02 21:04:02 +0200449
450 /* Reload arg registers from stack. (see sysenter_tracesys) */
451 movl RCX(%rsp), %ecx
452 movl RDX(%rsp), %edx
453 movl RSI(%rsp), %esi
454 movl RDI(%rsp), %edi
Ingo Molnar54ad7262015-06-05 13:02:28 +0200455 movl %eax, %eax /* zero extension */
Denys Vlasenko73cbf682015-06-02 21:04:02 +0200456
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800457 RESTORE_EXTRA_REGS
Denys Vlasenkoc0c33222015-07-24 14:16:43 +0200458 xchgl %ebp, %r9d
Denys Vlasenko53e9acc2015-06-03 14:56:09 +0200459 jmp cstar_do_call
Ingo Molnar2cd23552015-06-08 08:28:07 +0200460END(entry_SYSCALL_compat)
Ingo Molnar54ad7262015-06-05 13:02:28 +0200461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462ia32_badarg:
Andy Lutomirski5e99cb72015-07-03 12:44:19 -0700463 /*
464 * So far, we've entered kernel mode, set AC, turned on IRQs, and
465 * saved C regs except r8-r11. We haven't done any of the other
466 * standard entry work, though. We want to bail, but we shouldn't
467 * treat this as a syscall entry since we don't even know what the
468 * args are. Instead, treat this as a non-syscall entry, finish
469 * the entry work, and immediately exit after setting AX = -EFAULT.
470 *
471 * We're really just being polite here. Killing the task outright
472 * would be a reasonable action, too. Given that the only valid
473 * way to have gotten here is through the vDSO, and we already know
474 * that the stack pointer is bad, the task isn't going to survive
475 * for long no matter what we do.
476 */
477
478 ASM_CLAC /* undo STAC */
479 movq $-EFAULT, RAX(%rsp) /* return -EFAULT if possible */
480
481 /* Fill in the rest of pt_regs */
482 xorl %eax, %eax
483 movq %rax, R11(%rsp)
484 movq %rax, R10(%rsp)
485 movq %rax, R9(%rsp)
486 movq %rax, R8(%rsp)
487 SAVE_EXTRA_REGS
488
489 /* Turn IRQs back off. */
490 DISABLE_INTERRUPTS(CLBR_NONE)
491 TRACE_IRQS_OFF
492
Andy Lutomirski02bc7762015-07-03 12:44:31 -0700493 /* Now finish entering normal kernel mode. */
494#ifdef CONFIG_CONTEXT_TRACKING
495 call enter_from_user_mode
496#endif
497
Andy Lutomirski5e99cb72015-07-03 12:44:19 -0700498 /* And exit again. */
499 jmp retint_user
500
Andy Lutomirski8f7f06b2015-07-07 10:55:28 -0700501ia32_ret_from_sys_call_irqs_off:
502 TRACE_IRQS_ON
503 ENABLE_INTERRUPTS(CLBR_NONE)
504
Denys Vlasenko61b1e3e2015-06-02 19:35:10 +0200505ia32_ret_from_sys_call:
Ingo Molnar54ad7262015-06-05 13:02:28 +0200506 xorl %eax, %eax /* Do not leak kernel information */
Denys Vlasenkoef0cd5d2015-06-02 21:04:01 +0200507 movq %rax, R11(%rsp)
508 movq %rax, R10(%rsp)
509 movq %rax, R9(%rsp)
510 movq %rax, R8(%rsp)
Ingo Molnar54ad7262015-06-05 13:02:28 +0200511 jmp int_ret_from_sys_call
Denys Vlasenko61b1e3e2015-06-02 19:35:10 +0200512
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800513/*
514 * Emulated IA32 system calls via int 0x80.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 *
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800516 * Arguments:
517 * eax system call number
518 * ebx arg1
519 * ecx arg2
520 * edx arg3
521 * esi arg4
522 * edi arg5
523 * ebp arg6 (note: not saved in the stack frame, should not be touched)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 *
525 * Notes:
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800526 * Uses the same stack frame as the x86-64 version.
527 * All registers except eax must be saved (but ptrace may violate that).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 * Arguments are zero extended. For system calls that want sign extension and
529 * take long arguments a wrapper is needed. Most calls can just be called
530 * directly.
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800531 * Assumes it is only called from user space and entered with interrupts off.
532 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Ingo Molnar2cd23552015-06-08 08:28:07 +0200534ENTRY(entry_INT80_compat)
Denys Vlasenkoa232e3d2015-03-27 11:36:20 +0100535 /*
536 * Interrupts are off on entry.
537 * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
538 * it is too small to ever cause noticeable irq latency.
539 */
Jeremy Fitzhardinge360c0442008-07-08 15:06:28 -0700540 PARAVIRT_ADJUST_EXCEPTION_FRAME
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -0400541 SWAPGS
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -0400542 ENABLE_INTERRUPTS(CLBR_NONE)
Denys Vlasenkoa232e3d2015-03-27 11:36:20 +0100543
Denys Vlasenko4ee8ec12015-03-27 11:36:21 +0100544 /* Zero-extending 32-bit regs, do not remove */
Ingo Molnar54ad7262015-06-05 13:02:28 +0200545 movl %eax, %eax
Denys Vlasenko4ee8ec12015-03-27 11:36:21 +0100546
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +0200547 /* Construct struct pt_regs on stack (iret frame is already on stack) */
Ingo Molnar131484c2015-05-28 12:21:47 +0200548 pushq %rax /* pt_regs->orig_ax */
549 pushq %rdi /* pt_regs->di */
550 pushq %rsi /* pt_regs->si */
551 pushq %rdx /* pt_regs->dx */
552 pushq %rcx /* pt_regs->cx */
553 pushq $-ENOSYS /* pt_regs->ax */
Denys Vlasenko61b1e3e2015-06-02 19:35:10 +0200554 pushq $0 /* pt_regs->r8 */
555 pushq $0 /* pt_regs->r9 */
556 pushq $0 /* pt_regs->r10 */
557 pushq $0 /* pt_regs->r11 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 cld
Ingo Molnar54ad7262015-06-05 13:02:28 +0200559 sub $(6*8), %rsp /* pt_regs->bp, bx, r12-15 not saved */
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +0200560
Ingo Molnar54ad7262015-06-05 13:02:28 +0200561 orl $TS_COMPAT, ASM_THREAD_INFO(TI_status, %rsp, SIZEOF_PTREGS)
562 testl $_TIF_WORK_SYSCALL_ENTRY, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
563 jnz ia32_tracesys
564
Roland McGrathc09249f2009-02-06 18:15:18 -0800565ia32_do_call:
Ingo Molnar54ad7262015-06-05 13:02:28 +0200566 /* 32-bit syscall -> 64-bit C ABI argument conversion */
567 movl %edi, %r8d /* arg5 */
568 movl %ebp, %r9d /* arg6 */
569 xchg %ecx, %esi /* rsi:arg2, rcx:arg4 */
570 movl %ebx, %edi /* arg1 */
571 movl %edx, %edx /* arg3 (zero extension) */
572 cmpq $(IA32_NR_syscalls-1), %rax
Denys Vlasenko3f5159a2015-04-21 18:03:14 +0200573 ja 1f
Ingo Molnar54ad7262015-06-05 13:02:28 +0200574
Denys Vlasenkoeb478542015-06-07 20:24:30 +0200575 call *ia32_sys_call_table(, %rax, 8)
Ingo Molnar54ad7262015-06-05 13:02:28 +0200576 movq %rax, RAX(%rsp)
Denys Vlasenko3f5159a2015-04-21 18:03:14 +02005771:
Ingo Molnar54ad7262015-06-05 13:02:28 +0200578 jmp int_ret_from_sys_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800580ia32_tracesys:
581 SAVE_EXTRA_REGS
Ingo Molnar54ad7262015-06-05 13:02:28 +0200582 movq %rsp, %rdi /* &pt_regs -> arg1 */
583 call syscall_trace_enter
Denys Vlasenko73cbf682015-06-02 21:04:02 +0200584 /*
585 * Reload arg registers from stack in case ptrace changed them.
586 * Don't reload %eax because syscall_trace_enter() returned
587 * the %rax value we should see. But do truncate it to 32 bits.
588 * If it's -1 to make us punt the syscall, then (u32)-1 is still
589 * an appropriately invalid value.
590 */
591 movl RCX(%rsp), %ecx
592 movl RDX(%rsp), %edx
593 movl RSI(%rsp), %esi
594 movl RDI(%rsp), %edi
Ingo Molnar54ad7262015-06-05 13:02:28 +0200595 movl %eax, %eax /* zero extension */
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800596 RESTORE_EXTRA_REGS
Ingo Molnar54ad7262015-06-05 13:02:28 +0200597 jmp ia32_do_call
Ingo Molnar2cd23552015-06-08 08:28:07 +0200598END(entry_INT80_compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Ramkumar Ramachandrad2475b82013-07-10 23:34:28 +0530600 .macro PTREGSCALL label, func
Jan Beulichf6b2bc82011-11-29 11:24:10 +0000601 ALIGN
602GLOBAL(\label)
Ingo Molnar54ad7262015-06-05 13:02:28 +0200603 leaq \func(%rip), %rax
604 jmp ia32_ptregs_common
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 .endm
606
Ingo Molnar54ad7262015-06-05 13:02:28 +0200607 PTREGSCALL stub32_rt_sigreturn, sys32_rt_sigreturn
608 PTREGSCALL stub32_sigreturn, sys32_sigreturn
609 PTREGSCALL stub32_fork, sys_fork
610 PTREGSCALL stub32_vfork, sys_vfork
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Jan Beulichf6b2bc82011-11-29 11:24:10 +0000612 ALIGN
Al Viro1d4b4b22012-10-22 22:34:11 -0400613GLOBAL(stub32_clone)
Ingo Molnar54ad7262015-06-05 13:02:28 +0200614 leaq sys_clone(%rip), %rax
Denys Vlasenko5cdc6832015-06-03 15:58:49 +0200615 /*
Denys Vlasenko7a5a9822015-06-03 15:58:50 +0200616 * The 32-bit clone ABI is: clone(..., int tls_val, int *child_tidptr).
617 * The 64-bit clone ABI is: clone(..., int *child_tidptr, int tls_val).
618 *
619 * The native 64-bit kernel's sys_clone() implements the latter,
620 * so we need to swap arguments here before calling it:
Denys Vlasenko5cdc6832015-06-03 15:58:49 +0200621 */
Denys Vlasenko7a5a9822015-06-03 15:58:50 +0200622 xchg %r8, %rcx
Ingo Molnar54ad7262015-06-05 13:02:28 +0200623 jmp ia32_ptregs_common
Al Viro1d4b4b22012-10-22 22:34:11 -0400624
625 ALIGN
Jan Beulichf6b2bc82011-11-29 11:24:10 +0000626ia32_ptregs_common:
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800627 SAVE_EXTRA_REGS 8
Ingo Molnar54ad7262015-06-05 13:02:28 +0200628 call *%rax
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800629 RESTORE_EXTRA_REGS 8
630 ret
Jan Beulich4b787e02006-06-26 13:56:55 +0200631END(ia32_ptregs_common)