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