]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/freebsd32/freebsd32_misc.c
Fix some bugs found while fixing the representation and translation
[FreeBSD/FreeBSD.git] / sys / compat / freebsd32 / freebsd32_misc.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2002 Doug Rabson
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include "opt_inet.h"
33 #include "opt_inet6.h"
34 #include "opt_ktrace.h"
35
36 #define __ELF_WORD_SIZE 32
37
38 #ifdef COMPAT_FREEBSD11
39 #define _WANT_FREEBSD11_KEVENT
40 #endif
41
42 #include <sys/param.h>
43 #include <sys/bus.h>
44 #include <sys/capsicum.h>
45 #include <sys/clock.h>
46 #include <sys/exec.h>
47 #include <sys/fcntl.h>
48 #include <sys/filedesc.h>
49 #include <sys/imgact.h>
50 #include <sys/jail.h>
51 #include <sys/kernel.h>
52 #include <sys/limits.h>
53 #include <sys/linker.h>
54 #include <sys/lock.h>
55 #include <sys/malloc.h>
56 #include <sys/file.h>           /* Must come after sys/malloc.h */
57 #include <sys/imgact.h>
58 #include <sys/mbuf.h>
59 #include <sys/mman.h>
60 #include <sys/module.h>
61 #include <sys/mount.h>
62 #include <sys/mutex.h>
63 #include <sys/namei.h>
64 #include <sys/proc.h>
65 #include <sys/procctl.h>
66 #include <sys/reboot.h>
67 #include <sys/resource.h>
68 #include <sys/resourcevar.h>
69 #include <sys/selinfo.h>
70 #include <sys/eventvar.h>       /* Must come after sys/selinfo.h */
71 #include <sys/pipe.h>           /* Must come after sys/selinfo.h */
72 #include <sys/signal.h>
73 #include <sys/signalvar.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #include <sys/stat.h>
77 #include <sys/syscall.h>
78 #include <sys/syscallsubr.h>
79 #include <sys/sysctl.h>
80 #include <sys/sysent.h>
81 #include <sys/sysproto.h>
82 #include <sys/systm.h>
83 #include <sys/thr.h>
84 #include <sys/unistd.h>
85 #include <sys/ucontext.h>
86 #include <sys/vnode.h>
87 #include <sys/wait.h>
88 #include <sys/ipc.h>
89 #include <sys/msg.h>
90 #include <sys/sem.h>
91 #include <sys/shm.h>
92 #ifdef KTRACE
93 #include <sys/ktrace.h>
94 #endif
95
96 #ifdef INET
97 #include <netinet/in.h>
98 #endif
99
100 #include <vm/vm.h>
101 #include <vm/vm_param.h>
102 #include <vm/pmap.h>
103 #include <vm/vm_map.h>
104 #include <vm/vm_object.h>
105 #include <vm/vm_extern.h>
106
107 #include <machine/cpu.h>
108 #include <machine/elf.h>
109
110 #include <security/audit/audit.h>
111
112 #include <compat/freebsd32/freebsd32_util.h>
113 #include <compat/freebsd32/freebsd32.h>
114 #include <compat/freebsd32/freebsd32_ipc.h>
115 #include <compat/freebsd32/freebsd32_misc.h>
116 #include <compat/freebsd32/freebsd32_signal.h>
117 #include <compat/freebsd32/freebsd32_proto.h>
118
119 FEATURE(compat_freebsd_32bit, "Compatible with 32-bit FreeBSD");
120
121 #ifdef __amd64__
122 CTASSERT(sizeof(struct timeval32) == 8);
123 CTASSERT(sizeof(struct timespec32) == 8);
124 CTASSERT(sizeof(struct itimerval32) == 16);
125 CTASSERT(sizeof(struct bintime32) == 12);
126 #endif
127 CTASSERT(sizeof(struct statfs32) == 256);
128 #ifdef __amd64__
129 CTASSERT(sizeof(struct rusage32) == 72);
130 #endif
131 CTASSERT(sizeof(struct sigaltstack32) == 12);
132 #ifdef __amd64__
133 CTASSERT(sizeof(struct kevent32) == 56);
134 #else
135 CTASSERT(sizeof(struct kevent32) == 64);
136 #endif
137 CTASSERT(sizeof(struct iovec32) == 8);
138 CTASSERT(sizeof(struct msghdr32) == 28);
139 #ifdef __amd64__
140 CTASSERT(sizeof(struct stat32) == 208);
141 CTASSERT(sizeof(struct freebsd11_stat32) == 96);
142 #endif
143 CTASSERT(sizeof(struct sigaction32) == 24);
144
145 static int freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count);
146 static int freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count);
147 static int freebsd32_user_clock_nanosleep(struct thread *td, clockid_t clock_id,
148     int flags, const struct timespec32 *ua_rqtp, struct timespec32 *ua_rmtp);
149
150 void
151 freebsd32_rusage_out(const struct rusage *s, struct rusage32 *s32)
152 {
153
154         TV_CP(*s, *s32, ru_utime);
155         TV_CP(*s, *s32, ru_stime);
156         CP(*s, *s32, ru_maxrss);
157         CP(*s, *s32, ru_ixrss);
158         CP(*s, *s32, ru_idrss);
159         CP(*s, *s32, ru_isrss);
160         CP(*s, *s32, ru_minflt);
161         CP(*s, *s32, ru_majflt);
162         CP(*s, *s32, ru_nswap);
163         CP(*s, *s32, ru_inblock);
164         CP(*s, *s32, ru_oublock);
165         CP(*s, *s32, ru_msgsnd);
166         CP(*s, *s32, ru_msgrcv);
167         CP(*s, *s32, ru_nsignals);
168         CP(*s, *s32, ru_nvcsw);
169         CP(*s, *s32, ru_nivcsw);
170 }
171
172 int
173 freebsd32_wait4(struct thread *td, struct freebsd32_wait4_args *uap)
174 {
175         int error, status;
176         struct rusage32 ru32;
177         struct rusage ru, *rup;
178
179         if (uap->rusage != NULL)
180                 rup = &ru;
181         else
182                 rup = NULL;
183         error = kern_wait(td, uap->pid, &status, uap->options, rup);
184         if (error)
185                 return (error);
186         if (uap->status != NULL)
187                 error = copyout(&status, uap->status, sizeof(status));
188         if (uap->rusage != NULL && error == 0) {
189                 freebsd32_rusage_out(&ru, &ru32);
190                 error = copyout(&ru32, uap->rusage, sizeof(ru32));
191         }
192         return (error);
193 }
194
195 int
196 freebsd32_wait6(struct thread *td, struct freebsd32_wait6_args *uap)
197 {
198         struct wrusage32 wru32;
199         struct __wrusage wru, *wrup;
200         struct siginfo32 si32;
201         struct __siginfo si, *sip;
202         int error, status;
203
204         if (uap->wrusage != NULL)
205                 wrup = &wru;
206         else
207                 wrup = NULL;
208         if (uap->info != NULL) {
209                 sip = &si;
210                 bzero(sip, sizeof(*sip));
211         } else
212                 sip = NULL;
213         error = kern_wait6(td, uap->idtype, PAIR32TO64(id_t, uap->id),
214             &status, uap->options, wrup, sip);
215         if (error != 0)
216                 return (error);
217         if (uap->status != NULL)
218                 error = copyout(&status, uap->status, sizeof(status));
219         if (uap->wrusage != NULL && error == 0) {
220                 freebsd32_rusage_out(&wru.wru_self, &wru32.wru_self);
221                 freebsd32_rusage_out(&wru.wru_children, &wru32.wru_children);
222                 error = copyout(&wru32, uap->wrusage, sizeof(wru32));
223         }
224         if (uap->info != NULL && error == 0) {
225                 siginfo_to_siginfo32 (&si, &si32);
226                 error = copyout(&si32, uap->info, sizeof(si32));
227         }
228         return (error);
229 }
230
231 #ifdef COMPAT_FREEBSD4
232 static void
233 copy_statfs(struct statfs *in, struct statfs32 *out)
234 {
235
236         statfs_scale_blocks(in, INT32_MAX);
237         bzero(out, sizeof(*out));
238         CP(*in, *out, f_bsize);
239         out->f_iosize = MIN(in->f_iosize, INT32_MAX);
240         CP(*in, *out, f_blocks);
241         CP(*in, *out, f_bfree);
242         CP(*in, *out, f_bavail);
243         out->f_files = MIN(in->f_files, INT32_MAX);
244         out->f_ffree = MIN(in->f_ffree, INT32_MAX);
245         CP(*in, *out, f_fsid);
246         CP(*in, *out, f_owner);
247         CP(*in, *out, f_type);
248         CP(*in, *out, f_flags);
249         out->f_syncwrites = MIN(in->f_syncwrites, INT32_MAX);
250         out->f_asyncwrites = MIN(in->f_asyncwrites, INT32_MAX);
251         strlcpy(out->f_fstypename,
252               in->f_fstypename, MFSNAMELEN);
253         strlcpy(out->f_mntonname,
254               in->f_mntonname, min(MNAMELEN, FREEBSD4_MNAMELEN));
255         out->f_syncreads = MIN(in->f_syncreads, INT32_MAX);
256         out->f_asyncreads = MIN(in->f_asyncreads, INT32_MAX);
257         strlcpy(out->f_mntfromname,
258               in->f_mntfromname, min(MNAMELEN, FREEBSD4_MNAMELEN));
259 }
260 #endif
261
262 #ifdef COMPAT_FREEBSD4
263 int
264 freebsd4_freebsd32_getfsstat(struct thread *td,
265     struct freebsd4_freebsd32_getfsstat_args *uap)
266 {
267         struct statfs *buf, *sp;
268         struct statfs32 stat32;
269         size_t count, size, copycount;
270         int error;
271
272         count = uap->bufsize / sizeof(struct statfs32);
273         size = count * sizeof(struct statfs);
274         error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE, uap->mode);
275         if (size > 0) {
276                 sp = buf;
277                 copycount = count;
278                 while (copycount > 0 && error == 0) {
279                         copy_statfs(sp, &stat32);
280                         error = copyout(&stat32, uap->buf, sizeof(stat32));
281                         sp++;
282                         uap->buf++;
283                         copycount--;
284                 }
285                 free(buf, M_STATFS);
286         }
287         if (error == 0)
288                 td->td_retval[0] = count;
289         return (error);
290 }
291 #endif
292
293 #ifdef COMPAT_FREEBSD10
294 int
295 freebsd10_freebsd32_pipe(struct thread *td,
296     struct freebsd10_freebsd32_pipe_args *uap) {
297         
298         return (freebsd10_pipe(td, (struct freebsd10_pipe_args*)uap));
299 }
300 #endif
301
302 int
303 freebsd32_sigaltstack(struct thread *td,
304                       struct freebsd32_sigaltstack_args *uap)
305 {
306         struct sigaltstack32 s32;
307         struct sigaltstack ss, oss, *ssp;
308         int error;
309
310         if (uap->ss != NULL) {
311                 error = copyin(uap->ss, &s32, sizeof(s32));
312                 if (error)
313                         return (error);
314                 PTRIN_CP(s32, ss, ss_sp);
315                 CP(s32, ss, ss_size);
316                 CP(s32, ss, ss_flags);
317                 ssp = &ss;
318         } else
319                 ssp = NULL;
320         error = kern_sigaltstack(td, ssp, &oss);
321         if (error == 0 && uap->oss != NULL) {
322                 PTROUT_CP(oss, s32, ss_sp);
323                 CP(oss, s32, ss_size);
324                 CP(oss, s32, ss_flags);
325                 error = copyout(&s32, uap->oss, sizeof(s32));
326         }
327         return (error);
328 }
329
330 /*
331  * Custom version of exec_copyin_args() so that we can translate
332  * the pointers.
333  */
334 int
335 freebsd32_exec_copyin_args(struct image_args *args, char *fname,
336     enum uio_seg segflg, u_int32_t *argv, u_int32_t *envv)
337 {
338         char *argp, *envp;
339         u_int32_t *p32, arg;
340         size_t length;
341         int error;
342
343         bzero(args, sizeof(*args));
344         if (argv == NULL)
345                 return (EFAULT);
346
347         /*
348          * Allocate demand-paged memory for the file name, argument, and
349          * environment strings.
350          */
351         error = exec_alloc_args(args);
352         if (error != 0)
353                 return (error);
354
355         /*
356          * Copy the file name.
357          */
358         if (fname != NULL) {
359                 args->fname = args->buf;
360                 error = (segflg == UIO_SYSSPACE) ?
361                     copystr(fname, args->fname, PATH_MAX, &length) :
362                     copyinstr(fname, args->fname, PATH_MAX, &length);
363                 if (error != 0)
364                         goto err_exit;
365         } else
366                 length = 0;
367
368         args->begin_argv = args->buf + length;
369         args->endp = args->begin_argv;
370         args->stringspace = ARG_MAX;
371
372         /*
373          * extract arguments first
374          */
375         p32 = argv;
376         for (;;) {
377                 error = copyin(p32++, &arg, sizeof(arg));
378                 if (error)
379                         goto err_exit;
380                 if (arg == 0)
381                         break;
382                 argp = PTRIN(arg);
383                 error = copyinstr(argp, args->endp, args->stringspace, &length);
384                 if (error) {
385                         if (error == ENAMETOOLONG)
386                                 error = E2BIG;
387                         goto err_exit;
388                 }
389                 args->stringspace -= length;
390                 args->endp += length;
391                 args->argc++;
392         }
393                         
394         args->begin_envv = args->endp;
395
396         /*
397          * extract environment strings
398          */
399         if (envv) {
400                 p32 = envv;
401                 for (;;) {
402                         error = copyin(p32++, &arg, sizeof(arg));
403                         if (error)
404                                 goto err_exit;
405                         if (arg == 0)
406                                 break;
407                         envp = PTRIN(arg);
408                         error = copyinstr(envp, args->endp, args->stringspace,
409                             &length);
410                         if (error) {
411                                 if (error == ENAMETOOLONG)
412                                         error = E2BIG;
413                                 goto err_exit;
414                         }
415                         args->stringspace -= length;
416                         args->endp += length;
417                         args->envc++;
418                 }
419         }
420
421         return (0);
422
423 err_exit:
424         exec_free_args(args);
425         return (error);
426 }
427
428 int
429 freebsd32_execve(struct thread *td, struct freebsd32_execve_args *uap)
430 {
431         struct image_args eargs;
432         struct vmspace *oldvmspace;
433         int error;
434
435         error = pre_execve(td, &oldvmspace);
436         if (error != 0)
437                 return (error);
438         error = freebsd32_exec_copyin_args(&eargs, uap->fname, UIO_USERSPACE,
439             uap->argv, uap->envv);
440         if (error == 0)
441                 error = kern_execve(td, &eargs, NULL);
442         post_execve(td, error, oldvmspace);
443         return (error);
444 }
445
446 int
447 freebsd32_fexecve(struct thread *td, struct freebsd32_fexecve_args *uap)
448 {
449         struct image_args eargs;
450         struct vmspace *oldvmspace;
451         int error;
452
453         error = pre_execve(td, &oldvmspace);
454         if (error != 0)
455                 return (error);
456         error = freebsd32_exec_copyin_args(&eargs, NULL, UIO_SYSSPACE,
457             uap->argv, uap->envv);
458         if (error == 0) {
459                 eargs.fd = uap->fd;
460                 error = kern_execve(td, &eargs, NULL);
461         }
462         post_execve(td, error, oldvmspace);
463         return (error);
464 }
465
466 #if defined(COMPAT_FREEBSD11)
467 int
468 freebsd11_freebsd32_mknod(struct thread *td,
469     struct freebsd11_freebsd32_mknod_args *uap)
470 {
471
472         return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->mode,
473             uap->dev));
474 }
475
476 int
477 freebsd11_freebsd32_mknodat(struct thread *td,
478     struct freebsd11_freebsd32_mknodat_args *uap)
479 {
480
481         return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
482             uap->dev));
483 }
484 #endif /* COMPAT_FREEBSD11 */
485
486 int
487 freebsd32_mprotect(struct thread *td, struct freebsd32_mprotect_args *uap)
488 {
489         int prot;
490
491         prot = uap->prot;
492 #if defined(__amd64__)
493         if (i386_read_exec && (prot & PROT_READ) != 0)
494                 prot |= PROT_EXEC;
495 #endif
496         return (kern_mprotect(td, (uintptr_t)PTRIN(uap->addr), uap->len,
497             prot));
498 }
499
500 int
501 freebsd32_mmap(struct thread *td, struct freebsd32_mmap_args *uap)
502 {
503         int prot;
504
505         prot = uap->prot;
506 #if defined(__amd64__)
507         if (i386_read_exec && (prot & PROT_READ))
508                 prot |= PROT_EXEC;
509 #endif
510
511         return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, prot,
512             uap->flags, uap->fd, PAIR32TO64(off_t, uap->pos)));
513 }
514
515 #ifdef COMPAT_FREEBSD6
516 int
517 freebsd6_freebsd32_mmap(struct thread *td,
518     struct freebsd6_freebsd32_mmap_args *uap)
519 {
520         int prot;
521
522         prot = uap->prot;
523 #if defined(__amd64__)
524         if (i386_read_exec && (prot & PROT_READ))
525                 prot |= PROT_EXEC;
526 #endif
527
528         return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, prot,
529             uap->flags, uap->fd, PAIR32TO64(off_t, uap->pos)));
530 }
531 #endif
532
533 int
534 freebsd32_setitimer(struct thread *td, struct freebsd32_setitimer_args *uap)
535 {
536         struct itimerval itv, oitv, *itvp;      
537         struct itimerval32 i32;
538         int error;
539
540         if (uap->itv != NULL) {
541                 error = copyin(uap->itv, &i32, sizeof(i32));
542                 if (error)
543                         return (error);
544                 TV_CP(i32, itv, it_interval);
545                 TV_CP(i32, itv, it_value);
546                 itvp = &itv;
547         } else
548                 itvp = NULL;
549         error = kern_setitimer(td, uap->which, itvp, &oitv);
550         if (error || uap->oitv == NULL)
551                 return (error);
552         TV_CP(oitv, i32, it_interval);
553         TV_CP(oitv, i32, it_value);
554         return (copyout(&i32, uap->oitv, sizeof(i32)));
555 }
556
557 int
558 freebsd32_getitimer(struct thread *td, struct freebsd32_getitimer_args *uap)
559 {
560         struct itimerval itv;
561         struct itimerval32 i32;
562         int error;
563
564         error = kern_getitimer(td, uap->which, &itv);
565         if (error || uap->itv == NULL)
566                 return (error);
567         TV_CP(itv, i32, it_interval);
568         TV_CP(itv, i32, it_value);
569         return (copyout(&i32, uap->itv, sizeof(i32)));
570 }
571
572 int
573 freebsd32_select(struct thread *td, struct freebsd32_select_args *uap)
574 {
575         struct timeval32 tv32;
576         struct timeval tv, *tvp;
577         int error;
578
579         if (uap->tv != NULL) {
580                 error = copyin(uap->tv, &tv32, sizeof(tv32));
581                 if (error)
582                         return (error);
583                 CP(tv32, tv, tv_sec);
584                 CP(tv32, tv, tv_usec);
585                 tvp = &tv;
586         } else
587                 tvp = NULL;
588         /*
589          * XXX Do pointers need PTRIN()?
590          */
591         return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
592             sizeof(int32_t) * 8));
593 }
594
595 int
596 freebsd32_pselect(struct thread *td, struct freebsd32_pselect_args *uap)
597 {
598         struct timespec32 ts32;
599         struct timespec ts;
600         struct timeval tv, *tvp;
601         sigset_t set, *uset;
602         int error;
603
604         if (uap->ts != NULL) {
605                 error = copyin(uap->ts, &ts32, sizeof(ts32));
606                 if (error != 0)
607                         return (error);
608                 CP(ts32, ts, tv_sec);
609                 CP(ts32, ts, tv_nsec);
610                 TIMESPEC_TO_TIMEVAL(&tv, &ts);
611                 tvp = &tv;
612         } else
613                 tvp = NULL;
614         if (uap->sm != NULL) {
615                 error = copyin(uap->sm, &set, sizeof(set));
616                 if (error != 0)
617                         return (error);
618                 uset = &set;
619         } else
620                 uset = NULL;
621         /*
622          * XXX Do pointers need PTRIN()?
623          */
624         error = kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
625             uset, sizeof(int32_t) * 8);
626         return (error);
627 }
628
629 /*
630  * Copy 'count' items into the destination list pointed to by uap->eventlist.
631  */
632 static int
633 freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count)
634 {
635         struct freebsd32_kevent_args *uap;
636         struct kevent32 ks32[KQ_NEVENTS];
637         uint64_t e;
638         int i, j, error;
639
640         KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
641         uap = (struct freebsd32_kevent_args *)arg;
642
643         for (i = 0; i < count; i++) {
644                 CP(kevp[i], ks32[i], ident);
645                 CP(kevp[i], ks32[i], filter);
646                 CP(kevp[i], ks32[i], flags);
647                 CP(kevp[i], ks32[i], fflags);
648 #if BYTE_ORDER == LITTLE_ENDIAN
649                 ks32[i].data1 = kevp[i].data;
650                 ks32[i].data2 = kevp[i].data >> 32;
651 #else
652                 ks32[i].data1 = kevp[i].data >> 32;
653                 ks32[i].data2 = kevp[i].data;
654 #endif
655                 PTROUT_CP(kevp[i], ks32[i], udata);
656                 for (j = 0; j < nitems(kevp->ext); j++) {
657                         e = kevp[i].ext[j];
658 #if BYTE_ORDER == LITTLE_ENDIAN
659                         ks32[i].ext64[2 * j] = e;
660                         ks32[i].ext64[2 * j + 1] = e >> 32;
661 #else
662                         ks32[i].ext64[2 * j] = e >> 32;
663                         ks32[i].ext64[2 * j + 1] = e;
664 #endif
665                 }
666         }
667         error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
668         if (error == 0)
669                 uap->eventlist += count;
670         return (error);
671 }
672
673 /*
674  * Copy 'count' items from the list pointed to by uap->changelist.
675  */
676 static int
677 freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count)
678 {
679         struct freebsd32_kevent_args *uap;
680         struct kevent32 ks32[KQ_NEVENTS];
681         uint64_t e;
682         int i, j, error;
683
684         KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
685         uap = (struct freebsd32_kevent_args *)arg;
686
687         error = copyin(uap->changelist, ks32, count * sizeof *ks32);
688         if (error)
689                 goto done;
690         uap->changelist += count;
691
692         for (i = 0; i < count; i++) {
693                 CP(ks32[i], kevp[i], ident);
694                 CP(ks32[i], kevp[i], filter);
695                 CP(ks32[i], kevp[i], flags);
696                 CP(ks32[i], kevp[i], fflags);
697                 kevp[i].data = PAIR32TO64(uint64_t, ks32[i].data);
698                 PTRIN_CP(ks32[i], kevp[i], udata);
699                 for (j = 0; j < nitems(kevp->ext); j++) {
700 #if BYTE_ORDER == LITTLE_ENDIAN
701                         e = ks32[i].ext64[2 * j + 1];
702                         e <<= 32;
703                         e += ks32[i].ext64[2 * j];
704 #else
705                         e = ks32[i].ext64[2 * j];
706                         e <<= 32;
707                         e += ks32[i].ext64[2 * j + 1];
708 #endif
709                         kevp[i].ext[j] = e;
710                 }
711         }
712 done:
713         return (error);
714 }
715
716 int
717 freebsd32_kevent(struct thread *td, struct freebsd32_kevent_args *uap)
718 {
719         struct timespec32 ts32;
720         struct timespec ts, *tsp;
721         struct kevent_copyops k_ops = {
722                 .arg = uap,
723                 .k_copyout = freebsd32_kevent_copyout,
724                 .k_copyin = freebsd32_kevent_copyin,
725         };
726 #ifdef KTRACE
727         struct kevent32 *eventlist = uap->eventlist;
728 #endif
729         int error;
730
731         if (uap->timeout) {
732                 error = copyin(uap->timeout, &ts32, sizeof(ts32));
733                 if (error)
734                         return (error);
735                 CP(ts32, ts, tv_sec);
736                 CP(ts32, ts, tv_nsec);
737                 tsp = &ts;
738         } else
739                 tsp = NULL;
740 #ifdef KTRACE
741         if (KTRPOINT(td, KTR_STRUCT_ARRAY))
742                 ktrstructarray("kevent32", UIO_USERSPACE, uap->changelist,
743                     uap->nchanges, sizeof(struct kevent32));
744 #endif
745         error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
746             &k_ops, tsp);
747 #ifdef KTRACE
748         if (error == 0 && KTRPOINT(td, KTR_STRUCT_ARRAY))
749                 ktrstructarray("kevent32", UIO_USERSPACE, eventlist,
750                     td->td_retval[0], sizeof(struct kevent32));
751 #endif
752         return (error);
753 }
754
755 #ifdef COMPAT_FREEBSD11
756 static int
757 freebsd32_kevent11_copyout(void *arg, struct kevent *kevp, int count)
758 {
759         struct freebsd11_freebsd32_kevent_args *uap;
760         struct kevent32_freebsd11 ks32[KQ_NEVENTS];
761         int i, error;
762
763         KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
764         uap = (struct freebsd11_freebsd32_kevent_args *)arg;
765
766         for (i = 0; i < count; i++) {
767                 CP(kevp[i], ks32[i], ident);
768                 CP(kevp[i], ks32[i], filter);
769                 CP(kevp[i], ks32[i], flags);
770                 CP(kevp[i], ks32[i], fflags);
771                 CP(kevp[i], ks32[i], data);
772                 PTROUT_CP(kevp[i], ks32[i], udata);
773         }
774         error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
775         if (error == 0)
776                 uap->eventlist += count;
777         return (error);
778 }
779
780 /*
781  * Copy 'count' items from the list pointed to by uap->changelist.
782  */
783 static int
784 freebsd32_kevent11_copyin(void *arg, struct kevent *kevp, int count)
785 {
786         struct freebsd11_freebsd32_kevent_args *uap;
787         struct kevent32_freebsd11 ks32[KQ_NEVENTS];
788         int i, j, error;
789
790         KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
791         uap = (struct freebsd11_freebsd32_kevent_args *)arg;
792
793         error = copyin(uap->changelist, ks32, count * sizeof *ks32);
794         if (error)
795                 goto done;
796         uap->changelist += count;
797
798         for (i = 0; i < count; i++) {
799                 CP(ks32[i], kevp[i], ident);
800                 CP(ks32[i], kevp[i], filter);
801                 CP(ks32[i], kevp[i], flags);
802                 CP(ks32[i], kevp[i], fflags);
803                 CP(ks32[i], kevp[i], data);
804                 PTRIN_CP(ks32[i], kevp[i], udata);
805                 for (j = 0; j < nitems(kevp->ext); j++)
806                         kevp[i].ext[j] = 0;
807         }
808 done:
809         return (error);
810 }
811
812 int
813 freebsd11_freebsd32_kevent(struct thread *td,
814     struct freebsd11_freebsd32_kevent_args *uap)
815 {
816         struct timespec32 ts32;
817         struct timespec ts, *tsp;
818         struct kevent_copyops k_ops = {
819                 .arg = uap,
820                 .k_copyout = freebsd32_kevent11_copyout,
821                 .k_copyin = freebsd32_kevent11_copyin,
822         };
823 #ifdef KTRACE
824         struct kevent32_freebsd11 *eventlist = uap->eventlist;
825 #endif
826         int error;
827
828         if (uap->timeout) {
829                 error = copyin(uap->timeout, &ts32, sizeof(ts32));
830                 if (error)
831                         return (error);
832                 CP(ts32, ts, tv_sec);
833                 CP(ts32, ts, tv_nsec);
834                 tsp = &ts;
835         } else
836                 tsp = NULL;
837 #ifdef KTRACE
838         if (KTRPOINT(td, KTR_STRUCT_ARRAY))
839                 ktrstructarray("kevent32_freebsd11", UIO_USERSPACE,
840                     uap->changelist, uap->nchanges,
841                     sizeof(struct kevent32_freebsd11));
842 #endif
843         error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
844             &k_ops, tsp);
845 #ifdef KTRACE
846         if (error == 0 && KTRPOINT(td, KTR_STRUCT_ARRAY))
847                 ktrstructarray("kevent32_freebsd11", UIO_USERSPACE,
848                     eventlist, td->td_retval[0],
849                     sizeof(struct kevent32_freebsd11));
850 #endif
851         return (error);
852 }
853 #endif
854
855 int
856 freebsd32_gettimeofday(struct thread *td,
857                        struct freebsd32_gettimeofday_args *uap)
858 {
859         struct timeval atv;
860         struct timeval32 atv32;
861         struct timezone rtz;
862         int error = 0;
863
864         if (uap->tp) {
865                 microtime(&atv);
866                 CP(atv, atv32, tv_sec);
867                 CP(atv, atv32, tv_usec);
868                 error = copyout(&atv32, uap->tp, sizeof (atv32));
869         }
870         if (error == 0 && uap->tzp != NULL) {
871                 rtz.tz_minuteswest = tz_minuteswest;
872                 rtz.tz_dsttime = tz_dsttime;
873                 error = copyout(&rtz, uap->tzp, sizeof (rtz));
874         }
875         return (error);
876 }
877
878 int
879 freebsd32_getrusage(struct thread *td, struct freebsd32_getrusage_args *uap)
880 {
881         struct rusage32 s32;
882         struct rusage s;
883         int error;
884
885         error = kern_getrusage(td, uap->who, &s);
886         if (error)
887                 return (error);
888         if (uap->rusage != NULL) {
889                 freebsd32_rusage_out(&s, &s32);
890                 error = copyout(&s32, uap->rusage, sizeof(s32));
891         }
892         return (error);
893 }
894
895 static int
896 freebsd32_copyinuio(struct iovec32 *iovp, u_int iovcnt, struct uio **uiop)
897 {
898         struct iovec32 iov32;
899         struct iovec *iov;
900         struct uio *uio;
901         u_int iovlen;
902         int error, i;
903
904         *uiop = NULL;
905         if (iovcnt > UIO_MAXIOV)
906                 return (EINVAL);
907         iovlen = iovcnt * sizeof(struct iovec);
908         uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
909         iov = (struct iovec *)(uio + 1);
910         for (i = 0; i < iovcnt; i++) {
911                 error = copyin(&iovp[i], &iov32, sizeof(struct iovec32));
912                 if (error) {
913                         free(uio, M_IOV);
914                         return (error);
915                 }
916                 iov[i].iov_base = PTRIN(iov32.iov_base);
917                 iov[i].iov_len = iov32.iov_len;
918         }
919         uio->uio_iov = iov;
920         uio->uio_iovcnt = iovcnt;
921         uio->uio_segflg = UIO_USERSPACE;
922         uio->uio_offset = -1;
923         uio->uio_resid = 0;
924         for (i = 0; i < iovcnt; i++) {
925                 if (iov->iov_len > INT_MAX - uio->uio_resid) {
926                         free(uio, M_IOV);
927                         return (EINVAL);
928                 }
929                 uio->uio_resid += iov->iov_len;
930                 iov++;
931         }
932         *uiop = uio;
933         return (0);
934 }
935
936 int
937 freebsd32_readv(struct thread *td, struct freebsd32_readv_args *uap)
938 {
939         struct uio *auio;
940         int error;
941
942         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
943         if (error)
944                 return (error);
945         error = kern_readv(td, uap->fd, auio);
946         free(auio, M_IOV);
947         return (error);
948 }
949
950 int
951 freebsd32_writev(struct thread *td, struct freebsd32_writev_args *uap)
952 {
953         struct uio *auio;
954         int error;
955
956         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
957         if (error)
958                 return (error);
959         error = kern_writev(td, uap->fd, auio);
960         free(auio, M_IOV);
961         return (error);
962 }
963
964 int
965 freebsd32_preadv(struct thread *td, struct freebsd32_preadv_args *uap)
966 {
967         struct uio *auio;
968         int error;
969
970         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
971         if (error)
972                 return (error);
973         error = kern_preadv(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
974         free(auio, M_IOV);
975         return (error);
976 }
977
978 int
979 freebsd32_pwritev(struct thread *td, struct freebsd32_pwritev_args *uap)
980 {
981         struct uio *auio;
982         int error;
983
984         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
985         if (error)
986                 return (error);
987         error = kern_pwritev(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
988         free(auio, M_IOV);
989         return (error);
990 }
991
992 int
993 freebsd32_copyiniov(struct iovec32 *iovp32, u_int iovcnt, struct iovec **iovp,
994     int error)
995 {
996         struct iovec32 iov32;
997         struct iovec *iov;
998         u_int iovlen;
999         int i;
1000
1001         *iovp = NULL;
1002         if (iovcnt > UIO_MAXIOV)
1003                 return (error);
1004         iovlen = iovcnt * sizeof(struct iovec);
1005         iov = malloc(iovlen, M_IOV, M_WAITOK);
1006         for (i = 0; i < iovcnt; i++) {
1007                 error = copyin(&iovp32[i], &iov32, sizeof(struct iovec32));
1008                 if (error) {
1009                         free(iov, M_IOV);
1010                         return (error);
1011                 }
1012                 iov[i].iov_base = PTRIN(iov32.iov_base);
1013                 iov[i].iov_len = iov32.iov_len;
1014         }
1015         *iovp = iov;
1016         return (0);
1017 }
1018
1019 static int
1020 freebsd32_copyinmsghdr(struct msghdr32 *msg32, struct msghdr *msg)
1021 {
1022         struct msghdr32 m32;
1023         int error;
1024
1025         error = copyin(msg32, &m32, sizeof(m32));
1026         if (error)
1027                 return (error);
1028         msg->msg_name = PTRIN(m32.msg_name);
1029         msg->msg_namelen = m32.msg_namelen;
1030         msg->msg_iov = PTRIN(m32.msg_iov);
1031         msg->msg_iovlen = m32.msg_iovlen;
1032         msg->msg_control = PTRIN(m32.msg_control);
1033         msg->msg_controllen = m32.msg_controllen;
1034         msg->msg_flags = m32.msg_flags;
1035         return (0);
1036 }
1037
1038 static int
1039 freebsd32_copyoutmsghdr(struct msghdr *msg, struct msghdr32 *msg32)
1040 {
1041         struct msghdr32 m32;
1042         int error;
1043
1044         m32.msg_name = PTROUT(msg->msg_name);
1045         m32.msg_namelen = msg->msg_namelen;
1046         m32.msg_iov = PTROUT(msg->msg_iov);
1047         m32.msg_iovlen = msg->msg_iovlen;
1048         m32.msg_control = PTROUT(msg->msg_control);
1049         m32.msg_controllen = msg->msg_controllen;
1050         m32.msg_flags = msg->msg_flags;
1051         error = copyout(&m32, msg32, sizeof(m32));
1052         return (error);
1053 }
1054
1055 #ifndef __mips__
1056 #define FREEBSD32_ALIGNBYTES    (sizeof(int) - 1)
1057 #else
1058 #define FREEBSD32_ALIGNBYTES    (sizeof(long) - 1)
1059 #endif
1060 #define FREEBSD32_ALIGN(p)      \
1061         (((u_long)(p) + FREEBSD32_ALIGNBYTES) & ~FREEBSD32_ALIGNBYTES)
1062 #define FREEBSD32_CMSG_SPACE(l) \
1063         (FREEBSD32_ALIGN(sizeof(struct cmsghdr)) + FREEBSD32_ALIGN(l))
1064
1065 #define FREEBSD32_CMSG_DATA(cmsg)       ((unsigned char *)(cmsg) + \
1066                                  FREEBSD32_ALIGN(sizeof(struct cmsghdr)))
1067
1068 static size_t
1069 freebsd32_cmsg_convert(struct cmsghdr *cm, void *data, socklen_t datalen)
1070 {
1071         size_t copylen;
1072         union {
1073                 struct timespec32 ts;
1074                 struct timeval32 tv;
1075                 struct bintime32 bt;
1076         } tmp32;
1077
1078         union {
1079                 struct timespec ts;
1080                 struct timeval tv;
1081                 struct bintime bt;
1082         } *in;
1083
1084         in = data;
1085         copylen = 0;
1086         switch (cm->cmsg_level) {
1087         case SOL_SOCKET:
1088                 switch (cm->cmsg_type) {
1089                 case SCM_TIMESTAMP:
1090                         TV_CP(*in, tmp32, tv);
1091                         copylen = sizeof(tmp32.tv);
1092                         break;
1093
1094                 case SCM_BINTIME:
1095                         BT_CP(*in, tmp32, bt);
1096                         copylen = sizeof(tmp32.bt);
1097                         break;
1098
1099                 case SCM_REALTIME:
1100                 case SCM_MONOTONIC:
1101                         TS_CP(*in, tmp32, ts);
1102                         copylen = sizeof(tmp32.ts);
1103                         break;
1104
1105                 default:
1106                         break;
1107                 }
1108
1109         default:
1110                 break;
1111         }
1112
1113         if (copylen == 0)
1114                 return (datalen);
1115
1116         KASSERT((datalen >= copylen), ("corrupted cmsghdr"));
1117
1118         bcopy(&tmp32, data, copylen);
1119         return (copylen);
1120 }
1121
1122 static int
1123 freebsd32_copy_msg_out(struct msghdr *msg, struct mbuf *control)
1124 {
1125         struct cmsghdr *cm;
1126         void *data;
1127         socklen_t clen, datalen, datalen_out;
1128         int error;
1129         caddr_t ctlbuf;
1130         int len, maxlen, copylen;
1131         struct mbuf *m;
1132         error = 0;
1133
1134         len    = msg->msg_controllen;
1135         maxlen = msg->msg_controllen;
1136         msg->msg_controllen = 0;
1137
1138         m = control;
1139         ctlbuf = msg->msg_control;
1140       
1141         while (m && len > 0) {
1142                 cm = mtod(m, struct cmsghdr *);
1143                 clen = m->m_len;
1144
1145                 while (cm != NULL) {
1146
1147                         if (sizeof(struct cmsghdr) > clen ||
1148                             cm->cmsg_len > clen) {
1149                                 error = EINVAL;
1150                                 break;
1151                         }
1152
1153                         data   = CMSG_DATA(cm);
1154                         datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1155                         datalen_out = freebsd32_cmsg_convert(cm, data, datalen);
1156
1157                         /* Adjust message length */
1158                         cm->cmsg_len = FREEBSD32_ALIGN(sizeof(struct cmsghdr)) +
1159                             datalen_out;
1160
1161                         /* Copy cmsghdr */
1162                         copylen = sizeof(struct cmsghdr);
1163                         if (len < copylen) {
1164                                 msg->msg_flags |= MSG_CTRUNC;
1165                                 copylen = len;
1166                         }
1167
1168                         error = copyout(cm, ctlbuf, copylen);
1169                         if (error)
1170                                 goto exit;
1171
1172                         ctlbuf += FREEBSD32_ALIGN(copylen);
1173                         len    -= FREEBSD32_ALIGN(copylen);
1174
1175                         if (len <= 0)
1176                                 break;
1177
1178                         /* Copy data */
1179                         copylen = datalen_out;
1180                         if (len < copylen) {
1181                                 msg->msg_flags |= MSG_CTRUNC;
1182                                 copylen = len;
1183                         }
1184
1185                         error = copyout(data, ctlbuf, copylen);
1186                         if (error)
1187                                 goto exit;
1188
1189                         ctlbuf += FREEBSD32_ALIGN(copylen);
1190                         len    -= FREEBSD32_ALIGN(copylen);
1191
1192                         if (CMSG_SPACE(datalen) < clen) {
1193                                 clen -= CMSG_SPACE(datalen);
1194                                 cm = (struct cmsghdr *)
1195                                         ((caddr_t)cm + CMSG_SPACE(datalen));
1196                         } else {
1197                                 clen = 0;
1198                                 cm = NULL;
1199                         }
1200                 }       
1201                 m = m->m_next;
1202         }
1203
1204         msg->msg_controllen = (len <= 0) ? maxlen :  ctlbuf - (caddr_t)msg->msg_control;
1205         
1206 exit:
1207         return (error);
1208
1209 }
1210
1211 int
1212 freebsd32_recvmsg(td, uap)
1213         struct thread *td;
1214         struct freebsd32_recvmsg_args /* {
1215                 int     s;
1216                 struct  msghdr32 *msg;
1217                 int     flags;
1218         } */ *uap;
1219 {
1220         struct msghdr msg;
1221         struct msghdr32 m32;
1222         struct iovec *uiov, *iov;
1223         struct mbuf *control = NULL;
1224         struct mbuf **controlp;
1225
1226         int error;
1227         error = copyin(uap->msg, &m32, sizeof(m32));
1228         if (error)
1229                 return (error);
1230         error = freebsd32_copyinmsghdr(uap->msg, &msg);
1231         if (error)
1232                 return (error);
1233         error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1234             EMSGSIZE);
1235         if (error)
1236                 return (error);
1237         msg.msg_flags = uap->flags;
1238         uiov = msg.msg_iov;
1239         msg.msg_iov = iov;
1240
1241         controlp = (msg.msg_control != NULL) ?  &control : NULL;
1242         error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, controlp);
1243         if (error == 0) {
1244                 msg.msg_iov = uiov;
1245                 
1246                 if (control != NULL)
1247                         error = freebsd32_copy_msg_out(&msg, control);
1248                 else
1249                         msg.msg_controllen = 0;
1250                 
1251                 if (error == 0)
1252                         error = freebsd32_copyoutmsghdr(&msg, uap->msg);
1253         }
1254         free(iov, M_IOV);
1255
1256         if (control != NULL)
1257                 m_freem(control);
1258
1259         return (error);
1260 }
1261
1262 /*
1263  * Copy-in the array of control messages constructed using alignment
1264  * and padding suitable for a 32-bit environment and construct an
1265  * mbuf using alignment and padding suitable for a 64-bit kernel.
1266  * The alignment and padding are defined indirectly by CMSG_DATA(),
1267  * CMSG_SPACE() and CMSG_LEN().
1268  */
1269 static int
1270 freebsd32_copyin_control(struct mbuf **mp, caddr_t buf, u_int buflen)
1271 {
1272         struct mbuf *m;
1273         void *md;
1274         u_int idx, len, msglen;
1275         int error;
1276
1277         buflen = FREEBSD32_ALIGN(buflen);
1278
1279         if (buflen > MCLBYTES)
1280                 return (EINVAL);
1281
1282         /*
1283          * Iterate over the buffer and get the length of each message
1284          * in there. This has 32-bit alignment and padding. Use it to
1285          * determine the length of these messages when using 64-bit
1286          * alignment and padding.
1287          */
1288         idx = 0;
1289         len = 0;
1290         while (idx < buflen) {
1291                 error = copyin(buf + idx, &msglen, sizeof(msglen));
1292                 if (error)
1293                         return (error);
1294                 if (msglen < sizeof(struct cmsghdr))
1295                         return (EINVAL);
1296                 msglen = FREEBSD32_ALIGN(msglen);
1297                 if (idx + msglen > buflen)
1298                         return (EINVAL);
1299                 idx += msglen;
1300                 msglen += CMSG_ALIGN(sizeof(struct cmsghdr)) -
1301                     FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1302                 len += CMSG_ALIGN(msglen);
1303         }
1304
1305         if (len > MCLBYTES)
1306                 return (EINVAL);
1307
1308         m = m_get(M_WAITOK, MT_CONTROL);
1309         if (len > MLEN)
1310                 MCLGET(m, M_WAITOK);
1311         m->m_len = len;
1312
1313         md = mtod(m, void *);
1314         while (buflen > 0) {
1315                 error = copyin(buf, md, sizeof(struct cmsghdr));
1316                 if (error)
1317                         break;
1318                 msglen = *(u_int *)md;
1319                 msglen = FREEBSD32_ALIGN(msglen);
1320
1321                 /* Modify the message length to account for alignment. */
1322                 *(u_int *)md = msglen + CMSG_ALIGN(sizeof(struct cmsghdr)) -
1323                     FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1324
1325                 md = (char *)md + CMSG_ALIGN(sizeof(struct cmsghdr));
1326                 buf += FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1327                 buflen -= FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1328
1329                 msglen -= FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1330                 if (msglen > 0) {
1331                         error = copyin(buf, md, msglen);
1332                         if (error)
1333                                 break;
1334                         md = (char *)md + CMSG_ALIGN(msglen);
1335                         buf += msglen;
1336                         buflen -= msglen;
1337                 }
1338         }
1339
1340         if (error)
1341                 m_free(m);
1342         else
1343                 *mp = m;
1344         return (error);
1345 }
1346
1347 int
1348 freebsd32_sendmsg(struct thread *td,
1349                   struct freebsd32_sendmsg_args *uap)
1350 {
1351         struct msghdr msg;
1352         struct msghdr32 m32;
1353         struct iovec *iov;
1354         struct mbuf *control = NULL;
1355         struct sockaddr *to = NULL;
1356         int error;
1357
1358         error = copyin(uap->msg, &m32, sizeof(m32));
1359         if (error)
1360                 return (error);
1361         error = freebsd32_copyinmsghdr(uap->msg, &msg);
1362         if (error)
1363                 return (error);
1364         error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1365             EMSGSIZE);
1366         if (error)
1367                 return (error);
1368         msg.msg_iov = iov;
1369         if (msg.msg_name != NULL) {
1370                 error = getsockaddr(&to, msg.msg_name, msg.msg_namelen);
1371                 if (error) {
1372                         to = NULL;
1373                         goto out;
1374                 }
1375                 msg.msg_name = to;
1376         }
1377
1378         if (msg.msg_control) {
1379                 if (msg.msg_controllen < sizeof(struct cmsghdr)) {
1380                         error = EINVAL;
1381                         goto out;
1382                 }
1383
1384                 error = freebsd32_copyin_control(&control, msg.msg_control,
1385                     msg.msg_controllen);
1386                 if (error)
1387                         goto out;
1388
1389                 msg.msg_control = NULL;
1390                 msg.msg_controllen = 0;
1391         }
1392
1393         error = kern_sendit(td, uap->s, &msg, uap->flags, control,
1394             UIO_USERSPACE);
1395
1396 out:
1397         free(iov, M_IOV);
1398         if (to)
1399                 free(to, M_SONAME);
1400         return (error);
1401 }
1402
1403 int
1404 freebsd32_recvfrom(struct thread *td,
1405                    struct freebsd32_recvfrom_args *uap)
1406 {
1407         struct msghdr msg;
1408         struct iovec aiov;
1409         int error;
1410
1411         if (uap->fromlenaddr) {
1412                 error = copyin(PTRIN(uap->fromlenaddr), &msg.msg_namelen,
1413                     sizeof(msg.msg_namelen));
1414                 if (error)
1415                         return (error);
1416         } else {
1417                 msg.msg_namelen = 0;
1418         }
1419
1420         msg.msg_name = PTRIN(uap->from);
1421         msg.msg_iov = &aiov;
1422         msg.msg_iovlen = 1;
1423         aiov.iov_base = PTRIN(uap->buf);
1424         aiov.iov_len = uap->len;
1425         msg.msg_control = NULL;
1426         msg.msg_flags = uap->flags;
1427         error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, NULL);
1428         if (error == 0 && uap->fromlenaddr)
1429                 error = copyout(&msg.msg_namelen, PTRIN(uap->fromlenaddr),
1430                     sizeof (msg.msg_namelen));
1431         return (error);
1432 }
1433
1434 int
1435 freebsd32_settimeofday(struct thread *td,
1436                        struct freebsd32_settimeofday_args *uap)
1437 {
1438         struct timeval32 tv32;
1439         struct timeval tv, *tvp;
1440         struct timezone tz, *tzp;
1441         int error;
1442
1443         if (uap->tv) {
1444                 error = copyin(uap->tv, &tv32, sizeof(tv32));
1445                 if (error)
1446                         return (error);
1447                 CP(tv32, tv, tv_sec);
1448                 CP(tv32, tv, tv_usec);
1449                 tvp = &tv;
1450         } else
1451                 tvp = NULL;
1452         if (uap->tzp) {
1453                 error = copyin(uap->tzp, &tz, sizeof(tz));
1454                 if (error)
1455                         return (error);
1456                 tzp = &tz;
1457         } else
1458                 tzp = NULL;
1459         return (kern_settimeofday(td, tvp, tzp));
1460 }
1461
1462 int
1463 freebsd32_utimes(struct thread *td, struct freebsd32_utimes_args *uap)
1464 {
1465         struct timeval32 s32[2];
1466         struct timeval s[2], *sp;
1467         int error;
1468
1469         if (uap->tptr != NULL) {
1470                 error = copyin(uap->tptr, s32, sizeof(s32));
1471                 if (error)
1472                         return (error);
1473                 CP(s32[0], s[0], tv_sec);
1474                 CP(s32[0], s[0], tv_usec);
1475                 CP(s32[1], s[1], tv_sec);
1476                 CP(s32[1], s[1], tv_usec);
1477                 sp = s;
1478         } else
1479                 sp = NULL;
1480         return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1481             sp, UIO_SYSSPACE));
1482 }
1483
1484 int
1485 freebsd32_lutimes(struct thread *td, struct freebsd32_lutimes_args *uap)
1486 {
1487         struct timeval32 s32[2];
1488         struct timeval s[2], *sp;
1489         int error;
1490
1491         if (uap->tptr != NULL) {
1492                 error = copyin(uap->tptr, s32, sizeof(s32));
1493                 if (error)
1494                         return (error);
1495                 CP(s32[0], s[0], tv_sec);
1496                 CP(s32[0], s[0], tv_usec);
1497                 CP(s32[1], s[1], tv_sec);
1498                 CP(s32[1], s[1], tv_usec);
1499                 sp = s;
1500         } else
1501                 sp = NULL;
1502         return (kern_lutimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1503 }
1504
1505 int
1506 freebsd32_futimes(struct thread *td, struct freebsd32_futimes_args *uap)
1507 {
1508         struct timeval32 s32[2];
1509         struct timeval s[2], *sp;
1510         int error;
1511
1512         if (uap->tptr != NULL) {
1513                 error = copyin(uap->tptr, s32, sizeof(s32));
1514                 if (error)
1515                         return (error);
1516                 CP(s32[0], s[0], tv_sec);
1517                 CP(s32[0], s[0], tv_usec);
1518                 CP(s32[1], s[1], tv_sec);
1519                 CP(s32[1], s[1], tv_usec);
1520                 sp = s;
1521         } else
1522                 sp = NULL;
1523         return (kern_futimes(td, uap->fd, sp, UIO_SYSSPACE));
1524 }
1525
1526 int
1527 freebsd32_futimesat(struct thread *td, struct freebsd32_futimesat_args *uap)
1528 {
1529         struct timeval32 s32[2];
1530         struct timeval s[2], *sp;
1531         int error;
1532
1533         if (uap->times != NULL) {
1534                 error = copyin(uap->times, s32, sizeof(s32));
1535                 if (error)
1536                         return (error);
1537                 CP(s32[0], s[0], tv_sec);
1538                 CP(s32[0], s[0], tv_usec);
1539                 CP(s32[1], s[1], tv_sec);
1540                 CP(s32[1], s[1], tv_usec);
1541                 sp = s;
1542         } else
1543                 sp = NULL;
1544         return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
1545                 sp, UIO_SYSSPACE));
1546 }
1547
1548 int
1549 freebsd32_futimens(struct thread *td, struct freebsd32_futimens_args *uap)
1550 {
1551         struct timespec32 ts32[2];
1552         struct timespec ts[2], *tsp;
1553         int error;
1554
1555         if (uap->times != NULL) {
1556                 error = copyin(uap->times, ts32, sizeof(ts32));
1557                 if (error)
1558                         return (error);
1559                 CP(ts32[0], ts[0], tv_sec);
1560                 CP(ts32[0], ts[0], tv_nsec);
1561                 CP(ts32[1], ts[1], tv_sec);
1562                 CP(ts32[1], ts[1], tv_nsec);
1563                 tsp = ts;
1564         } else
1565                 tsp = NULL;
1566         return (kern_futimens(td, uap->fd, tsp, UIO_SYSSPACE));
1567 }
1568
1569 int
1570 freebsd32_utimensat(struct thread *td, struct freebsd32_utimensat_args *uap)
1571 {
1572         struct timespec32 ts32[2];
1573         struct timespec ts[2], *tsp;
1574         int error;
1575
1576         if (uap->times != NULL) {
1577                 error = copyin(uap->times, ts32, sizeof(ts32));
1578                 if (error)
1579                         return (error);
1580                 CP(ts32[0], ts[0], tv_sec);
1581                 CP(ts32[0], ts[0], tv_nsec);
1582                 CP(ts32[1], ts[1], tv_sec);
1583                 CP(ts32[1], ts[1], tv_nsec);
1584                 tsp = ts;
1585         } else
1586                 tsp = NULL;
1587         return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
1588             tsp, UIO_SYSSPACE, uap->flag));
1589 }
1590
1591 int
1592 freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap)
1593 {
1594         struct timeval32 tv32;
1595         struct timeval delta, olddelta, *deltap;
1596         int error;
1597
1598         if (uap->delta) {
1599                 error = copyin(uap->delta, &tv32, sizeof(tv32));
1600                 if (error)
1601                         return (error);
1602                 CP(tv32, delta, tv_sec);
1603                 CP(tv32, delta, tv_usec);
1604                 deltap = &delta;
1605         } else
1606                 deltap = NULL;
1607         error = kern_adjtime(td, deltap, &olddelta);
1608         if (uap->olddelta && error == 0) {
1609                 CP(olddelta, tv32, tv_sec);
1610                 CP(olddelta, tv32, tv_usec);
1611                 error = copyout(&tv32, uap->olddelta, sizeof(tv32));
1612         }
1613         return (error);
1614 }
1615
1616 #ifdef COMPAT_FREEBSD4
1617 int
1618 freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap)
1619 {
1620         struct statfs32 s32;
1621         struct statfs *sp;
1622         int error;
1623
1624         sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1625         error = kern_statfs(td, uap->path, UIO_USERSPACE, sp);
1626         if (error == 0) {
1627                 copy_statfs(sp, &s32);
1628                 error = copyout(&s32, uap->buf, sizeof(s32));
1629         }
1630         free(sp, M_STATFS);
1631         return (error);
1632 }
1633 #endif
1634
1635 #ifdef COMPAT_FREEBSD4
1636 int
1637 freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap)
1638 {
1639         struct statfs32 s32;
1640         struct statfs *sp;
1641         int error;
1642
1643         sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1644         error = kern_fstatfs(td, uap->fd, sp);
1645         if (error == 0) {
1646                 copy_statfs(sp, &s32);
1647                 error = copyout(&s32, uap->buf, sizeof(s32));
1648         }
1649         free(sp, M_STATFS);
1650         return (error);
1651 }
1652 #endif
1653
1654 #ifdef COMPAT_FREEBSD4
1655 int
1656 freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap)
1657 {
1658         struct statfs32 s32;
1659         struct statfs *sp;
1660         fhandle_t fh;
1661         int error;
1662
1663         if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
1664                 return (error);
1665         sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1666         error = kern_fhstatfs(td, fh, sp);
1667         if (error == 0) {
1668                 copy_statfs(sp, &s32);
1669                 error = copyout(&s32, uap->buf, sizeof(s32));
1670         }
1671         free(sp, M_STATFS);
1672         return (error);
1673 }
1674 #endif
1675
1676 int
1677 freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap)
1678 {
1679
1680         return (kern_pread(td, uap->fd, uap->buf, uap->nbyte,
1681             PAIR32TO64(off_t, uap->offset)));
1682 }
1683
1684 int
1685 freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap)
1686 {
1687
1688         return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte,
1689             PAIR32TO64(off_t, uap->offset)));
1690 }
1691
1692 #ifdef COMPAT_43
1693 int
1694 ofreebsd32_lseek(struct thread *td, struct ofreebsd32_lseek_args *uap)
1695 {
1696
1697         return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1698 }
1699 #endif
1700
1701 int
1702 freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap)
1703 {
1704         int error;
1705         off_t pos;
1706
1707         error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset),
1708             uap->whence);
1709         /* Expand the quad return into two parts for eax and edx */
1710         pos = td->td_uretoff.tdu_off;
1711         td->td_retval[RETVAL_LO] = pos & 0xffffffff;    /* %eax */
1712         td->td_retval[RETVAL_HI] = pos >> 32;           /* %edx */
1713         return error;
1714 }
1715
1716 int
1717 freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap)
1718 {
1719
1720         return (kern_truncate(td, uap->path, UIO_USERSPACE,
1721             PAIR32TO64(off_t, uap->length)));
1722 }
1723
1724 int
1725 freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap)
1726 {
1727
1728         return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length)));
1729 }
1730
1731 #ifdef COMPAT_43
1732 int
1733 ofreebsd32_getdirentries(struct thread *td,
1734     struct ofreebsd32_getdirentries_args *uap)
1735 {
1736         struct ogetdirentries_args ap;
1737         int error;
1738         long loff;
1739         int32_t loff_cut;
1740
1741         ap.fd = uap->fd;
1742         ap.buf = uap->buf;
1743         ap.count = uap->count;
1744         ap.basep = NULL;
1745         error = kern_ogetdirentries(td, &ap, &loff);
1746         if (error == 0) {
1747                 loff_cut = loff;
1748                 error = copyout(&loff_cut, uap->basep, sizeof(int32_t));
1749         }
1750         return (error);
1751 }
1752 #endif
1753
1754 #if defined(COMPAT_FREEBSD11)
1755 int
1756 freebsd11_freebsd32_getdirentries(struct thread *td,
1757     struct freebsd11_freebsd32_getdirentries_args *uap)
1758 {
1759         long base;
1760         int32_t base32;
1761         int error;
1762
1763         error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
1764             &base, NULL);
1765         if (error)
1766                 return (error);
1767         if (uap->basep != NULL) {
1768                 base32 = base;
1769                 error = copyout(&base32, uap->basep, sizeof(int32_t));
1770         }
1771         return (error);
1772 }
1773
1774 int
1775 freebsd11_freebsd32_getdents(struct thread *td,
1776     struct freebsd11_freebsd32_getdents_args *uap)
1777 {
1778         struct freebsd11_freebsd32_getdirentries_args ap;
1779
1780         ap.fd = uap->fd;
1781         ap.buf = uap->buf;
1782         ap.count = uap->count;
1783         ap.basep = NULL;
1784         return (freebsd11_freebsd32_getdirentries(td, &ap));
1785 }
1786 #endif /* COMPAT_FREEBSD11 */
1787
1788 #ifdef COMPAT_FREEBSD6
1789 /* versions with the 'int pad' argument */
1790 int
1791 freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap)
1792 {
1793
1794         return (kern_pread(td, uap->fd, uap->buf, uap->nbyte,
1795             PAIR32TO64(off_t, uap->offset)));
1796 }
1797
1798 int
1799 freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap)
1800 {
1801
1802         return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte,
1803             PAIR32TO64(off_t, uap->offset)));
1804 }
1805
1806 int
1807 freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap)
1808 {
1809         int error;
1810         off_t pos;
1811
1812         error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset),
1813             uap->whence);
1814         /* Expand the quad return into two parts for eax and edx */
1815         pos = *(off_t *)(td->td_retval);
1816         td->td_retval[RETVAL_LO] = pos & 0xffffffff;    /* %eax */
1817         td->td_retval[RETVAL_HI] = pos >> 32;           /* %edx */
1818         return error;
1819 }
1820
1821 int
1822 freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap)
1823 {
1824
1825         return (kern_truncate(td, uap->path, UIO_USERSPACE,
1826             PAIR32TO64(off_t, uap->length)));
1827 }
1828
1829 int
1830 freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap)
1831 {
1832
1833         return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length)));
1834 }
1835 #endif /* COMPAT_FREEBSD6 */
1836
1837 struct sf_hdtr32 {
1838         uint32_t headers;
1839         int hdr_cnt;
1840         uint32_t trailers;
1841         int trl_cnt;
1842 };
1843
1844 static int
1845 freebsd32_do_sendfile(struct thread *td,
1846     struct freebsd32_sendfile_args *uap, int compat)
1847 {
1848         struct sf_hdtr32 hdtr32;
1849         struct sf_hdtr hdtr;
1850         struct uio *hdr_uio, *trl_uio;
1851         struct file *fp;
1852         cap_rights_t rights;
1853         struct iovec32 *iov32;
1854         off_t offset, sbytes;
1855         int error;
1856
1857         offset = PAIR32TO64(off_t, uap->offset);
1858         if (offset < 0)
1859                 return (EINVAL);
1860
1861         hdr_uio = trl_uio = NULL;
1862
1863         if (uap->hdtr != NULL) {
1864                 error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
1865                 if (error)
1866                         goto out;
1867                 PTRIN_CP(hdtr32, hdtr, headers);
1868                 CP(hdtr32, hdtr, hdr_cnt);
1869                 PTRIN_CP(hdtr32, hdtr, trailers);
1870                 CP(hdtr32, hdtr, trl_cnt);
1871
1872                 if (hdtr.headers != NULL) {
1873                         iov32 = PTRIN(hdtr32.headers);
1874                         error = freebsd32_copyinuio(iov32,
1875                             hdtr32.hdr_cnt, &hdr_uio);
1876                         if (error)
1877                                 goto out;
1878 #ifdef COMPAT_FREEBSD4
1879                         /*
1880                          * In FreeBSD < 5.0 the nbytes to send also included
1881                          * the header.  If compat is specified subtract the
1882                          * header size from nbytes.
1883                          */
1884                         if (compat) {
1885                                 if (uap->nbytes > hdr_uio->uio_resid)
1886                                         uap->nbytes -= hdr_uio->uio_resid;
1887                                 else
1888                                         uap->nbytes = 0;
1889                         }
1890 #endif
1891                 }
1892                 if (hdtr.trailers != NULL) {
1893                         iov32 = PTRIN(hdtr32.trailers);
1894                         error = freebsd32_copyinuio(iov32,
1895                             hdtr32.trl_cnt, &trl_uio);
1896                         if (error)
1897                                 goto out;
1898                 }
1899         }
1900
1901         AUDIT_ARG_FD(uap->fd);
1902
1903         if ((error = fget_read(td, uap->fd,
1904             cap_rights_init(&rights, CAP_PREAD), &fp)) != 0)
1905                 goto out;
1906
1907         error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, offset,
1908             uap->nbytes, &sbytes, uap->flags, td);
1909         fdrop(fp, td);
1910
1911         if (uap->sbytes != NULL)
1912                 copyout(&sbytes, uap->sbytes, sizeof(off_t));
1913
1914 out:
1915         if (hdr_uio)
1916                 free(hdr_uio, M_IOV);
1917         if (trl_uio)
1918                 free(trl_uio, M_IOV);
1919         return (error);
1920 }
1921
1922 #ifdef COMPAT_FREEBSD4
1923 int
1924 freebsd4_freebsd32_sendfile(struct thread *td,
1925     struct freebsd4_freebsd32_sendfile_args *uap)
1926 {
1927         return (freebsd32_do_sendfile(td,
1928             (struct freebsd32_sendfile_args *)uap, 1));
1929 }
1930 #endif
1931
1932 int
1933 freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
1934 {
1935
1936         return (freebsd32_do_sendfile(td, uap, 0));
1937 }
1938
1939 static void
1940 copy_stat(struct stat *in, struct stat32 *out)
1941 {
1942
1943         CP(*in, *out, st_dev);
1944         CP(*in, *out, st_ino);
1945         CP(*in, *out, st_mode);
1946         CP(*in, *out, st_nlink);
1947         CP(*in, *out, st_uid);
1948         CP(*in, *out, st_gid);
1949         CP(*in, *out, st_rdev);
1950         TS_CP(*in, *out, st_atim);
1951         TS_CP(*in, *out, st_mtim);
1952         TS_CP(*in, *out, st_ctim);
1953         CP(*in, *out, st_size);
1954         CP(*in, *out, st_blocks);
1955         CP(*in, *out, st_blksize);
1956         CP(*in, *out, st_flags);
1957         CP(*in, *out, st_gen);
1958         TS_CP(*in, *out, st_birthtim);
1959         out->st_padding0 = 0;
1960         out->st_padding1 = 0;
1961 #ifdef __STAT32_TIME_T_EXT
1962         out->st_atim_ext = 0;
1963         out->st_mtim_ext = 0;
1964         out->st_ctim_ext = 0;
1965         out->st_btim_ext = 0;
1966 #endif
1967         bzero(out->st_spare, sizeof(out->st_spare));
1968 }
1969
1970 #ifdef COMPAT_43
1971 static void
1972 copy_ostat(struct stat *in, struct ostat32 *out)
1973 {
1974
1975         bzero(out, sizeof(*out));
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         out->st_size = MIN(in->st_size, INT32_MAX);
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)
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         vectp = (uint32_t *)destp;
3185         if (imgp->auxargs) {
3186                 /*
3187                  * Allocate room on the stack for the ELF auxargs
3188                  * array.  It has up to AT_COUNT entries.
3189                  */
3190                 vectp -= howmany(AT_COUNT * sizeof(Elf32_Auxinfo),
3191                     sizeof(*vectp));
3192         }
3193
3194         /*
3195          * Allocate room for the argv[] and env vectors including the
3196          * terminating NULL pointers.
3197          */
3198         vectp -= imgp->args->argc + 1 + imgp->args->envc + 1;
3199
3200         /*
3201          * vectp also becomes our initial stack base
3202          */
3203         stack_base = vectp;
3204
3205         stringp = imgp->args->begin_argv;
3206         argc = imgp->args->argc;
3207         envc = imgp->args->envc;
3208         /*
3209          * Copy out strings - arguments and environment.
3210          */
3211         copyout(stringp, (void *)destp, ARG_MAX - imgp->args->stringspace);
3212
3213         /*
3214          * Fill in "ps_strings" struct for ps, w, etc.
3215          */
3216         suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp);
3217         suword32(&arginfo->ps_nargvstr, argc);
3218
3219         /*
3220          * Fill in argument portion of vector table.
3221          */
3222         for (; argc > 0; --argc) {
3223                 suword32(vectp++, (u_int32_t)(intptr_t)destp);
3224                 while (*stringp++ != 0)
3225                         destp++;
3226                 destp++;
3227         }
3228
3229         /* a null vector table pointer separates the argp's from the envp's */
3230         suword32(vectp++, 0);
3231
3232         suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp);
3233         suword32(&arginfo->ps_nenvstr, envc);
3234
3235         /*
3236          * Fill in environment portion of vector table.
3237          */
3238         for (; envc > 0; --envc) {
3239                 suword32(vectp++, (u_int32_t)(intptr_t)destp);
3240                 while (*stringp++ != 0)
3241                         destp++;
3242                 destp++;
3243         }
3244
3245         /* end of vector table is a null pointer */
3246         suword32(vectp, 0);
3247
3248         return ((register_t *)stack_base);
3249 }
3250
3251 int
3252 freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
3253 {
3254         struct kld_file_stat *stat;
3255         struct kld32_file_stat *stat32;
3256         int error, version;
3257
3258         if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
3259             != 0)
3260                 return (error);
3261         if (version != sizeof(struct kld32_file_stat_1) &&
3262             version != sizeof(struct kld32_file_stat))
3263                 return (EINVAL);
3264
3265         stat = malloc(sizeof(*stat), M_TEMP, M_WAITOK | M_ZERO);
3266         stat32 = malloc(sizeof(*stat32), M_TEMP, M_WAITOK | M_ZERO);
3267         error = kern_kldstat(td, uap->fileid, stat);
3268         if (error == 0) {
3269                 bcopy(&stat->name[0], &stat32->name[0], sizeof(stat->name));
3270                 CP(*stat, *stat32, refs);
3271                 CP(*stat, *stat32, id);
3272                 PTROUT_CP(*stat, *stat32, address);
3273                 CP(*stat, *stat32, size);
3274                 bcopy(&stat->pathname[0], &stat32->pathname[0],
3275                     sizeof(stat->pathname));
3276                 stat32->version  = version;
3277                 error = copyout(stat32, uap->stat, version);
3278         }
3279         free(stat, M_TEMP);
3280         free(stat32, M_TEMP);
3281         return (error);
3282 }
3283
3284 int
3285 freebsd32_posix_fallocate(struct thread *td,
3286     struct freebsd32_posix_fallocate_args *uap)
3287 {
3288         int error;
3289
3290         error = kern_posix_fallocate(td, uap->fd,
3291             PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len));
3292         return (kern_posix_error(td, error));
3293 }
3294
3295 int
3296 freebsd32_posix_fadvise(struct thread *td,
3297     struct freebsd32_posix_fadvise_args *uap)
3298 {
3299         int error;
3300
3301         error = kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset),
3302             PAIR32TO64(off_t, uap->len), uap->advice);
3303         return (kern_posix_error(td, error));
3304 }
3305
3306 int
3307 convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig)
3308 {
3309
3310         CP(*sig32, *sig, sigev_notify);
3311         switch (sig->sigev_notify) {
3312         case SIGEV_NONE:
3313                 break;
3314         case SIGEV_THREAD_ID:
3315                 CP(*sig32, *sig, sigev_notify_thread_id);
3316                 /* FALLTHROUGH */
3317         case SIGEV_SIGNAL:
3318                 CP(*sig32, *sig, sigev_signo);
3319                 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3320                 break;
3321         case SIGEV_KEVENT:
3322                 CP(*sig32, *sig, sigev_notify_kqueue);
3323                 CP(*sig32, *sig, sigev_notify_kevent_flags);
3324                 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3325                 break;
3326         default:
3327                 return (EINVAL);
3328         }
3329         return (0);
3330 }
3331
3332 int
3333 freebsd32_procctl(struct thread *td, struct freebsd32_procctl_args *uap)
3334 {
3335         void *data;
3336         union {
3337                 struct procctl_reaper_status rs;
3338                 struct procctl_reaper_pids rp;
3339                 struct procctl_reaper_kill rk;
3340         } x;
3341         union {
3342                 struct procctl_reaper_pids32 rp;
3343         } x32;
3344         int error, error1, flags, signum;
3345
3346         switch (uap->com) {
3347         case PROC_SPROTECT:
3348         case PROC_TRACE_CTL:
3349         case PROC_TRAPCAP_CTL:
3350                 error = copyin(PTRIN(uap->data), &flags, sizeof(flags));
3351                 if (error != 0)
3352                         return (error);
3353                 data = &flags;
3354                 break;
3355         case PROC_REAP_ACQUIRE:
3356         case PROC_REAP_RELEASE:
3357                 if (uap->data != NULL)
3358                         return (EINVAL);
3359                 data = NULL;
3360                 break;
3361         case PROC_REAP_STATUS:
3362                 data = &x.rs;
3363                 break;
3364         case PROC_REAP_GETPIDS:
3365                 error = copyin(uap->data, &x32.rp, sizeof(x32.rp));
3366                 if (error != 0)
3367                         return (error);
3368                 CP(x32.rp, x.rp, rp_count);
3369                 PTRIN_CP(x32.rp, x.rp, rp_pids);
3370                 data = &x.rp;
3371                 break;
3372         case PROC_REAP_KILL:
3373                 error = copyin(uap->data, &x.rk, sizeof(x.rk));
3374                 if (error != 0)
3375                         return (error);
3376                 data = &x.rk;
3377                 break;
3378         case PROC_TRACE_STATUS:
3379         case PROC_TRAPCAP_STATUS:
3380                 data = &flags;
3381                 break;
3382         case PROC_PDEATHSIG_CTL:
3383                 error = copyin(uap->data, &signum, sizeof(signum));
3384                 if (error != 0)
3385                         return (error);
3386                 data = &signum;
3387                 break;
3388         case PROC_PDEATHSIG_STATUS:
3389                 data = &signum;
3390                 break;
3391         default:
3392                 return (EINVAL);
3393         }
3394         error = kern_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id),
3395             uap->com, data);
3396         switch (uap->com) {
3397         case PROC_REAP_STATUS:
3398                 if (error == 0)
3399                         error = copyout(&x.rs, uap->data, sizeof(x.rs));
3400                 break;
3401         case PROC_REAP_KILL:
3402                 error1 = copyout(&x.rk, uap->data, sizeof(x.rk));
3403                 if (error == 0)
3404                         error = error1;
3405                 break;
3406         case PROC_TRACE_STATUS:
3407         case PROC_TRAPCAP_STATUS:
3408                 if (error == 0)
3409                         error = copyout(&flags, uap->data, sizeof(flags));
3410                 break;
3411         case PROC_PDEATHSIG_STATUS:
3412                 if (error == 0)
3413                         error = copyout(&signum, uap->data, sizeof(signum));
3414                 break;
3415         }
3416         return (error);
3417 }
3418
3419 int
3420 freebsd32_fcntl(struct thread *td, struct freebsd32_fcntl_args *uap)
3421 {
3422         long tmp;
3423
3424         switch (uap->cmd) {
3425         /*
3426          * Do unsigned conversion for arg when operation
3427          * interprets it as flags or pointer.
3428          */
3429         case F_SETLK_REMOTE:
3430         case F_SETLKW:
3431         case F_SETLK:
3432         case F_GETLK:
3433         case F_SETFD:
3434         case F_SETFL:
3435         case F_OGETLK:
3436         case F_OSETLK:
3437         case F_OSETLKW:
3438                 tmp = (unsigned int)(uap->arg);
3439                 break;
3440         default:
3441                 tmp = uap->arg;
3442                 break;
3443         }
3444         return (kern_fcntl_freebsd(td, uap->fd, uap->cmd, tmp));
3445 }
3446
3447 int
3448 freebsd32_ppoll(struct thread *td, struct freebsd32_ppoll_args *uap)
3449 {
3450         struct timespec32 ts32;
3451         struct timespec ts, *tsp;
3452         sigset_t set, *ssp;
3453         int error;
3454
3455         if (uap->ts != NULL) {
3456                 error = copyin(uap->ts, &ts32, sizeof(ts32));
3457                 if (error != 0)
3458                         return (error);
3459                 CP(ts32, ts, tv_sec);
3460                 CP(ts32, ts, tv_nsec);
3461                 tsp = &ts;
3462         } else
3463                 tsp = NULL;
3464         if (uap->set != NULL) {
3465                 error = copyin(uap->set, &set, sizeof(set));
3466                 if (error != 0)
3467                         return (error);
3468                 ssp = &set;
3469         } else
3470                 ssp = NULL;
3471
3472         return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp));
3473 }