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