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