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