2 * Copyright (c) 1994-1995 Søren Schmidt
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * $FreeBSD: src/sys/compat/linux/linux_misc.c,v 1.85.2.9 2002/09/24 08:11:41 mdodd Exp $
31 #include "opt_compat.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/fcntl.h>
36 #include <sys/imgact_aout.h>
37 #include <sys/kernel.h>
38 #include <sys/kern_syscall.h>
41 #include <sys/mount.h>
45 #include <sys/nlookup.h>
46 #include <sys/blist.h>
47 #include <sys/reboot.h>
48 #include <sys/resourcevar.h>
49 #include <sys/signalvar.h>
51 #include <sys/sysctl.h>
52 #include <sys/sysproto.h>
54 #include <sys/unistd.h>
55 #include <sys/vmmeter.h>
56 #include <sys/vnode.h>
59 #include <sys/signal2.h>
60 #include <sys/thread2.h>
61 #include <sys/mplock2.h>
62 #include <sys/spinlock2.h>
66 #include <vm/vm_kern.h>
67 #include <vm/vm_map.h>
68 #include <vm/vm_extern.h>
69 #include <vm/vm_object.h>
70 #include <vm/vm_zone.h>
71 #include <vm/swap_pager.h>
73 #include <machine/frame.h>
74 #include <machine/limits.h>
75 #include <machine/psl.h>
76 #include <machine/sysarch.h>
78 #include <machine/segments.h>
81 #include <sys/sched.h>
83 #include <emulation/linux/linux_sysproto.h>
84 #include <arch_linux/linux.h>
85 #include <arch_linux/linux_proto.h>
86 #include "linux_mib.h"
87 #include "linux_util.h"
88 #include "linux_emuldata.h"
89 #include "i386/linux.h"
91 #define BSD_TO_LINUX_SIGNAL(sig) \
92 (((sig) <= LINUX_SIGTBLSZ) ? bsd_to_linux_signal[_SIG_IDX(sig)] : sig)
94 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
95 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
96 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
101 l_long uptime; /* Seconds since boot */
102 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */
103 l_ulong totalram; /* Total usable main memory size */
104 l_ulong freeram; /* Available memory size */
105 l_ulong sharedram; /* Amount of shared memory */
106 l_ulong bufferram; /* Memory used by buffers */
107 l_ulong totalswap; /* Total swap space size */
108 l_ulong freeswap; /* swap space still available */
109 l_ushort procs; /* Number of current processes */
110 l_ushort pad; /* explicit padding */
111 l_ulong totalhigh; /* Total high memory size */
112 l_ulong freehigh; /* Available high memory size */
113 l_uint mem_unit; /* Memory unit size in bytes */
114 char _f[20-2*sizeof(l_long)-sizeof(l_int)]; /* Padding for libc5 */
118 sys_linux_madvise(struct linux_madvise_args *args)
127 sys_linux_sysinfo(struct linux_sysinfo_args *args)
129 struct l_sysinfo sysinfo;
135 /* Uptime is copied out of print_uptime() in kern_shutdown.c */
138 if (ts.tv_sec >= 86400) {
142 if (i || ts.tv_sec >= 3600) {
146 if (i || ts.tv_sec >= 60) {
151 bzero(&sysinfo, sizeof(sysinfo));
152 sysinfo.uptime=ts.tv_sec;
154 /* Use the information from the mib to get our load averages */
155 for (i = 0; i < 3; i++)
156 sysinfo.loads[i] = averunnable.ldavg[i];
158 sysinfo.totalram = Maxmem * PAGE_SIZE;
159 sysinfo.freeram = sysinfo.totalram - vmstats.v_wire_count * PAGE_SIZE;
160 sysinfo.sharedram = 0;
162 for (n = 0; n < ncpus; ++n) {
163 globaldata_t gd = globaldata_find(n);
165 sysinfo.sharedram += gd->gd_vmtotal.t_avmshr;
167 sysinfo.sharedram *= PAGE_SIZE;
168 sysinfo.bufferram = 0;
170 if (swapblist == NULL) {
171 sysinfo.totalswap= 0;
172 sysinfo.freeswap = 0;
174 sysinfo.totalswap = swapblist->bl_blocks * 1024;
175 sysinfo.freeswap = swapblist->bl_root->u.bmu_avail * PAGE_SIZE;
178 sysinfo.procs = nprocs;
179 sysinfo.totalhigh = 0;
180 sysinfo.freehigh = 0;
181 sysinfo.mem_unit = 1; /* Set the basic mem unit to 1 */
183 error = copyout(&sysinfo, (caddr_t)args->info, sizeof(sysinfo));
191 sys_linux_alarm(struct linux_alarm_args *args)
193 struct thread *td = curthread;
194 struct proc *p = td->td_proc;
195 struct itimerval it, old_it;
200 kprintf(ARGS(alarm, "%u"), args->secs);
203 if (args->secs > 100000000)
206 it.it_value.tv_sec = (long)args->secs;
207 it.it_value.tv_usec = 0;
208 it.it_interval.tv_sec = 0;
209 it.it_interval.tv_usec = 0;
212 old_it = p->p_realtimer;
214 if (timevalisset(&old_it.it_value))
215 callout_stop(&p->p_ithandle);
216 if (it.it_value.tv_sec != 0) {
217 callout_reset(&p->p_ithandle, tvtohz_high(&it.it_value),
219 timevaladd(&it.it_value, &tv);
224 if (timevalcmp(&old_it.it_value, &tv, >)) {
225 timevalsub(&old_it.it_value, &tv);
226 if (old_it.it_value.tv_usec != 0)
227 old_it.it_value.tv_sec++;
228 args->sysmsg_result = old_it.it_value.tv_sec;
237 sys_linux_brk(struct linux_brk_args *args)
239 struct thread *td = curthread;
240 struct proc *p = td->td_proc;
242 vm_offset_t new, old;
243 struct obreak_args bsd_args;
249 kprintf(ARGS(brk, "%p"), (void *)args->dsend);
251 old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
252 new = (vm_offset_t)args->dsend;
253 bsd_args.sysmsg_result = 0;
254 bsd_args.nsize = (char *) new;
255 bsd_args.sysmsg_result = 0;
256 if (((caddr_t)new > vm->vm_daddr) && !sys_obreak(&bsd_args))
257 args->sysmsg_result = (long)new;
259 args->sysmsg_result = (long)old;
269 sys_linux_uselib(struct linux_uselib_args *args)
271 struct thread *td = curthread;
273 struct nlookupdata nd;
278 unsigned long file_offset;
280 unsigned long bss_size;
287 error = linux_copyin_path(args->library, &path, LINUX_PATH_EXISTS);
292 kprintf(ARGS(uselib, "%s"), path);
300 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
301 nd.nl_flags |= NLC_EXEC;
303 error = nlookup(&nd);
305 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
309 * From here on down, we have a locked vnode that must be unlocked.
314 if (vp->v_writecount) {
320 error = VOP_GETATTR(vp, &attr);
324 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
325 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
331 if (attr.va_size == 0) {
336 error = VOP_OPEN(vp, FREAD, td->td_ucred, NULL);
341 * Lock no longer needed
346 /* Pull in executable header into kernel_map */
347 error = vm_mmap(&kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE,
348 VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp, 0);
352 /* Is it a Linux binary ? */
353 if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
359 * While we are here, we should REALLY do some more checks
362 /* Set file/virtual offset based on a.out variant. */
363 switch ((int)(a_out->a_magic & 0xffff)) {
364 case 0413: /* ZMAGIC */
367 case 0314: /* QMAGIC */
375 bss_size = round_page(a_out->a_bss);
377 /* Check various fields in header for validity/bounds. */
378 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
383 /* text + data can't exceed file size */
384 if (a_out->a_data + a_out->a_text > attr.va_size) {
390 * text/data/bss must not exceed limits
391 * XXX - this is not complete. it should check current usage PLUS
392 * the resources needed by this library.
394 if (a_out->a_text > maxtsiz ||
395 a_out->a_data + bss_size > p->p_rlimit[RLIMIT_DATA].rlim_cur) {
400 /* prevent more writers */
401 vsetflags(vp, VTEXT);
404 * Check if file_offset page aligned. Currently we cannot handle
405 * misalinged file offsets, and so we read in the entire image
408 if (file_offset & PAGE_MASK) {
410 kprintf("uselib: Non page aligned binary %lu\n", file_offset);
412 /* Map text+data read/write/execute */
414 /* a_entry is the load address and is page aligned */
415 vmaddr = trunc_page(a_out->a_entry);
417 /* get anon user mapping, read+write+execute */
418 error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0,
419 &vmaddr, a_out->a_text + a_out->a_data,
421 FALSE, VM_MAPTYPE_NORMAL,
422 VM_PROT_ALL, VM_PROT_ALL,
427 /* map file into kernel_map */
428 error = vm_mmap(&kernel_map, &buffer,
429 round_page(a_out->a_text + a_out->a_data + file_offset),
430 VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp,
431 trunc_page(file_offset));
435 /* copy from kernel VM space to user space */
436 error = copyout((caddr_t)(uintptr_t)(buffer + file_offset),
437 (caddr_t)vmaddr, a_out->a_text + a_out->a_data);
439 /* release temporary kernel space */
440 vm_map_remove(&kernel_map, buffer, buffer +
441 round_page(a_out->a_text + a_out->a_data + file_offset));
447 kprintf("uselib: Page aligned binary %lu\n", file_offset);
450 * for QMAGIC, a_entry is 20 bytes beyond the load address
451 * to skip the executable header
453 vmaddr = trunc_page(a_out->a_entry);
456 * Map it all into the process's space as a single
457 * copy-on-write "data" segment.
459 error = vm_mmap(&p->p_vmspace->vm_map, &vmaddr,
460 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
461 MAP_PRIVATE | MAP_FIXED, (caddr_t)vp, file_offset);
466 kprintf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long*)vmaddr)[0],
470 /* Calculate BSS start address */
471 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
474 /* allocate some 'anon' space */
475 error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0,
478 FALSE, VM_MAPTYPE_NORMAL,
479 VM_PROT_ALL, VM_PROT_ALL,
486 /* Unlock/release vnode */
492 /* Release the kernel mapping. */
494 vm_map_remove(&kernel_map, (vm_offset_t)a_out,
495 (vm_offset_t)a_out + PAGE_SIZE);
499 linux_free_path(&path);
507 sys_linux_select(struct linux_select_args *args)
509 struct select_args bsa;
510 struct timeval tv0, tv1, utv, *tvp;
516 kprintf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
517 (void *)args->readfds, (void *)args->writefds,
518 (void *)args->exceptfds, (void *)args->timeout);
522 bsa.sysmsg_result = 0;
524 bsa.in = args->readfds;
525 bsa.ou = args->writefds;
526 bsa.ex = args->exceptfds;
527 bsa.tv = (struct timeval *)args->timeout;
530 * Store current time for computation of the amount of
534 if ((error = copyin((caddr_t)args->timeout, &utv,
539 kprintf(LMSG("incoming timeout (%ld/%ld)"),
540 utv.tv_sec, utv.tv_usec);
543 if (itimerfix(&utv)) {
545 * The timeval was invalid. Convert it to something
546 * valid that will act as it does under Linux.
548 sg = stackgap_init();
549 tvp = stackgap_alloc(&sg, sizeof(utv));
550 utv.tv_sec += utv.tv_usec / 1000000;
551 utv.tv_usec %= 1000000;
552 if (utv.tv_usec < 0) {
554 utv.tv_usec += 1000000;
558 if ((error = copyout(&utv, tvp, sizeof(utv))))
565 error = sys_select(&bsa);
566 args->sysmsg_result = bsa.sysmsg_result;
569 kprintf(LMSG("real select returns %d"), error);
573 * See fs/select.c in the Linux kernel. Without this,
574 * Maelstrom doesn't work.
576 if (error == ERESTART)
582 if (args->sysmsg_result) {
584 * Compute how much time was left of the timeout,
585 * by subtracting the current time and the time
586 * before we started the call, and subtracting
587 * that result from the user-supplied value.
590 timevalsub(&tv1, &tv0);
591 timevalsub(&utv, &tv1);
598 kprintf(LMSG("outgoing timeout (%ld/%ld)"),
599 utv.tv_sec, utv.tv_usec);
601 if ((error = copyout(&utv, (caddr_t)args->timeout,
609 kprintf(LMSG("select_out -> %d"), error);
618 sys_linux_mremap(struct linux_mremap_args *args)
620 struct munmap_args bsd_args;
625 kprintf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
627 (unsigned long)args->old_len,
628 (unsigned long)args->new_len,
629 (unsigned long)args->flags);
631 if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) {
632 args->sysmsg_resultp = NULL;
637 * Check for the page alignment.
638 * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK.
640 if (args->addr & PAGE_MASK) {
641 args->sysmsg_resultp = NULL;
645 args->new_len = round_page(args->new_len);
646 args->old_len = round_page(args->old_len);
648 if (args->new_len > args->old_len) {
649 args->sysmsg_result = 0;
653 if (args->new_len < args->old_len) {
654 bsd_args.sysmsg_result = 0;
655 bsd_args.addr = (caddr_t)(args->addr + args->new_len);
656 bsd_args.len = args->old_len - args->new_len;
657 error = sys_munmap(&bsd_args);
660 args->sysmsg_resultp = error ? NULL : (void *)args->addr;
664 #define LINUX_MS_ASYNC 0x0001
665 #define LINUX_MS_INVALIDATE 0x0002
666 #define LINUX_MS_SYNC 0x0004
672 sys_linux_msync(struct linux_msync_args *args)
674 struct msync_args bsd_args;
677 bsd_args.addr = (caddr_t)args->addr;
678 bsd_args.len = args->len;
679 bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
680 bsd_args.sysmsg_result = 0;
682 error = sys_msync(&bsd_args);
683 args->sysmsg_result = bsd_args.sysmsg_result;
691 sys_linux_time(struct linux_time_args *args)
699 kprintf(ARGS(time, "*"));
704 if (args->tm && (error = copyout(&tm, (caddr_t)args->tm, sizeof(tm))))
706 args->sysmsg_lresult = tm;
710 struct l_times_argv {
717 #define CLK_TCK 100 /* Linux uses 100 */
719 #define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
725 sys_linux_times(struct linux_times_args *args)
727 struct thread *td = curthread;
728 struct proc *p = td->td_proc;
730 struct l_times_argv tms;
736 kprintf(ARGS(times, "*"));
743 tms.tms_utime = CONVTCK(ru.ru_utime);
744 tms.tms_stime = CONVTCK(ru.ru_stime);
746 tms.tms_cutime = CONVTCK(p->p_cru.ru_utime);
747 tms.tms_cstime = CONVTCK(p->p_cru.ru_stime);
749 if ((error = copyout(&tms, (caddr_t)args->buf, sizeof(tms))))
753 args->sysmsg_result = (int)CONVTCK(tv);
761 sys_linux_newuname(struct linux_newuname_args *args)
763 struct thread *td = curthread;
764 struct l_new_utsname utsname;
765 char *osrelease, *osname;
768 if (ldebug(newuname))
769 kprintf(ARGS(newuname, "*"));
773 osname = linux_get_osname(td);
774 osrelease = linux_get_osrelease(td);
776 bzero(&utsname, sizeof(utsname));
777 strncpy(utsname.sysname, osname, LINUX_MAX_UTSNAME-1);
778 strncpy(utsname.nodename, hostname, LINUX_MAX_UTSNAME-1);
779 strncpy(utsname.release, osrelease, LINUX_MAX_UTSNAME-1);
780 strncpy(utsname.version, version, LINUX_MAX_UTSNAME-1);
781 strncpy(utsname.machine, machine, LINUX_MAX_UTSNAME-1);
782 strncpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME-1);
785 return (copyout(&utsname, (caddr_t)args->buf, sizeof(utsname)));
788 /* XXX: why would this be i386-only? most of these are wrong! */
789 #if defined(__i386__)
799 sys_linux_utime(struct linux_utime_args *args)
801 struct timeval tv[2];
802 struct l_utimbuf lut;
803 struct nlookupdata nd;
807 error = linux_copyin_path(args->fname, &path, LINUX_PATH_EXISTS);
812 kprintf(ARGS(utime, "%s, *"), path);
816 error = copyin(args->times, &lut, sizeof(lut));
819 tv[0].tv_sec = lut.l_actime;
821 tv[1].tv_sec = lut.l_modtime;
825 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
827 error = kern_utimes(&nd, args->times ? tv : NULL);
831 linux_free_path(&path);
836 sys_linux_utimes(struct linux_utimes_args *args)
839 struct timeval tv[2], *tvp = NULL;
840 struct nlookupdata nd;
844 error = linux_copyin_path(args->fname, &path, LINUX_PATH_EXISTS);
849 kprintf(ARGS(utimes, "%s, *"), path);
853 error = copyin(args->tptr, ltv, sizeof(ltv));
856 tv[0].tv_sec = ltv[0].tv_sec;
857 tv[0].tv_usec = ltv[0].tv_usec;
858 tv[1].tv_sec = ltv[1].tv_sec;
859 tv[1].tv_usec = ltv[1].tv_usec;
863 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
865 error = kern_utimes(&nd, tvp);
869 linux_free_path(&path);
874 sys_linux_futimesat(struct linux_futimesat_args *args)
877 struct timeval tv[2], *tvp = NULL;
879 struct nlookupdata nd;
883 error = linux_copyin_path(args->fname, &path, LINUX_PATH_EXISTS);
887 if (ldebug(futimesat))
888 kprintf(ARGS(futimesat, "%s, *"), path);
891 error = copyin(args->tptr, ltv, sizeof(ltv));
894 tv[0].tv_sec = ltv[0].tv_sec;
895 tv[0].tv_usec = ltv[0].tv_usec;
896 tv[1].tv_sec = ltv[1].tv_sec;
897 tv[1].tv_usec = ltv[1].tv_usec;
900 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
902 error = nlookup_init_at(&nd, &fp, dfd, path, UIO_SYSSPACE, NLC_FOLLOW);
904 error = kern_utimes(&nd, tvp);
905 nlookup_done_at(&nd, fp);
908 linux_free_path(&path);
914 sys_linux_utimensat(struct linux_utimensat_args *args)
916 struct l_timespec ltv[2];
917 struct timeval tv[2], *tvp = NULL;
919 struct nlookupdata nd;
921 int dfd, flags, error = 0;
923 if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
926 if (args->dfd == LINUX_AT_FDCWD && args->fname == NULL)
930 error = linux_copyin_path(args->fname, &path, LINUX_PATH_EXISTS);
935 if (ldebug(utimensat))
936 kprintf(ARGS(utimensat, "%s, *"), path);
939 error = copyin(args->tptr, ltv, sizeof(ltv));
943 if (ltv[0].tv_sec == LINUX_UTIME_NOW) {
945 } else if (ltv[0].tv_sec == LINUX_UTIME_OMIT) {
946 /* XXX: this is not right, but will do for now */
949 tv[0].tv_sec = ltv[0].tv_sec;
950 /* XXX: we lose precision here, as we don't have ns */
951 tv[0].tv_usec = ltv[0].tv_nsec/1000;
953 if (ltv[1].tv_sec == LINUX_UTIME_NOW) {
955 } else if (ltv[1].tv_sec == LINUX_UTIME_OMIT) {
956 /* XXX: this is not right, but will do for now */
959 tv[1].tv_sec = ltv[1].tv_sec;
960 /* XXX: we lose precision here, as we don't have ns */
961 tv[1].tv_usec = ltv[1].tv_nsec/1000;
966 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
967 flags = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
971 error = nlookup_init_at(&nd, &fp, dfd, path, UIO_SYSSPACE, flags);
973 error = kern_utimes(&nd, tvp);
974 nlookup_done_at(&nd, fp);
976 /* Thank you, Linux, for another non-standard "feature" */
977 KKASSERT(dfd != AT_FDCWD);
978 error = kern_futimes(dfd, tvp);
983 linux_free_path(&path);
987 #endif /* __i386__ */
989 #define __WCLONE 0x80000000
995 sys_linux_waitpid(struct linux_waitpid_args *args)
997 int error, options, status;
1000 if (ldebug(waitpid))
1001 kprintf(ARGS(waitpid, "%d, %p, %d"),
1002 args->pid, (void *)args->status, args->options);
1004 options = args->options & (WNOHANG | WUNTRACED);
1005 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
1006 if (args->options & __WCLONE)
1007 options |= WLINUXCLONE;
1009 error = kern_wait(args->pid, args->status ? &status : NULL, options,
1010 NULL, &args->sysmsg_result);
1012 if (error == 0 && args->status) {
1014 if (WIFSIGNALED(status))
1015 status = (status & 0xffffff80) |
1016 BSD_TO_LINUX_SIGNAL(WTERMSIG(status));
1017 else if (WIFSTOPPED(status))
1018 status = (status & 0xffff00ff) |
1019 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(status)) << 8);
1020 error = copyout(&status, args->status, sizeof(status));
1030 sys_linux_wait4(struct linux_wait4_args *args)
1032 struct thread *td = curthread;
1033 struct lwp *lp = td->td_lwp;
1034 struct rusage rusage;
1035 int error, options, status;
1039 kprintf(ARGS(wait4, "%d, %p, %d, %p"),
1040 args->pid, (void *)args->status, args->options,
1041 (void *)args->rusage);
1043 options = args->options & (WNOHANG | WUNTRACED);
1044 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
1045 if (args->options & __WCLONE)
1046 options |= WLINUXCLONE;
1048 error = kern_wait(args->pid, args->status ? &status : NULL, options,
1049 args->rusage ? &rusage : NULL, &args->sysmsg_result);
1052 spin_lock(&lp->lwp_spin);
1053 lwp_delsig(lp, SIGCHLD);
1054 spin_unlock(&lp->lwp_spin);
1057 if (error == 0 && args->status) {
1059 if (WIFSIGNALED(status))
1060 status = (status & 0xffffff80) |
1061 BSD_TO_LINUX_SIGNAL(WTERMSIG(status));
1062 else if (WIFSTOPPED(status))
1063 status = (status & 0xffff00ff) |
1064 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(status)) << 8);
1065 error = copyout(&status, args->status, sizeof(status));
1067 if (error == 0 && args->rusage)
1068 error = copyout(&rusage, args->rusage, sizeof(rusage));
1077 sys_linux_mknod(struct linux_mknod_args *args)
1079 struct nlookupdata nd;
1083 error = linux_copyin_path(args->path, &path, LINUX_PATH_CREATE);
1088 kprintf(ARGS(mknod, "%s, %d, %d"),
1089 path, args->mode, args->dev);
1092 error = nlookup_init(&nd, path, UIO_SYSSPACE, 0);
1094 if (args->mode & S_IFIFO) {
1095 error = kern_mkfifo(&nd, args->mode);
1097 error = kern_mknod(&nd, args->mode,
1105 linux_free_path(&path);
1110 sys_linux_mknodat(struct linux_mknodat_args *args)
1112 struct nlookupdata nd;
1117 error = linux_copyin_path(args->path, &path, LINUX_PATH_CREATE);
1122 kprintf(ARGS(mknod, "%s, %d, %d"),
1123 path, args->mode, args->dev);
1126 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1127 error = nlookup_init_at(&nd, &fp, dfd, path, UIO_SYSSPACE, 0);
1129 if (args->mode & S_IFIFO) {
1130 error = kern_mkfifo(&nd, args->mode);
1132 error = kern_mknod(&nd, args->mode,
1137 nlookup_done_at(&nd, fp);
1140 linux_free_path(&path);
1145 * UGH! This is just about the dumbest idea I've ever heard!!
1150 sys_linux_personality(struct linux_personality_args *args)
1153 if (ldebug(personality))
1154 kprintf(ARGS(personality, "%d"), args->per);
1159 /* Yes Jim, it's still a Linux... */
1160 args->sysmsg_result = 0;
1165 * Wrappers for get/setitimer for debugging..
1170 sys_linux_setitimer(struct linux_setitimer_args *args)
1172 struct setitimer_args bsa;
1173 struct itimerval foo;
1177 if (ldebug(setitimer))
1178 kprintf(ARGS(setitimer, "%p, %p"),
1179 (void *)args->itv, (void *)args->oitv);
1181 bsa.which = args->which;
1182 bsa.itv = (struct itimerval *)args->itv;
1183 bsa.oitv = (struct itimerval *)args->oitv;
1184 bsa.sysmsg_result = 0;
1186 if ((error = copyin((caddr_t)args->itv, &foo, sizeof(foo))))
1189 if (ldebug(setitimer)) {
1190 kprintf("setitimer: value: sec: %ld, usec: %ld\n",
1191 foo.it_value.tv_sec, foo.it_value.tv_usec);
1192 kprintf("setitimer: interval: sec: %ld, usec: %ld\n",
1193 foo.it_interval.tv_sec, foo.it_interval.tv_usec);
1197 error = sys_setitimer(&bsa);
1198 args->sysmsg_result = bsa.sysmsg_result;
1206 sys_linux_getitimer(struct linux_getitimer_args *args)
1208 struct getitimer_args bsa;
1211 if (ldebug(getitimer))
1212 kprintf(ARGS(getitimer, "%p"), (void *)args->itv);
1214 bsa.which = args->which;
1215 bsa.itv = (struct itimerval *)args->itv;
1216 bsa.sysmsg_result = 0;
1217 error = sys_getitimer(&bsa);
1218 args->sysmsg_result = bsa.sysmsg_result;
1226 sys_linux_nice(struct linux_nice_args *args)
1228 struct setpriority_args bsd_args;
1231 bsd_args.which = PRIO_PROCESS;
1232 bsd_args.who = 0; /* current process */
1233 bsd_args.prio = args->inc;
1234 bsd_args.sysmsg_result = 0;
1235 error = sys_setpriority(&bsd_args);
1236 args->sysmsg_result = bsd_args.sysmsg_result;
1244 sys_linux_setgroups(struct linux_setgroups_args *args)
1246 struct thread *td = curthread;
1247 struct proc *p = td->td_proc;
1248 struct ucred *newcred, *oldcred;
1249 l_gid_t linux_gidset[NGROUPS];
1253 ngrp = args->gidsetsize;
1254 oldcred = td->td_ucred;
1257 * cr_groups[0] holds egid. Setting the whole set from
1258 * the supplied set will cause egid to be changed too.
1259 * Keep cr_groups[0] unchanged to prevent that.
1262 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0)
1265 if ((u_int)ngrp >= NGROUPS)
1269 newcred = crdup(oldcred);
1271 error = copyin((caddr_t)args->grouplist, linux_gidset,
1272 ngrp * sizeof(l_gid_t));
1279 newcred->cr_ngroups = ngrp + 1;
1281 bsd_gidset = newcred->cr_groups;
1284 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1288 newcred->cr_ngroups = 1;
1292 oldcred = p->p_ucred; /* reload, deal with threads race */
1293 p->p_ucred = newcred;
1303 sys_linux_getgroups(struct linux_getgroups_args *args)
1305 struct thread *td = curthread;
1307 l_gid_t linux_gidset[NGROUPS];
1309 int bsd_gidsetsz, ngrp, error;
1311 cred = td->td_ucred;
1312 bsd_gidset = cred->cr_groups;
1313 bsd_gidsetsz = cred->cr_ngroups - 1;
1316 * cr_groups[0] holds egid. Returning the whole set
1317 * here will cause a duplicate. Exclude cr_groups[0]
1321 if ((ngrp = args->gidsetsize) == 0) {
1322 args->sysmsg_result = bsd_gidsetsz;
1326 if ((u_int)ngrp < bsd_gidsetsz)
1330 while (ngrp < bsd_gidsetsz) {
1331 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1335 if ((error = copyout(linux_gidset, args->grouplist,
1336 ngrp * sizeof(l_gid_t)))) {
1340 args->sysmsg_result = ngrp;
1348 sys_linux_setrlimit(struct linux_setrlimit_args *args)
1350 struct l_rlimit linux_rlim;
1356 if (ldebug(setrlimit))
1357 kprintf(ARGS(setrlimit, "%d, %p"),
1358 args->resource, (void *)args->rlim);
1360 if (args->resource >= LINUX_RLIM_NLIMITS)
1362 which = linux_to_bsd_resource[args->resource];
1366 error = copyin(args->rlim, &linux_rlim, sizeof(linux_rlim));
1369 rlim.rlim_cur = (rlim_t)linux_rlim.rlim_cur;
1370 rlim.rlim_max = (rlim_t)linux_rlim.rlim_max;
1372 error = kern_setrlimit(which, &rlim);
1381 sys_linux_old_getrlimit(struct linux_old_getrlimit_args *args)
1383 struct l_rlimit linux_rlim;
1389 if (ldebug(old_getrlimit))
1390 kprintf(ARGS(old_getrlimit, "%d, %p"),
1391 args->resource, (void *)args->rlim);
1393 if (args->resource >= LINUX_RLIM_NLIMITS)
1395 which = linux_to_bsd_resource[args->resource];
1399 error = kern_getrlimit(which, &rlim);
1402 linux_rlim.rlim_cur = (l_ulong)rlim.rlim_cur;
1403 if (linux_rlim.rlim_cur == ULONG_MAX)
1404 linux_rlim.rlim_cur = LONG_MAX;
1405 linux_rlim.rlim_max = (l_ulong)rlim.rlim_max;
1406 if (linux_rlim.rlim_max == ULONG_MAX)
1407 linux_rlim.rlim_max = LONG_MAX;
1408 error = copyout(&linux_rlim, args->rlim, sizeof(linux_rlim));
1417 sys_linux_getrlimit(struct linux_getrlimit_args *args)
1419 struct l_rlimit linux_rlim;
1425 if (ldebug(getrlimit))
1426 kprintf(ARGS(getrlimit, "%d, %p"),
1427 args->resource, (void *)args->rlim);
1429 if (args->resource >= LINUX_RLIM_NLIMITS)
1431 which = linux_to_bsd_resource[args->resource];
1435 error = kern_getrlimit(which, &rlim);
1438 linux_rlim.rlim_cur = (l_ulong)rlim.rlim_cur;
1439 linux_rlim.rlim_max = (l_ulong)rlim.rlim_max;
1440 error = copyout(&linux_rlim, args->rlim, sizeof(linux_rlim));
1449 sys_linux_sched_setscheduler(struct linux_sched_setscheduler_args *args)
1451 struct sched_setscheduler_args bsd;
1455 if (ldebug(sched_setscheduler))
1456 kprintf(ARGS(sched_setscheduler, "%d, %d, %p"),
1457 args->pid, args->policy, (const void *)args->param);
1460 switch (args->policy) {
1461 case LINUX_SCHED_OTHER:
1462 bsd.policy = SCHED_OTHER;
1464 case LINUX_SCHED_FIFO:
1465 bsd.policy = SCHED_FIFO;
1467 case LINUX_SCHED_RR:
1468 bsd.policy = SCHED_RR;
1474 bsd.pid = args->pid;
1475 bsd.param = (struct sched_param *)args->param;
1476 bsd.sysmsg_result = 0;
1478 error = sys_sched_setscheduler(&bsd);
1479 args->sysmsg_result = bsd.sysmsg_result;
1487 sys_linux_sched_getscheduler(struct linux_sched_getscheduler_args *args)
1489 struct sched_getscheduler_args bsd;
1493 if (ldebug(sched_getscheduler))
1494 kprintf(ARGS(sched_getscheduler, "%d"), args->pid);
1497 bsd.sysmsg_result = 0;
1498 bsd.pid = args->pid;
1499 error = sys_sched_getscheduler(&bsd);
1500 args->sysmsg_result = bsd.sysmsg_result;
1502 switch (args->sysmsg_result) {
1504 args->sysmsg_result = LINUX_SCHED_OTHER;
1507 args->sysmsg_result = LINUX_SCHED_FIFO;
1510 args->sysmsg_result = LINUX_SCHED_RR;
1520 sys_linux_sched_get_priority_max(struct linux_sched_get_priority_max_args *args)
1522 struct sched_get_priority_max_args bsd;
1526 if (ldebug(sched_get_priority_max))
1527 kprintf(ARGS(sched_get_priority_max, "%d"), args->policy);
1530 switch (args->policy) {
1531 case LINUX_SCHED_OTHER:
1532 bsd.policy = SCHED_OTHER;
1534 case LINUX_SCHED_FIFO:
1535 bsd.policy = SCHED_FIFO;
1537 case LINUX_SCHED_RR:
1538 bsd.policy = SCHED_RR;
1543 bsd.sysmsg_result = 0;
1545 error = sys_sched_get_priority_max(&bsd);
1546 args->sysmsg_result = bsd.sysmsg_result;
1554 sys_linux_sched_get_priority_min(struct linux_sched_get_priority_min_args *args)
1556 struct sched_get_priority_min_args bsd;
1560 if (ldebug(sched_get_priority_min))
1561 kprintf(ARGS(sched_get_priority_min, "%d"), args->policy);
1564 switch (args->policy) {
1565 case LINUX_SCHED_OTHER:
1566 bsd.policy = SCHED_OTHER;
1568 case LINUX_SCHED_FIFO:
1569 bsd.policy = SCHED_FIFO;
1571 case LINUX_SCHED_RR:
1572 bsd.policy = SCHED_RR;
1577 bsd.sysmsg_result = 0;
1579 error = sys_sched_get_priority_min(&bsd);
1580 args->sysmsg_result = bsd.sysmsg_result;
1584 #define REBOOT_CAD_ON 0x89abcdef
1585 #define REBOOT_CAD_OFF 0
1586 #define REBOOT_HALT 0xcdef0123
1587 #define REBOOT_RESTART 0x01234567
1588 #define REBOOT_RESTART2 0xA1B2C3D4
1589 #define REBOOT_POWEROFF 0x4321FEDC
1590 #define REBOOT_MAGIC1 0xfee1dead
1591 #define REBOOT_MAGIC2 0x28121969
1592 #define REBOOT_MAGIC2A 0x05121996
1593 #define REBOOT_MAGIC2B 0x16041998
1599 sys_linux_reboot(struct linux_reboot_args *args)
1601 struct reboot_args bsd_args;
1606 kprintf(ARGS(reboot, "0x%x"), args->cmd);
1609 if ((args->magic1 != REBOOT_MAGIC1) ||
1610 ((args->magic2 != REBOOT_MAGIC2) &&
1611 (args->magic2 != REBOOT_MAGIC2A) &&
1612 (args->magic2 != REBOOT_MAGIC2B)))
1615 switch (args->cmd) {
1617 case REBOOT_CAD_OFF:
1618 return (priv_check(curthread, PRIV_REBOOT));
1621 bsd_args.opt = RB_HALT;
1623 case REBOOT_RESTART:
1624 case REBOOT_RESTART2:
1627 case REBOOT_POWEROFF:
1628 bsd_args.opt = RB_POWEROFF;
1635 bsd_args.sysmsg_result = 0;
1637 error = sys_reboot(&bsd_args);
1638 args->sysmsg_result = bsd_args.sysmsg_result;
1643 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1644 * p->p_retval[1] when COMPAT_43 is defined. This
1645 * globbers registers that are assumed to be preserved. The following
1646 * lightweight syscalls fixes this. See also linux_getgid16() and
1647 * linux_getuid16() in linux_uid16.c.
1649 * linux_getpid() - MP SAFE
1650 * linux_getgid() - MP SAFE
1651 * linux_getuid() - MP SAFE
1658 sys_linux_getpid(struct linux_getpid_args *args)
1660 struct linux_emuldata *em;
1661 struct proc *p = curproc;
1665 em = emuldata_get(p);
1666 if (em == NULL) /* this should never happen */
1667 args->sysmsg_result = p->p_pid;
1669 args->sysmsg_result = em->s->group_pid;
1679 sys_linux_getppid(struct linux_getppid_args *args)
1681 struct linux_emuldata *em;
1682 struct proc *parent;
1687 em = emuldata_get(curproc);
1688 KKASSERT(em != NULL);
1689 group_pid = em->s->group_pid;
1692 p = pfind(group_pid);
1693 /* We are not allowed to fail */
1698 if (parent->p_sysent == &elf_linux_sysvec) {
1700 em = emuldata_get(parent);
1701 args->sysmsg_result = em->s->group_pid;
1704 args->sysmsg_result = parent->p_pid;
1716 sys_linux_getgid(struct linux_getgid_args *args)
1718 struct thread *td = curthread;
1720 args->sysmsg_result = td->td_ucred->cr_rgid;
1728 sys_linux_getuid(struct linux_getuid_args *args)
1730 struct thread *td = curthread;
1732 args->sysmsg_result = td->td_ucred->cr_ruid;
1740 sys_linux_getsid(struct linux_getsid_args *args)
1742 struct getsid_args bsd;
1745 bsd.sysmsg_result = 0;
1746 bsd.pid = args->pid;
1747 error = sys_getsid(&bsd);
1748 args->sysmsg_result = bsd.sysmsg_result;
1756 linux_nosys(struct nosys_args *args)
1763 sys_linux_mq_open(struct linux_mq_open_args *args)
1765 struct mq_open_args moa;
1769 if (args->oflag & LINUX_O_RDONLY)
1771 if (args->oflag & LINUX_O_WRONLY)
1773 if (args->oflag & LINUX_O_RDWR)
1776 if (args->oflag & LINUX_O_NONBLOCK)
1777 oflag |= O_NONBLOCK;
1778 if (args->oflag & LINUX_O_CREAT)
1780 if (args->oflag & LINUX_O_EXCL)
1783 moa.name = args->name;
1785 moa.mode = args->mode;
1786 moa.attr = args->attr;
1788 error = sys_mq_open(&moa);
1794 sys_linux_mq_getsetattr(struct linux_mq_getsetattr_args *args)
1796 struct mq_getattr_args gaa;
1797 struct mq_setattr_args saa;
1800 gaa.mqdes = args->mqd;
1801 gaa.mqstat = args->oattr;
1803 saa.mqdes = args->mqd;
1804 saa.mqstat = args->attr;
1805 saa.mqstat = args->oattr;
1807 if (args->attr != NULL) {
1808 error = sys_mq_setattr(&saa);
1810 error = sys_mq_getattr(&gaa);
1817 * Get affinity of a process.
1820 sys_linux_sched_getaffinity(struct linux_sched_getaffinity_args *args)
1828 if (ldebug(sched_getaffinity))
1829 kprintf(ARGS(sched_getaffinity, "%d, %d, *"), args->pid,
1832 if (args->len < sizeof(cpumask_t))
1835 if ((error = priv_check(curthread, PRIV_SCHED_CPUSET)) != 0)
1838 /* Get the mplock to ensure that the proc is not running */
1840 if (args->pid == 0) {
1844 p = pfind(args->pid);
1851 lp = FIRST_LWP_IN_PROC(p);
1853 * XXX: if lwp_cpumask is ever changed to support more than
1854 * 32 processors, this needs to be changed to a bcopy.
1856 mask = lp->lwp_cpumask;
1857 if ((error = copyout(&mask, args->user_mask_ptr, sizeof(cpumask_t))))
1863 args->sysmsg_iresult = sizeof(cpumask_t);
1871 * Set affinity of a process.
1874 sys_linux_sched_setaffinity(struct linux_sched_setaffinity_args *args)
1877 if (ldebug(sched_setaffinity))
1878 kprintf(ARGS(sched_setaffinity, "%d, %d, *"), args->pid,
1882 * From Linux man page:
1883 * sched_setaffinity() sets the CPU affinity mask of the process
1884 * whose ID is pid to the value specified by mask. If pid is zero,
1885 * then the calling process is used. The argument cpusetsize is
1886 * the length (in bytes) of the data pointed to by mask. Normally
1887 * this argument would be specified as sizeof(cpu_set_t).
1889 * If the process specified by pid is not currently running on one
1890 * of the CPUs specified in mask, then that process is migrated to
1891 * one of the CPUs specified in mask.
1894 * About our implementation: I don't think that it is too important
1895 * to have a working implementation, but if it was ever needed,
1896 * the best approach would be to implement the whole mechanism
1897 * properly in kern_usched.
1898 * The idea has to be to change the affinity mask AND migrate the
1899 * lwp to one of the new valid CPUs for the lwp, in case the current
1900 * CPU isn't anymore in the affinity mask passed in.
1901 * For now, we'll just signal success even if we didn't do anything.
1907 sys_linux_gettid(struct linux_gettid_args *args)
1909 args->sysmsg_iresult = curproc->p_pid;
1914 sys_linux_getcpu(struct linux_getcpu_args *args)
1916 struct globaldata *gd;
1921 error = copyout(&gd->gd_cpuid, args->pcpu, sizeof(gd->gd_cpuid));
1925 * XXX: this should be the NUMA node, but since we don't implement it,
1926 * just return 0 for it.
1928 error = copyout(&node, args->pnode, sizeof(node));
1933 sys_linux_sethostname(struct linux_sethostname_args *uap)
1935 struct thread *td = curthread;
1942 name[1] = KERN_HOSTNAME;
1943 error = priv_check_cred(td->td_ucred, PRIV_SETHOSTNAME, 0);
1946 len = MIN(uap->len, MAXHOSTNAMELEN);
1947 hostname = kmalloc(MAXHOSTNAMELEN, M_TEMP, M_WAITOK);
1949 error = copyin(uap->hostname, hostname, len);
1951 kfree(hostname, M_TEMP);
1956 error = kernel_sysctl(name, 2, NULL, 0, hostname, len, NULL);
1959 kfree(hostname, M_TEMP);