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 67#include <security/mac/mac_framework.h> 68 69#include <vm/vm.h> 70#include <vm/pmap.h> 71#include <vm/vm_kern.h> 72#include <vm/vm_map.h> 73#include <vm/vm_extern.h> 74#include <vm/vm_object.h> 75#include <vm/swap_pager.h> 76 77#include <compat/linux/linux_sysproto.h> 78#include <compat/linux/linux_emul.h> 79#include <compat/linux/linux_misc.h> 80 81#ifdef COMPAT_LINUX32 82#include <machine/../linux32/linux.h> 83#include <machine/../linux32/linux32_proto.h> 84#else 85#include <machine/../linux/linux.h> 86#include <machine/../linux/linux_proto.h> 87#endif 88 89#include <compat/linux/linux_mib.h> 90#include <compat/linux/linux_signal.h> 91#include <compat/linux/linux_util.h> 92 93#ifdef __i386__ 94#include <machine/cputypes.h> 95#endif 96 97#define BSD_TO_LINUX_SIGNAL(sig) \ 98 (((sig) <= LINUX_SIGTBLSZ) ? bsd_to_linux_signal[_SIG_IDX(sig)] : sig) 99 100static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = { 101 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK, 102 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE, 103 RLIMIT_MEMLOCK, -1 104}; 105 106struct l_sysinfo { 107 l_long uptime; /* Seconds since boot */ 108 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */ 109#define LINUX_SYSINFO_LOADS_SCALE 65536 110 l_ulong totalram; /* Total usable main memory size */ 111 l_ulong freeram; /* Available memory size */ 112 l_ulong sharedram; /* Amount of shared memory */ 113 l_ulong bufferram; /* Memory used by buffers */ 114 l_ulong totalswap; /* Total swap space size */ 115 l_ulong freeswap; /* swap space still available */ 116 l_ushort procs; /* Number of current processes */ 117 l_ushort pads; 118 l_ulong totalbig; 119 l_ulong freebig; 120 l_uint mem_unit; 121 char _f[20-2*sizeof(l_long)-sizeof(l_int)]; /* padding */ 122}; 123int 124linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args) 125{ 126 struct l_sysinfo sysinfo; 127 vm_object_t object; 128 int i, j; 129 struct timespec ts; 130 131 getnanouptime(&ts); 132 if (ts.tv_nsec != 0) 133 ts.tv_sec++; 134 sysinfo.uptime = ts.tv_sec; 135 136 /* Use the information from the mib to get our load averages */ 137 for (i = 0; i < 3; i++) 138 sysinfo.loads[i] = averunnable.ldavg[i] * 139 LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale; 140 141 sysinfo.totalram = physmem * PAGE_SIZE; 142 sysinfo.freeram = sysinfo.totalram - cnt.v_wire_count * PAGE_SIZE; 143 144 sysinfo.sharedram = 0; 145 mtx_lock(&vm_object_list_mtx); 146 TAILQ_FOREACH(object, &vm_object_list, object_list) 147 if (object->shadow_count > 1) 148 sysinfo.sharedram += object->resident_page_count; 149 mtx_unlock(&vm_object_list_mtx); 150 151 sysinfo.sharedram *= PAGE_SIZE; 152 sysinfo.bufferram = 0; 153 154 swap_pager_status(&i, &j); 155 sysinfo.totalswap = i * PAGE_SIZE; 156 sysinfo.freeswap = (i - j) * PAGE_SIZE; 157 158 sysinfo.procs = nprocs; 159 160 /* The following are only present in newer Linux kernels. */ 161 sysinfo.totalbig = 0; 162 sysinfo.freebig = 0; 163 sysinfo.mem_unit = 1; 164 165 return copyout(&sysinfo, args->info, sizeof(sysinfo)); 166} 167 168int 169linux_alarm(struct thread *td, struct linux_alarm_args *args) 170{ 171 struct itimerval it, old_it; 172 int error; 173 174#ifdef DEBUG 175 if (ldebug(alarm)) 176 printf(ARGS(alarm, "%u"), args->secs); 177#endif 178 179 if (args->secs > 100000000) 180 return (EINVAL); 181 182 it.it_value.tv_sec = (long)args->secs; 183 it.it_value.tv_usec = 0; 184 it.it_interval.tv_sec = 0; 185 it.it_interval.tv_usec = 0; 186 error = kern_setitimer(td, ITIMER_REAL, &it, &old_it); 187 if (error) 188 return (error); 189 if (timevalisset(&old_it.it_value)) { 190 if (old_it.it_value.tv_usec != 0) 191 old_it.it_value.tv_sec++; 192 td->td_retval[0] = old_it.it_value.tv_sec; 193 } 194 return (0); 195} 196 197int 198linux_brk(struct thread *td, struct linux_brk_args *args) 199{ 200 struct vmspace *vm = td->td_proc->p_vmspace; 201 vm_offset_t new, old; 202 struct obreak_args /* { 203 char * nsize; 204 } */ tmp; 205 206#ifdef DEBUG 207 if (ldebug(brk)) 208 printf(ARGS(brk, "%p"), (void *)(uintptr_t)args->dsend); 209#endif 210 old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize); 211 new = (vm_offset_t)args->dsend; 212 tmp.nsize = (char *)new; 213 if (((caddr_t)new > vm->vm_daddr) && !obreak(td, &tmp)) 214 td->td_retval[0] = (long)new; 215 else 216 td->td_retval[0] = (long)old; 217 218 return 0; 219} 220 221#if defined(__i386__) 222/* XXX: what about amd64/linux32? */ 223 224int 225linux_uselib(struct thread *td, struct linux_uselib_args *args) 226{ 227 struct nameidata ni; 228 struct vnode *vp; 229 struct exec *a_out; 230 struct vattr attr; 231 vm_offset_t vmaddr; 232 unsigned long file_offset; 233 vm_offset_t buffer; 234 unsigned long bss_size; 235 char *library; 236 int error; 237 int locked, vfslocked; 238 239 LCONVPATHEXIST(td, args->library, &library); 240 241#ifdef DEBUG 242 if (ldebug(uselib)) 243 printf(ARGS(uselib, "%s"), library); 244#endif 245 246 a_out = NULL; 247 vfslocked = 0; 248 locked = 0; 249 vp = NULL; 250 251 NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1, 252 UIO_SYSSPACE, library, td); 253 error = namei(&ni); 254 LFREEPATH(library); 255 if (error) 256 goto cleanup; 257 258 vp = ni.ni_vp; 259 vfslocked = NDHASGIANT(&ni); 260 NDFREE(&ni, NDF_ONLY_PNBUF); 261 262 /* 263 * From here on down, we have a locked vnode that must be unlocked. 264 * XXX: The code below largely duplicates exec_check_permissions(). 265 */ 266 locked = 1; 267 268 /* Writable? */ 269 if (vp->v_writecount) { 270 error = ETXTBSY; 271 goto cleanup; 272 } 273 274 /* Executable? */ 275 error = VOP_GETATTR(vp, &attr, td->td_ucred, td); 276 if (error) 277 goto cleanup; 278 279 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || 280 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) { 281 /* EACCESS is what exec(2) returns. */ 282 error = ENOEXEC; 283 goto cleanup; 284 } 285 286 /* Sensible size? */ 287 if (attr.va_size == 0) { 288 error = ENOEXEC; 289 goto cleanup; 290 } 291 292 /* Can we access it? */ 293 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td); 294 if (error) 295 goto cleanup; 296 297 /* 298 * XXX: This should use vn_open() so that it is properly authorized, 299 * and to reduce code redundancy all over the place here. 300 * XXX: Not really, it duplicates far more of exec_check_permissions() 301 * than vn_open(). 302 */ 303#ifdef MAC 304 error = mac_check_vnode_open(td->td_ucred, vp, FREAD); 305 if (error) 306 goto cleanup; 307#endif 308 error = VOP_OPEN(vp, FREAD, td->td_ucred, td, -1); 309 if (error) 310 goto cleanup; 311 312 /* Pull in executable header into kernel_map */ 313 error = vm_mmap(kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE, 314 VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 0); 315 if (error) 316 goto cleanup; 317 318 /* Is it a Linux binary ? */ 319 if (((a_out->a_magic >> 16) & 0xff) != 0x64) { 320 error = ENOEXEC; 321 goto cleanup; 322 } 323 324 /* 325 * While we are here, we should REALLY do some more checks 326 */ 327 328 /* Set file/virtual offset based on a.out variant. */ 329 switch ((int)(a_out->a_magic & 0xffff)) { 330 case 0413: /* ZMAGIC */ 331 file_offset = 1024; 332 break; 333 case 0314: /* QMAGIC */ 334 file_offset = 0; 335 break; 336 default: 337 error = ENOEXEC; 338 goto cleanup; 339 } 340 341 bss_size = round_page(a_out->a_bss); 342 343 /* Check various fields in header for validity/bounds. */ 344 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) { 345 error = ENOEXEC; 346 goto cleanup; 347 } 348 349 /* text + data can't exceed file size */ 350 if (a_out->a_data + a_out->a_text > attr.va_size) { 351 error = EFAULT; 352 goto cleanup; 353 } 354 355 /* 356 * text/data/bss must not exceed limits 357 * XXX - this is not complete. it should check current usage PLUS 358 * the resources needed by this library. 359 */ 360 PROC_LOCK(td->td_proc); 361 if (a_out->a_text > maxtsiz || 362 a_out->a_data + bss_size > lim_cur(td->td_proc, RLIMIT_DATA)) { 363 PROC_UNLOCK(td->td_proc); 364 error = ENOMEM; 365 goto cleanup; 366 } 367 PROC_UNLOCK(td->td_proc); 368 369 /* 370 * Prevent more writers. 371 * XXX: Note that if any of the VM operations fail below we don't 372 * clear this flag. 373 */ 374 vp->v_vflag |= VV_TEXT; 375 376 /* 377 * Lock no longer needed 378 */ 379 locked = 0; 380 VOP_UNLOCK(vp, 0, td); 381 VFS_UNLOCK_GIANT(vfslocked); 382 383 /* 384 * Check if file_offset page aligned. Currently we cannot handle 385 * misalinged file offsets, and so we read in the entire image 386 * (what a waste). 387 */ 388 if (file_offset & PAGE_MASK) { 389#ifdef DEBUG 390 printf("uselib: Non page aligned binary %lu\n", file_offset); 391#endif 392 /* Map text+data read/write/execute */ 393 394 /* a_entry is the load address and is page aligned */ 395 vmaddr = trunc_page(a_out->a_entry); 396 397 /* get anon user mapping, read+write+execute */ 398 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0, 399 &vmaddr, a_out->a_text + a_out->a_data, FALSE, VM_PROT_ALL, 400 VM_PROT_ALL, 0); 401 if (error) 402 goto cleanup; 403 404 /* map file into kernel_map */ 405 error = vm_mmap(kernel_map, &buffer, 406 round_page(a_out->a_text + a_out->a_data + file_offset), 407 VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 408 trunc_page(file_offset)); 409 if (error) 410 goto cleanup; 411 412 /* copy from kernel VM space to user space */ 413 error = copyout(PTRIN(buffer + file_offset), 414 (void *)vmaddr, a_out->a_text + a_out->a_data); 415 416 /* release temporary kernel space */ 417 vm_map_remove(kernel_map, buffer, buffer + 418 round_page(a_out->a_text + a_out->a_data + file_offset)); 419 420 if (error) 421 goto cleanup; 422 } else { 423#ifdef DEBUG 424 printf("uselib: Page aligned binary %lu\n", file_offset); 425#endif 426 /* 427 * for QMAGIC, a_entry is 20 bytes beyond the load address 428 * to skip the executable header 429 */ 430 vmaddr = trunc_page(a_out->a_entry); 431 432 /* 433 * Map it all into the process's space as a single 434 * copy-on-write "data" segment. 435 */ 436 error = vm_mmap(&td->td_proc->p_vmspace->vm_map, &vmaddr, 437 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL, 438 MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset); 439 if (error) 440 goto cleanup; 441 } 442#ifdef DEBUG 443 printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long *)vmaddr)[0], 444 ((long *)vmaddr)[1]); 445#endif 446 if (bss_size != 0) { 447 /* Calculate BSS start address */ 448 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text + 449 a_out->a_data; 450 451 /* allocate some 'anon' space */ 452 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0, 453 &vmaddr, bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0); 454 if (error) 455 goto cleanup; 456 } 457 458cleanup: 459 /* Unlock vnode if needed */ 460 if (locked) { 461 VOP_UNLOCK(vp, 0, td); 462 VFS_UNLOCK_GIANT(vfslocked); 463 } 464 465 /* Release the kernel mapping. */ 466 if (a_out) 467 vm_map_remove(kernel_map, (vm_offset_t)a_out, 468 (vm_offset_t)a_out + PAGE_SIZE); 469 470 return error; 471} 472 473#endif /* __i386__ */ 474 475int 476linux_select(struct thread *td, struct linux_select_args *args) 477{ 478 l_timeval ltv; 479 struct timeval tv0, tv1, utv, *tvp; 480 int error; 481 482#ifdef DEBUG 483 if (ldebug(select)) 484 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds, 485 (void *)args->readfds, (void *)args->writefds, 486 (void *)args->exceptfds, (void *)args->timeout); 487#endif 488 489 /* 490 * Store current time for computation of the amount of 491 * time left. 492 */ 493 if (args->timeout) { 494 if ((error = copyin(args->timeout, <v, sizeof(ltv)))) 495 goto select_out; 496 utv.tv_sec = ltv.tv_sec; 497 utv.tv_usec = ltv.tv_usec; 498#ifdef DEBUG 499 if (ldebug(select)) 500 printf(LMSG("incoming timeout (%jd/%ld)"), 501 (intmax_t)utv.tv_sec, utv.tv_usec); 502#endif 503 504 if (itimerfix(&utv)) { 505 /* 506 * The timeval was invalid. Convert it to something 507 * valid that will act as it does under Linux. 508 */ 509 utv.tv_sec += utv.tv_usec / 1000000; 510 utv.tv_usec %= 1000000; 511 if (utv.tv_usec < 0) { 512 utv.tv_sec -= 1; 513 utv.tv_usec += 1000000; 514 } 515 if (utv.tv_sec < 0) 516 timevalclear(&utv); 517 } 518 microtime(&tv0); 519 tvp = &utv; 520 } else 521 tvp = NULL; 522 523 error = kern_select(td, args->nfds, args->readfds, args->writefds, 524 args->exceptfds, tvp); 525 526#ifdef DEBUG 527 if (ldebug(select)) 528 printf(LMSG("real select returns %d"), error); 529#endif 530 if (error) { 531 /* 532 * See fs/select.c in the Linux kernel. Without this, 533 * Maelstrom doesn't work. 534 */ 535 if (error == ERESTART) 536 error = EINTR; 537 goto select_out; 538 } 539 540 if (args->timeout) { 541 if (td->td_retval[0]) { 542 /* 543 * Compute how much time was left of the timeout, 544 * by subtracting the current time and the time 545 * before we started the call, and subtracting 546 * that result from the user-supplied value. 547 */ 548 microtime(&tv1); 549 timevalsub(&tv1, &tv0); 550 timevalsub(&utv, &tv1); 551 if (utv.tv_sec < 0) 552 timevalclear(&utv); 553 } else 554 timevalclear(&utv); 555#ifdef DEBUG 556 if (ldebug(select)) 557 printf(LMSG("outgoing timeout (%jd/%ld)"), 558 (intmax_t)utv.tv_sec, utv.tv_usec); 559#endif 560 ltv.tv_sec = utv.tv_sec; 561 ltv.tv_usec = utv.tv_usec; 562 if ((error = copyout(<v, args->timeout, sizeof(ltv)))) 563 goto select_out; 564 } 565 566select_out: 567#ifdef DEBUG 568 if (ldebug(select)) 569 printf(LMSG("select_out -> %d"), error); 570#endif 571 return error; 572} 573 574int 575linux_mremap(struct thread *td, struct linux_mremap_args *args) 576{ 577 struct munmap_args /* { 578 void *addr; 579 size_t len; 580 } */ bsd_args; 581 int error = 0; 582 583#ifdef DEBUG 584 if (ldebug(mremap)) 585 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"), 586 (void *)(uintptr_t)args->addr, 587 (unsigned long)args->old_len, 588 (unsigned long)args->new_len, 589 (unsigned long)args->flags); 590#endif 591 args->new_len = round_page(args->new_len); 592 args->old_len = round_page(args->old_len); 593 594 if (args->new_len > args->old_len) { 595 td->td_retval[0] = 0; 596 return ENOMEM; 597 } 598 599 if (args->new_len < args->old_len) { 600 bsd_args.addr = 601 (caddr_t)((uintptr_t)args->addr + args->new_len); 602 bsd_args.len = args->old_len - args->new_len; 603 error = munmap(td, &bsd_args); 604 } 605 606 td->td_retval[0] = error ? 0 : (uintptr_t)args->addr; 607 return error; 608} 609 610#define LINUX_MS_ASYNC 0x0001 611#define LINUX_MS_INVALIDATE 0x0002 612#define LINUX_MS_SYNC 0x0004 613 614int 615linux_msync(struct thread *td, struct linux_msync_args *args) 616{ 617 struct msync_args bsd_args; 618 619 bsd_args.addr = (caddr_t)(uintptr_t)args->addr; 620 bsd_args.len = (uintptr_t)args->len; 621 bsd_args.flags = args->fl & ~LINUX_MS_SYNC; 622 623 return msync(td, &bsd_args); 624} 625 626int 627linux_time(struct thread *td, struct linux_time_args *args) 628{ 629 struct timeval tv; 630 l_time_t tm; 631 int error; 632 633#ifdef DEBUG 634 if (ldebug(time)) 635 printf(ARGS(time, "*")); 636#endif 637 638 microtime(&tv); 639 tm = tv.tv_sec; 640 if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm)))) 641 return error; 642 td->td_retval[0] = tm; 643 return 0; 644} 645 646struct l_times_argv { 647 l_long tms_utime; 648 l_long tms_stime; 649 l_long tms_cutime; 650 l_long tms_cstime; 651}; 652 653#define CLK_TCK 100 /* Linux uses 100 */ 654 655#define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK)) 656 657int 658linux_times(struct thread *td, struct linux_times_args *args) 659{ 660 struct timeval tv, utime, stime, cutime, cstime; 661 struct l_times_argv tms; 662 struct proc *p; 663 int error; 664 665#ifdef DEBUG 666 if (ldebug(times)) 667 printf(ARGS(times, "*")); 668#endif 669 670 if (args->buf != NULL) { 671 p = td->td_proc; 672 PROC_LOCK(p); 673 calcru(p, &utime, &stime); 674 calccru(p, &cutime, &cstime); 675 PROC_UNLOCK(p); 676 677 tms.tms_utime = CONVTCK(utime); 678 tms.tms_stime = CONVTCK(stime); 679 680 tms.tms_cutime = CONVTCK(cutime); 681 tms.tms_cstime = CONVTCK(cstime); 682 683 if ((error = copyout(&tms, args->buf, sizeof(tms)))) 684 return error; 685 } 686 687 microuptime(&tv); 688 td->td_retval[0] = (int)CONVTCK(tv); 689 return 0; 690} 691 692int 693linux_newuname(struct thread *td, struct linux_newuname_args *args) 694{ 695 struct l_new_utsname utsname; 696 char osname[LINUX_MAX_UTSNAME]; 697 char osrelease[LINUX_MAX_UTSNAME]; 698 char *p; 699 700#ifdef DEBUG 701 if (ldebug(newuname)) 702 printf(ARGS(newuname, "*")); 703#endif 704 705 linux_get_osname(td, osname); 706 linux_get_osrelease(td, osrelease); 707 708 bzero(&utsname, sizeof(utsname)); 709 strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME); 710 getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME); 711 strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME); 712 strlcpy(utsname.version, version, LINUX_MAX_UTSNAME); 713 for (p = utsname.version; *p != '\0'; ++p) 714 if (*p == '\n') { 715 *p = '\0'; 716 break; 717 } 718#ifdef __i386__ 719 { 720 const char *class; 721 722 switch (cpu_class) { 723 case CPUCLASS_686: 724 class = "i686"; 725 break; 726 case CPUCLASS_586: 727 class = "i586"; 728 break; 729 case CPUCLASS_486: 730 class = "i486"; 731 break; 732 default: 733 class = "i386"; 734 } 735 strlcpy(utsname.machine, class, LINUX_MAX_UTSNAME); 736 } 737#elif defined(__amd64__) /* XXX: Linux can change 'personality'. */ 738#ifdef COMPAT_LINUX32 739 strlcpy(utsname.machine, "i686", LINUX_MAX_UTSNAME); 740#else 741 strlcpy(utsname.machine, "x86_64", LINUX_MAX_UTSNAME); 742#endif /* COMPAT_LINUX32 */ 743#else /* something other than i386 or amd64 - assume we and Linux agree */ 744 strlcpy(utsname.machine, machine, LINUX_MAX_UTSNAME); 745#endif /* __i386__ */ 746 strlcpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME); 747 748 return (copyout(&utsname, args->buf, sizeof(utsname))); 749} 750 751#if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 752struct l_utimbuf { 753 l_time_t l_actime; 754 l_time_t l_modtime; 755}; 756 757int 758linux_utime(struct thread *td, struct linux_utime_args *args) 759{ 760 struct timeval tv[2], *tvp; 761 struct l_utimbuf lut; 762 char *fname; 763 int error; 764 765 LCONVPATHEXIST(td, args->fname, &fname); 766 767#ifdef DEBUG 768 if (ldebug(utime)) 769 printf(ARGS(utime, "%s, *"), fname); 770#endif 771 772 if (args->times) { 773 if ((error = copyin(args->times, &lut, sizeof lut))) { 774 LFREEPATH(fname); 775 return error; 776 } 777 tv[0].tv_sec = lut.l_actime; 778 tv[0].tv_usec = 0; 779 tv[1].tv_sec = lut.l_modtime; 780 tv[1].tv_usec = 0; 781 tvp = tv; 782 } else 783 tvp = NULL; 784 785 error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE); 786 LFREEPATH(fname); 787 return (error); 788} 789 790int 791linux_utimes(struct thread *td, struct linux_utimes_args *args) 792{ 793 l_timeval ltv[2]; 794 struct timeval tv[2], *tvp = NULL; 795 char *fname; 796 int error; 797 798 LCONVPATHEXIST(td, args->fname, &fname); 799 800#ifdef DEBUG 801 if (ldebug(utimes)) 802 printf(ARGS(utimes, "%s, *"), fname); 803#endif 804 805 if (args->tptr != NULL) { 806 if ((error = copyin(args->tptr, ltv, sizeof ltv))) { 807 LFREEPATH(fname); 808 return (error); 809 } 810 tv[0].tv_sec = ltv[0].tv_sec; 811 tv[0].tv_usec = ltv[0].tv_usec; 812 tv[1].tv_sec = ltv[1].tv_sec; 813 tv[1].tv_usec = ltv[1].tv_usec; 814 tvp = tv; 815 } 816 817 error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE); 818 LFREEPATH(fname); 819 return (error); 820} 821#endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 822 823#define __WCLONE 0x80000000 824 825int 826linux_waitpid(struct thread *td, struct linux_waitpid_args *args) 827{ 828 int error, options, tmpstat; 829 830#ifdef DEBUG 831 if (ldebug(waitpid)) 832 printf(ARGS(waitpid, "%d, %p, %d"), 833 args->pid, (void *)args->status, args->options); 834#endif 835 /* 836 * this is necessary because the test in kern_wait doesnt work 837 * because we mess with the options here 838 */ 839 if (args->options & ~(WUNTRACED | WNOHANG | WCONTINUED | __WCLONE)) 840 return (EINVAL); 841 842 options = (args->options & (WNOHANG | WUNTRACED)); 843 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */ 844 if (args->options & __WCLONE) 845 options |= WLINUXCLONE; 846 847 error = kern_wait(td, args->pid, &tmpstat, options, NULL); 848 if (error) 849 return error; 850 851 if (args->status) { 852 tmpstat &= 0xffff; 853 if (WIFSIGNALED(tmpstat)) 854 tmpstat = (tmpstat & 0xffffff80) | 855 BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat)); 856 else if (WIFSTOPPED(tmpstat)) 857 tmpstat = (tmpstat & 0xffff00ff) | 858 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8); 859 return copyout(&tmpstat, args->status, sizeof(int)); 860 } 861 862 return 0; 863} 864 865int 866linux_wait4(struct thread *td, struct linux_wait4_args *args) 867{ 868 int error, options, tmpstat; 869 struct rusage ru, *rup; 870 struct proc *p; 871 872#ifdef DEBUG 873 if (ldebug(wait4)) 874 printf(ARGS(wait4, "%d, %p, %d, %p"), 875 args->pid, (void *)args->status, args->options, 876 (void *)args->rusage); 877#endif 878 879 options = (args->options & (WNOHANG | WUNTRACED)); 880 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */ 881 if (args->options & __WCLONE) 882 options |= WLINUXCLONE; 883 884 if (args->rusage != NULL) 885 rup = &ru; 886 else 887 rup = NULL; 888 error = kern_wait(td, args->pid, &tmpstat, options, rup); 889 if (error) 890 return error; 891 892 p = td->td_proc; 893 PROC_LOCK(p); 894 sigqueue_delete(&p->p_sigqueue, SIGCHLD); 895 PROC_UNLOCK(p); 896 897 if (args->status) { 898 tmpstat &= 0xffff; 899 if (WIFSIGNALED(tmpstat)) 900 tmpstat = (tmpstat & 0xffffff80) | 901 BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat)); 902 else if (WIFSTOPPED(tmpstat)) 903 tmpstat = (tmpstat & 0xffff00ff) | 904 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8); 905 error = copyout(&tmpstat, args->status, sizeof(int)); 906 } 907 if (args->rusage != NULL && error == 0) 908 error = copyout(&ru, args->rusage, sizeof(ru)); 909 910 return (error); 911} 912 913int 914linux_mknod(struct thread *td, struct linux_mknod_args *args) 915{ 916 char *path; 917 int error; 918 919 LCONVPATHCREAT(td, args->path, &path); 920 921#ifdef DEBUG 922 if (ldebug(mknod)) 923 printf(ARGS(mknod, "%s, %d, %d"), path, args->mode, args->dev); 924#endif 925 926 switch (args->mode & S_IFMT) { 927 case S_IFIFO: 928 case S_IFSOCK: 929 error = kern_mkfifo(td, path, UIO_SYSSPACE, args->mode); 930 break; 931 932 case S_IFCHR: 933 case S_IFBLK: 934 error = kern_mknod(td, path, UIO_SYSSPACE, args->mode, 935 args->dev); 936 break; 937 938 case S_IFDIR: 939 error = EPERM; 940 break; 941 942 case 0: 943 args->mode |= S_IFREG; 944 /* FALLTHROUGH */ 945 case S_IFREG: 946 error = kern_open(td, path, UIO_SYSSPACE, 947 O_WRONLY | O_CREAT | O_TRUNC, args->mode); 948 break; 949 950 default: 951 error = EINVAL; 952 break; 953 } 954 LFREEPATH(path); 955 return (error); 956} 957 958/* 959 * UGH! This is just about the dumbest idea I've ever heard!! 960 */ 961int 962linux_personality(struct thread *td, struct linux_personality_args *args) 963{ 964#ifdef DEBUG 965 if (ldebug(personality)) 966 printf(ARGS(personality, "%lu"), (unsigned long)args->per); 967#endif 968 if (args->per != 0) 969 return EINVAL; 970 971 /* Yes Jim, it's still a Linux... */ 972 td->td_retval[0] = 0; 973 return 0; 974} 975 976struct l_itimerval { 977 l_timeval it_interval; 978 l_timeval it_value; 979}; 980 981#define B2L_ITIMERVAL(bip, lip) \ 982 (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec; \ 983 (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec; \ 984 (bip)->it_value.tv_sec = (lip)->it_value.tv_sec; \ 985 (bip)->it_value.tv_usec = (lip)->it_value.tv_usec; 986 987int 988linux_setitimer(struct thread *td, struct linux_setitimer_args *uap) 989{ 990 int error; 991 struct l_itimerval ls; 992 struct itimerval aitv, oitv; 993 994#ifdef DEBUG 995 if (ldebug(setitimer)) 996 printf(ARGS(setitimer, "%p, %p"), 997 (void *)uap->itv, (void *)uap->oitv); 998#endif 999 1000 if (uap->itv == NULL) { 1001 uap->itv = uap->oitv; 1002 return (linux_getitimer(td, (struct linux_getitimer_args *)uap)); 1003 } 1004 1005 error = copyin(uap->itv, &ls, sizeof(ls)); 1006 if (error != 0) 1007 return (error); 1008 B2L_ITIMERVAL(&aitv, &ls); 1009#ifdef DEBUG 1010 if (ldebug(setitimer)) { 1011 printf("setitimer: value: sec: %jd, usec: %ld\n", 1012 (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec); 1013 printf("setitimer: interval: sec: %jd, usec: %ld\n", 1014 (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec); 1015 } 1016#endif 1017 error = kern_setitimer(td, uap->which, &aitv, &oitv); 1018 if (error != 0 || uap->oitv == NULL) 1019 return (error); 1020 B2L_ITIMERVAL(&ls, &oitv); 1021 1022 return (copyout(&ls, uap->oitv, sizeof(ls))); 1023} 1024 1025int 1026linux_getitimer(struct thread *td, struct linux_getitimer_args *uap) 1027{ 1028 int error; 1029 struct l_itimerval ls; 1030 struct itimerval aitv; 1031 1032#ifdef DEBUG 1033 if (ldebug(getitimer)) 1034 printf(ARGS(getitimer, "%p"), (void *)uap->itv); 1035#endif 1036 error = kern_getitimer(td, uap->which, &aitv); 1037 if (error != 0) 1038 return (error); 1039 B2L_ITIMERVAL(&ls, &aitv); 1040 return (copyout(&ls, uap->itv, sizeof(ls))); 1041} 1042 1043int 1044linux_nice(struct thread *td, struct linux_nice_args *args) 1045{ 1046 struct setpriority_args bsd_args; 1047 1048 bsd_args.which = PRIO_PROCESS; 1049 bsd_args.who = 0; /* current process */ 1050 bsd_args.prio = args->inc; 1051 return setpriority(td, &bsd_args); 1052} 1053 1054int 1055linux_setgroups(struct thread *td, struct linux_setgroups_args *args) 1056{ 1057 struct ucred *newcred, *oldcred; 1058 l_gid_t linux_gidset[NGROUPS]; 1059 gid_t *bsd_gidset; 1060 int ngrp, error; 1061 struct proc *p; 1062 1063 ngrp = args->gidsetsize; 1064 if (ngrp < 0 || ngrp >= NGROUPS) 1065 return (EINVAL); 1066 error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t)); 1067 if (error) 1068 return (error); 1069 newcred = crget(); 1070 p = td->td_proc; 1071 PROC_LOCK(p); 1072 oldcred = p->p_ucred; 1073 1074 /* 1075 * cr_groups[0] holds egid. Setting the whole set from 1076 * the supplied set will cause egid to be changed too. 1077 * Keep cr_groups[0] unchanged to prevent that. 1078 */ 1079 1080 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 1081 SUSER_ALLOWJAIL)) != 0) { 1082 PROC_UNLOCK(p); 1083 crfree(newcred); 1084 return (error); 1085 } 1086 1087 crcopy(newcred, oldcred); 1088 if (ngrp > 0) { 1089 newcred->cr_ngroups = ngrp + 1; 1090 1091 bsd_gidset = newcred->cr_groups; 1092 ngrp--; 1093 while (ngrp >= 0) { 1094 bsd_gidset[ngrp + 1] = linux_gidset[ngrp]; 1095 ngrp--; 1096 } 1097 } else 1098 newcred->cr_ngroups = 1; 1099 1100 setsugid(p); 1101 p->p_ucred = newcred; 1102 PROC_UNLOCK(p); 1103 crfree(oldcred); 1104 return (0); 1105} 1106 1107int 1108linux_getgroups(struct thread *td, struct linux_getgroups_args *args) 1109{ 1110 struct ucred *cred; 1111 l_gid_t linux_gidset[NGROUPS]; 1112 gid_t *bsd_gidset; 1113 int bsd_gidsetsz, ngrp, error; 1114 1115 cred = td->td_ucred; 1116 bsd_gidset = cred->cr_groups; 1117 bsd_gidsetsz = cred->cr_ngroups - 1; 1118 1119 /* 1120 * cr_groups[0] holds egid. Returning the whole set 1121 * here will cause a duplicate. Exclude cr_groups[0] 1122 * to prevent that. 1123 */ 1124 1125 if ((ngrp = args->gidsetsize) == 0) { 1126 td->td_retval[0] = bsd_gidsetsz; 1127 return (0); 1128 } 1129 1130 if (ngrp < bsd_gidsetsz) 1131 return (EINVAL); 1132 1133 ngrp = 0; 1134 while (ngrp < bsd_gidsetsz) { 1135 linux_gidset[ngrp] = bsd_gidset[ngrp + 1]; 1136 ngrp++; 1137 } 1138 1139 if ((error = copyout(linux_gidset, args->grouplist, 1140 ngrp * sizeof(l_gid_t)))) 1141 return (error); 1142 1143 td->td_retval[0] = ngrp; 1144 return (0); 1145} 1146 1147int 1148linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args) 1149{ 1150 struct rlimit bsd_rlim; 1151 struct l_rlimit rlim; 1152 u_int which; 1153 int error; 1154 1155#ifdef DEBUG 1156 if (ldebug(setrlimit)) 1157 printf(ARGS(setrlimit, "%d, %p"), 1158 args->resource, (void *)args->rlim); 1159#endif 1160 1161 if (args->resource >= LINUX_RLIM_NLIMITS) 1162 return (EINVAL); 1163 1164 which = linux_to_bsd_resource[args->resource]; 1165 if (which == -1) 1166 return (EINVAL); 1167 1168 error = copyin(args->rlim, &rlim, sizeof(rlim)); 1169 if (error) 1170 return (error); 1171 1172 bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur; 1173 bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max; 1174 return (kern_setrlimit(td, which, &bsd_rlim)); 1175} 1176 1177int 1178linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args) 1179{ 1180 struct l_rlimit rlim; 1181 struct proc *p = td->td_proc; 1182 struct rlimit bsd_rlim; 1183 u_int which; 1184 1185#ifdef DEBUG 1186 if (ldebug(old_getrlimit)) 1187 printf(ARGS(old_getrlimit, "%d, %p"), 1188 args->resource, (void *)args->rlim); 1189#endif 1190 1191 if (args->resource >= LINUX_RLIM_NLIMITS) 1192 return (EINVAL); 1193 1194 which = linux_to_bsd_resource[args->resource]; 1195 if (which == -1) 1196 return (EINVAL); 1197 1198 PROC_LOCK(p); 1199 lim_rlimit(p, which, &bsd_rlim); 1200 PROC_UNLOCK(p); 1201 1202#ifdef COMPAT_LINUX32 1203 rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur; 1204 if (rlim.rlim_cur == UINT_MAX) 1205 rlim.rlim_cur = INT_MAX; 1206 rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max; 1207 if (rlim.rlim_max == UINT_MAX) 1208 rlim.rlim_max = INT_MAX; 1209#else 1210 rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur; 1211 if (rlim.rlim_cur == ULONG_MAX) 1212 rlim.rlim_cur = LONG_MAX; 1213 rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max; 1214 if (rlim.rlim_max == ULONG_MAX) 1215 rlim.rlim_max = LONG_MAX; 1216#endif 1217 return (copyout(&rlim, args->rlim, sizeof(rlim))); 1218} 1219 1220int 1221linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args) 1222{ 1223 struct l_rlimit rlim; 1224 struct proc *p = td->td_proc; 1225 struct rlimit bsd_rlim; 1226 u_int which; 1227 1228#ifdef DEBUG 1229 if (ldebug(getrlimit)) 1230 printf(ARGS(getrlimit, "%d, %p"), 1231 args->resource, (void *)args->rlim); 1232#endif 1233 1234 if (args->resource >= LINUX_RLIM_NLIMITS) 1235 return (EINVAL); 1236 1237 which = linux_to_bsd_resource[args->resource]; 1238 if (which == -1) 1239 return (EINVAL); 1240 1241 PROC_LOCK(p); 1242 lim_rlimit(p, which, &bsd_rlim); 1243 PROC_UNLOCK(p); 1244 1245 rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur; 1246 rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max; 1247 return (copyout(&rlim, args->rlim, sizeof(rlim))); 1248} 1249 1250int 1251linux_sched_setscheduler(struct thread *td, 1252 struct linux_sched_setscheduler_args *args) 1253{ 1254 struct sched_setscheduler_args bsd; 1255 1256#ifdef DEBUG 1257 if (ldebug(sched_setscheduler)) 1258 printf(ARGS(sched_setscheduler, "%d, %d, %p"), 1259 args->pid, args->policy, (const void *)args->param); 1260#endif 1261 1262 switch (args->policy) { 1263 case LINUX_SCHED_OTHER: 1264 bsd.policy = SCHED_OTHER; 1265 break; 1266 case LINUX_SCHED_FIFO: 1267 bsd.policy = SCHED_FIFO; 1268 break; 1269 case LINUX_SCHED_RR: 1270 bsd.policy = SCHED_RR; 1271 break; 1272 default: 1273 return EINVAL; 1274 } 1275 1276 bsd.pid = args->pid; 1277 bsd.param = (struct sched_param *)args->param; 1278 return sched_setscheduler(td, &bsd); 1279} 1280 1281int 1282linux_sched_getscheduler(struct thread *td, 1283 struct linux_sched_getscheduler_args *args) 1284{ 1285 struct sched_getscheduler_args bsd; 1286 int error; 1287 1288#ifdef DEBUG 1289 if (ldebug(sched_getscheduler)) 1290 printf(ARGS(sched_getscheduler, "%d"), args->pid); 1291#endif 1292 1293 bsd.pid = args->pid; 1294 error = sched_getscheduler(td, &bsd); 1295 1296 switch (td->td_retval[0]) { 1297 case SCHED_OTHER: 1298 td->td_retval[0] = LINUX_SCHED_OTHER; 1299 break; 1300 case SCHED_FIFO: 1301 td->td_retval[0] = LINUX_SCHED_FIFO; 1302 break; 1303 case SCHED_RR: 1304 td->td_retval[0] = LINUX_SCHED_RR; 1305 break; 1306 } 1307 1308 return error; 1309} 1310 1311int 1312linux_sched_get_priority_max(struct thread *td, 1313 struct linux_sched_get_priority_max_args *args) 1314{ 1315 struct sched_get_priority_max_args bsd; 1316 1317#ifdef DEBUG 1318 if (ldebug(sched_get_priority_max)) 1319 printf(ARGS(sched_get_priority_max, "%d"), args->policy); 1320#endif 1321 1322 switch (args->policy) { 1323 case LINUX_SCHED_OTHER: 1324 bsd.policy = SCHED_OTHER; 1325 break; 1326 case LINUX_SCHED_FIFO: 1327 bsd.policy = SCHED_FIFO; 1328 break; 1329 case LINUX_SCHED_RR: 1330 bsd.policy = SCHED_RR; 1331 break; 1332 default: 1333 return EINVAL; 1334 } 1335 return sched_get_priority_max(td, &bsd); 1336} 1337 1338int 1339linux_sched_get_priority_min(struct thread *td, 1340 struct linux_sched_get_priority_min_args *args) 1341{ 1342 struct sched_get_priority_min_args bsd; 1343 1344#ifdef DEBUG 1345 if (ldebug(sched_get_priority_min)) 1346 printf(ARGS(sched_get_priority_min, "%d"), args->policy); 1347#endif 1348 1349 switch (args->policy) { 1350 case LINUX_SCHED_OTHER: 1351 bsd.policy = SCHED_OTHER; 1352 break; 1353 case LINUX_SCHED_FIFO: 1354 bsd.policy = SCHED_FIFO; 1355 break; 1356 case LINUX_SCHED_RR: 1357 bsd.policy = SCHED_RR; 1358 break; 1359 default: 1360 return EINVAL; 1361 } 1362 return sched_get_priority_min(td, &bsd); 1363} 1364 1365#define REBOOT_CAD_ON 0x89abcdef 1366#define REBOOT_CAD_OFF 0 1367#define REBOOT_HALT 0xcdef0123 1368#define REBOOT_RESTART 0x01234567 1369#define REBOOT_RESTART2 0xA1B2C3D4 1370#define REBOOT_POWEROFF 0x4321FEDC 1371#define REBOOT_MAGIC1 0xfee1dead 1372#define REBOOT_MAGIC2 0x28121969 1373#define REBOOT_MAGIC2A 0x05121996 1374#define REBOOT_MAGIC2B 0x16041998 1375 1376int 1377linux_reboot(struct thread *td, struct linux_reboot_args *args) 1378{ 1379 struct reboot_args bsd_args; 1380 1381#ifdef DEBUG 1382 if (ldebug(reboot)) 1383 printf(ARGS(reboot, "0x%x"), args->cmd); 1384#endif 1385 1386 if (args->magic1 != REBOOT_MAGIC1) 1387 return EINVAL; 1388 1389 switch (args->magic2) { 1390 case REBOOT_MAGIC2: 1391 case REBOOT_MAGIC2A: 1392 case REBOOT_MAGIC2B: 1393 break; 1394 default: 1395 return EINVAL; 1396 } 1397 1398 switch (args->cmd) { 1399 case REBOOT_CAD_ON: 1400 case REBOOT_CAD_OFF: 1401 return (priv_check(td, PRIV_REBOOT)); 1402 case REBOOT_HALT: 1403 bsd_args.opt = RB_HALT; 1404 break; 1405 case REBOOT_RESTART: 1406 case REBOOT_RESTART2: 1407 bsd_args.opt = 0; 1408 break; 1409 case REBOOT_POWEROFF: 1410 bsd_args.opt = RB_POWEROFF; 1411 break; 1412 default: 1413 return EINVAL; 1414 } 1415 return reboot(td, &bsd_args); 1416} 1417 1418 1419/* 1420 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify 1421 * td->td_retval[1] when COMPAT_43 is defined. This 1422 * globbers registers that are assumed to be preserved. The following 1423 * lightweight syscalls fixes this. See also linux_getgid16() and 1424 * linux_getuid16() in linux_uid16.c. 1425 * 1426 * linux_getpid() - MP SAFE 1427 * linux_getgid() - MP SAFE 1428 * linux_getuid() - MP SAFE 1429 */ 1430 1431int 1432linux_getpid(struct thread *td, struct linux_getpid_args *args) 1433{ 1434 struct linux_emuldata *em; 1435 1436#ifdef DEBUG 1437 if (ldebug(getpid)) 1438 printf(ARGS(getpid, "")); 1439#endif 1440 1441 if (linux_use26(td)) {
| 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 67#include <security/mac/mac_framework.h> 68 69#include <vm/vm.h> 70#include <vm/pmap.h> 71#include <vm/vm_kern.h> 72#include <vm/vm_map.h> 73#include <vm/vm_extern.h> 74#include <vm/vm_object.h> 75#include <vm/swap_pager.h> 76 77#include <compat/linux/linux_sysproto.h> 78#include <compat/linux/linux_emul.h> 79#include <compat/linux/linux_misc.h> 80 81#ifdef COMPAT_LINUX32 82#include <machine/../linux32/linux.h> 83#include <machine/../linux32/linux32_proto.h> 84#else 85#include <machine/../linux/linux.h> 86#include <machine/../linux/linux_proto.h> 87#endif 88 89#include <compat/linux/linux_mib.h> 90#include <compat/linux/linux_signal.h> 91#include <compat/linux/linux_util.h> 92 93#ifdef __i386__ 94#include <machine/cputypes.h> 95#endif 96 97#define BSD_TO_LINUX_SIGNAL(sig) \ 98 (((sig) <= LINUX_SIGTBLSZ) ? bsd_to_linux_signal[_SIG_IDX(sig)] : sig) 99 100static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = { 101 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK, 102 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE, 103 RLIMIT_MEMLOCK, -1 104}; 105 106struct l_sysinfo { 107 l_long uptime; /* Seconds since boot */ 108 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */ 109#define LINUX_SYSINFO_LOADS_SCALE 65536 110 l_ulong totalram; /* Total usable main memory size */ 111 l_ulong freeram; /* Available memory size */ 112 l_ulong sharedram; /* Amount of shared memory */ 113 l_ulong bufferram; /* Memory used by buffers */ 114 l_ulong totalswap; /* Total swap space size */ 115 l_ulong freeswap; /* swap space still available */ 116 l_ushort procs; /* Number of current processes */ 117 l_ushort pads; 118 l_ulong totalbig; 119 l_ulong freebig; 120 l_uint mem_unit; 121 char _f[20-2*sizeof(l_long)-sizeof(l_int)]; /* padding */ 122}; 123int 124linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args) 125{ 126 struct l_sysinfo sysinfo; 127 vm_object_t object; 128 int i, j; 129 struct timespec ts; 130 131 getnanouptime(&ts); 132 if (ts.tv_nsec != 0) 133 ts.tv_sec++; 134 sysinfo.uptime = ts.tv_sec; 135 136 /* Use the information from the mib to get our load averages */ 137 for (i = 0; i < 3; i++) 138 sysinfo.loads[i] = averunnable.ldavg[i] * 139 LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale; 140 141 sysinfo.totalram = physmem * PAGE_SIZE; 142 sysinfo.freeram = sysinfo.totalram - cnt.v_wire_count * PAGE_SIZE; 143 144 sysinfo.sharedram = 0; 145 mtx_lock(&vm_object_list_mtx); 146 TAILQ_FOREACH(object, &vm_object_list, object_list) 147 if (object->shadow_count > 1) 148 sysinfo.sharedram += object->resident_page_count; 149 mtx_unlock(&vm_object_list_mtx); 150 151 sysinfo.sharedram *= PAGE_SIZE; 152 sysinfo.bufferram = 0; 153 154 swap_pager_status(&i, &j); 155 sysinfo.totalswap = i * PAGE_SIZE; 156 sysinfo.freeswap = (i - j) * PAGE_SIZE; 157 158 sysinfo.procs = nprocs; 159 160 /* The following are only present in newer Linux kernels. */ 161 sysinfo.totalbig = 0; 162 sysinfo.freebig = 0; 163 sysinfo.mem_unit = 1; 164 165 return copyout(&sysinfo, args->info, sizeof(sysinfo)); 166} 167 168int 169linux_alarm(struct thread *td, struct linux_alarm_args *args) 170{ 171 struct itimerval it, old_it; 172 int error; 173 174#ifdef DEBUG 175 if (ldebug(alarm)) 176 printf(ARGS(alarm, "%u"), args->secs); 177#endif 178 179 if (args->secs > 100000000) 180 return (EINVAL); 181 182 it.it_value.tv_sec = (long)args->secs; 183 it.it_value.tv_usec = 0; 184 it.it_interval.tv_sec = 0; 185 it.it_interval.tv_usec = 0; 186 error = kern_setitimer(td, ITIMER_REAL, &it, &old_it); 187 if (error) 188 return (error); 189 if (timevalisset(&old_it.it_value)) { 190 if (old_it.it_value.tv_usec != 0) 191 old_it.it_value.tv_sec++; 192 td->td_retval[0] = old_it.it_value.tv_sec; 193 } 194 return (0); 195} 196 197int 198linux_brk(struct thread *td, struct linux_brk_args *args) 199{ 200 struct vmspace *vm = td->td_proc->p_vmspace; 201 vm_offset_t new, old; 202 struct obreak_args /* { 203 char * nsize; 204 } */ tmp; 205 206#ifdef DEBUG 207 if (ldebug(brk)) 208 printf(ARGS(brk, "%p"), (void *)(uintptr_t)args->dsend); 209#endif 210 old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize); 211 new = (vm_offset_t)args->dsend; 212 tmp.nsize = (char *)new; 213 if (((caddr_t)new > vm->vm_daddr) && !obreak(td, &tmp)) 214 td->td_retval[0] = (long)new; 215 else 216 td->td_retval[0] = (long)old; 217 218 return 0; 219} 220 221#if defined(__i386__) 222/* XXX: what about amd64/linux32? */ 223 224int 225linux_uselib(struct thread *td, struct linux_uselib_args *args) 226{ 227 struct nameidata ni; 228 struct vnode *vp; 229 struct exec *a_out; 230 struct vattr attr; 231 vm_offset_t vmaddr; 232 unsigned long file_offset; 233 vm_offset_t buffer; 234 unsigned long bss_size; 235 char *library; 236 int error; 237 int locked, vfslocked; 238 239 LCONVPATHEXIST(td, args->library, &library); 240 241#ifdef DEBUG 242 if (ldebug(uselib)) 243 printf(ARGS(uselib, "%s"), library); 244#endif 245 246 a_out = NULL; 247 vfslocked = 0; 248 locked = 0; 249 vp = NULL; 250 251 NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1, 252 UIO_SYSSPACE, library, td); 253 error = namei(&ni); 254 LFREEPATH(library); 255 if (error) 256 goto cleanup; 257 258 vp = ni.ni_vp; 259 vfslocked = NDHASGIANT(&ni); 260 NDFREE(&ni, NDF_ONLY_PNBUF); 261 262 /* 263 * From here on down, we have a locked vnode that must be unlocked. 264 * XXX: The code below largely duplicates exec_check_permissions(). 265 */ 266 locked = 1; 267 268 /* Writable? */ 269 if (vp->v_writecount) { 270 error = ETXTBSY; 271 goto cleanup; 272 } 273 274 /* Executable? */ 275 error = VOP_GETATTR(vp, &attr, td->td_ucred, td); 276 if (error) 277 goto cleanup; 278 279 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || 280 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) { 281 /* EACCESS is what exec(2) returns. */ 282 error = ENOEXEC; 283 goto cleanup; 284 } 285 286 /* Sensible size? */ 287 if (attr.va_size == 0) { 288 error = ENOEXEC; 289 goto cleanup; 290 } 291 292 /* Can we access it? */ 293 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td); 294 if (error) 295 goto cleanup; 296 297 /* 298 * XXX: This should use vn_open() so that it is properly authorized, 299 * and to reduce code redundancy all over the place here. 300 * XXX: Not really, it duplicates far more of exec_check_permissions() 301 * than vn_open(). 302 */ 303#ifdef MAC 304 error = mac_check_vnode_open(td->td_ucred, vp, FREAD); 305 if (error) 306 goto cleanup; 307#endif 308 error = VOP_OPEN(vp, FREAD, td->td_ucred, td, -1); 309 if (error) 310 goto cleanup; 311 312 /* Pull in executable header into kernel_map */ 313 error = vm_mmap(kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE, 314 VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 0); 315 if (error) 316 goto cleanup; 317 318 /* Is it a Linux binary ? */ 319 if (((a_out->a_magic >> 16) & 0xff) != 0x64) { 320 error = ENOEXEC; 321 goto cleanup; 322 } 323 324 /* 325 * While we are here, we should REALLY do some more checks 326 */ 327 328 /* Set file/virtual offset based on a.out variant. */ 329 switch ((int)(a_out->a_magic & 0xffff)) { 330 case 0413: /* ZMAGIC */ 331 file_offset = 1024; 332 break; 333 case 0314: /* QMAGIC */ 334 file_offset = 0; 335 break; 336 default: 337 error = ENOEXEC; 338 goto cleanup; 339 } 340 341 bss_size = round_page(a_out->a_bss); 342 343 /* Check various fields in header for validity/bounds. */ 344 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) { 345 error = ENOEXEC; 346 goto cleanup; 347 } 348 349 /* text + data can't exceed file size */ 350 if (a_out->a_data + a_out->a_text > attr.va_size) { 351 error = EFAULT; 352 goto cleanup; 353 } 354 355 /* 356 * text/data/bss must not exceed limits 357 * XXX - this is not complete. it should check current usage PLUS 358 * the resources needed by this library. 359 */ 360 PROC_LOCK(td->td_proc); 361 if (a_out->a_text > maxtsiz || 362 a_out->a_data + bss_size > lim_cur(td->td_proc, RLIMIT_DATA)) { 363 PROC_UNLOCK(td->td_proc); 364 error = ENOMEM; 365 goto cleanup; 366 } 367 PROC_UNLOCK(td->td_proc); 368 369 /* 370 * Prevent more writers. 371 * XXX: Note that if any of the VM operations fail below we don't 372 * clear this flag. 373 */ 374 vp->v_vflag |= VV_TEXT; 375 376 /* 377 * Lock no longer needed 378 */ 379 locked = 0; 380 VOP_UNLOCK(vp, 0, td); 381 VFS_UNLOCK_GIANT(vfslocked); 382 383 /* 384 * Check if file_offset page aligned. Currently we cannot handle 385 * misalinged file offsets, and so we read in the entire image 386 * (what a waste). 387 */ 388 if (file_offset & PAGE_MASK) { 389#ifdef DEBUG 390 printf("uselib: Non page aligned binary %lu\n", file_offset); 391#endif 392 /* Map text+data read/write/execute */ 393 394 /* a_entry is the load address and is page aligned */ 395 vmaddr = trunc_page(a_out->a_entry); 396 397 /* get anon user mapping, read+write+execute */ 398 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0, 399 &vmaddr, a_out->a_text + a_out->a_data, FALSE, VM_PROT_ALL, 400 VM_PROT_ALL, 0); 401 if (error) 402 goto cleanup; 403 404 /* map file into kernel_map */ 405 error = vm_mmap(kernel_map, &buffer, 406 round_page(a_out->a_text + a_out->a_data + file_offset), 407 VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 408 trunc_page(file_offset)); 409 if (error) 410 goto cleanup; 411 412 /* copy from kernel VM space to user space */ 413 error = copyout(PTRIN(buffer + file_offset), 414 (void *)vmaddr, a_out->a_text + a_out->a_data); 415 416 /* release temporary kernel space */ 417 vm_map_remove(kernel_map, buffer, buffer + 418 round_page(a_out->a_text + a_out->a_data + file_offset)); 419 420 if (error) 421 goto cleanup; 422 } else { 423#ifdef DEBUG 424 printf("uselib: Page aligned binary %lu\n", file_offset); 425#endif 426 /* 427 * for QMAGIC, a_entry is 20 bytes beyond the load address 428 * to skip the executable header 429 */ 430 vmaddr = trunc_page(a_out->a_entry); 431 432 /* 433 * Map it all into the process's space as a single 434 * copy-on-write "data" segment. 435 */ 436 error = vm_mmap(&td->td_proc->p_vmspace->vm_map, &vmaddr, 437 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL, 438 MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset); 439 if (error) 440 goto cleanup; 441 } 442#ifdef DEBUG 443 printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long *)vmaddr)[0], 444 ((long *)vmaddr)[1]); 445#endif 446 if (bss_size != 0) { 447 /* Calculate BSS start address */ 448 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text + 449 a_out->a_data; 450 451 /* allocate some 'anon' space */ 452 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0, 453 &vmaddr, bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0); 454 if (error) 455 goto cleanup; 456 } 457 458cleanup: 459 /* Unlock vnode if needed */ 460 if (locked) { 461 VOP_UNLOCK(vp, 0, td); 462 VFS_UNLOCK_GIANT(vfslocked); 463 } 464 465 /* Release the kernel mapping. */ 466 if (a_out) 467 vm_map_remove(kernel_map, (vm_offset_t)a_out, 468 (vm_offset_t)a_out + PAGE_SIZE); 469 470 return error; 471} 472 473#endif /* __i386__ */ 474 475int 476linux_select(struct thread *td, struct linux_select_args *args) 477{ 478 l_timeval ltv; 479 struct timeval tv0, tv1, utv, *tvp; 480 int error; 481 482#ifdef DEBUG 483 if (ldebug(select)) 484 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds, 485 (void *)args->readfds, (void *)args->writefds, 486 (void *)args->exceptfds, (void *)args->timeout); 487#endif 488 489 /* 490 * Store current time for computation of the amount of 491 * time left. 492 */ 493 if (args->timeout) { 494 if ((error = copyin(args->timeout, <v, sizeof(ltv)))) 495 goto select_out; 496 utv.tv_sec = ltv.tv_sec; 497 utv.tv_usec = ltv.tv_usec; 498#ifdef DEBUG 499 if (ldebug(select)) 500 printf(LMSG("incoming timeout (%jd/%ld)"), 501 (intmax_t)utv.tv_sec, utv.tv_usec); 502#endif 503 504 if (itimerfix(&utv)) { 505 /* 506 * The timeval was invalid. Convert it to something 507 * valid that will act as it does under Linux. 508 */ 509 utv.tv_sec += utv.tv_usec / 1000000; 510 utv.tv_usec %= 1000000; 511 if (utv.tv_usec < 0) { 512 utv.tv_sec -= 1; 513 utv.tv_usec += 1000000; 514 } 515 if (utv.tv_sec < 0) 516 timevalclear(&utv); 517 } 518 microtime(&tv0); 519 tvp = &utv; 520 } else 521 tvp = NULL; 522 523 error = kern_select(td, args->nfds, args->readfds, args->writefds, 524 args->exceptfds, tvp); 525 526#ifdef DEBUG 527 if (ldebug(select)) 528 printf(LMSG("real select returns %d"), error); 529#endif 530 if (error) { 531 /* 532 * See fs/select.c in the Linux kernel. Without this, 533 * Maelstrom doesn't work. 534 */ 535 if (error == ERESTART) 536 error = EINTR; 537 goto select_out; 538 } 539 540 if (args->timeout) { 541 if (td->td_retval[0]) { 542 /* 543 * Compute how much time was left of the timeout, 544 * by subtracting the current time and the time 545 * before we started the call, and subtracting 546 * that result from the user-supplied value. 547 */ 548 microtime(&tv1); 549 timevalsub(&tv1, &tv0); 550 timevalsub(&utv, &tv1); 551 if (utv.tv_sec < 0) 552 timevalclear(&utv); 553 } else 554 timevalclear(&utv); 555#ifdef DEBUG 556 if (ldebug(select)) 557 printf(LMSG("outgoing timeout (%jd/%ld)"), 558 (intmax_t)utv.tv_sec, utv.tv_usec); 559#endif 560 ltv.tv_sec = utv.tv_sec; 561 ltv.tv_usec = utv.tv_usec; 562 if ((error = copyout(<v, args->timeout, sizeof(ltv)))) 563 goto select_out; 564 } 565 566select_out: 567#ifdef DEBUG 568 if (ldebug(select)) 569 printf(LMSG("select_out -> %d"), error); 570#endif 571 return error; 572} 573 574int 575linux_mremap(struct thread *td, struct linux_mremap_args *args) 576{ 577 struct munmap_args /* { 578 void *addr; 579 size_t len; 580 } */ bsd_args; 581 int error = 0; 582 583#ifdef DEBUG 584 if (ldebug(mremap)) 585 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"), 586 (void *)(uintptr_t)args->addr, 587 (unsigned long)args->old_len, 588 (unsigned long)args->new_len, 589 (unsigned long)args->flags); 590#endif 591 args->new_len = round_page(args->new_len); 592 args->old_len = round_page(args->old_len); 593 594 if (args->new_len > args->old_len) { 595 td->td_retval[0] = 0; 596 return ENOMEM; 597 } 598 599 if (args->new_len < args->old_len) { 600 bsd_args.addr = 601 (caddr_t)((uintptr_t)args->addr + args->new_len); 602 bsd_args.len = args->old_len - args->new_len; 603 error = munmap(td, &bsd_args); 604 } 605 606 td->td_retval[0] = error ? 0 : (uintptr_t)args->addr; 607 return error; 608} 609 610#define LINUX_MS_ASYNC 0x0001 611#define LINUX_MS_INVALIDATE 0x0002 612#define LINUX_MS_SYNC 0x0004 613 614int 615linux_msync(struct thread *td, struct linux_msync_args *args) 616{ 617 struct msync_args bsd_args; 618 619 bsd_args.addr = (caddr_t)(uintptr_t)args->addr; 620 bsd_args.len = (uintptr_t)args->len; 621 bsd_args.flags = args->fl & ~LINUX_MS_SYNC; 622 623 return msync(td, &bsd_args); 624} 625 626int 627linux_time(struct thread *td, struct linux_time_args *args) 628{ 629 struct timeval tv; 630 l_time_t tm; 631 int error; 632 633#ifdef DEBUG 634 if (ldebug(time)) 635 printf(ARGS(time, "*")); 636#endif 637 638 microtime(&tv); 639 tm = tv.tv_sec; 640 if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm)))) 641 return error; 642 td->td_retval[0] = tm; 643 return 0; 644} 645 646struct l_times_argv { 647 l_long tms_utime; 648 l_long tms_stime; 649 l_long tms_cutime; 650 l_long tms_cstime; 651}; 652 653#define CLK_TCK 100 /* Linux uses 100 */ 654 655#define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK)) 656 657int 658linux_times(struct thread *td, struct linux_times_args *args) 659{ 660 struct timeval tv, utime, stime, cutime, cstime; 661 struct l_times_argv tms; 662 struct proc *p; 663 int error; 664 665#ifdef DEBUG 666 if (ldebug(times)) 667 printf(ARGS(times, "*")); 668#endif 669 670 if (args->buf != NULL) { 671 p = td->td_proc; 672 PROC_LOCK(p); 673 calcru(p, &utime, &stime); 674 calccru(p, &cutime, &cstime); 675 PROC_UNLOCK(p); 676 677 tms.tms_utime = CONVTCK(utime); 678 tms.tms_stime = CONVTCK(stime); 679 680 tms.tms_cutime = CONVTCK(cutime); 681 tms.tms_cstime = CONVTCK(cstime); 682 683 if ((error = copyout(&tms, args->buf, sizeof(tms)))) 684 return error; 685 } 686 687 microuptime(&tv); 688 td->td_retval[0] = (int)CONVTCK(tv); 689 return 0; 690} 691 692int 693linux_newuname(struct thread *td, struct linux_newuname_args *args) 694{ 695 struct l_new_utsname utsname; 696 char osname[LINUX_MAX_UTSNAME]; 697 char osrelease[LINUX_MAX_UTSNAME]; 698 char *p; 699 700#ifdef DEBUG 701 if (ldebug(newuname)) 702 printf(ARGS(newuname, "*")); 703#endif 704 705 linux_get_osname(td, osname); 706 linux_get_osrelease(td, osrelease); 707 708 bzero(&utsname, sizeof(utsname)); 709 strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME); 710 getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME); 711 strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME); 712 strlcpy(utsname.version, version, LINUX_MAX_UTSNAME); 713 for (p = utsname.version; *p != '\0'; ++p) 714 if (*p == '\n') { 715 *p = '\0'; 716 break; 717 } 718#ifdef __i386__ 719 { 720 const char *class; 721 722 switch (cpu_class) { 723 case CPUCLASS_686: 724 class = "i686"; 725 break; 726 case CPUCLASS_586: 727 class = "i586"; 728 break; 729 case CPUCLASS_486: 730 class = "i486"; 731 break; 732 default: 733 class = "i386"; 734 } 735 strlcpy(utsname.machine, class, LINUX_MAX_UTSNAME); 736 } 737#elif defined(__amd64__) /* XXX: Linux can change 'personality'. */ 738#ifdef COMPAT_LINUX32 739 strlcpy(utsname.machine, "i686", LINUX_MAX_UTSNAME); 740#else 741 strlcpy(utsname.machine, "x86_64", LINUX_MAX_UTSNAME); 742#endif /* COMPAT_LINUX32 */ 743#else /* something other than i386 or amd64 - assume we and Linux agree */ 744 strlcpy(utsname.machine, machine, LINUX_MAX_UTSNAME); 745#endif /* __i386__ */ 746 strlcpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME); 747 748 return (copyout(&utsname, args->buf, sizeof(utsname))); 749} 750 751#if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 752struct l_utimbuf { 753 l_time_t l_actime; 754 l_time_t l_modtime; 755}; 756 757int 758linux_utime(struct thread *td, struct linux_utime_args *args) 759{ 760 struct timeval tv[2], *tvp; 761 struct l_utimbuf lut; 762 char *fname; 763 int error; 764 765 LCONVPATHEXIST(td, args->fname, &fname); 766 767#ifdef DEBUG 768 if (ldebug(utime)) 769 printf(ARGS(utime, "%s, *"), fname); 770#endif 771 772 if (args->times) { 773 if ((error = copyin(args->times, &lut, sizeof lut))) { 774 LFREEPATH(fname); 775 return error; 776 } 777 tv[0].tv_sec = lut.l_actime; 778 tv[0].tv_usec = 0; 779 tv[1].tv_sec = lut.l_modtime; 780 tv[1].tv_usec = 0; 781 tvp = tv; 782 } else 783 tvp = NULL; 784 785 error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE); 786 LFREEPATH(fname); 787 return (error); 788} 789 790int 791linux_utimes(struct thread *td, struct linux_utimes_args *args) 792{ 793 l_timeval ltv[2]; 794 struct timeval tv[2], *tvp = NULL; 795 char *fname; 796 int error; 797 798 LCONVPATHEXIST(td, args->fname, &fname); 799 800#ifdef DEBUG 801 if (ldebug(utimes)) 802 printf(ARGS(utimes, "%s, *"), fname); 803#endif 804 805 if (args->tptr != NULL) { 806 if ((error = copyin(args->tptr, ltv, sizeof ltv))) { 807 LFREEPATH(fname); 808 return (error); 809 } 810 tv[0].tv_sec = ltv[0].tv_sec; 811 tv[0].tv_usec = ltv[0].tv_usec; 812 tv[1].tv_sec = ltv[1].tv_sec; 813 tv[1].tv_usec = ltv[1].tv_usec; 814 tvp = tv; 815 } 816 817 error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE); 818 LFREEPATH(fname); 819 return (error); 820} 821#endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 822 823#define __WCLONE 0x80000000 824 825int 826linux_waitpid(struct thread *td, struct linux_waitpid_args *args) 827{ 828 int error, options, tmpstat; 829 830#ifdef DEBUG 831 if (ldebug(waitpid)) 832 printf(ARGS(waitpid, "%d, %p, %d"), 833 args->pid, (void *)args->status, args->options); 834#endif 835 /* 836 * this is necessary because the test in kern_wait doesnt work 837 * because we mess with the options here 838 */ 839 if (args->options & ~(WUNTRACED | WNOHANG | WCONTINUED | __WCLONE)) 840 return (EINVAL); 841 842 options = (args->options & (WNOHANG | WUNTRACED)); 843 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */ 844 if (args->options & __WCLONE) 845 options |= WLINUXCLONE; 846 847 error = kern_wait(td, args->pid, &tmpstat, options, NULL); 848 if (error) 849 return error; 850 851 if (args->status) { 852 tmpstat &= 0xffff; 853 if (WIFSIGNALED(tmpstat)) 854 tmpstat = (tmpstat & 0xffffff80) | 855 BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat)); 856 else if (WIFSTOPPED(tmpstat)) 857 tmpstat = (tmpstat & 0xffff00ff) | 858 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8); 859 return copyout(&tmpstat, args->status, sizeof(int)); 860 } 861 862 return 0; 863} 864 865int 866linux_wait4(struct thread *td, struct linux_wait4_args *args) 867{ 868 int error, options, tmpstat; 869 struct rusage ru, *rup; 870 struct proc *p; 871 872#ifdef DEBUG 873 if (ldebug(wait4)) 874 printf(ARGS(wait4, "%d, %p, %d, %p"), 875 args->pid, (void *)args->status, args->options, 876 (void *)args->rusage); 877#endif 878 879 options = (args->options & (WNOHANG | WUNTRACED)); 880 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */ 881 if (args->options & __WCLONE) 882 options |= WLINUXCLONE; 883 884 if (args->rusage != NULL) 885 rup = &ru; 886 else 887 rup = NULL; 888 error = kern_wait(td, args->pid, &tmpstat, options, rup); 889 if (error) 890 return error; 891 892 p = td->td_proc; 893 PROC_LOCK(p); 894 sigqueue_delete(&p->p_sigqueue, SIGCHLD); 895 PROC_UNLOCK(p); 896 897 if (args->status) { 898 tmpstat &= 0xffff; 899 if (WIFSIGNALED(tmpstat)) 900 tmpstat = (tmpstat & 0xffffff80) | 901 BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat)); 902 else if (WIFSTOPPED(tmpstat)) 903 tmpstat = (tmpstat & 0xffff00ff) | 904 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8); 905 error = copyout(&tmpstat, args->status, sizeof(int)); 906 } 907 if (args->rusage != NULL && error == 0) 908 error = copyout(&ru, args->rusage, sizeof(ru)); 909 910 return (error); 911} 912 913int 914linux_mknod(struct thread *td, struct linux_mknod_args *args) 915{ 916 char *path; 917 int error; 918 919 LCONVPATHCREAT(td, args->path, &path); 920 921#ifdef DEBUG 922 if (ldebug(mknod)) 923 printf(ARGS(mknod, "%s, %d, %d"), path, args->mode, args->dev); 924#endif 925 926 switch (args->mode & S_IFMT) { 927 case S_IFIFO: 928 case S_IFSOCK: 929 error = kern_mkfifo(td, path, UIO_SYSSPACE, args->mode); 930 break; 931 932 case S_IFCHR: 933 case S_IFBLK: 934 error = kern_mknod(td, path, UIO_SYSSPACE, args->mode, 935 args->dev); 936 break; 937 938 case S_IFDIR: 939 error = EPERM; 940 break; 941 942 case 0: 943 args->mode |= S_IFREG; 944 /* FALLTHROUGH */ 945 case S_IFREG: 946 error = kern_open(td, path, UIO_SYSSPACE, 947 O_WRONLY | O_CREAT | O_TRUNC, args->mode); 948 break; 949 950 default: 951 error = EINVAL; 952 break; 953 } 954 LFREEPATH(path); 955 return (error); 956} 957 958/* 959 * UGH! This is just about the dumbest idea I've ever heard!! 960 */ 961int 962linux_personality(struct thread *td, struct linux_personality_args *args) 963{ 964#ifdef DEBUG 965 if (ldebug(personality)) 966 printf(ARGS(personality, "%lu"), (unsigned long)args->per); 967#endif 968 if (args->per != 0) 969 return EINVAL; 970 971 /* Yes Jim, it's still a Linux... */ 972 td->td_retval[0] = 0; 973 return 0; 974} 975 976struct l_itimerval { 977 l_timeval it_interval; 978 l_timeval it_value; 979}; 980 981#define B2L_ITIMERVAL(bip, lip) \ 982 (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec; \ 983 (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec; \ 984 (bip)->it_value.tv_sec = (lip)->it_value.tv_sec; \ 985 (bip)->it_value.tv_usec = (lip)->it_value.tv_usec; 986 987int 988linux_setitimer(struct thread *td, struct linux_setitimer_args *uap) 989{ 990 int error; 991 struct l_itimerval ls; 992 struct itimerval aitv, oitv; 993 994#ifdef DEBUG 995 if (ldebug(setitimer)) 996 printf(ARGS(setitimer, "%p, %p"), 997 (void *)uap->itv, (void *)uap->oitv); 998#endif 999 1000 if (uap->itv == NULL) { 1001 uap->itv = uap->oitv; 1002 return (linux_getitimer(td, (struct linux_getitimer_args *)uap)); 1003 } 1004 1005 error = copyin(uap->itv, &ls, sizeof(ls)); 1006 if (error != 0) 1007 return (error); 1008 B2L_ITIMERVAL(&aitv, &ls); 1009#ifdef DEBUG 1010 if (ldebug(setitimer)) { 1011 printf("setitimer: value: sec: %jd, usec: %ld\n", 1012 (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec); 1013 printf("setitimer: interval: sec: %jd, usec: %ld\n", 1014 (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec); 1015 } 1016#endif 1017 error = kern_setitimer(td, uap->which, &aitv, &oitv); 1018 if (error != 0 || uap->oitv == NULL) 1019 return (error); 1020 B2L_ITIMERVAL(&ls, &oitv); 1021 1022 return (copyout(&ls, uap->oitv, sizeof(ls))); 1023} 1024 1025int 1026linux_getitimer(struct thread *td, struct linux_getitimer_args *uap) 1027{ 1028 int error; 1029 struct l_itimerval ls; 1030 struct itimerval aitv; 1031 1032#ifdef DEBUG 1033 if (ldebug(getitimer)) 1034 printf(ARGS(getitimer, "%p"), (void *)uap->itv); 1035#endif 1036 error = kern_getitimer(td, uap->which, &aitv); 1037 if (error != 0) 1038 return (error); 1039 B2L_ITIMERVAL(&ls, &aitv); 1040 return (copyout(&ls, uap->itv, sizeof(ls))); 1041} 1042 1043int 1044linux_nice(struct thread *td, struct linux_nice_args *args) 1045{ 1046 struct setpriority_args bsd_args; 1047 1048 bsd_args.which = PRIO_PROCESS; 1049 bsd_args.who = 0; /* current process */ 1050 bsd_args.prio = args->inc; 1051 return setpriority(td, &bsd_args); 1052} 1053 1054int 1055linux_setgroups(struct thread *td, struct linux_setgroups_args *args) 1056{ 1057 struct ucred *newcred, *oldcred; 1058 l_gid_t linux_gidset[NGROUPS]; 1059 gid_t *bsd_gidset; 1060 int ngrp, error; 1061 struct proc *p; 1062 1063 ngrp = args->gidsetsize; 1064 if (ngrp < 0 || ngrp >= NGROUPS) 1065 return (EINVAL); 1066 error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t)); 1067 if (error) 1068 return (error); 1069 newcred = crget(); 1070 p = td->td_proc; 1071 PROC_LOCK(p); 1072 oldcred = p->p_ucred; 1073 1074 /* 1075 * cr_groups[0] holds egid. Setting the whole set from 1076 * the supplied set will cause egid to be changed too. 1077 * Keep cr_groups[0] unchanged to prevent that. 1078 */ 1079 1080 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 1081 SUSER_ALLOWJAIL)) != 0) { 1082 PROC_UNLOCK(p); 1083 crfree(newcred); 1084 return (error); 1085 } 1086 1087 crcopy(newcred, oldcred); 1088 if (ngrp > 0) { 1089 newcred->cr_ngroups = ngrp + 1; 1090 1091 bsd_gidset = newcred->cr_groups; 1092 ngrp--; 1093 while (ngrp >= 0) { 1094 bsd_gidset[ngrp + 1] = linux_gidset[ngrp]; 1095 ngrp--; 1096 } 1097 } else 1098 newcred->cr_ngroups = 1; 1099 1100 setsugid(p); 1101 p->p_ucred = newcred; 1102 PROC_UNLOCK(p); 1103 crfree(oldcred); 1104 return (0); 1105} 1106 1107int 1108linux_getgroups(struct thread *td, struct linux_getgroups_args *args) 1109{ 1110 struct ucred *cred; 1111 l_gid_t linux_gidset[NGROUPS]; 1112 gid_t *bsd_gidset; 1113 int bsd_gidsetsz, ngrp, error; 1114 1115 cred = td->td_ucred; 1116 bsd_gidset = cred->cr_groups; 1117 bsd_gidsetsz = cred->cr_ngroups - 1; 1118 1119 /* 1120 * cr_groups[0] holds egid. Returning the whole set 1121 * here will cause a duplicate. Exclude cr_groups[0] 1122 * to prevent that. 1123 */ 1124 1125 if ((ngrp = args->gidsetsize) == 0) { 1126 td->td_retval[0] = bsd_gidsetsz; 1127 return (0); 1128 } 1129 1130 if (ngrp < bsd_gidsetsz) 1131 return (EINVAL); 1132 1133 ngrp = 0; 1134 while (ngrp < bsd_gidsetsz) { 1135 linux_gidset[ngrp] = bsd_gidset[ngrp + 1]; 1136 ngrp++; 1137 } 1138 1139 if ((error = copyout(linux_gidset, args->grouplist, 1140 ngrp * sizeof(l_gid_t)))) 1141 return (error); 1142 1143 td->td_retval[0] = ngrp; 1144 return (0); 1145} 1146 1147int 1148linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args) 1149{ 1150 struct rlimit bsd_rlim; 1151 struct l_rlimit rlim; 1152 u_int which; 1153 int error; 1154 1155#ifdef DEBUG 1156 if (ldebug(setrlimit)) 1157 printf(ARGS(setrlimit, "%d, %p"), 1158 args->resource, (void *)args->rlim); 1159#endif 1160 1161 if (args->resource >= LINUX_RLIM_NLIMITS) 1162 return (EINVAL); 1163 1164 which = linux_to_bsd_resource[args->resource]; 1165 if (which == -1) 1166 return (EINVAL); 1167 1168 error = copyin(args->rlim, &rlim, sizeof(rlim)); 1169 if (error) 1170 return (error); 1171 1172 bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur; 1173 bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max; 1174 return (kern_setrlimit(td, which, &bsd_rlim)); 1175} 1176 1177int 1178linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args) 1179{ 1180 struct l_rlimit rlim; 1181 struct proc *p = td->td_proc; 1182 struct rlimit bsd_rlim; 1183 u_int which; 1184 1185#ifdef DEBUG 1186 if (ldebug(old_getrlimit)) 1187 printf(ARGS(old_getrlimit, "%d, %p"), 1188 args->resource, (void *)args->rlim); 1189#endif 1190 1191 if (args->resource >= LINUX_RLIM_NLIMITS) 1192 return (EINVAL); 1193 1194 which = linux_to_bsd_resource[args->resource]; 1195 if (which == -1) 1196 return (EINVAL); 1197 1198 PROC_LOCK(p); 1199 lim_rlimit(p, which, &bsd_rlim); 1200 PROC_UNLOCK(p); 1201 1202#ifdef COMPAT_LINUX32 1203 rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur; 1204 if (rlim.rlim_cur == UINT_MAX) 1205 rlim.rlim_cur = INT_MAX; 1206 rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max; 1207 if (rlim.rlim_max == UINT_MAX) 1208 rlim.rlim_max = INT_MAX; 1209#else 1210 rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur; 1211 if (rlim.rlim_cur == ULONG_MAX) 1212 rlim.rlim_cur = LONG_MAX; 1213 rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max; 1214 if (rlim.rlim_max == ULONG_MAX) 1215 rlim.rlim_max = LONG_MAX; 1216#endif 1217 return (copyout(&rlim, args->rlim, sizeof(rlim))); 1218} 1219 1220int 1221linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args) 1222{ 1223 struct l_rlimit rlim; 1224 struct proc *p = td->td_proc; 1225 struct rlimit bsd_rlim; 1226 u_int which; 1227 1228#ifdef DEBUG 1229 if (ldebug(getrlimit)) 1230 printf(ARGS(getrlimit, "%d, %p"), 1231 args->resource, (void *)args->rlim); 1232#endif 1233 1234 if (args->resource >= LINUX_RLIM_NLIMITS) 1235 return (EINVAL); 1236 1237 which = linux_to_bsd_resource[args->resource]; 1238 if (which == -1) 1239 return (EINVAL); 1240 1241 PROC_LOCK(p); 1242 lim_rlimit(p, which, &bsd_rlim); 1243 PROC_UNLOCK(p); 1244 1245 rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur; 1246 rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max; 1247 return (copyout(&rlim, args->rlim, sizeof(rlim))); 1248} 1249 1250int 1251linux_sched_setscheduler(struct thread *td, 1252 struct linux_sched_setscheduler_args *args) 1253{ 1254 struct sched_setscheduler_args bsd; 1255 1256#ifdef DEBUG 1257 if (ldebug(sched_setscheduler)) 1258 printf(ARGS(sched_setscheduler, "%d, %d, %p"), 1259 args->pid, args->policy, (const void *)args->param); 1260#endif 1261 1262 switch (args->policy) { 1263 case LINUX_SCHED_OTHER: 1264 bsd.policy = SCHED_OTHER; 1265 break; 1266 case LINUX_SCHED_FIFO: 1267 bsd.policy = SCHED_FIFO; 1268 break; 1269 case LINUX_SCHED_RR: 1270 bsd.policy = SCHED_RR; 1271 break; 1272 default: 1273 return EINVAL; 1274 } 1275 1276 bsd.pid = args->pid; 1277 bsd.param = (struct sched_param *)args->param; 1278 return sched_setscheduler(td, &bsd); 1279} 1280 1281int 1282linux_sched_getscheduler(struct thread *td, 1283 struct linux_sched_getscheduler_args *args) 1284{ 1285 struct sched_getscheduler_args bsd; 1286 int error; 1287 1288#ifdef DEBUG 1289 if (ldebug(sched_getscheduler)) 1290 printf(ARGS(sched_getscheduler, "%d"), args->pid); 1291#endif 1292 1293 bsd.pid = args->pid; 1294 error = sched_getscheduler(td, &bsd); 1295 1296 switch (td->td_retval[0]) { 1297 case SCHED_OTHER: 1298 td->td_retval[0] = LINUX_SCHED_OTHER; 1299 break; 1300 case SCHED_FIFO: 1301 td->td_retval[0] = LINUX_SCHED_FIFO; 1302 break; 1303 case SCHED_RR: 1304 td->td_retval[0] = LINUX_SCHED_RR; 1305 break; 1306 } 1307 1308 return error; 1309} 1310 1311int 1312linux_sched_get_priority_max(struct thread *td, 1313 struct linux_sched_get_priority_max_args *args) 1314{ 1315 struct sched_get_priority_max_args bsd; 1316 1317#ifdef DEBUG 1318 if (ldebug(sched_get_priority_max)) 1319 printf(ARGS(sched_get_priority_max, "%d"), args->policy); 1320#endif 1321 1322 switch (args->policy) { 1323 case LINUX_SCHED_OTHER: 1324 bsd.policy = SCHED_OTHER; 1325 break; 1326 case LINUX_SCHED_FIFO: 1327 bsd.policy = SCHED_FIFO; 1328 break; 1329 case LINUX_SCHED_RR: 1330 bsd.policy = SCHED_RR; 1331 break; 1332 default: 1333 return EINVAL; 1334 } 1335 return sched_get_priority_max(td, &bsd); 1336} 1337 1338int 1339linux_sched_get_priority_min(struct thread *td, 1340 struct linux_sched_get_priority_min_args *args) 1341{ 1342 struct sched_get_priority_min_args bsd; 1343 1344#ifdef DEBUG 1345 if (ldebug(sched_get_priority_min)) 1346 printf(ARGS(sched_get_priority_min, "%d"), args->policy); 1347#endif 1348 1349 switch (args->policy) { 1350 case LINUX_SCHED_OTHER: 1351 bsd.policy = SCHED_OTHER; 1352 break; 1353 case LINUX_SCHED_FIFO: 1354 bsd.policy = SCHED_FIFO; 1355 break; 1356 case LINUX_SCHED_RR: 1357 bsd.policy = SCHED_RR; 1358 break; 1359 default: 1360 return EINVAL; 1361 } 1362 return sched_get_priority_min(td, &bsd); 1363} 1364 1365#define REBOOT_CAD_ON 0x89abcdef 1366#define REBOOT_CAD_OFF 0 1367#define REBOOT_HALT 0xcdef0123 1368#define REBOOT_RESTART 0x01234567 1369#define REBOOT_RESTART2 0xA1B2C3D4 1370#define REBOOT_POWEROFF 0x4321FEDC 1371#define REBOOT_MAGIC1 0xfee1dead 1372#define REBOOT_MAGIC2 0x28121969 1373#define REBOOT_MAGIC2A 0x05121996 1374#define REBOOT_MAGIC2B 0x16041998 1375 1376int 1377linux_reboot(struct thread *td, struct linux_reboot_args *args) 1378{ 1379 struct reboot_args bsd_args; 1380 1381#ifdef DEBUG 1382 if (ldebug(reboot)) 1383 printf(ARGS(reboot, "0x%x"), args->cmd); 1384#endif 1385 1386 if (args->magic1 != REBOOT_MAGIC1) 1387 return EINVAL; 1388 1389 switch (args->magic2) { 1390 case REBOOT_MAGIC2: 1391 case REBOOT_MAGIC2A: 1392 case REBOOT_MAGIC2B: 1393 break; 1394 default: 1395 return EINVAL; 1396 } 1397 1398 switch (args->cmd) { 1399 case REBOOT_CAD_ON: 1400 case REBOOT_CAD_OFF: 1401 return (priv_check(td, PRIV_REBOOT)); 1402 case REBOOT_HALT: 1403 bsd_args.opt = RB_HALT; 1404 break; 1405 case REBOOT_RESTART: 1406 case REBOOT_RESTART2: 1407 bsd_args.opt = 0; 1408 break; 1409 case REBOOT_POWEROFF: 1410 bsd_args.opt = RB_POWEROFF; 1411 break; 1412 default: 1413 return EINVAL; 1414 } 1415 return reboot(td, &bsd_args); 1416} 1417 1418 1419/* 1420 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify 1421 * td->td_retval[1] when COMPAT_43 is defined. This 1422 * globbers registers that are assumed to be preserved. The following 1423 * lightweight syscalls fixes this. See also linux_getgid16() and 1424 * linux_getuid16() in linux_uid16.c. 1425 * 1426 * linux_getpid() - MP SAFE 1427 * linux_getgid() - MP SAFE 1428 * linux_getuid() - MP SAFE 1429 */ 1430 1431int 1432linux_getpid(struct thread *td, struct linux_getpid_args *args) 1433{ 1434 struct linux_emuldata *em; 1435 1436#ifdef DEBUG 1437 if (ldebug(getpid)) 1438 printf(ARGS(getpid, "")); 1439#endif 1440 1441 if (linux_use26(td)) {
|