1 /*	$NetBSD: freebsd_misc.c,v 1.33 2014/11/09 18:30:38 maxv Exp $	*/
2 
3 /*
4  * Copyright (c) 1995 Frank van der Linden
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed for the NetBSD Project
18  *      by Frank van der Linden
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * FreeBSD compatibility module. Try to deal with various FreeBSD system calls.
36  */
37 
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: freebsd_misc.c,v 1.33 2014/11/09 18:30:38 maxv Exp $");
40 
41 #if defined(_KERNEL_OPT)
42 #include "opt_ntp.h"
43 #endif
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/proc.h>
48 #include <sys/mount.h>
49 #include <sys/signal.h>
50 #include <sys/signalvar.h>
51 #include <sys/mman.h>
52 #include <sys/ktrace.h>
53 
54 #include <sys/syscallargs.h>
55 
56 #include <compat/freebsd/freebsd_syscallargs.h>
57 #include <compat/common/compat_util.h>
58 #include <compat/freebsd/freebsd_rtprio.h>
59 #include <compat/freebsd/freebsd_timex.h>
60 #include <compat/freebsd/freebsd_signal.h>
61 #include <compat/freebsd/freebsd_mman.h>
62 
63 int
freebsd_sys_msync(struct lwp * l,const struct freebsd_sys_msync_args * uap,register_t * retval)64 freebsd_sys_msync(struct lwp *l, const struct freebsd_sys_msync_args *uap, register_t *retval)
65 {
66 	/* {
67 		syscallarg(void *) addr;
68 		syscallarg(size_t) len;
69 		syscallarg(int) flags;
70 	} */
71 	struct sys___msync13_args bma;
72 
73 	/*
74 	 * FreeBSD-2.0-RELEASE's msync(2) is compatible with NetBSD's.
75 	 * FreeBSD-2.0.5-RELEASE's msync(2) has addtional argument `flags',
76 	 * but syscall number is not changed. :-<
77 	 */
78 	SCARG(&bma, addr) = SCARG(uap, addr);
79 	SCARG(&bma, len) = SCARG(uap, len);
80 	SCARG(&bma, flags) = SCARG(uap, flags);
81 	return sys___msync13(l, &bma, retval);
82 }
83 
84 int
freebsd_sys_mmap(struct lwp * l,const struct freebsd_sys_mmap_args * uap,register_t * retval)85 freebsd_sys_mmap(struct lwp *l, const struct freebsd_sys_mmap_args *uap, register_t *retval)
86 {
87 	/* {
88 		syscallarg(void *) addr;
89 		syscallarg(size_t) len;
90 		syscallarg(int) prot;
91 		syscallarg(int) flags;
92 		syscallarg(int) fd;
93 		syscallarg(long) pad;
94 		syscallarg(off_t) pos;
95 	} */
96 	struct sys_mmap_args bma;
97 	int flags, prot, fd;
98 	off_t pos;
99 
100 	prot = SCARG(uap, prot);
101 	flags = SCARG(uap, flags);
102 	fd = SCARG(uap, fd);
103 	pos = SCARG(uap, pos);
104 
105 	/*
106 	 * If using MAP_STACK on FreeBSD:
107 	 *
108 	 * + fd has to be -1
109 	 * + prot must have read and write
110 	 * + MAP_STACK implies MAP_ANON
111 	 * + MAP_STACK implies offset of 0
112 	 */
113 	if (flags & FREEBSD_MAP_STACK) {
114 		if ((fd != -1)
115 		    ||((prot & (PROT_READ|PROT_WRITE))!=(PROT_READ|PROT_WRITE)))
116 			return (EINVAL);
117 
118 		flags |= (MAP_ANON | MAP_FIXED);
119 		flags &= ~FREEBSD_MAP_STACK;
120 
121 		pos = 0;
122 	}
123 
124 	SCARG(&bma, addr) = SCARG(uap, addr);
125 	SCARG(&bma, len) = SCARG(uap, len);
126 	SCARG(&bma, prot) = prot;
127 	SCARG(&bma, flags) = flags;
128 	SCARG(&bma, fd) = fd;
129 	SCARG(&bma, pos) = pos;
130 
131 	return sys_mmap(l, &bma, retval);
132 }
133 
134 /* just a place holder */
135 
136 int
freebsd_sys_rtprio(struct lwp * l,const struct freebsd_sys_rtprio_args * uap,register_t * retval)137 freebsd_sys_rtprio(struct lwp *l, const struct freebsd_sys_rtprio_args *uap, register_t *retval)
138 {
139 	/* {
140 		syscallarg(int) function;
141 		syscallarg(pid_t) pid;
142 		syscallarg(struct freebsd_rtprio *) rtp;
143 	} */
144 
145 	return ENOSYS;	/* XXX */
146 }
147 
148 #ifdef NTP
149 int
freebsd_ntp_adjtime(struct lwp * l,const struct freebsd_ntp_adjtime_args * uap,register_t * retval)150 freebsd_ntp_adjtime(struct lwp *l, const struct freebsd_ntp_adjtime_args *uap, register_t *retval)
151 {
152 	/* {
153 		syscallarg(struct freebsd_timex *) tp;
154 	} */
155 
156 	return ENOSYS;	/* XXX */
157 }
158 #endif
159 
160 int
freebsd_sys_sigaction4(struct lwp * l,const struct freebsd_sys_sigaction4_args * uap,register_t * retval)161 freebsd_sys_sigaction4(struct lwp *l, const struct freebsd_sys_sigaction4_args *uap, register_t *retval)
162 {
163 	/* {
164 		syscallarg(int) signum;
165 		syscallarg(const struct freebsd_sigaction4 *) nsa;
166 		syscallarg(struct freebsd_sigaction4 *) osa;
167 	} */
168 	struct freebsd_sigaction4 nesa, oesa;
169 	struct sigaction nbsa, obsa;
170 	int error;
171 
172 	if (SCARG(uap, nsa)) {
173 		error = copyin(SCARG(uap, nsa), &nesa, sizeof(nesa));
174 		if (error)
175 			return (error);
176 		nbsa.sa_handler = nesa.freebsd_sa_handler;
177 		nbsa.sa_mask    = nesa.freebsd_sa_mask;
178 		nbsa.sa_flags   = nesa.freebsd_sa_flags;
179 	}
180 	error = sigaction1(l, SCARG(uap, signum),
181 	    SCARG(uap, nsa) ? &nbsa : 0, SCARG(uap, osa) ? &obsa : 0,
182 	    NULL, 0);
183 	if (error)
184 		return (error);
185 	if (SCARG(uap, osa)) {
186 		oesa.freebsd_sa_handler = obsa.sa_handler;
187 		oesa.freebsd_sa_mask    = obsa.sa_mask;
188 		oesa.freebsd_sa_flags   = obsa.sa_flags;
189 		error = copyout(&oesa, SCARG(uap, osa), sizeof(oesa));
190 		if (error)
191 			return (error);
192 	}
193 	return (0);
194 }
195 
196 int
freebsd_sys_utrace(struct lwp * l,const struct freebsd_sys_utrace_args * uap,register_t * retval)197 freebsd_sys_utrace(struct lwp *l, const struct freebsd_sys_utrace_args *uap, register_t *retval)
198 {
199 	/* {
200 		syscallarg(void *) addr;
201 		syscallarg(size_t) len;
202 	} */
203 
204 	return ktruser("FreeBSD utrace", SCARG(uap, addr), SCARG(uap, len),
205 	    0);
206 }
207