xref: /illumos-gate/usr/src/lib/libc/sparc/inc/SYS.h (revision 4c87aefe8930bd07275b8dd2e96ea5f24d93a52e)
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_SPARC_INC_SYS_H
28 #define	_LIBC_SPARC_INC_SYS_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 /*
33  * This file defines common code sequences for system calls.  Note that
34  * it is assumed that __cerror is within the short branch distance from
35  * all the traps (so that a simple bcs can follow the trap, rather than
36  * a position independent code sequence.)  Ditto for __cerror64.
37  */
38 
39 #include <sys/asm_linkage.h>
40 #include <sys/syscall.h>
41 #include <sys/errno.h>
42 
43 /*
44  * While it's tempting to imagine we could use 'rd %pc' here,
45  * in fact it's a rather slow operation that consumes many
46  * cycles, so we use the usual side-effect of 'call' instead.
47  */
48 #define	PIC_SETUP(r)						\
49 	mov	%o7, %g1;					\
50 9:	call	8f;						\
51 	sethi	%hi(_GLOBAL_OFFSET_TABLE_ - (9b - .)), %r;	\
52 8:	or	%r, %lo(_GLOBAL_OFFSET_TABLE_ - (9b - .)), %r;	\
53 	add	%r, %o7, %r;					\
54 	mov	%g1, %o7
55 
56 /*
57  * Trap number for system calls
58  */
59 #define	SYSCALL_TRAPNUM		8
60 #define	FASTSCALL_TRAPNUM	9
61 
62 /*
63  * Define the external symbols __cerror and __cerror64 for all files.
64  */
65 	.global	__cerror
66 	.global	__cerror64
67 
68 /*
69  * __SYSTRAP provides the basic trap sequence.  It assumes that an entry
70  * of the form SYS_name exists (probably from sys/syscall.h).
71  */
72 #define	__SYSTRAP(name)			\
73 	/* CSTYLED */			\
74 	mov	SYS_/**/name, %g1;	\
75 	ta	SYSCALL_TRAPNUM
76 
77 #define	SYSTRAP_RVAL1(name)		__SYSTRAP(name)
78 #define	SYSTRAP_RVAL2(name)		__SYSTRAP(name)
79 #define	SYSTRAP_2RVALS(name)		__SYSTRAP(name)
80 #define	SYSTRAP_64RVAL(name)		__SYSTRAP(name)
81 
82 /*
83  * SYSFASTTRAP provides the fast system call trap sequence.  It assumes
84  * that an entry of the form ST_name exists (probably from sys/trap.h).
85  */
86 #define	SYSFASTTRAP(name)		\
87 	/* CSTYLED */			\
88 	ta	ST_/**/name
89 
90 /*
91  * SYSCERROR provides the sequence to branch to __cerror if an error is
92  * indicated by the carry-bit being set upon return from a trap.
93  */
94 #define	SYSCERROR			\
95 	bcs	__cerror;		\
96 	nop
97 
98 /*
99  * SYSCERROR64 provides the sequence to branch to __cerror64 if an error is
100  * indicated by the carry-bit being set upon return from a trap.
101  */
102 #define	SYSCERROR64			\
103 	bcs	__cerror64;		\
104 	nop
105 
106 /*
107  * SYSLWPERR provides the sequence to return 0 on a successful trap
108  * and the error code if unsuccessful.
109  * Error is indicated by the carry-bit being set upon return from a trap.
110  */
111 #define	SYSLWPERR			\
112 	/* CSTYLED */			\
113 	bcc,a,pt %icc, 1f;		\
114 	clr	%o0;			\
115 	cmp	%o0, ERESTART;		\
116 	move	%icc, EINTR, %o0;	\
117 1:
118 
119 #define	SAVE_OFFSET	68
120 
121 /*
122  * SYSREENTRY provides the entry sequence for restartable system calls.
123  */
124 #define	SYSREENTRY(name)			\
125 	ENTRY(name);				\
126 	stn	%o0, [%sp + SAVE_OFFSET];	\
127 /* CSTYLED */					\
128 .restart_/**/name:
129 
130 /*
131  * SYSRESTART provides the error handling sequence for restartable
132  * system calls.
133  */
134 #define	SYSRESTART(name)					\
135 	/* CSTYLED */						\
136 	bcc,pt	%icc, 1f;					\
137 	cmp	%o0, ERESTART;					\
138 	/* CSTYLED */						\
139 	be,a,pn	%icc, name;					\
140 	ldn	[%sp + SAVE_OFFSET], %o0;			\
141 	/* CSTYLED */						\
142 	ba,a	__cerror;					\
143 1:
144 
145 /*
146  * SYSINTR_RESTART provides the error handling sequence for restartable
147  * system calls in case of EINTR or ERESTART.
148  */
149 #define	SYSINTR_RESTART(name)					\
150 	/* CSTYLED */						\
151 	bcc,a,pt %icc, 1f;					\
152 	clr	%o0;						\
153 	cmp	%o0, ERESTART;					\
154 	/* CSTYLED */						\
155 	be,a,pn	%icc, name;					\
156 	ldn	[%sp + SAVE_OFFSET], %o0;			\
157 	cmp	%o0, EINTR;					\
158 	/* CSTYLED */						\
159 	be,a,pn	%icc, name;					\
160 	ldn	[%sp + SAVE_OFFSET], %o0;			\
161 1:
162 
163 /*
164  * SYSCALL provides the standard (i.e.: most common) system call sequence.
165  */
166 #define	SYSCALL(name)						\
167 	ENTRY(name);						\
168 	SYSTRAP_2RVALS(name);					\
169 	SYSCERROR
170 
171 #define	SYSCALL_RVAL1(name)					\
172 	ENTRY(name);						\
173 	SYSTRAP_RVAL1(name);					\
174 	SYSCERROR
175 
176 /*
177  * SYSCALL64 provides the standard (i.e.: most common) system call sequence
178  * for system calls that return 64-bit values.
179  */
180 #define	SYSCALL64(name)						\
181 	ENTRY(name);						\
182 	SYSTRAP_64RVAL(name);					\
183 	SYSCERROR64
184 
185 /*
186  * SYSCALL_RESTART provides the most common restartable system call sequence.
187  */
188 #define	SYSCALL_RESTART(name)					\
189 	SYSREENTRY(name);					\
190 	SYSTRAP_2RVALS(name);					\
191 	/* CSTYLED */						\
192 	SYSRESTART(.restart_/**/name)
193 
194 #define	SYSCALL_RESTART_RVAL1(name)				\
195 	SYSREENTRY(name);					\
196 	SYSTRAP_RVAL1(name);					\
197 	/* CSTYLED */						\
198 	SYSRESTART(.restart_/**/name)
199 
200 /*
201  * SYSCALL2 provides a common system call sequence when the entry name
202  * is different than the trap name.
203  */
204 #define	SYSCALL2(entryname, trapname)				\
205 	ENTRY(entryname);					\
206 	SYSTRAP_2RVALS(trapname);				\
207 	SYSCERROR
208 
209 #define	SYSCALL2_RVAL1(entryname, trapname)			\
210 	ENTRY(entryname);					\
211 	SYSTRAP_RVAL1(trapname);				\
212 	SYSCERROR
213 
214 /*
215  * SYSCALL2_RESTART provides a common restartable system call sequence when the
216  * entry name is different than the trap name.
217  */
218 #define	SYSCALL2_RESTART(entryname, trapname)			\
219 	SYSREENTRY(entryname);					\
220 	SYSTRAP_2RVALS(trapname);				\
221 	/* CSTYLED */						\
222 	SYSRESTART(.restart_/**/entryname)
223 
224 #define	SYSCALL2_RESTART_RVAL1(entryname, trapname)		\
225 	SYSREENTRY(entryname);					\
226 	SYSTRAP_RVAL1(trapname);				\
227 	/* CSTYLED */						\
228 	SYSRESTART(.restart_/**/entryname)
229 
230 /*
231  * SYSCALL_NOERROR provides the most common system call sequence for those
232  * system calls which don't check the error return (carry bit).
233  */
234 #define	SYSCALL_NOERROR(name)					\
235 	ENTRY(name);						\
236 	SYSTRAP_2RVALS(name)
237 
238 #define	SYSCALL_NOERROR_RVAL1(name)				\
239 	ENTRY(name);						\
240 	SYSTRAP_RVAL1(name)
241 
242 /*
243  * Standard syscall return sequence, return code equal to rval1.
244  */
245 #define	RET			\
246 	retl;			\
247 	nop
248 
249 /*
250  * Syscall return sequence, return code equal to rval2.
251  */
252 #define	RET2			\
253 	retl;			\
254 	mov	%o1, %o0
255 
256 /*
257  * Syscall return sequence with return code forced to zero.
258  */
259 #define	RETC			\
260 	retl;			\
261 	clr	%o0
262 
263 #endif	/* _LIBC_SPARC_INC_SYS_H */
264