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
->type
== OBJT_MARKER
)
153 if (object
->shadow_count
> 1)
154 sysinfo
.sharedram
+= object
->resident_page_count
;
157 sysinfo
.sharedram
*= PAGE_SIZE
;
158 sysinfo
.bufferram
= 0;
160 if (swapblist
== NULL
) {
161 sysinfo
.totalswap
= 0;
162 sysinfo
.freeswap
= 0;
164 sysinfo
.totalswap
= swapblist
->bl_blocks
* 1024;
165 sysinfo
.freeswap
= swapblist
->bl_root
->u
.bmu_avail
* PAGE_SIZE
;
169 sysinfo
.procs
= 20; /* Hack */
171 error
= copyout(&sysinfo
, (caddr_t
)args
->info
, sizeof(sysinfo
));
179 sys_linux_alarm(struct linux_alarm_args
*args
)
181 struct thread
*td
= curthread
;
182 struct proc
*p
= td
->td_proc
;
183 struct itimerval it
, old_it
;
188 kprintf(ARGS(alarm
, "%u"), args
->secs
);
191 if (args
->secs
> 100000000)
194 it
.it_value
.tv_sec
= (long)args
->secs
;
195 it
.it_value
.tv_usec
= 0;
196 it
.it_interval
.tv_sec
= 0;
197 it
.it_interval
.tv_usec
= 0;
200 old_it
= p
->p_realtimer
;
202 if (timevalisset(&old_it
.it_value
))
203 callout_stop(&p
->p_ithandle
);
204 if (it
.it_value
.tv_sec
!= 0) {
205 callout_reset(&p
->p_ithandle
, tvtohz_high(&it
.it_value
),
207 timevaladd(&it
.it_value
, &tv
);
212 if (timevalcmp(&old_it
.it_value
, &tv
, >)) {
213 timevalsub(&old_it
.it_value
, &tv
);
214 if (old_it
.it_value
.tv_usec
!= 0)
215 old_it
.it_value
.tv_sec
++;
216 args
->sysmsg_result
= old_it
.it_value
.tv_sec
;
225 sys_linux_brk(struct linux_brk_args
*args
)
227 struct thread
*td
= curthread
;
228 struct proc
*p
= td
->td_proc
;
230 vm_offset_t
new, old
;
231 struct obreak_args bsd_args
;
237 kprintf(ARGS(brk
, "%p"), (void *)args
->dsend
);
239 old
= (vm_offset_t
)vm
->vm_daddr
+ ctob(vm
->vm_dsize
);
240 new = (vm_offset_t
)args
->dsend
;
241 bsd_args
.sysmsg_result
= 0;
242 bsd_args
.nsize
= (char *) new;
243 bsd_args
.sysmsg_result
= 0;
244 if (((caddr_t
)new > vm
->vm_daddr
) && !sys_obreak(&bsd_args
))
245 args
->sysmsg_result
= (long)new;
247 args
->sysmsg_result
= (long)old
;
257 sys_linux_uselib(struct linux_uselib_args
*args
)
259 struct thread
*td
= curthread
;
261 struct nlookupdata nd
;
266 unsigned long file_offset
;
268 unsigned long bss_size
;
275 error
= linux_copyin_path(args
->library
, &path
, LINUX_PATH_EXISTS
);
280 kprintf(ARGS(uselib
, "%s"), path
);
288 error
= nlookup_init(&nd
, path
, UIO_SYSSPACE
, NLC_FOLLOW
);
289 nd
.nl_flags
|= NLC_EXEC
;
291 error
= nlookup(&nd
);
293 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
297 * From here on down, we have a locked vnode that must be unlocked.
302 if (vp
->v_writecount
) {
308 error
= VOP_GETATTR(vp
, &attr
);
312 if ((vp
->v_mount
->mnt_flag
& MNT_NOEXEC
) ||
313 ((attr
.va_mode
& 0111) == 0) || (attr
.va_type
!= VREG
)) {
319 if (attr
.va_size
== 0) {
324 error
= VOP_OPEN(vp
, FREAD
, td
->td_ucred
, NULL
);
329 * Lock no longer needed
334 /* Pull in executable header into kernel_map */
335 error
= vm_mmap(&kernel_map
, (vm_offset_t
*)&a_out
, PAGE_SIZE
,
336 VM_PROT_READ
, VM_PROT_READ
, 0, (caddr_t
)vp
, 0);
340 /* Is it a Linux binary ? */
341 if (((a_out
->a_magic
>> 16) & 0xff) != 0x64) {
347 * While we are here, we should REALLY do some more checks
350 /* Set file/virtual offset based on a.out variant. */
351 switch ((int)(a_out
->a_magic
& 0xffff)) {
352 case 0413: /* ZMAGIC */
355 case 0314: /* QMAGIC */
363 bss_size
= round_page(a_out
->a_bss
);
365 /* Check various fields in header for validity/bounds. */
366 if (a_out
->a_text
& PAGE_MASK
|| a_out
->a_data
& PAGE_MASK
) {
371 /* text + data can't exceed file size */
372 if (a_out
->a_data
+ a_out
->a_text
> attr
.va_size
) {
378 * text/data/bss must not exceed limits
379 * XXX - this is not complete. it should check current usage PLUS
380 * the resources needed by this library.
382 if (a_out
->a_text
> maxtsiz
||
383 a_out
->a_data
+ bss_size
> p
->p_rlimit
[RLIMIT_DATA
].rlim_cur
) {
388 /* prevent more writers */
389 vsetflags(vp
, VTEXT
);
392 * Check if file_offset page aligned. Currently we cannot handle
393 * misalinged file offsets, and so we read in the entire image
396 if (file_offset
& PAGE_MASK
) {
398 kprintf("uselib: Non page aligned binary %lu\n", file_offset
);
400 /* Map text+data read/write/execute */
402 /* a_entry is the load address and is page aligned */
403 vmaddr
= trunc_page(a_out
->a_entry
);
405 /* get anon user mapping, read+write+execute */
406 error
= vm_map_find(&p
->p_vmspace
->vm_map
, NULL
, 0,
407 &vmaddr
, a_out
->a_text
+ a_out
->a_data
,
409 FALSE
, VM_MAPTYPE_NORMAL
,
410 VM_PROT_ALL
, VM_PROT_ALL
,
415 /* map file into kernel_map */
416 error
= vm_mmap(&kernel_map
, &buffer
,
417 round_page(a_out
->a_text
+ a_out
->a_data
+ file_offset
),
418 VM_PROT_READ
, VM_PROT_READ
, 0, (caddr_t
)vp
,
419 trunc_page(file_offset
));
423 /* copy from kernel VM space to user space */
424 error
= copyout((caddr_t
)(uintptr_t)(buffer
+ file_offset
),
425 (caddr_t
)vmaddr
, a_out
->a_text
+ a_out
->a_data
);
427 /* release temporary kernel space */
428 vm_map_remove(&kernel_map
, buffer
, buffer
+
429 round_page(a_out
->a_text
+ a_out
->a_data
+ file_offset
));
435 kprintf("uselib: Page aligned binary %lu\n", file_offset
);
438 * for QMAGIC, a_entry is 20 bytes beyond the load address
439 * to skip the executable header
441 vmaddr
= trunc_page(a_out
->a_entry
);
444 * Map it all into the process's space as a single
445 * copy-on-write "data" segment.
447 error
= vm_mmap(&p
->p_vmspace
->vm_map
, &vmaddr
,
448 a_out
->a_text
+ a_out
->a_data
, VM_PROT_ALL
, VM_PROT_ALL
,
449 MAP_PRIVATE
| MAP_FIXED
, (caddr_t
)vp
, file_offset
);
454 kprintf("mem=%08lx = %08lx %08lx\n", (long)vmaddr
, ((long*)vmaddr
)[0],
458 /* Calculate BSS start address */
459 vmaddr
= trunc_page(a_out
->a_entry
) + a_out
->a_text
+
462 /* allocate some 'anon' space */
463 error
= vm_map_find(&p
->p_vmspace
->vm_map
, NULL
, 0,
466 FALSE
, VM_MAPTYPE_NORMAL
,
467 VM_PROT_ALL
, VM_PROT_ALL
,
474 /* Unlock/release vnode */
480 /* Release the kernel mapping. */
482 vm_map_remove(&kernel_map
, (vm_offset_t
)a_out
,
483 (vm_offset_t
)a_out
+ PAGE_SIZE
);
487 linux_free_path(&path
);
495 sys_linux_select(struct linux_select_args
*args
)
497 struct select_args bsa
;
498 struct timeval tv0
, tv1
, utv
, *tvp
;
504 kprintf(ARGS(select
, "%d, %p, %p, %p, %p"), args
->nfds
,
505 (void *)args
->readfds
, (void *)args
->writefds
,
506 (void *)args
->exceptfds
, (void *)args
->timeout
);
510 bsa
.sysmsg_result
= 0;
512 bsa
.in
= args
->readfds
;
513 bsa
.ou
= args
->writefds
;
514 bsa
.ex
= args
->exceptfds
;
515 bsa
.tv
= (struct timeval
*)args
->timeout
;
518 * Store current time for computation of the amount of
522 if ((error
= copyin((caddr_t
)args
->timeout
, &utv
,
527 kprintf(LMSG("incoming timeout (%ld/%ld)"),
528 utv
.tv_sec
, utv
.tv_usec
);
531 if (itimerfix(&utv
)) {
533 * The timeval was invalid. Convert it to something
534 * valid that will act as it does under Linux.
536 sg
= stackgap_init();
537 tvp
= stackgap_alloc(&sg
, sizeof(utv
));
538 utv
.tv_sec
+= utv
.tv_usec
/ 1000000;
539 utv
.tv_usec
%= 1000000;
540 if (utv
.tv_usec
< 0) {
542 utv
.tv_usec
+= 1000000;
546 if ((error
= copyout(&utv
, tvp
, sizeof(utv
))))
553 error
= sys_select(&bsa
);
554 args
->sysmsg_result
= bsa
.sysmsg_result
;
557 kprintf(LMSG("real select returns %d"), error
);
561 * See fs/select.c in the Linux kernel. Without this,
562 * Maelstrom doesn't work.
564 if (error
== ERESTART
)
570 if (args
->sysmsg_result
) {
572 * Compute how much time was left of the timeout,
573 * by subtracting the current time and the time
574 * before we started the call, and subtracting
575 * that result from the user-supplied value.
578 timevalsub(&tv1
, &tv0
);
579 timevalsub(&utv
, &tv1
);
586 kprintf(LMSG("outgoing timeout (%ld/%ld)"),
587 utv
.tv_sec
, utv
.tv_usec
);
589 if ((error
= copyout(&utv
, (caddr_t
)args
->timeout
,
597 kprintf(LMSG("select_out -> %d"), error
);
606 sys_linux_mremap(struct linux_mremap_args
*args
)
608 struct munmap_args bsd_args
;
613 kprintf(ARGS(mremap
, "%p, %08lx, %08lx, %08lx"),
615 (unsigned long)args
->old_len
,
616 (unsigned long)args
->new_len
,
617 (unsigned long)args
->flags
);
619 args
->new_len
= round_page(args
->new_len
);
620 args
->old_len
= round_page(args
->old_len
);
622 if (args
->new_len
> args
->old_len
) {
623 args
->sysmsg_result
= 0;
627 if (args
->new_len
< args
->old_len
) {
628 bsd_args
.sysmsg_result
= 0;
629 bsd_args
.addr
= (caddr_t
)(args
->addr
+ args
->new_len
);
630 bsd_args
.len
= args
->old_len
- args
->new_len
;
631 error
= sys_munmap(&bsd_args
);
634 args
->sysmsg_resultp
= error
? NULL
: (void *)args
->addr
;
638 #define LINUX_MS_ASYNC 0x0001
639 #define LINUX_MS_INVALIDATE 0x0002
640 #define LINUX_MS_SYNC 0x0004
646 sys_linux_msync(struct linux_msync_args
*args
)
648 struct msync_args bsd_args
;
651 bsd_args
.addr
= (caddr_t
)args
->addr
;
652 bsd_args
.len
= args
->len
;
653 bsd_args
.flags
= args
->fl
& ~LINUX_MS_SYNC
;
654 bsd_args
.sysmsg_result
= 0;
656 error
= sys_msync(&bsd_args
);
657 args
->sysmsg_result
= bsd_args
.sysmsg_result
;
665 sys_linux_time(struct linux_time_args
*args
)
673 kprintf(ARGS(time
, "*"));
678 if (args
->tm
&& (error
= copyout(&tm
, (caddr_t
)args
->tm
, sizeof(tm
))))
680 args
->sysmsg_lresult
= tm
;
684 struct l_times_argv
{
691 #define CLK_TCK 100 /* Linux uses 100 */
693 #define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
699 sys_linux_times(struct linux_times_args
*args
)
701 struct thread
*td
= curthread
;
702 struct proc
*p
= td
->td_proc
;
704 struct l_times_argv tms
;
710 kprintf(ARGS(times
, "*"));
717 tms
.tms_utime
= CONVTCK(ru
.ru_utime
);
718 tms
.tms_stime
= CONVTCK(ru
.ru_stime
);
720 tms
.tms_cutime
= CONVTCK(p
->p_cru
.ru_utime
);
721 tms
.tms_cstime
= CONVTCK(p
->p_cru
.ru_stime
);
723 if ((error
= copyout(&tms
, (caddr_t
)args
->buf
, sizeof(tms
))))
727 args
->sysmsg_result
= (int)CONVTCK(tv
);
735 sys_linux_newuname(struct linux_newuname_args
*args
)
737 struct thread
*td
= curthread
;
738 struct l_new_utsname utsname
;
739 char *osrelease
, *osname
;
742 if (ldebug(newuname
))
743 kprintf(ARGS(newuname
, "*"));
747 osname
= linux_get_osname(td
);
748 osrelease
= linux_get_osrelease(td
);
750 bzero(&utsname
, sizeof(utsname
));
751 strncpy(utsname
.sysname
, osname
, LINUX_MAX_UTSNAME
-1);
752 strncpy(utsname
.nodename
, hostname
, LINUX_MAX_UTSNAME
-1);
753 strncpy(utsname
.release
, osrelease
, LINUX_MAX_UTSNAME
-1);
754 strncpy(utsname
.version
, version
, LINUX_MAX_UTSNAME
-1);
755 strncpy(utsname
.machine
, machine
, LINUX_MAX_UTSNAME
-1);
756 strncpy(utsname
.domainname
, domainname
, LINUX_MAX_UTSNAME
-1);
759 return (copyout(&utsname
, (caddr_t
)args
->buf
, sizeof(utsname
)));
762 #if defined(__i386__)
772 sys_linux_utime(struct linux_utime_args
*args
)
774 struct timeval tv
[2];
775 struct l_utimbuf lut
;
776 struct nlookupdata nd
;
780 error
= linux_copyin_path(args
->fname
, &path
, LINUX_PATH_EXISTS
);
785 kprintf(ARGS(utime
, "%s, *"), path
);
789 error
= copyin(args
->times
, &lut
, sizeof(lut
));
792 tv
[0].tv_sec
= lut
.l_actime
;
794 tv
[1].tv_sec
= lut
.l_modtime
;
798 error
= nlookup_init(&nd
, path
, UIO_SYSSPACE
, NLC_FOLLOW
);
800 error
= kern_utimes(&nd
, args
->times
? tv
: NULL
);
804 linux_free_path(&path
);
807 #endif /* __i386__ */
809 #define __WCLONE 0x80000000
815 sys_linux_waitpid(struct linux_waitpid_args
*args
)
817 int error
, options
, status
;
821 kprintf(ARGS(waitpid
, "%d, %p, %d"),
822 args
->pid
, (void *)args
->status
, args
->options
);
824 options
= args
->options
& (WNOHANG
| WUNTRACED
);
825 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
826 if (args
->options
& __WCLONE
)
827 options
|= WLINUXCLONE
;
829 error
= kern_wait(args
->pid
, args
->status
? &status
: NULL
, options
,
830 NULL
, &args
->sysmsg_result
);
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
));
850 sys_linux_wait4(struct linux_wait4_args
*args
)
852 struct thread
*td
= curthread
;
853 struct lwp
*lp
= td
->td_lwp
;
854 struct rusage rusage
;
855 int error
, options
, status
;
859 kprintf(ARGS(wait4
, "%d, %p, %d, %p"),
860 args
->pid
, (void *)args
->status
, args
->options
,
861 (void *)args
->rusage
);
863 options
= args
->options
& (WNOHANG
| WUNTRACED
);
864 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
865 if (args
->options
& __WCLONE
)
866 options
|= WLINUXCLONE
;
868 error
= kern_wait(args
->pid
, args
->status
? &status
: NULL
, options
,
869 args
->rusage
? &rusage
: NULL
, &args
->sysmsg_result
);
872 lwp_delsig(lp
, SIGCHLD
);
874 if (error
== 0 && args
->status
) {
876 if (WIFSIGNALED(status
))
877 status
= (status
& 0xffffff80) |
878 BSD_TO_LINUX_SIGNAL(WTERMSIG(status
));
879 else if (WIFSTOPPED(status
))
880 status
= (status
& 0xffff00ff) |
881 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(status
)) << 8);
882 error
= copyout(&status
, args
->status
, sizeof(status
));
884 if (error
== 0 && args
->rusage
)
885 error
= copyout(&rusage
, args
->rusage
, sizeof(rusage
));
894 sys_linux_mknod(struct linux_mknod_args
*args
)
896 struct nlookupdata nd
;
900 error
= linux_copyin_path(args
->path
, &path
, LINUX_PATH_CREATE
);
905 kprintf(ARGS(mknod
, "%s, %d, %d"),
906 path
, args
->mode
, args
->dev
);
909 error
= nlookup_init(&nd
, path
, UIO_SYSSPACE
, 0);
911 if (args
->mode
& S_IFIFO
) {
912 error
= kern_mkfifo(&nd
, args
->mode
);
914 error
= kern_mknod(&nd
, args
->mode
,
922 linux_free_path(&path
);
927 * UGH! This is just about the dumbest idea I've ever heard!!
932 sys_linux_personality(struct linux_personality_args
*args
)
935 if (ldebug(personality
))
936 kprintf(ARGS(personality
, "%d"), args
->per
);
941 /* Yes Jim, it's still a Linux... */
942 args
->sysmsg_result
= 0;
947 * Wrappers for get/setitimer for debugging..
952 sys_linux_setitimer(struct linux_setitimer_args
*args
)
954 struct setitimer_args bsa
;
955 struct itimerval foo
;
959 if (ldebug(setitimer
))
960 kprintf(ARGS(setitimer
, "%p, %p"),
961 (void *)args
->itv
, (void *)args
->oitv
);
963 bsa
.which
= args
->which
;
964 bsa
.itv
= (struct itimerval
*)args
->itv
;
965 bsa
.oitv
= (struct itimerval
*)args
->oitv
;
966 bsa
.sysmsg_result
= 0;
968 if ((error
= copyin((caddr_t
)args
->itv
, &foo
, sizeof(foo
))))
971 if (ldebug(setitimer
)) {
972 kprintf("setitimer: value: sec: %ld, usec: %ld\n",
973 foo
.it_value
.tv_sec
, foo
.it_value
.tv_usec
);
974 kprintf("setitimer: interval: sec: %ld, usec: %ld\n",
975 foo
.it_interval
.tv_sec
, foo
.it_interval
.tv_usec
);
979 error
= sys_setitimer(&bsa
);
980 args
->sysmsg_result
= bsa
.sysmsg_result
;
988 sys_linux_getitimer(struct linux_getitimer_args
*args
)
990 struct getitimer_args bsa
;
993 if (ldebug(getitimer
))
994 kprintf(ARGS(getitimer
, "%p"), (void *)args
->itv
);
996 bsa
.which
= args
->which
;
997 bsa
.itv
= (struct itimerval
*)args
->itv
;
998 bsa
.sysmsg_result
= 0;
999 error
= sys_getitimer(&bsa
);
1000 args
->sysmsg_result
= bsa
.sysmsg_result
;
1008 sys_linux_nice(struct linux_nice_args
*args
)
1010 struct setpriority_args bsd_args
;
1013 bsd_args
.which
= PRIO_PROCESS
;
1014 bsd_args
.who
= 0; /* current process */
1015 bsd_args
.prio
= args
->inc
;
1016 bsd_args
.sysmsg_result
= 0;
1017 error
= sys_setpriority(&bsd_args
);
1018 args
->sysmsg_result
= bsd_args
.sysmsg_result
;
1026 sys_linux_setgroups(struct linux_setgroups_args
*args
)
1028 struct thread
*td
= curthread
;
1029 struct proc
*p
= td
->td_proc
;
1030 struct ucred
*newcred
, *oldcred
;
1031 l_gid_t linux_gidset
[NGROUPS
];
1035 ngrp
= args
->gidsetsize
;
1036 oldcred
= td
->td_ucred
;
1039 * cr_groups[0] holds egid. Setting the whole set from
1040 * the supplied set will cause egid to be changed too.
1041 * Keep cr_groups[0] unchanged to prevent that.
1044 if ((error
= priv_check_cred(oldcred
, PRIV_CRED_SETGROUPS
, 0)) != 0)
1047 if ((u_int
)ngrp
>= NGROUPS
)
1051 newcred
= crdup(oldcred
);
1053 error
= copyin((caddr_t
)args
->grouplist
, linux_gidset
,
1054 ngrp
* sizeof(l_gid_t
));
1061 newcred
->cr_ngroups
= ngrp
+ 1;
1063 bsd_gidset
= newcred
->cr_groups
;
1066 bsd_gidset
[ngrp
+ 1] = linux_gidset
[ngrp
];
1070 newcred
->cr_ngroups
= 1;
1074 oldcred
= p
->p_ucred
; /* reload, deal with threads race */
1075 p
->p_ucred
= newcred
;
1085 sys_linux_getgroups(struct linux_getgroups_args
*args
)
1087 struct thread
*td
= curthread
;
1089 l_gid_t linux_gidset
[NGROUPS
];
1091 int bsd_gidsetsz
, ngrp
, error
;
1093 cred
= td
->td_ucred
;
1094 bsd_gidset
= cred
->cr_groups
;
1095 bsd_gidsetsz
= cred
->cr_ngroups
- 1;
1098 * cr_groups[0] holds egid. Returning the whole set
1099 * here will cause a duplicate. Exclude cr_groups[0]
1103 if ((ngrp
= args
->gidsetsize
) == 0) {
1104 args
->sysmsg_result
= bsd_gidsetsz
;
1108 if ((u_int
)ngrp
< bsd_gidsetsz
)
1112 while (ngrp
< bsd_gidsetsz
) {
1113 linux_gidset
[ngrp
] = bsd_gidset
[ngrp
+ 1];
1117 if ((error
= copyout(linux_gidset
, args
->grouplist
,
1118 ngrp
* sizeof(l_gid_t
)))) {
1122 args
->sysmsg_result
= ngrp
;
1130 sys_linux_setrlimit(struct linux_setrlimit_args
*args
)
1132 struct l_rlimit linux_rlim
;
1138 if (ldebug(setrlimit
))
1139 kprintf(ARGS(setrlimit
, "%d, %p"),
1140 args
->resource
, (void *)args
->rlim
);
1142 if (args
->resource
>= LINUX_RLIM_NLIMITS
)
1144 which
= linux_to_bsd_resource
[args
->resource
];
1148 error
= copyin(args
->rlim
, &linux_rlim
, sizeof(linux_rlim
));
1151 rlim
.rlim_cur
= (rlim_t
)linux_rlim
.rlim_cur
;
1152 rlim
.rlim_max
= (rlim_t
)linux_rlim
.rlim_max
;
1154 error
= kern_setrlimit(which
, &rlim
);
1163 sys_linux_old_getrlimit(struct linux_old_getrlimit_args
*args
)
1165 struct l_rlimit linux_rlim
;
1171 if (ldebug(old_getrlimit
))
1172 kprintf(ARGS(old_getrlimit
, "%d, %p"),
1173 args
->resource
, (void *)args
->rlim
);
1175 if (args
->resource
>= LINUX_RLIM_NLIMITS
)
1177 which
= linux_to_bsd_resource
[args
->resource
];
1181 error
= kern_getrlimit(which
, &rlim
);
1184 linux_rlim
.rlim_cur
= (l_ulong
)rlim
.rlim_cur
;
1185 if (linux_rlim
.rlim_cur
== ULONG_MAX
)
1186 linux_rlim
.rlim_cur
= LONG_MAX
;
1187 linux_rlim
.rlim_max
= (l_ulong
)rlim
.rlim_max
;
1188 if (linux_rlim
.rlim_max
== ULONG_MAX
)
1189 linux_rlim
.rlim_max
= LONG_MAX
;
1190 error
= copyout(&linux_rlim
, args
->rlim
, sizeof(linux_rlim
));
1199 sys_linux_getrlimit(struct linux_getrlimit_args
*args
)
1201 struct l_rlimit linux_rlim
;
1207 if (ldebug(getrlimit
))
1208 kprintf(ARGS(getrlimit
, "%d, %p"),
1209 args
->resource
, (void *)args
->rlim
);
1211 if (args
->resource
>= LINUX_RLIM_NLIMITS
)
1213 which
= linux_to_bsd_resource
[args
->resource
];
1217 error
= kern_getrlimit(which
, &rlim
);
1220 linux_rlim
.rlim_cur
= (l_ulong
)rlim
.rlim_cur
;
1221 linux_rlim
.rlim_max
= (l_ulong
)rlim
.rlim_max
;
1222 error
= copyout(&linux_rlim
, args
->rlim
, sizeof(linux_rlim
));
1231 sys_linux_sched_setscheduler(struct linux_sched_setscheduler_args
*args
)
1233 struct sched_setscheduler_args bsd
;
1237 if (ldebug(sched_setscheduler
))
1238 kprintf(ARGS(sched_setscheduler
, "%d, %d, %p"),
1239 args
->pid
, args
->policy
, (const void *)args
->param
);
1242 switch (args
->policy
) {
1243 case LINUX_SCHED_OTHER
:
1244 bsd
.policy
= SCHED_OTHER
;
1246 case LINUX_SCHED_FIFO
:
1247 bsd
.policy
= SCHED_FIFO
;
1249 case LINUX_SCHED_RR
:
1250 bsd
.policy
= SCHED_RR
;
1256 bsd
.pid
= args
->pid
;
1257 bsd
.param
= (struct sched_param
*)args
->param
;
1258 bsd
.sysmsg_result
= 0;
1260 error
= sys_sched_setscheduler(&bsd
);
1261 args
->sysmsg_result
= bsd
.sysmsg_result
;
1269 sys_linux_sched_getscheduler(struct linux_sched_getscheduler_args
*args
)
1271 struct sched_getscheduler_args bsd
;
1275 if (ldebug(sched_getscheduler
))
1276 kprintf(ARGS(sched_getscheduler
, "%d"), args
->pid
);
1279 bsd
.sysmsg_result
= 0;
1280 bsd
.pid
= args
->pid
;
1281 error
= sys_sched_getscheduler(&bsd
);
1282 args
->sysmsg_result
= bsd
.sysmsg_result
;
1284 switch (args
->sysmsg_result
) {
1286 args
->sysmsg_result
= LINUX_SCHED_OTHER
;
1289 args
->sysmsg_result
= LINUX_SCHED_FIFO
;
1292 args
->sysmsg_result
= LINUX_SCHED_RR
;
1302 sys_linux_sched_get_priority_max(struct linux_sched_get_priority_max_args
*args
)
1304 struct sched_get_priority_max_args bsd
;
1308 if (ldebug(sched_get_priority_max
))
1309 kprintf(ARGS(sched_get_priority_max
, "%d"), args
->policy
);
1312 switch (args
->policy
) {
1313 case LINUX_SCHED_OTHER
:
1314 bsd
.policy
= SCHED_OTHER
;
1316 case LINUX_SCHED_FIFO
:
1317 bsd
.policy
= SCHED_FIFO
;
1319 case LINUX_SCHED_RR
:
1320 bsd
.policy
= SCHED_RR
;
1325 bsd
.sysmsg_result
= 0;
1327 error
= sys_sched_get_priority_max(&bsd
);
1328 args
->sysmsg_result
= bsd
.sysmsg_result
;
1336 sys_linux_sched_get_priority_min(struct linux_sched_get_priority_min_args
*args
)
1338 struct sched_get_priority_min_args bsd
;
1342 if (ldebug(sched_get_priority_min
))
1343 kprintf(ARGS(sched_get_priority_min
, "%d"), args
->policy
);
1346 switch (args
->policy
) {
1347 case LINUX_SCHED_OTHER
:
1348 bsd
.policy
= SCHED_OTHER
;
1350 case LINUX_SCHED_FIFO
:
1351 bsd
.policy
= SCHED_FIFO
;
1353 case LINUX_SCHED_RR
:
1354 bsd
.policy
= SCHED_RR
;
1359 bsd
.sysmsg_result
= 0;
1361 error
= sys_sched_get_priority_min(&bsd
);
1362 args
->sysmsg_result
= bsd
.sysmsg_result
;
1366 #define REBOOT_CAD_ON 0x89abcdef
1367 #define REBOOT_CAD_OFF 0
1368 #define REBOOT_HALT 0xcdef0123
1374 sys_linux_reboot(struct linux_reboot_args
*args
)
1376 struct reboot_args bsd_args
;
1381 kprintf(ARGS(reboot
, "0x%x"), args
->cmd
);
1383 if (args
->cmd
== REBOOT_CAD_ON
|| args
->cmd
== REBOOT_CAD_OFF
)
1385 bsd_args
.opt
= (args
->cmd
== REBOOT_HALT
) ? RB_HALT
: 0;
1386 bsd_args
.sysmsg_result
= 0;
1388 error
= sys_reboot(&bsd_args
);
1389 args
->sysmsg_result
= bsd_args
.sysmsg_result
;
1394 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1395 * p->p_retval[1] when COMPAT_43 or COMPAT_SUNOS is defined. This
1396 * globbers registers that are assumed to be preserved. The following
1397 * lightweight syscalls fixes this. See also linux_getgid16() and
1398 * linux_getuid16() in linux_uid16.c.
1400 * linux_getpid() - MP SAFE
1401 * linux_getgid() - MP SAFE
1402 * linux_getuid() - MP SAFE
1409 sys_linux_getpid(struct linux_getpid_args
*args
)
1411 struct thread
*td
= curthread
;
1412 struct proc
*p
= td
->td_proc
;
1414 args
->sysmsg_result
= p
->p_pid
;
1422 sys_linux_getgid(struct linux_getgid_args
*args
)
1424 struct thread
*td
= curthread
;
1426 args
->sysmsg_result
= td
->td_ucred
->cr_rgid
;
1434 sys_linux_getuid(struct linux_getuid_args
*args
)
1436 struct thread
*td
= curthread
;
1438 args
->sysmsg_result
= td
->td_ucred
->cr_ruid
;
1446 sys_linux_getsid(struct linux_getsid_args
*args
)
1448 struct getsid_args bsd
;
1451 bsd
.sysmsg_result
= 0;
1452 bsd
.pid
= args
->pid
;
1453 error
= sys_getsid(&bsd
);
1454 args
->sysmsg_result
= bsd
.sysmsg_result
;
1462 linux_nosys(struct nosys_args
*args
)