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