xref: /openbsd-src/lib/libc/arch/sh/SYS.h (revision 3374c67d44f9b75b98444cbf63020f777792342e)
1 /*	$OpenBSD: SYS.h,v 1.12 2022/09/02 06:19:04 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	__END_HIDDEN(x)					\
82 	SET_ENTRY_SIZE(_thread_sys_ ## x);		\
83 	_HIDDEN_FALIAS(x,_thread_sys_ ## x);		\
84 	SET_ENTRY_SIZE(_HIDDEN(x))
85 #define	__END(x)					\
86 	__END_HIDDEN(x); SET_ENTRY_SIZE(x)
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.
92  */
93 
94 .macro systrap num
95 .iflt \num - 128
96 	mov	# \num, r0
97 	trapa	#0x80
98 .else
99 	mov.l	903f, r0
100 	trapa	#0x80
101 	bra	904f
102 	 nop
103 	.align	2
104  903:	.long	\num
105  904:
106 .endif
107 .endm
108 #endif
109 
110 #define SYSTRAP(x)					\
111 		systrap	SYS_ ## x
112 
113 #define _SYSCALL_NOERROR(x,y)				\
114 		SYSENTRY(x);				\
115 		SYSTRAP(y)
116 #define _SYSCALL_HIDDEN_NOERROR(x,y)			\
117 		SYSENTRY_HIDDEN(x);			\
118 		SYSTRAP(y)
119 
120 #define SET_ERRNO_AND_RETURN				\
121 		stc	gbr,r1;				\
122 		mov	#TCB_OFFSET_ERRNO_NEG,r2;	\
123 		sub	r2,r1;				\
124 		mov.l	r0,@r1;				\
125 		mov	#-1, r1;			\
126 		rts;					\
127 		 mov	#-1, r0
128 
129 #define _SYSCALL(x,y)					\
130 		.text;					\
131 	911:	SET_ERRNO_AND_RETURN;			\
132 		_SYSCALL_NOERROR(x,y);			\
133 		bf	911b
134 #define _SYSCALL_HIDDEN(x,y)				\
135 		.text;					\
136 	911:	SET_ERRNO_AND_RETURN;			\
137 		_SYSCALL_HIDDEN_NOERROR(x,y);		\
138 		bf	911b
139 
140 #define SYSCALL_NOERROR(x)				\
141 		_SYSCALL_NOERROR(x,x)
142 
143 #define SYSCALL(x)					\
144 		_SYSCALL(x,x)
145 
146 #define PSEUDO_NOERROR(x,y)				\
147 		_SYSCALL_NOERROR(x,y);			\
148 		rts;					\
149 		 nop;					\
150 		__END(x)
151 
152 #define PSEUDO(x,y)					\
153 		_SYSCALL(x,y);				\
154 		rts;					\
155 		 nop;					\
156 		__END(x)
157 
158 #define PSEUDO_HIDDEN(x,y)				\
159 		_SYSCALL_HIDDEN(x,y);			\
160 		rts;					\
161 		 nop;					\
162 		__END_HIDDEN(x)
163 
164 #define RSYSCALL_NOERROR(x)		PSEUDO_NOERROR(x,x)
165 #define RSYSCALL(x)			PSEUDO(x,x)
166 #define RSYSCALL_HIDDEN(x)		PSEUDO_HIDDEN(x,x)
167 #define SYSCALL_END(x)			__END(x)
168 #define SYSCALL_END_HIDDEN(x)		__END_HIDDEN(x)
169