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>
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>
77 #include <compat/linux/linux_sysproto.h>
78 #include <compat/linux/linux_emul.h>
79 #include <compat/linux/linux_misc.h>
82 #include <machine/../linux32/linux.h>
83 #include <machine/../linux32/linux32_proto.h>
85 #include <machine/../linux/linux.h>
86 #include <machine/../linux/linux_proto.h>
89 #include <compat/linux/linux_mib.h>
90 #include <compat/linux/linux_signal.h>
91 #include <compat/linux/linux_util.h>
94 #include <machine/cputypes.h>
97 #define BSD_TO_LINUX_SIGNAL(sig) \
98 (((sig) <= LINUX_SIGTBLSZ) ? bsd_to_linux_signal[_SIG_IDX(sig)] : sig)
100 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
101 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
102 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
107 l_long uptime; /* Seconds since boot */
108 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */
109 #define LINUX_SYSINFO_LOADS_SCALE 65536
110 l_ulong totalram; /* Total usable main memory size */
111 l_ulong freeram; /* Available memory size */
112 l_ulong sharedram; /* Amount of shared memory */
113 l_ulong bufferram; /* Memory used by buffers */
114 l_ulong totalswap; /* Total swap space size */
115 l_ulong freeswap; /* swap space still available */
116 l_ushort procs; /* Number of current processes */
120 char _f[6]; /* Pads structure to 64 bytes */
123 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args)
125 struct l_sysinfo sysinfo;
133 sysinfo.uptime = ts.tv_sec;
135 /* Use the information from the mib to get our load averages */
136 for (i = 0; i < 3; i++)
137 sysinfo.loads[i] = averunnable.ldavg[i] *
138 LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale;
140 sysinfo.totalram = physmem * PAGE_SIZE;
141 sysinfo.freeram = sysinfo.totalram - cnt.v_wire_count * PAGE_SIZE;
143 sysinfo.sharedram = 0;
144 mtx_lock(&vm_object_list_mtx);
145 TAILQ_FOREACH(object, &vm_object_list, object_list)
146 if (object->shadow_count > 1)
147 sysinfo.sharedram += object->resident_page_count;
148 mtx_unlock(&vm_object_list_mtx);
150 sysinfo.sharedram *= PAGE_SIZE;
151 sysinfo.bufferram = 0;
153 swap_pager_status(&i, &j);
154 sysinfo.totalswap= i * PAGE_SIZE;
155 sysinfo.freeswap = (i - j) * PAGE_SIZE;
157 sysinfo.procs = nprocs;
159 /* The following are only present in newer Linux kernels. */
160 sysinfo.totalbig = 0;
162 sysinfo.mem_unit = 1;
164 return copyout(&sysinfo, args->info, sizeof(sysinfo));
168 linux_alarm(struct thread *td, struct linux_alarm_args *args)
170 struct itimerval it, old_it;
175 printf(ARGS(alarm, "%u"), args->secs);
178 if (args->secs > 100000000)
181 it.it_value.tv_sec = (long)args->secs;
182 it.it_value.tv_usec = 0;
183 it.it_interval.tv_sec = 0;
184 it.it_interval.tv_usec = 0;
185 error = kern_setitimer(td, ITIMER_REAL, &it, &old_it);
188 if (timevalisset(&old_it.it_value)) {
189 if (old_it.it_value.tv_usec != 0)
190 old_it.it_value.tv_sec++;
191 td->td_retval[0] = old_it.it_value.tv_sec;
197 linux_brk(struct thread *td, struct linux_brk_args *args)
199 struct vmspace *vm = td->td_proc->p_vmspace;
200 vm_offset_t new, old;
201 struct obreak_args /* {
207 printf(ARGS(brk, "%p"), (void *)(uintptr_t)args->dsend);
209 old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
210 new = (vm_offset_t)args->dsend;
211 tmp.nsize = (char *) new;
212 if (((caddr_t)new > vm->vm_daddr) && !obreak(td, &tmp))
213 td->td_retval[0] = (long)new;
215 td->td_retval[0] = (long)old;
220 #if defined(__i386__)
221 /* XXX: what about amd64/linux32? */
224 linux_uselib(struct thread *td, struct linux_uselib_args *args)
231 unsigned long file_offset;
233 unsigned long bss_size;
236 int locked, vfslocked;
238 LCONVPATHEXIST(td, args->library, &library);
242 printf(ARGS(uselib, "%s"), library);
250 NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
251 UIO_SYSSPACE, library, td);
258 vfslocked = NDHASGIANT(&ni);
259 NDFREE(&ni, NDF_ONLY_PNBUF);
262 * From here on down, we have a locked vnode that must be unlocked.
263 * XXX: The code below largely duplicates exec_check_permissions().
268 if (vp->v_writecount) {
274 error = VOP_GETATTR(vp, &attr, td->td_ucred, td);
278 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
279 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
280 /* EACCESS is what exec(2) returns. */
286 if (attr.va_size == 0) {
291 /* Can we access it? */
292 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
297 * XXX: This should use vn_open() so that it is properly authorized,
298 * and to reduce code redundancy all over the place here.
299 * XXX: Not really, it duplicates far more of exec_check_permissions()
303 error = mac_check_vnode_open(td->td_ucred, vp, FREAD);
307 error = VOP_OPEN(vp, FREAD, td->td_ucred, td, -1);
311 /* Pull in executable header into kernel_map */
312 error = vm_mmap(kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE,
313 VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 0);
317 /* Is it a Linux binary ? */
318 if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
324 * While we are here, we should REALLY do some more checks
327 /* Set file/virtual offset based on a.out variant. */
328 switch ((int)(a_out->a_magic & 0xffff)) {
329 case 0413: /* ZMAGIC */
332 case 0314: /* QMAGIC */
340 bss_size = round_page(a_out->a_bss);
342 /* Check various fields in header for validity/bounds. */
343 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
348 /* text + data can't exceed file size */
349 if (a_out->a_data + a_out->a_text > attr.va_size) {
355 * text/data/bss must not exceed limits
356 * XXX - this is not complete. it should check current usage PLUS
357 * the resources needed by this library.
359 PROC_LOCK(td->td_proc);
360 if (a_out->a_text > maxtsiz ||
361 a_out->a_data + bss_size > lim_cur(td->td_proc, RLIMIT_DATA)) {
362 PROC_UNLOCK(td->td_proc);
366 PROC_UNLOCK(td->td_proc);
369 * Prevent more writers.
370 * XXX: Note that if any of the VM operations fail below we don't
373 vp->v_vflag |= VV_TEXT;
376 * Lock no longer needed
379 VOP_UNLOCK(vp, 0, td);
380 VFS_UNLOCK_GIANT(vfslocked);
383 * Check if file_offset page aligned. Currently we cannot handle
384 * misalinged file offsets, and so we read in the entire image
387 if (file_offset & PAGE_MASK) {
389 printf("uselib: Non page aligned binary %lu\n", file_offset);
391 /* Map text+data read/write/execute */
393 /* a_entry is the load address and is page aligned */
394 vmaddr = trunc_page(a_out->a_entry);
396 /* get anon user mapping, read+write+execute */
397 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
398 &vmaddr, a_out->a_text + a_out->a_data, FALSE, VM_PROT_ALL,
403 /* map file into kernel_map */
404 error = vm_mmap(kernel_map, &buffer,
405 round_page(a_out->a_text + a_out->a_data + file_offset),
406 VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp,
407 trunc_page(file_offset));
411 /* copy from kernel VM space to user space */
412 error = copyout(PTRIN(buffer + file_offset),
413 (void *)vmaddr, a_out->a_text + a_out->a_data);
415 /* release temporary kernel space */
416 vm_map_remove(kernel_map, buffer, buffer +
417 round_page(a_out->a_text + a_out->a_data + file_offset));
423 printf("uselib: Page aligned binary %lu\n", file_offset);
426 * for QMAGIC, a_entry is 20 bytes beyond the load address
427 * to skip the executable header
429 vmaddr = trunc_page(a_out->a_entry);
432 * Map it all into the process's space as a single
433 * copy-on-write "data" segment.
435 error = vm_mmap(&td->td_proc->p_vmspace->vm_map, &vmaddr,
436 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
437 MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset);
442 printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long*)vmaddr)[0],
446 /* Calculate BSS start address */
447 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
450 /* allocate some 'anon' space */
451 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
452 &vmaddr, bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
458 /* Unlock vnode if needed */
460 VOP_UNLOCK(vp, 0, td);
461 VFS_UNLOCK_GIANT(vfslocked);
464 /* Release the kernel mapping. */
466 vm_map_remove(kernel_map, (vm_offset_t)a_out,
467 (vm_offset_t)a_out + PAGE_SIZE);
472 #endif /* __i386__ */
475 linux_select(struct thread *td, struct linux_select_args *args)
478 struct timeval tv0, tv1, utv, *tvp;
483 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
484 (void *)args->readfds, (void *)args->writefds,
485 (void *)args->exceptfds, (void *)args->timeout);
489 * Store current time for computation of the amount of
493 if ((error = copyin(args->timeout, <v, sizeof(ltv))))
495 utv.tv_sec = ltv.tv_sec;
496 utv.tv_usec = ltv.tv_usec;
499 printf(LMSG("incoming timeout (%jd/%ld)"),
500 (intmax_t)utv.tv_sec, utv.tv_usec);
503 if (itimerfix(&utv)) {
505 * The timeval was invalid. Convert it to something
506 * valid that will act as it does under Linux.
508 utv.tv_sec += utv.tv_usec / 1000000;
509 utv.tv_usec %= 1000000;
510 if (utv.tv_usec < 0) {
512 utv.tv_usec += 1000000;
522 error = kern_select(td, args->nfds, args->readfds, args->writefds,
523 args->exceptfds, tvp);
527 printf(LMSG("real select returns %d"), error);
531 * See fs/select.c in the Linux kernel. Without this,
532 * Maelstrom doesn't work.
534 if (error == ERESTART)
540 if (td->td_retval[0]) {
542 * Compute how much time was left of the timeout,
543 * by subtracting the current time and the time
544 * before we started the call, and subtracting
545 * that result from the user-supplied value.
548 timevalsub(&tv1, &tv0);
549 timevalsub(&utv, &tv1);
556 printf(LMSG("outgoing timeout (%jd/%ld)"),
557 (intmax_t)utv.tv_sec, utv.tv_usec);
559 ltv.tv_sec = utv.tv_sec;
560 ltv.tv_usec = utv.tv_usec;
561 if ((error = copyout(<v, args->timeout, sizeof(ltv))))
568 printf(LMSG("select_out -> %d"), error);
574 linux_mremap(struct thread *td, struct linux_mremap_args *args)
576 struct munmap_args /* {
584 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
585 (void *)(uintptr_t)args->addr,
586 (unsigned long)args->old_len,
587 (unsigned long)args->new_len,
588 (unsigned long)args->flags);
590 args->new_len = round_page(args->new_len);
591 args->old_len = round_page(args->old_len);
593 if (args->new_len > args->old_len) {
594 td->td_retval[0] = 0;
598 if (args->new_len < args->old_len) {
600 (caddr_t)((uintptr_t)args->addr + args->new_len);
601 bsd_args.len = args->old_len - args->new_len;
602 error = munmap(td, &bsd_args);
605 td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
609 #define LINUX_MS_ASYNC 0x0001
610 #define LINUX_MS_INVALIDATE 0x0002
611 #define LINUX_MS_SYNC 0x0004
614 linux_msync(struct thread *td, struct linux_msync_args *args)
616 struct msync_args bsd_args;
618 bsd_args.addr = (caddr_t)(uintptr_t)args->addr;
619 bsd_args.len = (uintptr_t)args->len;
620 bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
622 return msync(td, &bsd_args);
626 linux_time(struct thread *td, struct linux_time_args *args)
634 printf(ARGS(time, "*"));
639 if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
641 td->td_retval[0] = tm;
645 struct l_times_argv {
652 #define CLK_TCK 100 /* Linux uses 100 */
654 #define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
657 linux_times(struct thread *td, struct linux_times_args *args)
659 struct timeval tv, utime, stime, cutime, cstime;
660 struct l_times_argv tms;
666 printf(ARGS(times, "*"));
669 if (args->buf != NULL) {
672 calcru(p, &utime, &stime);
673 calccru(p, &cutime, &cstime);
676 tms.tms_utime = CONVTCK(utime);
677 tms.tms_stime = CONVTCK(stime);
679 tms.tms_cutime = CONVTCK(cutime);
680 tms.tms_cstime = CONVTCK(cstime);
682 if ((error = copyout(&tms, args->buf, sizeof(tms))))
687 td->td_retval[0] = (int)CONVTCK(tv);
692 linux_newuname(struct thread *td, struct linux_newuname_args *args)
694 struct l_new_utsname utsname;
695 char osname[LINUX_MAX_UTSNAME];
696 char osrelease[LINUX_MAX_UTSNAME];
700 if (ldebug(newuname))
701 printf(ARGS(newuname, "*"));
704 linux_get_osname(td, osname);
705 linux_get_osrelease(td, osrelease);
707 bzero(&utsname, sizeof(utsname));
708 strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
709 getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
710 strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
711 strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
712 for (p = utsname.version; *p != '\0'; ++p)
733 strlcpy(utsname.machine, class, LINUX_MAX_UTSNAME);
735 #elif defined(__amd64__) /* XXX: Linux can change 'personality'. */
736 #ifdef COMPAT_LINUX32
737 strlcpy(utsname.machine, "i686", LINUX_MAX_UTSNAME);
739 strlcpy(utsname.machine, "x86_64", LINUX_MAX_UTSNAME);
740 #endif /* COMPAT_LINUX32 */
741 #else /* something other than i386 or amd64 - assume we and Linux agree */
742 strlcpy(utsname.machine, machine, LINUX_MAX_UTSNAME);
743 #endif /* __i386__ */
744 strlcpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME);
746 return (copyout(&utsname, args->buf, sizeof(utsname)));
749 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
756 linux_utime(struct thread *td, struct linux_utime_args *args)
758 struct timeval tv[2], *tvp;
759 struct l_utimbuf lut;
763 LCONVPATHEXIST(td, args->fname, &fname);
767 printf(ARGS(utime, "%s, *"), fname);
771 if ((error = copyin(args->times, &lut, sizeof lut))) {
775 tv[0].tv_sec = lut.l_actime;
777 tv[1].tv_sec = lut.l_modtime;
783 error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
787 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
789 #define __WCLONE 0x80000000
792 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
794 int error, options, tmpstat;
798 printf(ARGS(waitpid, "%d, %p, %d"),
799 args->pid, (void *)args->status, args->options);
802 * this is necessary because the test in kern_wait doesnt
803 * work because we mess with the options here
805 if (args->options &~ (WUNTRACED|WNOHANG|WCONTINUED))
808 options = (args->options & (WNOHANG | WUNTRACED));
809 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
810 if (args->options & __WCLONE)
811 options |= WLINUXCLONE;
813 error = kern_wait(td, args->pid, &tmpstat, options, NULL);
819 if (WIFSIGNALED(tmpstat))
820 tmpstat = (tmpstat & 0xffffff80) |
821 BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
822 else if (WIFSTOPPED(tmpstat))
823 tmpstat = (tmpstat & 0xffff00ff) |
824 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
825 return copyout(&tmpstat, args->status, sizeof(int));
832 linux_wait4(struct thread *td, struct linux_wait4_args *args)
834 int error, options, tmpstat;
835 struct rusage ru, *rup;
840 printf(ARGS(wait4, "%d, %p, %d, %p"),
841 args->pid, (void *)args->status, args->options,
842 (void *)args->rusage);
845 options = (args->options & (WNOHANG | WUNTRACED));
846 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
847 if (args->options & __WCLONE)
848 options |= WLINUXCLONE;
850 if (args->rusage != NULL)
854 error = kern_wait(td, args->pid, &tmpstat, options, rup);
860 sigqueue_delete(&p->p_sigqueue, SIGCHLD);
865 if (WIFSIGNALED(tmpstat))
866 tmpstat = (tmpstat & 0xffffff80) |
867 BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
868 else if (WIFSTOPPED(tmpstat))
869 tmpstat = (tmpstat & 0xffff00ff) |
870 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
871 error = copyout(&tmpstat, args->status, sizeof(int));
873 if (args->rusage != NULL && error == 0)
874 error = copyout(&ru, args->rusage, sizeof(ru));
880 linux_mknod(struct thread *td, struct linux_mknod_args *args)
885 LCONVPATHCREAT(td, args->path, &path);
889 printf(ARGS(mknod, "%s, %d, %d"), path, args->mode, args->dev);
892 if (S_ISFIFO(args->mode))
893 error = kern_mkfifo(td, path, UIO_SYSSPACE, args->mode);
895 error = kern_mknod(td, path, UIO_SYSSPACE, args->mode,
902 * UGH! This is just about the dumbest idea I've ever heard!!
905 linux_personality(struct thread *td, struct linux_personality_args *args)
908 if (ldebug(personality))
909 printf(ARGS(personality, "%lu"), (unsigned long)args->per);
914 /* Yes Jim, it's still a Linux... */
915 td->td_retval[0] = 0;
920 l_timeval it_interval;
924 #define B2L_ITIMERVAL(bip, lip) \
925 (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec; \
926 (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec; \
927 (bip)->it_value.tv_sec = (lip)->it_value.tv_sec; \
928 (bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
931 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
934 struct l_itimerval ls;
935 struct itimerval aitv, oitv;
938 if (ldebug(setitimer))
939 printf(ARGS(setitimer, "%p, %p"),
940 (void *)uap->itv, (void *)uap->oitv);
943 if (uap->itv == NULL) {
944 uap->itv = uap->oitv;
945 return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
948 error = copyin(uap->itv, &ls, sizeof(ls));
951 B2L_ITIMERVAL(&aitv, &ls);
953 if (ldebug(setitimer)) {
954 printf("setitimer: value: sec: %jd, usec: %ld\n",
955 (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec);
956 printf("setitimer: interval: sec: %jd, usec: %ld\n",
957 (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec);
960 error = kern_setitimer(td, uap->which, &aitv, &oitv);
961 if (error != 0 || uap->oitv == NULL)
963 B2L_ITIMERVAL(&ls, &oitv);
965 return (copyout(&ls, uap->oitv, sizeof(ls)));
969 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
972 struct l_itimerval ls;
973 struct itimerval aitv;
976 if (ldebug(getitimer))
977 printf(ARGS(getitimer, "%p"), (void *)uap->itv);
979 error = kern_getitimer(td, uap->which, &aitv);
982 B2L_ITIMERVAL(&ls, &aitv);
983 return (copyout(&ls, uap->itv, sizeof(ls)));
987 linux_nice(struct thread *td, struct linux_nice_args *args)
989 struct setpriority_args bsd_args;
991 bsd_args.which = PRIO_PROCESS;
992 bsd_args.who = 0; /* current process */
993 bsd_args.prio = args->inc;
994 return setpriority(td, &bsd_args);
998 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
1000 struct ucred *newcred, *oldcred;
1001 l_gid_t linux_gidset[NGROUPS];
1006 ngrp = args->gidsetsize;
1007 if (ngrp < 0 || ngrp >= NGROUPS)
1009 error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
1015 oldcred = p->p_ucred;
1018 * cr_groups[0] holds egid. Setting the whole set from
1019 * the supplied set will cause egid to be changed too.
1020 * Keep cr_groups[0] unchanged to prevent that.
1023 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS,
1024 SUSER_ALLOWJAIL)) != 0) {
1030 crcopy(newcred, oldcred);
1032 newcred->cr_ngroups = ngrp + 1;
1034 bsd_gidset = newcred->cr_groups;
1037 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1042 newcred->cr_ngroups = 1;
1045 p->p_ucred = newcred;
1052 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1055 l_gid_t linux_gidset[NGROUPS];
1057 int bsd_gidsetsz, ngrp, error;
1059 cred = td->td_ucred;
1060 bsd_gidset = cred->cr_groups;
1061 bsd_gidsetsz = cred->cr_ngroups - 1;
1064 * cr_groups[0] holds egid. Returning the whole set
1065 * here will cause a duplicate. Exclude cr_groups[0]
1069 if ((ngrp = args->gidsetsize) == 0) {
1070 td->td_retval[0] = bsd_gidsetsz;
1074 if (ngrp < bsd_gidsetsz)
1078 while (ngrp < bsd_gidsetsz) {
1079 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1083 if ((error = copyout(linux_gidset, args->grouplist,
1084 ngrp * sizeof(l_gid_t))))
1087 td->td_retval[0] = ngrp;
1092 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1094 struct rlimit bsd_rlim;
1095 struct l_rlimit rlim;
1100 if (ldebug(setrlimit))
1101 printf(ARGS(setrlimit, "%d, %p"),
1102 args->resource, (void *)args->rlim);
1105 if (args->resource >= LINUX_RLIM_NLIMITS)
1108 which = linux_to_bsd_resource[args->resource];
1112 error = copyin(args->rlim, &rlim, sizeof(rlim));
1116 bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1117 bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1118 return (kern_setrlimit(td, which, &bsd_rlim));
1122 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1124 struct l_rlimit rlim;
1125 struct proc *p = td->td_proc;
1126 struct rlimit bsd_rlim;
1130 if (ldebug(old_getrlimit))
1131 printf(ARGS(old_getrlimit, "%d, %p"),
1132 args->resource, (void *)args->rlim);
1135 if (args->resource >= LINUX_RLIM_NLIMITS)
1138 which = linux_to_bsd_resource[args->resource];
1143 lim_rlimit(p, which, &bsd_rlim);
1146 #ifdef COMPAT_LINUX32
1147 rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1148 if (rlim.rlim_cur == UINT_MAX)
1149 rlim.rlim_cur = INT_MAX;
1150 rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1151 if (rlim.rlim_max == UINT_MAX)
1152 rlim.rlim_max = INT_MAX;
1154 rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1155 if (rlim.rlim_cur == ULONG_MAX)
1156 rlim.rlim_cur = LONG_MAX;
1157 rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1158 if (rlim.rlim_max == ULONG_MAX)
1159 rlim.rlim_max = LONG_MAX;
1161 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1165 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1167 struct l_rlimit rlim;
1168 struct proc *p = td->td_proc;
1169 struct rlimit bsd_rlim;
1173 if (ldebug(getrlimit))
1174 printf(ARGS(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 rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1190 rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1191 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1195 linux_sched_setscheduler(struct thread *td,
1196 struct linux_sched_setscheduler_args *args)
1198 struct sched_setscheduler_args bsd;
1201 if (ldebug(sched_setscheduler))
1202 printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1203 args->pid, args->policy, (const void *)args->param);
1206 switch (args->policy) {
1207 case LINUX_SCHED_OTHER:
1208 bsd.policy = SCHED_OTHER;
1210 case LINUX_SCHED_FIFO:
1211 bsd.policy = SCHED_FIFO;
1213 case LINUX_SCHED_RR:
1214 bsd.policy = SCHED_RR;
1220 bsd.pid = args->pid;
1221 bsd.param = (struct sched_param *)args->param;
1222 return sched_setscheduler(td, &bsd);
1226 linux_sched_getscheduler(struct thread *td,
1227 struct linux_sched_getscheduler_args *args)
1229 struct sched_getscheduler_args bsd;
1233 if (ldebug(sched_getscheduler))
1234 printf(ARGS(sched_getscheduler, "%d"), args->pid);
1237 bsd.pid = args->pid;
1238 error = sched_getscheduler(td, &bsd);
1240 switch (td->td_retval[0]) {
1242 td->td_retval[0] = LINUX_SCHED_OTHER;
1245 td->td_retval[0] = LINUX_SCHED_FIFO;
1248 td->td_retval[0] = LINUX_SCHED_RR;
1256 linux_sched_get_priority_max(struct thread *td,
1257 struct linux_sched_get_priority_max_args *args)
1259 struct sched_get_priority_max_args bsd;
1262 if (ldebug(sched_get_priority_max))
1263 printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1266 switch (args->policy) {
1267 case LINUX_SCHED_OTHER:
1268 bsd.policy = SCHED_OTHER;
1270 case LINUX_SCHED_FIFO:
1271 bsd.policy = SCHED_FIFO;
1273 case LINUX_SCHED_RR:
1274 bsd.policy = SCHED_RR;
1279 return sched_get_priority_max(td, &bsd);
1283 linux_sched_get_priority_min(struct thread *td,
1284 struct linux_sched_get_priority_min_args *args)
1286 struct sched_get_priority_min_args bsd;
1289 if (ldebug(sched_get_priority_min))
1290 printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1293 switch (args->policy) {
1294 case LINUX_SCHED_OTHER:
1295 bsd.policy = SCHED_OTHER;
1297 case LINUX_SCHED_FIFO:
1298 bsd.policy = SCHED_FIFO;
1300 case LINUX_SCHED_RR:
1301 bsd.policy = SCHED_RR;
1306 return sched_get_priority_min(td, &bsd);
1309 #define REBOOT_CAD_ON 0x89abcdef
1310 #define REBOOT_CAD_OFF 0
1311 #define REBOOT_HALT 0xcdef0123
1312 #define REBOOT_RESTART 0x01234567
1313 #define REBOOT_RESTART2 0xA1B2C3D4
1314 #define REBOOT_POWEROFF 0x4321FEDC
1315 #define REBOOT_MAGIC1 0xfee1dead
1316 #define REBOOT_MAGIC2 0x28121969
1317 #define REBOOT_MAGIC2A 0x05121996
1318 #define REBOOT_MAGIC2B 0x16041998
1321 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1323 struct reboot_args bsd_args;
1327 printf(ARGS(reboot, "0x%x"), args->cmd);
1330 if (args->magic1 != REBOOT_MAGIC1)
1333 switch (args->magic2) {
1335 case REBOOT_MAGIC2A:
1336 case REBOOT_MAGIC2B:
1342 switch (args->cmd) {
1344 case REBOOT_CAD_OFF:
1345 return (priv_check(td, PRIV_REBOOT));
1347 bsd_args.opt = RB_HALT;
1349 case REBOOT_RESTART:
1350 case REBOOT_RESTART2:
1353 case REBOOT_POWEROFF:
1354 bsd_args.opt = RB_POWEROFF;
1359 return reboot(td, &bsd_args);
1364 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1365 * td->td_retval[1] when COMPAT_43 is defined. This
1366 * globbers registers that are assumed to be preserved. The following
1367 * lightweight syscalls fixes this. See also linux_getgid16() and
1368 * linux_getuid16() in linux_uid16.c.
1370 * linux_getpid() - MP SAFE
1371 * linux_getgid() - MP SAFE
1372 * linux_getuid() - MP SAFE
1376 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1378 struct linux_emuldata *em;
1379 char osrel[LINUX_MAX_UTSNAME];
1381 linux_get_osrelease(td, osrel);
1382 if (strlen(osrel) >= 3 && osrel[2] == '6') {
1383 em = em_find(td->td_proc, EMUL_UNLOCKED);
1384 KASSERT(em != NULL, ("getpid: emuldata not found.\n"));
1385 td->td_retval[0] = em->shared->group_pid;
1386 EMUL_UNLOCK(&emul_lock);
1388 PROC_LOCK(td->td_proc);
1389 td->td_retval[0] = td->td_proc->p_pid;
1390 PROC_UNLOCK(td->td_proc);
1397 linux_gettid(struct thread *td, struct linux_gettid_args *args)
1401 printf(ARGS(gettid, ""));
1404 td->td_retval[0] = td->td_proc->p_pid;
1410 linux_getppid(struct thread *td, struct linux_getppid_args *args)
1412 struct linux_emuldata *em;
1413 struct proc *p, *pp;
1414 char osrel[LINUX_MAX_UTSNAME];
1416 linux_get_osrelease(td, osrel);
1417 if (strlen(osrel) >= 3 && osrel[2] != '6') {
1418 PROC_LOCK(td->td_proc);
1419 td->td_retval[0] = td->td_proc->p_pptr->p_pid;
1420 PROC_UNLOCK(td->td_proc);
1424 em = em_find(td->td_proc, EMUL_UNLOCKED);
1426 KASSERT(em != NULL, ("getppid: process emuldata not found.\n"));
1428 /* find the group leader */
1429 p = pfind(em->shared->group_pid);
1433 printf(LMSG("parent process not found.\n"));
1438 pp = p->p_pptr; /* switch to parent */
1442 /* if its also linux process */
1443 if (pp->p_sysent == &elf_linux_sysvec) {
1444 em = em_find(pp, EMUL_LOCKED);
1445 KASSERT(em != NULL, ("getppid: parent emuldata not found.\n"));
1447 td->td_retval[0] = em->shared->group_pid;
1449 td->td_retval[0] = pp->p_pid;
1451 EMUL_UNLOCK(&emul_lock);
1458 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1461 td->td_retval[0] = td->td_ucred->cr_rgid;
1466 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1469 td->td_retval[0] = td->td_ucred->cr_ruid;
1475 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1477 struct getsid_args bsd;
1478 bsd.pid = args->pid;
1479 return getsid(td, &bsd);
1483 linux_nosys(struct thread *td, struct nosys_args *ignore)
1490 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1492 struct getpriority_args bsd_args;
1495 bsd_args.which = args->which;
1496 bsd_args.who = args->who;
1497 error = getpriority(td, &bsd_args);
1498 td->td_retval[0] = 20 - td->td_retval[0];
1503 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1508 name[1] = KERN_HOSTNAME;
1509 return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1514 linux_exit_group(struct thread *td, struct linux_exit_group_args *args)
1516 struct linux_emuldata *em, *td_em, *tmp_em;
1518 char osrel[LINUX_MAX_UTSNAME];
1521 if (ldebug(exit_group))
1522 printf(ARGS(exit_group, "%i"), args->error_code);
1525 linux_get_osrelease(td, osrel);
1526 if (strlen(osrel) >= 3 && osrel[2] == '6') {
1527 td_em = em_find(td->td_proc, EMUL_UNLOCKED);
1529 KASSERT(td_em != NULL, ("exit_group: emuldata not found.\n"));
1531 EMUL_SHARED_RLOCK(&emul_shared_lock);
1532 LIST_FOREACH_SAFE(em, &td_em->shared->threads, threads, tmp_em) {
1533 if (em->pid == td_em->pid)
1536 sp = pfind(em->pid);
1537 psignal(sp, SIGKILL);
1540 printf(LMSG("linux_sys_exit_group: kill PID %d\n"), em->pid);
1544 EMUL_SHARED_RUNLOCK(&emul_shared_lock);
1545 EMUL_UNLOCK(&emul_lock);
1548 exit1(td, W_EXITCODE(args->error_code,0));
1554 linux_prctl(struct thread *td, struct linux_prctl_args *args)
1557 struct proc *p = td->td_proc;
1558 char comm[LINUX_MAX_COMM_LEN];
1559 struct linux_emuldata *em;
1563 printf(ARGS(prctl, "%d, %d, %d, %d, %d"), args->option, args->arg2,
1564 args->arg3, args->arg4, args->arg5);
1567 switch (args->option) {
1568 case LINUX_PR_SET_PDEATHSIG:
1569 if (!LINUX_SIG_VALID(args->arg2))
1571 em = em_find(p, EMUL_UNLOCKED);
1572 KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1573 em->pdeath_signal = args->arg2;
1574 EMUL_UNLOCK(&emul_lock);
1576 case LINUX_PR_GET_PDEATHSIG:
1577 em = em_find(p, EMUL_UNLOCKED);
1578 KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1579 error = copyout(&em->pdeath_signal, (void *)(register_t) args->arg2, sizeof(em->pdeath_signal));
1580 EMUL_UNLOCK(&emul_lock);
1582 case LINUX_PR_SET_NAME:
1583 comm[LINUX_MAX_COMM_LEN-1] = 0;
1584 error = copyin(comm, (void *)(register_t) args->arg2, LINUX_MAX_COMM_LEN-1);
1588 strcpy(p->p_comm, comm);
1591 case LINUX_PR_GET_NAME:
1592 error = copyout(&p->p_comm, (void *)(register_t) args->arg2, MAXCOMLEN+1);