linux_misc.c revision 1.165.2.2 1 /* $NetBSD: linux_misc.c,v 1.165.2.2 2007/04/20 20:26:04 bouyer 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.2 2007/04/20 20:26:04 bouyer 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 goto again;
904 fp->f_offset = off; /* update the vnode offset */
905
906 if (oldcall)
907 nbytes = resid + linux_reclen;
908
909 eof:
910 *retval = nbytes - resid;
911 out:
912 VOP_UNLOCK(vp, 0);
913 if (cookiebuf)
914 free(cookiebuf, M_TEMP);
915 free(tbuf, M_TEMP);
916 out1:
917 FILE_UNUSE(fp, l);
918 return error;
919 }
920
921 /*
922 * Even when just using registers to pass arguments to syscalls you can
923 * have 5 of them on the i386. So this newer version of select() does
924 * this.
925 */
926 int
927 linux_sys_select(l, v, retval)
928 struct lwp *l;
929 void *v;
930 register_t *retval;
931 {
932 struct linux_sys_select_args /* {
933 syscallarg(int) nfds;
934 syscallarg(fd_set *) readfds;
935 syscallarg(fd_set *) writefds;
936 syscallarg(fd_set *) exceptfds;
937 syscallarg(struct timeval *) timeout;
938 } */ *uap = v;
939
940 return linux_select1(l, retval, SCARG(uap, nfds), SCARG(uap, readfds),
941 SCARG(uap, writefds), SCARG(uap, exceptfds), SCARG(uap, timeout));
942 }
943
944 /*
945 * Common code for the old and new versions of select(). A couple of
946 * things are important:
947 * 1) return the amount of time left in the 'timeout' parameter
948 * 2) select never returns ERESTART on Linux, always return EINTR
949 */
950 int
951 linux_select1(l, retval, nfds, readfds, writefds, exceptfds, timeout)
952 struct lwp *l;
953 register_t *retval;
954 int nfds;
955 fd_set *readfds, *writefds, *exceptfds;
956 struct timeval *timeout;
957 {
958 struct sys_select_args bsa;
959 struct proc *p = l->l_proc;
960 struct timeval tv0, tv1, utv, *tvp;
961 caddr_t sg;
962 int error;
963
964 SCARG(&bsa, nd) = nfds;
965 SCARG(&bsa, in) = readfds;
966 SCARG(&bsa, ou) = writefds;
967 SCARG(&bsa, ex) = exceptfds;
968 SCARG(&bsa, tv) = timeout;
969
970 /*
971 * Store current time for computation of the amount of
972 * time left.
973 */
974 if (timeout) {
975 if ((error = copyin(timeout, &utv, sizeof(utv))))
976 return error;
977 if (itimerfix(&utv)) {
978 /*
979 * The timeval was invalid. Convert it to something
980 * valid that will act as it does under Linux.
981 */
982 sg = stackgap_init(p, 0);
983 tvp = stackgap_alloc(p, &sg, sizeof(utv));
984 utv.tv_sec += utv.tv_usec / 1000000;
985 utv.tv_usec %= 1000000;
986 if (utv.tv_usec < 0) {
987 utv.tv_sec -= 1;
988 utv.tv_usec += 1000000;
989 }
990 if (utv.tv_sec < 0)
991 timerclear(&utv);
992 if ((error = copyout(&utv, tvp, sizeof(utv))))
993 return error;
994 SCARG(&bsa, tv) = tvp;
995 }
996 microtime(&tv0);
997 }
998
999 error = sys_select(l, &bsa, retval);
1000 if (error) {
1001 /*
1002 * See fs/select.c in the Linux kernel. Without this,
1003 * Maelstrom doesn't work.
1004 */
1005 if (error == ERESTART)
1006 error = EINTR;
1007 return error;
1008 }
1009
1010 if (timeout) {
1011 if (*retval) {
1012 /*
1013 * Compute how much time was left of the timeout,
1014 * by subtracting the current time and the time
1015 * before we started the call, and subtracting
1016 * that result from the user-supplied value.
1017 */
1018 microtime(&tv1);
1019 timersub(&tv1, &tv0, &tv1);
1020 timersub(&utv, &tv1, &utv);
1021 if (utv.tv_sec < 0)
1022 timerclear(&utv);
1023 } else
1024 timerclear(&utv);
1025 if ((error = copyout(&utv, timeout, sizeof(utv))))
1026 return error;
1027 }
1028
1029 return 0;
1030 }
1031
1032 /*
1033 * Get the process group of a certain process. Look it up
1034 * and return the value.
1035 */
1036 int
1037 linux_sys_getpgid(l, v, retval)
1038 struct lwp *l;
1039 void *v;
1040 register_t *retval;
1041 {
1042 struct linux_sys_getpgid_args /* {
1043 syscallarg(int) pid;
1044 } */ *uap = v;
1045 struct proc *p = l->l_proc;
1046 struct proc *targp;
1047
1048 if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != p->p_pid) {
1049 if ((targp = pfind(SCARG(uap, pid))) == 0)
1050 return ESRCH;
1051 }
1052 else
1053 targp = p;
1054
1055 retval[0] = targp->p_pgid;
1056 return 0;
1057 }
1058
1059 /*
1060 * Set the 'personality' (emulation mode) for the current process. Only
1061 * accept the Linux personality here (0). This call is needed because
1062 * the Linux ELF crt0 issues it in an ugly kludge to make sure that
1063 * ELF binaries run in Linux mode, not SVR4 mode.
1064 */
1065 int
1066 linux_sys_personality(struct lwp *l, void *v, register_t *retval)
1067 {
1068 struct linux_sys_personality_args /* {
1069 syscallarg(int) per;
1070 } */ *uap = v;
1071
1072 if (SCARG(uap, per) != 0)
1073 return EINVAL;
1074 retval[0] = 0;
1075 return 0;
1076 }
1077 #endif /* !COMPAT_LINUX32 */
1078
1079 #if defined(__i386__) || defined(__m68k__) || defined(COMPAT_LINUX32)
1080 /*
1081 * The calls are here because of type conversions.
1082 */
1083 int
1084 linux_sys_setreuid16(l, v, retval)
1085 struct lwp *l;
1086 void *v;
1087 register_t *retval;
1088 {
1089 struct linux_sys_setreuid16_args /* {
1090 syscallarg(int) ruid;
1091 syscallarg(int) euid;
1092 } */ *uap = v;
1093 struct sys_setreuid_args bsa;
1094
1095 SCARG(&bsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
1096 (uid_t)-1 : SCARG(uap, ruid);
1097 SCARG(&bsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
1098 (uid_t)-1 : SCARG(uap, euid);
1099
1100 return sys_setreuid(l, &bsa, retval);
1101 }
1102
1103 int
1104 linux_sys_setregid16(l, v, retval)
1105 struct lwp *l;
1106 void *v;
1107 register_t *retval;
1108 {
1109 struct linux_sys_setregid16_args /* {
1110 syscallarg(int) rgid;
1111 syscallarg(int) egid;
1112 } */ *uap = v;
1113 struct sys_setregid_args bsa;
1114
1115 SCARG(&bsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
1116 (uid_t)-1 : SCARG(uap, rgid);
1117 SCARG(&bsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
1118 (uid_t)-1 : SCARG(uap, egid);
1119
1120 return sys_setregid(l, &bsa, retval);
1121 }
1122
1123 int
1124 linux_sys_setresuid16(l, v, retval)
1125 struct lwp *l;
1126 void *v;
1127 register_t *retval;
1128 {
1129 struct linux_sys_setresuid16_args /* {
1130 syscallarg(uid_t) ruid;
1131 syscallarg(uid_t) euid;
1132 syscallarg(uid_t) suid;
1133 } */ *uap = v;
1134 struct linux_sys_setresuid16_args lsa;
1135
1136 SCARG(&lsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
1137 (uid_t)-1 : SCARG(uap, ruid);
1138 SCARG(&lsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
1139 (uid_t)-1 : SCARG(uap, euid);
1140 SCARG(&lsa, suid) = ((linux_uid_t)SCARG(uap, suid) == (linux_uid_t)-1) ?
1141 (uid_t)-1 : SCARG(uap, suid);
1142
1143 return linux_sys_setresuid(l, &lsa, retval);
1144 }
1145
1146 int
1147 linux_sys_setresgid16(l, v, retval)
1148 struct lwp *l;
1149 void *v;
1150 register_t *retval;
1151 {
1152 struct linux_sys_setresgid16_args /* {
1153 syscallarg(gid_t) rgid;
1154 syscallarg(gid_t) egid;
1155 syscallarg(gid_t) sgid;
1156 } */ *uap = v;
1157 struct linux_sys_setresgid16_args lsa;
1158
1159 SCARG(&lsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
1160 (gid_t)-1 : SCARG(uap, rgid);
1161 SCARG(&lsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
1162 (gid_t)-1 : SCARG(uap, egid);
1163 SCARG(&lsa, sgid) = ((linux_gid_t)SCARG(uap, sgid) == (linux_gid_t)-1) ?
1164 (gid_t)-1 : SCARG(uap, sgid);
1165
1166 return linux_sys_setresgid(l, &lsa, retval);
1167 }
1168
1169 int
1170 linux_sys_getgroups16(l, v, retval)
1171 struct lwp *l;
1172 void *v;
1173 register_t *retval;
1174 {
1175 struct linux_sys_getgroups16_args /* {
1176 syscallarg(int) gidsetsize;
1177 syscallarg(linux_gid_t *) gidset;
1178 } */ *uap = v;
1179 struct proc *p = l->l_proc;
1180 caddr_t sg;
1181 int n, error, i;
1182 struct sys_getgroups_args bsa;
1183 gid_t *bset, *kbset;
1184 linux_gid_t *lset;
1185 kauth_cred_t pc = l->l_cred;
1186
1187 n = SCARG(uap, gidsetsize);
1188 if (n < 0)
1189 return EINVAL;
1190 error = 0;
1191 bset = kbset = NULL;
1192 lset = NULL;
1193 if (n > 0) {
1194 n = min(kauth_cred_ngroups(pc), n);
1195 sg = stackgap_init(p, 0);
1196 bset = stackgap_alloc(p, &sg, n * sizeof (gid_t));
1197 kbset = malloc(n * sizeof (gid_t), M_TEMP, M_WAITOK);
1198 lset = malloc(n * sizeof (linux_gid_t), M_TEMP, M_WAITOK);
1199 if (bset == NULL || kbset == NULL || lset == NULL)
1200 {
1201 error = ENOMEM;
1202 goto out;
1203 }
1204 SCARG(&bsa, gidsetsize) = n;
1205 SCARG(&bsa, gidset) = bset;
1206 error = sys_getgroups(l, &bsa, retval);
1207 if (error != 0)
1208 goto out;
1209 error = copyin(bset, kbset, n * sizeof (gid_t));
1210 if (error != 0)
1211 goto out;
1212 for (i = 0; i < n; i++)
1213 lset[i] = (linux_gid_t)kbset[i];
1214 error = copyout(lset, SCARG(uap, gidset),
1215 n * sizeof (linux_gid_t));
1216 } else
1217 *retval = kauth_cred_ngroups(pc);
1218 out:
1219 if (kbset != NULL)
1220 free(kbset, M_TEMP);
1221 if (lset != NULL)
1222 free(lset, M_TEMP);
1223 return error;
1224 }
1225
1226 int
1227 linux_sys_setgroups16(l, v, retval)
1228 struct lwp *l;
1229 void *v;
1230 register_t *retval;
1231 {
1232 struct linux_sys_setgroups16_args /* {
1233 syscallarg(int) gidsetsize;
1234 syscallarg(linux_gid_t *) gidset;
1235 } */ *uap = v;
1236 struct proc *p = l->l_proc;
1237 caddr_t sg;
1238 int n;
1239 int error, i;
1240 struct sys_setgroups_args bsa;
1241 gid_t *bset, *kbset;
1242 linux_gid_t *lset;
1243
1244 n = SCARG(uap, gidsetsize);
1245 if (n < 0 || n > NGROUPS)
1246 return EINVAL;
1247 sg = stackgap_init(p, 0);
1248 bset = stackgap_alloc(p, &sg, n * sizeof (gid_t));
1249 lset = malloc(n * sizeof (linux_gid_t), M_TEMP, M_WAITOK);
1250 kbset = malloc(n * sizeof (gid_t), M_TEMP, M_WAITOK);
1251 if (bset == NULL || kbset == NULL || lset == NULL)
1252 {
1253 error = ENOMEM;
1254 goto out;
1255 }
1256 error = copyin(SCARG(uap, gidset), lset, n * sizeof (linux_gid_t));
1257 if (error != 0)
1258 goto out;
1259 for (i = 0; i < n; i++)
1260 kbset[i] = (gid_t)lset[i];
1261 error = copyout(kbset, bset, n * sizeof (gid_t));
1262 if (error != 0)
1263 goto out;
1264 SCARG(&bsa, gidsetsize) = n;
1265 SCARG(&bsa, gidset) = bset;
1266 error = sys_setgroups(l, &bsa, retval);
1267
1268 out:
1269 if (lset != NULL)
1270 free(lset, M_TEMP);
1271 if (kbset != NULL)
1272 free(kbset, M_TEMP);
1273
1274 return error;
1275 }
1276
1277 #endif /* __i386__ || __m68k__ || COMPAT_LINUX32 */
1278
1279 #ifndef COMPAT_LINUX32
1280 /*
1281 * We have nonexistent fsuid equal to uid.
1282 * If modification is requested, refuse.
1283 */
1284 int
1285 linux_sys_setfsuid(l, v, retval)
1286 struct lwp *l;
1287 void *v;
1288 register_t *retval;
1289 {
1290 struct linux_sys_setfsuid_args /* {
1291 syscallarg(uid_t) uid;
1292 } */ *uap = v;
1293 uid_t uid;
1294
1295 uid = SCARG(uap, uid);
1296 if (kauth_cred_getuid(l->l_cred) != uid)
1297 return sys_nosys(l, v, retval);
1298 else
1299 return (0);
1300 }
1301
1302 /* XXX XXX XXX */
1303 # ifndef alpha
1304 int
1305 linux_sys_getfsuid(l, v, retval)
1306 struct lwp *l;
1307 void *v;
1308 register_t *retval;
1309 {
1310 return sys_getuid(l, v, retval);
1311 }
1312 # endif
1313
1314 int
1315 linux_sys_setresuid(struct lwp *l, void *v, register_t *retval)
1316 {
1317 struct linux_sys_setresuid_args /* {
1318 syscallarg(uid_t) ruid;
1319 syscallarg(uid_t) euid;
1320 syscallarg(uid_t) suid;
1321 } */ *uap = v;
1322
1323 /*
1324 * Note: These checks are a little different than the NetBSD
1325 * setreuid(2) call performs. This precisely follows the
1326 * behavior of the Linux kernel.
1327 */
1328
1329 return do_setresuid(l, SCARG(uap, ruid), SCARG(uap, euid),
1330 SCARG(uap, suid),
1331 ID_R_EQ_R | ID_R_EQ_E | ID_R_EQ_S |
1332 ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S |
1333 ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S );
1334 }
1335
1336 int
1337 linux_sys_getresuid(struct lwp *l, void *v, register_t *retval)
1338 {
1339 struct linux_sys_getresuid_args /* {
1340 syscallarg(uid_t *) ruid;
1341 syscallarg(uid_t *) euid;
1342 syscallarg(uid_t *) suid;
1343 } */ *uap = v;
1344 kauth_cred_t pc = l->l_cred;
1345 int error;
1346 uid_t uid;
1347
1348 /*
1349 * Linux copies these values out to userspace like so:
1350 *
1351 * 1. Copy out ruid.
1352 * 2. If that succeeds, copy out euid.
1353 * 3. If both of those succeed, copy out suid.
1354 */
1355 uid = kauth_cred_getuid(pc);
1356 if ((error = copyout(&uid, SCARG(uap, ruid), sizeof(uid_t))) != 0)
1357 return (error);
1358
1359 uid = kauth_cred_geteuid(pc);
1360 if ((error = copyout(&uid, SCARG(uap, euid), sizeof(uid_t))) != 0)
1361 return (error);
1362
1363 uid = kauth_cred_getsvuid(pc);
1364
1365 return (copyout(&uid, SCARG(uap, suid), sizeof(uid_t)));
1366 }
1367
1368 int
1369 linux_sys_ptrace(l, v, retval)
1370 struct lwp *l;
1371 void *v;
1372 register_t *retval;
1373 {
1374 #if defined(PTRACE) || defined(_LKM)
1375 struct linux_sys_ptrace_args /* {
1376 i386, m68k, powerpc: T=int
1377 alpha, amd64: T=long
1378 syscallarg(T) request;
1379 syscallarg(T) pid;
1380 syscallarg(T) addr;
1381 syscallarg(T) data;
1382 } */ *uap = v;
1383 const int *ptr;
1384 int request;
1385 int error;
1386 #ifdef _LKM
1387 #define sys_ptrace (*sysent[SYS_ptrace].sy_call)
1388 #endif
1389
1390 ptr = linux_ptrace_request_map;
1391 request = SCARG(uap, request);
1392 while (*ptr != -1)
1393 if (*ptr++ == request) {
1394 struct sys_ptrace_args pta;
1395
1396 SCARG(&pta, req) = *ptr;
1397 SCARG(&pta, pid) = SCARG(uap, pid);
1398 SCARG(&pta, addr) = (caddr_t)SCARG(uap, addr);
1399 SCARG(&pta, data) = SCARG(uap, data);
1400
1401 /*
1402 * Linux ptrace(PTRACE_CONT, pid, 0, 0) means actually
1403 * to continue where the process left off previously.
1404 * The same thing is achieved by addr == (caddr_t) 1
1405 * on NetBSD, so rewrite 'addr' appropriately.
1406 */
1407 if (request == LINUX_PTRACE_CONT && SCARG(uap, addr)==0)
1408 SCARG(&pta, addr) = (caddr_t) 1;
1409
1410 error = sys_ptrace(l, &pta, retval);
1411 if (error)
1412 return error;
1413 switch (request) {
1414 case LINUX_PTRACE_PEEKTEXT:
1415 case LINUX_PTRACE_PEEKDATA:
1416 error = copyout (retval,
1417 (caddr_t)SCARG(uap, data),
1418 sizeof *retval);
1419 *retval = SCARG(uap, data);
1420 break;
1421 default:
1422 break;
1423 }
1424 return error;
1425 }
1426 else
1427 ptr++;
1428
1429 return LINUX_SYS_PTRACE_ARCH(l, uap, retval);
1430 #else
1431 return ENOSYS;
1432 #endif /* PTRACE || _LKM */
1433 }
1434
1435 int
1436 linux_sys_reboot(struct lwp *l, void *v, register_t *retval)
1437 {
1438 struct linux_sys_reboot_args /* {
1439 syscallarg(int) magic1;
1440 syscallarg(int) magic2;
1441 syscallarg(int) cmd;
1442 syscallarg(void *) arg;
1443 } */ *uap = v;
1444 struct sys_reboot_args /* {
1445 syscallarg(int) opt;
1446 syscallarg(char *) bootstr;
1447 } */ sra;
1448 int error;
1449
1450 if ((error = kauth_authorize_system(l->l_cred,
1451 KAUTH_SYSTEM_REBOOT, 0, NULL, NULL, NULL)) != 0)
1452 return(error);
1453
1454 if (SCARG(uap, magic1) != LINUX_REBOOT_MAGIC1)
1455 return(EINVAL);
1456 if (SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2 &&
1457 SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2A &&
1458 SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2B)
1459 return(EINVAL);
1460
1461 switch (SCARG(uap, cmd)) {
1462 case LINUX_REBOOT_CMD_RESTART:
1463 SCARG(&sra, opt) = RB_AUTOBOOT;
1464 break;
1465 case LINUX_REBOOT_CMD_HALT:
1466 SCARG(&sra, opt) = RB_HALT;
1467 break;
1468 case LINUX_REBOOT_CMD_POWER_OFF:
1469 SCARG(&sra, opt) = RB_HALT|RB_POWERDOWN;
1470 break;
1471 case LINUX_REBOOT_CMD_RESTART2:
1472 /* Reboot with an argument. */
1473 SCARG(&sra, opt) = RB_AUTOBOOT|RB_STRING;
1474 SCARG(&sra, bootstr) = SCARG(uap, arg);
1475 break;
1476 case LINUX_REBOOT_CMD_CAD_ON:
1477 return(EINVAL); /* We don't implement ctrl-alt-delete */
1478 case LINUX_REBOOT_CMD_CAD_OFF:
1479 return(0);
1480 default:
1481 return(EINVAL);
1482 }
1483
1484 return(sys_reboot(l, &sra, retval));
1485 }
1486
1487 /*
1488 * Copy of compat_12_sys_swapon().
1489 */
1490 int
1491 linux_sys_swapon(l, v, retval)
1492 struct lwp *l;
1493 void *v;
1494 register_t *retval;
1495 {
1496 struct sys_swapctl_args ua;
1497 struct linux_sys_swapon_args /* {
1498 syscallarg(const char *) name;
1499 } */ *uap = v;
1500
1501 SCARG(&ua, cmd) = SWAP_ON;
1502 SCARG(&ua, arg) = (void *)__UNCONST(SCARG(uap, name));
1503 SCARG(&ua, misc) = 0; /* priority */
1504 return (sys_swapctl(l, &ua, retval));
1505 }
1506
1507 /*
1508 * Stop swapping to the file or block device specified by path.
1509 */
1510 int
1511 linux_sys_swapoff(l, v, retval)
1512 struct lwp *l;
1513 void *v;
1514 register_t *retval;
1515 {
1516 struct sys_swapctl_args ua;
1517 struct linux_sys_swapoff_args /* {
1518 syscallarg(const char *) path;
1519 } */ *uap = v;
1520
1521 SCARG(&ua, cmd) = SWAP_OFF;
1522 SCARG(&ua, arg) = __UNCONST(SCARG(uap, path)); /*XXXUNCONST*/
1523 return (sys_swapctl(l, &ua, retval));
1524 }
1525
1526 /*
1527 * Copy of compat_09_sys_setdomainname()
1528 */
1529 /* ARGSUSED */
1530 int
1531 linux_sys_setdomainname(struct lwp *l, void *v, register_t *retval)
1532 {
1533 struct linux_sys_setdomainname_args /* {
1534 syscallarg(char *) domainname;
1535 syscallarg(int) len;
1536 } */ *uap = v;
1537 int name[2];
1538
1539 name[0] = CTL_KERN;
1540 name[1] = KERN_DOMAINNAME;
1541 return (old_sysctl(&name[0], 2, 0, 0, SCARG(uap, domainname),
1542 SCARG(uap, len), l));
1543 }
1544
1545 /*
1546 * sysinfo()
1547 */
1548 /* ARGSUSED */
1549 int
1550 linux_sys_sysinfo(struct lwp *l, void *v, register_t *retval)
1551 {
1552 struct linux_sys_sysinfo_args /* {
1553 syscallarg(struct linux_sysinfo *) arg;
1554 } */ *uap = v;
1555 struct linux_sysinfo si;
1556 struct loadavg *la;
1557
1558 si.uptime = time_uptime;
1559 la = &averunnable;
1560 si.loads[0] = la->ldavg[0] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
1561 si.loads[1] = la->ldavg[1] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
1562 si.loads[2] = la->ldavg[2] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
1563 si.totalram = ctob((u_long)physmem);
1564 si.freeram = (u_long)uvmexp.free * uvmexp.pagesize;
1565 si.sharedram = 0; /* XXX */
1566 si.bufferram = (u_long)uvmexp.filepages * uvmexp.pagesize;
1567 si.totalswap = (u_long)uvmexp.swpages * uvmexp.pagesize;
1568 si.freeswap =
1569 (u_long)(uvmexp.swpages - uvmexp.swpginuse) * uvmexp.pagesize;
1570 si.procs = nprocs;
1571
1572 /* The following are only present in newer Linux kernels. */
1573 si.totalbig = 0;
1574 si.freebig = 0;
1575 si.mem_unit = 1;
1576
1577 return (copyout(&si, SCARG(uap, arg), sizeof si));
1578 }
1579
1580 int
1581 linux_sys_getrlimit(l, v, retval)
1582 struct lwp *l;
1583 void *v;
1584 register_t *retval;
1585 {
1586 struct linux_sys_getrlimit_args /* {
1587 syscallarg(int) which;
1588 # ifdef LINUX_LARGEFILE64
1589 syscallarg(struct rlimit *) rlp;
1590 # else
1591 syscallarg(struct orlimit *) rlp;
1592 # endif
1593 } */ *uap = v;
1594 struct proc *p = l->l_proc;
1595 caddr_t sg = stackgap_init(p, 0);
1596 struct sys_getrlimit_args ap;
1597 struct rlimit rl;
1598 # ifdef LINUX_LARGEFILE64
1599 struct rlimit orl;
1600 # else
1601 struct orlimit orl;
1602 # endif
1603 int error;
1604
1605 SCARG(&ap, which) = linux_to_bsd_limit(SCARG(uap, which));
1606 if ((error = SCARG(&ap, which)) < 0)
1607 return -error;
1608 SCARG(&ap, rlp) = stackgap_alloc(p, &sg, sizeof rl);
1609 if ((error = sys_getrlimit(l, &ap, retval)) != 0)
1610 return error;
1611 if ((error = copyin(SCARG(&ap, rlp), &rl, sizeof(rl))) != 0)
1612 return error;
1613 bsd_to_linux_rlimit(&orl, &rl);
1614
1615 return copyout(&orl, SCARG(uap, rlp), sizeof(orl));
1616 }
1617
1618 int
1619 linux_sys_setrlimit(l, v, retval)
1620 struct lwp *l;
1621 void *v;
1622 register_t *retval;
1623 {
1624 struct linux_sys_setrlimit_args /* {
1625 syscallarg(int) which;
1626 # ifdef LINUX_LARGEFILE64
1627 syscallarg(struct rlimit *) rlp;
1628 # else
1629 syscallarg(struct orlimit *) rlp;
1630 # endif
1631 } */ *uap = v;
1632 struct proc *p = l->l_proc;
1633 caddr_t sg = stackgap_init(p, 0);
1634 struct sys_getrlimit_args ap;
1635 struct rlimit rl;
1636 # ifdef LINUX_LARGEFILE64
1637 struct rlimit orl;
1638 # else
1639 struct orlimit orl;
1640 # endif
1641 int error;
1642
1643 SCARG(&ap, which) = linux_to_bsd_limit(SCARG(uap, which));
1644 SCARG(&ap, rlp) = stackgap_alloc(p, &sg, sizeof rl);
1645 if ((error = SCARG(&ap, which)) < 0)
1646 return -error;
1647 if ((error = copyin(SCARG(uap, rlp), &orl, sizeof(orl))) != 0)
1648 return error;
1649 linux_to_bsd_rlimit(&rl, &orl);
1650 if ((error = copyout(&rl, SCARG(&ap, rlp), sizeof(rl))) != 0)
1651 return error;
1652 return sys_setrlimit(l, &ap, retval);
1653 }
1654
1655 # if !defined(__mips__) && !defined(__amd64__)
1656 /* XXX: this doesn't look 100% common, at least mips doesn't have it */
1657 int
1658 linux_sys_ugetrlimit(l, v, retval)
1659 struct lwp *l;
1660 void *v;
1661 register_t *retval;
1662 {
1663 return linux_sys_getrlimit(l, v, retval);
1664 }
1665 # endif
1666
1667 /*
1668 * This gets called for unsupported syscalls. The difference to sys_nosys()
1669 * is that process does not get SIGSYS, the call just returns with ENOSYS.
1670 * This is the way Linux does it and glibc depends on this behaviour.
1671 */
1672 int
1673 linux_sys_nosys(struct lwp *l, void *v,
1674 register_t *retval)
1675 {
1676 return (ENOSYS);
1677 }
1678
1679 int
1680 linux_sys_getpriority(l, v, retval)
1681 struct lwp *l;
1682 void *v;
1683 register_t *retval;
1684 {
1685 struct linux_sys_getpriority_args /* {
1686 syscallarg(int) which;
1687 syscallarg(int) who;
1688 } */ *uap = v;
1689 struct sys_getpriority_args bsa;
1690 int error;
1691
1692 SCARG(&bsa, which) = SCARG(uap, which);
1693 SCARG(&bsa, who) = SCARG(uap, who);
1694
1695 if ((error = sys_getpriority(l, &bsa, retval)))
1696 return error;
1697
1698 *retval = NZERO - *retval;
1699
1700 return 0;
1701 }
1702
1703 #endif /* !COMPAT_LINUX32 */
1704