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