xref: /openbsd/lib/libc/arch/riscv64/SYS.h (revision 83762a71)
1 /*	$OpenBSD: SYS.h,v 1.5 2023/12/10 16:45:52 deraadt Exp $	*/
2 /*-
3  * Copyright (c) 2020 Brian Bamsch <bbamsch@google.com>
4  * Copyright (c) 1990 The Regents of the University of California.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * William Jolitz.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	from: @(#)SYS.h	5.5 (Berkeley) 5/7/91
35  */
36 
37 #include "DEFS.h"
38 #include <sys/syscall.h>
39 
40 /* offsetof(struct tib, tib_errno) - offsetof(struct tib, __tib_tcb) */
41 #define TCB_OFFSET_ERRNO	(-12)
42 
43 /* offset of errno from tp */
44 #define TP_OFFSET_ERRNO		TCB_OFFSET_ERRNO
45 
46 #define SYSENTRY(x)					\
47 	.weak x;					\
48 	x = _thread_sys_ ## x;				\
49 	ENTRY(_thread_sys_ ## x)
50 #define SYSENTRY_HIDDEN(x)				\
51 	ENTRY(_thread_sys_ ## x)
52 #define __END_HIDDEN(x)					\
53 	END(_thread_sys_ ## x);				\
54 	_HIDDEN_FALIAS(x, _thread_sys_ ## x);		\
55 	END(_HIDDEN(x))
56 #define __END(x)					\
57 	__END_HIDDEN(x); END(x)
58 
59 #define SYSTRAP(x)					\
60 	li	t0, SYS_ ## x;				\
61 97:	ecall;						\
62 	PINSYSCALL(SYS_ ## x, 97b)
63 
64 #define HANDLE_ERROR()							\
65 	beqz	t0, 200f;						\
66 	sw	a0, TP_OFFSET_ERRNO(tp);				\
67 	li	a0, -1;							\
68 200:
69 
70 #define HANDLE_ERROR_TARGET(target)					\
71 	beqz	t0, 200f;						\
72 	sw	a0, TP_OFFSET_ERRNO(tp);				\
73 	li	a0, -1;							\
74 	j	target;							\
75 200:
76 #define _SYSCALL_NOERROR(x,y)						\
77 	SYSENTRY(x);							\
78 	SYSTRAP(y)
79 #define _SYSCALL_HIDDEN_NOERROR(x,y)					\
80 	SYSENTRY_HIDDEN(x);						\
81 	SYSTRAP(y)
82 
83 #define _SYSCALL(x, y)							\
84 	_SYSCALL_NOERROR(x,y);						\
85 	HANDLE_ERROR()							\
86 #define _SYSCALL_HIDDEN(x, y)						\
87 	_SYSCALL_HIDDEN_NOERROR(x,y);					\
88 	HANDLE_ERROR()							\
89 
90 #define SYSCALL_NOERROR(x)						\
91 	_SYSCALL_NOERROR(x,x)
92 
93 #define SYSCALL(x)							\
94 	_SYSCALL(x,x)
95 
96 
97 #define PSEUDO_NOERROR(x,y)						\
98 	SYSENTRY(x);							\
99 	RETGUARD_SETUP(x, t6);						\
100 	SYSTRAP(y);							\
101 	RETGUARD_CHECK(x, t6);						\
102 	ret;								\
103 	__END(x)
104 
105 #define PSEUDO(x,y)							\
106 	SYSENTRY(x);							\
107 	RETGUARD_SETUP(x, t6);						\
108 	SYSTRAP(y);							\
109 	HANDLE_ERROR();							\
110 	RETGUARD_CHECK(x, t6);						\
111 	ret;								\
112 	__END(x)
113 #define PSEUDO_HIDDEN(x,y)						\
114 	SYSENTRY_HIDDEN(x);						\
115 	RETGUARD_SETUP(x, t6);						\
116 	SYSTRAP(y);							\
117 	HANDLE_ERROR();							\
118 	RETGUARD_CHECK(x, t6);						\
119 	ret;								\
120 	__END_HIDDEN(x)
121 
122 #define RSYSCALL_NOERROR(x)						\
123 	PSEUDO_NOERROR(x,x)
124 
125 #define RSYSCALL(x)							\
126 	PSEUDO(x,x)
127 #define RSYSCALL_HIDDEN(x)						\
128 	PSEUDO_HIDDEN(x,x)
129 #define SYSCALL_END(x)							\
130 	__END(x)
131 #define SYSCALL_END_HIDDEN(x)						\
132 	__END_HIDDEN(x)
133