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