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.34 2007/01/01 23:51:17 corecode 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>
58 #include <sys/thread2.h>
62 #include <vm/vm_kern.h>
63 #include <vm/vm_map.h>
64 #include <vm/vm_extern.h>
65 #include <vm/vm_object.h>
66 #include <vm/vm_zone.h>
67 #include <vm/swap_pager.h>
69 #include <machine/frame.h>
70 #include <machine/limits.h>
71 #include <machine/psl.h>
72 #include <machine/sysarch.h>
74 #include <machine/segments.h>
77 #include <emulation/posix4/sched.h>
79 #include <emulation/linux/linux_sysproto.h>
80 #include <arch_linux/linux.h>
81 #include <arch_linux/linux_proto.h>
82 #include "linux_mib.h"
83 #include "linux_util.h"
85 #define BSD_TO_LINUX_SIGNAL(sig) \
86 (((sig) <= LINUX_SIGTBLSZ) ? bsd_to_linux_signal[_SIG_IDX(sig)] : sig)
88 static unsigned int linux_to_bsd_resource
[LINUX_RLIM_NLIMITS
] = {
89 RLIMIT_CPU
, RLIMIT_FSIZE
, RLIMIT_DATA
, RLIMIT_STACK
,
90 RLIMIT_CORE
, RLIMIT_RSS
, RLIMIT_NPROC
, RLIMIT_NOFILE
,
95 l_long uptime
; /* Seconds since boot */
96 l_ulong loads
[3]; /* 1, 5, and 15 minute load averages */
97 l_ulong totalram
; /* Total usable main memory size */
98 l_ulong freeram
; /* Available memory size */
99 l_ulong sharedram
; /* Amount of shared memory */
100 l_ulong bufferram
; /* Memory used by buffers */
101 l_ulong totalswap
; /* Total swap space size */
102 l_ulong freeswap
; /* swap space still available */
103 l_ushort procs
; /* Number of current processes */
104 char _f
[22]; /* Pads structure to 64 bytes */
108 sys_linux_sysinfo(struct linux_sysinfo_args
*args
)
110 struct l_sysinfo sysinfo
;
115 /* Uptime is copied out of print_uptime() in kern_shutdown.c */
118 if (ts
.tv_sec
>= 86400) {
122 if (i
|| ts
.tv_sec
>= 3600) {
126 if (i
|| ts
.tv_sec
>= 60) {
130 sysinfo
.uptime
=ts
.tv_sec
;
132 /* Use the information from the mib to get our load averages */
133 for (i
= 0; i
< 3; i
++)
134 sysinfo
.loads
[i
] = averunnable
.ldavg
[i
];
136 sysinfo
.totalram
= physmem
* PAGE_SIZE
;
137 sysinfo
.freeram
= sysinfo
.totalram
- vmstats
.v_wire_count
* PAGE_SIZE
;
139 sysinfo
.sharedram
= 0;
140 for (object
= TAILQ_FIRST(&vm_object_list
); object
!= NULL
;
141 object
= TAILQ_NEXT(object
, object_list
))
142 if (object
->shadow_count
> 1)
143 sysinfo
.sharedram
+= object
->resident_page_count
;
145 sysinfo
.sharedram
*= PAGE_SIZE
;
146 sysinfo
.bufferram
= 0;
148 if (swapblist
== NULL
) {
149 sysinfo
.totalswap
= 0;
150 sysinfo
.freeswap
= 0;
152 sysinfo
.totalswap
= swapblist
->bl_blocks
* 1024;
153 sysinfo
.freeswap
= swapblist
->bl_root
->u
.bmu_avail
* PAGE_SIZE
;
156 sysinfo
.procs
= 20; /* Hack */
158 return copyout(&sysinfo
, (caddr_t
)args
->info
, sizeof(sysinfo
));
162 sys_linux_alarm(struct linux_alarm_args
*args
)
164 struct thread
*td
= curthread
;
165 struct proc
*p
= td
->td_proc
;
166 struct itimerval it
, old_it
;
173 kprintf(ARGS(alarm
, "%u"), args
->secs
);
176 if (args
->secs
> 100000000)
179 it
.it_value
.tv_sec
= (long)args
->secs
;
180 it
.it_value
.tv_usec
= 0;
181 it
.it_interval
.tv_sec
= 0;
182 it
.it_interval
.tv_usec
= 0;
184 old_it
= p
->p_realtimer
;
186 if (timevalisset(&old_it
.it_value
))
187 callout_stop(&p
->p_ithandle
);
188 if (it
.it_value
.tv_sec
!= 0) {
189 callout_reset(&p
->p_ithandle
, tvtohz_high(&it
.it_value
),
191 timevaladd(&it
.it_value
, &tv
);
195 if (timevalcmp(&old_it
.it_value
, &tv
, >)) {
196 timevalsub(&old_it
.it_value
, &tv
);
197 if (old_it
.it_value
.tv_usec
!= 0)
198 old_it
.it_value
.tv_sec
++;
199 args
->sysmsg_result
= old_it
.it_value
.tv_sec
;
205 sys_linux_brk(struct linux_brk_args
*args
)
207 struct thread
*td
= curthread
;
208 struct proc
*p
= td
->td_proc
;
210 vm_offset_t
new, old
;
211 struct obreak_args bsd_args
;
217 kprintf(ARGS(brk
, "%p"), (void *)args
->dsend
);
219 old
= (vm_offset_t
)vm
->vm_daddr
+ ctob(vm
->vm_dsize
);
220 new = (vm_offset_t
)args
->dsend
;
221 bsd_args
.sysmsg_result
= 0;
222 bsd_args
.nsize
= (char *) new;
223 bsd_args
.sysmsg_result
= 0;
224 if (((caddr_t
)new > vm
->vm_daddr
) && !sys_obreak(&bsd_args
))
225 args
->sysmsg_result
= (long)new;
227 args
->sysmsg_result
= (long)old
;
233 sys_linux_uselib(struct linux_uselib_args
*args
)
235 struct thread
*td
= curthread
;
237 struct nlookupdata nd
;
242 unsigned long file_offset
;
244 unsigned long bss_size
;
249 KKASSERT(td
->td_proc
);
252 error
= linux_copyin_path(args
->library
, &path
, LINUX_PATH_EXISTS
);
257 kprintf(ARGS(uselib
, "%s"), path
);
264 error
= nlookup_init(&nd
, path
, UIO_SYSSPACE
, NLC_FOLLOW
);
266 error
= nlookup(&nd
);
268 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
272 * From here on down, we have a locked vnode that must be unlocked.
277 if (vp
->v_writecount
) {
283 error
= VOP_GETATTR(vp
, &attr
);
287 if ((vp
->v_mount
->mnt_flag
& MNT_NOEXEC
) ||
288 ((attr
.va_mode
& 0111) == 0) || (attr
.va_type
!= VREG
)) {
294 if (attr
.va_size
== 0) {
299 /* Can we access it? */
300 error
= VOP_ACCESS(vp
, VEXEC
, p
->p_ucred
);
304 error
= VOP_OPEN(vp
, FREAD
, p
->p_ucred
, NULL
);
309 * Lock no longer needed
314 /* Pull in executable header into kernel_map */
315 error
= vm_mmap(&kernel_map
, (vm_offset_t
*)&a_out
, PAGE_SIZE
,
316 VM_PROT_READ
, VM_PROT_READ
, 0, (caddr_t
)vp
, 0);
320 /* Is it a Linux binary ? */
321 if (((a_out
->a_magic
>> 16) & 0xff) != 0x64) {
327 * While we are here, we should REALLY do some more checks
330 /* Set file/virtual offset based on a.out variant. */
331 switch ((int)(a_out
->a_magic
& 0xffff)) {
332 case 0413: /* ZMAGIC */
335 case 0314: /* QMAGIC */
343 bss_size
= round_page(a_out
->a_bss
);
345 /* Check various fields in header for validity/bounds. */
346 if (a_out
->a_text
& PAGE_MASK
|| a_out
->a_data
& PAGE_MASK
) {
351 /* text + data can't exceed file size */
352 if (a_out
->a_data
+ a_out
->a_text
> attr
.va_size
) {
358 * text/data/bss must not exceed limits
359 * XXX - this is not complete. it should check current usage PLUS
360 * the resources needed by this library.
362 if (a_out
->a_text
> maxtsiz
||
363 a_out
->a_data
+ bss_size
> p
->p_rlimit
[RLIMIT_DATA
].rlim_cur
) {
368 /* prevent more writers */
372 * Check if file_offset page aligned. Currently we cannot handle
373 * misalinged file offsets, and so we read in the entire image
376 if (file_offset
& PAGE_MASK
) {
378 kprintf("uselib: Non page aligned binary %lu\n", file_offset
);
380 /* Map text+data read/write/execute */
382 /* a_entry is the load address and is page aligned */
383 vmaddr
= trunc_page(a_out
->a_entry
);
385 /* get anon user mapping, read+write+execute */
386 error
= vm_map_find(&p
->p_vmspace
->vm_map
, NULL
, 0,
387 &vmaddr
, a_out
->a_text
+ a_out
->a_data
,
390 VM_PROT_ALL
, 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 kprintf("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 kprintf("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,
447 VM_PROT_ALL
, VM_PROT_ALL
,
454 /* Unlock/release vnode */
460 /* Release the kernel mapping. */
462 vm_map_remove(&kernel_map
, (vm_offset_t
)a_out
,
463 (vm_offset_t
)a_out
+ PAGE_SIZE
);
466 linux_free_path(&path
);
471 sys_linux_select(struct linux_select_args
*args
)
473 struct select_args bsa
;
474 struct timeval tv0
, tv1
, utv
, *tvp
;
480 kprintf(ARGS(select
, "%d, %p, %p, %p, %p"), args
->nfds
,
481 (void *)args
->readfds
, (void *)args
->writefds
,
482 (void *)args
->exceptfds
, (void *)args
->timeout
);
486 bsa
.sysmsg_result
= 0;
488 bsa
.in
= args
->readfds
;
489 bsa
.ou
= args
->writefds
;
490 bsa
.ex
= args
->exceptfds
;
491 bsa
.tv
= (struct timeval
*)args
->timeout
;
494 * Store current time for computation of the amount of
498 if ((error
= copyin((caddr_t
)args
->timeout
, &utv
,
503 kprintf(LMSG("incoming timeout (%ld/%ld)"),
504 utv
.tv_sec
, utv
.tv_usec
);
507 if (itimerfix(&utv
)) {
509 * The timeval was invalid. Convert it to something
510 * valid that will act as it does under Linux.
512 sg
= stackgap_init();
513 tvp
= stackgap_alloc(&sg
, sizeof(utv
));
514 utv
.tv_sec
+= utv
.tv_usec
/ 1000000;
515 utv
.tv_usec
%= 1000000;
516 if (utv
.tv_usec
< 0) {
518 utv
.tv_usec
+= 1000000;
522 if ((error
= copyout(&utv
, tvp
, sizeof(utv
))))
529 error
= sys_select(&bsa
);
530 args
->sysmsg_result
= bsa
.sysmsg_result
;
533 kprintf(LMSG("real select returns %d"), error
);
537 * See fs/select.c in the Linux kernel. Without this,
538 * Maelstrom doesn't work.
540 if (error
== ERESTART
)
546 if (args
->sysmsg_result
) {
548 * Compute how much time was left of the timeout,
549 * by subtracting the current time and the time
550 * before we started the call, and subtracting
551 * that result from the user-supplied value.
554 timevalsub(&tv1
, &tv0
);
555 timevalsub(&utv
, &tv1
);
562 kprintf(LMSG("outgoing timeout (%ld/%ld)"),
563 utv
.tv_sec
, utv
.tv_usec
);
565 if ((error
= copyout(&utv
, (caddr_t
)args
->timeout
,
573 kprintf(LMSG("select_out -> %d"), error
);
579 sys_linux_mremap(struct linux_mremap_args
*args
)
581 struct munmap_args bsd_args
;
586 kprintf(ARGS(mremap
, "%p, %08lx, %08lx, %08lx"),
588 (unsigned long)args
->old_len
,
589 (unsigned long)args
->new_len
,
590 (unsigned long)args
->flags
);
592 args
->new_len
= round_page(args
->new_len
);
593 args
->old_len
= round_page(args
->old_len
);
595 if (args
->new_len
> args
->old_len
) {
596 args
->sysmsg_result
= 0;
600 if (args
->new_len
< args
->old_len
) {
601 bsd_args
.sysmsg_result
= 0;
602 bsd_args
.addr
= (caddr_t
)(args
->addr
+ args
->new_len
);
603 bsd_args
.len
= args
->old_len
- args
->new_len
;
604 error
= sys_munmap(&bsd_args
);
607 args
->sysmsg_resultp
= error
? NULL
: (void *)args
->addr
;
611 #define LINUX_MS_ASYNC 0x0001
612 #define LINUX_MS_INVALIDATE 0x0002
613 #define LINUX_MS_SYNC 0x0004
616 sys_linux_msync(struct linux_msync_args
*args
)
618 struct msync_args bsd_args
;
621 bsd_args
.addr
= (caddr_t
)args
->addr
;
622 bsd_args
.len
= args
->len
;
623 bsd_args
.flags
= args
->fl
& ~LINUX_MS_SYNC
;
624 bsd_args
.sysmsg_result
= 0;
626 error
= sys_msync(&bsd_args
);
627 args
->sysmsg_result
= bsd_args
.sysmsg_result
;
632 sys_linux_time(struct linux_time_args
*args
)
640 kprintf(ARGS(time
, "*"));
645 if (args
->tm
&& (error
= copyout(&tm
, (caddr_t
)args
->tm
, sizeof(tm
))))
647 args
->sysmsg_lresult
= tm
;
651 struct l_times_argv
{
658 #define CLK_TCK 100 /* Linux uses 100 */
660 #define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
663 sys_linux_times(struct linux_times_args
*args
)
665 struct thread
*td
= curthread
;
666 struct proc
*p
= td
->td_proc
;
668 struct l_times_argv tms
;
675 kprintf(ARGS(times
, "*"));
680 tms
.tms_utime
= CONVTCK(ru
.ru_utime
);
681 tms
.tms_stime
= CONVTCK(ru
.ru_stime
);
683 tms
.tms_cutime
= CONVTCK(p
->p_cru
.ru_utime
);
684 tms
.tms_cstime
= CONVTCK(p
->p_cru
.ru_stime
);
686 if ((error
= copyout(&tms
, (caddr_t
)args
->buf
, sizeof(tms
))))
690 args
->sysmsg_result
= (int)CONVTCK(tv
);
695 sys_linux_newuname(struct linux_newuname_args
*args
)
697 struct thread
*td
= curthread
;
698 struct l_new_utsname utsname
;
699 char *osrelease
, *osname
;
702 if (ldebug(newuname
))
703 kprintf(ARGS(newuname
, "*"));
706 osname
= linux_get_osname(td
);
707 osrelease
= linux_get_osrelease(td
);
709 bzero(&utsname
, sizeof(utsname
));
710 strncpy(utsname
.sysname
, osname
, LINUX_MAX_UTSNAME
-1);
711 strncpy(utsname
.nodename
, hostname
, LINUX_MAX_UTSNAME
-1);
712 strncpy(utsname
.release
, osrelease
, LINUX_MAX_UTSNAME
-1);
713 strncpy(utsname
.version
, version
, LINUX_MAX_UTSNAME
-1);
714 strncpy(utsname
.machine
, machine
, LINUX_MAX_UTSNAME
-1);
715 strncpy(utsname
.domainname
, domainname
, LINUX_MAX_UTSNAME
-1);
717 return (copyout(&utsname
, (caddr_t
)args
->buf
, sizeof(utsname
)));
720 #if defined(__i386__)
727 sys_linux_utime(struct linux_utime_args
*args
)
729 struct timeval tv
[2];
730 struct l_utimbuf lut
;
731 struct nlookupdata nd
;
735 error
= linux_copyin_path(args
->fname
, &path
, LINUX_PATH_EXISTS
);
740 kprintf(ARGS(utime
, "%s, *"), path
);
744 error
= copyin(args
->times
, &lut
, sizeof(lut
));
747 tv
[0].tv_sec
= lut
.l_actime
;
749 tv
[1].tv_sec
= lut
.l_modtime
;
752 error
= nlookup_init(&nd
, path
, UIO_SYSSPACE
, NLC_FOLLOW
);
754 error
= kern_utimes(&nd
, args
->times
? tv
: NULL
);
757 linux_free_path(&path
);
760 #endif /* __i386__ */
762 #define __WCLONE 0x80000000
765 sys_linux_waitpid(struct linux_waitpid_args
*args
)
767 int error
, options
, status
;
771 kprintf(ARGS(waitpid
, "%d, %p, %d"),
772 args
->pid
, (void *)args
->status
, args
->options
);
774 options
= args
->options
& (WNOHANG
| WUNTRACED
);
775 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
776 if (args
->options
& __WCLONE
)
777 options
|= WLINUXCLONE
;
779 error
= kern_wait(args
->pid
, args
->status
? &status
: NULL
, options
,
780 NULL
, &args
->sysmsg_result
);
782 if (error
== 0 && args
->status
) {
784 if (WIFSIGNALED(status
))
785 status
= (status
& 0xffffff80) |
786 BSD_TO_LINUX_SIGNAL(WTERMSIG(status
));
787 else if (WIFSTOPPED(status
))
788 status
= (status
& 0xffff00ff) |
789 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(status
)) << 8);
790 error
= copyout(&status
, args
->status
, sizeof(status
));
797 sys_linux_wait4(struct linux_wait4_args
*args
)
799 struct thread
*td
= curthread
;
800 struct proc
*p
= td
->td_proc
;
801 struct rusage rusage
;
802 int error
, options
, status
;
808 kprintf(ARGS(wait4
, "%d, %p, %d, %p"),
809 args
->pid
, (void *)args
->status
, args
->options
,
810 (void *)args
->rusage
);
812 options
= args
->options
& (WNOHANG
| WUNTRACED
);
813 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
814 if (args
->options
& __WCLONE
)
815 options
|= WLINUXCLONE
;
817 error
= kern_wait(args
->pid
, args
->status
? &status
: NULL
, options
,
818 args
->rusage
? &rusage
: NULL
, &args
->sysmsg_result
);
821 SIGDELSET(p
->p_siglist
, SIGCHLD
);
823 if (error
== 0 && args
->status
) {
825 if (WIFSIGNALED(status
))
826 status
= (status
& 0xffffff80) |
827 BSD_TO_LINUX_SIGNAL(WTERMSIG(status
));
828 else if (WIFSTOPPED(status
))
829 status
= (status
& 0xffff00ff) |
830 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(status
)) << 8);
831 error
= copyout(&status
, args
->status
, sizeof(status
));
833 if (error
== 0 && args
->rusage
)
834 error
= copyout(&rusage
, args
->rusage
, sizeof(rusage
));
840 sys_linux_mknod(struct linux_mknod_args
*args
)
842 struct nlookupdata nd
;
846 error
= linux_copyin_path(args
->path
, &path
, LINUX_PATH_CREATE
);
851 kprintf(ARGS(mknod
, "%s, %d, %d"),
852 path
, args
->mode
, args
->dev
);
854 error
= nlookup_init(&nd
, path
, UIO_SYSSPACE
, 0);
856 if (args
->mode
& S_IFIFO
) {
857 error
= kern_mkfifo(&nd
, args
->mode
);
859 error
= kern_mknod(&nd
, args
->mode
, args
->dev
);
864 linux_free_path(&path
);
869 * UGH! This is just about the dumbest idea I've ever heard!!
872 sys_linux_personality(struct linux_personality_args
*args
)
875 if (ldebug(personality
))
876 kprintf(ARGS(personality
, "%d"), args
->per
);
881 /* Yes Jim, it's still a Linux... */
882 args
->sysmsg_result
= 0;
887 * Wrappers for get/setitimer for debugging..
890 sys_linux_setitimer(struct linux_setitimer_args
*args
)
892 struct setitimer_args bsa
;
893 struct itimerval foo
;
897 if (ldebug(setitimer
))
898 kprintf(ARGS(setitimer
, "%p, %p"),
899 (void *)args
->itv
, (void *)args
->oitv
);
901 bsa
.which
= args
->which
;
902 bsa
.itv
= (struct itimerval
*)args
->itv
;
903 bsa
.oitv
= (struct itimerval
*)args
->oitv
;
904 bsa
.sysmsg_result
= 0;
906 if ((error
= copyin((caddr_t
)args
->itv
, &foo
, sizeof(foo
))))
909 if (ldebug(setitimer
)) {
910 kprintf("setitimer: value: sec: %ld, usec: %ld\n",
911 foo
.it_value
.tv_sec
, foo
.it_value
.tv_usec
);
912 kprintf("setitimer: interval: sec: %ld, usec: %ld\n",
913 foo
.it_interval
.tv_sec
, foo
.it_interval
.tv_usec
);
917 error
= sys_setitimer(&bsa
);
918 args
->sysmsg_result
= bsa
.sysmsg_result
;
923 sys_linux_getitimer(struct linux_getitimer_args
*args
)
925 struct getitimer_args bsa
;
928 if (ldebug(getitimer
))
929 kprintf(ARGS(getitimer
, "%p"), (void *)args
->itv
);
931 bsa
.which
= args
->which
;
932 bsa
.itv
= (struct itimerval
*)args
->itv
;
933 bsa
.sysmsg_result
= 0;
934 error
= sys_getitimer(&bsa
);
935 args
->sysmsg_result
= bsa
.sysmsg_result
;
940 sys_linux_nice(struct linux_nice_args
*args
)
942 struct setpriority_args bsd_args
;
945 bsd_args
.which
= PRIO_PROCESS
;
946 bsd_args
.who
= 0; /* current process */
947 bsd_args
.prio
= args
->inc
;
948 bsd_args
.sysmsg_result
= 0;
949 error
= sys_setpriority(&bsd_args
);
950 args
->sysmsg_result
= bsd_args
.sysmsg_result
;
955 sys_linux_setgroups(struct linux_setgroups_args
*args
)
957 struct thread
*td
= curthread
;
958 struct proc
*p
= td
->td_proc
;
959 struct ucred
*newcred
, *oldcred
;
960 l_gid_t linux_gidset
[NGROUPS
];
966 ngrp
= args
->gidsetsize
;
967 oldcred
= p
->p_ucred
;
970 * cr_groups[0] holds egid. Setting the whole set from
971 * the supplied set will cause egid to be changed too.
972 * Keep cr_groups[0] unchanged to prevent that.
975 if ((error
= suser_cred(oldcred
, PRISON_ROOT
)) != 0)
981 newcred
= crdup(oldcred
);
983 error
= copyin((caddr_t
)args
->grouplist
, linux_gidset
,
984 ngrp
* sizeof(l_gid_t
));
988 newcred
->cr_ngroups
= ngrp
+ 1;
990 bsd_gidset
= newcred
->cr_groups
;
993 bsd_gidset
[ngrp
+ 1] = linux_gidset
[ngrp
];
997 newcred
->cr_ngroups
= 1;
1001 p
->p_ucred
= newcred
;
1007 sys_linux_getgroups(struct linux_getgroups_args
*args
)
1009 struct thread
*td
= curthread
;
1010 struct proc
*p
= td
->td_proc
;
1012 l_gid_t linux_gidset
[NGROUPS
];
1014 int bsd_gidsetsz
, ngrp
, error
;
1019 bsd_gidset
= cred
->cr_groups
;
1020 bsd_gidsetsz
= cred
->cr_ngroups
- 1;
1023 * cr_groups[0] holds egid. Returning the whole set
1024 * here will cause a duplicate. Exclude cr_groups[0]
1028 if ((ngrp
= args
->gidsetsize
) == 0) {
1029 args
->sysmsg_result
= bsd_gidsetsz
;
1033 if (ngrp
< bsd_gidsetsz
)
1037 while (ngrp
< bsd_gidsetsz
) {
1038 linux_gidset
[ngrp
] = bsd_gidset
[ngrp
+ 1];
1042 if ((error
= copyout(linux_gidset
, (caddr_t
)args
->grouplist
,
1043 ngrp
* sizeof(l_gid_t
))))
1046 args
->sysmsg_result
= ngrp
;
1051 sys_linux_setrlimit(struct linux_setrlimit_args
*args
)
1053 struct l_rlimit linux_rlim
;
1059 if (ldebug(setrlimit
))
1060 kprintf(ARGS(setrlimit
, "%d, %p"),
1061 args
->resource
, (void *)args
->rlim
);
1063 if (args
->resource
>= LINUX_RLIM_NLIMITS
)
1065 which
= linux_to_bsd_resource
[args
->resource
];
1069 error
= copyin(args
->rlim
, &linux_rlim
, sizeof(linux_rlim
));
1072 rlim
.rlim_cur
= (rlim_t
)linux_rlim
.rlim_cur
;
1073 rlim
.rlim_max
= (rlim_t
)linux_rlim
.rlim_max
;
1075 error
= kern_setrlimit(which
, &rlim
);
1081 sys_linux_old_getrlimit(struct linux_old_getrlimit_args
*args
)
1083 struct l_rlimit linux_rlim
;
1089 if (ldebug(old_getrlimit
))
1090 kprintf(ARGS(old_getrlimit
, "%d, %p"),
1091 args
->resource
, (void *)args
->rlim
);
1093 if (args
->resource
>= LINUX_RLIM_NLIMITS
)
1095 which
= linux_to_bsd_resource
[args
->resource
];
1099 error
= kern_getrlimit(which
, &rlim
);
1102 linux_rlim
.rlim_cur
= (l_ulong
)rlim
.rlim_cur
;
1103 if (linux_rlim
.rlim_cur
== ULONG_MAX
)
1104 linux_rlim
.rlim_cur
= LONG_MAX
;
1105 linux_rlim
.rlim_max
= (l_ulong
)rlim
.rlim_max
;
1106 if (linux_rlim
.rlim_max
== ULONG_MAX
)
1107 linux_rlim
.rlim_max
= LONG_MAX
;
1108 error
= copyout(&linux_rlim
, args
->rlim
, sizeof(linux_rlim
));
1114 sys_linux_getrlimit(struct linux_getrlimit_args
*args
)
1116 struct l_rlimit linux_rlim
;
1122 if (ldebug(getrlimit
))
1123 kprintf(ARGS(getrlimit
, "%d, %p"),
1124 args
->resource
, (void *)args
->rlim
);
1126 if (args
->resource
>= LINUX_RLIM_NLIMITS
)
1128 which
= linux_to_bsd_resource
[args
->resource
];
1132 error
= kern_getrlimit(which
, &rlim
);
1135 linux_rlim
.rlim_cur
= (l_ulong
)rlim
.rlim_cur
;
1136 linux_rlim
.rlim_max
= (l_ulong
)rlim
.rlim_max
;
1137 error
= copyout(&linux_rlim
, args
->rlim
, sizeof(linux_rlim
));
1143 sys_linux_sched_setscheduler(struct linux_sched_setscheduler_args
*args
)
1145 struct sched_setscheduler_args bsd
;
1149 if (ldebug(sched_setscheduler
))
1150 kprintf(ARGS(sched_setscheduler
, "%d, %d, %p"),
1151 args
->pid
, args
->policy
, (const void *)args
->param
);
1154 switch (args
->policy
) {
1155 case LINUX_SCHED_OTHER
:
1156 bsd
.policy
= SCHED_OTHER
;
1158 case LINUX_SCHED_FIFO
:
1159 bsd
.policy
= SCHED_FIFO
;
1161 case LINUX_SCHED_RR
:
1162 bsd
.policy
= SCHED_RR
;
1168 bsd
.pid
= args
->pid
;
1169 bsd
.param
= (struct sched_param
*)args
->param
;
1170 bsd
.sysmsg_result
= 0;
1172 error
= sys_sched_setscheduler(&bsd
);
1173 args
->sysmsg_result
= bsd
.sysmsg_result
;
1178 sys_linux_sched_getscheduler(struct linux_sched_getscheduler_args
*args
)
1180 struct sched_getscheduler_args bsd
;
1184 if (ldebug(sched_getscheduler
))
1185 kprintf(ARGS(sched_getscheduler
, "%d"), args
->pid
);
1188 bsd
.sysmsg_result
= 0;
1189 bsd
.pid
= args
->pid
;
1190 error
= sys_sched_getscheduler(&bsd
);
1191 args
->sysmsg_result
= bsd
.sysmsg_result
;
1193 switch (args
->sysmsg_result
) {
1195 args
->sysmsg_result
= LINUX_SCHED_OTHER
;
1198 args
->sysmsg_result
= LINUX_SCHED_FIFO
;
1201 args
->sysmsg_result
= LINUX_SCHED_RR
;
1208 sys_linux_sched_get_priority_max(struct linux_sched_get_priority_max_args
*args
)
1210 struct sched_get_priority_max_args bsd
;
1214 if (ldebug(sched_get_priority_max
))
1215 kprintf(ARGS(sched_get_priority_max
, "%d"), args
->policy
);
1218 switch (args
->policy
) {
1219 case LINUX_SCHED_OTHER
:
1220 bsd
.policy
= SCHED_OTHER
;
1222 case LINUX_SCHED_FIFO
:
1223 bsd
.policy
= SCHED_FIFO
;
1225 case LINUX_SCHED_RR
:
1226 bsd
.policy
= SCHED_RR
;
1231 bsd
.sysmsg_result
= 0;
1233 error
= sys_sched_get_priority_max(&bsd
);
1234 args
->sysmsg_result
= bsd
.sysmsg_result
;
1239 sys_linux_sched_get_priority_min(struct linux_sched_get_priority_min_args
*args
)
1241 struct sched_get_priority_min_args bsd
;
1245 if (ldebug(sched_get_priority_min
))
1246 kprintf(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 bsd
.sysmsg_result
= 0;
1264 error
= sys_sched_get_priority_min(&bsd
);
1265 args
->sysmsg_result
= bsd
.sysmsg_result
;
1269 #define REBOOT_CAD_ON 0x89abcdef
1270 #define REBOOT_CAD_OFF 0
1271 #define REBOOT_HALT 0xcdef0123
1274 sys_linux_reboot(struct linux_reboot_args
*args
)
1276 struct reboot_args bsd_args
;
1281 kprintf(ARGS(reboot
, "0x%x"), args
->cmd
);
1283 if (args
->cmd
== REBOOT_CAD_ON
|| args
->cmd
== REBOOT_CAD_OFF
)
1285 bsd_args
.opt
= (args
->cmd
== REBOOT_HALT
) ? RB_HALT
: 0;
1286 bsd_args
.sysmsg_result
= 0;
1288 error
= sys_reboot(&bsd_args
);
1289 args
->sysmsg_result
= bsd_args
.sysmsg_result
;
1294 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1295 * p->p_retval[1] when COMPAT_43 or COMPAT_SUNOS is defined. This
1296 * globbers registers that are assumed to be preserved. The following
1297 * lightweight syscalls fixes this. See also linux_getgid16() and
1298 * linux_getuid16() in linux_uid16.c.
1300 * linux_getpid() - MP SAFE
1301 * linux_getgid() - MP SAFE
1302 * linux_getuid() - MP SAFE
1306 sys_linux_getpid(struct linux_getpid_args
*args
)
1308 struct thread
*td
= curthread
;
1309 struct proc
*p
= td
->td_proc
;
1313 args
->sysmsg_result
= p
->p_pid
;
1318 sys_linux_getgid(struct linux_getgid_args
*args
)
1320 struct thread
*td
= curthread
;
1321 struct proc
*p
= td
->td_proc
;
1325 args
->sysmsg_result
= p
->p_ucred
->cr_rgid
;
1330 sys_linux_getuid(struct linux_getuid_args
*args
)
1332 struct thread
*td
= curthread
;
1333 struct proc
*p
= td
->td_proc
;
1337 args
->sysmsg_result
= p
->p_ucred
->cr_ruid
;
1342 sys_linux_getsid(struct linux_getsid_args
*args
)
1344 struct getsid_args bsd
;
1347 bsd
.sysmsg_result
= 0;
1348 bsd
.pid
= args
->pid
;
1349 error
= sys_getsid(&bsd
);
1350 args
->sysmsg_result
= bsd
.sysmsg_result
;
1355 linux_nosys(struct nosys_args
*args
)