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