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