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