]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/freebsd32/freebsd32_misc.c
Fix sendmsg(2) privilege escalation.
[FreeBSD/FreeBSD.git] / sys / compat / freebsd32 / freebsd32_misc.c
1 /*-
2  * Copyright (c) 2002 Doug Rabson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include "opt_compat.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 #include <sys/param.h>
38 #include <sys/bus.h>
39 #include <sys/capsicum.h>
40 #include <sys/clock.h>
41 #include <sys/exec.h>
42 #include <sys/fcntl.h>
43 #include <sys/filedesc.h>
44 #include <sys/imgact.h>
45 #include <sys/jail.h>
46 #include <sys/kernel.h>
47 #include <sys/limits.h>
48 #include <sys/linker.h>
49 #include <sys/lock.h>
50 #include <sys/malloc.h>
51 #include <sys/file.h>           /* Must come after sys/malloc.h */
52 #include <sys/imgact.h>
53 #include <sys/mbuf.h>
54 #include <sys/mman.h>
55 #include <sys/module.h>
56 #include <sys/mount.h>
57 #include <sys/mutex.h>
58 #include <sys/namei.h>
59 #include <sys/proc.h>
60 #include <sys/procctl.h>
61 #include <sys/reboot.h>
62 #include <sys/resource.h>
63 #include <sys/resourcevar.h>
64 #include <sys/selinfo.h>
65 #include <sys/eventvar.h>       /* Must come after sys/selinfo.h */
66 #include <sys/pipe.h>           /* Must come after sys/selinfo.h */
67 #include <sys/signal.h>
68 #include <sys/signalvar.h>
69 #include <sys/socket.h>
70 #include <sys/socketvar.h>
71 #include <sys/stat.h>
72 #include <sys/syscall.h>
73 #include <sys/syscallsubr.h>
74 #include <sys/sysctl.h>
75 #include <sys/sysent.h>
76 #include <sys/sysproto.h>
77 #include <sys/systm.h>
78 #include <sys/thr.h>
79 #include <sys/unistd.h>
80 #include <sys/ucontext.h>
81 #include <sys/vnode.h>
82 #include <sys/wait.h>
83 #include <sys/ipc.h>
84 #include <sys/msg.h>
85 #include <sys/sem.h>
86 #include <sys/shm.h>
87 #ifdef KTRACE
88 #include <sys/ktrace.h>
89 #endif
90
91 #ifdef INET
92 #include <netinet/in.h>
93 #endif
94
95 #include <vm/vm.h>
96 #include <vm/vm_param.h>
97 #include <vm/pmap.h>
98 #include <vm/vm_map.h>
99 #include <vm/vm_object.h>
100 #include <vm/vm_extern.h>
101
102 #include <machine/cpu.h>
103 #include <machine/elf.h>
104
105 #include <security/audit/audit.h>
106
107 #include <compat/freebsd32/freebsd32_util.h>
108 #include <compat/freebsd32/freebsd32.h>
109 #include <compat/freebsd32/freebsd32_ipc.h>
110 #include <compat/freebsd32/freebsd32_misc.h>
111 #include <compat/freebsd32/freebsd32_signal.h>
112 #include <compat/freebsd32/freebsd32_proto.h>
113
114 FEATURE(compat_freebsd_32bit, "Compatible with 32-bit FreeBSD");
115
116 #ifndef __mips__
117 CTASSERT(sizeof(struct timeval32) == 8);
118 CTASSERT(sizeof(struct timespec32) == 8);
119 CTASSERT(sizeof(struct itimerval32) == 16);
120 CTASSERT(sizeof(struct bintime32) == 12);
121 #endif
122 CTASSERT(sizeof(struct statfs32) == 256);
123 #ifndef __mips__
124 CTASSERT(sizeof(struct rusage32) == 72);
125 #endif
126 CTASSERT(sizeof(struct sigaltstack32) == 12);
127 CTASSERT(sizeof(struct kevent32) == 20);
128 CTASSERT(sizeof(struct iovec32) == 8);
129 CTASSERT(sizeof(struct msghdr32) == 28);
130 #ifndef __mips__
131 CTASSERT(sizeof(struct stat32) == 96);
132 #endif
133 CTASSERT(sizeof(struct sigaction32) == 24);
134
135 static int freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count);
136 static int freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count);
137 static int freebsd32_user_clock_nanosleep(struct thread *td, clockid_t clock_id,
138     int flags, const struct timespec32 *ua_rqtp, struct timespec32 *ua_rmtp);
139
140 void
141 freebsd32_rusage_out(const struct rusage *s, struct rusage32 *s32)
142 {
143
144         TV_CP(*s, *s32, ru_utime);
145         TV_CP(*s, *s32, ru_stime);
146         CP(*s, *s32, ru_maxrss);
147         CP(*s, *s32, ru_ixrss);
148         CP(*s, *s32, ru_idrss);
149         CP(*s, *s32, ru_isrss);
150         CP(*s, *s32, ru_minflt);
151         CP(*s, *s32, ru_majflt);
152         CP(*s, *s32, ru_nswap);
153         CP(*s, *s32, ru_inblock);
154         CP(*s, *s32, ru_oublock);
155         CP(*s, *s32, ru_msgsnd);
156         CP(*s, *s32, ru_msgrcv);
157         CP(*s, *s32, ru_nsignals);
158         CP(*s, *s32, ru_nvcsw);
159         CP(*s, *s32, ru_nivcsw);
160 }
161
162 int
163 freebsd32_wait4(struct thread *td, struct freebsd32_wait4_args *uap)
164 {
165         int error, status;
166         struct rusage32 ru32;
167         struct rusage ru, *rup;
168
169         if (uap->rusage != NULL)
170                 rup = &ru;
171         else
172                 rup = NULL;
173         error = kern_wait(td, uap->pid, &status, uap->options, rup);
174         if (error)
175                 return (error);
176         if (uap->status != NULL)
177                 error = copyout(&status, uap->status, sizeof(status));
178         if (uap->rusage != NULL && error == 0) {
179                 freebsd32_rusage_out(&ru, &ru32);
180                 error = copyout(&ru32, uap->rusage, sizeof(ru32));
181         }
182         return (error);
183 }
184
185 int
186 freebsd32_wait6(struct thread *td, struct freebsd32_wait6_args *uap)
187 {
188         struct wrusage32 wru32;
189         struct __wrusage wru, *wrup;
190         struct siginfo32 si32;
191         struct __siginfo si, *sip;
192         int error, status;
193
194         if (uap->wrusage != NULL)
195                 wrup = &wru;
196         else
197                 wrup = NULL;
198         if (uap->info != NULL) {
199                 sip = &si;
200                 bzero(sip, sizeof(*sip));
201         } else
202                 sip = NULL;
203         error = kern_wait6(td, uap->idtype, PAIR32TO64(id_t, uap->id),
204             &status, uap->options, wrup, sip);
205         if (error != 0)
206                 return (error);
207         if (uap->status != NULL)
208                 error = copyout(&status, uap->status, sizeof(status));
209         if (uap->wrusage != NULL && error == 0) {
210                 freebsd32_rusage_out(&wru.wru_self, &wru32.wru_self);
211                 freebsd32_rusage_out(&wru.wru_children, &wru32.wru_children);
212                 error = copyout(&wru32, uap->wrusage, sizeof(wru32));
213         }
214         if (uap->info != NULL && error == 0) {
215                 siginfo_to_siginfo32 (&si, &si32);
216                 error = copyout(&si32, uap->info, sizeof(si32));
217         }
218         return (error);
219 }
220
221 #ifdef COMPAT_FREEBSD4
222 static void
223 copy_statfs(struct statfs *in, struct statfs32 *out)
224 {
225
226         statfs_scale_blocks(in, INT32_MAX);
227         bzero(out, sizeof(*out));
228         CP(*in, *out, f_bsize);
229         out->f_iosize = MIN(in->f_iosize, INT32_MAX);
230         CP(*in, *out, f_blocks);
231         CP(*in, *out, f_bfree);
232         CP(*in, *out, f_bavail);
233         out->f_files = MIN(in->f_files, INT32_MAX);
234         out->f_ffree = MIN(in->f_ffree, INT32_MAX);
235         CP(*in, *out, f_fsid);
236         CP(*in, *out, f_owner);
237         CP(*in, *out, f_type);
238         CP(*in, *out, f_flags);
239         out->f_syncwrites = MIN(in->f_syncwrites, INT32_MAX);
240         out->f_asyncwrites = MIN(in->f_asyncwrites, INT32_MAX);
241         strlcpy(out->f_fstypename,
242               in->f_fstypename, MFSNAMELEN);
243         strlcpy(out->f_mntonname,
244               in->f_mntonname, min(MNAMELEN, FREEBSD4_MNAMELEN));
245         out->f_syncreads = MIN(in->f_syncreads, INT32_MAX);
246         out->f_asyncreads = MIN(in->f_asyncreads, INT32_MAX);
247         strlcpy(out->f_mntfromname,
248               in->f_mntfromname, min(MNAMELEN, FREEBSD4_MNAMELEN));
249 }
250 #endif
251
252 #ifdef COMPAT_FREEBSD4
253 int
254 freebsd4_freebsd32_getfsstat(struct thread *td,
255     struct freebsd4_freebsd32_getfsstat_args *uap)
256 {
257         struct statfs *buf, *sp;
258         struct statfs32 stat32;
259         size_t count, size, copycount;
260         int error;
261
262         count = uap->bufsize / sizeof(struct statfs32);
263         size = count * sizeof(struct statfs);
264         error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE, uap->mode);
265         if (size > 0) {
266                 sp = buf;
267                 copycount = count;
268                 while (copycount > 0 && error == 0) {
269                         copy_statfs(sp, &stat32);
270                         error = copyout(&stat32, uap->buf, sizeof(stat32));
271                         sp++;
272                         uap->buf++;
273                         copycount--;
274                 }
275                 free(buf, M_STATFS);
276         }
277         if (error == 0)
278                 td->td_retval[0] = count;
279         return (error);
280 }
281 #endif
282
283 #ifdef COMPAT_FREEBSD10
284 int
285 freebsd10_freebsd32_pipe(struct thread *td,
286     struct freebsd10_freebsd32_pipe_args *uap) {
287         
288         return (freebsd10_pipe(td, (struct freebsd10_pipe_args*)uap));
289 }
290 #endif
291
292 int
293 freebsd32_sigaltstack(struct thread *td,
294                       struct freebsd32_sigaltstack_args *uap)
295 {
296         struct sigaltstack32 s32;
297         struct sigaltstack ss, oss, *ssp;
298         int error;
299
300         if (uap->ss != NULL) {
301                 error = copyin(uap->ss, &s32, sizeof(s32));
302                 if (error)
303                         return (error);
304                 PTRIN_CP(s32, ss, ss_sp);
305                 CP(s32, ss, ss_size);
306                 CP(s32, ss, ss_flags);
307                 ssp = &ss;
308         } else
309                 ssp = NULL;
310         error = kern_sigaltstack(td, ssp, &oss);
311         if (error == 0 && uap->oss != NULL) {
312                 PTROUT_CP(oss, s32, ss_sp);
313                 CP(oss, s32, ss_size);
314                 CP(oss, s32, ss_flags);
315                 error = copyout(&s32, uap->oss, sizeof(s32));
316         }
317         return (error);
318 }
319
320 /*
321  * Custom version of exec_copyin_args() so that we can translate
322  * the pointers.
323  */
324 int
325 freebsd32_exec_copyin_args(struct image_args *args, char *fname,
326     enum uio_seg segflg, u_int32_t *argv, u_int32_t *envv)
327 {
328         char *argp, *envp;
329         u_int32_t *p32, arg;
330         size_t length;
331         int error;
332
333         bzero(args, sizeof(*args));
334         if (argv == NULL)
335                 return (EFAULT);
336
337         /*
338          * Allocate demand-paged memory for the file name, argument, and
339          * environment strings.
340          */
341         error = exec_alloc_args(args);
342         if (error != 0)
343                 return (error);
344
345         /*
346          * Copy the file name.
347          */
348         if (fname != NULL) {
349                 args->fname = args->buf;
350                 error = (segflg == UIO_SYSSPACE) ?
351                     copystr(fname, args->fname, PATH_MAX, &length) :
352                     copyinstr(fname, args->fname, PATH_MAX, &length);
353                 if (error != 0)
354                         goto err_exit;
355         } else
356                 length = 0;
357
358         args->begin_argv = args->buf + length;
359         args->endp = args->begin_argv;
360         args->stringspace = ARG_MAX;
361
362         /*
363          * extract arguments first
364          */
365         p32 = argv;
366         for (;;) {
367                 error = copyin(p32++, &arg, sizeof(arg));
368                 if (error)
369                         goto err_exit;
370                 if (arg == 0)
371                         break;
372                 argp = PTRIN(arg);
373                 error = copyinstr(argp, args->endp, args->stringspace, &length);
374                 if (error) {
375                         if (error == ENAMETOOLONG)
376                                 error = E2BIG;
377                         goto err_exit;
378                 }
379                 args->stringspace -= length;
380                 args->endp += length;
381                 args->argc++;
382         }
383                         
384         args->begin_envv = args->endp;
385
386         /*
387          * extract environment strings
388          */
389         if (envv) {
390                 p32 = envv;
391                 for (;;) {
392                         error = copyin(p32++, &arg, sizeof(arg));
393                         if (error)
394                                 goto err_exit;
395                         if (arg == 0)
396                                 break;
397                         envp = PTRIN(arg);
398                         error = copyinstr(envp, args->endp, args->stringspace,
399                             &length);
400                         if (error) {
401                                 if (error == ENAMETOOLONG)
402                                         error = E2BIG;
403                                 goto err_exit;
404                         }
405                         args->stringspace -= length;
406                         args->endp += length;
407                         args->envc++;
408                 }
409         }
410
411         return (0);
412
413 err_exit:
414         exec_free_args(args);
415         return (error);
416 }
417
418 int
419 freebsd32_execve(struct thread *td, struct freebsd32_execve_args *uap)
420 {
421         struct image_args eargs;
422         struct vmspace *oldvmspace;
423         int error;
424
425         error = pre_execve(td, &oldvmspace);
426         if (error != 0)
427                 return (error);
428         error = freebsd32_exec_copyin_args(&eargs, uap->fname, UIO_USERSPACE,
429             uap->argv, uap->envv);
430         if (error == 0)
431                 error = kern_execve(td, &eargs, NULL);
432         post_execve(td, error, oldvmspace);
433         return (error);
434 }
435
436 int
437 freebsd32_fexecve(struct thread *td, struct freebsd32_fexecve_args *uap)
438 {
439         struct image_args eargs;
440         struct vmspace *oldvmspace;
441         int error;
442
443         error = pre_execve(td, &oldvmspace);
444         if (error != 0)
445                 return (error);
446         error = freebsd32_exec_copyin_args(&eargs, NULL, UIO_SYSSPACE,
447             uap->argv, uap->envv);
448         if (error == 0) {
449                 eargs.fd = uap->fd;
450                 error = kern_execve(td, &eargs, NULL);
451         }
452         post_execve(td, error, oldvmspace);
453         return (error);
454 }
455
456 int
457 freebsd32_mprotect(struct thread *td, struct freebsd32_mprotect_args *uap)
458 {
459         int prot;
460
461         prot = uap->prot;
462 #if defined(__amd64__)
463         if (i386_read_exec && (prot & PROT_READ) != 0)
464                 prot |= PROT_EXEC;
465 #endif
466         return (kern_mprotect(td, (uintptr_t)PTRIN(uap->addr), uap->len,
467             prot));
468 }
469
470 int
471 freebsd32_mmap(struct thread *td, struct freebsd32_mmap_args *uap)
472 {
473         int prot;
474
475         prot = uap->prot;
476 #if defined(__amd64__)
477         if (i386_read_exec && (prot & PROT_READ))
478                 prot |= PROT_EXEC;
479 #endif
480
481         return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, prot,
482             uap->flags, uap->fd, PAIR32TO64(off_t, uap->pos)));
483 }
484
485 #ifdef COMPAT_FREEBSD6
486 int
487 freebsd6_freebsd32_mmap(struct thread *td,
488     struct freebsd6_freebsd32_mmap_args *uap)
489 {
490         int prot;
491
492         prot = uap->prot;
493 #if defined(__amd64__)
494         if (i386_read_exec && (prot & PROT_READ))
495                 prot |= PROT_EXEC;
496 #endif
497
498         return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, prot,
499             uap->flags, uap->fd, PAIR32TO64(off_t, uap->pos)));
500 }
501 #endif
502
503 int
504 freebsd32_setitimer(struct thread *td, struct freebsd32_setitimer_args *uap)
505 {
506         struct itimerval itv, oitv, *itvp;      
507         struct itimerval32 i32;
508         int error;
509
510         if (uap->itv != NULL) {
511                 error = copyin(uap->itv, &i32, sizeof(i32));
512                 if (error)
513                         return (error);
514                 TV_CP(i32, itv, it_interval);
515                 TV_CP(i32, itv, it_value);
516                 itvp = &itv;
517         } else
518                 itvp = NULL;
519         error = kern_setitimer(td, uap->which, itvp, &oitv);
520         if (error || uap->oitv == NULL)
521                 return (error);
522         TV_CP(oitv, i32, it_interval);
523         TV_CP(oitv, i32, it_value);
524         return (copyout(&i32, uap->oitv, sizeof(i32)));
525 }
526
527 int
528 freebsd32_getitimer(struct thread *td, struct freebsd32_getitimer_args *uap)
529 {
530         struct itimerval itv;
531         struct itimerval32 i32;
532         int error;
533
534         error = kern_getitimer(td, uap->which, &itv);
535         if (error || uap->itv == NULL)
536                 return (error);
537         TV_CP(itv, i32, it_interval);
538         TV_CP(itv, i32, it_value);
539         return (copyout(&i32, uap->itv, sizeof(i32)));
540 }
541
542 int
543 freebsd32_select(struct thread *td, struct freebsd32_select_args *uap)
544 {
545         struct timeval32 tv32;
546         struct timeval tv, *tvp;
547         int error;
548
549         if (uap->tv != NULL) {
550                 error = copyin(uap->tv, &tv32, sizeof(tv32));
551                 if (error)
552                         return (error);
553                 CP(tv32, tv, tv_sec);
554                 CP(tv32, tv, tv_usec);
555                 tvp = &tv;
556         } else
557                 tvp = NULL;
558         /*
559          * XXX Do pointers need PTRIN()?
560          */
561         return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
562             sizeof(int32_t) * 8));
563 }
564
565 int
566 freebsd32_pselect(struct thread *td, struct freebsd32_pselect_args *uap)
567 {
568         struct timespec32 ts32;
569         struct timespec ts;
570         struct timeval tv, *tvp;
571         sigset_t set, *uset;
572         int error;
573
574         if (uap->ts != NULL) {
575                 error = copyin(uap->ts, &ts32, sizeof(ts32));
576                 if (error != 0)
577                         return (error);
578                 CP(ts32, ts, tv_sec);
579                 CP(ts32, ts, tv_nsec);
580                 TIMESPEC_TO_TIMEVAL(&tv, &ts);
581                 tvp = &tv;
582         } else
583                 tvp = NULL;
584         if (uap->sm != NULL) {
585                 error = copyin(uap->sm, &set, sizeof(set));
586                 if (error != 0)
587                         return (error);
588                 uset = &set;
589         } else
590                 uset = NULL;
591         /*
592          * XXX Do pointers need PTRIN()?
593          */
594         error = kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
595             uset, sizeof(int32_t) * 8);
596         return (error);
597 }
598
599 /*
600  * Copy 'count' items into the destination list pointed to by uap->eventlist.
601  */
602 static int
603 freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count)
604 {
605         struct freebsd32_kevent_args *uap;
606         struct kevent32 ks32[KQ_NEVENTS];
607         int i, error = 0;
608
609         KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
610         uap = (struct freebsd32_kevent_args *)arg;
611
612         for (i = 0; i < count; i++) {
613                 CP(kevp[i], ks32[i], ident);
614                 CP(kevp[i], ks32[i], filter);
615                 CP(kevp[i], ks32[i], flags);
616                 CP(kevp[i], ks32[i], fflags);
617                 CP(kevp[i], ks32[i], data);
618                 PTROUT_CP(kevp[i], ks32[i], udata);
619         }
620         error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
621         if (error == 0)
622                 uap->eventlist += count;
623         return (error);
624 }
625
626 /*
627  * Copy 'count' items from the list pointed to by uap->changelist.
628  */
629 static int
630 freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count)
631 {
632         struct freebsd32_kevent_args *uap;
633         struct kevent32 ks32[KQ_NEVENTS];
634         int i, error = 0;
635
636         KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
637         uap = (struct freebsd32_kevent_args *)arg;
638
639         error = copyin(uap->changelist, ks32, count * sizeof *ks32);
640         if (error)
641                 goto done;
642         uap->changelist += count;
643
644         for (i = 0; i < count; i++) {
645                 CP(ks32[i], kevp[i], ident);
646                 CP(ks32[i], kevp[i], filter);
647                 CP(ks32[i], kevp[i], flags);
648                 CP(ks32[i], kevp[i], fflags);
649                 CP(ks32[i], kevp[i], data);
650                 PTRIN_CP(ks32[i], kevp[i], udata);
651         }
652 done:
653         return (error);
654 }
655
656 int
657 freebsd32_kevent(struct thread *td, struct freebsd32_kevent_args *uap)
658 {
659         struct timespec32 ts32;
660         struct timespec ts, *tsp;
661         struct kevent_copyops k_ops = {
662                 .arg = uap,
663                 .k_copyout = freebsd32_kevent_copyout,
664                 .k_copyin = freebsd32_kevent_copyin,
665         };
666 #ifdef KTRACE
667         struct kevent32 *eventlist = uap->eventlist;
668 #endif
669         int error;
670
671
672         if (uap->timeout) {
673                 error = copyin(uap->timeout, &ts32, sizeof(ts32));
674                 if (error)
675                         return (error);
676                 CP(ts32, ts, tv_sec);
677                 CP(ts32, ts, tv_nsec);
678                 tsp = &ts;
679         } else
680                 tsp = NULL;
681 #ifdef KTRACE
682         if (KTRPOINT(td, KTR_STRUCT_ARRAY))
683                 ktrstructarray("kevent32", UIO_USERSPACE, uap->changelist,
684                     uap->nchanges, sizeof(struct kevent32));
685 #endif
686         error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
687             &k_ops, tsp);
688 #ifdef KTRACE
689         if (error == 0 && KTRPOINT(td, KTR_STRUCT_ARRAY))
690                 ktrstructarray("kevent32", UIO_USERSPACE, eventlist,
691                     td->td_retval[0], sizeof(struct kevent32));
692 #endif
693         return (error);
694 }
695
696 int
697 freebsd32_gettimeofday(struct thread *td,
698                        struct freebsd32_gettimeofday_args *uap)
699 {
700         struct timeval atv;
701         struct timeval32 atv32;
702         struct timezone rtz;
703         int error = 0;
704
705         if (uap->tp) {
706                 microtime(&atv);
707                 CP(atv, atv32, tv_sec);
708                 CP(atv, atv32, tv_usec);
709                 error = copyout(&atv32, uap->tp, sizeof (atv32));
710         }
711         if (error == 0 && uap->tzp != NULL) {
712                 rtz.tz_minuteswest = tz_minuteswest;
713                 rtz.tz_dsttime = tz_dsttime;
714                 error = copyout(&rtz, uap->tzp, sizeof (rtz));
715         }
716         return (error);
717 }
718
719 int
720 freebsd32_getrusage(struct thread *td, struct freebsd32_getrusage_args *uap)
721 {
722         struct rusage32 s32;
723         struct rusage s;
724         int error;
725
726         error = kern_getrusage(td, uap->who, &s);
727         if (error == 0) {
728                 freebsd32_rusage_out(&s, &s32);
729                 error = copyout(&s32, uap->rusage, sizeof(s32));
730         }
731         return (error);
732 }
733
734 static int
735 freebsd32_copyinuio(struct iovec32 *iovp, u_int iovcnt, struct uio **uiop)
736 {
737         struct iovec32 iov32;
738         struct iovec *iov;
739         struct uio *uio;
740         u_int iovlen;
741         int error, i;
742
743         *uiop = NULL;
744         if (iovcnt > UIO_MAXIOV)
745                 return (EINVAL);
746         iovlen = iovcnt * sizeof(struct iovec);
747         uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
748         iov = (struct iovec *)(uio + 1);
749         for (i = 0; i < iovcnt; i++) {
750                 error = copyin(&iovp[i], &iov32, sizeof(struct iovec32));
751                 if (error) {
752                         free(uio, M_IOV);
753                         return (error);
754                 }
755                 iov[i].iov_base = PTRIN(iov32.iov_base);
756                 iov[i].iov_len = iov32.iov_len;
757         }
758         uio->uio_iov = iov;
759         uio->uio_iovcnt = iovcnt;
760         uio->uio_segflg = UIO_USERSPACE;
761         uio->uio_offset = -1;
762         uio->uio_resid = 0;
763         for (i = 0; i < iovcnt; i++) {
764                 if (iov->iov_len > INT_MAX - uio->uio_resid) {
765                         free(uio, M_IOV);
766                         return (EINVAL);
767                 }
768                 uio->uio_resid += iov->iov_len;
769                 iov++;
770         }
771         *uiop = uio;
772         return (0);
773 }
774
775 int
776 freebsd32_readv(struct thread *td, struct freebsd32_readv_args *uap)
777 {
778         struct uio *auio;
779         int error;
780
781         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
782         if (error)
783                 return (error);
784         error = kern_readv(td, uap->fd, auio);
785         free(auio, M_IOV);
786         return (error);
787 }
788
789 int
790 freebsd32_writev(struct thread *td, struct freebsd32_writev_args *uap)
791 {
792         struct uio *auio;
793         int error;
794
795         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
796         if (error)
797                 return (error);
798         error = kern_writev(td, uap->fd, auio);
799         free(auio, M_IOV);
800         return (error);
801 }
802
803 int
804 freebsd32_preadv(struct thread *td, struct freebsd32_preadv_args *uap)
805 {
806         struct uio *auio;
807         int error;
808
809         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
810         if (error)
811                 return (error);
812         error = kern_preadv(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
813         free(auio, M_IOV);
814         return (error);
815 }
816
817 int
818 freebsd32_pwritev(struct thread *td, struct freebsd32_pwritev_args *uap)
819 {
820         struct uio *auio;
821         int error;
822
823         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
824         if (error)
825                 return (error);
826         error = kern_pwritev(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
827         free(auio, M_IOV);
828         return (error);
829 }
830
831 int
832 freebsd32_copyiniov(struct iovec32 *iovp32, u_int iovcnt, struct iovec **iovp,
833     int error)
834 {
835         struct iovec32 iov32;
836         struct iovec *iov;
837         u_int iovlen;
838         int i;
839
840         *iovp = NULL;
841         if (iovcnt > UIO_MAXIOV)
842                 return (error);
843         iovlen = iovcnt * sizeof(struct iovec);
844         iov = malloc(iovlen, M_IOV, M_WAITOK);
845         for (i = 0; i < iovcnt; i++) {
846                 error = copyin(&iovp32[i], &iov32, sizeof(struct iovec32));
847                 if (error) {
848                         free(iov, M_IOV);
849                         return (error);
850                 }
851                 iov[i].iov_base = PTRIN(iov32.iov_base);
852                 iov[i].iov_len = iov32.iov_len;
853         }
854         *iovp = iov;
855         return (0);
856 }
857
858 static int
859 freebsd32_copyinmsghdr(struct msghdr32 *msg32, struct msghdr *msg)
860 {
861         struct msghdr32 m32;
862         int error;
863
864         error = copyin(msg32, &m32, sizeof(m32));
865         if (error)
866                 return (error);
867         msg->msg_name = PTRIN(m32.msg_name);
868         msg->msg_namelen = m32.msg_namelen;
869         msg->msg_iov = PTRIN(m32.msg_iov);
870         msg->msg_iovlen = m32.msg_iovlen;
871         msg->msg_control = PTRIN(m32.msg_control);
872         msg->msg_controllen = m32.msg_controllen;
873         msg->msg_flags = m32.msg_flags;
874         return (0);
875 }
876
877 static int
878 freebsd32_copyoutmsghdr(struct msghdr *msg, struct msghdr32 *msg32)
879 {
880         struct msghdr32 m32;
881         int error;
882
883         m32.msg_name = PTROUT(msg->msg_name);
884         m32.msg_namelen = msg->msg_namelen;
885         m32.msg_iov = PTROUT(msg->msg_iov);
886         m32.msg_iovlen = msg->msg_iovlen;
887         m32.msg_control = PTROUT(msg->msg_control);
888         m32.msg_controllen = msg->msg_controllen;
889         m32.msg_flags = msg->msg_flags;
890         error = copyout(&m32, msg32, sizeof(m32));
891         return (error);
892 }
893
894 #ifndef __mips__
895 #define FREEBSD32_ALIGNBYTES    (sizeof(int) - 1)
896 #else
897 #define FREEBSD32_ALIGNBYTES    (sizeof(long) - 1)
898 #endif
899 #define FREEBSD32_ALIGN(p)      \
900         (((u_long)(p) + FREEBSD32_ALIGNBYTES) & ~FREEBSD32_ALIGNBYTES)
901 #define FREEBSD32_CMSG_SPACE(l) \
902         (FREEBSD32_ALIGN(sizeof(struct cmsghdr)) + FREEBSD32_ALIGN(l))
903
904 #define FREEBSD32_CMSG_DATA(cmsg)       ((unsigned char *)(cmsg) + \
905                                  FREEBSD32_ALIGN(sizeof(struct cmsghdr)))
906
907 static size_t
908 freebsd32_cmsg_convert(const struct cmsghdr *cm, void *data, socklen_t datalen)
909 {
910         size_t copylen;
911         union {
912                 struct timespec32 ts;
913                 struct timeval32 tv;
914                 struct bintime32 bt;
915         } tmp32;
916
917         union {
918                 struct timespec ts;
919                 struct timeval tv;
920                 struct bintime bt;
921         } *in;
922
923         in = data;
924         copylen = 0;
925         switch (cm->cmsg_level) {
926         case SOL_SOCKET:
927                 switch (cm->cmsg_type) {
928                 case SCM_TIMESTAMP:
929                         TV_CP(*in, tmp32, tv);
930                         copylen = sizeof(tmp32.tv);
931                         break;
932
933                 case SCM_BINTIME:
934                         BT_CP(*in, tmp32, bt);
935                         copylen = sizeof(tmp32.bt);
936                         break;
937
938                 case SCM_REALTIME:
939                 case SCM_MONOTONIC:
940                         TS_CP(*in, tmp32, ts);
941                         copylen = sizeof(tmp32.ts);
942                         break;
943
944                 default:
945                         break;
946                 }
947
948         default:
949                 break;
950         }
951
952         if (copylen == 0)
953                 return (datalen);
954
955         KASSERT((datalen >= copylen), ("corrupted cmsghdr"));
956
957         bcopy(&tmp32, data, copylen);
958         return (copylen);
959 }
960
961 static int
962 freebsd32_copy_msg_out(struct msghdr *msg, struct mbuf *control)
963 {
964         struct cmsghdr *cm;
965         void *data;
966         socklen_t clen, datalen, datalen_out, oldclen;
967         int error;
968         caddr_t ctlbuf;
969         int len, maxlen, copylen;
970         struct mbuf *m;
971         error = 0;
972
973         len    = msg->msg_controllen;
974         maxlen = msg->msg_controllen;
975         msg->msg_controllen = 0;
976
977         ctlbuf = msg->msg_control;
978         for (m = control; m != NULL && len > 0; m = m->m_next) {
979                 cm = mtod(m, struct cmsghdr *);
980                 clen = m->m_len;
981                 while (cm != NULL) {
982                         if (sizeof(struct cmsghdr) > clen ||
983                             cm->cmsg_len > clen) {
984                                 error = EINVAL;
985                                 break;
986                         }
987
988                         data   = CMSG_DATA(cm);
989                         datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
990                         datalen_out = freebsd32_cmsg_convert(cm, data, datalen);
991
992                         /*
993                          * Copy out the message header.  Preserve the native
994                          * message size in case we need to inspect the message
995                          * contents later.
996                          */
997                         copylen = sizeof(struct cmsghdr);
998                         if (len < copylen) {
999                                 msg->msg_flags |= MSG_CTRUNC;
1000                                 m_dispose_extcontrolm(m);
1001                                 goto exit;
1002                         }
1003                         oldclen = cm->cmsg_len;
1004                         cm->cmsg_len = FREEBSD32_ALIGN(sizeof(struct cmsghdr)) +
1005                             datalen_out;
1006                         error = copyout(cm, ctlbuf, copylen);
1007                         cm->cmsg_len = oldclen;
1008                         if (error != 0)
1009                                 goto exit;
1010
1011                         ctlbuf += FREEBSD32_ALIGN(copylen);
1012                         len    -= FREEBSD32_ALIGN(copylen);
1013
1014                         copylen = datalen_out;
1015                         if (len < copylen) {
1016                                 msg->msg_flags |= MSG_CTRUNC;
1017                                 m_dispose_extcontrolm(m);
1018                                 break;
1019                         }
1020
1021                         /* Copy out the message data. */
1022                         error = copyout(data, ctlbuf, copylen);
1023                         if (error)
1024                                 goto exit;
1025
1026                         ctlbuf += FREEBSD32_ALIGN(copylen);
1027                         len    -= FREEBSD32_ALIGN(copylen);
1028
1029                         if (CMSG_SPACE(datalen) < clen) {
1030                                 clen -= CMSG_SPACE(datalen);
1031                                 cm = (struct cmsghdr *)
1032                                     ((caddr_t)cm + CMSG_SPACE(datalen));
1033                         } else {
1034                                 clen = 0;
1035                                 cm = NULL;
1036                         }
1037
1038                         msg->msg_controllen +=
1039                             FREEBSD32_CMSG_SPACE(datalen_out);
1040                 }
1041         }
1042         if (len == 0 && m != NULL) {
1043                 msg->msg_flags |= MSG_CTRUNC;
1044                 m_dispose_extcontrolm(m);
1045         }
1046
1047 exit:
1048         return (error);
1049 }
1050
1051 int
1052 freebsd32_recvmsg(td, uap)
1053         struct thread *td;
1054         struct freebsd32_recvmsg_args /* {
1055                 int     s;
1056                 struct  msghdr32 *msg;
1057                 int     flags;
1058         } */ *uap;
1059 {
1060         struct msghdr msg;
1061         struct msghdr32 m32;
1062         struct iovec *uiov, *iov;
1063         struct mbuf *control = NULL;
1064         struct mbuf **controlp;
1065
1066         int error;
1067         error = copyin(uap->msg, &m32, sizeof(m32));
1068         if (error)
1069                 return (error);
1070         error = freebsd32_copyinmsghdr(uap->msg, &msg);
1071         if (error)
1072                 return (error);
1073         error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1074             EMSGSIZE);
1075         if (error)
1076                 return (error);
1077         msg.msg_flags = uap->flags;
1078         uiov = msg.msg_iov;
1079         msg.msg_iov = iov;
1080
1081         controlp = (msg.msg_control != NULL) ?  &control : NULL;
1082         error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, controlp);
1083         if (error == 0) {
1084                 msg.msg_iov = uiov;
1085
1086                 if (control != NULL)
1087                         error = freebsd32_copy_msg_out(&msg, control);
1088                 else
1089                         msg.msg_controllen = 0;
1090
1091                 if (error == 0)
1092                         error = freebsd32_copyoutmsghdr(&msg, uap->msg);
1093         }
1094         free(iov, M_IOV);
1095
1096         if (control != NULL) {
1097                 if (error != 0)
1098                         m_dispose_extcontrolm(control);
1099                 m_freem(control);
1100         }
1101
1102         return (error);
1103 }
1104
1105 /*
1106  * Copy-in the array of control messages constructed using alignment
1107  * and padding suitable for a 32-bit environment and construct an
1108  * mbuf using alignment and padding suitable for a 64-bit kernel.
1109  * The alignment and padding are defined indirectly by CMSG_DATA(),
1110  * CMSG_SPACE() and CMSG_LEN().
1111  */
1112 static int
1113 freebsd32_copyin_control(struct mbuf **mp, caddr_t buf, u_int buflen)
1114 {
1115         struct cmsghdr *cm;
1116         struct mbuf *m;
1117         void *in, *in1, *md;
1118         u_int msglen, outlen;
1119         int error;
1120
1121         if (buflen > MCLBYTES)
1122                 return (EINVAL);
1123
1124         in = malloc(buflen, M_TEMP, M_WAITOK);
1125         error = copyin(buf, in, buflen);
1126         if (error != 0)
1127                 goto out;
1128
1129         /*
1130          * Make a pass over the input buffer to determine the amount of space
1131          * required for 64 bit-aligned copies of the control messages.
1132          */
1133         in1 = in;
1134         outlen = 0;
1135         while (buflen > 0) {
1136                 if (buflen < sizeof(*cm)) {
1137                         error = EINVAL;
1138                         break;
1139                 }
1140                 cm = (struct cmsghdr *)in1;
1141                 if (cm->cmsg_len < FREEBSD32_ALIGN(sizeof(*cm))) {
1142                         error = EINVAL;
1143                         break;
1144                 }
1145                 msglen = FREEBSD32_ALIGN(cm->cmsg_len);
1146                 if (msglen > buflen || msglen < cm->cmsg_len) {
1147                         error = EINVAL;
1148                         break;
1149                 }
1150                 buflen -= msglen;
1151
1152                 in1 = (char *)in1 + msglen;
1153                 outlen += CMSG_ALIGN(sizeof(*cm)) +
1154                     CMSG_ALIGN(msglen - FREEBSD32_ALIGN(sizeof(*cm)));
1155         }
1156         if (error == 0 && outlen > MCLBYTES) {
1157                 /*
1158                  * XXXMJ This implies that the upper limit on 32-bit aligned
1159                  * control messages is less than MCLBYTES, and so we are not
1160                  * perfectly compatible.  However, there is no platform
1161                  * guarantee that mbuf clusters larger than MCLBYTES can be
1162                  * allocated.
1163                  */
1164                 error = EINVAL;
1165         }
1166         if (error != 0)
1167                 goto out;
1168
1169         m = m_get2(outlen, M_WAITOK, MT_CONTROL, 0);
1170         m->m_len = outlen;
1171         md = mtod(m, void *);
1172
1173         /*
1174          * Make a second pass over input messages, copying them into the output
1175          * buffer.
1176          */
1177         in1 = in;
1178         while (outlen > 0) {
1179                 /* Copy the message header and align the length field. */
1180                 cm = md;
1181                 memcpy(cm, in1, sizeof(*cm));
1182                 msglen = cm->cmsg_len - FREEBSD32_ALIGN(sizeof(*cm));
1183                 cm->cmsg_len = CMSG_ALIGN(sizeof(*cm)) + msglen;
1184
1185                 /* Copy the message body. */
1186                 in1 = (char *)in1 + FREEBSD32_ALIGN(sizeof(*cm));
1187                 md = (char *)md + CMSG_ALIGN(sizeof(*cm));
1188                 memcpy(md, in1, msglen);
1189                 in1 = (char *)in1 + FREEBSD32_ALIGN(msglen);
1190                 md = (char *)md + CMSG_ALIGN(msglen);
1191                 KASSERT(outlen >= CMSG_ALIGN(sizeof(*cm)) + CMSG_ALIGN(msglen),
1192                     ("outlen %u underflow, msglen %u", outlen, msglen));
1193                 outlen -= CMSG_ALIGN(sizeof(*cm)) + CMSG_ALIGN(msglen);
1194         }
1195
1196         *mp = m;
1197 out:
1198         free(in, M_TEMP);
1199         return (error);
1200 }
1201
1202 int
1203 freebsd32_sendmsg(struct thread *td,
1204                   struct freebsd32_sendmsg_args *uap)
1205 {
1206         struct msghdr msg;
1207         struct msghdr32 m32;
1208         struct iovec *iov;
1209         struct mbuf *control = NULL;
1210         struct sockaddr *to = NULL;
1211         int error;
1212
1213         error = copyin(uap->msg, &m32, sizeof(m32));
1214         if (error)
1215                 return (error);
1216         error = freebsd32_copyinmsghdr(uap->msg, &msg);
1217         if (error)
1218                 return (error);
1219         error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1220             EMSGSIZE);
1221         if (error)
1222                 return (error);
1223         msg.msg_iov = iov;
1224         if (msg.msg_name != NULL) {
1225                 error = getsockaddr(&to, msg.msg_name, msg.msg_namelen);
1226                 if (error) {
1227                         to = NULL;
1228                         goto out;
1229                 }
1230                 msg.msg_name = to;
1231         }
1232
1233         if (msg.msg_control) {
1234                 if (msg.msg_controllen < sizeof(struct cmsghdr)) {
1235                         error = EINVAL;
1236                         goto out;
1237                 }
1238
1239                 error = freebsd32_copyin_control(&control, msg.msg_control,
1240                     msg.msg_controllen);
1241                 if (error)
1242                         goto out;
1243
1244                 msg.msg_control = NULL;
1245                 msg.msg_controllen = 0;
1246         }
1247
1248         error = kern_sendit(td, uap->s, &msg, uap->flags, control,
1249             UIO_USERSPACE);
1250
1251 out:
1252         free(iov, M_IOV);
1253         if (to)
1254                 free(to, M_SONAME);
1255         return (error);
1256 }
1257
1258 int
1259 freebsd32_recvfrom(struct thread *td,
1260                    struct freebsd32_recvfrom_args *uap)
1261 {
1262         struct msghdr msg;
1263         struct iovec aiov;
1264         int error;
1265
1266         if (uap->fromlenaddr) {
1267                 error = copyin(PTRIN(uap->fromlenaddr), &msg.msg_namelen,
1268                     sizeof(msg.msg_namelen));
1269                 if (error)
1270                         return (error);
1271         } else {
1272                 msg.msg_namelen = 0;
1273         }
1274
1275         msg.msg_name = PTRIN(uap->from);
1276         msg.msg_iov = &aiov;
1277         msg.msg_iovlen = 1;
1278         aiov.iov_base = PTRIN(uap->buf);
1279         aiov.iov_len = uap->len;
1280         msg.msg_control = NULL;
1281         msg.msg_flags = uap->flags;
1282         error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, NULL);
1283         if (error == 0 && uap->fromlenaddr)
1284                 error = copyout(&msg.msg_namelen, PTRIN(uap->fromlenaddr),
1285                     sizeof (msg.msg_namelen));
1286         return (error);
1287 }
1288
1289 int
1290 freebsd32_settimeofday(struct thread *td,
1291                        struct freebsd32_settimeofday_args *uap)
1292 {
1293         struct timeval32 tv32;
1294         struct timeval tv, *tvp;
1295         struct timezone tz, *tzp;
1296         int error;
1297
1298         if (uap->tv) {
1299                 error = copyin(uap->tv, &tv32, sizeof(tv32));
1300                 if (error)
1301                         return (error);
1302                 CP(tv32, tv, tv_sec);
1303                 CP(tv32, tv, tv_usec);
1304                 tvp = &tv;
1305         } else
1306                 tvp = NULL;
1307         if (uap->tzp) {
1308                 error = copyin(uap->tzp, &tz, sizeof(tz));
1309                 if (error)
1310                         return (error);
1311                 tzp = &tz;
1312         } else
1313                 tzp = NULL;
1314         return (kern_settimeofday(td, tvp, tzp));
1315 }
1316
1317 int
1318 freebsd32_utimes(struct thread *td, struct freebsd32_utimes_args *uap)
1319 {
1320         struct timeval32 s32[2];
1321         struct timeval s[2], *sp;
1322         int error;
1323
1324         if (uap->tptr != NULL) {
1325                 error = copyin(uap->tptr, s32, sizeof(s32));
1326                 if (error)
1327                         return (error);
1328                 CP(s32[0], s[0], tv_sec);
1329                 CP(s32[0], s[0], tv_usec);
1330                 CP(s32[1], s[1], tv_sec);
1331                 CP(s32[1], s[1], tv_usec);
1332                 sp = s;
1333         } else
1334                 sp = NULL;
1335         return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1336             sp, UIO_SYSSPACE));
1337 }
1338
1339 int
1340 freebsd32_lutimes(struct thread *td, struct freebsd32_lutimes_args *uap)
1341 {
1342         struct timeval32 s32[2];
1343         struct timeval s[2], *sp;
1344         int error;
1345
1346         if (uap->tptr != NULL) {
1347                 error = copyin(uap->tptr, s32, sizeof(s32));
1348                 if (error)
1349                         return (error);
1350                 CP(s32[0], s[0], tv_sec);
1351                 CP(s32[0], s[0], tv_usec);
1352                 CP(s32[1], s[1], tv_sec);
1353                 CP(s32[1], s[1], tv_usec);
1354                 sp = s;
1355         } else
1356                 sp = NULL;
1357         return (kern_lutimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1358 }
1359
1360 int
1361 freebsd32_futimes(struct thread *td, struct freebsd32_futimes_args *uap)
1362 {
1363         struct timeval32 s32[2];
1364         struct timeval s[2], *sp;
1365         int error;
1366
1367         if (uap->tptr != NULL) {
1368                 error = copyin(uap->tptr, s32, sizeof(s32));
1369                 if (error)
1370                         return (error);
1371                 CP(s32[0], s[0], tv_sec);
1372                 CP(s32[0], s[0], tv_usec);
1373                 CP(s32[1], s[1], tv_sec);
1374                 CP(s32[1], s[1], tv_usec);
1375                 sp = s;
1376         } else
1377                 sp = NULL;
1378         return (kern_futimes(td, uap->fd, sp, UIO_SYSSPACE));
1379 }
1380
1381 int
1382 freebsd32_futimesat(struct thread *td, struct freebsd32_futimesat_args *uap)
1383 {
1384         struct timeval32 s32[2];
1385         struct timeval s[2], *sp;
1386         int error;
1387
1388         if (uap->times != NULL) {
1389                 error = copyin(uap->times, s32, sizeof(s32));
1390                 if (error)
1391                         return (error);
1392                 CP(s32[0], s[0], tv_sec);
1393                 CP(s32[0], s[0], tv_usec);
1394                 CP(s32[1], s[1], tv_sec);
1395                 CP(s32[1], s[1], tv_usec);
1396                 sp = s;
1397         } else
1398                 sp = NULL;
1399         return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
1400                 sp, UIO_SYSSPACE));
1401 }
1402
1403 int
1404 freebsd32_futimens(struct thread *td, struct freebsd32_futimens_args *uap)
1405 {
1406         struct timespec32 ts32[2];
1407         struct timespec ts[2], *tsp;
1408         int error;
1409
1410         if (uap->times != NULL) {
1411                 error = copyin(uap->times, ts32, sizeof(ts32));
1412                 if (error)
1413                         return (error);
1414                 CP(ts32[0], ts[0], tv_sec);
1415                 CP(ts32[0], ts[0], tv_nsec);
1416                 CP(ts32[1], ts[1], tv_sec);
1417                 CP(ts32[1], ts[1], tv_nsec);
1418                 tsp = ts;
1419         } else
1420                 tsp = NULL;
1421         return (kern_futimens(td, uap->fd, tsp, UIO_SYSSPACE));
1422 }
1423
1424 int
1425 freebsd32_utimensat(struct thread *td, struct freebsd32_utimensat_args *uap)
1426 {
1427         struct timespec32 ts32[2];
1428         struct timespec ts[2], *tsp;
1429         int error;
1430
1431         if (uap->times != NULL) {
1432                 error = copyin(uap->times, ts32, sizeof(ts32));
1433                 if (error)
1434                         return (error);
1435                 CP(ts32[0], ts[0], tv_sec);
1436                 CP(ts32[0], ts[0], tv_nsec);
1437                 CP(ts32[1], ts[1], tv_sec);
1438                 CP(ts32[1], ts[1], tv_nsec);
1439                 tsp = ts;
1440         } else
1441                 tsp = NULL;
1442         return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
1443             tsp, UIO_SYSSPACE, uap->flag));
1444 }
1445
1446 int
1447 freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap)
1448 {
1449         struct timeval32 tv32;
1450         struct timeval delta, olddelta, *deltap;
1451         int error;
1452
1453         if (uap->delta) {
1454                 error = copyin(uap->delta, &tv32, sizeof(tv32));
1455                 if (error)
1456                         return (error);
1457                 CP(tv32, delta, tv_sec);
1458                 CP(tv32, delta, tv_usec);
1459                 deltap = &delta;
1460         } else
1461                 deltap = NULL;
1462         error = kern_adjtime(td, deltap, &olddelta);
1463         if (uap->olddelta && error == 0) {
1464                 CP(olddelta, tv32, tv_sec);
1465                 CP(olddelta, tv32, tv_usec);
1466                 error = copyout(&tv32, uap->olddelta, sizeof(tv32));
1467         }
1468         return (error);
1469 }
1470
1471 #ifdef COMPAT_FREEBSD4
1472 int
1473 freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap)
1474 {
1475         struct statfs32 s32;
1476         struct statfs *sp;
1477         int error;
1478
1479         sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1480         error = kern_statfs(td, uap->path, UIO_USERSPACE, sp);
1481         if (error == 0) {
1482                 copy_statfs(sp, &s32);
1483                 error = copyout(&s32, uap->buf, sizeof(s32));
1484         }
1485         free(sp, M_STATFS);
1486         return (error);
1487 }
1488 #endif
1489
1490 #ifdef COMPAT_FREEBSD4
1491 int
1492 freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap)
1493 {
1494         struct statfs32 s32;
1495         struct statfs *sp;
1496         int error;
1497
1498         sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1499         error = kern_fstatfs(td, uap->fd, sp);
1500         if (error == 0) {
1501                 copy_statfs(sp, &s32);
1502                 error = copyout(&s32, uap->buf, sizeof(s32));
1503         }
1504         free(sp, M_STATFS);
1505         return (error);
1506 }
1507 #endif
1508
1509 #ifdef COMPAT_FREEBSD4
1510 int
1511 freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap)
1512 {
1513         struct statfs32 s32;
1514         struct statfs *sp;
1515         fhandle_t fh;
1516         int error;
1517
1518         if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
1519                 return (error);
1520         sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1521         error = kern_fhstatfs(td, fh, sp);
1522         if (error == 0) {
1523                 copy_statfs(sp, &s32);
1524                 error = copyout(&s32, uap->buf, sizeof(s32));
1525         }
1526         free(sp, M_STATFS);
1527         return (error);
1528 }
1529 #endif
1530
1531 int
1532 freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap)
1533 {
1534
1535         return (kern_pread(td, uap->fd, uap->buf, uap->nbyte,
1536             PAIR32TO64(off_t, uap->offset)));
1537 }
1538
1539 int
1540 freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap)
1541 {
1542
1543         return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte,
1544             PAIR32TO64(off_t, uap->offset)));
1545 }
1546
1547 #ifdef COMPAT_43
1548 int
1549 ofreebsd32_lseek(struct thread *td, struct ofreebsd32_lseek_args *uap)
1550 {
1551
1552         return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1553 }
1554 #endif
1555
1556 int
1557 freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap)
1558 {
1559         int error;
1560         off_t pos;
1561
1562         error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset),
1563             uap->whence);
1564         /* Expand the quad return into two parts for eax and edx */
1565         pos = td->td_uretoff.tdu_off;
1566         td->td_retval[RETVAL_LO] = pos & 0xffffffff;    /* %eax */
1567         td->td_retval[RETVAL_HI] = pos >> 32;           /* %edx */
1568         return error;
1569 }
1570
1571 int
1572 freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap)
1573 {
1574
1575         return (kern_truncate(td, uap->path, UIO_USERSPACE,
1576             PAIR32TO64(off_t, uap->length)));
1577 }
1578
1579 int
1580 freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap)
1581 {
1582
1583         return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length)));
1584 }
1585
1586 #ifdef COMPAT_43
1587 int
1588 ofreebsd32_getdirentries(struct thread *td,
1589     struct ofreebsd32_getdirentries_args *uap)
1590 {
1591         struct ogetdirentries_args ap;
1592         int error;
1593         long loff;
1594         int32_t loff_cut;
1595
1596         ap.fd = uap->fd;
1597         ap.buf = uap->buf;
1598         ap.count = uap->count;
1599         ap.basep = NULL;
1600         error = kern_ogetdirentries(td, &ap, &loff);
1601         if (error == 0) {
1602                 loff_cut = loff;
1603                 error = copyout(&loff_cut, uap->basep, sizeof(int32_t));
1604         }
1605         return (error);
1606 }
1607 #endif
1608
1609 int
1610 freebsd32_getdirentries(struct thread *td,
1611     struct freebsd32_getdirentries_args *uap)
1612 {
1613         long base;
1614         int32_t base32;
1615         int error;
1616
1617         error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
1618             NULL, UIO_USERSPACE);
1619         if (error)
1620                 return (error);
1621         if (uap->basep != NULL) {
1622                 base32 = base;
1623                 error = copyout(&base32, uap->basep, sizeof(int32_t));
1624         }
1625         return (error);
1626 }
1627
1628 #ifdef COMPAT_FREEBSD6
1629 /* versions with the 'int pad' argument */
1630 int
1631 freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap)
1632 {
1633
1634         return (kern_pread(td, uap->fd, uap->buf, uap->nbyte,
1635             PAIR32TO64(off_t, uap->offset)));
1636 }
1637
1638 int
1639 freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap)
1640 {
1641
1642         return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte,
1643             PAIR32TO64(off_t, uap->offset)));
1644 }
1645
1646 int
1647 freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap)
1648 {
1649         int error;
1650         off_t pos;
1651
1652         error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset),
1653             uap->whence);
1654         /* Expand the quad return into two parts for eax and edx */
1655         pos = *(off_t *)(td->td_retval);
1656         td->td_retval[RETVAL_LO] = pos & 0xffffffff;    /* %eax */
1657         td->td_retval[RETVAL_HI] = pos >> 32;           /* %edx */
1658         return error;
1659 }
1660
1661 int
1662 freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap)
1663 {
1664
1665         return (kern_truncate(td, uap->path, UIO_USERSPACE,
1666             PAIR32TO64(off_t, uap->length)));
1667 }
1668
1669 int
1670 freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap)
1671 {
1672
1673         return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length)));
1674 }
1675 #endif /* COMPAT_FREEBSD6 */
1676
1677 struct sf_hdtr32 {
1678         uint32_t headers;
1679         int hdr_cnt;
1680         uint32_t trailers;
1681         int trl_cnt;
1682 };
1683
1684 static int
1685 freebsd32_do_sendfile(struct thread *td,
1686     struct freebsd32_sendfile_args *uap, int compat)
1687 {
1688         struct sf_hdtr32 hdtr32;
1689         struct sf_hdtr hdtr;
1690         struct uio *hdr_uio, *trl_uio;
1691         struct file *fp;
1692         cap_rights_t rights;
1693         struct iovec32 *iov32;
1694         off_t offset, sbytes;
1695         int error;
1696
1697         offset = PAIR32TO64(off_t, uap->offset);
1698         if (offset < 0)
1699                 return (EINVAL);
1700
1701         hdr_uio = trl_uio = NULL;
1702
1703         if (uap->hdtr != NULL) {
1704                 error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
1705                 if (error)
1706                         goto out;
1707                 PTRIN_CP(hdtr32, hdtr, headers);
1708                 CP(hdtr32, hdtr, hdr_cnt);
1709                 PTRIN_CP(hdtr32, hdtr, trailers);
1710                 CP(hdtr32, hdtr, trl_cnt);
1711
1712                 if (hdtr.headers != NULL) {
1713                         iov32 = PTRIN(hdtr32.headers);
1714                         error = freebsd32_copyinuio(iov32,
1715                             hdtr32.hdr_cnt, &hdr_uio);
1716                         if (error)
1717                                 goto out;
1718 #ifdef COMPAT_FREEBSD4
1719                         /*
1720                          * In FreeBSD < 5.0 the nbytes to send also included
1721                          * the header.  If compat is specified subtract the
1722                          * header size from nbytes.
1723                          */
1724                         if (compat) {
1725                                 if (uap->nbytes > hdr_uio->uio_resid)
1726                                         uap->nbytes -= hdr_uio->uio_resid;
1727                                 else
1728                                         uap->nbytes = 0;
1729                         }
1730 #endif
1731                 }
1732                 if (hdtr.trailers != NULL) {
1733                         iov32 = PTRIN(hdtr32.trailers);
1734                         error = freebsd32_copyinuio(iov32,
1735                             hdtr32.trl_cnt, &trl_uio);
1736                         if (error)
1737                                 goto out;
1738                 }
1739         }
1740
1741         AUDIT_ARG_FD(uap->fd);
1742
1743         if ((error = fget_read(td, uap->fd,
1744             cap_rights_init(&rights, CAP_PREAD), &fp)) != 0)
1745                 goto out;
1746
1747         error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, offset,
1748             uap->nbytes, &sbytes, uap->flags, td);
1749         fdrop(fp, td);
1750
1751         if (uap->sbytes != NULL)
1752                 copyout(&sbytes, uap->sbytes, sizeof(off_t));
1753
1754 out:
1755         if (hdr_uio)
1756                 free(hdr_uio, M_IOV);
1757         if (trl_uio)
1758                 free(trl_uio, M_IOV);
1759         return (error);
1760 }
1761
1762 #ifdef COMPAT_FREEBSD4
1763 int
1764 freebsd4_freebsd32_sendfile(struct thread *td,
1765     struct freebsd4_freebsd32_sendfile_args *uap)
1766 {
1767         return (freebsd32_do_sendfile(td,
1768             (struct freebsd32_sendfile_args *)uap, 1));
1769 }
1770 #endif
1771
1772 int
1773 freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
1774 {
1775
1776         return (freebsd32_do_sendfile(td, uap, 0));
1777 }
1778
1779 static void
1780 copy_stat(struct stat *in, struct stat32 *out)
1781 {
1782
1783         CP(*in, *out, st_dev);
1784         CP(*in, *out, st_ino);
1785         CP(*in, *out, st_mode);
1786         CP(*in, *out, st_nlink);
1787         CP(*in, *out, st_uid);
1788         CP(*in, *out, st_gid);
1789         CP(*in, *out, st_rdev);
1790         TS_CP(*in, *out, st_atim);
1791         TS_CP(*in, *out, st_mtim);
1792         TS_CP(*in, *out, st_ctim);
1793         CP(*in, *out, st_size);
1794         CP(*in, *out, st_blocks);
1795         CP(*in, *out, st_blksize);
1796         CP(*in, *out, st_flags);
1797         CP(*in, *out, st_gen);
1798         TS_CP(*in, *out, st_birthtim);
1799 }
1800
1801 #ifdef COMPAT_43
1802 static void
1803 copy_ostat(struct stat *in, struct ostat32 *out)
1804 {
1805
1806         CP(*in, *out, st_dev);
1807         CP(*in, *out, st_ino);
1808         CP(*in, *out, st_mode);
1809         CP(*in, *out, st_nlink);
1810         CP(*in, *out, st_uid);
1811         CP(*in, *out, st_gid);
1812         CP(*in, *out, st_rdev);
1813         CP(*in, *out, st_size);
1814         TS_CP(*in, *out, st_atim);
1815         TS_CP(*in, *out, st_mtim);
1816         TS_CP(*in, *out, st_ctim);
1817         CP(*in, *out, st_blksize);
1818         CP(*in, *out, st_blocks);
1819         CP(*in, *out, st_flags);
1820         CP(*in, *out, st_gen);
1821 }
1822 #endif
1823
1824 int
1825 freebsd32_stat(struct thread *td, struct freebsd32_stat_args *uap)
1826 {
1827         struct stat sb;
1828         struct stat32 sb32;
1829         int error;
1830
1831         error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
1832             &sb, NULL);
1833         if (error)
1834                 return (error);
1835         copy_stat(&sb, &sb32);
1836         error = copyout(&sb32, uap->ub, sizeof (sb32));
1837         return (error);
1838 }
1839
1840 #ifdef COMPAT_43
1841 int
1842 ofreebsd32_stat(struct thread *td, struct ofreebsd32_stat_args *uap)
1843 {
1844         struct stat sb;
1845         struct ostat32 sb32;
1846         int error;
1847
1848         error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
1849             &sb, NULL);
1850         if (error)
1851                 return (error);
1852         copy_ostat(&sb, &sb32);
1853         error = copyout(&sb32, uap->ub, sizeof (sb32));
1854         return (error);
1855 }
1856 #endif
1857
1858 int
1859 freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
1860 {
1861         struct stat ub;
1862         struct stat32 ub32;
1863         int error;
1864
1865         error = kern_fstat(td, uap->fd, &ub);
1866         if (error)
1867                 return (error);
1868         copy_stat(&ub, &ub32);
1869         error = copyout(&ub32, uap->ub, sizeof(ub32));
1870         return (error);
1871 }
1872
1873 #ifdef COMPAT_43
1874 int
1875 ofreebsd32_fstat(struct thread *td, struct ofreebsd32_fstat_args *uap)
1876 {
1877         struct stat ub;
1878         struct ostat32 ub32;
1879         int error;
1880
1881         error = kern_fstat(td, uap->fd, &ub);
1882         if (error)
1883                 return (error);
1884         copy_ostat(&ub, &ub32);
1885         error = copyout(&ub32, uap->ub, sizeof(ub32));
1886         return (error);
1887 }
1888 #endif
1889
1890 int
1891 freebsd32_fstatat(struct thread *td, struct freebsd32_fstatat_args *uap)
1892 {
1893         struct stat ub;
1894         struct stat32 ub32;
1895         int error;
1896
1897         error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE,
1898             &ub, NULL);
1899         if (error)
1900                 return (error);
1901         copy_stat(&ub, &ub32);
1902         error = copyout(&ub32, uap->buf, sizeof(ub32));
1903         return (error);
1904 }
1905
1906 int
1907 freebsd32_lstat(struct thread *td, struct freebsd32_lstat_args *uap)
1908 {
1909         struct stat sb;
1910         struct stat32 sb32;
1911         int error;
1912
1913         error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
1914             UIO_USERSPACE, &sb, NULL);
1915         if (error)
1916                 return (error);
1917         copy_stat(&sb, &sb32);
1918         error = copyout(&sb32, uap->ub, sizeof (sb32));
1919         return (error);
1920 }
1921
1922 #ifdef COMPAT_43
1923 int
1924 ofreebsd32_lstat(struct thread *td, struct ofreebsd32_lstat_args *uap)
1925 {
1926         struct stat sb;
1927         struct ostat32 sb32;
1928         int error;
1929
1930         error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
1931             UIO_USERSPACE, &sb, NULL);
1932         if (error)
1933                 return (error);
1934         copy_ostat(&sb, &sb32);
1935         error = copyout(&sb32, uap->ub, sizeof (sb32));
1936         return (error);
1937 }
1938 #endif
1939
1940 int
1941 freebsd32_sysctl(struct thread *td, struct freebsd32_sysctl_args *uap)
1942 {
1943         int error, name[CTL_MAXNAME];
1944         size_t j, oldlen;
1945         uint32_t tmp;
1946
1947         if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1948                 return (EINVAL);
1949         error = copyin(uap->name, name, uap->namelen * sizeof(int));
1950         if (error)
1951                 return (error);
1952         if (uap->oldlenp) {
1953                 error = fueword32(uap->oldlenp, &tmp);
1954                 oldlen = tmp;
1955         } else {
1956                 oldlen = 0;
1957         }
1958         if (error != 0)
1959                 return (EFAULT);
1960         error = userland_sysctl(td, name, uap->namelen,
1961                 uap->old, &oldlen, 1,
1962                 uap->new, uap->newlen, &j, SCTL_MASK32);
1963         if (error && error != ENOMEM)
1964                 return (error);
1965         if (uap->oldlenp)
1966                 suword32(uap->oldlenp, j);
1967         return (0);
1968 }
1969
1970 int
1971 freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap)
1972 {
1973         uint32_t version;
1974         int error;
1975         struct jail j;
1976
1977         error = copyin(uap->jail, &version, sizeof(uint32_t));
1978         if (error)
1979                 return (error);
1980
1981         switch (version) {
1982         case 0:
1983         {
1984                 /* FreeBSD single IPv4 jails. */
1985                 struct jail32_v0 j32_v0;
1986
1987                 bzero(&j, sizeof(struct jail));
1988                 error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
1989                 if (error)
1990                         return (error);
1991                 CP(j32_v0, j, version);
1992                 PTRIN_CP(j32_v0, j, path);
1993                 PTRIN_CP(j32_v0, j, hostname);
1994                 j.ip4s = htonl(j32_v0.ip_number);       /* jail_v0 is host order */
1995                 break;
1996         }
1997
1998         case 1:
1999                 /*
2000                  * Version 1 was used by multi-IPv4 jail implementations
2001                  * that never made it into the official kernel.
2002                  */
2003                 return (EINVAL);
2004
2005         case 2: /* JAIL_API_VERSION */
2006         {
2007                 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
2008                 struct jail32 j32;
2009
2010                 error = copyin(uap->jail, &j32, sizeof(struct jail32));
2011                 if (error)
2012                         return (error);
2013                 CP(j32, j, version);
2014                 PTRIN_CP(j32, j, path);
2015                 PTRIN_CP(j32, j, hostname);
2016                 PTRIN_CP(j32, j, jailname);
2017                 CP(j32, j, ip4s);
2018                 CP(j32, j, ip6s);
2019                 PTRIN_CP(j32, j, ip4);
2020                 PTRIN_CP(j32, j, ip6);
2021                 break;
2022         }
2023
2024         default:
2025                 /* Sci-Fi jails are not supported, sorry. */
2026                 return (EINVAL);
2027         }
2028         return (kern_jail(td, &j));
2029 }
2030
2031 int
2032 freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap)
2033 {
2034         struct uio *auio;
2035         int error;
2036
2037         /* Check that we have an even number of iovecs. */
2038         if (uap->iovcnt & 1)
2039                 return (EINVAL);
2040
2041         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2042         if (error)
2043                 return (error);
2044         error = kern_jail_set(td, auio, uap->flags);
2045         free(auio, M_IOV);
2046         return (error);
2047 }
2048
2049 int
2050 freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap)
2051 {
2052         struct iovec32 iov32;
2053         struct uio *auio;
2054         int error, i;
2055
2056         /* Check that we have an even number of iovecs. */
2057         if (uap->iovcnt & 1)
2058                 return (EINVAL);
2059
2060         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2061         if (error)
2062                 return (error);
2063         error = kern_jail_get(td, auio, uap->flags);
2064         if (error == 0)
2065                 for (i = 0; i < uap->iovcnt; i++) {
2066                         PTROUT_CP(auio->uio_iov[i], iov32, iov_base);
2067                         CP(auio->uio_iov[i], iov32, iov_len);
2068                         error = copyout(&iov32, uap->iovp + i, sizeof(iov32));
2069                         if (error != 0)
2070                                 break;
2071                 }
2072         free(auio, M_IOV);
2073         return (error);
2074 }
2075
2076 int
2077 freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
2078 {
2079         struct sigaction32 s32;
2080         struct sigaction sa, osa, *sap;
2081         int error;
2082
2083         if (uap->act) {
2084                 error = copyin(uap->act, &s32, sizeof(s32));
2085                 if (error)
2086                         return (error);
2087                 sa.sa_handler = PTRIN(s32.sa_u);
2088                 CP(s32, sa, sa_flags);
2089                 CP(s32, sa, sa_mask);
2090                 sap = &sa;
2091         } else
2092                 sap = NULL;
2093         error = kern_sigaction(td, uap->sig, sap, &osa, 0);
2094         if (error == 0 && uap->oact != NULL) {
2095                 s32.sa_u = PTROUT(osa.sa_handler);
2096                 CP(osa, s32, sa_flags);
2097                 CP(osa, s32, sa_mask);
2098                 error = copyout(&s32, uap->oact, sizeof(s32));
2099         }
2100         return (error);
2101 }
2102
2103 #ifdef COMPAT_FREEBSD4
2104 int
2105 freebsd4_freebsd32_sigaction(struct thread *td,
2106                              struct freebsd4_freebsd32_sigaction_args *uap)
2107 {
2108         struct sigaction32 s32;
2109         struct sigaction sa, osa, *sap;
2110         int error;
2111
2112         if (uap->act) {
2113                 error = copyin(uap->act, &s32, sizeof(s32));
2114                 if (error)
2115                         return (error);
2116                 sa.sa_handler = PTRIN(s32.sa_u);
2117                 CP(s32, sa, sa_flags);
2118                 CP(s32, sa, sa_mask);
2119                 sap = &sa;
2120         } else
2121                 sap = NULL;
2122         error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
2123         if (error == 0 && uap->oact != NULL) {
2124                 s32.sa_u = PTROUT(osa.sa_handler);
2125                 CP(osa, s32, sa_flags);
2126                 CP(osa, s32, sa_mask);
2127                 error = copyout(&s32, uap->oact, sizeof(s32));
2128         }
2129         return (error);
2130 }
2131 #endif
2132
2133 #ifdef COMPAT_43
2134 struct osigaction32 {
2135         u_int32_t       sa_u;
2136         osigset_t       sa_mask;
2137         int             sa_flags;
2138 };
2139
2140 #define ONSIG   32
2141
2142 int
2143 ofreebsd32_sigaction(struct thread *td,
2144                              struct ofreebsd32_sigaction_args *uap)
2145 {
2146         struct osigaction32 s32;
2147         struct sigaction sa, osa, *sap;
2148         int error;
2149
2150         if (uap->signum <= 0 || uap->signum >= ONSIG)
2151                 return (EINVAL);
2152
2153         if (uap->nsa) {
2154                 error = copyin(uap->nsa, &s32, sizeof(s32));
2155                 if (error)
2156                         return (error);
2157                 sa.sa_handler = PTRIN(s32.sa_u);
2158                 CP(s32, sa, sa_flags);
2159                 OSIG2SIG(s32.sa_mask, sa.sa_mask);
2160                 sap = &sa;
2161         } else
2162                 sap = NULL;
2163         error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2164         if (error == 0 && uap->osa != NULL) {
2165                 s32.sa_u = PTROUT(osa.sa_handler);
2166                 CP(osa, s32, sa_flags);
2167                 SIG2OSIG(osa.sa_mask, s32.sa_mask);
2168                 error = copyout(&s32, uap->osa, sizeof(s32));
2169         }
2170         return (error);
2171 }
2172
2173 int
2174 ofreebsd32_sigprocmask(struct thread *td,
2175                                struct ofreebsd32_sigprocmask_args *uap)
2176 {
2177         sigset_t set, oset;
2178         int error;
2179
2180         OSIG2SIG(uap->mask, set);
2181         error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD);
2182         SIG2OSIG(oset, td->td_retval[0]);
2183         return (error);
2184 }
2185
2186 int
2187 ofreebsd32_sigpending(struct thread *td,
2188                               struct ofreebsd32_sigpending_args *uap)
2189 {
2190         struct proc *p = td->td_proc;
2191         sigset_t siglist;
2192
2193         PROC_LOCK(p);
2194         siglist = p->p_siglist;
2195         SIGSETOR(siglist, td->td_siglist);
2196         PROC_UNLOCK(p);
2197         SIG2OSIG(siglist, td->td_retval[0]);
2198         return (0);
2199 }
2200
2201 struct sigvec32 {
2202         u_int32_t       sv_handler;
2203         int             sv_mask;
2204         int             sv_flags;
2205 };
2206
2207 int
2208 ofreebsd32_sigvec(struct thread *td,
2209                           struct ofreebsd32_sigvec_args *uap)
2210 {
2211         struct sigvec32 vec;
2212         struct sigaction sa, osa, *sap;
2213         int error;
2214
2215         if (uap->signum <= 0 || uap->signum >= ONSIG)
2216                 return (EINVAL);
2217
2218         if (uap->nsv) {
2219                 error = copyin(uap->nsv, &vec, sizeof(vec));
2220                 if (error)
2221                         return (error);
2222                 sa.sa_handler = PTRIN(vec.sv_handler);
2223                 OSIG2SIG(vec.sv_mask, sa.sa_mask);
2224                 sa.sa_flags = vec.sv_flags;
2225                 sa.sa_flags ^= SA_RESTART;
2226                 sap = &sa;
2227         } else
2228                 sap = NULL;
2229         error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2230         if (error == 0 && uap->osv != NULL) {
2231                 vec.sv_handler = PTROUT(osa.sa_handler);
2232                 SIG2OSIG(osa.sa_mask, vec.sv_mask);
2233                 vec.sv_flags = osa.sa_flags;
2234                 vec.sv_flags &= ~SA_NOCLDWAIT;
2235                 vec.sv_flags ^= SA_RESTART;
2236                 error = copyout(&vec, uap->osv, sizeof(vec));
2237         }
2238         return (error);
2239 }
2240
2241 int
2242 ofreebsd32_sigblock(struct thread *td,
2243                             struct ofreebsd32_sigblock_args *uap)
2244 {
2245         sigset_t set, oset;
2246
2247         OSIG2SIG(uap->mask, set);
2248         kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
2249         SIG2OSIG(oset, td->td_retval[0]);
2250         return (0);
2251 }
2252
2253 int
2254 ofreebsd32_sigsetmask(struct thread *td,
2255                               struct ofreebsd32_sigsetmask_args *uap)
2256 {
2257         sigset_t set, oset;
2258
2259         OSIG2SIG(uap->mask, set);
2260         kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
2261         SIG2OSIG(oset, td->td_retval[0]);
2262         return (0);
2263 }
2264
2265 int
2266 ofreebsd32_sigsuspend(struct thread *td,
2267                               struct ofreebsd32_sigsuspend_args *uap)
2268 {
2269         sigset_t mask;
2270
2271         OSIG2SIG(uap->mask, mask);
2272         return (kern_sigsuspend(td, mask));
2273 }
2274
2275 struct sigstack32 {
2276         u_int32_t       ss_sp;
2277         int             ss_onstack;
2278 };
2279
2280 int
2281 ofreebsd32_sigstack(struct thread *td,
2282                             struct ofreebsd32_sigstack_args *uap)
2283 {
2284         struct sigstack32 s32;
2285         struct sigstack nss, oss;
2286         int error = 0, unss;
2287
2288         if (uap->nss != NULL) {
2289                 error = copyin(uap->nss, &s32, sizeof(s32));
2290                 if (error)
2291                         return (error);
2292                 nss.ss_sp = PTRIN(s32.ss_sp);
2293                 CP(s32, nss, ss_onstack);
2294                 unss = 1;
2295         } else {
2296                 unss = 0;
2297         }
2298         oss.ss_sp = td->td_sigstk.ss_sp;
2299         oss.ss_onstack = sigonstack(cpu_getstack(td));
2300         if (unss) {
2301                 td->td_sigstk.ss_sp = nss.ss_sp;
2302                 td->td_sigstk.ss_size = 0;
2303                 td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2304                 td->td_pflags |= TDP_ALTSTACK;
2305         }
2306         if (uap->oss != NULL) {
2307                 s32.ss_sp = PTROUT(oss.ss_sp);
2308                 CP(oss, s32, ss_onstack);
2309                 error = copyout(&s32, uap->oss, sizeof(s32));
2310         }
2311         return (error);
2312 }
2313 #endif
2314
2315 int
2316 freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2317 {
2318
2319         return (freebsd32_user_clock_nanosleep(td, CLOCK_REALTIME,
2320             TIMER_RELTIME, uap->rqtp, uap->rmtp));
2321 }
2322
2323 int
2324 freebsd32_clock_nanosleep(struct thread *td,
2325     struct freebsd32_clock_nanosleep_args *uap)
2326 {
2327         int error;
2328
2329         error = freebsd32_user_clock_nanosleep(td, uap->clock_id, uap->flags,
2330             uap->rqtp, uap->rmtp);
2331         return (kern_posix_error(td, error));
2332 }
2333
2334 static int
2335 freebsd32_user_clock_nanosleep(struct thread *td, clockid_t clock_id,
2336     int flags, const struct timespec32 *ua_rqtp, struct timespec32 *ua_rmtp)
2337 {
2338         struct timespec32 rmt32, rqt32;
2339         struct timespec rmt, rqt;
2340         int error;
2341
2342         error = copyin(ua_rqtp, &rqt32, sizeof(rqt32));
2343         if (error)
2344                 return (error);
2345
2346         CP(rqt32, rqt, tv_sec);
2347         CP(rqt32, rqt, tv_nsec);
2348
2349         if (ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0 &&
2350             !useracc(ua_rmtp, sizeof(rmt32), VM_PROT_WRITE))
2351                 return (EFAULT);
2352         error = kern_clock_nanosleep(td, clock_id, flags, &rqt, &rmt);
2353         if (error == EINTR && ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0) {
2354                 int error2;
2355
2356                 CP(rmt, rmt32, tv_sec);
2357                 CP(rmt, rmt32, tv_nsec);
2358
2359                 error2 = copyout(&rmt32, ua_rmtp, sizeof(rmt32));
2360                 if (error2)
2361                         error = error2;
2362         }
2363         return (error);
2364 }
2365
2366 int
2367 freebsd32_clock_gettime(struct thread *td,
2368                         struct freebsd32_clock_gettime_args *uap)
2369 {
2370         struct timespec ats;
2371         struct timespec32 ats32;
2372         int error;
2373
2374         error = kern_clock_gettime(td, uap->clock_id, &ats);
2375         if (error == 0) {
2376                 CP(ats, ats32, tv_sec);
2377                 CP(ats, ats32, tv_nsec);
2378                 error = copyout(&ats32, uap->tp, sizeof(ats32));
2379         }
2380         return (error);
2381 }
2382
2383 int
2384 freebsd32_clock_settime(struct thread *td,
2385                         struct freebsd32_clock_settime_args *uap)
2386 {
2387         struct timespec ats;
2388         struct timespec32 ats32;
2389         int error;
2390
2391         error = copyin(uap->tp, &ats32, sizeof(ats32));
2392         if (error)
2393                 return (error);
2394         CP(ats32, ats, tv_sec);
2395         CP(ats32, ats, tv_nsec);
2396
2397         return (kern_clock_settime(td, uap->clock_id, &ats));
2398 }
2399
2400 int
2401 freebsd32_clock_getres(struct thread *td,
2402                        struct freebsd32_clock_getres_args *uap)
2403 {
2404         struct timespec ts;
2405         struct timespec32 ts32;
2406         int error;
2407
2408         if (uap->tp == NULL)
2409                 return (0);
2410         error = kern_clock_getres(td, uap->clock_id, &ts);
2411         if (error == 0) {
2412                 CP(ts, ts32, tv_sec);
2413                 CP(ts, ts32, tv_nsec);
2414                 error = copyout(&ts32, uap->tp, sizeof(ts32));
2415         }
2416         return (error);
2417 }
2418
2419 int freebsd32_ktimer_create(struct thread *td,
2420     struct freebsd32_ktimer_create_args *uap)
2421 {
2422         struct sigevent32 ev32;
2423         struct sigevent ev, *evp;
2424         int error, id;
2425
2426         if (uap->evp == NULL) {
2427                 evp = NULL;
2428         } else {
2429                 evp = &ev;
2430                 error = copyin(uap->evp, &ev32, sizeof(ev32));
2431                 if (error != 0)
2432                         return (error);
2433                 error = convert_sigevent32(&ev32, &ev);
2434                 if (error != 0)
2435                         return (error);
2436         }
2437         error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
2438         if (error == 0) {
2439                 error = copyout(&id, uap->timerid, sizeof(int));
2440                 if (error != 0)
2441                         kern_ktimer_delete(td, id);
2442         }
2443         return (error);
2444 }
2445
2446 int
2447 freebsd32_ktimer_settime(struct thread *td,
2448     struct freebsd32_ktimer_settime_args *uap)
2449 {
2450         struct itimerspec32 val32, oval32;
2451         struct itimerspec val, oval, *ovalp;
2452         int error;
2453
2454         error = copyin(uap->value, &val32, sizeof(val32));
2455         if (error != 0)
2456                 return (error);
2457         ITS_CP(val32, val);
2458         ovalp = uap->ovalue != NULL ? &oval : NULL;
2459         error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
2460         if (error == 0 && uap->ovalue != NULL) {
2461                 ITS_CP(oval, oval32);
2462                 error = copyout(&oval32, uap->ovalue, sizeof(oval32));
2463         }
2464         return (error);
2465 }
2466
2467 int
2468 freebsd32_ktimer_gettime(struct thread *td,
2469     struct freebsd32_ktimer_gettime_args *uap)
2470 {
2471         struct itimerspec32 val32;
2472         struct itimerspec val;
2473         int error;
2474
2475         error = kern_ktimer_gettime(td, uap->timerid, &val);
2476         if (error == 0) {
2477                 ITS_CP(val, val32);
2478                 error = copyout(&val32, uap->value, sizeof(val32));
2479         }
2480         return (error);
2481 }
2482
2483 int
2484 freebsd32_clock_getcpuclockid2(struct thread *td,
2485     struct freebsd32_clock_getcpuclockid2_args *uap)
2486 {
2487         clockid_t clk_id;
2488         int error;
2489
2490         error = kern_clock_getcpuclockid2(td, PAIR32TO64(id_t, uap->id),
2491             uap->which, &clk_id);
2492         if (error == 0)
2493                 error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
2494         return (error);
2495 }
2496
2497 int
2498 freebsd32_thr_new(struct thread *td,
2499                   struct freebsd32_thr_new_args *uap)
2500 {
2501         struct thr_param32 param32;
2502         struct thr_param param;
2503         int error;
2504
2505         if (uap->param_size < 0 ||
2506             uap->param_size > sizeof(struct thr_param32))
2507                 return (EINVAL);
2508         bzero(&param, sizeof(struct thr_param));
2509         bzero(&param32, sizeof(struct thr_param32));
2510         error = copyin(uap->param, &param32, uap->param_size);
2511         if (error != 0)
2512                 return (error);
2513         param.start_func = PTRIN(param32.start_func);
2514         param.arg = PTRIN(param32.arg);
2515         param.stack_base = PTRIN(param32.stack_base);
2516         param.stack_size = param32.stack_size;
2517         param.tls_base = PTRIN(param32.tls_base);
2518         param.tls_size = param32.tls_size;
2519         param.child_tid = PTRIN(param32.child_tid);
2520         param.parent_tid = PTRIN(param32.parent_tid);
2521         param.flags = param32.flags;
2522         param.rtp = PTRIN(param32.rtp);
2523         param.spare[0] = PTRIN(param32.spare[0]);
2524         param.spare[1] = PTRIN(param32.spare[1]);
2525         param.spare[2] = PTRIN(param32.spare[2]);
2526
2527         return (kern_thr_new(td, &param));
2528 }
2529
2530 int
2531 freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
2532 {
2533         struct timespec32 ts32;
2534         struct timespec ts, *tsp;
2535         int error;
2536
2537         error = 0;
2538         tsp = NULL;
2539         if (uap->timeout != NULL) {
2540                 error = copyin((const void *)uap->timeout, (void *)&ts32,
2541                     sizeof(struct timespec32));
2542                 if (error != 0)
2543                         return (error);
2544                 ts.tv_sec = ts32.tv_sec;
2545                 ts.tv_nsec = ts32.tv_nsec;
2546                 tsp = &ts;
2547         }
2548         return (kern_thr_suspend(td, tsp));
2549 }
2550
2551 void
2552 siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst)
2553 {
2554         bzero(dst, sizeof(*dst));
2555         dst->si_signo = src->si_signo;
2556         dst->si_errno = src->si_errno;
2557         dst->si_code = src->si_code;
2558         dst->si_pid = src->si_pid;
2559         dst->si_uid = src->si_uid;
2560         dst->si_status = src->si_status;
2561         dst->si_addr = (uintptr_t)src->si_addr;
2562         dst->si_value.sival_int = src->si_value.sival_int;
2563         dst->si_timerid = src->si_timerid;
2564         dst->si_overrun = src->si_overrun;
2565 }
2566
2567 #ifndef _FREEBSD32_SYSPROTO_H_
2568 struct freebsd32_sigqueue_args {
2569         pid_t pid;
2570         int signum;
2571         /* union sigval32 */ int value;
2572 };
2573 #endif
2574 int
2575 freebsd32_sigqueue(struct thread *td, struct freebsd32_sigqueue_args *uap)
2576 {
2577         union sigval sv;
2578
2579         /*
2580          * On 32-bit ABIs, sival_int and sival_ptr are the same.
2581          * On 64-bit little-endian ABIs, the low bits are the same.
2582          * In 64-bit big-endian ABIs, sival_int overlaps with
2583          * sival_ptr's HIGH bits.  We choose to support sival_int
2584          * rather than sival_ptr in this case as it seems to be
2585          * more common.
2586          */
2587         bzero(&sv, sizeof(sv));
2588         sv.sival_int = uap->value;
2589
2590         return (kern_sigqueue(td, uap->pid, uap->signum, &sv));
2591 }
2592
2593 int
2594 freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
2595 {
2596         struct timespec32 ts32;
2597         struct timespec ts;
2598         struct timespec *timeout;
2599         sigset_t set;
2600         ksiginfo_t ksi;
2601         struct siginfo32 si32;
2602         int error;
2603
2604         if (uap->timeout) {
2605                 error = copyin(uap->timeout, &ts32, sizeof(ts32));
2606                 if (error)
2607                         return (error);
2608                 ts.tv_sec = ts32.tv_sec;
2609                 ts.tv_nsec = ts32.tv_nsec;
2610                 timeout = &ts;
2611         } else
2612                 timeout = NULL;
2613
2614         error = copyin(uap->set, &set, sizeof(set));
2615         if (error)
2616                 return (error);
2617
2618         error = kern_sigtimedwait(td, set, &ksi, timeout);
2619         if (error)
2620                 return (error);
2621
2622         if (uap->info) {
2623                 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2624                 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2625         }
2626
2627         if (error == 0)
2628                 td->td_retval[0] = ksi.ksi_signo;
2629         return (error);
2630 }
2631
2632 /*
2633  * MPSAFE
2634  */
2635 int
2636 freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
2637 {
2638         ksiginfo_t ksi;
2639         struct siginfo32 si32;
2640         sigset_t set;
2641         int error;
2642
2643         error = copyin(uap->set, &set, sizeof(set));
2644         if (error)
2645                 return (error);
2646
2647         error = kern_sigtimedwait(td, set, &ksi, NULL);
2648         if (error)
2649                 return (error);
2650
2651         if (uap->info) {
2652                 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2653                 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2654         }       
2655         if (error == 0)
2656                 td->td_retval[0] = ksi.ksi_signo;
2657         return (error);
2658 }
2659
2660 int
2661 freebsd32_cpuset_setid(struct thread *td,
2662     struct freebsd32_cpuset_setid_args *uap)
2663 {
2664
2665         return (kern_cpuset_setid(td, uap->which,
2666             PAIR32TO64(id_t, uap->id), uap->setid));
2667 }
2668
2669 int
2670 freebsd32_cpuset_getid(struct thread *td,
2671     struct freebsd32_cpuset_getid_args *uap)
2672 {
2673
2674         return (kern_cpuset_getid(td, uap->level, uap->which,
2675             PAIR32TO64(id_t, uap->id), uap->setid));
2676 }
2677
2678 int
2679 freebsd32_cpuset_getaffinity(struct thread *td,
2680     struct freebsd32_cpuset_getaffinity_args *uap)
2681 {
2682
2683         return (kern_cpuset_getaffinity(td, uap->level, uap->which,
2684             PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask));
2685 }
2686
2687 int
2688 freebsd32_cpuset_setaffinity(struct thread *td,
2689     struct freebsd32_cpuset_setaffinity_args *uap)
2690 {
2691
2692         return (kern_cpuset_setaffinity(td, uap->level, uap->which,
2693             PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask));
2694 }
2695
2696 int
2697 freebsd32_nmount(struct thread *td,
2698     struct freebsd32_nmount_args /* {
2699         struct iovec *iovp;
2700         unsigned int iovcnt;
2701         int flags;
2702     } */ *uap)
2703 {
2704         struct uio *auio;
2705         uint64_t flags;
2706         int error;
2707
2708         /*
2709          * Mount flags are now 64-bits. On 32-bit archtectures only
2710          * 32-bits are passed in, but from here on everything handles
2711          * 64-bit flags correctly.
2712          */
2713         flags = uap->flags;
2714
2715         AUDIT_ARG_FFLAGS(flags);
2716
2717         /*
2718          * Filter out MNT_ROOTFS.  We do not want clients of nmount() in
2719          * userspace to set this flag, but we must filter it out if we want
2720          * MNT_UPDATE on the root file system to work.
2721          * MNT_ROOTFS should only be set by the kernel when mounting its
2722          * root file system.
2723          */
2724         flags &= ~MNT_ROOTFS;
2725
2726         /*
2727          * check that we have an even number of iovec's
2728          * and that we have at least two options.
2729          */
2730         if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
2731                 return (EINVAL);
2732
2733         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2734         if (error)
2735                 return (error);
2736         error = vfs_donmount(td, flags, auio);
2737
2738         free(auio, M_IOV);
2739         return error;
2740 }
2741
2742 #if 0
2743 int
2744 freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
2745 {
2746         struct yyy32 *p32, s32;
2747         struct yyy *p = NULL, s;
2748         struct xxx_arg ap;
2749         int error;
2750
2751         if (uap->zzz) {
2752                 error = copyin(uap->zzz, &s32, sizeof(s32));
2753                 if (error)
2754                         return (error);
2755                 /* translate in */
2756                 p = &s;
2757         }
2758         error = kern_xxx(td, p);
2759         if (error)
2760                 return (error);
2761         if (uap->zzz) {
2762                 /* translate out */
2763                 error = copyout(&s32, p32, sizeof(s32));
2764         }
2765         return (error);
2766 }
2767 #endif
2768
2769 int
2770 syscall32_register(int *offset, struct sysent *new_sysent,
2771     struct sysent *old_sysent, int flags)
2772 {
2773
2774         if ((flags & ~SY_THR_STATIC) != 0)
2775                 return (EINVAL);
2776
2777         if (*offset == NO_SYSCALL) {
2778                 int i;
2779
2780                 for (i = 1; i < SYS_MAXSYSCALL; ++i)
2781                         if (freebsd32_sysent[i].sy_call ==
2782                             (sy_call_t *)lkmnosys)
2783                                 break;
2784                 if (i == SYS_MAXSYSCALL)
2785                         return (ENFILE);
2786                 *offset = i;
2787         } else if (*offset < 0 || *offset >= SYS_MAXSYSCALL)
2788                 return (EINVAL);
2789         else if (freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmnosys &&
2790             freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmressys)
2791                 return (EEXIST);
2792
2793         *old_sysent = freebsd32_sysent[*offset];
2794         freebsd32_sysent[*offset] = *new_sysent;
2795         atomic_store_rel_32(&freebsd32_sysent[*offset].sy_thrcnt, flags);
2796         return (0);
2797 }
2798
2799 int
2800 syscall32_deregister(int *offset, struct sysent *old_sysent)
2801 {
2802
2803         if (*offset == 0)
2804                 return (0);
2805
2806         freebsd32_sysent[*offset] = *old_sysent;
2807         return (0);
2808 }
2809
2810 int
2811 syscall32_module_handler(struct module *mod, int what, void *arg)
2812 {
2813         struct syscall_module_data *data = (struct syscall_module_data*)arg;
2814         modspecific_t ms;
2815         int error;
2816
2817         switch (what) {
2818         case MOD_LOAD:
2819                 error = syscall32_register(data->offset, data->new_sysent,
2820                     &data->old_sysent, SY_THR_STATIC_KLD);
2821                 if (error) {
2822                         /* Leave a mark so we know to safely unload below. */
2823                         data->offset = NULL;
2824                         return error;
2825                 }
2826                 ms.intval = *data->offset;
2827                 MOD_XLOCK;
2828                 module_setspecific(mod, &ms);
2829                 MOD_XUNLOCK;
2830                 if (data->chainevh)
2831                         error = data->chainevh(mod, what, data->chainarg);
2832                 return (error);
2833         case MOD_UNLOAD:
2834                 /*
2835                  * MOD_LOAD failed, so just return without calling the
2836                  * chained handler since we didn't pass along the MOD_LOAD
2837                  * event.
2838                  */
2839                 if (data->offset == NULL)
2840                         return (0);
2841                 if (data->chainevh) {
2842                         error = data->chainevh(mod, what, data->chainarg);
2843                         if (error)
2844                                 return (error);
2845                 }
2846                 error = syscall32_deregister(data->offset, &data->old_sysent);
2847                 return (error);
2848         default:
2849                 error = EOPNOTSUPP;
2850                 if (data->chainevh)
2851                         error = data->chainevh(mod, what, data->chainarg);
2852                 return (error);
2853         }
2854 }
2855
2856 int
2857 syscall32_helper_register(struct syscall_helper_data *sd, int flags)
2858 {
2859         struct syscall_helper_data *sd1;
2860         int error;
2861
2862         for (sd1 = sd; sd1->syscall_no != NO_SYSCALL; sd1++) {
2863                 error = syscall32_register(&sd1->syscall_no, &sd1->new_sysent,
2864                     &sd1->old_sysent, flags);
2865                 if (error != 0) {
2866                         syscall32_helper_unregister(sd);
2867                         return (error);
2868                 }
2869                 sd1->registered = 1;
2870         }
2871         return (0);
2872 }
2873
2874 int
2875 syscall32_helper_unregister(struct syscall_helper_data *sd)
2876 {
2877         struct syscall_helper_data *sd1;
2878
2879         for (sd1 = sd; sd1->registered != 0; sd1++) {
2880                 syscall32_deregister(&sd1->syscall_no, &sd1->old_sysent);
2881                 sd1->registered = 0;
2882         }
2883         return (0);
2884 }
2885
2886 register_t *
2887 freebsd32_copyout_strings(struct image_params *imgp)
2888 {
2889         int argc, envc, i;
2890         u_int32_t *vectp;
2891         char *stringp;
2892         uintptr_t destp;
2893         u_int32_t *stack_base;
2894         struct freebsd32_ps_strings *arginfo;
2895         char canary[sizeof(long) * 8];
2896         int32_t pagesizes32[MAXPAGESIZES];
2897         size_t execpath_len;
2898         int szsigcode;
2899
2900         /*
2901          * Calculate string base and vector table pointers.
2902          * Also deal with signal trampoline code for this exec type.
2903          */
2904         if (imgp->execpath != NULL && imgp->auxargs != NULL)
2905                 execpath_len = strlen(imgp->execpath) + 1;
2906         else
2907                 execpath_len = 0;
2908         arginfo = (struct freebsd32_ps_strings *)curproc->p_sysent->
2909             sv_psstrings;
2910         if (imgp->proc->p_sysent->sv_sigcode_base == 0)
2911                 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
2912         else
2913                 szsigcode = 0;
2914         destp = (uintptr_t)arginfo;
2915
2916         /*
2917          * install sigcode
2918          */
2919         if (szsigcode != 0) {
2920                 destp -= szsigcode;
2921                 destp = rounddown2(destp, sizeof(uint32_t));
2922                 copyout(imgp->proc->p_sysent->sv_sigcode, (void *)destp,
2923                     szsigcode);
2924         }
2925
2926         /*
2927          * Copy the image path for the rtld.
2928          */
2929         if (execpath_len != 0) {
2930                 destp -= execpath_len;
2931                 imgp->execpathp = destp;
2932                 copyout(imgp->execpath, (void *)destp, execpath_len);
2933         }
2934
2935         /*
2936          * Prepare the canary for SSP.
2937          */
2938         arc4rand(canary, sizeof(canary), 0);
2939         destp -= sizeof(canary);
2940         imgp->canary = destp;
2941         copyout(canary, (void *)destp, sizeof(canary));
2942         imgp->canarylen = sizeof(canary);
2943
2944         /*
2945          * Prepare the pagesizes array.
2946          */
2947         for (i = 0; i < MAXPAGESIZES; i++)
2948                 pagesizes32[i] = (uint32_t)pagesizes[i];
2949         destp -= sizeof(pagesizes32);
2950         destp = rounddown2(destp, sizeof(uint32_t));
2951         imgp->pagesizes = destp;
2952         copyout(pagesizes32, (void *)destp, sizeof(pagesizes32));
2953         imgp->pagesizeslen = sizeof(pagesizes32);
2954
2955         destp -= ARG_MAX - imgp->args->stringspace;
2956         destp = rounddown2(destp, sizeof(uint32_t));
2957
2958         vectp = (uint32_t *)destp;
2959         if (imgp->auxargs) {
2960                 /*
2961                  * Allocate room on the stack for the ELF auxargs
2962                  * array.  It has up to AT_COUNT entries.
2963                  */
2964                 vectp -= howmany(AT_COUNT * sizeof(Elf32_Auxinfo),
2965                     sizeof(*vectp));
2966         }
2967
2968         /*
2969          * Allocate room for the argv[] and env vectors including the
2970          * terminating NULL pointers.
2971          */
2972         vectp -= imgp->args->argc + 1 + imgp->args->envc + 1;
2973
2974         /*
2975          * vectp also becomes our initial stack base
2976          */
2977         stack_base = vectp;
2978
2979         stringp = imgp->args->begin_argv;
2980         argc = imgp->args->argc;
2981         envc = imgp->args->envc;
2982         /*
2983          * Copy out strings - arguments and environment.
2984          */
2985         copyout(stringp, (void *)destp, ARG_MAX - imgp->args->stringspace);
2986
2987         /*
2988          * Fill in "ps_strings" struct for ps, w, etc.
2989          */
2990         suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp);
2991         suword32(&arginfo->ps_nargvstr, argc);
2992
2993         /*
2994          * Fill in argument portion of vector table.
2995          */
2996         for (; argc > 0; --argc) {
2997                 suword32(vectp++, (u_int32_t)(intptr_t)destp);
2998                 while (*stringp++ != 0)
2999                         destp++;
3000                 destp++;
3001         }
3002
3003         /* a null vector table pointer separates the argp's from the envp's */
3004         suword32(vectp++, 0);
3005
3006         suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp);
3007         suword32(&arginfo->ps_nenvstr, envc);
3008
3009         /*
3010          * Fill in environment portion of vector table.
3011          */
3012         for (; envc > 0; --envc) {
3013                 suword32(vectp++, (u_int32_t)(intptr_t)destp);
3014                 while (*stringp++ != 0)
3015                         destp++;
3016                 destp++;
3017         }
3018
3019         /* end of vector table is a null pointer */
3020         suword32(vectp, 0);
3021
3022         return ((register_t *)stack_base);
3023 }
3024
3025 int
3026 freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
3027 {
3028         struct kld_file_stat *stat;
3029         struct kld32_file_stat *stat32;
3030         int error, version;
3031
3032         if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
3033             != 0)
3034                 return (error);
3035         if (version != sizeof(struct kld32_file_stat_1) &&
3036             version != sizeof(struct kld32_file_stat))
3037                 return (EINVAL);
3038
3039         stat = malloc(sizeof(*stat), M_TEMP, M_WAITOK | M_ZERO);
3040         stat32 = malloc(sizeof(*stat32), M_TEMP, M_WAITOK | M_ZERO);
3041         error = kern_kldstat(td, uap->fileid, stat);
3042         if (error == 0) {
3043                 bcopy(&stat->name[0], &stat32->name[0], sizeof(stat->name));
3044                 CP(*stat, *stat32, refs);
3045                 CP(*stat, *stat32, id);
3046                 PTROUT_CP(*stat, *stat32, address);
3047                 CP(*stat, *stat32, size);
3048                 bcopy(&stat->pathname[0], &stat32->pathname[0],
3049                     sizeof(stat->pathname));
3050                 stat32->version  = version;
3051                 error = copyout(stat32, uap->stat, version);
3052         }
3053         free(stat, M_TEMP);
3054         free(stat32, M_TEMP);
3055         return (error);
3056 }
3057
3058 int
3059 freebsd32_posix_fallocate(struct thread *td,
3060     struct freebsd32_posix_fallocate_args *uap)
3061 {
3062         int error;
3063
3064         error = kern_posix_fallocate(td, uap->fd,
3065             PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len));
3066         return (kern_posix_error(td, error));
3067 }
3068
3069 int
3070 freebsd32_posix_fadvise(struct thread *td,
3071     struct freebsd32_posix_fadvise_args *uap)
3072 {
3073         int error;
3074
3075         error = kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset),
3076             PAIR32TO64(off_t, uap->len), uap->advice);
3077         return (kern_posix_error(td, error));
3078 }
3079
3080 int
3081 convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig)
3082 {
3083
3084         CP(*sig32, *sig, sigev_notify);
3085         switch (sig->sigev_notify) {
3086         case SIGEV_NONE:
3087                 break;
3088         case SIGEV_THREAD_ID:
3089                 CP(*sig32, *sig, sigev_notify_thread_id);
3090                 /* FALLTHROUGH */
3091         case SIGEV_SIGNAL:
3092                 CP(*sig32, *sig, sigev_signo);
3093                 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3094                 break;
3095         case SIGEV_KEVENT:
3096                 CP(*sig32, *sig, sigev_notify_kqueue);
3097                 CP(*sig32, *sig, sigev_notify_kevent_flags);
3098                 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3099                 break;
3100         default:
3101                 return (EINVAL);
3102         }
3103         return (0);
3104 }
3105
3106 int
3107 freebsd32_procctl(struct thread *td, struct freebsd32_procctl_args *uap)
3108 {
3109         void *data;
3110         union {
3111                 struct procctl_reaper_status rs;
3112                 struct procctl_reaper_pids rp;
3113                 struct procctl_reaper_kill rk;
3114         } x;
3115         union {
3116                 struct procctl_reaper_pids32 rp;
3117         } x32;
3118         int error, error1, flags, signum;
3119
3120         switch (uap->com) {
3121         case PROC_SPROTECT:
3122         case PROC_TRACE_CTL:
3123         case PROC_TRAPCAP_CTL:
3124                 error = copyin(PTRIN(uap->data), &flags, sizeof(flags));
3125                 if (error != 0)
3126                         return (error);
3127                 data = &flags;
3128                 break;
3129         case PROC_REAP_ACQUIRE:
3130         case PROC_REAP_RELEASE:
3131                 if (uap->data != NULL)
3132                         return (EINVAL);
3133                 data = NULL;
3134                 break;
3135         case PROC_REAP_STATUS:
3136                 data = &x.rs;
3137                 break;
3138         case PROC_REAP_GETPIDS:
3139                 error = copyin(uap->data, &x32.rp, sizeof(x32.rp));
3140                 if (error != 0)
3141                         return (error);
3142                 CP(x32.rp, x.rp, rp_count);
3143                 PTRIN_CP(x32.rp, x.rp, rp_pids);
3144                 data = &x.rp;
3145                 break;
3146         case PROC_REAP_KILL:
3147                 error = copyin(uap->data, &x.rk, sizeof(x.rk));
3148                 if (error != 0)
3149                         return (error);
3150                 data = &x.rk;
3151                 break;
3152         case PROC_TRACE_STATUS:
3153         case PROC_TRAPCAP_STATUS:
3154                 data = &flags;
3155                 break;
3156         case PROC_PDEATHSIG_CTL:
3157                 error = copyin(uap->data, &signum, sizeof(signum));
3158                 if (error != 0)
3159                         return (error);
3160                 data = &signum;
3161                 break;
3162         case PROC_PDEATHSIG_STATUS:
3163                 data = &signum;
3164                 break;
3165         default:
3166                 return (EINVAL);
3167         }
3168         error = kern_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id),
3169             uap->com, data);
3170         switch (uap->com) {
3171         case PROC_REAP_STATUS:
3172                 if (error == 0)
3173                         error = copyout(&x.rs, uap->data, sizeof(x.rs));
3174                 break;
3175         case PROC_REAP_KILL:
3176                 error1 = copyout(&x.rk, uap->data, sizeof(x.rk));
3177                 if (error == 0)
3178                         error = error1;
3179                 break;
3180         case PROC_TRACE_STATUS:
3181         case PROC_TRAPCAP_STATUS:
3182                 if (error == 0)
3183                         error = copyout(&flags, uap->data, sizeof(flags));
3184                 break;
3185         case PROC_PDEATHSIG_STATUS:
3186                 if (error == 0)
3187                         error = copyout(&signum, uap->data, sizeof(signum));
3188                 break;
3189         }
3190         return (error);
3191 }
3192
3193 int
3194 freebsd32_fcntl(struct thread *td, struct freebsd32_fcntl_args *uap)
3195 {
3196         long tmp;
3197
3198         switch (uap->cmd) {
3199         /*
3200          * Do unsigned conversion for arg when operation
3201          * interprets it as flags or pointer.
3202          */
3203         case F_SETLK_REMOTE:
3204         case F_SETLKW:
3205         case F_SETLK:
3206         case F_GETLK:
3207         case F_SETFD:
3208         case F_SETFL:
3209         case F_OGETLK:
3210         case F_OSETLK:
3211         case F_OSETLKW:
3212                 tmp = (unsigned int)(uap->arg);
3213                 break;
3214         default:
3215                 tmp = uap->arg;
3216                 break;
3217         }
3218         return (kern_fcntl_freebsd(td, uap->fd, uap->cmd, tmp));
3219 }
3220
3221 int
3222 freebsd32_ppoll(struct thread *td, struct freebsd32_ppoll_args *uap)
3223 {
3224         struct timespec32 ts32;
3225         struct timespec ts, *tsp;
3226         sigset_t set, *ssp;
3227         int error;
3228
3229         if (uap->ts != NULL) {
3230                 error = copyin(uap->ts, &ts32, sizeof(ts32));
3231                 if (error != 0)
3232                         return (error);
3233                 CP(ts32, ts, tv_sec);
3234                 CP(ts32, ts, tv_nsec);
3235                 tsp = &ts;
3236         } else
3237                 tsp = NULL;
3238         if (uap->set != NULL) {
3239                 error = copyin(uap->set, &set, sizeof(set));
3240                 if (error != 0)
3241                         return (error);
3242                 ssp = &set;
3243         } else
3244                 ssp = NULL;
3245
3246         return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp));
3247 }
3248
3249 int
3250 freebsd32_sched_rr_get_interval(struct thread *td,
3251     struct freebsd32_sched_rr_get_interval_args *uap)
3252 {
3253         struct timespec ts;
3254         struct timespec32 ts32;
3255         int error;
3256
3257         error = kern_sched_rr_get_interval(td, uap->pid, &ts);
3258         if (error == 0) {
3259                 CP(ts, ts32, tv_sec);
3260                 CP(ts, ts32, tv_nsec);
3261                 error = copyout(&ts32, uap->interval, sizeof(ts32));
3262         }
3263         return (error);
3264 }