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