freebsd32_misc.c revision 338618
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 338618 2018-09-12 19:13:32Z markj $"); 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) 728 return (error); 729 if (uap->rusage != NULL) { 730 freebsd32_rusage_out(&s, &s32); 731 error = copyout(&s32, uap->rusage, sizeof(s32)); 732 } 733 return (error); 734} 735 736static int 737freebsd32_copyinuio(struct iovec32 *iovp, u_int iovcnt, struct uio **uiop) 738{ 739 struct iovec32 iov32; 740 struct iovec *iov; 741 struct uio *uio; 742 u_int iovlen; 743 int error, i; 744 745 *uiop = NULL; 746 if (iovcnt > UIO_MAXIOV) 747 return (EINVAL); 748 iovlen = iovcnt * sizeof(struct iovec); 749 uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK); 750 iov = (struct iovec *)(uio + 1); 751 for (i = 0; i < iovcnt; i++) { 752 error = copyin(&iovp[i], &iov32, sizeof(struct iovec32)); 753 if (error) { 754 free(uio, M_IOV); 755 return (error); 756 } 757 iov[i].iov_base = PTRIN(iov32.iov_base); 758 iov[i].iov_len = iov32.iov_len; 759 } 760 uio->uio_iov = iov; 761 uio->uio_iovcnt = iovcnt; 762 uio->uio_segflg = UIO_USERSPACE; 763 uio->uio_offset = -1; 764 uio->uio_resid = 0; 765 for (i = 0; i < iovcnt; i++) { 766 if (iov->iov_len > INT_MAX - uio->uio_resid) { 767 free(uio, M_IOV); 768 return (EINVAL); 769 } 770 uio->uio_resid += iov->iov_len; 771 iov++; 772 } 773 *uiop = uio; 774 return (0); 775} 776 777int 778freebsd32_readv(struct thread *td, struct freebsd32_readv_args *uap) 779{ 780 struct uio *auio; 781 int error; 782 783 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio); 784 if (error) 785 return (error); 786 error = kern_readv(td, uap->fd, auio); 787 free(auio, M_IOV); 788 return (error); 789} 790 791int 792freebsd32_writev(struct thread *td, struct freebsd32_writev_args *uap) 793{ 794 struct uio *auio; 795 int error; 796 797 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio); 798 if (error) 799 return (error); 800 error = kern_writev(td, uap->fd, auio); 801 free(auio, M_IOV); 802 return (error); 803} 804 805int 806freebsd32_preadv(struct thread *td, struct freebsd32_preadv_args *uap) 807{ 808 struct uio *auio; 809 int error; 810 811 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio); 812 if (error) 813 return (error); 814 error = kern_preadv(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset)); 815 free(auio, M_IOV); 816 return (error); 817} 818 819int 820freebsd32_pwritev(struct thread *td, struct freebsd32_pwritev_args *uap) 821{ 822 struct uio *auio; 823 int error; 824 825 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio); 826 if (error) 827 return (error); 828 error = kern_pwritev(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset)); 829 free(auio, M_IOV); 830 return (error); 831} 832 833int 834freebsd32_copyiniov(struct iovec32 *iovp32, u_int iovcnt, struct iovec **iovp, 835 int error) 836{ 837 struct iovec32 iov32; 838 struct iovec *iov; 839 u_int iovlen; 840 int i; 841 842 *iovp = NULL; 843 if (iovcnt > UIO_MAXIOV) 844 return (error); 845 iovlen = iovcnt * sizeof(struct iovec); 846 iov = malloc(iovlen, M_IOV, M_WAITOK); 847 for (i = 0; i < iovcnt; i++) { 848 error = copyin(&iovp32[i], &iov32, sizeof(struct iovec32)); 849 if (error) { 850 free(iov, M_IOV); 851 return (error); 852 } 853 iov[i].iov_base = PTRIN(iov32.iov_base); 854 iov[i].iov_len = iov32.iov_len; 855 } 856 *iovp = iov; 857 return (0); 858} 859 860static int 861freebsd32_copyinmsghdr(struct msghdr32 *msg32, struct msghdr *msg) 862{ 863 struct msghdr32 m32; 864 int error; 865 866 error = copyin(msg32, &m32, sizeof(m32)); 867 if (error) 868 return (error); 869 msg->msg_name = PTRIN(m32.msg_name); 870 msg->msg_namelen = m32.msg_namelen; 871 msg->msg_iov = PTRIN(m32.msg_iov); 872 msg->msg_iovlen = m32.msg_iovlen; 873 msg->msg_control = PTRIN(m32.msg_control); 874 msg->msg_controllen = m32.msg_controllen; 875 msg->msg_flags = m32.msg_flags; 876 return (0); 877} 878 879static int 880freebsd32_copyoutmsghdr(struct msghdr *msg, struct msghdr32 *msg32) 881{ 882 struct msghdr32 m32; 883 int error; 884 885 m32.msg_name = PTROUT(msg->msg_name); 886 m32.msg_namelen = msg->msg_namelen; 887 m32.msg_iov = PTROUT(msg->msg_iov); 888 m32.msg_iovlen = msg->msg_iovlen; 889 m32.msg_control = PTROUT(msg->msg_control); 890 m32.msg_controllen = msg->msg_controllen; 891 m32.msg_flags = msg->msg_flags; 892 error = copyout(&m32, msg32, sizeof(m32)); 893 return (error); 894} 895 896#ifndef __mips__ 897#define FREEBSD32_ALIGNBYTES (sizeof(int) - 1) 898#else 899#define FREEBSD32_ALIGNBYTES (sizeof(long) - 1) 900#endif 901#define FREEBSD32_ALIGN(p) \ 902 (((u_long)(p) + FREEBSD32_ALIGNBYTES) & ~FREEBSD32_ALIGNBYTES) 903#define FREEBSD32_CMSG_SPACE(l) \ 904 (FREEBSD32_ALIGN(sizeof(struct cmsghdr)) + FREEBSD32_ALIGN(l)) 905 906#define FREEBSD32_CMSG_DATA(cmsg) ((unsigned char *)(cmsg) + \ 907 FREEBSD32_ALIGN(sizeof(struct cmsghdr))) 908 909static size_t 910freebsd32_cmsg_convert(const struct cmsghdr *cm, void *data, socklen_t datalen) 911{ 912 size_t copylen; 913 union { 914 struct timespec32 ts; 915 struct timeval32 tv; 916 struct bintime32 bt; 917 } tmp32; 918 919 union { 920 struct timespec ts; 921 struct timeval tv; 922 struct bintime bt; 923 } *in; 924 925 in = data; 926 copylen = 0; 927 switch (cm->cmsg_level) { 928 case SOL_SOCKET: 929 switch (cm->cmsg_type) { 930 case SCM_TIMESTAMP: 931 TV_CP(*in, tmp32, tv); 932 copylen = sizeof(tmp32.tv); 933 break; 934 935 case SCM_BINTIME: 936 BT_CP(*in, tmp32, bt); 937 copylen = sizeof(tmp32.bt); 938 break; 939 940 case SCM_REALTIME: 941 case SCM_MONOTONIC: 942 TS_CP(*in, tmp32, ts); 943 copylen = sizeof(tmp32.ts); 944 break; 945 946 default: 947 break; 948 } 949 950 default: 951 break; 952 } 953 954 if (copylen == 0) 955 return (datalen); 956 957 KASSERT((datalen >= copylen), ("corrupted cmsghdr")); 958 959 bcopy(&tmp32, data, copylen); 960 return (copylen); 961} 962 963static int 964freebsd32_copy_msg_out(struct msghdr *msg, struct mbuf *control) 965{ 966 struct cmsghdr *cm; 967 void *data; 968 socklen_t clen, datalen, datalen_out, oldclen; 969 int error; 970 caddr_t ctlbuf; 971 int len, maxlen, copylen; 972 struct mbuf *m; 973 error = 0; 974 975 len = msg->msg_controllen; 976 maxlen = msg->msg_controllen; 977 msg->msg_controllen = 0; 978 979 ctlbuf = msg->msg_control; 980 for (m = control; m != NULL && len > 0; m = m->m_next) { 981 cm = mtod(m, struct cmsghdr *); 982 clen = m->m_len; 983 while (cm != NULL) { 984 if (sizeof(struct cmsghdr) > clen || 985 cm->cmsg_len > clen) { 986 error = EINVAL; 987 break; 988 } 989 990 data = CMSG_DATA(cm); 991 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 992 datalen_out = freebsd32_cmsg_convert(cm, data, datalen); 993 994 /* 995 * Copy out the message header. Preserve the native 996 * message size in case we need to inspect the message 997 * contents later. 998 */ 999 copylen = sizeof(struct cmsghdr); 1000 if (len < copylen) { 1001 msg->msg_flags |= MSG_CTRUNC; 1002 m_dispose_extcontrolm(m); 1003 goto exit; 1004 } 1005 oldclen = cm->cmsg_len; 1006 cm->cmsg_len = FREEBSD32_ALIGN(sizeof(struct cmsghdr)) + 1007 datalen_out; 1008 error = copyout(cm, ctlbuf, copylen); 1009 cm->cmsg_len = oldclen; 1010 if (error != 0) 1011 goto exit; 1012 1013 ctlbuf += FREEBSD32_ALIGN(copylen); 1014 len -= FREEBSD32_ALIGN(copylen); 1015 1016 copylen = datalen_out; 1017 if (len < copylen) { 1018 msg->msg_flags |= MSG_CTRUNC; 1019 m_dispose_extcontrolm(m); 1020 break; 1021 } 1022 1023 /* Copy out the message data. */ 1024 error = copyout(data, ctlbuf, copylen); 1025 if (error) 1026 goto exit; 1027 1028 ctlbuf += FREEBSD32_ALIGN(copylen); 1029 len -= FREEBSD32_ALIGN(copylen); 1030 1031 if (CMSG_SPACE(datalen) < clen) { 1032 clen -= CMSG_SPACE(datalen); 1033 cm = (struct cmsghdr *) 1034 ((caddr_t)cm + CMSG_SPACE(datalen)); 1035 } else { 1036 clen = 0; 1037 cm = NULL; 1038 } 1039 1040 msg->msg_controllen += FREEBSD32_ALIGN(sizeof(*cm)) + 1041 datalen_out; 1042 } 1043 } 1044 if (len == 0 && m != NULL) { 1045 msg->msg_flags |= MSG_CTRUNC; 1046 m_dispose_extcontrolm(m); 1047 } 1048 1049exit: 1050 return (error); 1051} 1052 1053int 1054freebsd32_recvmsg(td, uap) 1055 struct thread *td; 1056 struct freebsd32_recvmsg_args /* { 1057 int s; 1058 struct msghdr32 *msg; 1059 int flags; 1060 } */ *uap; 1061{ 1062 struct msghdr msg; 1063 struct msghdr32 m32; 1064 struct iovec *uiov, *iov; 1065 struct mbuf *control = NULL; 1066 struct mbuf **controlp; 1067 1068 int error; 1069 error = copyin(uap->msg, &m32, sizeof(m32)); 1070 if (error) 1071 return (error); 1072 error = freebsd32_copyinmsghdr(uap->msg, &msg); 1073 if (error) 1074 return (error); 1075 error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov, 1076 EMSGSIZE); 1077 if (error) 1078 return (error); 1079 msg.msg_flags = uap->flags; 1080 uiov = msg.msg_iov; 1081 msg.msg_iov = iov; 1082 1083 controlp = (msg.msg_control != NULL) ? &control : NULL; 1084 error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, controlp); 1085 if (error == 0) { 1086 msg.msg_iov = uiov; 1087 1088 if (control != NULL) 1089 error = freebsd32_copy_msg_out(&msg, control); 1090 else 1091 msg.msg_controllen = 0; 1092 1093 if (error == 0) 1094 error = freebsd32_copyoutmsghdr(&msg, uap->msg); 1095 } 1096 free(iov, M_IOV); 1097 1098 if (control != NULL) { 1099 if (error != 0) 1100 m_dispose_extcontrolm(control); 1101 m_freem(control); 1102 } 1103 1104 return (error); 1105} 1106 1107/* 1108 * Copy-in the array of control messages constructed using alignment 1109 * and padding suitable for a 32-bit environment and construct an 1110 * mbuf using alignment and padding suitable for a 64-bit kernel. 1111 * The alignment and padding are defined indirectly by CMSG_DATA(), 1112 * CMSG_SPACE() and CMSG_LEN(). 1113 */ 1114static int 1115freebsd32_copyin_control(struct mbuf **mp, caddr_t buf, u_int buflen) 1116{ 1117 struct mbuf *m; 1118 void *md; 1119 u_int idx, len, msglen; 1120 int error; 1121 1122 buflen = FREEBSD32_ALIGN(buflen); 1123 1124 if (buflen > MCLBYTES) 1125 return (EINVAL); 1126 1127 /* 1128 * Iterate over the buffer and get the length of each message 1129 * in there. This has 32-bit alignment and padding. Use it to 1130 * determine the length of these messages when using 64-bit 1131 * alignment and padding. 1132 */ 1133 idx = 0; 1134 len = 0; 1135 while (idx < buflen) { 1136 error = copyin(buf + idx, &msglen, sizeof(msglen)); 1137 if (error) 1138 return (error); 1139 if (msglen < sizeof(struct cmsghdr)) 1140 return (EINVAL); 1141 msglen = FREEBSD32_ALIGN(msglen); 1142 if (idx + msglen > buflen) 1143 return (EINVAL); 1144 idx += msglen; 1145 msglen += CMSG_ALIGN(sizeof(struct cmsghdr)) - 1146 FREEBSD32_ALIGN(sizeof(struct cmsghdr)); 1147 len += CMSG_ALIGN(msglen); 1148 } 1149 1150 if (len > MCLBYTES) 1151 return (EINVAL); 1152 1153 m = m_get(M_WAITOK, MT_CONTROL); 1154 if (len > MLEN) 1155 MCLGET(m, M_WAITOK); 1156 m->m_len = len; 1157 1158 md = mtod(m, void *); 1159 while (buflen > 0) { 1160 error = copyin(buf, md, sizeof(struct cmsghdr)); 1161 if (error) 1162 break; 1163 msglen = *(u_int *)md; 1164 msglen = FREEBSD32_ALIGN(msglen); 1165 1166 /* Modify the message length to account for alignment. */ 1167 *(u_int *)md = msglen + CMSG_ALIGN(sizeof(struct cmsghdr)) - 1168 FREEBSD32_ALIGN(sizeof(struct cmsghdr)); 1169 1170 md = (char *)md + CMSG_ALIGN(sizeof(struct cmsghdr)); 1171 buf += FREEBSD32_ALIGN(sizeof(struct cmsghdr)); 1172 buflen -= FREEBSD32_ALIGN(sizeof(struct cmsghdr)); 1173 1174 msglen -= FREEBSD32_ALIGN(sizeof(struct cmsghdr)); 1175 if (msglen > 0) { 1176 error = copyin(buf, md, msglen); 1177 if (error) 1178 break; 1179 md = (char *)md + CMSG_ALIGN(msglen); 1180 buf += msglen; 1181 buflen -= msglen; 1182 } 1183 } 1184 1185 if (error) 1186 m_free(m); 1187 else 1188 *mp = m; 1189 return (error); 1190} 1191 1192int 1193freebsd32_sendmsg(struct thread *td, 1194 struct freebsd32_sendmsg_args *uap) 1195{ 1196 struct msghdr msg; 1197 struct msghdr32 m32; 1198 struct iovec *iov; 1199 struct mbuf *control = NULL; 1200 struct sockaddr *to = NULL; 1201 int error; 1202 1203 error = copyin(uap->msg, &m32, sizeof(m32)); 1204 if (error) 1205 return (error); 1206 error = freebsd32_copyinmsghdr(uap->msg, &msg); 1207 if (error) 1208 return (error); 1209 error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov, 1210 EMSGSIZE); 1211 if (error) 1212 return (error); 1213 msg.msg_iov = iov; 1214 if (msg.msg_name != NULL) { 1215 error = getsockaddr(&to, msg.msg_name, msg.msg_namelen); 1216 if (error) { 1217 to = NULL; 1218 goto out; 1219 } 1220 msg.msg_name = to; 1221 } 1222 1223 if (msg.msg_control) { 1224 if (msg.msg_controllen < sizeof(struct cmsghdr)) { 1225 error = EINVAL; 1226 goto out; 1227 } 1228 1229 error = freebsd32_copyin_control(&control, msg.msg_control, 1230 msg.msg_controllen); 1231 if (error) 1232 goto out; 1233 1234 msg.msg_control = NULL; 1235 msg.msg_controllen = 0; 1236 } 1237 1238 error = kern_sendit(td, uap->s, &msg, uap->flags, control, 1239 UIO_USERSPACE); 1240 1241out: 1242 free(iov, M_IOV); 1243 if (to) 1244 free(to, M_SONAME); 1245 return (error); 1246} 1247 1248int 1249freebsd32_recvfrom(struct thread *td, 1250 struct freebsd32_recvfrom_args *uap) 1251{ 1252 struct msghdr msg; 1253 struct iovec aiov; 1254 int error; 1255 1256 if (uap->fromlenaddr) { 1257 error = copyin(PTRIN(uap->fromlenaddr), &msg.msg_namelen, 1258 sizeof(msg.msg_namelen)); 1259 if (error) 1260 return (error); 1261 } else { 1262 msg.msg_namelen = 0; 1263 } 1264 1265 msg.msg_name = PTRIN(uap->from); 1266 msg.msg_iov = &aiov; 1267 msg.msg_iovlen = 1; 1268 aiov.iov_base = PTRIN(uap->buf); 1269 aiov.iov_len = uap->len; 1270 msg.msg_control = NULL; 1271 msg.msg_flags = uap->flags; 1272 error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, NULL); 1273 if (error == 0 && uap->fromlenaddr) 1274 error = copyout(&msg.msg_namelen, PTRIN(uap->fromlenaddr), 1275 sizeof (msg.msg_namelen)); 1276 return (error); 1277} 1278 1279int 1280freebsd32_settimeofday(struct thread *td, 1281 struct freebsd32_settimeofday_args *uap) 1282{ 1283 struct timeval32 tv32; 1284 struct timeval tv, *tvp; 1285 struct timezone tz, *tzp; 1286 int error; 1287 1288 if (uap->tv) { 1289 error = copyin(uap->tv, &tv32, sizeof(tv32)); 1290 if (error) 1291 return (error); 1292 CP(tv32, tv, tv_sec); 1293 CP(tv32, tv, tv_usec); 1294 tvp = &tv; 1295 } else 1296 tvp = NULL; 1297 if (uap->tzp) { 1298 error = copyin(uap->tzp, &tz, sizeof(tz)); 1299 if (error) 1300 return (error); 1301 tzp = &tz; 1302 } else 1303 tzp = NULL; 1304 return (kern_settimeofday(td, tvp, tzp)); 1305} 1306 1307int 1308freebsd32_utimes(struct thread *td, struct freebsd32_utimes_args *uap) 1309{ 1310 struct timeval32 s32[2]; 1311 struct timeval s[2], *sp; 1312 int error; 1313 1314 if (uap->tptr != NULL) { 1315 error = copyin(uap->tptr, s32, sizeof(s32)); 1316 if (error) 1317 return (error); 1318 CP(s32[0], s[0], tv_sec); 1319 CP(s32[0], s[0], tv_usec); 1320 CP(s32[1], s[1], tv_sec); 1321 CP(s32[1], s[1], tv_usec); 1322 sp = s; 1323 } else 1324 sp = NULL; 1325 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 1326 sp, UIO_SYSSPACE)); 1327} 1328 1329int 1330freebsd32_lutimes(struct thread *td, struct freebsd32_lutimes_args *uap) 1331{ 1332 struct timeval32 s32[2]; 1333 struct timeval s[2], *sp; 1334 int error; 1335 1336 if (uap->tptr != NULL) { 1337 error = copyin(uap->tptr, s32, sizeof(s32)); 1338 if (error) 1339 return (error); 1340 CP(s32[0], s[0], tv_sec); 1341 CP(s32[0], s[0], tv_usec); 1342 CP(s32[1], s[1], tv_sec); 1343 CP(s32[1], s[1], tv_usec); 1344 sp = s; 1345 } else 1346 sp = NULL; 1347 return (kern_lutimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE)); 1348} 1349 1350int 1351freebsd32_futimes(struct thread *td, struct freebsd32_futimes_args *uap) 1352{ 1353 struct timeval32 s32[2]; 1354 struct timeval s[2], *sp; 1355 int error; 1356 1357 if (uap->tptr != NULL) { 1358 error = copyin(uap->tptr, s32, sizeof(s32)); 1359 if (error) 1360 return (error); 1361 CP(s32[0], s[0], tv_sec); 1362 CP(s32[0], s[0], tv_usec); 1363 CP(s32[1], s[1], tv_sec); 1364 CP(s32[1], s[1], tv_usec); 1365 sp = s; 1366 } else 1367 sp = NULL; 1368 return (kern_futimes(td, uap->fd, sp, UIO_SYSSPACE)); 1369} 1370 1371int 1372freebsd32_futimesat(struct thread *td, struct freebsd32_futimesat_args *uap) 1373{ 1374 struct timeval32 s32[2]; 1375 struct timeval s[2], *sp; 1376 int error; 1377 1378 if (uap->times != NULL) { 1379 error = copyin(uap->times, s32, sizeof(s32)); 1380 if (error) 1381 return (error); 1382 CP(s32[0], s[0], tv_sec); 1383 CP(s32[0], s[0], tv_usec); 1384 CP(s32[1], s[1], tv_sec); 1385 CP(s32[1], s[1], tv_usec); 1386 sp = s; 1387 } else 1388 sp = NULL; 1389 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE, 1390 sp, UIO_SYSSPACE)); 1391} 1392 1393int 1394freebsd32_futimens(struct thread *td, struct freebsd32_futimens_args *uap) 1395{ 1396 struct timespec32 ts32[2]; 1397 struct timespec ts[2], *tsp; 1398 int error; 1399 1400 if (uap->times != NULL) { 1401 error = copyin(uap->times, ts32, sizeof(ts32)); 1402 if (error) 1403 return (error); 1404 CP(ts32[0], ts[0], tv_sec); 1405 CP(ts32[0], ts[0], tv_nsec); 1406 CP(ts32[1], ts[1], tv_sec); 1407 CP(ts32[1], ts[1], tv_nsec); 1408 tsp = ts; 1409 } else 1410 tsp = NULL; 1411 return (kern_futimens(td, uap->fd, tsp, UIO_SYSSPACE)); 1412} 1413 1414int 1415freebsd32_utimensat(struct thread *td, struct freebsd32_utimensat_args *uap) 1416{ 1417 struct timespec32 ts32[2]; 1418 struct timespec ts[2], *tsp; 1419 int error; 1420 1421 if (uap->times != NULL) { 1422 error = copyin(uap->times, ts32, sizeof(ts32)); 1423 if (error) 1424 return (error); 1425 CP(ts32[0], ts[0], tv_sec); 1426 CP(ts32[0], ts[0], tv_nsec); 1427 CP(ts32[1], ts[1], tv_sec); 1428 CP(ts32[1], ts[1], tv_nsec); 1429 tsp = ts; 1430 } else 1431 tsp = NULL; 1432 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE, 1433 tsp, UIO_SYSSPACE, uap->flag)); 1434} 1435 1436int 1437freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap) 1438{ 1439 struct timeval32 tv32; 1440 struct timeval delta, olddelta, *deltap; 1441 int error; 1442 1443 if (uap->delta) { 1444 error = copyin(uap->delta, &tv32, sizeof(tv32)); 1445 if (error) 1446 return (error); 1447 CP(tv32, delta, tv_sec); 1448 CP(tv32, delta, tv_usec); 1449 deltap = δ 1450 } else 1451 deltap = NULL; 1452 error = kern_adjtime(td, deltap, &olddelta); 1453 if (uap->olddelta && error == 0) { 1454 CP(olddelta, tv32, tv_sec); 1455 CP(olddelta, tv32, tv_usec); 1456 error = copyout(&tv32, uap->olddelta, sizeof(tv32)); 1457 } 1458 return (error); 1459} 1460 1461#ifdef COMPAT_FREEBSD4 1462int 1463freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap) 1464{ 1465 struct statfs32 s32; 1466 struct statfs *sp; 1467 int error; 1468 1469 sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK); 1470 error = kern_statfs(td, uap->path, UIO_USERSPACE, sp); 1471 if (error == 0) { 1472 copy_statfs(sp, &s32); 1473 error = copyout(&s32, uap->buf, sizeof(s32)); 1474 } 1475 free(sp, M_STATFS); 1476 return (error); 1477} 1478#endif 1479 1480#ifdef COMPAT_FREEBSD4 1481int 1482freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap) 1483{ 1484 struct statfs32 s32; 1485 struct statfs *sp; 1486 int error; 1487 1488 sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK); 1489 error = kern_fstatfs(td, uap->fd, sp); 1490 if (error == 0) { 1491 copy_statfs(sp, &s32); 1492 error = copyout(&s32, uap->buf, sizeof(s32)); 1493 } 1494 free(sp, M_STATFS); 1495 return (error); 1496} 1497#endif 1498 1499#ifdef COMPAT_FREEBSD4 1500int 1501freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap) 1502{ 1503 struct statfs32 s32; 1504 struct statfs *sp; 1505 fhandle_t fh; 1506 int error; 1507 1508 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0) 1509 return (error); 1510 sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK); 1511 error = kern_fhstatfs(td, fh, sp); 1512 if (error == 0) { 1513 copy_statfs(sp, &s32); 1514 error = copyout(&s32, uap->buf, sizeof(s32)); 1515 } 1516 free(sp, M_STATFS); 1517 return (error); 1518} 1519#endif 1520 1521int 1522freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap) 1523{ 1524 1525 return (kern_pread(td, uap->fd, uap->buf, uap->nbyte, 1526 PAIR32TO64(off_t, uap->offset))); 1527} 1528 1529int 1530freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap) 1531{ 1532 1533 return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, 1534 PAIR32TO64(off_t, uap->offset))); 1535} 1536 1537#ifdef COMPAT_43 1538int 1539ofreebsd32_lseek(struct thread *td, struct ofreebsd32_lseek_args *uap) 1540{ 1541 1542 return (kern_lseek(td, uap->fd, uap->offset, uap->whence)); 1543} 1544#endif 1545 1546int 1547freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap) 1548{ 1549 int error; 1550 off_t pos; 1551 1552 error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset), 1553 uap->whence); 1554 /* Expand the quad return into two parts for eax and edx */ 1555 pos = td->td_uretoff.tdu_off; 1556 td->td_retval[RETVAL_LO] = pos & 0xffffffff; /* %eax */ 1557 td->td_retval[RETVAL_HI] = pos >> 32; /* %edx */ 1558 return error; 1559} 1560 1561int 1562freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap) 1563{ 1564 1565 return (kern_truncate(td, uap->path, UIO_USERSPACE, 1566 PAIR32TO64(off_t, uap->length))); 1567} 1568 1569int 1570freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap) 1571{ 1572 1573 return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length))); 1574} 1575 1576#ifdef COMPAT_43 1577int 1578ofreebsd32_getdirentries(struct thread *td, 1579 struct ofreebsd32_getdirentries_args *uap) 1580{ 1581 struct ogetdirentries_args ap; 1582 int error; 1583 long loff; 1584 int32_t loff_cut; 1585 1586 ap.fd = uap->fd; 1587 ap.buf = uap->buf; 1588 ap.count = uap->count; 1589 ap.basep = NULL; 1590 error = kern_ogetdirentries(td, &ap, &loff); 1591 if (error == 0) { 1592 loff_cut = loff; 1593 error = copyout(&loff_cut, uap->basep, sizeof(int32_t)); 1594 } 1595 return (error); 1596} 1597#endif 1598 1599int 1600freebsd32_getdirentries(struct thread *td, 1601 struct freebsd32_getdirentries_args *uap) 1602{ 1603 long base; 1604 int32_t base32; 1605 int error; 1606 1607 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base, 1608 NULL, UIO_USERSPACE); 1609 if (error) 1610 return (error); 1611 if (uap->basep != NULL) { 1612 base32 = base; 1613 error = copyout(&base32, uap->basep, sizeof(int32_t)); 1614 } 1615 return (error); 1616} 1617 1618#ifdef COMPAT_FREEBSD6 1619/* versions with the 'int pad' argument */ 1620int 1621freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap) 1622{ 1623 1624 return (kern_pread(td, uap->fd, uap->buf, uap->nbyte, 1625 PAIR32TO64(off_t, uap->offset))); 1626} 1627 1628int 1629freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap) 1630{ 1631 1632 return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, 1633 PAIR32TO64(off_t, uap->offset))); 1634} 1635 1636int 1637freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap) 1638{ 1639 int error; 1640 off_t pos; 1641 1642 error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset), 1643 uap->whence); 1644 /* Expand the quad return into two parts for eax and edx */ 1645 pos = *(off_t *)(td->td_retval); 1646 td->td_retval[RETVAL_LO] = pos & 0xffffffff; /* %eax */ 1647 td->td_retval[RETVAL_HI] = pos >> 32; /* %edx */ 1648 return error; 1649} 1650 1651int 1652freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap) 1653{ 1654 1655 return (kern_truncate(td, uap->path, UIO_USERSPACE, 1656 PAIR32TO64(off_t, uap->length))); 1657} 1658 1659int 1660freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap) 1661{ 1662 1663 return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length))); 1664} 1665#endif /* COMPAT_FREEBSD6 */ 1666 1667struct sf_hdtr32 { 1668 uint32_t headers; 1669 int hdr_cnt; 1670 uint32_t trailers; 1671 int trl_cnt; 1672}; 1673 1674static int 1675freebsd32_do_sendfile(struct thread *td, 1676 struct freebsd32_sendfile_args *uap, int compat) 1677{ 1678 struct sf_hdtr32 hdtr32; 1679 struct sf_hdtr hdtr; 1680 struct uio *hdr_uio, *trl_uio; 1681 struct file *fp; 1682 cap_rights_t rights; 1683 struct iovec32 *iov32; 1684 off_t offset, sbytes; 1685 int error; 1686 1687 offset = PAIR32TO64(off_t, uap->offset); 1688 if (offset < 0) 1689 return (EINVAL); 1690 1691 hdr_uio = trl_uio = NULL; 1692 1693 if (uap->hdtr != NULL) { 1694 error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32)); 1695 if (error) 1696 goto out; 1697 PTRIN_CP(hdtr32, hdtr, headers); 1698 CP(hdtr32, hdtr, hdr_cnt); 1699 PTRIN_CP(hdtr32, hdtr, trailers); 1700 CP(hdtr32, hdtr, trl_cnt); 1701 1702 if (hdtr.headers != NULL) { 1703 iov32 = PTRIN(hdtr32.headers); 1704 error = freebsd32_copyinuio(iov32, 1705 hdtr32.hdr_cnt, &hdr_uio); 1706 if (error) 1707 goto out; 1708#ifdef COMPAT_FREEBSD4 1709 /* 1710 * In FreeBSD < 5.0 the nbytes to send also included 1711 * the header. If compat is specified subtract the 1712 * header size from nbytes. 1713 */ 1714 if (compat) { 1715 if (uap->nbytes > hdr_uio->uio_resid) 1716 uap->nbytes -= hdr_uio->uio_resid; 1717 else 1718 uap->nbytes = 0; 1719 } 1720#endif 1721 } 1722 if (hdtr.trailers != NULL) { 1723 iov32 = PTRIN(hdtr32.trailers); 1724 error = freebsd32_copyinuio(iov32, 1725 hdtr32.trl_cnt, &trl_uio); 1726 if (error) 1727 goto out; 1728 } 1729 } 1730 1731 AUDIT_ARG_FD(uap->fd); 1732 1733 if ((error = fget_read(td, uap->fd, 1734 cap_rights_init(&rights, CAP_PREAD), &fp)) != 0) 1735 goto out; 1736 1737 error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, offset, 1738 uap->nbytes, &sbytes, uap->flags, td); 1739 fdrop(fp, td); 1740 1741 if (uap->sbytes != NULL) 1742 copyout(&sbytes, uap->sbytes, sizeof(off_t)); 1743 1744out: 1745 if (hdr_uio) 1746 free(hdr_uio, M_IOV); 1747 if (trl_uio) 1748 free(trl_uio, M_IOV); 1749 return (error); 1750} 1751 1752#ifdef COMPAT_FREEBSD4 1753int 1754freebsd4_freebsd32_sendfile(struct thread *td, 1755 struct freebsd4_freebsd32_sendfile_args *uap) 1756{ 1757 return (freebsd32_do_sendfile(td, 1758 (struct freebsd32_sendfile_args *)uap, 1)); 1759} 1760#endif 1761 1762int 1763freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap) 1764{ 1765 1766 return (freebsd32_do_sendfile(td, uap, 0)); 1767} 1768 1769static void 1770copy_stat(struct stat *in, struct stat32 *out) 1771{ 1772 1773 CP(*in, *out, st_dev); 1774 CP(*in, *out, st_ino); 1775 CP(*in, *out, st_mode); 1776 CP(*in, *out, st_nlink); 1777 CP(*in, *out, st_uid); 1778 CP(*in, *out, st_gid); 1779 CP(*in, *out, st_rdev); 1780 TS_CP(*in, *out, st_atim); 1781 TS_CP(*in, *out, st_mtim); 1782 TS_CP(*in, *out, st_ctim); 1783 CP(*in, *out, st_size); 1784 CP(*in, *out, st_blocks); 1785 CP(*in, *out, st_blksize); 1786 CP(*in, *out, st_flags); 1787 CP(*in, *out, st_gen); 1788 TS_CP(*in, *out, st_birthtim); 1789} 1790 1791#ifdef COMPAT_43 1792static void 1793copy_ostat(struct stat *in, struct ostat32 *out) 1794{ 1795 1796 CP(*in, *out, st_dev); 1797 CP(*in, *out, st_ino); 1798 CP(*in, *out, st_mode); 1799 CP(*in, *out, st_nlink); 1800 CP(*in, *out, st_uid); 1801 CP(*in, *out, st_gid); 1802 CP(*in, *out, st_rdev); 1803 CP(*in, *out, st_size); 1804 TS_CP(*in, *out, st_atim); 1805 TS_CP(*in, *out, st_mtim); 1806 TS_CP(*in, *out, st_ctim); 1807 CP(*in, *out, st_blksize); 1808 CP(*in, *out, st_blocks); 1809 CP(*in, *out, st_flags); 1810 CP(*in, *out, st_gen); 1811} 1812#endif 1813 1814int 1815freebsd32_stat(struct thread *td, struct freebsd32_stat_args *uap) 1816{ 1817 struct stat sb; 1818 struct stat32 sb32; 1819 int error; 1820 1821 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE, 1822 &sb, NULL); 1823 if (error) 1824 return (error); 1825 copy_stat(&sb, &sb32); 1826 error = copyout(&sb32, uap->ub, sizeof (sb32)); 1827 return (error); 1828} 1829 1830#ifdef COMPAT_43 1831int 1832ofreebsd32_stat(struct thread *td, struct ofreebsd32_stat_args *uap) 1833{ 1834 struct stat sb; 1835 struct ostat32 sb32; 1836 int error; 1837 1838 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE, 1839 &sb, NULL); 1840 if (error) 1841 return (error); 1842 copy_ostat(&sb, &sb32); 1843 error = copyout(&sb32, uap->ub, sizeof (sb32)); 1844 return (error); 1845} 1846#endif 1847 1848int 1849freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap) 1850{ 1851 struct stat ub; 1852 struct stat32 ub32; 1853 int error; 1854 1855 error = kern_fstat(td, uap->fd, &ub); 1856 if (error) 1857 return (error); 1858 copy_stat(&ub, &ub32); 1859 error = copyout(&ub32, uap->ub, sizeof(ub32)); 1860 return (error); 1861} 1862 1863#ifdef COMPAT_43 1864int 1865ofreebsd32_fstat(struct thread *td, struct ofreebsd32_fstat_args *uap) 1866{ 1867 struct stat ub; 1868 struct ostat32 ub32; 1869 int error; 1870 1871 error = kern_fstat(td, uap->fd, &ub); 1872 if (error) 1873 return (error); 1874 copy_ostat(&ub, &ub32); 1875 error = copyout(&ub32, uap->ub, sizeof(ub32)); 1876 return (error); 1877} 1878#endif 1879 1880int 1881freebsd32_fstatat(struct thread *td, struct freebsd32_fstatat_args *uap) 1882{ 1883 struct stat ub; 1884 struct stat32 ub32; 1885 int error; 1886 1887 error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE, 1888 &ub, NULL); 1889 if (error) 1890 return (error); 1891 copy_stat(&ub, &ub32); 1892 error = copyout(&ub32, uap->buf, sizeof(ub32)); 1893 return (error); 1894} 1895 1896int 1897freebsd32_lstat(struct thread *td, struct freebsd32_lstat_args *uap) 1898{ 1899 struct stat sb; 1900 struct stat32 sb32; 1901 int error; 1902 1903 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path, 1904 UIO_USERSPACE, &sb, NULL); 1905 if (error) 1906 return (error); 1907 copy_stat(&sb, &sb32); 1908 error = copyout(&sb32, uap->ub, sizeof (sb32)); 1909 return (error); 1910} 1911 1912#ifdef COMPAT_43 1913int 1914ofreebsd32_lstat(struct thread *td, struct ofreebsd32_lstat_args *uap) 1915{ 1916 struct stat sb; 1917 struct ostat32 sb32; 1918 int error; 1919 1920 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path, 1921 UIO_USERSPACE, &sb, NULL); 1922 if (error) 1923 return (error); 1924 copy_ostat(&sb, &sb32); 1925 error = copyout(&sb32, uap->ub, sizeof (sb32)); 1926 return (error); 1927} 1928#endif 1929 1930int 1931freebsd32_sysctl(struct thread *td, struct freebsd32_sysctl_args *uap) 1932{ 1933 int error, name[CTL_MAXNAME]; 1934 size_t j, oldlen; 1935 uint32_t tmp; 1936 1937 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2) 1938 return (EINVAL); 1939 error = copyin(uap->name, name, uap->namelen * sizeof(int)); 1940 if (error) 1941 return (error); 1942 if (uap->oldlenp) { 1943 error = fueword32(uap->oldlenp, &tmp); 1944 oldlen = tmp; 1945 } else { 1946 oldlen = 0; 1947 } 1948 if (error != 0) 1949 return (EFAULT); 1950 error = userland_sysctl(td, name, uap->namelen, 1951 uap->old, &oldlen, 1, 1952 uap->new, uap->newlen, &j, SCTL_MASK32); 1953 if (error && error != ENOMEM) 1954 return (error); 1955 if (uap->oldlenp) 1956 suword32(uap->oldlenp, j); 1957 return (0); 1958} 1959 1960int 1961freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap) 1962{ 1963 uint32_t version; 1964 int error; 1965 struct jail j; 1966 1967 error = copyin(uap->jail, &version, sizeof(uint32_t)); 1968 if (error) 1969 return (error); 1970 1971 switch (version) { 1972 case 0: 1973 { 1974 /* FreeBSD single IPv4 jails. */ 1975 struct jail32_v0 j32_v0; 1976 1977 bzero(&j, sizeof(struct jail)); 1978 error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0)); 1979 if (error) 1980 return (error); 1981 CP(j32_v0, j, version); 1982 PTRIN_CP(j32_v0, j, path); 1983 PTRIN_CP(j32_v0, j, hostname); 1984 j.ip4s = htonl(j32_v0.ip_number); /* jail_v0 is host order */ 1985 break; 1986 } 1987 1988 case 1: 1989 /* 1990 * Version 1 was used by multi-IPv4 jail implementations 1991 * that never made it into the official kernel. 1992 */ 1993 return (EINVAL); 1994 1995 case 2: /* JAIL_API_VERSION */ 1996 { 1997 /* FreeBSD multi-IPv4/IPv6,noIP jails. */ 1998 struct jail32 j32; 1999 2000 error = copyin(uap->jail, &j32, sizeof(struct jail32)); 2001 if (error) 2002 return (error); 2003 CP(j32, j, version); 2004 PTRIN_CP(j32, j, path); 2005 PTRIN_CP(j32, j, hostname); 2006 PTRIN_CP(j32, j, jailname); 2007 CP(j32, j, ip4s); 2008 CP(j32, j, ip6s); 2009 PTRIN_CP(j32, j, ip4); 2010 PTRIN_CP(j32, j, ip6); 2011 break; 2012 } 2013 2014 default: 2015 /* Sci-Fi jails are not supported, sorry. */ 2016 return (EINVAL); 2017 } 2018 return (kern_jail(td, &j)); 2019} 2020 2021int 2022freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap) 2023{ 2024 struct uio *auio; 2025 int error; 2026 2027 /* Check that we have an even number of iovecs. */ 2028 if (uap->iovcnt & 1) 2029 return (EINVAL); 2030 2031 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio); 2032 if (error) 2033 return (error); 2034 error = kern_jail_set(td, auio, uap->flags); 2035 free(auio, M_IOV); 2036 return (error); 2037} 2038 2039int 2040freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap) 2041{ 2042 struct iovec32 iov32; 2043 struct uio *auio; 2044 int error, i; 2045 2046 /* Check that we have an even number of iovecs. */ 2047 if (uap->iovcnt & 1) 2048 return (EINVAL); 2049 2050 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio); 2051 if (error) 2052 return (error); 2053 error = kern_jail_get(td, auio, uap->flags); 2054 if (error == 0) 2055 for (i = 0; i < uap->iovcnt; i++) { 2056 PTROUT_CP(auio->uio_iov[i], iov32, iov_base); 2057 CP(auio->uio_iov[i], iov32, iov_len); 2058 error = copyout(&iov32, uap->iovp + i, sizeof(iov32)); 2059 if (error != 0) 2060 break; 2061 } 2062 free(auio, M_IOV); 2063 return (error); 2064} 2065 2066int 2067freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap) 2068{ 2069 struct sigaction32 s32; 2070 struct sigaction sa, osa, *sap; 2071 int error; 2072 2073 if (uap->act) { 2074 error = copyin(uap->act, &s32, sizeof(s32)); 2075 if (error) 2076 return (error); 2077 sa.sa_handler = PTRIN(s32.sa_u); 2078 CP(s32, sa, sa_flags); 2079 CP(s32, sa, sa_mask); 2080 sap = &sa; 2081 } else 2082 sap = NULL; 2083 error = kern_sigaction(td, uap->sig, sap, &osa, 0); 2084 if (error == 0 && uap->oact != NULL) { 2085 s32.sa_u = PTROUT(osa.sa_handler); 2086 CP(osa, s32, sa_flags); 2087 CP(osa, s32, sa_mask); 2088 error = copyout(&s32, uap->oact, sizeof(s32)); 2089 } 2090 return (error); 2091} 2092 2093#ifdef COMPAT_FREEBSD4 2094int 2095freebsd4_freebsd32_sigaction(struct thread *td, 2096 struct freebsd4_freebsd32_sigaction_args *uap) 2097{ 2098 struct sigaction32 s32; 2099 struct sigaction sa, osa, *sap; 2100 int error; 2101 2102 if (uap->act) { 2103 error = copyin(uap->act, &s32, sizeof(s32)); 2104 if (error) 2105 return (error); 2106 sa.sa_handler = PTRIN(s32.sa_u); 2107 CP(s32, sa, sa_flags); 2108 CP(s32, sa, sa_mask); 2109 sap = &sa; 2110 } else 2111 sap = NULL; 2112 error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4); 2113 if (error == 0 && uap->oact != NULL) { 2114 s32.sa_u = PTROUT(osa.sa_handler); 2115 CP(osa, s32, sa_flags); 2116 CP(osa, s32, sa_mask); 2117 error = copyout(&s32, uap->oact, sizeof(s32)); 2118 } 2119 return (error); 2120} 2121#endif 2122 2123#ifdef COMPAT_43 2124struct osigaction32 { 2125 u_int32_t sa_u; 2126 osigset_t sa_mask; 2127 int sa_flags; 2128}; 2129 2130#define ONSIG 32 2131 2132int 2133ofreebsd32_sigaction(struct thread *td, 2134 struct ofreebsd32_sigaction_args *uap) 2135{ 2136 struct osigaction32 s32; 2137 struct sigaction sa, osa, *sap; 2138 int error; 2139 2140 if (uap->signum <= 0 || uap->signum >= ONSIG) 2141 return (EINVAL); 2142 2143 if (uap->nsa) { 2144 error = copyin(uap->nsa, &s32, sizeof(s32)); 2145 if (error) 2146 return (error); 2147 sa.sa_handler = PTRIN(s32.sa_u); 2148 CP(s32, sa, sa_flags); 2149 OSIG2SIG(s32.sa_mask, sa.sa_mask); 2150 sap = &sa; 2151 } else 2152 sap = NULL; 2153 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET); 2154 if (error == 0 && uap->osa != NULL) { 2155 s32.sa_u = PTROUT(osa.sa_handler); 2156 CP(osa, s32, sa_flags); 2157 SIG2OSIG(osa.sa_mask, s32.sa_mask); 2158 error = copyout(&s32, uap->osa, sizeof(s32)); 2159 } 2160 return (error); 2161} 2162 2163int 2164ofreebsd32_sigprocmask(struct thread *td, 2165 struct ofreebsd32_sigprocmask_args *uap) 2166{ 2167 sigset_t set, oset; 2168 int error; 2169 2170 OSIG2SIG(uap->mask, set); 2171 error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD); 2172 SIG2OSIG(oset, td->td_retval[0]); 2173 return (error); 2174} 2175 2176int 2177ofreebsd32_sigpending(struct thread *td, 2178 struct ofreebsd32_sigpending_args *uap) 2179{ 2180 struct proc *p = td->td_proc; 2181 sigset_t siglist; 2182 2183 PROC_LOCK(p); 2184 siglist = p->p_siglist; 2185 SIGSETOR(siglist, td->td_siglist); 2186 PROC_UNLOCK(p); 2187 SIG2OSIG(siglist, td->td_retval[0]); 2188 return (0); 2189} 2190 2191struct sigvec32 { 2192 u_int32_t sv_handler; 2193 int sv_mask; 2194 int sv_flags; 2195}; 2196 2197int 2198ofreebsd32_sigvec(struct thread *td, 2199 struct ofreebsd32_sigvec_args *uap) 2200{ 2201 struct sigvec32 vec; 2202 struct sigaction sa, osa, *sap; 2203 int error; 2204 2205 if (uap->signum <= 0 || uap->signum >= ONSIG) 2206 return (EINVAL); 2207 2208 if (uap->nsv) { 2209 error = copyin(uap->nsv, &vec, sizeof(vec)); 2210 if (error) 2211 return (error); 2212 sa.sa_handler = PTRIN(vec.sv_handler); 2213 OSIG2SIG(vec.sv_mask, sa.sa_mask); 2214 sa.sa_flags = vec.sv_flags; 2215 sa.sa_flags ^= SA_RESTART; 2216 sap = &sa; 2217 } else 2218 sap = NULL; 2219 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET); 2220 if (error == 0 && uap->osv != NULL) { 2221 vec.sv_handler = PTROUT(osa.sa_handler); 2222 SIG2OSIG(osa.sa_mask, vec.sv_mask); 2223 vec.sv_flags = osa.sa_flags; 2224 vec.sv_flags &= ~SA_NOCLDWAIT; 2225 vec.sv_flags ^= SA_RESTART; 2226 error = copyout(&vec, uap->osv, sizeof(vec)); 2227 } 2228 return (error); 2229} 2230 2231int 2232ofreebsd32_sigblock(struct thread *td, 2233 struct ofreebsd32_sigblock_args *uap) 2234{ 2235 sigset_t set, oset; 2236 2237 OSIG2SIG(uap->mask, set); 2238 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0); 2239 SIG2OSIG(oset, td->td_retval[0]); 2240 return (0); 2241} 2242 2243int 2244ofreebsd32_sigsetmask(struct thread *td, 2245 struct ofreebsd32_sigsetmask_args *uap) 2246{ 2247 sigset_t set, oset; 2248 2249 OSIG2SIG(uap->mask, set); 2250 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0); 2251 SIG2OSIG(oset, td->td_retval[0]); 2252 return (0); 2253} 2254 2255int 2256ofreebsd32_sigsuspend(struct thread *td, 2257 struct ofreebsd32_sigsuspend_args *uap) 2258{ 2259 sigset_t mask; 2260 2261 OSIG2SIG(uap->mask, mask); 2262 return (kern_sigsuspend(td, mask)); 2263} 2264 2265struct sigstack32 { 2266 u_int32_t ss_sp; 2267 int ss_onstack; 2268}; 2269 2270int 2271ofreebsd32_sigstack(struct thread *td, 2272 struct ofreebsd32_sigstack_args *uap) 2273{ 2274 struct sigstack32 s32; 2275 struct sigstack nss, oss; 2276 int error = 0, unss; 2277 2278 if (uap->nss != NULL) { 2279 error = copyin(uap->nss, &s32, sizeof(s32)); 2280 if (error) 2281 return (error); 2282 nss.ss_sp = PTRIN(s32.ss_sp); 2283 CP(s32, nss, ss_onstack); 2284 unss = 1; 2285 } else { 2286 unss = 0; 2287 } 2288 oss.ss_sp = td->td_sigstk.ss_sp; 2289 oss.ss_onstack = sigonstack(cpu_getstack(td)); 2290 if (unss) { 2291 td->td_sigstk.ss_sp = nss.ss_sp; 2292 td->td_sigstk.ss_size = 0; 2293 td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK); 2294 td->td_pflags |= TDP_ALTSTACK; 2295 } 2296 if (uap->oss != NULL) { 2297 s32.ss_sp = PTROUT(oss.ss_sp); 2298 CP(oss, s32, ss_onstack); 2299 error = copyout(&s32, uap->oss, sizeof(s32)); 2300 } 2301 return (error); 2302} 2303#endif 2304 2305int 2306freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap) 2307{ 2308 2309 return (freebsd32_user_clock_nanosleep(td, CLOCK_REALTIME, 2310 TIMER_RELTIME, uap->rqtp, uap->rmtp)); 2311} 2312 2313int 2314freebsd32_clock_nanosleep(struct thread *td, 2315 struct freebsd32_clock_nanosleep_args *uap) 2316{ 2317 int error; 2318 2319 error = freebsd32_user_clock_nanosleep(td, uap->clock_id, uap->flags, 2320 uap->rqtp, uap->rmtp); 2321 return (kern_posix_error(td, error)); 2322} 2323 2324static int 2325freebsd32_user_clock_nanosleep(struct thread *td, clockid_t clock_id, 2326 int flags, const struct timespec32 *ua_rqtp, struct timespec32 *ua_rmtp) 2327{ 2328 struct timespec32 rmt32, rqt32; 2329 struct timespec rmt, rqt; 2330 int error; 2331 2332 error = copyin(ua_rqtp, &rqt32, sizeof(rqt32)); 2333 if (error) 2334 return (error); 2335 2336 CP(rqt32, rqt, tv_sec); 2337 CP(rqt32, rqt, tv_nsec); 2338 2339 if (ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0 && 2340 !useracc(ua_rmtp, sizeof(rmt32), VM_PROT_WRITE)) 2341 return (EFAULT); 2342 error = kern_clock_nanosleep(td, clock_id, flags, &rqt, &rmt); 2343 if (error == EINTR && ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0) { 2344 int error2; 2345 2346 CP(rmt, rmt32, tv_sec); 2347 CP(rmt, rmt32, tv_nsec); 2348 2349 error2 = copyout(&rmt32, ua_rmtp, sizeof(rmt32)); 2350 if (error2) 2351 error = error2; 2352 } 2353 return (error); 2354} 2355 2356int 2357freebsd32_clock_gettime(struct thread *td, 2358 struct freebsd32_clock_gettime_args *uap) 2359{ 2360 struct timespec ats; 2361 struct timespec32 ats32; 2362 int error; 2363 2364 error = kern_clock_gettime(td, uap->clock_id, &ats); 2365 if (error == 0) { 2366 CP(ats, ats32, tv_sec); 2367 CP(ats, ats32, tv_nsec); 2368 error = copyout(&ats32, uap->tp, sizeof(ats32)); 2369 } 2370 return (error); 2371} 2372 2373int 2374freebsd32_clock_settime(struct thread *td, 2375 struct freebsd32_clock_settime_args *uap) 2376{ 2377 struct timespec ats; 2378 struct timespec32 ats32; 2379 int error; 2380 2381 error = copyin(uap->tp, &ats32, sizeof(ats32)); 2382 if (error) 2383 return (error); 2384 CP(ats32, ats, tv_sec); 2385 CP(ats32, ats, tv_nsec); 2386 2387 return (kern_clock_settime(td, uap->clock_id, &ats)); 2388} 2389 2390int 2391freebsd32_clock_getres(struct thread *td, 2392 struct freebsd32_clock_getres_args *uap) 2393{ 2394 struct timespec ts; 2395 struct timespec32 ts32; 2396 int error; 2397 2398 if (uap->tp == NULL) 2399 return (0); 2400 error = kern_clock_getres(td, uap->clock_id, &ts); 2401 if (error == 0) { 2402 CP(ts, ts32, tv_sec); 2403 CP(ts, ts32, tv_nsec); 2404 error = copyout(&ts32, uap->tp, sizeof(ts32)); 2405 } 2406 return (error); 2407} 2408 2409int freebsd32_ktimer_create(struct thread *td, 2410 struct freebsd32_ktimer_create_args *uap) 2411{ 2412 struct sigevent32 ev32; 2413 struct sigevent ev, *evp; 2414 int error, id; 2415 2416 if (uap->evp == NULL) { 2417 evp = NULL; 2418 } else { 2419 evp = &ev; 2420 error = copyin(uap->evp, &ev32, sizeof(ev32)); 2421 if (error != 0) 2422 return (error); 2423 error = convert_sigevent32(&ev32, &ev); 2424 if (error != 0) 2425 return (error); 2426 } 2427 error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1); 2428 if (error == 0) { 2429 error = copyout(&id, uap->timerid, sizeof(int)); 2430 if (error != 0) 2431 kern_ktimer_delete(td, id); 2432 } 2433 return (error); 2434} 2435 2436int 2437freebsd32_ktimer_settime(struct thread *td, 2438 struct freebsd32_ktimer_settime_args *uap) 2439{ 2440 struct itimerspec32 val32, oval32; 2441 struct itimerspec val, oval, *ovalp; 2442 int error; 2443 2444 error = copyin(uap->value, &val32, sizeof(val32)); 2445 if (error != 0) 2446 return (error); 2447 ITS_CP(val32, val); 2448 ovalp = uap->ovalue != NULL ? &oval : NULL; 2449 error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp); 2450 if (error == 0 && uap->ovalue != NULL) { 2451 ITS_CP(oval, oval32); 2452 error = copyout(&oval32, uap->ovalue, sizeof(oval32)); 2453 } 2454 return (error); 2455} 2456 2457int 2458freebsd32_ktimer_gettime(struct thread *td, 2459 struct freebsd32_ktimer_gettime_args *uap) 2460{ 2461 struct itimerspec32 val32; 2462 struct itimerspec val; 2463 int error; 2464 2465 error = kern_ktimer_gettime(td, uap->timerid, &val); 2466 if (error == 0) { 2467 ITS_CP(val, val32); 2468 error = copyout(&val32, uap->value, sizeof(val32)); 2469 } 2470 return (error); 2471} 2472 2473int 2474freebsd32_clock_getcpuclockid2(struct thread *td, 2475 struct freebsd32_clock_getcpuclockid2_args *uap) 2476{ 2477 clockid_t clk_id; 2478 int error; 2479 2480 error = kern_clock_getcpuclockid2(td, PAIR32TO64(id_t, uap->id), 2481 uap->which, &clk_id); 2482 if (error == 0) 2483 error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t)); 2484 return (error); 2485} 2486 2487int 2488freebsd32_thr_new(struct thread *td, 2489 struct freebsd32_thr_new_args *uap) 2490{ 2491 struct thr_param32 param32; 2492 struct thr_param param; 2493 int error; 2494 2495 if (uap->param_size < 0 || 2496 uap->param_size > sizeof(struct thr_param32)) 2497 return (EINVAL); 2498 bzero(¶m, sizeof(struct thr_param)); 2499 bzero(¶m32, sizeof(struct thr_param32)); 2500 error = copyin(uap->param, ¶m32, uap->param_size); 2501 if (error != 0) 2502 return (error); 2503 param.start_func = PTRIN(param32.start_func); 2504 param.arg = PTRIN(param32.arg); 2505 param.stack_base = PTRIN(param32.stack_base); 2506 param.stack_size = param32.stack_size; 2507 param.tls_base = PTRIN(param32.tls_base); 2508 param.tls_size = param32.tls_size; 2509 param.child_tid = PTRIN(param32.child_tid); 2510 param.parent_tid = PTRIN(param32.parent_tid); 2511 param.flags = param32.flags; 2512 param.rtp = PTRIN(param32.rtp); 2513 param.spare[0] = PTRIN(param32.spare[0]); 2514 param.spare[1] = PTRIN(param32.spare[1]); 2515 param.spare[2] = PTRIN(param32.spare[2]); 2516 2517 return (kern_thr_new(td, ¶m)); 2518} 2519 2520int 2521freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap) 2522{ 2523 struct timespec32 ts32; 2524 struct timespec ts, *tsp; 2525 int error; 2526 2527 error = 0; 2528 tsp = NULL; 2529 if (uap->timeout != NULL) { 2530 error = copyin((const void *)uap->timeout, (void *)&ts32, 2531 sizeof(struct timespec32)); 2532 if (error != 0) 2533 return (error); 2534 ts.tv_sec = ts32.tv_sec; 2535 ts.tv_nsec = ts32.tv_nsec; 2536 tsp = &ts; 2537 } 2538 return (kern_thr_suspend(td, tsp)); 2539} 2540 2541void 2542siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst) 2543{ 2544 bzero(dst, sizeof(*dst)); 2545 dst->si_signo = src->si_signo; 2546 dst->si_errno = src->si_errno; 2547 dst->si_code = src->si_code; 2548 dst->si_pid = src->si_pid; 2549 dst->si_uid = src->si_uid; 2550 dst->si_status = src->si_status; 2551 dst->si_addr = (uintptr_t)src->si_addr; 2552 dst->si_value.sival_int = src->si_value.sival_int; 2553 dst->si_timerid = src->si_timerid; 2554 dst->si_overrun = src->si_overrun; 2555} 2556 2557#ifndef _FREEBSD32_SYSPROTO_H_ 2558struct freebsd32_sigqueue_args { 2559 pid_t pid; 2560 int signum; 2561 /* union sigval32 */ int value; 2562}; 2563#endif 2564int 2565freebsd32_sigqueue(struct thread *td, struct freebsd32_sigqueue_args *uap) 2566{ 2567 union sigval sv; 2568 2569 /* 2570 * On 32-bit ABIs, sival_int and sival_ptr are the same. 2571 * On 64-bit little-endian ABIs, the low bits are the same. 2572 * In 64-bit big-endian ABIs, sival_int overlaps with 2573 * sival_ptr's HIGH bits. We choose to support sival_int 2574 * rather than sival_ptr in this case as it seems to be 2575 * more common. 2576 */ 2577 bzero(&sv, sizeof(sv)); 2578 sv.sival_int = uap->value; 2579 2580 return (kern_sigqueue(td, uap->pid, uap->signum, &sv)); 2581} 2582 2583int 2584freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap) 2585{ 2586 struct timespec32 ts32; 2587 struct timespec ts; 2588 struct timespec *timeout; 2589 sigset_t set; 2590 ksiginfo_t ksi; 2591 struct siginfo32 si32; 2592 int error; 2593 2594 if (uap->timeout) { 2595 error = copyin(uap->timeout, &ts32, sizeof(ts32)); 2596 if (error) 2597 return (error); 2598 ts.tv_sec = ts32.tv_sec; 2599 ts.tv_nsec = ts32.tv_nsec; 2600 timeout = &ts; 2601 } else 2602 timeout = NULL; 2603 2604 error = copyin(uap->set, &set, sizeof(set)); 2605 if (error) 2606 return (error); 2607 2608 error = kern_sigtimedwait(td, set, &ksi, timeout); 2609 if (error) 2610 return (error); 2611 2612 if (uap->info) { 2613 siginfo_to_siginfo32(&ksi.ksi_info, &si32); 2614 error = copyout(&si32, uap->info, sizeof(struct siginfo32)); 2615 } 2616 2617 if (error == 0) 2618 td->td_retval[0] = ksi.ksi_signo; 2619 return (error); 2620} 2621 2622/* 2623 * MPSAFE 2624 */ 2625int 2626freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap) 2627{ 2628 ksiginfo_t ksi; 2629 struct siginfo32 si32; 2630 sigset_t set; 2631 int error; 2632 2633 error = copyin(uap->set, &set, sizeof(set)); 2634 if (error) 2635 return (error); 2636 2637 error = kern_sigtimedwait(td, set, &ksi, NULL); 2638 if (error) 2639 return (error); 2640 2641 if (uap->info) { 2642 siginfo_to_siginfo32(&ksi.ksi_info, &si32); 2643 error = copyout(&si32, uap->info, sizeof(struct siginfo32)); 2644 } 2645 if (error == 0) 2646 td->td_retval[0] = ksi.ksi_signo; 2647 return (error); 2648} 2649 2650int 2651freebsd32_cpuset_setid(struct thread *td, 2652 struct freebsd32_cpuset_setid_args *uap) 2653{ 2654 2655 return (kern_cpuset_setid(td, uap->which, 2656 PAIR32TO64(id_t, uap->id), uap->setid)); 2657} 2658 2659int 2660freebsd32_cpuset_getid(struct thread *td, 2661 struct freebsd32_cpuset_getid_args *uap) 2662{ 2663 2664 return (kern_cpuset_getid(td, uap->level, uap->which, 2665 PAIR32TO64(id_t, uap->id), uap->setid)); 2666} 2667 2668int 2669freebsd32_cpuset_getaffinity(struct thread *td, 2670 struct freebsd32_cpuset_getaffinity_args *uap) 2671{ 2672 2673 return (kern_cpuset_getaffinity(td, uap->level, uap->which, 2674 PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask)); 2675} 2676 2677int 2678freebsd32_cpuset_setaffinity(struct thread *td, 2679 struct freebsd32_cpuset_setaffinity_args *uap) 2680{ 2681 2682 return (kern_cpuset_setaffinity(td, uap->level, uap->which, 2683 PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask)); 2684} 2685 2686int 2687freebsd32_nmount(struct thread *td, 2688 struct freebsd32_nmount_args /* { 2689 struct iovec *iovp; 2690 unsigned int iovcnt; 2691 int flags; 2692 } */ *uap) 2693{ 2694 struct uio *auio; 2695 uint64_t flags; 2696 int error; 2697 2698 /* 2699 * Mount flags are now 64-bits. On 32-bit archtectures only 2700 * 32-bits are passed in, but from here on everything handles 2701 * 64-bit flags correctly. 2702 */ 2703 flags = uap->flags; 2704 2705 AUDIT_ARG_FFLAGS(flags); 2706 2707 /* 2708 * Filter out MNT_ROOTFS. We do not want clients of nmount() in 2709 * userspace to set this flag, but we must filter it out if we want 2710 * MNT_UPDATE on the root file system to work. 2711 * MNT_ROOTFS should only be set by the kernel when mounting its 2712 * root file system. 2713 */ 2714 flags &= ~MNT_ROOTFS; 2715 2716 /* 2717 * check that we have an even number of iovec's 2718 * and that we have at least two options. 2719 */ 2720 if ((uap->iovcnt & 1) || (uap->iovcnt < 4)) 2721 return (EINVAL); 2722 2723 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio); 2724 if (error) 2725 return (error); 2726 error = vfs_donmount(td, flags, auio); 2727 2728 free(auio, M_IOV); 2729 return error; 2730} 2731 2732#if 0 2733int 2734freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap) 2735{ 2736 struct yyy32 *p32, s32; 2737 struct yyy *p = NULL, s; 2738 struct xxx_arg ap; 2739 int error; 2740 2741 if (uap->zzz) { 2742 error = copyin(uap->zzz, &s32, sizeof(s32)); 2743 if (error) 2744 return (error); 2745 /* translate in */ 2746 p = &s; 2747 } 2748 error = kern_xxx(td, p); 2749 if (error) 2750 return (error); 2751 if (uap->zzz) { 2752 /* translate out */ 2753 error = copyout(&s32, p32, sizeof(s32)); 2754 } 2755 return (error); 2756} 2757#endif 2758 2759int 2760syscall32_register(int *offset, struct sysent *new_sysent, 2761 struct sysent *old_sysent, int flags) 2762{ 2763 2764 if ((flags & ~SY_THR_STATIC) != 0) 2765 return (EINVAL); 2766 2767 if (*offset == NO_SYSCALL) { 2768 int i; 2769 2770 for (i = 1; i < SYS_MAXSYSCALL; ++i) 2771 if (freebsd32_sysent[i].sy_call == 2772 (sy_call_t *)lkmnosys) 2773 break; 2774 if (i == SYS_MAXSYSCALL) 2775 return (ENFILE); 2776 *offset = i; 2777 } else if (*offset < 0 || *offset >= SYS_MAXSYSCALL) 2778 return (EINVAL); 2779 else if (freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmnosys && 2780 freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmressys) 2781 return (EEXIST); 2782 2783 *old_sysent = freebsd32_sysent[*offset]; 2784 freebsd32_sysent[*offset] = *new_sysent; 2785 atomic_store_rel_32(&freebsd32_sysent[*offset].sy_thrcnt, flags); 2786 return (0); 2787} 2788 2789int 2790syscall32_deregister(int *offset, struct sysent *old_sysent) 2791{ 2792 2793 if (*offset == 0) 2794 return (0); 2795 2796 freebsd32_sysent[*offset] = *old_sysent; 2797 return (0); 2798} 2799 2800int 2801syscall32_module_handler(struct module *mod, int what, void *arg) 2802{ 2803 struct syscall_module_data *data = (struct syscall_module_data*)arg; 2804 modspecific_t ms; 2805 int error; 2806 2807 switch (what) { 2808 case MOD_LOAD: 2809 error = syscall32_register(data->offset, data->new_sysent, 2810 &data->old_sysent, SY_THR_STATIC_KLD); 2811 if (error) { 2812 /* Leave a mark so we know to safely unload below. */ 2813 data->offset = NULL; 2814 return error; 2815 } 2816 ms.intval = *data->offset; 2817 MOD_XLOCK; 2818 module_setspecific(mod, &ms); 2819 MOD_XUNLOCK; 2820 if (data->chainevh) 2821 error = data->chainevh(mod, what, data->chainarg); 2822 return (error); 2823 case MOD_UNLOAD: 2824 /* 2825 * MOD_LOAD failed, so just return without calling the 2826 * chained handler since we didn't pass along the MOD_LOAD 2827 * event. 2828 */ 2829 if (data->offset == NULL) 2830 return (0); 2831 if (data->chainevh) { 2832 error = data->chainevh(mod, what, data->chainarg); 2833 if (error) 2834 return (error); 2835 } 2836 error = syscall32_deregister(data->offset, &data->old_sysent); 2837 return (error); 2838 default: 2839 error = EOPNOTSUPP; 2840 if (data->chainevh) 2841 error = data->chainevh(mod, what, data->chainarg); 2842 return (error); 2843 } 2844} 2845 2846int 2847syscall32_helper_register(struct syscall_helper_data *sd, int flags) 2848{ 2849 struct syscall_helper_data *sd1; 2850 int error; 2851 2852 for (sd1 = sd; sd1->syscall_no != NO_SYSCALL; sd1++) { 2853 error = syscall32_register(&sd1->syscall_no, &sd1->new_sysent, 2854 &sd1->old_sysent, flags); 2855 if (error != 0) { 2856 syscall32_helper_unregister(sd); 2857 return (error); 2858 } 2859 sd1->registered = 1; 2860 } 2861 return (0); 2862} 2863 2864int 2865syscall32_helper_unregister(struct syscall_helper_data *sd) 2866{ 2867 struct syscall_helper_data *sd1; 2868 2869 for (sd1 = sd; sd1->registered != 0; sd1++) { 2870 syscall32_deregister(&sd1->syscall_no, &sd1->old_sysent); 2871 sd1->registered = 0; 2872 } 2873 return (0); 2874} 2875 2876register_t * 2877freebsd32_copyout_strings(struct image_params *imgp) 2878{ 2879 int argc, envc, i; 2880 u_int32_t *vectp; 2881 char *stringp; 2882 uintptr_t destp; 2883 u_int32_t *stack_base; 2884 struct freebsd32_ps_strings *arginfo; 2885 char canary[sizeof(long) * 8]; 2886 int32_t pagesizes32[MAXPAGESIZES]; 2887 size_t execpath_len; 2888 int szsigcode; 2889 2890 /* 2891 * Calculate string base and vector table pointers. 2892 * Also deal with signal trampoline code for this exec type. 2893 */ 2894 if (imgp->execpath != NULL && imgp->auxargs != NULL) 2895 execpath_len = strlen(imgp->execpath) + 1; 2896 else 2897 execpath_len = 0; 2898 arginfo = (struct freebsd32_ps_strings *)curproc->p_sysent-> 2899 sv_psstrings; 2900 if (imgp->proc->p_sysent->sv_sigcode_base == 0) 2901 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode); 2902 else 2903 szsigcode = 0; 2904 destp = (uintptr_t)arginfo; 2905 2906 /* 2907 * install sigcode 2908 */ 2909 if (szsigcode != 0) { 2910 destp -= szsigcode; 2911 destp = rounddown2(destp, sizeof(uint32_t)); 2912 copyout(imgp->proc->p_sysent->sv_sigcode, (void *)destp, 2913 szsigcode); 2914 } 2915 2916 /* 2917 * Copy the image path for the rtld. 2918 */ 2919 if (execpath_len != 0) { 2920 destp -= execpath_len; 2921 imgp->execpathp = destp; 2922 copyout(imgp->execpath, (void *)destp, execpath_len); 2923 } 2924 2925 /* 2926 * Prepare the canary for SSP. 2927 */ 2928 arc4rand(canary, sizeof(canary), 0); 2929 destp -= sizeof(canary); 2930 imgp->canary = destp; 2931 copyout(canary, (void *)destp, sizeof(canary)); 2932 imgp->canarylen = sizeof(canary); 2933 2934 /* 2935 * Prepare the pagesizes array. 2936 */ 2937 for (i = 0; i < MAXPAGESIZES; i++) 2938 pagesizes32[i] = (uint32_t)pagesizes[i]; 2939 destp -= sizeof(pagesizes32); 2940 destp = rounddown2(destp, sizeof(uint32_t)); 2941 imgp->pagesizes = destp; 2942 copyout(pagesizes32, (void *)destp, sizeof(pagesizes32)); 2943 imgp->pagesizeslen = sizeof(pagesizes32); 2944 2945 destp -= ARG_MAX - imgp->args->stringspace; 2946 destp = rounddown2(destp, sizeof(uint32_t)); 2947 2948 vectp = (uint32_t *)destp; 2949 if (imgp->auxargs) { 2950 /* 2951 * Allocate room on the stack for the ELF auxargs 2952 * array. It has up to AT_COUNT entries. 2953 */ 2954 vectp -= howmany(AT_COUNT * sizeof(Elf32_Auxinfo), 2955 sizeof(*vectp)); 2956 } 2957 2958 /* 2959 * Allocate room for the argv[] and env vectors including the 2960 * terminating NULL pointers. 2961 */ 2962 vectp -= imgp->args->argc + 1 + imgp->args->envc + 1; 2963 2964 /* 2965 * vectp also becomes our initial stack base 2966 */ 2967 stack_base = vectp; 2968 2969 stringp = imgp->args->begin_argv; 2970 argc = imgp->args->argc; 2971 envc = imgp->args->envc; 2972 /* 2973 * Copy out strings - arguments and environment. 2974 */ 2975 copyout(stringp, (void *)destp, ARG_MAX - imgp->args->stringspace); 2976 2977 /* 2978 * Fill in "ps_strings" struct for ps, w, etc. 2979 */ 2980 suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp); 2981 suword32(&arginfo->ps_nargvstr, argc); 2982 2983 /* 2984 * Fill in argument portion of vector table. 2985 */ 2986 for (; argc > 0; --argc) { 2987 suword32(vectp++, (u_int32_t)(intptr_t)destp); 2988 while (*stringp++ != 0) 2989 destp++; 2990 destp++; 2991 } 2992 2993 /* a null vector table pointer separates the argp's from the envp's */ 2994 suword32(vectp++, 0); 2995 2996 suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp); 2997 suword32(&arginfo->ps_nenvstr, envc); 2998 2999 /* 3000 * Fill in environment portion of vector table. 3001 */ 3002 for (; envc > 0; --envc) { 3003 suword32(vectp++, (u_int32_t)(intptr_t)destp); 3004 while (*stringp++ != 0) 3005 destp++; 3006 destp++; 3007 } 3008 3009 /* end of vector table is a null pointer */ 3010 suword32(vectp, 0); 3011 3012 return ((register_t *)stack_base); 3013} 3014 3015int 3016freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap) 3017{ 3018 struct kld_file_stat *stat; 3019 struct kld32_file_stat *stat32; 3020 int error, version; 3021 3022 if ((error = copyin(&uap->stat->version, &version, sizeof(version))) 3023 != 0) 3024 return (error); 3025 if (version != sizeof(struct kld32_file_stat_1) && 3026 version != sizeof(struct kld32_file_stat)) 3027 return (EINVAL); 3028 3029 stat = malloc(sizeof(*stat), M_TEMP, M_WAITOK | M_ZERO); 3030 stat32 = malloc(sizeof(*stat32), M_TEMP, M_WAITOK | M_ZERO); 3031 error = kern_kldstat(td, uap->fileid, stat); 3032 if (error == 0) { 3033 bcopy(&stat->name[0], &stat32->name[0], sizeof(stat->name)); 3034 CP(*stat, *stat32, refs); 3035 CP(*stat, *stat32, id); 3036 PTROUT_CP(*stat, *stat32, address); 3037 CP(*stat, *stat32, size); 3038 bcopy(&stat->pathname[0], &stat32->pathname[0], 3039 sizeof(stat->pathname)); 3040 stat32->version = version; 3041 error = copyout(stat32, uap->stat, version); 3042 } 3043 free(stat, M_TEMP); 3044 free(stat32, M_TEMP); 3045 return (error); 3046} 3047 3048int 3049freebsd32_posix_fallocate(struct thread *td, 3050 struct freebsd32_posix_fallocate_args *uap) 3051{ 3052 int error; 3053 3054 error = kern_posix_fallocate(td, uap->fd, 3055 PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len)); 3056 return (kern_posix_error(td, error)); 3057} 3058 3059int 3060freebsd32_posix_fadvise(struct thread *td, 3061 struct freebsd32_posix_fadvise_args *uap) 3062{ 3063 int error; 3064 3065 error = kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset), 3066 PAIR32TO64(off_t, uap->len), uap->advice); 3067 return (kern_posix_error(td, error)); 3068} 3069 3070int 3071convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig) 3072{ 3073 3074 CP(*sig32, *sig, sigev_notify); 3075 switch (sig->sigev_notify) { 3076 case SIGEV_NONE: 3077 break; 3078 case SIGEV_THREAD_ID: 3079 CP(*sig32, *sig, sigev_notify_thread_id); 3080 /* FALLTHROUGH */ 3081 case SIGEV_SIGNAL: 3082 CP(*sig32, *sig, sigev_signo); 3083 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr); 3084 break; 3085 case SIGEV_KEVENT: 3086 CP(*sig32, *sig, sigev_notify_kqueue); 3087 CP(*sig32, *sig, sigev_notify_kevent_flags); 3088 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr); 3089 break; 3090 default: 3091 return (EINVAL); 3092 } 3093 return (0); 3094} 3095 3096int 3097freebsd32_procctl(struct thread *td, struct freebsd32_procctl_args *uap) 3098{ 3099 void *data; 3100 union { 3101 struct procctl_reaper_status rs; 3102 struct procctl_reaper_pids rp; 3103 struct procctl_reaper_kill rk; 3104 } x; 3105 union { 3106 struct procctl_reaper_pids32 rp; 3107 } x32; 3108 int error, error1, flags, signum; 3109 3110 switch (uap->com) { 3111 case PROC_SPROTECT: 3112 case PROC_TRACE_CTL: 3113 case PROC_TRAPCAP_CTL: 3114 error = copyin(PTRIN(uap->data), &flags, sizeof(flags)); 3115 if (error != 0) 3116 return (error); 3117 data = &flags; 3118 break; 3119 case PROC_REAP_ACQUIRE: 3120 case PROC_REAP_RELEASE: 3121 if (uap->data != NULL) 3122 return (EINVAL); 3123 data = NULL; 3124 break; 3125 case PROC_REAP_STATUS: 3126 data = &x.rs; 3127 break; 3128 case PROC_REAP_GETPIDS: 3129 error = copyin(uap->data, &x32.rp, sizeof(x32.rp)); 3130 if (error != 0) 3131 return (error); 3132 CP(x32.rp, x.rp, rp_count); 3133 PTRIN_CP(x32.rp, x.rp, rp_pids); 3134 data = &x.rp; 3135 break; 3136 case PROC_REAP_KILL: 3137 error = copyin(uap->data, &x.rk, sizeof(x.rk)); 3138 if (error != 0) 3139 return (error); 3140 data = &x.rk; 3141 break; 3142 case PROC_TRACE_STATUS: 3143 case PROC_TRAPCAP_STATUS: 3144 data = &flags; 3145 break; 3146 case PROC_PDEATHSIG_CTL: 3147 error = copyin(uap->data, &signum, sizeof(signum)); 3148 if (error != 0) 3149 return (error); 3150 data = &signum; 3151 break; 3152 case PROC_PDEATHSIG_STATUS: 3153 data = &signum; 3154 break; 3155 default: 3156 return (EINVAL); 3157 } 3158 error = kern_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id), 3159 uap->com, data); 3160 switch (uap->com) { 3161 case PROC_REAP_STATUS: 3162 if (error == 0) 3163 error = copyout(&x.rs, uap->data, sizeof(x.rs)); 3164 break; 3165 case PROC_REAP_KILL: 3166 error1 = copyout(&x.rk, uap->data, sizeof(x.rk)); 3167 if (error == 0) 3168 error = error1; 3169 break; 3170 case PROC_TRACE_STATUS: 3171 case PROC_TRAPCAP_STATUS: 3172 if (error == 0) 3173 error = copyout(&flags, uap->data, sizeof(flags)); 3174 break; 3175 case PROC_PDEATHSIG_STATUS: 3176 if (error == 0) 3177 error = copyout(&signum, uap->data, sizeof(signum)); 3178 break; 3179 } 3180 return (error); 3181} 3182 3183int 3184freebsd32_fcntl(struct thread *td, struct freebsd32_fcntl_args *uap) 3185{ 3186 long tmp; 3187 3188 switch (uap->cmd) { 3189 /* 3190 * Do unsigned conversion for arg when operation 3191 * interprets it as flags or pointer. 3192 */ 3193 case F_SETLK_REMOTE: 3194 case F_SETLKW: 3195 case F_SETLK: 3196 case F_GETLK: 3197 case F_SETFD: 3198 case F_SETFL: 3199 case F_OGETLK: 3200 case F_OSETLK: 3201 case F_OSETLKW: 3202 tmp = (unsigned int)(uap->arg); 3203 break; 3204 default: 3205 tmp = uap->arg; 3206 break; 3207 } 3208 return (kern_fcntl_freebsd(td, uap->fd, uap->cmd, tmp)); 3209} 3210 3211int 3212freebsd32_ppoll(struct thread *td, struct freebsd32_ppoll_args *uap) 3213{ 3214 struct timespec32 ts32; 3215 struct timespec ts, *tsp; 3216 sigset_t set, *ssp; 3217 int error; 3218 3219 if (uap->ts != NULL) { 3220 error = copyin(uap->ts, &ts32, sizeof(ts32)); 3221 if (error != 0) 3222 return (error); 3223 CP(ts32, ts, tv_sec); 3224 CP(ts32, ts, tv_nsec); 3225 tsp = &ts; 3226 } else 3227 tsp = NULL; 3228 if (uap->set != NULL) { 3229 error = copyin(uap->set, &set, sizeof(set)); 3230 if (error != 0) 3231 return (error); 3232 ssp = &set; 3233 } else 3234 ssp = NULL; 3235 3236 return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp)); 3237} 3238 3239int 3240freebsd32_sched_rr_get_interval(struct thread *td, 3241 struct freebsd32_sched_rr_get_interval_args *uap) 3242{ 3243 struct timespec ts; 3244 struct timespec32 ts32; 3245 int error; 3246 3247 error = kern_sched_rr_get_interval(td, uap->pid, &ts); 3248 if (error == 0) { 3249 CP(ts, ts32, tv_sec); 3250 CP(ts, ts32, tv_nsec); 3251 error = copyout(&ts32, uap->interval, sizeof(ts32)); 3252 } 3253 return (error); 3254} 3255