blob: 72bf2680f819ad06d8c0461f6ad7ac6d12042166 [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 Vlasenko4ee8ec12015-03-27 11:36:21 +0100125 /* Zero-extending 32-bit regs, do not remove */
126 movl %ebp, %ebp
127 movl %eax, %eax
128
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +0200129 movl ASM_THREAD_INFO(TI_sysenter_return, %rsp, 0), %r10d
Jan Beulich7effaa82005-09-12 18:49:24 +0200130 CFI_REGISTER rip,r10
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +0200131
132 /* Construct struct pt_regs on stack */
133 pushq_cfi $__USER32_DS /* pt_regs->ss */
134 pushq_cfi %rbp /* pt_regs->sp */
135 CFI_REL_OFFSET rsp,0
136 pushfq_cfi /* pt_regs->flags */
137 pushq_cfi $__USER32_CS /* pt_regs->cs */
138 pushq_cfi %r10 /* pt_regs->ip = thread_info->sysenter_return */
139 CFI_REL_OFFSET rip,0
140 pushq_cfi_reg rax /* pt_regs->orig_ax */
141 pushq_cfi_reg rdi /* pt_regs->di */
142 pushq_cfi_reg rsi /* pt_regs->si */
143 pushq_cfi_reg rdx /* pt_regs->dx */
144 pushq_cfi_reg rcx /* pt_regs->cx */
145 pushq_cfi_reg rax /* pt_regs->ax */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 cld
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +0200147 sub $(10*8),%rsp /* pt_regs->r8-11,bp,bx,r12-15 not saved */
148 CFI_ADJUST_CFA_OFFSET 10*8
149
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800150 /*
151 * no need to do an access_ok check here because rbp has been
152 * 32bit zero extended
153 */
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700154 ASM_STAC
Jan Beulich295286a2008-08-29 13:21:11 +01001551: movl (%rbp),%ebp
H. Peter Anvin1ce6f862012-04-20 12:19:50 -0700156 _ASM_EXTABLE(1b,ia32_badarg)
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700157 ASM_CLAC
Andy Lutomirski8c7aa692014-10-01 11:49:04 -0700158
159 /*
160 * Sysenter doesn't filter flags, so we need to clear NT
161 * ourselves. To save a few cycles, we can check whether
162 * NT was set instead of doing an unconditional popfq.
163 */
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800164 testl $X86_EFLAGS_NT,EFLAGS(%rsp)
Andy Lutomirski8c7aa692014-10-01 11:49:04 -0700165 jnz sysenter_fix_flags
166sysenter_flags_fixed:
167
Ingo Molnardca5b522015-03-24 19:44:42 +0100168 orl $TS_COMPAT, ASM_THREAD_INFO(TI_status, %rsp, SIZEOF_PTREGS)
169 testl $_TIF_WORK_SYSCALL_ENTRY, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Jan Beulich7effaa82005-09-12 18:49:24 +0200170 CFI_REMEMBER_STATE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 jnz sysenter_tracesys
H. Peter Anvin36d001c2010-09-14 12:42:41 -0700172 cmpq $(IA32_NR_syscalls-1),%rax
Andi Kleen67d53ea2006-04-07 19:50:31 +0200173 ja ia32_badsys
Roland McGrathd4d67152008-07-09 02:38:07 -0700174sysenter_do_call:
Denys Vlasenko14f6e952015-02-26 14:40:35 -0800175 /* 32bit syscall -> 64bit C ABI argument conversion */
176 movl %edi,%r8d /* arg5 */
177 movl %ebp,%r9d /* arg6 */
178 xchg %ecx,%esi /* rsi:arg2, rcx:arg4 */
179 movl %ebx,%edi /* arg1 */
180 movl %edx,%edx /* arg3 (zero extension) */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700181sysenter_dispatch:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 call *ia32_sys_call_table(,%rax,8)
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800183 movq %rax,RAX(%rsp)
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -0400184 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700185 TRACE_IRQS_OFF
Ingo Molnardca5b522015-03-24 19:44:42 +0100186 testl $_TIF_ALLWORK_MASK, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Roland McGrath5cbf1562008-06-24 01:13:31 -0700187 jnz sysexit_audit
188sysexit_from_sys_call:
Andy Lutomirski4214a162015-04-02 17:12:12 -0700189 /*
190 * NB: SYSEXIT is not obviously safe for 64-bit kernels -- an
191 * NMI between STI and SYSEXIT has poorly specified behavior,
192 * and and NMI followed by an IRQ with usergs is fatal. So
193 * we just pretend we're using SYSEXIT but we really use
194 * SYSRETL instead.
195 *
196 * This code path is still called 'sysexit' because it pairs
197 * with 'sysenter' and it uses the SYSENTER calling convention.
198 */
Ingo Molnardca5b522015-03-24 19:44:42 +0100199 andl $~TS_COMPAT,ASM_THREAD_INFO(TI_status, %rsp, SIZEOF_PTREGS)
Andy Lutomirski4214a162015-04-02 17:12:12 -0700200 movl RIP(%rsp),%ecx /* User %eip */
201 CFI_REGISTER rip,rcx
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800202 RESTORE_RSI_RDI
Andy Lutomirski4214a162015-04-02 17:12:12 -0700203 xorl %edx,%edx /* avoid info leaks */
Jan Beulich24e35802009-09-30 11:22:11 +0100204 xorq %r8,%r8
205 xorq %r9,%r9
206 xorq %r10,%r10
Andy Lutomirski4214a162015-04-02 17:12:12 -0700207 movl EFLAGS(%rsp),%r11d /* User eflags */
Jan Beulich7effaa82005-09-12 18:49:24 +0200208 /*CFI_RESTORE rflags*/
Ingo Molnar2601e642006-07-03 00:24:45 -0700209 TRACE_IRQS_ON
Andy Lutomirski4214a162015-04-02 17:12:12 -0700210
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800211 /*
Andy Lutomirski4214a162015-04-02 17:12:12 -0700212 * SYSRETL works even on Intel CPUs. Use it in preference to SYSEXIT,
213 * since it avoids a dicey window with interrupts enabled.
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800214 */
Andy Lutomirski4214a162015-04-02 17:12:12 -0700215 movl RSP(%rsp),%esp
216
217 /*
218 * USERGS_SYSRET32 does:
219 * gsbase = user's gs base
220 * eip = ecx
221 * rflags = r11
222 * cs = __USER32_CS
223 * ss = __USER_DS
224 *
225 * The prologue set RIP(%rsp) to VDSO32_SYSENTER_RETURN, which does:
226 *
227 * pop %ebp
228 * pop %edx
229 * pop %ecx
230 *
231 * Therefore, we invoke SYSRETL with EDX and R8-R10 zeroed to
232 * avoid info leaks. R11 ends up with VDSO32_SYSENTER_RETURN's
233 * address (already known to user code), and R12-R15 are
234 * callee-saved and therefore don't contain any interesting
235 * kernel data.
236 */
237 USERGS_SYSRET32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Andy Lutomirski8c7aa692014-10-01 11:49:04 -0700239 CFI_RESTORE_STATE
240
Roland McGrath5cbf1562008-06-24 01:13:31 -0700241#ifdef CONFIG_AUDITSYSCALL
242 .macro auditsys_entry_common
Richard Guy Briggsb4f0d372014-03-04 10:38:06 -0500243 movl %esi,%r8d /* 5th arg: 4th syscall arg */
244 movl %ecx,%r9d /*swap with edx*/
245 movl %edx,%ecx /* 4th arg: 3rd syscall arg */
246 movl %r9d,%edx /* 3rd arg: 2nd syscall arg */
247 movl %ebx,%esi /* 2nd arg: 1st syscall arg */
248 movl %eax,%edi /* 1st arg: syscall number */
Eric Parisb05d8442012-01-03 14:23:06 -0500249 call __audit_syscall_entry
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800250 movl RAX(%rsp),%eax /* reload syscall number */
H. Peter Anvin36d001c2010-09-14 12:42:41 -0700251 cmpq $(IA32_NR_syscalls-1),%rax
Roland McGrath5cbf1562008-06-24 01:13:31 -0700252 ja ia32_badsys
253 movl %ebx,%edi /* reload 1st syscall arg */
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800254 movl RCX(%rsp),%esi /* reload 2nd syscall arg */
255 movl RDX(%rsp),%edx /* reload 3rd syscall arg */
256 movl RSI(%rsp),%ecx /* reload 4th syscall arg */
257 movl RDI(%rsp),%r8d /* reload 5th syscall arg */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700258 .endm
259
Jan Beulich81766742009-10-26 15:20:29 +0000260 .macro auditsys_exit exit
Ingo Molnardca5b522015-03-24 19:44:42 +0100261 testl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT), ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Jan Beulich24e35802009-09-30 11:22:11 +0100262 jnz ia32_ret_from_sys_call
Roland McGrath5cbf1562008-06-24 01:13:31 -0700263 TRACE_IRQS_ON
Jan Beulich40a1ef92013-01-30 07:55:53 +0000264 ENABLE_INTERRUPTS(CLBR_NONE)
Roland McGrath5cbf1562008-06-24 01:13:31 -0700265 movl %eax,%esi /* second arg, syscall return value */
Eric Parisd7e75282012-01-03 14:23:06 -0500266 cmpl $-MAX_ERRNO,%eax /* is it an error ? */
Eric Parisf031cd22012-01-03 14:23:06 -0500267 jbe 1f
268 movslq %eax, %rsi /* if error sign extend to 64 bits */
2691: setbe %al /* 1 if error, 0 if not */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700270 movzbl %al,%edi /* zero-extend that into %edi */
Eric Parisd7e75282012-01-03 14:23:06 -0500271 call __audit_syscall_exit
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800272 movq RAX(%rsp),%rax /* reload syscall return value */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700273 movl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT),%edi
Jan Beulich40a1ef92013-01-30 07:55:53 +0000274 DISABLE_INTERRUPTS(CLBR_NONE)
Roland McGrath5cbf1562008-06-24 01:13:31 -0700275 TRACE_IRQS_OFF
Ingo Molnardca5b522015-03-24 19:44:42 +0100276 testl %edi, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Jan Beulich24e35802009-09-30 11:22:11 +0100277 jz \exit
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800278 CLEAR_RREGS
Jan Beulich24e35802009-09-30 11:22:11 +0100279 jmp int_with_check
Roland McGrath5cbf1562008-06-24 01:13:31 -0700280 .endm
281
282sysenter_auditsys:
Roland McGrath5cbf1562008-06-24 01:13:31 -0700283 auditsys_entry_common
284 movl %ebp,%r9d /* reload 6th syscall arg */
285 jmp sysenter_dispatch
286
287sysexit_audit:
288 auditsys_exit sysexit_from_sys_call
289#endif
290
Andy Lutomirski8c7aa692014-10-01 11:49:04 -0700291sysenter_fix_flags:
292 pushq_cfi $(X86_EFLAGS_IF|X86_EFLAGS_FIXED)
293 popfq_cfi
294 jmp sysenter_flags_fixed
295
Roland McGrath5cbf1562008-06-24 01:13:31 -0700296sysenter_tracesys:
Roland McGrath5cbf1562008-06-24 01:13:31 -0700297#ifdef CONFIG_AUDITSYSCALL
Ingo Molnardca5b522015-03-24 19:44:42 +0100298 testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT), ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Roland McGrath5cbf1562008-06-24 01:13:31 -0700299 jz sysenter_auditsys
300#endif
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800301 SAVE_EXTRA_REGS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 CLEAR_RREGS
Roland McGrath48ee6792008-03-18 18:23:50 -0700303 movq $-ENOSYS,RAX(%rsp)/* ptrace can change this for a bad syscall */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 movq %rsp,%rdi /* &pt_regs -> arg1 */
305 call syscall_trace_enter
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800306 LOAD_ARGS32 /* reload args from stack in case ptrace changed it */
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800307 RESTORE_EXTRA_REGS
H. Peter Anvin36d001c2010-09-14 12:42:41 -0700308 cmpq $(IA32_NR_syscalls-1),%rax
Roland McGrath48ee6792008-03-18 18:23:50 -0700309 ja int_ret_from_sys_call /* sysenter_tracesys has set RAX(%rsp) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 jmp sysenter_do_call
311 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200312ENDPROC(ia32_sysenter_target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314/*
315 * 32bit SYSCALL instruction entry.
316 *
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800317 * 32bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11,
318 * then loads new ss, cs, and rip from previously programmed MSRs.
319 * rflags gets masked by a value from another MSR (so CLD and CLAC
320 * are not needed). SYSCALL does not save anything on the stack
321 * and does not change rsp.
322 *
323 * Note: rflags saving+masking-with-MSR happens only in Long mode
324 * (in legacy 32bit mode, IF, RF and VM bits are cleared and that's it).
325 * Don't get confused: rflags saving+masking depends on Long Mode Active bit
326 * (EFER.LMA=1), NOT on bitness of userspace where SYSCALL executes
327 * or target CS descriptor's L bit (SYSCALL does not read segment descriptors).
328 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 * Arguments:
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800330 * eax system call number
331 * ecx return address
332 * ebx arg1
333 * ebp arg2 (note: not saved in the stack frame, should not be touched)
334 * edx arg3
335 * esi arg4
336 * edi arg5
337 * esp user stack
338 * 0(%esp) arg6
339 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 * This is purely a fast path. For anything complicated we use the int 0x80
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800341 * path below. We set up a complete hardware stack frame to share code
342 * with the int 0x80 path.
343 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344ENTRY(ia32_cstar_target)
Jan Beulich27651302006-01-11 22:41:59 +0100345 CFI_STARTPROC32 simple
Jan Beulichadf14232006-09-26 10:52:41 +0200346 CFI_SIGNAL_FRAME
Denys Vlasenkoef593262015-03-19 18:17:46 +0100347 CFI_DEF_CFA rsp,0
Jan Beulich7effaa82005-09-12 18:49:24 +0200348 CFI_REGISTER rip,rcx
349 /*CFI_REGISTER rflags,r11*/
Denys Vlasenkoa232e3d2015-03-27 11:36:20 +0100350
351 /*
352 * Interrupts are off on entry.
353 * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
354 * it is too small to ever cause noticeable irq latency.
355 */
Jeremy Fitzhardinge457da702008-06-26 07:28:51 -0700356 SWAPGS_UNSAFE_STACK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 movl %esp,%r8d
Jan Beulich7effaa82005-09-12 18:49:24 +0200358 CFI_REGISTER rsp,r8
Brian Gerst9af45652009-01-19 00:38:58 +0900359 movq PER_CPU_VAR(kernel_stack),%rsp
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -0400360 ENABLE_INTERRUPTS(CLBR_NONE)
Denys Vlasenkoa232e3d2015-03-27 11:36:20 +0100361
Denys Vlasenko4ee8ec12015-03-27 11:36:21 +0100362 /* Zero-extending 32-bit regs, do not remove */
363 movl %eax,%eax
364
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +0200365 /* Construct struct pt_regs on stack */
366 pushq_cfi $__USER32_DS /* pt_regs->ss */
367 pushq_cfi %r8 /* pt_regs->sp */
368 CFI_REL_OFFSET rsp,0
369 pushq_cfi %r11 /* pt_regs->flags */
370 pushq_cfi $__USER32_CS /* pt_regs->cs */
371 pushq_cfi %rcx /* pt_regs->ip */
372 CFI_REL_OFFSET rip,0
373 pushq_cfi_reg rax /* pt_regs->orig_ax */
374 pushq_cfi_reg rdi /* pt_regs->di */
375 pushq_cfi_reg rsi /* pt_regs->si */
376 pushq_cfi_reg rdx /* pt_regs->dx */
377 pushq_cfi_reg rbp /* pt_regs->cx */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 movl %ebp,%ecx
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +0200379 pushq_cfi_reg rax /* pt_regs->ax */
380 sub $(10*8),%rsp /* pt_regs->r8-11,bp,bx,r12-15 not saved */
381 CFI_ADJUST_CFA_OFFSET 10*8
382
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800383 /*
384 * no need to do an access_ok check here because r8 has been
385 * 32bit zero extended
386 */
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700387 ASM_STAC
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881: movl (%r8),%r9d
H. Peter Anvina3e859f2012-04-20 16:51:50 -0700389 _ASM_EXTABLE(1b,ia32_badarg)
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700390 ASM_CLAC
Ingo Molnardca5b522015-03-24 19:44:42 +0100391 orl $TS_COMPAT, ASM_THREAD_INFO(TI_status, %rsp, SIZEOF_PTREGS)
392 testl $_TIF_WORK_SYSCALL_ENTRY, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Jan Beulich7effaa82005-09-12 18:49:24 +0200393 CFI_REMEMBER_STATE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 jnz cstar_tracesys
H. Peter Anvin36d001c2010-09-14 12:42:41 -0700395 cmpq $IA32_NR_syscalls-1,%rax
Andi Kleen67d53ea2006-04-07 19:50:31 +0200396 ja ia32_badsys
Jan Beulich295286a2008-08-29 13:21:11 +0100397cstar_do_call:
Denys Vlasenko14f6e952015-02-26 14:40:35 -0800398 /* 32bit syscall -> 64bit C ABI argument conversion */
399 movl %edi,%r8d /* arg5 */
400 /* r9 already loaded */ /* arg6 */
401 xchg %ecx,%esi /* rsi:arg2, rcx:arg4 */
402 movl %ebx,%edi /* arg1 */
403 movl %edx,%edx /* arg3 (zero extension) */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700404cstar_dispatch:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 call *ia32_sys_call_table(,%rax,8)
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800406 movq %rax,RAX(%rsp)
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -0400407 DISABLE_INTERRUPTS(CLBR_NONE)
Ingo Molnar2601e642006-07-03 00:24:45 -0700408 TRACE_IRQS_OFF
Ingo Molnardca5b522015-03-24 19:44:42 +0100409 testl $_TIF_ALLWORK_MASK, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Roland McGrath5cbf1562008-06-24 01:13:31 -0700410 jnz sysretl_audit
411sysretl_from_sys_call:
Ingo Molnardca5b522015-03-24 19:44:42 +0100412 andl $~TS_COMPAT, ASM_THREAD_INFO(TI_status, %rsp, SIZEOF_PTREGS)
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800413 RESTORE_RSI_RDI_RDX
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800414 movl RIP(%rsp),%ecx
Jan Beulich7effaa82005-09-12 18:49:24 +0200415 CFI_REGISTER rip,rcx
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800416 movl EFLAGS(%rsp),%r11d
Jan Beulich7effaa82005-09-12 18:49:24 +0200417 /*CFI_REGISTER rflags,r11*/
Jan Beulich24e35802009-09-30 11:22:11 +0100418 xorq %r10,%r10
419 xorq %r9,%r9
420 xorq %r8,%r8
Ingo Molnar2601e642006-07-03 00:24:45 -0700421 TRACE_IRQS_ON
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800422 movl RSP(%rsp),%esp
Jan Beulich7effaa82005-09-12 18:49:24 +0200423 CFI_RESTORE rsp
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800424 /*
425 * 64bit->32bit SYSRET restores eip from ecx,
426 * eflags from r11 (but RF and VM bits are forced to 0),
427 * cs and ss are loaded from MSRs.
428 * (Note: 32bit->32bit SYSRET is different: since r11
429 * does not exist, it merely sets eflags.IF=1).
Andy Lutomirski61f01dd2015-04-26 16:47:59 -0700430 *
431 * NB: On AMD CPUs with the X86_BUG_SYSRET_SS_ATTRS bug, the ss
432 * descriptor is not reinitialized. This means that we must
433 * avoid SYSRET with SS == NULL, which could happen if we schedule,
434 * exit the kernel, and re-enter using an interrupt vector. (All
435 * interrupt entries on x86_64 set SS to NULL.) We prevent that
436 * from happening by reloading SS in __switch_to.
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800437 */
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -0400438 USERGS_SYSRET32
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800439
Roland McGrath5cbf1562008-06-24 01:13:31 -0700440#ifdef CONFIG_AUDITSYSCALL
441cstar_auditsys:
Jan Beulich7effaa82005-09-12 18:49:24 +0200442 CFI_RESTORE_STATE
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800443 movl %r9d,R9(%rsp) /* register to be clobbered by call */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700444 auditsys_entry_common
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800445 movl R9(%rsp),%r9d /* reload 6th syscall arg */
Roland McGrath5cbf1562008-06-24 01:13:31 -0700446 jmp cstar_dispatch
447
448sysretl_audit:
Jan Beulich81766742009-10-26 15:20:29 +0000449 auditsys_exit sysretl_from_sys_call
Roland McGrath5cbf1562008-06-24 01:13:31 -0700450#endif
451
452cstar_tracesys:
453#ifdef CONFIG_AUDITSYSCALL
Ingo Molnardca5b522015-03-24 19:44:42 +0100454 testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT), ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Roland McGrath5cbf1562008-06-24 01:13:31 -0700455 jz cstar_auditsys
456#endif
Chuck Ebbertecd744e2007-11-07 10:48:39 -0500457 xchgl %r9d,%ebp
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800458 SAVE_EXTRA_REGS
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800459 CLEAR_RREGS r9
Roland McGrath48ee6792008-03-18 18:23:50 -0700460 movq $-ENOSYS,RAX(%rsp) /* ptrace can change this for a bad syscall */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 movq %rsp,%rdi /* &pt_regs -> arg1 */
462 call syscall_trace_enter
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800463 LOAD_ARGS32 1 /* reload args from stack in case ptrace changed it */
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800464 RESTORE_EXTRA_REGS
Chuck Ebbertecd744e2007-11-07 10:48:39 -0500465 xchgl %ebp,%r9d
H. Peter Anvin36d001c2010-09-14 12:42:41 -0700466 cmpq $(IA32_NR_syscalls-1),%rax
Roland McGrath48ee6792008-03-18 18:23:50 -0700467 ja int_ret_from_sys_call /* cstar_tracesys has set RAX(%rsp) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 jmp cstar_do_call
Jan Beulich4b787e02006-06-26 13:56:55 +0200469END(ia32_cstar_target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471ia32_badarg:
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700472 ASM_CLAC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 movq $-EFAULT,%rax
474 jmp ia32_sysret
475 CFI_ENDPROC
476
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800477/*
478 * Emulated IA32 system calls via int 0x80.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 *
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800480 * Arguments:
481 * eax system call number
482 * ebx arg1
483 * ecx arg2
484 * edx arg3
485 * esi arg4
486 * edi arg5
487 * ebp arg6 (note: not saved in the stack frame, should not be touched)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 *
489 * Notes:
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800490 * Uses the same stack frame as the x86-64 version.
491 * All registers except eax must be saved (but ptrace may violate that).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 * Arguments are zero extended. For system calls that want sign extension and
493 * take long arguments a wrapper is needed. Most calls can just be called
494 * directly.
Denys Vlasenkob87cf632015-02-26 14:40:32 -0800495 * Assumes it is only called from user space and entered with interrupts off.
496 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498ENTRY(ia32_syscall)
Jan Beulichcdc17932007-07-21 17:10:20 +0200499 CFI_STARTPROC32 simple
Jan Beulichadf14232006-09-26 10:52:41 +0200500 CFI_SIGNAL_FRAME
Denys Vlasenkoa232e3d2015-03-27 11:36:20 +0100501 CFI_DEF_CFA rsp,5*8
502 /*CFI_REL_OFFSET ss,4*8 */
503 CFI_REL_OFFSET rsp,3*8
504 /*CFI_REL_OFFSET rflags,2*8 */
505 /*CFI_REL_OFFSET cs,1*8 */
506 CFI_REL_OFFSET rip,0*8
507
508 /*
509 * Interrupts are off on entry.
510 * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
511 * it is too small to ever cause noticeable irq latency.
512 */
Jeremy Fitzhardinge360c0442008-07-08 15:06:28 -0700513 PARAVIRT_ADJUST_EXCEPTION_FRAME
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -0400514 SWAPGS
Jeremy Fitzhardinge66804152008-06-25 00:19:29 -0400515 ENABLE_INTERRUPTS(CLBR_NONE)
Denys Vlasenkoa232e3d2015-03-27 11:36:20 +0100516
Denys Vlasenko4ee8ec12015-03-27 11:36:21 +0100517 /* Zero-extending 32-bit regs, do not remove */
518 movl %eax,%eax
519
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +0200520 /* Construct struct pt_regs on stack (iret frame is already on stack) */
521 pushq_cfi_reg rax /* pt_regs->orig_ax */
522 pushq_cfi_reg rdi /* pt_regs->di */
523 pushq_cfi_reg rsi /* pt_regs->si */
524 pushq_cfi_reg rdx /* pt_regs->dx */
525 pushq_cfi_reg rcx /* pt_regs->cx */
526 pushq_cfi_reg rax /* pt_regs->ax */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 cld
Denys Vlasenko4c9c0e92015-03-31 19:00:04 +0200528 sub $(10*8),%rsp /* pt_regs->r8-11,bp,bx,r12-15 not saved */
529 CFI_ADJUST_CFA_OFFSET 10*8
530
Ingo Molnardca5b522015-03-24 19:44:42 +0100531 orl $TS_COMPAT, ASM_THREAD_INFO(TI_status, %rsp, SIZEOF_PTREGS)
532 testl $_TIF_WORK_SYSCALL_ENTRY, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 jnz ia32_tracesys
H. Peter Anvin36d001c2010-09-14 12:42:41 -0700534 cmpq $(IA32_NR_syscalls-1),%rax
Roland McGrathc09249f2009-02-06 18:15:18 -0800535 ja ia32_badsys
536ia32_do_call:
Denys Vlasenko14f6e952015-02-26 14:40:35 -0800537 /* 32bit syscall -> 64bit C ABI argument conversion */
538 movl %edi,%r8d /* arg5 */
539 movl %ebp,%r9d /* arg6 */
540 xchg %ecx,%esi /* rsi:arg2, rcx:arg4 */
541 movl %ebx,%edi /* arg1 */
542 movl %edx,%edx /* arg3 (zero extension) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 call *ia32_sys_call_table(,%rax,8) # xxx: rip relative
544ia32_sysret:
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800545 movq %rax,RAX(%rsp)
Jan Beulich24e35802009-09-30 11:22:11 +0100546ia32_ret_from_sys_call:
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800547 CLEAR_RREGS
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800548 jmp int_ret_from_sys_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800550ia32_tracesys:
551 SAVE_EXTRA_REGS
Jan Beulichcdc17932007-07-21 17:10:20 +0200552 CLEAR_RREGS
Roland McGrath8ab32bb2008-03-16 21:57:41 -0700553 movq $-ENOSYS,RAX(%rsp) /* ptrace can change this for a bad syscall */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 movq %rsp,%rdi /* &pt_regs -> arg1 */
555 call syscall_trace_enter
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800556 LOAD_ARGS32 /* reload args from stack in case ptrace changed it */
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800557 RESTORE_EXTRA_REGS
H. Peter Anvin36d001c2010-09-14 12:42:41 -0700558 cmpq $(IA32_NR_syscalls-1),%rax
Roland McGrathc09249f2009-02-06 18:15:18 -0800559 ja int_ret_from_sys_call /* ia32_tracesys has set RAX(%rsp) */
560 jmp ia32_do_call
Jan Beulich4b787e02006-06-26 13:56:55 +0200561END(ia32_syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563ia32_badsys:
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800564 movq $0,ORIG_RAX(%rsp)
Jan Beulich24e35802009-09-30 11:22:11 +0100565 movq $-ENOSYS,%rax
566 jmp ia32_sysret
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 CFI_ENDPROC
569
Ramkumar Ramachandrad2475b82013-07-10 23:34:28 +0530570 .macro PTREGSCALL label, func
Jan Beulichf6b2bc82011-11-29 11:24:10 +0000571 ALIGN
572GLOBAL(\label)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 leaq \func(%rip),%rax
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 jmp ia32_ptregs_common
575 .endm
576
Jan Beulich27651302006-01-11 22:41:59 +0100577 CFI_STARTPROC32
Jan Beulich7effaa82005-09-12 18:49:24 +0200578
Al Viro3fe26fa2012-11-12 14:32:42 -0500579 PTREGSCALL stub32_rt_sigreturn, sys32_rt_sigreturn
580 PTREGSCALL stub32_sigreturn, sys32_sigreturn
Al Viro3fe26fa2012-11-12 14:32:42 -0500581 PTREGSCALL stub32_fork, sys_fork
582 PTREGSCALL stub32_vfork, sys_vfork
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Jan Beulichf6b2bc82011-11-29 11:24:10 +0000584 ALIGN
Al Viro1d4b4b22012-10-22 22:34:11 -0400585GLOBAL(stub32_clone)
586 leaq sys_clone(%rip),%rax
587 mov %r8, %rcx
588 jmp ia32_ptregs_common
589
590 ALIGN
Jan Beulichf6b2bc82011-11-29 11:24:10 +0000591ia32_ptregs_common:
Jan Beulich27651302006-01-11 22:41:59 +0100592 CFI_ENDPROC
593 CFI_STARTPROC32 simple
Jan Beulichadf14232006-09-26 10:52:41 +0200594 CFI_SIGNAL_FRAME
Denys Vlasenkob3ab90b2015-02-26 14:40:37 -0800595 CFI_DEF_CFA rsp,SIZEOF_PTREGS
Denys Vlasenkof2db9382015-02-26 14:40:30 -0800596 CFI_REL_OFFSET rax,RAX
597 CFI_REL_OFFSET rcx,RCX
598 CFI_REL_OFFSET rdx,RDX
599 CFI_REL_OFFSET rsi,RSI
600 CFI_REL_OFFSET rdi,RDI
601 CFI_REL_OFFSET rip,RIP
602/* CFI_REL_OFFSET cs,CS*/
603/* CFI_REL_OFFSET rflags,EFLAGS*/
604 CFI_REL_OFFSET rsp,RSP
605/* CFI_REL_OFFSET ss,SS*/
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800606 SAVE_EXTRA_REGS 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 call *%rax
Denys Vlasenko76f5df42015-02-26 14:40:27 -0800608 RESTORE_EXTRA_REGS 8
609 ret
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 CFI_ENDPROC
Jan Beulich4b787e02006-06-26 13:56:55 +0200611END(ia32_ptregs_common)