netbsd32_compat_30.c revision 1.31.16.9 1 1.31.16.9 pgoyette /* $NetBSD: netbsd32_compat_30.c,v 1.31.16.9 2018/09/23 08:01:13 pgoyette Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (c) 1998, 2001 Matthew R. Green
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * Redistribution and use in source and binary forms, with or without
8 1.1 christos * modification, are permitted provided that the following conditions
9 1.1 christos * are met:
10 1.1 christos * 1. Redistributions of source code must retain the above copyright
11 1.1 christos * notice, this list of conditions and the following disclaimer.
12 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 christos * notice, this list of conditions and the following disclaimer in the
14 1.1 christos * documentation and/or other materials provided with the distribution.
15 1.1 christos *
16 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 christos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 christos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 christos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 christos * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 christos * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 christos * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 christos * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 christos * SUCH DAMAGE.
27 1.1 christos */
28 1.1 christos
29 1.1 christos #include <sys/cdefs.h>
30 1.31.16.9 pgoyette __KERNEL_RCSID(0, "$NetBSD: netbsd32_compat_30.c,v 1.31.16.9 2018/09/23 08:01:13 pgoyette Exp $");
31 1.31.16.8 pgoyette
32 1.31.16.8 pgoyette #if defined(_KERNEL_OPT)
33 1.31.16.8 pgoyette #include <opt_ntp.h>
34 1.31.16.8 pgoyette #endif
35 1.1 christos
36 1.1 christos #include <sys/param.h>
37 1.1 christos #include <sys/systm.h>
38 1.31.16.5 pgoyette #include <sys/module.h>
39 1.1 christos #include <sys/mount.h>
40 1.31.16.2 pgoyette #include <sys/mount.h>
41 1.1 christos #include <sys/socket.h>
42 1.1 christos #include <sys/socketvar.h>
43 1.1 christos #include <sys/stat.h>
44 1.1 christos #include <sys/time.h>
45 1.1 christos #include <sys/ktrace.h>
46 1.1 christos #include <sys/resourcevar.h>
47 1.1 christos #include <sys/vnode.h>
48 1.1 christos #include <sys/file.h>
49 1.1 christos #include <sys/filedesc.h>
50 1.1 christos #include <sys/namei.h>
51 1.1 christos #include <sys/statvfs.h>
52 1.1 christos #include <sys/syscallargs.h>
53 1.31.16.3 pgoyette #include <sys/syscallvar.h>
54 1.1 christos #include <sys/proc.h>
55 1.2 christos #include <sys/dirent.h>
56 1.7 elad #include <sys/kauth.h>
57 1.18 dsl #include <sys/vfs_syscalls.h>
58 1.1 christos
59 1.1 christos #include <compat/netbsd32/netbsd32.h>
60 1.31.16.4 pgoyette #include <compat/netbsd32/netbsd32_syscall.h>
61 1.1 christos #include <compat/netbsd32/netbsd32_syscallargs.h>
62 1.1 christos #include <compat/netbsd32/netbsd32_conv.h>
63 1.9 martin #include <compat/sys/mount.h>
64 1.1 christos
65 1.1 christos int
66 1.23 dsl compat_30_netbsd32_getdents(struct lwp *l, const struct compat_30_netbsd32_getdents_args *uap, register_t *retval)
67 1.1 christos {
68 1.23 dsl /* {
69 1.1 christos syscallarg(int) fd;
70 1.1 christos syscallarg(netbsd32_charp) buf;
71 1.1 christos syscallarg(netbsd32_size_t) count;
72 1.23 dsl } */
73 1.24 ad file_t *fp;
74 1.1 christos int error, done;
75 1.1 christos char *buf;
76 1.8 simonb netbsd32_size_t count;
77 1.1 christos
78 1.8 simonb /* Limit the size on any kernel buffers used by VOP_READDIR */
79 1.31.16.1 pgoyette count = uimin(MAXBSIZE, SCARG(uap, count));
80 1.8 simonb
81 1.26 ad /* fd_getvnode() will use the descriptor for us */
82 1.24 ad if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
83 1.1 christos return (error);
84 1.1 christos if ((fp->f_flag & FREAD) == 0) {
85 1.1 christos error = EBADF;
86 1.1 christos goto out;
87 1.1 christos }
88 1.31 maxv if (count == 0)
89 1.31 maxv goto out;
90 1.31 maxv
91 1.30 rmind buf = kmem_alloc(count, KM_SLEEP);
92 1.8 simonb error = vn_readdir(fp, buf, UIO_SYSSPACE, count, &done, l, 0, 0);
93 1.1 christos if (error == 0) {
94 1.1 christos *retval = netbsd32_to_dirent12(buf, done);
95 1.19 dsl error = copyout(buf, SCARG_P32(uap, buf), *retval);
96 1.1 christos }
97 1.30 rmind kmem_free(buf, count);
98 1.1 christos out:
99 1.24 ad fd_putfile(SCARG(uap, fd));
100 1.1 christos return (error);
101 1.1 christos }
102 1.1 christos
103 1.1 christos int
104 1.23 dsl compat_30_netbsd32___stat13(struct lwp *l, const struct compat_30_netbsd32___stat13_args *uap, register_t *retval)
105 1.1 christos {
106 1.23 dsl /* {
107 1.1 christos syscallarg(const netbsd32_charp) path;
108 1.1 christos syscallarg(netbsd32_stat13p_t) ub;
109 1.23 dsl } */
110 1.2 christos struct netbsd32_stat13 sb32;
111 1.1 christos struct stat sb;
112 1.1 christos int error;
113 1.1 christos const char *path;
114 1.1 christos
115 1.19 dsl path = SCARG_P32(uap, path);
116 1.1 christos
117 1.24 ad error = do_sys_stat(path, FOLLOW, &sb);
118 1.1 christos if (error)
119 1.1 christos return (error);
120 1.1 christos netbsd32_from___stat13(&sb, &sb32);
121 1.19 dsl error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
122 1.1 christos return (error);
123 1.1 christos }
124 1.1 christos
125 1.1 christos int
126 1.23 dsl compat_30_netbsd32___fstat13(struct lwp *l, const struct compat_30_netbsd32___fstat13_args *uap, register_t *retval)
127 1.1 christos {
128 1.23 dsl /* {
129 1.1 christos syscallarg(int) fd;
130 1.1 christos syscallarg(netbsd32_stat13p_t) sb;
131 1.23 dsl } */
132 1.2 christos struct netbsd32_stat13 sb32;
133 1.1 christos struct stat ub;
134 1.29 njoly int error;
135 1.1 christos
136 1.29 njoly error = do_sys_fstat(SCARG(uap, fd), &ub);
137 1.1 christos if (error == 0) {
138 1.1 christos netbsd32_from___stat13(&ub, &sb32);
139 1.19 dsl error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb32));
140 1.1 christos }
141 1.1 christos return (error);
142 1.1 christos }
143 1.1 christos
144 1.1 christos int
145 1.23 dsl compat_30_netbsd32___lstat13(struct lwp *l, const struct compat_30_netbsd32___lstat13_args *uap, register_t *retval)
146 1.1 christos {
147 1.23 dsl /* {
148 1.1 christos syscallarg(const netbsd32_charp) path;
149 1.1 christos syscallarg(netbsd32_stat13p_t) ub;
150 1.23 dsl } */
151 1.2 christos struct netbsd32_stat13 sb32;
152 1.1 christos struct stat sb;
153 1.1 christos int error;
154 1.1 christos const char *path;
155 1.1 christos
156 1.19 dsl path = SCARG_P32(uap, path);
157 1.1 christos
158 1.24 ad error = do_sys_stat(path, NOFOLLOW, &sb);
159 1.1 christos if (error)
160 1.1 christos return (error);
161 1.1 christos netbsd32_from___stat13(&sb, &sb32);
162 1.19 dsl error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
163 1.1 christos return (error);
164 1.1 christos }
165 1.6 cube
166 1.6 cube int
167 1.23 dsl compat_30_netbsd32_fhstat(struct lwp *l, const struct compat_30_netbsd32_fhstat_args *uap, register_t *retval)
168 1.6 cube {
169 1.23 dsl /* {
170 1.6 cube syscallarg(const netbsd32_fhandlep_t) fhp;
171 1.19 dsl syscallarg(netbsd32_stat13p_t) sb;
172 1.23 dsl } */
173 1.6 cube struct stat sb;
174 1.6 cube struct netbsd32_stat13 sb32;
175 1.6 cube int error;
176 1.9 martin struct compat_30_fhandle fh;
177 1.6 cube struct mount *mp;
178 1.6 cube struct vnode *vp;
179 1.6 cube
180 1.6 cube /*
181 1.6 cube * Must be super user
182 1.6 cube */
183 1.15 elad if ((error = kauth_authorize_system(l->l_cred,
184 1.15 elad KAUTH_SYSTEM_FILEHANDLE, 0, NULL, NULL, NULL)))
185 1.6 cube return (error);
186 1.6 cube
187 1.19 dsl if ((error = copyin(SCARG_P32(uap, fhp), &fh, sizeof(fh))) != 0)
188 1.6 cube return (error);
189 1.6 cube
190 1.6 cube if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
191 1.6 cube return (ESTALE);
192 1.6 cube if (mp->mnt_op->vfs_fhtovp == NULL)
193 1.6 cube return EOPNOTSUPP;
194 1.9 martin if ((error = VFS_FHTOVP(mp, (struct fid*)&fh.fh_fid, &vp)))
195 1.6 cube return (error);
196 1.24 ad error = vn_stat(vp, &sb);
197 1.6 cube vput(vp);
198 1.6 cube if (error)
199 1.6 cube return (error);
200 1.6 cube netbsd32_from___stat13(&sb, &sb32);
201 1.19 dsl error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb));
202 1.6 cube return (error);
203 1.6 cube }
204 1.9 martin
205 1.9 martin int
206 1.23 dsl compat_30_netbsd32_fhstatvfs1(struct lwp *l, const struct compat_30_netbsd32_fhstatvfs1_args *uap, register_t *retval)
207 1.12 martin {
208 1.23 dsl /* {
209 1.12 martin syscallarg(const netbsd32_fhandlep_t) fhp;
210 1.12 martin syscallarg(netbsd32_statvfsp_t) buf;
211 1.12 martin syscallarg(int) flags;
212 1.23 dsl } */
213 1.12 martin struct statvfs *sbuf;
214 1.12 martin struct netbsd32_statvfs *s32;
215 1.12 martin int error;
216 1.12 martin
217 1.21 dsl sbuf = STATVFSBUF_GET();
218 1.21 dsl error = do_fhstatvfs(l, SCARG_P32(uap, fhp), FHANDLE_SIZE_COMPAT, sbuf,
219 1.21 dsl SCARG(uap, flags));
220 1.21 dsl
221 1.21 dsl if (error != 0) {
222 1.30 rmind s32 = kmem_alloc(sizeof(*s32), KM_SLEEP);
223 1.21 dsl netbsd32_from_statvfs(sbuf, s32);
224 1.21 dsl error = copyout(s32, SCARG_P32(uap, buf), sizeof *s32);
225 1.30 rmind kmem_free(s32, sizeof(*s32));
226 1.21 dsl }
227 1.21 dsl STATVFSBUF_PUT(sbuf);
228 1.12 martin
229 1.12 martin return (error);
230 1.12 martin }
231 1.12 martin
232 1.12 martin int
233 1.23 dsl compat_30_netbsd32_socket(struct lwp *l, const struct compat_30_netbsd32_socket_args *uap, register_t *retval)
234 1.9 martin {
235 1.23 dsl /* {
236 1.9 martin syscallarg(int) domain;
237 1.9 martin syscallarg(int) type;
238 1.9 martin syscallarg(int) protocol;
239 1.23 dsl } */
240 1.9 martin struct compat_30_sys_socket_args ua;
241 1.9 martin
242 1.9 martin NETBSD32TO64_UAP(domain);
243 1.9 martin NETBSD32TO64_UAP(type);
244 1.9 martin NETBSD32TO64_UAP(protocol);
245 1.9 martin return (compat_30_sys_socket(l, &ua, retval));
246 1.9 martin }
247 1.9 martin
248 1.9 martin int
249 1.23 dsl compat_30_netbsd32_getfh(struct lwp *l, const struct compat_30_netbsd32_getfh_args *uap, register_t *retval)
250 1.9 martin {
251 1.23 dsl /* {
252 1.9 martin syscallarg(const netbsd32_charp) fname;
253 1.9 martin syscallarg(netbsd32_compat_30_fhandlep_t) fhp;
254 1.23 dsl } */
255 1.9 martin struct compat_30_sys_getfh_args ua;
256 1.9 martin
257 1.9 martin NETBSD32TOP_UAP(fname, const char);
258 1.9 martin NETBSD32TOP_UAP(fhp, struct compat_30_fhandle);
259 1.9 martin /* Lucky for us a fhandle_t doesn't change sizes */
260 1.9 martin return (compat_30_sys_getfh(l, &ua, retval));
261 1.9 martin }
262 1.12 martin
263 1.12 martin
264 1.23 dsl int
265 1.28 christos compat_30_netbsd32___fhstat30(struct lwp *l, const struct compat_30_netbsd32___fhstat30_args *uap, register_t *retval)
266 1.12 martin {
267 1.23 dsl /* {
268 1.12 martin syscallarg(const netbsd32_fhandlep_t) fhp;
269 1.12 martin syscallarg(netbsd32_statp_t) sb;
270 1.23 dsl } */
271 1.12 martin struct stat sb;
272 1.28 christos struct netbsd32_stat50 sb32;
273 1.12 martin int error;
274 1.12 martin
275 1.21 dsl error = do_fhstat(l, SCARG_P32(uap, fhp), FHANDLE_SIZE_COMPAT, &sb);
276 1.21 dsl if (error)
277 1.12 martin return error;
278 1.12 martin
279 1.28 christos netbsd32_from___stat50(&sb, &sb32);
280 1.21 dsl error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb32));
281 1.12 martin return error;
282 1.12 martin }
283 1.12 martin
284 1.12 martin /*
285 1.12 martin * Open a file given a file handle.
286 1.12 martin *
287 1.12 martin * Check permissions, allocate an open file structure,
288 1.12 martin * and call the device open routine if any.
289 1.12 martin */
290 1.12 martin int
291 1.23 dsl compat_30_netbsd32_fhopen(struct lwp *l, const struct compat_30_netbsd32_fhopen_args *uap, register_t *retval)
292 1.12 martin {
293 1.23 dsl /* {
294 1.12 martin syscallarg(const fhandle_t *) fhp;
295 1.12 martin syscallarg(int) flags;
296 1.23 dsl } */
297 1.12 martin struct compat_30_sys_fhopen_args ua;
298 1.12 martin
299 1.12 martin NETBSD32TOP_UAP(fhp, struct compat_30_fhandle);
300 1.12 martin NETBSD32TO64_UAP(flags);
301 1.12 martin return (compat_30_sys_fhopen(l, &ua, retval));
302 1.12 martin }
303 1.31.16.2 pgoyette
304 1.31.16.8 pgoyette int
305 1.31.16.8 pgoyette compat_30_netbsd32_ntp_gettime(struct lwp *l, const struct compat_30_netbsd32_ntp_gettime_args *uap, register_t *retval)
306 1.31.16.8 pgoyette {
307 1.31.16.9 pgoyette #ifdef NTP
308 1.31.16.8 pgoyette /* {
309 1.31.16.8 pgoyette syscallarg(netbsd32_ntptimevalp_t) ntvp;
310 1.31.16.8 pgoyette } */
311 1.31.16.8 pgoyette struct netbsd32_ntptimeval30 ntv32;
312 1.31.16.8 pgoyette struct ntptimeval ntv;
313 1.31.16.8 pgoyette int error = 0;
314 1.31.16.8 pgoyette
315 1.31.16.8 pgoyette if (SCARG_P32(uap, ntvp)) {
316 1.31.16.8 pgoyette ntp_gettime(&ntv);
317 1.31.16.8 pgoyette
318 1.31.16.8 pgoyette ntv32.time.tv_sec = ntv.time.tv_sec;
319 1.31.16.8 pgoyette ntv32.time.tv_usec = ntv.time.tv_nsec / 1000;
320 1.31.16.8 pgoyette ntv32.maxerror = (netbsd32_long)ntv.maxerror;
321 1.31.16.8 pgoyette ntv32.esterror = (netbsd32_long)ntv.esterror;
322 1.31.16.8 pgoyette error = copyout(&ntv32, SCARG_P32(uap, ntvp), sizeof(ntv32));
323 1.31.16.8 pgoyette }
324 1.31.16.8 pgoyette if (!error) {
325 1.31.16.8 pgoyette *retval = ntp_timestatus();
326 1.31.16.8 pgoyette }
327 1.31.16.8 pgoyette
328 1.31.16.8 pgoyette return (error);
329 1.31.16.9 pgoyette #else
330 1.31.16.9 pgoyette
331 1.31.16.9 pgoyette return ENOSYS;
332 1.31.16.8 pgoyette #endif
333 1.31.16.9 pgoyette }
334 1.31.16.8 pgoyette
335 1.31.16.2 pgoyette static struct syscall_package compat_netbsd32_30_syscalls[] = {
336 1.31.16.4 pgoyette { NETBSD32_SYS_compat_30_netbsd32_getdents, 0,
337 1.31.16.2 pgoyette (sy_call_t *)compat_30_netbsd32_getdents },
338 1.31.16.4 pgoyette { NETBSD32_SYS_compat_30_netbsd32___stat13, 0,
339 1.31.16.2 pgoyette (sy_call_t *)compat_30_netbsd32___stat13 },
340 1.31.16.4 pgoyette { NETBSD32_SYS_compat_30_netbsd32___fstat13, 0,
341 1.31.16.2 pgoyette (sy_call_t *)compat_30_netbsd32___fstat13 },
342 1.31.16.4 pgoyette { NETBSD32_SYS_compat_30_netbsd32___lstat13, 0,
343 1.31.16.2 pgoyette (sy_call_t *)compat_30_netbsd32___lstat13 },
344 1.31.16.4 pgoyette { NETBSD32_SYS_compat_30_netbsd32_fhstat, 0,
345 1.31.16.2 pgoyette (sy_call_t *)compat_30_netbsd32_fhstat },
346 1.31.16.4 pgoyette { NETBSD32_SYS_compat_30_netbsd32_fhstatvfs1, 0,
347 1.31.16.2 pgoyette (sy_call_t *)compat_30_netbsd32_fhstatvfs1 },
348 1.31.16.4 pgoyette { NETBSD32_SYS_compat_30_netbsd32_socket, 0,
349 1.31.16.2 pgoyette (sy_call_t *)compat_30_netbsd32_socket },
350 1.31.16.4 pgoyette { NETBSD32_SYS_compat_30_netbsd32_getfh, 0,
351 1.31.16.2 pgoyette (sy_call_t *)compat_30_netbsd32_getfh },
352 1.31.16.4 pgoyette { NETBSD32_SYS_compat_30_netbsd32___fhstat30, 0,
353 1.31.16.2 pgoyette (sy_call_t *)compat_30_netbsd32___fhstat30 },
354 1.31.16.4 pgoyette { NETBSD32_SYS_compat_30_netbsd32_fhopen, 0,
355 1.31.16.5 pgoyette (sy_call_t *)compat_30_netbsd32_fhopen },
356 1.31.16.8 pgoyette { NETBSD32_SYS_compat_30_netbsd32_ntp_gettime, 0,
357 1.31.16.8 pgoyette (sy_call_t *)compat_30_netbsd32_ntp_gettime },
358 1.31.16.2 pgoyette { 0, 0, NULL }
359 1.31.16.2 pgoyette };
360 1.31.16.2 pgoyette
361 1.31.16.2 pgoyette MODULE(MODULE_CLASS_EXEC, compat_netbsd32_30, "compat_netbsd32,compat_30");
362 1.31.16.2 pgoyette
363 1.31.16.2 pgoyette static int
364 1.31.16.2 pgoyette compat_netbsd32_30_modcmd(modcmd_t cmd, void *arg)
365 1.31.16.2 pgoyette {
366 1.31.16.2 pgoyette
367 1.31.16.2 pgoyette switch (cmd) {
368 1.31.16.2 pgoyette case MODULE_CMD_INIT:
369 1.31.16.5 pgoyette return syscall_establish(&emul_netbsd32,
370 1.31.16.5 pgoyette compat_netbsd32_30_syscalls);
371 1.31.16.2 pgoyette
372 1.31.16.2 pgoyette case MODULE_CMD_FINI:
373 1.31.16.5 pgoyette return syscall_disestablish(&emul_netbsd32,
374 1.31.16.5 pgoyette compat_netbsd32_30_syscalls);
375 1.31.16.2 pgoyette
376 1.31.16.2 pgoyette default:
377 1.31.16.2 pgoyette return ENOTTY;
378 1.31.16.2 pgoyette }
379 1.31.16.2 pgoyette }
380