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