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