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.16 2003/11/13 04:04:42 daver 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/namei.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 untimeout(realitexpire, (caddr_t)p, p->p_ithandle);
195 if (it.it_value.tv_sec != 0) {
196 p->p_ithandle = timeout(realitexpire, (caddr_t)p,
197 tvtohz(&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;
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 NDINIT(&ni, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF,
273 UIO_SYSSPACE, path, td);
280 * XXX - This looks like a bogus check. A LOCKLEAF namei should not
281 * succeed without returning a vnode.
284 error = ENOEXEC; /* ?? */
287 NDFREE(&ni, NDF_ONLY_PNBUF);
290 * From here on down, we have a locked vnode that must be unlocked.
295 if (vp->v_writecount) {
301 error = VOP_GETATTR(vp, &attr, td);
305 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
306 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
312 if (attr.va_size == 0) {
317 /* Can we access it? */
318 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, td);
322 error = VOP_OPEN(vp, FREAD, p->p_ucred, td);
327 * Lock no longer needed
329 VOP_UNLOCK(vp, 0, td);
332 /* Pull in executable header into kernel_map */
333 error = vm_mmap(kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE,
334 VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp, 0);
338 /* Is it a Linux binary ? */
339 if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
345 * While we are here, we should REALLY do some more checks
348 /* Set file/virtual offset based on a.out variant. */
349 switch ((int)(a_out->a_magic & 0xffff)) {
350 case 0413: /* ZMAGIC */
353 case 0314: /* QMAGIC */
361 bss_size = round_page(a_out->a_bss);
363 /* Check various fields in header for validity/bounds. */
364 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
369 /* text + data can't exceed file size */
370 if (a_out->a_data + a_out->a_text > attr.va_size) {
376 * text/data/bss must not exceed limits
377 * XXX - this is not complete. it should check current usage PLUS
378 * the resources needed by this library.
380 if (a_out->a_text > maxtsiz ||
381 a_out->a_data + bss_size > p->p_rlimit[RLIMIT_DATA].rlim_cur) {
386 /* prevent more writers */
390 * Check if file_offset page aligned. Currently we cannot handle
391 * misalinged file offsets, and so we read in the entire image
394 if (file_offset & PAGE_MASK) {
396 printf("uselib: Non page aligned binary %lu\n", file_offset);
398 /* Map text+data read/write/execute */
400 /* a_entry is the load address and is page aligned */
401 vmaddr = trunc_page(a_out->a_entry);
403 /* get anon user mapping, read+write+execute */
404 error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0,
405 &vmaddr, a_out->a_text + a_out->a_data, FALSE, VM_PROT_ALL,
410 /* map file into kernel_map */
411 error = vm_mmap(kernel_map, &buffer,
412 round_page(a_out->a_text + a_out->a_data + file_offset),
413 VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp,
414 trunc_page(file_offset));
418 /* copy from kernel VM space to user space */
419 error = copyout((caddr_t)(uintptr_t)(buffer + file_offset),
420 (caddr_t)vmaddr, a_out->a_text + a_out->a_data);
422 /* release temporary kernel space */
423 vm_map_remove(kernel_map, buffer, buffer +
424 round_page(a_out->a_text + a_out->a_data + file_offset));
430 printf("uselib: Page aligned binary %lu\n", file_offset);
433 * for QMAGIC, a_entry is 20 bytes beyond the load address
434 * to skip the executable header
436 vmaddr = trunc_page(a_out->a_entry);
439 * Map it all into the process's space as a single
440 * copy-on-write "data" segment.
442 error = vm_mmap(&p->p_vmspace->vm_map, &vmaddr,
443 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
444 MAP_PRIVATE | MAP_FIXED, (caddr_t)vp, file_offset);
449 printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long*)vmaddr)[0],
453 /* Calculate BSS start address */
454 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
457 /* allocate some 'anon' space */
458 error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0,
459 &vmaddr, bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
465 /* Unlock vnode if needed */
467 VOP_UNLOCK(vp, 0, td);
469 /* Release the kernel mapping. */
471 vm_map_remove(kernel_map, (vm_offset_t)a_out,
472 (vm_offset_t)a_out + PAGE_SIZE);
474 linux_free_path(&path);
479 linux_select(struct linux_select_args *args)
481 struct select_args bsa;
482 struct timeval tv0, tv1, utv, *tvp;
488 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
489 (void *)args->readfds, (void *)args->writefds,
490 (void *)args->exceptfds, (void *)args->timeout);
494 bsa.sysmsg_result = 0;
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);
538 args->sysmsg_result = bsa.sysmsg_result;
541 printf(LMSG("real select returns %d"), error);
545 * See fs/select.c in the Linux kernel. Without this,
546 * Maelstrom doesn't work.
548 if (error == ERESTART)
554 if (args->sysmsg_result) {
556 * Compute how much time was left of the timeout,
557 * by subtracting the current time and the time
558 * before we started the call, and subtracting
559 * that result from the user-supplied value.
562 timevalsub(&tv1, &tv0);
563 timevalsub(&utv, &tv1);
570 printf(LMSG("outgoing timeout (%ld/%ld)"),
571 utv.tv_sec, utv.tv_usec);
573 if ((error = copyout(&utv, (caddr_t)args->timeout,
581 printf(LMSG("select_out -> %d"), error);
587 linux_mremap(struct linux_mremap_args *args)
589 struct munmap_args bsd_args;
594 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
596 (unsigned long)args->old_len,
597 (unsigned long)args->new_len,
598 (unsigned long)args->flags);
600 args->new_len = round_page(args->new_len);
601 args->old_len = round_page(args->old_len);
603 if (args->new_len > args->old_len) {
604 args->sysmsg_result = 0;
608 if (args->new_len < args->old_len) {
609 bsd_args.sysmsg_result = 0;
610 bsd_args.addr = (caddr_t)(args->addr + args->new_len);
611 bsd_args.len = args->old_len - args->new_len;
612 error = munmap(&bsd_args);
615 args->sysmsg_resultp = error ? NULL : (void *)args->addr;
619 #define LINUX_MS_ASYNC 0x0001
620 #define LINUX_MS_INVALIDATE 0x0002
621 #define LINUX_MS_SYNC 0x0004
624 linux_msync(struct linux_msync_args *args)
626 struct msync_args bsd_args;
629 bsd_args.addr = (caddr_t)args->addr;
630 bsd_args.len = args->len;
631 bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
632 bsd_args.sysmsg_result = 0;
634 error = msync(&bsd_args);
635 args->sysmsg_result = bsd_args.sysmsg_result;
641 linux_time(struct linux_time_args *args)
649 printf(ARGS(time, "*"));
654 if (args->tm && (error = copyout(&tm, (caddr_t)args->tm, sizeof(tm))))
656 args->sysmsg_lresult = tm;
659 #endif /*!__alpha__*/
661 struct l_times_argv {
669 #define CLK_TCK 1024 /* Linux uses 1024 on alpha */
671 #define CLK_TCK 100 /* Linux uses 100 */
674 #define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
677 linux_times(struct linux_times_args *args)
679 struct thread *td = curthread;
680 struct proc *p = td->td_proc;
682 struct l_times_argv tms;
689 printf(ARGS(times, "*"));
692 calcru(p, &ru.ru_utime, &ru.ru_stime, NULL);
694 tms.tms_utime = CONVTCK(ru.ru_utime);
695 tms.tms_stime = CONVTCK(ru.ru_stime);
697 tms.tms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
698 tms.tms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
700 if ((error = copyout(&tms, (caddr_t)args->buf, sizeof(tms))))
704 args->sysmsg_result = (int)CONVTCK(tv);
709 linux_newuname(struct linux_newuname_args *args)
711 struct thread *td = curthread;
712 struct l_new_utsname utsname;
713 char *osrelease, *osname;
716 if (ldebug(newuname))
717 printf(ARGS(newuname, "*"));
720 osname = linux_get_osname(td);
721 osrelease = linux_get_osrelease(td);
723 bzero(&utsname, sizeof(utsname));
724 strncpy(utsname.sysname, osname, LINUX_MAX_UTSNAME-1);
725 strncpy(utsname.nodename, hostname, LINUX_MAX_UTSNAME-1);
726 strncpy(utsname.release, osrelease, LINUX_MAX_UTSNAME-1);
727 strncpy(utsname.version, version, LINUX_MAX_UTSNAME-1);
728 strncpy(utsname.machine, machine, LINUX_MAX_UTSNAME-1);
729 strncpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME-1);
731 return (copyout(&utsname, (caddr_t)args->buf, sizeof(utsname)));
734 #if defined(__i386__)
741 linux_utime(struct linux_utime_args *args)
743 struct thread *td = curthread;
744 struct timeval tv[2];
745 struct l_utimbuf lut;
750 error = linux_copyin_path(args->fname, &path, LINUX_PATH_EXISTS);
755 printf(ARGS(utime, "%s, *"), path);
759 error = copyin(args->times, &lut, sizeof(lut));
762 tv[0].tv_sec = lut.l_actime;
764 tv[1].tv_sec = lut.l_modtime;
767 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_SYSSPACE, path, td);
769 error = kern_utimes(&nd, args->times ? tv : NULL);
772 linux_free_path(&path);
775 #endif /* __i386__ */
777 #define __WCLONE 0x80000000
781 linux_waitpid(struct linux_waitpid_args *args)
783 int error, options, status;
787 printf(ARGS(waitpid, "%d, %p, %d"),
788 args->pid, (void *)args->status, args->options);
790 options = args->options & (WNOHANG | WUNTRACED);
791 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
792 if (args->options & __WCLONE)
793 options |= WLINUXCLONE;
795 error = kern_wait(args->pid, args->status ? &status : NULL, options,
796 NULL, &args->sysmsg_result);
798 if (error == 0 && args->status) {
800 if (WIFSIGNALED(status))
801 status = (status & 0xffffff80) |
802 BSD_TO_LINUX_SIGNAL(WTERMSIG(status));
803 else if (WIFSTOPPED(status))
804 status = (status & 0xffff00ff) |
805 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(status)) << 8);
806 error = copyout(&status, args->status, sizeof(status));
811 #endif /*!__alpha__*/
814 linux_wait4(struct linux_wait4_args *args)
816 struct thread *td = curthread;
817 struct proc *p = td->td_proc;
818 struct rusage rusage;
819 int error, options, status;
825 printf(ARGS(wait4, "%d, %p, %d, %p"),
826 args->pid, (void *)args->status, args->options,
827 (void *)args->rusage);
829 options = args->options & (WNOHANG | WUNTRACED);
830 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
831 if (args->options & __WCLONE)
832 options |= WLINUXCLONE;
834 error = kern_wait(args->pid, args->status ? &status : NULL, options,
835 args->rusage ? &rusage : NULL, &args->sysmsg_result);
838 SIGDELSET(p->p_siglist, SIGCHLD);
840 if (error == 0 && args->status) {
842 if (WIFSIGNALED(status))
843 status = (status & 0xffffff80) |
844 BSD_TO_LINUX_SIGNAL(WTERMSIG(status));
845 else if (WIFSTOPPED(status))
846 status = (status & 0xffff00ff) |
847 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(status)) << 8);
848 error = copyout(&status, args->status, sizeof(status));
850 if (error == 0 && args->rusage)
851 error = copyout(&rusage, args->rusage, sizeof(rusage));
857 linux_mknod(struct linux_mknod_args *args)
859 struct thread *td = curthread;
864 error = linux_copyin_path(args->path, &path, LINUX_PATH_CREATE);
869 printf(ARGS(mknod, "%s, %d, %d"),
870 path, args->mode, args->dev);
872 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_SYSSPACE, path, td);
874 if (args->mode & S_IFIFO) {
875 error = kern_mkfifo(&nd, args->mode);
877 error = kern_mknod(&nd, args->mode, args->dev);
880 linux_free_path(&path);
885 * UGH! This is just about the dumbest idea I've ever heard!!
888 linux_personality(struct linux_personality_args *args)
891 if (ldebug(personality))
892 printf(ARGS(personality, "%d"), args->per);
899 /* Yes Jim, it's still a Linux... */
900 args->sysmsg_result = 0;
905 * Wrappers for get/setitimer for debugging..
908 linux_setitimer(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;
922 bsa.sysmsg_result = 0;
924 if ((error = copyin((caddr_t)args->itv, &foo, sizeof(foo))))
927 if (ldebug(setitimer)) {
928 printf("setitimer: value: sec: %ld, usec: %ld\n",
929 foo.it_value.tv_sec, foo.it_value.tv_usec);
930 printf("setitimer: interval: sec: %ld, usec: %ld\n",
931 foo.it_interval.tv_sec, foo.it_interval.tv_usec);
935 error = setitimer(&bsa);
936 args->sysmsg_result = bsa.sysmsg_result;
941 linux_getitimer(struct linux_getitimer_args *args)
943 struct getitimer_args bsa;
946 if (ldebug(getitimer))
947 printf(ARGS(getitimer, "%p"), (void *)args->itv);
949 bsa.which = args->which;
950 bsa.itv = (struct itimerval *)args->itv;
951 bsa.sysmsg_result = 0;
952 error = getitimer(&bsa);
953 args->sysmsg_result = bsa.sysmsg_result;
959 linux_nice(struct linux_nice_args *args)
961 struct setpriority_args bsd_args;
964 bsd_args.which = PRIO_PROCESS;
965 bsd_args.who = 0; /* current process */
966 bsd_args.prio = args->inc;
967 bsd_args.sysmsg_result = 0;
968 error = setpriority(&bsd_args);
969 args->sysmsg_result = bsd_args.sysmsg_result;
972 #endif /*!__alpha__*/
975 linux_setgroups(struct linux_setgroups_args *args)
977 struct thread *td = curthread;
978 struct proc *p = td->td_proc;
979 struct ucred *newcred, *oldcred;
980 l_gid_t linux_gidset[NGROUPS];
986 ngrp = args->gidsetsize;
987 oldcred = p->p_ucred;
990 * cr_groups[0] holds egid. Setting the whole set from
991 * the supplied set will cause egid to be changed too.
992 * Keep cr_groups[0] unchanged to prevent that.
995 if ((error = suser_cred(oldcred, PRISON_ROOT)) != 0)
1001 newcred = crdup(oldcred);
1003 error = copyin((caddr_t)args->grouplist, linux_gidset,
1004 ngrp * sizeof(l_gid_t));
1008 newcred->cr_ngroups = ngrp + 1;
1010 bsd_gidset = newcred->cr_groups;
1013 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1017 newcred->cr_ngroups = 1;
1021 p->p_ucred = newcred;
1027 linux_getgroups(struct linux_getgroups_args *args)
1029 struct thread *td = curthread;
1030 struct proc *p = td->td_proc;
1032 l_gid_t linux_gidset[NGROUPS];
1034 int bsd_gidsetsz, ngrp, error;
1039 bsd_gidset = cred->cr_groups;
1040 bsd_gidsetsz = cred->cr_ngroups - 1;
1043 * cr_groups[0] holds egid. Returning the whole set
1044 * here will cause a duplicate. Exclude cr_groups[0]
1048 if ((ngrp = args->gidsetsize) == 0) {
1049 args->sysmsg_result = bsd_gidsetsz;
1053 if (ngrp < bsd_gidsetsz)
1057 while (ngrp < bsd_gidsetsz) {
1058 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1062 if ((error = copyout(linux_gidset, (caddr_t)args->grouplist,
1063 ngrp * sizeof(l_gid_t))))
1066 args->sysmsg_result = ngrp;
1072 linux_setrlimit(struct linux_setrlimit_args *args)
1074 struct l_rlimit linux_rlim;
1080 if (ldebug(setrlimit))
1081 printf(ARGS(setrlimit, "%d, %p"),
1082 args->resource, (void *)args->rlim);
1084 if (args->resource >= LINUX_RLIM_NLIMITS)
1086 which = linux_to_bsd_resource[args->resource];
1090 error = copyin(args->rlim, &linux_rlim, sizeof(linux_rlim));
1093 rlim.rlim_cur = (rlim_t)linux_rlim.rlim_cur;
1094 rlim.rlim_max = (rlim_t)linux_rlim.rlim_max;
1096 error = kern_setrlimit(which, &rlim);
1102 linux_old_getrlimit(struct linux_old_getrlimit_args *args)
1104 struct l_rlimit linux_rlim;
1110 if (ldebug(old_getrlimit))
1111 printf(ARGS(old_getrlimit, "%d, %p"),
1112 args->resource, (void *)args->rlim);
1114 if (args->resource >= LINUX_RLIM_NLIMITS)
1116 which = linux_to_bsd_resource[args->resource];
1120 error = kern_getrlimit(which, &rlim);
1123 linux_rlim.rlim_cur = (l_ulong)rlim.rlim_cur;
1124 if (linux_rlim.rlim_cur == ULONG_MAX)
1125 linux_rlim.rlim_cur = LONG_MAX;
1126 linux_rlim.rlim_max = (l_ulong)rlim.rlim_max;
1127 if (linux_rlim.rlim_max == ULONG_MAX)
1128 linux_rlim.rlim_max = LONG_MAX;
1129 error = copyout(&linux_rlim, args->rlim, sizeof(rlim));
1135 linux_getrlimit(struct linux_getrlimit_args *args)
1137 struct l_rlimit linux_rlim;
1143 if (ldebug(getrlimit))
1144 printf(ARGS(getrlimit, "%d, %p"),
1145 args->resource, (void *)args->rlim);
1147 if (args->resource >= LINUX_RLIM_NLIMITS)
1149 which = linux_to_bsd_resource[args->resource];
1153 error = kern_getrlimit(which, &rlim);
1156 linux_rlim.rlim_cur = (l_ulong)rlim.rlim_cur;
1157 linux_rlim.rlim_max = (l_ulong)rlim.rlim_max;
1158 error = copyout(&linux_rlim, args->rlim, sizeof(rlim));
1162 #endif /*!__alpha__*/
1165 linux_sched_setscheduler(struct linux_sched_setscheduler_args *args)
1167 struct sched_setscheduler_args bsd;
1171 if (ldebug(sched_setscheduler))
1172 printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1173 args->pid, args->policy, (const void *)args->param);
1176 switch (args->policy) {
1177 case LINUX_SCHED_OTHER:
1178 bsd.policy = SCHED_OTHER;
1180 case LINUX_SCHED_FIFO:
1181 bsd.policy = SCHED_FIFO;
1183 case LINUX_SCHED_RR:
1184 bsd.policy = SCHED_RR;
1190 bsd.pid = args->pid;
1191 bsd.param = (struct sched_param *)args->param;
1192 bsd.sysmsg_result = 0;
1194 error = sched_setscheduler(&bsd);
1195 args->sysmsg_result = bsd.sysmsg_result;
1200 linux_sched_getscheduler(struct linux_sched_getscheduler_args *args)
1202 struct sched_getscheduler_args bsd;
1206 if (ldebug(sched_getscheduler))
1207 printf(ARGS(sched_getscheduler, "%d"), args->pid);
1210 bsd.sysmsg_result = 0;
1211 bsd.pid = args->pid;
1212 error = sched_getscheduler(&bsd);
1213 args->sysmsg_result = bsd.sysmsg_result;
1215 switch (args->sysmsg_result) {
1217 args->sysmsg_result = LINUX_SCHED_OTHER;
1220 args->sysmsg_result = LINUX_SCHED_FIFO;
1223 args->sysmsg_result = LINUX_SCHED_RR;
1230 linux_sched_get_priority_max(struct linux_sched_get_priority_max_args *args)
1232 struct sched_get_priority_max_args bsd;
1236 if (ldebug(sched_get_priority_max))
1237 printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1240 switch (args->policy) {
1241 case LINUX_SCHED_OTHER:
1242 bsd.policy = SCHED_OTHER;
1244 case LINUX_SCHED_FIFO:
1245 bsd.policy = SCHED_FIFO;
1247 case LINUX_SCHED_RR:
1248 bsd.policy = SCHED_RR;
1253 bsd.sysmsg_result = 0;
1255 error = sched_get_priority_max(&bsd);
1256 args->sysmsg_result = bsd.sysmsg_result;
1261 linux_sched_get_priority_min(struct linux_sched_get_priority_min_args *args)
1263 struct sched_get_priority_min_args bsd;
1267 if (ldebug(sched_get_priority_min))
1268 printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1271 switch (args->policy) {
1272 case LINUX_SCHED_OTHER:
1273 bsd.policy = SCHED_OTHER;
1275 case LINUX_SCHED_FIFO:
1276 bsd.policy = SCHED_FIFO;
1278 case LINUX_SCHED_RR:
1279 bsd.policy = SCHED_RR;
1284 bsd.sysmsg_result = 0;
1286 error = sched_get_priority_min(&bsd);
1287 args->sysmsg_result = bsd.sysmsg_result;
1291 #define REBOOT_CAD_ON 0x89abcdef
1292 #define REBOOT_CAD_OFF 0
1293 #define REBOOT_HALT 0xcdef0123
1296 linux_reboot(struct linux_reboot_args *args)
1298 struct reboot_args bsd_args;
1303 printf(ARGS(reboot, "0x%x"), args->cmd);
1305 if (args->cmd == REBOOT_CAD_ON || args->cmd == REBOOT_CAD_OFF)
1307 bsd_args.opt = (args->cmd == REBOOT_HALT) ? RB_HALT : 0;
1308 bsd_args.sysmsg_result = 0;
1310 error = reboot(&bsd_args);
1311 args->sysmsg_result = bsd_args.sysmsg_result;
1318 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1319 * p->p_retval[1] when COMPAT_43 or COMPAT_SUNOS is defined. This
1320 * globbers registers that are assumed to be preserved. The following
1321 * lightweight syscalls fixes this. See also linux_getgid16() and
1322 * linux_getuid16() in linux_uid16.c.
1324 * linux_getpid() - MP SAFE
1325 * linux_getgid() - MP SAFE
1326 * linux_getuid() - MP SAFE
1330 linux_getpid(struct linux_getpid_args *args)
1332 struct thread *td = curthread;
1333 struct proc *p = td->td_proc;
1337 args->sysmsg_result = p->p_pid;
1342 linux_getgid(struct linux_getgid_args *args)
1344 struct thread *td = curthread;
1345 struct proc *p = td->td_proc;
1349 args->sysmsg_result = p->p_ucred->cr_rgid;
1354 linux_getuid(struct linux_getuid_args *args)
1356 struct thread *td = curthread;
1357 struct proc *p = td->td_proc;
1361 args->sysmsg_result = p->p_ucred->cr_ruid;
1365 #endif /*!__alpha__*/
1368 linux_getsid(struct linux_getsid_args *args)
1370 struct getsid_args bsd;
1373 bsd.sysmsg_result = 0;
1374 bsd.pid = args->pid;
1375 error = getsid(&bsd);
1376 args->sysmsg_result = bsd.sysmsg_result;