xref: /titanic_52/usr/src/uts/i86pc/ml/syscall_asm_amd64.s (revision 94a65b20fd6706171eba5212ea3ca67570ba7e12)
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23 * Use is subject to license terms.
24 */
25
26#include <sys/asm_linkage.h>
27#include <sys/asm_misc.h>
28#include <sys/regset.h>
29#include <sys/privregs.h>
30#include <sys/psw.h>
31#include <sys/machbrand.h>
32
33#if defined(__lint)
34
35#include <sys/types.h>
36#include <sys/thread.h>
37#include <sys/systm.h>
38
39#else	/* __lint */
40
41#include <sys/segments.h>
42#include <sys/pcb.h>
43#include <sys/trap.h>
44#include <sys/ftrace.h>
45#include <sys/traptrace.h>
46#include <sys/clock.h>
47#include <sys/model.h>
48#include <sys/panic.h>
49
50#if defined(__xpv)
51#include <sys/hypervisor.h>
52#endif
53
54#include "assym.h"
55
56#endif	/* __lint */
57
58/*
59 * We implement five flavours of system call entry points
60 *
61 * -	syscall/sysretq		(amd64 generic)
62 * -	syscall/sysretl		(i386 plus SYSC bit)
63 * -	sysenter/sysexit	(i386 plus SEP bit)
64 * -	int/iret		(i386 generic)
65 * -	lcall/iret		(i386 generic)
66 *
67 * The current libc included in Solaris uses int/iret as the base unoptimized
68 * kernel entry method. Older libc implementations and legacy binaries may use
69 * the lcall call gate, so it must continue to be supported.
70 *
71 * System calls that use an lcall call gate are processed in trap() via a
72 * segment-not-present trap, i.e. lcalls are extremely slow(!).
73 *
74 * The basic pattern used in the 32-bit SYSC handler at this point in time is
75 * to have the bare minimum of assembler, and get to the C handlers as
76 * quickly as possible.
77 *
78 * The 64-bit handler is much closer to the sparcv9 handler; that's
79 * because of passing arguments in registers.  The 32-bit world still
80 * passes arguments on the stack -- that makes that handler substantially
81 * more complex.
82 *
83 * The two handlers share a few code fragments which are broken
84 * out into preprocessor macros below.
85 *
86 * XX64	come back and speed all this up later.  The 32-bit stuff looks
87 * especially easy to speed up the argument copying part ..
88 *
89 *
90 * Notes about segment register usage (c.f. the 32-bit kernel)
91 *
92 * In the 32-bit kernel, segment registers are dutifully saved and
93 * restored on all mode transitions because the kernel uses them directly.
94 * When the processor is running in 64-bit mode, segment registers are
95 * largely ignored.
96 *
97 * %cs and %ss
98 *	controlled by the hardware mechanisms that make mode transitions
99 *
100 * The remaining segment registers have to either be pointing at a valid
101 * descriptor i.e. with the 'present' bit set, or they can NULL descriptors
102 *
103 * %ds and %es
104 *	always ignored
105 *
106 * %fs and %gs
107 *	fsbase and gsbase are used to control the place they really point at.
108 *	The kernel only depends on %gs, and controls its own gsbase via swapgs
109 *
110 * Note that loading segment registers is still costly because the GDT
111 * lookup still happens (this is because the hardware can't know that we're
112 * not setting up these segment registers for a 32-bit program).  Thus we
113 * avoid doing this in the syscall path, and defer them to lwp context switch
114 * handlers, so the register values remain virtualized to the lwp.
115 */
116
117#if defined(SYSCALLTRACE)
118#define	ORL_SYSCALLTRACE(r32)		\
119	orl	syscalltrace(%rip), r32
120#else
121#define	ORL_SYSCALLTRACE(r32)
122#endif
123
124/*
125 * In the 32-bit kernel, we do absolutely nothing before getting into the
126 * brand callback checks.  In 64-bit land, we do swapgs and then come here.
127 * We assume that the %rsp- and %r15-stashing fields in the CPU structure
128 * are still unused.
129 *
130 * Check if a brand_mach_ops callback is defined for the specified callback_id
131 * type.  If so invoke it with the kernel's %gs value loaded and the following
132 * data on the stack:
133 *
134 * stack:  --------------------------------------
135 *      40 | user %gs				|
136 *      32 | callback pointer			|
137 *    | 24 | user stack pointer			|
138 *    | 16 | lwp pointer			|
139 *    v  8 | userland return address		|
140 *       0 | callback wrapper return addr	|
141 *         --------------------------------------
142 *
143 */
144#define	BRAND_CALLBACK(callback_id)					    \
145	movq	%rsp, %gs:CPU_RTMP_RSP	/* save the stack pointer	*/ ;\
146	movq	%r15, %gs:CPU_RTMP_R15	/* save %r15			*/ ;\
147	movq	%gs:CPU_THREAD, %r15	/* load the thread pointer	*/ ;\
148	movq	T_STACK(%r15), %rsp	/* switch to the kernel stack	*/ ;\
149	subq	$24, %rsp		/* save space for 3 pointers	*/ ;\
150	pushq	%r14			/* save %r14			*/ ;\
151	movq	%gs:CPU_RTMP_RSP, %r14					   ;\
152	movq	%r14, 8(%rsp)		/* stash the user stack pointer	*/ ;\
153	popq	%r14			/* restore %r14			*/ ;\
154	movq	T_LWP(%r15), %r15	/* load the lwp pointer		*/ ;\
155	pushq	%r15			/* push the lwp pointer		*/ ;\
156	movq	LWP_PROCP(%r15), %r15	/* load the proc pointer	*/ ;\
157	movq	P_BRAND(%r15), %r15	/* load the brand pointer	*/ ;\
158	movq	B_MACHOPS(%r15), %r15	/* load the machops pointer	*/ ;\
159	movq	_CONST(_MUL(callback_id, CPTRSIZE))(%r15), %r15		   ;\
160	cmpq	$0, %r15						   ;\
161	je	1f							   ;\
162	movq	%r15, 16(%rsp)		/* save the callback pointer	*/ ;\
163	movq	%gs:CPU_RTMP_RSP, %r15	/* grab the user stack pointer	*/ ;\
164	pushq	(%r15)			/* push the return address	*/ ;\
165	SWAPGS				/* user gsbase			*/ ;\
166	mov	%gs, %r15		/* get %gs			*/ ;\
167	movq	%r15, 32(%rsp)		/* save %gs on stack		*/ ;\
168	SWAPGS				/* kernel gsbase		*/ ;\
169	movq	%gs:CPU_RTMP_R15, %r15	/* restore %r15			*/ ;\
170	call	*24(%rsp)		/* call callback		*/ ;\
1711:	movq	%gs:CPU_RTMP_R15, %r15	/* restore %r15			*/ ;\
172	movq	%gs:CPU_RTMP_RSP, %rsp	/* restore the stack pointer	*/
173
174#define	MSTATE_TRANSITION(from, to)		\
175	movl	$from, %edi;			\
176	movl	$to, %esi;			\
177	call	syscall_mstate
178
179/*
180 * Check to see if a simple (direct) return is possible i.e.
181 *
182 *	if (t->t_post_sys_ast | syscalltrace |
183 *	    lwp->lwp_pcb.pcb_rupdate == 1)
184 *		do full version	;
185 *
186 * Preconditions:
187 * -	t is curthread
188 * Postconditions:
189 * -	condition code NE is set if post-sys is too complex
190 * -	rtmp is zeroed if it isn't (we rely on this!)
191 * -	ltmp is smashed
192 */
193#define	CHECK_POSTSYS_NE(t, ltmp, rtmp)			\
194	movq	T_LWP(t), ltmp;				\
195	movzbl	PCB_RUPDATE(ltmp), rtmp;		\
196	ORL_SYSCALLTRACE(rtmp);				\
197	orl	T_POST_SYS_AST(t), rtmp;		\
198	cmpl	$0, rtmp
199
200/*
201 * Fix up the lwp, thread, and eflags for a successful return
202 *
203 * Preconditions:
204 * -	zwreg contains zero
205 */
206#define	SIMPLE_SYSCALL_POSTSYS(t, lwp, zwreg)		\
207	movb	$LWP_USER, LWP_STATE(lwp);		\
208	movw	zwreg, T_SYSNUM(t);			\
209	andb	$_CONST(0xffff - PS_C), REGOFF_RFL(%rsp)
210
211/*
212 * ASSERT(lwptoregs(lwp) == rp);
213 *
214 * This may seem obvious, but very odd things happen if this
215 * assertion is false
216 *
217 * Preconditions:
218 *	(%rsp is ready for normal call sequence)
219 * Postconditions (if assertion is true):
220 *	%r11 is smashed
221 *
222 * ASSERT(rp->r_cs == descnum)
223 *
224 * The code selector is written into the regs structure when the
225 * lwp stack is created.  We use this ASSERT to validate that
226 * the regs structure really matches how we came in.
227 *
228 * Preconditions:
229 *	(%rsp is ready for normal call sequence)
230 * Postconditions (if assertion is true):
231 *	-none-
232 *
233 * ASSERT(lwp->lwp_pcb.pcb_rupdate == 0);
234 *
235 * If this is false, it meant that we returned to userland without
236 * updating the segment registers as we were supposed to.
237 *
238 * Note that we must ensure no interrupts or other traps intervene
239 * between entering privileged mode and performing the assertion,
240 * otherwise we may perform a context switch on the thread, which
241 * will end up setting pcb_rupdate to 1 again.
242 */
243#if defined(DEBUG)
244
245#if !defined(__lint)
246
247__lwptoregs_msg:
248	.string	"syscall_asm_amd64.s:%d lwptoregs(%p) [%p] != rp [%p]"
249
250__codesel_msg:
251	.string	"syscall_asm_amd64.s:%d rp->r_cs [%ld] != %ld"
252
253__no_rupdate_msg:
254	.string	"syscall_asm_amd64.s:%d lwp %p, pcb_rupdate != 0"
255
256#endif	/* !__lint */
257
258#define	ASSERT_LWPTOREGS(lwp, rp)			\
259	movq	LWP_REGS(lwp), %r11;			\
260	cmpq	rp, %r11;				\
261	je	7f;					\
262	leaq	__lwptoregs_msg(%rip), %rdi;		\
263	movl	$__LINE__, %esi;			\
264	movq	lwp, %rdx;				\
265	movq	%r11, %rcx;				\
266	movq	rp, %r8;				\
267	xorl	%eax, %eax;				\
268	call	panic;					\
2697:
270
271#define	ASSERT_NO_RUPDATE_PENDING(lwp)			\
272	testb	$0x1, PCB_RUPDATE(lwp);			\
273	je	8f;					\
274	movq	lwp, %rdx;				\
275	leaq	__no_rupdate_msg(%rip), %rdi;		\
276	movl	$__LINE__, %esi;			\
277	xorl	%eax, %eax;				\
278	call	panic;					\
2798:
280
281#else
282#define	ASSERT_LWPTOREGS(lwp, rp)
283#define	ASSERT_NO_RUPDATE_PENDING(lwp)
284#endif
285
286/*
287 * Do the traptrace thing and restore any registers we used
288 * in situ.  Assumes that %rsp is pointing at the base of
289 * the struct regs, obviously ..
290 */
291#ifdef TRAPTRACE
292#define	SYSCALL_TRAPTRACE(ttype)				\
293	TRACE_PTR(%rdi, %rbx, %ebx, %rcx, ttype);		\
294	TRACE_REGS(%rdi, %rsp, %rbx, %rcx);			\
295	TRACE_STAMP(%rdi);	/* rdtsc clobbers %eax, %edx */	\
296	movq	REGOFF_RAX(%rsp), %rax;				\
297	movq	REGOFF_RBX(%rsp), %rbx;				\
298	movq	REGOFF_RCX(%rsp), %rcx;				\
299	movq	REGOFF_RDX(%rsp), %rdx;				\
300	movl	%eax, TTR_SYSNUM(%rdi);				\
301	movq	REGOFF_RDI(%rsp), %rdi
302
303#define	SYSCALL_TRAPTRACE32(ttype)				\
304	SYSCALL_TRAPTRACE(ttype);				\
305	/* paranoia: clean the top 32-bits of the registers */	\
306	orl	%eax, %eax;					\
307	orl	%ebx, %ebx;					\
308	orl	%ecx, %ecx;					\
309	orl	%edx, %edx;					\
310	orl	%edi, %edi
311#else	/* TRAPTRACE */
312#define	SYSCALL_TRAPTRACE(ttype)
313#define	SYSCALL_TRAPTRACE32(ttype)
314#endif	/* TRAPTRACE */
315
316/*
317 * The 64-bit libc syscall wrapper does this:
318 *
319 * fn(<args>)
320 * {
321 *	movq	%rcx, %r10	-- because syscall smashes %rcx
322 *	movl	$CODE, %eax
323 *	syscall
324 *	<error processing>
325 * }
326 *
327 * Thus when we come into the kernel:
328 *
329 *	%rdi, %rsi, %rdx, %r10, %r8, %r9 contain first six args
330 *	%rax is the syscall number
331 *	%r12-%r15 contain caller state
332 *
333 * The syscall instruction arranges that:
334 *
335 *	%rcx contains the return %rip
336 *	%r11d contains bottom 32-bits of %rflags
337 *	%rflags is masked (as determined by the SFMASK msr)
338 *	%cs is set to UCS_SEL (as determined by the STAR msr)
339 *	%ss is set to UDS_SEL (as determined by the STAR msr)
340 *	%rip is set to sys_syscall (as determined by the LSTAR msr)
341 *
342 * Or in other words, we have no registers available at all.
343 * Only swapgs can save us!
344 *
345 * Under the hypervisor, the swapgs has happened already.  However, the
346 * state of the world is very different from that we're familiar with.
347 *
348 * In particular, we have a stack structure like that for interrupt
349 * gates, except that the %cs and %ss registers are modified for reasons
350 * that are not entirely clear.  Critically, the %rcx/%r11 values do
351 * *not* reflect the usage of those registers under a 'real' syscall[1];
352 * the stack, therefore, looks like this:
353 *
354 *	0x0(rsp)	potentially junk %rcx
355 *	0x8(rsp)	potentially junk %r11
356 *	0x10(rsp)	user %rip
357 *	0x18(rsp)	modified %cs
358 *	0x20(rsp)	user %rflags
359 *	0x28(rsp)	user %rsp
360 *	0x30(rsp)	modified %ss
361 *
362 *
363 * and before continuing on, we must load the %rip into %rcx and the
364 * %rflags into %r11.
365 *
366 * [1] They used to, and we relied on it, but this was broken in 3.1.1.
367 * Sigh.
368 */
369#if defined(__xpv)
370#define	XPV_SYSCALL_PROD						\
371	movq	0x10(%rsp), %rcx;					\
372	movq	0x20(%rsp), %r11;					\
373	movq	0x28(%rsp), %rsp
374#else
375#define	XPV_SYSCALL_PROD /* nothing */
376#endif
377
378#if defined(__lint)
379
380/*ARGSUSED*/
381void
382sys_syscall()
383{}
384
385void
386_allsyscalls()
387{}
388
389size_t _allsyscalls_size;
390
391#else	/* __lint */
392
393	ENTRY_NP2(brand_sys_syscall,_allsyscalls)
394	SWAPGS				/* kernel gsbase */
395	XPV_SYSCALL_PROD
396	BRAND_CALLBACK(BRAND_CB_SYSCALL)
397	SWAPGS				/* user gsbase */
398
399#if defined(__xpv)
400	jmp	noprod_sys_syscall
401#endif
402
403	ALTENTRY(sys_syscall)
404	SWAPGS				/* kernel gsbase */
405	XPV_SYSCALL_PROD
406
407noprod_sys_syscall:
408	ASSERT_UPCALL_MASK_IS_SET
409
410	movq	%r15, %gs:CPU_RTMP_R15
411	movq	%rsp, %gs:CPU_RTMP_RSP
412
413	movq	%gs:CPU_THREAD, %r15
414	movq	T_STACK(%r15), %rsp
415
416	movl	$UCS_SEL, REGOFF_CS(%rsp)
417	movq	%rcx, REGOFF_RIP(%rsp)		/* syscall: %rip -> %rcx */
418	movq	%r11, REGOFF_RFL(%rsp)		/* syscall: %rfl -> %r11d */
419	movl	$UDS_SEL, REGOFF_SS(%rsp)
420
421	movl	%eax, %eax			/* wrapper: sysc# -> %eax */
422	movq	%rdi, REGOFF_RDI(%rsp)
423	movq	%rsi, REGOFF_RSI(%rsp)
424	movq	%rdx, REGOFF_RDX(%rsp)
425	movq	%r10, REGOFF_RCX(%rsp)		/* wrapper: %rcx -> %r10 */
426	movq	%r10, %rcx			/* arg[3] for direct calls */
427
428	movq	%r8, REGOFF_R8(%rsp)
429	movq	%r9, REGOFF_R9(%rsp)
430	movq	%rax, REGOFF_RAX(%rsp)
431	movq	%rbx, REGOFF_RBX(%rsp)
432
433	movq	%rbp, REGOFF_RBP(%rsp)
434	movq	%r10, REGOFF_R10(%rsp)
435	movq	%gs:CPU_RTMP_RSP, %r11
436	movq	%r11, REGOFF_RSP(%rsp)
437	movq	%r12, REGOFF_R12(%rsp)
438
439	movq	%r13, REGOFF_R13(%rsp)
440	movq	%r14, REGOFF_R14(%rsp)
441	movq	%gs:CPU_RTMP_R15, %r10
442	movq	%r10, REGOFF_R15(%rsp)
443	movq	$0, REGOFF_SAVFP(%rsp)
444	movq	$0, REGOFF_SAVPC(%rsp)
445
446	/*
447	 * Copy these registers here in case we end up stopped with
448	 * someone (like, say, /proc) messing with our register state.
449	 * We don't -restore- them unless we have to in update_sregs.
450	 *
451	 * Since userland -can't- change fsbase or gsbase directly,
452	 * and capturing them involves two serializing instructions,
453	 * we don't bother to capture them here.
454	 */
455	xorl	%ebx, %ebx
456	movw	%ds, %bx
457	movq	%rbx, REGOFF_DS(%rsp)
458	movw	%es, %bx
459	movq	%rbx, REGOFF_ES(%rsp)
460	movw	%fs, %bx
461	movq	%rbx, REGOFF_FS(%rsp)
462	movw	%gs, %bx
463	movq	%rbx, REGOFF_GS(%rsp)
464
465	/*
466	 * Machine state saved in the regs structure on the stack
467	 * First six args in %rdi, %rsi, %rdx, %rcx, %r8, %r9
468	 * %eax is the syscall number
469	 * %rsp is the thread's stack, %r15 is curthread
470	 * REG_RSP(%rsp) is the user's stack
471	 */
472
473	SYSCALL_TRAPTRACE($TT_SYSC64)
474
475	movq	%rsp, %rbp
476
477	movq	T_LWP(%r15), %r14
478	ASSERT_NO_RUPDATE_PENDING(%r14)
479	ENABLE_INTR_FLAGS
480
481	MSTATE_TRANSITION(LMS_USER, LMS_SYSTEM)
482	movl	REGOFF_RAX(%rsp), %eax	/* (%rax damaged by mstate call) */
483
484	ASSERT_LWPTOREGS(%r14, %rsp)
485
486	movb	$LWP_SYS, LWP_STATE(%r14)
487	incq	LWP_RU_SYSC(%r14)
488	movb	$NORMALRETURN, LWP_EOSYS(%r14)
489
490	incq	%gs:CPU_STATS_SYS_SYSCALL
491
492	movw	%ax, T_SYSNUM(%r15)
493	movzbl	T_PRE_SYS(%r15), %ebx
494	ORL_SYSCALLTRACE(%ebx)
495	testl	%ebx, %ebx
496	jne	_syscall_pre
497
498_syscall_invoke:
499	movq	REGOFF_RDI(%rbp), %rdi
500	movq	REGOFF_RSI(%rbp), %rsi
501	movq	REGOFF_RDX(%rbp), %rdx
502	movq	REGOFF_RCX(%rbp), %rcx
503	movq	REGOFF_R8(%rbp), %r8
504	movq	REGOFF_R9(%rbp), %r9
505
506	cmpl	$NSYSCALL, %eax
507	jae	_syscall_ill
508	shll	$SYSENT_SIZE_SHIFT, %eax
509	leaq	sysent(%rax), %rbx
510
511	call	*SY_CALLC(%rbx)
512
513	movq	%rax, %r12
514	movq	%rdx, %r13
515
516	/*
517	 * If the handler returns two ints, then we need to split the
518	 * 64-bit return value into two 32-bit values.
519	 */
520	testw	$SE_32RVAL2, SY_FLAGS(%rbx)
521	je	5f
522	movq	%r12, %r13
523	shrq	$32, %r13	/* upper 32-bits into %edx */
524	movl	%r12d, %r12d	/* lower 32-bits into %eax */
5255:
526	/*
527	 * Optimistically assume that there's no post-syscall
528	 * work to do.  (This is to avoid having to call syscall_mstate()
529	 * with interrupts disabled)
530	 */
531	MSTATE_TRANSITION(LMS_SYSTEM, LMS_USER)
532
533	/*
534	 * We must protect ourselves from being descheduled here;
535	 * If we were, and we ended up on another cpu, or another
536	 * lwp got in ahead of us, it could change the segment
537	 * registers without us noticing before we return to userland.
538	 */
539	CLI(%r14)
540	CHECK_POSTSYS_NE(%r15, %r14, %ebx)
541	jne	_syscall_post
542	SIMPLE_SYSCALL_POSTSYS(%r15, %r14, %bx)
543
544	movq	%r12, REGOFF_RAX(%rsp)
545	movq	%r13, REGOFF_RDX(%rsp)
546
547	/*
548	 * To get back to userland, we need the return %rip in %rcx and
549	 * the return %rfl in %r11d.  The sysretq instruction also arranges
550	 * to fix up %cs and %ss; everything else is our responsibility.
551	 */
552	movq	REGOFF_RDI(%rsp), %rdi
553	movq	REGOFF_RSI(%rsp), %rsi
554	movq	REGOFF_RDX(%rsp), %rdx
555	/* %rcx used to restore %rip value */
556
557	movq	REGOFF_R8(%rsp), %r8
558	movq	REGOFF_R9(%rsp), %r9
559	movq	REGOFF_RAX(%rsp), %rax
560	movq	REGOFF_RBX(%rsp), %rbx
561
562	movq	REGOFF_RBP(%rsp), %rbp
563	movq	REGOFF_R10(%rsp), %r10
564	/* %r11 used to restore %rfl value */
565	movq	REGOFF_R12(%rsp), %r12
566
567	movq	REGOFF_R13(%rsp), %r13
568	movq	REGOFF_R14(%rsp), %r14
569	movq	REGOFF_R15(%rsp), %r15
570
571	movq	REGOFF_RIP(%rsp), %rcx
572	movl	REGOFF_RFL(%rsp), %r11d
573
574#if defined(__xpv)
575	addq	$REGOFF_RIP, %rsp
576#else
577	movq	REGOFF_RSP(%rsp), %rsp
578#endif
579
580        /*
581         * There can be no instructions between the ALTENTRY below and
582	 * SYSRET or we could end up breaking brand support. See label usage
583         * in sn1_brand_syscall_callback for an example.
584         */
585	ASSERT_UPCALL_MASK_IS_SET
586#if defined(__xpv)
587	SYSRETQ
588        ALTENTRY(nopop_sys_syscall_swapgs_sysretq)
589
590	/*
591	 * We can only get here after executing a brand syscall
592	 * interposition callback handler and simply need to
593	 * "sysretq" back to userland. On the hypervisor this
594	 * involves the iret hypercall which requires us to construct
595	 * just enough of the stack needed for the hypercall.
596	 * (rip, cs, rflags, rsp, ss).
597	 */
598	movq    %rsp, %gs:CPU_RTMP_RSP		/* save user's rsp */
599	movq	%gs:CPU_THREAD, %r11
600	movq	T_STACK(%r11), %rsp
601
602	movq	%rcx, REGOFF_RIP(%rsp)
603	movl	$UCS_SEL, REGOFF_CS(%rsp)
604	movq	%gs:CPU_RTMP_RSP, %r11
605	movq	%r11, REGOFF_RSP(%rsp)
606	pushfq
607	popq	%r11				/* hypercall enables ints */
608	movq	%r11, REGOFF_RFL(%rsp)
609	movl	$UDS_SEL, REGOFF_SS(%rsp)
610	addq	$REGOFF_RIP, %rsp
611	/*
612	 * XXPV: see comment in SYSRETQ definition for future optimization
613	 *       we could take.
614	 */
615	ASSERT_UPCALL_MASK_IS_SET
616	SYSRETQ
617#else
618        ALTENTRY(nopop_sys_syscall_swapgs_sysretq)
619	SWAPGS				/* user gsbase */
620	SYSRETQ
621#endif
622        /*NOTREACHED*/
623        SET_SIZE(nopop_sys_syscall_swapgs_sysretq)
624
625_syscall_pre:
626	call	pre_syscall
627	movl	%eax, %r12d
628	testl	%eax, %eax
629	jne	_syscall_post_call
630	/*
631	 * Didn't abort, so reload the syscall args and invoke the handler.
632	 */
633	movzwl	T_SYSNUM(%r15), %eax
634	jmp	_syscall_invoke
635
636_syscall_ill:
637	call	nosys
638	movq	%rax, %r12
639	movq	%rdx, %r13
640	jmp	_syscall_post_call
641
642_syscall_post:
643	STI
644	/*
645	 * Sigh, our optimism wasn't justified, put it back to LMS_SYSTEM
646	 * so that we can account for the extra work it takes us to finish.
647	 */
648	MSTATE_TRANSITION(LMS_USER, LMS_SYSTEM)
649_syscall_post_call:
650	movq	%r12, %rdi
651	movq	%r13, %rsi
652	call	post_syscall
653	MSTATE_TRANSITION(LMS_SYSTEM, LMS_USER)
654	jmp	_sys_rtt
655	SET_SIZE(sys_syscall)
656	SET_SIZE(brand_sys_syscall)
657
658#endif	/* __lint */
659
660#if defined(__lint)
661
662/*ARGSUSED*/
663void
664sys_syscall32()
665{}
666
667#else	/* __lint */
668
669	ENTRY_NP(brand_sys_syscall32)
670	SWAPGS				/* kernel gsbase */
671	XPV_TRAP_POP
672	BRAND_CALLBACK(BRAND_CB_SYSCALL32)
673	SWAPGS				/* user gsbase */
674
675#if defined(__xpv)
676	jmp	nopop_sys_syscall32
677#endif
678
679	ALTENTRY(sys_syscall32)
680	SWAPGS				/* kernel gsbase */
681
682#if defined(__xpv)
683	XPV_TRAP_POP
684nopop_sys_syscall32:
685#endif
686
687	movl	%esp, %r10d
688	movq	%gs:CPU_THREAD, %r15
689	movq	T_STACK(%r15), %rsp
690	movl	%eax, %eax
691
692	movl	$U32CS_SEL, REGOFF_CS(%rsp)
693	movl	%ecx, REGOFF_RIP(%rsp)		/* syscall: %rip -> %rcx */
694	movq	%r11, REGOFF_RFL(%rsp)		/* syscall: %rfl -> %r11d */
695	movq	%r10, REGOFF_RSP(%rsp)
696	movl	$UDS_SEL, REGOFF_SS(%rsp)
697
698_syscall32_save:
699	movl	%edi, REGOFF_RDI(%rsp)
700	movl	%esi, REGOFF_RSI(%rsp)
701	movl	%ebp, REGOFF_RBP(%rsp)
702	movl	%ebx, REGOFF_RBX(%rsp)
703	movl	%edx, REGOFF_RDX(%rsp)
704	movl	%ecx, REGOFF_RCX(%rsp)
705	movl	%eax, REGOFF_RAX(%rsp)		/* wrapper: sysc# -> %eax */
706	movq	$0, REGOFF_SAVFP(%rsp)
707	movq	$0, REGOFF_SAVPC(%rsp)
708
709	/*
710	 * Copy these registers here in case we end up stopped with
711	 * someone (like, say, /proc) messing with our register state.
712	 * We don't -restore- them unless we have to in update_sregs.
713	 *
714	 * Since userland -can't- change fsbase or gsbase directly,
715	 * we don't bother to capture them here.
716	 */
717	xorl	%ebx, %ebx
718	movw	%ds, %bx
719	movq	%rbx, REGOFF_DS(%rsp)
720	movw	%es, %bx
721	movq	%rbx, REGOFF_ES(%rsp)
722	movw	%fs, %bx
723	movq	%rbx, REGOFF_FS(%rsp)
724	movw	%gs, %bx
725	movq	%rbx, REGOFF_GS(%rsp)
726
727	/*
728	 * Application state saved in the regs structure on the stack
729	 * %eax is the syscall number
730	 * %rsp is the thread's stack, %r15 is curthread
731	 * REG_RSP(%rsp) is the user's stack
732	 */
733
734	SYSCALL_TRAPTRACE32($TT_SYSC)
735
736	movq	%rsp, %rbp
737
738	movq	T_LWP(%r15), %r14
739	ASSERT_NO_RUPDATE_PENDING(%r14)
740
741	ENABLE_INTR_FLAGS
742
743	MSTATE_TRANSITION(LMS_USER, LMS_SYSTEM)
744	movl	REGOFF_RAX(%rsp), %eax	/* (%rax damaged by mstate call) */
745
746	ASSERT_LWPTOREGS(%r14, %rsp)
747
748	incq	 %gs:CPU_STATS_SYS_SYSCALL
749
750	/*
751	 * Make some space for MAXSYSARGS (currently 8) 32-bit args placed
752	 * into 64-bit (long) arg slots, maintaining 16 byte alignment.  Or
753	 * more succinctly:
754	 *
755	 *	SA(MAXSYSARGS * sizeof (long)) == 64
756	 */
757#define	SYS_DROP	64			/* drop for args */
758	subq	$SYS_DROP, %rsp
759	movb	$LWP_SYS, LWP_STATE(%r14)
760	movq	%r15, %rdi
761	movq	%rsp, %rsi
762	call	syscall_entry
763
764	/*
765	 * Fetch the arguments copied onto the kernel stack and put
766	 * them in the right registers to invoke a C-style syscall handler.
767	 * %rax contains the handler address.
768	 *
769	 * Ideas for making all this go faster of course include simply
770	 * forcibly fetching 6 arguments from the user stack under lofault
771	 * protection, reverting to copyin_args only when watchpoints
772	 * are in effect.
773	 *
774	 * (If we do this, make sure that exec and libthread leave
775	 * enough space at the top of the stack to ensure that we'll
776	 * never do a fetch from an invalid page.)
777	 *
778	 * Lots of ideas here, but they won't really help with bringup B-)
779	 * Correctness can't wait, performance can wait a little longer ..
780	 */
781
782	movq	%rax, %rbx
783	movl	0(%rsp), %edi
784	movl	8(%rsp), %esi
785	movl	0x10(%rsp), %edx
786	movl	0x18(%rsp), %ecx
787	movl	0x20(%rsp), %r8d
788	movl	0x28(%rsp), %r9d
789
790	call	*SY_CALLC(%rbx)
791
792	movq	%rbp, %rsp	/* pop the args */
793
794	/*
795	 * amd64 syscall handlers -always- return a 64-bit value in %rax.
796	 * On the 32-bit kernel, they always return that value in %eax:%edx
797	 * as required by the 32-bit ABI.
798	 *
799	 * Simulate the same behaviour by unconditionally splitting the
800	 * return value in the same way.
801	 */
802	movq	%rax, %r13
803	shrq	$32, %r13	/* upper 32-bits into %edx */
804	movl	%eax, %r12d	/* lower 32-bits into %eax */
805
806	/*
807	 * Optimistically assume that there's no post-syscall
808	 * work to do.  (This is to avoid having to call syscall_mstate()
809	 * with interrupts disabled)
810	 */
811	MSTATE_TRANSITION(LMS_SYSTEM, LMS_USER)
812
813	/*
814	 * We must protect ourselves from being descheduled here;
815	 * If we were, and we ended up on another cpu, or another
816	 * lwp got in ahead of us, it could change the segment
817	 * registers without us noticing before we return to userland.
818	 */
819	CLI(%r14)
820	CHECK_POSTSYS_NE(%r15, %r14, %ebx)
821	jne	_full_syscall_postsys32
822	SIMPLE_SYSCALL_POSTSYS(%r15, %r14, %bx)
823
824	/*
825	 * To get back to userland, we need to put the return %rip in %rcx and
826	 * the return %rfl in %r11d.  The sysret instruction also arranges
827	 * to fix up %cs and %ss; everything else is our responsibility.
828	 */
829
830	movl	%r12d, %eax			/* %eax: rval1 */
831	movl	REGOFF_RBX(%rsp), %ebx
832	/* %ecx used for return pointer */
833	movl	%r13d, %edx			/* %edx: rval2 */
834	movl	REGOFF_RBP(%rsp), %ebp
835	movl	REGOFF_RSI(%rsp), %esi
836	movl	REGOFF_RDI(%rsp), %edi
837
838	movl	REGOFF_RFL(%rsp), %r11d		/* %r11 -> eflags */
839	movl	REGOFF_RIP(%rsp), %ecx		/* %ecx -> %eip */
840	movl	REGOFF_RSP(%rsp), %esp
841
842	ASSERT_UPCALL_MASK_IS_SET
843        ALTENTRY(nopop_sys_syscall32_swapgs_sysretl)
844	SWAPGS				/* user gsbase */
845	SYSRETL
846        SET_SIZE(nopop_sys_syscall32_swapgs_sysretl)
847	/*NOTREACHED*/
848
849_full_syscall_postsys32:
850	STI
851	/*
852	 * Sigh, our optimism wasn't justified, put it back to LMS_SYSTEM
853	 * so that we can account for the extra work it takes us to finish.
854	 */
855	MSTATE_TRANSITION(LMS_USER, LMS_SYSTEM)
856	movq	%r15, %rdi
857	movq	%r12, %rsi			/* rval1 - %eax */
858	movq	%r13, %rdx			/* rval2 - %edx */
859	call	syscall_exit
860	MSTATE_TRANSITION(LMS_SYSTEM, LMS_USER)
861	jmp	_sys_rtt
862	SET_SIZE(sys_syscall32)
863	SET_SIZE(brand_sys_syscall32)
864
865#endif	/* __lint */
866
867/*
868 * System call handler via the sysenter instruction
869 * Used only for 32-bit system calls on the 64-bit kernel.
870 *
871 * The caller in userland has arranged that:
872 *
873 * -	%eax contains the syscall number
874 * -	%ecx contains the user %esp
875 * -	%edx contains the return %eip
876 * -	the user stack contains the args to the syscall
877 *
878 * Hardware and (privileged) initialization code have arranged that by
879 * the time the sysenter instructions completes:
880 *
881 * - %rip is pointing to sys_sysenter (below).
882 * - %cs and %ss are set to kernel text and stack (data) selectors.
883 * - %rsp is pointing at the lwp's stack
884 * - interrupts have been disabled.
885 *
886 * Note that we are unable to return both "rvals" to userland with
887 * this call, as %edx is used by the sysexit instruction.
888 *
889 * One final complication in this routine is its interaction with
890 * single-stepping in a debugger.  For most of the system call mechanisms,
891 * the CPU automatically clears the single-step flag before we enter the
892 * kernel.  The sysenter mechanism does not clear the flag, so a user
893 * single-stepping through a libc routine may suddenly find him/herself
894 * single-stepping through the kernel.  To detect this, kmdb compares the
895 * trap %pc to the [brand_]sys_enter addresses on each single-step trap.
896 * If it finds that we have single-stepped to a sysenter entry point, it
897 * explicitly clears the flag and executes the sys_sysenter routine.
898 *
899 * One final complication in this final complication is the fact that we
900 * have two different entry points for sysenter: brand_sys_sysenter and
901 * sys_sysenter.  If we enter at brand_sys_sysenter and start single-stepping
902 * through the kernel with kmdb, we will eventually hit the instruction at
903 * sys_sysenter.  kmdb cannot distinguish between that valid single-step
904 * and the undesirable one mentioned above.  To avoid this situation, we
905 * simply add a jump over the instruction at sys_sysenter to make it
906 * impossible to single-step to it.
907 */
908#if defined(__lint)
909
910void
911sys_sysenter()
912{}
913
914#else	/* __lint */
915
916	ENTRY_NP(brand_sys_sysenter)
917	SWAPGS				/* kernel gsbase */
918	ALTENTRY(_brand_sys_sysenter_post_swapgs)
919	BRAND_CALLBACK(BRAND_CB_SYSENTER)
920	/*
921	 * Jump over sys_sysenter to allow single-stepping as described
922	 * above.
923	 */
924	jmp	_sys_sysenter_post_swapgs
925
926	ALTENTRY(sys_sysenter)
927	SWAPGS				/* kernel gsbase */
928
929	ALTENTRY(_sys_sysenter_post_swapgs)
930	movq	%gs:CPU_THREAD, %r15
931
932	movl	$U32CS_SEL, REGOFF_CS(%rsp)
933	movl	%ecx, REGOFF_RSP(%rsp)		/* wrapper: %esp -> %ecx */
934	movl	%edx, REGOFF_RIP(%rsp)		/* wrapper: %eip -> %edx */
935	pushfq
936	popq	%r10
937	movl	$UDS_SEL, REGOFF_SS(%rsp)
938
939	/*
940	 * Set the interrupt flag before storing the flags to the
941	 * flags image on the stack so we can return to user with
942	 * interrupts enabled if we return via sys_rtt_syscall32
943	 */
944	orq	$PS_IE, %r10
945	movq	%r10, REGOFF_RFL(%rsp)
946
947	movl	%edi, REGOFF_RDI(%rsp)
948	movl	%esi, REGOFF_RSI(%rsp)
949	movl	%ebp, REGOFF_RBP(%rsp)
950	movl	%ebx, REGOFF_RBX(%rsp)
951	movl	%edx, REGOFF_RDX(%rsp)
952	movl	%ecx, REGOFF_RCX(%rsp)
953	movl	%eax, REGOFF_RAX(%rsp)		/* wrapper: sysc# -> %eax */
954	movq	$0, REGOFF_SAVFP(%rsp)
955	movq	$0, REGOFF_SAVPC(%rsp)
956
957	/*
958	 * Copy these registers here in case we end up stopped with
959	 * someone (like, say, /proc) messing with our register state.
960	 * We don't -restore- them unless we have to in update_sregs.
961	 *
962	 * Since userland -can't- change fsbase or gsbase directly,
963	 * we don't bother to capture them here.
964	 */
965	xorl	%ebx, %ebx
966	movw	%ds, %bx
967	movq	%rbx, REGOFF_DS(%rsp)
968	movw	%es, %bx
969	movq	%rbx, REGOFF_ES(%rsp)
970	movw	%fs, %bx
971	movq	%rbx, REGOFF_FS(%rsp)
972	movw	%gs, %bx
973	movq	%rbx, REGOFF_GS(%rsp)
974
975	/*
976	 * Application state saved in the regs structure on the stack
977	 * %eax is the syscall number
978	 * %rsp is the thread's stack, %r15 is curthread
979	 * REG_RSP(%rsp) is the user's stack
980	 */
981
982	SYSCALL_TRAPTRACE($TT_SYSENTER)
983
984	movq	%rsp, %rbp
985
986	movq	T_LWP(%r15), %r14
987	ASSERT_NO_RUPDATE_PENDING(%r14)
988
989	ENABLE_INTR_FLAGS
990
991	/*
992	 * Catch 64-bit process trying to issue sysenter instruction
993	 * on Nocona based systems.
994	 */
995	movq	LWP_PROCP(%r14), %rax
996	cmpq	$DATAMODEL_ILP32, P_MODEL(%rax)
997	je	7f
998
999	/*
1000	 * For a non-32-bit process, simulate a #ud, since that's what
1001	 * native hardware does.  The traptrace entry (above) will
1002	 * let you know what really happened.
1003	 */
1004	movq	$T_ILLINST, REGOFF_TRAPNO(%rsp)
1005	movq	REGOFF_CS(%rsp), %rdi
1006	movq	%rdi, REGOFF_ERR(%rsp)
1007	movq	%rsp, %rdi
1008	movq	REGOFF_RIP(%rsp), %rsi
1009	movl	%gs:CPU_ID, %edx
1010	call	trap
1011	jmp	_sys_rtt
10127:
1013
1014	MSTATE_TRANSITION(LMS_USER, LMS_SYSTEM)
1015	movl	REGOFF_RAX(%rsp), %eax	/* (%rax damaged by mstate calls) */
1016
1017	ASSERT_LWPTOREGS(%r14, %rsp)
1018
1019	incq	%gs:CPU_STATS_SYS_SYSCALL
1020
1021	/*
1022	 * Make some space for MAXSYSARGS (currently 8) 32-bit args
1023	 * placed into 64-bit (long) arg slots, plus one 64-bit
1024	 * (long) arg count, maintaining 16 byte alignment.
1025	 */
1026	subq	$SYS_DROP, %rsp
1027	movb	$LWP_SYS, LWP_STATE(%r14)
1028	movq	%r15, %rdi
1029	movq	%rsp, %rsi
1030	call	syscall_entry
1031
1032	/*
1033	 * Fetch the arguments copied onto the kernel stack and put
1034	 * them in the right registers to invoke a C-style syscall handler.
1035	 * %rax contains the handler address.
1036	 */
1037	movq	%rax, %rbx
1038	movl	0(%rsp), %edi
1039	movl	8(%rsp), %esi
1040	movl	0x10(%rsp), %edx
1041	movl	0x18(%rsp), %ecx
1042	movl	0x20(%rsp), %r8d
1043	movl	0x28(%rsp), %r9d
1044
1045	call	*SY_CALLC(%rbx)
1046
1047	movq	%rbp, %rsp	/* pop the args */
1048
1049	/*
1050	 * amd64 syscall handlers -always- return a 64-bit value in %rax.
1051	 * On the 32-bit kernel, the always return that value in %eax:%edx
1052	 * as required by the 32-bit ABI.
1053	 *
1054	 * Simulate the same behaviour by unconditionally splitting the
1055	 * return value in the same way.
1056	 */
1057	movq	%rax, %r13
1058	shrq	$32, %r13	/* upper 32-bits into %edx */
1059	movl	%eax, %r12d	/* lower 32-bits into %eax */
1060
1061	/*
1062	 * Optimistically assume that there's no post-syscall
1063	 * work to do.  (This is to avoid having to call syscall_mstate()
1064	 * with interrupts disabled)
1065	 */
1066	MSTATE_TRANSITION(LMS_SYSTEM, LMS_USER)
1067
1068	/*
1069	 * We must protect ourselves from being descheduled here;
1070	 * If we were, and we ended up on another cpu, or another
1071	 * lwp got int ahead of us, it could change the segment
1072	 * registers without us noticing before we return to userland.
1073	 */
1074	cli
1075	CHECK_POSTSYS_NE(%r15, %r14, %ebx)
1076	jne	_full_syscall_postsys32
1077	SIMPLE_SYSCALL_POSTSYS(%r15, %r14, %bx)
1078
1079	/*
1080	 * To get back to userland, load up the 32-bit registers and
1081	 * sysexit back where we came from.
1082	 */
1083
1084	/*
1085	 * Interrupts will be turned on by the 'sti' executed just before
1086	 * sysexit.  The following ensures that restoring the user's rflags
1087	 * doesn't enable interrupts too soon.
1088	 */
1089	andq	$_BITNOT(PS_IE), REGOFF_RFL(%rsp)
1090
1091	/*
1092	 * (There's no point in loading up %edx because the sysexit
1093	 * mechanism smashes it.)
1094	 */
1095	movl	%r12d, %eax
1096	movl	REGOFF_RBX(%rsp), %ebx
1097	movl	REGOFF_RBP(%rsp), %ebp
1098	movl	REGOFF_RSI(%rsp), %esi
1099	movl	REGOFF_RDI(%rsp), %edi
1100
1101	movl	REGOFF_RIP(%rsp), %edx	/* sysexit: %edx -> %eip */
1102	pushq	REGOFF_RFL(%rsp)
1103	popfq
1104	movl	REGOFF_RSP(%rsp), %ecx	/* sysexit: %ecx -> %esp */
1105        ALTENTRY(sys_sysenter_swapgs_sysexit)
1106	swapgs
1107	sti
1108	sysexit
1109	SET_SIZE(sys_sysenter_swapgs_sysexit)
1110	SET_SIZE(sys_sysenter)
1111	SET_SIZE(_sys_sysenter_post_swapgs)
1112	SET_SIZE(brand_sys_sysenter)
1113
1114#endif	/* __lint */
1115
1116#if defined(__lint)
1117/*
1118 * System call via an int80.  This entry point is only used by the Linux
1119 * application environment.  Unlike the other entry points, there is no
1120 * default action to take if no callback is registered for this process.
1121 */
1122void
1123sys_int80()
1124{}
1125
1126#else	/* __lint */
1127
1128	ENTRY_NP(brand_sys_int80)
1129	SWAPGS				/* kernel gsbase */
1130	XPV_TRAP_POP
1131	BRAND_CALLBACK(BRAND_CB_INT80)
1132	SWAPGS				/* user gsbase */
1133#if defined(__xpv)
1134	jmp	nopop_int80
1135#endif
1136
1137	ENTRY_NP(sys_int80)
1138	/*
1139	 * We hit an int80, but this process isn't of a brand with an int80
1140	 * handler.  Bad process!  Make it look as if the INT failed.
1141	 * Modify %rip to point before the INT, push the expected error
1142	 * code and fake a GP fault. Note on 64-bit hypervisor we need
1143	 * to undo the XPV_TRAP_POP and push rcx and r11 back on the stack
1144	 * because gptrap will pop them again with its own XPV_TRAP_POP.
1145	 */
1146#if defined(__xpv)
1147	XPV_TRAP_POP
1148nopop_int80:
1149#endif
1150	subq	$2, (%rsp)	/* int insn 2-bytes */
1151	pushq	$_CONST(_MUL(T_INT80, GATE_DESC_SIZE) + 2)
1152#if defined(__xpv)
1153	push	%r11
1154	push	%rcx
1155#endif
1156	jmp	gptrap			/ GP fault
1157	SET_SIZE(sys_int80)
1158	SET_SIZE(brand_sys_int80)
1159#endif	/* __lint */
1160
1161
1162/*
1163 * This is the destination of the "int $T_SYSCALLINT" interrupt gate, used by
1164 * the generic i386 libc to do system calls. We do a small amount of setup
1165 * before jumping into the existing sys_syscall32 path.
1166 */
1167#if defined(__lint)
1168
1169/*ARGSUSED*/
1170void
1171sys_syscall_int()
1172{}
1173
1174#else	/* __lint */
1175
1176	ENTRY_NP(brand_sys_syscall_int)
1177	SWAPGS				/* kernel gsbase */
1178	XPV_TRAP_POP
1179	BRAND_CALLBACK(BRAND_CB_INT91)
1180	SWAPGS				/* user gsbase */
1181
1182#if defined(__xpv)
1183	jmp	nopop_syscall_int
1184#endif
1185
1186	ALTENTRY(sys_syscall_int)
1187	SWAPGS				/* kernel gsbase */
1188
1189#if defined(__xpv)
1190	XPV_TRAP_POP
1191nopop_syscall_int:
1192#endif
1193
1194	movq	%gs:CPU_THREAD, %r15
1195	movq	T_STACK(%r15), %rsp
1196	movl	%eax, %eax
1197	/*
1198	 * Set t_post_sys on this thread to force ourselves out via the slow
1199	 * path. It might be possible at some later date to optimize this out
1200	 * and use a faster return mechanism.
1201	 */
1202	movb	$1, T_POST_SYS(%r15)
1203	CLEAN_CS
1204	jmp	_syscall32_save
1205	/*
1206	 * There should be no instructions between this label and SWAPGS/IRET
1207	 * or we could end up breaking branded zone support. See the usage of
1208	 * this label in lx_brand_int80_callback and sn1_brand_int91_callback
1209	 * for examples.
1210	 */
1211        ALTENTRY(sys_sysint_swapgs_iret)
1212	SWAPGS				/* user gsbase */
1213	IRET
1214	/*NOTREACHED*/
1215	SET_SIZE(sys_sysint_swapgs_iret)
1216	SET_SIZE(sys_syscall_int)
1217	SET_SIZE(brand_sys_syscall_int)
1218
1219#endif	/* __lint */
1220
1221/*
1222 * Legacy 32-bit applications and old libc implementations do lcalls;
1223 * we should never get here because the LDT entry containing the syscall
1224 * segment descriptor has the "segment present" bit cleared, which means
1225 * we end up processing those system calls in trap() via a not-present trap.
1226 *
1227 * We do it this way because a call gate unhelpfully does -nothing- to the
1228 * interrupt flag bit, so an interrupt can run us just after the lcall
1229 * completes, but just before the swapgs takes effect.   Thus the INTR_PUSH and
1230 * INTR_POP paths would have to be slightly more complex to dance around
1231 * this problem, and end up depending explicitly on the first
1232 * instruction of this handler being either swapgs or cli.
1233 */
1234
1235#if defined(__lint)
1236
1237/*ARGSUSED*/
1238void
1239sys_lcall32()
1240{}
1241
1242#else	/* __lint */
1243
1244	ENTRY_NP(sys_lcall32)
1245	SWAPGS				/* kernel gsbase */
1246	pushq	$0
1247	pushq	%rbp
1248	movq	%rsp, %rbp
1249	leaq	__lcall_panic_str(%rip), %rdi
1250	xorl	%eax, %eax
1251	call	panic
1252	SET_SIZE(sys_lcall32)
1253
1254__lcall_panic_str:
1255	.string	"sys_lcall32: shouldn't be here!"
1256
1257/*
1258 * Declare a uintptr_t which covers the entire pc range of syscall
1259 * handlers for the stack walkers that need this.
1260 */
1261	.align	CPTRSIZE
1262	.globl	_allsyscalls_size
1263	.type	_allsyscalls_size, @object
1264_allsyscalls_size:
1265	.NWORD	. - _allsyscalls
1266	SET_SIZE(_allsyscalls_size)
1267
1268#endif	/* __lint */
1269
1270/*
1271 * These are the thread context handlers for lwps using sysenter/sysexit.
1272 */
1273
1274#if defined(__lint)
1275
1276/*ARGSUSED*/
1277void
1278sep_save(void *ksp)
1279{}
1280
1281/*ARGSUSED*/
1282void
1283sep_restore(void *ksp)
1284{}
1285
1286#else	/* __lint */
1287
1288	/*
1289	 * setting this value to zero as we switch away causes the
1290	 * stack-pointer-on-sysenter to be NULL, ensuring that we
1291	 * don't silently corrupt another (preempted) thread stack
1292	 * when running an lwp that (somehow) didn't get sep_restore'd
1293	 */
1294	ENTRY_NP(sep_save)
1295	xorl	%edx, %edx
1296	xorl	%eax, %eax
1297	movl	$MSR_INTC_SEP_ESP, %ecx
1298	wrmsr
1299	ret
1300	SET_SIZE(sep_save)
1301
1302	/*
1303	 * Update the kernel stack pointer as we resume onto this cpu.
1304	 */
1305	ENTRY_NP(sep_restore)
1306	movq	%rdi, %rdx
1307	shrq	$32, %rdx
1308	movl	%edi, %eax
1309	movl	$MSR_INTC_SEP_ESP, %ecx
1310	wrmsr
1311	ret
1312	SET_SIZE(sep_restore)
1313
1314#endif	/* __lint */
1315