1 /* $NetBSD: linux_misc.c,v 1.114 2002/11/25 02:11:23 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 1995, 1998, 1999 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Frank van der Linden and Eric Haszlakiewicz; by Jason R. Thorpe 9 * of the Numerical Aerospace Simulation Facility, NASA Ames Research Center. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 /* 41 * Linux compatibility module. Try to deal with various Linux system calls. 42 */ 43 44 /* 45 * These functions have been moved to multiarch to allow 46 * selection of which machines include them to be 47 * determined by the individual files.linux_<arch> files. 48 * 49 * Function in multiarch: 50 * linux_sys_break : linux_break.c 51 * linux_sys_alarm : linux_misc_notalpha.c 52 * linux_sys_getresgid : linux_misc_notalpha.c 53 * linux_sys_nice : linux_misc_notalpha.c 54 * linux_sys_readdir : linux_misc_notalpha.c 55 * linux_sys_setresgid : linux_misc_notalpha.c 56 * linux_sys_time : linux_misc_notalpha.c 57 * linux_sys_utime : linux_misc_notalpha.c 58 * linux_sys_waitpid : linux_misc_notalpha.c 59 * linux_sys_old_mmap : linux_oldmmap.c 60 * linux_sys_oldolduname : linux_oldolduname.c 61 * linux_sys_oldselect : linux_oldselect.c 62 * linux_sys_olduname : linux_olduname.c 63 * linux_sys_pipe : linux_pipe.c 64 */ 65 66 #include <sys/cdefs.h> 67 __KERNEL_RCSID(0, "$NetBSD: linux_misc.c,v 1.114 2002/11/25 02:11:23 thorpej Exp $"); 68 69 #include <sys/param.h> 70 #include <sys/systm.h> 71 #include <sys/namei.h> 72 #include <sys/proc.h> 73 #include <sys/dirent.h> 74 #include <sys/file.h> 75 #include <sys/stat.h> 76 #include <sys/filedesc.h> 77 #include <sys/ioctl.h> 78 #include <sys/kernel.h> 79 #include <sys/malloc.h> 80 #include <sys/mbuf.h> 81 #include <sys/mman.h> 82 #include <sys/mount.h> 83 #include <sys/reboot.h> 84 #include <sys/resource.h> 85 #include <sys/resourcevar.h> 86 #include <sys/signal.h> 87 #include <sys/signalvar.h> 88 #include <sys/socket.h> 89 #include <sys/time.h> 90 #include <sys/times.h> 91 #include <sys/vnode.h> 92 #include <sys/uio.h> 93 #include <sys/wait.h> 94 #include <sys/utsname.h> 95 #include <sys/unistd.h> 96 #include <sys/swap.h> /* for SWAP_ON */ 97 #include <sys/sysctl.h> /* for KERN_DOMAINNAME */ 98 99 #include <sys/ptrace.h> 100 #include <machine/ptrace.h> 101 102 #include <sys/syscallargs.h> 103 104 #include <compat/linux/common/linux_types.h> 105 #include <compat/linux/common/linux_signal.h> 106 107 #include <compat/linux/linux_syscallargs.h> 108 109 #include <compat/linux/common/linux_fcntl.h> 110 #include <compat/linux/common/linux_mmap.h> 111 #include <compat/linux/common/linux_dirent.h> 112 #include <compat/linux/common/linux_util.h> 113 #include <compat/linux/common/linux_misc.h> 114 #include <compat/linux/common/linux_ptrace.h> 115 #include <compat/linux/common/linux_reboot.h> 116 #include <compat/linux/common/linux_emuldata.h> 117 118 const int linux_ptrace_request_map[] = { 119 LINUX_PTRACE_TRACEME, PT_TRACE_ME, 120 LINUX_PTRACE_PEEKTEXT, PT_READ_I, 121 LINUX_PTRACE_PEEKDATA, PT_READ_D, 122 LINUX_PTRACE_POKETEXT, PT_WRITE_I, 123 LINUX_PTRACE_POKEDATA, PT_WRITE_D, 124 LINUX_PTRACE_CONT, PT_CONTINUE, 125 LINUX_PTRACE_KILL, PT_KILL, 126 LINUX_PTRACE_ATTACH, PT_ATTACH, 127 LINUX_PTRACE_DETACH, PT_DETACH, 128 #ifdef PT_STEP 129 LINUX_PTRACE_SINGLESTEP, PT_STEP, 130 #endif 131 -1 132 }; 133 134 static const struct mnttypes { 135 char *bsd; 136 int linux; 137 } fstypes[] = { 138 { MOUNT_FFS, LINUX_DEFAULT_SUPER_MAGIC }, 139 { MOUNT_NFS, LINUX_NFS_SUPER_MAGIC }, 140 { MOUNT_MFS, LINUX_DEFAULT_SUPER_MAGIC }, 141 { MOUNT_MSDOS, LINUX_MSDOS_SUPER_MAGIC }, 142 { MOUNT_LFS, LINUX_DEFAULT_SUPER_MAGIC }, 143 { MOUNT_FDESC, LINUX_DEFAULT_SUPER_MAGIC }, 144 { MOUNT_PORTAL, LINUX_DEFAULT_SUPER_MAGIC }, 145 { MOUNT_NULL, LINUX_DEFAULT_SUPER_MAGIC }, 146 { MOUNT_OVERLAY, LINUX_DEFAULT_SUPER_MAGIC }, 147 { MOUNT_UMAP, LINUX_DEFAULT_SUPER_MAGIC }, 148 { MOUNT_KERNFS, LINUX_DEFAULT_SUPER_MAGIC }, 149 { MOUNT_PROCFS, LINUX_PROC_SUPER_MAGIC }, 150 { MOUNT_AFS, LINUX_DEFAULT_SUPER_MAGIC }, 151 { MOUNT_CD9660, LINUX_ISOFS_SUPER_MAGIC }, 152 { MOUNT_UNION, LINUX_DEFAULT_SUPER_MAGIC }, 153 { MOUNT_ADOSFS, LINUX_ADFS_SUPER_MAGIC }, 154 { MOUNT_EXT2FS, LINUX_EXT2_SUPER_MAGIC }, 155 { MOUNT_CFS, LINUX_DEFAULT_SUPER_MAGIC }, 156 { MOUNT_CODA, LINUX_CODA_SUPER_MAGIC }, 157 { MOUNT_FILECORE, LINUX_DEFAULT_SUPER_MAGIC }, 158 { MOUNT_NTFS, LINUX_DEFAULT_SUPER_MAGIC }, 159 { MOUNT_SMBFS, LINUX_SMB_SUPER_MAGIC } 160 }; 161 #define FSTYPESSIZE (sizeof(fstypes) / sizeof(fstypes[0])) 162 163 #ifdef DEBUG_LINUX 164 #define DPRINTF(a) uprintf a 165 #else 166 #define DPRINTF(a) 167 #endif 168 169 /* Local linux_misc.c functions: */ 170 static void bsd_to_linux_statfs __P((struct statfs *, struct linux_statfs *)); 171 static int linux_to_bsd_limit __P((int)); 172 173 /* 174 * The information on a terminated (or stopped) process needs 175 * to be converted in order for Linux binaries to get a valid signal 176 * number out of it. 177 */ 178 void 179 bsd_to_linux_wstat(st) 180 int *st; 181 { 182 183 int sig; 184 185 if (WIFSIGNALED(*st)) { 186 sig = WTERMSIG(*st); 187 if (sig >= 0 && sig < NSIG) 188 *st= (*st& ~0177) | native_to_linux_signo[sig]; 189 } else if (WIFSTOPPED(*st)) { 190 sig = WSTOPSIG(*st); 191 if (sig >= 0 && sig < NSIG) 192 *st = (*st & ~0xff00) | 193 (native_to_linux_signo[sig] << 8); 194 } 195 } 196 197 /* 198 * This is very much the same as waitpid() 199 */ 200 int 201 linux_sys_wait4(p, v, retval) 202 struct proc *p; 203 void *v; 204 register_t *retval; 205 { 206 struct linux_sys_wait4_args /* { 207 syscallarg(int) pid; 208 syscallarg(int *) status; 209 syscallarg(int) options; 210 syscallarg(struct rusage *) rusage; 211 } */ *uap = v; 212 struct sys_wait4_args w4a; 213 int error, *status, tstat, options, linux_options; 214 caddr_t sg; 215 216 if (SCARG(uap, status) != NULL) { 217 sg = stackgap_init(p, 0); 218 status = (int *) stackgap_alloc(p, &sg, sizeof *status); 219 } else 220 status = NULL; 221 222 linux_options = SCARG(uap, options); 223 options = 0; 224 if (linux_options & 225 ~(LINUX_WAIT4_WNOHANG|LINUX_WAIT4_WUNTRACED|LINUX_WAIT4_WALL| 226 LINUX_WAIT4_WCLONE)) 227 return (EINVAL); 228 229 if (linux_options & LINUX_WAIT4_WNOHANG) 230 options |= WNOHANG; 231 if (linux_options & LINUX_WAIT4_WUNTRACED) 232 options |= WUNTRACED; 233 if (linux_options & LINUX_WAIT4_WALL) 234 options |= WALLSIG; 235 if (linux_options & LINUX_WAIT4_WCLONE) 236 options |= WALTSIG; 237 238 SCARG(&w4a, pid) = SCARG(uap, pid); 239 SCARG(&w4a, status) = status; 240 SCARG(&w4a, options) = options; 241 SCARG(&w4a, rusage) = SCARG(uap, rusage); 242 243 if ((error = sys_wait4(p, &w4a, retval))) 244 return error; 245 246 sigdelset(&p->p_sigctx.ps_siglist, SIGCHLD); 247 248 if (status != NULL) { 249 if ((error = copyin(status, &tstat, sizeof tstat))) 250 return error; 251 252 bsd_to_linux_wstat(&tstat); 253 return copyout(&tstat, SCARG(uap, status), sizeof tstat); 254 } 255 256 return 0; 257 } 258 259 /* 260 * Linux brk(2). The check if the new address is >= the old one is 261 * done in the kernel in Linux. NetBSD does it in the library. 262 */ 263 int 264 linux_sys_brk(p, v, retval) 265 struct proc *p; 266 void *v; 267 register_t *retval; 268 { 269 struct linux_sys_brk_args /* { 270 syscallarg(char *) nsize; 271 } */ *uap = v; 272 char *nbrk = SCARG(uap, nsize); 273 struct sys_obreak_args oba; 274 struct vmspace *vm = p->p_vmspace; 275 struct linux_emuldata *ed = (struct linux_emuldata*)p->p_emuldata; 276 277 SCARG(&oba, nsize) = nbrk; 278 279 if ((caddr_t) nbrk > vm->vm_daddr && sys_obreak(p, &oba, retval) == 0) 280 ed->p_break = (char*)nbrk; 281 else 282 nbrk = ed->p_break; 283 284 retval[0] = (register_t)nbrk; 285 286 return 0; 287 } 288 289 /* 290 * Convert BSD statfs structure to Linux statfs structure. 291 * The Linux structure has less fields, and it also wants 292 * the length of a name in a dir entry in a field, which 293 * we fake (probably the wrong way). 294 */ 295 static void 296 bsd_to_linux_statfs(bsp, lsp) 297 struct statfs *bsp; 298 struct linux_statfs *lsp; 299 { 300 int i; 301 302 for (i = 0; i < FSTYPESSIZE; i++) 303 if (strcmp(bsp->f_fstypename, fstypes[i].bsd) == 0) 304 break; 305 306 if (i == FSTYPESSIZE) { 307 DPRINTF(("unhandled fstype in linux emulation: %s\n", 308 bsp->f_fstypename)); 309 lsp->l_ftype = LINUX_DEFAULT_SUPER_MAGIC; 310 } else { 311 lsp->l_ftype = fstypes[i].linux; 312 } 313 314 lsp->l_fbsize = bsp->f_bsize; 315 lsp->l_fblocks = bsp->f_blocks; 316 lsp->l_fbfree = bsp->f_bfree; 317 lsp->l_fbavail = bsp->f_bavail; 318 lsp->l_ffiles = bsp->f_files; 319 lsp->l_fffree = bsp->f_ffree; 320 /* Linux sets the fsid to 0..., we don't */ 321 lsp->l_ffsid.val[0] = bsp->f_fsid.val[0]; 322 lsp->l_ffsid.val[1] = bsp->f_fsid.val[1]; 323 lsp->l_fnamelen = MAXNAMLEN; /* XXX */ 324 (void)memset(lsp->l_fspare, 0, sizeof(lsp->l_fspare)); 325 } 326 327 /* 328 * Implement the fs stat functions. Straightforward. 329 */ 330 int 331 linux_sys_statfs(p, v, retval) 332 struct proc *p; 333 void *v; 334 register_t *retval; 335 { 336 struct linux_sys_statfs_args /* { 337 syscallarg(const char *) path; 338 syscallarg(struct linux_statfs *) sp; 339 } */ *uap = v; 340 struct statfs btmp, *bsp; 341 struct linux_statfs ltmp; 342 struct sys_statfs_args bsa; 343 caddr_t sg; 344 int error; 345 346 sg = stackgap_init(p, 0); 347 bsp = (struct statfs *) stackgap_alloc(p, &sg, sizeof (struct statfs)); 348 349 CHECK_ALT_EXIST(p, &sg, SCARG(uap, path)); 350 351 SCARG(&bsa, path) = SCARG(uap, path); 352 SCARG(&bsa, buf) = bsp; 353 354 if ((error = sys_statfs(p, &bsa, retval))) 355 return error; 356 357 if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp))) 358 return error; 359 360 bsd_to_linux_statfs(&btmp, <mp); 361 362 return copyout((caddr_t) <mp, (caddr_t) SCARG(uap, sp), sizeof ltmp); 363 } 364 365 int 366 linux_sys_fstatfs(p, v, retval) 367 struct proc *p; 368 void *v; 369 register_t *retval; 370 { 371 struct linux_sys_fstatfs_args /* { 372 syscallarg(int) fd; 373 syscallarg(struct linux_statfs *) sp; 374 } */ *uap = v; 375 struct statfs btmp, *bsp; 376 struct linux_statfs ltmp; 377 struct sys_fstatfs_args bsa; 378 caddr_t sg; 379 int error; 380 381 sg = stackgap_init(p, 0); 382 bsp = (struct statfs *) stackgap_alloc(p, &sg, sizeof (struct statfs)); 383 384 SCARG(&bsa, fd) = SCARG(uap, fd); 385 SCARG(&bsa, buf) = bsp; 386 387 if ((error = sys_fstatfs(p, &bsa, retval))) 388 return error; 389 390 if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp))) 391 return error; 392 393 bsd_to_linux_statfs(&btmp, <mp); 394 395 return copyout((caddr_t) <mp, (caddr_t) SCARG(uap, sp), sizeof ltmp); 396 } 397 398 /* 399 * uname(). Just copy the info from the various strings stored in the 400 * kernel, and put it in the Linux utsname structure. That structure 401 * is almost the same as the NetBSD one, only it has fields 65 characters 402 * long, and an extra domainname field. 403 */ 404 int 405 linux_sys_uname(p, v, retval) 406 struct proc *p; 407 void *v; 408 register_t *retval; 409 { 410 struct linux_sys_uname_args /* { 411 syscallarg(struct linux_utsname *) up; 412 } */ *uap = v; 413 struct linux_utsname luts; 414 415 strncpy(luts.l_sysname, linux_sysname, sizeof(luts.l_sysname)); 416 strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename)); 417 strncpy(luts.l_release, linux_release, sizeof(luts.l_release)); 418 strncpy(luts.l_version, linux_version, sizeof(luts.l_version)); 419 strncpy(luts.l_machine, machine, sizeof(luts.l_machine)); 420 strncpy(luts.l_domainname, domainname, sizeof(luts.l_domainname)); 421 422 return copyout(&luts, SCARG(uap, up), sizeof(luts)); 423 } 424 425 /* Used directly on: alpha, mips, ppc, sparc, sparc64 */ 426 /* Used indirectly on: arm, i386, m68k */ 427 428 /* 429 * New type Linux mmap call. 430 * Only called directly on machines with >= 6 free regs. 431 */ 432 int 433 linux_sys_mmap(p, v, retval) 434 struct proc *p; 435 void *v; 436 register_t *retval; 437 { 438 struct linux_sys_mmap_args /* { 439 syscallarg(unsigned long) addr; 440 syscallarg(size_t) len; 441 syscallarg(int) prot; 442 syscallarg(int) flags; 443 syscallarg(int) fd; 444 syscallarg(linux_off_t) offset; 445 } */ *uap = v; 446 struct sys_mmap_args cma; 447 int flags, fl = SCARG(uap, flags); 448 449 flags = 0; 450 flags |= cvtto_bsd_mask(fl, LINUX_MAP_SHARED, MAP_SHARED); 451 flags |= cvtto_bsd_mask(fl, LINUX_MAP_PRIVATE, MAP_PRIVATE); 452 flags |= cvtto_bsd_mask(fl, LINUX_MAP_FIXED, MAP_FIXED); 453 flags |= cvtto_bsd_mask(fl, LINUX_MAP_ANON, MAP_ANON); 454 /* XXX XAX ERH: Any other flags here? There are more defined... */ 455 456 SCARG(&cma, addr) = (void *)SCARG(uap, addr); 457 SCARG(&cma, len) = SCARG(uap, len); 458 SCARG(&cma, prot) = SCARG(uap, prot); 459 if (SCARG(&cma, prot) & VM_PROT_WRITE) /* XXX */ 460 SCARG(&cma, prot) |= VM_PROT_READ; 461 SCARG(&cma, flags) = flags; 462 SCARG(&cma, fd) = flags & MAP_ANON ? -1 : SCARG(uap, fd); 463 SCARG(&cma, pad) = 0; 464 SCARG(&cma, pos) = (off_t)SCARG(uap, offset); 465 466 return sys_mmap(p, &cma, retval); 467 } 468 469 int 470 linux_sys_mremap(p, v, retval) 471 struct proc *p; 472 void *v; 473 register_t *retval; 474 { 475 struct linux_sys_mremap_args /* { 476 syscallarg(void *) old_address; 477 syscallarg(size_t) old_size; 478 syscallarg(size_t) new_size; 479 syscallarg(u_long) flags; 480 } */ *uap = v; 481 struct sys_munmap_args mua; 482 size_t old_size, new_size; 483 int error; 484 485 old_size = round_page(SCARG(uap, old_size)); 486 new_size = round_page(SCARG(uap, new_size)); 487 488 /* 489 * Growing mapped region. 490 */ 491 if (new_size > old_size) { 492 /* 493 * XXX Implement me. What we probably want to do is 494 * XXX dig out the guts of the old mapping, mmap that 495 * XXX object again with the new size, then munmap 496 * XXX the old mapping. 497 */ 498 *retval = 0; 499 return (ENOMEM); 500 } 501 502 /* 503 * Shrinking mapped region. 504 */ 505 if (new_size < old_size) { 506 SCARG(&mua, addr) = (caddr_t)SCARG(uap, old_address) + 507 new_size; 508 SCARG(&mua, len) = old_size - new_size; 509 error = sys_munmap(p, &mua, retval); 510 *retval = error ? 0 : (register_t)SCARG(uap, old_address); 511 return (error); 512 } 513 514 /* 515 * No change. 516 */ 517 *retval = (register_t)SCARG(uap, old_address); 518 return (0); 519 } 520 521 int 522 linux_sys_msync(p, v, retval) 523 struct proc *p; 524 void *v; 525 register_t *retval; 526 { 527 struct linux_sys_msync_args /* { 528 syscallarg(caddr_t) addr; 529 syscallarg(int) len; 530 syscallarg(int) fl; 531 } */ *uap = v; 532 533 struct sys___msync13_args bma; 534 535 /* flags are ignored */ 536 SCARG(&bma, addr) = SCARG(uap, addr); 537 SCARG(&bma, len) = SCARG(uap, len); 538 SCARG(&bma, flags) = SCARG(uap, fl); 539 540 return sys___msync13(p, &bma, retval); 541 } 542 543 int 544 linux_sys_mprotect(p, v, retval) 545 struct proc *p; 546 void *v; 547 register_t *retval; 548 { 549 struct linux_sys_mprotect_args /* { 550 syscallarg(const void *) start; 551 syscallarg(unsigned long) len; 552 syscallarg(int) prot; 553 } */ *uap = v; 554 unsigned long end, start = (unsigned long)SCARG(uap, start), len; 555 int prot = SCARG(uap, prot); 556 struct vm_map_entry *entry; 557 struct vm_map *map = &p->p_vmspace->vm_map; 558 559 if (start & PAGE_MASK) 560 return EINVAL; 561 562 len = round_page(SCARG(uap, len)); 563 end = start + len; 564 565 if (end < start) 566 return EINVAL; 567 else if (end == start) 568 return 0; 569 570 if (SCARG(uap, prot) & ~(PROT_READ | PROT_WRITE | PROT_EXEC)) 571 return EINVAL; 572 573 vm_map_lock(map); 574 #ifdef notdef 575 VM_MAP_RANGE_CHECK(map, start, end); 576 #endif 577 if (!uvm_map_lookup_entry(map, start, &entry) || entry->start > start) { 578 vm_map_unlock(map); 579 return EFAULT; 580 } 581 vm_map_unlock(map); 582 return uvm_map_protect(map, start, end, prot, FALSE); 583 } 584 585 /* 586 * This code is partly stolen from src/lib/libc/compat-43/times.c 587 */ 588 589 #define CONVTCK(r) (r.tv_sec * hz + r.tv_usec / (1000000 / hz)) 590 591 int 592 linux_sys_times(p, v, retval) 593 struct proc *p; 594 void *v; 595 register_t *retval; 596 { 597 struct linux_sys_times_args /* { 598 syscallarg(struct times *) tms; 599 } */ *uap = v; 600 struct timeval t; 601 int error, s; 602 603 if (SCARG(uap, tms)) { 604 struct linux_tms ltms; 605 struct rusage ru; 606 607 calcru(p, &ru.ru_utime, &ru.ru_stime, NULL); 608 ltms.ltms_utime = CONVTCK(ru.ru_utime); 609 ltms.ltms_stime = CONVTCK(ru.ru_stime); 610 611 ltms.ltms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime); 612 ltms.ltms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime); 613 614 if ((error = copyout(<ms, SCARG(uap, tms), sizeof ltms))) 615 return error; 616 } 617 618 s = splclock(); 619 timersub(&time, &boottime, &t); 620 splx(s); 621 622 retval[0] = ((linux_clock_t)(CONVTCK(t))); 623 return 0; 624 } 625 626 #undef CONVTCK 627 628 /* 629 * Linux 'readdir' call. This code is mostly taken from the 630 * SunOS getdents call (see compat/sunos/sunos_misc.c), though 631 * an attempt has been made to keep it a little cleaner (failing 632 * miserably, because of the cruft needed if count 1 is passed). 633 * 634 * The d_off field should contain the offset of the next valid entry, 635 * but in Linux it has the offset of the entry itself. We emulate 636 * that bug here. 637 * 638 * Read in BSD-style entries, convert them, and copy them out. 639 * 640 * Note that this doesn't handle union-mounted filesystems. 641 */ 642 int 643 linux_sys_getdents(p, v, retval) 644 struct proc *p; 645 void *v; 646 register_t *retval; 647 { 648 struct linux_sys_getdents_args /* { 649 syscallarg(int) fd; 650 syscallarg(struct linux_dirent *) dent; 651 syscallarg(unsigned int) count; 652 } */ *uap = v; 653 struct dirent *bdp; 654 struct vnode *vp; 655 caddr_t inp, buf; /* BSD-format */ 656 int len, reclen; /* BSD-format */ 657 caddr_t outp; /* Linux-format */ 658 int resid, linux_reclen = 0; /* Linux-format */ 659 struct file *fp; 660 struct uio auio; 661 struct iovec aiov; 662 struct linux_dirent idb; 663 off_t off; /* true file offset */ 664 int buflen, error, eofflag, nbytes, oldcall; 665 struct vattr va; 666 off_t *cookiebuf = NULL, *cookie; 667 int ncookies; 668 669 /* getvnode() will use the descriptor for us */ 670 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0) 671 return (error); 672 673 if ((fp->f_flag & FREAD) == 0) { 674 error = EBADF; 675 goto out1; 676 } 677 678 vp = (struct vnode *)fp->f_data; 679 if (vp->v_type != VDIR) { 680 error = EINVAL; 681 goto out1; 682 } 683 684 if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p))) 685 goto out1; 686 687 nbytes = SCARG(uap, count); 688 if (nbytes == 1) { /* emulating old, broken behaviour */ 689 nbytes = sizeof (idb); 690 buflen = max(va.va_blocksize, nbytes); 691 oldcall = 1; 692 } else { 693 buflen = min(MAXBSIZE, nbytes); 694 if (buflen < va.va_blocksize) 695 buflen = va.va_blocksize; 696 oldcall = 0; 697 } 698 buf = malloc(buflen, M_TEMP, M_WAITOK); 699 700 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 701 off = fp->f_offset; 702 again: 703 aiov.iov_base = buf; 704 aiov.iov_len = buflen; 705 auio.uio_iov = &aiov; 706 auio.uio_iovcnt = 1; 707 auio.uio_rw = UIO_READ; 708 auio.uio_segflg = UIO_SYSSPACE; 709 auio.uio_procp = p; 710 auio.uio_resid = buflen; 711 auio.uio_offset = off; 712 /* 713 * First we read into the malloc'ed buffer, then 714 * we massage it into user space, one record at a time. 715 */ 716 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf, 717 &ncookies); 718 if (error) 719 goto out; 720 721 inp = buf; 722 outp = (caddr_t)SCARG(uap, dent); 723 resid = nbytes; 724 if ((len = buflen - auio.uio_resid) == 0) 725 goto eof; 726 727 for (cookie = cookiebuf; len > 0; len -= reclen) { 728 bdp = (struct dirent *)inp; 729 reclen = bdp->d_reclen; 730 if (reclen & 3) 731 panic("linux_readdir"); 732 if (bdp->d_fileno == 0) { 733 inp += reclen; /* it is a hole; squish it out */ 734 off = *cookie++; 735 continue; 736 } 737 linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen); 738 if (reclen > len || resid < linux_reclen) { 739 /* entry too big for buffer, so just stop */ 740 outp++; 741 break; 742 } 743 /* 744 * Massage in place to make a Linux-shaped dirent (otherwise 745 * we have to worry about touching user memory outside of 746 * the copyout() call). 747 */ 748 idb.d_ino = bdp->d_fileno; 749 /* 750 * The old readdir() call misuses the offset and reclen fields. 751 */ 752 if (oldcall) { 753 idb.d_off = (linux_off_t)linux_reclen; 754 idb.d_reclen = (u_short)bdp->d_namlen; 755 } else { 756 if (sizeof (idb.d_off) <= 4 && (off >> 32) != 0) { 757 compat_offseterr(vp, "linux_getdents"); 758 error = EINVAL; 759 goto out; 760 } 761 idb.d_off = (linux_off_t)off; 762 idb.d_reclen = (u_short)linux_reclen; 763 } 764 strcpy(idb.d_name, bdp->d_name); 765 if ((error = copyout((caddr_t)&idb, outp, linux_reclen))) 766 goto out; 767 /* advance past this real entry */ 768 inp += reclen; 769 off = *cookie++; /* each entry points to itself */ 770 /* advance output past Linux-shaped entry */ 771 outp += linux_reclen; 772 resid -= linux_reclen; 773 if (oldcall) 774 break; 775 } 776 777 /* if we squished out the whole block, try again */ 778 if (outp == (caddr_t)SCARG(uap, dent)) 779 goto again; 780 fp->f_offset = off; /* update the vnode offset */ 781 782 if (oldcall) 783 nbytes = resid + linux_reclen; 784 785 eof: 786 *retval = nbytes - resid; 787 out: 788 VOP_UNLOCK(vp, 0); 789 if (cookiebuf) 790 free(cookiebuf, M_TEMP); 791 free(buf, M_TEMP); 792 out1: 793 FILE_UNUSE(fp, p); 794 return error; 795 } 796 797 /* 798 * Even when just using registers to pass arguments to syscalls you can 799 * have 5 of them on the i386. So this newer version of select() does 800 * this. 801 */ 802 int 803 linux_sys_select(p, v, retval) 804 struct proc *p; 805 void *v; 806 register_t *retval; 807 { 808 struct linux_sys_select_args /* { 809 syscallarg(int) nfds; 810 syscallarg(fd_set *) readfds; 811 syscallarg(fd_set *) writefds; 812 syscallarg(fd_set *) exceptfds; 813 syscallarg(struct timeval *) timeout; 814 } */ *uap = v; 815 816 return linux_select1(p, retval, SCARG(uap, nfds), SCARG(uap, readfds), 817 SCARG(uap, writefds), SCARG(uap, exceptfds), SCARG(uap, timeout)); 818 } 819 820 /* 821 * Common code for the old and new versions of select(). A couple of 822 * things are important: 823 * 1) return the amount of time left in the 'timeout' parameter 824 * 2) select never returns ERESTART on Linux, always return EINTR 825 */ 826 int 827 linux_select1(p, retval, nfds, readfds, writefds, exceptfds, timeout) 828 struct proc *p; 829 register_t *retval; 830 int nfds; 831 fd_set *readfds, *writefds, *exceptfds; 832 struct timeval *timeout; 833 { 834 struct sys_select_args bsa; 835 struct timeval tv0, tv1, utv, *tvp; 836 caddr_t sg; 837 int error; 838 839 SCARG(&bsa, nd) = nfds; 840 SCARG(&bsa, in) = readfds; 841 SCARG(&bsa, ou) = writefds; 842 SCARG(&bsa, ex) = exceptfds; 843 SCARG(&bsa, tv) = timeout; 844 845 /* 846 * Store current time for computation of the amount of 847 * time left. 848 */ 849 if (timeout) { 850 if ((error = copyin(timeout, &utv, sizeof(utv)))) 851 return error; 852 if (itimerfix(&utv)) { 853 /* 854 * The timeval was invalid. Convert it to something 855 * valid that will act as it does under Linux. 856 */ 857 sg = stackgap_init(p, 0); 858 tvp = stackgap_alloc(p, &sg, sizeof(utv)); 859 utv.tv_sec += utv.tv_usec / 1000000; 860 utv.tv_usec %= 1000000; 861 if (utv.tv_usec < 0) { 862 utv.tv_sec -= 1; 863 utv.tv_usec += 1000000; 864 } 865 if (utv.tv_sec < 0) 866 timerclear(&utv); 867 if ((error = copyout(&utv, tvp, sizeof(utv)))) 868 return error; 869 SCARG(&bsa, tv) = tvp; 870 } 871 microtime(&tv0); 872 } 873 874 error = sys_select(p, &bsa, retval); 875 if (error) { 876 /* 877 * See fs/select.c in the Linux kernel. Without this, 878 * Maelstrom doesn't work. 879 */ 880 if (error == ERESTART) 881 error = EINTR; 882 return error; 883 } 884 885 if (timeout) { 886 if (*retval) { 887 /* 888 * Compute how much time was left of the timeout, 889 * by subtracting the current time and the time 890 * before we started the call, and subtracting 891 * that result from the user-supplied value. 892 */ 893 microtime(&tv1); 894 timersub(&tv1, &tv0, &tv1); 895 timersub(&utv, &tv1, &utv); 896 if (utv.tv_sec < 0) 897 timerclear(&utv); 898 } else 899 timerclear(&utv); 900 if ((error = copyout(&utv, timeout, sizeof(utv)))) 901 return error; 902 } 903 904 return 0; 905 } 906 907 /* 908 * Get the process group of a certain process. Look it up 909 * and return the value. 910 */ 911 int 912 linux_sys_getpgid(p, v, retval) 913 struct proc *p; 914 void *v; 915 register_t *retval; 916 { 917 struct linux_sys_getpgid_args /* { 918 syscallarg(int) pid; 919 } */ *uap = v; 920 struct proc *targp; 921 922 if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != p->p_pid) { 923 if ((targp = pfind(SCARG(uap, pid))) == 0) 924 return ESRCH; 925 } 926 else 927 targp = p; 928 929 retval[0] = targp->p_pgid; 930 return 0; 931 } 932 933 /* 934 * Set the 'personality' (emulation mode) for the current process. Only 935 * accept the Linux personality here (0). This call is needed because 936 * the Linux ELF crt0 issues it in an ugly kludge to make sure that 937 * ELF binaries run in Linux mode, not SVR4 mode. 938 */ 939 int 940 linux_sys_personality(p, v, retval) 941 struct proc *p; 942 void *v; 943 register_t *retval; 944 { 945 struct linux_sys_personality_args /* { 946 syscallarg(int) per; 947 } */ *uap = v; 948 949 if (SCARG(uap, per) != 0) 950 return EINVAL; 951 retval[0] = 0; 952 return 0; 953 } 954 955 #if defined(__i386__) || defined(__m68k__) 956 /* 957 * The calls are here because of type conversions. 958 */ 959 int 960 linux_sys_setreuid16(p, v, retval) 961 struct proc *p; 962 void *v; 963 register_t *retval; 964 { 965 struct linux_sys_setreuid16_args /* { 966 syscallarg(int) ruid; 967 syscallarg(int) euid; 968 } */ *uap = v; 969 struct sys_setreuid_args bsa; 970 971 SCARG(&bsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ? 972 (uid_t)-1 : SCARG(uap, ruid); 973 SCARG(&bsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ? 974 (uid_t)-1 : SCARG(uap, euid); 975 976 return sys_setreuid(p, &bsa, retval); 977 } 978 979 int 980 linux_sys_setregid16(p, v, retval) 981 struct proc *p; 982 void *v; 983 register_t *retval; 984 { 985 struct linux_sys_setregid16_args /* { 986 syscallarg(int) rgid; 987 syscallarg(int) egid; 988 } */ *uap = v; 989 struct sys_setregid_args bsa; 990 991 SCARG(&bsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ? 992 (uid_t)-1 : SCARG(uap, rgid); 993 SCARG(&bsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ? 994 (uid_t)-1 : SCARG(uap, egid); 995 996 return sys_setregid(p, &bsa, retval); 997 } 998 999 int 1000 linux_sys_setresuid16(p, v, retval) 1001 struct proc *p; 1002 void *v; 1003 register_t *retval; 1004 { 1005 struct linux_sys_setresuid16_args /* { 1006 syscallarg(uid_t) ruid; 1007 syscallarg(uid_t) euid; 1008 syscallarg(uid_t) suid; 1009 } */ *uap = v; 1010 struct linux_sys_setresuid16_args lsa; 1011 1012 SCARG(&lsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ? 1013 (uid_t)-1 : SCARG(uap, ruid); 1014 SCARG(&lsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ? 1015 (uid_t)-1 : SCARG(uap, euid); 1016 SCARG(&lsa, suid) = ((linux_uid_t)SCARG(uap, suid) == (linux_uid_t)-1) ? 1017 (uid_t)-1 : SCARG(uap, suid); 1018 1019 return linux_sys_setresuid(p, &lsa, retval); 1020 } 1021 1022 int 1023 linux_sys_setresgid16(p, v, retval) 1024 struct proc *p; 1025 void *v; 1026 register_t *retval; 1027 { 1028 struct linux_sys_setresgid16_args /* { 1029 syscallarg(gid_t) rgid; 1030 syscallarg(gid_t) egid; 1031 syscallarg(gid_t) sgid; 1032 } */ *uap = v; 1033 struct linux_sys_setresgid16_args lsa; 1034 1035 SCARG(&lsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ? 1036 (gid_t)-1 : SCARG(uap, rgid); 1037 SCARG(&lsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ? 1038 (gid_t)-1 : SCARG(uap, egid); 1039 SCARG(&lsa, sgid) = ((linux_gid_t)SCARG(uap, sgid) == (linux_gid_t)-1) ? 1040 (gid_t)-1 : SCARG(uap, sgid); 1041 1042 return linux_sys_setresgid(p, &lsa, retval); 1043 } 1044 1045 int 1046 linux_sys_getgroups16(p, v, retval) 1047 struct proc *p; 1048 void *v; 1049 register_t *retval; 1050 { 1051 struct linux_sys_getgroups16_args /* { 1052 syscallarg(int) gidsetsize; 1053 syscallarg(linux_gid_t *) gidset; 1054 } */ *uap = v; 1055 caddr_t sg; 1056 int n, error, i; 1057 struct sys_getgroups_args bsa; 1058 gid_t *bset, *kbset; 1059 linux_gid_t *lset; 1060 struct pcred *pc = p->p_cred; 1061 1062 n = SCARG(uap, gidsetsize); 1063 if (n < 0) 1064 return EINVAL; 1065 error = 0; 1066 bset = kbset = NULL; 1067 lset = NULL; 1068 if (n > 0) { 1069 n = min(pc->pc_ucred->cr_ngroups, n); 1070 sg = stackgap_init(p, 0); 1071 bset = stackgap_alloc(p, &sg, n * sizeof (gid_t)); 1072 kbset = malloc(n * sizeof (gid_t), M_TEMP, M_WAITOK); 1073 lset = malloc(n * sizeof (linux_gid_t), M_TEMP, M_WAITOK); 1074 if (bset == NULL || kbset == NULL || lset == NULL) 1075 return ENOMEM; 1076 SCARG(&bsa, gidsetsize) = n; 1077 SCARG(&bsa, gidset) = bset; 1078 error = sys_getgroups(p, &bsa, retval); 1079 if (error != 0) 1080 goto out; 1081 error = copyin(bset, kbset, n * sizeof (gid_t)); 1082 if (error != 0) 1083 goto out; 1084 for (i = 0; i < n; i++) 1085 lset[i] = (linux_gid_t)kbset[i]; 1086 error = copyout(lset, SCARG(uap, gidset), 1087 n * sizeof (linux_gid_t)); 1088 } else 1089 *retval = pc->pc_ucred->cr_ngroups; 1090 out: 1091 if (kbset != NULL) 1092 free(kbset, M_TEMP); 1093 if (lset != NULL) 1094 free(lset, M_TEMP); 1095 return error; 1096 } 1097 1098 int 1099 linux_sys_setgroups16(p, v, retval) 1100 struct proc *p; 1101 void *v; 1102 register_t *retval; 1103 { 1104 struct linux_sys_setgroups16_args /* { 1105 syscallarg(int) gidsetsize; 1106 syscallarg(linux_gid_t *) gidset; 1107 } */ *uap = v; 1108 caddr_t sg; 1109 int n; 1110 int error, i; 1111 struct sys_setgroups_args bsa; 1112 gid_t *bset, *kbset; 1113 linux_gid_t *lset; 1114 1115 n = SCARG(uap, gidsetsize); 1116 if (n < 0 || n > NGROUPS) 1117 return EINVAL; 1118 sg = stackgap_init(p, 0); 1119 bset = stackgap_alloc(p, &sg, n * sizeof (gid_t)); 1120 lset = malloc(n * sizeof (linux_gid_t), M_TEMP, M_WAITOK); 1121 kbset = malloc(n * sizeof (gid_t), M_TEMP, M_WAITOK); 1122 if (lset == NULL || bset == NULL) 1123 return ENOMEM; 1124 error = copyin(SCARG(uap, gidset), lset, n * sizeof (linux_gid_t)); 1125 if (error != 0) 1126 goto out; 1127 for (i = 0; i < n; i++) 1128 kbset[i] = (gid_t)lset[i]; 1129 error = copyout(kbset, bset, n * sizeof (gid_t)); 1130 if (error != 0) 1131 goto out; 1132 SCARG(&bsa, gidsetsize) = n; 1133 SCARG(&bsa, gidset) = bset; 1134 error = sys_setgroups(p, &bsa, retval); 1135 1136 out: 1137 if (lset != NULL) 1138 free(lset, M_TEMP); 1139 if (kbset != NULL) 1140 free(kbset, M_TEMP); 1141 1142 return error; 1143 } 1144 1145 #endif /* __i386__ || __m68k__ */ 1146 1147 /* 1148 * We have nonexistent fsuid equal to uid. 1149 * If modification is requested, refuse. 1150 */ 1151 int 1152 linux_sys_setfsuid(p, v, retval) 1153 struct proc *p; 1154 void *v; 1155 register_t *retval; 1156 { 1157 struct linux_sys_setfsuid_args /* { 1158 syscallarg(uid_t) uid; 1159 } */ *uap = v; 1160 uid_t uid; 1161 1162 uid = SCARG(uap, uid); 1163 if (p->p_cred->p_ruid != uid) 1164 return sys_nosys(p, v, retval); 1165 else 1166 return (0); 1167 } 1168 1169 /* XXX XXX XXX */ 1170 #ifndef alpha 1171 int 1172 linux_sys_getfsuid(p, v, retval) 1173 struct proc *p; 1174 void *v; 1175 register_t *retval; 1176 { 1177 return sys_getuid(p, v, retval); 1178 } 1179 #endif 1180 1181 int 1182 linux_sys_setresuid(p, v, retval) 1183 struct proc *p; 1184 void *v; 1185 register_t *retval; 1186 { 1187 struct linux_sys_setresuid_args /* { 1188 syscallarg(uid_t) ruid; 1189 syscallarg(uid_t) euid; 1190 syscallarg(uid_t) suid; 1191 } */ *uap = v; 1192 struct pcred *pc = p->p_cred; 1193 uid_t ruid, euid, suid; 1194 int error; 1195 1196 ruid = SCARG(uap, ruid); 1197 euid = SCARG(uap, euid); 1198 suid = SCARG(uap, suid); 1199 1200 /* 1201 * Note: These checks are a little different than the NetBSD 1202 * setreuid(2) call performs. This precisely follows the 1203 * behavior of the Linux kernel. 1204 */ 1205 if (ruid != (uid_t)-1 && 1206 ruid != pc->p_ruid && 1207 ruid != pc->pc_ucred->cr_uid && 1208 ruid != pc->p_svuid && 1209 (error = suser(pc->pc_ucred, &p->p_acflag))) 1210 return (error); 1211 1212 if (euid != (uid_t)-1 && 1213 euid != pc->p_ruid && 1214 euid != pc->pc_ucred->cr_uid && 1215 euid != pc->p_svuid && 1216 (error = suser(pc->pc_ucred, &p->p_acflag))) 1217 return (error); 1218 1219 if (suid != (uid_t)-1 && 1220 suid != pc->p_ruid && 1221 suid != pc->pc_ucred->cr_uid && 1222 suid != pc->p_svuid && 1223 (error = suser(pc->pc_ucred, &p->p_acflag))) 1224 return (error); 1225 1226 /* 1227 * Now assign the new real, effective, and saved UIDs. 1228 * Note that Linux, unlike NetBSD in setreuid(2), does not 1229 * set the saved UID in this call unless the user specifies 1230 * it. 1231 */ 1232 if (ruid != (uid_t)-1) { 1233 (void)chgproccnt(pc->p_ruid, -1); 1234 (void)chgproccnt(ruid, 1); 1235 pc->p_ruid = ruid; 1236 } 1237 1238 if (euid != (uid_t)-1) { 1239 pc->pc_ucred = crcopy(pc->pc_ucred); 1240 pc->pc_ucred->cr_uid = euid; 1241 } 1242 1243 if (suid != (uid_t)-1) 1244 pc->p_svuid = suid; 1245 1246 if (ruid != (uid_t)-1 && euid != (uid_t)-1 && suid != (uid_t)-1) 1247 p->p_flag |= P_SUGID; 1248 return (0); 1249 } 1250 1251 int 1252 linux_sys_getresuid(p, v, retval) 1253 struct proc *p; 1254 void *v; 1255 register_t *retval; 1256 { 1257 struct linux_sys_getresuid_args /* { 1258 syscallarg(uid_t *) ruid; 1259 syscallarg(uid_t *) euid; 1260 syscallarg(uid_t *) suid; 1261 } */ *uap = v; 1262 struct pcred *pc = p->p_cred; 1263 int error; 1264 1265 /* 1266 * Linux copies these values out to userspace like so: 1267 * 1268 * 1. Copy out ruid. 1269 * 2. If that succeeds, copy out euid. 1270 * 3. If both of those succeed, copy out suid. 1271 */ 1272 if ((error = copyout(&pc->p_ruid, SCARG(uap, ruid), 1273 sizeof(uid_t))) != 0) 1274 return (error); 1275 1276 if ((error = copyout(&pc->pc_ucred->cr_uid, SCARG(uap, euid), 1277 sizeof(uid_t))) != 0) 1278 return (error); 1279 1280 return (copyout(&pc->p_svuid, SCARG(uap, suid), sizeof(uid_t))); 1281 } 1282 1283 int 1284 linux_sys_ptrace(p, v, retval) 1285 struct proc *p; 1286 void *v; 1287 register_t *retval; 1288 { 1289 struct linux_sys_ptrace_args /* { 1290 i386, m68k, powerpc: T=int 1291 alpha: T=long 1292 syscallarg(T) request; 1293 syscallarg(T) pid; 1294 syscallarg(T) addr; 1295 syscallarg(T) data; 1296 } */ *uap = v; 1297 const int *ptr; 1298 int request; 1299 int error; 1300 1301 ptr = linux_ptrace_request_map; 1302 request = SCARG(uap, request); 1303 while (*ptr != -1) 1304 if (*ptr++ == request) { 1305 struct sys_ptrace_args pta; 1306 1307 SCARG(&pta, req) = *ptr; 1308 SCARG(&pta, pid) = SCARG(uap, pid); 1309 SCARG(&pta, addr) = (caddr_t)SCARG(uap, addr); 1310 SCARG(&pta, data) = SCARG(uap, data); 1311 1312 /* 1313 * Linux ptrace(PTRACE_CONT, pid, 0, 0) means actually 1314 * to continue where the process left off previously. 1315 * The same thing is achieved by addr == (caddr_t) 1 1316 * on NetBSD, so rewrite 'addr' appropriately. 1317 */ 1318 if (request == LINUX_PTRACE_CONT && SCARG(uap, addr)==0) 1319 SCARG(&pta, addr) = (caddr_t) 1; 1320 1321 error = sys_ptrace(p, &pta, retval); 1322 if (error) 1323 return error; 1324 switch (request) { 1325 case LINUX_PTRACE_PEEKTEXT: 1326 case LINUX_PTRACE_PEEKDATA: 1327 error = copyout (retval, 1328 (caddr_t)SCARG(uap, data), sizeof *retval); 1329 *retval = SCARG(uap, data); 1330 break; 1331 default: 1332 break; 1333 } 1334 return error; 1335 } 1336 else 1337 ptr++; 1338 1339 return LINUX_SYS_PTRACE_ARCH(p, uap, retval); 1340 } 1341 1342 int 1343 linux_sys_reboot(struct proc *p, void *v, register_t *retval) 1344 { 1345 struct linux_sys_reboot_args /* { 1346 syscallarg(int) magic1; 1347 syscallarg(int) magic2; 1348 syscallarg(int) cmd; 1349 syscallarg(void *) arg; 1350 } */ *uap = v; 1351 struct sys_reboot_args /* { 1352 syscallarg(int) opt; 1353 syscallarg(char *) bootstr; 1354 } */ sra; 1355 int error; 1356 1357 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 1358 return(error); 1359 1360 if (SCARG(uap, magic1) != LINUX_REBOOT_MAGIC1) 1361 return(EINVAL); 1362 if (SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2 && 1363 SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2A && 1364 SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2B) 1365 return(EINVAL); 1366 1367 switch (SCARG(uap, cmd)) { 1368 case LINUX_REBOOT_CMD_RESTART: 1369 SCARG(&sra, opt) = RB_AUTOBOOT; 1370 break; 1371 case LINUX_REBOOT_CMD_HALT: 1372 SCARG(&sra, opt) = RB_HALT; 1373 break; 1374 case LINUX_REBOOT_CMD_POWER_OFF: 1375 SCARG(&sra, opt) = RB_HALT|RB_POWERDOWN; 1376 break; 1377 case LINUX_REBOOT_CMD_RESTART2: 1378 /* Reboot with an argument. */ 1379 SCARG(&sra, opt) = RB_AUTOBOOT|RB_STRING; 1380 SCARG(&sra, bootstr) = SCARG(uap, arg); 1381 break; 1382 case LINUX_REBOOT_CMD_CAD_ON: 1383 return(EINVAL); /* We don't implement ctrl-alt-delete */ 1384 case LINUX_REBOOT_CMD_CAD_OFF: 1385 return(0); 1386 default: 1387 return(EINVAL); 1388 } 1389 1390 return(sys_reboot(p, &sra, retval)); 1391 } 1392 1393 /* 1394 * Copy of compat_12_sys_swapon(). 1395 */ 1396 int 1397 linux_sys_swapon(p, v, retval) 1398 struct proc *p; 1399 void *v; 1400 register_t *retval; 1401 { 1402 struct sys_swapctl_args ua; 1403 struct linux_sys_swapon_args /* { 1404 syscallarg(const char *) name; 1405 } */ *uap = v; 1406 1407 SCARG(&ua, cmd) = SWAP_ON; 1408 SCARG(&ua, arg) = (void *)SCARG(uap, name); 1409 SCARG(&ua, misc) = 0; /* priority */ 1410 return (sys_swapctl(p, &ua, retval)); 1411 } 1412 1413 /* 1414 * Stop swapping to the file or block device specified by path. 1415 */ 1416 int 1417 linux_sys_swapoff(p, v, retval) 1418 struct proc *p; 1419 void *v; 1420 register_t *retval; 1421 { 1422 struct sys_swapctl_args ua; 1423 struct linux_sys_swapoff_args /* { 1424 syscallarg(const char *) path; 1425 } */ *uap = v; 1426 1427 SCARG(&ua, cmd) = SWAP_OFF; 1428 SCARG(&ua, arg) = (void *)SCARG(uap, path); 1429 return (sys_swapctl(p, &ua, retval)); 1430 } 1431 1432 /* 1433 * Copy of compat_09_sys_setdomainname() 1434 */ 1435 /* ARGSUSED */ 1436 int 1437 linux_sys_setdomainname(p, v, retval) 1438 struct proc *p; 1439 void *v; 1440 register_t *retval; 1441 { 1442 struct linux_sys_setdomainname_args /* { 1443 syscallarg(char *) domainname; 1444 syscallarg(int) len; 1445 } */ *uap = v; 1446 int name; 1447 int error; 1448 1449 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 1450 return (error); 1451 name = KERN_DOMAINNAME; 1452 return (kern_sysctl(&name, 1, 0, 0, SCARG(uap, domainname), 1453 SCARG(uap, len), p)); 1454 } 1455 1456 /* 1457 * sysinfo() 1458 */ 1459 /* ARGSUSED */ 1460 int 1461 linux_sys_sysinfo(p, v, retval) 1462 struct proc *p; 1463 void *v; 1464 register_t *retval; 1465 { 1466 struct linux_sys_sysinfo_args /* { 1467 syscallarg(struct linux_sysinfo *) arg; 1468 } */ *uap = v; 1469 struct linux_sysinfo si; 1470 struct loadavg *la; 1471 1472 si.uptime = time.tv_sec - boottime.tv_sec; 1473 la = &averunnable; 1474 si.loads[0] = la->ldavg[0] * LINUX_SYSINFO_LOADS_SCALE / la->fscale; 1475 si.loads[1] = la->ldavg[1] * LINUX_SYSINFO_LOADS_SCALE / la->fscale; 1476 si.loads[2] = la->ldavg[2] * LINUX_SYSINFO_LOADS_SCALE / la->fscale; 1477 si.totalram = ctob(physmem); 1478 si.freeram = uvmexp.free * uvmexp.pagesize; 1479 si.sharedram = 0; /* XXX */ 1480 si.bufferram = uvmexp.filepages * uvmexp.pagesize; 1481 si.totalswap = uvmexp.swpages * uvmexp.pagesize; 1482 si.freeswap = (uvmexp.swpages - uvmexp.swpginuse) * uvmexp.pagesize; 1483 si.procs = nprocs; 1484 1485 /* The following are only present in newer Linux kernels. */ 1486 si.totalbig = 0; 1487 si.freebig = 0; 1488 si.mem_unit = 1; 1489 1490 return (copyout(&si, SCARG(uap, arg), sizeof si)); 1491 } 1492 1493 #define bsd_to_linux_rlimit1(l, b, f) \ 1494 (l)->f = ((b)->f == RLIM_INFINITY || \ 1495 ((b)->f & 0xffffffff00000000ULL) != 0) ? \ 1496 LINUX_RLIM_INFINITY : (int32_t)(b)->f 1497 #define bsd_to_linux_rlimit(l, b) \ 1498 bsd_to_linux_rlimit1(l, b, rlim_cur); \ 1499 bsd_to_linux_rlimit1(l, b, rlim_max) 1500 1501 #define linux_to_bsd_rlimit1(b, l, f) \ 1502 (b)->f = (l)->f == LINUX_RLIM_INFINITY ? RLIM_INFINITY : (l)->f 1503 #define linux_to_bsd_rlimit(b, l) \ 1504 linux_to_bsd_rlimit1(b, l, rlim_cur); \ 1505 linux_to_bsd_rlimit1(b, l, rlim_max) 1506 1507 static int 1508 linux_to_bsd_limit(lim) 1509 int lim; 1510 { 1511 switch (lim) { 1512 case LINUX_RLIMIT_CPU: 1513 return RLIMIT_CPU; 1514 case LINUX_RLIMIT_FSIZE: 1515 return RLIMIT_FSIZE; 1516 case LINUX_RLIMIT_DATA: 1517 return RLIMIT_DATA; 1518 case LINUX_RLIMIT_STACK: 1519 return RLIMIT_STACK; 1520 case LINUX_RLIMIT_CORE: 1521 return RLIMIT_CORE; 1522 case LINUX_RLIMIT_RSS: 1523 return RLIMIT_RSS; 1524 case LINUX_RLIMIT_NPROC: 1525 return RLIMIT_NPROC; 1526 case LINUX_RLIMIT_NOFILE: 1527 return RLIMIT_NOFILE; 1528 case LINUX_RLIMIT_MEMLOCK: 1529 return RLIMIT_MEMLOCK; 1530 case LINUX_RLIMIT_AS: 1531 case LINUX_RLIMIT_LOCKS: 1532 return -EOPNOTSUPP; 1533 default: 1534 return -EINVAL; 1535 } 1536 } 1537 1538 1539 int 1540 linux_sys_getrlimit(p, v, retval) 1541 struct proc *p; 1542 void *v; 1543 register_t *retval; 1544 { 1545 struct linux_sys_getrlimit_args /* { 1546 syscallarg(int) which; 1547 syscallarg(struct orlimit *) rlp; 1548 } */ *uap = v; 1549 caddr_t sg = stackgap_init(p, 0); 1550 struct sys_getrlimit_args ap; 1551 struct rlimit rl; 1552 struct orlimit orl; 1553 int error; 1554 1555 SCARG(&ap, which) = linux_to_bsd_limit(SCARG(uap, which)); 1556 if ((error = SCARG(&ap, which)) < 0) 1557 return -error; 1558 SCARG(&ap, rlp) = stackgap_alloc(p, &sg, sizeof rl); 1559 if ((error = sys_getrlimit(p, &ap, retval)) != 0) 1560 return error; 1561 if ((error = copyin(SCARG(&ap, rlp), &rl, sizeof(rl))) != 0) 1562 return error; 1563 bsd_to_linux_rlimit(&orl, &rl); 1564 return copyout(&orl, SCARG(uap, rlp), sizeof(orl)); 1565 } 1566 1567 int 1568 linux_sys_setrlimit(p, v, retval) 1569 struct proc *p; 1570 void *v; 1571 register_t *retval; 1572 { 1573 struct linux_sys_setrlimit_args /* { 1574 syscallarg(int) which; 1575 syscallarg(struct orlimit *) rlp; 1576 } */ *uap = v; 1577 caddr_t sg = stackgap_init(p, 0); 1578 struct sys_setrlimit_args ap; 1579 struct rlimit rl; 1580 struct orlimit orl; 1581 int error; 1582 1583 SCARG(&ap, which) = linux_to_bsd_limit(SCARG(uap, which)); 1584 SCARG(&ap, rlp) = stackgap_alloc(p, &sg, sizeof rl); 1585 if ((error = SCARG(&ap, which)) < 0) 1586 return -error; 1587 if ((error = copyin(SCARG(uap, rlp), &orl, sizeof(orl))) != 0) 1588 return error; 1589 linux_to_bsd_rlimit(&rl, &orl); 1590 /* XXX: alpha complains about this */ 1591 if ((error = copyout(&rl, (void *)SCARG(&ap, rlp), sizeof(rl))) != 0) 1592 return error; 1593 return sys_setrlimit(p, &ap, retval); 1594 } 1595 1596 #ifndef __mips__ 1597 /* XXX: this doesn't look 100% common, at least mips doesn't have it */ 1598 int 1599 linux_sys_ugetrlimit(p, v, retval) 1600 struct proc *p; 1601 void *v; 1602 register_t *retval; 1603 { 1604 return linux_sys_getrlimit(p, v, retval); 1605 } 1606 #endif 1607 1608 /* 1609 * This gets called for unsupported syscalls. The difference to sys_nosys() 1610 * is that process does not get SIGSYS, the call just returns with ENOSYS. 1611 * This is the way Linux does it and glibc depends on this behaviour. 1612 */ 1613 int 1614 linux_sys_nosys(p, v, retval) 1615 struct proc *p; 1616 void *v; 1617 register_t *retval; 1618 { 1619 return (ENOSYS); 1620 } 1621