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