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