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