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/jail.h> 38 #include <sys/kernel.h> 39 #include <sys/lock.h> 40 #include <sys/mman.h> 41 #include <sys/mount.h> 42 #include <sys/mutex.h> 43 #include <sys/namei.h> 44 #include <sys/poll.h> 45 #include <sys/proc.h> 46 #include <sys/blist.h> 47 #include <sys/reboot.h> 48 #include <sys/resourcevar.h> 49 #include <sys/signalvar.h> 50 #include <sys/stat.h> 51 #include <sys/sysctl.h> 52 #include <sys/sysproto.h> 53 #include <sys/time.h> 54 #include <sys/unistd.h> 55 #include <sys/vmmeter.h> 56 #include <sys/vnode.h> 57 #include <sys/wait.h> 58 59 #include <vm/vm.h> 60 #include <vm/pmap.h> 61 #include <vm/vm_kern.h> 62 #include <vm/vm_map.h> 63 #include <vm/vm_extern.h> 64 #include <vm/vm_object.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->td_proc); 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 /* 259 * XXX This code should make use of vn_open(), rather than doing 260 * all this stuff itself. 261 */ 262 NDINIT(&ni, LOOKUP, FOLLOW|LOCKLEAF, UIO_USERSPACE, args->library, td); 263 error = namei(&ni); 264 if (error) 265 goto cleanup; 266 267 vp = ni.ni_vp; 268 /* 269 * XXX - This looks like a bogus check. A LOCKLEAF namei should not 270 * succeed without returning a vnode. 271 */ 272 if (vp == NULL) { 273 error = ENOEXEC; /* ?? */ 274 goto cleanup; 275 } 276 NDFREE(&ni, NDF_ONLY_PNBUF); 277 278 /* 279 * From here on down, we have a locked vnode that must be unlocked. 280 */ 281 locked++; 282 283 /* Writable? */ 284 if (vp->v_writecount) { 285 error = ETXTBSY; 286 goto cleanup; 287 } 288 289 /* Executable? */ 290 error = VOP_GETATTR(vp, &attr, td->td_ucred, td); 291 if (error) 292 goto cleanup; 293 294 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || 295 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) { 296 error = ENOEXEC; 297 goto cleanup; 298 } 299 300 /* Sensible size? */ 301 if (attr.va_size == 0) { 302 error = ENOEXEC; 303 goto cleanup; 304 } 305 306 /* Can we access it? */ 307 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td); 308 if (error) 309 goto cleanup; 310 311 error = VOP_OPEN(vp, FREAD, td->td_ucred, td); 312 if (error) 313 goto cleanup; 314 315 /* 316 * Lock no longer needed 317 */ 318 VOP_UNLOCK(vp, 0, td); 319 locked = 0; 320 321 /* Pull in executable header into kernel_map */ 322 error = vm_mmap(kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE, 323 VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp, 0); 324 if (error) 325 goto cleanup; 326 327 /* Is it a Linux binary ? */ 328 if (((a_out->a_magic >> 16) & 0xff) != 0x64) { 329 error = ENOEXEC; 330 goto cleanup; 331 } 332 333 /* 334 * While we are here, we should REALLY do some more checks 335 */ 336 337 /* Set file/virtual offset based on a.out variant. */ 338 switch ((int)(a_out->a_magic & 0xffff)) { 339 case 0413: /* ZMAGIC */ 340 file_offset = 1024; 341 break; 342 case 0314: /* QMAGIC */ 343 file_offset = 0; 344 break; 345 default: 346 error = ENOEXEC; 347 goto cleanup; 348 } 349 350 bss_size = round_page(a_out->a_bss); 351 352 /* Check various fields in header for validity/bounds. */ 353 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) { 354 error = ENOEXEC; 355 goto cleanup; 356 } 357 358 /* text + data can't exceed file size */ 359 if (a_out->a_data + a_out->a_text > attr.va_size) { 360 error = EFAULT; 361 goto cleanup; 362 } 363 364 /* To protect td->td_proc->p_rlimit in the if condition. */ 365 mtx_assert(&Giant, MA_OWNED); 366 367 /* 368 * text/data/bss must not exceed limits 369 * XXX - this is not complete. it should check current usage PLUS 370 * the resources needed by this library. 371 */ 372 if (a_out->a_text > maxtsiz || 373 a_out->a_data + bss_size > 374 td->td_proc->p_rlimit[RLIMIT_DATA].rlim_cur) { 375 error = ENOMEM; 376 goto cleanup; 377 } 378 379 /* prevent more writers */ 380 vp->v_flag |= VTEXT; 381 382 /* 383 * Check if file_offset page aligned. Currently we cannot handle 384 * misalinged file offsets, and so we read in the entire image 385 * (what a waste). 386 */ 387 if (file_offset & PAGE_MASK) { 388 #ifdef DEBUG 389 printf("uselib: Non page aligned binary %lu\n", file_offset); 390 #endif 391 /* Map text+data read/write/execute */ 392 393 /* a_entry is the load address and is page aligned */ 394 vmaddr = trunc_page(a_out->a_entry); 395 396 /* get anon user mapping, read+write+execute */ 397 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0, 398 &vmaddr, a_out->a_text + a_out->a_data, FALSE, VM_PROT_ALL, 399 VM_PROT_ALL, 0); 400 if (error) 401 goto cleanup; 402 403 /* map file into kernel_map */ 404 error = vm_mmap(kernel_map, &buffer, 405 round_page(a_out->a_text + a_out->a_data + file_offset), 406 VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp, 407 trunc_page(file_offset)); 408 if (error) 409 goto cleanup; 410 411 /* copy from kernel VM space to user space */ 412 error = copyout((caddr_t)(uintptr_t)(buffer + file_offset), 413 (caddr_t)vmaddr, a_out->a_text + a_out->a_data); 414 415 /* release temporary kernel space */ 416 vm_map_remove(kernel_map, buffer, buffer + 417 round_page(a_out->a_text + a_out->a_data + file_offset)); 418 419 if (error) 420 goto cleanup; 421 } else { 422 #ifdef DEBUG 423 printf("uselib: Page aligned binary %lu\n", file_offset); 424 #endif 425 /* 426 * for QMAGIC, a_entry is 20 bytes beyond the load address 427 * to skip the executable header 428 */ 429 vmaddr = trunc_page(a_out->a_entry); 430 431 /* 432 * Map it all into the process's space as a single 433 * copy-on-write "data" segment. 434 */ 435 error = vm_mmap(&td->td_proc->p_vmspace->vm_map, &vmaddr, 436 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL, 437 MAP_PRIVATE | MAP_FIXED, (caddr_t)vp, file_offset); 438 if (error) 439 goto cleanup; 440 } 441 #ifdef DEBUG 442 printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long*)vmaddr)[0], 443 ((long*)vmaddr)[1]); 444 #endif 445 if (bss_size != 0) { 446 /* Calculate BSS start address */ 447 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text + 448 a_out->a_data; 449 450 /* allocate some 'anon' space */ 451 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0, 452 &vmaddr, bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0); 453 if (error) 454 goto cleanup; 455 } 456 457 cleanup: 458 /* Unlock vnode if needed */ 459 if (locked) 460 VOP_UNLOCK(vp, 0, td); 461 462 /* Release the kernel mapping. */ 463 if (a_out) 464 vm_map_remove(kernel_map, (vm_offset_t)a_out, 465 (vm_offset_t)a_out + PAGE_SIZE); 466 467 return error; 468 } 469 470 int 471 linux_select(struct thread *td, struct linux_select_args *args) 472 { 473 struct select_args bsa; 474 struct timeval tv0, tv1, utv, *tvp; 475 caddr_t sg; 476 int error; 477 478 #ifdef DEBUG 479 if (ldebug(select)) 480 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds, 481 (void *)args->readfds, (void *)args->writefds, 482 (void *)args->exceptfds, (void *)args->timeout); 483 #endif 484 485 error = 0; 486 bsa.nd = args->nfds; 487 bsa.in = args->readfds; 488 bsa.ou = args->writefds; 489 bsa.ex = args->exceptfds; 490 bsa.tv = (struct timeval *)args->timeout; 491 492 /* 493 * Store current time for computation of the amount of 494 * time left. 495 */ 496 if (args->timeout) { 497 if ((error = copyin((caddr_t)args->timeout, &utv, 498 sizeof(utv)))) 499 goto select_out; 500 #ifdef DEBUG 501 if (ldebug(select)) 502 printf(LMSG("incoming timeout (%ld/%ld)"), 503 utv.tv_sec, utv.tv_usec); 504 #endif 505 506 if (itimerfix(&utv)) { 507 /* 508 * The timeval was invalid. Convert it to something 509 * valid that will act as it does under Linux. 510 */ 511 sg = stackgap_init(); 512 tvp = stackgap_alloc(&sg, sizeof(utv)); 513 utv.tv_sec += utv.tv_usec / 1000000; 514 utv.tv_usec %= 1000000; 515 if (utv.tv_usec < 0) { 516 utv.tv_sec -= 1; 517 utv.tv_usec += 1000000; 518 } 519 if (utv.tv_sec < 0) 520 timevalclear(&utv); 521 if ((error = copyout(&utv, tvp, sizeof(utv)))) 522 goto select_out; 523 bsa.tv = tvp; 524 } 525 microtime(&tv0); 526 } 527 528 error = select(td, &bsa); 529 #ifdef DEBUG 530 if (ldebug(select)) 531 printf(LMSG("real select returns %d"), error); 532 #endif 533 if (error) { 534 /* 535 * See fs/select.c in the Linux kernel. Without this, 536 * Maelstrom doesn't work. 537 */ 538 if (error == ERESTART) 539 error = EINTR; 540 goto select_out; 541 } 542 543 if (args->timeout) { 544 if (td->td_retval[0]) { 545 /* 546 * Compute how much time was left of the timeout, 547 * by subtracting the current time and the time 548 * before we started the call, and subtracting 549 * that result from the user-supplied value. 550 */ 551 microtime(&tv1); 552 timevalsub(&tv1, &tv0); 553 timevalsub(&utv, &tv1); 554 if (utv.tv_sec < 0) 555 timevalclear(&utv); 556 } else 557 timevalclear(&utv); 558 #ifdef DEBUG 559 if (ldebug(select)) 560 printf(LMSG("outgoing timeout (%ld/%ld)"), 561 utv.tv_sec, utv.tv_usec); 562 #endif 563 if ((error = copyout(&utv, (caddr_t)args->timeout, 564 sizeof(utv)))) 565 goto select_out; 566 } 567 568 select_out: 569 #ifdef DEBUG 570 if (ldebug(select)) 571 printf(LMSG("select_out -> %d"), error); 572 #endif 573 return error; 574 } 575 576 int 577 linux_mremap(struct thread *td, struct linux_mremap_args *args) 578 { 579 struct munmap_args /* { 580 void *addr; 581 size_t len; 582 } */ bsd_args; 583 int error = 0; 584 585 #ifdef DEBUG 586 if (ldebug(mremap)) 587 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"), 588 (void *)args->addr, 589 (unsigned long)args->old_len, 590 (unsigned long)args->new_len, 591 (unsigned long)args->flags); 592 #endif 593 args->new_len = round_page(args->new_len); 594 args->old_len = round_page(args->old_len); 595 596 if (args->new_len > args->old_len) { 597 td->td_retval[0] = 0; 598 return ENOMEM; 599 } 600 601 if (args->new_len < args->old_len) { 602 bsd_args.addr = (caddr_t)(args->addr + args->new_len); 603 bsd_args.len = args->old_len - args->new_len; 604 error = munmap(td, &bsd_args); 605 } 606 607 td->td_retval[0] = error ? 0 : (u_long)args->addr; 608 return error; 609 } 610 611 int 612 linux_msync(struct thread *td, struct linux_msync_args *args) 613 { 614 struct msync_args bsd_args; 615 616 bsd_args.addr = (caddr_t)args->addr; 617 bsd_args.len = args->len; 618 bsd_args.flags = 0; /* XXX ignore */ 619 620 return msync(td, &bsd_args); 621 } 622 623 #ifndef __alpha__ 624 int 625 linux_time(struct thread *td, struct linux_time_args *args) 626 { 627 struct timeval tv; 628 l_time_t tm; 629 int error; 630 631 #ifdef DEBUG 632 if (ldebug(time)) 633 printf(ARGS(time, "*")); 634 #endif 635 636 microtime(&tv); 637 tm = tv.tv_sec; 638 if (args->tm && (error = copyout(&tm, (caddr_t)args->tm, sizeof(tm)))) 639 return error; 640 td->td_retval[0] = tm; 641 return 0; 642 } 643 #endif /*!__alpha__*/ 644 645 struct l_times_argv { 646 l_long tms_utime; 647 l_long tms_stime; 648 l_long tms_cutime; 649 l_long tms_cstime; 650 }; 651 652 #ifdef __alpha__ 653 #define CLK_TCK 1024 /* Linux uses 1024 on alpha */ 654 #else 655 #define CLK_TCK 100 /* Linux uses 100 */ 656 #endif 657 658 #define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK)) 659 660 int 661 linux_times(struct thread *td, struct linux_times_args *args) 662 { 663 struct timeval tv; 664 struct l_times_argv tms; 665 struct rusage ru; 666 int error; 667 668 #ifdef DEBUG 669 if (ldebug(times)) 670 printf(ARGS(times, "*")); 671 #endif 672 673 mtx_lock_spin(&sched_lock); 674 calcru(td->td_proc, &ru.ru_utime, &ru.ru_stime, NULL); 675 mtx_unlock_spin(&sched_lock); 676 677 tms.tms_utime = CONVTCK(ru.ru_utime); 678 tms.tms_stime = CONVTCK(ru.ru_stime); 679 680 tms.tms_cutime = CONVTCK(td->td_proc->p_stats->p_cru.ru_utime); 681 tms.tms_cstime = CONVTCK(td->td_proc->p_stats->p_cru.ru_stime); 682 683 if ((error = copyout(&tms, (caddr_t)args->buf, sizeof(tms)))) 684 return error; 685 686 microuptime(&tv); 687 td->td_retval[0] = (int)CONVTCK(tv); 688 return 0; 689 } 690 691 int 692 linux_newuname(struct thread *td, struct linux_newuname_args *args) 693 { 694 struct l_new_utsname utsname; 695 char osname[LINUX_MAX_UTSNAME]; 696 char osrelease[LINUX_MAX_UTSNAME]; 697 698 #ifdef DEBUG 699 if (ldebug(newuname)) 700 printf(ARGS(newuname, "*")); 701 #endif 702 703 linux_get_osname(td->td_proc, osname); 704 linux_get_osrelease(td->td_proc, osrelease); 705 706 bzero(&utsname, sizeof(utsname)); 707 strncpy(utsname.sysname, osname, LINUX_MAX_UTSNAME-1); 708 getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME-1); 709 strncpy(utsname.release, osrelease, LINUX_MAX_UTSNAME-1); 710 strncpy(utsname.version, version, LINUX_MAX_UTSNAME-1); 711 strncpy(utsname.machine, machine, LINUX_MAX_UTSNAME-1); 712 strncpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME-1); 713 714 return (copyout(&utsname, (caddr_t)args->buf, sizeof(utsname))); 715 } 716 717 #if defined(__i386__) 718 struct l_utimbuf { 719 l_time_t l_actime; 720 l_time_t l_modtime; 721 }; 722 723 int 724 linux_utime(struct thread *td, struct linux_utime_args *args) 725 { 726 struct utimes_args /* { 727 char *path; 728 struct timeval *tptr; 729 } */ bsdutimes; 730 struct timeval tv[2], *tvp; 731 struct l_utimbuf lut; 732 int error; 733 caddr_t sg; 734 735 sg = stackgap_init(); 736 CHECKALTEXIST(td, &sg, args->fname); 737 738 #ifdef DEBUG 739 if (ldebug(utime)) 740 printf(ARGS(utime, "%s, *"), args->fname); 741 #endif 742 743 if (args->times) { 744 if ((error = copyin((caddr_t)args->times, &lut, sizeof lut))) 745 return error; 746 tv[0].tv_sec = lut.l_actime; 747 tv[0].tv_usec = 0; 748 tv[1].tv_sec = lut.l_modtime; 749 tv[1].tv_usec = 0; 750 /* so that utimes can copyin */ 751 tvp = (struct timeval *)stackgap_alloc(&sg, sizeof(tv)); 752 if (tvp == NULL) 753 return (ENAMETOOLONG); 754 if ((error = copyout(tv, tvp, sizeof(tv)))) 755 return error; 756 bsdutimes.tptr = tvp; 757 } else 758 bsdutimes.tptr = NULL; 759 760 bsdutimes.path = args->fname; 761 return utimes(td, &bsdutimes); 762 } 763 #endif /* __i386__ */ 764 765 #define __WCLONE 0x80000000 766 767 #ifndef __alpha__ 768 int 769 linux_waitpid(struct thread *td, struct linux_waitpid_args *args) 770 { 771 struct wait_args /* { 772 int pid; 773 int *status; 774 int options; 775 struct rusage *rusage; 776 } */ tmp; 777 int error, tmpstat; 778 779 #ifdef DEBUG 780 if (ldebug(waitpid)) 781 printf(ARGS(waitpid, "%d, %p, %d"), 782 args->pid, (void *)args->status, args->options); 783 #endif 784 785 tmp.pid = args->pid; 786 tmp.status = args->status; 787 tmp.options = (args->options & (WNOHANG | WUNTRACED)); 788 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */ 789 if (args->options & __WCLONE) 790 tmp.options |= WLINUXCLONE; 791 tmp.rusage = NULL; 792 793 if ((error = wait4(td, &tmp)) != 0) 794 return error; 795 796 if (args->status) { 797 if ((error = copyin((caddr_t)args->status, &tmpstat, 798 sizeof(int))) != 0) 799 return error; 800 tmpstat &= 0xffff; 801 if (WIFSIGNALED(tmpstat)) 802 tmpstat = (tmpstat & 0xffffff80) | 803 BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat)); 804 else if (WIFSTOPPED(tmpstat)) 805 tmpstat = (tmpstat & 0xffff00ff) | 806 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8); 807 return copyout(&tmpstat, (caddr_t)args->status, sizeof(int)); 808 } 809 810 return 0; 811 } 812 #endif /*!__alpha__*/ 813 814 int 815 linux_wait4(struct thread *td, struct linux_wait4_args *args) 816 { 817 struct wait_args /* { 818 int pid; 819 int *status; 820 int options; 821 struct rusage *rusage; 822 } */ tmp; 823 int error, tmpstat; 824 825 #ifdef DEBUG 826 if (ldebug(wait4)) 827 printf(ARGS(wait4, "%d, %p, %d, %p"), 828 args->pid, (void *)args->status, args->options, 829 (void *)args->rusage); 830 #endif 831 832 tmp.pid = args->pid; 833 tmp.status = args->status; 834 tmp.options = (args->options & (WNOHANG | WUNTRACED)); 835 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */ 836 if (args->options & __WCLONE) 837 tmp.options |= WLINUXCLONE; 838 tmp.rusage = (struct rusage *)args->rusage; 839 840 if ((error = wait4(td, &tmp)) != 0) 841 return error; 842 843 SIGDELSET(td->td_proc->p_siglist, SIGCHLD); 844 845 if (args->status) { 846 if ((error = copyin((caddr_t)args->status, &tmpstat, 847 sizeof(int))) != 0) 848 return error; 849 tmpstat &= 0xffff; 850 if (WIFSIGNALED(tmpstat)) 851 tmpstat = (tmpstat & 0xffffff80) | 852 BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat)); 853 else if (WIFSTOPPED(tmpstat)) 854 tmpstat = (tmpstat & 0xffff00ff) | 855 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8); 856 return copyout(&tmpstat, (caddr_t)args->status, sizeof(int)); 857 } 858 859 return 0; 860 } 861 862 int 863 linux_mknod(struct thread *td, struct linux_mknod_args *args) 864 { 865 caddr_t sg; 866 struct mknod_args bsd_mknod; 867 struct mkfifo_args bsd_mkfifo; 868 869 sg = stackgap_init(); 870 871 CHECKALTCREAT(td, &sg, args->path); 872 873 #ifdef DEBUG 874 if (ldebug(mknod)) 875 printf(ARGS(mknod, "%s, %d, %d"), 876 args->path, args->mode, args->dev); 877 #endif 878 879 if (args->mode & S_IFIFO) { 880 bsd_mkfifo.path = args->path; 881 bsd_mkfifo.mode = args->mode; 882 return mkfifo(td, &bsd_mkfifo); 883 } else { 884 bsd_mknod.path = args->path; 885 bsd_mknod.mode = args->mode; 886 bsd_mknod.dev = args->dev; 887 return mknod(td, &bsd_mknod); 888 } 889 } 890 891 /* 892 * UGH! This is just about the dumbest idea I've ever heard!! 893 */ 894 int 895 linux_personality(struct thread *td, struct linux_personality_args *args) 896 { 897 #ifdef DEBUG 898 if (ldebug(personality)) 899 printf(ARGS(personality, "%d"), args->per); 900 #endif 901 #ifndef __alpha__ 902 if (args->per != 0) 903 return EINVAL; 904 #endif 905 906 /* Yes Jim, it's still a Linux... */ 907 td->td_retval[0] = 0; 908 return 0; 909 } 910 911 /* 912 * Wrappers for get/setitimer for debugging.. 913 */ 914 int 915 linux_setitimer(struct thread *td, struct linux_setitimer_args *args) 916 { 917 struct setitimer_args bsa; 918 struct itimerval foo; 919 int error; 920 921 #ifdef DEBUG 922 if (ldebug(setitimer)) 923 printf(ARGS(setitimer, "%p, %p"), 924 (void *)args->itv, (void *)args->oitv); 925 #endif 926 bsa.which = args->which; 927 bsa.itv = (struct itimerval *)args->itv; 928 bsa.oitv = (struct itimerval *)args->oitv; 929 if (args->itv) { 930 if ((error = copyin((caddr_t)args->itv, &foo, sizeof(foo)))) 931 return error; 932 #ifdef DEBUG 933 if (ldebug(setitimer)) { 934 printf("setitimer: value: sec: %ld, usec: %ld\n", 935 foo.it_value.tv_sec, foo.it_value.tv_usec); 936 printf("setitimer: interval: sec: %ld, usec: %ld\n", 937 foo.it_interval.tv_sec, foo.it_interval.tv_usec); 938 } 939 #endif 940 } 941 return setitimer(td, &bsa); 942 } 943 944 int 945 linux_getitimer(struct thread *td, struct linux_getitimer_args *args) 946 { 947 struct getitimer_args bsa; 948 #ifdef DEBUG 949 if (ldebug(getitimer)) 950 printf(ARGS(getitimer, "%p"), (void *)args->itv); 951 #endif 952 bsa.which = args->which; 953 bsa.itv = (struct itimerval *)args->itv; 954 return getitimer(td, &bsa); 955 } 956 957 #ifndef __alpha__ 958 int 959 linux_nice(struct thread *td, struct linux_nice_args *args) 960 { 961 struct setpriority_args bsd_args; 962 963 bsd_args.which = PRIO_PROCESS; 964 bsd_args.who = 0; /* current process */ 965 bsd_args.prio = args->inc; 966 return setpriority(td, &bsd_args); 967 } 968 #endif /*!__alpha__*/ 969 970 int 971 linux_setgroups(struct thread *td, struct linux_setgroups_args *args) 972 { 973 struct ucred *newcred, *oldcred; 974 l_gid_t linux_gidset[NGROUPS]; 975 gid_t *bsd_gidset; 976 int ngrp, error; 977 struct proc *p; 978 979 ngrp = args->gidsetsize; 980 if (ngrp >= NGROUPS) 981 return (EINVAL); 982 error = copyin((caddr_t)args->grouplist, linux_gidset, 983 ngrp * sizeof(l_gid_t)); 984 if (error) 985 return (error); 986 newcred = crget(); 987 p = td->td_proc; 988 PROC_LOCK(p); 989 oldcred = p->p_ucred; 990 991 /* 992 * cr_groups[0] holds egid. Setting the whole set from 993 * the supplied set will cause egid to be changed too. 994 * Keep cr_groups[0] unchanged to prevent that. 995 */ 996 997 if ((error = suser_cred(oldcred, PRISON_ROOT)) != 0) { 998 PROC_UNLOCK(p); 999 crfree(newcred); 1000 return (error); 1001 } 1002 1003 crcopy(newcred, oldcred); 1004 if (ngrp > 0) { 1005 newcred->cr_ngroups = ngrp + 1; 1006 1007 bsd_gidset = newcred->cr_groups; 1008 ngrp--; 1009 while (ngrp >= 0) { 1010 bsd_gidset[ngrp + 1] = linux_gidset[ngrp]; 1011 ngrp--; 1012 } 1013 } 1014 else 1015 newcred->cr_ngroups = 1; 1016 1017 setsugid(p); 1018 p->p_ucred = newcred; 1019 PROC_UNLOCK(p); 1020 crfree(oldcred); 1021 return (0); 1022 } 1023 1024 int 1025 linux_getgroups(struct thread *td, struct linux_getgroups_args *args) 1026 { 1027 struct ucred *cred; 1028 l_gid_t linux_gidset[NGROUPS]; 1029 gid_t *bsd_gidset; 1030 int bsd_gidsetsz, ngrp, error; 1031 1032 cred = td->td_ucred; 1033 bsd_gidset = cred->cr_groups; 1034 bsd_gidsetsz = cred->cr_ngroups - 1; 1035 1036 /* 1037 * cr_groups[0] holds egid. Returning the whole set 1038 * here will cause a duplicate. Exclude cr_groups[0] 1039 * to prevent that. 1040 */ 1041 1042 if ((ngrp = args->gidsetsize) == 0) { 1043 td->td_retval[0] = bsd_gidsetsz; 1044 return (0); 1045 } 1046 1047 if (ngrp < bsd_gidsetsz) 1048 return (EINVAL); 1049 1050 ngrp = 0; 1051 while (ngrp < bsd_gidsetsz) { 1052 linux_gidset[ngrp] = bsd_gidset[ngrp + 1]; 1053 ngrp++; 1054 } 1055 1056 if ((error = copyout(linux_gidset, (caddr_t)args->grouplist, 1057 ngrp * sizeof(l_gid_t)))) 1058 return (error); 1059 1060 td->td_retval[0] = ngrp; 1061 return (0); 1062 } 1063 1064 #ifndef __alpha__ 1065 int 1066 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args) 1067 { 1068 struct __setrlimit_args bsd; 1069 struct l_rlimit rlim; 1070 int error; 1071 caddr_t sg = stackgap_init(); 1072 1073 #ifdef DEBUG 1074 if (ldebug(setrlimit)) 1075 printf(ARGS(setrlimit, "%d, %p"), 1076 args->resource, (void *)args->rlim); 1077 #endif 1078 1079 if (args->resource >= LINUX_RLIM_NLIMITS) 1080 return (EINVAL); 1081 1082 bsd.which = linux_to_bsd_resource[args->resource]; 1083 if (bsd.which == -1) 1084 return (EINVAL); 1085 1086 error = copyin((caddr_t)args->rlim, &rlim, sizeof(rlim)); 1087 if (error) 1088 return (error); 1089 1090 bsd.rlp = stackgap_alloc(&sg, sizeof(struct rlimit)); 1091 bsd.rlp->rlim_cur = (rlim_t)rlim.rlim_cur; 1092 bsd.rlp->rlim_max = (rlim_t)rlim.rlim_max; 1093 return (setrlimit(td, &bsd)); 1094 } 1095 1096 int 1097 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args) 1098 { 1099 struct __getrlimit_args bsd; 1100 struct l_rlimit rlim; 1101 int error; 1102 caddr_t sg = stackgap_init(); 1103 1104 #ifdef DEBUG 1105 if (ldebug(old_getrlimit)) 1106 printf(ARGS(old_getrlimit, "%d, %p"), 1107 args->resource, (void *)args->rlim); 1108 #endif 1109 1110 if (args->resource >= LINUX_RLIM_NLIMITS) 1111 return (EINVAL); 1112 1113 bsd.which = linux_to_bsd_resource[args->resource]; 1114 if (bsd.which == -1) 1115 return (EINVAL); 1116 1117 bsd.rlp = stackgap_alloc(&sg, sizeof(struct rlimit)); 1118 error = getrlimit(td, &bsd); 1119 if (error) 1120 return (error); 1121 1122 rlim.rlim_cur = (unsigned long)bsd.rlp->rlim_cur; 1123 if (rlim.rlim_cur == ULONG_MAX) 1124 rlim.rlim_cur = LONG_MAX; 1125 rlim.rlim_max = (unsigned long)bsd.rlp->rlim_max; 1126 if (rlim.rlim_max == ULONG_MAX) 1127 rlim.rlim_max = LONG_MAX; 1128 return (copyout(&rlim, (caddr_t)args->rlim, sizeof(rlim))); 1129 } 1130 1131 int 1132 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args) 1133 { 1134 struct __getrlimit_args bsd; 1135 struct l_rlimit rlim; 1136 int error; 1137 caddr_t sg = stackgap_init(); 1138 1139 #ifdef DEBUG 1140 if (ldebug(getrlimit)) 1141 printf(ARGS(getrlimit, "%d, %p"), 1142 args->resource, (void *)args->rlim); 1143 #endif 1144 1145 if (args->resource >= LINUX_RLIM_NLIMITS) 1146 return (EINVAL); 1147 1148 bsd.which = linux_to_bsd_resource[args->resource]; 1149 if (bsd.which == -1) 1150 return (EINVAL); 1151 1152 bsd.rlp = stackgap_alloc(&sg, sizeof(struct rlimit)); 1153 error = getrlimit(td, &bsd); 1154 if (error) 1155 return (error); 1156 1157 rlim.rlim_cur = (l_ulong)bsd.rlp->rlim_cur; 1158 rlim.rlim_max = (l_ulong)bsd.rlp->rlim_max; 1159 return (copyout(&rlim, (caddr_t)args->rlim, sizeof(rlim))); 1160 } 1161 #endif /*!__alpha__*/ 1162 1163 int 1164 linux_sched_setscheduler(struct thread *td, 1165 struct linux_sched_setscheduler_args *args) 1166 { 1167 struct sched_setscheduler_args bsd; 1168 1169 #ifdef DEBUG 1170 if (ldebug(sched_setscheduler)) 1171 printf(ARGS(sched_setscheduler, "%d, %d, %p"), 1172 args->pid, args->policy, (const void *)args->param); 1173 #endif 1174 1175 switch (args->policy) { 1176 case LINUX_SCHED_OTHER: 1177 bsd.policy = SCHED_OTHER; 1178 break; 1179 case LINUX_SCHED_FIFO: 1180 bsd.policy = SCHED_FIFO; 1181 break; 1182 case LINUX_SCHED_RR: 1183 bsd.policy = SCHED_RR; 1184 break; 1185 default: 1186 return EINVAL; 1187 } 1188 1189 bsd.pid = args->pid; 1190 bsd.param = (struct sched_param *)args->param; 1191 return sched_setscheduler(td, &bsd); 1192 } 1193 1194 int 1195 linux_sched_getscheduler(struct thread *td, 1196 struct linux_sched_getscheduler_args *args) 1197 { 1198 struct sched_getscheduler_args bsd; 1199 int error; 1200 1201 #ifdef DEBUG 1202 if (ldebug(sched_getscheduler)) 1203 printf(ARGS(sched_getscheduler, "%d"), args->pid); 1204 #endif 1205 1206 bsd.pid = args->pid; 1207 error = sched_getscheduler(td, &bsd); 1208 1209 switch (td->td_retval[0]) { 1210 case SCHED_OTHER: 1211 td->td_retval[0] = LINUX_SCHED_OTHER; 1212 break; 1213 case SCHED_FIFO: 1214 td->td_retval[0] = LINUX_SCHED_FIFO; 1215 break; 1216 case SCHED_RR: 1217 td->td_retval[0] = LINUX_SCHED_RR; 1218 break; 1219 } 1220 1221 return error; 1222 } 1223 1224 int 1225 linux_sched_get_priority_max(struct thread *td, 1226 struct linux_sched_get_priority_max_args *args) 1227 { 1228 struct sched_get_priority_max_args bsd; 1229 1230 #ifdef DEBUG 1231 if (ldebug(sched_get_priority_max)) 1232 printf(ARGS(sched_get_priority_max, "%d"), args->policy); 1233 #endif 1234 1235 switch (args->policy) { 1236 case LINUX_SCHED_OTHER: 1237 bsd.policy = SCHED_OTHER; 1238 break; 1239 case LINUX_SCHED_FIFO: 1240 bsd.policy = SCHED_FIFO; 1241 break; 1242 case LINUX_SCHED_RR: 1243 bsd.policy = SCHED_RR; 1244 break; 1245 default: 1246 return EINVAL; 1247 } 1248 return sched_get_priority_max(td, &bsd); 1249 } 1250 1251 int 1252 linux_sched_get_priority_min(struct thread *td, 1253 struct linux_sched_get_priority_min_args *args) 1254 { 1255 struct sched_get_priority_min_args bsd; 1256 1257 #ifdef DEBUG 1258 if (ldebug(sched_get_priority_min)) 1259 printf(ARGS(sched_get_priority_min, "%d"), args->policy); 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 return sched_get_priority_min(td, &bsd); 1276 } 1277 1278 #define REBOOT_CAD_ON 0x89abcdef 1279 #define REBOOT_CAD_OFF 0 1280 #define REBOOT_HALT 0xcdef0123 1281 1282 int 1283 linux_reboot(struct thread *td, struct linux_reboot_args *args) 1284 { 1285 struct reboot_args bsd_args; 1286 1287 #ifdef DEBUG 1288 if (ldebug(reboot)) 1289 printf(ARGS(reboot, "0x%x"), args->cmd); 1290 #endif 1291 if (args->cmd == REBOOT_CAD_ON || args->cmd == REBOOT_CAD_OFF) 1292 return (0); 1293 bsd_args.opt = (args->cmd == REBOOT_HALT) ? RB_HALT : 0; 1294 return (reboot(td, &bsd_args)); 1295 } 1296 1297 #ifndef __alpha__ 1298 1299 /* 1300 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify 1301 * td->td_retval[1] when COMPAT_43 or COMPAT_SUNOS is defined. This 1302 * globbers registers that are assumed to be preserved. The following 1303 * lightweight syscalls fixes this. See also linux_getgid16() and 1304 * linux_getuid16() in linux_uid16.c. 1305 * 1306 * linux_getpid() - MP SAFE 1307 * linux_getgid() - MP SAFE 1308 * linux_getuid() - MP SAFE 1309 */ 1310 1311 int 1312 linux_getpid(struct thread *td, struct linux_getpid_args *args) 1313 { 1314 1315 td->td_retval[0] = td->td_proc->p_pid; 1316 return (0); 1317 } 1318 1319 int 1320 linux_getgid(struct thread *td, struct linux_getgid_args *args) 1321 { 1322 1323 td->td_retval[0] = td->td_ucred->cr_rgid; 1324 return (0); 1325 } 1326 1327 int 1328 linux_getuid(struct thread *td, struct linux_getuid_args *args) 1329 { 1330 1331 td->td_retval[0] = td->td_ucred->cr_ruid; 1332 return (0); 1333 } 1334 1335 #endif /*!__alpha__*/ 1336 1337 int 1338 linux_getsid(struct thread *td, struct linux_getsid_args *args) 1339 { 1340 struct getsid_args bsd; 1341 bsd.pid = args->pid; 1342 return getsid(td, &bsd); 1343 } 1344