1 /*- 2 * Copyright (c) 2002 Doug Rabson 3 * Copyright (c) 1994-1995 S�ren Schmidt 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer 11 * in this position and unchanged. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include "opt_compat.h" 34 #include "opt_mac.h" 35 36 #include <sys/param.h> 37 #include <sys/blist.h> 38 #include <sys/fcntl.h> 39 #if defined(__i386__) 40 #include <sys/imgact_aout.h> 41 #endif 42 #include <sys/jail.h> 43 #include <sys/kernel.h> 44 #include <sys/limits.h> 45 #include <sys/lock.h> 46 #include <sys/malloc.h> 47 #include <sys/mman.h> 48 #include <sys/mount.h> 49 #include <sys/mutex.h> 50 #include <sys/namei.h> 51 #include <sys/priv.h> 52 #include <sys/proc.h> 53 #include <sys/reboot.h> 54 #include <sys/resourcevar.h> 55 #include <sys/sched.h> 56 #include <sys/signalvar.h> 57 #include <sys/stat.h> 58 #include <sys/syscallsubr.h> 59 #include <sys/sysctl.h> 60 #include <sys/sysproto.h> 61 #include <sys/systm.h> 62 #include <sys/time.h> 63 #include <sys/vmmeter.h> 64 #include <sys/vnode.h> 65 #include <sys/wait.h> 66 #include <sys/cpuset.h> 67 #include <sys/vimage.h> 68 69 #include <security/mac/mac_framework.h> 70 71 #include <vm/vm.h> 72 #include <vm/pmap.h> 73 #include <vm/vm_kern.h> 74 #include <vm/vm_map.h> 75 #include <vm/vm_extern.h> 76 #include <vm/vm_object.h> 77 #include <vm/swap_pager.h> 78 79 #ifdef COMPAT_LINUX32 80 #include <machine/../linux32/linux.h> 81 #include <machine/../linux32/linux32_proto.h> 82 #else 83 #include <machine/../linux/linux.h> 84 #include <machine/../linux/linux_proto.h> 85 #endif 86 87 #include <compat/linux/linux_file.h> 88 #include <compat/linux/linux_mib.h> 89 #include <compat/linux/linux_signal.h> 90 #include <compat/linux/linux_util.h> 91 #include <compat/linux/linux_sysproto.h> 92 #include <compat/linux/linux_emul.h> 93 #include <compat/linux/linux_misc.h> 94 95 #ifdef __i386__ 96 #include <machine/cputypes.h> 97 #endif 98 99 #define BSD_TO_LINUX_SIGNAL(sig) \ 100 (((sig) <= LINUX_SIGTBLSZ) ? bsd_to_linux_signal[_SIG_IDX(sig)] : sig) 101 102 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = { 103 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK, 104 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE, 105 RLIMIT_MEMLOCK, RLIMIT_AS 106 }; 107 108 struct l_sysinfo { 109 l_long uptime; /* Seconds since boot */ 110 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */ 111 #define LINUX_SYSINFO_LOADS_SCALE 65536 112 l_ulong totalram; /* Total usable main memory size */ 113 l_ulong freeram; /* Available memory size */ 114 l_ulong sharedram; /* Amount of shared memory */ 115 l_ulong bufferram; /* Memory used by buffers */ 116 l_ulong totalswap; /* Total swap space size */ 117 l_ulong freeswap; /* swap space still available */ 118 l_ushort procs; /* Number of current processes */ 119 l_ushort pads; 120 l_ulong totalbig; 121 l_ulong freebig; 122 l_uint mem_unit; 123 char _f[20-2*sizeof(l_long)-sizeof(l_int)]; /* padding */ 124 }; 125 int 126 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args) 127 { 128 struct l_sysinfo sysinfo; 129 vm_object_t object; 130 int i, j; 131 struct timespec ts; 132 133 getnanouptime(&ts); 134 if (ts.tv_nsec != 0) 135 ts.tv_sec++; 136 sysinfo.uptime = ts.tv_sec; 137 138 /* Use the information from the mib to get our load averages */ 139 for (i = 0; i < 3; i++) 140 sysinfo.loads[i] = averunnable.ldavg[i] * 141 LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale; 142 143 sysinfo.totalram = physmem * PAGE_SIZE; 144 sysinfo.freeram = sysinfo.totalram - cnt.v_wire_count * PAGE_SIZE; 145 146 sysinfo.sharedram = 0; 147 mtx_lock(&vm_object_list_mtx); 148 TAILQ_FOREACH(object, &vm_object_list, object_list) 149 if (object->shadow_count > 1) 150 sysinfo.sharedram += object->resident_page_count; 151 mtx_unlock(&vm_object_list_mtx); 152 153 sysinfo.sharedram *= PAGE_SIZE; 154 sysinfo.bufferram = 0; 155 156 swap_pager_status(&i, &j); 157 sysinfo.totalswap = i * PAGE_SIZE; 158 sysinfo.freeswap = (i - j) * PAGE_SIZE; 159 160 sysinfo.procs = nprocs; 161 162 /* The following are only present in newer Linux kernels. */ 163 sysinfo.totalbig = 0; 164 sysinfo.freebig = 0; 165 sysinfo.mem_unit = 1; 166 167 return copyout(&sysinfo, args->info, sizeof(sysinfo)); 168 } 169 170 int 171 linux_alarm(struct thread *td, struct linux_alarm_args *args) 172 { 173 struct itimerval it, old_it; 174 u_int secs; 175 int error; 176 177 #ifdef DEBUG 178 if (ldebug(alarm)) 179 printf(ARGS(alarm, "%u"), args->secs); 180 #endif 181 182 secs = args->secs; 183 184 if (secs > INT_MAX) 185 secs = INT_MAX; 186 187 it.it_value.tv_sec = (long) secs; 188 it.it_value.tv_usec = 0; 189 it.it_interval.tv_sec = 0; 190 it.it_interval.tv_usec = 0; 191 error = kern_setitimer(td, ITIMER_REAL, &it, &old_it); 192 if (error) 193 return (error); 194 if (timevalisset(&old_it.it_value)) { 195 if (old_it.it_value.tv_usec != 0) 196 old_it.it_value.tv_sec++; 197 td->td_retval[0] = old_it.it_value.tv_sec; 198 } 199 return (0); 200 } 201 202 int 203 linux_brk(struct thread *td, struct linux_brk_args *args) 204 { 205 struct vmspace *vm = td->td_proc->p_vmspace; 206 vm_offset_t new, old; 207 struct obreak_args /* { 208 char * nsize; 209 } */ tmp; 210 211 #ifdef DEBUG 212 if (ldebug(brk)) 213 printf(ARGS(brk, "%p"), (void *)(uintptr_t)args->dsend); 214 #endif 215 old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize); 216 new = (vm_offset_t)args->dsend; 217 tmp.nsize = (char *)new; 218 if (((caddr_t)new > vm->vm_daddr) && !obreak(td, &tmp)) 219 td->td_retval[0] = (long)new; 220 else 221 td->td_retval[0] = (long)old; 222 223 return 0; 224 } 225 226 #if defined(__i386__) 227 /* XXX: what about amd64/linux32? */ 228 229 int 230 linux_uselib(struct thread *td, struct linux_uselib_args *args) 231 { 232 struct nameidata ni; 233 struct vnode *vp; 234 struct exec *a_out; 235 struct vattr attr; 236 vm_offset_t vmaddr; 237 unsigned long file_offset; 238 vm_offset_t buffer; 239 unsigned long bss_size; 240 char *library; 241 int error; 242 int locked, vfslocked; 243 244 LCONVPATHEXIST(td, args->library, &library); 245 246 #ifdef DEBUG 247 if (ldebug(uselib)) 248 printf(ARGS(uselib, "%s"), library); 249 #endif 250 251 a_out = NULL; 252 vfslocked = 0; 253 locked = 0; 254 vp = NULL; 255 256 NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1, 257 UIO_SYSSPACE, library, td); 258 error = namei(&ni); 259 LFREEPATH(library); 260 if (error) 261 goto cleanup; 262 263 vp = ni.ni_vp; 264 vfslocked = NDHASGIANT(&ni); 265 NDFREE(&ni, NDF_ONLY_PNBUF); 266 267 /* 268 * From here on down, we have a locked vnode that must be unlocked. 269 * XXX: The code below largely duplicates exec_check_permissions(). 270 */ 271 locked = 1; 272 273 /* Writable? */ 274 if (vp->v_writecount) { 275 error = ETXTBSY; 276 goto cleanup; 277 } 278 279 /* Executable? */ 280 error = VOP_GETATTR(vp, &attr, td->td_ucred); 281 if (error) 282 goto cleanup; 283 284 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || 285 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) { 286 /* EACCESS is what exec(2) returns. */ 287 error = ENOEXEC; 288 goto cleanup; 289 } 290 291 /* Sensible size? */ 292 if (attr.va_size == 0) { 293 error = ENOEXEC; 294 goto cleanup; 295 } 296 297 /* Can we access it? */ 298 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td); 299 if (error) 300 goto cleanup; 301 302 /* 303 * XXX: This should use vn_open() so that it is properly authorized, 304 * and to reduce code redundancy all over the place here. 305 * XXX: Not really, it duplicates far more of exec_check_permissions() 306 * than vn_open(). 307 */ 308 #ifdef MAC 309 error = mac_vnode_check_open(td->td_ucred, vp, VREAD); 310 if (error) 311 goto cleanup; 312 #endif 313 error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL); 314 if (error) 315 goto cleanup; 316 317 /* Pull in executable header into kernel_map */ 318 error = vm_mmap(kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE, 319 VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 0); 320 if (error) 321 goto cleanup; 322 323 /* Is it a Linux binary ? */ 324 if (((a_out->a_magic >> 16) & 0xff) != 0x64) { 325 error = ENOEXEC; 326 goto cleanup; 327 } 328 329 /* 330 * While we are here, we should REALLY do some more checks 331 */ 332 333 /* Set file/virtual offset based on a.out variant. */ 334 switch ((int)(a_out->a_magic & 0xffff)) { 335 case 0413: /* ZMAGIC */ 336 file_offset = 1024; 337 break; 338 case 0314: /* QMAGIC */ 339 file_offset = 0; 340 break; 341 default: 342 error = ENOEXEC; 343 goto cleanup; 344 } 345 346 bss_size = round_page(a_out->a_bss); 347 348 /* Check various fields in header for validity/bounds. */ 349 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) { 350 error = ENOEXEC; 351 goto cleanup; 352 } 353 354 /* text + data can't exceed file size */ 355 if (a_out->a_data + a_out->a_text > attr.va_size) { 356 error = EFAULT; 357 goto cleanup; 358 } 359 360 /* 361 * text/data/bss must not exceed limits 362 * XXX - this is not complete. it should check current usage PLUS 363 * the resources needed by this library. 364 */ 365 PROC_LOCK(td->td_proc); 366 if (a_out->a_text > maxtsiz || 367 a_out->a_data + bss_size > lim_cur(td->td_proc, RLIMIT_DATA)) { 368 PROC_UNLOCK(td->td_proc); 369 error = ENOMEM; 370 goto cleanup; 371 } 372 PROC_UNLOCK(td->td_proc); 373 374 /* 375 * Prevent more writers. 376 * XXX: Note that if any of the VM operations fail below we don't 377 * clear this flag. 378 */ 379 vp->v_vflag |= VV_TEXT; 380 381 /* 382 * Lock no longer needed 383 */ 384 locked = 0; 385 VOP_UNLOCK(vp, 0); 386 VFS_UNLOCK_GIANT(vfslocked); 387 388 /* 389 * Check if file_offset page aligned. Currently we cannot handle 390 * misalinged file offsets, and so we read in the entire image 391 * (what a waste). 392 */ 393 if (file_offset & PAGE_MASK) { 394 #ifdef DEBUG 395 printf("uselib: Non page aligned binary %lu\n", file_offset); 396 #endif 397 /* Map text+data read/write/execute */ 398 399 /* a_entry is the load address and is page aligned */ 400 vmaddr = trunc_page(a_out->a_entry); 401 402 /* get anon user mapping, read+write+execute */ 403 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0, 404 &vmaddr, a_out->a_text + a_out->a_data, FALSE, VM_PROT_ALL, 405 VM_PROT_ALL, 0); 406 if (error) 407 goto cleanup; 408 409 /* map file into kernel_map */ 410 error = vm_mmap(kernel_map, &buffer, 411 round_page(a_out->a_text + a_out->a_data + file_offset), 412 VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 413 trunc_page(file_offset)); 414 if (error) 415 goto cleanup; 416 417 /* copy from kernel VM space to user space */ 418 error = copyout(PTRIN(buffer + file_offset), 419 (void *)vmaddr, a_out->a_text + a_out->a_data); 420 421 /* release temporary kernel space */ 422 vm_map_remove(kernel_map, buffer, buffer + 423 round_page(a_out->a_text + a_out->a_data + file_offset)); 424 425 if (error) 426 goto cleanup; 427 } else { 428 #ifdef DEBUG 429 printf("uselib: Page aligned binary %lu\n", file_offset); 430 #endif 431 /* 432 * for QMAGIC, a_entry is 20 bytes beyond the load address 433 * to skip the executable header 434 */ 435 vmaddr = trunc_page(a_out->a_entry); 436 437 /* 438 * Map it all into the process's space as a single 439 * copy-on-write "data" segment. 440 */ 441 error = vm_mmap(&td->td_proc->p_vmspace->vm_map, &vmaddr, 442 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL, 443 MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset); 444 if (error) 445 goto cleanup; 446 } 447 #ifdef DEBUG 448 printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long *)vmaddr)[0], 449 ((long *)vmaddr)[1]); 450 #endif 451 if (bss_size != 0) { 452 /* Calculate BSS start address */ 453 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text + 454 a_out->a_data; 455 456 /* allocate some 'anon' space */ 457 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0, 458 &vmaddr, bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0); 459 if (error) 460 goto cleanup; 461 } 462 463 cleanup: 464 /* Unlock vnode if needed */ 465 if (locked) { 466 VOP_UNLOCK(vp, 0); 467 VFS_UNLOCK_GIANT(vfslocked); 468 } 469 470 /* Release the kernel mapping. */ 471 if (a_out) 472 vm_map_remove(kernel_map, (vm_offset_t)a_out, 473 (vm_offset_t)a_out + PAGE_SIZE); 474 475 return error; 476 } 477 478 #endif /* __i386__ */ 479 480 int 481 linux_select(struct thread *td, struct linux_select_args *args) 482 { 483 l_timeval ltv; 484 struct timeval tv0, tv1, utv, *tvp; 485 int error; 486 487 #ifdef DEBUG 488 if (ldebug(select)) 489 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds, 490 (void *)args->readfds, (void *)args->writefds, 491 (void *)args->exceptfds, (void *)args->timeout); 492 #endif 493 494 /* 495 * Store current time for computation of the amount of 496 * time left. 497 */ 498 if (args->timeout) { 499 if ((error = copyin(args->timeout, <v, sizeof(ltv)))) 500 goto select_out; 501 utv.tv_sec = ltv.tv_sec; 502 utv.tv_usec = ltv.tv_usec; 503 #ifdef DEBUG 504 if (ldebug(select)) 505 printf(LMSG("incoming timeout (%jd/%ld)"), 506 (intmax_t)utv.tv_sec, utv.tv_usec); 507 #endif 508 509 if (itimerfix(&utv)) { 510 /* 511 * The timeval was invalid. Convert it to something 512 * valid that will act as it does under Linux. 513 */ 514 utv.tv_sec += utv.tv_usec / 1000000; 515 utv.tv_usec %= 1000000; 516 if (utv.tv_usec < 0) { 517 utv.tv_sec -= 1; 518 utv.tv_usec += 1000000; 519 } 520 if (utv.tv_sec < 0) 521 timevalclear(&utv); 522 } 523 microtime(&tv0); 524 tvp = &utv; 525 } else 526 tvp = NULL; 527 528 error = kern_select(td, args->nfds, args->readfds, args->writefds, 529 args->exceptfds, tvp); 530 531 #ifdef DEBUG 532 if (ldebug(select)) 533 printf(LMSG("real select returns %d"), error); 534 #endif 535 if (error) 536 goto select_out; 537 538 if (args->timeout) { 539 if (td->td_retval[0]) { 540 /* 541 * Compute how much time was left of the timeout, 542 * by subtracting the current time and the time 543 * before we started the call, and subtracting 544 * that result from the user-supplied value. 545 */ 546 microtime(&tv1); 547 timevalsub(&tv1, &tv0); 548 timevalsub(&utv, &tv1); 549 if (utv.tv_sec < 0) 550 timevalclear(&utv); 551 } else 552 timevalclear(&utv); 553 #ifdef DEBUG 554 if (ldebug(select)) 555 printf(LMSG("outgoing timeout (%jd/%ld)"), 556 (intmax_t)utv.tv_sec, utv.tv_usec); 557 #endif 558 ltv.tv_sec = utv.tv_sec; 559 ltv.tv_usec = utv.tv_usec; 560 if ((error = copyout(<v, args->timeout, sizeof(ltv)))) 561 goto select_out; 562 } 563 564 select_out: 565 #ifdef DEBUG 566 if (ldebug(select)) 567 printf(LMSG("select_out -> %d"), error); 568 #endif 569 return error; 570 } 571 572 int 573 linux_mremap(struct thread *td, struct linux_mremap_args *args) 574 { 575 struct munmap_args /* { 576 void *addr; 577 size_t len; 578 } */ bsd_args; 579 int error = 0; 580 581 #ifdef DEBUG 582 if (ldebug(mremap)) 583 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"), 584 (void *)(uintptr_t)args->addr, 585 (unsigned long)args->old_len, 586 (unsigned long)args->new_len, 587 (unsigned long)args->flags); 588 #endif 589 590 if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) { 591 td->td_retval[0] = 0; 592 return (EINVAL); 593 } 594 595 /* 596 * Check for the page alignment. 597 * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK. 598 */ 599 if (args->addr & PAGE_MASK) { 600 td->td_retval[0] = 0; 601 return (EINVAL); 602 } 603 604 args->new_len = round_page(args->new_len); 605 args->old_len = round_page(args->old_len); 606 607 if (args->new_len > args->old_len) { 608 td->td_retval[0] = 0; 609 return ENOMEM; 610 } 611 612 if (args->new_len < args->old_len) { 613 bsd_args.addr = 614 (caddr_t)((uintptr_t)args->addr + args->new_len); 615 bsd_args.len = args->old_len - args->new_len; 616 error = munmap(td, &bsd_args); 617 } 618 619 td->td_retval[0] = error ? 0 : (uintptr_t)args->addr; 620 return error; 621 } 622 623 #define LINUX_MS_ASYNC 0x0001 624 #define LINUX_MS_INVALIDATE 0x0002 625 #define LINUX_MS_SYNC 0x0004 626 627 int 628 linux_msync(struct thread *td, struct linux_msync_args *args) 629 { 630 struct msync_args bsd_args; 631 632 bsd_args.addr = (caddr_t)(uintptr_t)args->addr; 633 bsd_args.len = (uintptr_t)args->len; 634 bsd_args.flags = args->fl & ~LINUX_MS_SYNC; 635 636 return msync(td, &bsd_args); 637 } 638 639 int 640 linux_time(struct thread *td, struct linux_time_args *args) 641 { 642 struct timeval tv; 643 l_time_t tm; 644 int error; 645 646 #ifdef DEBUG 647 if (ldebug(time)) 648 printf(ARGS(time, "*")); 649 #endif 650 651 microtime(&tv); 652 tm = tv.tv_sec; 653 if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm)))) 654 return error; 655 td->td_retval[0] = tm; 656 return 0; 657 } 658 659 struct l_times_argv { 660 l_long tms_utime; 661 l_long tms_stime; 662 l_long tms_cutime; 663 l_long tms_cstime; 664 }; 665 666 #define CLK_TCK 100 /* Linux uses 100 */ 667 668 #define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK)) 669 670 int 671 linux_times(struct thread *td, struct linux_times_args *args) 672 { 673 struct timeval tv, utime, stime, cutime, cstime; 674 struct l_times_argv tms; 675 struct proc *p; 676 int error; 677 678 #ifdef DEBUG 679 if (ldebug(times)) 680 printf(ARGS(times, "*")); 681 #endif 682 683 if (args->buf != NULL) { 684 p = td->td_proc; 685 PROC_LOCK(p); 686 PROC_SLOCK(p); 687 calcru(p, &utime, &stime); 688 PROC_SUNLOCK(p); 689 calccru(p, &cutime, &cstime); 690 PROC_UNLOCK(p); 691 692 tms.tms_utime = CONVTCK(utime); 693 tms.tms_stime = CONVTCK(stime); 694 695 tms.tms_cutime = CONVTCK(cutime); 696 tms.tms_cstime = CONVTCK(cstime); 697 698 if ((error = copyout(&tms, args->buf, sizeof(tms)))) 699 return error; 700 } 701 702 microuptime(&tv); 703 td->td_retval[0] = (int)CONVTCK(tv); 704 return 0; 705 } 706 707 int 708 linux_newuname(struct thread *td, struct linux_newuname_args *args) 709 { 710 struct l_new_utsname utsname; 711 char osname[LINUX_MAX_UTSNAME]; 712 char osrelease[LINUX_MAX_UTSNAME]; 713 char *p; 714 715 #ifdef DEBUG 716 if (ldebug(newuname)) 717 printf(ARGS(newuname, "*")); 718 #endif 719 720 linux_get_osname(td, osname); 721 linux_get_osrelease(td, osrelease); 722 723 bzero(&utsname, sizeof(utsname)); 724 strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME); 725 getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME); 726 strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME); 727 strlcpy(utsname.version, version, LINUX_MAX_UTSNAME); 728 for (p = utsname.version; *p != '\0'; ++p) 729 if (*p == '\n') { 730 *p = '\0'; 731 break; 732 } 733 #ifdef __i386__ 734 { 735 const char *class; 736 737 switch (cpu_class) { 738 case CPUCLASS_686: 739 class = "i686"; 740 break; 741 case CPUCLASS_586: 742 class = "i586"; 743 break; 744 case CPUCLASS_486: 745 class = "i486"; 746 break; 747 default: 748 class = "i386"; 749 } 750 strlcpy(utsname.machine, class, LINUX_MAX_UTSNAME); 751 } 752 #elif defined(__amd64__) /* XXX: Linux can change 'personality'. */ 753 #ifdef COMPAT_LINUX32 754 strlcpy(utsname.machine, "i686", LINUX_MAX_UTSNAME); 755 #else 756 strlcpy(utsname.machine, "x86_64", LINUX_MAX_UTSNAME); 757 #endif /* COMPAT_LINUX32 */ 758 #else /* something other than i386 or amd64 - assume we and Linux agree */ 759 strlcpy(utsname.machine, machine, LINUX_MAX_UTSNAME); 760 #endif /* __i386__ */ 761 mtx_lock(&hostname_mtx); 762 strlcpy(utsname.domainname, V_domainname, LINUX_MAX_UTSNAME); 763 mtx_unlock(&hostname_mtx); 764 765 return (copyout(&utsname, args->buf, sizeof(utsname))); 766 } 767 768 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 769 struct l_utimbuf { 770 l_time_t l_actime; 771 l_time_t l_modtime; 772 }; 773 774 int 775 linux_utime(struct thread *td, struct linux_utime_args *args) 776 { 777 struct timeval tv[2], *tvp; 778 struct l_utimbuf lut; 779 char *fname; 780 int error; 781 782 LCONVPATHEXIST(td, args->fname, &fname); 783 784 #ifdef DEBUG 785 if (ldebug(utime)) 786 printf(ARGS(utime, "%s, *"), fname); 787 #endif 788 789 if (args->times) { 790 if ((error = copyin(args->times, &lut, sizeof lut))) { 791 LFREEPATH(fname); 792 return error; 793 } 794 tv[0].tv_sec = lut.l_actime; 795 tv[0].tv_usec = 0; 796 tv[1].tv_sec = lut.l_modtime; 797 tv[1].tv_usec = 0; 798 tvp = tv; 799 } else 800 tvp = NULL; 801 802 error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE); 803 LFREEPATH(fname); 804 return (error); 805 } 806 807 int 808 linux_utimes(struct thread *td, struct linux_utimes_args *args) 809 { 810 l_timeval ltv[2]; 811 struct timeval tv[2], *tvp = NULL; 812 char *fname; 813 int error; 814 815 LCONVPATHEXIST(td, args->fname, &fname); 816 817 #ifdef DEBUG 818 if (ldebug(utimes)) 819 printf(ARGS(utimes, "%s, *"), fname); 820 #endif 821 822 if (args->tptr != NULL) { 823 if ((error = copyin(args->tptr, ltv, sizeof ltv))) { 824 LFREEPATH(fname); 825 return (error); 826 } 827 tv[0].tv_sec = ltv[0].tv_sec; 828 tv[0].tv_usec = ltv[0].tv_usec; 829 tv[1].tv_sec = ltv[1].tv_sec; 830 tv[1].tv_usec = ltv[1].tv_usec; 831 tvp = tv; 832 } 833 834 error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE); 835 LFREEPATH(fname); 836 return (error); 837 } 838 839 int 840 linux_futimesat(struct thread *td, struct linux_futimesat_args *args) 841 { 842 l_timeval ltv[2]; 843 struct timeval tv[2], *tvp = NULL; 844 char *fname; 845 int error, dfd; 846 847 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 848 LCONVPATHEXIST_AT(td, args->filename, &fname, dfd); 849 850 #ifdef DEBUG 851 if (ldebug(futimesat)) 852 printf(ARGS(futimesat, "%s, *"), fname); 853 #endif 854 855 if (args->utimes != NULL) { 856 if ((error = copyin(args->utimes, ltv, sizeof ltv))) { 857 LFREEPATH(fname); 858 return (error); 859 } 860 tv[0].tv_sec = ltv[0].tv_sec; 861 tv[0].tv_usec = ltv[0].tv_usec; 862 tv[1].tv_sec = ltv[1].tv_sec; 863 tv[1].tv_usec = ltv[1].tv_usec; 864 tvp = tv; 865 } 866 867 error = kern_utimesat(td, dfd, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE); 868 LFREEPATH(fname); 869 return (error); 870 } 871 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 872 873 #define __WCLONE 0x80000000 874 875 int 876 linux_waitpid(struct thread *td, struct linux_waitpid_args *args) 877 { 878 int error, options, tmpstat; 879 880 #ifdef DEBUG 881 if (ldebug(waitpid)) 882 printf(ARGS(waitpid, "%d, %p, %d"), 883 args->pid, (void *)args->status, args->options); 884 #endif 885 /* 886 * this is necessary because the test in kern_wait doesn't work 887 * because we mess with the options here 888 */ 889 if (args->options & ~(WUNTRACED | WNOHANG | WCONTINUED | __WCLONE)) 890 return (EINVAL); 891 892 options = (args->options & (WNOHANG | WUNTRACED)); 893 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */ 894 if (args->options & __WCLONE) 895 options |= WLINUXCLONE; 896 897 error = kern_wait(td, args->pid, &tmpstat, options, NULL); 898 if (error) 899 return error; 900 901 if (args->status) { 902 tmpstat &= 0xffff; 903 if (WIFSIGNALED(tmpstat)) 904 tmpstat = (tmpstat & 0xffffff80) | 905 BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat)); 906 else if (WIFSTOPPED(tmpstat)) 907 tmpstat = (tmpstat & 0xffff00ff) | 908 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8); 909 return copyout(&tmpstat, args->status, sizeof(int)); 910 } 911 912 return 0; 913 } 914 915 int 916 linux_wait4(struct thread *td, struct linux_wait4_args *args) 917 { 918 int error, options, tmpstat; 919 struct rusage ru, *rup; 920 struct proc *p; 921 922 #ifdef DEBUG 923 if (ldebug(wait4)) 924 printf(ARGS(wait4, "%d, %p, %d, %p"), 925 args->pid, (void *)args->status, args->options, 926 (void *)args->rusage); 927 #endif 928 929 options = (args->options & (WNOHANG | WUNTRACED)); 930 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */ 931 if (args->options & __WCLONE) 932 options |= WLINUXCLONE; 933 934 if (args->rusage != NULL) 935 rup = &ru; 936 else 937 rup = NULL; 938 error = kern_wait(td, args->pid, &tmpstat, options, rup); 939 if (error) 940 return error; 941 942 p = td->td_proc; 943 PROC_LOCK(p); 944 sigqueue_delete(&p->p_sigqueue, SIGCHLD); 945 PROC_UNLOCK(p); 946 947 if (args->status) { 948 tmpstat &= 0xffff; 949 if (WIFSIGNALED(tmpstat)) 950 tmpstat = (tmpstat & 0xffffff80) | 951 BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat)); 952 else if (WIFSTOPPED(tmpstat)) 953 tmpstat = (tmpstat & 0xffff00ff) | 954 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8); 955 error = copyout(&tmpstat, args->status, sizeof(int)); 956 } 957 if (args->rusage != NULL && error == 0) 958 error = copyout(&ru, args->rusage, sizeof(ru)); 959 960 return (error); 961 } 962 963 int 964 linux_mknod(struct thread *td, struct linux_mknod_args *args) 965 { 966 char *path; 967 int error; 968 969 LCONVPATHCREAT(td, args->path, &path); 970 971 #ifdef DEBUG 972 if (ldebug(mknod)) 973 printf(ARGS(mknod, "%s, %d, %d"), path, args->mode, args->dev); 974 #endif 975 976 switch (args->mode & S_IFMT) { 977 case S_IFIFO: 978 case S_IFSOCK: 979 error = kern_mkfifo(td, path, UIO_SYSSPACE, args->mode); 980 break; 981 982 case S_IFCHR: 983 case S_IFBLK: 984 error = kern_mknod(td, path, UIO_SYSSPACE, args->mode, 985 args->dev); 986 break; 987 988 case S_IFDIR: 989 error = EPERM; 990 break; 991 992 case 0: 993 args->mode |= S_IFREG; 994 /* FALLTHROUGH */ 995 case S_IFREG: 996 error = kern_open(td, path, UIO_SYSSPACE, 997 O_WRONLY | O_CREAT | O_TRUNC, args->mode); 998 if (error == 0) 999 kern_close(td, td->td_retval[0]); 1000 break; 1001 1002 default: 1003 error = EINVAL; 1004 break; 1005 } 1006 LFREEPATH(path); 1007 return (error); 1008 } 1009 1010 int 1011 linux_mknodat(struct thread *td, struct linux_mknodat_args *args) 1012 { 1013 char *path; 1014 int error, dfd; 1015 1016 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 1017 LCONVPATHCREAT_AT(td, args->filename, &path, dfd); 1018 1019 #ifdef DEBUG 1020 if (ldebug(mknodat)) 1021 printf(ARGS(mknodat, "%s, %d, %d"), path, args->mode, args->dev); 1022 #endif 1023 1024 switch (args->mode & S_IFMT) { 1025 case S_IFIFO: 1026 case S_IFSOCK: 1027 error = kern_mkfifoat(td, dfd, path, UIO_SYSSPACE, args->mode); 1028 break; 1029 1030 case S_IFCHR: 1031 case S_IFBLK: 1032 error = kern_mknodat(td, dfd, path, UIO_SYSSPACE, args->mode, 1033 args->dev); 1034 break; 1035 1036 case S_IFDIR: 1037 error = EPERM; 1038 break; 1039 1040 case 0: 1041 args->mode |= S_IFREG; 1042 /* FALLTHROUGH */ 1043 case S_IFREG: 1044 error = kern_openat(td, dfd, path, UIO_SYSSPACE, 1045 O_WRONLY | O_CREAT | O_TRUNC, args->mode); 1046 if (error == 0) 1047 kern_close(td, td->td_retval[0]); 1048 break; 1049 1050 default: 1051 error = EINVAL; 1052 break; 1053 } 1054 LFREEPATH(path); 1055 return (error); 1056 } 1057 1058 /* 1059 * UGH! This is just about the dumbest idea I've ever heard!! 1060 */ 1061 int 1062 linux_personality(struct thread *td, struct linux_personality_args *args) 1063 { 1064 #ifdef DEBUG 1065 if (ldebug(personality)) 1066 printf(ARGS(personality, "%lu"), (unsigned long)args->per); 1067 #endif 1068 if (args->per != 0) 1069 return EINVAL; 1070 1071 /* Yes Jim, it's still a Linux... */ 1072 td->td_retval[0] = 0; 1073 return 0; 1074 } 1075 1076 struct l_itimerval { 1077 l_timeval it_interval; 1078 l_timeval it_value; 1079 }; 1080 1081 #define B2L_ITIMERVAL(bip, lip) \ 1082 (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec; \ 1083 (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec; \ 1084 (bip)->it_value.tv_sec = (lip)->it_value.tv_sec; \ 1085 (bip)->it_value.tv_usec = (lip)->it_value.tv_usec; 1086 1087 int 1088 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap) 1089 { 1090 int error; 1091 struct l_itimerval ls; 1092 struct itimerval aitv, oitv; 1093 1094 #ifdef DEBUG 1095 if (ldebug(setitimer)) 1096 printf(ARGS(setitimer, "%p, %p"), 1097 (void *)uap->itv, (void *)uap->oitv); 1098 #endif 1099 1100 if (uap->itv == NULL) { 1101 uap->itv = uap->oitv; 1102 return (linux_getitimer(td, (struct linux_getitimer_args *)uap)); 1103 } 1104 1105 error = copyin(uap->itv, &ls, sizeof(ls)); 1106 if (error != 0) 1107 return (error); 1108 B2L_ITIMERVAL(&aitv, &ls); 1109 #ifdef DEBUG 1110 if (ldebug(setitimer)) { 1111 printf("setitimer: value: sec: %jd, usec: %ld\n", 1112 (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec); 1113 printf("setitimer: interval: sec: %jd, usec: %ld\n", 1114 (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec); 1115 } 1116 #endif 1117 error = kern_setitimer(td, uap->which, &aitv, &oitv); 1118 if (error != 0 || uap->oitv == NULL) 1119 return (error); 1120 B2L_ITIMERVAL(&ls, &oitv); 1121 1122 return (copyout(&ls, uap->oitv, sizeof(ls))); 1123 } 1124 1125 int 1126 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap) 1127 { 1128 int error; 1129 struct l_itimerval ls; 1130 struct itimerval aitv; 1131 1132 #ifdef DEBUG 1133 if (ldebug(getitimer)) 1134 printf(ARGS(getitimer, "%p"), (void *)uap->itv); 1135 #endif 1136 error = kern_getitimer(td, uap->which, &aitv); 1137 if (error != 0) 1138 return (error); 1139 B2L_ITIMERVAL(&ls, &aitv); 1140 return (copyout(&ls, uap->itv, sizeof(ls))); 1141 } 1142 1143 int 1144 linux_nice(struct thread *td, struct linux_nice_args *args) 1145 { 1146 struct setpriority_args bsd_args; 1147 1148 bsd_args.which = PRIO_PROCESS; 1149 bsd_args.who = 0; /* current process */ 1150 bsd_args.prio = args->inc; 1151 return setpriority(td, &bsd_args); 1152 } 1153 1154 int 1155 linux_setgroups(struct thread *td, struct linux_setgroups_args *args) 1156 { 1157 struct ucred *newcred, *oldcred; 1158 l_gid_t linux_gidset[NGROUPS]; 1159 gid_t *bsd_gidset; 1160 int ngrp, error; 1161 struct proc *p; 1162 1163 ngrp = args->gidsetsize; 1164 if (ngrp < 0 || ngrp >= NGROUPS) 1165 return (EINVAL); 1166 error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t)); 1167 if (error) 1168 return (error); 1169 newcred = crget(); 1170 p = td->td_proc; 1171 PROC_LOCK(p); 1172 oldcred = p->p_ucred; 1173 1174 /* 1175 * cr_groups[0] holds egid. Setting the whole set from 1176 * the supplied set will cause egid to be changed too. 1177 * Keep cr_groups[0] unchanged to prevent that. 1178 */ 1179 1180 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0) { 1181 PROC_UNLOCK(p); 1182 crfree(newcred); 1183 return (error); 1184 } 1185 1186 crcopy(newcred, oldcred); 1187 if (ngrp > 0) { 1188 newcred->cr_ngroups = ngrp + 1; 1189 1190 bsd_gidset = newcred->cr_groups; 1191 ngrp--; 1192 while (ngrp >= 0) { 1193 bsd_gidset[ngrp + 1] = linux_gidset[ngrp]; 1194 ngrp--; 1195 } 1196 } else 1197 newcred->cr_ngroups = 1; 1198 1199 setsugid(p); 1200 p->p_ucred = newcred; 1201 PROC_UNLOCK(p); 1202 crfree(oldcred); 1203 return (0); 1204 } 1205 1206 int 1207 linux_getgroups(struct thread *td, struct linux_getgroups_args *args) 1208 { 1209 struct ucred *cred; 1210 l_gid_t linux_gidset[NGROUPS]; 1211 gid_t *bsd_gidset; 1212 int bsd_gidsetsz, ngrp, error; 1213 1214 cred = td->td_ucred; 1215 bsd_gidset = cred->cr_groups; 1216 bsd_gidsetsz = cred->cr_ngroups - 1; 1217 1218 /* 1219 * cr_groups[0] holds egid. Returning the whole set 1220 * here will cause a duplicate. Exclude cr_groups[0] 1221 * to prevent that. 1222 */ 1223 1224 if ((ngrp = args->gidsetsize) == 0) { 1225 td->td_retval[0] = bsd_gidsetsz; 1226 return (0); 1227 } 1228 1229 if (ngrp < bsd_gidsetsz) 1230 return (EINVAL); 1231 1232 ngrp = 0; 1233 while (ngrp < bsd_gidsetsz) { 1234 linux_gidset[ngrp] = bsd_gidset[ngrp + 1]; 1235 ngrp++; 1236 } 1237 1238 if ((error = copyout(linux_gidset, args->grouplist, 1239 ngrp * sizeof(l_gid_t)))) 1240 return (error); 1241 1242 td->td_retval[0] = ngrp; 1243 return (0); 1244 } 1245 1246 int 1247 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args) 1248 { 1249 struct rlimit bsd_rlim; 1250 struct l_rlimit rlim; 1251 u_int which; 1252 int error; 1253 1254 #ifdef DEBUG 1255 if (ldebug(setrlimit)) 1256 printf(ARGS(setrlimit, "%d, %p"), 1257 args->resource, (void *)args->rlim); 1258 #endif 1259 1260 if (args->resource >= LINUX_RLIM_NLIMITS) 1261 return (EINVAL); 1262 1263 which = linux_to_bsd_resource[args->resource]; 1264 if (which == -1) 1265 return (EINVAL); 1266 1267 error = copyin(args->rlim, &rlim, sizeof(rlim)); 1268 if (error) 1269 return (error); 1270 1271 bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur; 1272 bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max; 1273 return (kern_setrlimit(td, which, &bsd_rlim)); 1274 } 1275 1276 int 1277 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args) 1278 { 1279 struct l_rlimit rlim; 1280 struct proc *p = td->td_proc; 1281 struct rlimit bsd_rlim; 1282 u_int which; 1283 1284 #ifdef DEBUG 1285 if (ldebug(old_getrlimit)) 1286 printf(ARGS(old_getrlimit, "%d, %p"), 1287 args->resource, (void *)args->rlim); 1288 #endif 1289 1290 if (args->resource >= LINUX_RLIM_NLIMITS) 1291 return (EINVAL); 1292 1293 which = linux_to_bsd_resource[args->resource]; 1294 if (which == -1) 1295 return (EINVAL); 1296 1297 PROC_LOCK(p); 1298 lim_rlimit(p, which, &bsd_rlim); 1299 PROC_UNLOCK(p); 1300 1301 #ifdef COMPAT_LINUX32 1302 rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur; 1303 if (rlim.rlim_cur == UINT_MAX) 1304 rlim.rlim_cur = INT_MAX; 1305 rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max; 1306 if (rlim.rlim_max == UINT_MAX) 1307 rlim.rlim_max = INT_MAX; 1308 #else 1309 rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur; 1310 if (rlim.rlim_cur == ULONG_MAX) 1311 rlim.rlim_cur = LONG_MAX; 1312 rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max; 1313 if (rlim.rlim_max == ULONG_MAX) 1314 rlim.rlim_max = LONG_MAX; 1315 #endif 1316 return (copyout(&rlim, args->rlim, sizeof(rlim))); 1317 } 1318 1319 int 1320 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args) 1321 { 1322 struct l_rlimit rlim; 1323 struct proc *p = td->td_proc; 1324 struct rlimit bsd_rlim; 1325 u_int which; 1326 1327 #ifdef DEBUG 1328 if (ldebug(getrlimit)) 1329 printf(ARGS(getrlimit, "%d, %p"), 1330 args->resource, (void *)args->rlim); 1331 #endif 1332 1333 if (args->resource >= LINUX_RLIM_NLIMITS) 1334 return (EINVAL); 1335 1336 which = linux_to_bsd_resource[args->resource]; 1337 if (which == -1) 1338 return (EINVAL); 1339 1340 PROC_LOCK(p); 1341 lim_rlimit(p, which, &bsd_rlim); 1342 PROC_UNLOCK(p); 1343 1344 rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur; 1345 rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max; 1346 return (copyout(&rlim, args->rlim, sizeof(rlim))); 1347 } 1348 1349 int 1350 linux_sched_setscheduler(struct thread *td, 1351 struct linux_sched_setscheduler_args *args) 1352 { 1353 struct sched_setscheduler_args bsd; 1354 1355 #ifdef DEBUG 1356 if (ldebug(sched_setscheduler)) 1357 printf(ARGS(sched_setscheduler, "%d, %d, %p"), 1358 args->pid, args->policy, (const void *)args->param); 1359 #endif 1360 1361 switch (args->policy) { 1362 case LINUX_SCHED_OTHER: 1363 bsd.policy = SCHED_OTHER; 1364 break; 1365 case LINUX_SCHED_FIFO: 1366 bsd.policy = SCHED_FIFO; 1367 break; 1368 case LINUX_SCHED_RR: 1369 bsd.policy = SCHED_RR; 1370 break; 1371 default: 1372 return EINVAL; 1373 } 1374 1375 bsd.pid = args->pid; 1376 bsd.param = (struct sched_param *)args->param; 1377 return sched_setscheduler(td, &bsd); 1378 } 1379 1380 int 1381 linux_sched_getscheduler(struct thread *td, 1382 struct linux_sched_getscheduler_args *args) 1383 { 1384 struct sched_getscheduler_args bsd; 1385 int error; 1386 1387 #ifdef DEBUG 1388 if (ldebug(sched_getscheduler)) 1389 printf(ARGS(sched_getscheduler, "%d"), args->pid); 1390 #endif 1391 1392 bsd.pid = args->pid; 1393 error = sched_getscheduler(td, &bsd); 1394 1395 switch (td->td_retval[0]) { 1396 case SCHED_OTHER: 1397 td->td_retval[0] = LINUX_SCHED_OTHER; 1398 break; 1399 case SCHED_FIFO: 1400 td->td_retval[0] = LINUX_SCHED_FIFO; 1401 break; 1402 case SCHED_RR: 1403 td->td_retval[0] = LINUX_SCHED_RR; 1404 break; 1405 } 1406 1407 return error; 1408 } 1409 1410 int 1411 linux_sched_get_priority_max(struct thread *td, 1412 struct linux_sched_get_priority_max_args *args) 1413 { 1414 struct sched_get_priority_max_args bsd; 1415 1416 #ifdef DEBUG 1417 if (ldebug(sched_get_priority_max)) 1418 printf(ARGS(sched_get_priority_max, "%d"), args->policy); 1419 #endif 1420 1421 switch (args->policy) { 1422 case LINUX_SCHED_OTHER: 1423 bsd.policy = SCHED_OTHER; 1424 break; 1425 case LINUX_SCHED_FIFO: 1426 bsd.policy = SCHED_FIFO; 1427 break; 1428 case LINUX_SCHED_RR: 1429 bsd.policy = SCHED_RR; 1430 break; 1431 default: 1432 return EINVAL; 1433 } 1434 return sched_get_priority_max(td, &bsd); 1435 } 1436 1437 int 1438 linux_sched_get_priority_min(struct thread *td, 1439 struct linux_sched_get_priority_min_args *args) 1440 { 1441 struct sched_get_priority_min_args bsd; 1442 1443 #ifdef DEBUG 1444 if (ldebug(sched_get_priority_min)) 1445 printf(ARGS(sched_get_priority_min, "%d"), args->policy); 1446 #endif 1447 1448 switch (args->policy) { 1449 case LINUX_SCHED_OTHER: 1450 bsd.policy = SCHED_OTHER; 1451 break; 1452 case LINUX_SCHED_FIFO: 1453 bsd.policy = SCHED_FIFO; 1454 break; 1455 case LINUX_SCHED_RR: 1456 bsd.policy = SCHED_RR; 1457 break; 1458 default: 1459 return EINVAL; 1460 } 1461 return sched_get_priority_min(td, &bsd); 1462 } 1463 1464 #define REBOOT_CAD_ON 0x89abcdef 1465 #define REBOOT_CAD_OFF 0 1466 #define REBOOT_HALT 0xcdef0123 1467 #define REBOOT_RESTART 0x01234567 1468 #define REBOOT_RESTART2 0xA1B2C3D4 1469 #define REBOOT_POWEROFF 0x4321FEDC 1470 #define REBOOT_MAGIC1 0xfee1dead 1471 #define REBOOT_MAGIC2 0x28121969 1472 #define REBOOT_MAGIC2A 0x05121996 1473 #define REBOOT_MAGIC2B 0x16041998 1474 1475 int 1476 linux_reboot(struct thread *td, struct linux_reboot_args *args) 1477 { 1478 struct reboot_args bsd_args; 1479 1480 #ifdef DEBUG 1481 if (ldebug(reboot)) 1482 printf(ARGS(reboot, "0x%x"), args->cmd); 1483 #endif 1484 1485 if (args->magic1 != REBOOT_MAGIC1) 1486 return EINVAL; 1487 1488 switch (args->magic2) { 1489 case REBOOT_MAGIC2: 1490 case REBOOT_MAGIC2A: 1491 case REBOOT_MAGIC2B: 1492 break; 1493 default: 1494 return EINVAL; 1495 } 1496 1497 switch (args->cmd) { 1498 case REBOOT_CAD_ON: 1499 case REBOOT_CAD_OFF: 1500 return (priv_check(td, PRIV_REBOOT)); 1501 case REBOOT_HALT: 1502 bsd_args.opt = RB_HALT; 1503 break; 1504 case REBOOT_RESTART: 1505 case REBOOT_RESTART2: 1506 bsd_args.opt = 0; 1507 break; 1508 case REBOOT_POWEROFF: 1509 bsd_args.opt = RB_POWEROFF; 1510 break; 1511 default: 1512 return EINVAL; 1513 } 1514 return reboot(td, &bsd_args); 1515 } 1516 1517 1518 /* 1519 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify 1520 * td->td_retval[1] when COMPAT_43 is defined. This clobbers registers that 1521 * are assumed to be preserved. The following lightweight syscalls fixes 1522 * this. See also linux_getgid16() and linux_getuid16() in linux_uid16.c 1523 * 1524 * linux_getpid() - MP SAFE 1525 * linux_getgid() - MP SAFE 1526 * linux_getuid() - MP SAFE 1527 */ 1528 1529 int 1530 linux_getpid(struct thread *td, struct linux_getpid_args *args) 1531 { 1532 struct linux_emuldata *em; 1533 1534 #ifdef DEBUG 1535 if (ldebug(getpid)) 1536 printf(ARGS(getpid, "")); 1537 #endif 1538 1539 if (linux_use26(td)) { 1540 em = em_find(td->td_proc, EMUL_DONTLOCK); 1541 KASSERT(em != NULL, ("getpid: emuldata not found.\n")); 1542 td->td_retval[0] = em->shared->group_pid; 1543 } else { 1544 td->td_retval[0] = td->td_proc->p_pid; 1545 } 1546 1547 return (0); 1548 } 1549 1550 int 1551 linux_gettid(struct thread *td, struct linux_gettid_args *args) 1552 { 1553 1554 #ifdef DEBUG 1555 if (ldebug(gettid)) 1556 printf(ARGS(gettid, "")); 1557 #endif 1558 1559 td->td_retval[0] = td->td_proc->p_pid; 1560 return (0); 1561 } 1562 1563 1564 int 1565 linux_getppid(struct thread *td, struct linux_getppid_args *args) 1566 { 1567 struct linux_emuldata *em; 1568 struct proc *p, *pp; 1569 1570 #ifdef DEBUG 1571 if (ldebug(getppid)) 1572 printf(ARGS(getppid, "")); 1573 #endif 1574 1575 if (!linux_use26(td)) { 1576 PROC_LOCK(td->td_proc); 1577 td->td_retval[0] = td->td_proc->p_pptr->p_pid; 1578 PROC_UNLOCK(td->td_proc); 1579 return (0); 1580 } 1581 1582 em = em_find(td->td_proc, EMUL_DONTLOCK); 1583 1584 KASSERT(em != NULL, ("getppid: process emuldata not found.\n")); 1585 1586 /* find the group leader */ 1587 p = pfind(em->shared->group_pid); 1588 1589 if (p == NULL) { 1590 #ifdef DEBUG 1591 printf(LMSG("parent process not found.\n")); 1592 #endif 1593 return (0); 1594 } 1595 1596 pp = p->p_pptr; /* switch to parent */ 1597 PROC_LOCK(pp); 1598 PROC_UNLOCK(p); 1599 1600 /* if its also linux process */ 1601 if (pp->p_sysent == &elf_linux_sysvec) { 1602 em = em_find(pp, EMUL_DONTLOCK); 1603 KASSERT(em != NULL, ("getppid: parent emuldata not found.\n")); 1604 1605 td->td_retval[0] = em->shared->group_pid; 1606 } else 1607 td->td_retval[0] = pp->p_pid; 1608 1609 PROC_UNLOCK(pp); 1610 1611 return (0); 1612 } 1613 1614 int 1615 linux_getgid(struct thread *td, struct linux_getgid_args *args) 1616 { 1617 1618 #ifdef DEBUG 1619 if (ldebug(getgid)) 1620 printf(ARGS(getgid, "")); 1621 #endif 1622 1623 td->td_retval[0] = td->td_ucred->cr_rgid; 1624 return (0); 1625 } 1626 1627 int 1628 linux_getuid(struct thread *td, struct linux_getuid_args *args) 1629 { 1630 1631 #ifdef DEBUG 1632 if (ldebug(getuid)) 1633 printf(ARGS(getuid, "")); 1634 #endif 1635 1636 td->td_retval[0] = td->td_ucred->cr_ruid; 1637 return (0); 1638 } 1639 1640 1641 int 1642 linux_getsid(struct thread *td, struct linux_getsid_args *args) 1643 { 1644 struct getsid_args bsd; 1645 1646 #ifdef DEBUG 1647 if (ldebug(getsid)) 1648 printf(ARGS(getsid, "%i"), args->pid); 1649 #endif 1650 1651 bsd.pid = args->pid; 1652 return getsid(td, &bsd); 1653 } 1654 1655 int 1656 linux_nosys(struct thread *td, struct nosys_args *ignore) 1657 { 1658 1659 return (ENOSYS); 1660 } 1661 1662 int 1663 linux_getpriority(struct thread *td, struct linux_getpriority_args *args) 1664 { 1665 struct getpriority_args bsd_args; 1666 int error; 1667 1668 #ifdef DEBUG 1669 if (ldebug(getpriority)) 1670 printf(ARGS(getpriority, "%i, %i"), args->which, args->who); 1671 #endif 1672 1673 bsd_args.which = args->which; 1674 bsd_args.who = args->who; 1675 error = getpriority(td, &bsd_args); 1676 td->td_retval[0] = 20 - td->td_retval[0]; 1677 return error; 1678 } 1679 1680 int 1681 linux_sethostname(struct thread *td, struct linux_sethostname_args *args) 1682 { 1683 int name[2]; 1684 1685 #ifdef DEBUG 1686 if (ldebug(sethostname)) 1687 printf(ARGS(sethostname, "*, %i"), args->len); 1688 #endif 1689 1690 name[0] = CTL_KERN; 1691 name[1] = KERN_HOSTNAME; 1692 return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname, 1693 args->len, 0, 0)); 1694 } 1695 1696 int 1697 linux_exit_group(struct thread *td, struct linux_exit_group_args *args) 1698 { 1699 struct linux_emuldata *em, *td_em, *tmp_em; 1700 struct proc *sp; 1701 1702 #ifdef DEBUG 1703 if (ldebug(exit_group)) 1704 printf(ARGS(exit_group, "%i"), args->error_code); 1705 #endif 1706 1707 if (linux_use26(td)) { 1708 td_em = em_find(td->td_proc, EMUL_DONTLOCK); 1709 1710 KASSERT(td_em != NULL, ("exit_group: emuldata not found.\n")); 1711 1712 EMUL_SHARED_RLOCK(&emul_shared_lock); 1713 LIST_FOREACH_SAFE(em, &td_em->shared->threads, threads, tmp_em) { 1714 if (em->pid == td_em->pid) 1715 continue; 1716 1717 sp = pfind(em->pid); 1718 psignal(sp, SIGKILL); 1719 PROC_UNLOCK(sp); 1720 #ifdef DEBUG 1721 printf(LMSG("linux_sys_exit_group: kill PID %d\n"), em->pid); 1722 #endif 1723 } 1724 1725 EMUL_SHARED_RUNLOCK(&emul_shared_lock); 1726 } 1727 /* 1728 * XXX: we should send a signal to the parent if 1729 * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?) 1730 * as it doesnt occur often. 1731 */ 1732 exit1(td, W_EXITCODE(args->error_code, 0)); 1733 1734 return (0); 1735 } 1736 1737 int 1738 linux_prctl(struct thread *td, struct linux_prctl_args *args) 1739 { 1740 int error = 0, max_size; 1741 struct proc *p = td->td_proc; 1742 char comm[LINUX_MAX_COMM_LEN]; 1743 struct linux_emuldata *em; 1744 int pdeath_signal; 1745 1746 #ifdef DEBUG 1747 if (ldebug(prctl)) 1748 printf(ARGS(prctl, "%d, %d, %d, %d, %d"), args->option, 1749 args->arg2, args->arg3, args->arg4, args->arg5); 1750 #endif 1751 1752 switch (args->option) { 1753 case LINUX_PR_SET_PDEATHSIG: 1754 if (!LINUX_SIG_VALID(args->arg2)) 1755 return (EINVAL); 1756 em = em_find(p, EMUL_DOLOCK); 1757 KASSERT(em != NULL, ("prctl: emuldata not found.\n")); 1758 em->pdeath_signal = args->arg2; 1759 EMUL_UNLOCK(&emul_lock); 1760 break; 1761 case LINUX_PR_GET_PDEATHSIG: 1762 em = em_find(p, EMUL_DOLOCK); 1763 KASSERT(em != NULL, ("prctl: emuldata not found.\n")); 1764 pdeath_signal = em->pdeath_signal; 1765 EMUL_UNLOCK(&emul_lock); 1766 error = copyout(&pdeath_signal, 1767 (void *)(register_t)args->arg2, 1768 sizeof(pdeath_signal)); 1769 break; 1770 case LINUX_PR_SET_NAME: 1771 /* 1772 * To be on the safe side we need to make sure to not 1773 * overflow the size a linux program expects. We already 1774 * do this here in the copyin, so that we don't need to 1775 * check on copyout. 1776 */ 1777 max_size = MIN(sizeof(comm), sizeof(p->p_comm)); 1778 error = copyinstr((void *)(register_t)args->arg2, comm, 1779 max_size, NULL); 1780 1781 /* Linux silently truncates the name if it is too long. */ 1782 if (error == ENAMETOOLONG) { 1783 /* 1784 * XXX: copyinstr() isn't documented to populate the 1785 * array completely, so do a copyin() to be on the 1786 * safe side. This should be changed in case 1787 * copyinstr() is changed to guarantee this. 1788 */ 1789 error = copyin((void *)(register_t)args->arg2, comm, 1790 max_size - 1); 1791 comm[max_size - 1] = '\0'; 1792 } 1793 if (error) 1794 return (error); 1795 1796 PROC_LOCK(p); 1797 strlcpy(p->p_comm, comm, sizeof(p->p_comm)); 1798 PROC_UNLOCK(p); 1799 break; 1800 case LINUX_PR_GET_NAME: 1801 PROC_LOCK(p); 1802 strlcpy(comm, p->p_comm, sizeof(comm)); 1803 PROC_UNLOCK(p); 1804 error = copyout(comm, (void *)(register_t)args->arg2, 1805 strlen(comm) + 1); 1806 break; 1807 default: 1808 error = EINVAL; 1809 break; 1810 } 1811 1812 return (error); 1813 } 1814 1815 /* 1816 * Get affinity of a process. 1817 */ 1818 int 1819 linux_sched_getaffinity(struct thread *td, 1820 struct linux_sched_getaffinity_args *args) 1821 { 1822 int error; 1823 struct cpuset_getaffinity_args cga; 1824 1825 #ifdef DEBUG 1826 if (ldebug(sched_getaffinity)) 1827 printf(ARGS(sched_getaffinity, "%d, %d, *"), args->pid, 1828 args->len); 1829 #endif 1830 1831 cga.level = CPU_LEVEL_WHICH; 1832 cga.which = CPU_WHICH_PID; 1833 cga.id = args->pid; 1834 cga.cpusetsize = sizeof(cpumask_t); 1835 cga.mask = (cpuset_t *) args->user_mask_ptr; 1836 1837 if ((error = cpuset_getaffinity(td, &cga)) == 0) 1838 td->td_retval[0] = sizeof(cpumask_t); 1839 1840 return (error); 1841 } 1842 1843 /* 1844 * Set affinity of a process. 1845 */ 1846 int 1847 linux_sched_setaffinity(struct thread *td, 1848 struct linux_sched_setaffinity_args *args) 1849 { 1850 struct cpuset_setaffinity_args csa; 1851 1852 #ifdef DEBUG 1853 if (ldebug(sched_setaffinity)) 1854 printf(ARGS(sched_setaffinity, "%d, %d, *"), args->pid, 1855 args->len); 1856 #endif 1857 csa.level = CPU_LEVEL_WHICH; 1858 csa.which = CPU_WHICH_PID; 1859 csa.id = args->pid; 1860 csa.cpusetsize = args->len; 1861 csa.mask = (cpuset_t *) args->user_mask_ptr; 1862 1863 return (cpuset_setaffinity(td, &csa)); 1864 } 1865