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"
35 #include <sys/param.h>
36 #include <sys/blist.h>
37 #include <sys/fcntl.h>
39 #include <sys/imgact_aout.h>
42 #include <sys/kernel.h>
43 #include <sys/limits.h>
45 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/mutex.h>
49 #include <sys/namei.h>
52 #include <sys/reboot.h>
53 #include <sys/racct.h>
54 #include <sys/resourcevar.h>
55 #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_dtrace.h>
88 #include <compat/linux/linux_file.h>
89 #include <compat/linux/linux_mib.h>
90 #include <compat/linux/linux_signal.h>
91 #include <compat/linux/linux_timer.h>
92 #include <compat/linux/linux_util.h>
93 #include <compat/linux/linux_sysproto.h>
94 #include <compat/linux/linux_emul.h>
95 #include <compat/linux/linux_misc.h>
98 * Special DTrace provider for the linuxulator.
100 * In this file we define the provider for the entire linuxulator. All
101 * modules (= files of the linuxulator) use it.
103 * We define a different name depending on the emulated bitsize, see
104 * ../../<ARCH>/linux{,32}/linux.h, e.g.:
105 * native bitsize = linuxulator
106 * amd64, 32bit emulation = linuxulator32
108 LIN_SDT_PROVIDER_DEFINE(LINUX_DTRACE);
110 int stclohz; /* Statistics clock frequency */
112 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
113 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
114 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
115 RLIMIT_MEMLOCK, RLIMIT_AS
119 l_long uptime; /* Seconds since boot */
120 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */
121 #define LINUX_SYSINFO_LOADS_SCALE 65536
122 l_ulong totalram; /* Total usable main memory size */
123 l_ulong freeram; /* Available memory size */
124 l_ulong sharedram; /* Amount of shared memory */
125 l_ulong bufferram; /* Memory used by buffers */
126 l_ulong totalswap; /* Total swap space size */
127 l_ulong freeswap; /* swap space still available */
128 l_ushort procs; /* Number of current processes */
133 char _f[20-2*sizeof(l_long)-sizeof(l_int)]; /* padding */
136 struct l_pselect6arg {
141 static int linux_utimensat_nsec_valid(l_long);
145 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args)
147 struct l_sysinfo sysinfo;
152 bzero(&sysinfo, sizeof(sysinfo));
156 sysinfo.uptime = ts.tv_sec;
158 /* Use the information from the mib to get our load averages */
159 for (i = 0; i < 3; i++)
160 sysinfo.loads[i] = averunnable.ldavg[i] *
161 LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale;
163 sysinfo.totalram = physmem * PAGE_SIZE;
164 sysinfo.freeram = sysinfo.totalram - vm_cnt.v_wire_count * PAGE_SIZE;
166 sysinfo.sharedram = 0;
167 mtx_lock(&vm_object_list_mtx);
168 TAILQ_FOREACH(object, &vm_object_list, object_list)
169 if (object->shadow_count > 1)
170 sysinfo.sharedram += object->resident_page_count;
171 mtx_unlock(&vm_object_list_mtx);
173 sysinfo.sharedram *= PAGE_SIZE;
174 sysinfo.bufferram = 0;
176 swap_pager_status(&i, &j);
177 sysinfo.totalswap = i * PAGE_SIZE;
178 sysinfo.freeswap = (i - j) * PAGE_SIZE;
180 sysinfo.procs = nprocs;
182 /* The following are only present in newer Linux kernels. */
183 sysinfo.totalbig = 0;
185 sysinfo.mem_unit = 1;
187 return (copyout(&sysinfo, args->info, sizeof(sysinfo)));
191 linux_alarm(struct thread *td, struct linux_alarm_args *args)
193 struct itimerval it, old_it;
199 printf(ARGS(alarm, "%u"), args->secs);
203 * Linux alarm() is always successful. Limit secs to INT32_MAX / 2
204 * to match kern_setitimer()'s limit to avoid error from it.
206 * XXX. Linux limit secs to INT_MAX on 32 and does not limit on 64-bit
209 if (secs > INT32_MAX / 2)
210 secs = INT32_MAX / 2;
212 it.it_value.tv_sec = secs;
213 it.it_value.tv_usec = 0;
214 timevalclear(&it.it_interval);
215 error = kern_setitimer(td, ITIMER_REAL, &it, &old_it);
216 KASSERT(error == 0, ("kern_setitimer returns %d", error));
218 if ((old_it.it_value.tv_sec == 0 && old_it.it_value.tv_usec > 0) ||
219 old_it.it_value.tv_usec >= 500000)
220 old_it.it_value.tv_sec++;
221 td->td_retval[0] = old_it.it_value.tv_sec;
226 linux_brk(struct thread *td, struct linux_brk_args *args)
228 struct vmspace *vm = td->td_proc->p_vmspace;
229 vm_offset_t new, old;
230 struct obreak_args /* {
236 printf(ARGS(brk, "%p"), (void *)(uintptr_t)args->dsend);
238 old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
239 new = (vm_offset_t)args->dsend;
240 tmp.nsize = (char *)new;
241 if (((caddr_t)new > vm->vm_daddr) && !sys_obreak(td, &tmp))
242 td->td_retval[0] = (long)new;
244 td->td_retval[0] = (long)old;
249 #if defined(__i386__)
250 /* XXX: what about amd64/linux32? */
253 linux_uselib(struct thread *td, struct linux_uselib_args *args)
260 unsigned long file_offset;
261 unsigned long bss_size;
264 int error, locked, writecount;
266 LCONVPATHEXIST(td, args->library, &library);
270 printf(ARGS(uselib, "%s"), library);
277 NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | AUDITVNODE1,
278 UIO_SYSSPACE, library, td);
285 NDFREE(&ni, NDF_ONLY_PNBUF);
288 * From here on down, we have a locked vnode that must be unlocked.
289 * XXX: The code below largely duplicates exec_check_permissions().
294 error = VOP_GET_WRITECOUNT(vp, &writecount);
297 if (writecount != 0) {
303 error = VOP_GETATTR(vp, &attr, td->td_ucred);
307 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
308 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
309 /* EACCESS is what exec(2) returns. */
315 if (attr.va_size == 0) {
320 /* Can we access it? */
321 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
326 * XXX: This should use vn_open() so that it is properly authorized,
327 * and to reduce code redundancy all over the place here.
328 * XXX: Not really, it duplicates far more of exec_check_permissions()
332 error = mac_vnode_check_open(td->td_ucred, vp, VREAD);
336 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.
401 * XXX: Note that if any of the VM operations fail below we don't
407 * Lock no longer needed
413 * Check if file_offset page aligned. Currently we cannot handle
414 * misalinged file offsets, and so we read in the entire image
417 if (file_offset & PAGE_MASK) {
419 printf("uselib: Non page aligned binary %lu\n", file_offset);
421 /* Map text+data read/write/execute */
423 /* a_entry is the load address and is page aligned */
424 vmaddr = trunc_page(a_out->a_entry);
426 /* get anon user mapping, read+write+execute */
427 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
428 &vmaddr, a_out->a_text + a_out->a_data, 0, VMFS_NO_SPACE,
429 VM_PROT_ALL, VM_PROT_ALL, 0);
433 error = vn_rdwr(UIO_READ, vp, (void *)vmaddr, file_offset,
434 a_out->a_text + a_out->a_data, UIO_USERSPACE, 0,
435 td->td_ucred, NOCRED, &aresid, td);
444 printf("uselib: Page aligned binary %lu\n", file_offset);
447 * for QMAGIC, a_entry is 20 bytes beyond the load address
448 * to skip the executable header
450 vmaddr = trunc_page(a_out->a_entry);
453 * Map it all into the process's space as a single
454 * copy-on-write "data" segment.
456 error = vm_mmap(&td->td_proc->p_vmspace->vm_map, &vmaddr,
457 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
458 MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset);
463 printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long *)vmaddr)[0],
464 ((long *)vmaddr)[1]);
467 /* Calculate BSS start address */
468 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
471 /* allocate some 'anon' space */
472 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
473 &vmaddr, bss_size, 0, VMFS_NO_SPACE, VM_PROT_ALL,
480 /* Unlock vnode if needed */
484 /* Release the temporary mapping. */
486 kmap_free_wakeup(exec_map, (vm_offset_t)a_out, PAGE_SIZE);
491 #endif /* __i386__ */
494 linux_select(struct thread *td, struct linux_select_args *args)
497 struct timeval tv0, tv1, utv, *tvp;
502 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
503 (void *)args->readfds, (void *)args->writefds,
504 (void *)args->exceptfds, (void *)args->timeout);
508 * Store current time for computation of the amount of
512 if ((error = copyin(args->timeout, <v, sizeof(ltv))))
514 utv.tv_sec = ltv.tv_sec;
515 utv.tv_usec = ltv.tv_usec;
518 printf(LMSG("incoming timeout (%jd/%ld)"),
519 (intmax_t)utv.tv_sec, utv.tv_usec);
522 if (itimerfix(&utv)) {
524 * The timeval was invalid. Convert it to something
525 * valid that will act as it does under Linux.
527 utv.tv_sec += utv.tv_usec / 1000000;
528 utv.tv_usec %= 1000000;
529 if (utv.tv_usec < 0) {
531 utv.tv_usec += 1000000;
541 error = kern_select(td, args->nfds, args->readfds, args->writefds,
542 args->exceptfds, tvp, LINUX_NFDBITS);
546 printf(LMSG("real select returns %d"), error);
552 if (td->td_retval[0]) {
554 * Compute how much time was left of the timeout,
555 * by subtracting the current time and the time
556 * before we started the call, and subtracting
557 * that result from the user-supplied value.
560 timevalsub(&tv1, &tv0);
561 timevalsub(&utv, &tv1);
568 printf(LMSG("outgoing timeout (%jd/%ld)"),
569 (intmax_t)utv.tv_sec, utv.tv_usec);
571 ltv.tv_sec = utv.tv_sec;
572 ltv.tv_usec = utv.tv_usec;
573 if ((error = copyout(<v, args->timeout, sizeof(ltv))))
580 printf(LMSG("select_out -> %d"), error);
586 linux_mremap(struct thread *td, struct linux_mremap_args *args)
588 struct munmap_args /* {
596 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
597 (void *)(uintptr_t)args->addr,
598 (unsigned long)args->old_len,
599 (unsigned long)args->new_len,
600 (unsigned long)args->flags);
603 if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) {
604 td->td_retval[0] = 0;
609 * Check for the page alignment.
610 * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK.
612 if (args->addr & PAGE_MASK) {
613 td->td_retval[0] = 0;
617 args->new_len = round_page(args->new_len);
618 args->old_len = round_page(args->old_len);
620 if (args->new_len > args->old_len) {
621 td->td_retval[0] = 0;
625 if (args->new_len < args->old_len) {
627 (caddr_t)((uintptr_t)args->addr + args->new_len);
628 bsd_args.len = args->old_len - args->new_len;
629 error = sys_munmap(td, &bsd_args);
632 td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
636 #define LINUX_MS_ASYNC 0x0001
637 #define LINUX_MS_INVALIDATE 0x0002
638 #define LINUX_MS_SYNC 0x0004
641 linux_msync(struct thread *td, struct linux_msync_args *args)
643 struct msync_args bsd_args;
645 bsd_args.addr = (caddr_t)(uintptr_t)args->addr;
646 bsd_args.len = (uintptr_t)args->len;
647 bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
649 return (sys_msync(td, &bsd_args));
653 linux_time(struct thread *td, struct linux_time_args *args)
661 printf(ARGS(time, "*"));
666 if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
668 td->td_retval[0] = tm;
672 struct l_times_argv {
675 l_clock_t tms_cutime;
676 l_clock_t tms_cstime;
681 * Glibc versions prior to 2.2.1 always use hard-coded CLK_TCK value.
682 * Since 2.2.1 Glibc uses value exported from kernel via AT_CLKTCK
683 * auxiliary vector entry.
687 #define CONVOTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
688 #define CONVNTCK(r) (r.tv_sec * stclohz + r.tv_usec / (1000000 / stclohz))
690 #define CONVTCK(r) (linux_kernver(td) >= LINUX_KERNVER_2004000 ? \
691 CONVNTCK(r) : CONVOTCK(r))
694 linux_times(struct thread *td, struct linux_times_args *args)
696 struct timeval tv, utime, stime, cutime, cstime;
697 struct l_times_argv tms;
703 printf(ARGS(times, "*"));
706 if (args->buf != NULL) {
710 calcru(p, &utime, &stime);
712 calccru(p, &cutime, &cstime);
715 tms.tms_utime = CONVTCK(utime);
716 tms.tms_stime = CONVTCK(stime);
718 tms.tms_cutime = CONVTCK(cutime);
719 tms.tms_cstime = CONVTCK(cstime);
721 if ((error = copyout(&tms, args->buf, sizeof(tms))))
726 td->td_retval[0] = (int)CONVTCK(tv);
731 linux_newuname(struct thread *td, struct linux_newuname_args *args)
733 struct l_new_utsname utsname;
734 char osname[LINUX_MAX_UTSNAME];
735 char osrelease[LINUX_MAX_UTSNAME];
739 if (ldebug(newuname))
740 printf(ARGS(newuname, "*"));
743 linux_get_osname(td, osname);
744 linux_get_osrelease(td, osrelease);
746 bzero(&utsname, sizeof(utsname));
747 strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
748 getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
749 getcreddomainname(td->td_ucred, utsname.domainname, LINUX_MAX_UTSNAME);
750 strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
751 strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
752 for (p = utsname.version; *p != '\0'; ++p)
757 strlcpy(utsname.machine, linux_kplatform, LINUX_MAX_UTSNAME);
759 return (copyout(&utsname, args->buf, sizeof(utsname)));
768 linux_utime(struct thread *td, struct linux_utime_args *args)
770 struct timeval tv[2], *tvp;
771 struct l_utimbuf lut;
775 LCONVPATHEXIST(td, args->fname, &fname);
779 printf(ARGS(utime, "%s, *"), fname);
783 if ((error = copyin(args->times, &lut, sizeof lut))) {
787 tv[0].tv_sec = lut.l_actime;
789 tv[1].tv_sec = lut.l_modtime;
795 error = kern_utimesat(td, AT_FDCWD, fname, UIO_SYSSPACE, tvp,
802 linux_utimes(struct thread *td, struct linux_utimes_args *args)
805 struct timeval tv[2], *tvp = NULL;
809 LCONVPATHEXIST(td, args->fname, &fname);
813 printf(ARGS(utimes, "%s, *"), fname);
816 if (args->tptr != NULL) {
817 if ((error = copyin(args->tptr, ltv, sizeof ltv))) {
821 tv[0].tv_sec = ltv[0].tv_sec;
822 tv[0].tv_usec = ltv[0].tv_usec;
823 tv[1].tv_sec = ltv[1].tv_sec;
824 tv[1].tv_usec = ltv[1].tv_usec;
828 error = kern_utimesat(td, AT_FDCWD, fname, UIO_SYSSPACE,
835 linux_utimensat_nsec_valid(l_long nsec)
838 if (nsec == LINUX_UTIME_OMIT || nsec == LINUX_UTIME_NOW)
840 if (nsec >= 0 && nsec <= 999999999)
846 linux_utimensat(struct thread *td, struct linux_utimensat_args *args)
848 struct l_timespec l_times[2];
849 struct timespec times[2], *timesp = NULL;
851 int error, dfd, flags = 0;
853 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
856 if (ldebug(utimensat))
857 printf(ARGS(utimensat, "%d, *"), dfd);
860 if (args->flags & ~LINUX_AT_SYMLINK_NOFOLLOW)
863 if (args->times != NULL) {
864 error = copyin(args->times, l_times, sizeof(l_times));
868 if (linux_utimensat_nsec_valid(l_times[0].tv_nsec) != 0 ||
869 linux_utimensat_nsec_valid(l_times[1].tv_nsec) != 0)
872 times[0].tv_sec = l_times[0].tv_sec;
873 switch (l_times[0].tv_nsec)
875 case LINUX_UTIME_OMIT:
876 times[0].tv_nsec = UTIME_OMIT;
878 case LINUX_UTIME_NOW:
879 times[0].tv_nsec = UTIME_NOW;
882 times[0].tv_nsec = l_times[0].tv_nsec;
885 times[1].tv_sec = l_times[1].tv_sec;
886 switch (l_times[1].tv_nsec)
888 case LINUX_UTIME_OMIT:
889 times[1].tv_nsec = UTIME_OMIT;
891 case LINUX_UTIME_NOW:
892 times[1].tv_nsec = UTIME_NOW;
895 times[1].tv_nsec = l_times[1].tv_nsec;
900 /* This breaks POSIX, but is what the Linux kernel does
901 * _on purpose_ (documented in the man page for utimensat(2)),
902 * so we must follow that behaviour. */
903 if (times[0].tv_nsec == UTIME_OMIT &&
904 times[1].tv_nsec == UTIME_OMIT)
908 if (args->pathname != NULL)
909 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
910 else if (args->flags != 0)
913 if (args->flags & LINUX_AT_SYMLINK_NOFOLLOW)
914 flags |= AT_SYMLINK_NOFOLLOW;
917 error = kern_futimens(td, dfd, timesp, UIO_SYSSPACE);
919 error = kern_utimensat(td, dfd, path, UIO_SYSSPACE, timesp,
920 UIO_SYSSPACE, flags);
928 linux_futimesat(struct thread *td, struct linux_futimesat_args *args)
931 struct timeval tv[2], *tvp = NULL;
935 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
936 LCONVPATHEXIST_AT(td, args->filename, &fname, dfd);
939 if (ldebug(futimesat))
940 printf(ARGS(futimesat, "%s, *"), fname);
943 if (args->utimes != NULL) {
944 if ((error = copyin(args->utimes, ltv, sizeof ltv))) {
948 tv[0].tv_sec = ltv[0].tv_sec;
949 tv[0].tv_usec = ltv[0].tv_usec;
950 tv[1].tv_sec = ltv[1].tv_sec;
951 tv[1].tv_usec = ltv[1].tv_usec;
955 error = kern_utimesat(td, dfd, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
961 linux_common_wait(struct thread *td, int pid, int *status,
962 int options, struct rusage *ru)
966 error = kern_wait(td, pid, &tmpstat, options, ru);
972 if (WIFSIGNALED(tmpstat))
973 tmpstat = (tmpstat & 0xffffff80) |
974 bsd_to_linux_signal(WTERMSIG(tmpstat));
975 else if (WIFSTOPPED(tmpstat))
976 tmpstat = (tmpstat & 0xffff00ff) |
977 (bsd_to_linux_signal(WSTOPSIG(tmpstat)) << 8);
978 else if (WIFCONTINUED(tmpstat))
980 error = copyout(&tmpstat, status, sizeof(int));
986 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
988 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
990 struct linux_wait4_args wait4_args;
994 printf(ARGS(waitpid, "%d, %p, %d"),
995 args->pid, (void *)args->status, args->options);
998 wait4_args.pid = args->pid;
999 wait4_args.status = args->status;
1000 wait4_args.options = args->options;
1001 wait4_args.rusage = NULL;
1003 return (linux_wait4(td, &wait4_args));
1005 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1008 linux_wait4(struct thread *td, struct linux_wait4_args *args)
1011 struct rusage ru, *rup;
1015 printf(ARGS(wait4, "%d, %p, %d, %p"),
1016 args->pid, (void *)args->status, args->options,
1017 (void *)args->rusage);
1019 if (args->options & ~(LINUX_WUNTRACED | LINUX_WNOHANG |
1020 LINUX_WCONTINUED | __WCLONE | __WNOTHREAD | __WALL))
1024 linux_to_bsd_waitopts(args->options, &options);
1026 if (args->rusage != NULL)
1030 error = linux_common_wait(td, args->pid, args->status, options, rup);
1033 if (args->rusage != NULL)
1034 error = linux_copyout_rusage(&ru, args->rusage);
1039 linux_waitid(struct thread *td, struct linux_waitid_args *args)
1041 int status, options, sig;
1042 struct __wrusage wru;
1050 linux_to_bsd_waitopts(args->options, &options);
1052 if (options & ~(WNOHANG | WNOWAIT | WEXITED | WUNTRACED | WCONTINUED))
1054 if (!(options & (WEXITED | WUNTRACED | WCONTINUED)))
1057 switch (args->idtype) {
1075 error = kern_wait6(td, idtype, args->id, &status, options,
1079 if (args->rusage != NULL) {
1080 error = linux_copyout_rusage(&wru.wru_children,
1085 if (args->info != NULL) {
1087 if (td->td_retval[0] == 0)
1088 bzero(&lsi, sizeof(lsi));
1090 sig = bsd_to_linux_signal(siginfo.si_signo);
1091 siginfo_to_lsiginfo(&siginfo, &lsi, sig);
1093 error = copyout(&lsi, args->info, sizeof(lsi));
1095 td->td_retval[0] = 0;
1101 linux_mknod(struct thread *td, struct linux_mknod_args *args)
1106 LCONVPATHCREAT(td, args->path, &path);
1110 printf(ARGS(mknod, "%s, %d, %ju"), path, args->mode,
1111 (uintmax_t)args->dev);
1114 switch (args->mode & S_IFMT) {
1117 error = kern_mkfifoat(td, AT_FDCWD, path, UIO_SYSSPACE,
1123 error = kern_mknodat(td, AT_FDCWD, path, UIO_SYSSPACE,
1124 args->mode, args->dev);
1132 args->mode |= S_IFREG;
1135 error = kern_openat(td, AT_FDCWD, path, UIO_SYSSPACE,
1136 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1138 kern_close(td, td->td_retval[0]);
1150 linux_mknodat(struct thread *td, struct linux_mknodat_args *args)
1155 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1156 LCONVPATHCREAT_AT(td, args->filename, &path, dfd);
1159 if (ldebug(mknodat))
1160 printf(ARGS(mknodat, "%s, %d, %d"), path, args->mode, args->dev);
1163 switch (args->mode & S_IFMT) {
1166 error = kern_mkfifoat(td, dfd, path, UIO_SYSSPACE, args->mode);
1171 error = kern_mknodat(td, dfd, path, UIO_SYSSPACE, args->mode,
1180 args->mode |= S_IFREG;
1183 error = kern_openat(td, dfd, path, UIO_SYSSPACE,
1184 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1186 kern_close(td, td->td_retval[0]);
1198 * UGH! This is just about the dumbest idea I've ever heard!!
1201 linux_personality(struct thread *td, struct linux_personality_args *args)
1204 if (ldebug(personality))
1205 printf(ARGS(personality, "%lu"), (unsigned long)args->per);
1210 /* Yes Jim, it's still a Linux... */
1211 td->td_retval[0] = 0;
1215 struct l_itimerval {
1216 l_timeval it_interval;
1220 #define B2L_ITIMERVAL(bip, lip) \
1221 (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec; \
1222 (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec; \
1223 (bip)->it_value.tv_sec = (lip)->it_value.tv_sec; \
1224 (bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
1227 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
1230 struct l_itimerval ls;
1231 struct itimerval aitv, oitv;
1234 if (ldebug(setitimer))
1235 printf(ARGS(setitimer, "%p, %p"),
1236 (void *)uap->itv, (void *)uap->oitv);
1239 if (uap->itv == NULL) {
1240 uap->itv = uap->oitv;
1241 return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
1244 error = copyin(uap->itv, &ls, sizeof(ls));
1247 B2L_ITIMERVAL(&aitv, &ls);
1249 if (ldebug(setitimer)) {
1250 printf("setitimer: value: sec: %jd, usec: %ld\n",
1251 (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec);
1252 printf("setitimer: interval: sec: %jd, usec: %ld\n",
1253 (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec);
1256 error = kern_setitimer(td, uap->which, &aitv, &oitv);
1257 if (error != 0 || uap->oitv == NULL)
1259 B2L_ITIMERVAL(&ls, &oitv);
1261 return (copyout(&ls, uap->oitv, sizeof(ls)));
1265 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
1268 struct l_itimerval ls;
1269 struct itimerval aitv;
1272 if (ldebug(getitimer))
1273 printf(ARGS(getitimer, "%p"), (void *)uap->itv);
1275 error = kern_getitimer(td, uap->which, &aitv);
1278 B2L_ITIMERVAL(&ls, &aitv);
1279 return (copyout(&ls, uap->itv, sizeof(ls)));
1282 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1284 linux_nice(struct thread *td, struct linux_nice_args *args)
1286 struct setpriority_args bsd_args;
1288 bsd_args.which = PRIO_PROCESS;
1289 bsd_args.who = 0; /* current process */
1290 bsd_args.prio = args->inc;
1291 return (sys_setpriority(td, &bsd_args));
1293 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1296 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
1298 struct ucred *newcred, *oldcred;
1299 l_gid_t *linux_gidset;
1304 ngrp = args->gidsetsize;
1305 if (ngrp < 0 || ngrp >= ngroups_max + 1)
1307 linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_LINUX, M_WAITOK);
1308 error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
1312 crextend(newcred, ngrp + 1);
1315 oldcred = p->p_ucred;
1316 crcopy(newcred, oldcred);
1319 * cr_groups[0] holds egid. Setting the whole set from
1320 * the supplied set will cause egid to be changed too.
1321 * Keep cr_groups[0] unchanged to prevent that.
1324 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0) {
1331 newcred->cr_ngroups = ngrp + 1;
1333 bsd_gidset = newcred->cr_groups;
1336 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1340 newcred->cr_ngroups = 1;
1343 proc_set_cred(p, newcred);
1348 free(linux_gidset, M_LINUX);
1353 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1356 l_gid_t *linux_gidset;
1358 int bsd_gidsetsz, ngrp, error;
1360 cred = td->td_ucred;
1361 bsd_gidset = cred->cr_groups;
1362 bsd_gidsetsz = cred->cr_ngroups - 1;
1365 * cr_groups[0] holds egid. Returning the whole set
1366 * here will cause a duplicate. Exclude cr_groups[0]
1370 if ((ngrp = args->gidsetsize) == 0) {
1371 td->td_retval[0] = bsd_gidsetsz;
1375 if (ngrp < bsd_gidsetsz)
1379 linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset),
1381 while (ngrp < bsd_gidsetsz) {
1382 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1386 error = copyout(linux_gidset, args->grouplist, ngrp * sizeof(l_gid_t));
1387 free(linux_gidset, M_LINUX);
1391 td->td_retval[0] = ngrp;
1396 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1398 struct rlimit bsd_rlim;
1399 struct l_rlimit rlim;
1404 if (ldebug(setrlimit))
1405 printf(ARGS(setrlimit, "%d, %p"),
1406 args->resource, (void *)args->rlim);
1409 if (args->resource >= LINUX_RLIM_NLIMITS)
1412 which = linux_to_bsd_resource[args->resource];
1416 error = copyin(args->rlim, &rlim, sizeof(rlim));
1420 bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1421 bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1422 return (kern_setrlimit(td, which, &bsd_rlim));
1425 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1427 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1429 struct l_rlimit rlim;
1430 struct rlimit bsd_rlim;
1434 if (ldebug(old_getrlimit))
1435 printf(ARGS(old_getrlimit, "%d, %p"),
1436 args->resource, (void *)args->rlim);
1439 if (args->resource >= LINUX_RLIM_NLIMITS)
1442 which = linux_to_bsd_resource[args->resource];
1446 lim_rlimit(td, which, &bsd_rlim);
1448 #ifdef COMPAT_LINUX32
1449 rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1450 if (rlim.rlim_cur == UINT_MAX)
1451 rlim.rlim_cur = INT_MAX;
1452 rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1453 if (rlim.rlim_max == UINT_MAX)
1454 rlim.rlim_max = INT_MAX;
1456 rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1457 if (rlim.rlim_cur == ULONG_MAX)
1458 rlim.rlim_cur = LONG_MAX;
1459 rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1460 if (rlim.rlim_max == ULONG_MAX)
1461 rlim.rlim_max = LONG_MAX;
1463 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1465 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1468 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1470 struct l_rlimit rlim;
1471 struct rlimit bsd_rlim;
1475 if (ldebug(getrlimit))
1476 printf(ARGS(getrlimit, "%d, %p"),
1477 args->resource, (void *)args->rlim);
1480 if (args->resource >= LINUX_RLIM_NLIMITS)
1483 which = linux_to_bsd_resource[args->resource];
1487 lim_rlimit(td, which, &bsd_rlim);
1489 rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1490 rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1491 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1495 linux_sched_setscheduler(struct thread *td,
1496 struct linux_sched_setscheduler_args *args)
1498 struct sched_param sched_param;
1503 if (ldebug(sched_setscheduler))
1504 printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1505 args->pid, args->policy, (const void *)args->param);
1508 switch (args->policy) {
1509 case LINUX_SCHED_OTHER:
1510 policy = SCHED_OTHER;
1512 case LINUX_SCHED_FIFO:
1513 policy = SCHED_FIFO;
1515 case LINUX_SCHED_RR:
1522 error = copyin(args->param, &sched_param, sizeof(sched_param));
1526 tdt = linux_tdfind(td, args->pid, -1);
1530 error = kern_sched_setscheduler(td, tdt, policy, &sched_param);
1531 PROC_UNLOCK(tdt->td_proc);
1536 linux_sched_getscheduler(struct thread *td,
1537 struct linux_sched_getscheduler_args *args)
1543 if (ldebug(sched_getscheduler))
1544 printf(ARGS(sched_getscheduler, "%d"), args->pid);
1547 tdt = linux_tdfind(td, args->pid, -1);
1551 error = kern_sched_getscheduler(td, tdt, &policy);
1552 PROC_UNLOCK(tdt->td_proc);
1556 td->td_retval[0] = LINUX_SCHED_OTHER;
1559 td->td_retval[0] = LINUX_SCHED_FIFO;
1562 td->td_retval[0] = LINUX_SCHED_RR;
1569 linux_sched_get_priority_max(struct thread *td,
1570 struct linux_sched_get_priority_max_args *args)
1572 struct sched_get_priority_max_args bsd;
1575 if (ldebug(sched_get_priority_max))
1576 printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1579 switch (args->policy) {
1580 case LINUX_SCHED_OTHER:
1581 bsd.policy = SCHED_OTHER;
1583 case LINUX_SCHED_FIFO:
1584 bsd.policy = SCHED_FIFO;
1586 case LINUX_SCHED_RR:
1587 bsd.policy = SCHED_RR;
1592 return (sys_sched_get_priority_max(td, &bsd));
1596 linux_sched_get_priority_min(struct thread *td,
1597 struct linux_sched_get_priority_min_args *args)
1599 struct sched_get_priority_min_args bsd;
1602 if (ldebug(sched_get_priority_min))
1603 printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1606 switch (args->policy) {
1607 case LINUX_SCHED_OTHER:
1608 bsd.policy = SCHED_OTHER;
1610 case LINUX_SCHED_FIFO:
1611 bsd.policy = SCHED_FIFO;
1613 case LINUX_SCHED_RR:
1614 bsd.policy = SCHED_RR;
1619 return (sys_sched_get_priority_min(td, &bsd));
1622 #define REBOOT_CAD_ON 0x89abcdef
1623 #define REBOOT_CAD_OFF 0
1624 #define REBOOT_HALT 0xcdef0123
1625 #define REBOOT_RESTART 0x01234567
1626 #define REBOOT_RESTART2 0xA1B2C3D4
1627 #define REBOOT_POWEROFF 0x4321FEDC
1628 #define REBOOT_MAGIC1 0xfee1dead
1629 #define REBOOT_MAGIC2 0x28121969
1630 #define REBOOT_MAGIC2A 0x05121996
1631 #define REBOOT_MAGIC2B 0x16041998
1634 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1636 struct reboot_args bsd_args;
1640 printf(ARGS(reboot, "0x%x"), args->cmd);
1643 if (args->magic1 != REBOOT_MAGIC1)
1646 switch (args->magic2) {
1648 case REBOOT_MAGIC2A:
1649 case REBOOT_MAGIC2B:
1655 switch (args->cmd) {
1657 case REBOOT_CAD_OFF:
1658 return (priv_check(td, PRIV_REBOOT));
1660 bsd_args.opt = RB_HALT;
1662 case REBOOT_RESTART:
1663 case REBOOT_RESTART2:
1666 case REBOOT_POWEROFF:
1667 bsd_args.opt = RB_POWEROFF;
1672 return (sys_reboot(td, &bsd_args));
1677 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1678 * td->td_retval[1] when COMPAT_43 is defined. This clobbers registers that
1679 * are assumed to be preserved. The following lightweight syscalls fixes
1680 * this. See also linux_getgid16() and linux_getuid16() in linux_uid16.c
1682 * linux_getpid() - MP SAFE
1683 * linux_getgid() - MP SAFE
1684 * linux_getuid() - MP SAFE
1688 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1693 printf(ARGS(getpid, ""));
1695 td->td_retval[0] = td->td_proc->p_pid;
1701 linux_gettid(struct thread *td, struct linux_gettid_args *args)
1703 struct linux_emuldata *em;
1707 printf(ARGS(gettid, ""));
1711 KASSERT(em != NULL, ("gettid: emuldata not found.\n"));
1713 td->td_retval[0] = em->em_tid;
1720 linux_getppid(struct thread *td, struct linux_getppid_args *args)
1724 if (ldebug(getppid))
1725 printf(ARGS(getppid, ""));
1728 PROC_LOCK(td->td_proc);
1729 td->td_retval[0] = td->td_proc->p_pptr->p_pid;
1730 PROC_UNLOCK(td->td_proc);
1735 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1740 printf(ARGS(getgid, ""));
1743 td->td_retval[0] = td->td_ucred->cr_rgid;
1748 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1753 printf(ARGS(getuid, ""));
1756 td->td_retval[0] = td->td_ucred->cr_ruid;
1762 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1764 struct getsid_args bsd;
1768 printf(ARGS(getsid, "%i"), args->pid);
1771 bsd.pid = args->pid;
1772 return (sys_getsid(td, &bsd));
1776 linux_nosys(struct thread *td, struct nosys_args *ignore)
1783 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1785 struct getpriority_args bsd_args;
1789 if (ldebug(getpriority))
1790 printf(ARGS(getpriority, "%i, %i"), args->which, args->who);
1793 bsd_args.which = args->which;
1794 bsd_args.who = args->who;
1795 error = sys_getpriority(td, &bsd_args);
1796 td->td_retval[0] = 20 - td->td_retval[0];
1801 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1806 if (ldebug(sethostname))
1807 printf(ARGS(sethostname, "*, %i"), args->len);
1811 name[1] = KERN_HOSTNAME;
1812 return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1817 linux_setdomainname(struct thread *td, struct linux_setdomainname_args *args)
1822 if (ldebug(setdomainname))
1823 printf(ARGS(setdomainname, "*, %i"), args->len);
1827 name[1] = KERN_NISDOMAINNAME;
1828 return (userland_sysctl(td, name, 2, 0, 0, 0, args->name,
1833 linux_exit_group(struct thread *td, struct linux_exit_group_args *args)
1837 if (ldebug(exit_group))
1838 printf(ARGS(exit_group, "%i"), args->error_code);
1841 LINUX_CTR2(exit_group, "thread(%d) (%d)", td->td_tid,
1845 * XXX: we should send a signal to the parent if
1846 * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?)
1847 * as it doesnt occur often.
1849 exit1(td, args->error_code, 0);
1853 #define _LINUX_CAPABILITY_VERSION 0x19980330
1855 struct l_user_cap_header {
1860 struct l_user_cap_data {
1867 linux_capget(struct thread *td, struct linux_capget_args *args)
1869 struct l_user_cap_header luch;
1870 struct l_user_cap_data lucd;
1873 if (args->hdrp == NULL)
1876 error = copyin(args->hdrp, &luch, sizeof(luch));
1880 if (luch.version != _LINUX_CAPABILITY_VERSION) {
1881 luch.version = _LINUX_CAPABILITY_VERSION;
1882 error = copyout(&luch, args->hdrp, sizeof(luch));
1893 * The current implementation doesn't support setting
1894 * a capability (it's essentially a stub) so indicate
1895 * that no capabilities are currently set or available
1898 bzero (&lucd, sizeof(lucd));
1899 error = copyout(&lucd, args->datap, sizeof(lucd));
1906 linux_capset(struct thread *td, struct linux_capset_args *args)
1908 struct l_user_cap_header luch;
1909 struct l_user_cap_data lucd;
1912 if (args->hdrp == NULL || args->datap == NULL)
1915 error = copyin(args->hdrp, &luch, sizeof(luch));
1919 if (luch.version != _LINUX_CAPABILITY_VERSION) {
1920 luch.version = _LINUX_CAPABILITY_VERSION;
1921 error = copyout(&luch, args->hdrp, sizeof(luch));
1930 error = copyin(args->datap, &lucd, sizeof(lucd));
1934 /* We currently don't support setting any capabilities. */
1935 if (lucd.effective || lucd.permitted || lucd.inheritable) {
1937 "capset effective=0x%x, permitted=0x%x, "
1938 "inheritable=0x%x is not implemented",
1939 (int)lucd.effective, (int)lucd.permitted,
1940 (int)lucd.inheritable);
1948 linux_prctl(struct thread *td, struct linux_prctl_args *args)
1950 int error = 0, max_size;
1951 struct proc *p = td->td_proc;
1952 char comm[LINUX_MAX_COMM_LEN];
1953 struct linux_emuldata *em;
1958 printf(ARGS(prctl, "%d, %ju, %ju, %ju, %ju"), args->option,
1959 (uintmax_t)args->arg2, (uintmax_t)args->arg3,
1960 (uintmax_t)args->arg4, (uintmax_t)args->arg5);
1963 switch (args->option) {
1964 case LINUX_PR_SET_PDEATHSIG:
1965 if (!LINUX_SIG_VALID(args->arg2))
1968 KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1969 em->pdeath_signal = args->arg2;
1971 case LINUX_PR_GET_PDEATHSIG:
1973 KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1974 pdeath_signal = em->pdeath_signal;
1975 error = copyout(&pdeath_signal,
1976 (void *)(register_t)args->arg2,
1977 sizeof(pdeath_signal));
1979 case LINUX_PR_GET_KEEPCAPS:
1981 * Indicate that we always clear the effective and
1982 * permitted capability sets when the user id becomes
1983 * non-zero (actually the capability sets are simply
1984 * always zero in the current implementation).
1986 td->td_retval[0] = 0;
1988 case LINUX_PR_SET_KEEPCAPS:
1990 * Ignore requests to keep the effective and permitted
1991 * capability sets when the user id becomes non-zero.
1994 case LINUX_PR_SET_NAME:
1996 * To be on the safe side we need to make sure to not
1997 * overflow the size a linux program expects. We already
1998 * do this here in the copyin, so that we don't need to
2001 max_size = MIN(sizeof(comm), sizeof(p->p_comm));
2002 error = copyinstr((void *)(register_t)args->arg2, comm,
2005 /* Linux silently truncates the name if it is too long. */
2006 if (error == ENAMETOOLONG) {
2008 * XXX: copyinstr() isn't documented to populate the
2009 * array completely, so do a copyin() to be on the
2010 * safe side. This should be changed in case
2011 * copyinstr() is changed to guarantee this.
2013 error = copyin((void *)(register_t)args->arg2, comm,
2015 comm[max_size - 1] = '\0';
2021 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
2024 case LINUX_PR_GET_NAME:
2026 strlcpy(comm, p->p_comm, sizeof(comm));
2028 error = copyout(comm, (void *)(register_t)args->arg2,
2040 linux_sched_setparam(struct thread *td,
2041 struct linux_sched_setparam_args *uap)
2043 struct sched_param sched_param;
2048 if (ldebug(sched_setparam))
2049 printf(ARGS(sched_setparam, "%d, *"), uap->pid);
2052 error = copyin(uap->param, &sched_param, sizeof(sched_param));
2056 tdt = linux_tdfind(td, uap->pid, -1);
2060 error = kern_sched_setparam(td, tdt, &sched_param);
2061 PROC_UNLOCK(tdt->td_proc);
2066 linux_sched_getparam(struct thread *td,
2067 struct linux_sched_getparam_args *uap)
2069 struct sched_param sched_param;
2074 if (ldebug(sched_getparam))
2075 printf(ARGS(sched_getparam, "%d, *"), uap->pid);
2078 tdt = linux_tdfind(td, uap->pid, -1);
2082 error = kern_sched_getparam(td, tdt, &sched_param);
2083 PROC_UNLOCK(tdt->td_proc);
2085 error = copyout(&sched_param, uap->param,
2086 sizeof(sched_param));
2091 * Get affinity of a process.
2094 linux_sched_getaffinity(struct thread *td,
2095 struct linux_sched_getaffinity_args *args)
2099 struct cpuset_getaffinity_args cga;
2102 if (ldebug(sched_getaffinity))
2103 printf(ARGS(sched_getaffinity, "%d, %d, *"), args->pid,
2106 if (args->len < sizeof(cpuset_t))
2109 tdt = linux_tdfind(td, args->pid, -1);
2113 PROC_UNLOCK(tdt->td_proc);
2114 cga.level = CPU_LEVEL_WHICH;
2115 cga.which = CPU_WHICH_TID;
2116 cga.id = tdt->td_tid;
2117 cga.cpusetsize = sizeof(cpuset_t);
2118 cga.mask = (cpuset_t *) args->user_mask_ptr;
2120 if ((error = sys_cpuset_getaffinity(td, &cga)) == 0)
2121 td->td_retval[0] = sizeof(cpuset_t);
2127 * Set affinity of a process.
2130 linux_sched_setaffinity(struct thread *td,
2131 struct linux_sched_setaffinity_args *args)
2133 struct cpuset_setaffinity_args csa;
2137 if (ldebug(sched_setaffinity))
2138 printf(ARGS(sched_setaffinity, "%d, %d, *"), args->pid,
2141 if (args->len < sizeof(cpuset_t))
2144 tdt = linux_tdfind(td, args->pid, -1);
2148 PROC_UNLOCK(tdt->td_proc);
2149 csa.level = CPU_LEVEL_WHICH;
2150 csa.which = CPU_WHICH_TID;
2151 csa.id = tdt->td_tid;
2152 csa.cpusetsize = sizeof(cpuset_t);
2153 csa.mask = (cpuset_t *) args->user_mask_ptr;
2155 return (sys_cpuset_setaffinity(td, &csa));
2158 struct linux_rlimit64 {
2164 linux_prlimit64(struct thread *td, struct linux_prlimit64_args *args)
2166 struct rlimit rlim, nrlim;
2167 struct linux_rlimit64 lrlim;
2174 if (ldebug(prlimit64))
2175 printf(ARGS(prlimit64, "%d, %d, %p, %p"), args->pid,
2176 args->resource, (void *)args->new, (void *)args->old);
2179 if (args->resource >= LINUX_RLIM_NLIMITS)
2182 which = linux_to_bsd_resource[args->resource];
2186 if (args->new != NULL) {
2188 * Note. Unlike FreeBSD where rlim is signed 64-bit Linux
2189 * rlim is unsigned 64-bit. FreeBSD treats negative limits
2190 * as INFINITY so we do not need a conversion even.
2192 error = copyin(args->new, &nrlim, sizeof(nrlim));
2197 flags = PGET_HOLD | PGET_NOTWEXIT;
2198 if (args->new != NULL)
2199 flags |= PGET_CANDEBUG;
2201 flags |= PGET_CANSEE;
2202 error = pget(args->pid, flags, &p);
2206 if (args->old != NULL) {
2208 lim_rlimit_proc(p, which, &rlim);
2210 if (rlim.rlim_cur == RLIM_INFINITY)
2211 lrlim.rlim_cur = LINUX_RLIM_INFINITY;
2213 lrlim.rlim_cur = rlim.rlim_cur;
2214 if (rlim.rlim_max == RLIM_INFINITY)
2215 lrlim.rlim_max = LINUX_RLIM_INFINITY;
2217 lrlim.rlim_max = rlim.rlim_max;
2218 error = copyout(&lrlim, args->old, sizeof(lrlim));
2223 if (args->new != NULL)
2224 error = kern_proc_setrlimit(td, p, which, &nrlim);
2232 linux_pselect6(struct thread *td, struct linux_pselect6_args *args)
2234 struct timeval utv, tv0, tv1, *tvp;
2235 struct l_pselect6arg lpse6;
2236 struct l_timespec lts;
2237 struct timespec uts;
2244 if (args->sig != NULL) {
2245 error = copyin(args->sig, &lpse6, sizeof(lpse6));
2248 if (lpse6.ss_len != sizeof(l_ss))
2250 if (lpse6.ss != 0) {
2251 error = copyin(PTRIN(lpse6.ss), &l_ss,
2255 linux_to_bsd_sigset(&l_ss, &ss);
2261 * Currently glibc changes nanosecond number to microsecond.
2262 * This mean losing precision but for now it is hardly seen.
2264 if (args->tsp != NULL) {
2265 error = copyin(args->tsp, <s, sizeof(lts));
2268 error = linux_to_native_timespec(&uts, <s);
2272 TIMESPEC_TO_TIMEVAL(&utv, &uts);
2273 if (itimerfix(&utv))
2281 error = kern_pselect(td, args->nfds, args->readfds, args->writefds,
2282 args->exceptfds, tvp, ssp, LINUX_NFDBITS);
2284 if (error == 0 && args->tsp != NULL) {
2285 if (td->td_retval[0] != 0) {
2287 * Compute how much time was left of the timeout,
2288 * by subtracting the current time and the time
2289 * before we started the call, and subtracting
2290 * that result from the user-supplied value.
2294 timevalsub(&tv1, &tv0);
2295 timevalsub(&utv, &tv1);
2301 TIMEVAL_TO_TIMESPEC(&utv, &uts);
2303 native_to_linux_timespec(<s, &uts);
2304 error = copyout(<s, args->tsp, sizeof(lts));
2311 linux_ppoll(struct thread *td, struct linux_ppoll_args *args)
2313 struct timespec ts0, ts1;
2314 struct l_timespec lts;
2315 struct timespec uts, *tsp;
2321 if (args->sset != NULL) {
2322 if (args->ssize != sizeof(l_ss))
2324 error = copyin(args->sset, &l_ss, sizeof(l_ss));
2327 linux_to_bsd_sigset(&l_ss, &ss);
2331 if (args->tsp != NULL) {
2332 error = copyin(args->tsp, <s, sizeof(lts));
2335 error = linux_to_native_timespec(&uts, <s);
2344 error = kern_poll(td, args->fds, args->nfds, tsp, ssp);
2346 if (error == 0 && args->tsp != NULL) {
2347 if (td->td_retval[0]) {
2349 timespecsub(&ts1, &ts0);
2350 timespecsub(&uts, &ts1);
2352 timespecclear(&uts);
2354 timespecclear(&uts);
2356 native_to_linux_timespec(<s, &uts);
2357 error = copyout(<s, args->tsp, sizeof(lts));
2363 #if defined(DEBUG) || defined(KTR)
2364 /* XXX: can be removed when every ldebug(...) and KTR stuff are removed. */
2366 #ifdef COMPAT_LINUX32
2367 #define L_MAXSYSCALL LINUX32_SYS_MAXSYSCALL
2369 #define L_MAXSYSCALL LINUX_SYS_MAXSYSCALL
2372 u_char linux_debug_map[howmany(L_MAXSYSCALL, sizeof(u_char))];
2375 linux_debug(int syscall, int toggle, int global)
2379 char c = toggle ? 0 : 0xff;
2381 memset(linux_debug_map, c, sizeof(linux_debug_map));
2384 if (syscall < 0 || syscall >= L_MAXSYSCALL)
2387 clrbit(linux_debug_map, syscall);
2389 setbit(linux_debug_map, syscall);
2395 * Usage: sysctl linux.debug=<syscall_nr>.<0/1>
2397 * E.g.: sysctl linux.debug=21.0
2399 * As a special case, syscall "all" will apply to all syscalls globally.
2401 #define LINUX_MAX_DEBUGSTR 16
2403 linux_sysctl_debug(SYSCTL_HANDLER_ARGS)
2405 char value[LINUX_MAX_DEBUGSTR], *p;
2406 int error, sysc, toggle;
2410 error = sysctl_handle_string(oidp, value, LINUX_MAX_DEBUGSTR, req);
2411 if (error || req->newptr == NULL)
2413 for (p = value; *p != '\0' && *p != '.'; p++);
2417 sysc = strtol(value, NULL, 0);
2418 toggle = strtol(p, NULL, 0);
2419 if (strcmp(value, "all") == 0)
2421 error = linux_debug(sysc, toggle, global);
2425 #endif /* DEBUG || KTR */
2428 linux_sched_rr_get_interval(struct thread *td,
2429 struct linux_sched_rr_get_interval_args *uap)
2432 struct l_timespec lts;
2437 * According to man in case the invalid pid specified
2438 * EINVAL should be returned.
2443 tdt = linux_tdfind(td, uap->pid, -1);
2447 error = kern_sched_rr_get_interval_td(td, tdt, &ts);
2448 PROC_UNLOCK(tdt->td_proc);
2451 native_to_linux_timespec(<s, &ts);
2452 return (copyout(<s, uap->interval, sizeof(lts)));
2456 * In case when the Linux thread is the initial thread in
2457 * the thread group thread id is equal to the process id.
2458 * Glibc depends on this magic (assert in pthread_getattr_np.c).
2461 linux_tdfind(struct thread *td, lwpid_t tid, pid_t pid)
2463 struct linux_emuldata *em;
2468 if (tid == 0 || tid == td->td_tid) {
2470 PROC_LOCK(tdt->td_proc);
2471 } else if (tid > PID_MAX)
2472 tdt = tdfind(tid, pid);
2475 * Initial thread where the tid equal to the pid.
2479 if (SV_PROC_ABI(p) != SV_ABI_LINUX) {
2481 * p is not a Linuxulator process.
2486 FOREACH_THREAD_IN_PROC(p, tdt) {
2488 if (tid == em->em_tid)
2500 linux_to_bsd_waitopts(int options, int *bsdopts)
2503 if (options & LINUX_WNOHANG)
2504 *bsdopts |= WNOHANG;
2505 if (options & LINUX_WUNTRACED)
2506 *bsdopts |= WUNTRACED;
2507 if (options & LINUX_WEXITED)
2508 *bsdopts |= WEXITED;
2509 if (options & LINUX_WCONTINUED)
2510 *bsdopts |= WCONTINUED;
2511 if (options & LINUX_WNOWAIT)
2512 *bsdopts |= WNOWAIT;
2514 if (options & __WCLONE)
2515 *bsdopts |= WLINUXCLONE;