]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/freebsd32/freebsd32_misc.c
Fix sendmsg(2) privilege escalation.
[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_jail(struct thread *td, struct freebsd32_jail_args *uap)
2295 {
2296         uint32_t version;
2297         int error;
2298         struct jail j;
2299
2300         error = copyin(uap->jail, &version, sizeof(uint32_t));
2301         if (error)
2302                 return (error);
2303
2304         switch (version) {
2305         case 0:
2306         {
2307                 /* FreeBSD single IPv4 jails. */
2308                 struct jail32_v0 j32_v0;
2309
2310                 bzero(&j, sizeof(struct jail));
2311                 error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
2312                 if (error)
2313                         return (error);
2314                 CP(j32_v0, j, version);
2315                 PTRIN_CP(j32_v0, j, path);
2316                 PTRIN_CP(j32_v0, j, hostname);
2317                 j.ip4s = htonl(j32_v0.ip_number);       /* jail_v0 is host order */
2318                 break;
2319         }
2320
2321         case 1:
2322                 /*
2323                  * Version 1 was used by multi-IPv4 jail implementations
2324                  * that never made it into the official kernel.
2325                  */
2326                 return (EINVAL);
2327
2328         case 2: /* JAIL_API_VERSION */
2329         {
2330                 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
2331                 struct jail32 j32;
2332
2333                 error = copyin(uap->jail, &j32, sizeof(struct jail32));
2334                 if (error)
2335                         return (error);
2336                 CP(j32, j, version);
2337                 PTRIN_CP(j32, j, path);
2338                 PTRIN_CP(j32, j, hostname);
2339                 PTRIN_CP(j32, j, jailname);
2340                 CP(j32, j, ip4s);
2341                 CP(j32, j, ip6s);
2342                 PTRIN_CP(j32, j, ip4);
2343                 PTRIN_CP(j32, j, ip6);
2344                 break;
2345         }
2346
2347         default:
2348                 /* Sci-Fi jails are not supported, sorry. */
2349                 return (EINVAL);
2350         }
2351         return (kern_jail(td, &j));
2352 }
2353
2354 int
2355 freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap)
2356 {
2357         struct uio *auio;
2358         int error;
2359
2360         /* Check that we have an even number of iovecs. */
2361         if (uap->iovcnt & 1)
2362                 return (EINVAL);
2363
2364         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2365         if (error)
2366                 return (error);
2367         error = kern_jail_set(td, auio, uap->flags);
2368         free(auio, M_IOV);
2369         return (error);
2370 }
2371
2372 int
2373 freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap)
2374 {
2375         struct iovec32 iov32;
2376         struct uio *auio;
2377         int error, i;
2378
2379         /* Check that we have an even number of iovecs. */
2380         if (uap->iovcnt & 1)
2381                 return (EINVAL);
2382
2383         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2384         if (error)
2385                 return (error);
2386         error = kern_jail_get(td, auio, uap->flags);
2387         if (error == 0)
2388                 for (i = 0; i < uap->iovcnt; i++) {
2389                         PTROUT_CP(auio->uio_iov[i], iov32, iov_base);
2390                         CP(auio->uio_iov[i], iov32, iov_len);
2391                         error = copyout(&iov32, uap->iovp + i, sizeof(iov32));
2392                         if (error != 0)
2393                                 break;
2394                 }
2395         free(auio, M_IOV);
2396         return (error);
2397 }
2398
2399 int
2400 freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
2401 {
2402         struct sigaction32 s32;
2403         struct sigaction sa, osa, *sap;
2404         int error;
2405
2406         if (uap->act) {
2407                 error = copyin(uap->act, &s32, sizeof(s32));
2408                 if (error)
2409                         return (error);
2410                 sa.sa_handler = PTRIN(s32.sa_u);
2411                 CP(s32, sa, sa_flags);
2412                 CP(s32, sa, sa_mask);
2413                 sap = &sa;
2414         } else
2415                 sap = NULL;
2416         error = kern_sigaction(td, uap->sig, sap, &osa, 0);
2417         if (error == 0 && uap->oact != NULL) {
2418                 s32.sa_u = PTROUT(osa.sa_handler);
2419                 CP(osa, s32, sa_flags);
2420                 CP(osa, s32, sa_mask);
2421                 error = copyout(&s32, uap->oact, sizeof(s32));
2422         }
2423         return (error);
2424 }
2425
2426 #ifdef COMPAT_FREEBSD4
2427 int
2428 freebsd4_freebsd32_sigaction(struct thread *td,
2429                              struct freebsd4_freebsd32_sigaction_args *uap)
2430 {
2431         struct sigaction32 s32;
2432         struct sigaction sa, osa, *sap;
2433         int error;
2434
2435         if (uap->act) {
2436                 error = copyin(uap->act, &s32, sizeof(s32));
2437                 if (error)
2438                         return (error);
2439                 sa.sa_handler = PTRIN(s32.sa_u);
2440                 CP(s32, sa, sa_flags);
2441                 CP(s32, sa, sa_mask);
2442                 sap = &sa;
2443         } else
2444                 sap = NULL;
2445         error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
2446         if (error == 0 && uap->oact != NULL) {
2447                 s32.sa_u = PTROUT(osa.sa_handler);
2448                 CP(osa, s32, sa_flags);
2449                 CP(osa, s32, sa_mask);
2450                 error = copyout(&s32, uap->oact, sizeof(s32));
2451         }
2452         return (error);
2453 }
2454 #endif
2455
2456 #ifdef COMPAT_43
2457 struct osigaction32 {
2458         u_int32_t       sa_u;
2459         osigset_t       sa_mask;
2460         int             sa_flags;
2461 };
2462
2463 #define ONSIG   32
2464
2465 int
2466 ofreebsd32_sigaction(struct thread *td,
2467                              struct ofreebsd32_sigaction_args *uap)
2468 {
2469         struct osigaction32 s32;
2470         struct sigaction sa, osa, *sap;
2471         int error;
2472
2473         if (uap->signum <= 0 || uap->signum >= ONSIG)
2474                 return (EINVAL);
2475
2476         if (uap->nsa) {
2477                 error = copyin(uap->nsa, &s32, sizeof(s32));
2478                 if (error)
2479                         return (error);
2480                 sa.sa_handler = PTRIN(s32.sa_u);
2481                 CP(s32, sa, sa_flags);
2482                 OSIG2SIG(s32.sa_mask, sa.sa_mask);
2483                 sap = &sa;
2484         } else
2485                 sap = NULL;
2486         error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2487         if (error == 0 && uap->osa != NULL) {
2488                 s32.sa_u = PTROUT(osa.sa_handler);
2489                 CP(osa, s32, sa_flags);
2490                 SIG2OSIG(osa.sa_mask, s32.sa_mask);
2491                 error = copyout(&s32, uap->osa, sizeof(s32));
2492         }
2493         return (error);
2494 }
2495
2496 int
2497 ofreebsd32_sigprocmask(struct thread *td,
2498                                struct ofreebsd32_sigprocmask_args *uap)
2499 {
2500         sigset_t set, oset;
2501         int error;
2502
2503         OSIG2SIG(uap->mask, set);
2504         error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD);
2505         SIG2OSIG(oset, td->td_retval[0]);
2506         return (error);
2507 }
2508
2509 int
2510 ofreebsd32_sigpending(struct thread *td,
2511                               struct ofreebsd32_sigpending_args *uap)
2512 {
2513         struct proc *p = td->td_proc;
2514         sigset_t siglist;
2515
2516         PROC_LOCK(p);
2517         siglist = p->p_siglist;
2518         SIGSETOR(siglist, td->td_siglist);
2519         PROC_UNLOCK(p);
2520         SIG2OSIG(siglist, td->td_retval[0]);
2521         return (0);
2522 }
2523
2524 struct sigvec32 {
2525         u_int32_t       sv_handler;
2526         int             sv_mask;
2527         int             sv_flags;
2528 };
2529
2530 int
2531 ofreebsd32_sigvec(struct thread *td,
2532                           struct ofreebsd32_sigvec_args *uap)
2533 {
2534         struct sigvec32 vec;
2535         struct sigaction sa, osa, *sap;
2536         int error;
2537
2538         if (uap->signum <= 0 || uap->signum >= ONSIG)
2539                 return (EINVAL);
2540
2541         if (uap->nsv) {
2542                 error = copyin(uap->nsv, &vec, sizeof(vec));
2543                 if (error)
2544                         return (error);
2545                 sa.sa_handler = PTRIN(vec.sv_handler);
2546                 OSIG2SIG(vec.sv_mask, sa.sa_mask);
2547                 sa.sa_flags = vec.sv_flags;
2548                 sa.sa_flags ^= SA_RESTART;
2549                 sap = &sa;
2550         } else
2551                 sap = NULL;
2552         error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2553         if (error == 0 && uap->osv != NULL) {
2554                 vec.sv_handler = PTROUT(osa.sa_handler);
2555                 SIG2OSIG(osa.sa_mask, vec.sv_mask);
2556                 vec.sv_flags = osa.sa_flags;
2557                 vec.sv_flags &= ~SA_NOCLDWAIT;
2558                 vec.sv_flags ^= SA_RESTART;
2559                 error = copyout(&vec, uap->osv, sizeof(vec));
2560         }
2561         return (error);
2562 }
2563
2564 int
2565 ofreebsd32_sigblock(struct thread *td,
2566                             struct ofreebsd32_sigblock_args *uap)
2567 {
2568         sigset_t set, oset;
2569
2570         OSIG2SIG(uap->mask, set);
2571         kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
2572         SIG2OSIG(oset, td->td_retval[0]);
2573         return (0);
2574 }
2575
2576 int
2577 ofreebsd32_sigsetmask(struct thread *td,
2578                               struct ofreebsd32_sigsetmask_args *uap)
2579 {
2580         sigset_t set, oset;
2581
2582         OSIG2SIG(uap->mask, set);
2583         kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
2584         SIG2OSIG(oset, td->td_retval[0]);
2585         return (0);
2586 }
2587
2588 int
2589 ofreebsd32_sigsuspend(struct thread *td,
2590                               struct ofreebsd32_sigsuspend_args *uap)
2591 {
2592         sigset_t mask;
2593
2594         OSIG2SIG(uap->mask, mask);
2595         return (kern_sigsuspend(td, mask));
2596 }
2597
2598 struct sigstack32 {
2599         u_int32_t       ss_sp;
2600         int             ss_onstack;
2601 };
2602
2603 int
2604 ofreebsd32_sigstack(struct thread *td,
2605                             struct ofreebsd32_sigstack_args *uap)
2606 {
2607         struct sigstack32 s32;
2608         struct sigstack nss, oss;
2609         int error = 0, unss;
2610
2611         if (uap->nss != NULL) {
2612                 error = copyin(uap->nss, &s32, sizeof(s32));
2613                 if (error)
2614                         return (error);
2615                 nss.ss_sp = PTRIN(s32.ss_sp);
2616                 CP(s32, nss, ss_onstack);
2617                 unss = 1;
2618         } else {
2619                 unss = 0;
2620         }
2621         oss.ss_sp = td->td_sigstk.ss_sp;
2622         oss.ss_onstack = sigonstack(cpu_getstack(td));
2623         if (unss) {
2624                 td->td_sigstk.ss_sp = nss.ss_sp;
2625                 td->td_sigstk.ss_size = 0;
2626                 td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2627                 td->td_pflags |= TDP_ALTSTACK;
2628         }
2629         if (uap->oss != NULL) {
2630                 s32.ss_sp = PTROUT(oss.ss_sp);
2631                 CP(oss, s32, ss_onstack);
2632                 error = copyout(&s32, uap->oss, sizeof(s32));
2633         }
2634         return (error);
2635 }
2636 #endif
2637
2638 int
2639 freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2640 {
2641
2642         return (freebsd32_user_clock_nanosleep(td, CLOCK_REALTIME,
2643             TIMER_RELTIME, uap->rqtp, uap->rmtp));
2644 }
2645
2646 int
2647 freebsd32_clock_nanosleep(struct thread *td,
2648     struct freebsd32_clock_nanosleep_args *uap)
2649 {
2650         int error;
2651
2652         error = freebsd32_user_clock_nanosleep(td, uap->clock_id, uap->flags,
2653             uap->rqtp, uap->rmtp);
2654         return (kern_posix_error(td, error));
2655 }
2656
2657 static int
2658 freebsd32_user_clock_nanosleep(struct thread *td, clockid_t clock_id,
2659     int flags, const struct timespec32 *ua_rqtp, struct timespec32 *ua_rmtp)
2660 {
2661         struct timespec32 rmt32, rqt32;
2662         struct timespec rmt, rqt;
2663         int error;
2664
2665         error = copyin(ua_rqtp, &rqt32, sizeof(rqt32));
2666         if (error)
2667                 return (error);
2668
2669         CP(rqt32, rqt, tv_sec);
2670         CP(rqt32, rqt, tv_nsec);
2671
2672         if (ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0 &&
2673             !useracc(ua_rmtp, sizeof(rmt32), VM_PROT_WRITE))
2674                 return (EFAULT);
2675         error = kern_clock_nanosleep(td, clock_id, flags, &rqt, &rmt);
2676         if (error == EINTR && ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0) {
2677                 int error2;
2678
2679                 CP(rmt, rmt32, tv_sec);
2680                 CP(rmt, rmt32, tv_nsec);
2681
2682                 error2 = copyout(&rmt32, ua_rmtp, sizeof(rmt32));
2683                 if (error2)
2684                         error = error2;
2685         }
2686         return (error);
2687 }
2688
2689 int
2690 freebsd32_clock_gettime(struct thread *td,
2691                         struct freebsd32_clock_gettime_args *uap)
2692 {
2693         struct timespec ats;
2694         struct timespec32 ats32;
2695         int error;
2696
2697         error = kern_clock_gettime(td, uap->clock_id, &ats);
2698         if (error == 0) {
2699                 CP(ats, ats32, tv_sec);
2700                 CP(ats, ats32, tv_nsec);
2701                 error = copyout(&ats32, uap->tp, sizeof(ats32));
2702         }
2703         return (error);
2704 }
2705
2706 int
2707 freebsd32_clock_settime(struct thread *td,
2708                         struct freebsd32_clock_settime_args *uap)
2709 {
2710         struct timespec ats;
2711         struct timespec32 ats32;
2712         int error;
2713
2714         error = copyin(uap->tp, &ats32, sizeof(ats32));
2715         if (error)
2716                 return (error);
2717         CP(ats32, ats, tv_sec);
2718         CP(ats32, ats, tv_nsec);
2719
2720         return (kern_clock_settime(td, uap->clock_id, &ats));
2721 }
2722
2723 int
2724 freebsd32_clock_getres(struct thread *td,
2725                        struct freebsd32_clock_getres_args *uap)
2726 {
2727         struct timespec ts;
2728         struct timespec32 ts32;
2729         int error;
2730
2731         if (uap->tp == NULL)
2732                 return (0);
2733         error = kern_clock_getres(td, uap->clock_id, &ts);
2734         if (error == 0) {
2735                 CP(ts, ts32, tv_sec);
2736                 CP(ts, ts32, tv_nsec);
2737                 error = copyout(&ts32, uap->tp, sizeof(ts32));
2738         }
2739         return (error);
2740 }
2741
2742 int freebsd32_ktimer_create(struct thread *td,
2743     struct freebsd32_ktimer_create_args *uap)
2744 {
2745         struct sigevent32 ev32;
2746         struct sigevent ev, *evp;
2747         int error, id;
2748
2749         if (uap->evp == NULL) {
2750                 evp = NULL;
2751         } else {
2752                 evp = &ev;
2753                 error = copyin(uap->evp, &ev32, sizeof(ev32));
2754                 if (error != 0)
2755                         return (error);
2756                 error = convert_sigevent32(&ev32, &ev);
2757                 if (error != 0)
2758                         return (error);
2759         }
2760         error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
2761         if (error == 0) {
2762                 error = copyout(&id, uap->timerid, sizeof(int));
2763                 if (error != 0)
2764                         kern_ktimer_delete(td, id);
2765         }
2766         return (error);
2767 }
2768
2769 int
2770 freebsd32_ktimer_settime(struct thread *td,
2771     struct freebsd32_ktimer_settime_args *uap)
2772 {
2773         struct itimerspec32 val32, oval32;
2774         struct itimerspec val, oval, *ovalp;
2775         int error;
2776
2777         error = copyin(uap->value, &val32, sizeof(val32));
2778         if (error != 0)
2779                 return (error);
2780         ITS_CP(val32, val);
2781         ovalp = uap->ovalue != NULL ? &oval : NULL;
2782         error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
2783         if (error == 0 && uap->ovalue != NULL) {
2784                 ITS_CP(oval, oval32);
2785                 error = copyout(&oval32, uap->ovalue, sizeof(oval32));
2786         }
2787         return (error);
2788 }
2789
2790 int
2791 freebsd32_ktimer_gettime(struct thread *td,
2792     struct freebsd32_ktimer_gettime_args *uap)
2793 {
2794         struct itimerspec32 val32;
2795         struct itimerspec val;
2796         int error;
2797
2798         error = kern_ktimer_gettime(td, uap->timerid, &val);
2799         if (error == 0) {
2800                 ITS_CP(val, val32);
2801                 error = copyout(&val32, uap->value, sizeof(val32));
2802         }
2803         return (error);
2804 }
2805
2806 int
2807 freebsd32_clock_getcpuclockid2(struct thread *td,
2808     struct freebsd32_clock_getcpuclockid2_args *uap)
2809 {
2810         clockid_t clk_id;
2811         int error;
2812
2813         error = kern_clock_getcpuclockid2(td, PAIR32TO64(id_t, uap->id),
2814             uap->which, &clk_id);
2815         if (error == 0)
2816                 error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
2817         return (error);
2818 }
2819
2820 int
2821 freebsd32_thr_new(struct thread *td,
2822                   struct freebsd32_thr_new_args *uap)
2823 {
2824         struct thr_param32 param32;
2825         struct thr_param param;
2826         int error;
2827
2828         if (uap->param_size < 0 ||
2829             uap->param_size > sizeof(struct thr_param32))
2830                 return (EINVAL);
2831         bzero(&param, sizeof(struct thr_param));
2832         bzero(&param32, sizeof(struct thr_param32));
2833         error = copyin(uap->param, &param32, uap->param_size);
2834         if (error != 0)
2835                 return (error);
2836         param.start_func = PTRIN(param32.start_func);
2837         param.arg = PTRIN(param32.arg);
2838         param.stack_base = PTRIN(param32.stack_base);
2839         param.stack_size = param32.stack_size;
2840         param.tls_base = PTRIN(param32.tls_base);
2841         param.tls_size = param32.tls_size;
2842         param.child_tid = PTRIN(param32.child_tid);
2843         param.parent_tid = PTRIN(param32.parent_tid);
2844         param.flags = param32.flags;
2845         param.rtp = PTRIN(param32.rtp);
2846         param.spare[0] = PTRIN(param32.spare[0]);
2847         param.spare[1] = PTRIN(param32.spare[1]);
2848         param.spare[2] = PTRIN(param32.spare[2]);
2849
2850         return (kern_thr_new(td, &param));
2851 }
2852
2853 int
2854 freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
2855 {
2856         struct timespec32 ts32;
2857         struct timespec ts, *tsp;
2858         int error;
2859
2860         error = 0;
2861         tsp = NULL;
2862         if (uap->timeout != NULL) {
2863                 error = copyin((const void *)uap->timeout, (void *)&ts32,
2864                     sizeof(struct timespec32));
2865                 if (error != 0)
2866                         return (error);
2867                 ts.tv_sec = ts32.tv_sec;
2868                 ts.tv_nsec = ts32.tv_nsec;
2869                 tsp = &ts;
2870         }
2871         return (kern_thr_suspend(td, tsp));
2872 }
2873
2874 void
2875 siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst)
2876 {
2877         bzero(dst, sizeof(*dst));
2878         dst->si_signo = src->si_signo;
2879         dst->si_errno = src->si_errno;
2880         dst->si_code = src->si_code;
2881         dst->si_pid = src->si_pid;
2882         dst->si_uid = src->si_uid;
2883         dst->si_status = src->si_status;
2884         dst->si_addr = (uintptr_t)src->si_addr;
2885         dst->si_value.sival_int = src->si_value.sival_int;
2886         dst->si_timerid = src->si_timerid;
2887         dst->si_overrun = src->si_overrun;
2888 }
2889
2890 #ifndef _FREEBSD32_SYSPROTO_H_
2891 struct freebsd32_sigqueue_args {
2892         pid_t pid;
2893         int signum;
2894         /* union sigval32 */ int value;
2895 };
2896 #endif
2897 int
2898 freebsd32_sigqueue(struct thread *td, struct freebsd32_sigqueue_args *uap)
2899 {
2900         union sigval sv;
2901
2902         /*
2903          * On 32-bit ABIs, sival_int and sival_ptr are the same.
2904          * On 64-bit little-endian ABIs, the low bits are the same.
2905          * In 64-bit big-endian ABIs, sival_int overlaps with
2906          * sival_ptr's HIGH bits.  We choose to support sival_int
2907          * rather than sival_ptr in this case as it seems to be
2908          * more common.
2909          */
2910         bzero(&sv, sizeof(sv));
2911         sv.sival_int = uap->value;
2912
2913         return (kern_sigqueue(td, uap->pid, uap->signum, &sv));
2914 }
2915
2916 int
2917 freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
2918 {
2919         struct timespec32 ts32;
2920         struct timespec ts;
2921         struct timespec *timeout;
2922         sigset_t set;
2923         ksiginfo_t ksi;
2924         struct siginfo32 si32;
2925         int error;
2926
2927         if (uap->timeout) {
2928                 error = copyin(uap->timeout, &ts32, sizeof(ts32));
2929                 if (error)
2930                         return (error);
2931                 ts.tv_sec = ts32.tv_sec;
2932                 ts.tv_nsec = ts32.tv_nsec;
2933                 timeout = &ts;
2934         } else
2935                 timeout = NULL;
2936
2937         error = copyin(uap->set, &set, sizeof(set));
2938         if (error)
2939                 return (error);
2940
2941         error = kern_sigtimedwait(td, set, &ksi, timeout);
2942         if (error)
2943                 return (error);
2944
2945         if (uap->info) {
2946                 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2947                 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2948         }
2949
2950         if (error == 0)
2951                 td->td_retval[0] = ksi.ksi_signo;
2952         return (error);
2953 }
2954
2955 /*
2956  * MPSAFE
2957  */
2958 int
2959 freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
2960 {
2961         ksiginfo_t ksi;
2962         struct siginfo32 si32;
2963         sigset_t set;
2964         int error;
2965
2966         error = copyin(uap->set, &set, sizeof(set));
2967         if (error)
2968                 return (error);
2969
2970         error = kern_sigtimedwait(td, set, &ksi, NULL);
2971         if (error)
2972                 return (error);
2973
2974         if (uap->info) {
2975                 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2976                 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2977         }       
2978         if (error == 0)
2979                 td->td_retval[0] = ksi.ksi_signo;
2980         return (error);
2981 }
2982
2983 int
2984 freebsd32_cpuset_setid(struct thread *td,
2985     struct freebsd32_cpuset_setid_args *uap)
2986 {
2987
2988         return (kern_cpuset_setid(td, uap->which,
2989             PAIR32TO64(id_t, uap->id), uap->setid));
2990 }
2991
2992 int
2993 freebsd32_cpuset_getid(struct thread *td,
2994     struct freebsd32_cpuset_getid_args *uap)
2995 {
2996
2997         return (kern_cpuset_getid(td, uap->level, uap->which,
2998             PAIR32TO64(id_t, uap->id), uap->setid));
2999 }
3000
3001 int
3002 freebsd32_cpuset_getaffinity(struct thread *td,
3003     struct freebsd32_cpuset_getaffinity_args *uap)
3004 {
3005
3006         return (kern_cpuset_getaffinity(td, uap->level, uap->which,
3007             PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask));
3008 }
3009
3010 int
3011 freebsd32_cpuset_setaffinity(struct thread *td,
3012     struct freebsd32_cpuset_setaffinity_args *uap)
3013 {
3014
3015         return (kern_cpuset_setaffinity(td, uap->level, uap->which,
3016             PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask));
3017 }
3018
3019 int
3020 freebsd32_cpuset_getdomain(struct thread *td,
3021     struct freebsd32_cpuset_getdomain_args *uap)
3022 {
3023
3024         return (kern_cpuset_getdomain(td, uap->level, uap->which,
3025             PAIR32TO64(id_t,uap->id), uap->domainsetsize, uap->mask, uap->policy));
3026 }
3027
3028 int
3029 freebsd32_cpuset_setdomain(struct thread *td,
3030     struct freebsd32_cpuset_setdomain_args *uap)
3031 {
3032
3033         return (kern_cpuset_setdomain(td, uap->level, uap->which,
3034             PAIR32TO64(id_t,uap->id), uap->domainsetsize, uap->mask, uap->policy));
3035 }
3036
3037 int
3038 freebsd32_nmount(struct thread *td,
3039     struct freebsd32_nmount_args /* {
3040         struct iovec *iovp;
3041         unsigned int iovcnt;
3042         int flags;
3043     } */ *uap)
3044 {
3045         struct uio *auio;
3046         uint64_t flags;
3047         int error;
3048
3049         /*
3050          * Mount flags are now 64-bits. On 32-bit archtectures only
3051          * 32-bits are passed in, but from here on everything handles
3052          * 64-bit flags correctly.
3053          */
3054         flags = uap->flags;
3055
3056         AUDIT_ARG_FFLAGS(flags);
3057
3058         /*
3059          * Filter out MNT_ROOTFS.  We do not want clients of nmount() in
3060          * userspace to set this flag, but we must filter it out if we want
3061          * MNT_UPDATE on the root file system to work.
3062          * MNT_ROOTFS should only be set by the kernel when mounting its
3063          * root file system.
3064          */
3065         flags &= ~MNT_ROOTFS;
3066
3067         /*
3068          * check that we have an even number of iovec's
3069          * and that we have at least two options.
3070          */
3071         if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
3072                 return (EINVAL);
3073
3074         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
3075         if (error)
3076                 return (error);
3077         error = vfs_donmount(td, flags, auio);
3078
3079         free(auio, M_IOV);
3080         return error;
3081 }
3082
3083 #if 0
3084 int
3085 freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
3086 {
3087         struct yyy32 *p32, s32;
3088         struct yyy *p = NULL, s;
3089         struct xxx_arg ap;
3090         int error;
3091
3092         if (uap->zzz) {
3093                 error = copyin(uap->zzz, &s32, sizeof(s32));
3094                 if (error)
3095                         return (error);
3096                 /* translate in */
3097                 p = &s;
3098         }
3099         error = kern_xxx(td, p);
3100         if (error)
3101                 return (error);
3102         if (uap->zzz) {
3103                 /* translate out */
3104                 error = copyout(&s32, p32, sizeof(s32));
3105         }
3106         return (error);
3107 }
3108 #endif
3109
3110 int
3111 syscall32_module_handler(struct module *mod, int what, void *arg)
3112 {
3113
3114         return (kern_syscall_module_handler(freebsd32_sysent, mod, what, arg));
3115 }
3116
3117 int
3118 syscall32_helper_register(struct syscall_helper_data *sd, int flags)
3119 {
3120
3121         return (kern_syscall_helper_register(freebsd32_sysent, sd, flags));
3122 }
3123
3124 int
3125 syscall32_helper_unregister(struct syscall_helper_data *sd)
3126 {
3127
3128         return (kern_syscall_helper_unregister(freebsd32_sysent, sd));
3129 }
3130
3131 register_t *
3132 freebsd32_copyout_strings(struct image_params *imgp)
3133 {
3134         int argc, envc, i;
3135         u_int32_t *vectp;
3136         char *stringp;
3137         uintptr_t destp;
3138         u_int32_t *stack_base;
3139         struct freebsd32_ps_strings *arginfo;
3140         char canary[sizeof(long) * 8];
3141         int32_t pagesizes32[MAXPAGESIZES];
3142         size_t execpath_len;
3143         int szsigcode;
3144
3145         /*
3146          * Calculate string base and vector table pointers.
3147          * Also deal with signal trampoline code for this exec type.
3148          */
3149         if (imgp->execpath != NULL && imgp->auxargs != NULL)
3150                 execpath_len = strlen(imgp->execpath) + 1;
3151         else
3152                 execpath_len = 0;
3153         arginfo = (struct freebsd32_ps_strings *)curproc->p_sysent->
3154             sv_psstrings;
3155         if (imgp->proc->p_sysent->sv_sigcode_base == 0)
3156                 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
3157         else
3158                 szsigcode = 0;
3159         destp = (uintptr_t)arginfo;
3160
3161         /*
3162          * install sigcode
3163          */
3164         if (szsigcode != 0) {
3165                 destp -= szsigcode;
3166                 destp = rounddown2(destp, sizeof(uint32_t));
3167                 copyout(imgp->proc->p_sysent->sv_sigcode, (void *)destp,
3168                     szsigcode);
3169         }
3170
3171         /*
3172          * Copy the image path for the rtld.
3173          */
3174         if (execpath_len != 0) {
3175                 destp -= execpath_len;
3176                 imgp->execpathp = destp;
3177                 copyout(imgp->execpath, (void *)destp, execpath_len);
3178         }
3179
3180         /*
3181          * Prepare the canary for SSP.
3182          */
3183         arc4rand(canary, sizeof(canary), 0);
3184         destp -= sizeof(canary);
3185         imgp->canary = destp;
3186         copyout(canary, (void *)destp, sizeof(canary));
3187         imgp->canarylen = sizeof(canary);
3188
3189         /*
3190          * Prepare the pagesizes array.
3191          */
3192         for (i = 0; i < MAXPAGESIZES; i++)
3193                 pagesizes32[i] = (uint32_t)pagesizes[i];
3194         destp -= sizeof(pagesizes32);
3195         destp = rounddown2(destp, sizeof(uint32_t));
3196         imgp->pagesizes = destp;
3197         copyout(pagesizes32, (void *)destp, sizeof(pagesizes32));
3198         imgp->pagesizeslen = sizeof(pagesizes32);
3199
3200         destp -= ARG_MAX - imgp->args->stringspace;
3201         destp = rounddown2(destp, sizeof(uint32_t));
3202
3203         vectp = (uint32_t *)destp;
3204         if (imgp->sysent->sv_stackgap != NULL)
3205                 imgp->sysent->sv_stackgap(imgp, (u_long *)&vectp);
3206
3207         if (imgp->auxargs) {
3208                 /*
3209                  * Allocate room on the stack for the ELF auxargs
3210                  * array.  It has up to AT_COUNT entries.
3211                  */
3212                 vectp -= howmany(AT_COUNT * sizeof(Elf32_Auxinfo),
3213                     sizeof(*vectp));
3214         }
3215
3216         /*
3217          * Allocate room for the argv[] and env vectors including the
3218          * terminating NULL pointers.
3219          */
3220         vectp -= imgp->args->argc + 1 + imgp->args->envc + 1;
3221
3222         /*
3223          * vectp also becomes our initial stack base
3224          */
3225         stack_base = vectp;
3226
3227         stringp = imgp->args->begin_argv;
3228         argc = imgp->args->argc;
3229         envc = imgp->args->envc;
3230         /*
3231          * Copy out strings - arguments and environment.
3232          */
3233         copyout(stringp, (void *)destp, ARG_MAX - imgp->args->stringspace);
3234
3235         /*
3236          * Fill in "ps_strings" struct for ps, w, etc.
3237          */
3238         suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp);
3239         suword32(&arginfo->ps_nargvstr, argc);
3240
3241         /*
3242          * Fill in argument portion of vector table.
3243          */
3244         for (; argc > 0; --argc) {
3245                 suword32(vectp++, (u_int32_t)(intptr_t)destp);
3246                 while (*stringp++ != 0)
3247                         destp++;
3248                 destp++;
3249         }
3250
3251         /* a null vector table pointer separates the argp's from the envp's */
3252         suword32(vectp++, 0);
3253
3254         suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp);
3255         suword32(&arginfo->ps_nenvstr, envc);
3256
3257         /*
3258          * Fill in environment portion of vector table.
3259          */
3260         for (; envc > 0; --envc) {
3261                 suword32(vectp++, (u_int32_t)(intptr_t)destp);
3262                 while (*stringp++ != 0)
3263                         destp++;
3264                 destp++;
3265         }
3266
3267         /* end of vector table is a null pointer */
3268         suword32(vectp, 0);
3269
3270         return ((register_t *)stack_base);
3271 }
3272
3273 int
3274 freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
3275 {
3276         struct kld_file_stat *stat;
3277         struct kld32_file_stat *stat32;
3278         int error, version;
3279
3280         if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
3281             != 0)
3282                 return (error);
3283         if (version != sizeof(struct kld32_file_stat_1) &&
3284             version != sizeof(struct kld32_file_stat))
3285                 return (EINVAL);
3286
3287         stat = malloc(sizeof(*stat), M_TEMP, M_WAITOK | M_ZERO);
3288         stat32 = malloc(sizeof(*stat32), M_TEMP, M_WAITOK | M_ZERO);
3289         error = kern_kldstat(td, uap->fileid, stat);
3290         if (error == 0) {
3291                 bcopy(&stat->name[0], &stat32->name[0], sizeof(stat->name));
3292                 CP(*stat, *stat32, refs);
3293                 CP(*stat, *stat32, id);
3294                 PTROUT_CP(*stat, *stat32, address);
3295                 CP(*stat, *stat32, size);
3296                 bcopy(&stat->pathname[0], &stat32->pathname[0],
3297                     sizeof(stat->pathname));
3298                 stat32->version  = version;
3299                 error = copyout(stat32, uap->stat, version);
3300         }
3301         free(stat, M_TEMP);
3302         free(stat32, M_TEMP);
3303         return (error);
3304 }
3305
3306 int
3307 freebsd32_posix_fallocate(struct thread *td,
3308     struct freebsd32_posix_fallocate_args *uap)
3309 {
3310         int error;
3311
3312         error = kern_posix_fallocate(td, uap->fd,
3313             PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len));
3314         return (kern_posix_error(td, error));
3315 }
3316
3317 int
3318 freebsd32_posix_fadvise(struct thread *td,
3319     struct freebsd32_posix_fadvise_args *uap)
3320 {
3321         int error;
3322
3323         error = kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset),
3324             PAIR32TO64(off_t, uap->len), uap->advice);
3325         return (kern_posix_error(td, error));
3326 }
3327
3328 int
3329 convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig)
3330 {
3331
3332         CP(*sig32, *sig, sigev_notify);
3333         switch (sig->sigev_notify) {
3334         case SIGEV_NONE:
3335                 break;
3336         case SIGEV_THREAD_ID:
3337                 CP(*sig32, *sig, sigev_notify_thread_id);
3338                 /* FALLTHROUGH */
3339         case SIGEV_SIGNAL:
3340                 CP(*sig32, *sig, sigev_signo);
3341                 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3342                 break;
3343         case SIGEV_KEVENT:
3344                 CP(*sig32, *sig, sigev_notify_kqueue);
3345                 CP(*sig32, *sig, sigev_notify_kevent_flags);
3346                 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3347                 break;
3348         default:
3349                 return (EINVAL);
3350         }
3351         return (0);
3352 }
3353
3354 int
3355 freebsd32_procctl(struct thread *td, struct freebsd32_procctl_args *uap)
3356 {
3357         void *data;
3358         union {
3359                 struct procctl_reaper_status rs;
3360                 struct procctl_reaper_pids rp;
3361                 struct procctl_reaper_kill rk;
3362         } x;
3363         union {
3364                 struct procctl_reaper_pids32 rp;
3365         } x32;
3366         int error, error1, flags, signum;
3367
3368         if (uap->com >= PROC_PROCCTL_MD_MIN)
3369                 return (cpu_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id),
3370                     uap->com, PTRIN(uap->data)));
3371
3372         switch (uap->com) {
3373         case PROC_ASLR_CTL:
3374         case PROC_SPROTECT:
3375         case PROC_STACKGAP_CTL:
3376         case PROC_TRACE_CTL:
3377         case PROC_TRAPCAP_CTL:
3378                 error = copyin(PTRIN(uap->data), &flags, sizeof(flags));
3379                 if (error != 0)
3380                         return (error);
3381                 data = &flags;
3382                 break;
3383         case PROC_REAP_ACQUIRE:
3384         case PROC_REAP_RELEASE:
3385                 if (uap->data != NULL)
3386                         return (EINVAL);
3387                 data = NULL;
3388                 break;
3389         case PROC_REAP_STATUS:
3390                 data = &x.rs;
3391                 break;
3392         case PROC_REAP_GETPIDS:
3393                 error = copyin(uap->data, &x32.rp, sizeof(x32.rp));
3394                 if (error != 0)
3395                         return (error);
3396                 CP(x32.rp, x.rp, rp_count);
3397                 PTRIN_CP(x32.rp, x.rp, rp_pids);
3398                 data = &x.rp;
3399                 break;
3400         case PROC_REAP_KILL:
3401                 error = copyin(uap->data, &x.rk, sizeof(x.rk));
3402                 if (error != 0)
3403                         return (error);
3404                 data = &x.rk;
3405                 break;
3406         case PROC_ASLR_STATUS:
3407         case PROC_STACKGAP_STATUS:
3408         case PROC_TRACE_STATUS:
3409         case PROC_TRAPCAP_STATUS:
3410                 data = &flags;
3411                 break;
3412         case PROC_PDEATHSIG_CTL:
3413                 error = copyin(uap->data, &signum, sizeof(signum));
3414                 if (error != 0)
3415                         return (error);
3416                 data = &signum;
3417                 break;
3418         case PROC_PDEATHSIG_STATUS:
3419                 data = &signum;
3420                 break;
3421         default:
3422                 return (EINVAL);
3423         }
3424         error = kern_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id),
3425             uap->com, data);
3426         switch (uap->com) {
3427         case PROC_REAP_STATUS:
3428                 if (error == 0)
3429                         error = copyout(&x.rs, uap->data, sizeof(x.rs));
3430                 break;
3431         case PROC_REAP_KILL:
3432                 error1 = copyout(&x.rk, uap->data, sizeof(x.rk));
3433                 if (error == 0)
3434                         error = error1;
3435                 break;
3436         case PROC_ASLR_STATUS:
3437         case PROC_STACKGAP_STATUS:
3438         case PROC_TRACE_STATUS:
3439         case PROC_TRAPCAP_STATUS:
3440                 if (error == 0)
3441                         error = copyout(&flags, uap->data, sizeof(flags));
3442                 break;
3443         case PROC_PDEATHSIG_STATUS:
3444                 if (error == 0)
3445                         error = copyout(&signum, uap->data, sizeof(signum));
3446                 break;
3447         }
3448         return (error);
3449 }
3450
3451 int
3452 freebsd32_fcntl(struct thread *td, struct freebsd32_fcntl_args *uap)
3453 {
3454         long tmp;
3455
3456         switch (uap->cmd) {
3457         /*
3458          * Do unsigned conversion for arg when operation
3459          * interprets it as flags or pointer.
3460          */
3461         case F_SETLK_REMOTE:
3462         case F_SETLKW:
3463         case F_SETLK:
3464         case F_GETLK:
3465         case F_SETFD:
3466         case F_SETFL:
3467         case F_OGETLK:
3468         case F_OSETLK:
3469         case F_OSETLKW:
3470                 tmp = (unsigned int)(uap->arg);
3471                 break;
3472         default:
3473                 tmp = uap->arg;
3474                 break;
3475         }
3476         return (kern_fcntl_freebsd(td, uap->fd, uap->cmd, tmp));
3477 }
3478
3479 int
3480 freebsd32_ppoll(struct thread *td, struct freebsd32_ppoll_args *uap)
3481 {
3482         struct timespec32 ts32;
3483         struct timespec ts, *tsp;
3484         sigset_t set, *ssp;
3485         int error;
3486
3487         if (uap->ts != NULL) {
3488                 error = copyin(uap->ts, &ts32, sizeof(ts32));
3489                 if (error != 0)
3490                         return (error);
3491                 CP(ts32, ts, tv_sec);
3492                 CP(ts32, ts, tv_nsec);
3493                 tsp = &ts;
3494         } else
3495                 tsp = NULL;
3496         if (uap->set != NULL) {
3497                 error = copyin(uap->set, &set, sizeof(set));
3498                 if (error != 0)
3499                         return (error);
3500                 ssp = &set;
3501         } else
3502                 ssp = NULL;
3503
3504         return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp));
3505 }
3506
3507 int
3508 freebsd32_sched_rr_get_interval(struct thread *td,
3509     struct freebsd32_sched_rr_get_interval_args *uap)
3510 {
3511         struct timespec ts;
3512         struct timespec32 ts32;
3513         int error;
3514
3515         error = kern_sched_rr_get_interval(td, uap->pid, &ts);
3516         if (error == 0) {
3517                 CP(ts, ts32, tv_sec);
3518                 CP(ts, ts32, tv_nsec);
3519                 error = copyout(&ts32, uap->interval, sizeof(ts32));
3520         }
3521         return (error);
3522 }