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