1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #ifndef	_LIBC_AMD64_INC_SYS_H
28*0Sstevel@tonic-gate #define	_LIBC_AMD64_INC_SYS_H
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate /*
33*0Sstevel@tonic-gate  * This file defines common code sequences for system calls.
34*0Sstevel@tonic-gate  */
35*0Sstevel@tonic-gate #include <sys/asm_linkage.h>
36*0Sstevel@tonic-gate #include <sys/syscall.h>
37*0Sstevel@tonic-gate #include <sys/errno.h>
38*0Sstevel@tonic-gate #include "synonyms.h"
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #undef	syscall		/* override synonyms.h */
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate /*
43*0Sstevel@tonic-gate  * XX64 -- the SOS9 assembler doesn't recognize the 'syscall' instruction yet.
44*0Sstevel@tonic-gate  * We compensate by defining the byte sequence here.
45*0Sstevel@tonic-gate  */
46*0Sstevel@tonic-gate #define	syscall	.byte 0xf, 0x5
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate #define	_fref_(name)	name@PLT
49*0Sstevel@tonic-gate #define	_daref_(name)	name@GOTPCREL(%rip)
50*0Sstevel@tonic-gate #define	_sref_(name)	name(%rip)
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate /*
53*0Sstevel@tonic-gate  * Define the external symbol __cerror for all files.
54*0Sstevel@tonic-gate  */
55*0Sstevel@tonic-gate 	.globl	__cerror
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate /*
58*0Sstevel@tonic-gate  * __SYSCALL provides the basic trap sequence.  It assumes that
59*0Sstevel@tonic-gate  * an entry of the form SYS_name exists (from sys/syscall.h).
60*0Sstevel@tonic-gate  * Note that %rcx is smashed by the syscall instruction,
61*0Sstevel@tonic-gate  * so we move it to %r10 in order to pass it to the kernel.
62*0Sstevel@tonic-gate  */
63*0Sstevel@tonic-gate #define	__SYSCALL(name)			\
64*0Sstevel@tonic-gate 	movq	%rcx, %r10;		\
65*0Sstevel@tonic-gate 	/* CSTYLED */			\
66*0Sstevel@tonic-gate 	movl	$SYS_/**/name, %eax;	\
67*0Sstevel@tonic-gate 	syscall
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate #define	SYSTRAP_RVAL1(name)	__SYSCALL(name)
70*0Sstevel@tonic-gate #define	SYSTRAP_RVAL2(name)	__SYSCALL(name)
71*0Sstevel@tonic-gate #define	SYSTRAP_2RVALS(name)	__SYSCALL(name)
72*0Sstevel@tonic-gate #define	SYSTRAP_64RVAL(name)	__SYSCALL(name)
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate /*
75*0Sstevel@tonic-gate  * SYSFASTTRAP provides the fast system call trap sequence.  It assumes
76*0Sstevel@tonic-gate  * that an entry of the form T_name exists (probably from sys/trap.h).
77*0Sstevel@tonic-gate  */
78*0Sstevel@tonic-gate #define	SYSFASTTRAP(name)		\
79*0Sstevel@tonic-gate 	/* CSTYLED */			\
80*0Sstevel@tonic-gate 	movl	$T_/**/name, %eax;	\
81*0Sstevel@tonic-gate 	int	$T_FASTTRAP
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate /*
84*0Sstevel@tonic-gate  * SYSCERROR provides the sequence to branch to __cerror if an error is
85*0Sstevel@tonic-gate  * indicated by the carry-bit being set upon return from a trap.
86*0Sstevel@tonic-gate  */
87*0Sstevel@tonic-gate #define	SYSCERROR		\
88*0Sstevel@tonic-gate 	jb	__cerror
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate /*
91*0Sstevel@tonic-gate  * SYSLWPERR provides the sequence to return 0 on a successful trap
92*0Sstevel@tonic-gate  * and the error code if unsuccessful.
93*0Sstevel@tonic-gate  * Error is indicated by the carry-bit being set upon return from a trap.
94*0Sstevel@tonic-gate  */
95*0Sstevel@tonic-gate #define	SYSLWPERR			\
96*0Sstevel@tonic-gate 	jae	1f;			\
97*0Sstevel@tonic-gate 	cmpl	$ERESTART, %eax;	\
98*0Sstevel@tonic-gate 	jne	2f;			\
99*0Sstevel@tonic-gate 	movl	$EINTR, %eax;		\
100*0Sstevel@tonic-gate 	jmp	2f;			\
101*0Sstevel@tonic-gate 1:					\
102*0Sstevel@tonic-gate 	xorq	%rax, %rax;		\
103*0Sstevel@tonic-gate 2:
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate /*
106*0Sstevel@tonic-gate  * SYSREENTRY provides the entry sequence for restartable system calls.
107*0Sstevel@tonic-gate  */
108*0Sstevel@tonic-gate #define	SYSREENTRY(name)	\
109*0Sstevel@tonic-gate 	ENTRY(name);		\
110*0Sstevel@tonic-gate 1:
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate /*
113*0Sstevel@tonic-gate  * SYSRESTART provides the error handling sequence for restartable
114*0Sstevel@tonic-gate  * system calls.
115*0Sstevel@tonic-gate  * XX64 -- Are all of the argument registers restored to their
116*0Sstevel@tonic-gate  * original values on an ERESTART return (including %rcx)?
117*0Sstevel@tonic-gate  */
118*0Sstevel@tonic-gate #define	SYSRESTART(name)		\
119*0Sstevel@tonic-gate 	jae	1f;			\
120*0Sstevel@tonic-gate 	cmpl	$ERESTART, %eax;	\
121*0Sstevel@tonic-gate 	je	1b;			\
122*0Sstevel@tonic-gate 	jmp	__cerror;		\
123*0Sstevel@tonic-gate 1:
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate /*
126*0Sstevel@tonic-gate  * SYSINTR_RESTART provides the error handling sequence for restartable
127*0Sstevel@tonic-gate  * system calls in case of EINTR or ERESTART.
128*0Sstevel@tonic-gate  */
129*0Sstevel@tonic-gate #define	SYSINTR_RESTART(name)		\
130*0Sstevel@tonic-gate 	jae	1f;			\
131*0Sstevel@tonic-gate 	cmpl	$ERESTART, %eax;	\
132*0Sstevel@tonic-gate 	je	1b;			\
133*0Sstevel@tonic-gate 	cmpl	$EINTR, %eax;		\
134*0Sstevel@tonic-gate 	je	1b;			\
135*0Sstevel@tonic-gate 	jmp	2f;			\
136*0Sstevel@tonic-gate 1:					\
137*0Sstevel@tonic-gate 	xorq	%rax, %rax;		\
138*0Sstevel@tonic-gate 2:
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate /*
141*0Sstevel@tonic-gate  * SYSCALL provides the standard (i.e.: most common) system call sequence.
142*0Sstevel@tonic-gate  */
143*0Sstevel@tonic-gate #define	SYSCALL(name)			\
144*0Sstevel@tonic-gate 	ENTRY(name);			\
145*0Sstevel@tonic-gate 	SYSTRAP_2RVALS(name);		\
146*0Sstevel@tonic-gate 	SYSCERROR
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate #define	SYSCALL_RVAL1(name)		\
149*0Sstevel@tonic-gate 	ENTRY(name);			\
150*0Sstevel@tonic-gate 	SYSTRAP_RVAL1(name);		\
151*0Sstevel@tonic-gate 	SYSCERROR
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate /*
154*0Sstevel@tonic-gate  * SYSCALL64 provides the standard (i.e.: most common) system call sequence
155*0Sstevel@tonic-gate  * for system calls that return 64-bit values.
156*0Sstevel@tonic-gate  */
157*0Sstevel@tonic-gate #define	SYSCALL64(name)			\
158*0Sstevel@tonic-gate 	SYSCALL(name)			\
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate /*
161*0Sstevel@tonic-gate  * SYSCALL_RESTART provides the most common restartable system call sequence.
162*0Sstevel@tonic-gate  */
163*0Sstevel@tonic-gate #define	SYSCALL_RESTART(name)		\
164*0Sstevel@tonic-gate 	SYSREENTRY(name);		\
165*0Sstevel@tonic-gate 	SYSTRAP_2RVALS(name);		\
166*0Sstevel@tonic-gate 	SYSRESTART()
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate #define	SYSCALL_RESTART_RVAL1(name)	\
169*0Sstevel@tonic-gate 	SYSREENTRY(name);		\
170*0Sstevel@tonic-gate 	SYSTRAP_RVAL1(name);		\
171*0Sstevel@tonic-gate 	SYSRESTART()
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate /*
174*0Sstevel@tonic-gate  * SYSCALL2 provides a common system call sequence when the entry name
175*0Sstevel@tonic-gate  * is different than the trap name.
176*0Sstevel@tonic-gate  */
177*0Sstevel@tonic-gate #define	SYSCALL2(entryname, trapname)	\
178*0Sstevel@tonic-gate 	ENTRY(entryname);		\
179*0Sstevel@tonic-gate 	SYSTRAP_2RVALS(trapname);	\
180*0Sstevel@tonic-gate 	SYSCERROR
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate #define	SYSCALL2_RVAL1(entryname, trapname)	\
183*0Sstevel@tonic-gate 	ENTRY(entryname);			\
184*0Sstevel@tonic-gate 	SYSTRAP_RVAL1(trapname);		\
185*0Sstevel@tonic-gate 	SYSCERROR
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate /*
188*0Sstevel@tonic-gate  * SYSCALL2_RESTART provides a common restartable system call sequence when the
189*0Sstevel@tonic-gate  * entry name is different than the trap name.
190*0Sstevel@tonic-gate  */
191*0Sstevel@tonic-gate #define	SYSCALL2_RESTART(entryname, trapname)	\
192*0Sstevel@tonic-gate 	SYSREENTRY(entryname);			\
193*0Sstevel@tonic-gate 	SYSTRAP_2RVALS(trapname);		\
194*0Sstevel@tonic-gate 	SYSRESTART()
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate #define	SYSCALL2_RESTART_RVAL1(entryname, trapname)	\
197*0Sstevel@tonic-gate 	SYSREENTRY(entryname);				\
198*0Sstevel@tonic-gate 	SYSTRAP_RVAL1(trapname);			\
199*0Sstevel@tonic-gate 	SYSRESTART()
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate /*
202*0Sstevel@tonic-gate  * SYSCALL_NOERROR provides the most common system call sequence for those
203*0Sstevel@tonic-gate  * system calls which don't check the error reture (carry bit).
204*0Sstevel@tonic-gate  */
205*0Sstevel@tonic-gate #define	SYSCALL_NOERROR(name)		\
206*0Sstevel@tonic-gate 	ENTRY(name);			\
207*0Sstevel@tonic-gate 	SYSTRAP_2RVALS(name)
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate #define	SYSCALL_NOERROR_RVAL1(name)	\
210*0Sstevel@tonic-gate 	ENTRY(name);			\
211*0Sstevel@tonic-gate 	SYSTRAP_RVAL1(name)
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate /*
214*0Sstevel@tonic-gate  * Standard syscall return sequence, return code equal to rval1.
215*0Sstevel@tonic-gate  */
216*0Sstevel@tonic-gate #define	RET			\
217*0Sstevel@tonic-gate 	ret
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate /*
220*0Sstevel@tonic-gate  * Syscall return sequence, return code equal to rval2.
221*0Sstevel@tonic-gate  */
222*0Sstevel@tonic-gate #define	RET2			\
223*0Sstevel@tonic-gate 	movq	%rdx, %rax;	\
224*0Sstevel@tonic-gate 	ret
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate /*
227*0Sstevel@tonic-gate  * Syscall return sequence with return code forced to zero.
228*0Sstevel@tonic-gate  */
229*0Sstevel@tonic-gate #define	RETC			\
230*0Sstevel@tonic-gate 	xorq	%rax, %rax;	\
231*0Sstevel@tonic-gate 	ret
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate #endif	/* _LIBC_AMD64_INC_SYS_H */
234