2 * Copyright (c) 2002 Doug Rabson
3 * Copyright (c) 1994-1995 Søren Schmidt
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer
11 * in this position and unchanged.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include "opt_compat.h"
34 #include "opt_kdtrace.h"
36 #include <sys/param.h>
37 #include <sys/blist.h>
38 #include <sys/fcntl.h>
40 #include <sys/imgact_aout.h>
43 #include <sys/kernel.h>
44 #include <sys/limits.h>
46 #include <sys/malloc.h>
48 #include <sys/mount.h>
49 #include <sys/mutex.h>
50 #include <sys/namei.h>
53 #include <sys/reboot.h>
54 #include <sys/racct.h>
55 #include <sys/resourcevar.h>
56 #include <sys/sched.h>
58 #include <sys/signalvar.h>
60 #include <sys/syscallsubr.h>
61 #include <sys/sysctl.h>
62 #include <sys/sysproto.h>
63 #include <sys/systm.h>
65 #include <sys/vmmeter.h>
66 #include <sys/vnode.h>
68 #include <sys/cpuset.h>
70 #include <security/mac/mac_framework.h>
74 #include <vm/vm_kern.h>
75 #include <vm/vm_map.h>
76 #include <vm/vm_extern.h>
77 #include <vm/vm_object.h>
78 #include <vm/swap_pager.h>
81 #include <machine/../linux32/linux.h>
82 #include <machine/../linux32/linux32_proto.h>
84 #include <machine/../linux/linux.h>
85 #include <machine/../linux/linux_proto.h>
88 #include <compat/linux/linux_dtrace.h>
89 #include <compat/linux/linux_file.h>
90 #include <compat/linux/linux_mib.h>
91 #include <compat/linux/linux_signal.h>
92 #include <compat/linux/linux_timer.h>
93 #include <compat/linux/linux_util.h>
94 #include <compat/linux/linux_sysproto.h>
95 #include <compat/linux/linux_emul.h>
96 #include <compat/linux/linux_misc.h>
99 * Special DTrace provider for the linuxulator.
101 * In this file we define the provider for the entire linuxulator. All
102 * modules (= files of the linuxulator) use it.
104 * We define a different name depending on the emulated bitsize, see
105 * ../../<ARCH>/linux{,32}/linux.h, e.g.:
106 * native bitsize = linuxulator
107 * amd64, 32bit emulation = linuxulator32
109 LIN_SDT_PROVIDER_DEFINE(LINUX_DTRACE);
111 int stclohz; /* Statistics clock frequency */
113 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
114 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
115 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
116 RLIMIT_MEMLOCK, RLIMIT_AS
120 l_long uptime; /* Seconds since boot */
121 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */
122 #define LINUX_SYSINFO_LOADS_SCALE 65536
123 l_ulong totalram; /* Total usable main memory size */
124 l_ulong freeram; /* Available memory size */
125 l_ulong sharedram; /* Amount of shared memory */
126 l_ulong bufferram; /* Memory used by buffers */
127 l_ulong totalswap; /* Total swap space size */
128 l_ulong freeswap; /* swap space still available */
129 l_ushort procs; /* Number of current processes */
134 char _f[20-2*sizeof(l_long)-sizeof(l_int)]; /* padding */
137 struct l_pselect6arg {
143 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args)
145 struct l_sysinfo sysinfo;
153 sysinfo.uptime = ts.tv_sec;
155 /* Use the information from the mib to get our load averages */
156 for (i = 0; i < 3; i++)
157 sysinfo.loads[i] = averunnable.ldavg[i] *
158 LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale;
160 sysinfo.totalram = physmem * PAGE_SIZE;
161 sysinfo.freeram = sysinfo.totalram - cnt.v_wire_count * PAGE_SIZE;
163 sysinfo.sharedram = 0;
164 mtx_lock(&vm_object_list_mtx);
165 TAILQ_FOREACH(object, &vm_object_list, object_list)
166 if (object->shadow_count > 1)
167 sysinfo.sharedram += object->resident_page_count;
168 mtx_unlock(&vm_object_list_mtx);
170 sysinfo.sharedram *= PAGE_SIZE;
171 sysinfo.bufferram = 0;
173 swap_pager_status(&i, &j);
174 sysinfo.totalswap = i * PAGE_SIZE;
175 sysinfo.freeswap = (i - j) * PAGE_SIZE;
177 sysinfo.procs = nprocs;
179 /* The following are only present in newer Linux kernels. */
180 sysinfo.totalbig = 0;
182 sysinfo.mem_unit = 1;
184 return (copyout(&sysinfo, args->info, sizeof(sysinfo)));
188 linux_alarm(struct thread *td, struct linux_alarm_args *args)
190 struct itimerval it, old_it;
196 printf(ARGS(alarm, "%u"), args->secs);
204 it.it_value.tv_sec = (long) secs;
205 it.it_value.tv_usec = 0;
206 it.it_interval.tv_sec = 0;
207 it.it_interval.tv_usec = 0;
208 error = kern_setitimer(td, ITIMER_REAL, &it, &old_it);
211 if (timevalisset(&old_it.it_value)) {
212 if (old_it.it_value.tv_usec != 0)
213 old_it.it_value.tv_sec++;
214 td->td_retval[0] = old_it.it_value.tv_sec;
220 linux_brk(struct thread *td, struct linux_brk_args *args)
222 struct vmspace *vm = td->td_proc->p_vmspace;
223 vm_offset_t new, old;
224 struct obreak_args /* {
230 printf(ARGS(brk, "%p"), (void *)(uintptr_t)args->dsend);
232 old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
233 new = (vm_offset_t)args->dsend;
234 tmp.nsize = (char *)new;
235 if (((caddr_t)new > vm->vm_daddr) && !sys_obreak(td, &tmp))
236 td->td_retval[0] = (long)new;
238 td->td_retval[0] = (long)old;
243 #if defined(__i386__)
244 /* XXX: what about amd64/linux32? */
247 linux_uselib(struct thread *td, struct linux_uselib_args *args)
254 unsigned long file_offset;
255 unsigned long bss_size;
258 int error, locked, writecount;
260 LCONVPATHEXIST(td, args->library, &library);
264 printf(ARGS(uselib, "%s"), library);
271 NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | AUDITVNODE1,
272 UIO_SYSSPACE, library, td);
279 NDFREE(&ni, NDF_ONLY_PNBUF);
282 * From here on down, we have a locked vnode that must be unlocked.
283 * XXX: The code below largely duplicates exec_check_permissions().
288 error = VOP_GET_WRITECOUNT(vp, &writecount);
291 if (writecount != 0) {
297 error = VOP_GETATTR(vp, &attr, td->td_ucred);
301 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
302 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
303 /* EACCESS is what exec(2) returns. */
309 if (attr.va_size == 0) {
314 /* Can we access it? */
315 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
320 * XXX: This should use vn_open() so that it is properly authorized,
321 * and to reduce code redundancy all over the place here.
322 * XXX: Not really, it duplicates far more of exec_check_permissions()
326 error = mac_vnode_check_open(td->td_ucred, vp, VREAD);
330 error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL);
334 /* Pull in executable header into exec_map */
335 error = vm_mmap(exec_map, (vm_offset_t *)&a_out, PAGE_SIZE,
336 VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 0);
340 /* Is it a Linux binary ? */
341 if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
347 * While we are here, we should REALLY do some more checks
350 /* Set file/virtual offset based on a.out variant. */
351 switch ((int)(a_out->a_magic & 0xffff)) {
352 case 0413: /* ZMAGIC */
355 case 0314: /* QMAGIC */
363 bss_size = round_page(a_out->a_bss);
365 /* Check various fields in header for validity/bounds. */
366 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
371 /* text + data can't exceed file size */
372 if (a_out->a_data + a_out->a_text > attr.va_size) {
378 * text/data/bss must not exceed limits
379 * XXX - this is not complete. it should check current usage PLUS
380 * the resources needed by this library.
382 PROC_LOCK(td->td_proc);
383 if (a_out->a_text > maxtsiz ||
384 a_out->a_data + bss_size > lim_cur(td->td_proc, RLIMIT_DATA) ||
385 racct_set(td->td_proc, RACCT_DATA, a_out->a_data +
387 PROC_UNLOCK(td->td_proc);
391 PROC_UNLOCK(td->td_proc);
394 * Prevent more writers.
395 * XXX: Note that if any of the VM operations fail below we don't
401 * Lock no longer needed
407 * Check if file_offset page aligned. Currently we cannot handle
408 * misalinged file offsets, and so we read in the entire image
411 if (file_offset & PAGE_MASK) {
413 printf("uselib: Non page aligned binary %lu\n", file_offset);
415 /* Map text+data read/write/execute */
417 /* a_entry is the load address and is page aligned */
418 vmaddr = trunc_page(a_out->a_entry);
420 /* get anon user mapping, read+write+execute */
421 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
422 &vmaddr, a_out->a_text + a_out->a_data, 0, VMFS_NO_SPACE,
423 VM_PROT_ALL, VM_PROT_ALL, 0);
427 error = vn_rdwr(UIO_READ, vp, (void *)vmaddr, file_offset,
428 a_out->a_text + a_out->a_data, UIO_USERSPACE, 0,
429 td->td_ucred, NOCRED, &aresid, td);
438 printf("uselib: Page aligned binary %lu\n", file_offset);
441 * for QMAGIC, a_entry is 20 bytes beyond the load address
442 * to skip the executable header
444 vmaddr = trunc_page(a_out->a_entry);
447 * Map it all into the process's space as a single
448 * copy-on-write "data" segment.
450 error = vm_mmap(&td->td_proc->p_vmspace->vm_map, &vmaddr,
451 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
452 MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset);
457 printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long *)vmaddr)[0],
458 ((long *)vmaddr)[1]);
461 /* Calculate BSS start address */
462 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
465 /* allocate some 'anon' space */
466 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
467 &vmaddr, bss_size, 0, VMFS_NO_SPACE, VM_PROT_ALL,
474 /* Unlock vnode if needed */
478 /* Release the temporary mapping. */
480 kmap_free_wakeup(exec_map, (vm_offset_t)a_out, PAGE_SIZE);
485 #endif /* __i386__ */
488 linux_select(struct thread *td, struct linux_select_args *args)
491 struct timeval tv0, tv1, utv, *tvp;
496 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
497 (void *)args->readfds, (void *)args->writefds,
498 (void *)args->exceptfds, (void *)args->timeout);
502 * Store current time for computation of the amount of
506 if ((error = copyin(args->timeout, <v, sizeof(ltv))))
508 utv.tv_sec = ltv.tv_sec;
509 utv.tv_usec = ltv.tv_usec;
512 printf(LMSG("incoming timeout (%jd/%ld)"),
513 (intmax_t)utv.tv_sec, utv.tv_usec);
516 if (itimerfix(&utv)) {
518 * The timeval was invalid. Convert it to something
519 * valid that will act as it does under Linux.
521 utv.tv_sec += utv.tv_usec / 1000000;
522 utv.tv_usec %= 1000000;
523 if (utv.tv_usec < 0) {
525 utv.tv_usec += 1000000;
535 error = kern_select(td, args->nfds, args->readfds, args->writefds,
536 args->exceptfds, tvp, LINUX_NFDBITS);
540 printf(LMSG("real select returns %d"), error);
546 if (td->td_retval[0]) {
548 * Compute how much time was left of the timeout,
549 * by subtracting the current time and the time
550 * before we started the call, and subtracting
551 * that result from the user-supplied value.
554 timevalsub(&tv1, &tv0);
555 timevalsub(&utv, &tv1);
562 printf(LMSG("outgoing timeout (%jd/%ld)"),
563 (intmax_t)utv.tv_sec, utv.tv_usec);
565 ltv.tv_sec = utv.tv_sec;
566 ltv.tv_usec = utv.tv_usec;
567 if ((error = copyout(<v, args->timeout, sizeof(ltv))))
574 printf(LMSG("select_out -> %d"), error);
580 linux_mremap(struct thread *td, struct linux_mremap_args *args)
582 struct munmap_args /* {
590 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
591 (void *)(uintptr_t)args->addr,
592 (unsigned long)args->old_len,
593 (unsigned long)args->new_len,
594 (unsigned long)args->flags);
597 if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) {
598 td->td_retval[0] = 0;
603 * Check for the page alignment.
604 * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK.
606 if (args->addr & PAGE_MASK) {
607 td->td_retval[0] = 0;
611 args->new_len = round_page(args->new_len);
612 args->old_len = round_page(args->old_len);
614 if (args->new_len > args->old_len) {
615 td->td_retval[0] = 0;
619 if (args->new_len < args->old_len) {
621 (caddr_t)((uintptr_t)args->addr + args->new_len);
622 bsd_args.len = args->old_len - args->new_len;
623 error = sys_munmap(td, &bsd_args);
626 td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
630 #define LINUX_MS_ASYNC 0x0001
631 #define LINUX_MS_INVALIDATE 0x0002
632 #define LINUX_MS_SYNC 0x0004
635 linux_msync(struct thread *td, struct linux_msync_args *args)
637 struct msync_args bsd_args;
639 bsd_args.addr = (caddr_t)(uintptr_t)args->addr;
640 bsd_args.len = (uintptr_t)args->len;
641 bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
643 return (sys_msync(td, &bsd_args));
647 linux_time(struct thread *td, struct linux_time_args *args)
655 printf(ARGS(time, "*"));
660 if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
662 td->td_retval[0] = tm;
666 struct l_times_argv {
669 l_clock_t tms_cutime;
670 l_clock_t tms_cstime;
675 * Glibc versions prior to 2.2.1 always use hard-coded CLK_TCK value.
676 * Since 2.2.1 Glibc uses value exported from kernel via AT_CLKTCK
677 * auxiliary vector entry.
681 #define CONVOTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
682 #define CONVNTCK(r) (r.tv_sec * stclohz + r.tv_usec / (1000000 / stclohz))
684 #define CONVTCK(r) (linux_kernver(td) >= LINUX_KERNVER_2004000 ? \
685 CONVNTCK(r) : CONVOTCK(r))
688 linux_times(struct thread *td, struct linux_times_args *args)
690 struct timeval tv, utime, stime, cutime, cstime;
691 struct l_times_argv tms;
697 printf(ARGS(times, "*"));
700 if (args->buf != NULL) {
704 calcru(p, &utime, &stime);
706 calccru(p, &cutime, &cstime);
709 tms.tms_utime = CONVTCK(utime);
710 tms.tms_stime = CONVTCK(stime);
712 tms.tms_cutime = CONVTCK(cutime);
713 tms.tms_cstime = CONVTCK(cstime);
715 if ((error = copyout(&tms, args->buf, sizeof(tms))))
720 td->td_retval[0] = (int)CONVTCK(tv);
725 linux_newuname(struct thread *td, struct linux_newuname_args *args)
727 struct l_new_utsname utsname;
728 char osname[LINUX_MAX_UTSNAME];
729 char osrelease[LINUX_MAX_UTSNAME];
733 if (ldebug(newuname))
734 printf(ARGS(newuname, "*"));
737 linux_get_osname(td, osname);
738 linux_get_osrelease(td, osrelease);
740 bzero(&utsname, sizeof(utsname));
741 strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
742 getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
743 getcreddomainname(td->td_ucred, utsname.domainname, LINUX_MAX_UTSNAME);
744 strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
745 strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
746 for (p = utsname.version; *p != '\0'; ++p)
751 strlcpy(utsname.machine, linux_kplatform, LINUX_MAX_UTSNAME);
753 return (copyout(&utsname, args->buf, sizeof(utsname)));
762 linux_utime(struct thread *td, struct linux_utime_args *args)
764 struct timeval tv[2], *tvp;
765 struct l_utimbuf lut;
769 LCONVPATHEXIST(td, args->fname, &fname);
773 printf(ARGS(utime, "%s, *"), fname);
777 if ((error = copyin(args->times, &lut, sizeof lut))) {
781 tv[0].tv_sec = lut.l_actime;
783 tv[1].tv_sec = lut.l_modtime;
789 error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
795 linux_utimes(struct thread *td, struct linux_utimes_args *args)
798 struct timeval tv[2], *tvp = NULL;
802 LCONVPATHEXIST(td, args->fname, &fname);
806 printf(ARGS(utimes, "%s, *"), fname);
809 if (args->tptr != NULL) {
810 if ((error = copyin(args->tptr, ltv, sizeof ltv))) {
814 tv[0].tv_sec = ltv[0].tv_sec;
815 tv[0].tv_usec = ltv[0].tv_usec;
816 tv[1].tv_sec = ltv[1].tv_sec;
817 tv[1].tv_usec = ltv[1].tv_usec;
821 error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
827 linux_futimesat(struct thread *td, struct linux_futimesat_args *args)
830 struct timeval tv[2], *tvp = NULL;
834 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
835 LCONVPATHEXIST_AT(td, args->filename, &fname, dfd);
838 if (ldebug(futimesat))
839 printf(ARGS(futimesat, "%s, *"), fname);
842 if (args->utimes != NULL) {
843 if ((error = copyin(args->utimes, ltv, sizeof ltv))) {
847 tv[0].tv_sec = ltv[0].tv_sec;
848 tv[0].tv_usec = ltv[0].tv_usec;
849 tv[1].tv_sec = ltv[1].tv_sec;
850 tv[1].tv_usec = ltv[1].tv_usec;
854 error = kern_utimesat(td, dfd, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
860 linux_common_wait(struct thread *td, int pid, int *status,
861 int options, struct rusage *ru)
865 error = kern_wait(td, pid, &tmpstat, options, ru);
871 if (WIFSIGNALED(tmpstat))
872 tmpstat = (tmpstat & 0xffffff80) |
873 bsd_to_linux_signal(WTERMSIG(tmpstat));
874 else if (WIFSTOPPED(tmpstat))
875 tmpstat = (tmpstat & 0xffff00ff) |
876 (bsd_to_linux_signal(WSTOPSIG(tmpstat)) << 8);
877 else if (WIFCONTINUED(tmpstat))
879 error = copyout(&tmpstat, status, sizeof(int));
885 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
887 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
889 struct linux_wait4_args wait4_args;
893 printf(ARGS(waitpid, "%d, %p, %d"),
894 args->pid, (void *)args->status, args->options);
897 wait4_args.pid = args->pid;
898 wait4_args.status = args->status;
899 wait4_args.options = args->options;
900 wait4_args.rusage = NULL;
902 return (linux_wait4(td, &wait4_args));
904 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
907 linux_wait4(struct thread *td, struct linux_wait4_args *args)
910 struct rusage ru, *rup;
914 printf(ARGS(wait4, "%d, %p, %d, %p"),
915 args->pid, (void *)args->status, args->options,
916 (void *)args->rusage);
918 if (args->options & ~(LINUX_WUNTRACED | LINUX_WNOHANG |
919 LINUX_WCONTINUED | __WCLONE | __WNOTHREAD | __WALL))
923 linux_to_bsd_waitopts(args->options, &options);
925 if (args->rusage != NULL)
929 error = linux_common_wait(td, args->pid, args->status, options, rup);
932 if (args->rusage != NULL)
933 error = linux_copyout_rusage(&ru, args->rusage);
938 linux_waitid(struct thread *td, struct linux_waitid_args *args)
940 int status, options, sig;
941 struct __wrusage wru;
949 linux_to_bsd_waitopts(args->options, &options);
951 if (options & ~(WNOHANG | WNOWAIT | WEXITED | WUNTRACED | WCONTINUED))
953 if (!(options & (WEXITED | WUNTRACED | WCONTINUED)))
956 switch (args->idtype) {
974 error = kern_wait6(td, idtype, args->id, &status, options,
978 if (args->rusage != NULL) {
979 error = linux_copyout_rusage(&wru.wru_children,
984 if (args->info != NULL) {
986 if (td->td_retval[0] == 0)
987 bzero(&lsi, sizeof(lsi));
989 sig = bsd_to_linux_signal(siginfo.si_signo);
990 siginfo_to_lsiginfo(&siginfo, &lsi, sig);
992 error = copyout(&lsi, args->info, sizeof(lsi));
994 td->td_retval[0] = 0;
1000 linux_mknod(struct thread *td, struct linux_mknod_args *args)
1005 LCONVPATHCREAT(td, args->path, &path);
1009 printf(ARGS(mknod, "%s, %d, %ju"), path, args->mode,
1010 (uintmax_t)args->dev);
1013 switch (args->mode & S_IFMT) {
1016 error = kern_mkfifo(td, path, UIO_SYSSPACE, args->mode);
1021 error = kern_mknod(td, path, UIO_SYSSPACE, args->mode,
1030 args->mode |= S_IFREG;
1033 error = kern_open(td, path, UIO_SYSSPACE,
1034 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1036 kern_close(td, td->td_retval[0]);
1048 linux_mknodat(struct thread *td, struct linux_mknodat_args *args)
1053 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1054 LCONVPATHCREAT_AT(td, args->filename, &path, dfd);
1057 if (ldebug(mknodat))
1058 printf(ARGS(mknodat, "%s, %d, %d"), path, args->mode, args->dev);
1061 switch (args->mode & S_IFMT) {
1064 error = kern_mkfifoat(td, dfd, path, UIO_SYSSPACE, args->mode);
1069 error = kern_mknodat(td, dfd, path, UIO_SYSSPACE, args->mode,
1078 args->mode |= S_IFREG;
1081 error = kern_openat(td, dfd, path, UIO_SYSSPACE,
1082 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1084 kern_close(td, td->td_retval[0]);
1096 * UGH! This is just about the dumbest idea I've ever heard!!
1099 linux_personality(struct thread *td, struct linux_personality_args *args)
1102 if (ldebug(personality))
1103 printf(ARGS(personality, "%lu"), (unsigned long)args->per);
1108 /* Yes Jim, it's still a Linux... */
1109 td->td_retval[0] = 0;
1113 struct l_itimerval {
1114 l_timeval it_interval;
1118 #define B2L_ITIMERVAL(bip, lip) \
1119 (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec; \
1120 (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec; \
1121 (bip)->it_value.tv_sec = (lip)->it_value.tv_sec; \
1122 (bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
1125 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
1128 struct l_itimerval ls;
1129 struct itimerval aitv, oitv;
1132 if (ldebug(setitimer))
1133 printf(ARGS(setitimer, "%p, %p"),
1134 (void *)uap->itv, (void *)uap->oitv);
1137 if (uap->itv == NULL) {
1138 uap->itv = uap->oitv;
1139 return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
1142 error = copyin(uap->itv, &ls, sizeof(ls));
1145 B2L_ITIMERVAL(&aitv, &ls);
1147 if (ldebug(setitimer)) {
1148 printf("setitimer: value: sec: %jd, usec: %ld\n",
1149 (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec);
1150 printf("setitimer: interval: sec: %jd, usec: %ld\n",
1151 (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec);
1154 error = kern_setitimer(td, uap->which, &aitv, &oitv);
1155 if (error != 0 || uap->oitv == NULL)
1157 B2L_ITIMERVAL(&ls, &oitv);
1159 return (copyout(&ls, uap->oitv, sizeof(ls)));
1163 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
1166 struct l_itimerval ls;
1167 struct itimerval aitv;
1170 if (ldebug(getitimer))
1171 printf(ARGS(getitimer, "%p"), (void *)uap->itv);
1173 error = kern_getitimer(td, uap->which, &aitv);
1176 B2L_ITIMERVAL(&ls, &aitv);
1177 return (copyout(&ls, uap->itv, sizeof(ls)));
1180 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1182 linux_nice(struct thread *td, struct linux_nice_args *args)
1184 struct setpriority_args bsd_args;
1186 bsd_args.which = PRIO_PROCESS;
1187 bsd_args.who = 0; /* current process */
1188 bsd_args.prio = args->inc;
1189 return (sys_setpriority(td, &bsd_args));
1191 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1194 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
1196 struct ucred *newcred, *oldcred;
1197 l_gid_t *linux_gidset;
1202 ngrp = args->gidsetsize;
1203 if (ngrp < 0 || ngrp >= ngroups_max + 1)
1205 linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_LINUX, M_WAITOK);
1206 error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
1212 oldcred = crcopysafe(p, newcred);
1215 * cr_groups[0] holds egid. Setting the whole set from
1216 * the supplied set will cause egid to be changed too.
1217 * Keep cr_groups[0] unchanged to prevent that.
1220 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0) {
1227 newcred->cr_ngroups = ngrp + 1;
1229 bsd_gidset = newcred->cr_groups;
1232 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1236 newcred->cr_ngroups = 1;
1239 p->p_ucred = newcred;
1244 free(linux_gidset, M_LINUX);
1249 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1252 l_gid_t *linux_gidset;
1254 int bsd_gidsetsz, ngrp, error;
1256 cred = td->td_ucred;
1257 bsd_gidset = cred->cr_groups;
1258 bsd_gidsetsz = cred->cr_ngroups - 1;
1261 * cr_groups[0] holds egid. Returning the whole set
1262 * here will cause a duplicate. Exclude cr_groups[0]
1266 if ((ngrp = args->gidsetsize) == 0) {
1267 td->td_retval[0] = bsd_gidsetsz;
1271 if (ngrp < bsd_gidsetsz)
1275 linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset),
1277 while (ngrp < bsd_gidsetsz) {
1278 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1282 error = copyout(linux_gidset, args->grouplist, ngrp * sizeof(l_gid_t));
1283 free(linux_gidset, M_LINUX);
1287 td->td_retval[0] = ngrp;
1292 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1294 struct rlimit bsd_rlim;
1295 struct l_rlimit rlim;
1300 if (ldebug(setrlimit))
1301 printf(ARGS(setrlimit, "%d, %p"),
1302 args->resource, (void *)args->rlim);
1305 if (args->resource >= LINUX_RLIM_NLIMITS)
1308 which = linux_to_bsd_resource[args->resource];
1312 error = copyin(args->rlim, &rlim, sizeof(rlim));
1316 bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1317 bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1318 return (kern_setrlimit(td, which, &bsd_rlim));
1321 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1323 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1325 struct l_rlimit rlim;
1326 struct proc *p = td->td_proc;
1327 struct rlimit bsd_rlim;
1331 if (ldebug(old_getrlimit))
1332 printf(ARGS(old_getrlimit, "%d, %p"),
1333 args->resource, (void *)args->rlim);
1336 if (args->resource >= LINUX_RLIM_NLIMITS)
1339 which = linux_to_bsd_resource[args->resource];
1344 lim_rlimit(p, which, &bsd_rlim);
1347 #ifdef COMPAT_LINUX32
1348 rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1349 if (rlim.rlim_cur == UINT_MAX)
1350 rlim.rlim_cur = INT_MAX;
1351 rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1352 if (rlim.rlim_max == UINT_MAX)
1353 rlim.rlim_max = INT_MAX;
1355 rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1356 if (rlim.rlim_cur == ULONG_MAX)
1357 rlim.rlim_cur = LONG_MAX;
1358 rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1359 if (rlim.rlim_max == ULONG_MAX)
1360 rlim.rlim_max = LONG_MAX;
1362 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1364 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1367 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1369 struct l_rlimit rlim;
1370 struct proc *p = td->td_proc;
1371 struct rlimit bsd_rlim;
1375 if (ldebug(getrlimit))
1376 printf(ARGS(getrlimit, "%d, %p"),
1377 args->resource, (void *)args->rlim);
1380 if (args->resource >= LINUX_RLIM_NLIMITS)
1383 which = linux_to_bsd_resource[args->resource];
1388 lim_rlimit(p, which, &bsd_rlim);
1391 rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1392 rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1393 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1397 linux_sched_setscheduler(struct thread *td,
1398 struct linux_sched_setscheduler_args *args)
1400 struct sched_param sched_param;
1405 if (ldebug(sched_setscheduler))
1406 printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1407 args->pid, args->policy, (const void *)args->param);
1410 switch (args->policy) {
1411 case LINUX_SCHED_OTHER:
1412 policy = SCHED_OTHER;
1414 case LINUX_SCHED_FIFO:
1415 policy = SCHED_FIFO;
1417 case LINUX_SCHED_RR:
1424 error = copyin(args->param, &sched_param, sizeof(sched_param));
1428 tdt = linux_tdfind(td, args->pid, -1);
1432 error = kern_sched_setscheduler(td, tdt, policy, &sched_param);
1433 PROC_UNLOCK(tdt->td_proc);
1438 linux_sched_getscheduler(struct thread *td,
1439 struct linux_sched_getscheduler_args *args)
1445 if (ldebug(sched_getscheduler))
1446 printf(ARGS(sched_getscheduler, "%d"), args->pid);
1449 tdt = linux_tdfind(td, args->pid, -1);
1453 error = kern_sched_getscheduler(td, tdt, &policy);
1454 PROC_UNLOCK(tdt->td_proc);
1458 td->td_retval[0] = LINUX_SCHED_OTHER;
1461 td->td_retval[0] = LINUX_SCHED_FIFO;
1464 td->td_retval[0] = LINUX_SCHED_RR;
1471 linux_sched_get_priority_max(struct thread *td,
1472 struct linux_sched_get_priority_max_args *args)
1474 struct sched_get_priority_max_args bsd;
1477 if (ldebug(sched_get_priority_max))
1478 printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1481 switch (args->policy) {
1482 case LINUX_SCHED_OTHER:
1483 bsd.policy = SCHED_OTHER;
1485 case LINUX_SCHED_FIFO:
1486 bsd.policy = SCHED_FIFO;
1488 case LINUX_SCHED_RR:
1489 bsd.policy = SCHED_RR;
1494 return (sys_sched_get_priority_max(td, &bsd));
1498 linux_sched_get_priority_min(struct thread *td,
1499 struct linux_sched_get_priority_min_args *args)
1501 struct sched_get_priority_min_args bsd;
1504 if (ldebug(sched_get_priority_min))
1505 printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1508 switch (args->policy) {
1509 case LINUX_SCHED_OTHER:
1510 bsd.policy = SCHED_OTHER;
1512 case LINUX_SCHED_FIFO:
1513 bsd.policy = SCHED_FIFO;
1515 case LINUX_SCHED_RR:
1516 bsd.policy = SCHED_RR;
1521 return (sys_sched_get_priority_min(td, &bsd));
1524 #define REBOOT_CAD_ON 0x89abcdef
1525 #define REBOOT_CAD_OFF 0
1526 #define REBOOT_HALT 0xcdef0123
1527 #define REBOOT_RESTART 0x01234567
1528 #define REBOOT_RESTART2 0xA1B2C3D4
1529 #define REBOOT_POWEROFF 0x4321FEDC
1530 #define REBOOT_MAGIC1 0xfee1dead
1531 #define REBOOT_MAGIC2 0x28121969
1532 #define REBOOT_MAGIC2A 0x05121996
1533 #define REBOOT_MAGIC2B 0x16041998
1536 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1538 struct reboot_args bsd_args;
1542 printf(ARGS(reboot, "0x%x"), args->cmd);
1545 if (args->magic1 != REBOOT_MAGIC1)
1548 switch (args->magic2) {
1550 case REBOOT_MAGIC2A:
1551 case REBOOT_MAGIC2B:
1557 switch (args->cmd) {
1559 case REBOOT_CAD_OFF:
1560 return (priv_check(td, PRIV_REBOOT));
1562 bsd_args.opt = RB_HALT;
1564 case REBOOT_RESTART:
1565 case REBOOT_RESTART2:
1568 case REBOOT_POWEROFF:
1569 bsd_args.opt = RB_POWEROFF;
1574 return (sys_reboot(td, &bsd_args));
1579 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1580 * td->td_retval[1] when COMPAT_43 is defined. This clobbers registers that
1581 * are assumed to be preserved. The following lightweight syscalls fixes
1582 * this. See also linux_getgid16() and linux_getuid16() in linux_uid16.c
1584 * linux_getpid() - MP SAFE
1585 * linux_getgid() - MP SAFE
1586 * linux_getuid() - MP SAFE
1590 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1595 printf(ARGS(getpid, ""));
1597 td->td_retval[0] = td->td_proc->p_pid;
1603 linux_gettid(struct thread *td, struct linux_gettid_args *args)
1605 struct linux_emuldata *em;
1609 printf(ARGS(gettid, ""));
1613 KASSERT(em != NULL, ("gettid: emuldata not found.\n"));
1615 td->td_retval[0] = em->em_tid;
1622 linux_getppid(struct thread *td, struct linux_getppid_args *args)
1626 if (ldebug(getppid))
1627 printf(ARGS(getppid, ""));
1630 PROC_LOCK(td->td_proc);
1631 td->td_retval[0] = td->td_proc->p_pptr->p_pid;
1632 PROC_UNLOCK(td->td_proc);
1637 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1642 printf(ARGS(getgid, ""));
1645 td->td_retval[0] = td->td_ucred->cr_rgid;
1650 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1655 printf(ARGS(getuid, ""));
1658 td->td_retval[0] = td->td_ucred->cr_ruid;
1664 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1666 struct getsid_args bsd;
1670 printf(ARGS(getsid, "%i"), args->pid);
1673 bsd.pid = args->pid;
1674 return (sys_getsid(td, &bsd));
1678 linux_nosys(struct thread *td, struct nosys_args *ignore)
1685 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1687 struct getpriority_args bsd_args;
1691 if (ldebug(getpriority))
1692 printf(ARGS(getpriority, "%i, %i"), args->which, args->who);
1695 bsd_args.which = args->which;
1696 bsd_args.who = args->who;
1697 error = sys_getpriority(td, &bsd_args);
1698 td->td_retval[0] = 20 - td->td_retval[0];
1703 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1708 if (ldebug(sethostname))
1709 printf(ARGS(sethostname, "*, %i"), args->len);
1713 name[1] = KERN_HOSTNAME;
1714 return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1719 linux_setdomainname(struct thread *td, struct linux_setdomainname_args *args)
1724 if (ldebug(setdomainname))
1725 printf(ARGS(setdomainname, "*, %i"), args->len);
1729 name[1] = KERN_NISDOMAINNAME;
1730 return (userland_sysctl(td, name, 2, 0, 0, 0, args->name,
1735 linux_exit_group(struct thread *td, struct linux_exit_group_args *args)
1739 if (ldebug(exit_group))
1740 printf(ARGS(exit_group, "%i"), args->error_code);
1743 LINUX_CTR2(exit_group, "thread(%d) (%d)", td->td_tid,
1747 * XXX: we should send a signal to the parent if
1748 * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?)
1749 * as it doesnt occur often.
1751 exit1(td, W_EXITCODE(args->error_code, 0));
1755 #define _LINUX_CAPABILITY_VERSION 0x19980330
1757 struct l_user_cap_header {
1762 struct l_user_cap_data {
1769 linux_capget(struct thread *td, struct linux_capget_args *args)
1771 struct l_user_cap_header luch;
1772 struct l_user_cap_data lucd;
1775 if (args->hdrp == NULL)
1778 error = copyin(args->hdrp, &luch, sizeof(luch));
1782 if (luch.version != _LINUX_CAPABILITY_VERSION) {
1783 luch.version = _LINUX_CAPABILITY_VERSION;
1784 error = copyout(&luch, args->hdrp, sizeof(luch));
1795 * The current implementation doesn't support setting
1796 * a capability (it's essentially a stub) so indicate
1797 * that no capabilities are currently set or available
1800 bzero (&lucd, sizeof(lucd));
1801 error = copyout(&lucd, args->datap, sizeof(lucd));
1808 linux_capset(struct thread *td, struct linux_capset_args *args)
1810 struct l_user_cap_header luch;
1811 struct l_user_cap_data lucd;
1814 if (args->hdrp == NULL || args->datap == NULL)
1817 error = copyin(args->hdrp, &luch, sizeof(luch));
1821 if (luch.version != _LINUX_CAPABILITY_VERSION) {
1822 luch.version = _LINUX_CAPABILITY_VERSION;
1823 error = copyout(&luch, args->hdrp, sizeof(luch));
1832 error = copyin(args->datap, &lucd, sizeof(lucd));
1836 /* We currently don't support setting any capabilities. */
1837 if (lucd.effective || lucd.permitted || lucd.inheritable) {
1839 "capset effective=0x%x, permitted=0x%x, "
1840 "inheritable=0x%x is not implemented",
1841 (int)lucd.effective, (int)lucd.permitted,
1842 (int)lucd.inheritable);
1850 linux_prctl(struct thread *td, struct linux_prctl_args *args)
1852 int error = 0, max_size;
1853 struct proc *p = td->td_proc;
1854 char comm[LINUX_MAX_COMM_LEN];
1855 struct linux_emuldata *em;
1860 printf(ARGS(prctl, "%d, %ju, %ju, %ju, %ju"), args->option,
1861 (uintmax_t)args->arg2, (uintmax_t)args->arg3,
1862 (uintmax_t)args->arg4, (uintmax_t)args->arg5);
1865 switch (args->option) {
1866 case LINUX_PR_SET_PDEATHSIG:
1867 if (!LINUX_SIG_VALID(args->arg2))
1870 KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1871 em->pdeath_signal = args->arg2;
1873 case LINUX_PR_GET_PDEATHSIG:
1875 KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1876 pdeath_signal = em->pdeath_signal;
1877 error = copyout(&pdeath_signal,
1878 (void *)(register_t)args->arg2,
1879 sizeof(pdeath_signal));
1881 case LINUX_PR_GET_KEEPCAPS:
1883 * Indicate that we always clear the effective and
1884 * permitted capability sets when the user id becomes
1885 * non-zero (actually the capability sets are simply
1886 * always zero in the current implementation).
1888 td->td_retval[0] = 0;
1890 case LINUX_PR_SET_KEEPCAPS:
1892 * Ignore requests to keep the effective and permitted
1893 * capability sets when the user id becomes non-zero.
1896 case LINUX_PR_SET_NAME:
1898 * To be on the safe side we need to make sure to not
1899 * overflow the size a linux program expects. We already
1900 * do this here in the copyin, so that we don't need to
1903 max_size = MIN(sizeof(comm), sizeof(p->p_comm));
1904 error = copyinstr((void *)(register_t)args->arg2, comm,
1907 /* Linux silently truncates the name if it is too long. */
1908 if (error == ENAMETOOLONG) {
1910 * XXX: copyinstr() isn't documented to populate the
1911 * array completely, so do a copyin() to be on the
1912 * safe side. This should be changed in case
1913 * copyinstr() is changed to guarantee this.
1915 error = copyin((void *)(register_t)args->arg2, comm,
1917 comm[max_size - 1] = '\0';
1923 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
1926 case LINUX_PR_GET_NAME:
1928 strlcpy(comm, p->p_comm, sizeof(comm));
1930 error = copyout(comm, (void *)(register_t)args->arg2,
1942 linux_sched_setparam(struct thread *td,
1943 struct linux_sched_setparam_args *uap)
1945 struct sched_param sched_param;
1950 if (ldebug(sched_setparam))
1951 printf(ARGS(sched_setparam, "%d, *"), uap->pid);
1954 error = copyin(uap->param, &sched_param, sizeof(sched_param));
1958 tdt = linux_tdfind(td, uap->pid, -1);
1962 error = kern_sched_setparam(td, tdt, &sched_param);
1963 PROC_UNLOCK(tdt->td_proc);
1968 linux_sched_getparam(struct thread *td,
1969 struct linux_sched_getparam_args *uap)
1971 struct sched_param sched_param;
1976 if (ldebug(sched_getparam))
1977 printf(ARGS(sched_getparam, "%d, *"), uap->pid);
1980 tdt = linux_tdfind(td, uap->pid, -1);
1984 error = kern_sched_getparam(td, tdt, &sched_param);
1985 PROC_UNLOCK(tdt->td_proc);
1987 error = copyout(&sched_param, uap->param,
1988 sizeof(sched_param));
1993 * Get affinity of a process.
1996 linux_sched_getaffinity(struct thread *td,
1997 struct linux_sched_getaffinity_args *args)
2001 struct cpuset_getaffinity_args cga;
2004 if (ldebug(sched_getaffinity))
2005 printf(ARGS(sched_getaffinity, "%d, %d, *"), args->pid,
2008 if (args->len < sizeof(cpuset_t))
2011 tdt = linux_tdfind(td, args->pid, -1);
2015 PROC_UNLOCK(tdt->td_proc);
2016 cga.level = CPU_LEVEL_WHICH;
2017 cga.which = CPU_WHICH_TID;
2018 cga.id = tdt->td_tid;
2019 cga.cpusetsize = sizeof(cpuset_t);
2020 cga.mask = (cpuset_t *) args->user_mask_ptr;
2022 if ((error = sys_cpuset_getaffinity(td, &cga)) == 0)
2023 td->td_retval[0] = sizeof(cpuset_t);
2029 * Set affinity of a process.
2032 linux_sched_setaffinity(struct thread *td,
2033 struct linux_sched_setaffinity_args *args)
2035 struct cpuset_setaffinity_args csa;
2039 if (ldebug(sched_setaffinity))
2040 printf(ARGS(sched_setaffinity, "%d, %d, *"), args->pid,
2043 if (args->len < sizeof(cpuset_t))
2046 tdt = linux_tdfind(td, args->pid, -1);
2050 PROC_UNLOCK(tdt->td_proc);
2051 csa.level = CPU_LEVEL_WHICH;
2052 csa.which = CPU_WHICH_TID;
2053 csa.id = tdt->td_tid;
2054 csa.cpusetsize = sizeof(cpuset_t);
2055 csa.mask = (cpuset_t *) args->user_mask_ptr;
2057 return (sys_cpuset_setaffinity(td, &csa));
2060 struct linux_rlimit64 {
2066 linux_prlimit64(struct thread *td, struct linux_prlimit64_args *args)
2068 struct rlimit rlim, nrlim;
2069 struct linux_rlimit64 lrlim;
2076 if (ldebug(prlimit64))
2077 printf(ARGS(prlimit64, "%d, %d, %p, %p"), args->pid,
2078 args->resource, (void *)args->new, (void *)args->old);
2081 if (args->resource >= LINUX_RLIM_NLIMITS)
2084 which = linux_to_bsd_resource[args->resource];
2088 if (args->new != NULL) {
2090 * Note. Unlike FreeBSD where rlim is signed 64-bit Linux
2091 * rlim is unsigned 64-bit. FreeBSD treats negative limits
2092 * as INFINITY so we do not need a conversion even.
2094 error = copyin(args->new, &nrlim, sizeof(nrlim));
2099 flags = PGET_HOLD | PGET_NOTWEXIT;
2100 if (args->new != NULL)
2101 flags |= PGET_CANDEBUG;
2103 flags |= PGET_CANSEE;
2104 error = pget(args->pid, flags, &p);
2108 if (args->old != NULL) {
2110 lim_rlimit(p, which, &rlim);
2112 if (rlim.rlim_cur == RLIM_INFINITY)
2113 lrlim.rlim_cur = LINUX_RLIM_INFINITY;
2115 lrlim.rlim_cur = rlim.rlim_cur;
2116 if (rlim.rlim_max == RLIM_INFINITY)
2117 lrlim.rlim_max = LINUX_RLIM_INFINITY;
2119 lrlim.rlim_max = rlim.rlim_max;
2120 error = copyout(&lrlim, args->old, sizeof(lrlim));
2125 if (args->new != NULL)
2126 error = kern_proc_setrlimit(td, p, which, &nrlim);
2134 linux_pselect6(struct thread *td, struct linux_pselect6_args *args)
2136 struct timeval utv, tv0, tv1, *tvp;
2137 struct l_pselect6arg lpse6;
2138 struct l_timespec lts;
2139 struct timespec uts;
2146 if (args->sig != NULL) {
2147 error = copyin(args->sig, &lpse6, sizeof(lpse6));
2150 if (lpse6.ss_len != sizeof(l_ss))
2152 if (lpse6.ss != 0) {
2153 error = copyin(PTRIN(lpse6.ss), &l_ss,
2157 linux_to_bsd_sigset(&l_ss, &ss);
2163 * Currently glibc changes nanosecond number to microsecond.
2164 * This mean losing precision but for now it is hardly seen.
2166 if (args->tsp != NULL) {
2167 error = copyin(args->tsp, <s, sizeof(lts));
2170 error = linux_to_native_timespec(&uts, <s);
2174 TIMESPEC_TO_TIMEVAL(&utv, &uts);
2175 if (itimerfix(&utv))
2183 error = kern_pselect(td, args->nfds, args->readfds, args->writefds,
2184 args->exceptfds, tvp, ssp, LINUX_NFDBITS);
2186 if (error == 0 && args->tsp != NULL) {
2187 if (td->td_retval[0] != 0) {
2189 * Compute how much time was left of the timeout,
2190 * by subtracting the current time and the time
2191 * before we started the call, and subtracting
2192 * that result from the user-supplied value.
2196 timevalsub(&tv1, &tv0);
2197 timevalsub(&utv, &tv1);
2203 TIMEVAL_TO_TIMESPEC(&utv, &uts);
2205 native_to_linux_timespec(<s, &uts);
2206 error = copyout(<s, args->tsp, sizeof(lts));
2213 linux_ppoll(struct thread *td, struct linux_ppoll_args *args)
2215 struct timespec ts0, ts1;
2216 struct l_timespec lts;
2217 struct timespec uts, *tsp;
2223 if (args->sset != NULL) {
2224 if (args->ssize != sizeof(l_ss))
2226 error = copyin(args->sset, &l_ss, sizeof(l_ss));
2229 linux_to_bsd_sigset(&l_ss, &ss);
2233 if (args->tsp != NULL) {
2234 error = copyin(args->tsp, <s, sizeof(lts));
2237 error = linux_to_native_timespec(&uts, <s);
2246 error = kern_poll(td, args->fds, args->nfds, tsp, ssp);
2248 if (error == 0 && args->tsp != NULL) {
2249 if (td->td_retval[0]) {
2251 timespecsub(&ts1, &ts0);
2252 timespecsub(&uts, &ts1);
2254 timespecclear(&uts);
2256 timespecclear(&uts);
2258 native_to_linux_timespec(<s, &uts);
2259 error = copyout(<s, args->tsp, sizeof(lts));
2265 #if defined(DEBUG) || defined(KTR)
2266 /* XXX: can be removed when every ldebug(...) and KTR stuff are removed. */
2268 u_char linux_debug_map[howmany(LINUX_SYS_MAXSYSCALL, sizeof(u_char))];
2271 linux_debug(int syscall, int toggle, int global)
2275 char c = toggle ? 0 : 0xff;
2277 memset(linux_debug_map, c, sizeof(linux_debug_map));
2280 if (syscall < 0 || syscall >= LINUX_SYS_MAXSYSCALL)
2283 clrbit(linux_debug_map, syscall);
2285 setbit(linux_debug_map, syscall);
2290 * Usage: sysctl linux.debug=<syscall_nr>.<0/1>
2292 * E.g.: sysctl linux.debug=21.0
2294 * As a special case, syscall "all" will apply to all syscalls globally.
2296 #define LINUX_MAX_DEBUGSTR 16
2298 linux_sysctl_debug(SYSCTL_HANDLER_ARGS)
2300 char value[LINUX_MAX_DEBUGSTR], *p;
2301 int error, sysc, toggle;
2305 error = sysctl_handle_string(oidp, value, LINUX_MAX_DEBUGSTR, req);
2306 if (error || req->newptr == NULL)
2308 for (p = value; *p != '\0' && *p != '.'; p++);
2312 sysc = strtol(value, NULL, 0);
2313 toggle = strtol(p, NULL, 0);
2314 if (strcmp(value, "all") == 0)
2316 error = linux_debug(sysc, toggle, global);
2320 #endif /* DEBUG || KTR */
2323 linux_sched_rr_get_interval(struct thread *td,
2324 struct linux_sched_rr_get_interval_args *uap)
2327 struct l_timespec lts;
2332 * According to man in case the invalid pid specified
2333 * EINVAL should be returned.
2338 tdt = linux_tdfind(td, uap->pid, -1);
2342 error = kern_sched_rr_get_interval_td(td, tdt, &ts);
2343 PROC_UNLOCK(tdt->td_proc);
2346 native_to_linux_timespec(<s, &ts);
2347 return (copyout(<s, uap->interval, sizeof(lts)));
2351 * In case when the Linux thread is the initial thread in
2352 * the thread group thread id is equal to the process id.
2353 * Glibc depends on this magic (assert in pthread_getattr_np.c).
2356 linux_tdfind(struct thread *td, lwpid_t tid, pid_t pid)
2358 struct linux_emuldata *em;
2363 if (tid == 0 || tid == td->td_tid) {
2365 PROC_LOCK(tdt->td_proc);
2366 } else if (tid > PID_MAX)
2367 tdt = tdfind(tid, pid);
2370 * Initial thread where the tid equal to the pid.
2374 if (SV_PROC_ABI(p) != SV_ABI_LINUX) {
2376 * p is not a Linuxulator process.
2381 FOREACH_THREAD_IN_PROC(p, tdt) {
2383 if (tid == em->em_tid)
2395 linux_to_bsd_waitopts(int options, int *bsdopts)
2398 if (options & LINUX_WNOHANG)
2399 *bsdopts |= WNOHANG;
2400 if (options & LINUX_WUNTRACED)
2401 *bsdopts |= WUNTRACED;
2402 if (options & LINUX_WEXITED)
2403 *bsdopts |= WEXITED;
2404 if (options & LINUX_WCONTINUED)
2405 *bsdopts |= WCONTINUED;
2406 if (options & LINUX_WNOWAIT)
2407 *bsdopts |= WNOWAIT;
2409 if (options & __WCLONE)
2410 *bsdopts |= WLINUXCLONE;