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