2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2002 Doug Rabson
5 * Copyright (c) 1994-1995 Søren Schmidt
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer
13 * in this position and unchanged.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_compat.h"
37 #include <sys/param.h>
38 #include <sys/blist.h>
39 #include <sys/fcntl.h>
41 #include <sys/imgact_aout.h>
44 #include <sys/kernel.h>
45 #include <sys/limits.h>
47 #include <sys/malloc.h>
49 #include <sys/mount.h>
50 #include <sys/msgbuf.h>
51 #include <sys/mutex.h>
52 #include <sys/namei.h>
55 #include <sys/procctl.h>
56 #include <sys/reboot.h>
57 #include <sys/racct.h>
58 #include <sys/random.h>
59 #include <sys/resourcevar.h>
60 #include <sys/sched.h>
62 #include <sys/signalvar.h>
64 #include <sys/syscallsubr.h>
65 #include <sys/sysctl.h>
66 #include <sys/sysproto.h>
67 #include <sys/systm.h>
69 #include <sys/vmmeter.h>
70 #include <sys/vnode.h>
72 #include <sys/cpuset.h>
75 #include <security/mac/mac_framework.h>
79 #include <vm/vm_kern.h>
80 #include <vm/vm_map.h>
81 #include <vm/vm_extern.h>
82 #include <vm/swap_pager.h>
85 #include <machine/../linux32/linux.h>
86 #include <machine/../linux32/linux32_proto.h>
88 #include <machine/../linux/linux.h>
89 #include <machine/../linux/linux_proto.h>
92 #include <compat/linux/linux_dtrace.h>
93 #include <compat/linux/linux_file.h>
94 #include <compat/linux/linux_mib.h>
95 #include <compat/linux/linux_signal.h>
96 #include <compat/linux/linux_timer.h>
97 #include <compat/linux/linux_util.h>
98 #include <compat/linux/linux_sysproto.h>
99 #include <compat/linux/linux_emul.h>
100 #include <compat/linux/linux_misc.h>
103 * Special DTrace provider for the linuxulator.
105 * In this file we define the provider for the entire linuxulator. All
106 * modules (= files of the linuxulator) use it.
108 * We define a different name depending on the emulated bitsize, see
109 * ../../<ARCH>/linux{,32}/linux.h, e.g.:
110 * native bitsize = linuxulator
111 * amd64, 32bit emulation = linuxulator32
113 LIN_SDT_PROVIDER_DEFINE(LINUX_DTRACE);
115 int stclohz; /* Statistics clock frequency */
117 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
118 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
119 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
120 RLIMIT_MEMLOCK, RLIMIT_AS
124 l_long uptime; /* Seconds since boot */
125 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */
126 #define LINUX_SYSINFO_LOADS_SCALE 65536
127 l_ulong totalram; /* Total usable main memory size */
128 l_ulong freeram; /* Available memory size */
129 l_ulong sharedram; /* Amount of shared memory */
130 l_ulong bufferram; /* Memory used by buffers */
131 l_ulong totalswap; /* Total swap space size */
132 l_ulong freeswap; /* swap space still available */
133 l_ushort procs; /* Number of current processes */
138 char _f[20-2*sizeof(l_long)-sizeof(l_int)]; /* padding */
141 struct l_pselect6arg {
146 static int linux_utimensat_nsec_valid(l_long);
150 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args)
152 struct l_sysinfo sysinfo;
156 bzero(&sysinfo, sizeof(sysinfo));
160 sysinfo.uptime = ts.tv_sec;
162 /* Use the information from the mib to get our load averages */
163 for (i = 0; i < 3; i++)
164 sysinfo.loads[i] = averunnable.ldavg[i] *
165 LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale;
167 sysinfo.totalram = physmem * PAGE_SIZE;
168 sysinfo.freeram = (u_long)vm_free_count() * PAGE_SIZE;
171 * sharedram counts pages allocated to named, swap-backed objects such
172 * as shared memory segments and tmpfs files. There is no cheap way to
173 * compute this, so just leave the field unpopulated. Linux itself only
174 * started setting this field in the 3.x timeframe.
176 sysinfo.sharedram = 0;
177 sysinfo.bufferram = 0;
179 swap_pager_status(&i, &j);
180 sysinfo.totalswap = i * PAGE_SIZE;
181 sysinfo.freeswap = (i - j) * PAGE_SIZE;
183 sysinfo.procs = nprocs;
186 * Platforms supported by the emulation layer do not have a notion of
189 sysinfo.totalhigh = 0;
190 sysinfo.freehigh = 0;
192 sysinfo.mem_unit = 1;
194 return (copyout(&sysinfo, args->info, sizeof(sysinfo)));
197 #ifdef LINUX_LEGACY_SYSCALLS
199 linux_alarm(struct thread *td, struct linux_alarm_args *args)
201 struct itimerval it, old_it;
207 printf(ARGS(alarm, "%u"), args->secs);
211 * Linux alarm() is always successful. Limit secs to INT32_MAX / 2
212 * to match kern_setitimer()'s limit to avoid error from it.
214 * XXX. Linux limit secs to INT_MAX on 32 and does not limit on 64-bit
217 if (secs > INT32_MAX / 2)
218 secs = INT32_MAX / 2;
220 it.it_value.tv_sec = secs;
221 it.it_value.tv_usec = 0;
222 timevalclear(&it.it_interval);
223 error = kern_setitimer(td, ITIMER_REAL, &it, &old_it);
224 KASSERT(error == 0, ("kern_setitimer returns %d", error));
226 if ((old_it.it_value.tv_sec == 0 && old_it.it_value.tv_usec > 0) ||
227 old_it.it_value.tv_usec >= 500000)
228 old_it.it_value.tv_sec++;
229 td->td_retval[0] = old_it.it_value.tv_sec;
235 linux_brk(struct thread *td, struct linux_brk_args *args)
237 struct vmspace *vm = td->td_proc->p_vmspace;
242 printf(ARGS(brk, "%p"), (void *)(uintptr_t)args->dsend);
244 old = (uintptr_t)vm->vm_daddr + ctob(vm->vm_dsize);
245 new = (uintptr_t)args->dsend;
246 if ((caddr_t)new > vm->vm_daddr && !kern_break(td, &new))
247 td->td_retval[0] = (register_t)new;
249 td->td_retval[0] = (register_t)old;
254 #if defined(__i386__)
255 /* XXX: what about amd64/linux32? */
258 linux_uselib(struct thread *td, struct linux_uselib_args *args)
264 vm_map_entry_t entry;
267 unsigned long file_offset;
268 unsigned long bss_size;
272 bool locked, opened, textset;
274 LCONVPATHEXIST(td, args->library, &library);
278 printf(ARGS(uselib, "%s"), library);
287 NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | AUDITVNODE1,
288 UIO_SYSSPACE, library, td);
295 NDFREE(&ni, NDF_ONLY_PNBUF);
298 * From here on down, we have a locked vnode that must be unlocked.
299 * XXX: The code below largely duplicates exec_check_permissions().
304 error = VOP_GETATTR(vp, &attr, td->td_ucred);
308 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
309 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
310 /* EACCESS is what exec(2) returns. */
316 if (attr.va_size == 0) {
321 /* Can we access it? */
322 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
327 * XXX: This should use vn_open() so that it is properly authorized,
328 * and to reduce code redundancy all over the place here.
329 * XXX: Not really, it duplicates far more of exec_check_permissions()
333 error = mac_vnode_check_open(td->td_ucred, vp, VREAD);
337 error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL);
342 /* Pull in executable header into exec_map */
343 error = vm_mmap(exec_map, (vm_offset_t *)&a_out, PAGE_SIZE,
344 VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 0);
348 /* Is it a Linux binary ? */
349 if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
355 * While we are here, we should REALLY do some more checks
358 /* Set file/virtual offset based on a.out variant. */
359 switch ((int)(a_out->a_magic & 0xffff)) {
360 case 0413: /* ZMAGIC */
363 case 0314: /* QMAGIC */
371 bss_size = round_page(a_out->a_bss);
373 /* Check various fields in header for validity/bounds. */
374 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
379 /* text + data can't exceed file size */
380 if (a_out->a_data + a_out->a_text > attr.va_size) {
386 * text/data/bss must not exceed limits
387 * XXX - this is not complete. it should check current usage PLUS
388 * the resources needed by this library.
390 PROC_LOCK(td->td_proc);
391 if (a_out->a_text > maxtsiz ||
392 a_out->a_data + bss_size > lim_cur_proc(td->td_proc, RLIMIT_DATA) ||
393 racct_set(td->td_proc, RACCT_DATA, a_out->a_data +
395 PROC_UNLOCK(td->td_proc);
399 PROC_UNLOCK(td->td_proc);
402 * Prevent more writers.
404 error = VOP_SET_TEXT(vp);
410 * Lock no longer needed
416 * Check if file_offset page aligned. Currently we cannot handle
417 * misalinged file offsets, and so we read in the entire image
420 if (file_offset & PAGE_MASK) {
422 printf("uselib: Non page aligned binary %lu\n", file_offset);
424 /* Map text+data read/write/execute */
426 /* a_entry is the load address and is page aligned */
427 vmaddr = trunc_page(a_out->a_entry);
429 /* get anon user mapping, read+write+execute */
430 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
431 &vmaddr, a_out->a_text + a_out->a_data, 0, VMFS_NO_SPACE,
432 VM_PROT_ALL, VM_PROT_ALL, 0);
436 error = vn_rdwr(UIO_READ, vp, (void *)vmaddr, file_offset,
437 a_out->a_text + a_out->a_data, UIO_USERSPACE, 0,
438 td->td_ucred, NOCRED, &aresid, td);
447 printf("uselib: Page aligned binary %lu\n", file_offset);
450 * for QMAGIC, a_entry is 20 bytes beyond the load address
451 * to skip the executable header
453 vmaddr = trunc_page(a_out->a_entry);
456 * Map it all into the process's space as a single
457 * copy-on-write "data" segment.
459 map = &td->td_proc->p_vmspace->vm_map;
460 error = vm_mmap(map, &vmaddr,
461 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
462 MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset);
466 if (!vm_map_lookup_entry(map, vmaddr, &entry)) {
471 entry->eflags |= MAP_ENTRY_VN_EXEC;
476 printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long *)vmaddr)[0],
477 ((long *)vmaddr)[1]);
480 /* Calculate BSS start address */
481 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
484 /* allocate some 'anon' space */
485 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
486 &vmaddr, bss_size, 0, VMFS_NO_SPACE, VM_PROT_ALL,
497 VOP_CLOSE(vp, FREAD, td->td_ucred, td);
502 VOP_LOCK(vp, LK_SHARED | LK_RETRY);
504 VOP_UNSET_TEXT_CHECKED(vp);
509 /* Release the temporary mapping. */
511 kmap_free_wakeup(exec_map, (vm_offset_t)a_out, PAGE_SIZE);
516 #endif /* __i386__ */
518 #ifdef LINUX_LEGACY_SYSCALLS
520 linux_select(struct thread *td, struct linux_select_args *args)
523 struct timeval tv0, tv1, utv, *tvp;
528 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
529 (void *)args->readfds, (void *)args->writefds,
530 (void *)args->exceptfds, (void *)args->timeout);
534 * Store current time for computation of the amount of
538 if ((error = copyin(args->timeout, <v, sizeof(ltv))))
540 utv.tv_sec = ltv.tv_sec;
541 utv.tv_usec = ltv.tv_usec;
544 printf(LMSG("incoming timeout (%jd/%ld)"),
545 (intmax_t)utv.tv_sec, utv.tv_usec);
548 if (itimerfix(&utv)) {
550 * The timeval was invalid. Convert it to something
551 * valid that will act as it does under Linux.
553 utv.tv_sec += utv.tv_usec / 1000000;
554 utv.tv_usec %= 1000000;
555 if (utv.tv_usec < 0) {
557 utv.tv_usec += 1000000;
567 error = kern_select(td, args->nfds, args->readfds, args->writefds,
568 args->exceptfds, tvp, LINUX_NFDBITS);
572 printf(LMSG("real select returns %d"), error);
578 if (td->td_retval[0]) {
580 * Compute how much time was left of the timeout,
581 * by subtracting the current time and the time
582 * before we started the call, and subtracting
583 * that result from the user-supplied value.
586 timevalsub(&tv1, &tv0);
587 timevalsub(&utv, &tv1);
594 printf(LMSG("outgoing timeout (%jd/%ld)"),
595 (intmax_t)utv.tv_sec, utv.tv_usec);
597 ltv.tv_sec = utv.tv_sec;
598 ltv.tv_usec = utv.tv_usec;
599 if ((error = copyout(<v, args->timeout, sizeof(ltv))))
606 printf(LMSG("select_out -> %d"), error);
613 linux_mremap(struct thread *td, struct linux_mremap_args *args)
621 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
622 (void *)(uintptr_t)args->addr,
623 (unsigned long)args->old_len,
624 (unsigned long)args->new_len,
625 (unsigned long)args->flags);
628 if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) {
629 td->td_retval[0] = 0;
634 * Check for the page alignment.
635 * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK.
637 if (args->addr & PAGE_MASK) {
638 td->td_retval[0] = 0;
642 args->new_len = round_page(args->new_len);
643 args->old_len = round_page(args->old_len);
645 if (args->new_len > args->old_len) {
646 td->td_retval[0] = 0;
650 if (args->new_len < args->old_len) {
651 addr = args->addr + args->new_len;
652 len = args->old_len - args->new_len;
653 error = kern_munmap(td, addr, len);
656 td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
660 #define LINUX_MS_ASYNC 0x0001
661 #define LINUX_MS_INVALIDATE 0x0002
662 #define LINUX_MS_SYNC 0x0004
665 linux_msync(struct thread *td, struct linux_msync_args *args)
668 return (kern_msync(td, args->addr, args->len,
669 args->fl & ~LINUX_MS_SYNC));
672 #ifdef LINUX_LEGACY_SYSCALLS
674 linux_time(struct thread *td, struct linux_time_args *args)
682 printf(ARGS(time, "*"));
687 if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
689 td->td_retval[0] = tm;
694 struct l_times_argv {
697 l_clock_t tms_cutime;
698 l_clock_t tms_cstime;
703 * Glibc versions prior to 2.2.1 always use hard-coded CLK_TCK value.
704 * Since 2.2.1 Glibc uses value exported from kernel via AT_CLKTCK
705 * auxiliary vector entry.
709 #define CONVOTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
710 #define CONVNTCK(r) (r.tv_sec * stclohz + r.tv_usec / (1000000 / stclohz))
712 #define CONVTCK(r) (linux_kernver(td) >= LINUX_KERNVER_2004000 ? \
713 CONVNTCK(r) : CONVOTCK(r))
716 linux_times(struct thread *td, struct linux_times_args *args)
718 struct timeval tv, utime, stime, cutime, cstime;
719 struct l_times_argv tms;
725 printf(ARGS(times, "*"));
728 if (args->buf != NULL) {
732 calcru(p, &utime, &stime);
734 calccru(p, &cutime, &cstime);
737 tms.tms_utime = CONVTCK(utime);
738 tms.tms_stime = CONVTCK(stime);
740 tms.tms_cutime = CONVTCK(cutime);
741 tms.tms_cstime = CONVTCK(cstime);
743 if ((error = copyout(&tms, args->buf, sizeof(tms))))
748 td->td_retval[0] = (int)CONVTCK(tv);
753 linux_newuname(struct thread *td, struct linux_newuname_args *args)
755 struct l_new_utsname utsname;
756 char osname[LINUX_MAX_UTSNAME];
757 char osrelease[LINUX_MAX_UTSNAME];
761 if (ldebug(newuname))
762 printf(ARGS(newuname, "*"));
765 linux_get_osname(td, osname);
766 linux_get_osrelease(td, osrelease);
768 bzero(&utsname, sizeof(utsname));
769 strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
770 getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
771 getcreddomainname(td->td_ucred, utsname.domainname, LINUX_MAX_UTSNAME);
772 strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
773 strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
774 for (p = utsname.version; *p != '\0'; ++p)
779 strlcpy(utsname.machine, linux_kplatform, LINUX_MAX_UTSNAME);
781 return (copyout(&utsname, args->buf, sizeof(utsname)));
789 #ifdef LINUX_LEGACY_SYSCALLS
791 linux_utime(struct thread *td, struct linux_utime_args *args)
793 struct timeval tv[2], *tvp;
794 struct l_utimbuf lut;
798 LCONVPATHEXIST(td, args->fname, &fname);
802 printf(ARGS(utime, "%s, *"), fname);
806 if ((error = copyin(args->times, &lut, sizeof lut))) {
810 tv[0].tv_sec = lut.l_actime;
812 tv[1].tv_sec = lut.l_modtime;
818 error = kern_utimesat(td, AT_FDCWD, fname, UIO_SYSSPACE, tvp,
825 #ifdef LINUX_LEGACY_SYSCALLS
827 linux_utimes(struct thread *td, struct linux_utimes_args *args)
830 struct timeval tv[2], *tvp = NULL;
834 LCONVPATHEXIST(td, args->fname, &fname);
838 printf(ARGS(utimes, "%s, *"), fname);
841 if (args->tptr != NULL) {
842 if ((error = copyin(args->tptr, ltv, sizeof ltv))) {
846 tv[0].tv_sec = ltv[0].tv_sec;
847 tv[0].tv_usec = ltv[0].tv_usec;
848 tv[1].tv_sec = ltv[1].tv_sec;
849 tv[1].tv_usec = ltv[1].tv_usec;
853 error = kern_utimesat(td, AT_FDCWD, fname, UIO_SYSSPACE,
861 linux_utimensat_nsec_valid(l_long nsec)
864 if (nsec == LINUX_UTIME_OMIT || nsec == LINUX_UTIME_NOW)
866 if (nsec >= 0 && nsec <= 999999999)
872 linux_utimensat(struct thread *td, struct linux_utimensat_args *args)
874 struct l_timespec l_times[2];
875 struct timespec times[2], *timesp = NULL;
877 int error, dfd, flags = 0;
879 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
882 if (ldebug(utimensat))
883 printf(ARGS(utimensat, "%d, *"), dfd);
886 if (args->flags & ~LINUX_AT_SYMLINK_NOFOLLOW)
889 if (args->times != NULL) {
890 error = copyin(args->times, l_times, sizeof(l_times));
894 if (linux_utimensat_nsec_valid(l_times[0].tv_nsec) != 0 ||
895 linux_utimensat_nsec_valid(l_times[1].tv_nsec) != 0)
898 times[0].tv_sec = l_times[0].tv_sec;
899 switch (l_times[0].tv_nsec)
901 case LINUX_UTIME_OMIT:
902 times[0].tv_nsec = UTIME_OMIT;
904 case LINUX_UTIME_NOW:
905 times[0].tv_nsec = UTIME_NOW;
908 times[0].tv_nsec = l_times[0].tv_nsec;
911 times[1].tv_sec = l_times[1].tv_sec;
912 switch (l_times[1].tv_nsec)
914 case LINUX_UTIME_OMIT:
915 times[1].tv_nsec = UTIME_OMIT;
917 case LINUX_UTIME_NOW:
918 times[1].tv_nsec = UTIME_NOW;
921 times[1].tv_nsec = l_times[1].tv_nsec;
926 /* This breaks POSIX, but is what the Linux kernel does
927 * _on purpose_ (documented in the man page for utimensat(2)),
928 * so we must follow that behaviour. */
929 if (times[0].tv_nsec == UTIME_OMIT &&
930 times[1].tv_nsec == UTIME_OMIT)
934 if (args->pathname != NULL)
935 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
936 else if (args->flags != 0)
939 if (args->flags & LINUX_AT_SYMLINK_NOFOLLOW)
940 flags |= AT_SYMLINK_NOFOLLOW;
943 error = kern_futimens(td, dfd, timesp, UIO_SYSSPACE);
945 error = kern_utimensat(td, dfd, path, UIO_SYSSPACE, timesp,
946 UIO_SYSSPACE, flags);
953 #ifdef LINUX_LEGACY_SYSCALLS
955 linux_futimesat(struct thread *td, struct linux_futimesat_args *args)
958 struct timeval tv[2], *tvp = NULL;
962 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
963 LCONVPATHEXIST_AT(td, args->filename, &fname, dfd);
966 if (ldebug(futimesat))
967 printf(ARGS(futimesat, "%s, *"), fname);
970 if (args->utimes != NULL) {
971 if ((error = copyin(args->utimes, ltv, sizeof ltv))) {
975 tv[0].tv_sec = ltv[0].tv_sec;
976 tv[0].tv_usec = ltv[0].tv_usec;
977 tv[1].tv_sec = ltv[1].tv_sec;
978 tv[1].tv_usec = ltv[1].tv_usec;
982 error = kern_utimesat(td, dfd, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
989 linux_common_wait(struct thread *td, int pid, int *statusp,
990 int options, struct __wrusage *wrup)
995 int error, status, tmpstat;
997 if (pid == WAIT_ANY) {
1000 } else if (pid < 0) {
1009 * For backward compatibility we implicitly add flags WEXITED
1010 * and WTRAPPED here.
1012 options |= WEXITED | WTRAPPED;
1013 error = kern_wait6(td, idtype, id, &status, options, wrup, &siginfo);
1018 tmpstat = status & 0xffff;
1019 if (WIFSIGNALED(tmpstat)) {
1020 tmpstat = (tmpstat & 0xffffff80) |
1021 bsd_to_linux_signal(WTERMSIG(tmpstat));
1022 } else if (WIFSTOPPED(tmpstat)) {
1023 tmpstat = (tmpstat & 0xffff00ff) |
1024 (bsd_to_linux_signal(WSTOPSIG(tmpstat)) << 8);
1025 #if defined(__amd64__) && !defined(COMPAT_LINUX32)
1026 if (WSTOPSIG(status) == SIGTRAP) {
1027 tmpstat = linux_ptrace_status(td,
1028 siginfo.si_pid, tmpstat);
1031 } else if (WIFCONTINUED(tmpstat)) {
1034 error = copyout(&tmpstat, statusp, sizeof(int));
1040 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1042 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
1044 struct linux_wait4_args wait4_args;
1047 if (ldebug(waitpid))
1048 printf(ARGS(waitpid, "%d, %p, %d"),
1049 args->pid, (void *)args->status, args->options);
1052 wait4_args.pid = args->pid;
1053 wait4_args.status = args->status;
1054 wait4_args.options = args->options;
1055 wait4_args.rusage = NULL;
1057 return (linux_wait4(td, &wait4_args));
1059 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1062 linux_wait4(struct thread *td, struct linux_wait4_args *args)
1065 struct __wrusage wru, *wrup;
1069 printf(ARGS(wait4, "%d, %p, %d, %p"),
1070 args->pid, (void *)args->status, args->options,
1071 (void *)args->rusage);
1073 if (args->options & ~(LINUX_WUNTRACED | LINUX_WNOHANG |
1074 LINUX_WCONTINUED | __WCLONE | __WNOTHREAD | __WALL))
1078 linux_to_bsd_waitopts(args->options, &options);
1080 if (args->rusage != NULL)
1084 error = linux_common_wait(td, args->pid, args->status, options, wrup);
1087 if (args->rusage != NULL)
1088 error = linux_copyout_rusage(&wru.wru_self, args->rusage);
1093 linux_waitid(struct thread *td, struct linux_waitid_args *args)
1095 int status, options, sig;
1096 struct __wrusage wru;
1104 linux_to_bsd_waitopts(args->options, &options);
1106 if (options & ~(WNOHANG | WNOWAIT | WEXITED | WUNTRACED | WCONTINUED))
1108 if (!(options & (WEXITED | WUNTRACED | WCONTINUED)))
1111 switch (args->idtype) {
1129 error = kern_wait6(td, idtype, args->id, &status, options,
1133 if (args->rusage != NULL) {
1134 error = linux_copyout_rusage(&wru.wru_children,
1139 if (args->info != NULL) {
1141 bzero(&lsi, sizeof(lsi));
1142 if (td->td_retval[0] != 0) {
1143 sig = bsd_to_linux_signal(siginfo.si_signo);
1144 siginfo_to_lsiginfo(&siginfo, &lsi, sig);
1146 error = copyout(&lsi, args->info, sizeof(lsi));
1148 td->td_retval[0] = 0;
1153 #ifdef LINUX_LEGACY_SYSCALLS
1155 linux_mknod(struct thread *td, struct linux_mknod_args *args)
1160 LCONVPATHCREAT(td, args->path, &path);
1164 printf(ARGS(mknod, "%s, %d, %ju"), path, args->mode,
1165 (uintmax_t)args->dev);
1168 switch (args->mode & S_IFMT) {
1171 error = kern_mkfifoat(td, AT_FDCWD, path, UIO_SYSSPACE,
1177 error = kern_mknodat(td, AT_FDCWD, path, UIO_SYSSPACE,
1178 args->mode, args->dev);
1186 args->mode |= S_IFREG;
1189 error = kern_openat(td, AT_FDCWD, path, UIO_SYSSPACE,
1190 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1192 kern_close(td, td->td_retval[0]);
1205 linux_mknodat(struct thread *td, struct linux_mknodat_args *args)
1210 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1211 LCONVPATHCREAT_AT(td, args->filename, &path, dfd);
1214 if (ldebug(mknodat))
1215 printf(ARGS(mknodat, "%s, %d, %d"), path, args->mode, args->dev);
1218 switch (args->mode & S_IFMT) {
1221 error = kern_mkfifoat(td, dfd, path, UIO_SYSSPACE, args->mode);
1226 error = kern_mknodat(td, dfd, path, UIO_SYSSPACE, args->mode,
1235 args->mode |= S_IFREG;
1238 error = kern_openat(td, dfd, path, UIO_SYSSPACE,
1239 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1241 kern_close(td, td->td_retval[0]);
1253 * UGH! This is just about the dumbest idea I've ever heard!!
1256 linux_personality(struct thread *td, struct linux_personality_args *args)
1258 struct linux_pemuldata *pem;
1259 struct proc *p = td->td_proc;
1263 if (ldebug(personality))
1264 printf(ARGS(personality, "%u"), args->per);
1270 if (args->per != 0xffffffff)
1271 pem->persona = args->per;
1274 td->td_retval[0] = old;
1278 struct l_itimerval {
1279 l_timeval it_interval;
1283 #define B2L_ITIMERVAL(bip, lip) \
1284 (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec; \
1285 (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec; \
1286 (bip)->it_value.tv_sec = (lip)->it_value.tv_sec; \
1287 (bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
1290 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
1293 struct l_itimerval ls;
1294 struct itimerval aitv, oitv;
1297 if (ldebug(setitimer))
1298 printf(ARGS(setitimer, "%p, %p"),
1299 (void *)uap->itv, (void *)uap->oitv);
1302 if (uap->itv == NULL) {
1303 uap->itv = uap->oitv;
1304 return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
1307 error = copyin(uap->itv, &ls, sizeof(ls));
1310 B2L_ITIMERVAL(&aitv, &ls);
1312 if (ldebug(setitimer)) {
1313 printf("setitimer: value: sec: %jd, usec: %ld\n",
1314 (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec);
1315 printf("setitimer: interval: sec: %jd, usec: %ld\n",
1316 (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec);
1319 error = kern_setitimer(td, uap->which, &aitv, &oitv);
1320 if (error != 0 || uap->oitv == NULL)
1322 B2L_ITIMERVAL(&ls, &oitv);
1324 return (copyout(&ls, uap->oitv, sizeof(ls)));
1328 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
1331 struct l_itimerval ls;
1332 struct itimerval aitv;
1335 if (ldebug(getitimer))
1336 printf(ARGS(getitimer, "%p"), (void *)uap->itv);
1338 error = kern_getitimer(td, uap->which, &aitv);
1341 B2L_ITIMERVAL(&ls, &aitv);
1342 return (copyout(&ls, uap->itv, sizeof(ls)));
1345 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1347 linux_nice(struct thread *td, struct linux_nice_args *args)
1349 struct setpriority_args bsd_args;
1351 bsd_args.which = PRIO_PROCESS;
1352 bsd_args.who = 0; /* current process */
1353 bsd_args.prio = args->inc;
1354 return (sys_setpriority(td, &bsd_args));
1356 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1359 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
1361 struct ucred *newcred, *oldcred;
1362 l_gid_t *linux_gidset;
1367 ngrp = args->gidsetsize;
1368 if (ngrp < 0 || ngrp >= ngroups_max + 1)
1370 linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_LINUX, M_WAITOK);
1371 error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
1375 crextend(newcred, ngrp + 1);
1378 oldcred = p->p_ucred;
1379 crcopy(newcred, oldcred);
1382 * cr_groups[0] holds egid. Setting the whole set from
1383 * the supplied set will cause egid to be changed too.
1384 * Keep cr_groups[0] unchanged to prevent that.
1387 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0) {
1394 newcred->cr_ngroups = ngrp + 1;
1396 bsd_gidset = newcred->cr_groups;
1399 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1403 newcred->cr_ngroups = 1;
1406 proc_set_cred(p, newcred);
1411 free(linux_gidset, M_LINUX);
1416 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1419 l_gid_t *linux_gidset;
1421 int bsd_gidsetsz, ngrp, error;
1423 cred = td->td_ucred;
1424 bsd_gidset = cred->cr_groups;
1425 bsd_gidsetsz = cred->cr_ngroups - 1;
1428 * cr_groups[0] holds egid. Returning the whole set
1429 * here will cause a duplicate. Exclude cr_groups[0]
1433 if ((ngrp = args->gidsetsize) == 0) {
1434 td->td_retval[0] = bsd_gidsetsz;
1438 if (ngrp < bsd_gidsetsz)
1442 linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset),
1444 while (ngrp < bsd_gidsetsz) {
1445 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1449 error = copyout(linux_gidset, args->grouplist, ngrp * sizeof(l_gid_t));
1450 free(linux_gidset, M_LINUX);
1454 td->td_retval[0] = ngrp;
1459 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1461 struct rlimit bsd_rlim;
1462 struct l_rlimit rlim;
1467 if (ldebug(setrlimit))
1468 printf(ARGS(setrlimit, "%d, %p"),
1469 args->resource, (void *)args->rlim);
1472 if (args->resource >= LINUX_RLIM_NLIMITS)
1475 which = linux_to_bsd_resource[args->resource];
1479 error = copyin(args->rlim, &rlim, sizeof(rlim));
1483 bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1484 bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1485 return (kern_setrlimit(td, which, &bsd_rlim));
1488 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1490 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1492 struct l_rlimit rlim;
1493 struct rlimit bsd_rlim;
1497 if (ldebug(old_getrlimit))
1498 printf(ARGS(old_getrlimit, "%d, %p"),
1499 args->resource, (void *)args->rlim);
1502 if (args->resource >= LINUX_RLIM_NLIMITS)
1505 which = linux_to_bsd_resource[args->resource];
1509 lim_rlimit(td, which, &bsd_rlim);
1511 #ifdef COMPAT_LINUX32
1512 rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1513 if (rlim.rlim_cur == UINT_MAX)
1514 rlim.rlim_cur = INT_MAX;
1515 rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1516 if (rlim.rlim_max == UINT_MAX)
1517 rlim.rlim_max = INT_MAX;
1519 rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1520 if (rlim.rlim_cur == ULONG_MAX)
1521 rlim.rlim_cur = LONG_MAX;
1522 rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1523 if (rlim.rlim_max == ULONG_MAX)
1524 rlim.rlim_max = LONG_MAX;
1526 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1528 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1531 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1533 struct l_rlimit rlim;
1534 struct rlimit bsd_rlim;
1538 if (ldebug(getrlimit))
1539 printf(ARGS(getrlimit, "%d, %p"),
1540 args->resource, (void *)args->rlim);
1543 if (args->resource >= LINUX_RLIM_NLIMITS)
1546 which = linux_to_bsd_resource[args->resource];
1550 lim_rlimit(td, which, &bsd_rlim);
1552 rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1553 rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1554 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1558 linux_sched_setscheduler(struct thread *td,
1559 struct linux_sched_setscheduler_args *args)
1561 struct sched_param sched_param;
1566 if (ldebug(sched_setscheduler))
1567 printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1568 args->pid, args->policy, (const void *)args->param);
1571 switch (args->policy) {
1572 case LINUX_SCHED_OTHER:
1573 policy = SCHED_OTHER;
1575 case LINUX_SCHED_FIFO:
1576 policy = SCHED_FIFO;
1578 case LINUX_SCHED_RR:
1585 error = copyin(args->param, &sched_param, sizeof(sched_param));
1589 tdt = linux_tdfind(td, args->pid, -1);
1593 error = kern_sched_setscheduler(td, tdt, policy, &sched_param);
1594 PROC_UNLOCK(tdt->td_proc);
1599 linux_sched_getscheduler(struct thread *td,
1600 struct linux_sched_getscheduler_args *args)
1606 if (ldebug(sched_getscheduler))
1607 printf(ARGS(sched_getscheduler, "%d"), args->pid);
1610 tdt = linux_tdfind(td, args->pid, -1);
1614 error = kern_sched_getscheduler(td, tdt, &policy);
1615 PROC_UNLOCK(tdt->td_proc);
1619 td->td_retval[0] = LINUX_SCHED_OTHER;
1622 td->td_retval[0] = LINUX_SCHED_FIFO;
1625 td->td_retval[0] = LINUX_SCHED_RR;
1632 linux_sched_get_priority_max(struct thread *td,
1633 struct linux_sched_get_priority_max_args *args)
1635 struct sched_get_priority_max_args bsd;
1638 if (ldebug(sched_get_priority_max))
1639 printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1642 switch (args->policy) {
1643 case LINUX_SCHED_OTHER:
1644 bsd.policy = SCHED_OTHER;
1646 case LINUX_SCHED_FIFO:
1647 bsd.policy = SCHED_FIFO;
1649 case LINUX_SCHED_RR:
1650 bsd.policy = SCHED_RR;
1655 return (sys_sched_get_priority_max(td, &bsd));
1659 linux_sched_get_priority_min(struct thread *td,
1660 struct linux_sched_get_priority_min_args *args)
1662 struct sched_get_priority_min_args bsd;
1665 if (ldebug(sched_get_priority_min))
1666 printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1669 switch (args->policy) {
1670 case LINUX_SCHED_OTHER:
1671 bsd.policy = SCHED_OTHER;
1673 case LINUX_SCHED_FIFO:
1674 bsd.policy = SCHED_FIFO;
1676 case LINUX_SCHED_RR:
1677 bsd.policy = SCHED_RR;
1682 return (sys_sched_get_priority_min(td, &bsd));
1685 #define REBOOT_CAD_ON 0x89abcdef
1686 #define REBOOT_CAD_OFF 0
1687 #define REBOOT_HALT 0xcdef0123
1688 #define REBOOT_RESTART 0x01234567
1689 #define REBOOT_RESTART2 0xA1B2C3D4
1690 #define REBOOT_POWEROFF 0x4321FEDC
1691 #define REBOOT_MAGIC1 0xfee1dead
1692 #define REBOOT_MAGIC2 0x28121969
1693 #define REBOOT_MAGIC2A 0x05121996
1694 #define REBOOT_MAGIC2B 0x16041998
1697 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1699 struct reboot_args bsd_args;
1703 printf(ARGS(reboot, "0x%x"), args->cmd);
1706 if (args->magic1 != REBOOT_MAGIC1)
1709 switch (args->magic2) {
1711 case REBOOT_MAGIC2A:
1712 case REBOOT_MAGIC2B:
1718 switch (args->cmd) {
1720 case REBOOT_CAD_OFF:
1721 return (priv_check(td, PRIV_REBOOT));
1723 bsd_args.opt = RB_HALT;
1725 case REBOOT_RESTART:
1726 case REBOOT_RESTART2:
1729 case REBOOT_POWEROFF:
1730 bsd_args.opt = RB_POWEROFF;
1735 return (sys_reboot(td, &bsd_args));
1740 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1745 printf(ARGS(getpid, ""));
1747 td->td_retval[0] = td->td_proc->p_pid;
1753 linux_gettid(struct thread *td, struct linux_gettid_args *args)
1755 struct linux_emuldata *em;
1759 printf(ARGS(gettid, ""));
1763 KASSERT(em != NULL, ("gettid: emuldata not found.\n"));
1765 td->td_retval[0] = em->em_tid;
1772 linux_getppid(struct thread *td, struct linux_getppid_args *args)
1776 if (ldebug(getppid))
1777 printf(ARGS(getppid, ""));
1780 td->td_retval[0] = kern_getppid(td);
1785 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1790 printf(ARGS(getgid, ""));
1793 td->td_retval[0] = td->td_ucred->cr_rgid;
1798 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1803 printf(ARGS(getuid, ""));
1806 td->td_retval[0] = td->td_ucred->cr_ruid;
1812 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1814 struct getsid_args bsd;
1818 printf(ARGS(getsid, "%i"), args->pid);
1821 bsd.pid = args->pid;
1822 return (sys_getsid(td, &bsd));
1826 linux_nosys(struct thread *td, struct nosys_args *ignore)
1833 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1835 struct getpriority_args bsd_args;
1839 if (ldebug(getpriority))
1840 printf(ARGS(getpriority, "%i, %i"), args->which, args->who);
1843 bsd_args.which = args->which;
1844 bsd_args.who = args->who;
1845 error = sys_getpriority(td, &bsd_args);
1846 td->td_retval[0] = 20 - td->td_retval[0];
1851 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1856 if (ldebug(sethostname))
1857 printf(ARGS(sethostname, "*, %i"), args->len);
1861 name[1] = KERN_HOSTNAME;
1862 return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1867 linux_setdomainname(struct thread *td, struct linux_setdomainname_args *args)
1872 if (ldebug(setdomainname))
1873 printf(ARGS(setdomainname, "*, %i"), args->len);
1877 name[1] = KERN_NISDOMAINNAME;
1878 return (userland_sysctl(td, name, 2, 0, 0, 0, args->name,
1883 linux_exit_group(struct thread *td, struct linux_exit_group_args *args)
1887 if (ldebug(exit_group))
1888 printf(ARGS(exit_group, "%i"), args->error_code);
1891 LINUX_CTR2(exit_group, "thread(%d) (%d)", td->td_tid,
1895 * XXX: we should send a signal to the parent if
1896 * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?)
1897 * as it doesnt occur often.
1899 exit1(td, args->error_code, 0);
1903 #define _LINUX_CAPABILITY_VERSION_1 0x19980330
1904 #define _LINUX_CAPABILITY_VERSION_2 0x20071026
1905 #define _LINUX_CAPABILITY_VERSION_3 0x20080522
1907 struct l_user_cap_header {
1912 struct l_user_cap_data {
1919 linux_capget(struct thread *td, struct linux_capget_args *uap)
1921 struct l_user_cap_header luch;
1922 struct l_user_cap_data lucd[2];
1925 if (uap->hdrp == NULL)
1928 error = copyin(uap->hdrp, &luch, sizeof(luch));
1932 switch (luch.version) {
1933 case _LINUX_CAPABILITY_VERSION_1:
1936 case _LINUX_CAPABILITY_VERSION_2:
1937 case _LINUX_CAPABILITY_VERSION_3:
1943 printf(LMSG("invalid capget capability version 0x%x"),
1946 luch.version = _LINUX_CAPABILITY_VERSION_1;
1947 error = copyout(&luch, uap->hdrp, sizeof(luch));
1958 * The current implementation doesn't support setting
1959 * a capability (it's essentially a stub) so indicate
1960 * that no capabilities are currently set or available
1963 memset(&lucd, 0, u32s * sizeof(lucd[0]));
1964 error = copyout(&lucd, uap->datap, u32s * sizeof(lucd[0]));
1971 linux_capset(struct thread *td, struct linux_capset_args *uap)
1973 struct l_user_cap_header luch;
1974 struct l_user_cap_data lucd[2];
1977 if (uap->hdrp == NULL || uap->datap == NULL)
1980 error = copyin(uap->hdrp, &luch, sizeof(luch));
1984 switch (luch.version) {
1985 case _LINUX_CAPABILITY_VERSION_1:
1988 case _LINUX_CAPABILITY_VERSION_2:
1989 case _LINUX_CAPABILITY_VERSION_3:
1995 printf(LMSG("invalid capset capability version 0x%x"),
1998 luch.version = _LINUX_CAPABILITY_VERSION_1;
1999 error = copyout(&luch, uap->hdrp, sizeof(luch));
2008 error = copyin(uap->datap, &lucd, u32s * sizeof(lucd[0]));
2012 /* We currently don't support setting any capabilities. */
2013 for (i = 0; i < u32s; i++) {
2014 if (lucd[i].effective || lucd[i].permitted ||
2015 lucd[i].inheritable) {
2017 "capset[%d] effective=0x%x, permitted=0x%x, "
2018 "inheritable=0x%x is not implemented", i,
2019 (int)lucd[i].effective, (int)lucd[i].permitted,
2020 (int)lucd[i].inheritable);
2029 linux_prctl(struct thread *td, struct linux_prctl_args *args)
2031 int error = 0, max_size;
2032 struct proc *p = td->td_proc;
2033 char comm[LINUX_MAX_COMM_LEN];
2038 printf(ARGS(prctl, "%d, %ju, %ju, %ju, %ju"), args->option,
2039 (uintmax_t)args->arg2, (uintmax_t)args->arg3,
2040 (uintmax_t)args->arg4, (uintmax_t)args->arg5);
2043 switch (args->option) {
2044 case LINUX_PR_SET_PDEATHSIG:
2045 if (!LINUX_SIG_VALID(args->arg2))
2047 pdeath_signal = linux_to_bsd_signal(args->arg2);
2048 return (kern_procctl(td, P_PID, 0, PROC_PDEATHSIG_CTL,
2050 case LINUX_PR_GET_PDEATHSIG:
2051 error = kern_procctl(td, P_PID, 0, PROC_PDEATHSIG_STATUS,
2055 pdeath_signal = bsd_to_linux_signal(pdeath_signal);
2056 return (copyout(&pdeath_signal,
2057 (void *)(register_t)args->arg2,
2058 sizeof(pdeath_signal)));
2060 case LINUX_PR_GET_KEEPCAPS:
2062 * Indicate that we always clear the effective and
2063 * permitted capability sets when the user id becomes
2064 * non-zero (actually the capability sets are simply
2065 * always zero in the current implementation).
2067 td->td_retval[0] = 0;
2069 case LINUX_PR_SET_KEEPCAPS:
2071 * Ignore requests to keep the effective and permitted
2072 * capability sets when the user id becomes non-zero.
2075 case LINUX_PR_SET_NAME:
2077 * To be on the safe side we need to make sure to not
2078 * overflow the size a Linux program expects. We already
2079 * do this here in the copyin, so that we don't need to
2082 max_size = MIN(sizeof(comm), sizeof(p->p_comm));
2083 error = copyinstr((void *)(register_t)args->arg2, comm,
2086 /* Linux silently truncates the name if it is too long. */
2087 if (error == ENAMETOOLONG) {
2089 * XXX: copyinstr() isn't documented to populate the
2090 * array completely, so do a copyin() to be on the
2091 * safe side. This should be changed in case
2092 * copyinstr() is changed to guarantee this.
2094 error = copyin((void *)(register_t)args->arg2, comm,
2096 comm[max_size - 1] = '\0';
2102 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
2105 case LINUX_PR_GET_NAME:
2107 strlcpy(comm, p->p_comm, sizeof(comm));
2109 error = copyout(comm, (void *)(register_t)args->arg2,
2121 linux_sched_setparam(struct thread *td,
2122 struct linux_sched_setparam_args *uap)
2124 struct sched_param sched_param;
2129 if (ldebug(sched_setparam))
2130 printf(ARGS(sched_setparam, "%d, *"), uap->pid);
2133 error = copyin(uap->param, &sched_param, sizeof(sched_param));
2137 tdt = linux_tdfind(td, uap->pid, -1);
2141 error = kern_sched_setparam(td, tdt, &sched_param);
2142 PROC_UNLOCK(tdt->td_proc);
2147 linux_sched_getparam(struct thread *td,
2148 struct linux_sched_getparam_args *uap)
2150 struct sched_param sched_param;
2155 if (ldebug(sched_getparam))
2156 printf(ARGS(sched_getparam, "%d, *"), uap->pid);
2159 tdt = linux_tdfind(td, uap->pid, -1);
2163 error = kern_sched_getparam(td, tdt, &sched_param);
2164 PROC_UNLOCK(tdt->td_proc);
2166 error = copyout(&sched_param, uap->param,
2167 sizeof(sched_param));
2172 * Get affinity of a process.
2175 linux_sched_getaffinity(struct thread *td,
2176 struct linux_sched_getaffinity_args *args)
2182 if (ldebug(sched_getaffinity))
2183 printf(ARGS(sched_getaffinity, "%d, %d, *"), args->pid,
2186 if (args->len < sizeof(cpuset_t))
2189 tdt = linux_tdfind(td, args->pid, -1);
2193 PROC_UNLOCK(tdt->td_proc);
2195 error = kern_cpuset_getaffinity(td, CPU_LEVEL_WHICH, CPU_WHICH_TID,
2196 tdt->td_tid, sizeof(cpuset_t), (cpuset_t *)args->user_mask_ptr);
2198 td->td_retval[0] = sizeof(cpuset_t);
2204 * Set affinity of a process.
2207 linux_sched_setaffinity(struct thread *td,
2208 struct linux_sched_setaffinity_args *args)
2213 if (ldebug(sched_setaffinity))
2214 printf(ARGS(sched_setaffinity, "%d, %d, *"), args->pid,
2217 if (args->len < sizeof(cpuset_t))
2220 tdt = linux_tdfind(td, args->pid, -1);
2224 PROC_UNLOCK(tdt->td_proc);
2226 return (kern_cpuset_setaffinity(td, CPU_LEVEL_WHICH, CPU_WHICH_TID,
2227 tdt->td_tid, sizeof(cpuset_t), (cpuset_t *) args->user_mask_ptr));
2230 struct linux_rlimit64 {
2236 linux_prlimit64(struct thread *td, struct linux_prlimit64_args *args)
2238 struct rlimit rlim, nrlim;
2239 struct linux_rlimit64 lrlim;
2246 if (ldebug(prlimit64))
2247 printf(ARGS(prlimit64, "%d, %d, %p, %p"), args->pid,
2248 args->resource, (void *)args->new, (void *)args->old);
2251 if (args->resource >= LINUX_RLIM_NLIMITS)
2254 which = linux_to_bsd_resource[args->resource];
2258 if (args->new != NULL) {
2260 * Note. Unlike FreeBSD where rlim is signed 64-bit Linux
2261 * rlim is unsigned 64-bit. FreeBSD treats negative limits
2262 * as INFINITY so we do not need a conversion even.
2264 error = copyin(args->new, &nrlim, sizeof(nrlim));
2269 flags = PGET_HOLD | PGET_NOTWEXIT;
2270 if (args->new != NULL)
2271 flags |= PGET_CANDEBUG;
2273 flags |= PGET_CANSEE;
2274 error = pget(args->pid, flags, &p);
2278 if (args->old != NULL) {
2280 lim_rlimit_proc(p, which, &rlim);
2282 if (rlim.rlim_cur == RLIM_INFINITY)
2283 lrlim.rlim_cur = LINUX_RLIM_INFINITY;
2285 lrlim.rlim_cur = rlim.rlim_cur;
2286 if (rlim.rlim_max == RLIM_INFINITY)
2287 lrlim.rlim_max = LINUX_RLIM_INFINITY;
2289 lrlim.rlim_max = rlim.rlim_max;
2290 error = copyout(&lrlim, args->old, sizeof(lrlim));
2295 if (args->new != NULL)
2296 error = kern_proc_setrlimit(td, p, which, &nrlim);
2304 linux_pselect6(struct thread *td, struct linux_pselect6_args *args)
2306 struct timeval utv, tv0, tv1, *tvp;
2307 struct l_pselect6arg lpse6;
2308 struct l_timespec lts;
2309 struct timespec uts;
2316 if (args->sig != NULL) {
2317 error = copyin(args->sig, &lpse6, sizeof(lpse6));
2320 if (lpse6.ss_len != sizeof(l_ss))
2322 if (lpse6.ss != 0) {
2323 error = copyin(PTRIN(lpse6.ss), &l_ss,
2327 linux_to_bsd_sigset(&l_ss, &ss);
2333 * Currently glibc changes nanosecond number to microsecond.
2334 * This mean losing precision but for now it is hardly seen.
2336 if (args->tsp != NULL) {
2337 error = copyin(args->tsp, <s, sizeof(lts));
2340 error = linux_to_native_timespec(&uts, <s);
2344 TIMESPEC_TO_TIMEVAL(&utv, &uts);
2345 if (itimerfix(&utv))
2353 error = kern_pselect(td, args->nfds, args->readfds, args->writefds,
2354 args->exceptfds, tvp, ssp, LINUX_NFDBITS);
2356 if (error == 0 && args->tsp != NULL) {
2357 if (td->td_retval[0] != 0) {
2359 * Compute how much time was left of the timeout,
2360 * by subtracting the current time and the time
2361 * before we started the call, and subtracting
2362 * that result from the user-supplied value.
2366 timevalsub(&tv1, &tv0);
2367 timevalsub(&utv, &tv1);
2373 TIMEVAL_TO_TIMESPEC(&utv, &uts);
2375 error = native_to_linux_timespec(<s, &uts);
2377 error = copyout(<s, args->tsp, sizeof(lts));
2384 linux_ppoll(struct thread *td, struct linux_ppoll_args *args)
2386 struct timespec ts0, ts1;
2387 struct l_timespec lts;
2388 struct timespec uts, *tsp;
2394 if (args->sset != NULL) {
2395 if (args->ssize != sizeof(l_ss))
2397 error = copyin(args->sset, &l_ss, sizeof(l_ss));
2400 linux_to_bsd_sigset(&l_ss, &ss);
2404 if (args->tsp != NULL) {
2405 error = copyin(args->tsp, <s, sizeof(lts));
2408 error = linux_to_native_timespec(&uts, <s);
2417 error = kern_poll(td, args->fds, args->nfds, tsp, ssp);
2419 if (error == 0 && args->tsp != NULL) {
2420 if (td->td_retval[0]) {
2422 timespecsub(&ts1, &ts0, &ts1);
2423 timespecsub(&uts, &ts1, &uts);
2425 timespecclear(&uts);
2427 timespecclear(&uts);
2429 error = native_to_linux_timespec(<s, &uts);
2431 error = copyout(<s, args->tsp, sizeof(lts));
2437 #if defined(DEBUG) || defined(KTR)
2438 /* XXX: can be removed when every ldebug(...) and KTR stuff are removed. */
2440 #ifdef COMPAT_LINUX32
2441 #define L_MAXSYSCALL LINUX32_SYS_MAXSYSCALL
2443 #define L_MAXSYSCALL LINUX_SYS_MAXSYSCALL
2446 u_char linux_debug_map[howmany(L_MAXSYSCALL, sizeof(u_char))];
2449 linux_debug(int syscall, int toggle, int global)
2453 char c = toggle ? 0 : 0xff;
2455 memset(linux_debug_map, c, sizeof(linux_debug_map));
2458 if (syscall < 0 || syscall >= L_MAXSYSCALL)
2461 clrbit(linux_debug_map, syscall);
2463 setbit(linux_debug_map, syscall);
2469 * Usage: sysctl linux.debug=<syscall_nr>.<0/1>
2471 * E.g.: sysctl linux.debug=21.0
2473 * As a special case, syscall "all" will apply to all syscalls globally.
2475 #define LINUX_MAX_DEBUGSTR 16
2477 linux_sysctl_debug(SYSCTL_HANDLER_ARGS)
2479 char value[LINUX_MAX_DEBUGSTR], *p;
2480 int error, sysc, toggle;
2484 error = sysctl_handle_string(oidp, value, LINUX_MAX_DEBUGSTR, req);
2485 if (error || req->newptr == NULL)
2487 for (p = value; *p != '\0' && *p != '.'; p++);
2491 sysc = strtol(value, NULL, 0);
2492 toggle = strtol(p, NULL, 0);
2493 if (strcmp(value, "all") == 0)
2495 error = linux_debug(sysc, toggle, global);
2499 #endif /* DEBUG || KTR */
2502 linux_sched_rr_get_interval(struct thread *td,
2503 struct linux_sched_rr_get_interval_args *uap)
2506 struct l_timespec lts;
2511 * According to man in case the invalid pid specified
2512 * EINVAL should be returned.
2517 tdt = linux_tdfind(td, uap->pid, -1);
2521 error = kern_sched_rr_get_interval_td(td, tdt, &ts);
2522 PROC_UNLOCK(tdt->td_proc);
2525 error = native_to_linux_timespec(<s, &ts);
2528 return (copyout(<s, uap->interval, sizeof(lts)));
2532 * In case when the Linux thread is the initial thread in
2533 * the thread group thread id is equal to the process id.
2534 * Glibc depends on this magic (assert in pthread_getattr_np.c).
2537 linux_tdfind(struct thread *td, lwpid_t tid, pid_t pid)
2539 struct linux_emuldata *em;
2544 if (tid == 0 || tid == td->td_tid) {
2546 PROC_LOCK(tdt->td_proc);
2547 } else if (tid > PID_MAX)
2548 tdt = tdfind(tid, pid);
2551 * Initial thread where the tid equal to the pid.
2555 if (SV_PROC_ABI(p) != SV_ABI_LINUX) {
2557 * p is not a Linuxulator process.
2562 FOREACH_THREAD_IN_PROC(p, tdt) {
2564 if (tid == em->em_tid)
2576 linux_to_bsd_waitopts(int options, int *bsdopts)
2579 if (options & LINUX_WNOHANG)
2580 *bsdopts |= WNOHANG;
2581 if (options & LINUX_WUNTRACED)
2582 *bsdopts |= WUNTRACED;
2583 if (options & LINUX_WEXITED)
2584 *bsdopts |= WEXITED;
2585 if (options & LINUX_WCONTINUED)
2586 *bsdopts |= WCONTINUED;
2587 if (options & LINUX_WNOWAIT)
2588 *bsdopts |= WNOWAIT;
2590 if (options & __WCLONE)
2591 *bsdopts |= WLINUXCLONE;
2595 linux_getrandom(struct thread *td, struct linux_getrandom_args *args)
2601 if (args->flags & ~(LINUX_GRND_NONBLOCK|LINUX_GRND_RANDOM))
2603 if (args->count > INT_MAX)
2604 args->count = INT_MAX;
2606 iov.iov_base = args->buf;
2607 iov.iov_len = args->count;
2611 uio.uio_resid = iov.iov_len;
2612 uio.uio_segflg = UIO_USERSPACE;
2613 uio.uio_rw = UIO_READ;
2616 error = read_random_uio(&uio, args->flags & LINUX_GRND_NONBLOCK);
2618 td->td_retval[0] = args->count - uio.uio_resid;
2623 linux_mincore(struct thread *td, struct linux_mincore_args *args)
2626 /* Needs to be page-aligned */
2627 if (args->start & PAGE_MASK)
2629 return (kern_mincore(td, args->start, args->len, args->vec));
2632 #define SYSLOG_TAG "<6>"
2635 linux_syslog(struct thread *td, struct linux_syslog_args *args)
2637 char buf[128], *src, *dst;
2641 if (args->type != LINUX_SYSLOG_ACTION_READ_ALL) {
2642 linux_msg(td, "syslog unsupported type 0x%x", args->type);
2646 if (args->len < 6) {
2647 td->td_retval[0] = 0;
2651 error = priv_check(td, PRIV_MSGBUF);
2655 mtx_lock(&msgbuf_lock);
2656 msgbuf_peekbytes(msgbufp, NULL, 0, &seq);
2657 mtx_unlock(&msgbuf_lock);
2660 error = copyout(&SYSLOG_TAG, dst, sizeof(SYSLOG_TAG));
2661 /* The -1 is to skip the trailing '\0'. */
2662 dst += sizeof(SYSLOG_TAG) - 1;
2664 while (error == 0) {
2665 mtx_lock(&msgbuf_lock);
2666 buflen = msgbuf_peekbytes(msgbufp, buf, sizeof(buf), &seq);
2667 mtx_unlock(&msgbuf_lock);
2672 for (src = buf; src < buf + buflen && error == 0; src++) {
2676 if (dst >= args->buf + args->len)
2679 error = copyout(src, dst, 1);
2682 if (*src == '\n' && *(src + 1) != '<' &&
2683 dst + sizeof(SYSLOG_TAG) < args->buf + args->len) {
2684 error = copyout(&SYSLOG_TAG,
2685 dst, sizeof(SYSLOG_TAG));
2686 dst += sizeof(SYSLOG_TAG) - 1;
2691 td->td_retval[0] = dst - args->buf;