]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/freebsd32/freebsd32_misc.c
sendfile: Explicitly ignore errors from copyout()
[FreeBSD/FreeBSD.git] / sys / compat / freebsd32 / freebsd32_misc.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 #include "opt_ffclock.h"
31 #include "opt_inet.h"
32 #include "opt_inet6.h"
33 #include "opt_ktrace.h"
34
35 #define __ELF_WORD_SIZE 32
36
37 #ifdef COMPAT_FREEBSD11
38 #define _WANT_FREEBSD11_KEVENT
39 #endif
40
41 #include <sys/param.h>
42 #include <sys/bus.h>
43 #include <sys/capsicum.h>
44 #include <sys/clock.h>
45 #include <sys/exec.h>
46 #include <sys/fcntl.h>
47 #include <sys/filedesc.h>
48 #include <sys/imgact.h>
49 #include <sys/jail.h>
50 #include <sys/kernel.h>
51 #include <sys/limits.h>
52 #include <sys/linker.h>
53 #include <sys/lock.h>
54 #include <sys/malloc.h>
55 #include <sys/file.h>           /* Must come after sys/malloc.h */
56 #include <sys/imgact.h>
57 #include <sys/mbuf.h>
58 #include <sys/mman.h>
59 #include <sys/module.h>
60 #include <sys/mount.h>
61 #include <sys/mutex.h>
62 #include <sys/namei.h>
63 #include <sys/priv.h>
64 #include <sys/proc.h>
65 #include <sys/procctl.h>
66 #include <sys/ptrace.h>
67 #include <sys/reboot.h>
68 #include <sys/resource.h>
69 #include <sys/resourcevar.h>
70 #include <sys/selinfo.h>
71 #include <sys/eventvar.h>       /* Must come after sys/selinfo.h */
72 #include <sys/pipe.h>           /* Must come after sys/selinfo.h */
73 #include <sys/signal.h>
74 #include <sys/signalvar.h>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
77 #include <sys/stat.h>
78 #include <sys/syscall.h>
79 #include <sys/syscallsubr.h>
80 #include <sys/sysctl.h>
81 #include <sys/sysent.h>
82 #include <sys/sysproto.h>
83 #include <sys/systm.h>
84 #include <sys/thr.h>
85 #include <sys/timerfd.h>
86 #include <sys/timex.h>
87 #include <sys/unistd.h>
88 #include <sys/ucontext.h>
89 #include <sys/vnode.h>
90 #include <sys/wait.h>
91 #include <sys/ipc.h>
92 #include <sys/msg.h>
93 #include <sys/sem.h>
94 #include <sys/shm.h>
95 #include <sys/timeffc.h>
96 #ifdef KTRACE
97 #include <sys/ktrace.h>
98 #endif
99
100 #ifdef INET
101 #include <netinet/in.h>
102 #endif
103
104 #include <vm/vm.h>
105 #include <vm/vm_param.h>
106 #include <vm/pmap.h>
107 #include <vm/vm_map.h>
108 #include <vm/vm_object.h>
109 #include <vm/vm_extern.h>
110
111 #include <machine/cpu.h>
112 #include <machine/elf.h>
113 #ifdef __amd64__
114 #include <machine/md_var.h>
115 #endif
116
117 #include <security/audit/audit.h>
118
119 #include <compat/freebsd32/freebsd32_util.h>
120 #include <compat/freebsd32/freebsd32.h>
121 #include <compat/freebsd32/freebsd32_ipc.h>
122 #include <compat/freebsd32/freebsd32_misc.h>
123 #include <compat/freebsd32/freebsd32_signal.h>
124 #include <compat/freebsd32/freebsd32_proto.h>
125
126 int compat_freebsd_32bit = 1;
127
128 static void
129 register_compat32_feature(void *arg)
130 {
131         if (!compat_freebsd_32bit)
132                 return;
133
134         FEATURE_ADD("compat_freebsd32", "Compatible with 32-bit FreeBSD");
135         FEATURE_ADD("compat_freebsd_32bit",
136             "Compatible with 32-bit FreeBSD (legacy feature name)");
137 }
138 SYSINIT(freebsd32, SI_SUB_EXEC, SI_ORDER_ANY, register_compat32_feature,
139     NULL);
140
141 struct ptrace_io_desc32 {
142         int             piod_op;
143         uint32_t        piod_offs;
144         uint32_t        piod_addr;
145         uint32_t        piod_len;
146 };
147
148 struct ptrace_vm_entry32 {
149         int             pve_entry;
150         int             pve_timestamp;
151         uint32_t        pve_start;
152         uint32_t        pve_end;
153         uint32_t        pve_offset;
154         u_int           pve_prot;
155         u_int           pve_pathlen;
156         int32_t         pve_fileid;
157         u_int           pve_fsid;
158         uint32_t        pve_path;
159 };
160
161 #ifdef __amd64__
162 CTASSERT(sizeof(struct timeval32) == 8);
163 CTASSERT(sizeof(struct timespec32) == 8);
164 CTASSERT(sizeof(struct itimerval32) == 16);
165 CTASSERT(sizeof(struct bintime32) == 12);
166 #else
167 CTASSERT(sizeof(struct timeval32) == 16);
168 CTASSERT(sizeof(struct timespec32) == 16);
169 CTASSERT(sizeof(struct itimerval32) == 32);
170 CTASSERT(sizeof(struct bintime32) == 16);
171 #endif
172 CTASSERT(sizeof(struct ostatfs32) == 256);
173 #ifdef __amd64__
174 CTASSERT(sizeof(struct rusage32) == 72);
175 #else
176 CTASSERT(sizeof(struct rusage32) == 88);
177 #endif
178 CTASSERT(sizeof(struct sigaltstack32) == 12);
179 #ifdef __amd64__
180 CTASSERT(sizeof(struct kevent32) == 56);
181 #else
182 CTASSERT(sizeof(struct kevent32) == 64);
183 #endif
184 CTASSERT(sizeof(struct iovec32) == 8);
185 CTASSERT(sizeof(struct msghdr32) == 28);
186 #ifdef __amd64__
187 CTASSERT(sizeof(struct stat32) == 208);
188 CTASSERT(sizeof(struct freebsd11_stat32) == 96);
189 #else
190 CTASSERT(sizeof(struct stat32) == 224);
191 CTASSERT(sizeof(struct freebsd11_stat32) == 120);
192 #endif
193 CTASSERT(sizeof(struct sigaction32) == 24);
194
195 static int freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count);
196 static int freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count);
197 static int freebsd32_user_clock_nanosleep(struct thread *td, clockid_t clock_id,
198     int flags, const struct timespec32 *ua_rqtp, struct timespec32 *ua_rmtp);
199
200 void
201 freebsd32_rusage_out(const struct rusage *s, struct rusage32 *s32)
202 {
203
204         TV_CP(*s, *s32, ru_utime);
205         TV_CP(*s, *s32, ru_stime);
206         CP(*s, *s32, ru_maxrss);
207         CP(*s, *s32, ru_ixrss);
208         CP(*s, *s32, ru_idrss);
209         CP(*s, *s32, ru_isrss);
210         CP(*s, *s32, ru_minflt);
211         CP(*s, *s32, ru_majflt);
212         CP(*s, *s32, ru_nswap);
213         CP(*s, *s32, ru_inblock);
214         CP(*s, *s32, ru_oublock);
215         CP(*s, *s32, ru_msgsnd);
216         CP(*s, *s32, ru_msgrcv);
217         CP(*s, *s32, ru_nsignals);
218         CP(*s, *s32, ru_nvcsw);
219         CP(*s, *s32, ru_nivcsw);
220 }
221
222 int
223 freebsd32_wait4(struct thread *td, struct freebsd32_wait4_args *uap)
224 {
225         int error, status;
226         struct rusage32 ru32;
227         struct rusage ru, *rup;
228
229         if (uap->rusage != NULL)
230                 rup = &ru;
231         else
232                 rup = NULL;
233         error = kern_wait(td, uap->pid, &status, uap->options, rup);
234         if (error)
235                 return (error);
236         if (uap->status != NULL)
237                 error = copyout(&status, uap->status, sizeof(status));
238         if (uap->rusage != NULL && error == 0) {
239                 freebsd32_rusage_out(&ru, &ru32);
240                 error = copyout(&ru32, uap->rusage, sizeof(ru32));
241         }
242         return (error);
243 }
244
245 int
246 freebsd32_wait6(struct thread *td, struct freebsd32_wait6_args *uap)
247 {
248         struct __wrusage32 wru32;
249         struct __wrusage wru, *wrup;
250         struct siginfo32 si32;
251         struct __siginfo si, *sip;
252         int error, status;
253
254         if (uap->wrusage != NULL)
255                 wrup = &wru;
256         else
257                 wrup = NULL;
258         if (uap->info != NULL) {
259                 sip = &si;
260                 bzero(sip, sizeof(*sip));
261         } else
262                 sip = NULL;
263         error = kern_wait6(td, uap->idtype, PAIR32TO64(id_t, uap->id),
264             &status, uap->options, wrup, sip);
265         if (error != 0)
266                 return (error);
267         if (uap->status != NULL)
268                 error = copyout(&status, uap->status, sizeof(status));
269         if (uap->wrusage != NULL && error == 0) {
270                 freebsd32_rusage_out(&wru.wru_self, &wru32.wru_self);
271                 freebsd32_rusage_out(&wru.wru_children, &wru32.wru_children);
272                 error = copyout(&wru32, uap->wrusage, sizeof(wru32));
273         }
274         if (uap->info != NULL && error == 0) {
275                 siginfo_to_siginfo32 (&si, &si32);
276                 error = copyout(&si32, uap->info, sizeof(si32));
277         }
278         return (error);
279 }
280
281 #ifdef COMPAT_FREEBSD4
282 static void
283 copy_statfs(struct statfs *in, struct ostatfs32 *out)
284 {
285
286         statfs_scale_blocks(in, INT32_MAX);
287         bzero(out, sizeof(*out));
288         CP(*in, *out, f_bsize);
289         out->f_iosize = MIN(in->f_iosize, INT32_MAX);
290         CP(*in, *out, f_blocks);
291         CP(*in, *out, f_bfree);
292         CP(*in, *out, f_bavail);
293         out->f_files = MIN(in->f_files, INT32_MAX);
294         out->f_ffree = MIN(in->f_ffree, INT32_MAX);
295         CP(*in, *out, f_fsid);
296         CP(*in, *out, f_owner);
297         CP(*in, *out, f_type);
298         CP(*in, *out, f_flags);
299         out->f_syncwrites = MIN(in->f_syncwrites, INT32_MAX);
300         out->f_asyncwrites = MIN(in->f_asyncwrites, INT32_MAX);
301         strlcpy(out->f_fstypename,
302               in->f_fstypename, MFSNAMELEN);
303         strlcpy(out->f_mntonname,
304               in->f_mntonname, min(MNAMELEN, FREEBSD4_OMNAMELEN));
305         out->f_syncreads = MIN(in->f_syncreads, INT32_MAX);
306         out->f_asyncreads = MIN(in->f_asyncreads, INT32_MAX);
307         strlcpy(out->f_mntfromname,
308               in->f_mntfromname, min(MNAMELEN, FREEBSD4_OMNAMELEN));
309 }
310 #endif
311
312 int
313 freebsd32_getfsstat(struct thread *td, struct freebsd32_getfsstat_args *uap)
314 {
315         size_t count;
316         int error;
317
318         if (uap->bufsize < 0 || uap->bufsize > SIZE_MAX)
319                 return (EINVAL);
320         error = kern_getfsstat(td, &uap->buf, uap->bufsize, &count,
321             UIO_USERSPACE, uap->mode);
322         if (error == 0)
323                 td->td_retval[0] = count;
324         return (error);
325 }
326
327 #ifdef COMPAT_FREEBSD4
328 int
329 freebsd4_freebsd32_getfsstat(struct thread *td,
330     struct freebsd4_freebsd32_getfsstat_args *uap)
331 {
332         struct statfs *buf, *sp;
333         struct ostatfs32 stat32;
334         size_t count, size, copycount;
335         int error;
336
337         count = uap->bufsize / sizeof(struct ostatfs32);
338         size = count * sizeof(struct statfs);
339         error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE, uap->mode);
340         if (size > 0) {
341                 sp = buf;
342                 copycount = count;
343                 while (copycount > 0 && error == 0) {
344                         copy_statfs(sp, &stat32);
345                         error = copyout(&stat32, uap->buf, sizeof(stat32));
346                         sp++;
347                         uap->buf++;
348                         copycount--;
349                 }
350                 free(buf, M_STATFS);
351         }
352         if (error == 0)
353                 td->td_retval[0] = count;
354         return (error);
355 }
356 #endif
357
358 #ifdef COMPAT_FREEBSD11
359 int
360 freebsd11_freebsd32_getfsstat(struct thread *td,
361     struct freebsd11_freebsd32_getfsstat_args *uap)
362 {
363         return(kern_freebsd11_getfsstat(td, uap->buf, uap->bufsize,
364             uap->mode));
365 }
366 #endif
367
368 int
369 freebsd32_sigaltstack(struct thread *td,
370                       struct freebsd32_sigaltstack_args *uap)
371 {
372         struct sigaltstack32 s32;
373         struct sigaltstack ss, oss, *ssp;
374         int error;
375
376         if (uap->ss != NULL) {
377                 error = copyin(uap->ss, &s32, sizeof(s32));
378                 if (error)
379                         return (error);
380                 PTRIN_CP(s32, ss, ss_sp);
381                 CP(s32, ss, ss_size);
382                 CP(s32, ss, ss_flags);
383                 ssp = &ss;
384         } else
385                 ssp = NULL;
386         error = kern_sigaltstack(td, ssp, &oss);
387         if (error == 0 && uap->oss != NULL) {
388                 PTROUT_CP(oss, s32, ss_sp);
389                 CP(oss, s32, ss_size);
390                 CP(oss, s32, ss_flags);
391                 error = copyout(&s32, uap->oss, sizeof(s32));
392         }
393         return (error);
394 }
395
396 /*
397  * Custom version of exec_copyin_args() so that we can translate
398  * the pointers.
399  */
400 int
401 freebsd32_exec_copyin_args(struct image_args *args, const char *fname,
402     enum uio_seg segflg, uint32_t *argv, uint32_t *envv)
403 {
404         char *argp, *envp;
405         uint32_t *p32, arg;
406         int error;
407
408         bzero(args, sizeof(*args));
409         if (argv == NULL)
410                 return (EFAULT);
411
412         /*
413          * Allocate demand-paged memory for the file name, argument, and
414          * environment strings.
415          */
416         error = exec_alloc_args(args);
417         if (error != 0)
418                 return (error);
419
420         /*
421          * Copy the file name.
422          */
423         error = exec_args_add_fname(args, fname, segflg);
424         if (error != 0)
425                 goto err_exit;
426
427         /*
428          * extract arguments first
429          */
430         p32 = argv;
431         for (;;) {
432                 error = copyin(p32++, &arg, sizeof(arg));
433                 if (error)
434                         goto err_exit;
435                 if (arg == 0)
436                         break;
437                 argp = PTRIN(arg);
438                 error = exec_args_add_arg(args, argp, UIO_USERSPACE);
439                 if (error != 0)
440                         goto err_exit;
441         }
442
443         /*
444          * extract environment strings
445          */
446         if (envv) {
447                 p32 = envv;
448                 for (;;) {
449                         error = copyin(p32++, &arg, sizeof(arg));
450                         if (error)
451                                 goto err_exit;
452                         if (arg == 0)
453                                 break;
454                         envp = PTRIN(arg);
455                         error = exec_args_add_env(args, envp, UIO_USERSPACE);
456                         if (error != 0)
457                                 goto err_exit;
458                 }
459         }
460
461         return (0);
462
463 err_exit:
464         exec_free_args(args);
465         return (error);
466 }
467
468 int
469 freebsd32_execve(struct thread *td, struct freebsd32_execve_args *uap)
470 {
471         struct image_args eargs;
472         struct vmspace *oldvmspace;
473         int error;
474
475         error = pre_execve(td, &oldvmspace);
476         if (error != 0)
477                 return (error);
478         error = freebsd32_exec_copyin_args(&eargs, uap->fname, UIO_USERSPACE,
479             uap->argv, uap->envv);
480         if (error == 0)
481                 error = kern_execve(td, &eargs, NULL, oldvmspace);
482         post_execve(td, error, oldvmspace);
483         AUDIT_SYSCALL_EXIT(error == EJUSTRETURN ? 0 : error, td);
484         return (error);
485 }
486
487 int
488 freebsd32_fexecve(struct thread *td, struct freebsd32_fexecve_args *uap)
489 {
490         struct image_args eargs;
491         struct vmspace *oldvmspace;
492         int error;
493
494         error = pre_execve(td, &oldvmspace);
495         if (error != 0)
496                 return (error);
497         error = freebsd32_exec_copyin_args(&eargs, NULL, UIO_SYSSPACE,
498             uap->argv, uap->envv);
499         if (error == 0) {
500                 eargs.fd = uap->fd;
501                 error = kern_execve(td, &eargs, NULL, oldvmspace);
502         }
503         post_execve(td, error, oldvmspace);
504         AUDIT_SYSCALL_EXIT(error == EJUSTRETURN ? 0 : error, td);
505         return (error);
506 }
507
508 int
509 freebsd32_mknodat(struct thread *td, struct freebsd32_mknodat_args *uap)
510 {
511
512         return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE,
513             uap->mode, PAIR32TO64(dev_t, uap->dev)));
514 }
515
516 int
517 freebsd32_mprotect(struct thread *td, struct freebsd32_mprotect_args *uap)
518 {
519         int prot;
520
521         prot = uap->prot;
522 #if defined(__amd64__)
523         if (i386_read_exec && (prot & PROT_READ) != 0)
524                 prot |= PROT_EXEC;
525 #endif
526         return (kern_mprotect(td, (uintptr_t)PTRIN(uap->addr), uap->len,
527             prot, 0));
528 }
529
530 int
531 freebsd32_mmap(struct thread *td, struct freebsd32_mmap_args *uap)
532 {
533         int prot;
534
535         prot = uap->prot;
536 #if defined(__amd64__)
537         if (i386_read_exec && (prot & PROT_READ))
538                 prot |= PROT_EXEC;
539 #endif
540
541         return (kern_mmap(td, &(struct mmap_req){
542                 .mr_hint = (uintptr_t)uap->addr,
543                 .mr_len = uap->len,
544                 .mr_prot = prot,
545                 .mr_flags = uap->flags,
546                 .mr_fd = uap->fd,
547                 .mr_pos = PAIR32TO64(off_t, uap->pos),
548             }));
549 }
550
551 #ifdef COMPAT_FREEBSD6
552 int
553 freebsd6_freebsd32_mmap(struct thread *td,
554     struct freebsd6_freebsd32_mmap_args *uap)
555 {
556         int prot;
557
558         prot = uap->prot;
559 #if defined(__amd64__)
560         if (i386_read_exec && (prot & PROT_READ))
561                 prot |= PROT_EXEC;
562 #endif
563
564         return (kern_mmap(td, &(struct mmap_req){
565                 .mr_hint = (uintptr_t)uap->addr,
566                 .mr_len = uap->len,
567                 .mr_prot = prot,
568                 .mr_flags = uap->flags,
569                 .mr_fd = uap->fd,
570                 .mr_pos = PAIR32TO64(off_t, uap->pos),
571             }));
572 }
573 #endif
574
575 #ifdef COMPAT_43
576 int
577 ofreebsd32_mmap(struct thread *td, struct ofreebsd32_mmap_args *uap)
578 {
579         return (kern_ommap(td, (uintptr_t)uap->addr, uap->len, uap->prot,
580             uap->flags, uap->fd, uap->pos));
581 }
582 #endif
583
584 int
585 freebsd32_setitimer(struct thread *td, struct freebsd32_setitimer_args *uap)
586 {
587         struct itimerval itv, oitv, *itvp;      
588         struct itimerval32 i32;
589         int error;
590
591         if (uap->itv != NULL) {
592                 error = copyin(uap->itv, &i32, sizeof(i32));
593                 if (error)
594                         return (error);
595                 TV_CP(i32, itv, it_interval);
596                 TV_CP(i32, itv, it_value);
597                 itvp = &itv;
598         } else
599                 itvp = NULL;
600         error = kern_setitimer(td, uap->which, itvp, &oitv);
601         if (error || uap->oitv == NULL)
602                 return (error);
603         TV_CP(oitv, i32, it_interval);
604         TV_CP(oitv, i32, it_value);
605         return (copyout(&i32, uap->oitv, sizeof(i32)));
606 }
607
608 int
609 freebsd32_getitimer(struct thread *td, struct freebsd32_getitimer_args *uap)
610 {
611         struct itimerval itv;
612         struct itimerval32 i32;
613         int error;
614
615         error = kern_getitimer(td, uap->which, &itv);
616         if (error || uap->itv == NULL)
617                 return (error);
618         TV_CP(itv, i32, it_interval);
619         TV_CP(itv, i32, it_value);
620         return (copyout(&i32, uap->itv, sizeof(i32)));
621 }
622
623 int
624 freebsd32_select(struct thread *td, struct freebsd32_select_args *uap)
625 {
626         struct timeval32 tv32;
627         struct timeval tv, *tvp;
628         int error;
629
630         if (uap->tv != NULL) {
631                 error = copyin(uap->tv, &tv32, sizeof(tv32));
632                 if (error)
633                         return (error);
634                 CP(tv32, tv, tv_sec);
635                 CP(tv32, tv, tv_usec);
636                 tvp = &tv;
637         } else
638                 tvp = NULL;
639         /*
640          * XXX Do pointers need PTRIN()?
641          */
642         return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
643             sizeof(int32_t) * 8));
644 }
645
646 int
647 freebsd32_pselect(struct thread *td, struct freebsd32_pselect_args *uap)
648 {
649         struct timespec32 ts32;
650         struct timespec ts;
651         struct timeval tv, *tvp;
652         sigset_t set, *uset;
653         int error;
654
655         if (uap->ts != NULL) {
656                 error = copyin(uap->ts, &ts32, sizeof(ts32));
657                 if (error != 0)
658                         return (error);
659                 CP(ts32, ts, tv_sec);
660                 CP(ts32, ts, tv_nsec);
661                 TIMESPEC_TO_TIMEVAL(&tv, &ts);
662                 tvp = &tv;
663         } else
664                 tvp = NULL;
665         if (uap->sm != NULL) {
666                 error = copyin(uap->sm, &set, sizeof(set));
667                 if (error != 0)
668                         return (error);
669                 uset = &set;
670         } else
671                 uset = NULL;
672         /*
673          * XXX Do pointers need PTRIN()?
674          */
675         error = kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
676             uset, sizeof(int32_t) * 8);
677         return (error);
678 }
679
680 /*
681  * Copy 'count' items into the destination list pointed to by uap->eventlist.
682  */
683 static int
684 freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count)
685 {
686         struct freebsd32_kevent_args *uap;
687         struct kevent32 ks32[KQ_NEVENTS];
688         uint64_t e;
689         int i, j, error;
690
691         KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
692         uap = (struct freebsd32_kevent_args *)arg;
693
694         for (i = 0; i < count; i++) {
695                 CP(kevp[i], ks32[i], ident);
696                 CP(kevp[i], ks32[i], filter);
697                 CP(kevp[i], ks32[i], flags);
698                 CP(kevp[i], ks32[i], fflags);
699 #if BYTE_ORDER == LITTLE_ENDIAN
700                 ks32[i].data1 = kevp[i].data;
701                 ks32[i].data2 = kevp[i].data >> 32;
702 #else
703                 ks32[i].data1 = kevp[i].data >> 32;
704                 ks32[i].data2 = kevp[i].data;
705 #endif
706                 PTROUT_CP(kevp[i], ks32[i], udata);
707                 for (j = 0; j < nitems(kevp->ext); j++) {
708                         e = kevp[i].ext[j];
709 #if BYTE_ORDER == LITTLE_ENDIAN
710                         ks32[i].ext64[2 * j] = e;
711                         ks32[i].ext64[2 * j + 1] = e >> 32;
712 #else
713                         ks32[i].ext64[2 * j] = e >> 32;
714                         ks32[i].ext64[2 * j + 1] = e;
715 #endif
716                 }
717         }
718         error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
719         if (error == 0)
720                 uap->eventlist += count;
721         return (error);
722 }
723
724 /*
725  * Copy 'count' items from the list pointed to by uap->changelist.
726  */
727 static int
728 freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count)
729 {
730         struct freebsd32_kevent_args *uap;
731         struct kevent32 ks32[KQ_NEVENTS];
732         uint64_t e;
733         int i, j, error;
734
735         KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
736         uap = (struct freebsd32_kevent_args *)arg;
737
738         error = copyin(uap->changelist, ks32, count * sizeof *ks32);
739         if (error)
740                 goto done;
741         uap->changelist += count;
742
743         for (i = 0; i < count; i++) {
744                 CP(ks32[i], kevp[i], ident);
745                 CP(ks32[i], kevp[i], filter);
746                 CP(ks32[i], kevp[i], flags);
747                 CP(ks32[i], kevp[i], fflags);
748                 kevp[i].data = PAIR32TO64(uint64_t, ks32[i].data);
749                 PTRIN_CP(ks32[i], kevp[i], udata);
750                 for (j = 0; j < nitems(kevp->ext); j++) {
751 #if BYTE_ORDER == LITTLE_ENDIAN
752                         e = ks32[i].ext64[2 * j + 1];
753                         e <<= 32;
754                         e += ks32[i].ext64[2 * j];
755 #else
756                         e = ks32[i].ext64[2 * j];
757                         e <<= 32;
758                         e += ks32[i].ext64[2 * j + 1];
759 #endif
760                         kevp[i].ext[j] = e;
761                 }
762         }
763 done:
764         return (error);
765 }
766
767 int
768 freebsd32_kevent(struct thread *td, struct freebsd32_kevent_args *uap)
769 {
770         struct timespec32 ts32;
771         struct timespec ts, *tsp;
772         struct kevent_copyops k_ops = {
773                 .arg = uap,
774                 .k_copyout = freebsd32_kevent_copyout,
775                 .k_copyin = freebsd32_kevent_copyin,
776         };
777 #ifdef KTRACE
778         struct kevent32 *eventlist = uap->eventlist;
779 #endif
780         int error;
781
782         if (uap->timeout) {
783                 error = copyin(uap->timeout, &ts32, sizeof(ts32));
784                 if (error)
785                         return (error);
786                 CP(ts32, ts, tv_sec);
787                 CP(ts32, ts, tv_nsec);
788                 tsp = &ts;
789         } else
790                 tsp = NULL;
791 #ifdef KTRACE
792         if (KTRPOINT(td, KTR_STRUCT_ARRAY))
793                 ktrstructarray("kevent32", UIO_USERSPACE, uap->changelist,
794                     uap->nchanges, sizeof(struct kevent32));
795 #endif
796         error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
797             &k_ops, tsp);
798 #ifdef KTRACE
799         if (error == 0 && KTRPOINT(td, KTR_STRUCT_ARRAY))
800                 ktrstructarray("kevent32", UIO_USERSPACE, eventlist,
801                     td->td_retval[0], sizeof(struct kevent32));
802 #endif
803         return (error);
804 }
805
806 #ifdef COMPAT_FREEBSD11
807 static int
808 freebsd32_kevent11_copyout(void *arg, struct kevent *kevp, int count)
809 {
810         struct freebsd11_freebsd32_kevent_args *uap;
811         struct freebsd11_kevent32 ks32[KQ_NEVENTS];
812         int i, error;
813
814         KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
815         uap = (struct freebsd11_freebsd32_kevent_args *)arg;
816
817         for (i = 0; i < count; i++) {
818                 CP(kevp[i], ks32[i], ident);
819                 CP(kevp[i], ks32[i], filter);
820                 CP(kevp[i], ks32[i], flags);
821                 CP(kevp[i], ks32[i], fflags);
822                 CP(kevp[i], ks32[i], data);
823                 PTROUT_CP(kevp[i], ks32[i], udata);
824         }
825         error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
826         if (error == 0)
827                 uap->eventlist += count;
828         return (error);
829 }
830
831 /*
832  * Copy 'count' items from the list pointed to by uap->changelist.
833  */
834 static int
835 freebsd32_kevent11_copyin(void *arg, struct kevent *kevp, int count)
836 {
837         struct freebsd11_freebsd32_kevent_args *uap;
838         struct freebsd11_kevent32 ks32[KQ_NEVENTS];
839         int i, j, error;
840
841         KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
842         uap = (struct freebsd11_freebsd32_kevent_args *)arg;
843
844         error = copyin(uap->changelist, ks32, count * sizeof *ks32);
845         if (error)
846                 goto done;
847         uap->changelist += count;
848
849         for (i = 0; i < count; i++) {
850                 CP(ks32[i], kevp[i], ident);
851                 CP(ks32[i], kevp[i], filter);
852                 CP(ks32[i], kevp[i], flags);
853                 CP(ks32[i], kevp[i], fflags);
854                 CP(ks32[i], kevp[i], data);
855                 PTRIN_CP(ks32[i], kevp[i], udata);
856                 for (j = 0; j < nitems(kevp->ext); j++)
857                         kevp[i].ext[j] = 0;
858         }
859 done:
860         return (error);
861 }
862
863 int
864 freebsd11_freebsd32_kevent(struct thread *td,
865     struct freebsd11_freebsd32_kevent_args *uap)
866 {
867         struct timespec32 ts32;
868         struct timespec ts, *tsp;
869         struct kevent_copyops k_ops = {
870                 .arg = uap,
871                 .k_copyout = freebsd32_kevent11_copyout,
872                 .k_copyin = freebsd32_kevent11_copyin,
873         };
874 #ifdef KTRACE
875         struct freebsd11_kevent32 *eventlist = uap->eventlist;
876 #endif
877         int error;
878
879         if (uap->timeout) {
880                 error = copyin(uap->timeout, &ts32, sizeof(ts32));
881                 if (error)
882                         return (error);
883                 CP(ts32, ts, tv_sec);
884                 CP(ts32, ts, tv_nsec);
885                 tsp = &ts;
886         } else
887                 tsp = NULL;
888 #ifdef KTRACE
889         if (KTRPOINT(td, KTR_STRUCT_ARRAY))
890                 ktrstructarray("freebsd11_kevent32", UIO_USERSPACE,
891                     uap->changelist, uap->nchanges,
892                     sizeof(struct freebsd11_kevent32));
893 #endif
894         error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
895             &k_ops, tsp);
896 #ifdef KTRACE
897         if (error == 0 && KTRPOINT(td, KTR_STRUCT_ARRAY))
898                 ktrstructarray("freebsd11_kevent32", UIO_USERSPACE,
899                     eventlist, td->td_retval[0],
900                     sizeof(struct freebsd11_kevent32));
901 #endif
902         return (error);
903 }
904 #endif
905
906 int
907 freebsd32_gettimeofday(struct thread *td,
908                        struct freebsd32_gettimeofday_args *uap)
909 {
910         struct timeval atv;
911         struct timeval32 atv32;
912         struct timezone rtz;
913         int error = 0;
914
915         if (uap->tp) {
916                 microtime(&atv);
917                 CP(atv, atv32, tv_sec);
918                 CP(atv, atv32, tv_usec);
919                 error = copyout(&atv32, uap->tp, sizeof (atv32));
920         }
921         if (error == 0 && uap->tzp != NULL) {
922                 rtz.tz_minuteswest = 0;
923                 rtz.tz_dsttime = 0;
924                 error = copyout(&rtz, uap->tzp, sizeof (rtz));
925         }
926         return (error);
927 }
928
929 int
930 freebsd32_getrusage(struct thread *td, struct freebsd32_getrusage_args *uap)
931 {
932         struct rusage32 s32;
933         struct rusage s;
934         int error;
935
936         error = kern_getrusage(td, uap->who, &s);
937         if (error == 0) {
938                 freebsd32_rusage_out(&s, &s32);
939                 error = copyout(&s32, uap->rusage, sizeof(s32));
940         }
941         return (error);
942 }
943
944 static void
945 ptrace_lwpinfo_to32(const struct ptrace_lwpinfo *pl,
946     struct ptrace_lwpinfo32 *pl32)
947 {
948
949         bzero(pl32, sizeof(*pl32));
950         pl32->pl_lwpid = pl->pl_lwpid;
951         pl32->pl_event = pl->pl_event;
952         pl32->pl_flags = pl->pl_flags;
953         pl32->pl_sigmask = pl->pl_sigmask;
954         pl32->pl_siglist = pl->pl_siglist;
955         siginfo_to_siginfo32(&pl->pl_siginfo, &pl32->pl_siginfo);
956         strcpy(pl32->pl_tdname, pl->pl_tdname);
957         pl32->pl_child_pid = pl->pl_child_pid;
958         pl32->pl_syscall_code = pl->pl_syscall_code;
959         pl32->pl_syscall_narg = pl->pl_syscall_narg;
960 }
961
962 static void
963 ptrace_sc_ret_to32(const struct ptrace_sc_ret *psr,
964     struct ptrace_sc_ret32 *psr32)
965 {
966
967         bzero(psr32, sizeof(*psr32));
968         psr32->sr_retval[0] = psr->sr_retval[0];
969         psr32->sr_retval[1] = psr->sr_retval[1];
970         psr32->sr_error = psr->sr_error;
971 }
972
973 int
974 freebsd32_ptrace(struct thread *td, struct freebsd32_ptrace_args *uap)
975 {
976         union {
977                 struct ptrace_io_desc piod;
978                 struct ptrace_lwpinfo pl;
979                 struct ptrace_vm_entry pve;
980                 struct ptrace_coredump pc;
981                 struct ptrace_sc_remote sr;
982                 struct dbreg32 dbreg;
983                 struct fpreg32 fpreg;
984                 struct reg32 reg;
985                 struct iovec vec;
986                 register_t args[nitems(td->td_sa.args)];
987                 struct ptrace_sc_ret psr;
988                 int ptevents;
989         } r;
990         union {
991                 struct ptrace_io_desc32 piod;
992                 struct ptrace_lwpinfo32 pl;
993                 struct ptrace_vm_entry32 pve;
994                 struct ptrace_coredump32 pc;
995                 struct ptrace_sc_remote32 sr;
996                 uint32_t args[nitems(td->td_sa.args)];
997                 struct ptrace_sc_ret32 psr;
998                 struct iovec32 vec;
999         } r32;
1000         syscallarg_t pscr_args[nitems(td->td_sa.args)];
1001         u_int pscr_args32[nitems(td->td_sa.args)];
1002         void *addr;
1003         int data, error, i;
1004
1005         if (!allow_ptrace)
1006                 return (ENOSYS);
1007         error = 0;
1008
1009         AUDIT_ARG_PID(uap->pid);
1010         AUDIT_ARG_CMD(uap->req);
1011         AUDIT_ARG_VALUE(uap->data);
1012         addr = &r;
1013         data = uap->data;
1014         switch (uap->req) {
1015         case PT_GET_EVENT_MASK:
1016         case PT_GET_SC_ARGS:
1017         case PT_GET_SC_RET:
1018                 break;
1019         case PT_LWPINFO:
1020                 if (uap->data > sizeof(r32.pl))
1021                         return (EINVAL);
1022
1023                 /*
1024                  * Pass size of native structure in 'data'.  Truncate
1025                  * if necessary to avoid siginfo.
1026                  */
1027                 data = sizeof(r.pl);
1028                 if (uap->data < offsetof(struct ptrace_lwpinfo32, pl_siginfo) +
1029                     sizeof(struct siginfo32))
1030                         data = offsetof(struct ptrace_lwpinfo, pl_siginfo);
1031                 break;
1032         case PT_GETREGS:
1033                 bzero(&r.reg, sizeof(r.reg));
1034                 break;
1035         case PT_GETFPREGS:
1036                 bzero(&r.fpreg, sizeof(r.fpreg));
1037                 break;
1038         case PT_GETDBREGS:
1039                 bzero(&r.dbreg, sizeof(r.dbreg));
1040                 break;
1041         case PT_SETREGS:
1042                 error = copyin(uap->addr, &r.reg, sizeof(r.reg));
1043                 break;
1044         case PT_SETFPREGS:
1045                 error = copyin(uap->addr, &r.fpreg, sizeof(r.fpreg));
1046                 break;
1047         case PT_SETDBREGS:
1048                 error = copyin(uap->addr, &r.dbreg, sizeof(r.dbreg));
1049                 break;
1050         case PT_GETREGSET:
1051         case PT_SETREGSET:
1052                 error = copyin(uap->addr, &r32.vec, sizeof(r32.vec));
1053                 if (error != 0)
1054                         break;
1055
1056                 r.vec.iov_len = r32.vec.iov_len;
1057                 r.vec.iov_base = PTRIN(r32.vec.iov_base);
1058                 break;
1059         case PT_SET_EVENT_MASK:
1060                 if (uap->data != sizeof(r.ptevents))
1061                         error = EINVAL;
1062                 else
1063                         error = copyin(uap->addr, &r.ptevents, uap->data);
1064                 break;
1065         case PT_IO:
1066                 error = copyin(uap->addr, &r32.piod, sizeof(r32.piod));
1067                 if (error)
1068                         break;
1069                 CP(r32.piod, r.piod, piod_op);
1070                 PTRIN_CP(r32.piod, r.piod, piod_offs);
1071                 PTRIN_CP(r32.piod, r.piod, piod_addr);
1072                 CP(r32.piod, r.piod, piod_len);
1073                 break;
1074         case PT_VM_ENTRY:
1075                 error = copyin(uap->addr, &r32.pve, sizeof(r32.pve));
1076                 if (error)
1077                         break;
1078
1079                 CP(r32.pve, r.pve, pve_entry);
1080                 CP(r32.pve, r.pve, pve_timestamp);
1081                 CP(r32.pve, r.pve, pve_start);
1082                 CP(r32.pve, r.pve, pve_end);
1083                 CP(r32.pve, r.pve, pve_offset);
1084                 CP(r32.pve, r.pve, pve_prot);
1085                 CP(r32.pve, r.pve, pve_pathlen);
1086                 CP(r32.pve, r.pve, pve_fileid);
1087                 CP(r32.pve, r.pve, pve_fsid);
1088                 PTRIN_CP(r32.pve, r.pve, pve_path);
1089                 break;
1090         case PT_COREDUMP:
1091                 if (uap->data != sizeof(r32.pc))
1092                         error = EINVAL;
1093                 else
1094                         error = copyin(uap->addr, &r32.pc, uap->data);
1095                 CP(r32.pc, r.pc, pc_fd);
1096                 CP(r32.pc, r.pc, pc_flags);
1097                 r.pc.pc_limit = PAIR32TO64(off_t, r32.pc.pc_limit);
1098                 data = sizeof(r.pc);
1099                 break;
1100         case PT_SC_REMOTE:
1101                 if (uap->data != sizeof(r32.sr)) {
1102                         error = EINVAL;
1103                         break;
1104                 }
1105                 error = copyin(uap->addr, &r32.sr, uap->data);
1106                 if (error != 0)
1107                         break;
1108                 CP(r32.sr, r.sr, pscr_syscall);
1109                 CP(r32.sr, r.sr, pscr_nargs);
1110                 if (r.sr.pscr_nargs > nitems(td->td_sa.args)) {
1111                         error = EINVAL;
1112                         break;
1113                 }
1114                 error = copyin(PTRIN(r32.sr.pscr_args), pscr_args32,
1115                     sizeof(u_int) * r32.sr.pscr_nargs);
1116                 if (error != 0)
1117                         break;
1118                 for (i = 0; i < r32.sr.pscr_nargs; i++)
1119                         pscr_args[i] = pscr_args32[i];
1120                 r.sr.pscr_args = pscr_args;
1121                 break;
1122         default:
1123                 addr = uap->addr;
1124                 break;
1125         }
1126         if (error)
1127                 return (error);
1128
1129         error = kern_ptrace(td, uap->req, uap->pid, addr, data);
1130         if (error)
1131                 return (error);
1132
1133         switch (uap->req) {
1134         case PT_VM_ENTRY:
1135                 CP(r.pve, r32.pve, pve_entry);
1136                 CP(r.pve, r32.pve, pve_timestamp);
1137                 CP(r.pve, r32.pve, pve_start);
1138                 CP(r.pve, r32.pve, pve_end);
1139                 CP(r.pve, r32.pve, pve_offset);
1140                 CP(r.pve, r32.pve, pve_prot);
1141                 CP(r.pve, r32.pve, pve_pathlen);
1142                 CP(r.pve, r32.pve, pve_fileid);
1143                 CP(r.pve, r32.pve, pve_fsid);
1144                 error = copyout(&r32.pve, uap->addr, sizeof(r32.pve));
1145                 break;
1146         case PT_IO:
1147                 CP(r.piod, r32.piod, piod_len);
1148                 error = copyout(&r32.piod, uap->addr, sizeof(r32.piod));
1149                 break;
1150         case PT_GETREGS:
1151                 error = copyout(&r.reg, uap->addr, sizeof(r.reg));
1152                 break;
1153         case PT_GETFPREGS:
1154                 error = copyout(&r.fpreg, uap->addr, sizeof(r.fpreg));
1155                 break;
1156         case PT_GETDBREGS:
1157                 error = copyout(&r.dbreg, uap->addr, sizeof(r.dbreg));
1158                 break;
1159         case PT_GETREGSET:
1160                 r32.vec.iov_len = r.vec.iov_len;
1161                 error = copyout(&r32.vec, uap->addr, sizeof(r32.vec));
1162                 break;
1163         case PT_GET_EVENT_MASK:
1164                 /* NB: The size in uap->data is validated in kern_ptrace(). */
1165                 error = copyout(&r.ptevents, uap->addr, uap->data);
1166                 break;
1167         case PT_LWPINFO:
1168                 ptrace_lwpinfo_to32(&r.pl, &r32.pl);
1169                 error = copyout(&r32.pl, uap->addr, uap->data);
1170                 break;
1171         case PT_GET_SC_ARGS:
1172                 for (i = 0; i < nitems(r.args); i++)
1173                         r32.args[i] = (uint32_t)r.args[i];
1174                 error = copyout(r32.args, uap->addr, MIN(uap->data,
1175                     sizeof(r32.args)));
1176                 break;
1177         case PT_GET_SC_RET:
1178                 ptrace_sc_ret_to32(&r.psr, &r32.psr);
1179                 error = copyout(&r32.psr, uap->addr, MIN(uap->data,
1180                     sizeof(r32.psr)));
1181                 break;
1182         case PT_SC_REMOTE:
1183                 ptrace_sc_ret_to32(&r.sr.pscr_ret, &r32.sr.pscr_ret);
1184                 error = copyout(&r32.sr.pscr_ret, uap->addr +
1185                     offsetof(struct ptrace_sc_remote32, pscr_ret),
1186                     sizeof(r32.psr));
1187                 break;
1188         }
1189
1190         return (error);
1191 }
1192
1193 int
1194 freebsd32_copyinuio(struct iovec32 *iovp, u_int iovcnt, struct uio **uiop)
1195 {
1196         struct iovec32 iov32;
1197         struct iovec *iov;
1198         struct uio *uio;
1199         u_int iovlen;
1200         int error, i;
1201
1202         *uiop = NULL;
1203         if (iovcnt > UIO_MAXIOV)
1204                 return (EINVAL);
1205         iovlen = iovcnt * sizeof(struct iovec);
1206         uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
1207         iov = (struct iovec *)(uio + 1);
1208         for (i = 0; i < iovcnt; i++) {
1209                 error = copyin(&iovp[i], &iov32, sizeof(struct iovec32));
1210                 if (error) {
1211                         free(uio, M_IOV);
1212                         return (error);
1213                 }
1214                 iov[i].iov_base = PTRIN(iov32.iov_base);
1215                 iov[i].iov_len = iov32.iov_len;
1216         }
1217         uio->uio_iov = iov;
1218         uio->uio_iovcnt = iovcnt;
1219         uio->uio_segflg = UIO_USERSPACE;
1220         uio->uio_offset = -1;
1221         uio->uio_resid = 0;
1222         for (i = 0; i < iovcnt; i++) {
1223                 if (iov->iov_len > INT_MAX - uio->uio_resid) {
1224                         free(uio, M_IOV);
1225                         return (EINVAL);
1226                 }
1227                 uio->uio_resid += iov->iov_len;
1228                 iov++;
1229         }
1230         *uiop = uio;
1231         return (0);
1232 }
1233
1234 int
1235 freebsd32_readv(struct thread *td, struct freebsd32_readv_args *uap)
1236 {
1237         struct uio *auio;
1238         int error;
1239
1240         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1241         if (error)
1242                 return (error);
1243         error = kern_readv(td, uap->fd, auio);
1244         free(auio, M_IOV);
1245         return (error);
1246 }
1247
1248 int
1249 freebsd32_writev(struct thread *td, struct freebsd32_writev_args *uap)
1250 {
1251         struct uio *auio;
1252         int error;
1253
1254         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1255         if (error)
1256                 return (error);
1257         error = kern_writev(td, uap->fd, auio);
1258         free(auio, M_IOV);
1259         return (error);
1260 }
1261
1262 int
1263 freebsd32_preadv(struct thread *td, struct freebsd32_preadv_args *uap)
1264 {
1265         struct uio *auio;
1266         int error;
1267
1268         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1269         if (error)
1270                 return (error);
1271         error = kern_preadv(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
1272         free(auio, M_IOV);
1273         return (error);
1274 }
1275
1276 int
1277 freebsd32_pwritev(struct thread *td, struct freebsd32_pwritev_args *uap)
1278 {
1279         struct uio *auio;
1280         int error;
1281
1282         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1283         if (error)
1284                 return (error);
1285         error = kern_pwritev(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
1286         free(auio, M_IOV);
1287         return (error);
1288 }
1289
1290 int
1291 freebsd32_copyiniov(struct iovec32 *iovp32, u_int iovcnt, struct iovec **iovp,
1292     int error)
1293 {
1294         struct iovec32 iov32;
1295         struct iovec *iov;
1296         u_int iovlen;
1297         int i;
1298
1299         *iovp = NULL;
1300         if (iovcnt > UIO_MAXIOV)
1301                 return (error);
1302         iovlen = iovcnt * sizeof(struct iovec);
1303         iov = malloc(iovlen, M_IOV, M_WAITOK);
1304         for (i = 0; i < iovcnt; i++) {
1305                 error = copyin(&iovp32[i], &iov32, sizeof(struct iovec32));
1306                 if (error) {
1307                         free(iov, M_IOV);
1308                         return (error);
1309                 }
1310                 iov[i].iov_base = PTRIN(iov32.iov_base);
1311                 iov[i].iov_len = iov32.iov_len;
1312         }
1313         *iovp = iov;
1314         return (0);
1315 }
1316
1317 static int
1318 freebsd32_copyinmsghdr(const struct msghdr32 *msg32, struct msghdr *msg)
1319 {
1320         struct msghdr32 m32;
1321         int error;
1322
1323         error = copyin(msg32, &m32, sizeof(m32));
1324         if (error)
1325                 return (error);
1326         msg->msg_name = PTRIN(m32.msg_name);
1327         msg->msg_namelen = m32.msg_namelen;
1328         msg->msg_iov = PTRIN(m32.msg_iov);
1329         msg->msg_iovlen = m32.msg_iovlen;
1330         msg->msg_control = PTRIN(m32.msg_control);
1331         msg->msg_controllen = m32.msg_controllen;
1332         msg->msg_flags = m32.msg_flags;
1333         return (0);
1334 }
1335
1336 static int
1337 freebsd32_copyoutmsghdr(struct msghdr *msg, struct msghdr32 *msg32)
1338 {
1339         struct msghdr32 m32;
1340         int error;
1341
1342         m32.msg_name = PTROUT(msg->msg_name);
1343         m32.msg_namelen = msg->msg_namelen;
1344         m32.msg_iov = PTROUT(msg->msg_iov);
1345         m32.msg_iovlen = msg->msg_iovlen;
1346         m32.msg_control = PTROUT(msg->msg_control);
1347         m32.msg_controllen = msg->msg_controllen;
1348         m32.msg_flags = msg->msg_flags;
1349         error = copyout(&m32, msg32, sizeof(m32));
1350         return (error);
1351 }
1352
1353 #define FREEBSD32_ALIGNBYTES    (sizeof(int) - 1)
1354 #define FREEBSD32_ALIGN(p)      \
1355         (((u_long)(p) + FREEBSD32_ALIGNBYTES) & ~FREEBSD32_ALIGNBYTES)
1356 #define FREEBSD32_CMSG_SPACE(l) \
1357         (FREEBSD32_ALIGN(sizeof(struct cmsghdr)) + FREEBSD32_ALIGN(l))
1358
1359 #define FREEBSD32_CMSG_DATA(cmsg)       ((unsigned char *)(cmsg) + \
1360                                  FREEBSD32_ALIGN(sizeof(struct cmsghdr)))
1361
1362 static size_t
1363 freebsd32_cmsg_convert(const struct cmsghdr *cm, void *data, socklen_t datalen)
1364 {
1365         size_t copylen;
1366         union {
1367                 struct timespec32 ts;
1368                 struct timeval32 tv;
1369                 struct bintime32 bt;
1370         } tmp32;
1371
1372         union {
1373                 struct timespec ts;
1374                 struct timeval tv;
1375                 struct bintime bt;
1376         } *in;
1377
1378         in = data;
1379         copylen = 0;
1380         switch (cm->cmsg_level) {
1381         case SOL_SOCKET:
1382                 switch (cm->cmsg_type) {
1383                 case SCM_TIMESTAMP:
1384                         TV_CP(*in, tmp32, tv);
1385                         copylen = sizeof(tmp32.tv);
1386                         break;
1387
1388                 case SCM_BINTIME:
1389                         BT_CP(*in, tmp32, bt);
1390                         copylen = sizeof(tmp32.bt);
1391                         break;
1392
1393                 case SCM_REALTIME:
1394                 case SCM_MONOTONIC:
1395                         TS_CP(*in, tmp32, ts);
1396                         copylen = sizeof(tmp32.ts);
1397                         break;
1398
1399                 default:
1400                         break;
1401                 }
1402
1403         default:
1404                 break;
1405         }
1406
1407         if (copylen == 0)
1408                 return (datalen);
1409
1410         KASSERT((datalen >= copylen), ("corrupted cmsghdr"));
1411
1412         bcopy(&tmp32, data, copylen);
1413         return (copylen);
1414 }
1415
1416 static int
1417 freebsd32_copy_msg_out(struct msghdr *msg, struct mbuf *control)
1418 {
1419         struct cmsghdr *cm;
1420         void *data;
1421         socklen_t clen, datalen, datalen_out, oldclen;
1422         int error;
1423         caddr_t ctlbuf;
1424         int len, copylen;
1425         struct mbuf *m;
1426         error = 0;
1427
1428         len    = msg->msg_controllen;
1429         msg->msg_controllen = 0;
1430
1431         ctlbuf = msg->msg_control;
1432         for (m = control; m != NULL && len > 0; m = m->m_next) {
1433                 cm = mtod(m, struct cmsghdr *);
1434                 clen = m->m_len;
1435                 while (cm != NULL) {
1436                         if (sizeof(struct cmsghdr) > clen ||
1437                             cm->cmsg_len > clen) {
1438                                 error = EINVAL;
1439                                 break;
1440                         }
1441
1442                         data   = CMSG_DATA(cm);
1443                         datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1444                         datalen_out = freebsd32_cmsg_convert(cm, data, datalen);
1445
1446                         /*
1447                          * Copy out the message header.  Preserve the native
1448                          * message size in case we need to inspect the message
1449                          * contents later.
1450                          */
1451                         copylen = sizeof(struct cmsghdr);
1452                         if (len < copylen) {
1453                                 msg->msg_flags |= MSG_CTRUNC;
1454                                 m_dispose_extcontrolm(m);
1455                                 goto exit;
1456                         }
1457                         oldclen = cm->cmsg_len;
1458                         cm->cmsg_len = FREEBSD32_ALIGN(sizeof(struct cmsghdr)) +
1459                             datalen_out;
1460                         error = copyout(cm, ctlbuf, copylen);
1461                         cm->cmsg_len = oldclen;
1462                         if (error != 0)
1463                                 goto exit;
1464
1465                         ctlbuf += FREEBSD32_ALIGN(copylen);
1466                         len    -= FREEBSD32_ALIGN(copylen);
1467
1468                         copylen = datalen_out;
1469                         if (len < copylen) {
1470                                 msg->msg_flags |= MSG_CTRUNC;
1471                                 m_dispose_extcontrolm(m);
1472                                 break;
1473                         }
1474
1475                         /* Copy out the message data. */
1476                         error = copyout(data, ctlbuf, copylen);
1477                         if (error)
1478                                 goto exit;
1479
1480                         ctlbuf += FREEBSD32_ALIGN(copylen);
1481                         len    -= FREEBSD32_ALIGN(copylen);
1482
1483                         if (CMSG_SPACE(datalen) < clen) {
1484                                 clen -= CMSG_SPACE(datalen);
1485                                 cm = (struct cmsghdr *)
1486                                     ((caddr_t)cm + CMSG_SPACE(datalen));
1487                         } else {
1488                                 clen = 0;
1489                                 cm = NULL;
1490                         }
1491
1492                         msg->msg_controllen +=
1493                             FREEBSD32_CMSG_SPACE(datalen_out);
1494                 }
1495         }
1496         if (len == 0 && m != NULL) {
1497                 msg->msg_flags |= MSG_CTRUNC;
1498                 m_dispose_extcontrolm(m);
1499         }
1500
1501 exit:
1502         return (error);
1503 }
1504
1505 int
1506 freebsd32_recvmsg(struct thread *td, struct freebsd32_recvmsg_args *uap)
1507 {
1508         struct msghdr msg;
1509         struct iovec *uiov, *iov;
1510         struct mbuf *control = NULL;
1511         struct mbuf **controlp;
1512         int error;
1513
1514         error = freebsd32_copyinmsghdr(uap->msg, &msg);
1515         if (error)
1516                 return (error);
1517         error = freebsd32_copyiniov((void *)msg.msg_iov, msg.msg_iovlen, &iov,
1518             EMSGSIZE);
1519         if (error)
1520                 return (error);
1521         msg.msg_flags = uap->flags;
1522         uiov = msg.msg_iov;
1523         msg.msg_iov = iov;
1524
1525         controlp = (msg.msg_control != NULL) ?  &control : NULL;
1526         error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, controlp);
1527         if (error == 0) {
1528                 msg.msg_iov = uiov;
1529
1530                 if (control != NULL)
1531                         error = freebsd32_copy_msg_out(&msg, control);
1532                 else
1533                         msg.msg_controllen = 0;
1534
1535                 if (error == 0)
1536                         error = freebsd32_copyoutmsghdr(&msg, uap->msg);
1537         }
1538         free(iov, M_IOV);
1539
1540         if (control != NULL) {
1541                 if (error != 0)
1542                         m_dispose_extcontrolm(control);
1543                 m_freem(control);
1544         }
1545
1546         return (error);
1547 }
1548
1549 #ifdef COMPAT_43
1550 int
1551 ofreebsd32_recvmsg(struct thread *td, struct ofreebsd32_recvmsg_args *uap)
1552 {
1553         return (ENOSYS);
1554 }
1555 #endif
1556
1557 /*
1558  * Copy-in the array of control messages constructed using alignment
1559  * and padding suitable for a 32-bit environment and construct an
1560  * mbuf using alignment and padding suitable for a 64-bit kernel.
1561  * The alignment and padding are defined indirectly by CMSG_DATA(),
1562  * CMSG_SPACE() and CMSG_LEN().
1563  */
1564 static int
1565 freebsd32_copyin_control(struct mbuf **mp, caddr_t buf, u_int buflen)
1566 {
1567         struct cmsghdr *cm;
1568         struct mbuf *m;
1569         void *in, *in1, *md;
1570         u_int msglen, outlen;
1571         int error;
1572
1573         /* Enforce the size limit of the native implementation. */
1574         if (buflen > MCLBYTES)
1575                 return (EINVAL);
1576
1577         in = malloc(buflen, M_TEMP, M_WAITOK);
1578         error = copyin(buf, in, buflen);
1579         if (error != 0)
1580                 goto out;
1581
1582         /*
1583          * Make a pass over the input buffer to determine the amount of space
1584          * required for 64 bit-aligned copies of the control messages.
1585          */
1586         in1 = in;
1587         outlen = 0;
1588         while (buflen > 0) {
1589                 if (buflen < sizeof(*cm)) {
1590                         error = EINVAL;
1591                         break;
1592                 }
1593                 cm = (struct cmsghdr *)in1;
1594                 if (cm->cmsg_len < FREEBSD32_ALIGN(sizeof(*cm)) ||
1595                     cm->cmsg_len > buflen) {
1596                         error = EINVAL;
1597                         break;
1598                 }
1599                 msglen = FREEBSD32_ALIGN(cm->cmsg_len);
1600                 if (msglen < cm->cmsg_len) {
1601                         error = EINVAL;
1602                         break;
1603                 }
1604                 /* The native ABI permits the final padding to be omitted. */
1605                 if (msglen > buflen)
1606                         msglen = buflen;
1607                 buflen -= msglen;
1608
1609                 in1 = (char *)in1 + msglen;
1610                 outlen += CMSG_ALIGN(sizeof(*cm)) +
1611                     CMSG_ALIGN(msglen - FREEBSD32_ALIGN(sizeof(*cm)));
1612         }
1613         if (error != 0)
1614                 goto out;
1615
1616         /*
1617          * Allocate up to MJUMPAGESIZE space for the re-aligned and
1618          * re-padded control messages.  This allows a full MCLBYTES of
1619          * 32-bit sized and aligned messages to fit and avoids an ABI
1620          * mismatch with the native implementation.
1621          */
1622         m = m_get2(outlen, M_WAITOK, MT_CONTROL, 0);
1623         if (m == NULL) {
1624                 error = EINVAL;
1625                 goto out;
1626         }
1627         m->m_len = outlen;
1628         md = mtod(m, void *);
1629
1630         /*
1631          * Make a second pass over input messages, copying them into the output
1632          * buffer.
1633          */
1634         in1 = in;
1635         while (outlen > 0) {
1636                 /* Copy the message header and align the length field. */
1637                 cm = md;
1638                 memcpy(cm, in1, sizeof(*cm));
1639                 msglen = cm->cmsg_len - FREEBSD32_ALIGN(sizeof(*cm));
1640                 cm->cmsg_len = CMSG_ALIGN(sizeof(*cm)) + msglen;
1641
1642                 /* Copy the message body. */
1643                 in1 = (char *)in1 + FREEBSD32_ALIGN(sizeof(*cm));
1644                 md = (char *)md + CMSG_ALIGN(sizeof(*cm));
1645                 memcpy(md, in1, msglen);
1646                 in1 = (char *)in1 + FREEBSD32_ALIGN(msglen);
1647                 md = (char *)md + CMSG_ALIGN(msglen);
1648                 KASSERT(outlen >= CMSG_ALIGN(sizeof(*cm)) + CMSG_ALIGN(msglen),
1649                     ("outlen %u underflow, msglen %u", outlen, msglen));
1650                 outlen -= CMSG_ALIGN(sizeof(*cm)) + CMSG_ALIGN(msglen);
1651         }
1652
1653         *mp = m;
1654 out:
1655         free(in, M_TEMP);
1656         return (error);
1657 }
1658
1659 int
1660 freebsd32_sendmsg(struct thread *td, struct freebsd32_sendmsg_args *uap)
1661 {
1662         struct msghdr msg;
1663         struct iovec *iov;
1664         struct mbuf *control = NULL;
1665         struct sockaddr *to = NULL;
1666         int error;
1667
1668         error = freebsd32_copyinmsghdr(uap->msg, &msg);
1669         if (error)
1670                 return (error);
1671         error = freebsd32_copyiniov((void *)msg.msg_iov, msg.msg_iovlen, &iov,
1672             EMSGSIZE);
1673         if (error)
1674                 return (error);
1675         msg.msg_iov = iov;
1676         if (msg.msg_name != NULL) {
1677                 error = getsockaddr(&to, msg.msg_name, msg.msg_namelen);
1678                 if (error) {
1679                         to = NULL;
1680                         goto out;
1681                 }
1682                 msg.msg_name = to;
1683         }
1684
1685         if (msg.msg_control) {
1686                 if (msg.msg_controllen < sizeof(struct cmsghdr)) {
1687                         error = EINVAL;
1688                         goto out;
1689                 }
1690
1691                 error = freebsd32_copyin_control(&control, msg.msg_control,
1692                     msg.msg_controllen);
1693                 if (error)
1694                         goto out;
1695
1696                 msg.msg_control = NULL;
1697                 msg.msg_controllen = 0;
1698         }
1699
1700         error = kern_sendit(td, uap->s, &msg, uap->flags, control,
1701             UIO_USERSPACE);
1702
1703 out:
1704         free(iov, M_IOV);
1705         if (to)
1706                 free(to, M_SONAME);
1707         return (error);
1708 }
1709
1710 #ifdef COMPAT_43
1711 int
1712 ofreebsd32_sendmsg(struct thread *td, struct ofreebsd32_sendmsg_args *uap)
1713 {
1714         return (ENOSYS);
1715 }
1716 #endif
1717
1718
1719 int
1720 freebsd32_settimeofday(struct thread *td,
1721                        struct freebsd32_settimeofday_args *uap)
1722 {
1723         struct timeval32 tv32;
1724         struct timeval tv, *tvp;
1725         struct timezone tz, *tzp;
1726         int error;
1727
1728         if (uap->tv) {
1729                 error = copyin(uap->tv, &tv32, sizeof(tv32));
1730                 if (error)
1731                         return (error);
1732                 CP(tv32, tv, tv_sec);
1733                 CP(tv32, tv, tv_usec);
1734                 tvp = &tv;
1735         } else
1736                 tvp = NULL;
1737         if (uap->tzp) {
1738                 error = copyin(uap->tzp, &tz, sizeof(tz));
1739                 if (error)
1740                         return (error);
1741                 tzp = &tz;
1742         } else
1743                 tzp = NULL;
1744         return (kern_settimeofday(td, tvp, tzp));
1745 }
1746
1747 int
1748 freebsd32_utimes(struct thread *td, struct freebsd32_utimes_args *uap)
1749 {
1750         struct timeval32 s32[2];
1751         struct timeval s[2], *sp;
1752         int error;
1753
1754         if (uap->tptr != NULL) {
1755                 error = copyin(uap->tptr, s32, sizeof(s32));
1756                 if (error)
1757                         return (error);
1758                 CP(s32[0], s[0], tv_sec);
1759                 CP(s32[0], s[0], tv_usec);
1760                 CP(s32[1], s[1], tv_sec);
1761                 CP(s32[1], s[1], tv_usec);
1762                 sp = s;
1763         } else
1764                 sp = NULL;
1765         return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1766             sp, UIO_SYSSPACE));
1767 }
1768
1769 int
1770 freebsd32_lutimes(struct thread *td, struct freebsd32_lutimes_args *uap)
1771 {
1772         struct timeval32 s32[2];
1773         struct timeval s[2], *sp;
1774         int error;
1775
1776         if (uap->tptr != NULL) {
1777                 error = copyin(uap->tptr, s32, sizeof(s32));
1778                 if (error)
1779                         return (error);
1780                 CP(s32[0], s[0], tv_sec);
1781                 CP(s32[0], s[0], tv_usec);
1782                 CP(s32[1], s[1], tv_sec);
1783                 CP(s32[1], s[1], tv_usec);
1784                 sp = s;
1785         } else
1786                 sp = NULL;
1787         return (kern_lutimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1788 }
1789
1790 int
1791 freebsd32_futimes(struct thread *td, struct freebsd32_futimes_args *uap)
1792 {
1793         struct timeval32 s32[2];
1794         struct timeval s[2], *sp;
1795         int error;
1796
1797         if (uap->tptr != NULL) {
1798                 error = copyin(uap->tptr, s32, sizeof(s32));
1799                 if (error)
1800                         return (error);
1801                 CP(s32[0], s[0], tv_sec);
1802                 CP(s32[0], s[0], tv_usec);
1803                 CP(s32[1], s[1], tv_sec);
1804                 CP(s32[1], s[1], tv_usec);
1805                 sp = s;
1806         } else
1807                 sp = NULL;
1808         return (kern_futimes(td, uap->fd, sp, UIO_SYSSPACE));
1809 }
1810
1811 int
1812 freebsd32_futimesat(struct thread *td, struct freebsd32_futimesat_args *uap)
1813 {
1814         struct timeval32 s32[2];
1815         struct timeval s[2], *sp;
1816         int error;
1817
1818         if (uap->times != NULL) {
1819                 error = copyin(uap->times, s32, sizeof(s32));
1820                 if (error)
1821                         return (error);
1822                 CP(s32[0], s[0], tv_sec);
1823                 CP(s32[0], s[0], tv_usec);
1824                 CP(s32[1], s[1], tv_sec);
1825                 CP(s32[1], s[1], tv_usec);
1826                 sp = s;
1827         } else
1828                 sp = NULL;
1829         return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
1830                 sp, UIO_SYSSPACE));
1831 }
1832
1833 int
1834 freebsd32_futimens(struct thread *td, struct freebsd32_futimens_args *uap)
1835 {
1836         struct timespec32 ts32[2];
1837         struct timespec ts[2], *tsp;
1838         int error;
1839
1840         if (uap->times != NULL) {
1841                 error = copyin(uap->times, ts32, sizeof(ts32));
1842                 if (error)
1843                         return (error);
1844                 CP(ts32[0], ts[0], tv_sec);
1845                 CP(ts32[0], ts[0], tv_nsec);
1846                 CP(ts32[1], ts[1], tv_sec);
1847                 CP(ts32[1], ts[1], tv_nsec);
1848                 tsp = ts;
1849         } else
1850                 tsp = NULL;
1851         return (kern_futimens(td, uap->fd, tsp, UIO_SYSSPACE));
1852 }
1853
1854 int
1855 freebsd32_utimensat(struct thread *td, struct freebsd32_utimensat_args *uap)
1856 {
1857         struct timespec32 ts32[2];
1858         struct timespec ts[2], *tsp;
1859         int error;
1860
1861         if (uap->times != NULL) {
1862                 error = copyin(uap->times, ts32, sizeof(ts32));
1863                 if (error)
1864                         return (error);
1865                 CP(ts32[0], ts[0], tv_sec);
1866                 CP(ts32[0], ts[0], tv_nsec);
1867                 CP(ts32[1], ts[1], tv_sec);
1868                 CP(ts32[1], ts[1], tv_nsec);
1869                 tsp = ts;
1870         } else
1871                 tsp = NULL;
1872         return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
1873             tsp, UIO_SYSSPACE, uap->flag));
1874 }
1875
1876 int
1877 freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap)
1878 {
1879         struct timeval32 tv32;
1880         struct timeval delta, olddelta, *deltap;
1881         int error;
1882
1883         if (uap->delta) {
1884                 error = copyin(uap->delta, &tv32, sizeof(tv32));
1885                 if (error)
1886                         return (error);
1887                 CP(tv32, delta, tv_sec);
1888                 CP(tv32, delta, tv_usec);
1889                 deltap = &delta;
1890         } else
1891                 deltap = NULL;
1892         error = kern_adjtime(td, deltap, &olddelta);
1893         if (uap->olddelta && error == 0) {
1894                 CP(olddelta, tv32, tv_sec);
1895                 CP(olddelta, tv32, tv_usec);
1896                 error = copyout(&tv32, uap->olddelta, sizeof(tv32));
1897         }
1898         return (error);
1899 }
1900
1901 #ifdef COMPAT_FREEBSD4
1902 int
1903 freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap)
1904 {
1905         struct ostatfs32 s32;
1906         struct statfs *sp;
1907         int error;
1908
1909         sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1910         error = kern_statfs(td, uap->path, UIO_USERSPACE, sp);
1911         if (error == 0) {
1912                 copy_statfs(sp, &s32);
1913                 error = copyout(&s32, uap->buf, sizeof(s32));
1914         }
1915         free(sp, M_STATFS);
1916         return (error);
1917 }
1918 #endif
1919
1920 #ifdef COMPAT_FREEBSD4
1921 int
1922 freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap)
1923 {
1924         struct ostatfs32 s32;
1925         struct statfs *sp;
1926         int error;
1927
1928         sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1929         error = kern_fstatfs(td, uap->fd, sp);
1930         if (error == 0) {
1931                 copy_statfs(sp, &s32);
1932                 error = copyout(&s32, uap->buf, sizeof(s32));
1933         }
1934         free(sp, M_STATFS);
1935         return (error);
1936 }
1937 #endif
1938
1939 #ifdef COMPAT_FREEBSD4
1940 int
1941 freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap)
1942 {
1943         struct ostatfs32 s32;
1944         struct statfs *sp;
1945         fhandle_t fh;
1946         int error;
1947
1948         if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
1949                 return (error);
1950         sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1951         error = kern_fhstatfs(td, fh, sp);
1952         if (error == 0) {
1953                 copy_statfs(sp, &s32);
1954                 error = copyout(&s32, uap->buf, sizeof(s32));
1955         }
1956         free(sp, M_STATFS);
1957         return (error);
1958 }
1959 #endif
1960
1961 int
1962 freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap)
1963 {
1964
1965         return (kern_pread(td, uap->fd, uap->buf, uap->nbyte,
1966             PAIR32TO64(off_t, uap->offset)));
1967 }
1968
1969 int
1970 freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap)
1971 {
1972
1973         return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte,
1974             PAIR32TO64(off_t, uap->offset)));
1975 }
1976
1977 #ifdef COMPAT_43
1978 int
1979 ofreebsd32_lseek(struct thread *td, struct ofreebsd32_lseek_args *uap)
1980 {
1981
1982         return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1983 }
1984 #endif
1985
1986 int
1987 freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap)
1988 {
1989         int error;
1990         off_t pos;
1991
1992         error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset),
1993             uap->whence);
1994         /* Expand the quad return into two parts for eax and edx */
1995         pos = td->td_uretoff.tdu_off;
1996         td->td_retval[RETVAL_LO] = pos & 0xffffffff;    /* %eax */
1997         td->td_retval[RETVAL_HI] = pos >> 32;           /* %edx */
1998         return error;
1999 }
2000
2001 int
2002 freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap)
2003 {
2004
2005         return (kern_truncate(td, uap->path, UIO_USERSPACE,
2006             PAIR32TO64(off_t, uap->length)));
2007 }
2008
2009 #ifdef COMPAT_43
2010 int
2011 ofreebsd32_truncate(struct thread *td, struct ofreebsd32_truncate_args *uap)
2012 {
2013         return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
2014 }
2015 #endif
2016
2017 int
2018 freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap)
2019 {
2020
2021         return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length)));
2022 }
2023
2024 #ifdef COMPAT_43
2025 int
2026 ofreebsd32_ftruncate(struct thread *td, struct ofreebsd32_ftruncate_args *uap)
2027 {
2028         return (kern_ftruncate(td, uap->fd, uap->length));
2029 }
2030
2031 int
2032 ofreebsd32_getdirentries(struct thread *td,
2033     struct ofreebsd32_getdirentries_args *uap)
2034 {
2035         struct ogetdirentries_args ap;
2036         int error;
2037         long loff;
2038         int32_t loff_cut;
2039
2040         ap.fd = uap->fd;
2041         ap.buf = uap->buf;
2042         ap.count = uap->count;
2043         ap.basep = NULL;
2044         error = kern_ogetdirentries(td, &ap, &loff);
2045         if (error == 0) {
2046                 loff_cut = loff;
2047                 error = copyout(&loff_cut, uap->basep, sizeof(int32_t));
2048         }
2049         return (error);
2050 }
2051 #endif
2052
2053 #if defined(COMPAT_FREEBSD11)
2054 int
2055 freebsd11_freebsd32_getdirentries(struct thread *td,
2056     struct freebsd11_freebsd32_getdirentries_args *uap)
2057 {
2058         long base;
2059         int32_t base32;
2060         int error;
2061
2062         error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
2063             &base, NULL);
2064         if (error)
2065                 return (error);
2066         if (uap->basep != NULL) {
2067                 base32 = base;
2068                 error = copyout(&base32, uap->basep, sizeof(int32_t));
2069         }
2070         return (error);
2071 }
2072 #endif /* COMPAT_FREEBSD11 */
2073
2074 #ifdef COMPAT_FREEBSD6
2075 /* versions with the 'int pad' argument */
2076 int
2077 freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap)
2078 {
2079
2080         return (kern_pread(td, uap->fd, uap->buf, uap->nbyte,
2081             PAIR32TO64(off_t, uap->offset)));
2082 }
2083
2084 int
2085 freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap)
2086 {
2087
2088         return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte,
2089             PAIR32TO64(off_t, uap->offset)));
2090 }
2091
2092 int
2093 freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap)
2094 {
2095         int error;
2096         off_t pos;
2097
2098         error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset),
2099             uap->whence);
2100         /* Expand the quad return into two parts for eax and edx */
2101         pos = *(off_t *)(td->td_retval);
2102         td->td_retval[RETVAL_LO] = pos & 0xffffffff;    /* %eax */
2103         td->td_retval[RETVAL_HI] = pos >> 32;           /* %edx */
2104         return error;
2105 }
2106
2107 int
2108 freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap)
2109 {
2110
2111         return (kern_truncate(td, uap->path, UIO_USERSPACE,
2112             PAIR32TO64(off_t, uap->length)));
2113 }
2114
2115 int
2116 freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap)
2117 {
2118
2119         return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length)));
2120 }
2121 #endif /* COMPAT_FREEBSD6 */
2122
2123 struct sf_hdtr32 {
2124         uint32_t headers;
2125         int hdr_cnt;
2126         uint32_t trailers;
2127         int trl_cnt;
2128 };
2129
2130 static int
2131 freebsd32_do_sendfile(struct thread *td,
2132     struct freebsd32_sendfile_args *uap, int compat)
2133 {
2134         struct sf_hdtr32 hdtr32;
2135         struct sf_hdtr hdtr;
2136         struct uio *hdr_uio, *trl_uio;
2137         struct file *fp;
2138         cap_rights_t rights;
2139         struct iovec32 *iov32;
2140         off_t offset, sbytes;
2141         int error;
2142
2143         offset = PAIR32TO64(off_t, uap->offset);
2144         if (offset < 0)
2145                 return (EINVAL);
2146
2147         hdr_uio = trl_uio = NULL;
2148
2149         if (uap->hdtr != NULL) {
2150                 error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
2151                 if (error)
2152                         goto out;
2153                 PTRIN_CP(hdtr32, hdtr, headers);
2154                 CP(hdtr32, hdtr, hdr_cnt);
2155                 PTRIN_CP(hdtr32, hdtr, trailers);
2156                 CP(hdtr32, hdtr, trl_cnt);
2157
2158                 if (hdtr.headers != NULL) {
2159                         iov32 = PTRIN(hdtr32.headers);
2160                         error = freebsd32_copyinuio(iov32,
2161                             hdtr32.hdr_cnt, &hdr_uio);
2162                         if (error)
2163                                 goto out;
2164 #ifdef COMPAT_FREEBSD4
2165                         /*
2166                          * In FreeBSD < 5.0 the nbytes to send also included
2167                          * the header.  If compat is specified subtract the
2168                          * header size from nbytes.
2169                          */
2170                         if (compat) {
2171                                 if (uap->nbytes > hdr_uio->uio_resid)
2172                                         uap->nbytes -= hdr_uio->uio_resid;
2173                                 else
2174                                         uap->nbytes = 0;
2175                         }
2176 #endif
2177                 }
2178                 if (hdtr.trailers != NULL) {
2179                         iov32 = PTRIN(hdtr32.trailers);
2180                         error = freebsd32_copyinuio(iov32,
2181                             hdtr32.trl_cnt, &trl_uio);
2182                         if (error)
2183                                 goto out;
2184                 }
2185         }
2186
2187         AUDIT_ARG_FD(uap->fd);
2188
2189         if ((error = fget_read(td, uap->fd,
2190             cap_rights_init_one(&rights, CAP_PREAD), &fp)) != 0)
2191                 goto out;
2192
2193         error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, offset,
2194             uap->nbytes, &sbytes, uap->flags, td);
2195         fdrop(fp, td);
2196
2197         if (uap->sbytes != NULL)
2198                 (void)copyout(&sbytes, uap->sbytes, sizeof(off_t));
2199
2200 out:
2201         if (hdr_uio)
2202                 free(hdr_uio, M_IOV);
2203         if (trl_uio)
2204                 free(trl_uio, M_IOV);
2205         return (error);
2206 }
2207
2208 #ifdef COMPAT_FREEBSD4
2209 int
2210 freebsd4_freebsd32_sendfile(struct thread *td,
2211     struct freebsd4_freebsd32_sendfile_args *uap)
2212 {
2213         return (freebsd32_do_sendfile(td,
2214             (struct freebsd32_sendfile_args *)uap, 1));
2215 }
2216 #endif
2217
2218 int
2219 freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
2220 {
2221
2222         return (freebsd32_do_sendfile(td, uap, 0));
2223 }
2224
2225 static void
2226 copy_stat(struct stat *in, struct stat32 *out)
2227 {
2228
2229 #ifndef __amd64__
2230         /*
2231          * 32-bit architectures other than i386 have 64-bit time_t.  This
2232          * results in struct timespec32 with 12 bytes for tv_sec and tv_nsec,
2233          * and 4 bytes of padding.  Zero the padding holes in struct stat32.
2234          */
2235         bzero(&out->st_atim, sizeof(out->st_atim));
2236         bzero(&out->st_mtim, sizeof(out->st_mtim));
2237         bzero(&out->st_ctim, sizeof(out->st_ctim));
2238         bzero(&out->st_birthtim, sizeof(out->st_birthtim));
2239 #endif
2240         CP(*in, *out, st_dev);
2241         CP(*in, *out, st_ino);
2242         CP(*in, *out, st_mode);
2243         CP(*in, *out, st_nlink);
2244         CP(*in, *out, st_uid);
2245         CP(*in, *out, st_gid);
2246         CP(*in, *out, st_rdev);
2247         TS_CP(*in, *out, st_atim);
2248         TS_CP(*in, *out, st_mtim);
2249         TS_CP(*in, *out, st_ctim);
2250         CP(*in, *out, st_size);
2251         CP(*in, *out, st_blocks);
2252         CP(*in, *out, st_blksize);
2253         CP(*in, *out, st_flags);
2254         CP(*in, *out, st_gen);
2255         TS_CP(*in, *out, st_birthtim);
2256         out->st_padding0 = 0;
2257         out->st_padding1 = 0;
2258 #ifdef __STAT32_TIME_T_EXT
2259         out->st_atim_ext = 0;
2260         out->st_mtim_ext = 0;
2261         out->st_ctim_ext = 0;
2262         out->st_btim_ext = 0;
2263 #endif
2264         bzero(out->st_spare, sizeof(out->st_spare));
2265 }
2266
2267 #ifdef COMPAT_43
2268 static void
2269 copy_ostat(struct stat *in, struct ostat32 *out)
2270 {
2271
2272         bzero(out, sizeof(*out));
2273         CP(*in, *out, st_dev);
2274         CP(*in, *out, st_ino);
2275         CP(*in, *out, st_mode);
2276         CP(*in, *out, st_nlink);
2277         CP(*in, *out, st_uid);
2278         CP(*in, *out, st_gid);
2279         CP(*in, *out, st_rdev);
2280         out->st_size = MIN(in->st_size, INT32_MAX);
2281         TS_CP(*in, *out, st_atim);
2282         TS_CP(*in, *out, st_mtim);
2283         TS_CP(*in, *out, st_ctim);
2284         CP(*in, *out, st_blksize);
2285         CP(*in, *out, st_blocks);
2286         CP(*in, *out, st_flags);
2287         CP(*in, *out, st_gen);
2288 }
2289 #endif
2290
2291 #ifdef COMPAT_43
2292 int
2293 ofreebsd32_stat(struct thread *td, struct ofreebsd32_stat_args *uap)
2294 {
2295         struct stat sb;
2296         struct ostat32 sb32;
2297         int error;
2298
2299         error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE, &sb);
2300         if (error)
2301                 return (error);
2302         copy_ostat(&sb, &sb32);
2303         error = copyout(&sb32, uap->ub, sizeof (sb32));
2304         return (error);
2305 }
2306 #endif
2307
2308 int
2309 freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
2310 {
2311         struct stat ub;
2312         struct stat32 ub32;
2313         int error;
2314
2315         error = kern_fstat(td, uap->fd, &ub);
2316         if (error)
2317                 return (error);
2318         copy_stat(&ub, &ub32);
2319         error = copyout(&ub32, uap->sb, sizeof(ub32));
2320         return (error);
2321 }
2322
2323 #ifdef COMPAT_43
2324 int
2325 ofreebsd32_fstat(struct thread *td, struct ofreebsd32_fstat_args *uap)
2326 {
2327         struct stat ub;
2328         struct ostat32 ub32;
2329         int error;
2330
2331         error = kern_fstat(td, uap->fd, &ub);
2332         if (error)
2333                 return (error);
2334         copy_ostat(&ub, &ub32);
2335         error = copyout(&ub32, uap->sb, sizeof(ub32));
2336         return (error);
2337 }
2338 #endif
2339
2340 int
2341 freebsd32_fstatat(struct thread *td, struct freebsd32_fstatat_args *uap)
2342 {
2343         struct stat ub;
2344         struct stat32 ub32;
2345         int error;
2346
2347         error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE,
2348             &ub);
2349         if (error)
2350                 return (error);
2351         copy_stat(&ub, &ub32);
2352         error = copyout(&ub32, uap->buf, sizeof(ub32));
2353         return (error);
2354 }
2355
2356 #ifdef COMPAT_43
2357 int
2358 ofreebsd32_lstat(struct thread *td, struct ofreebsd32_lstat_args *uap)
2359 {
2360         struct stat sb;
2361         struct ostat32 sb32;
2362         int error;
2363
2364         error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2365             UIO_USERSPACE, &sb);
2366         if (error)
2367                 return (error);
2368         copy_ostat(&sb, &sb32);
2369         error = copyout(&sb32, uap->ub, sizeof (sb32));
2370         return (error);
2371 }
2372 #endif
2373
2374 int
2375 freebsd32_fhstat(struct thread *td, struct freebsd32_fhstat_args *uap)
2376 {
2377         struct stat sb;
2378         struct stat32 sb32;
2379         struct fhandle fh;
2380         int error;
2381
2382         error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2383         if (error != 0)
2384                 return (error);
2385         error = kern_fhstat(td, fh, &sb);
2386         if (error != 0)
2387                 return (error);
2388         copy_stat(&sb, &sb32);
2389         error = copyout(&sb32, uap->sb, sizeof (sb32));
2390         return (error);
2391 }
2392
2393 #if defined(COMPAT_FREEBSD11)
2394 extern int ino64_trunc_error;
2395
2396 static int
2397 freebsd11_cvtstat32(struct stat *in, struct freebsd11_stat32 *out)
2398 {
2399
2400 #ifndef __amd64__
2401         /*
2402          * 32-bit architectures other than i386 have 64-bit time_t.  This
2403          * results in struct timespec32 with 12 bytes for tv_sec and tv_nsec,
2404          * and 4 bytes of padding.  Zero the padding holes in freebsd11_stat32.
2405          */
2406         bzero(&out->st_atim, sizeof(out->st_atim));
2407         bzero(&out->st_mtim, sizeof(out->st_mtim));
2408         bzero(&out->st_ctim, sizeof(out->st_ctim));
2409         bzero(&out->st_birthtim, sizeof(out->st_birthtim));
2410 #endif
2411
2412         CP(*in, *out, st_ino);
2413         if (in->st_ino != out->st_ino) {
2414                 switch (ino64_trunc_error) {
2415                 default:
2416                 case 0:
2417                         break;
2418                 case 1:
2419                         return (EOVERFLOW);
2420                 case 2:
2421                         out->st_ino = UINT32_MAX;
2422                         break;
2423                 }
2424         }
2425         CP(*in, *out, st_nlink);
2426         if (in->st_nlink != out->st_nlink) {
2427                 switch (ino64_trunc_error) {
2428                 default:
2429                 case 0:
2430                         break;
2431                 case 1:
2432                         return (EOVERFLOW);
2433                 case 2:
2434                         out->st_nlink = UINT16_MAX;
2435                         break;
2436                 }
2437         }
2438         out->st_dev = in->st_dev;
2439         if (out->st_dev != in->st_dev) {
2440                 switch (ino64_trunc_error) {
2441                 default:
2442                         break;
2443                 case 1:
2444                         return (EOVERFLOW);
2445                 }
2446         }
2447         CP(*in, *out, st_mode);
2448         CP(*in, *out, st_uid);
2449         CP(*in, *out, st_gid);
2450         out->st_rdev = in->st_rdev;
2451         if (out->st_rdev != in->st_rdev) {
2452                 switch (ino64_trunc_error) {
2453                 default:
2454                         break;
2455                 case 1:
2456                         return (EOVERFLOW);
2457                 }
2458         }
2459         TS_CP(*in, *out, st_atim);
2460         TS_CP(*in, *out, st_mtim);
2461         TS_CP(*in, *out, st_ctim);
2462         CP(*in, *out, st_size);
2463         CP(*in, *out, st_blocks);
2464         CP(*in, *out, st_blksize);
2465         CP(*in, *out, st_flags);
2466         CP(*in, *out, st_gen);
2467         TS_CP(*in, *out, st_birthtim);
2468         out->st_lspare = 0;
2469         bzero((char *)&out->st_birthtim + sizeof(out->st_birthtim),
2470             sizeof(*out) - offsetof(struct freebsd11_stat32,
2471             st_birthtim) - sizeof(out->st_birthtim));
2472         return (0);
2473 }
2474
2475 int
2476 freebsd11_freebsd32_stat(struct thread *td,
2477     struct freebsd11_freebsd32_stat_args *uap)
2478 {
2479         struct stat sb;
2480         struct freebsd11_stat32 sb32;
2481         int error;
2482
2483         error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE, &sb);
2484         if (error != 0)
2485                 return (error);
2486         error = freebsd11_cvtstat32(&sb, &sb32);
2487         if (error == 0)
2488                 error = copyout(&sb32, uap->ub, sizeof (sb32));
2489         return (error);
2490 }
2491
2492 int
2493 freebsd11_freebsd32_fstat(struct thread *td,
2494     struct freebsd11_freebsd32_fstat_args *uap)
2495 {
2496         struct stat sb;
2497         struct freebsd11_stat32 sb32;
2498         int error;
2499
2500         error = kern_fstat(td, uap->fd, &sb);
2501         if (error != 0)
2502                 return (error);
2503         error = freebsd11_cvtstat32(&sb, &sb32);
2504         if (error == 0)
2505                 error = copyout(&sb32, uap->sb, sizeof (sb32));
2506         return (error);
2507 }
2508
2509 int
2510 freebsd11_freebsd32_fstatat(struct thread *td,
2511     struct freebsd11_freebsd32_fstatat_args *uap)
2512 {
2513         struct stat sb;
2514         struct freebsd11_stat32 sb32;
2515         int error;
2516
2517         error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE,
2518             &sb);
2519         if (error != 0)
2520                 return (error);
2521         error = freebsd11_cvtstat32(&sb, &sb32);
2522         if (error == 0)
2523                 error = copyout(&sb32, uap->buf, sizeof (sb32));
2524         return (error);
2525 }
2526
2527 int
2528 freebsd11_freebsd32_lstat(struct thread *td,
2529     struct freebsd11_freebsd32_lstat_args *uap)
2530 {
2531         struct stat sb;
2532         struct freebsd11_stat32 sb32;
2533         int error;
2534
2535         error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2536             UIO_USERSPACE, &sb);
2537         if (error != 0)
2538                 return (error);
2539         error = freebsd11_cvtstat32(&sb, &sb32);
2540         if (error == 0)
2541                 error = copyout(&sb32, uap->ub, sizeof (sb32));
2542         return (error);
2543 }
2544
2545 int
2546 freebsd11_freebsd32_fhstat(struct thread *td,
2547     struct freebsd11_freebsd32_fhstat_args *uap)
2548 {
2549         struct stat sb;
2550         struct freebsd11_stat32 sb32;
2551         struct fhandle fh;
2552         int error;
2553
2554         error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2555         if (error != 0)
2556                 return (error);
2557         error = kern_fhstat(td, fh, &sb);
2558         if (error != 0)
2559                 return (error);
2560         error = freebsd11_cvtstat32(&sb, &sb32);
2561         if (error == 0)
2562                 error = copyout(&sb32, uap->sb, sizeof (sb32));
2563         return (error);
2564 }
2565
2566 static int
2567 freebsd11_cvtnstat32(struct stat *sb, struct nstat32 *nsb32)
2568 {
2569         struct nstat nsb;
2570         int error;
2571
2572         error = freebsd11_cvtnstat(sb, &nsb);
2573         if (error != 0)
2574                 return (error);
2575
2576         bzero(nsb32, sizeof(*nsb32));
2577         CP(nsb, *nsb32, st_dev);
2578         CP(nsb, *nsb32, st_ino);
2579         CP(nsb, *nsb32, st_mode);
2580         CP(nsb, *nsb32, st_nlink);
2581         CP(nsb, *nsb32, st_uid);
2582         CP(nsb, *nsb32, st_gid);
2583         CP(nsb, *nsb32, st_rdev);
2584         CP(nsb, *nsb32, st_atim.tv_sec);
2585         CP(nsb, *nsb32, st_atim.tv_nsec);
2586         CP(nsb, *nsb32, st_mtim.tv_sec);
2587         CP(nsb, *nsb32, st_mtim.tv_nsec);
2588         CP(nsb, *nsb32, st_ctim.tv_sec);
2589         CP(nsb, *nsb32, st_ctim.tv_nsec);
2590         CP(nsb, *nsb32, st_size);
2591         CP(nsb, *nsb32, st_blocks);
2592         CP(nsb, *nsb32, st_blksize);
2593         CP(nsb, *nsb32, st_flags);
2594         CP(nsb, *nsb32, st_gen);
2595         CP(nsb, *nsb32, st_birthtim.tv_sec);
2596         CP(nsb, *nsb32, st_birthtim.tv_nsec);
2597         return (0);
2598 }
2599
2600 int
2601 freebsd11_freebsd32_nstat(struct thread *td,
2602     struct freebsd11_freebsd32_nstat_args *uap)
2603 {
2604         struct stat sb;
2605         struct nstat32 nsb;
2606         int error;
2607
2608         error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE, &sb);
2609         if (error != 0)
2610                 return (error);
2611         error = freebsd11_cvtnstat32(&sb, &nsb);
2612         if (error != 0)
2613                 error = copyout(&nsb, uap->ub, sizeof (nsb));
2614         return (error);
2615 }
2616
2617 int
2618 freebsd11_freebsd32_nlstat(struct thread *td,
2619     struct freebsd11_freebsd32_nlstat_args *uap)
2620 {
2621         struct stat sb;
2622         struct nstat32 nsb;
2623         int error;
2624
2625         error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2626             UIO_USERSPACE, &sb);
2627         if (error != 0)
2628                 return (error);
2629         error = freebsd11_cvtnstat32(&sb, &nsb);
2630         if (error == 0)
2631                 error = copyout(&nsb, uap->ub, sizeof (nsb));
2632         return (error);
2633 }
2634
2635 int
2636 freebsd11_freebsd32_nfstat(struct thread *td,
2637     struct freebsd11_freebsd32_nfstat_args *uap)
2638 {
2639         struct nstat32 nub;
2640         struct stat ub;
2641         int error;
2642
2643         error = kern_fstat(td, uap->fd, &ub);
2644         if (error != 0)
2645                 return (error);
2646         error = freebsd11_cvtnstat32(&ub, &nub);
2647         if (error == 0)
2648                 error = copyout(&nub, uap->sb, sizeof(nub));
2649         return (error);
2650 }
2651 #endif
2652
2653 int
2654 freebsd32___sysctl(struct thread *td, struct freebsd32___sysctl_args *uap)
2655 {
2656         int error, name[CTL_MAXNAME];
2657         size_t j, oldlen;
2658         uint32_t tmp;
2659
2660         if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
2661                 return (EINVAL);
2662         error = copyin(uap->name, name, uap->namelen * sizeof(int));
2663         if (error)
2664                 return (error);
2665         if (uap->oldlenp) {
2666                 error = fueword32(uap->oldlenp, &tmp);
2667                 oldlen = tmp;
2668         } else {
2669                 oldlen = 0;
2670         }
2671         if (error != 0)
2672                 return (EFAULT);
2673         error = userland_sysctl(td, name, uap->namelen,
2674                 uap->old, &oldlen, 1,
2675                 uap->new, uap->newlen, &j, SCTL_MASK32);
2676         if (error)
2677                 return (error);
2678         if (uap->oldlenp != NULL && suword32(uap->oldlenp, j) != 0)
2679                 error = EFAULT;
2680         return (error);
2681 }
2682
2683 int
2684 freebsd32___sysctlbyname(struct thread *td,
2685     struct freebsd32___sysctlbyname_args *uap)
2686 {
2687         size_t oldlen, rv;
2688         int error;
2689         uint32_t tmp;
2690
2691         if (uap->oldlenp != NULL) {
2692                 error = fueword32(uap->oldlenp, &tmp);
2693                 oldlen = tmp;
2694         } else {
2695                 error = oldlen = 0;
2696         }
2697         if (error != 0)
2698                 return (EFAULT);
2699         error = kern___sysctlbyname(td, uap->name, uap->namelen, uap->old,
2700             &oldlen, uap->new, uap->newlen, &rv, SCTL_MASK32, 1);
2701         if (error != 0)
2702                 return (error);
2703         if (uap->oldlenp != NULL && suword32(uap->oldlenp, rv) != 0)
2704                 error = EFAULT;
2705         return (error);
2706 }
2707
2708 int
2709 freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap)
2710 {
2711         uint32_t version;
2712         int error;
2713         struct jail j;
2714
2715         error = copyin(uap->jail, &version, sizeof(uint32_t));
2716         if (error)
2717                 return (error);
2718
2719         switch (version) {
2720         case 0:
2721         {
2722                 /* FreeBSD single IPv4 jails. */
2723                 struct jail32_v0 j32_v0;
2724
2725                 bzero(&j, sizeof(struct jail));
2726                 error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
2727                 if (error)
2728                         return (error);
2729                 CP(j32_v0, j, version);
2730                 PTRIN_CP(j32_v0, j, path);
2731                 PTRIN_CP(j32_v0, j, hostname);
2732                 j.ip4s = htonl(j32_v0.ip_number);       /* jail_v0 is host order */
2733                 break;
2734         }
2735
2736         case 1:
2737                 /*
2738                  * Version 1 was used by multi-IPv4 jail implementations
2739                  * that never made it into the official kernel.
2740                  */
2741                 return (EINVAL);
2742
2743         case 2: /* JAIL_API_VERSION */
2744         {
2745                 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
2746                 struct jail32 j32;
2747
2748                 error = copyin(uap->jail, &j32, sizeof(struct jail32));
2749                 if (error)
2750                         return (error);
2751                 CP(j32, j, version);
2752                 PTRIN_CP(j32, j, path);
2753                 PTRIN_CP(j32, j, hostname);
2754                 PTRIN_CP(j32, j, jailname);
2755                 CP(j32, j, ip4s);
2756                 CP(j32, j, ip6s);
2757                 PTRIN_CP(j32, j, ip4);
2758                 PTRIN_CP(j32, j, ip6);
2759                 break;
2760         }
2761
2762         default:
2763                 /* Sci-Fi jails are not supported, sorry. */
2764                 return (EINVAL);
2765         }
2766         return (kern_jail(td, &j));
2767 }
2768
2769 int
2770 freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap)
2771 {
2772         struct uio *auio;
2773         int error;
2774
2775         /* Check that we have an even number of iovecs. */
2776         if (uap->iovcnt & 1)
2777                 return (EINVAL);
2778
2779         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2780         if (error)
2781                 return (error);
2782         error = kern_jail_set(td, auio, uap->flags);
2783         free(auio, M_IOV);
2784         return (error);
2785 }
2786
2787 int
2788 freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap)
2789 {
2790         struct iovec32 iov32;
2791         struct uio *auio;
2792         int error, i;
2793
2794         /* Check that we have an even number of iovecs. */
2795         if (uap->iovcnt & 1)
2796                 return (EINVAL);
2797
2798         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2799         if (error)
2800                 return (error);
2801         error = kern_jail_get(td, auio, uap->flags);
2802         if (error == 0)
2803                 for (i = 0; i < uap->iovcnt; i++) {
2804                         PTROUT_CP(auio->uio_iov[i], iov32, iov_base);
2805                         CP(auio->uio_iov[i], iov32, iov_len);
2806                         error = copyout(&iov32, uap->iovp + i, sizeof(iov32));
2807                         if (error != 0)
2808                                 break;
2809                 }
2810         free(auio, M_IOV);
2811         return (error);
2812 }
2813
2814 int
2815 freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
2816 {
2817         struct sigaction32 s32;
2818         struct sigaction sa, osa, *sap;
2819         int error;
2820
2821         if (uap->act) {
2822                 error = copyin(uap->act, &s32, sizeof(s32));
2823                 if (error)
2824                         return (error);
2825                 sa.sa_handler = PTRIN(s32.sa_u);
2826                 CP(s32, sa, sa_flags);
2827                 CP(s32, sa, sa_mask);
2828                 sap = &sa;
2829         } else
2830                 sap = NULL;
2831         error = kern_sigaction(td, uap->sig, sap, &osa, 0);
2832         if (error == 0 && uap->oact != NULL) {
2833                 s32.sa_u = PTROUT(osa.sa_handler);
2834                 CP(osa, s32, sa_flags);
2835                 CP(osa, s32, sa_mask);
2836                 error = copyout(&s32, uap->oact, sizeof(s32));
2837         }
2838         return (error);
2839 }
2840
2841 #ifdef COMPAT_FREEBSD4
2842 int
2843 freebsd4_freebsd32_sigaction(struct thread *td,
2844                              struct freebsd4_freebsd32_sigaction_args *uap)
2845 {
2846         struct sigaction32 s32;
2847         struct sigaction sa, osa, *sap;
2848         int error;
2849
2850         if (uap->act) {
2851                 error = copyin(uap->act, &s32, sizeof(s32));
2852                 if (error)
2853                         return (error);
2854                 sa.sa_handler = PTRIN(s32.sa_u);
2855                 CP(s32, sa, sa_flags);
2856                 CP(s32, sa, sa_mask);
2857                 sap = &sa;
2858         } else
2859                 sap = NULL;
2860         error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
2861         if (error == 0 && uap->oact != NULL) {
2862                 s32.sa_u = PTROUT(osa.sa_handler);
2863                 CP(osa, s32, sa_flags);
2864                 CP(osa, s32, sa_mask);
2865                 error = copyout(&s32, uap->oact, sizeof(s32));
2866         }
2867         return (error);
2868 }
2869 #endif
2870
2871 #ifdef COMPAT_43
2872 struct osigaction32 {
2873         uint32_t        sa_u;
2874         osigset_t       sa_mask;
2875         int             sa_flags;
2876 };
2877
2878 #define ONSIG   32
2879
2880 int
2881 ofreebsd32_sigaction(struct thread *td,
2882                              struct ofreebsd32_sigaction_args *uap)
2883 {
2884         struct osigaction32 s32;
2885         struct sigaction sa, osa, *sap;
2886         int error;
2887
2888         if (uap->signum <= 0 || uap->signum >= ONSIG)
2889                 return (EINVAL);
2890
2891         if (uap->nsa) {
2892                 error = copyin(uap->nsa, &s32, sizeof(s32));
2893                 if (error)
2894                         return (error);
2895                 sa.sa_handler = PTRIN(s32.sa_u);
2896                 CP(s32, sa, sa_flags);
2897                 OSIG2SIG(s32.sa_mask, sa.sa_mask);
2898                 sap = &sa;
2899         } else
2900                 sap = NULL;
2901         error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2902         if (error == 0 && uap->osa != NULL) {
2903                 s32.sa_u = PTROUT(osa.sa_handler);
2904                 CP(osa, s32, sa_flags);
2905                 SIG2OSIG(osa.sa_mask, s32.sa_mask);
2906                 error = copyout(&s32, uap->osa, sizeof(s32));
2907         }
2908         return (error);
2909 }
2910
2911 struct sigvec32 {
2912         uint32_t        sv_handler;
2913         int             sv_mask;
2914         int             sv_flags;
2915 };
2916
2917 int
2918 ofreebsd32_sigvec(struct thread *td,
2919                           struct ofreebsd32_sigvec_args *uap)
2920 {
2921         struct sigvec32 vec;
2922         struct sigaction sa, osa, *sap;
2923         int error;
2924
2925         if (uap->signum <= 0 || uap->signum >= ONSIG)
2926                 return (EINVAL);
2927
2928         if (uap->nsv) {
2929                 error = copyin(uap->nsv, &vec, sizeof(vec));
2930                 if (error)
2931                         return (error);
2932                 sa.sa_handler = PTRIN(vec.sv_handler);
2933                 OSIG2SIG(vec.sv_mask, sa.sa_mask);
2934                 sa.sa_flags = vec.sv_flags;
2935                 sa.sa_flags ^= SA_RESTART;
2936                 sap = &sa;
2937         } else
2938                 sap = NULL;
2939         error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2940         if (error == 0 && uap->osv != NULL) {
2941                 vec.sv_handler = PTROUT(osa.sa_handler);
2942                 SIG2OSIG(osa.sa_mask, vec.sv_mask);
2943                 vec.sv_flags = osa.sa_flags;
2944                 vec.sv_flags &= ~SA_NOCLDWAIT;
2945                 vec.sv_flags ^= SA_RESTART;
2946                 error = copyout(&vec, uap->osv, sizeof(vec));
2947         }
2948         return (error);
2949 }
2950
2951 struct sigstack32 {
2952         uint32_t        ss_sp;
2953         int             ss_onstack;
2954 };
2955
2956 int
2957 ofreebsd32_sigstack(struct thread *td,
2958                             struct ofreebsd32_sigstack_args *uap)
2959 {
2960         struct sigstack32 s32;
2961         struct sigstack nss, oss;
2962         int error = 0, unss;
2963
2964         if (uap->nss != NULL) {
2965                 error = copyin(uap->nss, &s32, sizeof(s32));
2966                 if (error)
2967                         return (error);
2968                 nss.ss_sp = PTRIN(s32.ss_sp);
2969                 CP(s32, nss, ss_onstack);
2970                 unss = 1;
2971         } else {
2972                 unss = 0;
2973         }
2974         oss.ss_sp = td->td_sigstk.ss_sp;
2975         oss.ss_onstack = sigonstack(cpu_getstack(td));
2976         if (unss) {
2977                 td->td_sigstk.ss_sp = nss.ss_sp;
2978                 td->td_sigstk.ss_size = 0;
2979                 td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2980                 td->td_pflags |= TDP_ALTSTACK;
2981         }
2982         if (uap->oss != NULL) {
2983                 s32.ss_sp = PTROUT(oss.ss_sp);
2984                 CP(oss, s32, ss_onstack);
2985                 error = copyout(&s32, uap->oss, sizeof(s32));
2986         }
2987         return (error);
2988 }
2989 #endif
2990
2991 int
2992 freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2993 {
2994
2995         return (freebsd32_user_clock_nanosleep(td, CLOCK_REALTIME,
2996             TIMER_RELTIME, uap->rqtp, uap->rmtp));
2997 }
2998
2999 int
3000 freebsd32_clock_nanosleep(struct thread *td,
3001     struct freebsd32_clock_nanosleep_args *uap)
3002 {
3003         int error;
3004
3005         error = freebsd32_user_clock_nanosleep(td, uap->clock_id, uap->flags,
3006             uap->rqtp, uap->rmtp);
3007         return (kern_posix_error(td, error));
3008 }
3009
3010 static int
3011 freebsd32_user_clock_nanosleep(struct thread *td, clockid_t clock_id,
3012     int flags, const struct timespec32 *ua_rqtp, struct timespec32 *ua_rmtp)
3013 {
3014         struct timespec32 rmt32, rqt32;
3015         struct timespec rmt, rqt;
3016         int error, error2;
3017
3018         error = copyin(ua_rqtp, &rqt32, sizeof(rqt32));
3019         if (error)
3020                 return (error);
3021
3022         CP(rqt32, rqt, tv_sec);
3023         CP(rqt32, rqt, tv_nsec);
3024
3025         error = kern_clock_nanosleep(td, clock_id, flags, &rqt, &rmt);
3026         if (error == EINTR && ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0) {
3027                 CP(rmt, rmt32, tv_sec);
3028                 CP(rmt, rmt32, tv_nsec);
3029
3030                 error2 = copyout(&rmt32, ua_rmtp, sizeof(rmt32));
3031                 if (error2 != 0)
3032                         error = error2;
3033         }
3034         return (error);
3035 }
3036
3037 int
3038 freebsd32_clock_gettime(struct thread *td,
3039                         struct freebsd32_clock_gettime_args *uap)
3040 {
3041         struct timespec ats;
3042         struct timespec32 ats32;
3043         int error;
3044
3045         error = kern_clock_gettime(td, uap->clock_id, &ats);
3046         if (error == 0) {
3047                 CP(ats, ats32, tv_sec);
3048                 CP(ats, ats32, tv_nsec);
3049                 error = copyout(&ats32, uap->tp, sizeof(ats32));
3050         }
3051         return (error);
3052 }
3053
3054 int
3055 freebsd32_clock_settime(struct thread *td,
3056                         struct freebsd32_clock_settime_args *uap)
3057 {
3058         struct timespec ats;
3059         struct timespec32 ats32;
3060         int error;
3061
3062         error = copyin(uap->tp, &ats32, sizeof(ats32));
3063         if (error)
3064                 return (error);
3065         CP(ats32, ats, tv_sec);
3066         CP(ats32, ats, tv_nsec);
3067
3068         return (kern_clock_settime(td, uap->clock_id, &ats));
3069 }
3070
3071 int
3072 freebsd32_clock_getres(struct thread *td,
3073                        struct freebsd32_clock_getres_args *uap)
3074 {
3075         struct timespec ts;
3076         struct timespec32 ts32;
3077         int error;
3078
3079         if (uap->tp == NULL)
3080                 return (0);
3081         error = kern_clock_getres(td, uap->clock_id, &ts);
3082         if (error == 0) {
3083                 CP(ts, ts32, tv_sec);
3084                 CP(ts, ts32, tv_nsec);
3085                 error = copyout(&ts32, uap->tp, sizeof(ts32));
3086         }
3087         return (error);
3088 }
3089
3090 int freebsd32_ktimer_create(struct thread *td,
3091     struct freebsd32_ktimer_create_args *uap)
3092 {
3093         struct sigevent32 ev32;
3094         struct sigevent ev, *evp;
3095         int error, id;
3096
3097         if (uap->evp == NULL) {
3098                 evp = NULL;
3099         } else {
3100                 evp = &ev;
3101                 error = copyin(uap->evp, &ev32, sizeof(ev32));
3102                 if (error != 0)
3103                         return (error);
3104                 error = convert_sigevent32(&ev32, &ev);
3105                 if (error != 0)
3106                         return (error);
3107         }
3108         error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
3109         if (error == 0) {
3110                 error = copyout(&id, uap->timerid, sizeof(int));
3111                 if (error != 0)
3112                         kern_ktimer_delete(td, id);
3113         }
3114         return (error);
3115 }
3116
3117 int
3118 freebsd32_ktimer_settime(struct thread *td,
3119     struct freebsd32_ktimer_settime_args *uap)
3120 {
3121         struct itimerspec32 val32, oval32;
3122         struct itimerspec val, oval, *ovalp;
3123         int error;
3124
3125         error = copyin(uap->value, &val32, sizeof(val32));
3126         if (error != 0)
3127                 return (error);
3128         ITS_CP(val32, val);
3129         ovalp = uap->ovalue != NULL ? &oval : NULL;
3130         error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
3131         if (error == 0 && uap->ovalue != NULL) {
3132                 ITS_CP(oval, oval32);
3133                 error = copyout(&oval32, uap->ovalue, sizeof(oval32));
3134         }
3135         return (error);
3136 }
3137
3138 int
3139 freebsd32_ktimer_gettime(struct thread *td,
3140     struct freebsd32_ktimer_gettime_args *uap)
3141 {
3142         struct itimerspec32 val32;
3143         struct itimerspec val;
3144         int error;
3145
3146         error = kern_ktimer_gettime(td, uap->timerid, &val);
3147         if (error == 0) {
3148                 ITS_CP(val, val32);
3149                 error = copyout(&val32, uap->value, sizeof(val32));
3150         }
3151         return (error);
3152 }
3153
3154 int
3155 freebsd32_timerfd_gettime(struct thread *td,
3156     struct freebsd32_timerfd_gettime_args *uap)
3157 {
3158         struct itimerspec curr_value;
3159         struct itimerspec32 curr_value32;
3160         int error;
3161
3162         error = kern_timerfd_gettime(td, uap->fd, &curr_value);
3163         if (error == 0) {
3164                 CP(curr_value, curr_value32, it_value.tv_sec);
3165                 CP(curr_value, curr_value32, it_value.tv_nsec);
3166                 CP(curr_value, curr_value32, it_interval.tv_sec);
3167                 CP(curr_value, curr_value32, it_interval.tv_nsec);
3168                 error = copyout(&curr_value32, uap->curr_value,
3169                     sizeof(curr_value32));
3170         }
3171
3172         return (error);
3173 }
3174
3175 int
3176 freebsd32_timerfd_settime(struct thread *td,
3177     struct freebsd32_timerfd_settime_args *uap)
3178 {
3179         struct itimerspec new_value, old_value;
3180         struct itimerspec32 new_value32, old_value32;
3181         int error;
3182
3183         error = copyin(uap->new_value, &new_value32, sizeof(new_value32));
3184         if (error != 0)
3185                 return (error);
3186         CP(new_value32, new_value, it_value.tv_sec);
3187         CP(new_value32, new_value, it_value.tv_nsec);
3188         CP(new_value32, new_value, it_interval.tv_sec);
3189         CP(new_value32, new_value, it_interval.tv_nsec);
3190         if (uap->old_value == NULL) {
3191                 error = kern_timerfd_settime(td, uap->fd, uap->flags,
3192                     &new_value, NULL);
3193         } else {
3194                 error = kern_timerfd_settime(td, uap->fd, uap->flags,
3195                     &new_value, &old_value);
3196                 if (error == 0) {
3197                         CP(old_value, old_value32, it_value.tv_sec);
3198                         CP(old_value, old_value32, it_value.tv_nsec);
3199                         CP(old_value, old_value32, it_interval.tv_sec);
3200                         CP(old_value, old_value32, it_interval.tv_nsec);
3201                         error = copyout(&old_value32, uap->old_value,
3202                             sizeof(old_value32));
3203                 }
3204         }
3205         return (error);
3206 }
3207
3208 int
3209 freebsd32_clock_getcpuclockid2(struct thread *td,
3210     struct freebsd32_clock_getcpuclockid2_args *uap)
3211 {
3212         clockid_t clk_id;
3213         int error;
3214
3215         error = kern_clock_getcpuclockid2(td, PAIR32TO64(id_t, uap->id),
3216             uap->which, &clk_id);
3217         if (error == 0)
3218                 error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
3219         return (error);
3220 }
3221
3222 int
3223 freebsd32_thr_new(struct thread *td,
3224                   struct freebsd32_thr_new_args *uap)
3225 {
3226         struct thr_param32 param32;
3227         struct thr_param param;
3228         int error;
3229
3230         if (uap->param_size < 0 ||
3231             uap->param_size > sizeof(struct thr_param32))
3232                 return (EINVAL);
3233         bzero(&param, sizeof(struct thr_param));
3234         bzero(&param32, sizeof(struct thr_param32));
3235         error = copyin(uap->param, &param32, uap->param_size);
3236         if (error != 0)
3237                 return (error);
3238         param.start_func = PTRIN(param32.start_func);
3239         param.arg = PTRIN(param32.arg);
3240         param.stack_base = PTRIN(param32.stack_base);
3241         param.stack_size = param32.stack_size;
3242         param.tls_base = PTRIN(param32.tls_base);
3243         param.tls_size = param32.tls_size;
3244         param.child_tid = PTRIN(param32.child_tid);
3245         param.parent_tid = PTRIN(param32.parent_tid);
3246         param.flags = param32.flags;
3247         param.rtp = PTRIN(param32.rtp);
3248         param.spare[0] = PTRIN(param32.spare[0]);
3249         param.spare[1] = PTRIN(param32.spare[1]);
3250         param.spare[2] = PTRIN(param32.spare[2]);
3251
3252         return (kern_thr_new(td, &param));
3253 }
3254
3255 int
3256 freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
3257 {
3258         struct timespec32 ts32;
3259         struct timespec ts, *tsp;
3260         int error;
3261
3262         error = 0;
3263         tsp = NULL;
3264         if (uap->timeout != NULL) {
3265                 error = copyin((const void *)uap->timeout, (void *)&ts32,
3266                     sizeof(struct timespec32));
3267                 if (error != 0)
3268                         return (error);
3269                 ts.tv_sec = ts32.tv_sec;
3270                 ts.tv_nsec = ts32.tv_nsec;
3271                 tsp = &ts;
3272         }
3273         return (kern_thr_suspend(td, tsp));
3274 }
3275
3276 void
3277 siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst)
3278 {
3279         bzero(dst, sizeof(*dst));
3280         dst->si_signo = src->si_signo;
3281         dst->si_errno = src->si_errno;
3282         dst->si_code = src->si_code;
3283         dst->si_pid = src->si_pid;
3284         dst->si_uid = src->si_uid;
3285         dst->si_status = src->si_status;
3286         dst->si_addr = (uintptr_t)src->si_addr;
3287         dst->si_value.sival_int = src->si_value.sival_int;
3288         dst->si_timerid = src->si_timerid;
3289         dst->si_overrun = src->si_overrun;
3290 }
3291
3292 #ifndef _FREEBSD32_SYSPROTO_H_
3293 struct freebsd32_sigqueue_args {
3294         pid_t pid;
3295         int signum;
3296         /* union sigval32 */ int value;
3297 };
3298 #endif
3299 int
3300 freebsd32_sigqueue(struct thread *td, struct freebsd32_sigqueue_args *uap)
3301 {
3302         union sigval sv;
3303
3304         /*
3305          * On 32-bit ABIs, sival_int and sival_ptr are the same.
3306          * On 64-bit little-endian ABIs, the low bits are the same.
3307          * In 64-bit big-endian ABIs, sival_int overlaps with
3308          * sival_ptr's HIGH bits.  We choose to support sival_int
3309          * rather than sival_ptr in this case as it seems to be
3310          * more common.
3311          */
3312         bzero(&sv, sizeof(sv));
3313         sv.sival_int = (uint32_t)(uint64_t)uap->value;
3314
3315         return (kern_sigqueue(td, uap->pid, uap->signum, &sv));
3316 }
3317
3318 int
3319 freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
3320 {
3321         struct timespec32 ts32;
3322         struct timespec ts;
3323         struct timespec *timeout;
3324         sigset_t set;
3325         ksiginfo_t ksi;
3326         struct siginfo32 si32;
3327         int error;
3328
3329         if (uap->timeout) {
3330                 error = copyin(uap->timeout, &ts32, sizeof(ts32));
3331                 if (error)
3332                         return (error);
3333                 ts.tv_sec = ts32.tv_sec;
3334                 ts.tv_nsec = ts32.tv_nsec;
3335                 timeout = &ts;
3336         } else
3337                 timeout = NULL;
3338
3339         error = copyin(uap->set, &set, sizeof(set));
3340         if (error)
3341                 return (error);
3342
3343         error = kern_sigtimedwait(td, set, &ksi, timeout);
3344         if (error)
3345                 return (error);
3346
3347         if (uap->info) {
3348                 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
3349                 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
3350         }
3351
3352         if (error == 0)
3353                 td->td_retval[0] = ksi.ksi_signo;
3354         return (error);
3355 }
3356
3357 /*
3358  * MPSAFE
3359  */
3360 int
3361 freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
3362 {
3363         ksiginfo_t ksi;
3364         struct siginfo32 si32;
3365         sigset_t set;
3366         int error;
3367
3368         error = copyin(uap->set, &set, sizeof(set));
3369         if (error)
3370                 return (error);
3371
3372         error = kern_sigtimedwait(td, set, &ksi, NULL);
3373         if (error)
3374                 return (error);
3375
3376         if (uap->info) {
3377                 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
3378                 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
3379         }       
3380         if (error == 0)
3381                 td->td_retval[0] = ksi.ksi_signo;
3382         return (error);
3383 }
3384
3385 int
3386 freebsd32_cpuset_setid(struct thread *td,
3387     struct freebsd32_cpuset_setid_args *uap)
3388 {
3389
3390         return (kern_cpuset_setid(td, uap->which,
3391             PAIR32TO64(id_t, uap->id), uap->setid));
3392 }
3393
3394 int
3395 freebsd32_cpuset_getid(struct thread *td,
3396     struct freebsd32_cpuset_getid_args *uap)
3397 {
3398
3399         return (kern_cpuset_getid(td, uap->level, uap->which,
3400             PAIR32TO64(id_t, uap->id), uap->setid));
3401 }
3402
3403 static int
3404 copyin32_set(const void *u, void *k, size_t size)
3405 {
3406 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
3407         int rv;
3408         struct bitset *kb = k;
3409         int *p;
3410
3411         rv = copyin(u, k, size);
3412         if (rv != 0)
3413                 return (rv);
3414
3415         p = (int *)kb->__bits;
3416         /* Loop through swapping words.
3417          * `size' is in bytes, we need bits. */
3418         for (int i = 0; i < __bitset_words(size * 8); i++) {
3419                 int tmp = p[0];
3420                 p[0] = p[1];
3421                 p[1] = tmp;
3422                 p += 2;
3423         }
3424         return (0);
3425 #else
3426         return (copyin(u, k, size));
3427 #endif
3428 }
3429
3430 static int
3431 copyout32_set(const void *k, void *u, size_t size)
3432 {
3433 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
3434         const struct bitset *kb = k;
3435         struct bitset *ub = u;
3436         const int *kp = (const int *)kb->__bits;
3437         int *up = (int *)ub->__bits;
3438         int rv;
3439
3440         for (int i = 0; i < __bitset_words(CPU_SETSIZE); i++) {
3441                 /* `size' is in bytes, we need bits. */
3442                 for (int i = 0; i < __bitset_words(size * 8); i++) {
3443                         rv = suword32(up, kp[1]);
3444                         if (rv == 0)
3445                                 rv = suword32(up + 1, kp[0]);
3446                         if (rv != 0)
3447                                 return (EFAULT);
3448                 }
3449         }
3450         return (0);
3451 #else
3452         return (copyout(k, u, size));
3453 #endif
3454 }
3455
3456 static const struct cpuset_copy_cb cpuset_copy32_cb = {
3457         .cpuset_copyin = copyin32_set,
3458         .cpuset_copyout = copyout32_set
3459 };
3460
3461 int
3462 freebsd32_cpuset_getaffinity(struct thread *td,
3463     struct freebsd32_cpuset_getaffinity_args *uap)
3464 {
3465
3466         return (user_cpuset_getaffinity(td, uap->level, uap->which,
3467             PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask,
3468             &cpuset_copy32_cb));
3469 }
3470
3471 int
3472 freebsd32_cpuset_setaffinity(struct thread *td,
3473     struct freebsd32_cpuset_setaffinity_args *uap)
3474 {
3475
3476         return (user_cpuset_setaffinity(td, uap->level, uap->which,
3477             PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask,
3478             &cpuset_copy32_cb));
3479 }
3480
3481 int
3482 freebsd32_cpuset_getdomain(struct thread *td,
3483     struct freebsd32_cpuset_getdomain_args *uap)
3484 {
3485
3486         return (kern_cpuset_getdomain(td, uap->level, uap->which,
3487             PAIR32TO64(id_t,uap->id), uap->domainsetsize, uap->mask, uap->policy,
3488             &cpuset_copy32_cb));
3489 }
3490
3491 int
3492 freebsd32_cpuset_setdomain(struct thread *td,
3493     struct freebsd32_cpuset_setdomain_args *uap)
3494 {
3495
3496         return (kern_cpuset_setdomain(td, uap->level, uap->which,
3497             PAIR32TO64(id_t,uap->id), uap->domainsetsize, uap->mask, uap->policy,
3498             &cpuset_copy32_cb));
3499 }
3500
3501 int
3502 freebsd32_nmount(struct thread *td,
3503     struct freebsd32_nmount_args /* {
3504         struct iovec *iovp;
3505         unsigned int iovcnt;
3506         int flags;
3507     } */ *uap)
3508 {
3509         struct uio *auio;
3510         uint64_t flags;
3511         int error;
3512
3513         /*
3514          * Mount flags are now 64-bits. On 32-bit archtectures only
3515          * 32-bits are passed in, but from here on everything handles
3516          * 64-bit flags correctly.
3517          */
3518         flags = uap->flags;
3519
3520         AUDIT_ARG_FFLAGS(flags);
3521
3522         /*
3523          * Filter out MNT_ROOTFS.  We do not want clients of nmount() in
3524          * userspace to set this flag, but we must filter it out if we want
3525          * MNT_UPDATE on the root file system to work.
3526          * MNT_ROOTFS should only be set by the kernel when mounting its
3527          * root file system.
3528          */
3529         flags &= ~MNT_ROOTFS;
3530
3531         /*
3532          * check that we have an even number of iovec's
3533          * and that we have at least two options.
3534          */
3535         if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
3536                 return (EINVAL);
3537
3538         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
3539         if (error)
3540                 return (error);
3541         error = vfs_donmount(td, flags, auio);
3542
3543         free(auio, M_IOV);
3544         return error;
3545 }
3546
3547 #if 0
3548 int
3549 freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
3550 {
3551         struct yyy32 *p32, s32;
3552         struct yyy *p = NULL, s;
3553         struct xxx_arg ap;
3554         int error;
3555
3556         if (uap->zzz) {
3557                 error = copyin(uap->zzz, &s32, sizeof(s32));
3558                 if (error)
3559                         return (error);
3560                 /* translate in */
3561                 p = &s;
3562         }
3563         error = kern_xxx(td, p);
3564         if (error)
3565                 return (error);
3566         if (uap->zzz) {
3567                 /* translate out */
3568                 error = copyout(&s32, p32, sizeof(s32));
3569         }
3570         return (error);
3571 }
3572 #endif
3573
3574 int
3575 syscall32_module_handler(struct module *mod, int what, void *arg)
3576 {
3577
3578         return (kern_syscall_module_handler(freebsd32_sysent, mod, what, arg));
3579 }
3580
3581 int
3582 syscall32_helper_register(struct syscall_helper_data *sd, int flags)
3583 {
3584
3585         return (kern_syscall_helper_register(freebsd32_sysent, sd, flags));
3586 }
3587
3588 int
3589 syscall32_helper_unregister(struct syscall_helper_data *sd)
3590 {
3591
3592         return (kern_syscall_helper_unregister(freebsd32_sysent, sd));
3593 }
3594
3595 int
3596 freebsd32_copyout_strings(struct image_params *imgp, uintptr_t *stack_base)
3597 {
3598         struct sysentvec *sysent;
3599         int argc, envc, i;
3600         uint32_t *vectp;
3601         char *stringp;
3602         uintptr_t destp, ustringp;
3603         struct freebsd32_ps_strings *arginfo;
3604         char canary[sizeof(long) * 8];
3605         int32_t pagesizes32[MAXPAGESIZES];
3606         size_t execpath_len;
3607         int error, szsigcode;
3608
3609         sysent = imgp->sysent;
3610
3611         arginfo = (struct freebsd32_ps_strings *)PROC_PS_STRINGS(imgp->proc);
3612         imgp->ps_strings = arginfo;
3613         destp = (uintptr_t)arginfo;
3614
3615         /*
3616          * Install sigcode.
3617          */
3618         if (!PROC_HAS_SHP(imgp->proc)) {
3619                 szsigcode = *sysent->sv_szsigcode;
3620                 destp -= szsigcode;
3621                 destp = rounddown2(destp, sizeof(uint32_t));
3622                 error = copyout(sysent->sv_sigcode, (void *)destp,
3623                     szsigcode);
3624                 if (error != 0)
3625                         return (error);
3626         }
3627
3628         /*
3629          * Copy the image path for the rtld.
3630          */
3631         if (imgp->execpath != NULL && imgp->auxargs != NULL) {
3632                 execpath_len = strlen(imgp->execpath) + 1;
3633                 destp -= execpath_len;
3634                 imgp->execpathp = (void *)destp;
3635                 error = copyout(imgp->execpath, imgp->execpathp, execpath_len);
3636                 if (error != 0)
3637                         return (error);
3638         }
3639
3640         /*
3641          * Prepare the canary for SSP.
3642          */
3643         arc4rand(canary, sizeof(canary), 0);
3644         destp -= sizeof(canary);
3645         imgp->canary = (void *)destp;
3646         error = copyout(canary, imgp->canary, sizeof(canary));
3647         if (error != 0)
3648                 return (error);
3649         imgp->canarylen = sizeof(canary);
3650
3651         /*
3652          * Prepare the pagesizes array.
3653          */
3654         for (i = 0; i < MAXPAGESIZES; i++)
3655                 pagesizes32[i] = (uint32_t)pagesizes[i];
3656         destp -= sizeof(pagesizes32);
3657         destp = rounddown2(destp, sizeof(uint32_t));
3658         imgp->pagesizes = (void *)destp;
3659         error = copyout(pagesizes32, imgp->pagesizes, sizeof(pagesizes32));
3660         if (error != 0)
3661                 return (error);
3662         imgp->pagesizeslen = sizeof(pagesizes32);
3663
3664         /*
3665          * Allocate room for the argument and environment strings.
3666          */
3667         destp -= ARG_MAX - imgp->args->stringspace;
3668         destp = rounddown2(destp, sizeof(uint32_t));
3669         ustringp = destp;
3670
3671         if (imgp->auxargs) {
3672                 /*
3673                  * Allocate room on the stack for the ELF auxargs
3674                  * array.  It has up to AT_COUNT entries.
3675                  */
3676                 destp -= AT_COUNT * sizeof(Elf32_Auxinfo);
3677                 destp = rounddown2(destp, sizeof(uint32_t));
3678         }
3679
3680         vectp = (uint32_t *)destp;
3681
3682         /*
3683          * Allocate room for the argv[] and env vectors including the
3684          * terminating NULL pointers.
3685          */
3686         vectp -= imgp->args->argc + 1 + imgp->args->envc + 1;
3687
3688         /*
3689          * vectp also becomes our initial stack base
3690          */
3691         *stack_base = (uintptr_t)vectp;
3692
3693         stringp = imgp->args->begin_argv;
3694         argc = imgp->args->argc;
3695         envc = imgp->args->envc;
3696         /*
3697          * Copy out strings - arguments and environment.
3698          */
3699         error = copyout(stringp, (void *)ustringp,
3700             ARG_MAX - imgp->args->stringspace);
3701         if (error != 0)
3702                 return (error);
3703
3704         /*
3705          * Fill in "ps_strings" struct for ps, w, etc.
3706          */
3707         imgp->argv = vectp;
3708         if (suword32(&arginfo->ps_argvstr, (uint32_t)(intptr_t)vectp) != 0 ||
3709             suword32(&arginfo->ps_nargvstr, argc) != 0)
3710                 return (EFAULT);
3711
3712         /*
3713          * Fill in argument portion of vector table.
3714          */
3715         for (; argc > 0; --argc) {
3716                 if (suword32(vectp++, ustringp) != 0)
3717                         return (EFAULT);
3718                 while (*stringp++ != 0)
3719                         ustringp++;
3720                 ustringp++;
3721         }
3722
3723         /* a null vector table pointer separates the argp's from the envp's */
3724         if (suword32(vectp++, 0) != 0)
3725                 return (EFAULT);
3726
3727         imgp->envv = vectp;
3728         if (suword32(&arginfo->ps_envstr, (uint32_t)(intptr_t)vectp) != 0 ||
3729             suword32(&arginfo->ps_nenvstr, envc) != 0)
3730                 return (EFAULT);
3731
3732         /*
3733          * Fill in environment portion of vector table.
3734          */
3735         for (; envc > 0; --envc) {
3736                 if (suword32(vectp++, ustringp) != 0)
3737                         return (EFAULT);
3738                 while (*stringp++ != 0)
3739                         ustringp++;
3740                 ustringp++;
3741         }
3742
3743         /* end of vector table is a null pointer */
3744         if (suword32(vectp, 0) != 0)
3745                 return (EFAULT);
3746
3747         if (imgp->auxargs) {
3748                 vectp++;
3749                 error = imgp->sysent->sv_copyout_auxargs(imgp,
3750                     (uintptr_t)vectp);
3751                 if (error != 0)
3752                         return (error);
3753         }
3754
3755         return (0);
3756 }
3757
3758 int
3759 freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
3760 {
3761         struct kld_file_stat *stat;
3762         struct kld_file_stat32 *stat32;
3763         int error, version;
3764
3765         if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
3766             != 0)
3767                 return (error);
3768         if (version != sizeof(struct kld_file_stat_1_32) &&
3769             version != sizeof(struct kld_file_stat32))
3770                 return (EINVAL);
3771
3772         stat = malloc(sizeof(*stat), M_TEMP, M_WAITOK | M_ZERO);
3773         stat32 = malloc(sizeof(*stat32), M_TEMP, M_WAITOK | M_ZERO);
3774         error = kern_kldstat(td, uap->fileid, stat);
3775         if (error == 0) {
3776                 bcopy(&stat->name[0], &stat32->name[0], sizeof(stat->name));
3777                 CP(*stat, *stat32, refs);
3778                 CP(*stat, *stat32, id);
3779                 PTROUT_CP(*stat, *stat32, address);
3780                 CP(*stat, *stat32, size);
3781                 bcopy(&stat->pathname[0], &stat32->pathname[0],
3782                     sizeof(stat->pathname));
3783                 stat32->version  = version;
3784                 error = copyout(stat32, uap->stat, version);
3785         }
3786         free(stat, M_TEMP);
3787         free(stat32, M_TEMP);
3788         return (error);
3789 }
3790
3791 int
3792 freebsd32_posix_fallocate(struct thread *td,
3793     struct freebsd32_posix_fallocate_args *uap)
3794 {
3795         int error;
3796
3797         error = kern_posix_fallocate(td, uap->fd,
3798             PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len));
3799         return (kern_posix_error(td, error));
3800 }
3801
3802 int
3803 freebsd32_posix_fadvise(struct thread *td,
3804     struct freebsd32_posix_fadvise_args *uap)
3805 {
3806         int error;
3807
3808         error = kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset),
3809             PAIR32TO64(off_t, uap->len), uap->advice);
3810         return (kern_posix_error(td, error));
3811 }
3812
3813 int
3814 convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig)
3815 {
3816
3817         CP(*sig32, *sig, sigev_notify);
3818         switch (sig->sigev_notify) {
3819         case SIGEV_NONE:
3820                 break;
3821         case SIGEV_THREAD_ID:
3822                 CP(*sig32, *sig, sigev_notify_thread_id);
3823                 /* FALLTHROUGH */
3824         case SIGEV_SIGNAL:
3825                 CP(*sig32, *sig, sigev_signo);
3826                 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3827                 break;
3828         case SIGEV_KEVENT:
3829                 CP(*sig32, *sig, sigev_notify_kqueue);
3830                 CP(*sig32, *sig, sigev_notify_kevent_flags);
3831                 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3832                 break;
3833         default:
3834                 return (EINVAL);
3835         }
3836         return (0);
3837 }
3838
3839 int
3840 freebsd32_procctl(struct thread *td, struct freebsd32_procctl_args *uap)
3841 {
3842         void *data;
3843         union {
3844                 struct procctl_reaper_status rs;
3845                 struct procctl_reaper_pids rp;
3846                 struct procctl_reaper_kill rk;
3847         } x;
3848         union {
3849                 struct procctl_reaper_pids32 rp;
3850         } x32;
3851         int error, error1, flags, signum;
3852
3853         if (uap->com >= PROC_PROCCTL_MD_MIN)
3854                 return (cpu_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id),
3855                     uap->com, PTRIN(uap->data)));
3856
3857         switch (uap->com) {
3858         case PROC_ASLR_CTL:
3859         case PROC_PROTMAX_CTL:
3860         case PROC_SPROTECT:
3861         case PROC_STACKGAP_CTL:
3862         case PROC_TRACE_CTL:
3863         case PROC_TRAPCAP_CTL:
3864         case PROC_NO_NEW_PRIVS_CTL:
3865         case PROC_WXMAP_CTL:
3866                 error = copyin(PTRIN(uap->data), &flags, sizeof(flags));
3867                 if (error != 0)
3868                         return (error);
3869                 data = &flags;
3870                 break;
3871         case PROC_REAP_ACQUIRE:
3872         case PROC_REAP_RELEASE:
3873                 if (uap->data != NULL)
3874                         return (EINVAL);
3875                 data = NULL;
3876                 break;
3877         case PROC_REAP_STATUS:
3878                 data = &x.rs;
3879                 break;
3880         case PROC_REAP_GETPIDS:
3881                 error = copyin(uap->data, &x32.rp, sizeof(x32.rp));
3882                 if (error != 0)
3883                         return (error);
3884                 CP(x32.rp, x.rp, rp_count);
3885                 PTRIN_CP(x32.rp, x.rp, rp_pids);
3886                 data = &x.rp;
3887                 break;
3888         case PROC_REAP_KILL:
3889                 error = copyin(uap->data, &x.rk, sizeof(x.rk));
3890                 if (error != 0)
3891                         return (error);
3892                 data = &x.rk;
3893                 break;
3894         case PROC_ASLR_STATUS:
3895         case PROC_PROTMAX_STATUS:
3896         case PROC_STACKGAP_STATUS:
3897         case PROC_TRACE_STATUS:
3898         case PROC_TRAPCAP_STATUS:
3899         case PROC_NO_NEW_PRIVS_STATUS:
3900         case PROC_WXMAP_STATUS:
3901                 data = &flags;
3902                 break;
3903         case PROC_PDEATHSIG_CTL:
3904                 error = copyin(uap->data, &signum, sizeof(signum));
3905                 if (error != 0)
3906                         return (error);
3907                 data = &signum;
3908                 break;
3909         case PROC_PDEATHSIG_STATUS:
3910                 data = &signum;
3911                 break;
3912         default:
3913                 return (EINVAL);
3914         }
3915         error = kern_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id),
3916             uap->com, data);
3917         switch (uap->com) {
3918         case PROC_REAP_STATUS:
3919                 if (error == 0)
3920                         error = copyout(&x.rs, uap->data, sizeof(x.rs));
3921                 break;
3922         case PROC_REAP_KILL:
3923                 error1 = copyout(&x.rk, uap->data, sizeof(x.rk));
3924                 if (error == 0)
3925                         error = error1;
3926                 break;
3927         case PROC_ASLR_STATUS:
3928         case PROC_PROTMAX_STATUS:
3929         case PROC_STACKGAP_STATUS:
3930         case PROC_TRACE_STATUS:
3931         case PROC_TRAPCAP_STATUS:
3932         case PROC_NO_NEW_PRIVS_STATUS:
3933         case PROC_WXMAP_STATUS:
3934                 if (error == 0)
3935                         error = copyout(&flags, uap->data, sizeof(flags));
3936                 break;
3937         case PROC_PDEATHSIG_STATUS:
3938                 if (error == 0)
3939                         error = copyout(&signum, uap->data, sizeof(signum));
3940                 break;
3941         }
3942         return (error);
3943 }
3944
3945 int
3946 freebsd32_fcntl(struct thread *td, struct freebsd32_fcntl_args *uap)
3947 {
3948         long tmp;
3949
3950         switch (uap->cmd) {
3951         /*
3952          * Do unsigned conversion for arg when operation
3953          * interprets it as flags or pointer.
3954          */
3955         case F_SETLK_REMOTE:
3956         case F_SETLKW:
3957         case F_SETLK:
3958         case F_GETLK:
3959         case F_SETFD:
3960         case F_SETFL:
3961         case F_OGETLK:
3962         case F_OSETLK:
3963         case F_OSETLKW:
3964         case F_KINFO:
3965                 tmp = (unsigned int)(uap->arg);
3966                 break;
3967         default:
3968                 tmp = uap->arg;
3969                 break;
3970         }
3971         return (kern_fcntl_freebsd(td, uap->fd, uap->cmd, tmp));
3972 }
3973
3974 int
3975 freebsd32_ppoll(struct thread *td, struct freebsd32_ppoll_args *uap)
3976 {
3977         struct timespec32 ts32;
3978         struct timespec ts, *tsp;
3979         sigset_t set, *ssp;
3980         int error;
3981
3982         if (uap->ts != NULL) {
3983                 error = copyin(uap->ts, &ts32, sizeof(ts32));
3984                 if (error != 0)
3985                         return (error);
3986                 CP(ts32, ts, tv_sec);
3987                 CP(ts32, ts, tv_nsec);
3988                 tsp = &ts;
3989         } else
3990                 tsp = NULL;
3991         if (uap->set != NULL) {
3992                 error = copyin(uap->set, &set, sizeof(set));
3993                 if (error != 0)
3994                         return (error);
3995                 ssp = &set;
3996         } else
3997                 ssp = NULL;
3998
3999         return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp));
4000 }
4001
4002 int
4003 freebsd32_sched_rr_get_interval(struct thread *td,
4004     struct freebsd32_sched_rr_get_interval_args *uap)
4005 {
4006         struct timespec ts;
4007         struct timespec32 ts32;
4008         int error;
4009
4010         error = kern_sched_rr_get_interval(td, uap->pid, &ts);
4011         if (error == 0) {
4012                 CP(ts, ts32, tv_sec);
4013                 CP(ts, ts32, tv_nsec);
4014                 error = copyout(&ts32, uap->interval, sizeof(ts32));
4015         }
4016         return (error);
4017 }
4018
4019 static void
4020 timex_to_32(struct timex32 *dst, struct timex *src)
4021 {
4022         CP(*src, *dst, modes);
4023         CP(*src, *dst, offset);
4024         CP(*src, *dst, freq);
4025         CP(*src, *dst, maxerror);
4026         CP(*src, *dst, esterror);
4027         CP(*src, *dst, status);
4028         CP(*src, *dst, constant);
4029         CP(*src, *dst, precision);
4030         CP(*src, *dst, tolerance);
4031         CP(*src, *dst, ppsfreq);
4032         CP(*src, *dst, jitter);
4033         CP(*src, *dst, shift);
4034         CP(*src, *dst, stabil);
4035         CP(*src, *dst, jitcnt);
4036         CP(*src, *dst, calcnt);
4037         CP(*src, *dst, errcnt);
4038         CP(*src, *dst, stbcnt);
4039 }
4040
4041 static void
4042 timex_from_32(struct timex *dst, struct timex32 *src)
4043 {
4044         CP(*src, *dst, modes);
4045         CP(*src, *dst, offset);
4046         CP(*src, *dst, freq);
4047         CP(*src, *dst, maxerror);
4048         CP(*src, *dst, esterror);
4049         CP(*src, *dst, status);
4050         CP(*src, *dst, constant);
4051         CP(*src, *dst, precision);
4052         CP(*src, *dst, tolerance);
4053         CP(*src, *dst, ppsfreq);
4054         CP(*src, *dst, jitter);
4055         CP(*src, *dst, shift);
4056         CP(*src, *dst, stabil);
4057         CP(*src, *dst, jitcnt);
4058         CP(*src, *dst, calcnt);
4059         CP(*src, *dst, errcnt);
4060         CP(*src, *dst, stbcnt);
4061 }
4062
4063 int
4064 freebsd32_ntp_adjtime(struct thread *td, struct freebsd32_ntp_adjtime_args *uap)
4065 {
4066         struct timex tx;
4067         struct timex32 tx32;
4068         int error, retval;
4069
4070         error = copyin(uap->tp, &tx32, sizeof(tx32));
4071         if (error == 0) {
4072                 timex_from_32(&tx, &tx32);
4073                 error = kern_ntp_adjtime(td, &tx, &retval);
4074                 if (error == 0) {
4075                         timex_to_32(&tx32, &tx);
4076                         error = copyout(&tx32, uap->tp, sizeof(tx32));
4077                         if (error == 0)
4078                                 td->td_retval[0] = retval;
4079                 }
4080         }
4081         return (error);
4082 }
4083
4084 #ifdef FFCLOCK
4085 extern struct mtx ffclock_mtx;
4086 extern struct ffclock_estimate ffclock_estimate;
4087 extern int8_t ffclock_updated;
4088
4089 int
4090 freebsd32_ffclock_setestimate(struct thread *td,
4091     struct freebsd32_ffclock_setestimate_args *uap)
4092 {
4093         struct ffclock_estimate cest;
4094         struct ffclock_estimate32 cest32;
4095         int error;
4096
4097         /* Reuse of PRIV_CLOCK_SETTIME. */
4098         if ((error = priv_check(td, PRIV_CLOCK_SETTIME)) != 0)
4099                 return (error);
4100
4101         if ((error = copyin(uap->cest, &cest32,
4102             sizeof(struct ffclock_estimate32))) != 0)
4103                 return (error);
4104
4105         CP(cest.update_time, cest32.update_time, sec);
4106         memcpy(&cest.update_time.frac, &cest32.update_time.frac, sizeof(uint64_t));
4107         CP(cest, cest32, update_ffcount);
4108         CP(cest, cest32, leapsec_next);
4109         CP(cest, cest32, period);
4110         CP(cest, cest32, errb_abs);
4111         CP(cest, cest32, errb_rate);
4112         CP(cest, cest32, status);
4113         CP(cest, cest32, leapsec_total);
4114         CP(cest, cest32, leapsec);
4115
4116         mtx_lock(&ffclock_mtx);
4117         memcpy(&ffclock_estimate, &cest, sizeof(struct ffclock_estimate));
4118         ffclock_updated++;
4119         mtx_unlock(&ffclock_mtx);
4120         return (error);
4121 }
4122
4123 int
4124 freebsd32_ffclock_getestimate(struct thread *td,
4125     struct freebsd32_ffclock_getestimate_args *uap)
4126 {
4127         struct ffclock_estimate cest;
4128         struct ffclock_estimate32 cest32;
4129         int error;
4130
4131         mtx_lock(&ffclock_mtx);
4132         memcpy(&cest, &ffclock_estimate, sizeof(struct ffclock_estimate));
4133         mtx_unlock(&ffclock_mtx);
4134
4135         CP(cest32.update_time, cest.update_time, sec);
4136         memcpy(&cest32.update_time.frac, &cest.update_time.frac, sizeof(uint64_t));
4137         CP(cest32, cest, update_ffcount);
4138         CP(cest32, cest, leapsec_next);
4139         CP(cest32, cest, period);
4140         CP(cest32, cest, errb_abs);
4141         CP(cest32, cest, errb_rate);
4142         CP(cest32, cest, status);
4143         CP(cest32, cest, leapsec_total);
4144         CP(cest32, cest, leapsec);
4145
4146         error = copyout(&cest32, uap->cest, sizeof(struct ffclock_estimate32));
4147         return (error);
4148 }
4149 #else /* !FFCLOCK */
4150 int
4151 freebsd32_ffclock_setestimate(struct thread *td,
4152     struct freebsd32_ffclock_setestimate_args *uap)
4153 {
4154         return (ENOSYS);
4155 }
4156
4157 int
4158 freebsd32_ffclock_getestimate(struct thread *td,
4159     struct freebsd32_ffclock_getestimate_args *uap)
4160 {
4161         return (ENOSYS);
4162 }
4163 #endif /* FFCLOCK */
4164
4165 #ifdef COMPAT_43
4166 int
4167 ofreebsd32_sethostid(struct thread *td, struct ofreebsd32_sethostid_args *uap)
4168 {
4169         int name[] = { CTL_KERN, KERN_HOSTID };
4170         long hostid;
4171
4172         hostid = uap->hostid;
4173         return (kernel_sysctl(td, name, nitems(name), NULL, NULL, &hostid,
4174             sizeof(hostid), NULL, 0));
4175 }
4176 #endif