2 * Copyright (c) 2002 Doug Rabson
3 * Copyright (c) 1994-1995 Søren Schmidt
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer
11 * in this position and unchanged.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include "opt_compat.h"
35 #include <sys/param.h>
36 #include <sys/blist.h>
37 #include <sys/fcntl.h>
39 #include <sys/imgact_aout.h>
42 #include <sys/kernel.h>
43 #include <sys/limits.h>
45 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/mutex.h>
49 #include <sys/namei.h>
52 #include <sys/reboot.h>
53 #include <sys/resourcevar.h>
54 #include <sys/sched.h>
55 #include <sys/signalvar.h>
57 #include <sys/syscallsubr.h>
58 #include <sys/sysctl.h>
59 #include <sys/sysproto.h>
60 #include <sys/systm.h>
62 #include <sys/vmmeter.h>
63 #include <sys/vnode.h>
65 #include <sys/cpuset.h>
67 #include <security/mac/mac_framework.h>
71 #include <vm/vm_kern.h>
72 #include <vm/vm_map.h>
73 #include <vm/vm_extern.h>
74 #include <vm/vm_object.h>
75 #include <vm/swap_pager.h>
78 #include <machine/../linux32/linux.h>
79 #include <machine/../linux32/linux32_proto.h>
81 #include <machine/../linux/linux.h>
82 #include <machine/../linux/linux_proto.h>
85 #include <compat/linux/linux_file.h>
86 #include <compat/linux/linux_mib.h>
87 #include <compat/linux/linux_signal.h>
88 #include <compat/linux/linux_util.h>
89 #include <compat/linux/linux_sysproto.h>
90 #include <compat/linux/linux_emul.h>
91 #include <compat/linux/linux_misc.h>
93 int stclohz; /* Statistics clock frequency */
95 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
96 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
97 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
98 RLIMIT_MEMLOCK, RLIMIT_AS
102 l_long uptime; /* Seconds since boot */
103 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */
104 #define LINUX_SYSINFO_LOADS_SCALE 65536
105 l_ulong totalram; /* Total usable main memory size */
106 l_ulong freeram; /* Available memory size */
107 l_ulong sharedram; /* Amount of shared memory */
108 l_ulong bufferram; /* Memory used by buffers */
109 l_ulong totalswap; /* Total swap space size */
110 l_ulong freeswap; /* swap space still available */
111 l_ushort procs; /* Number of current processes */
116 char _f[20-2*sizeof(l_long)-sizeof(l_int)]; /* padding */
119 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args)
121 struct l_sysinfo sysinfo;
129 sysinfo.uptime = ts.tv_sec;
131 /* Use the information from the mib to get our load averages */
132 for (i = 0; i < 3; i++)
133 sysinfo.loads[i] = averunnable.ldavg[i] *
134 LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale;
136 sysinfo.totalram = physmem * PAGE_SIZE;
137 sysinfo.freeram = sysinfo.totalram - cnt.v_wire_count * PAGE_SIZE;
139 sysinfo.sharedram = 0;
140 mtx_lock(&vm_object_list_mtx);
141 TAILQ_FOREACH(object, &vm_object_list, object_list)
142 if (object->shadow_count > 1)
143 sysinfo.sharedram += object->resident_page_count;
144 mtx_unlock(&vm_object_list_mtx);
146 sysinfo.sharedram *= PAGE_SIZE;
147 sysinfo.bufferram = 0;
149 swap_pager_status(&i, &j);
150 sysinfo.totalswap = i * PAGE_SIZE;
151 sysinfo.freeswap = (i - j) * PAGE_SIZE;
153 sysinfo.procs = nprocs;
155 /* The following are only present in newer Linux kernels. */
156 sysinfo.totalbig = 0;
158 sysinfo.mem_unit = 1;
160 return (copyout(&sysinfo, args->info, sizeof(sysinfo)));
164 linux_alarm(struct thread *td, struct linux_alarm_args *args)
166 struct itimerval it, old_it;
172 printf(ARGS(alarm, "%u"), args->secs);
180 it.it_value.tv_sec = (long) secs;
181 it.it_value.tv_usec = 0;
182 it.it_interval.tv_sec = 0;
183 it.it_interval.tv_usec = 0;
184 error = kern_setitimer(td, ITIMER_REAL, &it, &old_it);
187 if (timevalisset(&old_it.it_value)) {
188 if (old_it.it_value.tv_usec != 0)
189 old_it.it_value.tv_sec++;
190 td->td_retval[0] = old_it.it_value.tv_sec;
196 linux_brk(struct thread *td, struct linux_brk_args *args)
198 struct vmspace *vm = td->td_proc->p_vmspace;
199 vm_offset_t new, old;
200 struct obreak_args /* {
206 printf(ARGS(brk, "%p"), (void *)(uintptr_t)args->dsend);
208 old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
209 new = (vm_offset_t)args->dsend;
210 tmp.nsize = (char *)new;
211 if (((caddr_t)new > vm->vm_daddr) && !obreak(td, &tmp))
212 td->td_retval[0] = (long)new;
214 td->td_retval[0] = (long)old;
219 #if defined(__i386__)
220 /* XXX: what about amd64/linux32? */
223 linux_uselib(struct thread *td, struct linux_uselib_args *args)
230 unsigned long file_offset;
232 unsigned long bss_size;
235 int locked, vfslocked;
237 LCONVPATHEXIST(td, args->library, &library);
241 printf(ARGS(uselib, "%s"), library);
249 NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
250 UIO_SYSSPACE, library, td);
257 vfslocked = NDHASGIANT(&ni);
258 NDFREE(&ni, NDF_ONLY_PNBUF);
261 * From here on down, we have a locked vnode that must be unlocked.
262 * XXX: The code below largely duplicates exec_check_permissions().
267 if (vp->v_writecount) {
273 error = VOP_GETATTR(vp, &attr, td->td_ucred);
277 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
278 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
279 /* EACCESS is what exec(2) returns. */
285 if (attr.va_size == 0) {
290 /* Can we access it? */
291 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
296 * XXX: This should use vn_open() so that it is properly authorized,
297 * and to reduce code redundancy all over the place here.
298 * XXX: Not really, it duplicates far more of exec_check_permissions()
302 error = mac_vnode_check_open(td->td_ucred, vp, VREAD);
306 error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL);
310 /* Pull in executable header into kernel_map */
311 error = vm_mmap(kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE,
312 VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 0);
316 /* Is it a Linux binary ? */
317 if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
323 * While we are here, we should REALLY do some more checks
326 /* Set file/virtual offset based on a.out variant. */
327 switch ((int)(a_out->a_magic & 0xffff)) {
328 case 0413: /* ZMAGIC */
331 case 0314: /* QMAGIC */
339 bss_size = round_page(a_out->a_bss);
341 /* Check various fields in header for validity/bounds. */
342 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
347 /* text + data can't exceed file size */
348 if (a_out->a_data + a_out->a_text > attr.va_size) {
354 * text/data/bss must not exceed limits
355 * XXX - this is not complete. it should check current usage PLUS
356 * the resources needed by this library.
358 PROC_LOCK(td->td_proc);
359 if (a_out->a_text > maxtsiz ||
360 a_out->a_data + bss_size > lim_cur(td->td_proc, RLIMIT_DATA)) {
361 PROC_UNLOCK(td->td_proc);
365 PROC_UNLOCK(td->td_proc);
368 * Prevent more writers.
369 * XXX: Note that if any of the VM operations fail below we don't
372 vp->v_vflag |= VV_TEXT;
375 * Lock no longer needed
379 VFS_UNLOCK_GIANT(vfslocked);
382 * Check if file_offset page aligned. Currently we cannot handle
383 * misalinged file offsets, and so we read in the entire image
386 if (file_offset & PAGE_MASK) {
388 printf("uselib: Non page aligned binary %lu\n", file_offset);
390 /* Map text+data read/write/execute */
392 /* a_entry is the load address and is page aligned */
393 vmaddr = trunc_page(a_out->a_entry);
395 /* get anon user mapping, read+write+execute */
396 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
397 &vmaddr, a_out->a_text + a_out->a_data, FALSE, VM_PROT_ALL,
402 /* map file into kernel_map */
403 error = vm_mmap(kernel_map, &buffer,
404 round_page(a_out->a_text + a_out->a_data + file_offset),
405 VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp,
406 trunc_page(file_offset));
410 /* copy from kernel VM space to user space */
411 error = copyout(PTRIN(buffer + file_offset),
412 (void *)vmaddr, a_out->a_text + a_out->a_data);
414 /* release temporary kernel space */
415 vm_map_remove(kernel_map, buffer, buffer +
416 round_page(a_out->a_text + a_out->a_data + file_offset));
422 printf("uselib: Page aligned binary %lu\n", file_offset);
425 * for QMAGIC, a_entry is 20 bytes beyond the load address
426 * to skip the executable header
428 vmaddr = trunc_page(a_out->a_entry);
431 * Map it all into the process's space as a single
432 * copy-on-write "data" segment.
434 error = vm_mmap(&td->td_proc->p_vmspace->vm_map, &vmaddr,
435 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
436 MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset);
441 printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long *)vmaddr)[0],
442 ((long *)vmaddr)[1]);
445 /* Calculate BSS start address */
446 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
449 /* allocate some 'anon' space */
450 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
451 &vmaddr, bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
457 /* Unlock vnode if needed */
460 VFS_UNLOCK_GIANT(vfslocked);
463 /* Release the kernel mapping. */
465 vm_map_remove(kernel_map, (vm_offset_t)a_out,
466 (vm_offset_t)a_out + PAGE_SIZE);
471 #endif /* __i386__ */
474 linux_select(struct thread *td, struct linux_select_args *args)
477 struct timeval tv0, tv1, utv, *tvp;
482 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
483 (void *)args->readfds, (void *)args->writefds,
484 (void *)args->exceptfds, (void *)args->timeout);
488 * Store current time for computation of the amount of
492 if ((error = copyin(args->timeout, <v, sizeof(ltv))))
494 utv.tv_sec = ltv.tv_sec;
495 utv.tv_usec = ltv.tv_usec;
498 printf(LMSG("incoming timeout (%jd/%ld)"),
499 (intmax_t)utv.tv_sec, utv.tv_usec);
502 if (itimerfix(&utv)) {
504 * The timeval was invalid. Convert it to something
505 * valid that will act as it does under Linux.
507 utv.tv_sec += utv.tv_usec / 1000000;
508 utv.tv_usec %= 1000000;
509 if (utv.tv_usec < 0) {
511 utv.tv_usec += 1000000;
521 error = kern_select(td, args->nfds, args->readfds, args->writefds,
522 args->exceptfds, tvp, sizeof(l_int) * 8);
526 printf(LMSG("real select returns %d"), error);
532 if (td->td_retval[0]) {
534 * Compute how much time was left of the timeout,
535 * by subtracting the current time and the time
536 * before we started the call, and subtracting
537 * that result from the user-supplied value.
540 timevalsub(&tv1, &tv0);
541 timevalsub(&utv, &tv1);
548 printf(LMSG("outgoing timeout (%jd/%ld)"),
549 (intmax_t)utv.tv_sec, utv.tv_usec);
551 ltv.tv_sec = utv.tv_sec;
552 ltv.tv_usec = utv.tv_usec;
553 if ((error = copyout(<v, args->timeout, sizeof(ltv))))
560 printf(LMSG("select_out -> %d"), error);
566 linux_mremap(struct thread *td, struct linux_mremap_args *args)
568 struct munmap_args /* {
576 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
577 (void *)(uintptr_t)args->addr,
578 (unsigned long)args->old_len,
579 (unsigned long)args->new_len,
580 (unsigned long)args->flags);
583 if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) {
584 td->td_retval[0] = 0;
589 * Check for the page alignment.
590 * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK.
592 if (args->addr & PAGE_MASK) {
593 td->td_retval[0] = 0;
597 args->new_len = round_page(args->new_len);
598 args->old_len = round_page(args->old_len);
600 if (args->new_len > args->old_len) {
601 td->td_retval[0] = 0;
605 if (args->new_len < args->old_len) {
607 (caddr_t)((uintptr_t)args->addr + args->new_len);
608 bsd_args.len = args->old_len - args->new_len;
609 error = munmap(td, &bsd_args);
612 td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
616 #define LINUX_MS_ASYNC 0x0001
617 #define LINUX_MS_INVALIDATE 0x0002
618 #define LINUX_MS_SYNC 0x0004
621 linux_msync(struct thread *td, struct linux_msync_args *args)
623 struct msync_args bsd_args;
625 bsd_args.addr = (caddr_t)(uintptr_t)args->addr;
626 bsd_args.len = (uintptr_t)args->len;
627 bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
629 return (msync(td, &bsd_args));
633 linux_time(struct thread *td, struct linux_time_args *args)
641 printf(ARGS(time, "*"));
646 if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
648 td->td_retval[0] = tm;
652 struct l_times_argv {
655 l_clock_t tms_cutime;
656 l_clock_t tms_cstime;
661 * Glibc versions prior to 2.2.1 always use hard-coded CLK_TCK value.
662 * Since 2.2.1 Glibc uses value exported from kernel via AT_CLKTCK
663 * auxiliary vector entry.
667 #define CONVOTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
668 #define CONVNTCK(r) (r.tv_sec * stclohz + r.tv_usec / (1000000 / stclohz))
670 #define CONVTCK(r) (linux_kernver(td) >= LINUX_KERNVER_2004000 ? \
671 CONVNTCK(r) : CONVOTCK(r))
674 linux_times(struct thread *td, struct linux_times_args *args)
676 struct timeval tv, utime, stime, cutime, cstime;
677 struct l_times_argv tms;
683 printf(ARGS(times, "*"));
686 if (args->buf != NULL) {
690 calcru(p, &utime, &stime);
692 calccru(p, &cutime, &cstime);
695 tms.tms_utime = CONVTCK(utime);
696 tms.tms_stime = CONVTCK(stime);
698 tms.tms_cutime = CONVTCK(cutime);
699 tms.tms_cstime = CONVTCK(cstime);
701 if ((error = copyout(&tms, args->buf, sizeof(tms))))
706 td->td_retval[0] = (int)CONVTCK(tv);
711 linux_newuname(struct thread *td, struct linux_newuname_args *args)
713 struct l_new_utsname utsname;
714 char osname[LINUX_MAX_UTSNAME];
715 char osrelease[LINUX_MAX_UTSNAME];
719 if (ldebug(newuname))
720 printf(ARGS(newuname, "*"));
723 linux_get_osname(td, osname);
724 linux_get_osrelease(td, osrelease);
726 bzero(&utsname, sizeof(utsname));
727 strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
728 getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
729 getcreddomainname(td->td_ucred, utsname.domainname, LINUX_MAX_UTSNAME);
730 strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
731 strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
732 for (p = utsname.version; *p != '\0'; ++p)
737 strlcpy(utsname.machine, linux_platform, LINUX_MAX_UTSNAME);
739 return (copyout(&utsname, args->buf, sizeof(utsname)));
742 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
749 linux_utime(struct thread *td, struct linux_utime_args *args)
751 struct timeval tv[2], *tvp;
752 struct l_utimbuf lut;
756 LCONVPATHEXIST(td, args->fname, &fname);
760 printf(ARGS(utime, "%s, *"), fname);
764 if ((error = copyin(args->times, &lut, sizeof lut))) {
768 tv[0].tv_sec = lut.l_actime;
770 tv[1].tv_sec = lut.l_modtime;
776 error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
782 linux_utimes(struct thread *td, struct linux_utimes_args *args)
785 struct timeval tv[2], *tvp = NULL;
789 LCONVPATHEXIST(td, args->fname, &fname);
793 printf(ARGS(utimes, "%s, *"), fname);
796 if (args->tptr != NULL) {
797 if ((error = copyin(args->tptr, ltv, sizeof ltv))) {
801 tv[0].tv_sec = ltv[0].tv_sec;
802 tv[0].tv_usec = ltv[0].tv_usec;
803 tv[1].tv_sec = ltv[1].tv_sec;
804 tv[1].tv_usec = ltv[1].tv_usec;
808 error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
814 linux_futimesat(struct thread *td, struct linux_futimesat_args *args)
817 struct timeval tv[2], *tvp = NULL;
821 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
822 LCONVPATHEXIST_AT(td, args->filename, &fname, dfd);
825 if (ldebug(futimesat))
826 printf(ARGS(futimesat, "%s, *"), fname);
829 if (args->utimes != NULL) {
830 if ((error = copyin(args->utimes, ltv, sizeof ltv))) {
834 tv[0].tv_sec = ltv[0].tv_sec;
835 tv[0].tv_usec = ltv[0].tv_usec;
836 tv[1].tv_sec = ltv[1].tv_sec;
837 tv[1].tv_usec = ltv[1].tv_usec;
841 error = kern_utimesat(td, dfd, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
845 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
848 linux_common_wait(struct thread *td, int pid, int *status,
849 int options, struct rusage *ru)
853 error = kern_wait(td, pid, &tmpstat, options, ru);
859 if (WIFSIGNALED(tmpstat))
860 tmpstat = (tmpstat & 0xffffff80) |
861 BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
862 else if (WIFSTOPPED(tmpstat))
863 tmpstat = (tmpstat & 0xffff00ff) |
864 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
865 error = copyout(&tmpstat, status, sizeof(int));
872 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
878 printf(ARGS(waitpid, "%d, %p, %d"),
879 args->pid, (void *)args->status, args->options);
882 * this is necessary because the test in kern_wait doesn't work
883 * because we mess with the options here
885 if (args->options & ~(WUNTRACED | WNOHANG | WCONTINUED | __WCLONE))
888 options = (args->options & (WNOHANG | WUNTRACED));
889 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
890 if (args->options & __WCLONE)
891 options |= WLINUXCLONE;
893 return (linux_common_wait(td, args->pid, args->status, options, NULL));
898 linux_mknod(struct thread *td, struct linux_mknod_args *args)
903 LCONVPATHCREAT(td, args->path, &path);
907 printf(ARGS(mknod, "%s, %d, %d"), path, args->mode, args->dev);
910 switch (args->mode & S_IFMT) {
913 error = kern_mkfifo(td, path, UIO_SYSSPACE, args->mode);
918 error = kern_mknod(td, path, UIO_SYSSPACE, args->mode,
927 args->mode |= S_IFREG;
930 error = kern_open(td, path, UIO_SYSSPACE,
931 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
933 kern_close(td, td->td_retval[0]);
945 linux_mknodat(struct thread *td, struct linux_mknodat_args *args)
950 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
951 LCONVPATHCREAT_AT(td, args->filename, &path, dfd);
955 printf(ARGS(mknodat, "%s, %d, %d"), path, args->mode, args->dev);
958 switch (args->mode & S_IFMT) {
961 error = kern_mkfifoat(td, dfd, path, UIO_SYSSPACE, args->mode);
966 error = kern_mknodat(td, dfd, path, UIO_SYSSPACE, args->mode,
975 args->mode |= S_IFREG;
978 error = kern_openat(td, dfd, path, UIO_SYSSPACE,
979 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
981 kern_close(td, td->td_retval[0]);
993 * UGH! This is just about the dumbest idea I've ever heard!!
996 linux_personality(struct thread *td, struct linux_personality_args *args)
999 if (ldebug(personality))
1000 printf(ARGS(personality, "%lu"), (unsigned long)args->per);
1005 /* Yes Jim, it's still a Linux... */
1006 td->td_retval[0] = 0;
1010 struct l_itimerval {
1011 l_timeval it_interval;
1015 #define B2L_ITIMERVAL(bip, lip) \
1016 (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec; \
1017 (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec; \
1018 (bip)->it_value.tv_sec = (lip)->it_value.tv_sec; \
1019 (bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
1022 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
1025 struct l_itimerval ls;
1026 struct itimerval aitv, oitv;
1029 if (ldebug(setitimer))
1030 printf(ARGS(setitimer, "%p, %p"),
1031 (void *)uap->itv, (void *)uap->oitv);
1034 if (uap->itv == NULL) {
1035 uap->itv = uap->oitv;
1036 return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
1039 error = copyin(uap->itv, &ls, sizeof(ls));
1042 B2L_ITIMERVAL(&aitv, &ls);
1044 if (ldebug(setitimer)) {
1045 printf("setitimer: value: sec: %jd, usec: %ld\n",
1046 (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec);
1047 printf("setitimer: interval: sec: %jd, usec: %ld\n",
1048 (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec);
1051 error = kern_setitimer(td, uap->which, &aitv, &oitv);
1052 if (error != 0 || uap->oitv == NULL)
1054 B2L_ITIMERVAL(&ls, &oitv);
1056 return (copyout(&ls, uap->oitv, sizeof(ls)));
1060 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
1063 struct l_itimerval ls;
1064 struct itimerval aitv;
1067 if (ldebug(getitimer))
1068 printf(ARGS(getitimer, "%p"), (void *)uap->itv);
1070 error = kern_getitimer(td, uap->which, &aitv);
1073 B2L_ITIMERVAL(&ls, &aitv);
1074 return (copyout(&ls, uap->itv, sizeof(ls)));
1078 linux_nice(struct thread *td, struct linux_nice_args *args)
1080 struct setpriority_args bsd_args;
1082 bsd_args.which = PRIO_PROCESS;
1083 bsd_args.who = 0; /* current process */
1084 bsd_args.prio = args->inc;
1085 return (setpriority(td, &bsd_args));
1089 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
1091 struct ucred *newcred, *oldcred;
1092 l_gid_t *linux_gidset;
1097 ngrp = args->gidsetsize;
1098 if (ngrp < 0 || ngrp >= ngroups_max + 1)
1100 linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_TEMP, M_WAITOK);
1101 error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
1107 oldcred = crcopysafe(p, newcred);
1110 * cr_groups[0] holds egid. Setting the whole set from
1111 * the supplied set will cause egid to be changed too.
1112 * Keep cr_groups[0] unchanged to prevent that.
1115 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0) {
1122 newcred->cr_ngroups = ngrp + 1;
1124 bsd_gidset = newcred->cr_groups;
1127 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1131 newcred->cr_ngroups = 1;
1134 p->p_ucred = newcred;
1139 free(linux_gidset, M_TEMP);
1144 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1147 l_gid_t *linux_gidset;
1149 int bsd_gidsetsz, ngrp, error;
1151 cred = td->td_ucred;
1152 bsd_gidset = cred->cr_groups;
1153 bsd_gidsetsz = cred->cr_ngroups - 1;
1156 * cr_groups[0] holds egid. Returning the whole set
1157 * here will cause a duplicate. Exclude cr_groups[0]
1161 if ((ngrp = args->gidsetsize) == 0) {
1162 td->td_retval[0] = bsd_gidsetsz;
1166 if (ngrp < bsd_gidsetsz)
1170 linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset),
1172 while (ngrp < bsd_gidsetsz) {
1173 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1177 error = copyout(linux_gidset, args->grouplist, ngrp * sizeof(l_gid_t));
1178 free(linux_gidset, M_TEMP);
1182 td->td_retval[0] = ngrp;
1187 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1189 struct rlimit bsd_rlim;
1190 struct l_rlimit rlim;
1195 if (ldebug(setrlimit))
1196 printf(ARGS(setrlimit, "%d, %p"),
1197 args->resource, (void *)args->rlim);
1200 if (args->resource >= LINUX_RLIM_NLIMITS)
1203 which = linux_to_bsd_resource[args->resource];
1207 error = copyin(args->rlim, &rlim, sizeof(rlim));
1211 bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1212 bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1213 return (kern_setrlimit(td, which, &bsd_rlim));
1217 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1219 struct l_rlimit rlim;
1220 struct proc *p = td->td_proc;
1221 struct rlimit bsd_rlim;
1225 if (ldebug(old_getrlimit))
1226 printf(ARGS(old_getrlimit, "%d, %p"),
1227 args->resource, (void *)args->rlim);
1230 if (args->resource >= LINUX_RLIM_NLIMITS)
1233 which = linux_to_bsd_resource[args->resource];
1238 lim_rlimit(p, which, &bsd_rlim);
1241 #ifdef COMPAT_LINUX32
1242 rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1243 if (rlim.rlim_cur == UINT_MAX)
1244 rlim.rlim_cur = INT_MAX;
1245 rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1246 if (rlim.rlim_max == UINT_MAX)
1247 rlim.rlim_max = INT_MAX;
1249 rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1250 if (rlim.rlim_cur == ULONG_MAX)
1251 rlim.rlim_cur = LONG_MAX;
1252 rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1253 if (rlim.rlim_max == ULONG_MAX)
1254 rlim.rlim_max = LONG_MAX;
1256 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1260 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1262 struct l_rlimit rlim;
1263 struct proc *p = td->td_proc;
1264 struct rlimit bsd_rlim;
1268 if (ldebug(getrlimit))
1269 printf(ARGS(getrlimit, "%d, %p"),
1270 args->resource, (void *)args->rlim);
1273 if (args->resource >= LINUX_RLIM_NLIMITS)
1276 which = linux_to_bsd_resource[args->resource];
1281 lim_rlimit(p, which, &bsd_rlim);
1284 rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1285 rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1286 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1290 linux_sched_setscheduler(struct thread *td,
1291 struct linux_sched_setscheduler_args *args)
1293 struct sched_setscheduler_args bsd;
1296 if (ldebug(sched_setscheduler))
1297 printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1298 args->pid, args->policy, (const void *)args->param);
1301 switch (args->policy) {
1302 case LINUX_SCHED_OTHER:
1303 bsd.policy = SCHED_OTHER;
1305 case LINUX_SCHED_FIFO:
1306 bsd.policy = SCHED_FIFO;
1308 case LINUX_SCHED_RR:
1309 bsd.policy = SCHED_RR;
1315 bsd.pid = args->pid;
1316 bsd.param = (struct sched_param *)args->param;
1317 return (sched_setscheduler(td, &bsd));
1321 linux_sched_getscheduler(struct thread *td,
1322 struct linux_sched_getscheduler_args *args)
1324 struct sched_getscheduler_args bsd;
1328 if (ldebug(sched_getscheduler))
1329 printf(ARGS(sched_getscheduler, "%d"), args->pid);
1332 bsd.pid = args->pid;
1333 error = sched_getscheduler(td, &bsd);
1335 switch (td->td_retval[0]) {
1337 td->td_retval[0] = LINUX_SCHED_OTHER;
1340 td->td_retval[0] = LINUX_SCHED_FIFO;
1343 td->td_retval[0] = LINUX_SCHED_RR;
1351 linux_sched_get_priority_max(struct thread *td,
1352 struct linux_sched_get_priority_max_args *args)
1354 struct sched_get_priority_max_args bsd;
1357 if (ldebug(sched_get_priority_max))
1358 printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1361 switch (args->policy) {
1362 case LINUX_SCHED_OTHER:
1363 bsd.policy = SCHED_OTHER;
1365 case LINUX_SCHED_FIFO:
1366 bsd.policy = SCHED_FIFO;
1368 case LINUX_SCHED_RR:
1369 bsd.policy = SCHED_RR;
1374 return (sched_get_priority_max(td, &bsd));
1378 linux_sched_get_priority_min(struct thread *td,
1379 struct linux_sched_get_priority_min_args *args)
1381 struct sched_get_priority_min_args bsd;
1384 if (ldebug(sched_get_priority_min))
1385 printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1388 switch (args->policy) {
1389 case LINUX_SCHED_OTHER:
1390 bsd.policy = SCHED_OTHER;
1392 case LINUX_SCHED_FIFO:
1393 bsd.policy = SCHED_FIFO;
1395 case LINUX_SCHED_RR:
1396 bsd.policy = SCHED_RR;
1401 return (sched_get_priority_min(td, &bsd));
1404 #define REBOOT_CAD_ON 0x89abcdef
1405 #define REBOOT_CAD_OFF 0
1406 #define REBOOT_HALT 0xcdef0123
1407 #define REBOOT_RESTART 0x01234567
1408 #define REBOOT_RESTART2 0xA1B2C3D4
1409 #define REBOOT_POWEROFF 0x4321FEDC
1410 #define REBOOT_MAGIC1 0xfee1dead
1411 #define REBOOT_MAGIC2 0x28121969
1412 #define REBOOT_MAGIC2A 0x05121996
1413 #define REBOOT_MAGIC2B 0x16041998
1416 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1418 struct reboot_args bsd_args;
1422 printf(ARGS(reboot, "0x%x"), args->cmd);
1425 if (args->magic1 != REBOOT_MAGIC1)
1428 switch (args->magic2) {
1430 case REBOOT_MAGIC2A:
1431 case REBOOT_MAGIC2B:
1437 switch (args->cmd) {
1439 case REBOOT_CAD_OFF:
1440 return (priv_check(td, PRIV_REBOOT));
1442 bsd_args.opt = RB_HALT;
1444 case REBOOT_RESTART:
1445 case REBOOT_RESTART2:
1448 case REBOOT_POWEROFF:
1449 bsd_args.opt = RB_POWEROFF;
1454 return (reboot(td, &bsd_args));
1459 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1460 * td->td_retval[1] when COMPAT_43 is defined. This clobbers registers that
1461 * are assumed to be preserved. The following lightweight syscalls fixes
1462 * this. See also linux_getgid16() and linux_getuid16() in linux_uid16.c
1464 * linux_getpid() - MP SAFE
1465 * linux_getgid() - MP SAFE
1466 * linux_getuid() - MP SAFE
1470 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1472 struct linux_emuldata *em;
1476 printf(ARGS(getpid, ""));
1479 if (linux_use26(td)) {
1480 em = em_find(td->td_proc, EMUL_DONTLOCK);
1481 KASSERT(em != NULL, ("getpid: emuldata not found.\n"));
1482 td->td_retval[0] = em->shared->group_pid;
1484 td->td_retval[0] = td->td_proc->p_pid;
1491 linux_gettid(struct thread *td, struct linux_gettid_args *args)
1496 printf(ARGS(gettid, ""));
1499 td->td_retval[0] = td->td_proc->p_pid;
1505 linux_getppid(struct thread *td, struct linux_getppid_args *args)
1507 struct linux_emuldata *em;
1508 struct proc *p, *pp;
1511 if (ldebug(getppid))
1512 printf(ARGS(getppid, ""));
1515 if (!linux_use26(td)) {
1516 PROC_LOCK(td->td_proc);
1517 td->td_retval[0] = td->td_proc->p_pptr->p_pid;
1518 PROC_UNLOCK(td->td_proc);
1522 em = em_find(td->td_proc, EMUL_DONTLOCK);
1524 KASSERT(em != NULL, ("getppid: process emuldata not found.\n"));
1526 /* find the group leader */
1527 p = pfind(em->shared->group_pid);
1531 printf(LMSG("parent process not found.\n"));
1536 pp = p->p_pptr; /* switch to parent */
1540 /* if its also linux process */
1541 if (pp->p_sysent == &elf_linux_sysvec) {
1542 em = em_find(pp, EMUL_DONTLOCK);
1543 KASSERT(em != NULL, ("getppid: parent emuldata not found.\n"));
1545 td->td_retval[0] = em->shared->group_pid;
1547 td->td_retval[0] = pp->p_pid;
1555 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1560 printf(ARGS(getgid, ""));
1563 td->td_retval[0] = td->td_ucred->cr_rgid;
1568 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1573 printf(ARGS(getuid, ""));
1576 td->td_retval[0] = td->td_ucred->cr_ruid;
1582 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1584 struct getsid_args bsd;
1588 printf(ARGS(getsid, "%i"), args->pid);
1591 bsd.pid = args->pid;
1592 return (getsid(td, &bsd));
1596 linux_nosys(struct thread *td, struct nosys_args *ignore)
1603 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1605 struct getpriority_args bsd_args;
1609 if (ldebug(getpriority))
1610 printf(ARGS(getpriority, "%i, %i"), args->which, args->who);
1613 bsd_args.which = args->which;
1614 bsd_args.who = args->who;
1615 error = getpriority(td, &bsd_args);
1616 td->td_retval[0] = 20 - td->td_retval[0];
1621 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1626 if (ldebug(sethostname))
1627 printf(ARGS(sethostname, "*, %i"), args->len);
1631 name[1] = KERN_HOSTNAME;
1632 return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1637 linux_setdomainname(struct thread *td, struct linux_setdomainname_args *args)
1642 if (ldebug(setdomainname))
1643 printf(ARGS(setdomainname, "*, %i"), args->len);
1647 name[1] = KERN_NISDOMAINNAME;
1648 return (userland_sysctl(td, name, 2, 0, 0, 0, args->name,
1653 linux_exit_group(struct thread *td, struct linux_exit_group_args *args)
1655 struct linux_emuldata *em;
1658 if (ldebug(exit_group))
1659 printf(ARGS(exit_group, "%i"), args->error_code);
1662 em = em_find(td->td_proc, EMUL_DONTLOCK);
1663 if (em->shared->refs > 1) {
1664 EMUL_SHARED_WLOCK(&emul_shared_lock);
1665 em->shared->flags |= EMUL_SHARED_HASXSTAT;
1666 em->shared->xstat = W_EXITCODE(args->error_code, 0);
1667 EMUL_SHARED_WUNLOCK(&emul_shared_lock);
1668 if (linux_use26(td))
1669 linux_kill_threads(td, SIGKILL);
1673 * XXX: we should send a signal to the parent if
1674 * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?)
1675 * as it doesnt occur often.
1677 exit1(td, W_EXITCODE(args->error_code, 0));
1682 #define _LINUX_CAPABILITY_VERSION 0x19980330
1684 struct l_user_cap_header {
1689 struct l_user_cap_data {
1696 linux_capget(struct thread *td, struct linux_capget_args *args)
1698 struct l_user_cap_header luch;
1699 struct l_user_cap_data lucd;
1702 if (args->hdrp == NULL)
1705 error = copyin(args->hdrp, &luch, sizeof(luch));
1709 if (luch.version != _LINUX_CAPABILITY_VERSION) {
1710 luch.version = _LINUX_CAPABILITY_VERSION;
1711 error = copyout(&luch, args->hdrp, sizeof(luch));
1722 * The current implementation doesn't support setting
1723 * a capability (it's essentially a stub) so indicate
1724 * that no capabilities are currently set or available
1727 bzero (&lucd, sizeof(lucd));
1728 error = copyout(&lucd, args->datap, sizeof(lucd));
1735 linux_capset(struct thread *td, struct linux_capset_args *args)
1737 struct l_user_cap_header luch;
1738 struct l_user_cap_data lucd;
1741 if (args->hdrp == NULL || args->datap == NULL)
1744 error = copyin(args->hdrp, &luch, sizeof(luch));
1748 if (luch.version != _LINUX_CAPABILITY_VERSION) {
1749 luch.version = _LINUX_CAPABILITY_VERSION;
1750 error = copyout(&luch, args->hdrp, sizeof(luch));
1759 error = copyin(args->datap, &lucd, sizeof(lucd));
1763 /* We currently don't support setting any capabilities. */
1764 if (lucd.effective || lucd.permitted || lucd.inheritable) {
1766 "capset effective=0x%x, permitted=0x%x, "
1767 "inheritable=0x%x is not implemented",
1768 (int)lucd.effective, (int)lucd.permitted,
1769 (int)lucd.inheritable);
1777 linux_prctl(struct thread *td, struct linux_prctl_args *args)
1779 int error = 0, max_size;
1780 struct proc *p = td->td_proc;
1781 char comm[LINUX_MAX_COMM_LEN];
1782 struct linux_emuldata *em;
1787 printf(ARGS(prctl, "%d, %d, %d, %d, %d"), args->option,
1788 args->arg2, args->arg3, args->arg4, args->arg5);
1791 switch (args->option) {
1792 case LINUX_PR_SET_PDEATHSIG:
1793 if (!LINUX_SIG_VALID(args->arg2))
1795 em = em_find(p, EMUL_DOLOCK);
1796 KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1797 em->pdeath_signal = args->arg2;
1798 EMUL_UNLOCK(&emul_lock);
1800 case LINUX_PR_GET_PDEATHSIG:
1801 em = em_find(p, EMUL_DOLOCK);
1802 KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1803 pdeath_signal = em->pdeath_signal;
1804 EMUL_UNLOCK(&emul_lock);
1805 error = copyout(&pdeath_signal,
1806 (void *)(register_t)args->arg2,
1807 sizeof(pdeath_signal));
1809 case LINUX_PR_GET_KEEPCAPS:
1811 * Indicate that we always clear the effective and
1812 * permitted capability sets when the user id becomes
1813 * non-zero (actually the capability sets are simply
1814 * always zero in the current implementation).
1816 td->td_retval[0] = 0;
1818 case LINUX_PR_SET_KEEPCAPS:
1820 * Ignore requests to keep the effective and permitted
1821 * capability sets when the user id becomes non-zero.
1824 case LINUX_PR_SET_NAME:
1826 * To be on the safe side we need to make sure to not
1827 * overflow the size a linux program expects. We already
1828 * do this here in the copyin, so that we don't need to
1831 max_size = MIN(sizeof(comm), sizeof(p->p_comm));
1832 error = copyinstr((void *)(register_t)args->arg2, comm,
1835 /* Linux silently truncates the name if it is too long. */
1836 if (error == ENAMETOOLONG) {
1838 * XXX: copyinstr() isn't documented to populate the
1839 * array completely, so do a copyin() to be on the
1840 * safe side. This should be changed in case
1841 * copyinstr() is changed to guarantee this.
1843 error = copyin((void *)(register_t)args->arg2, comm,
1845 comm[max_size - 1] = '\0';
1851 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
1854 case LINUX_PR_GET_NAME:
1856 strlcpy(comm, p->p_comm, sizeof(comm));
1858 error = copyout(comm, (void *)(register_t)args->arg2,
1870 * Get affinity of a process.
1873 linux_sched_getaffinity(struct thread *td,
1874 struct linux_sched_getaffinity_args *args)
1877 struct cpuset_getaffinity_args cga;
1880 if (ldebug(sched_getaffinity))
1881 printf(ARGS(sched_getaffinity, "%d, %d, *"), args->pid,
1884 if (args->len < sizeof(cpuset_t))
1887 cga.level = CPU_LEVEL_WHICH;
1888 cga.which = CPU_WHICH_PID;
1890 cga.cpusetsize = sizeof(cpuset_t);
1891 cga.mask = (cpuset_t *) args->user_mask_ptr;
1893 if ((error = cpuset_getaffinity(td, &cga)) == 0)
1894 td->td_retval[0] = sizeof(cpuset_t);
1900 * Set affinity of a process.
1903 linux_sched_setaffinity(struct thread *td,
1904 struct linux_sched_setaffinity_args *args)
1906 struct cpuset_setaffinity_args csa;
1909 if (ldebug(sched_setaffinity))
1910 printf(ARGS(sched_setaffinity, "%d, %d, *"), args->pid,
1913 if (args->len < sizeof(cpuset_t))
1916 csa.level = CPU_LEVEL_WHICH;
1917 csa.which = CPU_WHICH_PID;
1919 csa.cpusetsize = sizeof(cpuset_t);
1920 csa.mask = (cpuset_t *) args->user_mask_ptr;
1922 return (cpuset_setaffinity(td, &csa));