]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/freebsd32/freebsd32_misc.c
Merge bmake-20130904
[FreeBSD/FreeBSD.git] / sys / compat / freebsd32 / freebsd32_misc.c
1 /*-
2  * Copyright (c) 2002 Doug Rabson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include "opt_compat.h"
31 #include "opt_inet.h"
32 #include "opt_inet6.h"
33
34 #define __ELF_WORD_SIZE 32
35
36 #include <sys/param.h>
37 #include <sys/bus.h>
38 #include <sys/capability.h>
39 #include <sys/clock.h>
40 #include <sys/exec.h>
41 #include <sys/fcntl.h>
42 #include <sys/filedesc.h>
43 #include <sys/imgact.h>
44 #include <sys/jail.h>
45 #include <sys/kernel.h>
46 #include <sys/limits.h>
47 #include <sys/linker.h>
48 #include <sys/lock.h>
49 #include <sys/malloc.h>
50 #include <sys/file.h>           /* Must come after sys/malloc.h */
51 #include <sys/imgact.h>
52 #include <sys/mbuf.h>
53 #include <sys/mman.h>
54 #include <sys/module.h>
55 #include <sys/mount.h>
56 #include <sys/mutex.h>
57 #include <sys/namei.h>
58 #include <sys/proc.h>
59 #include <sys/reboot.h>
60 #include <sys/resource.h>
61 #include <sys/resourcevar.h>
62 #include <sys/selinfo.h>
63 #include <sys/eventvar.h>       /* Must come after sys/selinfo.h */
64 #include <sys/pipe.h>           /* Must come after sys/selinfo.h */
65 #include <sys/signal.h>
66 #include <sys/signalvar.h>
67 #include <sys/socket.h>
68 #include <sys/socketvar.h>
69 #include <sys/stat.h>
70 #include <sys/syscall.h>
71 #include <sys/syscallsubr.h>
72 #include <sys/sysctl.h>
73 #include <sys/sysent.h>
74 #include <sys/sysproto.h>
75 #include <sys/systm.h>
76 #include <sys/thr.h>
77 #include <sys/unistd.h>
78 #include <sys/ucontext.h>
79 #include <sys/vnode.h>
80 #include <sys/wait.h>
81 #include <sys/ipc.h>
82 #include <sys/msg.h>
83 #include <sys/sem.h>
84 #include <sys/shm.h>
85
86 #ifdef INET
87 #include <netinet/in.h>
88 #endif
89
90 #include <vm/vm.h>
91 #include <vm/vm_param.h>
92 #include <vm/pmap.h>
93 #include <vm/vm_map.h>
94 #include <vm/vm_object.h>
95 #include <vm/vm_extern.h>
96
97 #include <machine/cpu.h>
98 #include <machine/elf.h>
99
100 #include <security/audit/audit.h>
101
102 #include <compat/freebsd32/freebsd32_util.h>
103 #include <compat/freebsd32/freebsd32.h>
104 #include <compat/freebsd32/freebsd32_ipc.h>
105 #include <compat/freebsd32/freebsd32_misc.h>
106 #include <compat/freebsd32/freebsd32_signal.h>
107 #include <compat/freebsd32/freebsd32_proto.h>
108
109 FEATURE(compat_freebsd_32bit, "Compatible with 32-bit FreeBSD");
110
111 #ifndef __mips__
112 CTASSERT(sizeof(struct timeval32) == 8);
113 CTASSERT(sizeof(struct timespec32) == 8);
114 CTASSERT(sizeof(struct itimerval32) == 16);
115 #endif
116 CTASSERT(sizeof(struct statfs32) == 256);
117 #ifndef __mips__
118 CTASSERT(sizeof(struct rusage32) == 72);
119 #endif
120 CTASSERT(sizeof(struct sigaltstack32) == 12);
121 CTASSERT(sizeof(struct kevent32) == 20);
122 CTASSERT(sizeof(struct iovec32) == 8);
123 CTASSERT(sizeof(struct msghdr32) == 28);
124 #ifndef __mips__
125 CTASSERT(sizeof(struct stat32) == 96);
126 #endif
127 CTASSERT(sizeof(struct sigaction32) == 24);
128
129 static int freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count);
130 static int freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count);
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 #ifndef __mips__
986 #define FREEBSD32_ALIGNBYTES    (sizeof(int) - 1)
987 #else
988 #define FREEBSD32_ALIGNBYTES    (sizeof(long) - 1)
989 #endif
990 #define FREEBSD32_ALIGN(p)      \
991         (((u_long)(p) + FREEBSD32_ALIGNBYTES) & ~FREEBSD32_ALIGNBYTES)
992 #define FREEBSD32_CMSG_SPACE(l) \
993         (FREEBSD32_ALIGN(sizeof(struct cmsghdr)) + FREEBSD32_ALIGN(l))
994
995 #define FREEBSD32_CMSG_DATA(cmsg)       ((unsigned char *)(cmsg) + \
996                                  FREEBSD32_ALIGN(sizeof(struct cmsghdr)))
997 static int
998 freebsd32_copy_msg_out(struct msghdr *msg, struct mbuf *control)
999 {
1000         struct cmsghdr *cm;
1001         void *data;
1002         socklen_t clen, datalen;
1003         int error;
1004         caddr_t ctlbuf;
1005         int len, maxlen, copylen;
1006         struct mbuf *m;
1007         error = 0;
1008
1009         len    = msg->msg_controllen;
1010         maxlen = msg->msg_controllen;
1011         msg->msg_controllen = 0;
1012
1013         m = control;
1014         ctlbuf = msg->msg_control;
1015       
1016         while (m && len > 0) {
1017                 cm = mtod(m, struct cmsghdr *);
1018                 clen = m->m_len;
1019
1020                 while (cm != NULL) {
1021
1022                         if (sizeof(struct cmsghdr) > clen ||
1023                             cm->cmsg_len > clen) {
1024                                 error = EINVAL;
1025                                 break;
1026                         }       
1027
1028                         data   = CMSG_DATA(cm);
1029                         datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1030
1031                         /* Adjust message length */
1032                         cm->cmsg_len = FREEBSD32_ALIGN(sizeof(struct cmsghdr)) +
1033                             datalen;
1034
1035
1036                         /* Copy cmsghdr */
1037                         copylen = sizeof(struct cmsghdr);
1038                         if (len < copylen) {
1039                                 msg->msg_flags |= MSG_CTRUNC;
1040                                 copylen = len;
1041                         }
1042
1043                         error = copyout(cm,ctlbuf,copylen);
1044                         if (error)
1045                                 goto exit;
1046
1047                         ctlbuf += FREEBSD32_ALIGN(copylen);
1048                         len    -= FREEBSD32_ALIGN(copylen);
1049
1050                         if (len <= 0)
1051                                 break;
1052
1053                         /* Copy data */
1054                         copylen = datalen;
1055                         if (len < copylen) {
1056                                 msg->msg_flags |= MSG_CTRUNC;
1057                                 copylen = len;
1058                         }
1059
1060                         error = copyout(data,ctlbuf,copylen);
1061                         if (error)
1062                                 goto exit;
1063
1064                         ctlbuf += FREEBSD32_ALIGN(copylen);
1065                         len    -= FREEBSD32_ALIGN(copylen);
1066
1067                         if (CMSG_SPACE(datalen) < clen) {
1068                                 clen -= CMSG_SPACE(datalen);
1069                                 cm = (struct cmsghdr *)
1070                                         ((caddr_t)cm + CMSG_SPACE(datalen));
1071                         } else {
1072                                 clen = 0;
1073                                 cm = NULL;
1074                         }
1075                 }       
1076                 m = m->m_next;
1077         }
1078
1079         msg->msg_controllen = (len <= 0) ? maxlen :  ctlbuf - (caddr_t)msg->msg_control;
1080         
1081 exit:
1082         return (error);
1083
1084 }
1085
1086 int
1087 freebsd32_recvmsg(td, uap)
1088         struct thread *td;
1089         struct freebsd32_recvmsg_args /* {
1090                 int     s;
1091                 struct  msghdr32 *msg;
1092                 int     flags;
1093         } */ *uap;
1094 {
1095         struct msghdr msg;
1096         struct msghdr32 m32;
1097         struct iovec *uiov, *iov;
1098         struct mbuf *control = NULL;
1099         struct mbuf **controlp;
1100
1101         int error;
1102         error = copyin(uap->msg, &m32, sizeof(m32));
1103         if (error)
1104                 return (error);
1105         error = freebsd32_copyinmsghdr(uap->msg, &msg);
1106         if (error)
1107                 return (error);
1108         error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1109             EMSGSIZE);
1110         if (error)
1111                 return (error);
1112         msg.msg_flags = uap->flags;
1113         uiov = msg.msg_iov;
1114         msg.msg_iov = iov;
1115
1116         controlp = (msg.msg_control != NULL) ?  &control : NULL;
1117         error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, controlp);
1118         if (error == 0) {
1119                 msg.msg_iov = uiov;
1120                 
1121                 if (control != NULL)
1122                         error = freebsd32_copy_msg_out(&msg, control);
1123                 else
1124                         msg.msg_controllen = 0;
1125                 
1126                 if (error == 0)
1127                         error = freebsd32_copyoutmsghdr(&msg, uap->msg);
1128         }
1129         free(iov, M_IOV);
1130
1131         if (control != NULL)
1132                 m_freem(control);
1133
1134         return (error);
1135 }
1136
1137
1138 static int
1139 freebsd32_convert_msg_in(struct mbuf **controlp)
1140 {
1141         struct mbuf *control = *controlp;
1142         struct cmsghdr *cm = mtod(control, struct cmsghdr *);
1143         void *data;
1144         socklen_t clen = control->m_len, datalen;
1145         int error;
1146
1147         error = 0;
1148         *controlp = NULL;
1149
1150         while (cm != NULL) {
1151                 if (sizeof(struct cmsghdr) > clen || cm->cmsg_len > clen) {
1152                         error = EINVAL;
1153                         break;
1154                 }
1155
1156                 data = FREEBSD32_CMSG_DATA(cm);
1157                 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1158
1159                 *controlp = sbcreatecontrol(data, datalen, cm->cmsg_type,
1160                     cm->cmsg_level);
1161                 controlp = &(*controlp)->m_next;
1162
1163                 if (FREEBSD32_CMSG_SPACE(datalen) < clen) {
1164                         clen -= FREEBSD32_CMSG_SPACE(datalen);
1165                         cm = (struct cmsghdr *)
1166                                 ((caddr_t)cm + FREEBSD32_CMSG_SPACE(datalen));
1167                 } else {
1168                         clen = 0;
1169                         cm = NULL;
1170                 }
1171         }
1172
1173         m_freem(control);
1174         return (error);
1175 }
1176
1177
1178 int
1179 freebsd32_sendmsg(struct thread *td,
1180                   struct freebsd32_sendmsg_args *uap)
1181 {
1182         struct msghdr msg;
1183         struct msghdr32 m32;
1184         struct iovec *iov;
1185         struct mbuf *control = NULL;
1186         struct sockaddr *to = NULL;
1187         int error;
1188
1189         error = copyin(uap->msg, &m32, sizeof(m32));
1190         if (error)
1191                 return (error);
1192         error = freebsd32_copyinmsghdr(uap->msg, &msg);
1193         if (error)
1194                 return (error);
1195         error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1196             EMSGSIZE);
1197         if (error)
1198                 return (error);
1199         msg.msg_iov = iov;
1200         if (msg.msg_name != NULL) {
1201                 error = getsockaddr(&to, msg.msg_name, msg.msg_namelen);
1202                 if (error) {
1203                         to = NULL;
1204                         goto out;
1205                 }
1206                 msg.msg_name = to;
1207         }
1208
1209         if (msg.msg_control) {
1210                 if (msg.msg_controllen < sizeof(struct cmsghdr)) {
1211                         error = EINVAL;
1212                         goto out;
1213                 }
1214
1215                 error = sockargs(&control, msg.msg_control,
1216                     msg.msg_controllen, MT_CONTROL);
1217                 if (error)
1218                         goto out;
1219                 
1220                 error = freebsd32_convert_msg_in(&control);
1221                 if (error)
1222                         goto out;
1223         }
1224
1225         error = kern_sendit(td, uap->s, &msg, uap->flags, control,
1226             UIO_USERSPACE);
1227
1228 out:
1229         free(iov, M_IOV);
1230         if (to)
1231                 free(to, M_SONAME);
1232         return (error);
1233 }
1234
1235 int
1236 freebsd32_recvfrom(struct thread *td,
1237                    struct freebsd32_recvfrom_args *uap)
1238 {
1239         struct msghdr msg;
1240         struct iovec aiov;
1241         int error;
1242
1243         if (uap->fromlenaddr) {
1244                 error = copyin(PTRIN(uap->fromlenaddr), &msg.msg_namelen,
1245                     sizeof(msg.msg_namelen));
1246                 if (error)
1247                         return (error);
1248         } else {
1249                 msg.msg_namelen = 0;
1250         }
1251
1252         msg.msg_name = PTRIN(uap->from);
1253         msg.msg_iov = &aiov;
1254         msg.msg_iovlen = 1;
1255         aiov.iov_base = PTRIN(uap->buf);
1256         aiov.iov_len = uap->len;
1257         msg.msg_control = NULL;
1258         msg.msg_flags = uap->flags;
1259         error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, NULL);
1260         if (error == 0 && uap->fromlenaddr)
1261                 error = copyout(&msg.msg_namelen, PTRIN(uap->fromlenaddr),
1262                     sizeof (msg.msg_namelen));
1263         return (error);
1264 }
1265
1266 int
1267 freebsd32_settimeofday(struct thread *td,
1268                        struct freebsd32_settimeofday_args *uap)
1269 {
1270         struct timeval32 tv32;
1271         struct timeval tv, *tvp;
1272         struct timezone tz, *tzp;
1273         int error;
1274
1275         if (uap->tv) {
1276                 error = copyin(uap->tv, &tv32, sizeof(tv32));
1277                 if (error)
1278                         return (error);
1279                 CP(tv32, tv, tv_sec);
1280                 CP(tv32, tv, tv_usec);
1281                 tvp = &tv;
1282         } else
1283                 tvp = NULL;
1284         if (uap->tzp) {
1285                 error = copyin(uap->tzp, &tz, sizeof(tz));
1286                 if (error)
1287                         return (error);
1288                 tzp = &tz;
1289         } else
1290                 tzp = NULL;
1291         return (kern_settimeofday(td, tvp, tzp));
1292 }
1293
1294 int
1295 freebsd32_utimes(struct thread *td, struct freebsd32_utimes_args *uap)
1296 {
1297         struct timeval32 s32[2];
1298         struct timeval s[2], *sp;
1299         int error;
1300
1301         if (uap->tptr != NULL) {
1302                 error = copyin(uap->tptr, s32, sizeof(s32));
1303                 if (error)
1304                         return (error);
1305                 CP(s32[0], s[0], tv_sec);
1306                 CP(s32[0], s[0], tv_usec);
1307                 CP(s32[1], s[1], tv_sec);
1308                 CP(s32[1], s[1], tv_usec);
1309                 sp = s;
1310         } else
1311                 sp = NULL;
1312         return (kern_utimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1313 }
1314
1315 int
1316 freebsd32_lutimes(struct thread *td, struct freebsd32_lutimes_args *uap)
1317 {
1318         struct timeval32 s32[2];
1319         struct timeval s[2], *sp;
1320         int error;
1321
1322         if (uap->tptr != NULL) {
1323                 error = copyin(uap->tptr, s32, sizeof(s32));
1324                 if (error)
1325                         return (error);
1326                 CP(s32[0], s[0], tv_sec);
1327                 CP(s32[0], s[0], tv_usec);
1328                 CP(s32[1], s[1], tv_sec);
1329                 CP(s32[1], s[1], tv_usec);
1330                 sp = s;
1331         } else
1332                 sp = NULL;
1333         return (kern_lutimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1334 }
1335
1336 int
1337 freebsd32_futimes(struct thread *td, struct freebsd32_futimes_args *uap)
1338 {
1339         struct timeval32 s32[2];
1340         struct timeval s[2], *sp;
1341         int error;
1342
1343         if (uap->tptr != NULL) {
1344                 error = copyin(uap->tptr, s32, sizeof(s32));
1345                 if (error)
1346                         return (error);
1347                 CP(s32[0], s[0], tv_sec);
1348                 CP(s32[0], s[0], tv_usec);
1349                 CP(s32[1], s[1], tv_sec);
1350                 CP(s32[1], s[1], tv_usec);
1351                 sp = s;
1352         } else
1353                 sp = NULL;
1354         return (kern_futimes(td, uap->fd, sp, UIO_SYSSPACE));
1355 }
1356
1357 int
1358 freebsd32_futimesat(struct thread *td, struct freebsd32_futimesat_args *uap)
1359 {
1360         struct timeval32 s32[2];
1361         struct timeval s[2], *sp;
1362         int error;
1363
1364         if (uap->times != NULL) {
1365                 error = copyin(uap->times, s32, sizeof(s32));
1366                 if (error)
1367                         return (error);
1368                 CP(s32[0], s[0], tv_sec);
1369                 CP(s32[0], s[0], tv_usec);
1370                 CP(s32[1], s[1], tv_sec);
1371                 CP(s32[1], s[1], tv_usec);
1372                 sp = s;
1373         } else
1374                 sp = NULL;
1375         return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
1376                 sp, UIO_SYSSPACE));
1377 }
1378
1379 int
1380 freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap)
1381 {
1382         struct timeval32 tv32;
1383         struct timeval delta, olddelta, *deltap;
1384         int error;
1385
1386         if (uap->delta) {
1387                 error = copyin(uap->delta, &tv32, sizeof(tv32));
1388                 if (error)
1389                         return (error);
1390                 CP(tv32, delta, tv_sec);
1391                 CP(tv32, delta, tv_usec);
1392                 deltap = &delta;
1393         } else
1394                 deltap = NULL;
1395         error = kern_adjtime(td, deltap, &olddelta);
1396         if (uap->olddelta && error == 0) {
1397                 CP(olddelta, tv32, tv_sec);
1398                 CP(olddelta, tv32, tv_usec);
1399                 error = copyout(&tv32, uap->olddelta, sizeof(tv32));
1400         }
1401         return (error);
1402 }
1403
1404 #ifdef COMPAT_FREEBSD4
1405 int
1406 freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap)
1407 {
1408         struct statfs32 s32;
1409         struct statfs s;
1410         int error;
1411
1412         error = kern_statfs(td, uap->path, UIO_USERSPACE, &s);
1413         if (error)
1414                 return (error);
1415         copy_statfs(&s, &s32);
1416         return (copyout(&s32, uap->buf, sizeof(s32)));
1417 }
1418 #endif
1419
1420 #ifdef COMPAT_FREEBSD4
1421 int
1422 freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap)
1423 {
1424         struct statfs32 s32;
1425         struct statfs s;
1426         int error;
1427
1428         error = kern_fstatfs(td, uap->fd, &s);
1429         if (error)
1430                 return (error);
1431         copy_statfs(&s, &s32);
1432         return (copyout(&s32, uap->buf, sizeof(s32)));
1433 }
1434 #endif
1435
1436 #ifdef COMPAT_FREEBSD4
1437 int
1438 freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap)
1439 {
1440         struct statfs32 s32;
1441         struct statfs s;
1442         fhandle_t fh;
1443         int error;
1444
1445         if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
1446                 return (error);
1447         error = kern_fhstatfs(td, fh, &s);
1448         if (error)
1449                 return (error);
1450         copy_statfs(&s, &s32);
1451         return (copyout(&s32, uap->buf, sizeof(s32)));
1452 }
1453 #endif
1454
1455 int
1456 freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap)
1457 {
1458         struct pread_args ap;
1459
1460         ap.fd = uap->fd;
1461         ap.buf = uap->buf;
1462         ap.nbyte = uap->nbyte;
1463         ap.offset = PAIR32TO64(off_t,uap->offset);
1464         return (sys_pread(td, &ap));
1465 }
1466
1467 int
1468 freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap)
1469 {
1470         struct pwrite_args ap;
1471
1472         ap.fd = uap->fd;
1473         ap.buf = uap->buf;
1474         ap.nbyte = uap->nbyte;
1475         ap.offset = PAIR32TO64(off_t,uap->offset);
1476         return (sys_pwrite(td, &ap));
1477 }
1478
1479 #ifdef COMPAT_43
1480 int
1481 ofreebsd32_lseek(struct thread *td, struct ofreebsd32_lseek_args *uap)
1482 {
1483         struct lseek_args nuap;
1484
1485         nuap.fd = uap->fd;
1486         nuap.offset = uap->offset;
1487         nuap.whence = uap->whence;
1488         return (sys_lseek(td, &nuap));
1489 }
1490 #endif
1491
1492 int
1493 freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap)
1494 {
1495         int error;
1496         struct lseek_args ap;
1497         off_t pos;
1498
1499         ap.fd = uap->fd;
1500         ap.offset = PAIR32TO64(off_t,uap->offset);
1501         ap.whence = uap->whence;
1502         error = sys_lseek(td, &ap);
1503         /* Expand the quad return into two parts for eax and edx */
1504         pos = *(off_t *)(td->td_retval);
1505         td->td_retval[RETVAL_LO] = pos & 0xffffffff;    /* %eax */
1506         td->td_retval[RETVAL_HI] = pos >> 32;           /* %edx */
1507         return error;
1508 }
1509
1510 int
1511 freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap)
1512 {
1513         struct truncate_args ap;
1514
1515         ap.path = uap->path;
1516         ap.length = PAIR32TO64(off_t,uap->length);
1517         return (sys_truncate(td, &ap));
1518 }
1519
1520 int
1521 freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap)
1522 {
1523         struct ftruncate_args ap;
1524
1525         ap.fd = uap->fd;
1526         ap.length = PAIR32TO64(off_t,uap->length);
1527         return (sys_ftruncate(td, &ap));
1528 }
1529
1530 #ifdef COMPAT_43
1531 int
1532 ofreebsd32_getdirentries(struct thread *td,
1533     struct ofreebsd32_getdirentries_args *uap)
1534 {
1535         struct ogetdirentries_args ap;
1536         int error;
1537         long loff;
1538         int32_t loff_cut;
1539
1540         ap.fd = uap->fd;
1541         ap.buf = uap->buf;
1542         ap.count = uap->count;
1543         ap.basep = NULL;
1544         error = kern_ogetdirentries(td, &ap, &loff);
1545         if (error == 0) {
1546                 loff_cut = loff;
1547                 error = copyout(&loff_cut, uap->basep, sizeof(int32_t));
1548         }
1549         return (error);
1550 }
1551 #endif
1552
1553 int
1554 freebsd32_getdirentries(struct thread *td,
1555     struct freebsd32_getdirentries_args *uap)
1556 {
1557         long base;
1558         int32_t base32;
1559         int error;
1560
1561         error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
1562             NULL, UIO_USERSPACE);
1563         if (error)
1564                 return (error);
1565         if (uap->basep != NULL) {
1566                 base32 = base;
1567                 error = copyout(&base32, uap->basep, sizeof(int32_t));
1568         }
1569         return (error);
1570 }
1571
1572 #ifdef COMPAT_FREEBSD6
1573 /* versions with the 'int pad' argument */
1574 int
1575 freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap)
1576 {
1577         struct pread_args ap;
1578
1579         ap.fd = uap->fd;
1580         ap.buf = uap->buf;
1581         ap.nbyte = uap->nbyte;
1582         ap.offset = PAIR32TO64(off_t,uap->offset);
1583         return (sys_pread(td, &ap));
1584 }
1585
1586 int
1587 freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap)
1588 {
1589         struct pwrite_args ap;
1590
1591         ap.fd = uap->fd;
1592         ap.buf = uap->buf;
1593         ap.nbyte = uap->nbyte;
1594         ap.offset = PAIR32TO64(off_t,uap->offset);
1595         return (sys_pwrite(td, &ap));
1596 }
1597
1598 int
1599 freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap)
1600 {
1601         int error;
1602         struct lseek_args ap;
1603         off_t pos;
1604
1605         ap.fd = uap->fd;
1606         ap.offset = PAIR32TO64(off_t,uap->offset);
1607         ap.whence = uap->whence;
1608         error = sys_lseek(td, &ap);
1609         /* Expand the quad return into two parts for eax and edx */
1610         pos = *(off_t *)(td->td_retval);
1611         td->td_retval[RETVAL_LO] = pos & 0xffffffff;    /* %eax */
1612         td->td_retval[RETVAL_HI] = pos >> 32;           /* %edx */
1613         return error;
1614 }
1615
1616 int
1617 freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap)
1618 {
1619         struct truncate_args ap;
1620
1621         ap.path = uap->path;
1622         ap.length = PAIR32TO64(off_t,uap->length);
1623         return (sys_truncate(td, &ap));
1624 }
1625
1626 int
1627 freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap)
1628 {
1629         struct ftruncate_args ap;
1630
1631         ap.fd = uap->fd;
1632         ap.length = PAIR32TO64(off_t,uap->length);
1633         return (sys_ftruncate(td, &ap));
1634 }
1635 #endif /* COMPAT_FREEBSD6 */
1636
1637 struct sf_hdtr32 {
1638         uint32_t headers;
1639         int hdr_cnt;
1640         uint32_t trailers;
1641         int trl_cnt;
1642 };
1643
1644 static int
1645 freebsd32_do_sendfile(struct thread *td,
1646     struct freebsd32_sendfile_args *uap, int compat)
1647 {
1648         struct sf_hdtr32 hdtr32;
1649         struct sf_hdtr hdtr;
1650         struct uio *hdr_uio, *trl_uio;
1651         struct iovec32 *iov32;
1652         struct file *fp;
1653         cap_rights_t rights;
1654         off_t offset;
1655         int error;
1656
1657         offset = PAIR32TO64(off_t, uap->offset);
1658         if (offset < 0)
1659                 return (EINVAL);
1660
1661         hdr_uio = trl_uio = NULL;
1662
1663         if (uap->hdtr != NULL) {
1664                 error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
1665                 if (error)
1666                         goto out;
1667                 PTRIN_CP(hdtr32, hdtr, headers);
1668                 CP(hdtr32, hdtr, hdr_cnt);
1669                 PTRIN_CP(hdtr32, hdtr, trailers);
1670                 CP(hdtr32, hdtr, trl_cnt);
1671
1672                 if (hdtr.headers != NULL) {
1673                         iov32 = PTRIN(hdtr32.headers);
1674                         error = freebsd32_copyinuio(iov32,
1675                             hdtr32.hdr_cnt, &hdr_uio);
1676                         if (error)
1677                                 goto out;
1678                 }
1679                 if (hdtr.trailers != NULL) {
1680                         iov32 = PTRIN(hdtr32.trailers);
1681                         error = freebsd32_copyinuio(iov32,
1682                             hdtr32.trl_cnt, &trl_uio);
1683                         if (error)
1684                                 goto out;
1685                 }
1686         }
1687
1688         AUDIT_ARG_FD(uap->fd);
1689
1690         if ((error = fget_read(td, uap->fd,
1691             cap_rights_init(&rights, CAP_PREAD), &fp)) != 0) {
1692                 goto out;
1693         }
1694
1695         error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, offset,
1696             uap->nbytes, uap->sbytes, uap->flags, compat ? SFK_COMPAT : 0, td);
1697         fdrop(fp, td);
1698
1699 out:
1700         if (hdr_uio)
1701                 free(hdr_uio, M_IOV);
1702         if (trl_uio)
1703                 free(trl_uio, M_IOV);
1704         return (error);
1705 }
1706
1707 #ifdef COMPAT_FREEBSD4
1708 int
1709 freebsd4_freebsd32_sendfile(struct thread *td,
1710     struct freebsd4_freebsd32_sendfile_args *uap)
1711 {
1712         return (freebsd32_do_sendfile(td,
1713             (struct freebsd32_sendfile_args *)uap, 1));
1714 }
1715 #endif
1716
1717 int
1718 freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
1719 {
1720
1721         return (freebsd32_do_sendfile(td, uap, 0));
1722 }
1723
1724 static void
1725 copy_stat(struct stat *in, struct stat32 *out)
1726 {
1727
1728         CP(*in, *out, st_dev);
1729         CP(*in, *out, st_ino);
1730         CP(*in, *out, st_mode);
1731         CP(*in, *out, st_nlink);
1732         CP(*in, *out, st_uid);
1733         CP(*in, *out, st_gid);
1734         CP(*in, *out, st_rdev);
1735         TS_CP(*in, *out, st_atim);
1736         TS_CP(*in, *out, st_mtim);
1737         TS_CP(*in, *out, st_ctim);
1738         CP(*in, *out, st_size);
1739         CP(*in, *out, st_blocks);
1740         CP(*in, *out, st_blksize);
1741         CP(*in, *out, st_flags);
1742         CP(*in, *out, st_gen);
1743         TS_CP(*in, *out, st_birthtim);
1744 }
1745
1746 #ifdef COMPAT_43
1747 static void
1748 copy_ostat(struct stat *in, struct ostat32 *out)
1749 {
1750
1751         CP(*in, *out, st_dev);
1752         CP(*in, *out, st_ino);
1753         CP(*in, *out, st_mode);
1754         CP(*in, *out, st_nlink);
1755         CP(*in, *out, st_uid);
1756         CP(*in, *out, st_gid);
1757         CP(*in, *out, st_rdev);
1758         CP(*in, *out, st_size);
1759         TS_CP(*in, *out, st_atim);
1760         TS_CP(*in, *out, st_mtim);
1761         TS_CP(*in, *out, st_ctim);
1762         CP(*in, *out, st_blksize);
1763         CP(*in, *out, st_blocks);
1764         CP(*in, *out, st_flags);
1765         CP(*in, *out, st_gen);
1766 }
1767 #endif
1768
1769 int
1770 freebsd32_stat(struct thread *td, struct freebsd32_stat_args *uap)
1771 {
1772         struct stat sb;
1773         struct stat32 sb32;
1774         int error;
1775
1776         error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
1777         if (error)
1778                 return (error);
1779         copy_stat(&sb, &sb32);
1780         error = copyout(&sb32, uap->ub, sizeof (sb32));
1781         return (error);
1782 }
1783
1784 #ifdef COMPAT_43
1785 int
1786 ofreebsd32_stat(struct thread *td, struct ofreebsd32_stat_args *uap)
1787 {
1788         struct stat sb;
1789         struct ostat32 sb32;
1790         int error;
1791
1792         error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
1793         if (error)
1794                 return (error);
1795         copy_ostat(&sb, &sb32);
1796         error = copyout(&sb32, uap->ub, sizeof (sb32));
1797         return (error);
1798 }
1799 #endif
1800
1801 int
1802 freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
1803 {
1804         struct stat ub;
1805         struct stat32 ub32;
1806         int error;
1807
1808         error = kern_fstat(td, uap->fd, &ub);
1809         if (error)
1810                 return (error);
1811         copy_stat(&ub, &ub32);
1812         error = copyout(&ub32, uap->ub, sizeof(ub32));
1813         return (error);
1814 }
1815
1816 #ifdef COMPAT_43
1817 int
1818 ofreebsd32_fstat(struct thread *td, struct ofreebsd32_fstat_args *uap)
1819 {
1820         struct stat ub;
1821         struct ostat32 ub32;
1822         int error;
1823
1824         error = kern_fstat(td, uap->fd, &ub);
1825         if (error)
1826                 return (error);
1827         copy_ostat(&ub, &ub32);
1828         error = copyout(&ub32, uap->ub, sizeof(ub32));
1829         return (error);
1830 }
1831 #endif
1832
1833 int
1834 freebsd32_fstatat(struct thread *td, struct freebsd32_fstatat_args *uap)
1835 {
1836         struct stat ub;
1837         struct stat32 ub32;
1838         int error;
1839
1840         error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE, &ub);
1841         if (error)
1842                 return (error);
1843         copy_stat(&ub, &ub32);
1844         error = copyout(&ub32, uap->buf, sizeof(ub32));
1845         return (error);
1846 }
1847
1848 int
1849 freebsd32_lstat(struct thread *td, struct freebsd32_lstat_args *uap)
1850 {
1851         struct stat sb;
1852         struct stat32 sb32;
1853         int error;
1854
1855         error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
1856         if (error)
1857                 return (error);
1858         copy_stat(&sb, &sb32);
1859         error = copyout(&sb32, uap->ub, sizeof (sb32));
1860         return (error);
1861 }
1862
1863 #ifdef COMPAT_43
1864 int
1865 ofreebsd32_lstat(struct thread *td, struct ofreebsd32_lstat_args *uap)
1866 {
1867         struct stat sb;
1868         struct ostat32 sb32;
1869         int error;
1870
1871         error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
1872         if (error)
1873                 return (error);
1874         copy_ostat(&sb, &sb32);
1875         error = copyout(&sb32, uap->ub, sizeof (sb32));
1876         return (error);
1877 }
1878 #endif
1879
1880 int
1881 freebsd32_sysctl(struct thread *td, struct freebsd32_sysctl_args *uap)
1882 {
1883         int error, name[CTL_MAXNAME];
1884         size_t j, oldlen;
1885
1886         if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1887                 return (EINVAL);
1888         error = copyin(uap->name, name, uap->namelen * sizeof(int));
1889         if (error)
1890                 return (error);
1891         if (uap->oldlenp)
1892                 oldlen = fuword32(uap->oldlenp);
1893         else
1894                 oldlen = 0;
1895         error = userland_sysctl(td, name, uap->namelen,
1896                 uap->old, &oldlen, 1,
1897                 uap->new, uap->newlen, &j, SCTL_MASK32);
1898         if (error && error != ENOMEM)
1899                 return (error);
1900         if (uap->oldlenp)
1901                 suword32(uap->oldlenp, j);
1902         return (0);
1903 }
1904
1905 int
1906 freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap)
1907 {
1908         uint32_t version;
1909         int error;
1910         struct jail j;
1911
1912         error = copyin(uap->jail, &version, sizeof(uint32_t));
1913         if (error)
1914                 return (error);
1915
1916         switch (version) {
1917         case 0:
1918         {
1919                 /* FreeBSD single IPv4 jails. */
1920                 struct jail32_v0 j32_v0;
1921
1922                 bzero(&j, sizeof(struct jail));
1923                 error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
1924                 if (error)
1925                         return (error);
1926                 CP(j32_v0, j, version);
1927                 PTRIN_CP(j32_v0, j, path);
1928                 PTRIN_CP(j32_v0, j, hostname);
1929                 j.ip4s = j32_v0.ip_number;
1930                 break;
1931         }
1932
1933         case 1:
1934                 /*
1935                  * Version 1 was used by multi-IPv4 jail implementations
1936                  * that never made it into the official kernel.
1937                  */
1938                 return (EINVAL);
1939
1940         case 2: /* JAIL_API_VERSION */
1941         {
1942                 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
1943                 struct jail32 j32;
1944
1945                 error = copyin(uap->jail, &j32, sizeof(struct jail32));
1946                 if (error)
1947                         return (error);
1948                 CP(j32, j, version);
1949                 PTRIN_CP(j32, j, path);
1950                 PTRIN_CP(j32, j, hostname);
1951                 PTRIN_CP(j32, j, jailname);
1952                 CP(j32, j, ip4s);
1953                 CP(j32, j, ip6s);
1954                 PTRIN_CP(j32, j, ip4);
1955                 PTRIN_CP(j32, j, ip6);
1956                 break;
1957         }
1958
1959         default:
1960                 /* Sci-Fi jails are not supported, sorry. */
1961                 return (EINVAL);
1962         }
1963         return (kern_jail(td, &j));
1964 }
1965
1966 int
1967 freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap)
1968 {
1969         struct uio *auio;
1970         int error;
1971
1972         /* Check that we have an even number of iovecs. */
1973         if (uap->iovcnt & 1)
1974                 return (EINVAL);
1975
1976         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1977         if (error)
1978                 return (error);
1979         error = kern_jail_set(td, auio, uap->flags);
1980         free(auio, M_IOV);
1981         return (error);
1982 }
1983
1984 int
1985 freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap)
1986 {
1987         struct iovec32 iov32;
1988         struct uio *auio;
1989         int error, i;
1990
1991         /* Check that we have an even number of iovecs. */
1992         if (uap->iovcnt & 1)
1993                 return (EINVAL);
1994
1995         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1996         if (error)
1997                 return (error);
1998         error = kern_jail_get(td, auio, uap->flags);
1999         if (error == 0)
2000                 for (i = 0; i < uap->iovcnt; i++) {
2001                         PTROUT_CP(auio->uio_iov[i], iov32, iov_base);
2002                         CP(auio->uio_iov[i], iov32, iov_len);
2003                         error = copyout(&iov32, uap->iovp + i, sizeof(iov32));
2004                         if (error != 0)
2005                                 break;
2006                 }
2007         free(auio, M_IOV);
2008         return (error);
2009 }
2010
2011 int
2012 freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
2013 {
2014         struct sigaction32 s32;
2015         struct sigaction sa, osa, *sap;
2016         int error;
2017
2018         if (uap->act) {
2019                 error = copyin(uap->act, &s32, sizeof(s32));
2020                 if (error)
2021                         return (error);
2022                 sa.sa_handler = PTRIN(s32.sa_u);
2023                 CP(s32, sa, sa_flags);
2024                 CP(s32, sa, sa_mask);
2025                 sap = &sa;
2026         } else
2027                 sap = NULL;
2028         error = kern_sigaction(td, uap->sig, sap, &osa, 0);
2029         if (error == 0 && uap->oact != NULL) {
2030                 s32.sa_u = PTROUT(osa.sa_handler);
2031                 CP(osa, s32, sa_flags);
2032                 CP(osa, s32, sa_mask);
2033                 error = copyout(&s32, uap->oact, sizeof(s32));
2034         }
2035         return (error);
2036 }
2037
2038 #ifdef COMPAT_FREEBSD4
2039 int
2040 freebsd4_freebsd32_sigaction(struct thread *td,
2041                              struct freebsd4_freebsd32_sigaction_args *uap)
2042 {
2043         struct sigaction32 s32;
2044         struct sigaction sa, osa, *sap;
2045         int error;
2046
2047         if (uap->act) {
2048                 error = copyin(uap->act, &s32, sizeof(s32));
2049                 if (error)
2050                         return (error);
2051                 sa.sa_handler = PTRIN(s32.sa_u);
2052                 CP(s32, sa, sa_flags);
2053                 CP(s32, sa, sa_mask);
2054                 sap = &sa;
2055         } else
2056                 sap = NULL;
2057         error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
2058         if (error == 0 && uap->oact != NULL) {
2059                 s32.sa_u = PTROUT(osa.sa_handler);
2060                 CP(osa, s32, sa_flags);
2061                 CP(osa, s32, sa_mask);
2062                 error = copyout(&s32, uap->oact, sizeof(s32));
2063         }
2064         return (error);
2065 }
2066 #endif
2067
2068 #ifdef COMPAT_43
2069 struct osigaction32 {
2070         u_int32_t       sa_u;
2071         osigset_t       sa_mask;
2072         int             sa_flags;
2073 };
2074
2075 #define ONSIG   32
2076
2077 int
2078 ofreebsd32_sigaction(struct thread *td,
2079                              struct ofreebsd32_sigaction_args *uap)
2080 {
2081         struct osigaction32 s32;
2082         struct sigaction sa, osa, *sap;
2083         int error;
2084
2085         if (uap->signum <= 0 || uap->signum >= ONSIG)
2086                 return (EINVAL);
2087
2088         if (uap->nsa) {
2089                 error = copyin(uap->nsa, &s32, sizeof(s32));
2090                 if (error)
2091                         return (error);
2092                 sa.sa_handler = PTRIN(s32.sa_u);
2093                 CP(s32, sa, sa_flags);
2094                 OSIG2SIG(s32.sa_mask, sa.sa_mask);
2095                 sap = &sa;
2096         } else
2097                 sap = NULL;
2098         error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2099         if (error == 0 && uap->osa != NULL) {
2100                 s32.sa_u = PTROUT(osa.sa_handler);
2101                 CP(osa, s32, sa_flags);
2102                 SIG2OSIG(osa.sa_mask, s32.sa_mask);
2103                 error = copyout(&s32, uap->osa, sizeof(s32));
2104         }
2105         return (error);
2106 }
2107
2108 int
2109 ofreebsd32_sigprocmask(struct thread *td,
2110                                struct ofreebsd32_sigprocmask_args *uap)
2111 {
2112         sigset_t set, oset;
2113         int error;
2114
2115         OSIG2SIG(uap->mask, set);
2116         error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD);
2117         SIG2OSIG(oset, td->td_retval[0]);
2118         return (error);
2119 }
2120
2121 int
2122 ofreebsd32_sigpending(struct thread *td,
2123                               struct ofreebsd32_sigpending_args *uap)
2124 {
2125         struct proc *p = td->td_proc;
2126         sigset_t siglist;
2127
2128         PROC_LOCK(p);
2129         siglist = p->p_siglist;
2130         SIGSETOR(siglist, td->td_siglist);
2131         PROC_UNLOCK(p);
2132         SIG2OSIG(siglist, td->td_retval[0]);
2133         return (0);
2134 }
2135
2136 struct sigvec32 {
2137         u_int32_t       sv_handler;
2138         int             sv_mask;
2139         int             sv_flags;
2140 };
2141
2142 int
2143 ofreebsd32_sigvec(struct thread *td,
2144                           struct ofreebsd32_sigvec_args *uap)
2145 {
2146         struct sigvec32 vec;
2147         struct sigaction sa, osa, *sap;
2148         int error;
2149
2150         if (uap->signum <= 0 || uap->signum >= ONSIG)
2151                 return (EINVAL);
2152
2153         if (uap->nsv) {
2154                 error = copyin(uap->nsv, &vec, sizeof(vec));
2155                 if (error)
2156                         return (error);
2157                 sa.sa_handler = PTRIN(vec.sv_handler);
2158                 OSIG2SIG(vec.sv_mask, sa.sa_mask);
2159                 sa.sa_flags = vec.sv_flags;
2160                 sa.sa_flags ^= SA_RESTART;
2161                 sap = &sa;
2162         } else
2163                 sap = NULL;
2164         error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2165         if (error == 0 && uap->osv != NULL) {
2166                 vec.sv_handler = PTROUT(osa.sa_handler);
2167                 SIG2OSIG(osa.sa_mask, vec.sv_mask);
2168                 vec.sv_flags = osa.sa_flags;
2169                 vec.sv_flags &= ~SA_NOCLDWAIT;
2170                 vec.sv_flags ^= SA_RESTART;
2171                 error = copyout(&vec, uap->osv, sizeof(vec));
2172         }
2173         return (error);
2174 }
2175
2176 int
2177 ofreebsd32_sigblock(struct thread *td,
2178                             struct ofreebsd32_sigblock_args *uap)
2179 {
2180         sigset_t set, oset;
2181
2182         OSIG2SIG(uap->mask, set);
2183         kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
2184         SIG2OSIG(oset, td->td_retval[0]);
2185         return (0);
2186 }
2187
2188 int
2189 ofreebsd32_sigsetmask(struct thread *td,
2190                               struct ofreebsd32_sigsetmask_args *uap)
2191 {
2192         sigset_t set, oset;
2193
2194         OSIG2SIG(uap->mask, set);
2195         kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
2196         SIG2OSIG(oset, td->td_retval[0]);
2197         return (0);
2198 }
2199
2200 int
2201 ofreebsd32_sigsuspend(struct thread *td,
2202                               struct ofreebsd32_sigsuspend_args *uap)
2203 {
2204         sigset_t mask;
2205
2206         OSIG2SIG(uap->mask, mask);
2207         return (kern_sigsuspend(td, mask));
2208 }
2209
2210 struct sigstack32 {
2211         u_int32_t       ss_sp;
2212         int             ss_onstack;
2213 };
2214
2215 int
2216 ofreebsd32_sigstack(struct thread *td,
2217                             struct ofreebsd32_sigstack_args *uap)
2218 {
2219         struct sigstack32 s32;
2220         struct sigstack nss, oss;
2221         int error = 0, unss;
2222
2223         if (uap->nss != NULL) {
2224                 error = copyin(uap->nss, &s32, sizeof(s32));
2225                 if (error)
2226                         return (error);
2227                 nss.ss_sp = PTRIN(s32.ss_sp);
2228                 CP(s32, nss, ss_onstack);
2229                 unss = 1;
2230         } else {
2231                 unss = 0;
2232         }
2233         oss.ss_sp = td->td_sigstk.ss_sp;
2234         oss.ss_onstack = sigonstack(cpu_getstack(td));
2235         if (unss) {
2236                 td->td_sigstk.ss_sp = nss.ss_sp;
2237                 td->td_sigstk.ss_size = 0;
2238                 td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2239                 td->td_pflags |= TDP_ALTSTACK;
2240         }
2241         if (uap->oss != NULL) {
2242                 s32.ss_sp = PTROUT(oss.ss_sp);
2243                 CP(oss, s32, ss_onstack);
2244                 error = copyout(&s32, uap->oss, sizeof(s32));
2245         }
2246         return (error);
2247 }
2248 #endif
2249
2250 int
2251 freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2252 {
2253         struct timespec32 rmt32, rqt32;
2254         struct timespec rmt, rqt;
2255         int error;
2256
2257         error = copyin(uap->rqtp, &rqt32, sizeof(rqt32));
2258         if (error)
2259                 return (error);
2260
2261         CP(rqt32, rqt, tv_sec);
2262         CP(rqt32, rqt, tv_nsec);
2263
2264         if (uap->rmtp &&
2265             !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
2266                 return (EFAULT);
2267         error = kern_nanosleep(td, &rqt, &rmt);
2268         if (error && uap->rmtp) {
2269                 int error2;
2270
2271                 CP(rmt, rmt32, tv_sec);
2272                 CP(rmt, rmt32, tv_nsec);
2273
2274                 error2 = copyout(&rmt32, uap->rmtp, sizeof(rmt32));
2275                 if (error2)
2276                         error = error2;
2277         }
2278         return (error);
2279 }
2280
2281 int
2282 freebsd32_clock_gettime(struct thread *td,
2283                         struct freebsd32_clock_gettime_args *uap)
2284 {
2285         struct timespec ats;
2286         struct timespec32 ats32;
2287         int error;
2288
2289         error = kern_clock_gettime(td, uap->clock_id, &ats);
2290         if (error == 0) {
2291                 CP(ats, ats32, tv_sec);
2292                 CP(ats, ats32, tv_nsec);
2293                 error = copyout(&ats32, uap->tp, sizeof(ats32));
2294         }
2295         return (error);
2296 }
2297
2298 int
2299 freebsd32_clock_settime(struct thread *td,
2300                         struct freebsd32_clock_settime_args *uap)
2301 {
2302         struct timespec ats;
2303         struct timespec32 ats32;
2304         int error;
2305
2306         error = copyin(uap->tp, &ats32, sizeof(ats32));
2307         if (error)
2308                 return (error);
2309         CP(ats32, ats, tv_sec);
2310         CP(ats32, ats, tv_nsec);
2311
2312         return (kern_clock_settime(td, uap->clock_id, &ats));
2313 }
2314
2315 int
2316 freebsd32_clock_getres(struct thread *td,
2317                        struct freebsd32_clock_getres_args *uap)
2318 {
2319         struct timespec ts;
2320         struct timespec32 ts32;
2321         int error;
2322
2323         if (uap->tp == NULL)
2324                 return (0);
2325         error = kern_clock_getres(td, uap->clock_id, &ts);
2326         if (error == 0) {
2327                 CP(ts, ts32, tv_sec);
2328                 CP(ts, ts32, tv_nsec);
2329                 error = copyout(&ts32, uap->tp, sizeof(ts32));
2330         }
2331         return (error);
2332 }
2333
2334 int freebsd32_ktimer_create(struct thread *td,
2335     struct freebsd32_ktimer_create_args *uap)
2336 {
2337         struct sigevent32 ev32;
2338         struct sigevent ev, *evp;
2339         int error, id;
2340
2341         if (uap->evp == NULL) {
2342                 evp = NULL;
2343         } else {
2344                 evp = &ev;
2345                 error = copyin(uap->evp, &ev32, sizeof(ev32));
2346                 if (error != 0)
2347                         return (error);
2348                 error = convert_sigevent32(&ev32, &ev);
2349                 if (error != 0)
2350                         return (error);
2351         }
2352         error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
2353         if (error == 0) {
2354                 error = copyout(&id, uap->timerid, sizeof(int));
2355                 if (error != 0)
2356                         kern_ktimer_delete(td, id);
2357         }
2358         return (error);
2359 }
2360
2361 int
2362 freebsd32_ktimer_settime(struct thread *td,
2363     struct freebsd32_ktimer_settime_args *uap)
2364 {
2365         struct itimerspec32 val32, oval32;
2366         struct itimerspec val, oval, *ovalp;
2367         int error;
2368
2369         error = copyin(uap->value, &val32, sizeof(val32));
2370         if (error != 0)
2371                 return (error);
2372         ITS_CP(val32, val);
2373         ovalp = uap->ovalue != NULL ? &oval : NULL;
2374         error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
2375         if (error == 0 && uap->ovalue != NULL) {
2376                 ITS_CP(oval, oval32);
2377                 error = copyout(&oval32, uap->ovalue, sizeof(oval32));
2378         }
2379         return (error);
2380 }
2381
2382 int
2383 freebsd32_ktimer_gettime(struct thread *td,
2384     struct freebsd32_ktimer_gettime_args *uap)
2385 {
2386         struct itimerspec32 val32;
2387         struct itimerspec val;
2388         int error;
2389
2390         error = kern_ktimer_gettime(td, uap->timerid, &val);
2391         if (error == 0) {
2392                 ITS_CP(val, val32);
2393                 error = copyout(&val32, uap->value, sizeof(val32));
2394         }
2395         return (error);
2396 }
2397
2398 int
2399 freebsd32_clock_getcpuclockid2(struct thread *td,
2400     struct freebsd32_clock_getcpuclockid2_args *uap)
2401 {
2402         clockid_t clk_id;
2403         int error;
2404
2405         error = kern_clock_getcpuclockid2(td, PAIR32TO64(id_t, uap->id),
2406             uap->which, &clk_id);
2407         if (error == 0)
2408                 error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
2409         return (error);
2410 }
2411
2412 int
2413 freebsd32_thr_new(struct thread *td,
2414                   struct freebsd32_thr_new_args *uap)
2415 {
2416         struct thr_param32 param32;
2417         struct thr_param param;
2418         int error;
2419
2420         if (uap->param_size < 0 ||
2421             uap->param_size > sizeof(struct thr_param32))
2422                 return (EINVAL);
2423         bzero(&param, sizeof(struct thr_param));
2424         bzero(&param32, sizeof(struct thr_param32));
2425         error = copyin(uap->param, &param32, uap->param_size);
2426         if (error != 0)
2427                 return (error);
2428         param.start_func = PTRIN(param32.start_func);
2429         param.arg = PTRIN(param32.arg);
2430         param.stack_base = PTRIN(param32.stack_base);
2431         param.stack_size = param32.stack_size;
2432         param.tls_base = PTRIN(param32.tls_base);
2433         param.tls_size = param32.tls_size;
2434         param.child_tid = PTRIN(param32.child_tid);
2435         param.parent_tid = PTRIN(param32.parent_tid);
2436         param.flags = param32.flags;
2437         param.rtp = PTRIN(param32.rtp);
2438         param.spare[0] = PTRIN(param32.spare[0]);
2439         param.spare[1] = PTRIN(param32.spare[1]);
2440         param.spare[2] = PTRIN(param32.spare[2]);
2441
2442         return (kern_thr_new(td, &param));
2443 }
2444
2445 int
2446 freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
2447 {
2448         struct timespec32 ts32;
2449         struct timespec ts, *tsp;
2450         int error;
2451
2452         error = 0;
2453         tsp = NULL;
2454         if (uap->timeout != NULL) {
2455                 error = copyin((const void *)uap->timeout, (void *)&ts32,
2456                     sizeof(struct timespec32));
2457                 if (error != 0)
2458                         return (error);
2459                 ts.tv_sec = ts32.tv_sec;
2460                 ts.tv_nsec = ts32.tv_nsec;
2461                 tsp = &ts;
2462         }
2463         return (kern_thr_suspend(td, tsp));
2464 }
2465
2466 void
2467 siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst)
2468 {
2469         bzero(dst, sizeof(*dst));
2470         dst->si_signo = src->si_signo;
2471         dst->si_errno = src->si_errno;
2472         dst->si_code = src->si_code;
2473         dst->si_pid = src->si_pid;
2474         dst->si_uid = src->si_uid;
2475         dst->si_status = src->si_status;
2476         dst->si_addr = (uintptr_t)src->si_addr;
2477         dst->si_value.sival_int = src->si_value.sival_int;
2478         dst->si_timerid = src->si_timerid;
2479         dst->si_overrun = src->si_overrun;
2480 }
2481
2482 int
2483 freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
2484 {
2485         struct timespec32 ts32;
2486         struct timespec ts;
2487         struct timespec *timeout;
2488         sigset_t set;
2489         ksiginfo_t ksi;
2490         struct siginfo32 si32;
2491         int error;
2492
2493         if (uap->timeout) {
2494                 error = copyin(uap->timeout, &ts32, sizeof(ts32));
2495                 if (error)
2496                         return (error);
2497                 ts.tv_sec = ts32.tv_sec;
2498                 ts.tv_nsec = ts32.tv_nsec;
2499                 timeout = &ts;
2500         } else
2501                 timeout = NULL;
2502
2503         error = copyin(uap->set, &set, sizeof(set));
2504         if (error)
2505                 return (error);
2506
2507         error = kern_sigtimedwait(td, set, &ksi, timeout);
2508         if (error)
2509                 return (error);
2510
2511         if (uap->info) {
2512                 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2513                 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2514         }
2515
2516         if (error == 0)
2517                 td->td_retval[0] = ksi.ksi_signo;
2518         return (error);
2519 }
2520
2521 /*
2522  * MPSAFE
2523  */
2524 int
2525 freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
2526 {
2527         ksiginfo_t ksi;
2528         struct siginfo32 si32;
2529         sigset_t set;
2530         int error;
2531
2532         error = copyin(uap->set, &set, sizeof(set));
2533         if (error)
2534                 return (error);
2535
2536         error = kern_sigtimedwait(td, set, &ksi, NULL);
2537         if (error)
2538                 return (error);
2539
2540         if (uap->info) {
2541                 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2542                 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2543         }       
2544         if (error == 0)
2545                 td->td_retval[0] = ksi.ksi_signo;
2546         return (error);
2547 }
2548
2549 int
2550 freebsd32_cpuset_setid(struct thread *td,
2551     struct freebsd32_cpuset_setid_args *uap)
2552 {
2553         struct cpuset_setid_args ap;
2554
2555         ap.which = uap->which;
2556         ap.id = PAIR32TO64(id_t,uap->id);
2557         ap.setid = uap->setid;
2558
2559         return (sys_cpuset_setid(td, &ap));
2560 }
2561
2562 int
2563 freebsd32_cpuset_getid(struct thread *td,
2564     struct freebsd32_cpuset_getid_args *uap)
2565 {
2566         struct cpuset_getid_args ap;
2567
2568         ap.level = uap->level;
2569         ap.which = uap->which;
2570         ap.id = PAIR32TO64(id_t,uap->id);
2571         ap.setid = uap->setid;
2572
2573         return (sys_cpuset_getid(td, &ap));
2574 }
2575
2576 int
2577 freebsd32_cpuset_getaffinity(struct thread *td,
2578     struct freebsd32_cpuset_getaffinity_args *uap)
2579 {
2580         struct cpuset_getaffinity_args ap;
2581
2582         ap.level = uap->level;
2583         ap.which = uap->which;
2584         ap.id = PAIR32TO64(id_t,uap->id);
2585         ap.cpusetsize = uap->cpusetsize;
2586         ap.mask = uap->mask;
2587
2588         return (sys_cpuset_getaffinity(td, &ap));
2589 }
2590
2591 int
2592 freebsd32_cpuset_setaffinity(struct thread *td,
2593     struct freebsd32_cpuset_setaffinity_args *uap)
2594 {
2595         struct cpuset_setaffinity_args ap;
2596
2597         ap.level = uap->level;
2598         ap.which = uap->which;
2599         ap.id = PAIR32TO64(id_t,uap->id);
2600         ap.cpusetsize = uap->cpusetsize;
2601         ap.mask = uap->mask;
2602
2603         return (sys_cpuset_setaffinity(td, &ap));
2604 }
2605
2606 int
2607 freebsd32_nmount(struct thread *td,
2608     struct freebsd32_nmount_args /* {
2609         struct iovec *iovp;
2610         unsigned int iovcnt;
2611         int flags;
2612     } */ *uap)
2613 {
2614         struct uio *auio;
2615         uint64_t flags;
2616         int error;
2617
2618         /*
2619          * Mount flags are now 64-bits. On 32-bit archtectures only
2620          * 32-bits are passed in, but from here on everything handles
2621          * 64-bit flags correctly.
2622          */
2623         flags = uap->flags;
2624
2625         AUDIT_ARG_FFLAGS(flags);
2626
2627         /*
2628          * Filter out MNT_ROOTFS.  We do not want clients of nmount() in
2629          * userspace to set this flag, but we must filter it out if we want
2630          * MNT_UPDATE on the root file system to work.
2631          * MNT_ROOTFS should only be set by the kernel when mounting its
2632          * root file system.
2633          */
2634         flags &= ~MNT_ROOTFS;
2635
2636         /*
2637          * check that we have an even number of iovec's
2638          * and that we have at least two options.
2639          */
2640         if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
2641                 return (EINVAL);
2642
2643         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2644         if (error)
2645                 return (error);
2646         error = vfs_donmount(td, flags, auio);
2647
2648         free(auio, M_IOV);
2649         return error;
2650 }
2651
2652 #if 0
2653 int
2654 freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
2655 {
2656         struct yyy32 *p32, s32;
2657         struct yyy *p = NULL, s;
2658         struct xxx_arg ap;
2659         int error;
2660
2661         if (uap->zzz) {
2662                 error = copyin(uap->zzz, &s32, sizeof(s32));
2663                 if (error)
2664                         return (error);
2665                 /* translate in */
2666                 p = &s;
2667         }
2668         error = kern_xxx(td, p);
2669         if (error)
2670                 return (error);
2671         if (uap->zzz) {
2672                 /* translate out */
2673                 error = copyout(&s32, p32, sizeof(s32));
2674         }
2675         return (error);
2676 }
2677 #endif
2678
2679 int
2680 syscall32_register(int *offset, struct sysent *new_sysent,
2681     struct sysent *old_sysent)
2682 {
2683         if (*offset == NO_SYSCALL) {
2684                 int i;
2685
2686                 for (i = 1; i < SYS_MAXSYSCALL; ++i)
2687                         if (freebsd32_sysent[i].sy_call ==
2688                             (sy_call_t *)lkmnosys)
2689                                 break;
2690                 if (i == SYS_MAXSYSCALL)
2691                         return (ENFILE);
2692                 *offset = i;
2693         } else if (*offset < 0 || *offset >= SYS_MAXSYSCALL)
2694                 return (EINVAL);
2695         else if (freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmnosys &&
2696             freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmressys)
2697                 return (EEXIST);
2698
2699         *old_sysent = freebsd32_sysent[*offset];
2700         freebsd32_sysent[*offset] = *new_sysent;
2701         return 0;
2702 }
2703
2704 int
2705 syscall32_deregister(int *offset, struct sysent *old_sysent)
2706 {
2707
2708         if (*offset)
2709                 freebsd32_sysent[*offset] = *old_sysent;
2710         return 0;
2711 }
2712
2713 int
2714 syscall32_module_handler(struct module *mod, int what, void *arg)
2715 {
2716         struct syscall_module_data *data = (struct syscall_module_data*)arg;
2717         modspecific_t ms;
2718         int error;
2719
2720         switch (what) {
2721         case MOD_LOAD:
2722                 error = syscall32_register(data->offset, data->new_sysent,
2723                     &data->old_sysent);
2724                 if (error) {
2725                         /* Leave a mark so we know to safely unload below. */
2726                         data->offset = NULL;
2727                         return error;
2728                 }
2729                 ms.intval = *data->offset;
2730                 MOD_XLOCK;
2731                 module_setspecific(mod, &ms);
2732                 MOD_XUNLOCK;
2733                 if (data->chainevh)
2734                         error = data->chainevh(mod, what, data->chainarg);
2735                 return (error);
2736         case MOD_UNLOAD:
2737                 /*
2738                  * MOD_LOAD failed, so just return without calling the
2739                  * chained handler since we didn't pass along the MOD_LOAD
2740                  * event.
2741                  */
2742                 if (data->offset == NULL)
2743                         return (0);
2744                 if (data->chainevh) {
2745                         error = data->chainevh(mod, what, data->chainarg);
2746                         if (error)
2747                                 return (error);
2748                 }
2749                 error = syscall32_deregister(data->offset, &data->old_sysent);
2750                 return (error);
2751         default:
2752                 error = EOPNOTSUPP;
2753                 if (data->chainevh)
2754                         error = data->chainevh(mod, what, data->chainarg);
2755                 return (error);
2756         }
2757 }
2758
2759 int
2760 syscall32_helper_register(struct syscall_helper_data *sd)
2761 {
2762         struct syscall_helper_data *sd1;
2763         int error;
2764
2765         for (sd1 = sd; sd1->syscall_no != NO_SYSCALL; sd1++) {
2766                 error = syscall32_register(&sd1->syscall_no, &sd1->new_sysent,
2767                     &sd1->old_sysent);
2768                 if (error != 0) {
2769                         syscall32_helper_unregister(sd);
2770                         return (error);
2771                 }
2772                 sd1->registered = 1;
2773         }
2774         return (0);
2775 }
2776
2777 int
2778 syscall32_helper_unregister(struct syscall_helper_data *sd)
2779 {
2780         struct syscall_helper_data *sd1;
2781
2782         for (sd1 = sd; sd1->registered != 0; sd1++) {
2783                 syscall32_deregister(&sd1->syscall_no, &sd1->old_sysent);
2784                 sd1->registered = 0;
2785         }
2786         return (0);
2787 }
2788
2789 register_t *
2790 freebsd32_copyout_strings(struct image_params *imgp)
2791 {
2792         int argc, envc, i;
2793         u_int32_t *vectp;
2794         char *stringp, *destp;
2795         u_int32_t *stack_base;
2796         struct freebsd32_ps_strings *arginfo;
2797         char canary[sizeof(long) * 8];
2798         int32_t pagesizes32[MAXPAGESIZES];
2799         size_t execpath_len;
2800         int szsigcode;
2801
2802         /*
2803          * Calculate string base and vector table pointers.
2804          * Also deal with signal trampoline code for this exec type.
2805          */
2806         if (imgp->execpath != NULL && imgp->auxargs != NULL)
2807                 execpath_len = strlen(imgp->execpath) + 1;
2808         else
2809                 execpath_len = 0;
2810         arginfo = (struct freebsd32_ps_strings *)curproc->p_sysent->
2811             sv_psstrings;
2812         if (imgp->proc->p_sysent->sv_sigcode_base == 0)
2813                 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
2814         else
2815                 szsigcode = 0;
2816         destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
2817             roundup(execpath_len, sizeof(char *)) -
2818             roundup(sizeof(canary), sizeof(char *)) -
2819             roundup(sizeof(pagesizes32), sizeof(char *)) -
2820             roundup((ARG_MAX - imgp->args->stringspace), sizeof(char *));
2821
2822         /*
2823          * install sigcode
2824          */
2825         if (szsigcode != 0)
2826                 copyout(imgp->proc->p_sysent->sv_sigcode,
2827                         ((caddr_t)arginfo - szsigcode), szsigcode);
2828
2829         /*
2830          * Copy the image path for the rtld.
2831          */
2832         if (execpath_len != 0) {
2833                 imgp->execpathp = (uintptr_t)arginfo - szsigcode - execpath_len;
2834                 copyout(imgp->execpath, (void *)imgp->execpathp,
2835                     execpath_len);
2836         }
2837
2838         /*
2839          * Prepare the canary for SSP.
2840          */
2841         arc4rand(canary, sizeof(canary), 0);
2842         imgp->canary = (uintptr_t)arginfo - szsigcode - execpath_len -
2843             sizeof(canary);
2844         copyout(canary, (void *)imgp->canary, sizeof(canary));
2845         imgp->canarylen = sizeof(canary);
2846
2847         /*
2848          * Prepare the pagesizes array.
2849          */
2850         for (i = 0; i < MAXPAGESIZES; i++)
2851                 pagesizes32[i] = (uint32_t)pagesizes[i];
2852         imgp->pagesizes = (uintptr_t)arginfo - szsigcode - execpath_len -
2853             roundup(sizeof(canary), sizeof(char *)) - sizeof(pagesizes32);
2854         copyout(pagesizes32, (void *)imgp->pagesizes, sizeof(pagesizes32));
2855         imgp->pagesizeslen = sizeof(pagesizes32);
2856
2857         /*
2858          * If we have a valid auxargs ptr, prepare some room
2859          * on the stack.
2860          */
2861         if (imgp->auxargs) {
2862                 /*
2863                  * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
2864                  * lower compatibility.
2865                  */
2866                 imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size
2867                         : (AT_COUNT * 2);
2868                 /*
2869                  * The '+ 2' is for the null pointers at the end of each of
2870                  * the arg and env vector sets,and imgp->auxarg_size is room
2871                  * for argument of Runtime loader.
2872                  */
2873                 vectp = (u_int32_t *) (destp - (imgp->args->argc +
2874                     imgp->args->envc + 2 + imgp->auxarg_size + execpath_len) *
2875                     sizeof(u_int32_t));
2876         } else
2877                 /*
2878                  * The '+ 2' is for the null pointers at the end of each of
2879                  * the arg and env vector sets
2880                  */
2881                 vectp = (u_int32_t *)
2882                         (destp - (imgp->args->argc + imgp->args->envc + 2) * sizeof(u_int32_t));
2883
2884         /*
2885          * vectp also becomes our initial stack base
2886          */
2887         stack_base = vectp;
2888
2889         stringp = imgp->args->begin_argv;
2890         argc = imgp->args->argc;
2891         envc = imgp->args->envc;
2892         /*
2893          * Copy out strings - arguments and environment.
2894          */
2895         copyout(stringp, destp, ARG_MAX - imgp->args->stringspace);
2896
2897         /*
2898          * Fill in "ps_strings" struct for ps, w, etc.
2899          */
2900         suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp);
2901         suword32(&arginfo->ps_nargvstr, argc);
2902
2903         /*
2904          * Fill in argument portion of vector table.
2905          */
2906         for (; argc > 0; --argc) {
2907                 suword32(vectp++, (u_int32_t)(intptr_t)destp);
2908                 while (*stringp++ != 0)
2909                         destp++;
2910                 destp++;
2911         }
2912
2913         /* a null vector table pointer separates the argp's from the envp's */
2914         suword32(vectp++, 0);
2915
2916         suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp);
2917         suword32(&arginfo->ps_nenvstr, envc);
2918
2919         /*
2920          * Fill in environment portion of vector table.
2921          */
2922         for (; envc > 0; --envc) {
2923                 suword32(vectp++, (u_int32_t)(intptr_t)destp);
2924                 while (*stringp++ != 0)
2925                         destp++;
2926                 destp++;
2927         }
2928
2929         /* end of vector table is a null pointer */
2930         suword32(vectp, 0);
2931
2932         return ((register_t *)stack_base);
2933 }
2934
2935 int
2936 freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
2937 {
2938         struct kld_file_stat stat;
2939         struct kld32_file_stat stat32;
2940         int error, version;
2941
2942         if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
2943             != 0)
2944                 return (error);
2945         if (version != sizeof(struct kld32_file_stat_1) &&
2946             version != sizeof(struct kld32_file_stat))
2947                 return (EINVAL);
2948
2949         error = kern_kldstat(td, uap->fileid, &stat);
2950         if (error != 0)
2951                 return (error);
2952
2953         bcopy(&stat.name[0], &stat32.name[0], sizeof(stat.name));
2954         CP(stat, stat32, refs);
2955         CP(stat, stat32, id);
2956         PTROUT_CP(stat, stat32, address);
2957         CP(stat, stat32, size);
2958         bcopy(&stat.pathname[0], &stat32.pathname[0], sizeof(stat.pathname));
2959         return (copyout(&stat32, uap->stat, version));
2960 }
2961
2962 int
2963 freebsd32_posix_fallocate(struct thread *td,
2964     struct freebsd32_posix_fallocate_args *uap)
2965 {
2966
2967         return (kern_posix_fallocate(td, uap->fd,
2968             PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len)));
2969 }
2970
2971 int
2972 freebsd32_posix_fadvise(struct thread *td,
2973     struct freebsd32_posix_fadvise_args *uap)
2974 {
2975
2976         return (kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset),
2977             PAIR32TO64(off_t, uap->len), uap->advice));
2978 }
2979
2980 int
2981 convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig)
2982 {
2983
2984         CP(*sig32, *sig, sigev_notify);
2985         switch (sig->sigev_notify) {
2986         case SIGEV_NONE:
2987                 break;
2988         case SIGEV_THREAD_ID:
2989                 CP(*sig32, *sig, sigev_notify_thread_id);
2990                 /* FALLTHROUGH */
2991         case SIGEV_SIGNAL:
2992                 CP(*sig32, *sig, sigev_signo);
2993                 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
2994                 break;
2995         case SIGEV_KEVENT:
2996                 CP(*sig32, *sig, sigev_notify_kqueue);
2997                 CP(*sig32, *sig, sigev_notify_kevent_flags);
2998                 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
2999                 break;
3000         default:
3001                 return (EINVAL);
3002         }
3003         return (0);
3004 }