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