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