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