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