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 return (freebsd10_pipe(td, (struct freebsd10_pipe_args*)uap)); 327 } 328 #endif 329 330 int 331 freebsd32_sigaltstack(struct thread *td, 332 struct freebsd32_sigaltstack_args *uap) 333 { 334 struct sigaltstack32 s32; 335 struct sigaltstack ss, oss, *ssp; 336 int error; 337 338 if (uap->ss != NULL) { 339 error = copyin(uap->ss, &s32, sizeof(s32)); 340 if (error) 341 return (error); 342 PTRIN_CP(s32, ss, ss_sp); 343 CP(s32, ss, ss_size); 344 CP(s32, ss, ss_flags); 345 ssp = &ss; 346 } else 347 ssp = NULL; 348 error = kern_sigaltstack(td, ssp, &oss); 349 if (error == 0 && uap->oss != NULL) { 350 PTROUT_CP(oss, s32, ss_sp); 351 CP(oss, s32, ss_size); 352 CP(oss, s32, ss_flags); 353 error = copyout(&s32, uap->oss, sizeof(s32)); 354 } 355 return (error); 356 } 357 358 /* 359 * Custom version of exec_copyin_args() so that we can translate 360 * the pointers. 361 */ 362 int 363 freebsd32_exec_copyin_args(struct image_args *args, const char *fname, 364 enum uio_seg segflg, u_int32_t *argv, u_int32_t *envv) 365 { 366 char *argp, *envp; 367 u_int32_t *p32, arg; 368 int error; 369 370 bzero(args, sizeof(*args)); 371 if (argv == NULL) 372 return (EFAULT); 373 374 /* 375 * Allocate demand-paged memory for the file name, argument, and 376 * environment strings. 377 */ 378 error = exec_alloc_args(args); 379 if (error != 0) 380 return (error); 381 382 /* 383 * Copy the file name. 384 */ 385 error = exec_args_add_fname(args, fname, segflg); 386 if (error != 0) 387 goto err_exit; 388 389 /* 390 * extract arguments first 391 */ 392 p32 = argv; 393 for (;;) { 394 error = copyin(p32++, &arg, sizeof(arg)); 395 if (error) 396 goto err_exit; 397 if (arg == 0) 398 break; 399 argp = PTRIN(arg); 400 error = exec_args_add_arg(args, argp, UIO_USERSPACE); 401 if (error != 0) 402 goto err_exit; 403 } 404 405 /* 406 * extract environment strings 407 */ 408 if (envv) { 409 p32 = envv; 410 for (;;) { 411 error = copyin(p32++, &arg, sizeof(arg)); 412 if (error) 413 goto err_exit; 414 if (arg == 0) 415 break; 416 envp = PTRIN(arg); 417 error = exec_args_add_env(args, envp, UIO_USERSPACE); 418 if (error != 0) 419 goto err_exit; 420 } 421 } 422 423 return (0); 424 425 err_exit: 426 exec_free_args(args); 427 return (error); 428 } 429 430 int 431 freebsd32_execve(struct thread *td, struct freebsd32_execve_args *uap) 432 { 433 struct image_args eargs; 434 struct vmspace *oldvmspace; 435 int error; 436 437 error = pre_execve(td, &oldvmspace); 438 if (error != 0) 439 return (error); 440 error = freebsd32_exec_copyin_args(&eargs, uap->fname, UIO_USERSPACE, 441 uap->argv, uap->envv); 442 if (error == 0) 443 error = kern_execve(td, &eargs, NULL, oldvmspace); 444 post_execve(td, error, oldvmspace); 445 AUDIT_SYSCALL_EXIT(error == EJUSTRETURN ? 0 : error, td); 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, oldvmspace); 464 } 465 post_execve(td, error, oldvmspace); 466 AUDIT_SYSCALL_EXIT(error == EJUSTRETURN ? 0 : error, td); 467 return (error); 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 cmsghdr *cm; 1453 struct mbuf *m; 1454 void *in, *in1, *md; 1455 u_int msglen, outlen; 1456 int error; 1457 1458 if (buflen > MCLBYTES) 1459 return (EINVAL); 1460 1461 in = malloc(buflen, M_TEMP, M_WAITOK); 1462 error = copyin(buf, in, buflen); 1463 if (error != 0) 1464 goto out; 1465 1466 /* 1467 * Make a pass over the input buffer to determine the amount of space 1468 * required for 64 bit-aligned copies of the control messages. 1469 */ 1470 in1 = in; 1471 outlen = 0; 1472 while (buflen > 0) { 1473 if (buflen < sizeof(*cm)) { 1474 error = EINVAL; 1475 break; 1476 } 1477 cm = (struct cmsghdr *)in1; 1478 if (cm->cmsg_len < FREEBSD32_ALIGN(sizeof(*cm))) { 1479 error = EINVAL; 1480 break; 1481 } 1482 msglen = FREEBSD32_ALIGN(cm->cmsg_len); 1483 if (msglen > buflen || msglen < cm->cmsg_len) { 1484 error = EINVAL; 1485 break; 1486 } 1487 buflen -= msglen; 1488 1489 in1 = (char *)in1 + msglen; 1490 outlen += CMSG_ALIGN(sizeof(*cm)) + 1491 CMSG_ALIGN(msglen - FREEBSD32_ALIGN(sizeof(*cm))); 1492 } 1493 if (error == 0 && outlen > MCLBYTES) { 1494 /* 1495 * XXXMJ This implies that the upper limit on 32-bit aligned 1496 * control messages is less than MCLBYTES, and so we are not 1497 * perfectly compatible. However, there is no platform 1498 * guarantee that mbuf clusters larger than MCLBYTES can be 1499 * allocated. 1500 */ 1501 error = EINVAL; 1502 } 1503 if (error != 0) 1504 goto out; 1505 1506 m = m_get2(outlen, M_WAITOK, MT_CONTROL, 0); 1507 m->m_len = outlen; 1508 md = mtod(m, void *); 1509 1510 /* 1511 * Make a second pass over input messages, copying them into the output 1512 * buffer. 1513 */ 1514 in1 = in; 1515 while (outlen > 0) { 1516 /* Copy the message header and align the length field. */ 1517 cm = md; 1518 memcpy(cm, in1, sizeof(*cm)); 1519 msglen = cm->cmsg_len - FREEBSD32_ALIGN(sizeof(*cm)); 1520 cm->cmsg_len = CMSG_ALIGN(sizeof(*cm)) + msglen; 1521 1522 /* Copy the message body. */ 1523 in1 = (char *)in1 + FREEBSD32_ALIGN(sizeof(*cm)); 1524 md = (char *)md + CMSG_ALIGN(sizeof(*cm)); 1525 memcpy(md, in1, msglen); 1526 in1 = (char *)in1 + FREEBSD32_ALIGN(msglen); 1527 md = (char *)md + CMSG_ALIGN(msglen); 1528 KASSERT(outlen >= CMSG_ALIGN(sizeof(*cm)) + CMSG_ALIGN(msglen), 1529 ("outlen %u underflow, msglen %u", outlen, msglen)); 1530 outlen -= CMSG_ALIGN(sizeof(*cm)) + CMSG_ALIGN(msglen); 1531 } 1532 1533 *mp = m; 1534 out: 1535 free(in, M_TEMP); 1536 return (error); 1537 } 1538 1539 int 1540 freebsd32_sendmsg(struct thread *td, 1541 struct freebsd32_sendmsg_args *uap) 1542 { 1543 struct msghdr msg; 1544 struct msghdr32 m32; 1545 struct iovec *iov; 1546 struct mbuf *control = NULL; 1547 struct sockaddr *to = NULL; 1548 int error; 1549 1550 error = copyin(uap->msg, &m32, sizeof(m32)); 1551 if (error) 1552 return (error); 1553 error = freebsd32_copyinmsghdr(uap->msg, &msg); 1554 if (error) 1555 return (error); 1556 error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov, 1557 EMSGSIZE); 1558 if (error) 1559 return (error); 1560 msg.msg_iov = iov; 1561 if (msg.msg_name != NULL) { 1562 error = getsockaddr(&to, msg.msg_name, msg.msg_namelen); 1563 if (error) { 1564 to = NULL; 1565 goto out; 1566 } 1567 msg.msg_name = to; 1568 } 1569 1570 if (msg.msg_control) { 1571 if (msg.msg_controllen < sizeof(struct cmsghdr)) { 1572 error = EINVAL; 1573 goto out; 1574 } 1575 1576 error = freebsd32_copyin_control(&control, msg.msg_control, 1577 msg.msg_controllen); 1578 if (error) 1579 goto out; 1580 1581 msg.msg_control = NULL; 1582 msg.msg_controllen = 0; 1583 } 1584 1585 error = kern_sendit(td, uap->s, &msg, uap->flags, control, 1586 UIO_USERSPACE); 1587 1588 out: 1589 free(iov, M_IOV); 1590 if (to) 1591 free(to, M_SONAME); 1592 return (error); 1593 } 1594 1595 int 1596 freebsd32_recvfrom(struct thread *td, 1597 struct freebsd32_recvfrom_args *uap) 1598 { 1599 struct msghdr msg; 1600 struct iovec aiov; 1601 int error; 1602 1603 if (uap->fromlenaddr) { 1604 error = copyin(PTRIN(uap->fromlenaddr), &msg.msg_namelen, 1605 sizeof(msg.msg_namelen)); 1606 if (error) 1607 return (error); 1608 } else { 1609 msg.msg_namelen = 0; 1610 } 1611 1612 msg.msg_name = PTRIN(uap->from); 1613 msg.msg_iov = &aiov; 1614 msg.msg_iovlen = 1; 1615 aiov.iov_base = PTRIN(uap->buf); 1616 aiov.iov_len = uap->len; 1617 msg.msg_control = NULL; 1618 msg.msg_flags = uap->flags; 1619 error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, NULL); 1620 if (error == 0 && uap->fromlenaddr) 1621 error = copyout(&msg.msg_namelen, PTRIN(uap->fromlenaddr), 1622 sizeof (msg.msg_namelen)); 1623 return (error); 1624 } 1625 1626 int 1627 freebsd32_settimeofday(struct thread *td, 1628 struct freebsd32_settimeofday_args *uap) 1629 { 1630 struct timeval32 tv32; 1631 struct timeval tv, *tvp; 1632 struct timezone tz, *tzp; 1633 int error; 1634 1635 if (uap->tv) { 1636 error = copyin(uap->tv, &tv32, sizeof(tv32)); 1637 if (error) 1638 return (error); 1639 CP(tv32, tv, tv_sec); 1640 CP(tv32, tv, tv_usec); 1641 tvp = &tv; 1642 } else 1643 tvp = NULL; 1644 if (uap->tzp) { 1645 error = copyin(uap->tzp, &tz, sizeof(tz)); 1646 if (error) 1647 return (error); 1648 tzp = &tz; 1649 } else 1650 tzp = NULL; 1651 return (kern_settimeofday(td, tvp, tzp)); 1652 } 1653 1654 int 1655 freebsd32_utimes(struct thread *td, struct freebsd32_utimes_args *uap) 1656 { 1657 struct timeval32 s32[2]; 1658 struct timeval s[2], *sp; 1659 int error; 1660 1661 if (uap->tptr != NULL) { 1662 error = copyin(uap->tptr, s32, sizeof(s32)); 1663 if (error) 1664 return (error); 1665 CP(s32[0], s[0], tv_sec); 1666 CP(s32[0], s[0], tv_usec); 1667 CP(s32[1], s[1], tv_sec); 1668 CP(s32[1], s[1], tv_usec); 1669 sp = s; 1670 } else 1671 sp = NULL; 1672 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 1673 sp, UIO_SYSSPACE)); 1674 } 1675 1676 int 1677 freebsd32_lutimes(struct thread *td, struct freebsd32_lutimes_args *uap) 1678 { 1679 struct timeval32 s32[2]; 1680 struct timeval s[2], *sp; 1681 int error; 1682 1683 if (uap->tptr != NULL) { 1684 error = copyin(uap->tptr, s32, sizeof(s32)); 1685 if (error) 1686 return (error); 1687 CP(s32[0], s[0], tv_sec); 1688 CP(s32[0], s[0], tv_usec); 1689 CP(s32[1], s[1], tv_sec); 1690 CP(s32[1], s[1], tv_usec); 1691 sp = s; 1692 } else 1693 sp = NULL; 1694 return (kern_lutimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE)); 1695 } 1696 1697 int 1698 freebsd32_futimes(struct thread *td, struct freebsd32_futimes_args *uap) 1699 { 1700 struct timeval32 s32[2]; 1701 struct timeval s[2], *sp; 1702 int error; 1703 1704 if (uap->tptr != NULL) { 1705 error = copyin(uap->tptr, s32, sizeof(s32)); 1706 if (error) 1707 return (error); 1708 CP(s32[0], s[0], tv_sec); 1709 CP(s32[0], s[0], tv_usec); 1710 CP(s32[1], s[1], tv_sec); 1711 CP(s32[1], s[1], tv_usec); 1712 sp = s; 1713 } else 1714 sp = NULL; 1715 return (kern_futimes(td, uap->fd, sp, UIO_SYSSPACE)); 1716 } 1717 1718 int 1719 freebsd32_futimesat(struct thread *td, struct freebsd32_futimesat_args *uap) 1720 { 1721 struct timeval32 s32[2]; 1722 struct timeval s[2], *sp; 1723 int error; 1724 1725 if (uap->times != NULL) { 1726 error = copyin(uap->times, s32, sizeof(s32)); 1727 if (error) 1728 return (error); 1729 CP(s32[0], s[0], tv_sec); 1730 CP(s32[0], s[0], tv_usec); 1731 CP(s32[1], s[1], tv_sec); 1732 CP(s32[1], s[1], tv_usec); 1733 sp = s; 1734 } else 1735 sp = NULL; 1736 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE, 1737 sp, UIO_SYSSPACE)); 1738 } 1739 1740 int 1741 freebsd32_futimens(struct thread *td, struct freebsd32_futimens_args *uap) 1742 { 1743 struct timespec32 ts32[2]; 1744 struct timespec ts[2], *tsp; 1745 int error; 1746 1747 if (uap->times != NULL) { 1748 error = copyin(uap->times, ts32, sizeof(ts32)); 1749 if (error) 1750 return (error); 1751 CP(ts32[0], ts[0], tv_sec); 1752 CP(ts32[0], ts[0], tv_nsec); 1753 CP(ts32[1], ts[1], tv_sec); 1754 CP(ts32[1], ts[1], tv_nsec); 1755 tsp = ts; 1756 } else 1757 tsp = NULL; 1758 return (kern_futimens(td, uap->fd, tsp, UIO_SYSSPACE)); 1759 } 1760 1761 int 1762 freebsd32_utimensat(struct thread *td, struct freebsd32_utimensat_args *uap) 1763 { 1764 struct timespec32 ts32[2]; 1765 struct timespec ts[2], *tsp; 1766 int error; 1767 1768 if (uap->times != NULL) { 1769 error = copyin(uap->times, ts32, sizeof(ts32)); 1770 if (error) 1771 return (error); 1772 CP(ts32[0], ts[0], tv_sec); 1773 CP(ts32[0], ts[0], tv_nsec); 1774 CP(ts32[1], ts[1], tv_sec); 1775 CP(ts32[1], ts[1], tv_nsec); 1776 tsp = ts; 1777 } else 1778 tsp = NULL; 1779 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE, 1780 tsp, UIO_SYSSPACE, uap->flag)); 1781 } 1782 1783 int 1784 freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap) 1785 { 1786 struct timeval32 tv32; 1787 struct timeval delta, olddelta, *deltap; 1788 int error; 1789 1790 if (uap->delta) { 1791 error = copyin(uap->delta, &tv32, sizeof(tv32)); 1792 if (error) 1793 return (error); 1794 CP(tv32, delta, tv_sec); 1795 CP(tv32, delta, tv_usec); 1796 deltap = δ 1797 } else 1798 deltap = NULL; 1799 error = kern_adjtime(td, deltap, &olddelta); 1800 if (uap->olddelta && error == 0) { 1801 CP(olddelta, tv32, tv_sec); 1802 CP(olddelta, tv32, tv_usec); 1803 error = copyout(&tv32, uap->olddelta, sizeof(tv32)); 1804 } 1805 return (error); 1806 } 1807 1808 #ifdef COMPAT_FREEBSD4 1809 int 1810 freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap) 1811 { 1812 struct statfs32 s32; 1813 struct statfs *sp; 1814 int error; 1815 1816 sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK); 1817 error = kern_statfs(td, uap->path, UIO_USERSPACE, sp); 1818 if (error == 0) { 1819 copy_statfs(sp, &s32); 1820 error = copyout(&s32, uap->buf, sizeof(s32)); 1821 } 1822 free(sp, M_STATFS); 1823 return (error); 1824 } 1825 #endif 1826 1827 #ifdef COMPAT_FREEBSD4 1828 int 1829 freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap) 1830 { 1831 struct statfs32 s32; 1832 struct statfs *sp; 1833 int error; 1834 1835 sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK); 1836 error = kern_fstatfs(td, uap->fd, sp); 1837 if (error == 0) { 1838 copy_statfs(sp, &s32); 1839 error = copyout(&s32, uap->buf, sizeof(s32)); 1840 } 1841 free(sp, M_STATFS); 1842 return (error); 1843 } 1844 #endif 1845 1846 #ifdef COMPAT_FREEBSD4 1847 int 1848 freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap) 1849 { 1850 struct statfs32 s32; 1851 struct statfs *sp; 1852 fhandle_t fh; 1853 int error; 1854 1855 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0) 1856 return (error); 1857 sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK); 1858 error = kern_fhstatfs(td, fh, sp); 1859 if (error == 0) { 1860 copy_statfs(sp, &s32); 1861 error = copyout(&s32, uap->buf, sizeof(s32)); 1862 } 1863 free(sp, M_STATFS); 1864 return (error); 1865 } 1866 #endif 1867 1868 int 1869 freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap) 1870 { 1871 1872 return (kern_pread(td, uap->fd, uap->buf, uap->nbyte, 1873 PAIR32TO64(off_t, uap->offset))); 1874 } 1875 1876 int 1877 freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap) 1878 { 1879 1880 return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, 1881 PAIR32TO64(off_t, uap->offset))); 1882 } 1883 1884 #ifdef COMPAT_43 1885 int 1886 ofreebsd32_lseek(struct thread *td, struct ofreebsd32_lseek_args *uap) 1887 { 1888 1889 return (kern_lseek(td, uap->fd, uap->offset, uap->whence)); 1890 } 1891 #endif 1892 1893 int 1894 freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap) 1895 { 1896 int error; 1897 off_t pos; 1898 1899 error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset), 1900 uap->whence); 1901 /* Expand the quad return into two parts for eax and edx */ 1902 pos = td->td_uretoff.tdu_off; 1903 td->td_retval[RETVAL_LO] = pos & 0xffffffff; /* %eax */ 1904 td->td_retval[RETVAL_HI] = pos >> 32; /* %edx */ 1905 return error; 1906 } 1907 1908 int 1909 freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap) 1910 { 1911 1912 return (kern_truncate(td, uap->path, UIO_USERSPACE, 1913 PAIR32TO64(off_t, uap->length))); 1914 } 1915 1916 int 1917 freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap) 1918 { 1919 1920 return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length))); 1921 } 1922 1923 #ifdef COMPAT_43 1924 int 1925 ofreebsd32_getdirentries(struct thread *td, 1926 struct ofreebsd32_getdirentries_args *uap) 1927 { 1928 struct ogetdirentries_args ap; 1929 int error; 1930 long loff; 1931 int32_t loff_cut; 1932 1933 ap.fd = uap->fd; 1934 ap.buf = uap->buf; 1935 ap.count = uap->count; 1936 ap.basep = NULL; 1937 error = kern_ogetdirentries(td, &ap, &loff); 1938 if (error == 0) { 1939 loff_cut = loff; 1940 error = copyout(&loff_cut, uap->basep, sizeof(int32_t)); 1941 } 1942 return (error); 1943 } 1944 #endif 1945 1946 #if defined(COMPAT_FREEBSD11) 1947 int 1948 freebsd11_freebsd32_getdirentries(struct thread *td, 1949 struct freebsd11_freebsd32_getdirentries_args *uap) 1950 { 1951 long base; 1952 int32_t base32; 1953 int error; 1954 1955 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count, 1956 &base, NULL); 1957 if (error) 1958 return (error); 1959 if (uap->basep != NULL) { 1960 base32 = base; 1961 error = copyout(&base32, uap->basep, sizeof(int32_t)); 1962 } 1963 return (error); 1964 } 1965 1966 int 1967 freebsd11_freebsd32_getdents(struct thread *td, 1968 struct freebsd11_freebsd32_getdents_args *uap) 1969 { 1970 struct freebsd11_freebsd32_getdirentries_args ap; 1971 1972 ap.fd = uap->fd; 1973 ap.buf = uap->buf; 1974 ap.count = uap->count; 1975 ap.basep = NULL; 1976 return (freebsd11_freebsd32_getdirentries(td, &ap)); 1977 } 1978 #endif /* COMPAT_FREEBSD11 */ 1979 1980 #ifdef COMPAT_FREEBSD6 1981 /* versions with the 'int pad' argument */ 1982 int 1983 freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap) 1984 { 1985 1986 return (kern_pread(td, uap->fd, uap->buf, uap->nbyte, 1987 PAIR32TO64(off_t, uap->offset))); 1988 } 1989 1990 int 1991 freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap) 1992 { 1993 1994 return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, 1995 PAIR32TO64(off_t, uap->offset))); 1996 } 1997 1998 int 1999 freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap) 2000 { 2001 int error; 2002 off_t pos; 2003 2004 error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset), 2005 uap->whence); 2006 /* Expand the quad return into two parts for eax and edx */ 2007 pos = *(off_t *)(td->td_retval); 2008 td->td_retval[RETVAL_LO] = pos & 0xffffffff; /* %eax */ 2009 td->td_retval[RETVAL_HI] = pos >> 32; /* %edx */ 2010 return error; 2011 } 2012 2013 int 2014 freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap) 2015 { 2016 2017 return (kern_truncate(td, uap->path, UIO_USERSPACE, 2018 PAIR32TO64(off_t, uap->length))); 2019 } 2020 2021 int 2022 freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap) 2023 { 2024 2025 return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length))); 2026 } 2027 #endif /* COMPAT_FREEBSD6 */ 2028 2029 struct sf_hdtr32 { 2030 uint32_t headers; 2031 int hdr_cnt; 2032 uint32_t trailers; 2033 int trl_cnt; 2034 }; 2035 2036 static int 2037 freebsd32_do_sendfile(struct thread *td, 2038 struct freebsd32_sendfile_args *uap, int compat) 2039 { 2040 struct sf_hdtr32 hdtr32; 2041 struct sf_hdtr hdtr; 2042 struct uio *hdr_uio, *trl_uio; 2043 struct file *fp; 2044 cap_rights_t rights; 2045 struct iovec32 *iov32; 2046 off_t offset, sbytes; 2047 int error; 2048 2049 offset = PAIR32TO64(off_t, uap->offset); 2050 if (offset < 0) 2051 return (EINVAL); 2052 2053 hdr_uio = trl_uio = NULL; 2054 2055 if (uap->hdtr != NULL) { 2056 error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32)); 2057 if (error) 2058 goto out; 2059 PTRIN_CP(hdtr32, hdtr, headers); 2060 CP(hdtr32, hdtr, hdr_cnt); 2061 PTRIN_CP(hdtr32, hdtr, trailers); 2062 CP(hdtr32, hdtr, trl_cnt); 2063 2064 if (hdtr.headers != NULL) { 2065 iov32 = PTRIN(hdtr32.headers); 2066 error = freebsd32_copyinuio(iov32, 2067 hdtr32.hdr_cnt, &hdr_uio); 2068 if (error) 2069 goto out; 2070 #ifdef COMPAT_FREEBSD4 2071 /* 2072 * In FreeBSD < 5.0 the nbytes to send also included 2073 * the header. If compat is specified subtract the 2074 * header size from nbytes. 2075 */ 2076 if (compat) { 2077 if (uap->nbytes > hdr_uio->uio_resid) 2078 uap->nbytes -= hdr_uio->uio_resid; 2079 else 2080 uap->nbytes = 0; 2081 } 2082 #endif 2083 } 2084 if (hdtr.trailers != NULL) { 2085 iov32 = PTRIN(hdtr32.trailers); 2086 error = freebsd32_copyinuio(iov32, 2087 hdtr32.trl_cnt, &trl_uio); 2088 if (error) 2089 goto out; 2090 } 2091 } 2092 2093 AUDIT_ARG_FD(uap->fd); 2094 2095 if ((error = fget_read(td, uap->fd, 2096 cap_rights_init(&rights, CAP_PREAD), &fp)) != 0) 2097 goto out; 2098 2099 error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, offset, 2100 uap->nbytes, &sbytes, uap->flags, td); 2101 fdrop(fp, td); 2102 2103 if (uap->sbytes != NULL) 2104 copyout(&sbytes, uap->sbytes, sizeof(off_t)); 2105 2106 out: 2107 if (hdr_uio) 2108 free(hdr_uio, M_IOV); 2109 if (trl_uio) 2110 free(trl_uio, M_IOV); 2111 return (error); 2112 } 2113 2114 #ifdef COMPAT_FREEBSD4 2115 int 2116 freebsd4_freebsd32_sendfile(struct thread *td, 2117 struct freebsd4_freebsd32_sendfile_args *uap) 2118 { 2119 return (freebsd32_do_sendfile(td, 2120 (struct freebsd32_sendfile_args *)uap, 1)); 2121 } 2122 #endif 2123 2124 int 2125 freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap) 2126 { 2127 2128 return (freebsd32_do_sendfile(td, uap, 0)); 2129 } 2130 2131 static void 2132 copy_stat(struct stat *in, struct stat32 *out) 2133 { 2134 2135 CP(*in, *out, st_dev); 2136 CP(*in, *out, st_ino); 2137 CP(*in, *out, st_mode); 2138 CP(*in, *out, st_nlink); 2139 CP(*in, *out, st_uid); 2140 CP(*in, *out, st_gid); 2141 CP(*in, *out, st_rdev); 2142 TS_CP(*in, *out, st_atim); 2143 TS_CP(*in, *out, st_mtim); 2144 TS_CP(*in, *out, st_ctim); 2145 CP(*in, *out, st_size); 2146 CP(*in, *out, st_blocks); 2147 CP(*in, *out, st_blksize); 2148 CP(*in, *out, st_flags); 2149 CP(*in, *out, st_gen); 2150 TS_CP(*in, *out, st_birthtim); 2151 out->st_padding0 = 0; 2152 out->st_padding1 = 0; 2153 #ifdef __STAT32_TIME_T_EXT 2154 out->st_atim_ext = 0; 2155 out->st_mtim_ext = 0; 2156 out->st_ctim_ext = 0; 2157 out->st_btim_ext = 0; 2158 #endif 2159 bzero(out->st_spare, sizeof(out->st_spare)); 2160 } 2161 2162 #ifdef COMPAT_43 2163 static void 2164 copy_ostat(struct stat *in, struct ostat32 *out) 2165 { 2166 2167 bzero(out, sizeof(*out)); 2168 CP(*in, *out, st_dev); 2169 CP(*in, *out, st_ino); 2170 CP(*in, *out, st_mode); 2171 CP(*in, *out, st_nlink); 2172 CP(*in, *out, st_uid); 2173 CP(*in, *out, st_gid); 2174 CP(*in, *out, st_rdev); 2175 out->st_size = MIN(in->st_size, INT32_MAX); 2176 TS_CP(*in, *out, st_atim); 2177 TS_CP(*in, *out, st_mtim); 2178 TS_CP(*in, *out, st_ctim); 2179 CP(*in, *out, st_blksize); 2180 CP(*in, *out, st_blocks); 2181 CP(*in, *out, st_flags); 2182 CP(*in, *out, st_gen); 2183 } 2184 #endif 2185 2186 #ifdef COMPAT_43 2187 int 2188 ofreebsd32_stat(struct thread *td, struct ofreebsd32_stat_args *uap) 2189 { 2190 struct stat sb; 2191 struct ostat32 sb32; 2192 int error; 2193 2194 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE, 2195 &sb, NULL); 2196 if (error) 2197 return (error); 2198 copy_ostat(&sb, &sb32); 2199 error = copyout(&sb32, uap->ub, sizeof (sb32)); 2200 return (error); 2201 } 2202 #endif 2203 2204 int 2205 freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap) 2206 { 2207 struct stat ub; 2208 struct stat32 ub32; 2209 int error; 2210 2211 error = kern_fstat(td, uap->fd, &ub); 2212 if (error) 2213 return (error); 2214 copy_stat(&ub, &ub32); 2215 error = copyout(&ub32, uap->ub, sizeof(ub32)); 2216 return (error); 2217 } 2218 2219 #ifdef COMPAT_43 2220 int 2221 ofreebsd32_fstat(struct thread *td, struct ofreebsd32_fstat_args *uap) 2222 { 2223 struct stat ub; 2224 struct ostat32 ub32; 2225 int error; 2226 2227 error = kern_fstat(td, uap->fd, &ub); 2228 if (error) 2229 return (error); 2230 copy_ostat(&ub, &ub32); 2231 error = copyout(&ub32, uap->ub, sizeof(ub32)); 2232 return (error); 2233 } 2234 #endif 2235 2236 int 2237 freebsd32_fstatat(struct thread *td, struct freebsd32_fstatat_args *uap) 2238 { 2239 struct stat ub; 2240 struct stat32 ub32; 2241 int error; 2242 2243 error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE, 2244 &ub, NULL); 2245 if (error) 2246 return (error); 2247 copy_stat(&ub, &ub32); 2248 error = copyout(&ub32, uap->buf, sizeof(ub32)); 2249 return (error); 2250 } 2251 2252 #ifdef COMPAT_43 2253 int 2254 ofreebsd32_lstat(struct thread *td, struct ofreebsd32_lstat_args *uap) 2255 { 2256 struct stat sb; 2257 struct ostat32 sb32; 2258 int error; 2259 2260 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path, 2261 UIO_USERSPACE, &sb, NULL); 2262 if (error) 2263 return (error); 2264 copy_ostat(&sb, &sb32); 2265 error = copyout(&sb32, uap->ub, sizeof (sb32)); 2266 return (error); 2267 } 2268 #endif 2269 2270 int 2271 freebsd32_fhstat(struct thread *td, struct freebsd32_fhstat_args *uap) 2272 { 2273 struct stat sb; 2274 struct stat32 sb32; 2275 struct fhandle fh; 2276 int error; 2277 2278 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t)); 2279 if (error != 0) 2280 return (error); 2281 error = kern_fhstat(td, fh, &sb); 2282 if (error != 0) 2283 return (error); 2284 copy_stat(&sb, &sb32); 2285 error = copyout(&sb32, uap->sb, sizeof (sb32)); 2286 return (error); 2287 } 2288 2289 #if defined(COMPAT_FREEBSD11) 2290 extern int ino64_trunc_error; 2291 2292 static int 2293 freebsd11_cvtstat32(struct stat *in, struct freebsd11_stat32 *out) 2294 { 2295 2296 CP(*in, *out, st_ino); 2297 if (in->st_ino != out->st_ino) { 2298 switch (ino64_trunc_error) { 2299 default: 2300 case 0: 2301 break; 2302 case 1: 2303 return (EOVERFLOW); 2304 case 2: 2305 out->st_ino = UINT32_MAX; 2306 break; 2307 } 2308 } 2309 CP(*in, *out, st_nlink); 2310 if (in->st_nlink != out->st_nlink) { 2311 switch (ino64_trunc_error) { 2312 default: 2313 case 0: 2314 break; 2315 case 1: 2316 return (EOVERFLOW); 2317 case 2: 2318 out->st_nlink = UINT16_MAX; 2319 break; 2320 } 2321 } 2322 out->st_dev = in->st_dev; 2323 if (out->st_dev != in->st_dev) { 2324 switch (ino64_trunc_error) { 2325 default: 2326 break; 2327 case 1: 2328 return (EOVERFLOW); 2329 } 2330 } 2331 CP(*in, *out, st_mode); 2332 CP(*in, *out, st_uid); 2333 CP(*in, *out, st_gid); 2334 out->st_rdev = in->st_rdev; 2335 if (out->st_rdev != in->st_rdev) { 2336 switch (ino64_trunc_error) { 2337 default: 2338 break; 2339 case 1: 2340 return (EOVERFLOW); 2341 } 2342 } 2343 TS_CP(*in, *out, st_atim); 2344 TS_CP(*in, *out, st_mtim); 2345 TS_CP(*in, *out, st_ctim); 2346 CP(*in, *out, st_size); 2347 CP(*in, *out, st_blocks); 2348 CP(*in, *out, st_blksize); 2349 CP(*in, *out, st_flags); 2350 CP(*in, *out, st_gen); 2351 TS_CP(*in, *out, st_birthtim); 2352 out->st_lspare = 0; 2353 bzero((char *)&out->st_birthtim + sizeof(out->st_birthtim), 2354 sizeof(*out) - offsetof(struct freebsd11_stat32, 2355 st_birthtim) - sizeof(out->st_birthtim)); 2356 return (0); 2357 } 2358 2359 int 2360 freebsd11_freebsd32_stat(struct thread *td, 2361 struct freebsd11_freebsd32_stat_args *uap) 2362 { 2363 struct stat sb; 2364 struct freebsd11_stat32 sb32; 2365 int error; 2366 2367 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE, 2368 &sb, NULL); 2369 if (error != 0) 2370 return (error); 2371 error = freebsd11_cvtstat32(&sb, &sb32); 2372 if (error == 0) 2373 error = copyout(&sb32, uap->ub, sizeof (sb32)); 2374 return (error); 2375 } 2376 2377 int 2378 freebsd11_freebsd32_fstat(struct thread *td, 2379 struct freebsd11_freebsd32_fstat_args *uap) 2380 { 2381 struct stat sb; 2382 struct freebsd11_stat32 sb32; 2383 int error; 2384 2385 error = kern_fstat(td, uap->fd, &sb); 2386 if (error != 0) 2387 return (error); 2388 error = freebsd11_cvtstat32(&sb, &sb32); 2389 if (error == 0) 2390 error = copyout(&sb32, uap->ub, sizeof (sb32)); 2391 return (error); 2392 } 2393 2394 int 2395 freebsd11_freebsd32_fstatat(struct thread *td, 2396 struct freebsd11_freebsd32_fstatat_args *uap) 2397 { 2398 struct stat sb; 2399 struct freebsd11_stat32 sb32; 2400 int error; 2401 2402 error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE, 2403 &sb, NULL); 2404 if (error != 0) 2405 return (error); 2406 error = freebsd11_cvtstat32(&sb, &sb32); 2407 if (error == 0) 2408 error = copyout(&sb32, uap->buf, sizeof (sb32)); 2409 return (error); 2410 } 2411 2412 int 2413 freebsd11_freebsd32_lstat(struct thread *td, 2414 struct freebsd11_freebsd32_lstat_args *uap) 2415 { 2416 struct stat sb; 2417 struct freebsd11_stat32 sb32; 2418 int error; 2419 2420 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path, 2421 UIO_USERSPACE, &sb, NULL); 2422 if (error != 0) 2423 return (error); 2424 error = freebsd11_cvtstat32(&sb, &sb32); 2425 if (error == 0) 2426 error = copyout(&sb32, uap->ub, sizeof (sb32)); 2427 return (error); 2428 } 2429 2430 int 2431 freebsd11_freebsd32_fhstat(struct thread *td, 2432 struct freebsd11_freebsd32_fhstat_args *uap) 2433 { 2434 struct stat sb; 2435 struct freebsd11_stat32 sb32; 2436 struct fhandle fh; 2437 int error; 2438 2439 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t)); 2440 if (error != 0) 2441 return (error); 2442 error = kern_fhstat(td, fh, &sb); 2443 if (error != 0) 2444 return (error); 2445 error = freebsd11_cvtstat32(&sb, &sb32); 2446 if (error == 0) 2447 error = copyout(&sb32, uap->sb, sizeof (sb32)); 2448 return (error); 2449 } 2450 #endif 2451 2452 int 2453 freebsd32___sysctl(struct thread *td, struct freebsd32___sysctl_args *uap) 2454 { 2455 int error, name[CTL_MAXNAME]; 2456 size_t j, oldlen; 2457 uint32_t tmp; 2458 2459 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2) 2460 return (EINVAL); 2461 error = copyin(uap->name, name, uap->namelen * sizeof(int)); 2462 if (error) 2463 return (error); 2464 if (uap->oldlenp) { 2465 error = fueword32(uap->oldlenp, &tmp); 2466 oldlen = tmp; 2467 } else { 2468 oldlen = 0; 2469 } 2470 if (error != 0) 2471 return (EFAULT); 2472 error = userland_sysctl(td, name, uap->namelen, 2473 uap->old, &oldlen, 1, 2474 uap->new, uap->newlen, &j, SCTL_MASK32); 2475 if (error) 2476 return (error); 2477 if (uap->oldlenp) 2478 suword32(uap->oldlenp, j); 2479 return (0); 2480 } 2481 2482 int 2483 freebsd32___sysctlbyname(struct thread *td, 2484 struct freebsd32___sysctlbyname_args *uap) 2485 { 2486 size_t oldlen, rv; 2487 int error; 2488 uint32_t tmp; 2489 2490 if (uap->oldlenp != NULL) { 2491 error = fueword32(uap->oldlenp, &tmp); 2492 oldlen = tmp; 2493 } else { 2494 error = oldlen = 0; 2495 } 2496 if (error != 0) 2497 return (EFAULT); 2498 error = kern___sysctlbyname(td, uap->name, uap->namelen, uap->old, 2499 &oldlen, uap->new, uap->newlen, &rv, SCTL_MASK32, 1); 2500 if (error != 0) 2501 return (error); 2502 if (uap->oldlenp != NULL) 2503 error = suword32(uap->oldlenp, rv); 2504 2505 return (error); 2506 } 2507 2508 int 2509 freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap) 2510 { 2511 uint32_t version; 2512 int error; 2513 struct jail j; 2514 2515 error = copyin(uap->jail, &version, sizeof(uint32_t)); 2516 if (error) 2517 return (error); 2518 2519 switch (version) { 2520 case 0: 2521 { 2522 /* FreeBSD single IPv4 jails. */ 2523 struct jail32_v0 j32_v0; 2524 2525 bzero(&j, sizeof(struct jail)); 2526 error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0)); 2527 if (error) 2528 return (error); 2529 CP(j32_v0, j, version); 2530 PTRIN_CP(j32_v0, j, path); 2531 PTRIN_CP(j32_v0, j, hostname); 2532 j.ip4s = htonl(j32_v0.ip_number); /* jail_v0 is host order */ 2533 break; 2534 } 2535 2536 case 1: 2537 /* 2538 * Version 1 was used by multi-IPv4 jail implementations 2539 * that never made it into the official kernel. 2540 */ 2541 return (EINVAL); 2542 2543 case 2: /* JAIL_API_VERSION */ 2544 { 2545 /* FreeBSD multi-IPv4/IPv6,noIP jails. */ 2546 struct jail32 j32; 2547 2548 error = copyin(uap->jail, &j32, sizeof(struct jail32)); 2549 if (error) 2550 return (error); 2551 CP(j32, j, version); 2552 PTRIN_CP(j32, j, path); 2553 PTRIN_CP(j32, j, hostname); 2554 PTRIN_CP(j32, j, jailname); 2555 CP(j32, j, ip4s); 2556 CP(j32, j, ip6s); 2557 PTRIN_CP(j32, j, ip4); 2558 PTRIN_CP(j32, j, ip6); 2559 break; 2560 } 2561 2562 default: 2563 /* Sci-Fi jails are not supported, sorry. */ 2564 return (EINVAL); 2565 } 2566 return (kern_jail(td, &j)); 2567 } 2568 2569 int 2570 freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap) 2571 { 2572 struct uio *auio; 2573 int error; 2574 2575 /* Check that we have an even number of iovecs. */ 2576 if (uap->iovcnt & 1) 2577 return (EINVAL); 2578 2579 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio); 2580 if (error) 2581 return (error); 2582 error = kern_jail_set(td, auio, uap->flags); 2583 free(auio, M_IOV); 2584 return (error); 2585 } 2586 2587 int 2588 freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap) 2589 { 2590 struct iovec32 iov32; 2591 struct uio *auio; 2592 int error, i; 2593 2594 /* Check that we have an even number of iovecs. */ 2595 if (uap->iovcnt & 1) 2596 return (EINVAL); 2597 2598 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio); 2599 if (error) 2600 return (error); 2601 error = kern_jail_get(td, auio, uap->flags); 2602 if (error == 0) 2603 for (i = 0; i < uap->iovcnt; i++) { 2604 PTROUT_CP(auio->uio_iov[i], iov32, iov_base); 2605 CP(auio->uio_iov[i], iov32, iov_len); 2606 error = copyout(&iov32, uap->iovp + i, sizeof(iov32)); 2607 if (error != 0) 2608 break; 2609 } 2610 free(auio, M_IOV); 2611 return (error); 2612 } 2613 2614 int 2615 freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap) 2616 { 2617 struct sigaction32 s32; 2618 struct sigaction sa, osa, *sap; 2619 int error; 2620 2621 if (uap->act) { 2622 error = copyin(uap->act, &s32, sizeof(s32)); 2623 if (error) 2624 return (error); 2625 sa.sa_handler = PTRIN(s32.sa_u); 2626 CP(s32, sa, sa_flags); 2627 CP(s32, sa, sa_mask); 2628 sap = &sa; 2629 } else 2630 sap = NULL; 2631 error = kern_sigaction(td, uap->sig, sap, &osa, 0); 2632 if (error == 0 && uap->oact != NULL) { 2633 s32.sa_u = PTROUT(osa.sa_handler); 2634 CP(osa, s32, sa_flags); 2635 CP(osa, s32, sa_mask); 2636 error = copyout(&s32, uap->oact, sizeof(s32)); 2637 } 2638 return (error); 2639 } 2640 2641 #ifdef COMPAT_FREEBSD4 2642 int 2643 freebsd4_freebsd32_sigaction(struct thread *td, 2644 struct freebsd4_freebsd32_sigaction_args *uap) 2645 { 2646 struct sigaction32 s32; 2647 struct sigaction sa, osa, *sap; 2648 int error; 2649 2650 if (uap->act) { 2651 error = copyin(uap->act, &s32, sizeof(s32)); 2652 if (error) 2653 return (error); 2654 sa.sa_handler = PTRIN(s32.sa_u); 2655 CP(s32, sa, sa_flags); 2656 CP(s32, sa, sa_mask); 2657 sap = &sa; 2658 } else 2659 sap = NULL; 2660 error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4); 2661 if (error == 0 && uap->oact != NULL) { 2662 s32.sa_u = PTROUT(osa.sa_handler); 2663 CP(osa, s32, sa_flags); 2664 CP(osa, s32, sa_mask); 2665 error = copyout(&s32, uap->oact, sizeof(s32)); 2666 } 2667 return (error); 2668 } 2669 #endif 2670 2671 #ifdef COMPAT_43 2672 struct osigaction32 { 2673 u_int32_t sa_u; 2674 osigset_t sa_mask; 2675 int sa_flags; 2676 }; 2677 2678 #define ONSIG 32 2679 2680 int 2681 ofreebsd32_sigaction(struct thread *td, 2682 struct ofreebsd32_sigaction_args *uap) 2683 { 2684 struct osigaction32 s32; 2685 struct sigaction sa, osa, *sap; 2686 int error; 2687 2688 if (uap->signum <= 0 || uap->signum >= ONSIG) 2689 return (EINVAL); 2690 2691 if (uap->nsa) { 2692 error = copyin(uap->nsa, &s32, sizeof(s32)); 2693 if (error) 2694 return (error); 2695 sa.sa_handler = PTRIN(s32.sa_u); 2696 CP(s32, sa, sa_flags); 2697 OSIG2SIG(s32.sa_mask, sa.sa_mask); 2698 sap = &sa; 2699 } else 2700 sap = NULL; 2701 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET); 2702 if (error == 0 && uap->osa != NULL) { 2703 s32.sa_u = PTROUT(osa.sa_handler); 2704 CP(osa, s32, sa_flags); 2705 SIG2OSIG(osa.sa_mask, s32.sa_mask); 2706 error = copyout(&s32, uap->osa, sizeof(s32)); 2707 } 2708 return (error); 2709 } 2710 2711 int 2712 ofreebsd32_sigprocmask(struct thread *td, 2713 struct ofreebsd32_sigprocmask_args *uap) 2714 { 2715 sigset_t set, oset; 2716 int error; 2717 2718 OSIG2SIG(uap->mask, set); 2719 error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD); 2720 SIG2OSIG(oset, td->td_retval[0]); 2721 return (error); 2722 } 2723 2724 int 2725 ofreebsd32_sigpending(struct thread *td, 2726 struct ofreebsd32_sigpending_args *uap) 2727 { 2728 struct proc *p = td->td_proc; 2729 sigset_t siglist; 2730 2731 PROC_LOCK(p); 2732 siglist = p->p_siglist; 2733 SIGSETOR(siglist, td->td_siglist); 2734 PROC_UNLOCK(p); 2735 SIG2OSIG(siglist, td->td_retval[0]); 2736 return (0); 2737 } 2738 2739 struct sigvec32 { 2740 u_int32_t sv_handler; 2741 int sv_mask; 2742 int sv_flags; 2743 }; 2744 2745 int 2746 ofreebsd32_sigvec(struct thread *td, 2747 struct ofreebsd32_sigvec_args *uap) 2748 { 2749 struct sigvec32 vec; 2750 struct sigaction sa, osa, *sap; 2751 int error; 2752 2753 if (uap->signum <= 0 || uap->signum >= ONSIG) 2754 return (EINVAL); 2755 2756 if (uap->nsv) { 2757 error = copyin(uap->nsv, &vec, sizeof(vec)); 2758 if (error) 2759 return (error); 2760 sa.sa_handler = PTRIN(vec.sv_handler); 2761 OSIG2SIG(vec.sv_mask, sa.sa_mask); 2762 sa.sa_flags = vec.sv_flags; 2763 sa.sa_flags ^= SA_RESTART; 2764 sap = &sa; 2765 } else 2766 sap = NULL; 2767 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET); 2768 if (error == 0 && uap->osv != NULL) { 2769 vec.sv_handler = PTROUT(osa.sa_handler); 2770 SIG2OSIG(osa.sa_mask, vec.sv_mask); 2771 vec.sv_flags = osa.sa_flags; 2772 vec.sv_flags &= ~SA_NOCLDWAIT; 2773 vec.sv_flags ^= SA_RESTART; 2774 error = copyout(&vec, uap->osv, sizeof(vec)); 2775 } 2776 return (error); 2777 } 2778 2779 int 2780 ofreebsd32_sigblock(struct thread *td, 2781 struct ofreebsd32_sigblock_args *uap) 2782 { 2783 sigset_t set, oset; 2784 2785 OSIG2SIG(uap->mask, set); 2786 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0); 2787 SIG2OSIG(oset, td->td_retval[0]); 2788 return (0); 2789 } 2790 2791 int 2792 ofreebsd32_sigsetmask(struct thread *td, 2793 struct ofreebsd32_sigsetmask_args *uap) 2794 { 2795 sigset_t set, oset; 2796 2797 OSIG2SIG(uap->mask, set); 2798 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0); 2799 SIG2OSIG(oset, td->td_retval[0]); 2800 return (0); 2801 } 2802 2803 int 2804 ofreebsd32_sigsuspend(struct thread *td, 2805 struct ofreebsd32_sigsuspend_args *uap) 2806 { 2807 sigset_t mask; 2808 2809 OSIG2SIG(uap->mask, mask); 2810 return (kern_sigsuspend(td, mask)); 2811 } 2812 2813 struct sigstack32 { 2814 u_int32_t ss_sp; 2815 int ss_onstack; 2816 }; 2817 2818 int 2819 ofreebsd32_sigstack(struct thread *td, 2820 struct ofreebsd32_sigstack_args *uap) 2821 { 2822 struct sigstack32 s32; 2823 struct sigstack nss, oss; 2824 int error = 0, unss; 2825 2826 if (uap->nss != NULL) { 2827 error = copyin(uap->nss, &s32, sizeof(s32)); 2828 if (error) 2829 return (error); 2830 nss.ss_sp = PTRIN(s32.ss_sp); 2831 CP(s32, nss, ss_onstack); 2832 unss = 1; 2833 } else { 2834 unss = 0; 2835 } 2836 oss.ss_sp = td->td_sigstk.ss_sp; 2837 oss.ss_onstack = sigonstack(cpu_getstack(td)); 2838 if (unss) { 2839 td->td_sigstk.ss_sp = nss.ss_sp; 2840 td->td_sigstk.ss_size = 0; 2841 td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK); 2842 td->td_pflags |= TDP_ALTSTACK; 2843 } 2844 if (uap->oss != NULL) { 2845 s32.ss_sp = PTROUT(oss.ss_sp); 2846 CP(oss, s32, ss_onstack); 2847 error = copyout(&s32, uap->oss, sizeof(s32)); 2848 } 2849 return (error); 2850 } 2851 #endif 2852 2853 int 2854 freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap) 2855 { 2856 2857 return (freebsd32_user_clock_nanosleep(td, CLOCK_REALTIME, 2858 TIMER_RELTIME, uap->rqtp, uap->rmtp)); 2859 } 2860 2861 int 2862 freebsd32_clock_nanosleep(struct thread *td, 2863 struct freebsd32_clock_nanosleep_args *uap) 2864 { 2865 int error; 2866 2867 error = freebsd32_user_clock_nanosleep(td, uap->clock_id, uap->flags, 2868 uap->rqtp, uap->rmtp); 2869 return (kern_posix_error(td, error)); 2870 } 2871 2872 static int 2873 freebsd32_user_clock_nanosleep(struct thread *td, clockid_t clock_id, 2874 int flags, const struct timespec32 *ua_rqtp, struct timespec32 *ua_rmtp) 2875 { 2876 struct timespec32 rmt32, rqt32; 2877 struct timespec rmt, rqt; 2878 int error, error2; 2879 2880 error = copyin(ua_rqtp, &rqt32, sizeof(rqt32)); 2881 if (error) 2882 return (error); 2883 2884 CP(rqt32, rqt, tv_sec); 2885 CP(rqt32, rqt, tv_nsec); 2886 2887 error = kern_clock_nanosleep(td, clock_id, flags, &rqt, &rmt); 2888 if (error == EINTR && ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0) { 2889 CP(rmt, rmt32, tv_sec); 2890 CP(rmt, rmt32, tv_nsec); 2891 2892 error2 = copyout(&rmt32, ua_rmtp, sizeof(rmt32)); 2893 if (error2 != 0) 2894 error = error2; 2895 } 2896 return (error); 2897 } 2898 2899 int 2900 freebsd32_clock_gettime(struct thread *td, 2901 struct freebsd32_clock_gettime_args *uap) 2902 { 2903 struct timespec ats; 2904 struct timespec32 ats32; 2905 int error; 2906 2907 error = kern_clock_gettime(td, uap->clock_id, &ats); 2908 if (error == 0) { 2909 CP(ats, ats32, tv_sec); 2910 CP(ats, ats32, tv_nsec); 2911 error = copyout(&ats32, uap->tp, sizeof(ats32)); 2912 } 2913 return (error); 2914 } 2915 2916 int 2917 freebsd32_clock_settime(struct thread *td, 2918 struct freebsd32_clock_settime_args *uap) 2919 { 2920 struct timespec ats; 2921 struct timespec32 ats32; 2922 int error; 2923 2924 error = copyin(uap->tp, &ats32, sizeof(ats32)); 2925 if (error) 2926 return (error); 2927 CP(ats32, ats, tv_sec); 2928 CP(ats32, ats, tv_nsec); 2929 2930 return (kern_clock_settime(td, uap->clock_id, &ats)); 2931 } 2932 2933 int 2934 freebsd32_clock_getres(struct thread *td, 2935 struct freebsd32_clock_getres_args *uap) 2936 { 2937 struct timespec ts; 2938 struct timespec32 ts32; 2939 int error; 2940 2941 if (uap->tp == NULL) 2942 return (0); 2943 error = kern_clock_getres(td, uap->clock_id, &ts); 2944 if (error == 0) { 2945 CP(ts, ts32, tv_sec); 2946 CP(ts, ts32, tv_nsec); 2947 error = copyout(&ts32, uap->tp, sizeof(ts32)); 2948 } 2949 return (error); 2950 } 2951 2952 int freebsd32_ktimer_create(struct thread *td, 2953 struct freebsd32_ktimer_create_args *uap) 2954 { 2955 struct sigevent32 ev32; 2956 struct sigevent ev, *evp; 2957 int error, id; 2958 2959 if (uap->evp == NULL) { 2960 evp = NULL; 2961 } else { 2962 evp = &ev; 2963 error = copyin(uap->evp, &ev32, sizeof(ev32)); 2964 if (error != 0) 2965 return (error); 2966 error = convert_sigevent32(&ev32, &ev); 2967 if (error != 0) 2968 return (error); 2969 } 2970 error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1); 2971 if (error == 0) { 2972 error = copyout(&id, uap->timerid, sizeof(int)); 2973 if (error != 0) 2974 kern_ktimer_delete(td, id); 2975 } 2976 return (error); 2977 } 2978 2979 int 2980 freebsd32_ktimer_settime(struct thread *td, 2981 struct freebsd32_ktimer_settime_args *uap) 2982 { 2983 struct itimerspec32 val32, oval32; 2984 struct itimerspec val, oval, *ovalp; 2985 int error; 2986 2987 error = copyin(uap->value, &val32, sizeof(val32)); 2988 if (error != 0) 2989 return (error); 2990 ITS_CP(val32, val); 2991 ovalp = uap->ovalue != NULL ? &oval : NULL; 2992 error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp); 2993 if (error == 0 && uap->ovalue != NULL) { 2994 ITS_CP(oval, oval32); 2995 error = copyout(&oval32, uap->ovalue, sizeof(oval32)); 2996 } 2997 return (error); 2998 } 2999 3000 int 3001 freebsd32_ktimer_gettime(struct thread *td, 3002 struct freebsd32_ktimer_gettime_args *uap) 3003 { 3004 struct itimerspec32 val32; 3005 struct itimerspec val; 3006 int error; 3007 3008 error = kern_ktimer_gettime(td, uap->timerid, &val); 3009 if (error == 0) { 3010 ITS_CP(val, val32); 3011 error = copyout(&val32, uap->value, sizeof(val32)); 3012 } 3013 return (error); 3014 } 3015 3016 int 3017 freebsd32_clock_getcpuclockid2(struct thread *td, 3018 struct freebsd32_clock_getcpuclockid2_args *uap) 3019 { 3020 clockid_t clk_id; 3021 int error; 3022 3023 error = kern_clock_getcpuclockid2(td, PAIR32TO64(id_t, uap->id), 3024 uap->which, &clk_id); 3025 if (error == 0) 3026 error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t)); 3027 return (error); 3028 } 3029 3030 int 3031 freebsd32_thr_new(struct thread *td, 3032 struct freebsd32_thr_new_args *uap) 3033 { 3034 struct thr_param32 param32; 3035 struct thr_param param; 3036 int error; 3037 3038 if (uap->param_size < 0 || 3039 uap->param_size > sizeof(struct thr_param32)) 3040 return (EINVAL); 3041 bzero(¶m, sizeof(struct thr_param)); 3042 bzero(¶m32, sizeof(struct thr_param32)); 3043 error = copyin(uap->param, ¶m32, uap->param_size); 3044 if (error != 0) 3045 return (error); 3046 param.start_func = PTRIN(param32.start_func); 3047 param.arg = PTRIN(param32.arg); 3048 param.stack_base = PTRIN(param32.stack_base); 3049 param.stack_size = param32.stack_size; 3050 param.tls_base = PTRIN(param32.tls_base); 3051 param.tls_size = param32.tls_size; 3052 param.child_tid = PTRIN(param32.child_tid); 3053 param.parent_tid = PTRIN(param32.parent_tid); 3054 param.flags = param32.flags; 3055 param.rtp = PTRIN(param32.rtp); 3056 param.spare[0] = PTRIN(param32.spare[0]); 3057 param.spare[1] = PTRIN(param32.spare[1]); 3058 param.spare[2] = PTRIN(param32.spare[2]); 3059 3060 return (kern_thr_new(td, ¶m)); 3061 } 3062 3063 int 3064 freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap) 3065 { 3066 struct timespec32 ts32; 3067 struct timespec ts, *tsp; 3068 int error; 3069 3070 error = 0; 3071 tsp = NULL; 3072 if (uap->timeout != NULL) { 3073 error = copyin((const void *)uap->timeout, (void *)&ts32, 3074 sizeof(struct timespec32)); 3075 if (error != 0) 3076 return (error); 3077 ts.tv_sec = ts32.tv_sec; 3078 ts.tv_nsec = ts32.tv_nsec; 3079 tsp = &ts; 3080 } 3081 return (kern_thr_suspend(td, tsp)); 3082 } 3083 3084 void 3085 siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst) 3086 { 3087 bzero(dst, sizeof(*dst)); 3088 dst->si_signo = src->si_signo; 3089 dst->si_errno = src->si_errno; 3090 dst->si_code = src->si_code; 3091 dst->si_pid = src->si_pid; 3092 dst->si_uid = src->si_uid; 3093 dst->si_status = src->si_status; 3094 dst->si_addr = (uintptr_t)src->si_addr; 3095 dst->si_value.sival_int = src->si_value.sival_int; 3096 dst->si_timerid = src->si_timerid; 3097 dst->si_overrun = src->si_overrun; 3098 } 3099 3100 #ifndef _FREEBSD32_SYSPROTO_H_ 3101 struct freebsd32_sigqueue_args { 3102 pid_t pid; 3103 int signum; 3104 /* union sigval32 */ int value; 3105 }; 3106 #endif 3107 int 3108 freebsd32_sigqueue(struct thread *td, struct freebsd32_sigqueue_args *uap) 3109 { 3110 union sigval sv; 3111 3112 /* 3113 * On 32-bit ABIs, sival_int and sival_ptr are the same. 3114 * On 64-bit little-endian ABIs, the low bits are the same. 3115 * In 64-bit big-endian ABIs, sival_int overlaps with 3116 * sival_ptr's HIGH bits. We choose to support sival_int 3117 * rather than sival_ptr in this case as it seems to be 3118 * more common. 3119 */ 3120 bzero(&sv, sizeof(sv)); 3121 sv.sival_int = uap->value; 3122 3123 return (kern_sigqueue(td, uap->pid, uap->signum, &sv)); 3124 } 3125 3126 int 3127 freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap) 3128 { 3129 struct timespec32 ts32; 3130 struct timespec ts; 3131 struct timespec *timeout; 3132 sigset_t set; 3133 ksiginfo_t ksi; 3134 struct siginfo32 si32; 3135 int error; 3136 3137 if (uap->timeout) { 3138 error = copyin(uap->timeout, &ts32, sizeof(ts32)); 3139 if (error) 3140 return (error); 3141 ts.tv_sec = ts32.tv_sec; 3142 ts.tv_nsec = ts32.tv_nsec; 3143 timeout = &ts; 3144 } else 3145 timeout = NULL; 3146 3147 error = copyin(uap->set, &set, sizeof(set)); 3148 if (error) 3149 return (error); 3150 3151 error = kern_sigtimedwait(td, set, &ksi, timeout); 3152 if (error) 3153 return (error); 3154 3155 if (uap->info) { 3156 siginfo_to_siginfo32(&ksi.ksi_info, &si32); 3157 error = copyout(&si32, uap->info, sizeof(struct siginfo32)); 3158 } 3159 3160 if (error == 0) 3161 td->td_retval[0] = ksi.ksi_signo; 3162 return (error); 3163 } 3164 3165 /* 3166 * MPSAFE 3167 */ 3168 int 3169 freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap) 3170 { 3171 ksiginfo_t ksi; 3172 struct siginfo32 si32; 3173 sigset_t set; 3174 int error; 3175 3176 error = copyin(uap->set, &set, sizeof(set)); 3177 if (error) 3178 return (error); 3179 3180 error = kern_sigtimedwait(td, set, &ksi, NULL); 3181 if (error) 3182 return (error); 3183 3184 if (uap->info) { 3185 siginfo_to_siginfo32(&ksi.ksi_info, &si32); 3186 error = copyout(&si32, uap->info, sizeof(struct siginfo32)); 3187 } 3188 if (error == 0) 3189 td->td_retval[0] = ksi.ksi_signo; 3190 return (error); 3191 } 3192 3193 int 3194 freebsd32_cpuset_setid(struct thread *td, 3195 struct freebsd32_cpuset_setid_args *uap) 3196 { 3197 3198 return (kern_cpuset_setid(td, uap->which, 3199 PAIR32TO64(id_t, uap->id), uap->setid)); 3200 } 3201 3202 int 3203 freebsd32_cpuset_getid(struct thread *td, 3204 struct freebsd32_cpuset_getid_args *uap) 3205 { 3206 3207 return (kern_cpuset_getid(td, uap->level, uap->which, 3208 PAIR32TO64(id_t, uap->id), uap->setid)); 3209 } 3210 3211 int 3212 freebsd32_cpuset_getaffinity(struct thread *td, 3213 struct freebsd32_cpuset_getaffinity_args *uap) 3214 { 3215 3216 return (kern_cpuset_getaffinity(td, uap->level, uap->which, 3217 PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask)); 3218 } 3219 3220 int 3221 freebsd32_cpuset_setaffinity(struct thread *td, 3222 struct freebsd32_cpuset_setaffinity_args *uap) 3223 { 3224 3225 return (kern_cpuset_setaffinity(td, uap->level, uap->which, 3226 PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask)); 3227 } 3228 3229 int 3230 freebsd32_cpuset_getdomain(struct thread *td, 3231 struct freebsd32_cpuset_getdomain_args *uap) 3232 { 3233 3234 return (kern_cpuset_getdomain(td, uap->level, uap->which, 3235 PAIR32TO64(id_t,uap->id), uap->domainsetsize, uap->mask, uap->policy)); 3236 } 3237 3238 int 3239 freebsd32_cpuset_setdomain(struct thread *td, 3240 struct freebsd32_cpuset_setdomain_args *uap) 3241 { 3242 3243 return (kern_cpuset_setdomain(td, uap->level, uap->which, 3244 PAIR32TO64(id_t,uap->id), uap->domainsetsize, uap->mask, uap->policy)); 3245 } 3246 3247 int 3248 freebsd32_nmount(struct thread *td, 3249 struct freebsd32_nmount_args /* { 3250 struct iovec *iovp; 3251 unsigned int iovcnt; 3252 int flags; 3253 } */ *uap) 3254 { 3255 struct uio *auio; 3256 uint64_t flags; 3257 int error; 3258 3259 /* 3260 * Mount flags are now 64-bits. On 32-bit archtectures only 3261 * 32-bits are passed in, but from here on everything handles 3262 * 64-bit flags correctly. 3263 */ 3264 flags = uap->flags; 3265 3266 AUDIT_ARG_FFLAGS(flags); 3267 3268 /* 3269 * Filter out MNT_ROOTFS. We do not want clients of nmount() in 3270 * userspace to set this flag, but we must filter it out if we want 3271 * MNT_UPDATE on the root file system to work. 3272 * MNT_ROOTFS should only be set by the kernel when mounting its 3273 * root file system. 3274 */ 3275 flags &= ~MNT_ROOTFS; 3276 3277 /* 3278 * check that we have an even number of iovec's 3279 * and that we have at least two options. 3280 */ 3281 if ((uap->iovcnt & 1) || (uap->iovcnt < 4)) 3282 return (EINVAL); 3283 3284 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio); 3285 if (error) 3286 return (error); 3287 error = vfs_donmount(td, flags, auio); 3288 3289 free(auio, M_IOV); 3290 return error; 3291 } 3292 3293 #if 0 3294 int 3295 freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap) 3296 { 3297 struct yyy32 *p32, s32; 3298 struct yyy *p = NULL, s; 3299 struct xxx_arg ap; 3300 int error; 3301 3302 if (uap->zzz) { 3303 error = copyin(uap->zzz, &s32, sizeof(s32)); 3304 if (error) 3305 return (error); 3306 /* translate in */ 3307 p = &s; 3308 } 3309 error = kern_xxx(td, p); 3310 if (error) 3311 return (error); 3312 if (uap->zzz) { 3313 /* translate out */ 3314 error = copyout(&s32, p32, sizeof(s32)); 3315 } 3316 return (error); 3317 } 3318 #endif 3319 3320 int 3321 syscall32_module_handler(struct module *mod, int what, void *arg) 3322 { 3323 3324 return (kern_syscall_module_handler(freebsd32_sysent, mod, what, arg)); 3325 } 3326 3327 int 3328 syscall32_helper_register(struct syscall_helper_data *sd, int flags) 3329 { 3330 3331 return (kern_syscall_helper_register(freebsd32_sysent, sd, flags)); 3332 } 3333 3334 int 3335 syscall32_helper_unregister(struct syscall_helper_data *sd) 3336 { 3337 3338 return (kern_syscall_helper_unregister(freebsd32_sysent, sd)); 3339 } 3340 3341 int 3342 freebsd32_copyout_strings(struct image_params *imgp, uintptr_t *stack_base) 3343 { 3344 int argc, envc, i; 3345 u_int32_t *vectp; 3346 char *stringp; 3347 uintptr_t destp, ustringp; 3348 struct freebsd32_ps_strings *arginfo; 3349 char canary[sizeof(long) * 8]; 3350 int32_t pagesizes32[MAXPAGESIZES]; 3351 size_t execpath_len; 3352 int error, szsigcode; 3353 3354 /* 3355 * Calculate string base and vector table pointers. 3356 * Also deal with signal trampoline code for this exec type. 3357 */ 3358 if (imgp->execpath != NULL && imgp->auxargs != NULL) 3359 execpath_len = strlen(imgp->execpath) + 1; 3360 else 3361 execpath_len = 0; 3362 arginfo = (struct freebsd32_ps_strings *)curproc->p_sysent-> 3363 sv_psstrings; 3364 imgp->ps_strings = arginfo; 3365 if (imgp->proc->p_sysent->sv_sigcode_base == 0) 3366 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode); 3367 else 3368 szsigcode = 0; 3369 destp = (uintptr_t)arginfo; 3370 3371 /* 3372 * install sigcode 3373 */ 3374 if (szsigcode != 0) { 3375 destp -= szsigcode; 3376 destp = rounddown2(destp, sizeof(uint32_t)); 3377 error = copyout(imgp->proc->p_sysent->sv_sigcode, (void *)destp, 3378 szsigcode); 3379 if (error != 0) 3380 return (error); 3381 } 3382 3383 /* 3384 * Copy the image path for the rtld. 3385 */ 3386 if (execpath_len != 0) { 3387 destp -= execpath_len; 3388 imgp->execpathp = (void *)destp; 3389 error = copyout(imgp->execpath, imgp->execpathp, execpath_len); 3390 if (error != 0) 3391 return (error); 3392 } 3393 3394 /* 3395 * Prepare the canary for SSP. 3396 */ 3397 arc4rand(canary, sizeof(canary), 0); 3398 destp -= sizeof(canary); 3399 imgp->canary = (void *)destp; 3400 error = copyout(canary, imgp->canary, sizeof(canary)); 3401 if (error != 0) 3402 return (error); 3403 imgp->canarylen = sizeof(canary); 3404 3405 /* 3406 * Prepare the pagesizes array. 3407 */ 3408 for (i = 0; i < MAXPAGESIZES; i++) 3409 pagesizes32[i] = (uint32_t)pagesizes[i]; 3410 destp -= sizeof(pagesizes32); 3411 destp = rounddown2(destp, sizeof(uint32_t)); 3412 imgp->pagesizes = (void *)destp; 3413 error = copyout(pagesizes32, imgp->pagesizes, sizeof(pagesizes32)); 3414 if (error != 0) 3415 return (error); 3416 imgp->pagesizeslen = sizeof(pagesizes32); 3417 3418 /* 3419 * Allocate room for the argument and environment strings. 3420 */ 3421 destp -= ARG_MAX - imgp->args->stringspace; 3422 destp = rounddown2(destp, sizeof(uint32_t)); 3423 ustringp = destp; 3424 3425 if (imgp->sysent->sv_stackgap != NULL) 3426 imgp->sysent->sv_stackgap(imgp, &destp); 3427 3428 if (imgp->auxargs) { 3429 /* 3430 * Allocate room on the stack for the ELF auxargs 3431 * array. It has up to AT_COUNT entries. 3432 */ 3433 destp -= AT_COUNT * sizeof(Elf32_Auxinfo); 3434 destp = rounddown2(destp, sizeof(uint32_t)); 3435 } 3436 3437 vectp = (uint32_t *)destp; 3438 3439 /* 3440 * Allocate room for the argv[] and env vectors including the 3441 * terminating NULL pointers. 3442 */ 3443 vectp -= imgp->args->argc + 1 + imgp->args->envc + 1; 3444 3445 /* 3446 * vectp also becomes our initial stack base 3447 */ 3448 *stack_base = (uintptr_t)vectp; 3449 3450 stringp = imgp->args->begin_argv; 3451 argc = imgp->args->argc; 3452 envc = imgp->args->envc; 3453 /* 3454 * Copy out strings - arguments and environment. 3455 */ 3456 error = copyout(stringp, (void *)ustringp, 3457 ARG_MAX - imgp->args->stringspace); 3458 if (error != 0) 3459 return (error); 3460 3461 /* 3462 * Fill in "ps_strings" struct for ps, w, etc. 3463 */ 3464 imgp->argv = vectp; 3465 if (suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp) != 0 || 3466 suword32(&arginfo->ps_nargvstr, argc) != 0) 3467 return (EFAULT); 3468 3469 /* 3470 * Fill in argument portion of vector table. 3471 */ 3472 for (; argc > 0; --argc) { 3473 if (suword32(vectp++, ustringp) != 0) 3474 return (EFAULT); 3475 while (*stringp++ != 0) 3476 ustringp++; 3477 ustringp++; 3478 } 3479 3480 /* a null vector table pointer separates the argp's from the envp's */ 3481 if (suword32(vectp++, 0) != 0) 3482 return (EFAULT); 3483 3484 imgp->envv = vectp; 3485 if (suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp) != 0 || 3486 suword32(&arginfo->ps_nenvstr, envc) != 0) 3487 return (EFAULT); 3488 3489 /* 3490 * Fill in environment portion of vector table. 3491 */ 3492 for (; envc > 0; --envc) { 3493 if (suword32(vectp++, ustringp) != 0) 3494 return (EFAULT); 3495 while (*stringp++ != 0) 3496 ustringp++; 3497 ustringp++; 3498 } 3499 3500 /* end of vector table is a null pointer */ 3501 if (suword32(vectp, 0) != 0) 3502 return (EFAULT); 3503 3504 if (imgp->auxargs) { 3505 vectp++; 3506 error = imgp->sysent->sv_copyout_auxargs(imgp, 3507 (uintptr_t)vectp); 3508 if (error != 0) 3509 return (error); 3510 } 3511 3512 return (0); 3513 } 3514 3515 int 3516 freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap) 3517 { 3518 struct kld_file_stat *stat; 3519 struct kld32_file_stat *stat32; 3520 int error, version; 3521 3522 if ((error = copyin(&uap->stat->version, &version, sizeof(version))) 3523 != 0) 3524 return (error); 3525 if (version != sizeof(struct kld32_file_stat_1) && 3526 version != sizeof(struct kld32_file_stat)) 3527 return (EINVAL); 3528 3529 stat = malloc(sizeof(*stat), M_TEMP, M_WAITOK | M_ZERO); 3530 stat32 = malloc(sizeof(*stat32), M_TEMP, M_WAITOK | M_ZERO); 3531 error = kern_kldstat(td, uap->fileid, stat); 3532 if (error == 0) { 3533 bcopy(&stat->name[0], &stat32->name[0], sizeof(stat->name)); 3534 CP(*stat, *stat32, refs); 3535 CP(*stat, *stat32, id); 3536 PTROUT_CP(*stat, *stat32, address); 3537 CP(*stat, *stat32, size); 3538 bcopy(&stat->pathname[0], &stat32->pathname[0], 3539 sizeof(stat->pathname)); 3540 stat32->version = version; 3541 error = copyout(stat32, uap->stat, version); 3542 } 3543 free(stat, M_TEMP); 3544 free(stat32, M_TEMP); 3545 return (error); 3546 } 3547 3548 int 3549 freebsd32_posix_fallocate(struct thread *td, 3550 struct freebsd32_posix_fallocate_args *uap) 3551 { 3552 int error; 3553 3554 error = kern_posix_fallocate(td, uap->fd, 3555 PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len)); 3556 return (kern_posix_error(td, error)); 3557 } 3558 3559 int 3560 freebsd32_posix_fadvise(struct thread *td, 3561 struct freebsd32_posix_fadvise_args *uap) 3562 { 3563 int error; 3564 3565 error = kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset), 3566 PAIR32TO64(off_t, uap->len), uap->advice); 3567 return (kern_posix_error(td, error)); 3568 } 3569 3570 int 3571 convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig) 3572 { 3573 3574 CP(*sig32, *sig, sigev_notify); 3575 switch (sig->sigev_notify) { 3576 case SIGEV_NONE: 3577 break; 3578 case SIGEV_THREAD_ID: 3579 CP(*sig32, *sig, sigev_notify_thread_id); 3580 /* FALLTHROUGH */ 3581 case SIGEV_SIGNAL: 3582 CP(*sig32, *sig, sigev_signo); 3583 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr); 3584 break; 3585 case SIGEV_KEVENT: 3586 CP(*sig32, *sig, sigev_notify_kqueue); 3587 CP(*sig32, *sig, sigev_notify_kevent_flags); 3588 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr); 3589 break; 3590 default: 3591 return (EINVAL); 3592 } 3593 return (0); 3594 } 3595 3596 int 3597 freebsd32_procctl(struct thread *td, struct freebsd32_procctl_args *uap) 3598 { 3599 void *data; 3600 union { 3601 struct procctl_reaper_status rs; 3602 struct procctl_reaper_pids rp; 3603 struct procctl_reaper_kill rk; 3604 } x; 3605 union { 3606 struct procctl_reaper_pids32 rp; 3607 } x32; 3608 int error, error1, flags, signum; 3609 3610 if (uap->com >= PROC_PROCCTL_MD_MIN) 3611 return (cpu_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id), 3612 uap->com, PTRIN(uap->data))); 3613 3614 switch (uap->com) { 3615 case PROC_ASLR_CTL: 3616 case PROC_PROTMAX_CTL: 3617 case PROC_SPROTECT: 3618 case PROC_STACKGAP_CTL: 3619 case PROC_TRACE_CTL: 3620 case PROC_TRAPCAP_CTL: 3621 error = copyin(PTRIN(uap->data), &flags, sizeof(flags)); 3622 if (error != 0) 3623 return (error); 3624 data = &flags; 3625 break; 3626 case PROC_REAP_ACQUIRE: 3627 case PROC_REAP_RELEASE: 3628 if (uap->data != NULL) 3629 return (EINVAL); 3630 data = NULL; 3631 break; 3632 case PROC_REAP_STATUS: 3633 data = &x.rs; 3634 break; 3635 case PROC_REAP_GETPIDS: 3636 error = copyin(uap->data, &x32.rp, sizeof(x32.rp)); 3637 if (error != 0) 3638 return (error); 3639 CP(x32.rp, x.rp, rp_count); 3640 PTRIN_CP(x32.rp, x.rp, rp_pids); 3641 data = &x.rp; 3642 break; 3643 case PROC_REAP_KILL: 3644 error = copyin(uap->data, &x.rk, sizeof(x.rk)); 3645 if (error != 0) 3646 return (error); 3647 data = &x.rk; 3648 break; 3649 case PROC_ASLR_STATUS: 3650 case PROC_PROTMAX_STATUS: 3651 case PROC_STACKGAP_STATUS: 3652 case PROC_TRACE_STATUS: 3653 case PROC_TRAPCAP_STATUS: 3654 data = &flags; 3655 break; 3656 case PROC_PDEATHSIG_CTL: 3657 error = copyin(uap->data, &signum, sizeof(signum)); 3658 if (error != 0) 3659 return (error); 3660 data = &signum; 3661 break; 3662 case PROC_PDEATHSIG_STATUS: 3663 data = &signum; 3664 break; 3665 default: 3666 return (EINVAL); 3667 } 3668 error = kern_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id), 3669 uap->com, data); 3670 switch (uap->com) { 3671 case PROC_REAP_STATUS: 3672 if (error == 0) 3673 error = copyout(&x.rs, uap->data, sizeof(x.rs)); 3674 break; 3675 case PROC_REAP_KILL: 3676 error1 = copyout(&x.rk, uap->data, sizeof(x.rk)); 3677 if (error == 0) 3678 error = error1; 3679 break; 3680 case PROC_ASLR_STATUS: 3681 case PROC_PROTMAX_STATUS: 3682 case PROC_STACKGAP_STATUS: 3683 case PROC_TRACE_STATUS: 3684 case PROC_TRAPCAP_STATUS: 3685 if (error == 0) 3686 error = copyout(&flags, uap->data, sizeof(flags)); 3687 break; 3688 case PROC_PDEATHSIG_STATUS: 3689 if (error == 0) 3690 error = copyout(&signum, uap->data, sizeof(signum)); 3691 break; 3692 } 3693 return (error); 3694 } 3695 3696 int 3697 freebsd32_fcntl(struct thread *td, struct freebsd32_fcntl_args *uap) 3698 { 3699 long tmp; 3700 3701 switch (uap->cmd) { 3702 /* 3703 * Do unsigned conversion for arg when operation 3704 * interprets it as flags or pointer. 3705 */ 3706 case F_SETLK_REMOTE: 3707 case F_SETLKW: 3708 case F_SETLK: 3709 case F_GETLK: 3710 case F_SETFD: 3711 case F_SETFL: 3712 case F_OGETLK: 3713 case F_OSETLK: 3714 case F_OSETLKW: 3715 tmp = (unsigned int)(uap->arg); 3716 break; 3717 default: 3718 tmp = uap->arg; 3719 break; 3720 } 3721 return (kern_fcntl_freebsd(td, uap->fd, uap->cmd, tmp)); 3722 } 3723 3724 int 3725 freebsd32_ppoll(struct thread *td, struct freebsd32_ppoll_args *uap) 3726 { 3727 struct timespec32 ts32; 3728 struct timespec ts, *tsp; 3729 sigset_t set, *ssp; 3730 int error; 3731 3732 if (uap->ts != NULL) { 3733 error = copyin(uap->ts, &ts32, sizeof(ts32)); 3734 if (error != 0) 3735 return (error); 3736 CP(ts32, ts, tv_sec); 3737 CP(ts32, ts, tv_nsec); 3738 tsp = &ts; 3739 } else 3740 tsp = NULL; 3741 if (uap->set != NULL) { 3742 error = copyin(uap->set, &set, sizeof(set)); 3743 if (error != 0) 3744 return (error); 3745 ssp = &set; 3746 } else 3747 ssp = NULL; 3748 3749 return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp)); 3750 } 3751 3752 int 3753 freebsd32_sched_rr_get_interval(struct thread *td, 3754 struct freebsd32_sched_rr_get_interval_args *uap) 3755 { 3756 struct timespec ts; 3757 struct timespec32 ts32; 3758 int error; 3759 3760 error = kern_sched_rr_get_interval(td, uap->pid, &ts); 3761 if (error == 0) { 3762 CP(ts, ts32, tv_sec); 3763 CP(ts, ts32, tv_nsec); 3764 error = copyout(&ts32, uap->interval, sizeof(ts32)); 3765 } 3766 return (error); 3767 } 3768