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