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