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