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/mutex.h>
51 #include <sys/namei.h>
54 #include <sys/procctl.h>
55 #include <sys/reboot.h>
56 #include <sys/racct.h>
57 #include <sys/random.h>
58 #include <sys/resourcevar.h>
59 #include <sys/sched.h>
61 #include <sys/signalvar.h>
63 #include <sys/syscallsubr.h>
64 #include <sys/sysctl.h>
65 #include <sys/sysproto.h>
66 #include <sys/systm.h>
68 #include <sys/vmmeter.h>
69 #include <sys/vnode.h>
71 #include <sys/cpuset.h>
74 #include <security/mac/mac_framework.h>
78 #include <vm/vm_kern.h>
79 #include <vm/vm_map.h>
80 #include <vm/vm_extern.h>
81 #include <vm/vm_object.h>
82 #include <vm/swap_pager.h>
85 #include <machine/../linux32/linux.h>
86 #include <machine/../linux32/linux32_proto.h>
88 #include <machine/../linux/linux.h>
89 #include <machine/../linux/linux_proto.h>
92 #include <compat/linux/linux_dtrace.h>
93 #include <compat/linux/linux_file.h>
94 #include <compat/linux/linux_mib.h>
95 #include <compat/linux/linux_signal.h>
96 #include <compat/linux/linux_timer.h>
97 #include <compat/linux/linux_util.h>
98 #include <compat/linux/linux_sysproto.h>
99 #include <compat/linux/linux_emul.h>
100 #include <compat/linux/linux_misc.h>
103 * Special DTrace provider for the linuxulator.
105 * In this file we define the provider for the entire linuxulator. All
106 * modules (= files of the linuxulator) use it.
108 * We define a different name depending on the emulated bitsize, see
109 * ../../<ARCH>/linux{,32}/linux.h, e.g.:
110 * native bitsize = linuxulator
111 * amd64, 32bit emulation = linuxulator32
113 LIN_SDT_PROVIDER_DEFINE(LINUX_DTRACE);
115 int stclohz; /* Statistics clock frequency */
117 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
118 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
119 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
120 RLIMIT_MEMLOCK, RLIMIT_AS
124 l_long uptime; /* Seconds since boot */
125 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */
126 #define LINUX_SYSINFO_LOADS_SCALE 65536
127 l_ulong totalram; /* Total usable main memory size */
128 l_ulong freeram; /* Available memory size */
129 l_ulong sharedram; /* Amount of shared memory */
130 l_ulong bufferram; /* Memory used by buffers */
131 l_ulong totalswap; /* Total swap space size */
132 l_ulong freeswap; /* swap space still available */
133 l_ushort procs; /* Number of current processes */
138 char _f[20-2*sizeof(l_long)-sizeof(l_int)]; /* padding */
141 struct l_pselect6arg {
146 static int linux_utimensat_nsec_valid(l_long);
150 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args)
152 struct l_sysinfo sysinfo;
157 bzero(&sysinfo, sizeof(sysinfo));
161 sysinfo.uptime = ts.tv_sec;
163 /* Use the information from the mib to get our load averages */
164 for (i = 0; i < 3; i++)
165 sysinfo.loads[i] = averunnable.ldavg[i] *
166 LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale;
168 sysinfo.totalram = physmem * PAGE_SIZE;
169 sysinfo.freeram = sysinfo.totalram - vm_wire_count() * PAGE_SIZE;
171 sysinfo.sharedram = 0;
172 mtx_lock(&vm_object_list_mtx);
173 TAILQ_FOREACH(object, &vm_object_list, object_list)
174 if (object->shadow_count > 1)
175 sysinfo.sharedram += object->resident_page_count;
176 mtx_unlock(&vm_object_list_mtx);
178 sysinfo.sharedram *= PAGE_SIZE;
179 sysinfo.bufferram = 0;
181 swap_pager_status(&i, &j);
182 sysinfo.totalswap = i * PAGE_SIZE;
183 sysinfo.freeswap = (i - j) * PAGE_SIZE;
185 sysinfo.procs = nprocs;
187 /* The following are only present in newer Linux kernels. */
188 sysinfo.totalbig = 0;
190 sysinfo.mem_unit = 1;
192 return (copyout(&sysinfo, args->info, sizeof(sysinfo)));
195 #ifdef LINUX_LEGACY_SYSCALLS
197 linux_alarm(struct thread *td, struct linux_alarm_args *args)
199 struct itimerval it, old_it;
205 * Linux alarm() is always successful. Limit secs to INT32_MAX / 2
206 * to match kern_setitimer()'s limit to avoid error from it.
208 * XXX. Linux limit secs to INT_MAX on 32 and does not limit on 64-bit
211 if (secs > INT32_MAX / 2)
212 secs = INT32_MAX / 2;
214 it.it_value.tv_sec = secs;
215 it.it_value.tv_usec = 0;
216 timevalclear(&it.it_interval);
217 error = kern_setitimer(td, ITIMER_REAL, &it, &old_it);
218 KASSERT(error == 0, ("kern_setitimer returns %d", error));
220 if ((old_it.it_value.tv_sec == 0 && old_it.it_value.tv_usec > 0) ||
221 old_it.it_value.tv_usec >= 500000)
222 old_it.it_value.tv_sec++;
223 td->td_retval[0] = old_it.it_value.tv_sec;
229 linux_brk(struct thread *td, struct linux_brk_args *args)
231 struct vmspace *vm = td->td_proc->p_vmspace;
234 old = (uintptr_t)vm->vm_daddr + ctob(vm->vm_dsize);
235 new = (uintptr_t)args->dsend;
236 if ((caddr_t)new > vm->vm_daddr && !kern_break(td, &new))
237 td->td_retval[0] = (register_t)new;
239 td->td_retval[0] = (register_t)old;
244 #if defined(__i386__)
245 /* XXX: what about amd64/linux32? */
248 linux_uselib(struct thread *td, struct linux_uselib_args *args)
254 vm_map_entry_t entry;
257 unsigned long file_offset;
258 unsigned long bss_size;
262 bool locked, opened, textset;
264 LCONVPATHEXIST(td, args->library, &library);
272 NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | AUDITVNODE1,
273 UIO_SYSSPACE, library, td);
280 NDFREE(&ni, NDF_ONLY_PNBUF);
283 * From here on down, we have a locked vnode that must be unlocked.
284 * XXX: The code below largely duplicates exec_check_permissions().
289 error = VOP_GETATTR(vp, &attr, td->td_ucred);
293 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
294 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
295 /* EACCESS is what exec(2) returns. */
301 if (attr.va_size == 0) {
306 /* Can we access it? */
307 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
312 * XXX: This should use vn_open() so that it is properly authorized,
313 * and to reduce code redundancy all over the place here.
314 * XXX: Not really, it duplicates far more of exec_check_permissions()
318 error = mac_vnode_check_open(td->td_ucred, vp, VREAD);
322 error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL);
327 /* Pull in executable header into exec_map */
328 error = vm_mmap(exec_map, (vm_offset_t *)&a_out, PAGE_SIZE,
329 VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 0);
333 /* Is it a Linux binary ? */
334 if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
340 * While we are here, we should REALLY do some more checks
343 /* Set file/virtual offset based on a.out variant. */
344 switch ((int)(a_out->a_magic & 0xffff)) {
345 case 0413: /* ZMAGIC */
348 case 0314: /* QMAGIC */
356 bss_size = round_page(a_out->a_bss);
358 /* Check various fields in header for validity/bounds. */
359 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
364 /* text + data can't exceed file size */
365 if (a_out->a_data + a_out->a_text > attr.va_size) {
371 * text/data/bss must not exceed limits
372 * XXX - this is not complete. it should check current usage PLUS
373 * the resources needed by this library.
375 PROC_LOCK(td->td_proc);
376 if (a_out->a_text > maxtsiz ||
377 a_out->a_data + bss_size > lim_cur_proc(td->td_proc, RLIMIT_DATA) ||
378 racct_set(td->td_proc, RACCT_DATA, a_out->a_data +
380 PROC_UNLOCK(td->td_proc);
384 PROC_UNLOCK(td->td_proc);
387 * Prevent more writers.
389 error = VOP_SET_TEXT(vp);
395 * Lock no longer needed
401 * Check if file_offset page aligned. Currently we cannot handle
402 * misalinged file offsets, and so we read in the entire image
405 if (file_offset & PAGE_MASK) {
406 /* Map text+data read/write/execute */
408 /* a_entry is the load address and is page aligned */
409 vmaddr = trunc_page(a_out->a_entry);
411 /* get anon user mapping, read+write+execute */
412 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
413 &vmaddr, a_out->a_text + a_out->a_data, 0, VMFS_NO_SPACE,
414 VM_PROT_ALL, VM_PROT_ALL, 0);
418 error = vn_rdwr(UIO_READ, vp, (void *)vmaddr, file_offset,
419 a_out->a_text + a_out->a_data, UIO_USERSPACE, 0,
420 td->td_ucred, NOCRED, &aresid, td);
429 * for QMAGIC, a_entry is 20 bytes beyond the load address
430 * to skip the executable header
432 vmaddr = trunc_page(a_out->a_entry);
435 * Map it all into the process's space as a single
436 * copy-on-write "data" segment.
438 map = &td->td_proc->p_vmspace->vm_map;
439 error = vm_mmap(map, &vmaddr,
440 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
441 MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset);
445 if (!vm_map_lookup_entry(map, vmaddr, &entry)) {
450 entry->eflags |= MAP_ENTRY_VN_EXEC;
456 /* Calculate BSS start address */
457 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
460 /* allocate some 'anon' space */
461 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
462 &vmaddr, bss_size, 0, VMFS_NO_SPACE, VM_PROT_ALL,
473 VOP_CLOSE(vp, FREAD, td->td_ucred, td);
476 VOP_UNSET_TEXT_CHECKED(vp);
480 /* Release the temporary mapping. */
482 kmap_free_wakeup(exec_map, (vm_offset_t)a_out, PAGE_SIZE);
487 #endif /* __i386__ */
489 #ifdef LINUX_LEGACY_SYSCALLS
491 linux_select(struct thread *td, struct linux_select_args *args)
494 struct timeval tv0, tv1, utv, *tvp;
498 * Store current time for computation of the amount of
502 if ((error = copyin(args->timeout, <v, sizeof(ltv))))
504 utv.tv_sec = ltv.tv_sec;
505 utv.tv_usec = ltv.tv_usec;
507 if (itimerfix(&utv)) {
509 * The timeval was invalid. Convert it to something
510 * valid that will act as it does under Linux.
512 utv.tv_sec += utv.tv_usec / 1000000;
513 utv.tv_usec %= 1000000;
514 if (utv.tv_usec < 0) {
516 utv.tv_usec += 1000000;
526 error = kern_select(td, args->nfds, args->readfds, args->writefds,
527 args->exceptfds, tvp, LINUX_NFDBITS);
532 if (td->td_retval[0]) {
534 * Compute how much time was left of the timeout,
535 * by subtracting the current time and the time
536 * before we started the call, and subtracting
537 * that result from the user-supplied value.
540 timevalsub(&tv1, &tv0);
541 timevalsub(&utv, &tv1);
546 ltv.tv_sec = utv.tv_sec;
547 ltv.tv_usec = utv.tv_usec;
548 if ((error = copyout(<v, args->timeout, sizeof(ltv))))
558 linux_mremap(struct thread *td, struct linux_mremap_args *args)
564 if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) {
565 td->td_retval[0] = 0;
570 * Check for the page alignment.
571 * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK.
573 if (args->addr & PAGE_MASK) {
574 td->td_retval[0] = 0;
578 args->new_len = round_page(args->new_len);
579 args->old_len = round_page(args->old_len);
581 if (args->new_len > args->old_len) {
582 td->td_retval[0] = 0;
586 if (args->new_len < args->old_len) {
587 addr = args->addr + args->new_len;
588 len = args->old_len - args->new_len;
589 error = kern_munmap(td, addr, len);
592 td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
596 #define LINUX_MS_ASYNC 0x0001
597 #define LINUX_MS_INVALIDATE 0x0002
598 #define LINUX_MS_SYNC 0x0004
601 linux_msync(struct thread *td, struct linux_msync_args *args)
604 return (kern_msync(td, args->addr, args->len,
605 args->fl & ~LINUX_MS_SYNC));
608 #ifdef LINUX_LEGACY_SYSCALLS
610 linux_time(struct thread *td, struct linux_time_args *args)
618 if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
620 td->td_retval[0] = tm;
625 struct l_times_argv {
628 l_clock_t tms_cutime;
629 l_clock_t tms_cstime;
634 * Glibc versions prior to 2.2.1 always use hard-coded CLK_TCK value.
635 * Since 2.2.1 Glibc uses value exported from kernel via AT_CLKTCK
636 * auxiliary vector entry.
640 #define CONVOTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
641 #define CONVNTCK(r) (r.tv_sec * stclohz + r.tv_usec / (1000000 / stclohz))
643 #define CONVTCK(r) (linux_kernver(td) >= LINUX_KERNVER_2004000 ? \
644 CONVNTCK(r) : CONVOTCK(r))
647 linux_times(struct thread *td, struct linux_times_args *args)
649 struct timeval tv, utime, stime, cutime, cstime;
650 struct l_times_argv tms;
654 if (args->buf != NULL) {
658 calcru(p, &utime, &stime);
660 calccru(p, &cutime, &cstime);
663 tms.tms_utime = CONVTCK(utime);
664 tms.tms_stime = CONVTCK(stime);
666 tms.tms_cutime = CONVTCK(cutime);
667 tms.tms_cstime = CONVTCK(cstime);
669 if ((error = copyout(&tms, args->buf, sizeof(tms))))
674 td->td_retval[0] = (int)CONVTCK(tv);
679 linux_newuname(struct thread *td, struct linux_newuname_args *args)
681 struct l_new_utsname utsname;
682 char osname[LINUX_MAX_UTSNAME];
683 char osrelease[LINUX_MAX_UTSNAME];
686 linux_get_osname(td, osname);
687 linux_get_osrelease(td, osrelease);
689 bzero(&utsname, sizeof(utsname));
690 strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
691 getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
692 getcreddomainname(td->td_ucred, utsname.domainname, LINUX_MAX_UTSNAME);
693 strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
694 strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
695 for (p = utsname.version; *p != '\0'; ++p)
700 strlcpy(utsname.machine, linux_kplatform, LINUX_MAX_UTSNAME);
702 return (copyout(&utsname, args->buf, sizeof(utsname)));
710 #ifdef LINUX_LEGACY_SYSCALLS
712 linux_utime(struct thread *td, struct linux_utime_args *args)
714 struct timeval tv[2], *tvp;
715 struct l_utimbuf lut;
719 LCONVPATHEXIST(td, args->fname, &fname);
722 if ((error = copyin(args->times, &lut, sizeof lut))) {
726 tv[0].tv_sec = lut.l_actime;
728 tv[1].tv_sec = lut.l_modtime;
734 error = kern_utimesat(td, AT_FDCWD, fname, UIO_SYSSPACE, tvp,
741 #ifdef LINUX_LEGACY_SYSCALLS
743 linux_utimes(struct thread *td, struct linux_utimes_args *args)
746 struct timeval tv[2], *tvp = NULL;
750 LCONVPATHEXIST(td, args->fname, &fname);
752 if (args->tptr != NULL) {
753 if ((error = copyin(args->tptr, ltv, sizeof ltv))) {
757 tv[0].tv_sec = ltv[0].tv_sec;
758 tv[0].tv_usec = ltv[0].tv_usec;
759 tv[1].tv_sec = ltv[1].tv_sec;
760 tv[1].tv_usec = ltv[1].tv_usec;
764 error = kern_utimesat(td, AT_FDCWD, fname, UIO_SYSSPACE,
772 linux_utimensat_nsec_valid(l_long nsec)
775 if (nsec == LINUX_UTIME_OMIT || nsec == LINUX_UTIME_NOW)
777 if (nsec >= 0 && nsec <= 999999999)
783 linux_utimensat(struct thread *td, struct linux_utimensat_args *args)
785 struct l_timespec l_times[2];
786 struct timespec times[2], *timesp = NULL;
788 int error, dfd, flags = 0;
790 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
792 if (args->flags & ~LINUX_AT_SYMLINK_NOFOLLOW)
795 if (args->times != NULL) {
796 error = copyin(args->times, l_times, sizeof(l_times));
800 if (linux_utimensat_nsec_valid(l_times[0].tv_nsec) != 0 ||
801 linux_utimensat_nsec_valid(l_times[1].tv_nsec) != 0)
804 times[0].tv_sec = l_times[0].tv_sec;
805 switch (l_times[0].tv_nsec)
807 case LINUX_UTIME_OMIT:
808 times[0].tv_nsec = UTIME_OMIT;
810 case LINUX_UTIME_NOW:
811 times[0].tv_nsec = UTIME_NOW;
814 times[0].tv_nsec = l_times[0].tv_nsec;
817 times[1].tv_sec = l_times[1].tv_sec;
818 switch (l_times[1].tv_nsec)
820 case LINUX_UTIME_OMIT:
821 times[1].tv_nsec = UTIME_OMIT;
823 case LINUX_UTIME_NOW:
824 times[1].tv_nsec = UTIME_NOW;
827 times[1].tv_nsec = l_times[1].tv_nsec;
832 /* This breaks POSIX, but is what the Linux kernel does
833 * _on purpose_ (documented in the man page for utimensat(2)),
834 * so we must follow that behaviour. */
835 if (times[0].tv_nsec == UTIME_OMIT &&
836 times[1].tv_nsec == UTIME_OMIT)
840 if (args->pathname != NULL)
841 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
842 else if (args->flags != 0)
845 if (args->flags & LINUX_AT_SYMLINK_NOFOLLOW)
846 flags |= AT_SYMLINK_NOFOLLOW;
849 error = kern_futimens(td, dfd, timesp, UIO_SYSSPACE);
851 error = kern_utimensat(td, dfd, path, UIO_SYSSPACE, timesp,
852 UIO_SYSSPACE, flags);
859 #ifdef LINUX_LEGACY_SYSCALLS
861 linux_futimesat(struct thread *td, struct linux_futimesat_args *args)
864 struct timeval tv[2], *tvp = NULL;
868 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
869 LCONVPATHEXIST_AT(td, args->filename, &fname, dfd);
871 if (args->utimes != NULL) {
872 if ((error = copyin(args->utimes, ltv, sizeof ltv))) {
876 tv[0].tv_sec = ltv[0].tv_sec;
877 tv[0].tv_usec = ltv[0].tv_usec;
878 tv[1].tv_sec = ltv[1].tv_sec;
879 tv[1].tv_usec = ltv[1].tv_usec;
883 error = kern_utimesat(td, dfd, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
890 linux_common_wait(struct thread *td, int pid, int *status,
891 int options, struct rusage *ru)
895 error = kern_wait(td, pid, &tmpstat, options, ru);
901 if (WIFSIGNALED(tmpstat))
902 tmpstat = (tmpstat & 0xffffff80) |
903 bsd_to_linux_signal(WTERMSIG(tmpstat));
904 else if (WIFSTOPPED(tmpstat))
905 tmpstat = (tmpstat & 0xffff00ff) |
906 (bsd_to_linux_signal(WSTOPSIG(tmpstat)) << 8);
907 else if (WIFCONTINUED(tmpstat))
909 error = copyout(&tmpstat, status, sizeof(int));
915 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
917 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
919 struct linux_wait4_args wait4_args;
921 wait4_args.pid = args->pid;
922 wait4_args.status = args->status;
923 wait4_args.options = args->options;
924 wait4_args.rusage = NULL;
926 return (linux_wait4(td, &wait4_args));
928 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
931 linux_wait4(struct thread *td, struct linux_wait4_args *args)
934 struct rusage ru, *rup;
936 if (args->options & ~(LINUX_WUNTRACED | LINUX_WNOHANG |
937 LINUX_WCONTINUED | __WCLONE | __WNOTHREAD | __WALL))
941 linux_to_bsd_waitopts(args->options, &options);
943 if (args->rusage != NULL)
947 error = linux_common_wait(td, args->pid, args->status, options, rup);
950 if (args->rusage != NULL)
951 error = linux_copyout_rusage(&ru, args->rusage);
956 linux_waitid(struct thread *td, struct linux_waitid_args *args)
958 int status, options, sig;
959 struct __wrusage wru;
967 linux_to_bsd_waitopts(args->options, &options);
969 if (options & ~(WNOHANG | WNOWAIT | WEXITED | WUNTRACED | WCONTINUED))
971 if (!(options & (WEXITED | WUNTRACED | WCONTINUED)))
974 switch (args->idtype) {
992 error = kern_wait6(td, idtype, args->id, &status, options,
996 if (args->rusage != NULL) {
997 error = linux_copyout_rusage(&wru.wru_children,
1002 if (args->info != NULL) {
1004 bzero(&lsi, sizeof(lsi));
1005 if (td->td_retval[0] != 0) {
1006 sig = bsd_to_linux_signal(siginfo.si_signo);
1007 siginfo_to_lsiginfo(&siginfo, &lsi, sig);
1009 error = copyout(&lsi, args->info, sizeof(lsi));
1011 td->td_retval[0] = 0;
1016 #ifdef LINUX_LEGACY_SYSCALLS
1018 linux_mknod(struct thread *td, struct linux_mknod_args *args)
1023 LCONVPATHCREAT(td, args->path, &path);
1025 switch (args->mode & S_IFMT) {
1028 error = kern_mkfifoat(td, AT_FDCWD, path, UIO_SYSSPACE,
1034 error = kern_mknodat(td, AT_FDCWD, path, UIO_SYSSPACE,
1035 args->mode, args->dev);
1043 args->mode |= S_IFREG;
1046 error = kern_openat(td, AT_FDCWD, path, UIO_SYSSPACE,
1047 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1049 kern_close(td, td->td_retval[0]);
1062 linux_mknodat(struct thread *td, struct linux_mknodat_args *args)
1067 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1068 LCONVPATHCREAT_AT(td, args->filename, &path, dfd);
1070 switch (args->mode & S_IFMT) {
1073 error = kern_mkfifoat(td, dfd, path, UIO_SYSSPACE, args->mode);
1078 error = kern_mknodat(td, dfd, path, UIO_SYSSPACE, args->mode,
1087 args->mode |= S_IFREG;
1090 error = kern_openat(td, dfd, path, UIO_SYSSPACE,
1091 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1093 kern_close(td, td->td_retval[0]);
1105 * UGH! This is just about the dumbest idea I've ever heard!!
1108 linux_personality(struct thread *td, struct linux_personality_args *args)
1110 struct linux_pemuldata *pem;
1111 struct proc *p = td->td_proc;
1117 if (args->per != 0xffffffff)
1118 pem->persona = args->per;
1121 td->td_retval[0] = old;
1125 struct l_itimerval {
1126 l_timeval it_interval;
1130 #define B2L_ITIMERVAL(bip, lip) \
1131 (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec; \
1132 (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec; \
1133 (bip)->it_value.tv_sec = (lip)->it_value.tv_sec; \
1134 (bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
1137 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
1140 struct l_itimerval ls;
1141 struct itimerval aitv, oitv;
1143 if (uap->itv == NULL) {
1144 uap->itv = uap->oitv;
1145 return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
1148 error = copyin(uap->itv, &ls, sizeof(ls));
1151 B2L_ITIMERVAL(&aitv, &ls);
1152 error = kern_setitimer(td, uap->which, &aitv, &oitv);
1153 if (error != 0 || uap->oitv == NULL)
1155 B2L_ITIMERVAL(&ls, &oitv);
1157 return (copyout(&ls, uap->oitv, sizeof(ls)));
1161 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
1164 struct l_itimerval ls;
1165 struct itimerval aitv;
1167 error = kern_getitimer(td, uap->which, &aitv);
1170 B2L_ITIMERVAL(&ls, &aitv);
1171 return (copyout(&ls, uap->itv, sizeof(ls)));
1174 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1176 linux_nice(struct thread *td, struct linux_nice_args *args)
1178 struct setpriority_args bsd_args;
1180 bsd_args.which = PRIO_PROCESS;
1181 bsd_args.who = 0; /* current process */
1182 bsd_args.prio = args->inc;
1183 return (sys_setpriority(td, &bsd_args));
1185 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1188 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
1190 struct ucred *newcred, *oldcred;
1191 l_gid_t *linux_gidset;
1196 ngrp = args->gidsetsize;
1197 if (ngrp < 0 || ngrp >= ngroups_max + 1)
1199 linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_LINUX, M_WAITOK);
1200 error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
1204 crextend(newcred, ngrp + 1);
1207 oldcred = p->p_ucred;
1208 crcopy(newcred, oldcred);
1211 * cr_groups[0] holds egid. Setting the whole set from
1212 * the supplied set will cause egid to be changed too.
1213 * Keep cr_groups[0] unchanged to prevent that.
1216 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS)) != 0) {
1223 newcred->cr_ngroups = ngrp + 1;
1225 bsd_gidset = newcred->cr_groups;
1228 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1232 newcred->cr_ngroups = 1;
1235 proc_set_cred(p, newcred);
1240 free(linux_gidset, M_LINUX);
1245 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1248 l_gid_t *linux_gidset;
1250 int bsd_gidsetsz, ngrp, error;
1252 cred = td->td_ucred;
1253 bsd_gidset = cred->cr_groups;
1254 bsd_gidsetsz = cred->cr_ngroups - 1;
1257 * cr_groups[0] holds egid. Returning the whole set
1258 * here will cause a duplicate. Exclude cr_groups[0]
1262 if ((ngrp = args->gidsetsize) == 0) {
1263 td->td_retval[0] = bsd_gidsetsz;
1267 if (ngrp < bsd_gidsetsz)
1271 linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset),
1273 while (ngrp < bsd_gidsetsz) {
1274 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1278 error = copyout(linux_gidset, args->grouplist, ngrp * sizeof(l_gid_t));
1279 free(linux_gidset, M_LINUX);
1283 td->td_retval[0] = ngrp;
1288 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1290 struct rlimit bsd_rlim;
1291 struct l_rlimit rlim;
1295 if (args->resource >= LINUX_RLIM_NLIMITS)
1298 which = linux_to_bsd_resource[args->resource];
1302 error = copyin(args->rlim, &rlim, sizeof(rlim));
1306 bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1307 bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1308 return (kern_setrlimit(td, which, &bsd_rlim));
1311 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1313 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1315 struct l_rlimit rlim;
1316 struct rlimit bsd_rlim;
1319 if (args->resource >= LINUX_RLIM_NLIMITS)
1322 which = linux_to_bsd_resource[args->resource];
1326 lim_rlimit(td, which, &bsd_rlim);
1328 #ifdef COMPAT_LINUX32
1329 rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1330 if (rlim.rlim_cur == UINT_MAX)
1331 rlim.rlim_cur = INT_MAX;
1332 rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1333 if (rlim.rlim_max == UINT_MAX)
1334 rlim.rlim_max = INT_MAX;
1336 rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1337 if (rlim.rlim_cur == ULONG_MAX)
1338 rlim.rlim_cur = LONG_MAX;
1339 rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1340 if (rlim.rlim_max == ULONG_MAX)
1341 rlim.rlim_max = LONG_MAX;
1343 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1345 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1348 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1350 struct l_rlimit rlim;
1351 struct rlimit bsd_rlim;
1354 if (args->resource >= LINUX_RLIM_NLIMITS)
1357 which = linux_to_bsd_resource[args->resource];
1361 lim_rlimit(td, which, &bsd_rlim);
1363 rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1364 rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1365 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1369 linux_sched_setscheduler(struct thread *td,
1370 struct linux_sched_setscheduler_args *args)
1372 struct sched_param sched_param;
1376 switch (args->policy) {
1377 case LINUX_SCHED_OTHER:
1378 policy = SCHED_OTHER;
1380 case LINUX_SCHED_FIFO:
1381 policy = SCHED_FIFO;
1383 case LINUX_SCHED_RR:
1390 error = copyin(args->param, &sched_param, sizeof(sched_param));
1394 tdt = linux_tdfind(td, args->pid, -1);
1398 error = kern_sched_setscheduler(td, tdt, policy, &sched_param);
1399 PROC_UNLOCK(tdt->td_proc);
1404 linux_sched_getscheduler(struct thread *td,
1405 struct linux_sched_getscheduler_args *args)
1410 tdt = linux_tdfind(td, args->pid, -1);
1414 error = kern_sched_getscheduler(td, tdt, &policy);
1415 PROC_UNLOCK(tdt->td_proc);
1419 td->td_retval[0] = LINUX_SCHED_OTHER;
1422 td->td_retval[0] = LINUX_SCHED_FIFO;
1425 td->td_retval[0] = LINUX_SCHED_RR;
1432 linux_sched_get_priority_max(struct thread *td,
1433 struct linux_sched_get_priority_max_args *args)
1435 struct sched_get_priority_max_args bsd;
1437 switch (args->policy) {
1438 case LINUX_SCHED_OTHER:
1439 bsd.policy = SCHED_OTHER;
1441 case LINUX_SCHED_FIFO:
1442 bsd.policy = SCHED_FIFO;
1444 case LINUX_SCHED_RR:
1445 bsd.policy = SCHED_RR;
1450 return (sys_sched_get_priority_max(td, &bsd));
1454 linux_sched_get_priority_min(struct thread *td,
1455 struct linux_sched_get_priority_min_args *args)
1457 struct sched_get_priority_min_args bsd;
1459 switch (args->policy) {
1460 case LINUX_SCHED_OTHER:
1461 bsd.policy = SCHED_OTHER;
1463 case LINUX_SCHED_FIFO:
1464 bsd.policy = SCHED_FIFO;
1466 case LINUX_SCHED_RR:
1467 bsd.policy = SCHED_RR;
1472 return (sys_sched_get_priority_min(td, &bsd));
1475 #define REBOOT_CAD_ON 0x89abcdef
1476 #define REBOOT_CAD_OFF 0
1477 #define REBOOT_HALT 0xcdef0123
1478 #define REBOOT_RESTART 0x01234567
1479 #define REBOOT_RESTART2 0xA1B2C3D4
1480 #define REBOOT_POWEROFF 0x4321FEDC
1481 #define REBOOT_MAGIC1 0xfee1dead
1482 #define REBOOT_MAGIC2 0x28121969
1483 #define REBOOT_MAGIC2A 0x05121996
1484 #define REBOOT_MAGIC2B 0x16041998
1487 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1489 struct reboot_args bsd_args;
1491 if (args->magic1 != REBOOT_MAGIC1)
1494 switch (args->magic2) {
1496 case REBOOT_MAGIC2A:
1497 case REBOOT_MAGIC2B:
1503 switch (args->cmd) {
1505 case REBOOT_CAD_OFF:
1506 return (priv_check(td, PRIV_REBOOT));
1508 bsd_args.opt = RB_HALT;
1510 case REBOOT_RESTART:
1511 case REBOOT_RESTART2:
1514 case REBOOT_POWEROFF:
1515 bsd_args.opt = RB_POWEROFF;
1520 return (sys_reboot(td, &bsd_args));
1525 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1526 * td->td_retval[1] when COMPAT_43 is defined. This clobbers registers that
1527 * are assumed to be preserved. The following lightweight syscalls fixes
1528 * this. See also linux_getgid16() and linux_getuid16() in linux_uid16.c
1530 * linux_getpid() - MP SAFE
1531 * linux_getgid() - MP SAFE
1532 * linux_getuid() - MP SAFE
1536 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1539 td->td_retval[0] = td->td_proc->p_pid;
1545 linux_gettid(struct thread *td, struct linux_gettid_args *args)
1547 struct linux_emuldata *em;
1550 KASSERT(em != NULL, ("gettid: emuldata not found.\n"));
1552 td->td_retval[0] = em->em_tid;
1559 linux_getppid(struct thread *td, struct linux_getppid_args *args)
1562 td->td_retval[0] = kern_getppid(td);
1567 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1570 td->td_retval[0] = td->td_ucred->cr_rgid;
1575 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1578 td->td_retval[0] = td->td_ucred->cr_ruid;
1584 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1586 struct getsid_args bsd;
1588 bsd.pid = args->pid;
1589 return (sys_getsid(td, &bsd));
1593 linux_nosys(struct thread *td, struct nosys_args *ignore)
1600 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1602 struct getpriority_args bsd_args;
1605 bsd_args.which = args->which;
1606 bsd_args.who = args->who;
1607 error = sys_getpriority(td, &bsd_args);
1608 td->td_retval[0] = 20 - td->td_retval[0];
1613 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1618 name[1] = KERN_HOSTNAME;
1619 return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1624 linux_setdomainname(struct thread *td, struct linux_setdomainname_args *args)
1629 name[1] = KERN_NISDOMAINNAME;
1630 return (userland_sysctl(td, name, 2, 0, 0, 0, args->name,
1635 linux_exit_group(struct thread *td, struct linux_exit_group_args *args)
1638 LINUX_CTR2(exit_group, "thread(%d) (%d)", td->td_tid,
1642 * XXX: we should send a signal to the parent if
1643 * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?)
1644 * as it doesnt occur often.
1646 exit1(td, args->error_code, 0);
1650 #define _LINUX_CAPABILITY_VERSION_1 0x19980330
1651 #define _LINUX_CAPABILITY_VERSION_2 0x20071026
1652 #define _LINUX_CAPABILITY_VERSION_3 0x20080522
1654 struct l_user_cap_header {
1659 struct l_user_cap_data {
1666 linux_capget(struct thread *td, struct linux_capget_args *uap)
1668 struct l_user_cap_header luch;
1669 struct l_user_cap_data lucd[2];
1672 if (uap->hdrp == NULL)
1675 error = copyin(uap->hdrp, &luch, sizeof(luch));
1679 switch (luch.version) {
1680 case _LINUX_CAPABILITY_VERSION_1:
1683 case _LINUX_CAPABILITY_VERSION_2:
1684 case _LINUX_CAPABILITY_VERSION_3:
1688 luch.version = _LINUX_CAPABILITY_VERSION_1;
1689 error = copyout(&luch, uap->hdrp, sizeof(luch));
1700 * The current implementation doesn't support setting
1701 * a capability (it's essentially a stub) so indicate
1702 * that no capabilities are currently set or available
1705 memset(&lucd, 0, u32s * sizeof(lucd[0]));
1706 error = copyout(&lucd, uap->datap, u32s * sizeof(lucd[0]));
1713 linux_capset(struct thread *td, struct linux_capset_args *uap)
1715 struct l_user_cap_header luch;
1716 struct l_user_cap_data lucd[2];
1719 if (uap->hdrp == NULL || uap->datap == NULL)
1722 error = copyin(uap->hdrp, &luch, sizeof(luch));
1726 switch (luch.version) {
1727 case _LINUX_CAPABILITY_VERSION_1:
1730 case _LINUX_CAPABILITY_VERSION_2:
1731 case _LINUX_CAPABILITY_VERSION_3:
1735 luch.version = _LINUX_CAPABILITY_VERSION_1;
1736 error = copyout(&luch, uap->hdrp, sizeof(luch));
1745 error = copyin(uap->datap, &lucd, u32s * sizeof(lucd[0]));
1749 /* We currently don't support setting any capabilities. */
1750 for (i = 0; i < u32s; i++) {
1751 if (lucd[i].effective || lucd[i].permitted ||
1752 lucd[i].inheritable) {
1754 "capset[%d] effective=0x%x, permitted=0x%x, "
1755 "inheritable=0x%x is not implemented", i,
1756 (int)lucd[i].effective, (int)lucd[i].permitted,
1757 (int)lucd[i].inheritable);
1766 linux_prctl(struct thread *td, struct linux_prctl_args *args)
1768 int error = 0, max_size;
1769 struct proc *p = td->td_proc;
1770 char comm[LINUX_MAX_COMM_LEN];
1773 switch (args->option) {
1774 case LINUX_PR_SET_PDEATHSIG:
1775 if (!LINUX_SIG_VALID(args->arg2))
1777 pdeath_signal = linux_to_bsd_signal(args->arg2);
1778 return (kern_procctl(td, P_PID, 0, PROC_PDEATHSIG_CTL,
1780 case LINUX_PR_GET_PDEATHSIG:
1781 error = kern_procctl(td, P_PID, 0, PROC_PDEATHSIG_STATUS,
1785 pdeath_signal = bsd_to_linux_signal(pdeath_signal);
1786 return (copyout(&pdeath_signal,
1787 (void *)(register_t)args->arg2,
1788 sizeof(pdeath_signal)));
1790 case LINUX_PR_GET_KEEPCAPS:
1792 * Indicate that we always clear the effective and
1793 * permitted capability sets when the user id becomes
1794 * non-zero (actually the capability sets are simply
1795 * always zero in the current implementation).
1797 td->td_retval[0] = 0;
1799 case LINUX_PR_SET_KEEPCAPS:
1801 * Ignore requests to keep the effective and permitted
1802 * capability sets when the user id becomes non-zero.
1805 case LINUX_PR_SET_NAME:
1807 * To be on the safe side we need to make sure to not
1808 * overflow the size a Linux program expects. We already
1809 * do this here in the copyin, so that we don't need to
1812 max_size = MIN(sizeof(comm), sizeof(p->p_comm));
1813 error = copyinstr((void *)(register_t)args->arg2, comm,
1816 /* Linux silently truncates the name if it is too long. */
1817 if (error == ENAMETOOLONG) {
1819 * XXX: copyinstr() isn't documented to populate the
1820 * array completely, so do a copyin() to be on the
1821 * safe side. This should be changed in case
1822 * copyinstr() is changed to guarantee this.
1824 error = copyin((void *)(register_t)args->arg2, comm,
1826 comm[max_size - 1] = '\0';
1832 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
1835 case LINUX_PR_GET_NAME:
1837 strlcpy(comm, p->p_comm, sizeof(comm));
1839 error = copyout(comm, (void *)(register_t)args->arg2,
1851 linux_sched_setparam(struct thread *td,
1852 struct linux_sched_setparam_args *uap)
1854 struct sched_param sched_param;
1858 error = copyin(uap->param, &sched_param, sizeof(sched_param));
1862 tdt = linux_tdfind(td, uap->pid, -1);
1866 error = kern_sched_setparam(td, tdt, &sched_param);
1867 PROC_UNLOCK(tdt->td_proc);
1872 linux_sched_getparam(struct thread *td,
1873 struct linux_sched_getparam_args *uap)
1875 struct sched_param sched_param;
1879 tdt = linux_tdfind(td, uap->pid, -1);
1883 error = kern_sched_getparam(td, tdt, &sched_param);
1884 PROC_UNLOCK(tdt->td_proc);
1886 error = copyout(&sched_param, uap->param,
1887 sizeof(sched_param));
1892 * Get affinity of a process.
1895 linux_sched_getaffinity(struct thread *td,
1896 struct linux_sched_getaffinity_args *args)
1901 if (args->len < sizeof(cpuset_t))
1904 tdt = linux_tdfind(td, args->pid, -1);
1908 PROC_UNLOCK(tdt->td_proc);
1910 error = kern_cpuset_getaffinity(td, CPU_LEVEL_WHICH, CPU_WHICH_TID,
1911 tdt->td_tid, sizeof(cpuset_t), (cpuset_t *)args->user_mask_ptr);
1913 td->td_retval[0] = sizeof(cpuset_t);
1919 * Set affinity of a process.
1922 linux_sched_setaffinity(struct thread *td,
1923 struct linux_sched_setaffinity_args *args)
1927 if (args->len < sizeof(cpuset_t))
1930 tdt = linux_tdfind(td, args->pid, -1);
1934 PROC_UNLOCK(tdt->td_proc);
1936 return (kern_cpuset_setaffinity(td, CPU_LEVEL_WHICH, CPU_WHICH_TID,
1937 tdt->td_tid, sizeof(cpuset_t), (cpuset_t *) args->user_mask_ptr));
1940 struct linux_rlimit64 {
1946 linux_prlimit64(struct thread *td, struct linux_prlimit64_args *args)
1948 struct rlimit rlim, nrlim;
1949 struct linux_rlimit64 lrlim;
1955 if (args->resource >= LINUX_RLIM_NLIMITS)
1958 which = linux_to_bsd_resource[args->resource];
1962 if (args->new != NULL) {
1964 * Note. Unlike FreeBSD where rlim is signed 64-bit Linux
1965 * rlim is unsigned 64-bit. FreeBSD treats negative limits
1966 * as INFINITY so we do not need a conversion even.
1968 error = copyin(args->new, &nrlim, sizeof(nrlim));
1973 flags = PGET_HOLD | PGET_NOTWEXIT;
1974 if (args->new != NULL)
1975 flags |= PGET_CANDEBUG;
1977 flags |= PGET_CANSEE;
1978 error = pget(args->pid, flags, &p);
1982 if (args->old != NULL) {
1984 lim_rlimit_proc(p, which, &rlim);
1986 if (rlim.rlim_cur == RLIM_INFINITY)
1987 lrlim.rlim_cur = LINUX_RLIM_INFINITY;
1989 lrlim.rlim_cur = rlim.rlim_cur;
1990 if (rlim.rlim_max == RLIM_INFINITY)
1991 lrlim.rlim_max = LINUX_RLIM_INFINITY;
1993 lrlim.rlim_max = rlim.rlim_max;
1994 error = copyout(&lrlim, args->old, sizeof(lrlim));
1999 if (args->new != NULL)
2000 error = kern_proc_setrlimit(td, p, which, &nrlim);
2008 linux_pselect6(struct thread *td, struct linux_pselect6_args *args)
2010 struct timeval utv, tv0, tv1, *tvp;
2011 struct l_pselect6arg lpse6;
2012 struct l_timespec lts;
2013 struct timespec uts;
2020 if (args->sig != NULL) {
2021 error = copyin(args->sig, &lpse6, sizeof(lpse6));
2024 if (lpse6.ss_len != sizeof(l_ss))
2026 if (lpse6.ss != 0) {
2027 error = copyin(PTRIN(lpse6.ss), &l_ss,
2031 linux_to_bsd_sigset(&l_ss, &ss);
2037 * Currently glibc changes nanosecond number to microsecond.
2038 * This mean losing precision but for now it is hardly seen.
2040 if (args->tsp != NULL) {
2041 error = copyin(args->tsp, <s, sizeof(lts));
2044 error = linux_to_native_timespec(&uts, <s);
2048 TIMESPEC_TO_TIMEVAL(&utv, &uts);
2049 if (itimerfix(&utv))
2057 error = kern_pselect(td, args->nfds, args->readfds, args->writefds,
2058 args->exceptfds, tvp, ssp, LINUX_NFDBITS);
2060 if (error == 0 && args->tsp != NULL) {
2061 if (td->td_retval[0] != 0) {
2063 * Compute how much time was left of the timeout,
2064 * by subtracting the current time and the time
2065 * before we started the call, and subtracting
2066 * that result from the user-supplied value.
2070 timevalsub(&tv1, &tv0);
2071 timevalsub(&utv, &tv1);
2077 TIMEVAL_TO_TIMESPEC(&utv, &uts);
2079 error = native_to_linux_timespec(<s, &uts);
2081 error = copyout(<s, args->tsp, sizeof(lts));
2088 linux_ppoll(struct thread *td, struct linux_ppoll_args *args)
2090 struct timespec ts0, ts1;
2091 struct l_timespec lts;
2092 struct timespec uts, *tsp;
2098 if (args->sset != NULL) {
2099 if (args->ssize != sizeof(l_ss))
2101 error = copyin(args->sset, &l_ss, sizeof(l_ss));
2104 linux_to_bsd_sigset(&l_ss, &ss);
2108 if (args->tsp != NULL) {
2109 error = copyin(args->tsp, <s, sizeof(lts));
2112 error = linux_to_native_timespec(&uts, <s);
2121 error = kern_poll(td, args->fds, args->nfds, tsp, ssp);
2123 if (error == 0 && args->tsp != NULL) {
2124 if (td->td_retval[0]) {
2126 timespecsub(&ts1, &ts0, &ts1);
2127 timespecsub(&uts, &ts1, &uts);
2129 timespecclear(&uts);
2131 timespecclear(&uts);
2133 error = native_to_linux_timespec(<s, &uts);
2135 error = copyout(<s, args->tsp, sizeof(lts));
2142 linux_sched_rr_get_interval(struct thread *td,
2143 struct linux_sched_rr_get_interval_args *uap)
2146 struct l_timespec lts;
2151 * According to man in case the invalid pid specified
2152 * EINVAL should be returned.
2157 tdt = linux_tdfind(td, uap->pid, -1);
2161 error = kern_sched_rr_get_interval_td(td, tdt, &ts);
2162 PROC_UNLOCK(tdt->td_proc);
2165 error = native_to_linux_timespec(<s, &ts);
2168 return (copyout(<s, uap->interval, sizeof(lts)));
2172 * In case when the Linux thread is the initial thread in
2173 * the thread group thread id is equal to the process id.
2174 * Glibc depends on this magic (assert in pthread_getattr_np.c).
2177 linux_tdfind(struct thread *td, lwpid_t tid, pid_t pid)
2179 struct linux_emuldata *em;
2184 if (tid == 0 || tid == td->td_tid) {
2186 PROC_LOCK(tdt->td_proc);
2187 } else if (tid > PID_MAX)
2188 tdt = tdfind(tid, pid);
2191 * Initial thread where the tid equal to the pid.
2195 if (SV_PROC_ABI(p) != SV_ABI_LINUX) {
2197 * p is not a Linuxulator process.
2202 FOREACH_THREAD_IN_PROC(p, tdt) {
2204 if (tid == em->em_tid)
2216 linux_to_bsd_waitopts(int options, int *bsdopts)
2219 if (options & LINUX_WNOHANG)
2220 *bsdopts |= WNOHANG;
2221 if (options & LINUX_WUNTRACED)
2222 *bsdopts |= WUNTRACED;
2223 if (options & LINUX_WEXITED)
2224 *bsdopts |= WEXITED;
2225 if (options & LINUX_WCONTINUED)
2226 *bsdopts |= WCONTINUED;
2227 if (options & LINUX_WNOWAIT)
2228 *bsdopts |= WNOWAIT;
2230 if (options & __WCLONE)
2231 *bsdopts |= WLINUXCLONE;
2235 linux_getrandom(struct thread *td, struct linux_getrandom_args *args)
2241 if (args->flags & ~(LINUX_GRND_NONBLOCK|LINUX_GRND_RANDOM))
2243 if (args->count > INT_MAX)
2244 args->count = INT_MAX;
2246 iov.iov_base = args->buf;
2247 iov.iov_len = args->count;
2251 uio.uio_resid = iov.iov_len;
2252 uio.uio_segflg = UIO_USERSPACE;
2253 uio.uio_rw = UIO_READ;
2256 error = read_random_uio(&uio, args->flags & LINUX_GRND_NONBLOCK);
2258 td->td_retval[0] = args->count - uio.uio_resid;
2263 linux_mincore(struct thread *td, struct linux_mincore_args *args)
2266 /* Needs to be page-aligned */
2267 if (args->start & PAGE_MASK)
2269 return (kern_mincore(td, args->start, args->len, args->vec));