freebsd32_misc.c revision 210431
143548Ssemenu/*- 250476Speter * Copyright (c) 2002 Doug Rabson 343548Ssemenu * All rights reserved. 443548Ssemenu * 543548Ssemenu * Redistribution and use in source and binary forms, with or without 643548Ssemenu * modification, are permitted provided that the following conditions 774815Sru * are met: 843548Ssemenu * 1. Redistributions of source code must retain the above copyright 943548Ssemenu * notice, this list of conditions and the following disclaimer. 1043548Ssemenu * 2. Redistributions in binary form must reproduce the above copyright 1143548Ssemenu * notice, this list of conditions and the following disclaimer in the 1243548Ssemenu * documentation and/or other materials provided with the distribution. 1343548Ssemenu * 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 210431 2010-07-23 21:30:33Z 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/lock.h> 47#include <sys/malloc.h> 48#include <sys/file.h> /* Must come after sys/malloc.h */ 49#include <sys/imgact.h> 50#include <sys/mbuf.h> 51#include <sys/mman.h> 52#include <sys/module.h> 53#include <sys/mount.h> 54#include <sys/mutex.h> 55#include <sys/namei.h> 56#include <sys/proc.h> 57#include <sys/reboot.h> 58#include <sys/resource.h> 59#include <sys/resourcevar.h> 60#include <sys/selinfo.h> 61#include <sys/eventvar.h> /* Must come after sys/selinfo.h */ 62#include <sys/pipe.h> /* Must come after sys/selinfo.h */ 63#include <sys/signal.h> 64#include <sys/signalvar.h> 65#include <sys/socket.h> 66#include <sys/socketvar.h> 67#include <sys/stat.h> 68#include <sys/syscall.h> 69#include <sys/syscallsubr.h> 70#include <sys/sysctl.h> 71#include <sys/sysent.h> 72#include <sys/sysproto.h> 73#include <sys/systm.h> 74#include <sys/thr.h> 75#include <sys/unistd.h> 76#include <sys/ucontext.h> 77#include <sys/vnode.h> 78#include <sys/wait.h> 79#include <sys/ipc.h> 80#include <sys/msg.h> 81#include <sys/sem.h> 82#include <sys/shm.h> 83 84#ifdef INET 85#include <netinet/in.h> 86#endif 87 88#include <vm/vm.h> 89#include <vm/vm_kern.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 temporary demand zeroed space for argument and 283 * environment strings 284 */ 285 args->buf = (char *) kmem_alloc_wait(exec_map, 286 PATH_MAX + ARG_MAX + MAXSHELLCMDLEN); 287 if (args->buf == NULL) 288 return (ENOMEM); 289 args->begin_argv = args->buf; 290 args->endp = args->begin_argv; 291 args->stringspace = ARG_MAX; 292 293 /* 294 * Copy the file name. 295 */ 296 if (fname != NULL) { 297 args->fname = args->buf + ARG_MAX; 298 error = (segflg == UIO_SYSSPACE) ? 299 copystr(fname, args->fname, PATH_MAX, &length) : 300 copyinstr(fname, args->fname, PATH_MAX, &length); 301 if (error != 0) 302 goto err_exit; 303 } else 304 args->fname = NULL; 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 1064 if (error == 0) 1065 error = freebsd32_copyoutmsghdr(&msg, uap->msg); 1066 } 1067 free(iov, M_IOV); 1068 1069 if (control != NULL) 1070 m_freem(control); 1071 1072 return (error); 1073} 1074 1075 1076static int 1077freebsd32_convert_msg_in(struct mbuf **controlp) 1078{ 1079 struct mbuf *control = *controlp; 1080 struct cmsghdr *cm = mtod(control, struct cmsghdr *); 1081 void *data; 1082 socklen_t clen = control->m_len, datalen; 1083 int error; 1084 1085 error = 0; 1086 *controlp = NULL; 1087 1088 while (cm != NULL) { 1089 if (sizeof(struct cmsghdr) > clen || cm->cmsg_len > clen) { 1090 error = EINVAL; 1091 break; 1092 } 1093 1094 data = FREEBSD32_CMSG_DATA(cm); 1095 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 1096 1097 *controlp = sbcreatecontrol(data, datalen, cm->cmsg_type, 1098 cm->cmsg_level); 1099 controlp = &(*controlp)->m_next; 1100 1101 if (FREEBSD32_CMSG_SPACE(datalen) < clen) { 1102 clen -= FREEBSD32_CMSG_SPACE(datalen); 1103 cm = (struct cmsghdr *) 1104 ((caddr_t)cm + FREEBSD32_CMSG_SPACE(datalen)); 1105 } else { 1106 clen = 0; 1107 cm = NULL; 1108 } 1109 } 1110 1111 m_freem(control); 1112 return (error); 1113} 1114 1115 1116int 1117freebsd32_sendmsg(struct thread *td, 1118 struct freebsd32_sendmsg_args *uap) 1119{ 1120 struct msghdr msg; 1121 struct msghdr32 m32; 1122 struct iovec *iov; 1123 struct mbuf *control = NULL; 1124 struct sockaddr *to = NULL; 1125 int error; 1126 1127 error = copyin(uap->msg, &m32, sizeof(m32)); 1128 if (error) 1129 return (error); 1130 error = freebsd32_copyinmsghdr(uap->msg, &msg); 1131 if (error) 1132 return (error); 1133 error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov, 1134 EMSGSIZE); 1135 if (error) 1136 return (error); 1137 msg.msg_iov = iov; 1138 if (msg.msg_name != NULL) { 1139 error = getsockaddr(&to, msg.msg_name, msg.msg_namelen); 1140 if (error) { 1141 to = NULL; 1142 goto out; 1143 } 1144 msg.msg_name = to; 1145 } 1146 1147 if (msg.msg_control) { 1148 if (msg.msg_controllen < sizeof(struct cmsghdr)) { 1149 error = EINVAL; 1150 goto out; 1151 } 1152 1153 error = sockargs(&control, msg.msg_control, 1154 msg.msg_controllen, MT_CONTROL); 1155 if (error) 1156 goto out; 1157 1158 error = freebsd32_convert_msg_in(&control); 1159 if (error) 1160 goto out; 1161 } 1162 1163 error = kern_sendit(td, uap->s, &msg, uap->flags, control, 1164 UIO_USERSPACE); 1165 1166out: 1167 free(iov, M_IOV); 1168 if (to) 1169 free(to, M_SONAME); 1170 return (error); 1171} 1172 1173int 1174freebsd32_recvfrom(struct thread *td, 1175 struct freebsd32_recvfrom_args *uap) 1176{ 1177 struct msghdr msg; 1178 struct iovec aiov; 1179 int error; 1180 1181 if (uap->fromlenaddr) { 1182 error = copyin(PTRIN(uap->fromlenaddr), &msg.msg_namelen, 1183 sizeof(msg.msg_namelen)); 1184 if (error) 1185 return (error); 1186 } else { 1187 msg.msg_namelen = 0; 1188 } 1189 1190 msg.msg_name = PTRIN(uap->from); 1191 msg.msg_iov = &aiov; 1192 msg.msg_iovlen = 1; 1193 aiov.iov_base = PTRIN(uap->buf); 1194 aiov.iov_len = uap->len; 1195 msg.msg_control = NULL; 1196 msg.msg_flags = uap->flags; 1197 error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, NULL); 1198 if (error == 0 && uap->fromlenaddr) 1199 error = copyout(&msg.msg_namelen, PTRIN(uap->fromlenaddr), 1200 sizeof (msg.msg_namelen)); 1201 return (error); 1202} 1203 1204int 1205freebsd32_settimeofday(struct thread *td, 1206 struct freebsd32_settimeofday_args *uap) 1207{ 1208 struct timeval32 tv32; 1209 struct timeval tv, *tvp; 1210 struct timezone tz, *tzp; 1211 int error; 1212 1213 if (uap->tv) { 1214 error = copyin(uap->tv, &tv32, sizeof(tv32)); 1215 if (error) 1216 return (error); 1217 CP(tv32, tv, tv_sec); 1218 CP(tv32, tv, tv_usec); 1219 tvp = &tv; 1220 } else 1221 tvp = NULL; 1222 if (uap->tzp) { 1223 error = copyin(uap->tzp, &tz, sizeof(tz)); 1224 if (error) 1225 return (error); 1226 tzp = &tz; 1227 } else 1228 tzp = NULL; 1229 return (kern_settimeofday(td, tvp, tzp)); 1230} 1231 1232int 1233freebsd32_utimes(struct thread *td, struct freebsd32_utimes_args *uap) 1234{ 1235 struct timeval32 s32[2]; 1236 struct timeval s[2], *sp; 1237 int error; 1238 1239 if (uap->tptr != NULL) { 1240 error = copyin(uap->tptr, s32, sizeof(s32)); 1241 if (error) 1242 return (error); 1243 CP(s32[0], s[0], tv_sec); 1244 CP(s32[0], s[0], tv_usec); 1245 CP(s32[1], s[1], tv_sec); 1246 CP(s32[1], s[1], tv_usec); 1247 sp = s; 1248 } else 1249 sp = NULL; 1250 return (kern_utimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE)); 1251} 1252 1253int 1254freebsd32_lutimes(struct thread *td, struct freebsd32_lutimes_args *uap) 1255{ 1256 struct timeval32 s32[2]; 1257 struct timeval s[2], *sp; 1258 int error; 1259 1260 if (uap->tptr != NULL) { 1261 error = copyin(uap->tptr, s32, sizeof(s32)); 1262 if (error) 1263 return (error); 1264 CP(s32[0], s[0], tv_sec); 1265 CP(s32[0], s[0], tv_usec); 1266 CP(s32[1], s[1], tv_sec); 1267 CP(s32[1], s[1], tv_usec); 1268 sp = s; 1269 } else 1270 sp = NULL; 1271 return (kern_lutimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE)); 1272} 1273 1274int 1275freebsd32_futimes(struct thread *td, struct freebsd32_futimes_args *uap) 1276{ 1277 struct timeval32 s32[2]; 1278 struct timeval s[2], *sp; 1279 int error; 1280 1281 if (uap->tptr != NULL) { 1282 error = copyin(uap->tptr, s32, sizeof(s32)); 1283 if (error) 1284 return (error); 1285 CP(s32[0], s[0], tv_sec); 1286 CP(s32[0], s[0], tv_usec); 1287 CP(s32[1], s[1], tv_sec); 1288 CP(s32[1], s[1], tv_usec); 1289 sp = s; 1290 } else 1291 sp = NULL; 1292 return (kern_futimes(td, uap->fd, sp, UIO_SYSSPACE)); 1293} 1294 1295int 1296freebsd32_futimesat(struct thread *td, struct freebsd32_futimesat_args *uap) 1297{ 1298 struct timeval32 s32[2]; 1299 struct timeval s[2], *sp; 1300 int error; 1301 1302 if (uap->times != NULL) { 1303 error = copyin(uap->times, s32, sizeof(s32)); 1304 if (error) 1305 return (error); 1306 CP(s32[0], s[0], tv_sec); 1307 CP(s32[0], s[0], tv_usec); 1308 CP(s32[1], s[1], tv_sec); 1309 CP(s32[1], s[1], tv_usec); 1310 sp = s; 1311 } else 1312 sp = NULL; 1313 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE, 1314 sp, UIO_SYSSPACE)); 1315} 1316 1317int 1318freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap) 1319{ 1320 struct timeval32 tv32; 1321 struct timeval delta, olddelta, *deltap; 1322 int error; 1323 1324 if (uap->delta) { 1325 error = copyin(uap->delta, &tv32, sizeof(tv32)); 1326 if (error) 1327 return (error); 1328 CP(tv32, delta, tv_sec); 1329 CP(tv32, delta, tv_usec); 1330 deltap = δ 1331 } else 1332 deltap = NULL; 1333 error = kern_adjtime(td, deltap, &olddelta); 1334 if (uap->olddelta && error == 0) { 1335 CP(olddelta, tv32, tv_sec); 1336 CP(olddelta, tv32, tv_usec); 1337 error = copyout(&tv32, uap->olddelta, sizeof(tv32)); 1338 } 1339 return (error); 1340} 1341 1342#ifdef COMPAT_FREEBSD4 1343int 1344freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap) 1345{ 1346 struct statfs32 s32; 1347 struct statfs s; 1348 int error; 1349 1350 error = kern_statfs(td, uap->path, UIO_USERSPACE, &s); 1351 if (error) 1352 return (error); 1353 copy_statfs(&s, &s32); 1354 return (copyout(&s32, uap->buf, sizeof(s32))); 1355} 1356#endif 1357 1358#ifdef COMPAT_FREEBSD4 1359int 1360freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap) 1361{ 1362 struct statfs32 s32; 1363 struct statfs s; 1364 int error; 1365 1366 error = kern_fstatfs(td, uap->fd, &s); 1367 if (error) 1368 return (error); 1369 copy_statfs(&s, &s32); 1370 return (copyout(&s32, uap->buf, sizeof(s32))); 1371} 1372#endif 1373 1374#ifdef COMPAT_FREEBSD4 1375int 1376freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap) 1377{ 1378 struct statfs32 s32; 1379 struct statfs s; 1380 fhandle_t fh; 1381 int error; 1382 1383 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0) 1384 return (error); 1385 error = kern_fhstatfs(td, fh, &s); 1386 if (error) 1387 return (error); 1388 copy_statfs(&s, &s32); 1389 return (copyout(&s32, uap->buf, sizeof(s32))); 1390} 1391#endif 1392 1393int 1394freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap) 1395{ 1396 struct pread_args ap; 1397 1398 ap.fd = uap->fd; 1399 ap.buf = uap->buf; 1400 ap.nbyte = uap->nbyte; 1401 ap.offset = PAIR32TO64(off_t,uap->offset); 1402 return (pread(td, &ap)); 1403} 1404 1405int 1406freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap) 1407{ 1408 struct pwrite_args ap; 1409 1410 ap.fd = uap->fd; 1411 ap.buf = uap->buf; 1412 ap.nbyte = uap->nbyte; 1413 ap.offset = PAIR32TO64(off_t,uap->offset); 1414 return (pwrite(td, &ap)); 1415} 1416 1417int 1418freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap) 1419{ 1420 int error; 1421 struct lseek_args ap; 1422 off_t pos; 1423 1424 ap.fd = uap->fd; 1425 ap.offset = PAIR32TO64(off_t,uap->offset); 1426 ap.whence = uap->whence; 1427 error = lseek(td, &ap); 1428 /* Expand the quad return into two parts for eax and edx */ 1429 pos = *(off_t *)(td->td_retval); 1430 td->td_retval[RETVAL_LO] = pos & 0xffffffff; /* %eax */ 1431 td->td_retval[RETVAL_HI] = pos >> 32; /* %edx */ 1432 return error; 1433} 1434 1435int 1436freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap) 1437{ 1438 struct truncate_args ap; 1439 1440 ap.path = uap->path; 1441 ap.length = PAIR32TO64(off_t,uap->length); 1442 return (truncate(td, &ap)); 1443} 1444 1445int 1446freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap) 1447{ 1448 struct ftruncate_args ap; 1449 1450 ap.fd = uap->fd; 1451 ap.length = PAIR32TO64(off_t,uap->length); 1452 return (ftruncate(td, &ap)); 1453} 1454 1455int 1456freebsd32_getdirentries(struct thread *td, 1457 struct freebsd32_getdirentries_args *uap) 1458{ 1459 long base; 1460 int32_t base32; 1461 int error; 1462 1463 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base); 1464 if (error) 1465 return (error); 1466 if (uap->basep != NULL) { 1467 base32 = base; 1468 error = copyout(&base32, uap->basep, sizeof(int32_t)); 1469 } 1470 return (error); 1471} 1472 1473#ifdef COMPAT_FREEBSD6 1474/* versions with the 'int pad' argument */ 1475int 1476freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap) 1477{ 1478 struct pread_args ap; 1479 1480 ap.fd = uap->fd; 1481 ap.buf = uap->buf; 1482 ap.nbyte = uap->nbyte; 1483 ap.offset = PAIR32TO64(off_t,uap->offset); 1484 return (pread(td, &ap)); 1485} 1486 1487int 1488freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap) 1489{ 1490 struct pwrite_args ap; 1491 1492 ap.fd = uap->fd; 1493 ap.buf = uap->buf; 1494 ap.nbyte = uap->nbyte; 1495 ap.offset = PAIR32TO64(off_t,uap->offset); 1496 return (pwrite(td, &ap)); 1497} 1498 1499int 1500freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap) 1501{ 1502 int error; 1503 struct lseek_args ap; 1504 off_t pos; 1505 1506 ap.fd = uap->fd; 1507 ap.offset = PAIR32TO64(off_t,uap->offset); 1508 ap.whence = uap->whence; 1509 error = lseek(td, &ap); 1510 /* Expand the quad return into two parts for eax and edx */ 1511 pos = *(off_t *)(td->td_retval); 1512 td->td_retval[RETVAL_LO] = pos & 0xffffffff; /* %eax */ 1513 td->td_retval[RETVAL_HI] = pos >> 32; /* %edx */ 1514 return error; 1515} 1516 1517int 1518freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap) 1519{ 1520 struct truncate_args ap; 1521 1522 ap.path = uap->path; 1523 ap.length = PAIR32TO64(off_t,uap->length); 1524 return (truncate(td, &ap)); 1525} 1526 1527int 1528freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap) 1529{ 1530 struct ftruncate_args ap; 1531 1532 ap.fd = uap->fd; 1533 ap.length = PAIR32TO64(off_t,uap->length); 1534 return (ftruncate(td, &ap)); 1535} 1536#endif /* COMPAT_FREEBSD6 */ 1537 1538struct sf_hdtr32 { 1539 uint32_t headers; 1540 int hdr_cnt; 1541 uint32_t trailers; 1542 int trl_cnt; 1543}; 1544 1545static int 1546freebsd32_do_sendfile(struct thread *td, 1547 struct freebsd32_sendfile_args *uap, int compat) 1548{ 1549 struct sendfile_args ap; 1550 struct sf_hdtr32 hdtr32; 1551 struct sf_hdtr hdtr; 1552 struct uio *hdr_uio, *trl_uio; 1553 struct iovec32 *iov32; 1554 int error; 1555 1556 hdr_uio = trl_uio = NULL; 1557 1558 ap.fd = uap->fd; 1559 ap.s = uap->s; 1560 ap.offset = PAIR32TO64(off_t,uap->offset); 1561 ap.nbytes = uap->nbytes; 1562 ap.hdtr = (struct sf_hdtr *)uap->hdtr; /* XXX not used */ 1563 ap.sbytes = uap->sbytes; 1564 ap.flags = uap->flags; 1565 1566 if (uap->hdtr != NULL) { 1567 error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32)); 1568 if (error) 1569 goto out; 1570 PTRIN_CP(hdtr32, hdtr, headers); 1571 CP(hdtr32, hdtr, hdr_cnt); 1572 PTRIN_CP(hdtr32, hdtr, trailers); 1573 CP(hdtr32, hdtr, trl_cnt); 1574 1575 if (hdtr.headers != NULL) { 1576 iov32 = PTRIN(hdtr32.headers); 1577 error = freebsd32_copyinuio(iov32, 1578 hdtr32.hdr_cnt, &hdr_uio); 1579 if (error) 1580 goto out; 1581 } 1582 if (hdtr.trailers != NULL) { 1583 iov32 = PTRIN(hdtr32.trailers); 1584 error = freebsd32_copyinuio(iov32, 1585 hdtr32.trl_cnt, &trl_uio); 1586 if (error) 1587 goto out; 1588 } 1589 } 1590 1591 error = kern_sendfile(td, &ap, hdr_uio, trl_uio, compat); 1592out: 1593 if (hdr_uio) 1594 free(hdr_uio, M_IOV); 1595 if (trl_uio) 1596 free(trl_uio, M_IOV); 1597 return (error); 1598} 1599 1600#ifdef COMPAT_FREEBSD4 1601int 1602freebsd4_freebsd32_sendfile(struct thread *td, 1603 struct freebsd4_freebsd32_sendfile_args *uap) 1604{ 1605 return (freebsd32_do_sendfile(td, 1606 (struct freebsd32_sendfile_args *)uap, 1)); 1607} 1608#endif 1609 1610int 1611freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap) 1612{ 1613 1614 return (freebsd32_do_sendfile(td, uap, 0)); 1615} 1616 1617static void 1618copy_stat( struct stat *in, struct stat32 *out) 1619{ 1620 CP(*in, *out, st_dev); 1621 CP(*in, *out, st_ino); 1622 CP(*in, *out, st_mode); 1623 CP(*in, *out, st_nlink); 1624 CP(*in, *out, st_uid); 1625 CP(*in, *out, st_gid); 1626 CP(*in, *out, st_rdev); 1627 TS_CP(*in, *out, st_atim); 1628 TS_CP(*in, *out, st_mtim); 1629 TS_CP(*in, *out, st_ctim); 1630 CP(*in, *out, st_size); 1631 CP(*in, *out, st_blocks); 1632 CP(*in, *out, st_blksize); 1633 CP(*in, *out, st_flags); 1634 CP(*in, *out, st_gen); 1635} 1636 1637int 1638freebsd32_stat(struct thread *td, struct freebsd32_stat_args *uap) 1639{ 1640 struct stat sb; 1641 struct stat32 sb32; 1642 int error; 1643 1644 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb); 1645 if (error) 1646 return (error); 1647 copy_stat(&sb, &sb32); 1648 error = copyout(&sb32, uap->ub, sizeof (sb32)); 1649 return (error); 1650} 1651 1652int 1653freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap) 1654{ 1655 struct stat ub; 1656 struct stat32 ub32; 1657 int error; 1658 1659 error = kern_fstat(td, uap->fd, &ub); 1660 if (error) 1661 return (error); 1662 copy_stat(&ub, &ub32); 1663 error = copyout(&ub32, uap->ub, sizeof(ub32)); 1664 return (error); 1665} 1666 1667int 1668freebsd32_fstatat(struct thread *td, struct freebsd32_fstatat_args *uap) 1669{ 1670 struct stat ub; 1671 struct stat32 ub32; 1672 int error; 1673 1674 error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE, &ub); 1675 if (error) 1676 return (error); 1677 copy_stat(&ub, &ub32); 1678 error = copyout(&ub32, uap->buf, sizeof(ub32)); 1679 return (error); 1680} 1681 1682int 1683freebsd32_lstat(struct thread *td, struct freebsd32_lstat_args *uap) 1684{ 1685 struct stat sb; 1686 struct stat32 sb32; 1687 int error; 1688 1689 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb); 1690 if (error) 1691 return (error); 1692 copy_stat(&sb, &sb32); 1693 error = copyout(&sb32, uap->ub, sizeof (sb32)); 1694 return (error); 1695} 1696 1697/* 1698 * MPSAFE 1699 */ 1700int 1701freebsd32_sysctl(struct thread *td, struct freebsd32_sysctl_args *uap) 1702{ 1703 int error, name[CTL_MAXNAME]; 1704 size_t j, oldlen; 1705 1706 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2) 1707 return (EINVAL); 1708 error = copyin(uap->name, name, uap->namelen * sizeof(int)); 1709 if (error) 1710 return (error); 1711 if (uap->oldlenp) 1712 oldlen = fuword32(uap->oldlenp); 1713 else 1714 oldlen = 0; 1715 error = userland_sysctl(td, name, uap->namelen, 1716 uap->old, &oldlen, 1, 1717 uap->new, uap->newlen, &j, SCTL_MASK32); 1718 if (error && error != ENOMEM) 1719 return (error); 1720 if (uap->oldlenp) 1721 suword32(uap->oldlenp, j); 1722 return (0); 1723} 1724 1725int 1726freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap) 1727{ 1728 uint32_t version; 1729 int error; 1730 struct jail j; 1731 1732 error = copyin(uap->jail, &version, sizeof(uint32_t)); 1733 if (error) 1734 return (error); 1735 1736 switch (version) { 1737 case 0: 1738 { 1739 /* FreeBSD single IPv4 jails. */ 1740 struct jail32_v0 j32_v0; 1741 1742 bzero(&j, sizeof(struct jail)); 1743 error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0)); 1744 if (error) 1745 return (error); 1746 CP(j32_v0, j, version); 1747 PTRIN_CP(j32_v0, j, path); 1748 PTRIN_CP(j32_v0, j, hostname); 1749 j.ip4s = j32_v0.ip_number; 1750 break; 1751 } 1752 1753 case 1: 1754 /* 1755 * Version 1 was used by multi-IPv4 jail implementations 1756 * that never made it into the official kernel. 1757 */ 1758 return (EINVAL); 1759 1760 case 2: /* JAIL_API_VERSION */ 1761 { 1762 /* FreeBSD multi-IPv4/IPv6,noIP jails. */ 1763 struct jail32 j32; 1764 1765 error = copyin(uap->jail, &j32, sizeof(struct jail32)); 1766 if (error) 1767 return (error); 1768 CP(j32, j, version); 1769 PTRIN_CP(j32, j, path); 1770 PTRIN_CP(j32, j, hostname); 1771 PTRIN_CP(j32, j, jailname); 1772 CP(j32, j, ip4s); 1773 CP(j32, j, ip6s); 1774 PTRIN_CP(j32, j, ip4); 1775 PTRIN_CP(j32, j, ip6); 1776 break; 1777 } 1778 1779 default: 1780 /* Sci-Fi jails are not supported, sorry. */ 1781 return (EINVAL); 1782 } 1783 return (kern_jail(td, &j)); 1784} 1785 1786int 1787freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap) 1788{ 1789 struct uio *auio; 1790 int error; 1791 1792 /* Check that we have an even number of iovecs. */ 1793 if (uap->iovcnt & 1) 1794 return (EINVAL); 1795 1796 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio); 1797 if (error) 1798 return (error); 1799 error = kern_jail_set(td, auio, uap->flags); 1800 free(auio, M_IOV); 1801 return (error); 1802} 1803 1804int 1805freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap) 1806{ 1807 struct iovec32 iov32; 1808 struct uio *auio; 1809 int error, i; 1810 1811 /* Check that we have an even number of iovecs. */ 1812 if (uap->iovcnt & 1) 1813 return (EINVAL); 1814 1815 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio); 1816 if (error) 1817 return (error); 1818 error = kern_jail_get(td, auio, uap->flags); 1819 if (error == 0) 1820 for (i = 0; i < uap->iovcnt; i++) { 1821 PTROUT_CP(auio->uio_iov[i], iov32, iov_base); 1822 CP(auio->uio_iov[i], iov32, iov_len); 1823 error = copyout(&iov32, uap->iovp + i, sizeof(iov32)); 1824 if (error != 0) 1825 break; 1826 } 1827 free(auio, M_IOV); 1828 return (error); 1829} 1830 1831int 1832freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap) 1833{ 1834 struct sigaction32 s32; 1835 struct sigaction sa, osa, *sap; 1836 int error; 1837 1838 if (uap->act) { 1839 error = copyin(uap->act, &s32, sizeof(s32)); 1840 if (error) 1841 return (error); 1842 sa.sa_handler = PTRIN(s32.sa_u); 1843 CP(s32, sa, sa_flags); 1844 CP(s32, sa, sa_mask); 1845 sap = &sa; 1846 } else 1847 sap = NULL; 1848 error = kern_sigaction(td, uap->sig, sap, &osa, 0); 1849 if (error == 0 && uap->oact != NULL) { 1850 s32.sa_u = PTROUT(osa.sa_handler); 1851 CP(osa, s32, sa_flags); 1852 CP(osa, s32, sa_mask); 1853 error = copyout(&s32, uap->oact, sizeof(s32)); 1854 } 1855 return (error); 1856} 1857 1858#ifdef COMPAT_FREEBSD4 1859int 1860freebsd4_freebsd32_sigaction(struct thread *td, 1861 struct freebsd4_freebsd32_sigaction_args *uap) 1862{ 1863 struct sigaction32 s32; 1864 struct sigaction sa, osa, *sap; 1865 int error; 1866 1867 if (uap->act) { 1868 error = copyin(uap->act, &s32, sizeof(s32)); 1869 if (error) 1870 return (error); 1871 sa.sa_handler = PTRIN(s32.sa_u); 1872 CP(s32, sa, sa_flags); 1873 CP(s32, sa, sa_mask); 1874 sap = &sa; 1875 } else 1876 sap = NULL; 1877 error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4); 1878 if (error == 0 && uap->oact != NULL) { 1879 s32.sa_u = PTROUT(osa.sa_handler); 1880 CP(osa, s32, sa_flags); 1881 CP(osa, s32, sa_mask); 1882 error = copyout(&s32, uap->oact, sizeof(s32)); 1883 } 1884 return (error); 1885} 1886#endif 1887 1888#ifdef COMPAT_43 1889struct osigaction32 { 1890 u_int32_t sa_u; 1891 osigset_t sa_mask; 1892 int sa_flags; 1893}; 1894 1895#define ONSIG 32 1896 1897int 1898ofreebsd32_sigaction(struct thread *td, 1899 struct ofreebsd32_sigaction_args *uap) 1900{ 1901 struct osigaction32 s32; 1902 struct sigaction sa, osa, *sap; 1903 int error; 1904 1905 if (uap->signum <= 0 || uap->signum >= ONSIG) 1906 return (EINVAL); 1907 1908 if (uap->nsa) { 1909 error = copyin(uap->nsa, &s32, sizeof(s32)); 1910 if (error) 1911 return (error); 1912 sa.sa_handler = PTRIN(s32.sa_u); 1913 CP(s32, sa, sa_flags); 1914 OSIG2SIG(s32.sa_mask, sa.sa_mask); 1915 sap = &sa; 1916 } else 1917 sap = NULL; 1918 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET); 1919 if (error == 0 && uap->osa != NULL) { 1920 s32.sa_u = PTROUT(osa.sa_handler); 1921 CP(osa, s32, sa_flags); 1922 SIG2OSIG(osa.sa_mask, s32.sa_mask); 1923 error = copyout(&s32, uap->osa, sizeof(s32)); 1924 } 1925 return (error); 1926} 1927 1928int 1929ofreebsd32_sigprocmask(struct thread *td, 1930 struct ofreebsd32_sigprocmask_args *uap) 1931{ 1932 sigset_t set, oset; 1933 int error; 1934 1935 OSIG2SIG(uap->mask, set); 1936 error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD); 1937 SIG2OSIG(oset, td->td_retval[0]); 1938 return (error); 1939} 1940 1941int 1942ofreebsd32_sigpending(struct thread *td, 1943 struct ofreebsd32_sigpending_args *uap) 1944{ 1945 struct proc *p = td->td_proc; 1946 sigset_t siglist; 1947 1948 PROC_LOCK(p); 1949 siglist = p->p_siglist; 1950 SIGSETOR(siglist, td->td_siglist); 1951 PROC_UNLOCK(p); 1952 SIG2OSIG(siglist, td->td_retval[0]); 1953 return (0); 1954} 1955 1956struct sigvec32 { 1957 u_int32_t sv_handler; 1958 int sv_mask; 1959 int sv_flags; 1960}; 1961 1962int 1963ofreebsd32_sigvec(struct thread *td, 1964 struct ofreebsd32_sigvec_args *uap) 1965{ 1966 struct sigvec32 vec; 1967 struct sigaction sa, osa, *sap; 1968 int error; 1969 1970 if (uap->signum <= 0 || uap->signum >= ONSIG) 1971 return (EINVAL); 1972 1973 if (uap->nsv) { 1974 error = copyin(uap->nsv, &vec, sizeof(vec)); 1975 if (error) 1976 return (error); 1977 sa.sa_handler = PTRIN(vec.sv_handler); 1978 OSIG2SIG(vec.sv_mask, sa.sa_mask); 1979 sa.sa_flags = vec.sv_flags; 1980 sa.sa_flags ^= SA_RESTART; 1981 sap = &sa; 1982 } else 1983 sap = NULL; 1984 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET); 1985 if (error == 0 && uap->osv != NULL) { 1986 vec.sv_handler = PTROUT(osa.sa_handler); 1987 SIG2OSIG(osa.sa_mask, vec.sv_mask); 1988 vec.sv_flags = osa.sa_flags; 1989 vec.sv_flags &= ~SA_NOCLDWAIT; 1990 vec.sv_flags ^= SA_RESTART; 1991 error = copyout(&vec, uap->osv, sizeof(vec)); 1992 } 1993 return (error); 1994} 1995 1996int 1997ofreebsd32_sigblock(struct thread *td, 1998 struct ofreebsd32_sigblock_args *uap) 1999{ 2000 sigset_t set, oset; 2001 2002 OSIG2SIG(uap->mask, set); 2003 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0); 2004 SIG2OSIG(oset, td->td_retval[0]); 2005 return (0); 2006} 2007 2008int 2009ofreebsd32_sigsetmask(struct thread *td, 2010 struct ofreebsd32_sigsetmask_args *uap) 2011{ 2012 sigset_t set, oset; 2013 2014 OSIG2SIG(uap->mask, set); 2015 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0); 2016 SIG2OSIG(oset, td->td_retval[0]); 2017 return (0); 2018} 2019 2020int 2021ofreebsd32_sigsuspend(struct thread *td, 2022 struct ofreebsd32_sigsuspend_args *uap) 2023{ 2024 sigset_t mask; 2025 2026 OSIG2SIG(uap->mask, mask); 2027 return (kern_sigsuspend(td, mask)); 2028} 2029 2030struct sigstack32 { 2031 u_int32_t ss_sp; 2032 int ss_onstack; 2033}; 2034 2035int 2036ofreebsd32_sigstack(struct thread *td, 2037 struct ofreebsd32_sigstack_args *uap) 2038{ 2039 struct sigstack32 s32; 2040 struct sigstack nss, oss; 2041 int error = 0, unss; 2042 2043 if (uap->nss != NULL) { 2044 error = copyin(uap->nss, &s32, sizeof(s32)); 2045 if (error) 2046 return (error); 2047 nss.ss_sp = PTRIN(s32.ss_sp); 2048 CP(s32, nss, ss_onstack); 2049 unss = 1; 2050 } else { 2051 unss = 0; 2052 } 2053 oss.ss_sp = td->td_sigstk.ss_sp; 2054 oss.ss_onstack = sigonstack(cpu_getstack(td)); 2055 if (unss) { 2056 td->td_sigstk.ss_sp = nss.ss_sp; 2057 td->td_sigstk.ss_size = 0; 2058 td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK); 2059 td->td_pflags |= TDP_ALTSTACK; 2060 } 2061 if (uap->oss != NULL) { 2062 s32.ss_sp = PTROUT(oss.ss_sp); 2063 CP(oss, s32, ss_onstack); 2064 error = copyout(&s32, uap->oss, sizeof(s32)); 2065 } 2066 return (error); 2067} 2068#endif 2069 2070int 2071freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap) 2072{ 2073 struct timespec32 rmt32, rqt32; 2074 struct timespec rmt, rqt; 2075 int error; 2076 2077 error = copyin(uap->rqtp, &rqt32, sizeof(rqt32)); 2078 if (error) 2079 return (error); 2080 2081 CP(rqt32, rqt, tv_sec); 2082 CP(rqt32, rqt, tv_nsec); 2083 2084 if (uap->rmtp && 2085 !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE)) 2086 return (EFAULT); 2087 error = kern_nanosleep(td, &rqt, &rmt); 2088 if (error && uap->rmtp) { 2089 int error2; 2090 2091 CP(rmt, rmt32, tv_sec); 2092 CP(rmt, rmt32, tv_nsec); 2093 2094 error2 = copyout(&rmt32, uap->rmtp, sizeof(rmt32)); 2095 if (error2) 2096 error = error2; 2097 } 2098 return (error); 2099} 2100 2101int 2102freebsd32_clock_gettime(struct thread *td, 2103 struct freebsd32_clock_gettime_args *uap) 2104{ 2105 struct timespec ats; 2106 struct timespec32 ats32; 2107 int error; 2108 2109 error = kern_clock_gettime(td, uap->clock_id, &ats); 2110 if (error == 0) { 2111 CP(ats, ats32, tv_sec); 2112 CP(ats, ats32, tv_nsec); 2113 error = copyout(&ats32, uap->tp, sizeof(ats32)); 2114 } 2115 return (error); 2116} 2117 2118int 2119freebsd32_clock_settime(struct thread *td, 2120 struct freebsd32_clock_settime_args *uap) 2121{ 2122 struct timespec ats; 2123 struct timespec32 ats32; 2124 int error; 2125 2126 error = copyin(uap->tp, &ats32, sizeof(ats32)); 2127 if (error) 2128 return (error); 2129 CP(ats32, ats, tv_sec); 2130 CP(ats32, ats, tv_nsec); 2131 2132 return (kern_clock_settime(td, uap->clock_id, &ats)); 2133} 2134 2135int 2136freebsd32_clock_getres(struct thread *td, 2137 struct freebsd32_clock_getres_args *uap) 2138{ 2139 struct timespec ts; 2140 struct timespec32 ts32; 2141 int error; 2142 2143 if (uap->tp == NULL) 2144 return (0); 2145 error = kern_clock_getres(td, uap->clock_id, &ts); 2146 if (error == 0) { 2147 CP(ts, ts32, tv_sec); 2148 CP(ts, ts32, tv_nsec); 2149 error = copyout(&ts32, uap->tp, sizeof(ts32)); 2150 } 2151 return (error); 2152} 2153 2154int 2155freebsd32_thr_new(struct thread *td, 2156 struct freebsd32_thr_new_args *uap) 2157{ 2158 struct thr_param32 param32; 2159 struct thr_param param; 2160 int error; 2161 2162 if (uap->param_size < 0 || 2163 uap->param_size > sizeof(struct thr_param32)) 2164 return (EINVAL); 2165 bzero(¶m, sizeof(struct thr_param)); 2166 bzero(¶m32, sizeof(struct thr_param32)); 2167 error = copyin(uap->param, ¶m32, uap->param_size); 2168 if (error != 0) 2169 return (error); 2170 param.start_func = PTRIN(param32.start_func); 2171 param.arg = PTRIN(param32.arg); 2172 param.stack_base = PTRIN(param32.stack_base); 2173 param.stack_size = param32.stack_size; 2174 param.tls_base = PTRIN(param32.tls_base); 2175 param.tls_size = param32.tls_size; 2176 param.child_tid = PTRIN(param32.child_tid); 2177 param.parent_tid = PTRIN(param32.parent_tid); 2178 param.flags = param32.flags; 2179 param.rtp = PTRIN(param32.rtp); 2180 param.spare[0] = PTRIN(param32.spare[0]); 2181 param.spare[1] = PTRIN(param32.spare[1]); 2182 param.spare[2] = PTRIN(param32.spare[2]); 2183 2184 return (kern_thr_new(td, ¶m)); 2185} 2186 2187int 2188freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap) 2189{ 2190 struct timespec32 ts32; 2191 struct timespec ts, *tsp; 2192 int error; 2193 2194 error = 0; 2195 tsp = NULL; 2196 if (uap->timeout != NULL) { 2197 error = copyin((const void *)uap->timeout, (void *)&ts32, 2198 sizeof(struct timespec32)); 2199 if (error != 0) 2200 return (error); 2201 ts.tv_sec = ts32.tv_sec; 2202 ts.tv_nsec = ts32.tv_nsec; 2203 tsp = &ts; 2204 } 2205 return (kern_thr_suspend(td, tsp)); 2206} 2207 2208void 2209siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst) 2210{ 2211 bzero(dst, sizeof(*dst)); 2212 dst->si_signo = src->si_signo; 2213 dst->si_errno = src->si_errno; 2214 dst->si_code = src->si_code; 2215 dst->si_pid = src->si_pid; 2216 dst->si_uid = src->si_uid; 2217 dst->si_status = src->si_status; 2218 dst->si_addr = (uintptr_t)src->si_addr; 2219 dst->si_value.sigval_int = src->si_value.sival_int; 2220 dst->si_timerid = src->si_timerid; 2221 dst->si_overrun = src->si_overrun; 2222} 2223 2224int 2225freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap) 2226{ 2227 struct timespec32 ts32; 2228 struct timespec ts; 2229 struct timespec *timeout; 2230 sigset_t set; 2231 ksiginfo_t ksi; 2232 struct siginfo32 si32; 2233 int error; 2234 2235 if (uap->timeout) { 2236 error = copyin(uap->timeout, &ts32, sizeof(ts32)); 2237 if (error) 2238 return (error); 2239 ts.tv_sec = ts32.tv_sec; 2240 ts.tv_nsec = ts32.tv_nsec; 2241 timeout = &ts; 2242 } else 2243 timeout = NULL; 2244 2245 error = copyin(uap->set, &set, sizeof(set)); 2246 if (error) 2247 return (error); 2248 2249 error = kern_sigtimedwait(td, set, &ksi, timeout); 2250 if (error) 2251 return (error); 2252 2253 if (uap->info) { 2254 siginfo_to_siginfo32(&ksi.ksi_info, &si32); 2255 error = copyout(&si32, uap->info, sizeof(struct siginfo32)); 2256 } 2257 2258 if (error == 0) 2259 td->td_retval[0] = ksi.ksi_signo; 2260 return (error); 2261} 2262 2263/* 2264 * MPSAFE 2265 */ 2266int 2267freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap) 2268{ 2269 ksiginfo_t ksi; 2270 struct siginfo32 si32; 2271 sigset_t set; 2272 int error; 2273 2274 error = copyin(uap->set, &set, sizeof(set)); 2275 if (error) 2276 return (error); 2277 2278 error = kern_sigtimedwait(td, set, &ksi, NULL); 2279 if (error) 2280 return (error); 2281 2282 if (uap->info) { 2283 siginfo_to_siginfo32(&ksi.ksi_info, &si32); 2284 error = copyout(&si32, uap->info, sizeof(struct siginfo32)); 2285 } 2286 if (error == 0) 2287 td->td_retval[0] = ksi.ksi_signo; 2288 return (error); 2289} 2290 2291int 2292freebsd32_cpuset_setid(struct thread *td, 2293 struct freebsd32_cpuset_setid_args *uap) 2294{ 2295 struct cpuset_setid_args ap; 2296 2297 ap.which = uap->which; 2298 ap.id = PAIR32TO64(id_t,uap->id); 2299 ap.setid = uap->setid; 2300 2301 return (cpuset_setid(td, &ap)); 2302} 2303 2304int 2305freebsd32_cpuset_getid(struct thread *td, 2306 struct freebsd32_cpuset_getid_args *uap) 2307{ 2308 struct cpuset_getid_args ap; 2309 2310 ap.level = uap->level; 2311 ap.which = uap->which; 2312 ap.id = PAIR32TO64(id_t,uap->id); 2313 ap.setid = uap->setid; 2314 2315 return (cpuset_getid(td, &ap)); 2316} 2317 2318int 2319freebsd32_cpuset_getaffinity(struct thread *td, 2320 struct freebsd32_cpuset_getaffinity_args *uap) 2321{ 2322 struct cpuset_getaffinity_args ap; 2323 2324 ap.level = uap->level; 2325 ap.which = uap->which; 2326 ap.id = PAIR32TO64(id_t,uap->id); 2327 ap.cpusetsize = uap->cpusetsize; 2328 ap.mask = uap->mask; 2329 2330 return (cpuset_getaffinity(td, &ap)); 2331} 2332 2333int 2334freebsd32_cpuset_setaffinity(struct thread *td, 2335 struct freebsd32_cpuset_setaffinity_args *uap) 2336{ 2337 struct cpuset_setaffinity_args ap; 2338 2339 ap.level = uap->level; 2340 ap.which = uap->which; 2341 ap.id = PAIR32TO64(id_t,uap->id); 2342 ap.cpusetsize = uap->cpusetsize; 2343 ap.mask = uap->mask; 2344 2345 return (cpuset_setaffinity(td, &ap)); 2346} 2347 2348int 2349freebsd32_nmount(struct thread *td, 2350 struct freebsd32_nmount_args /* { 2351 struct iovec *iovp; 2352 unsigned int iovcnt; 2353 int flags; 2354 } */ *uap) 2355{ 2356 struct uio *auio; 2357 int error; 2358 2359 AUDIT_ARG_FFLAGS(uap->flags); 2360 2361 /* 2362 * Filter out MNT_ROOTFS. We do not want clients of nmount() in 2363 * userspace to set this flag, but we must filter it out if we want 2364 * MNT_UPDATE on the root file system to work. 2365 * MNT_ROOTFS should only be set in the kernel in vfs_mountroot_try(). 2366 */ 2367 uap->flags &= ~MNT_ROOTFS; 2368 2369 /* 2370 * check that we have an even number of iovec's 2371 * and that we have at least two options. 2372 */ 2373 if ((uap->iovcnt & 1) || (uap->iovcnt < 4)) 2374 return (EINVAL); 2375 2376 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio); 2377 if (error) 2378 return (error); 2379 error = vfs_donmount(td, uap->flags, auio); 2380 2381 free(auio, M_IOV); 2382 return error; 2383} 2384 2385#if 0 2386int 2387freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap) 2388{ 2389 struct yyy32 *p32, s32; 2390 struct yyy *p = NULL, s; 2391 struct xxx_arg ap; 2392 int error; 2393 2394 if (uap->zzz) { 2395 error = copyin(uap->zzz, &s32, sizeof(s32)); 2396 if (error) 2397 return (error); 2398 /* translate in */ 2399 p = &s; 2400 } 2401 error = kern_xxx(td, p); 2402 if (error) 2403 return (error); 2404 if (uap->zzz) { 2405 /* translate out */ 2406 error = copyout(&s32, p32, sizeof(s32)); 2407 } 2408 return (error); 2409} 2410#endif 2411 2412int 2413syscall32_register(int *offset, struct sysent *new_sysent, 2414 struct sysent *old_sysent) 2415{ 2416 if (*offset == NO_SYSCALL) { 2417 int i; 2418 2419 for (i = 1; i < SYS_MAXSYSCALL; ++i) 2420 if (freebsd32_sysent[i].sy_call == 2421 (sy_call_t *)lkmnosys) 2422 break; 2423 if (i == SYS_MAXSYSCALL) 2424 return (ENFILE); 2425 *offset = i; 2426 } else if (*offset < 0 || *offset >= SYS_MAXSYSCALL) 2427 return (EINVAL); 2428 else if (freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmnosys && 2429 freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmressys) 2430 return (EEXIST); 2431 2432 *old_sysent = freebsd32_sysent[*offset]; 2433 freebsd32_sysent[*offset] = *new_sysent; 2434 return 0; 2435} 2436 2437int 2438syscall32_deregister(int *offset, struct sysent *old_sysent) 2439{ 2440 2441 if (*offset) 2442 freebsd32_sysent[*offset] = *old_sysent; 2443 return 0; 2444} 2445 2446int 2447syscall32_module_handler(struct module *mod, int what, void *arg) 2448{ 2449 struct syscall_module_data *data = (struct syscall_module_data*)arg; 2450 modspecific_t ms; 2451 int error; 2452 2453 switch (what) { 2454 case MOD_LOAD: 2455 error = syscall32_register(data->offset, data->new_sysent, 2456 &data->old_sysent); 2457 if (error) { 2458 /* Leave a mark so we know to safely unload below. */ 2459 data->offset = NULL; 2460 return error; 2461 } 2462 ms.intval = *data->offset; 2463 MOD_XLOCK; 2464 module_setspecific(mod, &ms); 2465 MOD_XUNLOCK; 2466 if (data->chainevh) 2467 error = data->chainevh(mod, what, data->chainarg); 2468 return (error); 2469 case MOD_UNLOAD: 2470 /* 2471 * MOD_LOAD failed, so just return without calling the 2472 * chained handler since we didn't pass along the MOD_LOAD 2473 * event. 2474 */ 2475 if (data->offset == NULL) 2476 return (0); 2477 if (data->chainevh) { 2478 error = data->chainevh(mod, what, data->chainarg); 2479 if (error) 2480 return (error); 2481 } 2482 error = syscall32_deregister(data->offset, &data->old_sysent); 2483 return (error); 2484 default: 2485 error = EOPNOTSUPP; 2486 if (data->chainevh) 2487 error = data->chainevh(mod, what, data->chainarg); 2488 return (error); 2489 } 2490} 2491 2492int 2493syscall32_helper_register(struct syscall_helper_data *sd) 2494{ 2495 struct syscall_helper_data *sd1; 2496 int error; 2497 2498 for (sd1 = sd; sd1->syscall_no != NO_SYSCALL; sd1++) { 2499 error = syscall32_register(&sd1->syscall_no, &sd1->new_sysent, 2500 &sd1->old_sysent); 2501 if (error != 0) { 2502 syscall32_helper_unregister(sd); 2503 return (error); 2504 } 2505 sd1->registered = 1; 2506 } 2507 return (0); 2508} 2509 2510int 2511syscall32_helper_unregister(struct syscall_helper_data *sd) 2512{ 2513 struct syscall_helper_data *sd1; 2514 2515 for (sd1 = sd; sd1->registered != 0; sd1++) { 2516 syscall32_deregister(&sd1->syscall_no, &sd1->old_sysent); 2517 sd1->registered = 0; 2518 } 2519 return (0); 2520} 2521 2522register_t * 2523freebsd32_copyout_strings(struct image_params *imgp) 2524{ 2525 int argc, envc; 2526 u_int32_t *vectp; 2527 char *stringp, *destp; 2528 u_int32_t *stack_base; 2529 struct freebsd32_ps_strings *arginfo; 2530 size_t execpath_len; 2531 int szsigcode; 2532 2533 /* 2534 * Calculate string base and vector table pointers. 2535 * Also deal with signal trampoline code for this exec type. 2536 */ 2537 if (imgp->execpath != NULL && imgp->auxargs != NULL) 2538 execpath_len = strlen(imgp->execpath) + 1; 2539 else 2540 execpath_len = 0; 2541 arginfo = (struct freebsd32_ps_strings *)FREEBSD32_PS_STRINGS; 2542 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode); 2543 destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE - 2544 roundup(execpath_len, sizeof(char *)) - 2545 roundup((ARG_MAX - imgp->args->stringspace), sizeof(char *)); 2546 2547 /* 2548 * install sigcode 2549 */ 2550 if (szsigcode) 2551 copyout(imgp->proc->p_sysent->sv_sigcode, 2552 ((caddr_t)arginfo - szsigcode), szsigcode); 2553 2554 /* 2555 * Copy the image path for the rtld. 2556 */ 2557 if (execpath_len != 0) { 2558 imgp->execpathp = (uintptr_t)arginfo - szsigcode - execpath_len; 2559 copyout(imgp->execpath, (void *)imgp->execpathp, 2560 execpath_len); 2561 } 2562 2563 /* 2564 * If we have a valid auxargs ptr, prepare some room 2565 * on the stack. 2566 */ 2567 if (imgp->auxargs) { 2568 /* 2569 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for 2570 * lower compatibility. 2571 */ 2572 imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size 2573 : (AT_COUNT * 2); 2574 /* 2575 * The '+ 2' is for the null pointers at the end of each of 2576 * the arg and env vector sets,and imgp->auxarg_size is room 2577 * for argument of Runtime loader. 2578 */ 2579 vectp = (u_int32_t *) (destp - (imgp->args->argc + 2580 imgp->args->envc + 2 + imgp->auxarg_size + execpath_len) * 2581 sizeof(u_int32_t)); 2582 } else 2583 /* 2584 * The '+ 2' is for the null pointers at the end of each of 2585 * the arg and env vector sets 2586 */ 2587 vectp = (u_int32_t *) 2588 (destp - (imgp->args->argc + imgp->args->envc + 2) * sizeof(u_int32_t)); 2589 2590 /* 2591 * vectp also becomes our initial stack base 2592 */ 2593 stack_base = vectp; 2594 2595 stringp = imgp->args->begin_argv; 2596 argc = imgp->args->argc; 2597 envc = imgp->args->envc; 2598 /* 2599 * Copy out strings - arguments and environment. 2600 */ 2601 copyout(stringp, destp, ARG_MAX - imgp->args->stringspace); 2602 2603 /* 2604 * Fill in "ps_strings" struct for ps, w, etc. 2605 */ 2606 suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp); 2607 suword32(&arginfo->ps_nargvstr, argc); 2608 2609 /* 2610 * Fill in argument portion of vector table. 2611 */ 2612 for (; argc > 0; --argc) { 2613 suword32(vectp++, (u_int32_t)(intptr_t)destp); 2614 while (*stringp++ != 0) 2615 destp++; 2616 destp++; 2617 } 2618 2619 /* a null vector table pointer separates the argp's from the envp's */ 2620 suword32(vectp++, 0); 2621 2622 suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp); 2623 suword32(&arginfo->ps_nenvstr, envc); 2624 2625 /* 2626 * Fill in environment portion of vector table. 2627 */ 2628 for (; envc > 0; --envc) { 2629 suword32(vectp++, (u_int32_t)(intptr_t)destp); 2630 while (*stringp++ != 0) 2631 destp++; 2632 destp++; 2633 } 2634 2635 /* end of vector table is a null pointer */ 2636 suword32(vectp, 0); 2637 2638 return ((register_t *)stack_base); 2639} 2640 2641