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