blob: b5670564a1fbed846086fed8ca8936be1cdbebfb [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 .macro IA32_ARG_FIXUP noebp=0
34 movl %edi,%r8d
35 .if \noebp
36 .else
37 movl %ebp,%r9d
38 .endif
39 xchg %ecx,%esi
40 movl %ebx,%edi
41 movl %edx,%edx /* zero extension */
42 .endm
43
Denys Vlasenkof2db9382015-02-26 14:40:30 -080044 /* clobbers %rax */
45 .macro CLEAR_RREGS _r9=rax
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 xorl %eax,%eax
Denys Vlasenkof2db9382015-02-26 14:40:30 -080047 movq %rax,R11(%rsp)
48 movq %rax,R10(%rsp)
49 movq %\_r9,R9(%rsp)
50 movq %rax,R8(%rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 .endm
52
Roland McGrathd4d67152008-07-09 02:38:07 -070053 /*
54 * Reload arg registers from stack in case ptrace changed them.
55 * We don't reload %eax because syscall_trace_enter() returned
Roland McGratheefdca02010-09-14 12:22:58 -070056 * the %rax value we should see. Instead, we just truncate that
57 * value to 32 bits again as we did on entry from user mode.
58 * If it's a new value set by user_regset during entry tracing,
59 * this matches the normal truncation of the user-mode value.
60 * If it's -1 to make us punt the syscall, then (u32)-1 is still
61 * an appropriately invalid value.
Roland McGrathd4d67152008-07-09 02:38:07 -070062 */
Denys Vlasenkof2db9382015-02-26 14:40:30 -080063 .macro LOAD_ARGS32 _r9=0
Jan Beulich295286a2008-08-29 13:21:11 +010064 .if \_r9
Denys Vlasenkof2db9382015-02-26 14:40:30 -080065 movl R9(%rsp),%r9d
Jan Beulich295286a2008-08-29 13:21:11 +010066 .endif
Denys Vlasenkof2db9382015-02-26 14:40:30 -080067 movl RCX(%rsp),%ecx
68 movl RDX(%rsp),%edx
69 movl RSI(%rsp),%esi
70 movl RDI(%rsp),%edi
Roland McGratheefdca02010-09-14 12:22:58 -070071 movl %eax,%eax /* zero extension */
Andi Kleen176df242007-09-21 16:16:18 +020072 .endm
73
Jan Beulich27651302006-01-11 22:41:59 +010074 .macro CFI_STARTPROC32 simple
75 CFI_STARTPROC \simple
76 CFI_UNDEFINED r8
77 CFI_UNDEFINED r9
78 CFI_UNDEFINED r10
79 CFI_UNDEFINED r11
80 CFI_UNDEFINED r12
81 CFI_UNDEFINED r13
82 CFI_UNDEFINED r14
83 CFI_UNDEFINED r15
84 .endm
85
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -040086#ifdef CONFIG_PARAVIRT
87ENTRY(native_usergs_sysret32)
88 swapgs
89 sysretl
90ENDPROC(native_usergs_sysret32)
91
92ENTRY(native_irq_enable_sysexit)
93 swapgs
94 sti
95 sysexit
96ENDPROC(native_irq_enable_sysexit)
97#endif
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/*
100 * 32bit SYSENTER instruction entry.
101 *
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800102 * SYSENTER loads ss, rsp, cs, and rip from previously programmed MSRs.
103 * IF and VM in rflags are cleared (IOW: interrupts are off).
104 * SYSENTER does not save anything on the stack,
105 * and does not save old rip (!!!) and rflags.
106 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 * Arguments:
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800108 * eax system call number
109 * ebx arg1
110 * ecx arg2
111 * edx arg3
112 * esi arg4
113 * edi arg5
114 * ebp user stack
115 * 0(%ebp) arg6
116 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 * This is purely a fast path. For anything complicated we use the int 0x80
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800118 * path below. We set up a complete hardware stack frame to share code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 * with the int 0x80 path.
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800120 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121ENTRY(ia32_sysenter_target)
Jan Beulich27651302006-01-11 22:41:59 +0100122 CFI_STARTPROC32 simple
Jan Beulichadf14232006-09-26 10:52:41 +0200123 CFI_SIGNAL_FRAME
Jan Beulich7effaa82005-09-12 18:49:24 +0200124 CFI_DEF_CFA rsp,0
125 CFI_REGISTER rsp,rbp
Jeremy Fitzhardinge457da702008-06-26 07:28:51 -0700126 SWAPGS_UNSAFE_STACK
Brian Gerst9af45652009-01-19 00:38:58 +0900127 movq PER_CPU_VAR(kernel_stack), %rsp
128 addq $(KERNEL_STACK_OFFSET),%rsp
Ingo Molnar2601e642006-07-03 00:24:45 -0700129 /*
130 * No need to follow this irqs on/off section: the syscall
131 * disabled irqs, here we enable it straight after entry:
132 */
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -0400133 ENABLE_INTERRUPTS(CLBR_NONE)
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800134 /* Construct iret frame (ss,rsp,rflags,cs,rip) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 movl %ebp,%ebp /* zero extension */
Jan Beulich60cf6372011-02-28 15:54:40 +0000136 pushq_cfi $__USER32_DS
Jan Beulich7effaa82005-09-12 18:49:24 +0200137 /*CFI_REL_OFFSET ss,0*/
Jan Beulich60cf6372011-02-28 15:54:40 +0000138 pushq_cfi %rbp
Jan Beulich7effaa82005-09-12 18:49:24 +0200139 CFI_REL_OFFSET rsp,0
Jan Beulich60cf6372011-02-28 15:54:40 +0000140 pushfq_cfi
Jan Beulich7effaa82005-09-12 18:49:24 +0200141 /*CFI_REL_OFFSET rflags,0*/
Jan Beulich46db09d2011-11-29 11:17:45 +0000142 movl TI_sysenter_return+THREAD_INFO(%rsp,3*8-KERNEL_STACK_OFFSET),%r10d
Jan Beulich7effaa82005-09-12 18:49:24 +0200143 CFI_REGISTER rip,r10
Jan Beulich60cf6372011-02-28 15:54:40 +0000144 pushq_cfi $__USER32_CS
Jan Beulich7effaa82005-09-12 18:49:24 +0200145 /*CFI_REL_OFFSET cs,0*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 movl %eax, %eax
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800147 /* Store thread_info->sysenter_return in rip stack slot */
Jan Beulich60cf6372011-02-28 15:54:40 +0000148 pushq_cfi %r10
Jan Beulich7effaa82005-09-12 18:49:24 +0200149 CFI_REL_OFFSET rip,0
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800150 /* Store orig_ax */
Jan Beulich60cf6372011-02-28 15:54:40 +0000151 pushq_cfi %rax
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800152 /* Construct the rest of "struct pt_regs" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 cld
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800154 ALLOC_PT_GPREGS_ON_STACK
155 SAVE_C_REGS_EXCEPT_R891011
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800156 /*
157 * no need to do an access_ok check here because rbp has been
158 * 32bit zero extended
159 */
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700160 ASM_STAC
Jan Beulich295286a2008-08-29 13:21:11 +01001611: movl (%rbp),%ebp
H. Peter Anvin1ce6f862012-04-20 12:19:50 -0700162 _ASM_EXTABLE(1b,ia32_badarg)
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700163 ASM_CLAC
Andy Lutomirski8c7aa692014-10-01 11:49:04 -0700164
165 /*
166 * Sysenter doesn't filter flags, so we need to clear NT
167 * ourselves. To save a few cycles, we can check whether
168 * NT was set instead of doing an unconditional popfq.
169 */
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800170 testl $X86_EFLAGS_NT,EFLAGS(%rsp)
Andy Lutomirski8c7aa692014-10-01 11:49:04 -0700171 jnz sysenter_fix_flags
172sysenter_flags_fixed:
173
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800174 orl $TS_COMPAT,TI_status+THREAD_INFO(%rsp,RIP)
175 testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags+THREAD_INFO(%rsp,RIP)
Jan Beulich7effaa82005-09-12 18:49:24 +0200176 CFI_REMEMBER_STATE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 jnz sysenter_tracesys
H. Peter Anvin36d001c2010-09-14 12:42:41 -0700178 cmpq $(IA32_NR_syscalls-1),%rax
Andi Kleen67d53ea2006-04-07 19:50:31 +0200179 ja ia32_badsys
Roland McGrathd4d67152008-07-09 02:38:07 -0700180sysenter_do_call:
Jan Beulich295286a2008-08-29 13:21:11 +0100181 IA32_ARG_FIXUP
Roland McGrath5cbf1562008-06-24 01:13:31 -0700182sysenter_dispatch:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 call *ia32_sys_call_table(,%rax,8)
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800184 movq %rax,RAX(%rsp)
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -0400185 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700186 TRACE_IRQS_OFF
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800187 testl $_TIF_ALLWORK_MASK,TI_flags+THREAD_INFO(%rsp,RIP)
Roland McGrath5cbf1562008-06-24 01:13:31 -0700188 jnz sysexit_audit
189sysexit_from_sys_call:
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800190 andl $~TS_COMPAT,TI_status+THREAD_INFO(%rsp,RIP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 /* clear IF, that popfq doesn't enable interrupts early */
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800192 andl $~0x200,EFLAGS(%rsp)
193 movl RIP(%rsp),%edx /* User %eip */
Roland McGrath36197c92008-01-30 13:30:43 +0100194 CFI_REGISTER rip,rdx
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800195 RESTORE_RSI_RDI
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800196 /* pop everything except ss,rsp,rflags slots */
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800197 REMOVE_PT_GPREGS_FROM_STACK 3*8
Jan Beulich24e35802009-09-30 11:22:11 +0100198 xorq %r8,%r8
199 xorq %r9,%r9
200 xorq %r10,%r10
201 xorq %r11,%r11
Jan Beulich60cf6372011-02-28 15:54:40 +0000202 popfq_cfi
Jan Beulich7effaa82005-09-12 18:49:24 +0200203 /*CFI_RESTORE rflags*/
Jan Beulich60cf6372011-02-28 15:54:40 +0000204 popq_cfi %rcx /* User %esp */
Jan Beulich7effaa82005-09-12 18:49:24 +0200205 CFI_REGISTER rsp,rcx
Ingo Molnar2601e642006-07-03 00:24:45 -0700206 TRACE_IRQS_ON
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800207 /*
208 * 32bit SYSEXIT restores eip from edx, esp from ecx.
209 * cs and ss are loaded from MSRs.
210 */
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -0400211 ENABLE_INTERRUPTS_SYSEXIT32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Andy Lutomirski8c7aa692014-10-01 11:49:04 -0700213 CFI_RESTORE_STATE
214
Roland McGrath5cbf1562008-06-24 01:13:31 -0700215#ifdef CONFIG_AUDITSYSCALL
216 .macro auditsys_entry_common
Richard Guy Briggsb4f0d372014-03-04 10:38:06 -0500217 movl %esi,%r8d /* 5th arg: 4th syscall arg */
218 movl %ecx,%r9d /*swap with edx*/
219 movl %edx,%ecx /* 4th arg: 3rd syscall arg */
220 movl %r9d,%edx /* 3rd arg: 2nd syscall arg */
221 movl %ebx,%esi /* 2nd arg: 1st syscall arg */
222 movl %eax,%edi /* 1st arg: syscall number */
Eric Parisb05d8442012-01-03 14:23:06 -0500223 call __audit_syscall_entry
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800224 movl RAX(%rsp),%eax /* reload syscall number */
H. Peter Anvin36d001c2010-09-14 12:42:41 -0700225 cmpq $(IA32_NR_syscalls-1),%rax
Roland McGrath5cbf1562008-06-24 01:13:31 -0700226 ja ia32_badsys
227 movl %ebx,%edi /* reload 1st syscall arg */
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800228 movl RCX(%rsp),%esi /* reload 2nd syscall arg */
229 movl RDX(%rsp),%edx /* reload 3rd syscall arg */
230 movl RSI(%rsp),%ecx /* reload 4th syscall arg */
231 movl RDI(%rsp),%r8d /* reload 5th syscall arg */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700232 .endm
233
Jan Beulich81766742009-10-26 15:20:29 +0000234 .macro auditsys_exit exit
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800235 testl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT),TI_flags+THREAD_INFO(%rsp,RIP)
Jan Beulich24e35802009-09-30 11:22:11 +0100236 jnz ia32_ret_from_sys_call
Roland McGrath5cbf1562008-06-24 01:13:31 -0700237 TRACE_IRQS_ON
Jan Beulich40a1ef92013-01-30 07:55:53 +0000238 ENABLE_INTERRUPTS(CLBR_NONE)
Roland McGrath5cbf1562008-06-24 01:13:31 -0700239 movl %eax,%esi /* second arg, syscall return value */
Eric Parisd7e75282012-01-03 14:23:06 -0500240 cmpl $-MAX_ERRNO,%eax /* is it an error ? */
Eric Parisf031cd22012-01-03 14:23:06 -0500241 jbe 1f
242 movslq %eax, %rsi /* if error sign extend to 64 bits */
2431: setbe %al /* 1 if error, 0 if not */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700244 movzbl %al,%edi /* zero-extend that into %edi */
Eric Parisd7e75282012-01-03 14:23:06 -0500245 call __audit_syscall_exit
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800246 movq RAX(%rsp),%rax /* reload syscall return value */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700247 movl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT),%edi
Jan Beulich40a1ef92013-01-30 07:55:53 +0000248 DISABLE_INTERRUPTS(CLBR_NONE)
Roland McGrath5cbf1562008-06-24 01:13:31 -0700249 TRACE_IRQS_OFF
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800250 testl %edi,TI_flags+THREAD_INFO(%rsp,RIP)
Jan Beulich24e35802009-09-30 11:22:11 +0100251 jz \exit
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800252 CLEAR_RREGS
Jan Beulich24e35802009-09-30 11:22:11 +0100253 jmp int_with_check
Roland McGrath5cbf1562008-06-24 01:13:31 -0700254 .endm
255
256sysenter_auditsys:
Roland McGrath5cbf1562008-06-24 01:13:31 -0700257 auditsys_entry_common
258 movl %ebp,%r9d /* reload 6th syscall arg */
259 jmp sysenter_dispatch
260
261sysexit_audit:
262 auditsys_exit sysexit_from_sys_call
263#endif
264
Andy Lutomirski8c7aa692014-10-01 11:49:04 -0700265sysenter_fix_flags:
266 pushq_cfi $(X86_EFLAGS_IF|X86_EFLAGS_FIXED)
267 popfq_cfi
268 jmp sysenter_flags_fixed
269
Roland McGrath5cbf1562008-06-24 01:13:31 -0700270sysenter_tracesys:
Roland McGrath5cbf1562008-06-24 01:13:31 -0700271#ifdef CONFIG_AUDITSYSCALL
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800272 testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT),TI_flags+THREAD_INFO(%rsp,RIP)
Roland McGrath5cbf1562008-06-24 01:13:31 -0700273 jz sysenter_auditsys
274#endif
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800275 SAVE_EXTRA_REGS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 CLEAR_RREGS
Roland McGrath48ee6792008-03-18 18:23:50 -0700277 movq $-ENOSYS,RAX(%rsp)/* ptrace can change this for a bad syscall */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 movq %rsp,%rdi /* &pt_regs -> arg1 */
279 call syscall_trace_enter
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800280 LOAD_ARGS32 /* reload args from stack in case ptrace changed it */
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800281 RESTORE_EXTRA_REGS
H. Peter Anvin36d001c2010-09-14 12:42:41 -0700282 cmpq $(IA32_NR_syscalls-1),%rax
Roland McGrath48ee6792008-03-18 18:23:50 -0700283 ja int_ret_from_sys_call /* sysenter_tracesys has set RAX(%rsp) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 jmp sysenter_do_call
285 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200286ENDPROC(ia32_sysenter_target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288/*
289 * 32bit SYSCALL instruction entry.
290 *
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800291 * 32bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11,
292 * then loads new ss, cs, and rip from previously programmed MSRs.
293 * rflags gets masked by a value from another MSR (so CLD and CLAC
294 * are not needed). SYSCALL does not save anything on the stack
295 * and does not change rsp.
296 *
297 * Note: rflags saving+masking-with-MSR happens only in Long mode
298 * (in legacy 32bit mode, IF, RF and VM bits are cleared and that's it).
299 * Don't get confused: rflags saving+masking depends on Long Mode Active bit
300 * (EFER.LMA=1), NOT on bitness of userspace where SYSCALL executes
301 * or target CS descriptor's L bit (SYSCALL does not read segment descriptors).
302 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 * Arguments:
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800304 * eax system call number
305 * ecx return address
306 * ebx arg1
307 * ebp arg2 (note: not saved in the stack frame, should not be touched)
308 * edx arg3
309 * esi arg4
310 * edi arg5
311 * esp user stack
312 * 0(%esp) arg6
313 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 * This is purely a fast path. For anything complicated we use the int 0x80
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800315 * path below. We set up a complete hardware stack frame to share code
316 * with the int 0x80 path.
317 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318ENTRY(ia32_cstar_target)
Jan Beulich27651302006-01-11 22:41:59 +0100319 CFI_STARTPROC32 simple
Jan Beulichadf14232006-09-26 10:52:41 +0200320 CFI_SIGNAL_FRAME
Brian Gerst9af45652009-01-19 00:38:58 +0900321 CFI_DEF_CFA rsp,KERNEL_STACK_OFFSET
Jan Beulich7effaa82005-09-12 18:49:24 +0200322 CFI_REGISTER rip,rcx
323 /*CFI_REGISTER rflags,r11*/
Jeremy Fitzhardinge457da702008-06-26 07:28:51 -0700324 SWAPGS_UNSAFE_STACK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 movl %esp,%r8d
Jan Beulich7effaa82005-09-12 18:49:24 +0200326 CFI_REGISTER rsp,r8
Brian Gerst9af45652009-01-19 00:38:58 +0900327 movq PER_CPU_VAR(kernel_stack),%rsp
Ingo Molnar2601e642006-07-03 00:24:45 -0700328 /*
329 * No need to follow this irqs on/off section: the syscall
330 * disabled irqs and here we enable it straight after entry:
331 */
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -0400332 ENABLE_INTERRUPTS(CLBR_NONE)
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800333 ALLOC_PT_GPREGS_ON_STACK 8 /* +8: space for orig_ax */
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800334 SAVE_C_REGS_EXCEPT_RCX_R891011
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 movl %eax,%eax /* zero extension */
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800336 movq %rax,ORIG_RAX(%rsp)
337 movq %rcx,RIP(%rsp)
338 CFI_REL_OFFSET rip,RIP
339 movq %rbp,RCX(%rsp) /* this lies slightly to ptrace */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 movl %ebp,%ecx
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800341 movq $__USER32_CS,CS(%rsp)
342 movq $__USER32_DS,SS(%rsp)
343 movq %r11,EFLAGS(%rsp)
344 /*CFI_REL_OFFSET rflags,EFLAGS*/
345 movq %r8,RSP(%rsp)
346 CFI_REL_OFFSET rsp,RSP
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800347 /* iret stack frame is complete now */
348 /*
349 * no need to do an access_ok check here because r8 has been
350 * 32bit zero extended
351 */
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700352 ASM_STAC
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531: movl (%r8),%r9d
H. Peter Anvina3e859f2012-04-20 16:51:50 -0700354 _ASM_EXTABLE(1b,ia32_badarg)
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700355 ASM_CLAC
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800356 orl $TS_COMPAT,TI_status+THREAD_INFO(%rsp,RIP)
357 testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags+THREAD_INFO(%rsp,RIP)
Jan Beulich7effaa82005-09-12 18:49:24 +0200358 CFI_REMEMBER_STATE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 jnz cstar_tracesys
H. Peter Anvin36d001c2010-09-14 12:42:41 -0700360 cmpq $IA32_NR_syscalls-1,%rax
Andi Kleen67d53ea2006-04-07 19:50:31 +0200361 ja ia32_badsys
Jan Beulich295286a2008-08-29 13:21:11 +0100362cstar_do_call:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 IA32_ARG_FIXUP 1
Roland McGrath5cbf1562008-06-24 01:13:31 -0700364cstar_dispatch:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 call *ia32_sys_call_table(,%rax,8)
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800366 movq %rax,RAX(%rsp)
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -0400367 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700368 TRACE_IRQS_OFF
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800369 testl $_TIF_ALLWORK_MASK,TI_flags+THREAD_INFO(%rsp,RIP)
Roland McGrath5cbf1562008-06-24 01:13:31 -0700370 jnz sysretl_audit
371sysretl_from_sys_call:
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800372 andl $~TS_COMPAT,TI_status+THREAD_INFO(%rsp,RIP)
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800373 RESTORE_RSI_RDI_RDX
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800374 movl RIP(%rsp),%ecx
Jan Beulich7effaa82005-09-12 18:49:24 +0200375 CFI_REGISTER rip,rcx
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800376 movl EFLAGS(%rsp),%r11d
Jan Beulich7effaa82005-09-12 18:49:24 +0200377 /*CFI_REGISTER rflags,r11*/
Jan Beulich24e35802009-09-30 11:22:11 +0100378 xorq %r10,%r10
379 xorq %r9,%r9
380 xorq %r8,%r8
Ingo Molnar2601e642006-07-03 00:24:45 -0700381 TRACE_IRQS_ON
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800382 movl RSP(%rsp),%esp
Jan Beulich7effaa82005-09-12 18:49:24 +0200383 CFI_RESTORE rsp
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800384 /*
385 * 64bit->32bit SYSRET restores eip from ecx,
386 * eflags from r11 (but RF and VM bits are forced to 0),
387 * cs and ss are loaded from MSRs.
388 * (Note: 32bit->32bit SYSRET is different: since r11
389 * does not exist, it merely sets eflags.IF=1).
390 */
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -0400391 USERGS_SYSRET32
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800392
Roland McGrath5cbf1562008-06-24 01:13:31 -0700393#ifdef CONFIG_AUDITSYSCALL
394cstar_auditsys:
Jan Beulich7effaa82005-09-12 18:49:24 +0200395 CFI_RESTORE_STATE
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800396 movl %r9d,R9(%rsp) /* register to be clobbered by call */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700397 auditsys_entry_common
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800398 movl R9(%rsp),%r9d /* reload 6th syscall arg */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700399 jmp cstar_dispatch
400
401sysretl_audit:
Jan Beulich81766742009-10-26 15:20:29 +0000402 auditsys_exit sysretl_from_sys_call
Roland McGrath5cbf1562008-06-24 01:13:31 -0700403#endif
404
405cstar_tracesys:
406#ifdef CONFIG_AUDITSYSCALL
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800407 testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT),TI_flags+THREAD_INFO(%rsp,RIP)
Roland McGrath5cbf1562008-06-24 01:13:31 -0700408 jz cstar_auditsys
409#endif
Chuck Ebbertecd744e2007-11-07 10:48:39 -0500410 xchgl %r9d,%ebp
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800411 SAVE_EXTRA_REGS
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800412 CLEAR_RREGS r9
Roland McGrath48ee6792008-03-18 18:23:50 -0700413 movq $-ENOSYS,RAX(%rsp) /* ptrace can change this for a bad syscall */
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
H. Peter Anvin36d001c2010-09-14 12:42:41 -0700419 cmpq $(IA32_NR_syscalls-1),%rax
Roland McGrath48ee6792008-03-18 18:23:50 -0700420 ja int_ret_from_sys_call /* cstar_tracesys has set RAX(%rsp) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 jmp cstar_do_call
Jan Beulich4b787e02006-06-26 13:56:55 +0200422END(ia32_cstar_target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424ia32_badarg:
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700425 ASM_CLAC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 movq $-EFAULT,%rax
427 jmp ia32_sysret
428 CFI_ENDPROC
429
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800430/*
431 * Emulated IA32 system calls via int 0x80.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 *
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800433 * Arguments:
434 * eax system call number
435 * ebx arg1
436 * ecx arg2
437 * edx arg3
438 * esi arg4
439 * edi arg5
440 * ebp arg6 (note: not saved in the stack frame, should not be touched)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 *
442 * Notes:
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800443 * Uses the same stack frame as the x86-64 version.
444 * All registers except eax must be saved (but ptrace may violate that).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 * Arguments are zero extended. For system calls that want sign extension and
446 * take long arguments a wrapper is needed. Most calls can just be called
447 * directly.
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800448 * Assumes it is only called from user space and entered with interrupts off.
449 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451ENTRY(ia32_syscall)
Jan Beulichcdc17932007-07-21 17:10:20 +0200452 CFI_STARTPROC32 simple
Jan Beulichadf14232006-09-26 10:52:41 +0200453 CFI_SIGNAL_FRAME
Jan Beulich7effaa82005-09-12 18:49:24 +0200454 CFI_DEF_CFA rsp,SS+8-RIP
455 /*CFI_REL_OFFSET ss,SS-RIP*/
456 CFI_REL_OFFSET rsp,RSP-RIP
457 /*CFI_REL_OFFSET rflags,EFLAGS-RIP*/
458 /*CFI_REL_OFFSET cs,CS-RIP*/
459 CFI_REL_OFFSET rip,RIP-RIP
Jeremy Fitzhardinge360c0442008-07-08 15:06:28 -0700460 PARAVIRT_ADJUST_EXCEPTION_FRAME
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -0400461 SWAPGS
Ingo Molnar2601e642006-07-03 00:24:45 -0700462 /*
463 * No need to follow this irqs on/off section: the syscall
464 * disabled irqs and here we enable it straight after entry:
465 */
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -0400466 ENABLE_INTERRUPTS(CLBR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 movl %eax,%eax
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800468 pushq_cfi %rax /* store orig_ax */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 cld
470 /* note the registers are not zero extended to the sf.
471 this could be a problem. */
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800472 ALLOC_PT_GPREGS_ON_STACK
473 SAVE_C_REGS_EXCEPT_R891011
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800474 orl $TS_COMPAT,TI_status+THREAD_INFO(%rsp,RIP)
475 testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags+THREAD_INFO(%rsp,RIP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 jnz ia32_tracesys
H. Peter Anvin36d001c2010-09-14 12:42:41 -0700477 cmpq $(IA32_NR_syscalls-1),%rax
Roland McGrathc09249f2009-02-06 18:15:18 -0800478 ja ia32_badsys
479ia32_do_call:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 IA32_ARG_FIXUP
481 call *ia32_sys_call_table(,%rax,8) # xxx: rip relative
482ia32_sysret:
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800483 movq %rax,RAX(%rsp)
Jan Beulich24e35802009-09-30 11:22:11 +0100484ia32_ret_from_sys_call:
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800485 CLEAR_RREGS
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800486 jmp int_ret_from_sys_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800488ia32_tracesys:
489 SAVE_EXTRA_REGS
Jan Beulichcdc17932007-07-21 17:10:20 +0200490 CLEAR_RREGS
Roland McGrath8ab32bb2008-03-16 21:57:41 -0700491 movq $-ENOSYS,RAX(%rsp) /* ptrace can change this for a bad syscall */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 movq %rsp,%rdi /* &pt_regs -> arg1 */
493 call syscall_trace_enter
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800494 LOAD_ARGS32 /* reload args from stack in case ptrace changed it */
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800495 RESTORE_EXTRA_REGS
H. Peter Anvin36d001c2010-09-14 12:42:41 -0700496 cmpq $(IA32_NR_syscalls-1),%rax
Roland McGrathc09249f2009-02-06 18:15:18 -0800497 ja int_ret_from_sys_call /* ia32_tracesys has set RAX(%rsp) */
498 jmp ia32_do_call
Jan Beulich4b787e02006-06-26 13:56:55 +0200499END(ia32_syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501ia32_badsys:
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800502 movq $0,ORIG_RAX(%rsp)
Jan Beulich24e35802009-09-30 11:22:11 +0100503 movq $-ENOSYS,%rax
504 jmp ia32_sysret
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 CFI_ENDPROC
507
Ramkumar Ramachandrad2475b82013-07-10 23:34:28 +0530508 .macro PTREGSCALL label, func
Jan Beulichf6b2bc82011-11-29 11:24:10 +0000509 ALIGN
510GLOBAL(\label)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 leaq \func(%rip),%rax
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 jmp ia32_ptregs_common
513 .endm
514
Jan Beulich27651302006-01-11 22:41:59 +0100515 CFI_STARTPROC32
Jan Beulich7effaa82005-09-12 18:49:24 +0200516
Al Viro3fe26fa2012-11-12 14:32:42 -0500517 PTREGSCALL stub32_rt_sigreturn, sys32_rt_sigreturn
518 PTREGSCALL stub32_sigreturn, sys32_sigreturn
519 PTREGSCALL stub32_execve, compat_sys_execve
David Drysdale27d6ec72014-12-12 16:57:33 -0800520 PTREGSCALL stub32_execveat, compat_sys_execveat
Al Viro3fe26fa2012-11-12 14:32:42 -0500521 PTREGSCALL stub32_fork, sys_fork
522 PTREGSCALL stub32_vfork, sys_vfork
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Jan Beulichf6b2bc82011-11-29 11:24:10 +0000524 ALIGN
Al Viro1d4b4b22012-10-22 22:34:11 -0400525GLOBAL(stub32_clone)
526 leaq sys_clone(%rip),%rax
527 mov %r8, %rcx
528 jmp ia32_ptregs_common
529
530 ALIGN
Jan Beulichf6b2bc82011-11-29 11:24:10 +0000531ia32_ptregs_common:
Jan Beulich27651302006-01-11 22:41:59 +0100532 CFI_ENDPROC
533 CFI_STARTPROC32 simple
Jan Beulichadf14232006-09-26 10:52:41 +0200534 CFI_SIGNAL_FRAME
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800535 CFI_DEF_CFA rsp,SS+8
536 CFI_REL_OFFSET rax,RAX
537 CFI_REL_OFFSET rcx,RCX
538 CFI_REL_OFFSET rdx,RDX
539 CFI_REL_OFFSET rsi,RSI
540 CFI_REL_OFFSET rdi,RDI
541 CFI_REL_OFFSET rip,RIP
542/* CFI_REL_OFFSET cs,CS*/
543/* CFI_REL_OFFSET rflags,EFLAGS*/
544 CFI_REL_OFFSET rsp,RSP
545/* CFI_REL_OFFSET ss,SS*/
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800546 SAVE_EXTRA_REGS 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 call *%rax
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800548 RESTORE_EXTRA_REGS 8
549 ret
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200551END(ia32_ptregs_common)