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