]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/freebsd32/freebsd32_misc.c
Merge ACPICA 20130823.
[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         off_t offset;
1654         int error;
1655
1656         offset = PAIR32TO64(off_t, uap->offset);
1657         if (offset < 0)
1658                 return (EINVAL);
1659
1660         hdr_uio = trl_uio = NULL;
1661
1662         if (uap->hdtr != NULL) {
1663                 error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
1664                 if (error)
1665                         goto out;
1666                 PTRIN_CP(hdtr32, hdtr, headers);
1667                 CP(hdtr32, hdtr, hdr_cnt);
1668                 PTRIN_CP(hdtr32, hdtr, trailers);
1669                 CP(hdtr32, hdtr, trl_cnt);
1670
1671                 if (hdtr.headers != NULL) {
1672                         iov32 = PTRIN(hdtr32.headers);
1673                         error = freebsd32_copyinuio(iov32,
1674                             hdtr32.hdr_cnt, &hdr_uio);
1675                         if (error)
1676                                 goto out;
1677                 }
1678                 if (hdtr.trailers != NULL) {
1679                         iov32 = PTRIN(hdtr32.trailers);
1680                         error = freebsd32_copyinuio(iov32,
1681                             hdtr32.trl_cnt, &trl_uio);
1682                         if (error)
1683                                 goto out;
1684                 }
1685         }
1686
1687         AUDIT_ARG_FD(uap->fd);
1688
1689         if ((error = fget_read(td, uap->fd, CAP_PREAD, &fp)) != 0)
1690                 goto out;
1691
1692         error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, offset,
1693             uap->nbytes, uap->sbytes, uap->flags, compat ? SFK_COMPAT : 0, td);
1694         fdrop(fp, td);
1695
1696 out:
1697         if (hdr_uio)
1698                 free(hdr_uio, M_IOV);
1699         if (trl_uio)
1700                 free(trl_uio, M_IOV);
1701         return (error);
1702 }
1703
1704 #ifdef COMPAT_FREEBSD4
1705 int
1706 freebsd4_freebsd32_sendfile(struct thread *td,
1707     struct freebsd4_freebsd32_sendfile_args *uap)
1708 {
1709         return (freebsd32_do_sendfile(td,
1710             (struct freebsd32_sendfile_args *)uap, 1));
1711 }
1712 #endif
1713
1714 int
1715 freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
1716 {
1717
1718         return (freebsd32_do_sendfile(td, uap, 0));
1719 }
1720
1721 static void
1722 copy_stat(struct stat *in, struct stat32 *out)
1723 {
1724
1725         CP(*in, *out, st_dev);
1726         CP(*in, *out, st_ino);
1727         CP(*in, *out, st_mode);
1728         CP(*in, *out, st_nlink);
1729         CP(*in, *out, st_uid);
1730         CP(*in, *out, st_gid);
1731         CP(*in, *out, st_rdev);
1732         TS_CP(*in, *out, st_atim);
1733         TS_CP(*in, *out, st_mtim);
1734         TS_CP(*in, *out, st_ctim);
1735         CP(*in, *out, st_size);
1736         CP(*in, *out, st_blocks);
1737         CP(*in, *out, st_blksize);
1738         CP(*in, *out, st_flags);
1739         CP(*in, *out, st_gen);
1740         TS_CP(*in, *out, st_birthtim);
1741 }
1742
1743 #ifdef COMPAT_43
1744 static void
1745 copy_ostat(struct stat *in, struct ostat32 *out)
1746 {
1747
1748         CP(*in, *out, st_dev);
1749         CP(*in, *out, st_ino);
1750         CP(*in, *out, st_mode);
1751         CP(*in, *out, st_nlink);
1752         CP(*in, *out, st_uid);
1753         CP(*in, *out, st_gid);
1754         CP(*in, *out, st_rdev);
1755         CP(*in, *out, st_size);
1756         TS_CP(*in, *out, st_atim);
1757         TS_CP(*in, *out, st_mtim);
1758         TS_CP(*in, *out, st_ctim);
1759         CP(*in, *out, st_blksize);
1760         CP(*in, *out, st_blocks);
1761         CP(*in, *out, st_flags);
1762         CP(*in, *out, st_gen);
1763 }
1764 #endif
1765
1766 int
1767 freebsd32_stat(struct thread *td, struct freebsd32_stat_args *uap)
1768 {
1769         struct stat sb;
1770         struct stat32 sb32;
1771         int error;
1772
1773         error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
1774         if (error)
1775                 return (error);
1776         copy_stat(&sb, &sb32);
1777         error = copyout(&sb32, uap->ub, sizeof (sb32));
1778         return (error);
1779 }
1780
1781 #ifdef COMPAT_43
1782 int
1783 ofreebsd32_stat(struct thread *td, struct ofreebsd32_stat_args *uap)
1784 {
1785         struct stat sb;
1786         struct ostat32 sb32;
1787         int error;
1788
1789         error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
1790         if (error)
1791                 return (error);
1792         copy_ostat(&sb, &sb32);
1793         error = copyout(&sb32, uap->ub, sizeof (sb32));
1794         return (error);
1795 }
1796 #endif
1797
1798 int
1799 freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
1800 {
1801         struct stat ub;
1802         struct stat32 ub32;
1803         int error;
1804
1805         error = kern_fstat(td, uap->fd, &ub);
1806         if (error)
1807                 return (error);
1808         copy_stat(&ub, &ub32);
1809         error = copyout(&ub32, uap->ub, sizeof(ub32));
1810         return (error);
1811 }
1812
1813 #ifdef COMPAT_43
1814 int
1815 ofreebsd32_fstat(struct thread *td, struct ofreebsd32_fstat_args *uap)
1816 {
1817         struct stat ub;
1818         struct ostat32 ub32;
1819         int error;
1820
1821         error = kern_fstat(td, uap->fd, &ub);
1822         if (error)
1823                 return (error);
1824         copy_ostat(&ub, &ub32);
1825         error = copyout(&ub32, uap->ub, sizeof(ub32));
1826         return (error);
1827 }
1828 #endif
1829
1830 int
1831 freebsd32_fstatat(struct thread *td, struct freebsd32_fstatat_args *uap)
1832 {
1833         struct stat ub;
1834         struct stat32 ub32;
1835         int error;
1836
1837         error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE, &ub);
1838         if (error)
1839                 return (error);
1840         copy_stat(&ub, &ub32);
1841         error = copyout(&ub32, uap->buf, sizeof(ub32));
1842         return (error);
1843 }
1844
1845 int
1846 freebsd32_lstat(struct thread *td, struct freebsd32_lstat_args *uap)
1847 {
1848         struct stat sb;
1849         struct stat32 sb32;
1850         int error;
1851
1852         error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
1853         if (error)
1854                 return (error);
1855         copy_stat(&sb, &sb32);
1856         error = copyout(&sb32, uap->ub, sizeof (sb32));
1857         return (error);
1858 }
1859
1860 #ifdef COMPAT_43
1861 int
1862 ofreebsd32_lstat(struct thread *td, struct ofreebsd32_lstat_args *uap)
1863 {
1864         struct stat sb;
1865         struct ostat32 sb32;
1866         int error;
1867
1868         error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
1869         if (error)
1870                 return (error);
1871         copy_ostat(&sb, &sb32);
1872         error = copyout(&sb32, uap->ub, sizeof (sb32));
1873         return (error);
1874 }
1875 #endif
1876
1877 int
1878 freebsd32_sysctl(struct thread *td, struct freebsd32_sysctl_args *uap)
1879 {
1880         int error, name[CTL_MAXNAME];
1881         size_t j, oldlen;
1882
1883         if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1884                 return (EINVAL);
1885         error = copyin(uap->name, name, uap->namelen * sizeof(int));
1886         if (error)
1887                 return (error);
1888         if (uap->oldlenp)
1889                 oldlen = fuword32(uap->oldlenp);
1890         else
1891                 oldlen = 0;
1892         error = userland_sysctl(td, name, uap->namelen,
1893                 uap->old, &oldlen, 1,
1894                 uap->new, uap->newlen, &j, SCTL_MASK32);
1895         if (error && error != ENOMEM)
1896                 return (error);
1897         if (uap->oldlenp)
1898                 suword32(uap->oldlenp, j);
1899         return (0);
1900 }
1901
1902 int
1903 freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap)
1904 {
1905         uint32_t version;
1906         int error;
1907         struct jail j;
1908
1909         error = copyin(uap->jail, &version, sizeof(uint32_t));
1910         if (error)
1911                 return (error);
1912
1913         switch (version) {
1914         case 0:
1915         {
1916                 /* FreeBSD single IPv4 jails. */
1917                 struct jail32_v0 j32_v0;
1918
1919                 bzero(&j, sizeof(struct jail));
1920                 error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
1921                 if (error)
1922                         return (error);
1923                 CP(j32_v0, j, version);
1924                 PTRIN_CP(j32_v0, j, path);
1925                 PTRIN_CP(j32_v0, j, hostname);
1926                 j.ip4s = j32_v0.ip_number;
1927                 break;
1928         }
1929
1930         case 1:
1931                 /*
1932                  * Version 1 was used by multi-IPv4 jail implementations
1933                  * that never made it into the official kernel.
1934                  */
1935                 return (EINVAL);
1936
1937         case 2: /* JAIL_API_VERSION */
1938         {
1939                 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
1940                 struct jail32 j32;
1941
1942                 error = copyin(uap->jail, &j32, sizeof(struct jail32));
1943                 if (error)
1944                         return (error);
1945                 CP(j32, j, version);
1946                 PTRIN_CP(j32, j, path);
1947                 PTRIN_CP(j32, j, hostname);
1948                 PTRIN_CP(j32, j, jailname);
1949                 CP(j32, j, ip4s);
1950                 CP(j32, j, ip6s);
1951                 PTRIN_CP(j32, j, ip4);
1952                 PTRIN_CP(j32, j, ip6);
1953                 break;
1954         }
1955
1956         default:
1957                 /* Sci-Fi jails are not supported, sorry. */
1958                 return (EINVAL);
1959         }
1960         return (kern_jail(td, &j));
1961 }
1962
1963 int
1964 freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap)
1965 {
1966         struct uio *auio;
1967         int error;
1968
1969         /* Check that we have an even number of iovecs. */
1970         if (uap->iovcnt & 1)
1971                 return (EINVAL);
1972
1973         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1974         if (error)
1975                 return (error);
1976         error = kern_jail_set(td, auio, uap->flags);
1977         free(auio, M_IOV);
1978         return (error);
1979 }
1980
1981 int
1982 freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap)
1983 {
1984         struct iovec32 iov32;
1985         struct uio *auio;
1986         int error, i;
1987
1988         /* Check that we have an even number of iovecs. */
1989         if (uap->iovcnt & 1)
1990                 return (EINVAL);
1991
1992         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1993         if (error)
1994                 return (error);
1995         error = kern_jail_get(td, auio, uap->flags);
1996         if (error == 0)
1997                 for (i = 0; i < uap->iovcnt; i++) {
1998                         PTROUT_CP(auio->uio_iov[i], iov32, iov_base);
1999                         CP(auio->uio_iov[i], iov32, iov_len);
2000                         error = copyout(&iov32, uap->iovp + i, sizeof(iov32));
2001                         if (error != 0)
2002                                 break;
2003                 }
2004         free(auio, M_IOV);
2005         return (error);
2006 }
2007
2008 int
2009 freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
2010 {
2011         struct sigaction32 s32;
2012         struct sigaction sa, osa, *sap;
2013         int error;
2014
2015         if (uap->act) {
2016                 error = copyin(uap->act, &s32, sizeof(s32));
2017                 if (error)
2018                         return (error);
2019                 sa.sa_handler = PTRIN(s32.sa_u);
2020                 CP(s32, sa, sa_flags);
2021                 CP(s32, sa, sa_mask);
2022                 sap = &sa;
2023         } else
2024                 sap = NULL;
2025         error = kern_sigaction(td, uap->sig, sap, &osa, 0);
2026         if (error == 0 && uap->oact != NULL) {
2027                 s32.sa_u = PTROUT(osa.sa_handler);
2028                 CP(osa, s32, sa_flags);
2029                 CP(osa, s32, sa_mask);
2030                 error = copyout(&s32, uap->oact, sizeof(s32));
2031         }
2032         return (error);
2033 }
2034
2035 #ifdef COMPAT_FREEBSD4
2036 int
2037 freebsd4_freebsd32_sigaction(struct thread *td,
2038                              struct freebsd4_freebsd32_sigaction_args *uap)
2039 {
2040         struct sigaction32 s32;
2041         struct sigaction sa, osa, *sap;
2042         int error;
2043
2044         if (uap->act) {
2045                 error = copyin(uap->act, &s32, sizeof(s32));
2046                 if (error)
2047                         return (error);
2048                 sa.sa_handler = PTRIN(s32.sa_u);
2049                 CP(s32, sa, sa_flags);
2050                 CP(s32, sa, sa_mask);
2051                 sap = &sa;
2052         } else
2053                 sap = NULL;
2054         error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
2055         if (error == 0 && uap->oact != NULL) {
2056                 s32.sa_u = PTROUT(osa.sa_handler);
2057                 CP(osa, s32, sa_flags);
2058                 CP(osa, s32, sa_mask);
2059                 error = copyout(&s32, uap->oact, sizeof(s32));
2060         }
2061         return (error);
2062 }
2063 #endif
2064
2065 #ifdef COMPAT_43
2066 struct osigaction32 {
2067         u_int32_t       sa_u;
2068         osigset_t       sa_mask;
2069         int             sa_flags;
2070 };
2071
2072 #define ONSIG   32
2073
2074 int
2075 ofreebsd32_sigaction(struct thread *td,
2076                              struct ofreebsd32_sigaction_args *uap)
2077 {
2078         struct osigaction32 s32;
2079         struct sigaction sa, osa, *sap;
2080         int error;
2081
2082         if (uap->signum <= 0 || uap->signum >= ONSIG)
2083                 return (EINVAL);
2084
2085         if (uap->nsa) {
2086                 error = copyin(uap->nsa, &s32, sizeof(s32));
2087                 if (error)
2088                         return (error);
2089                 sa.sa_handler = PTRIN(s32.sa_u);
2090                 CP(s32, sa, sa_flags);
2091                 OSIG2SIG(s32.sa_mask, sa.sa_mask);
2092                 sap = &sa;
2093         } else
2094                 sap = NULL;
2095         error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2096         if (error == 0 && uap->osa != NULL) {
2097                 s32.sa_u = PTROUT(osa.sa_handler);
2098                 CP(osa, s32, sa_flags);
2099                 SIG2OSIG(osa.sa_mask, s32.sa_mask);
2100                 error = copyout(&s32, uap->osa, sizeof(s32));
2101         }
2102         return (error);
2103 }
2104
2105 int
2106 ofreebsd32_sigprocmask(struct thread *td,
2107                                struct ofreebsd32_sigprocmask_args *uap)
2108 {
2109         sigset_t set, oset;
2110         int error;
2111
2112         OSIG2SIG(uap->mask, set);
2113         error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD);
2114         SIG2OSIG(oset, td->td_retval[0]);
2115         return (error);
2116 }
2117
2118 int
2119 ofreebsd32_sigpending(struct thread *td,
2120                               struct ofreebsd32_sigpending_args *uap)
2121 {
2122         struct proc *p = td->td_proc;
2123         sigset_t siglist;
2124
2125         PROC_LOCK(p);
2126         siglist = p->p_siglist;
2127         SIGSETOR(siglist, td->td_siglist);
2128         PROC_UNLOCK(p);
2129         SIG2OSIG(siglist, td->td_retval[0]);
2130         return (0);
2131 }
2132
2133 struct sigvec32 {
2134         u_int32_t       sv_handler;
2135         int             sv_mask;
2136         int             sv_flags;
2137 };
2138
2139 int
2140 ofreebsd32_sigvec(struct thread *td,
2141                           struct ofreebsd32_sigvec_args *uap)
2142 {
2143         struct sigvec32 vec;
2144         struct sigaction sa, osa, *sap;
2145         int error;
2146
2147         if (uap->signum <= 0 || uap->signum >= ONSIG)
2148                 return (EINVAL);
2149
2150         if (uap->nsv) {
2151                 error = copyin(uap->nsv, &vec, sizeof(vec));
2152                 if (error)
2153                         return (error);
2154                 sa.sa_handler = PTRIN(vec.sv_handler);
2155                 OSIG2SIG(vec.sv_mask, sa.sa_mask);
2156                 sa.sa_flags = vec.sv_flags;
2157                 sa.sa_flags ^= SA_RESTART;
2158                 sap = &sa;
2159         } else
2160                 sap = NULL;
2161         error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2162         if (error == 0 && uap->osv != NULL) {
2163                 vec.sv_handler = PTROUT(osa.sa_handler);
2164                 SIG2OSIG(osa.sa_mask, vec.sv_mask);
2165                 vec.sv_flags = osa.sa_flags;
2166                 vec.sv_flags &= ~SA_NOCLDWAIT;
2167                 vec.sv_flags ^= SA_RESTART;
2168                 error = copyout(&vec, uap->osv, sizeof(vec));
2169         }
2170         return (error);
2171 }
2172
2173 int
2174 ofreebsd32_sigblock(struct thread *td,
2175                             struct ofreebsd32_sigblock_args *uap)
2176 {
2177         sigset_t set, oset;
2178
2179         OSIG2SIG(uap->mask, set);
2180         kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
2181         SIG2OSIG(oset, td->td_retval[0]);
2182         return (0);
2183 }
2184
2185 int
2186 ofreebsd32_sigsetmask(struct thread *td,
2187                               struct ofreebsd32_sigsetmask_args *uap)
2188 {
2189         sigset_t set, oset;
2190
2191         OSIG2SIG(uap->mask, set);
2192         kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
2193         SIG2OSIG(oset, td->td_retval[0]);
2194         return (0);
2195 }
2196
2197 int
2198 ofreebsd32_sigsuspend(struct thread *td,
2199                               struct ofreebsd32_sigsuspend_args *uap)
2200 {
2201         sigset_t mask;
2202
2203         OSIG2SIG(uap->mask, mask);
2204         return (kern_sigsuspend(td, mask));
2205 }
2206
2207 struct sigstack32 {
2208         u_int32_t       ss_sp;
2209         int             ss_onstack;
2210 };
2211
2212 int
2213 ofreebsd32_sigstack(struct thread *td,
2214                             struct ofreebsd32_sigstack_args *uap)
2215 {
2216         struct sigstack32 s32;
2217         struct sigstack nss, oss;
2218         int error = 0, unss;
2219
2220         if (uap->nss != NULL) {
2221                 error = copyin(uap->nss, &s32, sizeof(s32));
2222                 if (error)
2223                         return (error);
2224                 nss.ss_sp = PTRIN(s32.ss_sp);
2225                 CP(s32, nss, ss_onstack);
2226                 unss = 1;
2227         } else {
2228                 unss = 0;
2229         }
2230         oss.ss_sp = td->td_sigstk.ss_sp;
2231         oss.ss_onstack = sigonstack(cpu_getstack(td));
2232         if (unss) {
2233                 td->td_sigstk.ss_sp = nss.ss_sp;
2234                 td->td_sigstk.ss_size = 0;
2235                 td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2236                 td->td_pflags |= TDP_ALTSTACK;
2237         }
2238         if (uap->oss != NULL) {
2239                 s32.ss_sp = PTROUT(oss.ss_sp);
2240                 CP(oss, s32, ss_onstack);
2241                 error = copyout(&s32, uap->oss, sizeof(s32));
2242         }
2243         return (error);
2244 }
2245 #endif
2246
2247 int
2248 freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2249 {
2250         struct timespec32 rmt32, rqt32;
2251         struct timespec rmt, rqt;
2252         int error;
2253
2254         error = copyin(uap->rqtp, &rqt32, sizeof(rqt32));
2255         if (error)
2256                 return (error);
2257
2258         CP(rqt32, rqt, tv_sec);
2259         CP(rqt32, rqt, tv_nsec);
2260
2261         if (uap->rmtp &&
2262             !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
2263                 return (EFAULT);
2264         error = kern_nanosleep(td, &rqt, &rmt);
2265         if (error && uap->rmtp) {
2266                 int error2;
2267
2268                 CP(rmt, rmt32, tv_sec);
2269                 CP(rmt, rmt32, tv_nsec);
2270
2271                 error2 = copyout(&rmt32, uap->rmtp, sizeof(rmt32));
2272                 if (error2)
2273                         error = error2;
2274         }
2275         return (error);
2276 }
2277
2278 int
2279 freebsd32_clock_gettime(struct thread *td,
2280                         struct freebsd32_clock_gettime_args *uap)
2281 {
2282         struct timespec ats;
2283         struct timespec32 ats32;
2284         int error;
2285
2286         error = kern_clock_gettime(td, uap->clock_id, &ats);
2287         if (error == 0) {
2288                 CP(ats, ats32, tv_sec);
2289                 CP(ats, ats32, tv_nsec);
2290                 error = copyout(&ats32, uap->tp, sizeof(ats32));
2291         }
2292         return (error);
2293 }
2294
2295 int
2296 freebsd32_clock_settime(struct thread *td,
2297                         struct freebsd32_clock_settime_args *uap)
2298 {
2299         struct timespec ats;
2300         struct timespec32 ats32;
2301         int error;
2302
2303         error = copyin(uap->tp, &ats32, sizeof(ats32));
2304         if (error)
2305                 return (error);
2306         CP(ats32, ats, tv_sec);
2307         CP(ats32, ats, tv_nsec);
2308
2309         return (kern_clock_settime(td, uap->clock_id, &ats));
2310 }
2311
2312 int
2313 freebsd32_clock_getres(struct thread *td,
2314                        struct freebsd32_clock_getres_args *uap)
2315 {
2316         struct timespec ts;
2317         struct timespec32 ts32;
2318         int error;
2319
2320         if (uap->tp == NULL)
2321                 return (0);
2322         error = kern_clock_getres(td, uap->clock_id, &ts);
2323         if (error == 0) {
2324                 CP(ts, ts32, tv_sec);
2325                 CP(ts, ts32, tv_nsec);
2326                 error = copyout(&ts32, uap->tp, sizeof(ts32));
2327         }
2328         return (error);
2329 }
2330
2331 int freebsd32_ktimer_create(struct thread *td,
2332     struct freebsd32_ktimer_create_args *uap)
2333 {
2334         struct sigevent32 ev32;
2335         struct sigevent ev, *evp;
2336         int error, id;
2337
2338         if (uap->evp == NULL) {
2339                 evp = NULL;
2340         } else {
2341                 evp = &ev;
2342                 error = copyin(uap->evp, &ev32, sizeof(ev32));
2343                 if (error != 0)
2344                         return (error);
2345                 error = convert_sigevent32(&ev32, &ev);
2346                 if (error != 0)
2347                         return (error);
2348         }
2349         error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
2350         if (error == 0) {
2351                 error = copyout(&id, uap->timerid, sizeof(int));
2352                 if (error != 0)
2353                         kern_ktimer_delete(td, id);
2354         }
2355         return (error);
2356 }
2357
2358 int
2359 freebsd32_ktimer_settime(struct thread *td,
2360     struct freebsd32_ktimer_settime_args *uap)
2361 {
2362         struct itimerspec32 val32, oval32;
2363         struct itimerspec val, oval, *ovalp;
2364         int error;
2365
2366         error = copyin(uap->value, &val32, sizeof(val32));
2367         if (error != 0)
2368                 return (error);
2369         ITS_CP(val32, val);
2370         ovalp = uap->ovalue != NULL ? &oval : NULL;
2371         error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
2372         if (error == 0 && uap->ovalue != NULL) {
2373                 ITS_CP(oval, oval32);
2374                 error = copyout(&oval32, uap->ovalue, sizeof(oval32));
2375         }
2376         return (error);
2377 }
2378
2379 int
2380 freebsd32_ktimer_gettime(struct thread *td,
2381     struct freebsd32_ktimer_gettime_args *uap)
2382 {
2383         struct itimerspec32 val32;
2384         struct itimerspec val;
2385         int error;
2386
2387         error = kern_ktimer_gettime(td, uap->timerid, &val);
2388         if (error == 0) {
2389                 ITS_CP(val, val32);
2390                 error = copyout(&val32, uap->value, sizeof(val32));
2391         }
2392         return (error);
2393 }
2394
2395 int
2396 freebsd32_clock_getcpuclockid2(struct thread *td,
2397     struct freebsd32_clock_getcpuclockid2_args *uap)
2398 {
2399         clockid_t clk_id;
2400         int error;
2401
2402         error = kern_clock_getcpuclockid2(td, PAIR32TO64(id_t, uap->id),
2403             uap->which, &clk_id);
2404         if (error == 0)
2405                 error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
2406         return (error);
2407 }
2408
2409 int
2410 freebsd32_thr_new(struct thread *td,
2411                   struct freebsd32_thr_new_args *uap)
2412 {
2413         struct thr_param32 param32;
2414         struct thr_param param;
2415         int error;
2416
2417         if (uap->param_size < 0 ||
2418             uap->param_size > sizeof(struct thr_param32))
2419                 return (EINVAL);
2420         bzero(&param, sizeof(struct thr_param));
2421         bzero(&param32, sizeof(struct thr_param32));
2422         error = copyin(uap->param, &param32, uap->param_size);
2423         if (error != 0)
2424                 return (error);
2425         param.start_func = PTRIN(param32.start_func);
2426         param.arg = PTRIN(param32.arg);
2427         param.stack_base = PTRIN(param32.stack_base);
2428         param.stack_size = param32.stack_size;
2429         param.tls_base = PTRIN(param32.tls_base);
2430         param.tls_size = param32.tls_size;
2431         param.child_tid = PTRIN(param32.child_tid);
2432         param.parent_tid = PTRIN(param32.parent_tid);
2433         param.flags = param32.flags;
2434         param.rtp = PTRIN(param32.rtp);
2435         param.spare[0] = PTRIN(param32.spare[0]);
2436         param.spare[1] = PTRIN(param32.spare[1]);
2437         param.spare[2] = PTRIN(param32.spare[2]);
2438
2439         return (kern_thr_new(td, &param));
2440 }
2441
2442 int
2443 freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
2444 {
2445         struct timespec32 ts32;
2446         struct timespec ts, *tsp;
2447         int error;
2448
2449         error = 0;
2450         tsp = NULL;
2451         if (uap->timeout != NULL) {
2452                 error = copyin((const void *)uap->timeout, (void *)&ts32,
2453                     sizeof(struct timespec32));
2454                 if (error != 0)
2455                         return (error);
2456                 ts.tv_sec = ts32.tv_sec;
2457                 ts.tv_nsec = ts32.tv_nsec;
2458                 tsp = &ts;
2459         }
2460         return (kern_thr_suspend(td, tsp));
2461 }
2462
2463 void
2464 siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst)
2465 {
2466         bzero(dst, sizeof(*dst));
2467         dst->si_signo = src->si_signo;
2468         dst->si_errno = src->si_errno;
2469         dst->si_code = src->si_code;
2470         dst->si_pid = src->si_pid;
2471         dst->si_uid = src->si_uid;
2472         dst->si_status = src->si_status;
2473         dst->si_addr = (uintptr_t)src->si_addr;
2474         dst->si_value.sival_int = src->si_value.sival_int;
2475         dst->si_timerid = src->si_timerid;
2476         dst->si_overrun = src->si_overrun;
2477 }
2478
2479 int
2480 freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
2481 {
2482         struct timespec32 ts32;
2483         struct timespec ts;
2484         struct timespec *timeout;
2485         sigset_t set;
2486         ksiginfo_t ksi;
2487         struct siginfo32 si32;
2488         int error;
2489
2490         if (uap->timeout) {
2491                 error = copyin(uap->timeout, &ts32, sizeof(ts32));
2492                 if (error)
2493                         return (error);
2494                 ts.tv_sec = ts32.tv_sec;
2495                 ts.tv_nsec = ts32.tv_nsec;
2496                 timeout = &ts;
2497         } else
2498                 timeout = NULL;
2499
2500         error = copyin(uap->set, &set, sizeof(set));
2501         if (error)
2502                 return (error);
2503
2504         error = kern_sigtimedwait(td, set, &ksi, timeout);
2505         if (error)
2506                 return (error);
2507
2508         if (uap->info) {
2509                 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2510                 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2511         }
2512
2513         if (error == 0)
2514                 td->td_retval[0] = ksi.ksi_signo;
2515         return (error);
2516 }
2517
2518 /*
2519  * MPSAFE
2520  */
2521 int
2522 freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
2523 {
2524         ksiginfo_t ksi;
2525         struct siginfo32 si32;
2526         sigset_t set;
2527         int error;
2528
2529         error = copyin(uap->set, &set, sizeof(set));
2530         if (error)
2531                 return (error);
2532
2533         error = kern_sigtimedwait(td, set, &ksi, NULL);
2534         if (error)
2535                 return (error);
2536
2537         if (uap->info) {
2538                 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2539                 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2540         }       
2541         if (error == 0)
2542                 td->td_retval[0] = ksi.ksi_signo;
2543         return (error);
2544 }
2545
2546 int
2547 freebsd32_cpuset_setid(struct thread *td,
2548     struct freebsd32_cpuset_setid_args *uap)
2549 {
2550         struct cpuset_setid_args ap;
2551
2552         ap.which = uap->which;
2553         ap.id = PAIR32TO64(id_t,uap->id);
2554         ap.setid = uap->setid;
2555
2556         return (sys_cpuset_setid(td, &ap));
2557 }
2558
2559 int
2560 freebsd32_cpuset_getid(struct thread *td,
2561     struct freebsd32_cpuset_getid_args *uap)
2562 {
2563         struct cpuset_getid_args ap;
2564
2565         ap.level = uap->level;
2566         ap.which = uap->which;
2567         ap.id = PAIR32TO64(id_t,uap->id);
2568         ap.setid = uap->setid;
2569
2570         return (sys_cpuset_getid(td, &ap));
2571 }
2572
2573 int
2574 freebsd32_cpuset_getaffinity(struct thread *td,
2575     struct freebsd32_cpuset_getaffinity_args *uap)
2576 {
2577         struct cpuset_getaffinity_args ap;
2578
2579         ap.level = uap->level;
2580         ap.which = uap->which;
2581         ap.id = PAIR32TO64(id_t,uap->id);
2582         ap.cpusetsize = uap->cpusetsize;
2583         ap.mask = uap->mask;
2584
2585         return (sys_cpuset_getaffinity(td, &ap));
2586 }
2587
2588 int
2589 freebsd32_cpuset_setaffinity(struct thread *td,
2590     struct freebsd32_cpuset_setaffinity_args *uap)
2591 {
2592         struct cpuset_setaffinity_args ap;
2593
2594         ap.level = uap->level;
2595         ap.which = uap->which;
2596         ap.id = PAIR32TO64(id_t,uap->id);
2597         ap.cpusetsize = uap->cpusetsize;
2598         ap.mask = uap->mask;
2599
2600         return (sys_cpuset_setaffinity(td, &ap));
2601 }
2602
2603 int
2604 freebsd32_nmount(struct thread *td,
2605     struct freebsd32_nmount_args /* {
2606         struct iovec *iovp;
2607         unsigned int iovcnt;
2608         int flags;
2609     } */ *uap)
2610 {
2611         struct uio *auio;
2612         uint64_t flags;
2613         int error;
2614
2615         /*
2616          * Mount flags are now 64-bits. On 32-bit archtectures only
2617          * 32-bits are passed in, but from here on everything handles
2618          * 64-bit flags correctly.
2619          */
2620         flags = uap->flags;
2621
2622         AUDIT_ARG_FFLAGS(flags);
2623
2624         /*
2625          * Filter out MNT_ROOTFS.  We do not want clients of nmount() in
2626          * userspace to set this flag, but we must filter it out if we want
2627          * MNT_UPDATE on the root file system to work.
2628          * MNT_ROOTFS should only be set by the kernel when mounting its
2629          * root file system.
2630          */
2631         flags &= ~MNT_ROOTFS;
2632
2633         /*
2634          * check that we have an even number of iovec's
2635          * and that we have at least two options.
2636          */
2637         if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
2638                 return (EINVAL);
2639
2640         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2641         if (error)
2642                 return (error);
2643         error = vfs_donmount(td, flags, auio);
2644
2645         free(auio, M_IOV);
2646         return error;
2647 }
2648
2649 #if 0
2650 int
2651 freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
2652 {
2653         struct yyy32 *p32, s32;
2654         struct yyy *p = NULL, s;
2655         struct xxx_arg ap;
2656         int error;
2657
2658         if (uap->zzz) {
2659                 error = copyin(uap->zzz, &s32, sizeof(s32));
2660                 if (error)
2661                         return (error);
2662                 /* translate in */
2663                 p = &s;
2664         }
2665         error = kern_xxx(td, p);
2666         if (error)
2667                 return (error);
2668         if (uap->zzz) {
2669                 /* translate out */
2670                 error = copyout(&s32, p32, sizeof(s32));
2671         }
2672         return (error);
2673 }
2674 #endif
2675
2676 int
2677 syscall32_register(int *offset, struct sysent *new_sysent,
2678     struct sysent *old_sysent)
2679 {
2680         if (*offset == NO_SYSCALL) {
2681                 int i;
2682
2683                 for (i = 1; i < SYS_MAXSYSCALL; ++i)
2684                         if (freebsd32_sysent[i].sy_call ==
2685                             (sy_call_t *)lkmnosys)
2686                                 break;
2687                 if (i == SYS_MAXSYSCALL)
2688                         return (ENFILE);
2689                 *offset = i;
2690         } else if (*offset < 0 || *offset >= SYS_MAXSYSCALL)
2691                 return (EINVAL);
2692         else if (freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmnosys &&
2693             freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmressys)
2694                 return (EEXIST);
2695
2696         *old_sysent = freebsd32_sysent[*offset];
2697         freebsd32_sysent[*offset] = *new_sysent;
2698         return 0;
2699 }
2700
2701 int
2702 syscall32_deregister(int *offset, struct sysent *old_sysent)
2703 {
2704
2705         if (*offset)
2706                 freebsd32_sysent[*offset] = *old_sysent;
2707         return 0;
2708 }
2709
2710 int
2711 syscall32_module_handler(struct module *mod, int what, void *arg)
2712 {
2713         struct syscall_module_data *data = (struct syscall_module_data*)arg;
2714         modspecific_t ms;
2715         int error;
2716
2717         switch (what) {
2718         case MOD_LOAD:
2719                 error = syscall32_register(data->offset, data->new_sysent,
2720                     &data->old_sysent);
2721                 if (error) {
2722                         /* Leave a mark so we know to safely unload below. */
2723                         data->offset = NULL;
2724                         return error;
2725                 }
2726                 ms.intval = *data->offset;
2727                 MOD_XLOCK;
2728                 module_setspecific(mod, &ms);
2729                 MOD_XUNLOCK;
2730                 if (data->chainevh)
2731                         error = data->chainevh(mod, what, data->chainarg);
2732                 return (error);
2733         case MOD_UNLOAD:
2734                 /*
2735                  * MOD_LOAD failed, so just return without calling the
2736                  * chained handler since we didn't pass along the MOD_LOAD
2737                  * event.
2738                  */
2739                 if (data->offset == NULL)
2740                         return (0);
2741                 if (data->chainevh) {
2742                         error = data->chainevh(mod, what, data->chainarg);
2743                         if (error)
2744                                 return (error);
2745                 }
2746                 error = syscall32_deregister(data->offset, &data->old_sysent);
2747                 return (error);
2748         default:
2749                 error = EOPNOTSUPP;
2750                 if (data->chainevh)
2751                         error = data->chainevh(mod, what, data->chainarg);
2752                 return (error);
2753         }
2754 }
2755
2756 int
2757 syscall32_helper_register(struct syscall_helper_data *sd)
2758 {
2759         struct syscall_helper_data *sd1;
2760         int error;
2761
2762         for (sd1 = sd; sd1->syscall_no != NO_SYSCALL; sd1++) {
2763                 error = syscall32_register(&sd1->syscall_no, &sd1->new_sysent,
2764                     &sd1->old_sysent);
2765                 if (error != 0) {
2766                         syscall32_helper_unregister(sd);
2767                         return (error);
2768                 }
2769                 sd1->registered = 1;
2770         }
2771         return (0);
2772 }
2773
2774 int
2775 syscall32_helper_unregister(struct syscall_helper_data *sd)
2776 {
2777         struct syscall_helper_data *sd1;
2778
2779         for (sd1 = sd; sd1->registered != 0; sd1++) {
2780                 syscall32_deregister(&sd1->syscall_no, &sd1->old_sysent);
2781                 sd1->registered = 0;
2782         }
2783         return (0);
2784 }
2785
2786 register_t *
2787 freebsd32_copyout_strings(struct image_params *imgp)
2788 {
2789         int argc, envc, i;
2790         u_int32_t *vectp;
2791         char *stringp, *destp;
2792         u_int32_t *stack_base;
2793         struct freebsd32_ps_strings *arginfo;
2794         char canary[sizeof(long) * 8];
2795         int32_t pagesizes32[MAXPAGESIZES];
2796         size_t execpath_len;
2797         int szsigcode;
2798
2799         /*
2800          * Calculate string base and vector table pointers.
2801          * Also deal with signal trampoline code for this exec type.
2802          */
2803         if (imgp->execpath != NULL && imgp->auxargs != NULL)
2804                 execpath_len = strlen(imgp->execpath) + 1;
2805         else
2806                 execpath_len = 0;
2807         arginfo = (struct freebsd32_ps_strings *)curproc->p_sysent->
2808             sv_psstrings;
2809         if (imgp->proc->p_sysent->sv_sigcode_base == 0)
2810                 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
2811         else
2812                 szsigcode = 0;
2813         destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
2814             roundup(execpath_len, sizeof(char *)) -
2815             roundup(sizeof(canary), sizeof(char *)) -
2816             roundup(sizeof(pagesizes32), sizeof(char *)) -
2817             roundup((ARG_MAX - imgp->args->stringspace), sizeof(char *));
2818
2819         /*
2820          * install sigcode
2821          */
2822         if (szsigcode != 0)
2823                 copyout(imgp->proc->p_sysent->sv_sigcode,
2824                         ((caddr_t)arginfo - szsigcode), szsigcode);
2825
2826         /*
2827          * Copy the image path for the rtld.
2828          */
2829         if (execpath_len != 0) {
2830                 imgp->execpathp = (uintptr_t)arginfo - szsigcode - execpath_len;
2831                 copyout(imgp->execpath, (void *)imgp->execpathp,
2832                     execpath_len);
2833         }
2834
2835         /*
2836          * Prepare the canary for SSP.
2837          */
2838         arc4rand(canary, sizeof(canary), 0);
2839         imgp->canary = (uintptr_t)arginfo - szsigcode - execpath_len -
2840             sizeof(canary);
2841         copyout(canary, (void *)imgp->canary, sizeof(canary));
2842         imgp->canarylen = sizeof(canary);
2843
2844         /*
2845          * Prepare the pagesizes array.
2846          */
2847         for (i = 0; i < MAXPAGESIZES; i++)
2848                 pagesizes32[i] = (uint32_t)pagesizes[i];
2849         imgp->pagesizes = (uintptr_t)arginfo - szsigcode - execpath_len -
2850             roundup(sizeof(canary), sizeof(char *)) - sizeof(pagesizes32);
2851         copyout(pagesizes32, (void *)imgp->pagesizes, sizeof(pagesizes32));
2852         imgp->pagesizeslen = sizeof(pagesizes32);
2853
2854         /*
2855          * If we have a valid auxargs ptr, prepare some room
2856          * on the stack.
2857          */
2858         if (imgp->auxargs) {
2859                 /*
2860                  * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
2861                  * lower compatibility.
2862                  */
2863                 imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size
2864                         : (AT_COUNT * 2);
2865                 /*
2866                  * The '+ 2' is for the null pointers at the end of each of
2867                  * the arg and env vector sets,and imgp->auxarg_size is room
2868                  * for argument of Runtime loader.
2869                  */
2870                 vectp = (u_int32_t *) (destp - (imgp->args->argc +
2871                     imgp->args->envc + 2 + imgp->auxarg_size + execpath_len) *
2872                     sizeof(u_int32_t));
2873         } else
2874                 /*
2875                  * The '+ 2' is for the null pointers at the end of each of
2876                  * the arg and env vector sets
2877                  */
2878                 vectp = (u_int32_t *)
2879                         (destp - (imgp->args->argc + imgp->args->envc + 2) * sizeof(u_int32_t));
2880
2881         /*
2882          * vectp also becomes our initial stack base
2883          */
2884         stack_base = vectp;
2885
2886         stringp = imgp->args->begin_argv;
2887         argc = imgp->args->argc;
2888         envc = imgp->args->envc;
2889         /*
2890          * Copy out strings - arguments and environment.
2891          */
2892         copyout(stringp, destp, ARG_MAX - imgp->args->stringspace);
2893
2894         /*
2895          * Fill in "ps_strings" struct for ps, w, etc.
2896          */
2897         suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp);
2898         suword32(&arginfo->ps_nargvstr, argc);
2899
2900         /*
2901          * Fill in argument portion of vector table.
2902          */
2903         for (; argc > 0; --argc) {
2904                 suword32(vectp++, (u_int32_t)(intptr_t)destp);
2905                 while (*stringp++ != 0)
2906                         destp++;
2907                 destp++;
2908         }
2909
2910         /* a null vector table pointer separates the argp's from the envp's */
2911         suword32(vectp++, 0);
2912
2913         suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp);
2914         suword32(&arginfo->ps_nenvstr, envc);
2915
2916         /*
2917          * Fill in environment portion of vector table.
2918          */
2919         for (; envc > 0; --envc) {
2920                 suword32(vectp++, (u_int32_t)(intptr_t)destp);
2921                 while (*stringp++ != 0)
2922                         destp++;
2923                 destp++;
2924         }
2925
2926         /* end of vector table is a null pointer */
2927         suword32(vectp, 0);
2928
2929         return ((register_t *)stack_base);
2930 }
2931
2932 int
2933 freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
2934 {
2935         struct kld_file_stat stat;
2936         struct kld32_file_stat stat32;
2937         int error, version;
2938
2939         if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
2940             != 0)
2941                 return (error);
2942         if (version != sizeof(struct kld32_file_stat_1) &&
2943             version != sizeof(struct kld32_file_stat))
2944                 return (EINVAL);
2945
2946         error = kern_kldstat(td, uap->fileid, &stat);
2947         if (error != 0)
2948                 return (error);
2949
2950         bcopy(&stat.name[0], &stat32.name[0], sizeof(stat.name));
2951         CP(stat, stat32, refs);
2952         CP(stat, stat32, id);
2953         PTROUT_CP(stat, stat32, address);
2954         CP(stat, stat32, size);
2955         bcopy(&stat.pathname[0], &stat32.pathname[0], sizeof(stat.pathname));
2956         return (copyout(&stat32, uap->stat, version));
2957 }
2958
2959 int
2960 freebsd32_posix_fallocate(struct thread *td,
2961     struct freebsd32_posix_fallocate_args *uap)
2962 {
2963
2964         return (kern_posix_fallocate(td, uap->fd,
2965             PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len)));
2966 }
2967
2968 int
2969 freebsd32_posix_fadvise(struct thread *td,
2970     struct freebsd32_posix_fadvise_args *uap)
2971 {
2972
2973         return (kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset),
2974             PAIR32TO64(off_t, uap->len), uap->advice));
2975 }
2976
2977 int
2978 convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig)
2979 {
2980
2981         CP(*sig32, *sig, sigev_notify);
2982         switch (sig->sigev_notify) {
2983         case SIGEV_NONE:
2984                 break;
2985         case SIGEV_THREAD_ID:
2986                 CP(*sig32, *sig, sigev_notify_thread_id);
2987                 /* FALLTHROUGH */
2988         case SIGEV_SIGNAL:
2989                 CP(*sig32, *sig, sigev_signo);
2990                 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
2991                 break;
2992         case SIGEV_KEVENT:
2993                 CP(*sig32, *sig, sigev_notify_kqueue);
2994                 CP(*sig32, *sig, sigev_notify_kevent_flags);
2995                 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
2996                 break;
2997         default:
2998                 return (EINVAL);
2999         }
3000         return (0);
3001 }