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 #if defined(KLD_MODULE)
35 #include "opt_global.h"
38 #include <sys/param.h>
39 #include <sys/blist.h>
40 #include <sys/fcntl.h>
42 #include <sys/imgact_aout.h>
45 #include <sys/kernel.h>
46 #include <sys/limits.h>
48 #include <sys/malloc.h>
50 #include <sys/mount.h>
51 #include <sys/mutex.h>
52 #include <sys/namei.h>
55 #include <sys/reboot.h>
56 #include <sys/racct.h>
57 #include <sys/random.h>
58 #include <sys/resourcevar.h>
59 #include <sys/sched.h>
61 #include <sys/signalvar.h>
63 #include <sys/syscallsubr.h>
64 #include <sys/sysctl.h>
65 #include <sys/sysproto.h>
66 #include <sys/systm.h>
68 #include <sys/vmmeter.h>
69 #include <sys/vnode.h>
71 #include <sys/cpuset.h>
74 #include <security/mac/mac_framework.h>
78 #include <vm/vm_kern.h>
79 #include <vm/vm_map.h>
80 #include <vm/vm_extern.h>
81 #include <vm/vm_object.h>
82 #include <vm/swap_pager.h>
85 #include <machine/../linux32/linux.h>
86 #include <machine/../linux32/linux32_proto.h>
88 #include <machine/../linux/linux.h>
89 #include <machine/../linux/linux_proto.h>
92 #include <compat/linux/linux_dtrace.h>
93 #include <compat/linux/linux_file.h>
94 #include <compat/linux/linux_mib.h>
95 #include <compat/linux/linux_signal.h>
96 #include <compat/linux/linux_timer.h>
97 #include <compat/linux/linux_util.h>
98 #include <compat/linux/linux_sysproto.h>
99 #include <compat/linux/linux_emul.h>
100 #include <compat/linux/linux_misc.h>
103 * Special DTrace provider for the linuxulator.
105 * In this file we define the provider for the entire linuxulator. All
106 * modules (= files of the linuxulator) use it.
108 * We define a different name depending on the emulated bitsize, see
109 * ../../<ARCH>/linux{,32}/linux.h, e.g.:
110 * native bitsize = linuxulator
111 * amd64, 32bit emulation = linuxulator32
113 LIN_SDT_PROVIDER_DEFINE(LINUX_DTRACE);
115 int stclohz; /* Statistics clock frequency */
117 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
118 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
119 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
120 RLIMIT_MEMLOCK, RLIMIT_AS
124 l_long uptime; /* Seconds since boot */
125 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */
126 #define LINUX_SYSINFO_LOADS_SCALE 65536
127 l_ulong totalram; /* Total usable main memory size */
128 l_ulong freeram; /* Available memory size */
129 l_ulong sharedram; /* Amount of shared memory */
130 l_ulong bufferram; /* Memory used by buffers */
131 l_ulong totalswap; /* Total swap space size */
132 l_ulong freeswap; /* swap space still available */
133 l_ushort procs; /* Number of current processes */
138 char _f[20-2*sizeof(l_long)-sizeof(l_int)]; /* padding */
141 struct l_pselect6arg {
146 static int linux_utimensat_nsec_valid(l_long);
150 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args)
152 struct l_sysinfo sysinfo;
157 bzero(&sysinfo, sizeof(sysinfo));
161 sysinfo.uptime = ts.tv_sec;
163 /* Use the information from the mib to get our load averages */
164 for (i = 0; i < 3; i++)
165 sysinfo.loads[i] = averunnable.ldavg[i] *
166 LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale;
168 sysinfo.totalram = physmem * PAGE_SIZE;
169 sysinfo.freeram = sysinfo.totalram - vm_cnt.v_wire_count * PAGE_SIZE;
171 sysinfo.sharedram = 0;
172 mtx_lock(&vm_object_list_mtx);
173 TAILQ_FOREACH(object, &vm_object_list, object_list)
174 if (object->shadow_count > 1)
175 sysinfo.sharedram += object->resident_page_count;
176 mtx_unlock(&vm_object_list_mtx);
178 sysinfo.sharedram *= PAGE_SIZE;
179 sysinfo.bufferram = 0;
181 swap_pager_status(&i, &j);
182 sysinfo.totalswap = i * PAGE_SIZE;
183 sysinfo.freeswap = (i - j) * PAGE_SIZE;
185 sysinfo.procs = nprocs;
187 /* The following are only present in newer Linux kernels. */
188 sysinfo.totalbig = 0;
190 sysinfo.mem_unit = 1;
192 return (copyout(&sysinfo, args->info, sizeof(sysinfo)));
196 linux_alarm(struct thread *td, struct linux_alarm_args *args)
198 struct itimerval it, old_it;
204 printf(ARGS(alarm, "%u"), args->secs);
208 * Linux alarm() is always successful. Limit secs to INT32_MAX / 2
209 * to match kern_setitimer()'s limit to avoid error from it.
211 * XXX. Linux limit secs to INT_MAX on 32 and does not limit on 64-bit
214 if (secs > INT32_MAX / 2)
215 secs = INT32_MAX / 2;
217 it.it_value.tv_sec = secs;
218 it.it_value.tv_usec = 0;
219 timevalclear(&it.it_interval);
220 error = kern_setitimer(td, ITIMER_REAL, &it, &old_it);
221 KASSERT(error == 0, ("kern_setitimer returns %d", error));
223 if ((old_it.it_value.tv_sec == 0 && old_it.it_value.tv_usec > 0) ||
224 old_it.it_value.tv_usec >= 500000)
225 old_it.it_value.tv_sec++;
226 td->td_retval[0] = old_it.it_value.tv_sec;
231 linux_brk(struct thread *td, struct linux_brk_args *args)
233 struct vmspace *vm = td->td_proc->p_vmspace;
234 vm_offset_t new, old;
235 struct obreak_args /* {
241 printf(ARGS(brk, "%p"), (void *)(uintptr_t)args->dsend);
243 old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
244 new = (vm_offset_t)args->dsend;
245 tmp.nsize = (char *)new;
246 if (((caddr_t)new > vm->vm_daddr) && !sys_obreak(td, &tmp))
247 td->td_retval[0] = (long)new;
249 td->td_retval[0] = (long)old;
254 #if defined(__i386__)
255 /* XXX: what about amd64/linux32? */
258 linux_uselib(struct thread *td, struct linux_uselib_args *args)
265 unsigned long file_offset;
266 unsigned long bss_size;
269 int error, locked, writecount;
271 LCONVPATHEXIST(td, args->library, &library);
275 printf(ARGS(uselib, "%s"), library);
282 NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | AUDITVNODE1,
283 UIO_SYSSPACE, library, td);
290 NDFREE(&ni, NDF_ONLY_PNBUF);
293 * From here on down, we have a locked vnode that must be unlocked.
294 * XXX: The code below largely duplicates exec_check_permissions().
299 error = VOP_GET_WRITECOUNT(vp, &writecount);
302 if (writecount != 0) {
308 error = VOP_GETATTR(vp, &attr, td->td_ucred);
312 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
313 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
314 /* EACCESS is what exec(2) returns. */
320 if (attr.va_size == 0) {
325 /* Can we access it? */
326 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
331 * XXX: This should use vn_open() so that it is properly authorized,
332 * and to reduce code redundancy all over the place here.
333 * XXX: Not really, it duplicates far more of exec_check_permissions()
337 error = mac_vnode_check_open(td->td_ucred, vp, VREAD);
341 error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL);
345 /* Pull in executable header into exec_map */
346 error = vm_mmap(exec_map, (vm_offset_t *)&a_out, PAGE_SIZE,
347 VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 0);
351 /* Is it a Linux binary ? */
352 if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
358 * While we are here, we should REALLY do some more checks
361 /* Set file/virtual offset based on a.out variant. */
362 switch ((int)(a_out->a_magic & 0xffff)) {
363 case 0413: /* ZMAGIC */
366 case 0314: /* QMAGIC */
374 bss_size = round_page(a_out->a_bss);
376 /* Check various fields in header for validity/bounds. */
377 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
382 /* text + data can't exceed file size */
383 if (a_out->a_data + a_out->a_text > attr.va_size) {
389 * text/data/bss must not exceed limits
390 * XXX - this is not complete. it should check current usage PLUS
391 * the resources needed by this library.
393 PROC_LOCK(td->td_proc);
394 if (a_out->a_text > maxtsiz ||
395 a_out->a_data + bss_size > lim_cur_proc(td->td_proc, RLIMIT_DATA) ||
396 racct_set(td->td_proc, RACCT_DATA, a_out->a_data +
398 PROC_UNLOCK(td->td_proc);
402 PROC_UNLOCK(td->td_proc);
405 * Prevent more writers.
406 * XXX: Note that if any of the VM operations fail below we don't
412 * Lock no longer needed
418 * Check if file_offset page aligned. Currently we cannot handle
419 * misalinged file offsets, and so we read in the entire image
422 if (file_offset & PAGE_MASK) {
424 printf("uselib: Non page aligned binary %lu\n", file_offset);
426 /* Map text+data read/write/execute */
428 /* a_entry is the load address and is page aligned */
429 vmaddr = trunc_page(a_out->a_entry);
431 /* get anon user mapping, read+write+execute */
432 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
433 &vmaddr, a_out->a_text + a_out->a_data, 0, VMFS_NO_SPACE,
434 VM_PROT_ALL, VM_PROT_ALL, 0);
438 error = vn_rdwr(UIO_READ, vp, (void *)vmaddr, file_offset,
439 a_out->a_text + a_out->a_data, UIO_USERSPACE, 0,
440 td->td_ucred, NOCRED, &aresid, td);
449 printf("uselib: Page aligned binary %lu\n", file_offset);
452 * for QMAGIC, a_entry is 20 bytes beyond the load address
453 * to skip the executable header
455 vmaddr = trunc_page(a_out->a_entry);
458 * Map it all into the process's space as a single
459 * copy-on-write "data" segment.
461 error = vm_mmap(&td->td_proc->p_vmspace->vm_map, &vmaddr,
462 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
463 MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset);
468 printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long *)vmaddr)[0],
469 ((long *)vmaddr)[1]);
472 /* Calculate BSS start address */
473 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
476 /* allocate some 'anon' space */
477 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
478 &vmaddr, bss_size, 0, VMFS_NO_SPACE, VM_PROT_ALL,
485 /* Unlock vnode if needed */
489 /* Release the temporary mapping. */
491 kmap_free_wakeup(exec_map, (vm_offset_t)a_out, PAGE_SIZE);
496 #endif /* __i386__ */
499 linux_select(struct thread *td, struct linux_select_args *args)
502 struct timeval tv0, tv1, utv, *tvp;
507 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
508 (void *)args->readfds, (void *)args->writefds,
509 (void *)args->exceptfds, (void *)args->timeout);
513 * Store current time for computation of the amount of
517 if ((error = copyin(args->timeout, <v, sizeof(ltv))))
519 utv.tv_sec = ltv.tv_sec;
520 utv.tv_usec = ltv.tv_usec;
523 printf(LMSG("incoming timeout (%jd/%ld)"),
524 (intmax_t)utv.tv_sec, utv.tv_usec);
527 if (itimerfix(&utv)) {
529 * The timeval was invalid. Convert it to something
530 * valid that will act as it does under Linux.
532 utv.tv_sec += utv.tv_usec / 1000000;
533 utv.tv_usec %= 1000000;
534 if (utv.tv_usec < 0) {
536 utv.tv_usec += 1000000;
546 error = kern_select(td, args->nfds, args->readfds, args->writefds,
547 args->exceptfds, tvp, LINUX_NFDBITS);
551 printf(LMSG("real select returns %d"), error);
557 if (td->td_retval[0]) {
559 * Compute how much time was left of the timeout,
560 * by subtracting the current time and the time
561 * before we started the call, and subtracting
562 * that result from the user-supplied value.
565 timevalsub(&tv1, &tv0);
566 timevalsub(&utv, &tv1);
573 printf(LMSG("outgoing timeout (%jd/%ld)"),
574 (intmax_t)utv.tv_sec, utv.tv_usec);
576 ltv.tv_sec = utv.tv_sec;
577 ltv.tv_usec = utv.tv_usec;
578 if ((error = copyout(<v, args->timeout, sizeof(ltv))))
585 printf(LMSG("select_out -> %d"), error);
591 linux_mremap(struct thread *td, struct linux_mremap_args *args)
599 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
600 (void *)(uintptr_t)args->addr,
601 (unsigned long)args->old_len,
602 (unsigned long)args->new_len,
603 (unsigned long)args->flags);
606 if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) {
607 td->td_retval[0] = 0;
612 * Check for the page alignment.
613 * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK.
615 if (args->addr & PAGE_MASK) {
616 td->td_retval[0] = 0;
620 args->new_len = round_page(args->new_len);
621 args->old_len = round_page(args->old_len);
623 if (args->new_len > args->old_len) {
624 td->td_retval[0] = 0;
628 if (args->new_len < args->old_len) {
629 addr = args->addr + args->new_len;
630 len = args->old_len - args->new_len;
631 error = kern_munmap(td, addr, len);
634 td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
638 #define LINUX_MS_ASYNC 0x0001
639 #define LINUX_MS_INVALIDATE 0x0002
640 #define LINUX_MS_SYNC 0x0004
643 linux_msync(struct thread *td, struct linux_msync_args *args)
646 return (kern_msync(td, args->addr, args->len,
647 args->fl & ~LINUX_MS_SYNC));
651 linux_time(struct thread *td, struct linux_time_args *args)
659 printf(ARGS(time, "*"));
664 if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
666 td->td_retval[0] = tm;
670 struct l_times_argv {
673 l_clock_t tms_cutime;
674 l_clock_t tms_cstime;
679 * Glibc versions prior to 2.2.1 always use hard-coded CLK_TCK value.
680 * Since 2.2.1 Glibc uses value exported from kernel via AT_CLKTCK
681 * auxiliary vector entry.
685 #define CONVOTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
686 #define CONVNTCK(r) (r.tv_sec * stclohz + r.tv_usec / (1000000 / stclohz))
688 #define CONVTCK(r) (linux_kernver(td) >= LINUX_KERNVER_2004000 ? \
689 CONVNTCK(r) : CONVOTCK(r))
692 linux_times(struct thread *td, struct linux_times_args *args)
694 struct timeval tv, utime, stime, cutime, cstime;
695 struct l_times_argv tms;
701 printf(ARGS(times, "*"));
704 if (args->buf != NULL) {
708 calcru(p, &utime, &stime);
710 calccru(p, &cutime, &cstime);
713 tms.tms_utime = CONVTCK(utime);
714 tms.tms_stime = CONVTCK(stime);
716 tms.tms_cutime = CONVTCK(cutime);
717 tms.tms_cstime = CONVTCK(cstime);
719 if ((error = copyout(&tms, args->buf, sizeof(tms))))
724 td->td_retval[0] = (int)CONVTCK(tv);
729 linux_newuname(struct thread *td, struct linux_newuname_args *args)
731 struct l_new_utsname utsname;
732 char osname[LINUX_MAX_UTSNAME];
733 char osrelease[LINUX_MAX_UTSNAME];
737 if (ldebug(newuname))
738 printf(ARGS(newuname, "*"));
741 linux_get_osname(td, osname);
742 linux_get_osrelease(td, osrelease);
744 bzero(&utsname, sizeof(utsname));
745 strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
746 getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
747 getcreddomainname(td->td_ucred, utsname.domainname, LINUX_MAX_UTSNAME);
748 strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
749 strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
750 for (p = utsname.version; *p != '\0'; ++p)
755 strlcpy(utsname.machine, linux_kplatform, LINUX_MAX_UTSNAME);
757 return (copyout(&utsname, args->buf, sizeof(utsname)));
766 linux_utime(struct thread *td, struct linux_utime_args *args)
768 struct timeval tv[2], *tvp;
769 struct l_utimbuf lut;
773 LCONVPATHEXIST(td, args->fname, &fname);
777 printf(ARGS(utime, "%s, *"), fname);
781 if ((error = copyin(args->times, &lut, sizeof lut))) {
785 tv[0].tv_sec = lut.l_actime;
787 tv[1].tv_sec = lut.l_modtime;
793 error = kern_utimesat(td, AT_FDCWD, fname, UIO_SYSSPACE, tvp,
800 linux_utimes(struct thread *td, struct linux_utimes_args *args)
803 struct timeval tv[2], *tvp = NULL;
807 LCONVPATHEXIST(td, args->fname, &fname);
811 printf(ARGS(utimes, "%s, *"), fname);
814 if (args->tptr != NULL) {
815 if ((error = copyin(args->tptr, ltv, sizeof ltv))) {
819 tv[0].tv_sec = ltv[0].tv_sec;
820 tv[0].tv_usec = ltv[0].tv_usec;
821 tv[1].tv_sec = ltv[1].tv_sec;
822 tv[1].tv_usec = ltv[1].tv_usec;
826 error = kern_utimesat(td, AT_FDCWD, fname, UIO_SYSSPACE,
833 linux_utimensat_nsec_valid(l_long nsec)
836 if (nsec == LINUX_UTIME_OMIT || nsec == LINUX_UTIME_NOW)
838 if (nsec >= 0 && nsec <= 999999999)
844 linux_utimensat(struct thread *td, struct linux_utimensat_args *args)
846 struct l_timespec l_times[2];
847 struct timespec times[2], *timesp = NULL;
849 int error, dfd, flags = 0;
851 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
854 if (ldebug(utimensat))
855 printf(ARGS(utimensat, "%d, *"), dfd);
858 if (args->flags & ~LINUX_AT_SYMLINK_NOFOLLOW)
861 if (args->times != NULL) {
862 error = copyin(args->times, l_times, sizeof(l_times));
866 if (linux_utimensat_nsec_valid(l_times[0].tv_nsec) != 0 ||
867 linux_utimensat_nsec_valid(l_times[1].tv_nsec) != 0)
870 times[0].tv_sec = l_times[0].tv_sec;
871 switch (l_times[0].tv_nsec)
873 case LINUX_UTIME_OMIT:
874 times[0].tv_nsec = UTIME_OMIT;
876 case LINUX_UTIME_NOW:
877 times[0].tv_nsec = UTIME_NOW;
880 times[0].tv_nsec = l_times[0].tv_nsec;
883 times[1].tv_sec = l_times[1].tv_sec;
884 switch (l_times[1].tv_nsec)
886 case LINUX_UTIME_OMIT:
887 times[1].tv_nsec = UTIME_OMIT;
889 case LINUX_UTIME_NOW:
890 times[1].tv_nsec = UTIME_NOW;
893 times[1].tv_nsec = l_times[1].tv_nsec;
898 /* This breaks POSIX, but is what the Linux kernel does
899 * _on purpose_ (documented in the man page for utimensat(2)),
900 * so we must follow that behaviour. */
901 if (times[0].tv_nsec == UTIME_OMIT &&
902 times[1].tv_nsec == UTIME_OMIT)
906 if (args->pathname != NULL)
907 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
908 else if (args->flags != 0)
911 if (args->flags & LINUX_AT_SYMLINK_NOFOLLOW)
912 flags |= AT_SYMLINK_NOFOLLOW;
915 error = kern_futimens(td, dfd, timesp, UIO_SYSSPACE);
917 error = kern_utimensat(td, dfd, path, UIO_SYSSPACE, timesp,
918 UIO_SYSSPACE, flags);
926 linux_futimesat(struct thread *td, struct linux_futimesat_args *args)
929 struct timeval tv[2], *tvp = NULL;
933 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
934 LCONVPATHEXIST_AT(td, args->filename, &fname, dfd);
937 if (ldebug(futimesat))
938 printf(ARGS(futimesat, "%s, *"), fname);
941 if (args->utimes != NULL) {
942 if ((error = copyin(args->utimes, ltv, sizeof ltv))) {
946 tv[0].tv_sec = ltv[0].tv_sec;
947 tv[0].tv_usec = ltv[0].tv_usec;
948 tv[1].tv_sec = ltv[1].tv_sec;
949 tv[1].tv_usec = ltv[1].tv_usec;
953 error = kern_utimesat(td, dfd, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
959 linux_common_wait(struct thread *td, int pid, int *status,
960 int options, struct rusage *ru)
964 error = kern_wait(td, pid, &tmpstat, options, ru);
970 if (WIFSIGNALED(tmpstat))
971 tmpstat = (tmpstat & 0xffffff80) |
972 bsd_to_linux_signal(WTERMSIG(tmpstat));
973 else if (WIFSTOPPED(tmpstat))
974 tmpstat = (tmpstat & 0xffff00ff) |
975 (bsd_to_linux_signal(WSTOPSIG(tmpstat)) << 8);
976 else if (WIFCONTINUED(tmpstat))
978 error = copyout(&tmpstat, status, sizeof(int));
984 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
986 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
988 struct linux_wait4_args wait4_args;
992 printf(ARGS(waitpid, "%d, %p, %d"),
993 args->pid, (void *)args->status, args->options);
996 wait4_args.pid = args->pid;
997 wait4_args.status = args->status;
998 wait4_args.options = args->options;
999 wait4_args.rusage = NULL;
1001 return (linux_wait4(td, &wait4_args));
1003 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1006 linux_wait4(struct thread *td, struct linux_wait4_args *args)
1009 struct rusage ru, *rup;
1013 printf(ARGS(wait4, "%d, %p, %d, %p"),
1014 args->pid, (void *)args->status, args->options,
1015 (void *)args->rusage);
1017 if (args->options & ~(LINUX_WUNTRACED | LINUX_WNOHANG |
1018 LINUX_WCONTINUED | __WCLONE | __WNOTHREAD | __WALL))
1022 linux_to_bsd_waitopts(args->options, &options);
1024 if (args->rusage != NULL)
1028 error = linux_common_wait(td, args->pid, args->status, options, rup);
1031 if (args->rusage != NULL)
1032 error = linux_copyout_rusage(&ru, args->rusage);
1037 linux_waitid(struct thread *td, struct linux_waitid_args *args)
1039 int status, options, sig;
1040 struct __wrusage wru;
1048 linux_to_bsd_waitopts(args->options, &options);
1050 if (options & ~(WNOHANG | WNOWAIT | WEXITED | WUNTRACED | WCONTINUED))
1052 if (!(options & (WEXITED | WUNTRACED | WCONTINUED)))
1055 switch (args->idtype) {
1073 error = kern_wait6(td, idtype, args->id, &status, options,
1077 if (args->rusage != NULL) {
1078 error = linux_copyout_rusage(&wru.wru_children,
1083 if (args->info != NULL) {
1085 if (td->td_retval[0] == 0)
1086 bzero(&lsi, sizeof(lsi));
1088 sig = bsd_to_linux_signal(siginfo.si_signo);
1089 siginfo_to_lsiginfo(&siginfo, &lsi, sig);
1091 error = copyout(&lsi, args->info, sizeof(lsi));
1093 td->td_retval[0] = 0;
1099 linux_mknod(struct thread *td, struct linux_mknod_args *args)
1104 LCONVPATHCREAT(td, args->path, &path);
1108 printf(ARGS(mknod, "%s, %d, %ju"), path, args->mode,
1109 (uintmax_t)args->dev);
1112 switch (args->mode & S_IFMT) {
1115 error = kern_mkfifoat(td, AT_FDCWD, path, UIO_SYSSPACE,
1121 error = kern_mknodat(td, AT_FDCWD, path, UIO_SYSSPACE,
1122 args->mode, args->dev);
1130 args->mode |= S_IFREG;
1133 error = kern_openat(td, AT_FDCWD, 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 rlimit bsd_rlim;
1440 if (ldebug(old_getrlimit))
1441 printf(ARGS(old_getrlimit, "%d, %p"),
1442 args->resource, (void *)args->rlim);
1445 if (args->resource >= LINUX_RLIM_NLIMITS)
1448 which = linux_to_bsd_resource[args->resource];
1452 lim_rlimit(td, which, &bsd_rlim);
1454 #ifdef COMPAT_LINUX32
1455 rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1456 if (rlim.rlim_cur == UINT_MAX)
1457 rlim.rlim_cur = INT_MAX;
1458 rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1459 if (rlim.rlim_max == UINT_MAX)
1460 rlim.rlim_max = INT_MAX;
1462 rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1463 if (rlim.rlim_cur == ULONG_MAX)
1464 rlim.rlim_cur = LONG_MAX;
1465 rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1466 if (rlim.rlim_max == ULONG_MAX)
1467 rlim.rlim_max = LONG_MAX;
1469 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1471 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1474 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1476 struct l_rlimit rlim;
1477 struct rlimit bsd_rlim;
1481 if (ldebug(getrlimit))
1482 printf(ARGS(getrlimit, "%d, %p"),
1483 args->resource, (void *)args->rlim);
1486 if (args->resource >= LINUX_RLIM_NLIMITS)
1489 which = linux_to_bsd_resource[args->resource];
1493 lim_rlimit(td, which, &bsd_rlim);
1495 rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1496 rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1497 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1501 linux_sched_setscheduler(struct thread *td,
1502 struct linux_sched_setscheduler_args *args)
1504 struct sched_param sched_param;
1509 if (ldebug(sched_setscheduler))
1510 printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1511 args->pid, args->policy, (const void *)args->param);
1514 switch (args->policy) {
1515 case LINUX_SCHED_OTHER:
1516 policy = SCHED_OTHER;
1518 case LINUX_SCHED_FIFO:
1519 policy = SCHED_FIFO;
1521 case LINUX_SCHED_RR:
1528 error = copyin(args->param, &sched_param, sizeof(sched_param));
1532 tdt = linux_tdfind(td, args->pid, -1);
1536 error = kern_sched_setscheduler(td, tdt, policy, &sched_param);
1537 PROC_UNLOCK(tdt->td_proc);
1542 linux_sched_getscheduler(struct thread *td,
1543 struct linux_sched_getscheduler_args *args)
1549 if (ldebug(sched_getscheduler))
1550 printf(ARGS(sched_getscheduler, "%d"), args->pid);
1553 tdt = linux_tdfind(td, args->pid, -1);
1557 error = kern_sched_getscheduler(td, tdt, &policy);
1558 PROC_UNLOCK(tdt->td_proc);
1562 td->td_retval[0] = LINUX_SCHED_OTHER;
1565 td->td_retval[0] = LINUX_SCHED_FIFO;
1568 td->td_retval[0] = LINUX_SCHED_RR;
1575 linux_sched_get_priority_max(struct thread *td,
1576 struct linux_sched_get_priority_max_args *args)
1578 struct sched_get_priority_max_args bsd;
1581 if (ldebug(sched_get_priority_max))
1582 printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1585 switch (args->policy) {
1586 case LINUX_SCHED_OTHER:
1587 bsd.policy = SCHED_OTHER;
1589 case LINUX_SCHED_FIFO:
1590 bsd.policy = SCHED_FIFO;
1592 case LINUX_SCHED_RR:
1593 bsd.policy = SCHED_RR;
1598 return (sys_sched_get_priority_max(td, &bsd));
1602 linux_sched_get_priority_min(struct thread *td,
1603 struct linux_sched_get_priority_min_args *args)
1605 struct sched_get_priority_min_args bsd;
1608 if (ldebug(sched_get_priority_min))
1609 printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1612 switch (args->policy) {
1613 case LINUX_SCHED_OTHER:
1614 bsd.policy = SCHED_OTHER;
1616 case LINUX_SCHED_FIFO:
1617 bsd.policy = SCHED_FIFO;
1619 case LINUX_SCHED_RR:
1620 bsd.policy = SCHED_RR;
1625 return (sys_sched_get_priority_min(td, &bsd));
1628 #define REBOOT_CAD_ON 0x89abcdef
1629 #define REBOOT_CAD_OFF 0
1630 #define REBOOT_HALT 0xcdef0123
1631 #define REBOOT_RESTART 0x01234567
1632 #define REBOOT_RESTART2 0xA1B2C3D4
1633 #define REBOOT_POWEROFF 0x4321FEDC
1634 #define REBOOT_MAGIC1 0xfee1dead
1635 #define REBOOT_MAGIC2 0x28121969
1636 #define REBOOT_MAGIC2A 0x05121996
1637 #define REBOOT_MAGIC2B 0x16041998
1640 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1642 struct reboot_args bsd_args;
1646 printf(ARGS(reboot, "0x%x"), args->cmd);
1649 if (args->magic1 != REBOOT_MAGIC1)
1652 switch (args->magic2) {
1654 case REBOOT_MAGIC2A:
1655 case REBOOT_MAGIC2B:
1661 switch (args->cmd) {
1663 case REBOOT_CAD_OFF:
1664 return (priv_check(td, PRIV_REBOOT));
1666 bsd_args.opt = RB_HALT;
1668 case REBOOT_RESTART:
1669 case REBOOT_RESTART2:
1672 case REBOOT_POWEROFF:
1673 bsd_args.opt = RB_POWEROFF;
1678 return (sys_reboot(td, &bsd_args));
1683 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1684 * td->td_retval[1] when COMPAT_43 is defined. This clobbers registers that
1685 * are assumed to be preserved. The following lightweight syscalls fixes
1686 * this. See also linux_getgid16() and linux_getuid16() in linux_uid16.c
1688 * linux_getpid() - MP SAFE
1689 * linux_getgid() - MP SAFE
1690 * linux_getuid() - MP SAFE
1694 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1699 printf(ARGS(getpid, ""));
1701 td->td_retval[0] = td->td_proc->p_pid;
1707 linux_gettid(struct thread *td, struct linux_gettid_args *args)
1709 struct linux_emuldata *em;
1713 printf(ARGS(gettid, ""));
1717 KASSERT(em != NULL, ("gettid: emuldata not found.\n"));
1719 td->td_retval[0] = em->em_tid;
1726 linux_getppid(struct thread *td, struct linux_getppid_args *args)
1730 if (ldebug(getppid))
1731 printf(ARGS(getppid, ""));
1734 td->td_retval[0] = kern_getppid(td);
1739 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1744 printf(ARGS(getgid, ""));
1747 td->td_retval[0] = td->td_ucred->cr_rgid;
1752 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1757 printf(ARGS(getuid, ""));
1760 td->td_retval[0] = td->td_ucred->cr_ruid;
1766 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1768 struct getsid_args bsd;
1772 printf(ARGS(getsid, "%i"), args->pid);
1775 bsd.pid = args->pid;
1776 return (sys_getsid(td, &bsd));
1780 linux_nosys(struct thread *td, struct nosys_args *ignore)
1787 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1789 struct getpriority_args bsd_args;
1793 if (ldebug(getpriority))
1794 printf(ARGS(getpriority, "%i, %i"), args->which, args->who);
1797 bsd_args.which = args->which;
1798 bsd_args.who = args->who;
1799 error = sys_getpriority(td, &bsd_args);
1800 td->td_retval[0] = 20 - td->td_retval[0];
1805 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1810 if (ldebug(sethostname))
1811 printf(ARGS(sethostname, "*, %i"), args->len);
1815 name[1] = KERN_HOSTNAME;
1816 return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1821 linux_setdomainname(struct thread *td, struct linux_setdomainname_args *args)
1826 if (ldebug(setdomainname))
1827 printf(ARGS(setdomainname, "*, %i"), args->len);
1831 name[1] = KERN_NISDOMAINNAME;
1832 return (userland_sysctl(td, name, 2, 0, 0, 0, args->name,
1837 linux_exit_group(struct thread *td, struct linux_exit_group_args *args)
1841 if (ldebug(exit_group))
1842 printf(ARGS(exit_group, "%i"), args->error_code);
1845 LINUX_CTR2(exit_group, "thread(%d) (%d)", td->td_tid,
1849 * XXX: we should send a signal to the parent if
1850 * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?)
1851 * as it doesnt occur often.
1853 exit1(td, args->error_code, 0);
1857 #define _LINUX_CAPABILITY_VERSION 0x19980330
1859 struct l_user_cap_header {
1864 struct l_user_cap_data {
1871 linux_capget(struct thread *td, struct linux_capget_args *args)
1873 struct l_user_cap_header luch;
1874 struct l_user_cap_data lucd;
1877 if (args->hdrp == NULL)
1880 error = copyin(args->hdrp, &luch, sizeof(luch));
1884 if (luch.version != _LINUX_CAPABILITY_VERSION) {
1885 luch.version = _LINUX_CAPABILITY_VERSION;
1886 error = copyout(&luch, args->hdrp, sizeof(luch));
1897 * The current implementation doesn't support setting
1898 * a capability (it's essentially a stub) so indicate
1899 * that no capabilities are currently set or available
1902 bzero (&lucd, sizeof(lucd));
1903 error = copyout(&lucd, args->datap, sizeof(lucd));
1910 linux_capset(struct thread *td, struct linux_capset_args *args)
1912 struct l_user_cap_header luch;
1913 struct l_user_cap_data lucd;
1916 if (args->hdrp == NULL || args->datap == NULL)
1919 error = copyin(args->hdrp, &luch, sizeof(luch));
1923 if (luch.version != _LINUX_CAPABILITY_VERSION) {
1924 luch.version = _LINUX_CAPABILITY_VERSION;
1925 error = copyout(&luch, args->hdrp, sizeof(luch));
1934 error = copyin(args->datap, &lucd, sizeof(lucd));
1938 /* We currently don't support setting any capabilities. */
1939 if (lucd.effective || lucd.permitted || lucd.inheritable) {
1941 "capset effective=0x%x, permitted=0x%x, "
1942 "inheritable=0x%x is not implemented",
1943 (int)lucd.effective, (int)lucd.permitted,
1944 (int)lucd.inheritable);
1952 linux_prctl(struct thread *td, struct linux_prctl_args *args)
1954 int error = 0, max_size;
1955 struct proc *p = td->td_proc;
1956 char comm[LINUX_MAX_COMM_LEN];
1957 struct linux_emuldata *em;
1962 printf(ARGS(prctl, "%d, %ju, %ju, %ju, %ju"), args->option,
1963 (uintmax_t)args->arg2, (uintmax_t)args->arg3,
1964 (uintmax_t)args->arg4, (uintmax_t)args->arg5);
1967 switch (args->option) {
1968 case LINUX_PR_SET_PDEATHSIG:
1969 if (!LINUX_SIG_VALID(args->arg2))
1972 KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1973 em->pdeath_signal = args->arg2;
1975 case LINUX_PR_GET_PDEATHSIG:
1977 KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1978 pdeath_signal = em->pdeath_signal;
1979 error = copyout(&pdeath_signal,
1980 (void *)(register_t)args->arg2,
1981 sizeof(pdeath_signal));
1983 case LINUX_PR_GET_KEEPCAPS:
1985 * Indicate that we always clear the effective and
1986 * permitted capability sets when the user id becomes
1987 * non-zero (actually the capability sets are simply
1988 * always zero in the current implementation).
1990 td->td_retval[0] = 0;
1992 case LINUX_PR_SET_KEEPCAPS:
1994 * Ignore requests to keep the effective and permitted
1995 * capability sets when the user id becomes non-zero.
1998 case LINUX_PR_SET_NAME:
2000 * To be on the safe side we need to make sure to not
2001 * overflow the size a linux program expects. We already
2002 * do this here in the copyin, so that we don't need to
2005 max_size = MIN(sizeof(comm), sizeof(p->p_comm));
2006 error = copyinstr((void *)(register_t)args->arg2, comm,
2009 /* Linux silently truncates the name if it is too long. */
2010 if (error == ENAMETOOLONG) {
2012 * XXX: copyinstr() isn't documented to populate the
2013 * array completely, so do a copyin() to be on the
2014 * safe side. This should be changed in case
2015 * copyinstr() is changed to guarantee this.
2017 error = copyin((void *)(register_t)args->arg2, comm,
2019 comm[max_size - 1] = '\0';
2025 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
2028 case LINUX_PR_GET_NAME:
2030 strlcpy(comm, p->p_comm, sizeof(comm));
2032 error = copyout(comm, (void *)(register_t)args->arg2,
2044 linux_sched_setparam(struct thread *td,
2045 struct linux_sched_setparam_args *uap)
2047 struct sched_param sched_param;
2052 if (ldebug(sched_setparam))
2053 printf(ARGS(sched_setparam, "%d, *"), uap->pid);
2056 error = copyin(uap->param, &sched_param, sizeof(sched_param));
2060 tdt = linux_tdfind(td, uap->pid, -1);
2064 error = kern_sched_setparam(td, tdt, &sched_param);
2065 PROC_UNLOCK(tdt->td_proc);
2070 linux_sched_getparam(struct thread *td,
2071 struct linux_sched_getparam_args *uap)
2073 struct sched_param sched_param;
2078 if (ldebug(sched_getparam))
2079 printf(ARGS(sched_getparam, "%d, *"), uap->pid);
2082 tdt = linux_tdfind(td, uap->pid, -1);
2086 error = kern_sched_getparam(td, tdt, &sched_param);
2087 PROC_UNLOCK(tdt->td_proc);
2089 error = copyout(&sched_param, uap->param,
2090 sizeof(sched_param));
2095 * Get affinity of a process.
2098 linux_sched_getaffinity(struct thread *td,
2099 struct linux_sched_getaffinity_args *args)
2105 if (ldebug(sched_getaffinity))
2106 printf(ARGS(sched_getaffinity, "%d, %d, *"), args->pid,
2109 if (args->len < sizeof(cpuset_t))
2112 tdt = linux_tdfind(td, args->pid, -1);
2116 PROC_UNLOCK(tdt->td_proc);
2118 error = kern_cpuset_getaffinity(td, CPU_LEVEL_WHICH, CPU_WHICH_TID,
2119 tdt->td_tid, sizeof(cpuset_t), (cpuset_t *)args->user_mask_ptr);
2121 td->td_retval[0] = sizeof(cpuset_t);
2127 * Set affinity of a process.
2130 linux_sched_setaffinity(struct thread *td,
2131 struct linux_sched_setaffinity_args *args)
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);
2149 return (kern_cpuset_setaffinity(td, CPU_LEVEL_WHICH, CPU_WHICH_TID,
2150 tdt->td_tid, sizeof(cpuset_t), (cpuset_t *) args->user_mask_ptr));
2153 struct linux_rlimit64 {
2159 linux_prlimit64(struct thread *td, struct linux_prlimit64_args *args)
2161 struct rlimit rlim, nrlim;
2162 struct linux_rlimit64 lrlim;
2169 if (ldebug(prlimit64))
2170 printf(ARGS(prlimit64, "%d, %d, %p, %p"), args->pid,
2171 args->resource, (void *)args->new, (void *)args->old);
2174 if (args->resource >= LINUX_RLIM_NLIMITS)
2177 which = linux_to_bsd_resource[args->resource];
2181 if (args->new != NULL) {
2183 * Note. Unlike FreeBSD where rlim is signed 64-bit Linux
2184 * rlim is unsigned 64-bit. FreeBSD treats negative limits
2185 * as INFINITY so we do not need a conversion even.
2187 error = copyin(args->new, &nrlim, sizeof(nrlim));
2192 flags = PGET_HOLD | PGET_NOTWEXIT;
2193 if (args->new != NULL)
2194 flags |= PGET_CANDEBUG;
2196 flags |= PGET_CANSEE;
2197 error = pget(args->pid, flags, &p);
2201 if (args->old != NULL) {
2203 lim_rlimit_proc(p, which, &rlim);
2205 if (rlim.rlim_cur == RLIM_INFINITY)
2206 lrlim.rlim_cur = LINUX_RLIM_INFINITY;
2208 lrlim.rlim_cur = rlim.rlim_cur;
2209 if (rlim.rlim_max == RLIM_INFINITY)
2210 lrlim.rlim_max = LINUX_RLIM_INFINITY;
2212 lrlim.rlim_max = rlim.rlim_max;
2213 error = copyout(&lrlim, args->old, sizeof(lrlim));
2218 if (args->new != NULL)
2219 error = kern_proc_setrlimit(td, p, which, &nrlim);
2227 linux_pselect6(struct thread *td, struct linux_pselect6_args *args)
2229 struct timeval utv, tv0, tv1, *tvp;
2230 struct l_pselect6arg lpse6;
2231 struct l_timespec lts;
2232 struct timespec uts;
2239 if (args->sig != NULL) {
2240 error = copyin(args->sig, &lpse6, sizeof(lpse6));
2243 if (lpse6.ss_len != sizeof(l_ss))
2245 if (lpse6.ss != 0) {
2246 error = copyin(PTRIN(lpse6.ss), &l_ss,
2250 linux_to_bsd_sigset(&l_ss, &ss);
2256 * Currently glibc changes nanosecond number to microsecond.
2257 * This mean losing precision but for now it is hardly seen.
2259 if (args->tsp != NULL) {
2260 error = copyin(args->tsp, <s, sizeof(lts));
2263 error = linux_to_native_timespec(&uts, <s);
2267 TIMESPEC_TO_TIMEVAL(&utv, &uts);
2268 if (itimerfix(&utv))
2276 error = kern_pselect(td, args->nfds, args->readfds, args->writefds,
2277 args->exceptfds, tvp, ssp, LINUX_NFDBITS);
2279 if (error == 0 && args->tsp != NULL) {
2280 if (td->td_retval[0] != 0) {
2282 * Compute how much time was left of the timeout,
2283 * by subtracting the current time and the time
2284 * before we started the call, and subtracting
2285 * that result from the user-supplied value.
2289 timevalsub(&tv1, &tv0);
2290 timevalsub(&utv, &tv1);
2296 TIMEVAL_TO_TIMESPEC(&utv, &uts);
2298 error = native_to_linux_timespec(<s, &uts);
2300 error = copyout(<s, args->tsp, sizeof(lts));
2307 linux_ppoll(struct thread *td, struct linux_ppoll_args *args)
2309 struct timespec ts0, ts1;
2310 struct l_timespec lts;
2311 struct timespec uts, *tsp;
2317 if (args->sset != NULL) {
2318 if (args->ssize != sizeof(l_ss))
2320 error = copyin(args->sset, &l_ss, sizeof(l_ss));
2323 linux_to_bsd_sigset(&l_ss, &ss);
2327 if (args->tsp != NULL) {
2328 error = copyin(args->tsp, <s, sizeof(lts));
2331 error = linux_to_native_timespec(&uts, <s);
2340 error = kern_poll(td, args->fds, args->nfds, tsp, ssp);
2342 if (error == 0 && args->tsp != NULL) {
2343 if (td->td_retval[0]) {
2345 timespecsub(&ts1, &ts0);
2346 timespecsub(&uts, &ts1);
2348 timespecclear(&uts);
2350 timespecclear(&uts);
2352 error = native_to_linux_timespec(<s, &uts);
2354 error = copyout(<s, args->tsp, sizeof(lts));
2360 #if defined(DEBUG) || defined(KTR)
2361 /* XXX: can be removed when every ldebug(...) and KTR stuff are removed. */
2363 #ifdef COMPAT_LINUX32
2364 #define L_MAXSYSCALL LINUX32_SYS_MAXSYSCALL
2366 #define L_MAXSYSCALL LINUX_SYS_MAXSYSCALL
2369 u_char linux_debug_map[howmany(L_MAXSYSCALL, sizeof(u_char))];
2372 linux_debug(int syscall, int toggle, int global)
2376 char c = toggle ? 0 : 0xff;
2378 memset(linux_debug_map, c, sizeof(linux_debug_map));
2381 if (syscall < 0 || syscall >= L_MAXSYSCALL)
2384 clrbit(linux_debug_map, syscall);
2386 setbit(linux_debug_map, syscall);
2392 * Usage: sysctl linux.debug=<syscall_nr>.<0/1>
2394 * E.g.: sysctl linux.debug=21.0
2396 * As a special case, syscall "all" will apply to all syscalls globally.
2398 #define LINUX_MAX_DEBUGSTR 16
2400 linux_sysctl_debug(SYSCTL_HANDLER_ARGS)
2402 char value[LINUX_MAX_DEBUGSTR], *p;
2403 int error, sysc, toggle;
2407 error = sysctl_handle_string(oidp, value, LINUX_MAX_DEBUGSTR, req);
2408 if (error || req->newptr == NULL)
2410 for (p = value; *p != '\0' && *p != '.'; p++);
2414 sysc = strtol(value, NULL, 0);
2415 toggle = strtol(p, NULL, 0);
2416 if (strcmp(value, "all") == 0)
2418 error = linux_debug(sysc, toggle, global);
2422 #endif /* DEBUG || KTR */
2425 linux_sched_rr_get_interval(struct thread *td,
2426 struct linux_sched_rr_get_interval_args *uap)
2429 struct l_timespec lts;
2434 * According to man in case the invalid pid specified
2435 * EINVAL should be returned.
2440 tdt = linux_tdfind(td, uap->pid, -1);
2444 error = kern_sched_rr_get_interval_td(td, tdt, &ts);
2445 PROC_UNLOCK(tdt->td_proc);
2448 error = 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;
2518 linux_getrandom(struct thread *td, struct linux_getrandom_args *args)
2524 if (args->flags & ~(LINUX_GRND_NONBLOCK|LINUX_GRND_RANDOM))
2526 if (args->count > INT_MAX)
2527 args->count = INT_MAX;
2529 iov.iov_base = args->buf;
2530 iov.iov_len = args->count;
2534 uio.uio_resid = iov.iov_len;
2535 uio.uio_segflg = UIO_USERSPACE;
2536 uio.uio_rw = UIO_READ;
2539 error = read_random_uio(&uio, args->flags & LINUX_GRND_NONBLOCK);
2541 td->td_retval[0] = args->count - uio.uio_resid;
2546 linux_mincore(struct thread *td, struct linux_mincore_args *args)
2549 /* Needs to be page-aligned */
2550 if (args->start & PAGE_MASK)
2552 return (kern_mincore(td, args->start, args->len, args->vec));