linux_misc.c revision 1.166 1 /* $NetBSD: linux_misc.c,v 1.166 2007/02/09 21:55:19 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.166 2007/02/09 21:55:19 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/syscall.h>
108 #include <sys/syscallargs.h>
109
110 #include <compat/linux/common/linux_machdep.h>
111 #include <compat/linux/common/linux_types.h>
112 #include <compat/linux/common/linux_signal.h>
113
114 #include <compat/linux/linux_syscallargs.h>
115
116 #include <compat/linux/common/linux_fcntl.h>
117 #include <compat/linux/common/linux_mmap.h>
118 #include <compat/linux/common/linux_dirent.h>
119 #include <compat/linux/common/linux_util.h>
120 #include <compat/linux/common/linux_misc.h>
121 #ifndef COMPAT_LINUX32
122 #include <compat/linux/common/linux_limit.h>
123 #endif
124 #include <compat/linux/common/linux_ptrace.h>
125 #include <compat/linux/common/linux_reboot.h>
126 #include <compat/linux/common/linux_emuldata.h>
127
128 #ifndef COMPAT_LINUX32
129 const int linux_ptrace_request_map[] = {
130 LINUX_PTRACE_TRACEME, PT_TRACE_ME,
131 LINUX_PTRACE_PEEKTEXT, PT_READ_I,
132 LINUX_PTRACE_PEEKDATA, PT_READ_D,
133 LINUX_PTRACE_POKETEXT, PT_WRITE_I,
134 LINUX_PTRACE_POKEDATA, PT_WRITE_D,
135 LINUX_PTRACE_CONT, PT_CONTINUE,
136 LINUX_PTRACE_KILL, PT_KILL,
137 LINUX_PTRACE_ATTACH, PT_ATTACH,
138 LINUX_PTRACE_DETACH, PT_DETACH,
139 # ifdef PT_STEP
140 LINUX_PTRACE_SINGLESTEP, PT_STEP,
141 # endif
142 -1
143 };
144
145 const struct linux_mnttypes linux_fstypes[] = {
146 { MOUNT_FFS, LINUX_DEFAULT_SUPER_MAGIC },
147 { MOUNT_NFS, LINUX_NFS_SUPER_MAGIC },
148 { MOUNT_MFS, LINUX_DEFAULT_SUPER_MAGIC },
149 { MOUNT_MSDOS, LINUX_MSDOS_SUPER_MAGIC },
150 { MOUNT_LFS, LINUX_DEFAULT_SUPER_MAGIC },
151 { MOUNT_FDESC, LINUX_DEFAULT_SUPER_MAGIC },
152 { MOUNT_PORTAL, LINUX_DEFAULT_SUPER_MAGIC },
153 { MOUNT_NULL, LINUX_DEFAULT_SUPER_MAGIC },
154 { MOUNT_OVERLAY, LINUX_DEFAULT_SUPER_MAGIC },
155 { MOUNT_UMAP, LINUX_DEFAULT_SUPER_MAGIC },
156 { MOUNT_KERNFS, LINUX_DEFAULT_SUPER_MAGIC },
157 { MOUNT_PROCFS, LINUX_PROC_SUPER_MAGIC },
158 { MOUNT_AFS, LINUX_DEFAULT_SUPER_MAGIC },
159 { MOUNT_CD9660, LINUX_ISOFS_SUPER_MAGIC },
160 { MOUNT_UNION, LINUX_DEFAULT_SUPER_MAGIC },
161 { MOUNT_ADOSFS, LINUX_ADFS_SUPER_MAGIC },
162 { MOUNT_EXT2FS, LINUX_EXT2_SUPER_MAGIC },
163 { MOUNT_CFS, LINUX_DEFAULT_SUPER_MAGIC },
164 { MOUNT_CODA, LINUX_CODA_SUPER_MAGIC },
165 { MOUNT_FILECORE, LINUX_DEFAULT_SUPER_MAGIC },
166 { MOUNT_NTFS, LINUX_DEFAULT_SUPER_MAGIC },
167 { MOUNT_SMBFS, LINUX_SMB_SUPER_MAGIC },
168 { MOUNT_PTYFS, LINUX_DEVPTS_SUPER_MAGIC },
169 { MOUNT_TMPFS, LINUX_DEFAULT_SUPER_MAGIC }
170 };
171 const int linux_fstypes_cnt = sizeof(linux_fstypes) / sizeof(linux_fstypes[0]);
172
173 # ifdef DEBUG_LINUX
174 #define DPRINTF(a) uprintf a
175 # else
176 #define DPRINTF(a)
177 # endif
178
179 /* Local linux_misc.c functions: */
180 # ifndef __amd64__
181 static void bsd_to_linux_statfs __P((const struct statvfs *,
182 struct linux_statfs *));
183 # endif
184 static void linux_to_bsd_mmap_args __P((struct sys_mmap_args *,
185 const struct linux_sys_mmap_args *));
186 static int linux_mmap __P((struct lwp *, struct linux_sys_mmap_args *,
187 register_t *, off_t));
188
189
190 /*
191 * The information on a terminated (or stopped) process needs
192 * to be converted in order for Linux binaries to get a valid signal
193 * number out of it.
194 */
195 void
196 bsd_to_linux_wstat(st)
197 int *st;
198 {
199
200 int sig;
201
202 if (WIFSIGNALED(*st)) {
203 sig = WTERMSIG(*st);
204 if (sig >= 0 && sig < NSIG)
205 *st= (*st& ~0177) | native_to_linux_signo[sig];
206 } else if (WIFSTOPPED(*st)) {
207 sig = WSTOPSIG(*st);
208 if (sig >= 0 && sig < NSIG)
209 *st = (*st & ~0xff00) |
210 (native_to_linux_signo[sig] << 8);
211 }
212 }
213
214 /*
215 * wait4(2). Passed on to the NetBSD call, surrounded by code to
216 * reserve some space for a NetBSD-style wait status, and converting
217 * it to what Linux wants.
218 */
219 int
220 linux_sys_wait4(l, v, retval)
221 struct lwp *l;
222 void *v;
223 register_t *retval;
224 {
225 struct linux_sys_wait4_args /* {
226 syscallarg(int) pid;
227 syscallarg(int *) status;
228 syscallarg(int) options;
229 syscallarg(struct rusage *) rusage;
230 } */ *uap = v;
231 struct proc *p = l->l_proc;
232 struct sys_wait4_args w4a;
233 int error, *status, tstat, options, linux_options;
234 caddr_t sg;
235
236 if (SCARG(uap, status) != NULL) {
237 sg = stackgap_init(p, 0);
238 status = (int *) stackgap_alloc(p, &sg, sizeof *status);
239 } else
240 status = NULL;
241
242 linux_options = SCARG(uap, options);
243 options = 0;
244 if (linux_options & ~(LINUX_WAIT4_KNOWNFLAGS))
245 return (EINVAL);
246
247 if (linux_options & LINUX_WAIT4_WNOHANG)
248 options |= WNOHANG;
249 if (linux_options & LINUX_WAIT4_WUNTRACED)
250 options |= WUNTRACED;
251 if (linux_options & LINUX_WAIT4_WALL)
252 options |= WALLSIG;
253 if (linux_options & LINUX_WAIT4_WCLONE)
254 options |= WALTSIG;
255 # ifdef DIAGNOSTIC
256 if (linux_options & LINUX_WAIT4_WNOTHREAD)
257 printf("WARNING: %s: linux process %d.%d called "
258 "waitpid with __WNOTHREAD set!",
259 __FILE__, p->p_pid, l->l_lid);
260
261 # endif
262
263 SCARG(&w4a, pid) = SCARG(uap, pid);
264 SCARG(&w4a, status) = status;
265 SCARG(&w4a, options) = options;
266 SCARG(&w4a, rusage) = SCARG(uap, rusage);
267
268 if ((error = sys_wait4(l, &w4a, retval)))
269 return error;
270
271 sigdelset(&p->p_sigpend.sp_set, SIGCHLD); /* XXXAD ksiginfo leak */
272
273 if (status != NULL) {
274 if ((error = copyin(status, &tstat, sizeof tstat)))
275 return error;
276
277 bsd_to_linux_wstat(&tstat);
278 return copyout(&tstat, SCARG(uap, status), sizeof tstat);
279 }
280
281 return 0;
282 }
283
284 /*
285 * Linux brk(2). The check if the new address is >= the old one is
286 * done in the kernel in Linux. NetBSD does it in the library.
287 */
288 int
289 linux_sys_brk(l, v, retval)
290 struct lwp *l;
291 void *v;
292 register_t *retval;
293 {
294 struct linux_sys_brk_args /* {
295 syscallarg(char *) nsize;
296 } */ *uap = v;
297 struct proc *p = l->l_proc;
298 char *nbrk = SCARG(uap, nsize);
299 struct sys_obreak_args oba;
300 struct vmspace *vm = p->p_vmspace;
301 struct linux_emuldata *ed = (struct linux_emuldata*)p->p_emuldata;
302
303 SCARG(&oba, nsize) = nbrk;
304
305 if ((caddr_t) nbrk > vm->vm_daddr && sys_obreak(l, &oba, retval) == 0)
306 ed->s->p_break = (char*)nbrk;
307 else
308 nbrk = ed->s->p_break;
309
310 retval[0] = (register_t)nbrk;
311
312 return 0;
313 }
314
315 # ifndef __amd64__
316 /*
317 * Convert NetBSD statvfs structure to Linux statfs structure.
318 * Linux doesn't have f_flag, and we can't set f_frsize due
319 * to glibc statvfs() bug (see below).
320 */
321 static void
322 bsd_to_linux_statfs(bsp, lsp)
323 const struct statvfs *bsp;
324 struct linux_statfs *lsp;
325 {
326 int i;
327
328 for (i = 0; i < linux_fstypes_cnt; i++) {
329 if (strcmp(bsp->f_fstypename, linux_fstypes[i].bsd) == 0) {
330 lsp->l_ftype = linux_fstypes[i].linux;
331 break;
332 }
333 }
334
335 if (i == linux_fstypes_cnt) {
336 DPRINTF(("unhandled fstype in linux emulation: %s\n",
337 bsp->f_fstypename));
338 lsp->l_ftype = LINUX_DEFAULT_SUPER_MAGIC;
339 }
340
341 /*
342 * The sizes are expressed in number of blocks. The block
343 * size used for the size is f_frsize for POSIX-compliant
344 * statvfs. Linux statfs uses f_bsize as the block size
345 * (f_frsize used to not be available in Linux struct statfs).
346 * However, glibc 2.3.3 statvfs() wrapper fails to adjust the block
347 * counts for different f_frsize if f_frsize is provided by the kernel.
348 * POSIX conforming apps thus get wrong size if f_frsize
349 * is different to f_bsize. Thus, we just pretend we don't
350 * support f_frsize.
351 */
352
353 lsp->l_fbsize = bsp->f_frsize;
354 lsp->l_ffrsize = 0; /* compat */
355 lsp->l_fblocks = bsp->f_blocks;
356 lsp->l_fbfree = bsp->f_bfree;
357 lsp->l_fbavail = bsp->f_bavail;
358 lsp->l_ffiles = bsp->f_files;
359 lsp->l_fffree = bsp->f_ffree;
360 /* Linux sets the fsid to 0..., we don't */
361 lsp->l_ffsid.val[0] = bsp->f_fsidx.__fsid_val[0];
362 lsp->l_ffsid.val[1] = bsp->f_fsidx.__fsid_val[1];
363 lsp->l_fnamelen = bsp->f_namemax;
364 (void)memset(lsp->l_fspare, 0, sizeof(lsp->l_fspare));
365 }
366
367 /*
368 * Implement the fs stat functions. Straightforward.
369 */
370 int
371 linux_sys_statfs(l, v, retval)
372 struct lwp *l;
373 void *v;
374 register_t *retval;
375 {
376 struct linux_sys_statfs_args /* {
377 syscallarg(const char *) path;
378 syscallarg(struct linux_statfs *) sp;
379 } */ *uap = v;
380 struct proc *p = l->l_proc;
381 struct statvfs *btmp, *bsp;
382 struct linux_statfs ltmp;
383 struct sys_statvfs1_args bsa;
384 caddr_t sg;
385 int error;
386
387 sg = stackgap_init(p, 0);
388 bsp = stackgap_alloc(p, &sg, sizeof (struct statvfs));
389
390 CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
391
392 SCARG(&bsa, path) = SCARG(uap, path);
393 SCARG(&bsa, buf) = bsp;
394 SCARG(&bsa, flags) = ST_WAIT;
395
396 if ((error = sys_statvfs1(l, &bsa, retval)))
397 return error;
398
399 btmp = STATVFSBUF_GET();
400 error = copyin(bsp, btmp, sizeof(*btmp));
401 if (error) {
402 goto out;
403 }
404 bsd_to_linux_statfs(btmp, <mp);
405 error = copyout(<mp, SCARG(uap, sp), sizeof ltmp);
406 out:
407 STATVFSBUF_PUT(btmp);
408 return error;
409 }
410
411 int
412 linux_sys_fstatfs(l, v, retval)
413 struct lwp *l;
414 void *v;
415 register_t *retval;
416 {
417 struct linux_sys_fstatfs_args /* {
418 syscallarg(int) fd;
419 syscallarg(struct linux_statfs *) sp;
420 } */ *uap = v;
421 struct proc *p = l->l_proc;
422 struct statvfs *btmp, *bsp;
423 struct linux_statfs ltmp;
424 struct sys_fstatvfs1_args bsa;
425 caddr_t sg;
426 int error;
427
428 sg = stackgap_init(p, 0);
429 bsp = stackgap_alloc(p, &sg, sizeof (struct statvfs));
430
431 SCARG(&bsa, fd) = SCARG(uap, fd);
432 SCARG(&bsa, buf) = bsp;
433 SCARG(&bsa, flags) = ST_WAIT;
434
435 if ((error = sys_fstatvfs1(l, &bsa, retval)))
436 return error;
437
438 btmp = STATVFSBUF_GET();
439 error = copyin(bsp, btmp, sizeof(*btmp));
440 if (error) {
441 goto out;
442 }
443 bsd_to_linux_statfs(btmp, <mp);
444 error = copyout(<mp, SCARG(uap, sp), sizeof ltmp);
445 out:
446 STATVFSBUF_PUT(btmp);
447 return error;
448 }
449 # endif /* !__amd64__ */
450
451 /*
452 * uname(). Just copy the info from the various strings stored in the
453 * kernel, and put it in the Linux utsname structure. That structure
454 * is almost the same as the NetBSD one, only it has fields 65 characters
455 * long, and an extra domainname field.
456 */
457 int
458 linux_sys_uname(struct lwp *l, void *v, register_t *retval)
459 {
460 struct linux_sys_uname_args /* {
461 syscallarg(struct linux_utsname *) up;
462 } */ *uap = v;
463 struct linux_utsname luts;
464
465 strncpy(luts.l_sysname, linux_sysname, sizeof(luts.l_sysname));
466 strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
467 strncpy(luts.l_release, linux_release, sizeof(luts.l_release));
468 strncpy(luts.l_version, linux_version, sizeof(luts.l_version));
469 # ifdef LINUX_UNAME_ARCH
470 strncpy(luts.l_machine, LINUX_UNAME_ARCH, sizeof(luts.l_machine));
471 # else
472 strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
473 # endif
474 strncpy(luts.l_domainname, domainname, sizeof(luts.l_domainname));
475
476 return copyout(&luts, SCARG(uap, up), sizeof(luts));
477 }
478
479 /* Used directly on: alpha, mips, ppc, sparc, sparc64 */
480 /* Used indirectly on: arm, i386, m68k */
481
482 /*
483 * New type Linux mmap call.
484 * Only called directly on machines with >= 6 free regs.
485 */
486 int
487 linux_sys_mmap(l, v, retval)
488 struct lwp *l;
489 void *v;
490 register_t *retval;
491 {
492 struct linux_sys_mmap_args /* {
493 syscallarg(unsigned long) addr;
494 syscallarg(size_t) len;
495 syscallarg(int) prot;
496 syscallarg(int) flags;
497 syscallarg(int) fd;
498 syscallarg(linux_off_t) offset;
499 } */ *uap = v;
500
501 if (SCARG(uap, offset) & PAGE_MASK)
502 return EINVAL;
503
504 return linux_mmap(l, uap, retval, SCARG(uap, offset));
505 }
506
507 /*
508 * Guts of most architectures' mmap64() implementations. This shares
509 * its list of arguments with linux_sys_mmap().
510 *
511 * The difference in linux_sys_mmap2() is that "offset" is actually
512 * (offset / pagesize), not an absolute byte count. This translation
513 * to pagesize offsets is done inside glibc between the mmap64() call
514 * point, and the actual syscall.
515 */
516 int
517 linux_sys_mmap2(l, v, retval)
518 struct lwp *l;
519 void *v;
520 register_t *retval;
521 {
522 struct linux_sys_mmap2_args /* {
523 syscallarg(unsigned long) addr;
524 syscallarg(size_t) len;
525 syscallarg(int) prot;
526 syscallarg(int) flags;
527 syscallarg(int) fd;
528 syscallarg(linux_off_t) offset;
529 } */ *uap = v;
530
531 return linux_mmap(l, uap, retval,
532 ((off_t)SCARG(uap, offset)) << PAGE_SHIFT);
533 }
534
535 /*
536 * Massage arguments and call system mmap(2).
537 */
538 static int
539 linux_mmap(l, uap, retval, offset)
540 struct lwp *l;
541 struct linux_sys_mmap_args *uap;
542 register_t *retval;
543 off_t offset;
544 {
545 struct sys_mmap_args cma;
546 int error;
547 size_t mmoff=0;
548
549 if (SCARG(uap, flags) & LINUX_MAP_GROWSDOWN) {
550 /*
551 * Request for stack-like memory segment. On linux, this
552 * works by mmap()ping (small) segment, which is automatically
553 * extended when page fault happens below the currently
554 * allocated area. We emulate this by allocating (typically
555 * bigger) segment sized at current stack size limit, and
556 * offsetting the requested and returned address accordingly.
557 * Since physical pages are only allocated on-demand, this
558 * is effectively identical.
559 */
560 rlim_t ssl = l->l_proc->p_rlimit[RLIMIT_STACK].rlim_cur;
561
562 if (SCARG(uap, len) < ssl) {
563 /* Compute the address offset */
564 mmoff = round_page(ssl) - SCARG(uap, len);
565
566 if (SCARG(uap, addr))
567 SCARG(uap, addr) -= mmoff;
568
569 SCARG(uap, len) = (size_t) ssl;
570 }
571 }
572
573 linux_to_bsd_mmap_args(&cma, uap);
574 SCARG(&cma, pos) = offset;
575
576 error = sys_mmap(l, &cma, retval);
577 if (error)
578 return (error);
579
580 /* Shift the returned address for stack-like segment if necessary */
581 if (SCARG(uap, flags) & LINUX_MAP_GROWSDOWN && mmoff)
582 retval[0] += mmoff;
583
584 return (0);
585 }
586
587 static void
588 linux_to_bsd_mmap_args(cma, uap)
589 struct sys_mmap_args *cma;
590 const struct linux_sys_mmap_args *uap;
591 {
592 int flags = MAP_TRYFIXED, fl = SCARG(uap, flags);
593
594 flags |= cvtto_bsd_mask(fl, LINUX_MAP_SHARED, MAP_SHARED);
595 flags |= cvtto_bsd_mask(fl, LINUX_MAP_PRIVATE, MAP_PRIVATE);
596 flags |= cvtto_bsd_mask(fl, LINUX_MAP_FIXED, MAP_FIXED);
597 flags |= cvtto_bsd_mask(fl, LINUX_MAP_ANON, MAP_ANON);
598 /* XXX XAX ERH: Any other flags here? There are more defined... */
599
600 SCARG(cma, addr) = (void *)SCARG(uap, addr);
601 SCARG(cma, len) = SCARG(uap, len);
602 SCARG(cma, prot) = SCARG(uap, prot);
603 if (SCARG(cma, prot) & VM_PROT_WRITE) /* XXX */
604 SCARG(cma, prot) |= VM_PROT_READ;
605 SCARG(cma, flags) = flags;
606 SCARG(cma, fd) = flags & MAP_ANON ? -1 : SCARG(uap, fd);
607 SCARG(cma, pad) = 0;
608 }
609
610 #define LINUX_MREMAP_MAYMOVE 1
611 #define LINUX_MREMAP_FIXED 2
612
613 int
614 linux_sys_mremap(l, v, retval)
615 struct lwp *l;
616 void *v;
617 register_t *retval;
618 {
619 struct linux_sys_mremap_args /* {
620 syscallarg(void *) old_address;
621 syscallarg(size_t) old_size;
622 syscallarg(size_t) new_size;
623 syscallarg(u_long) flags;
624 } */ *uap = v;
625
626 struct proc *p;
627 struct vm_map *map;
628 vaddr_t oldva;
629 vaddr_t newva;
630 size_t oldsize;
631 size_t newsize;
632 int flags;
633 int uvmflags;
634 int error;
635
636 flags = SCARG(uap, flags);
637 oldva = (vaddr_t)SCARG(uap, old_address);
638 oldsize = round_page(SCARG(uap, old_size));
639 newsize = round_page(SCARG(uap, new_size));
640 if ((flags & ~(LINUX_MREMAP_FIXED|LINUX_MREMAP_MAYMOVE)) != 0) {
641 error = EINVAL;
642 goto done;
643 }
644 if ((flags & LINUX_MREMAP_FIXED) != 0) {
645 if ((flags & LINUX_MREMAP_MAYMOVE) == 0) {
646 error = EINVAL;
647 goto done;
648 }
649 #if 0 /* notyet */
650 newva = SCARG(uap, new_address);
651 uvmflags = UVM_MREMAP_FIXED;
652 #else /* notyet */
653 error = EOPNOTSUPP;
654 goto done;
655 #endif /* notyet */
656 } else if ((flags & LINUX_MREMAP_MAYMOVE) != 0) {
657 uvmflags = 0;
658 } else {
659 newva = oldva;
660 uvmflags = UVM_MREMAP_FIXED;
661 }
662 p = l->l_proc;
663 map = &p->p_vmspace->vm_map;
664 error = uvm_mremap(map, oldva, oldsize, map, &newva, newsize, p,
665 uvmflags);
666
667 done:
668 *retval = (error != 0) ? 0 : (register_t)newva;
669 return error;
670 }
671
672 int
673 linux_sys_msync(l, v, retval)
674 struct lwp *l;
675 void *v;
676 register_t *retval;
677 {
678 struct linux_sys_msync_args /* {
679 syscallarg(caddr_t) addr;
680 syscallarg(int) len;
681 syscallarg(int) fl;
682 } */ *uap = v;
683
684 struct sys___msync13_args bma;
685
686 /* flags are ignored */
687 SCARG(&bma, addr) = SCARG(uap, addr);
688 SCARG(&bma, len) = SCARG(uap, len);
689 SCARG(&bma, flags) = SCARG(uap, fl);
690
691 return sys___msync13(l, &bma, retval);
692 }
693
694 int
695 linux_sys_mprotect(struct lwp *l, void *v, register_t *retval)
696 {
697 struct linux_sys_mprotect_args /* {
698 syscallarg(const void *) start;
699 syscallarg(unsigned long) len;
700 syscallarg(int) prot;
701 } */ *uap = v;
702 struct vm_map_entry *entry;
703 struct vm_map *map;
704 struct proc *p;
705 vaddr_t end, start, len, stacklim;
706 int prot, grows;
707
708 start = (vaddr_t)SCARG(uap, start);
709 len = round_page(SCARG(uap, len));
710 prot = SCARG(uap, prot);
711 grows = prot & (LINUX_PROT_GROWSDOWN | LINUX_PROT_GROWSUP);
712 prot &= ~grows;
713 end = start + len;
714
715 if (start & PAGE_MASK)
716 return EINVAL;
717 if (end < start)
718 return EINVAL;
719 if (end == start)
720 return 0;
721
722 if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
723 return EINVAL;
724 if (grows == (LINUX_PROT_GROWSDOWN | LINUX_PROT_GROWSUP))
725 return EINVAL;
726
727 p = l->l_proc;
728 map = &p->p_vmspace->vm_map;
729 vm_map_lock(map);
730 # ifdef notdef
731 VM_MAP_RANGE_CHECK(map, start, end);
732 # endif
733 if (!uvm_map_lookup_entry(map, start, &entry) || entry->start > start) {
734 vm_map_unlock(map);
735 return ENOMEM;
736 }
737
738 /*
739 * Approximate the behaviour of PROT_GROWS{DOWN,UP}.
740 */
741
742 stacklim = (vaddr_t)p->p_limit->pl_rlimit[RLIMIT_STACK].rlim_cur;
743 if (grows & LINUX_PROT_GROWSDOWN) {
744 if (USRSTACK - stacklim <= start && start < USRSTACK) {
745 start = USRSTACK - stacklim;
746 } else {
747 start = entry->start;
748 }
749 } else if (grows & LINUX_PROT_GROWSUP) {
750 if (USRSTACK <= end && end < USRSTACK + stacklim) {
751 end = USRSTACK + stacklim;
752 } else {
753 end = entry->end;
754 }
755 }
756 vm_map_unlock(map);
757 return uvm_map_protect(map, start, end, prot, FALSE);
758 }
759
760 /*
761 * This code is partly stolen from src/lib/libc/compat-43/times.c
762 */
763
764 #define CONVTCK(r) (r.tv_sec * hz + r.tv_usec / (1000000 / hz))
765
766 int
767 linux_sys_times(l, v, retval)
768 struct lwp *l;
769 void *v;
770 register_t *retval;
771 {
772 struct linux_sys_times_args /* {
773 syscallarg(struct times *) tms;
774 } */ *uap = v;
775 struct proc *p = l->l_proc;
776 struct timeval t;
777 int error;
778
779 if (SCARG(uap, tms)) {
780 struct linux_tms ltms;
781 struct rusage ru;
782
783 mutex_enter(&p->p_smutex);
784 calcru(p, &ru.ru_utime, &ru.ru_stime, NULL, NULL);
785 ltms.ltms_utime = CONVTCK(ru.ru_utime);
786 ltms.ltms_stime = CONVTCK(ru.ru_stime);
787 ltms.ltms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
788 ltms.ltms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
789 mutex_exit(&p->p_smutex);
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, 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)
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)
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_system(l->l_cred,
1507 KAUTH_SYSTEM_REBOOT, 0, NULL, NULL, NULL)) != 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)
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, void *v, register_t *retval)
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, void *v,
1730 register_t *retval)
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