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 {
142 static int linux_utimensat_nsec_valid(l_long);
146 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args)
148 struct l_sysinfo 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 - 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);
207 it.it_value.tv_sec = (long) secs;
208 it.it_value.tv_usec = 0;
209 it.it_interval.tv_sec = 0;
210 it.it_interval.tv_usec = 0;
211 error = kern_setitimer(td, ITIMER_REAL, &it, &old_it);
214 if (timevalisset(&old_it.it_value)) {
215 if (old_it.it_value.tv_usec != 0)
216 old_it.it_value.tv_sec++;
217 td->td_retval[0] = old_it.it_value.tv_sec;
223 linux_brk(struct thread *td, struct linux_brk_args *args)
225 struct vmspace *vm = td->td_proc->p_vmspace;
226 vm_offset_t new, old;
227 struct obreak_args /* {
233 printf(ARGS(brk, "%p"), (void *)(uintptr_t)args->dsend);
235 old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
236 new = (vm_offset_t)args->dsend;
237 tmp.nsize = (char *)new;
238 if (((caddr_t)new > vm->vm_daddr) && !sys_obreak(td, &tmp))
239 td->td_retval[0] = (long)new;
241 td->td_retval[0] = (long)old;
246 #if defined(__i386__)
247 /* XXX: what about amd64/linux32? */
250 linux_uselib(struct thread *td, struct linux_uselib_args *args)
257 unsigned long file_offset;
258 unsigned long bss_size;
261 int error, locked, writecount;
263 LCONVPATHEXIST(td, args->library, &library);
267 printf(ARGS(uselib, "%s"), library);
274 NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | AUDITVNODE1,
275 UIO_SYSSPACE, library, td);
282 NDFREE(&ni, NDF_ONLY_PNBUF);
285 * From here on down, we have a locked vnode that must be unlocked.
286 * XXX: The code below largely duplicates exec_check_permissions().
291 error = VOP_GET_WRITECOUNT(vp, &writecount);
294 if (writecount != 0) {
300 error = VOP_GETATTR(vp, &attr, td->td_ucred);
304 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
305 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
306 /* EACCESS is what exec(2) returns. */
312 if (attr.va_size == 0) {
317 /* Can we access it? */
318 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
323 * XXX: This should use vn_open() so that it is properly authorized,
324 * and to reduce code redundancy all over the place here.
325 * XXX: Not really, it duplicates far more of exec_check_permissions()
329 error = mac_vnode_check_open(td->td_ucred, vp, VREAD);
333 error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL);
337 /* Pull in executable header into exec_map */
338 error = vm_mmap(exec_map, (vm_offset_t *)&a_out, PAGE_SIZE,
339 VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 0);
343 /* Is it a Linux binary ? */
344 if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
350 * While we are here, we should REALLY do some more checks
353 /* Set file/virtual offset based on a.out variant. */
354 switch ((int)(a_out->a_magic & 0xffff)) {
355 case 0413: /* ZMAGIC */
358 case 0314: /* QMAGIC */
366 bss_size = round_page(a_out->a_bss);
368 /* Check various fields in header for validity/bounds. */
369 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
374 /* text + data can't exceed file size */
375 if (a_out->a_data + a_out->a_text > attr.va_size) {
381 * text/data/bss must not exceed limits
382 * XXX - this is not complete. it should check current usage PLUS
383 * the resources needed by this library.
385 PROC_LOCK(td->td_proc);
386 if (a_out->a_text > maxtsiz ||
387 a_out->a_data + bss_size > lim_cur(td->td_proc, RLIMIT_DATA) ||
388 racct_set(td->td_proc, RACCT_DATA, a_out->a_data +
390 PROC_UNLOCK(td->td_proc);
394 PROC_UNLOCK(td->td_proc);
397 * Prevent more writers.
398 * XXX: Note that if any of the VM operations fail below we don't
404 * Lock no longer needed
410 * Check if file_offset page aligned. Currently we cannot handle
411 * misalinged file offsets, and so we read in the entire image
414 if (file_offset & PAGE_MASK) {
416 printf("uselib: Non page aligned binary %lu\n", file_offset);
418 /* Map text+data read/write/execute */
420 /* a_entry is the load address and is page aligned */
421 vmaddr = trunc_page(a_out->a_entry);
423 /* get anon user mapping, read+write+execute */
424 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
425 &vmaddr, a_out->a_text + a_out->a_data, 0, VMFS_NO_SPACE,
426 VM_PROT_ALL, VM_PROT_ALL, 0);
430 error = vn_rdwr(UIO_READ, vp, (void *)vmaddr, file_offset,
431 a_out->a_text + a_out->a_data, UIO_USERSPACE, 0,
432 td->td_ucred, NOCRED, &aresid, td);
441 printf("uselib: Page aligned binary %lu\n", file_offset);
444 * for QMAGIC, a_entry is 20 bytes beyond the load address
445 * to skip the executable header
447 vmaddr = trunc_page(a_out->a_entry);
450 * Map it all into the process's space as a single
451 * copy-on-write "data" segment.
453 error = vm_mmap(&td->td_proc->p_vmspace->vm_map, &vmaddr,
454 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
455 MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset);
460 printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long *)vmaddr)[0],
461 ((long *)vmaddr)[1]);
464 /* Calculate BSS start address */
465 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
468 /* allocate some 'anon' space */
469 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
470 &vmaddr, bss_size, 0, VMFS_NO_SPACE, VM_PROT_ALL,
477 /* Unlock vnode if needed */
481 /* Release the temporary mapping. */
483 kmap_free_wakeup(exec_map, (vm_offset_t)a_out, PAGE_SIZE);
488 #endif /* __i386__ */
491 linux_select(struct thread *td, struct linux_select_args *args)
494 struct timeval tv0, tv1, utv, *tvp;
499 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
500 (void *)args->readfds, (void *)args->writefds,
501 (void *)args->exceptfds, (void *)args->timeout);
505 * Store current time for computation of the amount of
509 if ((error = copyin(args->timeout, <v, sizeof(ltv))))
511 utv.tv_sec = ltv.tv_sec;
512 utv.tv_usec = ltv.tv_usec;
515 printf(LMSG("incoming timeout (%jd/%ld)"),
516 (intmax_t)utv.tv_sec, utv.tv_usec);
519 if (itimerfix(&utv)) {
521 * The timeval was invalid. Convert it to something
522 * valid that will act as it does under Linux.
524 utv.tv_sec += utv.tv_usec / 1000000;
525 utv.tv_usec %= 1000000;
526 if (utv.tv_usec < 0) {
528 utv.tv_usec += 1000000;
538 error = kern_select(td, args->nfds, args->readfds, args->writefds,
539 args->exceptfds, tvp, LINUX_NFDBITS);
543 printf(LMSG("real select returns %d"), error);
549 if (td->td_retval[0]) {
551 * Compute how much time was left of the timeout,
552 * by subtracting the current time and the time
553 * before we started the call, and subtracting
554 * that result from the user-supplied value.
557 timevalsub(&tv1, &tv0);
558 timevalsub(&utv, &tv1);
565 printf(LMSG("outgoing timeout (%jd/%ld)"),
566 (intmax_t)utv.tv_sec, utv.tv_usec);
568 ltv.tv_sec = utv.tv_sec;
569 ltv.tv_usec = utv.tv_usec;
570 if ((error = copyout(<v, args->timeout, sizeof(ltv))))
577 printf(LMSG("select_out -> %d"), error);
583 linux_mremap(struct thread *td, struct linux_mremap_args *args)
585 struct munmap_args /* {
593 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
594 (void *)(uintptr_t)args->addr,
595 (unsigned long)args->old_len,
596 (unsigned long)args->new_len,
597 (unsigned long)args->flags);
600 if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) {
601 td->td_retval[0] = 0;
606 * Check for the page alignment.
607 * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK.
609 if (args->addr & PAGE_MASK) {
610 td->td_retval[0] = 0;
614 args->new_len = round_page(args->new_len);
615 args->old_len = round_page(args->old_len);
617 if (args->new_len > args->old_len) {
618 td->td_retval[0] = 0;
622 if (args->new_len < args->old_len) {
624 (caddr_t)((uintptr_t)args->addr + args->new_len);
625 bsd_args.len = args->old_len - args->new_len;
626 error = sys_munmap(td, &bsd_args);
629 td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
633 #define LINUX_MS_ASYNC 0x0001
634 #define LINUX_MS_INVALIDATE 0x0002
635 #define LINUX_MS_SYNC 0x0004
638 linux_msync(struct thread *td, struct linux_msync_args *args)
640 struct msync_args bsd_args;
642 bsd_args.addr = (caddr_t)(uintptr_t)args->addr;
643 bsd_args.len = (uintptr_t)args->len;
644 bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
646 return (sys_msync(td, &bsd_args));
650 linux_time(struct thread *td, struct linux_time_args *args)
658 printf(ARGS(time, "*"));
663 if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
665 td->td_retval[0] = tm;
669 struct l_times_argv {
672 l_clock_t tms_cutime;
673 l_clock_t tms_cstime;
678 * Glibc versions prior to 2.2.1 always use hard-coded CLK_TCK value.
679 * Since 2.2.1 Glibc uses value exported from kernel via AT_CLKTCK
680 * auxiliary vector entry.
684 #define CONVOTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
685 #define CONVNTCK(r) (r.tv_sec * stclohz + r.tv_usec / (1000000 / stclohz))
687 #define CONVTCK(r) (linux_kernver(td) >= LINUX_KERNVER_2004000 ? \
688 CONVNTCK(r) : CONVOTCK(r))
691 linux_times(struct thread *td, struct linux_times_args *args)
693 struct timeval tv, utime, stime, cutime, cstime;
694 struct l_times_argv tms;
700 printf(ARGS(times, "*"));
703 if (args->buf != NULL) {
707 calcru(p, &utime, &stime);
709 calccru(p, &cutime, &cstime);
712 tms.tms_utime = CONVTCK(utime);
713 tms.tms_stime = CONVTCK(stime);
715 tms.tms_cutime = CONVTCK(cutime);
716 tms.tms_cstime = CONVTCK(cstime);
718 if ((error = copyout(&tms, args->buf, sizeof(tms))))
723 td->td_retval[0] = (int)CONVTCK(tv);
728 linux_newuname(struct thread *td, struct linux_newuname_args *args)
730 struct l_new_utsname utsname;
731 char osname[LINUX_MAX_UTSNAME];
732 char osrelease[LINUX_MAX_UTSNAME];
736 if (ldebug(newuname))
737 printf(ARGS(newuname, "*"));
740 linux_get_osname(td, osname);
741 linux_get_osrelease(td, osrelease);
743 bzero(&utsname, sizeof(utsname));
744 strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
745 getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
746 getcreddomainname(td->td_ucred, utsname.domainname, LINUX_MAX_UTSNAME);
747 strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
748 strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
749 for (p = utsname.version; *p != '\0'; ++p)
754 strlcpy(utsname.machine, linux_kplatform, LINUX_MAX_UTSNAME);
756 return (copyout(&utsname, args->buf, sizeof(utsname)));
765 linux_utime(struct thread *td, struct linux_utime_args *args)
767 struct timeval tv[2], *tvp;
768 struct l_utimbuf lut;
772 LCONVPATHEXIST(td, args->fname, &fname);
776 printf(ARGS(utime, "%s, *"), fname);
780 if ((error = copyin(args->times, &lut, sizeof lut))) {
784 tv[0].tv_sec = lut.l_actime;
786 tv[1].tv_sec = lut.l_modtime;
792 error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
798 linux_utimes(struct thread *td, struct linux_utimes_args *args)
801 struct timeval tv[2], *tvp = NULL;
805 LCONVPATHEXIST(td, args->fname, &fname);
809 printf(ARGS(utimes, "%s, *"), fname);
812 if (args->tptr != NULL) {
813 if ((error = copyin(args->tptr, ltv, sizeof ltv))) {
817 tv[0].tv_sec = ltv[0].tv_sec;
818 tv[0].tv_usec = ltv[0].tv_usec;
819 tv[1].tv_sec = ltv[1].tv_sec;
820 tv[1].tv_usec = ltv[1].tv_usec;
824 error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
830 linux_utimensat_nsec_valid(l_long nsec)
833 if (nsec == LINUX_UTIME_OMIT || nsec == LINUX_UTIME_NOW)
835 if (nsec >= 0 && nsec <= 999999999)
841 linux_utimensat(struct thread *td, struct linux_utimensat_args *args)
843 struct l_timespec l_times[2];
844 struct timespec times[2], *timesp = NULL;
846 int error, dfd, flags = 0;
848 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
851 if (ldebug(utimensat))
852 printf(ARGS(utimensat, "%d, *"), dfd);
855 if (args->flags & ~LINUX_AT_SYMLINK_NOFOLLOW)
858 if (args->times != NULL) {
859 error = copyin(args->times, l_times, sizeof(l_times));
863 if (linux_utimensat_nsec_valid(l_times[0].tv_nsec) != 0 ||
864 linux_utimensat_nsec_valid(l_times[1].tv_nsec) != 0)
867 times[0].tv_sec = l_times[0].tv_sec;
868 switch (l_times[0].tv_nsec)
870 case LINUX_UTIME_OMIT:
871 times[0].tv_nsec = UTIME_OMIT;
873 case LINUX_UTIME_NOW:
874 times[0].tv_nsec = UTIME_NOW;
877 times[0].tv_nsec = l_times[0].tv_nsec;
880 times[1].tv_sec = l_times[1].tv_sec;
881 switch (l_times[1].tv_nsec)
883 case LINUX_UTIME_OMIT:
884 times[1].tv_nsec = UTIME_OMIT;
886 case LINUX_UTIME_NOW:
887 times[1].tv_nsec = UTIME_NOW;
890 times[1].tv_nsec = l_times[1].tv_nsec;
896 if (times[0].tv_nsec == UTIME_OMIT && times[1].tv_nsec == UTIME_OMIT)
897 /* This breaks POSIX, but is what the Linux kernel does
898 * _on purpose_ (documented in the man page for utimensat(2)),
899 * so we must follow that behaviour. */
902 if (args->pathname != NULL)
903 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
904 else if (args->flags != 0)
907 if (args->flags & LINUX_AT_SYMLINK_NOFOLLOW)
908 flags |= AT_SYMLINK_NOFOLLOW;
911 error = kern_futimens(td, dfd, timesp, UIO_SYSSPACE);
913 error = kern_utimensat(td, dfd, path, UIO_SYSSPACE, timesp,
914 UIO_SYSSPACE, flags);
922 linux_futimesat(struct thread *td, struct linux_futimesat_args *args)
925 struct timeval tv[2], *tvp = NULL;
929 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
930 LCONVPATHEXIST_AT(td, args->filename, &fname, dfd);
933 if (ldebug(futimesat))
934 printf(ARGS(futimesat, "%s, *"), fname);
937 if (args->utimes != NULL) {
938 if ((error = copyin(args->utimes, ltv, sizeof ltv))) {
942 tv[0].tv_sec = ltv[0].tv_sec;
943 tv[0].tv_usec = ltv[0].tv_usec;
944 tv[1].tv_sec = ltv[1].tv_sec;
945 tv[1].tv_usec = ltv[1].tv_usec;
949 error = kern_utimesat(td, dfd, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
955 linux_common_wait(struct thread *td, int pid, int *status,
956 int options, struct rusage *ru)
960 error = kern_wait(td, pid, &tmpstat, options, ru);
966 if (WIFSIGNALED(tmpstat))
967 tmpstat = (tmpstat & 0xffffff80) |
968 bsd_to_linux_signal(WTERMSIG(tmpstat));
969 else if (WIFSTOPPED(tmpstat))
970 tmpstat = (tmpstat & 0xffff00ff) |
971 (bsd_to_linux_signal(WSTOPSIG(tmpstat)) << 8);
972 else if (WIFCONTINUED(tmpstat))
974 error = copyout(&tmpstat, status, sizeof(int));
980 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
982 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
984 struct linux_wait4_args wait4_args;
988 printf(ARGS(waitpid, "%d, %p, %d"),
989 args->pid, (void *)args->status, args->options);
992 wait4_args.pid = args->pid;
993 wait4_args.status = args->status;
994 wait4_args.options = args->options;
995 wait4_args.rusage = NULL;
997 return (linux_wait4(td, &wait4_args));
999 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1002 linux_wait4(struct thread *td, struct linux_wait4_args *args)
1005 struct rusage ru, *rup;
1009 printf(ARGS(wait4, "%d, %p, %d, %p"),
1010 args->pid, (void *)args->status, args->options,
1011 (void *)args->rusage);
1013 if (args->options & ~(LINUX_WUNTRACED | LINUX_WNOHANG |
1014 LINUX_WCONTINUED | __WCLONE | __WNOTHREAD | __WALL))
1018 linux_to_bsd_waitopts(args->options, &options);
1020 if (args->rusage != NULL)
1024 error = linux_common_wait(td, args->pid, args->status, options, rup);
1027 if (args->rusage != NULL)
1028 error = linux_copyout_rusage(&ru, args->rusage);
1033 linux_waitid(struct thread *td, struct linux_waitid_args *args)
1035 int status, options, sig;
1036 struct __wrusage wru;
1044 linux_to_bsd_waitopts(args->options, &options);
1046 if (options & ~(WNOHANG | WNOWAIT | WEXITED | WUNTRACED | WCONTINUED))
1048 if (!(options & (WEXITED | WUNTRACED | WCONTINUED)))
1051 switch (args->idtype) {
1069 error = kern_wait6(td, idtype, args->id, &status, options,
1073 if (args->rusage != NULL) {
1074 error = linux_copyout_rusage(&wru.wru_children,
1079 if (args->info != NULL) {
1081 if (td->td_retval[0] == 0)
1082 bzero(&lsi, sizeof(lsi));
1084 sig = bsd_to_linux_signal(siginfo.si_signo);
1085 siginfo_to_lsiginfo(&siginfo, &lsi, sig);
1087 error = copyout(&lsi, args->info, sizeof(lsi));
1089 td->td_retval[0] = 0;
1095 linux_mknod(struct thread *td, struct linux_mknod_args *args)
1100 LCONVPATHCREAT(td, args->path, &path);
1104 printf(ARGS(mknod, "%s, %d, %ju"), path, args->mode,
1105 (uintmax_t)args->dev);
1108 switch (args->mode & S_IFMT) {
1111 error = kern_mkfifo(td, path, UIO_SYSSPACE, args->mode);
1116 error = kern_mknod(td, path, UIO_SYSSPACE, args->mode,
1125 args->mode |= S_IFREG;
1128 error = kern_open(td, path, UIO_SYSSPACE,
1129 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1131 kern_close(td, td->td_retval[0]);
1143 linux_mknodat(struct thread *td, struct linux_mknodat_args *args)
1148 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1149 LCONVPATHCREAT_AT(td, args->filename, &path, dfd);
1152 if (ldebug(mknodat))
1153 printf(ARGS(mknodat, "%s, %d, %d"), path, args->mode, args->dev);
1156 switch (args->mode & S_IFMT) {
1159 error = kern_mkfifoat(td, dfd, path, UIO_SYSSPACE, args->mode);
1164 error = kern_mknodat(td, dfd, path, UIO_SYSSPACE, args->mode,
1173 args->mode |= S_IFREG;
1176 error = kern_openat(td, dfd, path, UIO_SYSSPACE,
1177 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1179 kern_close(td, td->td_retval[0]);
1191 * UGH! This is just about the dumbest idea I've ever heard!!
1194 linux_personality(struct thread *td, struct linux_personality_args *args)
1197 if (ldebug(personality))
1198 printf(ARGS(personality, "%lu"), (unsigned long)args->per);
1203 /* Yes Jim, it's still a Linux... */
1204 td->td_retval[0] = 0;
1208 struct l_itimerval {
1209 l_timeval it_interval;
1213 #define B2L_ITIMERVAL(bip, lip) \
1214 (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec; \
1215 (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec; \
1216 (bip)->it_value.tv_sec = (lip)->it_value.tv_sec; \
1217 (bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
1220 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
1223 struct l_itimerval ls;
1224 struct itimerval aitv, oitv;
1227 if (ldebug(setitimer))
1228 printf(ARGS(setitimer, "%p, %p"),
1229 (void *)uap->itv, (void *)uap->oitv);
1232 if (uap->itv == NULL) {
1233 uap->itv = uap->oitv;
1234 return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
1237 error = copyin(uap->itv, &ls, sizeof(ls));
1240 B2L_ITIMERVAL(&aitv, &ls);
1242 if (ldebug(setitimer)) {
1243 printf("setitimer: value: sec: %jd, usec: %ld\n",
1244 (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec);
1245 printf("setitimer: interval: sec: %jd, usec: %ld\n",
1246 (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec);
1249 error = kern_setitimer(td, uap->which, &aitv, &oitv);
1250 if (error != 0 || uap->oitv == NULL)
1252 B2L_ITIMERVAL(&ls, &oitv);
1254 return (copyout(&ls, uap->oitv, sizeof(ls)));
1258 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
1261 struct l_itimerval ls;
1262 struct itimerval aitv;
1265 if (ldebug(getitimer))
1266 printf(ARGS(getitimer, "%p"), (void *)uap->itv);
1268 error = kern_getitimer(td, uap->which, &aitv);
1271 B2L_ITIMERVAL(&ls, &aitv);
1272 return (copyout(&ls, uap->itv, sizeof(ls)));
1275 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1277 linux_nice(struct thread *td, struct linux_nice_args *args)
1279 struct setpriority_args bsd_args;
1281 bsd_args.which = PRIO_PROCESS;
1282 bsd_args.who = 0; /* current process */
1283 bsd_args.prio = args->inc;
1284 return (sys_setpriority(td, &bsd_args));
1286 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1289 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
1291 struct ucred *newcred, *oldcred;
1292 l_gid_t *linux_gidset;
1297 ngrp = args->gidsetsize;
1298 if (ngrp < 0 || ngrp >= ngroups_max + 1)
1300 linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_LINUX, M_WAITOK);
1301 error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
1305 crextend(newcred, ngrp + 1);
1308 oldcred = p->p_ucred;
1309 crcopy(newcred, oldcred);
1312 * cr_groups[0] holds egid. Setting the whole set from
1313 * the supplied set will cause egid to be changed too.
1314 * Keep cr_groups[0] unchanged to prevent that.
1317 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0) {
1324 newcred->cr_ngroups = ngrp + 1;
1326 bsd_gidset = newcred->cr_groups;
1329 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1333 newcred->cr_ngroups = 1;
1336 p->p_ucred = newcred;
1341 free(linux_gidset, M_LINUX);
1346 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1349 l_gid_t *linux_gidset;
1351 int bsd_gidsetsz, ngrp, error;
1353 cred = td->td_ucred;
1354 bsd_gidset = cred->cr_groups;
1355 bsd_gidsetsz = cred->cr_ngroups - 1;
1358 * cr_groups[0] holds egid. Returning the whole set
1359 * here will cause a duplicate. Exclude cr_groups[0]
1363 if ((ngrp = args->gidsetsize) == 0) {
1364 td->td_retval[0] = bsd_gidsetsz;
1368 if (ngrp < bsd_gidsetsz)
1372 linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset),
1374 while (ngrp < bsd_gidsetsz) {
1375 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1379 error = copyout(linux_gidset, args->grouplist, ngrp * sizeof(l_gid_t));
1380 free(linux_gidset, M_LINUX);
1384 td->td_retval[0] = ngrp;
1389 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1391 struct rlimit bsd_rlim;
1392 struct l_rlimit rlim;
1397 if (ldebug(setrlimit))
1398 printf(ARGS(setrlimit, "%d, %p"),
1399 args->resource, (void *)args->rlim);
1402 if (args->resource >= LINUX_RLIM_NLIMITS)
1405 which = linux_to_bsd_resource[args->resource];
1409 error = copyin(args->rlim, &rlim, sizeof(rlim));
1413 bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1414 bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1415 return (kern_setrlimit(td, which, &bsd_rlim));
1418 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1420 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1422 struct l_rlimit rlim;
1423 struct proc *p = td->td_proc;
1424 struct rlimit bsd_rlim;
1428 if (ldebug(old_getrlimit))
1429 printf(ARGS(old_getrlimit, "%d, %p"),
1430 args->resource, (void *)args->rlim);
1433 if (args->resource >= LINUX_RLIM_NLIMITS)
1436 which = linux_to_bsd_resource[args->resource];
1441 lim_rlimit(p, which, &bsd_rlim);
1444 #ifdef COMPAT_LINUX32
1445 rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1446 if (rlim.rlim_cur == UINT_MAX)
1447 rlim.rlim_cur = INT_MAX;
1448 rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1449 if (rlim.rlim_max == UINT_MAX)
1450 rlim.rlim_max = INT_MAX;
1452 rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1453 if (rlim.rlim_cur == ULONG_MAX)
1454 rlim.rlim_cur = LONG_MAX;
1455 rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1456 if (rlim.rlim_max == ULONG_MAX)
1457 rlim.rlim_max = LONG_MAX;
1459 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1461 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1464 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1466 struct l_rlimit rlim;
1467 struct proc *p = td->td_proc;
1468 struct rlimit bsd_rlim;
1472 if (ldebug(getrlimit))
1473 printf(ARGS(getrlimit, "%d, %p"),
1474 args->resource, (void *)args->rlim);
1477 if (args->resource >= LINUX_RLIM_NLIMITS)
1480 which = linux_to_bsd_resource[args->resource];
1485 lim_rlimit(p, which, &bsd_rlim);
1488 rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1489 rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1490 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1494 linux_sched_setscheduler(struct thread *td,
1495 struct linux_sched_setscheduler_args *args)
1497 struct sched_param sched_param;
1502 if (ldebug(sched_setscheduler))
1503 printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1504 args->pid, args->policy, (const void *)args->param);
1507 switch (args->policy) {
1508 case LINUX_SCHED_OTHER:
1509 policy = SCHED_OTHER;
1511 case LINUX_SCHED_FIFO:
1512 policy = SCHED_FIFO;
1514 case LINUX_SCHED_RR:
1521 error = copyin(args->param, &sched_param, sizeof(sched_param));
1525 tdt = linux_tdfind(td, args->pid, -1);
1529 error = kern_sched_setscheduler(td, tdt, policy, &sched_param);
1530 PROC_UNLOCK(tdt->td_proc);
1535 linux_sched_getscheduler(struct thread *td,
1536 struct linux_sched_getscheduler_args *args)
1542 if (ldebug(sched_getscheduler))
1543 printf(ARGS(sched_getscheduler, "%d"), args->pid);
1546 tdt = linux_tdfind(td, args->pid, -1);
1550 error = kern_sched_getscheduler(td, tdt, &policy);
1551 PROC_UNLOCK(tdt->td_proc);
1555 td->td_retval[0] = LINUX_SCHED_OTHER;
1558 td->td_retval[0] = LINUX_SCHED_FIFO;
1561 td->td_retval[0] = LINUX_SCHED_RR;
1568 linux_sched_get_priority_max(struct thread *td,
1569 struct linux_sched_get_priority_max_args *args)
1571 struct sched_get_priority_max_args bsd;
1574 if (ldebug(sched_get_priority_max))
1575 printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1578 switch (args->policy) {
1579 case LINUX_SCHED_OTHER:
1580 bsd.policy = SCHED_OTHER;
1582 case LINUX_SCHED_FIFO:
1583 bsd.policy = SCHED_FIFO;
1585 case LINUX_SCHED_RR:
1586 bsd.policy = SCHED_RR;
1591 return (sys_sched_get_priority_max(td, &bsd));
1595 linux_sched_get_priority_min(struct thread *td,
1596 struct linux_sched_get_priority_min_args *args)
1598 struct sched_get_priority_min_args bsd;
1601 if (ldebug(sched_get_priority_min))
1602 printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1605 switch (args->policy) {
1606 case LINUX_SCHED_OTHER:
1607 bsd.policy = SCHED_OTHER;
1609 case LINUX_SCHED_FIFO:
1610 bsd.policy = SCHED_FIFO;
1612 case LINUX_SCHED_RR:
1613 bsd.policy = SCHED_RR;
1618 return (sys_sched_get_priority_min(td, &bsd));
1621 #define REBOOT_CAD_ON 0x89abcdef
1622 #define REBOOT_CAD_OFF 0
1623 #define REBOOT_HALT 0xcdef0123
1624 #define REBOOT_RESTART 0x01234567
1625 #define REBOOT_RESTART2 0xA1B2C3D4
1626 #define REBOOT_POWEROFF 0x4321FEDC
1627 #define REBOOT_MAGIC1 0xfee1dead
1628 #define REBOOT_MAGIC2 0x28121969
1629 #define REBOOT_MAGIC2A 0x05121996
1630 #define REBOOT_MAGIC2B 0x16041998
1633 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1635 struct reboot_args bsd_args;
1639 printf(ARGS(reboot, "0x%x"), args->cmd);
1642 if (args->magic1 != REBOOT_MAGIC1)
1645 switch (args->magic2) {
1647 case REBOOT_MAGIC2A:
1648 case REBOOT_MAGIC2B:
1654 switch (args->cmd) {
1656 case REBOOT_CAD_OFF:
1657 return (priv_check(td, PRIV_REBOOT));
1659 bsd_args.opt = RB_HALT;
1661 case REBOOT_RESTART:
1662 case REBOOT_RESTART2:
1665 case REBOOT_POWEROFF:
1666 bsd_args.opt = RB_POWEROFF;
1671 return (sys_reboot(td, &bsd_args));
1676 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1677 * td->td_retval[1] when COMPAT_43 is defined. This clobbers registers that
1678 * are assumed to be preserved. The following lightweight syscalls fixes
1679 * this. See also linux_getgid16() and linux_getuid16() in linux_uid16.c
1681 * linux_getpid() - MP SAFE
1682 * linux_getgid() - MP SAFE
1683 * linux_getuid() - MP SAFE
1687 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1692 printf(ARGS(getpid, ""));
1694 td->td_retval[0] = td->td_proc->p_pid;
1700 linux_gettid(struct thread *td, struct linux_gettid_args *args)
1702 struct linux_emuldata *em;
1706 printf(ARGS(gettid, ""));
1710 KASSERT(em != NULL, ("gettid: emuldata not found.\n"));
1712 td->td_retval[0] = em->em_tid;
1719 linux_getppid(struct thread *td, struct linux_getppid_args *args)
1723 if (ldebug(getppid))
1724 printf(ARGS(getppid, ""));
1727 PROC_LOCK(td->td_proc);
1728 td->td_retval[0] = td->td_proc->p_pptr->p_pid;
1729 PROC_UNLOCK(td->td_proc);
1734 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1739 printf(ARGS(getgid, ""));
1742 td->td_retval[0] = td->td_ucred->cr_rgid;
1747 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1752 printf(ARGS(getuid, ""));
1755 td->td_retval[0] = td->td_ucred->cr_ruid;
1761 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1763 struct getsid_args bsd;
1767 printf(ARGS(getsid, "%i"), args->pid);
1770 bsd.pid = args->pid;
1771 return (sys_getsid(td, &bsd));
1775 linux_nosys(struct thread *td, struct nosys_args *ignore)
1782 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1784 struct getpriority_args bsd_args;
1788 if (ldebug(getpriority))
1789 printf(ARGS(getpriority, "%i, %i"), args->which, args->who);
1792 bsd_args.which = args->which;
1793 bsd_args.who = args->who;
1794 error = sys_getpriority(td, &bsd_args);
1795 td->td_retval[0] = 20 - td->td_retval[0];
1800 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1805 if (ldebug(sethostname))
1806 printf(ARGS(sethostname, "*, %i"), args->len);
1810 name[1] = KERN_HOSTNAME;
1811 return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1816 linux_setdomainname(struct thread *td, struct linux_setdomainname_args *args)
1821 if (ldebug(setdomainname))
1822 printf(ARGS(setdomainname, "*, %i"), args->len);
1826 name[1] = KERN_NISDOMAINNAME;
1827 return (userland_sysctl(td, name, 2, 0, 0, 0, args->name,
1832 linux_exit_group(struct thread *td, struct linux_exit_group_args *args)
1836 if (ldebug(exit_group))
1837 printf(ARGS(exit_group, "%i"), args->error_code);
1840 LINUX_CTR2(exit_group, "thread(%d) (%d)", td->td_tid,
1844 * XXX: we should send a signal to the parent if
1845 * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?)
1846 * as it doesnt occur often.
1848 exit1(td, W_EXITCODE(args->error_code, 0));
1852 #define _LINUX_CAPABILITY_VERSION 0x19980330
1854 struct l_user_cap_header {
1859 struct l_user_cap_data {
1866 linux_capget(struct thread *td, struct linux_capget_args *args)
1868 struct l_user_cap_header luch;
1869 struct l_user_cap_data lucd;
1872 if (args->hdrp == NULL)
1875 error = copyin(args->hdrp, &luch, sizeof(luch));
1879 if (luch.version != _LINUX_CAPABILITY_VERSION) {
1880 luch.version = _LINUX_CAPABILITY_VERSION;
1881 error = copyout(&luch, args->hdrp, sizeof(luch));
1892 * The current implementation doesn't support setting
1893 * a capability (it's essentially a stub) so indicate
1894 * that no capabilities are currently set or available
1897 bzero (&lucd, sizeof(lucd));
1898 error = copyout(&lucd, args->datap, sizeof(lucd));
1905 linux_capset(struct thread *td, struct linux_capset_args *args)
1907 struct l_user_cap_header luch;
1908 struct l_user_cap_data lucd;
1911 if (args->hdrp == NULL || args->datap == NULL)
1914 error = copyin(args->hdrp, &luch, sizeof(luch));
1918 if (luch.version != _LINUX_CAPABILITY_VERSION) {
1919 luch.version = _LINUX_CAPABILITY_VERSION;
1920 error = copyout(&luch, args->hdrp, sizeof(luch));
1929 error = copyin(args->datap, &lucd, sizeof(lucd));
1933 /* We currently don't support setting any capabilities. */
1934 if (lucd.effective || lucd.permitted || lucd.inheritable) {
1936 "capset effective=0x%x, permitted=0x%x, "
1937 "inheritable=0x%x is not implemented",
1938 (int)lucd.effective, (int)lucd.permitted,
1939 (int)lucd.inheritable);
1947 linux_prctl(struct thread *td, struct linux_prctl_args *args)
1949 int error = 0, max_size;
1950 struct proc *p = td->td_proc;
1951 char comm[LINUX_MAX_COMM_LEN];
1952 struct linux_emuldata *em;
1957 printf(ARGS(prctl, "%d, %ju, %ju, %ju, %ju"), args->option,
1958 (uintmax_t)args->arg2, (uintmax_t)args->arg3,
1959 (uintmax_t)args->arg4, (uintmax_t)args->arg5);
1962 switch (args->option) {
1963 case LINUX_PR_SET_PDEATHSIG:
1964 if (!LINUX_SIG_VALID(args->arg2))
1967 KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1968 em->pdeath_signal = args->arg2;
1970 case LINUX_PR_GET_PDEATHSIG:
1972 KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1973 pdeath_signal = em->pdeath_signal;
1974 error = copyout(&pdeath_signal,
1975 (void *)(register_t)args->arg2,
1976 sizeof(pdeath_signal));
1978 case LINUX_PR_GET_KEEPCAPS:
1980 * Indicate that we always clear the effective and
1981 * permitted capability sets when the user id becomes
1982 * non-zero (actually the capability sets are simply
1983 * always zero in the current implementation).
1985 td->td_retval[0] = 0;
1987 case LINUX_PR_SET_KEEPCAPS:
1989 * Ignore requests to keep the effective and permitted
1990 * capability sets when the user id becomes non-zero.
1993 case LINUX_PR_SET_NAME:
1995 * To be on the safe side we need to make sure to not
1996 * overflow the size a linux program expects. We already
1997 * do this here in the copyin, so that we don't need to
2000 max_size = MIN(sizeof(comm), sizeof(p->p_comm));
2001 error = copyinstr((void *)(register_t)args->arg2, comm,
2004 /* Linux silently truncates the name if it is too long. */
2005 if (error == ENAMETOOLONG) {
2007 * XXX: copyinstr() isn't documented to populate the
2008 * array completely, so do a copyin() to be on the
2009 * safe side. This should be changed in case
2010 * copyinstr() is changed to guarantee this.
2012 error = copyin((void *)(register_t)args->arg2, comm,
2014 comm[max_size - 1] = '\0';
2020 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
2023 case LINUX_PR_GET_NAME:
2025 strlcpy(comm, p->p_comm, sizeof(comm));
2027 error = copyout(comm, (void *)(register_t)args->arg2,
2039 linux_sched_setparam(struct thread *td,
2040 struct linux_sched_setparam_args *uap)
2042 struct sched_param sched_param;
2047 if (ldebug(sched_setparam))
2048 printf(ARGS(sched_setparam, "%d, *"), uap->pid);
2051 error = copyin(uap->param, &sched_param, sizeof(sched_param));
2055 tdt = linux_tdfind(td, uap->pid, -1);
2059 error = kern_sched_setparam(td, tdt, &sched_param);
2060 PROC_UNLOCK(tdt->td_proc);
2065 linux_sched_getparam(struct thread *td,
2066 struct linux_sched_getparam_args *uap)
2068 struct sched_param sched_param;
2073 if (ldebug(sched_getparam))
2074 printf(ARGS(sched_getparam, "%d, *"), uap->pid);
2077 tdt = linux_tdfind(td, uap->pid, -1);
2081 error = kern_sched_getparam(td, tdt, &sched_param);
2082 PROC_UNLOCK(tdt->td_proc);
2084 error = copyout(&sched_param, uap->param,
2085 sizeof(sched_param));
2090 * Get affinity of a process.
2093 linux_sched_getaffinity(struct thread *td,
2094 struct linux_sched_getaffinity_args *args)
2098 struct cpuset_getaffinity_args cga;
2101 if (ldebug(sched_getaffinity))
2102 printf(ARGS(sched_getaffinity, "%d, %d, *"), args->pid,
2105 if (args->len < sizeof(cpuset_t))
2108 tdt = linux_tdfind(td, args->pid, -1);
2112 PROC_UNLOCK(tdt->td_proc);
2113 cga.level = CPU_LEVEL_WHICH;
2114 cga.which = CPU_WHICH_TID;
2115 cga.id = tdt->td_tid;
2116 cga.cpusetsize = sizeof(cpuset_t);
2117 cga.mask = (cpuset_t *) args->user_mask_ptr;
2119 if ((error = sys_cpuset_getaffinity(td, &cga)) == 0)
2120 td->td_retval[0] = sizeof(cpuset_t);
2126 * Set affinity of a process.
2129 linux_sched_setaffinity(struct thread *td,
2130 struct linux_sched_setaffinity_args *args)
2132 struct cpuset_setaffinity_args csa;
2136 if (ldebug(sched_setaffinity))
2137 printf(ARGS(sched_setaffinity, "%d, %d, *"), args->pid,
2140 if (args->len < sizeof(cpuset_t))
2143 tdt = linux_tdfind(td, args->pid, -1);
2147 PROC_UNLOCK(tdt->td_proc);
2148 csa.level = CPU_LEVEL_WHICH;
2149 csa.which = CPU_WHICH_TID;
2150 csa.id = tdt->td_tid;
2151 csa.cpusetsize = sizeof(cpuset_t);
2152 csa.mask = (cpuset_t *) args->user_mask_ptr;
2154 return (sys_cpuset_setaffinity(td, &csa));
2157 struct linux_rlimit64 {
2163 linux_prlimit64(struct thread *td, struct linux_prlimit64_args *args)
2165 struct rlimit rlim, nrlim;
2166 struct linux_rlimit64 lrlim;
2173 if (ldebug(prlimit64))
2174 printf(ARGS(prlimit64, "%d, %d, %p, %p"), args->pid,
2175 args->resource, (void *)args->new, (void *)args->old);
2178 if (args->resource >= LINUX_RLIM_NLIMITS)
2181 which = linux_to_bsd_resource[args->resource];
2185 if (args->new != NULL) {
2187 * Note. Unlike FreeBSD where rlim is signed 64-bit Linux
2188 * rlim is unsigned 64-bit. FreeBSD treats negative limits
2189 * as INFINITY so we do not need a conversion even.
2191 error = copyin(args->new, &nrlim, sizeof(nrlim));
2196 flags = PGET_HOLD | PGET_NOTWEXIT;
2197 if (args->new != NULL)
2198 flags |= PGET_CANDEBUG;
2200 flags |= PGET_CANSEE;
2201 error = pget(args->pid, flags, &p);
2205 if (args->old != NULL) {
2207 lim_rlimit(p, which, &rlim);
2209 if (rlim.rlim_cur == RLIM_INFINITY)
2210 lrlim.rlim_cur = LINUX_RLIM_INFINITY;
2212 lrlim.rlim_cur = rlim.rlim_cur;
2213 if (rlim.rlim_max == RLIM_INFINITY)
2214 lrlim.rlim_max = LINUX_RLIM_INFINITY;
2216 lrlim.rlim_max = rlim.rlim_max;
2217 error = copyout(&lrlim, args->old, sizeof(lrlim));
2222 if (args->new != NULL)
2223 error = kern_proc_setrlimit(td, p, which, &nrlim);
2231 linux_pselect6(struct thread *td, struct linux_pselect6_args *args)
2233 struct timeval utv, tv0, tv1, *tvp;
2234 struct l_pselect6arg lpse6;
2235 struct l_timespec lts;
2236 struct timespec uts;
2243 if (args->sig != NULL) {
2244 error = copyin(args->sig, &lpse6, sizeof(lpse6));
2247 if (lpse6.ss_len != sizeof(l_ss))
2249 if (lpse6.ss != 0) {
2250 error = copyin(PTRIN(lpse6.ss), &l_ss,
2254 linux_to_bsd_sigset(&l_ss, &ss);
2260 * Currently glibc changes nanosecond number to microsecond.
2261 * This mean losing precision but for now it is hardly seen.
2263 if (args->tsp != NULL) {
2264 error = copyin(args->tsp, <s, sizeof(lts));
2267 error = linux_to_native_timespec(&uts, <s);
2271 TIMESPEC_TO_TIMEVAL(&utv, &uts);
2272 if (itimerfix(&utv))
2280 error = kern_pselect(td, args->nfds, args->readfds, args->writefds,
2281 args->exceptfds, tvp, ssp, LINUX_NFDBITS);
2283 if (error == 0 && args->tsp != NULL) {
2284 if (td->td_retval[0] != 0) {
2286 * Compute how much time was left of the timeout,
2287 * by subtracting the current time and the time
2288 * before we started the call, and subtracting
2289 * that result from the user-supplied value.
2293 timevalsub(&tv1, &tv0);
2294 timevalsub(&utv, &tv1);
2300 TIMEVAL_TO_TIMESPEC(&utv, &uts);
2302 native_to_linux_timespec(<s, &uts);
2303 error = copyout(<s, args->tsp, sizeof(lts));
2310 linux_ppoll(struct thread *td, struct linux_ppoll_args *args)
2312 struct timespec ts0, ts1;
2313 struct l_timespec lts;
2314 struct timespec uts, *tsp;
2320 if (args->sset != NULL) {
2321 if (args->ssize != sizeof(l_ss))
2323 error = copyin(args->sset, &l_ss, sizeof(l_ss));
2326 linux_to_bsd_sigset(&l_ss, &ss);
2330 if (args->tsp != NULL) {
2331 error = copyin(args->tsp, <s, sizeof(lts));
2334 error = linux_to_native_timespec(&uts, <s);
2343 error = kern_poll(td, args->fds, args->nfds, tsp, ssp);
2345 if (error == 0 && args->tsp != NULL) {
2346 if (td->td_retval[0]) {
2348 timespecsub(&ts1, &ts0);
2349 timespecsub(&uts, &ts1);
2351 timespecclear(&uts);
2353 timespecclear(&uts);
2355 native_to_linux_timespec(<s, &uts);
2356 error = copyout(<s, args->tsp, sizeof(lts));
2362 #if defined(DEBUG) || defined(KTR)
2363 /* XXX: can be removed when every ldebug(...) and KTR stuff are removed. */
2365 #ifdef COMPAT_LINUX32
2366 #define L_MAXSYSCALL LINUX32_SYS_MAXSYSCALL
2368 #define L_MAXSYSCALL LINUX_SYS_MAXSYSCALL
2371 u_char linux_debug_map[howmany(L_MAXSYSCALL, sizeof(u_char))];
2374 linux_debug(int syscall, int toggle, int global)
2378 char c = toggle ? 0 : 0xff;
2380 memset(linux_debug_map, c, sizeof(linux_debug_map));
2383 if (syscall < 0 || syscall >= L_MAXSYSCALL)
2386 clrbit(linux_debug_map, syscall);
2388 setbit(linux_debug_map, syscall);
2394 * Usage: sysctl linux.debug=<syscall_nr>.<0/1>
2396 * E.g.: sysctl linux.debug=21.0
2398 * As a special case, syscall "all" will apply to all syscalls globally.
2400 #define LINUX_MAX_DEBUGSTR 16
2402 linux_sysctl_debug(SYSCTL_HANDLER_ARGS)
2404 char value[LINUX_MAX_DEBUGSTR], *p;
2405 int error, sysc, toggle;
2409 error = sysctl_handle_string(oidp, value, LINUX_MAX_DEBUGSTR, req);
2410 if (error || req->newptr == NULL)
2412 for (p = value; *p != '\0' && *p != '.'; p++);
2416 sysc = strtol(value, NULL, 0);
2417 toggle = strtol(p, NULL, 0);
2418 if (strcmp(value, "all") == 0)
2420 error = linux_debug(sysc, toggle, global);
2424 #endif /* DEBUG || KTR */
2427 linux_sched_rr_get_interval(struct thread *td,
2428 struct linux_sched_rr_get_interval_args *uap)
2431 struct l_timespec lts;
2436 * According to man in case the invalid pid specified
2437 * EINVAL should be returned.
2442 tdt = linux_tdfind(td, uap->pid, -1);
2446 error = kern_sched_rr_get_interval_td(td, tdt, &ts);
2447 PROC_UNLOCK(tdt->td_proc);
2450 native_to_linux_timespec(<s, &ts);
2451 return (copyout(<s, uap->interval, sizeof(lts)));
2455 * In case when the Linux thread is the initial thread in
2456 * the thread group thread id is equal to the process id.
2457 * Glibc depends on this magic (assert in pthread_getattr_np.c).
2460 linux_tdfind(struct thread *td, lwpid_t tid, pid_t pid)
2462 struct linux_emuldata *em;
2467 if (tid == 0 || tid == td->td_tid) {
2469 PROC_LOCK(tdt->td_proc);
2470 } else if (tid > PID_MAX)
2471 tdt = tdfind(tid, pid);
2474 * Initial thread where the tid equal to the pid.
2478 if (SV_PROC_ABI(p) != SV_ABI_LINUX) {
2480 * p is not a Linuxulator process.
2485 FOREACH_THREAD_IN_PROC(p, tdt) {
2487 if (tid == em->em_tid)
2499 linux_to_bsd_waitopts(int options, int *bsdopts)
2502 if (options & LINUX_WNOHANG)
2503 *bsdopts |= WNOHANG;
2504 if (options & LINUX_WUNTRACED)
2505 *bsdopts |= WUNTRACED;
2506 if (options & LINUX_WEXITED)
2507 *bsdopts |= WEXITED;
2508 if (options & LINUX_WCONTINUED)
2509 *bsdopts |= WCONTINUED;
2510 if (options & LINUX_WNOWAIT)
2511 *bsdopts |= WNOWAIT;
2513 if (options & __WCLONE)
2514 *bsdopts |= WLINUXCLONE;