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 lwkt_gettoken(&vmobj_token);
163 for (object = TAILQ_FIRST(&vm_object_list); object != NULL;
164 object = TAILQ_NEXT(object, object_list)) {
165 if (object->type == OBJT_MARKER)
167 if (object->shadow_count > 1)
168 sysinfo.sharedram += object->resident_page_count;
170 lwkt_reltoken(&vmobj_token);
172 sysinfo.sharedram *= PAGE_SIZE;
173 sysinfo.bufferram = 0;
175 if (swapblist == NULL) {
176 sysinfo.totalswap= 0;
177 sysinfo.freeswap = 0;
179 sysinfo.totalswap = swapblist->bl_blocks * 1024;
180 sysinfo.freeswap = swapblist->bl_root->u.bmu_avail * PAGE_SIZE;
183 sysinfo.procs = nprocs;
184 sysinfo.totalhigh = 0;
185 sysinfo.freehigh = 0;
186 sysinfo.mem_unit = 1; /* Set the basic mem unit to 1 */
188 error = copyout(&sysinfo, (caddr_t)args->info, sizeof(sysinfo));
196 sys_linux_alarm(struct linux_alarm_args *args)
198 struct thread *td = curthread;
199 struct proc *p = td->td_proc;
200 struct itimerval it, old_it;
205 kprintf(ARGS(alarm, "%u"), args->secs);
208 if (args->secs > 100000000)
211 it.it_value.tv_sec = (long)args->secs;
212 it.it_value.tv_usec = 0;
213 it.it_interval.tv_sec = 0;
214 it.it_interval.tv_usec = 0;
217 old_it = p->p_realtimer;
219 if (timevalisset(&old_it.it_value))
220 callout_stop(&p->p_ithandle);
221 if (it.it_value.tv_sec != 0) {
222 callout_reset(&p->p_ithandle, tvtohz_high(&it.it_value),
224 timevaladd(&it.it_value, &tv);
229 if (timevalcmp(&old_it.it_value, &tv, >)) {
230 timevalsub(&old_it.it_value, &tv);
231 if (old_it.it_value.tv_usec != 0)
232 old_it.it_value.tv_sec++;
233 args->sysmsg_result = old_it.it_value.tv_sec;
242 sys_linux_brk(struct linux_brk_args *args)
244 struct thread *td = curthread;
245 struct proc *p = td->td_proc;
247 vm_offset_t new, old;
248 struct obreak_args bsd_args;
254 kprintf(ARGS(brk, "%p"), (void *)args->dsend);
256 old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
257 new = (vm_offset_t)args->dsend;
258 bsd_args.sysmsg_result = 0;
259 bsd_args.nsize = (char *) new;
260 bsd_args.sysmsg_result = 0;
261 if (((caddr_t)new > vm->vm_daddr) && !sys_obreak(&bsd_args))
262 args->sysmsg_result = (long)new;
264 args->sysmsg_result = (long)old;
274 sys_linux_uselib(struct linux_uselib_args *args)
276 struct thread *td = curthread;
278 struct nlookupdata nd;
283 unsigned long file_offset;
285 unsigned long bss_size;
292 error = linux_copyin_path(args->library, &path, LINUX_PATH_EXISTS);
297 kprintf(ARGS(uselib, "%s"), path);
305 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
306 nd.nl_flags |= NLC_EXEC;
308 error = nlookup(&nd);
310 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
314 * From here on down, we have a locked vnode that must be unlocked.
319 if (vp->v_writecount) {
325 error = VOP_GETATTR(vp, &attr);
329 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
330 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
336 if (attr.va_size == 0) {
341 error = VOP_OPEN(vp, FREAD, td->td_ucred, NULL);
346 * Lock no longer needed
351 /* Pull in executable header into kernel_map */
352 error = vm_mmap(&kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE,
353 VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp, 0);
357 /* Is it a Linux binary ? */
358 if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
364 * While we are here, we should REALLY do some more checks
367 /* Set file/virtual offset based on a.out variant. */
368 switch ((int)(a_out->a_magic & 0xffff)) {
369 case 0413: /* ZMAGIC */
372 case 0314: /* QMAGIC */
380 bss_size = round_page(a_out->a_bss);
382 /* Check various fields in header for validity/bounds. */
383 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
388 /* text + data can't exceed file size */
389 if (a_out->a_data + a_out->a_text > attr.va_size) {
395 * text/data/bss must not exceed limits
396 * XXX - this is not complete. it should check current usage PLUS
397 * the resources needed by this library.
399 if (a_out->a_text > maxtsiz ||
400 a_out->a_data + bss_size > p->p_rlimit[RLIMIT_DATA].rlim_cur) {
405 /* prevent more writers */
406 vsetflags(vp, VTEXT);
409 * Check if file_offset page aligned. Currently we cannot handle
410 * misalinged file offsets, and so we read in the entire image
413 if (file_offset & PAGE_MASK) {
415 kprintf("uselib: Non page aligned binary %lu\n", file_offset);
417 /* Map text+data read/write/execute */
419 /* a_entry is the load address and is page aligned */
420 vmaddr = trunc_page(a_out->a_entry);
422 /* get anon user mapping, read+write+execute */
423 error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0,
424 &vmaddr, a_out->a_text + a_out->a_data,
426 FALSE, VM_MAPTYPE_NORMAL,
427 VM_PROT_ALL, VM_PROT_ALL,
432 /* map file into kernel_map */
433 error = vm_mmap(&kernel_map, &buffer,
434 round_page(a_out->a_text + a_out->a_data + file_offset),
435 VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp,
436 trunc_page(file_offset));
440 /* copy from kernel VM space to user space */
441 error = copyout((caddr_t)(uintptr_t)(buffer + file_offset),
442 (caddr_t)vmaddr, a_out->a_text + a_out->a_data);
444 /* release temporary kernel space */
445 vm_map_remove(&kernel_map, buffer, buffer +
446 round_page(a_out->a_text + a_out->a_data + file_offset));
452 kprintf("uselib: Page aligned binary %lu\n", file_offset);
455 * for QMAGIC, a_entry is 20 bytes beyond the load address
456 * to skip the executable header
458 vmaddr = trunc_page(a_out->a_entry);
461 * Map it all into the process's space as a single
462 * copy-on-write "data" segment.
464 error = vm_mmap(&p->p_vmspace->vm_map, &vmaddr,
465 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
466 MAP_PRIVATE | MAP_FIXED, (caddr_t)vp, file_offset);
471 kprintf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long*)vmaddr)[0],
475 /* Calculate BSS start address */
476 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
479 /* allocate some 'anon' space */
480 error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0,
483 FALSE, VM_MAPTYPE_NORMAL,
484 VM_PROT_ALL, VM_PROT_ALL,
491 /* Unlock/release vnode */
497 /* Release the kernel mapping. */
499 vm_map_remove(&kernel_map, (vm_offset_t)a_out,
500 (vm_offset_t)a_out + PAGE_SIZE);
504 linux_free_path(&path);
512 sys_linux_select(struct linux_select_args *args)
514 struct select_args bsa;
515 struct timeval tv0, tv1, utv, *tvp;
521 kprintf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
522 (void *)args->readfds, (void *)args->writefds,
523 (void *)args->exceptfds, (void *)args->timeout);
527 bsa.sysmsg_result = 0;
529 bsa.in = args->readfds;
530 bsa.ou = args->writefds;
531 bsa.ex = args->exceptfds;
532 bsa.tv = (struct timeval *)args->timeout;
535 * Store current time for computation of the amount of
539 if ((error = copyin((caddr_t)args->timeout, &utv,
544 kprintf(LMSG("incoming timeout (%ld/%ld)"),
545 utv.tv_sec, utv.tv_usec);
548 if (itimerfix(&utv)) {
550 * The timeval was invalid. Convert it to something
551 * valid that will act as it does under Linux.
553 sg = stackgap_init();
554 tvp = stackgap_alloc(&sg, sizeof(utv));
555 utv.tv_sec += utv.tv_usec / 1000000;
556 utv.tv_usec %= 1000000;
557 if (utv.tv_usec < 0) {
559 utv.tv_usec += 1000000;
563 if ((error = copyout(&utv, tvp, sizeof(utv))))
570 error = sys_select(&bsa);
571 args->sysmsg_result = bsa.sysmsg_result;
574 kprintf(LMSG("real select returns %d"), error);
578 * See fs/select.c in the Linux kernel. Without this,
579 * Maelstrom doesn't work.
581 if (error == ERESTART)
587 if (args->sysmsg_result) {
589 * Compute how much time was left of the timeout,
590 * by subtracting the current time and the time
591 * before we started the call, and subtracting
592 * that result from the user-supplied value.
595 timevalsub(&tv1, &tv0);
596 timevalsub(&utv, &tv1);
603 kprintf(LMSG("outgoing timeout (%ld/%ld)"),
604 utv.tv_sec, utv.tv_usec);
606 if ((error = copyout(&utv, (caddr_t)args->timeout,
614 kprintf(LMSG("select_out -> %d"), error);
623 sys_linux_mremap(struct linux_mremap_args *args)
625 struct munmap_args bsd_args;
630 kprintf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
632 (unsigned long)args->old_len,
633 (unsigned long)args->new_len,
634 (unsigned long)args->flags);
636 if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) {
637 args->sysmsg_resultp = NULL;
642 * Check for the page alignment.
643 * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK.
645 if (args->addr & PAGE_MASK) {
646 args->sysmsg_resultp = NULL;
650 args->new_len = round_page(args->new_len);
651 args->old_len = round_page(args->old_len);
653 if (args->new_len > args->old_len) {
654 args->sysmsg_result = 0;
658 if (args->new_len < args->old_len) {
659 bsd_args.sysmsg_result = 0;
660 bsd_args.addr = (caddr_t)(args->addr + args->new_len);
661 bsd_args.len = args->old_len - args->new_len;
662 error = sys_munmap(&bsd_args);
665 args->sysmsg_resultp = error ? NULL : (void *)args->addr;
669 #define LINUX_MS_ASYNC 0x0001
670 #define LINUX_MS_INVALIDATE 0x0002
671 #define LINUX_MS_SYNC 0x0004
677 sys_linux_msync(struct linux_msync_args *args)
679 struct msync_args bsd_args;
682 bsd_args.addr = (caddr_t)args->addr;
683 bsd_args.len = args->len;
684 bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
685 bsd_args.sysmsg_result = 0;
687 error = sys_msync(&bsd_args);
688 args->sysmsg_result = bsd_args.sysmsg_result;
696 sys_linux_time(struct linux_time_args *args)
704 kprintf(ARGS(time, "*"));
709 if (args->tm && (error = copyout(&tm, (caddr_t)args->tm, sizeof(tm))))
711 args->sysmsg_lresult = tm;
715 struct l_times_argv {
722 #define CLK_TCK 100 /* Linux uses 100 */
724 #define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
730 sys_linux_times(struct linux_times_args *args)
732 struct thread *td = curthread;
733 struct proc *p = td->td_proc;
735 struct l_times_argv tms;
741 kprintf(ARGS(times, "*"));
748 tms.tms_utime = CONVTCK(ru.ru_utime);
749 tms.tms_stime = CONVTCK(ru.ru_stime);
751 tms.tms_cutime = CONVTCK(p->p_cru.ru_utime);
752 tms.tms_cstime = CONVTCK(p->p_cru.ru_stime);
754 if ((error = copyout(&tms, (caddr_t)args->buf, sizeof(tms))))
758 args->sysmsg_result = (int)CONVTCK(tv);
766 sys_linux_newuname(struct linux_newuname_args *args)
768 struct thread *td = curthread;
769 struct l_new_utsname utsname;
770 char *osrelease, *osname;
773 if (ldebug(newuname))
774 kprintf(ARGS(newuname, "*"));
778 osname = linux_get_osname(td);
779 osrelease = linux_get_osrelease(td);
781 bzero(&utsname, sizeof(utsname));
782 strncpy(utsname.sysname, osname, LINUX_MAX_UTSNAME-1);
783 strncpy(utsname.nodename, hostname, LINUX_MAX_UTSNAME-1);
784 strncpy(utsname.release, osrelease, LINUX_MAX_UTSNAME-1);
785 strncpy(utsname.version, version, LINUX_MAX_UTSNAME-1);
786 strncpy(utsname.machine, machine, LINUX_MAX_UTSNAME-1);
787 strncpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME-1);
790 return (copyout(&utsname, (caddr_t)args->buf, sizeof(utsname)));
793 /* XXX: why would this be i386-only? most of these are wrong! */
794 #if defined(__i386__)
804 sys_linux_utime(struct linux_utime_args *args)
806 struct timeval tv[2];
807 struct l_utimbuf lut;
808 struct nlookupdata nd;
812 error = linux_copyin_path(args->fname, &path, LINUX_PATH_EXISTS);
817 kprintf(ARGS(utime, "%s, *"), path);
821 error = copyin(args->times, &lut, sizeof(lut));
824 tv[0].tv_sec = lut.l_actime;
826 tv[1].tv_sec = lut.l_modtime;
830 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
832 error = kern_utimes(&nd, args->times ? tv : NULL);
836 linux_free_path(&path);
841 sys_linux_utimes(struct linux_utimes_args *args)
844 struct timeval tv[2], *tvp = NULL;
845 struct nlookupdata nd;
849 error = linux_copyin_path(args->fname, &path, LINUX_PATH_EXISTS);
854 kprintf(ARGS(utimes, "%s, *"), path);
858 error = copyin(args->tptr, ltv, sizeof(ltv));
861 tv[0].tv_sec = ltv[0].tv_sec;
862 tv[0].tv_usec = ltv[0].tv_usec;
863 tv[1].tv_sec = ltv[1].tv_sec;
864 tv[1].tv_usec = ltv[1].tv_usec;
868 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
870 error = kern_utimes(&nd, tvp);
874 linux_free_path(&path);
879 sys_linux_futimesat(struct linux_futimesat_args *args)
882 struct timeval tv[2], *tvp = NULL;
884 struct nlookupdata nd;
888 error = linux_copyin_path(args->fname, &path, LINUX_PATH_EXISTS);
892 if (ldebug(futimesat))
893 kprintf(ARGS(futimesat, "%s, *"), path);
896 error = copyin(args->tptr, ltv, sizeof(ltv));
899 tv[0].tv_sec = ltv[0].tv_sec;
900 tv[0].tv_usec = ltv[0].tv_usec;
901 tv[1].tv_sec = ltv[1].tv_sec;
902 tv[1].tv_usec = ltv[1].tv_usec;
905 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
907 error = nlookup_init_at(&nd, &fp, dfd, path, UIO_SYSSPACE, NLC_FOLLOW);
909 error = kern_utimes(&nd, tvp);
910 nlookup_done_at(&nd, fp);
913 linux_free_path(&path);
919 sys_linux_utimensat(struct linux_utimensat_args *args)
921 struct l_timespec ltv[2];
922 struct timeval tv[2], *tvp = NULL;
924 struct nlookupdata nd;
926 int dfd, flags, error = 0;
928 if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
931 if (args->dfd == LINUX_AT_FDCWD && args->fname == NULL)
935 error = linux_copyin_path(args->fname, &path, LINUX_PATH_EXISTS);
940 if (ldebug(utimensat))
941 kprintf(ARGS(utimensat, "%s, *"), path);
944 error = copyin(args->tptr, ltv, sizeof(ltv));
948 if (ltv[0].tv_sec == LINUX_UTIME_NOW) {
950 } else if (ltv[0].tv_sec == LINUX_UTIME_OMIT) {
951 /* XXX: this is not right, but will do for now */
954 tv[0].tv_sec = ltv[0].tv_sec;
955 /* XXX: we lose precision here, as we don't have ns */
956 tv[0].tv_usec = ltv[0].tv_nsec/1000;
958 if (ltv[1].tv_sec == LINUX_UTIME_NOW) {
960 } else if (ltv[1].tv_sec == LINUX_UTIME_OMIT) {
961 /* XXX: this is not right, but will do for now */
964 tv[1].tv_sec = ltv[1].tv_sec;
965 /* XXX: we lose precision here, as we don't have ns */
966 tv[1].tv_usec = ltv[1].tv_nsec/1000;
971 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
972 flags = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
976 error = nlookup_init_at(&nd, &fp, dfd, path, UIO_SYSSPACE, flags);
978 error = kern_utimes(&nd, tvp);
979 nlookup_done_at(&nd, fp);
981 /* Thank you, Linux, for another non-standard "feature" */
982 KKASSERT(dfd != AT_FDCWD);
983 error = kern_futimes(dfd, tvp);
988 linux_free_path(&path);
992 #endif /* __i386__ */
994 #define __WCLONE 0x80000000
1000 sys_linux_waitpid(struct linux_waitpid_args *args)
1002 int error, options, status;
1005 if (ldebug(waitpid))
1006 kprintf(ARGS(waitpid, "%d, %p, %d"),
1007 args->pid, (void *)args->status, args->options);
1009 options = args->options & (WNOHANG | WUNTRACED);
1010 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
1011 if (args->options & __WCLONE)
1012 options |= WLINUXCLONE;
1014 error = kern_wait(args->pid, args->status ? &status : NULL, options,
1015 NULL, &args->sysmsg_result);
1017 if (error == 0 && args->status) {
1019 if (WIFSIGNALED(status))
1020 status = (status & 0xffffff80) |
1021 BSD_TO_LINUX_SIGNAL(WTERMSIG(status));
1022 else if (WIFSTOPPED(status))
1023 status = (status & 0xffff00ff) |
1024 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(status)) << 8);
1025 error = copyout(&status, args->status, sizeof(status));
1035 sys_linux_wait4(struct linux_wait4_args *args)
1037 struct thread *td = curthread;
1038 struct lwp *lp = td->td_lwp;
1039 struct rusage rusage;
1040 int error, options, status;
1044 kprintf(ARGS(wait4, "%d, %p, %d, %p"),
1045 args->pid, (void *)args->status, args->options,
1046 (void *)args->rusage);
1048 options = args->options & (WNOHANG | WUNTRACED);
1049 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
1050 if (args->options & __WCLONE)
1051 options |= WLINUXCLONE;
1053 error = kern_wait(args->pid, args->status ? &status : NULL, options,
1054 args->rusage ? &rusage : NULL, &args->sysmsg_result);
1057 spin_lock(&lp->lwp_spin);
1058 lwp_delsig(lp, SIGCHLD);
1059 spin_unlock(&lp->lwp_spin);
1062 if (error == 0 && args->status) {
1064 if (WIFSIGNALED(status))
1065 status = (status & 0xffffff80) |
1066 BSD_TO_LINUX_SIGNAL(WTERMSIG(status));
1067 else if (WIFSTOPPED(status))
1068 status = (status & 0xffff00ff) |
1069 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(status)) << 8);
1070 error = copyout(&status, args->status, sizeof(status));
1072 if (error == 0 && args->rusage)
1073 error = copyout(&rusage, args->rusage, sizeof(rusage));
1082 sys_linux_mknod(struct linux_mknod_args *args)
1084 struct nlookupdata nd;
1088 error = linux_copyin_path(args->path, &path, LINUX_PATH_CREATE);
1093 kprintf(ARGS(mknod, "%s, %d, %d"),
1094 path, args->mode, args->dev);
1097 error = nlookup_init(&nd, path, UIO_SYSSPACE, 0);
1099 if (args->mode & S_IFIFO) {
1100 error = kern_mkfifo(&nd, args->mode);
1102 error = kern_mknod(&nd, args->mode,
1110 linux_free_path(&path);
1115 sys_linux_mknodat(struct linux_mknodat_args *args)
1117 struct nlookupdata nd;
1122 error = linux_copyin_path(args->path, &path, LINUX_PATH_CREATE);
1127 kprintf(ARGS(mknod, "%s, %d, %d"),
1128 path, args->mode, args->dev);
1131 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1132 error = nlookup_init_at(&nd, &fp, dfd, path, UIO_SYSSPACE, 0);
1134 if (args->mode & S_IFIFO) {
1135 error = kern_mkfifo(&nd, args->mode);
1137 error = kern_mknod(&nd, args->mode,
1142 nlookup_done_at(&nd, fp);
1145 linux_free_path(&path);
1150 * UGH! This is just about the dumbest idea I've ever heard!!
1155 sys_linux_personality(struct linux_personality_args *args)
1158 if (ldebug(personality))
1159 kprintf(ARGS(personality, "%d"), args->per);
1164 /* Yes Jim, it's still a Linux... */
1165 args->sysmsg_result = 0;
1170 * Wrappers for get/setitimer for debugging..
1175 sys_linux_setitimer(struct linux_setitimer_args *args)
1177 struct setitimer_args bsa;
1178 struct itimerval foo;
1182 if (ldebug(setitimer))
1183 kprintf(ARGS(setitimer, "%p, %p"),
1184 (void *)args->itv, (void *)args->oitv);
1186 bsa.which = args->which;
1187 bsa.itv = (struct itimerval *)args->itv;
1188 bsa.oitv = (struct itimerval *)args->oitv;
1189 bsa.sysmsg_result = 0;
1191 if ((error = copyin((caddr_t)args->itv, &foo, sizeof(foo))))
1194 if (ldebug(setitimer)) {
1195 kprintf("setitimer: value: sec: %ld, usec: %ld\n",
1196 foo.it_value.tv_sec, foo.it_value.tv_usec);
1197 kprintf("setitimer: interval: sec: %ld, usec: %ld\n",
1198 foo.it_interval.tv_sec, foo.it_interval.tv_usec);
1202 error = sys_setitimer(&bsa);
1203 args->sysmsg_result = bsa.sysmsg_result;
1211 sys_linux_getitimer(struct linux_getitimer_args *args)
1213 struct getitimer_args bsa;
1216 if (ldebug(getitimer))
1217 kprintf(ARGS(getitimer, "%p"), (void *)args->itv);
1219 bsa.which = args->which;
1220 bsa.itv = (struct itimerval *)args->itv;
1221 bsa.sysmsg_result = 0;
1222 error = sys_getitimer(&bsa);
1223 args->sysmsg_result = bsa.sysmsg_result;
1231 sys_linux_nice(struct linux_nice_args *args)
1233 struct setpriority_args bsd_args;
1236 bsd_args.which = PRIO_PROCESS;
1237 bsd_args.who = 0; /* current process */
1238 bsd_args.prio = args->inc;
1239 bsd_args.sysmsg_result = 0;
1240 error = sys_setpriority(&bsd_args);
1241 args->sysmsg_result = bsd_args.sysmsg_result;
1249 sys_linux_setgroups(struct linux_setgroups_args *args)
1251 struct thread *td = curthread;
1252 struct proc *p = td->td_proc;
1253 struct ucred *newcred, *oldcred;
1254 l_gid_t linux_gidset[NGROUPS];
1258 ngrp = args->gidsetsize;
1259 oldcred = td->td_ucred;
1262 * cr_groups[0] holds egid. Setting the whole set from
1263 * the supplied set will cause egid to be changed too.
1264 * Keep cr_groups[0] unchanged to prevent that.
1267 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0)
1270 if ((u_int)ngrp >= NGROUPS)
1274 newcred = crdup(oldcred);
1276 error = copyin((caddr_t)args->grouplist, linux_gidset,
1277 ngrp * sizeof(l_gid_t));
1284 newcred->cr_ngroups = ngrp + 1;
1286 bsd_gidset = newcred->cr_groups;
1289 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1293 newcred->cr_ngroups = 1;
1297 oldcred = p->p_ucred; /* reload, deal with threads race */
1298 p->p_ucred = newcred;
1308 sys_linux_getgroups(struct linux_getgroups_args *args)
1310 struct thread *td = curthread;
1312 l_gid_t linux_gidset[NGROUPS];
1314 int bsd_gidsetsz, ngrp, error;
1316 cred = td->td_ucred;
1317 bsd_gidset = cred->cr_groups;
1318 bsd_gidsetsz = cred->cr_ngroups - 1;
1321 * cr_groups[0] holds egid. Returning the whole set
1322 * here will cause a duplicate. Exclude cr_groups[0]
1326 if ((ngrp = args->gidsetsize) == 0) {
1327 args->sysmsg_result = bsd_gidsetsz;
1331 if ((u_int)ngrp < bsd_gidsetsz)
1335 while (ngrp < bsd_gidsetsz) {
1336 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1340 if ((error = copyout(linux_gidset, args->grouplist,
1341 ngrp * sizeof(l_gid_t)))) {
1345 args->sysmsg_result = ngrp;
1353 sys_linux_setrlimit(struct linux_setrlimit_args *args)
1355 struct l_rlimit linux_rlim;
1361 if (ldebug(setrlimit))
1362 kprintf(ARGS(setrlimit, "%d, %p"),
1363 args->resource, (void *)args->rlim);
1365 if (args->resource >= LINUX_RLIM_NLIMITS)
1367 which = linux_to_bsd_resource[args->resource];
1371 error = copyin(args->rlim, &linux_rlim, sizeof(linux_rlim));
1374 rlim.rlim_cur = (rlim_t)linux_rlim.rlim_cur;
1375 rlim.rlim_max = (rlim_t)linux_rlim.rlim_max;
1377 error = kern_setrlimit(which, &rlim);
1386 sys_linux_old_getrlimit(struct linux_old_getrlimit_args *args)
1388 struct l_rlimit linux_rlim;
1394 if (ldebug(old_getrlimit))
1395 kprintf(ARGS(old_getrlimit, "%d, %p"),
1396 args->resource, (void *)args->rlim);
1398 if (args->resource >= LINUX_RLIM_NLIMITS)
1400 which = linux_to_bsd_resource[args->resource];
1404 error = kern_getrlimit(which, &rlim);
1407 linux_rlim.rlim_cur = (l_ulong)rlim.rlim_cur;
1408 if (linux_rlim.rlim_cur == ULONG_MAX)
1409 linux_rlim.rlim_cur = LONG_MAX;
1410 linux_rlim.rlim_max = (l_ulong)rlim.rlim_max;
1411 if (linux_rlim.rlim_max == ULONG_MAX)
1412 linux_rlim.rlim_max = LONG_MAX;
1413 error = copyout(&linux_rlim, args->rlim, sizeof(linux_rlim));
1422 sys_linux_getrlimit(struct linux_getrlimit_args *args)
1424 struct l_rlimit linux_rlim;
1430 if (ldebug(getrlimit))
1431 kprintf(ARGS(getrlimit, "%d, %p"),
1432 args->resource, (void *)args->rlim);
1434 if (args->resource >= LINUX_RLIM_NLIMITS)
1436 which = linux_to_bsd_resource[args->resource];
1440 error = kern_getrlimit(which, &rlim);
1443 linux_rlim.rlim_cur = (l_ulong)rlim.rlim_cur;
1444 linux_rlim.rlim_max = (l_ulong)rlim.rlim_max;
1445 error = copyout(&linux_rlim, args->rlim, sizeof(linux_rlim));
1454 sys_linux_sched_setscheduler(struct linux_sched_setscheduler_args *args)
1456 struct sched_setscheduler_args bsd;
1460 if (ldebug(sched_setscheduler))
1461 kprintf(ARGS(sched_setscheduler, "%d, %d, %p"),
1462 args->pid, args->policy, (const void *)args->param);
1465 switch (args->policy) {
1466 case LINUX_SCHED_OTHER:
1467 bsd.policy = SCHED_OTHER;
1469 case LINUX_SCHED_FIFO:
1470 bsd.policy = SCHED_FIFO;
1472 case LINUX_SCHED_RR:
1473 bsd.policy = SCHED_RR;
1479 bsd.pid = args->pid;
1480 bsd.param = (struct sched_param *)args->param;
1481 bsd.sysmsg_result = 0;
1483 error = sys_sched_setscheduler(&bsd);
1484 args->sysmsg_result = bsd.sysmsg_result;
1492 sys_linux_sched_getscheduler(struct linux_sched_getscheduler_args *args)
1494 struct sched_getscheduler_args bsd;
1498 if (ldebug(sched_getscheduler))
1499 kprintf(ARGS(sched_getscheduler, "%d"), args->pid);
1502 bsd.sysmsg_result = 0;
1503 bsd.pid = args->pid;
1504 error = sys_sched_getscheduler(&bsd);
1505 args->sysmsg_result = bsd.sysmsg_result;
1507 switch (args->sysmsg_result) {
1509 args->sysmsg_result = LINUX_SCHED_OTHER;
1512 args->sysmsg_result = LINUX_SCHED_FIFO;
1515 args->sysmsg_result = LINUX_SCHED_RR;
1525 sys_linux_sched_get_priority_max(struct linux_sched_get_priority_max_args *args)
1527 struct sched_get_priority_max_args bsd;
1531 if (ldebug(sched_get_priority_max))
1532 kprintf(ARGS(sched_get_priority_max, "%d"), args->policy);
1535 switch (args->policy) {
1536 case LINUX_SCHED_OTHER:
1537 bsd.policy = SCHED_OTHER;
1539 case LINUX_SCHED_FIFO:
1540 bsd.policy = SCHED_FIFO;
1542 case LINUX_SCHED_RR:
1543 bsd.policy = SCHED_RR;
1548 bsd.sysmsg_result = 0;
1550 error = sys_sched_get_priority_max(&bsd);
1551 args->sysmsg_result = bsd.sysmsg_result;
1559 sys_linux_sched_get_priority_min(struct linux_sched_get_priority_min_args *args)
1561 struct sched_get_priority_min_args bsd;
1565 if (ldebug(sched_get_priority_min))
1566 kprintf(ARGS(sched_get_priority_min, "%d"), args->policy);
1569 switch (args->policy) {
1570 case LINUX_SCHED_OTHER:
1571 bsd.policy = SCHED_OTHER;
1573 case LINUX_SCHED_FIFO:
1574 bsd.policy = SCHED_FIFO;
1576 case LINUX_SCHED_RR:
1577 bsd.policy = SCHED_RR;
1582 bsd.sysmsg_result = 0;
1584 error = sys_sched_get_priority_min(&bsd);
1585 args->sysmsg_result = bsd.sysmsg_result;
1589 #define REBOOT_CAD_ON 0x89abcdef
1590 #define REBOOT_CAD_OFF 0
1591 #define REBOOT_HALT 0xcdef0123
1592 #define REBOOT_RESTART 0x01234567
1593 #define REBOOT_RESTART2 0xA1B2C3D4
1594 #define REBOOT_POWEROFF 0x4321FEDC
1595 #define REBOOT_MAGIC1 0xfee1dead
1596 #define REBOOT_MAGIC2 0x28121969
1597 #define REBOOT_MAGIC2A 0x05121996
1598 #define REBOOT_MAGIC2B 0x16041998
1604 sys_linux_reboot(struct linux_reboot_args *args)
1606 struct reboot_args bsd_args;
1611 kprintf(ARGS(reboot, "0x%x"), args->cmd);
1614 if ((args->magic1 != REBOOT_MAGIC1) ||
1615 ((args->magic2 != REBOOT_MAGIC2) &&
1616 (args->magic2 != REBOOT_MAGIC2A) &&
1617 (args->magic2 != REBOOT_MAGIC2B)))
1620 switch (args->cmd) {
1622 case REBOOT_CAD_OFF:
1623 return (priv_check(curthread, PRIV_REBOOT));
1626 bsd_args.opt = RB_HALT;
1628 case REBOOT_RESTART:
1629 case REBOOT_RESTART2:
1632 case REBOOT_POWEROFF:
1633 bsd_args.opt = RB_POWEROFF;
1640 bsd_args.sysmsg_result = 0;
1642 error = sys_reboot(&bsd_args);
1643 args->sysmsg_result = bsd_args.sysmsg_result;
1648 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1649 * p->p_retval[1] when COMPAT_43 or COMPAT_SUNOS is defined. This
1650 * globbers registers that are assumed to be preserved. The following
1651 * lightweight syscalls fixes this. See also linux_getgid16() and
1652 * linux_getuid16() in linux_uid16.c.
1654 * linux_getpid() - MP SAFE
1655 * linux_getgid() - MP SAFE
1656 * linux_getuid() - MP SAFE
1663 sys_linux_getpid(struct linux_getpid_args *args)
1665 struct linux_emuldata *em;
1666 struct proc *p = curproc;
1670 em = emuldata_get(p);
1671 if (em == NULL) /* this should never happen */
1672 args->sysmsg_result = p->p_pid;
1674 args->sysmsg_result = em->s->group_pid;
1684 sys_linux_getppid(struct linux_getppid_args *args)
1686 struct linux_emuldata *em;
1687 struct proc *parent;
1692 em = emuldata_get(curproc);
1693 KKASSERT(em != NULL);
1694 group_pid = em->s->group_pid;
1697 p = pfind(group_pid);
1698 /* We are not allowed to fail */
1703 if (parent->p_sysent == &elf_linux_sysvec) {
1705 em = emuldata_get(parent);
1706 args->sysmsg_result = em->s->group_pid;
1709 args->sysmsg_result = parent->p_pid;
1721 sys_linux_getgid(struct linux_getgid_args *args)
1723 struct thread *td = curthread;
1725 args->sysmsg_result = td->td_ucred->cr_rgid;
1733 sys_linux_getuid(struct linux_getuid_args *args)
1735 struct thread *td = curthread;
1737 args->sysmsg_result = td->td_ucred->cr_ruid;
1745 sys_linux_getsid(struct linux_getsid_args *args)
1747 struct getsid_args bsd;
1750 bsd.sysmsg_result = 0;
1751 bsd.pid = args->pid;
1752 error = sys_getsid(&bsd);
1753 args->sysmsg_result = bsd.sysmsg_result;
1761 linux_nosys(struct nosys_args *args)
1768 sys_linux_mq_open(struct linux_mq_open_args *args)
1770 struct mq_open_args moa;
1774 if (args->oflag & LINUX_O_RDONLY)
1776 if (args->oflag & LINUX_O_WRONLY)
1778 if (args->oflag & LINUX_O_RDWR)
1781 if (args->oflag & LINUX_O_NONBLOCK)
1782 oflag |= O_NONBLOCK;
1783 if (args->oflag & LINUX_O_CREAT)
1785 if (args->oflag & LINUX_O_EXCL)
1788 moa.name = args->name;
1790 moa.mode = args->mode;
1791 moa.attr = args->attr;
1793 error = sys_mq_open(&moa);
1799 sys_linux_mq_getsetattr(struct linux_mq_getsetattr_args *args)
1801 struct mq_getattr_args gaa;
1802 struct mq_setattr_args saa;
1805 gaa.mqdes = args->mqd;
1806 gaa.mqstat = args->oattr;
1808 saa.mqdes = args->mqd;
1809 saa.mqstat = args->attr;
1810 saa.mqstat = args->oattr;
1812 if (args->attr != NULL) {
1813 error = sys_mq_setattr(&saa);
1815 error = sys_mq_getattr(&gaa);
1822 * Get affinity of a process.
1825 sys_linux_sched_getaffinity(struct linux_sched_getaffinity_args *args)
1833 if (ldebug(sched_getaffinity))
1834 kprintf(ARGS(sched_getaffinity, "%d, %d, *"), args->pid,
1837 if (args->len < sizeof(cpumask_t))
1840 if ((error = priv_check(curthread, PRIV_SCHED_CPUSET)) != 0)
1843 /* Get the mplock to ensure that the proc is not running */
1845 if (args->pid == 0) {
1849 p = pfind(args->pid);
1856 lp = FIRST_LWP_IN_PROC(p);
1858 * XXX: if lwp_cpumask is ever changed to support more than
1859 * 32 processors, this needs to be changed to a bcopy.
1861 mask = lp->lwp_cpumask;
1862 if ((error = copyout(&mask, args->user_mask_ptr, sizeof(cpumask_t))))
1868 args->sysmsg_iresult = sizeof(cpumask_t);
1876 * Set affinity of a process.
1879 sys_linux_sched_setaffinity(struct linux_sched_setaffinity_args *args)
1882 if (ldebug(sched_setaffinity))
1883 kprintf(ARGS(sched_setaffinity, "%d, %d, *"), args->pid,
1887 * From Linux man page:
1888 * sched_setaffinity() sets the CPU affinity mask of the process
1889 * whose ID is pid to the value specified by mask. If pid is zero,
1890 * then the calling process is used. The argument cpusetsize is
1891 * the length (in bytes) of the data pointed to by mask. Normally
1892 * this argument would be specified as sizeof(cpu_set_t).
1894 * If the process specified by pid is not currently running on one
1895 * of the CPUs specified in mask, then that process is migrated to
1896 * one of the CPUs specified in mask.
1899 * About our implementation: I don't think that it is too important
1900 * to have a working implementation, but if it was ever needed,
1901 * the best approach would be to implement the whole mechanism
1902 * properly in kern_usched.
1903 * The idea has to be to change the affinity mask AND migrate the
1904 * lwp to one of the new valid CPUs for the lwp, in case the current
1905 * CPU isn't anymore in the affinity mask passed in.
1906 * For now, we'll just signal success even if we didn't do anything.
1912 sys_linux_gettid(struct linux_gettid_args *args)
1914 args->sysmsg_iresult = curproc->p_pid;
1919 sys_linux_getcpu(struct linux_getcpu_args *args)
1921 struct globaldata *gd;
1926 error = copyout(&gd->gd_cpuid, args->pcpu, sizeof(gd->gd_cpuid));
1930 * XXX: this should be the NUMA node, but since we don't implement it,
1931 * just return 0 for it.
1933 error = copyout(&node, args->pnode, sizeof(node));