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