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