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>
56 #include <sys/procctl.h>
57 #include <sys/reboot.h>
58 #include <sys/racct.h>
59 #include <sys/random.h>
60 #include <sys/resourcevar.h>
61 #include <sys/sched.h>
63 #include <sys/signalvar.h>
65 #include <sys/syscallsubr.h>
66 #include <sys/sysctl.h>
67 #include <sys/sysproto.h>
68 #include <sys/systm.h>
70 #include <sys/vmmeter.h>
71 #include <sys/vnode.h>
73 #include <sys/cpuset.h>
76 #include <security/mac/mac_framework.h>
80 #include <vm/vm_kern.h>
81 #include <vm/vm_map.h>
82 #include <vm/vm_extern.h>
83 #include <vm/swap_pager.h>
86 #include <machine/../linux32/linux.h>
87 #include <machine/../linux32/linux32_proto.h>
89 #include <machine/../linux/linux.h>
90 #include <machine/../linux/linux_proto.h>
93 #include <compat/linux/linux_common.h>
94 #include <compat/linux/linux_dtrace.h>
95 #include <compat/linux/linux_file.h>
96 #include <compat/linux/linux_mib.h>
97 #include <compat/linux/linux_signal.h>
98 #include <compat/linux/linux_timer.h>
99 #include <compat/linux/linux_util.h>
100 #include <compat/linux/linux_sysproto.h>
101 #include <compat/linux/linux_emul.h>
102 #include <compat/linux/linux_misc.h>
104 int stclohz; /* Statistics clock frequency */
106 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
107 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
108 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
109 RLIMIT_MEMLOCK, RLIMIT_AS
113 l_long uptime; /* Seconds since boot */
114 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */
115 #define LINUX_SYSINFO_LOADS_SCALE 65536
116 l_ulong totalram; /* Total usable main memory size */
117 l_ulong freeram; /* Available memory size */
118 l_ulong sharedram; /* Amount of shared memory */
119 l_ulong bufferram; /* Memory used by buffers */
120 l_ulong totalswap; /* Total swap space size */
121 l_ulong freeswap; /* swap space still available */
122 l_ushort procs; /* Number of current processes */
127 char _f[20-2*sizeof(l_long)-sizeof(l_int)]; /* padding */
130 struct l_pselect6arg {
135 static int linux_utimensat_lts_to_ts(struct l_timespec *,
137 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
138 static int linux_utimensat_lts64_to_ts(struct l_timespec64 *,
141 static int linux_common_utimensat(struct thread *, int,
142 const char *, struct timespec *, int);
143 static int linux_common_pselect6(struct thread *, l_int,
144 l_fd_set *, l_fd_set *, l_fd_set *,
145 struct timespec *, l_uintptr_t *);
146 static int linux_common_ppoll(struct thread *, struct pollfd *,
147 uint32_t, struct timespec *, l_sigset_t *,
149 static int linux_pollin(struct thread *, struct pollfd *,
150 struct pollfd *, u_int);
151 static int linux_pollout(struct thread *, struct pollfd *,
152 struct pollfd *, u_int);
155 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args)
157 struct l_sysinfo sysinfo;
161 bzero(&sysinfo, sizeof(sysinfo));
165 sysinfo.uptime = ts.tv_sec;
167 /* Use the information from the mib to get our load averages */
168 for (i = 0; i < 3; i++)
169 sysinfo.loads[i] = averunnable.ldavg[i] *
170 LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale;
172 sysinfo.totalram = physmem * PAGE_SIZE;
173 sysinfo.freeram = (u_long)vm_free_count() * PAGE_SIZE;
176 * sharedram counts pages allocated to named, swap-backed objects such
177 * as shared memory segments and tmpfs files. There is no cheap way to
178 * compute this, so just leave the field unpopulated. Linux itself only
179 * started setting this field in the 3.x timeframe.
181 sysinfo.sharedram = 0;
182 sysinfo.bufferram = 0;
184 swap_pager_status(&i, &j);
185 sysinfo.totalswap = i * PAGE_SIZE;
186 sysinfo.freeswap = (i - j) * PAGE_SIZE;
188 sysinfo.procs = nprocs;
191 * Platforms supported by the emulation layer do not have a notion of
194 sysinfo.totalhigh = 0;
195 sysinfo.freehigh = 0;
197 sysinfo.mem_unit = 1;
199 return (copyout(&sysinfo, args->info, sizeof(sysinfo)));
202 #ifdef LINUX_LEGACY_SYSCALLS
204 linux_alarm(struct thread *td, struct linux_alarm_args *args)
206 struct itimerval it, old_it;
208 int error __diagused;
212 * Linux alarm() is always successful. Limit secs to INT32_MAX / 2
213 * to match kern_setitimer()'s limit to avoid error from it.
215 * XXX. Linux limit secs to INT_MAX on 32 and does not limit on 64-bit
218 if (secs > INT32_MAX / 2)
219 secs = INT32_MAX / 2;
221 it.it_value.tv_sec = secs;
222 it.it_value.tv_usec = 0;
223 timevalclear(&it.it_interval);
224 error = kern_setitimer(td, ITIMER_REAL, &it, &old_it);
225 KASSERT(error == 0, ("kern_setitimer returns %d", error));
227 if ((old_it.it_value.tv_sec == 0 && old_it.it_value.tv_usec > 0) ||
228 old_it.it_value.tv_usec >= 500000)
229 old_it.it_value.tv_sec++;
230 td->td_retval[0] = old_it.it_value.tv_sec;
236 linux_brk(struct thread *td, struct linux_brk_args *args)
238 struct vmspace *vm = td->td_proc->p_vmspace;
241 old = (uintptr_t)vm->vm_daddr + ctob(vm->vm_dsize);
242 new = (uintptr_t)args->dsend;
243 if ((caddr_t)new > vm->vm_daddr && !kern_break(td, &new))
244 td->td_retval[0] = (register_t)new;
246 td->td_retval[0] = (register_t)old;
251 #if defined(__i386__)
252 /* XXX: what about amd64/linux32? */
255 linux_uselib(struct thread *td, struct linux_uselib_args *args)
261 vm_map_entry_t entry;
264 unsigned long file_offset;
265 unsigned long bss_size;
269 bool locked, opened, textset;
277 if (!LUSECONVPATH(td)) {
278 NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | AUDITVNODE1,
279 UIO_USERSPACE, args->library, td);
282 LCONVPATHEXIST(args->library, &library);
283 NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | AUDITVNODE1,
284 UIO_SYSSPACE, library, td);
292 NDFREE(&ni, NDF_ONLY_PNBUF);
295 * From here on down, we have a locked vnode that must be unlocked.
296 * XXX: The code below largely duplicates exec_check_permissions().
301 error = VOP_GETATTR(vp, &attr, td->td_ucred);
305 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
306 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
307 /* EACCESS is what exec(2) returns. */
313 if (attr.va_size == 0) {
318 /* Can we access it? */
319 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
324 * XXX: This should use vn_open() so that it is properly authorized,
325 * and to reduce code redundancy all over the place here.
326 * XXX: Not really, it duplicates far more of exec_check_permissions()
330 error = mac_vnode_check_open(td->td_ucred, vp, VREAD);
334 error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL);
339 /* Pull in executable header into exec_map */
340 error = vm_mmap(exec_map, (vm_offset_t *)&a_out, PAGE_SIZE,
341 VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 0);
345 /* Is it a Linux binary ? */
346 if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
352 * While we are here, we should REALLY do some more checks
355 /* Set file/virtual offset based on a.out variant. */
356 switch ((int)(a_out->a_magic & 0xffff)) {
357 case 0413: /* ZMAGIC */
360 case 0314: /* QMAGIC */
368 bss_size = round_page(a_out->a_bss);
370 /* Check various fields in header for validity/bounds. */
371 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
376 /* text + data can't exceed file size */
377 if (a_out->a_data + a_out->a_text > attr.va_size) {
383 * text/data/bss must not exceed limits
384 * XXX - this is not complete. it should check current usage PLUS
385 * the resources needed by this library.
387 PROC_LOCK(td->td_proc);
388 if (a_out->a_text > maxtsiz ||
389 a_out->a_data + bss_size > lim_cur_proc(td->td_proc, RLIMIT_DATA) ||
390 racct_set(td->td_proc, RACCT_DATA, a_out->a_data +
392 PROC_UNLOCK(td->td_proc);
396 PROC_UNLOCK(td->td_proc);
399 * Prevent more writers.
401 error = VOP_SET_TEXT(vp);
407 * Lock no longer needed
413 * Check if file_offset page aligned. Currently we cannot handle
414 * misalinged file offsets, and so we read in the entire image
417 if (file_offset & PAGE_MASK) {
418 /* Map text+data read/write/execute */
420 /* a_entry is the load address and is page aligned */
421 vmaddr = trunc_page(a_out->a_entry);
423 /* get anon user mapping, read+write+execute */
424 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
425 &vmaddr, a_out->a_text + a_out->a_data, 0, VMFS_NO_SPACE,
426 VM_PROT_ALL, VM_PROT_ALL, 0);
430 error = vn_rdwr(UIO_READ, vp, (void *)vmaddr, file_offset,
431 a_out->a_text + a_out->a_data, UIO_USERSPACE, 0,
432 td->td_ucred, NOCRED, &aresid, td);
441 * for QMAGIC, a_entry is 20 bytes beyond the load address
442 * to skip the executable header
444 vmaddr = trunc_page(a_out->a_entry);
447 * Map it all into the process's space as a single
448 * copy-on-write "data" segment.
450 map = &td->td_proc->p_vmspace->vm_map;
451 error = vm_mmap(map, &vmaddr,
452 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
453 MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset);
457 if (!vm_map_lookup_entry(map, vmaddr, &entry)) {
462 entry->eflags |= MAP_ENTRY_VN_EXEC;
468 /* Calculate BSS start address */
469 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
472 /* allocate some 'anon' space */
473 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
474 &vmaddr, bss_size, 0, VMFS_NO_SPACE, VM_PROT_ALL,
485 VOP_CLOSE(vp, FREAD, td->td_ucred, td);
490 VOP_LOCK(vp, LK_SHARED | LK_RETRY);
492 VOP_UNSET_TEXT_CHECKED(vp);
497 /* Release the temporary mapping. */
499 kmap_free_wakeup(exec_map, (vm_offset_t)a_out, PAGE_SIZE);
504 #endif /* __i386__ */
506 #ifdef LINUX_LEGACY_SYSCALLS
508 linux_select(struct thread *td, struct linux_select_args *args)
511 struct timeval tv0, tv1, utv, *tvp;
515 * Store current time for computation of the amount of
519 if ((error = copyin(args->timeout, <v, sizeof(ltv))))
521 utv.tv_sec = ltv.tv_sec;
522 utv.tv_usec = ltv.tv_usec;
524 if (itimerfix(&utv)) {
526 * The timeval was invalid. Convert it to something
527 * valid that will act as it does under Linux.
529 utv.tv_sec += utv.tv_usec / 1000000;
530 utv.tv_usec %= 1000000;
531 if (utv.tv_usec < 0) {
533 utv.tv_usec += 1000000;
543 error = kern_select(td, args->nfds, args->readfds, args->writefds,
544 args->exceptfds, tvp, LINUX_NFDBITS);
549 if (td->td_retval[0]) {
551 * Compute how much time was left of the timeout,
552 * by subtracting the current time and the time
553 * before we started the call, and subtracting
554 * that result from the user-supplied value.
557 timevalsub(&tv1, &tv0);
558 timevalsub(&utv, &tv1);
563 ltv.tv_sec = utv.tv_sec;
564 ltv.tv_usec = utv.tv_usec;
565 if ((error = copyout(<v, args->timeout, sizeof(ltv))))
575 linux_mremap(struct thread *td, struct linux_mremap_args *args)
581 if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) {
582 td->td_retval[0] = 0;
587 * Check for the page alignment.
588 * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK.
590 if (args->addr & PAGE_MASK) {
591 td->td_retval[0] = 0;
595 args->new_len = round_page(args->new_len);
596 args->old_len = round_page(args->old_len);
598 if (args->new_len > args->old_len) {
599 td->td_retval[0] = 0;
603 if (args->new_len < args->old_len) {
604 addr = args->addr + args->new_len;
605 len = args->old_len - args->new_len;
606 error = kern_munmap(td, addr, len);
609 td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
613 #define LINUX_MS_ASYNC 0x0001
614 #define LINUX_MS_INVALIDATE 0x0002
615 #define LINUX_MS_SYNC 0x0004
618 linux_msync(struct thread *td, struct linux_msync_args *args)
621 return (kern_msync(td, args->addr, args->len,
622 args->fl & ~LINUX_MS_SYNC));
625 #ifdef LINUX_LEGACY_SYSCALLS
627 linux_time(struct thread *td, struct linux_time_args *args)
635 if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
637 td->td_retval[0] = tm;
642 struct l_times_argv {
645 l_clock_t tms_cutime;
646 l_clock_t tms_cstime;
650 * Glibc versions prior to 2.2.1 always use hard-coded CLK_TCK value.
651 * Since 2.2.1 Glibc uses value exported from kernel via AT_CLKTCK
652 * auxiliary vector entry.
656 #define CONVOTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
657 #define CONVNTCK(r) (r.tv_sec * stclohz + r.tv_usec / (1000000 / stclohz))
659 #define CONVTCK(r) (linux_kernver(td) >= LINUX_KERNVER_2004000 ? \
660 CONVNTCK(r) : CONVOTCK(r))
663 linux_times(struct thread *td, struct linux_times_args *args)
665 struct timeval tv, utime, stime, cutime, cstime;
666 struct l_times_argv tms;
670 if (args->buf != NULL) {
674 calcru(p, &utime, &stime);
676 calccru(p, &cutime, &cstime);
679 tms.tms_utime = CONVTCK(utime);
680 tms.tms_stime = CONVTCK(stime);
682 tms.tms_cutime = CONVTCK(cutime);
683 tms.tms_cstime = CONVTCK(cstime);
685 if ((error = copyout(&tms, args->buf, sizeof(tms))))
690 td->td_retval[0] = (int)CONVTCK(tv);
695 linux_newuname(struct thread *td, struct linux_newuname_args *args)
697 struct l_new_utsname utsname;
698 char osname[LINUX_MAX_UTSNAME];
699 char osrelease[LINUX_MAX_UTSNAME];
702 linux_get_osname(td, osname);
703 linux_get_osrelease(td, osrelease);
705 bzero(&utsname, sizeof(utsname));
706 strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
707 getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
708 getcreddomainname(td->td_ucred, utsname.domainname, LINUX_MAX_UTSNAME);
709 strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
710 strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
711 for (p = utsname.version; *p != '\0'; ++p)
716 #if defined(__amd64__)
718 * On amd64, Linux uname(2) needs to return "x86_64"
719 * for both 64-bit and 32-bit applications. On 32-bit,
720 * the string returned by getauxval(AT_PLATFORM) needs
721 * to remain "i686", though.
723 #if defined(COMPAT_LINUX32)
724 if (linux32_emulate_i386)
725 strlcpy(utsname.machine, "i686", LINUX_MAX_UTSNAME);
728 strlcpy(utsname.machine, "x86_64", LINUX_MAX_UTSNAME);
729 #elif defined(__aarch64__)
730 strlcpy(utsname.machine, "aarch64", LINUX_MAX_UTSNAME);
731 #elif defined(__i386__)
732 strlcpy(utsname.machine, "i686", LINUX_MAX_UTSNAME);
735 return (copyout(&utsname, args->buf, sizeof(utsname)));
743 #ifdef LINUX_LEGACY_SYSCALLS
745 linux_utime(struct thread *td, struct linux_utime_args *args)
747 struct timeval tv[2], *tvp;
748 struct l_utimbuf lut;
753 if ((error = copyin(args->times, &lut, sizeof lut)) != 0)
755 tv[0].tv_sec = lut.l_actime;
757 tv[1].tv_sec = lut.l_modtime;
763 if (!LUSECONVPATH(td)) {
764 error = kern_utimesat(td, AT_FDCWD, args->fname, UIO_USERSPACE,
767 LCONVPATHEXIST(args->fname, &fname);
768 error = kern_utimesat(td, AT_FDCWD, fname, UIO_SYSSPACE, tvp,
776 #ifdef LINUX_LEGACY_SYSCALLS
778 linux_utimes(struct thread *td, struct linux_utimes_args *args)
781 struct timeval tv[2], *tvp = NULL;
785 if (args->tptr != NULL) {
786 if ((error = copyin(args->tptr, ltv, sizeof ltv)) != 0)
788 tv[0].tv_sec = ltv[0].tv_sec;
789 tv[0].tv_usec = ltv[0].tv_usec;
790 tv[1].tv_sec = ltv[1].tv_sec;
791 tv[1].tv_usec = ltv[1].tv_usec;
795 if (!LUSECONVPATH(td)) {
796 error = kern_utimesat(td, AT_FDCWD, args->fname, UIO_USERSPACE,
799 LCONVPATHEXIST(args->fname, &fname);
800 error = kern_utimesat(td, AT_FDCWD, fname, UIO_SYSSPACE,
809 linux_utimensat_lts_to_ts(struct l_timespec *l_times, struct timespec *times)
812 if (l_times->tv_nsec != LINUX_UTIME_OMIT &&
813 l_times->tv_nsec != LINUX_UTIME_NOW &&
814 (l_times->tv_nsec < 0 || l_times->tv_nsec > 999999999))
817 times->tv_sec = l_times->tv_sec;
818 switch (l_times->tv_nsec)
820 case LINUX_UTIME_OMIT:
821 times->tv_nsec = UTIME_OMIT;
823 case LINUX_UTIME_NOW:
824 times->tv_nsec = UTIME_NOW;
827 times->tv_nsec = l_times->tv_nsec;
834 linux_common_utimensat(struct thread *td, int ldfd, const char *pathname,
835 struct timespec *timesp, int lflags)
838 int error, dfd, flags = 0;
840 dfd = (ldfd == LINUX_AT_FDCWD) ? AT_FDCWD : ldfd;
842 if (lflags & ~(LINUX_AT_SYMLINK_NOFOLLOW | LINUX_AT_EMPTY_PATH))
845 if (timesp != NULL) {
846 /* This breaks POSIX, but is what the Linux kernel does
847 * _on purpose_ (documented in the man page for utimensat(2)),
848 * so we must follow that behaviour. */
849 if (timesp[0].tv_nsec == UTIME_OMIT &&
850 timesp[1].tv_nsec == UTIME_OMIT)
854 if (lflags & LINUX_AT_SYMLINK_NOFOLLOW)
855 flags |= AT_SYMLINK_NOFOLLOW;
856 if (lflags & LINUX_AT_EMPTY_PATH)
857 flags |= AT_EMPTY_PATH;
859 if (!LUSECONVPATH(td)) {
860 if (pathname != NULL) {
861 return (kern_utimensat(td, dfd, pathname,
862 UIO_USERSPACE, timesp, UIO_SYSSPACE, flags));
866 if (pathname != NULL)
867 LCONVPATHEXIST_AT(pathname, &path, dfd);
868 else if (lflags != 0)
872 error = kern_futimens(td, dfd, timesp, UIO_SYSSPACE);
874 error = kern_utimensat(td, dfd, path, UIO_SYSSPACE, timesp,
875 UIO_SYSSPACE, flags);
883 linux_utimensat(struct thread *td, struct linux_utimensat_args *args)
885 struct l_timespec l_times[2];
886 struct timespec times[2], *timesp;
889 if (args->times != NULL) {
890 error = copyin(args->times, l_times, sizeof(l_times));
894 error = linux_utimensat_lts_to_ts(&l_times[0], ×[0]);
897 error = linux_utimensat_lts_to_ts(&l_times[1], ×[1]);
904 return (linux_common_utimensat(td, args->dfd, args->pathname,
905 timesp, args->flags));
908 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
910 linux_utimensat_lts64_to_ts(struct l_timespec64 *l_times, struct timespec *times)
913 /* Zero out the padding in compat mode. */
914 l_times->tv_nsec &= 0xFFFFFFFFUL;
916 if (l_times->tv_nsec != LINUX_UTIME_OMIT &&
917 l_times->tv_nsec != LINUX_UTIME_NOW &&
918 (l_times->tv_nsec < 0 || l_times->tv_nsec > 999999999))
921 times->tv_sec = l_times->tv_sec;
922 switch (l_times->tv_nsec)
924 case LINUX_UTIME_OMIT:
925 times->tv_nsec = UTIME_OMIT;
927 case LINUX_UTIME_NOW:
928 times->tv_nsec = UTIME_NOW;
931 times->tv_nsec = l_times->tv_nsec;
938 linux_utimensat_time64(struct thread *td, struct linux_utimensat_time64_args *args)
940 struct l_timespec64 l_times[2];
941 struct timespec times[2], *timesp;
944 if (args->times64 != NULL) {
945 error = copyin(args->times64, l_times, sizeof(l_times));
949 error = linux_utimensat_lts64_to_ts(&l_times[0], ×[0]);
952 error = linux_utimensat_lts64_to_ts(&l_times[1], ×[1]);
959 return (linux_common_utimensat(td, args->dfd, args->pathname,
960 timesp, args->flags));
962 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
964 #ifdef LINUX_LEGACY_SYSCALLS
966 linux_futimesat(struct thread *td, struct linux_futimesat_args *args)
969 struct timeval tv[2], *tvp = NULL;
973 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
975 if (args->utimes != NULL) {
976 if ((error = copyin(args->utimes, ltv, sizeof ltv)) != 0)
978 tv[0].tv_sec = ltv[0].tv_sec;
979 tv[0].tv_usec = ltv[0].tv_usec;
980 tv[1].tv_sec = ltv[1].tv_sec;
981 tv[1].tv_usec = ltv[1].tv_usec;
985 if (!LUSECONVPATH(td)) {
986 error = kern_utimesat(td, dfd, args->filename, UIO_USERSPACE,
989 LCONVPATHEXIST_AT(args->filename, &fname, dfd);
990 error = kern_utimesat(td, dfd, fname, UIO_SYSSPACE,
999 linux_common_wait(struct thread *td, idtype_t idtype, int id, int *statusp,
1000 int options, void *rup, l_siginfo_t *infop)
1004 struct __wrusage wru;
1005 int error, status, tmpstat, sig;
1007 error = kern_wait6(td, idtype, id, &status, options,
1008 rup != NULL ? &wru : NULL, &siginfo);
1010 if (error == 0 && statusp) {
1011 tmpstat = status & 0xffff;
1012 if (WIFSIGNALED(tmpstat)) {
1013 tmpstat = (tmpstat & 0xffffff80) |
1014 bsd_to_linux_signal(WTERMSIG(tmpstat));
1015 } else if (WIFSTOPPED(tmpstat)) {
1016 tmpstat = (tmpstat & 0xffff00ff) |
1017 (bsd_to_linux_signal(WSTOPSIG(tmpstat)) << 8);
1018 #if defined(__aarch64__) || (defined(__amd64__) && !defined(COMPAT_LINUX32))
1019 if (WSTOPSIG(status) == SIGTRAP) {
1020 tmpstat = linux_ptrace_status(td,
1021 siginfo.si_pid, tmpstat);
1024 } else if (WIFCONTINUED(tmpstat)) {
1027 error = copyout(&tmpstat, statusp, sizeof(int));
1029 if (error == 0 && rup != NULL)
1030 error = linux_copyout_rusage(&wru.wru_self, rup);
1031 if (error == 0 && infop != NULL && td->td_retval[0] != 0) {
1032 sig = bsd_to_linux_signal(siginfo.si_signo);
1033 siginfo_to_lsiginfo(&siginfo, &lsi, sig);
1034 error = copyout(&lsi, infop, sizeof(lsi));
1040 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1042 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
1044 struct linux_wait4_args wait4_args;
1046 wait4_args.pid = args->pid;
1047 wait4_args.status = args->status;
1048 wait4_args.options = args->options;
1049 wait4_args.rusage = NULL;
1051 return (linux_wait4(td, &wait4_args));
1053 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1056 linux_wait4(struct thread *td, struct linux_wait4_args *args)
1059 int options, id, idtype;
1061 if (args->options & ~(LINUX_WUNTRACED | LINUX_WNOHANG |
1062 LINUX_WCONTINUED | __WCLONE | __WNOTHREAD | __WALL))
1065 /* -INT_MIN is not defined. */
1066 if (args->pid == INT_MIN)
1070 linux_to_bsd_waitopts(args->options, &options);
1073 * For backward compatibility we implicitly add flags WEXITED
1074 * and WTRAPPED here.
1076 options |= WEXITED | WTRAPPED;
1079 * As FreeBSD does not have __WALL option bit analogue explicitly set all
1080 * possible option bits to emulate Linux __WALL wait option bit. The same
1081 * for waitid system call.
1083 if ((args->options & __WALL) != 0)
1084 options |= WUNTRACED | WCONTINUED | WLINUXCLONE;
1086 if (args->pid == WAIT_ANY) {
1089 } else if (args->pid < 0) {
1091 id = (id_t)-args->pid;
1092 } else if (args->pid == 0) {
1100 id = (id_t)args->pid;
1103 return (linux_common_wait(td, idtype, id, args->status, options,
1104 args->rusage, NULL));
1108 linux_waitid(struct thread *td, struct linux_waitid_args *args)
1115 if (args->options & ~(LINUX_WNOHANG | LINUX_WNOWAIT | LINUX_WEXITED |
1116 LINUX_WSTOPPED | LINUX_WCONTINUED | __WCLONE | __WNOTHREAD | __WALL))
1120 linux_to_bsd_waitopts(args->options, &options);
1121 if ((args->options & __WALL) != 0)
1122 options |= WEXITED | WTRAPPED | WUNTRACED |
1123 WCONTINUED | WLINUXCLONE;
1126 switch (args->idtype) {
1136 if (linux_use54(td) && args->id == 0) {
1141 } else if (args->id <= 0)
1146 LINUX_RATELIMIT_MSG("unsupported waitid P_PIDFD idtype");
1152 error = linux_common_wait(td, idtype, id, NULL, options,
1153 args->rusage, args->info);
1154 td->td_retval[0] = 0;
1159 #ifdef LINUX_LEGACY_SYSCALLS
1161 linux_mknod(struct thread *td, struct linux_mknod_args *args)
1168 convpath = LUSECONVPATH(td);
1171 seg = UIO_USERSPACE;
1173 LCONVPATHCREAT(args->path, &path);
1177 switch (args->mode & S_IFMT) {
1180 error = kern_mkfifoat(td, AT_FDCWD, path, seg,
1186 error = kern_mknodat(td, AT_FDCWD, path, seg,
1187 args->mode, args->dev);
1195 args->mode |= S_IFREG;
1198 error = kern_openat(td, AT_FDCWD, path, seg,
1199 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1201 kern_close(td, td->td_retval[0]);
1215 linux_mknodat(struct thread *td, struct linux_mknodat_args *args)
1222 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1224 convpath = LUSECONVPATH(td);
1226 path = __DECONST(char *, args->filename);
1227 seg = UIO_USERSPACE;
1229 LCONVPATHCREAT_AT(args->filename, &path, dfd);
1233 switch (args->mode & S_IFMT) {
1236 error = kern_mkfifoat(td, dfd, path, seg, args->mode);
1241 error = kern_mknodat(td, dfd, path, seg, args->mode,
1250 args->mode |= S_IFREG;
1253 error = kern_openat(td, dfd, path, seg,
1254 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1256 kern_close(td, td->td_retval[0]);
1269 * UGH! This is just about the dumbest idea I've ever heard!!
1272 linux_personality(struct thread *td, struct linux_personality_args *args)
1274 struct linux_pemuldata *pem;
1275 struct proc *p = td->td_proc;
1281 if (args->per != 0xffffffff)
1282 pem->persona = args->per;
1285 td->td_retval[0] = old;
1289 struct l_itimerval {
1290 l_timeval it_interval;
1294 #define B2L_ITIMERVAL(bip, lip) \
1295 (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec; \
1296 (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec; \
1297 (bip)->it_value.tv_sec = (lip)->it_value.tv_sec; \
1298 (bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
1301 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
1304 struct l_itimerval ls;
1305 struct itimerval aitv, oitv;
1307 if (uap->itv == NULL) {
1308 uap->itv = uap->oitv;
1309 return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
1312 error = copyin(uap->itv, &ls, sizeof(ls));
1315 B2L_ITIMERVAL(&aitv, &ls);
1316 error = kern_setitimer(td, uap->which, &aitv, &oitv);
1317 if (error != 0 || uap->oitv == NULL)
1319 B2L_ITIMERVAL(&ls, &oitv);
1321 return (copyout(&ls, uap->oitv, sizeof(ls)));
1325 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
1328 struct l_itimerval ls;
1329 struct itimerval aitv;
1331 error = kern_getitimer(td, uap->which, &aitv);
1334 B2L_ITIMERVAL(&ls, &aitv);
1335 return (copyout(&ls, uap->itv, sizeof(ls)));
1338 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1340 linux_nice(struct thread *td, struct linux_nice_args *args)
1343 return (kern_setpriority(td, PRIO_PROCESS, 0, args->inc));
1345 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1348 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
1350 struct ucred *newcred, *oldcred;
1351 l_gid_t *linux_gidset;
1356 ngrp = args->gidsetsize;
1357 if (ngrp < 0 || ngrp >= ngroups_max + 1)
1359 linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_LINUX, M_WAITOK);
1360 error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
1364 crextend(newcred, ngrp + 1);
1367 oldcred = p->p_ucred;
1368 crcopy(newcred, oldcred);
1371 * cr_groups[0] holds egid. Setting the whole set from
1372 * the supplied set will cause egid to be changed too.
1373 * Keep cr_groups[0] unchanged to prevent that.
1376 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS)) != 0) {
1383 newcred->cr_ngroups = ngrp + 1;
1385 bsd_gidset = newcred->cr_groups;
1388 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1392 newcred->cr_ngroups = 1;
1395 proc_set_cred(p, newcred);
1400 free(linux_gidset, M_LINUX);
1405 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1408 l_gid_t *linux_gidset;
1410 int bsd_gidsetsz, ngrp, error;
1412 cred = td->td_ucred;
1413 bsd_gidset = cred->cr_groups;
1414 bsd_gidsetsz = cred->cr_ngroups - 1;
1417 * cr_groups[0] holds egid. Returning the whole set
1418 * here will cause a duplicate. Exclude cr_groups[0]
1422 if ((ngrp = args->gidsetsize) == 0) {
1423 td->td_retval[0] = bsd_gidsetsz;
1427 if (ngrp < bsd_gidsetsz)
1431 linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset),
1433 while (ngrp < bsd_gidsetsz) {
1434 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1438 error = copyout(linux_gidset, args->grouplist, ngrp * sizeof(l_gid_t));
1439 free(linux_gidset, M_LINUX);
1443 td->td_retval[0] = ngrp;
1448 linux_get_dummy_limit(l_uint resource, struct rlimit *rlim)
1451 if (linux_dummy_rlimits == 0)
1455 case LINUX_RLIMIT_LOCKS:
1456 case LINUX_RLIMIT_SIGPENDING:
1457 case LINUX_RLIMIT_MSGQUEUE:
1458 case LINUX_RLIMIT_RTTIME:
1459 rlim->rlim_cur = LINUX_RLIM_INFINITY;
1460 rlim->rlim_max = LINUX_RLIM_INFINITY;
1462 case LINUX_RLIMIT_NICE:
1463 case LINUX_RLIMIT_RTPRIO:
1473 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1475 struct rlimit bsd_rlim;
1476 struct l_rlimit rlim;
1480 if (args->resource >= LINUX_RLIM_NLIMITS)
1483 which = linux_to_bsd_resource[args->resource];
1487 error = copyin(args->rlim, &rlim, sizeof(rlim));
1491 bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1492 bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1493 return (kern_setrlimit(td, which, &bsd_rlim));
1496 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1498 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1500 struct l_rlimit rlim;
1501 struct rlimit bsd_rlim;
1504 if (linux_get_dummy_limit(args->resource, &bsd_rlim)) {
1505 rlim.rlim_cur = bsd_rlim.rlim_cur;
1506 rlim.rlim_max = bsd_rlim.rlim_max;
1507 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1510 if (args->resource >= LINUX_RLIM_NLIMITS)
1513 which = linux_to_bsd_resource[args->resource];
1517 lim_rlimit(td, which, &bsd_rlim);
1519 #ifdef COMPAT_LINUX32
1520 rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1521 if (rlim.rlim_cur == UINT_MAX)
1522 rlim.rlim_cur = INT_MAX;
1523 rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1524 if (rlim.rlim_max == UINT_MAX)
1525 rlim.rlim_max = INT_MAX;
1527 rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1528 if (rlim.rlim_cur == ULONG_MAX)
1529 rlim.rlim_cur = LONG_MAX;
1530 rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1531 if (rlim.rlim_max == ULONG_MAX)
1532 rlim.rlim_max = LONG_MAX;
1534 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1536 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1539 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1541 struct l_rlimit rlim;
1542 struct rlimit bsd_rlim;
1545 if (linux_get_dummy_limit(args->resource, &bsd_rlim)) {
1546 rlim.rlim_cur = bsd_rlim.rlim_cur;
1547 rlim.rlim_max = bsd_rlim.rlim_max;
1548 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1551 if (args->resource >= LINUX_RLIM_NLIMITS)
1554 which = linux_to_bsd_resource[args->resource];
1558 lim_rlimit(td, which, &bsd_rlim);
1560 rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1561 rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1562 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1566 linux_sched_setscheduler(struct thread *td,
1567 struct linux_sched_setscheduler_args *args)
1569 struct sched_param sched_param;
1573 switch (args->policy) {
1574 case LINUX_SCHED_OTHER:
1575 policy = SCHED_OTHER;
1577 case LINUX_SCHED_FIFO:
1578 policy = SCHED_FIFO;
1580 case LINUX_SCHED_RR:
1587 error = copyin(args->param, &sched_param, sizeof(sched_param));
1591 if (linux_map_sched_prio) {
1594 if (sched_param.sched_priority != 0)
1597 sched_param.sched_priority =
1598 PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE;
1602 if (sched_param.sched_priority < 1 ||
1603 sched_param.sched_priority >= LINUX_MAX_RT_PRIO)
1607 * Map [1, LINUX_MAX_RT_PRIO - 1] to
1608 * [0, RTP_PRIO_MAX - RTP_PRIO_MIN] (rounding down).
1610 sched_param.sched_priority =
1611 (sched_param.sched_priority - 1) *
1612 (RTP_PRIO_MAX - RTP_PRIO_MIN + 1) /
1613 (LINUX_MAX_RT_PRIO - 1);
1618 tdt = linux_tdfind(td, args->pid, -1);
1622 error = kern_sched_setscheduler(td, tdt, policy, &sched_param);
1623 PROC_UNLOCK(tdt->td_proc);
1628 linux_sched_getscheduler(struct thread *td,
1629 struct linux_sched_getscheduler_args *args)
1634 tdt = linux_tdfind(td, args->pid, -1);
1638 error = kern_sched_getscheduler(td, tdt, &policy);
1639 PROC_UNLOCK(tdt->td_proc);
1643 td->td_retval[0] = LINUX_SCHED_OTHER;
1646 td->td_retval[0] = LINUX_SCHED_FIFO;
1649 td->td_retval[0] = LINUX_SCHED_RR;
1656 linux_sched_get_priority_max(struct thread *td,
1657 struct linux_sched_get_priority_max_args *args)
1659 struct sched_get_priority_max_args bsd;
1661 if (linux_map_sched_prio) {
1662 switch (args->policy) {
1663 case LINUX_SCHED_OTHER:
1664 td->td_retval[0] = 0;
1666 case LINUX_SCHED_FIFO:
1667 case LINUX_SCHED_RR:
1668 td->td_retval[0] = LINUX_MAX_RT_PRIO - 1;
1675 switch (args->policy) {
1676 case LINUX_SCHED_OTHER:
1677 bsd.policy = SCHED_OTHER;
1679 case LINUX_SCHED_FIFO:
1680 bsd.policy = SCHED_FIFO;
1682 case LINUX_SCHED_RR:
1683 bsd.policy = SCHED_RR;
1688 return (sys_sched_get_priority_max(td, &bsd));
1692 linux_sched_get_priority_min(struct thread *td,
1693 struct linux_sched_get_priority_min_args *args)
1695 struct sched_get_priority_min_args bsd;
1697 if (linux_map_sched_prio) {
1698 switch (args->policy) {
1699 case LINUX_SCHED_OTHER:
1700 td->td_retval[0] = 0;
1702 case LINUX_SCHED_FIFO:
1703 case LINUX_SCHED_RR:
1704 td->td_retval[0] = 1;
1711 switch (args->policy) {
1712 case LINUX_SCHED_OTHER:
1713 bsd.policy = SCHED_OTHER;
1715 case LINUX_SCHED_FIFO:
1716 bsd.policy = SCHED_FIFO;
1718 case LINUX_SCHED_RR:
1719 bsd.policy = SCHED_RR;
1724 return (sys_sched_get_priority_min(td, &bsd));
1727 #define REBOOT_CAD_ON 0x89abcdef
1728 #define REBOOT_CAD_OFF 0
1729 #define REBOOT_HALT 0xcdef0123
1730 #define REBOOT_RESTART 0x01234567
1731 #define REBOOT_RESTART2 0xA1B2C3D4
1732 #define REBOOT_POWEROFF 0x4321FEDC
1733 #define REBOOT_MAGIC1 0xfee1dead
1734 #define REBOOT_MAGIC2 0x28121969
1735 #define REBOOT_MAGIC2A 0x05121996
1736 #define REBOOT_MAGIC2B 0x16041998
1739 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1741 struct reboot_args bsd_args;
1743 if (args->magic1 != REBOOT_MAGIC1)
1746 switch (args->magic2) {
1748 case REBOOT_MAGIC2A:
1749 case REBOOT_MAGIC2B:
1755 switch (args->cmd) {
1757 case REBOOT_CAD_OFF:
1758 return (priv_check(td, PRIV_REBOOT));
1760 bsd_args.opt = RB_HALT;
1762 case REBOOT_RESTART:
1763 case REBOOT_RESTART2:
1766 case REBOOT_POWEROFF:
1767 bsd_args.opt = RB_POWEROFF;
1772 return (sys_reboot(td, &bsd_args));
1776 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1779 td->td_retval[0] = td->td_proc->p_pid;
1785 linux_gettid(struct thread *td, struct linux_gettid_args *args)
1787 struct linux_emuldata *em;
1790 KASSERT(em != NULL, ("gettid: emuldata not found.\n"));
1792 td->td_retval[0] = em->em_tid;
1798 linux_getppid(struct thread *td, struct linux_getppid_args *args)
1801 td->td_retval[0] = kern_getppid(td);
1806 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1809 td->td_retval[0] = td->td_ucred->cr_rgid;
1814 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1817 td->td_retval[0] = td->td_ucred->cr_ruid;
1822 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1825 return (kern_getsid(td, args->pid));
1829 linux_nosys(struct thread *td, struct nosys_args *ignore)
1836 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1840 error = kern_getpriority(td, args->which, args->who);
1841 td->td_retval[0] = 20 - td->td_retval[0];
1846 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1851 name[1] = KERN_HOSTNAME;
1852 return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1857 linux_setdomainname(struct thread *td, struct linux_setdomainname_args *args)
1862 name[1] = KERN_NISDOMAINNAME;
1863 return (userland_sysctl(td, name, 2, 0, 0, 0, args->name,
1868 linux_exit_group(struct thread *td, struct linux_exit_group_args *args)
1871 LINUX_CTR2(exit_group, "thread(%d) (%d)", td->td_tid,
1875 * XXX: we should send a signal to the parent if
1876 * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?)
1877 * as it doesnt occur often.
1879 exit1(td, args->error_code, 0);
1883 #define _LINUX_CAPABILITY_VERSION_1 0x19980330
1884 #define _LINUX_CAPABILITY_VERSION_2 0x20071026
1885 #define _LINUX_CAPABILITY_VERSION_3 0x20080522
1887 struct l_user_cap_header {
1892 struct l_user_cap_data {
1899 linux_capget(struct thread *td, struct linux_capget_args *uap)
1901 struct l_user_cap_header luch;
1902 struct l_user_cap_data lucd[2];
1905 if (uap->hdrp == NULL)
1908 error = copyin(uap->hdrp, &luch, sizeof(luch));
1912 switch (luch.version) {
1913 case _LINUX_CAPABILITY_VERSION_1:
1916 case _LINUX_CAPABILITY_VERSION_2:
1917 case _LINUX_CAPABILITY_VERSION_3:
1921 luch.version = _LINUX_CAPABILITY_VERSION_1;
1922 error = copyout(&luch, uap->hdrp, sizeof(luch));
1933 * The current implementation doesn't support setting
1934 * a capability (it's essentially a stub) so indicate
1935 * that no capabilities are currently set or available
1938 memset(&lucd, 0, u32s * sizeof(lucd[0]));
1939 error = copyout(&lucd, uap->datap, u32s * sizeof(lucd[0]));
1946 linux_capset(struct thread *td, struct linux_capset_args *uap)
1948 struct l_user_cap_header luch;
1949 struct l_user_cap_data lucd[2];
1952 if (uap->hdrp == NULL || uap->datap == NULL)
1955 error = copyin(uap->hdrp, &luch, sizeof(luch));
1959 switch (luch.version) {
1960 case _LINUX_CAPABILITY_VERSION_1:
1963 case _LINUX_CAPABILITY_VERSION_2:
1964 case _LINUX_CAPABILITY_VERSION_3:
1968 luch.version = _LINUX_CAPABILITY_VERSION_1;
1969 error = copyout(&luch, uap->hdrp, sizeof(luch));
1978 error = copyin(uap->datap, &lucd, u32s * sizeof(lucd[0]));
1982 /* We currently don't support setting any capabilities. */
1983 for (i = 0; i < u32s; i++) {
1984 if (lucd[i].effective || lucd[i].permitted ||
1985 lucd[i].inheritable) {
1987 "capset[%d] effective=0x%x, permitted=0x%x, "
1988 "inheritable=0x%x is not implemented", i,
1989 (int)lucd[i].effective, (int)lucd[i].permitted,
1990 (int)lucd[i].inheritable);
1999 linux_prctl(struct thread *td, struct linux_prctl_args *args)
2001 int error = 0, max_size, arg;
2002 struct proc *p = td->td_proc;
2003 char comm[LINUX_MAX_COMM_LEN];
2004 int pdeath_signal, trace_state;
2006 switch (args->option) {
2007 case LINUX_PR_SET_PDEATHSIG:
2008 if (!LINUX_SIG_VALID(args->arg2))
2010 pdeath_signal = linux_to_bsd_signal(args->arg2);
2011 return (kern_procctl(td, P_PID, 0, PROC_PDEATHSIG_CTL,
2013 case LINUX_PR_GET_PDEATHSIG:
2014 error = kern_procctl(td, P_PID, 0, PROC_PDEATHSIG_STATUS,
2018 pdeath_signal = bsd_to_linux_signal(pdeath_signal);
2019 return (copyout(&pdeath_signal,
2020 (void *)(register_t)args->arg2,
2021 sizeof(pdeath_signal)));
2023 * In Linux, this flag controls if set[gu]id processes can coredump.
2024 * There are additional semantics imposed on processes that cannot
2026 * - Such processes can not be ptraced.
2027 * - There are some semantics around ownership of process-related files
2028 * in the /proc namespace.
2030 * In FreeBSD, we can (and by default, do) disable setuid coredump
2031 * system-wide with 'sugid_coredump.' We control tracability on a
2032 * per-process basis with the procctl PROC_TRACE (=> P2_NOTRACE flag).
2033 * By happy coincidence, P2_NOTRACE also prevents coredumping. So the
2034 * procctl is roughly analogous to Linux's DUMPABLE.
2036 * So, proxy these knobs to the corresponding PROC_TRACE setting.
2038 case LINUX_PR_GET_DUMPABLE:
2039 error = kern_procctl(td, P_PID, p->p_pid, PROC_TRACE_STATUS,
2043 td->td_retval[0] = (trace_state != -1);
2045 case LINUX_PR_SET_DUMPABLE:
2047 * It is only valid for userspace to set one of these two
2048 * flags, and only one at a time.
2050 switch (args->arg2) {
2051 case LINUX_SUID_DUMP_DISABLE:
2052 trace_state = PROC_TRACE_CTL_DISABLE_EXEC;
2054 case LINUX_SUID_DUMP_USER:
2055 trace_state = PROC_TRACE_CTL_ENABLE;
2060 return (kern_procctl(td, P_PID, p->p_pid, PROC_TRACE_CTL,
2062 case LINUX_PR_GET_KEEPCAPS:
2064 * Indicate that we always clear the effective and
2065 * permitted capability sets when the user id becomes
2066 * non-zero (actually the capability sets are simply
2067 * always zero in the current implementation).
2069 td->td_retval[0] = 0;
2071 case LINUX_PR_SET_KEEPCAPS:
2073 * Ignore requests to keep the effective and permitted
2074 * capability sets when the user id becomes non-zero.
2077 case LINUX_PR_SET_NAME:
2079 * To be on the safe side we need to make sure to not
2080 * overflow the size a Linux program expects. We already
2081 * do this here in the copyin, so that we don't need to
2084 max_size = MIN(sizeof(comm), sizeof(p->p_comm));
2085 error = copyinstr((void *)(register_t)args->arg2, comm,
2088 /* Linux silently truncates the name if it is too long. */
2089 if (error == ENAMETOOLONG) {
2091 * XXX: copyinstr() isn't documented to populate the
2092 * array completely, so do a copyin() to be on the
2093 * safe side. This should be changed in case
2094 * copyinstr() is changed to guarantee this.
2096 error = copyin((void *)(register_t)args->arg2, comm,
2098 comm[max_size - 1] = '\0';
2104 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
2107 case LINUX_PR_GET_NAME:
2109 strlcpy(comm, p->p_comm, sizeof(comm));
2111 error = copyout(comm, (void *)(register_t)args->arg2,
2114 case LINUX_PR_GET_SECCOMP:
2115 case LINUX_PR_SET_SECCOMP:
2117 * Same as returned by Linux without CONFIG_SECCOMP enabled.
2121 case LINUX_PR_CAPBSET_READ:
2124 * This makes too much noise with Ubuntu Focal.
2126 linux_msg(td, "unsupported prctl PR_CAPBSET_READ %d",
2131 case LINUX_PR_SET_NO_NEW_PRIVS:
2132 arg = args->arg2 == 1 ?
2133 PROC_NO_NEW_PRIVS_ENABLE : PROC_NO_NEW_PRIVS_DISABLE;
2134 error = kern_procctl(td, P_PID, p->p_pid,
2135 PROC_NO_NEW_PRIVS_CTL, &arg);
2137 case LINUX_PR_SET_PTRACER:
2138 linux_msg(td, "unsupported prctl PR_SET_PTRACER");
2142 linux_msg(td, "unsupported prctl option %d", args->option);
2151 linux_sched_setparam(struct thread *td,
2152 struct linux_sched_setparam_args *uap)
2154 struct sched_param sched_param;
2158 error = copyin(uap->param, &sched_param, sizeof(sched_param));
2162 tdt = linux_tdfind(td, uap->pid, -1);
2166 if (linux_map_sched_prio) {
2167 error = kern_sched_getscheduler(td, tdt, &policy);
2173 if (sched_param.sched_priority != 0) {
2177 sched_param.sched_priority =
2178 PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE;
2182 if (sched_param.sched_priority < 1 ||
2183 sched_param.sched_priority >= LINUX_MAX_RT_PRIO) {
2188 * Map [1, LINUX_MAX_RT_PRIO - 1] to
2189 * [0, RTP_PRIO_MAX - RTP_PRIO_MIN] (rounding down).
2191 sched_param.sched_priority =
2192 (sched_param.sched_priority - 1) *
2193 (RTP_PRIO_MAX - RTP_PRIO_MIN + 1) /
2194 (LINUX_MAX_RT_PRIO - 1);
2199 error = kern_sched_setparam(td, tdt, &sched_param);
2200 out: PROC_UNLOCK(tdt->td_proc);
2205 linux_sched_getparam(struct thread *td,
2206 struct linux_sched_getparam_args *uap)
2208 struct sched_param sched_param;
2212 tdt = linux_tdfind(td, uap->pid, -1);
2216 error = kern_sched_getparam(td, tdt, &sched_param);
2218 PROC_UNLOCK(tdt->td_proc);
2222 if (linux_map_sched_prio) {
2223 error = kern_sched_getscheduler(td, tdt, &policy);
2224 PROC_UNLOCK(tdt->td_proc);
2230 sched_param.sched_priority = 0;
2235 * Map [0, RTP_PRIO_MAX - RTP_PRIO_MIN] to
2236 * [1, LINUX_MAX_RT_PRIO - 1] (rounding up).
2238 sched_param.sched_priority =
2239 (sched_param.sched_priority *
2240 (LINUX_MAX_RT_PRIO - 1) +
2241 (RTP_PRIO_MAX - RTP_PRIO_MIN - 1)) /
2242 (RTP_PRIO_MAX - RTP_PRIO_MIN) + 1;
2246 PROC_UNLOCK(tdt->td_proc);
2248 error = copyout(&sched_param, uap->param, sizeof(sched_param));
2252 static const struct cpuset_copy_cb copy_set = {
2253 .cpuset_copyin = copyin,
2254 .cpuset_copyout = copyout
2258 * Get affinity of a process.
2261 linux_sched_getaffinity(struct thread *td,
2262 struct linux_sched_getaffinity_args *args)
2267 if (args->len < sizeof(cpuset_t))
2270 tdt = linux_tdfind(td, args->pid, -1);
2274 PROC_UNLOCK(tdt->td_proc);
2276 error = kern_cpuset_getaffinity(td, CPU_LEVEL_WHICH, CPU_WHICH_TID,
2277 tdt->td_tid, sizeof(cpuset_t), (cpuset_t *)args->user_mask_ptr,
2280 td->td_retval[0] = sizeof(cpuset_t);
2286 * Set affinity of a process.
2289 linux_sched_setaffinity(struct thread *td,
2290 struct linux_sched_setaffinity_args *args)
2294 if (args->len < sizeof(cpuset_t))
2297 tdt = linux_tdfind(td, args->pid, -1);
2301 PROC_UNLOCK(tdt->td_proc);
2303 return (kern_cpuset_setaffinity(td, CPU_LEVEL_WHICH, CPU_WHICH_TID,
2304 tdt->td_tid, sizeof(cpuset_t), (cpuset_t *) args->user_mask_ptr,
2308 struct linux_rlimit64 {
2314 linux_prlimit64(struct thread *td, struct linux_prlimit64_args *args)
2316 struct rlimit rlim, nrlim;
2317 struct linux_rlimit64 lrlim;
2323 if (args->new == NULL && args->old != NULL) {
2324 if (linux_get_dummy_limit(args->resource, &rlim)) {
2325 lrlim.rlim_cur = rlim.rlim_cur;
2326 lrlim.rlim_max = rlim.rlim_max;
2327 return (copyout(&lrlim, args->old, sizeof(lrlim)));
2331 if (args->resource >= LINUX_RLIM_NLIMITS)
2334 which = linux_to_bsd_resource[args->resource];
2338 if (args->new != NULL) {
2340 * Note. Unlike FreeBSD where rlim is signed 64-bit Linux
2341 * rlim is unsigned 64-bit. FreeBSD treats negative limits
2342 * as INFINITY so we do not need a conversion even.
2344 error = copyin(args->new, &nrlim, sizeof(nrlim));
2349 flags = PGET_HOLD | PGET_NOTWEXIT;
2350 if (args->new != NULL)
2351 flags |= PGET_CANDEBUG;
2353 flags |= PGET_CANSEE;
2354 if (args->pid == 0) {
2358 error = pget(args->pid, flags, &p);
2362 if (args->old != NULL) {
2364 lim_rlimit_proc(p, which, &rlim);
2366 if (rlim.rlim_cur == RLIM_INFINITY)
2367 lrlim.rlim_cur = LINUX_RLIM_INFINITY;
2369 lrlim.rlim_cur = rlim.rlim_cur;
2370 if (rlim.rlim_max == RLIM_INFINITY)
2371 lrlim.rlim_max = LINUX_RLIM_INFINITY;
2373 lrlim.rlim_max = rlim.rlim_max;
2374 error = copyout(&lrlim, args->old, sizeof(lrlim));
2379 if (args->new != NULL)
2380 error = kern_proc_setrlimit(td, p, which, &nrlim);
2388 linux_pselect6(struct thread *td, struct linux_pselect6_args *args)
2390 struct l_timespec lts;
2391 struct timespec ts, *tsp;
2394 if (args->tsp != NULL) {
2395 error = copyin(args->tsp, <s, sizeof(lts));
2398 error = linux_to_native_timespec(&ts, <s);
2405 error = linux_common_pselect6(td, args->nfds, args->readfds,
2406 args->writefds, args->exceptfds, tsp, args->sig);
2408 if (args->tsp != NULL)
2409 linux_put_timespec(&ts, args->tsp);
2414 linux_common_pselect6(struct thread *td, l_int nfds, l_fd_set *readfds,
2415 l_fd_set *writefds, l_fd_set *exceptfds, struct timespec *tsp,
2418 struct timeval utv, tv0, tv1, *tvp;
2419 struct l_pselect6arg lpse6;
2427 error = copyin(sig, &lpse6, sizeof(lpse6));
2430 if (lpse6.ss_len != sizeof(l_ss))
2432 if (lpse6.ss != 0) {
2433 error = copyin(PTRIN(lpse6.ss), &l_ss,
2437 linux_to_bsd_sigset(&l_ss, &ss);
2444 * Currently glibc changes nanosecond number to microsecond.
2445 * This mean losing precision but for now it is hardly seen.
2448 TIMESPEC_TO_TIMEVAL(&utv, tsp);
2449 if (itimerfix(&utv))
2457 error = kern_pselect(td, nfds, readfds, writefds,
2458 exceptfds, tvp, ssp, LINUX_NFDBITS);
2462 * Compute how much time was left of the timeout,
2463 * by subtracting the current time and the time
2464 * before we started the call, and subtracting
2465 * that result from the user-supplied value.
2468 timevalsub(&tv1, &tv0);
2469 timevalsub(&utv, &tv1);
2472 TIMEVAL_TO_TIMESPEC(&utv, tsp);
2477 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
2479 linux_pselect6_time64(struct thread *td,
2480 struct linux_pselect6_time64_args *args)
2482 struct l_timespec64 lts;
2483 struct timespec ts, *tsp;
2486 if (args->tsp != NULL) {
2487 error = copyin(args->tsp, <s, sizeof(lts));
2490 error = linux_to_native_timespec64(&ts, <s);
2497 error = linux_common_pselect6(td, args->nfds, args->readfds,
2498 args->writefds, args->exceptfds, tsp, args->sig);
2500 if (args->tsp != NULL)
2501 linux_put_timespec64(&ts, args->tsp);
2504 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
2507 linux_ppoll(struct thread *td, struct linux_ppoll_args *args)
2509 struct timespec uts, *tsp;
2510 struct l_timespec lts;
2513 if (args->tsp != NULL) {
2514 error = copyin(args->tsp, <s, sizeof(lts));
2517 error = linux_to_native_timespec(&uts, <s);
2524 error = linux_common_ppoll(td, args->fds, args->nfds, tsp,
2525 args->sset, args->ssize);
2526 if (error == 0 && args->tsp != NULL)
2527 error = linux_put_timespec(&uts, args->tsp);
2532 linux_common_ppoll(struct thread *td, struct pollfd *fds, uint32_t nfds,
2533 struct timespec *tsp, l_sigset_t *sset, l_size_t ssize)
2535 struct timespec ts0, ts1;
2536 struct pollfd stackfds[32];
2537 struct pollfd *kfds;
2543 if (kern_poll_maxfds(nfds))
2546 if (ssize != sizeof(l_ss))
2548 error = copyin(sset, &l_ss, sizeof(l_ss));
2551 linux_to_bsd_sigset(&l_ss, &ss);
2558 if (nfds > nitems(stackfds))
2559 kfds = mallocarray(nfds, sizeof(*kfds), M_TEMP, M_WAITOK);
2562 error = linux_pollin(td, kfds, fds, nfds);
2566 error = kern_poll_kfds(td, kfds, nfds, tsp, ssp);
2568 error = linux_pollout(td, kfds, fds, nfds);
2570 if (error == 0 && tsp != NULL) {
2571 if (td->td_retval[0]) {
2573 timespecsub(&ts1, &ts0, &ts1);
2574 timespecsub(tsp, &ts1, tsp);
2575 if (tsp->tv_sec < 0)
2582 if (nfds > nitems(stackfds))
2587 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
2589 linux_ppoll_time64(struct thread *td, struct linux_ppoll_time64_args *args)
2591 struct timespec uts, *tsp;
2592 struct l_timespec64 lts;
2595 if (args->tsp != NULL) {
2596 error = copyin(args->tsp, <s, sizeof(lts));
2599 error = linux_to_native_timespec64(&uts, <s);
2605 error = linux_common_ppoll(td, args->fds, args->nfds, tsp,
2606 args->sset, args->ssize);
2607 if (error == 0 && args->tsp != NULL)
2608 error = linux_put_timespec64(&uts, args->tsp);
2611 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
2614 linux_pollin(struct thread *td, struct pollfd *fds, struct pollfd *ufds, u_int nfd)
2619 error = copyin(ufds, fds, nfd * sizeof(*fds));
2623 for (i = 0; i < nfd; i++) {
2624 if (fds->events != 0)
2625 linux_to_bsd_poll_events(td, fds->fd,
2626 fds->events, &fds->events);
2633 linux_pollout(struct thread *td, struct pollfd *fds, struct pollfd *ufds, u_int nfd)
2638 for (i = 0; i < nfd; i++) {
2639 if (fds->revents != 0) {
2640 bsd_to_linux_poll_events(fds->revents,
2644 error = copyout(&fds->revents, &ufds->revents,
2645 sizeof(ufds->revents));
2651 td->td_retval[0] = n;
2656 linux_sched_rr_get_interval_common(struct thread *td, pid_t pid,
2657 struct timespec *ts)
2663 * According to man in case the invalid pid specified
2664 * EINVAL should be returned.
2669 tdt = linux_tdfind(td, pid, -1);
2673 error = kern_sched_rr_get_interval_td(td, tdt, ts);
2674 PROC_UNLOCK(tdt->td_proc);
2679 linux_sched_rr_get_interval(struct thread *td,
2680 struct linux_sched_rr_get_interval_args *uap)
2685 error = linux_sched_rr_get_interval_common(td, uap->pid, &ts);
2688 return (linux_put_timespec(&ts, uap->interval));
2691 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
2693 linux_sched_rr_get_interval_time64(struct thread *td,
2694 struct linux_sched_rr_get_interval_time64_args *uap)
2699 error = linux_sched_rr_get_interval_common(td, uap->pid, &ts);
2702 return (linux_put_timespec64(&ts, uap->interval));
2707 * In case when the Linux thread is the initial thread in
2708 * the thread group thread id is equal to the process id.
2709 * Glibc depends on this magic (assert in pthread_getattr_np.c).
2712 linux_tdfind(struct thread *td, lwpid_t tid, pid_t pid)
2714 struct linux_emuldata *em;
2719 if (tid == 0 || tid == td->td_tid) {
2720 if (pid != -1 && td->td_proc->p_pid != pid)
2722 PROC_LOCK(td->td_proc);
2724 } else if (tid > PID_MAX)
2725 return (tdfind(tid, pid));
2728 * Initial thread where the tid equal to the pid.
2732 if (SV_PROC_ABI(p) != SV_ABI_LINUX ||
2733 (pid != -1 && tid != pid)) {
2735 * p is not a Linuxulator process.
2740 FOREACH_THREAD_IN_PROC(p, tdt) {
2742 if (tid == em->em_tid)
2751 linux_to_bsd_waitopts(int options, int *bsdopts)
2754 if (options & LINUX_WNOHANG)
2755 *bsdopts |= WNOHANG;
2756 if (options & LINUX_WUNTRACED)
2757 *bsdopts |= WUNTRACED;
2758 if (options & LINUX_WEXITED)
2759 *bsdopts |= WEXITED;
2760 if (options & LINUX_WCONTINUED)
2761 *bsdopts |= WCONTINUED;
2762 if (options & LINUX_WNOWAIT)
2763 *bsdopts |= WNOWAIT;
2765 if (options & __WCLONE)
2766 *bsdopts |= WLINUXCLONE;
2770 linux_getrandom(struct thread *td, struct linux_getrandom_args *args)
2776 if (args->flags & ~(LINUX_GRND_NONBLOCK|LINUX_GRND_RANDOM))
2778 if (args->count > INT_MAX)
2779 args->count = INT_MAX;
2781 iov.iov_base = args->buf;
2782 iov.iov_len = args->count;
2786 uio.uio_resid = iov.iov_len;
2787 uio.uio_segflg = UIO_USERSPACE;
2788 uio.uio_rw = UIO_READ;
2791 error = read_random_uio(&uio, args->flags & LINUX_GRND_NONBLOCK);
2793 td->td_retval[0] = args->count - uio.uio_resid;
2798 linux_mincore(struct thread *td, struct linux_mincore_args *args)
2801 /* Needs to be page-aligned */
2802 if (args->start & PAGE_MASK)
2804 return (kern_mincore(td, args->start, args->len, args->vec));
2807 #define SYSLOG_TAG "<6>"
2810 linux_syslog(struct thread *td, struct linux_syslog_args *args)
2812 char buf[128], *src, *dst;
2816 if (args->type != LINUX_SYSLOG_ACTION_READ_ALL) {
2817 linux_msg(td, "syslog unsupported type 0x%x", args->type);
2821 if (args->len < 6) {
2822 td->td_retval[0] = 0;
2826 error = priv_check(td, PRIV_MSGBUF);
2830 mtx_lock(&msgbuf_lock);
2831 msgbuf_peekbytes(msgbufp, NULL, 0, &seq);
2832 mtx_unlock(&msgbuf_lock);
2835 error = copyout(&SYSLOG_TAG, dst, sizeof(SYSLOG_TAG));
2836 /* The -1 is to skip the trailing '\0'. */
2837 dst += sizeof(SYSLOG_TAG) - 1;
2839 while (error == 0) {
2840 mtx_lock(&msgbuf_lock);
2841 buflen = msgbuf_peekbytes(msgbufp, buf, sizeof(buf), &seq);
2842 mtx_unlock(&msgbuf_lock);
2847 for (src = buf; src < buf + buflen && error == 0; src++) {
2851 if (dst >= args->buf + args->len)
2854 error = copyout(src, dst, 1);
2857 if (*src == '\n' && *(src + 1) != '<' &&
2858 dst + sizeof(SYSLOG_TAG) < args->buf + args->len) {
2859 error = copyout(&SYSLOG_TAG,
2860 dst, sizeof(SYSLOG_TAG));
2861 dst += sizeof(SYSLOG_TAG) - 1;
2866 td->td_retval[0] = dst - args->buf;
2871 linux_getcpu(struct thread *td, struct linux_getcpu_args *args)
2873 int cpu, error, node;
2875 cpu = td->td_oncpu; /* Make sure it doesn't change during copyout(9) */
2877 node = cpuid_to_pcpu[cpu]->pc_domain;
2879 if (args->cpu != NULL)
2880 error = copyout(&cpu, args->cpu, sizeof(l_int));
2881 if (args->node != NULL)
2882 error = copyout(&node, args->node, sizeof(l_int));
2886 #if defined(__i386__) || defined(__amd64__)
2888 linux_poll(struct thread *td, struct linux_poll_args *args)
2890 struct timespec ts, *tsp;
2892 if (args->timeout != INFTIM) {
2893 if (args->timeout < 0)
2895 ts.tv_sec = args->timeout / 1000;
2896 ts.tv_nsec = (args->timeout % 1000) * 1000000;
2901 return (linux_common_ppoll(td, args->fds, args->nfds,
2904 #endif /* __i386__ || __amd64__ */
2907 linux_seccomp(struct thread *td, struct linux_seccomp_args *args)
2911 case LINUX_SECCOMP_GET_ACTION_AVAIL:
2912 return (EOPNOTSUPP);
2915 * Ignore unknown operations, just like Linux kernel built
2916 * without CONFIG_SECCOMP.