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.2 2003/06/17 04:28:19 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>
42 #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 proc *p, 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 - cnt.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 proc *p, struct linux_alarm_args *args)
169 struct itimerval it, old_it;
175 printf(ARGS(alarm, "%u"), args->secs);
178 if (args->secs > 100000000)
181 it.it_value.tv_sec = (long)args->secs;
182 it.it_value.tv_usec = 0;
183 it.it_interval.tv_sec = 0;
184 it.it_interval.tv_usec = 0;
186 old_it = p->p_realtimer;
188 if (timevalisset(&old_it.it_value))
189 untimeout(realitexpire, (caddr_t)p, p->p_ithandle);
190 if (it.it_value.tv_sec != 0) {
191 p->p_ithandle = timeout(realitexpire, (caddr_t)p,
192 tvtohz(&it.it_value));
193 timevaladd(&it.it_value, &tv);
197 if (timevalcmp(&old_it.it_value, &tv, >)) {
198 timevalsub(&old_it.it_value, &tv);
199 if (old_it.it_value.tv_usec != 0)
200 old_it.it_value.tv_sec++;
201 p->p_retval[0] = old_it.it_value.tv_sec;
205 #endif /*!__alpha__*/
208 linux_brk(struct proc *p, struct linux_brk_args *args)
210 struct vmspace *vm = p->p_vmspace;
211 vm_offset_t new, old;
212 struct obreak_args /* {
218 printf(ARGS(brk, "%p"), (void *)args->dsend);
220 old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
221 new = (vm_offset_t)args->dsend;
222 tmp.nsize = (char *) new;
223 if (((caddr_t)new > vm->vm_daddr) && !obreak(p, &tmp))
224 p->p_retval[0] = (long)new;
226 p->p_retval[0] = (long)old;
232 linux_uselib(struct proc *p, struct linux_uselib_args *args)
239 unsigned long file_offset;
241 unsigned long bss_size;
246 sg = stackgap_init();
247 CHECKALTEXIST(p, &sg, args->library);
251 printf(ARGS(uselib, "%s"), args->library);
258 NDINIT(&ni, LOOKUP, FOLLOW|LOCKLEAF, UIO_USERSPACE, args->library, p);
265 * XXX - This looks like a bogus check. A LOCKLEAF namei should not
266 * succeed without returning a vnode.
269 error = ENOEXEC; /* ?? */
272 NDFREE(&ni, NDF_ONLY_PNBUF);
275 * From here on down, we have a locked vnode that must be unlocked.
280 if (vp->v_writecount) {
286 error = VOP_GETATTR(vp, &attr, p->p_ucred, p);
290 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
291 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
297 if (attr.va_size == 0) {
302 /* Can we access it? */
303 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
307 error = VOP_OPEN(vp, FREAD, p->p_ucred, p);
312 * Lock no longer needed
314 VOP_UNLOCK(vp, 0, p);
317 /* Pull in executable header into kernel_map */
318 error = vm_mmap(kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE,
319 VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp, 0);
323 /* Is it a Linux binary ? */
324 if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
330 * While we are here, we should REALLY do some more checks
333 /* Set file/virtual offset based on a.out variant. */
334 switch ((int)(a_out->a_magic & 0xffff)) {
335 case 0413: /* ZMAGIC */
338 case 0314: /* QMAGIC */
346 bss_size = round_page(a_out->a_bss);
348 /* Check various fields in header for validity/bounds. */
349 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
354 /* text + data can't exceed file size */
355 if (a_out->a_data + a_out->a_text > attr.va_size) {
361 * text/data/bss must not exceed limits
362 * XXX - this is not complete. it should check current usage PLUS
363 * the resources needed by this library.
365 if (a_out->a_text > maxtsiz ||
366 a_out->a_data + bss_size > p->p_rlimit[RLIMIT_DATA].rlim_cur) {
371 /* prevent more writers */
375 * Check if file_offset page aligned. Currently we cannot handle
376 * misalinged file offsets, and so we read in the entire image
379 if (file_offset & PAGE_MASK) {
381 printf("uselib: Non page aligned binary %lu\n", file_offset);
383 /* Map text+data read/write/execute */
385 /* a_entry is the load address and is page aligned */
386 vmaddr = trunc_page(a_out->a_entry);
388 /* get anon user mapping, read+write+execute */
389 error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0,
390 &vmaddr, a_out->a_text + a_out->a_data, FALSE, VM_PROT_ALL,
395 /* map file into kernel_map */
396 error = vm_mmap(kernel_map, &buffer,
397 round_page(a_out->a_text + a_out->a_data + file_offset),
398 VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp,
399 trunc_page(file_offset));
403 /* copy from kernel VM space to user space */
404 error = copyout((caddr_t)(uintptr_t)(buffer + file_offset),
405 (caddr_t)vmaddr, a_out->a_text + a_out->a_data);
407 /* release temporary kernel space */
408 vm_map_remove(kernel_map, buffer, buffer +
409 round_page(a_out->a_text + a_out->a_data + file_offset));
415 printf("uselib: Page aligned binary %lu\n", file_offset);
418 * for QMAGIC, a_entry is 20 bytes beyond the load address
419 * to skip the executable header
421 vmaddr = trunc_page(a_out->a_entry);
424 * Map it all into the process's space as a single
425 * copy-on-write "data" segment.
427 error = vm_mmap(&p->p_vmspace->vm_map, &vmaddr,
428 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
429 MAP_PRIVATE | MAP_FIXED, (caddr_t)vp, file_offset);
434 printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long*)vmaddr)[0],
438 /* Calculate BSS start address */
439 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
442 /* allocate some 'anon' space */
443 error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0,
444 &vmaddr, bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
450 /* Unlock vnode if needed */
452 VOP_UNLOCK(vp, 0, p);
454 /* Release the kernel mapping. */
456 vm_map_remove(kernel_map, (vm_offset_t)a_out,
457 (vm_offset_t)a_out + PAGE_SIZE);
463 linux_select(struct proc *p, struct linux_select_args *args)
465 struct select_args bsa;
466 struct timeval tv0, tv1, utv, *tvp;
472 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
473 (void *)args->readfds, (void *)args->writefds,
474 (void *)args->exceptfds, (void *)args->timeout);
479 bsa.in = args->readfds;
480 bsa.ou = args->writefds;
481 bsa.ex = args->exceptfds;
482 bsa.tv = (struct timeval *)args->timeout;
485 * Store current time for computation of the amount of
489 if ((error = copyin((caddr_t)args->timeout, &utv,
494 printf(LMSG("incoming timeout (%ld/%ld)"),
495 utv.tv_sec, utv.tv_usec);
498 if (itimerfix(&utv)) {
500 * The timeval was invalid. Convert it to something
501 * valid that will act as it does under Linux.
503 sg = stackgap_init();
504 tvp = stackgap_alloc(&sg, sizeof(utv));
505 utv.tv_sec += utv.tv_usec / 1000000;
506 utv.tv_usec %= 1000000;
507 if (utv.tv_usec < 0) {
509 utv.tv_usec += 1000000;
513 if ((error = copyout(&utv, tvp, sizeof(utv))))
520 error = select(p, &bsa);
523 printf(LMSG("real select returns %d"), error);
527 * See fs/select.c in the Linux kernel. Without this,
528 * Maelstrom doesn't work.
530 if (error == ERESTART)
536 if (p->p_retval[0]) {
538 * Compute how much time was left of the timeout,
539 * by subtracting the current time and the time
540 * before we started the call, and subtracting
541 * that result from the user-supplied value.
544 timevalsub(&tv1, &tv0);
545 timevalsub(&utv, &tv1);
552 printf(LMSG("outgoing timeout (%ld/%ld)"),
553 utv.tv_sec, utv.tv_usec);
555 if ((error = copyout(&utv, (caddr_t)args->timeout,
563 printf(LMSG("select_out -> %d"), error);
569 linux_mremap(struct proc *p, struct linux_mremap_args *args)
571 struct munmap_args /* {
579 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
581 (unsigned long)args->old_len,
582 (unsigned long)args->new_len,
583 (unsigned long)args->flags);
585 args->new_len = round_page(args->new_len);
586 args->old_len = round_page(args->old_len);
588 if (args->new_len > args->old_len) {
593 if (args->new_len < args->old_len) {
594 bsd_args.addr = (caddr_t)(args->addr + args->new_len);
595 bsd_args.len = args->old_len - args->new_len;
596 error = munmap(p, &bsd_args);
599 p->p_retval[0] = error ? 0 : (u_long)args->addr;
603 #define LINUX_MS_ASYNC 0x0001
604 #define LINUX_MS_INVALIDATE 0x0002
605 #define LINUX_MS_SYNC 0x0004
608 linux_msync(struct proc *p, struct linux_msync_args *args)
610 struct msync_args bsd_args;
612 bsd_args.addr = (caddr_t)args->addr;
613 bsd_args.len = args->len;
614 bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
616 return msync(p, &bsd_args);
621 linux_time(struct proc *p, struct linux_time_args *args)
629 printf(ARGS(time, "*"));
634 if (args->tm && (error = copyout(&tm, (caddr_t)args->tm, sizeof(tm))))
639 #endif /*!__alpha__*/
641 struct l_times_argv {
649 #define CLK_TCK 1024 /* Linux uses 1024 on alpha */
651 #define CLK_TCK 100 /* Linux uses 100 */
654 #define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
657 linux_times(struct proc *p, struct linux_times_args *args)
660 struct l_times_argv tms;
666 printf(ARGS(times, "*"));
669 calcru(p, &ru.ru_utime, &ru.ru_stime, NULL);
671 tms.tms_utime = CONVTCK(ru.ru_utime);
672 tms.tms_stime = CONVTCK(ru.ru_stime);
674 tms.tms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
675 tms.tms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
677 if ((error = copyout(&tms, (caddr_t)args->buf, sizeof(tms))))
681 p->p_retval[0] = (int)CONVTCK(tv);
686 linux_newuname(struct proc *p, struct linux_newuname_args *args)
688 struct l_new_utsname utsname;
689 char *osrelease, *osname;
692 if (ldebug(newuname))
693 printf(ARGS(newuname, "*"));
696 osname = linux_get_osname(p);
697 osrelease = linux_get_osrelease(p);
699 bzero(&utsname, sizeof(utsname));
700 strncpy(utsname.sysname, osname, LINUX_MAX_UTSNAME-1);
701 strncpy(utsname.nodename, hostname, LINUX_MAX_UTSNAME-1);
702 strncpy(utsname.release, osrelease, LINUX_MAX_UTSNAME-1);
703 strncpy(utsname.version, version, LINUX_MAX_UTSNAME-1);
704 strncpy(utsname.machine, machine, LINUX_MAX_UTSNAME-1);
705 strncpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME-1);
707 return (copyout(&utsname, (caddr_t)args->buf, sizeof(utsname)));
710 #if defined(__i386__)
717 linux_utime(struct proc *p, struct linux_utime_args *args)
719 struct utimes_args /* {
721 struct timeval *tptr;
723 struct timeval tv[2], *tvp;
724 struct l_utimbuf lut;
728 sg = stackgap_init();
729 CHECKALTEXIST(p, &sg, args->fname);
733 printf(ARGS(utime, "%s, *"), args->fname);
737 if ((error = copyin((caddr_t)args->times, &lut, sizeof lut)))
739 tv[0].tv_sec = lut.l_actime;
741 tv[1].tv_sec = lut.l_modtime;
743 /* so that utimes can copyin */
744 tvp = (struct timeval *)stackgap_alloc(&sg, sizeof(tv));
746 return (ENAMETOOLONG);
747 if ((error = copyout(tv, tvp, sizeof(tv))))
749 bsdutimes.tptr = tvp;
751 bsdutimes.tptr = NULL;
753 bsdutimes.path = args->fname;
754 return utimes(p, &bsdutimes);
756 #endif /* __i386__ */
758 #define __WCLONE 0x80000000
762 linux_waitpid(struct proc *p, struct linux_waitpid_args *args)
764 struct wait_args /* {
768 struct rusage *rusage;
774 printf(ARGS(waitpid, "%d, %p, %d"),
775 args->pid, (void *)args->status, args->options);
779 tmp.status = args->status;
780 tmp.options = (args->options & (WNOHANG | WUNTRACED));
781 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
782 if (args->options & __WCLONE)
783 tmp.options |= WLINUXCLONE;
786 if ((error = wait4(p, &tmp)) != 0)
790 if ((error = copyin((caddr_t)args->status, &tmpstat,
794 if (WIFSIGNALED(tmpstat))
795 tmpstat = (tmpstat & 0xffffff80) |
796 BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
797 else if (WIFSTOPPED(tmpstat))
798 tmpstat = (tmpstat & 0xffff00ff) |
799 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
800 return copyout(&tmpstat, (caddr_t)args->status, sizeof(int));
805 #endif /*!__alpha__*/
808 linux_wait4(struct proc *p, struct linux_wait4_args *args)
810 struct wait_args /* {
814 struct rusage *rusage;
820 printf(ARGS(wait4, "%d, %p, %d, %p"),
821 args->pid, (void *)args->status, args->options,
822 (void *)args->rusage);
826 tmp.status = args->status;
827 tmp.options = (args->options & (WNOHANG | WUNTRACED));
828 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
829 if (args->options & __WCLONE)
830 tmp.options |= WLINUXCLONE;
831 tmp.rusage = (struct rusage *)args->rusage;
833 if ((error = wait4(p, &tmp)) != 0)
836 SIGDELSET(p->p_siglist, SIGCHLD);
839 if ((error = copyin((caddr_t)args->status, &tmpstat,
843 if (WIFSIGNALED(tmpstat))
844 tmpstat = (tmpstat & 0xffffff80) |
845 BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
846 else if (WIFSTOPPED(tmpstat))
847 tmpstat = (tmpstat & 0xffff00ff) |
848 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
849 return copyout(&tmpstat, (caddr_t)args->status, sizeof(int));
856 linux_mknod(struct proc *p, struct linux_mknod_args *args)
859 struct mknod_args bsd_mknod;
860 struct mkfifo_args bsd_mkfifo;
862 sg = stackgap_init();
864 CHECKALTCREAT(p, &sg, args->path);
868 printf(ARGS(mknod, "%s, %d, %d"),
869 args->path, args->mode, args->dev);
872 if (args->mode & S_IFIFO) {
873 bsd_mkfifo.path = args->path;
874 bsd_mkfifo.mode = args->mode;
875 return mkfifo(p, &bsd_mkfifo);
877 bsd_mknod.path = args->path;
878 bsd_mknod.mode = args->mode;
879 bsd_mknod.dev = args->dev;
880 return mknod(p, &bsd_mknod);
885 * UGH! This is just about the dumbest idea I've ever heard!!
888 linux_personality(struct proc *p, struct linux_personality_args *args)
891 if (ldebug(personality))
892 printf(ARGS(personality, "%d"), args->per);
899 /* Yes Jim, it's still a Linux... */
905 * Wrappers for get/setitimer for debugging..
908 linux_setitimer(struct proc *p, struct linux_setitimer_args *args)
910 struct setitimer_args bsa;
911 struct itimerval foo;
915 if (ldebug(setitimer))
916 printf(ARGS(setitimer, "%p, %p"),
917 (void *)args->itv, (void *)args->oitv);
919 bsa.which = args->which;
920 bsa.itv = (struct itimerval *)args->itv;
921 bsa.oitv = (struct itimerval *)args->oitv;
923 if ((error = copyin((caddr_t)args->itv, &foo, sizeof(foo))))
926 if (ldebug(setitimer)) {
927 printf("setitimer: value: sec: %ld, usec: %ld\n",
928 foo.it_value.tv_sec, foo.it_value.tv_usec);
929 printf("setitimer: interval: sec: %ld, usec: %ld\n",
930 foo.it_interval.tv_sec, foo.it_interval.tv_usec);
934 return setitimer(p, &bsa);
938 linux_getitimer(struct proc *p, struct linux_getitimer_args *args)
940 struct getitimer_args bsa;
942 if (ldebug(getitimer))
943 printf(ARGS(getitimer, "%p"), (void *)args->itv);
945 bsa.which = args->which;
946 bsa.itv = (struct itimerval *)args->itv;
947 return getitimer(p, &bsa);
952 linux_nice(struct proc *p, struct linux_nice_args *args)
954 struct setpriority_args bsd_args;
956 bsd_args.which = PRIO_PROCESS;
957 bsd_args.who = 0; /* current process */
958 bsd_args.prio = args->inc;
959 return setpriority(p, &bsd_args);
961 #endif /*!__alpha__*/
964 linux_setgroups(struct proc *p, struct linux_setgroups_args *args)
966 struct ucred *newcred, *oldcred;
967 l_gid_t linux_gidset[NGROUPS];
971 ngrp = args->gidsetsize;
972 oldcred = p->p_ucred;
975 * cr_groups[0] holds egid. Setting the whole set from
976 * the supplied set will cause egid to be changed too.
977 * Keep cr_groups[0] unchanged to prevent that.
980 if ((error = suser_xxx(oldcred, NULL, PRISON_ROOT)) != 0)
986 newcred = crdup(oldcred);
988 error = copyin((caddr_t)args->grouplist, linux_gidset,
989 ngrp * sizeof(l_gid_t));
993 newcred->cr_ngroups = ngrp + 1;
995 bsd_gidset = newcred->cr_groups;
998 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1003 newcred->cr_ngroups = 1;
1006 p->p_ucred = newcred;
1012 linux_getgroups(struct proc *p, struct linux_getgroups_args *args)
1015 l_gid_t linux_gidset[NGROUPS];
1017 int bsd_gidsetsz, ngrp, error;
1020 bsd_gidset = cred->cr_groups;
1021 bsd_gidsetsz = cred->cr_ngroups - 1;
1024 * cr_groups[0] holds egid. Returning the whole set
1025 * here will cause a duplicate. Exclude cr_groups[0]
1029 if ((ngrp = args->gidsetsize) == 0) {
1030 p->p_retval[0] = bsd_gidsetsz;
1034 if (ngrp < bsd_gidsetsz)
1038 while (ngrp < bsd_gidsetsz) {
1039 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1043 if ((error = copyout(linux_gidset, (caddr_t)args->grouplist,
1044 ngrp * sizeof(l_gid_t))))
1047 p->p_retval[0] = ngrp;
1053 linux_setrlimit(struct proc *p, struct linux_setrlimit_args *args)
1055 struct __setrlimit_args bsd;
1056 struct l_rlimit rlim;
1058 caddr_t sg = stackgap_init();
1061 if (ldebug(setrlimit))
1062 printf(ARGS(setrlimit, "%d, %p"),
1063 args->resource, (void *)args->rlim);
1066 if (args->resource >= LINUX_RLIM_NLIMITS)
1069 bsd.which = linux_to_bsd_resource[args->resource];
1070 if (bsd.which == -1)
1073 error = copyin((caddr_t)args->rlim, &rlim, sizeof(rlim));
1077 bsd.rlp = stackgap_alloc(&sg, sizeof(struct rlimit));
1078 bsd.rlp->rlim_cur = (rlim_t)rlim.rlim_cur;
1079 bsd.rlp->rlim_max = (rlim_t)rlim.rlim_max;
1080 return (setrlimit(p, &bsd));
1084 linux_old_getrlimit(struct proc *p, struct linux_old_getrlimit_args *args)
1086 struct __getrlimit_args bsd;
1087 struct l_rlimit rlim;
1089 caddr_t sg = stackgap_init();
1092 if (ldebug(old_getrlimit))
1093 printf(ARGS(old_getrlimit, "%d, %p"),
1094 args->resource, (void *)args->rlim);
1097 if (args->resource >= LINUX_RLIM_NLIMITS)
1100 bsd.which = linux_to_bsd_resource[args->resource];
1101 if (bsd.which == -1)
1104 bsd.rlp = stackgap_alloc(&sg, sizeof(struct rlimit));
1105 error = getrlimit(p, &bsd);
1109 rlim.rlim_cur = (unsigned long)bsd.rlp->rlim_cur;
1110 if (rlim.rlim_cur == ULONG_MAX)
1111 rlim.rlim_cur = LONG_MAX;
1112 rlim.rlim_max = (unsigned long)bsd.rlp->rlim_max;
1113 if (rlim.rlim_max == ULONG_MAX)
1114 rlim.rlim_max = LONG_MAX;
1115 return (copyout(&rlim, (caddr_t)args->rlim, sizeof(rlim)));
1119 linux_getrlimit(struct proc *p, struct linux_getrlimit_args *args)
1121 struct __getrlimit_args bsd;
1122 struct l_rlimit rlim;
1124 caddr_t sg = stackgap_init();
1127 if (ldebug(getrlimit))
1128 printf(ARGS(getrlimit, "%d, %p"),
1129 args->resource, (void *)args->rlim);
1132 if (args->resource >= LINUX_RLIM_NLIMITS)
1135 bsd.which = linux_to_bsd_resource[args->resource];
1136 if (bsd.which == -1)
1139 bsd.rlp = stackgap_alloc(&sg, sizeof(struct rlimit));
1140 error = getrlimit(p, &bsd);
1144 rlim.rlim_cur = (l_ulong)bsd.rlp->rlim_cur;
1145 rlim.rlim_max = (l_ulong)bsd.rlp->rlim_max;
1146 return (copyout(&rlim, (caddr_t)args->rlim, sizeof(rlim)));
1148 #endif /*!__alpha__*/
1151 linux_sched_setscheduler(struct proc *p,
1152 struct linux_sched_setscheduler_args *args)
1154 struct sched_setscheduler_args bsd;
1157 if (ldebug(sched_setscheduler))
1158 printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1159 args->pid, args->policy, (const void *)args->param);
1162 switch (args->policy) {
1163 case LINUX_SCHED_OTHER:
1164 bsd.policy = SCHED_OTHER;
1166 case LINUX_SCHED_FIFO:
1167 bsd.policy = SCHED_FIFO;
1169 case LINUX_SCHED_RR:
1170 bsd.policy = SCHED_RR;
1176 bsd.pid = args->pid;
1177 bsd.param = (struct sched_param *)args->param;
1178 return sched_setscheduler(p, &bsd);
1182 linux_sched_getscheduler(struct proc *p,
1183 struct linux_sched_getscheduler_args *args)
1185 struct sched_getscheduler_args bsd;
1189 if (ldebug(sched_getscheduler))
1190 printf(ARGS(sched_getscheduler, "%d"), args->pid);
1193 bsd.pid = args->pid;
1194 error = sched_getscheduler(p, &bsd);
1196 switch (p->p_retval[0]) {
1198 p->p_retval[0] = LINUX_SCHED_OTHER;
1201 p->p_retval[0] = LINUX_SCHED_FIFO;
1204 p->p_retval[0] = LINUX_SCHED_RR;
1212 linux_sched_get_priority_max(struct proc *p,
1213 struct linux_sched_get_priority_max_args *args)
1215 struct sched_get_priority_max_args bsd;
1218 if (ldebug(sched_get_priority_max))
1219 printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1222 switch (args->policy) {
1223 case LINUX_SCHED_OTHER:
1224 bsd.policy = SCHED_OTHER;
1226 case LINUX_SCHED_FIFO:
1227 bsd.policy = SCHED_FIFO;
1229 case LINUX_SCHED_RR:
1230 bsd.policy = SCHED_RR;
1235 return sched_get_priority_max(p, &bsd);
1239 linux_sched_get_priority_min(struct proc *p,
1240 struct linux_sched_get_priority_min_args *args)
1242 struct sched_get_priority_min_args bsd;
1245 if (ldebug(sched_get_priority_min))
1246 printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1249 switch (args->policy) {
1250 case LINUX_SCHED_OTHER:
1251 bsd.policy = SCHED_OTHER;
1253 case LINUX_SCHED_FIFO:
1254 bsd.policy = SCHED_FIFO;
1256 case LINUX_SCHED_RR:
1257 bsd.policy = SCHED_RR;
1262 return sched_get_priority_min(p, &bsd);
1265 #define REBOOT_CAD_ON 0x89abcdef
1266 #define REBOOT_CAD_OFF 0
1267 #define REBOOT_HALT 0xcdef0123
1270 linux_reboot(struct proc *p, struct linux_reboot_args *args)
1272 struct reboot_args bsd_args;
1276 printf(ARGS(reboot, "0x%x"), args->cmd);
1278 if (args->cmd == REBOOT_CAD_ON || args->cmd == REBOOT_CAD_OFF)
1280 bsd_args.opt = (args->cmd == REBOOT_HALT) ? RB_HALT : 0;
1281 return (reboot(p, &bsd_args));
1287 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1288 * p->p_retval[1] when COMPAT_43 or COMPAT_SUNOS is defined. This
1289 * globbers registers that are assumed to be preserved. The following
1290 * lightweight syscalls fixes this. See also linux_getgid16() and
1291 * linux_getuid16() in linux_uid16.c.
1293 * linux_getpid() - MP SAFE
1294 * linux_getgid() - MP SAFE
1295 * linux_getuid() - MP SAFE
1299 linux_getpid(struct proc *p, struct linux_getpid_args *args)
1302 p->p_retval[0] = p->p_pid;
1307 linux_getgid(struct proc *p, struct linux_getgid_args *args)
1310 p->p_retval[0] = p->p_cred->p_rgid;
1315 linux_getuid(struct proc *p, struct linux_getuid_args *args)
1318 p->p_retval[0] = p->p_cred->p_ruid;
1322 #endif /*!__alpha__*/
1325 linux_getsid(struct proc *p, struct linux_getsid_args *args)
1327 struct getsid_args bsd;
1328 bsd.pid = args->pid;
1329 return getsid(p, &bsd);