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);
282 LCONVPATHEXIST(args->library, &library);
283 NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | AUDITVNODE1,
284 UIO_SYSSPACE, library);
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 strlcpy(utsname.machine, "x86_64", LINUX_MAX_UTSNAME);
724 #elif defined(__aarch64__)
725 strlcpy(utsname.machine, "aarch64", LINUX_MAX_UTSNAME);
726 #elif defined(__i386__)
727 strlcpy(utsname.machine, "i686", LINUX_MAX_UTSNAME);
730 return (copyout(&utsname, args->buf, sizeof(utsname)));
738 #ifdef LINUX_LEGACY_SYSCALLS
740 linux_utime(struct thread *td, struct linux_utime_args *args)
742 struct timeval tv[2], *tvp;
743 struct l_utimbuf lut;
748 if ((error = copyin(args->times, &lut, sizeof lut)) != 0)
750 tv[0].tv_sec = lut.l_actime;
752 tv[1].tv_sec = lut.l_modtime;
758 if (!LUSECONVPATH(td)) {
759 error = kern_utimesat(td, AT_FDCWD, args->fname, UIO_USERSPACE,
762 LCONVPATHEXIST(args->fname, &fname);
763 error = kern_utimesat(td, AT_FDCWD, fname, UIO_SYSSPACE, tvp,
771 #ifdef LINUX_LEGACY_SYSCALLS
773 linux_utimes(struct thread *td, struct linux_utimes_args *args)
776 struct timeval tv[2], *tvp = NULL;
780 if (args->tptr != NULL) {
781 if ((error = copyin(args->tptr, ltv, sizeof ltv)) != 0)
783 tv[0].tv_sec = ltv[0].tv_sec;
784 tv[0].tv_usec = ltv[0].tv_usec;
785 tv[1].tv_sec = ltv[1].tv_sec;
786 tv[1].tv_usec = ltv[1].tv_usec;
790 if (!LUSECONVPATH(td)) {
791 error = kern_utimesat(td, AT_FDCWD, args->fname, UIO_USERSPACE,
794 LCONVPATHEXIST(args->fname, &fname);
795 error = kern_utimesat(td, AT_FDCWD, fname, UIO_SYSSPACE,
804 linux_utimensat_lts_to_ts(struct l_timespec *l_times, struct timespec *times)
807 if (l_times->tv_nsec != LINUX_UTIME_OMIT &&
808 l_times->tv_nsec != LINUX_UTIME_NOW &&
809 (l_times->tv_nsec < 0 || l_times->tv_nsec > 999999999))
812 times->tv_sec = l_times->tv_sec;
813 switch (l_times->tv_nsec)
815 case LINUX_UTIME_OMIT:
816 times->tv_nsec = UTIME_OMIT;
818 case LINUX_UTIME_NOW:
819 times->tv_nsec = UTIME_NOW;
822 times->tv_nsec = l_times->tv_nsec;
829 linux_common_utimensat(struct thread *td, int ldfd, const char *pathname,
830 struct timespec *timesp, int lflags)
833 int error, dfd, flags = 0;
835 dfd = (ldfd == LINUX_AT_FDCWD) ? AT_FDCWD : ldfd;
837 if (lflags & ~(LINUX_AT_SYMLINK_NOFOLLOW | LINUX_AT_EMPTY_PATH))
840 if (timesp != NULL) {
841 /* This breaks POSIX, but is what the Linux kernel does
842 * _on purpose_ (documented in the man page for utimensat(2)),
843 * so we must follow that behaviour. */
844 if (timesp[0].tv_nsec == UTIME_OMIT &&
845 timesp[1].tv_nsec == UTIME_OMIT)
849 if (lflags & LINUX_AT_SYMLINK_NOFOLLOW)
850 flags |= AT_SYMLINK_NOFOLLOW;
851 if (lflags & LINUX_AT_EMPTY_PATH)
852 flags |= AT_EMPTY_PATH;
854 if (!LUSECONVPATH(td)) {
855 if (pathname != NULL) {
856 return (kern_utimensat(td, dfd, pathname,
857 UIO_USERSPACE, timesp, UIO_SYSSPACE, flags));
861 if (pathname != NULL)
862 LCONVPATHEXIST_AT(pathname, &path, dfd);
863 else if (lflags != 0)
867 error = kern_futimens(td, dfd, timesp, UIO_SYSSPACE);
869 error = kern_utimensat(td, dfd, path, UIO_SYSSPACE, timesp,
870 UIO_SYSSPACE, flags);
878 linux_utimensat(struct thread *td, struct linux_utimensat_args *args)
880 struct l_timespec l_times[2];
881 struct timespec times[2], *timesp;
884 if (args->times != NULL) {
885 error = copyin(args->times, l_times, sizeof(l_times));
889 error = linux_utimensat_lts_to_ts(&l_times[0], ×[0]);
892 error = linux_utimensat_lts_to_ts(&l_times[1], ×[1]);
899 return (linux_common_utimensat(td, args->dfd, args->pathname,
900 timesp, args->flags));
903 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
905 linux_utimensat_lts64_to_ts(struct l_timespec64 *l_times, struct timespec *times)
908 if (l_times->tv_nsec != LINUX_UTIME_OMIT &&
909 l_times->tv_nsec != LINUX_UTIME_NOW &&
910 (l_times->tv_nsec < 0 || l_times->tv_nsec > 999999999))
913 times->tv_sec = l_times->tv_sec;
914 switch (l_times->tv_nsec)
916 case LINUX_UTIME_OMIT:
917 times->tv_nsec = UTIME_OMIT;
919 case LINUX_UTIME_NOW:
920 times->tv_nsec = UTIME_NOW;
923 times->tv_nsec = l_times->tv_nsec;
930 linux_utimensat_time64(struct thread *td, struct linux_utimensat_time64_args *args)
932 struct l_timespec64 l_times[2];
933 struct timespec times[2], *timesp;
936 if (args->times64 != NULL) {
937 error = copyin(args->times64, l_times, sizeof(l_times));
941 error = linux_utimensat_lts64_to_ts(&l_times[0], ×[0]);
944 error = linux_utimensat_lts64_to_ts(&l_times[1], ×[1]);
951 return (linux_common_utimensat(td, args->dfd, args->pathname,
952 timesp, args->flags));
954 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
956 #ifdef LINUX_LEGACY_SYSCALLS
958 linux_futimesat(struct thread *td, struct linux_futimesat_args *args)
961 struct timeval tv[2], *tvp = NULL;
965 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
967 if (args->utimes != NULL) {
968 if ((error = copyin(args->utimes, ltv, sizeof ltv)) != 0)
970 tv[0].tv_sec = ltv[0].tv_sec;
971 tv[0].tv_usec = ltv[0].tv_usec;
972 tv[1].tv_sec = ltv[1].tv_sec;
973 tv[1].tv_usec = ltv[1].tv_usec;
977 if (!LUSECONVPATH(td)) {
978 error = kern_utimesat(td, dfd, args->filename, UIO_USERSPACE,
981 LCONVPATHEXIST_AT(args->filename, &fname, dfd);
982 error = kern_utimesat(td, dfd, fname, UIO_SYSSPACE,
991 linux_common_wait(struct thread *td, idtype_t idtype, int id, int *statusp,
992 int options, void *rup, l_siginfo_t *infop)
996 struct __wrusage wru;
997 int error, status, tmpstat, sig;
999 error = kern_wait6(td, idtype, id, &status, options,
1000 rup != NULL ? &wru : NULL, &siginfo);
1002 if (error == 0 && statusp) {
1003 tmpstat = status & 0xffff;
1004 if (WIFSIGNALED(tmpstat)) {
1005 tmpstat = (tmpstat & 0xffffff80) |
1006 bsd_to_linux_signal(WTERMSIG(tmpstat));
1007 } else if (WIFSTOPPED(tmpstat)) {
1008 tmpstat = (tmpstat & 0xffff00ff) |
1009 (bsd_to_linux_signal(WSTOPSIG(tmpstat)) << 8);
1010 #if defined(__aarch64__) || (defined(__amd64__) && !defined(COMPAT_LINUX32))
1011 if (WSTOPSIG(status) == SIGTRAP) {
1012 tmpstat = linux_ptrace_status(td,
1013 siginfo.si_pid, tmpstat);
1016 } else if (WIFCONTINUED(tmpstat)) {
1019 error = copyout(&tmpstat, statusp, sizeof(int));
1021 if (error == 0 && rup != NULL)
1022 error = linux_copyout_rusage(&wru.wru_self, rup);
1023 if (error == 0 && infop != NULL && td->td_retval[0] != 0) {
1024 sig = bsd_to_linux_signal(siginfo.si_signo);
1025 siginfo_to_lsiginfo(&siginfo, &lsi, sig);
1026 error = copyout(&lsi, infop, sizeof(lsi));
1032 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1034 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
1036 struct linux_wait4_args wait4_args;
1038 wait4_args.pid = args->pid;
1039 wait4_args.status = args->status;
1040 wait4_args.options = args->options;
1041 wait4_args.rusage = NULL;
1043 return (linux_wait4(td, &wait4_args));
1045 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1048 linux_wait4(struct thread *td, struct linux_wait4_args *args)
1051 int options, id, idtype;
1053 if (args->options & ~(LINUX_WUNTRACED | LINUX_WNOHANG |
1054 LINUX_WCONTINUED | __WCLONE | __WNOTHREAD | __WALL))
1058 linux_to_bsd_waitopts(args->options, &options);
1061 * For backward compatibility we implicitly add flags WEXITED
1062 * and WTRAPPED here.
1064 options |= WEXITED | WTRAPPED;
1066 if (args->pid == WAIT_ANY) {
1069 } else if (args->pid < 0) {
1071 id = (id_t)-args->pid;
1072 } else if (args->pid == 0) {
1080 id = (id_t)args->pid;
1083 return (linux_common_wait(td, idtype, id, args->status, options,
1084 args->rusage, NULL));
1088 linux_waitid(struct thread *td, struct linux_waitid_args *args)
1093 if (args->options & ~(LINUX_WNOHANG | LINUX_WNOWAIT | LINUX_WEXITED |
1094 LINUX_WSTOPPED | LINUX_WCONTINUED | __WCLONE | __WNOTHREAD | __WALL))
1098 linux_to_bsd_waitopts(args->options, &options);
1100 switch (args->idtype) {
1118 error = linux_common_wait(td, idtype, args->id, NULL, options,
1119 args->rusage, args->info);
1120 td->td_retval[0] = 0;
1125 #ifdef LINUX_LEGACY_SYSCALLS
1127 linux_mknod(struct thread *td, struct linux_mknod_args *args)
1134 convpath = LUSECONVPATH(td);
1137 seg = UIO_USERSPACE;
1139 LCONVPATHCREAT(args->path, &path);
1143 switch (args->mode & S_IFMT) {
1146 error = kern_mkfifoat(td, AT_FDCWD, path, seg,
1152 error = kern_mknodat(td, AT_FDCWD, path, seg,
1153 args->mode, args->dev);
1161 args->mode |= S_IFREG;
1164 error = kern_openat(td, AT_FDCWD, path, seg,
1165 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1167 kern_close(td, td->td_retval[0]);
1181 linux_mknodat(struct thread *td, struct linux_mknodat_args *args)
1188 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1190 convpath = LUSECONVPATH(td);
1192 path = __DECONST(char *, args->filename);
1193 seg = UIO_USERSPACE;
1195 LCONVPATHCREAT_AT(args->filename, &path, dfd);
1199 switch (args->mode & S_IFMT) {
1202 error = kern_mkfifoat(td, dfd, path, seg, args->mode);
1207 error = kern_mknodat(td, dfd, path, seg, args->mode,
1216 args->mode |= S_IFREG;
1219 error = kern_openat(td, dfd, path, seg,
1220 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1222 kern_close(td, td->td_retval[0]);
1235 * UGH! This is just about the dumbest idea I've ever heard!!
1238 linux_personality(struct thread *td, struct linux_personality_args *args)
1240 struct linux_pemuldata *pem;
1241 struct proc *p = td->td_proc;
1247 if (args->per != 0xffffffff)
1248 pem->persona = args->per;
1251 td->td_retval[0] = old;
1255 struct l_itimerval {
1256 l_timeval it_interval;
1260 #define B2L_ITIMERVAL(bip, lip) \
1261 (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec; \
1262 (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec; \
1263 (bip)->it_value.tv_sec = (lip)->it_value.tv_sec; \
1264 (bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
1267 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
1270 struct l_itimerval ls;
1271 struct itimerval aitv, oitv;
1273 if (uap->itv == NULL) {
1274 uap->itv = uap->oitv;
1275 return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
1278 error = copyin(uap->itv, &ls, sizeof(ls));
1281 B2L_ITIMERVAL(&aitv, &ls);
1282 error = kern_setitimer(td, uap->which, &aitv, &oitv);
1283 if (error != 0 || uap->oitv == NULL)
1285 B2L_ITIMERVAL(&ls, &oitv);
1287 return (copyout(&ls, uap->oitv, sizeof(ls)));
1291 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
1294 struct l_itimerval ls;
1295 struct itimerval aitv;
1297 error = kern_getitimer(td, uap->which, &aitv);
1300 B2L_ITIMERVAL(&ls, &aitv);
1301 return (copyout(&ls, uap->itv, sizeof(ls)));
1304 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1306 linux_nice(struct thread *td, struct linux_nice_args *args)
1309 return (kern_setpriority(td, PRIO_PROCESS, 0, args->inc));
1311 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1314 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
1316 struct ucred *newcred, *oldcred;
1317 l_gid_t *linux_gidset;
1322 ngrp = args->gidsetsize;
1323 if (ngrp < 0 || ngrp >= ngroups_max + 1)
1325 linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_LINUX, M_WAITOK);
1326 error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
1330 crextend(newcred, ngrp + 1);
1333 oldcred = p->p_ucred;
1334 crcopy(newcred, oldcred);
1337 * cr_groups[0] holds egid. Setting the whole set from
1338 * the supplied set will cause egid to be changed too.
1339 * Keep cr_groups[0] unchanged to prevent that.
1342 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS)) != 0) {
1349 newcred->cr_ngroups = ngrp + 1;
1351 bsd_gidset = newcred->cr_groups;
1354 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1358 newcred->cr_ngroups = 1;
1361 proc_set_cred(p, newcred);
1366 free(linux_gidset, M_LINUX);
1371 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1374 l_gid_t *linux_gidset;
1376 int bsd_gidsetsz, ngrp, error;
1378 cred = td->td_ucred;
1379 bsd_gidset = cred->cr_groups;
1380 bsd_gidsetsz = cred->cr_ngroups - 1;
1383 * cr_groups[0] holds egid. Returning the whole set
1384 * here will cause a duplicate. Exclude cr_groups[0]
1388 if ((ngrp = args->gidsetsize) == 0) {
1389 td->td_retval[0] = bsd_gidsetsz;
1393 if (ngrp < bsd_gidsetsz)
1397 linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset),
1399 while (ngrp < bsd_gidsetsz) {
1400 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1404 error = copyout(linux_gidset, args->grouplist, ngrp * sizeof(l_gid_t));
1405 free(linux_gidset, M_LINUX);
1409 td->td_retval[0] = ngrp;
1414 linux_get_dummy_limit(l_uint resource, struct rlimit *rlim)
1417 if (linux_dummy_rlimits == 0)
1421 case LINUX_RLIMIT_LOCKS:
1422 case LINUX_RLIMIT_SIGPENDING:
1423 case LINUX_RLIMIT_MSGQUEUE:
1424 case LINUX_RLIMIT_RTTIME:
1425 rlim->rlim_cur = LINUX_RLIM_INFINITY;
1426 rlim->rlim_max = LINUX_RLIM_INFINITY;
1428 case LINUX_RLIMIT_NICE:
1429 case LINUX_RLIMIT_RTPRIO:
1439 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1441 struct rlimit bsd_rlim;
1442 struct l_rlimit rlim;
1446 if (args->resource >= LINUX_RLIM_NLIMITS)
1449 which = linux_to_bsd_resource[args->resource];
1453 error = copyin(args->rlim, &rlim, sizeof(rlim));
1457 bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1458 bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1459 return (kern_setrlimit(td, which, &bsd_rlim));
1462 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1464 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1466 struct l_rlimit rlim;
1467 struct rlimit bsd_rlim;
1470 if (linux_get_dummy_limit(args->resource, &bsd_rlim)) {
1471 rlim.rlim_cur = bsd_rlim.rlim_cur;
1472 rlim.rlim_max = bsd_rlim.rlim_max;
1473 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1476 if (args->resource >= LINUX_RLIM_NLIMITS)
1479 which = linux_to_bsd_resource[args->resource];
1483 lim_rlimit(td, which, &bsd_rlim);
1485 #ifdef COMPAT_LINUX32
1486 rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1487 if (rlim.rlim_cur == UINT_MAX)
1488 rlim.rlim_cur = INT_MAX;
1489 rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1490 if (rlim.rlim_max == UINT_MAX)
1491 rlim.rlim_max = INT_MAX;
1493 rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1494 if (rlim.rlim_cur == ULONG_MAX)
1495 rlim.rlim_cur = LONG_MAX;
1496 rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1497 if (rlim.rlim_max == ULONG_MAX)
1498 rlim.rlim_max = LONG_MAX;
1500 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1502 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1505 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1507 struct l_rlimit rlim;
1508 struct rlimit bsd_rlim;
1511 if (linux_get_dummy_limit(args->resource, &bsd_rlim)) {
1512 rlim.rlim_cur = bsd_rlim.rlim_cur;
1513 rlim.rlim_max = bsd_rlim.rlim_max;
1514 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1517 if (args->resource >= LINUX_RLIM_NLIMITS)
1520 which = linux_to_bsd_resource[args->resource];
1524 lim_rlimit(td, which, &bsd_rlim);
1526 rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1527 rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1528 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1532 linux_sched_setscheduler(struct thread *td,
1533 struct linux_sched_setscheduler_args *args)
1535 struct sched_param sched_param;
1539 switch (args->policy) {
1540 case LINUX_SCHED_OTHER:
1541 policy = SCHED_OTHER;
1543 case LINUX_SCHED_FIFO:
1544 policy = SCHED_FIFO;
1546 case LINUX_SCHED_RR:
1553 error = copyin(args->param, &sched_param, sizeof(sched_param));
1557 if (linux_map_sched_prio) {
1560 if (sched_param.sched_priority != 0)
1563 sched_param.sched_priority =
1564 PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE;
1568 if (sched_param.sched_priority < 1 ||
1569 sched_param.sched_priority >= LINUX_MAX_RT_PRIO)
1573 * Map [1, LINUX_MAX_RT_PRIO - 1] to
1574 * [0, RTP_PRIO_MAX - RTP_PRIO_MIN] (rounding down).
1576 sched_param.sched_priority =
1577 (sched_param.sched_priority - 1) *
1578 (RTP_PRIO_MAX - RTP_PRIO_MIN + 1) /
1579 (LINUX_MAX_RT_PRIO - 1);
1584 tdt = linux_tdfind(td, args->pid, -1);
1588 error = kern_sched_setscheduler(td, tdt, policy, &sched_param);
1589 PROC_UNLOCK(tdt->td_proc);
1594 linux_sched_getscheduler(struct thread *td,
1595 struct linux_sched_getscheduler_args *args)
1600 tdt = linux_tdfind(td, args->pid, -1);
1604 error = kern_sched_getscheduler(td, tdt, &policy);
1605 PROC_UNLOCK(tdt->td_proc);
1609 td->td_retval[0] = LINUX_SCHED_OTHER;
1612 td->td_retval[0] = LINUX_SCHED_FIFO;
1615 td->td_retval[0] = LINUX_SCHED_RR;
1622 linux_sched_get_priority_max(struct thread *td,
1623 struct linux_sched_get_priority_max_args *args)
1625 struct sched_get_priority_max_args bsd;
1627 if (linux_map_sched_prio) {
1628 switch (args->policy) {
1629 case LINUX_SCHED_OTHER:
1630 td->td_retval[0] = 0;
1632 case LINUX_SCHED_FIFO:
1633 case LINUX_SCHED_RR:
1634 td->td_retval[0] = LINUX_MAX_RT_PRIO - 1;
1641 switch (args->policy) {
1642 case LINUX_SCHED_OTHER:
1643 bsd.policy = SCHED_OTHER;
1645 case LINUX_SCHED_FIFO:
1646 bsd.policy = SCHED_FIFO;
1648 case LINUX_SCHED_RR:
1649 bsd.policy = SCHED_RR;
1654 return (sys_sched_get_priority_max(td, &bsd));
1658 linux_sched_get_priority_min(struct thread *td,
1659 struct linux_sched_get_priority_min_args *args)
1661 struct sched_get_priority_min_args bsd;
1663 if (linux_map_sched_prio) {
1664 switch (args->policy) {
1665 case LINUX_SCHED_OTHER:
1666 td->td_retval[0] = 0;
1668 case LINUX_SCHED_FIFO:
1669 case LINUX_SCHED_RR:
1670 td->td_retval[0] = 1;
1677 switch (args->policy) {
1678 case LINUX_SCHED_OTHER:
1679 bsd.policy = SCHED_OTHER;
1681 case LINUX_SCHED_FIFO:
1682 bsd.policy = SCHED_FIFO;
1684 case LINUX_SCHED_RR:
1685 bsd.policy = SCHED_RR;
1690 return (sys_sched_get_priority_min(td, &bsd));
1693 #define REBOOT_CAD_ON 0x89abcdef
1694 #define REBOOT_CAD_OFF 0
1695 #define REBOOT_HALT 0xcdef0123
1696 #define REBOOT_RESTART 0x01234567
1697 #define REBOOT_RESTART2 0xA1B2C3D4
1698 #define REBOOT_POWEROFF 0x4321FEDC
1699 #define REBOOT_MAGIC1 0xfee1dead
1700 #define REBOOT_MAGIC2 0x28121969
1701 #define REBOOT_MAGIC2A 0x05121996
1702 #define REBOOT_MAGIC2B 0x16041998
1705 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1707 struct reboot_args bsd_args;
1709 if (args->magic1 != REBOOT_MAGIC1)
1712 switch (args->magic2) {
1714 case REBOOT_MAGIC2A:
1715 case REBOOT_MAGIC2B:
1721 switch (args->cmd) {
1723 case REBOOT_CAD_OFF:
1724 return (priv_check(td, PRIV_REBOOT));
1726 bsd_args.opt = RB_HALT;
1728 case REBOOT_RESTART:
1729 case REBOOT_RESTART2:
1732 case REBOOT_POWEROFF:
1733 bsd_args.opt = RB_POWEROFF;
1738 return (sys_reboot(td, &bsd_args));
1742 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1745 td->td_retval[0] = td->td_proc->p_pid;
1751 linux_gettid(struct thread *td, struct linux_gettid_args *args)
1753 struct linux_emuldata *em;
1756 KASSERT(em != NULL, ("gettid: emuldata not found.\n"));
1758 td->td_retval[0] = em->em_tid;
1764 linux_getppid(struct thread *td, struct linux_getppid_args *args)
1767 td->td_retval[0] = kern_getppid(td);
1772 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1775 td->td_retval[0] = td->td_ucred->cr_rgid;
1780 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1783 td->td_retval[0] = td->td_ucred->cr_ruid;
1788 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1791 return (kern_getsid(td, args->pid));
1795 linux_nosys(struct thread *td, struct nosys_args *ignore)
1802 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1806 error = kern_getpriority(td, args->which, args->who);
1807 td->td_retval[0] = 20 - td->td_retval[0];
1812 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1817 name[1] = KERN_HOSTNAME;
1818 return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1823 linux_setdomainname(struct thread *td, struct linux_setdomainname_args *args)
1828 name[1] = KERN_NISDOMAINNAME;
1829 return (userland_sysctl(td, name, 2, 0, 0, 0, args->name,
1834 linux_exit_group(struct thread *td, struct linux_exit_group_args *args)
1837 LINUX_CTR2(exit_group, "thread(%d) (%d)", td->td_tid,
1841 * XXX: we should send a signal to the parent if
1842 * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?)
1843 * as it doesnt occur often.
1845 exit1(td, args->error_code, 0);
1849 #define _LINUX_CAPABILITY_VERSION_1 0x19980330
1850 #define _LINUX_CAPABILITY_VERSION_2 0x20071026
1851 #define _LINUX_CAPABILITY_VERSION_3 0x20080522
1853 struct l_user_cap_header {
1858 struct l_user_cap_data {
1865 linux_capget(struct thread *td, struct linux_capget_args *uap)
1867 struct l_user_cap_header luch;
1868 struct l_user_cap_data lucd[2];
1871 if (uap->hdrp == NULL)
1874 error = copyin(uap->hdrp, &luch, sizeof(luch));
1878 switch (luch.version) {
1879 case _LINUX_CAPABILITY_VERSION_1:
1882 case _LINUX_CAPABILITY_VERSION_2:
1883 case _LINUX_CAPABILITY_VERSION_3:
1887 luch.version = _LINUX_CAPABILITY_VERSION_1;
1888 error = copyout(&luch, uap->hdrp, sizeof(luch));
1899 * The current implementation doesn't support setting
1900 * a capability (it's essentially a stub) so indicate
1901 * that no capabilities are currently set or available
1904 memset(&lucd, 0, u32s * sizeof(lucd[0]));
1905 error = copyout(&lucd, uap->datap, u32s * sizeof(lucd[0]));
1912 linux_capset(struct thread *td, struct linux_capset_args *uap)
1914 struct l_user_cap_header luch;
1915 struct l_user_cap_data lucd[2];
1918 if (uap->hdrp == NULL || uap->datap == NULL)
1921 error = copyin(uap->hdrp, &luch, sizeof(luch));
1925 switch (luch.version) {
1926 case _LINUX_CAPABILITY_VERSION_1:
1929 case _LINUX_CAPABILITY_VERSION_2:
1930 case _LINUX_CAPABILITY_VERSION_3:
1934 luch.version = _LINUX_CAPABILITY_VERSION_1;
1935 error = copyout(&luch, uap->hdrp, sizeof(luch));
1944 error = copyin(uap->datap, &lucd, u32s * sizeof(lucd[0]));
1948 /* We currently don't support setting any capabilities. */
1949 for (i = 0; i < u32s; i++) {
1950 if (lucd[i].effective || lucd[i].permitted ||
1951 lucd[i].inheritable) {
1953 "capset[%d] effective=0x%x, permitted=0x%x, "
1954 "inheritable=0x%x is not implemented", i,
1955 (int)lucd[i].effective, (int)lucd[i].permitted,
1956 (int)lucd[i].inheritable);
1965 linux_prctl(struct thread *td, struct linux_prctl_args *args)
1967 int error = 0, max_size, arg;
1968 struct proc *p = td->td_proc;
1969 char comm[LINUX_MAX_COMM_LEN];
1970 int pdeath_signal, trace_state;
1972 switch (args->option) {
1973 case LINUX_PR_SET_PDEATHSIG:
1974 if (!LINUX_SIG_VALID(args->arg2))
1976 pdeath_signal = linux_to_bsd_signal(args->arg2);
1977 return (kern_procctl(td, P_PID, 0, PROC_PDEATHSIG_CTL,
1979 case LINUX_PR_GET_PDEATHSIG:
1980 error = kern_procctl(td, P_PID, 0, PROC_PDEATHSIG_STATUS,
1984 pdeath_signal = bsd_to_linux_signal(pdeath_signal);
1985 return (copyout(&pdeath_signal,
1986 (void *)(register_t)args->arg2,
1987 sizeof(pdeath_signal)));
1989 * In Linux, this flag controls if set[gu]id processes can coredump.
1990 * There are additional semantics imposed on processes that cannot
1992 * - Such processes can not be ptraced.
1993 * - There are some semantics around ownership of process-related files
1994 * in the /proc namespace.
1996 * In FreeBSD, we can (and by default, do) disable setuid coredump
1997 * system-wide with 'sugid_coredump.' We control tracability on a
1998 * per-process basis with the procctl PROC_TRACE (=> P2_NOTRACE flag).
1999 * By happy coincidence, P2_NOTRACE also prevents coredumping. So the
2000 * procctl is roughly analogous to Linux's DUMPABLE.
2002 * So, proxy these knobs to the corresponding PROC_TRACE setting.
2004 case LINUX_PR_GET_DUMPABLE:
2005 error = kern_procctl(td, P_PID, p->p_pid, PROC_TRACE_STATUS,
2009 td->td_retval[0] = (trace_state != -1);
2011 case LINUX_PR_SET_DUMPABLE:
2013 * It is only valid for userspace to set one of these two
2014 * flags, and only one at a time.
2016 switch (args->arg2) {
2017 case LINUX_SUID_DUMP_DISABLE:
2018 trace_state = PROC_TRACE_CTL_DISABLE_EXEC;
2020 case LINUX_SUID_DUMP_USER:
2021 trace_state = PROC_TRACE_CTL_ENABLE;
2026 return (kern_procctl(td, P_PID, p->p_pid, PROC_TRACE_CTL,
2028 case LINUX_PR_GET_KEEPCAPS:
2030 * Indicate that we always clear the effective and
2031 * permitted capability sets when the user id becomes
2032 * non-zero (actually the capability sets are simply
2033 * always zero in the current implementation).
2035 td->td_retval[0] = 0;
2037 case LINUX_PR_SET_KEEPCAPS:
2039 * Ignore requests to keep the effective and permitted
2040 * capability sets when the user id becomes non-zero.
2043 case LINUX_PR_SET_NAME:
2045 * To be on the safe side we need to make sure to not
2046 * overflow the size a Linux program expects. We already
2047 * do this here in the copyin, so that we don't need to
2050 max_size = MIN(sizeof(comm), sizeof(p->p_comm));
2051 error = copyinstr((void *)(register_t)args->arg2, comm,
2054 /* Linux silently truncates the name if it is too long. */
2055 if (error == ENAMETOOLONG) {
2057 * XXX: copyinstr() isn't documented to populate the
2058 * array completely, so do a copyin() to be on the
2059 * safe side. This should be changed in case
2060 * copyinstr() is changed to guarantee this.
2062 error = copyin((void *)(register_t)args->arg2, comm,
2064 comm[max_size - 1] = '\0';
2070 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
2073 case LINUX_PR_GET_NAME:
2075 strlcpy(comm, p->p_comm, sizeof(comm));
2077 error = copyout(comm, (void *)(register_t)args->arg2,
2080 case LINUX_PR_GET_SECCOMP:
2081 case LINUX_PR_SET_SECCOMP:
2083 * Same as returned by Linux without CONFIG_SECCOMP enabled.
2087 case LINUX_PR_CAPBSET_READ:
2090 * This makes too much noise with Ubuntu Focal.
2092 linux_msg(td, "unsupported prctl PR_CAPBSET_READ %d",
2097 case LINUX_PR_SET_NO_NEW_PRIVS:
2098 arg = args->arg2 == 1 ?
2099 PROC_NO_NEW_PRIVS_ENABLE : PROC_NO_NEW_PRIVS_DISABLE;
2100 error = kern_procctl(td, P_PID, p->p_pid,
2101 PROC_NO_NEW_PRIVS_CTL, &arg);
2103 case LINUX_PR_SET_PTRACER:
2104 linux_msg(td, "unsupported prctl PR_SET_PTRACER");
2108 linux_msg(td, "unsupported prctl option %d", args->option);
2117 linux_sched_setparam(struct thread *td,
2118 struct linux_sched_setparam_args *uap)
2120 struct sched_param sched_param;
2124 error = copyin(uap->param, &sched_param, sizeof(sched_param));
2128 tdt = linux_tdfind(td, uap->pid, -1);
2132 if (linux_map_sched_prio) {
2133 error = kern_sched_getscheduler(td, tdt, &policy);
2139 if (sched_param.sched_priority != 0) {
2143 sched_param.sched_priority =
2144 PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE;
2148 if (sched_param.sched_priority < 1 ||
2149 sched_param.sched_priority >= LINUX_MAX_RT_PRIO) {
2154 * Map [1, LINUX_MAX_RT_PRIO - 1] to
2155 * [0, RTP_PRIO_MAX - RTP_PRIO_MIN] (rounding down).
2157 sched_param.sched_priority =
2158 (sched_param.sched_priority - 1) *
2159 (RTP_PRIO_MAX - RTP_PRIO_MIN + 1) /
2160 (LINUX_MAX_RT_PRIO - 1);
2165 error = kern_sched_setparam(td, tdt, &sched_param);
2166 out: PROC_UNLOCK(tdt->td_proc);
2171 linux_sched_getparam(struct thread *td,
2172 struct linux_sched_getparam_args *uap)
2174 struct sched_param sched_param;
2178 tdt = linux_tdfind(td, uap->pid, -1);
2182 error = kern_sched_getparam(td, tdt, &sched_param);
2184 PROC_UNLOCK(tdt->td_proc);
2188 if (linux_map_sched_prio) {
2189 error = kern_sched_getscheduler(td, tdt, &policy);
2190 PROC_UNLOCK(tdt->td_proc);
2196 sched_param.sched_priority = 0;
2201 * Map [0, RTP_PRIO_MAX - RTP_PRIO_MIN] to
2202 * [1, LINUX_MAX_RT_PRIO - 1] (rounding up).
2204 sched_param.sched_priority =
2205 (sched_param.sched_priority *
2206 (LINUX_MAX_RT_PRIO - 1) +
2207 (RTP_PRIO_MAX - RTP_PRIO_MIN - 1)) /
2208 (RTP_PRIO_MAX - RTP_PRIO_MIN) + 1;
2212 PROC_UNLOCK(tdt->td_proc);
2214 error = copyout(&sched_param, uap->param, sizeof(sched_param));
2219 * Get affinity of a process.
2222 linux_sched_getaffinity(struct thread *td,
2223 struct linux_sched_getaffinity_args *args)
2228 if (args->len < sizeof(cpuset_t))
2231 tdt = linux_tdfind(td, args->pid, -1);
2235 PROC_UNLOCK(tdt->td_proc);
2237 error = kern_cpuset_getaffinity(td, CPU_LEVEL_WHICH, CPU_WHICH_TID,
2238 tdt->td_tid, sizeof(cpuset_t), (cpuset_t *)args->user_mask_ptr);
2240 td->td_retval[0] = sizeof(cpuset_t);
2246 * Set affinity of a process.
2249 linux_sched_setaffinity(struct thread *td,
2250 struct linux_sched_setaffinity_args *args)
2254 if (args->len < sizeof(cpuset_t))
2257 tdt = linux_tdfind(td, args->pid, -1);
2261 PROC_UNLOCK(tdt->td_proc);
2263 return (kern_cpuset_setaffinity(td, CPU_LEVEL_WHICH, CPU_WHICH_TID,
2264 tdt->td_tid, sizeof(cpuset_t), (cpuset_t *) args->user_mask_ptr));
2267 struct linux_rlimit64 {
2273 linux_prlimit64(struct thread *td, struct linux_prlimit64_args *args)
2275 struct rlimit rlim, nrlim;
2276 struct linux_rlimit64 lrlim;
2282 if (args->new == NULL && args->old != NULL) {
2283 if (linux_get_dummy_limit(args->resource, &rlim)) {
2284 lrlim.rlim_cur = rlim.rlim_cur;
2285 lrlim.rlim_max = rlim.rlim_max;
2286 return (copyout(&lrlim, args->old, sizeof(lrlim)));
2290 if (args->resource >= LINUX_RLIM_NLIMITS)
2293 which = linux_to_bsd_resource[args->resource];
2297 if (args->new != NULL) {
2299 * Note. Unlike FreeBSD where rlim is signed 64-bit Linux
2300 * rlim is unsigned 64-bit. FreeBSD treats negative limits
2301 * as INFINITY so we do not need a conversion even.
2303 error = copyin(args->new, &nrlim, sizeof(nrlim));
2308 flags = PGET_HOLD | PGET_NOTWEXIT;
2309 if (args->new != NULL)
2310 flags |= PGET_CANDEBUG;
2312 flags |= PGET_CANSEE;
2313 if (args->pid == 0) {
2317 error = pget(args->pid, flags, &p);
2321 if (args->old != NULL) {
2323 lim_rlimit_proc(p, which, &rlim);
2325 if (rlim.rlim_cur == RLIM_INFINITY)
2326 lrlim.rlim_cur = LINUX_RLIM_INFINITY;
2328 lrlim.rlim_cur = rlim.rlim_cur;
2329 if (rlim.rlim_max == RLIM_INFINITY)
2330 lrlim.rlim_max = LINUX_RLIM_INFINITY;
2332 lrlim.rlim_max = rlim.rlim_max;
2333 error = copyout(&lrlim, args->old, sizeof(lrlim));
2338 if (args->new != NULL)
2339 error = kern_proc_setrlimit(td, p, which, &nrlim);
2347 linux_pselect6(struct thread *td, struct linux_pselect6_args *args)
2349 struct l_timespec lts;
2350 struct timespec ts, *tsp;
2353 if (args->tsp != NULL) {
2354 error = copyin(args->tsp, <s, sizeof(lts));
2357 error = linux_to_native_timespec(&ts, <s);
2364 error = linux_common_pselect6(td, args->nfds, args->readfds,
2365 args->writefds, args->exceptfds, tsp, args->sig);
2369 if (args->tsp != NULL) {
2370 error = native_to_linux_timespec(<s, tsp);
2372 error = copyout(<s, args->tsp, sizeof(lts));
2378 linux_common_pselect6(struct thread *td, l_int nfds, l_fd_set *readfds,
2379 l_fd_set *writefds, l_fd_set *exceptfds, struct timespec *tsp,
2382 struct timeval utv, tv0, tv1, *tvp;
2383 struct l_pselect6arg lpse6;
2391 error = copyin(sig, &lpse6, sizeof(lpse6));
2394 if (lpse6.ss_len != sizeof(l_ss))
2396 if (lpse6.ss != 0) {
2397 error = copyin(PTRIN(lpse6.ss), &l_ss,
2401 linux_to_bsd_sigset(&l_ss, &ss);
2408 * Currently glibc changes nanosecond number to microsecond.
2409 * This mean losing precision but for now it is hardly seen.
2412 TIMESPEC_TO_TIMEVAL(&utv, tsp);
2413 if (itimerfix(&utv))
2421 error = kern_pselect(td, nfds, readfds, writefds,
2422 exceptfds, tvp, ssp, LINUX_NFDBITS);
2424 if (error == 0 && tsp != NULL) {
2425 if (td->td_retval[0] != 0) {
2427 * Compute how much time was left of the timeout,
2428 * by subtracting the current time and the time
2429 * before we started the call, and subtracting
2430 * that result from the user-supplied value.
2434 timevalsub(&tv1, &tv0);
2435 timevalsub(&utv, &tv1);
2440 TIMEVAL_TO_TIMESPEC(&utv, tsp);
2445 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
2447 linux_pselect6_time64(struct thread *td,
2448 struct linux_pselect6_time64_args *args)
2450 struct l_timespec64 lts;
2451 struct timespec ts, *tsp;
2454 if (args->tsp != NULL) {
2455 error = copyin(args->tsp, <s, sizeof(lts));
2458 error = linux_to_native_timespec64(&ts, <s);
2465 error = linux_common_pselect6(td, args->nfds, args->readfds,
2466 args->writefds, args->exceptfds, tsp, args->sig);
2470 if (args->tsp != NULL) {
2471 error = native_to_linux_timespec64(<s, tsp);
2473 error = copyout(<s, args->tsp, sizeof(lts));
2477 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
2480 linux_ppoll(struct thread *td, struct linux_ppoll_args *args)
2482 struct timespec uts, *tsp;
2483 struct l_timespec lts;
2486 if (args->tsp != NULL) {
2487 error = copyin(args->tsp, <s, sizeof(lts));
2490 error = linux_to_native_timespec(&uts, <s);
2497 error = linux_common_ppoll(td, args->fds, args->nfds, tsp,
2498 args->sset, args->ssize);
2502 error = native_to_linux_timespec(<s, tsp);
2504 error = copyout(<s, args->tsp, sizeof(lts));
2510 linux_common_ppoll(struct thread *td, struct pollfd *fds, uint32_t nfds,
2511 struct timespec *tsp, l_sigset_t *sset, l_size_t ssize)
2513 struct timespec ts0, ts1;
2514 struct pollfd stackfds[32];
2515 struct pollfd *kfds;
2521 if (kern_poll_maxfds(nfds))
2524 if (ssize != sizeof(l_ss))
2526 error = copyin(sset, &l_ss, sizeof(l_ss));
2529 linux_to_bsd_sigset(&l_ss, &ss);
2536 if (nfds > nitems(stackfds))
2537 kfds = mallocarray(nfds, sizeof(*kfds), M_TEMP, M_WAITOK);
2540 error = linux_pollin(td, kfds, fds, nfds);
2544 error = kern_poll_kfds(td, kfds, nfds, tsp, ssp);
2546 error = linux_pollout(td, kfds, fds, nfds);
2548 if (error == 0 && tsp != NULL) {
2549 if (td->td_retval[0]) {
2551 timespecsub(&ts1, &ts0, &ts1);
2552 timespecsub(tsp, &ts1, tsp);
2553 if (tsp->tv_sec < 0)
2560 if (nfds > nitems(stackfds))
2565 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
2567 linux_ppoll_time64(struct thread *td, struct linux_ppoll_time64_args *args)
2569 struct timespec uts, *tsp;
2570 struct l_timespec64 lts;
2573 if (args->tsp != NULL) {
2574 error = copyin(args->tsp, <s, sizeof(lts));
2577 error = linux_to_native_timespec64(&uts, <s);
2583 error = linux_common_ppoll(td, args->fds, args->nfds, tsp,
2584 args->sset, args->ssize);
2588 error = native_to_linux_timespec64(<s, tsp);
2590 error = copyout(<s, args->tsp, sizeof(lts));
2594 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
2597 linux_pollin(struct thread *td, struct pollfd *fds, struct pollfd *ufds, u_int nfd)
2602 error = copyin(ufds, fds, nfd * sizeof(*fds));
2606 for (i = 0; i < nfd; i++) {
2607 if (fds->events != 0)
2608 linux_to_bsd_poll_events(td, fds->fd,
2609 fds->events, &fds->events);
2616 linux_pollout(struct thread *td, struct pollfd *fds, struct pollfd *ufds, u_int nfd)
2621 for (i = 0; i < nfd; i++) {
2622 if (fds->revents != 0) {
2623 bsd_to_linux_poll_events(fds->revents,
2627 error = copyout(&fds->revents, &ufds->revents,
2628 sizeof(ufds->revents));
2634 td->td_retval[0] = n;
2639 linux_sched_rr_get_interval(struct thread *td,
2640 struct linux_sched_rr_get_interval_args *uap)
2643 struct l_timespec lts;
2648 * According to man in case the invalid pid specified
2649 * EINVAL should be returned.
2654 tdt = linux_tdfind(td, uap->pid, -1);
2658 error = kern_sched_rr_get_interval_td(td, tdt, &ts);
2659 PROC_UNLOCK(tdt->td_proc);
2662 error = native_to_linux_timespec(<s, &ts);
2665 return (copyout(<s, uap->interval, sizeof(lts)));
2669 * In case when the Linux thread is the initial thread in
2670 * the thread group thread id is equal to the process id.
2671 * Glibc depends on this magic (assert in pthread_getattr_np.c).
2674 linux_tdfind(struct thread *td, lwpid_t tid, pid_t pid)
2676 struct linux_emuldata *em;
2681 if (tid == 0 || tid == td->td_tid) {
2683 PROC_LOCK(tdt->td_proc);
2684 } else if (tid > PID_MAX)
2685 tdt = tdfind(tid, pid);
2688 * Initial thread where the tid equal to the pid.
2692 if (SV_PROC_ABI(p) != SV_ABI_LINUX) {
2694 * p is not a Linuxulator process.
2699 FOREACH_THREAD_IN_PROC(p, tdt) {
2701 if (tid == em->em_tid)
2713 linux_to_bsd_waitopts(int options, int *bsdopts)
2716 if (options & LINUX_WNOHANG)
2717 *bsdopts |= WNOHANG;
2718 if (options & LINUX_WUNTRACED)
2719 *bsdopts |= WUNTRACED;
2720 if (options & LINUX_WEXITED)
2721 *bsdopts |= WEXITED;
2722 if (options & LINUX_WCONTINUED)
2723 *bsdopts |= WCONTINUED;
2724 if (options & LINUX_WNOWAIT)
2725 *bsdopts |= WNOWAIT;
2727 if (options & __WCLONE)
2728 *bsdopts |= WLINUXCLONE;
2732 linux_getrandom(struct thread *td, struct linux_getrandom_args *args)
2738 if (args->flags & ~(LINUX_GRND_NONBLOCK|LINUX_GRND_RANDOM))
2740 if (args->count > INT_MAX)
2741 args->count = INT_MAX;
2743 iov.iov_base = args->buf;
2744 iov.iov_len = args->count;
2748 uio.uio_resid = iov.iov_len;
2749 uio.uio_segflg = UIO_USERSPACE;
2750 uio.uio_rw = UIO_READ;
2753 error = read_random_uio(&uio, args->flags & LINUX_GRND_NONBLOCK);
2755 td->td_retval[0] = args->count - uio.uio_resid;
2760 linux_mincore(struct thread *td, struct linux_mincore_args *args)
2763 /* Needs to be page-aligned */
2764 if (args->start & PAGE_MASK)
2766 return (kern_mincore(td, args->start, args->len, args->vec));
2769 #define SYSLOG_TAG "<6>"
2772 linux_syslog(struct thread *td, struct linux_syslog_args *args)
2774 char buf[128], *src, *dst;
2778 if (args->type != LINUX_SYSLOG_ACTION_READ_ALL) {
2779 linux_msg(td, "syslog unsupported type 0x%x", args->type);
2783 if (args->len < 6) {
2784 td->td_retval[0] = 0;
2788 error = priv_check(td, PRIV_MSGBUF);
2792 mtx_lock(&msgbuf_lock);
2793 msgbuf_peekbytes(msgbufp, NULL, 0, &seq);
2794 mtx_unlock(&msgbuf_lock);
2797 error = copyout(&SYSLOG_TAG, dst, sizeof(SYSLOG_TAG));
2798 /* The -1 is to skip the trailing '\0'. */
2799 dst += sizeof(SYSLOG_TAG) - 1;
2801 while (error == 0) {
2802 mtx_lock(&msgbuf_lock);
2803 buflen = msgbuf_peekbytes(msgbufp, buf, sizeof(buf), &seq);
2804 mtx_unlock(&msgbuf_lock);
2809 for (src = buf; src < buf + buflen && error == 0; src++) {
2813 if (dst >= args->buf + args->len)
2816 error = copyout(src, dst, 1);
2819 if (*src == '\n' && *(src + 1) != '<' &&
2820 dst + sizeof(SYSLOG_TAG) < args->buf + args->len) {
2821 error = copyout(&SYSLOG_TAG,
2822 dst, sizeof(SYSLOG_TAG));
2823 dst += sizeof(SYSLOG_TAG) - 1;
2828 td->td_retval[0] = dst - args->buf;
2833 linux_getcpu(struct thread *td, struct linux_getcpu_args *args)
2835 int cpu, error, node;
2837 cpu = td->td_oncpu; /* Make sure it doesn't change during copyout(9) */
2839 node = cpuid_to_pcpu[cpu]->pc_domain;
2841 if (args->cpu != NULL)
2842 error = copyout(&cpu, args->cpu, sizeof(l_int));
2843 if (args->node != NULL)
2844 error = copyout(&node, args->node, sizeof(l_int));
2848 #if defined(__i386__) || defined(__amd64__)
2850 linux_poll(struct thread *td, struct linux_poll_args *args)
2852 struct timespec ts, *tsp;
2854 if (args->timeout != INFTIM) {
2855 if (args->timeout < 0)
2857 ts.tv_sec = args->timeout / 1000;
2858 ts.tv_nsec = (args->timeout % 1000) * 1000000;
2863 return (linux_common_ppoll(td, args->fds, args->nfds,
2866 #endif /* __i386__ || __amd64__ */
2869 linux_seccomp(struct thread *td, struct linux_seccomp_args *args)
2873 case LINUX_SECCOMP_GET_ACTION_AVAIL:
2874 return (EOPNOTSUPP);
2877 * Ignore unknown operations, just like Linux kernel built
2878 * without CONFIG_SECCOMP.