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