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