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/reboot.h>
55 #include <sys/racct.h>
56 #include <sys/random.h>
57 #include <sys/resourcevar.h>
58 #include <sys/sched.h>
60 #include <sys/signalvar.h>
62 #include <sys/syscallsubr.h>
63 #include <sys/sysctl.h>
64 #include <sys/sysproto.h>
65 #include <sys/systm.h>
67 #include <sys/vmmeter.h>
68 #include <sys/vnode.h>
70 #include <sys/cpuset.h>
73 #include <security/mac/mac_framework.h>
77 #include <vm/vm_kern.h>
78 #include <vm/vm_map.h>
79 #include <vm/vm_extern.h>
80 #include <vm/vm_object.h>
81 #include <vm/swap_pager.h>
84 #include <machine/../linux32/linux.h>
85 #include <machine/../linux32/linux32_proto.h>
87 #include <machine/../linux/linux.h>
88 #include <machine/../linux/linux_proto.h>
91 #include <compat/linux/linux_dtrace.h>
92 #include <compat/linux/linux_file.h>
93 #include <compat/linux/linux_mib.h>
94 #include <compat/linux/linux_signal.h>
95 #include <compat/linux/linux_timer.h>
96 #include <compat/linux/linux_util.h>
97 #include <compat/linux/linux_sysproto.h>
98 #include <compat/linux/linux_emul.h>
99 #include <compat/linux/linux_misc.h>
102 * Special DTrace provider for the linuxulator.
104 * In this file we define the provider for the entire linuxulator. All
105 * modules (= files of the linuxulator) use it.
107 * We define a different name depending on the emulated bitsize, see
108 * ../../<ARCH>/linux{,32}/linux.h, e.g.:
109 * native bitsize = linuxulator
110 * amd64, 32bit emulation = linuxulator32
112 LIN_SDT_PROVIDER_DEFINE(LINUX_DTRACE);
114 int stclohz; /* Statistics clock frequency */
116 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
117 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
118 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
119 RLIMIT_MEMLOCK, RLIMIT_AS
123 l_long uptime; /* Seconds since boot */
124 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */
125 #define LINUX_SYSINFO_LOADS_SCALE 65536
126 l_ulong totalram; /* Total usable main memory size */
127 l_ulong freeram; /* Available memory size */
128 l_ulong sharedram; /* Amount of shared memory */
129 l_ulong bufferram; /* Memory used by buffers */
130 l_ulong totalswap; /* Total swap space size */
131 l_ulong freeswap; /* swap space still available */
132 l_ushort procs; /* Number of current processes */
137 char _f[20-2*sizeof(l_long)-sizeof(l_int)]; /* padding */
140 struct l_pselect6arg {
145 static int linux_utimensat_nsec_valid(l_long);
149 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args)
151 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 = sysinfo.totalram - vm_wire_count() * PAGE_SIZE;
170 sysinfo.sharedram = 0;
171 mtx_lock(&vm_object_list_mtx);
172 TAILQ_FOREACH(object, &vm_object_list, object_list)
173 if (object->shadow_count > 1)
174 sysinfo.sharedram += object->resident_page_count;
175 mtx_unlock(&vm_object_list_mtx);
177 sysinfo.sharedram *= PAGE_SIZE;
178 sysinfo.bufferram = 0;
180 swap_pager_status(&i, &j);
181 sysinfo.totalswap = i * PAGE_SIZE;
182 sysinfo.freeswap = (i - j) * PAGE_SIZE;
184 sysinfo.procs = nprocs;
186 /* The following are only present in newer Linux kernels. */
187 sysinfo.totalbig = 0;
189 sysinfo.mem_unit = 1;
191 return (copyout(&sysinfo, args->info, sizeof(sysinfo)));
194 #ifdef LINUX_LEGACY_SYSCALLS
196 linux_alarm(struct thread *td, struct linux_alarm_args *args)
198 struct itimerval it, old_it;
204 printf(ARGS(alarm, "%u"), args->secs);
208 * Linux alarm() is always successful. Limit secs to INT32_MAX / 2
209 * to match kern_setitimer()'s limit to avoid error from it.
211 * XXX. Linux limit secs to INT_MAX on 32 and does not limit on 64-bit
214 if (secs > INT32_MAX / 2)
215 secs = INT32_MAX / 2;
217 it.it_value.tv_sec = secs;
218 it.it_value.tv_usec = 0;
219 timevalclear(&it.it_interval);
220 error = kern_setitimer(td, ITIMER_REAL, &it, &old_it);
221 KASSERT(error == 0, ("kern_setitimer returns %d", error));
223 if ((old_it.it_value.tv_sec == 0 && old_it.it_value.tv_usec > 0) ||
224 old_it.it_value.tv_usec >= 500000)
225 old_it.it_value.tv_sec++;
226 td->td_retval[0] = old_it.it_value.tv_sec;
232 linux_brk(struct thread *td, struct linux_brk_args *args)
234 struct vmspace *vm = td->td_proc->p_vmspace;
239 printf(ARGS(brk, "%p"), (void *)(uintptr_t)args->dsend);
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)
262 unsigned long file_offset;
263 unsigned long bss_size;
266 int error, locked, writecount;
268 LCONVPATHEXIST(td, args->library, &library);
272 printf(ARGS(uselib, "%s"), library);
279 NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | AUDITVNODE1,
280 UIO_SYSSPACE, library, td);
287 NDFREE(&ni, NDF_ONLY_PNBUF);
290 * From here on down, we have a locked vnode that must be unlocked.
291 * XXX: The code below largely duplicates exec_check_permissions().
296 error = VOP_GET_WRITECOUNT(vp, &writecount);
299 if (writecount != 0) {
305 error = VOP_GETATTR(vp, &attr, td->td_ucred);
309 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
310 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
311 /* EACCESS is what exec(2) returns. */
317 if (attr.va_size == 0) {
322 /* Can we access it? */
323 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
328 * XXX: This should use vn_open() so that it is properly authorized,
329 * and to reduce code redundancy all over the place here.
330 * XXX: Not really, it duplicates far more of exec_check_permissions()
334 error = mac_vnode_check_open(td->td_ucred, vp, VREAD);
338 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.
403 * XXX: Note that if any of the VM operations fail below we don't
409 * Lock no longer needed
415 * Check if file_offset page aligned. Currently we cannot handle
416 * misalinged file offsets, and so we read in the entire image
419 if (file_offset & PAGE_MASK) {
421 printf("uselib: Non page aligned binary %lu\n", file_offset);
423 /* Map text+data read/write/execute */
425 /* a_entry is the load address and is page aligned */
426 vmaddr = trunc_page(a_out->a_entry);
428 /* get anon user mapping, read+write+execute */
429 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
430 &vmaddr, a_out->a_text + a_out->a_data, 0, VMFS_NO_SPACE,
431 VM_PROT_ALL, VM_PROT_ALL, 0);
435 error = vn_rdwr(UIO_READ, vp, (void *)vmaddr, file_offset,
436 a_out->a_text + a_out->a_data, UIO_USERSPACE, 0,
437 td->td_ucred, NOCRED, &aresid, td);
446 printf("uselib: Page aligned binary %lu\n", file_offset);
449 * for QMAGIC, a_entry is 20 bytes beyond the load address
450 * to skip the executable header
452 vmaddr = trunc_page(a_out->a_entry);
455 * Map it all into the process's space as a single
456 * copy-on-write "data" segment.
458 error = vm_mmap(&td->td_proc->p_vmspace->vm_map, &vmaddr,
459 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
460 MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset);
465 printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long *)vmaddr)[0],
466 ((long *)vmaddr)[1]);
469 /* Calculate BSS start address */
470 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
473 /* allocate some 'anon' space */
474 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
475 &vmaddr, bss_size, 0, VMFS_NO_SPACE, VM_PROT_ALL,
482 /* Unlock vnode if needed */
486 /* Release the temporary mapping. */
488 kmap_free_wakeup(exec_map, (vm_offset_t)a_out, PAGE_SIZE);
493 #endif /* __i386__ */
495 #ifdef LINUX_LEGACY_SYSCALLS
497 linux_select(struct thread *td, struct linux_select_args *args)
500 struct timeval tv0, tv1, utv, *tvp;
505 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
506 (void *)args->readfds, (void *)args->writefds,
507 (void *)args->exceptfds, (void *)args->timeout);
511 * Store current time for computation of the amount of
515 if ((error = copyin(args->timeout, <v, sizeof(ltv))))
517 utv.tv_sec = ltv.tv_sec;
518 utv.tv_usec = ltv.tv_usec;
521 printf(LMSG("incoming timeout (%jd/%ld)"),
522 (intmax_t)utv.tv_sec, utv.tv_usec);
525 if (itimerfix(&utv)) {
527 * The timeval was invalid. Convert it to something
528 * valid that will act as it does under Linux.
530 utv.tv_sec += utv.tv_usec / 1000000;
531 utv.tv_usec %= 1000000;
532 if (utv.tv_usec < 0) {
534 utv.tv_usec += 1000000;
544 error = kern_select(td, args->nfds, args->readfds, args->writefds,
545 args->exceptfds, tvp, LINUX_NFDBITS);
549 printf(LMSG("real select returns %d"), error);
555 if (td->td_retval[0]) {
557 * Compute how much time was left of the timeout,
558 * by subtracting the current time and the time
559 * before we started the call, and subtracting
560 * that result from the user-supplied value.
563 timevalsub(&tv1, &tv0);
564 timevalsub(&utv, &tv1);
571 printf(LMSG("outgoing timeout (%jd/%ld)"),
572 (intmax_t)utv.tv_sec, utv.tv_usec);
574 ltv.tv_sec = utv.tv_sec;
575 ltv.tv_usec = utv.tv_usec;
576 if ((error = copyout(<v, args->timeout, sizeof(ltv))))
583 printf(LMSG("select_out -> %d"), error);
590 linux_mremap(struct thread *td, struct linux_mremap_args *args)
598 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
599 (void *)(uintptr_t)args->addr,
600 (unsigned long)args->old_len,
601 (unsigned long)args->new_len,
602 (unsigned long)args->flags);
605 if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) {
606 td->td_retval[0] = 0;
611 * Check for the page alignment.
612 * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK.
614 if (args->addr & PAGE_MASK) {
615 td->td_retval[0] = 0;
619 args->new_len = round_page(args->new_len);
620 args->old_len = round_page(args->old_len);
622 if (args->new_len > args->old_len) {
623 td->td_retval[0] = 0;
627 if (args->new_len < args->old_len) {
628 addr = args->addr + args->new_len;
629 len = args->old_len - args->new_len;
630 error = kern_munmap(td, addr, len);
633 td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
637 #define LINUX_MS_ASYNC 0x0001
638 #define LINUX_MS_INVALIDATE 0x0002
639 #define LINUX_MS_SYNC 0x0004
642 linux_msync(struct thread *td, struct linux_msync_args *args)
645 return (kern_msync(td, args->addr, args->len,
646 args->fl & ~LINUX_MS_SYNC));
649 #ifdef LINUX_LEGACY_SYSCALLS
651 linux_time(struct thread *td, struct linux_time_args *args)
659 printf(ARGS(time, "*"));
664 if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
666 td->td_retval[0] = tm;
671 struct l_times_argv {
674 l_clock_t tms_cutime;
675 l_clock_t tms_cstime;
680 * Glibc versions prior to 2.2.1 always use hard-coded CLK_TCK value.
681 * Since 2.2.1 Glibc uses value exported from kernel via AT_CLKTCK
682 * auxiliary vector entry.
686 #define CONVOTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
687 #define CONVNTCK(r) (r.tv_sec * stclohz + r.tv_usec / (1000000 / stclohz))
689 #define CONVTCK(r) (linux_kernver(td) >= LINUX_KERNVER_2004000 ? \
690 CONVNTCK(r) : CONVOTCK(r))
693 linux_times(struct thread *td, struct linux_times_args *args)
695 struct timeval tv, utime, stime, cutime, cstime;
696 struct l_times_argv tms;
702 printf(ARGS(times, "*"));
705 if (args->buf != NULL) {
709 calcru(p, &utime, &stime);
711 calccru(p, &cutime, &cstime);
714 tms.tms_utime = CONVTCK(utime);
715 tms.tms_stime = CONVTCK(stime);
717 tms.tms_cutime = CONVTCK(cutime);
718 tms.tms_cstime = CONVTCK(cstime);
720 if ((error = copyout(&tms, args->buf, sizeof(tms))))
725 td->td_retval[0] = (int)CONVTCK(tv);
730 linux_newuname(struct thread *td, struct linux_newuname_args *args)
732 struct l_new_utsname utsname;
733 char osname[LINUX_MAX_UTSNAME];
734 char osrelease[LINUX_MAX_UTSNAME];
738 if (ldebug(newuname))
739 printf(ARGS(newuname, "*"));
742 linux_get_osname(td, osname);
743 linux_get_osrelease(td, osrelease);
745 bzero(&utsname, sizeof(utsname));
746 strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
747 getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
748 getcreddomainname(td->td_ucred, utsname.domainname, LINUX_MAX_UTSNAME);
749 strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
750 strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
751 for (p = utsname.version; *p != '\0'; ++p)
756 strlcpy(utsname.machine, linux_kplatform, LINUX_MAX_UTSNAME);
758 return (copyout(&utsname, args->buf, sizeof(utsname)));
766 #ifdef LINUX_LEGACY_SYSCALLS
768 linux_utime(struct thread *td, struct linux_utime_args *args)
770 struct timeval tv[2], *tvp;
771 struct l_utimbuf lut;
775 LCONVPATHEXIST(td, args->fname, &fname);
779 printf(ARGS(utime, "%s, *"), fname);
783 if ((error = copyin(args->times, &lut, sizeof lut))) {
787 tv[0].tv_sec = lut.l_actime;
789 tv[1].tv_sec = lut.l_modtime;
795 error = kern_utimesat(td, AT_FDCWD, fname, UIO_SYSSPACE, tvp,
802 #ifdef LINUX_LEGACY_SYSCALLS
804 linux_utimes(struct thread *td, struct linux_utimes_args *args)
807 struct timeval tv[2], *tvp = NULL;
811 LCONVPATHEXIST(td, args->fname, &fname);
815 printf(ARGS(utimes, "%s, *"), fname);
818 if (args->tptr != NULL) {
819 if ((error = copyin(args->tptr, ltv, sizeof ltv))) {
823 tv[0].tv_sec = ltv[0].tv_sec;
824 tv[0].tv_usec = ltv[0].tv_usec;
825 tv[1].tv_sec = ltv[1].tv_sec;
826 tv[1].tv_usec = ltv[1].tv_usec;
830 error = kern_utimesat(td, AT_FDCWD, fname, UIO_SYSSPACE,
838 linux_utimensat_nsec_valid(l_long nsec)
841 if (nsec == LINUX_UTIME_OMIT || nsec == LINUX_UTIME_NOW)
843 if (nsec >= 0 && nsec <= 999999999)
849 linux_utimensat(struct thread *td, struct linux_utimensat_args *args)
851 struct l_timespec l_times[2];
852 struct timespec times[2], *timesp = NULL;
854 int error, dfd, flags = 0;
856 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
859 if (ldebug(utimensat))
860 printf(ARGS(utimensat, "%d, *"), dfd);
863 if (args->flags & ~LINUX_AT_SYMLINK_NOFOLLOW)
866 if (args->times != NULL) {
867 error = copyin(args->times, l_times, sizeof(l_times));
871 if (linux_utimensat_nsec_valid(l_times[0].tv_nsec) != 0 ||
872 linux_utimensat_nsec_valid(l_times[1].tv_nsec) != 0)
875 times[0].tv_sec = l_times[0].tv_sec;
876 switch (l_times[0].tv_nsec)
878 case LINUX_UTIME_OMIT:
879 times[0].tv_nsec = UTIME_OMIT;
881 case LINUX_UTIME_NOW:
882 times[0].tv_nsec = UTIME_NOW;
885 times[0].tv_nsec = l_times[0].tv_nsec;
888 times[1].tv_sec = l_times[1].tv_sec;
889 switch (l_times[1].tv_nsec)
891 case LINUX_UTIME_OMIT:
892 times[1].tv_nsec = UTIME_OMIT;
894 case LINUX_UTIME_NOW:
895 times[1].tv_nsec = UTIME_NOW;
898 times[1].tv_nsec = l_times[1].tv_nsec;
903 /* This breaks POSIX, but is what the Linux kernel does
904 * _on purpose_ (documented in the man page for utimensat(2)),
905 * so we must follow that behaviour. */
906 if (times[0].tv_nsec == UTIME_OMIT &&
907 times[1].tv_nsec == UTIME_OMIT)
911 if (args->pathname != NULL)
912 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
913 else if (args->flags != 0)
916 if (args->flags & LINUX_AT_SYMLINK_NOFOLLOW)
917 flags |= AT_SYMLINK_NOFOLLOW;
920 error = kern_futimens(td, dfd, timesp, UIO_SYSSPACE);
922 error = kern_utimensat(td, dfd, path, UIO_SYSSPACE, timesp,
923 UIO_SYSSPACE, flags);
930 #ifdef LINUX_LEGACY_SYSCALLS
932 linux_futimesat(struct thread *td, struct linux_futimesat_args *args)
935 struct timeval tv[2], *tvp = NULL;
939 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
940 LCONVPATHEXIST_AT(td, args->filename, &fname, dfd);
943 if (ldebug(futimesat))
944 printf(ARGS(futimesat, "%s, *"), fname);
947 if (args->utimes != NULL) {
948 if ((error = copyin(args->utimes, ltv, sizeof ltv))) {
952 tv[0].tv_sec = ltv[0].tv_sec;
953 tv[0].tv_usec = ltv[0].tv_usec;
954 tv[1].tv_sec = ltv[1].tv_sec;
955 tv[1].tv_usec = ltv[1].tv_usec;
959 error = kern_utimesat(td, dfd, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
966 linux_common_wait(struct thread *td, int pid, int *status,
967 int options, struct rusage *ru)
971 error = kern_wait(td, pid, &tmpstat, options, ru);
977 if (WIFSIGNALED(tmpstat))
978 tmpstat = (tmpstat & 0xffffff80) |
979 bsd_to_linux_signal(WTERMSIG(tmpstat));
980 else if (WIFSTOPPED(tmpstat))
981 tmpstat = (tmpstat & 0xffff00ff) |
982 (bsd_to_linux_signal(WSTOPSIG(tmpstat)) << 8);
983 else if (WIFCONTINUED(tmpstat))
985 error = copyout(&tmpstat, status, sizeof(int));
991 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
993 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
995 struct linux_wait4_args wait4_args;
999 printf(ARGS(waitpid, "%d, %p, %d"),
1000 args->pid, (void *)args->status, args->options);
1003 wait4_args.pid = args->pid;
1004 wait4_args.status = args->status;
1005 wait4_args.options = args->options;
1006 wait4_args.rusage = NULL;
1008 return (linux_wait4(td, &wait4_args));
1010 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1013 linux_wait4(struct thread *td, struct linux_wait4_args *args)
1016 struct rusage ru, *rup;
1020 printf(ARGS(wait4, "%d, %p, %d, %p"),
1021 args->pid, (void *)args->status, args->options,
1022 (void *)args->rusage);
1024 if (args->options & ~(LINUX_WUNTRACED | LINUX_WNOHANG |
1025 LINUX_WCONTINUED | __WCLONE | __WNOTHREAD | __WALL))
1029 linux_to_bsd_waitopts(args->options, &options);
1031 if (args->rusage != NULL)
1035 error = linux_common_wait(td, args->pid, args->status, options, rup);
1038 if (args->rusage != NULL)
1039 error = linux_copyout_rusage(&ru, args->rusage);
1044 linux_waitid(struct thread *td, struct linux_waitid_args *args)
1046 int status, options, sig;
1047 struct __wrusage wru;
1055 linux_to_bsd_waitopts(args->options, &options);
1057 if (options & ~(WNOHANG | WNOWAIT | WEXITED | WUNTRACED | WCONTINUED))
1059 if (!(options & (WEXITED | WUNTRACED | WCONTINUED)))
1062 switch (args->idtype) {
1080 error = kern_wait6(td, idtype, args->id, &status, options,
1084 if (args->rusage != NULL) {
1085 error = linux_copyout_rusage(&wru.wru_children,
1090 if (args->info != NULL) {
1092 if (td->td_retval[0] == 0)
1093 bzero(&lsi, sizeof(lsi));
1095 sig = bsd_to_linux_signal(siginfo.si_signo);
1096 siginfo_to_lsiginfo(&siginfo, &lsi, sig);
1098 error = copyout(&lsi, args->info, sizeof(lsi));
1100 td->td_retval[0] = 0;
1105 #ifdef LINUX_LEGACY_SYSCALLS
1107 linux_mknod(struct thread *td, struct linux_mknod_args *args)
1112 LCONVPATHCREAT(td, args->path, &path);
1116 printf(ARGS(mknod, "%s, %d, %ju"), path, args->mode,
1117 (uintmax_t)args->dev);
1120 switch (args->mode & S_IFMT) {
1123 error = kern_mkfifoat(td, AT_FDCWD, path, UIO_SYSSPACE,
1129 error = kern_mknodat(td, AT_FDCWD, path, UIO_SYSSPACE,
1130 args->mode, args->dev);
1138 args->mode |= S_IFREG;
1141 error = kern_openat(td, AT_FDCWD, path, UIO_SYSSPACE,
1142 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1144 kern_close(td, td->td_retval[0]);
1157 linux_mknodat(struct thread *td, struct linux_mknodat_args *args)
1162 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1163 LCONVPATHCREAT_AT(td, args->filename, &path, dfd);
1166 if (ldebug(mknodat))
1167 printf(ARGS(mknodat, "%s, %d, %d"), path, args->mode, args->dev);
1170 switch (args->mode & S_IFMT) {
1173 error = kern_mkfifoat(td, dfd, path, UIO_SYSSPACE, args->mode);
1178 error = kern_mknodat(td, dfd, path, UIO_SYSSPACE, args->mode,
1187 args->mode |= S_IFREG;
1190 error = kern_openat(td, dfd, path, UIO_SYSSPACE,
1191 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1193 kern_close(td, td->td_retval[0]);
1205 * UGH! This is just about the dumbest idea I've ever heard!!
1208 linux_personality(struct thread *td, struct linux_personality_args *args)
1210 struct linux_pemuldata *pem;
1211 struct proc *p = td->td_proc;
1215 if (ldebug(personality))
1216 printf(ARGS(personality, "%u"), args->per);
1222 if (args->per != 0xffffffff)
1223 pem->persona = args->per;
1226 td->td_retval[0] = old;
1230 struct l_itimerval {
1231 l_timeval it_interval;
1235 #define B2L_ITIMERVAL(bip, lip) \
1236 (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec; \
1237 (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec; \
1238 (bip)->it_value.tv_sec = (lip)->it_value.tv_sec; \
1239 (bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
1242 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
1245 struct l_itimerval ls;
1246 struct itimerval aitv, oitv;
1249 if (ldebug(setitimer))
1250 printf(ARGS(setitimer, "%p, %p"),
1251 (void *)uap->itv, (void *)uap->oitv);
1254 if (uap->itv == NULL) {
1255 uap->itv = uap->oitv;
1256 return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
1259 error = copyin(uap->itv, &ls, sizeof(ls));
1262 B2L_ITIMERVAL(&aitv, &ls);
1264 if (ldebug(setitimer)) {
1265 printf("setitimer: value: sec: %jd, usec: %ld\n",
1266 (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec);
1267 printf("setitimer: interval: sec: %jd, usec: %ld\n",
1268 (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec);
1271 error = kern_setitimer(td, uap->which, &aitv, &oitv);
1272 if (error != 0 || uap->oitv == NULL)
1274 B2L_ITIMERVAL(&ls, &oitv);
1276 return (copyout(&ls, uap->oitv, sizeof(ls)));
1280 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
1283 struct l_itimerval ls;
1284 struct itimerval aitv;
1287 if (ldebug(getitimer))
1288 printf(ARGS(getitimer, "%p"), (void *)uap->itv);
1290 error = kern_getitimer(td, uap->which, &aitv);
1293 B2L_ITIMERVAL(&ls, &aitv);
1294 return (copyout(&ls, uap->itv, sizeof(ls)));
1297 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1299 linux_nice(struct thread *td, struct linux_nice_args *args)
1301 struct setpriority_args bsd_args;
1303 bsd_args.which = PRIO_PROCESS;
1304 bsd_args.who = 0; /* current process */
1305 bsd_args.prio = args->inc;
1306 return (sys_setpriority(td, &bsd_args));
1308 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1311 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
1313 struct ucred *newcred, *oldcred;
1314 l_gid_t *linux_gidset;
1319 ngrp = args->gidsetsize;
1320 if (ngrp < 0 || ngrp >= ngroups_max + 1)
1322 linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_LINUX, M_WAITOK);
1323 error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
1327 crextend(newcred, ngrp + 1);
1330 oldcred = p->p_ucred;
1331 crcopy(newcred, oldcred);
1334 * cr_groups[0] holds egid. Setting the whole set from
1335 * the supplied set will cause egid to be changed too.
1336 * Keep cr_groups[0] unchanged to prevent that.
1339 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS)) != 0) {
1346 newcred->cr_ngroups = ngrp + 1;
1348 bsd_gidset = newcred->cr_groups;
1351 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1355 newcred->cr_ngroups = 1;
1358 proc_set_cred(p, newcred);
1363 free(linux_gidset, M_LINUX);
1368 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1371 l_gid_t *linux_gidset;
1373 int bsd_gidsetsz, ngrp, error;
1375 cred = td->td_ucred;
1376 bsd_gidset = cred->cr_groups;
1377 bsd_gidsetsz = cred->cr_ngroups - 1;
1380 * cr_groups[0] holds egid. Returning the whole set
1381 * here will cause a duplicate. Exclude cr_groups[0]
1385 if ((ngrp = args->gidsetsize) == 0) {
1386 td->td_retval[0] = bsd_gidsetsz;
1390 if (ngrp < bsd_gidsetsz)
1394 linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset),
1396 while (ngrp < bsd_gidsetsz) {
1397 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1401 error = copyout(linux_gidset, args->grouplist, ngrp * sizeof(l_gid_t));
1402 free(linux_gidset, M_LINUX);
1406 td->td_retval[0] = ngrp;
1411 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1413 struct rlimit bsd_rlim;
1414 struct l_rlimit rlim;
1419 if (ldebug(setrlimit))
1420 printf(ARGS(setrlimit, "%d, %p"),
1421 args->resource, (void *)args->rlim);
1424 if (args->resource >= LINUX_RLIM_NLIMITS)
1427 which = linux_to_bsd_resource[args->resource];
1431 error = copyin(args->rlim, &rlim, sizeof(rlim));
1435 bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1436 bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1437 return (kern_setrlimit(td, which, &bsd_rlim));
1440 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1442 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1444 struct l_rlimit rlim;
1445 struct rlimit bsd_rlim;
1449 if (ldebug(old_getrlimit))
1450 printf(ARGS(old_getrlimit, "%d, %p"),
1451 args->resource, (void *)args->rlim);
1454 if (args->resource >= LINUX_RLIM_NLIMITS)
1457 which = linux_to_bsd_resource[args->resource];
1461 lim_rlimit(td, which, &bsd_rlim);
1463 #ifdef COMPAT_LINUX32
1464 rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1465 if (rlim.rlim_cur == UINT_MAX)
1466 rlim.rlim_cur = INT_MAX;
1467 rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1468 if (rlim.rlim_max == UINT_MAX)
1469 rlim.rlim_max = INT_MAX;
1471 rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1472 if (rlim.rlim_cur == ULONG_MAX)
1473 rlim.rlim_cur = LONG_MAX;
1474 rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1475 if (rlim.rlim_max == ULONG_MAX)
1476 rlim.rlim_max = LONG_MAX;
1478 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1480 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1483 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1485 struct l_rlimit rlim;
1486 struct rlimit bsd_rlim;
1490 if (ldebug(getrlimit))
1491 printf(ARGS(getrlimit, "%d, %p"),
1492 args->resource, (void *)args->rlim);
1495 if (args->resource >= LINUX_RLIM_NLIMITS)
1498 which = linux_to_bsd_resource[args->resource];
1502 lim_rlimit(td, which, &bsd_rlim);
1504 rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1505 rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1506 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1510 linux_sched_setscheduler(struct thread *td,
1511 struct linux_sched_setscheduler_args *args)
1513 struct sched_param sched_param;
1518 if (ldebug(sched_setscheduler))
1519 printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1520 args->pid, args->policy, (const void *)args->param);
1523 switch (args->policy) {
1524 case LINUX_SCHED_OTHER:
1525 policy = SCHED_OTHER;
1527 case LINUX_SCHED_FIFO:
1528 policy = SCHED_FIFO;
1530 case LINUX_SCHED_RR:
1537 error = copyin(args->param, &sched_param, sizeof(sched_param));
1541 tdt = linux_tdfind(td, args->pid, -1);
1545 error = kern_sched_setscheduler(td, tdt, policy, &sched_param);
1546 PROC_UNLOCK(tdt->td_proc);
1551 linux_sched_getscheduler(struct thread *td,
1552 struct linux_sched_getscheduler_args *args)
1558 if (ldebug(sched_getscheduler))
1559 printf(ARGS(sched_getscheduler, "%d"), args->pid);
1562 tdt = linux_tdfind(td, args->pid, -1);
1566 error = kern_sched_getscheduler(td, tdt, &policy);
1567 PROC_UNLOCK(tdt->td_proc);
1571 td->td_retval[0] = LINUX_SCHED_OTHER;
1574 td->td_retval[0] = LINUX_SCHED_FIFO;
1577 td->td_retval[0] = LINUX_SCHED_RR;
1584 linux_sched_get_priority_max(struct thread *td,
1585 struct linux_sched_get_priority_max_args *args)
1587 struct sched_get_priority_max_args bsd;
1590 if (ldebug(sched_get_priority_max))
1591 printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1594 switch (args->policy) {
1595 case LINUX_SCHED_OTHER:
1596 bsd.policy = SCHED_OTHER;
1598 case LINUX_SCHED_FIFO:
1599 bsd.policy = SCHED_FIFO;
1601 case LINUX_SCHED_RR:
1602 bsd.policy = SCHED_RR;
1607 return (sys_sched_get_priority_max(td, &bsd));
1611 linux_sched_get_priority_min(struct thread *td,
1612 struct linux_sched_get_priority_min_args *args)
1614 struct sched_get_priority_min_args bsd;
1617 if (ldebug(sched_get_priority_min))
1618 printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1621 switch (args->policy) {
1622 case LINUX_SCHED_OTHER:
1623 bsd.policy = SCHED_OTHER;
1625 case LINUX_SCHED_FIFO:
1626 bsd.policy = SCHED_FIFO;
1628 case LINUX_SCHED_RR:
1629 bsd.policy = SCHED_RR;
1634 return (sys_sched_get_priority_min(td, &bsd));
1637 #define REBOOT_CAD_ON 0x89abcdef
1638 #define REBOOT_CAD_OFF 0
1639 #define REBOOT_HALT 0xcdef0123
1640 #define REBOOT_RESTART 0x01234567
1641 #define REBOOT_RESTART2 0xA1B2C3D4
1642 #define REBOOT_POWEROFF 0x4321FEDC
1643 #define REBOOT_MAGIC1 0xfee1dead
1644 #define REBOOT_MAGIC2 0x28121969
1645 #define REBOOT_MAGIC2A 0x05121996
1646 #define REBOOT_MAGIC2B 0x16041998
1649 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1651 struct reboot_args bsd_args;
1655 printf(ARGS(reboot, "0x%x"), args->cmd);
1658 if (args->magic1 != REBOOT_MAGIC1)
1661 switch (args->magic2) {
1663 case REBOOT_MAGIC2A:
1664 case REBOOT_MAGIC2B:
1670 switch (args->cmd) {
1672 case REBOOT_CAD_OFF:
1673 return (priv_check(td, PRIV_REBOOT));
1675 bsd_args.opt = RB_HALT;
1677 case REBOOT_RESTART:
1678 case REBOOT_RESTART2:
1681 case REBOOT_POWEROFF:
1682 bsd_args.opt = RB_POWEROFF;
1687 return (sys_reboot(td, &bsd_args));
1692 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1693 * td->td_retval[1] when COMPAT_43 is defined. This clobbers registers that
1694 * are assumed to be preserved. The following lightweight syscalls fixes
1695 * this. See also linux_getgid16() and linux_getuid16() in linux_uid16.c
1697 * linux_getpid() - MP SAFE
1698 * linux_getgid() - MP SAFE
1699 * linux_getuid() - MP SAFE
1703 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1708 printf(ARGS(getpid, ""));
1710 td->td_retval[0] = td->td_proc->p_pid;
1716 linux_gettid(struct thread *td, struct linux_gettid_args *args)
1718 struct linux_emuldata *em;
1722 printf(ARGS(gettid, ""));
1726 KASSERT(em != NULL, ("gettid: emuldata not found.\n"));
1728 td->td_retval[0] = em->em_tid;
1735 linux_getppid(struct thread *td, struct linux_getppid_args *args)
1739 if (ldebug(getppid))
1740 printf(ARGS(getppid, ""));
1743 td->td_retval[0] = kern_getppid(td);
1748 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1753 printf(ARGS(getgid, ""));
1756 td->td_retval[0] = td->td_ucred->cr_rgid;
1761 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1766 printf(ARGS(getuid, ""));
1769 td->td_retval[0] = td->td_ucred->cr_ruid;
1775 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1777 struct getsid_args bsd;
1781 printf(ARGS(getsid, "%i"), args->pid);
1784 bsd.pid = args->pid;
1785 return (sys_getsid(td, &bsd));
1789 linux_nosys(struct thread *td, struct nosys_args *ignore)
1796 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1798 struct getpriority_args bsd_args;
1802 if (ldebug(getpriority))
1803 printf(ARGS(getpriority, "%i, %i"), args->which, args->who);
1806 bsd_args.which = args->which;
1807 bsd_args.who = args->who;
1808 error = sys_getpriority(td, &bsd_args);
1809 td->td_retval[0] = 20 - td->td_retval[0];
1814 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1819 if (ldebug(sethostname))
1820 printf(ARGS(sethostname, "*, %i"), args->len);
1824 name[1] = KERN_HOSTNAME;
1825 return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1830 linux_setdomainname(struct thread *td, struct linux_setdomainname_args *args)
1835 if (ldebug(setdomainname))
1836 printf(ARGS(setdomainname, "*, %i"), args->len);
1840 name[1] = KERN_NISDOMAINNAME;
1841 return (userland_sysctl(td, name, 2, 0, 0, 0, args->name,
1846 linux_exit_group(struct thread *td, struct linux_exit_group_args *args)
1850 if (ldebug(exit_group))
1851 printf(ARGS(exit_group, "%i"), args->error_code);
1854 LINUX_CTR2(exit_group, "thread(%d) (%d)", td->td_tid,
1858 * XXX: we should send a signal to the parent if
1859 * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?)
1860 * as it doesnt occur often.
1862 exit1(td, args->error_code, 0);
1866 #define _LINUX_CAPABILITY_VERSION_1 0x19980330
1867 #define _LINUX_CAPABILITY_VERSION_2 0x20071026
1868 #define _LINUX_CAPABILITY_VERSION_3 0x20080522
1870 struct l_user_cap_header {
1875 struct l_user_cap_data {
1882 linux_capget(struct thread *td, struct linux_capget_args *uap)
1884 struct l_user_cap_header luch;
1885 struct l_user_cap_data lucd[2];
1888 if (uap->hdrp == NULL)
1891 error = copyin(uap->hdrp, &luch, sizeof(luch));
1895 switch (luch.version) {
1896 case _LINUX_CAPABILITY_VERSION_1:
1899 case _LINUX_CAPABILITY_VERSION_2:
1900 case _LINUX_CAPABILITY_VERSION_3:
1906 printf(LMSG("invalid capget capability version 0x%x"),
1909 luch.version = _LINUX_CAPABILITY_VERSION_1;
1910 error = copyout(&luch, uap->hdrp, sizeof(luch));
1921 * The current implementation doesn't support setting
1922 * a capability (it's essentially a stub) so indicate
1923 * that no capabilities are currently set or available
1926 memset(&lucd, 0, u32s * sizeof(lucd[0]));
1927 error = copyout(&lucd, uap->datap, u32s * sizeof(lucd[0]));
1934 linux_capset(struct thread *td, struct linux_capset_args *uap)
1936 struct l_user_cap_header luch;
1937 struct l_user_cap_data lucd[2];
1940 if (uap->hdrp == NULL || uap->datap == NULL)
1943 error = copyin(uap->hdrp, &luch, sizeof(luch));
1947 switch (luch.version) {
1948 case _LINUX_CAPABILITY_VERSION_1:
1951 case _LINUX_CAPABILITY_VERSION_2:
1952 case _LINUX_CAPABILITY_VERSION_3:
1958 printf(LMSG("invalid capset capability version 0x%x"),
1961 luch.version = _LINUX_CAPABILITY_VERSION_1;
1962 error = copyout(&luch, uap->hdrp, sizeof(luch));
1971 error = copyin(uap->datap, &lucd, u32s * sizeof(lucd[0]));
1975 /* We currently don't support setting any capabilities. */
1976 for (i = 0; i < u32s; i++) {
1977 if (lucd[i].effective || lucd[i].permitted ||
1978 lucd[i].inheritable) {
1980 "capset[%d] effective=0x%x, permitted=0x%x, "
1981 "inheritable=0x%x is not implemented", i,
1982 (int)lucd[i].effective, (int)lucd[i].permitted,
1983 (int)lucd[i].inheritable);
1992 linux_prctl(struct thread *td, struct linux_prctl_args *args)
1994 int error = 0, max_size;
1995 struct proc *p = td->td_proc;
1996 char comm[LINUX_MAX_COMM_LEN];
1997 struct linux_emuldata *em;
2002 printf(ARGS(prctl, "%d, %ju, %ju, %ju, %ju"), args->option,
2003 (uintmax_t)args->arg2, (uintmax_t)args->arg3,
2004 (uintmax_t)args->arg4, (uintmax_t)args->arg5);
2007 switch (args->option) {
2008 case LINUX_PR_SET_PDEATHSIG:
2009 if (!LINUX_SIG_VALID(args->arg2))
2012 KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
2013 em->pdeath_signal = args->arg2;
2015 case LINUX_PR_GET_PDEATHSIG:
2017 KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
2018 pdeath_signal = em->pdeath_signal;
2019 error = copyout(&pdeath_signal,
2020 (void *)(register_t)args->arg2,
2021 sizeof(pdeath_signal));
2023 case LINUX_PR_GET_KEEPCAPS:
2025 * Indicate that we always clear the effective and
2026 * permitted capability sets when the user id becomes
2027 * non-zero (actually the capability sets are simply
2028 * always zero in the current implementation).
2030 td->td_retval[0] = 0;
2032 case LINUX_PR_SET_KEEPCAPS:
2034 * Ignore requests to keep the effective and permitted
2035 * capability sets when the user id becomes non-zero.
2038 case LINUX_PR_SET_NAME:
2040 * To be on the safe side we need to make sure to not
2041 * overflow the size a Linux program expects. We already
2042 * do this here in the copyin, so that we don't need to
2045 max_size = MIN(sizeof(comm), sizeof(p->p_comm));
2046 error = copyinstr((void *)(register_t)args->arg2, comm,
2049 /* Linux silently truncates the name if it is too long. */
2050 if (error == ENAMETOOLONG) {
2052 * XXX: copyinstr() isn't documented to populate the
2053 * array completely, so do a copyin() to be on the
2054 * safe side. This should be changed in case
2055 * copyinstr() is changed to guarantee this.
2057 error = copyin((void *)(register_t)args->arg2, comm,
2059 comm[max_size - 1] = '\0';
2065 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
2068 case LINUX_PR_GET_NAME:
2070 strlcpy(comm, p->p_comm, sizeof(comm));
2072 error = copyout(comm, (void *)(register_t)args->arg2,
2084 linux_sched_setparam(struct thread *td,
2085 struct linux_sched_setparam_args *uap)
2087 struct sched_param sched_param;
2092 if (ldebug(sched_setparam))
2093 printf(ARGS(sched_setparam, "%d, *"), uap->pid);
2096 error = copyin(uap->param, &sched_param, sizeof(sched_param));
2100 tdt = linux_tdfind(td, uap->pid, -1);
2104 error = kern_sched_setparam(td, tdt, &sched_param);
2105 PROC_UNLOCK(tdt->td_proc);
2110 linux_sched_getparam(struct thread *td,
2111 struct linux_sched_getparam_args *uap)
2113 struct sched_param sched_param;
2118 if (ldebug(sched_getparam))
2119 printf(ARGS(sched_getparam, "%d, *"), uap->pid);
2122 tdt = linux_tdfind(td, uap->pid, -1);
2126 error = kern_sched_getparam(td, tdt, &sched_param);
2127 PROC_UNLOCK(tdt->td_proc);
2129 error = copyout(&sched_param, uap->param,
2130 sizeof(sched_param));
2135 * Get affinity of a process.
2138 linux_sched_getaffinity(struct thread *td,
2139 struct linux_sched_getaffinity_args *args)
2145 if (ldebug(sched_getaffinity))
2146 printf(ARGS(sched_getaffinity, "%d, %d, *"), args->pid,
2149 if (args->len < sizeof(cpuset_t))
2152 tdt = linux_tdfind(td, args->pid, -1);
2156 PROC_UNLOCK(tdt->td_proc);
2158 error = kern_cpuset_getaffinity(td, CPU_LEVEL_WHICH, CPU_WHICH_TID,
2159 tdt->td_tid, sizeof(cpuset_t), (cpuset_t *)args->user_mask_ptr);
2161 td->td_retval[0] = sizeof(cpuset_t);
2167 * Set affinity of a process.
2170 linux_sched_setaffinity(struct thread *td,
2171 struct linux_sched_setaffinity_args *args)
2176 if (ldebug(sched_setaffinity))
2177 printf(ARGS(sched_setaffinity, "%d, %d, *"), args->pid,
2180 if (args->len < sizeof(cpuset_t))
2183 tdt = linux_tdfind(td, args->pid, -1);
2187 PROC_UNLOCK(tdt->td_proc);
2189 return (kern_cpuset_setaffinity(td, CPU_LEVEL_WHICH, CPU_WHICH_TID,
2190 tdt->td_tid, sizeof(cpuset_t), (cpuset_t *) args->user_mask_ptr));
2193 struct linux_rlimit64 {
2199 linux_prlimit64(struct thread *td, struct linux_prlimit64_args *args)
2201 struct rlimit rlim, nrlim;
2202 struct linux_rlimit64 lrlim;
2209 if (ldebug(prlimit64))
2210 printf(ARGS(prlimit64, "%d, %d, %p, %p"), args->pid,
2211 args->resource, (void *)args->new, (void *)args->old);
2214 if (args->resource >= LINUX_RLIM_NLIMITS)
2217 which = linux_to_bsd_resource[args->resource];
2221 if (args->new != NULL) {
2223 * Note. Unlike FreeBSD where rlim is signed 64-bit Linux
2224 * rlim is unsigned 64-bit. FreeBSD treats negative limits
2225 * as INFINITY so we do not need a conversion even.
2227 error = copyin(args->new, &nrlim, sizeof(nrlim));
2232 flags = PGET_HOLD | PGET_NOTWEXIT;
2233 if (args->new != NULL)
2234 flags |= PGET_CANDEBUG;
2236 flags |= PGET_CANSEE;
2237 error = pget(args->pid, flags, &p);
2241 if (args->old != NULL) {
2243 lim_rlimit_proc(p, which, &rlim);
2245 if (rlim.rlim_cur == RLIM_INFINITY)
2246 lrlim.rlim_cur = LINUX_RLIM_INFINITY;
2248 lrlim.rlim_cur = rlim.rlim_cur;
2249 if (rlim.rlim_max == RLIM_INFINITY)
2250 lrlim.rlim_max = LINUX_RLIM_INFINITY;
2252 lrlim.rlim_max = rlim.rlim_max;
2253 error = copyout(&lrlim, args->old, sizeof(lrlim));
2258 if (args->new != NULL)
2259 error = kern_proc_setrlimit(td, p, which, &nrlim);
2267 linux_pselect6(struct thread *td, struct linux_pselect6_args *args)
2269 struct timeval utv, tv0, tv1, *tvp;
2270 struct l_pselect6arg lpse6;
2271 struct l_timespec lts;
2272 struct timespec uts;
2279 if (args->sig != NULL) {
2280 error = copyin(args->sig, &lpse6, sizeof(lpse6));
2283 if (lpse6.ss_len != sizeof(l_ss))
2285 if (lpse6.ss != 0) {
2286 error = copyin(PTRIN(lpse6.ss), &l_ss,
2290 linux_to_bsd_sigset(&l_ss, &ss);
2296 * Currently glibc changes nanosecond number to microsecond.
2297 * This mean losing precision but for now it is hardly seen.
2299 if (args->tsp != NULL) {
2300 error = copyin(args->tsp, <s, sizeof(lts));
2303 error = linux_to_native_timespec(&uts, <s);
2307 TIMESPEC_TO_TIMEVAL(&utv, &uts);
2308 if (itimerfix(&utv))
2316 error = kern_pselect(td, args->nfds, args->readfds, args->writefds,
2317 args->exceptfds, tvp, ssp, LINUX_NFDBITS);
2319 if (error == 0 && args->tsp != NULL) {
2320 if (td->td_retval[0] != 0) {
2322 * Compute how much time was left of the timeout,
2323 * by subtracting the current time and the time
2324 * before we started the call, and subtracting
2325 * that result from the user-supplied value.
2329 timevalsub(&tv1, &tv0);
2330 timevalsub(&utv, &tv1);
2336 TIMEVAL_TO_TIMESPEC(&utv, &uts);
2338 error = native_to_linux_timespec(<s, &uts);
2340 error = copyout(<s, args->tsp, sizeof(lts));
2347 linux_ppoll(struct thread *td, struct linux_ppoll_args *args)
2349 struct timespec ts0, ts1;
2350 struct l_timespec lts;
2351 struct timespec uts, *tsp;
2357 if (args->sset != NULL) {
2358 if (args->ssize != sizeof(l_ss))
2360 error = copyin(args->sset, &l_ss, sizeof(l_ss));
2363 linux_to_bsd_sigset(&l_ss, &ss);
2367 if (args->tsp != NULL) {
2368 error = copyin(args->tsp, <s, sizeof(lts));
2371 error = linux_to_native_timespec(&uts, <s);
2380 error = kern_poll(td, args->fds, args->nfds, tsp, ssp);
2382 if (error == 0 && args->tsp != NULL) {
2383 if (td->td_retval[0]) {
2385 timespecsub(&ts1, &ts0, &ts1);
2386 timespecsub(&uts, &ts1, &uts);
2388 timespecclear(&uts);
2390 timespecclear(&uts);
2392 error = native_to_linux_timespec(<s, &uts);
2394 error = copyout(<s, args->tsp, sizeof(lts));
2400 #if defined(DEBUG) || defined(KTR)
2401 /* XXX: can be removed when every ldebug(...) and KTR stuff are removed. */
2403 #ifdef COMPAT_LINUX32
2404 #define L_MAXSYSCALL LINUX32_SYS_MAXSYSCALL
2406 #define L_MAXSYSCALL LINUX_SYS_MAXSYSCALL
2409 u_char linux_debug_map[howmany(L_MAXSYSCALL, sizeof(u_char))];
2412 linux_debug(int syscall, int toggle, int global)
2416 char c = toggle ? 0 : 0xff;
2418 memset(linux_debug_map, c, sizeof(linux_debug_map));
2421 if (syscall < 0 || syscall >= L_MAXSYSCALL)
2424 clrbit(linux_debug_map, syscall);
2426 setbit(linux_debug_map, syscall);
2432 * Usage: sysctl linux.debug=<syscall_nr>.<0/1>
2434 * E.g.: sysctl linux.debug=21.0
2436 * As a special case, syscall "all" will apply to all syscalls globally.
2438 #define LINUX_MAX_DEBUGSTR 16
2440 linux_sysctl_debug(SYSCTL_HANDLER_ARGS)
2442 char value[LINUX_MAX_DEBUGSTR], *p;
2443 int error, sysc, toggle;
2447 error = sysctl_handle_string(oidp, value, LINUX_MAX_DEBUGSTR, req);
2448 if (error || req->newptr == NULL)
2450 for (p = value; *p != '\0' && *p != '.'; p++);
2454 sysc = strtol(value, NULL, 0);
2455 toggle = strtol(p, NULL, 0);
2456 if (strcmp(value, "all") == 0)
2458 error = linux_debug(sysc, toggle, global);
2462 #endif /* DEBUG || KTR */
2465 linux_sched_rr_get_interval(struct thread *td,
2466 struct linux_sched_rr_get_interval_args *uap)
2469 struct l_timespec lts;
2474 * According to man in case the invalid pid specified
2475 * EINVAL should be returned.
2480 tdt = linux_tdfind(td, uap->pid, -1);
2484 error = kern_sched_rr_get_interval_td(td, tdt, &ts);
2485 PROC_UNLOCK(tdt->td_proc);
2488 error = native_to_linux_timespec(<s, &ts);
2491 return (copyout(<s, uap->interval, sizeof(lts)));
2495 * In case when the Linux thread is the initial thread in
2496 * the thread group thread id is equal to the process id.
2497 * Glibc depends on this magic (assert in pthread_getattr_np.c).
2500 linux_tdfind(struct thread *td, lwpid_t tid, pid_t pid)
2502 struct linux_emuldata *em;
2507 if (tid == 0 || tid == td->td_tid) {
2509 PROC_LOCK(tdt->td_proc);
2510 } else if (tid > PID_MAX)
2511 tdt = tdfind(tid, pid);
2514 * Initial thread where the tid equal to the pid.
2518 if (SV_PROC_ABI(p) != SV_ABI_LINUX) {
2520 * p is not a Linuxulator process.
2525 FOREACH_THREAD_IN_PROC(p, tdt) {
2527 if (tid == em->em_tid)
2539 linux_to_bsd_waitopts(int options, int *bsdopts)
2542 if (options & LINUX_WNOHANG)
2543 *bsdopts |= WNOHANG;
2544 if (options & LINUX_WUNTRACED)
2545 *bsdopts |= WUNTRACED;
2546 if (options & LINUX_WEXITED)
2547 *bsdopts |= WEXITED;
2548 if (options & LINUX_WCONTINUED)
2549 *bsdopts |= WCONTINUED;
2550 if (options & LINUX_WNOWAIT)
2551 *bsdopts |= WNOWAIT;
2553 if (options & __WCLONE)
2554 *bsdopts |= WLINUXCLONE;
2558 linux_getrandom(struct thread *td, struct linux_getrandom_args *args)
2564 if (args->flags & ~(LINUX_GRND_NONBLOCK|LINUX_GRND_RANDOM))
2566 if (args->count > INT_MAX)
2567 args->count = INT_MAX;
2569 iov.iov_base = args->buf;
2570 iov.iov_len = args->count;
2574 uio.uio_resid = iov.iov_len;
2575 uio.uio_segflg = UIO_USERSPACE;
2576 uio.uio_rw = UIO_READ;
2579 error = read_random_uio(&uio, args->flags & LINUX_GRND_NONBLOCK);
2581 td->td_retval[0] = args->count - uio.uio_resid;
2586 linux_mincore(struct thread *td, struct linux_mincore_args *args)
2589 /* Needs to be page-aligned */
2590 if (args->start & PAGE_MASK)
2592 return (kern_mincore(td, args->start, args->len, args->vec));