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, NULL,
419 0, &vmaddr, a_out->a_text + a_out->a_data,
421 FALSE, VM_MAPTYPE_NORMAL,
422 VM_PROT_ALL, VM_PROT_ALL, 0);
426 /* map file into kernel_map */
427 error = vm_mmap(&kernel_map, &buffer,
428 round_page(a_out->a_text + a_out->a_data + file_offset),
429 VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp,
430 trunc_page(file_offset));
434 /* copy from kernel VM space to user space */
435 error = copyout((caddr_t)(uintptr_t)(buffer + file_offset),
436 (caddr_t)vmaddr, a_out->a_text + a_out->a_data);
438 /* release temporary kernel space */
439 vm_map_remove(&kernel_map, buffer, buffer +
440 round_page(a_out->a_text + a_out->a_data + file_offset));
446 kprintf("uselib: Page aligned binary %lu\n", file_offset);
449 * for QMAGIC, a_entry is 20 bytes beyond the load address
450 * to skip the executable header
452 vmaddr = trunc_page(a_out->a_entry);
455 * Map it all into the process's space as a single
456 * copy-on-write "data" segment.
458 error = vm_mmap(&p->p_vmspace->vm_map, &vmaddr,
459 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
460 MAP_PRIVATE | MAP_FIXED, (caddr_t)vp, file_offset);
465 kprintf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long*)vmaddr)[0],
469 /* Calculate BSS start address */
470 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
473 /* allocate some 'anon' space */
474 error = vm_map_find(&p->p_vmspace->vm_map, NULL, NULL,
475 0, &vmaddr, bss_size,
477 FALSE, VM_MAPTYPE_NORMAL,
478 VM_PROT_ALL, VM_PROT_ALL, 0);
484 /* Unlock/release vnode */
490 /* Release the kernel mapping. */
492 vm_map_remove(&kernel_map, (vm_offset_t)a_out,
493 (vm_offset_t)a_out + PAGE_SIZE);
497 linux_free_path(&path);
505 sys_linux_select(struct linux_select_args *args)
507 struct select_args bsa;
508 struct timeval tv0, tv1, utv, *tvp;
514 kprintf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
515 (void *)args->readfds, (void *)args->writefds,
516 (void *)args->exceptfds, (void *)args->timeout);
520 bsa.sysmsg_result = 0;
522 bsa.in = args->readfds;
523 bsa.ou = args->writefds;
524 bsa.ex = args->exceptfds;
525 bsa.tv = (struct timeval *)args->timeout;
528 * Store current time for computation of the amount of
532 if ((error = copyin((caddr_t)args->timeout, &utv,
537 kprintf(LMSG("incoming timeout (%ld/%ld)"),
538 utv.tv_sec, utv.tv_usec);
541 if (itimerfix(&utv)) {
543 * The timeval was invalid. Convert it to something
544 * valid that will act as it does under Linux.
546 sg = stackgap_init();
547 tvp = stackgap_alloc(&sg, sizeof(utv));
548 utv.tv_sec += utv.tv_usec / 1000000;
549 utv.tv_usec %= 1000000;
550 if (utv.tv_usec < 0) {
552 utv.tv_usec += 1000000;
556 if ((error = copyout(&utv, tvp, sizeof(utv))))
563 error = sys_select(&bsa);
564 args->sysmsg_result = bsa.sysmsg_result;
567 kprintf(LMSG("real select returns %d"), error);
571 * See fs/select.c in the Linux kernel. Without this,
572 * Maelstrom doesn't work.
574 if (error == ERESTART)
580 if (args->sysmsg_result) {
582 * Compute how much time was left of the timeout,
583 * by subtracting the current time and the time
584 * before we started the call, and subtracting
585 * that result from the user-supplied value.
588 timevalsub(&tv1, &tv0);
589 timevalsub(&utv, &tv1);
596 kprintf(LMSG("outgoing timeout (%ld/%ld)"),
597 utv.tv_sec, utv.tv_usec);
599 if ((error = copyout(&utv, (caddr_t)args->timeout,
607 kprintf(LMSG("select_out -> %d"), error);
616 sys_linux_mremap(struct linux_mremap_args *args)
618 struct munmap_args bsd_args;
623 kprintf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
625 (unsigned long)args->old_len,
626 (unsigned long)args->new_len,
627 (unsigned long)args->flags);
629 if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) {
630 args->sysmsg_resultp = NULL;
635 * Check for the page alignment.
636 * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK.
638 if (args->addr & PAGE_MASK) {
639 args->sysmsg_resultp = NULL;
643 args->new_len = round_page(args->new_len);
644 args->old_len = round_page(args->old_len);
646 if (args->new_len > args->old_len) {
647 args->sysmsg_result = 0;
651 if (args->new_len < args->old_len) {
652 bsd_args.sysmsg_result = 0;
653 bsd_args.addr = (caddr_t)(args->addr + args->new_len);
654 bsd_args.len = args->old_len - args->new_len;
655 error = sys_munmap(&bsd_args);
658 args->sysmsg_resultp = error ? NULL : (void *)args->addr;
662 #define LINUX_MS_ASYNC 0x0001
663 #define LINUX_MS_INVALIDATE 0x0002
664 #define LINUX_MS_SYNC 0x0004
670 sys_linux_msync(struct linux_msync_args *args)
672 struct msync_args bsd_args;
675 bsd_args.addr = (caddr_t)args->addr;
676 bsd_args.len = args->len;
677 bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
678 bsd_args.sysmsg_result = 0;
680 error = sys_msync(&bsd_args);
681 args->sysmsg_result = bsd_args.sysmsg_result;
689 sys_linux_time(struct linux_time_args *args)
697 kprintf(ARGS(time, "*"));
702 if (args->tm && (error = copyout(&tm, (caddr_t)args->tm, sizeof(tm))))
704 args->sysmsg_lresult = tm;
708 struct l_times_argv {
715 #define CLK_TCK 100 /* Linux uses 100 */
717 #define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
723 sys_linux_times(struct linux_times_args *args)
725 struct thread *td = curthread;
726 struct proc *p = td->td_proc;
728 struct l_times_argv tms;
734 kprintf(ARGS(times, "*"));
741 tms.tms_utime = CONVTCK(ru.ru_utime);
742 tms.tms_stime = CONVTCK(ru.ru_stime);
744 tms.tms_cutime = CONVTCK(p->p_cru.ru_utime);
745 tms.tms_cstime = CONVTCK(p->p_cru.ru_stime);
747 if ((error = copyout(&tms, (caddr_t)args->buf, sizeof(tms))))
751 args->sysmsg_result = (int)CONVTCK(tv);
759 sys_linux_newuname(struct linux_newuname_args *args)
761 struct thread *td = curthread;
762 struct l_new_utsname utsname;
763 char *osrelease, *osname;
766 if (ldebug(newuname))
767 kprintf(ARGS(newuname, "*"));
771 osname = linux_get_osname(td);
772 osrelease = linux_get_osrelease(td);
774 bzero(&utsname, sizeof(utsname));
775 strncpy(utsname.sysname, osname, LINUX_MAX_UTSNAME-1);
776 strncpy(utsname.nodename, hostname, LINUX_MAX_UTSNAME-1);
777 strncpy(utsname.release, osrelease, LINUX_MAX_UTSNAME-1);
778 strncpy(utsname.version, version, LINUX_MAX_UTSNAME-1);
779 strncpy(utsname.machine, machine, LINUX_MAX_UTSNAME-1);
780 strncpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME-1);
783 return (copyout(&utsname, (caddr_t)args->buf, sizeof(utsname)));
786 /* XXX: why would this be i386-only? most of these are wrong! */
787 #if defined(__i386__)
797 sys_linux_utime(struct linux_utime_args *args)
799 struct timeval tv[2];
800 struct l_utimbuf lut;
801 struct nlookupdata nd;
805 error = linux_copyin_path(args->fname, &path, LINUX_PATH_EXISTS);
810 kprintf(ARGS(utime, "%s, *"), path);
814 error = copyin(args->times, &lut, sizeof(lut));
817 tv[0].tv_sec = lut.l_actime;
819 tv[1].tv_sec = lut.l_modtime;
823 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
825 error = kern_utimes(&nd, args->times ? tv : NULL);
829 linux_free_path(&path);
834 sys_linux_utimes(struct linux_utimes_args *args)
837 struct timeval tv[2], *tvp = NULL;
838 struct nlookupdata nd;
842 error = linux_copyin_path(args->fname, &path, LINUX_PATH_EXISTS);
847 kprintf(ARGS(utimes, "%s, *"), path);
851 error = copyin(args->tptr, ltv, sizeof(ltv));
854 tv[0].tv_sec = ltv[0].tv_sec;
855 tv[0].tv_usec = ltv[0].tv_usec;
856 tv[1].tv_sec = ltv[1].tv_sec;
857 tv[1].tv_usec = ltv[1].tv_usec;
861 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
863 error = kern_utimes(&nd, tvp);
867 linux_free_path(&path);
872 sys_linux_futimesat(struct linux_futimesat_args *args)
875 struct timeval tv[2], *tvp = NULL;
877 struct nlookupdata nd;
881 error = linux_copyin_path(args->fname, &path, LINUX_PATH_EXISTS);
885 if (ldebug(futimesat))
886 kprintf(ARGS(futimesat, "%s, *"), path);
889 error = copyin(args->tptr, ltv, sizeof(ltv));
892 tv[0].tv_sec = ltv[0].tv_sec;
893 tv[0].tv_usec = ltv[0].tv_usec;
894 tv[1].tv_sec = ltv[1].tv_sec;
895 tv[1].tv_usec = ltv[1].tv_usec;
898 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
900 error = nlookup_init_at(&nd, &fp, dfd, path, UIO_SYSSPACE, NLC_FOLLOW);
902 error = kern_utimes(&nd, tvp);
903 nlookup_done_at(&nd, fp);
906 linux_free_path(&path);
912 sys_linux_utimensat(struct linux_utimensat_args *args)
914 struct l_timespec ltv[2];
915 struct timeval tv[2], *tvp = NULL;
917 struct nlookupdata nd;
919 int dfd, flags, error = 0;
921 if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
924 if (args->dfd == LINUX_AT_FDCWD && args->fname == NULL)
928 error = linux_copyin_path(args->fname, &path, LINUX_PATH_EXISTS);
933 if (ldebug(utimensat))
934 kprintf(ARGS(utimensat, "%s, *"), path);
937 error = copyin(args->tptr, ltv, sizeof(ltv));
941 if (ltv[0].tv_sec == LINUX_UTIME_NOW) {
943 } else if (ltv[0].tv_sec == LINUX_UTIME_OMIT) {
944 /* XXX: this is not right, but will do for now */
947 tv[0].tv_sec = ltv[0].tv_sec;
948 /* XXX: we lose precision here, as we don't have ns */
949 tv[0].tv_usec = ltv[0].tv_nsec/1000;
951 if (ltv[1].tv_sec == LINUX_UTIME_NOW) {
953 } else if (ltv[1].tv_sec == LINUX_UTIME_OMIT) {
954 /* XXX: this is not right, but will do for now */
957 tv[1].tv_sec = ltv[1].tv_sec;
958 /* XXX: we lose precision here, as we don't have ns */
959 tv[1].tv_usec = ltv[1].tv_nsec/1000;
964 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
965 flags = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
969 error = nlookup_init_at(&nd, &fp, dfd, path, UIO_SYSSPACE, flags);
971 error = kern_utimes(&nd, tvp);
972 nlookup_done_at(&nd, fp);
974 /* Thank you, Linux, for another non-standard "feature" */
975 KKASSERT(dfd != AT_FDCWD);
976 error = kern_futimes(dfd, tvp);
981 linux_free_path(&path);
985 #endif /* __i386__ */
987 #define __WCLONE 0x80000000
993 sys_linux_waitpid(struct linux_waitpid_args *args)
995 int error, options, status;
999 kprintf(ARGS(waitpid, "%d, %p, %d"),
1000 args->pid, (void *)args->status, args->options);
1002 options = args->options & (WNOHANG | WUNTRACED);
1003 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
1004 if (args->options & __WCLONE)
1005 options |= WLINUXCLONE;
1007 error = kern_wait(args->pid, args->status ? &status : NULL, options,
1008 NULL, &args->sysmsg_result);
1010 if (error == 0 && args->status) {
1012 if (WIFSIGNALED(status))
1013 status = (status & 0xffffff80) |
1014 BSD_TO_LINUX_SIGNAL(WTERMSIG(status));
1015 else if (WIFSTOPPED(status))
1016 status = (status & 0xffff00ff) |
1017 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(status)) << 8);
1018 error = copyout(&status, args->status, sizeof(status));
1028 sys_linux_wait4(struct linux_wait4_args *args)
1030 struct thread *td = curthread;
1031 struct lwp *lp = td->td_lwp;
1032 struct rusage rusage;
1033 int error, options, status;
1037 kprintf(ARGS(wait4, "%d, %p, %d, %p"),
1038 args->pid, (void *)args->status, args->options,
1039 (void *)args->rusage);
1041 options = args->options & (WNOHANG | WUNTRACED);
1042 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
1043 if (args->options & __WCLONE)
1044 options |= WLINUXCLONE;
1046 error = kern_wait(args->pid, args->status ? &status : NULL, options,
1047 args->rusage ? &rusage : NULL, &args->sysmsg_result);
1050 spin_lock(&lp->lwp_spin);
1051 lwp_delsig(lp, SIGCHLD);
1052 spin_unlock(&lp->lwp_spin);
1055 if (error == 0 && args->status) {
1057 if (WIFSIGNALED(status))
1058 status = (status & 0xffffff80) |
1059 BSD_TO_LINUX_SIGNAL(WTERMSIG(status));
1060 else if (WIFSTOPPED(status))
1061 status = (status & 0xffff00ff) |
1062 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(status)) << 8);
1063 error = copyout(&status, args->status, sizeof(status));
1065 if (error == 0 && args->rusage)
1066 error = copyout(&rusage, args->rusage, sizeof(rusage));
1075 sys_linux_mknod(struct linux_mknod_args *args)
1077 struct nlookupdata nd;
1081 error = linux_copyin_path(args->path, &path, LINUX_PATH_CREATE);
1086 kprintf(ARGS(mknod, "%s, %d, %d"),
1087 path, args->mode, args->dev);
1090 error = nlookup_init(&nd, path, UIO_SYSSPACE, 0);
1092 if (args->mode & S_IFIFO) {
1093 error = kern_mkfifo(&nd, args->mode);
1095 error = kern_mknod(&nd, args->mode,
1103 linux_free_path(&path);
1108 sys_linux_mknodat(struct linux_mknodat_args *args)
1110 struct nlookupdata nd;
1115 error = linux_copyin_path(args->path, &path, LINUX_PATH_CREATE);
1120 kprintf(ARGS(mknod, "%s, %d, %d"),
1121 path, args->mode, args->dev);
1124 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1125 error = nlookup_init_at(&nd, &fp, dfd, path, UIO_SYSSPACE, 0);
1127 if (args->mode & S_IFIFO) {
1128 error = kern_mkfifo(&nd, args->mode);
1130 error = kern_mknod(&nd, args->mode,
1135 nlookup_done_at(&nd, fp);
1138 linux_free_path(&path);
1143 * UGH! This is just about the dumbest idea I've ever heard!!
1148 sys_linux_personality(struct linux_personality_args *args)
1151 if (ldebug(personality))
1152 kprintf(ARGS(personality, "%d"), args->per);
1157 /* Yes Jim, it's still a Linux... */
1158 args->sysmsg_result = 0;
1163 * Wrappers for get/setitimer for debugging..
1168 sys_linux_setitimer(struct linux_setitimer_args *args)
1170 struct setitimer_args bsa;
1171 struct itimerval foo;
1175 if (ldebug(setitimer))
1176 kprintf(ARGS(setitimer, "%p, %p"),
1177 (void *)args->itv, (void *)args->oitv);
1179 bsa.which = args->which;
1180 bsa.itv = (struct itimerval *)args->itv;
1181 bsa.oitv = (struct itimerval *)args->oitv;
1182 bsa.sysmsg_result = 0;
1184 if ((error = copyin((caddr_t)args->itv, &foo, sizeof(foo))))
1187 if (ldebug(setitimer)) {
1188 kprintf("setitimer: value: sec: %ld, usec: %ld\n",
1189 foo.it_value.tv_sec, foo.it_value.tv_usec);
1190 kprintf("setitimer: interval: sec: %ld, usec: %ld\n",
1191 foo.it_interval.tv_sec, foo.it_interval.tv_usec);
1195 error = sys_setitimer(&bsa);
1196 args->sysmsg_result = bsa.sysmsg_result;
1204 sys_linux_getitimer(struct linux_getitimer_args *args)
1206 struct getitimer_args bsa;
1209 if (ldebug(getitimer))
1210 kprintf(ARGS(getitimer, "%p"), (void *)args->itv);
1212 bsa.which = args->which;
1213 bsa.itv = (struct itimerval *)args->itv;
1214 bsa.sysmsg_result = 0;
1215 error = sys_getitimer(&bsa);
1216 args->sysmsg_result = bsa.sysmsg_result;
1224 sys_linux_nice(struct linux_nice_args *args)
1226 struct setpriority_args bsd_args;
1229 bsd_args.which = PRIO_PROCESS;
1230 bsd_args.who = 0; /* current process */
1231 bsd_args.prio = args->inc;
1232 bsd_args.sysmsg_result = 0;
1233 error = sys_setpriority(&bsd_args);
1234 args->sysmsg_result = bsd_args.sysmsg_result;
1242 sys_linux_setgroups(struct linux_setgroups_args *args)
1244 struct thread *td = curthread;
1245 struct proc *p = td->td_proc;
1246 struct ucred *newcred, *oldcred;
1247 l_gid_t linux_gidset[NGROUPS];
1251 ngrp = args->gidsetsize;
1252 oldcred = td->td_ucred;
1255 * cr_groups[0] holds egid. Setting the whole set from
1256 * the supplied set will cause egid to be changed too.
1257 * Keep cr_groups[0] unchanged to prevent that.
1260 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0)
1263 if ((u_int)ngrp >= NGROUPS)
1267 newcred = crdup(oldcred);
1269 error = copyin((caddr_t)args->grouplist, linux_gidset,
1270 ngrp * sizeof(l_gid_t));
1277 newcred->cr_ngroups = ngrp + 1;
1279 bsd_gidset = newcred->cr_groups;
1282 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1286 newcred->cr_ngroups = 1;
1290 oldcred = p->p_ucred; /* reload, deal with threads race */
1291 p->p_ucred = newcred;
1301 sys_linux_getgroups(struct linux_getgroups_args *args)
1303 struct thread *td = curthread;
1305 l_gid_t linux_gidset[NGROUPS];
1307 int bsd_gidsetsz, ngrp, error;
1309 cred = td->td_ucred;
1310 bsd_gidset = cred->cr_groups;
1311 bsd_gidsetsz = cred->cr_ngroups - 1;
1314 * cr_groups[0] holds egid. Returning the whole set
1315 * here will cause a duplicate. Exclude cr_groups[0]
1319 if ((ngrp = args->gidsetsize) == 0) {
1320 args->sysmsg_result = bsd_gidsetsz;
1324 if ((u_int)ngrp < bsd_gidsetsz)
1328 while (ngrp < bsd_gidsetsz) {
1329 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1333 if ((error = copyout(linux_gidset, args->grouplist,
1334 ngrp * sizeof(l_gid_t)))) {
1338 args->sysmsg_result = ngrp;
1346 sys_linux_setrlimit(struct linux_setrlimit_args *args)
1348 struct l_rlimit linux_rlim;
1354 if (ldebug(setrlimit))
1355 kprintf(ARGS(setrlimit, "%d, %p"),
1356 args->resource, (void *)args->rlim);
1358 if (args->resource >= LINUX_RLIM_NLIMITS)
1360 which = linux_to_bsd_resource[args->resource];
1364 error = copyin(args->rlim, &linux_rlim, sizeof(linux_rlim));
1367 rlim.rlim_cur = (rlim_t)linux_rlim.rlim_cur;
1368 rlim.rlim_max = (rlim_t)linux_rlim.rlim_max;
1370 error = kern_setrlimit(which, &rlim);
1379 sys_linux_old_getrlimit(struct linux_old_getrlimit_args *args)
1381 struct l_rlimit linux_rlim;
1387 if (ldebug(old_getrlimit))
1388 kprintf(ARGS(old_getrlimit, "%d, %p"),
1389 args->resource, (void *)args->rlim);
1391 if (args->resource >= LINUX_RLIM_NLIMITS)
1393 which = linux_to_bsd_resource[args->resource];
1397 error = kern_getrlimit(which, &rlim);
1400 linux_rlim.rlim_cur = (l_ulong)rlim.rlim_cur;
1401 if (linux_rlim.rlim_cur == ULONG_MAX)
1402 linux_rlim.rlim_cur = LONG_MAX;
1403 linux_rlim.rlim_max = (l_ulong)rlim.rlim_max;
1404 if (linux_rlim.rlim_max == ULONG_MAX)
1405 linux_rlim.rlim_max = LONG_MAX;
1406 error = copyout(&linux_rlim, args->rlim, sizeof(linux_rlim));
1415 sys_linux_getrlimit(struct linux_getrlimit_args *args)
1417 struct l_rlimit linux_rlim;
1423 if (ldebug(getrlimit))
1424 kprintf(ARGS(getrlimit, "%d, %p"),
1425 args->resource, (void *)args->rlim);
1427 if (args->resource >= LINUX_RLIM_NLIMITS)
1429 which = linux_to_bsd_resource[args->resource];
1433 error = kern_getrlimit(which, &rlim);
1436 linux_rlim.rlim_cur = (l_ulong)rlim.rlim_cur;
1437 linux_rlim.rlim_max = (l_ulong)rlim.rlim_max;
1438 error = copyout(&linux_rlim, args->rlim, sizeof(linux_rlim));
1447 sys_linux_sched_setscheduler(struct linux_sched_setscheduler_args *args)
1449 struct sched_setscheduler_args bsd;
1453 if (ldebug(sched_setscheduler))
1454 kprintf(ARGS(sched_setscheduler, "%d, %d, %p"),
1455 args->pid, args->policy, (const void *)args->param);
1458 switch (args->policy) {
1459 case LINUX_SCHED_OTHER:
1460 bsd.policy = SCHED_OTHER;
1462 case LINUX_SCHED_FIFO:
1463 bsd.policy = SCHED_FIFO;
1465 case LINUX_SCHED_RR:
1466 bsd.policy = SCHED_RR;
1472 bsd.pid = args->pid;
1473 bsd.param = (struct sched_param *)args->param;
1474 bsd.sysmsg_result = 0;
1476 error = sys_sched_setscheduler(&bsd);
1477 args->sysmsg_result = bsd.sysmsg_result;
1485 sys_linux_sched_getscheduler(struct linux_sched_getscheduler_args *args)
1487 struct sched_getscheduler_args bsd;
1491 if (ldebug(sched_getscheduler))
1492 kprintf(ARGS(sched_getscheduler, "%d"), args->pid);
1495 bsd.sysmsg_result = 0;
1496 bsd.pid = args->pid;
1497 error = sys_sched_getscheduler(&bsd);
1498 args->sysmsg_result = bsd.sysmsg_result;
1500 switch (args->sysmsg_result) {
1502 args->sysmsg_result = LINUX_SCHED_OTHER;
1505 args->sysmsg_result = LINUX_SCHED_FIFO;
1508 args->sysmsg_result = LINUX_SCHED_RR;
1518 sys_linux_sched_get_priority_max(struct linux_sched_get_priority_max_args *args)
1520 struct sched_get_priority_max_args bsd;
1524 if (ldebug(sched_get_priority_max))
1525 kprintf(ARGS(sched_get_priority_max, "%d"), args->policy);
1528 switch (args->policy) {
1529 case LINUX_SCHED_OTHER:
1530 bsd.policy = SCHED_OTHER;
1532 case LINUX_SCHED_FIFO:
1533 bsd.policy = SCHED_FIFO;
1535 case LINUX_SCHED_RR:
1536 bsd.policy = SCHED_RR;
1541 bsd.sysmsg_result = 0;
1543 error = sys_sched_get_priority_max(&bsd);
1544 args->sysmsg_result = bsd.sysmsg_result;
1552 sys_linux_sched_get_priority_min(struct linux_sched_get_priority_min_args *args)
1554 struct sched_get_priority_min_args bsd;
1558 if (ldebug(sched_get_priority_min))
1559 kprintf(ARGS(sched_get_priority_min, "%d"), args->policy);
1562 switch (args->policy) {
1563 case LINUX_SCHED_OTHER:
1564 bsd.policy = SCHED_OTHER;
1566 case LINUX_SCHED_FIFO:
1567 bsd.policy = SCHED_FIFO;
1569 case LINUX_SCHED_RR:
1570 bsd.policy = SCHED_RR;
1575 bsd.sysmsg_result = 0;
1577 error = sys_sched_get_priority_min(&bsd);
1578 args->sysmsg_result = bsd.sysmsg_result;
1582 #define REBOOT_CAD_ON 0x89abcdef
1583 #define REBOOT_CAD_OFF 0
1584 #define REBOOT_HALT 0xcdef0123
1585 #define REBOOT_RESTART 0x01234567
1586 #define REBOOT_RESTART2 0xA1B2C3D4
1587 #define REBOOT_POWEROFF 0x4321FEDC
1588 #define REBOOT_MAGIC1 0xfee1dead
1589 #define REBOOT_MAGIC2 0x28121969
1590 #define REBOOT_MAGIC2A 0x05121996
1591 #define REBOOT_MAGIC2B 0x16041998
1597 sys_linux_reboot(struct linux_reboot_args *args)
1599 struct reboot_args bsd_args;
1604 kprintf(ARGS(reboot, "0x%x"), args->cmd);
1607 if ((args->magic1 != REBOOT_MAGIC1) ||
1608 ((args->magic2 != REBOOT_MAGIC2) &&
1609 (args->magic2 != REBOOT_MAGIC2A) &&
1610 (args->magic2 != REBOOT_MAGIC2B)))
1613 switch (args->cmd) {
1615 case REBOOT_CAD_OFF:
1616 return (priv_check(curthread, PRIV_REBOOT));
1619 bsd_args.opt = RB_HALT;
1621 case REBOOT_RESTART:
1622 case REBOOT_RESTART2:
1625 case REBOOT_POWEROFF:
1626 bsd_args.opt = RB_POWEROFF;
1633 bsd_args.sysmsg_result = 0;
1635 error = sys_reboot(&bsd_args);
1636 args->sysmsg_result = bsd_args.sysmsg_result;
1641 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1642 * p->p_retval[1] when COMPAT_43 is defined. This
1643 * globbers registers that are assumed to be preserved. The following
1644 * lightweight syscalls fixes this. See also linux_getgid16() and
1645 * linux_getuid16() in linux_uid16.c.
1647 * linux_getpid() - MP SAFE
1648 * linux_getgid() - MP SAFE
1649 * linux_getuid() - MP SAFE
1656 sys_linux_getpid(struct linux_getpid_args *args)
1658 struct linux_emuldata *em;
1659 struct proc *p = curproc;
1663 em = emuldata_get(p);
1664 if (em == NULL) /* this should never happen */
1665 args->sysmsg_result = p->p_pid;
1667 args->sysmsg_result = em->s->group_pid;
1677 sys_linux_getppid(struct linux_getppid_args *args)
1679 struct linux_emuldata *em;
1680 struct proc *parent;
1685 em = emuldata_get(curproc);
1686 KKASSERT(em != NULL);
1687 group_pid = em->s->group_pid;
1690 p = pfind(group_pid);
1691 /* We are not allowed to fail */
1696 if (parent->p_sysent == &elf_linux_sysvec) {
1698 em = emuldata_get(parent);
1699 args->sysmsg_result = em->s->group_pid;
1702 args->sysmsg_result = parent->p_pid;
1714 sys_linux_getgid(struct linux_getgid_args *args)
1716 struct thread *td = curthread;
1718 args->sysmsg_result = td->td_ucred->cr_rgid;
1726 sys_linux_getuid(struct linux_getuid_args *args)
1728 struct thread *td = curthread;
1730 args->sysmsg_result = td->td_ucred->cr_ruid;
1738 sys_linux_getsid(struct linux_getsid_args *args)
1740 struct getsid_args bsd;
1743 bsd.sysmsg_result = 0;
1744 bsd.pid = args->pid;
1745 error = sys_getsid(&bsd);
1746 args->sysmsg_result = bsd.sysmsg_result;
1754 linux_nosys(struct nosys_args *args)
1761 sys_linux_mq_open(struct linux_mq_open_args *args)
1763 struct mq_open_args moa;
1767 if (args->oflag & LINUX_O_RDONLY)
1769 if (args->oflag & LINUX_O_WRONLY)
1771 if (args->oflag & LINUX_O_RDWR)
1774 if (args->oflag & LINUX_O_NONBLOCK)
1775 oflag |= O_NONBLOCK;
1776 if (args->oflag & LINUX_O_CREAT)
1778 if (args->oflag & LINUX_O_EXCL)
1781 moa.name = args->name;
1783 moa.mode = args->mode;
1784 moa.attr = args->attr;
1786 error = sys_mq_open(&moa);
1792 sys_linux_mq_getsetattr(struct linux_mq_getsetattr_args *args)
1794 struct mq_getattr_args gaa;
1795 struct mq_setattr_args saa;
1798 gaa.mqdes = args->mqd;
1799 gaa.mqstat = args->oattr;
1801 saa.mqdes = args->mqd;
1802 saa.mqstat = args->attr;
1803 saa.mqstat = args->oattr;
1805 if (args->attr != NULL) {
1806 error = sys_mq_setattr(&saa);
1808 error = sys_mq_getattr(&gaa);
1815 * Get affinity of a process.
1818 sys_linux_sched_getaffinity(struct linux_sched_getaffinity_args *args)
1826 if (ldebug(sched_getaffinity))
1827 kprintf(ARGS(sched_getaffinity, "%d, %d, *"), args->pid,
1830 if (args->len < sizeof(cpumask_t))
1833 if ((error = priv_check(curthread, PRIV_SCHED_CPUSET)) != 0)
1836 /* Get the mplock to ensure that the proc is not running */
1838 if (args->pid == 0) {
1842 p = pfind(args->pid);
1849 lp = FIRST_LWP_IN_PROC(p);
1851 * XXX: if lwp_cpumask is ever changed to support more than
1852 * 32 processors, this needs to be changed to a bcopy.
1854 mask = lp->lwp_cpumask;
1855 if ((error = copyout(&mask, args->user_mask_ptr, sizeof(cpumask_t))))
1861 args->sysmsg_iresult = sizeof(cpumask_t);
1869 * Set affinity of a process.
1872 sys_linux_sched_setaffinity(struct linux_sched_setaffinity_args *args)
1875 if (ldebug(sched_setaffinity))
1876 kprintf(ARGS(sched_setaffinity, "%d, %d, *"), args->pid,
1880 * From Linux man page:
1881 * sched_setaffinity() sets the CPU affinity mask of the process
1882 * whose ID is pid to the value specified by mask. If pid is zero,
1883 * then the calling process is used. The argument cpusetsize is
1884 * the length (in bytes) of the data pointed to by mask. Normally
1885 * this argument would be specified as sizeof(cpu_set_t).
1887 * If the process specified by pid is not currently running on one
1888 * of the CPUs specified in mask, then that process is migrated to
1889 * one of the CPUs specified in mask.
1892 * About our implementation: I don't think that it is too important
1893 * to have a working implementation, but if it was ever needed,
1894 * the best approach would be to implement the whole mechanism
1895 * properly in kern_usched.
1896 * The idea has to be to change the affinity mask AND migrate the
1897 * lwp to one of the new valid CPUs for the lwp, in case the current
1898 * CPU isn't anymore in the affinity mask passed in.
1899 * For now, we'll just signal success even if we didn't do anything.
1905 sys_linux_gettid(struct linux_gettid_args *args)
1907 args->sysmsg_iresult = curproc->p_pid;
1912 sys_linux_getcpu(struct linux_getcpu_args *args)
1914 struct globaldata *gd;
1919 error = copyout(&gd->gd_cpuid, args->pcpu, sizeof(gd->gd_cpuid));
1923 * XXX: this should be the NUMA node, but since we don't implement it,
1924 * just return 0 for it.
1926 error = copyout(&node, args->pnode, sizeof(node));
1931 sys_linux_sethostname(struct linux_sethostname_args *uap)
1933 struct thread *td = curthread;
1940 name[1] = KERN_HOSTNAME;
1941 error = priv_check_cred(td->td_ucred, PRIV_SETHOSTNAME, 0);
1944 len = MIN(uap->len, MAXHOSTNAMELEN);
1945 hostname = kmalloc(MAXHOSTNAMELEN, M_TEMP, M_WAITOK);
1947 error = copyin(uap->hostname, hostname, len);
1949 kfree(hostname, M_TEMP);
1954 error = kernel_sysctl(name, 2, NULL, 0, hostname, len, NULL);
1957 kfree(hostname, M_TEMP);