linux_misc.c revision 1.161.2.2 1 /* $NetBSD: linux_misc.c,v 1.161.2.2 2006/11/18 21:39:07 ad 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.161.2.2 2006/11/18 21:39:07 ad 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(&l->l_sigpend->sp_set, 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, void *v, register_t *retval)
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)
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 mutex_enter(&p->p_smutex);
785 calcru(p, &ru.ru_utime, &ru.ru_stime, NULL, NULL);
786 ltms.ltms_utime = CONVTCK(ru.ru_utime);
787 ltms.ltms_stime = CONVTCK(ru.ru_stime);
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 mutex_exit(&p->p_smutex);
791
792 if ((error = copyout(<ms, SCARG(uap, tms), sizeof ltms)))
793 return error;
794 }
795
796 getmicrouptime(&t);
797
798 retval[0] = ((linux_clock_t)(CONVTCK(t)));
799 return 0;
800 }
801
802 #undef CONVTCK
803
804 /*
805 * Linux 'readdir' call. This code is mostly taken from the
806 * SunOS getdents call (see compat/sunos/sunos_misc.c), though
807 * an attempt has been made to keep it a little cleaner (failing
808 * miserably, because of the cruft needed if count 1 is passed).
809 *
810 * The d_off field should contain the offset of the next valid entry,
811 * but in Linux it has the offset of the entry itself. We emulate
812 * that bug here.
813 *
814 * Read in BSD-style entries, convert them, and copy them out.
815 *
816 * Note that this doesn't handle union-mounted filesystems.
817 */
818 int
819 linux_sys_getdents(l, v, retval)
820 struct lwp *l;
821 void *v;
822 register_t *retval;
823 {
824 struct linux_sys_getdents_args /* {
825 syscallarg(int) fd;
826 syscallarg(struct linux_dirent *) dent;
827 syscallarg(unsigned int) count;
828 } */ *uap = v;
829 struct dirent *bdp;
830 struct vnode *vp;
831 caddr_t inp, tbuf; /* BSD-format */
832 int len, reclen; /* BSD-format */
833 caddr_t outp; /* Linux-format */
834 int resid, linux_reclen = 0; /* Linux-format */
835 struct file *fp;
836 struct uio auio;
837 struct iovec aiov;
838 struct linux_dirent idb;
839 off_t off; /* true file offset */
840 int buflen, error, eofflag, nbytes, oldcall;
841 struct vattr va;
842 off_t *cookiebuf = NULL, *cookie;
843 int ncookies;
844
845 /* getvnode() will use the descriptor for us */
846 if ((error = getvnode(l->l_proc->p_fd, SCARG(uap, fd), &fp)) != 0)
847 return (error);
848
849 if ((fp->f_flag & FREAD) == 0) {
850 error = EBADF;
851 goto out1;
852 }
853
854 vp = (struct vnode *)fp->f_data;
855 if (vp->v_type != VDIR) {
856 error = EINVAL;
857 goto out1;
858 }
859
860 if ((error = VOP_GETATTR(vp, &va, l->l_cred, l)))
861 goto out1;
862
863 nbytes = SCARG(uap, count);
864 if (nbytes == 1) { /* emulating old, broken behaviour */
865 nbytes = sizeof (idb);
866 buflen = max(va.va_blocksize, nbytes);
867 oldcall = 1;
868 } else {
869 buflen = min(MAXBSIZE, nbytes);
870 if (buflen < va.va_blocksize)
871 buflen = va.va_blocksize;
872 oldcall = 0;
873 }
874 tbuf = malloc(buflen, M_TEMP, M_WAITOK);
875
876 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
877 off = fp->f_offset;
878 again:
879 aiov.iov_base = tbuf;
880 aiov.iov_len = buflen;
881 auio.uio_iov = &aiov;
882 auio.uio_iovcnt = 1;
883 auio.uio_rw = UIO_READ;
884 auio.uio_resid = buflen;
885 auio.uio_offset = off;
886 UIO_SETUP_SYSSPACE(&auio);
887 /*
888 * First we read into the malloc'ed buffer, then
889 * we massage it into user space, one record at a time.
890 */
891 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
892 &ncookies);
893 if (error)
894 goto out;
895
896 inp = tbuf;
897 outp = (caddr_t)SCARG(uap, dent);
898 resid = nbytes;
899 if ((len = buflen - auio.uio_resid) == 0)
900 goto eof;
901
902 for (cookie = cookiebuf; len > 0; len -= reclen) {
903 bdp = (struct dirent *)inp;
904 reclen = bdp->d_reclen;
905 if (reclen & 3)
906 panic("linux_readdir");
907 if (bdp->d_fileno == 0) {
908 inp += reclen; /* it is a hole; squish it out */
909 if (cookie)
910 off = *cookie++;
911 else
912 off += reclen;
913 continue;
914 }
915 linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
916 if (reclen > len || resid < linux_reclen) {
917 /* entry too big for buffer, so just stop */
918 outp++;
919 break;
920 }
921 /*
922 * Massage in place to make a Linux-shaped dirent (otherwise
923 * we have to worry about touching user memory outside of
924 * the copyout() call).
925 */
926 idb.d_ino = bdp->d_fileno;
927 /*
928 * The old readdir() call misuses the offset and reclen fields.
929 */
930 if (oldcall) {
931 idb.d_off = (linux_off_t)linux_reclen;
932 idb.d_reclen = (u_short)bdp->d_namlen;
933 } else {
934 if (sizeof (idb.d_off) <= 4 && (off >> 32) != 0) {
935 compat_offseterr(vp, "linux_getdents");
936 error = EINVAL;
937 goto out;
938 }
939 idb.d_off = (linux_off_t)off;
940 idb.d_reclen = (u_short)linux_reclen;
941 }
942 strcpy(idb.d_name, bdp->d_name);
943 if ((error = copyout((caddr_t)&idb, outp, linux_reclen)))
944 goto out;
945 /* advance past this real entry */
946 inp += reclen;
947 if (cookie)
948 off = *cookie++; /* each entry points to itself */
949 else
950 off += reclen;
951 /* advance output past Linux-shaped entry */
952 outp += linux_reclen;
953 resid -= linux_reclen;
954 if (oldcall)
955 break;
956 }
957
958 /* if we squished out the whole block, try again */
959 if (outp == (caddr_t)SCARG(uap, dent))
960 goto again;
961 fp->f_offset = off; /* update the vnode offset */
962
963 if (oldcall)
964 nbytes = resid + linux_reclen;
965
966 eof:
967 *retval = nbytes - resid;
968 out:
969 VOP_UNLOCK(vp, 0);
970 if (cookiebuf)
971 free(cookiebuf, M_TEMP);
972 free(tbuf, M_TEMP);
973 out1:
974 FILE_UNUSE(fp, l);
975 return error;
976 }
977
978 /*
979 * Even when just using registers to pass arguments to syscalls you can
980 * have 5 of them on the i386. So this newer version of select() does
981 * this.
982 */
983 int
984 linux_sys_select(l, v, retval)
985 struct lwp *l;
986 void *v;
987 register_t *retval;
988 {
989 struct linux_sys_select_args /* {
990 syscallarg(int) nfds;
991 syscallarg(fd_set *) readfds;
992 syscallarg(fd_set *) writefds;
993 syscallarg(fd_set *) exceptfds;
994 syscallarg(struct timeval *) timeout;
995 } */ *uap = v;
996
997 return linux_select1(l, retval, SCARG(uap, nfds), SCARG(uap, readfds),
998 SCARG(uap, writefds), SCARG(uap, exceptfds), SCARG(uap, timeout));
999 }
1000
1001 /*
1002 * Common code for the old and new versions of select(). A couple of
1003 * things are important:
1004 * 1) return the amount of time left in the 'timeout' parameter
1005 * 2) select never returns ERESTART on Linux, always return EINTR
1006 */
1007 int
1008 linux_select1(l, retval, nfds, readfds, writefds, exceptfds, timeout)
1009 struct lwp *l;
1010 register_t *retval;
1011 int nfds;
1012 fd_set *readfds, *writefds, *exceptfds;
1013 struct timeval *timeout;
1014 {
1015 struct sys_select_args bsa;
1016 struct proc *p = l->l_proc;
1017 struct timeval tv0, tv1, utv, *tvp;
1018 caddr_t sg;
1019 int error;
1020
1021 SCARG(&bsa, nd) = nfds;
1022 SCARG(&bsa, in) = readfds;
1023 SCARG(&bsa, ou) = writefds;
1024 SCARG(&bsa, ex) = exceptfds;
1025 SCARG(&bsa, tv) = timeout;
1026
1027 /*
1028 * Store current time for computation of the amount of
1029 * time left.
1030 */
1031 if (timeout) {
1032 if ((error = copyin(timeout, &utv, sizeof(utv))))
1033 return error;
1034 if (itimerfix(&utv)) {
1035 /*
1036 * The timeval was invalid. Convert it to something
1037 * valid that will act as it does under Linux.
1038 */
1039 sg = stackgap_init(p, 0);
1040 tvp = stackgap_alloc(p, &sg, sizeof(utv));
1041 utv.tv_sec += utv.tv_usec / 1000000;
1042 utv.tv_usec %= 1000000;
1043 if (utv.tv_usec < 0) {
1044 utv.tv_sec -= 1;
1045 utv.tv_usec += 1000000;
1046 }
1047 if (utv.tv_sec < 0)
1048 timerclear(&utv);
1049 if ((error = copyout(&utv, tvp, sizeof(utv))))
1050 return error;
1051 SCARG(&bsa, tv) = tvp;
1052 }
1053 microtime(&tv0);
1054 }
1055
1056 error = sys_select(l, &bsa, retval);
1057 if (error) {
1058 /*
1059 * See fs/select.c in the Linux kernel. Without this,
1060 * Maelstrom doesn't work.
1061 */
1062 if (error == ERESTART)
1063 error = EINTR;
1064 return error;
1065 }
1066
1067 if (timeout) {
1068 if (*retval) {
1069 /*
1070 * Compute how much time was left of the timeout,
1071 * by subtracting the current time and the time
1072 * before we started the call, and subtracting
1073 * that result from the user-supplied value.
1074 */
1075 microtime(&tv1);
1076 timersub(&tv1, &tv0, &tv1);
1077 timersub(&utv, &tv1, &utv);
1078 if (utv.tv_sec < 0)
1079 timerclear(&utv);
1080 } else
1081 timerclear(&utv);
1082 if ((error = copyout(&utv, timeout, sizeof(utv))))
1083 return error;
1084 }
1085
1086 return 0;
1087 }
1088
1089 /*
1090 * Get the process group of a certain process. Look it up
1091 * and return the value.
1092 */
1093 int
1094 linux_sys_getpgid(l, v, retval)
1095 struct lwp *l;
1096 void *v;
1097 register_t *retval;
1098 {
1099 struct linux_sys_getpgid_args /* {
1100 syscallarg(int) pid;
1101 } */ *uap = v;
1102 struct proc *p = l->l_proc;
1103 struct proc *targp;
1104
1105 if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != p->p_pid) {
1106 if ((targp = pfind(SCARG(uap, pid))) == 0)
1107 return ESRCH;
1108 }
1109 else
1110 targp = p;
1111
1112 retval[0] = targp->p_pgid;
1113 return 0;
1114 }
1115
1116 /*
1117 * Set the 'personality' (emulation mode) for the current process. Only
1118 * accept the Linux personality here (0). This call is needed because
1119 * the Linux ELF crt0 issues it in an ugly kludge to make sure that
1120 * ELF binaries run in Linux mode, not SVR4 mode.
1121 */
1122 int
1123 linux_sys_personality(struct lwp *l, void *v, register_t *retval)
1124 {
1125 struct linux_sys_personality_args /* {
1126 syscallarg(int) per;
1127 } */ *uap = v;
1128
1129 if (SCARG(uap, per) != 0)
1130 return EINVAL;
1131 retval[0] = 0;
1132 return 0;
1133 }
1134 #endif /* !COMPAT_LINUX32 */
1135
1136 #if defined(__i386__) || defined(__m68k__) || defined(COMPAT_LINUX32)
1137 /*
1138 * The calls are here because of type conversions.
1139 */
1140 int
1141 linux_sys_setreuid16(l, v, retval)
1142 struct lwp *l;
1143 void *v;
1144 register_t *retval;
1145 {
1146 struct linux_sys_setreuid16_args /* {
1147 syscallarg(int) ruid;
1148 syscallarg(int) euid;
1149 } */ *uap = v;
1150 struct sys_setreuid_args bsa;
1151
1152 SCARG(&bsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
1153 (uid_t)-1 : SCARG(uap, ruid);
1154 SCARG(&bsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
1155 (uid_t)-1 : SCARG(uap, euid);
1156
1157 return sys_setreuid(l, &bsa, retval);
1158 }
1159
1160 int
1161 linux_sys_setregid16(l, v, retval)
1162 struct lwp *l;
1163 void *v;
1164 register_t *retval;
1165 {
1166 struct linux_sys_setregid16_args /* {
1167 syscallarg(int) rgid;
1168 syscallarg(int) egid;
1169 } */ *uap = v;
1170 struct sys_setregid_args bsa;
1171
1172 SCARG(&bsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
1173 (uid_t)-1 : SCARG(uap, rgid);
1174 SCARG(&bsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
1175 (uid_t)-1 : SCARG(uap, egid);
1176
1177 return sys_setregid(l, &bsa, retval);
1178 }
1179
1180 int
1181 linux_sys_setresuid16(l, v, retval)
1182 struct lwp *l;
1183 void *v;
1184 register_t *retval;
1185 {
1186 struct linux_sys_setresuid16_args /* {
1187 syscallarg(uid_t) ruid;
1188 syscallarg(uid_t) euid;
1189 syscallarg(uid_t) suid;
1190 } */ *uap = v;
1191 struct linux_sys_setresuid16_args lsa;
1192
1193 SCARG(&lsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
1194 (uid_t)-1 : SCARG(uap, ruid);
1195 SCARG(&lsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
1196 (uid_t)-1 : SCARG(uap, euid);
1197 SCARG(&lsa, suid) = ((linux_uid_t)SCARG(uap, suid) == (linux_uid_t)-1) ?
1198 (uid_t)-1 : SCARG(uap, suid);
1199
1200 return linux_sys_setresuid(l, &lsa, retval);
1201 }
1202
1203 int
1204 linux_sys_setresgid16(l, v, retval)
1205 struct lwp *l;
1206 void *v;
1207 register_t *retval;
1208 {
1209 struct linux_sys_setresgid16_args /* {
1210 syscallarg(gid_t) rgid;
1211 syscallarg(gid_t) egid;
1212 syscallarg(gid_t) sgid;
1213 } */ *uap = v;
1214 struct linux_sys_setresgid16_args lsa;
1215
1216 SCARG(&lsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
1217 (gid_t)-1 : SCARG(uap, rgid);
1218 SCARG(&lsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
1219 (gid_t)-1 : SCARG(uap, egid);
1220 SCARG(&lsa, sgid) = ((linux_gid_t)SCARG(uap, sgid) == (linux_gid_t)-1) ?
1221 (gid_t)-1 : SCARG(uap, sgid);
1222
1223 return linux_sys_setresgid(l, &lsa, retval);
1224 }
1225
1226 int
1227 linux_sys_getgroups16(l, v, retval)
1228 struct lwp *l;
1229 void *v;
1230 register_t *retval;
1231 {
1232 struct linux_sys_getgroups16_args /* {
1233 syscallarg(int) gidsetsize;
1234 syscallarg(linux_gid_t *) gidset;
1235 } */ *uap = v;
1236 struct proc *p = l->l_proc;
1237 caddr_t sg;
1238 int n, error, i;
1239 struct sys_getgroups_args bsa;
1240 gid_t *bset, *kbset;
1241 linux_gid_t *lset;
1242 kauth_cred_t pc = l->l_cred;
1243
1244 n = SCARG(uap, gidsetsize);
1245 if (n < 0)
1246 return EINVAL;
1247 error = 0;
1248 bset = kbset = NULL;
1249 lset = NULL;
1250 if (n > 0) {
1251 n = min(kauth_cred_ngroups(pc), n);
1252 sg = stackgap_init(p, 0);
1253 bset = stackgap_alloc(p, &sg, n * sizeof (gid_t));
1254 kbset = malloc(n * sizeof (gid_t), M_TEMP, M_WAITOK);
1255 lset = malloc(n * sizeof (linux_gid_t), M_TEMP, M_WAITOK);
1256 if (bset == NULL || kbset == NULL || lset == NULL)
1257 {
1258 error = ENOMEM;
1259 goto out;
1260 }
1261 SCARG(&bsa, gidsetsize) = n;
1262 SCARG(&bsa, gidset) = bset;
1263 error = sys_getgroups(l, &bsa, retval);
1264 if (error != 0)
1265 goto out;
1266 error = copyin(bset, kbset, n * sizeof (gid_t));
1267 if (error != 0)
1268 goto out;
1269 for (i = 0; i < n; i++)
1270 lset[i] = (linux_gid_t)kbset[i];
1271 error = copyout(lset, SCARG(uap, gidset),
1272 n * sizeof (linux_gid_t));
1273 } else
1274 *retval = kauth_cred_ngroups(pc);
1275 out:
1276 if (kbset != NULL)
1277 free(kbset, M_TEMP);
1278 if (lset != NULL)
1279 free(lset, M_TEMP);
1280 return error;
1281 }
1282
1283 int
1284 linux_sys_setgroups16(l, v, retval)
1285 struct lwp *l;
1286 void *v;
1287 register_t *retval;
1288 {
1289 struct linux_sys_setgroups16_args /* {
1290 syscallarg(int) gidsetsize;
1291 syscallarg(linux_gid_t *) gidset;
1292 } */ *uap = v;
1293 struct proc *p = l->l_proc;
1294 caddr_t sg;
1295 int n;
1296 int error, i;
1297 struct sys_setgroups_args bsa;
1298 gid_t *bset, *kbset;
1299 linux_gid_t *lset;
1300
1301 n = SCARG(uap, gidsetsize);
1302 if (n < 0 || n > NGROUPS)
1303 return EINVAL;
1304 sg = stackgap_init(p, 0);
1305 bset = stackgap_alloc(p, &sg, n * sizeof (gid_t));
1306 lset = malloc(n * sizeof (linux_gid_t), M_TEMP, M_WAITOK);
1307 kbset = malloc(n * sizeof (gid_t), M_TEMP, M_WAITOK);
1308 if (bset == NULL || kbset == NULL || lset == NULL)
1309 {
1310 error = ENOMEM;
1311 goto out;
1312 }
1313 error = copyin(SCARG(uap, gidset), lset, n * sizeof (linux_gid_t));
1314 if (error != 0)
1315 goto out;
1316 for (i = 0; i < n; i++)
1317 kbset[i] = (gid_t)lset[i];
1318 error = copyout(kbset, bset, n * sizeof (gid_t));
1319 if (error != 0)
1320 goto out;
1321 SCARG(&bsa, gidsetsize) = n;
1322 SCARG(&bsa, gidset) = bset;
1323 error = sys_setgroups(l, &bsa, retval);
1324
1325 out:
1326 if (lset != NULL)
1327 free(lset, M_TEMP);
1328 if (kbset != NULL)
1329 free(kbset, M_TEMP);
1330
1331 return error;
1332 }
1333
1334 #endif /* __i386__ || __m68k__ || COMPAT_LINUX32 */
1335
1336 #ifndef COMPAT_LINUX32
1337 /*
1338 * We have nonexistent fsuid equal to uid.
1339 * If modification is requested, refuse.
1340 */
1341 int
1342 linux_sys_setfsuid(l, v, retval)
1343 struct lwp *l;
1344 void *v;
1345 register_t *retval;
1346 {
1347 struct linux_sys_setfsuid_args /* {
1348 syscallarg(uid_t) uid;
1349 } */ *uap = v;
1350 uid_t uid;
1351
1352 uid = SCARG(uap, uid);
1353 if (kauth_cred_getuid(l->l_cred) != uid)
1354 return sys_nosys(l, v, retval);
1355 else
1356 return (0);
1357 }
1358
1359 /* XXX XXX XXX */
1360 # ifndef alpha
1361 int
1362 linux_sys_getfsuid(l, v, retval)
1363 struct lwp *l;
1364 void *v;
1365 register_t *retval;
1366 {
1367 return sys_getuid(l, v, retval);
1368 }
1369 # endif
1370
1371 int
1372 linux_sys_setresuid(struct lwp *l, void *v, register_t *retval)
1373 {
1374 struct linux_sys_setresuid_args /* {
1375 syscallarg(uid_t) ruid;
1376 syscallarg(uid_t) euid;
1377 syscallarg(uid_t) suid;
1378 } */ *uap = v;
1379
1380 /*
1381 * Note: These checks are a little different than the NetBSD
1382 * setreuid(2) call performs. This precisely follows the
1383 * behavior of the Linux kernel.
1384 */
1385
1386 return do_setresuid(l, SCARG(uap, ruid), SCARG(uap, euid),
1387 SCARG(uap, suid),
1388 ID_R_EQ_R | ID_R_EQ_E | ID_R_EQ_S |
1389 ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S |
1390 ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S );
1391 }
1392
1393 int
1394 linux_sys_getresuid(struct lwp *l, void *v, register_t *retval)
1395 {
1396 struct linux_sys_getresuid_args /* {
1397 syscallarg(uid_t *) ruid;
1398 syscallarg(uid_t *) euid;
1399 syscallarg(uid_t *) suid;
1400 } */ *uap = v;
1401 kauth_cred_t pc = l->l_cred;
1402 int error;
1403 uid_t uid;
1404
1405 /*
1406 * Linux copies these values out to userspace like so:
1407 *
1408 * 1. Copy out ruid.
1409 * 2. If that succeeds, copy out euid.
1410 * 3. If both of those succeed, copy out suid.
1411 */
1412 uid = kauth_cred_getuid(pc);
1413 if ((error = copyout(&uid, SCARG(uap, ruid), sizeof(uid_t))) != 0)
1414 return (error);
1415
1416 uid = kauth_cred_geteuid(pc);
1417 if ((error = copyout(&uid, SCARG(uap, euid), sizeof(uid_t))) != 0)
1418 return (error);
1419
1420 uid = kauth_cred_getsvuid(pc);
1421
1422 return (copyout(&uid, SCARG(uap, suid), sizeof(uid_t)));
1423 }
1424
1425 int
1426 linux_sys_ptrace(l, v, retval)
1427 struct lwp *l;
1428 void *v;
1429 register_t *retval;
1430 {
1431 #if defined(PTRACE) || defined(_LKM)
1432 struct linux_sys_ptrace_args /* {
1433 i386, m68k, powerpc: T=int
1434 alpha, amd64: T=long
1435 syscallarg(T) request;
1436 syscallarg(T) pid;
1437 syscallarg(T) addr;
1438 syscallarg(T) data;
1439 } */ *uap = v;
1440 const int *ptr;
1441 int request;
1442 int error;
1443 #ifdef _LKM
1444 #define sys_ptrace (*sysent[SYS_ptrace].sy_call)
1445 #endif
1446
1447 ptr = linux_ptrace_request_map;
1448 request = SCARG(uap, request);
1449 while (*ptr != -1)
1450 if (*ptr++ == request) {
1451 struct sys_ptrace_args pta;
1452
1453 SCARG(&pta, req) = *ptr;
1454 SCARG(&pta, pid) = SCARG(uap, pid);
1455 SCARG(&pta, addr) = (caddr_t)SCARG(uap, addr);
1456 SCARG(&pta, data) = SCARG(uap, data);
1457
1458 /*
1459 * Linux ptrace(PTRACE_CONT, pid, 0, 0) means actually
1460 * to continue where the process left off previously.
1461 * The same thing is achieved by addr == (caddr_t) 1
1462 * on NetBSD, so rewrite 'addr' appropriately.
1463 */
1464 if (request == LINUX_PTRACE_CONT && SCARG(uap, addr)==0)
1465 SCARG(&pta, addr) = (caddr_t) 1;
1466
1467 error = sys_ptrace(l, &pta, retval);
1468 if (error)
1469 return error;
1470 switch (request) {
1471 case LINUX_PTRACE_PEEKTEXT:
1472 case LINUX_PTRACE_PEEKDATA:
1473 error = copyout (retval,
1474 (caddr_t)SCARG(uap, data),
1475 sizeof *retval);
1476 *retval = SCARG(uap, data);
1477 break;
1478 default:
1479 break;
1480 }
1481 return error;
1482 }
1483 else
1484 ptr++;
1485
1486 return LINUX_SYS_PTRACE_ARCH(l, uap, retval);
1487 #else
1488 return ENOSYS;
1489 #endif /* PTRACE || _LKM */
1490 }
1491
1492 int
1493 linux_sys_reboot(struct lwp *l, void *v, register_t *retval)
1494 {
1495 struct linux_sys_reboot_args /* {
1496 syscallarg(int) magic1;
1497 syscallarg(int) magic2;
1498 syscallarg(int) cmd;
1499 syscallarg(void *) arg;
1500 } */ *uap = v;
1501 struct sys_reboot_args /* {
1502 syscallarg(int) opt;
1503 syscallarg(char *) bootstr;
1504 } */ sra;
1505 int error;
1506
1507 if ((error = kauth_authorize_system(l->l_cred,
1508 KAUTH_SYSTEM_REBOOT, 0, NULL, NULL, NULL)) != 0)
1509 return(error);
1510
1511 if (SCARG(uap, magic1) != LINUX_REBOOT_MAGIC1)
1512 return(EINVAL);
1513 if (SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2 &&
1514 SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2A &&
1515 SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2B)
1516 return(EINVAL);
1517
1518 switch (SCARG(uap, cmd)) {
1519 case LINUX_REBOOT_CMD_RESTART:
1520 SCARG(&sra, opt) = RB_AUTOBOOT;
1521 break;
1522 case LINUX_REBOOT_CMD_HALT:
1523 SCARG(&sra, opt) = RB_HALT;
1524 break;
1525 case LINUX_REBOOT_CMD_POWER_OFF:
1526 SCARG(&sra, opt) = RB_HALT|RB_POWERDOWN;
1527 break;
1528 case LINUX_REBOOT_CMD_RESTART2:
1529 /* Reboot with an argument. */
1530 SCARG(&sra, opt) = RB_AUTOBOOT|RB_STRING;
1531 SCARG(&sra, bootstr) = SCARG(uap, arg);
1532 break;
1533 case LINUX_REBOOT_CMD_CAD_ON:
1534 return(EINVAL); /* We don't implement ctrl-alt-delete */
1535 case LINUX_REBOOT_CMD_CAD_OFF:
1536 return(0);
1537 default:
1538 return(EINVAL);
1539 }
1540
1541 return(sys_reboot(l, &sra, retval));
1542 }
1543
1544 /*
1545 * Copy of compat_12_sys_swapon().
1546 */
1547 int
1548 linux_sys_swapon(l, v, retval)
1549 struct lwp *l;
1550 void *v;
1551 register_t *retval;
1552 {
1553 struct sys_swapctl_args ua;
1554 struct linux_sys_swapon_args /* {
1555 syscallarg(const char *) name;
1556 } */ *uap = v;
1557
1558 SCARG(&ua, cmd) = SWAP_ON;
1559 SCARG(&ua, arg) = (void *)__UNCONST(SCARG(uap, name));
1560 SCARG(&ua, misc) = 0; /* priority */
1561 return (sys_swapctl(l, &ua, retval));
1562 }
1563
1564 /*
1565 * Stop swapping to the file or block device specified by path.
1566 */
1567 int
1568 linux_sys_swapoff(l, v, retval)
1569 struct lwp *l;
1570 void *v;
1571 register_t *retval;
1572 {
1573 struct sys_swapctl_args ua;
1574 struct linux_sys_swapoff_args /* {
1575 syscallarg(const char *) path;
1576 } */ *uap = v;
1577
1578 SCARG(&ua, cmd) = SWAP_OFF;
1579 SCARG(&ua, arg) = __UNCONST(SCARG(uap, path)); /*XXXUNCONST*/
1580 return (sys_swapctl(l, &ua, retval));
1581 }
1582
1583 /*
1584 * Copy of compat_09_sys_setdomainname()
1585 */
1586 /* ARGSUSED */
1587 int
1588 linux_sys_setdomainname(struct lwp *l, void *v, register_t *retval)
1589 {
1590 struct linux_sys_setdomainname_args /* {
1591 syscallarg(char *) domainname;
1592 syscallarg(int) len;
1593 } */ *uap = v;
1594 int name[2];
1595
1596 name[0] = CTL_KERN;
1597 name[1] = KERN_DOMAINNAME;
1598 return (old_sysctl(&name[0], 2, 0, 0, SCARG(uap, domainname),
1599 SCARG(uap, len), l));
1600 }
1601
1602 /*
1603 * sysinfo()
1604 */
1605 /* ARGSUSED */
1606 int
1607 linux_sys_sysinfo(struct lwp *l, void *v, register_t *retval)
1608 {
1609 struct linux_sys_sysinfo_args /* {
1610 syscallarg(struct linux_sysinfo *) arg;
1611 } */ *uap = v;
1612 struct linux_sysinfo si;
1613 struct loadavg *la;
1614
1615 si.uptime = time_uptime;
1616 la = &averunnable;
1617 si.loads[0] = la->ldavg[0] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
1618 si.loads[1] = la->ldavg[1] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
1619 si.loads[2] = la->ldavg[2] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
1620 si.totalram = ctob((u_long)physmem);
1621 si.freeram = (u_long)uvmexp.free * uvmexp.pagesize;
1622 si.sharedram = 0; /* XXX */
1623 si.bufferram = (u_long)uvmexp.filepages * uvmexp.pagesize;
1624 si.totalswap = (u_long)uvmexp.swpages * uvmexp.pagesize;
1625 si.freeswap =
1626 (u_long)(uvmexp.swpages - uvmexp.swpginuse) * uvmexp.pagesize;
1627 si.procs = nprocs;
1628
1629 /* The following are only present in newer Linux kernels. */
1630 si.totalbig = 0;
1631 si.freebig = 0;
1632 si.mem_unit = 1;
1633
1634 return (copyout(&si, SCARG(uap, arg), sizeof si));
1635 }
1636
1637 int
1638 linux_sys_getrlimit(l, v, retval)
1639 struct lwp *l;
1640 void *v;
1641 register_t *retval;
1642 {
1643 struct linux_sys_getrlimit_args /* {
1644 syscallarg(int) which;
1645 # ifdef LINUX_LARGEFILE64
1646 syscallarg(struct rlimit *) rlp;
1647 # else
1648 syscallarg(struct orlimit *) rlp;
1649 # endif
1650 } */ *uap = v;
1651 struct proc *p = l->l_proc;
1652 caddr_t sg = stackgap_init(p, 0);
1653 struct sys_getrlimit_args ap;
1654 struct rlimit rl;
1655 # ifdef LINUX_LARGEFILE64
1656 struct rlimit orl;
1657 # else
1658 struct orlimit orl;
1659 # endif
1660 int error;
1661
1662 SCARG(&ap, which) = linux_to_bsd_limit(SCARG(uap, which));
1663 if ((error = SCARG(&ap, which)) < 0)
1664 return -error;
1665 SCARG(&ap, rlp) = stackgap_alloc(p, &sg, sizeof rl);
1666 if ((error = sys_getrlimit(l, &ap, retval)) != 0)
1667 return error;
1668 if ((error = copyin(SCARG(&ap, rlp), &rl, sizeof(rl))) != 0)
1669 return error;
1670 bsd_to_linux_rlimit(&orl, &rl);
1671
1672 return copyout(&orl, SCARG(uap, rlp), sizeof(orl));
1673 }
1674
1675 int
1676 linux_sys_setrlimit(l, v, retval)
1677 struct lwp *l;
1678 void *v;
1679 register_t *retval;
1680 {
1681 struct linux_sys_setrlimit_args /* {
1682 syscallarg(int) which;
1683 # ifdef LINUX_LARGEFILE64
1684 syscallarg(struct rlimit *) rlp;
1685 # else
1686 syscallarg(struct orlimit *) rlp;
1687 # endif
1688 } */ *uap = v;
1689 struct proc *p = l->l_proc;
1690 caddr_t sg = stackgap_init(p, 0);
1691 struct sys_getrlimit_args ap;
1692 struct rlimit rl;
1693 # ifdef LINUX_LARGEFILE64
1694 struct rlimit orl;
1695 # else
1696 struct orlimit orl;
1697 # endif
1698 int error;
1699
1700 SCARG(&ap, which) = linux_to_bsd_limit(SCARG(uap, which));
1701 SCARG(&ap, rlp) = stackgap_alloc(p, &sg, sizeof rl);
1702 if ((error = SCARG(&ap, which)) < 0)
1703 return -error;
1704 if ((error = copyin(SCARG(uap, rlp), &orl, sizeof(orl))) != 0)
1705 return error;
1706 linux_to_bsd_rlimit(&rl, &orl);
1707 if ((error = copyout(&rl, SCARG(&ap, rlp), sizeof(rl))) != 0)
1708 return error;
1709 return sys_setrlimit(l, &ap, retval);
1710 }
1711
1712 # if !defined(__mips__) && !defined(__amd64__)
1713 /* XXX: this doesn't look 100% common, at least mips doesn't have it */
1714 int
1715 linux_sys_ugetrlimit(l, v, retval)
1716 struct lwp *l;
1717 void *v;
1718 register_t *retval;
1719 {
1720 return linux_sys_getrlimit(l, v, retval);
1721 }
1722 # endif
1723
1724 /*
1725 * This gets called for unsupported syscalls. The difference to sys_nosys()
1726 * is that process does not get SIGSYS, the call just returns with ENOSYS.
1727 * This is the way Linux does it and glibc depends on this behaviour.
1728 */
1729 int
1730 linux_sys_nosys(struct lwp *l, void *v,
1731 register_t *retval)
1732 {
1733 return (ENOSYS);
1734 }
1735
1736 int
1737 linux_sys_getpriority(l, v, retval)
1738 struct lwp *l;
1739 void *v;
1740 register_t *retval;
1741 {
1742 struct linux_sys_getpriority_args /* {
1743 syscallarg(int) which;
1744 syscallarg(int) who;
1745 } */ *uap = v;
1746 struct sys_getpriority_args bsa;
1747 int error;
1748
1749 SCARG(&bsa, which) = SCARG(uap, which);
1750 SCARG(&bsa, who) = SCARG(uap, who);
1751
1752 if ((error = sys_getpriority(l, &bsa, retval)))
1753 return error;
1754
1755 *retval = NZERO - *retval;
1756
1757 return 0;
1758 }
1759
1760 #endif /* !COMPAT_LINUX32 */
1761