blob: 8a45d2cd2beffe57ae7b0617a7a2d32a052ca4a8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Compatibility mode system call entry point for x86-64.
3 *
4 * Copyright 2000-2002 Andi Kleen, SuSE Labs.
5 */
6
Ingo Molnard36f9472015-06-03 18:29:26 +02007#include "calling.h"
Sam Ravnborge2d5df92005-09-09 21:28:48 +02008#include <asm/asm-offsets.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#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>
Ingo Molnar2601e642006-07-03 00:24:45 -070014#include <asm/irqflags.h>
H. Peter Anvin1ce6f862012-04-20 12:19:50 -070015#include <asm/asm.h>
H. Peter Anvin63bcff22012-09-21 12:43:12 -070016#include <asm/smap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/linkage.h>
Eric Parisd7e75282012-01-03 14:23:06 -050018#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Roland McGrath5cbf1562008-06-24 01:13:31 -070020/* 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
24
25#ifndef CONFIG_AUDITSYSCALL
Jan Beulich24e35802009-09-30 11:22:11 +010026#define sysexit_audit ia32_ret_from_sys_call
27#define sysretl_audit ia32_ret_from_sys_call
Roland McGrath5cbf1562008-06-24 01:13:31 -070028#endif
29
Jiri Olsaea714542011-03-07 19:10:39 +010030 .section .entry.text, "ax"
31
Roland McGrathd4d67152008-07-09 02:38:07 -070032 /*
33 * Reload arg registers from stack in case ptrace changed them.
34 * We don't reload %eax because syscall_trace_enter() returned
Roland McGratheefdca02010-09-14 12:22:58 -070035 * the %rax value we should see. Instead, we just truncate that
36 * value to 32 bits again as we did on entry from user mode.
37 * If it's a new value set by user_regset during entry tracing,
38 * this matches the normal truncation of the user-mode value.
39 * If it's -1 to make us punt the syscall, then (u32)-1 is still
40 * an appropriately invalid value.
Roland McGrathd4d67152008-07-09 02:38:07 -070041 */
Denys Vlasenkof2db9382015-02-26 14:40:30 -080042 .macro LOAD_ARGS32 _r9=0
Jan Beulich295286a2008-08-29 13:21:11 +010043 .if \_r9
Denys Vlasenkof2db9382015-02-26 14:40:30 -080044 movl R9(%rsp),%r9d
Jan Beulich295286a2008-08-29 13:21:11 +010045 .endif
Denys Vlasenkof2db9382015-02-26 14:40:30 -080046 movl RCX(%rsp),%ecx
47 movl RDX(%rsp),%edx
48 movl RSI(%rsp),%esi
49 movl RDI(%rsp),%edi
Roland McGratheefdca02010-09-14 12:22:58 -070050 movl %eax,%eax /* zero extension */
Andi Kleen176df242007-09-21 16:16:18 +020051 .endm
52
Jan Beulich27651302006-01-11 22:41:59 +010053
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -040054#ifdef CONFIG_PARAVIRT
55ENTRY(native_usergs_sysret32)
56 swapgs
57 sysretl
58ENDPROC(native_usergs_sysret32)
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -040059#endif
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/*
62 * 32bit SYSENTER instruction entry.
63 *
Denys Vlasenkob87cf632015-02-26 14:40:32 -080064 * SYSENTER loads ss, rsp, cs, and rip from previously programmed MSRs.
65 * IF and VM in rflags are cleared (IOW: interrupts are off).
66 * SYSENTER does not save anything on the stack,
67 * and does not save old rip (!!!) and rflags.
68 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 * Arguments:
Denys Vlasenkob87cf632015-02-26 14:40:32 -080070 * eax system call number
71 * ebx arg1
72 * ecx arg2
73 * edx arg3
74 * esi arg4
75 * edi arg5
76 * ebp user stack
77 * 0(%ebp) arg6
78 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 * This is purely a fast path. For anything complicated we use the int 0x80
Denys Vlasenkob87cf632015-02-26 14:40:32 -080080 * path below. We set up a complete hardware stack frame to share code
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 * with the int 0x80 path.
Denys Vlasenkob87cf632015-02-26 14:40:32 -080082 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070083ENTRY(ia32_sysenter_target)
Denys Vlasenkoa232e3d2015-03-27 11:36:20 +010084 /*
85 * Interrupts are off on entry.
86 * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
87 * it is too small to ever cause noticeable irq latency.
88 */
Jeremy Fitzhardinge457da702008-06-26 07:28:51 -070089 SWAPGS_UNSAFE_STACK
Denys Vlasenko3a232082015-04-24 17:31:35 +020090 movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -040091 ENABLE_INTERRUPTS(CLBR_NONE)
Denys Vlasenkoa232e3d2015-03-27 11:36:20 +010092
Denys Vlasenko4ee8ec12015-03-27 11:36:21 +010093 /* Zero-extending 32-bit regs, do not remove */
94 movl %ebp, %ebp
95 movl %eax, %eax
96
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +020097 movl ASM_THREAD_INFO(TI_sysenter_return, %rsp, 0), %r10d
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +020098
99 /* Construct struct pt_regs on stack */
Ingo Molnar131484c2015-05-28 12:21:47 +0200100 pushq $__USER32_DS /* pt_regs->ss */
101 pushq %rbp /* pt_regs->sp */
102 pushfq /* pt_regs->flags */
103 pushq $__USER32_CS /* pt_regs->cs */
104 pushq %r10 /* pt_regs->ip = thread_info->sysenter_return */
105 pushq %rax /* pt_regs->orig_ax */
106 pushq %rdi /* pt_regs->di */
107 pushq %rsi /* pt_regs->si */
108 pushq %rdx /* pt_regs->dx */
109 pushq %rcx /* pt_regs->cx */
110 pushq $-ENOSYS /* pt_regs->ax */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 cld
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +0200112 sub $(10*8),%rsp /* pt_regs->r8-11,bp,bx,r12-15 not saved */
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +0200113
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800114 /*
115 * no need to do an access_ok check here because rbp has been
116 * 32bit zero extended
117 */
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700118 ASM_STAC
Jan Beulich295286a2008-08-29 13:21:11 +01001191: movl (%rbp),%ebp
H. Peter Anvin1ce6f862012-04-20 12:19:50 -0700120 _ASM_EXTABLE(1b,ia32_badarg)
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700121 ASM_CLAC
Andy Lutomirski8c7aa692014-10-01 11:49:04 -0700122
123 /*
124 * Sysenter doesn't filter flags, so we need to clear NT
125 * ourselves. To save a few cycles, we can check whether
126 * NT was set instead of doing an unconditional popfq.
127 */
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800128 testl $X86_EFLAGS_NT,EFLAGS(%rsp)
Andy Lutomirski8c7aa692014-10-01 11:49:04 -0700129 jnz sysenter_fix_flags
130sysenter_flags_fixed:
131
Ingo Molnardca5b522015-03-24 19:44:42 +0100132 orl $TS_COMPAT, ASM_THREAD_INFO(TI_status, %rsp, SIZEOF_PTREGS)
133 testl $_TIF_WORK_SYSCALL_ENTRY, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 jnz sysenter_tracesys
Ingo Molnar131484c2015-05-28 12:21:47 +0200135
Roland McGrathd4d67152008-07-09 02:38:07 -0700136sysenter_do_call:
Denys Vlasenko14f6e952015-02-26 14:40:35 -0800137 /* 32bit syscall -> 64bit C ABI argument conversion */
138 movl %edi,%r8d /* arg5 */
139 movl %ebp,%r9d /* arg6 */
140 xchg %ecx,%esi /* rsi:arg2, rcx:arg4 */
141 movl %ebx,%edi /* arg1 */
142 movl %edx,%edx /* arg3 (zero extension) */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700143sysenter_dispatch:
Denys Vlasenko3f5159a2015-04-21 18:03:14 +0200144 cmpq $(IA32_NR_syscalls-1),%rax
145 ja 1f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 call *ia32_sys_call_table(,%rax,8)
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800147 movq %rax,RAX(%rsp)
Denys Vlasenko3f5159a2015-04-21 18:03:14 +02001481:
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -0400149 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700150 TRACE_IRQS_OFF
Ingo Molnardca5b522015-03-24 19:44:42 +0100151 testl $_TIF_ALLWORK_MASK, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Roland McGrath5cbf1562008-06-24 01:13:31 -0700152 jnz sysexit_audit
153sysexit_from_sys_call:
Andy Lutomirski4214a162015-04-02 17:12:12 -0700154 /*
155 * NB: SYSEXIT is not obviously safe for 64-bit kernels -- an
156 * NMI between STI and SYSEXIT has poorly specified behavior,
157 * and and NMI followed by an IRQ with usergs is fatal. So
158 * we just pretend we're using SYSEXIT but we really use
159 * SYSRETL instead.
160 *
161 * This code path is still called 'sysexit' because it pairs
162 * with 'sysenter' and it uses the SYSENTER calling convention.
163 */
Ingo Molnardca5b522015-03-24 19:44:42 +0100164 andl $~TS_COMPAT,ASM_THREAD_INFO(TI_status, %rsp, SIZEOF_PTREGS)
Andy Lutomirski4214a162015-04-02 17:12:12 -0700165 movl RIP(%rsp),%ecx /* User %eip */
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800166 RESTORE_RSI_RDI
Andy Lutomirski4214a162015-04-02 17:12:12 -0700167 xorl %edx,%edx /* avoid info leaks */
Jan Beulich24e35802009-09-30 11:22:11 +0100168 xorq %r8,%r8
169 xorq %r9,%r9
170 xorq %r10,%r10
Andy Lutomirski4214a162015-04-02 17:12:12 -0700171 movl EFLAGS(%rsp),%r11d /* User eflags */
Ingo Molnar2601e642006-07-03 00:24:45 -0700172 TRACE_IRQS_ON
Andy Lutomirski4214a162015-04-02 17:12:12 -0700173
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800174 /*
Andy Lutomirski4214a162015-04-02 17:12:12 -0700175 * SYSRETL works even on Intel CPUs. Use it in preference to SYSEXIT,
176 * since it avoids a dicey window with interrupts enabled.
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800177 */
Andy Lutomirski4214a162015-04-02 17:12:12 -0700178 movl RSP(%rsp),%esp
179
180 /*
181 * USERGS_SYSRET32 does:
182 * gsbase = user's gs base
183 * eip = ecx
184 * rflags = r11
185 * cs = __USER32_CS
186 * ss = __USER_DS
187 *
188 * The prologue set RIP(%rsp) to VDSO32_SYSENTER_RETURN, which does:
189 *
190 * pop %ebp
191 * pop %edx
192 * pop %ecx
193 *
194 * Therefore, we invoke SYSRETL with EDX and R8-R10 zeroed to
195 * avoid info leaks. R11 ends up with VDSO32_SYSENTER_RETURN's
196 * address (already known to user code), and R12-R15 are
197 * callee-saved and therefore don't contain any interesting
198 * kernel data.
199 */
200 USERGS_SYSRET32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Roland McGrath5cbf1562008-06-24 01:13:31 -0700202#ifdef CONFIG_AUDITSYSCALL
203 .macro auditsys_entry_common
Richard Guy Briggsb4f0d372014-03-04 10:38:06 -0500204 movl %esi,%r8d /* 5th arg: 4th syscall arg */
205 movl %ecx,%r9d /*swap with edx*/
206 movl %edx,%ecx /* 4th arg: 3rd syscall arg */
207 movl %r9d,%edx /* 3rd arg: 2nd syscall arg */
208 movl %ebx,%esi /* 2nd arg: 1st syscall arg */
209 movl %eax,%edi /* 1st arg: syscall number */
Eric Parisb05d8442012-01-03 14:23:06 -0500210 call __audit_syscall_entry
Denys Vlasenko3f5159a2015-04-21 18:03:14 +0200211 movl ORIG_RAX(%rsp),%eax /* reload syscall number */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700212 movl %ebx,%edi /* reload 1st syscall arg */
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800213 movl RCX(%rsp),%esi /* reload 2nd syscall arg */
214 movl RDX(%rsp),%edx /* reload 3rd syscall arg */
215 movl RSI(%rsp),%ecx /* reload 4th syscall arg */
216 movl RDI(%rsp),%r8d /* reload 5th syscall arg */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700217 .endm
218
Jan Beulich81766742009-10-26 15:20:29 +0000219 .macro auditsys_exit exit
Ingo Molnardca5b522015-03-24 19:44:42 +0100220 testl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT), ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Jan Beulich24e35802009-09-30 11:22:11 +0100221 jnz ia32_ret_from_sys_call
Roland McGrath5cbf1562008-06-24 01:13:31 -0700222 TRACE_IRQS_ON
Jan Beulich40a1ef92013-01-30 07:55:53 +0000223 ENABLE_INTERRUPTS(CLBR_NONE)
Roland McGrath5cbf1562008-06-24 01:13:31 -0700224 movl %eax,%esi /* second arg, syscall return value */
Eric Parisd7e75282012-01-03 14:23:06 -0500225 cmpl $-MAX_ERRNO,%eax /* is it an error ? */
Eric Parisf031cd22012-01-03 14:23:06 -0500226 jbe 1f
227 movslq %eax, %rsi /* if error sign extend to 64 bits */
2281: setbe %al /* 1 if error, 0 if not */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700229 movzbl %al,%edi /* zero-extend that into %edi */
Eric Parisd7e75282012-01-03 14:23:06 -0500230 call __audit_syscall_exit
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800231 movq RAX(%rsp),%rax /* reload syscall return value */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700232 movl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT),%edi
Jan Beulich40a1ef92013-01-30 07:55:53 +0000233 DISABLE_INTERRUPTS(CLBR_NONE)
Roland McGrath5cbf1562008-06-24 01:13:31 -0700234 TRACE_IRQS_OFF
Ingo Molnardca5b522015-03-24 19:44:42 +0100235 testl %edi, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Jan Beulich24e35802009-09-30 11:22:11 +0100236 jz \exit
Denys Vlasenkoef0cd5d2015-06-02 21:04:01 +0200237 xorl %eax, %eax /* do not leak kernel information */
238 movq %rax, R11(%rsp)
239 movq %rax, R10(%rsp)
240 movq %rax, R9(%rsp)
241 movq %rax, R8(%rsp)
Jan Beulich24e35802009-09-30 11:22:11 +0100242 jmp int_with_check
Roland McGrath5cbf1562008-06-24 01:13:31 -0700243 .endm
244
245sysenter_auditsys:
Roland McGrath5cbf1562008-06-24 01:13:31 -0700246 auditsys_entry_common
247 movl %ebp,%r9d /* reload 6th syscall arg */
248 jmp sysenter_dispatch
249
250sysexit_audit:
251 auditsys_exit sysexit_from_sys_call
252#endif
253
Andy Lutomirski8c7aa692014-10-01 11:49:04 -0700254sysenter_fix_flags:
Ingo Molnar131484c2015-05-28 12:21:47 +0200255 pushq $(X86_EFLAGS_IF|X86_EFLAGS_FIXED)
256 popfq
Andy Lutomirski8c7aa692014-10-01 11:49:04 -0700257 jmp sysenter_flags_fixed
258
Roland McGrath5cbf1562008-06-24 01:13:31 -0700259sysenter_tracesys:
Roland McGrath5cbf1562008-06-24 01:13:31 -0700260#ifdef CONFIG_AUDITSYSCALL
Ingo Molnardca5b522015-03-24 19:44:42 +0100261 testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT), ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Roland McGrath5cbf1562008-06-24 01:13:31 -0700262 jz sysenter_auditsys
263#endif
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800264 SAVE_EXTRA_REGS
Denys Vlasenkoef0cd5d2015-06-02 21:04:01 +0200265 xorl %eax, %eax /* do not leak kernel information */
266 movq %rax, R11(%rsp)
267 movq %rax, R10(%rsp)
268 movq %rax, R9(%rsp)
269 movq %rax, R8(%rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 movq %rsp,%rdi /* &pt_regs -> arg1 */
271 call syscall_trace_enter
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800272 LOAD_ARGS32 /* reload args from stack in case ptrace changed it */
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800273 RESTORE_EXTRA_REGS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 jmp sysenter_do_call
Jan Beulich4b787e02006-06-26 13:56:55 +0200275ENDPROC(ia32_sysenter_target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277/*
278 * 32bit SYSCALL instruction entry.
279 *
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800280 * 32bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11,
281 * then loads new ss, cs, and rip from previously programmed MSRs.
282 * rflags gets masked by a value from another MSR (so CLD and CLAC
283 * are not needed). SYSCALL does not save anything on the stack
284 * and does not change rsp.
285 *
286 * Note: rflags saving+masking-with-MSR happens only in Long mode
287 * (in legacy 32bit mode, IF, RF and VM bits are cleared and that's it).
288 * Don't get confused: rflags saving+masking depends on Long Mode Active bit
289 * (EFER.LMA=1), NOT on bitness of userspace where SYSCALL executes
290 * or target CS descriptor's L bit (SYSCALL does not read segment descriptors).
291 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 * Arguments:
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800293 * eax system call number
294 * ecx return address
295 * ebx arg1
296 * ebp arg2 (note: not saved in the stack frame, should not be touched)
297 * edx arg3
298 * esi arg4
299 * edi arg5
300 * esp user stack
301 * 0(%esp) arg6
302 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 * This is purely a fast path. For anything complicated we use the int 0x80
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800304 * path below. We set up a complete hardware stack frame to share code
305 * with the int 0x80 path.
306 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307ENTRY(ia32_cstar_target)
Denys Vlasenkoa232e3d2015-03-27 11:36:20 +0100308 /*
309 * Interrupts are off on entry.
310 * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
311 * it is too small to ever cause noticeable irq latency.
312 */
Jeremy Fitzhardinge457da702008-06-26 07:28:51 -0700313 SWAPGS_UNSAFE_STACK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 movl %esp,%r8d
Denys Vlasenko3a232082015-04-24 17:31:35 +0200315 movq PER_CPU_VAR(cpu_current_top_of_stack),%rsp
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -0400316 ENABLE_INTERRUPTS(CLBR_NONE)
Denys Vlasenkoa232e3d2015-03-27 11:36:20 +0100317
Denys Vlasenko4ee8ec12015-03-27 11:36:21 +0100318 /* Zero-extending 32-bit regs, do not remove */
319 movl %eax,%eax
320
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +0200321 /* Construct struct pt_regs on stack */
Ingo Molnar131484c2015-05-28 12:21:47 +0200322 pushq $__USER32_DS /* pt_regs->ss */
323 pushq %r8 /* pt_regs->sp */
324 pushq %r11 /* pt_regs->flags */
325 pushq $__USER32_CS /* pt_regs->cs */
326 pushq %rcx /* pt_regs->ip */
327 pushq %rax /* pt_regs->orig_ax */
328 pushq %rdi /* pt_regs->di */
329 pushq %rsi /* pt_regs->si */
330 pushq %rdx /* pt_regs->dx */
331 pushq %rbp /* pt_regs->cx */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 movl %ebp,%ecx
Ingo Molnar131484c2015-05-28 12:21:47 +0200333 pushq $-ENOSYS /* pt_regs->ax */
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +0200334 sub $(10*8),%rsp /* pt_regs->r8-11,bp,bx,r12-15 not saved */
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +0200335
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800336 /*
337 * no need to do an access_ok check here because r8 has been
338 * 32bit zero extended
339 */
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700340 ASM_STAC
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411: movl (%r8),%r9d
H. Peter Anvina3e859f2012-04-20 16:51:50 -0700342 _ASM_EXTABLE(1b,ia32_badarg)
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700343 ASM_CLAC
Ingo Molnardca5b522015-03-24 19:44:42 +0100344 orl $TS_COMPAT, ASM_THREAD_INFO(TI_status, %rsp, SIZEOF_PTREGS)
345 testl $_TIF_WORK_SYSCALL_ENTRY, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 jnz cstar_tracesys
Ingo Molnar131484c2015-05-28 12:21:47 +0200347
Jan Beulich295286a2008-08-29 13:21:11 +0100348cstar_do_call:
Denys Vlasenko14f6e952015-02-26 14:40:35 -0800349 /* 32bit syscall -> 64bit C ABI argument conversion */
350 movl %edi,%r8d /* arg5 */
351 /* r9 already loaded */ /* arg6 */
352 xchg %ecx,%esi /* rsi:arg2, rcx:arg4 */
353 movl %ebx,%edi /* arg1 */
354 movl %edx,%edx /* arg3 (zero extension) */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700355cstar_dispatch:
Denys Vlasenko3f5159a2015-04-21 18:03:14 +0200356 cmpq $(IA32_NR_syscalls-1),%rax
357 ja 1f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 call *ia32_sys_call_table(,%rax,8)
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800359 movq %rax,RAX(%rsp)
Denys Vlasenko3f5159a2015-04-21 18:03:14 +02003601:
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -0400361 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700362 TRACE_IRQS_OFF
Ingo Molnardca5b522015-03-24 19:44:42 +0100363 testl $_TIF_ALLWORK_MASK, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Roland McGrath5cbf1562008-06-24 01:13:31 -0700364 jnz sysretl_audit
365sysretl_from_sys_call:
Ingo Molnardca5b522015-03-24 19:44:42 +0100366 andl $~TS_COMPAT, ASM_THREAD_INFO(TI_status, %rsp, SIZEOF_PTREGS)
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800367 RESTORE_RSI_RDI_RDX
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800368 movl RIP(%rsp),%ecx
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800369 movl EFLAGS(%rsp),%r11d
Jan Beulich24e35802009-09-30 11:22:11 +0100370 xorq %r10,%r10
371 xorq %r9,%r9
372 xorq %r8,%r8
Ingo Molnar2601e642006-07-03 00:24:45 -0700373 TRACE_IRQS_ON
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800374 movl RSP(%rsp),%esp
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800375 /*
376 * 64bit->32bit SYSRET restores eip from ecx,
377 * eflags from r11 (but RF and VM bits are forced to 0),
378 * cs and ss are loaded from MSRs.
379 * (Note: 32bit->32bit SYSRET is different: since r11
380 * does not exist, it merely sets eflags.IF=1).
Andy Lutomirski61f01dd2015-04-26 16:47:59 -0700381 *
382 * NB: On AMD CPUs with the X86_BUG_SYSRET_SS_ATTRS bug, the ss
383 * descriptor is not reinitialized. This means that we must
384 * avoid SYSRET with SS == NULL, which could happen if we schedule,
385 * exit the kernel, and re-enter using an interrupt vector. (All
386 * interrupt entries on x86_64 set SS to NULL.) We prevent that
387 * from happening by reloading SS in __switch_to.
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800388 */
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -0400389 USERGS_SYSRET32
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800390
Roland McGrath5cbf1562008-06-24 01:13:31 -0700391#ifdef CONFIG_AUDITSYSCALL
392cstar_auditsys:
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800393 movl %r9d,R9(%rsp) /* register to be clobbered by call */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700394 auditsys_entry_common
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800395 movl R9(%rsp),%r9d /* reload 6th syscall arg */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700396 jmp cstar_dispatch
397
398sysretl_audit:
Jan Beulich81766742009-10-26 15:20:29 +0000399 auditsys_exit sysretl_from_sys_call
Roland McGrath5cbf1562008-06-24 01:13:31 -0700400#endif
401
402cstar_tracesys:
403#ifdef CONFIG_AUDITSYSCALL
Ingo Molnardca5b522015-03-24 19:44:42 +0100404 testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT), ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Roland McGrath5cbf1562008-06-24 01:13:31 -0700405 jz cstar_auditsys
406#endif
Chuck Ebbertecd744e2007-11-07 10:48:39 -0500407 xchgl %r9d,%ebp
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800408 SAVE_EXTRA_REGS
Denys Vlasenkoef0cd5d2015-06-02 21:04:01 +0200409 xorl %eax, %eax /* do not leak kernel information */
410 movq %rax, R11(%rsp)
411 movq %rax, R10(%rsp)
412 movq %r9, R9(%rsp)
413 movq %rax, R8(%rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 movq %rsp,%rdi /* &pt_regs -> arg1 */
415 call syscall_trace_enter
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800416 LOAD_ARGS32 1 /* reload args from stack in case ptrace changed it */
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800417 RESTORE_EXTRA_REGS
Chuck Ebbertecd744e2007-11-07 10:48:39 -0500418 xchgl %ebp,%r9d
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 jmp cstar_do_call
Jan Beulich4b787e02006-06-26 13:56:55 +0200420END(ia32_cstar_target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422ia32_badarg:
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700423 ASM_CLAC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 movq $-EFAULT,%rax
425 jmp ia32_sysret
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Denys Vlasenko61b1e3e2015-06-02 19:35:10 +0200427ia32_ret_from_sys_call:
Denys Vlasenkoef0cd5d2015-06-02 21:04:01 +0200428 xorl %eax, %eax /* do not leak kernel information */
429 movq %rax, R11(%rsp)
430 movq %rax, R10(%rsp)
431 movq %rax, R9(%rsp)
432 movq %rax, R8(%rsp)
Denys Vlasenko61b1e3e2015-06-02 19:35:10 +0200433 jmp int_ret_from_sys_call
434
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800435/*
436 * Emulated IA32 system calls via int 0x80.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 *
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800438 * Arguments:
439 * eax system call number
440 * ebx arg1
441 * ecx arg2
442 * edx arg3
443 * esi arg4
444 * edi arg5
445 * ebp arg6 (note: not saved in the stack frame, should not be touched)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 *
447 * Notes:
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800448 * Uses the same stack frame as the x86-64 version.
449 * All registers except eax must be saved (but ptrace may violate that).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 * Arguments are zero extended. For system calls that want sign extension and
451 * take long arguments a wrapper is needed. Most calls can just be called
452 * directly.
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800453 * Assumes it is only called from user space and entered with interrupts off.
454 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456ENTRY(ia32_syscall)
Denys Vlasenkoa232e3d2015-03-27 11:36:20 +0100457 /*
458 * Interrupts are off on entry.
459 * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
460 * it is too small to ever cause noticeable irq latency.
461 */
Jeremy Fitzhardinge360c0442008-07-08 15:06:28 -0700462 PARAVIRT_ADJUST_EXCEPTION_FRAME
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -0400463 SWAPGS
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -0400464 ENABLE_INTERRUPTS(CLBR_NONE)
Denys Vlasenkoa232e3d2015-03-27 11:36:20 +0100465
Denys Vlasenko4ee8ec12015-03-27 11:36:21 +0100466 /* Zero-extending 32-bit regs, do not remove */
467 movl %eax,%eax
468
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +0200469 /* Construct struct pt_regs on stack (iret frame is already on stack) */
Ingo Molnar131484c2015-05-28 12:21:47 +0200470 pushq %rax /* pt_regs->orig_ax */
471 pushq %rdi /* pt_regs->di */
472 pushq %rsi /* pt_regs->si */
473 pushq %rdx /* pt_regs->dx */
474 pushq %rcx /* pt_regs->cx */
475 pushq $-ENOSYS /* pt_regs->ax */
Denys Vlasenko61b1e3e2015-06-02 19:35:10 +0200476 pushq $0 /* pt_regs->r8 */
477 pushq $0 /* pt_regs->r9 */
478 pushq $0 /* pt_regs->r10 */
479 pushq $0 /* pt_regs->r11 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 cld
Denys Vlasenko61b1e3e2015-06-02 19:35:10 +0200481 sub $(6*8),%rsp /* pt_regs->bp,bx,r12-15 not saved */
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +0200482
Ingo Molnardca5b522015-03-24 19:44:42 +0100483 orl $TS_COMPAT, ASM_THREAD_INFO(TI_status, %rsp, SIZEOF_PTREGS)
484 testl $_TIF_WORK_SYSCALL_ENTRY, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 jnz ia32_tracesys
Roland McGrathc09249f2009-02-06 18:15:18 -0800486ia32_do_call:
Denys Vlasenko14f6e952015-02-26 14:40:35 -0800487 /* 32bit syscall -> 64bit C ABI argument conversion */
488 movl %edi,%r8d /* arg5 */
489 movl %ebp,%r9d /* arg6 */
490 xchg %ecx,%esi /* rsi:arg2, rcx:arg4 */
491 movl %ebx,%edi /* arg1 */
492 movl %edx,%edx /* arg3 (zero extension) */
Denys Vlasenko3f5159a2015-04-21 18:03:14 +0200493 cmpq $(IA32_NR_syscalls-1),%rax
494 ja 1f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 call *ia32_sys_call_table(,%rax,8) # xxx: rip relative
496ia32_sysret:
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800497 movq %rax,RAX(%rsp)
Denys Vlasenko3f5159a2015-04-21 18:03:14 +02004981:
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800499 jmp int_ret_from_sys_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800501ia32_tracesys:
502 SAVE_EXTRA_REGS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 movq %rsp,%rdi /* &pt_regs -> arg1 */
504 call syscall_trace_enter
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800505 LOAD_ARGS32 /* reload args from stack in case ptrace changed it */
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800506 RESTORE_EXTRA_REGS
Roland McGrathc09249f2009-02-06 18:15:18 -0800507 jmp ia32_do_call
Jan Beulich4b787e02006-06-26 13:56:55 +0200508END(ia32_syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Ramkumar Ramachandrad2475b82013-07-10 23:34:28 +0530510 .macro PTREGSCALL label, func
Jan Beulichf6b2bc82011-11-29 11:24:10 +0000511 ALIGN
512GLOBAL(\label)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 leaq \func(%rip),%rax
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 jmp ia32_ptregs_common
515 .endm
516
Al Viro3fe26fa2012-11-12 14:32:42 -0500517 PTREGSCALL stub32_rt_sigreturn, sys32_rt_sigreturn
518 PTREGSCALL stub32_sigreturn, sys32_sigreturn
Al Viro3fe26fa2012-11-12 14:32:42 -0500519 PTREGSCALL stub32_fork, sys_fork
520 PTREGSCALL stub32_vfork, sys_vfork
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Jan Beulichf6b2bc82011-11-29 11:24:10 +0000522 ALIGN
Al Viro1d4b4b22012-10-22 22:34:11 -0400523GLOBAL(stub32_clone)
524 leaq sys_clone(%rip),%rax
525 mov %r8, %rcx
526 jmp ia32_ptregs_common
527
528 ALIGN
Jan Beulichf6b2bc82011-11-29 11:24:10 +0000529ia32_ptregs_common:
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800530 SAVE_EXTRA_REGS 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 call *%rax
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800532 RESTORE_EXTRA_REGS 8
533 ret
Jan Beulich4b787e02006-06-26 13:56:55 +0200534END(ia32_ptregs_common)