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"
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/resourcevar.h>
55 #include <sys/sched.h>
56 #include <sys/signalvar.h>
58 #include <sys/syscallsubr.h>
59 #include <sys/sysctl.h>
60 #include <sys/sysproto.h>
61 #include <sys/systm.h>
63 #include <sys/vmmeter.h>
64 #include <sys/vnode.h>
66 #include <sys/cpuset.h>
68 #include <security/mac/mac_framework.h>
72 #include <vm/vm_kern.h>
73 #include <vm/vm_map.h>
74 #include <vm/vm_extern.h>
75 #include <vm/vm_object.h>
76 #include <vm/swap_pager.h>
78 #include <compat/linux/linux_sysproto.h>
79 #include <compat/linux/linux_emul.h>
80 #include <compat/linux/linux_misc.h>
83 #include <machine/../linux32/linux.h>
84 #include <machine/../linux32/linux32_proto.h>
86 #include <machine/../linux/linux.h>
87 #include <machine/../linux/linux_proto.h>
90 #include <compat/linux/linux_mib.h>
91 #include <compat/linux/linux_signal.h>
92 #include <compat/linux/linux_util.h>
94 #define BSD_TO_LINUX_SIGNAL(sig) \
95 (((sig) <= LINUX_SIGTBLSZ) ? bsd_to_linux_signal[_SIG_IDX(sig)] : sig)
97 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
98 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
99 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
100 RLIMIT_MEMLOCK, RLIMIT_AS
104 l_long uptime; /* Seconds since boot */
105 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */
106 #define LINUX_SYSINFO_LOADS_SCALE 65536
107 l_ulong totalram; /* Total usable main memory size */
108 l_ulong freeram; /* Available memory size */
109 l_ulong sharedram; /* Amount of shared memory */
110 l_ulong bufferram; /* Memory used by buffers */
111 l_ulong totalswap; /* Total swap space size */
112 l_ulong freeswap; /* swap space still available */
113 l_ushort procs; /* Number of current processes */
118 char _f[20-2*sizeof(l_long)-sizeof(l_int)]; /* padding */
121 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args)
123 struct l_sysinfo sysinfo;
131 sysinfo.uptime = ts.tv_sec;
133 /* Use the information from the mib to get our load averages */
134 for (i = 0; i < 3; i++)
135 sysinfo.loads[i] = averunnable.ldavg[i] *
136 LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale;
138 sysinfo.totalram = physmem * PAGE_SIZE;
139 sysinfo.freeram = sysinfo.totalram - cnt.v_wire_count * PAGE_SIZE;
141 sysinfo.sharedram = 0;
142 mtx_lock(&vm_object_list_mtx);
143 TAILQ_FOREACH(object, &vm_object_list, object_list)
144 if (object->shadow_count > 1)
145 sysinfo.sharedram += object->resident_page_count;
146 mtx_unlock(&vm_object_list_mtx);
148 sysinfo.sharedram *= PAGE_SIZE;
149 sysinfo.bufferram = 0;
151 swap_pager_status(&i, &j);
152 sysinfo.totalswap = i * PAGE_SIZE;
153 sysinfo.freeswap = (i - j) * PAGE_SIZE;
155 sysinfo.procs = nprocs;
157 /* The following are only present in newer Linux kernels. */
158 sysinfo.totalbig = 0;
160 sysinfo.mem_unit = 1;
162 return copyout(&sysinfo, args->info, sizeof(sysinfo));
166 linux_alarm(struct thread *td, struct linux_alarm_args *args)
168 struct itimerval it, old_it;
173 printf(ARGS(alarm, "%u"), args->secs);
176 if (args->secs > 100000000)
179 it.it_value.tv_sec = (long)args->secs;
180 it.it_value.tv_usec = 0;
181 it.it_interval.tv_sec = 0;
182 it.it_interval.tv_usec = 0;
183 error = kern_setitimer(td, ITIMER_REAL, &it, &old_it);
186 if (timevalisset(&old_it.it_value)) {
187 if (old_it.it_value.tv_usec != 0)
188 old_it.it_value.tv_sec++;
189 td->td_retval[0] = old_it.it_value.tv_sec;
195 linux_brk(struct thread *td, struct linux_brk_args *args)
197 struct vmspace *vm = td->td_proc->p_vmspace;
198 vm_offset_t new, old;
199 struct obreak_args /* {
205 printf(ARGS(brk, "%p"), (void *)(uintptr_t)args->dsend);
207 old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
208 new = (vm_offset_t)args->dsend;
209 tmp.nsize = (char *)new;
210 if (((caddr_t)new > vm->vm_daddr) && !obreak(td, &tmp))
211 td->td_retval[0] = (long)new;
213 td->td_retval[0] = (long)old;
218 #if defined(__i386__)
219 /* XXX: what about amd64/linux32? */
222 linux_uselib(struct thread *td, struct linux_uselib_args *args)
229 unsigned long file_offset;
231 unsigned long bss_size;
234 int locked, vfslocked;
236 LCONVPATHEXIST(td, args->library, &library);
240 printf(ARGS(uselib, "%s"), library);
248 NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
249 UIO_SYSSPACE, library, td);
256 vfslocked = NDHASGIANT(&ni);
257 NDFREE(&ni, NDF_ONLY_PNBUF);
260 * From here on down, we have a locked vnode that must be unlocked.
261 * XXX: The code below largely duplicates exec_check_permissions().
266 if (vp->v_writecount) {
272 error = VOP_GETATTR(vp, &attr, td->td_ucred, td);
276 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
277 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
278 /* EACCESS is what exec(2) returns. */
284 if (attr.va_size == 0) {
289 /* Can we access it? */
290 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
295 * XXX: This should use vn_open() so that it is properly authorized,
296 * and to reduce code redundancy all over the place here.
297 * XXX: Not really, it duplicates far more of exec_check_permissions()
301 error = mac_check_vnode_open(td->td_ucred, vp, FREAD);
305 error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL);
309 /* Pull in executable header into kernel_map */
310 error = vm_mmap(kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE,
311 VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 0);
315 /* Is it a Linux binary ? */
316 if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
322 * While we are here, we should REALLY do some more checks
325 /* Set file/virtual offset based on a.out variant. */
326 switch ((int)(a_out->a_magic & 0xffff)) {
327 case 0413: /* ZMAGIC */
330 case 0314: /* QMAGIC */
338 bss_size = round_page(a_out->a_bss);
340 /* Check various fields in header for validity/bounds. */
341 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
346 /* text + data can't exceed file size */
347 if (a_out->a_data + a_out->a_text > attr.va_size) {
353 * text/data/bss must not exceed limits
354 * XXX - this is not complete. it should check current usage PLUS
355 * the resources needed by this library.
357 PROC_LOCK(td->td_proc);
358 if (a_out->a_text > maxtsiz ||
359 a_out->a_data + bss_size > lim_cur(td->td_proc, RLIMIT_DATA)) {
360 PROC_UNLOCK(td->td_proc);
364 PROC_UNLOCK(td->td_proc);
367 * Prevent more writers.
368 * XXX: Note that if any of the VM operations fail below we don't
371 vp->v_vflag |= VV_TEXT;
374 * Lock no longer needed
377 VOP_UNLOCK(vp, 0, td);
378 VFS_UNLOCK_GIANT(vfslocked);
381 * Check if file_offset page aligned. Currently we cannot handle
382 * misalinged file offsets, and so we read in the entire image
385 if (file_offset & PAGE_MASK) {
387 printf("uselib: Non page aligned binary %lu\n", file_offset);
389 /* Map text+data read/write/execute */
391 /* a_entry is the load address and is page aligned */
392 vmaddr = trunc_page(a_out->a_entry);
394 /* get anon user mapping, read+write+execute */
395 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
396 &vmaddr, a_out->a_text + a_out->a_data, FALSE, VM_PROT_ALL,
401 /* map file into kernel_map */
402 error = vm_mmap(kernel_map, &buffer,
403 round_page(a_out->a_text + a_out->a_data + file_offset),
404 VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp,
405 trunc_page(file_offset));
409 /* copy from kernel VM space to user space */
410 error = copyout(PTRIN(buffer + file_offset),
411 (void *)vmaddr, a_out->a_text + a_out->a_data);
413 /* release temporary kernel space */
414 vm_map_remove(kernel_map, buffer, buffer +
415 round_page(a_out->a_text + a_out->a_data + file_offset));
421 printf("uselib: Page aligned binary %lu\n", file_offset);
424 * for QMAGIC, a_entry is 20 bytes beyond the load address
425 * to skip the executable header
427 vmaddr = trunc_page(a_out->a_entry);
430 * Map it all into the process's space as a single
431 * copy-on-write "data" segment.
433 error = vm_mmap(&td->td_proc->p_vmspace->vm_map, &vmaddr,
434 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
435 MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset);
440 printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long *)vmaddr)[0],
441 ((long *)vmaddr)[1]);
444 /* Calculate BSS start address */
445 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
448 /* allocate some 'anon' space */
449 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
450 &vmaddr, bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
456 /* Unlock vnode if needed */
458 VOP_UNLOCK(vp, 0, td);
459 VFS_UNLOCK_GIANT(vfslocked);
462 /* Release the kernel mapping. */
464 vm_map_remove(kernel_map, (vm_offset_t)a_out,
465 (vm_offset_t)a_out + PAGE_SIZE);
470 #endif /* __i386__ */
473 linux_select(struct thread *td, struct linux_select_args *args)
476 struct timeval tv0, tv1, utv, *tvp;
481 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
482 (void *)args->readfds, (void *)args->writefds,
483 (void *)args->exceptfds, (void *)args->timeout);
487 * Store current time for computation of the amount of
491 if ((error = copyin(args->timeout, <v, sizeof(ltv))))
493 utv.tv_sec = ltv.tv_sec;
494 utv.tv_usec = ltv.tv_usec;
497 printf(LMSG("incoming timeout (%jd/%ld)"),
498 (intmax_t)utv.tv_sec, utv.tv_usec);
501 if (itimerfix(&utv)) {
503 * The timeval was invalid. Convert it to something
504 * valid that will act as it does under Linux.
506 utv.tv_sec += utv.tv_usec / 1000000;
507 utv.tv_usec %= 1000000;
508 if (utv.tv_usec < 0) {
510 utv.tv_usec += 1000000;
520 error = kern_select(td, args->nfds, args->readfds, args->writefds,
521 args->exceptfds, tvp);
525 printf(LMSG("real select returns %d"), error);
529 * See fs/select.c in the Linux kernel. Without this,
530 * Maelstrom doesn't work.
532 if (error == ERESTART)
538 if (td->td_retval[0]) {
540 * Compute how much time was left of the timeout,
541 * by subtracting the current time and the time
542 * before we started the call, and subtracting
543 * that result from the user-supplied value.
546 timevalsub(&tv1, &tv0);
547 timevalsub(&utv, &tv1);
554 printf(LMSG("outgoing timeout (%jd/%ld)"),
555 (intmax_t)utv.tv_sec, utv.tv_usec);
557 ltv.tv_sec = utv.tv_sec;
558 ltv.tv_usec = utv.tv_usec;
559 if ((error = copyout(<v, args->timeout, sizeof(ltv))))
566 printf(LMSG("select_out -> %d"), error);
572 linux_mremap(struct thread *td, struct linux_mremap_args *args)
574 struct munmap_args /* {
582 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
583 (void *)(uintptr_t)args->addr,
584 (unsigned long)args->old_len,
585 (unsigned long)args->new_len,
586 (unsigned long)args->flags);
589 if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) {
590 td->td_retval[0] = 0;
595 * Check for the page alignment.
596 * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK.
598 if (args->addr & PAGE_MASK) {
599 td->td_retval[0] = 0;
603 args->new_len = round_page(args->new_len);
604 args->old_len = round_page(args->old_len);
606 if (args->new_len > args->old_len) {
607 td->td_retval[0] = 0;
611 if (args->new_len < args->old_len) {
613 (caddr_t)((uintptr_t)args->addr + args->new_len);
614 bsd_args.len = args->old_len - args->new_len;
615 error = munmap(td, &bsd_args);
618 td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
622 #define LINUX_MS_ASYNC 0x0001
623 #define LINUX_MS_INVALIDATE 0x0002
624 #define LINUX_MS_SYNC 0x0004
627 linux_msync(struct thread *td, struct linux_msync_args *args)
629 struct msync_args bsd_args;
631 bsd_args.addr = (caddr_t)(uintptr_t)args->addr;
632 bsd_args.len = (uintptr_t)args->len;
633 bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
635 return msync(td, &bsd_args);
639 linux_time(struct thread *td, struct linux_time_args *args)
647 printf(ARGS(time, "*"));
652 if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
654 td->td_retval[0] = tm;
658 struct l_times_argv {
665 #define CLK_TCK 100 /* Linux uses 100 */
667 #define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
670 linux_times(struct thread *td, struct linux_times_args *args)
672 struct timeval tv, utime, stime, cutime, cstime;
673 struct l_times_argv tms;
679 printf(ARGS(times, "*"));
682 if (args->buf != NULL) {
686 calcru(p, &utime, &stime);
688 calccru(p, &cutime, &cstime);
691 tms.tms_utime = CONVTCK(utime);
692 tms.tms_stime = CONVTCK(stime);
694 tms.tms_cutime = CONVTCK(cutime);
695 tms.tms_cstime = CONVTCK(cstime);
697 if ((error = copyout(&tms, args->buf, sizeof(tms))))
702 td->td_retval[0] = (int)CONVTCK(tv);
707 linux_newuname(struct thread *td, struct linux_newuname_args *args)
709 struct l_new_utsname utsname;
710 char osname[LINUX_MAX_UTSNAME];
711 char osrelease[LINUX_MAX_UTSNAME];
715 if (ldebug(newuname))
716 printf(ARGS(newuname, "*"));
719 linux_get_osname(td, osname);
720 linux_get_osrelease(td, osrelease);
722 bzero(&utsname, sizeof(utsname));
723 strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
724 getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
725 strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
726 strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
727 for (p = utsname.version; *p != '\0'; ++p)
732 strlcpy(utsname.machine, linux_platform, LINUX_MAX_UTSNAME);
734 strlcpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME);
736 return (copyout(&utsname, args->buf, sizeof(utsname)));
739 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
746 linux_utime(struct thread *td, struct linux_utime_args *args)
748 struct timeval tv[2], *tvp;
749 struct l_utimbuf lut;
753 LCONVPATHEXIST(td, args->fname, &fname);
757 printf(ARGS(utime, "%s, *"), fname);
761 if ((error = copyin(args->times, &lut, sizeof lut))) {
765 tv[0].tv_sec = lut.l_actime;
767 tv[1].tv_sec = lut.l_modtime;
773 error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
779 linux_utimes(struct thread *td, struct linux_utimes_args *args)
782 struct timeval tv[2], *tvp = NULL;
786 LCONVPATHEXIST(td, args->fname, &fname);
790 printf(ARGS(utimes, "%s, *"), fname);
793 if (args->tptr != NULL) {
794 if ((error = copyin(args->tptr, ltv, sizeof ltv))) {
798 tv[0].tv_sec = ltv[0].tv_sec;
799 tv[0].tv_usec = ltv[0].tv_usec;
800 tv[1].tv_sec = ltv[1].tv_sec;
801 tv[1].tv_usec = ltv[1].tv_usec;
805 error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
809 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
811 #define __WCLONE 0x80000000
814 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
816 int error, options, tmpstat;
820 printf(ARGS(waitpid, "%d, %p, %d"),
821 args->pid, (void *)args->status, args->options);
824 * this is necessary because the test in kern_wait doesn't work
825 * because we mess with the options here
827 if (args->options & ~(WUNTRACED | WNOHANG | WCONTINUED | __WCLONE))
830 options = (args->options & (WNOHANG | WUNTRACED));
831 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
832 if (args->options & __WCLONE)
833 options |= WLINUXCLONE;
835 error = kern_wait(td, args->pid, &tmpstat, options, NULL);
841 if (WIFSIGNALED(tmpstat))
842 tmpstat = (tmpstat & 0xffffff80) |
843 BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
844 else if (WIFSTOPPED(tmpstat))
845 tmpstat = (tmpstat & 0xffff00ff) |
846 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
847 return copyout(&tmpstat, args->status, sizeof(int));
854 linux_wait4(struct thread *td, struct linux_wait4_args *args)
856 int error, options, tmpstat;
857 struct rusage ru, *rup;
862 printf(ARGS(wait4, "%d, %p, %d, %p"),
863 args->pid, (void *)args->status, args->options,
864 (void *)args->rusage);
867 options = (args->options & (WNOHANG | WUNTRACED));
868 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
869 if (args->options & __WCLONE)
870 options |= WLINUXCLONE;
872 if (args->rusage != NULL)
876 error = kern_wait(td, args->pid, &tmpstat, options, rup);
882 sigqueue_delete(&p->p_sigqueue, SIGCHLD);
887 if (WIFSIGNALED(tmpstat))
888 tmpstat = (tmpstat & 0xffffff80) |
889 BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
890 else if (WIFSTOPPED(tmpstat))
891 tmpstat = (tmpstat & 0xffff00ff) |
892 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
893 error = copyout(&tmpstat, args->status, sizeof(int));
895 if (args->rusage != NULL && error == 0)
896 error = copyout(&ru, args->rusage, sizeof(ru));
902 linux_mknod(struct thread *td, struct linux_mknod_args *args)
907 LCONVPATHCREAT(td, args->path, &path);
911 printf(ARGS(mknod, "%s, %d, %d"), path, args->mode, args->dev);
914 switch (args->mode & S_IFMT) {
917 error = kern_mkfifo(td, path, UIO_SYSSPACE, args->mode);
922 error = kern_mknod(td, path, UIO_SYSSPACE, args->mode,
931 args->mode |= S_IFREG;
934 error = kern_open(td, path, UIO_SYSSPACE,
935 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
947 * UGH! This is just about the dumbest idea I've ever heard!!
950 linux_personality(struct thread *td, struct linux_personality_args *args)
953 if (ldebug(personality))
954 printf(ARGS(personality, "%lu"), (unsigned long)args->per);
959 /* Yes Jim, it's still a Linux... */
960 td->td_retval[0] = 0;
965 l_timeval it_interval;
969 #define B2L_ITIMERVAL(bip, lip) \
970 (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec; \
971 (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec; \
972 (bip)->it_value.tv_sec = (lip)->it_value.tv_sec; \
973 (bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
976 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
979 struct l_itimerval ls;
980 struct itimerval aitv, oitv;
983 if (ldebug(setitimer))
984 printf(ARGS(setitimer, "%p, %p"),
985 (void *)uap->itv, (void *)uap->oitv);
988 if (uap->itv == NULL) {
989 uap->itv = uap->oitv;
990 return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
993 error = copyin(uap->itv, &ls, sizeof(ls));
996 B2L_ITIMERVAL(&aitv, &ls);
998 if (ldebug(setitimer)) {
999 printf("setitimer: value: sec: %jd, usec: %ld\n",
1000 (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec);
1001 printf("setitimer: interval: sec: %jd, usec: %ld\n",
1002 (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec);
1005 error = kern_setitimer(td, uap->which, &aitv, &oitv);
1006 if (error != 0 || uap->oitv == NULL)
1008 B2L_ITIMERVAL(&ls, &oitv);
1010 return (copyout(&ls, uap->oitv, sizeof(ls)));
1014 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
1017 struct l_itimerval ls;
1018 struct itimerval aitv;
1021 if (ldebug(getitimer))
1022 printf(ARGS(getitimer, "%p"), (void *)uap->itv);
1024 error = kern_getitimer(td, uap->which, &aitv);
1027 B2L_ITIMERVAL(&ls, &aitv);
1028 return (copyout(&ls, uap->itv, sizeof(ls)));
1032 linux_nice(struct thread *td, struct linux_nice_args *args)
1034 struct setpriority_args bsd_args;
1036 bsd_args.which = PRIO_PROCESS;
1037 bsd_args.who = 0; /* current process */
1038 bsd_args.prio = args->inc;
1039 return setpriority(td, &bsd_args);
1043 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
1045 struct ucred *newcred, *oldcred;
1046 l_gid_t linux_gidset[NGROUPS];
1051 ngrp = args->gidsetsize;
1052 if (ngrp < 0 || ngrp >= NGROUPS)
1054 error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
1060 oldcred = p->p_ucred;
1063 * cr_groups[0] holds egid. Setting the whole set from
1064 * the supplied set will cause egid to be changed too.
1065 * Keep cr_groups[0] unchanged to prevent that.
1068 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0) {
1074 crcopy(newcred, oldcred);
1076 newcred->cr_ngroups = ngrp + 1;
1078 bsd_gidset = newcred->cr_groups;
1081 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1085 newcred->cr_ngroups = 1;
1088 p->p_ucred = newcred;
1095 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1098 l_gid_t linux_gidset[NGROUPS];
1100 int bsd_gidsetsz, ngrp, error;
1102 cred = td->td_ucred;
1103 bsd_gidset = cred->cr_groups;
1104 bsd_gidsetsz = cred->cr_ngroups - 1;
1107 * cr_groups[0] holds egid. Returning the whole set
1108 * here will cause a duplicate. Exclude cr_groups[0]
1112 if ((ngrp = args->gidsetsize) == 0) {
1113 td->td_retval[0] = bsd_gidsetsz;
1117 if (ngrp < bsd_gidsetsz)
1121 while (ngrp < bsd_gidsetsz) {
1122 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1126 if ((error = copyout(linux_gidset, args->grouplist,
1127 ngrp * sizeof(l_gid_t))))
1130 td->td_retval[0] = ngrp;
1135 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1137 struct rlimit bsd_rlim;
1138 struct l_rlimit rlim;
1143 if (ldebug(setrlimit))
1144 printf(ARGS(setrlimit, "%d, %p"),
1145 args->resource, (void *)args->rlim);
1148 if (args->resource >= LINUX_RLIM_NLIMITS)
1151 which = linux_to_bsd_resource[args->resource];
1155 error = copyin(args->rlim, &rlim, sizeof(rlim));
1159 bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1160 bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1161 return (kern_setrlimit(td, which, &bsd_rlim));
1165 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1167 struct l_rlimit rlim;
1168 struct proc *p = td->td_proc;
1169 struct rlimit bsd_rlim;
1173 if (ldebug(old_getrlimit))
1174 printf(ARGS(old_getrlimit, "%d, %p"),
1175 args->resource, (void *)args->rlim);
1178 if (args->resource >= LINUX_RLIM_NLIMITS)
1181 which = linux_to_bsd_resource[args->resource];
1186 lim_rlimit(p, which, &bsd_rlim);
1189 #ifdef COMPAT_LINUX32
1190 rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1191 if (rlim.rlim_cur == UINT_MAX)
1192 rlim.rlim_cur = INT_MAX;
1193 rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1194 if (rlim.rlim_max == UINT_MAX)
1195 rlim.rlim_max = INT_MAX;
1197 rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1198 if (rlim.rlim_cur == ULONG_MAX)
1199 rlim.rlim_cur = LONG_MAX;
1200 rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1201 if (rlim.rlim_max == ULONG_MAX)
1202 rlim.rlim_max = LONG_MAX;
1204 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1208 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1210 struct l_rlimit rlim;
1211 struct proc *p = td->td_proc;
1212 struct rlimit bsd_rlim;
1216 if (ldebug(getrlimit))
1217 printf(ARGS(getrlimit, "%d, %p"),
1218 args->resource, (void *)args->rlim);
1221 if (args->resource >= LINUX_RLIM_NLIMITS)
1224 which = linux_to_bsd_resource[args->resource];
1229 lim_rlimit(p, which, &bsd_rlim);
1232 rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1233 rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1234 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1238 linux_sched_setscheduler(struct thread *td,
1239 struct linux_sched_setscheduler_args *args)
1241 struct sched_setscheduler_args bsd;
1244 if (ldebug(sched_setscheduler))
1245 printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1246 args->pid, args->policy, (const void *)args->param);
1249 switch (args->policy) {
1250 case LINUX_SCHED_OTHER:
1251 bsd.policy = SCHED_OTHER;
1253 case LINUX_SCHED_FIFO:
1254 bsd.policy = SCHED_FIFO;
1256 case LINUX_SCHED_RR:
1257 bsd.policy = SCHED_RR;
1263 bsd.pid = args->pid;
1264 bsd.param = (struct sched_param *)args->param;
1265 return sched_setscheduler(td, &bsd);
1269 linux_sched_getscheduler(struct thread *td,
1270 struct linux_sched_getscheduler_args *args)
1272 struct sched_getscheduler_args bsd;
1276 if (ldebug(sched_getscheduler))
1277 printf(ARGS(sched_getscheduler, "%d"), args->pid);
1280 bsd.pid = args->pid;
1281 error = sched_getscheduler(td, &bsd);
1283 switch (td->td_retval[0]) {
1285 td->td_retval[0] = LINUX_SCHED_OTHER;
1288 td->td_retval[0] = LINUX_SCHED_FIFO;
1291 td->td_retval[0] = LINUX_SCHED_RR;
1299 linux_sched_get_priority_max(struct thread *td,
1300 struct linux_sched_get_priority_max_args *args)
1302 struct sched_get_priority_max_args bsd;
1305 if (ldebug(sched_get_priority_max))
1306 printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1309 switch (args->policy) {
1310 case LINUX_SCHED_OTHER:
1311 bsd.policy = SCHED_OTHER;
1313 case LINUX_SCHED_FIFO:
1314 bsd.policy = SCHED_FIFO;
1316 case LINUX_SCHED_RR:
1317 bsd.policy = SCHED_RR;
1322 return sched_get_priority_max(td, &bsd);
1326 linux_sched_get_priority_min(struct thread *td,
1327 struct linux_sched_get_priority_min_args *args)
1329 struct sched_get_priority_min_args bsd;
1332 if (ldebug(sched_get_priority_min))
1333 printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1336 switch (args->policy) {
1337 case LINUX_SCHED_OTHER:
1338 bsd.policy = SCHED_OTHER;
1340 case LINUX_SCHED_FIFO:
1341 bsd.policy = SCHED_FIFO;
1343 case LINUX_SCHED_RR:
1344 bsd.policy = SCHED_RR;
1349 return sched_get_priority_min(td, &bsd);
1352 #define REBOOT_CAD_ON 0x89abcdef
1353 #define REBOOT_CAD_OFF 0
1354 #define REBOOT_HALT 0xcdef0123
1355 #define REBOOT_RESTART 0x01234567
1356 #define REBOOT_RESTART2 0xA1B2C3D4
1357 #define REBOOT_POWEROFF 0x4321FEDC
1358 #define REBOOT_MAGIC1 0xfee1dead
1359 #define REBOOT_MAGIC2 0x28121969
1360 #define REBOOT_MAGIC2A 0x05121996
1361 #define REBOOT_MAGIC2B 0x16041998
1364 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1366 struct reboot_args bsd_args;
1370 printf(ARGS(reboot, "0x%x"), args->cmd);
1373 if (args->magic1 != REBOOT_MAGIC1)
1376 switch (args->magic2) {
1378 case REBOOT_MAGIC2A:
1379 case REBOOT_MAGIC2B:
1385 switch (args->cmd) {
1387 case REBOOT_CAD_OFF:
1388 return (priv_check(td, PRIV_REBOOT));
1390 bsd_args.opt = RB_HALT;
1392 case REBOOT_RESTART:
1393 case REBOOT_RESTART2:
1396 case REBOOT_POWEROFF:
1397 bsd_args.opt = RB_POWEROFF;
1402 return reboot(td, &bsd_args);
1407 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1408 * td->td_retval[1] when COMPAT_43 is defined. This clobbers registers that
1409 * are assumed to be preserved. The following lightweight syscalls fixes
1410 * this. See also linux_getgid16() and linux_getuid16() in linux_uid16.c
1412 * linux_getpid() - MP SAFE
1413 * linux_getgid() - MP SAFE
1414 * linux_getuid() - MP SAFE
1418 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1420 struct linux_emuldata *em;
1424 printf(ARGS(getpid, ""));
1427 if (linux_use26(td)) {
1428 em = em_find(td->td_proc, EMUL_DONTLOCK);
1429 KASSERT(em != NULL, ("getpid: emuldata not found.\n"));
1430 td->td_retval[0] = em->shared->group_pid;
1432 td->td_retval[0] = td->td_proc->p_pid;
1439 linux_gettid(struct thread *td, struct linux_gettid_args *args)
1444 printf(ARGS(gettid, ""));
1447 td->td_retval[0] = td->td_proc->p_pid;
1453 linux_getppid(struct thread *td, struct linux_getppid_args *args)
1455 struct linux_emuldata *em;
1456 struct proc *p, *pp;
1459 if (ldebug(getppid))
1460 printf(ARGS(getppid, ""));
1463 if (!linux_use26(td)) {
1464 PROC_LOCK(td->td_proc);
1465 td->td_retval[0] = td->td_proc->p_pptr->p_pid;
1466 PROC_UNLOCK(td->td_proc);
1470 em = em_find(td->td_proc, EMUL_DONTLOCK);
1472 KASSERT(em != NULL, ("getppid: process emuldata not found.\n"));
1474 /* find the group leader */
1475 p = pfind(em->shared->group_pid);
1479 printf(LMSG("parent process not found.\n"));
1484 pp = p->p_pptr; /* switch to parent */
1488 /* if its also linux process */
1489 if (pp->p_sysent == &elf_linux_sysvec) {
1490 em = em_find(pp, EMUL_DONTLOCK);
1491 KASSERT(em != NULL, ("getppid: parent emuldata not found.\n"));
1493 td->td_retval[0] = em->shared->group_pid;
1495 td->td_retval[0] = pp->p_pid;
1503 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1508 printf(ARGS(getgid, ""));
1511 td->td_retval[0] = td->td_ucred->cr_rgid;
1516 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1521 printf(ARGS(getuid, ""));
1524 td->td_retval[0] = td->td_ucred->cr_ruid;
1530 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1532 struct getsid_args bsd;
1536 printf(ARGS(getsid, "%i"), args->pid);
1539 bsd.pid = args->pid;
1540 return getsid(td, &bsd);
1544 linux_nosys(struct thread *td, struct nosys_args *ignore)
1551 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1553 struct getpriority_args bsd_args;
1557 if (ldebug(getpriority))
1558 printf(ARGS(getpriority, "%i, %i"), args->which, args->who);
1561 bsd_args.which = args->which;
1562 bsd_args.who = args->who;
1563 error = getpriority(td, &bsd_args);
1564 td->td_retval[0] = 20 - td->td_retval[0];
1569 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1574 if (ldebug(sethostname))
1575 printf(ARGS(sethostname, "*, %i"), args->len);
1579 name[1] = KERN_HOSTNAME;
1580 return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1585 linux_exit_group(struct thread *td, struct linux_exit_group_args *args)
1587 struct linux_emuldata *em, *td_em, *tmp_em;
1591 if (ldebug(exit_group))
1592 printf(ARGS(exit_group, "%i"), args->error_code);
1595 if (linux_use26(td)) {
1596 td_em = em_find(td->td_proc, EMUL_DONTLOCK);
1598 KASSERT(td_em != NULL, ("exit_group: emuldata not found.\n"));
1600 EMUL_SHARED_RLOCK(&emul_shared_lock);
1601 LIST_FOREACH_SAFE(em, &td_em->shared->threads, threads, tmp_em) {
1602 if (em->pid == td_em->pid)
1605 sp = pfind(em->pid);
1606 psignal(sp, SIGKILL);
1609 printf(LMSG("linux_sys_exit_group: kill PID %d\n"), em->pid);
1613 EMUL_SHARED_RUNLOCK(&emul_shared_lock);
1616 * XXX: we should send a signal to the parent if
1617 * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?)
1618 * as it doesnt occur often.
1620 exit1(td, W_EXITCODE(args->error_code, 0));
1626 linux_prctl(struct thread *td, struct linux_prctl_args *args)
1628 int error = 0, max_size;
1629 struct proc *p = td->td_proc;
1630 char comm[LINUX_MAX_COMM_LEN];
1631 struct linux_emuldata *em;
1636 printf(ARGS(prctl, "%d, %d, %d, %d, %d"), args->option,
1637 args->arg2, args->arg3, args->arg4, args->arg5);
1640 switch (args->option) {
1641 case LINUX_PR_SET_PDEATHSIG:
1642 if (!LINUX_SIG_VALID(args->arg2))
1644 em = em_find(p, EMUL_DOLOCK);
1645 KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1646 em->pdeath_signal = args->arg2;
1647 EMUL_UNLOCK(&emul_lock);
1649 case LINUX_PR_GET_PDEATHSIG:
1650 em = em_find(p, EMUL_DOLOCK);
1651 KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1652 pdeath_signal = em->pdeath_signal;
1653 EMUL_UNLOCK(&emul_lock);
1654 error = copyout(&pdeath_signal,
1655 (void *)(register_t)args->arg2,
1656 sizeof(pdeath_signal));
1658 case LINUX_PR_SET_NAME:
1660 * To be on the safe side we need to make sure to not
1661 * overflow the size a linux program expects. We already
1662 * do this here in the copyin, so that we don't need to
1665 max_size = MIN(sizeof(comm), sizeof(p->p_comm));
1666 error = copyinstr((void *)(register_t)args->arg2, comm,
1669 /* Linux silently truncates the name if it is too long. */
1670 if (error == ENAMETOOLONG) {
1672 * XXX: copyinstr() isn't documented to populate the
1673 * array completely, so do a copyin() to be on the
1674 * safe side. This should be changed in case
1675 * copyinstr() is changed to guarantee this.
1677 error = copyin((void *)(register_t)args->arg2, comm,
1679 comm[max_size - 1] = '\0';
1685 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
1688 case LINUX_PR_GET_NAME:
1690 strlcpy(comm, p->p_comm, sizeof(comm));
1692 error = copyout(comm, (void *)(register_t)args->arg2,
1704 * Get affinity of a process.
1707 linux_sched_getaffinity(struct thread *td,
1708 struct linux_sched_getaffinity_args *args)
1711 struct cpuset_getaffinity_args cga;
1714 if (ldebug(sched_getaffinity))
1715 printf(ARGS(sched_getaffinity, "%d, %d, *"), args->pid,
1718 if (args->len < sizeof(cpuset_t))
1721 cga.level = CPU_LEVEL_WHICH;
1722 cga.which = CPU_WHICH_PID;
1724 cga.cpusetsize = sizeof(cpuset_t);
1725 cga.mask = (cpuset_t *) args->user_mask_ptr;
1727 if ((error = cpuset_getaffinity(td, &cga)) == 0)
1728 td->td_retval[0] = sizeof(cpuset_t);
1734 * Set affinity of a process.
1737 linux_sched_setaffinity(struct thread *td,
1738 struct linux_sched_setaffinity_args *args)
1740 struct cpuset_setaffinity_args csa;
1743 if (ldebug(sched_setaffinity))
1744 printf(ARGS(sched_setaffinity, "%d, %d, *"), args->pid,
1747 if (args->len < sizeof(cpuset_t))
1750 csa.level = CPU_LEVEL_WHICH;
1751 csa.which = CPU_WHICH_PID;
1753 csa.cpusetsize = sizeof(cpuset_t);
1754 csa.mask = (cpuset_t *) args->user_mask_ptr;
1756 return (cpuset_setaffinity(td, &csa));