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