2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2002 Doug Rabson
5 * Copyright (c) 1994-1995 Søren Schmidt
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer
13 * in this position and unchanged.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_compat.h"
37 #include <sys/param.h>
38 #include <sys/blist.h>
39 #include <sys/fcntl.h>
41 #include <sys/imgact_aout.h>
44 #include <sys/kernel.h>
45 #include <sys/limits.h>
47 #include <sys/malloc.h>
49 #include <sys/mount.h>
50 #include <sys/mutex.h>
51 #include <sys/namei.h>
54 #include <sys/procctl.h>
55 #include <sys/reboot.h>
56 #include <sys/racct.h>
57 #include <sys/random.h>
58 #include <sys/resourcevar.h>
59 #include <sys/sched.h>
61 #include <sys/signalvar.h>
63 #include <sys/syscallsubr.h>
64 #include <sys/sysctl.h>
65 #include <sys/sysproto.h>
66 #include <sys/systm.h>
68 #include <sys/vmmeter.h>
69 #include <sys/vnode.h>
71 #include <sys/cpuset.h>
74 #include <security/mac/mac_framework.h>
78 #include <vm/vm_kern.h>
79 #include <vm/vm_map.h>
80 #include <vm/vm_extern.h>
81 #include <vm/vm_object.h>
82 #include <vm/swap_pager.h>
85 #include <machine/../linux32/linux.h>
86 #include <machine/../linux32/linux32_proto.h>
88 #include <machine/../linux/linux.h>
89 #include <machine/../linux/linux_proto.h>
92 #include <compat/linux/linux_dtrace.h>
93 #include <compat/linux/linux_file.h>
94 #include <compat/linux/linux_mib.h>
95 #include <compat/linux/linux_signal.h>
96 #include <compat/linux/linux_timer.h>
97 #include <compat/linux/linux_util.h>
98 #include <compat/linux/linux_sysproto.h>
99 #include <compat/linux/linux_emul.h>
100 #include <compat/linux/linux_misc.h>
103 * Special DTrace provider for the linuxulator.
105 * In this file we define the provider for the entire linuxulator. All
106 * modules (= files of the linuxulator) use it.
108 * We define a different name depending on the emulated bitsize, see
109 * ../../<ARCH>/linux{,32}/linux.h, e.g.:
110 * native bitsize = linuxulator
111 * amd64, 32bit emulation = linuxulator32
113 LIN_SDT_PROVIDER_DEFINE(LINUX_DTRACE);
115 int stclohz; /* Statistics clock frequency */
117 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
118 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
119 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
120 RLIMIT_MEMLOCK, RLIMIT_AS
124 l_long uptime; /* Seconds since boot */
125 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */
126 #define LINUX_SYSINFO_LOADS_SCALE 65536
127 l_ulong totalram; /* Total usable main memory size */
128 l_ulong freeram; /* Available memory size */
129 l_ulong sharedram; /* Amount of shared memory */
130 l_ulong bufferram; /* Memory used by buffers */
131 l_ulong totalswap; /* Total swap space size */
132 l_ulong freeswap; /* swap space still available */
133 l_ushort procs; /* Number of current processes */
138 char _f[20-2*sizeof(l_long)-sizeof(l_int)]; /* padding */
141 struct l_pselect6arg {
146 static int linux_utimensat_nsec_valid(l_long);
150 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args)
152 struct l_sysinfo sysinfo;
157 bzero(&sysinfo, sizeof(sysinfo));
161 sysinfo.uptime = ts.tv_sec;
163 /* Use the information from the mib to get our load averages */
164 for (i = 0; i < 3; i++)
165 sysinfo.loads[i] = averunnable.ldavg[i] *
166 LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale;
168 sysinfo.totalram = physmem * PAGE_SIZE;
169 sysinfo.freeram = sysinfo.totalram - vm_wire_count() * PAGE_SIZE;
171 sysinfo.sharedram = 0;
172 mtx_lock(&vm_object_list_mtx);
173 TAILQ_FOREACH(object, &vm_object_list, object_list)
174 if (object->shadow_count > 1)
175 sysinfo.sharedram += object->resident_page_count;
176 mtx_unlock(&vm_object_list_mtx);
178 sysinfo.sharedram *= PAGE_SIZE;
179 sysinfo.bufferram = 0;
181 swap_pager_status(&i, &j);
182 sysinfo.totalswap = i * PAGE_SIZE;
183 sysinfo.freeswap = (i - j) * PAGE_SIZE;
185 sysinfo.procs = nprocs;
187 /* The following are only present in newer Linux kernels. */
188 sysinfo.totalbig = 0;
190 sysinfo.mem_unit = 1;
192 return (copyout(&sysinfo, args->info, sizeof(sysinfo)));
195 #ifdef LINUX_LEGACY_SYSCALLS
197 linux_alarm(struct thread *td, struct linux_alarm_args *args)
199 struct itimerval it, old_it;
205 printf(ARGS(alarm, "%u"), args->secs);
209 * Linux alarm() is always successful. Limit secs to INT32_MAX / 2
210 * to match kern_setitimer()'s limit to avoid error from it.
212 * XXX. Linux limit secs to INT_MAX on 32 and does not limit on 64-bit
215 if (secs > INT32_MAX / 2)
216 secs = INT32_MAX / 2;
218 it.it_value.tv_sec = secs;
219 it.it_value.tv_usec = 0;
220 timevalclear(&it.it_interval);
221 error = kern_setitimer(td, ITIMER_REAL, &it, &old_it);
222 KASSERT(error == 0, ("kern_setitimer returns %d", error));
224 if ((old_it.it_value.tv_sec == 0 && old_it.it_value.tv_usec > 0) ||
225 old_it.it_value.tv_usec >= 500000)
226 old_it.it_value.tv_sec++;
227 td->td_retval[0] = old_it.it_value.tv_sec;
233 linux_brk(struct thread *td, struct linux_brk_args *args)
235 struct vmspace *vm = td->td_proc->p_vmspace;
240 printf(ARGS(brk, "%p"), (void *)(uintptr_t)args->dsend);
242 old = (uintptr_t)vm->vm_daddr + ctob(vm->vm_dsize);
243 new = (uintptr_t)args->dsend;
244 if ((caddr_t)new > vm->vm_daddr && !kern_break(td, &new))
245 td->td_retval[0] = (register_t)new;
247 td->td_retval[0] = (register_t)old;
252 #if defined(__i386__)
253 /* XXX: what about amd64/linux32? */
256 linux_uselib(struct thread *td, struct linux_uselib_args *args)
262 vm_map_entry_t entry;
265 unsigned long file_offset;
266 unsigned long bss_size;
270 bool locked, opened, textset;
272 LCONVPATHEXIST(td, args->library, &library);
276 printf(ARGS(uselib, "%s"), library);
285 NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | AUDITVNODE1,
286 UIO_SYSSPACE, library, td);
293 NDFREE(&ni, NDF_ONLY_PNBUF);
296 * From here on down, we have a locked vnode that must be unlocked.
297 * XXX: The code below largely duplicates exec_check_permissions().
302 error = VOP_GETATTR(vp, &attr, td->td_ucred);
306 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
307 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
308 /* EACCESS is what exec(2) returns. */
314 if (attr.va_size == 0) {
319 /* Can we access it? */
320 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
325 * XXX: This should use vn_open() so that it is properly authorized,
326 * and to reduce code redundancy all over the place here.
327 * XXX: Not really, it duplicates far more of exec_check_permissions()
331 error = mac_vnode_check_open(td->td_ucred, vp, VREAD);
335 error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL);
340 /* Pull in executable header into exec_map */
341 error = vm_mmap(exec_map, (vm_offset_t *)&a_out, PAGE_SIZE,
342 VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 0);
346 /* Is it a Linux binary ? */
347 if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
353 * While we are here, we should REALLY do some more checks
356 /* Set file/virtual offset based on a.out variant. */
357 switch ((int)(a_out->a_magic & 0xffff)) {
358 case 0413: /* ZMAGIC */
361 case 0314: /* QMAGIC */
369 bss_size = round_page(a_out->a_bss);
371 /* Check various fields in header for validity/bounds. */
372 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
377 /* text + data can't exceed file size */
378 if (a_out->a_data + a_out->a_text > attr.va_size) {
384 * text/data/bss must not exceed limits
385 * XXX - this is not complete. it should check current usage PLUS
386 * the resources needed by this library.
388 PROC_LOCK(td->td_proc);
389 if (a_out->a_text > maxtsiz ||
390 a_out->a_data + bss_size > lim_cur_proc(td->td_proc, RLIMIT_DATA) ||
391 racct_set(td->td_proc, RACCT_DATA, a_out->a_data +
393 PROC_UNLOCK(td->td_proc);
397 PROC_UNLOCK(td->td_proc);
400 * Prevent more writers.
402 error = VOP_SET_TEXT(vp);
408 * Lock no longer needed
414 * Check if file_offset page aligned. Currently we cannot handle
415 * misalinged file offsets, and so we read in the entire image
418 if (file_offset & PAGE_MASK) {
420 printf("uselib: Non page aligned binary %lu\n", file_offset);
422 /* Map text+data read/write/execute */
424 /* a_entry is the load address and is page aligned */
425 vmaddr = trunc_page(a_out->a_entry);
427 /* get anon user mapping, read+write+execute */
428 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
429 &vmaddr, a_out->a_text + a_out->a_data, 0, VMFS_NO_SPACE,
430 VM_PROT_ALL, VM_PROT_ALL, 0);
434 error = vn_rdwr(UIO_READ, vp, (void *)vmaddr, file_offset,
435 a_out->a_text + a_out->a_data, UIO_USERSPACE, 0,
436 td->td_ucred, NOCRED, &aresid, td);
445 printf("uselib: Page aligned binary %lu\n", file_offset);
448 * for QMAGIC, a_entry is 20 bytes beyond the load address
449 * to skip the executable header
451 vmaddr = trunc_page(a_out->a_entry);
454 * Map it all into the process's space as a single
455 * copy-on-write "data" segment.
457 map = &td->td_proc->p_vmspace->vm_map;
458 error = vm_mmap(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);
464 if (!vm_map_lookup_entry(map, vmaddr, &entry)) {
469 entry->eflags |= MAP_ENTRY_VN_EXEC;
474 printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long *)vmaddr)[0],
475 ((long *)vmaddr)[1]);
478 /* Calculate BSS start address */
479 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
482 /* allocate some 'anon' space */
483 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
484 &vmaddr, bss_size, 0, VMFS_NO_SPACE, VM_PROT_ALL,
495 VOP_CLOSE(vp, FREAD, td->td_ucred, td);
498 VOP_UNSET_TEXT_CHECKED(vp);
502 /* Release the temporary mapping. */
504 kmap_free_wakeup(exec_map, (vm_offset_t)a_out, PAGE_SIZE);
509 #endif /* __i386__ */
511 #ifdef LINUX_LEGACY_SYSCALLS
513 linux_select(struct thread *td, struct linux_select_args *args)
516 struct timeval tv0, tv1, utv, *tvp;
521 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
522 (void *)args->readfds, (void *)args->writefds,
523 (void *)args->exceptfds, (void *)args->timeout);
527 * Store current time for computation of the amount of
531 if ((error = copyin(args->timeout, <v, sizeof(ltv))))
533 utv.tv_sec = ltv.tv_sec;
534 utv.tv_usec = ltv.tv_usec;
537 printf(LMSG("incoming timeout (%jd/%ld)"),
538 (intmax_t)utv.tv_sec, utv.tv_usec);
541 if (itimerfix(&utv)) {
543 * The timeval was invalid. Convert it to something
544 * valid that will act as it does under Linux.
546 utv.tv_sec += utv.tv_usec / 1000000;
547 utv.tv_usec %= 1000000;
548 if (utv.tv_usec < 0) {
550 utv.tv_usec += 1000000;
560 error = kern_select(td, args->nfds, args->readfds, args->writefds,
561 args->exceptfds, tvp, LINUX_NFDBITS);
565 printf(LMSG("real select returns %d"), error);
571 if (td->td_retval[0]) {
573 * Compute how much time was left of the timeout,
574 * by subtracting the current time and the time
575 * before we started the call, and subtracting
576 * that result from the user-supplied value.
579 timevalsub(&tv1, &tv0);
580 timevalsub(&utv, &tv1);
587 printf(LMSG("outgoing timeout (%jd/%ld)"),
588 (intmax_t)utv.tv_sec, utv.tv_usec);
590 ltv.tv_sec = utv.tv_sec;
591 ltv.tv_usec = utv.tv_usec;
592 if ((error = copyout(<v, args->timeout, sizeof(ltv))))
599 printf(LMSG("select_out -> %d"), error);
606 linux_mremap(struct thread *td, struct linux_mremap_args *args)
614 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
615 (void *)(uintptr_t)args->addr,
616 (unsigned long)args->old_len,
617 (unsigned long)args->new_len,
618 (unsigned long)args->flags);
621 if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) {
622 td->td_retval[0] = 0;
627 * Check for the page alignment.
628 * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK.
630 if (args->addr & PAGE_MASK) {
631 td->td_retval[0] = 0;
635 args->new_len = round_page(args->new_len);
636 args->old_len = round_page(args->old_len);
638 if (args->new_len > args->old_len) {
639 td->td_retval[0] = 0;
643 if (args->new_len < args->old_len) {
644 addr = args->addr + args->new_len;
645 len = args->old_len - args->new_len;
646 error = kern_munmap(td, addr, len);
649 td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
653 #define LINUX_MS_ASYNC 0x0001
654 #define LINUX_MS_INVALIDATE 0x0002
655 #define LINUX_MS_SYNC 0x0004
658 linux_msync(struct thread *td, struct linux_msync_args *args)
661 return (kern_msync(td, args->addr, args->len,
662 args->fl & ~LINUX_MS_SYNC));
665 #ifdef LINUX_LEGACY_SYSCALLS
667 linux_time(struct thread *td, struct linux_time_args *args)
675 printf(ARGS(time, "*"));
680 if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
682 td->td_retval[0] = tm;
687 struct l_times_argv {
690 l_clock_t tms_cutime;
691 l_clock_t tms_cstime;
696 * Glibc versions prior to 2.2.1 always use hard-coded CLK_TCK value.
697 * Since 2.2.1 Glibc uses value exported from kernel via AT_CLKTCK
698 * auxiliary vector entry.
702 #define CONVOTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
703 #define CONVNTCK(r) (r.tv_sec * stclohz + r.tv_usec / (1000000 / stclohz))
705 #define CONVTCK(r) (linux_kernver(td) >= LINUX_KERNVER_2004000 ? \
706 CONVNTCK(r) : CONVOTCK(r))
709 linux_times(struct thread *td, struct linux_times_args *args)
711 struct timeval tv, utime, stime, cutime, cstime;
712 struct l_times_argv tms;
718 printf(ARGS(times, "*"));
721 if (args->buf != NULL) {
725 calcru(p, &utime, &stime);
727 calccru(p, &cutime, &cstime);
730 tms.tms_utime = CONVTCK(utime);
731 tms.tms_stime = CONVTCK(stime);
733 tms.tms_cutime = CONVTCK(cutime);
734 tms.tms_cstime = CONVTCK(cstime);
736 if ((error = copyout(&tms, args->buf, sizeof(tms))))
741 td->td_retval[0] = (int)CONVTCK(tv);
746 linux_newuname(struct thread *td, struct linux_newuname_args *args)
748 struct l_new_utsname utsname;
749 char osname[LINUX_MAX_UTSNAME];
750 char osrelease[LINUX_MAX_UTSNAME];
754 if (ldebug(newuname))
755 printf(ARGS(newuname, "*"));
758 linux_get_osname(td, osname);
759 linux_get_osrelease(td, osrelease);
761 bzero(&utsname, sizeof(utsname));
762 strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
763 getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
764 getcreddomainname(td->td_ucred, utsname.domainname, LINUX_MAX_UTSNAME);
765 strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
766 strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
767 for (p = utsname.version; *p != '\0'; ++p)
772 strlcpy(utsname.machine, linux_kplatform, LINUX_MAX_UTSNAME);
774 return (copyout(&utsname, args->buf, sizeof(utsname)));
782 #ifdef LINUX_LEGACY_SYSCALLS
784 linux_utime(struct thread *td, struct linux_utime_args *args)
786 struct timeval tv[2], *tvp;
787 struct l_utimbuf lut;
791 LCONVPATHEXIST(td, args->fname, &fname);
795 printf(ARGS(utime, "%s, *"), fname);
799 if ((error = copyin(args->times, &lut, sizeof lut))) {
803 tv[0].tv_sec = lut.l_actime;
805 tv[1].tv_sec = lut.l_modtime;
811 error = kern_utimesat(td, AT_FDCWD, fname, UIO_SYSSPACE, tvp,
818 #ifdef LINUX_LEGACY_SYSCALLS
820 linux_utimes(struct thread *td, struct linux_utimes_args *args)
823 struct timeval tv[2], *tvp = NULL;
827 LCONVPATHEXIST(td, args->fname, &fname);
831 printf(ARGS(utimes, "%s, *"), fname);
834 if (args->tptr != NULL) {
835 if ((error = copyin(args->tptr, ltv, sizeof ltv))) {
839 tv[0].tv_sec = ltv[0].tv_sec;
840 tv[0].tv_usec = ltv[0].tv_usec;
841 tv[1].tv_sec = ltv[1].tv_sec;
842 tv[1].tv_usec = ltv[1].tv_usec;
846 error = kern_utimesat(td, AT_FDCWD, fname, UIO_SYSSPACE,
854 linux_utimensat_nsec_valid(l_long nsec)
857 if (nsec == LINUX_UTIME_OMIT || nsec == LINUX_UTIME_NOW)
859 if (nsec >= 0 && nsec <= 999999999)
865 linux_utimensat(struct thread *td, struct linux_utimensat_args *args)
867 struct l_timespec l_times[2];
868 struct timespec times[2], *timesp = NULL;
870 int error, dfd, flags = 0;
872 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
875 if (ldebug(utimensat))
876 printf(ARGS(utimensat, "%d, *"), dfd);
879 if (args->flags & ~LINUX_AT_SYMLINK_NOFOLLOW)
882 if (args->times != NULL) {
883 error = copyin(args->times, l_times, sizeof(l_times));
887 if (linux_utimensat_nsec_valid(l_times[0].tv_nsec) != 0 ||
888 linux_utimensat_nsec_valid(l_times[1].tv_nsec) != 0)
891 times[0].tv_sec = l_times[0].tv_sec;
892 switch (l_times[0].tv_nsec)
894 case LINUX_UTIME_OMIT:
895 times[0].tv_nsec = UTIME_OMIT;
897 case LINUX_UTIME_NOW:
898 times[0].tv_nsec = UTIME_NOW;
901 times[0].tv_nsec = l_times[0].tv_nsec;
904 times[1].tv_sec = l_times[1].tv_sec;
905 switch (l_times[1].tv_nsec)
907 case LINUX_UTIME_OMIT:
908 times[1].tv_nsec = UTIME_OMIT;
910 case LINUX_UTIME_NOW:
911 times[1].tv_nsec = UTIME_NOW;
914 times[1].tv_nsec = l_times[1].tv_nsec;
919 /* This breaks POSIX, but is what the Linux kernel does
920 * _on purpose_ (documented in the man page for utimensat(2)),
921 * so we must follow that behaviour. */
922 if (times[0].tv_nsec == UTIME_OMIT &&
923 times[1].tv_nsec == UTIME_OMIT)
927 if (args->pathname != NULL)
928 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
929 else if (args->flags != 0)
932 if (args->flags & LINUX_AT_SYMLINK_NOFOLLOW)
933 flags |= AT_SYMLINK_NOFOLLOW;
936 error = kern_futimens(td, dfd, timesp, UIO_SYSSPACE);
938 error = kern_utimensat(td, dfd, path, UIO_SYSSPACE, timesp,
939 UIO_SYSSPACE, flags);
946 #ifdef LINUX_LEGACY_SYSCALLS
948 linux_futimesat(struct thread *td, struct linux_futimesat_args *args)
951 struct timeval tv[2], *tvp = NULL;
955 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
956 LCONVPATHEXIST_AT(td, args->filename, &fname, dfd);
959 if (ldebug(futimesat))
960 printf(ARGS(futimesat, "%s, *"), fname);
963 if (args->utimes != NULL) {
964 if ((error = copyin(args->utimes, ltv, sizeof ltv))) {
968 tv[0].tv_sec = ltv[0].tv_sec;
969 tv[0].tv_usec = ltv[0].tv_usec;
970 tv[1].tv_sec = ltv[1].tv_sec;
971 tv[1].tv_usec = ltv[1].tv_usec;
975 error = kern_utimesat(td, dfd, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
982 linux_common_wait(struct thread *td, int pid, int *status,
983 int options, struct rusage *ru)
987 error = kern_wait(td, pid, &tmpstat, options, ru);
993 if (WIFSIGNALED(tmpstat))
994 tmpstat = (tmpstat & 0xffffff80) |
995 bsd_to_linux_signal(WTERMSIG(tmpstat));
996 else if (WIFSTOPPED(tmpstat))
997 tmpstat = (tmpstat & 0xffff00ff) |
998 (bsd_to_linux_signal(WSTOPSIG(tmpstat)) << 8);
999 else if (WIFCONTINUED(tmpstat))
1001 error = copyout(&tmpstat, status, sizeof(int));
1007 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1009 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
1011 struct linux_wait4_args wait4_args;
1014 if (ldebug(waitpid))
1015 printf(ARGS(waitpid, "%d, %p, %d"),
1016 args->pid, (void *)args->status, args->options);
1019 wait4_args.pid = args->pid;
1020 wait4_args.status = args->status;
1021 wait4_args.options = args->options;
1022 wait4_args.rusage = NULL;
1024 return (linux_wait4(td, &wait4_args));
1026 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1029 linux_wait4(struct thread *td, struct linux_wait4_args *args)
1032 struct rusage ru, *rup;
1036 printf(ARGS(wait4, "%d, %p, %d, %p"),
1037 args->pid, (void *)args->status, args->options,
1038 (void *)args->rusage);
1040 if (args->options & ~(LINUX_WUNTRACED | LINUX_WNOHANG |
1041 LINUX_WCONTINUED | __WCLONE | __WNOTHREAD | __WALL))
1045 linux_to_bsd_waitopts(args->options, &options);
1047 if (args->rusage != NULL)
1051 error = linux_common_wait(td, args->pid, args->status, options, rup);
1054 if (args->rusage != NULL)
1055 error = linux_copyout_rusage(&ru, args->rusage);
1060 linux_waitid(struct thread *td, struct linux_waitid_args *args)
1062 int status, options, sig;
1063 struct __wrusage wru;
1071 linux_to_bsd_waitopts(args->options, &options);
1073 if (options & ~(WNOHANG | WNOWAIT | WEXITED | WUNTRACED | WCONTINUED))
1075 if (!(options & (WEXITED | WUNTRACED | WCONTINUED)))
1078 switch (args->idtype) {
1096 error = kern_wait6(td, idtype, args->id, &status, options,
1100 if (args->rusage != NULL) {
1101 error = linux_copyout_rusage(&wru.wru_children,
1106 if (args->info != NULL) {
1108 bzero(&lsi, sizeof(lsi));
1109 if (td->td_retval[0] != 0) {
1110 sig = bsd_to_linux_signal(siginfo.si_signo);
1111 siginfo_to_lsiginfo(&siginfo, &lsi, sig);
1113 error = copyout(&lsi, args->info, sizeof(lsi));
1115 td->td_retval[0] = 0;
1120 #ifdef LINUX_LEGACY_SYSCALLS
1122 linux_mknod(struct thread *td, struct linux_mknod_args *args)
1127 LCONVPATHCREAT(td, args->path, &path);
1131 printf(ARGS(mknod, "%s, %d, %ju"), path, args->mode,
1132 (uintmax_t)args->dev);
1135 switch (args->mode & S_IFMT) {
1138 error = kern_mkfifoat(td, AT_FDCWD, path, UIO_SYSSPACE,
1144 error = kern_mknodat(td, AT_FDCWD, path, UIO_SYSSPACE,
1145 args->mode, args->dev);
1153 args->mode |= S_IFREG;
1156 error = kern_openat(td, AT_FDCWD, path, UIO_SYSSPACE,
1157 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1159 kern_close(td, td->td_retval[0]);
1172 linux_mknodat(struct thread *td, struct linux_mknodat_args *args)
1177 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1178 LCONVPATHCREAT_AT(td, args->filename, &path, dfd);
1181 if (ldebug(mknodat))
1182 printf(ARGS(mknodat, "%s, %d, %d"), path, args->mode, args->dev);
1185 switch (args->mode & S_IFMT) {
1188 error = kern_mkfifoat(td, dfd, path, UIO_SYSSPACE, args->mode);
1193 error = kern_mknodat(td, dfd, path, UIO_SYSSPACE, args->mode,
1202 args->mode |= S_IFREG;
1205 error = kern_openat(td, dfd, path, UIO_SYSSPACE,
1206 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1208 kern_close(td, td->td_retval[0]);
1220 * UGH! This is just about the dumbest idea I've ever heard!!
1223 linux_personality(struct thread *td, struct linux_personality_args *args)
1225 struct linux_pemuldata *pem;
1226 struct proc *p = td->td_proc;
1230 if (ldebug(personality))
1231 printf(ARGS(personality, "%u"), args->per);
1237 if (args->per != 0xffffffff)
1238 pem->persona = args->per;
1241 td->td_retval[0] = old;
1245 struct l_itimerval {
1246 l_timeval it_interval;
1250 #define B2L_ITIMERVAL(bip, lip) \
1251 (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec; \
1252 (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec; \
1253 (bip)->it_value.tv_sec = (lip)->it_value.tv_sec; \
1254 (bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
1257 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
1260 struct l_itimerval ls;
1261 struct itimerval aitv, oitv;
1264 if (ldebug(setitimer))
1265 printf(ARGS(setitimer, "%p, %p"),
1266 (void *)uap->itv, (void *)uap->oitv);
1269 if (uap->itv == NULL) {
1270 uap->itv = uap->oitv;
1271 return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
1274 error = copyin(uap->itv, &ls, sizeof(ls));
1277 B2L_ITIMERVAL(&aitv, &ls);
1279 if (ldebug(setitimer)) {
1280 printf("setitimer: value: sec: %jd, usec: %ld\n",
1281 (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec);
1282 printf("setitimer: interval: sec: %jd, usec: %ld\n",
1283 (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec);
1286 error = kern_setitimer(td, uap->which, &aitv, &oitv);
1287 if (error != 0 || uap->oitv == NULL)
1289 B2L_ITIMERVAL(&ls, &oitv);
1291 return (copyout(&ls, uap->oitv, sizeof(ls)));
1295 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
1298 struct l_itimerval ls;
1299 struct itimerval aitv;
1302 if (ldebug(getitimer))
1303 printf(ARGS(getitimer, "%p"), (void *)uap->itv);
1305 error = kern_getitimer(td, uap->which, &aitv);
1308 B2L_ITIMERVAL(&ls, &aitv);
1309 return (copyout(&ls, uap->itv, sizeof(ls)));
1312 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1314 linux_nice(struct thread *td, struct linux_nice_args *args)
1316 struct setpriority_args bsd_args;
1318 bsd_args.which = PRIO_PROCESS;
1319 bsd_args.who = 0; /* current process */
1320 bsd_args.prio = args->inc;
1321 return (sys_setpriority(td, &bsd_args));
1323 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1326 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
1328 struct ucred *newcred, *oldcred;
1329 l_gid_t *linux_gidset;
1334 ngrp = args->gidsetsize;
1335 if (ngrp < 0 || ngrp >= ngroups_max + 1)
1337 linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_LINUX, M_WAITOK);
1338 error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
1342 crextend(newcred, ngrp + 1);
1345 oldcred = p->p_ucred;
1346 crcopy(newcred, oldcred);
1349 * cr_groups[0] holds egid. Setting the whole set from
1350 * the supplied set will cause egid to be changed too.
1351 * Keep cr_groups[0] unchanged to prevent that.
1354 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS)) != 0) {
1361 newcred->cr_ngroups = ngrp + 1;
1363 bsd_gidset = newcred->cr_groups;
1366 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1370 newcred->cr_ngroups = 1;
1373 proc_set_cred(p, newcred);
1378 free(linux_gidset, M_LINUX);
1383 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1386 l_gid_t *linux_gidset;
1388 int bsd_gidsetsz, ngrp, error;
1390 cred = td->td_ucred;
1391 bsd_gidset = cred->cr_groups;
1392 bsd_gidsetsz = cred->cr_ngroups - 1;
1395 * cr_groups[0] holds egid. Returning the whole set
1396 * here will cause a duplicate. Exclude cr_groups[0]
1400 if ((ngrp = args->gidsetsize) == 0) {
1401 td->td_retval[0] = bsd_gidsetsz;
1405 if (ngrp < bsd_gidsetsz)
1409 linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset),
1411 while (ngrp < bsd_gidsetsz) {
1412 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1416 error = copyout(linux_gidset, args->grouplist, ngrp * sizeof(l_gid_t));
1417 free(linux_gidset, M_LINUX);
1421 td->td_retval[0] = ngrp;
1426 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1428 struct rlimit bsd_rlim;
1429 struct l_rlimit rlim;
1434 if (ldebug(setrlimit))
1435 printf(ARGS(setrlimit, "%d, %p"),
1436 args->resource, (void *)args->rlim);
1439 if (args->resource >= LINUX_RLIM_NLIMITS)
1442 which = linux_to_bsd_resource[args->resource];
1446 error = copyin(args->rlim, &rlim, sizeof(rlim));
1450 bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1451 bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1452 return (kern_setrlimit(td, which, &bsd_rlim));
1455 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1457 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1459 struct l_rlimit rlim;
1460 struct rlimit bsd_rlim;
1464 if (ldebug(old_getrlimit))
1465 printf(ARGS(old_getrlimit, "%d, %p"),
1466 args->resource, (void *)args->rlim);
1469 if (args->resource >= LINUX_RLIM_NLIMITS)
1472 which = linux_to_bsd_resource[args->resource];
1476 lim_rlimit(td, which, &bsd_rlim);
1478 #ifdef COMPAT_LINUX32
1479 rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1480 if (rlim.rlim_cur == UINT_MAX)
1481 rlim.rlim_cur = INT_MAX;
1482 rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1483 if (rlim.rlim_max == UINT_MAX)
1484 rlim.rlim_max = INT_MAX;
1486 rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1487 if (rlim.rlim_cur == ULONG_MAX)
1488 rlim.rlim_cur = LONG_MAX;
1489 rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1490 if (rlim.rlim_max == ULONG_MAX)
1491 rlim.rlim_max = LONG_MAX;
1493 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1495 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1498 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1500 struct l_rlimit rlim;
1501 struct rlimit bsd_rlim;
1505 if (ldebug(getrlimit))
1506 printf(ARGS(getrlimit, "%d, %p"),
1507 args->resource, (void *)args->rlim);
1510 if (args->resource >= LINUX_RLIM_NLIMITS)
1513 which = linux_to_bsd_resource[args->resource];
1517 lim_rlimit(td, which, &bsd_rlim);
1519 rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1520 rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1521 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1525 linux_sched_setscheduler(struct thread *td,
1526 struct linux_sched_setscheduler_args *args)
1528 struct sched_param sched_param;
1533 if (ldebug(sched_setscheduler))
1534 printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1535 args->pid, args->policy, (const void *)args->param);
1538 switch (args->policy) {
1539 case LINUX_SCHED_OTHER:
1540 policy = SCHED_OTHER;
1542 case LINUX_SCHED_FIFO:
1543 policy = SCHED_FIFO;
1545 case LINUX_SCHED_RR:
1552 error = copyin(args->param, &sched_param, sizeof(sched_param));
1556 tdt = linux_tdfind(td, args->pid, -1);
1560 error = kern_sched_setscheduler(td, tdt, policy, &sched_param);
1561 PROC_UNLOCK(tdt->td_proc);
1566 linux_sched_getscheduler(struct thread *td,
1567 struct linux_sched_getscheduler_args *args)
1573 if (ldebug(sched_getscheduler))
1574 printf(ARGS(sched_getscheduler, "%d"), args->pid);
1577 tdt = linux_tdfind(td, args->pid, -1);
1581 error = kern_sched_getscheduler(td, tdt, &policy);
1582 PROC_UNLOCK(tdt->td_proc);
1586 td->td_retval[0] = LINUX_SCHED_OTHER;
1589 td->td_retval[0] = LINUX_SCHED_FIFO;
1592 td->td_retval[0] = LINUX_SCHED_RR;
1599 linux_sched_get_priority_max(struct thread *td,
1600 struct linux_sched_get_priority_max_args *args)
1602 struct sched_get_priority_max_args bsd;
1605 if (ldebug(sched_get_priority_max))
1606 printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1609 switch (args->policy) {
1610 case LINUX_SCHED_OTHER:
1611 bsd.policy = SCHED_OTHER;
1613 case LINUX_SCHED_FIFO:
1614 bsd.policy = SCHED_FIFO;
1616 case LINUX_SCHED_RR:
1617 bsd.policy = SCHED_RR;
1622 return (sys_sched_get_priority_max(td, &bsd));
1626 linux_sched_get_priority_min(struct thread *td,
1627 struct linux_sched_get_priority_min_args *args)
1629 struct sched_get_priority_min_args bsd;
1632 if (ldebug(sched_get_priority_min))
1633 printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1636 switch (args->policy) {
1637 case LINUX_SCHED_OTHER:
1638 bsd.policy = SCHED_OTHER;
1640 case LINUX_SCHED_FIFO:
1641 bsd.policy = SCHED_FIFO;
1643 case LINUX_SCHED_RR:
1644 bsd.policy = SCHED_RR;
1649 return (sys_sched_get_priority_min(td, &bsd));
1652 #define REBOOT_CAD_ON 0x89abcdef
1653 #define REBOOT_CAD_OFF 0
1654 #define REBOOT_HALT 0xcdef0123
1655 #define REBOOT_RESTART 0x01234567
1656 #define REBOOT_RESTART2 0xA1B2C3D4
1657 #define REBOOT_POWEROFF 0x4321FEDC
1658 #define REBOOT_MAGIC1 0xfee1dead
1659 #define REBOOT_MAGIC2 0x28121969
1660 #define REBOOT_MAGIC2A 0x05121996
1661 #define REBOOT_MAGIC2B 0x16041998
1664 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1666 struct reboot_args bsd_args;
1670 printf(ARGS(reboot, "0x%x"), args->cmd);
1673 if (args->magic1 != REBOOT_MAGIC1)
1676 switch (args->magic2) {
1678 case REBOOT_MAGIC2A:
1679 case REBOOT_MAGIC2B:
1685 switch (args->cmd) {
1687 case REBOOT_CAD_OFF:
1688 return (priv_check(td, PRIV_REBOOT));
1690 bsd_args.opt = RB_HALT;
1692 case REBOOT_RESTART:
1693 case REBOOT_RESTART2:
1696 case REBOOT_POWEROFF:
1697 bsd_args.opt = RB_POWEROFF;
1702 return (sys_reboot(td, &bsd_args));
1707 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1708 * td->td_retval[1] when COMPAT_43 is defined. This clobbers registers that
1709 * are assumed to be preserved. The following lightweight syscalls fixes
1710 * this. See also linux_getgid16() and linux_getuid16() in linux_uid16.c
1712 * linux_getpid() - MP SAFE
1713 * linux_getgid() - MP SAFE
1714 * linux_getuid() - MP SAFE
1718 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1723 printf(ARGS(getpid, ""));
1725 td->td_retval[0] = td->td_proc->p_pid;
1731 linux_gettid(struct thread *td, struct linux_gettid_args *args)
1733 struct linux_emuldata *em;
1737 printf(ARGS(gettid, ""));
1741 KASSERT(em != NULL, ("gettid: emuldata not found.\n"));
1743 td->td_retval[0] = em->em_tid;
1750 linux_getppid(struct thread *td, struct linux_getppid_args *args)
1754 if (ldebug(getppid))
1755 printf(ARGS(getppid, ""));
1758 td->td_retval[0] = kern_getppid(td);
1763 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1768 printf(ARGS(getgid, ""));
1771 td->td_retval[0] = td->td_ucred->cr_rgid;
1776 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1781 printf(ARGS(getuid, ""));
1784 td->td_retval[0] = td->td_ucred->cr_ruid;
1790 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1792 struct getsid_args bsd;
1796 printf(ARGS(getsid, "%i"), args->pid);
1799 bsd.pid = args->pid;
1800 return (sys_getsid(td, &bsd));
1804 linux_nosys(struct thread *td, struct nosys_args *ignore)
1811 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1813 struct getpriority_args bsd_args;
1817 if (ldebug(getpriority))
1818 printf(ARGS(getpriority, "%i, %i"), args->which, args->who);
1821 bsd_args.which = args->which;
1822 bsd_args.who = args->who;
1823 error = sys_getpriority(td, &bsd_args);
1824 td->td_retval[0] = 20 - td->td_retval[0];
1829 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1834 if (ldebug(sethostname))
1835 printf(ARGS(sethostname, "*, %i"), args->len);
1839 name[1] = KERN_HOSTNAME;
1840 return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1845 linux_setdomainname(struct thread *td, struct linux_setdomainname_args *args)
1850 if (ldebug(setdomainname))
1851 printf(ARGS(setdomainname, "*, %i"), args->len);
1855 name[1] = KERN_NISDOMAINNAME;
1856 return (userland_sysctl(td, name, 2, 0, 0, 0, args->name,
1861 linux_exit_group(struct thread *td, struct linux_exit_group_args *args)
1865 if (ldebug(exit_group))
1866 printf(ARGS(exit_group, "%i"), args->error_code);
1869 LINUX_CTR2(exit_group, "thread(%d) (%d)", td->td_tid,
1873 * XXX: we should send a signal to the parent if
1874 * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?)
1875 * as it doesnt occur often.
1877 exit1(td, args->error_code, 0);
1881 #define _LINUX_CAPABILITY_VERSION_1 0x19980330
1882 #define _LINUX_CAPABILITY_VERSION_2 0x20071026
1883 #define _LINUX_CAPABILITY_VERSION_3 0x20080522
1885 struct l_user_cap_header {
1890 struct l_user_cap_data {
1897 linux_capget(struct thread *td, struct linux_capget_args *uap)
1899 struct l_user_cap_header luch;
1900 struct l_user_cap_data lucd[2];
1903 if (uap->hdrp == NULL)
1906 error = copyin(uap->hdrp, &luch, sizeof(luch));
1910 switch (luch.version) {
1911 case _LINUX_CAPABILITY_VERSION_1:
1914 case _LINUX_CAPABILITY_VERSION_2:
1915 case _LINUX_CAPABILITY_VERSION_3:
1921 printf(LMSG("invalid capget capability version 0x%x"),
1924 luch.version = _LINUX_CAPABILITY_VERSION_1;
1925 error = copyout(&luch, uap->hdrp, sizeof(luch));
1936 * The current implementation doesn't support setting
1937 * a capability (it's essentially a stub) so indicate
1938 * that no capabilities are currently set or available
1941 memset(&lucd, 0, u32s * sizeof(lucd[0]));
1942 error = copyout(&lucd, uap->datap, u32s * sizeof(lucd[0]));
1949 linux_capset(struct thread *td, struct linux_capset_args *uap)
1951 struct l_user_cap_header luch;
1952 struct l_user_cap_data lucd[2];
1955 if (uap->hdrp == NULL || uap->datap == NULL)
1958 error = copyin(uap->hdrp, &luch, sizeof(luch));
1962 switch (luch.version) {
1963 case _LINUX_CAPABILITY_VERSION_1:
1966 case _LINUX_CAPABILITY_VERSION_2:
1967 case _LINUX_CAPABILITY_VERSION_3:
1973 printf(LMSG("invalid capset capability version 0x%x"),
1976 luch.version = _LINUX_CAPABILITY_VERSION_1;
1977 error = copyout(&luch, uap->hdrp, sizeof(luch));
1986 error = copyin(uap->datap, &lucd, u32s * sizeof(lucd[0]));
1990 /* We currently don't support setting any capabilities. */
1991 for (i = 0; i < u32s; i++) {
1992 if (lucd[i].effective || lucd[i].permitted ||
1993 lucd[i].inheritable) {
1995 "capset[%d] effective=0x%x, permitted=0x%x, "
1996 "inheritable=0x%x is not implemented", i,
1997 (int)lucd[i].effective, (int)lucd[i].permitted,
1998 (int)lucd[i].inheritable);
2007 linux_prctl(struct thread *td, struct linux_prctl_args *args)
2009 int error = 0, max_size;
2010 struct proc *p = td->td_proc;
2011 char comm[LINUX_MAX_COMM_LEN];
2016 printf(ARGS(prctl, "%d, %ju, %ju, %ju, %ju"), args->option,
2017 (uintmax_t)args->arg2, (uintmax_t)args->arg3,
2018 (uintmax_t)args->arg4, (uintmax_t)args->arg5);
2021 switch (args->option) {
2022 case LINUX_PR_SET_PDEATHSIG:
2023 if (!LINUX_SIG_VALID(args->arg2))
2025 pdeath_signal = linux_to_bsd_signal(args->arg2);
2026 return (kern_procctl(td, P_PID, 0, PROC_PDEATHSIG_CTL,
2028 case LINUX_PR_GET_PDEATHSIG:
2029 error = kern_procctl(td, P_PID, 0, PROC_PDEATHSIG_STATUS,
2033 pdeath_signal = bsd_to_linux_signal(pdeath_signal);
2034 return (copyout(&pdeath_signal,
2035 (void *)(register_t)args->arg2,
2036 sizeof(pdeath_signal)));
2038 case LINUX_PR_GET_KEEPCAPS:
2040 * Indicate that we always clear the effective and
2041 * permitted capability sets when the user id becomes
2042 * non-zero (actually the capability sets are simply
2043 * always zero in the current implementation).
2045 td->td_retval[0] = 0;
2047 case LINUX_PR_SET_KEEPCAPS:
2049 * Ignore requests to keep the effective and permitted
2050 * capability sets when the user id becomes non-zero.
2053 case LINUX_PR_SET_NAME:
2055 * To be on the safe side we need to make sure to not
2056 * overflow the size a Linux program expects. We already
2057 * do this here in the copyin, so that we don't need to
2060 max_size = MIN(sizeof(comm), sizeof(p->p_comm));
2061 error = copyinstr((void *)(register_t)args->arg2, comm,
2064 /* Linux silently truncates the name if it is too long. */
2065 if (error == ENAMETOOLONG) {
2067 * XXX: copyinstr() isn't documented to populate the
2068 * array completely, so do a copyin() to be on the
2069 * safe side. This should be changed in case
2070 * copyinstr() is changed to guarantee this.
2072 error = copyin((void *)(register_t)args->arg2, comm,
2074 comm[max_size - 1] = '\0';
2080 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
2083 case LINUX_PR_GET_NAME:
2085 strlcpy(comm, p->p_comm, sizeof(comm));
2087 error = copyout(comm, (void *)(register_t)args->arg2,
2099 linux_sched_setparam(struct thread *td,
2100 struct linux_sched_setparam_args *uap)
2102 struct sched_param sched_param;
2107 if (ldebug(sched_setparam))
2108 printf(ARGS(sched_setparam, "%d, *"), uap->pid);
2111 error = copyin(uap->param, &sched_param, sizeof(sched_param));
2115 tdt = linux_tdfind(td, uap->pid, -1);
2119 error = kern_sched_setparam(td, tdt, &sched_param);
2120 PROC_UNLOCK(tdt->td_proc);
2125 linux_sched_getparam(struct thread *td,
2126 struct linux_sched_getparam_args *uap)
2128 struct sched_param sched_param;
2133 if (ldebug(sched_getparam))
2134 printf(ARGS(sched_getparam, "%d, *"), uap->pid);
2137 tdt = linux_tdfind(td, uap->pid, -1);
2141 error = kern_sched_getparam(td, tdt, &sched_param);
2142 PROC_UNLOCK(tdt->td_proc);
2144 error = copyout(&sched_param, uap->param,
2145 sizeof(sched_param));
2150 * Get affinity of a process.
2153 linux_sched_getaffinity(struct thread *td,
2154 struct linux_sched_getaffinity_args *args)
2160 if (ldebug(sched_getaffinity))
2161 printf(ARGS(sched_getaffinity, "%d, %d, *"), args->pid,
2164 if (args->len < sizeof(cpuset_t))
2167 tdt = linux_tdfind(td, args->pid, -1);
2171 PROC_UNLOCK(tdt->td_proc);
2173 error = kern_cpuset_getaffinity(td, CPU_LEVEL_WHICH, CPU_WHICH_TID,
2174 tdt->td_tid, sizeof(cpuset_t), (cpuset_t *)args->user_mask_ptr);
2176 td->td_retval[0] = sizeof(cpuset_t);
2182 * Set affinity of a process.
2185 linux_sched_setaffinity(struct thread *td,
2186 struct linux_sched_setaffinity_args *args)
2191 if (ldebug(sched_setaffinity))
2192 printf(ARGS(sched_setaffinity, "%d, %d, *"), args->pid,
2195 if (args->len < sizeof(cpuset_t))
2198 tdt = linux_tdfind(td, args->pid, -1);
2202 PROC_UNLOCK(tdt->td_proc);
2204 return (kern_cpuset_setaffinity(td, CPU_LEVEL_WHICH, CPU_WHICH_TID,
2205 tdt->td_tid, sizeof(cpuset_t), (cpuset_t *) args->user_mask_ptr));
2208 struct linux_rlimit64 {
2214 linux_prlimit64(struct thread *td, struct linux_prlimit64_args *args)
2216 struct rlimit rlim, nrlim;
2217 struct linux_rlimit64 lrlim;
2224 if (ldebug(prlimit64))
2225 printf(ARGS(prlimit64, "%d, %d, %p, %p"), args->pid,
2226 args->resource, (void *)args->new, (void *)args->old);
2229 if (args->resource >= LINUX_RLIM_NLIMITS)
2232 which = linux_to_bsd_resource[args->resource];
2236 if (args->new != NULL) {
2238 * Note. Unlike FreeBSD where rlim is signed 64-bit Linux
2239 * rlim is unsigned 64-bit. FreeBSD treats negative limits
2240 * as INFINITY so we do not need a conversion even.
2242 error = copyin(args->new, &nrlim, sizeof(nrlim));
2247 flags = PGET_HOLD | PGET_NOTWEXIT;
2248 if (args->new != NULL)
2249 flags |= PGET_CANDEBUG;
2251 flags |= PGET_CANSEE;
2252 error = pget(args->pid, flags, &p);
2256 if (args->old != NULL) {
2258 lim_rlimit_proc(p, which, &rlim);
2260 if (rlim.rlim_cur == RLIM_INFINITY)
2261 lrlim.rlim_cur = LINUX_RLIM_INFINITY;
2263 lrlim.rlim_cur = rlim.rlim_cur;
2264 if (rlim.rlim_max == RLIM_INFINITY)
2265 lrlim.rlim_max = LINUX_RLIM_INFINITY;
2267 lrlim.rlim_max = rlim.rlim_max;
2268 error = copyout(&lrlim, args->old, sizeof(lrlim));
2273 if (args->new != NULL)
2274 error = kern_proc_setrlimit(td, p, which, &nrlim);
2282 linux_pselect6(struct thread *td, struct linux_pselect6_args *args)
2284 struct timeval utv, tv0, tv1, *tvp;
2285 struct l_pselect6arg lpse6;
2286 struct l_timespec lts;
2287 struct timespec uts;
2294 if (args->sig != NULL) {
2295 error = copyin(args->sig, &lpse6, sizeof(lpse6));
2298 if (lpse6.ss_len != sizeof(l_ss))
2300 if (lpse6.ss != 0) {
2301 error = copyin(PTRIN(lpse6.ss), &l_ss,
2305 linux_to_bsd_sigset(&l_ss, &ss);
2311 * Currently glibc changes nanosecond number to microsecond.
2312 * This mean losing precision but for now it is hardly seen.
2314 if (args->tsp != NULL) {
2315 error = copyin(args->tsp, <s, sizeof(lts));
2318 error = linux_to_native_timespec(&uts, <s);
2322 TIMESPEC_TO_TIMEVAL(&utv, &uts);
2323 if (itimerfix(&utv))
2331 error = kern_pselect(td, args->nfds, args->readfds, args->writefds,
2332 args->exceptfds, tvp, ssp, LINUX_NFDBITS);
2334 if (error == 0 && args->tsp != NULL) {
2335 if (td->td_retval[0] != 0) {
2337 * Compute how much time was left of the timeout,
2338 * by subtracting the current time and the time
2339 * before we started the call, and subtracting
2340 * that result from the user-supplied value.
2344 timevalsub(&tv1, &tv0);
2345 timevalsub(&utv, &tv1);
2351 TIMEVAL_TO_TIMESPEC(&utv, &uts);
2353 error = native_to_linux_timespec(<s, &uts);
2355 error = copyout(<s, args->tsp, sizeof(lts));
2362 linux_ppoll(struct thread *td, struct linux_ppoll_args *args)
2364 struct timespec ts0, ts1;
2365 struct l_timespec lts;
2366 struct timespec uts, *tsp;
2372 if (args->sset != NULL) {
2373 if (args->ssize != sizeof(l_ss))
2375 error = copyin(args->sset, &l_ss, sizeof(l_ss));
2378 linux_to_bsd_sigset(&l_ss, &ss);
2382 if (args->tsp != NULL) {
2383 error = copyin(args->tsp, <s, sizeof(lts));
2386 error = linux_to_native_timespec(&uts, <s);
2395 error = kern_poll(td, args->fds, args->nfds, tsp, ssp);
2397 if (error == 0 && args->tsp != NULL) {
2398 if (td->td_retval[0]) {
2400 timespecsub(&ts1, &ts0, &ts1);
2401 timespecsub(&uts, &ts1, &uts);
2403 timespecclear(&uts);
2405 timespecclear(&uts);
2407 error = native_to_linux_timespec(<s, &uts);
2409 error = copyout(<s, args->tsp, sizeof(lts));
2415 #if defined(DEBUG) || defined(KTR)
2416 /* XXX: can be removed when every ldebug(...) and KTR stuff are removed. */
2418 #ifdef COMPAT_LINUX32
2419 #define L_MAXSYSCALL LINUX32_SYS_MAXSYSCALL
2421 #define L_MAXSYSCALL LINUX_SYS_MAXSYSCALL
2424 u_char linux_debug_map[howmany(L_MAXSYSCALL, sizeof(u_char))];
2427 linux_debug(int syscall, int toggle, int global)
2431 char c = toggle ? 0 : 0xff;
2433 memset(linux_debug_map, c, sizeof(linux_debug_map));
2436 if (syscall < 0 || syscall >= L_MAXSYSCALL)
2439 clrbit(linux_debug_map, syscall);
2441 setbit(linux_debug_map, syscall);
2447 * Usage: sysctl linux.debug=<syscall_nr>.<0/1>
2449 * E.g.: sysctl linux.debug=21.0
2451 * As a special case, syscall "all" will apply to all syscalls globally.
2453 #define LINUX_MAX_DEBUGSTR 16
2455 linux_sysctl_debug(SYSCTL_HANDLER_ARGS)
2457 char value[LINUX_MAX_DEBUGSTR], *p;
2458 int error, sysc, toggle;
2462 error = sysctl_handle_string(oidp, value, LINUX_MAX_DEBUGSTR, req);
2463 if (error || req->newptr == NULL)
2465 for (p = value; *p != '\0' && *p != '.'; p++);
2469 sysc = strtol(value, NULL, 0);
2470 toggle = strtol(p, NULL, 0);
2471 if (strcmp(value, "all") == 0)
2473 error = linux_debug(sysc, toggle, global);
2477 #endif /* DEBUG || KTR */
2480 linux_sched_rr_get_interval(struct thread *td,
2481 struct linux_sched_rr_get_interval_args *uap)
2484 struct l_timespec lts;
2489 * According to man in case the invalid pid specified
2490 * EINVAL should be returned.
2495 tdt = linux_tdfind(td, uap->pid, -1);
2499 error = kern_sched_rr_get_interval_td(td, tdt, &ts);
2500 PROC_UNLOCK(tdt->td_proc);
2503 error = native_to_linux_timespec(<s, &ts);
2506 return (copyout(<s, uap->interval, sizeof(lts)));
2510 * In case when the Linux thread is the initial thread in
2511 * the thread group thread id is equal to the process id.
2512 * Glibc depends on this magic (assert in pthread_getattr_np.c).
2515 linux_tdfind(struct thread *td, lwpid_t tid, pid_t pid)
2517 struct linux_emuldata *em;
2522 if (tid == 0 || tid == td->td_tid) {
2524 PROC_LOCK(tdt->td_proc);
2525 } else if (tid > PID_MAX)
2526 tdt = tdfind(tid, pid);
2529 * Initial thread where the tid equal to the pid.
2533 if (SV_PROC_ABI(p) != SV_ABI_LINUX) {
2535 * p is not a Linuxulator process.
2540 FOREACH_THREAD_IN_PROC(p, tdt) {
2542 if (tid == em->em_tid)
2554 linux_to_bsd_waitopts(int options, int *bsdopts)
2557 if (options & LINUX_WNOHANG)
2558 *bsdopts |= WNOHANG;
2559 if (options & LINUX_WUNTRACED)
2560 *bsdopts |= WUNTRACED;
2561 if (options & LINUX_WEXITED)
2562 *bsdopts |= WEXITED;
2563 if (options & LINUX_WCONTINUED)
2564 *bsdopts |= WCONTINUED;
2565 if (options & LINUX_WNOWAIT)
2566 *bsdopts |= WNOWAIT;
2568 if (options & __WCLONE)
2569 *bsdopts |= WLINUXCLONE;
2573 linux_getrandom(struct thread *td, struct linux_getrandom_args *args)
2579 if (args->flags & ~(LINUX_GRND_NONBLOCK|LINUX_GRND_RANDOM))
2581 if (args->count > INT_MAX)
2582 args->count = INT_MAX;
2584 iov.iov_base = args->buf;
2585 iov.iov_len = args->count;
2589 uio.uio_resid = iov.iov_len;
2590 uio.uio_segflg = UIO_USERSPACE;
2591 uio.uio_rw = UIO_READ;
2594 error = read_random_uio(&uio, args->flags & LINUX_GRND_NONBLOCK);
2596 td->td_retval[0] = args->count - uio.uio_resid;
2601 linux_mincore(struct thread *td, struct linux_mincore_args *args)
2604 /* Needs to be page-aligned */
2605 if (args->start & PAGE_MASK)
2607 return (kern_mincore(td, args->start, args->len, args->vec));