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.22 2004/11/12 00:09:18 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>
39 #include <sys/kern_syscall.h>
42 #include <sys/mount.h>
45 #include <sys/nlookup.h>
46 #include <sys/blist.h>
47 #include <sys/reboot.h>
48 #include <sys/resourcevar.h>
49 #include <sys/signalvar.h>
51 #include <sys/sysctl.h>
52 #include <sys/sysproto.h>
54 #include <sys/unistd.h>
55 #include <sys/vmmeter.h>
56 #include <sys/vnode.h>
61 #include <vm/vm_kern.h>
62 #include <vm/vm_map.h>
63 #include <vm/vm_extern.h>
64 #include <vm/vm_object.h>
65 #include <vm/vm_zone.h>
66 #include <vm/swap_pager.h>
68 #include <machine/frame.h>
69 #include <machine/limits.h>
70 #include <machine/psl.h>
71 #include <machine/sysarch.h>
73 #include <machine/segments.h>
76 #include <emulation/posix4/sched.h>
78 #include <arch_linux/linux.h>
79 #include <arch_linux/linux_proto.h>
80 #include "linux_mib.h"
81 #include "linux_util.h"
84 #define BSD_TO_LINUX_SIGNAL(sig) (sig)
86 #define BSD_TO_LINUX_SIGNAL(sig) \
87 (((sig) <= LINUX_SIGTBLSZ) ? bsd_to_linux_signal[_SIG_IDX(sig)] : sig)
91 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
92 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
93 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
99 l_long uptime; /* Seconds since boot */
100 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */
101 l_ulong totalram; /* Total usable main memory size */
102 l_ulong freeram; /* Available memory size */
103 l_ulong sharedram; /* Amount of shared memory */
104 l_ulong bufferram; /* Memory used by buffers */
105 l_ulong totalswap; /* Total swap space size */
106 l_ulong freeswap; /* swap space still available */
107 l_ushort procs; /* Number of current processes */
108 char _f[22]; /* Pads structure to 64 bytes */
112 linux_sysinfo(struct linux_sysinfo_args *args)
114 struct l_sysinfo sysinfo;
119 /* Uptime is copied out of print_uptime() in kern_shutdown.c */
122 if (ts.tv_sec >= 86400) {
126 if (i || ts.tv_sec >= 3600) {
130 if (i || ts.tv_sec >= 60) {
134 sysinfo.uptime=ts.tv_sec;
136 /* Use the information from the mib to get our load averages */
137 for (i = 0; i < 3; i++)
138 sysinfo.loads[i] = averunnable.ldavg[i];
140 sysinfo.totalram = physmem * PAGE_SIZE;
141 sysinfo.freeram = sysinfo.totalram - vmstats.v_wire_count * PAGE_SIZE;
143 sysinfo.sharedram = 0;
144 for (object = TAILQ_FIRST(&vm_object_list); object != NULL;
145 object = TAILQ_NEXT(object, object_list))
146 if (object->shadow_count > 1)
147 sysinfo.sharedram += object->resident_page_count;
149 sysinfo.sharedram *= PAGE_SIZE;
150 sysinfo.bufferram = 0;
152 if (swapblist == NULL) {
153 sysinfo.totalswap= 0;
154 sysinfo.freeswap = 0;
156 sysinfo.totalswap = swapblist->bl_blocks * 1024;
157 sysinfo.freeswap = swapblist->bl_root->u.bmu_avail * PAGE_SIZE;
160 sysinfo.procs = 20; /* Hack */
162 return copyout(&sysinfo, (caddr_t)args->info, sizeof(sysinfo));
164 #endif /*!__alpha__*/
168 linux_alarm(struct linux_alarm_args *args)
170 struct thread *td = curthread;
171 struct proc *p = td->td_proc;
172 struct itimerval it, old_it;
180 printf(ARGS(alarm, "%u"), args->secs);
183 if (args->secs > 100000000)
186 it.it_value.tv_sec = (long)args->secs;
187 it.it_value.tv_usec = 0;
188 it.it_interval.tv_sec = 0;
189 it.it_interval.tv_usec = 0;
191 old_it = p->p_realtimer;
193 if (timevalisset(&old_it.it_value))
194 callout_stop(&p->p_ithandle);
195 if (it.it_value.tv_sec != 0) {
196 callout_reset(&p->p_ithandle, tvtohz_high(&it.it_value),
198 timevaladd(&it.it_value, &tv);
202 if (timevalcmp(&old_it.it_value, &tv, >)) {
203 timevalsub(&old_it.it_value, &tv);
204 if (old_it.it_value.tv_usec != 0)
205 old_it.it_value.tv_sec++;
206 args->sysmsg_result = old_it.it_value.tv_sec;
210 #endif /*!__alpha__*/
213 linux_brk(struct linux_brk_args *args)
215 struct thread *td = curthread;
216 struct proc *p = td->td_proc;
218 vm_offset_t new, old;
219 struct obreak_args bsd_args;
225 printf(ARGS(brk, "%p"), (void *)args->dsend);
227 old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
228 new = (vm_offset_t)args->dsend;
229 bsd_args.sysmsg_result = 0;
230 bsd_args.nsize = (char *) new;
231 bsd_args.sysmsg_result = 0;
232 if (((caddr_t)new > vm->vm_daddr) && !obreak(&bsd_args))
233 args->sysmsg_result = (long)new;
235 args->sysmsg_result = (long)old;
241 linux_uselib(struct linux_uselib_args *args)
243 struct thread *td = curthread;
245 struct nlookupdata nd;
250 unsigned long file_offset;
252 unsigned long bss_size;
257 KKASSERT(td->td_proc);
260 error = linux_copyin_path(args->library, &path, LINUX_PATH_EXISTS);
265 printf(ARGS(uselib, "%s"), path);
272 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
274 error = nlookup(&nd);
276 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
280 * From here on down, we have a locked vnode that must be unlocked.
285 if (vp->v_writecount) {
291 error = VOP_GETATTR(vp, &attr, td);
295 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
296 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
302 if (attr.va_size == 0) {
307 /* Can we access it? */
308 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, td);
312 error = VOP_OPEN(vp, FREAD, p->p_ucred, NULL, td);
317 * Lock no longer needed
319 VOP_UNLOCK(vp, 0, td);
322 /* Pull in executable header into kernel_map */
323 error = vm_mmap(kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE,
324 VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp, 0);
328 /* Is it a Linux binary ? */
329 if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
335 * While we are here, we should REALLY do some more checks
338 /* Set file/virtual offset based on a.out variant. */
339 switch ((int)(a_out->a_magic & 0xffff)) {
340 case 0413: /* ZMAGIC */
343 case 0314: /* QMAGIC */
351 bss_size = round_page(a_out->a_bss);
353 /* Check various fields in header for validity/bounds. */
354 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
359 /* text + data can't exceed file size */
360 if (a_out->a_data + a_out->a_text > attr.va_size) {
366 * text/data/bss must not exceed limits
367 * XXX - this is not complete. it should check current usage PLUS
368 * the resources needed by this library.
370 if (a_out->a_text > maxtsiz ||
371 a_out->a_data + bss_size > p->p_rlimit[RLIMIT_DATA].rlim_cur) {
376 /* prevent more writers */
380 * Check if file_offset page aligned. Currently we cannot handle
381 * misalinged file offsets, and so we read in the entire image
384 if (file_offset & PAGE_MASK) {
386 printf("uselib: Non page aligned binary %lu\n", file_offset);
388 /* Map text+data read/write/execute */
390 /* a_entry is the load address and is page aligned */
391 vmaddr = trunc_page(a_out->a_entry);
393 /* get anon user mapping, read+write+execute */
394 error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0,
395 &vmaddr, a_out->a_text + a_out->a_data, FALSE, VM_PROT_ALL,
400 /* map file into kernel_map */
401 error = vm_mmap(kernel_map, &buffer,
402 round_page(a_out->a_text + a_out->a_data + file_offset),
403 VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp,
404 trunc_page(file_offset));
408 /* copy from kernel VM space to user space */
409 error = copyout((caddr_t)(uintptr_t)(buffer + file_offset),
410 (caddr_t)vmaddr, a_out->a_text + a_out->a_data);
412 /* release temporary kernel space */
413 vm_map_remove(kernel_map, buffer, buffer +
414 round_page(a_out->a_text + a_out->a_data + file_offset));
420 printf("uselib: Page aligned binary %lu\n", file_offset);
423 * for QMAGIC, a_entry is 20 bytes beyond the load address
424 * to skip the executable header
426 vmaddr = trunc_page(a_out->a_entry);
429 * Map it all into the process's space as a single
430 * copy-on-write "data" segment.
432 error = vm_mmap(&p->p_vmspace->vm_map, &vmaddr,
433 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
434 MAP_PRIVATE | MAP_FIXED, (caddr_t)vp, file_offset);
439 printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long*)vmaddr)[0],
443 /* Calculate BSS start address */
444 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
447 /* allocate some 'anon' space */
448 error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0,
449 &vmaddr, bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
455 /* Unlock/release vnode */
458 VOP_UNLOCK(vp, 0, td);
461 /* Release the kernel mapping. */
463 vm_map_remove(kernel_map, (vm_offset_t)a_out,
464 (vm_offset_t)a_out + PAGE_SIZE);
467 linux_free_path(&path);
472 linux_select(struct linux_select_args *args)
474 struct select_args bsa;
475 struct timeval tv0, tv1, utv, *tvp;
481 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
482 (void *)args->readfds, (void *)args->writefds,
483 (void *)args->exceptfds, (void *)args->timeout);
487 bsa.sysmsg_result = 0;
489 bsa.in = args->readfds;
490 bsa.ou = args->writefds;
491 bsa.ex = args->exceptfds;
492 bsa.tv = (struct timeval *)args->timeout;
495 * Store current time for computation of the amount of
499 if ((error = copyin((caddr_t)args->timeout, &utv,
504 printf(LMSG("incoming timeout (%ld/%ld)"),
505 utv.tv_sec, utv.tv_usec);
508 if (itimerfix(&utv)) {
510 * The timeval was invalid. Convert it to something
511 * valid that will act as it does under Linux.
513 sg = stackgap_init();
514 tvp = stackgap_alloc(&sg, sizeof(utv));
515 utv.tv_sec += utv.tv_usec / 1000000;
516 utv.tv_usec %= 1000000;
517 if (utv.tv_usec < 0) {
519 utv.tv_usec += 1000000;
523 if ((error = copyout(&utv, tvp, sizeof(utv))))
530 error = select(&bsa);
531 args->sysmsg_result = bsa.sysmsg_result;
534 printf(LMSG("real select returns %d"), error);
538 * See fs/select.c in the Linux kernel. Without this,
539 * Maelstrom doesn't work.
541 if (error == ERESTART)
547 if (args->sysmsg_result) {
549 * Compute how much time was left of the timeout,
550 * by subtracting the current time and the time
551 * before we started the call, and subtracting
552 * that result from the user-supplied value.
555 timevalsub(&tv1, &tv0);
556 timevalsub(&utv, &tv1);
563 printf(LMSG("outgoing timeout (%ld/%ld)"),
564 utv.tv_sec, utv.tv_usec);
566 if ((error = copyout(&utv, (caddr_t)args->timeout,
574 printf(LMSG("select_out -> %d"), error);
580 linux_mremap(struct linux_mremap_args *args)
582 struct munmap_args bsd_args;
587 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
589 (unsigned long)args->old_len,
590 (unsigned long)args->new_len,
591 (unsigned long)args->flags);
593 args->new_len = round_page(args->new_len);
594 args->old_len = round_page(args->old_len);
596 if (args->new_len > args->old_len) {
597 args->sysmsg_result = 0;
601 if (args->new_len < args->old_len) {
602 bsd_args.sysmsg_result = 0;
603 bsd_args.addr = (caddr_t)(args->addr + args->new_len);
604 bsd_args.len = args->old_len - args->new_len;
605 error = munmap(&bsd_args);
608 args->sysmsg_resultp = error ? NULL : (void *)args->addr;
612 #define LINUX_MS_ASYNC 0x0001
613 #define LINUX_MS_INVALIDATE 0x0002
614 #define LINUX_MS_SYNC 0x0004
617 linux_msync(struct linux_msync_args *args)
619 struct msync_args bsd_args;
622 bsd_args.addr = (caddr_t)args->addr;
623 bsd_args.len = args->len;
624 bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
625 bsd_args.sysmsg_result = 0;
627 error = msync(&bsd_args);
628 args->sysmsg_result = bsd_args.sysmsg_result;
634 linux_time(struct linux_time_args *args)
642 printf(ARGS(time, "*"));
647 if (args->tm && (error = copyout(&tm, (caddr_t)args->tm, sizeof(tm))))
649 args->sysmsg_lresult = tm;
652 #endif /*!__alpha__*/
654 struct l_times_argv {
662 #define CLK_TCK 1024 /* Linux uses 1024 on alpha */
664 #define CLK_TCK 100 /* Linux uses 100 */
667 #define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
670 linux_times(struct linux_times_args *args)
672 struct thread *td = curthread;
673 struct proc *p = td->td_proc;
675 struct l_times_argv tms;
682 printf(ARGS(times, "*"));
685 calcru(p, &ru.ru_utime, &ru.ru_stime, NULL);
687 tms.tms_utime = CONVTCK(ru.ru_utime);
688 tms.tms_stime = CONVTCK(ru.ru_stime);
690 tms.tms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
691 tms.tms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
693 if ((error = copyout(&tms, (caddr_t)args->buf, sizeof(tms))))
697 args->sysmsg_result = (int)CONVTCK(tv);
702 linux_newuname(struct linux_newuname_args *args)
704 struct thread *td = curthread;
705 struct l_new_utsname utsname;
706 char *osrelease, *osname;
709 if (ldebug(newuname))
710 printf(ARGS(newuname, "*"));
713 osname = linux_get_osname(td);
714 osrelease = linux_get_osrelease(td);
716 bzero(&utsname, sizeof(utsname));
717 strncpy(utsname.sysname, osname, LINUX_MAX_UTSNAME-1);
718 strncpy(utsname.nodename, hostname, LINUX_MAX_UTSNAME-1);
719 strncpy(utsname.release, osrelease, LINUX_MAX_UTSNAME-1);
720 strncpy(utsname.version, version, LINUX_MAX_UTSNAME-1);
721 strncpy(utsname.machine, machine, LINUX_MAX_UTSNAME-1);
722 strncpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME-1);
724 return (copyout(&utsname, (caddr_t)args->buf, sizeof(utsname)));
727 #if defined(__i386__)
734 linux_utime(struct linux_utime_args *args)
736 struct timeval tv[2];
737 struct l_utimbuf lut;
738 struct nlookupdata nd;
742 error = linux_copyin_path(args->fname, &path, LINUX_PATH_EXISTS);
747 printf(ARGS(utime, "%s, *"), path);
751 error = copyin(args->times, &lut, sizeof(lut));
754 tv[0].tv_sec = lut.l_actime;
756 tv[1].tv_sec = lut.l_modtime;
759 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
761 error = kern_utimes(&nd, args->times ? tv : NULL);
764 linux_free_path(&path);
767 #endif /* __i386__ */
769 #define __WCLONE 0x80000000
773 linux_waitpid(struct linux_waitpid_args *args)
775 int error, options, status;
779 printf(ARGS(waitpid, "%d, %p, %d"),
780 args->pid, (void *)args->status, args->options);
782 options = args->options & (WNOHANG | WUNTRACED);
783 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
784 if (args->options & __WCLONE)
785 options |= WLINUXCLONE;
787 error = kern_wait(args->pid, args->status ? &status : NULL, options,
788 NULL, &args->sysmsg_result);
790 if (error == 0 && args->status) {
792 if (WIFSIGNALED(status))
793 status = (status & 0xffffff80) |
794 BSD_TO_LINUX_SIGNAL(WTERMSIG(status));
795 else if (WIFSTOPPED(status))
796 status = (status & 0xffff00ff) |
797 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(status)) << 8);
798 error = copyout(&status, args->status, sizeof(status));
803 #endif /*!__alpha__*/
806 linux_wait4(struct linux_wait4_args *args)
808 struct thread *td = curthread;
809 struct proc *p = td->td_proc;
810 struct rusage rusage;
811 int error, options, status;
817 printf(ARGS(wait4, "%d, %p, %d, %p"),
818 args->pid, (void *)args->status, args->options,
819 (void *)args->rusage);
821 options = args->options & (WNOHANG | WUNTRACED);
822 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
823 if (args->options & __WCLONE)
824 options |= WLINUXCLONE;
826 error = kern_wait(args->pid, args->status ? &status : NULL, options,
827 args->rusage ? &rusage : NULL, &args->sysmsg_result);
830 SIGDELSET(p->p_siglist, SIGCHLD);
832 if (error == 0 && args->status) {
834 if (WIFSIGNALED(status))
835 status = (status & 0xffffff80) |
836 BSD_TO_LINUX_SIGNAL(WTERMSIG(status));
837 else if (WIFSTOPPED(status))
838 status = (status & 0xffff00ff) |
839 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(status)) << 8);
840 error = copyout(&status, args->status, sizeof(status));
842 if (error == 0 && args->rusage)
843 error = copyout(&rusage, args->rusage, sizeof(rusage));
849 linux_mknod(struct linux_mknod_args *args)
851 struct nlookupdata nd;
855 error = linux_copyin_path(args->path, &path, LINUX_PATH_CREATE);
860 printf(ARGS(mknod, "%s, %d, %d"),
861 path, args->mode, args->dev);
863 error = nlookup_init(&nd, path, UIO_SYSSPACE, 0);
865 if (args->mode & S_IFIFO) {
866 error = kern_mkfifo(&nd, args->mode);
868 error = kern_mknod(&nd, args->mode, args->dev);
873 linux_free_path(&path);
878 * UGH! This is just about the dumbest idea I've ever heard!!
881 linux_personality(struct linux_personality_args *args)
884 if (ldebug(personality))
885 printf(ARGS(personality, "%d"), args->per);
892 /* Yes Jim, it's still a Linux... */
893 args->sysmsg_result = 0;
898 * Wrappers for get/setitimer for debugging..
901 linux_setitimer(struct linux_setitimer_args *args)
903 struct setitimer_args bsa;
904 struct itimerval foo;
908 if (ldebug(setitimer))
909 printf(ARGS(setitimer, "%p, %p"),
910 (void *)args->itv, (void *)args->oitv);
912 bsa.which = args->which;
913 bsa.itv = (struct itimerval *)args->itv;
914 bsa.oitv = (struct itimerval *)args->oitv;
915 bsa.sysmsg_result = 0;
917 if ((error = copyin((caddr_t)args->itv, &foo, sizeof(foo))))
920 if (ldebug(setitimer)) {
921 printf("setitimer: value: sec: %ld, usec: %ld\n",
922 foo.it_value.tv_sec, foo.it_value.tv_usec);
923 printf("setitimer: interval: sec: %ld, usec: %ld\n",
924 foo.it_interval.tv_sec, foo.it_interval.tv_usec);
928 error = setitimer(&bsa);
929 args->sysmsg_result = bsa.sysmsg_result;
934 linux_getitimer(struct linux_getitimer_args *args)
936 struct getitimer_args bsa;
939 if (ldebug(getitimer))
940 printf(ARGS(getitimer, "%p"), (void *)args->itv);
942 bsa.which = args->which;
943 bsa.itv = (struct itimerval *)args->itv;
944 bsa.sysmsg_result = 0;
945 error = getitimer(&bsa);
946 args->sysmsg_result = bsa.sysmsg_result;
952 linux_nice(struct linux_nice_args *args)
954 struct setpriority_args bsd_args;
957 bsd_args.which = PRIO_PROCESS;
958 bsd_args.who = 0; /* current process */
959 bsd_args.prio = args->inc;
960 bsd_args.sysmsg_result = 0;
961 error = setpriority(&bsd_args);
962 args->sysmsg_result = bsd_args.sysmsg_result;
965 #endif /*!__alpha__*/
968 linux_setgroups(struct linux_setgroups_args *args)
970 struct thread *td = curthread;
971 struct proc *p = td->td_proc;
972 struct ucred *newcred, *oldcred;
973 l_gid_t linux_gidset[NGROUPS];
979 ngrp = args->gidsetsize;
980 oldcred = p->p_ucred;
983 * cr_groups[0] holds egid. Setting the whole set from
984 * the supplied set will cause egid to be changed too.
985 * Keep cr_groups[0] unchanged to prevent that.
988 if ((error = suser_cred(oldcred, PRISON_ROOT)) != 0)
994 newcred = crdup(oldcred);
996 error = copyin((caddr_t)args->grouplist, linux_gidset,
997 ngrp * sizeof(l_gid_t));
1001 newcred->cr_ngroups = ngrp + 1;
1003 bsd_gidset = newcred->cr_groups;
1006 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1010 newcred->cr_ngroups = 1;
1014 p->p_ucred = newcred;
1020 linux_getgroups(struct linux_getgroups_args *args)
1022 struct thread *td = curthread;
1023 struct proc *p = td->td_proc;
1025 l_gid_t linux_gidset[NGROUPS];
1027 int bsd_gidsetsz, ngrp, error;
1032 bsd_gidset = cred->cr_groups;
1033 bsd_gidsetsz = cred->cr_ngroups - 1;
1036 * cr_groups[0] holds egid. Returning the whole set
1037 * here will cause a duplicate. Exclude cr_groups[0]
1041 if ((ngrp = args->gidsetsize) == 0) {
1042 args->sysmsg_result = bsd_gidsetsz;
1046 if (ngrp < bsd_gidsetsz)
1050 while (ngrp < bsd_gidsetsz) {
1051 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1055 if ((error = copyout(linux_gidset, (caddr_t)args->grouplist,
1056 ngrp * sizeof(l_gid_t))))
1059 args->sysmsg_result = ngrp;
1065 linux_setrlimit(struct linux_setrlimit_args *args)
1067 struct l_rlimit linux_rlim;
1073 if (ldebug(setrlimit))
1074 printf(ARGS(setrlimit, "%d, %p"),
1075 args->resource, (void *)args->rlim);
1077 if (args->resource >= LINUX_RLIM_NLIMITS)
1079 which = linux_to_bsd_resource[args->resource];
1083 error = copyin(args->rlim, &linux_rlim, sizeof(linux_rlim));
1086 rlim.rlim_cur = (rlim_t)linux_rlim.rlim_cur;
1087 rlim.rlim_max = (rlim_t)linux_rlim.rlim_max;
1089 error = kern_setrlimit(which, &rlim);
1095 linux_old_getrlimit(struct linux_old_getrlimit_args *args)
1097 struct l_rlimit linux_rlim;
1103 if (ldebug(old_getrlimit))
1104 printf(ARGS(old_getrlimit, "%d, %p"),
1105 args->resource, (void *)args->rlim);
1107 if (args->resource >= LINUX_RLIM_NLIMITS)
1109 which = linux_to_bsd_resource[args->resource];
1113 error = kern_getrlimit(which, &rlim);
1116 linux_rlim.rlim_cur = (l_ulong)rlim.rlim_cur;
1117 if (linux_rlim.rlim_cur == ULONG_MAX)
1118 linux_rlim.rlim_cur = LONG_MAX;
1119 linux_rlim.rlim_max = (l_ulong)rlim.rlim_max;
1120 if (linux_rlim.rlim_max == ULONG_MAX)
1121 linux_rlim.rlim_max = LONG_MAX;
1122 error = copyout(&linux_rlim, args->rlim, sizeof(linux_rlim));
1128 linux_getrlimit(struct linux_getrlimit_args *args)
1130 struct l_rlimit linux_rlim;
1136 if (ldebug(getrlimit))
1137 printf(ARGS(getrlimit, "%d, %p"),
1138 args->resource, (void *)args->rlim);
1140 if (args->resource >= LINUX_RLIM_NLIMITS)
1142 which = linux_to_bsd_resource[args->resource];
1146 error = kern_getrlimit(which, &rlim);
1149 linux_rlim.rlim_cur = (l_ulong)rlim.rlim_cur;
1150 linux_rlim.rlim_max = (l_ulong)rlim.rlim_max;
1151 error = copyout(&linux_rlim, args->rlim, sizeof(linux_rlim));
1155 #endif /*!__alpha__*/
1158 linux_sched_setscheduler(struct linux_sched_setscheduler_args *args)
1160 struct sched_setscheduler_args bsd;
1164 if (ldebug(sched_setscheduler))
1165 printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1166 args->pid, args->policy, (const void *)args->param);
1169 switch (args->policy) {
1170 case LINUX_SCHED_OTHER:
1171 bsd.policy = SCHED_OTHER;
1173 case LINUX_SCHED_FIFO:
1174 bsd.policy = SCHED_FIFO;
1176 case LINUX_SCHED_RR:
1177 bsd.policy = SCHED_RR;
1183 bsd.pid = args->pid;
1184 bsd.param = (struct sched_param *)args->param;
1185 bsd.sysmsg_result = 0;
1187 error = sched_setscheduler(&bsd);
1188 args->sysmsg_result = bsd.sysmsg_result;
1193 linux_sched_getscheduler(struct linux_sched_getscheduler_args *args)
1195 struct sched_getscheduler_args bsd;
1199 if (ldebug(sched_getscheduler))
1200 printf(ARGS(sched_getscheduler, "%d"), args->pid);
1203 bsd.sysmsg_result = 0;
1204 bsd.pid = args->pid;
1205 error = sched_getscheduler(&bsd);
1206 args->sysmsg_result = bsd.sysmsg_result;
1208 switch (args->sysmsg_result) {
1210 args->sysmsg_result = LINUX_SCHED_OTHER;
1213 args->sysmsg_result = LINUX_SCHED_FIFO;
1216 args->sysmsg_result = LINUX_SCHED_RR;
1223 linux_sched_get_priority_max(struct linux_sched_get_priority_max_args *args)
1225 struct sched_get_priority_max_args bsd;
1229 if (ldebug(sched_get_priority_max))
1230 printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1233 switch (args->policy) {
1234 case LINUX_SCHED_OTHER:
1235 bsd.policy = SCHED_OTHER;
1237 case LINUX_SCHED_FIFO:
1238 bsd.policy = SCHED_FIFO;
1240 case LINUX_SCHED_RR:
1241 bsd.policy = SCHED_RR;
1246 bsd.sysmsg_result = 0;
1248 error = sched_get_priority_max(&bsd);
1249 args->sysmsg_result = bsd.sysmsg_result;
1254 linux_sched_get_priority_min(struct linux_sched_get_priority_min_args *args)
1256 struct sched_get_priority_min_args bsd;
1260 if (ldebug(sched_get_priority_min))
1261 printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1264 switch (args->policy) {
1265 case LINUX_SCHED_OTHER:
1266 bsd.policy = SCHED_OTHER;
1268 case LINUX_SCHED_FIFO:
1269 bsd.policy = SCHED_FIFO;
1271 case LINUX_SCHED_RR:
1272 bsd.policy = SCHED_RR;
1277 bsd.sysmsg_result = 0;
1279 error = sched_get_priority_min(&bsd);
1280 args->sysmsg_result = bsd.sysmsg_result;
1284 #define REBOOT_CAD_ON 0x89abcdef
1285 #define REBOOT_CAD_OFF 0
1286 #define REBOOT_HALT 0xcdef0123
1289 linux_reboot(struct linux_reboot_args *args)
1291 struct reboot_args bsd_args;
1296 printf(ARGS(reboot, "0x%x"), args->cmd);
1298 if (args->cmd == REBOOT_CAD_ON || args->cmd == REBOOT_CAD_OFF)
1300 bsd_args.opt = (args->cmd == REBOOT_HALT) ? RB_HALT : 0;
1301 bsd_args.sysmsg_result = 0;
1303 error = reboot(&bsd_args);
1304 args->sysmsg_result = bsd_args.sysmsg_result;
1311 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1312 * p->p_retval[1] when COMPAT_43 or COMPAT_SUNOS is defined. This
1313 * globbers registers that are assumed to be preserved. The following
1314 * lightweight syscalls fixes this. See also linux_getgid16() and
1315 * linux_getuid16() in linux_uid16.c.
1317 * linux_getpid() - MP SAFE
1318 * linux_getgid() - MP SAFE
1319 * linux_getuid() - MP SAFE
1323 linux_getpid(struct linux_getpid_args *args)
1325 struct thread *td = curthread;
1326 struct proc *p = td->td_proc;
1330 args->sysmsg_result = p->p_pid;
1335 linux_getgid(struct linux_getgid_args *args)
1337 struct thread *td = curthread;
1338 struct proc *p = td->td_proc;
1342 args->sysmsg_result = p->p_ucred->cr_rgid;
1347 linux_getuid(struct linux_getuid_args *args)
1349 struct thread *td = curthread;
1350 struct proc *p = td->td_proc;
1354 args->sysmsg_result = p->p_ucred->cr_ruid;
1358 #endif /*!__alpha__*/
1361 linux_getsid(struct linux_getsid_args *args)
1363 struct getsid_args bsd;
1366 bsd.sysmsg_result = 0;
1367 bsd.pid = args->pid;
1368 error = getsid(&bsd);
1369 args->sysmsg_result = bsd.sysmsg_result;