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