xref: /titanic_50/usr/src/uts/i86pc/ml/syscall_asm_amd64.s (revision 8cdd6a74847b5ae6ed26727528bdf6b139cf7552)
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
370#if defined(__xpv)
371#define	XPV_SYSCALL_PROD		\
372	XPV_TRAP_POP;			\
373	movq	(%rsp), %rcx;		\
374	movq	0x10(%rsp), %r11
375#else
376#define	XPV_SYSCALL_PROD /* nothing */
377#endif
378
379#if defined(__lint)
380
381/*ARGSUSED*/
382void
383sys_syscall()
384{}
385
386void
387_allsyscalls()
388{}
389
390size_t _allsyscalls_size;
391
392#else	/* __lint */
393
394	ENTRY_NP2(brand_sys_syscall,_allsyscalls)
395	SWAPGS				/* kernel gsbase */
396	XPV_SYSCALL_PROD
397	BRAND_CALLBACK(BRAND_CB_SYSCALL)
398	SWAPGS				/* user gsbase */
399
400#if defined(__xpv)
401	jmp	noprod_sys_syscall
402#endif
403
404	ALTENTRY(sys_syscall)
405	SWAPGS				/* kernel gsbase */
406	XPV_SYSCALL_PROD
407
408noprod_sys_syscall:
409	ASSERT_UPCALL_MASK_IS_SET
410
411	movq	%r15, %gs:CPU_RTMP_R15
412#if defined(__xpv)
413	movq	0x18(%rsp), %r15		/* save user stack */
414	movq	%r15, %gs:CPU_RTMP_RSP
415#else
416	movq	%rsp, %gs:CPU_RTMP_RSP
417#endif	/* __xpv */
418
419	movq	%gs:CPU_THREAD, %r15
420	movq	T_STACK(%r15), %rsp
421
422	movl	$UCS_SEL, REGOFF_CS(%rsp)
423	movq	%rcx, REGOFF_RIP(%rsp)		/* syscall: %rip -> %rcx */
424	movq	%r11, REGOFF_RFL(%rsp)		/* syscall: %rfl -> %r11d */
425	movl	$UDS_SEL, REGOFF_SS(%rsp)
426
427	movl	%eax, %eax			/* wrapper: sysc# -> %eax */
428	movq	%rdi, REGOFF_RDI(%rsp)
429	movq	%rsi, REGOFF_RSI(%rsp)
430	movq	%rdx, REGOFF_RDX(%rsp)
431	movq	%r10, REGOFF_RCX(%rsp)		/* wrapper: %rcx -> %r10 */
432	movq	%r10, %rcx			/* arg[3] for direct calls */
433
434	movq	%r8, REGOFF_R8(%rsp)
435	movq	%r9, REGOFF_R9(%rsp)
436	movq	%rax, REGOFF_RAX(%rsp)
437	movq	%rbx, REGOFF_RBX(%rsp)
438
439	movq	%rbp, REGOFF_RBP(%rsp)
440	movq	%r10, REGOFF_R10(%rsp)
441	movq	%gs:CPU_RTMP_RSP, %r11
442	movq	%r11, REGOFF_RSP(%rsp)
443	movq	%r12, REGOFF_R12(%rsp)
444
445	movq	%r13, REGOFF_R13(%rsp)
446	movq	%r14, REGOFF_R14(%rsp)
447	movq	%gs:CPU_RTMP_R15, %r10
448	movq	%r10, REGOFF_R15(%rsp)
449	movq	$0, REGOFF_SAVFP(%rsp)
450	movq	$0, REGOFF_SAVPC(%rsp)
451
452	/*
453	 * Copy these registers here in case we end up stopped with
454	 * someone (like, say, /proc) messing with our register state.
455	 * We don't -restore- them unless we have to in update_sregs.
456	 *
457	 * Since userland -can't- change fsbase or gsbase directly,
458	 * and capturing them involves two serializing instructions,
459	 * we don't bother to capture them here.
460	 */
461	xorl	%ebx, %ebx
462	movw	%ds, %bx
463	movq	%rbx, REGOFF_DS(%rsp)
464	movw	%es, %bx
465	movq	%rbx, REGOFF_ES(%rsp)
466	movw	%fs, %bx
467	movq	%rbx, REGOFF_FS(%rsp)
468	movw	%gs, %bx
469	movq	%rbx, REGOFF_GS(%rsp)
470
471	/*
472	 * Machine state saved in the regs structure on the stack
473	 * First six args in %rdi, %rsi, %rdx, %rcx, %r8, %r9
474	 * %eax is the syscall number
475	 * %rsp is the thread's stack, %r15 is curthread
476	 * REG_RSP(%rsp) is the user's stack
477	 */
478
479	SYSCALL_TRAPTRACE($TT_SYSC64)
480
481	movq	%rsp, %rbp
482
483	movq	T_LWP(%r15), %r14
484	ASSERT_NO_RUPDATE_PENDING(%r14)
485	ENABLE_INTR_FLAGS
486
487	MSTATE_TRANSITION(LMS_USER, LMS_SYSTEM)
488	movl	REGOFF_RAX(%rsp), %eax	/* (%rax damaged by mstate call) */
489
490	ASSERT_LWPTOREGS(%r14, %rsp)
491
492	movb	$LWP_SYS, LWP_STATE(%r14)
493	incq	LWP_RU_SYSC(%r14)
494	movb	$NORMALRETURN, LWP_EOSYS(%r14)
495
496	incq	%gs:CPU_STATS_SYS_SYSCALL
497
498	movw	%ax, T_SYSNUM(%r15)
499	movzbl	T_PRE_SYS(%r15), %ebx
500	ORL_SYSCALLTRACE(%ebx)
501	testl	%ebx, %ebx
502	jne	_syscall_pre
503
504_syscall_invoke:
505	movq	REGOFF_RDI(%rbp), %rdi
506	movq	REGOFF_RSI(%rbp), %rsi
507	movq	REGOFF_RDX(%rbp), %rdx
508	movq	REGOFF_RCX(%rbp), %rcx
509	movq	REGOFF_R8(%rbp), %r8
510	movq	REGOFF_R9(%rbp), %r9
511
512	cmpl	$NSYSCALL, %eax
513	jae	_syscall_ill
514	shll	$SYSENT_SIZE_SHIFT, %eax
515	leaq	sysent(%rax), %rbx
516
517	call	*SY_CALLC(%rbx)
518
519	movq	%rax, %r12
520	movq	%rdx, %r13
521
522	/*
523	 * If the handler returns two ints, then we need to split the
524	 * 64-bit return value into two 32-bit values.
525	 */
526	testw	$SE_32RVAL2, SY_FLAGS(%rbx)
527	je	5f
528	movq	%r12, %r13
529	shrq	$32, %r13	/* upper 32-bits into %edx */
530	movl	%r12d, %r12d	/* lower 32-bits into %eax */
5315:
532	/*
533	 * Optimistically assume that there's no post-syscall
534	 * work to do.  (This is to avoid having to call syscall_mstate()
535	 * with interrupts disabled)
536	 */
537	MSTATE_TRANSITION(LMS_SYSTEM, LMS_USER)
538
539	/*
540	 * We must protect ourselves from being descheduled here;
541	 * If we were, and we ended up on another cpu, or another
542	 * lwp got in ahead of us, it could change the segment
543	 * registers without us noticing before we return to userland.
544	 */
545	CLI(%r14)
546	CHECK_POSTSYS_NE(%r15, %r14, %ebx)
547	jne	_syscall_post
548	SIMPLE_SYSCALL_POSTSYS(%r15, %r14, %bx)
549
550	movq	%r12, REGOFF_RAX(%rsp)
551	movq	%r13, REGOFF_RDX(%rsp)
552
553	/*
554	 * To get back to userland, we need the return %rip in %rcx and
555	 * the return %rfl in %r11d.  The sysretq instruction also arranges
556	 * to fix up %cs and %ss; everything else is our responsibility.
557	 */
558	movq	REGOFF_RDI(%rsp), %rdi
559	movq	REGOFF_RSI(%rsp), %rsi
560	movq	REGOFF_RDX(%rsp), %rdx
561	/* %rcx used to restore %rip value */
562
563	movq	REGOFF_R8(%rsp), %r8
564	movq	REGOFF_R9(%rsp), %r9
565	movq	REGOFF_RAX(%rsp), %rax
566	movq	REGOFF_RBX(%rsp), %rbx
567
568	movq	REGOFF_RBP(%rsp), %rbp
569	movq	REGOFF_R10(%rsp), %r10
570	/* %r11 used to restore %rfl value */
571	movq	REGOFF_R12(%rsp), %r12
572
573	movq	REGOFF_R13(%rsp), %r13
574	movq	REGOFF_R14(%rsp), %r14
575	movq	REGOFF_R15(%rsp), %r15
576
577	movq	REGOFF_RIP(%rsp), %rcx
578	movl	REGOFF_RFL(%rsp), %r11d
579
580#if defined(__xpv)
581	addq	$REGOFF_RIP, %rsp
582#else
583	movq	REGOFF_RSP(%rsp), %rsp
584#endif
585
586        /*
587         * There can be no instructions between the ALTENTRY below and
588	 * SYSRET or we could end up breaking brand support. See label usage
589         * in sn1_brand_syscall_callback for an example.
590         */
591	ASSERT_UPCALL_MASK_IS_SET
592        ALTENTRY(nopop_sys_syscall_swapgs_sysretq)
593	SWAPGS				/* user gsbase */
594	SYSRETQ
595        /*NOTREACHED*/
596        SET_SIZE(nopop_sys_syscall_swapgs_sysretq)
597
598_syscall_pre:
599	call	pre_syscall
600	movl	%eax, %r12d
601	testl	%eax, %eax
602	jne	_syscall_post_call
603	/*
604	 * Didn't abort, so reload the syscall args and invoke the handler.
605	 */
606	movzwl	T_SYSNUM(%r15), %eax
607	jmp	_syscall_invoke
608
609_syscall_ill:
610	call	nosys
611	movq	%rax, %r12
612	movq	%rdx, %r13
613	jmp	_syscall_post_call
614
615_syscall_post:
616	STI
617	/*
618	 * Sigh, our optimism wasn't justified, put it back to LMS_SYSTEM
619	 * so that we can account for the extra work it takes us to finish.
620	 */
621	MSTATE_TRANSITION(LMS_USER, LMS_SYSTEM)
622_syscall_post_call:
623	movq	%r12, %rdi
624	movq	%r13, %rsi
625	call	post_syscall
626	MSTATE_TRANSITION(LMS_SYSTEM, LMS_USER)
627	jmp	_sys_rtt
628	SET_SIZE(sys_syscall)
629	SET_SIZE(brand_sys_syscall)
630
631#endif	/* __lint */
632
633#if defined(__lint)
634
635/*ARGSUSED*/
636void
637sys_syscall32()
638{}
639
640#else	/* __lint */
641
642	ENTRY_NP(brand_sys_syscall32)
643	SWAPGS				/* kernel gsbase */
644	XPV_TRAP_POP
645	BRAND_CALLBACK(BRAND_CB_SYSCALL32)
646	SWAPGS				/* user gsbase */
647
648#if defined(__xpv)
649	jmp	nopop_sys_syscall32
650#endif
651
652	ALTENTRY(sys_syscall32)
653	SWAPGS				/* kernel gsbase */
654
655#if defined(__xpv)
656	XPV_TRAP_POP
657nopop_sys_syscall32:
658#endif
659
660	movl	%esp, %r10d
661	movq	%gs:CPU_THREAD, %r15
662	movq	T_STACK(%r15), %rsp
663	movl	%eax, %eax
664
665	movl	$U32CS_SEL, REGOFF_CS(%rsp)
666	movl	%ecx, REGOFF_RIP(%rsp)		/* syscall: %rip -> %rcx */
667	movq	%r11, REGOFF_RFL(%rsp)		/* syscall: %rfl -> %r11d */
668	movq	%r10, REGOFF_RSP(%rsp)
669	movl	$UDS_SEL, REGOFF_SS(%rsp)
670
671_syscall32_save:
672	movl	%edi, REGOFF_RDI(%rsp)
673	movl	%esi, REGOFF_RSI(%rsp)
674	movl	%ebp, REGOFF_RBP(%rsp)
675	movl	%ebx, REGOFF_RBX(%rsp)
676	movl	%edx, REGOFF_RDX(%rsp)
677	movl	%ecx, REGOFF_RCX(%rsp)
678	movl	%eax, REGOFF_RAX(%rsp)		/* wrapper: sysc# -> %eax */
679	movq	$0, REGOFF_SAVFP(%rsp)
680	movq	$0, REGOFF_SAVPC(%rsp)
681
682	/*
683	 * Copy these registers here in case we end up stopped with
684	 * someone (like, say, /proc) messing with our register state.
685	 * We don't -restore- them unless we have to in update_sregs.
686	 *
687	 * Since userland -can't- change fsbase or gsbase directly,
688	 * we don't bother to capture them here.
689	 */
690	xorl	%ebx, %ebx
691	movw	%ds, %bx
692	movq	%rbx, REGOFF_DS(%rsp)
693	movw	%es, %bx
694	movq	%rbx, REGOFF_ES(%rsp)
695	movw	%fs, %bx
696	movq	%rbx, REGOFF_FS(%rsp)
697	movw	%gs, %bx
698	movq	%rbx, REGOFF_GS(%rsp)
699
700	/*
701	 * Application state saved in the regs structure on the stack
702	 * %eax is the syscall number
703	 * %rsp is the thread's stack, %r15 is curthread
704	 * REG_RSP(%rsp) is the user's stack
705	 */
706
707	SYSCALL_TRAPTRACE32($TT_SYSC)
708
709	movq	%rsp, %rbp
710
711	movq	T_LWP(%r15), %r14
712	ASSERT_NO_RUPDATE_PENDING(%r14)
713
714	ENABLE_INTR_FLAGS
715
716	MSTATE_TRANSITION(LMS_USER, LMS_SYSTEM)
717	movl	REGOFF_RAX(%rsp), %eax	/* (%rax damaged by mstate call) */
718
719	ASSERT_LWPTOREGS(%r14, %rsp)
720
721	incq	 %gs:CPU_STATS_SYS_SYSCALL
722
723	/*
724	 * Make some space for MAXSYSARGS (currently 8) 32-bit args placed
725	 * into 64-bit (long) arg slots, maintaining 16 byte alignment.  Or
726	 * more succinctly:
727	 *
728	 *	SA(MAXSYSARGS * sizeof (long)) == 64
729	 */
730#define	SYS_DROP	64			/* drop for args */
731	subq	$SYS_DROP, %rsp
732	movb	$LWP_SYS, LWP_STATE(%r14)
733	movq	%r15, %rdi
734	movq	%rsp, %rsi
735	call	syscall_entry
736
737	/*
738	 * Fetch the arguments copied onto the kernel stack and put
739	 * them in the right registers to invoke a C-style syscall handler.
740	 * %rax contains the handler address.
741	 *
742	 * Ideas for making all this go faster of course include simply
743	 * forcibly fetching 6 arguments from the user stack under lofault
744	 * protection, reverting to copyin_args only when watchpoints
745	 * are in effect.
746	 *
747	 * (If we do this, make sure that exec and libthread leave
748	 * enough space at the top of the stack to ensure that we'll
749	 * never do a fetch from an invalid page.)
750	 *
751	 * Lots of ideas here, but they won't really help with bringup B-)
752	 * Correctness can't wait, performance can wait a little longer ..
753	 */
754
755	movq	%rax, %rbx
756	movl	0(%rsp), %edi
757	movl	8(%rsp), %esi
758	movl	0x10(%rsp), %edx
759	movl	0x18(%rsp), %ecx
760	movl	0x20(%rsp), %r8d
761	movl	0x28(%rsp), %r9d
762
763	call	*SY_CALLC(%rbx)
764
765	movq	%rbp, %rsp	/* pop the args */
766
767	/*
768	 * amd64 syscall handlers -always- return a 64-bit value in %rax.
769	 * On the 32-bit kernel, they always return that value in %eax:%edx
770	 * as required by the 32-bit ABI.
771	 *
772	 * Simulate the same behaviour by unconditionally splitting the
773	 * return value in the same way.
774	 */
775	movq	%rax, %r13
776	shrq	$32, %r13	/* upper 32-bits into %edx */
777	movl	%eax, %r12d	/* lower 32-bits into %eax */
778
779	/*
780	 * Optimistically assume that there's no post-syscall
781	 * work to do.  (This is to avoid having to call syscall_mstate()
782	 * with interrupts disabled)
783	 */
784	MSTATE_TRANSITION(LMS_SYSTEM, LMS_USER)
785
786	/*
787	 * We must protect ourselves from being descheduled here;
788	 * If we were, and we ended up on another cpu, or another
789	 * lwp got in ahead of us, it could change the segment
790	 * registers without us noticing before we return to userland.
791	 */
792	CLI(%r14)
793	CHECK_POSTSYS_NE(%r15, %r14, %ebx)
794	jne	_full_syscall_postsys32
795	SIMPLE_SYSCALL_POSTSYS(%r15, %r14, %bx)
796
797	/*
798	 * To get back to userland, we need to put the return %rip in %rcx and
799	 * the return %rfl in %r11d.  The sysret instruction also arranges
800	 * to fix up %cs and %ss; everything else is our responsibility.
801	 */
802
803	movl	%r12d, %eax			/* %eax: rval1 */
804	movl	REGOFF_RBX(%rsp), %ebx
805	/* %ecx used for return pointer */
806	movl	%r13d, %edx			/* %edx: rval2 */
807	movl	REGOFF_RBP(%rsp), %ebp
808	movl	REGOFF_RSI(%rsp), %esi
809	movl	REGOFF_RDI(%rsp), %edi
810
811	movl	REGOFF_RFL(%rsp), %r11d		/* %r11 -> eflags */
812	movl	REGOFF_RIP(%rsp), %ecx		/* %ecx -> %eip */
813	movl	REGOFF_RSP(%rsp), %esp
814
815	ASSERT_UPCALL_MASK_IS_SET
816        ALTENTRY(nopop_sys_syscall32_swapgs_sysretl)
817	SWAPGS				/* user gsbase */
818	SYSRETL
819        SET_SIZE(nopop_sys_syscall32_swapgs_sysretl)
820	/*NOTREACHED*/
821
822_full_syscall_postsys32:
823	STI
824	/*
825	 * Sigh, our optimism wasn't justified, put it back to LMS_SYSTEM
826	 * so that we can account for the extra work it takes us to finish.
827	 */
828	MSTATE_TRANSITION(LMS_USER, LMS_SYSTEM)
829	movq	%r15, %rdi
830	movq	%r12, %rsi			/* rval1 - %eax */
831	movq	%r13, %rdx			/* rval2 - %edx */
832	call	syscall_exit
833	MSTATE_TRANSITION(LMS_SYSTEM, LMS_USER)
834	jmp	_sys_rtt
835	SET_SIZE(sys_syscall32)
836	SET_SIZE(brand_sys_syscall32)
837
838#endif	/* __lint */
839
840/*
841 * System call handler via the sysenter instruction
842 * Used only for 32-bit system calls on the 64-bit kernel.
843 *
844 * The caller in userland has arranged that:
845 *
846 * -	%eax contains the syscall number
847 * -	%ecx contains the user %esp
848 * -	%edx contains the return %eip
849 * -	the user stack contains the args to the syscall
850 *
851 * Hardware and (privileged) initialization code have arranged that by
852 * the time the sysenter instructions completes:
853 *
854 * - %rip is pointing to sys_sysenter (below).
855 * - %cs and %ss are set to kernel text and stack (data) selectors.
856 * - %rsp is pointing at the lwp's stack
857 * - interrupts have been disabled.
858 *
859 * Note that we are unable to return both "rvals" to userland with
860 * this call, as %edx is used by the sysexit instruction.
861 *
862 * One final complication in this routine is its interaction with
863 * single-stepping in a debugger.  For most of the system call mechanisms,
864 * the CPU automatically clears the single-step flag before we enter the
865 * kernel.  The sysenter mechanism does not clear the flag, so a user
866 * single-stepping through a libc routine may suddenly find him/herself
867 * single-stepping through the kernel.  To detect this, kmdb compares the
868 * trap %pc to the [brand_]sys_enter addresses on each single-step trap.
869 * If it finds that we have single-stepped to a sysenter entry point, it
870 * explicitly clears the flag and executes the sys_sysenter routine.
871 *
872 * One final complication in this final complication is the fact that we
873 * have two different entry points for sysenter: brand_sys_sysenter and
874 * sys_sysenter.  If we enter at brand_sys_sysenter and start single-stepping
875 * through the kernel with kmdb, we will eventually hit the instruction at
876 * sys_sysenter.  kmdb cannot distinguish between that valid single-step
877 * and the undesirable one mentioned above.  To avoid this situation, we
878 * simply add a jump over the instruction at sys_sysenter to make it
879 * impossible to single-step to it.
880 */
881#if defined(__lint)
882
883void
884sys_sysenter()
885{}
886
887#else	/* __lint */
888
889	ENTRY_NP(brand_sys_sysenter)
890	SWAPGS				/* kernel gsbase */
891	ALTENTRY(_brand_sys_sysenter_post_swapgs)
892	BRAND_CALLBACK(BRAND_CB_SYSENTER)
893	/*
894	 * Jump over sys_sysenter to allow single-stepping as described
895	 * above.
896	 */
897	jmp	_sys_sysenter_post_swapgs
898
899	ALTENTRY(sys_sysenter)
900	SWAPGS				/* kernel gsbase */
901
902	ALTENTRY(_sys_sysenter_post_swapgs)
903	movq	%gs:CPU_THREAD, %r15
904
905	movl	$U32CS_SEL, REGOFF_CS(%rsp)
906	movl	%ecx, REGOFF_RSP(%rsp)		/* wrapper: %esp -> %ecx */
907	movl	%edx, REGOFF_RIP(%rsp)		/* wrapper: %eip -> %edx */
908	pushfq
909	popq	%r10
910	movl	$UDS_SEL, REGOFF_SS(%rsp)
911
912	/*
913	 * Set the interrupt flag before storing the flags to the
914	 * flags image on the stack so we can return to user with
915	 * interrupts enabled if we return via sys_rtt_syscall32
916	 */
917	orq	$PS_IE, %r10
918	movq	%r10, REGOFF_RFL(%rsp)
919
920	movl	%edi, REGOFF_RDI(%rsp)
921	movl	%esi, REGOFF_RSI(%rsp)
922	movl	%ebp, REGOFF_RBP(%rsp)
923	movl	%ebx, REGOFF_RBX(%rsp)
924	movl	%edx, REGOFF_RDX(%rsp)
925	movl	%ecx, REGOFF_RCX(%rsp)
926	movl	%eax, REGOFF_RAX(%rsp)		/* wrapper: sysc# -> %eax */
927	movq	$0, REGOFF_SAVFP(%rsp)
928	movq	$0, REGOFF_SAVPC(%rsp)
929
930	/*
931	 * Copy these registers here in case we end up stopped with
932	 * someone (like, say, /proc) messing with our register state.
933	 * We don't -restore- them unless we have to in update_sregs.
934	 *
935	 * Since userland -can't- change fsbase or gsbase directly,
936	 * we don't bother to capture them here.
937	 */
938	xorl	%ebx, %ebx
939	movw	%ds, %bx
940	movq	%rbx, REGOFF_DS(%rsp)
941	movw	%es, %bx
942	movq	%rbx, REGOFF_ES(%rsp)
943	movw	%fs, %bx
944	movq	%rbx, REGOFF_FS(%rsp)
945	movw	%gs, %bx
946	movq	%rbx, REGOFF_GS(%rsp)
947
948	/*
949	 * Application state saved in the regs structure on the stack
950	 * %eax is the syscall number
951	 * %rsp is the thread's stack, %r15 is curthread
952	 * REG_RSP(%rsp) is the user's stack
953	 */
954
955	SYSCALL_TRAPTRACE($TT_SYSENTER)
956
957	movq	%rsp, %rbp
958
959	movq	T_LWP(%r15), %r14
960	ASSERT_NO_RUPDATE_PENDING(%r14)
961
962	ENABLE_INTR_FLAGS
963
964	/*
965	 * Catch 64-bit process trying to issue sysenter instruction
966	 * on Nocona based systems.
967	 */
968	movq	LWP_PROCP(%r14), %rax
969	cmpq	$DATAMODEL_ILP32, P_MODEL(%rax)
970	je	7f
971
972	/*
973	 * For a non-32-bit process, simulate a #ud, since that's what
974	 * native hardware does.  The traptrace entry (above) will
975	 * let you know what really happened.
976	 */
977	movq	$T_ILLINST, REGOFF_TRAPNO(%rsp)
978	movq	REGOFF_CS(%rsp), %rdi
979	movq	%rdi, REGOFF_ERR(%rsp)
980	movq	%rsp, %rdi
981	movq	REGOFF_RIP(%rsp), %rsi
982	movl	%gs:CPU_ID, %edx
983	call	trap
984	jmp	_sys_rtt
9857:
986
987	MSTATE_TRANSITION(LMS_USER, LMS_SYSTEM)
988	movl	REGOFF_RAX(%rsp), %eax	/* (%rax damaged by mstate calls) */
989
990	ASSERT_LWPTOREGS(%r14, %rsp)
991
992	incq	%gs:CPU_STATS_SYS_SYSCALL
993
994	/*
995	 * Make some space for MAXSYSARGS (currently 8) 32-bit args
996	 * placed into 64-bit (long) arg slots, plus one 64-bit
997	 * (long) arg count, maintaining 16 byte alignment.
998	 */
999	subq	$SYS_DROP, %rsp
1000	movb	$LWP_SYS, LWP_STATE(%r14)
1001	movq	%r15, %rdi
1002	movq	%rsp, %rsi
1003	call	syscall_entry
1004
1005	/*
1006	 * Fetch the arguments copied onto the kernel stack and put
1007	 * them in the right registers to invoke a C-style syscall handler.
1008	 * %rax contains the handler address.
1009	 */
1010	movq	%rax, %rbx
1011	movl	0(%rsp), %edi
1012	movl	8(%rsp), %esi
1013	movl	0x10(%rsp), %edx
1014	movl	0x18(%rsp), %ecx
1015	movl	0x20(%rsp), %r8d
1016	movl	0x28(%rsp), %r9d
1017
1018	call	*SY_CALLC(%rbx)
1019
1020	movq	%rbp, %rsp	/* pop the args */
1021
1022	/*
1023	 * amd64 syscall handlers -always- return a 64-bit value in %rax.
1024	 * On the 32-bit kernel, the always return that value in %eax:%edx
1025	 * as required by the 32-bit ABI.
1026	 *
1027	 * Simulate the same behaviour by unconditionally splitting the
1028	 * return value in the same way.
1029	 */
1030	movq	%rax, %r13
1031	shrq	$32, %r13	/* upper 32-bits into %edx */
1032	movl	%eax, %r12d	/* lower 32-bits into %eax */
1033
1034	/*
1035	 * Optimistically assume that there's no post-syscall
1036	 * work to do.  (This is to avoid having to call syscall_mstate()
1037	 * with interrupts disabled)
1038	 */
1039	MSTATE_TRANSITION(LMS_SYSTEM, LMS_USER)
1040
1041	/*
1042	 * We must protect ourselves from being descheduled here;
1043	 * If we were, and we ended up on another cpu, or another
1044	 * lwp got int ahead of us, it could change the segment
1045	 * registers without us noticing before we return to userland.
1046	 */
1047	cli
1048	CHECK_POSTSYS_NE(%r15, %r14, %ebx)
1049	jne	_full_syscall_postsys32
1050	SIMPLE_SYSCALL_POSTSYS(%r15, %r14, %bx)
1051
1052	/*
1053	 * To get back to userland, load up the 32-bit registers and
1054	 * sysexit back where we came from.
1055	 */
1056
1057	/*
1058	 * Interrupts will be turned on by the 'sti' executed just before
1059	 * sysexit.  The following ensures that restoring the user's rflags
1060	 * doesn't enable interrupts too soon.
1061	 */
1062	andq	$_BITNOT(PS_IE), REGOFF_RFL(%rsp)
1063
1064	/*
1065	 * (There's no point in loading up %edx because the sysexit
1066	 * mechanism smashes it.)
1067	 */
1068	movl	%r12d, %eax
1069	movl	REGOFF_RBX(%rsp), %ebx
1070	movl	REGOFF_RBP(%rsp), %ebp
1071	movl	REGOFF_RSI(%rsp), %esi
1072	movl	REGOFF_RDI(%rsp), %edi
1073
1074	movl	REGOFF_RIP(%rsp), %edx	/* sysexit: %edx -> %eip */
1075	pushq	REGOFF_RFL(%rsp)
1076	popfq
1077	movl	REGOFF_RSP(%rsp), %ecx	/* sysexit: %ecx -> %esp */
1078        ALTENTRY(sys_sysenter_swapgs_sysexit)
1079	swapgs
1080	sti
1081	sysexit
1082	SET_SIZE(sys_sysenter_swapgs_sysexit)
1083	SET_SIZE(sys_sysenter)
1084	SET_SIZE(_sys_sysenter_post_swapgs)
1085	SET_SIZE(brand_sys_sysenter)
1086
1087#endif	/* __lint */
1088
1089#if defined(__lint)
1090/*
1091 * System call via an int80.  This entry point is only used by the Linux
1092 * application environment.  Unlike the other entry points, there is no
1093 * default action to take if no callback is registered for this process.
1094 */
1095void
1096sys_int80()
1097{}
1098
1099#else	/* __lint */
1100
1101	ENTRY_NP(brand_sys_int80)
1102	SWAPGS				/* kernel gsbase */
1103	XPV_TRAP_POP
1104	BRAND_CALLBACK(BRAND_CB_INT80)
1105	SWAPGS				/* user gsbase */
1106#if defined(__xpv)
1107	jmp	nopop_int80
1108#endif
1109
1110	ENTRY_NP(sys_int80)
1111	/*
1112	 * We hit an int80, but this process isn't of a brand with an int80
1113	 * handler.  Bad process!  Make it look as if the INT failed.
1114	 * Modify %rip to point before the INT, push the expected error
1115	 * code and fake a GP fault. Note on 64-bit hypervisor we need
1116	 * to undo the XPV_TRAP_POP and push rcx and r11 back on the stack
1117	 * because gptrap will pop them again with its own XPV_TRAP_POP.
1118	 */
1119#if defined(__xpv)
1120	XPV_TRAP_POP
1121nopop_int80:
1122#endif
1123	subq	$2, (%rsp)	/* int insn 2-bytes */
1124	pushq	$_CONST(_MUL(T_INT80, GATE_DESC_SIZE) + 2)
1125#if defined(__xpv)
1126	push	%r11
1127	push	%rcx
1128#endif
1129	jmp	gptrap			/ GP fault
1130	SET_SIZE(sys_int80)
1131	SET_SIZE(brand_sys_int80)
1132#endif	/* __lint */
1133
1134
1135/*
1136 * This is the destination of the "int $T_SYSCALLINT" interrupt gate, used by
1137 * the generic i386 libc to do system calls. We do a small amount of setup
1138 * before jumping into the existing sys_syscall32 path.
1139 */
1140#if defined(__lint)
1141
1142/*ARGSUSED*/
1143void
1144sys_syscall_int()
1145{}
1146
1147#else	/* __lint */
1148
1149	ENTRY_NP(brand_sys_syscall_int)
1150	SWAPGS				/* kernel gsbase */
1151	XPV_TRAP_POP
1152	BRAND_CALLBACK(BRAND_CB_INT91)
1153	SWAPGS				/* user gsbase */
1154
1155#if defined(__xpv)
1156	jmp	nopop_syscall_int
1157#endif
1158
1159	ALTENTRY(sys_syscall_int)
1160	SWAPGS				/* kernel gsbase */
1161
1162#if defined(__xpv)
1163	XPV_TRAP_POP
1164nopop_syscall_int:
1165#endif
1166
1167	movq	%gs:CPU_THREAD, %r15
1168	movq	T_STACK(%r15), %rsp
1169	movl	%eax, %eax
1170	/*
1171	 * Set t_post_sys on this thread to force ourselves out via the slow
1172	 * path. It might be possible at some later date to optimize this out
1173	 * and use a faster return mechanism.
1174	 */
1175	movb	$1, T_POST_SYS(%r15)
1176	CLEAN_CS
1177	jmp	_syscall32_save
1178	/*
1179	 * There should be no instructions between this label and SWAPGS/IRET
1180	 * or we could end up breaking branded zone support. See the usage of
1181	 * this label in lx_brand_int80_callback and sn1_brand_int91_callback
1182	 * for examples.
1183	 */
1184        ALTENTRY(sys_sysint_swapgs_iret)
1185	SWAPGS				/* user gsbase */
1186	IRET
1187	/*NOTREACHED*/
1188	SET_SIZE(sys_sysint_swapgs_iret)
1189	SET_SIZE(sys_syscall_int)
1190	SET_SIZE(brand_sys_syscall_int)
1191
1192#endif	/* __lint */
1193
1194/*
1195 * Legacy 32-bit applications and old libc implementations do lcalls;
1196 * we should never get here because the LDT entry containing the syscall
1197 * segment descriptor has the "segment present" bit cleared, which means
1198 * we end up processing those system calls in trap() via a not-present trap.
1199 *
1200 * We do it this way because a call gate unhelpfully does -nothing- to the
1201 * interrupt flag bit, so an interrupt can run us just after the lcall
1202 * completes, but just before the swapgs takes effect.   Thus the INTR_PUSH and
1203 * INTR_POP paths would have to be slightly more complex to dance around
1204 * this problem, and end up depending explicitly on the first
1205 * instruction of this handler being either swapgs or cli.
1206 */
1207
1208#if defined(__lint)
1209
1210/*ARGSUSED*/
1211void
1212sys_lcall32()
1213{}
1214
1215#else	/* __lint */
1216
1217	ENTRY_NP(sys_lcall32)
1218	SWAPGS				/* kernel gsbase */
1219	pushq	$0
1220	pushq	%rbp
1221	movq	%rsp, %rbp
1222	leaq	__lcall_panic_str(%rip), %rdi
1223	xorl	%eax, %eax
1224	call	panic
1225	SET_SIZE(sys_lcall32)
1226
1227__lcall_panic_str:
1228	.string	"sys_lcall32: shouldn't be here!"
1229
1230/*
1231 * Declare a uintptr_t which covers the entire pc range of syscall
1232 * handlers for the stack walkers that need this.
1233 */
1234	.align	CPTRSIZE
1235	.globl	_allsyscalls_size
1236	.type	_allsyscalls_size, @object
1237_allsyscalls_size:
1238	.NWORD	. - _allsyscalls
1239	SET_SIZE(_allsyscalls_size)
1240
1241#endif	/* __lint */
1242
1243/*
1244 * These are the thread context handlers for lwps using sysenter/sysexit.
1245 */
1246
1247#if defined(__lint)
1248
1249/*ARGSUSED*/
1250void
1251sep_save(void *ksp)
1252{}
1253
1254/*ARGSUSED*/
1255void
1256sep_restore(void *ksp)
1257{}
1258
1259#else	/* __lint */
1260
1261	/*
1262	 * setting this value to zero as we switch away causes the
1263	 * stack-pointer-on-sysenter to be NULL, ensuring that we
1264	 * don't silently corrupt another (preempted) thread stack
1265	 * when running an lwp that (somehow) didn't get sep_restore'd
1266	 */
1267	ENTRY_NP(sep_save)
1268	xorl	%edx, %edx
1269	xorl	%eax, %eax
1270	movl	$MSR_INTC_SEP_ESP, %ecx
1271	wrmsr
1272	ret
1273	SET_SIZE(sep_save)
1274
1275	/*
1276	 * Update the kernel stack pointer as we resume onto this cpu.
1277	 */
1278	ENTRY_NP(sep_restore)
1279	movq	%rdi, %rdx
1280	shrq	$32, %rdx
1281	movl	%edi, %eax
1282	movl	$MSR_INTC_SEP_ESP, %ecx
1283	wrmsr
1284	ret
1285	SET_SIZE(sep_restore)
1286
1287#endif	/* __lint */
1288