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