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>
57 #include <sys/signalvar.h>
59 #include <sys/syscallsubr.h>
60 #include <sys/sysctl.h>
61 #include <sys/sysproto.h>
62 #include <sys/systm.h>
64 #include <sys/vmmeter.h>
65 #include <sys/vnode.h>
67 #include <sys/cpuset.h>
69 #include <security/mac/mac_framework.h>
73 #include <vm/vm_kern.h>
74 #include <vm/vm_map.h>
75 #include <vm/vm_extern.h>
76 #include <vm/vm_object.h>
77 #include <vm/swap_pager.h>
80 #include <machine/../linux32/linux.h>
81 #include <machine/../linux32/linux32_proto.h>
83 #include <machine/../linux/linux.h>
84 #include <machine/../linux/linux_proto.h>
87 #include <compat/linux/linux_file.h>
88 #include <compat/linux/linux_mib.h>
89 #include <compat/linux/linux_signal.h>
90 #include <compat/linux/linux_util.h>
91 #include <compat/linux/linux_sysproto.h>
92 #include <compat/linux/linux_emul.h>
93 #include <compat/linux/linux_misc.h>
95 int stclohz; /* Statistics clock frequency */
97 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
98 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
99 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
100 RLIMIT_MEMLOCK, RLIMIT_AS
104 l_long uptime; /* Seconds since boot */
105 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */
106 #define LINUX_SYSINFO_LOADS_SCALE 65536
107 l_ulong totalram; /* Total usable main memory size */
108 l_ulong freeram; /* Available memory size */
109 l_ulong sharedram; /* Amount of shared memory */
110 l_ulong bufferram; /* Memory used by buffers */
111 l_ulong totalswap; /* Total swap space size */
112 l_ulong freeswap; /* swap space still available */
113 l_ushort procs; /* Number of current processes */
118 char _f[20-2*sizeof(l_long)-sizeof(l_int)]; /* padding */
121 struct l_pselect6arg {
127 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args)
129 struct l_sysinfo sysinfo;
137 sysinfo.uptime = ts.tv_sec;
139 /* Use the information from the mib to get our load averages */
140 for (i = 0; i < 3; i++)
141 sysinfo.loads[i] = averunnable.ldavg[i] *
142 LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale;
144 sysinfo.totalram = physmem * PAGE_SIZE;
145 sysinfo.freeram = sysinfo.totalram - cnt.v_wire_count * PAGE_SIZE;
147 sysinfo.sharedram = 0;
148 mtx_lock(&vm_object_list_mtx);
149 TAILQ_FOREACH(object, &vm_object_list, object_list)
150 if (object->shadow_count > 1)
151 sysinfo.sharedram += object->resident_page_count;
152 mtx_unlock(&vm_object_list_mtx);
154 sysinfo.sharedram *= PAGE_SIZE;
155 sysinfo.bufferram = 0;
157 swap_pager_status(&i, &j);
158 sysinfo.totalswap = i * PAGE_SIZE;
159 sysinfo.freeswap = (i - j) * PAGE_SIZE;
161 sysinfo.procs = nprocs;
163 /* The following are only present in newer Linux kernels. */
164 sysinfo.totalbig = 0;
166 sysinfo.mem_unit = 1;
168 return (copyout(&sysinfo, args->info, sizeof(sysinfo)));
172 linux_alarm(struct thread *td, struct linux_alarm_args *args)
174 struct itimerval it, old_it;
180 printf(ARGS(alarm, "%u"), args->secs);
188 it.it_value.tv_sec = (long) secs;
189 it.it_value.tv_usec = 0;
190 it.it_interval.tv_sec = 0;
191 it.it_interval.tv_usec = 0;
192 error = kern_setitimer(td, ITIMER_REAL, &it, &old_it);
195 if (timevalisset(&old_it.it_value)) {
196 if (old_it.it_value.tv_usec != 0)
197 old_it.it_value.tv_sec++;
198 td->td_retval[0] = old_it.it_value.tv_sec;
204 linux_brk(struct thread *td, struct linux_brk_args *args)
206 struct vmspace *vm = td->td_proc->p_vmspace;
207 vm_offset_t new, old;
208 struct obreak_args /* {
214 printf(ARGS(brk, "%p"), (void *)(uintptr_t)args->dsend);
216 old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
217 new = (vm_offset_t)args->dsend;
218 tmp.nsize = (char *)new;
219 if (((caddr_t)new > vm->vm_daddr) && !sys_obreak(td, &tmp))
220 td->td_retval[0] = (long)new;
222 td->td_retval[0] = (long)old;
227 #if defined(__i386__)
228 /* XXX: what about amd64/linux32? */
231 linux_uselib(struct thread *td, struct linux_uselib_args *args)
238 unsigned long file_offset;
239 unsigned long bss_size;
242 int error, locked, writecount;
244 LCONVPATHEXIST(td, args->library, &library);
248 printf(ARGS(uselib, "%s"), library);
255 NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | AUDITVNODE1,
256 UIO_SYSSPACE, library, td);
263 NDFREE(&ni, NDF_ONLY_PNBUF);
266 * From here on down, we have a locked vnode that must be unlocked.
267 * XXX: The code below largely duplicates exec_check_permissions().
272 error = VOP_GET_WRITECOUNT(vp, &writecount);
275 if (writecount != 0) {
281 error = VOP_GETATTR(vp, &attr, td->td_ucred);
285 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
286 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
287 /* EACCESS is what exec(2) returns. */
293 if (attr.va_size == 0) {
298 /* Can we access it? */
299 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
304 * XXX: This should use vn_open() so that it is properly authorized,
305 * and to reduce code redundancy all over the place here.
306 * XXX: Not really, it duplicates far more of exec_check_permissions()
310 error = mac_vnode_check_open(td->td_ucred, vp, VREAD);
314 error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL);
318 /* Pull in executable header into exec_map */
319 error = vm_mmap(exec_map, (vm_offset_t *)&a_out, PAGE_SIZE,
320 VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 0);
324 /* Is it a Linux binary ? */
325 if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
331 * While we are here, we should REALLY do some more checks
334 /* Set file/virtual offset based on a.out variant. */
335 switch ((int)(a_out->a_magic & 0xffff)) {
336 case 0413: /* ZMAGIC */
339 case 0314: /* QMAGIC */
347 bss_size = round_page(a_out->a_bss);
349 /* Check various fields in header for validity/bounds. */
350 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
355 /* text + data can't exceed file size */
356 if (a_out->a_data + a_out->a_text > attr.va_size) {
362 * text/data/bss must not exceed limits
363 * XXX - this is not complete. it should check current usage PLUS
364 * the resources needed by this library.
366 PROC_LOCK(td->td_proc);
367 if (a_out->a_text > maxtsiz ||
368 a_out->a_data + bss_size > lim_cur(td->td_proc, RLIMIT_DATA) ||
369 racct_set(td->td_proc, RACCT_DATA, a_out->a_data +
371 PROC_UNLOCK(td->td_proc);
375 PROC_UNLOCK(td->td_proc);
378 * Prevent more writers.
379 * XXX: Note that if any of the VM operations fail below we don't
385 * Lock no longer needed
391 * Check if file_offset page aligned. Currently we cannot handle
392 * misalinged file offsets, and so we read in the entire image
395 if (file_offset & PAGE_MASK) {
397 printf("uselib: Non page aligned binary %lu\n", file_offset);
399 /* Map text+data read/write/execute */
401 /* a_entry is the load address and is page aligned */
402 vmaddr = trunc_page(a_out->a_entry);
404 /* get anon user mapping, read+write+execute */
405 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
406 &vmaddr, a_out->a_text + a_out->a_data, 0, VMFS_NO_SPACE,
407 VM_PROT_ALL, VM_PROT_ALL, 0);
411 error = vn_rdwr(UIO_READ, vp, (void *)vmaddr, file_offset,
412 a_out->a_text + a_out->a_data, UIO_USERSPACE, 0,
413 td->td_ucred, NOCRED, &aresid, td);
422 printf("uselib: Page aligned binary %lu\n", file_offset);
425 * for QMAGIC, a_entry is 20 bytes beyond the load address
426 * to skip the executable header
428 vmaddr = trunc_page(a_out->a_entry);
431 * Map it all into the process's space as a single
432 * copy-on-write "data" segment.
434 error = vm_mmap(&td->td_proc->p_vmspace->vm_map, &vmaddr,
435 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
436 MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset);
441 printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long *)vmaddr)[0],
442 ((long *)vmaddr)[1]);
445 /* Calculate BSS start address */
446 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
449 /* allocate some 'anon' space */
450 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
451 &vmaddr, bss_size, 0, VMFS_NO_SPACE, VM_PROT_ALL,
458 /* Unlock vnode if needed */
462 /* Release the temporary mapping. */
464 kmap_free_wakeup(exec_map, (vm_offset_t)a_out, PAGE_SIZE);
469 #endif /* __i386__ */
472 linux_select(struct thread *td, struct linux_select_args *args)
475 struct timeval tv0, tv1, utv, *tvp;
480 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
481 (void *)args->readfds, (void *)args->writefds,
482 (void *)args->exceptfds, (void *)args->timeout);
486 * Store current time for computation of the amount of
490 if ((error = copyin(args->timeout, <v, sizeof(ltv))))
492 utv.tv_sec = ltv.tv_sec;
493 utv.tv_usec = ltv.tv_usec;
496 printf(LMSG("incoming timeout (%jd/%ld)"),
497 (intmax_t)utv.tv_sec, utv.tv_usec);
500 if (itimerfix(&utv)) {
502 * The timeval was invalid. Convert it to something
503 * valid that will act as it does under Linux.
505 utv.tv_sec += utv.tv_usec / 1000000;
506 utv.tv_usec %= 1000000;
507 if (utv.tv_usec < 0) {
509 utv.tv_usec += 1000000;
519 error = kern_select(td, args->nfds, args->readfds, args->writefds,
520 args->exceptfds, tvp, sizeof(l_int) * 8);
524 printf(LMSG("real select returns %d"), error);
530 if (td->td_retval[0]) {
532 * Compute how much time was left of the timeout,
533 * by subtracting the current time and the time
534 * before we started the call, and subtracting
535 * that result from the user-supplied value.
538 timevalsub(&tv1, &tv0);
539 timevalsub(&utv, &tv1);
546 printf(LMSG("outgoing timeout (%jd/%ld)"),
547 (intmax_t)utv.tv_sec, utv.tv_usec);
549 ltv.tv_sec = utv.tv_sec;
550 ltv.tv_usec = utv.tv_usec;
551 if ((error = copyout(<v, args->timeout, sizeof(ltv))))
558 printf(LMSG("select_out -> %d"), error);
564 linux_mremap(struct thread *td, struct linux_mremap_args *args)
566 struct munmap_args /* {
574 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
575 (void *)(uintptr_t)args->addr,
576 (unsigned long)args->old_len,
577 (unsigned long)args->new_len,
578 (unsigned long)args->flags);
581 if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) {
582 td->td_retval[0] = 0;
587 * Check for the page alignment.
588 * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK.
590 if (args->addr & PAGE_MASK) {
591 td->td_retval[0] = 0;
595 args->new_len = round_page(args->new_len);
596 args->old_len = round_page(args->old_len);
598 if (args->new_len > args->old_len) {
599 td->td_retval[0] = 0;
603 if (args->new_len < args->old_len) {
605 (caddr_t)((uintptr_t)args->addr + args->new_len);
606 bsd_args.len = args->old_len - args->new_len;
607 error = sys_munmap(td, &bsd_args);
610 td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
614 #define LINUX_MS_ASYNC 0x0001
615 #define LINUX_MS_INVALIDATE 0x0002
616 #define LINUX_MS_SYNC 0x0004
619 linux_msync(struct thread *td, struct linux_msync_args *args)
621 struct msync_args bsd_args;
623 bsd_args.addr = (caddr_t)(uintptr_t)args->addr;
624 bsd_args.len = (uintptr_t)args->len;
625 bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
627 return (sys_msync(td, &bsd_args));
631 linux_time(struct thread *td, struct linux_time_args *args)
639 printf(ARGS(time, "*"));
644 if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
646 td->td_retval[0] = tm;
650 struct l_times_argv {
653 l_clock_t tms_cutime;
654 l_clock_t tms_cstime;
659 * Glibc versions prior to 2.2.1 always use hard-coded CLK_TCK value.
660 * Since 2.2.1 Glibc uses value exported from kernel via AT_CLKTCK
661 * auxiliary vector entry.
665 #define CONVOTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
666 #define CONVNTCK(r) (r.tv_sec * stclohz + r.tv_usec / (1000000 / stclohz))
668 #define CONVTCK(r) (linux_kernver(td) >= LINUX_KERNVER_2004000 ? \
669 CONVNTCK(r) : CONVOTCK(r))
672 linux_times(struct thread *td, struct linux_times_args *args)
674 struct timeval tv, utime, stime, cutime, cstime;
675 struct l_times_argv tms;
681 printf(ARGS(times, "*"));
684 if (args->buf != NULL) {
688 calcru(p, &utime, &stime);
690 calccru(p, &cutime, &cstime);
693 tms.tms_utime = CONVTCK(utime);
694 tms.tms_stime = CONVTCK(stime);
696 tms.tms_cutime = CONVTCK(cutime);
697 tms.tms_cstime = CONVTCK(cstime);
699 if ((error = copyout(&tms, args->buf, sizeof(tms))))
704 td->td_retval[0] = (int)CONVTCK(tv);
709 linux_newuname(struct thread *td, struct linux_newuname_args *args)
711 struct l_new_utsname utsname;
712 char osname[LINUX_MAX_UTSNAME];
713 char osrelease[LINUX_MAX_UTSNAME];
717 if (ldebug(newuname))
718 printf(ARGS(newuname, "*"));
721 linux_get_osname(td, osname);
722 linux_get_osrelease(td, osrelease);
724 bzero(&utsname, sizeof(utsname));
725 strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
726 getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
727 getcreddomainname(td->td_ucred, utsname.domainname, LINUX_MAX_UTSNAME);
728 strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
729 strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
730 for (p = utsname.version; *p != '\0'; ++p)
735 strlcpy(utsname.machine, linux_kplatform, LINUX_MAX_UTSNAME);
737 return (copyout(&utsname, args->buf, sizeof(utsname)));
746 linux_utime(struct thread *td, struct linux_utime_args *args)
748 struct timeval tv[2], *tvp;
749 struct l_utimbuf lut;
753 LCONVPATHEXIST(td, args->fname, &fname);
757 printf(ARGS(utime, "%s, *"), fname);
761 if ((error = copyin(args->times, &lut, sizeof lut))) {
765 tv[0].tv_sec = lut.l_actime;
767 tv[1].tv_sec = lut.l_modtime;
773 error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
779 linux_utimes(struct thread *td, struct linux_utimes_args *args)
782 struct timeval tv[2], *tvp = NULL;
786 LCONVPATHEXIST(td, args->fname, &fname);
790 printf(ARGS(utimes, "%s, *"), fname);
793 if (args->tptr != NULL) {
794 if ((error = copyin(args->tptr, ltv, sizeof ltv))) {
798 tv[0].tv_sec = ltv[0].tv_sec;
799 tv[0].tv_usec = ltv[0].tv_usec;
800 tv[1].tv_sec = ltv[1].tv_sec;
801 tv[1].tv_usec = ltv[1].tv_usec;
805 error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
811 linux_futimesat(struct thread *td, struct linux_futimesat_args *args)
814 struct timeval tv[2], *tvp = NULL;
818 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
819 LCONVPATHEXIST_AT(td, args->filename, &fname, dfd);
822 if (ldebug(futimesat))
823 printf(ARGS(futimesat, "%s, *"), fname);
826 if (args->utimes != NULL) {
827 if ((error = copyin(args->utimes, ltv, sizeof ltv))) {
831 tv[0].tv_sec = ltv[0].tv_sec;
832 tv[0].tv_usec = ltv[0].tv_usec;
833 tv[1].tv_sec = ltv[1].tv_sec;
834 tv[1].tv_usec = ltv[1].tv_usec;
838 error = kern_utimesat(td, dfd, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
844 linux_common_wait(struct thread *td, int pid, int *status,
845 int options, struct rusage *ru)
849 error = kern_wait(td, pid, &tmpstat, options, ru);
855 if (WIFSIGNALED(tmpstat))
856 tmpstat = (tmpstat & 0xffffff80) |
857 BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
858 else if (WIFSTOPPED(tmpstat))
859 tmpstat = (tmpstat & 0xffff00ff) |
860 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
861 error = copyout(&tmpstat, status, sizeof(int));
867 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
869 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
875 printf(ARGS(waitpid, "%d, %p, %d"),
876 args->pid, (void *)args->status, args->options);
879 * this is necessary because the test in kern_wait doesn't work
880 * because we mess with the options here
882 if (args->options & ~(WUNTRACED | WNOHANG | WCONTINUED | __WCLONE))
885 options = (args->options & (WNOHANG | WUNTRACED));
886 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
887 if (args->options & __WCLONE)
888 options |= WLINUXCLONE;
890 return (linux_common_wait(td, args->pid, args->status, options, NULL));
892 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
895 linux_wait4(struct thread *td, struct linux_wait4_args *args)
898 struct rusage ru, *rup;
902 printf(ARGS(wait4, "%d, %p, %d, %p"),
903 args->pid, (void *)args->status, args->options,
904 (void *)args->rusage);
907 options = (args->options & (WNOHANG | WUNTRACED));
908 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
909 if (args->options & __WCLONE)
910 options |= WLINUXCLONE;
912 if (args->rusage != NULL)
916 error = linux_common_wait(td, args->pid, args->status, options, rup);
919 if (args->rusage != NULL)
920 error = linux_copyout_rusage(&ru, args->rusage);
925 linux_waitid(struct thread *td, struct linux_waitid_args *args)
927 int status, options, sig;
928 struct __wrusage wru;
936 linux_to_bsd_waitopts(args->options, &options);
938 if (options & ~(WNOHANG | WNOWAIT | WEXITED | WUNTRACED | WCONTINUED))
940 if (!(options & (WEXITED | WUNTRACED | WCONTINUED)))
943 switch (args->idtype) {
961 error = kern_wait6(td, idtype, args->id, &status, options,
965 if (args->rusage != NULL) {
966 error = linux_copyout_rusage(&wru.wru_children,
971 if (args->info != NULL) {
973 if (td->td_retval[0] == 0)
974 bzero(&lsi, sizeof(lsi));
976 sig = BSD_TO_LINUX_SIGNAL(siginfo.si_signo);
977 siginfo_to_lsiginfo(&siginfo, &lsi, sig);
979 error = copyout(&lsi, args->info, sizeof(lsi));
981 td->td_retval[0] = 0;
987 linux_mknod(struct thread *td, struct linux_mknod_args *args)
992 LCONVPATHCREAT(td, args->path, &path);
996 printf(ARGS(mknod, "%s, %d, %d"), path, args->mode, args->dev);
999 switch (args->mode & S_IFMT) {
1002 error = kern_mkfifo(td, path, UIO_SYSSPACE, args->mode);
1007 error = kern_mknod(td, path, UIO_SYSSPACE, args->mode,
1016 args->mode |= S_IFREG;
1019 error = kern_open(td, path, UIO_SYSSPACE,
1020 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1022 kern_close(td, td->td_retval[0]);
1034 linux_mknodat(struct thread *td, struct linux_mknodat_args *args)
1039 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1040 LCONVPATHCREAT_AT(td, args->filename, &path, dfd);
1043 if (ldebug(mknodat))
1044 printf(ARGS(mknodat, "%s, %d, %d"), path, args->mode, args->dev);
1047 switch (args->mode & S_IFMT) {
1050 error = kern_mkfifoat(td, dfd, path, UIO_SYSSPACE, args->mode);
1055 error = kern_mknodat(td, dfd, path, UIO_SYSSPACE, args->mode,
1064 args->mode |= S_IFREG;
1067 error = kern_openat(td, dfd, path, UIO_SYSSPACE,
1068 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1070 kern_close(td, td->td_retval[0]);
1082 * UGH! This is just about the dumbest idea I've ever heard!!
1085 linux_personality(struct thread *td, struct linux_personality_args *args)
1088 if (ldebug(personality))
1089 printf(ARGS(personality, "%lu"), (unsigned long)args->per);
1094 /* Yes Jim, it's still a Linux... */
1095 td->td_retval[0] = 0;
1099 struct l_itimerval {
1100 l_timeval it_interval;
1104 #define B2L_ITIMERVAL(bip, lip) \
1105 (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec; \
1106 (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec; \
1107 (bip)->it_value.tv_sec = (lip)->it_value.tv_sec; \
1108 (bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
1111 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
1114 struct l_itimerval ls;
1115 struct itimerval aitv, oitv;
1118 if (ldebug(setitimer))
1119 printf(ARGS(setitimer, "%p, %p"),
1120 (void *)uap->itv, (void *)uap->oitv);
1123 if (uap->itv == NULL) {
1124 uap->itv = uap->oitv;
1125 return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
1128 error = copyin(uap->itv, &ls, sizeof(ls));
1131 B2L_ITIMERVAL(&aitv, &ls);
1133 if (ldebug(setitimer)) {
1134 printf("setitimer: value: sec: %jd, usec: %ld\n",
1135 (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec);
1136 printf("setitimer: interval: sec: %jd, usec: %ld\n",
1137 (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec);
1140 error = kern_setitimer(td, uap->which, &aitv, &oitv);
1141 if (error != 0 || uap->oitv == NULL)
1143 B2L_ITIMERVAL(&ls, &oitv);
1145 return (copyout(&ls, uap->oitv, sizeof(ls)));
1149 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
1152 struct l_itimerval ls;
1153 struct itimerval aitv;
1156 if (ldebug(getitimer))
1157 printf(ARGS(getitimer, "%p"), (void *)uap->itv);
1159 error = kern_getitimer(td, uap->which, &aitv);
1162 B2L_ITIMERVAL(&ls, &aitv);
1163 return (copyout(&ls, uap->itv, sizeof(ls)));
1166 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1168 linux_nice(struct thread *td, struct linux_nice_args *args)
1170 struct setpriority_args bsd_args;
1172 bsd_args.which = PRIO_PROCESS;
1173 bsd_args.who = 0; /* current process */
1174 bsd_args.prio = args->inc;
1175 return (sys_setpriority(td, &bsd_args));
1177 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1180 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
1182 struct ucred *newcred, *oldcred;
1183 l_gid_t *linux_gidset;
1188 ngrp = args->gidsetsize;
1189 if (ngrp < 0 || ngrp >= ngroups_max + 1)
1191 linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_TEMP, M_WAITOK);
1192 error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
1198 oldcred = crcopysafe(p, newcred);
1201 * cr_groups[0] holds egid. Setting the whole set from
1202 * the supplied set will cause egid to be changed too.
1203 * Keep cr_groups[0] unchanged to prevent that.
1206 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0) {
1213 newcred->cr_ngroups = ngrp + 1;
1215 bsd_gidset = newcred->cr_groups;
1218 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1222 newcred->cr_ngroups = 1;
1225 p->p_ucred = newcred;
1230 free(linux_gidset, M_TEMP);
1235 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1238 l_gid_t *linux_gidset;
1240 int bsd_gidsetsz, ngrp, error;
1242 cred = td->td_ucred;
1243 bsd_gidset = cred->cr_groups;
1244 bsd_gidsetsz = cred->cr_ngroups - 1;
1247 * cr_groups[0] holds egid. Returning the whole set
1248 * here will cause a duplicate. Exclude cr_groups[0]
1252 if ((ngrp = args->gidsetsize) == 0) {
1253 td->td_retval[0] = bsd_gidsetsz;
1257 if (ngrp < bsd_gidsetsz)
1261 linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset),
1263 while (ngrp < bsd_gidsetsz) {
1264 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1268 error = copyout(linux_gidset, args->grouplist, ngrp * sizeof(l_gid_t));
1269 free(linux_gidset, M_TEMP);
1273 td->td_retval[0] = ngrp;
1278 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1280 struct rlimit bsd_rlim;
1281 struct l_rlimit rlim;
1286 if (ldebug(setrlimit))
1287 printf(ARGS(setrlimit, "%d, %p"),
1288 args->resource, (void *)args->rlim);
1291 if (args->resource >= LINUX_RLIM_NLIMITS)
1294 which = linux_to_bsd_resource[args->resource];
1298 error = copyin(args->rlim, &rlim, sizeof(rlim));
1302 bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1303 bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1304 return (kern_setrlimit(td, which, &bsd_rlim));
1307 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1309 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1311 struct l_rlimit rlim;
1312 struct proc *p = td->td_proc;
1313 struct rlimit bsd_rlim;
1317 if (ldebug(old_getrlimit))
1318 printf(ARGS(old_getrlimit, "%d, %p"),
1319 args->resource, (void *)args->rlim);
1322 if (args->resource >= LINUX_RLIM_NLIMITS)
1325 which = linux_to_bsd_resource[args->resource];
1330 lim_rlimit(p, which, &bsd_rlim);
1333 #ifdef COMPAT_LINUX32
1334 rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1335 if (rlim.rlim_cur == UINT_MAX)
1336 rlim.rlim_cur = INT_MAX;
1337 rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1338 if (rlim.rlim_max == UINT_MAX)
1339 rlim.rlim_max = INT_MAX;
1341 rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1342 if (rlim.rlim_cur == ULONG_MAX)
1343 rlim.rlim_cur = LONG_MAX;
1344 rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1345 if (rlim.rlim_max == ULONG_MAX)
1346 rlim.rlim_max = LONG_MAX;
1348 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1350 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1353 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1355 struct l_rlimit rlim;
1356 struct proc *p = td->td_proc;
1357 struct rlimit bsd_rlim;
1361 if (ldebug(getrlimit))
1362 printf(ARGS(getrlimit, "%d, %p"),
1363 args->resource, (void *)args->rlim);
1366 if (args->resource >= LINUX_RLIM_NLIMITS)
1369 which = linux_to_bsd_resource[args->resource];
1374 lim_rlimit(p, which, &bsd_rlim);
1377 rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1378 rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1379 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1383 linux_sched_setscheduler(struct thread *td,
1384 struct linux_sched_setscheduler_args *args)
1386 struct sched_param sched_param;
1391 if (ldebug(sched_setscheduler))
1392 printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1393 args->pid, args->policy, (const void *)args->param);
1396 switch (args->policy) {
1397 case LINUX_SCHED_OTHER:
1398 policy = SCHED_OTHER;
1400 case LINUX_SCHED_FIFO:
1401 policy = SCHED_FIFO;
1403 case LINUX_SCHED_RR:
1410 error = copyin(args->param, &sched_param, sizeof(sched_param));
1414 tdt = linux_tdfind(td, args->pid, -1);
1418 error = kern_sched_setscheduler(td, tdt, policy, &sched_param);
1419 PROC_UNLOCK(tdt->td_proc);
1424 linux_sched_getscheduler(struct thread *td,
1425 struct linux_sched_getscheduler_args *args)
1431 if (ldebug(sched_getscheduler))
1432 printf(ARGS(sched_getscheduler, "%d"), args->pid);
1435 tdt = linux_tdfind(td, args->pid, -1);
1439 error = kern_sched_getscheduler(td, tdt, &policy);
1440 PROC_UNLOCK(tdt->td_proc);
1444 td->td_retval[0] = LINUX_SCHED_OTHER;
1447 td->td_retval[0] = LINUX_SCHED_FIFO;
1450 td->td_retval[0] = LINUX_SCHED_RR;
1457 linux_sched_get_priority_max(struct thread *td,
1458 struct linux_sched_get_priority_max_args *args)
1460 struct sched_get_priority_max_args bsd;
1463 if (ldebug(sched_get_priority_max))
1464 printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1467 switch (args->policy) {
1468 case LINUX_SCHED_OTHER:
1469 bsd.policy = SCHED_OTHER;
1471 case LINUX_SCHED_FIFO:
1472 bsd.policy = SCHED_FIFO;
1474 case LINUX_SCHED_RR:
1475 bsd.policy = SCHED_RR;
1480 return (sys_sched_get_priority_max(td, &bsd));
1484 linux_sched_get_priority_min(struct thread *td,
1485 struct linux_sched_get_priority_min_args *args)
1487 struct sched_get_priority_min_args bsd;
1490 if (ldebug(sched_get_priority_min))
1491 printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1494 switch (args->policy) {
1495 case LINUX_SCHED_OTHER:
1496 bsd.policy = SCHED_OTHER;
1498 case LINUX_SCHED_FIFO:
1499 bsd.policy = SCHED_FIFO;
1501 case LINUX_SCHED_RR:
1502 bsd.policy = SCHED_RR;
1507 return (sys_sched_get_priority_min(td, &bsd));
1510 #define REBOOT_CAD_ON 0x89abcdef
1511 #define REBOOT_CAD_OFF 0
1512 #define REBOOT_HALT 0xcdef0123
1513 #define REBOOT_RESTART 0x01234567
1514 #define REBOOT_RESTART2 0xA1B2C3D4
1515 #define REBOOT_POWEROFF 0x4321FEDC
1516 #define REBOOT_MAGIC1 0xfee1dead
1517 #define REBOOT_MAGIC2 0x28121969
1518 #define REBOOT_MAGIC2A 0x05121996
1519 #define REBOOT_MAGIC2B 0x16041998
1522 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1524 struct reboot_args bsd_args;
1528 printf(ARGS(reboot, "0x%x"), args->cmd);
1531 if (args->magic1 != REBOOT_MAGIC1)
1534 switch (args->magic2) {
1536 case REBOOT_MAGIC2A:
1537 case REBOOT_MAGIC2B:
1543 switch (args->cmd) {
1545 case REBOOT_CAD_OFF:
1546 return (priv_check(td, PRIV_REBOOT));
1548 bsd_args.opt = RB_HALT;
1550 case REBOOT_RESTART:
1551 case REBOOT_RESTART2:
1554 case REBOOT_POWEROFF:
1555 bsd_args.opt = RB_POWEROFF;
1560 return (sys_reboot(td, &bsd_args));
1565 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1566 * td->td_retval[1] when COMPAT_43 is defined. This clobbers registers that
1567 * are assumed to be preserved. The following lightweight syscalls fixes
1568 * this. See also linux_getgid16() and linux_getuid16() in linux_uid16.c
1570 * linux_getpid() - MP SAFE
1571 * linux_getgid() - MP SAFE
1572 * linux_getuid() - MP SAFE
1576 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1581 printf(ARGS(getpid, ""));
1583 td->td_retval[0] = td->td_proc->p_pid;
1589 linux_gettid(struct thread *td, struct linux_gettid_args *args)
1591 struct linux_emuldata *em;
1595 printf(ARGS(gettid, ""));
1599 KASSERT(em != NULL, ("gettid: emuldata not found.\n"));
1601 td->td_retval[0] = em->em_tid;
1608 linux_getppid(struct thread *td, struct linux_getppid_args *args)
1612 if (ldebug(getppid))
1613 printf(ARGS(getppid, ""));
1616 PROC_LOCK(td->td_proc);
1617 td->td_retval[0] = td->td_proc->p_pptr->p_pid;
1618 PROC_UNLOCK(td->td_proc);
1623 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1628 printf(ARGS(getgid, ""));
1631 td->td_retval[0] = td->td_ucred->cr_rgid;
1636 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1641 printf(ARGS(getuid, ""));
1644 td->td_retval[0] = td->td_ucred->cr_ruid;
1650 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1652 struct getsid_args bsd;
1656 printf(ARGS(getsid, "%i"), args->pid);
1659 bsd.pid = args->pid;
1660 return (sys_getsid(td, &bsd));
1664 linux_nosys(struct thread *td, struct nosys_args *ignore)
1671 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1673 struct getpriority_args bsd_args;
1677 if (ldebug(getpriority))
1678 printf(ARGS(getpriority, "%i, %i"), args->which, args->who);
1681 bsd_args.which = args->which;
1682 bsd_args.who = args->who;
1683 error = sys_getpriority(td, &bsd_args);
1684 td->td_retval[0] = 20 - td->td_retval[0];
1689 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1694 if (ldebug(sethostname))
1695 printf(ARGS(sethostname, "*, %i"), args->len);
1699 name[1] = KERN_HOSTNAME;
1700 return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1705 linux_setdomainname(struct thread *td, struct linux_setdomainname_args *args)
1710 if (ldebug(setdomainname))
1711 printf(ARGS(setdomainname, "*, %i"), args->len);
1715 name[1] = KERN_NISDOMAINNAME;
1716 return (userland_sysctl(td, name, 2, 0, 0, 0, args->name,
1721 linux_exit_group(struct thread *td, struct linux_exit_group_args *args)
1725 if (ldebug(exit_group))
1726 printf(ARGS(exit_group, "%i"), args->error_code);
1729 LINUX_CTR2(exit_group, "thread(%d) (%d)", td->td_tid,
1733 * XXX: we should send a signal to the parent if
1734 * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?)
1735 * as it doesnt occur often.
1737 exit1(td, W_EXITCODE(args->error_code, 0));
1741 #define _LINUX_CAPABILITY_VERSION 0x19980330
1743 struct l_user_cap_header {
1748 struct l_user_cap_data {
1755 linux_capget(struct thread *td, struct linux_capget_args *args)
1757 struct l_user_cap_header luch;
1758 struct l_user_cap_data lucd;
1761 if (args->hdrp == NULL)
1764 error = copyin(args->hdrp, &luch, sizeof(luch));
1768 if (luch.version != _LINUX_CAPABILITY_VERSION) {
1769 luch.version = _LINUX_CAPABILITY_VERSION;
1770 error = copyout(&luch, args->hdrp, sizeof(luch));
1781 * The current implementation doesn't support setting
1782 * a capability (it's essentially a stub) so indicate
1783 * that no capabilities are currently set or available
1786 bzero (&lucd, sizeof(lucd));
1787 error = copyout(&lucd, args->datap, sizeof(lucd));
1794 linux_capset(struct thread *td, struct linux_capset_args *args)
1796 struct l_user_cap_header luch;
1797 struct l_user_cap_data lucd;
1800 if (args->hdrp == NULL || args->datap == NULL)
1803 error = copyin(args->hdrp, &luch, sizeof(luch));
1807 if (luch.version != _LINUX_CAPABILITY_VERSION) {
1808 luch.version = _LINUX_CAPABILITY_VERSION;
1809 error = copyout(&luch, args->hdrp, sizeof(luch));
1818 error = copyin(args->datap, &lucd, sizeof(lucd));
1822 /* We currently don't support setting any capabilities. */
1823 if (lucd.effective || lucd.permitted || lucd.inheritable) {
1825 "capset effective=0x%x, permitted=0x%x, "
1826 "inheritable=0x%x is not implemented",
1827 (int)lucd.effective, (int)lucd.permitted,
1828 (int)lucd.inheritable);
1836 linux_prctl(struct thread *td, struct linux_prctl_args *args)
1838 int error = 0, max_size;
1839 struct proc *p = td->td_proc;
1840 char comm[LINUX_MAX_COMM_LEN];
1841 struct linux_emuldata *em;
1846 printf(ARGS(prctl, "%d, %d, %d, %d, %d"), args->option,
1847 args->arg2, args->arg3, args->arg4, args->arg5);
1850 switch (args->option) {
1851 case LINUX_PR_SET_PDEATHSIG:
1852 if (!LINUX_SIG_VALID(args->arg2))
1855 KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1856 em->pdeath_signal = args->arg2;
1858 case LINUX_PR_GET_PDEATHSIG:
1860 KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1861 pdeath_signal = em->pdeath_signal;
1862 error = copyout(&pdeath_signal,
1863 (void *)(register_t)args->arg2,
1864 sizeof(pdeath_signal));
1866 case LINUX_PR_GET_KEEPCAPS:
1868 * Indicate that we always clear the effective and
1869 * permitted capability sets when the user id becomes
1870 * non-zero (actually the capability sets are simply
1871 * always zero in the current implementation).
1873 td->td_retval[0] = 0;
1875 case LINUX_PR_SET_KEEPCAPS:
1877 * Ignore requests to keep the effective and permitted
1878 * capability sets when the user id becomes non-zero.
1881 case LINUX_PR_SET_NAME:
1883 * To be on the safe side we need to make sure to not
1884 * overflow the size a linux program expects. We already
1885 * do this here in the copyin, so that we don't need to
1888 max_size = MIN(sizeof(comm), sizeof(p->p_comm));
1889 error = copyinstr((void *)(register_t)args->arg2, comm,
1892 /* Linux silently truncates the name if it is too long. */
1893 if (error == ENAMETOOLONG) {
1895 * XXX: copyinstr() isn't documented to populate the
1896 * array completely, so do a copyin() to be on the
1897 * safe side. This should be changed in case
1898 * copyinstr() is changed to guarantee this.
1900 error = copyin((void *)(register_t)args->arg2, comm,
1902 comm[max_size - 1] = '\0';
1908 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
1911 case LINUX_PR_GET_NAME:
1913 strlcpy(comm, p->p_comm, sizeof(comm));
1915 error = copyout(comm, (void *)(register_t)args->arg2,
1927 linux_sched_setparam(struct thread *td,
1928 struct linux_sched_setparam_args *uap)
1930 struct sched_param sched_param;
1935 if (ldebug(sched_setparam))
1936 printf(ARGS(sched_setparam, "%d, *"), uap->pid);
1939 error = copyin(uap->param, &sched_param, sizeof(sched_param));
1943 tdt = linux_tdfind(td, uap->pid, -1);
1947 error = kern_sched_setparam(td, tdt, &sched_param);
1948 PROC_UNLOCK(tdt->td_proc);
1953 linux_sched_getparam(struct thread *td,
1954 struct linux_sched_getparam_args *uap)
1956 struct sched_param sched_param;
1961 if (ldebug(sched_getparam))
1962 printf(ARGS(sched_getparam, "%d, *"), uap->pid);
1965 tdt = linux_tdfind(td, uap->pid, -1);
1969 error = kern_sched_getparam(td, tdt, &sched_param);
1970 PROC_UNLOCK(tdt->td_proc);
1972 error = copyout(&sched_param, uap->param,
1973 sizeof(sched_param));
1978 * Get affinity of a process.
1981 linux_sched_getaffinity(struct thread *td,
1982 struct linux_sched_getaffinity_args *args)
1986 struct cpuset_getaffinity_args cga;
1989 if (ldebug(sched_getaffinity))
1990 printf(ARGS(sched_getaffinity, "%d, %d, *"), args->pid,
1993 if (args->len < sizeof(cpuset_t))
1996 tdt = linux_tdfind(td, args->pid, -1);
2000 PROC_UNLOCK(tdt->td_proc);
2001 cga.level = CPU_LEVEL_WHICH;
2002 cga.which = CPU_WHICH_TID;
2003 cga.id = tdt->td_tid;
2004 cga.cpusetsize = sizeof(cpuset_t);
2005 cga.mask = (cpuset_t *) args->user_mask_ptr;
2007 if ((error = sys_cpuset_getaffinity(td, &cga)) == 0)
2008 td->td_retval[0] = sizeof(cpuset_t);
2014 * Set affinity of a process.
2017 linux_sched_setaffinity(struct thread *td,
2018 struct linux_sched_setaffinity_args *args)
2020 struct cpuset_setaffinity_args csa;
2024 if (ldebug(sched_setaffinity))
2025 printf(ARGS(sched_setaffinity, "%d, %d, *"), args->pid,
2028 if (args->len < sizeof(cpuset_t))
2031 tdt = linux_tdfind(td, args->pid, -1);
2035 PROC_UNLOCK(tdt->td_proc);
2036 csa.level = CPU_LEVEL_WHICH;
2037 csa.which = CPU_WHICH_TID;
2038 csa.id = tdt->td_tid;
2039 csa.cpusetsize = sizeof(cpuset_t);
2040 csa.mask = (cpuset_t *) args->user_mask_ptr;
2042 return (sys_cpuset_setaffinity(td, &csa));
2045 struct linux_rlimit64 {
2051 linux_prlimit64(struct thread *td, struct linux_prlimit64_args *args)
2053 struct rlimit rlim, nrlim;
2054 struct linux_rlimit64 lrlim;
2061 if (ldebug(prlimit64))
2062 printf(ARGS(prlimit64, "%d, %d, %p, %p"), args->pid,
2063 args->resource, (void *)args->new, (void *)args->old);
2066 if (args->resource >= LINUX_RLIM_NLIMITS)
2069 which = linux_to_bsd_resource[args->resource];
2073 if (args->new != NULL) {
2075 * Note. Unlike FreeBSD where rlim is signed 64-bit Linux
2076 * rlim is unsigned 64-bit. FreeBSD treats negative limits
2077 * as INFINITY so we do not need a conversion even.
2079 error = copyin(args->new, &nrlim, sizeof(nrlim));
2084 flags = PGET_HOLD | PGET_NOTWEXIT;
2085 if (args->new != NULL)
2086 flags |= PGET_CANDEBUG;
2088 flags |= PGET_CANSEE;
2089 error = pget(args->pid, flags, &p);
2093 if (args->old != NULL) {
2095 lim_rlimit(p, which, &rlim);
2097 if (rlim.rlim_cur == RLIM_INFINITY)
2098 lrlim.rlim_cur = LINUX_RLIM_INFINITY;
2100 lrlim.rlim_cur = rlim.rlim_cur;
2101 if (rlim.rlim_max == RLIM_INFINITY)
2102 lrlim.rlim_max = LINUX_RLIM_INFINITY;
2104 lrlim.rlim_max = rlim.rlim_max;
2105 error = copyout(&lrlim, args->old, sizeof(lrlim));
2110 if (args->new != NULL)
2111 error = kern_proc_setrlimit(td, p, which, &nrlim);
2119 linux_pselect6(struct thread *td, struct linux_pselect6_args *args)
2121 struct timeval utv, tv0, tv1, *tvp;
2122 struct l_pselect6arg lpse6;
2123 struct l_timespec lts;
2124 struct timespec uts;
2131 if (args->sig != NULL) {
2132 error = copyin(args->sig, &lpse6, sizeof(lpse6));
2135 if (lpse6.ss_len != sizeof(l_ss))
2137 if (lpse6.ss != 0) {
2138 error = copyin(PTRIN(lpse6.ss), &l_ss,
2142 linux_to_bsd_sigset(&l_ss, &ss);
2148 * Currently glibc changes nanosecond number to microsecond.
2149 * This mean losing precision but for now it is hardly seen.
2151 if (args->tsp != NULL) {
2152 error = copyin(args->tsp, <s, sizeof(lts));
2155 uts.tv_sec = lts.tv_sec;
2156 uts.tv_nsec = lts.tv_nsec;
2158 TIMESPEC_TO_TIMEVAL(&utv, &uts);
2159 if (itimerfix(&utv))
2167 error = kern_pselect(td, args->nfds, args->readfds, args->writefds,
2168 args->exceptfds, tvp, ssp, sizeof(l_int) * 8);
2170 if (error == 0 && args->tsp != NULL) {
2171 if (td->td_retval[0] != 0) {
2173 * Compute how much time was left of the timeout,
2174 * by subtracting the current time and the time
2175 * before we started the call, and subtracting
2176 * that result from the user-supplied value.
2180 timevalsub(&tv1, &tv0);
2181 timevalsub(&utv, &tv1);
2187 TIMEVAL_TO_TIMESPEC(&utv, &uts);
2188 lts.tv_sec = uts.tv_sec;
2189 lts.tv_nsec = uts.tv_nsec;
2190 error = copyout(<s, args->tsp, sizeof(lts));
2197 linux_sched_rr_get_interval(struct thread *td,
2198 struct linux_sched_rr_get_interval_args *uap)
2201 struct l_timespec lts;
2206 * According to man in case the invalid pid specified
2207 * EINVAL should be returned.
2212 tdt = linux_tdfind(td, uap->pid, -1);
2216 error = kern_sched_rr_get_interval_td(td, tdt, &ts);
2217 PROC_UNLOCK(tdt->td_proc);
2220 lts.tv_sec = ts.tv_sec;
2221 lts.tv_nsec = ts.tv_nsec;
2222 return (copyout(<s, uap->interval, sizeof(lts)));
2226 * In case when the Linux thread is the initial thread in
2227 * the thread group thread id is equal to the process id.
2228 * Glibc depends on this magic (assert in pthread_getattr_np.c).
2231 linux_tdfind(struct thread *td, lwpid_t tid, pid_t pid)
2233 struct linux_emuldata *em;
2238 if (tid == 0 || tid == td->td_tid) {
2240 PROC_LOCK(tdt->td_proc);
2241 } else if (tid > PID_MAX)
2242 tdt = tdfind(tid, pid);
2245 * Initial thread where the tid equal to the pid.
2249 if (SV_PROC_ABI(p) != SV_ABI_LINUX) {
2251 * p is not a Linuxulator process.
2256 FOREACH_THREAD_IN_PROC(p, tdt) {
2258 if (tid == em->em_tid)
2270 linux_to_bsd_waitopts(int options, int *bsdopts)
2273 if (options & LINUX_WNOHANG)
2274 *bsdopts |= WNOHANG;
2275 if (options & LINUX_WUNTRACED)
2276 *bsdopts |= WUNTRACED;
2277 if (options & LINUX_WEXITED)
2278 *bsdopts |= WEXITED;
2279 if (options & LINUX_WCONTINUED)
2280 *bsdopts |= WCONTINUED;
2281 if (options & LINUX_WNOWAIT)
2282 *bsdopts |= WNOWAIT;
2284 if (options & __WCLONE)
2285 *bsdopts |= WLINUXCLONE;