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