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