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