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 if (l_times->tv_nsec != LINUX_UTIME_OMIT &&
914 l_times->tv_nsec != LINUX_UTIME_NOW &&
915 (l_times->tv_nsec < 0 || l_times->tv_nsec > 999999999))
918 times->tv_sec = l_times->tv_sec;
919 switch (l_times->tv_nsec)
921 case LINUX_UTIME_OMIT:
922 times->tv_nsec = UTIME_OMIT;
924 case LINUX_UTIME_NOW:
925 times->tv_nsec = UTIME_NOW;
928 times->tv_nsec = l_times->tv_nsec;
935 linux_utimensat_time64(struct thread *td, struct linux_utimensat_time64_args *args)
937 struct l_timespec64 l_times[2];
938 struct timespec times[2], *timesp;
941 if (args->times64 != NULL) {
942 error = copyin(args->times64, l_times, sizeof(l_times));
946 error = linux_utimensat_lts64_to_ts(&l_times[0], ×[0]);
949 error = linux_utimensat_lts64_to_ts(&l_times[1], ×[1]);
956 return (linux_common_utimensat(td, args->dfd, args->pathname,
957 timesp, args->flags));
959 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
961 #ifdef LINUX_LEGACY_SYSCALLS
963 linux_futimesat(struct thread *td, struct linux_futimesat_args *args)
966 struct timeval tv[2], *tvp = NULL;
970 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
972 if (args->utimes != NULL) {
973 if ((error = copyin(args->utimes, ltv, sizeof ltv)) != 0)
975 tv[0].tv_sec = ltv[0].tv_sec;
976 tv[0].tv_usec = ltv[0].tv_usec;
977 tv[1].tv_sec = ltv[1].tv_sec;
978 tv[1].tv_usec = ltv[1].tv_usec;
982 if (!LUSECONVPATH(td)) {
983 error = kern_utimesat(td, dfd, args->filename, UIO_USERSPACE,
986 LCONVPATHEXIST_AT(args->filename, &fname, dfd);
987 error = kern_utimesat(td, dfd, fname, UIO_SYSSPACE,
996 linux_common_wait(struct thread *td, idtype_t idtype, int id, int *statusp,
997 int options, void *rup, l_siginfo_t *infop)
1001 struct __wrusage wru;
1002 int error, status, tmpstat, sig;
1004 error = kern_wait6(td, idtype, id, &status, options,
1005 rup != NULL ? &wru : NULL, &siginfo);
1007 if (error == 0 && statusp) {
1008 tmpstat = status & 0xffff;
1009 if (WIFSIGNALED(tmpstat)) {
1010 tmpstat = (tmpstat & 0xffffff80) |
1011 bsd_to_linux_signal(WTERMSIG(tmpstat));
1012 } else if (WIFSTOPPED(tmpstat)) {
1013 tmpstat = (tmpstat & 0xffff00ff) |
1014 (bsd_to_linux_signal(WSTOPSIG(tmpstat)) << 8);
1015 #if defined(__aarch64__) || (defined(__amd64__) && !defined(COMPAT_LINUX32))
1016 if (WSTOPSIG(status) == SIGTRAP) {
1017 tmpstat = linux_ptrace_status(td,
1018 siginfo.si_pid, tmpstat);
1021 } else if (WIFCONTINUED(tmpstat)) {
1024 error = copyout(&tmpstat, statusp, sizeof(int));
1026 if (error == 0 && rup != NULL)
1027 error = linux_copyout_rusage(&wru.wru_self, rup);
1028 if (error == 0 && infop != NULL && td->td_retval[0] != 0) {
1029 sig = bsd_to_linux_signal(siginfo.si_signo);
1030 siginfo_to_lsiginfo(&siginfo, &lsi, sig);
1031 error = copyout(&lsi, infop, sizeof(lsi));
1037 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1039 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
1041 struct linux_wait4_args wait4_args;
1043 wait4_args.pid = args->pid;
1044 wait4_args.status = args->status;
1045 wait4_args.options = args->options;
1046 wait4_args.rusage = NULL;
1048 return (linux_wait4(td, &wait4_args));
1050 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1053 linux_wait4(struct thread *td, struct linux_wait4_args *args)
1056 int options, id, idtype;
1058 if (args->options & ~(LINUX_WUNTRACED | LINUX_WNOHANG |
1059 LINUX_WCONTINUED | __WCLONE | __WNOTHREAD | __WALL))
1062 /* -INT_MIN is not defined. */
1063 if (args->pid == INT_MIN)
1067 linux_to_bsd_waitopts(args->options, &options);
1070 * For backward compatibility we implicitly add flags WEXITED
1071 * and WTRAPPED here.
1073 options |= WEXITED | WTRAPPED;
1076 * As FreeBSD does not have __WALL option bit analogue explicitly set all
1077 * possible option bits to emulate Linux __WALL wait option bit. The same
1078 * for waitid system call.
1080 if ((args->options & __WALL) != 0)
1081 options |= WUNTRACED | WCONTINUED | WLINUXCLONE;
1083 if (args->pid == WAIT_ANY) {
1086 } else if (args->pid < 0) {
1088 id = (id_t)-args->pid;
1089 } else if (args->pid == 0) {
1097 id = (id_t)args->pid;
1100 return (linux_common_wait(td, idtype, id, args->status, options,
1101 args->rusage, NULL));
1105 linux_waitid(struct thread *td, struct linux_waitid_args *args)
1112 if (args->options & ~(LINUX_WNOHANG | LINUX_WNOWAIT | LINUX_WEXITED |
1113 LINUX_WSTOPPED | LINUX_WCONTINUED | __WCLONE | __WNOTHREAD | __WALL))
1117 linux_to_bsd_waitopts(args->options, &options);
1118 if ((args->options & __WALL) != 0)
1119 options |= WEXITED | WTRAPPED | WUNTRACED |
1120 WCONTINUED | WLINUXCLONE;
1123 switch (args->idtype) {
1133 if (linux_use54(td) && args->id == 0) {
1138 } else if (args->id <= 0)
1143 LINUX_RATELIMIT_MSG("unsupported waitid P_PIDFD idtype");
1149 error = linux_common_wait(td, idtype, id, NULL, options,
1150 args->rusage, args->info);
1151 td->td_retval[0] = 0;
1156 #ifdef LINUX_LEGACY_SYSCALLS
1158 linux_mknod(struct thread *td, struct linux_mknod_args *args)
1165 convpath = LUSECONVPATH(td);
1168 seg = UIO_USERSPACE;
1170 LCONVPATHCREAT(args->path, &path);
1174 switch (args->mode & S_IFMT) {
1177 error = kern_mkfifoat(td, AT_FDCWD, path, seg,
1183 error = kern_mknodat(td, AT_FDCWD, path, seg,
1184 args->mode, args->dev);
1192 args->mode |= S_IFREG;
1195 error = kern_openat(td, AT_FDCWD, path, seg,
1196 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1198 kern_close(td, td->td_retval[0]);
1212 linux_mknodat(struct thread *td, struct linux_mknodat_args *args)
1219 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1221 convpath = LUSECONVPATH(td);
1223 path = __DECONST(char *, args->filename);
1224 seg = UIO_USERSPACE;
1226 LCONVPATHCREAT_AT(args->filename, &path, dfd);
1230 switch (args->mode & S_IFMT) {
1233 error = kern_mkfifoat(td, dfd, path, seg, args->mode);
1238 error = kern_mknodat(td, dfd, path, seg, args->mode,
1247 args->mode |= S_IFREG;
1250 error = kern_openat(td, dfd, path, seg,
1251 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1253 kern_close(td, td->td_retval[0]);
1266 * UGH! This is just about the dumbest idea I've ever heard!!
1269 linux_personality(struct thread *td, struct linux_personality_args *args)
1271 struct linux_pemuldata *pem;
1272 struct proc *p = td->td_proc;
1278 if (args->per != 0xffffffff)
1279 pem->persona = args->per;
1282 td->td_retval[0] = old;
1286 struct l_itimerval {
1287 l_timeval it_interval;
1291 #define B2L_ITIMERVAL(bip, lip) \
1292 (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec; \
1293 (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec; \
1294 (bip)->it_value.tv_sec = (lip)->it_value.tv_sec; \
1295 (bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
1298 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
1301 struct l_itimerval ls;
1302 struct itimerval aitv, oitv;
1304 if (uap->itv == NULL) {
1305 uap->itv = uap->oitv;
1306 return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
1309 error = copyin(uap->itv, &ls, sizeof(ls));
1312 B2L_ITIMERVAL(&aitv, &ls);
1313 error = kern_setitimer(td, uap->which, &aitv, &oitv);
1314 if (error != 0 || uap->oitv == NULL)
1316 B2L_ITIMERVAL(&ls, &oitv);
1318 return (copyout(&ls, uap->oitv, sizeof(ls)));
1322 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
1325 struct l_itimerval ls;
1326 struct itimerval aitv;
1328 error = kern_getitimer(td, uap->which, &aitv);
1331 B2L_ITIMERVAL(&ls, &aitv);
1332 return (copyout(&ls, uap->itv, sizeof(ls)));
1335 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1337 linux_nice(struct thread *td, struct linux_nice_args *args)
1340 return (kern_setpriority(td, PRIO_PROCESS, 0, args->inc));
1342 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1345 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
1347 struct ucred *newcred, *oldcred;
1348 l_gid_t *linux_gidset;
1353 ngrp = args->gidsetsize;
1354 if (ngrp < 0 || ngrp >= ngroups_max + 1)
1356 linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_LINUX, M_WAITOK);
1357 error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
1361 crextend(newcred, ngrp + 1);
1364 oldcred = p->p_ucred;
1365 crcopy(newcred, oldcred);
1368 * cr_groups[0] holds egid. Setting the whole set from
1369 * the supplied set will cause egid to be changed too.
1370 * Keep cr_groups[0] unchanged to prevent that.
1373 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS)) != 0) {
1380 newcred->cr_ngroups = ngrp + 1;
1382 bsd_gidset = newcred->cr_groups;
1385 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1389 newcred->cr_ngroups = 1;
1392 proc_set_cred(p, newcred);
1397 free(linux_gidset, M_LINUX);
1402 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1405 l_gid_t *linux_gidset;
1407 int bsd_gidsetsz, ngrp, error;
1409 cred = td->td_ucred;
1410 bsd_gidset = cred->cr_groups;
1411 bsd_gidsetsz = cred->cr_ngroups - 1;
1414 * cr_groups[0] holds egid. Returning the whole set
1415 * here will cause a duplicate. Exclude cr_groups[0]
1419 if ((ngrp = args->gidsetsize) == 0) {
1420 td->td_retval[0] = bsd_gidsetsz;
1424 if (ngrp < bsd_gidsetsz)
1428 linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset),
1430 while (ngrp < bsd_gidsetsz) {
1431 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1435 error = copyout(linux_gidset, args->grouplist, ngrp * sizeof(l_gid_t));
1436 free(linux_gidset, M_LINUX);
1440 td->td_retval[0] = ngrp;
1445 linux_get_dummy_limit(l_uint resource, struct rlimit *rlim)
1448 if (linux_dummy_rlimits == 0)
1452 case LINUX_RLIMIT_LOCKS:
1453 case LINUX_RLIMIT_SIGPENDING:
1454 case LINUX_RLIMIT_MSGQUEUE:
1455 case LINUX_RLIMIT_RTTIME:
1456 rlim->rlim_cur = LINUX_RLIM_INFINITY;
1457 rlim->rlim_max = LINUX_RLIM_INFINITY;
1459 case LINUX_RLIMIT_NICE:
1460 case LINUX_RLIMIT_RTPRIO:
1470 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1472 struct rlimit bsd_rlim;
1473 struct l_rlimit rlim;
1477 if (args->resource >= LINUX_RLIM_NLIMITS)
1480 which = linux_to_bsd_resource[args->resource];
1484 error = copyin(args->rlim, &rlim, sizeof(rlim));
1488 bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1489 bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1490 return (kern_setrlimit(td, which, &bsd_rlim));
1493 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1495 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1497 struct l_rlimit rlim;
1498 struct rlimit bsd_rlim;
1501 if (linux_get_dummy_limit(args->resource, &bsd_rlim)) {
1502 rlim.rlim_cur = bsd_rlim.rlim_cur;
1503 rlim.rlim_max = bsd_rlim.rlim_max;
1504 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1507 if (args->resource >= LINUX_RLIM_NLIMITS)
1510 which = linux_to_bsd_resource[args->resource];
1514 lim_rlimit(td, which, &bsd_rlim);
1516 #ifdef COMPAT_LINUX32
1517 rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1518 if (rlim.rlim_cur == UINT_MAX)
1519 rlim.rlim_cur = INT_MAX;
1520 rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1521 if (rlim.rlim_max == UINT_MAX)
1522 rlim.rlim_max = INT_MAX;
1524 rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1525 if (rlim.rlim_cur == ULONG_MAX)
1526 rlim.rlim_cur = LONG_MAX;
1527 rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1528 if (rlim.rlim_max == ULONG_MAX)
1529 rlim.rlim_max = LONG_MAX;
1531 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1533 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1536 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1538 struct l_rlimit rlim;
1539 struct rlimit bsd_rlim;
1542 if (linux_get_dummy_limit(args->resource, &bsd_rlim)) {
1543 rlim.rlim_cur = bsd_rlim.rlim_cur;
1544 rlim.rlim_max = bsd_rlim.rlim_max;
1545 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1548 if (args->resource >= LINUX_RLIM_NLIMITS)
1551 which = linux_to_bsd_resource[args->resource];
1555 lim_rlimit(td, which, &bsd_rlim);
1557 rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1558 rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1559 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1563 linux_sched_setscheduler(struct thread *td,
1564 struct linux_sched_setscheduler_args *args)
1566 struct sched_param sched_param;
1570 switch (args->policy) {
1571 case LINUX_SCHED_OTHER:
1572 policy = SCHED_OTHER;
1574 case LINUX_SCHED_FIFO:
1575 policy = SCHED_FIFO;
1577 case LINUX_SCHED_RR:
1584 error = copyin(args->param, &sched_param, sizeof(sched_param));
1588 if (linux_map_sched_prio) {
1591 if (sched_param.sched_priority != 0)
1594 sched_param.sched_priority =
1595 PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE;
1599 if (sched_param.sched_priority < 1 ||
1600 sched_param.sched_priority >= LINUX_MAX_RT_PRIO)
1604 * Map [1, LINUX_MAX_RT_PRIO - 1] to
1605 * [0, RTP_PRIO_MAX - RTP_PRIO_MIN] (rounding down).
1607 sched_param.sched_priority =
1608 (sched_param.sched_priority - 1) *
1609 (RTP_PRIO_MAX - RTP_PRIO_MIN + 1) /
1610 (LINUX_MAX_RT_PRIO - 1);
1615 tdt = linux_tdfind(td, args->pid, -1);
1619 error = kern_sched_setscheduler(td, tdt, policy, &sched_param);
1620 PROC_UNLOCK(tdt->td_proc);
1625 linux_sched_getscheduler(struct thread *td,
1626 struct linux_sched_getscheduler_args *args)
1631 tdt = linux_tdfind(td, args->pid, -1);
1635 error = kern_sched_getscheduler(td, tdt, &policy);
1636 PROC_UNLOCK(tdt->td_proc);
1640 td->td_retval[0] = LINUX_SCHED_OTHER;
1643 td->td_retval[0] = LINUX_SCHED_FIFO;
1646 td->td_retval[0] = LINUX_SCHED_RR;
1653 linux_sched_get_priority_max(struct thread *td,
1654 struct linux_sched_get_priority_max_args *args)
1656 struct sched_get_priority_max_args bsd;
1658 if (linux_map_sched_prio) {
1659 switch (args->policy) {
1660 case LINUX_SCHED_OTHER:
1661 td->td_retval[0] = 0;
1663 case LINUX_SCHED_FIFO:
1664 case LINUX_SCHED_RR:
1665 td->td_retval[0] = LINUX_MAX_RT_PRIO - 1;
1672 switch (args->policy) {
1673 case LINUX_SCHED_OTHER:
1674 bsd.policy = SCHED_OTHER;
1676 case LINUX_SCHED_FIFO:
1677 bsd.policy = SCHED_FIFO;
1679 case LINUX_SCHED_RR:
1680 bsd.policy = SCHED_RR;
1685 return (sys_sched_get_priority_max(td, &bsd));
1689 linux_sched_get_priority_min(struct thread *td,
1690 struct linux_sched_get_priority_min_args *args)
1692 struct sched_get_priority_min_args bsd;
1694 if (linux_map_sched_prio) {
1695 switch (args->policy) {
1696 case LINUX_SCHED_OTHER:
1697 td->td_retval[0] = 0;
1699 case LINUX_SCHED_FIFO:
1700 case LINUX_SCHED_RR:
1701 td->td_retval[0] = 1;
1708 switch (args->policy) {
1709 case LINUX_SCHED_OTHER:
1710 bsd.policy = SCHED_OTHER;
1712 case LINUX_SCHED_FIFO:
1713 bsd.policy = SCHED_FIFO;
1715 case LINUX_SCHED_RR:
1716 bsd.policy = SCHED_RR;
1721 return (sys_sched_get_priority_min(td, &bsd));
1724 #define REBOOT_CAD_ON 0x89abcdef
1725 #define REBOOT_CAD_OFF 0
1726 #define REBOOT_HALT 0xcdef0123
1727 #define REBOOT_RESTART 0x01234567
1728 #define REBOOT_RESTART2 0xA1B2C3D4
1729 #define REBOOT_POWEROFF 0x4321FEDC
1730 #define REBOOT_MAGIC1 0xfee1dead
1731 #define REBOOT_MAGIC2 0x28121969
1732 #define REBOOT_MAGIC2A 0x05121996
1733 #define REBOOT_MAGIC2B 0x16041998
1736 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1738 struct reboot_args bsd_args;
1740 if (args->magic1 != REBOOT_MAGIC1)
1743 switch (args->magic2) {
1745 case REBOOT_MAGIC2A:
1746 case REBOOT_MAGIC2B:
1752 switch (args->cmd) {
1754 case REBOOT_CAD_OFF:
1755 return (priv_check(td, PRIV_REBOOT));
1757 bsd_args.opt = RB_HALT;
1759 case REBOOT_RESTART:
1760 case REBOOT_RESTART2:
1763 case REBOOT_POWEROFF:
1764 bsd_args.opt = RB_POWEROFF;
1769 return (sys_reboot(td, &bsd_args));
1773 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1776 td->td_retval[0] = td->td_proc->p_pid;
1782 linux_gettid(struct thread *td, struct linux_gettid_args *args)
1784 struct linux_emuldata *em;
1787 KASSERT(em != NULL, ("gettid: emuldata not found.\n"));
1789 td->td_retval[0] = em->em_tid;
1795 linux_getppid(struct thread *td, struct linux_getppid_args *args)
1798 td->td_retval[0] = kern_getppid(td);
1803 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1806 td->td_retval[0] = td->td_ucred->cr_rgid;
1811 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1814 td->td_retval[0] = td->td_ucred->cr_ruid;
1819 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1822 return (kern_getsid(td, args->pid));
1826 linux_nosys(struct thread *td, struct nosys_args *ignore)
1833 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1837 error = kern_getpriority(td, args->which, args->who);
1838 td->td_retval[0] = 20 - td->td_retval[0];
1843 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1848 name[1] = KERN_HOSTNAME;
1849 return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1854 linux_setdomainname(struct thread *td, struct linux_setdomainname_args *args)
1859 name[1] = KERN_NISDOMAINNAME;
1860 return (userland_sysctl(td, name, 2, 0, 0, 0, args->name,
1865 linux_exit_group(struct thread *td, struct linux_exit_group_args *args)
1868 LINUX_CTR2(exit_group, "thread(%d) (%d)", td->td_tid,
1872 * XXX: we should send a signal to the parent if
1873 * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?)
1874 * as it doesnt occur often.
1876 exit1(td, args->error_code, 0);
1880 #define _LINUX_CAPABILITY_VERSION_1 0x19980330
1881 #define _LINUX_CAPABILITY_VERSION_2 0x20071026
1882 #define _LINUX_CAPABILITY_VERSION_3 0x20080522
1884 struct l_user_cap_header {
1889 struct l_user_cap_data {
1896 linux_capget(struct thread *td, struct linux_capget_args *uap)
1898 struct l_user_cap_header luch;
1899 struct l_user_cap_data lucd[2];
1902 if (uap->hdrp == NULL)
1905 error = copyin(uap->hdrp, &luch, sizeof(luch));
1909 switch (luch.version) {
1910 case _LINUX_CAPABILITY_VERSION_1:
1913 case _LINUX_CAPABILITY_VERSION_2:
1914 case _LINUX_CAPABILITY_VERSION_3:
1918 luch.version = _LINUX_CAPABILITY_VERSION_1;
1919 error = copyout(&luch, uap->hdrp, sizeof(luch));
1930 * The current implementation doesn't support setting
1931 * a capability (it's essentially a stub) so indicate
1932 * that no capabilities are currently set or available
1935 memset(&lucd, 0, u32s * sizeof(lucd[0]));
1936 error = copyout(&lucd, uap->datap, u32s * sizeof(lucd[0]));
1943 linux_capset(struct thread *td, struct linux_capset_args *uap)
1945 struct l_user_cap_header luch;
1946 struct l_user_cap_data lucd[2];
1949 if (uap->hdrp == NULL || uap->datap == NULL)
1952 error = copyin(uap->hdrp, &luch, sizeof(luch));
1956 switch (luch.version) {
1957 case _LINUX_CAPABILITY_VERSION_1:
1960 case _LINUX_CAPABILITY_VERSION_2:
1961 case _LINUX_CAPABILITY_VERSION_3:
1965 luch.version = _LINUX_CAPABILITY_VERSION_1;
1966 error = copyout(&luch, uap->hdrp, sizeof(luch));
1975 error = copyin(uap->datap, &lucd, u32s * sizeof(lucd[0]));
1979 /* We currently don't support setting any capabilities. */
1980 for (i = 0; i < u32s; i++) {
1981 if (lucd[i].effective || lucd[i].permitted ||
1982 lucd[i].inheritable) {
1984 "capset[%d] effective=0x%x, permitted=0x%x, "
1985 "inheritable=0x%x is not implemented", i,
1986 (int)lucd[i].effective, (int)lucd[i].permitted,
1987 (int)lucd[i].inheritable);
1996 linux_prctl(struct thread *td, struct linux_prctl_args *args)
1998 int error = 0, max_size, arg;
1999 struct proc *p = td->td_proc;
2000 char comm[LINUX_MAX_COMM_LEN];
2001 int pdeath_signal, trace_state;
2003 switch (args->option) {
2004 case LINUX_PR_SET_PDEATHSIG:
2005 if (!LINUX_SIG_VALID(args->arg2))
2007 pdeath_signal = linux_to_bsd_signal(args->arg2);
2008 return (kern_procctl(td, P_PID, 0, PROC_PDEATHSIG_CTL,
2010 case LINUX_PR_GET_PDEATHSIG:
2011 error = kern_procctl(td, P_PID, 0, PROC_PDEATHSIG_STATUS,
2015 pdeath_signal = bsd_to_linux_signal(pdeath_signal);
2016 return (copyout(&pdeath_signal,
2017 (void *)(register_t)args->arg2,
2018 sizeof(pdeath_signal)));
2020 * In Linux, this flag controls if set[gu]id processes can coredump.
2021 * There are additional semantics imposed on processes that cannot
2023 * - Such processes can not be ptraced.
2024 * - There are some semantics around ownership of process-related files
2025 * in the /proc namespace.
2027 * In FreeBSD, we can (and by default, do) disable setuid coredump
2028 * system-wide with 'sugid_coredump.' We control tracability on a
2029 * per-process basis with the procctl PROC_TRACE (=> P2_NOTRACE flag).
2030 * By happy coincidence, P2_NOTRACE also prevents coredumping. So the
2031 * procctl is roughly analogous to Linux's DUMPABLE.
2033 * So, proxy these knobs to the corresponding PROC_TRACE setting.
2035 case LINUX_PR_GET_DUMPABLE:
2036 error = kern_procctl(td, P_PID, p->p_pid, PROC_TRACE_STATUS,
2040 td->td_retval[0] = (trace_state != -1);
2042 case LINUX_PR_SET_DUMPABLE:
2044 * It is only valid for userspace to set one of these two
2045 * flags, and only one at a time.
2047 switch (args->arg2) {
2048 case LINUX_SUID_DUMP_DISABLE:
2049 trace_state = PROC_TRACE_CTL_DISABLE_EXEC;
2051 case LINUX_SUID_DUMP_USER:
2052 trace_state = PROC_TRACE_CTL_ENABLE;
2057 return (kern_procctl(td, P_PID, p->p_pid, PROC_TRACE_CTL,
2059 case LINUX_PR_GET_KEEPCAPS:
2061 * Indicate that we always clear the effective and
2062 * permitted capability sets when the user id becomes
2063 * non-zero (actually the capability sets are simply
2064 * always zero in the current implementation).
2066 td->td_retval[0] = 0;
2068 case LINUX_PR_SET_KEEPCAPS:
2070 * Ignore requests to keep the effective and permitted
2071 * capability sets when the user id becomes non-zero.
2074 case LINUX_PR_SET_NAME:
2076 * To be on the safe side we need to make sure to not
2077 * overflow the size a Linux program expects. We already
2078 * do this here in the copyin, so that we don't need to
2081 max_size = MIN(sizeof(comm), sizeof(p->p_comm));
2082 error = copyinstr((void *)(register_t)args->arg2, comm,
2085 /* Linux silently truncates the name if it is too long. */
2086 if (error == ENAMETOOLONG) {
2088 * XXX: copyinstr() isn't documented to populate the
2089 * array completely, so do a copyin() to be on the
2090 * safe side. This should be changed in case
2091 * copyinstr() is changed to guarantee this.
2093 error = copyin((void *)(register_t)args->arg2, comm,
2095 comm[max_size - 1] = '\0';
2101 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
2104 case LINUX_PR_GET_NAME:
2106 strlcpy(comm, p->p_comm, sizeof(comm));
2108 error = copyout(comm, (void *)(register_t)args->arg2,
2111 case LINUX_PR_GET_SECCOMP:
2112 case LINUX_PR_SET_SECCOMP:
2114 * Same as returned by Linux without CONFIG_SECCOMP enabled.
2118 case LINUX_PR_CAPBSET_READ:
2121 * This makes too much noise with Ubuntu Focal.
2123 linux_msg(td, "unsupported prctl PR_CAPBSET_READ %d",
2128 case LINUX_PR_SET_NO_NEW_PRIVS:
2129 arg = args->arg2 == 1 ?
2130 PROC_NO_NEW_PRIVS_ENABLE : PROC_NO_NEW_PRIVS_DISABLE;
2131 error = kern_procctl(td, P_PID, p->p_pid,
2132 PROC_NO_NEW_PRIVS_CTL, &arg);
2134 case LINUX_PR_SET_PTRACER:
2135 linux_msg(td, "unsupported prctl PR_SET_PTRACER");
2139 linux_msg(td, "unsupported prctl option %d", args->option);
2148 linux_sched_setparam(struct thread *td,
2149 struct linux_sched_setparam_args *uap)
2151 struct sched_param sched_param;
2155 error = copyin(uap->param, &sched_param, sizeof(sched_param));
2159 tdt = linux_tdfind(td, uap->pid, -1);
2163 if (linux_map_sched_prio) {
2164 error = kern_sched_getscheduler(td, tdt, &policy);
2170 if (sched_param.sched_priority != 0) {
2174 sched_param.sched_priority =
2175 PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE;
2179 if (sched_param.sched_priority < 1 ||
2180 sched_param.sched_priority >= LINUX_MAX_RT_PRIO) {
2185 * Map [1, LINUX_MAX_RT_PRIO - 1] to
2186 * [0, RTP_PRIO_MAX - RTP_PRIO_MIN] (rounding down).
2188 sched_param.sched_priority =
2189 (sched_param.sched_priority - 1) *
2190 (RTP_PRIO_MAX - RTP_PRIO_MIN + 1) /
2191 (LINUX_MAX_RT_PRIO - 1);
2196 error = kern_sched_setparam(td, tdt, &sched_param);
2197 out: PROC_UNLOCK(tdt->td_proc);
2202 linux_sched_getparam(struct thread *td,
2203 struct linux_sched_getparam_args *uap)
2205 struct sched_param sched_param;
2209 tdt = linux_tdfind(td, uap->pid, -1);
2213 error = kern_sched_getparam(td, tdt, &sched_param);
2215 PROC_UNLOCK(tdt->td_proc);
2219 if (linux_map_sched_prio) {
2220 error = kern_sched_getscheduler(td, tdt, &policy);
2221 PROC_UNLOCK(tdt->td_proc);
2227 sched_param.sched_priority = 0;
2232 * Map [0, RTP_PRIO_MAX - RTP_PRIO_MIN] to
2233 * [1, LINUX_MAX_RT_PRIO - 1] (rounding up).
2235 sched_param.sched_priority =
2236 (sched_param.sched_priority *
2237 (LINUX_MAX_RT_PRIO - 1) +
2238 (RTP_PRIO_MAX - RTP_PRIO_MIN - 1)) /
2239 (RTP_PRIO_MAX - RTP_PRIO_MIN) + 1;
2243 PROC_UNLOCK(tdt->td_proc);
2245 error = copyout(&sched_param, uap->param, sizeof(sched_param));
2249 static const struct cpuset_copy_cb copy_set = {
2250 .cpuset_copyin = copyin,
2251 .cpuset_copyout = copyout
2255 * Get affinity of a process.
2258 linux_sched_getaffinity(struct thread *td,
2259 struct linux_sched_getaffinity_args *args)
2264 if (args->len < sizeof(cpuset_t))
2267 tdt = linux_tdfind(td, args->pid, -1);
2271 PROC_UNLOCK(tdt->td_proc);
2273 error = kern_cpuset_getaffinity(td, CPU_LEVEL_WHICH, CPU_WHICH_TID,
2274 tdt->td_tid, sizeof(cpuset_t), (cpuset_t *)args->user_mask_ptr,
2277 td->td_retval[0] = sizeof(cpuset_t);
2283 * Set affinity of a process.
2286 linux_sched_setaffinity(struct thread *td,
2287 struct linux_sched_setaffinity_args *args)
2291 if (args->len < sizeof(cpuset_t))
2294 tdt = linux_tdfind(td, args->pid, -1);
2298 PROC_UNLOCK(tdt->td_proc);
2300 return (kern_cpuset_setaffinity(td, CPU_LEVEL_WHICH, CPU_WHICH_TID,
2301 tdt->td_tid, sizeof(cpuset_t), (cpuset_t *) args->user_mask_ptr,
2305 struct linux_rlimit64 {
2311 linux_prlimit64(struct thread *td, struct linux_prlimit64_args *args)
2313 struct rlimit rlim, nrlim;
2314 struct linux_rlimit64 lrlim;
2320 if (args->new == NULL && args->old != NULL) {
2321 if (linux_get_dummy_limit(args->resource, &rlim)) {
2322 lrlim.rlim_cur = rlim.rlim_cur;
2323 lrlim.rlim_max = rlim.rlim_max;
2324 return (copyout(&lrlim, args->old, sizeof(lrlim)));
2328 if (args->resource >= LINUX_RLIM_NLIMITS)
2331 which = linux_to_bsd_resource[args->resource];
2335 if (args->new != NULL) {
2337 * Note. Unlike FreeBSD where rlim is signed 64-bit Linux
2338 * rlim is unsigned 64-bit. FreeBSD treats negative limits
2339 * as INFINITY so we do not need a conversion even.
2341 error = copyin(args->new, &nrlim, sizeof(nrlim));
2346 flags = PGET_HOLD | PGET_NOTWEXIT;
2347 if (args->new != NULL)
2348 flags |= PGET_CANDEBUG;
2350 flags |= PGET_CANSEE;
2351 if (args->pid == 0) {
2355 error = pget(args->pid, flags, &p);
2359 if (args->old != NULL) {
2361 lim_rlimit_proc(p, which, &rlim);
2363 if (rlim.rlim_cur == RLIM_INFINITY)
2364 lrlim.rlim_cur = LINUX_RLIM_INFINITY;
2366 lrlim.rlim_cur = rlim.rlim_cur;
2367 if (rlim.rlim_max == RLIM_INFINITY)
2368 lrlim.rlim_max = LINUX_RLIM_INFINITY;
2370 lrlim.rlim_max = rlim.rlim_max;
2371 error = copyout(&lrlim, args->old, sizeof(lrlim));
2376 if (args->new != NULL)
2377 error = kern_proc_setrlimit(td, p, which, &nrlim);
2385 linux_pselect6(struct thread *td, struct linux_pselect6_args *args)
2387 struct l_timespec lts;
2388 struct timespec ts, *tsp;
2391 if (args->tsp != NULL) {
2392 error = copyin(args->tsp, <s, sizeof(lts));
2395 error = linux_to_native_timespec(&ts, <s);
2402 error = linux_common_pselect6(td, args->nfds, args->readfds,
2403 args->writefds, args->exceptfds, tsp, args->sig);
2407 if (args->tsp != NULL) {
2408 error = native_to_linux_timespec(<s, tsp);
2410 error = copyout(<s, args->tsp, sizeof(lts));
2416 linux_common_pselect6(struct thread *td, l_int nfds, l_fd_set *readfds,
2417 l_fd_set *writefds, l_fd_set *exceptfds, struct timespec *tsp,
2420 struct timeval utv, tv0, tv1, *tvp;
2421 struct l_pselect6arg lpse6;
2429 error = copyin(sig, &lpse6, sizeof(lpse6));
2432 if (lpse6.ss_len != sizeof(l_ss))
2434 if (lpse6.ss != 0) {
2435 error = copyin(PTRIN(lpse6.ss), &l_ss,
2439 linux_to_bsd_sigset(&l_ss, &ss);
2446 * Currently glibc changes nanosecond number to microsecond.
2447 * This mean losing precision but for now it is hardly seen.
2450 TIMESPEC_TO_TIMEVAL(&utv, tsp);
2451 if (itimerfix(&utv))
2459 error = kern_pselect(td, nfds, readfds, writefds,
2460 exceptfds, tvp, ssp, LINUX_NFDBITS);
2462 if (error == 0 && tsp != NULL) {
2463 if (td->td_retval[0] != 0) {
2465 * Compute how much time was left of the timeout,
2466 * by subtracting the current time and the time
2467 * before we started the call, and subtracting
2468 * that result from the user-supplied value.
2472 timevalsub(&tv1, &tv0);
2473 timevalsub(&utv, &tv1);
2478 TIMEVAL_TO_TIMESPEC(&utv, tsp);
2483 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
2485 linux_pselect6_time64(struct thread *td,
2486 struct linux_pselect6_time64_args *args)
2488 struct l_timespec64 lts;
2489 struct timespec ts, *tsp;
2492 if (args->tsp != NULL) {
2493 error = copyin(args->tsp, <s, sizeof(lts));
2496 error = linux_to_native_timespec64(&ts, <s);
2503 error = linux_common_pselect6(td, args->nfds, args->readfds,
2504 args->writefds, args->exceptfds, tsp, args->sig);
2508 if (args->tsp != NULL) {
2509 error = native_to_linux_timespec64(<s, tsp);
2511 error = copyout(<s, args->tsp, sizeof(lts));
2515 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
2518 linux_ppoll(struct thread *td, struct linux_ppoll_args *args)
2520 struct timespec uts, *tsp;
2521 struct l_timespec lts;
2524 if (args->tsp != NULL) {
2525 error = copyin(args->tsp, <s, sizeof(lts));
2528 error = linux_to_native_timespec(&uts, <s);
2535 error = linux_common_ppoll(td, args->fds, args->nfds, tsp,
2536 args->sset, args->ssize);
2540 error = native_to_linux_timespec(<s, tsp);
2542 error = copyout(<s, args->tsp, sizeof(lts));
2548 linux_common_ppoll(struct thread *td, struct pollfd *fds, uint32_t nfds,
2549 struct timespec *tsp, l_sigset_t *sset, l_size_t ssize)
2551 struct timespec ts0, ts1;
2552 struct pollfd stackfds[32];
2553 struct pollfd *kfds;
2559 if (kern_poll_maxfds(nfds))
2562 if (ssize != sizeof(l_ss))
2564 error = copyin(sset, &l_ss, sizeof(l_ss));
2567 linux_to_bsd_sigset(&l_ss, &ss);
2574 if (nfds > nitems(stackfds))
2575 kfds = mallocarray(nfds, sizeof(*kfds), M_TEMP, M_WAITOK);
2578 error = linux_pollin(td, kfds, fds, nfds);
2582 error = kern_poll_kfds(td, kfds, nfds, tsp, ssp);
2584 error = linux_pollout(td, kfds, fds, nfds);
2586 if (error == 0 && tsp != NULL) {
2587 if (td->td_retval[0]) {
2589 timespecsub(&ts1, &ts0, &ts1);
2590 timespecsub(tsp, &ts1, tsp);
2591 if (tsp->tv_sec < 0)
2598 if (nfds > nitems(stackfds))
2603 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
2605 linux_ppoll_time64(struct thread *td, struct linux_ppoll_time64_args *args)
2607 struct timespec uts, *tsp;
2608 struct l_timespec64 lts;
2611 if (args->tsp != NULL) {
2612 error = copyin(args->tsp, <s, sizeof(lts));
2615 error = linux_to_native_timespec64(&uts, <s);
2621 error = linux_common_ppoll(td, args->fds, args->nfds, tsp,
2622 args->sset, args->ssize);
2626 error = native_to_linux_timespec64(<s, tsp);
2628 error = copyout(<s, args->tsp, sizeof(lts));
2632 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
2635 linux_pollin(struct thread *td, struct pollfd *fds, struct pollfd *ufds, u_int nfd)
2640 error = copyin(ufds, fds, nfd * sizeof(*fds));
2644 for (i = 0; i < nfd; i++) {
2645 if (fds->events != 0)
2646 linux_to_bsd_poll_events(td, fds->fd,
2647 fds->events, &fds->events);
2654 linux_pollout(struct thread *td, struct pollfd *fds, struct pollfd *ufds, u_int nfd)
2659 for (i = 0; i < nfd; i++) {
2660 if (fds->revents != 0) {
2661 bsd_to_linux_poll_events(fds->revents,
2665 error = copyout(&fds->revents, &ufds->revents,
2666 sizeof(ufds->revents));
2672 td->td_retval[0] = n;
2677 linux_sched_rr_get_interval(struct thread *td,
2678 struct linux_sched_rr_get_interval_args *uap)
2681 struct l_timespec lts;
2686 * According to man in case the invalid pid specified
2687 * EINVAL should be returned.
2692 tdt = linux_tdfind(td, uap->pid, -1);
2696 error = kern_sched_rr_get_interval_td(td, tdt, &ts);
2697 PROC_UNLOCK(tdt->td_proc);
2700 error = native_to_linux_timespec(<s, &ts);
2703 return (copyout(<s, uap->interval, sizeof(lts)));
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) {
2721 PROC_LOCK(tdt->td_proc);
2722 } else if (tid > PID_MAX)
2723 tdt = tdfind(tid, pid);
2726 * Initial thread where the tid equal to the pid.
2730 if (SV_PROC_ABI(p) != SV_ABI_LINUX) {
2732 * p is not a Linuxulator process.
2737 FOREACH_THREAD_IN_PROC(p, tdt) {
2739 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.