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