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