1209231Sjchandra/*	$NetBSD: SYS.h,v 1.19 2009/12/14 01:07:41 matt Exp $ */
2178580Simp/* $FreeBSD$ */
3178580Simp
4178580Simp/*-
5178580Simp * Copyright (c) 1996 Jonathan Stone
6178580Simp * All rights reserved.
7178580Simp *
8178580Simp * Redistribution and use in source and binary forms, with or without
9178580Simp * modification, are permitted provided that the following conditions
10178580Simp * are met:
11178580Simp * 1. Redistributions of source code must retain the above copyright
12178580Simp *    notice, this list of conditions and the following disclaimer.
13178580Simp * 2. Redistributions in binary form must reproduce the above copyright
14178580Simp *    notice, this list of conditions and the following disclaimer in the
15178580Simp *    documentation and/or other materials provided with the distribution.
16178580Simp * 3. All advertising materials mentioning features or use of this software
17178580Simp *    must display the following acknowledgement:
18178580Simp *      This product includes software developed by Jonathan Stone for
19178580Simp *      the NetBSD Project.
20178580Simp * 4. The name of the author may not be used to endorse or promote products
21178580Simp *    derived from this software without specific prior written permission.
22178580Simp *
23178580Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24178580Simp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25178580Simp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26178580Simp * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27178580Simp * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28178580Simp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29178580Simp * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30178580Simp * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31178580Simp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32178580Simp * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33178580Simp */
34178580Simp
35178580Simp/*-
36178580Simp * Copyright (c) 1991, 1993
37178580Simp *	The Regents of the University of California.  All rights reserved.
38178580Simp *
39178580Simp * This code is derived from software contributed to Berkeley by
40178580Simp * Ralph Campbell.
41178580Simp *
42178580Simp * Redistribution and use in source and binary forms, with or without
43178580Simp * modification, are permitted provided that the following conditions
44178580Simp * are met:
45178580Simp * 1. Redistributions of source code must retain the above copyright
46178580Simp *    notice, this list of conditions and the following disclaimer.
47178580Simp * 2. Redistributions in binary form must reproduce the above copyright
48178580Simp *    notice, this list of conditions and the following disclaimer in the
49178580Simp *    documentation and/or other materials provided with the distribution.
50178580Simp * 3. Neither the name of the University nor the names of its contributors
51178580Simp *    may be used to endorse or promote products derived from this software
52178580Simp *    without specific prior written permission.
53178580Simp *
54178580Simp * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55178580Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56178580Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57178580Simp * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58178580Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59178580Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60178580Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61178580Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62178580Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63178580Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64178580Simp * SUCH DAMAGE.
65178580Simp *
66178580Simp *	from: @(#)SYS.h	8.1 (Berkeley) 6/4/93
67178580Simp */
68178580Simp
69178580Simp#include <sys/syscall.h>
70178580Simp
71178580Simp#include <machine/asm.h>
72178580Simp
73178580Simp/*
74178580Simp * If compiling for shared libs, Emit sysV ABI PIC segment pseudo-ops.
75178580Simp *
76178580Simp * i)  Emit .abicalls before .LEAF entrypoint, and .cpload/.cprestore after.
77178580Simp * ii) Do interprocedure jumps indirectly via t9, with the side-effect of
78178580Simp *     preserving the callee's entry address in t9.
79178580Simp */
80178580Simp#ifdef __ABICALLS__
81178580Simp	.abicalls
82209231Sjchandra# if defined(__mips_o32) || defined(__mips_o64)
83209231Sjchandra#  define PIC_PROLOGUE(x)	SETUP_GP
84209231Sjchandra#  define PIC_TAILCALL(l)	PTR_LA t9, _C_LABEL(l); jr t9
85209231Sjchandra#  define PIC_RETURN()		j ra
86209231Sjchandra# else
87209231Sjchandra#  define PIC_PROLOGUE(x)	SETUP_GP64(t3, x)
88209231Sjchandra#  define PIC_TAILCALL(l)	PTR_LA t9, _C_LABEL(l); RESTORE_GP64; jr t9
89209231Sjchandra#  define PIC_RETURN()		RESTORE_GP64; j ra
90209231Sjchandra# endif
91178580Simp#else
92209231Sjchandra# define PIC_PROLOGUE(x)
93209231Sjchandra# define PIC_TAILCALL(l)	j  _C_LABEL(l)
94214260Sjchandra# define PIC_RETURN()		j ra
95209231Sjchandra#endif /* __ABICALLS__ */
96178580Simp
97209231Sjchandra# define SYSTRAP(x)	li v0,SYS_ ## x; syscall;
98178580Simp
99178580Simp/*
100178580Simp * Do a syscall that cannot fail (sync, get{p,u,g,eu,eg)id)
101178580Simp */
102178580Simp#define RSYSCALL_NOERROR(x)						\
103178580Simp	PSEUDO_NOERROR(x)
104178580Simp
105178580Simp/*
106178580Simp * Do a normal syscall.
107178580Simp */
108178580Simp#define RSYSCALL(x)							\
109178580Simp	PSEUDO(x)
110178580Simp
111178580Simp/*
112178580Simp * Do a renamed or pseudo syscall (e.g., _exit()), where the entrypoint
113178580Simp * and syscall name are not the same.
114178580Simp */
115178580Simp#define PSEUDO_NOERROR(x)						\
116178580SimpLEAF(__sys_ ## x);							\
117209231Sjchandra	.weak _C_LABEL(x);						\
118178580Simp	_C_LABEL(x) = _C_LABEL(__CONCAT(__sys_,x));			\
119178580Simp	.weak _C_LABEL(__CONCAT(_,x));					\
120178580Simp	_C_LABEL(__CONCAT(_,x)) = _C_LABEL(__CONCAT(__sys_,x));		\
121178580Simp	SYSTRAP(x);							\
122178580Simp	j ra;								\
123178580Simp	END(__sys_ ## x)
124178580Simp
125178580Simp#define PSEUDO(x)							\
126178580SimpLEAF(__sys_ ## x);							\
127209231Sjchandra	.weak _C_LABEL(x);						\
128178580Simp	_C_LABEL(x) = _C_LABEL(__CONCAT(__sys_,x));			\
129178580Simp	.weak _C_LABEL(__CONCAT(_,x));					\
130178580Simp	_C_LABEL(__CONCAT(_,x)) = _C_LABEL(__CONCAT(__sys_,x));		\
131209231Sjchandra	PIC_PROLOGUE(__sys_ ## x);					\
132178580Simp	SYSTRAP(x);							\
133178580Simp	bne a3,zero,err;						\
134209231Sjchandra	PIC_RETURN();							\
135178580Simperr:									\
136209231Sjchandra	PIC_TAILCALL(__cerror);						\
137209231SjchandraEND(__sys_ ## x)
138