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.39 2007/06/26 19:31:03 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>
46 #include <sys/nlookup.h>
47 #include <sys/blist.h>
48 #include <sys/reboot.h>
49 #include <sys/resourcevar.h>
50 #include <sys/signalvar.h>
52 #include <sys/sysctl.h>
53 #include <sys/sysproto.h>
55 #include <sys/unistd.h>
56 #include <sys/vmmeter.h>
57 #include <sys/vnode.h>
60 #include <sys/signal2.h>
61 #include <sys/thread2.h>
62 #include <sys/mplock2.h>
66 #include <vm/vm_kern.h>
67 #include <vm/vm_map.h>
68 #include <vm/vm_extern.h>
69 #include <vm/vm_object.h>
70 #include <vm/vm_zone.h>
71 #include <vm/swap_pager.h>
73 #include <machine/frame.h>
74 #include <machine/limits.h>
75 #include <machine/psl.h>
76 #include <machine/sysarch.h>
78 #include <machine/segments.h>
81 #include <sys/sched.h>
83 #include <emulation/linux/linux_sysproto.h>
84 #include <arch_linux/linux.h>
85 #include <arch_linux/linux_proto.h>
86 #include "linux_mib.h"
87 #include "linux_util.h"
89 #define BSD_TO_LINUX_SIGNAL(sig) \
90 (((sig) <= LINUX_SIGTBLSZ) ? bsd_to_linux_signal[_SIG_IDX(sig)] : sig)
92 static unsigned int linux_to_bsd_resource
[LINUX_RLIM_NLIMITS
] = {
93 RLIMIT_CPU
, RLIMIT_FSIZE
, RLIMIT_DATA
, RLIMIT_STACK
,
94 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 */
115 sys_linux_sysinfo(struct linux_sysinfo_args
*args
)
117 struct l_sysinfo sysinfo
;
123 /* Uptime is copied out of print_uptime() in kern_shutdown.c */
126 if (ts
.tv_sec
>= 86400) {
130 if (i
|| ts
.tv_sec
>= 3600) {
134 if (i
|| ts
.tv_sec
>= 60) {
138 sysinfo
.uptime
=ts
.tv_sec
;
140 /* Use the information from the mib to get our load averages */
141 for (i
= 0; i
< 3; i
++)
142 sysinfo
.loads
[i
] = averunnable
.ldavg
[i
];
144 sysinfo
.totalram
= Maxmem
* PAGE_SIZE
;
145 sysinfo
.freeram
= sysinfo
.totalram
- vmstats
.v_wire_count
* PAGE_SIZE
;
148 sysinfo
.sharedram
= 0;
149 for (object
= TAILQ_FIRST(&vm_object_list
); object
!= NULL
;
150 object
= TAILQ_NEXT(object
, object_list
))
151 if (object
->shadow_count
> 1)
152 sysinfo
.sharedram
+= object
->resident_page_count
;
154 sysinfo
.sharedram
*= PAGE_SIZE
;
155 sysinfo
.bufferram
= 0;
157 if (swapblist
== NULL
) {
158 sysinfo
.totalswap
= 0;
159 sysinfo
.freeswap
= 0;
161 sysinfo
.totalswap
= swapblist
->bl_blocks
* 1024;
162 sysinfo
.freeswap
= swapblist
->bl_root
->u
.bmu_avail
* PAGE_SIZE
;
166 sysinfo
.procs
= 20; /* Hack */
168 error
= copyout(&sysinfo
, (caddr_t
)args
->info
, sizeof(sysinfo
));
176 sys_linux_alarm(struct linux_alarm_args
*args
)
178 struct thread
*td
= curthread
;
179 struct proc
*p
= td
->td_proc
;
180 struct itimerval it
, old_it
;
185 kprintf(ARGS(alarm
, "%u"), args
->secs
);
188 if (args
->secs
> 100000000)
191 it
.it_value
.tv_sec
= (long)args
->secs
;
192 it
.it_value
.tv_usec
= 0;
193 it
.it_interval
.tv_sec
= 0;
194 it
.it_interval
.tv_usec
= 0;
197 old_it
= p
->p_realtimer
;
199 if (timevalisset(&old_it
.it_value
))
200 callout_stop(&p
->p_ithandle
);
201 if (it
.it_value
.tv_sec
!= 0) {
202 callout_reset(&p
->p_ithandle
, tvtohz_high(&it
.it_value
),
204 timevaladd(&it
.it_value
, &tv
);
209 if (timevalcmp(&old_it
.it_value
, &tv
, >)) {
210 timevalsub(&old_it
.it_value
, &tv
);
211 if (old_it
.it_value
.tv_usec
!= 0)
212 old_it
.it_value
.tv_sec
++;
213 args
->sysmsg_result
= old_it
.it_value
.tv_sec
;
222 sys_linux_brk(struct linux_brk_args
*args
)
224 struct thread
*td
= curthread
;
225 struct proc
*p
= td
->td_proc
;
227 vm_offset_t
new, old
;
228 struct obreak_args bsd_args
;
234 kprintf(ARGS(brk
, "%p"), (void *)args
->dsend
);
236 old
= (vm_offset_t
)vm
->vm_daddr
+ ctob(vm
->vm_dsize
);
237 new = (vm_offset_t
)args
->dsend
;
238 bsd_args
.sysmsg_result
= 0;
239 bsd_args
.nsize
= (char *) new;
240 bsd_args
.sysmsg_result
= 0;
241 if (((caddr_t
)new > vm
->vm_daddr
) && !sys_obreak(&bsd_args
))
242 args
->sysmsg_result
= (long)new;
244 args
->sysmsg_result
= (long)old
;
254 sys_linux_uselib(struct linux_uselib_args
*args
)
256 struct thread
*td
= curthread
;
258 struct nlookupdata nd
;
263 unsigned long file_offset
;
265 unsigned long bss_size
;
272 error
= linux_copyin_path(args
->library
, &path
, LINUX_PATH_EXISTS
);
277 kprintf(ARGS(uselib
, "%s"), path
);
285 error
= nlookup_init(&nd
, path
, UIO_SYSSPACE
, NLC_FOLLOW
);
286 nd
.nl_flags
|= NLC_EXEC
;
288 error
= nlookup(&nd
);
290 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
294 * From here on down, we have a locked vnode that must be unlocked.
299 if (vp
->v_writecount
) {
305 error
= VOP_GETATTR(vp
, &attr
);
309 if ((vp
->v_mount
->mnt_flag
& MNT_NOEXEC
) ||
310 ((attr
.va_mode
& 0111) == 0) || (attr
.va_type
!= VREG
)) {
316 if (attr
.va_size
== 0) {
321 error
= VOP_OPEN(vp
, FREAD
, td
->td_ucred
, NULL
);
326 * Lock no longer needed
331 /* Pull in executable header into kernel_map */
332 error
= vm_mmap(&kernel_map
, (vm_offset_t
*)&a_out
, PAGE_SIZE
,
333 VM_PROT_READ
, VM_PROT_READ
, 0, (caddr_t
)vp
, 0);
337 /* Is it a Linux binary ? */
338 if (((a_out
->a_magic
>> 16) & 0xff) != 0x64) {
344 * While we are here, we should REALLY do some more checks
347 /* Set file/virtual offset based on a.out variant. */
348 switch ((int)(a_out
->a_magic
& 0xffff)) {
349 case 0413: /* ZMAGIC */
352 case 0314: /* QMAGIC */
360 bss_size
= round_page(a_out
->a_bss
);
362 /* Check various fields in header for validity/bounds. */
363 if (a_out
->a_text
& PAGE_MASK
|| a_out
->a_data
& PAGE_MASK
) {
368 /* text + data can't exceed file size */
369 if (a_out
->a_data
+ a_out
->a_text
> attr
.va_size
) {
375 * text/data/bss must not exceed limits
376 * XXX - this is not complete. it should check current usage PLUS
377 * the resources needed by this library.
379 if (a_out
->a_text
> maxtsiz
||
380 a_out
->a_data
+ bss_size
> p
->p_rlimit
[RLIMIT_DATA
].rlim_cur
) {
385 /* prevent more writers */
386 vsetflags(vp
, VTEXT
);
389 * Check if file_offset page aligned. Currently we cannot handle
390 * misalinged file offsets, and so we read in the entire image
393 if (file_offset
& PAGE_MASK
) {
395 kprintf("uselib: Non page aligned binary %lu\n", file_offset
);
397 /* Map text+data read/write/execute */
399 /* a_entry is the load address and is page aligned */
400 vmaddr
= trunc_page(a_out
->a_entry
);
402 /* get anon user mapping, read+write+execute */
403 error
= vm_map_find(&p
->p_vmspace
->vm_map
, NULL
, 0,
404 &vmaddr
, a_out
->a_text
+ a_out
->a_data
,
407 VM_PROT_ALL
, VM_PROT_ALL
,
412 /* map file into kernel_map */
413 error
= vm_mmap(&kernel_map
, &buffer
,
414 round_page(a_out
->a_text
+ a_out
->a_data
+ file_offset
),
415 VM_PROT_READ
, VM_PROT_READ
, 0, (caddr_t
)vp
,
416 trunc_page(file_offset
));
420 /* copy from kernel VM space to user space */
421 error
= copyout((caddr_t
)(uintptr_t)(buffer
+ file_offset
),
422 (caddr_t
)vmaddr
, a_out
->a_text
+ a_out
->a_data
);
424 /* release temporary kernel space */
425 vm_map_remove(&kernel_map
, buffer
, buffer
+
426 round_page(a_out
->a_text
+ a_out
->a_data
+ file_offset
));
432 kprintf("uselib: Page aligned binary %lu\n", file_offset
);
435 * for QMAGIC, a_entry is 20 bytes beyond the load address
436 * to skip the executable header
438 vmaddr
= trunc_page(a_out
->a_entry
);
441 * Map it all into the process's space as a single
442 * copy-on-write "data" segment.
444 error
= vm_mmap(&p
->p_vmspace
->vm_map
, &vmaddr
,
445 a_out
->a_text
+ a_out
->a_data
, VM_PROT_ALL
, VM_PROT_ALL
,
446 MAP_PRIVATE
| MAP_FIXED
, (caddr_t
)vp
, file_offset
);
451 kprintf("mem=%08lx = %08lx %08lx\n", (long)vmaddr
, ((long*)vmaddr
)[0],
455 /* Calculate BSS start address */
456 vmaddr
= trunc_page(a_out
->a_entry
) + a_out
->a_text
+
459 /* allocate some 'anon' space */
460 error
= vm_map_find(&p
->p_vmspace
->vm_map
, NULL
, 0,
464 VM_PROT_ALL
, VM_PROT_ALL
,
471 /* Unlock/release vnode */
477 /* Release the kernel mapping. */
479 vm_map_remove(&kernel_map
, (vm_offset_t
)a_out
,
480 (vm_offset_t
)a_out
+ PAGE_SIZE
);
484 linux_free_path(&path
);
492 sys_linux_select(struct linux_select_args
*args
)
494 struct select_args bsa
;
495 struct timeval tv0
, tv1
, utv
, *tvp
;
501 kprintf(ARGS(select
, "%d, %p, %p, %p, %p"), args
->nfds
,
502 (void *)args
->readfds
, (void *)args
->writefds
,
503 (void *)args
->exceptfds
, (void *)args
->timeout
);
507 bsa
.sysmsg_result
= 0;
509 bsa
.in
= args
->readfds
;
510 bsa
.ou
= args
->writefds
;
511 bsa
.ex
= args
->exceptfds
;
512 bsa
.tv
= (struct timeval
*)args
->timeout
;
515 * Store current time for computation of the amount of
519 if ((error
= copyin((caddr_t
)args
->timeout
, &utv
,
524 kprintf(LMSG("incoming timeout (%ld/%ld)"),
525 utv
.tv_sec
, utv
.tv_usec
);
528 if (itimerfix(&utv
)) {
530 * The timeval was invalid. Convert it to something
531 * valid that will act as it does under Linux.
533 sg
= stackgap_init();
534 tvp
= stackgap_alloc(&sg
, sizeof(utv
));
535 utv
.tv_sec
+= utv
.tv_usec
/ 1000000;
536 utv
.tv_usec
%= 1000000;
537 if (utv
.tv_usec
< 0) {
539 utv
.tv_usec
+= 1000000;
543 if ((error
= copyout(&utv
, tvp
, sizeof(utv
))))
550 error
= sys_select(&bsa
);
551 args
->sysmsg_result
= bsa
.sysmsg_result
;
554 kprintf(LMSG("real select returns %d"), error
);
558 * See fs/select.c in the Linux kernel. Without this,
559 * Maelstrom doesn't work.
561 if (error
== ERESTART
)
567 if (args
->sysmsg_result
) {
569 * Compute how much time was left of the timeout,
570 * by subtracting the current time and the time
571 * before we started the call, and subtracting
572 * that result from the user-supplied value.
575 timevalsub(&tv1
, &tv0
);
576 timevalsub(&utv
, &tv1
);
583 kprintf(LMSG("outgoing timeout (%ld/%ld)"),
584 utv
.tv_sec
, utv
.tv_usec
);
586 if ((error
= copyout(&utv
, (caddr_t
)args
->timeout
,
594 kprintf(LMSG("select_out -> %d"), error
);
603 sys_linux_mremap(struct linux_mremap_args
*args
)
605 struct munmap_args bsd_args
;
610 kprintf(ARGS(mremap
, "%p, %08lx, %08lx, %08lx"),
612 (unsigned long)args
->old_len
,
613 (unsigned long)args
->new_len
,
614 (unsigned long)args
->flags
);
616 args
->new_len
= round_page(args
->new_len
);
617 args
->old_len
= round_page(args
->old_len
);
619 if (args
->new_len
> args
->old_len
) {
620 args
->sysmsg_result
= 0;
624 if (args
->new_len
< args
->old_len
) {
625 bsd_args
.sysmsg_result
= 0;
626 bsd_args
.addr
= (caddr_t
)(args
->addr
+ args
->new_len
);
627 bsd_args
.len
= args
->old_len
- args
->new_len
;
628 error
= sys_munmap(&bsd_args
);
631 args
->sysmsg_resultp
= error
? NULL
: (void *)args
->addr
;
635 #define LINUX_MS_ASYNC 0x0001
636 #define LINUX_MS_INVALIDATE 0x0002
637 #define LINUX_MS_SYNC 0x0004
643 sys_linux_msync(struct linux_msync_args
*args
)
645 struct msync_args bsd_args
;
648 bsd_args
.addr
= (caddr_t
)args
->addr
;
649 bsd_args
.len
= args
->len
;
650 bsd_args
.flags
= args
->fl
& ~LINUX_MS_SYNC
;
651 bsd_args
.sysmsg_result
= 0;
653 error
= sys_msync(&bsd_args
);
654 args
->sysmsg_result
= bsd_args
.sysmsg_result
;
662 sys_linux_time(struct linux_time_args
*args
)
670 kprintf(ARGS(time
, "*"));
675 if (args
->tm
&& (error
= copyout(&tm
, (caddr_t
)args
->tm
, sizeof(tm
))))
677 args
->sysmsg_lresult
= tm
;
681 struct l_times_argv
{
688 #define CLK_TCK 100 /* Linux uses 100 */
690 #define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
696 sys_linux_times(struct linux_times_args
*args
)
698 struct thread
*td
= curthread
;
699 struct proc
*p
= td
->td_proc
;
701 struct l_times_argv tms
;
707 kprintf(ARGS(times
, "*"));
714 tms
.tms_utime
= CONVTCK(ru
.ru_utime
);
715 tms
.tms_stime
= CONVTCK(ru
.ru_stime
);
717 tms
.tms_cutime
= CONVTCK(p
->p_cru
.ru_utime
);
718 tms
.tms_cstime
= CONVTCK(p
->p_cru
.ru_stime
);
720 if ((error
= copyout(&tms
, (caddr_t
)args
->buf
, sizeof(tms
))))
724 args
->sysmsg_result
= (int)CONVTCK(tv
);
732 sys_linux_newuname(struct linux_newuname_args
*args
)
734 struct thread
*td
= curthread
;
735 struct l_new_utsname utsname
;
736 char *osrelease
, *osname
;
739 if (ldebug(newuname
))
740 kprintf(ARGS(newuname
, "*"));
744 osname
= linux_get_osname(td
);
745 osrelease
= linux_get_osrelease(td
);
747 bzero(&utsname
, sizeof(utsname
));
748 strncpy(utsname
.sysname
, osname
, LINUX_MAX_UTSNAME
-1);
749 strncpy(utsname
.nodename
, hostname
, LINUX_MAX_UTSNAME
-1);
750 strncpy(utsname
.release
, osrelease
, LINUX_MAX_UTSNAME
-1);
751 strncpy(utsname
.version
, version
, LINUX_MAX_UTSNAME
-1);
752 strncpy(utsname
.machine
, machine
, LINUX_MAX_UTSNAME
-1);
753 strncpy(utsname
.domainname
, domainname
, LINUX_MAX_UTSNAME
-1);
756 return (copyout(&utsname
, (caddr_t
)args
->buf
, sizeof(utsname
)));
759 #if defined(__i386__)
769 sys_linux_utime(struct linux_utime_args
*args
)
771 struct timeval tv
[2];
772 struct l_utimbuf lut
;
773 struct nlookupdata nd
;
777 error
= linux_copyin_path(args
->fname
, &path
, LINUX_PATH_EXISTS
);
782 kprintf(ARGS(utime
, "%s, *"), path
);
786 error
= copyin(args
->times
, &lut
, sizeof(lut
));
789 tv
[0].tv_sec
= lut
.l_actime
;
791 tv
[1].tv_sec
= lut
.l_modtime
;
795 error
= nlookup_init(&nd
, path
, UIO_SYSSPACE
, NLC_FOLLOW
);
797 error
= kern_utimes(&nd
, args
->times
? tv
: NULL
);
801 linux_free_path(&path
);
804 #endif /* __i386__ */
806 #define __WCLONE 0x80000000
812 sys_linux_waitpid(struct linux_waitpid_args
*args
)
814 int error
, options
, status
;
818 kprintf(ARGS(waitpid
, "%d, %p, %d"),
819 args
->pid
, (void *)args
->status
, args
->options
);
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 NULL
, &args
->sysmsg_result
);
829 if (error
== 0 && args
->status
) {
831 if (WIFSIGNALED(status
))
832 status
= (status
& 0xffffff80) |
833 BSD_TO_LINUX_SIGNAL(WTERMSIG(status
));
834 else if (WIFSTOPPED(status
))
835 status
= (status
& 0xffff00ff) |
836 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(status
)) << 8);
837 error
= copyout(&status
, args
->status
, sizeof(status
));
847 sys_linux_wait4(struct linux_wait4_args
*args
)
849 struct thread
*td
= curthread
;
850 struct lwp
*lp
= td
->td_lwp
;
851 struct rusage rusage
;
852 int error
, options
, status
;
856 kprintf(ARGS(wait4
, "%d, %p, %d, %p"),
857 args
->pid
, (void *)args
->status
, args
->options
,
858 (void *)args
->rusage
);
860 options
= args
->options
& (WNOHANG
| WUNTRACED
);
861 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
862 if (args
->options
& __WCLONE
)
863 options
|= WLINUXCLONE
;
865 error
= kern_wait(args
->pid
, args
->status
? &status
: NULL
, options
,
866 args
->rusage
? &rusage
: NULL
, &args
->sysmsg_result
);
869 lwp_delsig(lp
, SIGCHLD
);
871 if (error
== 0 && args
->status
) {
873 if (WIFSIGNALED(status
))
874 status
= (status
& 0xffffff80) |
875 BSD_TO_LINUX_SIGNAL(WTERMSIG(status
));
876 else if (WIFSTOPPED(status
))
877 status
= (status
& 0xffff00ff) |
878 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(status
)) << 8);
879 error
= copyout(&status
, args
->status
, sizeof(status
));
881 if (error
== 0 && args
->rusage
)
882 error
= copyout(&rusage
, args
->rusage
, sizeof(rusage
));
891 sys_linux_mknod(struct linux_mknod_args
*args
)
893 struct nlookupdata nd
;
897 error
= linux_copyin_path(args
->path
, &path
, LINUX_PATH_CREATE
);
902 kprintf(ARGS(mknod
, "%s, %d, %d"),
903 path
, args
->mode
, args
->dev
);
906 error
= nlookup_init(&nd
, path
, UIO_SYSSPACE
, 0);
908 if (args
->mode
& S_IFIFO
) {
909 error
= kern_mkfifo(&nd
, args
->mode
);
911 error
= kern_mknod(&nd
, args
->mode
,
919 linux_free_path(&path
);
924 * UGH! This is just about the dumbest idea I've ever heard!!
929 sys_linux_personality(struct linux_personality_args
*args
)
932 if (ldebug(personality
))
933 kprintf(ARGS(personality
, "%d"), args
->per
);
938 /* Yes Jim, it's still a Linux... */
939 args
->sysmsg_result
= 0;
944 * Wrappers for get/setitimer for debugging..
949 sys_linux_setitimer(struct linux_setitimer_args
*args
)
951 struct setitimer_args bsa
;
952 struct itimerval foo
;
956 if (ldebug(setitimer
))
957 kprintf(ARGS(setitimer
, "%p, %p"),
958 (void *)args
->itv
, (void *)args
->oitv
);
960 bsa
.which
= args
->which
;
961 bsa
.itv
= (struct itimerval
*)args
->itv
;
962 bsa
.oitv
= (struct itimerval
*)args
->oitv
;
963 bsa
.sysmsg_result
= 0;
965 if ((error
= copyin((caddr_t
)args
->itv
, &foo
, sizeof(foo
))))
968 if (ldebug(setitimer
)) {
969 kprintf("setitimer: value: sec: %ld, usec: %ld\n",
970 foo
.it_value
.tv_sec
, foo
.it_value
.tv_usec
);
971 kprintf("setitimer: interval: sec: %ld, usec: %ld\n",
972 foo
.it_interval
.tv_sec
, foo
.it_interval
.tv_usec
);
976 error
= sys_setitimer(&bsa
);
977 args
->sysmsg_result
= bsa
.sysmsg_result
;
985 sys_linux_getitimer(struct linux_getitimer_args
*args
)
987 struct getitimer_args bsa
;
990 if (ldebug(getitimer
))
991 kprintf(ARGS(getitimer
, "%p"), (void *)args
->itv
);
993 bsa
.which
= args
->which
;
994 bsa
.itv
= (struct itimerval
*)args
->itv
;
995 bsa
.sysmsg_result
= 0;
996 error
= sys_getitimer(&bsa
);
997 args
->sysmsg_result
= bsa
.sysmsg_result
;
1005 sys_linux_nice(struct linux_nice_args
*args
)
1007 struct setpriority_args bsd_args
;
1010 bsd_args
.which
= PRIO_PROCESS
;
1011 bsd_args
.who
= 0; /* current process */
1012 bsd_args
.prio
= args
->inc
;
1013 bsd_args
.sysmsg_result
= 0;
1014 error
= sys_setpriority(&bsd_args
);
1015 args
->sysmsg_result
= bsd_args
.sysmsg_result
;
1023 sys_linux_setgroups(struct linux_setgroups_args
*args
)
1025 struct thread
*td
= curthread
;
1026 struct proc
*p
= td
->td_proc
;
1027 struct ucred
*newcred
, *oldcred
;
1028 l_gid_t linux_gidset
[NGROUPS
];
1032 ngrp
= args
->gidsetsize
;
1033 oldcred
= td
->td_ucred
;
1036 * cr_groups[0] holds egid. Setting the whole set from
1037 * the supplied set will cause egid to be changed too.
1038 * Keep cr_groups[0] unchanged to prevent that.
1041 if ((error
= priv_check_cred(oldcred
, PRIV_CRED_SETGROUPS
, 0)) != 0)
1044 if ((u_int
)ngrp
>= NGROUPS
)
1048 newcred
= crdup(oldcred
);
1050 error
= copyin((caddr_t
)args
->grouplist
, linux_gidset
,
1051 ngrp
* sizeof(l_gid_t
));
1058 newcred
->cr_ngroups
= ngrp
+ 1;
1060 bsd_gidset
= newcred
->cr_groups
;
1063 bsd_gidset
[ngrp
+ 1] = linux_gidset
[ngrp
];
1067 newcred
->cr_ngroups
= 1;
1071 oldcred
= p
->p_ucred
; /* reload, deal with threads race */
1072 p
->p_ucred
= newcred
;
1082 sys_linux_getgroups(struct linux_getgroups_args
*args
)
1084 struct thread
*td
= curthread
;
1086 l_gid_t linux_gidset
[NGROUPS
];
1088 int bsd_gidsetsz
, ngrp
, error
;
1090 cred
= td
->td_ucred
;
1091 bsd_gidset
= cred
->cr_groups
;
1092 bsd_gidsetsz
= cred
->cr_ngroups
- 1;
1095 * cr_groups[0] holds egid. Returning the whole set
1096 * here will cause a duplicate. Exclude cr_groups[0]
1100 if ((ngrp
= args
->gidsetsize
) == 0) {
1101 args
->sysmsg_result
= bsd_gidsetsz
;
1105 if ((u_int
)ngrp
< bsd_gidsetsz
)
1109 while (ngrp
< bsd_gidsetsz
) {
1110 linux_gidset
[ngrp
] = bsd_gidset
[ngrp
+ 1];
1114 if ((error
= copyout(linux_gidset
, args
->grouplist
,
1115 ngrp
* sizeof(l_gid_t
)))) {
1119 args
->sysmsg_result
= ngrp
;
1127 sys_linux_setrlimit(struct linux_setrlimit_args
*args
)
1129 struct l_rlimit linux_rlim
;
1135 if (ldebug(setrlimit
))
1136 kprintf(ARGS(setrlimit
, "%d, %p"),
1137 args
->resource
, (void *)args
->rlim
);
1139 if (args
->resource
>= LINUX_RLIM_NLIMITS
)
1141 which
= linux_to_bsd_resource
[args
->resource
];
1145 error
= copyin(args
->rlim
, &linux_rlim
, sizeof(linux_rlim
));
1148 rlim
.rlim_cur
= (rlim_t
)linux_rlim
.rlim_cur
;
1149 rlim
.rlim_max
= (rlim_t
)linux_rlim
.rlim_max
;
1151 error
= kern_setrlimit(which
, &rlim
);
1160 sys_linux_old_getrlimit(struct linux_old_getrlimit_args
*args
)
1162 struct l_rlimit linux_rlim
;
1168 if (ldebug(old_getrlimit
))
1169 kprintf(ARGS(old_getrlimit
, "%d, %p"),
1170 args
->resource
, (void *)args
->rlim
);
1172 if (args
->resource
>= LINUX_RLIM_NLIMITS
)
1174 which
= linux_to_bsd_resource
[args
->resource
];
1178 error
= kern_getrlimit(which
, &rlim
);
1181 linux_rlim
.rlim_cur
= (l_ulong
)rlim
.rlim_cur
;
1182 if (linux_rlim
.rlim_cur
== ULONG_MAX
)
1183 linux_rlim
.rlim_cur
= LONG_MAX
;
1184 linux_rlim
.rlim_max
= (l_ulong
)rlim
.rlim_max
;
1185 if (linux_rlim
.rlim_max
== ULONG_MAX
)
1186 linux_rlim
.rlim_max
= LONG_MAX
;
1187 error
= copyout(&linux_rlim
, args
->rlim
, sizeof(linux_rlim
));
1196 sys_linux_getrlimit(struct linux_getrlimit_args
*args
)
1198 struct l_rlimit linux_rlim
;
1204 if (ldebug(getrlimit
))
1205 kprintf(ARGS(getrlimit
, "%d, %p"),
1206 args
->resource
, (void *)args
->rlim
);
1208 if (args
->resource
>= LINUX_RLIM_NLIMITS
)
1210 which
= linux_to_bsd_resource
[args
->resource
];
1214 error
= kern_getrlimit(which
, &rlim
);
1217 linux_rlim
.rlim_cur
= (l_ulong
)rlim
.rlim_cur
;
1218 linux_rlim
.rlim_max
= (l_ulong
)rlim
.rlim_max
;
1219 error
= copyout(&linux_rlim
, args
->rlim
, sizeof(linux_rlim
));
1228 sys_linux_sched_setscheduler(struct linux_sched_setscheduler_args
*args
)
1230 struct sched_setscheduler_args bsd
;
1234 if (ldebug(sched_setscheduler
))
1235 kprintf(ARGS(sched_setscheduler
, "%d, %d, %p"),
1236 args
->pid
, args
->policy
, (const void *)args
->param
);
1239 switch (args
->policy
) {
1240 case LINUX_SCHED_OTHER
:
1241 bsd
.policy
= SCHED_OTHER
;
1243 case LINUX_SCHED_FIFO
:
1244 bsd
.policy
= SCHED_FIFO
;
1246 case LINUX_SCHED_RR
:
1247 bsd
.policy
= SCHED_RR
;
1253 bsd
.pid
= args
->pid
;
1254 bsd
.param
= (struct sched_param
*)args
->param
;
1255 bsd
.sysmsg_result
= 0;
1257 error
= sys_sched_setscheduler(&bsd
);
1258 args
->sysmsg_result
= bsd
.sysmsg_result
;
1266 sys_linux_sched_getscheduler(struct linux_sched_getscheduler_args
*args
)
1268 struct sched_getscheduler_args bsd
;
1272 if (ldebug(sched_getscheduler
))
1273 kprintf(ARGS(sched_getscheduler
, "%d"), args
->pid
);
1276 bsd
.sysmsg_result
= 0;
1277 bsd
.pid
= args
->pid
;
1278 error
= sys_sched_getscheduler(&bsd
);
1279 args
->sysmsg_result
= bsd
.sysmsg_result
;
1281 switch (args
->sysmsg_result
) {
1283 args
->sysmsg_result
= LINUX_SCHED_OTHER
;
1286 args
->sysmsg_result
= LINUX_SCHED_FIFO
;
1289 args
->sysmsg_result
= LINUX_SCHED_RR
;
1299 sys_linux_sched_get_priority_max(struct linux_sched_get_priority_max_args
*args
)
1301 struct sched_get_priority_max_args bsd
;
1305 if (ldebug(sched_get_priority_max
))
1306 kprintf(ARGS(sched_get_priority_max
, "%d"), args
->policy
);
1309 switch (args
->policy
) {
1310 case LINUX_SCHED_OTHER
:
1311 bsd
.policy
= SCHED_OTHER
;
1313 case LINUX_SCHED_FIFO
:
1314 bsd
.policy
= SCHED_FIFO
;
1316 case LINUX_SCHED_RR
:
1317 bsd
.policy
= SCHED_RR
;
1322 bsd
.sysmsg_result
= 0;
1324 error
= sys_sched_get_priority_max(&bsd
);
1325 args
->sysmsg_result
= bsd
.sysmsg_result
;
1333 sys_linux_sched_get_priority_min(struct linux_sched_get_priority_min_args
*args
)
1335 struct sched_get_priority_min_args bsd
;
1339 if (ldebug(sched_get_priority_min
))
1340 kprintf(ARGS(sched_get_priority_min
, "%d"), args
->policy
);
1343 switch (args
->policy
) {
1344 case LINUX_SCHED_OTHER
:
1345 bsd
.policy
= SCHED_OTHER
;
1347 case LINUX_SCHED_FIFO
:
1348 bsd
.policy
= SCHED_FIFO
;
1350 case LINUX_SCHED_RR
:
1351 bsd
.policy
= SCHED_RR
;
1356 bsd
.sysmsg_result
= 0;
1358 error
= sys_sched_get_priority_min(&bsd
);
1359 args
->sysmsg_result
= bsd
.sysmsg_result
;
1363 #define REBOOT_CAD_ON 0x89abcdef
1364 #define REBOOT_CAD_OFF 0
1365 #define REBOOT_HALT 0xcdef0123
1371 sys_linux_reboot(struct linux_reboot_args
*args
)
1373 struct reboot_args bsd_args
;
1378 kprintf(ARGS(reboot
, "0x%x"), args
->cmd
);
1380 if (args
->cmd
== REBOOT_CAD_ON
|| args
->cmd
== REBOOT_CAD_OFF
)
1382 bsd_args
.opt
= (args
->cmd
== REBOOT_HALT
) ? RB_HALT
: 0;
1383 bsd_args
.sysmsg_result
= 0;
1385 error
= sys_reboot(&bsd_args
);
1386 args
->sysmsg_result
= bsd_args
.sysmsg_result
;
1391 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1392 * p->p_retval[1] when COMPAT_43 or COMPAT_SUNOS is defined. This
1393 * globbers registers that are assumed to be preserved. The following
1394 * lightweight syscalls fixes this. See also linux_getgid16() and
1395 * linux_getuid16() in linux_uid16.c.
1397 * linux_getpid() - MP SAFE
1398 * linux_getgid() - MP SAFE
1399 * linux_getuid() - MP SAFE
1406 sys_linux_getpid(struct linux_getpid_args
*args
)
1408 struct thread
*td
= curthread
;
1409 struct proc
*p
= td
->td_proc
;
1411 args
->sysmsg_result
= p
->p_pid
;
1419 sys_linux_getgid(struct linux_getgid_args
*args
)
1421 struct thread
*td
= curthread
;
1423 args
->sysmsg_result
= td
->td_ucred
->cr_rgid
;
1431 sys_linux_getuid(struct linux_getuid_args
*args
)
1433 struct thread
*td
= curthread
;
1435 args
->sysmsg_result
= td
->td_ucred
->cr_ruid
;
1443 sys_linux_getsid(struct linux_getsid_args
*args
)
1445 struct getsid_args bsd
;
1448 bsd
.sysmsg_result
= 0;
1449 bsd
.pid
= args
->pid
;
1450 error
= sys_getsid(&bsd
);
1451 args
->sysmsg_result
= bsd
.sysmsg_result
;
1459 linux_nosys(struct nosys_args
*args
)