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