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;
153 bzero(&sysinfo, sizeof(sysinfo));
157 sysinfo.uptime = ts.tv_sec;
159 /* Use the information from the mib to get our load averages */
160 for (i = 0; i < 3; i++)
161 sysinfo.loads[i] = averunnable.ldavg[i] *
162 LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale;
164 sysinfo.totalram = physmem * PAGE_SIZE;
165 sysinfo.freeram = sysinfo.totalram - cnt.v_wire_count * PAGE_SIZE;
167 sysinfo.sharedram = 0;
168 mtx_lock(&vm_object_list_mtx);
169 TAILQ_FOREACH(object, &vm_object_list, object_list)
170 if (object->shadow_count > 1)
171 sysinfo.sharedram += object->resident_page_count;
172 mtx_unlock(&vm_object_list_mtx);
174 sysinfo.sharedram *= PAGE_SIZE;
175 sysinfo.bufferram = 0;
177 swap_pager_status(&i, &j);
178 sysinfo.totalswap = i * PAGE_SIZE;
179 sysinfo.freeswap = (i - j) * PAGE_SIZE;
181 sysinfo.procs = nprocs;
183 /* The following are only present in newer Linux kernels. */
184 sysinfo.totalbig = 0;
186 sysinfo.mem_unit = 1;
188 return (copyout(&sysinfo, args->info, sizeof(sysinfo)));
192 linux_alarm(struct thread *td, struct linux_alarm_args *args)
194 struct itimerval it, old_it;
200 printf(ARGS(alarm, "%u"), args->secs);
204 * Linux alarm() is always successful. Limit secs to INT32_MAX / 2
205 * to match kern_setitimer()'s limit to avoid error from it.
207 * XXX. Linux limit secs to INT_MAX on 32 and does not limit on 64-bit
210 if (secs > INT32_MAX / 2)
211 secs = INT32_MAX / 2;
213 it.it_value.tv_sec = secs;
214 it.it_value.tv_usec = 0;
215 timevalclear(&it.it_interval);
216 error = kern_setitimer(td, ITIMER_REAL, &it, &old_it);
217 KASSERT(error == 0, ("kern_setitimer returns %d", error));
219 if ((old_it.it_value.tv_sec == 0 && old_it.it_value.tv_usec > 0) ||
220 old_it.it_value.tv_usec >= 500000)
221 old_it.it_value.tv_sec++;
222 td->td_retval[0] = old_it.it_value.tv_sec;
227 linux_brk(struct thread *td, struct linux_brk_args *args)
229 struct vmspace *vm = td->td_proc->p_vmspace;
230 vm_offset_t new, old;
231 struct obreak_args /* {
237 printf(ARGS(brk, "%p"), (void *)(uintptr_t)args->dsend);
239 old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
240 new = (vm_offset_t)args->dsend;
241 tmp.nsize = (char *)new;
242 if (((caddr_t)new > vm->vm_daddr) && !sys_obreak(td, &tmp))
243 td->td_retval[0] = (long)new;
245 td->td_retval[0] = (long)old;
250 #if defined(__i386__)
251 /* XXX: what about amd64/linux32? */
254 linux_uselib(struct thread *td, struct linux_uselib_args *args)
261 unsigned long file_offset;
262 unsigned long bss_size;
265 int error, locked, writecount;
267 LCONVPATHEXIST(td, args->library, &library);
271 printf(ARGS(uselib, "%s"), library);
278 NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | AUDITVNODE1,
279 UIO_SYSSPACE, library, td);
286 NDFREE(&ni, NDF_ONLY_PNBUF);
289 * From here on down, we have a locked vnode that must be unlocked.
290 * XXX: The code below largely duplicates exec_check_permissions().
295 error = VOP_GET_WRITECOUNT(vp, &writecount);
298 if (writecount != 0) {
304 error = VOP_GETATTR(vp, &attr, td->td_ucred);
308 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
309 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
310 /* EACCESS is what exec(2) returns. */
316 if (attr.va_size == 0) {
321 /* Can we access it? */
322 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
327 * XXX: This should use vn_open() so that it is properly authorized,
328 * and to reduce code redundancy all over the place here.
329 * XXX: Not really, it duplicates far more of exec_check_permissions()
333 error = mac_vnode_check_open(td->td_ucred, vp, VREAD);
337 error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL);
341 /* Pull in executable header into exec_map */
342 error = vm_mmap(exec_map, (vm_offset_t *)&a_out, PAGE_SIZE,
343 VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 0);
347 /* Is it a Linux binary ? */
348 if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
354 * While we are here, we should REALLY do some more checks
357 /* Set file/virtual offset based on a.out variant. */
358 switch ((int)(a_out->a_magic & 0xffff)) {
359 case 0413: /* ZMAGIC */
362 case 0314: /* QMAGIC */
370 bss_size = round_page(a_out->a_bss);
372 /* Check various fields in header for validity/bounds. */
373 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
378 /* text + data can't exceed file size */
379 if (a_out->a_data + a_out->a_text > attr.va_size) {
385 * text/data/bss must not exceed limits
386 * XXX - this is not complete. it should check current usage PLUS
387 * the resources needed by this library.
389 PROC_LOCK(td->td_proc);
390 if (a_out->a_text > maxtsiz ||
391 a_out->a_data + bss_size > lim_cur(td->td_proc, RLIMIT_DATA) ||
392 racct_set(td->td_proc, RACCT_DATA, a_out->a_data +
394 PROC_UNLOCK(td->td_proc);
398 PROC_UNLOCK(td->td_proc);
401 * Prevent more writers.
402 * XXX: Note that if any of the VM operations fail below we don't
408 * Lock no longer needed
414 * Check if file_offset page aligned. Currently we cannot handle
415 * misalinged file offsets, and so we read in the entire image
418 if (file_offset & PAGE_MASK) {
420 printf("uselib: Non page aligned binary %lu\n", file_offset);
422 /* Map text+data read/write/execute */
424 /* a_entry is the load address and is page aligned */
425 vmaddr = trunc_page(a_out->a_entry);
427 /* get anon user mapping, read+write+execute */
428 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
429 &vmaddr, a_out->a_text + a_out->a_data, 0, VMFS_NO_SPACE,
430 VM_PROT_ALL, VM_PROT_ALL, 0);
434 error = vn_rdwr(UIO_READ, vp, (void *)vmaddr, file_offset,
435 a_out->a_text + a_out->a_data, UIO_USERSPACE, 0,
436 td->td_ucred, NOCRED, &aresid, td);
445 printf("uselib: Page aligned binary %lu\n", file_offset);
448 * for QMAGIC, a_entry is 20 bytes beyond the load address
449 * to skip the executable header
451 vmaddr = trunc_page(a_out->a_entry);
454 * Map it all into the process's space as a single
455 * copy-on-write "data" segment.
457 error = vm_mmap(&td->td_proc->p_vmspace->vm_map, &vmaddr,
458 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
459 MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset);
464 printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long *)vmaddr)[0],
465 ((long *)vmaddr)[1]);
468 /* Calculate BSS start address */
469 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
472 /* allocate some 'anon' space */
473 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
474 &vmaddr, bss_size, 0, VMFS_NO_SPACE, VM_PROT_ALL,
481 /* Unlock vnode if needed */
485 /* Release the temporary mapping. */
487 kmap_free_wakeup(exec_map, (vm_offset_t)a_out, PAGE_SIZE);
492 #endif /* __i386__ */
495 linux_select(struct thread *td, struct linux_select_args *args)
498 struct timeval tv0, tv1, utv, *tvp;
503 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
504 (void *)args->readfds, (void *)args->writefds,
505 (void *)args->exceptfds, (void *)args->timeout);
509 * Store current time for computation of the amount of
513 if ((error = copyin(args->timeout, <v, sizeof(ltv))))
515 utv.tv_sec = ltv.tv_sec;
516 utv.tv_usec = ltv.tv_usec;
519 printf(LMSG("incoming timeout (%jd/%ld)"),
520 (intmax_t)utv.tv_sec, utv.tv_usec);
523 if (itimerfix(&utv)) {
525 * The timeval was invalid. Convert it to something
526 * valid that will act as it does under Linux.
528 utv.tv_sec += utv.tv_usec / 1000000;
529 utv.tv_usec %= 1000000;
530 if (utv.tv_usec < 0) {
532 utv.tv_usec += 1000000;
542 error = kern_select(td, args->nfds, args->readfds, args->writefds,
543 args->exceptfds, tvp, LINUX_NFDBITS);
547 printf(LMSG("real select returns %d"), error);
553 if (td->td_retval[0]) {
555 * Compute how much time was left of the timeout,
556 * by subtracting the current time and the time
557 * before we started the call, and subtracting
558 * that result from the user-supplied value.
561 timevalsub(&tv1, &tv0);
562 timevalsub(&utv, &tv1);
569 printf(LMSG("outgoing timeout (%jd/%ld)"),
570 (intmax_t)utv.tv_sec, utv.tv_usec);
572 ltv.tv_sec = utv.tv_sec;
573 ltv.tv_usec = utv.tv_usec;
574 if ((error = copyout(<v, args->timeout, sizeof(ltv))))
581 printf(LMSG("select_out -> %d"), error);
587 linux_mremap(struct thread *td, struct linux_mremap_args *args)
589 struct munmap_args /* {
597 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
598 (void *)(uintptr_t)args->addr,
599 (unsigned long)args->old_len,
600 (unsigned long)args->new_len,
601 (unsigned long)args->flags);
604 if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) {
605 td->td_retval[0] = 0;
610 * Check for the page alignment.
611 * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK.
613 if (args->addr & PAGE_MASK) {
614 td->td_retval[0] = 0;
618 args->new_len = round_page(args->new_len);
619 args->old_len = round_page(args->old_len);
621 if (args->new_len > args->old_len) {
622 td->td_retval[0] = 0;
626 if (args->new_len < args->old_len) {
628 (caddr_t)((uintptr_t)args->addr + args->new_len);
629 bsd_args.len = args->old_len - args->new_len;
630 error = sys_munmap(td, &bsd_args);
633 td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
637 #define LINUX_MS_ASYNC 0x0001
638 #define LINUX_MS_INVALIDATE 0x0002
639 #define LINUX_MS_SYNC 0x0004
642 linux_msync(struct thread *td, struct linux_msync_args *args)
644 struct msync_args bsd_args;
646 bsd_args.addr = (caddr_t)(uintptr_t)args->addr;
647 bsd_args.len = (uintptr_t)args->len;
648 bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
650 return (sys_msync(td, &bsd_args));
654 linux_time(struct thread *td, struct linux_time_args *args)
662 printf(ARGS(time, "*"));
667 if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
669 td->td_retval[0] = tm;
673 struct l_times_argv {
676 l_clock_t tms_cutime;
677 l_clock_t tms_cstime;
682 * Glibc versions prior to 2.2.1 always use hard-coded CLK_TCK value.
683 * Since 2.2.1 Glibc uses value exported from kernel via AT_CLKTCK
684 * auxiliary vector entry.
688 #define CONVOTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
689 #define CONVNTCK(r) (r.tv_sec * stclohz + r.tv_usec / (1000000 / stclohz))
691 #define CONVTCK(r) (linux_kernver(td) >= LINUX_KERNVER_2004000 ? \
692 CONVNTCK(r) : CONVOTCK(r))
695 linux_times(struct thread *td, struct linux_times_args *args)
697 struct timeval tv, utime, stime, cutime, cstime;
698 struct l_times_argv tms;
704 printf(ARGS(times, "*"));
707 if (args->buf != NULL) {
711 calcru(p, &utime, &stime);
713 calccru(p, &cutime, &cstime);
716 tms.tms_utime = CONVTCK(utime);
717 tms.tms_stime = CONVTCK(stime);
719 tms.tms_cutime = CONVTCK(cutime);
720 tms.tms_cstime = CONVTCK(cstime);
722 if ((error = copyout(&tms, args->buf, sizeof(tms))))
727 td->td_retval[0] = (int)CONVTCK(tv);
732 linux_newuname(struct thread *td, struct linux_newuname_args *args)
734 struct l_new_utsname utsname;
735 char osname[LINUX_MAX_UTSNAME];
736 char osrelease[LINUX_MAX_UTSNAME];
740 if (ldebug(newuname))
741 printf(ARGS(newuname, "*"));
744 linux_get_osname(td, osname);
745 linux_get_osrelease(td, osrelease);
747 bzero(&utsname, sizeof(utsname));
748 strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
749 getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
750 getcreddomainname(td->td_ucred, utsname.domainname, LINUX_MAX_UTSNAME);
751 strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
752 strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
753 for (p = utsname.version; *p != '\0'; ++p)
758 strlcpy(utsname.machine, linux_kplatform, LINUX_MAX_UTSNAME);
760 return (copyout(&utsname, args->buf, sizeof(utsname)));
769 linux_utime(struct thread *td, struct linux_utime_args *args)
771 struct timeval tv[2], *tvp;
772 struct l_utimbuf lut;
776 LCONVPATHEXIST(td, args->fname, &fname);
780 printf(ARGS(utime, "%s, *"), fname);
784 if ((error = copyin(args->times, &lut, sizeof lut))) {
788 tv[0].tv_sec = lut.l_actime;
790 tv[1].tv_sec = lut.l_modtime;
796 error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
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_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
834 linux_utimensat_nsec_valid(l_long nsec)
837 if (nsec == LINUX_UTIME_OMIT || nsec == LINUX_UTIME_NOW)
839 if (nsec >= 0 && nsec <= 999999999)
845 linux_utimensat(struct thread *td, struct linux_utimensat_args *args)
847 struct l_timespec l_times[2];
848 struct timespec times[2], *timesp = NULL;
850 int error, dfd, flags = 0;
852 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
855 if (ldebug(utimensat))
856 printf(ARGS(utimensat, "%d, *"), dfd);
859 if (args->flags & ~LINUX_AT_SYMLINK_NOFOLLOW)
862 if (args->times != NULL) {
863 error = copyin(args->times, l_times, sizeof(l_times));
867 if (linux_utimensat_nsec_valid(l_times[0].tv_nsec) != 0 ||
868 linux_utimensat_nsec_valid(l_times[1].tv_nsec) != 0)
871 times[0].tv_sec = l_times[0].tv_sec;
872 switch (l_times[0].tv_nsec)
874 case LINUX_UTIME_OMIT:
875 times[0].tv_nsec = UTIME_OMIT;
877 case LINUX_UTIME_NOW:
878 times[0].tv_nsec = UTIME_NOW;
881 times[0].tv_nsec = l_times[0].tv_nsec;
884 times[1].tv_sec = l_times[1].tv_sec;
885 switch (l_times[1].tv_nsec)
887 case LINUX_UTIME_OMIT:
888 times[1].tv_nsec = UTIME_OMIT;
890 case LINUX_UTIME_NOW:
891 times[1].tv_nsec = UTIME_NOW;
894 times[1].tv_nsec = l_times[1].tv_nsec;
899 /* This breaks POSIX, but is what the Linux kernel does
900 * _on purpose_ (documented in the man page for utimensat(2)),
901 * so we must follow that behaviour. */
902 if (times[0].tv_nsec == UTIME_OMIT &&
903 times[1].tv_nsec == UTIME_OMIT)
907 if (args->pathname != NULL)
908 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
909 else if (args->flags != 0)
912 if (args->flags & LINUX_AT_SYMLINK_NOFOLLOW)
913 flags |= AT_SYMLINK_NOFOLLOW;
916 error = kern_futimens(td, dfd, timesp, UIO_SYSSPACE);
918 error = kern_utimensat(td, dfd, path, UIO_SYSSPACE, timesp,
919 UIO_SYSSPACE, flags);
927 linux_futimesat(struct thread *td, struct linux_futimesat_args *args)
930 struct timeval tv[2], *tvp = NULL;
934 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
935 LCONVPATHEXIST_AT(td, args->filename, &fname, dfd);
938 if (ldebug(futimesat))
939 printf(ARGS(futimesat, "%s, *"), fname);
942 if (args->utimes != NULL) {
943 if ((error = copyin(args->utimes, ltv, sizeof ltv))) {
947 tv[0].tv_sec = ltv[0].tv_sec;
948 tv[0].tv_usec = ltv[0].tv_usec;
949 tv[1].tv_sec = ltv[1].tv_sec;
950 tv[1].tv_usec = ltv[1].tv_usec;
954 error = kern_utimesat(td, dfd, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
960 linux_common_wait(struct thread *td, int pid, int *status,
961 int options, struct rusage *ru)
965 error = kern_wait(td, pid, &tmpstat, options, ru);
971 if (WIFSIGNALED(tmpstat))
972 tmpstat = (tmpstat & 0xffffff80) |
973 bsd_to_linux_signal(WTERMSIG(tmpstat));
974 else if (WIFSTOPPED(tmpstat))
975 tmpstat = (tmpstat & 0xffff00ff) |
976 (bsd_to_linux_signal(WSTOPSIG(tmpstat)) << 8);
977 else if (WIFCONTINUED(tmpstat))
979 error = copyout(&tmpstat, status, sizeof(int));
985 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
987 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
989 struct linux_wait4_args wait4_args;
993 printf(ARGS(waitpid, "%d, %p, %d"),
994 args->pid, (void *)args->status, args->options);
997 wait4_args.pid = args->pid;
998 wait4_args.status = args->status;
999 wait4_args.options = args->options;
1000 wait4_args.rusage = NULL;
1002 return (linux_wait4(td, &wait4_args));
1004 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1007 linux_wait4(struct thread *td, struct linux_wait4_args *args)
1010 struct rusage ru, *rup;
1014 printf(ARGS(wait4, "%d, %p, %d, %p"),
1015 args->pid, (void *)args->status, args->options,
1016 (void *)args->rusage);
1018 if (args->options & ~(LINUX_WUNTRACED | LINUX_WNOHANG |
1019 LINUX_WCONTINUED | __WCLONE | __WNOTHREAD | __WALL))
1023 linux_to_bsd_waitopts(args->options, &options);
1025 if (args->rusage != NULL)
1029 error = linux_common_wait(td, args->pid, args->status, options, rup);
1032 if (args->rusage != NULL)
1033 error = linux_copyout_rusage(&ru, args->rusage);
1038 linux_waitid(struct thread *td, struct linux_waitid_args *args)
1040 int status, options, sig;
1041 struct __wrusage wru;
1049 linux_to_bsd_waitopts(args->options, &options);
1051 if (options & ~(WNOHANG | WNOWAIT | WEXITED | WUNTRACED | WCONTINUED))
1053 if (!(options & (WEXITED | WUNTRACED | WCONTINUED)))
1056 switch (args->idtype) {
1074 error = kern_wait6(td, idtype, args->id, &status, options,
1078 if (args->rusage != NULL) {
1079 error = linux_copyout_rusage(&wru.wru_children,
1084 if (args->info != NULL) {
1086 if (td->td_retval[0] == 0)
1087 bzero(&lsi, sizeof(lsi));
1089 sig = bsd_to_linux_signal(siginfo.si_signo);
1090 siginfo_to_lsiginfo(&siginfo, &lsi, sig);
1092 error = copyout(&lsi, args->info, sizeof(lsi));
1094 td->td_retval[0] = 0;
1100 linux_mknod(struct thread *td, struct linux_mknod_args *args)
1105 LCONVPATHCREAT(td, args->path, &path);
1109 printf(ARGS(mknod, "%s, %d, %ju"), path, args->mode,
1110 (uintmax_t)args->dev);
1113 switch (args->mode & S_IFMT) {
1116 error = kern_mkfifo(td, path, UIO_SYSSPACE, args->mode);
1121 error = kern_mknod(td, path, UIO_SYSSPACE, args->mode,
1130 args->mode |= S_IFREG;
1133 error = kern_open(td, path, UIO_SYSSPACE,
1134 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1136 kern_close(td, td->td_retval[0]);
1148 linux_mknodat(struct thread *td, struct linux_mknodat_args *args)
1153 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1154 LCONVPATHCREAT_AT(td, args->filename, &path, dfd);
1157 if (ldebug(mknodat))
1158 printf(ARGS(mknodat, "%s, %d, %d"), path, args->mode, args->dev);
1161 switch (args->mode & S_IFMT) {
1164 error = kern_mkfifoat(td, dfd, path, UIO_SYSSPACE, args->mode);
1169 error = kern_mknodat(td, dfd, path, UIO_SYSSPACE, args->mode,
1178 args->mode |= S_IFREG;
1181 error = kern_openat(td, dfd, path, UIO_SYSSPACE,
1182 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1184 kern_close(td, td->td_retval[0]);
1196 * UGH! This is just about the dumbest idea I've ever heard!!
1199 linux_personality(struct thread *td, struct linux_personality_args *args)
1201 struct linux_pemuldata *pem;
1202 struct proc *p = td->td_proc;
1206 if (ldebug(personality))
1207 printf(ARGS(personality, "%u"), args->per);
1213 if (args->per != 0xffffffff)
1214 pem->persona = args->per;
1217 td->td_retval[0] = old;
1221 struct l_itimerval {
1222 l_timeval it_interval;
1226 #define B2L_ITIMERVAL(bip, lip) \
1227 (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec; \
1228 (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec; \
1229 (bip)->it_value.tv_sec = (lip)->it_value.tv_sec; \
1230 (bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
1233 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
1236 struct l_itimerval ls;
1237 struct itimerval aitv, oitv;
1240 if (ldebug(setitimer))
1241 printf(ARGS(setitimer, "%p, %p"),
1242 (void *)uap->itv, (void *)uap->oitv);
1245 if (uap->itv == NULL) {
1246 uap->itv = uap->oitv;
1247 return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
1250 error = copyin(uap->itv, &ls, sizeof(ls));
1253 B2L_ITIMERVAL(&aitv, &ls);
1255 if (ldebug(setitimer)) {
1256 printf("setitimer: value: sec: %jd, usec: %ld\n",
1257 (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec);
1258 printf("setitimer: interval: sec: %jd, usec: %ld\n",
1259 (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec);
1262 error = kern_setitimer(td, uap->which, &aitv, &oitv);
1263 if (error != 0 || uap->oitv == NULL)
1265 B2L_ITIMERVAL(&ls, &oitv);
1267 return (copyout(&ls, uap->oitv, sizeof(ls)));
1271 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
1274 struct l_itimerval ls;
1275 struct itimerval aitv;
1278 if (ldebug(getitimer))
1279 printf(ARGS(getitimer, "%p"), (void *)uap->itv);
1281 error = kern_getitimer(td, uap->which, &aitv);
1284 B2L_ITIMERVAL(&ls, &aitv);
1285 return (copyout(&ls, uap->itv, sizeof(ls)));
1288 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1290 linux_nice(struct thread *td, struct linux_nice_args *args)
1292 struct setpriority_args bsd_args;
1294 bsd_args.which = PRIO_PROCESS;
1295 bsd_args.who = 0; /* current process */
1296 bsd_args.prio = args->inc;
1297 return (sys_setpriority(td, &bsd_args));
1299 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1302 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
1304 struct ucred *newcred, *oldcred;
1305 l_gid_t *linux_gidset;
1310 ngrp = args->gidsetsize;
1311 if (ngrp < 0 || ngrp >= ngroups_max + 1)
1313 linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_LINUX, M_WAITOK);
1314 error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
1318 crextend(newcred, ngrp + 1);
1321 oldcred = p->p_ucred;
1322 crcopy(newcred, oldcred);
1325 * cr_groups[0] holds egid. Setting the whole set from
1326 * the supplied set will cause egid to be changed too.
1327 * Keep cr_groups[0] unchanged to prevent that.
1330 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0) {
1337 newcred->cr_ngroups = ngrp + 1;
1339 bsd_gidset = newcred->cr_groups;
1342 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1346 newcred->cr_ngroups = 1;
1349 proc_set_cred(p, newcred);
1354 free(linux_gidset, M_LINUX);
1359 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1362 l_gid_t *linux_gidset;
1364 int bsd_gidsetsz, ngrp, error;
1366 cred = td->td_ucred;
1367 bsd_gidset = cred->cr_groups;
1368 bsd_gidsetsz = cred->cr_ngroups - 1;
1371 * cr_groups[0] holds egid. Returning the whole set
1372 * here will cause a duplicate. Exclude cr_groups[0]
1376 if ((ngrp = args->gidsetsize) == 0) {
1377 td->td_retval[0] = bsd_gidsetsz;
1381 if (ngrp < bsd_gidsetsz)
1385 linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset),
1387 while (ngrp < bsd_gidsetsz) {
1388 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1392 error = copyout(linux_gidset, args->grouplist, ngrp * sizeof(l_gid_t));
1393 free(linux_gidset, M_LINUX);
1397 td->td_retval[0] = ngrp;
1402 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1404 struct rlimit bsd_rlim;
1405 struct l_rlimit rlim;
1410 if (ldebug(setrlimit))
1411 printf(ARGS(setrlimit, "%d, %p"),
1412 args->resource, (void *)args->rlim);
1415 if (args->resource >= LINUX_RLIM_NLIMITS)
1418 which = linux_to_bsd_resource[args->resource];
1422 error = copyin(args->rlim, &rlim, sizeof(rlim));
1426 bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1427 bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1428 return (kern_setrlimit(td, which, &bsd_rlim));
1431 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1433 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1435 struct l_rlimit rlim;
1436 struct proc *p = td->td_proc;
1437 struct rlimit bsd_rlim;
1441 if (ldebug(old_getrlimit))
1442 printf(ARGS(old_getrlimit, "%d, %p"),
1443 args->resource, (void *)args->rlim);
1446 if (args->resource >= LINUX_RLIM_NLIMITS)
1449 which = linux_to_bsd_resource[args->resource];
1454 lim_rlimit(p, which, &bsd_rlim);
1457 #ifdef COMPAT_LINUX32
1458 rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1459 if (rlim.rlim_cur == UINT_MAX)
1460 rlim.rlim_cur = INT_MAX;
1461 rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1462 if (rlim.rlim_max == UINT_MAX)
1463 rlim.rlim_max = INT_MAX;
1465 rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1466 if (rlim.rlim_cur == ULONG_MAX)
1467 rlim.rlim_cur = LONG_MAX;
1468 rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1469 if (rlim.rlim_max == ULONG_MAX)
1470 rlim.rlim_max = LONG_MAX;
1472 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1474 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1477 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1479 struct l_rlimit rlim;
1480 struct proc *p = td->td_proc;
1481 struct rlimit bsd_rlim;
1485 if (ldebug(getrlimit))
1486 printf(ARGS(getrlimit, "%d, %p"),
1487 args->resource, (void *)args->rlim);
1490 if (args->resource >= LINUX_RLIM_NLIMITS)
1493 which = linux_to_bsd_resource[args->resource];
1498 lim_rlimit(p, which, &bsd_rlim);
1501 rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1502 rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1503 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1507 linux_sched_setscheduler(struct thread *td,
1508 struct linux_sched_setscheduler_args *args)
1510 struct sched_param sched_param;
1515 if (ldebug(sched_setscheduler))
1516 printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1517 args->pid, args->policy, (const void *)args->param);
1520 switch (args->policy) {
1521 case LINUX_SCHED_OTHER:
1522 policy = SCHED_OTHER;
1524 case LINUX_SCHED_FIFO:
1525 policy = SCHED_FIFO;
1527 case LINUX_SCHED_RR:
1534 error = copyin(args->param, &sched_param, sizeof(sched_param));
1538 tdt = linux_tdfind(td, args->pid, -1);
1542 error = kern_sched_setscheduler(td, tdt, policy, &sched_param);
1543 PROC_UNLOCK(tdt->td_proc);
1548 linux_sched_getscheduler(struct thread *td,
1549 struct linux_sched_getscheduler_args *args)
1555 if (ldebug(sched_getscheduler))
1556 printf(ARGS(sched_getscheduler, "%d"), args->pid);
1559 tdt = linux_tdfind(td, args->pid, -1);
1563 error = kern_sched_getscheduler(td, tdt, &policy);
1564 PROC_UNLOCK(tdt->td_proc);
1568 td->td_retval[0] = LINUX_SCHED_OTHER;
1571 td->td_retval[0] = LINUX_SCHED_FIFO;
1574 td->td_retval[0] = LINUX_SCHED_RR;
1581 linux_sched_get_priority_max(struct thread *td,
1582 struct linux_sched_get_priority_max_args *args)
1584 struct sched_get_priority_max_args bsd;
1587 if (ldebug(sched_get_priority_max))
1588 printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1591 switch (args->policy) {
1592 case LINUX_SCHED_OTHER:
1593 bsd.policy = SCHED_OTHER;
1595 case LINUX_SCHED_FIFO:
1596 bsd.policy = SCHED_FIFO;
1598 case LINUX_SCHED_RR:
1599 bsd.policy = SCHED_RR;
1604 return (sys_sched_get_priority_max(td, &bsd));
1608 linux_sched_get_priority_min(struct thread *td,
1609 struct linux_sched_get_priority_min_args *args)
1611 struct sched_get_priority_min_args bsd;
1614 if (ldebug(sched_get_priority_min))
1615 printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1618 switch (args->policy) {
1619 case LINUX_SCHED_OTHER:
1620 bsd.policy = SCHED_OTHER;
1622 case LINUX_SCHED_FIFO:
1623 bsd.policy = SCHED_FIFO;
1625 case LINUX_SCHED_RR:
1626 bsd.policy = SCHED_RR;
1631 return (sys_sched_get_priority_min(td, &bsd));
1634 #define REBOOT_CAD_ON 0x89abcdef
1635 #define REBOOT_CAD_OFF 0
1636 #define REBOOT_HALT 0xcdef0123
1637 #define REBOOT_RESTART 0x01234567
1638 #define REBOOT_RESTART2 0xA1B2C3D4
1639 #define REBOOT_POWEROFF 0x4321FEDC
1640 #define REBOOT_MAGIC1 0xfee1dead
1641 #define REBOOT_MAGIC2 0x28121969
1642 #define REBOOT_MAGIC2A 0x05121996
1643 #define REBOOT_MAGIC2B 0x16041998
1646 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1648 struct reboot_args bsd_args;
1652 printf(ARGS(reboot, "0x%x"), args->cmd);
1655 if (args->magic1 != REBOOT_MAGIC1)
1658 switch (args->magic2) {
1660 case REBOOT_MAGIC2A:
1661 case REBOOT_MAGIC2B:
1667 switch (args->cmd) {
1669 case REBOOT_CAD_OFF:
1670 return (priv_check(td, PRIV_REBOOT));
1672 bsd_args.opt = RB_HALT;
1674 case REBOOT_RESTART:
1675 case REBOOT_RESTART2:
1678 case REBOOT_POWEROFF:
1679 bsd_args.opt = RB_POWEROFF;
1684 return (sys_reboot(td, &bsd_args));
1689 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1690 * td->td_retval[1] when COMPAT_43 is defined. This clobbers registers that
1691 * are assumed to be preserved. The following lightweight syscalls fixes
1692 * this. See also linux_getgid16() and linux_getuid16() in linux_uid16.c
1694 * linux_getpid() - MP SAFE
1695 * linux_getgid() - MP SAFE
1696 * linux_getuid() - MP SAFE
1700 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1705 printf(ARGS(getpid, ""));
1707 td->td_retval[0] = td->td_proc->p_pid;
1713 linux_gettid(struct thread *td, struct linux_gettid_args *args)
1715 struct linux_emuldata *em;
1719 printf(ARGS(gettid, ""));
1723 KASSERT(em != NULL, ("gettid: emuldata not found.\n"));
1725 td->td_retval[0] = em->em_tid;
1732 linux_getppid(struct thread *td, struct linux_getppid_args *args)
1736 if (ldebug(getppid))
1737 printf(ARGS(getppid, ""));
1740 PROC_LOCK(td->td_proc);
1741 td->td_retval[0] = td->td_proc->p_pptr->p_pid;
1742 PROC_UNLOCK(td->td_proc);
1747 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1752 printf(ARGS(getgid, ""));
1755 td->td_retval[0] = td->td_ucred->cr_rgid;
1760 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1765 printf(ARGS(getuid, ""));
1768 td->td_retval[0] = td->td_ucred->cr_ruid;
1774 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1776 struct getsid_args bsd;
1780 printf(ARGS(getsid, "%i"), args->pid);
1783 bsd.pid = args->pid;
1784 return (sys_getsid(td, &bsd));
1788 linux_nosys(struct thread *td, struct nosys_args *ignore)
1795 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1797 struct getpriority_args bsd_args;
1801 if (ldebug(getpriority))
1802 printf(ARGS(getpriority, "%i, %i"), args->which, args->who);
1805 bsd_args.which = args->which;
1806 bsd_args.who = args->who;
1807 error = sys_getpriority(td, &bsd_args);
1808 td->td_retval[0] = 20 - td->td_retval[0];
1813 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1818 if (ldebug(sethostname))
1819 printf(ARGS(sethostname, "*, %i"), args->len);
1823 name[1] = KERN_HOSTNAME;
1824 return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1829 linux_setdomainname(struct thread *td, struct linux_setdomainname_args *args)
1834 if (ldebug(setdomainname))
1835 printf(ARGS(setdomainname, "*, %i"), args->len);
1839 name[1] = KERN_NISDOMAINNAME;
1840 return (userland_sysctl(td, name, 2, 0, 0, 0, args->name,
1845 linux_exit_group(struct thread *td, struct linux_exit_group_args *args)
1849 if (ldebug(exit_group))
1850 printf(ARGS(exit_group, "%i"), args->error_code);
1853 LINUX_CTR2(exit_group, "thread(%d) (%d)", td->td_tid,
1857 * XXX: we should send a signal to the parent if
1858 * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?)
1859 * as it doesnt occur often.
1861 exit1(td, W_EXITCODE(args->error_code, 0));
1865 #define _LINUX_CAPABILITY_VERSION 0x19980330
1867 struct l_user_cap_header {
1872 struct l_user_cap_data {
1879 linux_capget(struct thread *td, struct linux_capget_args *args)
1881 struct l_user_cap_header luch;
1882 struct l_user_cap_data lucd;
1885 if (args->hdrp == NULL)
1888 error = copyin(args->hdrp, &luch, sizeof(luch));
1892 if (luch.version != _LINUX_CAPABILITY_VERSION) {
1893 luch.version = _LINUX_CAPABILITY_VERSION;
1894 error = copyout(&luch, args->hdrp, sizeof(luch));
1905 * The current implementation doesn't support setting
1906 * a capability (it's essentially a stub) so indicate
1907 * that no capabilities are currently set or available
1910 bzero (&lucd, sizeof(lucd));
1911 error = copyout(&lucd, args->datap, sizeof(lucd));
1918 linux_capset(struct thread *td, struct linux_capset_args *args)
1920 struct l_user_cap_header luch;
1921 struct l_user_cap_data lucd;
1924 if (args->hdrp == NULL || args->datap == NULL)
1927 error = copyin(args->hdrp, &luch, sizeof(luch));
1931 if (luch.version != _LINUX_CAPABILITY_VERSION) {
1932 luch.version = _LINUX_CAPABILITY_VERSION;
1933 error = copyout(&luch, args->hdrp, sizeof(luch));
1942 error = copyin(args->datap, &lucd, sizeof(lucd));
1946 /* We currently don't support setting any capabilities. */
1947 if (lucd.effective || lucd.permitted || lucd.inheritable) {
1949 "capset effective=0x%x, permitted=0x%x, "
1950 "inheritable=0x%x is not implemented",
1951 (int)lucd.effective, (int)lucd.permitted,
1952 (int)lucd.inheritable);
1960 linux_prctl(struct thread *td, struct linux_prctl_args *args)
1962 int error = 0, max_size;
1963 struct proc *p = td->td_proc;
1964 char comm[LINUX_MAX_COMM_LEN];
1965 struct linux_emuldata *em;
1970 printf(ARGS(prctl, "%d, %ju, %ju, %ju, %ju"), args->option,
1971 (uintmax_t)args->arg2, (uintmax_t)args->arg3,
1972 (uintmax_t)args->arg4, (uintmax_t)args->arg5);
1975 switch (args->option) {
1976 case LINUX_PR_SET_PDEATHSIG:
1977 if (!LINUX_SIG_VALID(args->arg2))
1980 KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1981 em->pdeath_signal = args->arg2;
1983 case LINUX_PR_GET_PDEATHSIG:
1985 KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1986 pdeath_signal = em->pdeath_signal;
1987 error = copyout(&pdeath_signal,
1988 (void *)(register_t)args->arg2,
1989 sizeof(pdeath_signal));
1991 case LINUX_PR_GET_KEEPCAPS:
1993 * Indicate that we always clear the effective and
1994 * permitted capability sets when the user id becomes
1995 * non-zero (actually the capability sets are simply
1996 * always zero in the current implementation).
1998 td->td_retval[0] = 0;
2000 case LINUX_PR_SET_KEEPCAPS:
2002 * Ignore requests to keep the effective and permitted
2003 * capability sets when the user id becomes non-zero.
2006 case LINUX_PR_SET_NAME:
2008 * To be on the safe side we need to make sure to not
2009 * overflow the size a linux program expects. We already
2010 * do this here in the copyin, so that we don't need to
2013 max_size = MIN(sizeof(comm), sizeof(p->p_comm));
2014 error = copyinstr((void *)(register_t)args->arg2, comm,
2017 /* Linux silently truncates the name if it is too long. */
2018 if (error == ENAMETOOLONG) {
2020 * XXX: copyinstr() isn't documented to populate the
2021 * array completely, so do a copyin() to be on the
2022 * safe side. This should be changed in case
2023 * copyinstr() is changed to guarantee this.
2025 error = copyin((void *)(register_t)args->arg2, comm,
2027 comm[max_size - 1] = '\0';
2033 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
2036 case LINUX_PR_GET_NAME:
2038 strlcpy(comm, p->p_comm, sizeof(comm));
2040 error = copyout(comm, (void *)(register_t)args->arg2,
2052 linux_sched_setparam(struct thread *td,
2053 struct linux_sched_setparam_args *uap)
2055 struct sched_param sched_param;
2060 if (ldebug(sched_setparam))
2061 printf(ARGS(sched_setparam, "%d, *"), uap->pid);
2064 error = copyin(uap->param, &sched_param, sizeof(sched_param));
2068 tdt = linux_tdfind(td, uap->pid, -1);
2072 error = kern_sched_setparam(td, tdt, &sched_param);
2073 PROC_UNLOCK(tdt->td_proc);
2078 linux_sched_getparam(struct thread *td,
2079 struct linux_sched_getparam_args *uap)
2081 struct sched_param sched_param;
2086 if (ldebug(sched_getparam))
2087 printf(ARGS(sched_getparam, "%d, *"), uap->pid);
2090 tdt = linux_tdfind(td, uap->pid, -1);
2094 error = kern_sched_getparam(td, tdt, &sched_param);
2095 PROC_UNLOCK(tdt->td_proc);
2097 error = copyout(&sched_param, uap->param,
2098 sizeof(sched_param));
2103 * Get affinity of a process.
2106 linux_sched_getaffinity(struct thread *td,
2107 struct linux_sched_getaffinity_args *args)
2111 struct cpuset_getaffinity_args cga;
2114 if (ldebug(sched_getaffinity))
2115 printf(ARGS(sched_getaffinity, "%d, %d, *"), args->pid,
2118 if (args->len < sizeof(cpuset_t))
2121 tdt = linux_tdfind(td, args->pid, -1);
2125 PROC_UNLOCK(tdt->td_proc);
2126 cga.level = CPU_LEVEL_WHICH;
2127 cga.which = CPU_WHICH_TID;
2128 cga.id = tdt->td_tid;
2129 cga.cpusetsize = sizeof(cpuset_t);
2130 cga.mask = (cpuset_t *) args->user_mask_ptr;
2132 if ((error = sys_cpuset_getaffinity(td, &cga)) == 0)
2133 td->td_retval[0] = sizeof(cpuset_t);
2139 * Set affinity of a process.
2142 linux_sched_setaffinity(struct thread *td,
2143 struct linux_sched_setaffinity_args *args)
2145 struct cpuset_setaffinity_args csa;
2149 if (ldebug(sched_setaffinity))
2150 printf(ARGS(sched_setaffinity, "%d, %d, *"), args->pid,
2153 if (args->len < sizeof(cpuset_t))
2156 tdt = linux_tdfind(td, args->pid, -1);
2160 PROC_UNLOCK(tdt->td_proc);
2161 csa.level = CPU_LEVEL_WHICH;
2162 csa.which = CPU_WHICH_TID;
2163 csa.id = tdt->td_tid;
2164 csa.cpusetsize = sizeof(cpuset_t);
2165 csa.mask = (cpuset_t *) args->user_mask_ptr;
2167 return (sys_cpuset_setaffinity(td, &csa));
2170 struct linux_rlimit64 {
2176 linux_prlimit64(struct thread *td, struct linux_prlimit64_args *args)
2178 struct rlimit rlim, nrlim;
2179 struct linux_rlimit64 lrlim;
2186 if (ldebug(prlimit64))
2187 printf(ARGS(prlimit64, "%d, %d, %p, %p"), args->pid,
2188 args->resource, (void *)args->new, (void *)args->old);
2191 if (args->resource >= LINUX_RLIM_NLIMITS)
2194 which = linux_to_bsd_resource[args->resource];
2198 if (args->new != NULL) {
2200 * Note. Unlike FreeBSD where rlim is signed 64-bit Linux
2201 * rlim is unsigned 64-bit. FreeBSD treats negative limits
2202 * as INFINITY so we do not need a conversion even.
2204 error = copyin(args->new, &nrlim, sizeof(nrlim));
2209 flags = PGET_HOLD | PGET_NOTWEXIT;
2210 if (args->new != NULL)
2211 flags |= PGET_CANDEBUG;
2213 flags |= PGET_CANSEE;
2214 error = pget(args->pid, flags, &p);
2218 if (args->old != NULL) {
2220 lim_rlimit(p, which, &rlim);
2222 if (rlim.rlim_cur == RLIM_INFINITY)
2223 lrlim.rlim_cur = LINUX_RLIM_INFINITY;
2225 lrlim.rlim_cur = rlim.rlim_cur;
2226 if (rlim.rlim_max == RLIM_INFINITY)
2227 lrlim.rlim_max = LINUX_RLIM_INFINITY;
2229 lrlim.rlim_max = rlim.rlim_max;
2230 error = copyout(&lrlim, args->old, sizeof(lrlim));
2235 if (args->new != NULL)
2236 error = kern_proc_setrlimit(td, p, which, &nrlim);
2244 linux_pselect6(struct thread *td, struct linux_pselect6_args *args)
2246 struct timeval utv, tv0, tv1, *tvp;
2247 struct l_pselect6arg lpse6;
2248 struct l_timespec lts;
2249 struct timespec uts;
2256 if (args->sig != NULL) {
2257 error = copyin(args->sig, &lpse6, sizeof(lpse6));
2260 if (lpse6.ss_len != sizeof(l_ss))
2262 if (lpse6.ss != 0) {
2263 error = copyin(PTRIN(lpse6.ss), &l_ss,
2267 linux_to_bsd_sigset(&l_ss, &ss);
2273 * Currently glibc changes nanosecond number to microsecond.
2274 * This mean losing precision but for now it is hardly seen.
2276 if (args->tsp != NULL) {
2277 error = copyin(args->tsp, <s, sizeof(lts));
2280 error = linux_to_native_timespec(&uts, <s);
2284 TIMESPEC_TO_TIMEVAL(&utv, &uts);
2285 if (itimerfix(&utv))
2293 error = kern_pselect(td, args->nfds, args->readfds, args->writefds,
2294 args->exceptfds, tvp, ssp, LINUX_NFDBITS);
2296 if (error == 0 && args->tsp != NULL) {
2297 if (td->td_retval[0] != 0) {
2299 * Compute how much time was left of the timeout,
2300 * by subtracting the current time and the time
2301 * before we started the call, and subtracting
2302 * that result from the user-supplied value.
2306 timevalsub(&tv1, &tv0);
2307 timevalsub(&utv, &tv1);
2313 TIMEVAL_TO_TIMESPEC(&utv, &uts);
2315 native_to_linux_timespec(<s, &uts);
2316 error = copyout(<s, args->tsp, sizeof(lts));
2323 linux_ppoll(struct thread *td, struct linux_ppoll_args *args)
2325 struct timespec ts0, ts1;
2326 struct l_timespec lts;
2327 struct timespec uts, *tsp;
2333 if (args->sset != NULL) {
2334 if (args->ssize != sizeof(l_ss))
2336 error = copyin(args->sset, &l_ss, sizeof(l_ss));
2339 linux_to_bsd_sigset(&l_ss, &ss);
2343 if (args->tsp != NULL) {
2344 error = copyin(args->tsp, <s, sizeof(lts));
2347 error = linux_to_native_timespec(&uts, <s);
2356 error = kern_poll(td, args->fds, args->nfds, tsp, ssp);
2358 if (error == 0 && args->tsp != NULL) {
2359 if (td->td_retval[0]) {
2361 timespecsub(&ts1, &ts0);
2362 timespecsub(&uts, &ts1);
2364 timespecclear(&uts);
2366 timespecclear(&uts);
2368 native_to_linux_timespec(<s, &uts);
2369 error = copyout(<s, args->tsp, sizeof(lts));
2375 #if defined(DEBUG) || defined(KTR)
2376 /* XXX: can be removed when every ldebug(...) and KTR stuff are removed. */
2378 #ifdef COMPAT_LINUX32
2379 #define L_MAXSYSCALL LINUX32_SYS_MAXSYSCALL
2381 #define L_MAXSYSCALL LINUX_SYS_MAXSYSCALL
2384 u_char linux_debug_map[howmany(L_MAXSYSCALL, sizeof(u_char))];
2387 linux_debug(int syscall, int toggle, int global)
2391 char c = toggle ? 0 : 0xff;
2393 memset(linux_debug_map, c, sizeof(linux_debug_map));
2396 if (syscall < 0 || syscall >= L_MAXSYSCALL)
2399 clrbit(linux_debug_map, syscall);
2401 setbit(linux_debug_map, syscall);
2407 * Usage: sysctl linux.debug=<syscall_nr>.<0/1>
2409 * E.g.: sysctl linux.debug=21.0
2411 * As a special case, syscall "all" will apply to all syscalls globally.
2413 #define LINUX_MAX_DEBUGSTR 16
2415 linux_sysctl_debug(SYSCTL_HANDLER_ARGS)
2417 char value[LINUX_MAX_DEBUGSTR], *p;
2418 int error, sysc, toggle;
2422 error = sysctl_handle_string(oidp, value, LINUX_MAX_DEBUGSTR, req);
2423 if (error || req->newptr == NULL)
2425 for (p = value; *p != '\0' && *p != '.'; p++);
2429 sysc = strtol(value, NULL, 0);
2430 toggle = strtol(p, NULL, 0);
2431 if (strcmp(value, "all") == 0)
2433 error = linux_debug(sysc, toggle, global);
2437 #endif /* DEBUG || KTR */
2440 linux_sched_rr_get_interval(struct thread *td,
2441 struct linux_sched_rr_get_interval_args *uap)
2444 struct l_timespec lts;
2449 * According to man in case the invalid pid specified
2450 * EINVAL should be returned.
2455 tdt = linux_tdfind(td, uap->pid, -1);
2459 error = kern_sched_rr_get_interval_td(td, tdt, &ts);
2460 PROC_UNLOCK(tdt->td_proc);
2463 native_to_linux_timespec(<s, &ts);
2464 return (copyout(<s, uap->interval, sizeof(lts)));
2468 * In case when the Linux thread is the initial thread in
2469 * the thread group thread id is equal to the process id.
2470 * Glibc depends on this magic (assert in pthread_getattr_np.c).
2473 linux_tdfind(struct thread *td, lwpid_t tid, pid_t pid)
2475 struct linux_emuldata *em;
2480 if (tid == 0 || tid == td->td_tid) {
2482 PROC_LOCK(tdt->td_proc);
2483 } else if (tid > PID_MAX)
2484 tdt = tdfind(tid, pid);
2487 * Initial thread where the tid equal to the pid.
2491 if (SV_PROC_ABI(p) != SV_ABI_LINUX) {
2493 * p is not a Linuxulator process.
2498 FOREACH_THREAD_IN_PROC(p, tdt) {
2500 if (tid == em->em_tid)
2512 linux_to_bsd_waitopts(int options, int *bsdopts)
2515 if (options & LINUX_WNOHANG)
2516 *bsdopts |= WNOHANG;
2517 if (options & LINUX_WUNTRACED)
2518 *bsdopts |= WUNTRACED;
2519 if (options & LINUX_WEXITED)
2520 *bsdopts |= WEXITED;
2521 if (options & LINUX_WCONTINUED)
2522 *bsdopts |= WCONTINUED;
2523 if (options & LINUX_WNOWAIT)
2524 *bsdopts |= WNOWAIT;
2526 if (options & __WCLONE)
2527 *bsdopts |= WLINUXCLONE;