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