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