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);
150 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args)
152 struct l_sysinfo sysinfo;
156 bzero(&sysinfo, sizeof(sysinfo));
160 sysinfo.uptime = ts.tv_sec;
162 /* Use the information from the mib to get our load averages */
163 for (i = 0; i < 3; i++)
164 sysinfo.loads[i] = averunnable.ldavg[i] *
165 LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale;
167 sysinfo.totalram = physmem * PAGE_SIZE;
168 sysinfo.freeram = (u_long)vm_free_count() * PAGE_SIZE;
171 * sharedram counts pages allocated to named, swap-backed objects such
172 * as shared memory segments and tmpfs files. There is no cheap way to
173 * compute this, so just leave the field unpopulated. Linux itself only
174 * started setting this field in the 3.x timeframe.
176 sysinfo.sharedram = 0;
177 sysinfo.bufferram = 0;
179 swap_pager_status(&i, &j);
180 sysinfo.totalswap = i * PAGE_SIZE;
181 sysinfo.freeswap = (i - j) * PAGE_SIZE;
183 sysinfo.procs = nprocs;
186 * Platforms supported by the emulation layer do not have a notion of
189 sysinfo.totalhigh = 0;
190 sysinfo.freehigh = 0;
192 sysinfo.mem_unit = 1;
194 return (copyout(&sysinfo, args->info, sizeof(sysinfo)));
197 #ifdef LINUX_LEGACY_SYSCALLS
199 linux_alarm(struct thread *td, struct linux_alarm_args *args)
201 struct itimerval it, old_it;
207 * Linux alarm() is always successful. Limit secs to INT32_MAX / 2
208 * to match kern_setitimer()'s limit to avoid error from it.
210 * XXX. Linux limit secs to INT_MAX on 32 and does not limit on 64-bit
213 if (secs > INT32_MAX / 2)
214 secs = INT32_MAX / 2;
216 it.it_value.tv_sec = secs;
217 it.it_value.tv_usec = 0;
218 timevalclear(&it.it_interval);
219 error = kern_setitimer(td, ITIMER_REAL, &it, &old_it);
220 KASSERT(error == 0, ("kern_setitimer returns %d", error));
222 if ((old_it.it_value.tv_sec == 0 && old_it.it_value.tv_usec > 0) ||
223 old_it.it_value.tv_usec >= 500000)
224 old_it.it_value.tv_sec++;
225 td->td_retval[0] = old_it.it_value.tv_sec;
231 linux_brk(struct thread *td, struct linux_brk_args *args)
233 struct vmspace *vm = td->td_proc->p_vmspace;
236 old = (uintptr_t)vm->vm_daddr + ctob(vm->vm_dsize);
237 new = (uintptr_t)args->dsend;
238 if ((caddr_t)new > vm->vm_daddr && !kern_break(td, &new))
239 td->td_retval[0] = (register_t)new;
241 td->td_retval[0] = (register_t)old;
246 #if defined(__i386__)
247 /* XXX: what about amd64/linux32? */
250 linux_uselib(struct thread *td, struct linux_uselib_args *args)
256 vm_map_entry_t entry;
259 unsigned long file_offset;
260 unsigned long bss_size;
264 bool locked, opened, textset;
272 if (!LUSECONVPATH(td)) {
273 NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | AUDITVNODE1,
274 UIO_USERSPACE, args->library, td);
277 LCONVPATHEXIST(td, args->library, &library);
278 NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | AUDITVNODE1,
279 UIO_SYSSPACE, library, td);
287 NDFREE(&ni, NDF_ONLY_PNBUF);
290 * From here on down, we have a locked vnode that must be unlocked.
291 * XXX: The code below largely duplicates exec_check_permissions().
296 error = VOP_GETATTR(vp, &attr, td->td_ucred);
300 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
301 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
302 /* EACCESS is what exec(2) returns. */
308 if (attr.va_size == 0) {
313 /* Can we access it? */
314 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
319 * XXX: This should use vn_open() so that it is properly authorized,
320 * and to reduce code redundancy all over the place here.
321 * XXX: Not really, it duplicates far more of exec_check_permissions()
325 error = mac_vnode_check_open(td->td_ucred, vp, VREAD);
329 error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL);
334 /* Pull in executable header into exec_map */
335 error = vm_mmap(exec_map, (vm_offset_t *)&a_out, PAGE_SIZE,
336 VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 0);
340 /* Is it a Linux binary ? */
341 if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
347 * While we are here, we should REALLY do some more checks
350 /* Set file/virtual offset based on a.out variant. */
351 switch ((int)(a_out->a_magic & 0xffff)) {
352 case 0413: /* ZMAGIC */
355 case 0314: /* QMAGIC */
363 bss_size = round_page(a_out->a_bss);
365 /* Check various fields in header for validity/bounds. */
366 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
371 /* text + data can't exceed file size */
372 if (a_out->a_data + a_out->a_text > attr.va_size) {
378 * text/data/bss must not exceed limits
379 * XXX - this is not complete. it should check current usage PLUS
380 * the resources needed by this library.
382 PROC_LOCK(td->td_proc);
383 if (a_out->a_text > maxtsiz ||
384 a_out->a_data + bss_size > lim_cur_proc(td->td_proc, RLIMIT_DATA) ||
385 racct_set(td->td_proc, RACCT_DATA, a_out->a_data +
387 PROC_UNLOCK(td->td_proc);
391 PROC_UNLOCK(td->td_proc);
394 * Prevent more writers.
396 error = VOP_SET_TEXT(vp);
402 * Lock no longer needed
408 * Check if file_offset page aligned. Currently we cannot handle
409 * misalinged file offsets, and so we read in the entire image
412 if (file_offset & PAGE_MASK) {
413 /* Map text+data read/write/execute */
415 /* a_entry is the load address and is page aligned */
416 vmaddr = trunc_page(a_out->a_entry);
418 /* get anon user mapping, read+write+execute */
419 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
420 &vmaddr, a_out->a_text + a_out->a_data, 0, VMFS_NO_SPACE,
421 VM_PROT_ALL, VM_PROT_ALL, 0);
425 error = vn_rdwr(UIO_READ, vp, (void *)vmaddr, file_offset,
426 a_out->a_text + a_out->a_data, UIO_USERSPACE, 0,
427 td->td_ucred, NOCRED, &aresid, td);
436 * for QMAGIC, a_entry is 20 bytes beyond the load address
437 * to skip the executable header
439 vmaddr = trunc_page(a_out->a_entry);
442 * Map it all into the process's space as a single
443 * copy-on-write "data" segment.
445 map = &td->td_proc->p_vmspace->vm_map;
446 error = vm_mmap(map, &vmaddr,
447 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
448 MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset);
452 if (!vm_map_lookup_entry(map, vmaddr, &entry)) {
457 entry->eflags |= MAP_ENTRY_VN_EXEC;
463 /* Calculate BSS start address */
464 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
467 /* allocate some 'anon' space */
468 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
469 &vmaddr, bss_size, 0, VMFS_NO_SPACE, VM_PROT_ALL,
480 VOP_CLOSE(vp, FREAD, td->td_ucred, td);
485 VOP_LOCK(vp, LK_SHARED | LK_RETRY);
487 VOP_UNSET_TEXT_CHECKED(vp);
492 /* Release the temporary mapping. */
494 kmap_free_wakeup(exec_map, (vm_offset_t)a_out, PAGE_SIZE);
499 #endif /* __i386__ */
501 #ifdef LINUX_LEGACY_SYSCALLS
503 linux_select(struct thread *td, struct linux_select_args *args)
506 struct timeval tv0, tv1, utv, *tvp;
510 * Store current time for computation of the amount of
514 if ((error = copyin(args->timeout, <v, sizeof(ltv))))
516 utv.tv_sec = ltv.tv_sec;
517 utv.tv_usec = ltv.tv_usec;
519 if (itimerfix(&utv)) {
521 * The timeval was invalid. Convert it to something
522 * valid that will act as it does under Linux.
524 utv.tv_sec += utv.tv_usec / 1000000;
525 utv.tv_usec %= 1000000;
526 if (utv.tv_usec < 0) {
528 utv.tv_usec += 1000000;
538 error = kern_select(td, args->nfds, args->readfds, args->writefds,
539 args->exceptfds, tvp, LINUX_NFDBITS);
544 if (td->td_retval[0]) {
546 * Compute how much time was left of the timeout,
547 * by subtracting the current time and the time
548 * before we started the call, and subtracting
549 * that result from the user-supplied value.
552 timevalsub(&tv1, &tv0);
553 timevalsub(&utv, &tv1);
558 ltv.tv_sec = utv.tv_sec;
559 ltv.tv_usec = utv.tv_usec;
560 if ((error = copyout(<v, args->timeout, sizeof(ltv))))
570 linux_mremap(struct thread *td, struct linux_mremap_args *args)
576 if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) {
577 td->td_retval[0] = 0;
582 * Check for the page alignment.
583 * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK.
585 if (args->addr & PAGE_MASK) {
586 td->td_retval[0] = 0;
590 args->new_len = round_page(args->new_len);
591 args->old_len = round_page(args->old_len);
593 if (args->new_len > args->old_len) {
594 td->td_retval[0] = 0;
598 if (args->new_len < args->old_len) {
599 addr = args->addr + args->new_len;
600 len = args->old_len - args->new_len;
601 error = kern_munmap(td, addr, len);
604 td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
608 #define LINUX_MS_ASYNC 0x0001
609 #define LINUX_MS_INVALIDATE 0x0002
610 #define LINUX_MS_SYNC 0x0004
613 linux_msync(struct thread *td, struct linux_msync_args *args)
616 return (kern_msync(td, args->addr, args->len,
617 args->fl & ~LINUX_MS_SYNC));
620 #ifdef LINUX_LEGACY_SYSCALLS
622 linux_time(struct thread *td, struct linux_time_args *args)
630 if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
632 td->td_retval[0] = tm;
637 struct l_times_argv {
640 l_clock_t tms_cutime;
641 l_clock_t tms_cstime;
646 * Glibc versions prior to 2.2.1 always use hard-coded CLK_TCK value.
647 * Since 2.2.1 Glibc uses value exported from kernel via AT_CLKTCK
648 * auxiliary vector entry.
652 #define CONVOTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
653 #define CONVNTCK(r) (r.tv_sec * stclohz + r.tv_usec / (1000000 / stclohz))
655 #define CONVTCK(r) (linux_kernver(td) >= LINUX_KERNVER_2004000 ? \
656 CONVNTCK(r) : CONVOTCK(r))
659 linux_times(struct thread *td, struct linux_times_args *args)
661 struct timeval tv, utime, stime, cutime, cstime;
662 struct l_times_argv tms;
666 if (args->buf != NULL) {
670 calcru(p, &utime, &stime);
672 calccru(p, &cutime, &cstime);
675 tms.tms_utime = CONVTCK(utime);
676 tms.tms_stime = CONVTCK(stime);
678 tms.tms_cutime = CONVTCK(cutime);
679 tms.tms_cstime = CONVTCK(cstime);
681 if ((error = copyout(&tms, args->buf, sizeof(tms))))
686 td->td_retval[0] = (int)CONVTCK(tv);
691 linux_newuname(struct thread *td, struct linux_newuname_args *args)
693 struct l_new_utsname utsname;
694 char osname[LINUX_MAX_UTSNAME];
695 char osrelease[LINUX_MAX_UTSNAME];
698 linux_get_osname(td, osname);
699 linux_get_osrelease(td, osrelease);
701 bzero(&utsname, sizeof(utsname));
702 strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
703 getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
704 getcreddomainname(td->td_ucred, utsname.domainname, LINUX_MAX_UTSNAME);
705 strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
706 strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
707 for (p = utsname.version; *p != '\0'; ++p)
712 #if defined(__amd64__)
714 * On amd64, Linux uname(2) needs to return "x86_64"
715 * for both 64-bit and 32-bit applications. On 32-bit,
716 * the string returned by getauxval(AT_PLATFORM) needs
717 * to remain "i686", though.
719 strlcpy(utsname.machine, "x86_64", LINUX_MAX_UTSNAME);
721 strlcpy(utsname.machine, linux_kplatform, LINUX_MAX_UTSNAME);
724 return (copyout(&utsname, args->buf, sizeof(utsname)));
732 #ifdef LINUX_LEGACY_SYSCALLS
734 linux_utime(struct thread *td, struct linux_utime_args *args)
736 struct timeval tv[2], *tvp;
737 struct l_utimbuf lut;
742 convpath = LUSECONVPATH(td);
744 LCONVPATHEXIST(td, args->fname, &fname);
747 if ((error = copyin(args->times, &lut, sizeof lut))) {
752 tv[0].tv_sec = lut.l_actime;
754 tv[1].tv_sec = lut.l_modtime;
761 error = kern_utimesat(td, AT_FDCWD, args->fname, UIO_USERSPACE,
764 error = kern_utimesat(td, AT_FDCWD, fname, UIO_SYSSPACE, tvp,
772 #ifdef LINUX_LEGACY_SYSCALLS
774 linux_utimes(struct thread *td, struct linux_utimes_args *args)
777 struct timeval tv[2], *tvp = NULL;
782 convpath = LUSECONVPATH(td);
784 LCONVPATHEXIST(td, args->fname, &fname);
786 if (args->tptr != NULL) {
787 if ((error = copyin(args->tptr, ltv, sizeof ltv))) {
791 tv[0].tv_sec = ltv[0].tv_sec;
792 tv[0].tv_usec = ltv[0].tv_usec;
793 tv[1].tv_sec = ltv[1].tv_sec;
794 tv[1].tv_usec = ltv[1].tv_usec;
799 error = kern_utimesat(td, AT_FDCWD, args->fname, UIO_USERSPACE,
802 error = kern_utimesat(td, AT_FDCWD, fname, UIO_SYSSPACE,
811 linux_utimensat_nsec_valid(l_long nsec)
814 if (nsec == LINUX_UTIME_OMIT || nsec == LINUX_UTIME_NOW)
816 if (nsec >= 0 && nsec <= 999999999)
822 linux_utimensat(struct thread *td, struct linux_utimensat_args *args)
824 struct l_timespec l_times[2];
825 struct timespec times[2], *timesp = NULL;
827 int error, dfd, flags = 0;
829 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
831 if (args->flags & ~LINUX_AT_SYMLINK_NOFOLLOW)
834 if (args->times != NULL) {
835 error = copyin(args->times, l_times, sizeof(l_times));
839 if (linux_utimensat_nsec_valid(l_times[0].tv_nsec) != 0 ||
840 linux_utimensat_nsec_valid(l_times[1].tv_nsec) != 0)
843 times[0].tv_sec = l_times[0].tv_sec;
844 switch (l_times[0].tv_nsec)
846 case LINUX_UTIME_OMIT:
847 times[0].tv_nsec = UTIME_OMIT;
849 case LINUX_UTIME_NOW:
850 times[0].tv_nsec = UTIME_NOW;
853 times[0].tv_nsec = l_times[0].tv_nsec;
856 times[1].tv_sec = l_times[1].tv_sec;
857 switch (l_times[1].tv_nsec)
859 case LINUX_UTIME_OMIT:
860 times[1].tv_nsec = UTIME_OMIT;
862 case LINUX_UTIME_NOW:
863 times[1].tv_nsec = UTIME_NOW;
866 times[1].tv_nsec = l_times[1].tv_nsec;
871 /* This breaks POSIX, but is what the Linux kernel does
872 * _on purpose_ (documented in the man page for utimensat(2)),
873 * so we must follow that behaviour. */
874 if (times[0].tv_nsec == UTIME_OMIT &&
875 times[1].tv_nsec == UTIME_OMIT)
879 if (args->pathname != NULL)
880 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
881 else if (args->flags != 0)
884 if (args->flags & LINUX_AT_SYMLINK_NOFOLLOW)
885 flags |= AT_SYMLINK_NOFOLLOW;
888 error = kern_futimens(td, dfd, timesp, UIO_SYSSPACE);
890 error = kern_utimensat(td, dfd, path, UIO_SYSSPACE, timesp,
891 UIO_SYSSPACE, flags);
898 #ifdef LINUX_LEGACY_SYSCALLS
900 linux_futimesat(struct thread *td, struct linux_futimesat_args *args)
903 struct timeval tv[2], *tvp = NULL;
908 convpath = LUSECONVPATH(td);
909 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
911 LCONVPATHEXIST_AT(td, args->filename, &fname, dfd);
913 if (args->utimes != NULL) {
914 if ((error = copyin(args->utimes, ltv, sizeof ltv))) {
919 tv[0].tv_sec = ltv[0].tv_sec;
920 tv[0].tv_usec = ltv[0].tv_usec;
921 tv[1].tv_sec = ltv[1].tv_sec;
922 tv[1].tv_usec = ltv[1].tv_usec;
927 error = kern_utimesat(td, dfd, args->filename, UIO_USERSPACE,
930 error = kern_utimesat(td, dfd, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
938 linux_common_wait(struct thread *td, int pid, int *statusp,
939 int options, struct __wrusage *wrup)
944 int error, status, tmpstat;
946 if (pid == WAIT_ANY) {
949 } else if (pid < 0) {
958 * For backward compatibility we implicitly add flags WEXITED
961 options |= WEXITED | WTRAPPED;
962 error = kern_wait6(td, idtype, id, &status, options, wrup, &siginfo);
967 tmpstat = status & 0xffff;
968 if (WIFSIGNALED(tmpstat)) {
969 tmpstat = (tmpstat & 0xffffff80) |
970 bsd_to_linux_signal(WTERMSIG(tmpstat));
971 } else if (WIFSTOPPED(tmpstat)) {
972 tmpstat = (tmpstat & 0xffff00ff) |
973 (bsd_to_linux_signal(WSTOPSIG(tmpstat)) << 8);
974 #if defined(__amd64__) && !defined(COMPAT_LINUX32)
975 if (WSTOPSIG(status) == SIGTRAP) {
976 tmpstat = linux_ptrace_status(td,
977 siginfo.si_pid, tmpstat);
980 } else if (WIFCONTINUED(tmpstat)) {
983 error = copyout(&tmpstat, statusp, sizeof(int));
989 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
991 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
993 struct linux_wait4_args wait4_args;
995 wait4_args.pid = args->pid;
996 wait4_args.status = args->status;
997 wait4_args.options = args->options;
998 wait4_args.rusage = NULL;
1000 return (linux_wait4(td, &wait4_args));
1002 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1005 linux_wait4(struct thread *td, struct linux_wait4_args *args)
1008 struct __wrusage wru, *wrup;
1010 if (args->options & ~(LINUX_WUNTRACED | LINUX_WNOHANG |
1011 LINUX_WCONTINUED | __WCLONE | __WNOTHREAD | __WALL))
1015 linux_to_bsd_waitopts(args->options, &options);
1017 if (args->rusage != NULL)
1021 error = linux_common_wait(td, args->pid, args->status, options, wrup);
1024 if (args->rusage != NULL)
1025 error = linux_copyout_rusage(&wru.wru_self, args->rusage);
1030 linux_waitid(struct thread *td, struct linux_waitid_args *args)
1032 int status, options, sig;
1033 struct __wrusage wru;
1041 linux_to_bsd_waitopts(args->options, &options);
1043 if (options & ~(WNOHANG | WNOWAIT | WEXITED | WUNTRACED | WCONTINUED))
1045 if (!(options & (WEXITED | WUNTRACED | WCONTINUED)))
1048 switch (args->idtype) {
1066 error = kern_wait6(td, idtype, args->id, &status, options,
1070 if (args->rusage != NULL) {
1071 error = linux_copyout_rusage(&wru.wru_children,
1076 if (args->info != NULL) {
1078 bzero(&lsi, sizeof(lsi));
1079 if (td->td_retval[0] != 0) {
1080 sig = bsd_to_linux_signal(siginfo.si_signo);
1081 siginfo_to_lsiginfo(&siginfo, &lsi, sig);
1083 error = copyout(&lsi, args->info, sizeof(lsi));
1085 td->td_retval[0] = 0;
1090 #ifdef LINUX_LEGACY_SYSCALLS
1092 linux_mknod(struct thread *td, struct linux_mknod_args *args)
1099 convpath = LUSECONVPATH(td);
1102 seg = UIO_USERSPACE;
1104 LCONVPATHCREAT(td, args->path, &path);
1108 switch (args->mode & S_IFMT) {
1111 error = kern_mkfifoat(td, AT_FDCWD, path, seg,
1117 error = kern_mknodat(td, AT_FDCWD, path, seg,
1118 args->mode, args->dev);
1126 args->mode |= S_IFREG;
1129 error = kern_openat(td, AT_FDCWD, path, seg,
1130 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1132 kern_close(td, td->td_retval[0]);
1146 linux_mknodat(struct thread *td, struct linux_mknodat_args *args)
1153 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1155 convpath = LUSECONVPATH(td);
1157 path = __DECONST(char *, args->filename);
1158 seg = UIO_USERSPACE;
1160 LCONVPATHCREAT_AT(td, args->filename, &path, dfd);
1164 switch (args->mode & S_IFMT) {
1167 error = kern_mkfifoat(td, dfd, path, seg, args->mode);
1172 error = kern_mknodat(td, dfd, path, seg, args->mode,
1181 args->mode |= S_IFREG;
1184 error = kern_openat(td, dfd, path, seg,
1185 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1187 kern_close(td, td->td_retval[0]);
1200 * UGH! This is just about the dumbest idea I've ever heard!!
1203 linux_personality(struct thread *td, struct linux_personality_args *args)
1205 struct linux_pemuldata *pem;
1206 struct proc *p = td->td_proc;
1212 if (args->per != 0xffffffff)
1213 pem->persona = args->per;
1216 td->td_retval[0] = old;
1220 struct l_itimerval {
1221 l_timeval it_interval;
1225 #define B2L_ITIMERVAL(bip, lip) \
1226 (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec; \
1227 (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec; \
1228 (bip)->it_value.tv_sec = (lip)->it_value.tv_sec; \
1229 (bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
1232 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
1235 struct l_itimerval ls;
1236 struct itimerval aitv, oitv;
1238 if (uap->itv == NULL) {
1239 uap->itv = uap->oitv;
1240 return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
1243 error = copyin(uap->itv, &ls, sizeof(ls));
1246 B2L_ITIMERVAL(&aitv, &ls);
1247 error = kern_setitimer(td, uap->which, &aitv, &oitv);
1248 if (error != 0 || uap->oitv == NULL)
1250 B2L_ITIMERVAL(&ls, &oitv);
1252 return (copyout(&ls, uap->oitv, sizeof(ls)));
1256 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
1259 struct l_itimerval ls;
1260 struct itimerval aitv;
1262 error = kern_getitimer(td, uap->which, &aitv);
1265 B2L_ITIMERVAL(&ls, &aitv);
1266 return (copyout(&ls, uap->itv, sizeof(ls)));
1269 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1271 linux_nice(struct thread *td, struct linux_nice_args *args)
1274 return (kern_setpriority(td, PRIO_PROCESS, 0, args->inc));
1276 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1279 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
1281 struct ucred *newcred, *oldcred;
1282 l_gid_t *linux_gidset;
1287 ngrp = args->gidsetsize;
1288 if (ngrp < 0 || ngrp >= ngroups_max + 1)
1290 linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_LINUX, M_WAITOK);
1291 error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
1295 crextend(newcred, ngrp + 1);
1298 oldcred = p->p_ucred;
1299 crcopy(newcred, oldcred);
1302 * cr_groups[0] holds egid. Setting the whole set from
1303 * the supplied set will cause egid to be changed too.
1304 * Keep cr_groups[0] unchanged to prevent that.
1307 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS)) != 0) {
1314 newcred->cr_ngroups = ngrp + 1;
1316 bsd_gidset = newcred->cr_groups;
1319 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1323 newcred->cr_ngroups = 1;
1326 proc_set_cred(p, newcred);
1331 free(linux_gidset, M_LINUX);
1336 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1339 l_gid_t *linux_gidset;
1341 int bsd_gidsetsz, ngrp, error;
1343 cred = td->td_ucred;
1344 bsd_gidset = cred->cr_groups;
1345 bsd_gidsetsz = cred->cr_ngroups - 1;
1348 * cr_groups[0] holds egid. Returning the whole set
1349 * here will cause a duplicate. Exclude cr_groups[0]
1353 if ((ngrp = args->gidsetsize) == 0) {
1354 td->td_retval[0] = bsd_gidsetsz;
1358 if (ngrp < bsd_gidsetsz)
1362 linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset),
1364 while (ngrp < bsd_gidsetsz) {
1365 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1369 error = copyout(linux_gidset, args->grouplist, ngrp * sizeof(l_gid_t));
1370 free(linux_gidset, M_LINUX);
1374 td->td_retval[0] = ngrp;
1379 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1381 struct rlimit bsd_rlim;
1382 struct l_rlimit rlim;
1386 if (args->resource >= LINUX_RLIM_NLIMITS)
1389 which = linux_to_bsd_resource[args->resource];
1393 error = copyin(args->rlim, &rlim, sizeof(rlim));
1397 bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1398 bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1399 return (kern_setrlimit(td, which, &bsd_rlim));
1402 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1404 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1406 struct l_rlimit rlim;
1407 struct rlimit bsd_rlim;
1410 if (args->resource >= LINUX_RLIM_NLIMITS)
1413 which = linux_to_bsd_resource[args->resource];
1417 lim_rlimit(td, which, &bsd_rlim);
1419 #ifdef COMPAT_LINUX32
1420 rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1421 if (rlim.rlim_cur == UINT_MAX)
1422 rlim.rlim_cur = INT_MAX;
1423 rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1424 if (rlim.rlim_max == UINT_MAX)
1425 rlim.rlim_max = INT_MAX;
1427 rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1428 if (rlim.rlim_cur == ULONG_MAX)
1429 rlim.rlim_cur = LONG_MAX;
1430 rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1431 if (rlim.rlim_max == ULONG_MAX)
1432 rlim.rlim_max = LONG_MAX;
1434 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1436 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1439 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1441 struct l_rlimit rlim;
1442 struct rlimit bsd_rlim;
1445 if (args->resource >= LINUX_RLIM_NLIMITS)
1448 which = linux_to_bsd_resource[args->resource];
1452 lim_rlimit(td, which, &bsd_rlim);
1454 rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1455 rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1456 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1460 linux_sched_setscheduler(struct thread *td,
1461 struct linux_sched_setscheduler_args *args)
1463 struct sched_param sched_param;
1467 switch (args->policy) {
1468 case LINUX_SCHED_OTHER:
1469 policy = SCHED_OTHER;
1471 case LINUX_SCHED_FIFO:
1472 policy = SCHED_FIFO;
1474 case LINUX_SCHED_RR:
1481 error = copyin(args->param, &sched_param, sizeof(sched_param));
1485 if (linux_map_sched_prio) {
1488 if (sched_param.sched_priority != 0)
1491 sched_param.sched_priority =
1492 PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE;
1496 if (sched_param.sched_priority < 1 ||
1497 sched_param.sched_priority >= LINUX_MAX_RT_PRIO)
1501 * Map [1, LINUX_MAX_RT_PRIO - 1] to
1502 * [0, RTP_PRIO_MAX - RTP_PRIO_MIN] (rounding down).
1504 sched_param.sched_priority =
1505 (sched_param.sched_priority - 1) *
1506 (RTP_PRIO_MAX - RTP_PRIO_MIN + 1) /
1507 (LINUX_MAX_RT_PRIO - 1);
1512 tdt = linux_tdfind(td, args->pid, -1);
1516 error = kern_sched_setscheduler(td, tdt, policy, &sched_param);
1517 PROC_UNLOCK(tdt->td_proc);
1522 linux_sched_getscheduler(struct thread *td,
1523 struct linux_sched_getscheduler_args *args)
1528 tdt = linux_tdfind(td, args->pid, -1);
1532 error = kern_sched_getscheduler(td, tdt, &policy);
1533 PROC_UNLOCK(tdt->td_proc);
1537 td->td_retval[0] = LINUX_SCHED_OTHER;
1540 td->td_retval[0] = LINUX_SCHED_FIFO;
1543 td->td_retval[0] = LINUX_SCHED_RR;
1550 linux_sched_get_priority_max(struct thread *td,
1551 struct linux_sched_get_priority_max_args *args)
1553 struct sched_get_priority_max_args bsd;
1555 if (linux_map_sched_prio) {
1556 switch (args->policy) {
1557 case LINUX_SCHED_OTHER:
1558 td->td_retval[0] = 0;
1560 case LINUX_SCHED_FIFO:
1561 case LINUX_SCHED_RR:
1562 td->td_retval[0] = LINUX_MAX_RT_PRIO - 1;
1569 switch (args->policy) {
1570 case LINUX_SCHED_OTHER:
1571 bsd.policy = SCHED_OTHER;
1573 case LINUX_SCHED_FIFO:
1574 bsd.policy = SCHED_FIFO;
1576 case LINUX_SCHED_RR:
1577 bsd.policy = SCHED_RR;
1582 return (sys_sched_get_priority_max(td, &bsd));
1586 linux_sched_get_priority_min(struct thread *td,
1587 struct linux_sched_get_priority_min_args *args)
1589 struct sched_get_priority_min_args bsd;
1591 if (linux_map_sched_prio) {
1592 switch (args->policy) {
1593 case LINUX_SCHED_OTHER:
1594 td->td_retval[0] = 0;
1596 case LINUX_SCHED_FIFO:
1597 case LINUX_SCHED_RR:
1598 td->td_retval[0] = 1;
1605 switch (args->policy) {
1606 case LINUX_SCHED_OTHER:
1607 bsd.policy = SCHED_OTHER;
1609 case LINUX_SCHED_FIFO:
1610 bsd.policy = SCHED_FIFO;
1612 case LINUX_SCHED_RR:
1613 bsd.policy = SCHED_RR;
1618 return (sys_sched_get_priority_min(td, &bsd));
1621 #define REBOOT_CAD_ON 0x89abcdef
1622 #define REBOOT_CAD_OFF 0
1623 #define REBOOT_HALT 0xcdef0123
1624 #define REBOOT_RESTART 0x01234567
1625 #define REBOOT_RESTART2 0xA1B2C3D4
1626 #define REBOOT_POWEROFF 0x4321FEDC
1627 #define REBOOT_MAGIC1 0xfee1dead
1628 #define REBOOT_MAGIC2 0x28121969
1629 #define REBOOT_MAGIC2A 0x05121996
1630 #define REBOOT_MAGIC2B 0x16041998
1633 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1635 struct reboot_args bsd_args;
1637 if (args->magic1 != REBOOT_MAGIC1)
1640 switch (args->magic2) {
1642 case REBOOT_MAGIC2A:
1643 case REBOOT_MAGIC2B:
1649 switch (args->cmd) {
1651 case REBOOT_CAD_OFF:
1652 return (priv_check(td, PRIV_REBOOT));
1654 bsd_args.opt = RB_HALT;
1656 case REBOOT_RESTART:
1657 case REBOOT_RESTART2:
1660 case REBOOT_POWEROFF:
1661 bsd_args.opt = RB_POWEROFF;
1666 return (sys_reboot(td, &bsd_args));
1671 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1674 td->td_retval[0] = td->td_proc->p_pid;
1680 linux_gettid(struct thread *td, struct linux_gettid_args *args)
1682 struct linux_emuldata *em;
1685 KASSERT(em != NULL, ("gettid: emuldata not found.\n"));
1687 td->td_retval[0] = em->em_tid;
1694 linux_getppid(struct thread *td, struct linux_getppid_args *args)
1697 td->td_retval[0] = kern_getppid(td);
1702 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1705 td->td_retval[0] = td->td_ucred->cr_rgid;
1710 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1713 td->td_retval[0] = td->td_ucred->cr_ruid;
1718 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1721 return (kern_getsid(td, args->pid));
1725 linux_nosys(struct thread *td, struct nosys_args *ignore)
1732 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1736 error = kern_getpriority(td, args->which, args->who);
1737 td->td_retval[0] = 20 - td->td_retval[0];
1742 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1747 name[1] = KERN_HOSTNAME;
1748 return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1753 linux_setdomainname(struct thread *td, struct linux_setdomainname_args *args)
1758 name[1] = KERN_NISDOMAINNAME;
1759 return (userland_sysctl(td, name, 2, 0, 0, 0, args->name,
1764 linux_exit_group(struct thread *td, struct linux_exit_group_args *args)
1767 LINUX_CTR2(exit_group, "thread(%d) (%d)", td->td_tid,
1771 * XXX: we should send a signal to the parent if
1772 * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?)
1773 * as it doesnt occur often.
1775 exit1(td, args->error_code, 0);
1779 #define _LINUX_CAPABILITY_VERSION_1 0x19980330
1780 #define _LINUX_CAPABILITY_VERSION_2 0x20071026
1781 #define _LINUX_CAPABILITY_VERSION_3 0x20080522
1783 struct l_user_cap_header {
1788 struct l_user_cap_data {
1795 linux_capget(struct thread *td, struct linux_capget_args *uap)
1797 struct l_user_cap_header luch;
1798 struct l_user_cap_data lucd[2];
1801 if (uap->hdrp == NULL)
1804 error = copyin(uap->hdrp, &luch, sizeof(luch));
1808 switch (luch.version) {
1809 case _LINUX_CAPABILITY_VERSION_1:
1812 case _LINUX_CAPABILITY_VERSION_2:
1813 case _LINUX_CAPABILITY_VERSION_3:
1817 luch.version = _LINUX_CAPABILITY_VERSION_1;
1818 error = copyout(&luch, uap->hdrp, sizeof(luch));
1829 * The current implementation doesn't support setting
1830 * a capability (it's essentially a stub) so indicate
1831 * that no capabilities are currently set or available
1834 memset(&lucd, 0, u32s * sizeof(lucd[0]));
1835 error = copyout(&lucd, uap->datap, u32s * sizeof(lucd[0]));
1842 linux_capset(struct thread *td, struct linux_capset_args *uap)
1844 struct l_user_cap_header luch;
1845 struct l_user_cap_data lucd[2];
1848 if (uap->hdrp == NULL || uap->datap == NULL)
1851 error = copyin(uap->hdrp, &luch, sizeof(luch));
1855 switch (luch.version) {
1856 case _LINUX_CAPABILITY_VERSION_1:
1859 case _LINUX_CAPABILITY_VERSION_2:
1860 case _LINUX_CAPABILITY_VERSION_3:
1864 luch.version = _LINUX_CAPABILITY_VERSION_1;
1865 error = copyout(&luch, uap->hdrp, sizeof(luch));
1874 error = copyin(uap->datap, &lucd, u32s * sizeof(lucd[0]));
1878 /* We currently don't support setting any capabilities. */
1879 for (i = 0; i < u32s; i++) {
1880 if (lucd[i].effective || lucd[i].permitted ||
1881 lucd[i].inheritable) {
1883 "capset[%d] effective=0x%x, permitted=0x%x, "
1884 "inheritable=0x%x is not implemented", i,
1885 (int)lucd[i].effective, (int)lucd[i].permitted,
1886 (int)lucd[i].inheritable);
1895 linux_prctl(struct thread *td, struct linux_prctl_args *args)
1897 int error = 0, max_size;
1898 struct proc *p = td->td_proc;
1899 char comm[LINUX_MAX_COMM_LEN];
1902 switch (args->option) {
1903 case LINUX_PR_SET_PDEATHSIG:
1904 if (!LINUX_SIG_VALID(args->arg2))
1906 pdeath_signal = linux_to_bsd_signal(args->arg2);
1907 return (kern_procctl(td, P_PID, 0, PROC_PDEATHSIG_CTL,
1909 case LINUX_PR_GET_PDEATHSIG:
1910 error = kern_procctl(td, P_PID, 0, PROC_PDEATHSIG_STATUS,
1914 pdeath_signal = bsd_to_linux_signal(pdeath_signal);
1915 return (copyout(&pdeath_signal,
1916 (void *)(register_t)args->arg2,
1917 sizeof(pdeath_signal)));
1919 case LINUX_PR_GET_KEEPCAPS:
1921 * Indicate that we always clear the effective and
1922 * permitted capability sets when the user id becomes
1923 * non-zero (actually the capability sets are simply
1924 * always zero in the current implementation).
1926 td->td_retval[0] = 0;
1928 case LINUX_PR_SET_KEEPCAPS:
1930 * Ignore requests to keep the effective and permitted
1931 * capability sets when the user id becomes non-zero.
1934 case LINUX_PR_SET_NAME:
1936 * To be on the safe side we need to make sure to not
1937 * overflow the size a Linux program expects. We already
1938 * do this here in the copyin, so that we don't need to
1941 max_size = MIN(sizeof(comm), sizeof(p->p_comm));
1942 error = copyinstr((void *)(register_t)args->arg2, comm,
1945 /* Linux silently truncates the name if it is too long. */
1946 if (error == ENAMETOOLONG) {
1948 * XXX: copyinstr() isn't documented to populate the
1949 * array completely, so do a copyin() to be on the
1950 * safe side. This should be changed in case
1951 * copyinstr() is changed to guarantee this.
1953 error = copyin((void *)(register_t)args->arg2, comm,
1955 comm[max_size - 1] = '\0';
1961 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
1964 case LINUX_PR_GET_NAME:
1966 strlcpy(comm, p->p_comm, sizeof(comm));
1968 error = copyout(comm, (void *)(register_t)args->arg2,
1980 linux_sched_setparam(struct thread *td,
1981 struct linux_sched_setparam_args *uap)
1983 struct sched_param sched_param;
1987 error = copyin(uap->param, &sched_param, sizeof(sched_param));
1991 tdt = linux_tdfind(td, uap->pid, -1);
1995 if (linux_map_sched_prio) {
1996 error = kern_sched_getscheduler(td, tdt, &policy);
2002 if (sched_param.sched_priority != 0) {
2006 sched_param.sched_priority =
2007 PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE;
2011 if (sched_param.sched_priority < 1 ||
2012 sched_param.sched_priority >= LINUX_MAX_RT_PRIO) {
2017 * Map [1, LINUX_MAX_RT_PRIO - 1] to
2018 * [0, RTP_PRIO_MAX - RTP_PRIO_MIN] (rounding down).
2020 sched_param.sched_priority =
2021 (sched_param.sched_priority - 1) *
2022 (RTP_PRIO_MAX - RTP_PRIO_MIN + 1) /
2023 (LINUX_MAX_RT_PRIO - 1);
2028 error = kern_sched_setparam(td, tdt, &sched_param);
2029 out: PROC_UNLOCK(tdt->td_proc);
2034 linux_sched_getparam(struct thread *td,
2035 struct linux_sched_getparam_args *uap)
2037 struct sched_param sched_param;
2041 tdt = linux_tdfind(td, uap->pid, -1);
2045 error = kern_sched_getparam(td, tdt, &sched_param);
2047 PROC_UNLOCK(tdt->td_proc);
2051 if (linux_map_sched_prio) {
2052 error = kern_sched_getscheduler(td, tdt, &policy);
2053 PROC_UNLOCK(tdt->td_proc);
2059 sched_param.sched_priority = 0;
2064 * Map [0, RTP_PRIO_MAX - RTP_PRIO_MIN] to
2065 * [1, LINUX_MAX_RT_PRIO - 1] (rounding up).
2067 sched_param.sched_priority =
2068 (sched_param.sched_priority *
2069 (LINUX_MAX_RT_PRIO - 1) +
2070 (RTP_PRIO_MAX - RTP_PRIO_MIN - 1)) /
2071 (RTP_PRIO_MAX - RTP_PRIO_MIN) + 1;
2075 PROC_UNLOCK(tdt->td_proc);
2077 error = copyout(&sched_param, uap->param, sizeof(sched_param));
2082 * Get affinity of a process.
2085 linux_sched_getaffinity(struct thread *td,
2086 struct linux_sched_getaffinity_args *args)
2091 if (args->len < sizeof(cpuset_t))
2094 tdt = linux_tdfind(td, args->pid, -1);
2098 PROC_UNLOCK(tdt->td_proc);
2100 error = kern_cpuset_getaffinity(td, CPU_LEVEL_WHICH, CPU_WHICH_TID,
2101 tdt->td_tid, sizeof(cpuset_t), (cpuset_t *)args->user_mask_ptr);
2103 td->td_retval[0] = sizeof(cpuset_t);
2109 * Set affinity of a process.
2112 linux_sched_setaffinity(struct thread *td,
2113 struct linux_sched_setaffinity_args *args)
2117 if (args->len < sizeof(cpuset_t))
2120 tdt = linux_tdfind(td, args->pid, -1);
2124 PROC_UNLOCK(tdt->td_proc);
2126 return (kern_cpuset_setaffinity(td, CPU_LEVEL_WHICH, CPU_WHICH_TID,
2127 tdt->td_tid, sizeof(cpuset_t), (cpuset_t *) args->user_mask_ptr));
2130 struct linux_rlimit64 {
2136 linux_prlimit64(struct thread *td, struct linux_prlimit64_args *args)
2138 struct rlimit rlim, nrlim;
2139 struct linux_rlimit64 lrlim;
2145 if (args->resource >= LINUX_RLIM_NLIMITS)
2148 which = linux_to_bsd_resource[args->resource];
2152 if (args->new != NULL) {
2154 * Note. Unlike FreeBSD where rlim is signed 64-bit Linux
2155 * rlim is unsigned 64-bit. FreeBSD treats negative limits
2156 * as INFINITY so we do not need a conversion even.
2158 error = copyin(args->new, &nrlim, sizeof(nrlim));
2163 flags = PGET_HOLD | PGET_NOTWEXIT;
2164 if (args->new != NULL)
2165 flags |= PGET_CANDEBUG;
2167 flags |= PGET_CANSEE;
2168 if (args->pid == 0) {
2172 error = pget(args->pid, flags, &p);
2176 if (args->old != NULL) {
2178 lim_rlimit_proc(p, which, &rlim);
2180 if (rlim.rlim_cur == RLIM_INFINITY)
2181 lrlim.rlim_cur = LINUX_RLIM_INFINITY;
2183 lrlim.rlim_cur = rlim.rlim_cur;
2184 if (rlim.rlim_max == RLIM_INFINITY)
2185 lrlim.rlim_max = LINUX_RLIM_INFINITY;
2187 lrlim.rlim_max = rlim.rlim_max;
2188 error = copyout(&lrlim, args->old, sizeof(lrlim));
2193 if (args->new != NULL)
2194 error = kern_proc_setrlimit(td, p, which, &nrlim);
2202 linux_pselect6(struct thread *td, struct linux_pselect6_args *args)
2204 struct timeval utv, tv0, tv1, *tvp;
2205 struct l_pselect6arg lpse6;
2206 struct l_timespec lts;
2207 struct timespec uts;
2214 if (args->sig != NULL) {
2215 error = copyin(args->sig, &lpse6, sizeof(lpse6));
2218 if (lpse6.ss_len != sizeof(l_ss))
2220 if (lpse6.ss != 0) {
2221 error = copyin(PTRIN(lpse6.ss), &l_ss,
2225 linux_to_bsd_sigset(&l_ss, &ss);
2231 * Currently glibc changes nanosecond number to microsecond.
2232 * This mean losing precision but for now it is hardly seen.
2234 if (args->tsp != NULL) {
2235 error = copyin(args->tsp, <s, sizeof(lts));
2238 error = linux_to_native_timespec(&uts, <s);
2242 TIMESPEC_TO_TIMEVAL(&utv, &uts);
2243 if (itimerfix(&utv))
2251 error = kern_pselect(td, args->nfds, args->readfds, args->writefds,
2252 args->exceptfds, tvp, ssp, LINUX_NFDBITS);
2254 if (error == 0 && args->tsp != NULL) {
2255 if (td->td_retval[0] != 0) {
2257 * Compute how much time was left of the timeout,
2258 * by subtracting the current time and the time
2259 * before we started the call, and subtracting
2260 * that result from the user-supplied value.
2264 timevalsub(&tv1, &tv0);
2265 timevalsub(&utv, &tv1);
2271 TIMEVAL_TO_TIMESPEC(&utv, &uts);
2273 error = native_to_linux_timespec(<s, &uts);
2275 error = copyout(<s, args->tsp, sizeof(lts));
2282 linux_ppoll(struct thread *td, struct linux_ppoll_args *args)
2284 struct timespec ts0, ts1;
2285 struct l_timespec lts;
2286 struct timespec uts, *tsp;
2292 if (args->sset != NULL) {
2293 if (args->ssize != sizeof(l_ss))
2295 error = copyin(args->sset, &l_ss, sizeof(l_ss));
2298 linux_to_bsd_sigset(&l_ss, &ss);
2302 if (args->tsp != NULL) {
2303 error = copyin(args->tsp, <s, sizeof(lts));
2306 error = linux_to_native_timespec(&uts, <s);
2315 error = kern_poll(td, args->fds, args->nfds, tsp, ssp);
2317 if (error == 0 && args->tsp != NULL) {
2318 if (td->td_retval[0]) {
2320 timespecsub(&ts1, &ts0, &ts1);
2321 timespecsub(&uts, &ts1, &uts);
2323 timespecclear(&uts);
2325 timespecclear(&uts);
2327 error = native_to_linux_timespec(<s, &uts);
2329 error = copyout(<s, args->tsp, sizeof(lts));
2336 linux_sched_rr_get_interval(struct thread *td,
2337 struct linux_sched_rr_get_interval_args *uap)
2340 struct l_timespec lts;
2345 * According to man in case the invalid pid specified
2346 * EINVAL should be returned.
2351 tdt = linux_tdfind(td, uap->pid, -1);
2355 error = kern_sched_rr_get_interval_td(td, tdt, &ts);
2356 PROC_UNLOCK(tdt->td_proc);
2359 error = native_to_linux_timespec(<s, &ts);
2362 return (copyout(<s, uap->interval, sizeof(lts)));
2366 * In case when the Linux thread is the initial thread in
2367 * the thread group thread id is equal to the process id.
2368 * Glibc depends on this magic (assert in pthread_getattr_np.c).
2371 linux_tdfind(struct thread *td, lwpid_t tid, pid_t pid)
2373 struct linux_emuldata *em;
2378 if (tid == 0 || tid == td->td_tid) {
2380 PROC_LOCK(tdt->td_proc);
2381 } else if (tid > PID_MAX)
2382 tdt = tdfind(tid, pid);
2385 * Initial thread where the tid equal to the pid.
2389 if (SV_PROC_ABI(p) != SV_ABI_LINUX) {
2391 * p is not a Linuxulator process.
2396 FOREACH_THREAD_IN_PROC(p, tdt) {
2398 if (tid == em->em_tid)
2410 linux_to_bsd_waitopts(int options, int *bsdopts)
2413 if (options & LINUX_WNOHANG)
2414 *bsdopts |= WNOHANG;
2415 if (options & LINUX_WUNTRACED)
2416 *bsdopts |= WUNTRACED;
2417 if (options & LINUX_WEXITED)
2418 *bsdopts |= WEXITED;
2419 if (options & LINUX_WCONTINUED)
2420 *bsdopts |= WCONTINUED;
2421 if (options & LINUX_WNOWAIT)
2422 *bsdopts |= WNOWAIT;
2424 if (options & __WCLONE)
2425 *bsdopts |= WLINUXCLONE;
2429 linux_getrandom(struct thread *td, struct linux_getrandom_args *args)
2435 if (args->flags & ~(LINUX_GRND_NONBLOCK|LINUX_GRND_RANDOM))
2437 if (args->count > INT_MAX)
2438 args->count = INT_MAX;
2440 iov.iov_base = args->buf;
2441 iov.iov_len = args->count;
2445 uio.uio_resid = iov.iov_len;
2446 uio.uio_segflg = UIO_USERSPACE;
2447 uio.uio_rw = UIO_READ;
2450 error = read_random_uio(&uio, args->flags & LINUX_GRND_NONBLOCK);
2452 td->td_retval[0] = args->count - uio.uio_resid;
2457 linux_mincore(struct thread *td, struct linux_mincore_args *args)
2460 /* Needs to be page-aligned */
2461 if (args->start & PAGE_MASK)
2463 return (kern_mincore(td, args->start, args->len, args->vec));
2466 #define SYSLOG_TAG "<6>"
2469 linux_syslog(struct thread *td, struct linux_syslog_args *args)
2471 char buf[128], *src, *dst;
2475 if (args->type != LINUX_SYSLOG_ACTION_READ_ALL) {
2476 linux_msg(td, "syslog unsupported type 0x%x", args->type);
2480 if (args->len < 6) {
2481 td->td_retval[0] = 0;
2485 error = priv_check(td, PRIV_MSGBUF);
2489 mtx_lock(&msgbuf_lock);
2490 msgbuf_peekbytes(msgbufp, NULL, 0, &seq);
2491 mtx_unlock(&msgbuf_lock);
2494 error = copyout(&SYSLOG_TAG, dst, sizeof(SYSLOG_TAG));
2495 /* The -1 is to skip the trailing '\0'. */
2496 dst += sizeof(SYSLOG_TAG) - 1;
2498 while (error == 0) {
2499 mtx_lock(&msgbuf_lock);
2500 buflen = msgbuf_peekbytes(msgbufp, buf, sizeof(buf), &seq);
2501 mtx_unlock(&msgbuf_lock);
2506 for (src = buf; src < buf + buflen && error == 0; src++) {
2510 if (dst >= args->buf + args->len)
2513 error = copyout(src, dst, 1);
2516 if (*src == '\n' && *(src + 1) != '<' &&
2517 dst + sizeof(SYSLOG_TAG) < args->buf + args->len) {
2518 error = copyout(&SYSLOG_TAG,
2519 dst, sizeof(SYSLOG_TAG));
2520 dst += sizeof(SYSLOG_TAG) - 1;
2525 td->td_retval[0] = dst - args->buf;
2530 linux_getcpu(struct thread *td, struct linux_getcpu_args *args)
2532 int cpu, error, node;
2534 cpu = td->td_oncpu; /* Make sure it doesn't change during copyout(9) */
2536 node = cpuid_to_pcpu[cpu]->pc_domain;
2538 if (args->cpu != NULL)
2539 error = copyout(&cpu, args->cpu, sizeof(l_int));
2540 if (args->node != NULL)
2541 error = copyout(&node, args->node, sizeof(l_int));