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