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