]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/freebsd32/freebsd32_misc.c
MFV r311899:
[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         struct pread_args ap;
1456
1457         ap.fd = uap->fd;
1458         ap.buf = uap->buf;
1459         ap.nbyte = uap->nbyte;
1460         ap.offset = PAIR32TO64(off_t,uap->offset);
1461         return (sys_pread(td, &ap));
1462 }
1463
1464 int
1465 freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap)
1466 {
1467         struct pwrite_args ap;
1468
1469         ap.fd = uap->fd;
1470         ap.buf = uap->buf;
1471         ap.nbyte = uap->nbyte;
1472         ap.offset = PAIR32TO64(off_t,uap->offset);
1473         return (sys_pwrite(td, &ap));
1474 }
1475
1476 #ifdef COMPAT_43
1477 int
1478 ofreebsd32_lseek(struct thread *td, struct ofreebsd32_lseek_args *uap)
1479 {
1480         struct lseek_args nuap;
1481
1482         nuap.fd = uap->fd;
1483         nuap.offset = uap->offset;
1484         nuap.whence = uap->whence;
1485         return (sys_lseek(td, &nuap));
1486 }
1487 #endif
1488
1489 int
1490 freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap)
1491 {
1492         int error;
1493         struct lseek_args ap;
1494         off_t pos;
1495
1496         ap.fd = uap->fd;
1497         ap.offset = PAIR32TO64(off_t,uap->offset);
1498         ap.whence = uap->whence;
1499         error = sys_lseek(td, &ap);
1500         /* Expand the quad return into two parts for eax and edx */
1501         pos = td->td_uretoff.tdu_off;
1502         td->td_retval[RETVAL_LO] = pos & 0xffffffff;    /* %eax */
1503         td->td_retval[RETVAL_HI] = pos >> 32;           /* %edx */
1504         return error;
1505 }
1506
1507 int
1508 freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap)
1509 {
1510         struct truncate_args ap;
1511
1512         ap.path = uap->path;
1513         ap.length = PAIR32TO64(off_t,uap->length);
1514         return (sys_truncate(td, &ap));
1515 }
1516
1517 int
1518 freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap)
1519 {
1520         struct ftruncate_args ap;
1521
1522         ap.fd = uap->fd;
1523         ap.length = PAIR32TO64(off_t,uap->length);
1524         return (sys_ftruncate(td, &ap));
1525 }
1526
1527 #ifdef COMPAT_43
1528 int
1529 ofreebsd32_getdirentries(struct thread *td,
1530     struct ofreebsd32_getdirentries_args *uap)
1531 {
1532         struct ogetdirentries_args ap;
1533         int error;
1534         long loff;
1535         int32_t loff_cut;
1536
1537         ap.fd = uap->fd;
1538         ap.buf = uap->buf;
1539         ap.count = uap->count;
1540         ap.basep = NULL;
1541         error = kern_ogetdirentries(td, &ap, &loff);
1542         if (error == 0) {
1543                 loff_cut = loff;
1544                 error = copyout(&loff_cut, uap->basep, sizeof(int32_t));
1545         }
1546         return (error);
1547 }
1548 #endif
1549
1550 int
1551 freebsd32_getdirentries(struct thread *td,
1552     struct freebsd32_getdirentries_args *uap)
1553 {
1554         long base;
1555         int32_t base32;
1556         int error;
1557
1558         error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
1559             NULL, UIO_USERSPACE);
1560         if (error)
1561                 return (error);
1562         if (uap->basep != NULL) {
1563                 base32 = base;
1564                 error = copyout(&base32, uap->basep, sizeof(int32_t));
1565         }
1566         return (error);
1567 }
1568
1569 #ifdef COMPAT_FREEBSD6
1570 /* versions with the 'int pad' argument */
1571 int
1572 freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap)
1573 {
1574         struct pread_args ap;
1575
1576         ap.fd = uap->fd;
1577         ap.buf = uap->buf;
1578         ap.nbyte = uap->nbyte;
1579         ap.offset = PAIR32TO64(off_t,uap->offset);
1580         return (sys_pread(td, &ap));
1581 }
1582
1583 int
1584 freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap)
1585 {
1586         struct pwrite_args ap;
1587
1588         ap.fd = uap->fd;
1589         ap.buf = uap->buf;
1590         ap.nbyte = uap->nbyte;
1591         ap.offset = PAIR32TO64(off_t,uap->offset);
1592         return (sys_pwrite(td, &ap));
1593 }
1594
1595 int
1596 freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap)
1597 {
1598         int error;
1599         struct lseek_args ap;
1600         off_t pos;
1601
1602         ap.fd = uap->fd;
1603         ap.offset = PAIR32TO64(off_t,uap->offset);
1604         ap.whence = uap->whence;
1605         error = sys_lseek(td, &ap);
1606         /* Expand the quad return into two parts for eax and edx */
1607         pos = *(off_t *)(td->td_retval);
1608         td->td_retval[RETVAL_LO] = pos & 0xffffffff;    /* %eax */
1609         td->td_retval[RETVAL_HI] = pos >> 32;           /* %edx */
1610         return error;
1611 }
1612
1613 int
1614 freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap)
1615 {
1616         struct truncate_args ap;
1617
1618         ap.path = uap->path;
1619         ap.length = PAIR32TO64(off_t,uap->length);
1620         return (sys_truncate(td, &ap));
1621 }
1622
1623 int
1624 freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap)
1625 {
1626         struct ftruncate_args ap;
1627
1628         ap.fd = uap->fd;
1629         ap.length = PAIR32TO64(off_t,uap->length);
1630         return (sys_ftruncate(td, &ap));
1631 }
1632 #endif /* COMPAT_FREEBSD6 */
1633
1634 struct sf_hdtr32 {
1635         uint32_t headers;
1636         int hdr_cnt;
1637         uint32_t trailers;
1638         int trl_cnt;
1639 };
1640
1641 static int
1642 freebsd32_do_sendfile(struct thread *td,
1643     struct freebsd32_sendfile_args *uap, int compat)
1644 {
1645         struct sf_hdtr32 hdtr32;
1646         struct sf_hdtr hdtr;
1647         struct uio *hdr_uio, *trl_uio;
1648         struct file *fp;
1649         cap_rights_t rights;
1650         struct iovec32 *iov32;
1651         off_t offset, sbytes;
1652         int error;
1653
1654         offset = PAIR32TO64(off_t, uap->offset);
1655         if (offset < 0)
1656                 return (EINVAL);
1657
1658         hdr_uio = trl_uio = NULL;
1659
1660         if (uap->hdtr != NULL) {
1661                 error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
1662                 if (error)
1663                         goto out;
1664                 PTRIN_CP(hdtr32, hdtr, headers);
1665                 CP(hdtr32, hdtr, hdr_cnt);
1666                 PTRIN_CP(hdtr32, hdtr, trailers);
1667                 CP(hdtr32, hdtr, trl_cnt);
1668
1669                 if (hdtr.headers != NULL) {
1670                         iov32 = PTRIN(hdtr32.headers);
1671                         error = freebsd32_copyinuio(iov32,
1672                             hdtr32.hdr_cnt, &hdr_uio);
1673                         if (error)
1674                                 goto out;
1675 #ifdef COMPAT_FREEBSD4
1676                         /*
1677                          * In FreeBSD < 5.0 the nbytes to send also included
1678                          * the header.  If compat is specified subtract the
1679                          * header size from nbytes.
1680                          */
1681                         if (compat) {
1682                                 if (uap->nbytes > hdr_uio->uio_resid)
1683                                         uap->nbytes -= hdr_uio->uio_resid;
1684                                 else
1685                                         uap->nbytes = 0;
1686                         }
1687 #endif
1688                 }
1689                 if (hdtr.trailers != NULL) {
1690                         iov32 = PTRIN(hdtr32.trailers);
1691                         error = freebsd32_copyinuio(iov32,
1692                             hdtr32.trl_cnt, &trl_uio);
1693                         if (error)
1694                                 goto out;
1695                 }
1696         }
1697
1698         AUDIT_ARG_FD(uap->fd);
1699
1700         if ((error = fget_read(td, uap->fd,
1701             cap_rights_init(&rights, CAP_PREAD), &fp)) != 0)
1702                 goto out;
1703
1704         error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, offset,
1705             uap->nbytes, &sbytes, uap->flags, td);
1706         fdrop(fp, td);
1707
1708         if (uap->sbytes != NULL)
1709                 copyout(&sbytes, uap->sbytes, sizeof(off_t));
1710
1711 out:
1712         if (hdr_uio)
1713                 free(hdr_uio, M_IOV);
1714         if (trl_uio)
1715                 free(trl_uio, M_IOV);
1716         return (error);
1717 }
1718
1719 #ifdef COMPAT_FREEBSD4
1720 int
1721 freebsd4_freebsd32_sendfile(struct thread *td,
1722     struct freebsd4_freebsd32_sendfile_args *uap)
1723 {
1724         return (freebsd32_do_sendfile(td,
1725             (struct freebsd32_sendfile_args *)uap, 1));
1726 }
1727 #endif
1728
1729 int
1730 freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
1731 {
1732
1733         return (freebsd32_do_sendfile(td, uap, 0));
1734 }
1735
1736 static void
1737 copy_stat(struct stat *in, struct stat32 *out)
1738 {
1739
1740         CP(*in, *out, st_dev);
1741         CP(*in, *out, st_ino);
1742         CP(*in, *out, st_mode);
1743         CP(*in, *out, st_nlink);
1744         CP(*in, *out, st_uid);
1745         CP(*in, *out, st_gid);
1746         CP(*in, *out, st_rdev);
1747         TS_CP(*in, *out, st_atim);
1748         TS_CP(*in, *out, st_mtim);
1749         TS_CP(*in, *out, st_ctim);
1750         CP(*in, *out, st_size);
1751         CP(*in, *out, st_blocks);
1752         CP(*in, *out, st_blksize);
1753         CP(*in, *out, st_flags);
1754         CP(*in, *out, st_gen);
1755         TS_CP(*in, *out, st_birthtim);
1756 }
1757
1758 #ifdef COMPAT_43
1759 static void
1760 copy_ostat(struct stat *in, struct ostat32 *out)
1761 {
1762
1763         CP(*in, *out, st_dev);
1764         CP(*in, *out, st_ino);
1765         CP(*in, *out, st_mode);
1766         CP(*in, *out, st_nlink);
1767         CP(*in, *out, st_uid);
1768         CP(*in, *out, st_gid);
1769         CP(*in, *out, st_rdev);
1770         CP(*in, *out, st_size);
1771         TS_CP(*in, *out, st_atim);
1772         TS_CP(*in, *out, st_mtim);
1773         TS_CP(*in, *out, st_ctim);
1774         CP(*in, *out, st_blksize);
1775         CP(*in, *out, st_blocks);
1776         CP(*in, *out, st_flags);
1777         CP(*in, *out, st_gen);
1778 }
1779 #endif
1780
1781 int
1782 freebsd32_stat(struct thread *td, struct freebsd32_stat_args *uap)
1783 {
1784         struct stat sb;
1785         struct stat32 sb32;
1786         int error;
1787
1788         error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
1789             &sb, NULL);
1790         if (error)
1791                 return (error);
1792         copy_stat(&sb, &sb32);
1793         error = copyout(&sb32, uap->ub, sizeof (sb32));
1794         return (error);
1795 }
1796
1797 #ifdef COMPAT_43
1798 int
1799 ofreebsd32_stat(struct thread *td, struct ofreebsd32_stat_args *uap)
1800 {
1801         struct stat sb;
1802         struct ostat32 sb32;
1803         int error;
1804
1805         error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
1806             &sb, NULL);
1807         if (error)
1808                 return (error);
1809         copy_ostat(&sb, &sb32);
1810         error = copyout(&sb32, uap->ub, sizeof (sb32));
1811         return (error);
1812 }
1813 #endif
1814
1815 int
1816 freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
1817 {
1818         struct stat ub;
1819         struct stat32 ub32;
1820         int error;
1821
1822         error = kern_fstat(td, uap->fd, &ub);
1823         if (error)
1824                 return (error);
1825         copy_stat(&ub, &ub32);
1826         error = copyout(&ub32, uap->ub, sizeof(ub32));
1827         return (error);
1828 }
1829
1830 #ifdef COMPAT_43
1831 int
1832 ofreebsd32_fstat(struct thread *td, struct ofreebsd32_fstat_args *uap)
1833 {
1834         struct stat ub;
1835         struct ostat32 ub32;
1836         int error;
1837
1838         error = kern_fstat(td, uap->fd, &ub);
1839         if (error)
1840                 return (error);
1841         copy_ostat(&ub, &ub32);
1842         error = copyout(&ub32, uap->ub, sizeof(ub32));
1843         return (error);
1844 }
1845 #endif
1846
1847 int
1848 freebsd32_fstatat(struct thread *td, struct freebsd32_fstatat_args *uap)
1849 {
1850         struct stat ub;
1851         struct stat32 ub32;
1852         int error;
1853
1854         error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE,
1855             &ub, NULL);
1856         if (error)
1857                 return (error);
1858         copy_stat(&ub, &ub32);
1859         error = copyout(&ub32, uap->buf, sizeof(ub32));
1860         return (error);
1861 }
1862
1863 int
1864 freebsd32_lstat(struct thread *td, struct freebsd32_lstat_args *uap)
1865 {
1866         struct stat sb;
1867         struct stat32 sb32;
1868         int error;
1869
1870         error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
1871             UIO_USERSPACE, &sb, NULL);
1872         if (error)
1873                 return (error);
1874         copy_stat(&sb, &sb32);
1875         error = copyout(&sb32, uap->ub, sizeof (sb32));
1876         return (error);
1877 }
1878
1879 #ifdef COMPAT_43
1880 int
1881 ofreebsd32_lstat(struct thread *td, struct ofreebsd32_lstat_args *uap)
1882 {
1883         struct stat sb;
1884         struct ostat32 sb32;
1885         int error;
1886
1887         error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
1888             UIO_USERSPACE, &sb, NULL);
1889         if (error)
1890                 return (error);
1891         copy_ostat(&sb, &sb32);
1892         error = copyout(&sb32, uap->ub, sizeof (sb32));
1893         return (error);
1894 }
1895 #endif
1896
1897 int
1898 freebsd32_sysctl(struct thread *td, struct freebsd32_sysctl_args *uap)
1899 {
1900         int error, name[CTL_MAXNAME];
1901         size_t j, oldlen;
1902         uint32_t tmp;
1903
1904         if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1905                 return (EINVAL);
1906         error = copyin(uap->name, name, uap->namelen * sizeof(int));
1907         if (error)
1908                 return (error);
1909         if (uap->oldlenp) {
1910                 error = fueword32(uap->oldlenp, &tmp);
1911                 oldlen = tmp;
1912         } else {
1913                 oldlen = 0;
1914         }
1915         if (error != 0)
1916                 return (EFAULT);
1917         error = userland_sysctl(td, name, uap->namelen,
1918                 uap->old, &oldlen, 1,
1919                 uap->new, uap->newlen, &j, SCTL_MASK32);
1920         if (error && error != ENOMEM)
1921                 return (error);
1922         if (uap->oldlenp)
1923                 suword32(uap->oldlenp, j);
1924         return (0);
1925 }
1926
1927 int
1928 freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap)
1929 {
1930         uint32_t version;
1931         int error;
1932         struct jail j;
1933
1934         error = copyin(uap->jail, &version, sizeof(uint32_t));
1935         if (error)
1936                 return (error);
1937
1938         switch (version) {
1939         case 0:
1940         {
1941                 /* FreeBSD single IPv4 jails. */
1942                 struct jail32_v0 j32_v0;
1943
1944                 bzero(&j, sizeof(struct jail));
1945                 error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
1946                 if (error)
1947                         return (error);
1948                 CP(j32_v0, j, version);
1949                 PTRIN_CP(j32_v0, j, path);
1950                 PTRIN_CP(j32_v0, j, hostname);
1951                 j.ip4s = htonl(j32_v0.ip_number);       /* jail_v0 is host order */
1952                 break;
1953         }
1954
1955         case 1:
1956                 /*
1957                  * Version 1 was used by multi-IPv4 jail implementations
1958                  * that never made it into the official kernel.
1959                  */
1960                 return (EINVAL);
1961
1962         case 2: /* JAIL_API_VERSION */
1963         {
1964                 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
1965                 struct jail32 j32;
1966
1967                 error = copyin(uap->jail, &j32, sizeof(struct jail32));
1968                 if (error)
1969                         return (error);
1970                 CP(j32, j, version);
1971                 PTRIN_CP(j32, j, path);
1972                 PTRIN_CP(j32, j, hostname);
1973                 PTRIN_CP(j32, j, jailname);
1974                 CP(j32, j, ip4s);
1975                 CP(j32, j, ip6s);
1976                 PTRIN_CP(j32, j, ip4);
1977                 PTRIN_CP(j32, j, ip6);
1978                 break;
1979         }
1980
1981         default:
1982                 /* Sci-Fi jails are not supported, sorry. */
1983                 return (EINVAL);
1984         }
1985         return (kern_jail(td, &j));
1986 }
1987
1988 int
1989 freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap)
1990 {
1991         struct uio *auio;
1992         int error;
1993
1994         /* Check that we have an even number of iovecs. */
1995         if (uap->iovcnt & 1)
1996                 return (EINVAL);
1997
1998         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1999         if (error)
2000                 return (error);
2001         error = kern_jail_set(td, auio, uap->flags);
2002         free(auio, M_IOV);
2003         return (error);
2004 }
2005
2006 int
2007 freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap)
2008 {
2009         struct iovec32 iov32;
2010         struct uio *auio;
2011         int error, i;
2012
2013         /* Check that we have an even number of iovecs. */
2014         if (uap->iovcnt & 1)
2015                 return (EINVAL);
2016
2017         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2018         if (error)
2019                 return (error);
2020         error = kern_jail_get(td, auio, uap->flags);
2021         if (error == 0)
2022                 for (i = 0; i < uap->iovcnt; i++) {
2023                         PTROUT_CP(auio->uio_iov[i], iov32, iov_base);
2024                         CP(auio->uio_iov[i], iov32, iov_len);
2025                         error = copyout(&iov32, uap->iovp + i, sizeof(iov32));
2026                         if (error != 0)
2027                                 break;
2028                 }
2029         free(auio, M_IOV);
2030         return (error);
2031 }
2032
2033 int
2034 freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
2035 {
2036         struct sigaction32 s32;
2037         struct sigaction sa, osa, *sap;
2038         int error;
2039
2040         if (uap->act) {
2041                 error = copyin(uap->act, &s32, sizeof(s32));
2042                 if (error)
2043                         return (error);
2044                 sa.sa_handler = PTRIN(s32.sa_u);
2045                 CP(s32, sa, sa_flags);
2046                 CP(s32, sa, sa_mask);
2047                 sap = &sa;
2048         } else
2049                 sap = NULL;
2050         error = kern_sigaction(td, uap->sig, sap, &osa, 0);
2051         if (error == 0 && uap->oact != NULL) {
2052                 s32.sa_u = PTROUT(osa.sa_handler);
2053                 CP(osa, s32, sa_flags);
2054                 CP(osa, s32, sa_mask);
2055                 error = copyout(&s32, uap->oact, sizeof(s32));
2056         }
2057         return (error);
2058 }
2059
2060 #ifdef COMPAT_FREEBSD4
2061 int
2062 freebsd4_freebsd32_sigaction(struct thread *td,
2063                              struct freebsd4_freebsd32_sigaction_args *uap)
2064 {
2065         struct sigaction32 s32;
2066         struct sigaction sa, osa, *sap;
2067         int error;
2068
2069         if (uap->act) {
2070                 error = copyin(uap->act, &s32, sizeof(s32));
2071                 if (error)
2072                         return (error);
2073                 sa.sa_handler = PTRIN(s32.sa_u);
2074                 CP(s32, sa, sa_flags);
2075                 CP(s32, sa, sa_mask);
2076                 sap = &sa;
2077         } else
2078                 sap = NULL;
2079         error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
2080         if (error == 0 && uap->oact != NULL) {
2081                 s32.sa_u = PTROUT(osa.sa_handler);
2082                 CP(osa, s32, sa_flags);
2083                 CP(osa, s32, sa_mask);
2084                 error = copyout(&s32, uap->oact, sizeof(s32));
2085         }
2086         return (error);
2087 }
2088 #endif
2089
2090 #ifdef COMPAT_43
2091 struct osigaction32 {
2092         u_int32_t       sa_u;
2093         osigset_t       sa_mask;
2094         int             sa_flags;
2095 };
2096
2097 #define ONSIG   32
2098
2099 int
2100 ofreebsd32_sigaction(struct thread *td,
2101                              struct ofreebsd32_sigaction_args *uap)
2102 {
2103         struct osigaction32 s32;
2104         struct sigaction sa, osa, *sap;
2105         int error;
2106
2107         if (uap->signum <= 0 || uap->signum >= ONSIG)
2108                 return (EINVAL);
2109
2110         if (uap->nsa) {
2111                 error = copyin(uap->nsa, &s32, sizeof(s32));
2112                 if (error)
2113                         return (error);
2114                 sa.sa_handler = PTRIN(s32.sa_u);
2115                 CP(s32, sa, sa_flags);
2116                 OSIG2SIG(s32.sa_mask, sa.sa_mask);
2117                 sap = &sa;
2118         } else
2119                 sap = NULL;
2120         error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2121         if (error == 0 && uap->osa != NULL) {
2122                 s32.sa_u = PTROUT(osa.sa_handler);
2123                 CP(osa, s32, sa_flags);
2124                 SIG2OSIG(osa.sa_mask, s32.sa_mask);
2125                 error = copyout(&s32, uap->osa, sizeof(s32));
2126         }
2127         return (error);
2128 }
2129
2130 int
2131 ofreebsd32_sigprocmask(struct thread *td,
2132                                struct ofreebsd32_sigprocmask_args *uap)
2133 {
2134         sigset_t set, oset;
2135         int error;
2136
2137         OSIG2SIG(uap->mask, set);
2138         error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD);
2139         SIG2OSIG(oset, td->td_retval[0]);
2140         return (error);
2141 }
2142
2143 int
2144 ofreebsd32_sigpending(struct thread *td,
2145                               struct ofreebsd32_sigpending_args *uap)
2146 {
2147         struct proc *p = td->td_proc;
2148         sigset_t siglist;
2149
2150         PROC_LOCK(p);
2151         siglist = p->p_siglist;
2152         SIGSETOR(siglist, td->td_siglist);
2153         PROC_UNLOCK(p);
2154         SIG2OSIG(siglist, td->td_retval[0]);
2155         return (0);
2156 }
2157
2158 struct sigvec32 {
2159         u_int32_t       sv_handler;
2160         int             sv_mask;
2161         int             sv_flags;
2162 };
2163
2164 int
2165 ofreebsd32_sigvec(struct thread *td,
2166                           struct ofreebsd32_sigvec_args *uap)
2167 {
2168         struct sigvec32 vec;
2169         struct sigaction sa, osa, *sap;
2170         int error;
2171
2172         if (uap->signum <= 0 || uap->signum >= ONSIG)
2173                 return (EINVAL);
2174
2175         if (uap->nsv) {
2176                 error = copyin(uap->nsv, &vec, sizeof(vec));
2177                 if (error)
2178                         return (error);
2179                 sa.sa_handler = PTRIN(vec.sv_handler);
2180                 OSIG2SIG(vec.sv_mask, sa.sa_mask);
2181                 sa.sa_flags = vec.sv_flags;
2182                 sa.sa_flags ^= SA_RESTART;
2183                 sap = &sa;
2184         } else
2185                 sap = NULL;
2186         error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2187         if (error == 0 && uap->osv != NULL) {
2188                 vec.sv_handler = PTROUT(osa.sa_handler);
2189                 SIG2OSIG(osa.sa_mask, vec.sv_mask);
2190                 vec.sv_flags = osa.sa_flags;
2191                 vec.sv_flags &= ~SA_NOCLDWAIT;
2192                 vec.sv_flags ^= SA_RESTART;
2193                 error = copyout(&vec, uap->osv, sizeof(vec));
2194         }
2195         return (error);
2196 }
2197
2198 int
2199 ofreebsd32_sigblock(struct thread *td,
2200                             struct ofreebsd32_sigblock_args *uap)
2201 {
2202         sigset_t set, oset;
2203
2204         OSIG2SIG(uap->mask, set);
2205         kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
2206         SIG2OSIG(oset, td->td_retval[0]);
2207         return (0);
2208 }
2209
2210 int
2211 ofreebsd32_sigsetmask(struct thread *td,
2212                               struct ofreebsd32_sigsetmask_args *uap)
2213 {
2214         sigset_t set, oset;
2215
2216         OSIG2SIG(uap->mask, set);
2217         kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
2218         SIG2OSIG(oset, td->td_retval[0]);
2219         return (0);
2220 }
2221
2222 int
2223 ofreebsd32_sigsuspend(struct thread *td,
2224                               struct ofreebsd32_sigsuspend_args *uap)
2225 {
2226         sigset_t mask;
2227
2228         OSIG2SIG(uap->mask, mask);
2229         return (kern_sigsuspend(td, mask));
2230 }
2231
2232 struct sigstack32 {
2233         u_int32_t       ss_sp;
2234         int             ss_onstack;
2235 };
2236
2237 int
2238 ofreebsd32_sigstack(struct thread *td,
2239                             struct ofreebsd32_sigstack_args *uap)
2240 {
2241         struct sigstack32 s32;
2242         struct sigstack nss, oss;
2243         int error = 0, unss;
2244
2245         if (uap->nss != NULL) {
2246                 error = copyin(uap->nss, &s32, sizeof(s32));
2247                 if (error)
2248                         return (error);
2249                 nss.ss_sp = PTRIN(s32.ss_sp);
2250                 CP(s32, nss, ss_onstack);
2251                 unss = 1;
2252         } else {
2253                 unss = 0;
2254         }
2255         oss.ss_sp = td->td_sigstk.ss_sp;
2256         oss.ss_onstack = sigonstack(cpu_getstack(td));
2257         if (unss) {
2258                 td->td_sigstk.ss_sp = nss.ss_sp;
2259                 td->td_sigstk.ss_size = 0;
2260                 td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2261                 td->td_pflags |= TDP_ALTSTACK;
2262         }
2263         if (uap->oss != NULL) {
2264                 s32.ss_sp = PTROUT(oss.ss_sp);
2265                 CP(oss, s32, ss_onstack);
2266                 error = copyout(&s32, uap->oss, sizeof(s32));
2267         }
2268         return (error);
2269 }
2270 #endif
2271
2272 int
2273 freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2274 {
2275         struct timespec32 rmt32, rqt32;
2276         struct timespec rmt, rqt;
2277         int error;
2278
2279         error = copyin(uap->rqtp, &rqt32, sizeof(rqt32));
2280         if (error)
2281                 return (error);
2282
2283         CP(rqt32, rqt, tv_sec);
2284         CP(rqt32, rqt, tv_nsec);
2285
2286         if (uap->rmtp &&
2287             !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
2288                 return (EFAULT);
2289         error = kern_nanosleep(td, &rqt, &rmt);
2290         if (error && uap->rmtp) {
2291                 int error2;
2292
2293                 CP(rmt, rmt32, tv_sec);
2294                 CP(rmt, rmt32, tv_nsec);
2295
2296                 error2 = copyout(&rmt32, uap->rmtp, sizeof(rmt32));
2297                 if (error2)
2298                         error = error2;
2299         }
2300         return (error);
2301 }
2302
2303 int
2304 freebsd32_clock_gettime(struct thread *td,
2305                         struct freebsd32_clock_gettime_args *uap)
2306 {
2307         struct timespec ats;
2308         struct timespec32 ats32;
2309         int error;
2310
2311         error = kern_clock_gettime(td, uap->clock_id, &ats);
2312         if (error == 0) {
2313                 CP(ats, ats32, tv_sec);
2314                 CP(ats, ats32, tv_nsec);
2315                 error = copyout(&ats32, uap->tp, sizeof(ats32));
2316         }
2317         return (error);
2318 }
2319
2320 int
2321 freebsd32_clock_settime(struct thread *td,
2322                         struct freebsd32_clock_settime_args *uap)
2323 {
2324         struct timespec ats;
2325         struct timespec32 ats32;
2326         int error;
2327
2328         error = copyin(uap->tp, &ats32, sizeof(ats32));
2329         if (error)
2330                 return (error);
2331         CP(ats32, ats, tv_sec);
2332         CP(ats32, ats, tv_nsec);
2333
2334         return (kern_clock_settime(td, uap->clock_id, &ats));
2335 }
2336
2337 int
2338 freebsd32_clock_getres(struct thread *td,
2339                        struct freebsd32_clock_getres_args *uap)
2340 {
2341         struct timespec ts;
2342         struct timespec32 ts32;
2343         int error;
2344
2345         if (uap->tp == NULL)
2346                 return (0);
2347         error = kern_clock_getres(td, uap->clock_id, &ts);
2348         if (error == 0) {
2349                 CP(ts, ts32, tv_sec);
2350                 CP(ts, ts32, tv_nsec);
2351                 error = copyout(&ts32, uap->tp, sizeof(ts32));
2352         }
2353         return (error);
2354 }
2355
2356 int freebsd32_ktimer_create(struct thread *td,
2357     struct freebsd32_ktimer_create_args *uap)
2358 {
2359         struct sigevent32 ev32;
2360         struct sigevent ev, *evp;
2361         int error, id;
2362
2363         if (uap->evp == NULL) {
2364                 evp = NULL;
2365         } else {
2366                 evp = &ev;
2367                 error = copyin(uap->evp, &ev32, sizeof(ev32));
2368                 if (error != 0)
2369                         return (error);
2370                 error = convert_sigevent32(&ev32, &ev);
2371                 if (error != 0)
2372                         return (error);
2373         }
2374         error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
2375         if (error == 0) {
2376                 error = copyout(&id, uap->timerid, sizeof(int));
2377                 if (error != 0)
2378                         kern_ktimer_delete(td, id);
2379         }
2380         return (error);
2381 }
2382
2383 int
2384 freebsd32_ktimer_settime(struct thread *td,
2385     struct freebsd32_ktimer_settime_args *uap)
2386 {
2387         struct itimerspec32 val32, oval32;
2388         struct itimerspec val, oval, *ovalp;
2389         int error;
2390
2391         error = copyin(uap->value, &val32, sizeof(val32));
2392         if (error != 0)
2393                 return (error);
2394         ITS_CP(val32, val);
2395         ovalp = uap->ovalue != NULL ? &oval : NULL;
2396         error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
2397         if (error == 0 && uap->ovalue != NULL) {
2398                 ITS_CP(oval, oval32);
2399                 error = copyout(&oval32, uap->ovalue, sizeof(oval32));
2400         }
2401         return (error);
2402 }
2403
2404 int
2405 freebsd32_ktimer_gettime(struct thread *td,
2406     struct freebsd32_ktimer_gettime_args *uap)
2407 {
2408         struct itimerspec32 val32;
2409         struct itimerspec val;
2410         int error;
2411
2412         error = kern_ktimer_gettime(td, uap->timerid, &val);
2413         if (error == 0) {
2414                 ITS_CP(val, val32);
2415                 error = copyout(&val32, uap->value, sizeof(val32));
2416         }
2417         return (error);
2418 }
2419
2420 int
2421 freebsd32_clock_getcpuclockid2(struct thread *td,
2422     struct freebsd32_clock_getcpuclockid2_args *uap)
2423 {
2424         clockid_t clk_id;
2425         int error;
2426
2427         error = kern_clock_getcpuclockid2(td, PAIR32TO64(id_t, uap->id),
2428             uap->which, &clk_id);
2429         if (error == 0)
2430                 error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
2431         return (error);
2432 }
2433
2434 int
2435 freebsd32_thr_new(struct thread *td,
2436                   struct freebsd32_thr_new_args *uap)
2437 {
2438         struct thr_param32 param32;
2439         struct thr_param param;
2440         int error;
2441
2442         if (uap->param_size < 0 ||
2443             uap->param_size > sizeof(struct thr_param32))
2444                 return (EINVAL);
2445         bzero(&param, sizeof(struct thr_param));
2446         bzero(&param32, sizeof(struct thr_param32));
2447         error = copyin(uap->param, &param32, uap->param_size);
2448         if (error != 0)
2449                 return (error);
2450         param.start_func = PTRIN(param32.start_func);
2451         param.arg = PTRIN(param32.arg);
2452         param.stack_base = PTRIN(param32.stack_base);
2453         param.stack_size = param32.stack_size;
2454         param.tls_base = PTRIN(param32.tls_base);
2455         param.tls_size = param32.tls_size;
2456         param.child_tid = PTRIN(param32.child_tid);
2457         param.parent_tid = PTRIN(param32.parent_tid);
2458         param.flags = param32.flags;
2459         param.rtp = PTRIN(param32.rtp);
2460         param.spare[0] = PTRIN(param32.spare[0]);
2461         param.spare[1] = PTRIN(param32.spare[1]);
2462         param.spare[2] = PTRIN(param32.spare[2]);
2463
2464         return (kern_thr_new(td, &param));
2465 }
2466
2467 int
2468 freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
2469 {
2470         struct timespec32 ts32;
2471         struct timespec ts, *tsp;
2472         int error;
2473
2474         error = 0;
2475         tsp = NULL;
2476         if (uap->timeout != NULL) {
2477                 error = copyin((const void *)uap->timeout, (void *)&ts32,
2478                     sizeof(struct timespec32));
2479                 if (error != 0)
2480                         return (error);
2481                 ts.tv_sec = ts32.tv_sec;
2482                 ts.tv_nsec = ts32.tv_nsec;
2483                 tsp = &ts;
2484         }
2485         return (kern_thr_suspend(td, tsp));
2486 }
2487
2488 void
2489 siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst)
2490 {
2491         bzero(dst, sizeof(*dst));
2492         dst->si_signo = src->si_signo;
2493         dst->si_errno = src->si_errno;
2494         dst->si_code = src->si_code;
2495         dst->si_pid = src->si_pid;
2496         dst->si_uid = src->si_uid;
2497         dst->si_status = src->si_status;
2498         dst->si_addr = (uintptr_t)src->si_addr;
2499         dst->si_value.sival_int = src->si_value.sival_int;
2500         dst->si_timerid = src->si_timerid;
2501         dst->si_overrun = src->si_overrun;
2502 }
2503
2504 int
2505 freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
2506 {
2507         struct timespec32 ts32;
2508         struct timespec ts;
2509         struct timespec *timeout;
2510         sigset_t set;
2511         ksiginfo_t ksi;
2512         struct siginfo32 si32;
2513         int error;
2514
2515         if (uap->timeout) {
2516                 error = copyin(uap->timeout, &ts32, sizeof(ts32));
2517                 if (error)
2518                         return (error);
2519                 ts.tv_sec = ts32.tv_sec;
2520                 ts.tv_nsec = ts32.tv_nsec;
2521                 timeout = &ts;
2522         } else
2523                 timeout = NULL;
2524
2525         error = copyin(uap->set, &set, sizeof(set));
2526         if (error)
2527                 return (error);
2528
2529         error = kern_sigtimedwait(td, set, &ksi, timeout);
2530         if (error)
2531                 return (error);
2532
2533         if (uap->info) {
2534                 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2535                 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2536         }
2537
2538         if (error == 0)
2539                 td->td_retval[0] = ksi.ksi_signo;
2540         return (error);
2541 }
2542
2543 /*
2544  * MPSAFE
2545  */
2546 int
2547 freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
2548 {
2549         ksiginfo_t ksi;
2550         struct siginfo32 si32;
2551         sigset_t set;
2552         int error;
2553
2554         error = copyin(uap->set, &set, sizeof(set));
2555         if (error)
2556                 return (error);
2557
2558         error = kern_sigtimedwait(td, set, &ksi, NULL);
2559         if (error)
2560                 return (error);
2561
2562         if (uap->info) {
2563                 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2564                 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2565         }       
2566         if (error == 0)
2567                 td->td_retval[0] = ksi.ksi_signo;
2568         return (error);
2569 }
2570
2571 int
2572 freebsd32_cpuset_setid(struct thread *td,
2573     struct freebsd32_cpuset_setid_args *uap)
2574 {
2575         struct cpuset_setid_args ap;
2576
2577         ap.which = uap->which;
2578         ap.id = PAIR32TO64(id_t,uap->id);
2579         ap.setid = uap->setid;
2580
2581         return (sys_cpuset_setid(td, &ap));
2582 }
2583
2584 int
2585 freebsd32_cpuset_getid(struct thread *td,
2586     struct freebsd32_cpuset_getid_args *uap)
2587 {
2588         struct cpuset_getid_args ap;
2589
2590         ap.level = uap->level;
2591         ap.which = uap->which;
2592         ap.id = PAIR32TO64(id_t,uap->id);
2593         ap.setid = uap->setid;
2594
2595         return (sys_cpuset_getid(td, &ap));
2596 }
2597
2598 int
2599 freebsd32_cpuset_getaffinity(struct thread *td,
2600     struct freebsd32_cpuset_getaffinity_args *uap)
2601 {
2602         struct cpuset_getaffinity_args ap;
2603
2604         ap.level = uap->level;
2605         ap.which = uap->which;
2606         ap.id = PAIR32TO64(id_t,uap->id);
2607         ap.cpusetsize = uap->cpusetsize;
2608         ap.mask = uap->mask;
2609
2610         return (sys_cpuset_getaffinity(td, &ap));
2611 }
2612
2613 int
2614 freebsd32_cpuset_setaffinity(struct thread *td,
2615     struct freebsd32_cpuset_setaffinity_args *uap)
2616 {
2617         struct cpuset_setaffinity_args ap;
2618
2619         ap.level = uap->level;
2620         ap.which = uap->which;
2621         ap.id = PAIR32TO64(id_t,uap->id);
2622         ap.cpusetsize = uap->cpusetsize;
2623         ap.mask = uap->mask;
2624
2625         return (sys_cpuset_setaffinity(td, &ap));
2626 }
2627
2628 int
2629 freebsd32_nmount(struct thread *td,
2630     struct freebsd32_nmount_args /* {
2631         struct iovec *iovp;
2632         unsigned int iovcnt;
2633         int flags;
2634     } */ *uap)
2635 {
2636         struct uio *auio;
2637         uint64_t flags;
2638         int error;
2639
2640         /*
2641          * Mount flags are now 64-bits. On 32-bit archtectures only
2642          * 32-bits are passed in, but from here on everything handles
2643          * 64-bit flags correctly.
2644          */
2645         flags = uap->flags;
2646
2647         AUDIT_ARG_FFLAGS(flags);
2648
2649         /*
2650          * Filter out MNT_ROOTFS.  We do not want clients of nmount() in
2651          * userspace to set this flag, but we must filter it out if we want
2652          * MNT_UPDATE on the root file system to work.
2653          * MNT_ROOTFS should only be set by the kernel when mounting its
2654          * root file system.
2655          */
2656         flags &= ~MNT_ROOTFS;
2657
2658         /*
2659          * check that we have an even number of iovec's
2660          * and that we have at least two options.
2661          */
2662         if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
2663                 return (EINVAL);
2664
2665         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2666         if (error)
2667                 return (error);
2668         error = vfs_donmount(td, flags, auio);
2669
2670         free(auio, M_IOV);
2671         return error;
2672 }
2673
2674 #if 0
2675 int
2676 freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
2677 {
2678         struct yyy32 *p32, s32;
2679         struct yyy *p = NULL, s;
2680         struct xxx_arg ap;
2681         int error;
2682
2683         if (uap->zzz) {
2684                 error = copyin(uap->zzz, &s32, sizeof(s32));
2685                 if (error)
2686                         return (error);
2687                 /* translate in */
2688                 p = &s;
2689         }
2690         error = kern_xxx(td, p);
2691         if (error)
2692                 return (error);
2693         if (uap->zzz) {
2694                 /* translate out */
2695                 error = copyout(&s32, p32, sizeof(s32));
2696         }
2697         return (error);
2698 }
2699 #endif
2700
2701 int
2702 syscall32_register(int *offset, struct sysent *new_sysent,
2703     struct sysent *old_sysent, int flags)
2704 {
2705
2706         if ((flags & ~SY_THR_STATIC) != 0)
2707                 return (EINVAL);
2708
2709         if (*offset == NO_SYSCALL) {
2710                 int i;
2711
2712                 for (i = 1; i < SYS_MAXSYSCALL; ++i)
2713                         if (freebsd32_sysent[i].sy_call ==
2714                             (sy_call_t *)lkmnosys)
2715                                 break;
2716                 if (i == SYS_MAXSYSCALL)
2717                         return (ENFILE);
2718                 *offset = i;
2719         } else if (*offset < 0 || *offset >= SYS_MAXSYSCALL)
2720                 return (EINVAL);
2721         else if (freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmnosys &&
2722             freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmressys)
2723                 return (EEXIST);
2724
2725         *old_sysent = freebsd32_sysent[*offset];
2726         freebsd32_sysent[*offset] = *new_sysent;
2727         atomic_store_rel_32(&freebsd32_sysent[*offset].sy_thrcnt, flags);
2728         return (0);
2729 }
2730
2731 int
2732 syscall32_deregister(int *offset, struct sysent *old_sysent)
2733 {
2734
2735         if (*offset == 0)
2736                 return (0);
2737
2738         freebsd32_sysent[*offset] = *old_sysent;
2739         return (0);
2740 }
2741
2742 int
2743 syscall32_module_handler(struct module *mod, int what, void *arg)
2744 {
2745         struct syscall_module_data *data = (struct syscall_module_data*)arg;
2746         modspecific_t ms;
2747         int error;
2748
2749         switch (what) {
2750         case MOD_LOAD:
2751                 error = syscall32_register(data->offset, data->new_sysent,
2752                     &data->old_sysent, SY_THR_STATIC_KLD);
2753                 if (error) {
2754                         /* Leave a mark so we know to safely unload below. */
2755                         data->offset = NULL;
2756                         return error;
2757                 }
2758                 ms.intval = *data->offset;
2759                 MOD_XLOCK;
2760                 module_setspecific(mod, &ms);
2761                 MOD_XUNLOCK;
2762                 if (data->chainevh)
2763                         error = data->chainevh(mod, what, data->chainarg);
2764                 return (error);
2765         case MOD_UNLOAD:
2766                 /*
2767                  * MOD_LOAD failed, so just return without calling the
2768                  * chained handler since we didn't pass along the MOD_LOAD
2769                  * event.
2770                  */
2771                 if (data->offset == NULL)
2772                         return (0);
2773                 if (data->chainevh) {
2774                         error = data->chainevh(mod, what, data->chainarg);
2775                         if (error)
2776                                 return (error);
2777                 }
2778                 error = syscall32_deregister(data->offset, &data->old_sysent);
2779                 return (error);
2780         default:
2781                 error = EOPNOTSUPP;
2782                 if (data->chainevh)
2783                         error = data->chainevh(mod, what, data->chainarg);
2784                 return (error);
2785         }
2786 }
2787
2788 int
2789 syscall32_helper_register(struct syscall_helper_data *sd, int flags)
2790 {
2791         struct syscall_helper_data *sd1;
2792         int error;
2793
2794         for (sd1 = sd; sd1->syscall_no != NO_SYSCALL; sd1++) {
2795                 error = syscall32_register(&sd1->syscall_no, &sd1->new_sysent,
2796                     &sd1->old_sysent, flags);
2797                 if (error != 0) {
2798                         syscall32_helper_unregister(sd);
2799                         return (error);
2800                 }
2801                 sd1->registered = 1;
2802         }
2803         return (0);
2804 }
2805
2806 int
2807 syscall32_helper_unregister(struct syscall_helper_data *sd)
2808 {
2809         struct syscall_helper_data *sd1;
2810
2811         for (sd1 = sd; sd1->registered != 0; sd1++) {
2812                 syscall32_deregister(&sd1->syscall_no, &sd1->old_sysent);
2813                 sd1->registered = 0;
2814         }
2815         return (0);
2816 }
2817
2818 register_t *
2819 freebsd32_copyout_strings(struct image_params *imgp)
2820 {
2821         int argc, envc, i;
2822         u_int32_t *vectp;
2823         char *stringp;
2824         uintptr_t destp;
2825         u_int32_t *stack_base;
2826         struct freebsd32_ps_strings *arginfo;
2827         char canary[sizeof(long) * 8];
2828         int32_t pagesizes32[MAXPAGESIZES];
2829         size_t execpath_len;
2830         int szsigcode;
2831
2832         /*
2833          * Calculate string base and vector table pointers.
2834          * Also deal with signal trampoline code for this exec type.
2835          */
2836         if (imgp->execpath != NULL && imgp->auxargs != NULL)
2837                 execpath_len = strlen(imgp->execpath) + 1;
2838         else
2839                 execpath_len = 0;
2840         arginfo = (struct freebsd32_ps_strings *)curproc->p_sysent->
2841             sv_psstrings;
2842         if (imgp->proc->p_sysent->sv_sigcode_base == 0)
2843                 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
2844         else
2845                 szsigcode = 0;
2846         destp = (uintptr_t)arginfo;
2847
2848         /*
2849          * install sigcode
2850          */
2851         if (szsigcode != 0) {
2852                 destp -= szsigcode;
2853                 destp = rounddown2(destp, sizeof(uint32_t));
2854                 copyout(imgp->proc->p_sysent->sv_sigcode, (void *)destp,
2855                     szsigcode);
2856         }
2857
2858         /*
2859          * Copy the image path for the rtld.
2860          */
2861         if (execpath_len != 0) {
2862                 destp -= execpath_len;
2863                 imgp->execpathp = destp;
2864                 copyout(imgp->execpath, (void *)destp, execpath_len);
2865         }
2866
2867         /*
2868          * Prepare the canary for SSP.
2869          */
2870         arc4rand(canary, sizeof(canary), 0);
2871         destp -= sizeof(canary);
2872         imgp->canary = destp;
2873         copyout(canary, (void *)destp, sizeof(canary));
2874         imgp->canarylen = sizeof(canary);
2875
2876         /*
2877          * Prepare the pagesizes array.
2878          */
2879         for (i = 0; i < MAXPAGESIZES; i++)
2880                 pagesizes32[i] = (uint32_t)pagesizes[i];
2881         destp -= sizeof(pagesizes32);
2882         destp = rounddown2(destp, sizeof(uint32_t));
2883         imgp->pagesizes = destp;
2884         copyout(pagesizes32, (void *)destp, sizeof(pagesizes32));
2885         imgp->pagesizeslen = sizeof(pagesizes32);
2886
2887         destp -= ARG_MAX - imgp->args->stringspace;
2888         destp = rounddown2(destp, sizeof(uint32_t));
2889
2890         /*
2891          * If we have a valid auxargs ptr, prepare some room
2892          * on the stack.
2893          */
2894         if (imgp->auxargs) {
2895                 /*
2896                  * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
2897                  * lower compatibility.
2898                  */
2899                 imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size
2900                         : (AT_COUNT * 2);
2901                 /*
2902                  * The '+ 2' is for the null pointers at the end of each of
2903                  * the arg and env vector sets,and imgp->auxarg_size is room
2904                  * for argument of Runtime loader.
2905                  */
2906                 vectp = (u_int32_t *) (destp - (imgp->args->argc +
2907                     imgp->args->envc + 2 + imgp->auxarg_size + execpath_len) *
2908                     sizeof(u_int32_t));
2909         } else {
2910                 /*
2911                  * The '+ 2' is for the null pointers at the end of each of
2912                  * the arg and env vector sets
2913                  */
2914                 vectp = (u_int32_t *)(destp - (imgp->args->argc +
2915                     imgp->args->envc + 2) * sizeof(u_int32_t));
2916         }
2917
2918         /*
2919          * vectp also becomes our initial stack base
2920          */
2921         stack_base = vectp;
2922
2923         stringp = imgp->args->begin_argv;
2924         argc = imgp->args->argc;
2925         envc = imgp->args->envc;
2926         /*
2927          * Copy out strings - arguments and environment.
2928          */
2929         copyout(stringp, (void *)destp, ARG_MAX - imgp->args->stringspace);
2930
2931         /*
2932          * Fill in "ps_strings" struct for ps, w, etc.
2933          */
2934         suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp);
2935         suword32(&arginfo->ps_nargvstr, argc);
2936
2937         /*
2938          * Fill in argument portion of vector table.
2939          */
2940         for (; argc > 0; --argc) {
2941                 suword32(vectp++, (u_int32_t)(intptr_t)destp);
2942                 while (*stringp++ != 0)
2943                         destp++;
2944                 destp++;
2945         }
2946
2947         /* a null vector table pointer separates the argp's from the envp's */
2948         suword32(vectp++, 0);
2949
2950         suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp);
2951         suword32(&arginfo->ps_nenvstr, envc);
2952
2953         /*
2954          * Fill in environment portion of vector table.
2955          */
2956         for (; envc > 0; --envc) {
2957                 suword32(vectp++, (u_int32_t)(intptr_t)destp);
2958                 while (*stringp++ != 0)
2959                         destp++;
2960                 destp++;
2961         }
2962
2963         /* end of vector table is a null pointer */
2964         suword32(vectp, 0);
2965
2966         return ((register_t *)stack_base);
2967 }
2968
2969 int
2970 freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
2971 {
2972         struct kld_file_stat stat;
2973         struct kld32_file_stat stat32;
2974         int error, version;
2975
2976         if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
2977             != 0)
2978                 return (error);
2979         if (version != sizeof(struct kld32_file_stat_1) &&
2980             version != sizeof(struct kld32_file_stat))
2981                 return (EINVAL);
2982
2983         error = kern_kldstat(td, uap->fileid, &stat);
2984         if (error != 0)
2985                 return (error);
2986
2987         bcopy(&stat.name[0], &stat32.name[0], sizeof(stat.name));
2988         CP(stat, stat32, refs);
2989         CP(stat, stat32, id);
2990         PTROUT_CP(stat, stat32, address);
2991         CP(stat, stat32, size);
2992         bcopy(&stat.pathname[0], &stat32.pathname[0], sizeof(stat.pathname));
2993         return (copyout(&stat32, uap->stat, version));
2994 }
2995
2996 int
2997 freebsd32_posix_fallocate(struct thread *td,
2998     struct freebsd32_posix_fallocate_args *uap)
2999 {
3000         int error;
3001
3002         error = kern_posix_fallocate(td, uap->fd,
3003             PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len));
3004         return (kern_posix_error(td, error));
3005 }
3006
3007 int
3008 freebsd32_posix_fadvise(struct thread *td,
3009     struct freebsd32_posix_fadvise_args *uap)
3010 {
3011         int error;
3012
3013         error = kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset),
3014             PAIR32TO64(off_t, uap->len), uap->advice);
3015         return (kern_posix_error(td, error));
3016 }
3017
3018 int
3019 convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig)
3020 {
3021
3022         CP(*sig32, *sig, sigev_notify);
3023         switch (sig->sigev_notify) {
3024         case SIGEV_NONE:
3025                 break;
3026         case SIGEV_THREAD_ID:
3027                 CP(*sig32, *sig, sigev_notify_thread_id);
3028                 /* FALLTHROUGH */
3029         case SIGEV_SIGNAL:
3030                 CP(*sig32, *sig, sigev_signo);
3031                 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3032                 break;
3033         case SIGEV_KEVENT:
3034                 CP(*sig32, *sig, sigev_notify_kqueue);
3035                 CP(*sig32, *sig, sigev_notify_kevent_flags);
3036                 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3037                 break;
3038         default:
3039                 return (EINVAL);
3040         }
3041         return (0);
3042 }
3043
3044 int
3045 freebsd32_procctl(struct thread *td, struct freebsd32_procctl_args *uap)
3046 {
3047         void *data;
3048         union {
3049                 struct procctl_reaper_status rs;
3050                 struct procctl_reaper_pids rp;
3051                 struct procctl_reaper_kill rk;
3052         } x;
3053         union {
3054                 struct procctl_reaper_pids32 rp;
3055         } x32;
3056         int error, error1, flags;
3057
3058         switch (uap->com) {
3059         case PROC_SPROTECT:
3060         case PROC_TRACE_CTL:
3061         case PROC_TRAPCAP_CTL:
3062                 error = copyin(PTRIN(uap->data), &flags, sizeof(flags));
3063                 if (error != 0)
3064                         return (error);
3065                 data = &flags;
3066                 break;
3067         case PROC_REAP_ACQUIRE:
3068         case PROC_REAP_RELEASE:
3069                 if (uap->data != NULL)
3070                         return (EINVAL);
3071                 data = NULL;
3072                 break;
3073         case PROC_REAP_STATUS:
3074                 data = &x.rs;
3075                 break;
3076         case PROC_REAP_GETPIDS:
3077                 error = copyin(uap->data, &x32.rp, sizeof(x32.rp));
3078                 if (error != 0)
3079                         return (error);
3080                 CP(x32.rp, x.rp, rp_count);
3081                 PTRIN_CP(x32.rp, x.rp, rp_pids);
3082                 data = &x.rp;
3083                 break;
3084         case PROC_REAP_KILL:
3085                 error = copyin(uap->data, &x.rk, sizeof(x.rk));
3086                 if (error != 0)
3087                         return (error);
3088                 data = &x.rk;
3089                 break;
3090         case PROC_TRACE_STATUS:
3091         case PROC_TRAPCAP_STATUS:
3092                 data = &flags;
3093                 break;
3094         default:
3095                 return (EINVAL);
3096         }
3097         error = kern_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id),
3098             uap->com, data);
3099         switch (uap->com) {
3100         case PROC_REAP_STATUS:
3101                 if (error == 0)
3102                         error = copyout(&x.rs, uap->data, sizeof(x.rs));
3103                 break;
3104         case PROC_REAP_KILL:
3105                 error1 = copyout(&x.rk, uap->data, sizeof(x.rk));
3106                 if (error == 0)
3107                         error = error1;
3108                 break;
3109         case PROC_TRACE_STATUS:
3110         case PROC_TRAPCAP_STATUS:
3111                 if (error == 0)
3112                         error = copyout(&flags, uap->data, sizeof(flags));
3113                 break;
3114         }
3115         return (error);
3116 }
3117
3118 int
3119 freebsd32_fcntl(struct thread *td, struct freebsd32_fcntl_args *uap)
3120 {
3121         long tmp;
3122
3123         switch (uap->cmd) {
3124         /*
3125          * Do unsigned conversion for arg when operation
3126          * interprets it as flags or pointer.
3127          */
3128         case F_SETLK_REMOTE:
3129         case F_SETLKW:
3130         case F_SETLK:
3131         case F_GETLK:
3132         case F_SETFD:
3133         case F_SETFL:
3134         case F_OGETLK:
3135         case F_OSETLK:
3136         case F_OSETLKW:
3137                 tmp = (unsigned int)(uap->arg);
3138                 break;
3139         default:
3140                 tmp = uap->arg;
3141                 break;
3142         }
3143         return (kern_fcntl_freebsd(td, uap->fd, uap->cmd, tmp));
3144 }
3145
3146 int
3147 freebsd32_ppoll(struct thread *td, struct freebsd32_ppoll_args *uap)
3148 {
3149         struct timespec32 ts32;
3150         struct timespec ts, *tsp;
3151         sigset_t set, *ssp;
3152         int error;
3153
3154         if (uap->ts != NULL) {
3155                 error = copyin(uap->ts, &ts32, sizeof(ts32));
3156                 if (error != 0)
3157                         return (error);
3158                 CP(ts32, ts, tv_sec);
3159                 CP(ts32, ts, tv_nsec);
3160                 tsp = &ts;
3161         } else
3162                 tsp = NULL;
3163         if (uap->set != NULL) {
3164                 error = copyin(uap->set, &set, sizeof(set));
3165                 if (error != 0)
3166                         return (error);
3167                 ssp = &set;
3168         } else
3169                 ssp = NULL;
3170
3171         return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp));
3172 }