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