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