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