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.6 2003/07/03 17:23:59 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 p->p_retval[0] = 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 /* {
226 printf(ARGS(brk, "%p"), (void *)args->dsend);
228 old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
229 new = (vm_offset_t)args->dsend;
230 tmp.nsize = (char *) new;
231 if (((caddr_t)new > vm->vm_daddr) && !obreak(&tmp))
232 p->p_retval[0] = (long)new;
234 p->p_retval[0] = (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 thread *td = curthread;
479 struct proc *p = td->td_proc;
480 struct select_args bsa;
481 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);
496 bsa.in = args->readfds;
497 bsa.ou = args->writefds;
498 bsa.ex = args->exceptfds;
499 bsa.tv = (struct timeval *)args->timeout;
502 * Store current time for computation of the amount of
506 if ((error = copyin((caddr_t)args->timeout, &utv,
511 printf(LMSG("incoming timeout (%ld/%ld)"),
512 utv.tv_sec, utv.tv_usec);
515 if (itimerfix(&utv)) {
517 * The timeval was invalid. Convert it to something
518 * valid that will act as it does under Linux.
520 sg = stackgap_init();
521 tvp = stackgap_alloc(&sg, sizeof(utv));
522 utv.tv_sec += utv.tv_usec / 1000000;
523 utv.tv_usec %= 1000000;
524 if (utv.tv_usec < 0) {
526 utv.tv_usec += 1000000;
530 if ((error = copyout(&utv, tvp, sizeof(utv))))
537 error = select(&bsa);
540 printf(LMSG("real select returns %d"), error);
544 * See fs/select.c in the Linux kernel. Without this,
545 * Maelstrom doesn't work.
547 if (error == ERESTART)
553 if (p->p_retval[0]) {
555 * Compute how much time was left of the timeout,
556 * by subtracting the current time and the time
557 * before we started the call, and subtracting
558 * that result from the user-supplied value.
561 timevalsub(&tv1, &tv0);
562 timevalsub(&utv, &tv1);
569 printf(LMSG("outgoing timeout (%ld/%ld)"),
570 utv.tv_sec, utv.tv_usec);
572 if ((error = copyout(&utv, (caddr_t)args->timeout,
580 printf(LMSG("select_out -> %d"), error);
586 linux_mremap(struct linux_mremap_args *args)
588 struct thread *td = curthread;
589 struct munmap_args /* {
594 struct proc *p = td->td_proc;
600 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
602 (unsigned long)args->old_len,
603 (unsigned long)args->new_len,
604 (unsigned long)args->flags);
606 args->new_len = round_page(args->new_len);
607 args->old_len = round_page(args->old_len);
609 if (args->new_len > args->old_len) {
614 if (args->new_len < args->old_len) {
615 bsd_args.addr = (caddr_t)(args->addr + args->new_len);
616 bsd_args.len = args->old_len - args->new_len;
617 error = munmap(&bsd_args);
620 p->p_retval[0] = error ? 0 : (u_long)args->addr;
624 #define LINUX_MS_ASYNC 0x0001
625 #define LINUX_MS_INVALIDATE 0x0002
626 #define LINUX_MS_SYNC 0x0004
629 linux_msync(struct linux_msync_args *args)
631 struct msync_args bsd_args;
633 bsd_args.addr = (caddr_t)args->addr;
634 bsd_args.len = args->len;
635 bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
637 return msync(&bsd_args);
642 linux_time(struct linux_time_args *args)
644 struct thread *td = curthread;
645 struct proc *p = td->td_proc;
654 printf(ARGS(time, "*"));
659 if (args->tm && (error = copyout(&tm, (caddr_t)args->tm, sizeof(tm))))
664 #endif /*!__alpha__*/
666 struct l_times_argv {
674 #define CLK_TCK 1024 /* Linux uses 1024 on alpha */
676 #define CLK_TCK 100 /* Linux uses 100 */
679 #define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
682 linux_times(struct linux_times_args *args)
684 struct thread *td = curthread;
685 struct proc *p = td->td_proc;
687 struct l_times_argv tms;
694 printf(ARGS(times, "*"));
697 calcru(p, &ru.ru_utime, &ru.ru_stime, NULL);
699 tms.tms_utime = CONVTCK(ru.ru_utime);
700 tms.tms_stime = CONVTCK(ru.ru_stime);
702 tms.tms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
703 tms.tms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
705 if ((error = copyout(&tms, (caddr_t)args->buf, sizeof(tms))))
709 p->p_retval[0] = (int)CONVTCK(tv);
714 linux_newuname(struct linux_newuname_args *args)
716 struct thread *td = curthread;
717 struct l_new_utsname utsname;
718 char *osrelease, *osname;
721 if (ldebug(newuname))
722 printf(ARGS(newuname, "*"));
725 osname = linux_get_osname(td);
726 osrelease = linux_get_osrelease(td);
728 bzero(&utsname, sizeof(utsname));
729 strncpy(utsname.sysname, osname, LINUX_MAX_UTSNAME-1);
730 strncpy(utsname.nodename, hostname, LINUX_MAX_UTSNAME-1);
731 strncpy(utsname.release, osrelease, LINUX_MAX_UTSNAME-1);
732 strncpy(utsname.version, version, LINUX_MAX_UTSNAME-1);
733 strncpy(utsname.machine, machine, LINUX_MAX_UTSNAME-1);
734 strncpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME-1);
736 return (copyout(&utsname, (caddr_t)args->buf, sizeof(utsname)));
739 #if defined(__i386__)
746 linux_utime(struct linux_utime_args *args)
748 struct utimes_args /* {
750 struct timeval *tptr;
752 struct timeval tv[2], *tvp;
753 struct l_utimbuf lut;
757 sg = stackgap_init();
758 CHECKALTEXIST(&sg, args->fname);
762 printf(ARGS(utime, "%s, *"), args->fname);
766 if ((error = copyin((caddr_t)args->times, &lut, sizeof lut)))
768 tv[0].tv_sec = lut.l_actime;
770 tv[1].tv_sec = lut.l_modtime;
772 /* so that utimes can copyin */
773 tvp = (struct timeval *)stackgap_alloc(&sg, sizeof(tv));
775 return (ENAMETOOLONG);
776 if ((error = copyout(tv, tvp, sizeof(tv))))
778 bsdutimes.tptr = tvp;
780 bsdutimes.tptr = NULL;
782 bsdutimes.path = args->fname;
783 return utimes(&bsdutimes);
785 #endif /* __i386__ */
787 #define __WCLONE 0x80000000
791 linux_waitpid(struct linux_waitpid_args *args)
793 struct wait_args /* {
797 struct rusage *rusage;
803 printf(ARGS(waitpid, "%d, %p, %d"),
804 args->pid, (void *)args->status, args->options);
808 tmp.status = args->status;
809 tmp.options = (args->options & (WNOHANG | WUNTRACED));
810 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
811 if (args->options & __WCLONE)
812 tmp.options |= WLINUXCLONE;
815 if ((error = wait4(&tmp)) != 0)
819 if ((error = copyin((caddr_t)args->status, &tmpstat,
823 if (WIFSIGNALED(tmpstat))
824 tmpstat = (tmpstat & 0xffffff80) |
825 BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
826 else if (WIFSTOPPED(tmpstat))
827 tmpstat = (tmpstat & 0xffff00ff) |
828 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
829 return copyout(&tmpstat, (caddr_t)args->status, sizeof(int));
834 #endif /*!__alpha__*/
837 linux_wait4(struct linux_wait4_args *args)
839 struct thread *td = curthread;
840 struct proc *p = td->td_proc;
841 struct wait_args /* {
845 struct rusage *rusage;
853 printf(ARGS(wait4, "%d, %p, %d, %p"),
854 args->pid, (void *)args->status, args->options,
855 (void *)args->rusage);
859 tmp.status = args->status;
860 tmp.options = (args->options & (WNOHANG | WUNTRACED));
861 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
862 if (args->options & __WCLONE)
863 tmp.options |= WLINUXCLONE;
864 tmp.rusage = (struct rusage *)args->rusage;
866 if ((error = wait4(&tmp)) != 0)
869 SIGDELSET(p->p_siglist, SIGCHLD);
872 if ((error = copyin((caddr_t)args->status, &tmpstat,
876 if (WIFSIGNALED(tmpstat))
877 tmpstat = (tmpstat & 0xffffff80) |
878 BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
879 else if (WIFSTOPPED(tmpstat))
880 tmpstat = (tmpstat & 0xffff00ff) |
881 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
882 return copyout(&tmpstat, (caddr_t)args->status, sizeof(int));
889 linux_mknod(struct linux_mknod_args *args)
892 struct mknod_args bsd_mknod;
893 struct mkfifo_args bsd_mkfifo;
895 sg = stackgap_init();
897 CHECKALTCREAT(&sg, args->path);
901 printf(ARGS(mknod, "%s, %d, %d"),
902 args->path, args->mode, args->dev);
905 if (args->mode & S_IFIFO) {
906 bsd_mkfifo.path = args->path;
907 bsd_mkfifo.mode = args->mode;
908 return mkfifo(&bsd_mkfifo);
910 bsd_mknod.path = args->path;
911 bsd_mknod.mode = args->mode;
912 bsd_mknod.dev = args->dev;
913 return mknod(&bsd_mknod);
918 * UGH! This is just about the dumbest idea I've ever heard!!
921 linux_personality(struct linux_personality_args *args)
923 struct thread *td = curthread;
924 struct proc *p = td->td_proc;
928 if (ldebug(personality))
929 printf(ARGS(personality, "%d"), args->per);
936 /* Yes Jim, it's still a Linux... */
942 * Wrappers for get/setitimer for debugging..
945 linux_setitimer(struct linux_setitimer_args *args)
947 struct setitimer_args bsa;
948 struct itimerval foo;
952 if (ldebug(setitimer))
953 printf(ARGS(setitimer, "%p, %p"),
954 (void *)args->itv, (void *)args->oitv);
956 bsa.which = args->which;
957 bsa.itv = (struct itimerval *)args->itv;
958 bsa.oitv = (struct itimerval *)args->oitv;
960 if ((error = copyin((caddr_t)args->itv, &foo, sizeof(foo))))
963 if (ldebug(setitimer)) {
964 printf("setitimer: value: sec: %ld, usec: %ld\n",
965 foo.it_value.tv_sec, foo.it_value.tv_usec);
966 printf("setitimer: interval: sec: %ld, usec: %ld\n",
967 foo.it_interval.tv_sec, foo.it_interval.tv_usec);
971 return setitimer(&bsa);
975 linux_getitimer(struct linux_getitimer_args *args)
977 struct getitimer_args bsa;
979 if (ldebug(getitimer))
980 printf(ARGS(getitimer, "%p"), (void *)args->itv);
982 bsa.which = args->which;
983 bsa.itv = (struct itimerval *)args->itv;
984 return getitimer(&bsa);
989 linux_nice(struct linux_nice_args *args)
991 struct setpriority_args bsd_args;
993 bsd_args.which = PRIO_PROCESS;
994 bsd_args.who = 0; /* current process */
995 bsd_args.prio = args->inc;
996 return setpriority(&bsd_args);
998 #endif /*!__alpha__*/
1001 linux_setgroups(struct linux_setgroups_args *args)
1003 struct thread *td = curthread;
1004 struct proc *p = td->td_proc;
1005 struct ucred *newcred, *oldcred;
1006 l_gid_t linux_gidset[NGROUPS];
1012 ngrp = args->gidsetsize;
1013 oldcred = p->p_ucred;
1016 * cr_groups[0] holds egid. Setting the whole set from
1017 * the supplied set will cause egid to be changed too.
1018 * Keep cr_groups[0] unchanged to prevent that.
1021 if ((error = suser_cred(oldcred, PRISON_ROOT)) != 0)
1024 if (ngrp >= NGROUPS)
1027 newcred = crdup(oldcred);
1029 error = copyin((caddr_t)args->grouplist, linux_gidset,
1030 ngrp * sizeof(l_gid_t));
1034 newcred->cr_ngroups = ngrp + 1;
1036 bsd_gidset = newcred->cr_groups;
1039 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1043 newcred->cr_ngroups = 1;
1047 p->p_ucred = newcred;
1053 linux_getgroups(struct linux_getgroups_args *args)
1055 struct thread *td = curthread;
1056 struct proc *p = td->td_proc;
1058 l_gid_t linux_gidset[NGROUPS];
1060 int bsd_gidsetsz, ngrp, error;
1065 bsd_gidset = cred->cr_groups;
1066 bsd_gidsetsz = cred->cr_ngroups - 1;
1069 * cr_groups[0] holds egid. Returning the whole set
1070 * here will cause a duplicate. Exclude cr_groups[0]
1074 if ((ngrp = args->gidsetsize) == 0) {
1075 p->p_retval[0] = bsd_gidsetsz;
1079 if (ngrp < bsd_gidsetsz)
1083 while (ngrp < bsd_gidsetsz) {
1084 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1088 if ((error = copyout(linux_gidset, (caddr_t)args->grouplist,
1089 ngrp * sizeof(l_gid_t))))
1092 p->p_retval[0] = ngrp;
1098 linux_setrlimit(struct linux_setrlimit_args *args)
1100 struct __setrlimit_args bsd;
1101 struct l_rlimit rlim;
1103 caddr_t sg = stackgap_init();
1106 if (ldebug(setrlimit))
1107 printf(ARGS(setrlimit, "%d, %p"),
1108 args->resource, (void *)args->rlim);
1111 if (args->resource >= LINUX_RLIM_NLIMITS)
1114 bsd.which = linux_to_bsd_resource[args->resource];
1115 if (bsd.which == -1)
1118 error = copyin((caddr_t)args->rlim, &rlim, sizeof(rlim));
1122 bsd.rlp = stackgap_alloc(&sg, sizeof(struct rlimit));
1123 bsd.rlp->rlim_cur = (rlim_t)rlim.rlim_cur;
1124 bsd.rlp->rlim_max = (rlim_t)rlim.rlim_max;
1125 return (setrlimit(&bsd));
1129 linux_old_getrlimit(struct linux_old_getrlimit_args *args)
1131 struct __getrlimit_args bsd;
1132 struct l_rlimit rlim;
1134 caddr_t sg = stackgap_init();
1137 if (ldebug(old_getrlimit))
1138 printf(ARGS(old_getrlimit, "%d, %p"),
1139 args->resource, (void *)args->rlim);
1142 if (args->resource >= LINUX_RLIM_NLIMITS)
1145 bsd.which = linux_to_bsd_resource[args->resource];
1146 if (bsd.which == -1)
1149 bsd.rlp = stackgap_alloc(&sg, sizeof(struct rlimit));
1150 error = getrlimit(&bsd);
1154 rlim.rlim_cur = (unsigned long)bsd.rlp->rlim_cur;
1155 if (rlim.rlim_cur == ULONG_MAX)
1156 rlim.rlim_cur = LONG_MAX;
1157 rlim.rlim_max = (unsigned long)bsd.rlp->rlim_max;
1158 if (rlim.rlim_max == ULONG_MAX)
1159 rlim.rlim_max = LONG_MAX;
1160 return (copyout(&rlim, (caddr_t)args->rlim, sizeof(rlim)));
1164 linux_getrlimit(struct linux_getrlimit_args *args)
1166 struct __getrlimit_args bsd;
1167 struct l_rlimit rlim;
1169 caddr_t sg = stackgap_init();
1172 if (ldebug(getrlimit))
1173 printf(ARGS(getrlimit, "%d, %p"),
1174 args->resource, (void *)args->rlim);
1177 if (args->resource >= LINUX_RLIM_NLIMITS)
1180 bsd.which = linux_to_bsd_resource[args->resource];
1181 if (bsd.which == -1)
1184 bsd.rlp = stackgap_alloc(&sg, sizeof(struct rlimit));
1185 error = getrlimit(&bsd);
1189 rlim.rlim_cur = (l_ulong)bsd.rlp->rlim_cur;
1190 rlim.rlim_max = (l_ulong)bsd.rlp->rlim_max;
1191 return (copyout(&rlim, (caddr_t)args->rlim, sizeof(rlim)));
1193 #endif /*!__alpha__*/
1196 linux_sched_setscheduler(struct linux_sched_setscheduler_args *args)
1198 struct sched_setscheduler_args bsd;
1201 if (ldebug(sched_setscheduler))
1202 printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1203 args->pid, args->policy, (const void *)args->param);
1206 switch (args->policy) {
1207 case LINUX_SCHED_OTHER:
1208 bsd.policy = SCHED_OTHER;
1210 case LINUX_SCHED_FIFO:
1211 bsd.policy = SCHED_FIFO;
1213 case LINUX_SCHED_RR:
1214 bsd.policy = SCHED_RR;
1220 bsd.pid = args->pid;
1221 bsd.param = (struct sched_param *)args->param;
1222 return sched_setscheduler(&bsd);
1226 linux_sched_getscheduler(struct linux_sched_getscheduler_args *args)
1228 struct thread *td = curthread;
1229 struct proc *p = td->td_proc;
1230 struct sched_getscheduler_args bsd;
1236 if (ldebug(sched_getscheduler))
1237 printf(ARGS(sched_getscheduler, "%d"), args->pid);
1240 bsd.pid = args->pid;
1241 error = sched_getscheduler(&bsd);
1243 switch (p->p_retval[0]) {
1245 p->p_retval[0] = LINUX_SCHED_OTHER;
1248 p->p_retval[0] = LINUX_SCHED_FIFO;
1251 p->p_retval[0] = LINUX_SCHED_RR;
1259 linux_sched_get_priority_max(struct linux_sched_get_priority_max_args *args)
1261 struct sched_get_priority_max_args bsd;
1264 if (ldebug(sched_get_priority_max))
1265 printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1268 switch (args->policy) {
1269 case LINUX_SCHED_OTHER:
1270 bsd.policy = SCHED_OTHER;
1272 case LINUX_SCHED_FIFO:
1273 bsd.policy = SCHED_FIFO;
1275 case LINUX_SCHED_RR:
1276 bsd.policy = SCHED_RR;
1281 return sched_get_priority_max(&bsd);
1285 linux_sched_get_priority_min(struct linux_sched_get_priority_min_args *args)
1287 struct sched_get_priority_min_args bsd;
1290 if (ldebug(sched_get_priority_min))
1291 printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1294 switch (args->policy) {
1295 case LINUX_SCHED_OTHER:
1296 bsd.policy = SCHED_OTHER;
1298 case LINUX_SCHED_FIFO:
1299 bsd.policy = SCHED_FIFO;
1301 case LINUX_SCHED_RR:
1302 bsd.policy = SCHED_RR;
1307 return sched_get_priority_min(&bsd);
1310 #define REBOOT_CAD_ON 0x89abcdef
1311 #define REBOOT_CAD_OFF 0
1312 #define REBOOT_HALT 0xcdef0123
1315 linux_reboot(struct linux_reboot_args *args)
1317 struct reboot_args bsd_args;
1321 printf(ARGS(reboot, "0x%x"), args->cmd);
1323 if (args->cmd == REBOOT_CAD_ON || args->cmd == REBOOT_CAD_OFF)
1325 bsd_args.opt = (args->cmd == REBOOT_HALT) ? RB_HALT : 0;
1326 return (reboot(&bsd_args));
1332 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1333 * p->p_retval[1] when COMPAT_43 or COMPAT_SUNOS is defined. This
1334 * globbers registers that are assumed to be preserved. The following
1335 * lightweight syscalls fixes this. See also linux_getgid16() and
1336 * linux_getuid16() in linux_uid16.c.
1338 * linux_getpid() - MP SAFE
1339 * linux_getgid() - MP SAFE
1340 * linux_getuid() - MP SAFE
1344 linux_getpid(struct linux_getpid_args *args)
1346 struct thread *td = curthread;
1347 struct proc *p = td->td_proc;
1351 p->p_retval[0] = p->p_pid;
1356 linux_getgid(struct linux_getgid_args *args)
1358 struct thread *td = curthread;
1359 struct proc *p = td->td_proc;
1363 p->p_retval[0] = p->p_ucred->cr_rgid;
1368 linux_getuid(struct linux_getuid_args *args)
1370 struct thread *td = curthread;
1371 struct proc *p = td->td_proc;
1375 p->p_retval[0] = p->p_ucred->cr_ruid;
1379 #endif /*!__alpha__*/
1382 linux_getsid(struct linux_getsid_args *args)
1384 struct getsid_args bsd;
1385 bsd.pid = args->pid;
1386 return getsid(&bsd);