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