]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/freebsd32/freebsd32_misc.c
Add sysctl vfs.ino64_trunc_error controlling action on truncating
[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 && ino64_trunc_error)
1915                 return (EOVERFLOW);
1916         CP(*in, *out, st_nlink);
1917         if (in->st_nlink != out->st_nlink && ino64_trunc_error)
1918                 return (EOVERFLOW);
1919         CP(*in, *out, st_dev);
1920         CP(*in, *out, st_mode);
1921         CP(*in, *out, st_uid);
1922         CP(*in, *out, st_gid);
1923         CP(*in, *out, st_rdev);
1924         TS_CP(*in, *out, st_atim);
1925         TS_CP(*in, *out, st_mtim);
1926         TS_CP(*in, *out, st_ctim);
1927         CP(*in, *out, st_size);
1928         CP(*in, *out, st_blocks);
1929         CP(*in, *out, st_blksize);
1930         CP(*in, *out, st_flags);
1931         CP(*in, *out, st_gen);
1932         TS_CP(*in, *out, st_birthtim);
1933         out->st_lspare = 0;
1934         bzero((char *)&out->st_birthtim + sizeof(out->st_birthtim),
1935             sizeof(*out) - offsetof(struct freebsd11_stat32,
1936             st_birthtim) - sizeof(out->st_birthtim));
1937         return (0);
1938 }
1939
1940 int
1941 freebsd11_freebsd32_stat(struct thread *td,
1942     struct freebsd11_freebsd32_stat_args *uap)
1943 {
1944         struct stat sb;
1945         struct freebsd11_stat32 sb32;
1946         int error;
1947
1948         error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
1949             &sb, NULL);
1950         if (error != 0)
1951                 return (error);
1952         error = freebsd11_cvtstat32(&sb, &sb32);
1953         if (error == 0)
1954                 error = copyout(&sb32, uap->ub, sizeof (sb32));
1955         return (error);
1956 }
1957
1958 int
1959 freebsd11_freebsd32_fstat(struct thread *td,
1960     struct freebsd11_freebsd32_fstat_args *uap)
1961 {
1962         struct stat sb;
1963         struct freebsd11_stat32 sb32;
1964         int error;
1965
1966         error = kern_fstat(td, uap->fd, &sb);
1967         if (error != 0)
1968                 return (error);
1969         error = freebsd11_cvtstat32(&sb, &sb32);
1970         if (error == 0)
1971                 error = copyout(&sb32, uap->ub, sizeof (sb32));
1972         return (error);
1973 }
1974
1975 int
1976 freebsd11_freebsd32_fstatat(struct thread *td,
1977     struct freebsd11_freebsd32_fstatat_args *uap)
1978 {
1979         struct stat sb;
1980         struct freebsd11_stat32 sb32;
1981         int error;
1982
1983         error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE,
1984             &sb, NULL);
1985         if (error != 0)
1986                 return (error);
1987         error = freebsd11_cvtstat32(&sb, &sb32);
1988         if (error == 0)
1989                 error = copyout(&sb32, uap->buf, sizeof (sb32));
1990         return (error);
1991 }
1992
1993 int
1994 freebsd11_freebsd32_lstat(struct thread *td,
1995     struct freebsd11_freebsd32_lstat_args *uap)
1996 {
1997         struct stat sb;
1998         struct freebsd11_stat32 sb32;
1999         int error;
2000
2001         error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2002             UIO_USERSPACE, &sb, NULL);
2003         if (error != 0)
2004                 return (error);
2005         error = freebsd11_cvtstat32(&sb, &sb32);
2006         if (error == 0)
2007                 error = copyout(&sb32, uap->ub, sizeof (sb32));
2008         return (error);
2009 }
2010
2011 int
2012 freebsd11_freebsd32_fhstat(struct thread *td,
2013     struct freebsd11_freebsd32_fhstat_args *uap)
2014 {
2015         struct stat sb;
2016         struct freebsd11_stat32 sb32;
2017         struct fhandle fh;
2018         int error;
2019
2020         error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2021         if (error != 0)
2022                 return (error);
2023         error = kern_fhstat(td, fh, &sb);
2024         if (error != 0)
2025                 return (error);
2026         error = freebsd11_cvtstat32(&sb, &sb32);
2027         if (error == 0)
2028                 error = copyout(&sb32, uap->sb, sizeof (sb32));
2029         return (error);
2030 }
2031 #endif
2032
2033 int
2034 freebsd32_sysctl(struct thread *td, struct freebsd32_sysctl_args *uap)
2035 {
2036         int error, name[CTL_MAXNAME];
2037         size_t j, oldlen;
2038         uint32_t tmp;
2039
2040         if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
2041                 return (EINVAL);
2042         error = copyin(uap->name, name, uap->namelen * sizeof(int));
2043         if (error)
2044                 return (error);
2045         if (uap->oldlenp) {
2046                 error = fueword32(uap->oldlenp, &tmp);
2047                 oldlen = tmp;
2048         } else {
2049                 oldlen = 0;
2050         }
2051         if (error != 0)
2052                 return (EFAULT);
2053         error = userland_sysctl(td, name, uap->namelen,
2054                 uap->old, &oldlen, 1,
2055                 uap->new, uap->newlen, &j, SCTL_MASK32);
2056         if (error && error != ENOMEM)
2057                 return (error);
2058         if (uap->oldlenp)
2059                 suword32(uap->oldlenp, j);
2060         return (0);
2061 }
2062
2063 int
2064 freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap)
2065 {
2066         uint32_t version;
2067         int error;
2068         struct jail j;
2069
2070         error = copyin(uap->jail, &version, sizeof(uint32_t));
2071         if (error)
2072                 return (error);
2073
2074         switch (version) {
2075         case 0:
2076         {
2077                 /* FreeBSD single IPv4 jails. */
2078                 struct jail32_v0 j32_v0;
2079
2080                 bzero(&j, sizeof(struct jail));
2081                 error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
2082                 if (error)
2083                         return (error);
2084                 CP(j32_v0, j, version);
2085                 PTRIN_CP(j32_v0, j, path);
2086                 PTRIN_CP(j32_v0, j, hostname);
2087                 j.ip4s = htonl(j32_v0.ip_number);       /* jail_v0 is host order */
2088                 break;
2089         }
2090
2091         case 1:
2092                 /*
2093                  * Version 1 was used by multi-IPv4 jail implementations
2094                  * that never made it into the official kernel.
2095                  */
2096                 return (EINVAL);
2097
2098         case 2: /* JAIL_API_VERSION */
2099         {
2100                 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
2101                 struct jail32 j32;
2102
2103                 error = copyin(uap->jail, &j32, sizeof(struct jail32));
2104                 if (error)
2105                         return (error);
2106                 CP(j32, j, version);
2107                 PTRIN_CP(j32, j, path);
2108                 PTRIN_CP(j32, j, hostname);
2109                 PTRIN_CP(j32, j, jailname);
2110                 CP(j32, j, ip4s);
2111                 CP(j32, j, ip6s);
2112                 PTRIN_CP(j32, j, ip4);
2113                 PTRIN_CP(j32, j, ip6);
2114                 break;
2115         }
2116
2117         default:
2118                 /* Sci-Fi jails are not supported, sorry. */
2119                 return (EINVAL);
2120         }
2121         return (kern_jail(td, &j));
2122 }
2123
2124 int
2125 freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap)
2126 {
2127         struct uio *auio;
2128         int error;
2129
2130         /* Check that we have an even number of iovecs. */
2131         if (uap->iovcnt & 1)
2132                 return (EINVAL);
2133
2134         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2135         if (error)
2136                 return (error);
2137         error = kern_jail_set(td, auio, uap->flags);
2138         free(auio, M_IOV);
2139         return (error);
2140 }
2141
2142 int
2143 freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap)
2144 {
2145         struct iovec32 iov32;
2146         struct uio *auio;
2147         int error, i;
2148
2149         /* Check that we have an even number of iovecs. */
2150         if (uap->iovcnt & 1)
2151                 return (EINVAL);
2152
2153         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2154         if (error)
2155                 return (error);
2156         error = kern_jail_get(td, auio, uap->flags);
2157         if (error == 0)
2158                 for (i = 0; i < uap->iovcnt; i++) {
2159                         PTROUT_CP(auio->uio_iov[i], iov32, iov_base);
2160                         CP(auio->uio_iov[i], iov32, iov_len);
2161                         error = copyout(&iov32, uap->iovp + i, sizeof(iov32));
2162                         if (error != 0)
2163                                 break;
2164                 }
2165         free(auio, M_IOV);
2166         return (error);
2167 }
2168
2169 int
2170 freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
2171 {
2172         struct sigaction32 s32;
2173         struct sigaction sa, osa, *sap;
2174         int error;
2175
2176         if (uap->act) {
2177                 error = copyin(uap->act, &s32, sizeof(s32));
2178                 if (error)
2179                         return (error);
2180                 sa.sa_handler = PTRIN(s32.sa_u);
2181                 CP(s32, sa, sa_flags);
2182                 CP(s32, sa, sa_mask);
2183                 sap = &sa;
2184         } else
2185                 sap = NULL;
2186         error = kern_sigaction(td, uap->sig, sap, &osa, 0);
2187         if (error == 0 && uap->oact != NULL) {
2188                 s32.sa_u = PTROUT(osa.sa_handler);
2189                 CP(osa, s32, sa_flags);
2190                 CP(osa, s32, sa_mask);
2191                 error = copyout(&s32, uap->oact, sizeof(s32));
2192         }
2193         return (error);
2194 }
2195
2196 #ifdef COMPAT_FREEBSD4
2197 int
2198 freebsd4_freebsd32_sigaction(struct thread *td,
2199                              struct freebsd4_freebsd32_sigaction_args *uap)
2200 {
2201         struct sigaction32 s32;
2202         struct sigaction sa, osa, *sap;
2203         int error;
2204
2205         if (uap->act) {
2206                 error = copyin(uap->act, &s32, sizeof(s32));
2207                 if (error)
2208                         return (error);
2209                 sa.sa_handler = PTRIN(s32.sa_u);
2210                 CP(s32, sa, sa_flags);
2211                 CP(s32, sa, sa_mask);
2212                 sap = &sa;
2213         } else
2214                 sap = NULL;
2215         error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
2216         if (error == 0 && uap->oact != NULL) {
2217                 s32.sa_u = PTROUT(osa.sa_handler);
2218                 CP(osa, s32, sa_flags);
2219                 CP(osa, s32, sa_mask);
2220                 error = copyout(&s32, uap->oact, sizeof(s32));
2221         }
2222         return (error);
2223 }
2224 #endif
2225
2226 #ifdef COMPAT_43
2227 struct osigaction32 {
2228         u_int32_t       sa_u;
2229         osigset_t       sa_mask;
2230         int             sa_flags;
2231 };
2232
2233 #define ONSIG   32
2234
2235 int
2236 ofreebsd32_sigaction(struct thread *td,
2237                              struct ofreebsd32_sigaction_args *uap)
2238 {
2239         struct osigaction32 s32;
2240         struct sigaction sa, osa, *sap;
2241         int error;
2242
2243         if (uap->signum <= 0 || uap->signum >= ONSIG)
2244                 return (EINVAL);
2245
2246         if (uap->nsa) {
2247                 error = copyin(uap->nsa, &s32, sizeof(s32));
2248                 if (error)
2249                         return (error);
2250                 sa.sa_handler = PTRIN(s32.sa_u);
2251                 CP(s32, sa, sa_flags);
2252                 OSIG2SIG(s32.sa_mask, sa.sa_mask);
2253                 sap = &sa;
2254         } else
2255                 sap = NULL;
2256         error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2257         if (error == 0 && uap->osa != NULL) {
2258                 s32.sa_u = PTROUT(osa.sa_handler);
2259                 CP(osa, s32, sa_flags);
2260                 SIG2OSIG(osa.sa_mask, s32.sa_mask);
2261                 error = copyout(&s32, uap->osa, sizeof(s32));
2262         }
2263         return (error);
2264 }
2265
2266 int
2267 ofreebsd32_sigprocmask(struct thread *td,
2268                                struct ofreebsd32_sigprocmask_args *uap)
2269 {
2270         sigset_t set, oset;
2271         int error;
2272
2273         OSIG2SIG(uap->mask, set);
2274         error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD);
2275         SIG2OSIG(oset, td->td_retval[0]);
2276         return (error);
2277 }
2278
2279 int
2280 ofreebsd32_sigpending(struct thread *td,
2281                               struct ofreebsd32_sigpending_args *uap)
2282 {
2283         struct proc *p = td->td_proc;
2284         sigset_t siglist;
2285
2286         PROC_LOCK(p);
2287         siglist = p->p_siglist;
2288         SIGSETOR(siglist, td->td_siglist);
2289         PROC_UNLOCK(p);
2290         SIG2OSIG(siglist, td->td_retval[0]);
2291         return (0);
2292 }
2293
2294 struct sigvec32 {
2295         u_int32_t       sv_handler;
2296         int             sv_mask;
2297         int             sv_flags;
2298 };
2299
2300 int
2301 ofreebsd32_sigvec(struct thread *td,
2302                           struct ofreebsd32_sigvec_args *uap)
2303 {
2304         struct sigvec32 vec;
2305         struct sigaction sa, osa, *sap;
2306         int error;
2307
2308         if (uap->signum <= 0 || uap->signum >= ONSIG)
2309                 return (EINVAL);
2310
2311         if (uap->nsv) {
2312                 error = copyin(uap->nsv, &vec, sizeof(vec));
2313                 if (error)
2314                         return (error);
2315                 sa.sa_handler = PTRIN(vec.sv_handler);
2316                 OSIG2SIG(vec.sv_mask, sa.sa_mask);
2317                 sa.sa_flags = vec.sv_flags;
2318                 sa.sa_flags ^= SA_RESTART;
2319                 sap = &sa;
2320         } else
2321                 sap = NULL;
2322         error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2323         if (error == 0 && uap->osv != NULL) {
2324                 vec.sv_handler = PTROUT(osa.sa_handler);
2325                 SIG2OSIG(osa.sa_mask, vec.sv_mask);
2326                 vec.sv_flags = osa.sa_flags;
2327                 vec.sv_flags &= ~SA_NOCLDWAIT;
2328                 vec.sv_flags ^= SA_RESTART;
2329                 error = copyout(&vec, uap->osv, sizeof(vec));
2330         }
2331         return (error);
2332 }
2333
2334 int
2335 ofreebsd32_sigblock(struct thread *td,
2336                             struct ofreebsd32_sigblock_args *uap)
2337 {
2338         sigset_t set, oset;
2339
2340         OSIG2SIG(uap->mask, set);
2341         kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
2342         SIG2OSIG(oset, td->td_retval[0]);
2343         return (0);
2344 }
2345
2346 int
2347 ofreebsd32_sigsetmask(struct thread *td,
2348                               struct ofreebsd32_sigsetmask_args *uap)
2349 {
2350         sigset_t set, oset;
2351
2352         OSIG2SIG(uap->mask, set);
2353         kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
2354         SIG2OSIG(oset, td->td_retval[0]);
2355         return (0);
2356 }
2357
2358 int
2359 ofreebsd32_sigsuspend(struct thread *td,
2360                               struct ofreebsd32_sigsuspend_args *uap)
2361 {
2362         sigset_t mask;
2363
2364         OSIG2SIG(uap->mask, mask);
2365         return (kern_sigsuspend(td, mask));
2366 }
2367
2368 struct sigstack32 {
2369         u_int32_t       ss_sp;
2370         int             ss_onstack;
2371 };
2372
2373 int
2374 ofreebsd32_sigstack(struct thread *td,
2375                             struct ofreebsd32_sigstack_args *uap)
2376 {
2377         struct sigstack32 s32;
2378         struct sigstack nss, oss;
2379         int error = 0, unss;
2380
2381         if (uap->nss != NULL) {
2382                 error = copyin(uap->nss, &s32, sizeof(s32));
2383                 if (error)
2384                         return (error);
2385                 nss.ss_sp = PTRIN(s32.ss_sp);
2386                 CP(s32, nss, ss_onstack);
2387                 unss = 1;
2388         } else {
2389                 unss = 0;
2390         }
2391         oss.ss_sp = td->td_sigstk.ss_sp;
2392         oss.ss_onstack = sigonstack(cpu_getstack(td));
2393         if (unss) {
2394                 td->td_sigstk.ss_sp = nss.ss_sp;
2395                 td->td_sigstk.ss_size = 0;
2396                 td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2397                 td->td_pflags |= TDP_ALTSTACK;
2398         }
2399         if (uap->oss != NULL) {
2400                 s32.ss_sp = PTROUT(oss.ss_sp);
2401                 CP(oss, s32, ss_onstack);
2402                 error = copyout(&s32, uap->oss, sizeof(s32));
2403         }
2404         return (error);
2405 }
2406 #endif
2407
2408 int
2409 freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2410 {
2411
2412         return (freebsd32_user_clock_nanosleep(td, CLOCK_REALTIME,
2413             TIMER_RELTIME, uap->rqtp, uap->rmtp));
2414 }
2415
2416 int
2417 freebsd32_clock_nanosleep(struct thread *td,
2418     struct freebsd32_clock_nanosleep_args *uap)
2419 {
2420         int error;
2421
2422         error = freebsd32_user_clock_nanosleep(td, uap->clock_id, uap->flags,
2423             uap->rqtp, uap->rmtp);
2424         return (kern_posix_error(td, error));
2425 }
2426
2427 static int
2428 freebsd32_user_clock_nanosleep(struct thread *td, clockid_t clock_id,
2429     int flags, const struct timespec32 *ua_rqtp, struct timespec32 *ua_rmtp)
2430 {
2431         struct timespec32 rmt32, rqt32;
2432         struct timespec rmt, rqt;
2433         int error;
2434
2435         error = copyin(ua_rqtp, &rqt32, sizeof(rqt32));
2436         if (error)
2437                 return (error);
2438
2439         CP(rqt32, rqt, tv_sec);
2440         CP(rqt32, rqt, tv_nsec);
2441
2442         if (ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0 &&
2443             !useracc(ua_rmtp, sizeof(rmt32), VM_PROT_WRITE))
2444                 return (EFAULT);
2445         error = kern_clock_nanosleep(td, clock_id, flags, &rqt, &rmt);
2446         if (error == EINTR && ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0) {
2447                 int error2;
2448
2449                 CP(rmt, rmt32, tv_sec);
2450                 CP(rmt, rmt32, tv_nsec);
2451
2452                 error2 = copyout(&rmt32, ua_rmtp, sizeof(rmt32));
2453                 if (error2)
2454                         error = error2;
2455         }
2456         return (error);
2457 }
2458
2459 int
2460 freebsd32_clock_gettime(struct thread *td,
2461                         struct freebsd32_clock_gettime_args *uap)
2462 {
2463         struct timespec ats;
2464         struct timespec32 ats32;
2465         int error;
2466
2467         error = kern_clock_gettime(td, uap->clock_id, &ats);
2468         if (error == 0) {
2469                 CP(ats, ats32, tv_sec);
2470                 CP(ats, ats32, tv_nsec);
2471                 error = copyout(&ats32, uap->tp, sizeof(ats32));
2472         }
2473         return (error);
2474 }
2475
2476 int
2477 freebsd32_clock_settime(struct thread *td,
2478                         struct freebsd32_clock_settime_args *uap)
2479 {
2480         struct timespec ats;
2481         struct timespec32 ats32;
2482         int error;
2483
2484         error = copyin(uap->tp, &ats32, sizeof(ats32));
2485         if (error)
2486                 return (error);
2487         CP(ats32, ats, tv_sec);
2488         CP(ats32, ats, tv_nsec);
2489
2490         return (kern_clock_settime(td, uap->clock_id, &ats));
2491 }
2492
2493 int
2494 freebsd32_clock_getres(struct thread *td,
2495                        struct freebsd32_clock_getres_args *uap)
2496 {
2497         struct timespec ts;
2498         struct timespec32 ts32;
2499         int error;
2500
2501         if (uap->tp == NULL)
2502                 return (0);
2503         error = kern_clock_getres(td, uap->clock_id, &ts);
2504         if (error == 0) {
2505                 CP(ts, ts32, tv_sec);
2506                 CP(ts, ts32, tv_nsec);
2507                 error = copyout(&ts32, uap->tp, sizeof(ts32));
2508         }
2509         return (error);
2510 }
2511
2512 int freebsd32_ktimer_create(struct thread *td,
2513     struct freebsd32_ktimer_create_args *uap)
2514 {
2515         struct sigevent32 ev32;
2516         struct sigevent ev, *evp;
2517         int error, id;
2518
2519         if (uap->evp == NULL) {
2520                 evp = NULL;
2521         } else {
2522                 evp = &ev;
2523                 error = copyin(uap->evp, &ev32, sizeof(ev32));
2524                 if (error != 0)
2525                         return (error);
2526                 error = convert_sigevent32(&ev32, &ev);
2527                 if (error != 0)
2528                         return (error);
2529         }
2530         error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
2531         if (error == 0) {
2532                 error = copyout(&id, uap->timerid, sizeof(int));
2533                 if (error != 0)
2534                         kern_ktimer_delete(td, id);
2535         }
2536         return (error);
2537 }
2538
2539 int
2540 freebsd32_ktimer_settime(struct thread *td,
2541     struct freebsd32_ktimer_settime_args *uap)
2542 {
2543         struct itimerspec32 val32, oval32;
2544         struct itimerspec val, oval, *ovalp;
2545         int error;
2546
2547         error = copyin(uap->value, &val32, sizeof(val32));
2548         if (error != 0)
2549                 return (error);
2550         ITS_CP(val32, val);
2551         ovalp = uap->ovalue != NULL ? &oval : NULL;
2552         error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
2553         if (error == 0 && uap->ovalue != NULL) {
2554                 ITS_CP(oval, oval32);
2555                 error = copyout(&oval32, uap->ovalue, sizeof(oval32));
2556         }
2557         return (error);
2558 }
2559
2560 int
2561 freebsd32_ktimer_gettime(struct thread *td,
2562     struct freebsd32_ktimer_gettime_args *uap)
2563 {
2564         struct itimerspec32 val32;
2565         struct itimerspec val;
2566         int error;
2567
2568         error = kern_ktimer_gettime(td, uap->timerid, &val);
2569         if (error == 0) {
2570                 ITS_CP(val, val32);
2571                 error = copyout(&val32, uap->value, sizeof(val32));
2572         }
2573         return (error);
2574 }
2575
2576 int
2577 freebsd32_clock_getcpuclockid2(struct thread *td,
2578     struct freebsd32_clock_getcpuclockid2_args *uap)
2579 {
2580         clockid_t clk_id;
2581         int error;
2582
2583         error = kern_clock_getcpuclockid2(td, PAIR32TO64(id_t, uap->id),
2584             uap->which, &clk_id);
2585         if (error == 0)
2586                 error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
2587         return (error);
2588 }
2589
2590 int
2591 freebsd32_thr_new(struct thread *td,
2592                   struct freebsd32_thr_new_args *uap)
2593 {
2594         struct thr_param32 param32;
2595         struct thr_param param;
2596         int error;
2597
2598         if (uap->param_size < 0 ||
2599             uap->param_size > sizeof(struct thr_param32))
2600                 return (EINVAL);
2601         bzero(&param, sizeof(struct thr_param));
2602         bzero(&param32, sizeof(struct thr_param32));
2603         error = copyin(uap->param, &param32, uap->param_size);
2604         if (error != 0)
2605                 return (error);
2606         param.start_func = PTRIN(param32.start_func);
2607         param.arg = PTRIN(param32.arg);
2608         param.stack_base = PTRIN(param32.stack_base);
2609         param.stack_size = param32.stack_size;
2610         param.tls_base = PTRIN(param32.tls_base);
2611         param.tls_size = param32.tls_size;
2612         param.child_tid = PTRIN(param32.child_tid);
2613         param.parent_tid = PTRIN(param32.parent_tid);
2614         param.flags = param32.flags;
2615         param.rtp = PTRIN(param32.rtp);
2616         param.spare[0] = PTRIN(param32.spare[0]);
2617         param.spare[1] = PTRIN(param32.spare[1]);
2618         param.spare[2] = PTRIN(param32.spare[2]);
2619
2620         return (kern_thr_new(td, &param));
2621 }
2622
2623 int
2624 freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
2625 {
2626         struct timespec32 ts32;
2627         struct timespec ts, *tsp;
2628         int error;
2629
2630         error = 0;
2631         tsp = NULL;
2632         if (uap->timeout != NULL) {
2633                 error = copyin((const void *)uap->timeout, (void *)&ts32,
2634                     sizeof(struct timespec32));
2635                 if (error != 0)
2636                         return (error);
2637                 ts.tv_sec = ts32.tv_sec;
2638                 ts.tv_nsec = ts32.tv_nsec;
2639                 tsp = &ts;
2640         }
2641         return (kern_thr_suspend(td, tsp));
2642 }
2643
2644 void
2645 siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst)
2646 {
2647         bzero(dst, sizeof(*dst));
2648         dst->si_signo = src->si_signo;
2649         dst->si_errno = src->si_errno;
2650         dst->si_code = src->si_code;
2651         dst->si_pid = src->si_pid;
2652         dst->si_uid = src->si_uid;
2653         dst->si_status = src->si_status;
2654         dst->si_addr = (uintptr_t)src->si_addr;
2655         dst->si_value.sival_int = src->si_value.sival_int;
2656         dst->si_timerid = src->si_timerid;
2657         dst->si_overrun = src->si_overrun;
2658 }
2659
2660 #ifndef _FREEBSD32_SYSPROTO_H_
2661 struct freebsd32_sigqueue_args {
2662         pid_t pid;
2663         int signum;
2664         /* union sigval32 */ int value;
2665 };
2666 #endif
2667 int
2668 freebsd32_sigqueue(struct thread *td, struct freebsd32_sigqueue_args *uap)
2669 {
2670         union sigval sv;
2671
2672         /*
2673          * On 32-bit ABIs, sival_int and sival_ptr are the same.
2674          * On 64-bit little-endian ABIs, the low bits are the same.
2675          * In 64-bit big-endian ABIs, sival_int overlaps with
2676          * sival_ptr's HIGH bits.  We choose to support sival_int
2677          * rather than sival_ptr in this case as it seems to be
2678          * more common.
2679          */
2680         bzero(&sv, sizeof(sv));
2681         sv.sival_int = uap->value;
2682
2683         return (kern_sigqueue(td, uap->pid, uap->signum, &sv));
2684 }
2685
2686 int
2687 freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
2688 {
2689         struct timespec32 ts32;
2690         struct timespec ts;
2691         struct timespec *timeout;
2692         sigset_t set;
2693         ksiginfo_t ksi;
2694         struct siginfo32 si32;
2695         int error;
2696
2697         if (uap->timeout) {
2698                 error = copyin(uap->timeout, &ts32, sizeof(ts32));
2699                 if (error)
2700                         return (error);
2701                 ts.tv_sec = ts32.tv_sec;
2702                 ts.tv_nsec = ts32.tv_nsec;
2703                 timeout = &ts;
2704         } else
2705                 timeout = NULL;
2706
2707         error = copyin(uap->set, &set, sizeof(set));
2708         if (error)
2709                 return (error);
2710
2711         error = kern_sigtimedwait(td, set, &ksi, timeout);
2712         if (error)
2713                 return (error);
2714
2715         if (uap->info) {
2716                 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2717                 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2718         }
2719
2720         if (error == 0)
2721                 td->td_retval[0] = ksi.ksi_signo;
2722         return (error);
2723 }
2724
2725 /*
2726  * MPSAFE
2727  */
2728 int
2729 freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
2730 {
2731         ksiginfo_t ksi;
2732         struct siginfo32 si32;
2733         sigset_t set;
2734         int error;
2735
2736         error = copyin(uap->set, &set, sizeof(set));
2737         if (error)
2738                 return (error);
2739
2740         error = kern_sigtimedwait(td, set, &ksi, NULL);
2741         if (error)
2742                 return (error);
2743
2744         if (uap->info) {
2745                 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2746                 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2747         }       
2748         if (error == 0)
2749                 td->td_retval[0] = ksi.ksi_signo;
2750         return (error);
2751 }
2752
2753 int
2754 freebsd32_cpuset_setid(struct thread *td,
2755     struct freebsd32_cpuset_setid_args *uap)
2756 {
2757
2758         return (kern_cpuset_setid(td, uap->which,
2759             PAIR32TO64(id_t, uap->id), uap->setid));
2760 }
2761
2762 int
2763 freebsd32_cpuset_getid(struct thread *td,
2764     struct freebsd32_cpuset_getid_args *uap)
2765 {
2766
2767         return (kern_cpuset_getid(td, uap->level, uap->which,
2768             PAIR32TO64(id_t, uap->id), uap->setid));
2769 }
2770
2771 int
2772 freebsd32_cpuset_getaffinity(struct thread *td,
2773     struct freebsd32_cpuset_getaffinity_args *uap)
2774 {
2775
2776         return (kern_cpuset_getaffinity(td, uap->level, uap->which,
2777             PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask));
2778 }
2779
2780 int
2781 freebsd32_cpuset_setaffinity(struct thread *td,
2782     struct freebsd32_cpuset_setaffinity_args *uap)
2783 {
2784
2785         return (kern_cpuset_setaffinity(td, uap->level, uap->which,
2786             PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask));
2787 }
2788
2789 int
2790 freebsd32_nmount(struct thread *td,
2791     struct freebsd32_nmount_args /* {
2792         struct iovec *iovp;
2793         unsigned int iovcnt;
2794         int flags;
2795     } */ *uap)
2796 {
2797         struct uio *auio;
2798         uint64_t flags;
2799         int error;
2800
2801         /*
2802          * Mount flags are now 64-bits. On 32-bit archtectures only
2803          * 32-bits are passed in, but from here on everything handles
2804          * 64-bit flags correctly.
2805          */
2806         flags = uap->flags;
2807
2808         AUDIT_ARG_FFLAGS(flags);
2809
2810         /*
2811          * Filter out MNT_ROOTFS.  We do not want clients of nmount() in
2812          * userspace to set this flag, but we must filter it out if we want
2813          * MNT_UPDATE on the root file system to work.
2814          * MNT_ROOTFS should only be set by the kernel when mounting its
2815          * root file system.
2816          */
2817         flags &= ~MNT_ROOTFS;
2818
2819         /*
2820          * check that we have an even number of iovec's
2821          * and that we have at least two options.
2822          */
2823         if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
2824                 return (EINVAL);
2825
2826         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2827         if (error)
2828                 return (error);
2829         error = vfs_donmount(td, flags, auio);
2830
2831         free(auio, M_IOV);
2832         return error;
2833 }
2834
2835 #if 0
2836 int
2837 freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
2838 {
2839         struct yyy32 *p32, s32;
2840         struct yyy *p = NULL, s;
2841         struct xxx_arg ap;
2842         int error;
2843
2844         if (uap->zzz) {
2845                 error = copyin(uap->zzz, &s32, sizeof(s32));
2846                 if (error)
2847                         return (error);
2848                 /* translate in */
2849                 p = &s;
2850         }
2851         error = kern_xxx(td, p);
2852         if (error)
2853                 return (error);
2854         if (uap->zzz) {
2855                 /* translate out */
2856                 error = copyout(&s32, p32, sizeof(s32));
2857         }
2858         return (error);
2859 }
2860 #endif
2861
2862 int
2863 syscall32_register(int *offset, struct sysent *new_sysent,
2864     struct sysent *old_sysent, int flags)
2865 {
2866
2867         if ((flags & ~SY_THR_STATIC) != 0)
2868                 return (EINVAL);
2869
2870         if (*offset == NO_SYSCALL) {
2871                 int i;
2872
2873                 for (i = 1; i < SYS_MAXSYSCALL; ++i)
2874                         if (freebsd32_sysent[i].sy_call ==
2875                             (sy_call_t *)lkmnosys)
2876                                 break;
2877                 if (i == SYS_MAXSYSCALL)
2878                         return (ENFILE);
2879                 *offset = i;
2880         } else if (*offset < 0 || *offset >= SYS_MAXSYSCALL)
2881                 return (EINVAL);
2882         else if (freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmnosys &&
2883             freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmressys)
2884                 return (EEXIST);
2885
2886         *old_sysent = freebsd32_sysent[*offset];
2887         freebsd32_sysent[*offset] = *new_sysent;
2888         atomic_store_rel_32(&freebsd32_sysent[*offset].sy_thrcnt, flags);
2889         return (0);
2890 }
2891
2892 int
2893 syscall32_deregister(int *offset, struct sysent *old_sysent)
2894 {
2895
2896         if (*offset == 0)
2897                 return (0);
2898
2899         freebsd32_sysent[*offset] = *old_sysent;
2900         return (0);
2901 }
2902
2903 int
2904 syscall32_module_handler(struct module *mod, int what, void *arg)
2905 {
2906         struct syscall_module_data *data = (struct syscall_module_data*)arg;
2907         modspecific_t ms;
2908         int error;
2909
2910         switch (what) {
2911         case MOD_LOAD:
2912                 error = syscall32_register(data->offset, data->new_sysent,
2913                     &data->old_sysent, SY_THR_STATIC_KLD);
2914                 if (error) {
2915                         /* Leave a mark so we know to safely unload below. */
2916                         data->offset = NULL;
2917                         return error;
2918                 }
2919                 ms.intval = *data->offset;
2920                 MOD_XLOCK;
2921                 module_setspecific(mod, &ms);
2922                 MOD_XUNLOCK;
2923                 if (data->chainevh)
2924                         error = data->chainevh(mod, what, data->chainarg);
2925                 return (error);
2926         case MOD_UNLOAD:
2927                 /*
2928                  * MOD_LOAD failed, so just return without calling the
2929                  * chained handler since we didn't pass along the MOD_LOAD
2930                  * event.
2931                  */
2932                 if (data->offset == NULL)
2933                         return (0);
2934                 if (data->chainevh) {
2935                         error = data->chainevh(mod, what, data->chainarg);
2936                         if (error)
2937                                 return (error);
2938                 }
2939                 error = syscall32_deregister(data->offset, &data->old_sysent);
2940                 return (error);
2941         default:
2942                 error = EOPNOTSUPP;
2943                 if (data->chainevh)
2944                         error = data->chainevh(mod, what, data->chainarg);
2945                 return (error);
2946         }
2947 }
2948
2949 int
2950 syscall32_helper_register(struct syscall_helper_data *sd, int flags)
2951 {
2952         struct syscall_helper_data *sd1;
2953         int error;
2954
2955         for (sd1 = sd; sd1->syscall_no != NO_SYSCALL; sd1++) {
2956                 error = syscall32_register(&sd1->syscall_no, &sd1->new_sysent,
2957                     &sd1->old_sysent, flags);
2958                 if (error != 0) {
2959                         syscall32_helper_unregister(sd);
2960                         return (error);
2961                 }
2962                 sd1->registered = 1;
2963         }
2964         return (0);
2965 }
2966
2967 int
2968 syscall32_helper_unregister(struct syscall_helper_data *sd)
2969 {
2970         struct syscall_helper_data *sd1;
2971
2972         for (sd1 = sd; sd1->registered != 0; sd1++) {
2973                 syscall32_deregister(&sd1->syscall_no, &sd1->old_sysent);
2974                 sd1->registered = 0;
2975         }
2976         return (0);
2977 }
2978
2979 register_t *
2980 freebsd32_copyout_strings(struct image_params *imgp)
2981 {
2982         int argc, envc, i;
2983         u_int32_t *vectp;
2984         char *stringp;
2985         uintptr_t destp;
2986         u_int32_t *stack_base;
2987         struct freebsd32_ps_strings *arginfo;
2988         char canary[sizeof(long) * 8];
2989         int32_t pagesizes32[MAXPAGESIZES];
2990         size_t execpath_len;
2991         int szsigcode;
2992
2993         /*
2994          * Calculate string base and vector table pointers.
2995          * Also deal with signal trampoline code for this exec type.
2996          */
2997         if (imgp->execpath != NULL && imgp->auxargs != NULL)
2998                 execpath_len = strlen(imgp->execpath) + 1;
2999         else
3000                 execpath_len = 0;
3001         arginfo = (struct freebsd32_ps_strings *)curproc->p_sysent->
3002             sv_psstrings;
3003         if (imgp->proc->p_sysent->sv_sigcode_base == 0)
3004                 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
3005         else
3006                 szsigcode = 0;
3007         destp = (uintptr_t)arginfo;
3008
3009         /*
3010          * install sigcode
3011          */
3012         if (szsigcode != 0) {
3013                 destp -= szsigcode;
3014                 destp = rounddown2(destp, sizeof(uint32_t));
3015                 copyout(imgp->proc->p_sysent->sv_sigcode, (void *)destp,
3016                     szsigcode);
3017         }
3018
3019         /*
3020          * Copy the image path for the rtld.
3021          */
3022         if (execpath_len != 0) {
3023                 destp -= execpath_len;
3024                 imgp->execpathp = destp;
3025                 copyout(imgp->execpath, (void *)destp, execpath_len);
3026         }
3027
3028         /*
3029          * Prepare the canary for SSP.
3030          */
3031         arc4rand(canary, sizeof(canary), 0);
3032         destp -= sizeof(canary);
3033         imgp->canary = destp;
3034         copyout(canary, (void *)destp, sizeof(canary));
3035         imgp->canarylen = sizeof(canary);
3036
3037         /*
3038          * Prepare the pagesizes array.
3039          */
3040         for (i = 0; i < MAXPAGESIZES; i++)
3041                 pagesizes32[i] = (uint32_t)pagesizes[i];
3042         destp -= sizeof(pagesizes32);
3043         destp = rounddown2(destp, sizeof(uint32_t));
3044         imgp->pagesizes = destp;
3045         copyout(pagesizes32, (void *)destp, sizeof(pagesizes32));
3046         imgp->pagesizeslen = sizeof(pagesizes32);
3047
3048         destp -= ARG_MAX - imgp->args->stringspace;
3049         destp = rounddown2(destp, sizeof(uint32_t));
3050
3051         /*
3052          * If we have a valid auxargs ptr, prepare some room
3053          * on the stack.
3054          */
3055         if (imgp->auxargs) {
3056                 /*
3057                  * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
3058                  * lower compatibility.
3059                  */
3060                 imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size
3061                         : (AT_COUNT * 2);
3062                 /*
3063                  * The '+ 2' is for the null pointers at the end of each of
3064                  * the arg and env vector sets,and imgp->auxarg_size is room
3065                  * for argument of Runtime loader.
3066                  */
3067                 vectp = (u_int32_t *) (destp - (imgp->args->argc +
3068                     imgp->args->envc + 2 + imgp->auxarg_size + execpath_len) *
3069                     sizeof(u_int32_t));
3070         } else {
3071                 /*
3072                  * The '+ 2' is for the null pointers at the end of each of
3073                  * the arg and env vector sets
3074                  */
3075                 vectp = (u_int32_t *)(destp - (imgp->args->argc +
3076                     imgp->args->envc + 2) * sizeof(u_int32_t));
3077         }
3078
3079         /*
3080          * vectp also becomes our initial stack base
3081          */
3082         stack_base = vectp;
3083
3084         stringp = imgp->args->begin_argv;
3085         argc = imgp->args->argc;
3086         envc = imgp->args->envc;
3087         /*
3088          * Copy out strings - arguments and environment.
3089          */
3090         copyout(stringp, (void *)destp, ARG_MAX - imgp->args->stringspace);
3091
3092         /*
3093          * Fill in "ps_strings" struct for ps, w, etc.
3094          */
3095         suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp);
3096         suword32(&arginfo->ps_nargvstr, argc);
3097
3098         /*
3099          * Fill in argument portion of vector table.
3100          */
3101         for (; argc > 0; --argc) {
3102                 suword32(vectp++, (u_int32_t)(intptr_t)destp);
3103                 while (*stringp++ != 0)
3104                         destp++;
3105                 destp++;
3106         }
3107
3108         /* a null vector table pointer separates the argp's from the envp's */
3109         suword32(vectp++, 0);
3110
3111         suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp);
3112         suword32(&arginfo->ps_nenvstr, envc);
3113
3114         /*
3115          * Fill in environment portion of vector table.
3116          */
3117         for (; envc > 0; --envc) {
3118                 suword32(vectp++, (u_int32_t)(intptr_t)destp);
3119                 while (*stringp++ != 0)
3120                         destp++;
3121                 destp++;
3122         }
3123
3124         /* end of vector table is a null pointer */
3125         suword32(vectp, 0);
3126
3127         return ((register_t *)stack_base);
3128 }
3129
3130 int
3131 freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
3132 {
3133         struct kld_file_stat stat;
3134         struct kld32_file_stat stat32;
3135         int error, version;
3136
3137         if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
3138             != 0)
3139                 return (error);
3140         if (version != sizeof(struct kld32_file_stat_1) &&
3141             version != sizeof(struct kld32_file_stat))
3142                 return (EINVAL);
3143
3144         error = kern_kldstat(td, uap->fileid, &stat);
3145         if (error != 0)
3146                 return (error);
3147
3148         bcopy(&stat.name[0], &stat32.name[0], sizeof(stat.name));
3149         CP(stat, stat32, refs);
3150         CP(stat, stat32, id);
3151         PTROUT_CP(stat, stat32, address);
3152         CP(stat, stat32, size);
3153         bcopy(&stat.pathname[0], &stat32.pathname[0], sizeof(stat.pathname));
3154         return (copyout(&stat32, uap->stat, version));
3155 }
3156
3157 int
3158 freebsd32_posix_fallocate(struct thread *td,
3159     struct freebsd32_posix_fallocate_args *uap)
3160 {
3161         int error;
3162
3163         error = kern_posix_fallocate(td, uap->fd,
3164             PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len));
3165         return (kern_posix_error(td, error));
3166 }
3167
3168 int
3169 freebsd32_posix_fadvise(struct thread *td,
3170     struct freebsd32_posix_fadvise_args *uap)
3171 {
3172         int error;
3173
3174         error = kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset),
3175             PAIR32TO64(off_t, uap->len), uap->advice);
3176         return (kern_posix_error(td, error));
3177 }
3178
3179 int
3180 convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig)
3181 {
3182
3183         CP(*sig32, *sig, sigev_notify);
3184         switch (sig->sigev_notify) {
3185         case SIGEV_NONE:
3186                 break;
3187         case SIGEV_THREAD_ID:
3188                 CP(*sig32, *sig, sigev_notify_thread_id);
3189                 /* FALLTHROUGH */
3190         case SIGEV_SIGNAL:
3191                 CP(*sig32, *sig, sigev_signo);
3192                 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3193                 break;
3194         case SIGEV_KEVENT:
3195                 CP(*sig32, *sig, sigev_notify_kqueue);
3196                 CP(*sig32, *sig, sigev_notify_kevent_flags);
3197                 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3198                 break;
3199         default:
3200                 return (EINVAL);
3201         }
3202         return (0);
3203 }
3204
3205 int
3206 freebsd32_procctl(struct thread *td, struct freebsd32_procctl_args *uap)
3207 {
3208         void *data;
3209         union {
3210                 struct procctl_reaper_status rs;
3211                 struct procctl_reaper_pids rp;
3212                 struct procctl_reaper_kill rk;
3213         } x;
3214         union {
3215                 struct procctl_reaper_pids32 rp;
3216         } x32;
3217         int error, error1, flags;
3218
3219         switch (uap->com) {
3220         case PROC_SPROTECT:
3221         case PROC_TRACE_CTL:
3222         case PROC_TRAPCAP_CTL:
3223                 error = copyin(PTRIN(uap->data), &flags, sizeof(flags));
3224                 if (error != 0)
3225                         return (error);
3226                 data = &flags;
3227                 break;
3228         case PROC_REAP_ACQUIRE:
3229         case PROC_REAP_RELEASE:
3230                 if (uap->data != NULL)
3231                         return (EINVAL);
3232                 data = NULL;
3233                 break;
3234         case PROC_REAP_STATUS:
3235                 data = &x.rs;
3236                 break;
3237         case PROC_REAP_GETPIDS:
3238                 error = copyin(uap->data, &x32.rp, sizeof(x32.rp));
3239                 if (error != 0)
3240                         return (error);
3241                 CP(x32.rp, x.rp, rp_count);
3242                 PTRIN_CP(x32.rp, x.rp, rp_pids);
3243                 data = &x.rp;
3244                 break;
3245         case PROC_REAP_KILL:
3246                 error = copyin(uap->data, &x.rk, sizeof(x.rk));
3247                 if (error != 0)
3248                         return (error);
3249                 data = &x.rk;
3250                 break;
3251         case PROC_TRACE_STATUS:
3252         case PROC_TRAPCAP_STATUS:
3253                 data = &flags;
3254                 break;
3255         default:
3256                 return (EINVAL);
3257         }
3258         error = kern_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id),
3259             uap->com, data);
3260         switch (uap->com) {
3261         case PROC_REAP_STATUS:
3262                 if (error == 0)
3263                         error = copyout(&x.rs, uap->data, sizeof(x.rs));
3264                 break;
3265         case PROC_REAP_KILL:
3266                 error1 = copyout(&x.rk, uap->data, sizeof(x.rk));
3267                 if (error == 0)
3268                         error = error1;
3269                 break;
3270         case PROC_TRACE_STATUS:
3271         case PROC_TRAPCAP_STATUS:
3272                 if (error == 0)
3273                         error = copyout(&flags, uap->data, sizeof(flags));
3274                 break;
3275         }
3276         return (error);
3277 }
3278
3279 int
3280 freebsd32_fcntl(struct thread *td, struct freebsd32_fcntl_args *uap)
3281 {
3282         long tmp;
3283
3284         switch (uap->cmd) {
3285         /*
3286          * Do unsigned conversion for arg when operation
3287          * interprets it as flags or pointer.
3288          */
3289         case F_SETLK_REMOTE:
3290         case F_SETLKW:
3291         case F_SETLK:
3292         case F_GETLK:
3293         case F_SETFD:
3294         case F_SETFL:
3295         case F_OGETLK:
3296         case F_OSETLK:
3297         case F_OSETLKW:
3298                 tmp = (unsigned int)(uap->arg);
3299                 break;
3300         default:
3301                 tmp = uap->arg;
3302                 break;
3303         }
3304         return (kern_fcntl_freebsd(td, uap->fd, uap->cmd, tmp));
3305 }
3306
3307 int
3308 freebsd32_ppoll(struct thread *td, struct freebsd32_ppoll_args *uap)
3309 {
3310         struct timespec32 ts32;
3311         struct timespec ts, *tsp;
3312         sigset_t set, *ssp;
3313         int error;
3314
3315         if (uap->ts != NULL) {
3316                 error = copyin(uap->ts, &ts32, sizeof(ts32));
3317                 if (error != 0)
3318                         return (error);
3319                 CP(ts32, ts, tv_sec);
3320                 CP(ts32, ts, tv_nsec);
3321                 tsp = &ts;
3322         } else
3323                 tsp = NULL;
3324         if (uap->set != NULL) {
3325                 error = copyin(uap->set, &set, sizeof(set));
3326                 if (error != 0)
3327                         return (error);
3328                 ssp = &set;
3329         } else
3330                 ssp = NULL;
3331
3332         return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp));
3333 }