blob: 7502ff0b938e0296320b839d09c4857d48248225 [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
7#include <asm/dwarf2.h>
8#include <asm/calling.h>
Sam Ravnborge2d5df92005-09-09 21:28:48 +02009#include <asm/asm-offsets.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <asm/current.h>
11#include <asm/errno.h>
12#include <asm/ia32_unistd.h>
13#include <asm/thread_info.h>
14#include <asm/segment.h>
Ingo Molnar2601e642006-07-03 00:24:45 -070015#include <asm/irqflags.h>
H. Peter Anvin1ce6f862012-04-20 12:19:50 -070016#include <asm/asm.h>
H. Peter Anvin63bcff22012-09-21 12:43:12 -070017#include <asm/smap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/linkage.h>
Eric Parisd7e75282012-01-03 14:23:06 -050019#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Roland McGrath5cbf1562008-06-24 01:13:31 -070021/* Avoid __ASSEMBLER__'ifying <linux/audit.h> just for this. */
22#include <linux/elf-em.h>
23#define AUDIT_ARCH_I386 (EM_386|__AUDIT_ARCH_LE)
24#define __AUDIT_ARCH_LE 0x40000000
25
26#ifndef CONFIG_AUDITSYSCALL
Jan Beulich24e35802009-09-30 11:22:11 +010027#define sysexit_audit ia32_ret_from_sys_call
28#define sysretl_audit ia32_ret_from_sys_call
Roland McGrath5cbf1562008-06-24 01:13:31 -070029#endif
30
Jiri Olsaea714542011-03-07 19:10:39 +010031 .section .entry.text, "ax"
32
Denys Vlasenkof2db9382015-02-26 14:40:30 -080033 /* clobbers %rax */
34 .macro CLEAR_RREGS _r9=rax
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 xorl %eax,%eax
Denys Vlasenkof2db9382015-02-26 14:40:30 -080036 movq %rax,R11(%rsp)
37 movq %rax,R10(%rsp)
38 movq %\_r9,R9(%rsp)
39 movq %rax,R8(%rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 .endm
41
Roland McGrathd4d67152008-07-09 02:38:07 -070042 /*
43 * Reload arg registers from stack in case ptrace changed them.
44 * We don't reload %eax because syscall_trace_enter() returned
Roland McGratheefdca02010-09-14 12:22:58 -070045 * the %rax value we should see. Instead, we just truncate that
46 * value to 32 bits again as we did on entry from user mode.
47 * If it's a new value set by user_regset during entry tracing,
48 * this matches the normal truncation of the user-mode value.
49 * If it's -1 to make us punt the syscall, then (u32)-1 is still
50 * an appropriately invalid value.
Roland McGrathd4d67152008-07-09 02:38:07 -070051 */
Denys Vlasenkof2db9382015-02-26 14:40:30 -080052 .macro LOAD_ARGS32 _r9=0
Jan Beulich295286a2008-08-29 13:21:11 +010053 .if \_r9
Denys Vlasenkof2db9382015-02-26 14:40:30 -080054 movl R9(%rsp),%r9d
Jan Beulich295286a2008-08-29 13:21:11 +010055 .endif
Denys Vlasenkof2db9382015-02-26 14:40:30 -080056 movl RCX(%rsp),%ecx
57 movl RDX(%rsp),%edx
58 movl RSI(%rsp),%esi
59 movl RDI(%rsp),%edi
Roland McGratheefdca02010-09-14 12:22:58 -070060 movl %eax,%eax /* zero extension */
Andi Kleen176df242007-09-21 16:16:18 +020061 .endm
62
Jan Beulich27651302006-01-11 22:41:59 +010063 .macro CFI_STARTPROC32 simple
64 CFI_STARTPROC \simple
65 CFI_UNDEFINED r8
66 CFI_UNDEFINED r9
67 CFI_UNDEFINED r10
68 CFI_UNDEFINED r11
69 CFI_UNDEFINED r12
70 CFI_UNDEFINED r13
71 CFI_UNDEFINED r14
72 CFI_UNDEFINED r15
73 .endm
74
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -040075#ifdef CONFIG_PARAVIRT
76ENTRY(native_usergs_sysret32)
77 swapgs
78 sysretl
79ENDPROC(native_usergs_sysret32)
80
81ENTRY(native_irq_enable_sysexit)
82 swapgs
83 sti
84 sysexit
85ENDPROC(native_irq_enable_sysexit)
86#endif
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/*
89 * 32bit SYSENTER instruction entry.
90 *
Denys Vlasenkob87cf632015-02-26 14:40:32 -080091 * SYSENTER loads ss, rsp, cs, and rip from previously programmed MSRs.
92 * IF and VM in rflags are cleared (IOW: interrupts are off).
93 * SYSENTER does not save anything on the stack,
94 * and does not save old rip (!!!) and rflags.
95 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 * Arguments:
Denys Vlasenkob87cf632015-02-26 14:40:32 -080097 * eax system call number
98 * ebx arg1
99 * ecx arg2
100 * edx arg3
101 * esi arg4
102 * edi arg5
103 * ebp user stack
104 * 0(%ebp) arg6
105 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 * This is purely a fast path. For anything complicated we use the int 0x80
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800107 * path below. We set up a complete hardware stack frame to share code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 * with the int 0x80 path.
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800109 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110ENTRY(ia32_sysenter_target)
Jan Beulich27651302006-01-11 22:41:59 +0100111 CFI_STARTPROC32 simple
Jan Beulichadf14232006-09-26 10:52:41 +0200112 CFI_SIGNAL_FRAME
Jan Beulich7effaa82005-09-12 18:49:24 +0200113 CFI_DEF_CFA rsp,0
114 CFI_REGISTER rsp,rbp
Denys Vlasenkoa232e3d2015-03-27 11:36:20 +0100115
116 /*
117 * Interrupts are off on entry.
118 * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
119 * it is too small to ever cause noticeable irq latency.
120 */
Jeremy Fitzhardinge457da702008-06-26 07:28:51 -0700121 SWAPGS_UNSAFE_STACK
Andy Lutomirski24933b82015-03-05 19:19:05 -0800122 movq PER_CPU_VAR(cpu_tss + TSS_sp0), %rsp
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -0400123 ENABLE_INTERRUPTS(CLBR_NONE)
Denys Vlasenkoa232e3d2015-03-27 11:36:20 +0100124
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800125 /* Construct iret frame (ss,rsp,rflags,cs,rip) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 movl %ebp,%ebp /* zero extension */
Jan Beulich60cf6372011-02-28 15:54:40 +0000127 pushq_cfi $__USER32_DS
Jan Beulich7effaa82005-09-12 18:49:24 +0200128 /*CFI_REL_OFFSET ss,0*/
Jan Beulich60cf6372011-02-28 15:54:40 +0000129 pushq_cfi %rbp
Jan Beulich7effaa82005-09-12 18:49:24 +0200130 CFI_REL_OFFSET rsp,0
Jan Beulich60cf6372011-02-28 15:54:40 +0000131 pushfq_cfi
Jan Beulich7effaa82005-09-12 18:49:24 +0200132 /*CFI_REL_OFFSET rflags,0*/
Ingo Molnardca5b522015-03-24 19:44:42 +0100133 movl ASM_THREAD_INFO(TI_sysenter_return, %rsp, 3*8), %r10d
Jan Beulich7effaa82005-09-12 18:49:24 +0200134 CFI_REGISTER rip,r10
Jan Beulich60cf6372011-02-28 15:54:40 +0000135 pushq_cfi $__USER32_CS
Jan Beulich7effaa82005-09-12 18:49:24 +0200136 /*CFI_REL_OFFSET cs,0*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 movl %eax, %eax
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800138 /* Store thread_info->sysenter_return in rip stack slot */
Jan Beulich60cf6372011-02-28 15:54:40 +0000139 pushq_cfi %r10
Jan Beulich7effaa82005-09-12 18:49:24 +0200140 CFI_REL_OFFSET rip,0
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800141 /* Store orig_ax */
Jan Beulich60cf6372011-02-28 15:54:40 +0000142 pushq_cfi %rax
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800143 /* Construct the rest of "struct pt_regs" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 cld
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800145 ALLOC_PT_GPREGS_ON_STACK
146 SAVE_C_REGS_EXCEPT_R891011
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800147 /*
148 * no need to do an access_ok check here because rbp has been
149 * 32bit zero extended
150 */
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700151 ASM_STAC
Jan Beulich295286a2008-08-29 13:21:11 +01001521: movl (%rbp),%ebp
H. Peter Anvin1ce6f862012-04-20 12:19:50 -0700153 _ASM_EXTABLE(1b,ia32_badarg)
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700154 ASM_CLAC
Andy Lutomirski8c7aa692014-10-01 11:49:04 -0700155
156 /*
157 * Sysenter doesn't filter flags, so we need to clear NT
158 * ourselves. To save a few cycles, we can check whether
159 * NT was set instead of doing an unconditional popfq.
160 */
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800161 testl $X86_EFLAGS_NT,EFLAGS(%rsp)
Andy Lutomirski8c7aa692014-10-01 11:49:04 -0700162 jnz sysenter_fix_flags
163sysenter_flags_fixed:
164
Ingo Molnardca5b522015-03-24 19:44:42 +0100165 orl $TS_COMPAT, ASM_THREAD_INFO(TI_status, %rsp, SIZEOF_PTREGS)
166 testl $_TIF_WORK_SYSCALL_ENTRY, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Jan Beulich7effaa82005-09-12 18:49:24 +0200167 CFI_REMEMBER_STATE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 jnz sysenter_tracesys
H. Peter Anvin36d001c2010-09-14 12:42:41 -0700169 cmpq $(IA32_NR_syscalls-1),%rax
Andi Kleen67d53ea2006-04-07 19:50:31 +0200170 ja ia32_badsys
Roland McGrathd4d67152008-07-09 02:38:07 -0700171sysenter_do_call:
Denys Vlasenko14f6e952015-02-26 14:40:35 -0800172 /* 32bit syscall -> 64bit C ABI argument conversion */
173 movl %edi,%r8d /* arg5 */
174 movl %ebp,%r9d /* arg6 */
175 xchg %ecx,%esi /* rsi:arg2, rcx:arg4 */
176 movl %ebx,%edi /* arg1 */
177 movl %edx,%edx /* arg3 (zero extension) */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700178sysenter_dispatch:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 call *ia32_sys_call_table(,%rax,8)
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800180 movq %rax,RAX(%rsp)
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -0400181 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700182 TRACE_IRQS_OFF
Ingo Molnardca5b522015-03-24 19:44:42 +0100183 testl $_TIF_ALLWORK_MASK, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Roland McGrath5cbf1562008-06-24 01:13:31 -0700184 jnz sysexit_audit
185sysexit_from_sys_call:
Ingo Molnardca5b522015-03-24 19:44:42 +0100186 andl $~TS_COMPAT,ASM_THREAD_INFO(TI_status, %rsp, SIZEOF_PTREGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 /* clear IF, that popfq doesn't enable interrupts early */
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800188 andl $~0x200,EFLAGS(%rsp)
189 movl RIP(%rsp),%edx /* User %eip */
Roland McGrath36197c92008-01-30 13:30:43 +0100190 CFI_REGISTER rip,rdx
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800191 RESTORE_RSI_RDI
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800192 /* pop everything except ss,rsp,rflags slots */
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800193 REMOVE_PT_GPREGS_FROM_STACK 3*8
Jan Beulich24e35802009-09-30 11:22:11 +0100194 xorq %r8,%r8
195 xorq %r9,%r9
196 xorq %r10,%r10
197 xorq %r11,%r11
Jan Beulich60cf6372011-02-28 15:54:40 +0000198 popfq_cfi
Jan Beulich7effaa82005-09-12 18:49:24 +0200199 /*CFI_RESTORE rflags*/
Jan Beulich60cf6372011-02-28 15:54:40 +0000200 popq_cfi %rcx /* User %esp */
Jan Beulich7effaa82005-09-12 18:49:24 +0200201 CFI_REGISTER rsp,rcx
Ingo Molnar2601e642006-07-03 00:24:45 -0700202 TRACE_IRQS_ON
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800203 /*
204 * 32bit SYSEXIT restores eip from edx, esp from ecx.
205 * cs and ss are loaded from MSRs.
206 */
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -0400207 ENABLE_INTERRUPTS_SYSEXIT32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Andy Lutomirski8c7aa692014-10-01 11:49:04 -0700209 CFI_RESTORE_STATE
210
Roland McGrath5cbf1562008-06-24 01:13:31 -0700211#ifdef CONFIG_AUDITSYSCALL
212 .macro auditsys_entry_common
Richard Guy Briggsb4f0d372014-03-04 10:38:06 -0500213 movl %esi,%r8d /* 5th arg: 4th syscall arg */
214 movl %ecx,%r9d /*swap with edx*/
215 movl %edx,%ecx /* 4th arg: 3rd syscall arg */
216 movl %r9d,%edx /* 3rd arg: 2nd syscall arg */
217 movl %ebx,%esi /* 2nd arg: 1st syscall arg */
218 movl %eax,%edi /* 1st arg: syscall number */
Eric Parisb05d8442012-01-03 14:23:06 -0500219 call __audit_syscall_entry
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800220 movl RAX(%rsp),%eax /* reload syscall number */
H. Peter Anvin36d001c2010-09-14 12:42:41 -0700221 cmpq $(IA32_NR_syscalls-1),%rax
Roland McGrath5cbf1562008-06-24 01:13:31 -0700222 ja ia32_badsys
223 movl %ebx,%edi /* reload 1st syscall arg */
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800224 movl RCX(%rsp),%esi /* reload 2nd syscall arg */
225 movl RDX(%rsp),%edx /* reload 3rd syscall arg */
226 movl RSI(%rsp),%ecx /* reload 4th syscall arg */
227 movl RDI(%rsp),%r8d /* reload 5th syscall arg */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700228 .endm
229
Jan Beulich81766742009-10-26 15:20:29 +0000230 .macro auditsys_exit exit
Ingo Molnardca5b522015-03-24 19:44:42 +0100231 testl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT), ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Jan Beulich24e35802009-09-30 11:22:11 +0100232 jnz ia32_ret_from_sys_call
Roland McGrath5cbf1562008-06-24 01:13:31 -0700233 TRACE_IRQS_ON
Jan Beulich40a1ef92013-01-30 07:55:53 +0000234 ENABLE_INTERRUPTS(CLBR_NONE)
Roland McGrath5cbf1562008-06-24 01:13:31 -0700235 movl %eax,%esi /* second arg, syscall return value */
Eric Parisd7e75282012-01-03 14:23:06 -0500236 cmpl $-MAX_ERRNO,%eax /* is it an error ? */
Eric Parisf031cd22012-01-03 14:23:06 -0500237 jbe 1f
238 movslq %eax, %rsi /* if error sign extend to 64 bits */
2391: setbe %al /* 1 if error, 0 if not */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700240 movzbl %al,%edi /* zero-extend that into %edi */
Eric Parisd7e75282012-01-03 14:23:06 -0500241 call __audit_syscall_exit
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800242 movq RAX(%rsp),%rax /* reload syscall return value */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700243 movl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT),%edi
Jan Beulich40a1ef92013-01-30 07:55:53 +0000244 DISABLE_INTERRUPTS(CLBR_NONE)
Roland McGrath5cbf1562008-06-24 01:13:31 -0700245 TRACE_IRQS_OFF
Ingo Molnardca5b522015-03-24 19:44:42 +0100246 testl %edi, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Jan Beulich24e35802009-09-30 11:22:11 +0100247 jz \exit
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800248 CLEAR_RREGS
Jan Beulich24e35802009-09-30 11:22:11 +0100249 jmp int_with_check
Roland McGrath5cbf1562008-06-24 01:13:31 -0700250 .endm
251
252sysenter_auditsys:
Roland McGrath5cbf1562008-06-24 01:13:31 -0700253 auditsys_entry_common
254 movl %ebp,%r9d /* reload 6th syscall arg */
255 jmp sysenter_dispatch
256
257sysexit_audit:
258 auditsys_exit sysexit_from_sys_call
259#endif
260
Andy Lutomirski8c7aa692014-10-01 11:49:04 -0700261sysenter_fix_flags:
262 pushq_cfi $(X86_EFLAGS_IF|X86_EFLAGS_FIXED)
263 popfq_cfi
264 jmp sysenter_flags_fixed
265
Roland McGrath5cbf1562008-06-24 01:13:31 -0700266sysenter_tracesys:
Roland McGrath5cbf1562008-06-24 01:13:31 -0700267#ifdef CONFIG_AUDITSYSCALL
Ingo Molnardca5b522015-03-24 19:44:42 +0100268 testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT), ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Roland McGrath5cbf1562008-06-24 01:13:31 -0700269 jz sysenter_auditsys
270#endif
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800271 SAVE_EXTRA_REGS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 CLEAR_RREGS
Roland McGrath48ee6792008-03-18 18:23:50 -0700273 movq $-ENOSYS,RAX(%rsp)/* ptrace can change this for a bad syscall */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 movq %rsp,%rdi /* &pt_regs -> arg1 */
275 call syscall_trace_enter
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800276 LOAD_ARGS32 /* reload args from stack in case ptrace changed it */
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800277 RESTORE_EXTRA_REGS
H. Peter Anvin36d001c2010-09-14 12:42:41 -0700278 cmpq $(IA32_NR_syscalls-1),%rax
Roland McGrath48ee6792008-03-18 18:23:50 -0700279 ja int_ret_from_sys_call /* sysenter_tracesys has set RAX(%rsp) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 jmp sysenter_do_call
281 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200282ENDPROC(ia32_sysenter_target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284/*
285 * 32bit SYSCALL instruction entry.
286 *
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800287 * 32bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11,
288 * then loads new ss, cs, and rip from previously programmed MSRs.
289 * rflags gets masked by a value from another MSR (so CLD and CLAC
290 * are not needed). SYSCALL does not save anything on the stack
291 * and does not change rsp.
292 *
293 * Note: rflags saving+masking-with-MSR happens only in Long mode
294 * (in legacy 32bit mode, IF, RF and VM bits are cleared and that's it).
295 * Don't get confused: rflags saving+masking depends on Long Mode Active bit
296 * (EFER.LMA=1), NOT on bitness of userspace where SYSCALL executes
297 * or target CS descriptor's L bit (SYSCALL does not read segment descriptors).
298 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 * Arguments:
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800300 * eax system call number
301 * ecx return address
302 * ebx arg1
303 * ebp arg2 (note: not saved in the stack frame, should not be touched)
304 * edx arg3
305 * esi arg4
306 * edi arg5
307 * esp user stack
308 * 0(%esp) arg6
309 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 * This is purely a fast path. For anything complicated we use the int 0x80
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800311 * path below. We set up a complete hardware stack frame to share code
312 * with the int 0x80 path.
313 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314ENTRY(ia32_cstar_target)
Jan Beulich27651302006-01-11 22:41:59 +0100315 CFI_STARTPROC32 simple
Jan Beulichadf14232006-09-26 10:52:41 +0200316 CFI_SIGNAL_FRAME
Denys Vlasenkoef593262015-03-19 18:17:46 +0100317 CFI_DEF_CFA rsp,0
Jan Beulich7effaa82005-09-12 18:49:24 +0200318 CFI_REGISTER rip,rcx
319 /*CFI_REGISTER rflags,r11*/
Denys Vlasenkoa232e3d2015-03-27 11:36:20 +0100320
321 /*
322 * Interrupts are off on entry.
323 * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
324 * it is too small to ever cause noticeable irq latency.
325 */
Jeremy Fitzhardinge457da702008-06-26 07:28:51 -0700326 SWAPGS_UNSAFE_STACK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 movl %esp,%r8d
Jan Beulich7effaa82005-09-12 18:49:24 +0200328 CFI_REGISTER rsp,r8
Brian Gerst9af45652009-01-19 00:38:58 +0900329 movq PER_CPU_VAR(kernel_stack),%rsp
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -0400330 ENABLE_INTERRUPTS(CLBR_NONE)
Denys Vlasenkoa232e3d2015-03-27 11:36:20 +0100331
Denys Vlasenkoef593262015-03-19 18:17:46 +0100332 ALLOC_PT_GPREGS_ON_STACK 6*8 /* 6*8: space for orig_ax and iret frame */
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800333 SAVE_C_REGS_EXCEPT_RCX_R891011
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 movl %eax,%eax /* zero extension */
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800335 movq %rax,ORIG_RAX(%rsp)
336 movq %rcx,RIP(%rsp)
337 CFI_REL_OFFSET rip,RIP
338 movq %rbp,RCX(%rsp) /* this lies slightly to ptrace */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 movl %ebp,%ecx
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800340 movq $__USER32_CS,CS(%rsp)
341 movq $__USER32_DS,SS(%rsp)
342 movq %r11,EFLAGS(%rsp)
343 /*CFI_REL_OFFSET rflags,EFLAGS*/
344 movq %r8,RSP(%rsp)
345 CFI_REL_OFFSET rsp,RSP
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800346 /* iret stack frame is complete now */
347 /*
348 * no need to do an access_ok check here because r8 has been
349 * 32bit zero extended
350 */
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700351 ASM_STAC
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521: movl (%r8),%r9d
H. Peter Anvina3e859f2012-04-20 16:51:50 -0700353 _ASM_EXTABLE(1b,ia32_badarg)
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700354 ASM_CLAC
Ingo Molnardca5b522015-03-24 19:44:42 +0100355 orl $TS_COMPAT, ASM_THREAD_INFO(TI_status, %rsp, SIZEOF_PTREGS)
356 testl $_TIF_WORK_SYSCALL_ENTRY, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Jan Beulich7effaa82005-09-12 18:49:24 +0200357 CFI_REMEMBER_STATE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 jnz cstar_tracesys
H. Peter Anvin36d001c2010-09-14 12:42:41 -0700359 cmpq $IA32_NR_syscalls-1,%rax
Andi Kleen67d53ea2006-04-07 19:50:31 +0200360 ja ia32_badsys
Jan Beulich295286a2008-08-29 13:21:11 +0100361cstar_do_call:
Denys Vlasenko14f6e952015-02-26 14:40:35 -0800362 /* 32bit syscall -> 64bit C ABI argument conversion */
363 movl %edi,%r8d /* arg5 */
364 /* r9 already loaded */ /* arg6 */
365 xchg %ecx,%esi /* rsi:arg2, rcx:arg4 */
366 movl %ebx,%edi /* arg1 */
367 movl %edx,%edx /* arg3 (zero extension) */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700368cstar_dispatch:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 call *ia32_sys_call_table(,%rax,8)
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800370 movq %rax,RAX(%rsp)
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -0400371 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700372 TRACE_IRQS_OFF
Ingo Molnardca5b522015-03-24 19:44:42 +0100373 testl $_TIF_ALLWORK_MASK, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Roland McGrath5cbf1562008-06-24 01:13:31 -0700374 jnz sysretl_audit
375sysretl_from_sys_call:
Ingo Molnardca5b522015-03-24 19:44:42 +0100376 andl $~TS_COMPAT, ASM_THREAD_INFO(TI_status, %rsp, SIZEOF_PTREGS)
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800377 RESTORE_RSI_RDI_RDX
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800378 movl RIP(%rsp),%ecx
Jan Beulich7effaa82005-09-12 18:49:24 +0200379 CFI_REGISTER rip,rcx
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800380 movl EFLAGS(%rsp),%r11d
Jan Beulich7effaa82005-09-12 18:49:24 +0200381 /*CFI_REGISTER rflags,r11*/
Jan Beulich24e35802009-09-30 11:22:11 +0100382 xorq %r10,%r10
383 xorq %r9,%r9
384 xorq %r8,%r8
Ingo Molnar2601e642006-07-03 00:24:45 -0700385 TRACE_IRQS_ON
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800386 movl RSP(%rsp),%esp
Jan Beulich7effaa82005-09-12 18:49:24 +0200387 CFI_RESTORE rsp
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800388 /*
389 * 64bit->32bit SYSRET restores eip from ecx,
390 * eflags from r11 (but RF and VM bits are forced to 0),
391 * cs and ss are loaded from MSRs.
392 * (Note: 32bit->32bit SYSRET is different: since r11
393 * does not exist, it merely sets eflags.IF=1).
394 */
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -0400395 USERGS_SYSRET32
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800396
Roland McGrath5cbf1562008-06-24 01:13:31 -0700397#ifdef CONFIG_AUDITSYSCALL
398cstar_auditsys:
Jan Beulich7effaa82005-09-12 18:49:24 +0200399 CFI_RESTORE_STATE
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800400 movl %r9d,R9(%rsp) /* register to be clobbered by call */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700401 auditsys_entry_common
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800402 movl R9(%rsp),%r9d /* reload 6th syscall arg */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700403 jmp cstar_dispatch
404
405sysretl_audit:
Jan Beulich81766742009-10-26 15:20:29 +0000406 auditsys_exit sysretl_from_sys_call
Roland McGrath5cbf1562008-06-24 01:13:31 -0700407#endif
408
409cstar_tracesys:
410#ifdef CONFIG_AUDITSYSCALL
Ingo Molnardca5b522015-03-24 19:44:42 +0100411 testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT), ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Roland McGrath5cbf1562008-06-24 01:13:31 -0700412 jz cstar_auditsys
413#endif
Chuck Ebbertecd744e2007-11-07 10:48:39 -0500414 xchgl %r9d,%ebp
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800415 SAVE_EXTRA_REGS
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800416 CLEAR_RREGS r9
Roland McGrath48ee6792008-03-18 18:23:50 -0700417 movq $-ENOSYS,RAX(%rsp) /* ptrace can change this for a bad syscall */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 movq %rsp,%rdi /* &pt_regs -> arg1 */
419 call syscall_trace_enter
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800420 LOAD_ARGS32 1 /* reload args from stack in case ptrace changed it */
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800421 RESTORE_EXTRA_REGS
Chuck Ebbertecd744e2007-11-07 10:48:39 -0500422 xchgl %ebp,%r9d
H. Peter Anvin36d001c2010-09-14 12:42:41 -0700423 cmpq $(IA32_NR_syscalls-1),%rax
Roland McGrath48ee6792008-03-18 18:23:50 -0700424 ja int_ret_from_sys_call /* cstar_tracesys has set RAX(%rsp) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 jmp cstar_do_call
Jan Beulich4b787e02006-06-26 13:56:55 +0200426END(ia32_cstar_target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428ia32_badarg:
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700429 ASM_CLAC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 movq $-EFAULT,%rax
431 jmp ia32_sysret
432 CFI_ENDPROC
433
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800434/*
435 * Emulated IA32 system calls via int 0x80.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 *
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800437 * Arguments:
438 * eax system call number
439 * ebx arg1
440 * ecx arg2
441 * edx arg3
442 * esi arg4
443 * edi arg5
444 * ebp arg6 (note: not saved in the stack frame, should not be touched)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 *
446 * Notes:
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800447 * Uses the same stack frame as the x86-64 version.
448 * All registers except eax must be saved (but ptrace may violate that).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 * Arguments are zero extended. For system calls that want sign extension and
450 * take long arguments a wrapper is needed. Most calls can just be called
451 * directly.
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800452 * Assumes it is only called from user space and entered with interrupts off.
453 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455ENTRY(ia32_syscall)
Jan Beulichcdc17932007-07-21 17:10:20 +0200456 CFI_STARTPROC32 simple
Jan Beulichadf14232006-09-26 10:52:41 +0200457 CFI_SIGNAL_FRAME
Denys Vlasenkoa232e3d2015-03-27 11:36:20 +0100458 CFI_DEF_CFA rsp,5*8
459 /*CFI_REL_OFFSET ss,4*8 */
460 CFI_REL_OFFSET rsp,3*8
461 /*CFI_REL_OFFSET rflags,2*8 */
462 /*CFI_REL_OFFSET cs,1*8 */
463 CFI_REL_OFFSET rip,0*8
464
465 /*
466 * Interrupts are off on entry.
467 * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
468 * it is too small to ever cause noticeable irq latency.
469 */
Jeremy Fitzhardinge360c0442008-07-08 15:06:28 -0700470 PARAVIRT_ADJUST_EXCEPTION_FRAME
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -0400471 SWAPGS
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -0400472 ENABLE_INTERRUPTS(CLBR_NONE)
Denys Vlasenkoa232e3d2015-03-27 11:36:20 +0100473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 movl %eax,%eax
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800475 pushq_cfi %rax /* store orig_ax */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 cld
477 /* note the registers are not zero extended to the sf.
478 this could be a problem. */
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800479 ALLOC_PT_GPREGS_ON_STACK
480 SAVE_C_REGS_EXCEPT_R891011
Ingo Molnardca5b522015-03-24 19:44:42 +0100481 orl $TS_COMPAT, ASM_THREAD_INFO(TI_status, %rsp, SIZEOF_PTREGS)
482 testl $_TIF_WORK_SYSCALL_ENTRY, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 jnz ia32_tracesys
H. Peter Anvin36d001c2010-09-14 12:42:41 -0700484 cmpq $(IA32_NR_syscalls-1),%rax
Roland McGrathc09249f2009-02-06 18:15:18 -0800485 ja ia32_badsys
486ia32_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) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 call *ia32_sys_call_table(,%rax,8) # xxx: rip relative
494ia32_sysret:
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800495 movq %rax,RAX(%rsp)
Jan Beulich24e35802009-09-30 11:22:11 +0100496ia32_ret_from_sys_call:
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800497 CLEAR_RREGS
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800498 jmp int_ret_from_sys_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800500ia32_tracesys:
501 SAVE_EXTRA_REGS
Jan Beulichcdc17932007-07-21 17:10:20 +0200502 CLEAR_RREGS
Roland McGrath8ab32bb2008-03-16 21:57:41 -0700503 movq $-ENOSYS,RAX(%rsp) /* ptrace can change this for a bad syscall */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 movq %rsp,%rdi /* &pt_regs -> arg1 */
505 call syscall_trace_enter
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800506 LOAD_ARGS32 /* reload args from stack in case ptrace changed it */
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800507 RESTORE_EXTRA_REGS
H. Peter Anvin36d001c2010-09-14 12:42:41 -0700508 cmpq $(IA32_NR_syscalls-1),%rax
Roland McGrathc09249f2009-02-06 18:15:18 -0800509 ja int_ret_from_sys_call /* ia32_tracesys has set RAX(%rsp) */
510 jmp ia32_do_call
Jan Beulich4b787e02006-06-26 13:56:55 +0200511END(ia32_syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513ia32_badsys:
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800514 movq $0,ORIG_RAX(%rsp)
Jan Beulich24e35802009-09-30 11:22:11 +0100515 movq $-ENOSYS,%rax
516 jmp ia32_sysret
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 CFI_ENDPROC
519
Ramkumar Ramachandrad2475b82013-07-10 23:34:28 +0530520 .macro PTREGSCALL label, func
Jan Beulichf6b2bc82011-11-29 11:24:10 +0000521 ALIGN
522GLOBAL(\label)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 leaq \func(%rip),%rax
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 jmp ia32_ptregs_common
525 .endm
526
Jan Beulich27651302006-01-11 22:41:59 +0100527 CFI_STARTPROC32
Jan Beulich7effaa82005-09-12 18:49:24 +0200528
Al Viro3fe26fa2012-11-12 14:32:42 -0500529 PTREGSCALL stub32_rt_sigreturn, sys32_rt_sigreturn
530 PTREGSCALL stub32_sigreturn, sys32_sigreturn
531 PTREGSCALL stub32_execve, compat_sys_execve
David Drysdale27d6ec72014-12-12 16:57:33 -0800532 PTREGSCALL stub32_execveat, compat_sys_execveat
Al Viro3fe26fa2012-11-12 14:32:42 -0500533 PTREGSCALL stub32_fork, sys_fork
534 PTREGSCALL stub32_vfork, sys_vfork
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Jan Beulichf6b2bc82011-11-29 11:24:10 +0000536 ALIGN
Al Viro1d4b4b22012-10-22 22:34:11 -0400537GLOBAL(stub32_clone)
538 leaq sys_clone(%rip),%rax
539 mov %r8, %rcx
540 jmp ia32_ptregs_common
541
542 ALIGN
Jan Beulichf6b2bc82011-11-29 11:24:10 +0000543ia32_ptregs_common:
Jan Beulich27651302006-01-11 22:41:59 +0100544 CFI_ENDPROC
545 CFI_STARTPROC32 simple
Jan Beulichadf14232006-09-26 10:52:41 +0200546 CFI_SIGNAL_FRAME
Denys Vlasenkob3ab90b2015-02-26 14:40:37 -0800547 CFI_DEF_CFA rsp,SIZEOF_PTREGS
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800548 CFI_REL_OFFSET rax,RAX
549 CFI_REL_OFFSET rcx,RCX
550 CFI_REL_OFFSET rdx,RDX
551 CFI_REL_OFFSET rsi,RSI
552 CFI_REL_OFFSET rdi,RDI
553 CFI_REL_OFFSET rip,RIP
554/* CFI_REL_OFFSET cs,CS*/
555/* CFI_REL_OFFSET rflags,EFLAGS*/
556 CFI_REL_OFFSET rsp,RSP
557/* CFI_REL_OFFSET ss,SS*/
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800558 SAVE_EXTRA_REGS 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 call *%rax
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800560 RESTORE_EXTRA_REGS 8
561 ret
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200563END(ia32_ptregs_common)