]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/freebsd32/freebsd32_misc.c
Upgrade Unbound to 1.7.2. More to follow.
[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 == 0) {
887                 freebsd32_rusage_out(&s, &s32);
888                 error = copyout(&s32, uap->rusage, sizeof(s32));
889         }
890         return (error);
891 }
892
893 static int
894 freebsd32_copyinuio(struct iovec32 *iovp, u_int iovcnt, struct uio **uiop)
895 {
896         struct iovec32 iov32;
897         struct iovec *iov;
898         struct uio *uio;
899         u_int iovlen;
900         int error, i;
901
902         *uiop = NULL;
903         if (iovcnt > UIO_MAXIOV)
904                 return (EINVAL);
905         iovlen = iovcnt * sizeof(struct iovec);
906         uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
907         iov = (struct iovec *)(uio + 1);
908         for (i = 0; i < iovcnt; i++) {
909                 error = copyin(&iovp[i], &iov32, sizeof(struct iovec32));
910                 if (error) {
911                         free(uio, M_IOV);
912                         return (error);
913                 }
914                 iov[i].iov_base = PTRIN(iov32.iov_base);
915                 iov[i].iov_len = iov32.iov_len;
916         }
917         uio->uio_iov = iov;
918         uio->uio_iovcnt = iovcnt;
919         uio->uio_segflg = UIO_USERSPACE;
920         uio->uio_offset = -1;
921         uio->uio_resid = 0;
922         for (i = 0; i < iovcnt; i++) {
923                 if (iov->iov_len > INT_MAX - uio->uio_resid) {
924                         free(uio, M_IOV);
925                         return (EINVAL);
926                 }
927                 uio->uio_resid += iov->iov_len;
928                 iov++;
929         }
930         *uiop = uio;
931         return (0);
932 }
933
934 int
935 freebsd32_readv(struct thread *td, struct freebsd32_readv_args *uap)
936 {
937         struct uio *auio;
938         int error;
939
940         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
941         if (error)
942                 return (error);
943         error = kern_readv(td, uap->fd, auio);
944         free(auio, M_IOV);
945         return (error);
946 }
947
948 int
949 freebsd32_writev(struct thread *td, struct freebsd32_writev_args *uap)
950 {
951         struct uio *auio;
952         int error;
953
954         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
955         if (error)
956                 return (error);
957         error = kern_writev(td, uap->fd, auio);
958         free(auio, M_IOV);
959         return (error);
960 }
961
962 int
963 freebsd32_preadv(struct thread *td, struct freebsd32_preadv_args *uap)
964 {
965         struct uio *auio;
966         int error;
967
968         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
969         if (error)
970                 return (error);
971         error = kern_preadv(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
972         free(auio, M_IOV);
973         return (error);
974 }
975
976 int
977 freebsd32_pwritev(struct thread *td, struct freebsd32_pwritev_args *uap)
978 {
979         struct uio *auio;
980         int error;
981
982         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
983         if (error)
984                 return (error);
985         error = kern_pwritev(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
986         free(auio, M_IOV);
987         return (error);
988 }
989
990 int
991 freebsd32_copyiniov(struct iovec32 *iovp32, u_int iovcnt, struct iovec **iovp,
992     int error)
993 {
994         struct iovec32 iov32;
995         struct iovec *iov;
996         u_int iovlen;
997         int i;
998
999         *iovp = NULL;
1000         if (iovcnt > UIO_MAXIOV)
1001                 return (error);
1002         iovlen = iovcnt * sizeof(struct iovec);
1003         iov = malloc(iovlen, M_IOV, M_WAITOK);
1004         for (i = 0; i < iovcnt; i++) {
1005                 error = copyin(&iovp32[i], &iov32, sizeof(struct iovec32));
1006                 if (error) {
1007                         free(iov, M_IOV);
1008                         return (error);
1009                 }
1010                 iov[i].iov_base = PTRIN(iov32.iov_base);
1011                 iov[i].iov_len = iov32.iov_len;
1012         }
1013         *iovp = iov;
1014         return (0);
1015 }
1016
1017 static int
1018 freebsd32_copyinmsghdr(struct msghdr32 *msg32, struct msghdr *msg)
1019 {
1020         struct msghdr32 m32;
1021         int error;
1022
1023         error = copyin(msg32, &m32, sizeof(m32));
1024         if (error)
1025                 return (error);
1026         msg->msg_name = PTRIN(m32.msg_name);
1027         msg->msg_namelen = m32.msg_namelen;
1028         msg->msg_iov = PTRIN(m32.msg_iov);
1029         msg->msg_iovlen = m32.msg_iovlen;
1030         msg->msg_control = PTRIN(m32.msg_control);
1031         msg->msg_controllen = m32.msg_controllen;
1032         msg->msg_flags = m32.msg_flags;
1033         return (0);
1034 }
1035
1036 static int
1037 freebsd32_copyoutmsghdr(struct msghdr *msg, struct msghdr32 *msg32)
1038 {
1039         struct msghdr32 m32;
1040         int error;
1041
1042         m32.msg_name = PTROUT(msg->msg_name);
1043         m32.msg_namelen = msg->msg_namelen;
1044         m32.msg_iov = PTROUT(msg->msg_iov);
1045         m32.msg_iovlen = msg->msg_iovlen;
1046         m32.msg_control = PTROUT(msg->msg_control);
1047         m32.msg_controllen = msg->msg_controllen;
1048         m32.msg_flags = msg->msg_flags;
1049         error = copyout(&m32, msg32, sizeof(m32));
1050         return (error);
1051 }
1052
1053 #ifndef __mips__
1054 #define FREEBSD32_ALIGNBYTES    (sizeof(int) - 1)
1055 #else
1056 #define FREEBSD32_ALIGNBYTES    (sizeof(long) - 1)
1057 #endif
1058 #define FREEBSD32_ALIGN(p)      \
1059         (((u_long)(p) + FREEBSD32_ALIGNBYTES) & ~FREEBSD32_ALIGNBYTES)
1060 #define FREEBSD32_CMSG_SPACE(l) \
1061         (FREEBSD32_ALIGN(sizeof(struct cmsghdr)) + FREEBSD32_ALIGN(l))
1062
1063 #define FREEBSD32_CMSG_DATA(cmsg)       ((unsigned char *)(cmsg) + \
1064                                  FREEBSD32_ALIGN(sizeof(struct cmsghdr)))
1065
1066 static size_t
1067 freebsd32_cmsg_convert(const struct cmsghdr *cm, void *data, socklen_t datalen)
1068 {
1069         size_t copylen;
1070         union {
1071                 struct timespec32 ts;
1072                 struct timeval32 tv;
1073                 struct bintime32 bt;
1074         } tmp32;
1075
1076         union {
1077                 struct timespec ts;
1078                 struct timeval tv;
1079                 struct bintime bt;
1080         } *in;
1081
1082         in = data;
1083         copylen = 0;
1084         switch (cm->cmsg_level) {
1085         case SOL_SOCKET:
1086                 switch (cm->cmsg_type) {
1087                 case SCM_TIMESTAMP:
1088                         TV_CP(*in, tmp32, tv);
1089                         copylen = sizeof(tmp32.tv);
1090                         break;
1091
1092                 case SCM_BINTIME:
1093                         BT_CP(*in, tmp32, bt);
1094                         copylen = sizeof(tmp32.bt);
1095                         break;
1096
1097                 case SCM_REALTIME:
1098                 case SCM_MONOTONIC:
1099                         TS_CP(*in, tmp32, ts);
1100                         copylen = sizeof(tmp32.ts);
1101                         break;
1102
1103                 default:
1104                         break;
1105                 }
1106
1107         default:
1108                 break;
1109         }
1110
1111         if (copylen == 0)
1112                 return (datalen);
1113
1114         KASSERT((datalen >= copylen), ("corrupted cmsghdr"));
1115
1116         bcopy(&tmp32, data, copylen);
1117         return (copylen);
1118 }
1119
1120 static int
1121 freebsd32_copy_msg_out(struct msghdr *msg, struct mbuf *control)
1122 {
1123         struct cmsghdr *cm;
1124         void *data;
1125         socklen_t clen, datalen, datalen_out, oldclen;
1126         int error;
1127         caddr_t ctlbuf;
1128         int len, maxlen, copylen;
1129         struct mbuf *m;
1130         error = 0;
1131
1132         len    = msg->msg_controllen;
1133         maxlen = msg->msg_controllen;
1134         msg->msg_controllen = 0;
1135
1136         ctlbuf = msg->msg_control;
1137         for (m = control; m != NULL && len > 0; m = m->m_next) {
1138                 cm = mtod(m, struct cmsghdr *);
1139                 clen = m->m_len;
1140                 while (cm != NULL) {
1141                         if (sizeof(struct cmsghdr) > clen ||
1142                             cm->cmsg_len > clen) {
1143                                 error = EINVAL;
1144                                 break;
1145                         }
1146
1147                         data   = CMSG_DATA(cm);
1148                         datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1149                         datalen_out = freebsd32_cmsg_convert(cm, data, datalen);
1150
1151                         /*
1152                          * Copy out the message header.  Preserve the native
1153                          * message size in case we need to inspect the message
1154                          * contents later.
1155                          */
1156                         copylen = sizeof(struct cmsghdr);
1157                         if (len < copylen) {
1158                                 msg->msg_flags |= MSG_CTRUNC;
1159                                 m_dispose_extcontrolm(m);
1160                                 goto exit;
1161                         }
1162                         oldclen = cm->cmsg_len;
1163                         cm->cmsg_len = FREEBSD32_ALIGN(sizeof(struct cmsghdr)) +
1164                             datalen_out;
1165                         error = copyout(cm, ctlbuf, copylen);
1166                         cm->cmsg_len = oldclen;
1167                         if (error != 0)
1168                                 goto exit;
1169
1170                         ctlbuf += FREEBSD32_ALIGN(copylen);
1171                         len    -= FREEBSD32_ALIGN(copylen);
1172
1173                         copylen = datalen_out;
1174                         if (len < copylen) {
1175                                 msg->msg_flags |= MSG_CTRUNC;
1176                                 m_dispose_extcontrolm(m);
1177                                 break;
1178                         }
1179
1180                         /* Copy out the message data. */
1181                         error = copyout(data, ctlbuf, copylen);
1182                         if (error)
1183                                 goto exit;
1184
1185                         ctlbuf += FREEBSD32_ALIGN(copylen);
1186                         len    -= FREEBSD32_ALIGN(copylen);
1187
1188                         if (CMSG_SPACE(datalen) < clen) {
1189                                 clen -= CMSG_SPACE(datalen);
1190                                 cm = (struct cmsghdr *)
1191                                     ((caddr_t)cm + CMSG_SPACE(datalen));
1192                         } else {
1193                                 clen = 0;
1194                                 cm = NULL;
1195                         }
1196
1197                         msg->msg_controllen += FREEBSD32_ALIGN(sizeof(*cm)) +
1198                             datalen_out;
1199                 }
1200         }
1201         if (len == 0 && m != NULL) {
1202                 msg->msg_flags |= MSG_CTRUNC;
1203                 m_dispose_extcontrolm(m);
1204         }
1205
1206 exit:
1207         return (error);
1208 }
1209
1210 int
1211 freebsd32_recvmsg(td, uap)
1212         struct thread *td;
1213         struct freebsd32_recvmsg_args /* {
1214                 int     s;
1215                 struct  msghdr32 *msg;
1216                 int     flags;
1217         } */ *uap;
1218 {
1219         struct msghdr msg;
1220         struct msghdr32 m32;
1221         struct iovec *uiov, *iov;
1222         struct mbuf *control = NULL;
1223         struct mbuf **controlp;
1224
1225         int error;
1226         error = copyin(uap->msg, &m32, sizeof(m32));
1227         if (error)
1228                 return (error);
1229         error = freebsd32_copyinmsghdr(uap->msg, &msg);
1230         if (error)
1231                 return (error);
1232         error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1233             EMSGSIZE);
1234         if (error)
1235                 return (error);
1236         msg.msg_flags = uap->flags;
1237         uiov = msg.msg_iov;
1238         msg.msg_iov = iov;
1239
1240         controlp = (msg.msg_control != NULL) ?  &control : NULL;
1241         error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, controlp);
1242         if (error == 0) {
1243                 msg.msg_iov = uiov;
1244
1245                 if (control != NULL)
1246                         error = freebsd32_copy_msg_out(&msg, control);
1247                 else
1248                         msg.msg_controllen = 0;
1249
1250                 if (error == 0)
1251                         error = freebsd32_copyoutmsghdr(&msg, uap->msg);
1252         }
1253         free(iov, M_IOV);
1254
1255         if (control != NULL) {
1256                 if (error != 0)
1257                         m_dispose_extcontrolm(control);
1258                 m_freem(control);
1259         }
1260
1261         return (error);
1262 }
1263
1264 /*
1265  * Copy-in the array of control messages constructed using alignment
1266  * and padding suitable for a 32-bit environment and construct an
1267  * mbuf using alignment and padding suitable for a 64-bit kernel.
1268  * The alignment and padding are defined indirectly by CMSG_DATA(),
1269  * CMSG_SPACE() and CMSG_LEN().
1270  */
1271 static int
1272 freebsd32_copyin_control(struct mbuf **mp, caddr_t buf, u_int buflen)
1273 {
1274         struct mbuf *m;
1275         void *md;
1276         u_int idx, len, msglen;
1277         int error;
1278
1279         buflen = FREEBSD32_ALIGN(buflen);
1280
1281         if (buflen > MCLBYTES)
1282                 return (EINVAL);
1283
1284         /*
1285          * Iterate over the buffer and get the length of each message
1286          * in there. This has 32-bit alignment and padding. Use it to
1287          * determine the length of these messages when using 64-bit
1288          * alignment and padding.
1289          */
1290         idx = 0;
1291         len = 0;
1292         while (idx < buflen) {
1293                 error = copyin(buf + idx, &msglen, sizeof(msglen));
1294                 if (error)
1295                         return (error);
1296                 if (msglen < sizeof(struct cmsghdr))
1297                         return (EINVAL);
1298                 msglen = FREEBSD32_ALIGN(msglen);
1299                 if (idx + msglen > buflen)
1300                         return (EINVAL);
1301                 idx += msglen;
1302                 msglen += CMSG_ALIGN(sizeof(struct cmsghdr)) -
1303                     FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1304                 len += CMSG_ALIGN(msglen);
1305         }
1306
1307         if (len > MCLBYTES)
1308                 return (EINVAL);
1309
1310         m = m_get(M_WAITOK, MT_CONTROL);
1311         if (len > MLEN)
1312                 MCLGET(m, M_WAITOK);
1313         m->m_len = len;
1314
1315         md = mtod(m, void *);
1316         while (buflen > 0) {
1317                 error = copyin(buf, md, sizeof(struct cmsghdr));
1318                 if (error)
1319                         break;
1320                 msglen = *(u_int *)md;
1321                 msglen = FREEBSD32_ALIGN(msglen);
1322
1323                 /* Modify the message length to account for alignment. */
1324                 *(u_int *)md = msglen + CMSG_ALIGN(sizeof(struct cmsghdr)) -
1325                     FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1326
1327                 md = (char *)md + CMSG_ALIGN(sizeof(struct cmsghdr));
1328                 buf += FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1329                 buflen -= FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1330
1331                 msglen -= FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1332                 if (msglen > 0) {
1333                         error = copyin(buf, md, msglen);
1334                         if (error)
1335                                 break;
1336                         md = (char *)md + CMSG_ALIGN(msglen);
1337                         buf += msglen;
1338                         buflen -= msglen;
1339                 }
1340         }
1341
1342         if (error)
1343                 m_free(m);
1344         else
1345                 *mp = m;
1346         return (error);
1347 }
1348
1349 int
1350 freebsd32_sendmsg(struct thread *td,
1351                   struct freebsd32_sendmsg_args *uap)
1352 {
1353         struct msghdr msg;
1354         struct msghdr32 m32;
1355         struct iovec *iov;
1356         struct mbuf *control = NULL;
1357         struct sockaddr *to = NULL;
1358         int error;
1359
1360         error = copyin(uap->msg, &m32, sizeof(m32));
1361         if (error)
1362                 return (error);
1363         error = freebsd32_copyinmsghdr(uap->msg, &msg);
1364         if (error)
1365                 return (error);
1366         error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1367             EMSGSIZE);
1368         if (error)
1369                 return (error);
1370         msg.msg_iov = iov;
1371         if (msg.msg_name != NULL) {
1372                 error = getsockaddr(&to, msg.msg_name, msg.msg_namelen);
1373                 if (error) {
1374                         to = NULL;
1375                         goto out;
1376                 }
1377                 msg.msg_name = to;
1378         }
1379
1380         if (msg.msg_control) {
1381                 if (msg.msg_controllen < sizeof(struct cmsghdr)) {
1382                         error = EINVAL;
1383                         goto out;
1384                 }
1385
1386                 error = freebsd32_copyin_control(&control, msg.msg_control,
1387                     msg.msg_controllen);
1388                 if (error)
1389                         goto out;
1390
1391                 msg.msg_control = NULL;
1392                 msg.msg_controllen = 0;
1393         }
1394
1395         error = kern_sendit(td, uap->s, &msg, uap->flags, control,
1396             UIO_USERSPACE);
1397
1398 out:
1399         free(iov, M_IOV);
1400         if (to)
1401                 free(to, M_SONAME);
1402         return (error);
1403 }
1404
1405 int
1406 freebsd32_recvfrom(struct thread *td,
1407                    struct freebsd32_recvfrom_args *uap)
1408 {
1409         struct msghdr msg;
1410         struct iovec aiov;
1411         int error;
1412
1413         if (uap->fromlenaddr) {
1414                 error = copyin(PTRIN(uap->fromlenaddr), &msg.msg_namelen,
1415                     sizeof(msg.msg_namelen));
1416                 if (error)
1417                         return (error);
1418         } else {
1419                 msg.msg_namelen = 0;
1420         }
1421
1422         msg.msg_name = PTRIN(uap->from);
1423         msg.msg_iov = &aiov;
1424         msg.msg_iovlen = 1;
1425         aiov.iov_base = PTRIN(uap->buf);
1426         aiov.iov_len = uap->len;
1427         msg.msg_control = NULL;
1428         msg.msg_flags = uap->flags;
1429         error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, NULL);
1430         if (error == 0 && uap->fromlenaddr)
1431                 error = copyout(&msg.msg_namelen, PTRIN(uap->fromlenaddr),
1432                     sizeof (msg.msg_namelen));
1433         return (error);
1434 }
1435
1436 int
1437 freebsd32_settimeofday(struct thread *td,
1438                        struct freebsd32_settimeofday_args *uap)
1439 {
1440         struct timeval32 tv32;
1441         struct timeval tv, *tvp;
1442         struct timezone tz, *tzp;
1443         int error;
1444
1445         if (uap->tv) {
1446                 error = copyin(uap->tv, &tv32, sizeof(tv32));
1447                 if (error)
1448                         return (error);
1449                 CP(tv32, tv, tv_sec);
1450                 CP(tv32, tv, tv_usec);
1451                 tvp = &tv;
1452         } else
1453                 tvp = NULL;
1454         if (uap->tzp) {
1455                 error = copyin(uap->tzp, &tz, sizeof(tz));
1456                 if (error)
1457                         return (error);
1458                 tzp = &tz;
1459         } else
1460                 tzp = NULL;
1461         return (kern_settimeofday(td, tvp, tzp));
1462 }
1463
1464 int
1465 freebsd32_utimes(struct thread *td, struct freebsd32_utimes_args *uap)
1466 {
1467         struct timeval32 s32[2];
1468         struct timeval s[2], *sp;
1469         int error;
1470
1471         if (uap->tptr != NULL) {
1472                 error = copyin(uap->tptr, s32, sizeof(s32));
1473                 if (error)
1474                         return (error);
1475                 CP(s32[0], s[0], tv_sec);
1476                 CP(s32[0], s[0], tv_usec);
1477                 CP(s32[1], s[1], tv_sec);
1478                 CP(s32[1], s[1], tv_usec);
1479                 sp = s;
1480         } else
1481                 sp = NULL;
1482         return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1483             sp, UIO_SYSSPACE));
1484 }
1485
1486 int
1487 freebsd32_lutimes(struct thread *td, struct freebsd32_lutimes_args *uap)
1488 {
1489         struct timeval32 s32[2];
1490         struct timeval s[2], *sp;
1491         int error;
1492
1493         if (uap->tptr != NULL) {
1494                 error = copyin(uap->tptr, s32, sizeof(s32));
1495                 if (error)
1496                         return (error);
1497                 CP(s32[0], s[0], tv_sec);
1498                 CP(s32[0], s[0], tv_usec);
1499                 CP(s32[1], s[1], tv_sec);
1500                 CP(s32[1], s[1], tv_usec);
1501                 sp = s;
1502         } else
1503                 sp = NULL;
1504         return (kern_lutimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1505 }
1506
1507 int
1508 freebsd32_futimes(struct thread *td, struct freebsd32_futimes_args *uap)
1509 {
1510         struct timeval32 s32[2];
1511         struct timeval s[2], *sp;
1512         int error;
1513
1514         if (uap->tptr != NULL) {
1515                 error = copyin(uap->tptr, s32, sizeof(s32));
1516                 if (error)
1517                         return (error);
1518                 CP(s32[0], s[0], tv_sec);
1519                 CP(s32[0], s[0], tv_usec);
1520                 CP(s32[1], s[1], tv_sec);
1521                 CP(s32[1], s[1], tv_usec);
1522                 sp = s;
1523         } else
1524                 sp = NULL;
1525         return (kern_futimes(td, uap->fd, sp, UIO_SYSSPACE));
1526 }
1527
1528 int
1529 freebsd32_futimesat(struct thread *td, struct freebsd32_futimesat_args *uap)
1530 {
1531         struct timeval32 s32[2];
1532         struct timeval s[2], *sp;
1533         int error;
1534
1535         if (uap->times != NULL) {
1536                 error = copyin(uap->times, s32, sizeof(s32));
1537                 if (error)
1538                         return (error);
1539                 CP(s32[0], s[0], tv_sec);
1540                 CP(s32[0], s[0], tv_usec);
1541                 CP(s32[1], s[1], tv_sec);
1542                 CP(s32[1], s[1], tv_usec);
1543                 sp = s;
1544         } else
1545                 sp = NULL;
1546         return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
1547                 sp, UIO_SYSSPACE));
1548 }
1549
1550 int
1551 freebsd32_futimens(struct thread *td, struct freebsd32_futimens_args *uap)
1552 {
1553         struct timespec32 ts32[2];
1554         struct timespec ts[2], *tsp;
1555         int error;
1556
1557         if (uap->times != NULL) {
1558                 error = copyin(uap->times, ts32, sizeof(ts32));
1559                 if (error)
1560                         return (error);
1561                 CP(ts32[0], ts[0], tv_sec);
1562                 CP(ts32[0], ts[0], tv_nsec);
1563                 CP(ts32[1], ts[1], tv_sec);
1564                 CP(ts32[1], ts[1], tv_nsec);
1565                 tsp = ts;
1566         } else
1567                 tsp = NULL;
1568         return (kern_futimens(td, uap->fd, tsp, UIO_SYSSPACE));
1569 }
1570
1571 int
1572 freebsd32_utimensat(struct thread *td, struct freebsd32_utimensat_args *uap)
1573 {
1574         struct timespec32 ts32[2];
1575         struct timespec ts[2], *tsp;
1576         int error;
1577
1578         if (uap->times != NULL) {
1579                 error = copyin(uap->times, ts32, sizeof(ts32));
1580                 if (error)
1581                         return (error);
1582                 CP(ts32[0], ts[0], tv_sec);
1583                 CP(ts32[0], ts[0], tv_nsec);
1584                 CP(ts32[1], ts[1], tv_sec);
1585                 CP(ts32[1], ts[1], tv_nsec);
1586                 tsp = ts;
1587         } else
1588                 tsp = NULL;
1589         return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
1590             tsp, UIO_SYSSPACE, uap->flag));
1591 }
1592
1593 int
1594 freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap)
1595 {
1596         struct timeval32 tv32;
1597         struct timeval delta, olddelta, *deltap;
1598         int error;
1599
1600         if (uap->delta) {
1601                 error = copyin(uap->delta, &tv32, sizeof(tv32));
1602                 if (error)
1603                         return (error);
1604                 CP(tv32, delta, tv_sec);
1605                 CP(tv32, delta, tv_usec);
1606                 deltap = &delta;
1607         } else
1608                 deltap = NULL;
1609         error = kern_adjtime(td, deltap, &olddelta);
1610         if (uap->olddelta && error == 0) {
1611                 CP(olddelta, tv32, tv_sec);
1612                 CP(olddelta, tv32, tv_usec);
1613                 error = copyout(&tv32, uap->olddelta, sizeof(tv32));
1614         }
1615         return (error);
1616 }
1617
1618 #ifdef COMPAT_FREEBSD4
1619 int
1620 freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap)
1621 {
1622         struct statfs32 s32;
1623         struct statfs *sp;
1624         int error;
1625
1626         sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1627         error = kern_statfs(td, uap->path, UIO_USERSPACE, sp);
1628         if (error == 0) {
1629                 copy_statfs(sp, &s32);
1630                 error = copyout(&s32, uap->buf, sizeof(s32));
1631         }
1632         free(sp, M_STATFS);
1633         return (error);
1634 }
1635 #endif
1636
1637 #ifdef COMPAT_FREEBSD4
1638 int
1639 freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap)
1640 {
1641         struct statfs32 s32;
1642         struct statfs *sp;
1643         int error;
1644
1645         sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1646         error = kern_fstatfs(td, uap->fd, sp);
1647         if (error == 0) {
1648                 copy_statfs(sp, &s32);
1649                 error = copyout(&s32, uap->buf, sizeof(s32));
1650         }
1651         free(sp, M_STATFS);
1652         return (error);
1653 }
1654 #endif
1655
1656 #ifdef COMPAT_FREEBSD4
1657 int
1658 freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap)
1659 {
1660         struct statfs32 s32;
1661         struct statfs *sp;
1662         fhandle_t fh;
1663         int error;
1664
1665         if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
1666                 return (error);
1667         sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1668         error = kern_fhstatfs(td, fh, sp);
1669         if (error == 0) {
1670                 copy_statfs(sp, &s32);
1671                 error = copyout(&s32, uap->buf, sizeof(s32));
1672         }
1673         free(sp, M_STATFS);
1674         return (error);
1675 }
1676 #endif
1677
1678 int
1679 freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap)
1680 {
1681
1682         return (kern_pread(td, uap->fd, uap->buf, uap->nbyte,
1683             PAIR32TO64(off_t, uap->offset)));
1684 }
1685
1686 int
1687 freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap)
1688 {
1689
1690         return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte,
1691             PAIR32TO64(off_t, uap->offset)));
1692 }
1693
1694 #ifdef COMPAT_43
1695 int
1696 ofreebsd32_lseek(struct thread *td, struct ofreebsd32_lseek_args *uap)
1697 {
1698
1699         return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1700 }
1701 #endif
1702
1703 int
1704 freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap)
1705 {
1706         int error;
1707         off_t pos;
1708
1709         error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset),
1710             uap->whence);
1711         /* Expand the quad return into two parts for eax and edx */
1712         pos = td->td_uretoff.tdu_off;
1713         td->td_retval[RETVAL_LO] = pos & 0xffffffff;    /* %eax */
1714         td->td_retval[RETVAL_HI] = pos >> 32;           /* %edx */
1715         return error;
1716 }
1717
1718 int
1719 freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap)
1720 {
1721
1722         return (kern_truncate(td, uap->path, UIO_USERSPACE,
1723             PAIR32TO64(off_t, uap->length)));
1724 }
1725
1726 int
1727 freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap)
1728 {
1729
1730         return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length)));
1731 }
1732
1733 #ifdef COMPAT_43
1734 int
1735 ofreebsd32_getdirentries(struct thread *td,
1736     struct ofreebsd32_getdirentries_args *uap)
1737 {
1738         struct ogetdirentries_args ap;
1739         int error;
1740         long loff;
1741         int32_t loff_cut;
1742
1743         ap.fd = uap->fd;
1744         ap.buf = uap->buf;
1745         ap.count = uap->count;
1746         ap.basep = NULL;
1747         error = kern_ogetdirentries(td, &ap, &loff);
1748         if (error == 0) {
1749                 loff_cut = loff;
1750                 error = copyout(&loff_cut, uap->basep, sizeof(int32_t));
1751         }
1752         return (error);
1753 }
1754 #endif
1755
1756 #if defined(COMPAT_FREEBSD11)
1757 int
1758 freebsd11_freebsd32_getdirentries(struct thread *td,
1759     struct freebsd11_freebsd32_getdirentries_args *uap)
1760 {
1761         long base;
1762         int32_t base32;
1763         int error;
1764
1765         error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
1766             &base, NULL);
1767         if (error)
1768                 return (error);
1769         if (uap->basep != NULL) {
1770                 base32 = base;
1771                 error = copyout(&base32, uap->basep, sizeof(int32_t));
1772         }
1773         return (error);
1774 }
1775
1776 int
1777 freebsd11_freebsd32_getdents(struct thread *td,
1778     struct freebsd11_freebsd32_getdents_args *uap)
1779 {
1780         struct freebsd11_freebsd32_getdirentries_args ap;
1781
1782         ap.fd = uap->fd;
1783         ap.buf = uap->buf;
1784         ap.count = uap->count;
1785         ap.basep = NULL;
1786         return (freebsd11_freebsd32_getdirentries(td, &ap));
1787 }
1788 #endif /* COMPAT_FREEBSD11 */
1789
1790 #ifdef COMPAT_FREEBSD6
1791 /* versions with the 'int pad' argument */
1792 int
1793 freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap)
1794 {
1795
1796         return (kern_pread(td, uap->fd, uap->buf, uap->nbyte,
1797             PAIR32TO64(off_t, uap->offset)));
1798 }
1799
1800 int
1801 freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap)
1802 {
1803
1804         return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte,
1805             PAIR32TO64(off_t, uap->offset)));
1806 }
1807
1808 int
1809 freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap)
1810 {
1811         int error;
1812         off_t pos;
1813
1814         error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset),
1815             uap->whence);
1816         /* Expand the quad return into two parts for eax and edx */
1817         pos = *(off_t *)(td->td_retval);
1818         td->td_retval[RETVAL_LO] = pos & 0xffffffff;    /* %eax */
1819         td->td_retval[RETVAL_HI] = pos >> 32;           /* %edx */
1820         return error;
1821 }
1822
1823 int
1824 freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap)
1825 {
1826
1827         return (kern_truncate(td, uap->path, UIO_USERSPACE,
1828             PAIR32TO64(off_t, uap->length)));
1829 }
1830
1831 int
1832 freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap)
1833 {
1834
1835         return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length)));
1836 }
1837 #endif /* COMPAT_FREEBSD6 */
1838
1839 struct sf_hdtr32 {
1840         uint32_t headers;
1841         int hdr_cnt;
1842         uint32_t trailers;
1843         int trl_cnt;
1844 };
1845
1846 static int
1847 freebsd32_do_sendfile(struct thread *td,
1848     struct freebsd32_sendfile_args *uap, int compat)
1849 {
1850         struct sf_hdtr32 hdtr32;
1851         struct sf_hdtr hdtr;
1852         struct uio *hdr_uio, *trl_uio;
1853         struct file *fp;
1854         cap_rights_t rights;
1855         struct iovec32 *iov32;
1856         off_t offset, sbytes;
1857         int error;
1858
1859         offset = PAIR32TO64(off_t, uap->offset);
1860         if (offset < 0)
1861                 return (EINVAL);
1862
1863         hdr_uio = trl_uio = NULL;
1864
1865         if (uap->hdtr != NULL) {
1866                 error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
1867                 if (error)
1868                         goto out;
1869                 PTRIN_CP(hdtr32, hdtr, headers);
1870                 CP(hdtr32, hdtr, hdr_cnt);
1871                 PTRIN_CP(hdtr32, hdtr, trailers);
1872                 CP(hdtr32, hdtr, trl_cnt);
1873
1874                 if (hdtr.headers != NULL) {
1875                         iov32 = PTRIN(hdtr32.headers);
1876                         error = freebsd32_copyinuio(iov32,
1877                             hdtr32.hdr_cnt, &hdr_uio);
1878                         if (error)
1879                                 goto out;
1880 #ifdef COMPAT_FREEBSD4
1881                         /*
1882                          * In FreeBSD < 5.0 the nbytes to send also included
1883                          * the header.  If compat is specified subtract the
1884                          * header size from nbytes.
1885                          */
1886                         if (compat) {
1887                                 if (uap->nbytes > hdr_uio->uio_resid)
1888                                         uap->nbytes -= hdr_uio->uio_resid;
1889                                 else
1890                                         uap->nbytes = 0;
1891                         }
1892 #endif
1893                 }
1894                 if (hdtr.trailers != NULL) {
1895                         iov32 = PTRIN(hdtr32.trailers);
1896                         error = freebsd32_copyinuio(iov32,
1897                             hdtr32.trl_cnt, &trl_uio);
1898                         if (error)
1899                                 goto out;
1900                 }
1901         }
1902
1903         AUDIT_ARG_FD(uap->fd);
1904
1905         if ((error = fget_read(td, uap->fd,
1906             cap_rights_init(&rights, CAP_PREAD), &fp)) != 0)
1907                 goto out;
1908
1909         error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, offset,
1910             uap->nbytes, &sbytes, uap->flags, td);
1911         fdrop(fp, td);
1912
1913         if (uap->sbytes != NULL)
1914                 copyout(&sbytes, uap->sbytes, sizeof(off_t));
1915
1916 out:
1917         if (hdr_uio)
1918                 free(hdr_uio, M_IOV);
1919         if (trl_uio)
1920                 free(trl_uio, M_IOV);
1921         return (error);
1922 }
1923
1924 #ifdef COMPAT_FREEBSD4
1925 int
1926 freebsd4_freebsd32_sendfile(struct thread *td,
1927     struct freebsd4_freebsd32_sendfile_args *uap)
1928 {
1929         return (freebsd32_do_sendfile(td,
1930             (struct freebsd32_sendfile_args *)uap, 1));
1931 }
1932 #endif
1933
1934 int
1935 freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
1936 {
1937
1938         return (freebsd32_do_sendfile(td, uap, 0));
1939 }
1940
1941 static void
1942 copy_stat(struct stat *in, struct stat32 *out)
1943 {
1944
1945         CP(*in, *out, st_dev);
1946         CP(*in, *out, st_ino);
1947         CP(*in, *out, st_mode);
1948         CP(*in, *out, st_nlink);
1949         CP(*in, *out, st_uid);
1950         CP(*in, *out, st_gid);
1951         CP(*in, *out, st_rdev);
1952         TS_CP(*in, *out, st_atim);
1953         TS_CP(*in, *out, st_mtim);
1954         TS_CP(*in, *out, st_ctim);
1955         CP(*in, *out, st_size);
1956         CP(*in, *out, st_blocks);
1957         CP(*in, *out, st_blksize);
1958         CP(*in, *out, st_flags);
1959         CP(*in, *out, st_gen);
1960         TS_CP(*in, *out, st_birthtim);
1961         out->st_padding0 = 0;
1962         out->st_padding1 = 0;
1963 #ifdef __STAT32_TIME_T_EXT
1964         out->st_atim_ext = 0;
1965         out->st_mtim_ext = 0;
1966         out->st_ctim_ext = 0;
1967         out->st_btim_ext = 0;
1968 #endif
1969         bzero(out->st_spare, sizeof(out->st_spare));
1970 }
1971
1972 #ifdef COMPAT_43
1973 static void
1974 copy_ostat(struct stat *in, struct ostat32 *out)
1975 {
1976
1977         bzero(out, sizeof(*out));
1978         CP(*in, *out, st_dev);
1979         CP(*in, *out, st_ino);
1980         CP(*in, *out, st_mode);
1981         CP(*in, *out, st_nlink);
1982         CP(*in, *out, st_uid);
1983         CP(*in, *out, st_gid);
1984         CP(*in, *out, st_rdev);
1985         out->st_size = MIN(in->st_size, INT32_MAX);
1986         TS_CP(*in, *out, st_atim);
1987         TS_CP(*in, *out, st_mtim);
1988         TS_CP(*in, *out, st_ctim);
1989         CP(*in, *out, st_blksize);
1990         CP(*in, *out, st_blocks);
1991         CP(*in, *out, st_flags);
1992         CP(*in, *out, st_gen);
1993 }
1994 #endif
1995
1996 #ifdef COMPAT_43
1997 int
1998 ofreebsd32_stat(struct thread *td, struct ofreebsd32_stat_args *uap)
1999 {
2000         struct stat sb;
2001         struct ostat32 sb32;
2002         int error;
2003
2004         error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2005             &sb, NULL);
2006         if (error)
2007                 return (error);
2008         copy_ostat(&sb, &sb32);
2009         error = copyout(&sb32, uap->ub, sizeof (sb32));
2010         return (error);
2011 }
2012 #endif
2013
2014 int
2015 freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
2016 {
2017         struct stat ub;
2018         struct stat32 ub32;
2019         int error;
2020
2021         error = kern_fstat(td, uap->fd, &ub);
2022         if (error)
2023                 return (error);
2024         copy_stat(&ub, &ub32);
2025         error = copyout(&ub32, uap->ub, sizeof(ub32));
2026         return (error);
2027 }
2028
2029 #ifdef COMPAT_43
2030 int
2031 ofreebsd32_fstat(struct thread *td, struct ofreebsd32_fstat_args *uap)
2032 {
2033         struct stat ub;
2034         struct ostat32 ub32;
2035         int error;
2036
2037         error = kern_fstat(td, uap->fd, &ub);
2038         if (error)
2039                 return (error);
2040         copy_ostat(&ub, &ub32);
2041         error = copyout(&ub32, uap->ub, sizeof(ub32));
2042         return (error);
2043 }
2044 #endif
2045
2046 int
2047 freebsd32_fstatat(struct thread *td, struct freebsd32_fstatat_args *uap)
2048 {
2049         struct stat ub;
2050         struct stat32 ub32;
2051         int error;
2052
2053         error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE,
2054             &ub, NULL);
2055         if (error)
2056                 return (error);
2057         copy_stat(&ub, &ub32);
2058         error = copyout(&ub32, uap->buf, sizeof(ub32));
2059         return (error);
2060 }
2061
2062 #ifdef COMPAT_43
2063 int
2064 ofreebsd32_lstat(struct thread *td, struct ofreebsd32_lstat_args *uap)
2065 {
2066         struct stat sb;
2067         struct ostat32 sb32;
2068         int error;
2069
2070         error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2071             UIO_USERSPACE, &sb, NULL);
2072         if (error)
2073                 return (error);
2074         copy_ostat(&sb, &sb32);
2075         error = copyout(&sb32, uap->ub, sizeof (sb32));
2076         return (error);
2077 }
2078 #endif
2079
2080 int
2081 freebsd32_fhstat(struct thread *td, struct freebsd32_fhstat_args *uap)
2082 {
2083         struct stat sb;
2084         struct stat32 sb32;
2085         struct fhandle fh;
2086         int error;
2087
2088         error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2089         if (error != 0)
2090                 return (error);
2091         error = kern_fhstat(td, fh, &sb);
2092         if (error != 0)
2093                 return (error);
2094         copy_stat(&sb, &sb32);
2095         error = copyout(&sb32, uap->sb, sizeof (sb32));
2096         return (error);
2097 }
2098
2099 #if defined(COMPAT_FREEBSD11)
2100 extern int ino64_trunc_error;
2101
2102 static int
2103 freebsd11_cvtstat32(struct stat *in, struct freebsd11_stat32 *out)
2104 {
2105
2106         CP(*in, *out, st_ino);
2107         if (in->st_ino != out->st_ino) {
2108                 switch (ino64_trunc_error) {
2109                 default:
2110                 case 0:
2111                         break;
2112                 case 1:
2113                         return (EOVERFLOW);
2114                 case 2:
2115                         out->st_ino = UINT32_MAX;
2116                         break;
2117                 }
2118         }
2119         CP(*in, *out, st_nlink);
2120         if (in->st_nlink != out->st_nlink) {
2121                 switch (ino64_trunc_error) {
2122                 default:
2123                 case 0:
2124                         break;
2125                 case 1:
2126                         return (EOVERFLOW);
2127                 case 2:
2128                         out->st_nlink = UINT16_MAX;
2129                         break;
2130                 }
2131         }
2132         out->st_dev = in->st_dev;
2133         if (out->st_dev != in->st_dev) {
2134                 switch (ino64_trunc_error) {
2135                 default:
2136                         break;
2137                 case 1:
2138                         return (EOVERFLOW);
2139                 }
2140         }
2141         CP(*in, *out, st_mode);
2142         CP(*in, *out, st_uid);
2143         CP(*in, *out, st_gid);
2144         out->st_rdev = in->st_rdev;
2145         if (out->st_rdev != in->st_rdev) {
2146                 switch (ino64_trunc_error) {
2147                 default:
2148                         break;
2149                 case 1:
2150                         return (EOVERFLOW);
2151                 }
2152         }
2153         TS_CP(*in, *out, st_atim);
2154         TS_CP(*in, *out, st_mtim);
2155         TS_CP(*in, *out, st_ctim);
2156         CP(*in, *out, st_size);
2157         CP(*in, *out, st_blocks);
2158         CP(*in, *out, st_blksize);
2159         CP(*in, *out, st_flags);
2160         CP(*in, *out, st_gen);
2161         TS_CP(*in, *out, st_birthtim);
2162         out->st_lspare = 0;
2163         bzero((char *)&out->st_birthtim + sizeof(out->st_birthtim),
2164             sizeof(*out) - offsetof(struct freebsd11_stat32,
2165             st_birthtim) - sizeof(out->st_birthtim));
2166         return (0);
2167 }
2168
2169 int
2170 freebsd11_freebsd32_stat(struct thread *td,
2171     struct freebsd11_freebsd32_stat_args *uap)
2172 {
2173         struct stat sb;
2174         struct freebsd11_stat32 sb32;
2175         int error;
2176
2177         error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2178             &sb, NULL);
2179         if (error != 0)
2180                 return (error);
2181         error = freebsd11_cvtstat32(&sb, &sb32);
2182         if (error == 0)
2183                 error = copyout(&sb32, uap->ub, sizeof (sb32));
2184         return (error);
2185 }
2186
2187 int
2188 freebsd11_freebsd32_fstat(struct thread *td,
2189     struct freebsd11_freebsd32_fstat_args *uap)
2190 {
2191         struct stat sb;
2192         struct freebsd11_stat32 sb32;
2193         int error;
2194
2195         error = kern_fstat(td, uap->fd, &sb);
2196         if (error != 0)
2197                 return (error);
2198         error = freebsd11_cvtstat32(&sb, &sb32);
2199         if (error == 0)
2200                 error = copyout(&sb32, uap->ub, sizeof (sb32));
2201         return (error);
2202 }
2203
2204 int
2205 freebsd11_freebsd32_fstatat(struct thread *td,
2206     struct freebsd11_freebsd32_fstatat_args *uap)
2207 {
2208         struct stat sb;
2209         struct freebsd11_stat32 sb32;
2210         int error;
2211
2212         error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE,
2213             &sb, NULL);
2214         if (error != 0)
2215                 return (error);
2216         error = freebsd11_cvtstat32(&sb, &sb32);
2217         if (error == 0)
2218                 error = copyout(&sb32, uap->buf, sizeof (sb32));
2219         return (error);
2220 }
2221
2222 int
2223 freebsd11_freebsd32_lstat(struct thread *td,
2224     struct freebsd11_freebsd32_lstat_args *uap)
2225 {
2226         struct stat sb;
2227         struct freebsd11_stat32 sb32;
2228         int error;
2229
2230         error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2231             UIO_USERSPACE, &sb, NULL);
2232         if (error != 0)
2233                 return (error);
2234         error = freebsd11_cvtstat32(&sb, &sb32);
2235         if (error == 0)
2236                 error = copyout(&sb32, uap->ub, sizeof (sb32));
2237         return (error);
2238 }
2239
2240 int
2241 freebsd11_freebsd32_fhstat(struct thread *td,
2242     struct freebsd11_freebsd32_fhstat_args *uap)
2243 {
2244         struct stat sb;
2245         struct freebsd11_stat32 sb32;
2246         struct fhandle fh;
2247         int error;
2248
2249         error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2250         if (error != 0)
2251                 return (error);
2252         error = kern_fhstat(td, fh, &sb);
2253         if (error != 0)
2254                 return (error);
2255         error = freebsd11_cvtstat32(&sb, &sb32);
2256         if (error == 0)
2257                 error = copyout(&sb32, uap->sb, sizeof (sb32));
2258         return (error);
2259 }
2260 #endif
2261
2262 int
2263 freebsd32_sysctl(struct thread *td, struct freebsd32_sysctl_args *uap)
2264 {
2265         int error, name[CTL_MAXNAME];
2266         size_t j, oldlen;
2267         uint32_t tmp;
2268
2269         if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
2270                 return (EINVAL);
2271         error = copyin(uap->name, name, uap->namelen * sizeof(int));
2272         if (error)
2273                 return (error);
2274         if (uap->oldlenp) {
2275                 error = fueword32(uap->oldlenp, &tmp);
2276                 oldlen = tmp;
2277         } else {
2278                 oldlen = 0;
2279         }
2280         if (error != 0)
2281                 return (EFAULT);
2282         error = userland_sysctl(td, name, uap->namelen,
2283                 uap->old, &oldlen, 1,
2284                 uap->new, uap->newlen, &j, SCTL_MASK32);
2285         if (error)
2286                 return (error);
2287         if (uap->oldlenp)
2288                 suword32(uap->oldlenp, j);
2289         return (0);
2290 }
2291
2292 int
2293 freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap)
2294 {
2295         uint32_t version;
2296         int error;
2297         struct jail j;
2298
2299         error = copyin(uap->jail, &version, sizeof(uint32_t));
2300         if (error)
2301                 return (error);
2302
2303         switch (version) {
2304         case 0:
2305         {
2306                 /* FreeBSD single IPv4 jails. */
2307                 struct jail32_v0 j32_v0;
2308
2309                 bzero(&j, sizeof(struct jail));
2310                 error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
2311                 if (error)
2312                         return (error);
2313                 CP(j32_v0, j, version);
2314                 PTRIN_CP(j32_v0, j, path);
2315                 PTRIN_CP(j32_v0, j, hostname);
2316                 j.ip4s = htonl(j32_v0.ip_number);       /* jail_v0 is host order */
2317                 break;
2318         }
2319
2320         case 1:
2321                 /*
2322                  * Version 1 was used by multi-IPv4 jail implementations
2323                  * that never made it into the official kernel.
2324                  */
2325                 return (EINVAL);
2326
2327         case 2: /* JAIL_API_VERSION */
2328         {
2329                 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
2330                 struct jail32 j32;
2331
2332                 error = copyin(uap->jail, &j32, sizeof(struct jail32));
2333                 if (error)
2334                         return (error);
2335                 CP(j32, j, version);
2336                 PTRIN_CP(j32, j, path);
2337                 PTRIN_CP(j32, j, hostname);
2338                 PTRIN_CP(j32, j, jailname);
2339                 CP(j32, j, ip4s);
2340                 CP(j32, j, ip6s);
2341                 PTRIN_CP(j32, j, ip4);
2342                 PTRIN_CP(j32, j, ip6);
2343                 break;
2344         }
2345
2346         default:
2347                 /* Sci-Fi jails are not supported, sorry. */
2348                 return (EINVAL);
2349         }
2350         return (kern_jail(td, &j));
2351 }
2352
2353 int
2354 freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap)
2355 {
2356         struct uio *auio;
2357         int error;
2358
2359         /* Check that we have an even number of iovecs. */
2360         if (uap->iovcnt & 1)
2361                 return (EINVAL);
2362
2363         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2364         if (error)
2365                 return (error);
2366         error = kern_jail_set(td, auio, uap->flags);
2367         free(auio, M_IOV);
2368         return (error);
2369 }
2370
2371 int
2372 freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap)
2373 {
2374         struct iovec32 iov32;
2375         struct uio *auio;
2376         int error, i;
2377
2378         /* Check that we have an even number of iovecs. */
2379         if (uap->iovcnt & 1)
2380                 return (EINVAL);
2381
2382         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2383         if (error)
2384                 return (error);
2385         error = kern_jail_get(td, auio, uap->flags);
2386         if (error == 0)
2387                 for (i = 0; i < uap->iovcnt; i++) {
2388                         PTROUT_CP(auio->uio_iov[i], iov32, iov_base);
2389                         CP(auio->uio_iov[i], iov32, iov_len);
2390                         error = copyout(&iov32, uap->iovp + i, sizeof(iov32));
2391                         if (error != 0)
2392                                 break;
2393                 }
2394         free(auio, M_IOV);
2395         return (error);
2396 }
2397
2398 int
2399 freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
2400 {
2401         struct sigaction32 s32;
2402         struct sigaction sa, osa, *sap;
2403         int error;
2404
2405         if (uap->act) {
2406                 error = copyin(uap->act, &s32, sizeof(s32));
2407                 if (error)
2408                         return (error);
2409                 sa.sa_handler = PTRIN(s32.sa_u);
2410                 CP(s32, sa, sa_flags);
2411                 CP(s32, sa, sa_mask);
2412                 sap = &sa;
2413         } else
2414                 sap = NULL;
2415         error = kern_sigaction(td, uap->sig, sap, &osa, 0);
2416         if (error == 0 && uap->oact != NULL) {
2417                 s32.sa_u = PTROUT(osa.sa_handler);
2418                 CP(osa, s32, sa_flags);
2419                 CP(osa, s32, sa_mask);
2420                 error = copyout(&s32, uap->oact, sizeof(s32));
2421         }
2422         return (error);
2423 }
2424
2425 #ifdef COMPAT_FREEBSD4
2426 int
2427 freebsd4_freebsd32_sigaction(struct thread *td,
2428                              struct freebsd4_freebsd32_sigaction_args *uap)
2429 {
2430         struct sigaction32 s32;
2431         struct sigaction sa, osa, *sap;
2432         int error;
2433
2434         if (uap->act) {
2435                 error = copyin(uap->act, &s32, sizeof(s32));
2436                 if (error)
2437                         return (error);
2438                 sa.sa_handler = PTRIN(s32.sa_u);
2439                 CP(s32, sa, sa_flags);
2440                 CP(s32, sa, sa_mask);
2441                 sap = &sa;
2442         } else
2443                 sap = NULL;
2444         error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
2445         if (error == 0 && uap->oact != NULL) {
2446                 s32.sa_u = PTROUT(osa.sa_handler);
2447                 CP(osa, s32, sa_flags);
2448                 CP(osa, s32, sa_mask);
2449                 error = copyout(&s32, uap->oact, sizeof(s32));
2450         }
2451         return (error);
2452 }
2453 #endif
2454
2455 #ifdef COMPAT_43
2456 struct osigaction32 {
2457         u_int32_t       sa_u;
2458         osigset_t       sa_mask;
2459         int             sa_flags;
2460 };
2461
2462 #define ONSIG   32
2463
2464 int
2465 ofreebsd32_sigaction(struct thread *td,
2466                              struct ofreebsd32_sigaction_args *uap)
2467 {
2468         struct osigaction32 s32;
2469         struct sigaction sa, osa, *sap;
2470         int error;
2471
2472         if (uap->signum <= 0 || uap->signum >= ONSIG)
2473                 return (EINVAL);
2474
2475         if (uap->nsa) {
2476                 error = copyin(uap->nsa, &s32, sizeof(s32));
2477                 if (error)
2478                         return (error);
2479                 sa.sa_handler = PTRIN(s32.sa_u);
2480                 CP(s32, sa, sa_flags);
2481                 OSIG2SIG(s32.sa_mask, sa.sa_mask);
2482                 sap = &sa;
2483         } else
2484                 sap = NULL;
2485         error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2486         if (error == 0 && uap->osa != NULL) {
2487                 s32.sa_u = PTROUT(osa.sa_handler);
2488                 CP(osa, s32, sa_flags);
2489                 SIG2OSIG(osa.sa_mask, s32.sa_mask);
2490                 error = copyout(&s32, uap->osa, sizeof(s32));
2491         }
2492         return (error);
2493 }
2494
2495 int
2496 ofreebsd32_sigprocmask(struct thread *td,
2497                                struct ofreebsd32_sigprocmask_args *uap)
2498 {
2499         sigset_t set, oset;
2500         int error;
2501
2502         OSIG2SIG(uap->mask, set);
2503         error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD);
2504         SIG2OSIG(oset, td->td_retval[0]);
2505         return (error);
2506 }
2507
2508 int
2509 ofreebsd32_sigpending(struct thread *td,
2510                               struct ofreebsd32_sigpending_args *uap)
2511 {
2512         struct proc *p = td->td_proc;
2513         sigset_t siglist;
2514
2515         PROC_LOCK(p);
2516         siglist = p->p_siglist;
2517         SIGSETOR(siglist, td->td_siglist);
2518         PROC_UNLOCK(p);
2519         SIG2OSIG(siglist, td->td_retval[0]);
2520         return (0);
2521 }
2522
2523 struct sigvec32 {
2524         u_int32_t       sv_handler;
2525         int             sv_mask;
2526         int             sv_flags;
2527 };
2528
2529 int
2530 ofreebsd32_sigvec(struct thread *td,
2531                           struct ofreebsd32_sigvec_args *uap)
2532 {
2533         struct sigvec32 vec;
2534         struct sigaction sa, osa, *sap;
2535         int error;
2536
2537         if (uap->signum <= 0 || uap->signum >= ONSIG)
2538                 return (EINVAL);
2539
2540         if (uap->nsv) {
2541                 error = copyin(uap->nsv, &vec, sizeof(vec));
2542                 if (error)
2543                         return (error);
2544                 sa.sa_handler = PTRIN(vec.sv_handler);
2545                 OSIG2SIG(vec.sv_mask, sa.sa_mask);
2546                 sa.sa_flags = vec.sv_flags;
2547                 sa.sa_flags ^= SA_RESTART;
2548                 sap = &sa;
2549         } else
2550                 sap = NULL;
2551         error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2552         if (error == 0 && uap->osv != NULL) {
2553                 vec.sv_handler = PTROUT(osa.sa_handler);
2554                 SIG2OSIG(osa.sa_mask, vec.sv_mask);
2555                 vec.sv_flags = osa.sa_flags;
2556                 vec.sv_flags &= ~SA_NOCLDWAIT;
2557                 vec.sv_flags ^= SA_RESTART;
2558                 error = copyout(&vec, uap->osv, sizeof(vec));
2559         }
2560         return (error);
2561 }
2562
2563 int
2564 ofreebsd32_sigblock(struct thread *td,
2565                             struct ofreebsd32_sigblock_args *uap)
2566 {
2567         sigset_t set, oset;
2568
2569         OSIG2SIG(uap->mask, set);
2570         kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
2571         SIG2OSIG(oset, td->td_retval[0]);
2572         return (0);
2573 }
2574
2575 int
2576 ofreebsd32_sigsetmask(struct thread *td,
2577                               struct ofreebsd32_sigsetmask_args *uap)
2578 {
2579         sigset_t set, oset;
2580
2581         OSIG2SIG(uap->mask, set);
2582         kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
2583         SIG2OSIG(oset, td->td_retval[0]);
2584         return (0);
2585 }
2586
2587 int
2588 ofreebsd32_sigsuspend(struct thread *td,
2589                               struct ofreebsd32_sigsuspend_args *uap)
2590 {
2591         sigset_t mask;
2592
2593         OSIG2SIG(uap->mask, mask);
2594         return (kern_sigsuspend(td, mask));
2595 }
2596
2597 struct sigstack32 {
2598         u_int32_t       ss_sp;
2599         int             ss_onstack;
2600 };
2601
2602 int
2603 ofreebsd32_sigstack(struct thread *td,
2604                             struct ofreebsd32_sigstack_args *uap)
2605 {
2606         struct sigstack32 s32;
2607         struct sigstack nss, oss;
2608         int error = 0, unss;
2609
2610         if (uap->nss != NULL) {
2611                 error = copyin(uap->nss, &s32, sizeof(s32));
2612                 if (error)
2613                         return (error);
2614                 nss.ss_sp = PTRIN(s32.ss_sp);
2615                 CP(s32, nss, ss_onstack);
2616                 unss = 1;
2617         } else {
2618                 unss = 0;
2619         }
2620         oss.ss_sp = td->td_sigstk.ss_sp;
2621         oss.ss_onstack = sigonstack(cpu_getstack(td));
2622         if (unss) {
2623                 td->td_sigstk.ss_sp = nss.ss_sp;
2624                 td->td_sigstk.ss_size = 0;
2625                 td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2626                 td->td_pflags |= TDP_ALTSTACK;
2627         }
2628         if (uap->oss != NULL) {
2629                 s32.ss_sp = PTROUT(oss.ss_sp);
2630                 CP(oss, s32, ss_onstack);
2631                 error = copyout(&s32, uap->oss, sizeof(s32));
2632         }
2633         return (error);
2634 }
2635 #endif
2636
2637 int
2638 freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2639 {
2640
2641         return (freebsd32_user_clock_nanosleep(td, CLOCK_REALTIME,
2642             TIMER_RELTIME, uap->rqtp, uap->rmtp));
2643 }
2644
2645 int
2646 freebsd32_clock_nanosleep(struct thread *td,
2647     struct freebsd32_clock_nanosleep_args *uap)
2648 {
2649         int error;
2650
2651         error = freebsd32_user_clock_nanosleep(td, uap->clock_id, uap->flags,
2652             uap->rqtp, uap->rmtp);
2653         return (kern_posix_error(td, error));
2654 }
2655
2656 static int
2657 freebsd32_user_clock_nanosleep(struct thread *td, clockid_t clock_id,
2658     int flags, const struct timespec32 *ua_rqtp, struct timespec32 *ua_rmtp)
2659 {
2660         struct timespec32 rmt32, rqt32;
2661         struct timespec rmt, rqt;
2662         int error;
2663
2664         error = copyin(ua_rqtp, &rqt32, sizeof(rqt32));
2665         if (error)
2666                 return (error);
2667
2668         CP(rqt32, rqt, tv_sec);
2669         CP(rqt32, rqt, tv_nsec);
2670
2671         if (ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0 &&
2672             !useracc(ua_rmtp, sizeof(rmt32), VM_PROT_WRITE))
2673                 return (EFAULT);
2674         error = kern_clock_nanosleep(td, clock_id, flags, &rqt, &rmt);
2675         if (error == EINTR && ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0) {
2676                 int error2;
2677
2678                 CP(rmt, rmt32, tv_sec);
2679                 CP(rmt, rmt32, tv_nsec);
2680
2681                 error2 = copyout(&rmt32, ua_rmtp, sizeof(rmt32));
2682                 if (error2)
2683                         error = error2;
2684         }
2685         return (error);
2686 }
2687
2688 int
2689 freebsd32_clock_gettime(struct thread *td,
2690                         struct freebsd32_clock_gettime_args *uap)
2691 {
2692         struct timespec ats;
2693         struct timespec32 ats32;
2694         int error;
2695
2696         error = kern_clock_gettime(td, uap->clock_id, &ats);
2697         if (error == 0) {
2698                 CP(ats, ats32, tv_sec);
2699                 CP(ats, ats32, tv_nsec);
2700                 error = copyout(&ats32, uap->tp, sizeof(ats32));
2701         }
2702         return (error);
2703 }
2704
2705 int
2706 freebsd32_clock_settime(struct thread *td,
2707                         struct freebsd32_clock_settime_args *uap)
2708 {
2709         struct timespec ats;
2710         struct timespec32 ats32;
2711         int error;
2712
2713         error = copyin(uap->tp, &ats32, sizeof(ats32));
2714         if (error)
2715                 return (error);
2716         CP(ats32, ats, tv_sec);
2717         CP(ats32, ats, tv_nsec);
2718
2719         return (kern_clock_settime(td, uap->clock_id, &ats));
2720 }
2721
2722 int
2723 freebsd32_clock_getres(struct thread *td,
2724                        struct freebsd32_clock_getres_args *uap)
2725 {
2726         struct timespec ts;
2727         struct timespec32 ts32;
2728         int error;
2729
2730         if (uap->tp == NULL)
2731                 return (0);
2732         error = kern_clock_getres(td, uap->clock_id, &ts);
2733         if (error == 0) {
2734                 CP(ts, ts32, tv_sec);
2735                 CP(ts, ts32, tv_nsec);
2736                 error = copyout(&ts32, uap->tp, sizeof(ts32));
2737         }
2738         return (error);
2739 }
2740
2741 int freebsd32_ktimer_create(struct thread *td,
2742     struct freebsd32_ktimer_create_args *uap)
2743 {
2744         struct sigevent32 ev32;
2745         struct sigevent ev, *evp;
2746         int error, id;
2747
2748         if (uap->evp == NULL) {
2749                 evp = NULL;
2750         } else {
2751                 evp = &ev;
2752                 error = copyin(uap->evp, &ev32, sizeof(ev32));
2753                 if (error != 0)
2754                         return (error);
2755                 error = convert_sigevent32(&ev32, &ev);
2756                 if (error != 0)
2757                         return (error);
2758         }
2759         error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
2760         if (error == 0) {
2761                 error = copyout(&id, uap->timerid, sizeof(int));
2762                 if (error != 0)
2763                         kern_ktimer_delete(td, id);
2764         }
2765         return (error);
2766 }
2767
2768 int
2769 freebsd32_ktimer_settime(struct thread *td,
2770     struct freebsd32_ktimer_settime_args *uap)
2771 {
2772         struct itimerspec32 val32, oval32;
2773         struct itimerspec val, oval, *ovalp;
2774         int error;
2775
2776         error = copyin(uap->value, &val32, sizeof(val32));
2777         if (error != 0)
2778                 return (error);
2779         ITS_CP(val32, val);
2780         ovalp = uap->ovalue != NULL ? &oval : NULL;
2781         error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
2782         if (error == 0 && uap->ovalue != NULL) {
2783                 ITS_CP(oval, oval32);
2784                 error = copyout(&oval32, uap->ovalue, sizeof(oval32));
2785         }
2786         return (error);
2787 }
2788
2789 int
2790 freebsd32_ktimer_gettime(struct thread *td,
2791     struct freebsd32_ktimer_gettime_args *uap)
2792 {
2793         struct itimerspec32 val32;
2794         struct itimerspec val;
2795         int error;
2796
2797         error = kern_ktimer_gettime(td, uap->timerid, &val);
2798         if (error == 0) {
2799                 ITS_CP(val, val32);
2800                 error = copyout(&val32, uap->value, sizeof(val32));
2801         }
2802         return (error);
2803 }
2804
2805 int
2806 freebsd32_clock_getcpuclockid2(struct thread *td,
2807     struct freebsd32_clock_getcpuclockid2_args *uap)
2808 {
2809         clockid_t clk_id;
2810         int error;
2811
2812         error = kern_clock_getcpuclockid2(td, PAIR32TO64(id_t, uap->id),
2813             uap->which, &clk_id);
2814         if (error == 0)
2815                 error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
2816         return (error);
2817 }
2818
2819 int
2820 freebsd32_thr_new(struct thread *td,
2821                   struct freebsd32_thr_new_args *uap)
2822 {
2823         struct thr_param32 param32;
2824         struct thr_param param;
2825         int error;
2826
2827         if (uap->param_size < 0 ||
2828             uap->param_size > sizeof(struct thr_param32))
2829                 return (EINVAL);
2830         bzero(&param, sizeof(struct thr_param));
2831         bzero(&param32, sizeof(struct thr_param32));
2832         error = copyin(uap->param, &param32, uap->param_size);
2833         if (error != 0)
2834                 return (error);
2835         param.start_func = PTRIN(param32.start_func);
2836         param.arg = PTRIN(param32.arg);
2837         param.stack_base = PTRIN(param32.stack_base);
2838         param.stack_size = param32.stack_size;
2839         param.tls_base = PTRIN(param32.tls_base);
2840         param.tls_size = param32.tls_size;
2841         param.child_tid = PTRIN(param32.child_tid);
2842         param.parent_tid = PTRIN(param32.parent_tid);
2843         param.flags = param32.flags;
2844         param.rtp = PTRIN(param32.rtp);
2845         param.spare[0] = PTRIN(param32.spare[0]);
2846         param.spare[1] = PTRIN(param32.spare[1]);
2847         param.spare[2] = PTRIN(param32.spare[2]);
2848
2849         return (kern_thr_new(td, &param));
2850 }
2851
2852 int
2853 freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
2854 {
2855         struct timespec32 ts32;
2856         struct timespec ts, *tsp;
2857         int error;
2858
2859         error = 0;
2860         tsp = NULL;
2861         if (uap->timeout != NULL) {
2862                 error = copyin((const void *)uap->timeout, (void *)&ts32,
2863                     sizeof(struct timespec32));
2864                 if (error != 0)
2865                         return (error);
2866                 ts.tv_sec = ts32.tv_sec;
2867                 ts.tv_nsec = ts32.tv_nsec;
2868                 tsp = &ts;
2869         }
2870         return (kern_thr_suspend(td, tsp));
2871 }
2872
2873 void
2874 siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst)
2875 {
2876         bzero(dst, sizeof(*dst));
2877         dst->si_signo = src->si_signo;
2878         dst->si_errno = src->si_errno;
2879         dst->si_code = src->si_code;
2880         dst->si_pid = src->si_pid;
2881         dst->si_uid = src->si_uid;
2882         dst->si_status = src->si_status;
2883         dst->si_addr = (uintptr_t)src->si_addr;
2884         dst->si_value.sival_int = src->si_value.sival_int;
2885         dst->si_timerid = src->si_timerid;
2886         dst->si_overrun = src->si_overrun;
2887 }
2888
2889 #ifndef _FREEBSD32_SYSPROTO_H_
2890 struct freebsd32_sigqueue_args {
2891         pid_t pid;
2892         int signum;
2893         /* union sigval32 */ int value;
2894 };
2895 #endif
2896 int
2897 freebsd32_sigqueue(struct thread *td, struct freebsd32_sigqueue_args *uap)
2898 {
2899         union sigval sv;
2900
2901         /*
2902          * On 32-bit ABIs, sival_int and sival_ptr are the same.
2903          * On 64-bit little-endian ABIs, the low bits are the same.
2904          * In 64-bit big-endian ABIs, sival_int overlaps with
2905          * sival_ptr's HIGH bits.  We choose to support sival_int
2906          * rather than sival_ptr in this case as it seems to be
2907          * more common.
2908          */
2909         bzero(&sv, sizeof(sv));
2910         sv.sival_int = uap->value;
2911
2912         return (kern_sigqueue(td, uap->pid, uap->signum, &sv));
2913 }
2914
2915 int
2916 freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
2917 {
2918         struct timespec32 ts32;
2919         struct timespec ts;
2920         struct timespec *timeout;
2921         sigset_t set;
2922         ksiginfo_t ksi;
2923         struct siginfo32 si32;
2924         int error;
2925
2926         if (uap->timeout) {
2927                 error = copyin(uap->timeout, &ts32, sizeof(ts32));
2928                 if (error)
2929                         return (error);
2930                 ts.tv_sec = ts32.tv_sec;
2931                 ts.tv_nsec = ts32.tv_nsec;
2932                 timeout = &ts;
2933         } else
2934                 timeout = NULL;
2935
2936         error = copyin(uap->set, &set, sizeof(set));
2937         if (error)
2938                 return (error);
2939
2940         error = kern_sigtimedwait(td, set, &ksi, timeout);
2941         if (error)
2942                 return (error);
2943
2944         if (uap->info) {
2945                 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2946                 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2947         }
2948
2949         if (error == 0)
2950                 td->td_retval[0] = ksi.ksi_signo;
2951         return (error);
2952 }
2953
2954 /*
2955  * MPSAFE
2956  */
2957 int
2958 freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
2959 {
2960         ksiginfo_t ksi;
2961         struct siginfo32 si32;
2962         sigset_t set;
2963         int error;
2964
2965         error = copyin(uap->set, &set, sizeof(set));
2966         if (error)
2967                 return (error);
2968
2969         error = kern_sigtimedwait(td, set, &ksi, NULL);
2970         if (error)
2971                 return (error);
2972
2973         if (uap->info) {
2974                 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2975                 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2976         }       
2977         if (error == 0)
2978                 td->td_retval[0] = ksi.ksi_signo;
2979         return (error);
2980 }
2981
2982 int
2983 freebsd32_cpuset_setid(struct thread *td,
2984     struct freebsd32_cpuset_setid_args *uap)
2985 {
2986
2987         return (kern_cpuset_setid(td, uap->which,
2988             PAIR32TO64(id_t, uap->id), uap->setid));
2989 }
2990
2991 int
2992 freebsd32_cpuset_getid(struct thread *td,
2993     struct freebsd32_cpuset_getid_args *uap)
2994 {
2995
2996         return (kern_cpuset_getid(td, uap->level, uap->which,
2997             PAIR32TO64(id_t, uap->id), uap->setid));
2998 }
2999
3000 int
3001 freebsd32_cpuset_getaffinity(struct thread *td,
3002     struct freebsd32_cpuset_getaffinity_args *uap)
3003 {
3004
3005         return (kern_cpuset_getaffinity(td, uap->level, uap->which,
3006             PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask));
3007 }
3008
3009 int
3010 freebsd32_cpuset_setaffinity(struct thread *td,
3011     struct freebsd32_cpuset_setaffinity_args *uap)
3012 {
3013
3014         return (kern_cpuset_setaffinity(td, uap->level, uap->which,
3015             PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask));
3016 }
3017
3018 int
3019 freebsd32_cpuset_getdomain(struct thread *td,
3020     struct freebsd32_cpuset_getdomain_args *uap)
3021 {
3022
3023         return (kern_cpuset_getdomain(td, uap->level, uap->which,
3024             PAIR32TO64(id_t,uap->id), uap->domainsetsize, uap->mask, uap->policy));
3025 }
3026
3027 int
3028 freebsd32_cpuset_setdomain(struct thread *td,
3029     struct freebsd32_cpuset_setdomain_args *uap)
3030 {
3031
3032         return (kern_cpuset_setdomain(td, uap->level, uap->which,
3033             PAIR32TO64(id_t,uap->id), uap->domainsetsize, uap->mask, uap->policy));
3034 }
3035
3036 int
3037 freebsd32_nmount(struct thread *td,
3038     struct freebsd32_nmount_args /* {
3039         struct iovec *iovp;
3040         unsigned int iovcnt;
3041         int flags;
3042     } */ *uap)
3043 {
3044         struct uio *auio;
3045         uint64_t flags;
3046         int error;
3047
3048         /*
3049          * Mount flags are now 64-bits. On 32-bit archtectures only
3050          * 32-bits are passed in, but from here on everything handles
3051          * 64-bit flags correctly.
3052          */
3053         flags = uap->flags;
3054
3055         AUDIT_ARG_FFLAGS(flags);
3056
3057         /*
3058          * Filter out MNT_ROOTFS.  We do not want clients of nmount() in
3059          * userspace to set this flag, but we must filter it out if we want
3060          * MNT_UPDATE on the root file system to work.
3061          * MNT_ROOTFS should only be set by the kernel when mounting its
3062          * root file system.
3063          */
3064         flags &= ~MNT_ROOTFS;
3065
3066         /*
3067          * check that we have an even number of iovec's
3068          * and that we have at least two options.
3069          */
3070         if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
3071                 return (EINVAL);
3072
3073         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
3074         if (error)
3075                 return (error);
3076         error = vfs_donmount(td, flags, auio);
3077
3078         free(auio, M_IOV);
3079         return error;
3080 }
3081
3082 #if 0
3083 int
3084 freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
3085 {
3086         struct yyy32 *p32, s32;
3087         struct yyy *p = NULL, s;
3088         struct xxx_arg ap;
3089         int error;
3090
3091         if (uap->zzz) {
3092                 error = copyin(uap->zzz, &s32, sizeof(s32));
3093                 if (error)
3094                         return (error);
3095                 /* translate in */
3096                 p = &s;
3097         }
3098         error = kern_xxx(td, p);
3099         if (error)
3100                 return (error);
3101         if (uap->zzz) {
3102                 /* translate out */
3103                 error = copyout(&s32, p32, sizeof(s32));
3104         }
3105         return (error);
3106 }
3107 #endif
3108
3109 int
3110 syscall32_module_handler(struct module *mod, int what, void *arg)
3111 {
3112
3113         return (kern_syscall_module_handler(freebsd32_sysent, mod, what, arg));
3114 }
3115
3116 int
3117 syscall32_helper_register(struct syscall_helper_data *sd, int flags)
3118 {
3119
3120         return (kern_syscall_helper_register(freebsd32_sysent, sd, flags));
3121 }
3122
3123 int
3124 syscall32_helper_unregister(struct syscall_helper_data *sd)
3125 {
3126
3127         return (kern_syscall_helper_unregister(freebsd32_sysent, sd));
3128 }
3129
3130 register_t *
3131 freebsd32_copyout_strings(struct image_params *imgp)
3132 {
3133         int argc, envc, i;
3134         u_int32_t *vectp;
3135         char *stringp;
3136         uintptr_t destp;
3137         u_int32_t *stack_base;
3138         struct freebsd32_ps_strings *arginfo;
3139         char canary[sizeof(long) * 8];
3140         int32_t pagesizes32[MAXPAGESIZES];
3141         size_t execpath_len;
3142         int szsigcode;
3143
3144         /*
3145          * Calculate string base and vector table pointers.
3146          * Also deal with signal trampoline code for this exec type.
3147          */
3148         if (imgp->execpath != NULL && imgp->auxargs != NULL)
3149                 execpath_len = strlen(imgp->execpath) + 1;
3150         else
3151                 execpath_len = 0;
3152         arginfo = (struct freebsd32_ps_strings *)curproc->p_sysent->
3153             sv_psstrings;
3154         if (imgp->proc->p_sysent->sv_sigcode_base == 0)
3155                 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
3156         else
3157                 szsigcode = 0;
3158         destp = (uintptr_t)arginfo;
3159
3160         /*
3161          * install sigcode
3162          */
3163         if (szsigcode != 0) {
3164                 destp -= szsigcode;
3165                 destp = rounddown2(destp, sizeof(uint32_t));
3166                 copyout(imgp->proc->p_sysent->sv_sigcode, (void *)destp,
3167                     szsigcode);
3168         }
3169
3170         /*
3171          * Copy the image path for the rtld.
3172          */
3173         if (execpath_len != 0) {
3174                 destp -= execpath_len;
3175                 imgp->execpathp = destp;
3176                 copyout(imgp->execpath, (void *)destp, execpath_len);
3177         }
3178
3179         /*
3180          * Prepare the canary for SSP.
3181          */
3182         arc4rand(canary, sizeof(canary), 0);
3183         destp -= sizeof(canary);
3184         imgp->canary = destp;
3185         copyout(canary, (void *)destp, sizeof(canary));
3186         imgp->canarylen = sizeof(canary);
3187
3188         /*
3189          * Prepare the pagesizes array.
3190          */
3191         for (i = 0; i < MAXPAGESIZES; i++)
3192                 pagesizes32[i] = (uint32_t)pagesizes[i];
3193         destp -= sizeof(pagesizes32);
3194         destp = rounddown2(destp, sizeof(uint32_t));
3195         imgp->pagesizes = destp;
3196         copyout(pagesizes32, (void *)destp, sizeof(pagesizes32));
3197         imgp->pagesizeslen = sizeof(pagesizes32);
3198
3199         destp -= ARG_MAX - imgp->args->stringspace;
3200         destp = rounddown2(destp, sizeof(uint32_t));
3201
3202         vectp = (uint32_t *)destp;
3203         if (imgp->auxargs) {
3204                 /*
3205                  * Allocate room on the stack for the ELF auxargs
3206                  * array.  It has up to AT_COUNT entries.
3207                  */
3208                 vectp -= howmany(AT_COUNT * sizeof(Elf32_Auxinfo),
3209                     sizeof(*vectp));
3210         }
3211
3212         /*
3213          * Allocate room for the argv[] and env vectors including the
3214          * terminating NULL pointers.
3215          */
3216         vectp -= imgp->args->argc + 1 + imgp->args->envc + 1;
3217
3218         /*
3219          * vectp also becomes our initial stack base
3220          */
3221         stack_base = vectp;
3222
3223         stringp = imgp->args->begin_argv;
3224         argc = imgp->args->argc;
3225         envc = imgp->args->envc;
3226         /*
3227          * Copy out strings - arguments and environment.
3228          */
3229         copyout(stringp, (void *)destp, ARG_MAX - imgp->args->stringspace);
3230
3231         /*
3232          * Fill in "ps_strings" struct for ps, w, etc.
3233          */
3234         suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp);
3235         suword32(&arginfo->ps_nargvstr, argc);
3236
3237         /*
3238          * Fill in argument portion of vector table.
3239          */
3240         for (; argc > 0; --argc) {
3241                 suword32(vectp++, (u_int32_t)(intptr_t)destp);
3242                 while (*stringp++ != 0)
3243                         destp++;
3244                 destp++;
3245         }
3246
3247         /* a null vector table pointer separates the argp's from the envp's */
3248         suword32(vectp++, 0);
3249
3250         suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp);
3251         suword32(&arginfo->ps_nenvstr, envc);
3252
3253         /*
3254          * Fill in environment portion of vector table.
3255          */
3256         for (; envc > 0; --envc) {
3257                 suword32(vectp++, (u_int32_t)(intptr_t)destp);
3258                 while (*stringp++ != 0)
3259                         destp++;
3260                 destp++;
3261         }
3262
3263         /* end of vector table is a null pointer */
3264         suword32(vectp, 0);
3265
3266         return ((register_t *)stack_base);
3267 }
3268
3269 int
3270 freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
3271 {
3272         struct kld_file_stat *stat;
3273         struct kld32_file_stat *stat32;
3274         int error, version;
3275
3276         if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
3277             != 0)
3278                 return (error);
3279         if (version != sizeof(struct kld32_file_stat_1) &&
3280             version != sizeof(struct kld32_file_stat))
3281                 return (EINVAL);
3282
3283         stat = malloc(sizeof(*stat), M_TEMP, M_WAITOK | M_ZERO);
3284         stat32 = malloc(sizeof(*stat32), M_TEMP, M_WAITOK | M_ZERO);
3285         error = kern_kldstat(td, uap->fileid, stat);
3286         if (error == 0) {
3287                 bcopy(&stat->name[0], &stat32->name[0], sizeof(stat->name));
3288                 CP(*stat, *stat32, refs);
3289                 CP(*stat, *stat32, id);
3290                 PTROUT_CP(*stat, *stat32, address);
3291                 CP(*stat, *stat32, size);
3292                 bcopy(&stat->pathname[0], &stat32->pathname[0],
3293                     sizeof(stat->pathname));
3294                 stat32->version  = version;
3295                 error = copyout(stat32, uap->stat, version);
3296         }
3297         free(stat, M_TEMP);
3298         free(stat32, M_TEMP);
3299         return (error);
3300 }
3301
3302 int
3303 freebsd32_posix_fallocate(struct thread *td,
3304     struct freebsd32_posix_fallocate_args *uap)
3305 {
3306         int error;
3307
3308         error = kern_posix_fallocate(td, uap->fd,
3309             PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len));
3310         return (kern_posix_error(td, error));
3311 }
3312
3313 int
3314 freebsd32_posix_fadvise(struct thread *td,
3315     struct freebsd32_posix_fadvise_args *uap)
3316 {
3317         int error;
3318
3319         error = kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset),
3320             PAIR32TO64(off_t, uap->len), uap->advice);
3321         return (kern_posix_error(td, error));
3322 }
3323
3324 int
3325 convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig)
3326 {
3327
3328         CP(*sig32, *sig, sigev_notify);
3329         switch (sig->sigev_notify) {
3330         case SIGEV_NONE:
3331                 break;
3332         case SIGEV_THREAD_ID:
3333                 CP(*sig32, *sig, sigev_notify_thread_id);
3334                 /* FALLTHROUGH */
3335         case SIGEV_SIGNAL:
3336                 CP(*sig32, *sig, sigev_signo);
3337                 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3338                 break;
3339         case SIGEV_KEVENT:
3340                 CP(*sig32, *sig, sigev_notify_kqueue);
3341                 CP(*sig32, *sig, sigev_notify_kevent_flags);
3342                 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3343                 break;
3344         default:
3345                 return (EINVAL);
3346         }
3347         return (0);
3348 }
3349
3350 int
3351 freebsd32_procctl(struct thread *td, struct freebsd32_procctl_args *uap)
3352 {
3353         void *data;
3354         union {
3355                 struct procctl_reaper_status rs;
3356                 struct procctl_reaper_pids rp;
3357                 struct procctl_reaper_kill rk;
3358         } x;
3359         union {
3360                 struct procctl_reaper_pids32 rp;
3361         } x32;
3362         int error, error1, flags, signum;
3363
3364         switch (uap->com) {
3365         case PROC_SPROTECT:
3366         case PROC_TRACE_CTL:
3367         case PROC_TRAPCAP_CTL:
3368                 error = copyin(PTRIN(uap->data), &flags, sizeof(flags));
3369                 if (error != 0)
3370                         return (error);
3371                 data = &flags;
3372                 break;
3373         case PROC_REAP_ACQUIRE:
3374         case PROC_REAP_RELEASE:
3375                 if (uap->data != NULL)
3376                         return (EINVAL);
3377                 data = NULL;
3378                 break;
3379         case PROC_REAP_STATUS:
3380                 data = &x.rs;
3381                 break;
3382         case PROC_REAP_GETPIDS:
3383                 error = copyin(uap->data, &x32.rp, sizeof(x32.rp));
3384                 if (error != 0)
3385                         return (error);
3386                 CP(x32.rp, x.rp, rp_count);
3387                 PTRIN_CP(x32.rp, x.rp, rp_pids);
3388                 data = &x.rp;
3389                 break;
3390         case PROC_REAP_KILL:
3391                 error = copyin(uap->data, &x.rk, sizeof(x.rk));
3392                 if (error != 0)
3393                         return (error);
3394                 data = &x.rk;
3395                 break;
3396         case PROC_TRACE_STATUS:
3397         case PROC_TRAPCAP_STATUS:
3398                 data = &flags;
3399                 break;
3400         case PROC_PDEATHSIG_CTL:
3401                 error = copyin(uap->data, &signum, sizeof(signum));
3402                 if (error != 0)
3403                         return (error);
3404                 data = &signum;
3405                 break;
3406         case PROC_PDEATHSIG_STATUS:
3407                 data = &signum;
3408                 break;
3409         default:
3410                 return (EINVAL);
3411         }
3412         error = kern_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id),
3413             uap->com, data);
3414         switch (uap->com) {
3415         case PROC_REAP_STATUS:
3416                 if (error == 0)
3417                         error = copyout(&x.rs, uap->data, sizeof(x.rs));
3418                 break;
3419         case PROC_REAP_KILL:
3420                 error1 = copyout(&x.rk, uap->data, sizeof(x.rk));
3421                 if (error == 0)
3422                         error = error1;
3423                 break;
3424         case PROC_TRACE_STATUS:
3425         case PROC_TRAPCAP_STATUS:
3426                 if (error == 0)
3427                         error = copyout(&flags, uap->data, sizeof(flags));
3428                 break;
3429         case PROC_PDEATHSIG_STATUS:
3430                 if (error == 0)
3431                         error = copyout(&signum, uap->data, sizeof(signum));
3432                 break;
3433         }
3434         return (error);
3435 }
3436
3437 int
3438 freebsd32_fcntl(struct thread *td, struct freebsd32_fcntl_args *uap)
3439 {
3440         long tmp;
3441
3442         switch (uap->cmd) {
3443         /*
3444          * Do unsigned conversion for arg when operation
3445          * interprets it as flags or pointer.
3446          */
3447         case F_SETLK_REMOTE:
3448         case F_SETLKW:
3449         case F_SETLK:
3450         case F_GETLK:
3451         case F_SETFD:
3452         case F_SETFL:
3453         case F_OGETLK:
3454         case F_OSETLK:
3455         case F_OSETLKW:
3456                 tmp = (unsigned int)(uap->arg);
3457                 break;
3458         default:
3459                 tmp = uap->arg;
3460                 break;
3461         }
3462         return (kern_fcntl_freebsd(td, uap->fd, uap->cmd, tmp));
3463 }
3464
3465 int
3466 freebsd32_ppoll(struct thread *td, struct freebsd32_ppoll_args *uap)
3467 {
3468         struct timespec32 ts32;
3469         struct timespec ts, *tsp;
3470         sigset_t set, *ssp;
3471         int error;
3472
3473         if (uap->ts != NULL) {
3474                 error = copyin(uap->ts, &ts32, sizeof(ts32));
3475                 if (error != 0)
3476                         return (error);
3477                 CP(ts32, ts, tv_sec);
3478                 CP(ts32, ts, tv_nsec);
3479                 tsp = &ts;
3480         } else
3481                 tsp = NULL;
3482         if (uap->set != NULL) {
3483                 error = copyin(uap->set, &set, sizeof(set));
3484                 if (error != 0)
3485                         return (error);
3486                 ssp = &set;
3487         } else
3488                 ssp = NULL;
3489
3490         return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp));
3491 }
3492
3493 int
3494 freebsd32_sched_rr_get_interval(struct thread *td,
3495     struct freebsd32_sched_rr_get_interval_args *uap)
3496 {
3497         struct timespec ts;
3498         struct timespec32 ts32;
3499         int error;
3500
3501         error = kern_sched_rr_get_interval(td, uap->pid, &ts);
3502         if (error == 0) {
3503                 CP(ts, ts32, tv_sec);
3504                 CP(ts, ts32, tv_nsec);
3505                 error = copyout(&ts32, uap->interval, sizeof(ts32));
3506         }
3507         return (error);
3508 }