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