linux32_machdep.c revision 293493
1/*- 2 * Copyright (c) 2004 Tim J. Robbins 3 * Copyright (c) 2002 Doug Rabson 4 * Copyright (c) 2000 Marcel Moolenaar 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer 12 * in this position and unchanged. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31#include <sys/cdefs.h> 32__FBSDID("$FreeBSD: stable/10/sys/amd64/linux32/linux32_machdep.c 293493 2016-01-09 15:16:13Z dchagin $"); 33 34#include <sys/param.h> 35#include <sys/kernel.h> 36#include <sys/systm.h> 37#include <sys/capsicum.h> 38#include <sys/file.h> 39#include <sys/fcntl.h> 40#include <sys/clock.h> 41#include <sys/imgact.h> 42#include <sys/limits.h> 43#include <sys/lock.h> 44#include <sys/malloc.h> 45#include <sys/mman.h> 46#include <sys/mutex.h> 47#include <sys/priv.h> 48#include <sys/proc.h> 49#include <sys/resource.h> 50#include <sys/resourcevar.h> 51#include <sys/syscallsubr.h> 52#include <sys/sysproto.h> 53#include <sys/unistd.h> 54#include <sys/wait.h> 55 56#include <machine/frame.h> 57#include <machine/pcb.h> 58#include <machine/psl.h> 59#include <machine/segments.h> 60#include <machine/specialreg.h> 61 62#include <vm/vm.h> 63#include <vm/pmap.h> 64#include <vm/vm_map.h> 65 66#include <compat/freebsd32/freebsd32_util.h> 67#include <amd64/linux32/linux.h> 68#include <amd64/linux32/linux32_proto.h> 69#include <compat/linux/linux_ipc.h> 70#include <compat/linux/linux_misc.h> 71#include <compat/linux/linux_signal.h> 72#include <compat/linux/linux_util.h> 73#include <compat/linux/linux_emul.h> 74 75struct l_old_select_argv { 76 l_int nfds; 77 l_uintptr_t readfds; 78 l_uintptr_t writefds; 79 l_uintptr_t exceptfds; 80 l_uintptr_t timeout; 81} __packed; 82 83int 84linux_to_bsd_sigaltstack(int lsa) 85{ 86 int bsa = 0; 87 88 if (lsa & LINUX_SS_DISABLE) 89 bsa |= SS_DISABLE; 90 if (lsa & LINUX_SS_ONSTACK) 91 bsa |= SS_ONSTACK; 92 return (bsa); 93} 94 95static int linux_mmap_common(struct thread *td, l_uintptr_t addr, 96 l_size_t len, l_int prot, l_int flags, l_int fd, 97 l_loff_t pos); 98 99int 100bsd_to_linux_sigaltstack(int bsa) 101{ 102 int lsa = 0; 103 104 if (bsa & SS_DISABLE) 105 lsa |= LINUX_SS_DISABLE; 106 if (bsa & SS_ONSTACK) 107 lsa |= LINUX_SS_ONSTACK; 108 return (lsa); 109} 110 111static void 112bsd_to_linux_rusage(struct rusage *ru, struct l_rusage *lru) 113{ 114 115 lru->ru_utime.tv_sec = ru->ru_utime.tv_sec; 116 lru->ru_utime.tv_usec = ru->ru_utime.tv_usec; 117 lru->ru_stime.tv_sec = ru->ru_stime.tv_sec; 118 lru->ru_stime.tv_usec = ru->ru_stime.tv_usec; 119 lru->ru_maxrss = ru->ru_maxrss; 120 lru->ru_ixrss = ru->ru_ixrss; 121 lru->ru_idrss = ru->ru_idrss; 122 lru->ru_isrss = ru->ru_isrss; 123 lru->ru_minflt = ru->ru_minflt; 124 lru->ru_majflt = ru->ru_majflt; 125 lru->ru_nswap = ru->ru_nswap; 126 lru->ru_inblock = ru->ru_inblock; 127 lru->ru_oublock = ru->ru_oublock; 128 lru->ru_msgsnd = ru->ru_msgsnd; 129 lru->ru_msgrcv = ru->ru_msgrcv; 130 lru->ru_nsignals = ru->ru_nsignals; 131 lru->ru_nvcsw = ru->ru_nvcsw; 132 lru->ru_nivcsw = ru->ru_nivcsw; 133} 134 135int 136linux_execve(struct thread *td, struct linux_execve_args *args) 137{ 138 struct image_args eargs; 139 struct vmspace *oldvmspace; 140 char *path; 141 int error; 142 143 LCONVPATHEXIST(td, args->path, &path); 144 145#ifdef DEBUG 146 if (ldebug(execve)) 147 printf(ARGS(execve, "%s"), path); 148#endif 149 150 error = pre_execve(td, &oldvmspace); 151 if (error != 0) { 152 free(path, M_TEMP); 153 return (error); 154 } 155 error = freebsd32_exec_copyin_args(&eargs, path, UIO_SYSSPACE, 156 args->argp, args->envp); 157 free(path, M_TEMP); 158 if (error == 0) 159 error = kern_execve(td, &eargs, NULL); 160 if (error == 0) 161 error = linux_common_execve(td, &eargs); 162 post_execve(td, error, oldvmspace); 163 return (error); 164} 165 166CTASSERT(sizeof(struct l_iovec32) == 8); 167 168static int 169linux32_copyinuio(struct l_iovec32 *iovp, l_ulong iovcnt, struct uio **uiop) 170{ 171 struct l_iovec32 iov32; 172 struct iovec *iov; 173 struct uio *uio; 174 uint32_t iovlen; 175 int error, i; 176 177 *uiop = NULL; 178 if (iovcnt > UIO_MAXIOV) 179 return (EINVAL); 180 iovlen = iovcnt * sizeof(struct iovec); 181 uio = malloc(iovlen + sizeof(*uio), M_IOV, M_WAITOK); 182 iov = (struct iovec *)(uio + 1); 183 for (i = 0; i < iovcnt; i++) { 184 error = copyin(&iovp[i], &iov32, sizeof(struct l_iovec32)); 185 if (error) { 186 free(uio, M_IOV); 187 return (error); 188 } 189 iov[i].iov_base = PTRIN(iov32.iov_base); 190 iov[i].iov_len = iov32.iov_len; 191 } 192 uio->uio_iov = iov; 193 uio->uio_iovcnt = iovcnt; 194 uio->uio_segflg = UIO_USERSPACE; 195 uio->uio_offset = -1; 196 uio->uio_resid = 0; 197 for (i = 0; i < iovcnt; i++) { 198 if (iov->iov_len > INT_MAX - uio->uio_resid) { 199 free(uio, M_IOV); 200 return (EINVAL); 201 } 202 uio->uio_resid += iov->iov_len; 203 iov++; 204 } 205 *uiop = uio; 206 return (0); 207} 208 209int 210linux32_copyiniov(struct l_iovec32 *iovp32, l_ulong iovcnt, struct iovec **iovp, 211 int error) 212{ 213 struct l_iovec32 iov32; 214 struct iovec *iov; 215 uint32_t iovlen; 216 int i; 217 218 *iovp = NULL; 219 if (iovcnt > UIO_MAXIOV) 220 return (error); 221 iovlen = iovcnt * sizeof(struct iovec); 222 iov = malloc(iovlen, M_IOV, M_WAITOK); 223 for (i = 0; i < iovcnt; i++) { 224 error = copyin(&iovp32[i], &iov32, sizeof(struct l_iovec32)); 225 if (error) { 226 free(iov, M_IOV); 227 return (error); 228 } 229 iov[i].iov_base = PTRIN(iov32.iov_base); 230 iov[i].iov_len = iov32.iov_len; 231 } 232 *iovp = iov; 233 return(0); 234 235} 236 237int 238linux_readv(struct thread *td, struct linux_readv_args *uap) 239{ 240 struct uio *auio; 241 int error; 242 243 error = linux32_copyinuio(uap->iovp, uap->iovcnt, &auio); 244 if (error) 245 return (error); 246 error = kern_readv(td, uap->fd, auio); 247 free(auio, M_IOV); 248 return (error); 249} 250 251int 252linux_writev(struct thread *td, struct linux_writev_args *uap) 253{ 254 struct uio *auio; 255 int error; 256 257 error = linux32_copyinuio(uap->iovp, uap->iovcnt, &auio); 258 if (error) 259 return (error); 260 error = kern_writev(td, uap->fd, auio); 261 free(auio, M_IOV); 262 return (error); 263} 264 265struct l_ipc_kludge { 266 l_uintptr_t msgp; 267 l_long msgtyp; 268} __packed; 269 270int 271linux_ipc(struct thread *td, struct linux_ipc_args *args) 272{ 273 274 switch (args->what & 0xFFFF) { 275 case LINUX_SEMOP: { 276 struct linux_semop_args a; 277 278 a.semid = args->arg1; 279 a.tsops = args->ptr; 280 a.nsops = args->arg2; 281 return (linux_semop(td, &a)); 282 } 283 case LINUX_SEMGET: { 284 struct linux_semget_args a; 285 286 a.key = args->arg1; 287 a.nsems = args->arg2; 288 a.semflg = args->arg3; 289 return (linux_semget(td, &a)); 290 } 291 case LINUX_SEMCTL: { 292 struct linux_semctl_args a; 293 int error; 294 295 a.semid = args->arg1; 296 a.semnum = args->arg2; 297 a.cmd = args->arg3; 298 error = copyin(args->ptr, &a.arg, sizeof(a.arg)); 299 if (error) 300 return (error); 301 return (linux_semctl(td, &a)); 302 } 303 case LINUX_MSGSND: { 304 struct linux_msgsnd_args a; 305 306 a.msqid = args->arg1; 307 a.msgp = args->ptr; 308 a.msgsz = args->arg2; 309 a.msgflg = args->arg3; 310 return (linux_msgsnd(td, &a)); 311 } 312 case LINUX_MSGRCV: { 313 struct linux_msgrcv_args a; 314 315 a.msqid = args->arg1; 316 a.msgsz = args->arg2; 317 a.msgflg = args->arg3; 318 if ((args->what >> 16) == 0) { 319 struct l_ipc_kludge tmp; 320 int error; 321 322 if (args->ptr == 0) 323 return (EINVAL); 324 error = copyin(args->ptr, &tmp, sizeof(tmp)); 325 if (error) 326 return (error); 327 a.msgp = PTRIN(tmp.msgp); 328 a.msgtyp = tmp.msgtyp; 329 } else { 330 a.msgp = args->ptr; 331 a.msgtyp = args->arg5; 332 } 333 return (linux_msgrcv(td, &a)); 334 } 335 case LINUX_MSGGET: { 336 struct linux_msgget_args a; 337 338 a.key = args->arg1; 339 a.msgflg = args->arg2; 340 return (linux_msgget(td, &a)); 341 } 342 case LINUX_MSGCTL: { 343 struct linux_msgctl_args a; 344 345 a.msqid = args->arg1; 346 a.cmd = args->arg2; 347 a.buf = args->ptr; 348 return (linux_msgctl(td, &a)); 349 } 350 case LINUX_SHMAT: { 351 struct linux_shmat_args a; 352 353 a.shmid = args->arg1; 354 a.shmaddr = args->ptr; 355 a.shmflg = args->arg2; 356 a.raddr = PTRIN((l_uint)args->arg3); 357 return (linux_shmat(td, &a)); 358 } 359 case LINUX_SHMDT: { 360 struct linux_shmdt_args a; 361 362 a.shmaddr = args->ptr; 363 return (linux_shmdt(td, &a)); 364 } 365 case LINUX_SHMGET: { 366 struct linux_shmget_args a; 367 368 a.key = args->arg1; 369 a.size = args->arg2; 370 a.shmflg = args->arg3; 371 return (linux_shmget(td, &a)); 372 } 373 case LINUX_SHMCTL: { 374 struct linux_shmctl_args a; 375 376 a.shmid = args->arg1; 377 a.cmd = args->arg2; 378 a.buf = args->ptr; 379 return (linux_shmctl(td, &a)); 380 } 381 default: 382 break; 383 } 384 385 return (EINVAL); 386} 387 388int 389linux_old_select(struct thread *td, struct linux_old_select_args *args) 390{ 391 struct l_old_select_argv linux_args; 392 struct linux_select_args newsel; 393 int error; 394 395#ifdef DEBUG 396 if (ldebug(old_select)) 397 printf(ARGS(old_select, "%p"), args->ptr); 398#endif 399 400 error = copyin(args->ptr, &linux_args, sizeof(linux_args)); 401 if (error) 402 return (error); 403 404 newsel.nfds = linux_args.nfds; 405 newsel.readfds = PTRIN(linux_args.readfds); 406 newsel.writefds = PTRIN(linux_args.writefds); 407 newsel.exceptfds = PTRIN(linux_args.exceptfds); 408 newsel.timeout = PTRIN(linux_args.timeout); 409 return (linux_select(td, &newsel)); 410} 411 412int 413linux_set_cloned_tls(struct thread *td, void *desc) 414{ 415 struct user_segment_descriptor sd; 416 struct l_user_desc info; 417 struct pcb *pcb; 418 int error; 419 int a[2]; 420 421 error = copyin(desc, &info, sizeof(struct l_user_desc)); 422 if (error) { 423 printf(LMSG("copyin failed!")); 424 } else { 425 /* We might copy out the entry_number as GUGS32_SEL. */ 426 info.entry_number = GUGS32_SEL; 427 error = copyout(&info, desc, sizeof(struct l_user_desc)); 428 if (error) 429 printf(LMSG("copyout failed!")); 430 431 a[0] = LINUX_LDT_entry_a(&info); 432 a[1] = LINUX_LDT_entry_b(&info); 433 434 memcpy(&sd, &a, sizeof(a)); 435#ifdef DEBUG 436 if (ldebug(clone)) 437 printf("Segment created in clone with " 438 "CLONE_SETTLS: lobase: %x, hibase: %x, " 439 "lolimit: %x, hilimit: %x, type: %i, " 440 "dpl: %i, p: %i, xx: %i, long: %i, " 441 "def32: %i, gran: %i\n", sd.sd_lobase, 442 sd.sd_hibase, sd.sd_lolimit, sd.sd_hilimit, 443 sd.sd_type, sd.sd_dpl, sd.sd_p, sd.sd_xx, 444 sd.sd_long, sd.sd_def32, sd.sd_gran); 445#endif 446 pcb = td->td_pcb; 447 pcb->pcb_gsbase = (register_t)info.base_addr; 448 td->td_frame->tf_gs = GSEL(GUGS32_SEL, SEL_UPL); 449 set_pcb_flags(pcb, PCB_32BIT); 450 } 451 452 return (error); 453} 454 455int 456linux_set_upcall_kse(struct thread *td, register_t stack) 457{ 458 459 if (stack) 460 td->td_frame->tf_rsp = stack; 461 462 /* 463 * The newly created Linux thread returns 464 * to the user space by the same path that a parent do. 465 */ 466 td->td_frame->tf_rax = 0; 467 return (0); 468} 469 470#define STACK_SIZE (2 * 1024 * 1024) 471#define GUARD_SIZE (4 * PAGE_SIZE) 472 473int 474linux_mmap2(struct thread *td, struct linux_mmap2_args *args) 475{ 476 477#ifdef DEBUG 478 if (ldebug(mmap2)) 479 printf(ARGS(mmap2, "0x%08x, %d, %d, 0x%08x, %d, %d"), 480 args->addr, args->len, args->prot, 481 args->flags, args->fd, args->pgoff); 482#endif 483 484 return (linux_mmap_common(td, PTROUT(args->addr), args->len, args->prot, 485 args->flags, args->fd, (uint64_t)(uint32_t)args->pgoff * 486 PAGE_SIZE)); 487} 488 489int 490linux_mmap(struct thread *td, struct linux_mmap_args *args) 491{ 492 int error; 493 struct l_mmap_argv linux_args; 494 495 error = copyin(args->ptr, &linux_args, sizeof(linux_args)); 496 if (error) 497 return (error); 498 499#ifdef DEBUG 500 if (ldebug(mmap)) 501 printf(ARGS(mmap, "0x%08x, %d, %d, 0x%08x, %d, %d"), 502 linux_args.addr, linux_args.len, linux_args.prot, 503 linux_args.flags, linux_args.fd, linux_args.pgoff); 504#endif 505 506 return (linux_mmap_common(td, linux_args.addr, linux_args.len, 507 linux_args.prot, linux_args.flags, linux_args.fd, 508 (uint32_t)linux_args.pgoff)); 509} 510 511static int 512linux_mmap_common(struct thread *td, l_uintptr_t addr, l_size_t len, l_int prot, 513 l_int flags, l_int fd, l_loff_t pos) 514{ 515 struct proc *p = td->td_proc; 516 struct mmap_args /* { 517 caddr_t addr; 518 size_t len; 519 int prot; 520 int flags; 521 int fd; 522 long pad; 523 off_t pos; 524 } */ bsd_args; 525 int error; 526 struct file *fp; 527 cap_rights_t rights; 528 529 error = 0; 530 bsd_args.flags = 0; 531 fp = NULL; 532 533 /* 534 * Linux mmap(2): 535 * You must specify exactly one of MAP_SHARED and MAP_PRIVATE 536 */ 537 if (!((flags & LINUX_MAP_SHARED) ^ (flags & LINUX_MAP_PRIVATE))) 538 return (EINVAL); 539 540 if (flags & LINUX_MAP_SHARED) 541 bsd_args.flags |= MAP_SHARED; 542 if (flags & LINUX_MAP_PRIVATE) 543 bsd_args.flags |= MAP_PRIVATE; 544 if (flags & LINUX_MAP_FIXED) 545 bsd_args.flags |= MAP_FIXED; 546 if (flags & LINUX_MAP_ANON) { 547 /* Enforce pos to be on page boundary, then ignore. */ 548 if ((pos & PAGE_MASK) != 0) 549 return (EINVAL); 550 pos = 0; 551 bsd_args.flags |= MAP_ANON; 552 } else 553 bsd_args.flags |= MAP_NOSYNC; 554 if (flags & LINUX_MAP_GROWSDOWN) 555 bsd_args.flags |= MAP_STACK; 556 557 /* 558 * PROT_READ, PROT_WRITE, or PROT_EXEC implies PROT_READ and PROT_EXEC 559 * on Linux/i386. We do this to ensure maximum compatibility. 560 * Linux/ia64 does the same in i386 emulation mode. 561 */ 562 bsd_args.prot = prot; 563 if (bsd_args.prot & (PROT_READ | PROT_WRITE | PROT_EXEC)) 564 bsd_args.prot |= PROT_READ | PROT_EXEC; 565 566 /* Linux does not check file descriptor when MAP_ANONYMOUS is set. */ 567 bsd_args.fd = (bsd_args.flags & MAP_ANON) ? -1 : fd; 568 if (bsd_args.fd != -1) { 569 /* 570 * Linux follows Solaris mmap(2) description: 571 * The file descriptor fildes is opened with 572 * read permission, regardless of the 573 * protection options specified. 574 */ 575 576 error = fget(td, bsd_args.fd, 577 cap_rights_init(&rights, CAP_MMAP), &fp); 578 if (error != 0) 579 return (error); 580 if (fp->f_type != DTYPE_VNODE) { 581 fdrop(fp, td); 582 return (EINVAL); 583 } 584 585 /* Linux mmap() just fails for O_WRONLY files */ 586 if (!(fp->f_flag & FREAD)) { 587 fdrop(fp, td); 588 return (EACCES); 589 } 590 591 fdrop(fp, td); 592 } 593 594 if (flags & LINUX_MAP_GROWSDOWN) { 595 /* 596 * The Linux MAP_GROWSDOWN option does not limit auto 597 * growth of the region. Linux mmap with this option 598 * takes as addr the inital BOS, and as len, the initial 599 * region size. It can then grow down from addr without 600 * limit. However, Linux threads has an implicit internal 601 * limit to stack size of STACK_SIZE. Its just not 602 * enforced explicitly in Linux. But, here we impose 603 * a limit of (STACK_SIZE - GUARD_SIZE) on the stack 604 * region, since we can do this with our mmap. 605 * 606 * Our mmap with MAP_STACK takes addr as the maximum 607 * downsize limit on BOS, and as len the max size of 608 * the region. It then maps the top SGROWSIZ bytes, 609 * and auto grows the region down, up to the limit 610 * in addr. 611 * 612 * If we don't use the MAP_STACK option, the effect 613 * of this code is to allocate a stack region of a 614 * fixed size of (STACK_SIZE - GUARD_SIZE). 615 */ 616 617 if ((caddr_t)PTRIN(addr) + len > p->p_vmspace->vm_maxsaddr) { 618 /* 619 * Some Linux apps will attempt to mmap 620 * thread stacks near the top of their 621 * address space. If their TOS is greater 622 * than vm_maxsaddr, vm_map_growstack() 623 * will confuse the thread stack with the 624 * process stack and deliver a SEGV if they 625 * attempt to grow the thread stack past their 626 * current stacksize rlimit. To avoid this, 627 * adjust vm_maxsaddr upwards to reflect 628 * the current stacksize rlimit rather 629 * than the maximum possible stacksize. 630 * It would be better to adjust the 631 * mmap'ed region, but some apps do not check 632 * mmap's return value. 633 */ 634 PROC_LOCK(p); 635 p->p_vmspace->vm_maxsaddr = (char *)LINUX32_USRSTACK - 636 lim_cur(p, RLIMIT_STACK); 637 PROC_UNLOCK(p); 638 } 639 640 /* 641 * This gives us our maximum stack size and a new BOS. 642 * If we're using VM_STACK, then mmap will just map 643 * the top SGROWSIZ bytes, and let the stack grow down 644 * to the limit at BOS. If we're not using VM_STACK 645 * we map the full stack, since we don't have a way 646 * to autogrow it. 647 */ 648 if (len > STACK_SIZE - GUARD_SIZE) { 649 bsd_args.addr = (caddr_t)PTRIN(addr); 650 bsd_args.len = len; 651 } else { 652 bsd_args.addr = (caddr_t)PTRIN(addr) - 653 (STACK_SIZE - GUARD_SIZE - len); 654 bsd_args.len = STACK_SIZE - GUARD_SIZE; 655 } 656 } else { 657 bsd_args.addr = (caddr_t)PTRIN(addr); 658 bsd_args.len = len; 659 } 660 bsd_args.pos = pos; 661 662#ifdef DEBUG 663 if (ldebug(mmap)) 664 printf("-> %s(%p, %d, %d, 0x%08x, %d, 0x%x)\n", 665 __func__, 666 (void *)bsd_args.addr, (int)bsd_args.len, bsd_args.prot, 667 bsd_args.flags, bsd_args.fd, (int)bsd_args.pos); 668#endif 669 error = sys_mmap(td, &bsd_args); 670#ifdef DEBUG 671 if (ldebug(mmap)) 672 printf("-> %s() return: 0x%x (0x%08x)\n", 673 __func__, error, (u_int)td->td_retval[0]); 674#endif 675 return (error); 676} 677 678int 679linux_mprotect(struct thread *td, struct linux_mprotect_args *uap) 680{ 681 struct mprotect_args bsd_args; 682 683 bsd_args.addr = uap->addr; 684 bsd_args.len = uap->len; 685 bsd_args.prot = uap->prot; 686 if (bsd_args.prot & (PROT_READ | PROT_WRITE | PROT_EXEC)) 687 bsd_args.prot |= PROT_READ | PROT_EXEC; 688 return (sys_mprotect(td, &bsd_args)); 689} 690 691int 692linux_iopl(struct thread *td, struct linux_iopl_args *args) 693{ 694 int error; 695 696 if (args->level < 0 || args->level > 3) 697 return (EINVAL); 698 if ((error = priv_check(td, PRIV_IO)) != 0) 699 return (error); 700 if ((error = securelevel_gt(td->td_ucred, 0)) != 0) 701 return (error); 702 td->td_frame->tf_rflags = (td->td_frame->tf_rflags & ~PSL_IOPL) | 703 (args->level * (PSL_IOPL / 3)); 704 705 return (0); 706} 707 708int 709linux_sigaction(struct thread *td, struct linux_sigaction_args *args) 710{ 711 l_osigaction_t osa; 712 l_sigaction_t act, oact; 713 int error; 714 715#ifdef DEBUG 716 if (ldebug(sigaction)) 717 printf(ARGS(sigaction, "%d, %p, %p"), 718 args->sig, (void *)args->nsa, (void *)args->osa); 719#endif 720 721 if (args->nsa != NULL) { 722 error = copyin(args->nsa, &osa, sizeof(l_osigaction_t)); 723 if (error) 724 return (error); 725 act.lsa_handler = osa.lsa_handler; 726 act.lsa_flags = osa.lsa_flags; 727 act.lsa_restorer = osa.lsa_restorer; 728 LINUX_SIGEMPTYSET(act.lsa_mask); 729 act.lsa_mask.__bits[0] = osa.lsa_mask; 730 } 731 732 error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL, 733 args->osa ? &oact : NULL); 734 735 if (args->osa != NULL && !error) { 736 osa.lsa_handler = oact.lsa_handler; 737 osa.lsa_flags = oact.lsa_flags; 738 osa.lsa_restorer = oact.lsa_restorer; 739 osa.lsa_mask = oact.lsa_mask.__bits[0]; 740 error = copyout(&osa, args->osa, sizeof(l_osigaction_t)); 741 } 742 743 return (error); 744} 745 746/* 747 * Linux has two extra args, restart and oldmask. We don't use these, 748 * but it seems that "restart" is actually a context pointer that 749 * enables the signal to happen with a different register set. 750 */ 751int 752linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args) 753{ 754 sigset_t sigmask; 755 l_sigset_t mask; 756 757#ifdef DEBUG 758 if (ldebug(sigsuspend)) 759 printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask); 760#endif 761 762 LINUX_SIGEMPTYSET(mask); 763 mask.__bits[0] = args->mask; 764 linux_to_bsd_sigset(&mask, &sigmask); 765 return (kern_sigsuspend(td, sigmask)); 766} 767 768int 769linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap) 770{ 771 l_sigset_t lmask; 772 sigset_t sigmask; 773 int error; 774 775#ifdef DEBUG 776 if (ldebug(rt_sigsuspend)) 777 printf(ARGS(rt_sigsuspend, "%p, %d"), 778 (void *)uap->newset, uap->sigsetsize); 779#endif 780 781 if (uap->sigsetsize != sizeof(l_sigset_t)) 782 return (EINVAL); 783 784 error = copyin(uap->newset, &lmask, sizeof(l_sigset_t)); 785 if (error) 786 return (error); 787 788 linux_to_bsd_sigset(&lmask, &sigmask); 789 return (kern_sigsuspend(td, sigmask)); 790} 791 792int 793linux_pause(struct thread *td, struct linux_pause_args *args) 794{ 795 struct proc *p = td->td_proc; 796 sigset_t sigmask; 797 798#ifdef DEBUG 799 if (ldebug(pause)) 800 printf(ARGS(pause, "")); 801#endif 802 803 PROC_LOCK(p); 804 sigmask = td->td_sigmask; 805 PROC_UNLOCK(p); 806 return (kern_sigsuspend(td, sigmask)); 807} 808 809int 810linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap) 811{ 812 stack_t ss, oss; 813 l_stack_t lss; 814 int error; 815 816#ifdef DEBUG 817 if (ldebug(sigaltstack)) 818 printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss); 819#endif 820 821 if (uap->uss != NULL) { 822 error = copyin(uap->uss, &lss, sizeof(l_stack_t)); 823 if (error) 824 return (error); 825 826 ss.ss_sp = PTRIN(lss.ss_sp); 827 ss.ss_size = lss.ss_size; 828 ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags); 829 } 830 error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL, 831 (uap->uoss != NULL) ? &oss : NULL); 832 if (!error && uap->uoss != NULL) { 833 lss.ss_sp = PTROUT(oss.ss_sp); 834 lss.ss_size = oss.ss_size; 835 lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags); 836 error = copyout(&lss, uap->uoss, sizeof(l_stack_t)); 837 } 838 839 return (error); 840} 841 842int 843linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args) 844{ 845 struct ftruncate_args sa; 846 847#ifdef DEBUG 848 if (ldebug(ftruncate64)) 849 printf(ARGS(ftruncate64, "%u, %jd"), args->fd, 850 (intmax_t)args->length); 851#endif 852 853 sa.fd = args->fd; 854 sa.length = args->length; 855 return sys_ftruncate(td, &sa); 856} 857 858int 859linux_gettimeofday(struct thread *td, struct linux_gettimeofday_args *uap) 860{ 861 struct timeval atv; 862 l_timeval atv32; 863 struct timezone rtz; 864 int error = 0; 865 866 if (uap->tp) { 867 microtime(&atv); 868 atv32.tv_sec = atv.tv_sec; 869 atv32.tv_usec = atv.tv_usec; 870 error = copyout(&atv32, uap->tp, sizeof(atv32)); 871 } 872 if (error == 0 && uap->tzp != NULL) { 873 rtz.tz_minuteswest = tz_minuteswest; 874 rtz.tz_dsttime = tz_dsttime; 875 error = copyout(&rtz, uap->tzp, sizeof(rtz)); 876 } 877 return (error); 878} 879 880int 881linux_settimeofday(struct thread *td, struct linux_settimeofday_args *uap) 882{ 883 l_timeval atv32; 884 struct timeval atv, *tvp; 885 struct timezone atz, *tzp; 886 int error; 887 888 if (uap->tp) { 889 error = copyin(uap->tp, &atv32, sizeof(atv32)); 890 if (error) 891 return (error); 892 atv.tv_sec = atv32.tv_sec; 893 atv.tv_usec = atv32.tv_usec; 894 tvp = &atv; 895 } else 896 tvp = NULL; 897 if (uap->tzp) { 898 error = copyin(uap->tzp, &atz, sizeof(atz)); 899 if (error) 900 return (error); 901 tzp = &atz; 902 } else 903 tzp = NULL; 904 return (kern_settimeofday(td, tvp, tzp)); 905} 906 907int 908linux_getrusage(struct thread *td, struct linux_getrusage_args *uap) 909{ 910 struct l_rusage s32; 911 struct rusage s; 912 int error; 913 914 error = kern_getrusage(td, uap->who, &s); 915 if (error != 0) 916 return (error); 917 if (uap->rusage != NULL) { 918 bsd_to_linux_rusage(&s, &s32); 919 error = copyout(&s32, uap->rusage, sizeof(s32)); 920 } 921 return (error); 922} 923 924int 925linux_set_thread_area(struct thread *td, 926 struct linux_set_thread_area_args *args) 927{ 928 struct l_user_desc info; 929 struct user_segment_descriptor sd; 930 struct pcb *pcb; 931 int a[2]; 932 int error; 933 934 error = copyin(args->desc, &info, sizeof(struct l_user_desc)); 935 if (error) 936 return (error); 937 938#ifdef DEBUG 939 if (ldebug(set_thread_area)) 940 printf(ARGS(set_thread_area, "%i, %x, %x, %i, %i, %i, " 941 "%i, %i, %i"), info.entry_number, info.base_addr, 942 info.limit, info.seg_32bit, info.contents, 943 info.read_exec_only, info.limit_in_pages, 944 info.seg_not_present, info.useable); 945#endif 946 947 /* 948 * Semantics of Linux version: every thread in the system has array 949 * of three TLS descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown. 950 * This syscall loads one of the selected TLS decriptors with a value 951 * and also loads GDT descriptors 6, 7 and 8 with the content of 952 * the per-thread descriptors. 953 * 954 * Semantics of FreeBSD version: I think we can ignore that Linux has 955 * three per-thread descriptors and use just the first one. 956 * The tls_array[] is used only in [gs]et_thread_area() syscalls and 957 * for loading the GDT descriptors. We use just one GDT descriptor 958 * for TLS, so we will load just one. 959 * 960 * XXX: This doesn't work when a user space process tries to use more 961 * than one TLS segment. Comment in the Linux source says wine might 962 * do this. 963 */ 964 965 /* 966 * GLIBC reads current %gs and call set_thread_area() with it. 967 * We should let GUDATA_SEL and GUGS32_SEL proceed as well because 968 * we use these segments. 969 */ 970 switch (info.entry_number) { 971 case GUGS32_SEL: 972 case GUDATA_SEL: 973 case 6: 974 case -1: 975 info.entry_number = GUGS32_SEL; 976 break; 977 default: 978 return (EINVAL); 979 } 980 981 /* 982 * We have to copy out the GDT entry we use. 983 * 984 * XXX: What if a user space program does not check the return value 985 * and tries to use 6, 7 or 8? 986 */ 987 error = copyout(&info, args->desc, sizeof(struct l_user_desc)); 988 if (error) 989 return (error); 990 991 if (LINUX_LDT_empty(&info)) { 992 a[0] = 0; 993 a[1] = 0; 994 } else { 995 a[0] = LINUX_LDT_entry_a(&info); 996 a[1] = LINUX_LDT_entry_b(&info); 997 } 998 999 memcpy(&sd, &a, sizeof(a)); 1000#ifdef DEBUG 1001 if (ldebug(set_thread_area)) 1002 printf("Segment created in set_thread_area: " 1003 "lobase: %x, hibase: %x, lolimit: %x, hilimit: %x, " 1004 "type: %i, dpl: %i, p: %i, xx: %i, long: %i, " 1005 "def32: %i, gran: %i\n", 1006 sd.sd_lobase, 1007 sd.sd_hibase, 1008 sd.sd_lolimit, 1009 sd.sd_hilimit, 1010 sd.sd_type, 1011 sd.sd_dpl, 1012 sd.sd_p, 1013 sd.sd_xx, 1014 sd.sd_long, 1015 sd.sd_def32, 1016 sd.sd_gran); 1017#endif 1018 1019 pcb = td->td_pcb; 1020 pcb->pcb_gsbase = (register_t)info.base_addr; 1021 set_pcb_flags(pcb, PCB_32BIT); 1022 update_gdt_gsbase(td, info.base_addr); 1023 1024 return (0); 1025} 1026 1027int 1028linux_wait4(struct thread *td, struct linux_wait4_args *args) 1029{ 1030 int error, options; 1031 struct rusage ru, *rup; 1032 struct l_rusage lru; 1033 1034#ifdef DEBUG 1035 if (ldebug(wait4)) 1036 printf(ARGS(wait4, "%d, %p, %d, %p"), 1037 args->pid, (void *)args->status, args->options, 1038 (void *)args->rusage); 1039#endif 1040 1041 options = (args->options & (WNOHANG | WUNTRACED)); 1042 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */ 1043 if (args->options & __WCLONE) 1044 options |= WLINUXCLONE; 1045 1046 if (args->rusage != NULL) 1047 rup = &ru; 1048 else 1049 rup = NULL; 1050 error = linux_common_wait(td, args->pid, args->status, options, rup); 1051 if (error) 1052 return (error); 1053 if (args->rusage != NULL) { 1054 bsd_to_linux_rusage(rup, &lru); 1055 error = copyout(&lru, args->rusage, sizeof(lru)); 1056 } 1057 1058 return (error); 1059} 1060