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/racct.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>
79 #include <machine/../linux32/linux.h>
80 #include <machine/../linux32/linux32_proto.h>
82 #include <machine/../linux/linux.h>
83 #include <machine/../linux/linux_proto.h>
86 #include <compat/linux/linux_file.h>
87 #include <compat/linux/linux_mib.h>
88 #include <compat/linux/linux_signal.h>
89 #include <compat/linux/linux_util.h>
90 #include <compat/linux/linux_sysproto.h>
91 #include <compat/linux/linux_emul.h>
92 #include <compat/linux/linux_misc.h>
94 int stclohz; /* Statistics clock frequency */
96 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
97 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
98 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
99 RLIMIT_MEMLOCK, RLIMIT_AS
103 l_long uptime; /* Seconds since boot */
104 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */
105 #define LINUX_SYSINFO_LOADS_SCALE 65536
106 l_ulong totalram; /* Total usable main memory size */
107 l_ulong freeram; /* Available memory size */
108 l_ulong sharedram; /* Amount of shared memory */
109 l_ulong bufferram; /* Memory used by buffers */
110 l_ulong totalswap; /* Total swap space size */
111 l_ulong freeswap; /* swap space still available */
112 l_ushort procs; /* Number of current processes */
117 char _f[20-2*sizeof(l_long)-sizeof(l_int)]; /* padding */
120 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args)
122 struct l_sysinfo sysinfo;
130 sysinfo.uptime = ts.tv_sec;
132 /* Use the information from the mib to get our load averages */
133 for (i = 0; i < 3; i++)
134 sysinfo.loads[i] = averunnable.ldavg[i] *
135 LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale;
137 sysinfo.totalram = physmem * PAGE_SIZE;
138 sysinfo.freeram = sysinfo.totalram - cnt.v_wire_count * PAGE_SIZE;
140 sysinfo.sharedram = 0;
141 mtx_lock(&vm_object_list_mtx);
142 TAILQ_FOREACH(object, &vm_object_list, object_list)
143 if (object->shadow_count > 1)
144 sysinfo.sharedram += object->resident_page_count;
145 mtx_unlock(&vm_object_list_mtx);
147 sysinfo.sharedram *= PAGE_SIZE;
148 sysinfo.bufferram = 0;
150 swap_pager_status(&i, &j);
151 sysinfo.totalswap = i * PAGE_SIZE;
152 sysinfo.freeswap = (i - j) * PAGE_SIZE;
154 sysinfo.procs = nprocs;
156 /* The following are only present in newer Linux kernels. */
157 sysinfo.totalbig = 0;
159 sysinfo.mem_unit = 1;
161 return (copyout(&sysinfo, args->info, sizeof(sysinfo)));
165 linux_alarm(struct thread *td, struct linux_alarm_args *args)
167 struct itimerval it, old_it;
173 printf(ARGS(alarm, "%u"), args->secs);
181 it.it_value.tv_sec = (long) 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) && !sys_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;
232 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);
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_vnode_check_open(td->td_ucred, vp, VREAD);
307 error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL);
311 /* Pull in executable header into exec_map */
312 error = vm_mmap(exec_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 racct_set(td->td_proc, RACCT_DATA, a_out->a_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 error = vn_rdwr(UIO_READ, vp, (void *)vmaddr, file_offset,
406 a_out->a_text + a_out->a_data, UIO_USERSPACE, 0,
407 td->td_ucred, NOCRED, &aresid, td);
416 printf("uselib: Page aligned binary %lu\n", file_offset);
419 * for QMAGIC, a_entry is 20 bytes beyond the load address
420 * to skip the executable header
422 vmaddr = trunc_page(a_out->a_entry);
425 * Map it all into the process's space as a single
426 * copy-on-write "data" segment.
428 error = vm_mmap(&td->td_proc->p_vmspace->vm_map, &vmaddr,
429 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
430 MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset);
435 printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long *)vmaddr)[0],
436 ((long *)vmaddr)[1]);
439 /* Calculate BSS start address */
440 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
443 /* allocate some 'anon' space */
444 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
445 &vmaddr, bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
451 /* Unlock vnode if needed */
454 VFS_UNLOCK_GIANT(vfslocked);
457 /* Release the temporary mapping. */
459 kmem_free_wakeup(exec_map, (vm_offset_t)a_out, PAGE_SIZE);
464 #endif /* __i386__ */
467 linux_select(struct thread *td, struct linux_select_args *args)
470 struct timeval tv0, tv1, utv, *tvp;
475 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
476 (void *)args->readfds, (void *)args->writefds,
477 (void *)args->exceptfds, (void *)args->timeout);
481 * Store current time for computation of the amount of
485 if ((error = copyin(args->timeout, <v, sizeof(ltv))))
487 utv.tv_sec = ltv.tv_sec;
488 utv.tv_usec = ltv.tv_usec;
491 printf(LMSG("incoming timeout (%jd/%ld)"),
492 (intmax_t)utv.tv_sec, utv.tv_usec);
495 if (itimerfix(&utv)) {
497 * The timeval was invalid. Convert it to something
498 * valid that will act as it does under Linux.
500 utv.tv_sec += utv.tv_usec / 1000000;
501 utv.tv_usec %= 1000000;
502 if (utv.tv_usec < 0) {
504 utv.tv_usec += 1000000;
514 error = kern_select(td, args->nfds, args->readfds, args->writefds,
515 args->exceptfds, tvp, sizeof(l_int) * 8);
519 printf(LMSG("real select returns %d"), error);
525 if (td->td_retval[0]) {
527 * Compute how much time was left of the timeout,
528 * by subtracting the current time and the time
529 * before we started the call, and subtracting
530 * that result from the user-supplied value.
533 timevalsub(&tv1, &tv0);
534 timevalsub(&utv, &tv1);
541 printf(LMSG("outgoing timeout (%jd/%ld)"),
542 (intmax_t)utv.tv_sec, utv.tv_usec);
544 ltv.tv_sec = utv.tv_sec;
545 ltv.tv_usec = utv.tv_usec;
546 if ((error = copyout(<v, args->timeout, sizeof(ltv))))
553 printf(LMSG("select_out -> %d"), error);
559 linux_mremap(struct thread *td, struct linux_mremap_args *args)
561 struct munmap_args /* {
569 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
570 (void *)(uintptr_t)args->addr,
571 (unsigned long)args->old_len,
572 (unsigned long)args->new_len,
573 (unsigned long)args->flags);
576 if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) {
577 td->td_retval[0] = 0;
582 * Check for the page alignment.
583 * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK.
585 if (args->addr & PAGE_MASK) {
586 td->td_retval[0] = 0;
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 = sys_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 (sys_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 {
648 l_clock_t tms_cutime;
649 l_clock_t tms_cstime;
654 * Glibc versions prior to 2.2.1 always use hard-coded CLK_TCK value.
655 * Since 2.2.1 Glibc uses value exported from kernel via AT_CLKTCK
656 * auxiliary vector entry.
660 #define CONVOTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
661 #define CONVNTCK(r) (r.tv_sec * stclohz + r.tv_usec / (1000000 / stclohz))
663 #define CONVTCK(r) (linux_kernver(td) >= LINUX_KERNVER_2004000 ? \
664 CONVNTCK(r) : CONVOTCK(r))
667 linux_times(struct thread *td, struct linux_times_args *args)
669 struct timeval tv, utime, stime, cutime, cstime;
670 struct l_times_argv tms;
676 printf(ARGS(times, "*"));
679 if (args->buf != NULL) {
683 calcru(p, &utime, &stime);
685 calccru(p, &cutime, &cstime);
688 tms.tms_utime = CONVTCK(utime);
689 tms.tms_stime = CONVTCK(stime);
691 tms.tms_cutime = CONVTCK(cutime);
692 tms.tms_cstime = CONVTCK(cstime);
694 if ((error = copyout(&tms, args->buf, sizeof(tms))))
699 td->td_retval[0] = (int)CONVTCK(tv);
704 linux_newuname(struct thread *td, struct linux_newuname_args *args)
706 struct l_new_utsname utsname;
707 char osname[LINUX_MAX_UTSNAME];
708 char osrelease[LINUX_MAX_UTSNAME];
712 if (ldebug(newuname))
713 printf(ARGS(newuname, "*"));
716 linux_get_osname(td, osname);
717 linux_get_osrelease(td, osrelease);
719 bzero(&utsname, sizeof(utsname));
720 strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
721 getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
722 getcreddomainname(td->td_ucred, utsname.domainname, LINUX_MAX_UTSNAME);
723 strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
724 strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
725 for (p = utsname.version; *p != '\0'; ++p)
730 strlcpy(utsname.machine, linux_platform, LINUX_MAX_UTSNAME);
732 return (copyout(&utsname, args->buf, sizeof(utsname)));
735 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
742 linux_utime(struct thread *td, struct linux_utime_args *args)
744 struct timeval tv[2], *tvp;
745 struct l_utimbuf lut;
749 LCONVPATHEXIST(td, args->fname, &fname);
753 printf(ARGS(utime, "%s, *"), fname);
757 if ((error = copyin(args->times, &lut, sizeof lut))) {
761 tv[0].tv_sec = lut.l_actime;
763 tv[1].tv_sec = lut.l_modtime;
769 error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
775 linux_utimes(struct thread *td, struct linux_utimes_args *args)
778 struct timeval tv[2], *tvp = NULL;
782 LCONVPATHEXIST(td, args->fname, &fname);
786 printf(ARGS(utimes, "%s, *"), fname);
789 if (args->tptr != NULL) {
790 if ((error = copyin(args->tptr, ltv, sizeof ltv))) {
794 tv[0].tv_sec = ltv[0].tv_sec;
795 tv[0].tv_usec = ltv[0].tv_usec;
796 tv[1].tv_sec = ltv[1].tv_sec;
797 tv[1].tv_usec = ltv[1].tv_usec;
801 error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
807 linux_futimesat(struct thread *td, struct linux_futimesat_args *args)
810 struct timeval tv[2], *tvp = NULL;
814 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
815 LCONVPATHEXIST_AT(td, args->filename, &fname, dfd);
818 if (ldebug(futimesat))
819 printf(ARGS(futimesat, "%s, *"), fname);
822 if (args->utimes != NULL) {
823 if ((error = copyin(args->utimes, ltv, sizeof ltv))) {
827 tv[0].tv_sec = ltv[0].tv_sec;
828 tv[0].tv_usec = ltv[0].tv_usec;
829 tv[1].tv_sec = ltv[1].tv_sec;
830 tv[1].tv_usec = ltv[1].tv_usec;
834 error = kern_utimesat(td, dfd, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
838 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
841 linux_common_wait(struct thread *td, int pid, int *status,
842 int options, struct rusage *ru)
846 error = kern_wait(td, pid, &tmpstat, options, ru);
852 if (WIFSIGNALED(tmpstat))
853 tmpstat = (tmpstat & 0xffffff80) |
854 BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
855 else if (WIFSTOPPED(tmpstat))
856 tmpstat = (tmpstat & 0xffff00ff) |
857 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
858 error = copyout(&tmpstat, status, sizeof(int));
865 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
871 printf(ARGS(waitpid, "%d, %p, %d"),
872 args->pid, (void *)args->status, args->options);
875 * this is necessary because the test in kern_wait doesn't work
876 * because we mess with the options here
878 if (args->options & ~(WUNTRACED | WNOHANG | WCONTINUED | __WCLONE))
881 options = (args->options & (WNOHANG | WUNTRACED));
882 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
883 if (args->options & __WCLONE)
884 options |= WLINUXCLONE;
886 return (linux_common_wait(td, args->pid, args->status, options, NULL));
891 linux_mknod(struct thread *td, struct linux_mknod_args *args)
896 LCONVPATHCREAT(td, args->path, &path);
900 printf(ARGS(mknod, "%s, %d, %d"), path, args->mode, args->dev);
903 switch (args->mode & S_IFMT) {
906 error = kern_mkfifo(td, path, UIO_SYSSPACE, args->mode);
911 error = kern_mknod(td, path, UIO_SYSSPACE, args->mode,
920 args->mode |= S_IFREG;
923 error = kern_open(td, path, UIO_SYSSPACE,
924 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
926 kern_close(td, td->td_retval[0]);
938 linux_mknodat(struct thread *td, struct linux_mknodat_args *args)
943 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
944 LCONVPATHCREAT_AT(td, args->filename, &path, dfd);
948 printf(ARGS(mknodat, "%s, %d, %d"), path, args->mode, args->dev);
951 switch (args->mode & S_IFMT) {
954 error = kern_mkfifoat(td, dfd, path, UIO_SYSSPACE, args->mode);
959 error = kern_mknodat(td, dfd, path, UIO_SYSSPACE, args->mode,
968 args->mode |= S_IFREG;
971 error = kern_openat(td, dfd, path, UIO_SYSSPACE,
972 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
974 kern_close(td, td->td_retval[0]);
986 * UGH! This is just about the dumbest idea I've ever heard!!
989 linux_personality(struct thread *td, struct linux_personality_args *args)
992 if (ldebug(personality))
993 printf(ARGS(personality, "%lu"), (unsigned long)args->per);
998 /* Yes Jim, it's still a Linux... */
999 td->td_retval[0] = 0;
1003 struct l_itimerval {
1004 l_timeval it_interval;
1008 #define B2L_ITIMERVAL(bip, lip) \
1009 (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec; \
1010 (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec; \
1011 (bip)->it_value.tv_sec = (lip)->it_value.tv_sec; \
1012 (bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
1015 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
1018 struct l_itimerval ls;
1019 struct itimerval aitv, oitv;
1022 if (ldebug(setitimer))
1023 printf(ARGS(setitimer, "%p, %p"),
1024 (void *)uap->itv, (void *)uap->oitv);
1027 if (uap->itv == NULL) {
1028 uap->itv = uap->oitv;
1029 return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
1032 error = copyin(uap->itv, &ls, sizeof(ls));
1035 B2L_ITIMERVAL(&aitv, &ls);
1037 if (ldebug(setitimer)) {
1038 printf("setitimer: value: sec: %jd, usec: %ld\n",
1039 (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec);
1040 printf("setitimer: interval: sec: %jd, usec: %ld\n",
1041 (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec);
1044 error = kern_setitimer(td, uap->which, &aitv, &oitv);
1045 if (error != 0 || uap->oitv == NULL)
1047 B2L_ITIMERVAL(&ls, &oitv);
1049 return (copyout(&ls, uap->oitv, sizeof(ls)));
1053 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
1056 struct l_itimerval ls;
1057 struct itimerval aitv;
1060 if (ldebug(getitimer))
1061 printf(ARGS(getitimer, "%p"), (void *)uap->itv);
1063 error = kern_getitimer(td, uap->which, &aitv);
1066 B2L_ITIMERVAL(&ls, &aitv);
1067 return (copyout(&ls, uap->itv, sizeof(ls)));
1071 linux_nice(struct thread *td, struct linux_nice_args *args)
1073 struct setpriority_args bsd_args;
1075 bsd_args.which = PRIO_PROCESS;
1076 bsd_args.who = 0; /* current process */
1077 bsd_args.prio = args->inc;
1078 return (sys_setpriority(td, &bsd_args));
1082 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
1084 struct ucred *newcred, *oldcred;
1085 l_gid_t *linux_gidset;
1090 ngrp = args->gidsetsize;
1091 if (ngrp < 0 || ngrp >= ngroups_max + 1)
1093 linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_TEMP, M_WAITOK);
1094 error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
1100 oldcred = crcopysafe(p, newcred);
1103 * cr_groups[0] holds egid. Setting the whole set from
1104 * the supplied set will cause egid to be changed too.
1105 * Keep cr_groups[0] unchanged to prevent that.
1108 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0) {
1115 newcred->cr_ngroups = ngrp + 1;
1117 bsd_gidset = newcred->cr_groups;
1120 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1124 newcred->cr_ngroups = 1;
1127 p->p_ucred = newcred;
1132 free(linux_gidset, M_TEMP);
1137 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1140 l_gid_t *linux_gidset;
1142 int bsd_gidsetsz, ngrp, error;
1144 cred = td->td_ucred;
1145 bsd_gidset = cred->cr_groups;
1146 bsd_gidsetsz = cred->cr_ngroups - 1;
1149 * cr_groups[0] holds egid. Returning the whole set
1150 * here will cause a duplicate. Exclude cr_groups[0]
1154 if ((ngrp = args->gidsetsize) == 0) {
1155 td->td_retval[0] = bsd_gidsetsz;
1159 if (ngrp < bsd_gidsetsz)
1163 linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset),
1165 while (ngrp < bsd_gidsetsz) {
1166 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1170 error = copyout(linux_gidset, args->grouplist, ngrp * sizeof(l_gid_t));
1171 free(linux_gidset, M_TEMP);
1175 td->td_retval[0] = ngrp;
1180 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1182 struct rlimit bsd_rlim;
1183 struct l_rlimit rlim;
1188 if (ldebug(setrlimit))
1189 printf(ARGS(setrlimit, "%d, %p"),
1190 args->resource, (void *)args->rlim);
1193 if (args->resource >= LINUX_RLIM_NLIMITS)
1196 which = linux_to_bsd_resource[args->resource];
1200 error = copyin(args->rlim, &rlim, sizeof(rlim));
1204 bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1205 bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1206 return (kern_setrlimit(td, which, &bsd_rlim));
1210 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1212 struct l_rlimit rlim;
1213 struct proc *p = td->td_proc;
1214 struct rlimit bsd_rlim;
1218 if (ldebug(old_getrlimit))
1219 printf(ARGS(old_getrlimit, "%d, %p"),
1220 args->resource, (void *)args->rlim);
1223 if (args->resource >= LINUX_RLIM_NLIMITS)
1226 which = linux_to_bsd_resource[args->resource];
1231 lim_rlimit(p, which, &bsd_rlim);
1234 #ifdef COMPAT_LINUX32
1235 rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1236 if (rlim.rlim_cur == UINT_MAX)
1237 rlim.rlim_cur = INT_MAX;
1238 rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1239 if (rlim.rlim_max == UINT_MAX)
1240 rlim.rlim_max = INT_MAX;
1242 rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1243 if (rlim.rlim_cur == ULONG_MAX)
1244 rlim.rlim_cur = LONG_MAX;
1245 rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1246 if (rlim.rlim_max == ULONG_MAX)
1247 rlim.rlim_max = LONG_MAX;
1249 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1253 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1255 struct l_rlimit rlim;
1256 struct proc *p = td->td_proc;
1257 struct rlimit bsd_rlim;
1261 if (ldebug(getrlimit))
1262 printf(ARGS(getrlimit, "%d, %p"),
1263 args->resource, (void *)args->rlim);
1266 if (args->resource >= LINUX_RLIM_NLIMITS)
1269 which = linux_to_bsd_resource[args->resource];
1274 lim_rlimit(p, which, &bsd_rlim);
1277 rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1278 rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1279 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1283 linux_sched_setscheduler(struct thread *td,
1284 struct linux_sched_setscheduler_args *args)
1286 struct sched_setscheduler_args bsd;
1289 if (ldebug(sched_setscheduler))
1290 printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1291 args->pid, args->policy, (const void *)args->param);
1294 switch (args->policy) {
1295 case LINUX_SCHED_OTHER:
1296 bsd.policy = SCHED_OTHER;
1298 case LINUX_SCHED_FIFO:
1299 bsd.policy = SCHED_FIFO;
1301 case LINUX_SCHED_RR:
1302 bsd.policy = SCHED_RR;
1308 bsd.pid = args->pid;
1309 bsd.param = (struct sched_param *)args->param;
1310 return (sys_sched_setscheduler(td, &bsd));
1314 linux_sched_getscheduler(struct thread *td,
1315 struct linux_sched_getscheduler_args *args)
1317 struct sched_getscheduler_args bsd;
1321 if (ldebug(sched_getscheduler))
1322 printf(ARGS(sched_getscheduler, "%d"), args->pid);
1325 bsd.pid = args->pid;
1326 error = sys_sched_getscheduler(td, &bsd);
1328 switch (td->td_retval[0]) {
1330 td->td_retval[0] = LINUX_SCHED_OTHER;
1333 td->td_retval[0] = LINUX_SCHED_FIFO;
1336 td->td_retval[0] = LINUX_SCHED_RR;
1344 linux_sched_get_priority_max(struct thread *td,
1345 struct linux_sched_get_priority_max_args *args)
1347 struct sched_get_priority_max_args bsd;
1350 if (ldebug(sched_get_priority_max))
1351 printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1354 switch (args->policy) {
1355 case LINUX_SCHED_OTHER:
1356 bsd.policy = SCHED_OTHER;
1358 case LINUX_SCHED_FIFO:
1359 bsd.policy = SCHED_FIFO;
1361 case LINUX_SCHED_RR:
1362 bsd.policy = SCHED_RR;
1367 return (sys_sched_get_priority_max(td, &bsd));
1371 linux_sched_get_priority_min(struct thread *td,
1372 struct linux_sched_get_priority_min_args *args)
1374 struct sched_get_priority_min_args bsd;
1377 if (ldebug(sched_get_priority_min))
1378 printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1381 switch (args->policy) {
1382 case LINUX_SCHED_OTHER:
1383 bsd.policy = SCHED_OTHER;
1385 case LINUX_SCHED_FIFO:
1386 bsd.policy = SCHED_FIFO;
1388 case LINUX_SCHED_RR:
1389 bsd.policy = SCHED_RR;
1394 return (sys_sched_get_priority_min(td, &bsd));
1397 #define REBOOT_CAD_ON 0x89abcdef
1398 #define REBOOT_CAD_OFF 0
1399 #define REBOOT_HALT 0xcdef0123
1400 #define REBOOT_RESTART 0x01234567
1401 #define REBOOT_RESTART2 0xA1B2C3D4
1402 #define REBOOT_POWEROFF 0x4321FEDC
1403 #define REBOOT_MAGIC1 0xfee1dead
1404 #define REBOOT_MAGIC2 0x28121969
1405 #define REBOOT_MAGIC2A 0x05121996
1406 #define REBOOT_MAGIC2B 0x16041998
1409 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1411 struct reboot_args bsd_args;
1415 printf(ARGS(reboot, "0x%x"), args->cmd);
1418 if (args->magic1 != REBOOT_MAGIC1)
1421 switch (args->magic2) {
1423 case REBOOT_MAGIC2A:
1424 case REBOOT_MAGIC2B:
1430 switch (args->cmd) {
1432 case REBOOT_CAD_OFF:
1433 return (priv_check(td, PRIV_REBOOT));
1435 bsd_args.opt = RB_HALT;
1437 case REBOOT_RESTART:
1438 case REBOOT_RESTART2:
1441 case REBOOT_POWEROFF:
1442 bsd_args.opt = RB_POWEROFF;
1447 return (sys_reboot(td, &bsd_args));
1452 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1453 * td->td_retval[1] when COMPAT_43 is defined. This clobbers registers that
1454 * are assumed to be preserved. The following lightweight syscalls fixes
1455 * this. See also linux_getgid16() and linux_getuid16() in linux_uid16.c
1457 * linux_getpid() - MP SAFE
1458 * linux_getgid() - MP SAFE
1459 * linux_getuid() - MP SAFE
1463 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1465 struct linux_emuldata *em;
1469 printf(ARGS(getpid, ""));
1472 if (linux_use26(td)) {
1473 em = em_find(td->td_proc, EMUL_DONTLOCK);
1474 KASSERT(em != NULL, ("getpid: emuldata not found.\n"));
1475 td->td_retval[0] = em->shared->group_pid;
1477 td->td_retval[0] = td->td_proc->p_pid;
1484 linux_gettid(struct thread *td, struct linux_gettid_args *args)
1489 printf(ARGS(gettid, ""));
1492 td->td_retval[0] = td->td_proc->p_pid;
1498 linux_getppid(struct thread *td, struct linux_getppid_args *args)
1500 struct linux_emuldata *em;
1501 struct proc *p, *pp;
1504 if (ldebug(getppid))
1505 printf(ARGS(getppid, ""));
1508 if (!linux_use26(td)) {
1509 PROC_LOCK(td->td_proc);
1510 td->td_retval[0] = td->td_proc->p_pptr->p_pid;
1511 PROC_UNLOCK(td->td_proc);
1515 em = em_find(td->td_proc, EMUL_DONTLOCK);
1517 KASSERT(em != NULL, ("getppid: process emuldata not found.\n"));
1519 /* find the group leader */
1520 p = pfind(em->shared->group_pid);
1524 printf(LMSG("parent process not found.\n"));
1529 pp = p->p_pptr; /* switch to parent */
1533 /* if its also linux process */
1534 if (pp->p_sysent == &elf_linux_sysvec) {
1535 em = em_find(pp, EMUL_DONTLOCK);
1536 KASSERT(em != NULL, ("getppid: parent emuldata not found.\n"));
1538 td->td_retval[0] = em->shared->group_pid;
1540 td->td_retval[0] = pp->p_pid;
1548 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1553 printf(ARGS(getgid, ""));
1556 td->td_retval[0] = td->td_ucred->cr_rgid;
1561 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1566 printf(ARGS(getuid, ""));
1569 td->td_retval[0] = td->td_ucred->cr_ruid;
1575 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1577 struct getsid_args bsd;
1581 printf(ARGS(getsid, "%i"), args->pid);
1584 bsd.pid = args->pid;
1585 return (sys_getsid(td, &bsd));
1589 linux_nosys(struct thread *td, struct nosys_args *ignore)
1596 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1598 struct getpriority_args bsd_args;
1602 if (ldebug(getpriority))
1603 printf(ARGS(getpriority, "%i, %i"), args->which, args->who);
1606 bsd_args.which = args->which;
1607 bsd_args.who = args->who;
1608 error = sys_getpriority(td, &bsd_args);
1609 td->td_retval[0] = 20 - td->td_retval[0];
1614 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1619 if (ldebug(sethostname))
1620 printf(ARGS(sethostname, "*, %i"), args->len);
1624 name[1] = KERN_HOSTNAME;
1625 return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1630 linux_setdomainname(struct thread *td, struct linux_setdomainname_args *args)
1635 if (ldebug(setdomainname))
1636 printf(ARGS(setdomainname, "*, %i"), args->len);
1640 name[1] = KERN_NISDOMAINNAME;
1641 return (userland_sysctl(td, name, 2, 0, 0, 0, args->name,
1646 linux_exit_group(struct thread *td, struct linux_exit_group_args *args)
1648 struct linux_emuldata *em;
1651 if (ldebug(exit_group))
1652 printf(ARGS(exit_group, "%i"), args->error_code);
1655 em = em_find(td->td_proc, EMUL_DONTLOCK);
1656 if (em->shared->refs > 1) {
1657 EMUL_SHARED_WLOCK(&emul_shared_lock);
1658 em->shared->flags |= EMUL_SHARED_HASXSTAT;
1659 em->shared->xstat = W_EXITCODE(args->error_code, 0);
1660 EMUL_SHARED_WUNLOCK(&emul_shared_lock);
1661 if (linux_use26(td))
1662 linux_kill_threads(td, SIGKILL);
1666 * XXX: we should send a signal to the parent if
1667 * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?)
1668 * as it doesnt occur often.
1670 exit1(td, W_EXITCODE(args->error_code, 0));
1675 #define _LINUX_CAPABILITY_VERSION 0x19980330
1677 struct l_user_cap_header {
1682 struct l_user_cap_data {
1689 linux_capget(struct thread *td, struct linux_capget_args *args)
1691 struct l_user_cap_header luch;
1692 struct l_user_cap_data lucd;
1695 if (args->hdrp == NULL)
1698 error = copyin(args->hdrp, &luch, sizeof(luch));
1702 if (luch.version != _LINUX_CAPABILITY_VERSION) {
1703 luch.version = _LINUX_CAPABILITY_VERSION;
1704 error = copyout(&luch, args->hdrp, sizeof(luch));
1715 * The current implementation doesn't support setting
1716 * a capability (it's essentially a stub) so indicate
1717 * that no capabilities are currently set or available
1720 bzero (&lucd, sizeof(lucd));
1721 error = copyout(&lucd, args->datap, sizeof(lucd));
1728 linux_capset(struct thread *td, struct linux_capset_args *args)
1730 struct l_user_cap_header luch;
1731 struct l_user_cap_data lucd;
1734 if (args->hdrp == NULL || args->datap == NULL)
1737 error = copyin(args->hdrp, &luch, sizeof(luch));
1741 if (luch.version != _LINUX_CAPABILITY_VERSION) {
1742 luch.version = _LINUX_CAPABILITY_VERSION;
1743 error = copyout(&luch, args->hdrp, sizeof(luch));
1752 error = copyin(args->datap, &lucd, sizeof(lucd));
1756 /* We currently don't support setting any capabilities. */
1757 if (lucd.effective || lucd.permitted || lucd.inheritable) {
1759 "capset effective=0x%x, permitted=0x%x, "
1760 "inheritable=0x%x is not implemented",
1761 (int)lucd.effective, (int)lucd.permitted,
1762 (int)lucd.inheritable);
1770 linux_prctl(struct thread *td, struct linux_prctl_args *args)
1772 int error = 0, max_size;
1773 struct proc *p = td->td_proc;
1774 char comm[LINUX_MAX_COMM_LEN];
1775 struct linux_emuldata *em;
1780 printf(ARGS(prctl, "%d, %d, %d, %d, %d"), args->option,
1781 args->arg2, args->arg3, args->arg4, args->arg5);
1784 switch (args->option) {
1785 case LINUX_PR_SET_PDEATHSIG:
1786 if (!LINUX_SIG_VALID(args->arg2))
1788 em = em_find(p, EMUL_DOLOCK);
1789 KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1790 em->pdeath_signal = args->arg2;
1791 EMUL_UNLOCK(&emul_lock);
1793 case LINUX_PR_GET_PDEATHSIG:
1794 em = em_find(p, EMUL_DOLOCK);
1795 KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1796 pdeath_signal = em->pdeath_signal;
1797 EMUL_UNLOCK(&emul_lock);
1798 error = copyout(&pdeath_signal,
1799 (void *)(register_t)args->arg2,
1800 sizeof(pdeath_signal));
1802 case LINUX_PR_GET_KEEPCAPS:
1804 * Indicate that we always clear the effective and
1805 * permitted capability sets when the user id becomes
1806 * non-zero (actually the capability sets are simply
1807 * always zero in the current implementation).
1809 td->td_retval[0] = 0;
1811 case LINUX_PR_SET_KEEPCAPS:
1813 * Ignore requests to keep the effective and permitted
1814 * capability sets when the user id becomes non-zero.
1817 case LINUX_PR_SET_NAME:
1819 * To be on the safe side we need to make sure to not
1820 * overflow the size a linux program expects. We already
1821 * do this here in the copyin, so that we don't need to
1824 max_size = MIN(sizeof(comm), sizeof(p->p_comm));
1825 error = copyinstr((void *)(register_t)args->arg2, comm,
1828 /* Linux silently truncates the name if it is too long. */
1829 if (error == ENAMETOOLONG) {
1831 * XXX: copyinstr() isn't documented to populate the
1832 * array completely, so do a copyin() to be on the
1833 * safe side. This should be changed in case
1834 * copyinstr() is changed to guarantee this.
1836 error = copyin((void *)(register_t)args->arg2, comm,
1838 comm[max_size - 1] = '\0';
1844 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
1847 case LINUX_PR_GET_NAME:
1849 strlcpy(comm, p->p_comm, sizeof(comm));
1851 error = copyout(comm, (void *)(register_t)args->arg2,
1863 * Get affinity of a process.
1866 linux_sched_getaffinity(struct thread *td,
1867 struct linux_sched_getaffinity_args *args)
1870 struct cpuset_getaffinity_args cga;
1873 if (ldebug(sched_getaffinity))
1874 printf(ARGS(sched_getaffinity, "%d, %d, *"), args->pid,
1877 if (args->len < sizeof(cpuset_t))
1880 cga.level = CPU_LEVEL_WHICH;
1881 cga.which = CPU_WHICH_PID;
1883 cga.cpusetsize = sizeof(cpuset_t);
1884 cga.mask = (cpuset_t *) args->user_mask_ptr;
1886 if ((error = sys_cpuset_getaffinity(td, &cga)) == 0)
1887 td->td_retval[0] = sizeof(cpuset_t);
1893 * Set affinity of a process.
1896 linux_sched_setaffinity(struct thread *td,
1897 struct linux_sched_setaffinity_args *args)
1899 struct cpuset_setaffinity_args csa;
1902 if (ldebug(sched_setaffinity))
1903 printf(ARGS(sched_setaffinity, "%d, %d, *"), args->pid,
1906 if (args->len < sizeof(cpuset_t))
1909 csa.level = CPU_LEVEL_WHICH;
1910 csa.which = CPU_WHICH_PID;
1912 csa.cpusetsize = sizeof(cpuset_t);
1913 csa.mask = (cpuset_t *) args->user_mask_ptr;
1915 return (sys_cpuset_setaffinity(td, &csa));