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