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.11 2003/07/30 00:19:13 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>
41 #include <sys/mount.h>
44 #include <sys/namei.h>
45 #include <sys/blist.h>
46 #include <sys/reboot.h>
47 #include <sys/resourcevar.h>
48 #include <sys/signalvar.h>
50 #include <sys/sysctl.h>
51 #include <sys/sysproto.h>
53 #include <sys/unistd.h>
54 #include <sys/vmmeter.h>
55 #include <sys/vnode.h>
60 #include <vm/vm_kern.h>
61 #include <vm/vm_map.h>
62 #include <vm/vm_extern.h>
63 #include <vm/vm_object.h>
64 #include <vm/vm_zone.h>
65 #include <vm/swap_pager.h>
67 #include <machine/frame.h>
68 #include <machine/limits.h>
69 #include <machine/psl.h>
70 #include <machine/sysarch.h>
72 #include <machine/segments.h>
75 #include <posix4/sched.h>
77 #include <machine/../linux/linux.h>
78 #include <machine/../linux/linux_proto.h>
79 #include <compat/linux/linux_mib.h>
80 #include <compat/linux/linux_util.h>
83 #define BSD_TO_LINUX_SIGNAL(sig) (sig)
85 #define BSD_TO_LINUX_SIGNAL(sig) \
86 (((sig) <= LINUX_SIGTBLSZ) ? bsd_to_linux_signal[_SIG_IDX(sig)] : sig)
90 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
91 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
92 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
98 l_long uptime; /* Seconds since boot */
99 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */
100 l_ulong totalram; /* Total usable main memory size */
101 l_ulong freeram; /* Available memory size */
102 l_ulong sharedram; /* Amount of shared memory */
103 l_ulong bufferram; /* Memory used by buffers */
104 l_ulong totalswap; /* Total swap space size */
105 l_ulong freeswap; /* swap space still available */
106 l_ushort procs; /* Number of current processes */
107 char _f[22]; /* Pads structure to 64 bytes */
111 linux_sysinfo(struct linux_sysinfo_args *args)
113 struct l_sysinfo sysinfo;
118 /* Uptime is copied out of print_uptime() in kern_shutdown.c */
121 if (ts.tv_sec >= 86400) {
125 if (i || ts.tv_sec >= 3600) {
129 if (i || ts.tv_sec >= 60) {
133 sysinfo.uptime=ts.tv_sec;
135 /* Use the information from the mib to get our load averages */
136 for (i = 0; i < 3; i++)
137 sysinfo.loads[i] = averunnable.ldavg[i];
139 sysinfo.totalram = physmem * PAGE_SIZE;
140 sysinfo.freeram = sysinfo.totalram - vmstats.v_wire_count * PAGE_SIZE;
142 sysinfo.sharedram = 0;
143 for (object = TAILQ_FIRST(&vm_object_list); object != NULL;
144 object = TAILQ_NEXT(object, object_list))
145 if (object->shadow_count > 1)
146 sysinfo.sharedram += object->resident_page_count;
148 sysinfo.sharedram *= PAGE_SIZE;
149 sysinfo.bufferram = 0;
151 if (swapblist == NULL) {
152 sysinfo.totalswap= 0;
153 sysinfo.freeswap = 0;
155 sysinfo.totalswap = swapblist->bl_blocks * 1024;
156 sysinfo.freeswap = swapblist->bl_root->u.bmu_avail * PAGE_SIZE;
159 sysinfo.procs = 20; /* Hack */
161 return copyout(&sysinfo, (caddr_t)args->info, sizeof(sysinfo));
163 #endif /*!__alpha__*/
167 linux_alarm(struct linux_alarm_args *args)
169 struct thread *td = curthread;
170 struct proc *p = td->td_proc;
171 struct itimerval it, old_it;
179 printf(ARGS(alarm, "%u"), args->secs);
182 if (args->secs > 100000000)
185 it.it_value.tv_sec = (long)args->secs;
186 it.it_value.tv_usec = 0;
187 it.it_interval.tv_sec = 0;
188 it.it_interval.tv_usec = 0;
190 old_it = p->p_realtimer;
192 if (timevalisset(&old_it.it_value))
193 untimeout(realitexpire, (caddr_t)p, p->p_ithandle);
194 if (it.it_value.tv_sec != 0) {
195 p->p_ithandle = timeout(realitexpire, (caddr_t)p,
196 tvtohz(&it.it_value));
197 timevaladd(&it.it_value, &tv);
201 if (timevalcmp(&old_it.it_value, &tv, >)) {
202 timevalsub(&old_it.it_value, &tv);
203 if (old_it.it_value.tv_usec != 0)
204 old_it.it_value.tv_sec++;
205 args->sysmsg_result = old_it.it_value.tv_sec;
209 #endif /*!__alpha__*/
212 linux_brk(struct linux_brk_args *args)
214 struct thread *td = curthread;
215 struct proc *p = td->td_proc;
217 vm_offset_t new, old;
218 struct obreak_args bsd_args;
224 printf(ARGS(brk, "%p"), (void *)args->dsend);
226 old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
227 new = (vm_offset_t)args->dsend;
228 bsd_args.sysmsg_result = 0;
229 bsd_args.nsize = (char *) new;
230 bsd_args.sysmsg_result = 0;
231 if (((caddr_t)new > vm->vm_daddr) && !obreak(&bsd_args))
232 args->sysmsg_result = (long)new;
234 args->sysmsg_result = (long)old;
240 linux_uselib(struct linux_uselib_args *args)
242 struct thread *td = curthread;
249 unsigned long file_offset;
251 unsigned long bss_size;
256 KKASSERT(td->td_proc);
259 sg = stackgap_init();
260 CHECKALTEXIST(&sg, args->library);
264 printf(ARGS(uselib, "%s"), args->library);
271 NDINIT(&ni, LOOKUP, FOLLOW|LOCKLEAF, UIO_USERSPACE, args->library, td);
278 * XXX - This looks like a bogus check. A LOCKLEAF namei should not
279 * succeed without returning a vnode.
282 error = ENOEXEC; /* ?? */
285 NDFREE(&ni, NDF_ONLY_PNBUF);
288 * From here on down, we have a locked vnode that must be unlocked.
293 if (vp->v_writecount) {
299 error = VOP_GETATTR(vp, &attr, td);
303 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
304 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
310 if (attr.va_size == 0) {
315 /* Can we access it? */
316 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, td);
320 error = VOP_OPEN(vp, FREAD, p->p_ucred, td);
325 * Lock no longer needed
327 VOP_UNLOCK(vp, 0, td);
330 /* Pull in executable header into kernel_map */
331 error = vm_mmap(kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE,
332 VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp, 0);
336 /* Is it a Linux binary ? */
337 if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
343 * While we are here, we should REALLY do some more checks
346 /* Set file/virtual offset based on a.out variant. */
347 switch ((int)(a_out->a_magic & 0xffff)) {
348 case 0413: /* ZMAGIC */
351 case 0314: /* QMAGIC */
359 bss_size = round_page(a_out->a_bss);
361 /* Check various fields in header for validity/bounds. */
362 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
367 /* text + data can't exceed file size */
368 if (a_out->a_data + a_out->a_text > attr.va_size) {
374 * text/data/bss must not exceed limits
375 * XXX - this is not complete. it should check current usage PLUS
376 * the resources needed by this library.
378 if (a_out->a_text > maxtsiz ||
379 a_out->a_data + bss_size > p->p_rlimit[RLIMIT_DATA].rlim_cur) {
384 /* prevent more writers */
388 * Check if file_offset page aligned. Currently we cannot handle
389 * misalinged file offsets, and so we read in the entire image
392 if (file_offset & PAGE_MASK) {
394 printf("uselib: Non page aligned binary %lu\n", file_offset);
396 /* Map text+data read/write/execute */
398 /* a_entry is the load address and is page aligned */
399 vmaddr = trunc_page(a_out->a_entry);
401 /* get anon user mapping, read+write+execute */
402 error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0,
403 &vmaddr, a_out->a_text + a_out->a_data, FALSE, VM_PROT_ALL,
408 /* map file into kernel_map */
409 error = vm_mmap(kernel_map, &buffer,
410 round_page(a_out->a_text + a_out->a_data + file_offset),
411 VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp,
412 trunc_page(file_offset));
416 /* copy from kernel VM space to user space */
417 error = copyout((caddr_t)(uintptr_t)(buffer + file_offset),
418 (caddr_t)vmaddr, a_out->a_text + a_out->a_data);
420 /* release temporary kernel space */
421 vm_map_remove(kernel_map, buffer, buffer +
422 round_page(a_out->a_text + a_out->a_data + file_offset));
428 printf("uselib: Page aligned binary %lu\n", file_offset);
431 * for QMAGIC, a_entry is 20 bytes beyond the load address
432 * to skip the executable header
434 vmaddr = trunc_page(a_out->a_entry);
437 * Map it all into the process's space as a single
438 * copy-on-write "data" segment.
440 error = vm_mmap(&p->p_vmspace->vm_map, &vmaddr,
441 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
442 MAP_PRIVATE | MAP_FIXED, (caddr_t)vp, file_offset);
447 printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long*)vmaddr)[0],
451 /* Calculate BSS start address */
452 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
455 /* allocate some 'anon' space */
456 error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0,
457 &vmaddr, bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
463 /* Unlock vnode if needed */
465 VOP_UNLOCK(vp, 0, td);
467 /* Release the kernel mapping. */
469 vm_map_remove(kernel_map, (vm_offset_t)a_out,
470 (vm_offset_t)a_out + PAGE_SIZE);
476 linux_select(struct linux_select_args *args)
478 struct select_args bsa;
479 struct timeval tv0, tv1, utv, *tvp;
485 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
486 (void *)args->readfds, (void *)args->writefds,
487 (void *)args->exceptfds, (void *)args->timeout);
491 bsa.sysmsg_result = 0;
493 bsa.in = args->readfds;
494 bsa.ou = args->writefds;
495 bsa.ex = args->exceptfds;
496 bsa.tv = (struct timeval *)args->timeout;
499 * Store current time for computation of the amount of
503 if ((error = copyin((caddr_t)args->timeout, &utv,
508 printf(LMSG("incoming timeout (%ld/%ld)"),
509 utv.tv_sec, utv.tv_usec);
512 if (itimerfix(&utv)) {
514 * The timeval was invalid. Convert it to something
515 * valid that will act as it does under Linux.
517 sg = stackgap_init();
518 tvp = stackgap_alloc(&sg, sizeof(utv));
519 utv.tv_sec += utv.tv_usec / 1000000;
520 utv.tv_usec %= 1000000;
521 if (utv.tv_usec < 0) {
523 utv.tv_usec += 1000000;
527 if ((error = copyout(&utv, tvp, sizeof(utv))))
534 error = select(&bsa);
535 args->sysmsg_result = bsa.sysmsg_result;
538 printf(LMSG("real select returns %d"), error);
542 * See fs/select.c in the Linux kernel. Without this,
543 * Maelstrom doesn't work.
545 if (error == ERESTART)
551 if (args->sysmsg_result) {
553 * Compute how much time was left of the timeout,
554 * by subtracting the current time and the time
555 * before we started the call, and subtracting
556 * that result from the user-supplied value.
559 timevalsub(&tv1, &tv0);
560 timevalsub(&utv, &tv1);
567 printf(LMSG("outgoing timeout (%ld/%ld)"),
568 utv.tv_sec, utv.tv_usec);
570 if ((error = copyout(&utv, (caddr_t)args->timeout,
578 printf(LMSG("select_out -> %d"), error);
584 linux_mremap(struct linux_mremap_args *args)
586 struct munmap_args bsd_args;
591 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
593 (unsigned long)args->old_len,
594 (unsigned long)args->new_len,
595 (unsigned long)args->flags);
597 args->new_len = round_page(args->new_len);
598 args->old_len = round_page(args->old_len);
600 if (args->new_len > args->old_len) {
601 args->sysmsg_result = 0;
605 if (args->new_len < args->old_len) {
606 bsd_args.sysmsg_result = 0;
607 bsd_args.addr = (caddr_t)(args->addr + args->new_len);
608 bsd_args.len = args->old_len - args->new_len;
609 error = munmap(&bsd_args);
612 args->sysmsg_resultp = error ? NULL : (void *)args->addr;
616 #define LINUX_MS_ASYNC 0x0001
617 #define LINUX_MS_INVALIDATE 0x0002
618 #define LINUX_MS_SYNC 0x0004
621 linux_msync(struct linux_msync_args *args)
623 struct msync_args bsd_args;
626 bsd_args.addr = (caddr_t)args->addr;
627 bsd_args.len = args->len;
628 bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
629 bsd_args.sysmsg_result = 0;
631 error = msync(&bsd_args);
632 args->sysmsg_result = bsd_args.sysmsg_result;
638 linux_time(struct linux_time_args *args)
646 printf(ARGS(time, "*"));
651 if (args->tm && (error = copyout(&tm, (caddr_t)args->tm, sizeof(tm))))
653 args->sysmsg_lresult = tm;
656 #endif /*!__alpha__*/
658 struct l_times_argv {
666 #define CLK_TCK 1024 /* Linux uses 1024 on alpha */
668 #define CLK_TCK 100 /* Linux uses 100 */
671 #define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
674 linux_times(struct linux_times_args *args)
676 struct thread *td = curthread;
677 struct proc *p = td->td_proc;
679 struct l_times_argv tms;
686 printf(ARGS(times, "*"));
689 calcru(p, &ru.ru_utime, &ru.ru_stime, NULL);
691 tms.tms_utime = CONVTCK(ru.ru_utime);
692 tms.tms_stime = CONVTCK(ru.ru_stime);
694 tms.tms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
695 tms.tms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
697 if ((error = copyout(&tms, (caddr_t)args->buf, sizeof(tms))))
701 args->sysmsg_result = (int)CONVTCK(tv);
706 linux_newuname(struct linux_newuname_args *args)
708 struct thread *td = curthread;
709 struct l_new_utsname utsname;
710 char *osrelease, *osname;
713 if (ldebug(newuname))
714 printf(ARGS(newuname, "*"));
717 osname = linux_get_osname(td);
718 osrelease = linux_get_osrelease(td);
720 bzero(&utsname, sizeof(utsname));
721 strncpy(utsname.sysname, osname, LINUX_MAX_UTSNAME-1);
722 strncpy(utsname.nodename, hostname, LINUX_MAX_UTSNAME-1);
723 strncpy(utsname.release, osrelease, LINUX_MAX_UTSNAME-1);
724 strncpy(utsname.version, version, LINUX_MAX_UTSNAME-1);
725 strncpy(utsname.machine, machine, LINUX_MAX_UTSNAME-1);
726 strncpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME-1);
728 return (copyout(&utsname, (caddr_t)args->buf, sizeof(utsname)));
731 #if defined(__i386__)
738 linux_utime(struct linux_utime_args *args)
740 struct utimes_args /* {
742 struct timeval *tptr;
744 struct timeval tv[2], *tvp;
745 struct l_utimbuf lut;
749 sg = stackgap_init();
750 CHECKALTEXIST(&sg, args->fname);
754 printf(ARGS(utime, "%s, *"), args->fname);
758 if ((error = copyin((caddr_t)args->times, &lut, sizeof lut)))
760 tv[0].tv_sec = lut.l_actime;
762 tv[1].tv_sec = lut.l_modtime;
764 /* so that utimes can copyin */
765 tvp = (struct timeval *)stackgap_alloc(&sg, sizeof(tv));
767 return (ENAMETOOLONG);
768 if ((error = copyout(tv, tvp, sizeof(tv))))
770 bsdutimes.tptr = tvp;
772 bsdutimes.tptr = NULL;
774 bsdutimes.path = args->fname;
775 bsdutimes.sysmsg_result = 0;
776 error = utimes(&bsdutimes);
777 args->sysmsg_result = bsdutimes.sysmsg_result;
780 #endif /* __i386__ */
782 #define __WCLONE 0x80000000
786 linux_waitpid(struct linux_waitpid_args *args)
788 struct wait_args bsd_args;
793 printf(ARGS(waitpid, "%d, %p, %d"),
794 args->pid, (void *)args->status, args->options);
797 bsd_args.sysmsg_result = 0;
798 bsd_args.pid = args->pid;
799 bsd_args.status = args->status;
800 bsd_args.options = (args->options & (WNOHANG | WUNTRACED));
801 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
802 if (args->options & __WCLONE)
803 bsd_args.options |= WLINUXCLONE;
804 bsd_args.rusage = NULL;
806 if ((error = wait4(&bsd_args)) != 0)
808 args->sysmsg_result = bsd_args.sysmsg_result;
811 if ((error = copyin((caddr_t)args->status, &tmpstat,
815 if (WIFSIGNALED(tmpstat))
816 tmpstat = (tmpstat & 0xffffff80) |
817 BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
818 else if (WIFSTOPPED(tmpstat))
819 tmpstat = (tmpstat & 0xffff00ff) |
820 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
821 return copyout(&tmpstat, (caddr_t)args->status, sizeof(int));
826 #endif /*!__alpha__*/
829 linux_wait4(struct linux_wait4_args *args)
831 struct thread *td = curthread;
832 struct proc *p = td->td_proc;
833 struct wait_args bsd_args;
840 printf(ARGS(wait4, "%d, %p, %d, %p"),
841 args->pid, (void *)args->status, args->options,
842 (void *)args->rusage);
845 bsd_args.sysmsg_result = 0;
846 bsd_args.pid = args->pid;
847 bsd_args.status = args->status;
848 bsd_args.options = (args->options & (WNOHANG | WUNTRACED));
849 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
850 if (args->options & __WCLONE)
851 bsd_args.options |= WLINUXCLONE;
852 bsd_args.rusage = (struct rusage *)args->rusage;
854 if ((error = wait4(&bsd_args)) != 0)
856 args->sysmsg_result = bsd_args.sysmsg_result;
858 SIGDELSET(p->p_siglist, SIGCHLD);
861 if ((error = copyin((caddr_t)args->status, &tmpstat,
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, (caddr_t)args->status, sizeof(int));
878 linux_mknod(struct linux_mknod_args *args)
882 struct mknod_args bsd_mknod;
883 struct mkfifo_args bsd_mkfifo;
885 sg = stackgap_init();
887 CHECKALTCREAT(&sg, args->path);
891 printf(ARGS(mknod, "%s, %d, %d"),
892 args->path, args->mode, args->dev);
895 if (args->mode & S_IFIFO) {
896 bsd_mkfifo.path = args->path;
897 bsd_mkfifo.mode = args->mode;
898 bsd_mkfifo.sysmsg_result = 0;
899 error = mkfifo(&bsd_mkfifo);
900 args->sysmsg_result = bsd_mkfifo.sysmsg_result;
902 bsd_mknod.path = args->path;
903 bsd_mknod.mode = args->mode;
904 bsd_mknod.dev = args->dev;
905 bsd_mknod.sysmsg_result = 0;
906 error = mknod(&bsd_mknod);
907 args->sysmsg_result = bsd_mknod.sysmsg_result;
913 * UGH! This is just about the dumbest idea I've ever heard!!
916 linux_personality(struct linux_personality_args *args)
919 if (ldebug(personality))
920 printf(ARGS(personality, "%d"), args->per);
927 /* Yes Jim, it's still a Linux... */
928 args->sysmsg_result = 0;
933 * Wrappers for get/setitimer for debugging..
936 linux_setitimer(struct linux_setitimer_args *args)
938 struct setitimer_args bsa;
939 struct itimerval foo;
943 if (ldebug(setitimer))
944 printf(ARGS(setitimer, "%p, %p"),
945 (void *)args->itv, (void *)args->oitv);
947 bsa.which = args->which;
948 bsa.itv = (struct itimerval *)args->itv;
949 bsa.oitv = (struct itimerval *)args->oitv;
950 bsa.sysmsg_result = 0;
952 if ((error = copyin((caddr_t)args->itv, &foo, sizeof(foo))))
955 if (ldebug(setitimer)) {
956 printf("setitimer: value: sec: %ld, usec: %ld\n",
957 foo.it_value.tv_sec, foo.it_value.tv_usec);
958 printf("setitimer: interval: sec: %ld, usec: %ld\n",
959 foo.it_interval.tv_sec, foo.it_interval.tv_usec);
963 error = setitimer(&bsa);
964 args->sysmsg_result = bsa.sysmsg_result;
969 linux_getitimer(struct linux_getitimer_args *args)
971 struct getitimer_args bsa;
974 if (ldebug(getitimer))
975 printf(ARGS(getitimer, "%p"), (void *)args->itv);
977 bsa.which = args->which;
978 bsa.itv = (struct itimerval *)args->itv;
979 bsa.sysmsg_result = 0;
980 error = getitimer(&bsa);
981 args->sysmsg_result = bsa.sysmsg_result;
987 linux_nice(struct linux_nice_args *args)
989 struct setpriority_args bsd_args;
992 bsd_args.which = PRIO_PROCESS;
993 bsd_args.who = 0; /* current process */
994 bsd_args.prio = args->inc;
995 bsd_args.sysmsg_result = 0;
996 error = setpriority(&bsd_args);
997 args->sysmsg_result = bsd_args.sysmsg_result;
1000 #endif /*!__alpha__*/
1003 linux_setgroups(struct linux_setgroups_args *args)
1005 struct thread *td = curthread;
1006 struct proc *p = td->td_proc;
1007 struct ucred *newcred, *oldcred;
1008 l_gid_t linux_gidset[NGROUPS];
1014 ngrp = args->gidsetsize;
1015 oldcred = p->p_ucred;
1018 * cr_groups[0] holds egid. Setting the whole set from
1019 * the supplied set will cause egid to be changed too.
1020 * Keep cr_groups[0] unchanged to prevent that.
1023 if ((error = suser_cred(oldcred, PRISON_ROOT)) != 0)
1026 if (ngrp >= NGROUPS)
1029 newcred = crdup(oldcred);
1031 error = copyin((caddr_t)args->grouplist, linux_gidset,
1032 ngrp * sizeof(l_gid_t));
1036 newcred->cr_ngroups = ngrp + 1;
1038 bsd_gidset = newcred->cr_groups;
1041 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1045 newcred->cr_ngroups = 1;
1049 p->p_ucred = newcred;
1055 linux_getgroups(struct linux_getgroups_args *args)
1057 struct thread *td = curthread;
1058 struct proc *p = td->td_proc;
1060 l_gid_t linux_gidset[NGROUPS];
1062 int bsd_gidsetsz, ngrp, error;
1067 bsd_gidset = cred->cr_groups;
1068 bsd_gidsetsz = cred->cr_ngroups - 1;
1071 * cr_groups[0] holds egid. Returning the whole set
1072 * here will cause a duplicate. Exclude cr_groups[0]
1076 if ((ngrp = args->gidsetsize) == 0) {
1077 args->sysmsg_result = bsd_gidsetsz;
1081 if (ngrp < bsd_gidsetsz)
1085 while (ngrp < bsd_gidsetsz) {
1086 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1090 if ((error = copyout(linux_gidset, (caddr_t)args->grouplist,
1091 ngrp * sizeof(l_gid_t))))
1094 args->sysmsg_result = ngrp;
1100 linux_setrlimit(struct linux_setrlimit_args *args)
1102 struct __setrlimit_args bsd;
1103 struct l_rlimit rlim;
1105 caddr_t sg = stackgap_init();
1108 if (ldebug(setrlimit))
1109 printf(ARGS(setrlimit, "%d, %p"),
1110 args->resource, (void *)args->rlim);
1113 if (args->resource >= LINUX_RLIM_NLIMITS)
1116 bsd.which = linux_to_bsd_resource[args->resource];
1117 if (bsd.which == -1)
1120 error = copyin((caddr_t)args->rlim, &rlim, sizeof(rlim));
1124 bsd.rlp = stackgap_alloc(&sg, sizeof(struct rlimit));
1125 bsd.rlp->rlim_cur = (rlim_t)rlim.rlim_cur;
1126 bsd.rlp->rlim_max = (rlim_t)rlim.rlim_max;
1127 bsd.sysmsg_result = 0;
1128 error = setrlimit(&bsd);
1129 args->sysmsg_result = bsd.sysmsg_result;
1134 linux_old_getrlimit(struct linux_old_getrlimit_args *args)
1136 struct __getrlimit_args bsd;
1137 struct l_rlimit rlim;
1139 caddr_t sg = stackgap_init();
1142 if (ldebug(old_getrlimit))
1143 printf(ARGS(old_getrlimit, "%d, %p"),
1144 args->resource, (void *)args->rlim);
1147 if (args->resource >= LINUX_RLIM_NLIMITS)
1150 bsd.which = linux_to_bsd_resource[args->resource];
1151 if (bsd.which == -1)
1154 bsd.rlp = stackgap_alloc(&sg, sizeof(struct rlimit));
1155 bsd.sysmsg_result = 0;
1156 error = getrlimit(&bsd);
1159 args->sysmsg_result = bsd.sysmsg_result;
1160 rlim.rlim_cur = (unsigned long)bsd.rlp->rlim_cur;
1161 if (rlim.rlim_cur == ULONG_MAX)
1162 rlim.rlim_cur = LONG_MAX;
1163 rlim.rlim_max = (unsigned long)bsd.rlp->rlim_max;
1164 if (rlim.rlim_max == ULONG_MAX)
1165 rlim.rlim_max = LONG_MAX;
1166 return (copyout(&rlim, (caddr_t)args->rlim, sizeof(rlim)));
1170 linux_getrlimit(struct linux_getrlimit_args *args)
1172 struct __getrlimit_args bsd;
1173 struct l_rlimit rlim;
1175 caddr_t sg = stackgap_init();
1178 if (ldebug(getrlimit))
1179 printf(ARGS(getrlimit, "%d, %p"),
1180 args->resource, (void *)args->rlim);
1183 if (args->resource >= LINUX_RLIM_NLIMITS)
1186 bsd.which = linux_to_bsd_resource[args->resource];
1187 if (bsd.which == -1)
1190 bsd.rlp = stackgap_alloc(&sg, sizeof(struct rlimit));
1191 bsd.sysmsg_result = 0;
1192 error = getrlimit(&bsd);
1195 args->sysmsg_result = bsd.sysmsg_result;
1197 rlim.rlim_cur = (l_ulong)bsd.rlp->rlim_cur;
1198 rlim.rlim_max = (l_ulong)bsd.rlp->rlim_max;
1199 return (copyout(&rlim, (caddr_t)args->rlim, sizeof(rlim)));
1201 #endif /*!__alpha__*/
1204 linux_sched_setscheduler(struct linux_sched_setscheduler_args *args)
1206 struct sched_setscheduler_args bsd;
1210 if (ldebug(sched_setscheduler))
1211 printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1212 args->pid, args->policy, (const void *)args->param);
1215 switch (args->policy) {
1216 case LINUX_SCHED_OTHER:
1217 bsd.policy = SCHED_OTHER;
1219 case LINUX_SCHED_FIFO:
1220 bsd.policy = SCHED_FIFO;
1222 case LINUX_SCHED_RR:
1223 bsd.policy = SCHED_RR;
1229 bsd.pid = args->pid;
1230 bsd.param = (struct sched_param *)args->param;
1231 bsd.sysmsg_result = 0;
1233 error = sched_setscheduler(&bsd);
1234 args->sysmsg_result = bsd.sysmsg_result;
1239 linux_sched_getscheduler(struct linux_sched_getscheduler_args *args)
1241 struct sched_getscheduler_args bsd;
1245 if (ldebug(sched_getscheduler))
1246 printf(ARGS(sched_getscheduler, "%d"), args->pid);
1249 bsd.sysmsg_result = 0;
1250 bsd.pid = args->pid;
1251 error = sched_getscheduler(&bsd);
1252 args->sysmsg_result = bsd.sysmsg_result;
1254 switch (args->sysmsg_result) {
1256 args->sysmsg_result = LINUX_SCHED_OTHER;
1259 args->sysmsg_result = LINUX_SCHED_FIFO;
1262 args->sysmsg_result = LINUX_SCHED_RR;
1269 linux_sched_get_priority_max(struct linux_sched_get_priority_max_args *args)
1271 struct sched_get_priority_max_args bsd;
1275 if (ldebug(sched_get_priority_max))
1276 printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1279 switch (args->policy) {
1280 case LINUX_SCHED_OTHER:
1281 bsd.policy = SCHED_OTHER;
1283 case LINUX_SCHED_FIFO:
1284 bsd.policy = SCHED_FIFO;
1286 case LINUX_SCHED_RR:
1287 bsd.policy = SCHED_RR;
1292 bsd.sysmsg_result = 0;
1294 error = sched_get_priority_max(&bsd);
1295 args->sysmsg_result = bsd.sysmsg_result;
1300 linux_sched_get_priority_min(struct linux_sched_get_priority_min_args *args)
1302 struct sched_get_priority_min_args bsd;
1306 if (ldebug(sched_get_priority_min))
1307 printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1310 switch (args->policy) {
1311 case LINUX_SCHED_OTHER:
1312 bsd.policy = SCHED_OTHER;
1314 case LINUX_SCHED_FIFO:
1315 bsd.policy = SCHED_FIFO;
1317 case LINUX_SCHED_RR:
1318 bsd.policy = SCHED_RR;
1323 bsd.sysmsg_result = 0;
1325 error = sched_get_priority_min(&bsd);
1326 args->sysmsg_result = bsd.sysmsg_result;
1330 #define REBOOT_CAD_ON 0x89abcdef
1331 #define REBOOT_CAD_OFF 0
1332 #define REBOOT_HALT 0xcdef0123
1335 linux_reboot(struct linux_reboot_args *args)
1337 struct reboot_args bsd_args;
1342 printf(ARGS(reboot, "0x%x"), args->cmd);
1344 if (args->cmd == REBOOT_CAD_ON || args->cmd == REBOOT_CAD_OFF)
1346 bsd_args.opt = (args->cmd == REBOOT_HALT) ? RB_HALT : 0;
1347 bsd_args.sysmsg_result = 0;
1349 error = reboot(&bsd_args);
1350 args->sysmsg_result = bsd_args.sysmsg_result;
1357 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1358 * p->p_retval[1] when COMPAT_43 or COMPAT_SUNOS is defined. This
1359 * globbers registers that are assumed to be preserved. The following
1360 * lightweight syscalls fixes this. See also linux_getgid16() and
1361 * linux_getuid16() in linux_uid16.c.
1363 * linux_getpid() - MP SAFE
1364 * linux_getgid() - MP SAFE
1365 * linux_getuid() - MP SAFE
1369 linux_getpid(struct linux_getpid_args *args)
1371 struct thread *td = curthread;
1372 struct proc *p = td->td_proc;
1376 args->sysmsg_result = p->p_pid;
1381 linux_getgid(struct linux_getgid_args *args)
1383 struct thread *td = curthread;
1384 struct proc *p = td->td_proc;
1388 args->sysmsg_result = p->p_ucred->cr_rgid;
1393 linux_getuid(struct linux_getuid_args *args)
1395 struct thread *td = curthread;
1396 struct proc *p = td->td_proc;
1400 args->sysmsg_result = p->p_ucred->cr_ruid;
1404 #endif /*!__alpha__*/
1407 linux_getsid(struct linux_getsid_args *args)
1409 struct getsid_args bsd;
1412 bsd.sysmsg_result = 0;
1413 bsd.pid = args->pid;
1414 error = getsid(&bsd);
1415 args->sysmsg_result = bsd.sysmsg_result;