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