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