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