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