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