2 * Copyright (c) 2002 Doug Rabson
3 * Copyright (c) 1994-1995 Søren Schmidt
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer
11 * in this position and unchanged.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include "opt_compat.h"
35 #include <sys/param.h>
36 #include <sys/blist.h>
37 #include <sys/fcntl.h>
39 #include <sys/imgact_aout.h>
42 #include <sys/kernel.h>
43 #include <sys/limits.h>
45 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/mutex.h>
49 #include <sys/namei.h>
52 #include <sys/reboot.h>
53 #include <sys/resourcevar.h>
54 #include <sys/sched.h>
55 #include <sys/signalvar.h>
57 #include <sys/syscallsubr.h>
58 #include <sys/sysctl.h>
59 #include <sys/sysproto.h>
60 #include <sys/systm.h>
62 #include <sys/vmmeter.h>
63 #include <sys/vnode.h>
65 #include <sys/cpuset.h>
67 #include <security/mac/mac_framework.h>
71 #include <vm/vm_kern.h>
72 #include <vm/vm_map.h>
73 #include <vm/vm_extern.h>
74 #include <vm/vm_object.h>
75 #include <vm/swap_pager.h>
78 #include <machine/../linux32/linux.h>
79 #include <machine/../linux32/linux32_proto.h>
81 #include <machine/../linux/linux.h>
82 #include <machine/../linux/linux_proto.h>
85 #include <compat/linux/linux_file.h>
86 #include <compat/linux/linux_mib.h>
87 #include <compat/linux/linux_signal.h>
88 #include <compat/linux/linux_util.h>
89 #include <compat/linux/linux_sysproto.h>
90 #include <compat/linux/linux_emul.h>
91 #include <compat/linux/linux_misc.h>
93 int stclohz; /* Statistics clock frequency */
95 #define BSD_TO_LINUX_SIGNAL(sig) \
96 (((sig) <= LINUX_SIGTBLSZ) ? bsd_to_linux_signal[_SIG_IDX(sig)] : sig)
98 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
99 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
100 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
101 RLIMIT_MEMLOCK, RLIMIT_AS
105 l_long uptime; /* Seconds since boot */
106 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */
107 #define LINUX_SYSINFO_LOADS_SCALE 65536
108 l_ulong totalram; /* Total usable main memory size */
109 l_ulong freeram; /* Available memory size */
110 l_ulong sharedram; /* Amount of shared memory */
111 l_ulong bufferram; /* Memory used by buffers */
112 l_ulong totalswap; /* Total swap space size */
113 l_ulong freeswap; /* swap space still available */
114 l_ushort procs; /* Number of current processes */
119 char _f[20-2*sizeof(l_long)-sizeof(l_int)]; /* padding */
122 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args)
124 struct l_sysinfo sysinfo;
132 sysinfo.uptime = ts.tv_sec;
134 /* Use the information from the mib to get our load averages */
135 for (i = 0; i < 3; i++)
136 sysinfo.loads[i] = averunnable.ldavg[i] *
137 LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale;
139 sysinfo.totalram = physmem * PAGE_SIZE;
140 sysinfo.freeram = sysinfo.totalram - cnt.v_wire_count * PAGE_SIZE;
142 sysinfo.sharedram = 0;
143 mtx_lock(&vm_object_list_mtx);
144 TAILQ_FOREACH(object, &vm_object_list, object_list)
145 if (object->shadow_count > 1)
146 sysinfo.sharedram += object->resident_page_count;
147 mtx_unlock(&vm_object_list_mtx);
149 sysinfo.sharedram *= PAGE_SIZE;
150 sysinfo.bufferram = 0;
152 swap_pager_status(&i, &j);
153 sysinfo.totalswap = i * PAGE_SIZE;
154 sysinfo.freeswap = (i - j) * PAGE_SIZE;
156 sysinfo.procs = nprocs;
158 /* The following are only present in newer Linux kernels. */
159 sysinfo.totalbig = 0;
161 sysinfo.mem_unit = 1;
163 return copyout(&sysinfo, args->info, sizeof(sysinfo));
167 linux_alarm(struct thread *td, struct linux_alarm_args *args)
169 struct itimerval it, old_it;
175 printf(ARGS(alarm, "%u"), args->secs);
183 it.it_value.tv_sec = (long) secs;
184 it.it_value.tv_usec = 0;
185 it.it_interval.tv_sec = 0;
186 it.it_interval.tv_usec = 0;
187 error = kern_setitimer(td, ITIMER_REAL, &it, &old_it);
190 if (timevalisset(&old_it.it_value)) {
191 if (old_it.it_value.tv_usec != 0)
192 old_it.it_value.tv_sec++;
193 td->td_retval[0] = old_it.it_value.tv_sec;
199 linux_brk(struct thread *td, struct linux_brk_args *args)
201 struct vmspace *vm = td->td_proc->p_vmspace;
202 vm_offset_t new, old;
203 struct obreak_args /* {
209 printf(ARGS(brk, "%p"), (void *)(uintptr_t)args->dsend);
211 old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
212 new = (vm_offset_t)args->dsend;
213 tmp.nsize = (char *)new;
214 if (((caddr_t)new > vm->vm_daddr) && !obreak(td, &tmp))
215 td->td_retval[0] = (long)new;
217 td->td_retval[0] = (long)old;
222 #if defined(__i386__)
223 /* XXX: what about amd64/linux32? */
226 linux_uselib(struct thread *td, struct linux_uselib_args *args)
233 unsigned long file_offset;
235 unsigned long bss_size;
238 int locked, vfslocked;
240 LCONVPATHEXIST(td, args->library, &library);
244 printf(ARGS(uselib, "%s"), library);
252 NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
253 UIO_SYSSPACE, library, td);
260 vfslocked = NDHASGIANT(&ni);
261 NDFREE(&ni, NDF_ONLY_PNBUF);
264 * From here on down, we have a locked vnode that must be unlocked.
265 * XXX: The code below largely duplicates exec_check_permissions().
270 if (vp->v_writecount) {
276 error = VOP_GETATTR(vp, &attr, td->td_ucred);
280 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
281 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
282 /* EACCESS is what exec(2) returns. */
288 if (attr.va_size == 0) {
293 /* Can we access it? */
294 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
299 * XXX: This should use vn_open() so that it is properly authorized,
300 * and to reduce code redundancy all over the place here.
301 * XXX: Not really, it duplicates far more of exec_check_permissions()
305 error = mac_vnode_check_open(td->td_ucred, vp, VREAD);
309 error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL);
313 /* Pull in executable header into kernel_map */
314 error = vm_mmap(kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE,
315 VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 0);
319 /* Is it a Linux binary ? */
320 if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
326 * While we are here, we should REALLY do some more checks
329 /* Set file/virtual offset based on a.out variant. */
330 switch ((int)(a_out->a_magic & 0xffff)) {
331 case 0413: /* ZMAGIC */
334 case 0314: /* QMAGIC */
342 bss_size = round_page(a_out->a_bss);
344 /* Check various fields in header for validity/bounds. */
345 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
350 /* text + data can't exceed file size */
351 if (a_out->a_data + a_out->a_text > attr.va_size) {
357 * text/data/bss must not exceed limits
358 * XXX - this is not complete. it should check current usage PLUS
359 * the resources needed by this library.
361 PROC_LOCK(td->td_proc);
362 if (a_out->a_text > maxtsiz ||
363 a_out->a_data + bss_size > lim_cur(td->td_proc, RLIMIT_DATA)) {
364 PROC_UNLOCK(td->td_proc);
368 PROC_UNLOCK(td->td_proc);
371 * Prevent more writers.
372 * XXX: Note that if any of the VM operations fail below we don't
375 vp->v_vflag |= VV_TEXT;
378 * Lock no longer needed
382 VFS_UNLOCK_GIANT(vfslocked);
385 * Check if file_offset page aligned. Currently we cannot handle
386 * misalinged file offsets, and so we read in the entire image
389 if (file_offset & PAGE_MASK) {
391 printf("uselib: Non page aligned binary %lu\n", file_offset);
393 /* Map text+data read/write/execute */
395 /* a_entry is the load address and is page aligned */
396 vmaddr = trunc_page(a_out->a_entry);
398 /* get anon user mapping, read+write+execute */
399 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
400 &vmaddr, a_out->a_text + a_out->a_data, FALSE, VM_PROT_ALL,
405 /* map file into kernel_map */
406 error = vm_mmap(kernel_map, &buffer,
407 round_page(a_out->a_text + a_out->a_data + file_offset),
408 VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp,
409 trunc_page(file_offset));
413 /* copy from kernel VM space to user space */
414 error = copyout(PTRIN(buffer + file_offset),
415 (void *)vmaddr, a_out->a_text + a_out->a_data);
417 /* release temporary kernel space */
418 vm_map_remove(kernel_map, buffer, buffer +
419 round_page(a_out->a_text + a_out->a_data + file_offset));
425 printf("uselib: Page aligned binary %lu\n", file_offset);
428 * for QMAGIC, a_entry is 20 bytes beyond the load address
429 * to skip the executable header
431 vmaddr = trunc_page(a_out->a_entry);
434 * Map it all into the process's space as a single
435 * copy-on-write "data" segment.
437 error = vm_mmap(&td->td_proc->p_vmspace->vm_map, &vmaddr,
438 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
439 MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset);
444 printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long *)vmaddr)[0],
445 ((long *)vmaddr)[1]);
448 /* Calculate BSS start address */
449 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
452 /* allocate some 'anon' space */
453 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
454 &vmaddr, bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
460 /* Unlock vnode if needed */
463 VFS_UNLOCK_GIANT(vfslocked);
466 /* Release the kernel mapping. */
468 vm_map_remove(kernel_map, (vm_offset_t)a_out,
469 (vm_offset_t)a_out + PAGE_SIZE);
474 #endif /* __i386__ */
477 linux_select(struct thread *td, struct linux_select_args *args)
480 struct timeval tv0, tv1, utv, *tvp;
485 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
486 (void *)args->readfds, (void *)args->writefds,
487 (void *)args->exceptfds, (void *)args->timeout);
491 * Store current time for computation of the amount of
495 if ((error = copyin(args->timeout, <v, sizeof(ltv))))
497 utv.tv_sec = ltv.tv_sec;
498 utv.tv_usec = ltv.tv_usec;
501 printf(LMSG("incoming timeout (%jd/%ld)"),
502 (intmax_t)utv.tv_sec, utv.tv_usec);
505 if (itimerfix(&utv)) {
507 * The timeval was invalid. Convert it to something
508 * valid that will act as it does under Linux.
510 utv.tv_sec += utv.tv_usec / 1000000;
511 utv.tv_usec %= 1000000;
512 if (utv.tv_usec < 0) {
514 utv.tv_usec += 1000000;
524 error = kern_select(td, args->nfds, args->readfds, args->writefds,
525 args->exceptfds, tvp, sizeof(l_int) * 8);
529 printf(LMSG("real select returns %d"), error);
535 if (td->td_retval[0]) {
537 * Compute how much time was left of the timeout,
538 * by subtracting the current time and the time
539 * before we started the call, and subtracting
540 * that result from the user-supplied value.
543 timevalsub(&tv1, &tv0);
544 timevalsub(&utv, &tv1);
551 printf(LMSG("outgoing timeout (%jd/%ld)"),
552 (intmax_t)utv.tv_sec, utv.tv_usec);
554 ltv.tv_sec = utv.tv_sec;
555 ltv.tv_usec = utv.tv_usec;
556 if ((error = copyout(<v, args->timeout, sizeof(ltv))))
563 printf(LMSG("select_out -> %d"), error);
569 linux_mremap(struct thread *td, struct linux_mremap_args *args)
571 struct munmap_args /* {
579 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
580 (void *)(uintptr_t)args->addr,
581 (unsigned long)args->old_len,
582 (unsigned long)args->new_len,
583 (unsigned long)args->flags);
586 if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) {
587 td->td_retval[0] = 0;
592 * Check for the page alignment.
593 * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK.
595 if (args->addr & PAGE_MASK) {
596 td->td_retval[0] = 0;
600 args->new_len = round_page(args->new_len);
601 args->old_len = round_page(args->old_len);
603 if (args->new_len > args->old_len) {
604 td->td_retval[0] = 0;
608 if (args->new_len < args->old_len) {
610 (caddr_t)((uintptr_t)args->addr + args->new_len);
611 bsd_args.len = args->old_len - args->new_len;
612 error = munmap(td, &bsd_args);
615 td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
619 #define LINUX_MS_ASYNC 0x0001
620 #define LINUX_MS_INVALIDATE 0x0002
621 #define LINUX_MS_SYNC 0x0004
624 linux_msync(struct thread *td, struct linux_msync_args *args)
626 struct msync_args bsd_args;
628 bsd_args.addr = (caddr_t)(uintptr_t)args->addr;
629 bsd_args.len = (uintptr_t)args->len;
630 bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
632 return msync(td, &bsd_args);
636 linux_time(struct thread *td, struct linux_time_args *args)
644 printf(ARGS(time, "*"));
649 if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
651 td->td_retval[0] = tm;
655 struct l_times_argv {
658 l_clock_t tms_cutime;
659 l_clock_t tms_cstime;
664 * Glibc versions prior to 2.2.1 always use hard-coded CLK_TCK value.
665 * Since 2.2.1 Glibc uses value exported from kernel via AT_CLKTCK
666 * auxiliary vector entry.
670 #define CONVOTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
671 #define CONVNTCK(r) (r.tv_sec * stclohz + r.tv_usec / (1000000 / stclohz))
673 #define CONVTCK(r) (linux_kernver(td) >= LINUX_KERNVER_2004000 ? \
674 CONVNTCK(r) : CONVOTCK(r))
677 linux_times(struct thread *td, struct linux_times_args *args)
679 struct timeval tv, utime, stime, cutime, cstime;
680 struct l_times_argv tms;
686 printf(ARGS(times, "*"));
689 if (args->buf != NULL) {
693 calcru(p, &utime, &stime);
695 calccru(p, &cutime, &cstime);
698 tms.tms_utime = CONVTCK(utime);
699 tms.tms_stime = CONVTCK(stime);
701 tms.tms_cutime = CONVTCK(cutime);
702 tms.tms_cstime = CONVTCK(cstime);
704 if ((error = copyout(&tms, args->buf, sizeof(tms))))
709 td->td_retval[0] = (int)CONVTCK(tv);
714 linux_newuname(struct thread *td, struct linux_newuname_args *args)
716 struct l_new_utsname utsname;
717 char osname[LINUX_MAX_UTSNAME];
718 char osrelease[LINUX_MAX_UTSNAME];
722 if (ldebug(newuname))
723 printf(ARGS(newuname, "*"));
726 linux_get_osname(td, osname);
727 linux_get_osrelease(td, osrelease);
729 bzero(&utsname, sizeof(utsname));
730 strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
731 getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
732 getcreddomainname(td->td_ucred, utsname.domainname, LINUX_MAX_UTSNAME);
733 strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
734 strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
735 for (p = utsname.version; *p != '\0'; ++p)
740 strlcpy(utsname.machine, linux_platform, LINUX_MAX_UTSNAME);
742 return (copyout(&utsname, args->buf, sizeof(utsname)));
745 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
752 linux_utime(struct thread *td, struct linux_utime_args *args)
754 struct timeval tv[2], *tvp;
755 struct l_utimbuf lut;
759 LCONVPATHEXIST(td, args->fname, &fname);
763 printf(ARGS(utime, "%s, *"), fname);
767 if ((error = copyin(args->times, &lut, sizeof lut))) {
771 tv[0].tv_sec = lut.l_actime;
773 tv[1].tv_sec = lut.l_modtime;
779 error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
785 linux_utimes(struct thread *td, struct linux_utimes_args *args)
788 struct timeval tv[2], *tvp = NULL;
792 LCONVPATHEXIST(td, args->fname, &fname);
796 printf(ARGS(utimes, "%s, *"), fname);
799 if (args->tptr != NULL) {
800 if ((error = copyin(args->tptr, ltv, sizeof ltv))) {
804 tv[0].tv_sec = ltv[0].tv_sec;
805 tv[0].tv_usec = ltv[0].tv_usec;
806 tv[1].tv_sec = ltv[1].tv_sec;
807 tv[1].tv_usec = ltv[1].tv_usec;
811 error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
817 linux_futimesat(struct thread *td, struct linux_futimesat_args *args)
820 struct timeval tv[2], *tvp = NULL;
824 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
825 LCONVPATHEXIST_AT(td, args->filename, &fname, dfd);
828 if (ldebug(futimesat))
829 printf(ARGS(futimesat, "%s, *"), fname);
832 if (args->utimes != NULL) {
833 if ((error = copyin(args->utimes, ltv, sizeof ltv))) {
837 tv[0].tv_sec = ltv[0].tv_sec;
838 tv[0].tv_usec = ltv[0].tv_usec;
839 tv[1].tv_sec = ltv[1].tv_sec;
840 tv[1].tv_usec = ltv[1].tv_usec;
844 error = kern_utimesat(td, dfd, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
848 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
850 #define __WCLONE 0x80000000
853 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
855 int error, options, tmpstat;
859 printf(ARGS(waitpid, "%d, %p, %d"),
860 args->pid, (void *)args->status, args->options);
863 * this is necessary because the test in kern_wait doesn't work
864 * because we mess with the options here
866 if (args->options & ~(WUNTRACED | WNOHANG | WCONTINUED | __WCLONE))
869 options = (args->options & (WNOHANG | WUNTRACED));
870 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
871 if (args->options & __WCLONE)
872 options |= WLINUXCLONE;
874 error = kern_wait(td, args->pid, &tmpstat, options, NULL);
880 if (WIFSIGNALED(tmpstat))
881 tmpstat = (tmpstat & 0xffffff80) |
882 BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
883 else if (WIFSTOPPED(tmpstat))
884 tmpstat = (tmpstat & 0xffff00ff) |
885 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
886 return copyout(&tmpstat, args->status, sizeof(int));
893 linux_wait4(struct thread *td, struct linux_wait4_args *args)
895 int error, options, tmpstat;
896 struct rusage ru, *rup;
901 printf(ARGS(wait4, "%d, %p, %d, %p"),
902 args->pid, (void *)args->status, args->options,
903 (void *)args->rusage);
906 options = (args->options & (WNOHANG | WUNTRACED));
907 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
908 if (args->options & __WCLONE)
909 options |= WLINUXCLONE;
911 if (args->rusage != NULL)
915 error = kern_wait(td, args->pid, &tmpstat, options, rup);
921 sigqueue_delete(&p->p_sigqueue, SIGCHLD);
926 if (WIFSIGNALED(tmpstat))
927 tmpstat = (tmpstat & 0xffffff80) |
928 BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
929 else if (WIFSTOPPED(tmpstat))
930 tmpstat = (tmpstat & 0xffff00ff) |
931 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
932 error = copyout(&tmpstat, args->status, sizeof(int));
934 if (args->rusage != NULL && error == 0)
935 error = copyout(&ru, args->rusage, sizeof(ru));
941 linux_mknod(struct thread *td, struct linux_mknod_args *args)
946 LCONVPATHCREAT(td, args->path, &path);
950 printf(ARGS(mknod, "%s, %d, %d"), path, args->mode, args->dev);
953 switch (args->mode & S_IFMT) {
956 error = kern_mkfifo(td, path, UIO_SYSSPACE, args->mode);
961 error = kern_mknod(td, path, UIO_SYSSPACE, args->mode,
970 args->mode |= S_IFREG;
973 error = kern_open(td, path, UIO_SYSSPACE,
974 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
976 kern_close(td, td->td_retval[0]);
988 linux_mknodat(struct thread *td, struct linux_mknodat_args *args)
993 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
994 LCONVPATHCREAT_AT(td, args->filename, &path, dfd);
998 printf(ARGS(mknodat, "%s, %d, %d"), path, args->mode, args->dev);
1001 switch (args->mode & S_IFMT) {
1004 error = kern_mkfifoat(td, dfd, path, UIO_SYSSPACE, args->mode);
1009 error = kern_mknodat(td, dfd, path, UIO_SYSSPACE, args->mode,
1018 args->mode |= S_IFREG;
1021 error = kern_openat(td, dfd, path, UIO_SYSSPACE,
1022 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1024 kern_close(td, td->td_retval[0]);
1036 * UGH! This is just about the dumbest idea I've ever heard!!
1039 linux_personality(struct thread *td, struct linux_personality_args *args)
1042 if (ldebug(personality))
1043 printf(ARGS(personality, "%lu"), (unsigned long)args->per);
1048 /* Yes Jim, it's still a Linux... */
1049 td->td_retval[0] = 0;
1053 struct l_itimerval {
1054 l_timeval it_interval;
1058 #define B2L_ITIMERVAL(bip, lip) \
1059 (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec; \
1060 (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec; \
1061 (bip)->it_value.tv_sec = (lip)->it_value.tv_sec; \
1062 (bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
1065 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
1068 struct l_itimerval ls;
1069 struct itimerval aitv, oitv;
1072 if (ldebug(setitimer))
1073 printf(ARGS(setitimer, "%p, %p"),
1074 (void *)uap->itv, (void *)uap->oitv);
1077 if (uap->itv == NULL) {
1078 uap->itv = uap->oitv;
1079 return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
1082 error = copyin(uap->itv, &ls, sizeof(ls));
1085 B2L_ITIMERVAL(&aitv, &ls);
1087 if (ldebug(setitimer)) {
1088 printf("setitimer: value: sec: %jd, usec: %ld\n",
1089 (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec);
1090 printf("setitimer: interval: sec: %jd, usec: %ld\n",
1091 (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec);
1094 error = kern_setitimer(td, uap->which, &aitv, &oitv);
1095 if (error != 0 || uap->oitv == NULL)
1097 B2L_ITIMERVAL(&ls, &oitv);
1099 return (copyout(&ls, uap->oitv, sizeof(ls)));
1103 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
1106 struct l_itimerval ls;
1107 struct itimerval aitv;
1110 if (ldebug(getitimer))
1111 printf(ARGS(getitimer, "%p"), (void *)uap->itv);
1113 error = kern_getitimer(td, uap->which, &aitv);
1116 B2L_ITIMERVAL(&ls, &aitv);
1117 return (copyout(&ls, uap->itv, sizeof(ls)));
1121 linux_nice(struct thread *td, struct linux_nice_args *args)
1123 struct setpriority_args bsd_args;
1125 bsd_args.which = PRIO_PROCESS;
1126 bsd_args.who = 0; /* current process */
1127 bsd_args.prio = args->inc;
1128 return setpriority(td, &bsd_args);
1132 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
1134 struct ucred *newcred, *oldcred;
1135 l_gid_t *linux_gidset;
1140 ngrp = args->gidsetsize;
1141 if (ngrp < 0 || ngrp >= ngroups_max + 1)
1143 linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_TEMP, M_WAITOK);
1144 error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
1150 oldcred = crcopysafe(p, newcred);
1153 * cr_groups[0] holds egid. Setting the whole set from
1154 * the supplied set will cause egid to be changed too.
1155 * Keep cr_groups[0] unchanged to prevent that.
1158 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0) {
1165 newcred->cr_ngroups = ngrp + 1;
1167 bsd_gidset = newcred->cr_groups;
1170 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1174 newcred->cr_ngroups = 1;
1177 p->p_ucred = newcred;
1182 free(linux_gidset, M_TEMP);
1187 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1190 l_gid_t *linux_gidset;
1192 int bsd_gidsetsz, ngrp, error;
1194 cred = td->td_ucred;
1195 bsd_gidset = cred->cr_groups;
1196 bsd_gidsetsz = cred->cr_ngroups - 1;
1199 * cr_groups[0] holds egid. Returning the whole set
1200 * here will cause a duplicate. Exclude cr_groups[0]
1204 if ((ngrp = args->gidsetsize) == 0) {
1205 td->td_retval[0] = bsd_gidsetsz;
1209 if (ngrp < bsd_gidsetsz)
1213 linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset),
1215 while (ngrp < bsd_gidsetsz) {
1216 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1220 error = copyout(linux_gidset, args->grouplist, ngrp * sizeof(l_gid_t));
1221 free(linux_gidset, M_TEMP);
1225 td->td_retval[0] = ngrp;
1230 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1232 struct rlimit bsd_rlim;
1233 struct l_rlimit rlim;
1238 if (ldebug(setrlimit))
1239 printf(ARGS(setrlimit, "%d, %p"),
1240 args->resource, (void *)args->rlim);
1243 if (args->resource >= LINUX_RLIM_NLIMITS)
1246 which = linux_to_bsd_resource[args->resource];
1250 error = copyin(args->rlim, &rlim, sizeof(rlim));
1254 bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1255 bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1256 return (kern_setrlimit(td, which, &bsd_rlim));
1260 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1262 struct l_rlimit rlim;
1263 struct proc *p = td->td_proc;
1264 struct rlimit bsd_rlim;
1268 if (ldebug(old_getrlimit))
1269 printf(ARGS(old_getrlimit, "%d, %p"),
1270 args->resource, (void *)args->rlim);
1273 if (args->resource >= LINUX_RLIM_NLIMITS)
1276 which = linux_to_bsd_resource[args->resource];
1281 lim_rlimit(p, which, &bsd_rlim);
1284 #ifdef COMPAT_LINUX32
1285 rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1286 if (rlim.rlim_cur == UINT_MAX)
1287 rlim.rlim_cur = INT_MAX;
1288 rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1289 if (rlim.rlim_max == UINT_MAX)
1290 rlim.rlim_max = INT_MAX;
1292 rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1293 if (rlim.rlim_cur == ULONG_MAX)
1294 rlim.rlim_cur = LONG_MAX;
1295 rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1296 if (rlim.rlim_max == ULONG_MAX)
1297 rlim.rlim_max = LONG_MAX;
1299 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1303 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1305 struct l_rlimit rlim;
1306 struct proc *p = td->td_proc;
1307 struct rlimit bsd_rlim;
1311 if (ldebug(getrlimit))
1312 printf(ARGS(getrlimit, "%d, %p"),
1313 args->resource, (void *)args->rlim);
1316 if (args->resource >= LINUX_RLIM_NLIMITS)
1319 which = linux_to_bsd_resource[args->resource];
1324 lim_rlimit(p, which, &bsd_rlim);
1327 rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1328 rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1329 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1333 linux_sched_setscheduler(struct thread *td,
1334 struct linux_sched_setscheduler_args *args)
1336 struct sched_setscheduler_args bsd;
1339 if (ldebug(sched_setscheduler))
1340 printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1341 args->pid, args->policy, (const void *)args->param);
1344 switch (args->policy) {
1345 case LINUX_SCHED_OTHER:
1346 bsd.policy = SCHED_OTHER;
1348 case LINUX_SCHED_FIFO:
1349 bsd.policy = SCHED_FIFO;
1351 case LINUX_SCHED_RR:
1352 bsd.policy = SCHED_RR;
1358 bsd.pid = args->pid;
1359 bsd.param = (struct sched_param *)args->param;
1360 return sched_setscheduler(td, &bsd);
1364 linux_sched_getscheduler(struct thread *td,
1365 struct linux_sched_getscheduler_args *args)
1367 struct sched_getscheduler_args bsd;
1371 if (ldebug(sched_getscheduler))
1372 printf(ARGS(sched_getscheduler, "%d"), args->pid);
1375 bsd.pid = args->pid;
1376 error = sched_getscheduler(td, &bsd);
1378 switch (td->td_retval[0]) {
1380 td->td_retval[0] = LINUX_SCHED_OTHER;
1383 td->td_retval[0] = LINUX_SCHED_FIFO;
1386 td->td_retval[0] = LINUX_SCHED_RR;
1394 linux_sched_get_priority_max(struct thread *td,
1395 struct linux_sched_get_priority_max_args *args)
1397 struct sched_get_priority_max_args bsd;
1400 if (ldebug(sched_get_priority_max))
1401 printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1404 switch (args->policy) {
1405 case LINUX_SCHED_OTHER:
1406 bsd.policy = SCHED_OTHER;
1408 case LINUX_SCHED_FIFO:
1409 bsd.policy = SCHED_FIFO;
1411 case LINUX_SCHED_RR:
1412 bsd.policy = SCHED_RR;
1417 return sched_get_priority_max(td, &bsd);
1421 linux_sched_get_priority_min(struct thread *td,
1422 struct linux_sched_get_priority_min_args *args)
1424 struct sched_get_priority_min_args bsd;
1427 if (ldebug(sched_get_priority_min))
1428 printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1431 switch (args->policy) {
1432 case LINUX_SCHED_OTHER:
1433 bsd.policy = SCHED_OTHER;
1435 case LINUX_SCHED_FIFO:
1436 bsd.policy = SCHED_FIFO;
1438 case LINUX_SCHED_RR:
1439 bsd.policy = SCHED_RR;
1444 return sched_get_priority_min(td, &bsd);
1447 #define REBOOT_CAD_ON 0x89abcdef
1448 #define REBOOT_CAD_OFF 0
1449 #define REBOOT_HALT 0xcdef0123
1450 #define REBOOT_RESTART 0x01234567
1451 #define REBOOT_RESTART2 0xA1B2C3D4
1452 #define REBOOT_POWEROFF 0x4321FEDC
1453 #define REBOOT_MAGIC1 0xfee1dead
1454 #define REBOOT_MAGIC2 0x28121969
1455 #define REBOOT_MAGIC2A 0x05121996
1456 #define REBOOT_MAGIC2B 0x16041998
1459 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1461 struct reboot_args bsd_args;
1465 printf(ARGS(reboot, "0x%x"), args->cmd);
1468 if (args->magic1 != REBOOT_MAGIC1)
1471 switch (args->magic2) {
1473 case REBOOT_MAGIC2A:
1474 case REBOOT_MAGIC2B:
1480 switch (args->cmd) {
1482 case REBOOT_CAD_OFF:
1483 return (priv_check(td, PRIV_REBOOT));
1485 bsd_args.opt = RB_HALT;
1487 case REBOOT_RESTART:
1488 case REBOOT_RESTART2:
1491 case REBOOT_POWEROFF:
1492 bsd_args.opt = RB_POWEROFF;
1497 return reboot(td, &bsd_args);
1502 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1503 * td->td_retval[1] when COMPAT_43 is defined. This clobbers registers that
1504 * are assumed to be preserved. The following lightweight syscalls fixes
1505 * this. See also linux_getgid16() and linux_getuid16() in linux_uid16.c
1507 * linux_getpid() - MP SAFE
1508 * linux_getgid() - MP SAFE
1509 * linux_getuid() - MP SAFE
1513 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1515 struct linux_emuldata *em;
1519 printf(ARGS(getpid, ""));
1522 if (linux_use26(td)) {
1523 em = em_find(td->td_proc, EMUL_DONTLOCK);
1524 KASSERT(em != NULL, ("getpid: emuldata not found.\n"));
1525 td->td_retval[0] = em->shared->group_pid;
1527 td->td_retval[0] = td->td_proc->p_pid;
1534 linux_gettid(struct thread *td, struct linux_gettid_args *args)
1539 printf(ARGS(gettid, ""));
1542 td->td_retval[0] = td->td_proc->p_pid;
1548 linux_getppid(struct thread *td, struct linux_getppid_args *args)
1550 struct linux_emuldata *em;
1551 struct proc *p, *pp;
1554 if (ldebug(getppid))
1555 printf(ARGS(getppid, ""));
1558 if (!linux_use26(td)) {
1559 PROC_LOCK(td->td_proc);
1560 td->td_retval[0] = td->td_proc->p_pptr->p_pid;
1561 PROC_UNLOCK(td->td_proc);
1565 em = em_find(td->td_proc, EMUL_DONTLOCK);
1567 KASSERT(em != NULL, ("getppid: process emuldata not found.\n"));
1569 /* find the group leader */
1570 p = pfind(em->shared->group_pid);
1574 printf(LMSG("parent process not found.\n"));
1579 pp = p->p_pptr; /* switch to parent */
1583 /* if its also linux process */
1584 if (pp->p_sysent == &elf_linux_sysvec) {
1585 em = em_find(pp, EMUL_DONTLOCK);
1586 KASSERT(em != NULL, ("getppid: parent emuldata not found.\n"));
1588 td->td_retval[0] = em->shared->group_pid;
1590 td->td_retval[0] = pp->p_pid;
1598 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1603 printf(ARGS(getgid, ""));
1606 td->td_retval[0] = td->td_ucred->cr_rgid;
1611 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1616 printf(ARGS(getuid, ""));
1619 td->td_retval[0] = td->td_ucred->cr_ruid;
1625 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1627 struct getsid_args bsd;
1631 printf(ARGS(getsid, "%i"), args->pid);
1634 bsd.pid = args->pid;
1635 return getsid(td, &bsd);
1639 linux_nosys(struct thread *td, struct nosys_args *ignore)
1646 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1648 struct getpriority_args bsd_args;
1652 if (ldebug(getpriority))
1653 printf(ARGS(getpriority, "%i, %i"), args->which, args->who);
1656 bsd_args.which = args->which;
1657 bsd_args.who = args->who;
1658 error = getpriority(td, &bsd_args);
1659 td->td_retval[0] = 20 - td->td_retval[0];
1664 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1669 if (ldebug(sethostname))
1670 printf(ARGS(sethostname, "*, %i"), args->len);
1674 name[1] = KERN_HOSTNAME;
1675 return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1680 linux_setdomainname(struct thread *td, struct linux_setdomainname_args *args)
1685 if (ldebug(setdomainname))
1686 printf(ARGS(setdomainname, "*, %i"), args->len);
1690 name[1] = KERN_NISDOMAINNAME;
1691 return (userland_sysctl(td, name, 2, 0, 0, 0, args->name,
1696 linux_exit_group(struct thread *td, struct linux_exit_group_args *args)
1698 struct linux_emuldata *em, *td_em, *tmp_em;
1702 if (ldebug(exit_group))
1703 printf(ARGS(exit_group, "%i"), args->error_code);
1706 if (linux_use26(td)) {
1707 td_em = em_find(td->td_proc, EMUL_DONTLOCK);
1709 KASSERT(td_em != NULL, ("exit_group: emuldata not found.\n"));
1711 EMUL_SHARED_RLOCK(&emul_shared_lock);
1712 LIST_FOREACH_SAFE(em, &td_em->shared->threads, threads, tmp_em) {
1713 if (em->pid == td_em->pid)
1716 sp = pfind(em->pid);
1717 psignal(sp, SIGKILL);
1720 printf(LMSG("linux_sys_exit_group: kill PID %d\n"), em->pid);
1724 EMUL_SHARED_RUNLOCK(&emul_shared_lock);
1727 * XXX: we should send a signal to the parent if
1728 * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?)
1729 * as it doesnt occur often.
1731 exit1(td, W_EXITCODE(args->error_code, 0));
1737 linux_prctl(struct thread *td, struct linux_prctl_args *args)
1739 int error = 0, max_size;
1740 struct proc *p = td->td_proc;
1741 char comm[LINUX_MAX_COMM_LEN];
1742 struct linux_emuldata *em;
1747 printf(ARGS(prctl, "%d, %d, %d, %d, %d"), args->option,
1748 args->arg2, args->arg3, args->arg4, args->arg5);
1751 switch (args->option) {
1752 case LINUX_PR_SET_PDEATHSIG:
1753 if (!LINUX_SIG_VALID(args->arg2))
1755 em = em_find(p, EMUL_DOLOCK);
1756 KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1757 em->pdeath_signal = args->arg2;
1758 EMUL_UNLOCK(&emul_lock);
1760 case LINUX_PR_GET_PDEATHSIG:
1761 em = em_find(p, EMUL_DOLOCK);
1762 KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1763 pdeath_signal = em->pdeath_signal;
1764 EMUL_UNLOCK(&emul_lock);
1765 error = copyout(&pdeath_signal,
1766 (void *)(register_t)args->arg2,
1767 sizeof(pdeath_signal));
1769 case LINUX_PR_SET_NAME:
1771 * To be on the safe side we need to make sure to not
1772 * overflow the size a linux program expects. We already
1773 * do this here in the copyin, so that we don't need to
1776 max_size = MIN(sizeof(comm), sizeof(p->p_comm));
1777 error = copyinstr((void *)(register_t)args->arg2, comm,
1780 /* Linux silently truncates the name if it is too long. */
1781 if (error == ENAMETOOLONG) {
1783 * XXX: copyinstr() isn't documented to populate the
1784 * array completely, so do a copyin() to be on the
1785 * safe side. This should be changed in case
1786 * copyinstr() is changed to guarantee this.
1788 error = copyin((void *)(register_t)args->arg2, comm,
1790 comm[max_size - 1] = '\0';
1796 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
1799 case LINUX_PR_GET_NAME:
1801 strlcpy(comm, p->p_comm, sizeof(comm));
1803 error = copyout(comm, (void *)(register_t)args->arg2,
1815 * Get affinity of a process.
1818 linux_sched_getaffinity(struct thread *td,
1819 struct linux_sched_getaffinity_args *args)
1822 struct cpuset_getaffinity_args cga;
1825 if (ldebug(sched_getaffinity))
1826 printf(ARGS(sched_getaffinity, "%d, %d, *"), args->pid,
1829 if (args->len < sizeof(cpuset_t))
1832 cga.level = CPU_LEVEL_WHICH;
1833 cga.which = CPU_WHICH_PID;
1835 cga.cpusetsize = sizeof(cpuset_t);
1836 cga.mask = (cpuset_t *) args->user_mask_ptr;
1838 if ((error = cpuset_getaffinity(td, &cga)) == 0)
1839 td->td_retval[0] = sizeof(cpuset_t);
1845 * Set affinity of a process.
1848 linux_sched_setaffinity(struct thread *td,
1849 struct linux_sched_setaffinity_args *args)
1851 struct cpuset_setaffinity_args csa;
1854 if (ldebug(sched_setaffinity))
1855 printf(ARGS(sched_setaffinity, "%d, %d, *"), args->pid,
1858 if (args->len < sizeof(cpuset_t))
1861 csa.level = CPU_LEVEL_WHICH;
1862 csa.which = CPU_WHICH_PID;
1864 csa.cpusetsize = sizeof(cpuset_t);
1865 csa.mask = (cpuset_t *) args->user_mask_ptr;
1867 return (cpuset_setaffinity(td, &csa));