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