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_util.h>
93 #include <compat/linux/linux_sysproto.h>
94 #include <compat/linux/linux_emul.h>
95 #include <compat/linux/linux_misc.h>
98 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE);
100 /* Linuxulator-global DTrace probes */
101 LIN_SDT_PROBE_DECLARE(locks, emul_lock, locked);
102 LIN_SDT_PROBE_DECLARE(locks, emul_lock, unlock);
103 LIN_SDT_PROBE_DECLARE(locks, emul_shared_rlock, locked);
104 LIN_SDT_PROBE_DECLARE(locks, emul_shared_rlock, unlock);
105 LIN_SDT_PROBE_DECLARE(locks, emul_shared_wlock, locked);
106 LIN_SDT_PROBE_DECLARE(locks, emul_shared_wlock, unlock);
108 int stclohz; /* Statistics clock frequency */
110 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
111 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
112 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
113 RLIMIT_MEMLOCK, RLIMIT_AS
117 l_long uptime; /* Seconds since boot */
118 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */
119 #define LINUX_SYSINFO_LOADS_SCALE 65536
120 l_ulong totalram; /* Total usable main memory size */
121 l_ulong freeram; /* Available memory size */
122 l_ulong sharedram; /* Amount of shared memory */
123 l_ulong bufferram; /* Memory used by buffers */
124 l_ulong totalswap; /* Total swap space size */
125 l_ulong freeswap; /* swap space still available */
126 l_ushort procs; /* Number of current processes */
131 char _f[20-2*sizeof(l_long)-sizeof(l_int)]; /* padding */
134 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args)
136 struct l_sysinfo sysinfo;
144 sysinfo.uptime = ts.tv_sec;
146 /* Use the information from the mib to get our load averages */
147 for (i = 0; i < 3; i++)
148 sysinfo.loads[i] = averunnable.ldavg[i] *
149 LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale;
151 sysinfo.totalram = physmem * PAGE_SIZE;
152 sysinfo.freeram = sysinfo.totalram - cnt.v_wire_count * PAGE_SIZE;
154 sysinfo.sharedram = 0;
155 mtx_lock(&vm_object_list_mtx);
156 TAILQ_FOREACH(object, &vm_object_list, object_list)
157 if (object->shadow_count > 1)
158 sysinfo.sharedram += object->resident_page_count;
159 mtx_unlock(&vm_object_list_mtx);
161 sysinfo.sharedram *= PAGE_SIZE;
162 sysinfo.bufferram = 0;
164 swap_pager_status(&i, &j);
165 sysinfo.totalswap = i * PAGE_SIZE;
166 sysinfo.freeswap = (i - j) * PAGE_SIZE;
168 sysinfo.procs = nprocs;
170 /* The following are only present in newer Linux kernels. */
171 sysinfo.totalbig = 0;
173 sysinfo.mem_unit = 1;
175 return (copyout(&sysinfo, args->info, sizeof(sysinfo)));
179 linux_alarm(struct thread *td, struct linux_alarm_args *args)
181 struct itimerval it, old_it;
187 printf(ARGS(alarm, "%u"), args->secs);
195 it.it_value.tv_sec = (long) secs;
196 it.it_value.tv_usec = 0;
197 it.it_interval.tv_sec = 0;
198 it.it_interval.tv_usec = 0;
199 error = kern_setitimer(td, ITIMER_REAL, &it, &old_it);
202 if (timevalisset(&old_it.it_value)) {
203 if (old_it.it_value.tv_usec != 0)
204 old_it.it_value.tv_sec++;
205 td->td_retval[0] = old_it.it_value.tv_sec;
211 linux_brk(struct thread *td, struct linux_brk_args *args)
213 struct vmspace *vm = td->td_proc->p_vmspace;
214 vm_offset_t new, old;
215 struct obreak_args /* {
221 printf(ARGS(brk, "%p"), (void *)(uintptr_t)args->dsend);
223 old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
224 new = (vm_offset_t)args->dsend;
225 tmp.nsize = (char *)new;
226 if (((caddr_t)new > vm->vm_daddr) && !sys_obreak(td, &tmp))
227 td->td_retval[0] = (long)new;
229 td->td_retval[0] = (long)old;
234 #if defined(__i386__)
235 /* XXX: what about amd64/linux32? */
238 linux_uselib(struct thread *td, struct linux_uselib_args *args)
245 unsigned long file_offset;
246 unsigned long bss_size;
249 int error, locked, vfslocked, writecount;
251 LCONVPATHEXIST(td, args->library, &library);
255 printf(ARGS(uselib, "%s"), library);
263 NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
264 UIO_SYSSPACE, library, td);
271 vfslocked = NDHASGIANT(&ni);
272 NDFREE(&ni, NDF_ONLY_PNBUF);
275 * From here on down, we have a locked vnode that must be unlocked.
276 * XXX: The code below largely duplicates exec_check_permissions().
281 error = VOP_GET_WRITECOUNT(vp, &writecount);
284 if (writecount != 0) {
290 error = VOP_GETATTR(vp, &attr, td->td_ucred);
294 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
295 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
296 /* EACCESS is what exec(2) returns. */
302 if (attr.va_size == 0) {
307 /* Can we access it? */
308 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
313 * XXX: This should use vn_open() so that it is properly authorized,
314 * and to reduce code redundancy all over the place here.
315 * XXX: Not really, it duplicates far more of exec_check_permissions()
319 error = mac_vnode_check_open(td->td_ucred, vp, VREAD);
323 error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL);
327 /* Pull in executable header into exec_map */
328 error = vm_mmap(exec_map, (vm_offset_t *)&a_out, PAGE_SIZE,
329 VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 0);
333 /* Is it a Linux binary ? */
334 if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
340 * While we are here, we should REALLY do some more checks
343 /* Set file/virtual offset based on a.out variant. */
344 switch ((int)(a_out->a_magic & 0xffff)) {
345 case 0413: /* ZMAGIC */
348 case 0314: /* QMAGIC */
356 bss_size = round_page(a_out->a_bss);
358 /* Check various fields in header for validity/bounds. */
359 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
364 /* text + data can't exceed file size */
365 if (a_out->a_data + a_out->a_text > attr.va_size) {
371 * text/data/bss must not exceed limits
372 * XXX - this is not complete. it should check current usage PLUS
373 * the resources needed by this library.
375 PROC_LOCK(td->td_proc);
376 if (a_out->a_text > maxtsiz ||
377 a_out->a_data + bss_size > lim_cur(td->td_proc, RLIMIT_DATA) ||
378 racct_set(td->td_proc, RACCT_DATA, a_out->a_data +
380 PROC_UNLOCK(td->td_proc);
384 PROC_UNLOCK(td->td_proc);
387 * Prevent more writers.
388 * XXX: Note that if any of the VM operations fail below we don't
394 * Lock no longer needed
398 VFS_UNLOCK_GIANT(vfslocked);
401 * Check if file_offset page aligned. Currently we cannot handle
402 * misalinged file offsets, and so we read in the entire image
405 if (file_offset & PAGE_MASK) {
407 printf("uselib: Non page aligned binary %lu\n", file_offset);
409 /* Map text+data read/write/execute */
411 /* a_entry is the load address and is page aligned */
412 vmaddr = trunc_page(a_out->a_entry);
414 /* get anon user mapping, read+write+execute */
415 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
416 &vmaddr, a_out->a_text + a_out->a_data, FALSE, VM_PROT_ALL,
421 error = vn_rdwr(UIO_READ, vp, (void *)vmaddr, file_offset,
422 a_out->a_text + a_out->a_data, UIO_USERSPACE, 0,
423 td->td_ucred, NOCRED, &aresid, td);
432 printf("uselib: Page aligned binary %lu\n", file_offset);
435 * for QMAGIC, a_entry is 20 bytes beyond the load address
436 * to skip the executable header
438 vmaddr = trunc_page(a_out->a_entry);
441 * Map it all into the process's space as a single
442 * copy-on-write "data" segment.
444 error = vm_mmap(&td->td_proc->p_vmspace->vm_map, &vmaddr,
445 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
446 MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset);
451 printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long *)vmaddr)[0],
452 ((long *)vmaddr)[1]);
455 /* Calculate BSS start address */
456 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
459 /* allocate some 'anon' space */
460 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
461 &vmaddr, bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
467 /* Unlock vnode if needed */
470 VFS_UNLOCK_GIANT(vfslocked);
473 /* Release the temporary mapping. */
475 kmem_free_wakeup(exec_map, (vm_offset_t)a_out, PAGE_SIZE);
480 #endif /* __i386__ */
483 linux_select(struct thread *td, struct linux_select_args *args)
486 struct timeval tv0, tv1, utv, *tvp;
491 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
492 (void *)args->readfds, (void *)args->writefds,
493 (void *)args->exceptfds, (void *)args->timeout);
497 * Store current time for computation of the amount of
501 if ((error = copyin(args->timeout, <v, sizeof(ltv))))
503 utv.tv_sec = ltv.tv_sec;
504 utv.tv_usec = ltv.tv_usec;
507 printf(LMSG("incoming timeout (%jd/%ld)"),
508 (intmax_t)utv.tv_sec, utv.tv_usec);
511 if (itimerfix(&utv)) {
513 * The timeval was invalid. Convert it to something
514 * valid that will act as it does under Linux.
516 utv.tv_sec += utv.tv_usec / 1000000;
517 utv.tv_usec %= 1000000;
518 if (utv.tv_usec < 0) {
520 utv.tv_usec += 1000000;
530 error = kern_select(td, args->nfds, args->readfds, args->writefds,
531 args->exceptfds, tvp, sizeof(l_int) * 8);
535 printf(LMSG("real select returns %d"), error);
541 if (td->td_retval[0]) {
543 * Compute how much time was left of the timeout,
544 * by subtracting the current time and the time
545 * before we started the call, and subtracting
546 * that result from the user-supplied value.
549 timevalsub(&tv1, &tv0);
550 timevalsub(&utv, &tv1);
557 printf(LMSG("outgoing timeout (%jd/%ld)"),
558 (intmax_t)utv.tv_sec, utv.tv_usec);
560 ltv.tv_sec = utv.tv_sec;
561 ltv.tv_usec = utv.tv_usec;
562 if ((error = copyout(<v, args->timeout, sizeof(ltv))))
569 printf(LMSG("select_out -> %d"), error);
575 linux_mremap(struct thread *td, struct linux_mremap_args *args)
577 struct munmap_args /* {
585 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
586 (void *)(uintptr_t)args->addr,
587 (unsigned long)args->old_len,
588 (unsigned long)args->new_len,
589 (unsigned long)args->flags);
592 if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) {
593 td->td_retval[0] = 0;
598 * Check for the page alignment.
599 * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK.
601 if (args->addr & PAGE_MASK) {
602 td->td_retval[0] = 0;
606 args->new_len = round_page(args->new_len);
607 args->old_len = round_page(args->old_len);
609 if (args->new_len > args->old_len) {
610 td->td_retval[0] = 0;
614 if (args->new_len < args->old_len) {
616 (caddr_t)((uintptr_t)args->addr + args->new_len);
617 bsd_args.len = args->old_len - args->new_len;
618 error = sys_munmap(td, &bsd_args);
621 td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
625 #define LINUX_MS_ASYNC 0x0001
626 #define LINUX_MS_INVALIDATE 0x0002
627 #define LINUX_MS_SYNC 0x0004
630 linux_msync(struct thread *td, struct linux_msync_args *args)
632 struct msync_args bsd_args;
634 bsd_args.addr = (caddr_t)(uintptr_t)args->addr;
635 bsd_args.len = (uintptr_t)args->len;
636 bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
638 return (sys_msync(td, &bsd_args));
642 linux_time(struct thread *td, struct linux_time_args *args)
650 printf(ARGS(time, "*"));
655 if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
657 td->td_retval[0] = tm;
661 struct l_times_argv {
664 l_clock_t tms_cutime;
665 l_clock_t tms_cstime;
670 * Glibc versions prior to 2.2.1 always use hard-coded CLK_TCK value.
671 * Since 2.2.1 Glibc uses value exported from kernel via AT_CLKTCK
672 * auxiliary vector entry.
676 #define CONVOTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
677 #define CONVNTCK(r) (r.tv_sec * stclohz + r.tv_usec / (1000000 / stclohz))
679 #define CONVTCK(r) (linux_kernver(td) >= LINUX_KERNVER_2004000 ? \
680 CONVNTCK(r) : CONVOTCK(r))
683 linux_times(struct thread *td, struct linux_times_args *args)
685 struct timeval tv, utime, stime, cutime, cstime;
686 struct l_times_argv tms;
692 printf(ARGS(times, "*"));
695 if (args->buf != NULL) {
699 calcru(p, &utime, &stime);
701 calccru(p, &cutime, &cstime);
704 tms.tms_utime = CONVTCK(utime);
705 tms.tms_stime = CONVTCK(stime);
707 tms.tms_cutime = CONVTCK(cutime);
708 tms.tms_cstime = CONVTCK(cstime);
710 if ((error = copyout(&tms, args->buf, sizeof(tms))))
715 td->td_retval[0] = (int)CONVTCK(tv);
720 linux_newuname(struct thread *td, struct linux_newuname_args *args)
722 struct l_new_utsname utsname;
723 char osname[LINUX_MAX_UTSNAME];
724 char osrelease[LINUX_MAX_UTSNAME];
728 if (ldebug(newuname))
729 printf(ARGS(newuname, "*"));
732 linux_get_osname(td, osname);
733 linux_get_osrelease(td, osrelease);
735 bzero(&utsname, sizeof(utsname));
736 strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
737 getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
738 getcreddomainname(td->td_ucred, utsname.domainname, LINUX_MAX_UTSNAME);
739 strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
740 strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
741 for (p = utsname.version; *p != '\0'; ++p)
746 strlcpy(utsname.machine, linux_platform, LINUX_MAX_UTSNAME);
748 return (copyout(&utsname, args->buf, sizeof(utsname)));
751 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
758 linux_utime(struct thread *td, struct linux_utime_args *args)
760 struct timeval tv[2], *tvp;
761 struct l_utimbuf lut;
765 LCONVPATHEXIST(td, args->fname, &fname);
769 printf(ARGS(utime, "%s, *"), fname);
773 if ((error = copyin(args->times, &lut, sizeof lut))) {
777 tv[0].tv_sec = lut.l_actime;
779 tv[1].tv_sec = lut.l_modtime;
785 error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
791 linux_utimes(struct thread *td, struct linux_utimes_args *args)
794 struct timeval tv[2], *tvp = NULL;
798 LCONVPATHEXIST(td, args->fname, &fname);
802 printf(ARGS(utimes, "%s, *"), fname);
805 if (args->tptr != NULL) {
806 if ((error = copyin(args->tptr, ltv, sizeof ltv))) {
810 tv[0].tv_sec = ltv[0].tv_sec;
811 tv[0].tv_usec = ltv[0].tv_usec;
812 tv[1].tv_sec = ltv[1].tv_sec;
813 tv[1].tv_usec = ltv[1].tv_usec;
817 error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
823 linux_futimesat(struct thread *td, struct linux_futimesat_args *args)
826 struct timeval tv[2], *tvp = NULL;
830 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
831 LCONVPATHEXIST_AT(td, args->filename, &fname, dfd);
834 if (ldebug(futimesat))
835 printf(ARGS(futimesat, "%s, *"), fname);
838 if (args->utimes != NULL) {
839 if ((error = copyin(args->utimes, ltv, sizeof ltv))) {
843 tv[0].tv_sec = ltv[0].tv_sec;
844 tv[0].tv_usec = ltv[0].tv_usec;
845 tv[1].tv_sec = ltv[1].tv_sec;
846 tv[1].tv_usec = ltv[1].tv_usec;
850 error = kern_utimesat(td, dfd, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
854 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
857 linux_common_wait(struct thread *td, int pid, int *status,
858 int options, struct rusage *ru)
862 error = kern_wait(td, pid, &tmpstat, options, ru);
868 if (WIFSIGNALED(tmpstat))
869 tmpstat = (tmpstat & 0xffffff80) |
870 BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
871 else if (WIFSTOPPED(tmpstat))
872 tmpstat = (tmpstat & 0xffff00ff) |
873 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
874 error = copyout(&tmpstat, status, sizeof(int));
881 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
887 printf(ARGS(waitpid, "%d, %p, %d"),
888 args->pid, (void *)args->status, args->options);
891 * this is necessary because the test in kern_wait doesn't work
892 * because we mess with the options here
894 if (args->options & ~(WUNTRACED | WNOHANG | WCONTINUED | __WCLONE))
897 options = (args->options & (WNOHANG | WUNTRACED));
898 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
899 if (args->options & __WCLONE)
900 options |= WLINUXCLONE;
902 return (linux_common_wait(td, args->pid, args->status, options, NULL));
907 linux_mknod(struct thread *td, struct linux_mknod_args *args)
912 LCONVPATHCREAT(td, args->path, &path);
916 printf(ARGS(mknod, "%s, %d, %d"), path, args->mode, args->dev);
919 switch (args->mode & S_IFMT) {
922 error = kern_mkfifo(td, path, UIO_SYSSPACE, args->mode);
927 error = kern_mknod(td, path, UIO_SYSSPACE, args->mode,
936 args->mode |= S_IFREG;
939 error = kern_open(td, path, UIO_SYSSPACE,
940 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
942 kern_close(td, td->td_retval[0]);
954 linux_mknodat(struct thread *td, struct linux_mknodat_args *args)
959 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
960 LCONVPATHCREAT_AT(td, args->filename, &path, dfd);
964 printf(ARGS(mknodat, "%s, %d, %d"), path, args->mode, args->dev);
967 switch (args->mode & S_IFMT) {
970 error = kern_mkfifoat(td, dfd, path, UIO_SYSSPACE, args->mode);
975 error = kern_mknodat(td, dfd, path, UIO_SYSSPACE, args->mode,
984 args->mode |= S_IFREG;
987 error = kern_openat(td, dfd, path, UIO_SYSSPACE,
988 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
990 kern_close(td, td->td_retval[0]);
1002 * UGH! This is just about the dumbest idea I've ever heard!!
1005 linux_personality(struct thread *td, struct linux_personality_args *args)
1008 if (ldebug(personality))
1009 printf(ARGS(personality, "%lu"), (unsigned long)args->per);
1014 /* Yes Jim, it's still a Linux... */
1015 td->td_retval[0] = 0;
1019 struct l_itimerval {
1020 l_timeval it_interval;
1024 #define B2L_ITIMERVAL(bip, lip) \
1025 (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec; \
1026 (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec; \
1027 (bip)->it_value.tv_sec = (lip)->it_value.tv_sec; \
1028 (bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
1031 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
1034 struct l_itimerval ls;
1035 struct itimerval aitv, oitv;
1038 if (ldebug(setitimer))
1039 printf(ARGS(setitimer, "%p, %p"),
1040 (void *)uap->itv, (void *)uap->oitv);
1043 if (uap->itv == NULL) {
1044 uap->itv = uap->oitv;
1045 return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
1048 error = copyin(uap->itv, &ls, sizeof(ls));
1051 B2L_ITIMERVAL(&aitv, &ls);
1053 if (ldebug(setitimer)) {
1054 printf("setitimer: value: sec: %jd, usec: %ld\n",
1055 (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec);
1056 printf("setitimer: interval: sec: %jd, usec: %ld\n",
1057 (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec);
1060 error = kern_setitimer(td, uap->which, &aitv, &oitv);
1061 if (error != 0 || uap->oitv == NULL)
1063 B2L_ITIMERVAL(&ls, &oitv);
1065 return (copyout(&ls, uap->oitv, sizeof(ls)));
1069 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
1072 struct l_itimerval ls;
1073 struct itimerval aitv;
1076 if (ldebug(getitimer))
1077 printf(ARGS(getitimer, "%p"), (void *)uap->itv);
1079 error = kern_getitimer(td, uap->which, &aitv);
1082 B2L_ITIMERVAL(&ls, &aitv);
1083 return (copyout(&ls, uap->itv, sizeof(ls)));
1087 linux_nice(struct thread *td, struct linux_nice_args *args)
1089 struct setpriority_args bsd_args;
1091 bsd_args.which = PRIO_PROCESS;
1092 bsd_args.who = 0; /* current process */
1093 bsd_args.prio = args->inc;
1094 return (sys_setpriority(td, &bsd_args));
1098 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
1100 struct ucred *newcred, *oldcred;
1101 l_gid_t *linux_gidset;
1106 ngrp = args->gidsetsize;
1107 if (ngrp < 0 || ngrp >= ngroups_max + 1)
1109 linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_TEMP, M_WAITOK);
1110 error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
1114 crextend(newcred, ngrp + 1);
1117 oldcred = p->p_ucred;
1118 crcopy(newcred, oldcred);
1121 * cr_groups[0] holds egid. Setting the whole set from
1122 * the supplied set will cause egid to be changed too.
1123 * Keep cr_groups[0] unchanged to prevent that.
1126 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0) {
1133 newcred->cr_ngroups = ngrp + 1;
1135 bsd_gidset = newcred->cr_groups;
1138 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1142 newcred->cr_ngroups = 1;
1145 p->p_ucred = newcred;
1150 free(linux_gidset, M_TEMP);
1155 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1158 l_gid_t *linux_gidset;
1160 int bsd_gidsetsz, ngrp, error;
1162 cred = td->td_ucred;
1163 bsd_gidset = cred->cr_groups;
1164 bsd_gidsetsz = cred->cr_ngroups - 1;
1167 * cr_groups[0] holds egid. Returning the whole set
1168 * here will cause a duplicate. Exclude cr_groups[0]
1172 if ((ngrp = args->gidsetsize) == 0) {
1173 td->td_retval[0] = bsd_gidsetsz;
1177 if (ngrp < bsd_gidsetsz)
1181 linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset),
1183 while (ngrp < bsd_gidsetsz) {
1184 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1188 error = copyout(linux_gidset, args->grouplist, ngrp * sizeof(l_gid_t));
1189 free(linux_gidset, M_TEMP);
1193 td->td_retval[0] = ngrp;
1198 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1200 struct rlimit bsd_rlim;
1201 struct l_rlimit rlim;
1206 if (ldebug(setrlimit))
1207 printf(ARGS(setrlimit, "%d, %p"),
1208 args->resource, (void *)args->rlim);
1211 if (args->resource >= LINUX_RLIM_NLIMITS)
1214 which = linux_to_bsd_resource[args->resource];
1218 error = copyin(args->rlim, &rlim, sizeof(rlim));
1222 bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1223 bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1224 return (kern_setrlimit(td, which, &bsd_rlim));
1228 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1230 struct l_rlimit rlim;
1231 struct proc *p = td->td_proc;
1232 struct rlimit bsd_rlim;
1236 if (ldebug(old_getrlimit))
1237 printf(ARGS(old_getrlimit, "%d, %p"),
1238 args->resource, (void *)args->rlim);
1241 if (args->resource >= LINUX_RLIM_NLIMITS)
1244 which = linux_to_bsd_resource[args->resource];
1249 lim_rlimit(p, which, &bsd_rlim);
1252 #ifdef COMPAT_LINUX32
1253 rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1254 if (rlim.rlim_cur == UINT_MAX)
1255 rlim.rlim_cur = INT_MAX;
1256 rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1257 if (rlim.rlim_max == UINT_MAX)
1258 rlim.rlim_max = INT_MAX;
1260 rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1261 if (rlim.rlim_cur == ULONG_MAX)
1262 rlim.rlim_cur = LONG_MAX;
1263 rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1264 if (rlim.rlim_max == ULONG_MAX)
1265 rlim.rlim_max = LONG_MAX;
1267 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1271 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1273 struct l_rlimit rlim;
1274 struct proc *p = td->td_proc;
1275 struct rlimit bsd_rlim;
1279 if (ldebug(getrlimit))
1280 printf(ARGS(getrlimit, "%d, %p"),
1281 args->resource, (void *)args->rlim);
1284 if (args->resource >= LINUX_RLIM_NLIMITS)
1287 which = linux_to_bsd_resource[args->resource];
1292 lim_rlimit(p, which, &bsd_rlim);
1295 rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1296 rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1297 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1301 linux_sched_setscheduler(struct thread *td,
1302 struct linux_sched_setscheduler_args *args)
1304 struct sched_setscheduler_args bsd;
1307 if (ldebug(sched_setscheduler))
1308 printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1309 args->pid, args->policy, (const void *)args->param);
1312 switch (args->policy) {
1313 case LINUX_SCHED_OTHER:
1314 bsd.policy = SCHED_OTHER;
1316 case LINUX_SCHED_FIFO:
1317 bsd.policy = SCHED_FIFO;
1319 case LINUX_SCHED_RR:
1320 bsd.policy = SCHED_RR;
1326 bsd.pid = args->pid;
1327 bsd.param = (struct sched_param *)args->param;
1328 return (sys_sched_setscheduler(td, &bsd));
1332 linux_sched_getscheduler(struct thread *td,
1333 struct linux_sched_getscheduler_args *args)
1335 struct sched_getscheduler_args bsd;
1339 if (ldebug(sched_getscheduler))
1340 printf(ARGS(sched_getscheduler, "%d"), args->pid);
1343 bsd.pid = args->pid;
1344 error = sys_sched_getscheduler(td, &bsd);
1346 switch (td->td_retval[0]) {
1348 td->td_retval[0] = LINUX_SCHED_OTHER;
1351 td->td_retval[0] = LINUX_SCHED_FIFO;
1354 td->td_retval[0] = LINUX_SCHED_RR;
1362 linux_sched_get_priority_max(struct thread *td,
1363 struct linux_sched_get_priority_max_args *args)
1365 struct sched_get_priority_max_args bsd;
1368 if (ldebug(sched_get_priority_max))
1369 printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1372 switch (args->policy) {
1373 case LINUX_SCHED_OTHER:
1374 bsd.policy = SCHED_OTHER;
1376 case LINUX_SCHED_FIFO:
1377 bsd.policy = SCHED_FIFO;
1379 case LINUX_SCHED_RR:
1380 bsd.policy = SCHED_RR;
1385 return (sys_sched_get_priority_max(td, &bsd));
1389 linux_sched_get_priority_min(struct thread *td,
1390 struct linux_sched_get_priority_min_args *args)
1392 struct sched_get_priority_min_args bsd;
1395 if (ldebug(sched_get_priority_min))
1396 printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1399 switch (args->policy) {
1400 case LINUX_SCHED_OTHER:
1401 bsd.policy = SCHED_OTHER;
1403 case LINUX_SCHED_FIFO:
1404 bsd.policy = SCHED_FIFO;
1406 case LINUX_SCHED_RR:
1407 bsd.policy = SCHED_RR;
1412 return (sys_sched_get_priority_min(td, &bsd));
1415 #define REBOOT_CAD_ON 0x89abcdef
1416 #define REBOOT_CAD_OFF 0
1417 #define REBOOT_HALT 0xcdef0123
1418 #define REBOOT_RESTART 0x01234567
1419 #define REBOOT_RESTART2 0xA1B2C3D4
1420 #define REBOOT_POWEROFF 0x4321FEDC
1421 #define REBOOT_MAGIC1 0xfee1dead
1422 #define REBOOT_MAGIC2 0x28121969
1423 #define REBOOT_MAGIC2A 0x05121996
1424 #define REBOOT_MAGIC2B 0x16041998
1427 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1429 struct reboot_args bsd_args;
1433 printf(ARGS(reboot, "0x%x"), args->cmd);
1436 if (args->magic1 != REBOOT_MAGIC1)
1439 switch (args->magic2) {
1441 case REBOOT_MAGIC2A:
1442 case REBOOT_MAGIC2B:
1448 switch (args->cmd) {
1450 case REBOOT_CAD_OFF:
1451 return (priv_check(td, PRIV_REBOOT));
1453 bsd_args.opt = RB_HALT;
1455 case REBOOT_RESTART:
1456 case REBOOT_RESTART2:
1459 case REBOOT_POWEROFF:
1460 bsd_args.opt = RB_POWEROFF;
1465 return (sys_reboot(td, &bsd_args));
1470 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1471 * td->td_retval[1] when COMPAT_43 is defined. This clobbers registers that
1472 * are assumed to be preserved. The following lightweight syscalls fixes
1473 * this. See also linux_getgid16() and linux_getuid16() in linux_uid16.c
1475 * linux_getpid() - MP SAFE
1476 * linux_getgid() - MP SAFE
1477 * linux_getuid() - MP SAFE
1481 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1483 struct linux_emuldata *em;
1487 printf(ARGS(getpid, ""));
1490 if (linux_use26(td)) {
1491 em = em_find(td->td_proc, EMUL_DONTLOCK);
1492 KASSERT(em != NULL, ("getpid: emuldata not found.\n"));
1493 td->td_retval[0] = em->shared->group_pid;
1495 td->td_retval[0] = td->td_proc->p_pid;
1502 linux_gettid(struct thread *td, struct linux_gettid_args *args)
1507 printf(ARGS(gettid, ""));
1510 td->td_retval[0] = td->td_proc->p_pid;
1516 linux_getppid(struct thread *td, struct linux_getppid_args *args)
1518 struct linux_emuldata *em;
1519 struct proc *p, *pp;
1522 if (ldebug(getppid))
1523 printf(ARGS(getppid, ""));
1526 if (!linux_use26(td)) {
1527 PROC_LOCK(td->td_proc);
1528 td->td_retval[0] = td->td_proc->p_pptr->p_pid;
1529 PROC_UNLOCK(td->td_proc);
1533 em = em_find(td->td_proc, EMUL_DONTLOCK);
1535 KASSERT(em != NULL, ("getppid: process emuldata not found.\n"));
1537 /* find the group leader */
1538 p = pfind(em->shared->group_pid);
1542 printf(LMSG("parent process not found.\n"));
1547 pp = p->p_pptr; /* switch to parent */
1551 /* if its also linux process */
1552 if (pp->p_sysent == &elf_linux_sysvec) {
1553 em = em_find(pp, EMUL_DONTLOCK);
1554 KASSERT(em != NULL, ("getppid: parent emuldata not found.\n"));
1556 td->td_retval[0] = em->shared->group_pid;
1558 td->td_retval[0] = pp->p_pid;
1566 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1571 printf(ARGS(getgid, ""));
1574 td->td_retval[0] = td->td_ucred->cr_rgid;
1579 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1584 printf(ARGS(getuid, ""));
1587 td->td_retval[0] = td->td_ucred->cr_ruid;
1593 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1595 struct getsid_args bsd;
1599 printf(ARGS(getsid, "%i"), args->pid);
1602 bsd.pid = args->pid;
1603 return (sys_getsid(td, &bsd));
1607 linux_nosys(struct thread *td, struct nosys_args *ignore)
1614 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1616 struct getpriority_args bsd_args;
1620 if (ldebug(getpriority))
1621 printf(ARGS(getpriority, "%i, %i"), args->which, args->who);
1624 bsd_args.which = args->which;
1625 bsd_args.who = args->who;
1626 error = sys_getpriority(td, &bsd_args);
1627 td->td_retval[0] = 20 - td->td_retval[0];
1632 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1637 if (ldebug(sethostname))
1638 printf(ARGS(sethostname, "*, %i"), args->len);
1642 name[1] = KERN_HOSTNAME;
1643 return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1648 linux_setdomainname(struct thread *td, struct linux_setdomainname_args *args)
1653 if (ldebug(setdomainname))
1654 printf(ARGS(setdomainname, "*, %i"), args->len);
1658 name[1] = KERN_NISDOMAINNAME;
1659 return (userland_sysctl(td, name, 2, 0, 0, 0, args->name,
1664 linux_exit_group(struct thread *td, struct linux_exit_group_args *args)
1666 struct linux_emuldata *em;
1669 if (ldebug(exit_group))
1670 printf(ARGS(exit_group, "%i"), args->error_code);
1673 em = em_find(td->td_proc, EMUL_DONTLOCK);
1674 if (em->shared->refs > 1) {
1675 EMUL_SHARED_WLOCK(&emul_shared_lock);
1676 em->shared->flags |= EMUL_SHARED_HASXSTAT;
1677 em->shared->xstat = W_EXITCODE(args->error_code, 0);
1678 EMUL_SHARED_WUNLOCK(&emul_shared_lock);
1679 if (linux_use26(td))
1680 linux_kill_threads(td, SIGKILL);
1684 * XXX: we should send a signal to the parent if
1685 * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?)
1686 * as it doesnt occur often.
1688 exit1(td, W_EXITCODE(args->error_code, 0));
1693 #define _LINUX_CAPABILITY_VERSION 0x19980330
1695 struct l_user_cap_header {
1700 struct l_user_cap_data {
1707 linux_capget(struct thread *td, struct linux_capget_args *args)
1709 struct l_user_cap_header luch;
1710 struct l_user_cap_data lucd;
1713 if (args->hdrp == NULL)
1716 error = copyin(args->hdrp, &luch, sizeof(luch));
1720 if (luch.version != _LINUX_CAPABILITY_VERSION) {
1721 luch.version = _LINUX_CAPABILITY_VERSION;
1722 error = copyout(&luch, args->hdrp, sizeof(luch));
1733 * The current implementation doesn't support setting
1734 * a capability (it's essentially a stub) so indicate
1735 * that no capabilities are currently set or available
1738 bzero (&lucd, sizeof(lucd));
1739 error = copyout(&lucd, args->datap, sizeof(lucd));
1746 linux_capset(struct thread *td, struct linux_capset_args *args)
1748 struct l_user_cap_header luch;
1749 struct l_user_cap_data lucd;
1752 if (args->hdrp == NULL || args->datap == NULL)
1755 error = copyin(args->hdrp, &luch, sizeof(luch));
1759 if (luch.version != _LINUX_CAPABILITY_VERSION) {
1760 luch.version = _LINUX_CAPABILITY_VERSION;
1761 error = copyout(&luch, args->hdrp, sizeof(luch));
1770 error = copyin(args->datap, &lucd, sizeof(lucd));
1774 /* We currently don't support setting any capabilities. */
1775 if (lucd.effective || lucd.permitted || lucd.inheritable) {
1777 "capset effective=0x%x, permitted=0x%x, "
1778 "inheritable=0x%x is not implemented",
1779 (int)lucd.effective, (int)lucd.permitted,
1780 (int)lucd.inheritable);
1788 linux_prctl(struct thread *td, struct linux_prctl_args *args)
1790 int error = 0, max_size;
1791 struct proc *p = td->td_proc;
1792 char comm[LINUX_MAX_COMM_LEN];
1793 struct linux_emuldata *em;
1798 printf(ARGS(prctl, "%d, %d, %d, %d, %d"), args->option,
1799 args->arg2, args->arg3, args->arg4, args->arg5);
1802 switch (args->option) {
1803 case LINUX_PR_SET_PDEATHSIG:
1804 if (!LINUX_SIG_VALID(args->arg2))
1806 em = em_find(p, EMUL_DOLOCK);
1807 KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1808 em->pdeath_signal = args->arg2;
1809 EMUL_UNLOCK(&emul_lock);
1811 case LINUX_PR_GET_PDEATHSIG:
1812 em = em_find(p, EMUL_DOLOCK);
1813 KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1814 pdeath_signal = em->pdeath_signal;
1815 EMUL_UNLOCK(&emul_lock);
1816 error = copyout(&pdeath_signal,
1817 (void *)(register_t)args->arg2,
1818 sizeof(pdeath_signal));
1820 case LINUX_PR_GET_KEEPCAPS:
1822 * Indicate that we always clear the effective and
1823 * permitted capability sets when the user id becomes
1824 * non-zero (actually the capability sets are simply
1825 * always zero in the current implementation).
1827 td->td_retval[0] = 0;
1829 case LINUX_PR_SET_KEEPCAPS:
1831 * Ignore requests to keep the effective and permitted
1832 * capability sets when the user id becomes non-zero.
1835 case LINUX_PR_SET_NAME:
1837 * To be on the safe side we need to make sure to not
1838 * overflow the size a linux program expects. We already
1839 * do this here in the copyin, so that we don't need to
1842 max_size = MIN(sizeof(comm), sizeof(p->p_comm));
1843 error = copyinstr((void *)(register_t)args->arg2, comm,
1846 /* Linux silently truncates the name if it is too long. */
1847 if (error == ENAMETOOLONG) {
1849 * XXX: copyinstr() isn't documented to populate the
1850 * array completely, so do a copyin() to be on the
1851 * safe side. This should be changed in case
1852 * copyinstr() is changed to guarantee this.
1854 error = copyin((void *)(register_t)args->arg2, comm,
1856 comm[max_size - 1] = '\0';
1862 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
1865 case LINUX_PR_GET_NAME:
1867 strlcpy(comm, p->p_comm, sizeof(comm));
1869 error = copyout(comm, (void *)(register_t)args->arg2,
1881 * Get affinity of a process.
1884 linux_sched_getaffinity(struct thread *td,
1885 struct linux_sched_getaffinity_args *args)
1888 struct cpuset_getaffinity_args cga;
1891 if (ldebug(sched_getaffinity))
1892 printf(ARGS(sched_getaffinity, "%d, %d, *"), args->pid,
1895 if (args->len < sizeof(cpuset_t))
1898 cga.level = CPU_LEVEL_WHICH;
1899 cga.which = CPU_WHICH_PID;
1901 cga.cpusetsize = sizeof(cpuset_t);
1902 cga.mask = (cpuset_t *) args->user_mask_ptr;
1904 if ((error = sys_cpuset_getaffinity(td, &cga)) == 0)
1905 td->td_retval[0] = sizeof(cpuset_t);
1911 * Set affinity of a process.
1914 linux_sched_setaffinity(struct thread *td,
1915 struct linux_sched_setaffinity_args *args)
1917 struct cpuset_setaffinity_args csa;
1920 if (ldebug(sched_setaffinity))
1921 printf(ARGS(sched_setaffinity, "%d, %d, *"), args->pid,
1924 if (args->len < sizeof(cpuset_t))
1927 csa.level = CPU_LEVEL_WHICH;
1928 csa.which = CPU_WHICH_PID;
1930 csa.cpusetsize = sizeof(cpuset_t);
1931 csa.mask = (cpuset_t *) args->user_mask_ptr;
1933 return (sys_cpuset_setaffinity(td, &csa));