linux_misc.c revision 1.161.2.1 1 /* $NetBSD: linux_misc.c,v 1.161.2.1 2006/10/24 21:10:22 ad Exp $ */
2
3 /*-
4 * Copyright (c) 1995, 1998, 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Frank van der Linden and Eric Haszlakiewicz; by Jason R. Thorpe
9 * of the Numerical Aerospace Simulation Facility, NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Linux compatibility module. Try to deal with various Linux system calls.
42 */
43
44 /*
45 * These functions have been moved to multiarch to allow
46 * selection of which machines include them to be
47 * determined by the individual files.linux_<arch> files.
48 *
49 * Function in multiarch:
50 * linux_sys_break : linux_break.c
51 * linux_sys_alarm : linux_misc_notalpha.c
52 * linux_sys_getresgid : linux_misc_notalpha.c
53 * linux_sys_nice : linux_misc_notalpha.c
54 * linux_sys_readdir : linux_misc_notalpha.c
55 * linux_sys_setresgid : linux_misc_notalpha.c
56 * linux_sys_time : linux_misc_notalpha.c
57 * linux_sys_utime : linux_misc_notalpha.c
58 * linux_sys_waitpid : linux_misc_notalpha.c
59 * linux_sys_old_mmap : linux_oldmmap.c
60 * linux_sys_oldolduname : linux_oldolduname.c
61 * linux_sys_oldselect : linux_oldselect.c
62 * linux_sys_olduname : linux_olduname.c
63 * linux_sys_pipe : linux_pipe.c
64 */
65
66 #include <sys/cdefs.h>
67 __KERNEL_RCSID(0, "$NetBSD: linux_misc.c,v 1.161.2.1 2006/10/24 21:10:22 ad Exp $");
68
69 #if defined(_KERNEL_OPT)
70 #include "opt_ptrace.h"
71 #endif
72
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/namei.h>
76 #include <sys/proc.h>
77 #include <sys/dirent.h>
78 #include <sys/file.h>
79 #include <sys/stat.h>
80 #include <sys/filedesc.h>
81 #include <sys/ioctl.h>
82 #include <sys/kernel.h>
83 #include <sys/malloc.h>
84 #include <sys/mbuf.h>
85 #include <sys/mman.h>
86 #include <sys/mount.h>
87 #include <sys/reboot.h>
88 #include <sys/resource.h>
89 #include <sys/resourcevar.h>
90 #include <sys/signal.h>
91 #include <sys/signalvar.h>
92 #include <sys/socket.h>
93 #include <sys/time.h>
94 #include <sys/times.h>
95 #include <sys/vnode.h>
96 #include <sys/uio.h>
97 #include <sys/wait.h>
98 #include <sys/utsname.h>
99 #include <sys/unistd.h>
100 #include <sys/swap.h> /* for SWAP_ON */
101 #include <sys/sysctl.h> /* for KERN_DOMAINNAME */
102 #include <sys/kauth.h>
103
104 #include <sys/ptrace.h>
105 #include <machine/ptrace.h>
106
107 #include <sys/sa.h>
108 #include <sys/syscall.h>
109 #include <sys/syscallargs.h>
110
111 #include <compat/linux/common/linux_machdep.h>
112 #include <compat/linux/common/linux_types.h>
113 #include <compat/linux/common/linux_signal.h>
114
115 #include <compat/linux/linux_syscallargs.h>
116
117 #include <compat/linux/common/linux_fcntl.h>
118 #include <compat/linux/common/linux_mmap.h>
119 #include <compat/linux/common/linux_dirent.h>
120 #include <compat/linux/common/linux_util.h>
121 #include <compat/linux/common/linux_misc.h>
122 #ifndef COMPAT_LINUX32
123 #include <compat/linux/common/linux_limit.h>
124 #endif
125 #include <compat/linux/common/linux_ptrace.h>
126 #include <compat/linux/common/linux_reboot.h>
127 #include <compat/linux/common/linux_emuldata.h>
128
129 #ifndef COMPAT_LINUX32
130 const int linux_ptrace_request_map[] = {
131 LINUX_PTRACE_TRACEME, PT_TRACE_ME,
132 LINUX_PTRACE_PEEKTEXT, PT_READ_I,
133 LINUX_PTRACE_PEEKDATA, PT_READ_D,
134 LINUX_PTRACE_POKETEXT, PT_WRITE_I,
135 LINUX_PTRACE_POKEDATA, PT_WRITE_D,
136 LINUX_PTRACE_CONT, PT_CONTINUE,
137 LINUX_PTRACE_KILL, PT_KILL,
138 LINUX_PTRACE_ATTACH, PT_ATTACH,
139 LINUX_PTRACE_DETACH, PT_DETACH,
140 # ifdef PT_STEP
141 LINUX_PTRACE_SINGLESTEP, PT_STEP,
142 # endif
143 -1
144 };
145
146 const struct linux_mnttypes linux_fstypes[] = {
147 { MOUNT_FFS, LINUX_DEFAULT_SUPER_MAGIC },
148 { MOUNT_NFS, LINUX_NFS_SUPER_MAGIC },
149 { MOUNT_MFS, LINUX_DEFAULT_SUPER_MAGIC },
150 { MOUNT_MSDOS, LINUX_MSDOS_SUPER_MAGIC },
151 { MOUNT_LFS, LINUX_DEFAULT_SUPER_MAGIC },
152 { MOUNT_FDESC, LINUX_DEFAULT_SUPER_MAGIC },
153 { MOUNT_PORTAL, LINUX_DEFAULT_SUPER_MAGIC },
154 { MOUNT_NULL, LINUX_DEFAULT_SUPER_MAGIC },
155 { MOUNT_OVERLAY, LINUX_DEFAULT_SUPER_MAGIC },
156 { MOUNT_UMAP, LINUX_DEFAULT_SUPER_MAGIC },
157 { MOUNT_KERNFS, LINUX_DEFAULT_SUPER_MAGIC },
158 { MOUNT_PROCFS, LINUX_PROC_SUPER_MAGIC },
159 { MOUNT_AFS, LINUX_DEFAULT_SUPER_MAGIC },
160 { MOUNT_CD9660, LINUX_ISOFS_SUPER_MAGIC },
161 { MOUNT_UNION, LINUX_DEFAULT_SUPER_MAGIC },
162 { MOUNT_ADOSFS, LINUX_ADFS_SUPER_MAGIC },
163 { MOUNT_EXT2FS, LINUX_EXT2_SUPER_MAGIC },
164 { MOUNT_CFS, LINUX_DEFAULT_SUPER_MAGIC },
165 { MOUNT_CODA, LINUX_CODA_SUPER_MAGIC },
166 { MOUNT_FILECORE, LINUX_DEFAULT_SUPER_MAGIC },
167 { MOUNT_NTFS, LINUX_DEFAULT_SUPER_MAGIC },
168 { MOUNT_SMBFS, LINUX_SMB_SUPER_MAGIC },
169 { MOUNT_PTYFS, LINUX_DEVPTS_SUPER_MAGIC },
170 { MOUNT_TMPFS, LINUX_DEFAULT_SUPER_MAGIC }
171 };
172 const int linux_fstypes_cnt = sizeof(linux_fstypes) / sizeof(linux_fstypes[0]);
173
174 # ifdef DEBUG_LINUX
175 #define DPRINTF(a) uprintf a
176 # else
177 #define DPRINTF(a)
178 # endif
179
180 /* Local linux_misc.c functions: */
181 # ifndef __amd64__
182 static void bsd_to_linux_statfs __P((const struct statvfs *,
183 struct linux_statfs *));
184 # endif
185 static void linux_to_bsd_mmap_args __P((struct sys_mmap_args *,
186 const struct linux_sys_mmap_args *));
187 static int linux_mmap __P((struct lwp *, struct linux_sys_mmap_args *,
188 register_t *, off_t));
189
190
191 /*
192 * The information on a terminated (or stopped) process needs
193 * to be converted in order for Linux binaries to get a valid signal
194 * number out of it.
195 */
196 void
197 bsd_to_linux_wstat(st)
198 int *st;
199 {
200
201 int sig;
202
203 if (WIFSIGNALED(*st)) {
204 sig = WTERMSIG(*st);
205 if (sig >= 0 && sig < NSIG)
206 *st= (*st& ~0177) | native_to_linux_signo[sig];
207 } else if (WIFSTOPPED(*st)) {
208 sig = WSTOPSIG(*st);
209 if (sig >= 0 && sig < NSIG)
210 *st = (*st & ~0xff00) |
211 (native_to_linux_signo[sig] << 8);
212 }
213 }
214
215 /*
216 * wait4(2). Passed on to the NetBSD call, surrounded by code to
217 * reserve some space for a NetBSD-style wait status, and converting
218 * it to what Linux wants.
219 */
220 int
221 linux_sys_wait4(l, v, retval)
222 struct lwp *l;
223 void *v;
224 register_t *retval;
225 {
226 struct linux_sys_wait4_args /* {
227 syscallarg(int) pid;
228 syscallarg(int *) status;
229 syscallarg(int) options;
230 syscallarg(struct rusage *) rusage;
231 } */ *uap = v;
232 struct proc *p = l->l_proc;
233 struct sys_wait4_args w4a;
234 int error, *status, tstat, options, linux_options;
235 caddr_t sg;
236
237 if (SCARG(uap, status) != NULL) {
238 sg = stackgap_init(p, 0);
239 status = (int *) stackgap_alloc(p, &sg, sizeof *status);
240 } else
241 status = NULL;
242
243 linux_options = SCARG(uap, options);
244 options = 0;
245 if (linux_options & ~(LINUX_WAIT4_KNOWNFLAGS))
246 return (EINVAL);
247
248 if (linux_options & LINUX_WAIT4_WNOHANG)
249 options |= WNOHANG;
250 if (linux_options & LINUX_WAIT4_WUNTRACED)
251 options |= WUNTRACED;
252 if (linux_options & LINUX_WAIT4_WALL)
253 options |= WALLSIG;
254 if (linux_options & LINUX_WAIT4_WCLONE)
255 options |= WALTSIG;
256 # ifdef DIAGNOSTIC
257 if (linux_options & LINUX_WAIT4_WNOTHREAD)
258 printf("WARNING: %s: linux process %d.%d called "
259 "waitpid with __WNOTHREAD set!",
260 __FILE__, p->p_pid, l->l_lid);
261
262 # endif
263
264 SCARG(&w4a, pid) = SCARG(uap, pid);
265 SCARG(&w4a, status) = status;
266 SCARG(&w4a, options) = options;
267 SCARG(&w4a, rusage) = SCARG(uap, rusage);
268
269 if ((error = sys_wait4(l, &w4a, retval)))
270 return error;
271
272 sigdelset(&l->l_sigpend->sp_set, SIGCHLD);
273
274 if (status != NULL) {
275 if ((error = copyin(status, &tstat, sizeof tstat)))
276 return error;
277
278 bsd_to_linux_wstat(&tstat);
279 return copyout(&tstat, SCARG(uap, status), sizeof tstat);
280 }
281
282 return 0;
283 }
284
285 /*
286 * Linux brk(2). The check if the new address is >= the old one is
287 * done in the kernel in Linux. NetBSD does it in the library.
288 */
289 int
290 linux_sys_brk(l, v, retval)
291 struct lwp *l;
292 void *v;
293 register_t *retval;
294 {
295 struct linux_sys_brk_args /* {
296 syscallarg(char *) nsize;
297 } */ *uap = v;
298 struct proc *p = l->l_proc;
299 char *nbrk = SCARG(uap, nsize);
300 struct sys_obreak_args oba;
301 struct vmspace *vm = p->p_vmspace;
302 struct linux_emuldata *ed = (struct linux_emuldata*)p->p_emuldata;
303
304 SCARG(&oba, nsize) = nbrk;
305
306 if ((caddr_t) nbrk > vm->vm_daddr && sys_obreak(l, &oba, retval) == 0)
307 ed->s->p_break = (char*)nbrk;
308 else
309 nbrk = ed->s->p_break;
310
311 retval[0] = (register_t)nbrk;
312
313 return 0;
314 }
315
316 # ifndef __amd64__
317 /*
318 * Convert NetBSD statvfs structure to Linux statfs structure.
319 * Linux doesn't have f_flag, and we can't set f_frsize due
320 * to glibc statvfs() bug (see below).
321 */
322 static void
323 bsd_to_linux_statfs(bsp, lsp)
324 const struct statvfs *bsp;
325 struct linux_statfs *lsp;
326 {
327 int i;
328
329 for (i = 0; i < linux_fstypes_cnt; i++) {
330 if (strcmp(bsp->f_fstypename, linux_fstypes[i].bsd) == 0) {
331 lsp->l_ftype = linux_fstypes[i].linux;
332 break;
333 }
334 }
335
336 if (i == linux_fstypes_cnt) {
337 DPRINTF(("unhandled fstype in linux emulation: %s\n",
338 bsp->f_fstypename));
339 lsp->l_ftype = LINUX_DEFAULT_SUPER_MAGIC;
340 }
341
342 /*
343 * The sizes are expressed in number of blocks. The block
344 * size used for the size is f_frsize for POSIX-compliant
345 * statvfs. Linux statfs uses f_bsize as the block size
346 * (f_frsize used to not be available in Linux struct statfs).
347 * However, glibc 2.3.3 statvfs() wrapper fails to adjust the block
348 * counts for different f_frsize if f_frsize is provided by the kernel.
349 * POSIX conforming apps thus get wrong size if f_frsize
350 * is different to f_bsize. Thus, we just pretend we don't
351 * support f_frsize.
352 */
353
354 lsp->l_fbsize = bsp->f_frsize;
355 lsp->l_ffrsize = 0; /* compat */
356 lsp->l_fblocks = bsp->f_blocks;
357 lsp->l_fbfree = bsp->f_bfree;
358 lsp->l_fbavail = bsp->f_bavail;
359 lsp->l_ffiles = bsp->f_files;
360 lsp->l_fffree = bsp->f_ffree;
361 /* Linux sets the fsid to 0..., we don't */
362 lsp->l_ffsid.val[0] = bsp->f_fsidx.__fsid_val[0];
363 lsp->l_ffsid.val[1] = bsp->f_fsidx.__fsid_val[1];
364 lsp->l_fnamelen = bsp->f_namemax;
365 (void)memset(lsp->l_fspare, 0, sizeof(lsp->l_fspare));
366 }
367
368 /*
369 * Implement the fs stat functions. Straightforward.
370 */
371 int
372 linux_sys_statfs(l, v, retval)
373 struct lwp *l;
374 void *v;
375 register_t *retval;
376 {
377 struct linux_sys_statfs_args /* {
378 syscallarg(const char *) path;
379 syscallarg(struct linux_statfs *) sp;
380 } */ *uap = v;
381 struct proc *p = l->l_proc;
382 struct statvfs *btmp, *bsp;
383 struct linux_statfs ltmp;
384 struct sys_statvfs1_args bsa;
385 caddr_t sg;
386 int error;
387
388 sg = stackgap_init(p, 0);
389 bsp = stackgap_alloc(p, &sg, sizeof (struct statvfs));
390
391 CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
392
393 SCARG(&bsa, path) = SCARG(uap, path);
394 SCARG(&bsa, buf) = bsp;
395 SCARG(&bsa, flags) = ST_WAIT;
396
397 if ((error = sys_statvfs1(l, &bsa, retval)))
398 return error;
399
400 btmp = STATVFSBUF_GET();
401 error = copyin(bsp, btmp, sizeof(*btmp));
402 if (error) {
403 goto out;
404 }
405 bsd_to_linux_statfs(btmp, <mp);
406 error = copyout(<mp, SCARG(uap, sp), sizeof ltmp);
407 out:
408 STATVFSBUF_PUT(btmp);
409 return error;
410 }
411
412 int
413 linux_sys_fstatfs(l, v, retval)
414 struct lwp *l;
415 void *v;
416 register_t *retval;
417 {
418 struct linux_sys_fstatfs_args /* {
419 syscallarg(int) fd;
420 syscallarg(struct linux_statfs *) sp;
421 } */ *uap = v;
422 struct proc *p = l->l_proc;
423 struct statvfs *btmp, *bsp;
424 struct linux_statfs ltmp;
425 struct sys_fstatvfs1_args bsa;
426 caddr_t sg;
427 int error;
428
429 sg = stackgap_init(p, 0);
430 bsp = stackgap_alloc(p, &sg, sizeof (struct statvfs));
431
432 SCARG(&bsa, fd) = SCARG(uap, fd);
433 SCARG(&bsa, buf) = bsp;
434 SCARG(&bsa, flags) = ST_WAIT;
435
436 if ((error = sys_fstatvfs1(l, &bsa, retval)))
437 return error;
438
439 btmp = STATVFSBUF_GET();
440 error = copyin(bsp, btmp, sizeof(*btmp));
441 if (error) {
442 goto out;
443 }
444 bsd_to_linux_statfs(btmp, <mp);
445 error = copyout(<mp, SCARG(uap, sp), sizeof ltmp);
446 out:
447 STATVFSBUF_PUT(btmp);
448 return error;
449 }
450 # endif /* !__amd64__ */
451
452 /*
453 * uname(). Just copy the info from the various strings stored in the
454 * kernel, and put it in the Linux utsname structure. That structure
455 * is almost the same as the NetBSD one, only it has fields 65 characters
456 * long, and an extra domainname field.
457 */
458 int
459 linux_sys_uname(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 mutex_enter(&p->p_smutex);
791 calcru(p, &ru.ru_utime, &ru.ru_stime, NULL, NULL);
792 ltms.ltms_utime = CONVTCK(ru.ru_utime);
793 ltms.ltms_stime = CONVTCK(ru.ru_stime);
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 mutex_exit(&p->p_smutex);
797
798 if ((error = copyout(<ms, SCARG(uap, tms), sizeof ltms)))
799 return error;
800 }
801
802 getmicrouptime(&t);
803
804 retval[0] = ((linux_clock_t)(CONVTCK(t)));
805 return 0;
806 }
807
808 #undef CONVTCK
809
810 /*
811 * Linux 'readdir' call. This code is mostly taken from the
812 * SunOS getdents call (see compat/sunos/sunos_misc.c), though
813 * an attempt has been made to keep it a little cleaner (failing
814 * miserably, because of the cruft needed if count 1 is passed).
815 *
816 * The d_off field should contain the offset of the next valid entry,
817 * but in Linux it has the offset of the entry itself. We emulate
818 * that bug here.
819 *
820 * Read in BSD-style entries, convert them, and copy them out.
821 *
822 * Note that this doesn't handle union-mounted filesystems.
823 */
824 int
825 linux_sys_getdents(l, v, retval)
826 struct lwp *l;
827 void *v;
828 register_t *retval;
829 {
830 struct linux_sys_getdents_args /* {
831 syscallarg(int) fd;
832 syscallarg(struct linux_dirent *) dent;
833 syscallarg(unsigned int) count;
834 } */ *uap = v;
835 struct dirent *bdp;
836 struct vnode *vp;
837 caddr_t inp, tbuf; /* BSD-format */
838 int len, reclen; /* BSD-format */
839 caddr_t outp; /* Linux-format */
840 int resid, linux_reclen = 0; /* Linux-format */
841 struct file *fp;
842 struct uio auio;
843 struct iovec aiov;
844 struct linux_dirent idb;
845 off_t off; /* true file offset */
846 int buflen, error, eofflag, nbytes, oldcall;
847 struct vattr va;
848 off_t *cookiebuf = NULL, *cookie;
849 int ncookies;
850
851 /* getvnode() will use the descriptor for us */
852 if ((error = getvnode(l->l_proc->p_fd, SCARG(uap, fd), &fp)) != 0)
853 return (error);
854
855 if ((fp->f_flag & FREAD) == 0) {
856 error = EBADF;
857 goto out1;
858 }
859
860 vp = (struct vnode *)fp->f_data;
861 if (vp->v_type != VDIR) {
862 error = EINVAL;
863 goto out1;
864 }
865
866 if ((error = VOP_GETATTR(vp, &va, l->l_cred, l)))
867 goto out1;
868
869 nbytes = SCARG(uap, count);
870 if (nbytes == 1) { /* emulating old, broken behaviour */
871 nbytes = sizeof (idb);
872 buflen = max(va.va_blocksize, nbytes);
873 oldcall = 1;
874 } else {
875 buflen = min(MAXBSIZE, nbytes);
876 if (buflen < va.va_blocksize)
877 buflen = va.va_blocksize;
878 oldcall = 0;
879 }
880 tbuf = malloc(buflen, M_TEMP, M_WAITOK);
881
882 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
883 off = fp->f_offset;
884 again:
885 aiov.iov_base = tbuf;
886 aiov.iov_len = buflen;
887 auio.uio_iov = &aiov;
888 auio.uio_iovcnt = 1;
889 auio.uio_rw = UIO_READ;
890 auio.uio_resid = buflen;
891 auio.uio_offset = off;
892 UIO_SETUP_SYSSPACE(&auio);
893 /*
894 * First we read into the malloc'ed buffer, then
895 * we massage it into user space, one record at a time.
896 */
897 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
898 &ncookies);
899 if (error)
900 goto out;
901
902 inp = tbuf;
903 outp = (caddr_t)SCARG(uap, dent);
904 resid = nbytes;
905 if ((len = buflen - auio.uio_resid) == 0)
906 goto eof;
907
908 for (cookie = cookiebuf; len > 0; len -= reclen) {
909 bdp = (struct dirent *)inp;
910 reclen = bdp->d_reclen;
911 if (reclen & 3)
912 panic("linux_readdir");
913 if (bdp->d_fileno == 0) {
914 inp += reclen; /* it is a hole; squish it out */
915 if (cookie)
916 off = *cookie++;
917 else
918 off += reclen;
919 continue;
920 }
921 linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
922 if (reclen > len || resid < linux_reclen) {
923 /* entry too big for buffer, so just stop */
924 outp++;
925 break;
926 }
927 /*
928 * Massage in place to make a Linux-shaped dirent (otherwise
929 * we have to worry about touching user memory outside of
930 * the copyout() call).
931 */
932 idb.d_ino = bdp->d_fileno;
933 /*
934 * The old readdir() call misuses the offset and reclen fields.
935 */
936 if (oldcall) {
937 idb.d_off = (linux_off_t)linux_reclen;
938 idb.d_reclen = (u_short)bdp->d_namlen;
939 } else {
940 if (sizeof (idb.d_off) <= 4 && (off >> 32) != 0) {
941 compat_offseterr(vp, "linux_getdents");
942 error = EINVAL;
943 goto out;
944 }
945 idb.d_off = (linux_off_t)off;
946 idb.d_reclen = (u_short)linux_reclen;
947 }
948 strcpy(idb.d_name, bdp->d_name);
949 if ((error = copyout((caddr_t)&idb, outp, linux_reclen)))
950 goto out;
951 /* advance past this real entry */
952 inp += reclen;
953 if (cookie)
954 off = *cookie++; /* each entry points to itself */
955 else
956 off += reclen;
957 /* advance output past Linux-shaped entry */
958 outp += linux_reclen;
959 resid -= linux_reclen;
960 if (oldcall)
961 break;
962 }
963
964 /* if we squished out the whole block, try again */
965 if (outp == (caddr_t)SCARG(uap, dent))
966 goto again;
967 fp->f_offset = off; /* update the vnode offset */
968
969 if (oldcall)
970 nbytes = resid + linux_reclen;
971
972 eof:
973 *retval = nbytes - resid;
974 out:
975 VOP_UNLOCK(vp, 0);
976 if (cookiebuf)
977 free(cookiebuf, M_TEMP);
978 free(tbuf, M_TEMP);
979 out1:
980 FILE_UNUSE(fp, l);
981 return error;
982 }
983
984 /*
985 * Even when just using registers to pass arguments to syscalls you can
986 * have 5 of them on the i386. So this newer version of select() does
987 * this.
988 */
989 int
990 linux_sys_select(l, v, retval)
991 struct lwp *l;
992 void *v;
993 register_t *retval;
994 {
995 struct linux_sys_select_args /* {
996 syscallarg(int) nfds;
997 syscallarg(fd_set *) readfds;
998 syscallarg(fd_set *) writefds;
999 syscallarg(fd_set *) exceptfds;
1000 syscallarg(struct timeval *) timeout;
1001 } */ *uap = v;
1002
1003 return linux_select1(l, retval, SCARG(uap, nfds), SCARG(uap, readfds),
1004 SCARG(uap, writefds), SCARG(uap, exceptfds), SCARG(uap, timeout));
1005 }
1006
1007 /*
1008 * Common code for the old and new versions of select(). A couple of
1009 * things are important:
1010 * 1) return the amount of time left in the 'timeout' parameter
1011 * 2) select never returns ERESTART on Linux, always return EINTR
1012 */
1013 int
1014 linux_select1(l, retval, nfds, readfds, writefds, exceptfds, timeout)
1015 struct lwp *l;
1016 register_t *retval;
1017 int nfds;
1018 fd_set *readfds, *writefds, *exceptfds;
1019 struct timeval *timeout;
1020 {
1021 struct sys_select_args bsa;
1022 struct proc *p = l->l_proc;
1023 struct timeval tv0, tv1, utv, *tvp;
1024 caddr_t sg;
1025 int error;
1026
1027 SCARG(&bsa, nd) = nfds;
1028 SCARG(&bsa, in) = readfds;
1029 SCARG(&bsa, ou) = writefds;
1030 SCARG(&bsa, ex) = exceptfds;
1031 SCARG(&bsa, tv) = timeout;
1032
1033 /*
1034 * Store current time for computation of the amount of
1035 * time left.
1036 */
1037 if (timeout) {
1038 if ((error = copyin(timeout, &utv, sizeof(utv))))
1039 return error;
1040 if (itimerfix(&utv)) {
1041 /*
1042 * The timeval was invalid. Convert it to something
1043 * valid that will act as it does under Linux.
1044 */
1045 sg = stackgap_init(p, 0);
1046 tvp = stackgap_alloc(p, &sg, sizeof(utv));
1047 utv.tv_sec += utv.tv_usec / 1000000;
1048 utv.tv_usec %= 1000000;
1049 if (utv.tv_usec < 0) {
1050 utv.tv_sec -= 1;
1051 utv.tv_usec += 1000000;
1052 }
1053 if (utv.tv_sec < 0)
1054 timerclear(&utv);
1055 if ((error = copyout(&utv, tvp, sizeof(utv))))
1056 return error;
1057 SCARG(&bsa, tv) = tvp;
1058 }
1059 microtime(&tv0);
1060 }
1061
1062 error = sys_select(l, &bsa, retval);
1063 if (error) {
1064 /*
1065 * See fs/select.c in the Linux kernel. Without this,
1066 * Maelstrom doesn't work.
1067 */
1068 if (error == ERESTART)
1069 error = EINTR;
1070 return error;
1071 }
1072
1073 if (timeout) {
1074 if (*retval) {
1075 /*
1076 * Compute how much time was left of the timeout,
1077 * by subtracting the current time and the time
1078 * before we started the call, and subtracting
1079 * that result from the user-supplied value.
1080 */
1081 microtime(&tv1);
1082 timersub(&tv1, &tv0, &tv1);
1083 timersub(&utv, &tv1, &utv);
1084 if (utv.tv_sec < 0)
1085 timerclear(&utv);
1086 } else
1087 timerclear(&utv);
1088 if ((error = copyout(&utv, timeout, sizeof(utv))))
1089 return error;
1090 }
1091
1092 return 0;
1093 }
1094
1095 /*
1096 * Get the process group of a certain process. Look it up
1097 * and return the value.
1098 */
1099 int
1100 linux_sys_getpgid(l, v, retval)
1101 struct lwp *l;
1102 void *v;
1103 register_t *retval;
1104 {
1105 struct linux_sys_getpgid_args /* {
1106 syscallarg(int) pid;
1107 } */ *uap = v;
1108 struct proc *p = l->l_proc;
1109 struct proc *targp;
1110
1111 if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != p->p_pid) {
1112 if ((targp = pfind(SCARG(uap, pid))) == 0)
1113 return ESRCH;
1114 }
1115 else
1116 targp = p;
1117
1118 retval[0] = targp->p_pgid;
1119 return 0;
1120 }
1121
1122 /*
1123 * Set the 'personality' (emulation mode) for the current process. Only
1124 * accept the Linux personality here (0). This call is needed because
1125 * the Linux ELF crt0 issues it in an ugly kludge to make sure that
1126 * ELF binaries run in Linux mode, not SVR4 mode.
1127 */
1128 int
1129 linux_sys_personality(l, v, retval)
1130 struct lwp *l;
1131 void *v;
1132 register_t *retval;
1133 {
1134 struct linux_sys_personality_args /* {
1135 syscallarg(int) per;
1136 } */ *uap = v;
1137
1138 if (SCARG(uap, per) != 0)
1139 return EINVAL;
1140 retval[0] = 0;
1141 return 0;
1142 }
1143 #endif /* !COMPAT_LINUX32 */
1144
1145 #if defined(__i386__) || defined(__m68k__) || defined(COMPAT_LINUX32)
1146 /*
1147 * The calls are here because of type conversions.
1148 */
1149 int
1150 linux_sys_setreuid16(l, v, retval)
1151 struct lwp *l;
1152 void *v;
1153 register_t *retval;
1154 {
1155 struct linux_sys_setreuid16_args /* {
1156 syscallarg(int) ruid;
1157 syscallarg(int) euid;
1158 } */ *uap = v;
1159 struct sys_setreuid_args bsa;
1160
1161 SCARG(&bsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
1162 (uid_t)-1 : SCARG(uap, ruid);
1163 SCARG(&bsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
1164 (uid_t)-1 : SCARG(uap, euid);
1165
1166 return sys_setreuid(l, &bsa, retval);
1167 }
1168
1169 int
1170 linux_sys_setregid16(l, v, retval)
1171 struct lwp *l;
1172 void *v;
1173 register_t *retval;
1174 {
1175 struct linux_sys_setregid16_args /* {
1176 syscallarg(int) rgid;
1177 syscallarg(int) egid;
1178 } */ *uap = v;
1179 struct sys_setregid_args bsa;
1180
1181 SCARG(&bsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
1182 (uid_t)-1 : SCARG(uap, rgid);
1183 SCARG(&bsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
1184 (uid_t)-1 : SCARG(uap, egid);
1185
1186 return sys_setregid(l, &bsa, retval);
1187 }
1188
1189 int
1190 linux_sys_setresuid16(l, v, retval)
1191 struct lwp *l;
1192 void *v;
1193 register_t *retval;
1194 {
1195 struct linux_sys_setresuid16_args /* {
1196 syscallarg(uid_t) ruid;
1197 syscallarg(uid_t) euid;
1198 syscallarg(uid_t) suid;
1199 } */ *uap = v;
1200 struct linux_sys_setresuid16_args lsa;
1201
1202 SCARG(&lsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
1203 (uid_t)-1 : SCARG(uap, ruid);
1204 SCARG(&lsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
1205 (uid_t)-1 : SCARG(uap, euid);
1206 SCARG(&lsa, suid) = ((linux_uid_t)SCARG(uap, suid) == (linux_uid_t)-1) ?
1207 (uid_t)-1 : SCARG(uap, suid);
1208
1209 return linux_sys_setresuid(l, &lsa, retval);
1210 }
1211
1212 int
1213 linux_sys_setresgid16(l, v, retval)
1214 struct lwp *l;
1215 void *v;
1216 register_t *retval;
1217 {
1218 struct linux_sys_setresgid16_args /* {
1219 syscallarg(gid_t) rgid;
1220 syscallarg(gid_t) egid;
1221 syscallarg(gid_t) sgid;
1222 } */ *uap = v;
1223 struct linux_sys_setresgid16_args lsa;
1224
1225 SCARG(&lsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
1226 (gid_t)-1 : SCARG(uap, rgid);
1227 SCARG(&lsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
1228 (gid_t)-1 : SCARG(uap, egid);
1229 SCARG(&lsa, sgid) = ((linux_gid_t)SCARG(uap, sgid) == (linux_gid_t)-1) ?
1230 (gid_t)-1 : SCARG(uap, sgid);
1231
1232 return linux_sys_setresgid(l, &lsa, retval);
1233 }
1234
1235 int
1236 linux_sys_getgroups16(l, v, retval)
1237 struct lwp *l;
1238 void *v;
1239 register_t *retval;
1240 {
1241 struct linux_sys_getgroups16_args /* {
1242 syscallarg(int) gidsetsize;
1243 syscallarg(linux_gid_t *) gidset;
1244 } */ *uap = v;
1245 struct proc *p = l->l_proc;
1246 caddr_t sg;
1247 int n, error, i;
1248 struct sys_getgroups_args bsa;
1249 gid_t *bset, *kbset;
1250 linux_gid_t *lset;
1251 kauth_cred_t pc = l->l_cred;
1252
1253 n = SCARG(uap, gidsetsize);
1254 if (n < 0)
1255 return EINVAL;
1256 error = 0;
1257 bset = kbset = NULL;
1258 lset = NULL;
1259 if (n > 0) {
1260 n = min(kauth_cred_ngroups(pc), n);
1261 sg = stackgap_init(p, 0);
1262 bset = stackgap_alloc(p, &sg, n * sizeof (gid_t));
1263 kbset = malloc(n * sizeof (gid_t), M_TEMP, M_WAITOK);
1264 lset = malloc(n * sizeof (linux_gid_t), M_TEMP, M_WAITOK);
1265 if (bset == NULL || kbset == NULL || lset == NULL)
1266 {
1267 error = ENOMEM;
1268 goto out;
1269 }
1270 SCARG(&bsa, gidsetsize) = n;
1271 SCARG(&bsa, gidset) = bset;
1272 error = sys_getgroups(l, &bsa, retval);
1273 if (error != 0)
1274 goto out;
1275 error = copyin(bset, kbset, n * sizeof (gid_t));
1276 if (error != 0)
1277 goto out;
1278 for (i = 0; i < n; i++)
1279 lset[i] = (linux_gid_t)kbset[i];
1280 error = copyout(lset, SCARG(uap, gidset),
1281 n * sizeof (linux_gid_t));
1282 } else
1283 *retval = kauth_cred_ngroups(pc);
1284 out:
1285 if (kbset != NULL)
1286 free(kbset, M_TEMP);
1287 if (lset != NULL)
1288 free(lset, M_TEMP);
1289 return error;
1290 }
1291
1292 int
1293 linux_sys_setgroups16(l, v, retval)
1294 struct lwp *l;
1295 void *v;
1296 register_t *retval;
1297 {
1298 struct linux_sys_setgroups16_args /* {
1299 syscallarg(int) gidsetsize;
1300 syscallarg(linux_gid_t *) gidset;
1301 } */ *uap = v;
1302 struct proc *p = l->l_proc;
1303 caddr_t sg;
1304 int n;
1305 int error, i;
1306 struct sys_setgroups_args bsa;
1307 gid_t *bset, *kbset;
1308 linux_gid_t *lset;
1309
1310 n = SCARG(uap, gidsetsize);
1311 if (n < 0 || n > NGROUPS)
1312 return EINVAL;
1313 sg = stackgap_init(p, 0);
1314 bset = stackgap_alloc(p, &sg, n * sizeof (gid_t));
1315 lset = malloc(n * sizeof (linux_gid_t), M_TEMP, M_WAITOK);
1316 kbset = malloc(n * sizeof (gid_t), M_TEMP, M_WAITOK);
1317 if (bset == NULL || kbset == NULL || lset == NULL)
1318 {
1319 error = ENOMEM;
1320 goto out;
1321 }
1322 error = copyin(SCARG(uap, gidset), lset, n * sizeof (linux_gid_t));
1323 if (error != 0)
1324 goto out;
1325 for (i = 0; i < n; i++)
1326 kbset[i] = (gid_t)lset[i];
1327 error = copyout(kbset, bset, n * sizeof (gid_t));
1328 if (error != 0)
1329 goto out;
1330 SCARG(&bsa, gidsetsize) = n;
1331 SCARG(&bsa, gidset) = bset;
1332 error = sys_setgroups(l, &bsa, retval);
1333
1334 out:
1335 if (lset != NULL)
1336 free(lset, M_TEMP);
1337 if (kbset != NULL)
1338 free(kbset, M_TEMP);
1339
1340 return error;
1341 }
1342
1343 #endif /* __i386__ || __m68k__ || COMPAT_LINUX32 */
1344
1345 #ifndef COMPAT_LINUX32
1346 /*
1347 * We have nonexistent fsuid equal to uid.
1348 * If modification is requested, refuse.
1349 */
1350 int
1351 linux_sys_setfsuid(l, v, retval)
1352 struct lwp *l;
1353 void *v;
1354 register_t *retval;
1355 {
1356 struct linux_sys_setfsuid_args /* {
1357 syscallarg(uid_t) uid;
1358 } */ *uap = v;
1359 uid_t uid;
1360
1361 uid = SCARG(uap, uid);
1362 if (kauth_cred_getuid(l->l_cred) != uid)
1363 return sys_nosys(l, v, retval);
1364 else
1365 return (0);
1366 }
1367
1368 /* XXX XXX XXX */
1369 # ifndef alpha
1370 int
1371 linux_sys_getfsuid(l, v, retval)
1372 struct lwp *l;
1373 void *v;
1374 register_t *retval;
1375 {
1376 return sys_getuid(l, v, retval);
1377 }
1378 # endif
1379
1380 int
1381 linux_sys_setresuid(l, v, retval)
1382 struct lwp *l;
1383 void *v;
1384 register_t *retval;
1385 {
1386 struct linux_sys_setresuid_args /* {
1387 syscallarg(uid_t) ruid;
1388 syscallarg(uid_t) euid;
1389 syscallarg(uid_t) suid;
1390 } */ *uap = v;
1391
1392 /*
1393 * Note: These checks are a little different than the NetBSD
1394 * setreuid(2) call performs. This precisely follows the
1395 * behavior of the Linux kernel.
1396 */
1397
1398 return do_setresuid(l, SCARG(uap, ruid), SCARG(uap, euid),
1399 SCARG(uap, suid),
1400 ID_R_EQ_R | ID_R_EQ_E | ID_R_EQ_S |
1401 ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S |
1402 ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S );
1403 }
1404
1405 int
1406 linux_sys_getresuid(l, v, retval)
1407 struct lwp *l;
1408 void *v;
1409 register_t *retval;
1410 {
1411 struct linux_sys_getresuid_args /* {
1412 syscallarg(uid_t *) ruid;
1413 syscallarg(uid_t *) euid;
1414 syscallarg(uid_t *) suid;
1415 } */ *uap = v;
1416 kauth_cred_t pc = l->l_cred;
1417 int error;
1418 uid_t uid;
1419
1420 /*
1421 * Linux copies these values out to userspace like so:
1422 *
1423 * 1. Copy out ruid.
1424 * 2. If that succeeds, copy out euid.
1425 * 3. If both of those succeed, copy out suid.
1426 */
1427 uid = kauth_cred_getuid(pc);
1428 if ((error = copyout(&uid, SCARG(uap, ruid), sizeof(uid_t))) != 0)
1429 return (error);
1430
1431 uid = kauth_cred_geteuid(pc);
1432 if ((error = copyout(&uid, SCARG(uap, euid), sizeof(uid_t))) != 0)
1433 return (error);
1434
1435 uid = kauth_cred_getsvuid(pc);
1436
1437 return (copyout(&uid, SCARG(uap, suid), sizeof(uid_t)));
1438 }
1439
1440 int
1441 linux_sys_ptrace(l, v, retval)
1442 struct lwp *l;
1443 void *v;
1444 register_t *retval;
1445 {
1446 #if defined(PTRACE) || defined(_LKM)
1447 struct linux_sys_ptrace_args /* {
1448 i386, m68k, powerpc: T=int
1449 alpha, amd64: T=long
1450 syscallarg(T) request;
1451 syscallarg(T) pid;
1452 syscallarg(T) addr;
1453 syscallarg(T) data;
1454 } */ *uap = v;
1455 const int *ptr;
1456 int request;
1457 int error;
1458 #ifdef _LKM
1459 #define sys_ptrace (*sysent[SYS_ptrace].sy_call)
1460 #endif
1461
1462 ptr = linux_ptrace_request_map;
1463 request = SCARG(uap, request);
1464 while (*ptr != -1)
1465 if (*ptr++ == request) {
1466 struct sys_ptrace_args pta;
1467
1468 SCARG(&pta, req) = *ptr;
1469 SCARG(&pta, pid) = SCARG(uap, pid);
1470 SCARG(&pta, addr) = (caddr_t)SCARG(uap, addr);
1471 SCARG(&pta, data) = SCARG(uap, data);
1472
1473 /*
1474 * Linux ptrace(PTRACE_CONT, pid, 0, 0) means actually
1475 * to continue where the process left off previously.
1476 * The same thing is achieved by addr == (caddr_t) 1
1477 * on NetBSD, so rewrite 'addr' appropriately.
1478 */
1479 if (request == LINUX_PTRACE_CONT && SCARG(uap, addr)==0)
1480 SCARG(&pta, addr) = (caddr_t) 1;
1481
1482 error = sys_ptrace(l, &pta, retval);
1483 if (error)
1484 return error;
1485 switch (request) {
1486 case LINUX_PTRACE_PEEKTEXT:
1487 case LINUX_PTRACE_PEEKDATA:
1488 error = copyout (retval,
1489 (caddr_t)SCARG(uap, data),
1490 sizeof *retval);
1491 *retval = SCARG(uap, data);
1492 break;
1493 default:
1494 break;
1495 }
1496 return error;
1497 }
1498 else
1499 ptr++;
1500
1501 return LINUX_SYS_PTRACE_ARCH(l, uap, retval);
1502 #else
1503 return ENOSYS;
1504 #endif /* PTRACE || _LKM */
1505 }
1506
1507 int
1508 linux_sys_reboot(struct lwp *l, void *v, register_t *retval)
1509 {
1510 struct linux_sys_reboot_args /* {
1511 syscallarg(int) magic1;
1512 syscallarg(int) magic2;
1513 syscallarg(int) cmd;
1514 syscallarg(void *) arg;
1515 } */ *uap = v;
1516 struct sys_reboot_args /* {
1517 syscallarg(int) opt;
1518 syscallarg(char *) bootstr;
1519 } */ sra;
1520 int error;
1521
1522 if ((error = kauth_authorize_generic(l->l_cred,
1523 KAUTH_GENERIC_ISSUSER, &l->l_acflag)) != 0)
1524 return(error);
1525
1526 if (SCARG(uap, magic1) != LINUX_REBOOT_MAGIC1)
1527 return(EINVAL);
1528 if (SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2 &&
1529 SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2A &&
1530 SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2B)
1531 return(EINVAL);
1532
1533 switch (SCARG(uap, cmd)) {
1534 case LINUX_REBOOT_CMD_RESTART:
1535 SCARG(&sra, opt) = RB_AUTOBOOT;
1536 break;
1537 case LINUX_REBOOT_CMD_HALT:
1538 SCARG(&sra, opt) = RB_HALT;
1539 break;
1540 case LINUX_REBOOT_CMD_POWER_OFF:
1541 SCARG(&sra, opt) = RB_HALT|RB_POWERDOWN;
1542 break;
1543 case LINUX_REBOOT_CMD_RESTART2:
1544 /* Reboot with an argument. */
1545 SCARG(&sra, opt) = RB_AUTOBOOT|RB_STRING;
1546 SCARG(&sra, bootstr) = SCARG(uap, arg);
1547 break;
1548 case LINUX_REBOOT_CMD_CAD_ON:
1549 return(EINVAL); /* We don't implement ctrl-alt-delete */
1550 case LINUX_REBOOT_CMD_CAD_OFF:
1551 return(0);
1552 default:
1553 return(EINVAL);
1554 }
1555
1556 return(sys_reboot(l, &sra, retval));
1557 }
1558
1559 /*
1560 * Copy of compat_12_sys_swapon().
1561 */
1562 int
1563 linux_sys_swapon(l, v, retval)
1564 struct lwp *l;
1565 void *v;
1566 register_t *retval;
1567 {
1568 struct sys_swapctl_args ua;
1569 struct linux_sys_swapon_args /* {
1570 syscallarg(const char *) name;
1571 } */ *uap = v;
1572
1573 SCARG(&ua, cmd) = SWAP_ON;
1574 SCARG(&ua, arg) = (void *)__UNCONST(SCARG(uap, name));
1575 SCARG(&ua, misc) = 0; /* priority */
1576 return (sys_swapctl(l, &ua, retval));
1577 }
1578
1579 /*
1580 * Stop swapping to the file or block device specified by path.
1581 */
1582 int
1583 linux_sys_swapoff(l, v, retval)
1584 struct lwp *l;
1585 void *v;
1586 register_t *retval;
1587 {
1588 struct sys_swapctl_args ua;
1589 struct linux_sys_swapoff_args /* {
1590 syscallarg(const char *) path;
1591 } */ *uap = v;
1592
1593 SCARG(&ua, cmd) = SWAP_OFF;
1594 SCARG(&ua, arg) = __UNCONST(SCARG(uap, path)); /*XXXUNCONST*/
1595 return (sys_swapctl(l, &ua, retval));
1596 }
1597
1598 /*
1599 * Copy of compat_09_sys_setdomainname()
1600 */
1601 /* ARGSUSED */
1602 int
1603 linux_sys_setdomainname(l, v, retval)
1604 struct lwp *l;
1605 void *v;
1606 register_t *retval;
1607 {
1608 struct linux_sys_setdomainname_args /* {
1609 syscallarg(char *) domainname;
1610 syscallarg(int) len;
1611 } */ *uap = v;
1612 int name[2];
1613
1614 name[0] = CTL_KERN;
1615 name[1] = KERN_DOMAINNAME;
1616 return (old_sysctl(&name[0], 2, 0, 0, SCARG(uap, domainname),
1617 SCARG(uap, len), l));
1618 }
1619
1620 /*
1621 * sysinfo()
1622 */
1623 /* ARGSUSED */
1624 int
1625 linux_sys_sysinfo(l, v, retval)
1626 struct lwp *l;
1627 void *v;
1628 register_t *retval;
1629 {
1630 struct linux_sys_sysinfo_args /* {
1631 syscallarg(struct linux_sysinfo *) arg;
1632 } */ *uap = v;
1633 struct linux_sysinfo si;
1634 struct loadavg *la;
1635
1636 si.uptime = time_uptime;
1637 la = &averunnable;
1638 si.loads[0] = la->ldavg[0] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
1639 si.loads[1] = la->ldavg[1] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
1640 si.loads[2] = la->ldavg[2] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
1641 si.totalram = ctob(physmem);
1642 si.freeram = uvmexp.free * uvmexp.pagesize;
1643 si.sharedram = 0; /* XXX */
1644 si.bufferram = uvmexp.filepages * uvmexp.pagesize;
1645 si.totalswap = uvmexp.swpages * uvmexp.pagesize;
1646 si.freeswap = (uvmexp.swpages - uvmexp.swpginuse) * uvmexp.pagesize;
1647 si.procs = nprocs;
1648
1649 /* The following are only present in newer Linux kernels. */
1650 si.totalbig = 0;
1651 si.freebig = 0;
1652 si.mem_unit = 1;
1653
1654 return (copyout(&si, SCARG(uap, arg), sizeof si));
1655 }
1656
1657 int
1658 linux_sys_getrlimit(l, v, retval)
1659 struct lwp *l;
1660 void *v;
1661 register_t *retval;
1662 {
1663 struct linux_sys_getrlimit_args /* {
1664 syscallarg(int) which;
1665 # ifdef LINUX_LARGEFILE64
1666 syscallarg(struct rlimit *) rlp;
1667 # else
1668 syscallarg(struct orlimit *) rlp;
1669 # endif
1670 } */ *uap = v;
1671 struct proc *p = l->l_proc;
1672 caddr_t sg = stackgap_init(p, 0);
1673 struct sys_getrlimit_args ap;
1674 struct rlimit rl;
1675 # ifdef LINUX_LARGEFILE64
1676 struct rlimit orl;
1677 # else
1678 struct orlimit orl;
1679 # endif
1680 int error;
1681
1682 SCARG(&ap, which) = linux_to_bsd_limit(SCARG(uap, which));
1683 if ((error = SCARG(&ap, which)) < 0)
1684 return -error;
1685 SCARG(&ap, rlp) = stackgap_alloc(p, &sg, sizeof rl);
1686 if ((error = sys_getrlimit(l, &ap, retval)) != 0)
1687 return error;
1688 if ((error = copyin(SCARG(&ap, rlp), &rl, sizeof(rl))) != 0)
1689 return error;
1690 bsd_to_linux_rlimit(&orl, &rl);
1691
1692 return copyout(&orl, SCARG(uap, rlp), sizeof(orl));
1693 }
1694
1695 int
1696 linux_sys_setrlimit(l, v, retval)
1697 struct lwp *l;
1698 void *v;
1699 register_t *retval;
1700 {
1701 struct linux_sys_setrlimit_args /* {
1702 syscallarg(int) which;
1703 # ifdef LINUX_LARGEFILE64
1704 syscallarg(struct rlimit *) rlp;
1705 # else
1706 syscallarg(struct orlimit *) rlp;
1707 # endif
1708 } */ *uap = v;
1709 struct proc *p = l->l_proc;
1710 caddr_t sg = stackgap_init(p, 0);
1711 struct sys_getrlimit_args ap;
1712 struct rlimit rl;
1713 # ifdef LINUX_LARGEFILE64
1714 struct rlimit orl;
1715 # else
1716 struct orlimit orl;
1717 # endif
1718 int error;
1719
1720 SCARG(&ap, which) = linux_to_bsd_limit(SCARG(uap, which));
1721 SCARG(&ap, rlp) = stackgap_alloc(p, &sg, sizeof rl);
1722 if ((error = SCARG(&ap, which)) < 0)
1723 return -error;
1724 if ((error = copyin(SCARG(uap, rlp), &orl, sizeof(orl))) != 0)
1725 return error;
1726 linux_to_bsd_rlimit(&rl, &orl);
1727 if ((error = copyout(&rl, SCARG(&ap, rlp), sizeof(rl))) != 0)
1728 return error;
1729 return sys_setrlimit(l, &ap, retval);
1730 }
1731
1732 # if !defined(__mips__) && !defined(__amd64__)
1733 /* XXX: this doesn't look 100% common, at least mips doesn't have it */
1734 int
1735 linux_sys_ugetrlimit(l, v, retval)
1736 struct lwp *l;
1737 void *v;
1738 register_t *retval;
1739 {
1740 return linux_sys_getrlimit(l, v, retval);
1741 }
1742 # endif
1743
1744 /*
1745 * This gets called for unsupported syscalls. The difference to sys_nosys()
1746 * is that process does not get SIGSYS, the call just returns with ENOSYS.
1747 * This is the way Linux does it and glibc depends on this behaviour.
1748 */
1749 int
1750 linux_sys_nosys(l, v, retval)
1751 struct lwp *l;
1752 void *v;
1753 register_t *retval;
1754 {
1755 return (ENOSYS);
1756 }
1757
1758 int
1759 linux_sys_getpriority(l, v, retval)
1760 struct lwp *l;
1761 void *v;
1762 register_t *retval;
1763 {
1764 struct linux_sys_getpriority_args /* {
1765 syscallarg(int) which;
1766 syscallarg(int) who;
1767 } */ *uap = v;
1768 struct sys_getpriority_args bsa;
1769 int error;
1770
1771 SCARG(&bsa, which) = SCARG(uap, which);
1772 SCARG(&bsa, who) = SCARG(uap, who);
1773
1774 if ((error = sys_getpriority(l, &bsa, retval)))
1775 return error;
1776
1777 *retval = NZERO - *retval;
1778
1779 return 0;
1780 }
1781
1782 #endif /* !COMPAT_LINUX32 */
1783