1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 2002 Doug Rabson 5 * Copyright (c) 1994-1995 Søren Schmidt 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer 13 * in this position and unchanged. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/param.h> 33 #include <sys/fcntl.h> 34 #include <sys/jail.h> 35 #include <sys/imgact.h> 36 #include <sys/limits.h> 37 #include <sys/lock.h> 38 #include <sys/membarrier.h> 39 #include <sys/msgbuf.h> 40 #include <sys/mqueue.h> 41 #include <sys/mutex.h> 42 #include <sys/poll.h> 43 #include <sys/priv.h> 44 #include <sys/proc.h> 45 #include <sys/procctl.h> 46 #include <sys/reboot.h> 47 #include <sys/random.h> 48 #include <sys/resourcevar.h> 49 #include <sys/rtprio.h> 50 #include <sys/sched.h> 51 #include <sys/smp.h> 52 #include <sys/stat.h> 53 #include <sys/syscallsubr.h> 54 #include <sys/sysctl.h> 55 #include <sys/sysent.h> 56 #include <sys/sysproto.h> 57 #include <sys/time.h> 58 #include <sys/unistd.h> 59 #include <sys/vmmeter.h> 60 #include <sys/vnode.h> 61 62 #include <security/audit/audit.h> 63 #include <security/mac/mac_framework.h> 64 65 #include <vm/pmap.h> 66 #include <vm/vm_map.h> 67 #include <vm/swap_pager.h> 68 69 #ifdef COMPAT_LINUX32 70 #include <machine/../linux32/linux.h> 71 #include <machine/../linux32/linux32_proto.h> 72 #else 73 #include <machine/../linux/linux.h> 74 #include <machine/../linux/linux_proto.h> 75 #endif 76 77 #include <compat/linux/linux_common.h> 78 #include <compat/linux/linux_dtrace.h> 79 #include <compat/linux/linux_file.h> 80 #include <compat/linux/linux_mib.h> 81 #include <compat/linux/linux_mmap.h> 82 #include <compat/linux/linux_signal.h> 83 #include <compat/linux/linux_time.h> 84 #include <compat/linux/linux_util.h> 85 #include <compat/linux/linux_emul.h> 86 #include <compat/linux/linux_misc.h> 87 88 int stclohz; /* Statistics clock frequency */ 89 90 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = { 91 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK, 92 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE, 93 RLIMIT_MEMLOCK, RLIMIT_AS 94 }; 95 96 struct l_sysinfo { 97 l_long uptime; /* Seconds since boot */ 98 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */ 99 #define LINUX_SYSINFO_LOADS_SCALE 65536 100 l_ulong totalram; /* Total usable main memory size */ 101 l_ulong freeram; /* Available memory size */ 102 l_ulong sharedram; /* Amount of shared memory */ 103 l_ulong bufferram; /* Memory used by buffers */ 104 l_ulong totalswap; /* Total swap space size */ 105 l_ulong freeswap; /* swap space still available */ 106 l_ushort procs; /* Number of current processes */ 107 l_ushort pads; 108 l_ulong totalhigh; 109 l_ulong freehigh; 110 l_uint mem_unit; 111 char _f[20-2*sizeof(l_long)-sizeof(l_int)]; /* padding */ 112 }; 113 114 struct l_pselect6arg { 115 l_uintptr_t ss; 116 l_size_t ss_len; 117 }; 118 119 static int linux_utimensat_lts_to_ts(struct l_timespec *, 120 struct timespec *); 121 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 122 static int linux_utimensat_lts64_to_ts(struct l_timespec64 *, 123 struct timespec *); 124 #endif 125 static int linux_common_utimensat(struct thread *, int, 126 const char *, struct timespec *, int); 127 static int linux_common_pselect6(struct thread *, l_int, 128 l_fd_set *, l_fd_set *, l_fd_set *, 129 struct timespec *, l_uintptr_t *); 130 static int linux_common_ppoll(struct thread *, struct pollfd *, 131 uint32_t, struct timespec *, l_sigset_t *, 132 l_size_t); 133 static int linux_pollin(struct thread *, struct pollfd *, 134 struct pollfd *, u_int); 135 static int linux_pollout(struct thread *, struct pollfd *, 136 struct pollfd *, u_int); 137 138 int 139 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args) 140 { 141 struct l_sysinfo sysinfo; 142 int i, j; 143 struct timespec ts; 144 145 bzero(&sysinfo, sizeof(sysinfo)); 146 getnanouptime(&ts); 147 if (ts.tv_nsec != 0) 148 ts.tv_sec++; 149 sysinfo.uptime = ts.tv_sec; 150 151 /* Use the information from the mib to get our load averages */ 152 for (i = 0; i < 3; i++) 153 sysinfo.loads[i] = averunnable.ldavg[i] * 154 LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale; 155 156 sysinfo.totalram = physmem * PAGE_SIZE; 157 sysinfo.freeram = (u_long)vm_free_count() * PAGE_SIZE; 158 159 /* 160 * sharedram counts pages allocated to named, swap-backed objects such 161 * as shared memory segments and tmpfs files. There is no cheap way to 162 * compute this, so just leave the field unpopulated. Linux itself only 163 * started setting this field in the 3.x timeframe. 164 */ 165 sysinfo.sharedram = 0; 166 sysinfo.bufferram = 0; 167 168 swap_pager_status(&i, &j); 169 sysinfo.totalswap = i * PAGE_SIZE; 170 sysinfo.freeswap = (i - j) * PAGE_SIZE; 171 172 sysinfo.procs = nprocs; 173 174 /* 175 * Platforms supported by the emulation layer do not have a notion of 176 * high memory. 177 */ 178 sysinfo.totalhigh = 0; 179 sysinfo.freehigh = 0; 180 181 sysinfo.mem_unit = 1; 182 183 return (copyout(&sysinfo, args->info, sizeof(sysinfo))); 184 } 185 186 #ifdef LINUX_LEGACY_SYSCALLS 187 int 188 linux_alarm(struct thread *td, struct linux_alarm_args *args) 189 { 190 struct itimerval it, old_it; 191 u_int secs; 192 int error __diagused; 193 194 secs = args->secs; 195 /* 196 * Linux alarm() is always successful. Limit secs to INT32_MAX / 2 197 * to match kern_setitimer()'s limit to avoid error from it. 198 * 199 * XXX. Linux limit secs to INT_MAX on 32 and does not limit on 64-bit 200 * platforms. 201 */ 202 if (secs > INT32_MAX / 2) 203 secs = INT32_MAX / 2; 204 205 it.it_value.tv_sec = secs; 206 it.it_value.tv_usec = 0; 207 timevalclear(&it.it_interval); 208 error = kern_setitimer(td, ITIMER_REAL, &it, &old_it); 209 KASSERT(error == 0, ("kern_setitimer returns %d", error)); 210 211 if ((old_it.it_value.tv_sec == 0 && old_it.it_value.tv_usec > 0) || 212 old_it.it_value.tv_usec >= 500000) 213 old_it.it_value.tv_sec++; 214 td->td_retval[0] = old_it.it_value.tv_sec; 215 return (0); 216 } 217 #endif 218 219 int 220 linux_brk(struct thread *td, struct linux_brk_args *args) 221 { 222 struct vmspace *vm = td->td_proc->p_vmspace; 223 uintptr_t new, old; 224 225 old = (uintptr_t)vm->vm_daddr + ctob(vm->vm_dsize); 226 new = (uintptr_t)args->dsend; 227 if ((caddr_t)new > vm->vm_daddr && !kern_break(td, &new)) 228 td->td_retval[0] = (register_t)new; 229 else 230 td->td_retval[0] = (register_t)old; 231 232 return (0); 233 } 234 235 #ifdef LINUX_LEGACY_SYSCALLS 236 int 237 linux_select(struct thread *td, struct linux_select_args *args) 238 { 239 l_timeval ltv; 240 struct timeval tv0, tv1, utv, *tvp; 241 int error; 242 243 /* 244 * Store current time for computation of the amount of 245 * time left. 246 */ 247 if (args->timeout) { 248 if ((error = copyin(args->timeout, <v, sizeof(ltv)))) 249 goto select_out; 250 utv.tv_sec = ltv.tv_sec; 251 utv.tv_usec = ltv.tv_usec; 252 253 if (itimerfix(&utv)) { 254 /* 255 * The timeval was invalid. Convert it to something 256 * valid that will act as it does under Linux. 257 */ 258 utv.tv_sec += utv.tv_usec / 1000000; 259 utv.tv_usec %= 1000000; 260 if (utv.tv_usec < 0) { 261 utv.tv_sec -= 1; 262 utv.tv_usec += 1000000; 263 } 264 if (utv.tv_sec < 0) 265 timevalclear(&utv); 266 } 267 microtime(&tv0); 268 tvp = &utv; 269 } else 270 tvp = NULL; 271 272 error = kern_select(td, args->nfds, args->readfds, args->writefds, 273 args->exceptfds, tvp, LINUX_NFDBITS); 274 if (error) 275 goto select_out; 276 277 if (args->timeout) { 278 if (td->td_retval[0]) { 279 /* 280 * Compute how much time was left of the timeout, 281 * by subtracting the current time and the time 282 * before we started the call, and subtracting 283 * that result from the user-supplied value. 284 */ 285 microtime(&tv1); 286 timevalsub(&tv1, &tv0); 287 timevalsub(&utv, &tv1); 288 if (utv.tv_sec < 0) 289 timevalclear(&utv); 290 } else 291 timevalclear(&utv); 292 ltv.tv_sec = utv.tv_sec; 293 ltv.tv_usec = utv.tv_usec; 294 if ((error = copyout(<v, args->timeout, sizeof(ltv)))) 295 goto select_out; 296 } 297 298 select_out: 299 return (error); 300 } 301 #endif 302 303 int 304 linux_mremap(struct thread *td, struct linux_mremap_args *args) 305 { 306 uintptr_t addr; 307 size_t len; 308 int error = 0; 309 310 if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) { 311 td->td_retval[0] = 0; 312 return (EINVAL); 313 } 314 315 /* 316 * Check for the page alignment. 317 * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK. 318 */ 319 if (args->addr & PAGE_MASK) { 320 td->td_retval[0] = 0; 321 return (EINVAL); 322 } 323 324 args->new_len = round_page(args->new_len); 325 args->old_len = round_page(args->old_len); 326 327 if (args->new_len > args->old_len) { 328 td->td_retval[0] = 0; 329 return (ENOMEM); 330 } 331 332 if (args->new_len < args->old_len) { 333 addr = args->addr + args->new_len; 334 len = args->old_len - args->new_len; 335 error = kern_munmap(td, addr, len); 336 } 337 338 td->td_retval[0] = error ? 0 : (uintptr_t)args->addr; 339 return (error); 340 } 341 342 #define LINUX_MS_ASYNC 0x0001 343 #define LINUX_MS_INVALIDATE 0x0002 344 #define LINUX_MS_SYNC 0x0004 345 346 int 347 linux_msync(struct thread *td, struct linux_msync_args *args) 348 { 349 350 return (kern_msync(td, args->addr, args->len, 351 args->fl & ~LINUX_MS_SYNC)); 352 } 353 354 int 355 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap) 356 { 357 358 return (linux_mprotect_common(td, PTROUT(uap->addr), uap->len, 359 uap->prot)); 360 } 361 362 int 363 linux_pkey_mprotect(struct thread *td, struct linux_pkey_mprotect_args *uap) 364 { 365 366 return (linux_pkey_mprotect_common(td, uap->start, uap->len, 367 uap->prot, uap->pkey)); 368 } 369 370 int 371 linux_pkey_alloc(struct thread *td, struct linux_pkey_alloc_args *uap) 372 { 373 374 return (linux_pkey_alloc_common(td, uap->flags, uap->init_val)); 375 } 376 377 int 378 linux_pkey_free(struct thread *td, struct linux_pkey_free_args *uap) 379 { 380 381 return (linux_pkey_free_common(td, uap->pkey)); 382 } 383 384 int 385 linux_madvise(struct thread *td, struct linux_madvise_args *uap) 386 { 387 388 return (linux_madvise_common(td, PTROUT(uap->addr), uap->len, 389 uap->behav)); 390 } 391 392 int 393 linux_mmap2(struct thread *td, struct linux_mmap2_args *uap) 394 { 395 #if defined(LINUX_ARCHWANT_MMAP2PGOFF) 396 /* 397 * For architectures with sizeof (off_t) < sizeof (loff_t) mmap is 398 * implemented with mmap2 syscall and the offset is represented in 399 * multiples of page size. 400 */ 401 return (linux_mmap_common(td, PTROUT(uap->addr), uap->len, uap->prot, 402 uap->flags, uap->fd, (uint64_t)(uint32_t)uap->pgoff * PAGE_SIZE)); 403 #else 404 return (linux_mmap_common(td, PTROUT(uap->addr), uap->len, uap->prot, 405 uap->flags, uap->fd, uap->pgoff)); 406 #endif 407 } 408 409 #ifdef LINUX_LEGACY_SYSCALLS 410 int 411 linux_time(struct thread *td, struct linux_time_args *args) 412 { 413 struct timeval tv; 414 l_time_t tm; 415 int error; 416 417 microtime(&tv); 418 tm = tv.tv_sec; 419 if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm)))) 420 return (error); 421 td->td_retval[0] = tm; 422 return (0); 423 } 424 #endif 425 426 struct l_times_argv { 427 l_clock_t tms_utime; 428 l_clock_t tms_stime; 429 l_clock_t tms_cutime; 430 l_clock_t tms_cstime; 431 }; 432 433 /* 434 * Glibc versions prior to 2.2.1 always use hard-coded CLK_TCK value. 435 * Since 2.2.1 Glibc uses value exported from kernel via AT_CLKTCK 436 * auxiliary vector entry. 437 */ 438 #define CLK_TCK 100 439 440 #define CONVOTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK)) 441 #define CONVNTCK(r) (r.tv_sec * stclohz + r.tv_usec / (1000000 / stclohz)) 442 443 #define CONVTCK(r) (linux_kernver(td) >= LINUX_KERNVER(2,4,0) ? \ 444 CONVNTCK(r) : CONVOTCK(r)) 445 446 int 447 linux_times(struct thread *td, struct linux_times_args *args) 448 { 449 struct timeval tv, utime, stime, cutime, cstime; 450 struct l_times_argv tms; 451 struct proc *p; 452 int error; 453 454 if (args->buf != NULL) { 455 p = td->td_proc; 456 PROC_LOCK(p); 457 PROC_STATLOCK(p); 458 calcru(p, &utime, &stime); 459 PROC_STATUNLOCK(p); 460 calccru(p, &cutime, &cstime); 461 PROC_UNLOCK(p); 462 463 tms.tms_utime = CONVTCK(utime); 464 tms.tms_stime = CONVTCK(stime); 465 466 tms.tms_cutime = CONVTCK(cutime); 467 tms.tms_cstime = CONVTCK(cstime); 468 469 if ((error = copyout(&tms, args->buf, sizeof(tms)))) 470 return (error); 471 } 472 473 microuptime(&tv); 474 td->td_retval[0] = (int)CONVTCK(tv); 475 return (0); 476 } 477 478 int 479 linux_newuname(struct thread *td, struct linux_newuname_args *args) 480 { 481 struct l_new_utsname utsname; 482 char osname[LINUX_MAX_UTSNAME]; 483 char osrelease[LINUX_MAX_UTSNAME]; 484 char *p; 485 486 linux_get_osname(td, osname); 487 linux_get_osrelease(td, osrelease); 488 489 bzero(&utsname, sizeof(utsname)); 490 strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME); 491 getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME); 492 getcreddomainname(td->td_ucred, utsname.domainname, LINUX_MAX_UTSNAME); 493 strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME); 494 strlcpy(utsname.version, version, LINUX_MAX_UTSNAME); 495 for (p = utsname.version; *p != '\0'; ++p) 496 if (*p == '\n') { 497 *p = '\0'; 498 break; 499 } 500 #if defined(__amd64__) 501 /* 502 * On amd64, Linux uname(2) needs to return "x86_64" 503 * for both 64-bit and 32-bit applications. On 32-bit, 504 * the string returned by getauxval(AT_PLATFORM) needs 505 * to remain "i686", though. 506 */ 507 #if defined(COMPAT_LINUX32) 508 if (linux32_emulate_i386) 509 strlcpy(utsname.machine, "i686", LINUX_MAX_UTSNAME); 510 else 511 #endif 512 strlcpy(utsname.machine, "x86_64", LINUX_MAX_UTSNAME); 513 #elif defined(__aarch64__) 514 strlcpy(utsname.machine, "aarch64", LINUX_MAX_UTSNAME); 515 #elif defined(__i386__) 516 strlcpy(utsname.machine, "i686", LINUX_MAX_UTSNAME); 517 #endif 518 519 return (copyout(&utsname, args->buf, sizeof(utsname))); 520 } 521 522 struct l_utimbuf { 523 l_time_t l_actime; 524 l_time_t l_modtime; 525 }; 526 527 #ifdef LINUX_LEGACY_SYSCALLS 528 int 529 linux_utime(struct thread *td, struct linux_utime_args *args) 530 { 531 struct timeval tv[2], *tvp; 532 struct l_utimbuf lut; 533 int error; 534 535 if (args->times) { 536 if ((error = copyin(args->times, &lut, sizeof lut)) != 0) 537 return (error); 538 tv[0].tv_sec = lut.l_actime; 539 tv[0].tv_usec = 0; 540 tv[1].tv_sec = lut.l_modtime; 541 tv[1].tv_usec = 0; 542 tvp = tv; 543 } else 544 tvp = NULL; 545 546 return (kern_utimesat(td, AT_FDCWD, args->fname, UIO_USERSPACE, 547 tvp, UIO_SYSSPACE)); 548 } 549 #endif 550 551 #ifdef LINUX_LEGACY_SYSCALLS 552 int 553 linux_utimes(struct thread *td, struct linux_utimes_args *args) 554 { 555 l_timeval ltv[2]; 556 struct timeval tv[2], *tvp = NULL; 557 int error; 558 559 if (args->tptr != NULL) { 560 if ((error = copyin(args->tptr, ltv, sizeof ltv)) != 0) 561 return (error); 562 tv[0].tv_sec = ltv[0].tv_sec; 563 tv[0].tv_usec = ltv[0].tv_usec; 564 tv[1].tv_sec = ltv[1].tv_sec; 565 tv[1].tv_usec = ltv[1].tv_usec; 566 tvp = tv; 567 } 568 569 return (kern_utimesat(td, AT_FDCWD, args->fname, UIO_USERSPACE, 570 tvp, UIO_SYSSPACE)); 571 } 572 #endif 573 574 static int 575 linux_utimensat_lts_to_ts(struct l_timespec *l_times, struct timespec *times) 576 { 577 578 if (l_times->tv_nsec != LINUX_UTIME_OMIT && 579 l_times->tv_nsec != LINUX_UTIME_NOW && 580 (l_times->tv_nsec < 0 || l_times->tv_nsec > 999999999)) 581 return (EINVAL); 582 583 times->tv_sec = l_times->tv_sec; 584 switch (l_times->tv_nsec) 585 { 586 case LINUX_UTIME_OMIT: 587 times->tv_nsec = UTIME_OMIT; 588 break; 589 case LINUX_UTIME_NOW: 590 times->tv_nsec = UTIME_NOW; 591 break; 592 default: 593 times->tv_nsec = l_times->tv_nsec; 594 } 595 596 return (0); 597 } 598 599 static int 600 linux_common_utimensat(struct thread *td, int ldfd, const char *pathname, 601 struct timespec *timesp, int lflags) 602 { 603 int dfd, flags = 0; 604 605 dfd = (ldfd == LINUX_AT_FDCWD) ? AT_FDCWD : ldfd; 606 607 if (lflags & ~(LINUX_AT_SYMLINK_NOFOLLOW | LINUX_AT_EMPTY_PATH)) 608 return (EINVAL); 609 610 if (timesp != NULL) { 611 /* This breaks POSIX, but is what the Linux kernel does 612 * _on purpose_ (documented in the man page for utimensat(2)), 613 * so we must follow that behaviour. */ 614 if (timesp[0].tv_nsec == UTIME_OMIT && 615 timesp[1].tv_nsec == UTIME_OMIT) 616 return (0); 617 } 618 619 if (lflags & LINUX_AT_SYMLINK_NOFOLLOW) 620 flags |= AT_SYMLINK_NOFOLLOW; 621 if (lflags & LINUX_AT_EMPTY_PATH) 622 flags |= AT_EMPTY_PATH; 623 624 if (pathname != NULL) 625 return (kern_utimensat(td, dfd, pathname, 626 UIO_USERSPACE, timesp, UIO_SYSSPACE, flags)); 627 628 if (lflags != 0) 629 return (EINVAL); 630 631 return (kern_futimens(td, dfd, timesp, UIO_SYSSPACE)); 632 } 633 634 int 635 linux_utimensat(struct thread *td, struct linux_utimensat_args *args) 636 { 637 struct l_timespec l_times[2]; 638 struct timespec times[2], *timesp; 639 int error; 640 641 if (args->times != NULL) { 642 error = copyin(args->times, l_times, sizeof(l_times)); 643 if (error != 0) 644 return (error); 645 646 error = linux_utimensat_lts_to_ts(&l_times[0], ×[0]); 647 if (error != 0) 648 return (error); 649 error = linux_utimensat_lts_to_ts(&l_times[1], ×[1]); 650 if (error != 0) 651 return (error); 652 timesp = times; 653 } else 654 timesp = NULL; 655 656 return (linux_common_utimensat(td, args->dfd, args->pathname, 657 timesp, args->flags)); 658 } 659 660 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 661 static int 662 linux_utimensat_lts64_to_ts(struct l_timespec64 *l_times, struct timespec *times) 663 { 664 665 /* Zero out the padding in compat mode. */ 666 l_times->tv_nsec &= 0xFFFFFFFFUL; 667 668 if (l_times->tv_nsec != LINUX_UTIME_OMIT && 669 l_times->tv_nsec != LINUX_UTIME_NOW && 670 (l_times->tv_nsec < 0 || l_times->tv_nsec > 999999999)) 671 return (EINVAL); 672 673 times->tv_sec = l_times->tv_sec; 674 switch (l_times->tv_nsec) 675 { 676 case LINUX_UTIME_OMIT: 677 times->tv_nsec = UTIME_OMIT; 678 break; 679 case LINUX_UTIME_NOW: 680 times->tv_nsec = UTIME_NOW; 681 break; 682 default: 683 times->tv_nsec = l_times->tv_nsec; 684 } 685 686 return (0); 687 } 688 689 int 690 linux_utimensat_time64(struct thread *td, struct linux_utimensat_time64_args *args) 691 { 692 struct l_timespec64 l_times[2]; 693 struct timespec times[2], *timesp; 694 int error; 695 696 if (args->times64 != NULL) { 697 error = copyin(args->times64, l_times, sizeof(l_times)); 698 if (error != 0) 699 return (error); 700 701 error = linux_utimensat_lts64_to_ts(&l_times[0], ×[0]); 702 if (error != 0) 703 return (error); 704 error = linux_utimensat_lts64_to_ts(&l_times[1], ×[1]); 705 if (error != 0) 706 return (error); 707 timesp = times; 708 } else 709 timesp = NULL; 710 711 return (linux_common_utimensat(td, args->dfd, args->pathname, 712 timesp, args->flags)); 713 } 714 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 715 716 #ifdef LINUX_LEGACY_SYSCALLS 717 int 718 linux_futimesat(struct thread *td, struct linux_futimesat_args *args) 719 { 720 l_timeval ltv[2]; 721 struct timeval tv[2], *tvp = NULL; 722 int error, dfd; 723 724 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 725 726 if (args->utimes != NULL) { 727 if ((error = copyin(args->utimes, ltv, sizeof ltv)) != 0) 728 return (error); 729 tv[0].tv_sec = ltv[0].tv_sec; 730 tv[0].tv_usec = ltv[0].tv_usec; 731 tv[1].tv_sec = ltv[1].tv_sec; 732 tv[1].tv_usec = ltv[1].tv_usec; 733 tvp = tv; 734 } 735 736 return (kern_utimesat(td, dfd, args->filename, UIO_USERSPACE, 737 tvp, UIO_SYSSPACE)); 738 } 739 #endif 740 741 static int 742 linux_common_wait(struct thread *td, idtype_t idtype, int id, int *statusp, 743 int options, void *rup, l_siginfo_t *infop) 744 { 745 l_siginfo_t lsi; 746 siginfo_t siginfo; 747 struct __wrusage wru; 748 int error, status, tmpstat, sig; 749 750 error = kern_wait6(td, idtype, id, &status, options, 751 rup != NULL ? &wru : NULL, &siginfo); 752 753 if (error == 0 && statusp) { 754 tmpstat = status & 0xffff; 755 if (WIFSIGNALED(tmpstat)) { 756 tmpstat = (tmpstat & 0xffffff80) | 757 bsd_to_linux_signal(WTERMSIG(tmpstat)); 758 } else if (WIFSTOPPED(tmpstat)) { 759 tmpstat = (tmpstat & 0xffff00ff) | 760 (bsd_to_linux_signal(WSTOPSIG(tmpstat)) << 8); 761 #if defined(__aarch64__) || (defined(__amd64__) && !defined(COMPAT_LINUX32)) 762 if (WSTOPSIG(status) == SIGTRAP) { 763 tmpstat = linux_ptrace_status(td, 764 siginfo.si_pid, tmpstat); 765 } 766 #endif 767 } else if (WIFCONTINUED(tmpstat)) { 768 tmpstat = 0xffff; 769 } 770 error = copyout(&tmpstat, statusp, sizeof(int)); 771 } 772 if (error == 0 && rup != NULL) 773 error = linux_copyout_rusage(&wru.wru_self, rup); 774 if (error == 0 && infop != NULL && td->td_retval[0] != 0) { 775 sig = bsd_to_linux_signal(siginfo.si_signo); 776 memset(&lsi, 0, sizeof(lsi)); 777 siginfo_to_lsiginfo(&siginfo, &lsi, sig); 778 error = copyout(&lsi, infop, sizeof(lsi)); 779 } 780 781 return (error); 782 } 783 784 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 785 int 786 linux_waitpid(struct thread *td, struct linux_waitpid_args *args) 787 { 788 struct linux_wait4_args wait4_args = { 789 .pid = args->pid, 790 .status = args->status, 791 .options = args->options, 792 .rusage = NULL, 793 }; 794 795 return (linux_wait4(td, &wait4_args)); 796 } 797 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 798 799 int 800 linux_wait4(struct thread *td, struct linux_wait4_args *args) 801 { 802 struct proc *p; 803 int options, id, idtype; 804 805 if (args->options & ~(LINUX_WUNTRACED | LINUX_WNOHANG | 806 LINUX_WCONTINUED | __WCLONE | __WNOTHREAD | __WALL)) 807 return (EINVAL); 808 809 /* -INT_MIN is not defined. */ 810 if (args->pid == INT_MIN) 811 return (ESRCH); 812 813 options = 0; 814 linux_to_bsd_waitopts(args->options, &options); 815 816 /* 817 * For backward compatibility we implicitly add flags WEXITED 818 * and WTRAPPED here. 819 */ 820 options |= WEXITED | WTRAPPED; 821 822 if (args->pid == WAIT_ANY) { 823 idtype = P_ALL; 824 id = 0; 825 } else if (args->pid < 0) { 826 idtype = P_PGID; 827 id = (id_t)-args->pid; 828 } else if (args->pid == 0) { 829 idtype = P_PGID; 830 p = td->td_proc; 831 PROC_LOCK(p); 832 id = p->p_pgid; 833 PROC_UNLOCK(p); 834 } else { 835 idtype = P_PID; 836 id = (id_t)args->pid; 837 } 838 839 return (linux_common_wait(td, idtype, id, args->status, options, 840 args->rusage, NULL)); 841 } 842 843 int 844 linux_waitid(struct thread *td, struct linux_waitid_args *args) 845 { 846 idtype_t idtype; 847 int error, options; 848 struct proc *p; 849 pid_t id; 850 851 if (args->options & ~(LINUX_WNOHANG | LINUX_WNOWAIT | LINUX_WEXITED | 852 LINUX_WSTOPPED | LINUX_WCONTINUED | __WCLONE | __WNOTHREAD | __WALL)) 853 return (EINVAL); 854 855 options = 0; 856 linux_to_bsd_waitopts(args->options, &options); 857 858 id = args->id; 859 switch (args->idtype) { 860 case LINUX_P_ALL: 861 idtype = P_ALL; 862 break; 863 case LINUX_P_PID: 864 if (args->id <= 0) 865 return (EINVAL); 866 idtype = P_PID; 867 break; 868 case LINUX_P_PGID: 869 if (linux_kernver(td) >= LINUX_KERNVER(5,4,0) && args->id == 0) { 870 p = td->td_proc; 871 PROC_LOCK(p); 872 id = p->p_pgid; 873 PROC_UNLOCK(p); 874 } else if (args->id <= 0) 875 return (EINVAL); 876 idtype = P_PGID; 877 break; 878 case LINUX_P_PIDFD: 879 LINUX_RATELIMIT_MSG("unsupported waitid P_PIDFD idtype"); 880 return (ENOSYS); 881 default: 882 return (EINVAL); 883 } 884 885 error = linux_common_wait(td, idtype, id, NULL, options, 886 args->rusage, args->info); 887 td->td_retval[0] = 0; 888 889 return (error); 890 } 891 892 #ifdef LINUX_LEGACY_SYSCALLS 893 int 894 linux_mknod(struct thread *td, struct linux_mknod_args *args) 895 { 896 int error; 897 898 switch (args->mode & S_IFMT) { 899 case S_IFIFO: 900 case S_IFSOCK: 901 error = kern_mkfifoat(td, AT_FDCWD, args->path, UIO_USERSPACE, 902 args->mode); 903 break; 904 905 case S_IFCHR: 906 case S_IFBLK: 907 error = kern_mknodat(td, AT_FDCWD, args->path, UIO_USERSPACE, 908 args->mode, linux_decode_dev(args->dev)); 909 break; 910 911 case S_IFDIR: 912 error = EPERM; 913 break; 914 915 case 0: 916 args->mode |= S_IFREG; 917 /* FALLTHROUGH */ 918 case S_IFREG: 919 error = kern_openat(td, AT_FDCWD, args->path, UIO_USERSPACE, 920 O_WRONLY | O_CREAT | O_TRUNC, args->mode); 921 if (error == 0) 922 kern_close(td, td->td_retval[0]); 923 break; 924 925 default: 926 error = EINVAL; 927 break; 928 } 929 return (error); 930 } 931 #endif 932 933 int 934 linux_mknodat(struct thread *td, struct linux_mknodat_args *args) 935 { 936 int error, dfd; 937 938 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 939 940 switch (args->mode & S_IFMT) { 941 case S_IFIFO: 942 case S_IFSOCK: 943 error = kern_mkfifoat(td, dfd, args->filename, UIO_USERSPACE, 944 args->mode); 945 break; 946 947 case S_IFCHR: 948 case S_IFBLK: 949 error = kern_mknodat(td, dfd, args->filename, UIO_USERSPACE, 950 args->mode, linux_decode_dev(args->dev)); 951 break; 952 953 case S_IFDIR: 954 error = EPERM; 955 break; 956 957 case 0: 958 args->mode |= S_IFREG; 959 /* FALLTHROUGH */ 960 case S_IFREG: 961 error = kern_openat(td, dfd, args->filename, UIO_USERSPACE, 962 O_WRONLY | O_CREAT | O_TRUNC, args->mode); 963 if (error == 0) 964 kern_close(td, td->td_retval[0]); 965 break; 966 967 default: 968 error = EINVAL; 969 break; 970 } 971 return (error); 972 } 973 974 /* 975 * UGH! This is just about the dumbest idea I've ever heard!! 976 */ 977 int 978 linux_personality(struct thread *td, struct linux_personality_args *args) 979 { 980 struct linux_pemuldata *pem; 981 struct proc *p = td->td_proc; 982 uint32_t old; 983 984 PROC_LOCK(p); 985 pem = pem_find(p); 986 old = pem->persona; 987 if (args->per != 0xffffffff) 988 pem->persona = args->per; 989 PROC_UNLOCK(p); 990 991 td->td_retval[0] = old; 992 return (0); 993 } 994 995 struct l_itimerval { 996 l_timeval it_interval; 997 l_timeval it_value; 998 }; 999 1000 #define B2L_ITIMERVAL(bip, lip) \ 1001 (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec; \ 1002 (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec; \ 1003 (bip)->it_value.tv_sec = (lip)->it_value.tv_sec; \ 1004 (bip)->it_value.tv_usec = (lip)->it_value.tv_usec; 1005 1006 int 1007 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap) 1008 { 1009 int error; 1010 struct l_itimerval ls; 1011 struct itimerval aitv, oitv; 1012 1013 if (uap->itv == NULL) { 1014 uap->itv = uap->oitv; 1015 return (linux_getitimer(td, (struct linux_getitimer_args *)uap)); 1016 } 1017 1018 error = copyin(uap->itv, &ls, sizeof(ls)); 1019 if (error != 0) 1020 return (error); 1021 B2L_ITIMERVAL(&aitv, &ls); 1022 error = kern_setitimer(td, uap->which, &aitv, &oitv); 1023 if (error != 0 || uap->oitv == NULL) 1024 return (error); 1025 B2L_ITIMERVAL(&ls, &oitv); 1026 1027 return (copyout(&ls, uap->oitv, sizeof(ls))); 1028 } 1029 1030 int 1031 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap) 1032 { 1033 int error; 1034 struct l_itimerval ls; 1035 struct itimerval aitv; 1036 1037 error = kern_getitimer(td, uap->which, &aitv); 1038 if (error != 0) 1039 return (error); 1040 B2L_ITIMERVAL(&ls, &aitv); 1041 return (copyout(&ls, uap->itv, sizeof(ls))); 1042 } 1043 1044 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 1045 int 1046 linux_nice(struct thread *td, struct linux_nice_args *args) 1047 { 1048 1049 return (kern_setpriority(td, PRIO_PROCESS, 0, args->inc)); 1050 } 1051 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 1052 1053 int 1054 linux_setgroups(struct thread *td, struct linux_setgroups_args *args) 1055 { 1056 const int ngrp = args->gidsetsize; 1057 struct ucred *newcred, *oldcred; 1058 l_gid_t *linux_gidset; 1059 int error; 1060 struct proc *p; 1061 1062 if (ngrp < 0 || ngrp > ngroups_max) 1063 return (EINVAL); 1064 linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_LINUX, M_WAITOK); 1065 error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t)); 1066 if (error) 1067 goto out; 1068 1069 newcred = crget(); 1070 crextend(newcred, ngrp); 1071 p = td->td_proc; 1072 PROC_LOCK(p); 1073 oldcred = crcopysafe(p, newcred); 1074 1075 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS)) != 0) { 1076 PROC_UNLOCK(p); 1077 crfree(newcred); 1078 goto out; 1079 } 1080 1081 newcred->cr_ngroups = ngrp; 1082 for (int i = 0; i < ngrp; i++) 1083 newcred->cr_groups[i] = linux_gidset[i]; 1084 newcred->cr_flags |= CRED_FLAG_GROUPSET; 1085 1086 setsugid(p); 1087 proc_set_cred(p, newcred); 1088 PROC_UNLOCK(p); 1089 crfree(oldcred); 1090 error = 0; 1091 out: 1092 free(linux_gidset, M_LINUX); 1093 return (error); 1094 } 1095 1096 int 1097 linux_getgroups(struct thread *td, struct linux_getgroups_args *args) 1098 { 1099 const struct ucred *const cred = td->td_ucred; 1100 l_gid_t *linux_gidset; 1101 int ngrp, error; 1102 1103 ngrp = args->gidsetsize; 1104 1105 if (ngrp == 0) { 1106 td->td_retval[0] = cred->cr_ngroups; 1107 return (0); 1108 } 1109 if (ngrp < cred->cr_ngroups) 1110 return (EINVAL); 1111 1112 ngrp = cred->cr_ngroups; 1113 1114 linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_LINUX, M_WAITOK); 1115 for (int i = 0; i < ngrp; ++i) 1116 linux_gidset[i] = cred->cr_groups[i]; 1117 1118 error = copyout(linux_gidset, args->grouplist, ngrp * sizeof(l_gid_t)); 1119 free(linux_gidset, M_LINUX); 1120 1121 if (error != 0) 1122 return (error); 1123 1124 td->td_retval[0] = ngrp; 1125 return (0); 1126 } 1127 1128 static bool 1129 linux_get_dummy_limit(struct thread *td, l_uint resource, struct rlimit *rlim) 1130 { 1131 ssize_t size; 1132 int res, error; 1133 1134 if (linux_dummy_rlimits == 0) 1135 return (false); 1136 1137 switch (resource) { 1138 case LINUX_RLIMIT_LOCKS: 1139 case LINUX_RLIMIT_RTTIME: 1140 rlim->rlim_cur = LINUX_RLIM_INFINITY; 1141 rlim->rlim_max = LINUX_RLIM_INFINITY; 1142 return (true); 1143 case LINUX_RLIMIT_NICE: 1144 case LINUX_RLIMIT_RTPRIO: 1145 rlim->rlim_cur = 0; 1146 rlim->rlim_max = 0; 1147 return (true); 1148 case LINUX_RLIMIT_SIGPENDING: 1149 error = kernel_sysctlbyname(td, 1150 "kern.sigqueue.max_pending_per_proc", 1151 &res, &size, 0, 0, 0, 0); 1152 if (error != 0) 1153 return (false); 1154 rlim->rlim_cur = res; 1155 rlim->rlim_max = res; 1156 return (true); 1157 case LINUX_RLIMIT_MSGQUEUE: 1158 error = kernel_sysctlbyname(td, 1159 "kern.ipc.msgmnb", &res, &size, 0, 0, 0, 0); 1160 if (error != 0) 1161 return (false); 1162 rlim->rlim_cur = res; 1163 rlim->rlim_max = res; 1164 return (true); 1165 default: 1166 return (false); 1167 } 1168 } 1169 1170 int 1171 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args) 1172 { 1173 struct rlimit bsd_rlim; 1174 struct l_rlimit rlim; 1175 u_int which; 1176 int error; 1177 1178 if (args->resource >= LINUX_RLIM_NLIMITS) 1179 return (EINVAL); 1180 1181 which = linux_to_bsd_resource[args->resource]; 1182 if (which == -1) 1183 return (EINVAL); 1184 1185 error = copyin(args->rlim, &rlim, sizeof(rlim)); 1186 if (error) 1187 return (error); 1188 1189 bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur; 1190 bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max; 1191 return (kern_setrlimit(td, which, &bsd_rlim)); 1192 } 1193 1194 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 1195 int 1196 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args) 1197 { 1198 struct l_rlimit rlim; 1199 struct rlimit bsd_rlim; 1200 u_int which; 1201 1202 if (linux_get_dummy_limit(td, args->resource, &bsd_rlim)) { 1203 rlim.rlim_cur = bsd_rlim.rlim_cur; 1204 rlim.rlim_max = bsd_rlim.rlim_max; 1205 return (copyout(&rlim, args->rlim, sizeof(rlim))); 1206 } 1207 1208 if (args->resource >= LINUX_RLIM_NLIMITS) 1209 return (EINVAL); 1210 1211 which = linux_to_bsd_resource[args->resource]; 1212 if (which == -1) 1213 return (EINVAL); 1214 1215 lim_rlimit(td, which, &bsd_rlim); 1216 1217 #ifdef COMPAT_LINUX32 1218 rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur; 1219 if (rlim.rlim_cur == UINT_MAX) 1220 rlim.rlim_cur = INT_MAX; 1221 rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max; 1222 if (rlim.rlim_max == UINT_MAX) 1223 rlim.rlim_max = INT_MAX; 1224 #else 1225 rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur; 1226 if (rlim.rlim_cur == ULONG_MAX) 1227 rlim.rlim_cur = LONG_MAX; 1228 rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max; 1229 if (rlim.rlim_max == ULONG_MAX) 1230 rlim.rlim_max = LONG_MAX; 1231 #endif 1232 return (copyout(&rlim, args->rlim, sizeof(rlim))); 1233 } 1234 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 1235 1236 int 1237 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args) 1238 { 1239 struct l_rlimit rlim; 1240 struct rlimit bsd_rlim; 1241 u_int which; 1242 1243 if (linux_get_dummy_limit(td, args->resource, &bsd_rlim)) { 1244 rlim.rlim_cur = bsd_rlim.rlim_cur; 1245 rlim.rlim_max = bsd_rlim.rlim_max; 1246 return (copyout(&rlim, args->rlim, sizeof(rlim))); 1247 } 1248 1249 if (args->resource >= LINUX_RLIM_NLIMITS) 1250 return (EINVAL); 1251 1252 which = linux_to_bsd_resource[args->resource]; 1253 if (which == -1) 1254 return (EINVAL); 1255 1256 lim_rlimit(td, which, &bsd_rlim); 1257 1258 rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur; 1259 rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max; 1260 return (copyout(&rlim, args->rlim, sizeof(rlim))); 1261 } 1262 1263 int 1264 linux_sched_setscheduler(struct thread *td, 1265 struct linux_sched_setscheduler_args *args) 1266 { 1267 struct sched_param sched_param; 1268 struct thread *tdt; 1269 int error, policy; 1270 1271 switch (args->policy) { 1272 case LINUX_SCHED_OTHER: 1273 policy = SCHED_OTHER; 1274 break; 1275 case LINUX_SCHED_FIFO: 1276 policy = SCHED_FIFO; 1277 break; 1278 case LINUX_SCHED_RR: 1279 policy = SCHED_RR; 1280 break; 1281 default: 1282 return (EINVAL); 1283 } 1284 1285 error = copyin(args->param, &sched_param, sizeof(sched_param)); 1286 if (error) 1287 return (error); 1288 1289 if (linux_map_sched_prio) { 1290 switch (policy) { 1291 case SCHED_OTHER: 1292 if (sched_param.sched_priority != 0) 1293 return (EINVAL); 1294 1295 sched_param.sched_priority = 1296 PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE; 1297 break; 1298 case SCHED_FIFO: 1299 case SCHED_RR: 1300 if (sched_param.sched_priority < 1 || 1301 sched_param.sched_priority >= LINUX_MAX_RT_PRIO) 1302 return (EINVAL); 1303 1304 /* 1305 * Map [1, LINUX_MAX_RT_PRIO - 1] to 1306 * [0, RTP_PRIO_MAX - RTP_PRIO_MIN] (rounding down). 1307 */ 1308 sched_param.sched_priority = 1309 (sched_param.sched_priority - 1) * 1310 (RTP_PRIO_MAX - RTP_PRIO_MIN + 1) / 1311 (LINUX_MAX_RT_PRIO - 1); 1312 break; 1313 } 1314 } 1315 1316 tdt = linux_tdfind(td, args->pid, -1); 1317 if (tdt == NULL) 1318 return (ESRCH); 1319 1320 error = kern_sched_setscheduler(td, tdt, policy, &sched_param); 1321 PROC_UNLOCK(tdt->td_proc); 1322 return (error); 1323 } 1324 1325 int 1326 linux_sched_getscheduler(struct thread *td, 1327 struct linux_sched_getscheduler_args *args) 1328 { 1329 struct thread *tdt; 1330 int error, policy; 1331 1332 tdt = linux_tdfind(td, args->pid, -1); 1333 if (tdt == NULL) 1334 return (ESRCH); 1335 1336 error = kern_sched_getscheduler(td, tdt, &policy); 1337 PROC_UNLOCK(tdt->td_proc); 1338 1339 switch (policy) { 1340 case SCHED_OTHER: 1341 td->td_retval[0] = LINUX_SCHED_OTHER; 1342 break; 1343 case SCHED_FIFO: 1344 td->td_retval[0] = LINUX_SCHED_FIFO; 1345 break; 1346 case SCHED_RR: 1347 td->td_retval[0] = LINUX_SCHED_RR; 1348 break; 1349 } 1350 return (error); 1351 } 1352 1353 int 1354 linux_sched_get_priority_max(struct thread *td, 1355 struct linux_sched_get_priority_max_args *args) 1356 { 1357 struct sched_get_priority_max_args bsd; 1358 1359 if (linux_map_sched_prio) { 1360 switch (args->policy) { 1361 case LINUX_SCHED_OTHER: 1362 td->td_retval[0] = 0; 1363 return (0); 1364 case LINUX_SCHED_FIFO: 1365 case LINUX_SCHED_RR: 1366 td->td_retval[0] = LINUX_MAX_RT_PRIO - 1; 1367 return (0); 1368 default: 1369 return (EINVAL); 1370 } 1371 } 1372 1373 switch (args->policy) { 1374 case LINUX_SCHED_OTHER: 1375 bsd.policy = SCHED_OTHER; 1376 break; 1377 case LINUX_SCHED_FIFO: 1378 bsd.policy = SCHED_FIFO; 1379 break; 1380 case LINUX_SCHED_RR: 1381 bsd.policy = SCHED_RR; 1382 break; 1383 default: 1384 return (EINVAL); 1385 } 1386 return (sys_sched_get_priority_max(td, &bsd)); 1387 } 1388 1389 int 1390 linux_sched_get_priority_min(struct thread *td, 1391 struct linux_sched_get_priority_min_args *args) 1392 { 1393 struct sched_get_priority_min_args bsd; 1394 1395 if (linux_map_sched_prio) { 1396 switch (args->policy) { 1397 case LINUX_SCHED_OTHER: 1398 td->td_retval[0] = 0; 1399 return (0); 1400 case LINUX_SCHED_FIFO: 1401 case LINUX_SCHED_RR: 1402 td->td_retval[0] = 1; 1403 return (0); 1404 default: 1405 return (EINVAL); 1406 } 1407 } 1408 1409 switch (args->policy) { 1410 case LINUX_SCHED_OTHER: 1411 bsd.policy = SCHED_OTHER; 1412 break; 1413 case LINUX_SCHED_FIFO: 1414 bsd.policy = SCHED_FIFO; 1415 break; 1416 case LINUX_SCHED_RR: 1417 bsd.policy = SCHED_RR; 1418 break; 1419 default: 1420 return (EINVAL); 1421 } 1422 return (sys_sched_get_priority_min(td, &bsd)); 1423 } 1424 1425 #define REBOOT_CAD_ON 0x89abcdef 1426 #define REBOOT_CAD_OFF 0 1427 #define REBOOT_HALT 0xcdef0123 1428 #define REBOOT_RESTART 0x01234567 1429 #define REBOOT_RESTART2 0xA1B2C3D4 1430 #define REBOOT_POWEROFF 0x4321FEDC 1431 #define REBOOT_MAGIC1 0xfee1dead 1432 #define REBOOT_MAGIC2 0x28121969 1433 #define REBOOT_MAGIC2A 0x05121996 1434 #define REBOOT_MAGIC2B 0x16041998 1435 1436 int 1437 linux_reboot(struct thread *td, struct linux_reboot_args *args) 1438 { 1439 struct reboot_args bsd_args; 1440 1441 if (args->magic1 != REBOOT_MAGIC1) 1442 return (EINVAL); 1443 1444 switch (args->magic2) { 1445 case REBOOT_MAGIC2: 1446 case REBOOT_MAGIC2A: 1447 case REBOOT_MAGIC2B: 1448 break; 1449 default: 1450 return (EINVAL); 1451 } 1452 1453 switch (args->cmd) { 1454 case REBOOT_CAD_ON: 1455 case REBOOT_CAD_OFF: 1456 return (priv_check(td, PRIV_REBOOT)); 1457 case REBOOT_HALT: 1458 bsd_args.opt = RB_HALT; 1459 break; 1460 case REBOOT_RESTART: 1461 case REBOOT_RESTART2: 1462 bsd_args.opt = 0; 1463 break; 1464 case REBOOT_POWEROFF: 1465 bsd_args.opt = RB_POWEROFF; 1466 break; 1467 default: 1468 return (EINVAL); 1469 } 1470 return (sys_reboot(td, &bsd_args)); 1471 } 1472 1473 int 1474 linux_getpid(struct thread *td, struct linux_getpid_args *args) 1475 { 1476 1477 td->td_retval[0] = td->td_proc->p_pid; 1478 1479 return (0); 1480 } 1481 1482 int 1483 linux_gettid(struct thread *td, struct linux_gettid_args *args) 1484 { 1485 struct linux_emuldata *em; 1486 1487 em = em_find(td); 1488 KASSERT(em != NULL, ("gettid: emuldata not found.\n")); 1489 1490 td->td_retval[0] = em->em_tid; 1491 1492 return (0); 1493 } 1494 1495 int 1496 linux_getppid(struct thread *td, struct linux_getppid_args *args) 1497 { 1498 1499 td->td_retval[0] = kern_getppid(td); 1500 return (0); 1501 } 1502 1503 int 1504 linux_getgid(struct thread *td, struct linux_getgid_args *args) 1505 { 1506 1507 td->td_retval[0] = td->td_ucred->cr_rgid; 1508 return (0); 1509 } 1510 1511 int 1512 linux_getuid(struct thread *td, struct linux_getuid_args *args) 1513 { 1514 1515 td->td_retval[0] = td->td_ucred->cr_ruid; 1516 return (0); 1517 } 1518 1519 int 1520 linux_getsid(struct thread *td, struct linux_getsid_args *args) 1521 { 1522 1523 return (kern_getsid(td, args->pid)); 1524 } 1525 1526 int 1527 linux_getpriority(struct thread *td, struct linux_getpriority_args *args) 1528 { 1529 int error; 1530 1531 error = kern_getpriority(td, args->which, args->who); 1532 td->td_retval[0] = 20 - td->td_retval[0]; 1533 return (error); 1534 } 1535 1536 int 1537 linux_sethostname(struct thread *td, struct linux_sethostname_args *args) 1538 { 1539 int name[2]; 1540 1541 name[0] = CTL_KERN; 1542 name[1] = KERN_HOSTNAME; 1543 return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname, 1544 args->len, 0, 0)); 1545 } 1546 1547 int 1548 linux_setdomainname(struct thread *td, struct linux_setdomainname_args *args) 1549 { 1550 int name[2]; 1551 1552 name[0] = CTL_KERN; 1553 name[1] = KERN_NISDOMAINNAME; 1554 return (userland_sysctl(td, name, 2, 0, 0, 0, args->name, 1555 args->len, 0, 0)); 1556 } 1557 1558 int 1559 linux_exit_group(struct thread *td, struct linux_exit_group_args *args) 1560 { 1561 1562 LINUX_CTR2(exit_group, "thread(%d) (%d)", td->td_tid, 1563 args->error_code); 1564 1565 /* 1566 * XXX: we should send a signal to the parent if 1567 * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?) 1568 * as it doesnt occur often. 1569 */ 1570 kern_exit(td, args->error_code, 0); 1571 return (0); 1572 } 1573 1574 #define _LINUX_CAPABILITY_VERSION_1 0x19980330 1575 #define _LINUX_CAPABILITY_VERSION_2 0x20071026 1576 #define _LINUX_CAPABILITY_VERSION_3 0x20080522 1577 1578 struct l_user_cap_header { 1579 l_int version; 1580 l_int pid; 1581 }; 1582 1583 struct l_user_cap_data { 1584 l_int effective; 1585 l_int permitted; 1586 l_int inheritable; 1587 }; 1588 1589 int 1590 linux_capget(struct thread *td, struct linux_capget_args *uap) 1591 { 1592 struct l_user_cap_header luch; 1593 struct l_user_cap_data lucd[2]; 1594 int error, u32s; 1595 1596 if (uap->hdrp == NULL) 1597 return (EFAULT); 1598 1599 error = copyin(uap->hdrp, &luch, sizeof(luch)); 1600 if (error != 0) 1601 return (error); 1602 1603 switch (luch.version) { 1604 case _LINUX_CAPABILITY_VERSION_1: 1605 u32s = 1; 1606 break; 1607 case _LINUX_CAPABILITY_VERSION_2: 1608 case _LINUX_CAPABILITY_VERSION_3: 1609 u32s = 2; 1610 break; 1611 default: 1612 luch.version = _LINUX_CAPABILITY_VERSION_1; 1613 error = copyout(&luch, uap->hdrp, sizeof(luch)); 1614 if (error) 1615 return (error); 1616 return (EINVAL); 1617 } 1618 1619 if (luch.pid) 1620 return (EPERM); 1621 1622 if (uap->datap) { 1623 /* 1624 * The current implementation doesn't support setting 1625 * a capability (it's essentially a stub) so indicate 1626 * that no capabilities are currently set or available 1627 * to request. 1628 */ 1629 memset(&lucd, 0, u32s * sizeof(lucd[0])); 1630 error = copyout(&lucd, uap->datap, u32s * sizeof(lucd[0])); 1631 } 1632 1633 return (error); 1634 } 1635 1636 int 1637 linux_capset(struct thread *td, struct linux_capset_args *uap) 1638 { 1639 struct l_user_cap_header luch; 1640 struct l_user_cap_data lucd[2]; 1641 int error, i, u32s; 1642 1643 if (uap->hdrp == NULL || uap->datap == NULL) 1644 return (EFAULT); 1645 1646 error = copyin(uap->hdrp, &luch, sizeof(luch)); 1647 if (error != 0) 1648 return (error); 1649 1650 switch (luch.version) { 1651 case _LINUX_CAPABILITY_VERSION_1: 1652 u32s = 1; 1653 break; 1654 case _LINUX_CAPABILITY_VERSION_2: 1655 case _LINUX_CAPABILITY_VERSION_3: 1656 u32s = 2; 1657 break; 1658 default: 1659 luch.version = _LINUX_CAPABILITY_VERSION_1; 1660 error = copyout(&luch, uap->hdrp, sizeof(luch)); 1661 if (error) 1662 return (error); 1663 return (EINVAL); 1664 } 1665 1666 if (luch.pid) 1667 return (EPERM); 1668 1669 error = copyin(uap->datap, &lucd, u32s * sizeof(lucd[0])); 1670 if (error != 0) 1671 return (error); 1672 1673 /* We currently don't support setting any capabilities. */ 1674 for (i = 0; i < u32s; i++) { 1675 if (lucd[i].effective || lucd[i].permitted || 1676 lucd[i].inheritable) { 1677 linux_msg(td, 1678 "capset[%d] effective=0x%x, permitted=0x%x, " 1679 "inheritable=0x%x is not implemented", i, 1680 (int)lucd[i].effective, (int)lucd[i].permitted, 1681 (int)lucd[i].inheritable); 1682 return (EPERM); 1683 } 1684 } 1685 1686 return (0); 1687 } 1688 1689 int 1690 linux_prctl(struct thread *td, struct linux_prctl_args *args) 1691 { 1692 int error = 0, max_size, arg; 1693 struct proc *p = td->td_proc; 1694 char comm[LINUX_MAX_COMM_LEN]; 1695 int pdeath_signal, trace_state; 1696 1697 switch (args->option) { 1698 case LINUX_PR_SET_PDEATHSIG: 1699 if (!LINUX_SIG_VALID(args->arg2)) 1700 return (EINVAL); 1701 pdeath_signal = linux_to_bsd_signal(args->arg2); 1702 return (kern_procctl(td, P_PID, 0, PROC_PDEATHSIG_CTL, 1703 &pdeath_signal)); 1704 case LINUX_PR_GET_PDEATHSIG: 1705 error = kern_procctl(td, P_PID, 0, PROC_PDEATHSIG_STATUS, 1706 &pdeath_signal); 1707 if (error != 0) 1708 return (error); 1709 pdeath_signal = bsd_to_linux_signal(pdeath_signal); 1710 return (copyout(&pdeath_signal, 1711 (void *)(register_t)args->arg2, 1712 sizeof(pdeath_signal))); 1713 /* 1714 * In Linux, this flag controls if set[gu]id processes can coredump. 1715 * There are additional semantics imposed on processes that cannot 1716 * coredump: 1717 * - Such processes can not be ptraced. 1718 * - There are some semantics around ownership of process-related files 1719 * in the /proc namespace. 1720 * 1721 * In FreeBSD, we can (and by default, do) disable setuid coredump 1722 * system-wide with 'sugid_coredump.' We control tracability on a 1723 * per-process basis with the procctl PROC_TRACE (=> P2_NOTRACE flag). 1724 * By happy coincidence, P2_NOTRACE also prevents coredumping. So the 1725 * procctl is roughly analogous to Linux's DUMPABLE. 1726 * 1727 * So, proxy these knobs to the corresponding PROC_TRACE setting. 1728 */ 1729 case LINUX_PR_GET_DUMPABLE: 1730 error = kern_procctl(td, P_PID, p->p_pid, PROC_TRACE_STATUS, 1731 &trace_state); 1732 if (error != 0) 1733 return (error); 1734 td->td_retval[0] = (trace_state != -1); 1735 return (0); 1736 case LINUX_PR_SET_DUMPABLE: 1737 /* 1738 * It is only valid for userspace to set one of these two 1739 * flags, and only one at a time. 1740 */ 1741 switch (args->arg2) { 1742 case LINUX_SUID_DUMP_DISABLE: 1743 trace_state = PROC_TRACE_CTL_DISABLE_EXEC; 1744 break; 1745 case LINUX_SUID_DUMP_USER: 1746 trace_state = PROC_TRACE_CTL_ENABLE; 1747 break; 1748 default: 1749 return (EINVAL); 1750 } 1751 return (kern_procctl(td, P_PID, p->p_pid, PROC_TRACE_CTL, 1752 &trace_state)); 1753 case LINUX_PR_GET_KEEPCAPS: 1754 /* 1755 * Indicate that we always clear the effective and 1756 * permitted capability sets when the user id becomes 1757 * non-zero (actually the capability sets are simply 1758 * always zero in the current implementation). 1759 */ 1760 td->td_retval[0] = 0; 1761 break; 1762 case LINUX_PR_SET_KEEPCAPS: 1763 /* 1764 * Ignore requests to keep the effective and permitted 1765 * capability sets when the user id becomes non-zero. 1766 */ 1767 break; 1768 case LINUX_PR_SET_NAME: 1769 /* 1770 * To be on the safe side we need to make sure to not 1771 * overflow the size a Linux program expects. We already 1772 * do this here in the copyin, so that we don't need to 1773 * check on copyout. 1774 */ 1775 max_size = MIN(sizeof(comm), sizeof(p->p_comm)); 1776 error = copyinstr((void *)(register_t)args->arg2, comm, 1777 max_size, NULL); 1778 1779 /* Linux silently truncates the name if it is too long. */ 1780 if (error == ENAMETOOLONG) { 1781 /* 1782 * XXX: copyinstr() isn't documented to populate the 1783 * array completely, so do a copyin() to be on the 1784 * safe side. This should be changed in case 1785 * copyinstr() is changed to guarantee this. 1786 */ 1787 error = copyin((void *)(register_t)args->arg2, comm, 1788 max_size - 1); 1789 comm[max_size - 1] = '\0'; 1790 } 1791 if (error) 1792 return (error); 1793 1794 PROC_LOCK(p); 1795 strlcpy(p->p_comm, comm, sizeof(p->p_comm)); 1796 PROC_UNLOCK(p); 1797 break; 1798 case LINUX_PR_GET_NAME: 1799 PROC_LOCK(p); 1800 strlcpy(comm, p->p_comm, sizeof(comm)); 1801 PROC_UNLOCK(p); 1802 error = copyout(comm, (void *)(register_t)args->arg2, 1803 strlen(comm) + 1); 1804 break; 1805 case LINUX_PR_GET_SECCOMP: 1806 case LINUX_PR_SET_SECCOMP: 1807 /* 1808 * Same as returned by Linux without CONFIG_SECCOMP enabled. 1809 */ 1810 error = EINVAL; 1811 break; 1812 case LINUX_PR_CAPBSET_READ: 1813 #if 0 1814 /* 1815 * This makes too much noise with Ubuntu Focal. 1816 */ 1817 linux_msg(td, "unsupported prctl PR_CAPBSET_READ %d", 1818 (int)args->arg2); 1819 #endif 1820 error = EINVAL; 1821 break; 1822 case LINUX_PR_SET_CHILD_SUBREAPER: 1823 if (args->arg2 == 0) { 1824 return (kern_procctl(td, P_PID, 0, PROC_REAP_RELEASE, 1825 NULL)); 1826 } 1827 1828 return (kern_procctl(td, P_PID, 0, PROC_REAP_ACQUIRE, 1829 NULL)); 1830 case LINUX_PR_GET_CHILD_SUBREAPER: { 1831 struct procctl_reaper_status rs; 1832 l_int val; 1833 1834 error = kern_procctl(td, P_PID, 0, PROC_REAP_STATUS, &rs); 1835 if (error != 0) 1836 return (error); 1837 val = rs.rs_reaper == p->p_pid ? 1 : 0; 1838 error = copyout(&val, (void *)(register_t)args->arg2, 1839 sizeof(val)); 1840 break; 1841 } 1842 case LINUX_PR_SET_NO_NEW_PRIVS: 1843 arg = args->arg2 == 1 ? 1844 PROC_NO_NEW_PRIVS_ENABLE : PROC_NO_NEW_PRIVS_DISABLE; 1845 error = kern_procctl(td, P_PID, p->p_pid, 1846 PROC_NO_NEW_PRIVS_CTL, &arg); 1847 break; 1848 case LINUX_PR_GET_NO_NEW_PRIVS: 1849 error = kern_procctl(td, P_PID, p->p_pid, 1850 PROC_NO_NEW_PRIVS_STATUS, &arg); 1851 if (error != 0) 1852 return (error); 1853 /* Linux returns the value as the syscall return */ 1854 td->td_retval[0] = arg == PROC_NO_NEW_PRIVS_ENABLE ? 1 : 0; 1855 break; 1856 case LINUX_PR_SET_PTRACER: 1857 linux_msg(td, "unsupported prctl PR_SET_PTRACER"); 1858 error = EINVAL; 1859 break; 1860 case LINUX_PR_SET_VMA: 1861 if (args->arg2 != LINUX_PR_SET_VMA_ANON_NAME) { 1862 linux_msg(td, "unsupported prctl PR_SET_VMA attr %ju", 1863 (uintmax_t)args->arg2); 1864 error = EINVAL; 1865 } 1866 break; 1867 case LINUX_PR_GET_THP_DISABLE: 1868 /* 1869 * THP not in play, since FreeBSD doesn't have THP. Although 1870 * similar, superpages don't have the crazy issues THP does, so 1871 * tell the best lie possible: there's no problems with crazy 1872 * latency spikes: this feature is disabled. 1873 */ 1874 1875 td->td_retval[0] = 1; 1876 break; 1877 case LINUX_PR_SET_THP_DISABLE: 1878 /* 1879 * Accept anything that's valid. 1880 */ 1881 if (args->arg2 != 0 && args->arg2 != 1) 1882 return (EINVAL); 1883 td->td_retval[0] = 0; 1884 break; 1885 default: 1886 linux_msg(td, "unsupported prctl option %d", args->option); 1887 error = EINVAL; 1888 break; 1889 } 1890 1891 return (error); 1892 } 1893 1894 int 1895 linux_sched_setparam(struct thread *td, 1896 struct linux_sched_setparam_args *uap) 1897 { 1898 struct sched_param sched_param; 1899 struct thread *tdt; 1900 int error, policy; 1901 1902 error = copyin(uap->param, &sched_param, sizeof(sched_param)); 1903 if (error) 1904 return (error); 1905 1906 tdt = linux_tdfind(td, uap->pid, -1); 1907 if (tdt == NULL) 1908 return (ESRCH); 1909 1910 if (linux_map_sched_prio) { 1911 error = kern_sched_getscheduler(td, tdt, &policy); 1912 if (error) 1913 goto out; 1914 1915 switch (policy) { 1916 case SCHED_OTHER: 1917 if (sched_param.sched_priority != 0) { 1918 error = EINVAL; 1919 goto out; 1920 } 1921 sched_param.sched_priority = 1922 PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE; 1923 break; 1924 case SCHED_FIFO: 1925 case SCHED_RR: 1926 if (sched_param.sched_priority < 1 || 1927 sched_param.sched_priority >= LINUX_MAX_RT_PRIO) { 1928 error = EINVAL; 1929 goto out; 1930 } 1931 /* 1932 * Map [1, LINUX_MAX_RT_PRIO - 1] to 1933 * [0, RTP_PRIO_MAX - RTP_PRIO_MIN] (rounding down). 1934 */ 1935 sched_param.sched_priority = 1936 (sched_param.sched_priority - 1) * 1937 (RTP_PRIO_MAX - RTP_PRIO_MIN + 1) / 1938 (LINUX_MAX_RT_PRIO - 1); 1939 break; 1940 } 1941 } 1942 1943 error = kern_sched_setparam(td, tdt, &sched_param); 1944 out: PROC_UNLOCK(tdt->td_proc); 1945 return (error); 1946 } 1947 1948 int 1949 linux_sched_getparam(struct thread *td, 1950 struct linux_sched_getparam_args *uap) 1951 { 1952 struct sched_param sched_param; 1953 struct thread *tdt; 1954 int error, policy; 1955 1956 tdt = linux_tdfind(td, uap->pid, -1); 1957 if (tdt == NULL) 1958 return (ESRCH); 1959 1960 error = kern_sched_getparam(td, tdt, &sched_param); 1961 if (error) { 1962 PROC_UNLOCK(tdt->td_proc); 1963 return (error); 1964 } 1965 1966 if (linux_map_sched_prio) { 1967 error = kern_sched_getscheduler(td, tdt, &policy); 1968 PROC_UNLOCK(tdt->td_proc); 1969 if (error) 1970 return (error); 1971 1972 switch (policy) { 1973 case SCHED_OTHER: 1974 sched_param.sched_priority = 0; 1975 break; 1976 case SCHED_FIFO: 1977 case SCHED_RR: 1978 /* 1979 * Map [0, RTP_PRIO_MAX - RTP_PRIO_MIN] to 1980 * [1, LINUX_MAX_RT_PRIO - 1] (rounding up). 1981 */ 1982 sched_param.sched_priority = 1983 (sched_param.sched_priority * 1984 (LINUX_MAX_RT_PRIO - 1) + 1985 (RTP_PRIO_MAX - RTP_PRIO_MIN - 1)) / 1986 (RTP_PRIO_MAX - RTP_PRIO_MIN) + 1; 1987 break; 1988 } 1989 } else 1990 PROC_UNLOCK(tdt->td_proc); 1991 1992 error = copyout(&sched_param, uap->param, sizeof(sched_param)); 1993 return (error); 1994 } 1995 1996 /* 1997 * Get affinity of a process. 1998 */ 1999 int 2000 linux_sched_getaffinity(struct thread *td, 2001 struct linux_sched_getaffinity_args *args) 2002 { 2003 struct thread *tdt; 2004 cpuset_t *mask; 2005 size_t size; 2006 int error; 2007 id_t tid; 2008 2009 tdt = linux_tdfind(td, args->pid, -1); 2010 if (tdt == NULL) 2011 return (ESRCH); 2012 tid = tdt->td_tid; 2013 PROC_UNLOCK(tdt->td_proc); 2014 2015 mask = malloc(sizeof(cpuset_t), M_LINUX, M_WAITOK | M_ZERO); 2016 size = min(args->len, sizeof(cpuset_t)); 2017 error = kern_cpuset_getaffinity(td, CPU_LEVEL_WHICH, CPU_WHICH_TID, 2018 tid, size, mask); 2019 if (error == ERANGE) 2020 error = EINVAL; 2021 if (error == 0) 2022 error = copyout(mask, args->user_mask_ptr, size); 2023 if (error == 0) 2024 td->td_retval[0] = size; 2025 free(mask, M_LINUX); 2026 return (error); 2027 } 2028 2029 /* 2030 * Set affinity of a process. 2031 */ 2032 int 2033 linux_sched_setaffinity(struct thread *td, 2034 struct linux_sched_setaffinity_args *args) 2035 { 2036 struct thread *tdt; 2037 cpuset_t *mask; 2038 int cpu, error; 2039 size_t len; 2040 id_t tid; 2041 2042 tdt = linux_tdfind(td, args->pid, -1); 2043 if (tdt == NULL) 2044 return (ESRCH); 2045 tid = tdt->td_tid; 2046 PROC_UNLOCK(tdt->td_proc); 2047 2048 len = min(args->len, sizeof(cpuset_t)); 2049 mask = malloc(sizeof(cpuset_t), M_TEMP, M_WAITOK | M_ZERO); 2050 error = copyin(args->user_mask_ptr, mask, len); 2051 if (error != 0) 2052 goto out; 2053 /* Linux ignore high bits */ 2054 CPU_FOREACH_ISSET(cpu, mask) 2055 if (cpu > mp_maxid) 2056 CPU_CLR(cpu, mask); 2057 2058 error = kern_cpuset_setaffinity(td, CPU_LEVEL_WHICH, CPU_WHICH_TID, 2059 tid, mask); 2060 if (error == EDEADLK) 2061 error = EINVAL; 2062 out: 2063 free(mask, M_TEMP); 2064 return (error); 2065 } 2066 2067 struct linux_rlimit64 { 2068 uint64_t rlim_cur; 2069 uint64_t rlim_max; 2070 }; 2071 2072 int 2073 linux_prlimit64(struct thread *td, struct linux_prlimit64_args *args) 2074 { 2075 struct rlimit rlim, nrlim; 2076 struct linux_rlimit64 lrlim; 2077 struct proc *p; 2078 u_int which; 2079 int flags; 2080 int error; 2081 bool exec_blocked; 2082 2083 if (args->new == NULL && args->old != NULL) { 2084 if (linux_get_dummy_limit(td, args->resource, &rlim)) { 2085 lrlim.rlim_cur = rlim.rlim_cur; 2086 lrlim.rlim_max = rlim.rlim_max; 2087 return (copyout(&lrlim, args->old, sizeof(lrlim))); 2088 } 2089 } 2090 2091 if (args->resource >= LINUX_RLIM_NLIMITS) 2092 return (EINVAL); 2093 2094 which = linux_to_bsd_resource[args->resource]; 2095 if (which == -1) 2096 return (EINVAL); 2097 2098 if (args->new != NULL) { 2099 /* 2100 * Note. Unlike FreeBSD where rlim is signed 64-bit Linux 2101 * rlim is unsigned 64-bit. FreeBSD treats negative limits 2102 * as INFINITY so we do not need a conversion even. 2103 */ 2104 error = copyin(args->new, &nrlim, sizeof(nrlim)); 2105 if (error != 0) 2106 return (error); 2107 } 2108 2109 exec_blocked = false; 2110 flags = PGET_HOLD | PGET_NOTWEXIT; 2111 if (args->new != NULL) 2112 flags |= PGET_CANDEBUG; 2113 else 2114 flags |= PGET_CANSEE; 2115 if (args->pid == 0) { 2116 p = td->td_proc; 2117 PHOLD(p); 2118 } else { 2119 error = pget(args->pid, flags, &p); 2120 if (error != 0) 2121 return (error); 2122 exec_blocked = true; 2123 PROC_LOCK(p); 2124 execve_block_wait(td, p); 2125 error = args->new != NULL ? p_candebug(td, p) : 2126 p_cansee(td, p); 2127 PROC_UNLOCK(p); 2128 if (error != 0) 2129 goto out; 2130 } 2131 if (args->old != NULL) { 2132 PROC_LOCK(p); 2133 lim_rlimit_proc(p, which, &rlim); 2134 PROC_UNLOCK(p); 2135 if (rlim.rlim_cur == RLIM_INFINITY) 2136 lrlim.rlim_cur = LINUX_RLIM_INFINITY; 2137 else 2138 lrlim.rlim_cur = rlim.rlim_cur; 2139 if (rlim.rlim_max == RLIM_INFINITY) 2140 lrlim.rlim_max = LINUX_RLIM_INFINITY; 2141 else 2142 lrlim.rlim_max = rlim.rlim_max; 2143 error = copyout(&lrlim, args->old, sizeof(lrlim)); 2144 if (error != 0) 2145 goto out; 2146 } 2147 2148 if (args->new != NULL) 2149 error = kern_proc_setrlimit(td, p, which, &nrlim); 2150 2151 out: 2152 if (exec_blocked) { 2153 PROC_LOCK(p); 2154 execve_unblock(td, p); 2155 PROC_UNLOCK(p); 2156 } 2157 PRELE(p); 2158 return (error); 2159 } 2160 2161 int 2162 linux_pselect6(struct thread *td, struct linux_pselect6_args *args) 2163 { 2164 struct timespec ts, *tsp; 2165 int error; 2166 2167 if (args->tsp != NULL) { 2168 error = linux_get_timespec(&ts, args->tsp); 2169 if (error != 0) 2170 return (error); 2171 tsp = &ts; 2172 } else 2173 tsp = NULL; 2174 2175 error = linux_common_pselect6(td, args->nfds, args->readfds, 2176 args->writefds, args->exceptfds, tsp, args->sig); 2177 2178 if (args->tsp != NULL) 2179 linux_put_timespec(&ts, args->tsp); 2180 return (error); 2181 } 2182 2183 static int 2184 linux_common_pselect6(struct thread *td, l_int nfds, l_fd_set *readfds, 2185 l_fd_set *writefds, l_fd_set *exceptfds, struct timespec *tsp, 2186 l_uintptr_t *sig) 2187 { 2188 struct timeval utv, tv0, tv1, *tvp; 2189 struct l_pselect6arg lpse6; 2190 sigset_t *ssp; 2191 sigset_t ss; 2192 int error; 2193 2194 ssp = NULL; 2195 if (sig != NULL) { 2196 error = copyin(sig, &lpse6, sizeof(lpse6)); 2197 if (error != 0) 2198 return (error); 2199 error = linux_copyin_sigset(td, PTRIN(lpse6.ss), 2200 lpse6.ss_len, &ss, &ssp); 2201 if (error != 0) 2202 return (error); 2203 } else 2204 ssp = NULL; 2205 2206 /* 2207 * Currently glibc changes nanosecond number to microsecond. 2208 * This mean losing precision but for now it is hardly seen. 2209 */ 2210 if (tsp != NULL) { 2211 TIMESPEC_TO_TIMEVAL(&utv, tsp); 2212 if (itimerfix(&utv)) 2213 return (EINVAL); 2214 2215 microtime(&tv0); 2216 tvp = &utv; 2217 } else 2218 tvp = NULL; 2219 2220 error = kern_pselect(td, nfds, readfds, writefds, 2221 exceptfds, tvp, ssp, LINUX_NFDBITS); 2222 2223 if (tsp != NULL) { 2224 /* 2225 * Compute how much time was left of the timeout, 2226 * by subtracting the current time and the time 2227 * before we started the call, and subtracting 2228 * that result from the user-supplied value. 2229 */ 2230 microtime(&tv1); 2231 timevalsub(&tv1, &tv0); 2232 timevalsub(&utv, &tv1); 2233 if (utv.tv_sec < 0) 2234 timevalclear(&utv); 2235 TIMEVAL_TO_TIMESPEC(&utv, tsp); 2236 } 2237 return (error); 2238 } 2239 2240 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 2241 int 2242 linux_pselect6_time64(struct thread *td, 2243 struct linux_pselect6_time64_args *args) 2244 { 2245 struct timespec ts, *tsp; 2246 int error; 2247 2248 if (args->tsp != NULL) { 2249 error = linux_get_timespec64(&ts, args->tsp); 2250 if (error != 0) 2251 return (error); 2252 tsp = &ts; 2253 } else 2254 tsp = NULL; 2255 2256 error = linux_common_pselect6(td, args->nfds, args->readfds, 2257 args->writefds, args->exceptfds, tsp, args->sig); 2258 2259 if (args->tsp != NULL) 2260 linux_put_timespec64(&ts, args->tsp); 2261 return (error); 2262 } 2263 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 2264 2265 int 2266 linux_ppoll(struct thread *td, struct linux_ppoll_args *args) 2267 { 2268 struct timespec uts, *tsp; 2269 int error; 2270 2271 if (args->tsp != NULL) { 2272 error = linux_get_timespec(&uts, args->tsp); 2273 if (error != 0) 2274 return (error); 2275 tsp = &uts; 2276 } else 2277 tsp = NULL; 2278 2279 error = linux_common_ppoll(td, args->fds, args->nfds, tsp, 2280 args->sset, args->ssize); 2281 if (error == 0 && args->tsp != NULL) 2282 error = linux_put_timespec(&uts, args->tsp); 2283 return (error); 2284 } 2285 2286 static int 2287 linux_common_ppoll(struct thread *td, struct pollfd *fds, uint32_t nfds, 2288 struct timespec *tsp, l_sigset_t *sset, l_size_t ssize) 2289 { 2290 struct timespec ts0, ts1; 2291 struct pollfd stackfds[32]; 2292 struct pollfd *kfds; 2293 sigset_t *ssp; 2294 sigset_t ss; 2295 int error; 2296 2297 if (kern_poll_maxfds(nfds)) 2298 return (EINVAL); 2299 if (sset != NULL) { 2300 error = linux_copyin_sigset(td, sset, ssize, &ss, &ssp); 2301 if (error != 0) 2302 return (error); 2303 } else 2304 ssp = NULL; 2305 if (tsp != NULL) 2306 nanotime(&ts0); 2307 2308 if (nfds > nitems(stackfds)) 2309 kfds = mallocarray(nfds, sizeof(*kfds), M_TEMP, M_WAITOK); 2310 else 2311 kfds = stackfds; 2312 error = linux_pollin(td, kfds, fds, nfds); 2313 if (error != 0) 2314 goto out; 2315 2316 error = kern_poll_kfds(td, kfds, nfds, tsp, ssp); 2317 if (error == 0) 2318 error = linux_pollout(td, kfds, fds, nfds); 2319 2320 if (error == 0 && tsp != NULL) { 2321 if (td->td_retval[0]) { 2322 nanotime(&ts1); 2323 timespecsub(&ts1, &ts0, &ts1); 2324 timespecsub(tsp, &ts1, tsp); 2325 if (tsp->tv_sec < 0) 2326 timespecclear(tsp); 2327 } else 2328 timespecclear(tsp); 2329 } 2330 2331 out: 2332 if (nfds > nitems(stackfds)) 2333 free(kfds, M_TEMP); 2334 return (error); 2335 } 2336 2337 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 2338 int 2339 linux_ppoll_time64(struct thread *td, struct linux_ppoll_time64_args *args) 2340 { 2341 struct timespec uts, *tsp; 2342 int error; 2343 2344 if (args->tsp != NULL) { 2345 error = linux_get_timespec64(&uts, args->tsp); 2346 if (error != 0) 2347 return (error); 2348 tsp = &uts; 2349 } else 2350 tsp = NULL; 2351 error = linux_common_ppoll(td, args->fds, args->nfds, tsp, 2352 args->sset, args->ssize); 2353 if (error == 0 && args->tsp != NULL) 2354 error = linux_put_timespec64(&uts, args->tsp); 2355 return (error); 2356 } 2357 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 2358 2359 static int 2360 linux_pollin(struct thread *td, struct pollfd *fds, struct pollfd *ufds, u_int nfd) 2361 { 2362 int error; 2363 u_int i; 2364 2365 error = copyin(ufds, fds, nfd * sizeof(*fds)); 2366 if (error != 0) 2367 return (error); 2368 2369 for (i = 0; i < nfd; i++) { 2370 if (fds->events != 0) 2371 linux_to_bsd_poll_events(td, fds->fd, 2372 fds->events, &fds->events); 2373 fds++; 2374 } 2375 return (0); 2376 } 2377 2378 static int 2379 linux_pollout(struct thread *td, struct pollfd *fds, struct pollfd *ufds, u_int nfd) 2380 { 2381 int error = 0; 2382 u_int i, n = 0; 2383 2384 for (i = 0; i < nfd; i++) { 2385 if (fds->revents != 0) { 2386 bsd_to_linux_poll_events(fds->revents, 2387 &fds->revents); 2388 n++; 2389 } 2390 error = copyout(&fds->revents, &ufds->revents, 2391 sizeof(ufds->revents)); 2392 if (error) 2393 return (error); 2394 fds++; 2395 ufds++; 2396 } 2397 td->td_retval[0] = n; 2398 return (0); 2399 } 2400 2401 static int 2402 linux_sched_rr_get_interval_common(struct thread *td, pid_t pid, 2403 struct timespec *ts) 2404 { 2405 struct thread *tdt; 2406 int error; 2407 2408 /* 2409 * According to man in case the invalid pid specified 2410 * EINVAL should be returned. 2411 */ 2412 if (pid < 0) 2413 return (EINVAL); 2414 2415 tdt = linux_tdfind(td, pid, -1); 2416 if (tdt == NULL) 2417 return (ESRCH); 2418 2419 error = kern_sched_rr_get_interval_td(td, tdt, ts); 2420 PROC_UNLOCK(tdt->td_proc); 2421 return (error); 2422 } 2423 2424 int 2425 linux_sched_rr_get_interval(struct thread *td, 2426 struct linux_sched_rr_get_interval_args *uap) 2427 { 2428 struct timespec ts; 2429 int error; 2430 2431 error = linux_sched_rr_get_interval_common(td, uap->pid, &ts); 2432 if (error != 0) 2433 return (error); 2434 return (linux_put_timespec(&ts, uap->interval)); 2435 } 2436 2437 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 2438 int 2439 linux_sched_rr_get_interval_time64(struct thread *td, 2440 struct linux_sched_rr_get_interval_time64_args *uap) 2441 { 2442 struct timespec ts; 2443 int error; 2444 2445 error = linux_sched_rr_get_interval_common(td, uap->pid, &ts); 2446 if (error != 0) 2447 return (error); 2448 return (linux_put_timespec64(&ts, uap->interval)); 2449 } 2450 #endif 2451 2452 /* 2453 * In case when the Linux thread is the initial thread in 2454 * the thread group thread id is equal to the process id. 2455 * Glibc depends on this magic (assert in pthread_getattr_np.c). 2456 */ 2457 struct thread * 2458 linux_tdfind(struct thread *td, lwpid_t tid, pid_t pid) 2459 { 2460 struct linux_emuldata *em; 2461 struct thread *tdt; 2462 struct proc *p; 2463 2464 tdt = NULL; 2465 if (tid == 0 || tid == td->td_tid) { 2466 if (pid != -1 && td->td_proc->p_pid != pid) 2467 return (NULL); 2468 PROC_LOCK(td->td_proc); 2469 return (td); 2470 } else if (tid > PID_MAX) 2471 return (tdfind(tid, pid)); 2472 2473 /* 2474 * Initial thread where the tid equal to the pid. 2475 */ 2476 p = pfind(tid); 2477 if (p != NULL) { 2478 if (SV_PROC_ABI(p) != SV_ABI_LINUX || 2479 (pid != -1 && tid != pid)) { 2480 /* 2481 * p is not a Linuxulator process. 2482 */ 2483 PROC_UNLOCK(p); 2484 return (NULL); 2485 } 2486 FOREACH_THREAD_IN_PROC(p, tdt) { 2487 em = em_find(tdt); 2488 if (tid == em->em_tid) 2489 return (tdt); 2490 } 2491 PROC_UNLOCK(p); 2492 } 2493 return (NULL); 2494 } 2495 2496 void 2497 linux_to_bsd_waitopts(int options, int *bsdopts) 2498 { 2499 2500 if (options & LINUX_WNOHANG) 2501 *bsdopts |= WNOHANG; 2502 if (options & LINUX_WUNTRACED) 2503 *bsdopts |= WUNTRACED; 2504 if (options & LINUX_WEXITED) 2505 *bsdopts |= WEXITED; 2506 if (options & LINUX_WCONTINUED) 2507 *bsdopts |= WCONTINUED; 2508 if (options & LINUX_WNOWAIT) 2509 *bsdopts |= WNOWAIT; 2510 2511 if (options & __WCLONE) 2512 *bsdopts |= WLINUXCLONE; 2513 } 2514 2515 int 2516 linux_getrandom(struct thread *td, struct linux_getrandom_args *args) 2517 { 2518 struct uio uio; 2519 struct iovec iov; 2520 int error; 2521 2522 if (args->flags & ~(LINUX_GRND_NONBLOCK|LINUX_GRND_RANDOM)) 2523 return (EINVAL); 2524 if (args->count > INT_MAX) 2525 args->count = INT_MAX; 2526 2527 iov.iov_base = args->buf; 2528 iov.iov_len = args->count; 2529 2530 uio.uio_iov = &iov; 2531 uio.uio_iovcnt = 1; 2532 uio.uio_resid = iov.iov_len; 2533 uio.uio_segflg = UIO_USERSPACE; 2534 uio.uio_rw = UIO_READ; 2535 uio.uio_td = td; 2536 2537 error = read_random_uio(&uio, args->flags & LINUX_GRND_NONBLOCK); 2538 if (error == 0) 2539 td->td_retval[0] = args->count - uio.uio_resid; 2540 return (error); 2541 } 2542 2543 int 2544 linux_mincore(struct thread *td, struct linux_mincore_args *args) 2545 { 2546 2547 /* Needs to be page-aligned */ 2548 if (args->start & PAGE_MASK) 2549 return (EINVAL); 2550 return (kern_mincore(td, args->start, args->len, args->vec)); 2551 } 2552 2553 #define SYSLOG_TAG "<6>" 2554 2555 int 2556 linux_syslog(struct thread *td, struct linux_syslog_args *args) 2557 { 2558 char buf[128], *src, *dst; 2559 u_int seq; 2560 int buflen, error; 2561 2562 if (args->type != LINUX_SYSLOG_ACTION_READ_ALL) { 2563 linux_msg(td, "syslog unsupported type 0x%x", args->type); 2564 return (EINVAL); 2565 } 2566 2567 if (args->len < 6) { 2568 td->td_retval[0] = 0; 2569 return (0); 2570 } 2571 2572 error = priv_check(td, PRIV_MSGBUF); 2573 if (error) 2574 return (error); 2575 2576 mtx_lock(&msgbuf_lock); 2577 msgbuf_peekbytes(msgbufp, NULL, 0, &seq); 2578 mtx_unlock(&msgbuf_lock); 2579 2580 dst = args->buf; 2581 error = copyout(&SYSLOG_TAG, dst, sizeof(SYSLOG_TAG)); 2582 /* The -1 is to skip the trailing '\0'. */ 2583 dst += sizeof(SYSLOG_TAG) - 1; 2584 2585 while (error == 0) { 2586 mtx_lock(&msgbuf_lock); 2587 buflen = msgbuf_peekbytes(msgbufp, buf, sizeof(buf), &seq); 2588 mtx_unlock(&msgbuf_lock); 2589 2590 if (buflen == 0) 2591 break; 2592 2593 for (src = buf; src < buf + buflen && error == 0; src++) { 2594 if (*src == '\0') 2595 continue; 2596 2597 if (dst >= args->buf + args->len) 2598 goto out; 2599 2600 error = copyout(src, dst, 1); 2601 dst++; 2602 2603 if (*src == '\n' && *(src + 1) != '<' && 2604 dst + sizeof(SYSLOG_TAG) < args->buf + args->len) { 2605 error = copyout(&SYSLOG_TAG, 2606 dst, sizeof(SYSLOG_TAG)); 2607 dst += sizeof(SYSLOG_TAG) - 1; 2608 } 2609 } 2610 } 2611 out: 2612 td->td_retval[0] = dst - args->buf; 2613 return (error); 2614 } 2615 2616 int 2617 linux_getcpu(struct thread *td, struct linux_getcpu_args *args) 2618 { 2619 int cpu, error, node; 2620 2621 cpu = td->td_oncpu; /* Make sure it doesn't change during copyout(9) */ 2622 error = 0; 2623 node = cpuid_to_pcpu[cpu]->pc_domain; 2624 2625 if (args->cpu != NULL) 2626 error = copyout(&cpu, args->cpu, sizeof(l_int)); 2627 if (args->node != NULL) 2628 error = copyout(&node, args->node, sizeof(l_int)); 2629 return (error); 2630 } 2631 2632 #if defined(__i386__) || defined(__amd64__) 2633 int 2634 linux_poll(struct thread *td, struct linux_poll_args *args) 2635 { 2636 struct timespec ts, *tsp; 2637 2638 if (args->timeout != INFTIM) { 2639 if (args->timeout < 0) 2640 return (EINVAL); 2641 ts.tv_sec = args->timeout / 1000; 2642 ts.tv_nsec = (args->timeout % 1000) * 1000000; 2643 tsp = &ts; 2644 } else 2645 tsp = NULL; 2646 2647 return (linux_common_ppoll(td, args->fds, args->nfds, 2648 tsp, NULL, 0)); 2649 } 2650 #endif /* __i386__ || __amd64__ */ 2651 2652 int 2653 linux_seccomp(struct thread *td, struct linux_seccomp_args *args) 2654 { 2655 2656 switch (args->op) { 2657 case LINUX_SECCOMP_GET_ACTION_AVAIL: 2658 return (EOPNOTSUPP); 2659 default: 2660 /* 2661 * Ignore unknown operations, just like Linux kernel built 2662 * without CONFIG_SECCOMP. 2663 */ 2664 return (EINVAL); 2665 } 2666 } 2667 2668 /* 2669 * Custom version of exec_copyin_args(), to copy out argument and environment 2670 * strings from the old process address space into the temporary string buffer. 2671 * Based on freebsd32_exec_copyin_args. 2672 */ 2673 static int 2674 linux_exec_copyin_args(struct image_args *args, const char *fname, 2675 l_uintptr_t *argv, l_uintptr_t *envv) 2676 { 2677 char *argp, *envp; 2678 l_uintptr_t *ptr, arg; 2679 int error; 2680 2681 bzero(args, sizeof(*args)); 2682 if (argv == NULL) 2683 return (EFAULT); 2684 2685 /* 2686 * Allocate demand-paged memory for the file name, argument, and 2687 * environment strings. 2688 */ 2689 error = exec_alloc_args(args); 2690 if (error != 0) 2691 return (error); 2692 2693 /* 2694 * Copy the file name. 2695 */ 2696 error = exec_args_add_fname(args, fname, UIO_USERSPACE); 2697 if (error != 0) 2698 goto err_exit; 2699 2700 /* 2701 * extract arguments first 2702 */ 2703 ptr = argv; 2704 for (;;) { 2705 error = copyin(ptr++, &arg, sizeof(arg)); 2706 if (error) 2707 goto err_exit; 2708 if (arg == 0) 2709 break; 2710 argp = PTRIN(arg); 2711 error = exec_args_add_arg(args, argp, UIO_USERSPACE); 2712 if (error != 0) 2713 goto err_exit; 2714 } 2715 2716 /* 2717 * This comment is from Linux do_execveat_common: 2718 * When argv is empty, add an empty string ("") as argv[0] to 2719 * ensure confused userspace programs that start processing 2720 * from argv[1] won't end up walking envp. 2721 */ 2722 if (args->argc == 0 && 2723 (error = exec_args_add_arg(args, "", UIO_SYSSPACE) != 0)) 2724 goto err_exit; 2725 2726 /* 2727 * extract environment strings 2728 */ 2729 if (envv) { 2730 ptr = envv; 2731 for (;;) { 2732 error = copyin(ptr++, &arg, sizeof(arg)); 2733 if (error) 2734 goto err_exit; 2735 if (arg == 0) 2736 break; 2737 envp = PTRIN(arg); 2738 error = exec_args_add_env(args, envp, UIO_USERSPACE); 2739 if (error != 0) 2740 goto err_exit; 2741 } 2742 } 2743 2744 return (0); 2745 2746 err_exit: 2747 exec_free_args(args); 2748 return (error); 2749 } 2750 2751 int 2752 linux_execve(struct thread *td, struct linux_execve_args *args) 2753 { 2754 struct image_args eargs; 2755 int error; 2756 2757 LINUX_CTR(execve); 2758 2759 error = linux_exec_copyin_args(&eargs, args->path, args->argp, 2760 args->envp); 2761 if (error == 0) 2762 error = linux_common_execve(td, &eargs); 2763 AUDIT_SYSCALL_EXIT(error == EJUSTRETURN ? 0 : error, td); 2764 return (error); 2765 } 2766 2767 static void 2768 linux_up_rtprio_if(struct thread *td1, struct rtprio *rtp) 2769 { 2770 struct rtprio rtp2; 2771 2772 pri_to_rtp(td1, &rtp2); 2773 if (rtp2.type < rtp->type || 2774 (rtp2.type == rtp->type && 2775 rtp2.prio < rtp->prio)) { 2776 rtp->type = rtp2.type; 2777 rtp->prio = rtp2.prio; 2778 } 2779 } 2780 2781 #define LINUX_PRIO_DIVIDER RTP_PRIO_MAX / LINUX_IOPRIO_MAX 2782 2783 static int 2784 linux_rtprio2ioprio(struct rtprio *rtp) 2785 { 2786 int ioprio, prio; 2787 2788 switch (rtp->type) { 2789 case RTP_PRIO_IDLE: 2790 prio = RTP_PRIO_MIN; 2791 ioprio = LINUX_IOPRIO_PRIO(LINUX_IOPRIO_CLASS_IDLE, prio); 2792 break; 2793 case RTP_PRIO_NORMAL: 2794 prio = rtp->prio / LINUX_PRIO_DIVIDER; 2795 ioprio = LINUX_IOPRIO_PRIO(LINUX_IOPRIO_CLASS_BE, prio); 2796 break; 2797 case RTP_PRIO_REALTIME: 2798 prio = rtp->prio / LINUX_PRIO_DIVIDER; 2799 ioprio = LINUX_IOPRIO_PRIO(LINUX_IOPRIO_CLASS_RT, prio); 2800 break; 2801 default: 2802 prio = RTP_PRIO_MIN; 2803 ioprio = LINUX_IOPRIO_PRIO(LINUX_IOPRIO_CLASS_NONE, prio); 2804 break; 2805 } 2806 return (ioprio); 2807 } 2808 2809 static int 2810 linux_ioprio2rtprio(int ioprio, struct rtprio *rtp) 2811 { 2812 2813 switch (LINUX_IOPRIO_PRIO_CLASS(ioprio)) { 2814 case LINUX_IOPRIO_CLASS_IDLE: 2815 rtp->prio = RTP_PRIO_MIN; 2816 rtp->type = RTP_PRIO_IDLE; 2817 break; 2818 case LINUX_IOPRIO_CLASS_BE: 2819 rtp->prio = LINUX_IOPRIO_PRIO_DATA(ioprio) * LINUX_PRIO_DIVIDER; 2820 rtp->type = RTP_PRIO_NORMAL; 2821 break; 2822 case LINUX_IOPRIO_CLASS_RT: 2823 rtp->prio = LINUX_IOPRIO_PRIO_DATA(ioprio) * LINUX_PRIO_DIVIDER; 2824 rtp->type = RTP_PRIO_REALTIME; 2825 break; 2826 default: 2827 return (EINVAL); 2828 } 2829 return (0); 2830 } 2831 #undef LINUX_PRIO_DIVIDER 2832 2833 int 2834 linux_ioprio_get(struct thread *td, struct linux_ioprio_get_args *args) 2835 { 2836 struct thread *td1; 2837 struct rtprio rtp; 2838 struct pgrp *pg; 2839 struct proc *p; 2840 int error, found; 2841 2842 p = NULL; 2843 td1 = NULL; 2844 error = 0; 2845 found = 0; 2846 rtp.type = RTP_PRIO_IDLE; 2847 rtp.prio = RTP_PRIO_MAX; 2848 switch (args->which) { 2849 case LINUX_IOPRIO_WHO_PROCESS: 2850 if (args->who == 0) { 2851 td1 = td; 2852 p = td1->td_proc; 2853 PROC_LOCK(p); 2854 } else if (args->who > PID_MAX) { 2855 td1 = linux_tdfind(td, args->who, -1); 2856 if (td1 != NULL) 2857 p = td1->td_proc; 2858 } else 2859 p = pfind(args->who); 2860 if (p == NULL) 2861 return (ESRCH); 2862 if ((error = p_cansee(td, p))) { 2863 PROC_UNLOCK(p); 2864 break; 2865 } 2866 if (td1 != NULL) { 2867 pri_to_rtp(td1, &rtp); 2868 } else { 2869 FOREACH_THREAD_IN_PROC(p, td1) { 2870 linux_up_rtprio_if(td1, &rtp); 2871 } 2872 } 2873 found++; 2874 PROC_UNLOCK(p); 2875 break; 2876 case LINUX_IOPRIO_WHO_PGRP: 2877 sx_slock(&proctree_lock); 2878 if (args->who == 0) { 2879 pg = td->td_proc->p_pgrp; 2880 PGRP_LOCK(pg); 2881 } else { 2882 pg = pgfind(args->who); 2883 if (pg == NULL) { 2884 sx_sunlock(&proctree_lock); 2885 error = ESRCH; 2886 break; 2887 } 2888 } 2889 sx_sunlock(&proctree_lock); 2890 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 2891 PROC_LOCK(p); 2892 if (p->p_state == PRS_NORMAL && 2893 p_cansee(td, p) == 0) { 2894 FOREACH_THREAD_IN_PROC(p, td1) { 2895 linux_up_rtprio_if(td1, &rtp); 2896 found++; 2897 } 2898 } 2899 PROC_UNLOCK(p); 2900 } 2901 PGRP_UNLOCK(pg); 2902 break; 2903 case LINUX_IOPRIO_WHO_USER: 2904 if (args->who == 0) 2905 args->who = td->td_ucred->cr_uid; 2906 sx_slock(&allproc_lock); 2907 FOREACH_PROC_IN_SYSTEM(p) { 2908 PROC_LOCK(p); 2909 if (p->p_state == PRS_NORMAL && 2910 p->p_ucred->cr_uid == args->who && 2911 p_cansee(td, p) == 0) { 2912 FOREACH_THREAD_IN_PROC(p, td1) { 2913 linux_up_rtprio_if(td1, &rtp); 2914 found++; 2915 } 2916 } 2917 PROC_UNLOCK(p); 2918 } 2919 sx_sunlock(&allproc_lock); 2920 break; 2921 default: 2922 error = EINVAL; 2923 break; 2924 } 2925 if (error == 0) { 2926 if (found != 0) 2927 td->td_retval[0] = linux_rtprio2ioprio(&rtp); 2928 else 2929 error = ESRCH; 2930 } 2931 return (error); 2932 } 2933 2934 int 2935 linux_ioprio_set(struct thread *td, struct linux_ioprio_set_args *args) 2936 { 2937 struct thread *td1; 2938 struct rtprio rtp; 2939 struct pgrp *pg; 2940 struct proc *p; 2941 int error; 2942 2943 if ((error = linux_ioprio2rtprio(args->ioprio, &rtp)) != 0) 2944 return (error); 2945 /* Attempts to set high priorities (REALTIME) require su privileges. */ 2946 if (RTP_PRIO_BASE(rtp.type) == RTP_PRIO_REALTIME && 2947 (error = priv_check(td, PRIV_SCHED_RTPRIO)) != 0) 2948 return (error); 2949 2950 p = NULL; 2951 td1 = NULL; 2952 switch (args->which) { 2953 case LINUX_IOPRIO_WHO_PROCESS: 2954 if (args->who == 0) { 2955 td1 = td; 2956 p = td1->td_proc; 2957 PROC_LOCK(p); 2958 } else if (args->who > PID_MAX) { 2959 td1 = linux_tdfind(td, args->who, -1); 2960 if (td1 != NULL) 2961 p = td1->td_proc; 2962 } else 2963 p = pfind(args->who); 2964 if (p == NULL) 2965 return (ESRCH); 2966 if ((error = p_cansched(td, p))) { 2967 PROC_UNLOCK(p); 2968 break; 2969 } 2970 if (td1 != NULL) { 2971 error = rtp_to_pri(&rtp, td1); 2972 } else { 2973 FOREACH_THREAD_IN_PROC(p, td1) { 2974 if ((error = rtp_to_pri(&rtp, td1)) != 0) 2975 break; 2976 } 2977 } 2978 PROC_UNLOCK(p); 2979 break; 2980 case LINUX_IOPRIO_WHO_PGRP: 2981 sx_slock(&proctree_lock); 2982 if (args->who == 0) { 2983 pg = td->td_proc->p_pgrp; 2984 PGRP_LOCK(pg); 2985 } else { 2986 pg = pgfind(args->who); 2987 if (pg == NULL) { 2988 sx_sunlock(&proctree_lock); 2989 error = ESRCH; 2990 break; 2991 } 2992 } 2993 sx_sunlock(&proctree_lock); 2994 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 2995 PROC_LOCK(p); 2996 if (p->p_state == PRS_NORMAL && 2997 p_cansched(td, p) == 0) { 2998 FOREACH_THREAD_IN_PROC(p, td1) { 2999 if ((error = rtp_to_pri(&rtp, td1)) != 0) 3000 break; 3001 } 3002 } 3003 PROC_UNLOCK(p); 3004 if (error != 0) 3005 break; 3006 } 3007 PGRP_UNLOCK(pg); 3008 break; 3009 case LINUX_IOPRIO_WHO_USER: 3010 if (args->who == 0) 3011 args->who = td->td_ucred->cr_uid; 3012 sx_slock(&allproc_lock); 3013 FOREACH_PROC_IN_SYSTEM(p) { 3014 PROC_LOCK(p); 3015 if (p->p_state == PRS_NORMAL && 3016 p->p_ucred->cr_uid == args->who && 3017 p_cansched(td, p) == 0) { 3018 FOREACH_THREAD_IN_PROC(p, td1) { 3019 if ((error = rtp_to_pri(&rtp, td1)) != 0) 3020 break; 3021 } 3022 } 3023 PROC_UNLOCK(p); 3024 if (error != 0) 3025 break; 3026 } 3027 sx_sunlock(&allproc_lock); 3028 break; 3029 default: 3030 error = EINVAL; 3031 break; 3032 } 3033 return (error); 3034 } 3035 3036 /* The only flag is O_NONBLOCK */ 3037 #define B2L_MQ_FLAGS(bflags) ((bflags) != 0 ? LINUX_O_NONBLOCK : 0) 3038 #define L2B_MQ_FLAGS(lflags) ((lflags) != 0 ? O_NONBLOCK : 0) 3039 3040 int 3041 linux_mq_open(struct thread *td, struct linux_mq_open_args *args) 3042 { 3043 struct mq_attr attr; 3044 int error, flags; 3045 3046 flags = linux_common_openflags(args->oflag); 3047 if ((flags & O_ACCMODE) == O_ACCMODE || (flags & O_EXEC) != 0) 3048 return (EINVAL); 3049 flags = FFLAGS(flags); 3050 if ((flags & O_CREAT) != 0 && args->attr != NULL) { 3051 error = copyin(args->attr, &attr, sizeof(attr)); 3052 if (error != 0) 3053 return (error); 3054 attr.mq_flags = L2B_MQ_FLAGS(attr.mq_flags); 3055 } 3056 3057 return (kern_kmq_open(td, args->name, flags, args->mode, 3058 args->attr != NULL ? &attr : NULL)); 3059 } 3060 3061 int 3062 linux_mq_unlink(struct thread *td, struct linux_mq_unlink_args *args) 3063 { 3064 struct kmq_unlink_args bsd_args = { 3065 .path = PTRIN(args->name) 3066 }; 3067 3068 return (sys_kmq_unlink(td, &bsd_args)); 3069 } 3070 3071 int 3072 linux_mq_timedsend(struct thread *td, struct linux_mq_timedsend_args *args) 3073 { 3074 struct timespec ts, *abs_timeout; 3075 int error; 3076 3077 if (args->abs_timeout == NULL) 3078 abs_timeout = NULL; 3079 else { 3080 error = linux_get_timespec(&ts, args->abs_timeout); 3081 if (error != 0) 3082 return (error); 3083 abs_timeout = &ts; 3084 } 3085 3086 return (kern_kmq_timedsend(td, args->mqd, PTRIN(args->msg_ptr), 3087 args->msg_len, args->msg_prio, abs_timeout)); 3088 } 3089 3090 int 3091 linux_mq_timedreceive(struct thread *td, struct linux_mq_timedreceive_args *args) 3092 { 3093 struct timespec ts, *abs_timeout; 3094 int error; 3095 3096 if (args->abs_timeout == NULL) 3097 abs_timeout = NULL; 3098 else { 3099 error = linux_get_timespec(&ts, args->abs_timeout); 3100 if (error != 0) 3101 return (error); 3102 abs_timeout = &ts; 3103 } 3104 3105 return (kern_kmq_timedreceive(td, args->mqd, PTRIN(args->msg_ptr), 3106 args->msg_len, args->msg_prio, abs_timeout)); 3107 } 3108 3109 int 3110 linux_mq_notify(struct thread *td, struct linux_mq_notify_args *args) 3111 { 3112 struct sigevent ev, *evp; 3113 struct l_sigevent l_ev; 3114 int error; 3115 3116 if (args->sevp == NULL) 3117 evp = NULL; 3118 else { 3119 error = copyin(args->sevp, &l_ev, sizeof(l_ev)); 3120 if (error != 0) 3121 return (error); 3122 error = linux_convert_l_sigevent(&l_ev, &ev); 3123 if (error != 0) 3124 return (error); 3125 evp = &ev; 3126 } 3127 3128 return (kern_kmq_notify(td, args->mqd, evp)); 3129 } 3130 3131 int 3132 linux_mq_getsetattr(struct thread *td, struct linux_mq_getsetattr_args *args) 3133 { 3134 struct mq_attr attr, oattr; 3135 int error; 3136 3137 if (args->attr != NULL) { 3138 error = copyin(args->attr, &attr, sizeof(attr)); 3139 if (error != 0) 3140 return (error); 3141 attr.mq_flags = L2B_MQ_FLAGS(attr.mq_flags); 3142 } 3143 3144 error = kern_kmq_setattr(td, args->mqd, args->attr != NULL ? &attr : NULL, 3145 &oattr); 3146 if (error == 0 && args->oattr != NULL) { 3147 oattr.mq_flags = B2L_MQ_FLAGS(oattr.mq_flags); 3148 bzero(oattr.__reserved, sizeof(oattr.__reserved)); 3149 error = copyout(&oattr, args->oattr, sizeof(oattr)); 3150 } 3151 3152 return (error); 3153 } 3154 3155 int 3156 linux_kcmp(struct thread *td, struct linux_kcmp_args *args) 3157 { 3158 int type; 3159 3160 switch (args->type) { 3161 case LINUX_KCMP_FILE: 3162 type = KCMP_FILE; 3163 break; 3164 case LINUX_KCMP_FILES: 3165 type = KCMP_FILES; 3166 break; 3167 case LINUX_KCMP_SIGHAND: 3168 type = KCMP_SIGHAND; 3169 break; 3170 case LINUX_KCMP_VM: 3171 type = KCMP_VM; 3172 break; 3173 default: 3174 return (EINVAL); 3175 } 3176 3177 return (kern_kcmp(td, args->pid1, args->pid2, type, args->idx1, 3178 args->idx)); 3179 } 3180 3181 int 3182 linux_membarrier(struct thread *td, struct linux_membarrier_args *args) 3183 { 3184 static const struct { 3185 int linux_cmd; 3186 int freebsd_cmd; 3187 } cmds[] = { 3188 { LINUX_MEMBARRIER_CMD_QUERY, 3189 MEMBARRIER_CMD_QUERY }, 3190 { LINUX_MEMBARRIER_CMD_GLOBAL, 3191 MEMBARRIER_CMD_GLOBAL }, 3192 { LINUX_MEMBARRIER_CMD_GLOBAL_EXPEDITED, 3193 MEMBARRIER_CMD_GLOBAL_EXPEDITED }, 3194 { LINUX_MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED, 3195 MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED }, 3196 { LINUX_MEMBARRIER_CMD_PRIVATE_EXPEDITED, 3197 MEMBARRIER_CMD_PRIVATE_EXPEDITED }, 3198 { LINUX_MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED, 3199 MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED }, 3200 { LINUX_MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE, 3201 MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE }, 3202 { LINUX_MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE, 3203 MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE }, 3204 { LINUX_MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ, 3205 MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ }, 3206 { LINUX_MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_RSEQ, 3207 MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_RSEQ }, 3208 { LINUX_MEMBARRIER_CMD_GET_REGISTRATIONS, 3209 MEMBARRIER_CMD_GET_REGISTRATIONS }, 3210 }; 3211 int cmd, error, flags, i, mask; 3212 3213 cmd = -1; 3214 for (i = 0; i < nitems(cmds); i++) { 3215 if (args->cmd == cmds[i].linux_cmd) { 3216 cmd = cmds[i].freebsd_cmd; 3217 break; 3218 } 3219 } 3220 3221 if (cmd == -1 || (args->flags & ~LINUX_MEMBARRIER_CMD_FLAG_CPU) != 0) 3222 return (EINVAL); 3223 3224 flags = 0; 3225 if ((args->flags & LINUX_MEMBARRIER_CMD_FLAG_CPU) != 0) 3226 flags |= MEMBARRIER_CMD_FLAG_CPU; 3227 3228 error = kern_membarrier(td, cmd, flags, args->cpu_id); 3229 if (error != 0) 3230 return (error); 3231 3232 if (args->cmd == LINUX_MEMBARRIER_CMD_QUERY || 3233 args->cmd == LINUX_MEMBARRIER_CMD_GET_REGISTRATIONS) { 3234 mask = td->td_retval[0]; 3235 td->td_retval[0] = 0; 3236 for (i = 0; i < nitems(cmds); i++) 3237 if ((mask & cmds[i].freebsd_cmd) != 0) 3238 td->td_retval[0] |= cmds[i].linux_cmd; 3239 } 3240 3241 return (0); 3242 } 3243 3244 /* 3245 * setfsuid() & setfsgid() exist to decouple the Linux filesystem credentials 3246 * from the effective credentials, avoiding signal exposure during privilege 3247 * transitions. The signal permission model that motivated this was revised in 3248 * Linux 2.0, making these syscalls obsolete for new applications. 3249 * 3250 * As there's no FreeBSD equivalent, implement both syscalls as no-ops that 3251 * return the current effective UID/GID as the previous filesystem UID/GID. 3252 * Linux returns the previous filesystem UID/GID for these syscalls, with no 3253 * error indication. 3254 */ 3255 3256 int 3257 linux_setfsuid(struct thread *td, struct linux_setfsuid_args *args) 3258 { 3259 td->td_retval[0] = td->td_ucred->cr_uid; 3260 return (0); 3261 } 3262 3263 int 3264 linux_setfsgid(struct thread *td, struct linux_setfsgid_args *args) 3265 { 3266 td->td_retval[0] = td->td_ucred->cr_gid; 3267 return (0); 3268 } 3269 3270 MODULE_DEPEND(linux, mqueuefs, 1, 1, 1); 3271