2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2002 Doug Rabson
5 * Copyright (c) 1994-1995 Søren Schmidt
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer
13 * in this position and unchanged.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_compat.h"
37 #include <sys/param.h>
38 #include <sys/blist.h>
39 #include <sys/fcntl.h>
41 #include <sys/imgact_aout.h>
44 #include <sys/kernel.h>
45 #include <sys/limits.h>
47 #include <sys/malloc.h>
49 #include <sys/mount.h>
50 #include <sys/msgbuf.h>
51 #include <sys/mutex.h>
52 #include <sys/namei.h>
55 #include <sys/procctl.h>
56 #include <sys/reboot.h>
57 #include <sys/racct.h>
58 #include <sys/random.h>
59 #include <sys/resourcevar.h>
60 #include <sys/sched.h>
62 #include <sys/signalvar.h>
64 #include <sys/syscallsubr.h>
65 #include <sys/sysctl.h>
66 #include <sys/sysproto.h>
67 #include <sys/systm.h>
69 #include <sys/vmmeter.h>
70 #include <sys/vnode.h>
72 #include <sys/cpuset.h>
75 #include <security/mac/mac_framework.h>
79 #include <vm/vm_kern.h>
80 #include <vm/vm_map.h>
81 #include <vm/vm_extern.h>
82 #include <vm/swap_pager.h>
85 #include <machine/../linux32/linux.h>
86 #include <machine/../linux32/linux32_proto.h>
88 #include <machine/../linux/linux.h>
89 #include <machine/../linux/linux_proto.h>
92 #include <compat/linux/linux_dtrace.h>
93 #include <compat/linux/linux_file.h>
94 #include <compat/linux/linux_mib.h>
95 #include <compat/linux/linux_signal.h>
96 #include <compat/linux/linux_timer.h>
97 #include <compat/linux/linux_util.h>
98 #include <compat/linux/linux_sysproto.h>
99 #include <compat/linux/linux_emul.h>
100 #include <compat/linux/linux_misc.h>
103 * Special DTrace provider for the linuxulator.
105 * In this file we define the provider for the entire linuxulator. All
106 * modules (= files of the linuxulator) use it.
108 * We define a different name depending on the emulated bitsize, see
109 * ../../<ARCH>/linux{,32}/linux.h, e.g.:
110 * native bitsize = linuxulator
111 * amd64, 32bit emulation = linuxulator32
113 LIN_SDT_PROVIDER_DEFINE(LINUX_DTRACE);
115 int stclohz; /* Statistics clock frequency */
117 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
118 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
119 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
120 RLIMIT_MEMLOCK, RLIMIT_AS
124 l_long uptime; /* Seconds since boot */
125 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */
126 #define LINUX_SYSINFO_LOADS_SCALE 65536
127 l_ulong totalram; /* Total usable main memory size */
128 l_ulong freeram; /* Available memory size */
129 l_ulong sharedram; /* Amount of shared memory */
130 l_ulong bufferram; /* Memory used by buffers */
131 l_ulong totalswap; /* Total swap space size */
132 l_ulong freeswap; /* swap space still available */
133 l_ushort procs; /* Number of current processes */
138 char _f[20-2*sizeof(l_long)-sizeof(l_int)]; /* padding */
141 struct l_pselect6arg {
146 static int linux_utimensat_nsec_valid(l_long);
149 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args)
151 struct l_sysinfo sysinfo;
155 bzero(&sysinfo, sizeof(sysinfo));
159 sysinfo.uptime = ts.tv_sec;
161 /* Use the information from the mib to get our load averages */
162 for (i = 0; i < 3; i++)
163 sysinfo.loads[i] = averunnable.ldavg[i] *
164 LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale;
166 sysinfo.totalram = physmem * PAGE_SIZE;
167 sysinfo.freeram = (u_long)vm_free_count() * PAGE_SIZE;
170 * sharedram counts pages allocated to named, swap-backed objects such
171 * as shared memory segments and tmpfs files. There is no cheap way to
172 * compute this, so just leave the field unpopulated. Linux itself only
173 * started setting this field in the 3.x timeframe.
175 sysinfo.sharedram = 0;
176 sysinfo.bufferram = 0;
178 swap_pager_status(&i, &j);
179 sysinfo.totalswap = i * PAGE_SIZE;
180 sysinfo.freeswap = (i - j) * PAGE_SIZE;
182 sysinfo.procs = nprocs;
185 * Platforms supported by the emulation layer do not have a notion of
188 sysinfo.totalhigh = 0;
189 sysinfo.freehigh = 0;
191 sysinfo.mem_unit = 1;
193 return (copyout(&sysinfo, args->info, sizeof(sysinfo)));
196 #ifdef LINUX_LEGACY_SYSCALLS
198 linux_alarm(struct thread *td, struct linux_alarm_args *args)
200 struct itimerval it, old_it;
206 * Linux alarm() is always successful. Limit secs to INT32_MAX / 2
207 * to match kern_setitimer()'s limit to avoid error from it.
209 * XXX. Linux limit secs to INT_MAX on 32 and does not limit on 64-bit
212 if (secs > INT32_MAX / 2)
213 secs = INT32_MAX / 2;
215 it.it_value.tv_sec = secs;
216 it.it_value.tv_usec = 0;
217 timevalclear(&it.it_interval);
218 error = kern_setitimer(td, ITIMER_REAL, &it, &old_it);
219 KASSERT(error == 0, ("kern_setitimer returns %d", error));
221 if ((old_it.it_value.tv_sec == 0 && old_it.it_value.tv_usec > 0) ||
222 old_it.it_value.tv_usec >= 500000)
223 old_it.it_value.tv_sec++;
224 td->td_retval[0] = old_it.it_value.tv_sec;
230 linux_brk(struct thread *td, struct linux_brk_args *args)
232 struct vmspace *vm = td->td_proc->p_vmspace;
235 old = (uintptr_t)vm->vm_daddr + ctob(vm->vm_dsize);
236 new = (uintptr_t)args->dsend;
237 if ((caddr_t)new > vm->vm_daddr && !kern_break(td, &new))
238 td->td_retval[0] = (register_t)new;
240 td->td_retval[0] = (register_t)old;
245 #if defined(__i386__)
246 /* XXX: what about amd64/linux32? */
249 linux_uselib(struct thread *td, struct linux_uselib_args *args)
255 vm_map_entry_t entry;
258 unsigned long file_offset;
259 unsigned long bss_size;
263 bool locked, opened, textset;
271 if (!LUSECONVPATH(td)) {
272 NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | AUDITVNODE1,
273 UIO_USERSPACE, args->library, td);
276 LCONVPATHEXIST(td, args->library, &library);
277 NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | AUDITVNODE1,
278 UIO_SYSSPACE, library, td);
286 NDFREE(&ni, NDF_ONLY_PNBUF);
289 * From here on down, we have a locked vnode that must be unlocked.
290 * XXX: The code below largely duplicates exec_check_permissions().
295 error = VOP_GETATTR(vp, &attr, td->td_ucred);
299 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
300 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
301 /* EACCESS is what exec(2) returns. */
307 if (attr.va_size == 0) {
312 /* Can we access it? */
313 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
318 * XXX: This should use vn_open() so that it is properly authorized,
319 * and to reduce code redundancy all over the place here.
320 * XXX: Not really, it duplicates far more of exec_check_permissions()
324 error = mac_vnode_check_open(td->td_ucred, vp, VREAD);
328 error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL);
333 /* Pull in executable header into exec_map */
334 error = vm_mmap(exec_map, (vm_offset_t *)&a_out, PAGE_SIZE,
335 VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 0);
339 /* Is it a Linux binary ? */
340 if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
346 * While we are here, we should REALLY do some more checks
349 /* Set file/virtual offset based on a.out variant. */
350 switch ((int)(a_out->a_magic & 0xffff)) {
351 case 0413: /* ZMAGIC */
354 case 0314: /* QMAGIC */
362 bss_size = round_page(a_out->a_bss);
364 /* Check various fields in header for validity/bounds. */
365 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
370 /* text + data can't exceed file size */
371 if (a_out->a_data + a_out->a_text > attr.va_size) {
377 * text/data/bss must not exceed limits
378 * XXX - this is not complete. it should check current usage PLUS
379 * the resources needed by this library.
381 PROC_LOCK(td->td_proc);
382 if (a_out->a_text > maxtsiz ||
383 a_out->a_data + bss_size > lim_cur_proc(td->td_proc, RLIMIT_DATA) ||
384 racct_set(td->td_proc, RACCT_DATA, a_out->a_data +
386 PROC_UNLOCK(td->td_proc);
390 PROC_UNLOCK(td->td_proc);
393 * Prevent more writers.
395 error = VOP_SET_TEXT(vp);
401 * Lock no longer needed
407 * Check if file_offset page aligned. Currently we cannot handle
408 * misalinged file offsets, and so we read in the entire image
411 if (file_offset & PAGE_MASK) {
412 /* Map text+data read/write/execute */
414 /* a_entry is the load address and is page aligned */
415 vmaddr = trunc_page(a_out->a_entry);
417 /* get anon user mapping, read+write+execute */
418 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
419 &vmaddr, a_out->a_text + a_out->a_data, 0, VMFS_NO_SPACE,
420 VM_PROT_ALL, VM_PROT_ALL, 0);
424 error = vn_rdwr(UIO_READ, vp, (void *)vmaddr, file_offset,
425 a_out->a_text + a_out->a_data, UIO_USERSPACE, 0,
426 td->td_ucred, NOCRED, &aresid, td);
435 * for QMAGIC, a_entry is 20 bytes beyond the load address
436 * to skip the executable header
438 vmaddr = trunc_page(a_out->a_entry);
441 * Map it all into the process's space as a single
442 * copy-on-write "data" segment.
444 map = &td->td_proc->p_vmspace->vm_map;
445 error = vm_mmap(map, &vmaddr,
446 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
447 MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset);
451 if (!vm_map_lookup_entry(map, vmaddr, &entry)) {
456 entry->eflags |= MAP_ENTRY_VN_EXEC;
462 /* Calculate BSS start address */
463 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
466 /* allocate some 'anon' space */
467 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
468 &vmaddr, bss_size, 0, VMFS_NO_SPACE, VM_PROT_ALL,
479 VOP_CLOSE(vp, FREAD, td->td_ucred, td);
484 VOP_LOCK(vp, LK_SHARED | LK_RETRY);
486 VOP_UNSET_TEXT_CHECKED(vp);
491 /* Release the temporary mapping. */
493 kmap_free_wakeup(exec_map, (vm_offset_t)a_out, PAGE_SIZE);
498 #endif /* __i386__ */
500 #ifdef LINUX_LEGACY_SYSCALLS
502 linux_select(struct thread *td, struct linux_select_args *args)
505 struct timeval tv0, tv1, utv, *tvp;
509 * Store current time for computation of the amount of
513 if ((error = copyin(args->timeout, <v, sizeof(ltv))))
515 utv.tv_sec = ltv.tv_sec;
516 utv.tv_usec = ltv.tv_usec;
518 if (itimerfix(&utv)) {
520 * The timeval was invalid. Convert it to something
521 * valid that will act as it does under Linux.
523 utv.tv_sec += utv.tv_usec / 1000000;
524 utv.tv_usec %= 1000000;
525 if (utv.tv_usec < 0) {
527 utv.tv_usec += 1000000;
537 error = kern_select(td, args->nfds, args->readfds, args->writefds,
538 args->exceptfds, tvp, LINUX_NFDBITS);
543 if (td->td_retval[0]) {
545 * Compute how much time was left of the timeout,
546 * by subtracting the current time and the time
547 * before we started the call, and subtracting
548 * that result from the user-supplied value.
551 timevalsub(&tv1, &tv0);
552 timevalsub(&utv, &tv1);
557 ltv.tv_sec = utv.tv_sec;
558 ltv.tv_usec = utv.tv_usec;
559 if ((error = copyout(<v, args->timeout, sizeof(ltv))))
569 linux_mremap(struct thread *td, struct linux_mremap_args *args)
575 if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) {
576 td->td_retval[0] = 0;
581 * Check for the page alignment.
582 * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK.
584 if (args->addr & PAGE_MASK) {
585 td->td_retval[0] = 0;
589 args->new_len = round_page(args->new_len);
590 args->old_len = round_page(args->old_len);
592 if (args->new_len > args->old_len) {
593 td->td_retval[0] = 0;
597 if (args->new_len < args->old_len) {
598 addr = args->addr + args->new_len;
599 len = args->old_len - args->new_len;
600 error = kern_munmap(td, addr, len);
603 td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
607 #define LINUX_MS_ASYNC 0x0001
608 #define LINUX_MS_INVALIDATE 0x0002
609 #define LINUX_MS_SYNC 0x0004
612 linux_msync(struct thread *td, struct linux_msync_args *args)
615 return (kern_msync(td, args->addr, args->len,
616 args->fl & ~LINUX_MS_SYNC));
619 #ifdef LINUX_LEGACY_SYSCALLS
621 linux_time(struct thread *td, struct linux_time_args *args)
629 if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
631 td->td_retval[0] = tm;
636 struct l_times_argv {
639 l_clock_t tms_cutime;
640 l_clock_t tms_cstime;
644 * Glibc versions prior to 2.2.1 always use hard-coded CLK_TCK value.
645 * Since 2.2.1 Glibc uses value exported from kernel via AT_CLKTCK
646 * auxiliary vector entry.
650 #define CONVOTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
651 #define CONVNTCK(r) (r.tv_sec * stclohz + r.tv_usec / (1000000 / stclohz))
653 #define CONVTCK(r) (linux_kernver(td) >= LINUX_KERNVER_2004000 ? \
654 CONVNTCK(r) : CONVOTCK(r))
657 linux_times(struct thread *td, struct linux_times_args *args)
659 struct timeval tv, utime, stime, cutime, cstime;
660 struct l_times_argv tms;
664 if (args->buf != NULL) {
668 calcru(p, &utime, &stime);
670 calccru(p, &cutime, &cstime);
673 tms.tms_utime = CONVTCK(utime);
674 tms.tms_stime = CONVTCK(stime);
676 tms.tms_cutime = CONVTCK(cutime);
677 tms.tms_cstime = CONVTCK(cstime);
679 if ((error = copyout(&tms, args->buf, sizeof(tms))))
684 td->td_retval[0] = (int)CONVTCK(tv);
689 linux_newuname(struct thread *td, struct linux_newuname_args *args)
691 struct l_new_utsname utsname;
692 char osname[LINUX_MAX_UTSNAME];
693 char osrelease[LINUX_MAX_UTSNAME];
696 linux_get_osname(td, osname);
697 linux_get_osrelease(td, osrelease);
699 bzero(&utsname, sizeof(utsname));
700 strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
701 getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
702 getcreddomainname(td->td_ucred, utsname.domainname, LINUX_MAX_UTSNAME);
703 strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
704 strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
705 for (p = utsname.version; *p != '\0'; ++p)
710 #if defined(__amd64__)
712 * On amd64, Linux uname(2) needs to return "x86_64"
713 * for both 64-bit and 32-bit applications. On 32-bit,
714 * the string returned by getauxval(AT_PLATFORM) needs
715 * to remain "i686", though.
717 strlcpy(utsname.machine, "x86_64", LINUX_MAX_UTSNAME);
719 strlcpy(utsname.machine, linux_kplatform, LINUX_MAX_UTSNAME);
722 return (copyout(&utsname, args->buf, sizeof(utsname)));
730 #ifdef LINUX_LEGACY_SYSCALLS
732 linux_utime(struct thread *td, struct linux_utime_args *args)
734 struct timeval tv[2], *tvp;
735 struct l_utimbuf lut;
740 convpath = LUSECONVPATH(td);
742 LCONVPATHEXIST(td, args->fname, &fname);
745 if ((error = copyin(args->times, &lut, sizeof lut))) {
750 tv[0].tv_sec = lut.l_actime;
752 tv[1].tv_sec = lut.l_modtime;
759 error = kern_utimesat(td, AT_FDCWD, args->fname, UIO_USERSPACE,
762 error = kern_utimesat(td, AT_FDCWD, fname, UIO_SYSSPACE, tvp,
770 #ifdef LINUX_LEGACY_SYSCALLS
772 linux_utimes(struct thread *td, struct linux_utimes_args *args)
775 struct timeval tv[2], *tvp = NULL;
780 convpath = LUSECONVPATH(td);
782 LCONVPATHEXIST(td, args->fname, &fname);
784 if (args->tptr != NULL) {
785 if ((error = copyin(args->tptr, ltv, sizeof ltv))) {
789 tv[0].tv_sec = ltv[0].tv_sec;
790 tv[0].tv_usec = ltv[0].tv_usec;
791 tv[1].tv_sec = ltv[1].tv_sec;
792 tv[1].tv_usec = ltv[1].tv_usec;
797 error = kern_utimesat(td, AT_FDCWD, args->fname, UIO_USERSPACE,
800 error = kern_utimesat(td, AT_FDCWD, fname, UIO_SYSSPACE,
809 linux_utimensat_nsec_valid(l_long nsec)
812 if (nsec == LINUX_UTIME_OMIT || nsec == LINUX_UTIME_NOW)
814 if (nsec >= 0 && nsec <= 999999999)
820 linux_utimensat(struct thread *td, struct linux_utimensat_args *args)
822 struct l_timespec l_times[2];
823 struct timespec times[2], *timesp = NULL;
825 int error, dfd, flags = 0;
827 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
829 if (args->flags & ~LINUX_AT_SYMLINK_NOFOLLOW)
832 if (args->times != NULL) {
833 error = copyin(args->times, l_times, sizeof(l_times));
837 if (linux_utimensat_nsec_valid(l_times[0].tv_nsec) != 0 ||
838 linux_utimensat_nsec_valid(l_times[1].tv_nsec) != 0)
841 times[0].tv_sec = l_times[0].tv_sec;
842 switch (l_times[0].tv_nsec)
844 case LINUX_UTIME_OMIT:
845 times[0].tv_nsec = UTIME_OMIT;
847 case LINUX_UTIME_NOW:
848 times[0].tv_nsec = UTIME_NOW;
851 times[0].tv_nsec = l_times[0].tv_nsec;
854 times[1].tv_sec = l_times[1].tv_sec;
855 switch (l_times[1].tv_nsec)
857 case LINUX_UTIME_OMIT:
858 times[1].tv_nsec = UTIME_OMIT;
860 case LINUX_UTIME_NOW:
861 times[1].tv_nsec = UTIME_NOW;
864 times[1].tv_nsec = l_times[1].tv_nsec;
869 /* This breaks POSIX, but is what the Linux kernel does
870 * _on purpose_ (documented in the man page for utimensat(2)),
871 * so we must follow that behaviour. */
872 if (times[0].tv_nsec == UTIME_OMIT &&
873 times[1].tv_nsec == UTIME_OMIT)
877 if (!LUSECONVPATH(td)) {
878 if (args->pathname != NULL) {
879 return (kern_utimensat(td, dfd, args->pathname,
880 UIO_USERSPACE, timesp, UIO_SYSSPACE, flags));
884 if (args->pathname != NULL)
885 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
886 else if (args->flags != 0)
889 if (args->flags & LINUX_AT_SYMLINK_NOFOLLOW)
890 flags |= AT_SYMLINK_NOFOLLOW;
893 error = kern_futimens(td, dfd, timesp, UIO_SYSSPACE);
895 error = kern_utimensat(td, dfd, path, UIO_SYSSPACE, timesp,
896 UIO_SYSSPACE, flags);
903 #ifdef LINUX_LEGACY_SYSCALLS
905 linux_futimesat(struct thread *td, struct linux_futimesat_args *args)
908 struct timeval tv[2], *tvp = NULL;
913 convpath = LUSECONVPATH(td);
914 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
916 LCONVPATHEXIST_AT(td, args->filename, &fname, dfd);
918 if (args->utimes != NULL) {
919 if ((error = copyin(args->utimes, ltv, sizeof ltv))) {
924 tv[0].tv_sec = ltv[0].tv_sec;
925 tv[0].tv_usec = ltv[0].tv_usec;
926 tv[1].tv_sec = ltv[1].tv_sec;
927 tv[1].tv_usec = ltv[1].tv_usec;
932 error = kern_utimesat(td, dfd, args->filename, UIO_USERSPACE,
935 error = kern_utimesat(td, dfd, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
943 linux_common_wait(struct thread *td, int pid, int *statusp,
944 int options, struct __wrusage *wrup)
949 int error, status, tmpstat;
951 if (pid == WAIT_ANY) {
954 } else if (pid < 0) {
963 * For backward compatibility we implicitly add flags WEXITED
966 options |= WEXITED | WTRAPPED;
967 error = kern_wait6(td, idtype, id, &status, options, wrup, &siginfo);
972 tmpstat = status & 0xffff;
973 if (WIFSIGNALED(tmpstat)) {
974 tmpstat = (tmpstat & 0xffffff80) |
975 bsd_to_linux_signal(WTERMSIG(tmpstat));
976 } else if (WIFSTOPPED(tmpstat)) {
977 tmpstat = (tmpstat & 0xffff00ff) |
978 (bsd_to_linux_signal(WSTOPSIG(tmpstat)) << 8);
979 #if defined(__amd64__) && !defined(COMPAT_LINUX32)
980 if (WSTOPSIG(status) == SIGTRAP) {
981 tmpstat = linux_ptrace_status(td,
982 siginfo.si_pid, tmpstat);
985 } else if (WIFCONTINUED(tmpstat)) {
988 error = copyout(&tmpstat, statusp, sizeof(int));
994 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
996 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
998 struct linux_wait4_args wait4_args;
1000 wait4_args.pid = args->pid;
1001 wait4_args.status = args->status;
1002 wait4_args.options = args->options;
1003 wait4_args.rusage = NULL;
1005 return (linux_wait4(td, &wait4_args));
1007 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1010 linux_wait4(struct thread *td, struct linux_wait4_args *args)
1013 struct __wrusage wru, *wrup;
1015 if (args->options & ~(LINUX_WUNTRACED | LINUX_WNOHANG |
1016 LINUX_WCONTINUED | __WCLONE | __WNOTHREAD | __WALL))
1020 linux_to_bsd_waitopts(args->options, &options);
1022 if (args->rusage != NULL)
1026 error = linux_common_wait(td, args->pid, args->status, options, wrup);
1029 if (args->rusage != NULL)
1030 error = linux_copyout_rusage(&wru.wru_self, args->rusage);
1035 linux_waitid(struct thread *td, struct linux_waitid_args *args)
1037 int status, options, sig;
1038 struct __wrusage wru;
1046 linux_to_bsd_waitopts(args->options, &options);
1048 if (options & ~(WNOHANG | WNOWAIT | WEXITED | WUNTRACED | WCONTINUED))
1050 if (!(options & (WEXITED | WUNTRACED | WCONTINUED)))
1053 switch (args->idtype) {
1071 error = kern_wait6(td, idtype, args->id, &status, options,
1075 if (args->rusage != NULL) {
1076 error = linux_copyout_rusage(&wru.wru_children,
1081 if (args->info != NULL) {
1083 bzero(&lsi, sizeof(lsi));
1084 if (td->td_retval[0] != 0) {
1085 sig = bsd_to_linux_signal(siginfo.si_signo);
1086 siginfo_to_lsiginfo(&siginfo, &lsi, sig);
1088 error = copyout(&lsi, args->info, sizeof(lsi));
1090 td->td_retval[0] = 0;
1095 #ifdef LINUX_LEGACY_SYSCALLS
1097 linux_mknod(struct thread *td, struct linux_mknod_args *args)
1104 convpath = LUSECONVPATH(td);
1107 seg = UIO_USERSPACE;
1109 LCONVPATHCREAT(td, args->path, &path);
1113 switch (args->mode & S_IFMT) {
1116 error = kern_mkfifoat(td, AT_FDCWD, path, seg,
1122 error = kern_mknodat(td, AT_FDCWD, path, seg,
1123 args->mode, args->dev);
1131 args->mode |= S_IFREG;
1134 error = kern_openat(td, AT_FDCWD, path, seg,
1135 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1137 kern_close(td, td->td_retval[0]);
1151 linux_mknodat(struct thread *td, struct linux_mknodat_args *args)
1158 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1160 convpath = LUSECONVPATH(td);
1162 path = __DECONST(char *, args->filename);
1163 seg = UIO_USERSPACE;
1165 LCONVPATHCREAT_AT(td, args->filename, &path, dfd);
1169 switch (args->mode & S_IFMT) {
1172 error = kern_mkfifoat(td, dfd, path, seg, args->mode);
1177 error = kern_mknodat(td, dfd, path, seg, args->mode,
1186 args->mode |= S_IFREG;
1189 error = kern_openat(td, dfd, path, seg,
1190 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1192 kern_close(td, td->td_retval[0]);
1205 * UGH! This is just about the dumbest idea I've ever heard!!
1208 linux_personality(struct thread *td, struct linux_personality_args *args)
1210 struct linux_pemuldata *pem;
1211 struct proc *p = td->td_proc;
1217 if (args->per != 0xffffffff)
1218 pem->persona = args->per;
1221 td->td_retval[0] = old;
1225 struct l_itimerval {
1226 l_timeval it_interval;
1230 #define B2L_ITIMERVAL(bip, lip) \
1231 (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec; \
1232 (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec; \
1233 (bip)->it_value.tv_sec = (lip)->it_value.tv_sec; \
1234 (bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
1237 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
1240 struct l_itimerval ls;
1241 struct itimerval aitv, oitv;
1243 if (uap->itv == NULL) {
1244 uap->itv = uap->oitv;
1245 return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
1248 error = copyin(uap->itv, &ls, sizeof(ls));
1251 B2L_ITIMERVAL(&aitv, &ls);
1252 error = kern_setitimer(td, uap->which, &aitv, &oitv);
1253 if (error != 0 || uap->oitv == NULL)
1255 B2L_ITIMERVAL(&ls, &oitv);
1257 return (copyout(&ls, uap->oitv, sizeof(ls)));
1261 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
1264 struct l_itimerval ls;
1265 struct itimerval aitv;
1267 error = kern_getitimer(td, uap->which, &aitv);
1270 B2L_ITIMERVAL(&ls, &aitv);
1271 return (copyout(&ls, uap->itv, sizeof(ls)));
1274 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1276 linux_nice(struct thread *td, struct linux_nice_args *args)
1279 return (kern_setpriority(td, PRIO_PROCESS, 0, args->inc));
1281 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1284 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
1286 struct ucred *newcred, *oldcred;
1287 l_gid_t *linux_gidset;
1292 ngrp = args->gidsetsize;
1293 if (ngrp < 0 || ngrp >= ngroups_max + 1)
1295 linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_LINUX, M_WAITOK);
1296 error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
1300 crextend(newcred, ngrp + 1);
1303 oldcred = p->p_ucred;
1304 crcopy(newcred, oldcred);
1307 * cr_groups[0] holds egid. Setting the whole set from
1308 * the supplied set will cause egid to be changed too.
1309 * Keep cr_groups[0] unchanged to prevent that.
1312 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS)) != 0) {
1319 newcred->cr_ngroups = ngrp + 1;
1321 bsd_gidset = newcred->cr_groups;
1324 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1328 newcred->cr_ngroups = 1;
1331 proc_set_cred(p, newcred);
1336 free(linux_gidset, M_LINUX);
1341 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1344 l_gid_t *linux_gidset;
1346 int bsd_gidsetsz, ngrp, error;
1348 cred = td->td_ucred;
1349 bsd_gidset = cred->cr_groups;
1350 bsd_gidsetsz = cred->cr_ngroups - 1;
1353 * cr_groups[0] holds egid. Returning the whole set
1354 * here will cause a duplicate. Exclude cr_groups[0]
1358 if ((ngrp = args->gidsetsize) == 0) {
1359 td->td_retval[0] = bsd_gidsetsz;
1363 if (ngrp < bsd_gidsetsz)
1367 linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset),
1369 while (ngrp < bsd_gidsetsz) {
1370 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1374 error = copyout(linux_gidset, args->grouplist, ngrp * sizeof(l_gid_t));
1375 free(linux_gidset, M_LINUX);
1379 td->td_retval[0] = ngrp;
1384 linux_get_dummy_limit(l_uint resource, struct rlimit *rlim)
1387 if (linux_dummy_rlimits == 0)
1391 case LINUX_RLIMIT_LOCKS:
1392 case LINUX_RLIMIT_SIGPENDING:
1393 case LINUX_RLIMIT_MSGQUEUE:
1394 case LINUX_RLIMIT_RTTIME:
1395 rlim->rlim_cur = LINUX_RLIM_INFINITY;
1396 rlim->rlim_max = LINUX_RLIM_INFINITY;
1398 case LINUX_RLIMIT_NICE:
1399 case LINUX_RLIMIT_RTPRIO:
1409 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1411 struct rlimit bsd_rlim;
1412 struct l_rlimit rlim;
1416 if (args->resource >= LINUX_RLIM_NLIMITS)
1419 which = linux_to_bsd_resource[args->resource];
1423 error = copyin(args->rlim, &rlim, sizeof(rlim));
1427 bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1428 bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1429 return (kern_setrlimit(td, which, &bsd_rlim));
1432 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1434 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1436 struct l_rlimit rlim;
1437 struct rlimit bsd_rlim;
1440 if (linux_get_dummy_limit(args->resource, &bsd_rlim)) {
1441 rlim.rlim_cur = bsd_rlim.rlim_cur;
1442 rlim.rlim_max = bsd_rlim.rlim_max;
1443 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1446 if (args->resource >= LINUX_RLIM_NLIMITS)
1449 which = linux_to_bsd_resource[args->resource];
1453 lim_rlimit(td, which, &bsd_rlim);
1455 #ifdef COMPAT_LINUX32
1456 rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1457 if (rlim.rlim_cur == UINT_MAX)
1458 rlim.rlim_cur = INT_MAX;
1459 rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1460 if (rlim.rlim_max == UINT_MAX)
1461 rlim.rlim_max = INT_MAX;
1463 rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1464 if (rlim.rlim_cur == ULONG_MAX)
1465 rlim.rlim_cur = LONG_MAX;
1466 rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1467 if (rlim.rlim_max == ULONG_MAX)
1468 rlim.rlim_max = LONG_MAX;
1470 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1472 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1475 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1477 struct l_rlimit rlim;
1478 struct rlimit bsd_rlim;
1481 if (linux_get_dummy_limit(args->resource, &bsd_rlim)) {
1482 rlim.rlim_cur = bsd_rlim.rlim_cur;
1483 rlim.rlim_max = bsd_rlim.rlim_max;
1484 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1487 if (args->resource >= LINUX_RLIM_NLIMITS)
1490 which = linux_to_bsd_resource[args->resource];
1494 lim_rlimit(td, which, &bsd_rlim);
1496 rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1497 rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1498 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1502 linux_sched_setscheduler(struct thread *td,
1503 struct linux_sched_setscheduler_args *args)
1505 struct sched_param sched_param;
1509 switch (args->policy) {
1510 case LINUX_SCHED_OTHER:
1511 policy = SCHED_OTHER;
1513 case LINUX_SCHED_FIFO:
1514 policy = SCHED_FIFO;
1516 case LINUX_SCHED_RR:
1523 error = copyin(args->param, &sched_param, sizeof(sched_param));
1527 if (linux_map_sched_prio) {
1530 if (sched_param.sched_priority != 0)
1533 sched_param.sched_priority =
1534 PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE;
1538 if (sched_param.sched_priority < 1 ||
1539 sched_param.sched_priority >= LINUX_MAX_RT_PRIO)
1543 * Map [1, LINUX_MAX_RT_PRIO - 1] to
1544 * [0, RTP_PRIO_MAX - RTP_PRIO_MIN] (rounding down).
1546 sched_param.sched_priority =
1547 (sched_param.sched_priority - 1) *
1548 (RTP_PRIO_MAX - RTP_PRIO_MIN + 1) /
1549 (LINUX_MAX_RT_PRIO - 1);
1554 tdt = linux_tdfind(td, args->pid, -1);
1558 error = kern_sched_setscheduler(td, tdt, policy, &sched_param);
1559 PROC_UNLOCK(tdt->td_proc);
1564 linux_sched_getscheduler(struct thread *td,
1565 struct linux_sched_getscheduler_args *args)
1570 tdt = linux_tdfind(td, args->pid, -1);
1574 error = kern_sched_getscheduler(td, tdt, &policy);
1575 PROC_UNLOCK(tdt->td_proc);
1579 td->td_retval[0] = LINUX_SCHED_OTHER;
1582 td->td_retval[0] = LINUX_SCHED_FIFO;
1585 td->td_retval[0] = LINUX_SCHED_RR;
1592 linux_sched_get_priority_max(struct thread *td,
1593 struct linux_sched_get_priority_max_args *args)
1595 struct sched_get_priority_max_args bsd;
1597 if (linux_map_sched_prio) {
1598 switch (args->policy) {
1599 case LINUX_SCHED_OTHER:
1600 td->td_retval[0] = 0;
1602 case LINUX_SCHED_FIFO:
1603 case LINUX_SCHED_RR:
1604 td->td_retval[0] = LINUX_MAX_RT_PRIO - 1;
1611 switch (args->policy) {
1612 case LINUX_SCHED_OTHER:
1613 bsd.policy = SCHED_OTHER;
1615 case LINUX_SCHED_FIFO:
1616 bsd.policy = SCHED_FIFO;
1618 case LINUX_SCHED_RR:
1619 bsd.policy = SCHED_RR;
1624 return (sys_sched_get_priority_max(td, &bsd));
1628 linux_sched_get_priority_min(struct thread *td,
1629 struct linux_sched_get_priority_min_args *args)
1631 struct sched_get_priority_min_args bsd;
1633 if (linux_map_sched_prio) {
1634 switch (args->policy) {
1635 case LINUX_SCHED_OTHER:
1636 td->td_retval[0] = 0;
1638 case LINUX_SCHED_FIFO:
1639 case LINUX_SCHED_RR:
1640 td->td_retval[0] = 1;
1647 switch (args->policy) {
1648 case LINUX_SCHED_OTHER:
1649 bsd.policy = SCHED_OTHER;
1651 case LINUX_SCHED_FIFO:
1652 bsd.policy = SCHED_FIFO;
1654 case LINUX_SCHED_RR:
1655 bsd.policy = SCHED_RR;
1660 return (sys_sched_get_priority_min(td, &bsd));
1663 #define REBOOT_CAD_ON 0x89abcdef
1664 #define REBOOT_CAD_OFF 0
1665 #define REBOOT_HALT 0xcdef0123
1666 #define REBOOT_RESTART 0x01234567
1667 #define REBOOT_RESTART2 0xA1B2C3D4
1668 #define REBOOT_POWEROFF 0x4321FEDC
1669 #define REBOOT_MAGIC1 0xfee1dead
1670 #define REBOOT_MAGIC2 0x28121969
1671 #define REBOOT_MAGIC2A 0x05121996
1672 #define REBOOT_MAGIC2B 0x16041998
1675 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1677 struct reboot_args bsd_args;
1679 if (args->magic1 != REBOOT_MAGIC1)
1682 switch (args->magic2) {
1684 case REBOOT_MAGIC2A:
1685 case REBOOT_MAGIC2B:
1691 switch (args->cmd) {
1693 case REBOOT_CAD_OFF:
1694 return (priv_check(td, PRIV_REBOOT));
1696 bsd_args.opt = RB_HALT;
1698 case REBOOT_RESTART:
1699 case REBOOT_RESTART2:
1702 case REBOOT_POWEROFF:
1703 bsd_args.opt = RB_POWEROFF;
1708 return (sys_reboot(td, &bsd_args));
1712 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1715 td->td_retval[0] = td->td_proc->p_pid;
1721 linux_gettid(struct thread *td, struct linux_gettid_args *args)
1723 struct linux_emuldata *em;
1726 KASSERT(em != NULL, ("gettid: emuldata not found.\n"));
1728 td->td_retval[0] = em->em_tid;
1734 linux_getppid(struct thread *td, struct linux_getppid_args *args)
1737 td->td_retval[0] = kern_getppid(td);
1742 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1745 td->td_retval[0] = td->td_ucred->cr_rgid;
1750 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1753 td->td_retval[0] = td->td_ucred->cr_ruid;
1758 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1761 return (kern_getsid(td, args->pid));
1765 linux_nosys(struct thread *td, struct nosys_args *ignore)
1772 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1776 error = kern_getpriority(td, args->which, args->who);
1777 td->td_retval[0] = 20 - td->td_retval[0];
1782 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1787 name[1] = KERN_HOSTNAME;
1788 return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1793 linux_setdomainname(struct thread *td, struct linux_setdomainname_args *args)
1798 name[1] = KERN_NISDOMAINNAME;
1799 return (userland_sysctl(td, name, 2, 0, 0, 0, args->name,
1804 linux_exit_group(struct thread *td, struct linux_exit_group_args *args)
1807 LINUX_CTR2(exit_group, "thread(%d) (%d)", td->td_tid,
1811 * XXX: we should send a signal to the parent if
1812 * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?)
1813 * as it doesnt occur often.
1815 exit1(td, args->error_code, 0);
1819 #define _LINUX_CAPABILITY_VERSION_1 0x19980330
1820 #define _LINUX_CAPABILITY_VERSION_2 0x20071026
1821 #define _LINUX_CAPABILITY_VERSION_3 0x20080522
1823 struct l_user_cap_header {
1828 struct l_user_cap_data {
1835 linux_capget(struct thread *td, struct linux_capget_args *uap)
1837 struct l_user_cap_header luch;
1838 struct l_user_cap_data lucd[2];
1841 if (uap->hdrp == NULL)
1844 error = copyin(uap->hdrp, &luch, sizeof(luch));
1848 switch (luch.version) {
1849 case _LINUX_CAPABILITY_VERSION_1:
1852 case _LINUX_CAPABILITY_VERSION_2:
1853 case _LINUX_CAPABILITY_VERSION_3:
1857 luch.version = _LINUX_CAPABILITY_VERSION_1;
1858 error = copyout(&luch, uap->hdrp, sizeof(luch));
1869 * The current implementation doesn't support setting
1870 * a capability (it's essentially a stub) so indicate
1871 * that no capabilities are currently set or available
1874 memset(&lucd, 0, u32s * sizeof(lucd[0]));
1875 error = copyout(&lucd, uap->datap, u32s * sizeof(lucd[0]));
1882 linux_capset(struct thread *td, struct linux_capset_args *uap)
1884 struct l_user_cap_header luch;
1885 struct l_user_cap_data lucd[2];
1888 if (uap->hdrp == NULL || uap->datap == NULL)
1891 error = copyin(uap->hdrp, &luch, sizeof(luch));
1895 switch (luch.version) {
1896 case _LINUX_CAPABILITY_VERSION_1:
1899 case _LINUX_CAPABILITY_VERSION_2:
1900 case _LINUX_CAPABILITY_VERSION_3:
1904 luch.version = _LINUX_CAPABILITY_VERSION_1;
1905 error = copyout(&luch, uap->hdrp, sizeof(luch));
1914 error = copyin(uap->datap, &lucd, u32s * sizeof(lucd[0]));
1918 /* We currently don't support setting any capabilities. */
1919 for (i = 0; i < u32s; i++) {
1920 if (lucd[i].effective || lucd[i].permitted ||
1921 lucd[i].inheritable) {
1923 "capset[%d] effective=0x%x, permitted=0x%x, "
1924 "inheritable=0x%x is not implemented", i,
1925 (int)lucd[i].effective, (int)lucd[i].permitted,
1926 (int)lucd[i].inheritable);
1935 linux_prctl(struct thread *td, struct linux_prctl_args *args)
1937 int error = 0, max_size;
1938 struct proc *p = td->td_proc;
1939 char comm[LINUX_MAX_COMM_LEN];
1942 switch (args->option) {
1943 case LINUX_PR_SET_PDEATHSIG:
1944 if (!LINUX_SIG_VALID(args->arg2))
1946 pdeath_signal = linux_to_bsd_signal(args->arg2);
1947 return (kern_procctl(td, P_PID, 0, PROC_PDEATHSIG_CTL,
1949 case LINUX_PR_GET_PDEATHSIG:
1950 error = kern_procctl(td, P_PID, 0, PROC_PDEATHSIG_STATUS,
1954 pdeath_signal = bsd_to_linux_signal(pdeath_signal);
1955 return (copyout(&pdeath_signal,
1956 (void *)(register_t)args->arg2,
1957 sizeof(pdeath_signal)));
1959 case LINUX_PR_SET_DUMPABLE:
1960 linux_msg(td, "unsupported prctl PR_SET_DUMPABLE");
1963 case LINUX_PR_GET_KEEPCAPS:
1965 * Indicate that we always clear the effective and
1966 * permitted capability sets when the user id becomes
1967 * non-zero (actually the capability sets are simply
1968 * always zero in the current implementation).
1970 td->td_retval[0] = 0;
1972 case LINUX_PR_SET_KEEPCAPS:
1974 * Ignore requests to keep the effective and permitted
1975 * capability sets when the user id becomes non-zero.
1978 case LINUX_PR_SET_NAME:
1980 * To be on the safe side we need to make sure to not
1981 * overflow the size a Linux program expects. We already
1982 * do this here in the copyin, so that we don't need to
1985 max_size = MIN(sizeof(comm), sizeof(p->p_comm));
1986 error = copyinstr((void *)(register_t)args->arg2, comm,
1989 /* Linux silently truncates the name if it is too long. */
1990 if (error == ENAMETOOLONG) {
1992 * XXX: copyinstr() isn't documented to populate the
1993 * array completely, so do a copyin() to be on the
1994 * safe side. This should be changed in case
1995 * copyinstr() is changed to guarantee this.
1997 error = copyin((void *)(register_t)args->arg2, comm,
1999 comm[max_size - 1] = '\0';
2005 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
2008 case LINUX_PR_GET_NAME:
2010 strlcpy(comm, p->p_comm, sizeof(comm));
2012 error = copyout(comm, (void *)(register_t)args->arg2,
2015 case LINUX_PR_GET_SECCOMP:
2016 case LINUX_PR_SET_SECCOMP:
2018 * Same as returned by Linux without CONFIG_SECCOMP enabled.
2022 case LINUX_PR_SET_NO_NEW_PRIVS:
2023 linux_msg(td, "unsupported prctl PR_SET_NO_NEW_PRIVS");
2026 case LINUX_PR_SET_PTRACER:
2027 linux_msg(td, "unsupported prctl PR_SET_PTRACER");
2031 linux_msg(td, "unsupported prctl option %d", args->option);
2040 linux_sched_setparam(struct thread *td,
2041 struct linux_sched_setparam_args *uap)
2043 struct sched_param sched_param;
2047 error = copyin(uap->param, &sched_param, sizeof(sched_param));
2051 tdt = linux_tdfind(td, uap->pid, -1);
2055 if (linux_map_sched_prio) {
2056 error = kern_sched_getscheduler(td, tdt, &policy);
2062 if (sched_param.sched_priority != 0) {
2066 sched_param.sched_priority =
2067 PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE;
2071 if (sched_param.sched_priority < 1 ||
2072 sched_param.sched_priority >= LINUX_MAX_RT_PRIO) {
2077 * Map [1, LINUX_MAX_RT_PRIO - 1] to
2078 * [0, RTP_PRIO_MAX - RTP_PRIO_MIN] (rounding down).
2080 sched_param.sched_priority =
2081 (sched_param.sched_priority - 1) *
2082 (RTP_PRIO_MAX - RTP_PRIO_MIN + 1) /
2083 (LINUX_MAX_RT_PRIO - 1);
2088 error = kern_sched_setparam(td, tdt, &sched_param);
2089 out: PROC_UNLOCK(tdt->td_proc);
2094 linux_sched_getparam(struct thread *td,
2095 struct linux_sched_getparam_args *uap)
2097 struct sched_param sched_param;
2101 tdt = linux_tdfind(td, uap->pid, -1);
2105 error = kern_sched_getparam(td, tdt, &sched_param);
2107 PROC_UNLOCK(tdt->td_proc);
2111 if (linux_map_sched_prio) {
2112 error = kern_sched_getscheduler(td, tdt, &policy);
2113 PROC_UNLOCK(tdt->td_proc);
2119 sched_param.sched_priority = 0;
2124 * Map [0, RTP_PRIO_MAX - RTP_PRIO_MIN] to
2125 * [1, LINUX_MAX_RT_PRIO - 1] (rounding up).
2127 sched_param.sched_priority =
2128 (sched_param.sched_priority *
2129 (LINUX_MAX_RT_PRIO - 1) +
2130 (RTP_PRIO_MAX - RTP_PRIO_MIN - 1)) /
2131 (RTP_PRIO_MAX - RTP_PRIO_MIN) + 1;
2135 PROC_UNLOCK(tdt->td_proc);
2137 error = copyout(&sched_param, uap->param, sizeof(sched_param));
2142 * Get affinity of a process.
2145 linux_sched_getaffinity(struct thread *td,
2146 struct linux_sched_getaffinity_args *args)
2151 if (args->len < sizeof(cpuset_t))
2154 tdt = linux_tdfind(td, args->pid, -1);
2158 PROC_UNLOCK(tdt->td_proc);
2160 error = kern_cpuset_getaffinity(td, CPU_LEVEL_WHICH, CPU_WHICH_TID,
2161 tdt->td_tid, sizeof(cpuset_t), (cpuset_t *)args->user_mask_ptr);
2163 td->td_retval[0] = sizeof(cpuset_t);
2169 * Set affinity of a process.
2172 linux_sched_setaffinity(struct thread *td,
2173 struct linux_sched_setaffinity_args *args)
2177 if (args->len < sizeof(cpuset_t))
2180 tdt = linux_tdfind(td, args->pid, -1);
2184 PROC_UNLOCK(tdt->td_proc);
2186 return (kern_cpuset_setaffinity(td, CPU_LEVEL_WHICH, CPU_WHICH_TID,
2187 tdt->td_tid, sizeof(cpuset_t), (cpuset_t *) args->user_mask_ptr));
2190 struct linux_rlimit64 {
2196 linux_prlimit64(struct thread *td, struct linux_prlimit64_args *args)
2198 struct rlimit rlim, nrlim;
2199 struct linux_rlimit64 lrlim;
2205 if (args->new == NULL && args->old != NULL) {
2206 if (linux_get_dummy_limit(args->resource, &rlim)) {
2207 lrlim.rlim_cur = rlim.rlim_cur;
2208 lrlim.rlim_max = rlim.rlim_max;
2209 return (copyout(&lrlim, args->old, sizeof(lrlim)));
2213 if (args->resource >= LINUX_RLIM_NLIMITS)
2216 which = linux_to_bsd_resource[args->resource];
2220 if (args->new != NULL) {
2222 * Note. Unlike FreeBSD where rlim is signed 64-bit Linux
2223 * rlim is unsigned 64-bit. FreeBSD treats negative limits
2224 * as INFINITY so we do not need a conversion even.
2226 error = copyin(args->new, &nrlim, sizeof(nrlim));
2231 flags = PGET_HOLD | PGET_NOTWEXIT;
2232 if (args->new != NULL)
2233 flags |= PGET_CANDEBUG;
2235 flags |= PGET_CANSEE;
2236 if (args->pid == 0) {
2240 error = pget(args->pid, flags, &p);
2244 if (args->old != NULL) {
2246 lim_rlimit_proc(p, which, &rlim);
2248 if (rlim.rlim_cur == RLIM_INFINITY)
2249 lrlim.rlim_cur = LINUX_RLIM_INFINITY;
2251 lrlim.rlim_cur = rlim.rlim_cur;
2252 if (rlim.rlim_max == RLIM_INFINITY)
2253 lrlim.rlim_max = LINUX_RLIM_INFINITY;
2255 lrlim.rlim_max = rlim.rlim_max;
2256 error = copyout(&lrlim, args->old, sizeof(lrlim));
2261 if (args->new != NULL)
2262 error = kern_proc_setrlimit(td, p, which, &nrlim);
2270 linux_pselect6(struct thread *td, struct linux_pselect6_args *args)
2272 struct timeval utv, tv0, tv1, *tvp;
2273 struct l_pselect6arg lpse6;
2274 struct l_timespec lts;
2275 struct timespec uts;
2282 if (args->sig != NULL) {
2283 error = copyin(args->sig, &lpse6, sizeof(lpse6));
2286 if (lpse6.ss_len != sizeof(l_ss))
2288 if (lpse6.ss != 0) {
2289 error = copyin(PTRIN(lpse6.ss), &l_ss,
2293 linux_to_bsd_sigset(&l_ss, &ss);
2299 * Currently glibc changes nanosecond number to microsecond.
2300 * This mean losing precision but for now it is hardly seen.
2302 if (args->tsp != NULL) {
2303 error = copyin(args->tsp, <s, sizeof(lts));
2306 error = linux_to_native_timespec(&uts, <s);
2310 TIMESPEC_TO_TIMEVAL(&utv, &uts);
2311 if (itimerfix(&utv))
2319 error = kern_pselect(td, args->nfds, args->readfds, args->writefds,
2320 args->exceptfds, tvp, ssp, LINUX_NFDBITS);
2322 if (error == 0 && args->tsp != NULL) {
2323 if (td->td_retval[0] != 0) {
2325 * Compute how much time was left of the timeout,
2326 * by subtracting the current time and the time
2327 * before we started the call, and subtracting
2328 * that result from the user-supplied value.
2332 timevalsub(&tv1, &tv0);
2333 timevalsub(&utv, &tv1);
2339 TIMEVAL_TO_TIMESPEC(&utv, &uts);
2341 error = native_to_linux_timespec(<s, &uts);
2343 error = copyout(<s, args->tsp, sizeof(lts));
2350 linux_ppoll(struct thread *td, struct linux_ppoll_args *args)
2352 struct timespec ts0, ts1;
2353 struct l_timespec lts;
2354 struct timespec uts, *tsp;
2360 if (args->sset != NULL) {
2361 if (args->ssize != sizeof(l_ss))
2363 error = copyin(args->sset, &l_ss, sizeof(l_ss));
2366 linux_to_bsd_sigset(&l_ss, &ss);
2370 if (args->tsp != NULL) {
2371 error = copyin(args->tsp, <s, sizeof(lts));
2374 error = linux_to_native_timespec(&uts, <s);
2383 error = kern_poll(td, args->fds, args->nfds, tsp, ssp);
2385 if (error == 0 && args->tsp != NULL) {
2386 if (td->td_retval[0]) {
2388 timespecsub(&ts1, &ts0, &ts1);
2389 timespecsub(&uts, &ts1, &uts);
2391 timespecclear(&uts);
2393 timespecclear(&uts);
2395 error = native_to_linux_timespec(<s, &uts);
2397 error = copyout(<s, args->tsp, sizeof(lts));
2404 linux_sched_rr_get_interval(struct thread *td,
2405 struct linux_sched_rr_get_interval_args *uap)
2408 struct l_timespec lts;
2413 * According to man in case the invalid pid specified
2414 * EINVAL should be returned.
2419 tdt = linux_tdfind(td, uap->pid, -1);
2423 error = kern_sched_rr_get_interval_td(td, tdt, &ts);
2424 PROC_UNLOCK(tdt->td_proc);
2427 error = native_to_linux_timespec(<s, &ts);
2430 return (copyout(<s, uap->interval, sizeof(lts)));
2434 * In case when the Linux thread is the initial thread in
2435 * the thread group thread id is equal to the process id.
2436 * Glibc depends on this magic (assert in pthread_getattr_np.c).
2439 linux_tdfind(struct thread *td, lwpid_t tid, pid_t pid)
2441 struct linux_emuldata *em;
2446 if (tid == 0 || tid == td->td_tid) {
2448 PROC_LOCK(tdt->td_proc);
2449 } else if (tid > PID_MAX)
2450 tdt = tdfind(tid, pid);
2453 * Initial thread where the tid equal to the pid.
2457 if (SV_PROC_ABI(p) != SV_ABI_LINUX) {
2459 * p is not a Linuxulator process.
2464 FOREACH_THREAD_IN_PROC(p, tdt) {
2466 if (tid == em->em_tid)
2478 linux_to_bsd_waitopts(int options, int *bsdopts)
2481 if (options & LINUX_WNOHANG)
2482 *bsdopts |= WNOHANG;
2483 if (options & LINUX_WUNTRACED)
2484 *bsdopts |= WUNTRACED;
2485 if (options & LINUX_WEXITED)
2486 *bsdopts |= WEXITED;
2487 if (options & LINUX_WCONTINUED)
2488 *bsdopts |= WCONTINUED;
2489 if (options & LINUX_WNOWAIT)
2490 *bsdopts |= WNOWAIT;
2492 if (options & __WCLONE)
2493 *bsdopts |= WLINUXCLONE;
2497 linux_getrandom(struct thread *td, struct linux_getrandom_args *args)
2503 if (args->flags & ~(LINUX_GRND_NONBLOCK|LINUX_GRND_RANDOM))
2505 if (args->count > INT_MAX)
2506 args->count = INT_MAX;
2508 iov.iov_base = args->buf;
2509 iov.iov_len = args->count;
2513 uio.uio_resid = iov.iov_len;
2514 uio.uio_segflg = UIO_USERSPACE;
2515 uio.uio_rw = UIO_READ;
2518 error = read_random_uio(&uio, args->flags & LINUX_GRND_NONBLOCK);
2520 td->td_retval[0] = args->count - uio.uio_resid;
2525 linux_mincore(struct thread *td, struct linux_mincore_args *args)
2528 /* Needs to be page-aligned */
2529 if (args->start & PAGE_MASK)
2531 return (kern_mincore(td, args->start, args->len, args->vec));
2534 #define SYSLOG_TAG "<6>"
2537 linux_syslog(struct thread *td, struct linux_syslog_args *args)
2539 char buf[128], *src, *dst;
2543 if (args->type != LINUX_SYSLOG_ACTION_READ_ALL) {
2544 linux_msg(td, "syslog unsupported type 0x%x", args->type);
2548 if (args->len < 6) {
2549 td->td_retval[0] = 0;
2553 error = priv_check(td, PRIV_MSGBUF);
2557 mtx_lock(&msgbuf_lock);
2558 msgbuf_peekbytes(msgbufp, NULL, 0, &seq);
2559 mtx_unlock(&msgbuf_lock);
2562 error = copyout(&SYSLOG_TAG, dst, sizeof(SYSLOG_TAG));
2563 /* The -1 is to skip the trailing '\0'. */
2564 dst += sizeof(SYSLOG_TAG) - 1;
2566 while (error == 0) {
2567 mtx_lock(&msgbuf_lock);
2568 buflen = msgbuf_peekbytes(msgbufp, buf, sizeof(buf), &seq);
2569 mtx_unlock(&msgbuf_lock);
2574 for (src = buf; src < buf + buflen && error == 0; src++) {
2578 if (dst >= args->buf + args->len)
2581 error = copyout(src, dst, 1);
2584 if (*src == '\n' && *(src + 1) != '<' &&
2585 dst + sizeof(SYSLOG_TAG) < args->buf + args->len) {
2586 error = copyout(&SYSLOG_TAG,
2587 dst, sizeof(SYSLOG_TAG));
2588 dst += sizeof(SYSLOG_TAG) - 1;
2593 td->td_retval[0] = dst - args->buf;
2598 linux_getcpu(struct thread *td, struct linux_getcpu_args *args)
2600 int cpu, error, node;
2602 cpu = td->td_oncpu; /* Make sure it doesn't change during copyout(9) */
2604 node = cpuid_to_pcpu[cpu]->pc_domain;
2606 if (args->cpu != NULL)
2607 error = copyout(&cpu, args->cpu, sizeof(l_int));
2608 if (args->node != NULL)
2609 error = copyout(&node, args->node, sizeof(l_int));