blob: 56f819e99258cf6b5544ea97b8070b951e5fa314 [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
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -040032#ifdef CONFIG_PARAVIRT
33ENTRY(native_usergs_sysret32)
34 swapgs
35 sysretl
36ENDPROC(native_usergs_sysret32)
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -040037#endif
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/*
40 * 32bit SYSENTER instruction entry.
41 *
Denys Vlasenkob87cf632015-02-26 14:40:32 -080042 * SYSENTER loads ss, rsp, cs, and rip from previously programmed MSRs.
43 * IF and VM in rflags are cleared (IOW: interrupts are off).
44 * SYSENTER does not save anything on the stack,
45 * and does not save old rip (!!!) and rflags.
46 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 * Arguments:
Denys Vlasenkob87cf632015-02-26 14:40:32 -080048 * eax system call number
49 * ebx arg1
50 * ecx arg2
51 * edx arg3
52 * esi arg4
53 * edi arg5
54 * ebp user stack
55 * 0(%ebp) arg6
56 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 * This is purely a fast path. For anything complicated we use the int 0x80
Denys Vlasenkob87cf632015-02-26 14:40:32 -080058 * path below. We set up a complete hardware stack frame to share code
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 * with the int 0x80 path.
Denys Vlasenkob87cf632015-02-26 14:40:32 -080060 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061ENTRY(ia32_sysenter_target)
Denys Vlasenkoa232e3d2015-03-27 11:36:20 +010062 /*
63 * Interrupts are off on entry.
64 * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
65 * it is too small to ever cause noticeable irq latency.
66 */
Jeremy Fitzhardinge457da702008-06-26 07:28:51 -070067 SWAPGS_UNSAFE_STACK
Denys Vlasenko3a232082015-04-24 17:31:35 +020068 movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -040069 ENABLE_INTERRUPTS(CLBR_NONE)
Denys Vlasenkoa232e3d2015-03-27 11:36:20 +010070
Denys Vlasenko4ee8ec12015-03-27 11:36:21 +010071 /* Zero-extending 32-bit regs, do not remove */
72 movl %ebp, %ebp
73 movl %eax, %eax
74
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +020075 movl ASM_THREAD_INFO(TI_sysenter_return, %rsp, 0), %r10d
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +020076
77 /* Construct struct pt_regs on stack */
Ingo Molnar131484c2015-05-28 12:21:47 +020078 pushq $__USER32_DS /* pt_regs->ss */
79 pushq %rbp /* pt_regs->sp */
80 pushfq /* pt_regs->flags */
81 pushq $__USER32_CS /* pt_regs->cs */
82 pushq %r10 /* pt_regs->ip = thread_info->sysenter_return */
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 cld
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +020090 sub $(10*8),%rsp /* pt_regs->r8-11,bp,bx,r12-15 not saved */
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +020091
Denys Vlasenkob87cf632015-02-26 14:40:32 -080092 /*
93 * no need to do an access_ok check here because rbp has been
94 * 32bit zero extended
95 */
H. Peter Anvin63bcff22012-09-21 12:43:12 -070096 ASM_STAC
Jan Beulich295286a2008-08-29 13:21:11 +0100971: movl (%rbp),%ebp
H. Peter Anvin1ce6f862012-04-20 12:19:50 -070098 _ASM_EXTABLE(1b,ia32_badarg)
H. Peter Anvin63bcff22012-09-21 12:43:12 -070099 ASM_CLAC
Andy Lutomirski8c7aa692014-10-01 11:49:04 -0700100
101 /*
102 * Sysenter doesn't filter flags, so we need to clear NT
103 * ourselves. To save a few cycles, we can check whether
104 * NT was set instead of doing an unconditional popfq.
105 */
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800106 testl $X86_EFLAGS_NT,EFLAGS(%rsp)
Andy Lutomirski8c7aa692014-10-01 11:49:04 -0700107 jnz sysenter_fix_flags
108sysenter_flags_fixed:
109
Ingo Molnardca5b522015-03-24 19:44:42 +0100110 orl $TS_COMPAT, ASM_THREAD_INFO(TI_status, %rsp, SIZEOF_PTREGS)
111 testl $_TIF_WORK_SYSCALL_ENTRY, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 jnz sysenter_tracesys
Ingo Molnar131484c2015-05-28 12:21:47 +0200113
Roland McGrathd4d67152008-07-09 02:38:07 -0700114sysenter_do_call:
Denys Vlasenko14f6e952015-02-26 14:40:35 -0800115 /* 32bit syscall -> 64bit C ABI argument conversion */
116 movl %edi,%r8d /* arg5 */
117 movl %ebp,%r9d /* arg6 */
118 xchg %ecx,%esi /* rsi:arg2, rcx:arg4 */
119 movl %ebx,%edi /* arg1 */
120 movl %edx,%edx /* arg3 (zero extension) */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700121sysenter_dispatch:
Denys Vlasenko3f5159a2015-04-21 18:03:14 +0200122 cmpq $(IA32_NR_syscalls-1),%rax
123 ja 1f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 call *ia32_sys_call_table(,%rax,8)
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800125 movq %rax,RAX(%rsp)
Denys Vlasenko3f5159a2015-04-21 18:03:14 +02001261:
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -0400127 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700128 TRACE_IRQS_OFF
Ingo Molnardca5b522015-03-24 19:44:42 +0100129 testl $_TIF_ALLWORK_MASK, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Roland McGrath5cbf1562008-06-24 01:13:31 -0700130 jnz sysexit_audit
131sysexit_from_sys_call:
Andy Lutomirski4214a162015-04-02 17:12:12 -0700132 /*
133 * NB: SYSEXIT is not obviously safe for 64-bit kernels -- an
134 * NMI between STI and SYSEXIT has poorly specified behavior,
135 * and and NMI followed by an IRQ with usergs is fatal. So
136 * we just pretend we're using SYSEXIT but we really use
137 * SYSRETL instead.
138 *
139 * This code path is still called 'sysexit' because it pairs
140 * with 'sysenter' and it uses the SYSENTER calling convention.
141 */
Ingo Molnardca5b522015-03-24 19:44:42 +0100142 andl $~TS_COMPAT,ASM_THREAD_INFO(TI_status, %rsp, SIZEOF_PTREGS)
Andy Lutomirski4214a162015-04-02 17:12:12 -0700143 movl RIP(%rsp),%ecx /* User %eip */
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800144 RESTORE_RSI_RDI
Andy Lutomirski4214a162015-04-02 17:12:12 -0700145 xorl %edx,%edx /* avoid info leaks */
Jan Beulich24e35802009-09-30 11:22:11 +0100146 xorq %r8,%r8
147 xorq %r9,%r9
148 xorq %r10,%r10
Andy Lutomirski4214a162015-04-02 17:12:12 -0700149 movl EFLAGS(%rsp),%r11d /* User eflags */
Ingo Molnar2601e642006-07-03 00:24:45 -0700150 TRACE_IRQS_ON
Andy Lutomirski4214a162015-04-02 17:12:12 -0700151
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800152 /*
Andy Lutomirski4214a162015-04-02 17:12:12 -0700153 * SYSRETL works even on Intel CPUs. Use it in preference to SYSEXIT,
154 * since it avoids a dicey window with interrupts enabled.
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800155 */
Andy Lutomirski4214a162015-04-02 17:12:12 -0700156 movl RSP(%rsp),%esp
157
158 /*
159 * USERGS_SYSRET32 does:
160 * gsbase = user's gs base
161 * eip = ecx
162 * rflags = r11
163 * cs = __USER32_CS
164 * ss = __USER_DS
165 *
166 * The prologue set RIP(%rsp) to VDSO32_SYSENTER_RETURN, which does:
167 *
168 * pop %ebp
169 * pop %edx
170 * pop %ecx
171 *
172 * Therefore, we invoke SYSRETL with EDX and R8-R10 zeroed to
173 * avoid info leaks. R11 ends up with VDSO32_SYSENTER_RETURN's
174 * address (already known to user code), and R12-R15 are
175 * callee-saved and therefore don't contain any interesting
176 * kernel data.
177 */
178 USERGS_SYSRET32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Roland McGrath5cbf1562008-06-24 01:13:31 -0700180#ifdef CONFIG_AUDITSYSCALL
181 .macro auditsys_entry_common
Richard Guy Briggsb4f0d372014-03-04 10:38:06 -0500182 movl %esi,%r8d /* 5th arg: 4th syscall arg */
183 movl %ecx,%r9d /*swap with edx*/
184 movl %edx,%ecx /* 4th arg: 3rd syscall arg */
185 movl %r9d,%edx /* 3rd arg: 2nd syscall arg */
186 movl %ebx,%esi /* 2nd arg: 1st syscall arg */
187 movl %eax,%edi /* 1st arg: syscall number */
Eric Parisb05d8442012-01-03 14:23:06 -0500188 call __audit_syscall_entry
Denys Vlasenko3f5159a2015-04-21 18:03:14 +0200189 movl ORIG_RAX(%rsp),%eax /* reload syscall number */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700190 movl %ebx,%edi /* reload 1st syscall arg */
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800191 movl RCX(%rsp),%esi /* reload 2nd syscall arg */
192 movl RDX(%rsp),%edx /* reload 3rd syscall arg */
193 movl RSI(%rsp),%ecx /* reload 4th syscall arg */
194 movl RDI(%rsp),%r8d /* reload 5th syscall arg */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700195 .endm
196
Jan Beulich81766742009-10-26 15:20:29 +0000197 .macro auditsys_exit exit
Ingo Molnardca5b522015-03-24 19:44:42 +0100198 testl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT), ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Jan Beulich24e35802009-09-30 11:22:11 +0100199 jnz ia32_ret_from_sys_call
Roland McGrath5cbf1562008-06-24 01:13:31 -0700200 TRACE_IRQS_ON
Jan Beulich40a1ef92013-01-30 07:55:53 +0000201 ENABLE_INTERRUPTS(CLBR_NONE)
Roland McGrath5cbf1562008-06-24 01:13:31 -0700202 movl %eax,%esi /* second arg, syscall return value */
Eric Parisd7e75282012-01-03 14:23:06 -0500203 cmpl $-MAX_ERRNO,%eax /* is it an error ? */
Eric Parisf031cd22012-01-03 14:23:06 -0500204 jbe 1f
205 movslq %eax, %rsi /* if error sign extend to 64 bits */
2061: setbe %al /* 1 if error, 0 if not */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700207 movzbl %al,%edi /* zero-extend that into %edi */
Eric Parisd7e75282012-01-03 14:23:06 -0500208 call __audit_syscall_exit
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800209 movq RAX(%rsp),%rax /* reload syscall return value */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700210 movl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT),%edi
Jan Beulich40a1ef92013-01-30 07:55:53 +0000211 DISABLE_INTERRUPTS(CLBR_NONE)
Roland McGrath5cbf1562008-06-24 01:13:31 -0700212 TRACE_IRQS_OFF
Ingo Molnardca5b522015-03-24 19:44:42 +0100213 testl %edi, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Jan Beulich24e35802009-09-30 11:22:11 +0100214 jz \exit
Denys Vlasenkoef0cd5d2015-06-02 21:04:01 +0200215 xorl %eax, %eax /* do not leak kernel information */
216 movq %rax, R11(%rsp)
217 movq %rax, R10(%rsp)
218 movq %rax, R9(%rsp)
219 movq %rax, R8(%rsp)
Jan Beulich24e35802009-09-30 11:22:11 +0100220 jmp int_with_check
Roland McGrath5cbf1562008-06-24 01:13:31 -0700221 .endm
222
223sysenter_auditsys:
Roland McGrath5cbf1562008-06-24 01:13:31 -0700224 auditsys_entry_common
225 movl %ebp,%r9d /* reload 6th syscall arg */
226 jmp sysenter_dispatch
227
228sysexit_audit:
229 auditsys_exit sysexit_from_sys_call
230#endif
231
Andy Lutomirski8c7aa692014-10-01 11:49:04 -0700232sysenter_fix_flags:
Ingo Molnar131484c2015-05-28 12:21:47 +0200233 pushq $(X86_EFLAGS_IF|X86_EFLAGS_FIXED)
234 popfq
Andy Lutomirski8c7aa692014-10-01 11:49:04 -0700235 jmp sysenter_flags_fixed
236
Roland McGrath5cbf1562008-06-24 01:13:31 -0700237sysenter_tracesys:
Roland McGrath5cbf1562008-06-24 01:13:31 -0700238#ifdef CONFIG_AUDITSYSCALL
Ingo Molnardca5b522015-03-24 19:44:42 +0100239 testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT), ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Roland McGrath5cbf1562008-06-24 01:13:31 -0700240 jz sysenter_auditsys
241#endif
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800242 SAVE_EXTRA_REGS
Denys Vlasenkoef0cd5d2015-06-02 21:04:01 +0200243 xorl %eax, %eax /* do not leak kernel information */
244 movq %rax, R11(%rsp)
245 movq %rax, R10(%rsp)
246 movq %rax, R9(%rsp)
247 movq %rax, R8(%rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 movq %rsp,%rdi /* &pt_regs -> arg1 */
249 call syscall_trace_enter
Denys Vlasenko73cbf682015-06-02 21:04:02 +0200250
251 /* Reload arg registers from stack. (see sysenter_tracesys) */
252 movl RCX(%rsp), %ecx
253 movl RDX(%rsp), %edx
254 movl RSI(%rsp), %esi
255 movl RDI(%rsp), %edi
256 movl %eax, %eax /* zero extension */
257
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800258 RESTORE_EXTRA_REGS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 jmp sysenter_do_call
Jan Beulich4b787e02006-06-26 13:56:55 +0200260ENDPROC(ia32_sysenter_target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262/*
263 * 32bit SYSCALL instruction entry.
264 *
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800265 * 32bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11,
266 * then loads new ss, cs, and rip from previously programmed MSRs.
267 * rflags gets masked by a value from another MSR (so CLD and CLAC
268 * are not needed). SYSCALL does not save anything on the stack
269 * and does not change rsp.
270 *
271 * Note: rflags saving+masking-with-MSR happens only in Long mode
272 * (in legacy 32bit mode, IF, RF and VM bits are cleared and that's it).
273 * Don't get confused: rflags saving+masking depends on Long Mode Active bit
274 * (EFER.LMA=1), NOT on bitness of userspace where SYSCALL executes
275 * or target CS descriptor's L bit (SYSCALL does not read segment descriptors).
276 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 * Arguments:
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800278 * eax system call number
279 * ecx return address
280 * ebx arg1
281 * ebp arg2 (note: not saved in the stack frame, should not be touched)
282 * edx arg3
283 * esi arg4
284 * edi arg5
285 * esp user stack
286 * 0(%esp) arg6
287 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 * This is purely a fast path. For anything complicated we use the int 0x80
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800289 * path below. We set up a complete hardware stack frame to share code
290 * with the int 0x80 path.
291 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292ENTRY(ia32_cstar_target)
Denys Vlasenkoa232e3d2015-03-27 11:36:20 +0100293 /*
294 * Interrupts are off on entry.
295 * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
296 * it is too small to ever cause noticeable irq latency.
297 */
Jeremy Fitzhardinge457da702008-06-26 07:28:51 -0700298 SWAPGS_UNSAFE_STACK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 movl %esp,%r8d
Denys Vlasenko3a232082015-04-24 17:31:35 +0200300 movq PER_CPU_VAR(cpu_current_top_of_stack),%rsp
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -0400301 ENABLE_INTERRUPTS(CLBR_NONE)
Denys Vlasenkoa232e3d2015-03-27 11:36:20 +0100302
Denys Vlasenko4ee8ec12015-03-27 11:36:21 +0100303 /* Zero-extending 32-bit regs, do not remove */
304 movl %eax,%eax
305
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +0200306 /* Construct struct pt_regs on stack */
Ingo Molnar131484c2015-05-28 12:21:47 +0200307 pushq $__USER32_DS /* pt_regs->ss */
308 pushq %r8 /* pt_regs->sp */
309 pushq %r11 /* pt_regs->flags */
310 pushq $__USER32_CS /* pt_regs->cs */
311 pushq %rcx /* pt_regs->ip */
312 pushq %rax /* pt_regs->orig_ax */
313 pushq %rdi /* pt_regs->di */
314 pushq %rsi /* pt_regs->si */
315 pushq %rdx /* pt_regs->dx */
316 pushq %rbp /* pt_regs->cx */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 movl %ebp,%ecx
Ingo Molnar131484c2015-05-28 12:21:47 +0200318 pushq $-ENOSYS /* pt_regs->ax */
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +0200319 sub $(10*8),%rsp /* pt_regs->r8-11,bp,bx,r12-15 not saved */
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +0200320
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800321 /*
322 * no need to do an access_ok check here because r8 has been
323 * 32bit zero extended
324 */
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700325 ASM_STAC
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261: movl (%r8),%r9d
H. Peter Anvina3e859f2012-04-20 16:51:50 -0700327 _ASM_EXTABLE(1b,ia32_badarg)
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700328 ASM_CLAC
Ingo Molnardca5b522015-03-24 19:44:42 +0100329 orl $TS_COMPAT, ASM_THREAD_INFO(TI_status, %rsp, SIZEOF_PTREGS)
330 testl $_TIF_WORK_SYSCALL_ENTRY, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 jnz cstar_tracesys
Ingo Molnar131484c2015-05-28 12:21:47 +0200332
Jan Beulich295286a2008-08-29 13:21:11 +0100333cstar_do_call:
Denys Vlasenko14f6e952015-02-26 14:40:35 -0800334 /* 32bit syscall -> 64bit C ABI argument conversion */
335 movl %edi,%r8d /* arg5 */
336 /* r9 already loaded */ /* arg6 */
337 xchg %ecx,%esi /* rsi:arg2, rcx:arg4 */
338 movl %ebx,%edi /* arg1 */
339 movl %edx,%edx /* arg3 (zero extension) */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700340cstar_dispatch:
Denys Vlasenko3f5159a2015-04-21 18:03:14 +0200341 cmpq $(IA32_NR_syscalls-1),%rax
342 ja 1f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 call *ia32_sys_call_table(,%rax,8)
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800344 movq %rax,RAX(%rsp)
Denys Vlasenko3f5159a2015-04-21 18:03:14 +02003451:
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -0400346 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700347 TRACE_IRQS_OFF
Ingo Molnardca5b522015-03-24 19:44:42 +0100348 testl $_TIF_ALLWORK_MASK, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Roland McGrath5cbf1562008-06-24 01:13:31 -0700349 jnz sysretl_audit
350sysretl_from_sys_call:
Ingo Molnardca5b522015-03-24 19:44:42 +0100351 andl $~TS_COMPAT, ASM_THREAD_INFO(TI_status, %rsp, SIZEOF_PTREGS)
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800352 RESTORE_RSI_RDI_RDX
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800353 movl RIP(%rsp),%ecx
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800354 movl EFLAGS(%rsp),%r11d
Jan Beulich24e35802009-09-30 11:22:11 +0100355 xorq %r10,%r10
356 xorq %r9,%r9
357 xorq %r8,%r8
Ingo Molnar2601e642006-07-03 00:24:45 -0700358 TRACE_IRQS_ON
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800359 movl RSP(%rsp),%esp
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800360 /*
361 * 64bit->32bit SYSRET restores eip from ecx,
362 * eflags from r11 (but RF and VM bits are forced to 0),
363 * cs and ss are loaded from MSRs.
364 * (Note: 32bit->32bit SYSRET is different: since r11
365 * does not exist, it merely sets eflags.IF=1).
Andy Lutomirski61f01dd2015-04-26 16:47:59 -0700366 *
367 * NB: On AMD CPUs with the X86_BUG_SYSRET_SS_ATTRS bug, the ss
368 * descriptor is not reinitialized. This means that we must
369 * avoid SYSRET with SS == NULL, which could happen if we schedule,
370 * exit the kernel, and re-enter using an interrupt vector. (All
371 * interrupt entries on x86_64 set SS to NULL.) We prevent that
372 * from happening by reloading SS in __switch_to.
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800373 */
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -0400374 USERGS_SYSRET32
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800375
Roland McGrath5cbf1562008-06-24 01:13:31 -0700376#ifdef CONFIG_AUDITSYSCALL
377cstar_auditsys:
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800378 movl %r9d,R9(%rsp) /* register to be clobbered by call */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700379 auditsys_entry_common
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800380 movl R9(%rsp),%r9d /* reload 6th syscall arg */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700381 jmp cstar_dispatch
382
383sysretl_audit:
Jan Beulich81766742009-10-26 15:20:29 +0000384 auditsys_exit sysretl_from_sys_call
Roland McGrath5cbf1562008-06-24 01:13:31 -0700385#endif
386
387cstar_tracesys:
388#ifdef CONFIG_AUDITSYSCALL
Ingo Molnardca5b522015-03-24 19:44:42 +0100389 testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT), ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Roland McGrath5cbf1562008-06-24 01:13:31 -0700390 jz cstar_auditsys
391#endif
Chuck Ebbertecd744e2007-11-07 10:48:39 -0500392 xchgl %r9d,%ebp
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800393 SAVE_EXTRA_REGS
Denys Vlasenkoef0cd5d2015-06-02 21:04:01 +0200394 xorl %eax, %eax /* do not leak kernel information */
395 movq %rax, R11(%rsp)
396 movq %rax, R10(%rsp)
397 movq %r9, R9(%rsp)
398 movq %rax, R8(%rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 movq %rsp,%rdi /* &pt_regs -> arg1 */
400 call syscall_trace_enter
Denys Vlasenko73cbf682015-06-02 21:04:02 +0200401 movl R9(%rsp),%r9d
402
403 /* Reload arg registers from stack. (see sysenter_tracesys) */
404 movl RCX(%rsp), %ecx
405 movl RDX(%rsp), %edx
406 movl RSI(%rsp), %esi
407 movl RDI(%rsp), %edi
408 movl %eax, %eax /* zero extension */
409
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800410 RESTORE_EXTRA_REGS
Chuck Ebbertecd744e2007-11-07 10:48:39 -0500411 xchgl %ebp,%r9d
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 jmp cstar_do_call
Jan Beulich4b787e02006-06-26 13:56:55 +0200413END(ia32_cstar_target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415ia32_badarg:
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700416 ASM_CLAC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 movq $-EFAULT,%rax
418 jmp ia32_sysret
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Denys Vlasenko61b1e3e2015-06-02 19:35:10 +0200420ia32_ret_from_sys_call:
Denys Vlasenkoef0cd5d2015-06-02 21:04:01 +0200421 xorl %eax, %eax /* do not leak kernel information */
422 movq %rax, R11(%rsp)
423 movq %rax, R10(%rsp)
424 movq %rax, R9(%rsp)
425 movq %rax, R8(%rsp)
Denys Vlasenko61b1e3e2015-06-02 19:35:10 +0200426 jmp int_ret_from_sys_call
427
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800428/*
429 * Emulated IA32 system calls via int 0x80.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 *
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800431 * Arguments:
432 * eax system call number
433 * ebx arg1
434 * ecx arg2
435 * edx arg3
436 * esi arg4
437 * edi arg5
438 * ebp arg6 (note: not saved in the stack frame, should not be touched)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 *
440 * Notes:
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800441 * Uses the same stack frame as the x86-64 version.
442 * All registers except eax must be saved (but ptrace may violate that).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 * Arguments are zero extended. For system calls that want sign extension and
444 * take long arguments a wrapper is needed. Most calls can just be called
445 * directly.
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800446 * Assumes it is only called from user space and entered with interrupts off.
447 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449ENTRY(ia32_syscall)
Denys Vlasenkoa232e3d2015-03-27 11:36:20 +0100450 /*
451 * Interrupts are off on entry.
452 * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
453 * it is too small to ever cause noticeable irq latency.
454 */
Jeremy Fitzhardinge360c0442008-07-08 15:06:28 -0700455 PARAVIRT_ADJUST_EXCEPTION_FRAME
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -0400456 SWAPGS
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -0400457 ENABLE_INTERRUPTS(CLBR_NONE)
Denys Vlasenkoa232e3d2015-03-27 11:36:20 +0100458
Denys Vlasenko4ee8ec12015-03-27 11:36:21 +0100459 /* Zero-extending 32-bit regs, do not remove */
460 movl %eax,%eax
461
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +0200462 /* Construct struct pt_regs on stack (iret frame is already on stack) */
Ingo Molnar131484c2015-05-28 12:21:47 +0200463 pushq %rax /* pt_regs->orig_ax */
464 pushq %rdi /* pt_regs->di */
465 pushq %rsi /* pt_regs->si */
466 pushq %rdx /* pt_regs->dx */
467 pushq %rcx /* pt_regs->cx */
468 pushq $-ENOSYS /* pt_regs->ax */
Denys Vlasenko61b1e3e2015-06-02 19:35:10 +0200469 pushq $0 /* pt_regs->r8 */
470 pushq $0 /* pt_regs->r9 */
471 pushq $0 /* pt_regs->r10 */
472 pushq $0 /* pt_regs->r11 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 cld
Denys Vlasenko61b1e3e2015-06-02 19:35:10 +0200474 sub $(6*8),%rsp /* pt_regs->bp,bx,r12-15 not saved */
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +0200475
Ingo Molnardca5b522015-03-24 19:44:42 +0100476 orl $TS_COMPAT, ASM_THREAD_INFO(TI_status, %rsp, SIZEOF_PTREGS)
477 testl $_TIF_WORK_SYSCALL_ENTRY, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 jnz ia32_tracesys
Roland McGrathc09249f2009-02-06 18:15:18 -0800479ia32_do_call:
Denys Vlasenko14f6e952015-02-26 14:40:35 -0800480 /* 32bit syscall -> 64bit C ABI argument conversion */
481 movl %edi,%r8d /* arg5 */
482 movl %ebp,%r9d /* arg6 */
483 xchg %ecx,%esi /* rsi:arg2, rcx:arg4 */
484 movl %ebx,%edi /* arg1 */
485 movl %edx,%edx /* arg3 (zero extension) */
Denys Vlasenko3f5159a2015-04-21 18:03:14 +0200486 cmpq $(IA32_NR_syscalls-1),%rax
487 ja 1f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 call *ia32_sys_call_table(,%rax,8) # xxx: rip relative
489ia32_sysret:
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800490 movq %rax,RAX(%rsp)
Denys Vlasenko3f5159a2015-04-21 18:03:14 +02004911:
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800492 jmp int_ret_from_sys_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800494ia32_tracesys:
495 SAVE_EXTRA_REGS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 movq %rsp,%rdi /* &pt_regs -> arg1 */
497 call syscall_trace_enter
Denys Vlasenko73cbf682015-06-02 21:04:02 +0200498 /*
499 * Reload arg registers from stack in case ptrace changed them.
500 * Don't reload %eax because syscall_trace_enter() returned
501 * the %rax value we should see. But do truncate it to 32 bits.
502 * If it's -1 to make us punt the syscall, then (u32)-1 is still
503 * an appropriately invalid value.
504 */
505 movl RCX(%rsp), %ecx
506 movl RDX(%rsp), %edx
507 movl RSI(%rsp), %esi
508 movl RDI(%rsp), %edi
509 movl %eax, %eax /* zero extension */
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800510 RESTORE_EXTRA_REGS
Roland McGrathc09249f2009-02-06 18:15:18 -0800511 jmp ia32_do_call
Jan Beulich4b787e02006-06-26 13:56:55 +0200512END(ia32_syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Ramkumar Ramachandrad2475b82013-07-10 23:34:28 +0530514 .macro PTREGSCALL label, func
Jan Beulichf6b2bc82011-11-29 11:24:10 +0000515 ALIGN
516GLOBAL(\label)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 leaq \func(%rip),%rax
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 jmp ia32_ptregs_common
519 .endm
520
Al Viro3fe26fa2012-11-12 14:32:42 -0500521 PTREGSCALL stub32_rt_sigreturn, sys32_rt_sigreturn
522 PTREGSCALL stub32_sigreturn, sys32_sigreturn
Al Viro3fe26fa2012-11-12 14:32:42 -0500523 PTREGSCALL stub32_fork, sys_fork
524 PTREGSCALL stub32_vfork, sys_vfork
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Jan Beulichf6b2bc82011-11-29 11:24:10 +0000526 ALIGN
Al Viro1d4b4b22012-10-22 22:34:11 -0400527GLOBAL(stub32_clone)
528 leaq sys_clone(%rip),%rax
529 mov %r8, %rcx
530 jmp ia32_ptregs_common
531
532 ALIGN
Jan Beulichf6b2bc82011-11-29 11:24:10 +0000533ia32_ptregs_common:
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800534 SAVE_EXTRA_REGS 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 call *%rax
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800536 RESTORE_EXTRA_REGS 8
537 ret
Jan Beulich4b787e02006-06-26 13:56:55 +0200538END(ia32_ptregs_common)