1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 2004 Tim J. Robbins 5 * Copyright (c) 2002 Doug Rabson 6 * Copyright (c) 2000 Marcel Moolenaar 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer 14 * in this position and unchanged. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <sys/param.h> 34 #include <sys/fcntl.h> 35 #include <sys/imgact.h> 36 #include <sys/limits.h> 37 #include <sys/lock.h> 38 #include <sys/malloc.h> 39 #include <sys/mutex.h> 40 #include <sys/priv.h> 41 #include <sys/proc.h> 42 #include <sys/reg.h> 43 #include <sys/syscallsubr.h> 44 45 #include <machine/frame.h> 46 #include <machine/md_var.h> 47 #include <machine/pcb.h> 48 #include <machine/psl.h> 49 #include <machine/segments.h> 50 #include <machine/specialreg.h> 51 #include <x86/ifunc.h> 52 53 #include <vm/pmap.h> 54 #include <vm/vm.h> 55 #include <vm/vm_map.h> 56 57 #include <security/audit/audit.h> 58 59 #include <compat/freebsd32/freebsd32_util.h> 60 #include <amd64/linux32/linux.h> 61 #include <amd64/linux32/linux32_proto.h> 62 #include <compat/linux/linux_emul.h> 63 #include <compat/linux/linux_fork.h> 64 #include <compat/linux/linux_ipc.h> 65 #include <compat/linux/linux_mmap.h> 66 #include <compat/linux/linux_signal.h> 67 #include <compat/linux/linux_util.h> 68 69 static void bsd_to_linux_rusage(struct rusage *ru, struct l_rusage *lru); 70 71 struct l_old_select_argv { 72 l_int nfds; 73 l_uintptr_t readfds; 74 l_uintptr_t writefds; 75 l_uintptr_t exceptfds; 76 l_uintptr_t timeout; 77 } __packed; 78 79 static void 80 bsd_to_linux_rusage(struct rusage *ru, struct l_rusage *lru) 81 { 82 83 lru->ru_utime.tv_sec = ru->ru_utime.tv_sec; 84 lru->ru_utime.tv_usec = ru->ru_utime.tv_usec; 85 lru->ru_stime.tv_sec = ru->ru_stime.tv_sec; 86 lru->ru_stime.tv_usec = ru->ru_stime.tv_usec; 87 lru->ru_maxrss = ru->ru_maxrss; 88 lru->ru_ixrss = ru->ru_ixrss; 89 lru->ru_idrss = ru->ru_idrss; 90 lru->ru_isrss = ru->ru_isrss; 91 lru->ru_minflt = ru->ru_minflt; 92 lru->ru_majflt = ru->ru_majflt; 93 lru->ru_nswap = ru->ru_nswap; 94 lru->ru_inblock = ru->ru_inblock; 95 lru->ru_oublock = ru->ru_oublock; 96 lru->ru_msgsnd = ru->ru_msgsnd; 97 lru->ru_msgrcv = ru->ru_msgrcv; 98 lru->ru_nsignals = ru->ru_nsignals; 99 lru->ru_nvcsw = ru->ru_nvcsw; 100 lru->ru_nivcsw = ru->ru_nivcsw; 101 } 102 103 int 104 linux_copyout_rusage(struct rusage *ru, void *uaddr) 105 { 106 struct l_rusage lru; 107 108 bsd_to_linux_rusage(ru, &lru); 109 110 return (copyout(&lru, uaddr, sizeof(struct l_rusage))); 111 } 112 113 int 114 linux_readv(struct thread *td, struct linux_readv_args *uap) 115 { 116 struct uio *auio; 117 int error; 118 119 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio); 120 if (error) 121 return (error); 122 error = kern_readv(td, uap->fd, auio); 123 free(auio, M_IOV); 124 return (error); 125 } 126 127 struct l_ipc_kludge { 128 l_uintptr_t msgp; 129 l_long msgtyp; 130 } __packed; 131 132 int 133 linux_ipc(struct thread *td, struct linux_ipc_args *args) 134 { 135 136 switch (args->what & 0xFFFF) { 137 case LINUX_SEMOP: { 138 139 return (kern_semop(td, args->arg1, PTRIN(args->ptr), 140 args->arg2, NULL)); 141 } 142 case LINUX_SEMGET: { 143 struct linux_semget_args a; 144 145 a.key = args->arg1; 146 a.nsems = args->arg2; 147 a.semflg = args->arg3; 148 return (linux_semget(td, &a)); 149 } 150 case LINUX_SEMCTL: { 151 struct linux_semctl_args a; 152 int error; 153 154 a.semid = args->arg1; 155 a.semnum = args->arg2; 156 a.cmd = args->arg3; 157 error = copyin(PTRIN(args->ptr), &a.arg, sizeof(a.arg)); 158 if (error) 159 return (error); 160 return (linux_semctl(td, &a)); 161 } 162 case LINUX_SEMTIMEDOP: { 163 struct linux_semtimedop_args a; 164 165 a.semid = args->arg1; 166 a.tsops = PTRIN(args->ptr); 167 a.nsops = args->arg2; 168 a.timeout = PTRIN(args->arg5); 169 return (linux_semtimedop(td, &a)); 170 } 171 case LINUX_MSGSND: { 172 struct linux_msgsnd_args a; 173 174 a.msqid = args->arg1; 175 a.msgp = PTRIN(args->ptr); 176 a.msgsz = args->arg2; 177 a.msgflg = args->arg3; 178 return (linux_msgsnd(td, &a)); 179 } 180 case LINUX_MSGRCV: { 181 struct linux_msgrcv_args a; 182 183 a.msqid = args->arg1; 184 a.msgsz = args->arg2; 185 a.msgflg = args->arg3; 186 if ((args->what >> 16) == 0) { 187 struct l_ipc_kludge tmp; 188 int error; 189 190 if (args->ptr == 0) 191 return (EINVAL); 192 error = copyin(PTRIN(args->ptr), &tmp, sizeof(tmp)); 193 if (error) 194 return (error); 195 a.msgp = PTRIN(tmp.msgp); 196 a.msgtyp = tmp.msgtyp; 197 } else { 198 a.msgp = PTRIN(args->ptr); 199 a.msgtyp = args->arg5; 200 } 201 return (linux_msgrcv(td, &a)); 202 } 203 case LINUX_MSGGET: { 204 struct linux_msgget_args a; 205 206 a.key = args->arg1; 207 a.msgflg = args->arg2; 208 return (linux_msgget(td, &a)); 209 } 210 case LINUX_MSGCTL: { 211 struct linux_msgctl_args a; 212 213 a.msqid = args->arg1; 214 a.cmd = args->arg2; 215 a.buf = PTRIN(args->ptr); 216 return (linux_msgctl(td, &a)); 217 } 218 case LINUX_SHMAT: { 219 struct linux_shmat_args a; 220 l_uintptr_t addr; 221 int error; 222 223 a.shmid = args->arg1; 224 a.shmaddr = PTRIN(args->ptr); 225 a.shmflg = args->arg2; 226 error = linux_shmat(td, &a); 227 if (error != 0) 228 return (error); 229 addr = td->td_retval[0]; 230 error = copyout(&addr, PTRIN(args->arg3), sizeof(addr)); 231 td->td_retval[0] = 0; 232 return (error); 233 } 234 case LINUX_SHMDT: { 235 struct linux_shmdt_args a; 236 237 a.shmaddr = PTRIN(args->ptr); 238 return (linux_shmdt(td, &a)); 239 } 240 case LINUX_SHMGET: { 241 struct linux_shmget_args a; 242 243 a.key = args->arg1; 244 a.size = args->arg2; 245 a.shmflg = args->arg3; 246 return (linux_shmget(td, &a)); 247 } 248 case LINUX_SHMCTL: { 249 struct linux_shmctl_args a; 250 251 a.shmid = args->arg1; 252 a.cmd = args->arg2; 253 a.buf = PTRIN(args->ptr); 254 return (linux_shmctl(td, &a)); 255 } 256 default: 257 break; 258 } 259 260 return (EINVAL); 261 } 262 263 int 264 linux_old_select(struct thread *td, struct linux_old_select_args *args) 265 { 266 struct l_old_select_argv linux_args; 267 struct linux_select_args newsel; 268 int error; 269 270 error = copyin(args->ptr, &linux_args, sizeof(linux_args)); 271 if (error) 272 return (error); 273 274 newsel.nfds = linux_args.nfds; 275 newsel.readfds = PTRIN(linux_args.readfds); 276 newsel.writefds = PTRIN(linux_args.writefds); 277 newsel.exceptfds = PTRIN(linux_args.exceptfds); 278 newsel.timeout = PTRIN(linux_args.timeout); 279 return (linux_select(td, &newsel)); 280 } 281 282 int 283 linux_set_cloned_tls(struct thread *td, void *desc) 284 { 285 struct l_user_desc info; 286 struct pcb *pcb; 287 int error; 288 289 error = copyin(desc, &info, sizeof(struct l_user_desc)); 290 if (error) { 291 linux_msg(td, "set_cloned_tls copyin info failed!"); 292 } else { 293 /* We might copy out the entry_number as GUGS32_SEL. */ 294 info.entry_number = GUGS32_SEL; 295 error = copyout(&info, desc, sizeof(struct l_user_desc)); 296 if (error) 297 linux_msg(td, "set_cloned_tls copyout info failed!"); 298 299 pcb = td->td_pcb; 300 update_pcb_bases(pcb); 301 pcb->pcb_gsbase = (register_t)info.base_addr; 302 td->td_frame->tf_gs = GSEL(GUGS32_SEL, SEL_UPL); 303 } 304 305 return (error); 306 } 307 308 int 309 linux_set_upcall(struct thread *td, register_t stack) 310 { 311 312 if (stack) 313 td->td_frame->tf_rsp = stack; 314 315 /* 316 * The newly created Linux thread returns 317 * to the user space by the same path that a parent do. 318 */ 319 td->td_frame->tf_rax = 0; 320 return (0); 321 } 322 323 int 324 linux_mmap(struct thread *td, struct linux_mmap_args *args) 325 { 326 int error; 327 struct l_mmap_argv linux_args; 328 329 error = copyin(args->ptr, &linux_args, sizeof(linux_args)); 330 if (error) 331 return (error); 332 333 return (linux_mmap_common(td, linux_args.addr, linux_args.len, 334 linux_args.prot, linux_args.flags, linux_args.fd, 335 (uint32_t)linux_args.pgoff)); 336 } 337 338 int 339 linux_iopl(struct thread *td, struct linux_iopl_args *args) 340 { 341 int error; 342 343 if (args->level < 0 || args->level > 3) 344 return (EINVAL); 345 if ((error = priv_check(td, PRIV_IO)) != 0) 346 return (error); 347 if ((error = securelevel_gt(td->td_ucred, 0)) != 0) 348 return (error); 349 td->td_frame->tf_rflags = (td->td_frame->tf_rflags & ~PSL_IOPL) | 350 (args->level * (PSL_IOPL / 3)); 351 352 return (0); 353 } 354 355 int 356 linux_sigaction(struct thread *td, struct linux_sigaction_args *args) 357 { 358 l_osigaction_t osa; 359 l_sigaction_t act, oact; 360 int error; 361 362 if (args->nsa != NULL) { 363 error = copyin(args->nsa, &osa, sizeof(l_osigaction_t)); 364 if (error) 365 return (error); 366 act.lsa_handler = osa.lsa_handler; 367 act.lsa_flags = osa.lsa_flags; 368 act.lsa_restorer = osa.lsa_restorer; 369 LINUX_SIGEMPTYSET(act.lsa_mask); 370 act.lsa_mask.__mask = osa.lsa_mask; 371 } 372 373 error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL, 374 args->osa ? &oact : NULL); 375 376 if (args->osa != NULL && !error) { 377 osa.lsa_handler = oact.lsa_handler; 378 osa.lsa_flags = oact.lsa_flags; 379 osa.lsa_restorer = oact.lsa_restorer; 380 osa.lsa_mask = oact.lsa_mask.__mask; 381 error = copyout(&osa, args->osa, sizeof(l_osigaction_t)); 382 } 383 384 return (error); 385 } 386 387 /* 388 * Linux has two extra args, restart and oldmask. We don't use these, 389 * but it seems that "restart" is actually a context pointer that 390 * enables the signal to happen with a different register set. 391 */ 392 int 393 linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args) 394 { 395 sigset_t sigmask; 396 l_sigset_t mask; 397 398 LINUX_SIGEMPTYSET(mask); 399 mask.__mask = args->mask; 400 linux_to_bsd_sigset(&mask, &sigmask); 401 return (kern_sigsuspend(td, sigmask)); 402 } 403 404 int 405 linux_pause(struct thread *td, struct linux_pause_args *args) 406 { 407 struct proc *p = td->td_proc; 408 sigset_t sigmask; 409 410 PROC_LOCK(p); 411 sigmask = td->td_sigmask; 412 PROC_UNLOCK(p); 413 return (kern_sigsuspend(td, sigmask)); 414 } 415 416 int 417 linux_gettimeofday(struct thread *td, struct linux_gettimeofday_args *uap) 418 { 419 struct timeval atv; 420 l_timeval atv32; 421 struct timezone rtz; 422 int error = 0; 423 424 if (uap->tp) { 425 microtime(&atv); 426 atv32.tv_sec = atv.tv_sec; 427 atv32.tv_usec = atv.tv_usec; 428 error = copyout(&atv32, uap->tp, sizeof(atv32)); 429 } 430 if (error == 0 && uap->tzp != NULL) { 431 rtz.tz_minuteswest = 0; 432 rtz.tz_dsttime = 0; 433 error = copyout(&rtz, uap->tzp, sizeof(rtz)); 434 } 435 return (error); 436 } 437 438 int 439 linux_settimeofday(struct thread *td, struct linux_settimeofday_args *uap) 440 { 441 l_timeval atv32; 442 struct timeval atv, *tvp; 443 struct timezone atz, *tzp; 444 int error; 445 446 if (uap->tp) { 447 error = copyin(uap->tp, &atv32, sizeof(atv32)); 448 if (error) 449 return (error); 450 atv.tv_sec = atv32.tv_sec; 451 atv.tv_usec = atv32.tv_usec; 452 tvp = &atv; 453 } else 454 tvp = NULL; 455 if (uap->tzp) { 456 error = copyin(uap->tzp, &atz, sizeof(atz)); 457 if (error) 458 return (error); 459 tzp = &atz; 460 } else 461 tzp = NULL; 462 return (kern_settimeofday(td, tvp, tzp)); 463 } 464 465 int 466 linux_getrusage(struct thread *td, struct linux_getrusage_args *uap) 467 { 468 struct rusage s; 469 int error; 470 471 error = kern_getrusage(td, uap->who, &s); 472 if (error != 0) 473 return (error); 474 if (uap->rusage != NULL) 475 error = linux_copyout_rusage(&s, uap->rusage); 476 return (error); 477 } 478 479 int 480 linux_set_thread_area(struct thread *td, 481 struct linux_set_thread_area_args *args) 482 { 483 struct l_user_desc info; 484 struct pcb *pcb; 485 int error; 486 487 error = copyin(args->desc, &info, sizeof(struct l_user_desc)); 488 if (error) 489 return (error); 490 491 /* 492 * Semantics of Linux version: every thread in the system has array 493 * of three TLS descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown. 494 * This syscall loads one of the selected TLS descriptors with a value 495 * and also loads GDT descriptors 6, 7 and 8 with the content of 496 * the per-thread descriptors. 497 * 498 * Semantics of FreeBSD version: I think we can ignore that Linux has 499 * three per-thread descriptors and use just the first one. 500 * The tls_array[] is used only in [gs]et_thread_area() syscalls and 501 * for loading the GDT descriptors. We use just one GDT descriptor 502 * for TLS, so we will load just one. 503 * 504 * XXX: This doesn't work when a user space process tries to use more 505 * than one TLS segment. Comment in the Linux source says wine might 506 * do this. 507 */ 508 509 /* 510 * GLIBC reads current %gs and call set_thread_area() with it. 511 * We should let GUDATA_SEL and GUGS32_SEL proceed as well because 512 * we use these segments. 513 */ 514 switch (info.entry_number) { 515 case GUGS32_SEL: 516 case GUDATA_SEL: 517 case 6: 518 case -1: 519 info.entry_number = GUGS32_SEL; 520 break; 521 default: 522 return (EINVAL); 523 } 524 525 /* 526 * We have to copy out the GDT entry we use. 527 * 528 * XXX: What if a user space program does not check the return value 529 * and tries to use 6, 7 or 8? 530 */ 531 error = copyout(&info, args->desc, sizeof(struct l_user_desc)); 532 if (error) 533 return (error); 534 535 pcb = td->td_pcb; 536 update_pcb_bases(pcb); 537 pcb->pcb_gsbase = (register_t)info.base_addr; 538 update_gdt_gsbase(td, info.base_addr); 539 540 return (0); 541 } 542 543 void 544 bsd_to_linux_regset32(const struct reg32 *b_reg, 545 struct linux_pt_regset32 *l_regset) 546 { 547 548 l_regset->ebx = b_reg->r_ebx; 549 l_regset->ecx = b_reg->r_ecx; 550 l_regset->edx = b_reg->r_edx; 551 l_regset->esi = b_reg->r_esi; 552 l_regset->edi = b_reg->r_edi; 553 l_regset->ebp = b_reg->r_ebp; 554 l_regset->eax = b_reg->r_eax; 555 l_regset->ds = b_reg->r_ds; 556 l_regset->es = b_reg->r_es; 557 l_regset->fs = b_reg->r_fs; 558 l_regset->gs = b_reg->r_gs; 559 l_regset->orig_eax = b_reg->r_eax; 560 l_regset->eip = b_reg->r_eip; 561 l_regset->cs = b_reg->r_cs; 562 l_regset->eflags = b_reg->r_eflags; 563 l_regset->esp = b_reg->r_esp; 564 l_regset->ss = b_reg->r_ss; 565 } 566 567 int futex_xchgl_nosmap(int oparg, uint32_t *uaddr, int *oldval); 568 int futex_xchgl_smap(int oparg, uint32_t *uaddr, int *oldval); 569 DEFINE_IFUNC(, int, futex_xchgl, (int, uint32_t *, int *)) 570 { 571 572 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ? 573 futex_xchgl_smap : futex_xchgl_nosmap); 574 } 575 576 int futex_addl_nosmap(int oparg, uint32_t *uaddr, int *oldval); 577 int futex_addl_smap(int oparg, uint32_t *uaddr, int *oldval); 578 DEFINE_IFUNC(, int, futex_addl, (int, uint32_t *, int *)) 579 { 580 581 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ? 582 futex_addl_smap : futex_addl_nosmap); 583 } 584 585 int futex_orl_nosmap(int oparg, uint32_t *uaddr, int *oldval); 586 int futex_orl_smap(int oparg, uint32_t *uaddr, int *oldval); 587 DEFINE_IFUNC(, int, futex_orl, (int, uint32_t *, int *)) 588 { 589 590 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ? 591 futex_orl_smap : futex_orl_nosmap); 592 } 593 594 int futex_andl_nosmap(int oparg, uint32_t *uaddr, int *oldval); 595 int futex_andl_smap(int oparg, uint32_t *uaddr, int *oldval); 596 DEFINE_IFUNC(, int, futex_andl, (int, uint32_t *, int *)) 597 { 598 599 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ? 600 futex_andl_smap : futex_andl_nosmap); 601 } 602 603 int futex_xorl_nosmap(int oparg, uint32_t *uaddr, int *oldval); 604 int futex_xorl_smap(int oparg, uint32_t *uaddr, int *oldval); 605 DEFINE_IFUNC(, int, futex_xorl, (int, uint32_t *, int *)) 606 { 607 608 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ? 609 futex_xorl_smap : futex_xorl_nosmap); 610 } 611 612 int 613 linux_ptrace_peekuser(struct thread *td, pid_t pid, void *addr, void *data) 614 { 615 616 LINUX_RATELIMIT_MSG_OPT1("PTRACE_PEEKUSER offset %ld not implemented; " 617 "returning EINVAL", (uintptr_t)addr); 618 return (EINVAL); 619 } 620 621 int 622 linux_ptrace_pokeuser(struct thread *td, pid_t pid, void *addr, void *data) 623 { 624 625 LINUX_RATELIMIT_MSG_OPT1("PTRACE_POKEUSER offset %ld " 626 "not implemented; returning EINVAL", (uintptr_t)addr); 627 return (EINVAL); 628 } 629