1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 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 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/capsicum.h> 34 #include <sys/fcntl.h> 35 #include <sys/file.h> 36 #include <sys/lock.h> 37 #include <sys/malloc.h> 38 #include <sys/mman.h> 39 #include <sys/mutex.h> 40 #include <sys/priv.h> 41 #include <sys/proc.h> 42 #include <sys/queue.h> 43 #include <sys/resource.h> 44 #include <sys/resourcevar.h> 45 #include <sys/sched.h> 46 #include <sys/signalvar.h> 47 #include <sys/syscallsubr.h> 48 #include <sys/sysproto.h> 49 #include <sys/systm.h> 50 #include <sys/sx.h> 51 #include <sys/unistd.h> 52 #include <sys/wait.h> 53 54 #include <machine/frame.h> 55 #include <machine/psl.h> 56 #include <machine/segments.h> 57 #include <machine/sysarch.h> 58 59 #include <vm/pmap.h> 60 #include <vm/vm.h> 61 #include <vm/vm_map.h> 62 63 #include <x86/reg.h> 64 65 #include <i386/linux/linux.h> 66 #include <i386/linux/linux_proto.h> 67 #include <compat/linux/linux_emul.h> 68 #include <compat/linux/linux_fork.h> 69 #include <compat/linux/linux_ipc.h> 70 #include <compat/linux/linux_misc.h> 71 #include <compat/linux/linux_mmap.h> 72 #include <compat/linux/linux_signal.h> 73 #include <compat/linux/linux_util.h> 74 75 #include <i386/include/pcb.h> /* needed for pcb definition in linux_set_thread_area */ 76 77 #include "opt_posix.h" 78 79 struct l_descriptor { 80 l_uint entry_number; 81 l_ulong base_addr; 82 l_uint limit; 83 l_uint seg_32bit:1; 84 l_uint contents:2; 85 l_uint read_exec_only:1; 86 l_uint limit_in_pages:1; 87 l_uint seg_not_present:1; 88 l_uint useable:1; 89 }; 90 91 struct l_old_select_argv { 92 l_int nfds; 93 l_fd_set *readfds; 94 l_fd_set *writefds; 95 l_fd_set *exceptfds; 96 struct l_timeval *timeout; 97 }; 98 99 struct l_ipc_kludge { 100 struct l_msgbuf *msgp; 101 l_long msgtyp; 102 }; 103 104 int 105 linux_ipc(struct thread *td, struct linux_ipc_args *args) 106 { 107 108 switch (args->what & 0xFFFF) { 109 case LINUX_SEMOP: { 110 111 return (kern_semop(td, args->arg1, PTRIN(args->ptr), 112 args->arg2, NULL)); 113 } 114 case LINUX_SEMGET: { 115 struct linux_semget_args a; 116 117 a.key = args->arg1; 118 a.nsems = args->arg2; 119 a.semflg = args->arg3; 120 return (linux_semget(td, &a)); 121 } 122 case LINUX_SEMCTL: { 123 struct linux_semctl_args a; 124 int error; 125 126 a.semid = args->arg1; 127 a.semnum = args->arg2; 128 a.cmd = args->arg3; 129 error = copyin(PTRIN(args->ptr), &a.arg, sizeof(a.arg)); 130 if (error) 131 return (error); 132 return (linux_semctl(td, &a)); 133 } 134 case LINUX_SEMTIMEDOP: { 135 struct linux_semtimedop_args a; 136 137 a.semid = args->arg1; 138 a.tsops = PTRIN(args->ptr); 139 a.nsops = args->arg2; 140 a.timeout = PTRIN(args->arg5); 141 return (linux_semtimedop(td, &a)); 142 } 143 case LINUX_MSGSND: { 144 struct linux_msgsnd_args a; 145 146 a.msqid = args->arg1; 147 a.msgp = PTRIN(args->ptr); 148 a.msgsz = args->arg2; 149 a.msgflg = args->arg3; 150 return (linux_msgsnd(td, &a)); 151 } 152 case LINUX_MSGRCV: { 153 struct linux_msgrcv_args a; 154 155 a.msqid = args->arg1; 156 a.msgsz = args->arg2; 157 a.msgflg = args->arg3; 158 if ((args->what >> 16) == 0) { 159 struct l_ipc_kludge tmp; 160 int error; 161 162 if (args->ptr == 0) 163 return (EINVAL); 164 error = copyin(PTRIN(args->ptr), &tmp, sizeof(tmp)); 165 if (error) 166 return (error); 167 a.msgp = PTRIN(tmp.msgp); 168 a.msgtyp = tmp.msgtyp; 169 } else { 170 a.msgp = PTRIN(args->ptr); 171 a.msgtyp = args->arg5; 172 } 173 return (linux_msgrcv(td, &a)); 174 } 175 case LINUX_MSGGET: { 176 struct linux_msgget_args a; 177 178 a.key = args->arg1; 179 a.msgflg = args->arg2; 180 return (linux_msgget(td, &a)); 181 } 182 case LINUX_MSGCTL: { 183 struct linux_msgctl_args a; 184 185 a.msqid = args->arg1; 186 a.cmd = args->arg2; 187 a.buf = PTRIN(args->ptr); 188 return (linux_msgctl(td, &a)); 189 } 190 case LINUX_SHMAT: { 191 struct linux_shmat_args a; 192 l_uintptr_t addr; 193 int error; 194 195 a.shmid = args->arg1; 196 a.shmaddr = PTRIN(args->ptr); 197 a.shmflg = args->arg2; 198 error = linux_shmat(td, &a); 199 if (error != 0) 200 return (error); 201 addr = td->td_retval[0]; 202 error = copyout(&addr, PTRIN(args->arg3), sizeof(addr)); 203 td->td_retval[0] = 0; 204 return (error); 205 } 206 case LINUX_SHMDT: { 207 struct linux_shmdt_args a; 208 209 a.shmaddr = PTRIN(args->ptr); 210 return (linux_shmdt(td, &a)); 211 } 212 case LINUX_SHMGET: { 213 struct linux_shmget_args a; 214 215 a.key = args->arg1; 216 a.size = args->arg2; 217 a.shmflg = args->arg3; 218 return (linux_shmget(td, &a)); 219 } 220 case LINUX_SHMCTL: { 221 struct linux_shmctl_args a; 222 223 a.shmid = args->arg1; 224 a.cmd = args->arg2; 225 a.buf = PTRIN(args->ptr); 226 return (linux_shmctl(td, &a)); 227 } 228 default: 229 break; 230 } 231 232 return (EINVAL); 233 } 234 235 int 236 linux_old_select(struct thread *td, struct linux_old_select_args *args) 237 { 238 struct l_old_select_argv linux_args; 239 struct linux_select_args newsel; 240 int error; 241 242 error = copyin(args->ptr, &linux_args, sizeof(linux_args)); 243 if (error) 244 return (error); 245 246 newsel.nfds = linux_args.nfds; 247 newsel.readfds = linux_args.readfds; 248 newsel.writefds = linux_args.writefds; 249 newsel.exceptfds = linux_args.exceptfds; 250 newsel.timeout = linux_args.timeout; 251 return (linux_select(td, &newsel)); 252 } 253 254 int 255 linux_set_cloned_tls(struct thread *td, void *desc) 256 { 257 struct segment_descriptor sd; 258 struct l_user_desc info; 259 int idx, error; 260 int a[2]; 261 262 error = copyin(desc, &info, sizeof(struct l_user_desc)); 263 if (error) { 264 linux_msg(td, "set_cloned_tls copyin failed!"); 265 } else { 266 idx = info.entry_number; 267 268 /* 269 * looks like we're getting the idx we returned 270 * in the set_thread_area() syscall 271 */ 272 if (idx != 6 && idx != 3) { 273 linux_msg(td, "set_cloned_tls resetting idx!"); 274 idx = 3; 275 } 276 277 /* this doesnt happen in practice */ 278 if (idx == 6) { 279 /* we might copy out the entry_number as 3 */ 280 info.entry_number = 3; 281 error = copyout(&info, desc, sizeof(struct l_user_desc)); 282 if (error) 283 linux_msg(td, "set_cloned_tls copyout failed!"); 284 } 285 286 a[0] = LINUX_LDT_entry_a(&info); 287 a[1] = LINUX_LDT_entry_b(&info); 288 289 memcpy(&sd, &a, sizeof(a)); 290 /* set %gs */ 291 td->td_pcb->pcb_gsd = sd; 292 td->td_pcb->pcb_gs = GSEL(GUGS_SEL, SEL_UPL); 293 } 294 295 return (error); 296 } 297 298 int 299 linux_set_upcall(struct thread *td, register_t stack) 300 { 301 302 if (stack) 303 td->td_frame->tf_esp = stack; 304 305 /* 306 * The newly created Linux thread returns 307 * to the user space by the same path that a parent do. 308 */ 309 td->td_frame->tf_eax = 0; 310 return (0); 311 } 312 313 int 314 linux_mmap2(struct thread *td, struct linux_mmap2_args *args) 315 { 316 317 return (linux_mmap_common(td, args->addr, args->len, args->prot, 318 args->flags, args->fd, (uint64_t)(uint32_t)args->pgoff * 319 PAGE_SIZE)); 320 } 321 322 int 323 linux_mmap(struct thread *td, struct linux_mmap_args *args) 324 { 325 int error; 326 struct l_mmap_argv linux_args; 327 328 error = copyin(args->ptr, &linux_args, sizeof(linux_args)); 329 if (error) 330 return (error); 331 332 return (linux_mmap_common(td, linux_args.addr, linux_args.len, 333 linux_args.prot, linux_args.flags, linux_args.fd, 334 (uint32_t)linux_args.pgoff)); 335 } 336 337 int 338 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap) 339 { 340 341 return (linux_mprotect_common(td, PTROUT(uap->addr), uap->len, uap->prot)); 342 } 343 344 int 345 linux_madvise(struct thread *td, struct linux_madvise_args *uap) 346 { 347 348 return (linux_madvise_common(td, PTROUT(uap->addr), uap->len, uap->behav)); 349 } 350 351 int 352 linux_ioperm(struct thread *td, struct linux_ioperm_args *args) 353 { 354 int error; 355 struct i386_ioperm_args iia; 356 357 iia.start = args->start; 358 iia.length = args->length; 359 iia.enable = args->enable; 360 error = i386_set_ioperm(td, &iia); 361 return (error); 362 } 363 364 int 365 linux_iopl(struct thread *td, struct linux_iopl_args *args) 366 { 367 int error; 368 369 if (args->level < 0 || args->level > 3) 370 return (EINVAL); 371 if ((error = priv_check(td, PRIV_IO)) != 0) 372 return (error); 373 if ((error = securelevel_gt(td->td_ucred, 0)) != 0) 374 return (error); 375 td->td_frame->tf_eflags = (td->td_frame->tf_eflags & ~PSL_IOPL) | 376 (args->level * (PSL_IOPL / 3)); 377 return (0); 378 } 379 380 int 381 linux_modify_ldt(struct thread *td, struct linux_modify_ldt_args *uap) 382 { 383 int error; 384 struct i386_ldt_args ldt; 385 struct l_descriptor ld; 386 union descriptor desc; 387 int size, written; 388 389 switch (uap->func) { 390 case 0x00: /* read_ldt */ 391 ldt.start = 0; 392 ldt.descs = uap->ptr; 393 ldt.num = uap->bytecount / sizeof(union descriptor); 394 error = i386_get_ldt(td, &ldt); 395 td->td_retval[0] *= sizeof(union descriptor); 396 break; 397 case 0x02: /* read_default_ldt = 0 */ 398 size = 5*sizeof(struct l_desc_struct); 399 if (size > uap->bytecount) 400 size = uap->bytecount; 401 for (written = error = 0; written < size && error == 0; written++) 402 error = subyte((char *)uap->ptr + written, 0); 403 td->td_retval[0] = written; 404 break; 405 case 0x01: /* write_ldt */ 406 case 0x11: /* write_ldt */ 407 if (uap->bytecount != sizeof(ld)) 408 return (EINVAL); 409 410 error = copyin(uap->ptr, &ld, sizeof(ld)); 411 if (error) 412 return (error); 413 414 ldt.start = ld.entry_number; 415 ldt.descs = &desc; 416 ldt.num = 1; 417 desc.sd.sd_lolimit = (ld.limit & 0x0000ffff); 418 desc.sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16; 419 desc.sd.sd_lobase = (ld.base_addr & 0x00ffffff); 420 desc.sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24; 421 desc.sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) | 422 (ld.contents << 2); 423 desc.sd.sd_dpl = 3; 424 desc.sd.sd_p = (ld.seg_not_present ^ 1); 425 desc.sd.sd_xx = 0; 426 desc.sd.sd_def32 = ld.seg_32bit; 427 desc.sd.sd_gran = ld.limit_in_pages; 428 error = i386_set_ldt(td, &ldt, &desc); 429 break; 430 default: 431 error = ENOSYS; 432 break; 433 } 434 435 if (error == EOPNOTSUPP) { 436 linux_msg(td, "modify_ldt needs kernel option USER_LDT"); 437 error = ENOSYS; 438 } 439 440 return (error); 441 } 442 443 int 444 linux_sigaction(struct thread *td, struct linux_sigaction_args *args) 445 { 446 l_osigaction_t osa; 447 l_sigaction_t act, oact; 448 int error; 449 450 if (args->nsa != NULL) { 451 error = copyin(args->nsa, &osa, sizeof(l_osigaction_t)); 452 if (error) 453 return (error); 454 act.lsa_handler = osa.lsa_handler; 455 act.lsa_flags = osa.lsa_flags; 456 act.lsa_restorer = osa.lsa_restorer; 457 LINUX_SIGEMPTYSET(act.lsa_mask); 458 act.lsa_mask.__mask = osa.lsa_mask; 459 } 460 461 error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL, 462 args->osa ? &oact : NULL); 463 464 if (args->osa != NULL && !error) { 465 osa.lsa_handler = oact.lsa_handler; 466 osa.lsa_flags = oact.lsa_flags; 467 osa.lsa_restorer = oact.lsa_restorer; 468 osa.lsa_mask = oact.lsa_mask.__mask; 469 error = copyout(&osa, args->osa, sizeof(l_osigaction_t)); 470 } 471 472 return (error); 473 } 474 475 /* 476 * Linux has two extra args, restart and oldmask. We dont use these, 477 * but it seems that "restart" is actually a context pointer that 478 * enables the signal to happen with a different register set. 479 */ 480 int 481 linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args) 482 { 483 sigset_t sigmask; 484 l_sigset_t mask; 485 486 LINUX_SIGEMPTYSET(mask); 487 mask.__mask = args->mask; 488 linux_to_bsd_sigset(&mask, &sigmask); 489 return (kern_sigsuspend(td, sigmask)); 490 } 491 492 int 493 linux_pause(struct thread *td, struct linux_pause_args *args) 494 { 495 struct proc *p = td->td_proc; 496 sigset_t sigmask; 497 498 PROC_LOCK(p); 499 sigmask = td->td_sigmask; 500 PROC_UNLOCK(p); 501 return (kern_sigsuspend(td, sigmask)); 502 } 503 504 int 505 linux_set_thread_area(struct thread *td, struct linux_set_thread_area_args *args) 506 { 507 struct l_user_desc info; 508 int error; 509 int idx; 510 int a[2]; 511 struct segment_descriptor sd; 512 513 error = copyin(args->desc, &info, sizeof(struct l_user_desc)); 514 if (error) 515 return (error); 516 517 idx = info.entry_number; 518 /* 519 * Semantics of Linux version: every thread in the system has array of 520 * 3 tls descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown. This 521 * syscall loads one of the selected tls decriptors with a value and 522 * also loads GDT descriptors 6, 7 and 8 with the content of the 523 * per-thread descriptors. 524 * 525 * Semantics of FreeBSD version: I think we can ignore that Linux has 3 526 * per-thread descriptors and use just the 1st one. The tls_array[] 527 * is used only in set/get-thread_area() syscalls and for loading the 528 * GDT descriptors. In FreeBSD we use just one GDT descriptor for TLS 529 * so we will load just one. 530 * 531 * XXX: this doesn't work when a user space process tries to use more 532 * than 1 TLS segment. Comment in the Linux sources says wine might do 533 * this. 534 */ 535 536 /* 537 * we support just GLIBC TLS now 538 * we should let 3 proceed as well because we use this segment so 539 * if code does two subsequent calls it should succeed 540 */ 541 if (idx != 6 && idx != -1 && idx != 3) 542 return (EINVAL); 543 544 /* 545 * we have to copy out the GDT entry we use 546 * FreeBSD uses GDT entry #3 for storing %gs so load that 547 * 548 * XXX: what if a user space program doesn't check this value and tries 549 * to use 6, 7 or 8? 550 */ 551 idx = info.entry_number = 3; 552 error = copyout(&info, args->desc, sizeof(struct l_user_desc)); 553 if (error) 554 return (error); 555 556 if (LINUX_LDT_empty(&info)) { 557 a[0] = 0; 558 a[1] = 0; 559 } else { 560 a[0] = LINUX_LDT_entry_a(&info); 561 a[1] = LINUX_LDT_entry_b(&info); 562 } 563 564 memcpy(&sd, &a, sizeof(a)); 565 /* this is taken from i386 version of cpu_set_user_tls() */ 566 critical_enter(); 567 /* set %gs */ 568 td->td_pcb->pcb_gsd = sd; 569 PCPU_GET(fsgs_gdt)[1] = sd; 570 load_gs(GSEL(GUGS_SEL, SEL_UPL)); 571 critical_exit(); 572 573 return (0); 574 } 575 576 int 577 linux_get_thread_area(struct thread *td, struct linux_get_thread_area_args *args) 578 { 579 580 struct l_user_desc info; 581 int error; 582 int idx; 583 struct l_desc_struct desc; 584 struct segment_descriptor sd; 585 586 error = copyin(args->desc, &info, sizeof(struct l_user_desc)); 587 if (error) 588 return (error); 589 590 idx = info.entry_number; 591 /* XXX: I am not sure if we want 3 to be allowed too. */ 592 if (idx != 6 && idx != 3) 593 return (EINVAL); 594 595 idx = 3; 596 597 memset(&info, 0, sizeof(info)); 598 599 sd = PCPU_GET(fsgs_gdt)[1]; 600 601 memcpy(&desc, &sd, sizeof(desc)); 602 603 info.entry_number = idx; 604 info.base_addr = LINUX_GET_BASE(&desc); 605 info.limit = LINUX_GET_LIMIT(&desc); 606 info.seg_32bit = LINUX_GET_32BIT(&desc); 607 info.contents = LINUX_GET_CONTENTS(&desc); 608 info.read_exec_only = !LINUX_GET_WRITABLE(&desc); 609 info.limit_in_pages = LINUX_GET_LIMIT_PAGES(&desc); 610 info.seg_not_present = !LINUX_GET_PRESENT(&desc); 611 info.useable = LINUX_GET_USEABLE(&desc); 612 613 error = copyout(&info, args->desc, sizeof(struct l_user_desc)); 614 if (error) 615 return (EFAULT); 616 617 return (0); 618 } 619 620 /* XXX: this wont work with module - convert it */ 621 int 622 linux_mq_open(struct thread *td, struct linux_mq_open_args *args) 623 { 624 #ifdef P1003_1B_MQUEUE 625 return (sys_kmq_open(td, (struct kmq_open_args *)args)); 626 #else 627 return (ENOSYS); 628 #endif 629 } 630 631 int 632 linux_mq_unlink(struct thread *td, struct linux_mq_unlink_args *args) 633 { 634 #ifdef P1003_1B_MQUEUE 635 return (sys_kmq_unlink(td, (struct kmq_unlink_args *)args)); 636 #else 637 return (ENOSYS); 638 #endif 639 } 640 641 int 642 linux_mq_timedsend(struct thread *td, struct linux_mq_timedsend_args *args) 643 { 644 #ifdef P1003_1B_MQUEUE 645 return (sys_kmq_timedsend(td, (struct kmq_timedsend_args *)args)); 646 #else 647 return (ENOSYS); 648 #endif 649 } 650 651 int 652 linux_mq_timedreceive(struct thread *td, struct linux_mq_timedreceive_args *args) 653 { 654 #ifdef P1003_1B_MQUEUE 655 return (sys_kmq_timedreceive(td, (struct kmq_timedreceive_args *)args)); 656 #else 657 return (ENOSYS); 658 #endif 659 } 660 661 int 662 linux_mq_notify(struct thread *td, struct linux_mq_notify_args *args) 663 { 664 #ifdef P1003_1B_MQUEUE 665 return (sys_kmq_notify(td, (struct kmq_notify_args *)args)); 666 #else 667 return (ENOSYS); 668 #endif 669 } 670 671 int 672 linux_mq_getsetattr(struct thread *td, struct linux_mq_getsetattr_args *args) 673 { 674 #ifdef P1003_1B_MQUEUE 675 return (sys_kmq_setattr(td, (struct kmq_setattr_args *)args)); 676 #else 677 return (ENOSYS); 678 #endif 679 } 680 681 void 682 bsd_to_linux_regset(const struct reg *b_reg, 683 struct linux_pt_regset *l_regset) 684 { 685 686 l_regset->ebx = b_reg->r_ebx; 687 l_regset->ecx = b_reg->r_ecx; 688 l_regset->edx = b_reg->r_edx; 689 l_regset->esi = b_reg->r_esi; 690 l_regset->edi = b_reg->r_edi; 691 l_regset->ebp = b_reg->r_ebp; 692 l_regset->eax = b_reg->r_eax; 693 l_regset->ds = b_reg->r_ds; 694 l_regset->es = b_reg->r_es; 695 l_regset->fs = b_reg->r_fs; 696 l_regset->gs = b_reg->r_gs; 697 l_regset->orig_eax = b_reg->r_eax; 698 l_regset->eip = b_reg->r_eip; 699 l_regset->cs = b_reg->r_cs; 700 l_regset->eflags = b_reg->r_eflags; 701 l_regset->esp = b_reg->r_esp; 702 l_regset->ss = b_reg->r_ss; 703 } 704