xref: /openbsd-src/lib/libc/arch/sh/SYS.h (revision fc31befaf105b97a112f7671f6f1ed3695619019)
1 /*	$OpenBSD: SYS.h,v 1.16 2024/03/27 20:03:29 miod Exp $	*/
2 /*-
3  * Copyright (c) 1990 The Regents of the University of California.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * William Jolitz.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	from: @(#)SYS.h	5.5 (Berkeley) 5/7/91
34  *	$NetBSD: SYS.h,v 1.9 2006/01/06 06:19:20 uwe Exp $
35  */
36 
37 #include <machine/asm.h>
38 #include <sys/syscall.h>
39 
40 
41 /*
42  * We need to offset the TCB pointer (in register gbr) by this much:
43  *	offsetof(struct tib, tib_errno) - offsetof(struct tib, __tib_tcb)
44  * That's negative on a variant I arch like sh, but you can't directly
45  * load negative numbers or use them as displacements.  Ha!  So this is the
46  * negative of the real value and we'll explicitly subtract it in the asm
47  */
48 #define	TCB_OFFSET_ERRNO_NEG	8
49 
50 /*
51  * We define a hidden alias with the prefix "_libc_" for each global symbol
52  * that may be used internally.  By referencing _libc_x instead of x, other
53  * parts of libc prevent overriding by the application and avoid unnecessary
54  * relocations.
55  */
56 #define _HIDDEN(x)		_libc_##x
57 #define _HIDDEN_ALIAS(x,y)			\
58 	STRONG_ALIAS(_HIDDEN(x),y);		\
59 	.hidden _HIDDEN(x)
60 #define _HIDDEN_FALIAS(x,y)			\
61 	_HIDDEN_ALIAS(x,y);			\
62 	.type _HIDDEN(x),@function
63 
64 /*
65  * For functions implemented in ASM that aren't syscalls.
66  *   END_STRONG(x)	Like DEF_STRONG() in C; for standard/reserved C names
67  *   END_WEAK(x)	Like DEF_WEAK() in C; for non-ISO C names
68  */
69 #define	END_STRONG(x)	SET_ENTRY_SIZE(x);		\
70 			_HIDDEN_FALIAS(x,x);		\
71 			SET_ENTRY_SIZE(_HIDDEN(x))
72 #define	END_WEAK(x)	END_STRONG(x); .weak x
73 
74 
75 #define	SYSENTRY(x)					\
76 	WEAK_ALIAS(x,_thread_sys_ ## x);		\
77 	ENTRY(_thread_sys_ ## x)
78 #define	SYSENTRY_HIDDEN(x)				\
79 	ENTRY(_thread_sys_ ## x)
80 
81 #define PINSYSCALL(sysno, label)					\
82 	.pushsection .openbsd.syscalls,"",@progbits;			\
83 	.p2align 2;							\
84 	.long label;							\
85 	.long sysno;							\
86 	.popsection;
87 
88 #ifdef __ASSEMBLER__
89 /*
90  * If the system call number fits in a 8-bit signed value (i.e. fits in 7 bits),
91  * then we can use the #imm8 addressing mode. Otherwise, we'll load the number
92  * from memory at the end of the system call wrapper.
93  */
94 
95 .macro systrap num
96 .iflt \num - 128
97 	mov	# \num, r0
98 97:	trapa	#0x80
99 	PINSYSCALL(\num, 97b)
100 .else
101 	mov.l	903f, r0
102 97:	trapa	#0x80
103 	PINSYSCALL(\num, 97b)
104 .endif
105 .endm
106 
107 .macro systrap_data num
108 .iflt \num - 128
109 .else
110 	.text
111 	.align	2
112  903:	.long	\num
113 .endif
114 .endm
115 
116 .macro syscall_error num
117 .ifeq \num - SYS_lseek
118 	mov	#-1, r1
119 .endif
120 	rts
121 	 mov	#-1, r0
122 .endm
123 
124 #endif
125 
126 #define SYSTRAP(x)					\
127 		systrap	SYS_ ## x
128 
129 #define _SYSCALL_NOERROR(x,y)				\
130 		SYSENTRY(x);				\
131 		SYSTRAP(y)
132 #define _SYSCALL_HIDDEN_NOERROR(x,y)			\
133 		SYSENTRY_HIDDEN(x);			\
134 		SYSTRAP(y)
135 
136 #define SET_ERRNO_AND_RETURN(y)				\
137 		stc	gbr,r1;				\
138 		mov	#TCB_OFFSET_ERRNO_NEG,r2;	\
139 		sub	r2,r1;				\
140 		mov.l	r0,@r1;				\
141 		syscall_error SYS_ ## y
142 
143 #define _SYSCALL(x,y)					\
144 		.text;					\
145 	911:	SET_ERRNO_AND_RETURN(y);		\
146 		_SYSCALL_NOERROR(x,y);			\
147 		bf	911b
148 #define _SYSCALL_HIDDEN(x,y)				\
149 		.text;					\
150 	911:	SET_ERRNO_AND_RETURN(y);		\
151 		_SYSCALL_HIDDEN_NOERROR(x,y);		\
152 		bf	911b
153 
154 #define	__END_HIDDEN(x,y)				\
155 		systrap_data SYS_ ## y;			\
156 		SET_ENTRY_SIZE(_thread_sys_ ## x);	\
157 		_HIDDEN_FALIAS(x,_thread_sys_ ## x);	\
158 		SET_ENTRY_SIZE(_HIDDEN(x))
159 #define	__END(x,y)					\
160 		__END_HIDDEN(x,y); SET_ENTRY_SIZE(x)
161 
162 #define SYSCALL_NOERROR(x)				\
163 		_SYSCALL_NOERROR(x,x)
164 
165 #define SYSCALL(x)					\
166 		_SYSCALL(x,x)
167 
168 #define PSEUDO_NOERROR(x,y)				\
169 		_SYSCALL_NOERROR(x,y);			\
170 		rts;					\
171 		 nop;					\
172 		__END(x,y)
173 
174 #define PSEUDO(x,y)					\
175 		_SYSCALL(x,y);				\
176 		rts;					\
177 		 nop;					\
178 		__END(x,y)
179 
180 #define PSEUDO_HIDDEN(x,y)				\
181 		_SYSCALL_HIDDEN(x,y);			\
182 		rts;					\
183 		 nop;					\
184 		__END_HIDDEN(x,y)
185 
186 #define RSYSCALL_NOERROR(x)		PSEUDO_NOERROR(x,x)
187 #define RSYSCALL(x)			PSEUDO(x,x)
188 #define RSYSCALL_HIDDEN(x)		PSEUDO_HIDDEN(x,x)
189 #define SYSCALL_END(x)			__END(x,x)
190 #define SYSCALL_END_HIDDEN(x)		__END_HIDDEN(x,x)
191