blob: 0fa108cdf182d70832c5434c765768d6b3c010c2 [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
Ingo Molnar54ad7262015-06-05 13:02:28 +020025# define sysexit_audit ia32_ret_from_sys_call
26# define sysretl_audit ia32_ret_from_sys_call
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)
Denys Vlasenkoa232e3d2015-03-27 11:36:20 +010061 /*
62 * Interrupts are off on entry.
63 * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
64 * it is too small to ever cause noticeable irq latency.
65 */
Jeremy Fitzhardinge457da702008-06-26 07:28:51 -070066 SWAPGS_UNSAFE_STACK
Denys Vlasenko3a232082015-04-24 17:31:35 +020067 movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -040068 ENABLE_INTERRUPTS(CLBR_NONE)
Denys Vlasenkoa232e3d2015-03-27 11:36:20 +010069
Denys Vlasenko4ee8ec12015-03-27 11:36:21 +010070 /* Zero-extending 32-bit regs, do not remove */
71 movl %ebp, %ebp
72 movl %eax, %eax
73
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +020074 movl ASM_THREAD_INFO(TI_sysenter_return, %rsp, 0), %r10d
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +020075
76 /* Construct struct pt_regs on stack */
Ingo Molnar131484c2015-05-28 12:21:47 +020077 pushq $__USER32_DS /* pt_regs->ss */
78 pushq %rbp /* pt_regs->sp */
79 pushfq /* pt_regs->flags */
80 pushq $__USER32_CS /* pt_regs->cs */
Ingo Molnar54ad7262015-06-05 13:02:28 +020081 pushq %r10 /* pt_regs->ip = thread_info->sysenter_return */
Ingo Molnar131484c2015-05-28 12:21:47 +020082 pushq %rax /* pt_regs->orig_ax */
83 pushq %rdi /* pt_regs->di */
84 pushq %rsi /* pt_regs->si */
85 pushq %rdx /* pt_regs->dx */
86 pushq %rcx /* pt_regs->cx */
87 pushq $-ENOSYS /* pt_regs->ax */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 cld
Ingo Molnar54ad7262015-06-05 13:02:28 +020089 sub $(10*8), %rsp /* pt_regs->r8-11, bp, bx, r12-15 not saved */
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +020090
Denys Vlasenkob87cf632015-02-26 14:40:32 -080091 /*
92 * no need to do an access_ok check here because rbp has been
Ingo Molnar54ad7262015-06-05 13:02:28 +020093 * 32-bit zero extended
Denys Vlasenkob87cf632015-02-26 14:40:32 -080094 */
H. Peter Anvin63bcff22012-09-21 12:43:12 -070095 ASM_STAC
Ingo Molnar54ad7262015-06-05 13:02:28 +0200961: movl (%rbp), %ebp
97 _ASM_EXTABLE(1b, ia32_badarg)
H. Peter Anvin63bcff22012-09-21 12:43:12 -070098 ASM_CLAC
Andy Lutomirski8c7aa692014-10-01 11:49:04 -070099
100 /*
101 * Sysenter doesn't filter flags, so we need to clear NT
102 * ourselves. To save a few cycles, we can check whether
103 * NT was set instead of doing an unconditional popfq.
104 */
Ingo Molnar54ad7262015-06-05 13:02:28 +0200105 testl $X86_EFLAGS_NT, EFLAGS(%rsp)
106 jnz sysenter_fix_flags
Andy Lutomirski8c7aa692014-10-01 11:49:04 -0700107sysenter_flags_fixed:
108
Denys Vlasenko9b47feb2015-06-08 22:35:33 +0200109 orl $TS_COMPAT, ASM_THREAD_INFO(TI_status, %rsp, SIZEOF_PTREGS)
110 testl $_TIF_WORK_SYSCALL_ENTRY, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Ingo Molnar54ad7262015-06-05 13:02:28 +0200111 jnz sysenter_tracesys
Ingo Molnar131484c2015-05-28 12:21:47 +0200112
Roland McGrathd4d67152008-07-09 02:38:07 -0700113sysenter_do_call:
Ingo Molnar54ad7262015-06-05 13:02:28 +0200114 /* 32-bit syscall -> 64-bit C ABI argument conversion */
115 movl %edi, %r8d /* arg5 */
116 movl %ebp, %r9d /* arg6 */
117 xchg %ecx, %esi /* rsi:arg2, rcx:arg4 */
118 movl %ebx, %edi /* arg1 */
119 movl %edx, %edx /* arg3 (zero extension) */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700120sysenter_dispatch:
Ingo Molnar54ad7262015-06-05 13:02:28 +0200121 cmpq $(IA32_NR_syscalls-1), %rax
Denys Vlasenko3f5159a2015-04-21 18:03:14 +0200122 ja 1f
Ingo Molnar54ad7262015-06-05 13:02:28 +0200123 call *ia32_sys_call_table(, %rax, 8)
124 movq %rax, RAX(%rsp)
Denys Vlasenko3f5159a2015-04-21 18:03:14 +02001251:
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -0400126 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700127 TRACE_IRQS_OFF
Ingo Molnardca5b522015-03-24 19:44:42 +0100128 testl $_TIF_ALLWORK_MASK, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Roland McGrath5cbf1562008-06-24 01:13:31 -0700129 jnz sysexit_audit
130sysexit_from_sys_call:
Andy Lutomirski4214a162015-04-02 17:12:12 -0700131 /*
132 * NB: SYSEXIT is not obviously safe for 64-bit kernels -- an
133 * NMI between STI and SYSEXIT has poorly specified behavior,
134 * and and NMI followed by an IRQ with usergs is fatal. So
135 * we just pretend we're using SYSEXIT but we really use
136 * SYSRETL instead.
137 *
138 * This code path is still called 'sysexit' because it pairs
139 * with 'sysenter' and it uses the SYSENTER calling convention.
140 */
Denys Vlasenko9b47feb2015-06-08 22:35:33 +0200141 andl $~TS_COMPAT, ASM_THREAD_INFO(TI_status, %rsp, SIZEOF_PTREGS)
Ingo Molnar54ad7262015-06-05 13:02:28 +0200142 movl RIP(%rsp), %ecx /* User %eip */
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800143 RESTORE_RSI_RDI
Ingo Molnar54ad7262015-06-05 13:02:28 +0200144 xorl %edx, %edx /* Do not leak kernel information */
145 xorq %r8, %r8
146 xorq %r9, %r9
147 xorq %r10, %r10
148 movl EFLAGS(%rsp), %r11d /* User eflags */
Ingo Molnar2601e642006-07-03 00:24:45 -0700149 TRACE_IRQS_ON
Andy Lutomirski4214a162015-04-02 17:12:12 -0700150
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800151 /*
Andy Lutomirski4214a162015-04-02 17:12:12 -0700152 * SYSRETL works even on Intel CPUs. Use it in preference to SYSEXIT,
153 * since it avoids a dicey window with interrupts enabled.
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800154 */
Ingo Molnar54ad7262015-06-05 13:02:28 +0200155 movl RSP(%rsp), %esp
Andy Lutomirski4214a162015-04-02 17:12:12 -0700156
157 /*
158 * USERGS_SYSRET32 does:
159 * gsbase = user's gs base
160 * eip = ecx
161 * rflags = r11
162 * cs = __USER32_CS
163 * ss = __USER_DS
164 *
165 * The prologue set RIP(%rsp) to VDSO32_SYSENTER_RETURN, which does:
166 *
167 * pop %ebp
168 * pop %edx
169 * pop %ecx
170 *
171 * Therefore, we invoke SYSRETL with EDX and R8-R10 zeroed to
172 * avoid info leaks. R11 ends up with VDSO32_SYSENTER_RETURN's
173 * address (already known to user code), and R12-R15 are
174 * callee-saved and therefore don't contain any interesting
175 * kernel data.
176 */
177 USERGS_SYSRET32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Roland McGrath5cbf1562008-06-24 01:13:31 -0700179#ifdef CONFIG_AUDITSYSCALL
180 .macro auditsys_entry_common
Ingo Molnar54ad7262015-06-05 13:02:28 +0200181 movl %esi, %r8d /* 5th arg: 4th syscall arg */
182 movl %ecx, %r9d /* swap with edx */
183 movl %edx, %ecx /* 4th arg: 3rd syscall arg */
184 movl %r9d, %edx /* 3rd arg: 2nd syscall arg */
185 movl %ebx, %esi /* 2nd arg: 1st syscall arg */
186 movl %eax, %edi /* 1st arg: syscall number */
187 call __audit_syscall_entry
Denys Vlasenko1536bb42015-06-09 20:54:08 +0200188 /*
189 * We are going to jump back to syscall dispatch.
190 * Prepare syscall args as required by 64-bit C ABI.
191 * Clobbered registers are loaded from pt_regs on stack.
192 */
193 movl ORIG_RAX(%rsp), %eax /* syscall number */
194 movl %ebx, %edi /* arg1 */
195 movl RCX(%rsp), %esi /* arg2 */
196 movl RDX(%rsp), %edx /* arg3 */
197 movl RSI(%rsp), %ecx /* arg4 */
198 movl RDI(%rsp), %r8d /* arg5 */
199 movl %ebp, %r9d /* arg6 */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700200 .endm
201
Jan Beulich81766742009-10-26 15:20:29 +0000202 .macro auditsys_exit exit
Ingo Molnar54ad7262015-06-05 13:02:28 +0200203 testl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT), ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
204 jnz ia32_ret_from_sys_call
Roland McGrath5cbf1562008-06-24 01:13:31 -0700205 TRACE_IRQS_ON
Jan Beulich40a1ef92013-01-30 07:55:53 +0000206 ENABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar54ad7262015-06-05 13:02:28 +0200207 movl %eax, %esi /* second arg, syscall return value */
208 cmpl $-MAX_ERRNO, %eax /* is it an error ? */
209 jbe 1f
210 movslq %eax, %rsi /* if error sign extend to 64 bits */
2111: setbe %al /* 1 if error, 0 if not */
212 movzbl %al, %edi /* zero-extend that into %edi */
213 call __audit_syscall_exit
214 movq RAX(%rsp), %rax /* reload syscall return value */
215 movl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT), %edi
Jan Beulich40a1ef92013-01-30 07:55:53 +0000216 DISABLE_INTERRUPTS(CLBR_NONE)
Roland McGrath5cbf1562008-06-24 01:13:31 -0700217 TRACE_IRQS_OFF
Ingo Molnar54ad7262015-06-05 13:02:28 +0200218 testl %edi, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
219 jz \exit
220 xorl %eax, %eax /* Do not leak kernel information */
Denys Vlasenkoef0cd5d2015-06-02 21:04:01 +0200221 movq %rax, R11(%rsp)
222 movq %rax, R10(%rsp)
223 movq %rax, R9(%rsp)
224 movq %rax, R8(%rsp)
Ingo Molnar54ad7262015-06-05 13:02:28 +0200225 jmp int_with_check
Roland McGrath5cbf1562008-06-24 01:13:31 -0700226 .endm
227
228sysenter_auditsys:
Roland McGrath5cbf1562008-06-24 01:13:31 -0700229 auditsys_entry_common
Ingo Molnar54ad7262015-06-05 13:02:28 +0200230 jmp sysenter_dispatch
Roland McGrath5cbf1562008-06-24 01:13:31 -0700231
232sysexit_audit:
233 auditsys_exit sysexit_from_sys_call
234#endif
235
Andy Lutomirski8c7aa692014-10-01 11:49:04 -0700236sysenter_fix_flags:
Denys Vlasenko9b47feb2015-06-08 22:35:33 +0200237 pushq $(X86_EFLAGS_IF|X86_EFLAGS_FIXED)
Ingo Molnar131484c2015-05-28 12:21:47 +0200238 popfq
Ingo Molnar54ad7262015-06-05 13:02:28 +0200239 jmp sysenter_flags_fixed
Andy Lutomirski8c7aa692014-10-01 11:49:04 -0700240
Roland McGrath5cbf1562008-06-24 01:13:31 -0700241sysenter_tracesys:
Roland McGrath5cbf1562008-06-24 01:13:31 -0700242#ifdef CONFIG_AUDITSYSCALL
Ingo Molnardca5b522015-03-24 19:44:42 +0100243 testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT), ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Roland McGrath5cbf1562008-06-24 01:13:31 -0700244 jz sysenter_auditsys
245#endif
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800246 SAVE_EXTRA_REGS
Ingo Molnar54ad7262015-06-05 13:02:28 +0200247 xorl %eax, %eax /* Do not leak kernel information */
Denys Vlasenkoef0cd5d2015-06-02 21:04:01 +0200248 movq %rax, R11(%rsp)
249 movq %rax, R10(%rsp)
250 movq %rax, R9(%rsp)
251 movq %rax, R8(%rsp)
Ingo Molnar54ad7262015-06-05 13:02:28 +0200252 movq %rsp, %rdi /* &pt_regs -> arg1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 call syscall_trace_enter
Denys Vlasenko73cbf682015-06-02 21:04:02 +0200254
255 /* Reload arg registers from stack. (see sysenter_tracesys) */
256 movl RCX(%rsp), %ecx
257 movl RDX(%rsp), %edx
258 movl RSI(%rsp), %esi
259 movl RDI(%rsp), %edi
Ingo Molnar54ad7262015-06-05 13:02:28 +0200260 movl %eax, %eax /* zero extension */
Denys Vlasenko73cbf682015-06-02 21:04:02 +0200261
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800262 RESTORE_EXTRA_REGS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 jmp sysenter_do_call
Ingo Molnar4c8cd0c2015-06-08 08:33:56 +0200264ENDPROC(entry_SYSENTER_compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266/*
Ingo Molnar54ad7262015-06-05 13:02:28 +0200267 * 32-bit SYSCALL instruction entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 *
Ingo Molnar54ad7262015-06-05 13:02:28 +0200269 * 32-bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11,
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800270 * then loads new ss, cs, and rip from previously programmed MSRs.
271 * rflags gets masked by a value from another MSR (so CLD and CLAC
272 * are not needed). SYSCALL does not save anything on the stack
273 * and does not change rsp.
274 *
275 * Note: rflags saving+masking-with-MSR happens only in Long mode
Ingo Molnar54ad7262015-06-05 13:02:28 +0200276 * (in legacy 32-bit mode, IF, RF and VM bits are cleared and that's it).
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800277 * Don't get confused: rflags saving+masking depends on Long Mode Active bit
278 * (EFER.LMA=1), NOT on bitness of userspace where SYSCALL executes
279 * or target CS descriptor's L bit (SYSCALL does not read segment descriptors).
280 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 * Arguments:
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800282 * eax system call number
283 * ecx return address
284 * ebx arg1
285 * ebp arg2 (note: not saved in the stack frame, should not be touched)
286 * edx arg3
287 * esi arg4
288 * edi arg5
289 * esp user stack
290 * 0(%esp) arg6
291 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 * This is purely a fast path. For anything complicated we use the int 0x80
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800293 * path below. We set up a complete hardware stack frame to share code
294 * with the int 0x80 path.
295 */
Ingo Molnar2cd23552015-06-08 08:28:07 +0200296ENTRY(entry_SYSCALL_compat)
Denys Vlasenkoa232e3d2015-03-27 11:36:20 +0100297 /*
298 * Interrupts are off on entry.
299 * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
300 * it is too small to ever cause noticeable irq latency.
301 */
Jeremy Fitzhardinge457da702008-06-26 07:28:51 -0700302 SWAPGS_UNSAFE_STACK
Ingo Molnar54ad7262015-06-05 13:02:28 +0200303 movl %esp, %r8d
304 movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -0400305 ENABLE_INTERRUPTS(CLBR_NONE)
Denys Vlasenkoa232e3d2015-03-27 11:36:20 +0100306
Denys Vlasenko4ee8ec12015-03-27 11:36:21 +0100307 /* Zero-extending 32-bit regs, do not remove */
Ingo Molnar54ad7262015-06-05 13:02:28 +0200308 movl %eax, %eax
Denys Vlasenko4ee8ec12015-03-27 11:36:21 +0100309
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +0200310 /* Construct struct pt_regs on stack */
Ingo Molnar131484c2015-05-28 12:21:47 +0200311 pushq $__USER32_DS /* pt_regs->ss */
312 pushq %r8 /* pt_regs->sp */
313 pushq %r11 /* pt_regs->flags */
314 pushq $__USER32_CS /* pt_regs->cs */
315 pushq %rcx /* pt_regs->ip */
316 pushq %rax /* pt_regs->orig_ax */
317 pushq %rdi /* pt_regs->di */
318 pushq %rsi /* pt_regs->si */
319 pushq %rdx /* pt_regs->dx */
320 pushq %rbp /* pt_regs->cx */
Ingo Molnar54ad7262015-06-05 13:02:28 +0200321 movl %ebp, %ecx
Ingo Molnar131484c2015-05-28 12:21:47 +0200322 pushq $-ENOSYS /* pt_regs->ax */
Ingo Molnar54ad7262015-06-05 13:02:28 +0200323 sub $(10*8), %rsp /* pt_regs->r8-11, bp, bx, r12-15 not saved */
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +0200324
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800325 /*
Ingo Molnar54ad7262015-06-05 13:02:28 +0200326 * No need to do an access_ok check here because r8 has been
327 * 32-bit zero extended:
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800328 */
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700329 ASM_STAC
Ingo Molnar54ad7262015-06-05 13:02:28 +02003301: movl (%r8), %ebp
331 _ASM_EXTABLE(1b, ia32_badarg)
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700332 ASM_CLAC
Denys Vlasenko9b47feb2015-06-08 22:35:33 +0200333 orl $TS_COMPAT, ASM_THREAD_INFO(TI_status, %rsp, SIZEOF_PTREGS)
334 testl $_TIF_WORK_SYSCALL_ENTRY, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
335 jnz cstar_tracesys
Ingo Molnar131484c2015-05-28 12:21:47 +0200336
Jan Beulich295286a2008-08-29 13:21:11 +0100337cstar_do_call:
Ingo Molnar54ad7262015-06-05 13:02:28 +0200338 /* 32-bit syscall -> 64-bit C ABI argument conversion */
339 movl %edi, %r8d /* arg5 */
340 movl %ebp, %r9d /* arg6 */
341 xchg %ecx, %esi /* rsi:arg2, rcx:arg4 */
342 movl %ebx, %edi /* arg1 */
343 movl %edx, %edx /* arg3 (zero extension) */
344
Roland McGrath5cbf1562008-06-24 01:13:31 -0700345cstar_dispatch:
Ingo Molnar54ad7262015-06-05 13:02:28 +0200346 cmpq $(IA32_NR_syscalls-1), %rax
Denys Vlasenko3f5159a2015-04-21 18:03:14 +0200347 ja 1f
Ingo Molnar54ad7262015-06-05 13:02:28 +0200348
349 call *ia32_sys_call_table(, %rax, 8)
350 movq %rax, RAX(%rsp)
Denys Vlasenko3f5159a2015-04-21 18:03:14 +02003511:
Denys Vlasenkoaee4b012015-06-09 20:54:07 +0200352 movl RCX(%rsp), %ebp
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -0400353 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700354 TRACE_IRQS_OFF
Ingo Molnar54ad7262015-06-05 13:02:28 +0200355 testl $_TIF_ALLWORK_MASK, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
356 jnz sysretl_audit
357
Roland McGrath5cbf1562008-06-24 01:13:31 -0700358sysretl_from_sys_call:
Ingo Molnar54ad7262015-06-05 13:02:28 +0200359 andl $~TS_COMPAT, ASM_THREAD_INFO(TI_status, %rsp, SIZEOF_PTREGS)
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800360 RESTORE_RSI_RDI_RDX
Ingo Molnar54ad7262015-06-05 13:02:28 +0200361 movl RIP(%rsp), %ecx
362 movl EFLAGS(%rsp), %r11d
363 xorq %r10, %r10
364 xorq %r9, %r9
365 xorq %r8, %r8
Ingo Molnar2601e642006-07-03 00:24:45 -0700366 TRACE_IRQS_ON
Ingo Molnar54ad7262015-06-05 13:02:28 +0200367 movl RSP(%rsp), %esp
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800368 /*
Ingo Molnar54ad7262015-06-05 13:02:28 +0200369 * 64-bit->32-bit SYSRET restores eip from ecx,
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800370 * eflags from r11 (but RF and VM bits are forced to 0),
371 * cs and ss are loaded from MSRs.
Ingo Molnar54ad7262015-06-05 13:02:28 +0200372 * (Note: 32-bit->32-bit SYSRET is different: since r11
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800373 * does not exist, it merely sets eflags.IF=1).
Andy Lutomirski61f01dd2015-04-26 16:47:59 -0700374 *
375 * NB: On AMD CPUs with the X86_BUG_SYSRET_SS_ATTRS bug, the ss
376 * descriptor is not reinitialized. This means that we must
377 * avoid SYSRET with SS == NULL, which could happen if we schedule,
378 * exit the kernel, and re-enter using an interrupt vector. (All
379 * interrupt entries on x86_64 set SS to NULL.) We prevent that
380 * from happening by reloading SS in __switch_to.
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800381 */
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -0400382 USERGS_SYSRET32
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800383
Roland McGrath5cbf1562008-06-24 01:13:31 -0700384#ifdef CONFIG_AUDITSYSCALL
385cstar_auditsys:
Roland McGrath5cbf1562008-06-24 01:13:31 -0700386 auditsys_entry_common
Ingo Molnar54ad7262015-06-05 13:02:28 +0200387 jmp cstar_dispatch
Roland McGrath5cbf1562008-06-24 01:13:31 -0700388
389sysretl_audit:
Jan Beulich81766742009-10-26 15:20:29 +0000390 auditsys_exit sysretl_from_sys_call
Roland McGrath5cbf1562008-06-24 01:13:31 -0700391#endif
392
393cstar_tracesys:
394#ifdef CONFIG_AUDITSYSCALL
Ingo Molnar54ad7262015-06-05 13:02:28 +0200395 testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT), ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
396 jz cstar_auditsys
Roland McGrath5cbf1562008-06-24 01:13:31 -0700397#endif
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800398 SAVE_EXTRA_REGS
Ingo Molnar54ad7262015-06-05 13:02:28 +0200399 xorl %eax, %eax /* Do not leak kernel information */
Denys Vlasenkoef0cd5d2015-06-02 21:04:01 +0200400 movq %rax, R11(%rsp)
401 movq %rax, R10(%rsp)
Denys Vlasenko53e9acc2015-06-03 14:56:09 +0200402 movq %rax, R9(%rsp)
Denys Vlasenkoef0cd5d2015-06-02 21:04:01 +0200403 movq %rax, R8(%rsp)
Ingo Molnar54ad7262015-06-05 13:02:28 +0200404 movq %rsp, %rdi /* &pt_regs -> arg1 */
Denys Vlasenko53e9acc2015-06-03 14:56:09 +0200405 call syscall_trace_enter
Denys Vlasenko73cbf682015-06-02 21:04:02 +0200406
407 /* Reload arg registers from stack. (see sysenter_tracesys) */
408 movl RCX(%rsp), %ecx
409 movl RDX(%rsp), %edx
410 movl RSI(%rsp), %esi
411 movl RDI(%rsp), %edi
Ingo Molnar54ad7262015-06-05 13:02:28 +0200412 movl %eax, %eax /* zero extension */
Denys Vlasenko73cbf682015-06-02 21:04:02 +0200413
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800414 RESTORE_EXTRA_REGS
Denys Vlasenko53e9acc2015-06-03 14:56:09 +0200415 jmp cstar_do_call
Ingo Molnar2cd23552015-06-08 08:28:07 +0200416END(entry_SYSCALL_compat)
Ingo Molnar54ad7262015-06-05 13:02:28 +0200417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418ia32_badarg:
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700419 ASM_CLAC
Denys Vlasenkoeb478542015-06-07 20:24:30 +0200420 movq $-EFAULT, RAX(%rsp)
Denys Vlasenko61b1e3e2015-06-02 19:35:10 +0200421ia32_ret_from_sys_call:
Ingo Molnar54ad7262015-06-05 13:02:28 +0200422 xorl %eax, %eax /* Do not leak kernel information */
Denys Vlasenkoef0cd5d2015-06-02 21:04:01 +0200423 movq %rax, R11(%rsp)
424 movq %rax, R10(%rsp)
425 movq %rax, R9(%rsp)
426 movq %rax, R8(%rsp)
Ingo Molnar54ad7262015-06-05 13:02:28 +0200427 jmp int_ret_from_sys_call
Denys Vlasenko61b1e3e2015-06-02 19:35:10 +0200428
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800429/*
430 * Emulated IA32 system calls via int 0x80.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 *
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800432 * Arguments:
433 * eax system call number
434 * ebx arg1
435 * ecx arg2
436 * edx arg3
437 * esi arg4
438 * edi arg5
439 * ebp arg6 (note: not saved in the stack frame, should not be touched)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 *
441 * Notes:
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800442 * Uses the same stack frame as the x86-64 version.
443 * All registers except eax must be saved (but ptrace may violate that).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 * Arguments are zero extended. For system calls that want sign extension and
445 * take long arguments a wrapper is needed. Most calls can just be called
446 * directly.
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800447 * Assumes it is only called from user space and entered with interrupts off.
448 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Ingo Molnar2cd23552015-06-08 08:28:07 +0200450ENTRY(entry_INT80_compat)
Denys Vlasenkoa232e3d2015-03-27 11:36:20 +0100451 /*
452 * Interrupts are off on entry.
453 * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
454 * it is too small to ever cause noticeable irq latency.
455 */
Jeremy Fitzhardinge360c0442008-07-08 15:06:28 -0700456 PARAVIRT_ADJUST_EXCEPTION_FRAME
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -0400457 SWAPGS
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -0400458 ENABLE_INTERRUPTS(CLBR_NONE)
Denys Vlasenkoa232e3d2015-03-27 11:36:20 +0100459
Denys Vlasenko4ee8ec12015-03-27 11:36:21 +0100460 /* Zero-extending 32-bit regs, do not remove */
Ingo Molnar54ad7262015-06-05 13:02:28 +0200461 movl %eax, %eax
Denys Vlasenko4ee8ec12015-03-27 11:36:21 +0100462
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +0200463 /* Construct struct pt_regs on stack (iret frame is already on stack) */
Ingo Molnar131484c2015-05-28 12:21:47 +0200464 pushq %rax /* pt_regs->orig_ax */
465 pushq %rdi /* pt_regs->di */
466 pushq %rsi /* pt_regs->si */
467 pushq %rdx /* pt_regs->dx */
468 pushq %rcx /* pt_regs->cx */
469 pushq $-ENOSYS /* pt_regs->ax */
Denys Vlasenko61b1e3e2015-06-02 19:35:10 +0200470 pushq $0 /* pt_regs->r8 */
471 pushq $0 /* pt_regs->r9 */
472 pushq $0 /* pt_regs->r10 */
473 pushq $0 /* pt_regs->r11 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 cld
Ingo Molnar54ad7262015-06-05 13:02:28 +0200475 sub $(6*8), %rsp /* pt_regs->bp, bx, r12-15 not saved */
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +0200476
Ingo Molnar54ad7262015-06-05 13:02:28 +0200477 orl $TS_COMPAT, ASM_THREAD_INFO(TI_status, %rsp, SIZEOF_PTREGS)
478 testl $_TIF_WORK_SYSCALL_ENTRY, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
479 jnz ia32_tracesys
480
Roland McGrathc09249f2009-02-06 18:15:18 -0800481ia32_do_call:
Ingo Molnar54ad7262015-06-05 13:02:28 +0200482 /* 32-bit syscall -> 64-bit C ABI argument conversion */
483 movl %edi, %r8d /* arg5 */
484 movl %ebp, %r9d /* arg6 */
485 xchg %ecx, %esi /* rsi:arg2, rcx:arg4 */
486 movl %ebx, %edi /* arg1 */
487 movl %edx, %edx /* arg3 (zero extension) */
488 cmpq $(IA32_NR_syscalls-1), %rax
Denys Vlasenko3f5159a2015-04-21 18:03:14 +0200489 ja 1f
Ingo Molnar54ad7262015-06-05 13:02:28 +0200490
Denys Vlasenkoeb478542015-06-07 20:24:30 +0200491 call *ia32_sys_call_table(, %rax, 8)
Ingo Molnar54ad7262015-06-05 13:02:28 +0200492 movq %rax, RAX(%rsp)
Denys Vlasenko3f5159a2015-04-21 18:03:14 +02004931:
Ingo Molnar54ad7262015-06-05 13:02:28 +0200494 jmp int_ret_from_sys_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800496ia32_tracesys:
497 SAVE_EXTRA_REGS
Ingo Molnar54ad7262015-06-05 13:02:28 +0200498 movq %rsp, %rdi /* &pt_regs -> arg1 */
499 call syscall_trace_enter
Denys Vlasenko73cbf682015-06-02 21:04:02 +0200500 /*
501 * Reload arg registers from stack in case ptrace changed them.
502 * Don't reload %eax because syscall_trace_enter() returned
503 * the %rax value we should see. But do truncate it to 32 bits.
504 * If it's -1 to make us punt the syscall, then (u32)-1 is still
505 * an appropriately invalid value.
506 */
507 movl RCX(%rsp), %ecx
508 movl RDX(%rsp), %edx
509 movl RSI(%rsp), %esi
510 movl RDI(%rsp), %edi
Ingo Molnar54ad7262015-06-05 13:02:28 +0200511 movl %eax, %eax /* zero extension */
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800512 RESTORE_EXTRA_REGS
Ingo Molnar54ad7262015-06-05 13:02:28 +0200513 jmp ia32_do_call
Ingo Molnar2cd23552015-06-08 08:28:07 +0200514END(entry_INT80_compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Ramkumar Ramachandrad2475b82013-07-10 23:34:28 +0530516 .macro PTREGSCALL label, func
Jan Beulichf6b2bc82011-11-29 11:24:10 +0000517 ALIGN
518GLOBAL(\label)
Ingo Molnar54ad7262015-06-05 13:02:28 +0200519 leaq \func(%rip), %rax
520 jmp ia32_ptregs_common
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 .endm
522
Ingo Molnar54ad7262015-06-05 13:02:28 +0200523 PTREGSCALL stub32_rt_sigreturn, sys32_rt_sigreturn
524 PTREGSCALL stub32_sigreturn, sys32_sigreturn
525 PTREGSCALL stub32_fork, sys_fork
526 PTREGSCALL stub32_vfork, sys_vfork
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Jan Beulichf6b2bc82011-11-29 11:24:10 +0000528 ALIGN
Al Viro1d4b4b22012-10-22 22:34:11 -0400529GLOBAL(stub32_clone)
Ingo Molnar54ad7262015-06-05 13:02:28 +0200530 leaq sys_clone(%rip), %rax
Denys Vlasenko5cdc6832015-06-03 15:58:49 +0200531 /*
Denys Vlasenko7a5a9822015-06-03 15:58:50 +0200532 * The 32-bit clone ABI is: clone(..., int tls_val, int *child_tidptr).
533 * The 64-bit clone ABI is: clone(..., int *child_tidptr, int tls_val).
534 *
535 * The native 64-bit kernel's sys_clone() implements the latter,
536 * so we need to swap arguments here before calling it:
Denys Vlasenko5cdc6832015-06-03 15:58:49 +0200537 */
Denys Vlasenko7a5a9822015-06-03 15:58:50 +0200538 xchg %r8, %rcx
Ingo Molnar54ad7262015-06-05 13:02:28 +0200539 jmp ia32_ptregs_common
Al Viro1d4b4b22012-10-22 22:34:11 -0400540
541 ALIGN
Jan Beulichf6b2bc82011-11-29 11:24:10 +0000542ia32_ptregs_common:
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800543 SAVE_EXTRA_REGS 8
Ingo Molnar54ad7262015-06-05 13:02:28 +0200544 call *%rax
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800545 RESTORE_EXTRA_REGS 8
546 ret
Jan Beulich4b787e02006-06-26 13:56:55 +0200547END(ia32_ptregs_common)