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