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 2005 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_I386_INC_SYS_H
28*0Sstevel@tonic-gate #define	_LIBC_I386_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/trap.h>
38*0Sstevel@tonic-gate #include <sys/errno.h>
39*0Sstevel@tonic-gate #include "synonyms.h"
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #define	_prologue_			\
42*0Sstevel@tonic-gate 	pushl	%ebx;			\
43*0Sstevel@tonic-gate 	call	9f;			\
44*0Sstevel@tonic-gate 9:					\
45*0Sstevel@tonic-gate 	popl	%ebx;			\
46*0Sstevel@tonic-gate 	addl	$_GLOBAL_OFFSET_TABLE_ + [. - 9b], %ebx
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate #define	_epilogue_			\
49*0Sstevel@tonic-gate 	popl	%ebx
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate #define	_fref_(name)	name@PLT
52*0Sstevel@tonic-gate #define	_daref_(name)	name@GOT(%ebx)
53*0Sstevel@tonic-gate #define	_sref_(name)	name@GOTOFF(%ebx)
54*0Sstevel@tonic-gate #define	_esp_(offset)	offset+4(%esp)	/* add 4 for the saved %ebx */
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate /*
57*0Sstevel@tonic-gate  * Define the external symbols __cerror and __cerror64 for all files.
58*0Sstevel@tonic-gate  */
59*0Sstevel@tonic-gate 	.globl	__cerror
60*0Sstevel@tonic-gate 	.globl	__cerror64
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate /*
63*0Sstevel@tonic-gate  * __SYSCALLINT provides the basic trap sequence.  It assumes that an entry
64*0Sstevel@tonic-gate  * of the form SYS_name exists (probably from sys/syscall.h).
65*0Sstevel@tonic-gate  */
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate #define	__SYSCALLINT(name)		\
68*0Sstevel@tonic-gate 	/* CSTYLED */			\
69*0Sstevel@tonic-gate 	movl	$SYS_/**/name, %eax;	\
70*0Sstevel@tonic-gate 	int	$T_SYSCALLINT
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate /*
73*0Sstevel@tonic-gate  * __SYSENTER provides a faster variant that is only able to
74*0Sstevel@tonic-gate  * return rval1.  Note that %ecx and %edx are ALWAYS smashed.
75*0Sstevel@tonic-gate  */
76*0Sstevel@tonic-gate #define	__SYSENTER(name)		\
77*0Sstevel@tonic-gate 	call	8f;			\
78*0Sstevel@tonic-gate 8:	popl	%edx;			\
79*0Sstevel@tonic-gate 	/* CSTYLED */			\
80*0Sstevel@tonic-gate 	movl	$SYS_/**/name, %eax;	\
81*0Sstevel@tonic-gate 	movl	%esp, %ecx;		\
82*0Sstevel@tonic-gate 	add	$[9f - 8b], %edx;	\
83*0Sstevel@tonic-gate 	sysenter;			\
84*0Sstevel@tonic-gate 9:
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate /*
87*0Sstevel@tonic-gate  * __SYSCALL provides a faster variant on processors and kernels
88*0Sstevel@tonic-gate  * that support it.  Note that %ecx is ALWAYS smashed.
89*0Sstevel@tonic-gate  */
90*0Sstevel@tonic-gate #define	__SYSCALL(name)			\
91*0Sstevel@tonic-gate 	/* CSTYLED */			\
92*0Sstevel@tonic-gate 	movl	$SYS_/**/name, %eax;	\
93*0Sstevel@tonic-gate 	.byte	0xf, 0x5	/* syscall */
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate #if defined(_SYSC_INSN)
96*0Sstevel@tonic-gate #define	SYSTRAP_RVAL1(name)	__SYSCALL(name)
97*0Sstevel@tonic-gate #define	SYSTRAP_RVAL2(name)	__SYSCALL(name)
98*0Sstevel@tonic-gate #define	SYSTRAP_2RVALS(name)	__SYSCALL(name)
99*0Sstevel@tonic-gate #define	SYSTRAP_64RVAL(name)	__SYSCALL(name)
100*0Sstevel@tonic-gate #else	/* _SYSC_INSN */
101*0Sstevel@tonic-gate #if defined(_SEP_INSN)
102*0Sstevel@tonic-gate #define	SYSTRAP_RVAL1(name)	__SYSENTER(name)
103*0Sstevel@tonic-gate #else	/* _SEP_INSN */
104*0Sstevel@tonic-gate #define	SYSTRAP_RVAL1(name)	__SYSCALLINT(name)
105*0Sstevel@tonic-gate #endif	/* _SEP_INSN */
106*0Sstevel@tonic-gate #define	SYSTRAP_RVAL2(name)	__SYSCALLINT(name)
107*0Sstevel@tonic-gate #define	SYSTRAP_2RVALS(name)	__SYSCALLINT(name)
108*0Sstevel@tonic-gate #define	SYSTRAP_64RVAL(name)	__SYSCALLINT(name)
109*0Sstevel@tonic-gate #endif	/* _SYSC_INSN */
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate /*
112*0Sstevel@tonic-gate  * SYSFASTTRAP provides the fast system call trap sequence.  It assumes
113*0Sstevel@tonic-gate  * that an entry of the form T_name exists (probably from sys/trap.h).
114*0Sstevel@tonic-gate  */
115*0Sstevel@tonic-gate #define	SYSFASTTRAP(name)		\
116*0Sstevel@tonic-gate 	/* CSTYLED */			\
117*0Sstevel@tonic-gate 	movl	$T_/**/name, %eax;	\
118*0Sstevel@tonic-gate 	int	$T_FASTTRAP
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate /*
121*0Sstevel@tonic-gate  * SYSCERROR provides the sequence to branch to __cerror if an error is
122*0Sstevel@tonic-gate  * indicated by the carry-bit being set upon return from a trap.
123*0Sstevel@tonic-gate  */
124*0Sstevel@tonic-gate #define	SYSCERROR		\
125*0Sstevel@tonic-gate 	jb	__cerror
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate /*
128*0Sstevel@tonic-gate  * SYSCERROR64 provides the sequence to branch to __cerror64 if an error is
129*0Sstevel@tonic-gate  * indicated by the carry-bit being set upon return from a trap.
130*0Sstevel@tonic-gate  */
131*0Sstevel@tonic-gate #define	SYSCERROR64		\
132*0Sstevel@tonic-gate 	jb	__cerror64
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate /*
135*0Sstevel@tonic-gate  * SYSLWPERR provides the sequence to return 0 on a successful trap
136*0Sstevel@tonic-gate  * and the error code if unsuccessful.
137*0Sstevel@tonic-gate  * Error is indicated by the carry-bit being set upon return from a trap.
138*0Sstevel@tonic-gate  */
139*0Sstevel@tonic-gate #define	SYSLWPERR			\
140*0Sstevel@tonic-gate 	jae	1f;			\
141*0Sstevel@tonic-gate 	cmpl	$ERESTART, %eax;	\
142*0Sstevel@tonic-gate 	jne	2f;			\
143*0Sstevel@tonic-gate 	movl	$EINTR, %eax;		\
144*0Sstevel@tonic-gate 	jmp	2f;			\
145*0Sstevel@tonic-gate 1:					\
146*0Sstevel@tonic-gate 	xorl	%eax, %eax;		\
147*0Sstevel@tonic-gate 2:
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate /*
150*0Sstevel@tonic-gate  * SYSREENTRY provides the entry sequence for restartable system calls.
151*0Sstevel@tonic-gate  */
152*0Sstevel@tonic-gate #define	SYSREENTRY(name)	\
153*0Sstevel@tonic-gate /* CSTYLED */			\
154*0Sstevel@tonic-gate .restart_/**/name:		\
155*0Sstevel@tonic-gate 	ENTRY(name)
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate /*
158*0Sstevel@tonic-gate  * SYSRESTART provides the error handling sequence for restartable
159*0Sstevel@tonic-gate  * system calls.
160*0Sstevel@tonic-gate  */
161*0Sstevel@tonic-gate #define	SYSRESTART(name)		\
162*0Sstevel@tonic-gate 	jae	1f;			\
163*0Sstevel@tonic-gate 	cmpl	$ERESTART, %eax;	\
164*0Sstevel@tonic-gate 	je	name;			\
165*0Sstevel@tonic-gate 	jmp	__cerror;		\
166*0Sstevel@tonic-gate 1:
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate /*
169*0Sstevel@tonic-gate  * SYSINTR_RESTART provides the error handling sequence for restartable
170*0Sstevel@tonic-gate  * system calls in case of EINTR or ERESTART.
171*0Sstevel@tonic-gate  */
172*0Sstevel@tonic-gate #define	SYSINTR_RESTART(name)		\
173*0Sstevel@tonic-gate 	jae	1f;			\
174*0Sstevel@tonic-gate 	cmpl	$ERESTART, %eax;	\
175*0Sstevel@tonic-gate 	je	name;			\
176*0Sstevel@tonic-gate 	cmpl	$EINTR, %eax;		\
177*0Sstevel@tonic-gate 	je	name;			\
178*0Sstevel@tonic-gate 	jmp	2f;			\
179*0Sstevel@tonic-gate 1:					\
180*0Sstevel@tonic-gate 	xorl	%eax, %eax;		\
181*0Sstevel@tonic-gate 2:
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate /*
184*0Sstevel@tonic-gate  * SYSCALL provides the standard (i.e.: most common) system call sequence.
185*0Sstevel@tonic-gate  */
186*0Sstevel@tonic-gate #define	SYSCALL(name)		\
187*0Sstevel@tonic-gate 	ENTRY(name);		\
188*0Sstevel@tonic-gate 	SYSTRAP_2RVALS(name);	\
189*0Sstevel@tonic-gate 	SYSCERROR
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate #define	SYSCALL_RVAL1(name)	\
192*0Sstevel@tonic-gate 	ENTRY(name);		\
193*0Sstevel@tonic-gate 	SYSTRAP_RVAL1(name);	\
194*0Sstevel@tonic-gate 	SYSCERROR
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate /*
197*0Sstevel@tonic-gate  * SYSCALL64 provides the standard (i.e.: most common) system call sequence
198*0Sstevel@tonic-gate  * for system calls that return 64-bit values.
199*0Sstevel@tonic-gate  */
200*0Sstevel@tonic-gate #define	SYSCALL64(name)		\
201*0Sstevel@tonic-gate 	ENTRY(name);		\
202*0Sstevel@tonic-gate 	SYSTRAP_64RVAL(name);	\
203*0Sstevel@tonic-gate 	SYSCERROR64
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate /*
206*0Sstevel@tonic-gate  * SYSCALL_RESTART provides the most common restartable system call sequence.
207*0Sstevel@tonic-gate  */
208*0Sstevel@tonic-gate #define	SYSCALL_RESTART(name)	\
209*0Sstevel@tonic-gate 	SYSREENTRY(name);	\
210*0Sstevel@tonic-gate 	SYSTRAP_2RVALS(name);	\
211*0Sstevel@tonic-gate 	/* CSTYLED */		\
212*0Sstevel@tonic-gate 	SYSRESTART(.restart_/**/name)
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate #define	SYSCALL_RESTART_RVAL1(name)	\
215*0Sstevel@tonic-gate 	SYSREENTRY(name);		\
216*0Sstevel@tonic-gate 	SYSTRAP_RVAL1(name);		\
217*0Sstevel@tonic-gate 	/* CSTYLED */			\
218*0Sstevel@tonic-gate 	SYSRESTART(.restart_/**/name)
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate /*
221*0Sstevel@tonic-gate  * SYSCALL2 provides a common system call sequence when the entry name
222*0Sstevel@tonic-gate  * is different than the trap name.
223*0Sstevel@tonic-gate  */
224*0Sstevel@tonic-gate #define	SYSCALL2(entryname, trapname)	\
225*0Sstevel@tonic-gate 	ENTRY(entryname);		\
226*0Sstevel@tonic-gate 	SYSTRAP_2RVALS(trapname);	\
227*0Sstevel@tonic-gate 	SYSCERROR
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate #define	SYSCALL2_RVAL1(entryname, trapname)	\
230*0Sstevel@tonic-gate 	ENTRY(entryname);			\
231*0Sstevel@tonic-gate 	SYSTRAP_RVAL1(trapname);		\
232*0Sstevel@tonic-gate 	SYSCERROR
233*0Sstevel@tonic-gate 
234*0Sstevel@tonic-gate /*
235*0Sstevel@tonic-gate  * SYSCALL2_RESTART provides a common restartable system call sequence when the
236*0Sstevel@tonic-gate  * entry name is different than the trap name.
237*0Sstevel@tonic-gate  */
238*0Sstevel@tonic-gate #define	SYSCALL2_RESTART(entryname, trapname)	\
239*0Sstevel@tonic-gate 	SYSREENTRY(entryname);			\
240*0Sstevel@tonic-gate 	SYSTRAP_2RVALS(trapname);		\
241*0Sstevel@tonic-gate 	/* CSTYLED */				\
242*0Sstevel@tonic-gate 	SYSRESTART(.restart_/**/entryname)
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate #define	SYSCALL2_RESTART_RVAL1(entryname, trapname)	\
245*0Sstevel@tonic-gate 	SYSREENTRY(entryname);				\
246*0Sstevel@tonic-gate 	SYSTRAP_RVAL1(trapname);			\
247*0Sstevel@tonic-gate 	/* CSTYLED */					\
248*0Sstevel@tonic-gate 	SYSRESTART(.restart_/**/entryname)
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate /*
251*0Sstevel@tonic-gate  * SYSCALL_NOERROR provides the most common system call sequence for those
252*0Sstevel@tonic-gate  * system calls which don't check the error return (carry bit).
253*0Sstevel@tonic-gate  */
254*0Sstevel@tonic-gate #define	SYSCALL_NOERROR(name)	\
255*0Sstevel@tonic-gate 	ENTRY(name);		\
256*0Sstevel@tonic-gate 	SYSTRAP_2RVALS(name)
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate #define	SYSCALL_NOERROR_RVAL1(name)	\
259*0Sstevel@tonic-gate 	ENTRY(name);			\
260*0Sstevel@tonic-gate 	SYSTRAP_RVAL1(name)
261*0Sstevel@tonic-gate 
262*0Sstevel@tonic-gate /*
263*0Sstevel@tonic-gate  * Standard syscall return sequence, return code equal to rval1.
264*0Sstevel@tonic-gate  */
265*0Sstevel@tonic-gate #define	RET			\
266*0Sstevel@tonic-gate 	ret
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate /*
269*0Sstevel@tonic-gate  * Syscall return sequence, return code equal to rval2.
270*0Sstevel@tonic-gate  */
271*0Sstevel@tonic-gate #define	RET2			\
272*0Sstevel@tonic-gate 	movl	%edx, %eax;	\
273*0Sstevel@tonic-gate 	ret
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate /*
276*0Sstevel@tonic-gate  * Syscall return sequence with return code forced to zero.
277*0Sstevel@tonic-gate  */
278*0Sstevel@tonic-gate #define	RETC			\
279*0Sstevel@tonic-gate 	xorl	%eax, %eax;	\
280*0Sstevel@tonic-gate 	ret
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate #endif	/* _LIBC_I386_INC_SYS_H */
283