xref: /netbsd-src/sys/compat/netbsd32/netbsd32_compat_50.c (revision bbde328be4e75ea9ad02e9715ea13ca54b797ada)
1 /*	$NetBSD: netbsd32_compat_50.c,v 1.16 2010/04/23 15:19:20 rmind Exp $	*/
2 
3 /*-
4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: netbsd32_compat_50.c,v 1.16 2010/04/23 15:19:20 rmind Exp $");
40 
41 #if defined(_KERNEL_OPT)
42 #include "opt_sysv.h"
43 #endif
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/mount.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/stat.h>
51 #include <sys/time.h>
52 #include <sys/ktrace.h>
53 #include <sys/eventvar.h>
54 #include <sys/resourcevar.h>
55 #include <sys/vnode.h>
56 #include <sys/file.h>
57 #include <sys/filedesc.h>
58 #include <sys/poll.h>
59 #include <sys/namei.h>
60 #include <sys/statvfs.h>
61 #include <sys/syscallargs.h>
62 #include <sys/proc.h>
63 #include <sys/dirent.h>
64 #include <sys/kauth.h>
65 #include <sys/vfs_syscalls.h>
66 #include <sys/ipc.h>
67 #include <sys/msg.h>
68 #include <sys/sem.h>
69 #include <sys/shm.h>
70 
71 #include <compat/netbsd32/netbsd32.h>
72 #include <compat/netbsd32/netbsd32_syscallargs.h>
73 #include <compat/netbsd32/netbsd32_conv.h>
74 #include <compat/sys/mount.h>
75 #include <compat/sys/time.h>
76 
77 
78 /*
79  * Common routine to set access and modification times given a vnode.
80  */
81 static int
82 get_utimes32(const netbsd32_timeval50p_t *tptr, struct timeval *tv,
83     struct timeval **tvp)
84 {
85 	int error;
86 	struct netbsd32_timeval50 tv32[2];
87 
88 	if (tptr == NULL) {
89 		*tvp = NULL;
90 		return 0;
91 	}
92 
93 	error = copyin(tptr, tv32, sizeof(tv32));
94 	if (error)
95 		return error;
96 	netbsd32_to_timeval50(&tv32[0], &tv[0]);
97 	netbsd32_to_timeval50(&tv32[1], &tv[1]);
98 
99 	*tvp = tv;
100 	return 0;
101 }
102 
103 int
104 compat_50_netbsd32_mknod(struct lwp *l,
105     const struct compat_50_netbsd32_mknod_args *uap, register_t *retval)
106 {
107 	/* {
108 		syscallarg(netbsd32_charp) path;
109 		syscallarg(mode_t) mode;
110 		syscallarg(uint32_t) dev;
111 	} */
112 	return do_sys_mknod(l, SCARG_P32(uap, path), SCARG(uap, mode),
113 	    SCARG(uap, dev), retval, UIO_USERSPACE);
114 }
115 
116 int
117 compat_50_netbsd32_select(struct lwp *l,
118     const struct compat_50_netbsd32_select_args *uap, register_t *retval)
119 {
120 	/* {
121 		syscallarg(int) nd;
122 		syscallarg(netbsd32_fd_setp_t) in;
123 		syscallarg(netbsd32_fd_setp_t) ou;
124 		syscallarg(netbsd32_fd_setp_t) ex;
125 		syscallarg(netbsd32_timeval50p_t) tv;
126 	} */
127 	int error;
128 	struct netbsd32_timeval50 tv32;
129 	struct timespec ats, *ts = NULL;
130 
131 	if (SCARG_P32(uap, tv)) {
132 		error = copyin(SCARG_P32(uap, tv), &tv32, sizeof(tv32));
133 		if (error != 0)
134 			return error;
135 		ats.tv_sec = tv32.tv_sec;
136 		ats.tv_nsec = tv32.tv_usec * 1000;
137 		ts = &ats;
138 	}
139 
140 	return selcommon(retval, SCARG(uap, nd), SCARG_P32(uap, in),
141 	    SCARG_P32(uap, ou), SCARG_P32(uap, ex), ts, NULL);
142 	return 0;
143 }
144 
145 int
146 compat_50_netbsd32_gettimeofday(struct lwp *l,
147     const struct compat_50_netbsd32_gettimeofday_args *uap, register_t *retval)
148 {
149 	/* {
150 		syscallarg(netbsd32_timeval50p_t) tp;
151 		syscallarg(netbsd32_timezonep_t) tzp;
152 	} */
153 	struct timeval atv;
154 	struct netbsd32_timeval50 tv32;
155 	int error = 0;
156 	struct netbsd32_timezone tzfake;
157 
158 	if (SCARG_P32(uap, tp)) {
159 		microtime(&atv);
160 		netbsd32_from_timeval50(&atv, &tv32);
161 		error = copyout(&tv32, SCARG_P32(uap, tp), sizeof(tv32));
162 		if (error)
163 			return error;
164 	}
165 	if (SCARG_P32(uap, tzp)) {
166 		/*
167 		 * NetBSD has no kernel notion of time zone, so we just
168 		 * fake up a timezone struct and return it if demanded.
169 		 */
170 		tzfake.tz_minuteswest = 0;
171 		tzfake.tz_dsttime = 0;
172 		error = copyout(&tzfake, SCARG_P32(uap, tzp), sizeof(tzfake));
173 	}
174 	return error;
175 }
176 
177 int
178 compat_50_netbsd32_settimeofday(struct lwp *l,
179     const struct compat_50_netbsd32_settimeofday_args *uap, register_t *retval)
180 {
181 	/* {
182 		syscallarg(const netbsd32_timeval50p_t) tv;
183 		syscallarg(const netbsd32_timezonep_t) tzp;
184 	} */
185 	struct netbsd32_timeval50 atv32;
186 	struct timeval atv;
187 	struct timespec ats;
188 	int error;
189 	struct proc *p = l->l_proc;
190 
191 	/* Verify all parameters before changing time. */
192 
193 	/*
194 	 * NetBSD has no kernel notion of time zone, and only an
195 	 * obsolete program would try to set it, so we log a warning.
196 	 */
197 	if (SCARG_P32(uap, tzp))
198 		printf("pid %d attempted to set the "
199 		    "(obsolete) kernel time zone\n", p->p_pid);
200 
201 	if (SCARG_P32(uap, tv) == 0)
202 		return 0;
203 
204 	if ((error = copyin(SCARG_P32(uap, tv), &atv32, sizeof(atv32))) != 0)
205 		return error;
206 
207 	netbsd32_to_timeval50(&atv32, &atv);
208 	TIMEVAL_TO_TIMESPEC(&atv, &ats);
209 	return settime(p, &ats);
210 }
211 
212 int
213 compat_50_netbsd32_utimes(struct lwp *l,
214     const struct compat_50_netbsd32_utimes_args *uap, register_t *retval)
215 {
216 	/* {
217 		syscallarg(const netbsd32_charp) path;
218 		syscallarg(const netbsd32_timeval50p_t) tptr;
219 	} */
220 	int error;
221 	struct timeval tv[2], *tvp;
222 
223 	error = get_utimes32(SCARG_P32(uap, tptr), tv, &tvp);
224 	if (error != 0)
225 		return error;
226 
227 	return do_sys_utimes(l, NULL, SCARG_P32(uap, path), FOLLOW,
228 	    tvp, UIO_SYSSPACE);
229 }
230 
231 int
232 compat_50_netbsd32_adjtime(struct lwp *l,
233     const struct compat_50_netbsd32_adjtime_args *uap, register_t *retval)
234 {
235 	/* {
236 		syscallarg(const netbsd32_timeval50p_t) delta;
237 		syscallarg(netbsd32_timeval50p_t) olddelta;
238 	} */
239 	struct netbsd32_timeval50 atv;
240 	int error;
241 
242 	extern int time_adjusted;     /* in kern_ntptime.c */
243 	extern int64_t time_adjtime;  /* in kern_ntptime.c */
244 
245 	if ((error = kauth_authorize_system(l->l_cred,
246 	    KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_ADJTIME, NULL, NULL,
247 	    NULL)) != 0)
248 		return (error);
249 
250 	if (SCARG_P32(uap, olddelta)) {
251 		atv.tv_sec = time_adjtime / 1000000;
252 		atv.tv_usec = time_adjtime % 1000000;
253 		if (atv.tv_usec < 0) {
254 			atv.tv_usec += 1000000;
255 			atv.tv_sec--;
256 		}
257 		(void) copyout(&atv,
258 			       SCARG_P32(uap, olddelta),
259 			       sizeof(atv));
260 		if (error)
261 			return (error);
262 	}
263 
264 	if (SCARG_P32(uap, delta)) {
265 		error = copyin(SCARG_P32(uap, delta), &atv,
266 			       sizeof(struct timeval));
267 		if (error)
268 			return (error);
269 
270 		time_adjtime = (int64_t)atv.tv_sec * 1000000 + atv.tv_usec;
271 
272 		if (time_adjtime)
273 			/* We need to save the system time during shutdown */
274 			time_adjusted |= 1;
275 	}
276 
277 	return 0;
278 }
279 
280 int
281 compat_50_netbsd32_futimes(struct lwp *l,
282     const struct compat_50_netbsd32_futimes_args *uap, register_t *retval)
283 {
284 	/* {
285 		syscallarg(int) fd;
286 		syscallarg(const netbsd32_timeval50p_t) tptr;
287 	} */
288 	int error;
289 	file_t *fp;
290 	struct timeval tv[2], *tvp;
291 
292 	error = get_utimes32(SCARG_P32(uap, tptr), tv, &tvp);
293 	if (error != 0)
294 		return error;
295 
296 	/* fd_getvnode() will use the descriptor for us */
297 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
298 		return error;
299 
300 	error = do_sys_utimes(l, fp->f_data, NULL, 0, tvp, UIO_SYSSPACE);
301 
302 	fd_putfile(SCARG(uap, fd));
303 	return error;
304 }
305 
306 int
307 compat_50_netbsd32_clock_gettime(struct lwp *l,
308     const struct compat_50_netbsd32_clock_gettime_args *uap, register_t *retval)
309 {
310 	/* {
311 		syscallarg(netbsd32_clockid_t) clock_id;
312 		syscallarg(netbsd32_timespec50p_t) tp;
313 	} */
314 	int error;
315 	struct timespec ats;
316 	struct netbsd32_timespec50 ts32;
317 
318 	error = clock_gettime1(SCARG(uap, clock_id), &ats);
319 	if (error != 0)
320 		return error;
321 
322 	netbsd32_from_timespec50(&ats, &ts32);
323 	return copyout(&ts32, SCARG_P32(uap, tp), sizeof(ts32));
324 }
325 
326 int
327 compat_50_netbsd32_clock_settime(struct lwp *l,
328     const struct compat_50_netbsd32_clock_settime_args *uap, register_t *retval)
329 {
330 	/* {
331 		syscallarg(netbsd32_clockid_t) clock_id;
332 		syscallarg(const netbsd32_timespec50p_t) tp;
333 	} */
334 	struct netbsd32_timespec50 ts32;
335 	struct timespec ats;
336 	int error;
337 
338 	if ((error = copyin(SCARG_P32(uap, tp), &ts32, sizeof(ts32))) != 0)
339 		return (error);
340 
341 	netbsd32_to_timespec50(&ts32, &ats);
342 	return clock_settime1(l->l_proc, SCARG(uap, clock_id), &ats, true);
343 }
344 
345 int
346 compat_50_netbsd32_clock_getres(struct lwp *l,
347     const struct compat_50_netbsd32_clock_getres_args *uap, register_t *retval)
348 {
349 	/* {
350 		syscallarg(netbsd32_clockid_t) clock_id;
351 		syscallarg(netbsd32_timespec50p_t) tp;
352 	} */
353 	struct netbsd32_timespec50 ts32;
354 	struct timespec ts;
355 	int error = 0;
356 
357 	error = clock_getres1(SCARG(uap, clock_id), &ts);
358 	if (error != 0)
359 		return error;
360 
361 	if (SCARG_P32(uap, tp)) {
362 		netbsd32_from_timespec50(&ts, &ts32);
363 		error = copyout(&ts32, SCARG_P32(uap, tp), sizeof(ts32));
364 	}
365 
366 	return error;
367 }
368 
369 int
370 compat_50_netbsd32_timer_settime(struct lwp *l,
371     const struct compat_50_netbsd32_timer_settime_args *uap, register_t *retval)
372 {
373 	/* {
374 		syscallarg(netbsd32_timer_t) timerid;
375 		syscallarg(int) flags;
376 		syscallarg(const netbsd32_itimerspec50p_t) value;
377 		syscallarg(netbsd32_itimerspec50p_t) ovalue;
378 	} */
379 	int error;
380 	struct itimerspec value, ovalue, *ovp = NULL;
381 	struct netbsd32_itimerspec50 its32;
382 
383 	if ((error = copyin(SCARG_P32(uap, value), &its32, sizeof(its32))) != 0)
384 		return (error);
385 	netbsd32_to_timespec50(&its32.it_interval, &value.it_interval);
386 	netbsd32_to_timespec50(&its32.it_value, &value.it_value);
387 
388 	if (SCARG_P32(uap, ovalue))
389 		ovp = &ovalue;
390 
391 	if ((error = dotimer_settime(SCARG(uap, timerid), &value, ovp,
392 	    SCARG(uap, flags), l->l_proc)) != 0)
393 		return error;
394 
395 	if (ovp) {
396 		netbsd32_from_timespec50(&ovp->it_interval, &its32.it_interval);
397 		netbsd32_from_timespec50(&ovp->it_value, &its32.it_value);
398 		return copyout(&its32, SCARG_P32(uap, ovalue), sizeof(its32));
399 	}
400 	return 0;
401 }
402 
403 int
404 compat_50_netbsd32_timer_gettime(struct lwp *l, const struct compat_50_netbsd32_timer_gettime_args *uap, register_t *retval)
405 {
406 	/* {
407 		syscallarg(netbsd32_timer_t) timerid;
408 		syscallarg(netbsd32_itimerspec50p_t) value;
409 	} */
410 	int error;
411 	struct itimerspec its;
412 	struct netbsd32_itimerspec50 its32;
413 
414 	if ((error = dotimer_gettime(SCARG(uap, timerid), l->l_proc,
415 	    &its)) != 0)
416 		return error;
417 
418 	netbsd32_from_timespec50(&its.it_interval, &its32.it_interval);
419 	netbsd32_from_timespec50(&its.it_value, &its32.it_value);
420 
421 	return copyout(&its32, SCARG_P32(uap, value), sizeof(its32));
422 }
423 
424 int
425 compat_50_netbsd32_nanosleep(struct lwp *l,
426     const struct compat_50_netbsd32_nanosleep_args *uap, register_t *retval)
427 {
428 	/* {
429 		syscallarg(const netbsd32_timespec50p_t) rqtp;
430 		syscallarg(netbsd32_timespecp_t) rmtp;
431 	} */
432 	struct netbsd32_timespec50 ts32;
433 	struct timespec rqt, rmt;
434 	int error, error1;
435 
436 	error = copyin(SCARG_P32(uap, rqtp), &ts32, sizeof(ts32));
437 	if (error)
438 		return (error);
439 	netbsd32_to_timespec50(&ts32, &rqt);
440 
441 	error = nanosleep1(l, &rqt, SCARG_P32(uap, rmtp) ? &rmt : NULL);
442 	if (SCARG_P32(uap, rmtp) == NULL || (error != 0 && error != EINTR))
443 		return error;
444 
445 	netbsd32_from_timespec50(&rmt, &ts32);
446 	error1 = copyout(&ts32, SCARG_P32(uap,rmtp), sizeof(ts32));
447 	return error1 ? error1 : error;
448 }
449 
450 static int
451 compat_50_netbsd32_sigtimedwait_put_info(const void *src, void *dst, size_t size)
452 {
453 	const siginfo_t *info = src;
454 	siginfo32_t info32;
455 
456 	netbsd32_si_to_si32(&info32, info);
457 
458 	return copyout(&info32, dst, sizeof(info32));
459 }
460 
461 static int
462 compat_50_netbsd32_sigtimedwait_fetch_timeout(const void *src, void *dst, size_t size)
463 {
464 	struct timespec *ts = dst;
465 	struct netbsd32_timespec50 ts32;
466 	int error;
467 
468 	error = copyin(src, &ts32, sizeof(ts32));
469 	if (error)
470 		return error;
471 
472 	netbsd32_to_timespec50(&ts32, ts);
473 	return 0;
474 }
475 
476 static int
477 compat_50_netbsd32_sigtimedwait_put_timeout(const void *src, void *dst, size_t size)
478 {
479 	const struct timespec *ts = src;
480 	struct netbsd32_timespec50 ts32;
481 
482 	netbsd32_from_timespec50(ts, &ts32);
483 
484 	return copyout(&ts32, dst, sizeof(ts32));
485 }
486 
487 int
488 compat_50_netbsd32___sigtimedwait(struct lwp *l,
489     const struct compat_50_netbsd32___sigtimedwait_args *uap, register_t *retval)
490 {
491 	/* {
492 		syscallarg(netbsd32_sigsetp_t) set;
493 		syscallarg(netbsd32_siginfop_t) info;
494 		syscallarg(netbsd32_timespec50p_t) timeout;
495 	} */
496 	struct sys_____sigtimedwait50_args ua;
497 
498 	NETBSD32TOP_UAP(set, const sigset_t);
499 	NETBSD32TOP_UAP(info, siginfo_t);
500 	NETBSD32TOP_UAP(timeout, struct timespec);
501 
502 	return sigtimedwait1(l, &ua, retval,
503 	    compat_50_netbsd32_sigtimedwait_put_info,
504 	    compat_50_netbsd32_sigtimedwait_fetch_timeout,
505 	    compat_50_netbsd32_sigtimedwait_put_timeout);
506 	return 0;
507 }
508 
509 int
510 compat_50_netbsd32_lutimes(struct lwp *l,
511     const struct compat_50_netbsd32_lutimes_args *uap, register_t *retval)
512 {
513 	/* {
514 		syscallarg(const netbsd32_charp) path;
515 		syscallarg(const netbsd32_timeval50p_t) tptr;
516 	} */
517 	int error;
518 	struct timeval tv[2], *tvp;
519 
520 	error = get_utimes32(SCARG_P32(uap, tptr), tv, &tvp);
521 	if (error != 0)
522 		return error;
523 
524 	return do_sys_utimes(l, NULL, SCARG_P32(uap, path), NOFOLLOW,
525 	    tvp, UIO_SYSSPACE);
526 }
527 
528 int
529 compat_50_netbsd32__lwp_park(struct lwp *l,
530     const struct compat_50_netbsd32__lwp_park_args *uap, register_t *retval)
531 {
532 	/* {
533 		syscallarg(const netbsd32_timespec50p) ts;
534 		syscallarg(lwpid_t) unpark;
535 		syscallarg(netbsd32_voidp) hint;
536 		syscallarg(netbsd32_voidp) unparkhint;
537 	} */
538 	struct timespec ts, *tsp;
539 	struct netbsd32_timespec50 ts32;
540 	int error;
541 
542 	if (SCARG_P32(uap, ts) == NULL)
543 		tsp = NULL;
544 	else {
545 		error = copyin(SCARG_P32(uap, ts), &ts32, sizeof ts32);
546 		if (error != 0)
547 			return error;
548 		netbsd32_to_timespec50(&ts32, &ts);
549 		tsp = &ts;
550 	}
551 
552 	if (SCARG(uap, unpark) != 0) {
553 		error = lwp_unpark(SCARG(uap, unpark),
554 		    SCARG_P32(uap, unparkhint));
555 		if (error != 0)
556 			return error;
557 	}
558 
559 	return lwp_park(tsp, SCARG_P32(uap, hint));
560 	return 0;
561 }
562 
563 static int
564 netbsd32_kevent_fetch_timeout(const void *src, void *dest, size_t length)
565 {
566 	struct netbsd32_timespec50 ts32;
567 	int error;
568 
569 	KASSERT(length == sizeof(struct timespec50));
570 
571 	error = copyin(src, &ts32, sizeof(ts32));
572 	if (error)
573 		return error;
574 	netbsd32_to_timespec50(&ts32, (struct timespec *)dest);
575 	return 0;
576 }
577 
578 static int
579 netbsd32_kevent_fetch_changes(void *private, const struct kevent *changelist,
580     struct kevent *changes, size_t index, int n)
581 {
582 	const struct netbsd32_kevent *src =
583 	    (const struct netbsd32_kevent *)changelist;
584 	struct netbsd32_kevent *kev32, *changes32 = private;
585 	int error, i;
586 
587 	error = copyin(src + index, changes32, n * sizeof(*changes32));
588 	if (error)
589 		return error;
590 	for (i = 0, kev32 = changes32; i < n; i++, kev32++, changes++)
591 		netbsd32_to_kevent(kev32, changes);
592 	return 0;
593 }
594 
595 static int
596 netbsd32_kevent_put_events(void *private, struct kevent *events,
597     struct kevent *eventlist, size_t index, int n)
598 {
599 	struct netbsd32_kevent *kev32, *events32 = private;
600 	int i;
601 
602 	for (i = 0, kev32 = events32; i < n; i++, kev32++, events++)
603 		netbsd32_from_kevent(events, kev32);
604 	kev32 = (struct netbsd32_kevent *)eventlist;
605 	return  copyout(events32, kev32, n * sizeof(*events32));
606 }
607 
608 int
609 compat_50_netbsd32_kevent(struct lwp *l,
610     const struct compat_50_netbsd32_kevent_args *uap, register_t *retval)
611 {
612 	/* {
613 		syscallarg(int) fd;
614 		syscallarg(netbsd32_keventp_t) changelist;
615 		syscallarg(netbsd32_size_t) nchanges;
616 		syscallarg(netbsd32_keventp_t) eventlist;
617 		syscallarg(netbsd32_size_t) nevents;
618 		syscallarg(netbsd32_timespec50p_t) timeout;
619 	} */
620 	int error;
621 	size_t maxalloc, nchanges, nevents;
622 	struct kevent_ops netbsd32_kevent_ops = {
623 		keo_fetch_timeout: netbsd32_kevent_fetch_timeout,
624 		keo_fetch_changes: netbsd32_kevent_fetch_changes,
625 		keo_put_events: netbsd32_kevent_put_events,
626 	};
627 
628 	nchanges = SCARG(uap, nchanges);
629 	nevents = SCARG(uap, nevents);
630 	maxalloc = MIN(KQ_NEVENTS, MAX(nchanges, nevents));
631 	netbsd32_kevent_ops.keo_private =
632 	    kmem_alloc(maxalloc * sizeof(struct netbsd32_kevent), KM_SLEEP);
633 
634 	error = kevent1(retval, SCARG(uap, fd),
635 	    NETBSD32PTR64(SCARG(uap, changelist)), nchanges,
636 	    NETBSD32PTR64(SCARG(uap, eventlist)), nevents,
637 	    NETBSD32PTR64(SCARG(uap, timeout)), &netbsd32_kevent_ops);
638 
639 	kmem_free(netbsd32_kevent_ops.keo_private,
640 	    maxalloc * sizeof(struct netbsd32_kevent));
641 	return error;
642 }
643 
644 int
645 compat_50_netbsd32_pselect(struct lwp *l,
646     const struct compat_50_netbsd32_pselect_args *uap, register_t *retval)
647 {
648 	/* {
649 		syscallarg(int) nd;
650 		syscallarg(netbsd32_fd_setp_t) in;
651 		syscallarg(netbsd32_fd_setp_t) ou;
652 		syscallarg(netbsd32_fd_setp_t) ex;
653 		syscallarg(const netbsd32_timespec50p_t) ts;
654 		syscallarg(const netbsd32_sigsetp_t) mask;
655 	} */
656 	int error;
657 	struct netbsd32_timespec50 ts32;
658 	struct timespec ats, *ts = NULL;
659 	sigset_t amask, *mask = NULL;
660 
661 	if (SCARG_P32(uap, ts)) {
662 		error = copyin(SCARG_P32(uap, ts), &ts32, sizeof(ts32));
663 		if (error != 0)
664 			return error;
665 		netbsd32_to_timespec50(&ts32, &ats);
666 		ts = &ats;
667 	}
668 	if (SCARG_P32(uap, mask)) {
669 		error = copyin(SCARG_P32(uap, mask), &amask, sizeof(amask));
670 		if (error != 0)
671 			return error;
672 		mask = &amask;
673 	}
674 
675 	return selcommon(retval, SCARG(uap, nd), SCARG_P32(uap, in),
676 	    SCARG_P32(uap, ou), SCARG_P32(uap, ex), ts, mask);
677 	return 0;
678 }
679 
680 int
681 compat_50_netbsd32_pollts(struct lwp *l,
682     const struct compat_50_netbsd32_pollts_args *uap, register_t *retval)
683 {
684 	/* {
685 		syscallarg(struct netbsd32_pollfdp_t) fds;
686 		syscallarg(u_int) nfds;
687 		syscallarg(const netbsd32_timespec50p_t) ts;
688 		syscallarg(const netbsd32_sigsetp_t) mask;
689 	} */
690 	int error;
691 	struct netbsd32_timespec50 ts32;
692 	struct timespec ats, *ts = NULL;
693 	sigset_t amask, *mask = NULL;
694 
695 	if (SCARG_P32(uap, ts)) {
696 		error = copyin(SCARG_P32(uap, ts), &ts32, sizeof(ts32));
697 		if (error != 0)
698 			return error;
699 		netbsd32_to_timespec50(&ts32, &ats);
700 		ts = &ats;
701 	}
702 	if (NETBSD32PTR64( SCARG(uap, mask))) {
703 		error = copyin(SCARG_P32(uap, mask), &amask, sizeof(amask));
704 		if (error != 0)
705 			return error;
706 		mask = &amask;
707 	}
708 
709 	return pollcommon(retval, SCARG_P32(uap, fds),
710 	    SCARG(uap, nfds), ts, mask);
711 }
712 
713 int
714 compat_50_netbsd32___stat30(struct lwp *l,
715     const struct compat_50_netbsd32___stat30_args *uap, register_t *retval)
716 {
717 	/* {
718 		syscallarg(const netbsd32_charp) path;
719 		syscallarg(netbsd32_stat50p_t) ub;
720 	} */
721 	struct netbsd32_stat50 sb32;
722 	struct stat sb;
723 	int error;
724 	const char *path;
725 
726 	path = SCARG_P32(uap, path);
727 
728 	error = do_sys_stat(path, FOLLOW, &sb);
729 	if (error)
730 		return error;
731 	netbsd32_from___stat50(&sb, &sb32);
732 	error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
733 	return error;
734 }
735 
736 int
737 compat_50_netbsd32___fstat30(struct lwp *l,
738     const struct compat_50_netbsd32___fstat30_args *uap, register_t *retval)
739 {
740 	/* {
741 		syscallarg(int) fd;
742 		syscallarg(netbsd32_stat50p_t) sb;
743 	} */
744 	struct netbsd32_stat50 sb32;
745 	struct stat ub;
746 	int error;
747 
748 	error = do_sys_fstat(SCARG(uap, fd), &ub);
749 	if (error == 0) {
750 		netbsd32_from___stat50(&ub, &sb32);
751 		error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb32));
752 	}
753 	return error;
754 }
755 
756 int
757 compat_50_netbsd32___lstat30(struct lwp *l,
758     const struct compat_50_netbsd32___lstat30_args *uap, register_t *retval)
759 {
760 	/* {
761 		syscallarg(const netbsd32_charp) path;
762 		syscallarg(netbsd32_stat50p_t) ub;
763 	} */
764 	struct netbsd32_stat50 sb32;
765 	struct stat sb;
766 	int error;
767 	const char *path;
768 
769 	path = SCARG_P32(uap, path);
770 
771 	error = do_sys_stat(path, NOFOLLOW, &sb);
772 	if (error)
773 		return error;
774 	netbsd32_from___stat50(&sb, &sb32);
775 	error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
776 	return error;
777 }
778 
779 int
780 compat_50_netbsd32___fhstat40(struct lwp *l, const struct compat_50_netbsd32___fhstat40_args *uap, register_t *retval)
781 {
782 	/* {
783 		syscallarg(const netbsd32_pointer_t) fhp;
784 		syscallarg(netbsd32_size_t) fh_size;
785 		syscallarg(netbsd32_stat50p_t) sb;
786 	} */
787 	struct stat sb;
788 	struct netbsd32_stat50 sb32;
789 	int error;
790 
791 	error = do_fhstat(l, SCARG_P32(uap, fhp), SCARG(uap, fh_size), &sb);
792 	if (error != 0) {
793 		netbsd32_from___stat50(&sb, &sb32);
794 		error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb));
795 	}
796 	return error;
797 }
798 
799 int
800 compat_50_netbsd32_wait4(struct lwp *l, const struct compat_50_netbsd32_wait4_args *uap, register_t *retval)
801 {
802 	/* {
803 		syscallarg(int) pid;
804 		syscallarg(netbsd32_intp) status;
805 		syscallarg(int) options;
806 		syscallarg(netbsd32_rusage50p_t) rusage;
807 	} */
808 	int error, status, pid = SCARG(uap, pid);
809 	struct netbsd32_rusage50 ru32;
810 	struct rusage ru;
811 
812 	error = do_sys_wait(&pid, &status, SCARG(uap, options),
813 	    SCARG_P32(uap, rusage) != NULL ? &ru : NULL);
814 
815 	retval[0] = pid;
816 	if (pid == 0)
817 		return error;
818 
819 	if (SCARG_P32(uap, rusage)) {
820 		netbsd32_from_rusage50(&ru, &ru32);
821 		error = copyout(&ru32, SCARG_P32(uap, rusage), sizeof(ru32));
822 	}
823 
824 	if (error == 0 && SCARG_P32(uap, status))
825 		error = copyout(&status, SCARG_P32(uap, status), sizeof(status));
826 
827 	return error;
828 }
829 
830 
831 int
832 compat_50_netbsd32_getrusage(struct lwp *l, const struct compat_50_netbsd32_getrusage_args *uap, register_t *retval)
833 {
834 	/* {
835 		syscallarg(int) who;
836 		syscallarg(netbsd32_rusage50p_t) rusage;
837 	} */
838 	struct proc *p = l->l_proc;
839 	struct rusage *rup;
840 	struct netbsd32_rusage50 ru;
841 
842 	switch (SCARG(uap, who)) {
843 
844 	case RUSAGE_SELF:
845 		rup = &p->p_stats->p_ru;
846 		mutex_enter(p->p_lock);
847 		calcru(p, &rup->ru_utime, &rup->ru_stime, NULL, NULL);
848 		mutex_exit(p->p_lock);
849 		break;
850 
851 	case RUSAGE_CHILDREN:
852 		rup = &p->p_stats->p_cru;
853 		break;
854 
855 	default:
856 		return (EINVAL);
857 	}
858 	netbsd32_from_rusage50(rup, &ru);
859 	return copyout(&ru, SCARG_P32(uap, rusage), sizeof(ru));
860 }
861 
862 int
863 compat_50_netbsd32_setitimer(struct lwp *l,
864     const struct compat_50_netbsd32_setitimer_args *uap, register_t *retval)
865 {
866 	/* {
867 		syscallarg(int) which;
868 		syscallarg(const netbsd32_itimerval50p_t) itv;
869 		syscallarg(netbsd32_itimerval50p_t) oitv;
870 	} */
871 	struct proc *p = l->l_proc;
872 	struct netbsd32_itimerval50 s32it, *itv32;
873 	int which = SCARG(uap, which);
874 	struct compat_50_netbsd32_getitimer_args getargs;
875 	struct itimerval aitv;
876 	int error;
877 
878 	if ((u_int)which > ITIMER_PROF)
879 		return (EINVAL);
880 	itv32 = SCARG_P32(uap, itv);
881 	if (itv32) {
882 		if ((error = copyin(itv32, &s32it, sizeof(s32it))))
883 			return (error);
884 		netbsd32_to_itimerval50(&s32it, &aitv);
885 	}
886 	if (SCARG_P32(uap, oitv) != 0) {
887 		SCARG(&getargs, which) = which;
888 		SCARG(&getargs, itv) = SCARG(uap, oitv);
889 		if ((error = compat_50_netbsd32_getitimer(l, &getargs, retval)) != 0)
890 			return (error);
891 	}
892 	if (itv32 == 0)
893 		return 0;
894 
895 	return dosetitimer(p, which, &aitv);
896 }
897 
898 int
899 compat_50_netbsd32_getitimer(struct lwp *l, const struct compat_50_netbsd32_getitimer_args *uap, register_t *retval)
900 {
901 	/* {
902 		syscallarg(int) which;
903 		syscallarg(netbsd32_itimerval50p_t) itv;
904 	} */
905 	struct proc *p = l->l_proc;
906 	struct netbsd32_itimerval50 s32it;
907 	struct itimerval aitv;
908 	int error;
909 
910 	error = dogetitimer(p, SCARG(uap, which), &aitv);
911 	if (error)
912 		return error;
913 
914 	netbsd32_from_itimerval50(&aitv, &s32it);
915 	return copyout(&s32it, SCARG_P32(uap, itv), sizeof(s32it));
916 }
917 
918 #if defined(SYSVSEM)
919 
920 int
921 compat_50_netbsd32___semctl14(struct lwp *l, const struct compat_50_netbsd32___semctl14_args *uap, register_t *retval)
922 {
923 	return do_netbsd32___semctl14(l, uap, retval, NULL);
924 }
925 
926 int
927 do_netbsd32___semctl14(struct lwp *l, const struct compat_50_netbsd32___semctl14_args *uap, register_t *retval, void *vkarg)
928 {
929 	/* {
930 		syscallarg(int) semid;
931 		syscallarg(int) semnum;
932 		syscallarg(int) cmd;
933 		syscallarg(netbsd32_semun50p_t) arg;
934 	} */
935 	struct semid_ds sembuf;
936 	struct netbsd32_semid_ds50 sembuf32;
937 	int cmd, error;
938 	void *pass_arg;
939 	union __semun karg;
940 	union netbsd32_semun50 karg32;
941 
942 	cmd = SCARG(uap, cmd);
943 
944 	switch (cmd) {
945 	case IPC_SET:
946 	case IPC_STAT:
947 		pass_arg = &sembuf;
948 		break;
949 
950 	case GETALL:
951 	case SETVAL:
952 	case SETALL:
953 		pass_arg = &karg;
954 		break;
955 	default:
956 		pass_arg = NULL;
957 		break;
958 	}
959 
960 	if (pass_arg) {
961 		if (vkarg != NULL)
962 			karg32 = *(union netbsd32_semun50 *)vkarg;
963 		else {
964 			error = copyin(SCARG_P32(uap, arg), &karg32,
965 					sizeof(karg32));
966 			if (error)
967 				return error;
968 		}
969 		if (pass_arg == &karg) {
970 			switch (cmd) {
971 			case GETALL:
972 			case SETALL:
973 				karg.array = NETBSD32PTR64(karg32.array);
974 				break;
975 			case SETVAL:
976 				karg.val = karg32.val;
977 				break;
978 			}
979 		}
980 		if (cmd == IPC_SET) {
981 			error = copyin(NETBSD32PTR64(karg32.buf), &sembuf32,
982 			    sizeof(sembuf32));
983 			if (error)
984 				return (error);
985 			netbsd32_to_semid_ds50(&sembuf32, &sembuf);
986 		}
987 	}
988 
989 	error = semctl1(l, SCARG(uap, semid), SCARG(uap, semnum), cmd,
990 	    pass_arg, retval);
991 
992 	if (error == 0 && cmd == IPC_STAT) {
993 		netbsd32_from_semid_ds50(&sembuf, &sembuf32);
994 		error = copyout(&sembuf32, NETBSD32PTR64(karg32.buf),
995 		    sizeof(sembuf32));
996 	}
997 
998 	return (error);
999 }
1000 #endif
1001 
1002 #if defined(SYSVMSG)
1003 
1004 int
1005 compat_50_netbsd32___msgctl13(struct lwp *l, const struct compat_50_netbsd32___msgctl13_args *uap, register_t *retval)
1006 {
1007 	/* {
1008 		syscallarg(int) msqid;
1009 		syscallarg(int) cmd;
1010 		syscallarg(netbsd32_msqid_ds50p_t) buf;
1011 	} */
1012 	struct msqid_ds ds;
1013 	struct netbsd32_msqid_ds50 ds32;
1014 	int error, cmd;
1015 
1016 	cmd = SCARG(uap, cmd);
1017 	if (cmd == IPC_SET) {
1018 		error = copyin(SCARG_P32(uap, buf), &ds32, sizeof(ds32));
1019 		if (error)
1020 			return error;
1021 		netbsd32_to_msqid_ds50(&ds32, &ds);
1022 	}
1023 
1024 	error = msgctl1(l, SCARG(uap, msqid), cmd,
1025 	    (cmd == IPC_SET || cmd == IPC_STAT) ? &ds : NULL);
1026 
1027 	if (error == 0 && cmd == IPC_STAT) {
1028 		netbsd32_from_msqid_ds50(&ds, &ds32);
1029 		error = copyout(&ds32, SCARG_P32(uap, buf), sizeof(ds32));
1030 	}
1031 
1032 	return error;
1033 }
1034 #endif
1035 
1036 #if defined(SYSVSHM)
1037 
1038 int
1039 compat_50_netbsd32___shmctl13(struct lwp *l, const struct compat_50_netbsd32___shmctl13_args *uap, register_t *retval)
1040 {
1041 	/* {
1042 		syscallarg(int) shmid;
1043 		syscallarg(int) cmd;
1044 		syscallarg(netbsd32_shmid_ds50p_t) buf;
1045 	} */
1046 	struct shmid_ds ds;
1047 	struct netbsd32_shmid_ds50 ds32;
1048 	int error, cmd;
1049 
1050 	cmd = SCARG(uap, cmd);
1051 	if (cmd == IPC_SET) {
1052 		error = copyin(SCARG_P32(uap, buf), &ds32, sizeof(ds32));
1053 		if (error)
1054 			return error;
1055 		netbsd32_to_shmid_ds50(&ds32, &ds);
1056 	}
1057 
1058 	error = shmctl1(l, SCARG(uap, shmid), cmd,
1059 	    (cmd == IPC_SET || cmd == IPC_STAT) ? &ds : NULL);
1060 
1061 	if (error == 0 && cmd == IPC_STAT) {
1062 		netbsd32_from_shmid_ds50(&ds, &ds32);
1063 		error = copyout(&ds32, SCARG_P32(uap, buf), sizeof(ds32));
1064 	}
1065 
1066 	return error;
1067 }
1068 #endif
1069