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