linux_misc.c revision 1.160 1 /* $NetBSD: linux_misc.c,v 1.160 2006/08/30 17:14:34 he 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.160 2006/08/30 17:14:34 he 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/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_sigctx.ps_siglist, SIGCHLD);
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(l, v, retval)
459 struct lwp *l;
460 void *v;
461 register_t *retval;
462 {
463 struct linux_sys_uname_args /* {
464 syscallarg(struct linux_utsname *) up;
465 } */ *uap = v;
466 struct linux_utsname luts;
467
468 strncpy(luts.l_sysname, linux_sysname, sizeof(luts.l_sysname));
469 strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
470 strncpy(luts.l_release, linux_release, sizeof(luts.l_release));
471 strncpy(luts.l_version, linux_version, sizeof(luts.l_version));
472 # ifdef LINUX_UNAME_ARCH
473 strncpy(luts.l_machine, LINUX_UNAME_ARCH, sizeof(luts.l_machine));
474 # else
475 strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
476 # endif
477 strncpy(luts.l_domainname, domainname, sizeof(luts.l_domainname));
478
479 return copyout(&luts, SCARG(uap, up), sizeof(luts));
480 }
481
482 /* Used directly on: alpha, mips, ppc, sparc, sparc64 */
483 /* Used indirectly on: arm, i386, m68k */
484
485 /*
486 * New type Linux mmap call.
487 * Only called directly on machines with >= 6 free regs.
488 */
489 int
490 linux_sys_mmap(l, v, retval)
491 struct lwp *l;
492 void *v;
493 register_t *retval;
494 {
495 struct linux_sys_mmap_args /* {
496 syscallarg(unsigned long) addr;
497 syscallarg(size_t) len;
498 syscallarg(int) prot;
499 syscallarg(int) flags;
500 syscallarg(int) fd;
501 syscallarg(linux_off_t) offset;
502 } */ *uap = v;
503
504 if (SCARG(uap, offset) & PAGE_MASK)
505 return EINVAL;
506
507 return linux_mmap(l, uap, retval, SCARG(uap, offset));
508 }
509
510 /*
511 * Guts of most architectures' mmap64() implementations. This shares
512 * its list of arguments with linux_sys_mmap().
513 *
514 * The difference in linux_sys_mmap2() is that "offset" is actually
515 * (offset / pagesize), not an absolute byte count. This translation
516 * to pagesize offsets is done inside glibc between the mmap64() call
517 * point, and the actual syscall.
518 */
519 int
520 linux_sys_mmap2(l, v, retval)
521 struct lwp *l;
522 void *v;
523 register_t *retval;
524 {
525 struct linux_sys_mmap2_args /* {
526 syscallarg(unsigned long) addr;
527 syscallarg(size_t) len;
528 syscallarg(int) prot;
529 syscallarg(int) flags;
530 syscallarg(int) fd;
531 syscallarg(linux_off_t) offset;
532 } */ *uap = v;
533
534 return linux_mmap(l, uap, retval,
535 ((off_t)SCARG(uap, offset)) << PAGE_SHIFT);
536 }
537
538 /*
539 * Massage arguments and call system mmap(2).
540 */
541 static int
542 linux_mmap(l, uap, retval, offset)
543 struct lwp *l;
544 struct linux_sys_mmap_args *uap;
545 register_t *retval;
546 off_t offset;
547 {
548 struct sys_mmap_args cma;
549 int error;
550 size_t mmoff=0;
551
552 if (SCARG(uap, flags) & LINUX_MAP_GROWSDOWN) {
553 /*
554 * Request for stack-like memory segment. On linux, this
555 * works by mmap()ping (small) segment, which is automatically
556 * extended when page fault happens below the currently
557 * allocated area. We emulate this by allocating (typically
558 * bigger) segment sized at current stack size limit, and
559 * offsetting the requested and returned address accordingly.
560 * Since physical pages are only allocated on-demand, this
561 * is effectively identical.
562 */
563 rlim_t ssl = l->l_proc->p_rlimit[RLIMIT_STACK].rlim_cur;
564
565 if (SCARG(uap, len) < ssl) {
566 /* Compute the address offset */
567 mmoff = round_page(ssl) - SCARG(uap, len);
568
569 if (SCARG(uap, addr))
570 SCARG(uap, addr) -= mmoff;
571
572 SCARG(uap, len) = (size_t) ssl;
573 }
574 }
575
576 linux_to_bsd_mmap_args(&cma, uap);
577 SCARG(&cma, pos) = offset;
578
579 error = sys_mmap(l, &cma, retval);
580 if (error)
581 return (error);
582
583 /* Shift the returned address for stack-like segment if necessary */
584 if (SCARG(uap, flags) & LINUX_MAP_GROWSDOWN && mmoff)
585 retval[0] += mmoff;
586
587 return (0);
588 }
589
590 static void
591 linux_to_bsd_mmap_args(cma, uap)
592 struct sys_mmap_args *cma;
593 const struct linux_sys_mmap_args *uap;
594 {
595 int flags = MAP_TRYFIXED, fl = SCARG(uap, flags);
596
597 flags |= cvtto_bsd_mask(fl, LINUX_MAP_SHARED, MAP_SHARED);
598 flags |= cvtto_bsd_mask(fl, LINUX_MAP_PRIVATE, MAP_PRIVATE);
599 flags |= cvtto_bsd_mask(fl, LINUX_MAP_FIXED, MAP_FIXED);
600 flags |= cvtto_bsd_mask(fl, LINUX_MAP_ANON, MAP_ANON);
601 /* XXX XAX ERH: Any other flags here? There are more defined... */
602
603 SCARG(cma, addr) = (void *)SCARG(uap, addr);
604 SCARG(cma, len) = SCARG(uap, len);
605 SCARG(cma, prot) = SCARG(uap, prot);
606 if (SCARG(cma, prot) & VM_PROT_WRITE) /* XXX */
607 SCARG(cma, prot) |= VM_PROT_READ;
608 SCARG(cma, flags) = flags;
609 SCARG(cma, fd) = flags & MAP_ANON ? -1 : SCARG(uap, fd);
610 SCARG(cma, pad) = 0;
611 }
612
613 #define LINUX_MREMAP_MAYMOVE 1
614 #define LINUX_MREMAP_FIXED 2
615
616 int
617 linux_sys_mremap(l, v, retval)
618 struct lwp *l;
619 void *v;
620 register_t *retval;
621 {
622 struct linux_sys_mremap_args /* {
623 syscallarg(void *) old_address;
624 syscallarg(size_t) old_size;
625 syscallarg(size_t) new_size;
626 syscallarg(u_long) flags;
627 } */ *uap = v;
628
629 struct proc *p;
630 struct vm_map *map;
631 vaddr_t oldva;
632 vaddr_t newva;
633 size_t oldsize;
634 size_t newsize;
635 int flags;
636 int uvmflags;
637 int error;
638
639 flags = SCARG(uap, flags);
640 oldva = (vaddr_t)SCARG(uap, old_address);
641 oldsize = round_page(SCARG(uap, old_size));
642 newsize = round_page(SCARG(uap, new_size));
643 if ((flags & ~(LINUX_MREMAP_FIXED|LINUX_MREMAP_MAYMOVE)) != 0) {
644 error = EINVAL;
645 goto done;
646 }
647 if ((flags & LINUX_MREMAP_FIXED) != 0) {
648 if ((flags & LINUX_MREMAP_MAYMOVE) == 0) {
649 error = EINVAL;
650 goto done;
651 }
652 #if 0 /* notyet */
653 newva = SCARG(uap, new_address);
654 uvmflags = UVM_MREMAP_FIXED;
655 #else /* notyet */
656 error = EOPNOTSUPP;
657 goto done;
658 #endif /* notyet */
659 } else if ((flags & LINUX_MREMAP_MAYMOVE) != 0) {
660 uvmflags = 0;
661 } else {
662 newva = oldva;
663 uvmflags = UVM_MREMAP_FIXED;
664 }
665 p = l->l_proc;
666 map = &p->p_vmspace->vm_map;
667 error = uvm_mremap(map, oldva, oldsize, map, &newva, newsize, p,
668 uvmflags);
669
670 done:
671 *retval = (error != 0) ? 0 : (register_t)newva;
672 return error;
673 }
674
675 int
676 linux_sys_msync(l, v, retval)
677 struct lwp *l;
678 void *v;
679 register_t *retval;
680 {
681 struct linux_sys_msync_args /* {
682 syscallarg(caddr_t) addr;
683 syscallarg(int) len;
684 syscallarg(int) fl;
685 } */ *uap = v;
686
687 struct sys___msync13_args bma;
688
689 /* flags are ignored */
690 SCARG(&bma, addr) = SCARG(uap, addr);
691 SCARG(&bma, len) = SCARG(uap, len);
692 SCARG(&bma, flags) = SCARG(uap, fl);
693
694 return sys___msync13(l, &bma, retval);
695 }
696
697 int
698 linux_sys_mprotect(l, v, retval)
699 struct lwp *l;
700 void *v;
701 register_t *retval;
702 {
703 struct linux_sys_mprotect_args /* {
704 syscallarg(const void *) start;
705 syscallarg(unsigned long) len;
706 syscallarg(int) prot;
707 } */ *uap = v;
708 struct vm_map_entry *entry;
709 struct vm_map *map;
710 struct proc *p;
711 vaddr_t end, start, len, stacklim;
712 int prot, grows;
713
714 start = (vaddr_t)SCARG(uap, start);
715 len = round_page(SCARG(uap, len));
716 prot = SCARG(uap, prot);
717 grows = prot & (LINUX_PROT_GROWSDOWN | LINUX_PROT_GROWSUP);
718 prot &= ~grows;
719 end = start + len;
720
721 if (start & PAGE_MASK)
722 return EINVAL;
723 if (end < start)
724 return EINVAL;
725 if (end == start)
726 return 0;
727
728 if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
729 return EINVAL;
730 if (grows == (LINUX_PROT_GROWSDOWN | LINUX_PROT_GROWSUP))
731 return EINVAL;
732
733 p = l->l_proc;
734 map = &p->p_vmspace->vm_map;
735 vm_map_lock(map);
736 # ifdef notdef
737 VM_MAP_RANGE_CHECK(map, start, end);
738 # endif
739 if (!uvm_map_lookup_entry(map, start, &entry) || entry->start > start) {
740 vm_map_unlock(map);
741 return ENOMEM;
742 }
743
744 /*
745 * Approximate the behaviour of PROT_GROWS{DOWN,UP}.
746 */
747
748 stacklim = (vaddr_t)p->p_limit->pl_rlimit[RLIMIT_STACK].rlim_cur;
749 if (grows & LINUX_PROT_GROWSDOWN) {
750 if (USRSTACK - stacklim <= start && start < USRSTACK) {
751 start = USRSTACK - stacklim;
752 } else {
753 start = entry->start;
754 }
755 } else if (grows & LINUX_PROT_GROWSUP) {
756 if (USRSTACK <= end && end < USRSTACK + stacklim) {
757 end = USRSTACK + stacklim;
758 } else {
759 end = entry->end;
760 }
761 }
762 vm_map_unlock(map);
763 return uvm_map_protect(map, start, end, prot, FALSE);
764 }
765
766 /*
767 * This code is partly stolen from src/lib/libc/compat-43/times.c
768 */
769
770 #define CONVTCK(r) (r.tv_sec * hz + r.tv_usec / (1000000 / hz))
771
772 int
773 linux_sys_times(l, v, retval)
774 struct lwp *l;
775 void *v;
776 register_t *retval;
777 {
778 struct linux_sys_times_args /* {
779 syscallarg(struct times *) tms;
780 } */ *uap = v;
781 struct proc *p = l->l_proc;
782 struct timeval t;
783 int error;
784
785 if (SCARG(uap, tms)) {
786 struct linux_tms ltms;
787 struct rusage ru;
788
789 calcru(p, &ru.ru_utime, &ru.ru_stime, NULL);
790 ltms.ltms_utime = CONVTCK(ru.ru_utime);
791 ltms.ltms_stime = CONVTCK(ru.ru_stime);
792
793 ltms.ltms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
794 ltms.ltms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
795
796 if ((error = copyout(<ms, SCARG(uap, tms), sizeof ltms)))
797 return error;
798 }
799
800 getmicrouptime(&t);
801
802 retval[0] = ((linux_clock_t)(CONVTCK(t)));
803 return 0;
804 }
805
806 #undef CONVTCK
807
808 /*
809 * Linux 'readdir' call. This code is mostly taken from the
810 * SunOS getdents call (see compat/sunos/sunos_misc.c), though
811 * an attempt has been made to keep it a little cleaner (failing
812 * miserably, because of the cruft needed if count 1 is passed).
813 *
814 * The d_off field should contain the offset of the next valid entry,
815 * but in Linux it has the offset of the entry itself. We emulate
816 * that bug here.
817 *
818 * Read in BSD-style entries, convert them, and copy them out.
819 *
820 * Note that this doesn't handle union-mounted filesystems.
821 */
822 int
823 linux_sys_getdents(l, v, retval)
824 struct lwp *l;
825 void *v;
826 register_t *retval;
827 {
828 struct linux_sys_getdents_args /* {
829 syscallarg(int) fd;
830 syscallarg(struct linux_dirent *) dent;
831 syscallarg(unsigned int) count;
832 } */ *uap = v;
833 struct dirent *bdp;
834 struct vnode *vp;
835 caddr_t inp, tbuf; /* BSD-format */
836 int len, reclen; /* BSD-format */
837 caddr_t outp; /* Linux-format */
838 int resid, linux_reclen = 0; /* Linux-format */
839 struct file *fp;
840 struct uio auio;
841 struct iovec aiov;
842 struct linux_dirent idb;
843 off_t off; /* true file offset */
844 int buflen, error, eofflag, nbytes, oldcall;
845 struct vattr va;
846 off_t *cookiebuf = NULL, *cookie;
847 int ncookies;
848
849 /* getvnode() will use the descriptor for us */
850 if ((error = getvnode(l->l_proc->p_fd, SCARG(uap, fd), &fp)) != 0)
851 return (error);
852
853 if ((fp->f_flag & FREAD) == 0) {
854 error = EBADF;
855 goto out1;
856 }
857
858 vp = (struct vnode *)fp->f_data;
859 if (vp->v_type != VDIR) {
860 error = EINVAL;
861 goto out1;
862 }
863
864 if ((error = VOP_GETATTR(vp, &va, l->l_cred, l)))
865 goto out1;
866
867 nbytes = SCARG(uap, count);
868 if (nbytes == 1) { /* emulating old, broken behaviour */
869 nbytes = sizeof (idb);
870 buflen = max(va.va_blocksize, nbytes);
871 oldcall = 1;
872 } else {
873 buflen = min(MAXBSIZE, nbytes);
874 if (buflen < va.va_blocksize)
875 buflen = va.va_blocksize;
876 oldcall = 0;
877 }
878 tbuf = malloc(buflen, M_TEMP, M_WAITOK);
879
880 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
881 off = fp->f_offset;
882 again:
883 aiov.iov_base = tbuf;
884 aiov.iov_len = buflen;
885 auio.uio_iov = &aiov;
886 auio.uio_iovcnt = 1;
887 auio.uio_rw = UIO_READ;
888 auio.uio_resid = buflen;
889 auio.uio_offset = off;
890 UIO_SETUP_SYSSPACE(&auio);
891 /*
892 * First we read into the malloc'ed buffer, then
893 * we massage it into user space, one record at a time.
894 */
895 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
896 &ncookies);
897 if (error)
898 goto out;
899
900 inp = tbuf;
901 outp = (caddr_t)SCARG(uap, dent);
902 resid = nbytes;
903 if ((len = buflen - auio.uio_resid) == 0)
904 goto eof;
905
906 for (cookie = cookiebuf; len > 0; len -= reclen) {
907 bdp = (struct dirent *)inp;
908 reclen = bdp->d_reclen;
909 if (reclen & 3)
910 panic("linux_readdir");
911 if (bdp->d_fileno == 0) {
912 inp += reclen; /* it is a hole; squish it out */
913 if (cookie)
914 off = *cookie++;
915 else
916 off += reclen;
917 continue;
918 }
919 linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
920 if (reclen > len || resid < linux_reclen) {
921 /* entry too big for buffer, so just stop */
922 outp++;
923 break;
924 }
925 /*
926 * Massage in place to make a Linux-shaped dirent (otherwise
927 * we have to worry about touching user memory outside of
928 * the copyout() call).
929 */
930 idb.d_ino = bdp->d_fileno;
931 /*
932 * The old readdir() call misuses the offset and reclen fields.
933 */
934 if (oldcall) {
935 idb.d_off = (linux_off_t)linux_reclen;
936 idb.d_reclen = (u_short)bdp->d_namlen;
937 } else {
938 if (sizeof (idb.d_off) <= 4 && (off >> 32) != 0) {
939 compat_offseterr(vp, "linux_getdents");
940 error = EINVAL;
941 goto out;
942 }
943 idb.d_off = (linux_off_t)off;
944 idb.d_reclen = (u_short)linux_reclen;
945 }
946 strcpy(idb.d_name, bdp->d_name);
947 if ((error = copyout((caddr_t)&idb, outp, linux_reclen)))
948 goto out;
949 /* advance past this real entry */
950 inp += reclen;
951 if (cookie)
952 off = *cookie++; /* each entry points to itself */
953 else
954 off += reclen;
955 /* advance output past Linux-shaped entry */
956 outp += linux_reclen;
957 resid -= linux_reclen;
958 if (oldcall)
959 break;
960 }
961
962 /* if we squished out the whole block, try again */
963 if (outp == (caddr_t)SCARG(uap, dent))
964 goto again;
965 fp->f_offset = off; /* update the vnode offset */
966
967 if (oldcall)
968 nbytes = resid + linux_reclen;
969
970 eof:
971 *retval = nbytes - resid;
972 out:
973 VOP_UNLOCK(vp, 0);
974 if (cookiebuf)
975 free(cookiebuf, M_TEMP);
976 free(tbuf, M_TEMP);
977 out1:
978 FILE_UNUSE(fp, l);
979 return error;
980 }
981
982 /*
983 * Even when just using registers to pass arguments to syscalls you can
984 * have 5 of them on the i386. So this newer version of select() does
985 * this.
986 */
987 int
988 linux_sys_select(l, v, retval)
989 struct lwp *l;
990 void *v;
991 register_t *retval;
992 {
993 struct linux_sys_select_args /* {
994 syscallarg(int) nfds;
995 syscallarg(fd_set *) readfds;
996 syscallarg(fd_set *) writefds;
997 syscallarg(fd_set *) exceptfds;
998 syscallarg(struct timeval *) timeout;
999 } */ *uap = v;
1000
1001 return linux_select1(l, retval, SCARG(uap, nfds), SCARG(uap, readfds),
1002 SCARG(uap, writefds), SCARG(uap, exceptfds), SCARG(uap, timeout));
1003 }
1004
1005 /*
1006 * Common code for the old and new versions of select(). A couple of
1007 * things are important:
1008 * 1) return the amount of time left in the 'timeout' parameter
1009 * 2) select never returns ERESTART on Linux, always return EINTR
1010 */
1011 int
1012 linux_select1(l, retval, nfds, readfds, writefds, exceptfds, timeout)
1013 struct lwp *l;
1014 register_t *retval;
1015 int nfds;
1016 fd_set *readfds, *writefds, *exceptfds;
1017 struct timeval *timeout;
1018 {
1019 struct sys_select_args bsa;
1020 struct proc *p = l->l_proc;
1021 struct timeval tv0, tv1, utv, *tvp;
1022 caddr_t sg;
1023 int error;
1024
1025 SCARG(&bsa, nd) = nfds;
1026 SCARG(&bsa, in) = readfds;
1027 SCARG(&bsa, ou) = writefds;
1028 SCARG(&bsa, ex) = exceptfds;
1029 SCARG(&bsa, tv) = timeout;
1030
1031 /*
1032 * Store current time for computation of the amount of
1033 * time left.
1034 */
1035 if (timeout) {
1036 if ((error = copyin(timeout, &utv, sizeof(utv))))
1037 return error;
1038 if (itimerfix(&utv)) {
1039 /*
1040 * The timeval was invalid. Convert it to something
1041 * valid that will act as it does under Linux.
1042 */
1043 sg = stackgap_init(p, 0);
1044 tvp = stackgap_alloc(p, &sg, sizeof(utv));
1045 utv.tv_sec += utv.tv_usec / 1000000;
1046 utv.tv_usec %= 1000000;
1047 if (utv.tv_usec < 0) {
1048 utv.tv_sec -= 1;
1049 utv.tv_usec += 1000000;
1050 }
1051 if (utv.tv_sec < 0)
1052 timerclear(&utv);
1053 if ((error = copyout(&utv, tvp, sizeof(utv))))
1054 return error;
1055 SCARG(&bsa, tv) = tvp;
1056 }
1057 microtime(&tv0);
1058 }
1059
1060 error = sys_select(l, &bsa, retval);
1061 if (error) {
1062 /*
1063 * See fs/select.c in the Linux kernel. Without this,
1064 * Maelstrom doesn't work.
1065 */
1066 if (error == ERESTART)
1067 error = EINTR;
1068 return error;
1069 }
1070
1071 if (timeout) {
1072 if (*retval) {
1073 /*
1074 * Compute how much time was left of the timeout,
1075 * by subtracting the current time and the time
1076 * before we started the call, and subtracting
1077 * that result from the user-supplied value.
1078 */
1079 microtime(&tv1);
1080 timersub(&tv1, &tv0, &tv1);
1081 timersub(&utv, &tv1, &utv);
1082 if (utv.tv_sec < 0)
1083 timerclear(&utv);
1084 } else
1085 timerclear(&utv);
1086 if ((error = copyout(&utv, timeout, sizeof(utv))))
1087 return error;
1088 }
1089
1090 return 0;
1091 }
1092
1093 /*
1094 * Get the process group of a certain process. Look it up
1095 * and return the value.
1096 */
1097 int
1098 linux_sys_getpgid(l, v, retval)
1099 struct lwp *l;
1100 void *v;
1101 register_t *retval;
1102 {
1103 struct linux_sys_getpgid_args /* {
1104 syscallarg(int) pid;
1105 } */ *uap = v;
1106 struct proc *p = l->l_proc;
1107 struct proc *targp;
1108
1109 if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != p->p_pid) {
1110 if ((targp = pfind(SCARG(uap, pid))) == 0)
1111 return ESRCH;
1112 }
1113 else
1114 targp = p;
1115
1116 retval[0] = targp->p_pgid;
1117 return 0;
1118 }
1119
1120 /*
1121 * Set the 'personality' (emulation mode) for the current process. Only
1122 * accept the Linux personality here (0). This call is needed because
1123 * the Linux ELF crt0 issues it in an ugly kludge to make sure that
1124 * ELF binaries run in Linux mode, not SVR4 mode.
1125 */
1126 int
1127 linux_sys_personality(l, v, retval)
1128 struct lwp *l;
1129 void *v;
1130 register_t *retval;
1131 {
1132 struct linux_sys_personality_args /* {
1133 syscallarg(int) per;
1134 } */ *uap = v;
1135
1136 if (SCARG(uap, per) != 0)
1137 return EINVAL;
1138 retval[0] = 0;
1139 return 0;
1140 }
1141 #endif /* !COMPAT_LINUX32 */
1142
1143 #if defined(__i386__) || defined(__m68k__) || defined(COMPAT_LINUX32)
1144 /*
1145 * The calls are here because of type conversions.
1146 */
1147 int
1148 linux_sys_setreuid16(l, v, retval)
1149 struct lwp *l;
1150 void *v;
1151 register_t *retval;
1152 {
1153 struct linux_sys_setreuid16_args /* {
1154 syscallarg(int) ruid;
1155 syscallarg(int) euid;
1156 } */ *uap = v;
1157 struct sys_setreuid_args bsa;
1158
1159 SCARG(&bsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
1160 (uid_t)-1 : SCARG(uap, ruid);
1161 SCARG(&bsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
1162 (uid_t)-1 : SCARG(uap, euid);
1163
1164 return sys_setreuid(l, &bsa, retval);
1165 }
1166
1167 int
1168 linux_sys_setregid16(l, v, retval)
1169 struct lwp *l;
1170 void *v;
1171 register_t *retval;
1172 {
1173 struct linux_sys_setregid16_args /* {
1174 syscallarg(int) rgid;
1175 syscallarg(int) egid;
1176 } */ *uap = v;
1177 struct sys_setregid_args bsa;
1178
1179 SCARG(&bsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
1180 (uid_t)-1 : SCARG(uap, rgid);
1181 SCARG(&bsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
1182 (uid_t)-1 : SCARG(uap, egid);
1183
1184 return sys_setregid(l, &bsa, retval);
1185 }
1186
1187 int
1188 linux_sys_setresuid16(l, v, retval)
1189 struct lwp *l;
1190 void *v;
1191 register_t *retval;
1192 {
1193 struct linux_sys_setresuid16_args /* {
1194 syscallarg(uid_t) ruid;
1195 syscallarg(uid_t) euid;
1196 syscallarg(uid_t) suid;
1197 } */ *uap = v;
1198 struct linux_sys_setresuid16_args lsa;
1199
1200 SCARG(&lsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
1201 (uid_t)-1 : SCARG(uap, ruid);
1202 SCARG(&lsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
1203 (uid_t)-1 : SCARG(uap, euid);
1204 SCARG(&lsa, suid) = ((linux_uid_t)SCARG(uap, suid) == (linux_uid_t)-1) ?
1205 (uid_t)-1 : SCARG(uap, suid);
1206
1207 return linux_sys_setresuid(l, &lsa, retval);
1208 }
1209
1210 int
1211 linux_sys_setresgid16(l, v, retval)
1212 struct lwp *l;
1213 void *v;
1214 register_t *retval;
1215 {
1216 struct linux_sys_setresgid16_args /* {
1217 syscallarg(gid_t) rgid;
1218 syscallarg(gid_t) egid;
1219 syscallarg(gid_t) sgid;
1220 } */ *uap = v;
1221 struct linux_sys_setresgid16_args lsa;
1222
1223 SCARG(&lsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
1224 (gid_t)-1 : SCARG(uap, rgid);
1225 SCARG(&lsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
1226 (gid_t)-1 : SCARG(uap, egid);
1227 SCARG(&lsa, sgid) = ((linux_gid_t)SCARG(uap, sgid) == (linux_gid_t)-1) ?
1228 (gid_t)-1 : SCARG(uap, sgid);
1229
1230 return linux_sys_setresgid(l, &lsa, retval);
1231 }
1232
1233 int
1234 linux_sys_getgroups16(l, v, retval)
1235 struct lwp *l;
1236 void *v;
1237 register_t *retval;
1238 {
1239 struct linux_sys_getgroups16_args /* {
1240 syscallarg(int) gidsetsize;
1241 syscallarg(linux_gid_t *) gidset;
1242 } */ *uap = v;
1243 struct proc *p = l->l_proc;
1244 caddr_t sg;
1245 int n, error, i;
1246 struct sys_getgroups_args bsa;
1247 gid_t *bset, *kbset;
1248 linux_gid_t *lset;
1249 kauth_cred_t pc = l->l_cred;
1250
1251 n = SCARG(uap, gidsetsize);
1252 if (n < 0)
1253 return EINVAL;
1254 error = 0;
1255 bset = kbset = NULL;
1256 lset = NULL;
1257 if (n > 0) {
1258 n = min(kauth_cred_ngroups(pc), n);
1259 sg = stackgap_init(p, 0);
1260 bset = stackgap_alloc(p, &sg, n * sizeof (gid_t));
1261 kbset = malloc(n * sizeof (gid_t), M_TEMP, M_WAITOK);
1262 lset = malloc(n * sizeof (linux_gid_t), M_TEMP, M_WAITOK);
1263 if (bset == NULL || kbset == NULL || lset == NULL)
1264 {
1265 error = ENOMEM;
1266 goto out;
1267 }
1268 SCARG(&bsa, gidsetsize) = n;
1269 SCARG(&bsa, gidset) = bset;
1270 error = sys_getgroups(l, &bsa, retval);
1271 if (error != 0)
1272 goto out;
1273 error = copyin(bset, kbset, n * sizeof (gid_t));
1274 if (error != 0)
1275 goto out;
1276 for (i = 0; i < n; i++)
1277 lset[i] = (linux_gid_t)kbset[i];
1278 error = copyout(lset, SCARG(uap, gidset),
1279 n * sizeof (linux_gid_t));
1280 } else
1281 *retval = kauth_cred_ngroups(pc);
1282 out:
1283 if (kbset != NULL)
1284 free(kbset, M_TEMP);
1285 if (lset != NULL)
1286 free(lset, M_TEMP);
1287 return error;
1288 }
1289
1290 int
1291 linux_sys_setgroups16(l, v, retval)
1292 struct lwp *l;
1293 void *v;
1294 register_t *retval;
1295 {
1296 struct linux_sys_setgroups16_args /* {
1297 syscallarg(int) gidsetsize;
1298 syscallarg(linux_gid_t *) gidset;
1299 } */ *uap = v;
1300 struct proc *p = l->l_proc;
1301 caddr_t sg;
1302 int n;
1303 int error, i;
1304 struct sys_setgroups_args bsa;
1305 gid_t *bset, *kbset;
1306 linux_gid_t *lset;
1307
1308 n = SCARG(uap, gidsetsize);
1309 if (n < 0 || n > NGROUPS)
1310 return EINVAL;
1311 sg = stackgap_init(p, 0);
1312 bset = stackgap_alloc(p, &sg, n * sizeof (gid_t));
1313 lset = malloc(n * sizeof (linux_gid_t), M_TEMP, M_WAITOK);
1314 kbset = malloc(n * sizeof (gid_t), M_TEMP, M_WAITOK);
1315 if (bset == NULL || kbset == NULL || lset == NULL)
1316 {
1317 error = ENOMEM;
1318 goto out;
1319 }
1320 error = copyin(SCARG(uap, gidset), lset, n * sizeof (linux_gid_t));
1321 if (error != 0)
1322 goto out;
1323 for (i = 0; i < n; i++)
1324 kbset[i] = (gid_t)lset[i];
1325 error = copyout(kbset, bset, n * sizeof (gid_t));
1326 if (error != 0)
1327 goto out;
1328 SCARG(&bsa, gidsetsize) = n;
1329 SCARG(&bsa, gidset) = bset;
1330 error = sys_setgroups(l, &bsa, retval);
1331
1332 out:
1333 if (lset != NULL)
1334 free(lset, M_TEMP);
1335 if (kbset != NULL)
1336 free(kbset, M_TEMP);
1337
1338 return error;
1339 }
1340
1341 #endif /* __i386__ || __m68k__ || COMPAT_LINUX32 */
1342
1343 #ifndef COMPAT_LINUX32
1344 /*
1345 * We have nonexistent fsuid equal to uid.
1346 * If modification is requested, refuse.
1347 */
1348 int
1349 linux_sys_setfsuid(l, v, retval)
1350 struct lwp *l;
1351 void *v;
1352 register_t *retval;
1353 {
1354 struct linux_sys_setfsuid_args /* {
1355 syscallarg(uid_t) uid;
1356 } */ *uap = v;
1357 uid_t uid;
1358
1359 uid = SCARG(uap, uid);
1360 if (kauth_cred_getuid(l->l_cred) != uid)
1361 return sys_nosys(l, v, retval);
1362 else
1363 return (0);
1364 }
1365
1366 /* XXX XXX XXX */
1367 # ifndef alpha
1368 int
1369 linux_sys_getfsuid(l, v, retval)
1370 struct lwp *l;
1371 void *v;
1372 register_t *retval;
1373 {
1374 return sys_getuid(l, v, retval);
1375 }
1376 # endif
1377
1378 int
1379 linux_sys_setresuid(l, v, retval)
1380 struct lwp *l;
1381 void *v;
1382 register_t *retval;
1383 {
1384 struct linux_sys_setresuid_args /* {
1385 syscallarg(uid_t) ruid;
1386 syscallarg(uid_t) euid;
1387 syscallarg(uid_t) suid;
1388 } */ *uap = v;
1389
1390 /*
1391 * Note: These checks are a little different than the NetBSD
1392 * setreuid(2) call performs. This precisely follows the
1393 * behavior of the Linux kernel.
1394 */
1395
1396 return do_setresuid(l, SCARG(uap, ruid), SCARG(uap, euid),
1397 SCARG(uap, suid),
1398 ID_R_EQ_R | ID_R_EQ_E | ID_R_EQ_S |
1399 ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S |
1400 ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S );
1401 }
1402
1403 int
1404 linux_sys_getresuid(l, v, retval)
1405 struct lwp *l;
1406 void *v;
1407 register_t *retval;
1408 {
1409 struct linux_sys_getresuid_args /* {
1410 syscallarg(uid_t *) ruid;
1411 syscallarg(uid_t *) euid;
1412 syscallarg(uid_t *) suid;
1413 } */ *uap = v;
1414 kauth_cred_t pc = l->l_cred;
1415 int error;
1416 uid_t uid;
1417
1418 /*
1419 * Linux copies these values out to userspace like so:
1420 *
1421 * 1. Copy out ruid.
1422 * 2. If that succeeds, copy out euid.
1423 * 3. If both of those succeed, copy out suid.
1424 */
1425 uid = kauth_cred_getuid(pc);
1426 if ((error = copyout(&uid, SCARG(uap, ruid), sizeof(uid_t))) != 0)
1427 return (error);
1428
1429 uid = kauth_cred_geteuid(pc);
1430 if ((error = copyout(&uid, SCARG(uap, euid), sizeof(uid_t))) != 0)
1431 return (error);
1432
1433 uid = kauth_cred_getsvuid(pc);
1434
1435 return (copyout(&uid, SCARG(uap, suid), sizeof(uid_t)));
1436 }
1437
1438 #ifdef PTRACE
1439 int
1440 linux_sys_ptrace(l, v, retval)
1441 struct lwp *l;
1442 void *v;
1443 register_t *retval;
1444 {
1445 struct linux_sys_ptrace_args /* {
1446 i386, m68k, powerpc: T=int
1447 alpha, amd64: T=long
1448 syscallarg(T) request;
1449 syscallarg(T) pid;
1450 syscallarg(T) addr;
1451 syscallarg(T) data;
1452 } */ *uap = v;
1453 const int *ptr;
1454 int request;
1455 int error;
1456
1457 ptr = linux_ptrace_request_map;
1458 request = SCARG(uap, request);
1459 while (*ptr != -1)
1460 if (*ptr++ == request) {
1461 struct sys_ptrace_args pta;
1462
1463 SCARG(&pta, req) = *ptr;
1464 SCARG(&pta, pid) = SCARG(uap, pid);
1465 SCARG(&pta, addr) = (caddr_t)SCARG(uap, addr);
1466 SCARG(&pta, data) = SCARG(uap, data);
1467
1468 /*
1469 * Linux ptrace(PTRACE_CONT, pid, 0, 0) means actually
1470 * to continue where the process left off previously.
1471 * The same thing is achieved by addr == (caddr_t) 1
1472 * on NetBSD, so rewrite 'addr' appropriately.
1473 */
1474 if (request == LINUX_PTRACE_CONT && SCARG(uap, addr)==0)
1475 SCARG(&pta, addr) = (caddr_t) 1;
1476
1477 error = sys_ptrace(l, &pta, retval);
1478 if (error)
1479 return error;
1480 switch (request) {
1481 case LINUX_PTRACE_PEEKTEXT:
1482 case LINUX_PTRACE_PEEKDATA:
1483 error = copyout (retval,
1484 (caddr_t)SCARG(uap, data),
1485 sizeof *retval);
1486 *retval = SCARG(uap, data);
1487 break;
1488 default:
1489 break;
1490 }
1491 return error;
1492 }
1493 else
1494 ptr++;
1495
1496 return LINUX_SYS_PTRACE_ARCH(l, uap, retval);
1497 }
1498 #endif
1499
1500 int
1501 linux_sys_reboot(struct lwp *l, void *v, register_t *retval)
1502 {
1503 struct linux_sys_reboot_args /* {
1504 syscallarg(int) magic1;
1505 syscallarg(int) magic2;
1506 syscallarg(int) cmd;
1507 syscallarg(void *) arg;
1508 } */ *uap = v;
1509 struct sys_reboot_args /* {
1510 syscallarg(int) opt;
1511 syscallarg(char *) bootstr;
1512 } */ sra;
1513 int error;
1514
1515 if ((error = kauth_authorize_generic(l->l_cred,
1516 KAUTH_GENERIC_ISSUSER, &l->l_acflag)) != 0)
1517 return(error);
1518
1519 if (SCARG(uap, magic1) != LINUX_REBOOT_MAGIC1)
1520 return(EINVAL);
1521 if (SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2 &&
1522 SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2A &&
1523 SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2B)
1524 return(EINVAL);
1525
1526 switch (SCARG(uap, cmd)) {
1527 case LINUX_REBOOT_CMD_RESTART:
1528 SCARG(&sra, opt) = RB_AUTOBOOT;
1529 break;
1530 case LINUX_REBOOT_CMD_HALT:
1531 SCARG(&sra, opt) = RB_HALT;
1532 break;
1533 case LINUX_REBOOT_CMD_POWER_OFF:
1534 SCARG(&sra, opt) = RB_HALT|RB_POWERDOWN;
1535 break;
1536 case LINUX_REBOOT_CMD_RESTART2:
1537 /* Reboot with an argument. */
1538 SCARG(&sra, opt) = RB_AUTOBOOT|RB_STRING;
1539 SCARG(&sra, bootstr) = SCARG(uap, arg);
1540 break;
1541 case LINUX_REBOOT_CMD_CAD_ON:
1542 return(EINVAL); /* We don't implement ctrl-alt-delete */
1543 case LINUX_REBOOT_CMD_CAD_OFF:
1544 return(0);
1545 default:
1546 return(EINVAL);
1547 }
1548
1549 return(sys_reboot(l, &sra, retval));
1550 }
1551
1552 /*
1553 * Copy of compat_12_sys_swapon().
1554 */
1555 int
1556 linux_sys_swapon(l, v, retval)
1557 struct lwp *l;
1558 void *v;
1559 register_t *retval;
1560 {
1561 struct sys_swapctl_args ua;
1562 struct linux_sys_swapon_args /* {
1563 syscallarg(const char *) name;
1564 } */ *uap = v;
1565
1566 SCARG(&ua, cmd) = SWAP_ON;
1567 SCARG(&ua, arg) = (void *)__UNCONST(SCARG(uap, name));
1568 SCARG(&ua, misc) = 0; /* priority */
1569 return (sys_swapctl(l, &ua, retval));
1570 }
1571
1572 /*
1573 * Stop swapping to the file or block device specified by path.
1574 */
1575 int
1576 linux_sys_swapoff(l, v, retval)
1577 struct lwp *l;
1578 void *v;
1579 register_t *retval;
1580 {
1581 struct sys_swapctl_args ua;
1582 struct linux_sys_swapoff_args /* {
1583 syscallarg(const char *) path;
1584 } */ *uap = v;
1585
1586 SCARG(&ua, cmd) = SWAP_OFF;
1587 SCARG(&ua, arg) = __UNCONST(SCARG(uap, path)); /*XXXUNCONST*/
1588 return (sys_swapctl(l, &ua, retval));
1589 }
1590
1591 /*
1592 * Copy of compat_09_sys_setdomainname()
1593 */
1594 /* ARGSUSED */
1595 int
1596 linux_sys_setdomainname(l, v, retval)
1597 struct lwp *l;
1598 void *v;
1599 register_t *retval;
1600 {
1601 struct linux_sys_setdomainname_args /* {
1602 syscallarg(char *) domainname;
1603 syscallarg(int) len;
1604 } */ *uap = v;
1605 int name[2];
1606
1607 name[0] = CTL_KERN;
1608 name[1] = KERN_DOMAINNAME;
1609 return (old_sysctl(&name[0], 2, 0, 0, SCARG(uap, domainname),
1610 SCARG(uap, len), l));
1611 }
1612
1613 /*
1614 * sysinfo()
1615 */
1616 /* ARGSUSED */
1617 int
1618 linux_sys_sysinfo(l, v, retval)
1619 struct lwp *l;
1620 void *v;
1621 register_t *retval;
1622 {
1623 struct linux_sys_sysinfo_args /* {
1624 syscallarg(struct linux_sysinfo *) arg;
1625 } */ *uap = v;
1626 struct linux_sysinfo si;
1627 struct loadavg *la;
1628
1629 si.uptime = time_uptime;
1630 la = &averunnable;
1631 si.loads[0] = la->ldavg[0] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
1632 si.loads[1] = la->ldavg[1] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
1633 si.loads[2] = la->ldavg[2] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
1634 si.totalram = ctob(physmem);
1635 si.freeram = uvmexp.free * uvmexp.pagesize;
1636 si.sharedram = 0; /* XXX */
1637 si.bufferram = uvmexp.filepages * uvmexp.pagesize;
1638 si.totalswap = uvmexp.swpages * uvmexp.pagesize;
1639 si.freeswap = (uvmexp.swpages - uvmexp.swpginuse) * uvmexp.pagesize;
1640 si.procs = nprocs;
1641
1642 /* The following are only present in newer Linux kernels. */
1643 si.totalbig = 0;
1644 si.freebig = 0;
1645 si.mem_unit = 1;
1646
1647 return (copyout(&si, SCARG(uap, arg), sizeof si));
1648 }
1649
1650 int
1651 linux_sys_getrlimit(l, v, retval)
1652 struct lwp *l;
1653 void *v;
1654 register_t *retval;
1655 {
1656 struct linux_sys_getrlimit_args /* {
1657 syscallarg(int) which;
1658 # ifdef LINUX_LARGEFILE64
1659 syscallarg(struct rlimit *) rlp;
1660 # else
1661 syscallarg(struct orlimit *) rlp;
1662 # endif
1663 } */ *uap = v;
1664 struct proc *p = l->l_proc;
1665 caddr_t sg = stackgap_init(p, 0);
1666 struct sys_getrlimit_args ap;
1667 struct rlimit rl;
1668 # ifdef LINUX_LARGEFILE64
1669 struct rlimit orl;
1670 # else
1671 struct orlimit orl;
1672 # endif
1673 int error;
1674
1675 SCARG(&ap, which) = linux_to_bsd_limit(SCARG(uap, which));
1676 if ((error = SCARG(&ap, which)) < 0)
1677 return -error;
1678 SCARG(&ap, rlp) = stackgap_alloc(p, &sg, sizeof rl);
1679 if ((error = sys_getrlimit(l, &ap, retval)) != 0)
1680 return error;
1681 if ((error = copyin(SCARG(&ap, rlp), &rl, sizeof(rl))) != 0)
1682 return error;
1683 bsd_to_linux_rlimit(&orl, &rl);
1684
1685 return copyout(&orl, SCARG(uap, rlp), sizeof(orl));
1686 }
1687
1688 int
1689 linux_sys_setrlimit(l, v, retval)
1690 struct lwp *l;
1691 void *v;
1692 register_t *retval;
1693 {
1694 struct linux_sys_setrlimit_args /* {
1695 syscallarg(int) which;
1696 # ifdef LINUX_LARGEFILE64
1697 syscallarg(struct rlimit *) rlp;
1698 # else
1699 syscallarg(struct orlimit *) rlp;
1700 # endif
1701 } */ *uap = v;
1702 struct proc *p = l->l_proc;
1703 caddr_t sg = stackgap_init(p, 0);
1704 struct sys_getrlimit_args ap;
1705 struct rlimit rl;
1706 # ifdef LINUX_LARGEFILE64
1707 struct rlimit orl;
1708 # else
1709 struct orlimit orl;
1710 # endif
1711 int error;
1712
1713 SCARG(&ap, which) = linux_to_bsd_limit(SCARG(uap, which));
1714 SCARG(&ap, rlp) = stackgap_alloc(p, &sg, sizeof rl);
1715 if ((error = SCARG(&ap, which)) < 0)
1716 return -error;
1717 if ((error = copyin(SCARG(uap, rlp), &orl, sizeof(orl))) != 0)
1718 return error;
1719 linux_to_bsd_rlimit(&rl, &orl);
1720 if ((error = copyout(&rl, SCARG(&ap, rlp), sizeof(rl))) != 0)
1721 return error;
1722 return sys_setrlimit(l, &ap, retval);
1723 }
1724
1725 # if !defined(__mips__) && !defined(__amd64__)
1726 /* XXX: this doesn't look 100% common, at least mips doesn't have it */
1727 int
1728 linux_sys_ugetrlimit(l, v, retval)
1729 struct lwp *l;
1730 void *v;
1731 register_t *retval;
1732 {
1733 return linux_sys_getrlimit(l, v, retval);
1734 }
1735 # endif
1736
1737 /*
1738 * This gets called for unsupported syscalls. The difference to sys_nosys()
1739 * is that process does not get SIGSYS, the call just returns with ENOSYS.
1740 * This is the way Linux does it and glibc depends on this behaviour.
1741 */
1742 int
1743 linux_sys_nosys(l, v, retval)
1744 struct lwp *l;
1745 void *v;
1746 register_t *retval;
1747 {
1748 return (ENOSYS);
1749 }
1750
1751 int
1752 linux_sys_getpriority(l, v, retval)
1753 struct lwp *l;
1754 void *v;
1755 register_t *retval;
1756 {
1757 struct linux_sys_getpriority_args /* {
1758 syscallarg(int) which;
1759 syscallarg(int) who;
1760 } */ *uap = v;
1761 struct sys_getpriority_args bsa;
1762 int error;
1763
1764 SCARG(&bsa, which) = SCARG(uap, which);
1765 SCARG(&bsa, who) = SCARG(uap, who);
1766
1767 if ((error = sys_getpriority(l, &bsa, retval)))
1768 return error;
1769
1770 *retval = NZERO - *retval;
1771
1772 return 0;
1773 }
1774
1775 #endif /* !COMPAT_LINUX32 */
1776