xref: /freebsd-11-stable/sys/compat/freebsd32/freebsd32_misc.c (revision d3aef2d8fc89def105f84dca7f35a901d861539c)
1 /*-
2  * Copyright (c) 2002 Doug Rabson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include "opt_compat.h"
31 #include "opt_inet.h"
32 #include "opt_inet6.h"
33 #include "opt_ktrace.h"
34 
35 #define __ELF_WORD_SIZE 32
36 
37 #include <sys/param.h>
38 #include <sys/bus.h>
39 #include <sys/capsicum.h>
40 #include <sys/clock.h>
41 #include <sys/exec.h>
42 #include <sys/fcntl.h>
43 #include <sys/filedesc.h>
44 #include <sys/imgact.h>
45 #include <sys/jail.h>
46 #include <sys/kernel.h>
47 #include <sys/limits.h>
48 #include <sys/linker.h>
49 #include <sys/lock.h>
50 #include <sys/malloc.h>
51 #include <sys/file.h>		/* Must come after sys/malloc.h */
52 #include <sys/imgact.h>
53 #include <sys/mbuf.h>
54 #include <sys/mman.h>
55 #include <sys/module.h>
56 #include <sys/mount.h>
57 #include <sys/mutex.h>
58 #include <sys/namei.h>
59 #include <sys/proc.h>
60 #include <sys/procctl.h>
61 #include <sys/reboot.h>
62 #include <sys/resource.h>
63 #include <sys/resourcevar.h>
64 #include <sys/selinfo.h>
65 #include <sys/eventvar.h>	/* Must come after sys/selinfo.h */
66 #include <sys/pipe.h>		/* Must come after sys/selinfo.h */
67 #include <sys/signal.h>
68 #include <sys/signalvar.h>
69 #include <sys/socket.h>
70 #include <sys/socketvar.h>
71 #include <sys/stat.h>
72 #include <sys/syscall.h>
73 #include <sys/syscallsubr.h>
74 #include <sys/sysctl.h>
75 #include <sys/sysent.h>
76 #include <sys/sysproto.h>
77 #include <sys/systm.h>
78 #include <sys/thr.h>
79 #include <sys/unistd.h>
80 #include <sys/ucontext.h>
81 #include <sys/vnode.h>
82 #include <sys/wait.h>
83 #include <sys/ipc.h>
84 #include <sys/msg.h>
85 #include <sys/sem.h>
86 #include <sys/shm.h>
87 #ifdef KTRACE
88 #include <sys/ktrace.h>
89 #endif
90 
91 #ifdef INET
92 #include <netinet/in.h>
93 #endif
94 
95 #include <vm/vm.h>
96 #include <vm/vm_param.h>
97 #include <vm/pmap.h>
98 #include <vm/vm_map.h>
99 #include <vm/vm_object.h>
100 #include <vm/vm_extern.h>
101 
102 #include <machine/cpu.h>
103 #include <machine/elf.h>
104 
105 #include <security/audit/audit.h>
106 
107 #include <compat/freebsd32/freebsd32_util.h>
108 #include <compat/freebsd32/freebsd32.h>
109 #include <compat/freebsd32/freebsd32_ipc.h>
110 #include <compat/freebsd32/freebsd32_misc.h>
111 #include <compat/freebsd32/freebsd32_signal.h>
112 #include <compat/freebsd32/freebsd32_proto.h>
113 
114 FEATURE(compat_freebsd_32bit, "Compatible with 32-bit FreeBSD");
115 
116 #ifndef __mips__
117 CTASSERT(sizeof(struct timeval32) == 8);
118 CTASSERT(sizeof(struct timespec32) == 8);
119 CTASSERT(sizeof(struct itimerval32) == 16);
120 CTASSERT(sizeof(struct bintime32) == 12);
121 #endif
122 CTASSERT(sizeof(struct statfs32) == 256);
123 #ifndef __mips__
124 CTASSERT(sizeof(struct rusage32) == 72);
125 #endif
126 CTASSERT(sizeof(struct sigaltstack32) == 12);
127 CTASSERT(sizeof(struct kevent32) == 20);
128 CTASSERT(sizeof(struct iovec32) == 8);
129 CTASSERT(sizeof(struct msghdr32) == 28);
130 #ifndef __mips__
131 CTASSERT(sizeof(struct stat32) == 96);
132 #endif
133 CTASSERT(sizeof(struct sigaction32) == 24);
134 
135 static int freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count);
136 static int freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count);
137 static int freebsd32_user_clock_nanosleep(struct thread *td, clockid_t clock_id,
138     int flags, const struct timespec32 *ua_rqtp, struct timespec32 *ua_rmtp);
139 
140 void
freebsd32_rusage_out(const struct rusage * s,struct rusage32 * s32)141 freebsd32_rusage_out(const struct rusage *s, struct rusage32 *s32)
142 {
143 
144 	TV_CP(*s, *s32, ru_utime);
145 	TV_CP(*s, *s32, ru_stime);
146 	CP(*s, *s32, ru_maxrss);
147 	CP(*s, *s32, ru_ixrss);
148 	CP(*s, *s32, ru_idrss);
149 	CP(*s, *s32, ru_isrss);
150 	CP(*s, *s32, ru_minflt);
151 	CP(*s, *s32, ru_majflt);
152 	CP(*s, *s32, ru_nswap);
153 	CP(*s, *s32, ru_inblock);
154 	CP(*s, *s32, ru_oublock);
155 	CP(*s, *s32, ru_msgsnd);
156 	CP(*s, *s32, ru_msgrcv);
157 	CP(*s, *s32, ru_nsignals);
158 	CP(*s, *s32, ru_nvcsw);
159 	CP(*s, *s32, ru_nivcsw);
160 }
161 
162 int
freebsd32_wait4(struct thread * td,struct freebsd32_wait4_args * uap)163 freebsd32_wait4(struct thread *td, struct freebsd32_wait4_args *uap)
164 {
165 	int error, status;
166 	struct rusage32 ru32;
167 	struct rusage ru, *rup;
168 
169 	if (uap->rusage != NULL)
170 		rup = &ru;
171 	else
172 		rup = NULL;
173 	error = kern_wait(td, uap->pid, &status, uap->options, rup);
174 	if (error)
175 		return (error);
176 	if (uap->status != NULL)
177 		error = copyout(&status, uap->status, sizeof(status));
178 	if (uap->rusage != NULL && error == 0) {
179 		freebsd32_rusage_out(&ru, &ru32);
180 		error = copyout(&ru32, uap->rusage, sizeof(ru32));
181 	}
182 	return (error);
183 }
184 
185 int
freebsd32_wait6(struct thread * td,struct freebsd32_wait6_args * uap)186 freebsd32_wait6(struct thread *td, struct freebsd32_wait6_args *uap)
187 {
188 	struct wrusage32 wru32;
189 	struct __wrusage wru, *wrup;
190 	struct siginfo32 si32;
191 	struct __siginfo si, *sip;
192 	int error, status;
193 
194 	if (uap->wrusage != NULL)
195 		wrup = &wru;
196 	else
197 		wrup = NULL;
198 	if (uap->info != NULL) {
199 		sip = &si;
200 		bzero(sip, sizeof(*sip));
201 	} else
202 		sip = NULL;
203 	error = kern_wait6(td, uap->idtype, PAIR32TO64(id_t, uap->id),
204 	    &status, uap->options, wrup, sip);
205 	if (error != 0)
206 		return (error);
207 	if (uap->status != NULL)
208 		error = copyout(&status, uap->status, sizeof(status));
209 	if (uap->wrusage != NULL && error == 0) {
210 		freebsd32_rusage_out(&wru.wru_self, &wru32.wru_self);
211 		freebsd32_rusage_out(&wru.wru_children, &wru32.wru_children);
212 		error = copyout(&wru32, uap->wrusage, sizeof(wru32));
213 	}
214 	if (uap->info != NULL && error == 0) {
215 		siginfo_to_siginfo32 (&si, &si32);
216 		error = copyout(&si32, uap->info, sizeof(si32));
217 	}
218 	return (error);
219 }
220 
221 #ifdef COMPAT_FREEBSD4
222 static void
copy_statfs(struct statfs * in,struct statfs32 * out)223 copy_statfs(struct statfs *in, struct statfs32 *out)
224 {
225 
226 	statfs_scale_blocks(in, INT32_MAX);
227 	bzero(out, sizeof(*out));
228 	CP(*in, *out, f_bsize);
229 	out->f_iosize = MIN(in->f_iosize, INT32_MAX);
230 	CP(*in, *out, f_blocks);
231 	CP(*in, *out, f_bfree);
232 	CP(*in, *out, f_bavail);
233 	out->f_files = MIN(in->f_files, INT32_MAX);
234 	out->f_ffree = MIN(in->f_ffree, INT32_MAX);
235 	CP(*in, *out, f_fsid);
236 	CP(*in, *out, f_owner);
237 	CP(*in, *out, f_type);
238 	CP(*in, *out, f_flags);
239 	out->f_syncwrites = MIN(in->f_syncwrites, INT32_MAX);
240 	out->f_asyncwrites = MIN(in->f_asyncwrites, INT32_MAX);
241 	strlcpy(out->f_fstypename,
242 	      in->f_fstypename, MFSNAMELEN);
243 	strlcpy(out->f_mntonname,
244 	      in->f_mntonname, min(MNAMELEN, FREEBSD4_MNAMELEN));
245 	out->f_syncreads = MIN(in->f_syncreads, INT32_MAX);
246 	out->f_asyncreads = MIN(in->f_asyncreads, INT32_MAX);
247 	strlcpy(out->f_mntfromname,
248 	      in->f_mntfromname, min(MNAMELEN, FREEBSD4_MNAMELEN));
249 }
250 #endif
251 
252 #ifdef COMPAT_FREEBSD4
253 int
freebsd4_freebsd32_getfsstat(struct thread * td,struct freebsd4_freebsd32_getfsstat_args * uap)254 freebsd4_freebsd32_getfsstat(struct thread *td,
255     struct freebsd4_freebsd32_getfsstat_args *uap)
256 {
257 	struct statfs *buf, *sp;
258 	struct statfs32 stat32;
259 	size_t count, size, copycount;
260 	int error;
261 
262 	count = uap->bufsize / sizeof(struct statfs32);
263 	size = count * sizeof(struct statfs);
264 	error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE, uap->mode);
265 	if (size > 0) {
266 		sp = buf;
267 		copycount = count;
268 		while (copycount > 0 && error == 0) {
269 			copy_statfs(sp, &stat32);
270 			error = copyout(&stat32, uap->buf, sizeof(stat32));
271 			sp++;
272 			uap->buf++;
273 			copycount--;
274 		}
275 		free(buf, M_STATFS);
276 	}
277 	if (error == 0)
278 		td->td_retval[0] = count;
279 	return (error);
280 }
281 #endif
282 
283 #ifdef COMPAT_FREEBSD10
284 int
freebsd10_freebsd32_pipe(struct thread * td,struct freebsd10_freebsd32_pipe_args * uap)285 freebsd10_freebsd32_pipe(struct thread *td,
286     struct freebsd10_freebsd32_pipe_args *uap) {
287 
288 	return (freebsd10_pipe(td, (struct freebsd10_pipe_args*)uap));
289 }
290 #endif
291 
292 int
freebsd32_sigaltstack(struct thread * td,struct freebsd32_sigaltstack_args * uap)293 freebsd32_sigaltstack(struct thread *td,
294 		      struct freebsd32_sigaltstack_args *uap)
295 {
296 	struct sigaltstack32 s32;
297 	struct sigaltstack ss, oss, *ssp;
298 	int error;
299 
300 	if (uap->ss != NULL) {
301 		error = copyin(uap->ss, &s32, sizeof(s32));
302 		if (error)
303 			return (error);
304 		PTRIN_CP(s32, ss, ss_sp);
305 		CP(s32, ss, ss_size);
306 		CP(s32, ss, ss_flags);
307 		ssp = &ss;
308 	} else
309 		ssp = NULL;
310 	error = kern_sigaltstack(td, ssp, &oss);
311 	if (error == 0 && uap->oss != NULL) {
312 		PTROUT_CP(oss, s32, ss_sp);
313 		CP(oss, s32, ss_size);
314 		CP(oss, s32, ss_flags);
315 		error = copyout(&s32, uap->oss, sizeof(s32));
316 	}
317 	return (error);
318 }
319 
320 /*
321  * Custom version of exec_copyin_args() so that we can translate
322  * the pointers.
323  */
324 int
freebsd32_exec_copyin_args(struct image_args * args,char * fname,enum uio_seg segflg,u_int32_t * argv,u_int32_t * envv)325 freebsd32_exec_copyin_args(struct image_args *args, char *fname,
326     enum uio_seg segflg, u_int32_t *argv, u_int32_t *envv)
327 {
328 	char *argp, *envp;
329 	u_int32_t *p32, arg;
330 	size_t length;
331 	int error;
332 
333 	bzero(args, sizeof(*args));
334 	if (argv == NULL)
335 		return (EFAULT);
336 
337 	/*
338 	 * Allocate demand-paged memory for the file name, argument, and
339 	 * environment strings.
340 	 */
341 	error = exec_alloc_args(args);
342 	if (error != 0)
343 		return (error);
344 
345 	/*
346 	 * Copy the file name.
347 	 */
348 	if (fname != NULL) {
349 		args->fname = args->buf;
350 		error = (segflg == UIO_SYSSPACE) ?
351 		    copystr(fname, args->fname, PATH_MAX, &length) :
352 		    copyinstr(fname, args->fname, PATH_MAX, &length);
353 		if (error != 0)
354 			goto err_exit;
355 	} else
356 		length = 0;
357 
358 	args->begin_argv = args->buf + length;
359 	args->endp = args->begin_argv;
360 	args->stringspace = ARG_MAX;
361 
362 	/*
363 	 * extract arguments first
364 	 */
365 	p32 = argv;
366 	for (;;) {
367 		error = copyin(p32++, &arg, sizeof(arg));
368 		if (error)
369 			goto err_exit;
370 		if (arg == 0)
371 			break;
372 		argp = PTRIN(arg);
373 		error = copyinstr(argp, args->endp, args->stringspace, &length);
374 		if (error) {
375 			if (error == ENAMETOOLONG)
376 				error = E2BIG;
377 			goto err_exit;
378 		}
379 		args->stringspace -= length;
380 		args->endp += length;
381 		args->argc++;
382 	}
383 
384 	args->begin_envv = args->endp;
385 
386 	/*
387 	 * extract environment strings
388 	 */
389 	if (envv) {
390 		p32 = envv;
391 		for (;;) {
392 			error = copyin(p32++, &arg, sizeof(arg));
393 			if (error)
394 				goto err_exit;
395 			if (arg == 0)
396 				break;
397 			envp = PTRIN(arg);
398 			error = copyinstr(envp, args->endp, args->stringspace,
399 			    &length);
400 			if (error) {
401 				if (error == ENAMETOOLONG)
402 					error = E2BIG;
403 				goto err_exit;
404 			}
405 			args->stringspace -= length;
406 			args->endp += length;
407 			args->envc++;
408 		}
409 	}
410 
411 	return (0);
412 
413 err_exit:
414 	exec_free_args(args);
415 	return (error);
416 }
417 
418 int
freebsd32_execve(struct thread * td,struct freebsd32_execve_args * uap)419 freebsd32_execve(struct thread *td, struct freebsd32_execve_args *uap)
420 {
421 	struct image_args eargs;
422 	struct vmspace *oldvmspace;
423 	int error;
424 
425 	error = pre_execve(td, &oldvmspace);
426 	if (error != 0)
427 		return (error);
428 	error = freebsd32_exec_copyin_args(&eargs, uap->fname, UIO_USERSPACE,
429 	    uap->argv, uap->envv);
430 	if (error == 0)
431 		error = kern_execve(td, &eargs, NULL, oldvmspace);
432 	post_execve(td, error, oldvmspace);
433 	return (error);
434 }
435 
436 int
freebsd32_fexecve(struct thread * td,struct freebsd32_fexecve_args * uap)437 freebsd32_fexecve(struct thread *td, struct freebsd32_fexecve_args *uap)
438 {
439 	struct image_args eargs;
440 	struct vmspace *oldvmspace;
441 	int error;
442 
443 	error = pre_execve(td, &oldvmspace);
444 	if (error != 0)
445 		return (error);
446 	error = freebsd32_exec_copyin_args(&eargs, NULL, UIO_SYSSPACE,
447 	    uap->argv, uap->envv);
448 	if (error == 0) {
449 		eargs.fd = uap->fd;
450 		error = kern_execve(td, &eargs, NULL, oldvmspace);
451 	}
452 	post_execve(td, error, oldvmspace);
453 	return (error);
454 }
455 
456 int
freebsd32_mprotect(struct thread * td,struct freebsd32_mprotect_args * uap)457 freebsd32_mprotect(struct thread *td, struct freebsd32_mprotect_args *uap)
458 {
459 	int prot;
460 
461 	prot = uap->prot;
462 #if defined(__amd64__)
463 	if (i386_read_exec && (prot & PROT_READ) != 0)
464 		prot |= PROT_EXEC;
465 #endif
466 	return (kern_mprotect(td, (uintptr_t)PTRIN(uap->addr), uap->len,
467 	    prot));
468 }
469 
470 int
freebsd32_mmap(struct thread * td,struct freebsd32_mmap_args * uap)471 freebsd32_mmap(struct thread *td, struct freebsd32_mmap_args *uap)
472 {
473 	int prot;
474 
475 	prot = uap->prot;
476 #if defined(__amd64__)
477 	if (i386_read_exec && (prot & PROT_READ))
478 		prot |= PROT_EXEC;
479 #endif
480 
481 	return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, prot,
482 	    uap->flags, uap->fd, PAIR32TO64(off_t, uap->pos)));
483 }
484 
485 #ifdef COMPAT_FREEBSD6
486 int
freebsd6_freebsd32_mmap(struct thread * td,struct freebsd6_freebsd32_mmap_args * uap)487 freebsd6_freebsd32_mmap(struct thread *td,
488     struct freebsd6_freebsd32_mmap_args *uap)
489 {
490 	int prot;
491 
492 	prot = uap->prot;
493 #if defined(__amd64__)
494 	if (i386_read_exec && (prot & PROT_READ))
495 		prot |= PROT_EXEC;
496 #endif
497 
498 	return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, prot,
499 	    uap->flags, uap->fd, PAIR32TO64(off_t, uap->pos)));
500 }
501 #endif
502 
503 int
freebsd32_setitimer(struct thread * td,struct freebsd32_setitimer_args * uap)504 freebsd32_setitimer(struct thread *td, struct freebsd32_setitimer_args *uap)
505 {
506 	struct itimerval itv, oitv, *itvp;
507 	struct itimerval32 i32;
508 	int error;
509 
510 	if (uap->itv != NULL) {
511 		error = copyin(uap->itv, &i32, sizeof(i32));
512 		if (error)
513 			return (error);
514 		TV_CP(i32, itv, it_interval);
515 		TV_CP(i32, itv, it_value);
516 		itvp = &itv;
517 	} else
518 		itvp = NULL;
519 	error = kern_setitimer(td, uap->which, itvp, &oitv);
520 	if (error || uap->oitv == NULL)
521 		return (error);
522 	TV_CP(oitv, i32, it_interval);
523 	TV_CP(oitv, i32, it_value);
524 	return (copyout(&i32, uap->oitv, sizeof(i32)));
525 }
526 
527 int
freebsd32_getitimer(struct thread * td,struct freebsd32_getitimer_args * uap)528 freebsd32_getitimer(struct thread *td, struct freebsd32_getitimer_args *uap)
529 {
530 	struct itimerval itv;
531 	struct itimerval32 i32;
532 	int error;
533 
534 	error = kern_getitimer(td, uap->which, &itv);
535 	if (error || uap->itv == NULL)
536 		return (error);
537 	TV_CP(itv, i32, it_interval);
538 	TV_CP(itv, i32, it_value);
539 	return (copyout(&i32, uap->itv, sizeof(i32)));
540 }
541 
542 int
freebsd32_select(struct thread * td,struct freebsd32_select_args * uap)543 freebsd32_select(struct thread *td, struct freebsd32_select_args *uap)
544 {
545 	struct timeval32 tv32;
546 	struct timeval tv, *tvp;
547 	int error;
548 
549 	if (uap->tv != NULL) {
550 		error = copyin(uap->tv, &tv32, sizeof(tv32));
551 		if (error)
552 			return (error);
553 		CP(tv32, tv, tv_sec);
554 		CP(tv32, tv, tv_usec);
555 		tvp = &tv;
556 	} else
557 		tvp = NULL;
558 	/*
559 	 * XXX Do pointers need PTRIN()?
560 	 */
561 	return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
562 	    sizeof(int32_t) * 8));
563 }
564 
565 int
freebsd32_pselect(struct thread * td,struct freebsd32_pselect_args * uap)566 freebsd32_pselect(struct thread *td, struct freebsd32_pselect_args *uap)
567 {
568 	struct timespec32 ts32;
569 	struct timespec ts;
570 	struct timeval tv, *tvp;
571 	sigset_t set, *uset;
572 	int error;
573 
574 	if (uap->ts != NULL) {
575 		error = copyin(uap->ts, &ts32, sizeof(ts32));
576 		if (error != 0)
577 			return (error);
578 		CP(ts32, ts, tv_sec);
579 		CP(ts32, ts, tv_nsec);
580 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
581 		tvp = &tv;
582 	} else
583 		tvp = NULL;
584 	if (uap->sm != NULL) {
585 		error = copyin(uap->sm, &set, sizeof(set));
586 		if (error != 0)
587 			return (error);
588 		uset = &set;
589 	} else
590 		uset = NULL;
591 	/*
592 	 * XXX Do pointers need PTRIN()?
593 	 */
594 	error = kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
595 	    uset, sizeof(int32_t) * 8);
596 	return (error);
597 }
598 
599 /*
600  * Copy 'count' items into the destination list pointed to by uap->eventlist.
601  */
602 static int
freebsd32_kevent_copyout(void * arg,struct kevent * kevp,int count)603 freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count)
604 {
605 	struct freebsd32_kevent_args *uap;
606 	struct kevent32	ks32[KQ_NEVENTS];
607 	int i, error = 0;
608 
609 	KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
610 	uap = (struct freebsd32_kevent_args *)arg;
611 
612 	for (i = 0; i < count; i++) {
613 		CP(kevp[i], ks32[i], ident);
614 		CP(kevp[i], ks32[i], filter);
615 		CP(kevp[i], ks32[i], flags);
616 		CP(kevp[i], ks32[i], fflags);
617 		CP(kevp[i], ks32[i], data);
618 		PTROUT_CP(kevp[i], ks32[i], udata);
619 	}
620 	error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
621 	if (error == 0)
622 		uap->eventlist += count;
623 	return (error);
624 }
625 
626 /*
627  * Copy 'count' items from the list pointed to by uap->changelist.
628  */
629 static int
freebsd32_kevent_copyin(void * arg,struct kevent * kevp,int count)630 freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count)
631 {
632 	struct freebsd32_kevent_args *uap;
633 	struct kevent32	ks32[KQ_NEVENTS];
634 	int i, error = 0;
635 
636 	KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
637 	uap = (struct freebsd32_kevent_args *)arg;
638 
639 	error = copyin(uap->changelist, ks32, count * sizeof *ks32);
640 	if (error)
641 		goto done;
642 	uap->changelist += count;
643 
644 	for (i = 0; i < count; i++) {
645 		CP(ks32[i], kevp[i], ident);
646 		CP(ks32[i], kevp[i], filter);
647 		CP(ks32[i], kevp[i], flags);
648 		CP(ks32[i], kevp[i], fflags);
649 		CP(ks32[i], kevp[i], data);
650 		PTRIN_CP(ks32[i], kevp[i], udata);
651 	}
652 done:
653 	return (error);
654 }
655 
656 int
freebsd32_kevent(struct thread * td,struct freebsd32_kevent_args * uap)657 freebsd32_kevent(struct thread *td, struct freebsd32_kevent_args *uap)
658 {
659 	struct timespec32 ts32;
660 	struct timespec ts, *tsp;
661 	struct kevent_copyops k_ops = {
662 		.arg = uap,
663 		.k_copyout = freebsd32_kevent_copyout,
664 		.k_copyin = freebsd32_kevent_copyin,
665 	};
666 #ifdef KTRACE
667 	struct kevent32 *eventlist = uap->eventlist;
668 #endif
669 	int error;
670 
671 
672 	if (uap->timeout) {
673 		error = copyin(uap->timeout, &ts32, sizeof(ts32));
674 		if (error)
675 			return (error);
676 		CP(ts32, ts, tv_sec);
677 		CP(ts32, ts, tv_nsec);
678 		tsp = &ts;
679 	} else
680 		tsp = NULL;
681 #ifdef KTRACE
682 	if (KTRPOINT(td, KTR_STRUCT_ARRAY))
683 		ktrstructarray("kevent32", UIO_USERSPACE, uap->changelist,
684 		    uap->nchanges, sizeof(struct kevent32));
685 #endif
686 	error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
687 	    &k_ops, tsp);
688 #ifdef KTRACE
689 	if (error == 0 && KTRPOINT(td, KTR_STRUCT_ARRAY))
690 		ktrstructarray("kevent32", UIO_USERSPACE, eventlist,
691 		    td->td_retval[0], sizeof(struct kevent32));
692 #endif
693 	return (error);
694 }
695 
696 int
freebsd32_gettimeofday(struct thread * td,struct freebsd32_gettimeofday_args * uap)697 freebsd32_gettimeofday(struct thread *td,
698 		       struct freebsd32_gettimeofday_args *uap)
699 {
700 	struct timeval atv;
701 	struct timeval32 atv32;
702 	struct timezone rtz;
703 	int error = 0;
704 
705 	if (uap->tp) {
706 		microtime(&atv);
707 		CP(atv, atv32, tv_sec);
708 		CP(atv, atv32, tv_usec);
709 		error = copyout(&atv32, uap->tp, sizeof (atv32));
710 	}
711 	if (error == 0 && uap->tzp != NULL) {
712 		rtz.tz_minuteswest = tz_minuteswest;
713 		rtz.tz_dsttime = tz_dsttime;
714 		error = copyout(&rtz, uap->tzp, sizeof (rtz));
715 	}
716 	return (error);
717 }
718 
719 int
freebsd32_getrusage(struct thread * td,struct freebsd32_getrusage_args * uap)720 freebsd32_getrusage(struct thread *td, struct freebsd32_getrusage_args *uap)
721 {
722 	struct rusage32 s32;
723 	struct rusage s;
724 	int error;
725 
726 	error = kern_getrusage(td, uap->who, &s);
727 	if (error == 0) {
728 		freebsd32_rusage_out(&s, &s32);
729 		error = copyout(&s32, uap->rusage, sizeof(s32));
730 	}
731 	return (error);
732 }
733 
734 static int
freebsd32_copyinuio(struct iovec32 * iovp,u_int iovcnt,struct uio ** uiop)735 freebsd32_copyinuio(struct iovec32 *iovp, u_int iovcnt, struct uio **uiop)
736 {
737 	struct iovec32 iov32;
738 	struct iovec *iov;
739 	struct uio *uio;
740 	u_int iovlen;
741 	int error, i;
742 
743 	*uiop = NULL;
744 	if (iovcnt > UIO_MAXIOV)
745 		return (EINVAL);
746 	iovlen = iovcnt * sizeof(struct iovec);
747 	uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
748 	iov = (struct iovec *)(uio + 1);
749 	for (i = 0; i < iovcnt; i++) {
750 		error = copyin(&iovp[i], &iov32, sizeof(struct iovec32));
751 		if (error) {
752 			free(uio, M_IOV);
753 			return (error);
754 		}
755 		iov[i].iov_base = PTRIN(iov32.iov_base);
756 		iov[i].iov_len = iov32.iov_len;
757 	}
758 	uio->uio_iov = iov;
759 	uio->uio_iovcnt = iovcnt;
760 	uio->uio_segflg = UIO_USERSPACE;
761 	uio->uio_offset = -1;
762 	uio->uio_resid = 0;
763 	for (i = 0; i < iovcnt; i++) {
764 		if (iov->iov_len > INT_MAX - uio->uio_resid) {
765 			free(uio, M_IOV);
766 			return (EINVAL);
767 		}
768 		uio->uio_resid += iov->iov_len;
769 		iov++;
770 	}
771 	*uiop = uio;
772 	return (0);
773 }
774 
775 int
freebsd32_readv(struct thread * td,struct freebsd32_readv_args * uap)776 freebsd32_readv(struct thread *td, struct freebsd32_readv_args *uap)
777 {
778 	struct uio *auio;
779 	int error;
780 
781 	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
782 	if (error)
783 		return (error);
784 	error = kern_readv(td, uap->fd, auio);
785 	free(auio, M_IOV);
786 	return (error);
787 }
788 
789 int
freebsd32_writev(struct thread * td,struct freebsd32_writev_args * uap)790 freebsd32_writev(struct thread *td, struct freebsd32_writev_args *uap)
791 {
792 	struct uio *auio;
793 	int error;
794 
795 	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
796 	if (error)
797 		return (error);
798 	error = kern_writev(td, uap->fd, auio);
799 	free(auio, M_IOV);
800 	return (error);
801 }
802 
803 int
freebsd32_preadv(struct thread * td,struct freebsd32_preadv_args * uap)804 freebsd32_preadv(struct thread *td, struct freebsd32_preadv_args *uap)
805 {
806 	struct uio *auio;
807 	int error;
808 
809 	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
810 	if (error)
811 		return (error);
812 	error = kern_preadv(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
813 	free(auio, M_IOV);
814 	return (error);
815 }
816 
817 int
freebsd32_pwritev(struct thread * td,struct freebsd32_pwritev_args * uap)818 freebsd32_pwritev(struct thread *td, struct freebsd32_pwritev_args *uap)
819 {
820 	struct uio *auio;
821 	int error;
822 
823 	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
824 	if (error)
825 		return (error);
826 	error = kern_pwritev(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
827 	free(auio, M_IOV);
828 	return (error);
829 }
830 
831 int
freebsd32_copyiniov(struct iovec32 * iovp32,u_int iovcnt,struct iovec ** iovp,int error)832 freebsd32_copyiniov(struct iovec32 *iovp32, u_int iovcnt, struct iovec **iovp,
833     int error)
834 {
835 	struct iovec32 iov32;
836 	struct iovec *iov;
837 	u_int iovlen;
838 	int i;
839 
840 	*iovp = NULL;
841 	if (iovcnt > UIO_MAXIOV)
842 		return (error);
843 	iovlen = iovcnt * sizeof(struct iovec);
844 	iov = malloc(iovlen, M_IOV, M_WAITOK);
845 	for (i = 0; i < iovcnt; i++) {
846 		error = copyin(&iovp32[i], &iov32, sizeof(struct iovec32));
847 		if (error) {
848 			free(iov, M_IOV);
849 			return (error);
850 		}
851 		iov[i].iov_base = PTRIN(iov32.iov_base);
852 		iov[i].iov_len = iov32.iov_len;
853 	}
854 	*iovp = iov;
855 	return (0);
856 }
857 
858 static int
freebsd32_copyinmsghdr(struct msghdr32 * msg32,struct msghdr * msg)859 freebsd32_copyinmsghdr(struct msghdr32 *msg32, struct msghdr *msg)
860 {
861 	struct msghdr32 m32;
862 	int error;
863 
864 	error = copyin(msg32, &m32, sizeof(m32));
865 	if (error)
866 		return (error);
867 	msg->msg_name = PTRIN(m32.msg_name);
868 	msg->msg_namelen = m32.msg_namelen;
869 	msg->msg_iov = PTRIN(m32.msg_iov);
870 	msg->msg_iovlen = m32.msg_iovlen;
871 	msg->msg_control = PTRIN(m32.msg_control);
872 	msg->msg_controllen = m32.msg_controllen;
873 	msg->msg_flags = m32.msg_flags;
874 	return (0);
875 }
876 
877 static int
freebsd32_copyoutmsghdr(struct msghdr * msg,struct msghdr32 * msg32)878 freebsd32_copyoutmsghdr(struct msghdr *msg, struct msghdr32 *msg32)
879 {
880 	struct msghdr32 m32;
881 	int error;
882 
883 	m32.msg_name = PTROUT(msg->msg_name);
884 	m32.msg_namelen = msg->msg_namelen;
885 	m32.msg_iov = PTROUT(msg->msg_iov);
886 	m32.msg_iovlen = msg->msg_iovlen;
887 	m32.msg_control = PTROUT(msg->msg_control);
888 	m32.msg_controllen = msg->msg_controllen;
889 	m32.msg_flags = msg->msg_flags;
890 	error = copyout(&m32, msg32, sizeof(m32));
891 	return (error);
892 }
893 
894 #ifndef __mips__
895 #define FREEBSD32_ALIGNBYTES	(sizeof(int) - 1)
896 #else
897 #define FREEBSD32_ALIGNBYTES	(sizeof(long) - 1)
898 #endif
899 #define FREEBSD32_ALIGN(p)	\
900 	(((u_long)(p) + FREEBSD32_ALIGNBYTES) & ~FREEBSD32_ALIGNBYTES)
901 #define	FREEBSD32_CMSG_SPACE(l)	\
902 	(FREEBSD32_ALIGN(sizeof(struct cmsghdr)) + FREEBSD32_ALIGN(l))
903 
904 #define	FREEBSD32_CMSG_DATA(cmsg)	((unsigned char *)(cmsg) + \
905 				 FREEBSD32_ALIGN(sizeof(struct cmsghdr)))
906 
907 static size_t
freebsd32_cmsg_convert(const struct cmsghdr * cm,void * data,socklen_t datalen)908 freebsd32_cmsg_convert(const struct cmsghdr *cm, void *data, socklen_t datalen)
909 {
910 	size_t copylen;
911 	union {
912 		struct timespec32 ts;
913 		struct timeval32 tv;
914 		struct bintime32 bt;
915 	} tmp32;
916 
917 	union {
918 		struct timespec ts;
919 		struct timeval tv;
920 		struct bintime bt;
921 	} *in;
922 
923 	in = data;
924 	copylen = 0;
925 	switch (cm->cmsg_level) {
926 	case SOL_SOCKET:
927 		switch (cm->cmsg_type) {
928 		case SCM_TIMESTAMP:
929 			TV_CP(*in, tmp32, tv);
930 			copylen = sizeof(tmp32.tv);
931 			break;
932 
933 		case SCM_BINTIME:
934 			BT_CP(*in, tmp32, bt);
935 			copylen = sizeof(tmp32.bt);
936 			break;
937 
938 		case SCM_REALTIME:
939 		case SCM_MONOTONIC:
940 			TS_CP(*in, tmp32, ts);
941 			copylen = sizeof(tmp32.ts);
942 			break;
943 
944 		default:
945 			break;
946 		}
947 
948 	default:
949 		break;
950 	}
951 
952 	if (copylen == 0)
953 		return (datalen);
954 
955 	KASSERT((datalen >= copylen), ("corrupted cmsghdr"));
956 
957 	bcopy(&tmp32, data, copylen);
958 	return (copylen);
959 }
960 
961 static int
freebsd32_copy_msg_out(struct msghdr * msg,struct mbuf * control)962 freebsd32_copy_msg_out(struct msghdr *msg, struct mbuf *control)
963 {
964 	struct cmsghdr *cm;
965 	void *data;
966 	socklen_t clen, datalen, datalen_out, oldclen;
967 	int error;
968 	caddr_t ctlbuf;
969 	int len, maxlen, copylen;
970 	struct mbuf *m;
971 	error = 0;
972 
973 	len    = msg->msg_controllen;
974 	maxlen = msg->msg_controllen;
975 	msg->msg_controllen = 0;
976 
977 	ctlbuf = msg->msg_control;
978 	for (m = control; m != NULL && len > 0; m = m->m_next) {
979 		cm = mtod(m, struct cmsghdr *);
980 		clen = m->m_len;
981 		while (cm != NULL) {
982 			if (sizeof(struct cmsghdr) > clen ||
983 			    cm->cmsg_len > clen) {
984 				error = EINVAL;
985 				break;
986 			}
987 
988 			data   = CMSG_DATA(cm);
989 			datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
990 			datalen_out = freebsd32_cmsg_convert(cm, data, datalen);
991 
992 			/*
993 			 * Copy out the message header.  Preserve the native
994 			 * message size in case we need to inspect the message
995 			 * contents later.
996 			 */
997 			copylen = sizeof(struct cmsghdr);
998 			if (len < copylen) {
999 				msg->msg_flags |= MSG_CTRUNC;
1000 				m_dispose_extcontrolm(m);
1001 				goto exit;
1002 			}
1003 			oldclen = cm->cmsg_len;
1004 			cm->cmsg_len = FREEBSD32_ALIGN(sizeof(struct cmsghdr)) +
1005 			    datalen_out;
1006 			error = copyout(cm, ctlbuf, copylen);
1007 			cm->cmsg_len = oldclen;
1008 			if (error != 0)
1009 				goto exit;
1010 
1011 			ctlbuf += FREEBSD32_ALIGN(copylen);
1012 			len    -= FREEBSD32_ALIGN(copylen);
1013 
1014 			copylen = datalen_out;
1015 			if (len < copylen) {
1016 				msg->msg_flags |= MSG_CTRUNC;
1017 				m_dispose_extcontrolm(m);
1018 				break;
1019 			}
1020 
1021 			/* Copy out the message data. */
1022 			error = copyout(data, ctlbuf, copylen);
1023 			if (error)
1024 				goto exit;
1025 
1026 			ctlbuf += FREEBSD32_ALIGN(copylen);
1027 			len    -= FREEBSD32_ALIGN(copylen);
1028 
1029 			if (CMSG_SPACE(datalen) < clen) {
1030 				clen -= CMSG_SPACE(datalen);
1031 				cm = (struct cmsghdr *)
1032 				    ((caddr_t)cm + CMSG_SPACE(datalen));
1033 			} else {
1034 				clen = 0;
1035 				cm = NULL;
1036 			}
1037 
1038 			msg->msg_controllen +=
1039 			    FREEBSD32_CMSG_SPACE(datalen_out);
1040 		}
1041 	}
1042 	if (len == 0 && m != NULL) {
1043 		msg->msg_flags |= MSG_CTRUNC;
1044 		m_dispose_extcontrolm(m);
1045 	}
1046 
1047 exit:
1048 	return (error);
1049 }
1050 
1051 int
freebsd32_recvmsg(td,uap)1052 freebsd32_recvmsg(td, uap)
1053 	struct thread *td;
1054 	struct freebsd32_recvmsg_args /* {
1055 		int	s;
1056 		struct	msghdr32 *msg;
1057 		int	flags;
1058 	} */ *uap;
1059 {
1060 	struct msghdr msg;
1061 	struct msghdr32 m32;
1062 	struct iovec *uiov, *iov;
1063 	struct mbuf *control = NULL;
1064 	struct mbuf **controlp;
1065 
1066 	int error;
1067 	error = copyin(uap->msg, &m32, sizeof(m32));
1068 	if (error)
1069 		return (error);
1070 	error = freebsd32_copyinmsghdr(uap->msg, &msg);
1071 	if (error)
1072 		return (error);
1073 	error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1074 	    EMSGSIZE);
1075 	if (error)
1076 		return (error);
1077 	msg.msg_flags = uap->flags;
1078 	uiov = msg.msg_iov;
1079 	msg.msg_iov = iov;
1080 
1081 	controlp = (msg.msg_control != NULL) ?  &control : NULL;
1082 	error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, controlp);
1083 	if (error == 0) {
1084 		msg.msg_iov = uiov;
1085 
1086 		if (control != NULL)
1087 			error = freebsd32_copy_msg_out(&msg, control);
1088 		else
1089 			msg.msg_controllen = 0;
1090 
1091 		if (error == 0)
1092 			error = freebsd32_copyoutmsghdr(&msg, uap->msg);
1093 	}
1094 	free(iov, M_IOV);
1095 
1096 	if (control != NULL) {
1097 		if (error != 0)
1098 			m_dispose_extcontrolm(control);
1099 		m_freem(control);
1100 	}
1101 
1102 	return (error);
1103 }
1104 
1105 /*
1106  * Copy-in the array of control messages constructed using alignment
1107  * and padding suitable for a 32-bit environment and construct an
1108  * mbuf using alignment and padding suitable for a 64-bit kernel.
1109  * The alignment and padding are defined indirectly by CMSG_DATA(),
1110  * CMSG_SPACE() and CMSG_LEN().
1111  */
1112 static int
freebsd32_copyin_control(struct mbuf ** mp,caddr_t buf,u_int buflen)1113 freebsd32_copyin_control(struct mbuf **mp, caddr_t buf, u_int buflen)
1114 {
1115 	struct cmsghdr *cm;
1116 	struct mbuf *m;
1117 	void *in, *in1, *md;
1118 	u_int msglen, outlen;
1119 	int error;
1120 
1121 	if (buflen > MCLBYTES)
1122 		return (EINVAL);
1123 
1124 	in = malloc(buflen, M_TEMP, M_WAITOK);
1125 	error = copyin(buf, in, buflen);
1126 	if (error != 0)
1127 		goto out;
1128 
1129 	/*
1130 	 * Make a pass over the input buffer to determine the amount of space
1131 	 * required for 64 bit-aligned copies of the control messages.
1132 	 */
1133 	in1 = in;
1134 	outlen = 0;
1135 	while (buflen > 0) {
1136 		if (buflen < sizeof(*cm)) {
1137 			error = EINVAL;
1138 			break;
1139 		}
1140 		cm = (struct cmsghdr *)in1;
1141 		if (cm->cmsg_len < FREEBSD32_ALIGN(sizeof(*cm))) {
1142 			error = EINVAL;
1143 			break;
1144 		}
1145 		msglen = FREEBSD32_ALIGN(cm->cmsg_len);
1146 		if (msglen > buflen || msglen < cm->cmsg_len) {
1147 			error = EINVAL;
1148 			break;
1149 		}
1150 		buflen -= msglen;
1151 
1152 		in1 = (char *)in1 + msglen;
1153 		outlen += CMSG_ALIGN(sizeof(*cm)) +
1154 		    CMSG_ALIGN(msglen - FREEBSD32_ALIGN(sizeof(*cm)));
1155 	}
1156 	if (error == 0 && outlen > MCLBYTES) {
1157 		/*
1158 		 * XXXMJ This implies that the upper limit on 32-bit aligned
1159 		 * control messages is less than MCLBYTES, and so we are not
1160 		 * perfectly compatible.  However, there is no platform
1161 		 * guarantee that mbuf clusters larger than MCLBYTES can be
1162 		 * allocated.
1163 		 */
1164 		error = EINVAL;
1165 	}
1166 	if (error != 0)
1167 		goto out;
1168 
1169 	m = m_get2(outlen, M_WAITOK, MT_CONTROL, 0);
1170 	m->m_len = outlen;
1171 	md = mtod(m, void *);
1172 
1173 	/*
1174 	 * Make a second pass over input messages, copying them into the output
1175 	 * buffer.
1176 	 */
1177 	in1 = in;
1178 	while (outlen > 0) {
1179 		/* Copy the message header and align the length field. */
1180 		cm = md;
1181 		memcpy(cm, in1, sizeof(*cm));
1182 		msglen = cm->cmsg_len - FREEBSD32_ALIGN(sizeof(*cm));
1183 		cm->cmsg_len = CMSG_ALIGN(sizeof(*cm)) + msglen;
1184 
1185 		/* Copy the message body. */
1186 		in1 = (char *)in1 + FREEBSD32_ALIGN(sizeof(*cm));
1187 		md = (char *)md + CMSG_ALIGN(sizeof(*cm));
1188 		memcpy(md, in1, msglen);
1189 		in1 = (char *)in1 + FREEBSD32_ALIGN(msglen);
1190 		md = (char *)md + CMSG_ALIGN(msglen);
1191 		KASSERT(outlen >= CMSG_ALIGN(sizeof(*cm)) + CMSG_ALIGN(msglen),
1192 		    ("outlen %u underflow, msglen %u", outlen, msglen));
1193 		outlen -= CMSG_ALIGN(sizeof(*cm)) + CMSG_ALIGN(msglen);
1194 	}
1195 
1196 	*mp = m;
1197 out:
1198 	free(in, M_TEMP);
1199 	return (error);
1200 }
1201 
1202 int
freebsd32_sendmsg(struct thread * td,struct freebsd32_sendmsg_args * uap)1203 freebsd32_sendmsg(struct thread *td,
1204 		  struct freebsd32_sendmsg_args *uap)
1205 {
1206 	struct msghdr msg;
1207 	struct msghdr32 m32;
1208 	struct iovec *iov;
1209 	struct mbuf *control = NULL;
1210 	struct sockaddr *to = NULL;
1211 	int error;
1212 
1213 	error = copyin(uap->msg, &m32, sizeof(m32));
1214 	if (error)
1215 		return (error);
1216 	error = freebsd32_copyinmsghdr(uap->msg, &msg);
1217 	if (error)
1218 		return (error);
1219 	error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1220 	    EMSGSIZE);
1221 	if (error)
1222 		return (error);
1223 	msg.msg_iov = iov;
1224 	if (msg.msg_name != NULL) {
1225 		error = getsockaddr(&to, msg.msg_name, msg.msg_namelen);
1226 		if (error) {
1227 			to = NULL;
1228 			goto out;
1229 		}
1230 		msg.msg_name = to;
1231 	}
1232 
1233 	if (msg.msg_control) {
1234 		if (msg.msg_controllen < sizeof(struct cmsghdr)) {
1235 			error = EINVAL;
1236 			goto out;
1237 		}
1238 
1239 		error = freebsd32_copyin_control(&control, msg.msg_control,
1240 		    msg.msg_controllen);
1241 		if (error)
1242 			goto out;
1243 
1244 		msg.msg_control = NULL;
1245 		msg.msg_controllen = 0;
1246 	}
1247 
1248 	error = kern_sendit(td, uap->s, &msg, uap->flags, control,
1249 	    UIO_USERSPACE);
1250 
1251 out:
1252 	free(iov, M_IOV);
1253 	if (to)
1254 		free(to, M_SONAME);
1255 	return (error);
1256 }
1257 
1258 int
freebsd32_recvfrom(struct thread * td,struct freebsd32_recvfrom_args * uap)1259 freebsd32_recvfrom(struct thread *td,
1260 		   struct freebsd32_recvfrom_args *uap)
1261 {
1262 	struct msghdr msg;
1263 	struct iovec aiov;
1264 	int error;
1265 
1266 	if (uap->fromlenaddr) {
1267 		error = copyin(PTRIN(uap->fromlenaddr), &msg.msg_namelen,
1268 		    sizeof(msg.msg_namelen));
1269 		if (error)
1270 			return (error);
1271 	} else {
1272 		msg.msg_namelen = 0;
1273 	}
1274 
1275 	msg.msg_name = PTRIN(uap->from);
1276 	msg.msg_iov = &aiov;
1277 	msg.msg_iovlen = 1;
1278 	aiov.iov_base = PTRIN(uap->buf);
1279 	aiov.iov_len = uap->len;
1280 	msg.msg_control = NULL;
1281 	msg.msg_flags = uap->flags;
1282 	error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, NULL);
1283 	if (error == 0 && uap->fromlenaddr)
1284 		error = copyout(&msg.msg_namelen, PTRIN(uap->fromlenaddr),
1285 		    sizeof (msg.msg_namelen));
1286 	return (error);
1287 }
1288 
1289 int
freebsd32_settimeofday(struct thread * td,struct freebsd32_settimeofday_args * uap)1290 freebsd32_settimeofday(struct thread *td,
1291 		       struct freebsd32_settimeofday_args *uap)
1292 {
1293 	struct timeval32 tv32;
1294 	struct timeval tv, *tvp;
1295 	struct timezone tz, *tzp;
1296 	int error;
1297 
1298 	if (uap->tv) {
1299 		error = copyin(uap->tv, &tv32, sizeof(tv32));
1300 		if (error)
1301 			return (error);
1302 		CP(tv32, tv, tv_sec);
1303 		CP(tv32, tv, tv_usec);
1304 		tvp = &tv;
1305 	} else
1306 		tvp = NULL;
1307 	if (uap->tzp) {
1308 		error = copyin(uap->tzp, &tz, sizeof(tz));
1309 		if (error)
1310 			return (error);
1311 		tzp = &tz;
1312 	} else
1313 		tzp = NULL;
1314 	return (kern_settimeofday(td, tvp, tzp));
1315 }
1316 
1317 int
freebsd32_utimes(struct thread * td,struct freebsd32_utimes_args * uap)1318 freebsd32_utimes(struct thread *td, struct freebsd32_utimes_args *uap)
1319 {
1320 	struct timeval32 s32[2];
1321 	struct timeval s[2], *sp;
1322 	int error;
1323 
1324 	if (uap->tptr != NULL) {
1325 		error = copyin(uap->tptr, s32, sizeof(s32));
1326 		if (error)
1327 			return (error);
1328 		CP(s32[0], s[0], tv_sec);
1329 		CP(s32[0], s[0], tv_usec);
1330 		CP(s32[1], s[1], tv_sec);
1331 		CP(s32[1], s[1], tv_usec);
1332 		sp = s;
1333 	} else
1334 		sp = NULL;
1335 	return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1336 	    sp, UIO_SYSSPACE));
1337 }
1338 
1339 int
freebsd32_lutimes(struct thread * td,struct freebsd32_lutimes_args * uap)1340 freebsd32_lutimes(struct thread *td, struct freebsd32_lutimes_args *uap)
1341 {
1342 	struct timeval32 s32[2];
1343 	struct timeval s[2], *sp;
1344 	int error;
1345 
1346 	if (uap->tptr != NULL) {
1347 		error = copyin(uap->tptr, s32, sizeof(s32));
1348 		if (error)
1349 			return (error);
1350 		CP(s32[0], s[0], tv_sec);
1351 		CP(s32[0], s[0], tv_usec);
1352 		CP(s32[1], s[1], tv_sec);
1353 		CP(s32[1], s[1], tv_usec);
1354 		sp = s;
1355 	} else
1356 		sp = NULL;
1357 	return (kern_lutimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1358 }
1359 
1360 int
freebsd32_futimes(struct thread * td,struct freebsd32_futimes_args * uap)1361 freebsd32_futimes(struct thread *td, struct freebsd32_futimes_args *uap)
1362 {
1363 	struct timeval32 s32[2];
1364 	struct timeval s[2], *sp;
1365 	int error;
1366 
1367 	if (uap->tptr != NULL) {
1368 		error = copyin(uap->tptr, s32, sizeof(s32));
1369 		if (error)
1370 			return (error);
1371 		CP(s32[0], s[0], tv_sec);
1372 		CP(s32[0], s[0], tv_usec);
1373 		CP(s32[1], s[1], tv_sec);
1374 		CP(s32[1], s[1], tv_usec);
1375 		sp = s;
1376 	} else
1377 		sp = NULL;
1378 	return (kern_futimes(td, uap->fd, sp, UIO_SYSSPACE));
1379 }
1380 
1381 int
freebsd32_futimesat(struct thread * td,struct freebsd32_futimesat_args * uap)1382 freebsd32_futimesat(struct thread *td, struct freebsd32_futimesat_args *uap)
1383 {
1384 	struct timeval32 s32[2];
1385 	struct timeval s[2], *sp;
1386 	int error;
1387 
1388 	if (uap->times != NULL) {
1389 		error = copyin(uap->times, s32, sizeof(s32));
1390 		if (error)
1391 			return (error);
1392 		CP(s32[0], s[0], tv_sec);
1393 		CP(s32[0], s[0], tv_usec);
1394 		CP(s32[1], s[1], tv_sec);
1395 		CP(s32[1], s[1], tv_usec);
1396 		sp = s;
1397 	} else
1398 		sp = NULL;
1399 	return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
1400 		sp, UIO_SYSSPACE));
1401 }
1402 
1403 int
freebsd32_futimens(struct thread * td,struct freebsd32_futimens_args * uap)1404 freebsd32_futimens(struct thread *td, struct freebsd32_futimens_args *uap)
1405 {
1406 	struct timespec32 ts32[2];
1407 	struct timespec ts[2], *tsp;
1408 	int error;
1409 
1410 	if (uap->times != NULL) {
1411 		error = copyin(uap->times, ts32, sizeof(ts32));
1412 		if (error)
1413 			return (error);
1414 		CP(ts32[0], ts[0], tv_sec);
1415 		CP(ts32[0], ts[0], tv_nsec);
1416 		CP(ts32[1], ts[1], tv_sec);
1417 		CP(ts32[1], ts[1], tv_nsec);
1418 		tsp = ts;
1419 	} else
1420 		tsp = NULL;
1421 	return (kern_futimens(td, uap->fd, tsp, UIO_SYSSPACE));
1422 }
1423 
1424 int
freebsd32_utimensat(struct thread * td,struct freebsd32_utimensat_args * uap)1425 freebsd32_utimensat(struct thread *td, struct freebsd32_utimensat_args *uap)
1426 {
1427 	struct timespec32 ts32[2];
1428 	struct timespec ts[2], *tsp;
1429 	int error;
1430 
1431 	if (uap->times != NULL) {
1432 		error = copyin(uap->times, ts32, sizeof(ts32));
1433 		if (error)
1434 			return (error);
1435 		CP(ts32[0], ts[0], tv_sec);
1436 		CP(ts32[0], ts[0], tv_nsec);
1437 		CP(ts32[1], ts[1], tv_sec);
1438 		CP(ts32[1], ts[1], tv_nsec);
1439 		tsp = ts;
1440 	} else
1441 		tsp = NULL;
1442 	return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
1443 	    tsp, UIO_SYSSPACE, uap->flag));
1444 }
1445 
1446 int
freebsd32_adjtime(struct thread * td,struct freebsd32_adjtime_args * uap)1447 freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap)
1448 {
1449 	struct timeval32 tv32;
1450 	struct timeval delta, olddelta, *deltap;
1451 	int error;
1452 
1453 	if (uap->delta) {
1454 		error = copyin(uap->delta, &tv32, sizeof(tv32));
1455 		if (error)
1456 			return (error);
1457 		CP(tv32, delta, tv_sec);
1458 		CP(tv32, delta, tv_usec);
1459 		deltap = &delta;
1460 	} else
1461 		deltap = NULL;
1462 	error = kern_adjtime(td, deltap, &olddelta);
1463 	if (uap->olddelta && error == 0) {
1464 		CP(olddelta, tv32, tv_sec);
1465 		CP(olddelta, tv32, tv_usec);
1466 		error = copyout(&tv32, uap->olddelta, sizeof(tv32));
1467 	}
1468 	return (error);
1469 }
1470 
1471 #ifdef COMPAT_FREEBSD4
1472 int
freebsd4_freebsd32_statfs(struct thread * td,struct freebsd4_freebsd32_statfs_args * uap)1473 freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap)
1474 {
1475 	struct statfs32 s32;
1476 	struct statfs *sp;
1477 	int error;
1478 
1479 	sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1480 	error = kern_statfs(td, uap->path, UIO_USERSPACE, sp);
1481 	if (error == 0) {
1482 		copy_statfs(sp, &s32);
1483 		error = copyout(&s32, uap->buf, sizeof(s32));
1484 	}
1485 	free(sp, M_STATFS);
1486 	return (error);
1487 }
1488 #endif
1489 
1490 #ifdef COMPAT_FREEBSD4
1491 int
freebsd4_freebsd32_fstatfs(struct thread * td,struct freebsd4_freebsd32_fstatfs_args * uap)1492 freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap)
1493 {
1494 	struct statfs32 s32;
1495 	struct statfs *sp;
1496 	int error;
1497 
1498 	sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1499 	error = kern_fstatfs(td, uap->fd, sp);
1500 	if (error == 0) {
1501 		copy_statfs(sp, &s32);
1502 		error = copyout(&s32, uap->buf, sizeof(s32));
1503 	}
1504 	free(sp, M_STATFS);
1505 	return (error);
1506 }
1507 #endif
1508 
1509 #ifdef COMPAT_FREEBSD4
1510 int
freebsd4_freebsd32_fhstatfs(struct thread * td,struct freebsd4_freebsd32_fhstatfs_args * uap)1511 freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap)
1512 {
1513 	struct statfs32 s32;
1514 	struct statfs *sp;
1515 	fhandle_t fh;
1516 	int error;
1517 
1518 	if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
1519 		return (error);
1520 	sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1521 	error = kern_fhstatfs(td, fh, sp);
1522 	if (error == 0) {
1523 		copy_statfs(sp, &s32);
1524 		error = copyout(&s32, uap->buf, sizeof(s32));
1525 	}
1526 	free(sp, M_STATFS);
1527 	return (error);
1528 }
1529 #endif
1530 
1531 int
freebsd32_pread(struct thread * td,struct freebsd32_pread_args * uap)1532 freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap)
1533 {
1534 
1535 	return (kern_pread(td, uap->fd, uap->buf, uap->nbyte,
1536 	    PAIR32TO64(off_t, uap->offset)));
1537 }
1538 
1539 int
freebsd32_pwrite(struct thread * td,struct freebsd32_pwrite_args * uap)1540 freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap)
1541 {
1542 
1543 	return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte,
1544 	    PAIR32TO64(off_t, uap->offset)));
1545 }
1546 
1547 #ifdef COMPAT_43
1548 int
ofreebsd32_lseek(struct thread * td,struct ofreebsd32_lseek_args * uap)1549 ofreebsd32_lseek(struct thread *td, struct ofreebsd32_lseek_args *uap)
1550 {
1551 
1552 	return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1553 }
1554 #endif
1555 
1556 int
freebsd32_lseek(struct thread * td,struct freebsd32_lseek_args * uap)1557 freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap)
1558 {
1559 	int error;
1560 	off_t pos;
1561 
1562 	error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset),
1563 	    uap->whence);
1564 	/* Expand the quad return into two parts for eax and edx */
1565 	pos = td->td_uretoff.tdu_off;
1566 	td->td_retval[RETVAL_LO] = pos & 0xffffffff;	/* %eax */
1567 	td->td_retval[RETVAL_HI] = pos >> 32;		/* %edx */
1568 	return error;
1569 }
1570 
1571 int
freebsd32_truncate(struct thread * td,struct freebsd32_truncate_args * uap)1572 freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap)
1573 {
1574 
1575 	return (kern_truncate(td, uap->path, UIO_USERSPACE,
1576 	    PAIR32TO64(off_t, uap->length)));
1577 }
1578 
1579 int
freebsd32_ftruncate(struct thread * td,struct freebsd32_ftruncate_args * uap)1580 freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap)
1581 {
1582 
1583 	return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length)));
1584 }
1585 
1586 #ifdef COMPAT_43
1587 int
ofreebsd32_getdirentries(struct thread * td,struct ofreebsd32_getdirentries_args * uap)1588 ofreebsd32_getdirentries(struct thread *td,
1589     struct ofreebsd32_getdirentries_args *uap)
1590 {
1591 	struct ogetdirentries_args ap;
1592 	int error;
1593 	long loff;
1594 	int32_t loff_cut;
1595 
1596 	ap.fd = uap->fd;
1597 	ap.buf = uap->buf;
1598 	ap.count = uap->count;
1599 	ap.basep = NULL;
1600 	error = kern_ogetdirentries(td, &ap, &loff);
1601 	if (error == 0) {
1602 		loff_cut = loff;
1603 		error = copyout(&loff_cut, uap->basep, sizeof(int32_t));
1604 	}
1605 	return (error);
1606 }
1607 #endif
1608 
1609 int
freebsd32_getdirentries(struct thread * td,struct freebsd32_getdirentries_args * uap)1610 freebsd32_getdirentries(struct thread *td,
1611     struct freebsd32_getdirentries_args *uap)
1612 {
1613 	long base;
1614 	int32_t base32;
1615 	int error;
1616 
1617 	error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
1618 	    NULL, UIO_USERSPACE);
1619 	if (error)
1620 		return (error);
1621 	if (uap->basep != NULL) {
1622 		base32 = base;
1623 		error = copyout(&base32, uap->basep, sizeof(int32_t));
1624 	}
1625 	return (error);
1626 }
1627 
1628 #ifdef COMPAT_FREEBSD6
1629 /* versions with the 'int pad' argument */
1630 int
freebsd6_freebsd32_pread(struct thread * td,struct freebsd6_freebsd32_pread_args * uap)1631 freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap)
1632 {
1633 
1634 	return (kern_pread(td, uap->fd, uap->buf, uap->nbyte,
1635 	    PAIR32TO64(off_t, uap->offset)));
1636 }
1637 
1638 int
freebsd6_freebsd32_pwrite(struct thread * td,struct freebsd6_freebsd32_pwrite_args * uap)1639 freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap)
1640 {
1641 
1642 	return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte,
1643 	    PAIR32TO64(off_t, uap->offset)));
1644 }
1645 
1646 int
freebsd6_freebsd32_lseek(struct thread * td,struct freebsd6_freebsd32_lseek_args * uap)1647 freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap)
1648 {
1649 	int error;
1650 	off_t pos;
1651 
1652 	error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset),
1653 	    uap->whence);
1654 	/* Expand the quad return into two parts for eax and edx */
1655 	pos = *(off_t *)(td->td_retval);
1656 	td->td_retval[RETVAL_LO] = pos & 0xffffffff;	/* %eax */
1657 	td->td_retval[RETVAL_HI] = pos >> 32;		/* %edx */
1658 	return error;
1659 }
1660 
1661 int
freebsd6_freebsd32_truncate(struct thread * td,struct freebsd6_freebsd32_truncate_args * uap)1662 freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap)
1663 {
1664 
1665 	return (kern_truncate(td, uap->path, UIO_USERSPACE,
1666 	    PAIR32TO64(off_t, uap->length)));
1667 }
1668 
1669 int
freebsd6_freebsd32_ftruncate(struct thread * td,struct freebsd6_freebsd32_ftruncate_args * uap)1670 freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap)
1671 {
1672 
1673 	return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length)));
1674 }
1675 #endif /* COMPAT_FREEBSD6 */
1676 
1677 struct sf_hdtr32 {
1678 	uint32_t headers;
1679 	int hdr_cnt;
1680 	uint32_t trailers;
1681 	int trl_cnt;
1682 };
1683 
1684 static int
freebsd32_do_sendfile(struct thread * td,struct freebsd32_sendfile_args * uap,int compat)1685 freebsd32_do_sendfile(struct thread *td,
1686     struct freebsd32_sendfile_args *uap, int compat)
1687 {
1688 	struct sf_hdtr32 hdtr32;
1689 	struct sf_hdtr hdtr;
1690 	struct uio *hdr_uio, *trl_uio;
1691 	struct file *fp;
1692 	cap_rights_t rights;
1693 	struct iovec32 *iov32;
1694 	off_t offset, sbytes;
1695 	int error;
1696 
1697 	offset = PAIR32TO64(off_t, uap->offset);
1698 	if (offset < 0)
1699 		return (EINVAL);
1700 
1701 	hdr_uio = trl_uio = NULL;
1702 
1703 	if (uap->hdtr != NULL) {
1704 		error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
1705 		if (error)
1706 			goto out;
1707 		PTRIN_CP(hdtr32, hdtr, headers);
1708 		CP(hdtr32, hdtr, hdr_cnt);
1709 		PTRIN_CP(hdtr32, hdtr, trailers);
1710 		CP(hdtr32, hdtr, trl_cnt);
1711 
1712 		if (hdtr.headers != NULL) {
1713 			iov32 = PTRIN(hdtr32.headers);
1714 			error = freebsd32_copyinuio(iov32,
1715 			    hdtr32.hdr_cnt, &hdr_uio);
1716 			if (error)
1717 				goto out;
1718 #ifdef COMPAT_FREEBSD4
1719 			/*
1720 			 * In FreeBSD < 5.0 the nbytes to send also included
1721 			 * the header.  If compat is specified subtract the
1722 			 * header size from nbytes.
1723 			 */
1724 			if (compat) {
1725 				if (uap->nbytes > hdr_uio->uio_resid)
1726 					uap->nbytes -= hdr_uio->uio_resid;
1727 				else
1728 					uap->nbytes = 0;
1729 			}
1730 #endif
1731 		}
1732 		if (hdtr.trailers != NULL) {
1733 			iov32 = PTRIN(hdtr32.trailers);
1734 			error = freebsd32_copyinuio(iov32,
1735 			    hdtr32.trl_cnt, &trl_uio);
1736 			if (error)
1737 				goto out;
1738 		}
1739 	}
1740 
1741 	AUDIT_ARG_FD(uap->fd);
1742 
1743 	if ((error = fget_read(td, uap->fd,
1744 	    cap_rights_init(&rights, CAP_PREAD), &fp)) != 0)
1745 		goto out;
1746 
1747 	error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, offset,
1748 	    uap->nbytes, &sbytes, uap->flags, td);
1749 	fdrop(fp, td);
1750 
1751 	if (uap->sbytes != NULL)
1752 		copyout(&sbytes, uap->sbytes, sizeof(off_t));
1753 
1754 out:
1755 	if (hdr_uio)
1756 		free(hdr_uio, M_IOV);
1757 	if (trl_uio)
1758 		free(trl_uio, M_IOV);
1759 	return (error);
1760 }
1761 
1762 #ifdef COMPAT_FREEBSD4
1763 int
freebsd4_freebsd32_sendfile(struct thread * td,struct freebsd4_freebsd32_sendfile_args * uap)1764 freebsd4_freebsd32_sendfile(struct thread *td,
1765     struct freebsd4_freebsd32_sendfile_args *uap)
1766 {
1767 	return (freebsd32_do_sendfile(td,
1768 	    (struct freebsd32_sendfile_args *)uap, 1));
1769 }
1770 #endif
1771 
1772 int
freebsd32_sendfile(struct thread * td,struct freebsd32_sendfile_args * uap)1773 freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
1774 {
1775 
1776 	return (freebsd32_do_sendfile(td, uap, 0));
1777 }
1778 
1779 static void
copy_stat(struct stat * in,struct stat32 * out)1780 copy_stat(struct stat *in, struct stat32 *out)
1781 {
1782 
1783 	bzero(out, sizeof(*out));
1784 	CP(*in, *out, st_dev);
1785 	CP(*in, *out, st_ino);
1786 	CP(*in, *out, st_mode);
1787 	CP(*in, *out, st_nlink);
1788 	CP(*in, *out, st_uid);
1789 	CP(*in, *out, st_gid);
1790 	CP(*in, *out, st_rdev);
1791 	TS_CP(*in, *out, st_atim);
1792 	TS_CP(*in, *out, st_mtim);
1793 	TS_CP(*in, *out, st_ctim);
1794 	CP(*in, *out, st_size);
1795 	CP(*in, *out, st_blocks);
1796 	CP(*in, *out, st_blksize);
1797 	CP(*in, *out, st_flags);
1798 	CP(*in, *out, st_gen);
1799 	TS_CP(*in, *out, st_birthtim);
1800 }
1801 
1802 #ifdef COMPAT_43
1803 static void
copy_ostat(struct stat * in,struct ostat32 * out)1804 copy_ostat(struct stat *in, struct ostat32 *out)
1805 {
1806 
1807 	bzero(out, sizeof(*out));
1808 	CP(*in, *out, st_dev);
1809 	CP(*in, *out, st_ino);
1810 	CP(*in, *out, st_mode);
1811 	CP(*in, *out, st_nlink);
1812 	CP(*in, *out, st_uid);
1813 	CP(*in, *out, st_gid);
1814 	CP(*in, *out, st_rdev);
1815 	CP(*in, *out, st_size);
1816 	TS_CP(*in, *out, st_atim);
1817 	TS_CP(*in, *out, st_mtim);
1818 	TS_CP(*in, *out, st_ctim);
1819 	CP(*in, *out, st_blksize);
1820 	CP(*in, *out, st_blocks);
1821 	CP(*in, *out, st_flags);
1822 	CP(*in, *out, st_gen);
1823 }
1824 #endif
1825 
1826 int
freebsd32_stat(struct thread * td,struct freebsd32_stat_args * uap)1827 freebsd32_stat(struct thread *td, struct freebsd32_stat_args *uap)
1828 {
1829 	struct stat sb;
1830 	struct stat32 sb32;
1831 	int error;
1832 
1833 	error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
1834 	    &sb, NULL);
1835 	if (error)
1836 		return (error);
1837 	copy_stat(&sb, &sb32);
1838 	error = copyout(&sb32, uap->ub, sizeof (sb32));
1839 	return (error);
1840 }
1841 
1842 #ifdef COMPAT_43
1843 int
ofreebsd32_stat(struct thread * td,struct ofreebsd32_stat_args * uap)1844 ofreebsd32_stat(struct thread *td, struct ofreebsd32_stat_args *uap)
1845 {
1846 	struct stat sb;
1847 	struct ostat32 sb32;
1848 	int error;
1849 
1850 	error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
1851 	    &sb, NULL);
1852 	if (error)
1853 		return (error);
1854 	copy_ostat(&sb, &sb32);
1855 	error = copyout(&sb32, uap->ub, sizeof (sb32));
1856 	return (error);
1857 }
1858 #endif
1859 
1860 int
freebsd32_fstat(struct thread * td,struct freebsd32_fstat_args * uap)1861 freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
1862 {
1863 	struct stat ub;
1864 	struct stat32 ub32;
1865 	int error;
1866 
1867 	error = kern_fstat(td, uap->fd, &ub);
1868 	if (error)
1869 		return (error);
1870 	copy_stat(&ub, &ub32);
1871 	error = copyout(&ub32, uap->ub, sizeof(ub32));
1872 	return (error);
1873 }
1874 
1875 #ifdef COMPAT_43
1876 int
ofreebsd32_fstat(struct thread * td,struct ofreebsd32_fstat_args * uap)1877 ofreebsd32_fstat(struct thread *td, struct ofreebsd32_fstat_args *uap)
1878 {
1879 	struct stat ub;
1880 	struct ostat32 ub32;
1881 	int error;
1882 
1883 	error = kern_fstat(td, uap->fd, &ub);
1884 	if (error)
1885 		return (error);
1886 	copy_ostat(&ub, &ub32);
1887 	error = copyout(&ub32, uap->ub, sizeof(ub32));
1888 	return (error);
1889 }
1890 #endif
1891 
1892 int
freebsd32_fstatat(struct thread * td,struct freebsd32_fstatat_args * uap)1893 freebsd32_fstatat(struct thread *td, struct freebsd32_fstatat_args *uap)
1894 {
1895 	struct stat ub;
1896 	struct stat32 ub32;
1897 	int error;
1898 
1899 	error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE,
1900 	    &ub, NULL);
1901 	if (error)
1902 		return (error);
1903 	copy_stat(&ub, &ub32);
1904 	error = copyout(&ub32, uap->buf, sizeof(ub32));
1905 	return (error);
1906 }
1907 
1908 int
freebsd32_lstat(struct thread * td,struct freebsd32_lstat_args * uap)1909 freebsd32_lstat(struct thread *td, struct freebsd32_lstat_args *uap)
1910 {
1911 	struct stat sb;
1912 	struct stat32 sb32;
1913 	int error;
1914 
1915 	error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
1916 	    UIO_USERSPACE, &sb, NULL);
1917 	if (error)
1918 		return (error);
1919 	copy_stat(&sb, &sb32);
1920 	error = copyout(&sb32, uap->ub, sizeof (sb32));
1921 	return (error);
1922 }
1923 
1924 #ifdef COMPAT_43
1925 int
ofreebsd32_lstat(struct thread * td,struct ofreebsd32_lstat_args * uap)1926 ofreebsd32_lstat(struct thread *td, struct ofreebsd32_lstat_args *uap)
1927 {
1928 	struct stat sb;
1929 	struct ostat32 sb32;
1930 	int error;
1931 
1932 	error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
1933 	    UIO_USERSPACE, &sb, NULL);
1934 	if (error)
1935 		return (error);
1936 	copy_ostat(&sb, &sb32);
1937 	error = copyout(&sb32, uap->ub, sizeof (sb32));
1938 	return (error);
1939 }
1940 #endif
1941 
1942 int
freebsd32_sysctl(struct thread * td,struct freebsd32_sysctl_args * uap)1943 freebsd32_sysctl(struct thread *td, struct freebsd32_sysctl_args *uap)
1944 {
1945 	int error, name[CTL_MAXNAME];
1946 	size_t j, oldlen;
1947 	uint32_t tmp;
1948 
1949 	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1950 		return (EINVAL);
1951  	error = copyin(uap->name, name, uap->namelen * sizeof(int));
1952  	if (error)
1953 		return (error);
1954 	if (uap->oldlenp) {
1955 		error = fueword32(uap->oldlenp, &tmp);
1956 		oldlen = tmp;
1957 	} else {
1958 		oldlen = 0;
1959 	}
1960 	if (error != 0)
1961 		return (EFAULT);
1962 	error = userland_sysctl(td, name, uap->namelen,
1963 		uap->old, &oldlen, 1,
1964 		uap->new, uap->newlen, &j, SCTL_MASK32);
1965 	if (error && error != ENOMEM)
1966 		return (error);
1967 	if (uap->oldlenp)
1968 		suword32(uap->oldlenp, j);
1969 	return (0);
1970 }
1971 
1972 int
freebsd32_jail(struct thread * td,struct freebsd32_jail_args * uap)1973 freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap)
1974 {
1975 	uint32_t version;
1976 	int error;
1977 	struct jail j;
1978 
1979 	error = copyin(uap->jail, &version, sizeof(uint32_t));
1980 	if (error)
1981 		return (error);
1982 
1983 	switch (version) {
1984 	case 0:
1985 	{
1986 		/* FreeBSD single IPv4 jails. */
1987 		struct jail32_v0 j32_v0;
1988 
1989 		bzero(&j, sizeof(struct jail));
1990 		error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
1991 		if (error)
1992 			return (error);
1993 		CP(j32_v0, j, version);
1994 		PTRIN_CP(j32_v0, j, path);
1995 		PTRIN_CP(j32_v0, j, hostname);
1996 		j.ip4s = htonl(j32_v0.ip_number);	/* jail_v0 is host order */
1997 		break;
1998 	}
1999 
2000 	case 1:
2001 		/*
2002 		 * Version 1 was used by multi-IPv4 jail implementations
2003 		 * that never made it into the official kernel.
2004 		 */
2005 		return (EINVAL);
2006 
2007 	case 2:	/* JAIL_API_VERSION */
2008 	{
2009 		/* FreeBSD multi-IPv4/IPv6,noIP jails. */
2010 		struct jail32 j32;
2011 
2012 		error = copyin(uap->jail, &j32, sizeof(struct jail32));
2013 		if (error)
2014 			return (error);
2015 		CP(j32, j, version);
2016 		PTRIN_CP(j32, j, path);
2017 		PTRIN_CP(j32, j, hostname);
2018 		PTRIN_CP(j32, j, jailname);
2019 		CP(j32, j, ip4s);
2020 		CP(j32, j, ip6s);
2021 		PTRIN_CP(j32, j, ip4);
2022 		PTRIN_CP(j32, j, ip6);
2023 		break;
2024 	}
2025 
2026 	default:
2027 		/* Sci-Fi jails are not supported, sorry. */
2028 		return (EINVAL);
2029 	}
2030 	return (kern_jail(td, &j));
2031 }
2032 
2033 int
freebsd32_jail_set(struct thread * td,struct freebsd32_jail_set_args * uap)2034 freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap)
2035 {
2036 	struct uio *auio;
2037 	int error;
2038 
2039 	/* Check that we have an even number of iovecs. */
2040 	if (uap->iovcnt & 1)
2041 		return (EINVAL);
2042 
2043 	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2044 	if (error)
2045 		return (error);
2046 	error = kern_jail_set(td, auio, uap->flags);
2047 	free(auio, M_IOV);
2048 	return (error);
2049 }
2050 
2051 int
freebsd32_jail_get(struct thread * td,struct freebsd32_jail_get_args * uap)2052 freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap)
2053 {
2054 	struct iovec32 iov32;
2055 	struct uio *auio;
2056 	int error, i;
2057 
2058 	/* Check that we have an even number of iovecs. */
2059 	if (uap->iovcnt & 1)
2060 		return (EINVAL);
2061 
2062 	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2063 	if (error)
2064 		return (error);
2065 	error = kern_jail_get(td, auio, uap->flags);
2066 	if (error == 0)
2067 		for (i = 0; i < uap->iovcnt; i++) {
2068 			PTROUT_CP(auio->uio_iov[i], iov32, iov_base);
2069 			CP(auio->uio_iov[i], iov32, iov_len);
2070 			error = copyout(&iov32, uap->iovp + i, sizeof(iov32));
2071 			if (error != 0)
2072 				break;
2073 		}
2074 	free(auio, M_IOV);
2075 	return (error);
2076 }
2077 
2078 int
freebsd32_sigaction(struct thread * td,struct freebsd32_sigaction_args * uap)2079 freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
2080 {
2081 	struct sigaction32 s32;
2082 	struct sigaction sa, osa, *sap;
2083 	int error;
2084 
2085 	if (uap->act) {
2086 		error = copyin(uap->act, &s32, sizeof(s32));
2087 		if (error)
2088 			return (error);
2089 		sa.sa_handler = PTRIN(s32.sa_u);
2090 		CP(s32, sa, sa_flags);
2091 		CP(s32, sa, sa_mask);
2092 		sap = &sa;
2093 	} else
2094 		sap = NULL;
2095 	error = kern_sigaction(td, uap->sig, sap, &osa, 0);
2096 	if (error == 0 && uap->oact != NULL) {
2097 		s32.sa_u = PTROUT(osa.sa_handler);
2098 		CP(osa, s32, sa_flags);
2099 		CP(osa, s32, sa_mask);
2100 		error = copyout(&s32, uap->oact, sizeof(s32));
2101 	}
2102 	return (error);
2103 }
2104 
2105 #ifdef COMPAT_FREEBSD4
2106 int
freebsd4_freebsd32_sigaction(struct thread * td,struct freebsd4_freebsd32_sigaction_args * uap)2107 freebsd4_freebsd32_sigaction(struct thread *td,
2108 			     struct freebsd4_freebsd32_sigaction_args *uap)
2109 {
2110 	struct sigaction32 s32;
2111 	struct sigaction sa, osa, *sap;
2112 	int error;
2113 
2114 	if (uap->act) {
2115 		error = copyin(uap->act, &s32, sizeof(s32));
2116 		if (error)
2117 			return (error);
2118 		sa.sa_handler = PTRIN(s32.sa_u);
2119 		CP(s32, sa, sa_flags);
2120 		CP(s32, sa, sa_mask);
2121 		sap = &sa;
2122 	} else
2123 		sap = NULL;
2124 	error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
2125 	if (error == 0 && uap->oact != NULL) {
2126 		s32.sa_u = PTROUT(osa.sa_handler);
2127 		CP(osa, s32, sa_flags);
2128 		CP(osa, s32, sa_mask);
2129 		error = copyout(&s32, uap->oact, sizeof(s32));
2130 	}
2131 	return (error);
2132 }
2133 #endif
2134 
2135 #ifdef COMPAT_43
2136 struct osigaction32 {
2137 	u_int32_t	sa_u;
2138 	osigset_t	sa_mask;
2139 	int		sa_flags;
2140 };
2141 
2142 #define	ONSIG	32
2143 
2144 int
ofreebsd32_sigaction(struct thread * td,struct ofreebsd32_sigaction_args * uap)2145 ofreebsd32_sigaction(struct thread *td,
2146 			     struct ofreebsd32_sigaction_args *uap)
2147 {
2148 	struct osigaction32 s32;
2149 	struct sigaction sa, osa, *sap;
2150 	int error;
2151 
2152 	if (uap->signum <= 0 || uap->signum >= ONSIG)
2153 		return (EINVAL);
2154 
2155 	if (uap->nsa) {
2156 		error = copyin(uap->nsa, &s32, sizeof(s32));
2157 		if (error)
2158 			return (error);
2159 		sa.sa_handler = PTRIN(s32.sa_u);
2160 		CP(s32, sa, sa_flags);
2161 		OSIG2SIG(s32.sa_mask, sa.sa_mask);
2162 		sap = &sa;
2163 	} else
2164 		sap = NULL;
2165 	error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2166 	if (error == 0 && uap->osa != NULL) {
2167 		s32.sa_u = PTROUT(osa.sa_handler);
2168 		CP(osa, s32, sa_flags);
2169 		SIG2OSIG(osa.sa_mask, s32.sa_mask);
2170 		error = copyout(&s32, uap->osa, sizeof(s32));
2171 	}
2172 	return (error);
2173 }
2174 
2175 int
ofreebsd32_sigprocmask(struct thread * td,struct ofreebsd32_sigprocmask_args * uap)2176 ofreebsd32_sigprocmask(struct thread *td,
2177 			       struct ofreebsd32_sigprocmask_args *uap)
2178 {
2179 	sigset_t set, oset;
2180 	int error;
2181 
2182 	OSIG2SIG(uap->mask, set);
2183 	error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD);
2184 	SIG2OSIG(oset, td->td_retval[0]);
2185 	return (error);
2186 }
2187 
2188 int
ofreebsd32_sigpending(struct thread * td,struct ofreebsd32_sigpending_args * uap)2189 ofreebsd32_sigpending(struct thread *td,
2190 			      struct ofreebsd32_sigpending_args *uap)
2191 {
2192 	struct proc *p = td->td_proc;
2193 	sigset_t siglist;
2194 
2195 	PROC_LOCK(p);
2196 	siglist = p->p_siglist;
2197 	SIGSETOR(siglist, td->td_siglist);
2198 	PROC_UNLOCK(p);
2199 	SIG2OSIG(siglist, td->td_retval[0]);
2200 	return (0);
2201 }
2202 
2203 struct sigvec32 {
2204 	u_int32_t	sv_handler;
2205 	int		sv_mask;
2206 	int		sv_flags;
2207 };
2208 
2209 int
ofreebsd32_sigvec(struct thread * td,struct ofreebsd32_sigvec_args * uap)2210 ofreebsd32_sigvec(struct thread *td,
2211 			  struct ofreebsd32_sigvec_args *uap)
2212 {
2213 	struct sigvec32 vec;
2214 	struct sigaction sa, osa, *sap;
2215 	int error;
2216 
2217 	if (uap->signum <= 0 || uap->signum >= ONSIG)
2218 		return (EINVAL);
2219 
2220 	if (uap->nsv) {
2221 		error = copyin(uap->nsv, &vec, sizeof(vec));
2222 		if (error)
2223 			return (error);
2224 		sa.sa_handler = PTRIN(vec.sv_handler);
2225 		OSIG2SIG(vec.sv_mask, sa.sa_mask);
2226 		sa.sa_flags = vec.sv_flags;
2227 		sa.sa_flags ^= SA_RESTART;
2228 		sap = &sa;
2229 	} else
2230 		sap = NULL;
2231 	error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2232 	if (error == 0 && uap->osv != NULL) {
2233 		vec.sv_handler = PTROUT(osa.sa_handler);
2234 		SIG2OSIG(osa.sa_mask, vec.sv_mask);
2235 		vec.sv_flags = osa.sa_flags;
2236 		vec.sv_flags &= ~SA_NOCLDWAIT;
2237 		vec.sv_flags ^= SA_RESTART;
2238 		error = copyout(&vec, uap->osv, sizeof(vec));
2239 	}
2240 	return (error);
2241 }
2242 
2243 int
ofreebsd32_sigblock(struct thread * td,struct ofreebsd32_sigblock_args * uap)2244 ofreebsd32_sigblock(struct thread *td,
2245 			    struct ofreebsd32_sigblock_args *uap)
2246 {
2247 	sigset_t set, oset;
2248 
2249 	OSIG2SIG(uap->mask, set);
2250 	kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
2251 	SIG2OSIG(oset, td->td_retval[0]);
2252 	return (0);
2253 }
2254 
2255 int
ofreebsd32_sigsetmask(struct thread * td,struct ofreebsd32_sigsetmask_args * uap)2256 ofreebsd32_sigsetmask(struct thread *td,
2257 			      struct ofreebsd32_sigsetmask_args *uap)
2258 {
2259 	sigset_t set, oset;
2260 
2261 	OSIG2SIG(uap->mask, set);
2262 	kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
2263 	SIG2OSIG(oset, td->td_retval[0]);
2264 	return (0);
2265 }
2266 
2267 int
ofreebsd32_sigsuspend(struct thread * td,struct ofreebsd32_sigsuspend_args * uap)2268 ofreebsd32_sigsuspend(struct thread *td,
2269 			      struct ofreebsd32_sigsuspend_args *uap)
2270 {
2271 	sigset_t mask;
2272 
2273 	OSIG2SIG(uap->mask, mask);
2274 	return (kern_sigsuspend(td, mask));
2275 }
2276 
2277 struct sigstack32 {
2278 	u_int32_t	ss_sp;
2279 	int		ss_onstack;
2280 };
2281 
2282 int
ofreebsd32_sigstack(struct thread * td,struct ofreebsd32_sigstack_args * uap)2283 ofreebsd32_sigstack(struct thread *td,
2284 			    struct ofreebsd32_sigstack_args *uap)
2285 {
2286 	struct sigstack32 s32;
2287 	struct sigstack nss, oss;
2288 	int error = 0, unss;
2289 
2290 	if (uap->nss != NULL) {
2291 		error = copyin(uap->nss, &s32, sizeof(s32));
2292 		if (error)
2293 			return (error);
2294 		nss.ss_sp = PTRIN(s32.ss_sp);
2295 		CP(s32, nss, ss_onstack);
2296 		unss = 1;
2297 	} else {
2298 		unss = 0;
2299 	}
2300 	oss.ss_sp = td->td_sigstk.ss_sp;
2301 	oss.ss_onstack = sigonstack(cpu_getstack(td));
2302 	if (unss) {
2303 		td->td_sigstk.ss_sp = nss.ss_sp;
2304 		td->td_sigstk.ss_size = 0;
2305 		td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2306 		td->td_pflags |= TDP_ALTSTACK;
2307 	}
2308 	if (uap->oss != NULL) {
2309 		s32.ss_sp = PTROUT(oss.ss_sp);
2310 		CP(oss, s32, ss_onstack);
2311 		error = copyout(&s32, uap->oss, sizeof(s32));
2312 	}
2313 	return (error);
2314 }
2315 #endif
2316 
2317 int
freebsd32_nanosleep(struct thread * td,struct freebsd32_nanosleep_args * uap)2318 freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2319 {
2320 
2321 	return (freebsd32_user_clock_nanosleep(td, CLOCK_REALTIME,
2322 	    TIMER_RELTIME, uap->rqtp, uap->rmtp));
2323 }
2324 
2325 int
freebsd32_clock_nanosleep(struct thread * td,struct freebsd32_clock_nanosleep_args * uap)2326 freebsd32_clock_nanosleep(struct thread *td,
2327     struct freebsd32_clock_nanosleep_args *uap)
2328 {
2329 	int error;
2330 
2331 	error = freebsd32_user_clock_nanosleep(td, uap->clock_id, uap->flags,
2332 	    uap->rqtp, uap->rmtp);
2333 	return (kern_posix_error(td, error));
2334 }
2335 
2336 static int
freebsd32_user_clock_nanosleep(struct thread * td,clockid_t clock_id,int flags,const struct timespec32 * ua_rqtp,struct timespec32 * ua_rmtp)2337 freebsd32_user_clock_nanosleep(struct thread *td, clockid_t clock_id,
2338     int flags, const struct timespec32 *ua_rqtp, struct timespec32 *ua_rmtp)
2339 {
2340 	struct timespec32 rmt32, rqt32;
2341 	struct timespec rmt, rqt;
2342 	int error, error2;
2343 
2344 	error = copyin(ua_rqtp, &rqt32, sizeof(rqt32));
2345 	if (error)
2346 		return (error);
2347 
2348 	CP(rqt32, rqt, tv_sec);
2349 	CP(rqt32, rqt, tv_nsec);
2350 
2351 	error = kern_clock_nanosleep(td, clock_id, flags, &rqt, &rmt);
2352 	if (error == EINTR && ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0) {
2353 		CP(rmt, rmt32, tv_sec);
2354 		CP(rmt, rmt32, tv_nsec);
2355 
2356 		error2 = copyout(&rmt32, ua_rmtp, sizeof(rmt32));
2357 		if (error2 != 0)
2358 			error = error2;
2359 	}
2360 	return (error);
2361 }
2362 
2363 int
freebsd32_clock_gettime(struct thread * td,struct freebsd32_clock_gettime_args * uap)2364 freebsd32_clock_gettime(struct thread *td,
2365 			struct freebsd32_clock_gettime_args *uap)
2366 {
2367 	struct timespec	ats;
2368 	struct timespec32 ats32;
2369 	int error;
2370 
2371 	error = kern_clock_gettime(td, uap->clock_id, &ats);
2372 	if (error == 0) {
2373 		CP(ats, ats32, tv_sec);
2374 		CP(ats, ats32, tv_nsec);
2375 		error = copyout(&ats32, uap->tp, sizeof(ats32));
2376 	}
2377 	return (error);
2378 }
2379 
2380 int
freebsd32_clock_settime(struct thread * td,struct freebsd32_clock_settime_args * uap)2381 freebsd32_clock_settime(struct thread *td,
2382 			struct freebsd32_clock_settime_args *uap)
2383 {
2384 	struct timespec	ats;
2385 	struct timespec32 ats32;
2386 	int error;
2387 
2388 	error = copyin(uap->tp, &ats32, sizeof(ats32));
2389 	if (error)
2390 		return (error);
2391 	CP(ats32, ats, tv_sec);
2392 	CP(ats32, ats, tv_nsec);
2393 
2394 	return (kern_clock_settime(td, uap->clock_id, &ats));
2395 }
2396 
2397 int
freebsd32_clock_getres(struct thread * td,struct freebsd32_clock_getres_args * uap)2398 freebsd32_clock_getres(struct thread *td,
2399 		       struct freebsd32_clock_getres_args *uap)
2400 {
2401 	struct timespec	ts;
2402 	struct timespec32 ts32;
2403 	int error;
2404 
2405 	if (uap->tp == NULL)
2406 		return (0);
2407 	error = kern_clock_getres(td, uap->clock_id, &ts);
2408 	if (error == 0) {
2409 		CP(ts, ts32, tv_sec);
2410 		CP(ts, ts32, tv_nsec);
2411 		error = copyout(&ts32, uap->tp, sizeof(ts32));
2412 	}
2413 	return (error);
2414 }
2415 
freebsd32_ktimer_create(struct thread * td,struct freebsd32_ktimer_create_args * uap)2416 int freebsd32_ktimer_create(struct thread *td,
2417     struct freebsd32_ktimer_create_args *uap)
2418 {
2419 	struct sigevent32 ev32;
2420 	struct sigevent ev, *evp;
2421 	int error, id;
2422 
2423 	if (uap->evp == NULL) {
2424 		evp = NULL;
2425 	} else {
2426 		evp = &ev;
2427 		error = copyin(uap->evp, &ev32, sizeof(ev32));
2428 		if (error != 0)
2429 			return (error);
2430 		error = convert_sigevent32(&ev32, &ev);
2431 		if (error != 0)
2432 			return (error);
2433 	}
2434 	error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
2435 	if (error == 0) {
2436 		error = copyout(&id, uap->timerid, sizeof(int));
2437 		if (error != 0)
2438 			kern_ktimer_delete(td, id);
2439 	}
2440 	return (error);
2441 }
2442 
2443 int
freebsd32_ktimer_settime(struct thread * td,struct freebsd32_ktimer_settime_args * uap)2444 freebsd32_ktimer_settime(struct thread *td,
2445     struct freebsd32_ktimer_settime_args *uap)
2446 {
2447 	struct itimerspec32 val32, oval32;
2448 	struct itimerspec val, oval, *ovalp;
2449 	int error;
2450 
2451 	error = copyin(uap->value, &val32, sizeof(val32));
2452 	if (error != 0)
2453 		return (error);
2454 	ITS_CP(val32, val);
2455 	ovalp = uap->ovalue != NULL ? &oval : NULL;
2456 	error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
2457 	if (error == 0 && uap->ovalue != NULL) {
2458 		ITS_CP(oval, oval32);
2459 		error = copyout(&oval32, uap->ovalue, sizeof(oval32));
2460 	}
2461 	return (error);
2462 }
2463 
2464 int
freebsd32_ktimer_gettime(struct thread * td,struct freebsd32_ktimer_gettime_args * uap)2465 freebsd32_ktimer_gettime(struct thread *td,
2466     struct freebsd32_ktimer_gettime_args *uap)
2467 {
2468 	struct itimerspec32 val32;
2469 	struct itimerspec val;
2470 	int error;
2471 
2472 	error = kern_ktimer_gettime(td, uap->timerid, &val);
2473 	if (error == 0) {
2474 		ITS_CP(val, val32);
2475 		error = copyout(&val32, uap->value, sizeof(val32));
2476 	}
2477 	return (error);
2478 }
2479 
2480 int
freebsd32_clock_getcpuclockid2(struct thread * td,struct freebsd32_clock_getcpuclockid2_args * uap)2481 freebsd32_clock_getcpuclockid2(struct thread *td,
2482     struct freebsd32_clock_getcpuclockid2_args *uap)
2483 {
2484 	clockid_t clk_id;
2485 	int error;
2486 
2487 	error = kern_clock_getcpuclockid2(td, PAIR32TO64(id_t, uap->id),
2488 	    uap->which, &clk_id);
2489 	if (error == 0)
2490 		error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
2491 	return (error);
2492 }
2493 
2494 int
freebsd32_thr_new(struct thread * td,struct freebsd32_thr_new_args * uap)2495 freebsd32_thr_new(struct thread *td,
2496 		  struct freebsd32_thr_new_args *uap)
2497 {
2498 	struct thr_param32 param32;
2499 	struct thr_param param;
2500 	int error;
2501 
2502 	if (uap->param_size < 0 ||
2503 	    uap->param_size > sizeof(struct thr_param32))
2504 		return (EINVAL);
2505 	bzero(&param, sizeof(struct thr_param));
2506 	bzero(&param32, sizeof(struct thr_param32));
2507 	error = copyin(uap->param, &param32, uap->param_size);
2508 	if (error != 0)
2509 		return (error);
2510 	param.start_func = PTRIN(param32.start_func);
2511 	param.arg = PTRIN(param32.arg);
2512 	param.stack_base = PTRIN(param32.stack_base);
2513 	param.stack_size = param32.stack_size;
2514 	param.tls_base = PTRIN(param32.tls_base);
2515 	param.tls_size = param32.tls_size;
2516 	param.child_tid = PTRIN(param32.child_tid);
2517 	param.parent_tid = PTRIN(param32.parent_tid);
2518 	param.flags = param32.flags;
2519 	param.rtp = PTRIN(param32.rtp);
2520 	param.spare[0] = PTRIN(param32.spare[0]);
2521 	param.spare[1] = PTRIN(param32.spare[1]);
2522 	param.spare[2] = PTRIN(param32.spare[2]);
2523 
2524 	return (kern_thr_new(td, &param));
2525 }
2526 
2527 int
freebsd32_thr_suspend(struct thread * td,struct freebsd32_thr_suspend_args * uap)2528 freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
2529 {
2530 	struct timespec32 ts32;
2531 	struct timespec ts, *tsp;
2532 	int error;
2533 
2534 	error = 0;
2535 	tsp = NULL;
2536 	if (uap->timeout != NULL) {
2537 		error = copyin((const void *)uap->timeout, (void *)&ts32,
2538 		    sizeof(struct timespec32));
2539 		if (error != 0)
2540 			return (error);
2541 		ts.tv_sec = ts32.tv_sec;
2542 		ts.tv_nsec = ts32.tv_nsec;
2543 		tsp = &ts;
2544 	}
2545 	return (kern_thr_suspend(td, tsp));
2546 }
2547 
2548 void
siginfo_to_siginfo32(const siginfo_t * src,struct siginfo32 * dst)2549 siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst)
2550 {
2551 	bzero(dst, sizeof(*dst));
2552 	dst->si_signo = src->si_signo;
2553 	dst->si_errno = src->si_errno;
2554 	dst->si_code = src->si_code;
2555 	dst->si_pid = src->si_pid;
2556 	dst->si_uid = src->si_uid;
2557 	dst->si_status = src->si_status;
2558 	dst->si_addr = (uintptr_t)src->si_addr;
2559 	dst->si_value.sival_int = src->si_value.sival_int;
2560 	dst->si_timerid = src->si_timerid;
2561 	dst->si_overrun = src->si_overrun;
2562 }
2563 
2564 #ifndef _FREEBSD32_SYSPROTO_H_
2565 struct freebsd32_sigqueue_args {
2566         pid_t pid;
2567         int signum;
2568         /* union sigval32 */ int value;
2569 };
2570 #endif
2571 int
freebsd32_sigqueue(struct thread * td,struct freebsd32_sigqueue_args * uap)2572 freebsd32_sigqueue(struct thread *td, struct freebsd32_sigqueue_args *uap)
2573 {
2574 	union sigval sv;
2575 
2576 	/*
2577 	 * On 32-bit ABIs, sival_int and sival_ptr are the same.
2578 	 * On 64-bit little-endian ABIs, the low bits are the same.
2579 	 * In 64-bit big-endian ABIs, sival_int overlaps with
2580 	 * sival_ptr's HIGH bits.  We choose to support sival_int
2581 	 * rather than sival_ptr in this case as it seems to be
2582 	 * more common.
2583 	 */
2584 	bzero(&sv, sizeof(sv));
2585 	sv.sival_int = uap->value;
2586 
2587 	return (kern_sigqueue(td, uap->pid, uap->signum, &sv));
2588 }
2589 
2590 int
freebsd32_sigtimedwait(struct thread * td,struct freebsd32_sigtimedwait_args * uap)2591 freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
2592 {
2593 	struct timespec32 ts32;
2594 	struct timespec ts;
2595 	struct timespec *timeout;
2596 	sigset_t set;
2597 	ksiginfo_t ksi;
2598 	struct siginfo32 si32;
2599 	int error;
2600 
2601 	if (uap->timeout) {
2602 		error = copyin(uap->timeout, &ts32, sizeof(ts32));
2603 		if (error)
2604 			return (error);
2605 		ts.tv_sec = ts32.tv_sec;
2606 		ts.tv_nsec = ts32.tv_nsec;
2607 		timeout = &ts;
2608 	} else
2609 		timeout = NULL;
2610 
2611 	error = copyin(uap->set, &set, sizeof(set));
2612 	if (error)
2613 		return (error);
2614 
2615 	error = kern_sigtimedwait(td, set, &ksi, timeout);
2616 	if (error)
2617 		return (error);
2618 
2619 	if (uap->info) {
2620 		siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2621 		error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2622 	}
2623 
2624 	if (error == 0)
2625 		td->td_retval[0] = ksi.ksi_signo;
2626 	return (error);
2627 }
2628 
2629 /*
2630  * MPSAFE
2631  */
2632 int
freebsd32_sigwaitinfo(struct thread * td,struct freebsd32_sigwaitinfo_args * uap)2633 freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
2634 {
2635 	ksiginfo_t ksi;
2636 	struct siginfo32 si32;
2637 	sigset_t set;
2638 	int error;
2639 
2640 	error = copyin(uap->set, &set, sizeof(set));
2641 	if (error)
2642 		return (error);
2643 
2644 	error = kern_sigtimedwait(td, set, &ksi, NULL);
2645 	if (error)
2646 		return (error);
2647 
2648 	if (uap->info) {
2649 		siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2650 		error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2651 	}
2652 	if (error == 0)
2653 		td->td_retval[0] = ksi.ksi_signo;
2654 	return (error);
2655 }
2656 
2657 int
freebsd32_cpuset_setid(struct thread * td,struct freebsd32_cpuset_setid_args * uap)2658 freebsd32_cpuset_setid(struct thread *td,
2659     struct freebsd32_cpuset_setid_args *uap)
2660 {
2661 
2662 	return (kern_cpuset_setid(td, uap->which,
2663 	    PAIR32TO64(id_t, uap->id), uap->setid));
2664 }
2665 
2666 int
freebsd32_cpuset_getid(struct thread * td,struct freebsd32_cpuset_getid_args * uap)2667 freebsd32_cpuset_getid(struct thread *td,
2668     struct freebsd32_cpuset_getid_args *uap)
2669 {
2670 
2671 	return (kern_cpuset_getid(td, uap->level, uap->which,
2672 	    PAIR32TO64(id_t, uap->id), uap->setid));
2673 }
2674 
2675 int
freebsd32_cpuset_getaffinity(struct thread * td,struct freebsd32_cpuset_getaffinity_args * uap)2676 freebsd32_cpuset_getaffinity(struct thread *td,
2677     struct freebsd32_cpuset_getaffinity_args *uap)
2678 {
2679 
2680 	return (kern_cpuset_getaffinity(td, uap->level, uap->which,
2681 	    PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask));
2682 }
2683 
2684 int
freebsd32_cpuset_setaffinity(struct thread * td,struct freebsd32_cpuset_setaffinity_args * uap)2685 freebsd32_cpuset_setaffinity(struct thread *td,
2686     struct freebsd32_cpuset_setaffinity_args *uap)
2687 {
2688 
2689 	return (kern_cpuset_setaffinity(td, uap->level, uap->which,
2690 	    PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask));
2691 }
2692 
2693 int
freebsd32_nmount(struct thread * td,struct freebsd32_nmount_args * uap)2694 freebsd32_nmount(struct thread *td,
2695     struct freebsd32_nmount_args /* {
2696     	struct iovec *iovp;
2697     	unsigned int iovcnt;
2698     	int flags;
2699     } */ *uap)
2700 {
2701 	struct uio *auio;
2702 	uint64_t flags;
2703 	int error;
2704 
2705 	/*
2706 	 * Mount flags are now 64-bits. On 32-bit archtectures only
2707 	 * 32-bits are passed in, but from here on everything handles
2708 	 * 64-bit flags correctly.
2709 	 */
2710 	flags = uap->flags;
2711 
2712 	AUDIT_ARG_FFLAGS(flags);
2713 
2714 	/*
2715 	 * Filter out MNT_ROOTFS.  We do not want clients of nmount() in
2716 	 * userspace to set this flag, but we must filter it out if we want
2717 	 * MNT_UPDATE on the root file system to work.
2718 	 * MNT_ROOTFS should only be set by the kernel when mounting its
2719 	 * root file system.
2720 	 */
2721 	flags &= ~MNT_ROOTFS;
2722 
2723 	/*
2724 	 * check that we have an even number of iovec's
2725 	 * and that we have at least two options.
2726 	 */
2727 	if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
2728 		return (EINVAL);
2729 
2730 	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2731 	if (error)
2732 		return (error);
2733 	error = vfs_donmount(td, flags, auio);
2734 
2735 	free(auio, M_IOV);
2736 	return error;
2737 }
2738 
2739 #if 0
2740 int
2741 freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
2742 {
2743 	struct yyy32 *p32, s32;
2744 	struct yyy *p = NULL, s;
2745 	struct xxx_arg ap;
2746 	int error;
2747 
2748 	if (uap->zzz) {
2749 		error = copyin(uap->zzz, &s32, sizeof(s32));
2750 		if (error)
2751 			return (error);
2752 		/* translate in */
2753 		p = &s;
2754 	}
2755 	error = kern_xxx(td, p);
2756 	if (error)
2757 		return (error);
2758 	if (uap->zzz) {
2759 		/* translate out */
2760 		error = copyout(&s32, p32, sizeof(s32));
2761 	}
2762 	return (error);
2763 }
2764 #endif
2765 
2766 int
syscall32_register(int * offset,struct sysent * new_sysent,struct sysent * old_sysent,int flags)2767 syscall32_register(int *offset, struct sysent *new_sysent,
2768     struct sysent *old_sysent, int flags)
2769 {
2770 
2771 	if ((flags & ~SY_THR_STATIC) != 0)
2772 		return (EINVAL);
2773 
2774 	if (*offset == NO_SYSCALL) {
2775 		int i;
2776 
2777 		for (i = 1; i < SYS_MAXSYSCALL; ++i)
2778 			if (freebsd32_sysent[i].sy_call ==
2779 			    (sy_call_t *)lkmnosys)
2780 				break;
2781 		if (i == SYS_MAXSYSCALL)
2782 			return (ENFILE);
2783 		*offset = i;
2784 	} else if (*offset < 0 || *offset >= SYS_MAXSYSCALL)
2785 		return (EINVAL);
2786 	else if (freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmnosys &&
2787 	    freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmressys)
2788 		return (EEXIST);
2789 
2790 	*old_sysent = freebsd32_sysent[*offset];
2791 	freebsd32_sysent[*offset] = *new_sysent;
2792 	atomic_store_rel_32(&freebsd32_sysent[*offset].sy_thrcnt, flags);
2793 	return (0);
2794 }
2795 
2796 int
syscall32_deregister(int * offset,struct sysent * old_sysent)2797 syscall32_deregister(int *offset, struct sysent *old_sysent)
2798 {
2799 
2800 	if (*offset == 0)
2801 		return (0);
2802 
2803 	freebsd32_sysent[*offset] = *old_sysent;
2804 	return (0);
2805 }
2806 
2807 int
syscall32_module_handler(struct module * mod,int what,void * arg)2808 syscall32_module_handler(struct module *mod, int what, void *arg)
2809 {
2810 	struct syscall_module_data *data = (struct syscall_module_data*)arg;
2811 	modspecific_t ms;
2812 	int error;
2813 
2814 	switch (what) {
2815 	case MOD_LOAD:
2816 		error = syscall32_register(data->offset, data->new_sysent,
2817 		    &data->old_sysent, SY_THR_STATIC_KLD);
2818 		if (error) {
2819 			/* Leave a mark so we know to safely unload below. */
2820 			data->offset = NULL;
2821 			return error;
2822 		}
2823 		ms.intval = *data->offset;
2824 		MOD_XLOCK;
2825 		module_setspecific(mod, &ms);
2826 		MOD_XUNLOCK;
2827 		if (data->chainevh)
2828 			error = data->chainevh(mod, what, data->chainarg);
2829 		return (error);
2830 	case MOD_UNLOAD:
2831 		/*
2832 		 * MOD_LOAD failed, so just return without calling the
2833 		 * chained handler since we didn't pass along the MOD_LOAD
2834 		 * event.
2835 		 */
2836 		if (data->offset == NULL)
2837 			return (0);
2838 		if (data->chainevh) {
2839 			error = data->chainevh(mod, what, data->chainarg);
2840 			if (error)
2841 				return (error);
2842 		}
2843 		error = syscall32_deregister(data->offset, &data->old_sysent);
2844 		return (error);
2845 	default:
2846 		error = EOPNOTSUPP;
2847 		if (data->chainevh)
2848 			error = data->chainevh(mod, what, data->chainarg);
2849 		return (error);
2850 	}
2851 }
2852 
2853 int
syscall32_helper_register(struct syscall_helper_data * sd,int flags)2854 syscall32_helper_register(struct syscall_helper_data *sd, int flags)
2855 {
2856 	struct syscall_helper_data *sd1;
2857 	int error;
2858 
2859 	for (sd1 = sd; sd1->syscall_no != NO_SYSCALL; sd1++) {
2860 		error = syscall32_register(&sd1->syscall_no, &sd1->new_sysent,
2861 		    &sd1->old_sysent, flags);
2862 		if (error != 0) {
2863 			syscall32_helper_unregister(sd);
2864 			return (error);
2865 		}
2866 		sd1->registered = 1;
2867 	}
2868 	return (0);
2869 }
2870 
2871 int
syscall32_helper_unregister(struct syscall_helper_data * sd)2872 syscall32_helper_unregister(struct syscall_helper_data *sd)
2873 {
2874 	struct syscall_helper_data *sd1;
2875 
2876 	for (sd1 = sd; sd1->registered != 0; sd1++) {
2877 		syscall32_deregister(&sd1->syscall_no, &sd1->old_sysent);
2878 		sd1->registered = 0;
2879 	}
2880 	return (0);
2881 }
2882 
2883 register_t *
freebsd32_copyout_strings(struct image_params * imgp)2884 freebsd32_copyout_strings(struct image_params *imgp)
2885 {
2886 	int argc, envc, i;
2887 	u_int32_t *vectp;
2888 	char *stringp;
2889 	uintptr_t destp;
2890 	u_int32_t *stack_base;
2891 	struct freebsd32_ps_strings *arginfo;
2892 	char canary[sizeof(long) * 8];
2893 	int32_t pagesizes32[MAXPAGESIZES];
2894 	size_t execpath_len;
2895 	int szsigcode;
2896 
2897 	/*
2898 	 * Calculate string base and vector table pointers.
2899 	 * Also deal with signal trampoline code for this exec type.
2900 	 */
2901 	if (imgp->execpath != NULL && imgp->auxargs != NULL)
2902 		execpath_len = strlen(imgp->execpath) + 1;
2903 	else
2904 		execpath_len = 0;
2905 	arginfo = (struct freebsd32_ps_strings *)curproc->p_sysent->
2906 	    sv_psstrings;
2907 	if (imgp->proc->p_sysent->sv_sigcode_base == 0)
2908 		szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
2909 	else
2910 		szsigcode = 0;
2911 	destp =	(uintptr_t)arginfo;
2912 
2913 	/*
2914 	 * install sigcode
2915 	 */
2916 	if (szsigcode != 0) {
2917 		destp -= szsigcode;
2918 		destp = rounddown2(destp, sizeof(uint32_t));
2919 		copyout(imgp->proc->p_sysent->sv_sigcode, (void *)destp,
2920 		    szsigcode);
2921 	}
2922 
2923 	/*
2924 	 * Copy the image path for the rtld.
2925 	 */
2926 	if (execpath_len != 0) {
2927 		destp -= execpath_len;
2928 		imgp->execpathp = destp;
2929 		copyout(imgp->execpath, (void *)destp, execpath_len);
2930 	}
2931 
2932 	/*
2933 	 * Prepare the canary for SSP.
2934 	 */
2935 	arc4rand(canary, sizeof(canary), 0);
2936 	destp -= sizeof(canary);
2937 	imgp->canary = destp;
2938 	copyout(canary, (void *)destp, sizeof(canary));
2939 	imgp->canarylen = sizeof(canary);
2940 
2941 	/*
2942 	 * Prepare the pagesizes array.
2943 	 */
2944 	for (i = 0; i < MAXPAGESIZES; i++)
2945 		pagesizes32[i] = (uint32_t)pagesizes[i];
2946 	destp -= sizeof(pagesizes32);
2947 	destp = rounddown2(destp, sizeof(uint32_t));
2948 	imgp->pagesizes = destp;
2949 	copyout(pagesizes32, (void *)destp, sizeof(pagesizes32));
2950 	imgp->pagesizeslen = sizeof(pagesizes32);
2951 
2952 	destp -= ARG_MAX - imgp->args->stringspace;
2953 	destp = rounddown2(destp, sizeof(uint32_t));
2954 
2955 	vectp = (uint32_t *)destp;
2956 	if (imgp->auxargs) {
2957 		/*
2958 		 * Allocate room on the stack for the ELF auxargs
2959 		 * array.  It has up to AT_COUNT entries.
2960 		 */
2961 		vectp -= howmany(AT_COUNT * sizeof(Elf32_Auxinfo),
2962 		    sizeof(*vectp));
2963 	}
2964 
2965 	/*
2966 	 * Allocate room for the argv[] and env vectors including the
2967 	 * terminating NULL pointers.
2968 	 */
2969 	vectp -= imgp->args->argc + 1 + imgp->args->envc + 1;
2970 
2971 	/*
2972 	 * vectp also becomes our initial stack base
2973 	 */
2974 	stack_base = vectp;
2975 
2976 	stringp = imgp->args->begin_argv;
2977 	argc = imgp->args->argc;
2978 	envc = imgp->args->envc;
2979 	/*
2980 	 * Copy out strings - arguments and environment.
2981 	 */
2982 	copyout(stringp, (void *)destp, ARG_MAX - imgp->args->stringspace);
2983 
2984 	/*
2985 	 * Fill in "ps_strings" struct for ps, w, etc.
2986 	 */
2987 	suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp);
2988 	suword32(&arginfo->ps_nargvstr, argc);
2989 
2990 	/*
2991 	 * Fill in argument portion of vector table.
2992 	 */
2993 	for (; argc > 0; --argc) {
2994 		suword32(vectp++, (u_int32_t)(intptr_t)destp);
2995 		while (*stringp++ != 0)
2996 			destp++;
2997 		destp++;
2998 	}
2999 
3000 	/* a null vector table pointer separates the argp's from the envp's */
3001 	suword32(vectp++, 0);
3002 
3003 	suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp);
3004 	suword32(&arginfo->ps_nenvstr, envc);
3005 
3006 	/*
3007 	 * Fill in environment portion of vector table.
3008 	 */
3009 	for (; envc > 0; --envc) {
3010 		suword32(vectp++, (u_int32_t)(intptr_t)destp);
3011 		while (*stringp++ != 0)
3012 			destp++;
3013 		destp++;
3014 	}
3015 
3016 	/* end of vector table is a null pointer */
3017 	suword32(vectp, 0);
3018 
3019 	return ((register_t *)stack_base);
3020 }
3021 
3022 int
freebsd32_kldstat(struct thread * td,struct freebsd32_kldstat_args * uap)3023 freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
3024 {
3025 	struct kld_file_stat *stat;
3026 	struct kld32_file_stat *stat32;
3027 	int error, version;
3028 
3029 	if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
3030 	    != 0)
3031 		return (error);
3032 	if (version != sizeof(struct kld32_file_stat_1) &&
3033 	    version != sizeof(struct kld32_file_stat))
3034 		return (EINVAL);
3035 
3036 	stat = malloc(sizeof(*stat), M_TEMP, M_WAITOK | M_ZERO);
3037 	stat32 = malloc(sizeof(*stat32), M_TEMP, M_WAITOK | M_ZERO);
3038 	error = kern_kldstat(td, uap->fileid, stat);
3039 	if (error == 0) {
3040 		bcopy(&stat->name[0], &stat32->name[0], sizeof(stat->name));
3041 		CP(*stat, *stat32, refs);
3042 		CP(*stat, *stat32, id);
3043 		PTROUT_CP(*stat, *stat32, address);
3044 		CP(*stat, *stat32, size);
3045 		bcopy(&stat->pathname[0], &stat32->pathname[0],
3046 		    sizeof(stat->pathname));
3047 		stat32->version  = version;
3048 		error = copyout(stat32, uap->stat, version);
3049 	}
3050 	free(stat, M_TEMP);
3051 	free(stat32, M_TEMP);
3052 	return (error);
3053 }
3054 
3055 int
freebsd32_posix_fallocate(struct thread * td,struct freebsd32_posix_fallocate_args * uap)3056 freebsd32_posix_fallocate(struct thread *td,
3057     struct freebsd32_posix_fallocate_args *uap)
3058 {
3059 	int error;
3060 
3061 	error = kern_posix_fallocate(td, uap->fd,
3062 	    PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len));
3063 	return (kern_posix_error(td, error));
3064 }
3065 
3066 int
freebsd32_posix_fadvise(struct thread * td,struct freebsd32_posix_fadvise_args * uap)3067 freebsd32_posix_fadvise(struct thread *td,
3068     struct freebsd32_posix_fadvise_args *uap)
3069 {
3070 	int error;
3071 
3072 	error = kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset),
3073 	    PAIR32TO64(off_t, uap->len), uap->advice);
3074 	return (kern_posix_error(td, error));
3075 }
3076 
3077 int
convert_sigevent32(struct sigevent32 * sig32,struct sigevent * sig)3078 convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig)
3079 {
3080 
3081 	CP(*sig32, *sig, sigev_notify);
3082 	switch (sig->sigev_notify) {
3083 	case SIGEV_NONE:
3084 		break;
3085 	case SIGEV_THREAD_ID:
3086 		CP(*sig32, *sig, sigev_notify_thread_id);
3087 		/* FALLTHROUGH */
3088 	case SIGEV_SIGNAL:
3089 		CP(*sig32, *sig, sigev_signo);
3090 		PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3091 		break;
3092 	case SIGEV_KEVENT:
3093 		CP(*sig32, *sig, sigev_notify_kqueue);
3094 		CP(*sig32, *sig, sigev_notify_kevent_flags);
3095 		PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3096 		break;
3097 	default:
3098 		return (EINVAL);
3099 	}
3100 	return (0);
3101 }
3102 
3103 int
freebsd32_procctl(struct thread * td,struct freebsd32_procctl_args * uap)3104 freebsd32_procctl(struct thread *td, struct freebsd32_procctl_args *uap)
3105 {
3106 	void *data;
3107 	union {
3108 		struct procctl_reaper_status rs;
3109 		struct procctl_reaper_pids rp;
3110 		struct procctl_reaper_kill rk;
3111 	} x;
3112 	union {
3113 		struct procctl_reaper_pids32 rp;
3114 	} x32;
3115 	int error, error1, flags, signum;
3116 
3117 	switch (uap->com) {
3118 	case PROC_SPROTECT:
3119 	case PROC_STACKGAP_CTL:
3120 	case PROC_TRACE_CTL:
3121 	case PROC_TRAPCAP_CTL:
3122 		error = copyin(PTRIN(uap->data), &flags, sizeof(flags));
3123 		if (error != 0)
3124 			return (error);
3125 		data = &flags;
3126 		break;
3127 	case PROC_REAP_ACQUIRE:
3128 	case PROC_REAP_RELEASE:
3129 		if (uap->data != NULL)
3130 			return (EINVAL);
3131 		data = NULL;
3132 		break;
3133 	case PROC_REAP_STATUS:
3134 		data = &x.rs;
3135 		break;
3136 	case PROC_REAP_GETPIDS:
3137 		error = copyin(uap->data, &x32.rp, sizeof(x32.rp));
3138 		if (error != 0)
3139 			return (error);
3140 		CP(x32.rp, x.rp, rp_count);
3141 		PTRIN_CP(x32.rp, x.rp, rp_pids);
3142 		data = &x.rp;
3143 		break;
3144 	case PROC_REAP_KILL:
3145 		error = copyin(uap->data, &x.rk, sizeof(x.rk));
3146 		if (error != 0)
3147 			return (error);
3148 		data = &x.rk;
3149 		break;
3150 	case PROC_STACKGAP_STATUS:
3151 	case PROC_TRACE_STATUS:
3152 	case PROC_TRAPCAP_STATUS:
3153 		data = &flags;
3154 		break;
3155 	case PROC_PDEATHSIG_CTL:
3156 		error = copyin(uap->data, &signum, sizeof(signum));
3157 		if (error != 0)
3158 			return (error);
3159 		data = &signum;
3160 		break;
3161 	case PROC_PDEATHSIG_STATUS:
3162 		data = &signum;
3163 		break;
3164 	default:
3165 		return (EINVAL);
3166 	}
3167 	error = kern_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id),
3168 	    uap->com, data);
3169 	switch (uap->com) {
3170 	case PROC_REAP_STATUS:
3171 		if (error == 0)
3172 			error = copyout(&x.rs, uap->data, sizeof(x.rs));
3173 		break;
3174 	case PROC_REAP_KILL:
3175 		error1 = copyout(&x.rk, uap->data, sizeof(x.rk));
3176 		if (error == 0)
3177 			error = error1;
3178 		break;
3179 	case PROC_STACKGAP_STATUS:
3180 	case PROC_TRACE_STATUS:
3181 	case PROC_TRAPCAP_STATUS:
3182 		if (error == 0)
3183 			error = copyout(&flags, uap->data, sizeof(flags));
3184 		break;
3185 	case PROC_PDEATHSIG_STATUS:
3186 		if (error == 0)
3187 			error = copyout(&signum, uap->data, sizeof(signum));
3188 		break;
3189 	}
3190 	return (error);
3191 }
3192 
3193 int
freebsd32_fcntl(struct thread * td,struct freebsd32_fcntl_args * uap)3194 freebsd32_fcntl(struct thread *td, struct freebsd32_fcntl_args *uap)
3195 {
3196 	long tmp;
3197 
3198 	switch (uap->cmd) {
3199 	/*
3200 	 * Do unsigned conversion for arg when operation
3201 	 * interprets it as flags or pointer.
3202 	 */
3203 	case F_SETLK_REMOTE:
3204 	case F_SETLKW:
3205 	case F_SETLK:
3206 	case F_GETLK:
3207 	case F_SETFD:
3208 	case F_SETFL:
3209 	case F_OGETLK:
3210 	case F_OSETLK:
3211 	case F_OSETLKW:
3212 		tmp = (unsigned int)(uap->arg);
3213 		break;
3214 	default:
3215 		tmp = uap->arg;
3216 		break;
3217 	}
3218 	return (kern_fcntl_freebsd(td, uap->fd, uap->cmd, tmp));
3219 }
3220 
3221 int
freebsd32_ppoll(struct thread * td,struct freebsd32_ppoll_args * uap)3222 freebsd32_ppoll(struct thread *td, struct freebsd32_ppoll_args *uap)
3223 {
3224 	struct timespec32 ts32;
3225 	struct timespec ts, *tsp;
3226 	sigset_t set, *ssp;
3227 	int error;
3228 
3229 	if (uap->ts != NULL) {
3230 		error = copyin(uap->ts, &ts32, sizeof(ts32));
3231 		if (error != 0)
3232 			return (error);
3233 		CP(ts32, ts, tv_sec);
3234 		CP(ts32, ts, tv_nsec);
3235 		tsp = &ts;
3236 	} else
3237 		tsp = NULL;
3238 	if (uap->set != NULL) {
3239 		error = copyin(uap->set, &set, sizeof(set));
3240 		if (error != 0)
3241 			return (error);
3242 		ssp = &set;
3243 	} else
3244 		ssp = NULL;
3245 
3246 	return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp));
3247 }
3248 
3249 int
freebsd32_sched_rr_get_interval(struct thread * td,struct freebsd32_sched_rr_get_interval_args * uap)3250 freebsd32_sched_rr_get_interval(struct thread *td,
3251     struct freebsd32_sched_rr_get_interval_args *uap)
3252 {
3253 	struct timespec ts;
3254 	struct timespec32 ts32;
3255 	int error;
3256 
3257 	error = kern_sched_rr_get_interval(td, uap->pid, &ts);
3258 	if (error == 0) {
3259 		CP(ts, ts32, tv_sec);
3260 		CP(ts, ts32, tv_nsec);
3261 		error = copyout(&ts32, uap->interval, sizeof(ts32));
3262 	}
3263 	return (error);
3264 }
3265