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 withough 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 $
29 * $DragonFly: src/sys/emulation/linux/linux_misc.c,v 1.39 2007/06/26 19:31:03 dillon Exp $
32 #include "opt_compat.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/fcntl.h>
37 #include <sys/imgact_aout.h>
38 #include <sys/kernel.h>
39 #include <sys/kern_syscall.h>
42 #include <sys/mount.h>
46 #include <sys/nlookup.h>
47 #include <sys/blist.h>
48 #include <sys/reboot.h>
49 #include <sys/resourcevar.h>
50 #include <sys/signalvar.h>
52 #include <sys/sysctl.h>
53 #include <sys/sysproto.h>
55 #include <sys/unistd.h>
56 #include <sys/vmmeter.h>
57 #include <sys/vnode.h>
60 #include <sys/signal2.h>
61 #include <sys/thread2.h>
62 #include <sys/mplock2.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) {
150 sysinfo.uptime=ts.tv_sec;
152 /* Use the information from the mib to get our load averages */
153 for (i = 0; i < 3; i++)
154 sysinfo.loads[i] = averunnable.ldavg[i];
156 sysinfo.totalram = Maxmem * PAGE_SIZE;
157 sysinfo.freeram = sysinfo.totalram - vmstats.v_wire_count * PAGE_SIZE;
160 sysinfo.sharedram = 0;
161 for (object = TAILQ_FIRST(&vm_object_list); object != NULL;
162 object = TAILQ_NEXT(object, object_list)) {
163 if (object->type == OBJT_MARKER)
165 if (object->shadow_count > 1)
166 sysinfo.sharedram += object->resident_page_count;
169 sysinfo.sharedram *= PAGE_SIZE;
170 sysinfo.bufferram = 0;
172 if (swapblist == NULL) {
173 sysinfo.totalswap= 0;
174 sysinfo.freeswap = 0;
176 sysinfo.totalswap = swapblist->bl_blocks * 1024;
177 sysinfo.freeswap = swapblist->bl_root->u.bmu_avail * PAGE_SIZE;
181 sysinfo.procs = nprocs;
182 sysinfo.totalhigh = 0;
183 sysinfo.freehigh = 0;
184 sysinfo.mem_unit = 1; /* Set the basic mem unit to 1 */
186 error = copyout(&sysinfo, (caddr_t)args->info, sizeof(sysinfo));
194 sys_linux_alarm(struct linux_alarm_args *args)
196 struct thread *td = curthread;
197 struct proc *p = td->td_proc;
198 struct itimerval it, old_it;
203 kprintf(ARGS(alarm, "%u"), args->secs);
206 if (args->secs > 100000000)
209 it.it_value.tv_sec = (long)args->secs;
210 it.it_value.tv_usec = 0;
211 it.it_interval.tv_sec = 0;
212 it.it_interval.tv_usec = 0;
215 old_it = p->p_realtimer;
217 if (timevalisset(&old_it.it_value))
218 callout_stop(&p->p_ithandle);
219 if (it.it_value.tv_sec != 0) {
220 callout_reset(&p->p_ithandle, tvtohz_high(&it.it_value),
222 timevaladd(&it.it_value, &tv);
227 if (timevalcmp(&old_it.it_value, &tv, >)) {
228 timevalsub(&old_it.it_value, &tv);
229 if (old_it.it_value.tv_usec != 0)
230 old_it.it_value.tv_sec++;
231 args->sysmsg_result = old_it.it_value.tv_sec;
240 sys_linux_brk(struct linux_brk_args *args)
242 struct thread *td = curthread;
243 struct proc *p = td->td_proc;
245 vm_offset_t new, old;
246 struct obreak_args bsd_args;
252 kprintf(ARGS(brk, "%p"), (void *)args->dsend);
254 old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
255 new = (vm_offset_t)args->dsend;
256 bsd_args.sysmsg_result = 0;
257 bsd_args.nsize = (char *) new;
258 bsd_args.sysmsg_result = 0;
259 if (((caddr_t)new > vm->vm_daddr) && !sys_obreak(&bsd_args))
260 args->sysmsg_result = (long)new;
262 args->sysmsg_result = (long)old;
272 sys_linux_uselib(struct linux_uselib_args *args)
274 struct thread *td = curthread;
276 struct nlookupdata nd;
281 unsigned long file_offset;
283 unsigned long bss_size;
290 error = linux_copyin_path(args->library, &path, LINUX_PATH_EXISTS);
295 kprintf(ARGS(uselib, "%s"), path);
303 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
304 nd.nl_flags |= NLC_EXEC;
306 error = nlookup(&nd);
308 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
312 * From here on down, we have a locked vnode that must be unlocked.
317 if (vp->v_writecount) {
323 error = VOP_GETATTR(vp, &attr);
327 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
328 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
334 if (attr.va_size == 0) {
339 error = VOP_OPEN(vp, FREAD, td->td_ucred, NULL);
344 * Lock no longer needed
349 /* Pull in executable header into kernel_map */
350 error = vm_mmap(&kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE,
351 VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp, 0);
355 /* Is it a Linux binary ? */
356 if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
362 * While we are here, we should REALLY do some more checks
365 /* Set file/virtual offset based on a.out variant. */
366 switch ((int)(a_out->a_magic & 0xffff)) {
367 case 0413: /* ZMAGIC */
370 case 0314: /* QMAGIC */
378 bss_size = round_page(a_out->a_bss);
380 /* Check various fields in header for validity/bounds. */
381 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
386 /* text + data can't exceed file size */
387 if (a_out->a_data + a_out->a_text > attr.va_size) {
393 * text/data/bss must not exceed limits
394 * XXX - this is not complete. it should check current usage PLUS
395 * the resources needed by this library.
397 if (a_out->a_text > maxtsiz ||
398 a_out->a_data + bss_size > p->p_rlimit[RLIMIT_DATA].rlim_cur) {
403 /* prevent more writers */
404 vsetflags(vp, VTEXT);
407 * Check if file_offset page aligned. Currently we cannot handle
408 * misalinged file offsets, and so we read in the entire image
411 if (file_offset & PAGE_MASK) {
413 kprintf("uselib: Non page aligned binary %lu\n", file_offset);
415 /* Map text+data read/write/execute */
417 /* a_entry is the load address and is page aligned */
418 vmaddr = trunc_page(a_out->a_entry);
420 /* get anon user mapping, read+write+execute */
421 error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0,
422 &vmaddr, a_out->a_text + a_out->a_data,
424 FALSE, VM_MAPTYPE_NORMAL,
425 VM_PROT_ALL, VM_PROT_ALL,
430 /* map file into kernel_map */
431 error = vm_mmap(&kernel_map, &buffer,
432 round_page(a_out->a_text + a_out->a_data + file_offset),
433 VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp,
434 trunc_page(file_offset));
438 /* copy from kernel VM space to user space */
439 error = copyout((caddr_t)(uintptr_t)(buffer + file_offset),
440 (caddr_t)vmaddr, a_out->a_text + a_out->a_data);
442 /* release temporary kernel space */
443 vm_map_remove(&kernel_map, buffer, buffer +
444 round_page(a_out->a_text + a_out->a_data + file_offset));
450 kprintf("uselib: Page aligned binary %lu\n", file_offset);
453 * for QMAGIC, a_entry is 20 bytes beyond the load address
454 * to skip the executable header
456 vmaddr = trunc_page(a_out->a_entry);
459 * Map it all into the process's space as a single
460 * copy-on-write "data" segment.
462 error = vm_mmap(&p->p_vmspace->vm_map, &vmaddr,
463 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
464 MAP_PRIVATE | MAP_FIXED, (caddr_t)vp, file_offset);
469 kprintf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long*)vmaddr)[0],
473 /* Calculate BSS start address */
474 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
477 /* allocate some 'anon' space */
478 error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0,
481 FALSE, VM_MAPTYPE_NORMAL,
482 VM_PROT_ALL, VM_PROT_ALL,
489 /* Unlock/release vnode */
495 /* Release the kernel mapping. */
497 vm_map_remove(&kernel_map, (vm_offset_t)a_out,
498 (vm_offset_t)a_out + PAGE_SIZE);
502 linux_free_path(&path);
510 sys_linux_select(struct linux_select_args *args)
512 struct select_args bsa;
513 struct timeval tv0, tv1, utv, *tvp;
519 kprintf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
520 (void *)args->readfds, (void *)args->writefds,
521 (void *)args->exceptfds, (void *)args->timeout);
525 bsa.sysmsg_result = 0;
527 bsa.in = args->readfds;
528 bsa.ou = args->writefds;
529 bsa.ex = args->exceptfds;
530 bsa.tv = (struct timeval *)args->timeout;
533 * Store current time for computation of the amount of
537 if ((error = copyin((caddr_t)args->timeout, &utv,
542 kprintf(LMSG("incoming timeout (%ld/%ld)"),
543 utv.tv_sec, utv.tv_usec);
546 if (itimerfix(&utv)) {
548 * The timeval was invalid. Convert it to something
549 * valid that will act as it does under Linux.
551 sg = stackgap_init();
552 tvp = stackgap_alloc(&sg, sizeof(utv));
553 utv.tv_sec += utv.tv_usec / 1000000;
554 utv.tv_usec %= 1000000;
555 if (utv.tv_usec < 0) {
557 utv.tv_usec += 1000000;
561 if ((error = copyout(&utv, tvp, sizeof(utv))))
568 error = sys_select(&bsa);
569 args->sysmsg_result = bsa.sysmsg_result;
572 kprintf(LMSG("real select returns %d"), error);
576 * See fs/select.c in the Linux kernel. Without this,
577 * Maelstrom doesn't work.
579 if (error == ERESTART)
585 if (args->sysmsg_result) {
587 * Compute how much time was left of the timeout,
588 * by subtracting the current time and the time
589 * before we started the call, and subtracting
590 * that result from the user-supplied value.
593 timevalsub(&tv1, &tv0);
594 timevalsub(&utv, &tv1);
601 kprintf(LMSG("outgoing timeout (%ld/%ld)"),
602 utv.tv_sec, utv.tv_usec);
604 if ((error = copyout(&utv, (caddr_t)args->timeout,
612 kprintf(LMSG("select_out -> %d"), error);
621 sys_linux_mremap(struct linux_mremap_args *args)
623 struct munmap_args bsd_args;
628 kprintf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
630 (unsigned long)args->old_len,
631 (unsigned long)args->new_len,
632 (unsigned long)args->flags);
634 if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) {
635 args->sysmsg_resultp = NULL;
640 * Check for the page alignment.
641 * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK.
643 if (args->addr & PAGE_MASK) {
644 args->sysmsg_resultp = NULL;
648 args->new_len = round_page(args->new_len);
649 args->old_len = round_page(args->old_len);
651 if (args->new_len > args->old_len) {
652 args->sysmsg_result = 0;
656 if (args->new_len < args->old_len) {
657 bsd_args.sysmsg_result = 0;
658 bsd_args.addr = (caddr_t)(args->addr + args->new_len);
659 bsd_args.len = args->old_len - args->new_len;
660 error = sys_munmap(&bsd_args);
663 args->sysmsg_resultp = error ? NULL : (void *)args->addr;
667 #define LINUX_MS_ASYNC 0x0001
668 #define LINUX_MS_INVALIDATE 0x0002
669 #define LINUX_MS_SYNC 0x0004
675 sys_linux_msync(struct linux_msync_args *args)
677 struct msync_args bsd_args;
680 bsd_args.addr = (caddr_t)args->addr;
681 bsd_args.len = args->len;
682 bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
683 bsd_args.sysmsg_result = 0;
685 error = sys_msync(&bsd_args);
686 args->sysmsg_result = bsd_args.sysmsg_result;
694 sys_linux_time(struct linux_time_args *args)
702 kprintf(ARGS(time, "*"));
707 if (args->tm && (error = copyout(&tm, (caddr_t)args->tm, sizeof(tm))))
709 args->sysmsg_lresult = tm;
713 struct l_times_argv {
720 #define CLK_TCK 100 /* Linux uses 100 */
722 #define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
728 sys_linux_times(struct linux_times_args *args)
730 struct thread *td = curthread;
731 struct proc *p = td->td_proc;
733 struct l_times_argv tms;
739 kprintf(ARGS(times, "*"));
746 tms.tms_utime = CONVTCK(ru.ru_utime);
747 tms.tms_stime = CONVTCK(ru.ru_stime);
749 tms.tms_cutime = CONVTCK(p->p_cru.ru_utime);
750 tms.tms_cstime = CONVTCK(p->p_cru.ru_stime);
752 if ((error = copyout(&tms, (caddr_t)args->buf, sizeof(tms))))
756 args->sysmsg_result = (int)CONVTCK(tv);
764 sys_linux_newuname(struct linux_newuname_args *args)
766 struct thread *td = curthread;
767 struct l_new_utsname utsname;
768 char *osrelease, *osname;
771 if (ldebug(newuname))
772 kprintf(ARGS(newuname, "*"));
776 osname = linux_get_osname(td);
777 osrelease = linux_get_osrelease(td);
779 bzero(&utsname, sizeof(utsname));
780 strncpy(utsname.sysname, osname, LINUX_MAX_UTSNAME-1);
781 strncpy(utsname.nodename, hostname, LINUX_MAX_UTSNAME-1);
782 strncpy(utsname.release, osrelease, LINUX_MAX_UTSNAME-1);
783 strncpy(utsname.version, version, LINUX_MAX_UTSNAME-1);
784 strncpy(utsname.machine, machine, LINUX_MAX_UTSNAME-1);
785 strncpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME-1);
788 return (copyout(&utsname, (caddr_t)args->buf, sizeof(utsname)));
791 /* XXX: why would this be i386-only? most of these are wrong! */
792 #if defined(__i386__)
802 sys_linux_utime(struct linux_utime_args *args)
804 struct timeval tv[2];
805 struct l_utimbuf lut;
806 struct nlookupdata nd;
810 error = linux_copyin_path(args->fname, &path, LINUX_PATH_EXISTS);
815 kprintf(ARGS(utime, "%s, *"), path);
819 error = copyin(args->times, &lut, sizeof(lut));
822 tv[0].tv_sec = lut.l_actime;
824 tv[1].tv_sec = lut.l_modtime;
828 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
830 error = kern_utimes(&nd, args->times ? tv : NULL);
834 linux_free_path(&path);
839 sys_linux_utimes(struct linux_utimes_args *args)
842 struct timeval tv[2], *tvp = NULL;
843 struct nlookupdata nd;
847 error = linux_copyin_path(args->fname, &path, LINUX_PATH_EXISTS);
852 kprintf(ARGS(utimes, "%s, *"), path);
856 error = copyin(args->tptr, ltv, sizeof(ltv));
859 tv[0].tv_sec = ltv[0].tv_sec;
860 tv[0].tv_usec = ltv[0].tv_usec;
861 tv[1].tv_sec = ltv[1].tv_sec;
862 tv[1].tv_usec = ltv[1].tv_usec;
866 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
868 error = kern_utimes(&nd, tvp);
872 linux_free_path(&path);
877 sys_linux_futimesat(struct linux_futimesat_args *args)
880 struct timeval tv[2], *tvp = NULL;
882 struct nlookupdata nd;
886 error = linux_copyin_path(args->fname, &path, LINUX_PATH_EXISTS);
890 if (ldebug(futimesat))
891 kprintf(ARGS(futimesat, "%s, *"), path);
894 error = copyin(args->tptr, ltv, sizeof(ltv));
897 tv[0].tv_sec = ltv[0].tv_sec;
898 tv[0].tv_usec = ltv[0].tv_usec;
899 tv[1].tv_sec = ltv[1].tv_sec;
900 tv[1].tv_usec = ltv[1].tv_usec;
903 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
905 error = nlookup_init_at(&nd, &fp, dfd, path, UIO_SYSSPACE, NLC_FOLLOW);
907 error = kern_utimes(&nd, tvp);
908 nlookup_done_at(&nd, fp);
911 linux_free_path(&path);
917 sys_linux_utimensat(struct linux_utimensat_args *args)
919 struct l_timespec ltv[2];
920 struct timeval tv[2], *tvp = NULL;
922 struct nlookupdata nd;
924 int dfd, flags, error = 0;
926 if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
929 if (args->dfd == LINUX_AT_FDCWD && args->fname == NULL)
933 error = linux_copyin_path(args->fname, &path, LINUX_PATH_EXISTS);
938 if (ldebug(utimensat))
939 kprintf(ARGS(utimensat, "%s, *"), path);
942 error = copyin(args->tptr, ltv, sizeof(ltv));
946 if (ltv[0].tv_sec == LINUX_UTIME_NOW) {
948 } else if (ltv[0].tv_sec == LINUX_UTIME_OMIT) {
949 /* XXX: this is not right, but will do for now */
952 tv[0].tv_sec = ltv[0].tv_sec;
953 /* XXX: we lose precision here, as we don't have ns */
954 tv[0].tv_usec = ltv[0].tv_nsec/1000;
956 if (ltv[1].tv_sec == LINUX_UTIME_NOW) {
958 } else if (ltv[1].tv_sec == LINUX_UTIME_OMIT) {
959 /* XXX: this is not right, but will do for now */
962 tv[1].tv_sec = ltv[1].tv_sec;
963 /* XXX: we lose precision here, as we don't have ns */
964 tv[1].tv_usec = ltv[1].tv_nsec/1000;
969 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
970 flags = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
974 error = nlookup_init_at(&nd, &fp, dfd, path, UIO_SYSSPACE, flags);
976 error = kern_utimes(&nd, tvp);
977 nlookup_done_at(&nd, fp);
979 /* Thank you, Linux, for another non-standard "feature" */
980 KKASSERT(dfd != AT_FDCWD);
981 error = kern_futimes(dfd, tvp);
986 linux_free_path(&path);
990 #endif /* __i386__ */
992 #define __WCLONE 0x80000000
998 sys_linux_waitpid(struct linux_waitpid_args *args)
1000 int error, options, status;
1003 if (ldebug(waitpid))
1004 kprintf(ARGS(waitpid, "%d, %p, %d"),
1005 args->pid, (void *)args->status, args->options);
1007 options = args->options & (WNOHANG | WUNTRACED);
1008 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
1009 if (args->options & __WCLONE)
1010 options |= WLINUXCLONE;
1012 error = kern_wait(args->pid, args->status ? &status : NULL, options,
1013 NULL, &args->sysmsg_result);
1015 if (error == 0 && args->status) {
1017 if (WIFSIGNALED(status))
1018 status = (status & 0xffffff80) |
1019 BSD_TO_LINUX_SIGNAL(WTERMSIG(status));
1020 else if (WIFSTOPPED(status))
1021 status = (status & 0xffff00ff) |
1022 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(status)) << 8);
1023 error = copyout(&status, args->status, sizeof(status));
1033 sys_linux_wait4(struct linux_wait4_args *args)
1035 struct thread *td = curthread;
1036 struct lwp *lp = td->td_lwp;
1037 struct rusage rusage;
1038 int error, options, status;
1042 kprintf(ARGS(wait4, "%d, %p, %d, %p"),
1043 args->pid, (void *)args->status, args->options,
1044 (void *)args->rusage);
1046 options = args->options & (WNOHANG | WUNTRACED);
1047 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
1048 if (args->options & __WCLONE)
1049 options |= WLINUXCLONE;
1051 error = kern_wait(args->pid, args->status ? &status : NULL, options,
1052 args->rusage ? &rusage : NULL, &args->sysmsg_result);
1055 lwp_delsig(lp, SIGCHLD);
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 or COMPAT_SUNOS 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);
1690 p = pfind(em->s->group_pid);
1691 /* We are not allowed to fail */
1696 if (parent->p_sysent == &elf_linux_sysvec) {
1697 em = emuldata_get(parent);
1698 args->sysmsg_result = em->s->group_pid;
1700 args->sysmsg_result = parent->p_pid;
1712 sys_linux_getgid(struct linux_getgid_args *args)
1714 struct thread *td = curthread;
1716 args->sysmsg_result = td->td_ucred->cr_rgid;
1724 sys_linux_getuid(struct linux_getuid_args *args)
1726 struct thread *td = curthread;
1728 args->sysmsg_result = td->td_ucred->cr_ruid;
1736 sys_linux_getsid(struct linux_getsid_args *args)
1738 struct getsid_args bsd;
1741 bsd.sysmsg_result = 0;
1742 bsd.pid = args->pid;
1743 error = sys_getsid(&bsd);
1744 args->sysmsg_result = bsd.sysmsg_result;
1752 linux_nosys(struct nosys_args *args)
1759 sys_linux_mq_open(struct linux_mq_open_args *args)
1761 struct mq_open_args moa;
1765 if (args->oflag & LINUX_O_RDONLY)
1767 if (args->oflag & LINUX_O_WRONLY)
1769 if (args->oflag & LINUX_O_RDWR)
1772 if (args->oflag & LINUX_O_NONBLOCK)
1773 oflag |= O_NONBLOCK;
1774 if (args->oflag & LINUX_O_CREAT)
1776 if (args->oflag & LINUX_O_EXCL)
1779 moa.name = args->name;
1781 moa.mode = args->mode;
1782 moa.attr = args->attr;
1784 error = sys_mq_open(&moa);
1790 sys_linux_mq_getsetattr(struct linux_mq_getsetattr_args *args)
1792 struct mq_getattr_args gaa;
1793 struct mq_setattr_args saa;
1796 gaa.mqdes = args->mqd;
1797 gaa.mqstat = args->oattr;
1799 saa.mqdes = args->mqd;
1800 saa.mqstat = args->attr;
1801 saa.mqstat = args->oattr;
1803 if (args->attr != NULL) {
1804 error = sys_mq_setattr(&saa);
1806 error = sys_mq_getattr(&gaa);
1813 * Get affinity of a process.
1816 sys_linux_sched_getaffinity(struct linux_sched_getaffinity_args *args)
1824 if (ldebug(sched_getaffinity))
1825 kprintf(ARGS(sched_getaffinity, "%d, %d, *"), args->pid,
1828 if (args->len < sizeof(cpumask_t))
1831 if ((error = priv_check(curthread, PRIV_SCHED_CPUSET)) != 0)
1834 /* Get the mplock to ensure that the proc is not running */
1836 if (args->pid == 0) {
1839 p = pfind(args->pid);
1846 lp = FIRST_LWP_IN_PROC(p);
1848 * XXX: if lwp_cpumask is ever changed to support more than
1849 * 32 processors, this needs to be changed to a bcopy.
1851 mask = lp->lwp_cpumask;
1852 if ((error = copyout(&mask, args->user_mask_ptr, sizeof(cpumask_t))))
1858 args->sysmsg_iresult = sizeof(cpumask_t);
1864 * Set affinity of a process.
1867 sys_linux_sched_setaffinity(struct linux_sched_setaffinity_args *args)
1870 if (ldebug(sched_setaffinity))
1871 kprintf(ARGS(sched_setaffinity, "%d, %d, *"), args->pid,
1875 * From Linux man page:
1876 * sched_setaffinity() sets the CPU affinity mask of the process
1877 * whose ID is pid to the value specified by mask. If pid is zero,
1878 * then the calling process is used. The argument cpusetsize is
1879 * the length (in bytes) of the data pointed to by mask. Normally
1880 * this argument would be specified as sizeof(cpu_set_t).
1882 * If the process specified by pid is not currently running on one
1883 * of the CPUs specified in mask, then that process is migrated to
1884 * one of the CPUs specified in mask.
1887 * About our implementation: I don't think that it is too important
1888 * to have a working implementation, but if it was ever needed,
1889 * the best approach would be to implement the whole mechanism
1890 * properly in kern_usched.
1891 * The idea has to be to change the affinity mask AND migrate the
1892 * lwp to one of the new valid CPUs for the lwp, in case the current
1893 * CPU isn't anymore in the affinity mask passed in.
1894 * For now, we'll just signal success even if we didn't do anything.
1900 sys_linux_gettid(struct linux_gettid_args *args)
1902 args->sysmsg_iresult = curproc->p_pid;
1907 sys_linux_getcpu(struct linux_getcpu_args *args)
1909 struct globaldata *gd;
1914 error = copyout(&gd->gd_cpuid, args->pcpu, sizeof(gd->gd_cpuid));
1918 * XXX: this should be the NUMA node, but since we don't implement it,
1919 * just return 0 for it.
1921 error = copyout(&node, args->pnode, sizeof(node));