1 /*- 2 * Copyright (c) 1994-1995 S�ren Schmidt 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer 10 * in this position and unchanged. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 #include "opt_compat.h" 32 #include "opt_mac.h" 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/fcntl.h> 37 #include <sys/imgact_aout.h> 38 #include <sys/jail.h> 39 #include <sys/kernel.h> 40 #include <sys/lock.h> 41 #include <sys/mac.h> 42 #include <sys/mman.h> 43 #include <sys/mount.h> 44 #include <sys/mutex.h> 45 #include <sys/namei.h> 46 #include <sys/poll.h> 47 #include <sys/proc.h> 48 #include <sys/blist.h> 49 #include <sys/reboot.h> 50 #include <sys/resourcevar.h> 51 #include <sys/signalvar.h> 52 #include <sys/stat.h> 53 #include <sys/sysctl.h> 54 #include <sys/sysproto.h> 55 #include <sys/time.h> 56 #include <sys/unistd.h> 57 #include <sys/vmmeter.h> 58 #include <sys/vnode.h> 59 #include <sys/wait.h> 60 61 #include <vm/vm.h> 62 #include <vm/pmap.h> 63 #include <vm/vm_kern.h> 64 #include <vm/vm_map.h> 65 #include <vm/vm_extern.h> 66 #include <vm/vm_object.h> 67 #include <vm/swap_pager.h> 68 69 #include <machine/limits.h> 70 71 #include <posix4/sched.h> 72 73 #include <machine/../linux/linux.h> 74 #include <machine/../linux/linux_proto.h> 75 #include <compat/linux/linux_mib.h> 76 #include <compat/linux/linux_util.h> 77 78 #ifdef __alpha__ 79 #define BSD_TO_LINUX_SIGNAL(sig) (sig) 80 #else 81 #define BSD_TO_LINUX_SIGNAL(sig) \ 82 (((sig) <= LINUX_SIGTBLSZ) ? bsd_to_linux_signal[_SIG_IDX(sig)] : sig) 83 #endif 84 85 #ifndef __alpha__ 86 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = { 87 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK, 88 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE, 89 RLIMIT_MEMLOCK, -1 90 }; 91 #endif /*!__alpha__*/ 92 93 struct l_sysinfo { 94 l_long uptime; /* Seconds since boot */ 95 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */ 96 l_ulong totalram; /* Total usable main memory size */ 97 l_ulong freeram; /* Available memory size */ 98 l_ulong sharedram; /* Amount of shared memory */ 99 l_ulong bufferram; /* Memory used by buffers */ 100 l_ulong totalswap; /* Total swap space size */ 101 l_ulong freeswap; /* swap space still available */ 102 l_ushort procs; /* Number of current processes */ 103 char _f[22]; /* Pads structure to 64 bytes */ 104 }; 105 #ifndef __alpha__ 106 int 107 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args) 108 { 109 struct l_sysinfo sysinfo; 110 vm_object_t object; 111 int i; 112 struct timespec ts; 113 114 /* Uptime is copied out of print_uptime() in kern_shutdown.c */ 115 getnanouptime(&ts); 116 i = 0; 117 if (ts.tv_sec >= 86400) { 118 ts.tv_sec %= 86400; 119 i = 1; 120 } 121 if (i || ts.tv_sec >= 3600) { 122 ts.tv_sec %= 3600; 123 i = 1; 124 } 125 if (i || ts.tv_sec >= 60) { 126 ts.tv_sec %= 60; 127 i = 1; 128 } 129 sysinfo.uptime=ts.tv_sec; 130 131 /* Use the information from the mib to get our load averages */ 132 for (i = 0; i < 3; i++) 133 sysinfo.loads[i] = averunnable.ldavg[i]; 134 135 sysinfo.totalram = physmem * PAGE_SIZE; 136 sysinfo.freeram = sysinfo.totalram - cnt.v_wire_count * PAGE_SIZE; 137 138 sysinfo.sharedram = 0; 139 for (object = TAILQ_FIRST(&vm_object_list); object != NULL; 140 object = TAILQ_NEXT(object, object_list)) 141 if (object->shadow_count > 1) 142 sysinfo.sharedram += object->resident_page_count; 143 144 sysinfo.sharedram *= PAGE_SIZE; 145 sysinfo.bufferram = 0; 146 147 if (swapblist == NULL) { 148 sysinfo.totalswap= 0; 149 sysinfo.freeswap = 0; 150 } else { 151 sysinfo.totalswap = swapblist->bl_blocks * 1024; 152 sysinfo.freeswap = swapblist->bl_root->u.bmu_avail * PAGE_SIZE; 153 } 154 155 sysinfo.procs = 20; /* Hack */ 156 157 return copyout(&sysinfo, (caddr_t)args->info, sizeof(sysinfo)); 158 } 159 #endif /*!__alpha__*/ 160 161 #ifndef __alpha__ 162 int 163 linux_alarm(struct thread *td, struct linux_alarm_args *args) 164 { 165 struct itimerval it, old_it; 166 struct timeval tv; 167 int s; 168 169 #ifdef DEBUG 170 if (ldebug(alarm)) 171 printf(ARGS(alarm, "%u"), args->secs); 172 #endif 173 174 if (args->secs > 100000000) 175 return EINVAL; 176 177 it.it_value.tv_sec = (long)args->secs; 178 it.it_value.tv_usec = 0; 179 it.it_interval.tv_sec = 0; 180 it.it_interval.tv_usec = 0; 181 s = splsoftclock(); 182 old_it = td->td_proc->p_realtimer; 183 getmicrouptime(&tv); 184 if (timevalisset(&old_it.it_value)) 185 callout_stop(&td->td_proc->p_itcallout); 186 if (it.it_value.tv_sec != 0) { 187 callout_reset(&td->td_proc->p_itcallout, tvtohz(&it.it_value), 188 realitexpire, td->td_proc); 189 timevaladd(&it.it_value, &tv); 190 } 191 td->td_proc->p_realtimer = it; 192 splx(s); 193 if (timevalcmp(&old_it.it_value, &tv, >)) { 194 timevalsub(&old_it.it_value, &tv); 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 #endif /*!__alpha__*/ 202 203 int 204 linux_brk(struct thread *td, struct linux_brk_args *args) 205 { 206 struct vmspace *vm = td->td_proc->p_vmspace; 207 vm_offset_t new, old; 208 struct obreak_args /* { 209 char * nsize; 210 } */ tmp; 211 212 #ifdef DEBUG 213 if (ldebug(brk)) 214 printf(ARGS(brk, "%p"), (void *)args->dsend); 215 #endif 216 old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize); 217 new = (vm_offset_t)args->dsend; 218 tmp.nsize = (char *) new; 219 if (((caddr_t)new > vm->vm_daddr) && !obreak(td, &tmp)) 220 td->td_retval[0] = (long)new; 221 else 222 td->td_retval[0] = (long)old; 223 224 return 0; 225 } 226 227 int 228 linux_uselib(struct thread *td, struct linux_uselib_args *args) 229 { 230 struct nameidata ni; 231 struct vnode *vp; 232 struct exec *a_out; 233 struct vattr attr; 234 vm_offset_t vmaddr; 235 unsigned long file_offset; 236 vm_offset_t buffer; 237 unsigned long bss_size; 238 int error; 239 caddr_t sg; 240 int locked; 241 242 sg = stackgap_init(); 243 CHECKALTEXIST(td, &sg, args->library); 244 245 #ifdef DEBUG 246 if (ldebug(uselib)) 247 printf(ARGS(uselib, "%s"), args->library); 248 #endif 249 250 a_out = NULL; 251 locked = 0; 252 vp = NULL; 253 254 /* 255 * XXX: This code should make use of vn_open(), rather than doing 256 * all this stuff itself. 257 */ 258 NDINIT(&ni, LOOKUP, FOLLOW|LOCKLEAF, UIO_USERSPACE, args->library, td); 259 error = namei(&ni); 260 if (error) 261 goto cleanup; 262 263 vp = ni.ni_vp; 264 /* 265 * XXX - This looks like a bogus check. A LOCKLEAF namei should not 266 * succeed without returning a vnode. 267 */ 268 if (vp == NULL) { 269 error = ENOEXEC; /* ?? */ 270 goto cleanup; 271 } 272 NDFREE(&ni, NDF_ONLY_PNBUF); 273 274 /* 275 * From here on down, we have a locked vnode that must be unlocked. 276 */ 277 locked++; 278 279 /* Writable? */ 280 if (vp->v_writecount) { 281 error = ETXTBSY; 282 goto cleanup; 283 } 284 285 /* Executable? */ 286 error = VOP_GETATTR(vp, &attr, td->td_ucred, td); 287 if (error) 288 goto cleanup; 289 290 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || 291 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) { 292 error = ENOEXEC; 293 goto cleanup; 294 } 295 296 /* Sensible size? */ 297 if (attr.va_size == 0) { 298 error = ENOEXEC; 299 goto cleanup; 300 } 301 302 /* Can we access it? */ 303 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td); 304 if (error) 305 goto cleanup; 306 307 /* 308 * XXX: This should use vn_open() so that it is properly authorized, 309 * and to reduce code redundancy all over the place here. 310 */ 311 #ifdef MAC 312 error = mac_check_vnode_open(td->td_ucred, vp, FREAD); 313 if (error) 314 goto cleanup; 315 #endif 316 error = VOP_OPEN(vp, FREAD, td->td_ucred, td); 317 if (error) 318 goto cleanup; 319 320 /* 321 * Lock no longer needed 322 */ 323 VOP_UNLOCK(vp, 0, td); 324 locked = 0; 325 326 /* Pull in executable header into kernel_map */ 327 error = vm_mmap(kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE, 328 VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp, 0); 329 if (error) 330 goto cleanup; 331 332 /* Is it a Linux binary ? */ 333 if (((a_out->a_magic >> 16) & 0xff) != 0x64) { 334 error = ENOEXEC; 335 goto cleanup; 336 } 337 338 /* 339 * While we are here, we should REALLY do some more checks 340 */ 341 342 /* Set file/virtual offset based on a.out variant. */ 343 switch ((int)(a_out->a_magic & 0xffff)) { 344 case 0413: /* ZMAGIC */ 345 file_offset = 1024; 346 break; 347 case 0314: /* QMAGIC */ 348 file_offset = 0; 349 break; 350 default: 351 error = ENOEXEC; 352 goto cleanup; 353 } 354 355 bss_size = round_page(a_out->a_bss); 356 357 /* Check various fields in header for validity/bounds. */ 358 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) { 359 error = ENOEXEC; 360 goto cleanup; 361 } 362 363 /* text + data can't exceed file size */ 364 if (a_out->a_data + a_out->a_text > attr.va_size) { 365 error = EFAULT; 366 goto cleanup; 367 } 368 369 /* To protect td->td_proc->p_rlimit in the if condition. */ 370 mtx_assert(&Giant, MA_OWNED); 371 372 /* 373 * text/data/bss must not exceed limits 374 * XXX - this is not complete. it should check current usage PLUS 375 * the resources needed by this library. 376 */ 377 if (a_out->a_text > maxtsiz || 378 a_out->a_data + bss_size > 379 td->td_proc->p_rlimit[RLIMIT_DATA].rlim_cur) { 380 error = ENOMEM; 381 goto cleanup; 382 } 383 384 mp_fixme("Unlocked vflags access."); 385 /* prevent more writers */ 386 vp->v_vflag |= VV_TEXT; 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, (caddr_t)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((caddr_t)(uintptr_t)(buffer + file_offset), 419 (caddr_t)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, (caddr_t)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, td); 467 468 /* Release the kernel mapping. */ 469 if (a_out) 470 vm_map_remove(kernel_map, (vm_offset_t)a_out, 471 (vm_offset_t)a_out + PAGE_SIZE); 472 473 return error; 474 } 475 476 int 477 linux_select(struct thread *td, struct linux_select_args *args) 478 { 479 struct select_args bsa; 480 struct timeval tv0, tv1, utv, *tvp; 481 caddr_t sg; 482 int error; 483 484 #ifdef DEBUG 485 if (ldebug(select)) 486 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds, 487 (void *)args->readfds, (void *)args->writefds, 488 (void *)args->exceptfds, (void *)args->timeout); 489 #endif 490 491 error = 0; 492 bsa.nd = args->nfds; 493 bsa.in = args->readfds; 494 bsa.ou = args->writefds; 495 bsa.ex = args->exceptfds; 496 bsa.tv = (struct timeval *)args->timeout; 497 498 /* 499 * Store current time for computation of the amount of 500 * time left. 501 */ 502 if (args->timeout) { 503 if ((error = copyin((caddr_t)args->timeout, &utv, 504 sizeof(utv)))) 505 goto select_out; 506 #ifdef DEBUG 507 if (ldebug(select)) 508 printf(LMSG("incoming timeout (%ld/%ld)"), 509 utv.tv_sec, utv.tv_usec); 510 #endif 511 512 if (itimerfix(&utv)) { 513 /* 514 * The timeval was invalid. Convert it to something 515 * valid that will act as it does under Linux. 516 */ 517 sg = stackgap_init(); 518 tvp = stackgap_alloc(&sg, sizeof(utv)); 519 utv.tv_sec += utv.tv_usec / 1000000; 520 utv.tv_usec %= 1000000; 521 if (utv.tv_usec < 0) { 522 utv.tv_sec -= 1; 523 utv.tv_usec += 1000000; 524 } 525 if (utv.tv_sec < 0) 526 timevalclear(&utv); 527 if ((error = copyout(&utv, tvp, sizeof(utv)))) 528 goto select_out; 529 bsa.tv = tvp; 530 } 531 microtime(&tv0); 532 } 533 534 error = select(td, &bsa); 535 #ifdef DEBUG 536 if (ldebug(select)) 537 printf(LMSG("real select returns %d"), error); 538 #endif 539 if (error) { 540 /* 541 * See fs/select.c in the Linux kernel. Without this, 542 * Maelstrom doesn't work. 543 */ 544 if (error == ERESTART) 545 error = EINTR; 546 goto select_out; 547 } 548 549 if (args->timeout) { 550 if (td->td_retval[0]) { 551 /* 552 * Compute how much time was left of the timeout, 553 * by subtracting the current time and the time 554 * before we started the call, and subtracting 555 * that result from the user-supplied value. 556 */ 557 microtime(&tv1); 558 timevalsub(&tv1, &tv0); 559 timevalsub(&utv, &tv1); 560 if (utv.tv_sec < 0) 561 timevalclear(&utv); 562 } else 563 timevalclear(&utv); 564 #ifdef DEBUG 565 if (ldebug(select)) 566 printf(LMSG("outgoing timeout (%ld/%ld)"), 567 utv.tv_sec, utv.tv_usec); 568 #endif 569 if ((error = copyout(&utv, (caddr_t)args->timeout, 570 sizeof(utv)))) 571 goto select_out; 572 } 573 574 select_out: 575 #ifdef DEBUG 576 if (ldebug(select)) 577 printf(LMSG("select_out -> %d"), error); 578 #endif 579 return error; 580 } 581 582 int 583 linux_mremap(struct thread *td, struct linux_mremap_args *args) 584 { 585 struct munmap_args /* { 586 void *addr; 587 size_t len; 588 } */ bsd_args; 589 int error = 0; 590 591 #ifdef DEBUG 592 if (ldebug(mremap)) 593 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"), 594 (void *)args->addr, 595 (unsigned long)args->old_len, 596 (unsigned long)args->new_len, 597 (unsigned long)args->flags); 598 #endif 599 args->new_len = round_page(args->new_len); 600 args->old_len = round_page(args->old_len); 601 602 if (args->new_len > args->old_len) { 603 td->td_retval[0] = 0; 604 return ENOMEM; 605 } 606 607 if (args->new_len < args->old_len) { 608 bsd_args.addr = (caddr_t)(args->addr + args->new_len); 609 bsd_args.len = args->old_len - args->new_len; 610 error = munmap(td, &bsd_args); 611 } 612 613 td->td_retval[0] = error ? 0 : (u_long)args->addr; 614 return error; 615 } 616 617 int 618 linux_msync(struct thread *td, struct linux_msync_args *args) 619 { 620 struct msync_args bsd_args; 621 622 bsd_args.addr = (caddr_t)args->addr; 623 bsd_args.len = args->len; 624 bsd_args.flags = 0; /* XXX ignore */ 625 626 return msync(td, &bsd_args); 627 } 628 629 #ifndef __alpha__ 630 int 631 linux_time(struct thread *td, struct linux_time_args *args) 632 { 633 struct timeval tv; 634 l_time_t tm; 635 int error; 636 637 #ifdef DEBUG 638 if (ldebug(time)) 639 printf(ARGS(time, "*")); 640 #endif 641 642 microtime(&tv); 643 tm = tv.tv_sec; 644 if (args->tm && (error = copyout(&tm, (caddr_t)args->tm, sizeof(tm)))) 645 return error; 646 td->td_retval[0] = tm; 647 return 0; 648 } 649 #endif /*!__alpha__*/ 650 651 struct l_times_argv { 652 l_long tms_utime; 653 l_long tms_stime; 654 l_long tms_cutime; 655 l_long tms_cstime; 656 }; 657 658 #ifdef __alpha__ 659 #define CLK_TCK 1024 /* Linux uses 1024 on alpha */ 660 #else 661 #define CLK_TCK 100 /* Linux uses 100 */ 662 #endif 663 664 #define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK)) 665 666 int 667 linux_times(struct thread *td, struct linux_times_args *args) 668 { 669 struct timeval tv; 670 struct l_times_argv tms; 671 struct rusage ru; 672 int error; 673 674 #ifdef DEBUG 675 if (ldebug(times)) 676 printf(ARGS(times, "*")); 677 #endif 678 679 mtx_lock_spin(&sched_lock); 680 calcru(td->td_proc, &ru.ru_utime, &ru.ru_stime, NULL); 681 mtx_unlock_spin(&sched_lock); 682 683 tms.tms_utime = CONVTCK(ru.ru_utime); 684 tms.tms_stime = CONVTCK(ru.ru_stime); 685 686 tms.tms_cutime = CONVTCK(td->td_proc->p_stats->p_cru.ru_utime); 687 tms.tms_cstime = CONVTCK(td->td_proc->p_stats->p_cru.ru_stime); 688 689 if ((error = copyout(&tms, (caddr_t)args->buf, sizeof(tms)))) 690 return error; 691 692 microuptime(&tv); 693 td->td_retval[0] = (int)CONVTCK(tv); 694 return 0; 695 } 696 697 int 698 linux_newuname(struct thread *td, struct linux_newuname_args *args) 699 { 700 struct l_new_utsname utsname; 701 char osname[LINUX_MAX_UTSNAME]; 702 char osrelease[LINUX_MAX_UTSNAME]; 703 704 #ifdef DEBUG 705 if (ldebug(newuname)) 706 printf(ARGS(newuname, "*")); 707 #endif 708 709 linux_get_osname(td->td_proc, osname); 710 linux_get_osrelease(td->td_proc, osrelease); 711 712 bzero(&utsname, sizeof(utsname)); 713 strncpy(utsname.sysname, osname, LINUX_MAX_UTSNAME-1); 714 getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME-1); 715 strncpy(utsname.release, osrelease, LINUX_MAX_UTSNAME-1); 716 strncpy(utsname.version, version, LINUX_MAX_UTSNAME-1); 717 strncpy(utsname.machine, machine, LINUX_MAX_UTSNAME-1); 718 strncpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME-1); 719 720 return (copyout(&utsname, (caddr_t)args->buf, sizeof(utsname))); 721 } 722 723 #if defined(__i386__) 724 struct l_utimbuf { 725 l_time_t l_actime; 726 l_time_t l_modtime; 727 }; 728 729 int 730 linux_utime(struct thread *td, struct linux_utime_args *args) 731 { 732 struct utimes_args /* { 733 char *path; 734 struct timeval *tptr; 735 } */ bsdutimes; 736 struct timeval tv[2], *tvp; 737 struct l_utimbuf lut; 738 int error; 739 caddr_t sg; 740 741 sg = stackgap_init(); 742 CHECKALTEXIST(td, &sg, args->fname); 743 744 #ifdef DEBUG 745 if (ldebug(utime)) 746 printf(ARGS(utime, "%s, *"), args->fname); 747 #endif 748 749 if (args->times) { 750 if ((error = copyin((caddr_t)args->times, &lut, sizeof lut))) 751 return error; 752 tv[0].tv_sec = lut.l_actime; 753 tv[0].tv_usec = 0; 754 tv[1].tv_sec = lut.l_modtime; 755 tv[1].tv_usec = 0; 756 /* so that utimes can copyin */ 757 tvp = (struct timeval *)stackgap_alloc(&sg, sizeof(tv)); 758 if (tvp == NULL) 759 return (ENAMETOOLONG); 760 if ((error = copyout(tv, tvp, sizeof(tv)))) 761 return error; 762 bsdutimes.tptr = tvp; 763 } else 764 bsdutimes.tptr = NULL; 765 766 bsdutimes.path = args->fname; 767 return utimes(td, &bsdutimes); 768 } 769 #endif /* __i386__ */ 770 771 #define __WCLONE 0x80000000 772 773 #ifndef __alpha__ 774 int 775 linux_waitpid(struct thread *td, struct linux_waitpid_args *args) 776 { 777 struct wait_args /* { 778 int pid; 779 int *status; 780 int options; 781 struct rusage *rusage; 782 } */ tmp; 783 int error, tmpstat; 784 785 #ifdef DEBUG 786 if (ldebug(waitpid)) 787 printf(ARGS(waitpid, "%d, %p, %d"), 788 args->pid, (void *)args->status, args->options); 789 #endif 790 791 tmp.pid = args->pid; 792 tmp.status = args->status; 793 tmp.options = (args->options & (WNOHANG | WUNTRACED)); 794 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */ 795 if (args->options & __WCLONE) 796 tmp.options |= WLINUXCLONE; 797 tmp.rusage = NULL; 798 799 if ((error = wait4(td, &tmp)) != 0) 800 return error; 801 802 if (args->status) { 803 if ((error = copyin((caddr_t)args->status, &tmpstat, 804 sizeof(int))) != 0) 805 return error; 806 tmpstat &= 0xffff; 807 if (WIFSIGNALED(tmpstat)) 808 tmpstat = (tmpstat & 0xffffff80) | 809 BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat)); 810 else if (WIFSTOPPED(tmpstat)) 811 tmpstat = (tmpstat & 0xffff00ff) | 812 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8); 813 return copyout(&tmpstat, (caddr_t)args->status, sizeof(int)); 814 } 815 816 return 0; 817 } 818 #endif /*!__alpha__*/ 819 820 int 821 linux_wait4(struct thread *td, struct linux_wait4_args *args) 822 { 823 struct wait_args /* { 824 int pid; 825 int *status; 826 int options; 827 struct rusage *rusage; 828 } */ tmp; 829 int error, tmpstat; 830 831 #ifdef DEBUG 832 if (ldebug(wait4)) 833 printf(ARGS(wait4, "%d, %p, %d, %p"), 834 args->pid, (void *)args->status, args->options, 835 (void *)args->rusage); 836 #endif 837 838 tmp.pid = args->pid; 839 tmp.status = args->status; 840 tmp.options = (args->options & (WNOHANG | WUNTRACED)); 841 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */ 842 if (args->options & __WCLONE) 843 tmp.options |= WLINUXCLONE; 844 tmp.rusage = (struct rusage *)args->rusage; 845 846 if ((error = wait4(td, &tmp)) != 0) 847 return error; 848 849 SIGDELSET(td->td_proc->p_siglist, SIGCHLD); 850 851 if (args->status) { 852 if ((error = copyin((caddr_t)args->status, &tmpstat, 853 sizeof(int))) != 0) 854 return error; 855 tmpstat &= 0xffff; 856 if (WIFSIGNALED(tmpstat)) 857 tmpstat = (tmpstat & 0xffffff80) | 858 BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat)); 859 else if (WIFSTOPPED(tmpstat)) 860 tmpstat = (tmpstat & 0xffff00ff) | 861 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8); 862 return copyout(&tmpstat, (caddr_t)args->status, sizeof(int)); 863 } 864 865 return 0; 866 } 867 868 int 869 linux_mknod(struct thread *td, struct linux_mknod_args *args) 870 { 871 caddr_t sg; 872 struct mknod_args bsd_mknod; 873 struct mkfifo_args bsd_mkfifo; 874 875 sg = stackgap_init(); 876 877 CHECKALTCREAT(td, &sg, args->path); 878 879 #ifdef DEBUG 880 if (ldebug(mknod)) 881 printf(ARGS(mknod, "%s, %d, %d"), 882 args->path, args->mode, args->dev); 883 #endif 884 885 if (args->mode & S_IFIFO) { 886 bsd_mkfifo.path = args->path; 887 bsd_mkfifo.mode = args->mode; 888 return mkfifo(td, &bsd_mkfifo); 889 } else { 890 bsd_mknod.path = args->path; 891 bsd_mknod.mode = args->mode; 892 bsd_mknod.dev = args->dev; 893 return mknod(td, &bsd_mknod); 894 } 895 } 896 897 /* 898 * UGH! This is just about the dumbest idea I've ever heard!! 899 */ 900 int 901 linux_personality(struct thread *td, struct linux_personality_args *args) 902 { 903 #ifdef DEBUG 904 if (ldebug(personality)) 905 printf(ARGS(personality, "%d"), args->per); 906 #endif 907 #ifndef __alpha__ 908 if (args->per != 0) 909 return EINVAL; 910 #endif 911 912 /* Yes Jim, it's still a Linux... */ 913 td->td_retval[0] = 0; 914 return 0; 915 } 916 917 /* 918 * Wrappers for get/setitimer for debugging.. 919 */ 920 int 921 linux_setitimer(struct thread *td, struct linux_setitimer_args *args) 922 { 923 struct setitimer_args bsa; 924 struct itimerval foo; 925 int error; 926 927 #ifdef DEBUG 928 if (ldebug(setitimer)) 929 printf(ARGS(setitimer, "%p, %p"), 930 (void *)args->itv, (void *)args->oitv); 931 #endif 932 bsa.which = args->which; 933 bsa.itv = (struct itimerval *)args->itv; 934 bsa.oitv = (struct itimerval *)args->oitv; 935 if (args->itv) { 936 if ((error = copyin((caddr_t)args->itv, &foo, sizeof(foo)))) 937 return error; 938 #ifdef DEBUG 939 if (ldebug(setitimer)) { 940 printf("setitimer: value: sec: %ld, usec: %ld\n", 941 foo.it_value.tv_sec, foo.it_value.tv_usec); 942 printf("setitimer: interval: sec: %ld, usec: %ld\n", 943 foo.it_interval.tv_sec, foo.it_interval.tv_usec); 944 } 945 #endif 946 } 947 return setitimer(td, &bsa); 948 } 949 950 int 951 linux_getitimer(struct thread *td, struct linux_getitimer_args *args) 952 { 953 struct getitimer_args bsa; 954 #ifdef DEBUG 955 if (ldebug(getitimer)) 956 printf(ARGS(getitimer, "%p"), (void *)args->itv); 957 #endif 958 bsa.which = args->which; 959 bsa.itv = (struct itimerval *)args->itv; 960 return getitimer(td, &bsa); 961 } 962 963 #ifndef __alpha__ 964 int 965 linux_nice(struct thread *td, struct linux_nice_args *args) 966 { 967 struct setpriority_args bsd_args; 968 969 bsd_args.which = PRIO_PROCESS; 970 bsd_args.who = 0; /* current process */ 971 bsd_args.prio = args->inc; 972 return setpriority(td, &bsd_args); 973 } 974 #endif /*!__alpha__*/ 975 976 int 977 linux_setgroups(struct thread *td, struct linux_setgroups_args *args) 978 { 979 struct ucred *newcred, *oldcred; 980 l_gid_t linux_gidset[NGROUPS]; 981 gid_t *bsd_gidset; 982 int ngrp, error; 983 struct proc *p; 984 985 ngrp = args->gidsetsize; 986 if (ngrp >= NGROUPS) 987 return (EINVAL); 988 error = copyin((caddr_t)args->grouplist, linux_gidset, 989 ngrp * sizeof(l_gid_t)); 990 if (error) 991 return (error); 992 newcred = crget(); 993 p = td->td_proc; 994 PROC_LOCK(p); 995 oldcred = p->p_ucred; 996 997 /* 998 * cr_groups[0] holds egid. Setting the whole set from 999 * the supplied set will cause egid to be changed too. 1000 * Keep cr_groups[0] unchanged to prevent that. 1001 */ 1002 1003 if ((error = suser_cred(oldcred, PRISON_ROOT)) != 0) { 1004 PROC_UNLOCK(p); 1005 crfree(newcred); 1006 return (error); 1007 } 1008 1009 crcopy(newcred, oldcred); 1010 if (ngrp > 0) { 1011 newcred->cr_ngroups = ngrp + 1; 1012 1013 bsd_gidset = newcred->cr_groups; 1014 ngrp--; 1015 while (ngrp >= 0) { 1016 bsd_gidset[ngrp + 1] = linux_gidset[ngrp]; 1017 ngrp--; 1018 } 1019 } 1020 else 1021 newcred->cr_ngroups = 1; 1022 1023 setsugid(p); 1024 p->p_ucred = newcred; 1025 PROC_UNLOCK(p); 1026 crfree(oldcred); 1027 return (0); 1028 } 1029 1030 int 1031 linux_getgroups(struct thread *td, struct linux_getgroups_args *args) 1032 { 1033 struct ucred *cred; 1034 l_gid_t linux_gidset[NGROUPS]; 1035 gid_t *bsd_gidset; 1036 int bsd_gidsetsz, ngrp, error; 1037 1038 cred = td->td_ucred; 1039 bsd_gidset = cred->cr_groups; 1040 bsd_gidsetsz = cred->cr_ngroups - 1; 1041 1042 /* 1043 * cr_groups[0] holds egid. Returning the whole set 1044 * here will cause a duplicate. Exclude cr_groups[0] 1045 * to prevent that. 1046 */ 1047 1048 if ((ngrp = args->gidsetsize) == 0) { 1049 td->td_retval[0] = bsd_gidsetsz; 1050 return (0); 1051 } 1052 1053 if (ngrp < bsd_gidsetsz) 1054 return (EINVAL); 1055 1056 ngrp = 0; 1057 while (ngrp < bsd_gidsetsz) { 1058 linux_gidset[ngrp] = bsd_gidset[ngrp + 1]; 1059 ngrp++; 1060 } 1061 1062 if ((error = copyout(linux_gidset, (caddr_t)args->grouplist, 1063 ngrp * sizeof(l_gid_t)))) 1064 return (error); 1065 1066 td->td_retval[0] = ngrp; 1067 return (0); 1068 } 1069 1070 #ifndef __alpha__ 1071 int 1072 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args) 1073 { 1074 struct __setrlimit_args bsd; 1075 struct l_rlimit rlim; 1076 int error; 1077 caddr_t sg = stackgap_init(); 1078 1079 #ifdef DEBUG 1080 if (ldebug(setrlimit)) 1081 printf(ARGS(setrlimit, "%d, %p"), 1082 args->resource, (void *)args->rlim); 1083 #endif 1084 1085 if (args->resource >= LINUX_RLIM_NLIMITS) 1086 return (EINVAL); 1087 1088 bsd.which = linux_to_bsd_resource[args->resource]; 1089 if (bsd.which == -1) 1090 return (EINVAL); 1091 1092 error = copyin((caddr_t)args->rlim, &rlim, sizeof(rlim)); 1093 if (error) 1094 return (error); 1095 1096 bsd.rlp = stackgap_alloc(&sg, sizeof(struct rlimit)); 1097 bsd.rlp->rlim_cur = (rlim_t)rlim.rlim_cur; 1098 bsd.rlp->rlim_max = (rlim_t)rlim.rlim_max; 1099 return (setrlimit(td, &bsd)); 1100 } 1101 1102 int 1103 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args) 1104 { 1105 struct __getrlimit_args bsd; 1106 struct l_rlimit rlim; 1107 int error; 1108 caddr_t sg = stackgap_init(); 1109 1110 #ifdef DEBUG 1111 if (ldebug(old_getrlimit)) 1112 printf(ARGS(old_getrlimit, "%d, %p"), 1113 args->resource, (void *)args->rlim); 1114 #endif 1115 1116 if (args->resource >= LINUX_RLIM_NLIMITS) 1117 return (EINVAL); 1118 1119 bsd.which = linux_to_bsd_resource[args->resource]; 1120 if (bsd.which == -1) 1121 return (EINVAL); 1122 1123 bsd.rlp = stackgap_alloc(&sg, sizeof(struct rlimit)); 1124 error = getrlimit(td, &bsd); 1125 if (error) 1126 return (error); 1127 1128 rlim.rlim_cur = (unsigned long)bsd.rlp->rlim_cur; 1129 if (rlim.rlim_cur == ULONG_MAX) 1130 rlim.rlim_cur = LONG_MAX; 1131 rlim.rlim_max = (unsigned long)bsd.rlp->rlim_max; 1132 if (rlim.rlim_max == ULONG_MAX) 1133 rlim.rlim_max = LONG_MAX; 1134 return (copyout(&rlim, (caddr_t)args->rlim, sizeof(rlim))); 1135 } 1136 1137 int 1138 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args) 1139 { 1140 struct __getrlimit_args bsd; 1141 struct l_rlimit rlim; 1142 int error; 1143 caddr_t sg = stackgap_init(); 1144 1145 #ifdef DEBUG 1146 if (ldebug(getrlimit)) 1147 printf(ARGS(getrlimit, "%d, %p"), 1148 args->resource, (void *)args->rlim); 1149 #endif 1150 1151 if (args->resource >= LINUX_RLIM_NLIMITS) 1152 return (EINVAL); 1153 1154 bsd.which = linux_to_bsd_resource[args->resource]; 1155 if (bsd.which == -1) 1156 return (EINVAL); 1157 1158 bsd.rlp = stackgap_alloc(&sg, sizeof(struct rlimit)); 1159 error = getrlimit(td, &bsd); 1160 if (error) 1161 return (error); 1162 1163 rlim.rlim_cur = (l_ulong)bsd.rlp->rlim_cur; 1164 rlim.rlim_max = (l_ulong)bsd.rlp->rlim_max; 1165 return (copyout(&rlim, (caddr_t)args->rlim, sizeof(rlim))); 1166 } 1167 #endif /*!__alpha__*/ 1168 1169 int 1170 linux_sched_setscheduler(struct thread *td, 1171 struct linux_sched_setscheduler_args *args) 1172 { 1173 struct sched_setscheduler_args bsd; 1174 1175 #ifdef DEBUG 1176 if (ldebug(sched_setscheduler)) 1177 printf(ARGS(sched_setscheduler, "%d, %d, %p"), 1178 args->pid, args->policy, (const void *)args->param); 1179 #endif 1180 1181 switch (args->policy) { 1182 case LINUX_SCHED_OTHER: 1183 bsd.policy = SCHED_OTHER; 1184 break; 1185 case LINUX_SCHED_FIFO: 1186 bsd.policy = SCHED_FIFO; 1187 break; 1188 case LINUX_SCHED_RR: 1189 bsd.policy = SCHED_RR; 1190 break; 1191 default: 1192 return EINVAL; 1193 } 1194 1195 bsd.pid = args->pid; 1196 bsd.param = (struct sched_param *)args->param; 1197 return sched_setscheduler(td, &bsd); 1198 } 1199 1200 int 1201 linux_sched_getscheduler(struct thread *td, 1202 struct linux_sched_getscheduler_args *args) 1203 { 1204 struct sched_getscheduler_args bsd; 1205 int error; 1206 1207 #ifdef DEBUG 1208 if (ldebug(sched_getscheduler)) 1209 printf(ARGS(sched_getscheduler, "%d"), args->pid); 1210 #endif 1211 1212 bsd.pid = args->pid; 1213 error = sched_getscheduler(td, &bsd); 1214 1215 switch (td->td_retval[0]) { 1216 case SCHED_OTHER: 1217 td->td_retval[0] = LINUX_SCHED_OTHER; 1218 break; 1219 case SCHED_FIFO: 1220 td->td_retval[0] = LINUX_SCHED_FIFO; 1221 break; 1222 case SCHED_RR: 1223 td->td_retval[0] = LINUX_SCHED_RR; 1224 break; 1225 } 1226 1227 return error; 1228 } 1229 1230 int 1231 linux_sched_get_priority_max(struct thread *td, 1232 struct linux_sched_get_priority_max_args *args) 1233 { 1234 struct sched_get_priority_max_args bsd; 1235 1236 #ifdef DEBUG 1237 if (ldebug(sched_get_priority_max)) 1238 printf(ARGS(sched_get_priority_max, "%d"), args->policy); 1239 #endif 1240 1241 switch (args->policy) { 1242 case LINUX_SCHED_OTHER: 1243 bsd.policy = SCHED_OTHER; 1244 break; 1245 case LINUX_SCHED_FIFO: 1246 bsd.policy = SCHED_FIFO; 1247 break; 1248 case LINUX_SCHED_RR: 1249 bsd.policy = SCHED_RR; 1250 break; 1251 default: 1252 return EINVAL; 1253 } 1254 return sched_get_priority_max(td, &bsd); 1255 } 1256 1257 int 1258 linux_sched_get_priority_min(struct thread *td, 1259 struct linux_sched_get_priority_min_args *args) 1260 { 1261 struct sched_get_priority_min_args bsd; 1262 1263 #ifdef DEBUG 1264 if (ldebug(sched_get_priority_min)) 1265 printf(ARGS(sched_get_priority_min, "%d"), args->policy); 1266 #endif 1267 1268 switch (args->policy) { 1269 case LINUX_SCHED_OTHER: 1270 bsd.policy = SCHED_OTHER; 1271 break; 1272 case LINUX_SCHED_FIFO: 1273 bsd.policy = SCHED_FIFO; 1274 break; 1275 case LINUX_SCHED_RR: 1276 bsd.policy = SCHED_RR; 1277 break; 1278 default: 1279 return EINVAL; 1280 } 1281 return sched_get_priority_min(td, &bsd); 1282 } 1283 1284 #define REBOOT_CAD_ON 0x89abcdef 1285 #define REBOOT_CAD_OFF 0 1286 #define REBOOT_HALT 0xcdef0123 1287 1288 int 1289 linux_reboot(struct thread *td, struct linux_reboot_args *args) 1290 { 1291 struct reboot_args bsd_args; 1292 1293 #ifdef DEBUG 1294 if (ldebug(reboot)) 1295 printf(ARGS(reboot, "0x%x"), args->cmd); 1296 #endif 1297 if (args->cmd == REBOOT_CAD_ON || args->cmd == REBOOT_CAD_OFF) 1298 return (0); 1299 bsd_args.opt = (args->cmd == REBOOT_HALT) ? RB_HALT : 0; 1300 return (reboot(td, &bsd_args)); 1301 } 1302 1303 #ifndef __alpha__ 1304 1305 /* 1306 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify 1307 * td->td_retval[1] when COMPAT_43 or COMPAT_SUNOS is defined. This 1308 * globbers registers that are assumed to be preserved. The following 1309 * lightweight syscalls fixes this. See also linux_getgid16() and 1310 * linux_getuid16() in linux_uid16.c. 1311 * 1312 * linux_getpid() - MP SAFE 1313 * linux_getgid() - MP SAFE 1314 * linux_getuid() - MP SAFE 1315 */ 1316 1317 int 1318 linux_getpid(struct thread *td, struct linux_getpid_args *args) 1319 { 1320 1321 td->td_retval[0] = td->td_proc->p_pid; 1322 return (0); 1323 } 1324 1325 int 1326 linux_getgid(struct thread *td, struct linux_getgid_args *args) 1327 { 1328 1329 td->td_retval[0] = td->td_ucred->cr_rgid; 1330 return (0); 1331 } 1332 1333 int 1334 linux_getuid(struct thread *td, struct linux_getuid_args *args) 1335 { 1336 1337 td->td_retval[0] = td->td_ucred->cr_ruid; 1338 return (0); 1339 } 1340 1341 #endif /*!__alpha__*/ 1342 1343 int 1344 linux_getsid(struct thread *td, struct linux_getsid_args *args) 1345 { 1346 struct getsid_args bsd; 1347 bsd.pid = args->pid; 1348 return getsid(td, &bsd); 1349 } 1350