2 * Copyright (c) 2002 Doug Rabson
3 * Copyright (c) 1994-1995 Søren Schmidt
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer
11 * in this position and unchanged.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/blist.h>
37 #include <sys/fcntl.h>
38 #if defined(__i386__) || defined(__alpha__)
39 #include <sys/imgact_aout.h>
42 #include <sys/kernel.h>
43 #include <sys/limits.h>
46 #include <sys/malloc.h>
48 #include <sys/mount.h>
49 #include <sys/mutex.h>
50 #include <sys/namei.h>
52 #include <sys/reboot.h>
53 #include <sys/resourcevar.h>
54 #include <sys/signalvar.h>
56 #include <sys/syscallsubr.h>
57 #include <sys/sysctl.h>
58 #include <sys/sysproto.h>
59 #include <sys/systm.h>
61 #include <sys/vmmeter.h>
62 #include <sys/vnode.h>
67 #include <vm/vm_kern.h>
68 #include <vm/vm_map.h>
69 #include <vm/vm_extern.h>
70 #include <vm/vm_object.h>
71 #include <vm/swap_pager.h>
73 #include <posix4/sched.h>
75 #include "opt_compat.h"
77 #include <compat/linux/linux_sysproto.h>
80 #include <machine/../linux32/linux.h>
81 #include <machine/../linux32/linux32_proto.h>
83 #include <machine/../linux/linux.h>
84 #include <machine/../linux/linux_proto.h>
87 #include <compat/linux/linux_mib.h>
88 #include <compat/linux/linux_util.h>
91 #include <machine/cputypes.h>
95 #define BSD_TO_LINUX_SIGNAL(sig) (sig)
97 #define BSD_TO_LINUX_SIGNAL(sig) \
98 (((sig) <= LINUX_SIGTBLSZ) ? bsd_to_linux_signal[_SIG_IDX(sig)] : sig)
102 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
103 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
104 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
105 RLIMIT_MEMLOCK, RLIMIT_AS
107 #endif /*!__alpha__*/
110 l_long uptime; /* Seconds since boot */
111 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */
112 #define LINUX_SYSINFO_LOADS_SCALE 65536
113 l_ulong totalram; /* Total usable main memory size */
114 l_ulong freeram; /* Available memory size */
115 l_ulong sharedram; /* Amount of shared memory */
116 l_ulong bufferram; /* Memory used by buffers */
117 l_ulong totalswap; /* Total swap space size */
118 l_ulong freeswap; /* swap space still available */
119 l_ushort procs; /* Number of current processes */
123 char _f[6]; /* Pads structure to 64 bytes */
127 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args)
129 struct l_sysinfo sysinfo;
137 sysinfo.uptime = ts.tv_sec;
139 /* Use the information from the mib to get our load averages */
140 for (i = 0; i < 3; i++)
141 sysinfo.loads[i] = averunnable.ldavg[i] *
142 LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale;
144 sysinfo.totalram = physmem * PAGE_SIZE;
145 sysinfo.freeram = sysinfo.totalram - cnt.v_wire_count * PAGE_SIZE;
147 sysinfo.sharedram = 0;
148 mtx_lock(&vm_object_list_mtx);
149 TAILQ_FOREACH(object, &vm_object_list, object_list)
150 if (object->shadow_count > 1)
151 sysinfo.sharedram += object->resident_page_count;
152 mtx_unlock(&vm_object_list_mtx);
154 sysinfo.sharedram *= PAGE_SIZE;
155 sysinfo.bufferram = 0;
157 swap_pager_status(&i, &j);
158 sysinfo.totalswap = i * PAGE_SIZE;
159 sysinfo.freeswap = (i - j) * PAGE_SIZE;
161 sysinfo.procs = nprocs;
163 /* The following are only present in newer Linux kernels. */
164 sysinfo.totalbig = 0;
166 sysinfo.mem_unit = 1;
168 return copyout(&sysinfo, args->info, sizeof(sysinfo));
170 #endif /*!__alpha__*/
174 linux_alarm(struct thread *td, struct linux_alarm_args *args)
176 struct itimerval it, old_it;
181 printf(ARGS(alarm, "%u"), args->secs);
184 if (args->secs > 100000000)
187 it.it_value.tv_sec = (long)args->secs;
188 it.it_value.tv_usec = 0;
189 it.it_interval.tv_sec = 0;
190 it.it_interval.tv_usec = 0;
191 error = kern_setitimer(td, ITIMER_REAL, &it, &old_it);
194 if (timevalisset(&old_it.it_value)) {
195 if (old_it.it_value.tv_usec != 0)
196 old_it.it_value.tv_sec++;
197 td->td_retval[0] = old_it.it_value.tv_sec;
201 #endif /*!__alpha__*/
204 linux_brk(struct thread *td, struct linux_brk_args *args)
206 struct vmspace *vm = td->td_proc->p_vmspace;
207 vm_offset_t new, old;
208 struct obreak_args /* {
214 printf(ARGS(brk, "%p"), (void *)(uintptr_t)args->dsend);
216 old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
217 new = (vm_offset_t)args->dsend;
218 tmp.nsize = (char *)new;
219 if (((caddr_t)new > vm->vm_daddr) && !obreak(td, &tmp))
220 td->td_retval[0] = (long)new;
222 td->td_retval[0] = (long)old;
227 #if defined(__i386__) || defined(__alpha__)
230 linux_uselib(struct thread *td, struct linux_uselib_args *args)
237 unsigned long file_offset;
239 unsigned long bss_size;
244 LCONVPATHEXIST(td, args->library, &library);
248 printf(ARGS(uselib, "%s"), library);
256 * XXX: This code should make use of vn_open(), rather than doing
257 * all this stuff itself.
259 NDINIT(&ni, LOOKUP, ISOPEN|FOLLOW|LOCKLEAF, UIO_SYSSPACE, library, td);
267 * XXX - This looks like a bogus check. A LOCKLEAF namei should not
268 * succeed without returning a vnode.
271 error = ENOEXEC; /* ?? */
274 NDFREE(&ni, NDF_ONLY_PNBUF);
277 * From here on down, we have a locked vnode that must be unlocked.
282 if (vp->v_writecount) {
288 error = VOP_GETATTR(vp, &attr, td->td_ucred, td);
292 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
293 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
294 /* EACCESS is what exec(2) returns. */
300 if (attr.va_size == 0) {
305 /* Can we access it? */
306 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
311 * XXX: This should use vn_open() so that it is properly authorized,
312 * and to reduce code redundancy all over the place here.
315 error = mac_check_vnode_open(td->td_ucred, vp, FREAD);
319 error = VOP_OPEN(vp, FREAD, td->td_ucred, td, -1);
323 /* Pull in executable header into kernel_map */
324 error = vm_mmap(kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE,
325 VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 0);
327 * Lock no longer needed
330 VOP_UNLOCK(vp, 0, td);
335 /* Is it a Linux binary ? */
336 if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
342 * While we are here, we should REALLY do some more checks
345 /* Set file/virtual offset based on a.out variant. */
346 switch ((int)(a_out->a_magic & 0xffff)) {
347 case 0413: /* ZMAGIC */
350 case 0314: /* QMAGIC */
358 bss_size = round_page(a_out->a_bss);
360 /* Check various fields in header for validity/bounds. */
361 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
366 /* text + data can't exceed file size */
367 if (a_out->a_data + a_out->a_text > attr.va_size) {
373 * text/data/bss must not exceed limits
374 * XXX - this is not complete. it should check current usage PLUS
375 * the resources needed by this library.
377 PROC_LOCK(td->td_proc);
378 if (a_out->a_text > maxtsiz ||
379 a_out->a_data + bss_size > lim_cur(td->td_proc, RLIMIT_DATA)) {
380 PROC_UNLOCK(td->td_proc);
384 PROC_UNLOCK(td->td_proc);
386 mp_fixme("Unlocked vflags access.");
387 /* prevent more writers */
388 vp->v_vflag |= VV_TEXT;
391 * Check if file_offset page aligned. Currently we cannot handle
392 * misalinged file offsets, and so we read in the entire image
395 if (file_offset & PAGE_MASK) {
397 printf("uselib: Non page aligned binary %lu\n", file_offset);
399 /* Map text+data read/write/execute */
401 /* a_entry is the load address and is page aligned */
402 vmaddr = trunc_page(a_out->a_entry);
404 /* get anon user mapping, read+write+execute */
405 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
406 &vmaddr, a_out->a_text + a_out->a_data, FALSE, VM_PROT_ALL,
411 /* map file into kernel_map */
412 error = vm_mmap(kernel_map, &buffer,
413 round_page(a_out->a_text + a_out->a_data + file_offset),
414 VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp,
415 trunc_page(file_offset));
419 /* copy from kernel VM space to user space */
420 error = copyout(PTRIN(buffer + file_offset),
421 (void *)vmaddr, a_out->a_text + a_out->a_data);
423 /* release temporary kernel space */
424 vm_map_remove(kernel_map, buffer, buffer +
425 round_page(a_out->a_text + a_out->a_data + file_offset));
431 printf("uselib: Page aligned binary %lu\n", file_offset);
434 * for QMAGIC, a_entry is 20 bytes beyond the load address
435 * to skip the executable header
437 vmaddr = trunc_page(a_out->a_entry);
440 * Map it all into the process's space as a single
441 * copy-on-write "data" segment.
443 error = vm_mmap(&td->td_proc->p_vmspace->vm_map, &vmaddr,
444 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
445 MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset);
450 printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long *)vmaddr)[0],
451 ((long *)vmaddr)[1]);
454 /* Calculate BSS start address */
455 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
458 /* allocate some 'anon' space */
459 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
460 &vmaddr, bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
466 /* Unlock vnode if needed */
468 VOP_UNLOCK(vp, 0, td);
470 /* Release the kernel mapping. */
472 vm_map_remove(kernel_map, (vm_offset_t)a_out,
473 (vm_offset_t)a_out + PAGE_SIZE);
478 #endif /* __i386__ || __alpha__ */
481 linux_select(struct thread *td, struct linux_select_args *args)
484 struct timeval tv0, tv1, utv, *tvp;
489 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
490 (void *)args->readfds, (void *)args->writefds,
491 (void *)args->exceptfds, (void *)args->timeout);
495 * Store current time for computation of the amount of
499 if ((error = copyin(args->timeout, <v, sizeof(ltv))))
501 utv.tv_sec = ltv.tv_sec;
502 utv.tv_usec = ltv.tv_usec;
505 printf(LMSG("incoming timeout (%jd/%ld)"),
506 (intmax_t)utv.tv_sec, utv.tv_usec);
509 if (itimerfix(&utv)) {
511 * The timeval was invalid. Convert it to something
512 * valid that will act as it does under Linux.
514 utv.tv_sec += utv.tv_usec / 1000000;
515 utv.tv_usec %= 1000000;
516 if (utv.tv_usec < 0) {
518 utv.tv_usec += 1000000;
528 error = kern_select(td, args->nfds, args->readfds, args->writefds,
529 args->exceptfds, tvp);
533 printf(LMSG("real select returns %d"), error);
537 * See fs/select.c in the Linux kernel. Without this,
538 * Maelstrom doesn't work.
540 if (error == ERESTART)
546 if (td->td_retval[0]) {
548 * Compute how much time was left of the timeout,
549 * by subtracting the current time and the time
550 * before we started the call, and subtracting
551 * that result from the user-supplied value.
554 timevalsub(&tv1, &tv0);
555 timevalsub(&utv, &tv1);
562 printf(LMSG("outgoing timeout (%jd/%ld)"),
563 (intmax_t)utv.tv_sec, utv.tv_usec);
565 ltv.tv_sec = utv.tv_sec;
566 ltv.tv_usec = utv.tv_usec;
567 if ((error = copyout(<v, args->timeout, sizeof(ltv))))
574 printf(LMSG("select_out -> %d"), error);
580 linux_mremap(struct thread *td, struct linux_mremap_args *args)
582 struct munmap_args /* {
590 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
591 (void *)(uintptr_t)args->addr,
592 (unsigned long)args->old_len,
593 (unsigned long)args->new_len,
594 (unsigned long)args->flags);
596 args->new_len = round_page(args->new_len);
597 args->old_len = round_page(args->old_len);
599 if (args->new_len > args->old_len) {
600 td->td_retval[0] = 0;
604 if (args->new_len < args->old_len) {
606 (caddr_t)((uintptr_t)args->addr + args->new_len);
607 bsd_args.len = args->old_len - args->new_len;
608 error = munmap(td, &bsd_args);
611 td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
615 #define LINUX_MS_ASYNC 0x0001
616 #define LINUX_MS_INVALIDATE 0x0002
617 #define LINUX_MS_SYNC 0x0004
620 linux_msync(struct thread *td, struct linux_msync_args *args)
622 struct msync_args bsd_args;
624 bsd_args.addr = (caddr_t)(uintptr_t)args->addr;
625 bsd_args.len = (uintptr_t)args->len;
626 bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
628 return msync(td, &bsd_args);
633 linux_time(struct thread *td, struct linux_time_args *args)
641 printf(ARGS(time, "*"));
646 if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
648 td->td_retval[0] = tm;
651 #endif /*!__alpha__*/
653 struct l_times_argv {
661 #define CLK_TCK 1024 /* Linux uses 1024 on alpha */
663 #define CLK_TCK 100 /* Linux uses 100 */
666 #define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
669 linux_times(struct thread *td, struct linux_times_args *args)
671 struct timeval tv, utime, stime, cutime, cstime;
672 struct l_times_argv tms;
678 printf(ARGS(times, "*"));
681 if (args->buf != NULL) {
684 calcru(p, &utime, &stime);
685 calccru(p, &cutime, &cstime);
688 tms.tms_utime = CONVTCK(utime);
689 tms.tms_stime = CONVTCK(stime);
691 tms.tms_cutime = CONVTCK(cutime);
692 tms.tms_cstime = CONVTCK(cstime);
694 if ((error = copyout(&tms, args->buf, sizeof(tms))))
699 td->td_retval[0] = (int)CONVTCK(tv);
704 linux_newuname(struct thread *td, struct linux_newuname_args *args)
706 struct l_new_utsname utsname;
707 char osname[LINUX_MAX_UTSNAME];
708 char osrelease[LINUX_MAX_UTSNAME];
712 if (ldebug(newuname))
713 printf(ARGS(newuname, "*"));
716 linux_get_osname(td, osname);
717 linux_get_osrelease(td, osrelease);
719 bzero(&utsname, sizeof(utsname));
720 strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
721 getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
722 strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
723 strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
724 for (p = utsname.version; *p != '\0'; ++p)
746 strlcpy(utsname.machine, class, LINUX_MAX_UTSNAME);
748 #elif defined(__amd64__) /* XXX: Linux can change 'personality'. */
749 #ifdef COMPAT_LINUX32
750 strlcpy(utsname.machine, "i686", LINUX_MAX_UTSNAME);
752 strlcpy(utsname.machine, "x86_64", LINUX_MAX_UTSNAME);
753 #endif /* COMPAT_LINUX32 */
754 #else /* something other than i386 or amd64 - assume we and Linux agree */
755 strlcpy(utsname.machine, machine, LINUX_MAX_UTSNAME);
756 #endif /* __i386__ */
757 strlcpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME);
759 return (copyout(&utsname, args->buf, sizeof(utsname)));
762 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
769 linux_utime(struct thread *td, struct linux_utime_args *args)
771 struct timeval tv[2], *tvp;
772 struct l_utimbuf lut;
776 LCONVPATHEXIST(td, args->fname, &fname);
780 printf(ARGS(utime, "%s, *"), fname);
784 if ((error = copyin(args->times, &lut, sizeof lut))) {
788 tv[0].tv_sec = lut.l_actime;
790 tv[1].tv_sec = lut.l_modtime;
796 error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
802 linux_utimes(struct thread *td, struct linux_utimes_args *args)
805 struct timeval tv[2], *tvp = NULL;
809 LCONVPATHEXIST(td, args->fname, &fname);
813 printf(ARGS(utimes, "%s, *"), fname);
816 if (args->tptr != NULL) {
817 if ((error = copyin(args->tptr, ltv, sizeof ltv))) {
821 tv[0].tv_sec = ltv[0].tv_sec;
822 tv[0].tv_usec = ltv[0].tv_usec;
823 tv[1].tv_sec = ltv[1].tv_sec;
824 tv[1].tv_usec = ltv[1].tv_usec;
828 error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
832 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
834 #define __WCLONE 0x80000000
838 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
840 int error, options, tmpstat;
844 printf(ARGS(waitpid, "%d, %p, %d"),
845 args->pid, (void *)args->status, args->options);
848 * this is necessary because the test in kern_wait doesn't work
849 * because we mess with the options here
851 if (args->options & ~(WUNTRACED | WNOHANG | WCONTINUED | __WCLONE))
854 options = (args->options & (WNOHANG | WUNTRACED));
855 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
856 if (args->options & __WCLONE)
857 options |= WLINUXCLONE;
859 error = kern_wait(td, args->pid, &tmpstat, options, NULL);
865 if (WIFSIGNALED(tmpstat))
866 tmpstat = (tmpstat & 0xffffff80) |
867 BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
868 else if (WIFSTOPPED(tmpstat))
869 tmpstat = (tmpstat & 0xffff00ff) |
870 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
871 return copyout(&tmpstat, args->status, sizeof(int));
876 #endif /*!__alpha__*/
879 linux_wait4(struct thread *td, struct linux_wait4_args *args)
881 int error, options, tmpstat;
882 struct rusage ru, *rup;
887 printf(ARGS(wait4, "%d, %p, %d, %p"),
888 args->pid, (void *)args->status, args->options,
889 (void *)args->rusage);
892 options = (args->options & (WNOHANG | WUNTRACED));
893 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
894 if (args->options & __WCLONE)
895 options |= WLINUXCLONE;
897 if (args->rusage != NULL)
901 error = kern_wait(td, args->pid, &tmpstat, options, rup);
907 SIGDELSET(p->p_siglist, SIGCHLD);
912 if (WIFSIGNALED(tmpstat))
913 tmpstat = (tmpstat & 0xffffff80) |
914 BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
915 else if (WIFSTOPPED(tmpstat))
916 tmpstat = (tmpstat & 0xffff00ff) |
917 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
918 error = copyout(&tmpstat, args->status, sizeof(int));
920 if (args->rusage != NULL && error == 0)
921 error = copyout(&ru, args->rusage, sizeof(ru));
927 linux_mknod(struct thread *td, struct linux_mknod_args *args)
932 LCONVPATHCREAT(td, args->path, &path);
936 printf(ARGS(mknod, "%s, %d, %d"), path, args->mode, args->dev);
939 switch (args->mode & S_IFMT) {
942 error = kern_mkfifo(td, path, UIO_SYSSPACE, args->mode);
947 error = kern_mknod(td, path, UIO_SYSSPACE, args->mode,
956 args->mode |= S_IFREG;
959 error = kern_open(td, path, UIO_SYSSPACE,
960 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
972 * UGH! This is just about the dumbest idea I've ever heard!!
975 linux_personality(struct thread *td, struct linux_personality_args *args)
978 if (ldebug(personality))
979 printf(ARGS(personality, "%lu"), (unsigned long)args->per);
986 /* Yes Jim, it's still a Linux... */
987 td->td_retval[0] = 0;
992 l_timeval it_interval;
996 #define B2L_ITIMERVAL(bip, lip) \
997 (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec; \
998 (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec; \
999 (bip)->it_value.tv_sec = (lip)->it_value.tv_sec; \
1000 (bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
1003 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
1006 struct l_itimerval ls;
1007 struct itimerval aitv, oitv;
1010 if (ldebug(setitimer))
1011 printf(ARGS(setitimer, "%p, %p"),
1012 (void *)uap->itv, (void *)uap->oitv);
1015 if (uap->itv == NULL) {
1016 uap->itv = uap->oitv;
1017 return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
1020 error = copyin(uap->itv, &ls, sizeof(ls));
1023 B2L_ITIMERVAL(&aitv, &ls);
1025 if (ldebug(setitimer)) {
1026 printf("setitimer: value: sec: %jd, usec: %ld\n",
1027 (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec);
1028 printf("setitimer: interval: sec: %jd, usec: %ld\n",
1029 (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec);
1032 error = kern_setitimer(td, uap->which, &aitv, &oitv);
1033 if (error != 0 || uap->oitv == NULL)
1035 B2L_ITIMERVAL(&ls, &oitv);
1037 return (copyout(&ls, uap->oitv, sizeof(ls)));
1041 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
1044 struct l_itimerval ls;
1045 struct itimerval aitv;
1048 if (ldebug(getitimer))
1049 printf(ARGS(getitimer, "%p"), (void *)uap->itv);
1051 error = kern_getitimer(td, uap->which, &aitv);
1054 B2L_ITIMERVAL(&ls, &aitv);
1055 return (copyout(&ls, uap->itv, sizeof(ls)));
1060 linux_nice(struct thread *td, struct linux_nice_args *args)
1062 struct setpriority_args bsd_args;
1064 bsd_args.which = PRIO_PROCESS;
1065 bsd_args.who = 0; /* current process */
1066 bsd_args.prio = args->inc;
1067 return setpriority(td, &bsd_args);
1069 #endif /*!__alpha__*/
1072 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
1074 struct ucred *newcred, *oldcred;
1075 l_gid_t linux_gidset[NGROUPS];
1080 ngrp = args->gidsetsize;
1081 if (ngrp < 0 || ngrp >= NGROUPS)
1083 error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
1089 oldcred = p->p_ucred;
1092 * cr_groups[0] holds egid. Setting the whole set from
1093 * the supplied set will cause egid to be changed too.
1094 * Keep cr_groups[0] unchanged to prevent that.
1097 if ((error = suser_cred(oldcred, SUSER_ALLOWJAIL)) != 0) {
1103 crcopy(newcred, oldcred);
1105 newcred->cr_ngroups = ngrp + 1;
1107 bsd_gidset = newcred->cr_groups;
1110 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1114 newcred->cr_ngroups = 1;
1117 p->p_ucred = newcred;
1124 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1127 l_gid_t linux_gidset[NGROUPS];
1129 int bsd_gidsetsz, ngrp, error;
1131 cred = td->td_ucred;
1132 bsd_gidset = cred->cr_groups;
1133 bsd_gidsetsz = cred->cr_ngroups - 1;
1136 * cr_groups[0] holds egid. Returning the whole set
1137 * here will cause a duplicate. Exclude cr_groups[0]
1141 if ((ngrp = args->gidsetsize) == 0) {
1142 td->td_retval[0] = bsd_gidsetsz;
1146 if (ngrp < bsd_gidsetsz)
1150 while (ngrp < bsd_gidsetsz) {
1151 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1155 if ((error = copyout(linux_gidset, args->grouplist,
1156 ngrp * sizeof(l_gid_t))))
1159 td->td_retval[0] = ngrp;
1165 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1167 struct rlimit bsd_rlim;
1168 struct l_rlimit rlim;
1173 if (ldebug(setrlimit))
1174 printf(ARGS(setrlimit, "%d, %p"),
1175 args->resource, (void *)args->rlim);
1178 if (args->resource >= LINUX_RLIM_NLIMITS)
1181 which = linux_to_bsd_resource[args->resource];
1185 error = copyin(args->rlim, &rlim, sizeof(rlim));
1189 bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1190 bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1191 return (kern_setrlimit(td, which, &bsd_rlim));
1195 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1197 struct l_rlimit rlim;
1198 struct proc *p = td->td_proc;
1199 struct rlimit bsd_rlim;
1203 if (ldebug(old_getrlimit))
1204 printf(ARGS(old_getrlimit, "%d, %p"),
1205 args->resource, (void *)args->rlim);
1208 if (args->resource >= LINUX_RLIM_NLIMITS)
1211 which = linux_to_bsd_resource[args->resource];
1216 lim_rlimit(p, which, &bsd_rlim);
1219 #ifdef COMPAT_LINUX32
1220 rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1221 if (rlim.rlim_cur == UINT_MAX)
1222 rlim.rlim_cur = INT_MAX;
1223 rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1224 if (rlim.rlim_max == UINT_MAX)
1225 rlim.rlim_max = INT_MAX;
1227 rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1228 if (rlim.rlim_cur == ULONG_MAX)
1229 rlim.rlim_cur = LONG_MAX;
1230 rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1231 if (rlim.rlim_max == ULONG_MAX)
1232 rlim.rlim_max = LONG_MAX;
1234 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1238 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1240 struct l_rlimit rlim;
1241 struct proc *p = td->td_proc;
1242 struct rlimit bsd_rlim;
1246 if (ldebug(getrlimit))
1247 printf(ARGS(getrlimit, "%d, %p"),
1248 args->resource, (void *)args->rlim);
1251 if (args->resource >= LINUX_RLIM_NLIMITS)
1254 which = linux_to_bsd_resource[args->resource];
1259 lim_rlimit(p, which, &bsd_rlim);
1262 rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1263 rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1264 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1266 #endif /*!__alpha__*/
1269 linux_sched_setscheduler(struct thread *td,
1270 struct linux_sched_setscheduler_args *args)
1272 struct sched_setscheduler_args bsd;
1275 if (ldebug(sched_setscheduler))
1276 printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1277 args->pid, args->policy, (const void *)args->param);
1280 switch (args->policy) {
1281 case LINUX_SCHED_OTHER:
1282 bsd.policy = SCHED_OTHER;
1284 case LINUX_SCHED_FIFO:
1285 bsd.policy = SCHED_FIFO;
1287 case LINUX_SCHED_RR:
1288 bsd.policy = SCHED_RR;
1294 bsd.pid = args->pid;
1295 bsd.param = (struct sched_param *)args->param;
1296 return sched_setscheduler(td, &bsd);
1300 linux_sched_getscheduler(struct thread *td,
1301 struct linux_sched_getscheduler_args *args)
1303 struct sched_getscheduler_args bsd;
1307 if (ldebug(sched_getscheduler))
1308 printf(ARGS(sched_getscheduler, "%d"), args->pid);
1311 bsd.pid = args->pid;
1312 error = sched_getscheduler(td, &bsd);
1314 switch (td->td_retval[0]) {
1316 td->td_retval[0] = LINUX_SCHED_OTHER;
1319 td->td_retval[0] = LINUX_SCHED_FIFO;
1322 td->td_retval[0] = LINUX_SCHED_RR;
1330 linux_sched_get_priority_max(struct thread *td,
1331 struct linux_sched_get_priority_max_args *args)
1333 struct sched_get_priority_max_args bsd;
1336 if (ldebug(sched_get_priority_max))
1337 printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1340 switch (args->policy) {
1341 case LINUX_SCHED_OTHER:
1342 bsd.policy = SCHED_OTHER;
1344 case LINUX_SCHED_FIFO:
1345 bsd.policy = SCHED_FIFO;
1347 case LINUX_SCHED_RR:
1348 bsd.policy = SCHED_RR;
1353 return sched_get_priority_max(td, &bsd);
1357 linux_sched_get_priority_min(struct thread *td,
1358 struct linux_sched_get_priority_min_args *args)
1360 struct sched_get_priority_min_args bsd;
1363 if (ldebug(sched_get_priority_min))
1364 printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1367 switch (args->policy) {
1368 case LINUX_SCHED_OTHER:
1369 bsd.policy = SCHED_OTHER;
1371 case LINUX_SCHED_FIFO:
1372 bsd.policy = SCHED_FIFO;
1374 case LINUX_SCHED_RR:
1375 bsd.policy = SCHED_RR;
1380 return sched_get_priority_min(td, &bsd);
1383 #define REBOOT_CAD_ON 0x89abcdef
1384 #define REBOOT_CAD_OFF 0
1385 #define REBOOT_HALT 0xcdef0123
1386 #define REBOOT_RESTART 0x01234567
1387 #define REBOOT_RESTART2 0xA1B2C3D4
1388 #define REBOOT_POWEROFF 0x4321FEDC
1389 #define REBOOT_MAGIC1 0xfee1dead
1390 #define REBOOT_MAGIC2 0x28121969
1391 #define REBOOT_MAGIC2A 0x05121996
1392 #define REBOOT_MAGIC2B 0x16041998
1395 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1397 struct reboot_args bsd_args;
1401 printf(ARGS(reboot, "0x%x"), args->cmd);
1404 if (args->magic1 != REBOOT_MAGIC1)
1407 switch (args->magic2) {
1409 case REBOOT_MAGIC2A:
1410 case REBOOT_MAGIC2B:
1416 switch (args->cmd) {
1418 case REBOOT_CAD_OFF:
1421 bsd_args.opt = RB_HALT;
1423 case REBOOT_RESTART:
1424 case REBOOT_RESTART2:
1427 case REBOOT_POWEROFF:
1428 bsd_args.opt = RB_POWEROFF;
1433 return reboot(td, &bsd_args);
1439 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1440 * td->td_retval[1] when COMPAT_43 is defined. This
1441 * globbers registers that are assumed to be preserved. The following
1442 * lightweight syscalls fixes this. See also linux_getgid16() and
1443 * linux_getuid16() in linux_uid16.c.
1445 * linux_getpid() - MP SAFE
1446 * linux_getgid() - MP SAFE
1447 * linux_getuid() - MP SAFE
1451 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1454 td->td_retval[0] = td->td_proc->p_pid;
1459 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1462 td->td_retval[0] = td->td_ucred->cr_rgid;
1467 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1470 td->td_retval[0] = td->td_ucred->cr_ruid;
1474 #endif /*!__alpha__*/
1477 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1479 struct getsid_args bsd;
1480 bsd.pid = args->pid;
1481 return getsid(td, &bsd);
1485 linux_nosys(struct thread *td, struct nosys_args *ignore)
1492 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1494 struct getpriority_args bsd_args;
1497 bsd_args.which = args->which;
1498 bsd_args.who = args->who;
1499 error = getpriority(td, &bsd_args);
1500 td->td_retval[0] = 20 - td->td_retval[0];
1505 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1510 if (ldebug(sethostname))
1511 printf(ARGS(sethostname, "*, %i"), args->len);
1515 name[1] = KERN_HOSTNAME;
1516 return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,