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