xref: /openbsd/lib/libc/arch/sh/SYS.h (revision fc31befa)
1*fc31befaSmiod /*	$OpenBSD: SYS.h,v 1.16 2024/03/27 20:03:29 miod Exp $	*/
2cf252584Smiod /*-
3cf252584Smiod  * Copyright (c) 1990 The Regents of the University of California.
4cf252584Smiod  * All rights reserved.
5cf252584Smiod  *
6cf252584Smiod  * This code is derived from software contributed to Berkeley by
7cf252584Smiod  * William Jolitz.
8cf252584Smiod  *
9cf252584Smiod  * Redistribution and use in source and binary forms, with or without
10cf252584Smiod  * modification, are permitted provided that the following conditions
11cf252584Smiod  * are met:
12cf252584Smiod  * 1. Redistributions of source code must retain the above copyright
13cf252584Smiod  *    notice, this list of conditions and the following disclaimer.
14cf252584Smiod  * 2. Redistributions in binary form must reproduce the above copyright
15cf252584Smiod  *    notice, this list of conditions and the following disclaimer in the
16cf252584Smiod  *    documentation and/or other materials provided with the distribution.
17cf252584Smiod  * 3. Neither the name of the University nor the names of its contributors
18cf252584Smiod  *    may be used to endorse or promote products derived from this software
19cf252584Smiod  *    without specific prior written permission.
20cf252584Smiod  *
21cf252584Smiod  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22cf252584Smiod  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23cf252584Smiod  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24cf252584Smiod  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25cf252584Smiod  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26cf252584Smiod  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27cf252584Smiod  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28cf252584Smiod  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29cf252584Smiod  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30cf252584Smiod  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31cf252584Smiod  * SUCH DAMAGE.
32cf252584Smiod  *
33cf252584Smiod  *	from: @(#)SYS.h	5.5 (Berkeley) 5/7/91
34cf252584Smiod  *	$NetBSD: SYS.h,v 1.9 2006/01/06 06:19:20 uwe Exp $
35cf252584Smiod  */
36cf252584Smiod 
37cf252584Smiod #include <machine/asm.h>
38cf252584Smiod #include <sys/syscall.h>
39cf252584Smiod 
408908800cSguenther 
418908800cSguenther /*
428908800cSguenther  * We need to offset the TCB pointer (in register gbr) by this much:
438908800cSguenther  *	offsetof(struct tib, tib_errno) - offsetof(struct tib, __tib_tcb)
448908800cSguenther  * That's negative on a variant I arch like sh, but you can't directly
458908800cSguenther  * load negative numbers or use them as displacements.  Ha!  So this is the
468908800cSguenther  * negative of the real value and we'll explicitly subtract it in the asm
478908800cSguenther  */
488908800cSguenther #define	TCB_OFFSET_ERRNO_NEG	8
498908800cSguenther 
509b9d2a55Sguenther /*
519b9d2a55Sguenther  * We define a hidden alias with the prefix "_libc_" for each global symbol
529b9d2a55Sguenther  * that may be used internally.  By referencing _libc_x instead of x, other
539b9d2a55Sguenther  * parts of libc prevent overriding by the application and avoid unnecessary
549b9d2a55Sguenther  * relocations.
559b9d2a55Sguenther  */
569b9d2a55Sguenther #define _HIDDEN(x)		_libc_##x
579b9d2a55Sguenther #define _HIDDEN_ALIAS(x,y)			\
589b9d2a55Sguenther 	STRONG_ALIAS(_HIDDEN(x),y);		\
599b9d2a55Sguenther 	.hidden _HIDDEN(x)
609b9d2a55Sguenther #define _HIDDEN_FALIAS(x,y)			\
619b9d2a55Sguenther 	_HIDDEN_ALIAS(x,y);			\
629b9d2a55Sguenther 	.type _HIDDEN(x),@function
639b9d2a55Sguenther 
649b9d2a55Sguenther /*
659b9d2a55Sguenther  * For functions implemented in ASM that aren't syscalls.
669b9d2a55Sguenther  *   END_STRONG(x)	Like DEF_STRONG() in C; for standard/reserved C names
679b9d2a55Sguenther  *   END_WEAK(x)	Like DEF_WEAK() in C; for non-ISO C names
689b9d2a55Sguenther  */
699b9d2a55Sguenther #define	END_STRONG(x)	SET_ENTRY_SIZE(x);		\
709b9d2a55Sguenther 			_HIDDEN_FALIAS(x,x);		\
719b9d2a55Sguenther 			SET_ENTRY_SIZE(_HIDDEN(x))
729b9d2a55Sguenther #define	END_WEAK(x)	END_STRONG(x); .weak x
739b9d2a55Sguenther 
749b9d2a55Sguenther 
75cf252584Smiod #define	SYSENTRY(x)					\
76eca0a8dbSguenther 	WEAK_ALIAS(x,_thread_sys_ ## x);		\
77cf252584Smiod 	ENTRY(_thread_sys_ ## x)
78514a545fSguenther #define	SYSENTRY_HIDDEN(x)				\
79514a545fSguenther 	ENTRY(_thread_sys_ ## x)
80cf252584Smiod 
8183762a71Sderaadt #define PINSYSCALL(sysno, label)					\
8283762a71Sderaadt 	.pushsection .openbsd.syscalls,"",@progbits;			\
835bcead81Skettenis 	.p2align 2;							\
8483762a71Sderaadt 	.long label;							\
8583762a71Sderaadt 	.long sysno;							\
8683762a71Sderaadt 	.popsection;
8783762a71Sderaadt 
883e492f6eSmiod #ifdef __ASSEMBLER__
893e492f6eSmiod /*
903e492f6eSmiod  * If the system call number fits in a 8-bit signed value (i.e. fits in 7 bits),
91*fc31befaSmiod  * then we can use the #imm8 addressing mode. Otherwise, we'll load the number
92*fc31befaSmiod  * from memory at the end of the system call wrapper.
933e492f6eSmiod  */
943e492f6eSmiod 
953e492f6eSmiod .macro systrap num
963e492f6eSmiod .iflt \num - 128
973e492f6eSmiod 	mov	# \num, r0
9883762a71Sderaadt 97:	trapa	#0x80
9983762a71Sderaadt 	PINSYSCALL(\num, 97b)
1003e492f6eSmiod .else
1013e492f6eSmiod 	mov.l	903f, r0
10283762a71Sderaadt 97:	trapa	#0x80
10383762a71Sderaadt 	PINSYSCALL(\num, 97b)
1043e492f6eSmiod .endif
1053e492f6eSmiod .endm
106*fc31befaSmiod 
107*fc31befaSmiod .macro systrap_data num
108*fc31befaSmiod .iflt \num - 128
109*fc31befaSmiod .else
110*fc31befaSmiod 	.text
111*fc31befaSmiod 	.align	2
112*fc31befaSmiod  903:	.long	\num
113*fc31befaSmiod .endif
114*fc31befaSmiod .endm
115*fc31befaSmiod 
116*fc31befaSmiod .macro syscall_error num
117*fc31befaSmiod .ifeq \num - SYS_lseek
118*fc31befaSmiod 	mov	#-1, r1
119*fc31befaSmiod .endif
120*fc31befaSmiod 	rts
121*fc31befaSmiod 	 mov	#-1, r0
122*fc31befaSmiod .endm
123*fc31befaSmiod 
1243e492f6eSmiod #endif
1253e492f6eSmiod 
1263e492f6eSmiod #define SYSTRAP(x)					\
1273e492f6eSmiod 		systrap	SYS_ ## x
128cf252584Smiod 
129cf252584Smiod #define _SYSCALL_NOERROR(x,y)				\
130cf252584Smiod 		SYSENTRY(x);				\
131cf252584Smiod 		SYSTRAP(y)
132514a545fSguenther #define _SYSCALL_HIDDEN_NOERROR(x,y)			\
133514a545fSguenther 		SYSENTRY_HIDDEN(x);			\
134514a545fSguenther 		SYSTRAP(y)
135cf252584Smiod 
136*fc31befaSmiod #define SET_ERRNO_AND_RETURN(y)				\
1378908800cSguenther 		stc	gbr,r1;				\
1388908800cSguenther 		mov	#TCB_OFFSET_ERRNO_NEG,r2;	\
1398908800cSguenther 		sub	r2,r1;				\
1408908800cSguenther 		mov.l	r0,@r1;				\
141*fc31befaSmiod 		syscall_error SYS_ ## y
142cf252584Smiod 
143cf252584Smiod #define _SYSCALL(x,y)					\
144cf252584Smiod 		.text;					\
145*fc31befaSmiod 	911:	SET_ERRNO_AND_RETURN(y);		\
146cf252584Smiod 		_SYSCALL_NOERROR(x,y);			\
147cf252584Smiod 		bf	911b
148514a545fSguenther #define _SYSCALL_HIDDEN(x,y)				\
149514a545fSguenther 		.text;					\
150*fc31befaSmiod 	911:	SET_ERRNO_AND_RETURN(y);		\
151514a545fSguenther 		_SYSCALL_HIDDEN_NOERROR(x,y);		\
152514a545fSguenther 		bf	911b
153cf252584Smiod 
154*fc31befaSmiod #define	__END_HIDDEN(x,y)				\
155*fc31befaSmiod 		systrap_data SYS_ ## y;			\
156*fc31befaSmiod 		SET_ENTRY_SIZE(_thread_sys_ ## x);	\
157*fc31befaSmiod 		_HIDDEN_FALIAS(x,_thread_sys_ ## x);	\
158*fc31befaSmiod 		SET_ENTRY_SIZE(_HIDDEN(x))
159*fc31befaSmiod #define	__END(x,y)					\
160*fc31befaSmiod 		__END_HIDDEN(x,y); SET_ENTRY_SIZE(x)
161*fc31befaSmiod 
162cf252584Smiod #define SYSCALL_NOERROR(x)				\
163cf252584Smiod 		_SYSCALL_NOERROR(x,x)
164cf252584Smiod 
165cf252584Smiod #define SYSCALL(x)					\
166cf252584Smiod 		_SYSCALL(x,x)
167cf252584Smiod 
168cf252584Smiod #define PSEUDO_NOERROR(x,y)				\
169cf252584Smiod 		_SYSCALL_NOERROR(x,y);			\
170cf252584Smiod 		rts;					\
1713f373d41Sguenther 		 nop;					\
172*fc31befaSmiod 		__END(x,y)
173cf252584Smiod 
174cf252584Smiod #define PSEUDO(x,y)					\
175cf252584Smiod 		_SYSCALL(x,y);				\
176cf252584Smiod 		rts;					\
1773f373d41Sguenther 		 nop;					\
178*fc31befaSmiod 		__END(x,y)
179cf252584Smiod 
180514a545fSguenther #define PSEUDO_HIDDEN(x,y)				\
181514a545fSguenther 		_SYSCALL_HIDDEN(x,y);			\
182514a545fSguenther 		rts;					\
1833f373d41Sguenther 		 nop;					\
184*fc31befaSmiod 		__END_HIDDEN(x,y)
185cf252584Smiod 
186514a545fSguenther #define RSYSCALL_NOERROR(x)		PSEUDO_NOERROR(x,x)
187514a545fSguenther #define RSYSCALL(x)			PSEUDO(x,x)
188514a545fSguenther #define RSYSCALL_HIDDEN(x)		PSEUDO_HIDDEN(x,x)
189*fc31befaSmiod #define SYSCALL_END(x)			__END(x,x)
190*fc31befaSmiod #define SYSCALL_END_HIDDEN(x)		__END_HIDDEN(x,x)
191