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