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