xref: /openbsd-src/lib/libc/arch/i386/SYS.h (revision 83762a71f74848f4d09174ce350838b4204957c5)
1df930be7Sderaadt /*-
2df930be7Sderaadt  * Copyright (c) 1990 The Regents of the University of California.
3df930be7Sderaadt  * All rights reserved.
4df930be7Sderaadt  *
5df930be7Sderaadt  * This code is derived from software contributed to Berkeley by
6df930be7Sderaadt  * William Jolitz.
7df930be7Sderaadt  *
8df930be7Sderaadt  * Redistribution and use in source and binary forms, with or without
9df930be7Sderaadt  * modification, are permitted provided that the following conditions
10df930be7Sderaadt  * are met:
11df930be7Sderaadt  * 1. Redistributions of source code must retain the above copyright
12df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer.
13df930be7Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
14df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer in the
15df930be7Sderaadt  *    documentation and/or other materials provided with the distribution.
166580fee3Smillert  * 3. Neither the name of the University nor the names of its contributors
17df930be7Sderaadt  *    may be used to endorse or promote products derived from this software
18df930be7Sderaadt  *    without specific prior written permission.
19df930be7Sderaadt  *
20df930be7Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21df930be7Sderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22df930be7Sderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23df930be7Sderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24df930be7Sderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25df930be7Sderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26df930be7Sderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27df930be7Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28df930be7Sderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29df930be7Sderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30df930be7Sderaadt  * SUCH DAMAGE.
31df930be7Sderaadt  *
32*83762a71Sderaadt  *	$OpenBSD: SYS.h,v 1.28 2023/12/10 16:45:51 deraadt Exp $
33df930be7Sderaadt  */
34df930be7Sderaadt 
35ea6088e7Sguenther #include "DEFS.h"
36df930be7Sderaadt #include <sys/syscall.h>
37df930be7Sderaadt 
38fe38b55cSguenther #define TCB_OFFSET_ERRNO	16
399b9d2a55Sguenther 
409b9d2a55Sguenther /*
41dbeaad6eSd  * Design note:
42dbeaad6eSd  *
43dbeaad6eSd  * System calls entry points are really named _thread_sys_{syscall},
44dbeaad6eSd  * and weakly aliased to the name {syscall}. This allows the thread
45dbeaad6eSd  * library to replace system calls at link time.
46dbeaad6eSd  */
47dbeaad6eSd 
48dbeaad6eSd /* Use both _thread_sys_{syscall} and [weak] {syscall}. */
49dbeaad6eSd 
50dbeaad6eSd #define	SYSENTRY(x)					\
5178a11894Sdrahn 			ENTRY(_thread_sys_##x);		\
52fe38b55cSguenther 			WEAK_ALIAS(x, _thread_sys_##x)
53514a545fSguenther #define	SYSENTRY_HIDDEN(x)				\
54514a545fSguenther 			ENTRY(_thread_sys_ ## x)
55aa797096Sguenther #define	__END_HIDDEN(x)	END(_thread_sys_ ## x);			\
56aa797096Sguenther 			_HIDDEN_FALIAS(x,_thread_sys_ ## x);	\
57aa797096Sguenther 			END(_HIDDEN(x))
58aa797096Sguenther #define	__END(x)	__END_HIDDEN(x); END(x)
59dbeaad6eSd 
6092efb735Sd #define	__DO_SYSCALL(x)					\
61650bc085Sd 			movl $(SYS_ ## x),%eax;		\
62*83762a71Sderaadt 		97:	int $0x80;			\
63*83762a71Sderaadt 			PINSYSCALL(SYS_ ## x, 97b)
64*83762a71Sderaadt 
65df930be7Sderaadt 
66fe38b55cSguenther #define SET_ERRNO()					\
67fe38b55cSguenther 	movl	%eax,%gs:(TCB_OFFSET_ERRNO);		\
68fe38b55cSguenther 	movl	$-1, %eax;				\
69fe38b55cSguenther 	movl	$-1, %edx	/* for lseek */
70fe38b55cSguenther #define HANDLE_ERRNO()					\
71ac65b426Snaddy 	jnc	99f;					\
72fe38b55cSguenther 	SET_ERRNO();					\
73fe38b55cSguenther 	99:
74a5037010Sdrahn 
75ba8732b6Smillert /* perform a syscall */
76ba8732b6Smillert #define	_SYSCALL_NOERROR(x,y)				\
77ba8732b6Smillert 		SYSENTRY(x);				\
78ba8732b6Smillert 			__DO_SYSCALL(y);
79514a545fSguenther #define	_SYSCALL_HIDDEN_NOERROR(x,y)			\
80514a545fSguenther 		SYSENTRY_HIDDEN(x);			\
81514a545fSguenther 			__DO_SYSCALL(y);
82ba8732b6Smillert 
83ba8732b6Smillert #define	SYSCALL_NOERROR(x)				\
84ba8732b6Smillert 		_SYSCALL_NOERROR(x,x)
85ba8732b6Smillert 
8692efb735Sd /* perform a syscall, set errno */
87a5037010Sdrahn #define	_SYSCALL(x,y)					\
88a5037010Sdrahn 			.text;				\
89a5037010Sdrahn 			.align 2;			\
90a5037010Sdrahn 		_SYSCALL_NOERROR(x,y)			\
91fe38b55cSguenther 			HANDLE_ERRNO()
92514a545fSguenther #define	_SYSCALL_HIDDEN(x,y)				\
93514a545fSguenther 			.text;				\
94514a545fSguenther 			.align 2;			\
95514a545fSguenther 		_SYSCALL_HIDDEN_NOERROR(x,y)		\
96fe38b55cSguenther 			HANDLE_ERRNO()
9792efb735Sd 
98ba8732b6Smillert #define	SYSCALL(x)					\
99ba8732b6Smillert 		_SYSCALL(x,x)
100fe38b55cSguenther #define	SYSCALL_HIDDEN(x)				\
101fe38b55cSguenther 		_SYSCALL_HIDDEN(x,y)
10292efb735Sd 
10392efb735Sd /* perform a syscall, return */
104ba8732b6Smillert #define	PSEUDO_NOERROR(x,y)				\
105ba8732b6Smillert 		_SYSCALL_NOERROR(x,y);			\
106aa797096Sguenther 			ret;				\
107aa797096Sguenther 		__END(x)
10892efb735Sd 
109ba8732b6Smillert /* perform a syscall, set errno, return */
110ba8732b6Smillert #define	PSEUDO(x,y)					\
111ba8732b6Smillert 		_SYSCALL(x,y);				\
112aa797096Sguenther 			ret;				\
113aa797096Sguenther 		__END(x)
114514a545fSguenther #define	PSEUDO_HIDDEN(x,y)				\
115514a545fSguenther 		_SYSCALL_HIDDEN(x,y);			\
116aa797096Sguenther 			ret;				\
117aa797096Sguenther 		__END_HIDDEN(x)
118ba8732b6Smillert 
119ba8732b6Smillert /* perform a syscall with the same name, set errno, return */
120ba8732b6Smillert #define	RSYSCALL(x)					\
121ba8732b6Smillert 			PSEUDO(x,x);
122514a545fSguenther #define	RSYSCALL_HIDDEN(x)				\
123514a545fSguenther 			PSEUDO_HIDDEN(x,x)
124aa797096Sguenther #define	SYSCALL_END(x)	__END(x)
1255aed4d28Sguenther #define	SYSCALL_END_HIDDEN(x)				\
1265aed4d28Sguenther 			__END_HIDDEN(x)
127