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 - vm_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;
235 int error, locked, writecount;
237 LCONVPATHEXIST(td, args->library, &library);
241 printf(ARGS(uselib, "%s"), library);
248 NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | AUDITVNODE1,
249 UIO_SYSSPACE, library, td);
256 NDFREE(&ni, NDF_ONLY_PNBUF);
259 * From here on down, we have a locked vnode that must be unlocked.
260 * XXX: The code below largely duplicates exec_check_permissions().
265 error = VOP_GET_WRITECOUNT(vp, &writecount);
268 if (writecount != 0) {
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
378 * Lock no longer needed
384 * Check if file_offset page aligned. Currently we cannot handle
385 * misalinged file offsets, and so we read in the entire image
388 if (file_offset & PAGE_MASK) {
390 printf("uselib: Non page aligned binary %lu\n", file_offset);
392 /* Map text+data read/write/execute */
394 /* a_entry is the load address and is page aligned */
395 vmaddr = trunc_page(a_out->a_entry);
397 /* get anon user mapping, read+write+execute */
398 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
399 &vmaddr, a_out->a_text + a_out->a_data, 0, VMFS_NO_SPACE,
400 VM_PROT_ALL, VM_PROT_ALL, 0);
404 error = vn_rdwr(UIO_READ, vp, (void *)vmaddr, file_offset,
405 a_out->a_text + a_out->a_data, UIO_USERSPACE, 0,
406 td->td_ucred, NOCRED, &aresid, td);
415 printf("uselib: Page aligned binary %lu\n", file_offset);
418 * for QMAGIC, a_entry is 20 bytes beyond the load address
419 * to skip the executable header
421 vmaddr = trunc_page(a_out->a_entry);
424 * Map it all into the process's space as a single
425 * copy-on-write "data" segment.
427 error = vm_mmap(&td->td_proc->p_vmspace->vm_map, &vmaddr,
428 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
429 MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset);
434 printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long *)vmaddr)[0],
435 ((long *)vmaddr)[1]);
438 /* Calculate BSS start address */
439 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
442 /* allocate some 'anon' space */
443 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
444 &vmaddr, bss_size, 0, VMFS_NO_SPACE, VM_PROT_ALL,
451 /* Unlock vnode if needed */
455 /* Release the temporary mapping. */
457 kmap_free_wakeup(exec_map, (vm_offset_t)a_out, PAGE_SIZE);
462 #endif /* __i386__ */
465 linux_select(struct thread *td, struct linux_select_args *args)
468 struct timeval tv0, tv1, utv, *tvp;
473 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
474 (void *)args->readfds, (void *)args->writefds,
475 (void *)args->exceptfds, (void *)args->timeout);
479 * Store current time for computation of the amount of
483 if ((error = copyin(args->timeout, <v, sizeof(ltv))))
485 utv.tv_sec = ltv.tv_sec;
486 utv.tv_usec = ltv.tv_usec;
489 printf(LMSG("incoming timeout (%jd/%ld)"),
490 (intmax_t)utv.tv_sec, utv.tv_usec);
493 if (itimerfix(&utv)) {
495 * The timeval was invalid. Convert it to something
496 * valid that will act as it does under Linux.
498 utv.tv_sec += utv.tv_usec / 1000000;
499 utv.tv_usec %= 1000000;
500 if (utv.tv_usec < 0) {
502 utv.tv_usec += 1000000;
512 error = kern_select(td, args->nfds, args->readfds, args->writefds,
513 args->exceptfds, tvp, sizeof(l_int) * 8);
517 printf(LMSG("real select returns %d"), error);
523 if (td->td_retval[0]) {
525 * Compute how much time was left of the timeout,
526 * by subtracting the current time and the time
527 * before we started the call, and subtracting
528 * that result from the user-supplied value.
531 timevalsub(&tv1, &tv0);
532 timevalsub(&utv, &tv1);
539 printf(LMSG("outgoing timeout (%jd/%ld)"),
540 (intmax_t)utv.tv_sec, utv.tv_usec);
542 ltv.tv_sec = utv.tv_sec;
543 ltv.tv_usec = utv.tv_usec;
544 if ((error = copyout(<v, args->timeout, sizeof(ltv))))
551 printf(LMSG("select_out -> %d"), error);
557 linux_mremap(struct thread *td, struct linux_mremap_args *args)
559 struct munmap_args /* {
567 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
568 (void *)(uintptr_t)args->addr,
569 (unsigned long)args->old_len,
570 (unsigned long)args->new_len,
571 (unsigned long)args->flags);
574 if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) {
575 td->td_retval[0] = 0;
580 * Check for the page alignment.
581 * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK.
583 if (args->addr & PAGE_MASK) {
584 td->td_retval[0] = 0;
588 args->new_len = round_page(args->new_len);
589 args->old_len = round_page(args->old_len);
591 if (args->new_len > args->old_len) {
592 td->td_retval[0] = 0;
596 if (args->new_len < args->old_len) {
598 (caddr_t)((uintptr_t)args->addr + args->new_len);
599 bsd_args.len = args->old_len - args->new_len;
600 error = sys_munmap(td, &bsd_args);
603 td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
607 #define LINUX_MS_ASYNC 0x0001
608 #define LINUX_MS_INVALIDATE 0x0002
609 #define LINUX_MS_SYNC 0x0004
612 linux_msync(struct thread *td, struct linux_msync_args *args)
614 struct msync_args bsd_args;
616 bsd_args.addr = (caddr_t)(uintptr_t)args->addr;
617 bsd_args.len = (uintptr_t)args->len;
618 bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
620 return (sys_msync(td, &bsd_args));
624 linux_time(struct thread *td, struct linux_time_args *args)
632 printf(ARGS(time, "*"));
637 if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
639 td->td_retval[0] = tm;
643 struct l_times_argv {
646 l_clock_t tms_cutime;
647 l_clock_t tms_cstime;
652 * Glibc versions prior to 2.2.1 always use hard-coded CLK_TCK value.
653 * Since 2.2.1 Glibc uses value exported from kernel via AT_CLKTCK
654 * auxiliary vector entry.
658 #define CONVOTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
659 #define CONVNTCK(r) (r.tv_sec * stclohz + r.tv_usec / (1000000 / stclohz))
661 #define CONVTCK(r) (linux_kernver(td) >= LINUX_KERNVER_2004000 ? \
662 CONVNTCK(r) : CONVOTCK(r))
665 linux_times(struct thread *td, struct linux_times_args *args)
667 struct timeval tv, utime, stime, cutime, cstime;
668 struct l_times_argv tms;
674 printf(ARGS(times, "*"));
677 if (args->buf != NULL) {
681 calcru(p, &utime, &stime);
683 calccru(p, &cutime, &cstime);
686 tms.tms_utime = CONVTCK(utime);
687 tms.tms_stime = CONVTCK(stime);
689 tms.tms_cutime = CONVTCK(cutime);
690 tms.tms_cstime = CONVTCK(cstime);
692 if ((error = copyout(&tms, args->buf, sizeof(tms))))
697 td->td_retval[0] = (int)CONVTCK(tv);
702 linux_newuname(struct thread *td, struct linux_newuname_args *args)
704 struct l_new_utsname utsname;
705 char osname[LINUX_MAX_UTSNAME];
706 char osrelease[LINUX_MAX_UTSNAME];
710 if (ldebug(newuname))
711 printf(ARGS(newuname, "*"));
714 linux_get_osname(td, osname);
715 linux_get_osrelease(td, osrelease);
717 bzero(&utsname, sizeof(utsname));
718 strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
719 getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
720 getcreddomainname(td->td_ucred, utsname.domainname, LINUX_MAX_UTSNAME);
721 strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
722 strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
723 for (p = utsname.version; *p != '\0'; ++p)
728 strlcpy(utsname.machine, linux_platform, LINUX_MAX_UTSNAME);
730 return (copyout(&utsname, args->buf, sizeof(utsname)));
733 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
740 linux_utime(struct thread *td, struct linux_utime_args *args)
742 struct timeval tv[2], *tvp;
743 struct l_utimbuf lut;
747 LCONVPATHEXIST(td, args->fname, &fname);
751 printf(ARGS(utime, "%s, *"), fname);
755 if ((error = copyin(args->times, &lut, sizeof lut))) {
759 tv[0].tv_sec = lut.l_actime;
761 tv[1].tv_sec = lut.l_modtime;
767 error = kern_utimesat(td, AT_FDCWD, fname, UIO_SYSSPACE, tvp,
774 linux_utimes(struct thread *td, struct linux_utimes_args *args)
777 struct timeval tv[2], *tvp = NULL;
781 LCONVPATHEXIST(td, args->fname, &fname);
785 printf(ARGS(utimes, "%s, *"), fname);
788 if (args->tptr != NULL) {
789 if ((error = copyin(args->tptr, ltv, sizeof ltv))) {
793 tv[0].tv_sec = ltv[0].tv_sec;
794 tv[0].tv_usec = ltv[0].tv_usec;
795 tv[1].tv_sec = ltv[1].tv_sec;
796 tv[1].tv_usec = ltv[1].tv_usec;
800 error = kern_utimesat(td, AT_FDCWD, fname, 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_mkfifoat(td, AT_FDCWD, path, UIO_SYSSPACE,
912 error = kern_mknodat(td, AT_FDCWD, path, UIO_SYSSPACE,
913 args->mode, args->dev);
921 args->mode |= S_IFREG;
924 error = kern_openat(td, AT_FDCWD, path, UIO_SYSSPACE,
925 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
927 kern_close(td, td->td_retval[0]);
939 linux_mknodat(struct thread *td, struct linux_mknodat_args *args)
944 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
945 LCONVPATHCREAT_AT(td, args->filename, &path, dfd);
949 printf(ARGS(mknodat, "%s, %d, %d"), path, args->mode, args->dev);
952 switch (args->mode & S_IFMT) {
955 error = kern_mkfifoat(td, dfd, path, UIO_SYSSPACE, args->mode);
960 error = kern_mknodat(td, dfd, path, UIO_SYSSPACE, args->mode,
969 args->mode |= S_IFREG;
972 error = kern_openat(td, dfd, path, UIO_SYSSPACE,
973 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
975 kern_close(td, td->td_retval[0]);
987 * UGH! This is just about the dumbest idea I've ever heard!!
990 linux_personality(struct thread *td, struct linux_personality_args *args)
993 if (ldebug(personality))
994 printf(ARGS(personality, "%lu"), (unsigned long)args->per);
999 /* Yes Jim, it's still a Linux... */
1000 td->td_retval[0] = 0;
1004 struct l_itimerval {
1005 l_timeval it_interval;
1009 #define B2L_ITIMERVAL(bip, lip) \
1010 (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec; \
1011 (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec; \
1012 (bip)->it_value.tv_sec = (lip)->it_value.tv_sec; \
1013 (bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
1016 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
1019 struct l_itimerval ls;
1020 struct itimerval aitv, oitv;
1023 if (ldebug(setitimer))
1024 printf(ARGS(setitimer, "%p, %p"),
1025 (void *)uap->itv, (void *)uap->oitv);
1028 if (uap->itv == NULL) {
1029 uap->itv = uap->oitv;
1030 return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
1033 error = copyin(uap->itv, &ls, sizeof(ls));
1036 B2L_ITIMERVAL(&aitv, &ls);
1038 if (ldebug(setitimer)) {
1039 printf("setitimer: value: sec: %jd, usec: %ld\n",
1040 (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec);
1041 printf("setitimer: interval: sec: %jd, usec: %ld\n",
1042 (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec);
1045 error = kern_setitimer(td, uap->which, &aitv, &oitv);
1046 if (error != 0 || uap->oitv == NULL)
1048 B2L_ITIMERVAL(&ls, &oitv);
1050 return (copyout(&ls, uap->oitv, sizeof(ls)));
1054 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
1057 struct l_itimerval ls;
1058 struct itimerval aitv;
1061 if (ldebug(getitimer))
1062 printf(ARGS(getitimer, "%p"), (void *)uap->itv);
1064 error = kern_getitimer(td, uap->which, &aitv);
1067 B2L_ITIMERVAL(&ls, &aitv);
1068 return (copyout(&ls, uap->itv, sizeof(ls)));
1072 linux_nice(struct thread *td, struct linux_nice_args *args)
1074 struct setpriority_args bsd_args;
1076 bsd_args.which = PRIO_PROCESS;
1077 bsd_args.who = 0; /* current process */
1078 bsd_args.prio = args->inc;
1079 return (sys_setpriority(td, &bsd_args));
1083 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
1085 struct ucred *newcred, *oldcred;
1086 l_gid_t *linux_gidset;
1091 ngrp = args->gidsetsize;
1092 if (ngrp < 0 || ngrp >= ngroups_max + 1)
1094 linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_TEMP, M_WAITOK);
1095 error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
1101 oldcred = crcopysafe(p, newcred);
1104 * cr_groups[0] holds egid. Setting the whole set from
1105 * the supplied set will cause egid to be changed too.
1106 * Keep cr_groups[0] unchanged to prevent that.
1109 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0) {
1116 newcred->cr_ngroups = ngrp + 1;
1118 bsd_gidset = newcred->cr_groups;
1121 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1125 newcred->cr_ngroups = 1;
1128 proc_set_cred(p, newcred);
1133 free(linux_gidset, M_TEMP);
1138 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1141 l_gid_t *linux_gidset;
1143 int bsd_gidsetsz, ngrp, error;
1145 cred = td->td_ucred;
1146 bsd_gidset = cred->cr_groups;
1147 bsd_gidsetsz = cred->cr_ngroups - 1;
1150 * cr_groups[0] holds egid. Returning the whole set
1151 * here will cause a duplicate. Exclude cr_groups[0]
1155 if ((ngrp = args->gidsetsize) == 0) {
1156 td->td_retval[0] = bsd_gidsetsz;
1160 if (ngrp < bsd_gidsetsz)
1164 linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset),
1166 while (ngrp < bsd_gidsetsz) {
1167 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1171 error = copyout(linux_gidset, args->grouplist, ngrp * sizeof(l_gid_t));
1172 free(linux_gidset, M_TEMP);
1176 td->td_retval[0] = ngrp;
1181 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1183 struct rlimit bsd_rlim;
1184 struct l_rlimit rlim;
1189 if (ldebug(setrlimit))
1190 printf(ARGS(setrlimit, "%d, %p"),
1191 args->resource, (void *)args->rlim);
1194 if (args->resource >= LINUX_RLIM_NLIMITS)
1197 which = linux_to_bsd_resource[args->resource];
1201 error = copyin(args->rlim, &rlim, sizeof(rlim));
1205 bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1206 bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1207 return (kern_setrlimit(td, which, &bsd_rlim));
1211 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1213 struct l_rlimit rlim;
1214 struct proc *p = td->td_proc;
1215 struct rlimit bsd_rlim;
1219 if (ldebug(old_getrlimit))
1220 printf(ARGS(old_getrlimit, "%d, %p"),
1221 args->resource, (void *)args->rlim);
1224 if (args->resource >= LINUX_RLIM_NLIMITS)
1227 which = linux_to_bsd_resource[args->resource];
1232 lim_rlimit(p, which, &bsd_rlim);
1235 #ifdef COMPAT_LINUX32
1236 rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1237 if (rlim.rlim_cur == UINT_MAX)
1238 rlim.rlim_cur = INT_MAX;
1239 rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1240 if (rlim.rlim_max == UINT_MAX)
1241 rlim.rlim_max = INT_MAX;
1243 rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1244 if (rlim.rlim_cur == ULONG_MAX)
1245 rlim.rlim_cur = LONG_MAX;
1246 rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1247 if (rlim.rlim_max == ULONG_MAX)
1248 rlim.rlim_max = LONG_MAX;
1250 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1254 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1256 struct l_rlimit rlim;
1257 struct proc *p = td->td_proc;
1258 struct rlimit bsd_rlim;
1262 if (ldebug(getrlimit))
1263 printf(ARGS(getrlimit, "%d, %p"),
1264 args->resource, (void *)args->rlim);
1267 if (args->resource >= LINUX_RLIM_NLIMITS)
1270 which = linux_to_bsd_resource[args->resource];
1275 lim_rlimit(p, which, &bsd_rlim);
1278 rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1279 rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1280 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1284 linux_sched_setscheduler(struct thread *td,
1285 struct linux_sched_setscheduler_args *args)
1287 struct sched_param sched_param;
1292 if (ldebug(sched_setscheduler))
1293 printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1294 args->pid, args->policy, (const void *)args->param);
1297 switch (args->policy) {
1298 case LINUX_SCHED_OTHER:
1299 policy = SCHED_OTHER;
1301 case LINUX_SCHED_FIFO:
1302 policy = SCHED_FIFO;
1304 case LINUX_SCHED_RR:
1311 error = copyin(args->param, &sched_param, sizeof(sched_param));
1315 tdt = linux_tdfind(td, args->pid, -1);
1319 error = kern_sched_setscheduler(td, tdt, policy, &sched_param);
1320 PROC_UNLOCK(tdt->td_proc);
1325 linux_sched_getscheduler(struct thread *td,
1326 struct linux_sched_getscheduler_args *args)
1332 if (ldebug(sched_getscheduler))
1333 printf(ARGS(sched_getscheduler, "%d"), args->pid);
1336 tdt = linux_tdfind(td, args->pid, -1);
1340 error = kern_sched_getscheduler(td, tdt, &policy);
1341 PROC_UNLOCK(tdt->td_proc);
1345 td->td_retval[0] = LINUX_SCHED_OTHER;
1348 td->td_retval[0] = LINUX_SCHED_FIFO;
1351 td->td_retval[0] = LINUX_SCHED_RR;
1358 linux_sched_get_priority_max(struct thread *td,
1359 struct linux_sched_get_priority_max_args *args)
1361 struct sched_get_priority_max_args bsd;
1364 if (ldebug(sched_get_priority_max))
1365 printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1368 switch (args->policy) {
1369 case LINUX_SCHED_OTHER:
1370 bsd.policy = SCHED_OTHER;
1372 case LINUX_SCHED_FIFO:
1373 bsd.policy = SCHED_FIFO;
1375 case LINUX_SCHED_RR:
1376 bsd.policy = SCHED_RR;
1381 return (sys_sched_get_priority_max(td, &bsd));
1385 linux_sched_get_priority_min(struct thread *td,
1386 struct linux_sched_get_priority_min_args *args)
1388 struct sched_get_priority_min_args bsd;
1391 if (ldebug(sched_get_priority_min))
1392 printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1395 switch (args->policy) {
1396 case LINUX_SCHED_OTHER:
1397 bsd.policy = SCHED_OTHER;
1399 case LINUX_SCHED_FIFO:
1400 bsd.policy = SCHED_FIFO;
1402 case LINUX_SCHED_RR:
1403 bsd.policy = SCHED_RR;
1408 return (sys_sched_get_priority_min(td, &bsd));
1411 #define REBOOT_CAD_ON 0x89abcdef
1412 #define REBOOT_CAD_OFF 0
1413 #define REBOOT_HALT 0xcdef0123
1414 #define REBOOT_RESTART 0x01234567
1415 #define REBOOT_RESTART2 0xA1B2C3D4
1416 #define REBOOT_POWEROFF 0x4321FEDC
1417 #define REBOOT_MAGIC1 0xfee1dead
1418 #define REBOOT_MAGIC2 0x28121969
1419 #define REBOOT_MAGIC2A 0x05121996
1420 #define REBOOT_MAGIC2B 0x16041998
1423 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1425 struct reboot_args bsd_args;
1429 printf(ARGS(reboot, "0x%x"), args->cmd);
1432 if (args->magic1 != REBOOT_MAGIC1)
1435 switch (args->magic2) {
1437 case REBOOT_MAGIC2A:
1438 case REBOOT_MAGIC2B:
1444 switch (args->cmd) {
1446 case REBOOT_CAD_OFF:
1447 return (priv_check(td, PRIV_REBOOT));
1449 bsd_args.opt = RB_HALT;
1451 case REBOOT_RESTART:
1452 case REBOOT_RESTART2:
1455 case REBOOT_POWEROFF:
1456 bsd_args.opt = RB_POWEROFF;
1461 return (sys_reboot(td, &bsd_args));
1466 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1467 * td->td_retval[1] when COMPAT_43 is defined. This clobbers registers that
1468 * are assumed to be preserved. The following lightweight syscalls fixes
1469 * this. See also linux_getgid16() and linux_getuid16() in linux_uid16.c
1471 * linux_getpid() - MP SAFE
1472 * linux_getgid() - MP SAFE
1473 * linux_getuid() - MP SAFE
1477 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1482 printf(ARGS(getpid, ""));
1484 td->td_retval[0] = td->td_proc->p_pid;
1490 linux_gettid(struct thread *td, struct linux_gettid_args *args)
1492 struct linux_emuldata *em;
1496 printf(ARGS(gettid, ""));
1500 KASSERT(em != NULL, ("gettid: emuldata not found.\n"));
1502 td->td_retval[0] = em->em_tid;
1509 linux_getppid(struct thread *td, struct linux_getppid_args *args)
1513 if (ldebug(getppid))
1514 printf(ARGS(getppid, ""));
1517 PROC_LOCK(td->td_proc);
1518 td->td_retval[0] = td->td_proc->p_pptr->p_pid;
1519 PROC_UNLOCK(td->td_proc);
1524 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1529 printf(ARGS(getgid, ""));
1532 td->td_retval[0] = td->td_ucred->cr_rgid;
1537 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1542 printf(ARGS(getuid, ""));
1545 td->td_retval[0] = td->td_ucred->cr_ruid;
1551 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1553 struct getsid_args bsd;
1557 printf(ARGS(getsid, "%i"), args->pid);
1560 bsd.pid = args->pid;
1561 return (sys_getsid(td, &bsd));
1565 linux_nosys(struct thread *td, struct nosys_args *ignore)
1572 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1574 struct getpriority_args bsd_args;
1578 if (ldebug(getpriority))
1579 printf(ARGS(getpriority, "%i, %i"), args->which, args->who);
1582 bsd_args.which = args->which;
1583 bsd_args.who = args->who;
1584 error = sys_getpriority(td, &bsd_args);
1585 td->td_retval[0] = 20 - td->td_retval[0];
1590 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1595 if (ldebug(sethostname))
1596 printf(ARGS(sethostname, "*, %i"), args->len);
1600 name[1] = KERN_HOSTNAME;
1601 return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1606 linux_setdomainname(struct thread *td, struct linux_setdomainname_args *args)
1611 if (ldebug(setdomainname))
1612 printf(ARGS(setdomainname, "*, %i"), args->len);
1616 name[1] = KERN_NISDOMAINNAME;
1617 return (userland_sysctl(td, name, 2, 0, 0, 0, args->name,
1622 linux_exit_group(struct thread *td, struct linux_exit_group_args *args)
1626 if (ldebug(exit_group))
1627 printf(ARGS(exit_group, "%i"), args->error_code);
1630 LINUX_CTR2(exit_group, "thread(%d) (%d)", td->td_tid,
1634 * XXX: we should send a signal to the parent if
1635 * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?)
1636 * as it doesnt occur often.
1638 exit1(td, W_EXITCODE(args->error_code, 0));
1642 #define _LINUX_CAPABILITY_VERSION 0x19980330
1644 struct l_user_cap_header {
1649 struct l_user_cap_data {
1656 linux_capget(struct thread *td, struct linux_capget_args *args)
1658 struct l_user_cap_header luch;
1659 struct l_user_cap_data lucd;
1662 if (args->hdrp == NULL)
1665 error = copyin(args->hdrp, &luch, sizeof(luch));
1669 if (luch.version != _LINUX_CAPABILITY_VERSION) {
1670 luch.version = _LINUX_CAPABILITY_VERSION;
1671 error = copyout(&luch, args->hdrp, sizeof(luch));
1682 * The current implementation doesn't support setting
1683 * a capability (it's essentially a stub) so indicate
1684 * that no capabilities are currently set or available
1687 bzero (&lucd, sizeof(lucd));
1688 error = copyout(&lucd, args->datap, sizeof(lucd));
1695 linux_capset(struct thread *td, struct linux_capset_args *args)
1697 struct l_user_cap_header luch;
1698 struct l_user_cap_data lucd;
1701 if (args->hdrp == NULL || args->datap == NULL)
1704 error = copyin(args->hdrp, &luch, sizeof(luch));
1708 if (luch.version != _LINUX_CAPABILITY_VERSION) {
1709 luch.version = _LINUX_CAPABILITY_VERSION;
1710 error = copyout(&luch, args->hdrp, sizeof(luch));
1719 error = copyin(args->datap, &lucd, sizeof(lucd));
1723 /* We currently don't support setting any capabilities. */
1724 if (lucd.effective || lucd.permitted || lucd.inheritable) {
1726 "capset effective=0x%x, permitted=0x%x, "
1727 "inheritable=0x%x is not implemented",
1728 (int)lucd.effective, (int)lucd.permitted,
1729 (int)lucd.inheritable);
1737 linux_prctl(struct thread *td, struct linux_prctl_args *args)
1739 int error = 0, max_size;
1740 struct proc *p = td->td_proc;
1741 char comm[LINUX_MAX_COMM_LEN];
1742 struct linux_emuldata *em;
1747 printf(ARGS(prctl, "%d, %d, %d, %d, %d"), args->option,
1748 args->arg2, args->arg3, args->arg4, args->arg5);
1751 switch (args->option) {
1752 case LINUX_PR_SET_PDEATHSIG:
1753 if (!LINUX_SIG_VALID(args->arg2))
1756 KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1757 em->pdeath_signal = args->arg2;
1759 case LINUX_PR_GET_PDEATHSIG:
1761 KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1762 pdeath_signal = em->pdeath_signal;
1763 error = copyout(&pdeath_signal,
1764 (void *)(register_t)args->arg2,
1765 sizeof(pdeath_signal));
1767 case LINUX_PR_GET_KEEPCAPS:
1769 * Indicate that we always clear the effective and
1770 * permitted capability sets when the user id becomes
1771 * non-zero (actually the capability sets are simply
1772 * always zero in the current implementation).
1774 td->td_retval[0] = 0;
1776 case LINUX_PR_SET_KEEPCAPS:
1778 * Ignore requests to keep the effective and permitted
1779 * capability sets when the user id becomes non-zero.
1782 case LINUX_PR_SET_NAME:
1784 * To be on the safe side we need to make sure to not
1785 * overflow the size a linux program expects. We already
1786 * do this here in the copyin, so that we don't need to
1789 max_size = MIN(sizeof(comm), sizeof(p->p_comm));
1790 error = copyinstr((void *)(register_t)args->arg2, comm,
1793 /* Linux silently truncates the name if it is too long. */
1794 if (error == ENAMETOOLONG) {
1796 * XXX: copyinstr() isn't documented to populate the
1797 * array completely, so do a copyin() to be on the
1798 * safe side. This should be changed in case
1799 * copyinstr() is changed to guarantee this.
1801 error = copyin((void *)(register_t)args->arg2, comm,
1803 comm[max_size - 1] = '\0';
1809 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
1812 case LINUX_PR_GET_NAME:
1814 strlcpy(comm, p->p_comm, sizeof(comm));
1816 error = copyout(comm, (void *)(register_t)args->arg2,
1828 linux_sched_setparam(struct thread *td,
1829 struct linux_sched_setparam_args *uap)
1831 struct sched_param sched_param;
1836 if (ldebug(sched_setparam))
1837 printf(ARGS(sched_setparam, "%d, *"), uap->pid);
1840 error = copyin(uap->param, &sched_param, sizeof(sched_param));
1844 tdt = linux_tdfind(td, uap->pid, -1);
1848 error = kern_sched_setparam(td, tdt, &sched_param);
1849 PROC_UNLOCK(tdt->td_proc);
1854 linux_sched_getparam(struct thread *td,
1855 struct linux_sched_getparam_args *uap)
1857 struct sched_param sched_param;
1862 if (ldebug(sched_getparam))
1863 printf(ARGS(sched_getparam, "%d, *"), uap->pid);
1866 tdt = linux_tdfind(td, uap->pid, -1);
1870 error = kern_sched_getparam(td, tdt, &sched_param);
1871 PROC_UNLOCK(tdt->td_proc);
1873 error = copyout(&sched_param, uap->param,
1874 sizeof(sched_param));
1879 * Get affinity of a process.
1882 linux_sched_getaffinity(struct thread *td,
1883 struct linux_sched_getaffinity_args *args)
1887 struct cpuset_getaffinity_args cga;
1890 if (ldebug(sched_getaffinity))
1891 printf(ARGS(sched_getaffinity, "%d, %d, *"), args->pid,
1894 if (args->len < sizeof(cpuset_t))
1897 tdt = linux_tdfind(td, args->pid, -1);
1901 PROC_UNLOCK(tdt->td_proc);
1902 cga.level = CPU_LEVEL_WHICH;
1903 cga.which = CPU_WHICH_TID;
1904 cga.id = tdt->td_tid;
1905 cga.cpusetsize = sizeof(cpuset_t);
1906 cga.mask = (cpuset_t *) args->user_mask_ptr;
1908 if ((error = sys_cpuset_getaffinity(td, &cga)) == 0)
1909 td->td_retval[0] = sizeof(cpuset_t);
1915 * Set affinity of a process.
1918 linux_sched_setaffinity(struct thread *td,
1919 struct linux_sched_setaffinity_args *args)
1921 struct cpuset_setaffinity_args csa;
1925 if (ldebug(sched_setaffinity))
1926 printf(ARGS(sched_setaffinity, "%d, %d, *"), args->pid,
1929 if (args->len < sizeof(cpuset_t))
1932 tdt = linux_tdfind(td, args->pid, -1);
1936 PROC_UNLOCK(tdt->td_proc);
1937 csa.level = CPU_LEVEL_WHICH;
1938 csa.which = CPU_WHICH_TID;
1939 csa.id = tdt->td_tid;
1940 csa.cpusetsize = sizeof(cpuset_t);
1941 csa.mask = (cpuset_t *) args->user_mask_ptr;
1943 return (sys_cpuset_setaffinity(td, &csa));
1947 linux_sched_rr_get_interval(struct thread *td,
1948 struct linux_sched_rr_get_interval_args *uap)
1951 struct l_timespec lts;
1955 tdt = linux_tdfind(td, uap->pid, -1);
1959 error = kern_sched_rr_get_interval_td(td, tdt, &ts);
1960 PROC_UNLOCK(tdt->td_proc);
1963 lts.tv_sec = ts.tv_sec;
1964 lts.tv_nsec = ts.tv_nsec;
1965 return (copyout(<s, uap->interval, sizeof(lts)));
1969 * In case when the Linux thread is the initial thread in
1970 * the thread group thread id is equal to the process id.
1971 * Glibc depends on this magic (assert in pthread_getattr_np.c).
1974 linux_tdfind(struct thread *td, lwpid_t tid, pid_t pid)
1976 struct linux_emuldata *em;
1981 if (tid == 0 || tid == td->td_tid) {
1983 PROC_LOCK(tdt->td_proc);
1984 } else if (tid > PID_MAX)
1985 tdt = tdfind(tid, pid);
1988 * Initial thread where the tid equal to the pid.
1992 if (SV_PROC_ABI(p) != SV_ABI_LINUX) {
1994 * p is not a Linuxulator process.
1999 FOREACH_THREAD_IN_PROC(p, tdt) {
2001 if (tid == em->em_tid)