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_SETREGSET: 1030 error = copyin(uap->addr, &r32.vec, sizeof(r32.vec)); 1031 if (error != 0) 1032 break; 1033 1034 r.vec.iov_len = r32.vec.iov_len; 1035 r.vec.iov_base = PTRIN(r32.vec.iov_base); 1036 break; 1037 case PT_GETREGSET: 1038 error = copyin(uap->addr, &r32.vec, sizeof(r32.vec)); 1039 if (error != 0) 1040 break; 1041 1042 r.vec.iov_len = r32.vec.iov_len; 1043 r.vec.iov_base = PTRIN(r32.vec.iov_base); 1044 break; 1045 case PT_SET_EVENT_MASK: 1046 if (uap->data != sizeof(r.ptevents)) 1047 error = EINVAL; 1048 else 1049 error = copyin(uap->addr, &r.ptevents, uap->data); 1050 break; 1051 case PT_IO: 1052 error = copyin(uap->addr, &r32.piod, sizeof(r32.piod)); 1053 if (error) 1054 break; 1055 CP(r32.piod, r.piod, piod_op); 1056 PTRIN_CP(r32.piod, r.piod, piod_offs); 1057 PTRIN_CP(r32.piod, r.piod, piod_addr); 1058 CP(r32.piod, r.piod, piod_len); 1059 break; 1060 case PT_VM_ENTRY: 1061 error = copyin(uap->addr, &r32.pve, sizeof(r32.pve)); 1062 if (error) 1063 break; 1064 1065 CP(r32.pve, r.pve, pve_entry); 1066 CP(r32.pve, r.pve, pve_timestamp); 1067 CP(r32.pve, r.pve, pve_start); 1068 CP(r32.pve, r.pve, pve_end); 1069 CP(r32.pve, r.pve, pve_offset); 1070 CP(r32.pve, r.pve, pve_prot); 1071 CP(r32.pve, r.pve, pve_pathlen); 1072 CP(r32.pve, r.pve, pve_fileid); 1073 CP(r32.pve, r.pve, pve_fsid); 1074 PTRIN_CP(r32.pve, r.pve, pve_path); 1075 break; 1076 case PT_COREDUMP: 1077 if (uap->data != sizeof(r32.pc)) 1078 error = EINVAL; 1079 else 1080 error = copyin(uap->addr, &r32.pc, uap->data); 1081 CP(r32.pc, r.pc, pc_fd); 1082 CP(r32.pc, r.pc, pc_flags); 1083 r.pc.pc_limit = PAIR32TO64(off_t, r32.pc.pc_limit); 1084 data = sizeof(r.pc); 1085 break; 1086 default: 1087 addr = uap->addr; 1088 break; 1089 } 1090 if (error) 1091 return (error); 1092 1093 error = kern_ptrace(td, uap->req, uap->pid, addr, data); 1094 if (error) 1095 return (error); 1096 1097 switch (uap->req) { 1098 case PT_VM_ENTRY: 1099 CP(r.pve, r32.pve, pve_entry); 1100 CP(r.pve, r32.pve, pve_timestamp); 1101 CP(r.pve, r32.pve, pve_start); 1102 CP(r.pve, r32.pve, pve_end); 1103 CP(r.pve, r32.pve, pve_offset); 1104 CP(r.pve, r32.pve, pve_prot); 1105 CP(r.pve, r32.pve, pve_pathlen); 1106 CP(r.pve, r32.pve, pve_fileid); 1107 CP(r.pve, r32.pve, pve_fsid); 1108 error = copyout(&r32.pve, uap->addr, sizeof(r32.pve)); 1109 break; 1110 case PT_IO: 1111 CP(r.piod, r32.piod, piod_len); 1112 error = copyout(&r32.piod, uap->addr, sizeof(r32.piod)); 1113 break; 1114 case PT_GETREGS: 1115 error = copyout(&r.reg, uap->addr, sizeof(r.reg)); 1116 break; 1117 case PT_GETFPREGS: 1118 error = copyout(&r.fpreg, uap->addr, sizeof(r.fpreg)); 1119 break; 1120 case PT_GETDBREGS: 1121 error = copyout(&r.dbreg, uap->addr, sizeof(r.dbreg)); 1122 break; 1123 case PT_GETREGSET: 1124 r32.vec.iov_len = r.vec.iov_len; 1125 error = copyout(&r32.vec, uap->addr, sizeof(r32.vec)); 1126 break; 1127 case PT_GET_EVENT_MASK: 1128 /* NB: The size in uap->data is validated in kern_ptrace(). */ 1129 error = copyout(&r.ptevents, uap->addr, uap->data); 1130 break; 1131 case PT_LWPINFO: 1132 ptrace_lwpinfo_to32(&r.pl, &r32.pl); 1133 error = copyout(&r32.pl, uap->addr, uap->data); 1134 break; 1135 case PT_GET_SC_ARGS: 1136 for (i = 0; i < nitems(r.args); i++) 1137 r32.args[i] = (uint32_t)r.args[i]; 1138 error = copyout(r32.args, uap->addr, MIN(uap->data, 1139 sizeof(r32.args))); 1140 break; 1141 case PT_GET_SC_RET: 1142 ptrace_sc_ret_to32(&r.psr, &r32.psr); 1143 error = copyout(&r32.psr, uap->addr, MIN(uap->data, 1144 sizeof(r32.psr))); 1145 break; 1146 } 1147 1148 return (error); 1149 } 1150 1151 int 1152 freebsd32_copyinuio(struct iovec32 *iovp, u_int iovcnt, struct uio **uiop) 1153 { 1154 struct iovec32 iov32; 1155 struct iovec *iov; 1156 struct uio *uio; 1157 u_int iovlen; 1158 int error, i; 1159 1160 *uiop = NULL; 1161 if (iovcnt > UIO_MAXIOV) 1162 return (EINVAL); 1163 iovlen = iovcnt * sizeof(struct iovec); 1164 uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK); 1165 iov = (struct iovec *)(uio + 1); 1166 for (i = 0; i < iovcnt; i++) { 1167 error = copyin(&iovp[i], &iov32, sizeof(struct iovec32)); 1168 if (error) { 1169 free(uio, M_IOV); 1170 return (error); 1171 } 1172 iov[i].iov_base = PTRIN(iov32.iov_base); 1173 iov[i].iov_len = iov32.iov_len; 1174 } 1175 uio->uio_iov = iov; 1176 uio->uio_iovcnt = iovcnt; 1177 uio->uio_segflg = UIO_USERSPACE; 1178 uio->uio_offset = -1; 1179 uio->uio_resid = 0; 1180 for (i = 0; i < iovcnt; i++) { 1181 if (iov->iov_len > INT_MAX - uio->uio_resid) { 1182 free(uio, M_IOV); 1183 return (EINVAL); 1184 } 1185 uio->uio_resid += iov->iov_len; 1186 iov++; 1187 } 1188 *uiop = uio; 1189 return (0); 1190 } 1191 1192 int 1193 freebsd32_readv(struct thread *td, struct freebsd32_readv_args *uap) 1194 { 1195 struct uio *auio; 1196 int error; 1197 1198 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio); 1199 if (error) 1200 return (error); 1201 error = kern_readv(td, uap->fd, auio); 1202 free(auio, M_IOV); 1203 return (error); 1204 } 1205 1206 int 1207 freebsd32_writev(struct thread *td, struct freebsd32_writev_args *uap) 1208 { 1209 struct uio *auio; 1210 int error; 1211 1212 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio); 1213 if (error) 1214 return (error); 1215 error = kern_writev(td, uap->fd, auio); 1216 free(auio, M_IOV); 1217 return (error); 1218 } 1219 1220 int 1221 freebsd32_preadv(struct thread *td, struct freebsd32_preadv_args *uap) 1222 { 1223 struct uio *auio; 1224 int error; 1225 1226 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio); 1227 if (error) 1228 return (error); 1229 error = kern_preadv(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset)); 1230 free(auio, M_IOV); 1231 return (error); 1232 } 1233 1234 int 1235 freebsd32_pwritev(struct thread *td, struct freebsd32_pwritev_args *uap) 1236 { 1237 struct uio *auio; 1238 int error; 1239 1240 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio); 1241 if (error) 1242 return (error); 1243 error = kern_pwritev(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset)); 1244 free(auio, M_IOV); 1245 return (error); 1246 } 1247 1248 int 1249 freebsd32_copyiniov(struct iovec32 *iovp32, u_int iovcnt, struct iovec **iovp, 1250 int error) 1251 { 1252 struct iovec32 iov32; 1253 struct iovec *iov; 1254 u_int iovlen; 1255 int i; 1256 1257 *iovp = NULL; 1258 if (iovcnt > UIO_MAXIOV) 1259 return (error); 1260 iovlen = iovcnt * sizeof(struct iovec); 1261 iov = malloc(iovlen, M_IOV, M_WAITOK); 1262 for (i = 0; i < iovcnt; i++) { 1263 error = copyin(&iovp32[i], &iov32, sizeof(struct iovec32)); 1264 if (error) { 1265 free(iov, M_IOV); 1266 return (error); 1267 } 1268 iov[i].iov_base = PTRIN(iov32.iov_base); 1269 iov[i].iov_len = iov32.iov_len; 1270 } 1271 *iovp = iov; 1272 return (0); 1273 } 1274 1275 static int 1276 freebsd32_copyinmsghdr(const struct msghdr32 *msg32, struct msghdr *msg) 1277 { 1278 struct msghdr32 m32; 1279 int error; 1280 1281 error = copyin(msg32, &m32, sizeof(m32)); 1282 if (error) 1283 return (error); 1284 msg->msg_name = PTRIN(m32.msg_name); 1285 msg->msg_namelen = m32.msg_namelen; 1286 msg->msg_iov = PTRIN(m32.msg_iov); 1287 msg->msg_iovlen = m32.msg_iovlen; 1288 msg->msg_control = PTRIN(m32.msg_control); 1289 msg->msg_controllen = m32.msg_controllen; 1290 msg->msg_flags = m32.msg_flags; 1291 return (0); 1292 } 1293 1294 static int 1295 freebsd32_copyoutmsghdr(struct msghdr *msg, struct msghdr32 *msg32) 1296 { 1297 struct msghdr32 m32; 1298 int error; 1299 1300 m32.msg_name = PTROUT(msg->msg_name); 1301 m32.msg_namelen = msg->msg_namelen; 1302 m32.msg_iov = PTROUT(msg->msg_iov); 1303 m32.msg_iovlen = msg->msg_iovlen; 1304 m32.msg_control = PTROUT(msg->msg_control); 1305 m32.msg_controllen = msg->msg_controllen; 1306 m32.msg_flags = msg->msg_flags; 1307 error = copyout(&m32, msg32, sizeof(m32)); 1308 return (error); 1309 } 1310 1311 #ifndef __mips__ 1312 #define FREEBSD32_ALIGNBYTES (sizeof(int) - 1) 1313 #else 1314 #define FREEBSD32_ALIGNBYTES (sizeof(long) - 1) 1315 #endif 1316 #define FREEBSD32_ALIGN(p) \ 1317 (((u_long)(p) + FREEBSD32_ALIGNBYTES) & ~FREEBSD32_ALIGNBYTES) 1318 #define FREEBSD32_CMSG_SPACE(l) \ 1319 (FREEBSD32_ALIGN(sizeof(struct cmsghdr)) + FREEBSD32_ALIGN(l)) 1320 1321 #define FREEBSD32_CMSG_DATA(cmsg) ((unsigned char *)(cmsg) + \ 1322 FREEBSD32_ALIGN(sizeof(struct cmsghdr))) 1323 1324 static size_t 1325 freebsd32_cmsg_convert(const struct cmsghdr *cm, void *data, socklen_t datalen) 1326 { 1327 size_t copylen; 1328 union { 1329 struct timespec32 ts; 1330 struct timeval32 tv; 1331 struct bintime32 bt; 1332 } tmp32; 1333 1334 union { 1335 struct timespec ts; 1336 struct timeval tv; 1337 struct bintime bt; 1338 } *in; 1339 1340 in = data; 1341 copylen = 0; 1342 switch (cm->cmsg_level) { 1343 case SOL_SOCKET: 1344 switch (cm->cmsg_type) { 1345 case SCM_TIMESTAMP: 1346 TV_CP(*in, tmp32, tv); 1347 copylen = sizeof(tmp32.tv); 1348 break; 1349 1350 case SCM_BINTIME: 1351 BT_CP(*in, tmp32, bt); 1352 copylen = sizeof(tmp32.bt); 1353 break; 1354 1355 case SCM_REALTIME: 1356 case SCM_MONOTONIC: 1357 TS_CP(*in, tmp32, ts); 1358 copylen = sizeof(tmp32.ts); 1359 break; 1360 1361 default: 1362 break; 1363 } 1364 1365 default: 1366 break; 1367 } 1368 1369 if (copylen == 0) 1370 return (datalen); 1371 1372 KASSERT((datalen >= copylen), ("corrupted cmsghdr")); 1373 1374 bcopy(&tmp32, data, copylen); 1375 return (copylen); 1376 } 1377 1378 static int 1379 freebsd32_copy_msg_out(struct msghdr *msg, struct mbuf *control) 1380 { 1381 struct cmsghdr *cm; 1382 void *data; 1383 socklen_t clen, datalen, datalen_out, oldclen; 1384 int error; 1385 caddr_t ctlbuf; 1386 int len, copylen; 1387 struct mbuf *m; 1388 error = 0; 1389 1390 len = msg->msg_controllen; 1391 msg->msg_controllen = 0; 1392 1393 ctlbuf = msg->msg_control; 1394 for (m = control; m != NULL && len > 0; m = m->m_next) { 1395 cm = mtod(m, struct cmsghdr *); 1396 clen = m->m_len; 1397 while (cm != NULL) { 1398 if (sizeof(struct cmsghdr) > clen || 1399 cm->cmsg_len > clen) { 1400 error = EINVAL; 1401 break; 1402 } 1403 1404 data = CMSG_DATA(cm); 1405 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 1406 datalen_out = freebsd32_cmsg_convert(cm, data, datalen); 1407 1408 /* 1409 * Copy out the message header. Preserve the native 1410 * message size in case we need to inspect the message 1411 * contents later. 1412 */ 1413 copylen = sizeof(struct cmsghdr); 1414 if (len < copylen) { 1415 msg->msg_flags |= MSG_CTRUNC; 1416 m_dispose_extcontrolm(m); 1417 goto exit; 1418 } 1419 oldclen = cm->cmsg_len; 1420 cm->cmsg_len = FREEBSD32_ALIGN(sizeof(struct cmsghdr)) + 1421 datalen_out; 1422 error = copyout(cm, ctlbuf, copylen); 1423 cm->cmsg_len = oldclen; 1424 if (error != 0) 1425 goto exit; 1426 1427 ctlbuf += FREEBSD32_ALIGN(copylen); 1428 len -= FREEBSD32_ALIGN(copylen); 1429 1430 copylen = datalen_out; 1431 if (len < copylen) { 1432 msg->msg_flags |= MSG_CTRUNC; 1433 m_dispose_extcontrolm(m); 1434 break; 1435 } 1436 1437 /* Copy out the message data. */ 1438 error = copyout(data, ctlbuf, copylen); 1439 if (error) 1440 goto exit; 1441 1442 ctlbuf += FREEBSD32_ALIGN(copylen); 1443 len -= FREEBSD32_ALIGN(copylen); 1444 1445 if (CMSG_SPACE(datalen) < clen) { 1446 clen -= CMSG_SPACE(datalen); 1447 cm = (struct cmsghdr *) 1448 ((caddr_t)cm + CMSG_SPACE(datalen)); 1449 } else { 1450 clen = 0; 1451 cm = NULL; 1452 } 1453 1454 msg->msg_controllen += 1455 FREEBSD32_CMSG_SPACE(datalen_out); 1456 } 1457 } 1458 if (len == 0 && m != NULL) { 1459 msg->msg_flags |= MSG_CTRUNC; 1460 m_dispose_extcontrolm(m); 1461 } 1462 1463 exit: 1464 return (error); 1465 } 1466 1467 int 1468 freebsd32_recvmsg(struct thread *td, struct freebsd32_recvmsg_args *uap) 1469 { 1470 struct msghdr msg; 1471 struct iovec *uiov, *iov; 1472 struct mbuf *control = NULL; 1473 struct mbuf **controlp; 1474 int error; 1475 1476 error = freebsd32_copyinmsghdr(uap->msg, &msg); 1477 if (error) 1478 return (error); 1479 error = freebsd32_copyiniov((void *)msg.msg_iov, msg.msg_iovlen, &iov, 1480 EMSGSIZE); 1481 if (error) 1482 return (error); 1483 msg.msg_flags = uap->flags; 1484 uiov = msg.msg_iov; 1485 msg.msg_iov = iov; 1486 1487 controlp = (msg.msg_control != NULL) ? &control : NULL; 1488 error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, controlp); 1489 if (error == 0) { 1490 msg.msg_iov = uiov; 1491 1492 if (control != NULL) 1493 error = freebsd32_copy_msg_out(&msg, control); 1494 else 1495 msg.msg_controllen = 0; 1496 1497 if (error == 0) 1498 error = freebsd32_copyoutmsghdr(&msg, uap->msg); 1499 } 1500 free(iov, M_IOV); 1501 1502 if (control != NULL) { 1503 if (error != 0) 1504 m_dispose_extcontrolm(control); 1505 m_freem(control); 1506 } 1507 1508 return (error); 1509 } 1510 1511 #ifdef COMPAT_43 1512 int 1513 ofreebsd32_recvmsg(struct thread *td, struct ofreebsd32_recvmsg_args *uap) 1514 { 1515 return (ENOSYS); 1516 } 1517 #endif 1518 1519 /* 1520 * Copy-in the array of control messages constructed using alignment 1521 * and padding suitable for a 32-bit environment and construct an 1522 * mbuf using alignment and padding suitable for a 64-bit kernel. 1523 * The alignment and padding are defined indirectly by CMSG_DATA(), 1524 * CMSG_SPACE() and CMSG_LEN(). 1525 */ 1526 static int 1527 freebsd32_copyin_control(struct mbuf **mp, caddr_t buf, u_int buflen) 1528 { 1529 struct cmsghdr *cm; 1530 struct mbuf *m; 1531 void *in, *in1, *md; 1532 u_int msglen, outlen; 1533 int error; 1534 1535 if (buflen > MCLBYTES) 1536 return (EINVAL); 1537 1538 in = malloc(buflen, M_TEMP, M_WAITOK); 1539 error = copyin(buf, in, buflen); 1540 if (error != 0) 1541 goto out; 1542 1543 /* 1544 * Make a pass over the input buffer to determine the amount of space 1545 * required for 64 bit-aligned copies of the control messages. 1546 */ 1547 in1 = in; 1548 outlen = 0; 1549 while (buflen > 0) { 1550 if (buflen < sizeof(*cm)) { 1551 error = EINVAL; 1552 break; 1553 } 1554 cm = (struct cmsghdr *)in1; 1555 if (cm->cmsg_len < FREEBSD32_ALIGN(sizeof(*cm))) { 1556 error = EINVAL; 1557 break; 1558 } 1559 msglen = FREEBSD32_ALIGN(cm->cmsg_len); 1560 if (msglen > buflen || msglen < cm->cmsg_len) { 1561 error = EINVAL; 1562 break; 1563 } 1564 buflen -= msglen; 1565 1566 in1 = (char *)in1 + msglen; 1567 outlen += CMSG_ALIGN(sizeof(*cm)) + 1568 CMSG_ALIGN(msglen - FREEBSD32_ALIGN(sizeof(*cm))); 1569 } 1570 if (error == 0 && outlen > MCLBYTES) { 1571 /* 1572 * XXXMJ This implies that the upper limit on 32-bit aligned 1573 * control messages is less than MCLBYTES, and so we are not 1574 * perfectly compatible. However, there is no platform 1575 * guarantee that mbuf clusters larger than MCLBYTES can be 1576 * allocated. 1577 */ 1578 error = EINVAL; 1579 } 1580 if (error != 0) 1581 goto out; 1582 1583 m = m_get2(outlen, M_WAITOK, MT_CONTROL, 0); 1584 m->m_len = outlen; 1585 md = mtod(m, void *); 1586 1587 /* 1588 * Make a second pass over input messages, copying them into the output 1589 * buffer. 1590 */ 1591 in1 = in; 1592 while (outlen > 0) { 1593 /* Copy the message header and align the length field. */ 1594 cm = md; 1595 memcpy(cm, in1, sizeof(*cm)); 1596 msglen = cm->cmsg_len - FREEBSD32_ALIGN(sizeof(*cm)); 1597 cm->cmsg_len = CMSG_ALIGN(sizeof(*cm)) + msglen; 1598 1599 /* Copy the message body. */ 1600 in1 = (char *)in1 + FREEBSD32_ALIGN(sizeof(*cm)); 1601 md = (char *)md + CMSG_ALIGN(sizeof(*cm)); 1602 memcpy(md, in1, msglen); 1603 in1 = (char *)in1 + FREEBSD32_ALIGN(msglen); 1604 md = (char *)md + CMSG_ALIGN(msglen); 1605 KASSERT(outlen >= CMSG_ALIGN(sizeof(*cm)) + CMSG_ALIGN(msglen), 1606 ("outlen %u underflow, msglen %u", outlen, msglen)); 1607 outlen -= CMSG_ALIGN(sizeof(*cm)) + CMSG_ALIGN(msglen); 1608 } 1609 1610 *mp = m; 1611 out: 1612 free(in, M_TEMP); 1613 return (error); 1614 } 1615 1616 int 1617 freebsd32_sendmsg(struct thread *td, struct freebsd32_sendmsg_args *uap) 1618 { 1619 struct msghdr msg; 1620 struct iovec *iov; 1621 struct mbuf *control = NULL; 1622 struct sockaddr *to = NULL; 1623 int error; 1624 1625 error = freebsd32_copyinmsghdr(uap->msg, &msg); 1626 if (error) 1627 return (error); 1628 error = freebsd32_copyiniov((void *)msg.msg_iov, msg.msg_iovlen, &iov, 1629 EMSGSIZE); 1630 if (error) 1631 return (error); 1632 msg.msg_iov = iov; 1633 if (msg.msg_name != NULL) { 1634 error = getsockaddr(&to, msg.msg_name, msg.msg_namelen); 1635 if (error) { 1636 to = NULL; 1637 goto out; 1638 } 1639 msg.msg_name = to; 1640 } 1641 1642 if (msg.msg_control) { 1643 if (msg.msg_controllen < sizeof(struct cmsghdr)) { 1644 error = EINVAL; 1645 goto out; 1646 } 1647 1648 error = freebsd32_copyin_control(&control, msg.msg_control, 1649 msg.msg_controllen); 1650 if (error) 1651 goto out; 1652 1653 msg.msg_control = NULL; 1654 msg.msg_controllen = 0; 1655 } 1656 1657 error = kern_sendit(td, uap->s, &msg, uap->flags, control, 1658 UIO_USERSPACE); 1659 1660 out: 1661 free(iov, M_IOV); 1662 if (to) 1663 free(to, M_SONAME); 1664 return (error); 1665 } 1666 1667 #ifdef COMPAT_43 1668 int 1669 ofreebsd32_sendmsg(struct thread *td, struct ofreebsd32_sendmsg_args *uap) 1670 { 1671 return (ENOSYS); 1672 } 1673 #endif 1674 1675 1676 int 1677 freebsd32_settimeofday(struct thread *td, 1678 struct freebsd32_settimeofday_args *uap) 1679 { 1680 struct timeval32 tv32; 1681 struct timeval tv, *tvp; 1682 struct timezone tz, *tzp; 1683 int error; 1684 1685 if (uap->tv) { 1686 error = copyin(uap->tv, &tv32, sizeof(tv32)); 1687 if (error) 1688 return (error); 1689 CP(tv32, tv, tv_sec); 1690 CP(tv32, tv, tv_usec); 1691 tvp = &tv; 1692 } else 1693 tvp = NULL; 1694 if (uap->tzp) { 1695 error = copyin(uap->tzp, &tz, sizeof(tz)); 1696 if (error) 1697 return (error); 1698 tzp = &tz; 1699 } else 1700 tzp = NULL; 1701 return (kern_settimeofday(td, tvp, tzp)); 1702 } 1703 1704 int 1705 freebsd32_utimes(struct thread *td, struct freebsd32_utimes_args *uap) 1706 { 1707 struct timeval32 s32[2]; 1708 struct timeval s[2], *sp; 1709 int error; 1710 1711 if (uap->tptr != NULL) { 1712 error = copyin(uap->tptr, s32, sizeof(s32)); 1713 if (error) 1714 return (error); 1715 CP(s32[0], s[0], tv_sec); 1716 CP(s32[0], s[0], tv_usec); 1717 CP(s32[1], s[1], tv_sec); 1718 CP(s32[1], s[1], tv_usec); 1719 sp = s; 1720 } else 1721 sp = NULL; 1722 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 1723 sp, UIO_SYSSPACE)); 1724 } 1725 1726 int 1727 freebsd32_lutimes(struct thread *td, struct freebsd32_lutimes_args *uap) 1728 { 1729 struct timeval32 s32[2]; 1730 struct timeval s[2], *sp; 1731 int error; 1732 1733 if (uap->tptr != NULL) { 1734 error = copyin(uap->tptr, s32, sizeof(s32)); 1735 if (error) 1736 return (error); 1737 CP(s32[0], s[0], tv_sec); 1738 CP(s32[0], s[0], tv_usec); 1739 CP(s32[1], s[1], tv_sec); 1740 CP(s32[1], s[1], tv_usec); 1741 sp = s; 1742 } else 1743 sp = NULL; 1744 return (kern_lutimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE)); 1745 } 1746 1747 int 1748 freebsd32_futimes(struct thread *td, struct freebsd32_futimes_args *uap) 1749 { 1750 struct timeval32 s32[2]; 1751 struct timeval s[2], *sp; 1752 int error; 1753 1754 if (uap->tptr != NULL) { 1755 error = copyin(uap->tptr, s32, sizeof(s32)); 1756 if (error) 1757 return (error); 1758 CP(s32[0], s[0], tv_sec); 1759 CP(s32[0], s[0], tv_usec); 1760 CP(s32[1], s[1], tv_sec); 1761 CP(s32[1], s[1], tv_usec); 1762 sp = s; 1763 } else 1764 sp = NULL; 1765 return (kern_futimes(td, uap->fd, sp, UIO_SYSSPACE)); 1766 } 1767 1768 int 1769 freebsd32_futimesat(struct thread *td, struct freebsd32_futimesat_args *uap) 1770 { 1771 struct timeval32 s32[2]; 1772 struct timeval s[2], *sp; 1773 int error; 1774 1775 if (uap->times != NULL) { 1776 error = copyin(uap->times, s32, sizeof(s32)); 1777 if (error) 1778 return (error); 1779 CP(s32[0], s[0], tv_sec); 1780 CP(s32[0], s[0], tv_usec); 1781 CP(s32[1], s[1], tv_sec); 1782 CP(s32[1], s[1], tv_usec); 1783 sp = s; 1784 } else 1785 sp = NULL; 1786 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE, 1787 sp, UIO_SYSSPACE)); 1788 } 1789 1790 int 1791 freebsd32_futimens(struct thread *td, struct freebsd32_futimens_args *uap) 1792 { 1793 struct timespec32 ts32[2]; 1794 struct timespec ts[2], *tsp; 1795 int error; 1796 1797 if (uap->times != NULL) { 1798 error = copyin(uap->times, ts32, sizeof(ts32)); 1799 if (error) 1800 return (error); 1801 CP(ts32[0], ts[0], tv_sec); 1802 CP(ts32[0], ts[0], tv_nsec); 1803 CP(ts32[1], ts[1], tv_sec); 1804 CP(ts32[1], ts[1], tv_nsec); 1805 tsp = ts; 1806 } else 1807 tsp = NULL; 1808 return (kern_futimens(td, uap->fd, tsp, UIO_SYSSPACE)); 1809 } 1810 1811 int 1812 freebsd32_utimensat(struct thread *td, struct freebsd32_utimensat_args *uap) 1813 { 1814 struct timespec32 ts32[2]; 1815 struct timespec ts[2], *tsp; 1816 int error; 1817 1818 if (uap->times != NULL) { 1819 error = copyin(uap->times, ts32, sizeof(ts32)); 1820 if (error) 1821 return (error); 1822 CP(ts32[0], ts[0], tv_sec); 1823 CP(ts32[0], ts[0], tv_nsec); 1824 CP(ts32[1], ts[1], tv_sec); 1825 CP(ts32[1], ts[1], tv_nsec); 1826 tsp = ts; 1827 } else 1828 tsp = NULL; 1829 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE, 1830 tsp, UIO_SYSSPACE, uap->flag)); 1831 } 1832 1833 int 1834 freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap) 1835 { 1836 struct timeval32 tv32; 1837 struct timeval delta, olddelta, *deltap; 1838 int error; 1839 1840 if (uap->delta) { 1841 error = copyin(uap->delta, &tv32, sizeof(tv32)); 1842 if (error) 1843 return (error); 1844 CP(tv32, delta, tv_sec); 1845 CP(tv32, delta, tv_usec); 1846 deltap = δ 1847 } else 1848 deltap = NULL; 1849 error = kern_adjtime(td, deltap, &olddelta); 1850 if (uap->olddelta && error == 0) { 1851 CP(olddelta, tv32, tv_sec); 1852 CP(olddelta, tv32, tv_usec); 1853 error = copyout(&tv32, uap->olddelta, sizeof(tv32)); 1854 } 1855 return (error); 1856 } 1857 1858 #ifdef COMPAT_FREEBSD4 1859 int 1860 freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap) 1861 { 1862 struct ostatfs32 s32; 1863 struct statfs *sp; 1864 int error; 1865 1866 sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK); 1867 error = kern_statfs(td, uap->path, UIO_USERSPACE, sp); 1868 if (error == 0) { 1869 copy_statfs(sp, &s32); 1870 error = copyout(&s32, uap->buf, sizeof(s32)); 1871 } 1872 free(sp, M_STATFS); 1873 return (error); 1874 } 1875 #endif 1876 1877 #ifdef COMPAT_FREEBSD4 1878 int 1879 freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap) 1880 { 1881 struct ostatfs32 s32; 1882 struct statfs *sp; 1883 int error; 1884 1885 sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK); 1886 error = kern_fstatfs(td, uap->fd, sp); 1887 if (error == 0) { 1888 copy_statfs(sp, &s32); 1889 error = copyout(&s32, uap->buf, sizeof(s32)); 1890 } 1891 free(sp, M_STATFS); 1892 return (error); 1893 } 1894 #endif 1895 1896 #ifdef COMPAT_FREEBSD4 1897 int 1898 freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap) 1899 { 1900 struct ostatfs32 s32; 1901 struct statfs *sp; 1902 fhandle_t fh; 1903 int error; 1904 1905 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0) 1906 return (error); 1907 sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK); 1908 error = kern_fhstatfs(td, fh, sp); 1909 if (error == 0) { 1910 copy_statfs(sp, &s32); 1911 error = copyout(&s32, uap->buf, sizeof(s32)); 1912 } 1913 free(sp, M_STATFS); 1914 return (error); 1915 } 1916 #endif 1917 1918 int 1919 freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap) 1920 { 1921 1922 return (kern_pread(td, uap->fd, uap->buf, uap->nbyte, 1923 PAIR32TO64(off_t, uap->offset))); 1924 } 1925 1926 int 1927 freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap) 1928 { 1929 1930 return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, 1931 PAIR32TO64(off_t, uap->offset))); 1932 } 1933 1934 #ifdef COMPAT_43 1935 int 1936 ofreebsd32_lseek(struct thread *td, struct ofreebsd32_lseek_args *uap) 1937 { 1938 1939 return (kern_lseek(td, uap->fd, uap->offset, uap->whence)); 1940 } 1941 #endif 1942 1943 int 1944 freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap) 1945 { 1946 int error; 1947 off_t pos; 1948 1949 error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset), 1950 uap->whence); 1951 /* Expand the quad return into two parts for eax and edx */ 1952 pos = td->td_uretoff.tdu_off; 1953 td->td_retval[RETVAL_LO] = pos & 0xffffffff; /* %eax */ 1954 td->td_retval[RETVAL_HI] = pos >> 32; /* %edx */ 1955 return error; 1956 } 1957 1958 int 1959 freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap) 1960 { 1961 1962 return (kern_truncate(td, uap->path, UIO_USERSPACE, 1963 PAIR32TO64(off_t, uap->length))); 1964 } 1965 1966 #ifdef COMPAT_43 1967 int 1968 ofreebsd32_truncate(struct thread *td, struct ofreebsd32_truncate_args *uap) 1969 { 1970 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length)); 1971 } 1972 #endif 1973 1974 int 1975 freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap) 1976 { 1977 1978 return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length))); 1979 } 1980 1981 #ifdef COMPAT_43 1982 int 1983 ofreebsd32_ftruncate(struct thread *td, struct ofreebsd32_ftruncate_args *uap) 1984 { 1985 return (kern_ftruncate(td, uap->fd, uap->length)); 1986 } 1987 1988 int 1989 ofreebsd32_getdirentries(struct thread *td, 1990 struct ofreebsd32_getdirentries_args *uap) 1991 { 1992 struct ogetdirentries_args ap; 1993 int error; 1994 long loff; 1995 int32_t loff_cut; 1996 1997 ap.fd = uap->fd; 1998 ap.buf = uap->buf; 1999 ap.count = uap->count; 2000 ap.basep = NULL; 2001 error = kern_ogetdirentries(td, &ap, &loff); 2002 if (error == 0) { 2003 loff_cut = loff; 2004 error = copyout(&loff_cut, uap->basep, sizeof(int32_t)); 2005 } 2006 return (error); 2007 } 2008 #endif 2009 2010 #if defined(COMPAT_FREEBSD11) 2011 int 2012 freebsd11_freebsd32_getdirentries(struct thread *td, 2013 struct freebsd11_freebsd32_getdirentries_args *uap) 2014 { 2015 long base; 2016 int32_t base32; 2017 int error; 2018 2019 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count, 2020 &base, NULL); 2021 if (error) 2022 return (error); 2023 if (uap->basep != NULL) { 2024 base32 = base; 2025 error = copyout(&base32, uap->basep, sizeof(int32_t)); 2026 } 2027 return (error); 2028 } 2029 #endif /* COMPAT_FREEBSD11 */ 2030 2031 #ifdef COMPAT_FREEBSD6 2032 /* versions with the 'int pad' argument */ 2033 int 2034 freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap) 2035 { 2036 2037 return (kern_pread(td, uap->fd, uap->buf, uap->nbyte, 2038 PAIR32TO64(off_t, uap->offset))); 2039 } 2040 2041 int 2042 freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap) 2043 { 2044 2045 return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, 2046 PAIR32TO64(off_t, uap->offset))); 2047 } 2048 2049 int 2050 freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap) 2051 { 2052 int error; 2053 off_t pos; 2054 2055 error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset), 2056 uap->whence); 2057 /* Expand the quad return into two parts for eax and edx */ 2058 pos = *(off_t *)(td->td_retval); 2059 td->td_retval[RETVAL_LO] = pos & 0xffffffff; /* %eax */ 2060 td->td_retval[RETVAL_HI] = pos >> 32; /* %edx */ 2061 return error; 2062 } 2063 2064 int 2065 freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap) 2066 { 2067 2068 return (kern_truncate(td, uap->path, UIO_USERSPACE, 2069 PAIR32TO64(off_t, uap->length))); 2070 } 2071 2072 int 2073 freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap) 2074 { 2075 2076 return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length))); 2077 } 2078 #endif /* COMPAT_FREEBSD6 */ 2079 2080 struct sf_hdtr32 { 2081 uint32_t headers; 2082 int hdr_cnt; 2083 uint32_t trailers; 2084 int trl_cnt; 2085 }; 2086 2087 static int 2088 freebsd32_do_sendfile(struct thread *td, 2089 struct freebsd32_sendfile_args *uap, int compat) 2090 { 2091 struct sf_hdtr32 hdtr32; 2092 struct sf_hdtr hdtr; 2093 struct uio *hdr_uio, *trl_uio; 2094 struct file *fp; 2095 cap_rights_t rights; 2096 struct iovec32 *iov32; 2097 off_t offset, sbytes; 2098 int error; 2099 2100 offset = PAIR32TO64(off_t, uap->offset); 2101 if (offset < 0) 2102 return (EINVAL); 2103 2104 hdr_uio = trl_uio = NULL; 2105 2106 if (uap->hdtr != NULL) { 2107 error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32)); 2108 if (error) 2109 goto out; 2110 PTRIN_CP(hdtr32, hdtr, headers); 2111 CP(hdtr32, hdtr, hdr_cnt); 2112 PTRIN_CP(hdtr32, hdtr, trailers); 2113 CP(hdtr32, hdtr, trl_cnt); 2114 2115 if (hdtr.headers != NULL) { 2116 iov32 = PTRIN(hdtr32.headers); 2117 error = freebsd32_copyinuio(iov32, 2118 hdtr32.hdr_cnt, &hdr_uio); 2119 if (error) 2120 goto out; 2121 #ifdef COMPAT_FREEBSD4 2122 /* 2123 * In FreeBSD < 5.0 the nbytes to send also included 2124 * the header. If compat is specified subtract the 2125 * header size from nbytes. 2126 */ 2127 if (compat) { 2128 if (uap->nbytes > hdr_uio->uio_resid) 2129 uap->nbytes -= hdr_uio->uio_resid; 2130 else 2131 uap->nbytes = 0; 2132 } 2133 #endif 2134 } 2135 if (hdtr.trailers != NULL) { 2136 iov32 = PTRIN(hdtr32.trailers); 2137 error = freebsd32_copyinuio(iov32, 2138 hdtr32.trl_cnt, &trl_uio); 2139 if (error) 2140 goto out; 2141 } 2142 } 2143 2144 AUDIT_ARG_FD(uap->fd); 2145 2146 if ((error = fget_read(td, uap->fd, 2147 cap_rights_init_one(&rights, CAP_PREAD), &fp)) != 0) 2148 goto out; 2149 2150 error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, offset, 2151 uap->nbytes, &sbytes, uap->flags, td); 2152 fdrop(fp, td); 2153 2154 if (uap->sbytes != NULL) 2155 copyout(&sbytes, uap->sbytes, sizeof(off_t)); 2156 2157 out: 2158 if (hdr_uio) 2159 free(hdr_uio, M_IOV); 2160 if (trl_uio) 2161 free(trl_uio, M_IOV); 2162 return (error); 2163 } 2164 2165 #ifdef COMPAT_FREEBSD4 2166 int 2167 freebsd4_freebsd32_sendfile(struct thread *td, 2168 struct freebsd4_freebsd32_sendfile_args *uap) 2169 { 2170 return (freebsd32_do_sendfile(td, 2171 (struct freebsd32_sendfile_args *)uap, 1)); 2172 } 2173 #endif 2174 2175 int 2176 freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap) 2177 { 2178 2179 return (freebsd32_do_sendfile(td, uap, 0)); 2180 } 2181 2182 static void 2183 copy_stat(struct stat *in, struct stat32 *out) 2184 { 2185 2186 CP(*in, *out, st_dev); 2187 CP(*in, *out, st_ino); 2188 CP(*in, *out, st_mode); 2189 CP(*in, *out, st_nlink); 2190 CP(*in, *out, st_uid); 2191 CP(*in, *out, st_gid); 2192 CP(*in, *out, st_rdev); 2193 TS_CP(*in, *out, st_atim); 2194 TS_CP(*in, *out, st_mtim); 2195 TS_CP(*in, *out, st_ctim); 2196 CP(*in, *out, st_size); 2197 CP(*in, *out, st_blocks); 2198 CP(*in, *out, st_blksize); 2199 CP(*in, *out, st_flags); 2200 CP(*in, *out, st_gen); 2201 TS_CP(*in, *out, st_birthtim); 2202 out->st_padding0 = 0; 2203 out->st_padding1 = 0; 2204 #ifdef __STAT32_TIME_T_EXT 2205 out->st_atim_ext = 0; 2206 out->st_mtim_ext = 0; 2207 out->st_ctim_ext = 0; 2208 out->st_btim_ext = 0; 2209 #endif 2210 bzero(out->st_spare, sizeof(out->st_spare)); 2211 } 2212 2213 #ifdef COMPAT_43 2214 static void 2215 copy_ostat(struct stat *in, struct ostat32 *out) 2216 { 2217 2218 bzero(out, sizeof(*out)); 2219 CP(*in, *out, st_dev); 2220 CP(*in, *out, st_ino); 2221 CP(*in, *out, st_mode); 2222 CP(*in, *out, st_nlink); 2223 CP(*in, *out, st_uid); 2224 CP(*in, *out, st_gid); 2225 CP(*in, *out, st_rdev); 2226 out->st_size = MIN(in->st_size, INT32_MAX); 2227 TS_CP(*in, *out, st_atim); 2228 TS_CP(*in, *out, st_mtim); 2229 TS_CP(*in, *out, st_ctim); 2230 CP(*in, *out, st_blksize); 2231 CP(*in, *out, st_blocks); 2232 CP(*in, *out, st_flags); 2233 CP(*in, *out, st_gen); 2234 } 2235 #endif 2236 2237 #ifdef COMPAT_43 2238 int 2239 ofreebsd32_stat(struct thread *td, struct ofreebsd32_stat_args *uap) 2240 { 2241 struct stat sb; 2242 struct ostat32 sb32; 2243 int error; 2244 2245 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE, 2246 &sb, NULL); 2247 if (error) 2248 return (error); 2249 copy_ostat(&sb, &sb32); 2250 error = copyout(&sb32, uap->ub, sizeof (sb32)); 2251 return (error); 2252 } 2253 #endif 2254 2255 int 2256 freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap) 2257 { 2258 struct stat ub; 2259 struct stat32 ub32; 2260 int error; 2261 2262 error = kern_fstat(td, uap->fd, &ub); 2263 if (error) 2264 return (error); 2265 copy_stat(&ub, &ub32); 2266 error = copyout(&ub32, uap->sb, sizeof(ub32)); 2267 return (error); 2268 } 2269 2270 #ifdef COMPAT_43 2271 int 2272 ofreebsd32_fstat(struct thread *td, struct ofreebsd32_fstat_args *uap) 2273 { 2274 struct stat ub; 2275 struct ostat32 ub32; 2276 int error; 2277 2278 error = kern_fstat(td, uap->fd, &ub); 2279 if (error) 2280 return (error); 2281 copy_ostat(&ub, &ub32); 2282 error = copyout(&ub32, uap->sb, sizeof(ub32)); 2283 return (error); 2284 } 2285 #endif 2286 2287 int 2288 freebsd32_fstatat(struct thread *td, struct freebsd32_fstatat_args *uap) 2289 { 2290 struct stat ub; 2291 struct stat32 ub32; 2292 int error; 2293 2294 error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE, 2295 &ub, NULL); 2296 if (error) 2297 return (error); 2298 copy_stat(&ub, &ub32); 2299 error = copyout(&ub32, uap->buf, sizeof(ub32)); 2300 return (error); 2301 } 2302 2303 #ifdef COMPAT_43 2304 int 2305 ofreebsd32_lstat(struct thread *td, struct ofreebsd32_lstat_args *uap) 2306 { 2307 struct stat sb; 2308 struct ostat32 sb32; 2309 int error; 2310 2311 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path, 2312 UIO_USERSPACE, &sb, NULL); 2313 if (error) 2314 return (error); 2315 copy_ostat(&sb, &sb32); 2316 error = copyout(&sb32, uap->ub, sizeof (sb32)); 2317 return (error); 2318 } 2319 #endif 2320 2321 int 2322 freebsd32_fhstat(struct thread *td, struct freebsd32_fhstat_args *uap) 2323 { 2324 struct stat sb; 2325 struct stat32 sb32; 2326 struct fhandle fh; 2327 int error; 2328 2329 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t)); 2330 if (error != 0) 2331 return (error); 2332 error = kern_fhstat(td, fh, &sb); 2333 if (error != 0) 2334 return (error); 2335 copy_stat(&sb, &sb32); 2336 error = copyout(&sb32, uap->sb, sizeof (sb32)); 2337 return (error); 2338 } 2339 2340 #if defined(COMPAT_FREEBSD11) 2341 extern int ino64_trunc_error; 2342 2343 static int 2344 freebsd11_cvtstat32(struct stat *in, struct freebsd11_stat32 *out) 2345 { 2346 2347 CP(*in, *out, st_ino); 2348 if (in->st_ino != out->st_ino) { 2349 switch (ino64_trunc_error) { 2350 default: 2351 case 0: 2352 break; 2353 case 1: 2354 return (EOVERFLOW); 2355 case 2: 2356 out->st_ino = UINT32_MAX; 2357 break; 2358 } 2359 } 2360 CP(*in, *out, st_nlink); 2361 if (in->st_nlink != out->st_nlink) { 2362 switch (ino64_trunc_error) { 2363 default: 2364 case 0: 2365 break; 2366 case 1: 2367 return (EOVERFLOW); 2368 case 2: 2369 out->st_nlink = UINT16_MAX; 2370 break; 2371 } 2372 } 2373 out->st_dev = in->st_dev; 2374 if (out->st_dev != in->st_dev) { 2375 switch (ino64_trunc_error) { 2376 default: 2377 break; 2378 case 1: 2379 return (EOVERFLOW); 2380 } 2381 } 2382 CP(*in, *out, st_mode); 2383 CP(*in, *out, st_uid); 2384 CP(*in, *out, st_gid); 2385 out->st_rdev = in->st_rdev; 2386 if (out->st_rdev != in->st_rdev) { 2387 switch (ino64_trunc_error) { 2388 default: 2389 break; 2390 case 1: 2391 return (EOVERFLOW); 2392 } 2393 } 2394 TS_CP(*in, *out, st_atim); 2395 TS_CP(*in, *out, st_mtim); 2396 TS_CP(*in, *out, st_ctim); 2397 CP(*in, *out, st_size); 2398 CP(*in, *out, st_blocks); 2399 CP(*in, *out, st_blksize); 2400 CP(*in, *out, st_flags); 2401 CP(*in, *out, st_gen); 2402 TS_CP(*in, *out, st_birthtim); 2403 out->st_lspare = 0; 2404 bzero((char *)&out->st_birthtim + sizeof(out->st_birthtim), 2405 sizeof(*out) - offsetof(struct freebsd11_stat32, 2406 st_birthtim) - sizeof(out->st_birthtim)); 2407 return (0); 2408 } 2409 2410 int 2411 freebsd11_freebsd32_stat(struct thread *td, 2412 struct freebsd11_freebsd32_stat_args *uap) 2413 { 2414 struct stat sb; 2415 struct freebsd11_stat32 sb32; 2416 int error; 2417 2418 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE, 2419 &sb, NULL); 2420 if (error != 0) 2421 return (error); 2422 error = freebsd11_cvtstat32(&sb, &sb32); 2423 if (error == 0) 2424 error = copyout(&sb32, uap->ub, sizeof (sb32)); 2425 return (error); 2426 } 2427 2428 int 2429 freebsd11_freebsd32_fstat(struct thread *td, 2430 struct freebsd11_freebsd32_fstat_args *uap) 2431 { 2432 struct stat sb; 2433 struct freebsd11_stat32 sb32; 2434 int error; 2435 2436 error = kern_fstat(td, uap->fd, &sb); 2437 if (error != 0) 2438 return (error); 2439 error = freebsd11_cvtstat32(&sb, &sb32); 2440 if (error == 0) 2441 error = copyout(&sb32, uap->sb, sizeof (sb32)); 2442 return (error); 2443 } 2444 2445 int 2446 freebsd11_freebsd32_fstatat(struct thread *td, 2447 struct freebsd11_freebsd32_fstatat_args *uap) 2448 { 2449 struct stat sb; 2450 struct freebsd11_stat32 sb32; 2451 int error; 2452 2453 error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE, 2454 &sb, NULL); 2455 if (error != 0) 2456 return (error); 2457 error = freebsd11_cvtstat32(&sb, &sb32); 2458 if (error == 0) 2459 error = copyout(&sb32, uap->buf, sizeof (sb32)); 2460 return (error); 2461 } 2462 2463 int 2464 freebsd11_freebsd32_lstat(struct thread *td, 2465 struct freebsd11_freebsd32_lstat_args *uap) 2466 { 2467 struct stat sb; 2468 struct freebsd11_stat32 sb32; 2469 int error; 2470 2471 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path, 2472 UIO_USERSPACE, &sb, NULL); 2473 if (error != 0) 2474 return (error); 2475 error = freebsd11_cvtstat32(&sb, &sb32); 2476 if (error == 0) 2477 error = copyout(&sb32, uap->ub, sizeof (sb32)); 2478 return (error); 2479 } 2480 2481 int 2482 freebsd11_freebsd32_fhstat(struct thread *td, 2483 struct freebsd11_freebsd32_fhstat_args *uap) 2484 { 2485 struct stat sb; 2486 struct freebsd11_stat32 sb32; 2487 struct fhandle fh; 2488 int error; 2489 2490 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t)); 2491 if (error != 0) 2492 return (error); 2493 error = kern_fhstat(td, fh, &sb); 2494 if (error != 0) 2495 return (error); 2496 error = freebsd11_cvtstat32(&sb, &sb32); 2497 if (error == 0) 2498 error = copyout(&sb32, uap->sb, sizeof (sb32)); 2499 return (error); 2500 } 2501 2502 static int 2503 freebsd11_cvtnstat32(struct stat *sb, struct nstat32 *nsb32) 2504 { 2505 struct nstat nsb; 2506 int error; 2507 2508 error = freebsd11_cvtnstat(sb, &nsb); 2509 if (error != 0) 2510 return (error); 2511 2512 bzero(nsb32, sizeof(*nsb32)); 2513 CP(nsb, *nsb32, st_dev); 2514 CP(nsb, *nsb32, st_ino); 2515 CP(nsb, *nsb32, st_mode); 2516 CP(nsb, *nsb32, st_nlink); 2517 CP(nsb, *nsb32, st_uid); 2518 CP(nsb, *nsb32, st_gid); 2519 CP(nsb, *nsb32, st_rdev); 2520 CP(nsb, *nsb32, st_atim.tv_sec); 2521 CP(nsb, *nsb32, st_atim.tv_nsec); 2522 CP(nsb, *nsb32, st_mtim.tv_sec); 2523 CP(nsb, *nsb32, st_mtim.tv_nsec); 2524 CP(nsb, *nsb32, st_ctim.tv_sec); 2525 CP(nsb, *nsb32, st_ctim.tv_nsec); 2526 CP(nsb, *nsb32, st_size); 2527 CP(nsb, *nsb32, st_blocks); 2528 CP(nsb, *nsb32, st_blksize); 2529 CP(nsb, *nsb32, st_flags); 2530 CP(nsb, *nsb32, st_gen); 2531 CP(nsb, *nsb32, st_birthtim.tv_sec); 2532 CP(nsb, *nsb32, st_birthtim.tv_nsec); 2533 return (0); 2534 } 2535 2536 int 2537 freebsd11_freebsd32_nstat(struct thread *td, 2538 struct freebsd11_freebsd32_nstat_args *uap) 2539 { 2540 struct stat sb; 2541 struct nstat32 nsb; 2542 int error; 2543 2544 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE, 2545 &sb, NULL); 2546 if (error != 0) 2547 return (error); 2548 error = freebsd11_cvtnstat32(&sb, &nsb); 2549 if (error != 0) 2550 error = copyout(&nsb, uap->ub, sizeof (nsb)); 2551 return (error); 2552 } 2553 2554 int 2555 freebsd11_freebsd32_nlstat(struct thread *td, 2556 struct freebsd11_freebsd32_nlstat_args *uap) 2557 { 2558 struct stat sb; 2559 struct nstat32 nsb; 2560 int error; 2561 2562 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path, 2563 UIO_USERSPACE, &sb, NULL); 2564 if (error != 0) 2565 return (error); 2566 error = freebsd11_cvtnstat32(&sb, &nsb); 2567 if (error == 0) 2568 error = copyout(&nsb, uap->ub, sizeof (nsb)); 2569 return (error); 2570 } 2571 2572 int 2573 freebsd11_freebsd32_nfstat(struct thread *td, 2574 struct freebsd11_freebsd32_nfstat_args *uap) 2575 { 2576 struct nstat32 nub; 2577 struct stat ub; 2578 int error; 2579 2580 error = kern_fstat(td, uap->fd, &ub); 2581 if (error != 0) 2582 return (error); 2583 error = freebsd11_cvtnstat32(&ub, &nub); 2584 if (error == 0) 2585 error = copyout(&nub, uap->sb, sizeof(nub)); 2586 return (error); 2587 } 2588 #endif 2589 2590 int 2591 freebsd32___sysctl(struct thread *td, struct freebsd32___sysctl_args *uap) 2592 { 2593 int error, name[CTL_MAXNAME]; 2594 size_t j, oldlen; 2595 uint32_t tmp; 2596 2597 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2) 2598 return (EINVAL); 2599 error = copyin(uap->name, name, uap->namelen * sizeof(int)); 2600 if (error) 2601 return (error); 2602 if (uap->oldlenp) { 2603 error = fueword32(uap->oldlenp, &tmp); 2604 oldlen = tmp; 2605 } else { 2606 oldlen = 0; 2607 } 2608 if (error != 0) 2609 return (EFAULT); 2610 error = userland_sysctl(td, name, uap->namelen, 2611 uap->old, &oldlen, 1, 2612 uap->new, uap->newlen, &j, SCTL_MASK32); 2613 if (error) 2614 return (error); 2615 if (uap->oldlenp) 2616 suword32(uap->oldlenp, j); 2617 return (0); 2618 } 2619 2620 int 2621 freebsd32___sysctlbyname(struct thread *td, 2622 struct freebsd32___sysctlbyname_args *uap) 2623 { 2624 size_t oldlen, rv; 2625 int error; 2626 uint32_t tmp; 2627 2628 if (uap->oldlenp != NULL) { 2629 error = fueword32(uap->oldlenp, &tmp); 2630 oldlen = tmp; 2631 } else { 2632 error = oldlen = 0; 2633 } 2634 if (error != 0) 2635 return (EFAULT); 2636 error = kern___sysctlbyname(td, uap->name, uap->namelen, uap->old, 2637 &oldlen, uap->new, uap->newlen, &rv, SCTL_MASK32, 1); 2638 if (error != 0) 2639 return (error); 2640 if (uap->oldlenp != NULL) 2641 error = suword32(uap->oldlenp, rv); 2642 2643 return (error); 2644 } 2645 2646 int 2647 freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap) 2648 { 2649 uint32_t version; 2650 int error; 2651 struct jail j; 2652 2653 error = copyin(uap->jail, &version, sizeof(uint32_t)); 2654 if (error) 2655 return (error); 2656 2657 switch (version) { 2658 case 0: 2659 { 2660 /* FreeBSD single IPv4 jails. */ 2661 struct jail32_v0 j32_v0; 2662 2663 bzero(&j, sizeof(struct jail)); 2664 error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0)); 2665 if (error) 2666 return (error); 2667 CP(j32_v0, j, version); 2668 PTRIN_CP(j32_v0, j, path); 2669 PTRIN_CP(j32_v0, j, hostname); 2670 j.ip4s = htonl(j32_v0.ip_number); /* jail_v0 is host order */ 2671 break; 2672 } 2673 2674 case 1: 2675 /* 2676 * Version 1 was used by multi-IPv4 jail implementations 2677 * that never made it into the official kernel. 2678 */ 2679 return (EINVAL); 2680 2681 case 2: /* JAIL_API_VERSION */ 2682 { 2683 /* FreeBSD multi-IPv4/IPv6,noIP jails. */ 2684 struct jail32 j32; 2685 2686 error = copyin(uap->jail, &j32, sizeof(struct jail32)); 2687 if (error) 2688 return (error); 2689 CP(j32, j, version); 2690 PTRIN_CP(j32, j, path); 2691 PTRIN_CP(j32, j, hostname); 2692 PTRIN_CP(j32, j, jailname); 2693 CP(j32, j, ip4s); 2694 CP(j32, j, ip6s); 2695 PTRIN_CP(j32, j, ip4); 2696 PTRIN_CP(j32, j, ip6); 2697 break; 2698 } 2699 2700 default: 2701 /* Sci-Fi jails are not supported, sorry. */ 2702 return (EINVAL); 2703 } 2704 return (kern_jail(td, &j)); 2705 } 2706 2707 int 2708 freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap) 2709 { 2710 struct uio *auio; 2711 int error; 2712 2713 /* Check that we have an even number of iovecs. */ 2714 if (uap->iovcnt & 1) 2715 return (EINVAL); 2716 2717 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio); 2718 if (error) 2719 return (error); 2720 error = kern_jail_set(td, auio, uap->flags); 2721 free(auio, M_IOV); 2722 return (error); 2723 } 2724 2725 int 2726 freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap) 2727 { 2728 struct iovec32 iov32; 2729 struct uio *auio; 2730 int error, i; 2731 2732 /* Check that we have an even number of iovecs. */ 2733 if (uap->iovcnt & 1) 2734 return (EINVAL); 2735 2736 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio); 2737 if (error) 2738 return (error); 2739 error = kern_jail_get(td, auio, uap->flags); 2740 if (error == 0) 2741 for (i = 0; i < uap->iovcnt; i++) { 2742 PTROUT_CP(auio->uio_iov[i], iov32, iov_base); 2743 CP(auio->uio_iov[i], iov32, iov_len); 2744 error = copyout(&iov32, uap->iovp + i, sizeof(iov32)); 2745 if (error != 0) 2746 break; 2747 } 2748 free(auio, M_IOV); 2749 return (error); 2750 } 2751 2752 int 2753 freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap) 2754 { 2755 struct sigaction32 s32; 2756 struct sigaction sa, osa, *sap; 2757 int error; 2758 2759 if (uap->act) { 2760 error = copyin(uap->act, &s32, sizeof(s32)); 2761 if (error) 2762 return (error); 2763 sa.sa_handler = PTRIN(s32.sa_u); 2764 CP(s32, sa, sa_flags); 2765 CP(s32, sa, sa_mask); 2766 sap = &sa; 2767 } else 2768 sap = NULL; 2769 error = kern_sigaction(td, uap->sig, sap, &osa, 0); 2770 if (error == 0 && uap->oact != NULL) { 2771 s32.sa_u = PTROUT(osa.sa_handler); 2772 CP(osa, s32, sa_flags); 2773 CP(osa, s32, sa_mask); 2774 error = copyout(&s32, uap->oact, sizeof(s32)); 2775 } 2776 return (error); 2777 } 2778 2779 #ifdef COMPAT_FREEBSD4 2780 int 2781 freebsd4_freebsd32_sigaction(struct thread *td, 2782 struct freebsd4_freebsd32_sigaction_args *uap) 2783 { 2784 struct sigaction32 s32; 2785 struct sigaction sa, osa, *sap; 2786 int error; 2787 2788 if (uap->act) { 2789 error = copyin(uap->act, &s32, sizeof(s32)); 2790 if (error) 2791 return (error); 2792 sa.sa_handler = PTRIN(s32.sa_u); 2793 CP(s32, sa, sa_flags); 2794 CP(s32, sa, sa_mask); 2795 sap = &sa; 2796 } else 2797 sap = NULL; 2798 error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4); 2799 if (error == 0 && uap->oact != NULL) { 2800 s32.sa_u = PTROUT(osa.sa_handler); 2801 CP(osa, s32, sa_flags); 2802 CP(osa, s32, sa_mask); 2803 error = copyout(&s32, uap->oact, sizeof(s32)); 2804 } 2805 return (error); 2806 } 2807 #endif 2808 2809 #ifdef COMPAT_43 2810 struct osigaction32 { 2811 uint32_t sa_u; 2812 osigset_t sa_mask; 2813 int sa_flags; 2814 }; 2815 2816 #define ONSIG 32 2817 2818 int 2819 ofreebsd32_sigaction(struct thread *td, 2820 struct ofreebsd32_sigaction_args *uap) 2821 { 2822 struct osigaction32 s32; 2823 struct sigaction sa, osa, *sap; 2824 int error; 2825 2826 if (uap->signum <= 0 || uap->signum >= ONSIG) 2827 return (EINVAL); 2828 2829 if (uap->nsa) { 2830 error = copyin(uap->nsa, &s32, sizeof(s32)); 2831 if (error) 2832 return (error); 2833 sa.sa_handler = PTRIN(s32.sa_u); 2834 CP(s32, sa, sa_flags); 2835 OSIG2SIG(s32.sa_mask, sa.sa_mask); 2836 sap = &sa; 2837 } else 2838 sap = NULL; 2839 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET); 2840 if (error == 0 && uap->osa != NULL) { 2841 s32.sa_u = PTROUT(osa.sa_handler); 2842 CP(osa, s32, sa_flags); 2843 SIG2OSIG(osa.sa_mask, s32.sa_mask); 2844 error = copyout(&s32, uap->osa, sizeof(s32)); 2845 } 2846 return (error); 2847 } 2848 2849 struct sigvec32 { 2850 uint32_t sv_handler; 2851 int sv_mask; 2852 int sv_flags; 2853 }; 2854 2855 int 2856 ofreebsd32_sigvec(struct thread *td, 2857 struct ofreebsd32_sigvec_args *uap) 2858 { 2859 struct sigvec32 vec; 2860 struct sigaction sa, osa, *sap; 2861 int error; 2862 2863 if (uap->signum <= 0 || uap->signum >= ONSIG) 2864 return (EINVAL); 2865 2866 if (uap->nsv) { 2867 error = copyin(uap->nsv, &vec, sizeof(vec)); 2868 if (error) 2869 return (error); 2870 sa.sa_handler = PTRIN(vec.sv_handler); 2871 OSIG2SIG(vec.sv_mask, sa.sa_mask); 2872 sa.sa_flags = vec.sv_flags; 2873 sa.sa_flags ^= SA_RESTART; 2874 sap = &sa; 2875 } else 2876 sap = NULL; 2877 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET); 2878 if (error == 0 && uap->osv != NULL) { 2879 vec.sv_handler = PTROUT(osa.sa_handler); 2880 SIG2OSIG(osa.sa_mask, vec.sv_mask); 2881 vec.sv_flags = osa.sa_flags; 2882 vec.sv_flags &= ~SA_NOCLDWAIT; 2883 vec.sv_flags ^= SA_RESTART; 2884 error = copyout(&vec, uap->osv, sizeof(vec)); 2885 } 2886 return (error); 2887 } 2888 2889 struct sigstack32 { 2890 uint32_t ss_sp; 2891 int ss_onstack; 2892 }; 2893 2894 int 2895 ofreebsd32_sigstack(struct thread *td, 2896 struct ofreebsd32_sigstack_args *uap) 2897 { 2898 struct sigstack32 s32; 2899 struct sigstack nss, oss; 2900 int error = 0, unss; 2901 2902 if (uap->nss != NULL) { 2903 error = copyin(uap->nss, &s32, sizeof(s32)); 2904 if (error) 2905 return (error); 2906 nss.ss_sp = PTRIN(s32.ss_sp); 2907 CP(s32, nss, ss_onstack); 2908 unss = 1; 2909 } else { 2910 unss = 0; 2911 } 2912 oss.ss_sp = td->td_sigstk.ss_sp; 2913 oss.ss_onstack = sigonstack(cpu_getstack(td)); 2914 if (unss) { 2915 td->td_sigstk.ss_sp = nss.ss_sp; 2916 td->td_sigstk.ss_size = 0; 2917 td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK); 2918 td->td_pflags |= TDP_ALTSTACK; 2919 } 2920 if (uap->oss != NULL) { 2921 s32.ss_sp = PTROUT(oss.ss_sp); 2922 CP(oss, s32, ss_onstack); 2923 error = copyout(&s32, uap->oss, sizeof(s32)); 2924 } 2925 return (error); 2926 } 2927 #endif 2928 2929 int 2930 freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap) 2931 { 2932 2933 return (freebsd32_user_clock_nanosleep(td, CLOCK_REALTIME, 2934 TIMER_RELTIME, uap->rqtp, uap->rmtp)); 2935 } 2936 2937 int 2938 freebsd32_clock_nanosleep(struct thread *td, 2939 struct freebsd32_clock_nanosleep_args *uap) 2940 { 2941 int error; 2942 2943 error = freebsd32_user_clock_nanosleep(td, uap->clock_id, uap->flags, 2944 uap->rqtp, uap->rmtp); 2945 return (kern_posix_error(td, error)); 2946 } 2947 2948 static int 2949 freebsd32_user_clock_nanosleep(struct thread *td, clockid_t clock_id, 2950 int flags, const struct timespec32 *ua_rqtp, struct timespec32 *ua_rmtp) 2951 { 2952 struct timespec32 rmt32, rqt32; 2953 struct timespec rmt, rqt; 2954 int error, error2; 2955 2956 error = copyin(ua_rqtp, &rqt32, sizeof(rqt32)); 2957 if (error) 2958 return (error); 2959 2960 CP(rqt32, rqt, tv_sec); 2961 CP(rqt32, rqt, tv_nsec); 2962 2963 error = kern_clock_nanosleep(td, clock_id, flags, &rqt, &rmt); 2964 if (error == EINTR && ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0) { 2965 CP(rmt, rmt32, tv_sec); 2966 CP(rmt, rmt32, tv_nsec); 2967 2968 error2 = copyout(&rmt32, ua_rmtp, sizeof(rmt32)); 2969 if (error2 != 0) 2970 error = error2; 2971 } 2972 return (error); 2973 } 2974 2975 int 2976 freebsd32_clock_gettime(struct thread *td, 2977 struct freebsd32_clock_gettime_args *uap) 2978 { 2979 struct timespec ats; 2980 struct timespec32 ats32; 2981 int error; 2982 2983 error = kern_clock_gettime(td, uap->clock_id, &ats); 2984 if (error == 0) { 2985 CP(ats, ats32, tv_sec); 2986 CP(ats, ats32, tv_nsec); 2987 error = copyout(&ats32, uap->tp, sizeof(ats32)); 2988 } 2989 return (error); 2990 } 2991 2992 int 2993 freebsd32_clock_settime(struct thread *td, 2994 struct freebsd32_clock_settime_args *uap) 2995 { 2996 struct timespec ats; 2997 struct timespec32 ats32; 2998 int error; 2999 3000 error = copyin(uap->tp, &ats32, sizeof(ats32)); 3001 if (error) 3002 return (error); 3003 CP(ats32, ats, tv_sec); 3004 CP(ats32, ats, tv_nsec); 3005 3006 return (kern_clock_settime(td, uap->clock_id, &ats)); 3007 } 3008 3009 int 3010 freebsd32_clock_getres(struct thread *td, 3011 struct freebsd32_clock_getres_args *uap) 3012 { 3013 struct timespec ts; 3014 struct timespec32 ts32; 3015 int error; 3016 3017 if (uap->tp == NULL) 3018 return (0); 3019 error = kern_clock_getres(td, uap->clock_id, &ts); 3020 if (error == 0) { 3021 CP(ts, ts32, tv_sec); 3022 CP(ts, ts32, tv_nsec); 3023 error = copyout(&ts32, uap->tp, sizeof(ts32)); 3024 } 3025 return (error); 3026 } 3027 3028 int freebsd32_ktimer_create(struct thread *td, 3029 struct freebsd32_ktimer_create_args *uap) 3030 { 3031 struct sigevent32 ev32; 3032 struct sigevent ev, *evp; 3033 int error, id; 3034 3035 if (uap->evp == NULL) { 3036 evp = NULL; 3037 } else { 3038 evp = &ev; 3039 error = copyin(uap->evp, &ev32, sizeof(ev32)); 3040 if (error != 0) 3041 return (error); 3042 error = convert_sigevent32(&ev32, &ev); 3043 if (error != 0) 3044 return (error); 3045 } 3046 error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1); 3047 if (error == 0) { 3048 error = copyout(&id, uap->timerid, sizeof(int)); 3049 if (error != 0) 3050 kern_ktimer_delete(td, id); 3051 } 3052 return (error); 3053 } 3054 3055 int 3056 freebsd32_ktimer_settime(struct thread *td, 3057 struct freebsd32_ktimer_settime_args *uap) 3058 { 3059 struct itimerspec32 val32, oval32; 3060 struct itimerspec val, oval, *ovalp; 3061 int error; 3062 3063 error = copyin(uap->value, &val32, sizeof(val32)); 3064 if (error != 0) 3065 return (error); 3066 ITS_CP(val32, val); 3067 ovalp = uap->ovalue != NULL ? &oval : NULL; 3068 error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp); 3069 if (error == 0 && uap->ovalue != NULL) { 3070 ITS_CP(oval, oval32); 3071 error = copyout(&oval32, uap->ovalue, sizeof(oval32)); 3072 } 3073 return (error); 3074 } 3075 3076 int 3077 freebsd32_ktimer_gettime(struct thread *td, 3078 struct freebsd32_ktimer_gettime_args *uap) 3079 { 3080 struct itimerspec32 val32; 3081 struct itimerspec val; 3082 int error; 3083 3084 error = kern_ktimer_gettime(td, uap->timerid, &val); 3085 if (error == 0) { 3086 ITS_CP(val, val32); 3087 error = copyout(&val32, uap->value, sizeof(val32)); 3088 } 3089 return (error); 3090 } 3091 3092 int 3093 freebsd32_clock_getcpuclockid2(struct thread *td, 3094 struct freebsd32_clock_getcpuclockid2_args *uap) 3095 { 3096 clockid_t clk_id; 3097 int error; 3098 3099 error = kern_clock_getcpuclockid2(td, PAIR32TO64(id_t, uap->id), 3100 uap->which, &clk_id); 3101 if (error == 0) 3102 error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t)); 3103 return (error); 3104 } 3105 3106 int 3107 freebsd32_thr_new(struct thread *td, 3108 struct freebsd32_thr_new_args *uap) 3109 { 3110 struct thr_param32 param32; 3111 struct thr_param param; 3112 int error; 3113 3114 if (uap->param_size < 0 || 3115 uap->param_size > sizeof(struct thr_param32)) 3116 return (EINVAL); 3117 bzero(¶m, sizeof(struct thr_param)); 3118 bzero(¶m32, sizeof(struct thr_param32)); 3119 error = copyin(uap->param, ¶m32, uap->param_size); 3120 if (error != 0) 3121 return (error); 3122 param.start_func = PTRIN(param32.start_func); 3123 param.arg = PTRIN(param32.arg); 3124 param.stack_base = PTRIN(param32.stack_base); 3125 param.stack_size = param32.stack_size; 3126 param.tls_base = PTRIN(param32.tls_base); 3127 param.tls_size = param32.tls_size; 3128 param.child_tid = PTRIN(param32.child_tid); 3129 param.parent_tid = PTRIN(param32.parent_tid); 3130 param.flags = param32.flags; 3131 param.rtp = PTRIN(param32.rtp); 3132 param.spare[0] = PTRIN(param32.spare[0]); 3133 param.spare[1] = PTRIN(param32.spare[1]); 3134 param.spare[2] = PTRIN(param32.spare[2]); 3135 3136 return (kern_thr_new(td, ¶m)); 3137 } 3138 3139 int 3140 freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap) 3141 { 3142 struct timespec32 ts32; 3143 struct timespec ts, *tsp; 3144 int error; 3145 3146 error = 0; 3147 tsp = NULL; 3148 if (uap->timeout != NULL) { 3149 error = copyin((const void *)uap->timeout, (void *)&ts32, 3150 sizeof(struct timespec32)); 3151 if (error != 0) 3152 return (error); 3153 ts.tv_sec = ts32.tv_sec; 3154 ts.tv_nsec = ts32.tv_nsec; 3155 tsp = &ts; 3156 } 3157 return (kern_thr_suspend(td, tsp)); 3158 } 3159 3160 void 3161 siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst) 3162 { 3163 bzero(dst, sizeof(*dst)); 3164 dst->si_signo = src->si_signo; 3165 dst->si_errno = src->si_errno; 3166 dst->si_code = src->si_code; 3167 dst->si_pid = src->si_pid; 3168 dst->si_uid = src->si_uid; 3169 dst->si_status = src->si_status; 3170 dst->si_addr = (uintptr_t)src->si_addr; 3171 dst->si_value.sival_int = src->si_value.sival_int; 3172 dst->si_timerid = src->si_timerid; 3173 dst->si_overrun = src->si_overrun; 3174 } 3175 3176 #ifndef _FREEBSD32_SYSPROTO_H_ 3177 struct freebsd32_sigqueue_args { 3178 pid_t pid; 3179 int signum; 3180 /* union sigval32 */ int value; 3181 }; 3182 #endif 3183 int 3184 freebsd32_sigqueue(struct thread *td, struct freebsd32_sigqueue_args *uap) 3185 { 3186 union sigval sv; 3187 3188 /* 3189 * On 32-bit ABIs, sival_int and sival_ptr are the same. 3190 * On 64-bit little-endian ABIs, the low bits are the same. 3191 * In 64-bit big-endian ABIs, sival_int overlaps with 3192 * sival_ptr's HIGH bits. We choose to support sival_int 3193 * rather than sival_ptr in this case as it seems to be 3194 * more common. 3195 */ 3196 bzero(&sv, sizeof(sv)); 3197 sv.sival_int = (uint32_t)(uint64_t)uap->value; 3198 3199 return (kern_sigqueue(td, uap->pid, uap->signum, &sv)); 3200 } 3201 3202 int 3203 freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap) 3204 { 3205 struct timespec32 ts32; 3206 struct timespec ts; 3207 struct timespec *timeout; 3208 sigset_t set; 3209 ksiginfo_t ksi; 3210 struct siginfo32 si32; 3211 int error; 3212 3213 if (uap->timeout) { 3214 error = copyin(uap->timeout, &ts32, sizeof(ts32)); 3215 if (error) 3216 return (error); 3217 ts.tv_sec = ts32.tv_sec; 3218 ts.tv_nsec = ts32.tv_nsec; 3219 timeout = &ts; 3220 } else 3221 timeout = NULL; 3222 3223 error = copyin(uap->set, &set, sizeof(set)); 3224 if (error) 3225 return (error); 3226 3227 error = kern_sigtimedwait(td, set, &ksi, timeout); 3228 if (error) 3229 return (error); 3230 3231 if (uap->info) { 3232 siginfo_to_siginfo32(&ksi.ksi_info, &si32); 3233 error = copyout(&si32, uap->info, sizeof(struct siginfo32)); 3234 } 3235 3236 if (error == 0) 3237 td->td_retval[0] = ksi.ksi_signo; 3238 return (error); 3239 } 3240 3241 /* 3242 * MPSAFE 3243 */ 3244 int 3245 freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap) 3246 { 3247 ksiginfo_t ksi; 3248 struct siginfo32 si32; 3249 sigset_t set; 3250 int error; 3251 3252 error = copyin(uap->set, &set, sizeof(set)); 3253 if (error) 3254 return (error); 3255 3256 error = kern_sigtimedwait(td, set, &ksi, NULL); 3257 if (error) 3258 return (error); 3259 3260 if (uap->info) { 3261 siginfo_to_siginfo32(&ksi.ksi_info, &si32); 3262 error = copyout(&si32, uap->info, sizeof(struct siginfo32)); 3263 } 3264 if (error == 0) 3265 td->td_retval[0] = ksi.ksi_signo; 3266 return (error); 3267 } 3268 3269 int 3270 freebsd32_cpuset_setid(struct thread *td, 3271 struct freebsd32_cpuset_setid_args *uap) 3272 { 3273 3274 return (kern_cpuset_setid(td, uap->which, 3275 PAIR32TO64(id_t, uap->id), uap->setid)); 3276 } 3277 3278 int 3279 freebsd32_cpuset_getid(struct thread *td, 3280 struct freebsd32_cpuset_getid_args *uap) 3281 { 3282 3283 return (kern_cpuset_getid(td, uap->level, uap->which, 3284 PAIR32TO64(id_t, uap->id), uap->setid)); 3285 } 3286 3287 int 3288 freebsd32_cpuset_getaffinity(struct thread *td, 3289 struct freebsd32_cpuset_getaffinity_args *uap) 3290 { 3291 3292 return (kern_cpuset_getaffinity(td, uap->level, uap->which, 3293 PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask)); 3294 } 3295 3296 int 3297 freebsd32_cpuset_setaffinity(struct thread *td, 3298 struct freebsd32_cpuset_setaffinity_args *uap) 3299 { 3300 3301 return (kern_cpuset_setaffinity(td, uap->level, uap->which, 3302 PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask)); 3303 } 3304 3305 int 3306 freebsd32_cpuset_getdomain(struct thread *td, 3307 struct freebsd32_cpuset_getdomain_args *uap) 3308 { 3309 3310 return (kern_cpuset_getdomain(td, uap->level, uap->which, 3311 PAIR32TO64(id_t,uap->id), uap->domainsetsize, uap->mask, uap->policy)); 3312 } 3313 3314 int 3315 freebsd32_cpuset_setdomain(struct thread *td, 3316 struct freebsd32_cpuset_setdomain_args *uap) 3317 { 3318 3319 return (kern_cpuset_setdomain(td, uap->level, uap->which, 3320 PAIR32TO64(id_t,uap->id), uap->domainsetsize, uap->mask, uap->policy)); 3321 } 3322 3323 int 3324 freebsd32_nmount(struct thread *td, 3325 struct freebsd32_nmount_args /* { 3326 struct iovec *iovp; 3327 unsigned int iovcnt; 3328 int flags; 3329 } */ *uap) 3330 { 3331 struct uio *auio; 3332 uint64_t flags; 3333 int error; 3334 3335 /* 3336 * Mount flags are now 64-bits. On 32-bit archtectures only 3337 * 32-bits are passed in, but from here on everything handles 3338 * 64-bit flags correctly. 3339 */ 3340 flags = uap->flags; 3341 3342 AUDIT_ARG_FFLAGS(flags); 3343 3344 /* 3345 * Filter out MNT_ROOTFS. We do not want clients of nmount() in 3346 * userspace to set this flag, but we must filter it out if we want 3347 * MNT_UPDATE on the root file system to work. 3348 * MNT_ROOTFS should only be set by the kernel when mounting its 3349 * root file system. 3350 */ 3351 flags &= ~MNT_ROOTFS; 3352 3353 /* 3354 * check that we have an even number of iovec's 3355 * and that we have at least two options. 3356 */ 3357 if ((uap->iovcnt & 1) || (uap->iovcnt < 4)) 3358 return (EINVAL); 3359 3360 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio); 3361 if (error) 3362 return (error); 3363 error = vfs_donmount(td, flags, auio); 3364 3365 free(auio, M_IOV); 3366 return error; 3367 } 3368 3369 #if 0 3370 int 3371 freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap) 3372 { 3373 struct yyy32 *p32, s32; 3374 struct yyy *p = NULL, s; 3375 struct xxx_arg ap; 3376 int error; 3377 3378 if (uap->zzz) { 3379 error = copyin(uap->zzz, &s32, sizeof(s32)); 3380 if (error) 3381 return (error); 3382 /* translate in */ 3383 p = &s; 3384 } 3385 error = kern_xxx(td, p); 3386 if (error) 3387 return (error); 3388 if (uap->zzz) { 3389 /* translate out */ 3390 error = copyout(&s32, p32, sizeof(s32)); 3391 } 3392 return (error); 3393 } 3394 #endif 3395 3396 int 3397 syscall32_module_handler(struct module *mod, int what, void *arg) 3398 { 3399 3400 return (kern_syscall_module_handler(freebsd32_sysent, mod, what, arg)); 3401 } 3402 3403 int 3404 syscall32_helper_register(struct syscall_helper_data *sd, int flags) 3405 { 3406 3407 return (kern_syscall_helper_register(freebsd32_sysent, sd, flags)); 3408 } 3409 3410 int 3411 syscall32_helper_unregister(struct syscall_helper_data *sd) 3412 { 3413 3414 return (kern_syscall_helper_unregister(freebsd32_sysent, sd)); 3415 } 3416 3417 int 3418 freebsd32_copyout_strings(struct image_params *imgp, uintptr_t *stack_base) 3419 { 3420 struct sysentvec *sysent; 3421 int argc, envc, i; 3422 uint32_t *vectp; 3423 char *stringp; 3424 uintptr_t destp, ustringp; 3425 struct freebsd32_ps_strings *arginfo; 3426 char canary[sizeof(long) * 8]; 3427 int32_t pagesizes32[MAXPAGESIZES]; 3428 size_t execpath_len; 3429 int error, szsigcode; 3430 3431 sysent = imgp->sysent; 3432 3433 arginfo = (struct freebsd32_ps_strings *)PROC_PS_STRINGS(imgp->proc); 3434 imgp->ps_strings = arginfo; 3435 destp = (uintptr_t)arginfo; 3436 3437 /* 3438 * Install sigcode. 3439 */ 3440 if (sysent->sv_sigcode_base == 0) { 3441 szsigcode = *sysent->sv_szsigcode; 3442 destp -= szsigcode; 3443 destp = rounddown2(destp, sizeof(uint32_t)); 3444 error = copyout(sysent->sv_sigcode, (void *)destp, 3445 szsigcode); 3446 if (error != 0) 3447 return (error); 3448 } 3449 3450 /* 3451 * Copy the image path for the rtld. 3452 */ 3453 if (imgp->execpath != NULL && imgp->auxargs != NULL) { 3454 execpath_len = strlen(imgp->execpath) + 1; 3455 destp -= execpath_len; 3456 imgp->execpathp = (void *)destp; 3457 error = copyout(imgp->execpath, imgp->execpathp, execpath_len); 3458 if (error != 0) 3459 return (error); 3460 } 3461 3462 /* 3463 * Prepare the canary for SSP. 3464 */ 3465 arc4rand(canary, sizeof(canary), 0); 3466 destp -= sizeof(canary); 3467 imgp->canary = (void *)destp; 3468 error = copyout(canary, imgp->canary, sizeof(canary)); 3469 if (error != 0) 3470 return (error); 3471 imgp->canarylen = sizeof(canary); 3472 3473 /* 3474 * Prepare the pagesizes array. 3475 */ 3476 for (i = 0; i < MAXPAGESIZES; i++) 3477 pagesizes32[i] = (uint32_t)pagesizes[i]; 3478 destp -= sizeof(pagesizes32); 3479 destp = rounddown2(destp, sizeof(uint32_t)); 3480 imgp->pagesizes = (void *)destp; 3481 error = copyout(pagesizes32, imgp->pagesizes, sizeof(pagesizes32)); 3482 if (error != 0) 3483 return (error); 3484 imgp->pagesizeslen = sizeof(pagesizes32); 3485 3486 /* 3487 * Allocate room for the argument and environment strings. 3488 */ 3489 destp -= ARG_MAX - imgp->args->stringspace; 3490 destp = rounddown2(destp, sizeof(uint32_t)); 3491 ustringp = destp; 3492 3493 if (imgp->auxargs) { 3494 /* 3495 * Allocate room on the stack for the ELF auxargs 3496 * array. It has up to AT_COUNT entries. 3497 */ 3498 destp -= AT_COUNT * sizeof(Elf32_Auxinfo); 3499 destp = rounddown2(destp, sizeof(uint32_t)); 3500 } 3501 3502 vectp = (uint32_t *)destp; 3503 3504 /* 3505 * Allocate room for the argv[] and env vectors including the 3506 * terminating NULL pointers. 3507 */ 3508 vectp -= imgp->args->argc + 1 + imgp->args->envc + 1; 3509 3510 /* 3511 * vectp also becomes our initial stack base 3512 */ 3513 *stack_base = (uintptr_t)vectp; 3514 3515 stringp = imgp->args->begin_argv; 3516 argc = imgp->args->argc; 3517 envc = imgp->args->envc; 3518 /* 3519 * Copy out strings - arguments and environment. 3520 */ 3521 error = copyout(stringp, (void *)ustringp, 3522 ARG_MAX - imgp->args->stringspace); 3523 if (error != 0) 3524 return (error); 3525 3526 /* 3527 * Fill in "ps_strings" struct for ps, w, etc. 3528 */ 3529 imgp->argv = vectp; 3530 if (suword32(&arginfo->ps_argvstr, (uint32_t)(intptr_t)vectp) != 0 || 3531 suword32(&arginfo->ps_nargvstr, argc) != 0) 3532 return (EFAULT); 3533 3534 /* 3535 * Fill in argument portion of vector table. 3536 */ 3537 for (; argc > 0; --argc) { 3538 if (suword32(vectp++, ustringp) != 0) 3539 return (EFAULT); 3540 while (*stringp++ != 0) 3541 ustringp++; 3542 ustringp++; 3543 } 3544 3545 /* a null vector table pointer separates the argp's from the envp's */ 3546 if (suword32(vectp++, 0) != 0) 3547 return (EFAULT); 3548 3549 imgp->envv = vectp; 3550 if (suword32(&arginfo->ps_envstr, (uint32_t)(intptr_t)vectp) != 0 || 3551 suword32(&arginfo->ps_nenvstr, envc) != 0) 3552 return (EFAULT); 3553 3554 /* 3555 * Fill in environment portion of vector table. 3556 */ 3557 for (; envc > 0; --envc) { 3558 if (suword32(vectp++, ustringp) != 0) 3559 return (EFAULT); 3560 while (*stringp++ != 0) 3561 ustringp++; 3562 ustringp++; 3563 } 3564 3565 /* end of vector table is a null pointer */ 3566 if (suword32(vectp, 0) != 0) 3567 return (EFAULT); 3568 3569 if (imgp->auxargs) { 3570 vectp++; 3571 error = imgp->sysent->sv_copyout_auxargs(imgp, 3572 (uintptr_t)vectp); 3573 if (error != 0) 3574 return (error); 3575 } 3576 3577 return (0); 3578 } 3579 3580 int 3581 freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap) 3582 { 3583 struct kld_file_stat *stat; 3584 struct kld_file_stat32 *stat32; 3585 int error, version; 3586 3587 if ((error = copyin(&uap->stat->version, &version, sizeof(version))) 3588 != 0) 3589 return (error); 3590 if (version != sizeof(struct kld_file_stat_1_32) && 3591 version != sizeof(struct kld_file_stat32)) 3592 return (EINVAL); 3593 3594 stat = malloc(sizeof(*stat), M_TEMP, M_WAITOK | M_ZERO); 3595 stat32 = malloc(sizeof(*stat32), M_TEMP, M_WAITOK | M_ZERO); 3596 error = kern_kldstat(td, uap->fileid, stat); 3597 if (error == 0) { 3598 bcopy(&stat->name[0], &stat32->name[0], sizeof(stat->name)); 3599 CP(*stat, *stat32, refs); 3600 CP(*stat, *stat32, id); 3601 PTROUT_CP(*stat, *stat32, address); 3602 CP(*stat, *stat32, size); 3603 bcopy(&stat->pathname[0], &stat32->pathname[0], 3604 sizeof(stat->pathname)); 3605 stat32->version = version; 3606 error = copyout(stat32, uap->stat, version); 3607 } 3608 free(stat, M_TEMP); 3609 free(stat32, M_TEMP); 3610 return (error); 3611 } 3612 3613 int 3614 freebsd32_posix_fallocate(struct thread *td, 3615 struct freebsd32_posix_fallocate_args *uap) 3616 { 3617 int error; 3618 3619 error = kern_posix_fallocate(td, uap->fd, 3620 PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len)); 3621 return (kern_posix_error(td, error)); 3622 } 3623 3624 int 3625 freebsd32_posix_fadvise(struct thread *td, 3626 struct freebsd32_posix_fadvise_args *uap) 3627 { 3628 int error; 3629 3630 error = kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset), 3631 PAIR32TO64(off_t, uap->len), uap->advice); 3632 return (kern_posix_error(td, error)); 3633 } 3634 3635 int 3636 convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig) 3637 { 3638 3639 CP(*sig32, *sig, sigev_notify); 3640 switch (sig->sigev_notify) { 3641 case SIGEV_NONE: 3642 break; 3643 case SIGEV_THREAD_ID: 3644 CP(*sig32, *sig, sigev_notify_thread_id); 3645 /* FALLTHROUGH */ 3646 case SIGEV_SIGNAL: 3647 CP(*sig32, *sig, sigev_signo); 3648 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr); 3649 break; 3650 case SIGEV_KEVENT: 3651 CP(*sig32, *sig, sigev_notify_kqueue); 3652 CP(*sig32, *sig, sigev_notify_kevent_flags); 3653 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr); 3654 break; 3655 default: 3656 return (EINVAL); 3657 } 3658 return (0); 3659 } 3660 3661 int 3662 freebsd32_procctl(struct thread *td, struct freebsd32_procctl_args *uap) 3663 { 3664 void *data; 3665 union { 3666 struct procctl_reaper_status rs; 3667 struct procctl_reaper_pids rp; 3668 struct procctl_reaper_kill rk; 3669 } x; 3670 union { 3671 struct procctl_reaper_pids32 rp; 3672 } x32; 3673 int error, error1, flags, signum; 3674 3675 if (uap->com >= PROC_PROCCTL_MD_MIN) 3676 return (cpu_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id), 3677 uap->com, PTRIN(uap->data))); 3678 3679 switch (uap->com) { 3680 case PROC_ASLR_CTL: 3681 case PROC_PROTMAX_CTL: 3682 case PROC_SPROTECT: 3683 case PROC_STACKGAP_CTL: 3684 case PROC_TRACE_CTL: 3685 case PROC_TRAPCAP_CTL: 3686 case PROC_NO_NEW_PRIVS_CTL: 3687 case PROC_WXMAP_CTL: 3688 error = copyin(PTRIN(uap->data), &flags, sizeof(flags)); 3689 if (error != 0) 3690 return (error); 3691 data = &flags; 3692 break; 3693 case PROC_REAP_ACQUIRE: 3694 case PROC_REAP_RELEASE: 3695 if (uap->data != NULL) 3696 return (EINVAL); 3697 data = NULL; 3698 break; 3699 case PROC_REAP_STATUS: 3700 data = &x.rs; 3701 break; 3702 case PROC_REAP_GETPIDS: 3703 error = copyin(uap->data, &x32.rp, sizeof(x32.rp)); 3704 if (error != 0) 3705 return (error); 3706 CP(x32.rp, x.rp, rp_count); 3707 PTRIN_CP(x32.rp, x.rp, rp_pids); 3708 data = &x.rp; 3709 break; 3710 case PROC_REAP_KILL: 3711 error = copyin(uap->data, &x.rk, sizeof(x.rk)); 3712 if (error != 0) 3713 return (error); 3714 data = &x.rk; 3715 break; 3716 case PROC_ASLR_STATUS: 3717 case PROC_PROTMAX_STATUS: 3718 case PROC_STACKGAP_STATUS: 3719 case PROC_TRACE_STATUS: 3720 case PROC_TRAPCAP_STATUS: 3721 case PROC_NO_NEW_PRIVS_STATUS: 3722 case PROC_WXMAP_STATUS: 3723 data = &flags; 3724 break; 3725 case PROC_PDEATHSIG_CTL: 3726 error = copyin(uap->data, &signum, sizeof(signum)); 3727 if (error != 0) 3728 return (error); 3729 data = &signum; 3730 break; 3731 case PROC_PDEATHSIG_STATUS: 3732 data = &signum; 3733 break; 3734 default: 3735 return (EINVAL); 3736 } 3737 error = kern_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id), 3738 uap->com, data); 3739 switch (uap->com) { 3740 case PROC_REAP_STATUS: 3741 if (error == 0) 3742 error = copyout(&x.rs, uap->data, sizeof(x.rs)); 3743 break; 3744 case PROC_REAP_KILL: 3745 error1 = copyout(&x.rk, uap->data, sizeof(x.rk)); 3746 if (error == 0) 3747 error = error1; 3748 break; 3749 case PROC_ASLR_STATUS: 3750 case PROC_PROTMAX_STATUS: 3751 case PROC_STACKGAP_STATUS: 3752 case PROC_TRACE_STATUS: 3753 case PROC_TRAPCAP_STATUS: 3754 case PROC_NO_NEW_PRIVS_STATUS: 3755 case PROC_WXMAP_STATUS: 3756 if (error == 0) 3757 error = copyout(&flags, uap->data, sizeof(flags)); 3758 break; 3759 case PROC_PDEATHSIG_STATUS: 3760 if (error == 0) 3761 error = copyout(&signum, uap->data, sizeof(signum)); 3762 break; 3763 } 3764 return (error); 3765 } 3766 3767 int 3768 freebsd32_fcntl(struct thread *td, struct freebsd32_fcntl_args *uap) 3769 { 3770 long tmp; 3771 3772 switch (uap->cmd) { 3773 /* 3774 * Do unsigned conversion for arg when operation 3775 * interprets it as flags or pointer. 3776 */ 3777 case F_SETLK_REMOTE: 3778 case F_SETLKW: 3779 case F_SETLK: 3780 case F_GETLK: 3781 case F_SETFD: 3782 case F_SETFL: 3783 case F_OGETLK: 3784 case F_OSETLK: 3785 case F_OSETLKW: 3786 case F_KINFO: 3787 tmp = (unsigned int)(uap->arg); 3788 break; 3789 default: 3790 tmp = uap->arg; 3791 break; 3792 } 3793 return (kern_fcntl_freebsd(td, uap->fd, uap->cmd, tmp)); 3794 } 3795 3796 int 3797 freebsd32_ppoll(struct thread *td, struct freebsd32_ppoll_args *uap) 3798 { 3799 struct timespec32 ts32; 3800 struct timespec ts, *tsp; 3801 sigset_t set, *ssp; 3802 int error; 3803 3804 if (uap->ts != NULL) { 3805 error = copyin(uap->ts, &ts32, sizeof(ts32)); 3806 if (error != 0) 3807 return (error); 3808 CP(ts32, ts, tv_sec); 3809 CP(ts32, ts, tv_nsec); 3810 tsp = &ts; 3811 } else 3812 tsp = NULL; 3813 if (uap->set != NULL) { 3814 error = copyin(uap->set, &set, sizeof(set)); 3815 if (error != 0) 3816 return (error); 3817 ssp = &set; 3818 } else 3819 ssp = NULL; 3820 3821 return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp)); 3822 } 3823 3824 int 3825 freebsd32_sched_rr_get_interval(struct thread *td, 3826 struct freebsd32_sched_rr_get_interval_args *uap) 3827 { 3828 struct timespec ts; 3829 struct timespec32 ts32; 3830 int error; 3831 3832 error = kern_sched_rr_get_interval(td, uap->pid, &ts); 3833 if (error == 0) { 3834 CP(ts, ts32, tv_sec); 3835 CP(ts, ts32, tv_nsec); 3836 error = copyout(&ts32, uap->interval, sizeof(ts32)); 3837 } 3838 return (error); 3839 } 3840 3841 static void 3842 timex_to_32(struct timex32 *dst, struct timex *src) 3843 { 3844 CP(*src, *dst, modes); 3845 CP(*src, *dst, offset); 3846 CP(*src, *dst, freq); 3847 CP(*src, *dst, maxerror); 3848 CP(*src, *dst, esterror); 3849 CP(*src, *dst, status); 3850 CP(*src, *dst, constant); 3851 CP(*src, *dst, precision); 3852 CP(*src, *dst, tolerance); 3853 CP(*src, *dst, ppsfreq); 3854 CP(*src, *dst, jitter); 3855 CP(*src, *dst, shift); 3856 CP(*src, *dst, stabil); 3857 CP(*src, *dst, jitcnt); 3858 CP(*src, *dst, calcnt); 3859 CP(*src, *dst, errcnt); 3860 CP(*src, *dst, stbcnt); 3861 } 3862 3863 static void 3864 timex_from_32(struct timex *dst, struct timex32 *src) 3865 { 3866 CP(*src, *dst, modes); 3867 CP(*src, *dst, offset); 3868 CP(*src, *dst, freq); 3869 CP(*src, *dst, maxerror); 3870 CP(*src, *dst, esterror); 3871 CP(*src, *dst, status); 3872 CP(*src, *dst, constant); 3873 CP(*src, *dst, precision); 3874 CP(*src, *dst, tolerance); 3875 CP(*src, *dst, ppsfreq); 3876 CP(*src, *dst, jitter); 3877 CP(*src, *dst, shift); 3878 CP(*src, *dst, stabil); 3879 CP(*src, *dst, jitcnt); 3880 CP(*src, *dst, calcnt); 3881 CP(*src, *dst, errcnt); 3882 CP(*src, *dst, stbcnt); 3883 } 3884 3885 int 3886 freebsd32_ntp_adjtime(struct thread *td, struct freebsd32_ntp_adjtime_args *uap) 3887 { 3888 struct timex tx; 3889 struct timex32 tx32; 3890 int error, retval; 3891 3892 error = copyin(uap->tp, &tx32, sizeof(tx32)); 3893 if (error == 0) { 3894 timex_from_32(&tx, &tx32); 3895 error = kern_ntp_adjtime(td, &tx, &retval); 3896 if (error == 0) { 3897 timex_to_32(&tx32, &tx); 3898 error = copyout(&tx32, uap->tp, sizeof(tx32)); 3899 if (error == 0) 3900 td->td_retval[0] = retval; 3901 } 3902 } 3903 return (error); 3904 } 3905 3906 #ifdef FFCLOCK 3907 extern struct mtx ffclock_mtx; 3908 extern struct ffclock_estimate ffclock_estimate; 3909 extern int8_t ffclock_updated; 3910 3911 int 3912 freebsd32_ffclock_setestimate(struct thread *td, 3913 struct freebsd32_ffclock_setestimate_args *uap) 3914 { 3915 struct ffclock_estimate cest; 3916 struct ffclock_estimate32 cest32; 3917 int error; 3918 3919 /* Reuse of PRIV_CLOCK_SETTIME. */ 3920 if ((error = priv_check(td, PRIV_CLOCK_SETTIME)) != 0) 3921 return (error); 3922 3923 if ((error = copyin(uap->cest, &cest32, 3924 sizeof(struct ffclock_estimate32))) != 0) 3925 return (error); 3926 3927 CP(cest.update_time, cest32.update_time, sec); 3928 memcpy(&cest.update_time.frac, &cest32.update_time.frac, sizeof(uint64_t)); 3929 CP(cest, cest32, update_ffcount); 3930 CP(cest, cest32, leapsec_next); 3931 CP(cest, cest32, period); 3932 CP(cest, cest32, errb_abs); 3933 CP(cest, cest32, errb_rate); 3934 CP(cest, cest32, status); 3935 CP(cest, cest32, leapsec_total); 3936 CP(cest, cest32, leapsec); 3937 3938 mtx_lock(&ffclock_mtx); 3939 memcpy(&ffclock_estimate, &cest, sizeof(struct ffclock_estimate)); 3940 ffclock_updated++; 3941 mtx_unlock(&ffclock_mtx); 3942 return (error); 3943 } 3944 3945 int 3946 freebsd32_ffclock_getestimate(struct thread *td, 3947 struct freebsd32_ffclock_getestimate_args *uap) 3948 { 3949 struct ffclock_estimate cest; 3950 struct ffclock_estimate32 cest32; 3951 int error; 3952 3953 mtx_lock(&ffclock_mtx); 3954 memcpy(&cest, &ffclock_estimate, sizeof(struct ffclock_estimate)); 3955 mtx_unlock(&ffclock_mtx); 3956 3957 CP(cest32.update_time, cest.update_time, sec); 3958 memcpy(&cest32.update_time.frac, &cest.update_time.frac, sizeof(uint64_t)); 3959 CP(cest32, cest, update_ffcount); 3960 CP(cest32, cest, leapsec_next); 3961 CP(cest32, cest, period); 3962 CP(cest32, cest, errb_abs); 3963 CP(cest32, cest, errb_rate); 3964 CP(cest32, cest, status); 3965 CP(cest32, cest, leapsec_total); 3966 CP(cest32, cest, leapsec); 3967 3968 error = copyout(&cest32, uap->cest, sizeof(struct ffclock_estimate32)); 3969 return (error); 3970 } 3971 #else /* !FFCLOCK */ 3972 int 3973 freebsd32_ffclock_setestimate(struct thread *td, 3974 struct freebsd32_ffclock_setestimate_args *uap) 3975 { 3976 return (ENOSYS); 3977 } 3978 3979 int 3980 freebsd32_ffclock_getestimate(struct thread *td, 3981 struct freebsd32_ffclock_getestimate_args *uap) 3982 { 3983 return (ENOSYS); 3984 } 3985 #endif /* FFCLOCK */ 3986 3987 #ifdef COMPAT_43 3988 int 3989 ofreebsd32_sethostid(struct thread *td, struct ofreebsd32_sethostid_args *uap) 3990 { 3991 int name[] = { CTL_KERN, KERN_HOSTID }; 3992 long hostid; 3993 3994 hostid = uap->hostid; 3995 return (kernel_sysctl(td, name, nitems(name), NULL, NULL, &hostid, 3996 sizeof(hostid), NULL, 0)); 3997 } 3998 #endif 3999