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>
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>
50 #include <sys/signal2.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>
59 #include <sys/thread2.h>
63 #include <vm/vm_kern.h>
64 #include <vm/vm_map.h>
65 #include <vm/vm_extern.h>
66 #include <vm/vm_object.h>
67 #include <vm/vm_zone.h>
68 #include <vm/swap_pager.h>
70 #include <machine/frame.h>
71 #include <machine/limits.h>
72 #include <machine/psl.h>
73 #include <machine/sysarch.h>
75 #include <machine/segments.h>
78 #include <sys/sched.h>
80 #include <emulation/linux/linux_sysproto.h>
81 #include <arch_linux/linux.h>
82 #include <arch_linux/linux_proto.h>
83 #include "linux_mib.h"
84 #include "linux_util.h"
86 #define BSD_TO_LINUX_SIGNAL(sig) \
87 (((sig) <= LINUX_SIGTBLSZ) ? bsd_to_linux_signal[_SIG_IDX(sig)] : sig)
89 static unsigned int linux_to_bsd_resource
[LINUX_RLIM_NLIMITS
] = {
90 RLIMIT_CPU
, RLIMIT_FSIZE
, RLIMIT_DATA
, RLIMIT_STACK
,
91 RLIMIT_CORE
, RLIMIT_RSS
, RLIMIT_NPROC
, RLIMIT_NOFILE
,
96 l_long uptime
; /* Seconds since boot */
97 l_ulong loads
[3]; /* 1, 5, and 15 minute load averages */
98 l_ulong totalram
; /* Total usable main memory size */
99 l_ulong freeram
; /* Available memory size */
100 l_ulong sharedram
; /* Amount of shared memory */
101 l_ulong bufferram
; /* Memory used by buffers */
102 l_ulong totalswap
; /* Total swap space size */
103 l_ulong freeswap
; /* swap space still available */
104 l_ushort procs
; /* Number of current processes */
105 char _f
[22]; /* Pads structure to 64 bytes */
109 sys_linux_sysinfo(struct linux_sysinfo_args
*args
)
111 struct l_sysinfo sysinfo
;
116 /* Uptime is copied out of print_uptime() in kern_shutdown.c */
119 if (ts
.tv_sec
>= 86400) {
123 if (i
|| ts
.tv_sec
>= 3600) {
127 if (i
|| ts
.tv_sec
>= 60) {
131 sysinfo
.uptime
=ts
.tv_sec
;
133 /* Use the information from the mib to get our load averages */
134 for (i
= 0; i
< 3; i
++)
135 sysinfo
.loads
[i
] = averunnable
.ldavg
[i
];
137 sysinfo
.totalram
= Maxmem
* PAGE_SIZE
;
138 sysinfo
.freeram
= sysinfo
.totalram
- vmstats
.v_wire_count
* PAGE_SIZE
;
140 sysinfo
.sharedram
= 0;
141 for (object
= TAILQ_FIRST(&vm_object_list
); object
!= NULL
;
142 object
= TAILQ_NEXT(object
, object_list
))
143 if (object
->shadow_count
> 1)
144 sysinfo
.sharedram
+= object
->resident_page_count
;
146 sysinfo
.sharedram
*= PAGE_SIZE
;
147 sysinfo
.bufferram
= 0;
149 if (swapblist
== NULL
) {
150 sysinfo
.totalswap
= 0;
151 sysinfo
.freeswap
= 0;
153 sysinfo
.totalswap
= swapblist
->bl_blocks
* 1024;
154 sysinfo
.freeswap
= swapblist
->bl_root
->u
.bmu_avail
* PAGE_SIZE
;
157 sysinfo
.procs
= 20; /* Hack */
159 return copyout(&sysinfo
, (caddr_t
)args
->info
, sizeof(sysinfo
));
163 sys_linux_alarm(struct linux_alarm_args
*args
)
165 struct thread
*td
= curthread
;
166 struct proc
*p
= td
->td_proc
;
167 struct itimerval it
, old_it
;
174 kprintf(ARGS(alarm
, "%u"), args
->secs
);
177 if (args
->secs
> 100000000)
180 it
.it_value
.tv_sec
= (long)args
->secs
;
181 it
.it_value
.tv_usec
= 0;
182 it
.it_interval
.tv_sec
= 0;
183 it
.it_interval
.tv_usec
= 0;
185 old_it
= p
->p_realtimer
;
187 if (timevalisset(&old_it
.it_value
))
188 callout_stop(&p
->p_ithandle
);
189 if (it
.it_value
.tv_sec
!= 0) {
190 callout_reset(&p
->p_ithandle
, tvtohz_high(&it
.it_value
),
192 timevaladd(&it
.it_value
, &tv
);
196 if (timevalcmp(&old_it
.it_value
, &tv
, >)) {
197 timevalsub(&old_it
.it_value
, &tv
);
198 if (old_it
.it_value
.tv_usec
!= 0)
199 old_it
.it_value
.tv_sec
++;
200 args
->sysmsg_result
= old_it
.it_value
.tv_sec
;
206 sys_linux_brk(struct linux_brk_args
*args
)
208 struct thread
*td
= curthread
;
209 struct proc
*p
= td
->td_proc
;
211 vm_offset_t
new, old
;
212 struct obreak_args bsd_args
;
218 kprintf(ARGS(brk
, "%p"), (void *)args
->dsend
);
220 old
= (vm_offset_t
)vm
->vm_daddr
+ ctob(vm
->vm_dsize
);
221 new = (vm_offset_t
)args
->dsend
;
222 bsd_args
.sysmsg_result
= 0;
223 bsd_args
.nsize
= (char *) new;
224 bsd_args
.sysmsg_result
= 0;
225 if (((caddr_t
)new > vm
->vm_daddr
) && !sys_obreak(&bsd_args
))
226 args
->sysmsg_result
= (long)new;
228 args
->sysmsg_result
= (long)old
;
234 sys_linux_uselib(struct linux_uselib_args
*args
)
236 struct thread
*td
= curthread
;
238 struct nlookupdata nd
;
243 unsigned long file_offset
;
245 unsigned long bss_size
;
250 KKASSERT(td
->td_proc
);
253 error
= linux_copyin_path(args
->library
, &path
, LINUX_PATH_EXISTS
);
258 kprintf(ARGS(uselib
, "%s"), path
);
265 error
= nlookup_init(&nd
, path
, UIO_SYSSPACE
, NLC_FOLLOW
);
267 error
= nlookup(&nd
);
269 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
273 * From here on down, we have a locked vnode that must be unlocked.
278 if (vp
->v_writecount
) {
284 error
= VOP_GETATTR(vp
, &attr
);
288 if ((vp
->v_mount
->mnt_flag
& MNT_NOEXEC
) ||
289 ((attr
.va_mode
& 0111) == 0) || (attr
.va_type
!= VREG
)) {
295 if (attr
.va_size
== 0) {
300 /* Can we access it? */
301 error
= VOP_ACCESS(vp
, VEXEC
, p
->p_ucred
);
305 error
= VOP_OPEN(vp
, FREAD
, p
->p_ucred
, NULL
);
310 * Lock no longer needed
315 /* Pull in executable header into kernel_map */
316 error
= vm_mmap(&kernel_map
, (vm_offset_t
*)&a_out
, PAGE_SIZE
,
317 VM_PROT_READ
, VM_PROT_READ
, 0, (caddr_t
)vp
, 0);
321 /* Is it a Linux binary ? */
322 if (((a_out
->a_magic
>> 16) & 0xff) != 0x64) {
328 * While we are here, we should REALLY do some more checks
331 /* Set file/virtual offset based on a.out variant. */
332 switch ((int)(a_out
->a_magic
& 0xffff)) {
333 case 0413: /* ZMAGIC */
336 case 0314: /* QMAGIC */
344 bss_size
= round_page(a_out
->a_bss
);
346 /* Check various fields in header for validity/bounds. */
347 if (a_out
->a_text
& PAGE_MASK
|| a_out
->a_data
& PAGE_MASK
) {
352 /* text + data can't exceed file size */
353 if (a_out
->a_data
+ a_out
->a_text
> attr
.va_size
) {
359 * text/data/bss must not exceed limits
360 * XXX - this is not complete. it should check current usage PLUS
361 * the resources needed by this library.
363 if (a_out
->a_text
> maxtsiz
||
364 a_out
->a_data
+ bss_size
> p
->p_rlimit
[RLIMIT_DATA
].rlim_cur
) {
369 /* prevent more writers */
373 * Check if file_offset page aligned. Currently we cannot handle
374 * misalinged file offsets, and so we read in the entire image
377 if (file_offset
& PAGE_MASK
) {
379 kprintf("uselib: Non page aligned binary %lu\n", file_offset
);
381 /* Map text+data read/write/execute */
383 /* a_entry is the load address and is page aligned */
384 vmaddr
= trunc_page(a_out
->a_entry
);
386 /* get anon user mapping, read+write+execute */
387 error
= vm_map_find(&p
->p_vmspace
->vm_map
, NULL
, 0,
388 &vmaddr
, a_out
->a_text
+ a_out
->a_data
,
391 VM_PROT_ALL
, VM_PROT_ALL
,
396 /* map file into kernel_map */
397 error
= vm_mmap(&kernel_map
, &buffer
,
398 round_page(a_out
->a_text
+ a_out
->a_data
+ file_offset
),
399 VM_PROT_READ
, VM_PROT_READ
, 0, (caddr_t
)vp
,
400 trunc_page(file_offset
));
404 /* copy from kernel VM space to user space */
405 error
= copyout((caddr_t
)(uintptr_t)(buffer
+ file_offset
),
406 (caddr_t
)vmaddr
, a_out
->a_text
+ a_out
->a_data
);
408 /* release temporary kernel space */
409 vm_map_remove(&kernel_map
, buffer
, buffer
+
410 round_page(a_out
->a_text
+ a_out
->a_data
+ file_offset
));
416 kprintf("uselib: Page aligned binary %lu\n", file_offset
);
419 * for QMAGIC, a_entry is 20 bytes beyond the load address
420 * to skip the executable header
422 vmaddr
= trunc_page(a_out
->a_entry
);
425 * Map it all into the process's space as a single
426 * copy-on-write "data" segment.
428 error
= vm_mmap(&p
->p_vmspace
->vm_map
, &vmaddr
,
429 a_out
->a_text
+ a_out
->a_data
, VM_PROT_ALL
, VM_PROT_ALL
,
430 MAP_PRIVATE
| MAP_FIXED
, (caddr_t
)vp
, file_offset
);
435 kprintf("mem=%08lx = %08lx %08lx\n", (long)vmaddr
, ((long*)vmaddr
)[0],
439 /* Calculate BSS start address */
440 vmaddr
= trunc_page(a_out
->a_entry
) + a_out
->a_text
+
443 /* allocate some 'anon' space */
444 error
= vm_map_find(&p
->p_vmspace
->vm_map
, NULL
, 0,
448 VM_PROT_ALL
, VM_PROT_ALL
,
455 /* Unlock/release vnode */
461 /* Release the kernel mapping. */
463 vm_map_remove(&kernel_map
, (vm_offset_t
)a_out
,
464 (vm_offset_t
)a_out
+ PAGE_SIZE
);
467 linux_free_path(&path
);
472 sys_linux_select(struct linux_select_args
*args
)
474 struct select_args bsa
;
475 struct timeval tv0
, tv1
, utv
, *tvp
;
481 kprintf(ARGS(select
, "%d, %p, %p, %p, %p"), args
->nfds
,
482 (void *)args
->readfds
, (void *)args
->writefds
,
483 (void *)args
->exceptfds
, (void *)args
->timeout
);
487 bsa
.sysmsg_result
= 0;
489 bsa
.in
= args
->readfds
;
490 bsa
.ou
= args
->writefds
;
491 bsa
.ex
= args
->exceptfds
;
492 bsa
.tv
= (struct timeval
*)args
->timeout
;
495 * Store current time for computation of the amount of
499 if ((error
= copyin((caddr_t
)args
->timeout
, &utv
,
504 kprintf(LMSG("incoming timeout (%ld/%ld)"),
505 utv
.tv_sec
, utv
.tv_usec
);
508 if (itimerfix(&utv
)) {
510 * The timeval was invalid. Convert it to something
511 * valid that will act as it does under Linux.
513 sg
= stackgap_init();
514 tvp
= stackgap_alloc(&sg
, sizeof(utv
));
515 utv
.tv_sec
+= utv
.tv_usec
/ 1000000;
516 utv
.tv_usec
%= 1000000;
517 if (utv
.tv_usec
< 0) {
519 utv
.tv_usec
+= 1000000;
523 if ((error
= copyout(&utv
, tvp
, sizeof(utv
))))
530 error
= sys_select(&bsa
);
531 args
->sysmsg_result
= bsa
.sysmsg_result
;
534 kprintf(LMSG("real select returns %d"), error
);
538 * See fs/select.c in the Linux kernel. Without this,
539 * Maelstrom doesn't work.
541 if (error
== ERESTART
)
547 if (args
->sysmsg_result
) {
549 * Compute how much time was left of the timeout,
550 * by subtracting the current time and the time
551 * before we started the call, and subtracting
552 * that result from the user-supplied value.
555 timevalsub(&tv1
, &tv0
);
556 timevalsub(&utv
, &tv1
);
563 kprintf(LMSG("outgoing timeout (%ld/%ld)"),
564 utv
.tv_sec
, utv
.tv_usec
);
566 if ((error
= copyout(&utv
, (caddr_t
)args
->timeout
,
574 kprintf(LMSG("select_out -> %d"), error
);
580 sys_linux_mremap(struct linux_mremap_args
*args
)
582 struct munmap_args bsd_args
;
587 kprintf(ARGS(mremap
, "%p, %08lx, %08lx, %08lx"),
589 (unsigned long)args
->old_len
,
590 (unsigned long)args
->new_len
,
591 (unsigned long)args
->flags
);
593 args
->new_len
= round_page(args
->new_len
);
594 args
->old_len
= round_page(args
->old_len
);
596 if (args
->new_len
> args
->old_len
) {
597 args
->sysmsg_result
= 0;
601 if (args
->new_len
< args
->old_len
) {
602 bsd_args
.sysmsg_result
= 0;
603 bsd_args
.addr
= (caddr_t
)(args
->addr
+ args
->new_len
);
604 bsd_args
.len
= args
->old_len
- args
->new_len
;
605 error
= sys_munmap(&bsd_args
);
608 args
->sysmsg_resultp
= error
? NULL
: (void *)args
->addr
;
612 #define LINUX_MS_ASYNC 0x0001
613 #define LINUX_MS_INVALIDATE 0x0002
614 #define LINUX_MS_SYNC 0x0004
617 sys_linux_msync(struct linux_msync_args
*args
)
619 struct msync_args bsd_args
;
622 bsd_args
.addr
= (caddr_t
)args
->addr
;
623 bsd_args
.len
= args
->len
;
624 bsd_args
.flags
= args
->fl
& ~LINUX_MS_SYNC
;
625 bsd_args
.sysmsg_result
= 0;
627 error
= sys_msync(&bsd_args
);
628 args
->sysmsg_result
= bsd_args
.sysmsg_result
;
633 sys_linux_time(struct linux_time_args
*args
)
641 kprintf(ARGS(time
, "*"));
646 if (args
->tm
&& (error
= copyout(&tm
, (caddr_t
)args
->tm
, sizeof(tm
))))
648 args
->sysmsg_lresult
= tm
;
652 struct l_times_argv
{
659 #define CLK_TCK 100 /* Linux uses 100 */
661 #define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
664 sys_linux_times(struct linux_times_args
*args
)
666 struct thread
*td
= curthread
;
667 struct proc
*p
= td
->td_proc
;
669 struct l_times_argv tms
;
676 kprintf(ARGS(times
, "*"));
681 tms
.tms_utime
= CONVTCK(ru
.ru_utime
);
682 tms
.tms_stime
= CONVTCK(ru
.ru_stime
);
684 tms
.tms_cutime
= CONVTCK(p
->p_cru
.ru_utime
);
685 tms
.tms_cstime
= CONVTCK(p
->p_cru
.ru_stime
);
687 if ((error
= copyout(&tms
, (caddr_t
)args
->buf
, sizeof(tms
))))
691 args
->sysmsg_result
= (int)CONVTCK(tv
);
696 sys_linux_newuname(struct linux_newuname_args
*args
)
698 struct thread
*td
= curthread
;
699 struct l_new_utsname utsname
;
700 char *osrelease
, *osname
;
703 if (ldebug(newuname
))
704 kprintf(ARGS(newuname
, "*"));
707 osname
= linux_get_osname(td
);
708 osrelease
= linux_get_osrelease(td
);
710 bzero(&utsname
, sizeof(utsname
));
711 strncpy(utsname
.sysname
, osname
, LINUX_MAX_UTSNAME
-1);
712 strncpy(utsname
.nodename
, hostname
, LINUX_MAX_UTSNAME
-1);
713 strncpy(utsname
.release
, osrelease
, LINUX_MAX_UTSNAME
-1);
714 strncpy(utsname
.version
, version
, LINUX_MAX_UTSNAME
-1);
715 strncpy(utsname
.machine
, machine
, LINUX_MAX_UTSNAME
-1);
716 strncpy(utsname
.domainname
, domainname
, LINUX_MAX_UTSNAME
-1);
718 return (copyout(&utsname
, (caddr_t
)args
->buf
, sizeof(utsname
)));
721 #if defined(__i386__)
728 sys_linux_utime(struct linux_utime_args
*args
)
730 struct timeval tv
[2];
731 struct l_utimbuf lut
;
732 struct nlookupdata nd
;
736 error
= linux_copyin_path(args
->fname
, &path
, LINUX_PATH_EXISTS
);
741 kprintf(ARGS(utime
, "%s, *"), path
);
745 error
= copyin(args
->times
, &lut
, sizeof(lut
));
748 tv
[0].tv_sec
= lut
.l_actime
;
750 tv
[1].tv_sec
= lut
.l_modtime
;
753 error
= nlookup_init(&nd
, path
, UIO_SYSSPACE
, NLC_FOLLOW
);
755 error
= kern_utimes(&nd
, args
->times
? tv
: NULL
);
758 linux_free_path(&path
);
761 #endif /* __i386__ */
763 #define __WCLONE 0x80000000
766 sys_linux_waitpid(struct linux_waitpid_args
*args
)
768 int error
, options
, status
;
772 kprintf(ARGS(waitpid
, "%d, %p, %d"),
773 args
->pid
, (void *)args
->status
, args
->options
);
775 options
= args
->options
& (WNOHANG
| WUNTRACED
);
776 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
777 if (args
->options
& __WCLONE
)
778 options
|= WLINUXCLONE
;
780 error
= kern_wait(args
->pid
, args
->status
? &status
: NULL
, options
,
781 NULL
, &args
->sysmsg_result
);
783 if (error
== 0 && args
->status
) {
785 if (WIFSIGNALED(status
))
786 status
= (status
& 0xffffff80) |
787 BSD_TO_LINUX_SIGNAL(WTERMSIG(status
));
788 else if (WIFSTOPPED(status
))
789 status
= (status
& 0xffff00ff) |
790 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(status
)) << 8);
791 error
= copyout(&status
, args
->status
, sizeof(status
));
798 sys_linux_wait4(struct linux_wait4_args
*args
)
800 struct thread
*td
= curthread
;
801 struct lwp
*lp
= td
->td_lwp
;
802 struct rusage rusage
;
803 int error
, options
, status
;
809 kprintf(ARGS(wait4
, "%d, %p, %d, %p"),
810 args
->pid
, (void *)args
->status
, args
->options
,
811 (void *)args
->rusage
);
813 options
= args
->options
& (WNOHANG
| WUNTRACED
);
814 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
815 if (args
->options
& __WCLONE
)
816 options
|= WLINUXCLONE
;
818 error
= kern_wait(args
->pid
, args
->status
? &status
: NULL
, options
,
819 args
->rusage
? &rusage
: NULL
, &args
->sysmsg_result
);
822 lwp_delsig(lp
, SIGCHLD
);
824 if (error
== 0 && args
->status
) {
826 if (WIFSIGNALED(status
))
827 status
= (status
& 0xffffff80) |
828 BSD_TO_LINUX_SIGNAL(WTERMSIG(status
));
829 else if (WIFSTOPPED(status
))
830 status
= (status
& 0xffff00ff) |
831 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(status
)) << 8);
832 error
= copyout(&status
, args
->status
, sizeof(status
));
834 if (error
== 0 && args
->rusage
)
835 error
= copyout(&rusage
, args
->rusage
, sizeof(rusage
));
841 sys_linux_mknod(struct linux_mknod_args
*args
)
843 struct nlookupdata nd
;
847 error
= linux_copyin_path(args
->path
, &path
, LINUX_PATH_CREATE
);
852 kprintf(ARGS(mknod
, "%s, %d, %d"),
853 path
, args
->mode
, args
->dev
);
855 error
= nlookup_init(&nd
, path
, UIO_SYSSPACE
, 0);
857 if (args
->mode
& S_IFIFO
) {
858 error
= kern_mkfifo(&nd
, args
->mode
);
860 error
= kern_mknod(&nd
, args
->mode
,
867 linux_free_path(&path
);
872 * UGH! This is just about the dumbest idea I've ever heard!!
875 sys_linux_personality(struct linux_personality_args
*args
)
878 if (ldebug(personality
))
879 kprintf(ARGS(personality
, "%d"), args
->per
);
884 /* Yes Jim, it's still a Linux... */
885 args
->sysmsg_result
= 0;
890 * Wrappers for get/setitimer for debugging..
893 sys_linux_setitimer(struct linux_setitimer_args
*args
)
895 struct setitimer_args bsa
;
896 struct itimerval foo
;
900 if (ldebug(setitimer
))
901 kprintf(ARGS(setitimer
, "%p, %p"),
902 (void *)args
->itv
, (void *)args
->oitv
);
904 bsa
.which
= args
->which
;
905 bsa
.itv
= (struct itimerval
*)args
->itv
;
906 bsa
.oitv
= (struct itimerval
*)args
->oitv
;
907 bsa
.sysmsg_result
= 0;
909 if ((error
= copyin((caddr_t
)args
->itv
, &foo
, sizeof(foo
))))
912 if (ldebug(setitimer
)) {
913 kprintf("setitimer: value: sec: %ld, usec: %ld\n",
914 foo
.it_value
.tv_sec
, foo
.it_value
.tv_usec
);
915 kprintf("setitimer: interval: sec: %ld, usec: %ld\n",
916 foo
.it_interval
.tv_sec
, foo
.it_interval
.tv_usec
);
920 error
= sys_setitimer(&bsa
);
921 args
->sysmsg_result
= bsa
.sysmsg_result
;
926 sys_linux_getitimer(struct linux_getitimer_args
*args
)
928 struct getitimer_args bsa
;
931 if (ldebug(getitimer
))
932 kprintf(ARGS(getitimer
, "%p"), (void *)args
->itv
);
934 bsa
.which
= args
->which
;
935 bsa
.itv
= (struct itimerval
*)args
->itv
;
936 bsa
.sysmsg_result
= 0;
937 error
= sys_getitimer(&bsa
);
938 args
->sysmsg_result
= bsa
.sysmsg_result
;
943 sys_linux_nice(struct linux_nice_args
*args
)
945 struct setpriority_args bsd_args
;
948 bsd_args
.which
= PRIO_PROCESS
;
949 bsd_args
.who
= 0; /* current process */
950 bsd_args
.prio
= args
->inc
;
951 bsd_args
.sysmsg_result
= 0;
952 error
= sys_setpriority(&bsd_args
);
953 args
->sysmsg_result
= bsd_args
.sysmsg_result
;
958 sys_linux_setgroups(struct linux_setgroups_args
*args
)
960 struct thread
*td
= curthread
;
961 struct proc
*p
= td
->td_proc
;
962 struct ucred
*newcred
, *oldcred
;
963 l_gid_t linux_gidset
[NGROUPS
];
969 ngrp
= args
->gidsetsize
;
970 oldcred
= p
->p_ucred
;
973 * cr_groups[0] holds egid. Setting the whole set from
974 * the supplied set will cause egid to be changed too.
975 * Keep cr_groups[0] unchanged to prevent that.
978 if ((error
= suser_cred(oldcred
, PRISON_ROOT
)) != 0)
984 newcred
= crdup(oldcred
);
986 error
= copyin((caddr_t
)args
->grouplist
, linux_gidset
,
987 ngrp
* sizeof(l_gid_t
));
991 newcred
->cr_ngroups
= ngrp
+ 1;
993 bsd_gidset
= newcred
->cr_groups
;
996 bsd_gidset
[ngrp
+ 1] = linux_gidset
[ngrp
];
1000 newcred
->cr_ngroups
= 1;
1004 p
->p_ucred
= newcred
;
1010 sys_linux_getgroups(struct linux_getgroups_args
*args
)
1012 struct thread
*td
= curthread
;
1013 struct proc
*p
= td
->td_proc
;
1015 l_gid_t linux_gidset
[NGROUPS
];
1017 int bsd_gidsetsz
, ngrp
, error
;
1022 bsd_gidset
= cred
->cr_groups
;
1023 bsd_gidsetsz
= cred
->cr_ngroups
- 1;
1026 * cr_groups[0] holds egid. Returning the whole set
1027 * here will cause a duplicate. Exclude cr_groups[0]
1031 if ((ngrp
= args
->gidsetsize
) == 0) {
1032 args
->sysmsg_result
= bsd_gidsetsz
;
1036 if (ngrp
< bsd_gidsetsz
)
1040 while (ngrp
< bsd_gidsetsz
) {
1041 linux_gidset
[ngrp
] = bsd_gidset
[ngrp
+ 1];
1045 if ((error
= copyout(linux_gidset
, (caddr_t
)args
->grouplist
,
1046 ngrp
* sizeof(l_gid_t
))))
1049 args
->sysmsg_result
= ngrp
;
1054 sys_linux_setrlimit(struct linux_setrlimit_args
*args
)
1056 struct l_rlimit linux_rlim
;
1062 if (ldebug(setrlimit
))
1063 kprintf(ARGS(setrlimit
, "%d, %p"),
1064 args
->resource
, (void *)args
->rlim
);
1066 if (args
->resource
>= LINUX_RLIM_NLIMITS
)
1068 which
= linux_to_bsd_resource
[args
->resource
];
1072 error
= copyin(args
->rlim
, &linux_rlim
, sizeof(linux_rlim
));
1075 rlim
.rlim_cur
= (rlim_t
)linux_rlim
.rlim_cur
;
1076 rlim
.rlim_max
= (rlim_t
)linux_rlim
.rlim_max
;
1078 error
= kern_setrlimit(which
, &rlim
);
1084 sys_linux_old_getrlimit(struct linux_old_getrlimit_args
*args
)
1086 struct l_rlimit linux_rlim
;
1092 if (ldebug(old_getrlimit
))
1093 kprintf(ARGS(old_getrlimit
, "%d, %p"),
1094 args
->resource
, (void *)args
->rlim
);
1096 if (args
->resource
>= LINUX_RLIM_NLIMITS
)
1098 which
= linux_to_bsd_resource
[args
->resource
];
1102 error
= kern_getrlimit(which
, &rlim
);
1105 linux_rlim
.rlim_cur
= (l_ulong
)rlim
.rlim_cur
;
1106 if (linux_rlim
.rlim_cur
== ULONG_MAX
)
1107 linux_rlim
.rlim_cur
= LONG_MAX
;
1108 linux_rlim
.rlim_max
= (l_ulong
)rlim
.rlim_max
;
1109 if (linux_rlim
.rlim_max
== ULONG_MAX
)
1110 linux_rlim
.rlim_max
= LONG_MAX
;
1111 error
= copyout(&linux_rlim
, args
->rlim
, sizeof(linux_rlim
));
1117 sys_linux_getrlimit(struct linux_getrlimit_args
*args
)
1119 struct l_rlimit linux_rlim
;
1125 if (ldebug(getrlimit
))
1126 kprintf(ARGS(getrlimit
, "%d, %p"),
1127 args
->resource
, (void *)args
->rlim
);
1129 if (args
->resource
>= LINUX_RLIM_NLIMITS
)
1131 which
= linux_to_bsd_resource
[args
->resource
];
1135 error
= kern_getrlimit(which
, &rlim
);
1138 linux_rlim
.rlim_cur
= (l_ulong
)rlim
.rlim_cur
;
1139 linux_rlim
.rlim_max
= (l_ulong
)rlim
.rlim_max
;
1140 error
= copyout(&linux_rlim
, args
->rlim
, sizeof(linux_rlim
));
1146 sys_linux_sched_setscheduler(struct linux_sched_setscheduler_args
*args
)
1148 struct sched_setscheduler_args bsd
;
1152 if (ldebug(sched_setscheduler
))
1153 kprintf(ARGS(sched_setscheduler
, "%d, %d, %p"),
1154 args
->pid
, args
->policy
, (const void *)args
->param
);
1157 switch (args
->policy
) {
1158 case LINUX_SCHED_OTHER
:
1159 bsd
.policy
= SCHED_OTHER
;
1161 case LINUX_SCHED_FIFO
:
1162 bsd
.policy
= SCHED_FIFO
;
1164 case LINUX_SCHED_RR
:
1165 bsd
.policy
= SCHED_RR
;
1171 bsd
.pid
= args
->pid
;
1172 bsd
.param
= (struct sched_param
*)args
->param
;
1173 bsd
.sysmsg_result
= 0;
1175 error
= sys_sched_setscheduler(&bsd
);
1176 args
->sysmsg_result
= bsd
.sysmsg_result
;
1181 sys_linux_sched_getscheduler(struct linux_sched_getscheduler_args
*args
)
1183 struct sched_getscheduler_args bsd
;
1187 if (ldebug(sched_getscheduler
))
1188 kprintf(ARGS(sched_getscheduler
, "%d"), args
->pid
);
1191 bsd
.sysmsg_result
= 0;
1192 bsd
.pid
= args
->pid
;
1193 error
= sys_sched_getscheduler(&bsd
);
1194 args
->sysmsg_result
= bsd
.sysmsg_result
;
1196 switch (args
->sysmsg_result
) {
1198 args
->sysmsg_result
= LINUX_SCHED_OTHER
;
1201 args
->sysmsg_result
= LINUX_SCHED_FIFO
;
1204 args
->sysmsg_result
= LINUX_SCHED_RR
;
1211 sys_linux_sched_get_priority_max(struct linux_sched_get_priority_max_args
*args
)
1213 struct sched_get_priority_max_args bsd
;
1217 if (ldebug(sched_get_priority_max
))
1218 kprintf(ARGS(sched_get_priority_max
, "%d"), args
->policy
);
1221 switch (args
->policy
) {
1222 case LINUX_SCHED_OTHER
:
1223 bsd
.policy
= SCHED_OTHER
;
1225 case LINUX_SCHED_FIFO
:
1226 bsd
.policy
= SCHED_FIFO
;
1228 case LINUX_SCHED_RR
:
1229 bsd
.policy
= SCHED_RR
;
1234 bsd
.sysmsg_result
= 0;
1236 error
= sys_sched_get_priority_max(&bsd
);
1237 args
->sysmsg_result
= bsd
.sysmsg_result
;
1242 sys_linux_sched_get_priority_min(struct linux_sched_get_priority_min_args
*args
)
1244 struct sched_get_priority_min_args bsd
;
1248 if (ldebug(sched_get_priority_min
))
1249 kprintf(ARGS(sched_get_priority_min
, "%d"), args
->policy
);
1252 switch (args
->policy
) {
1253 case LINUX_SCHED_OTHER
:
1254 bsd
.policy
= SCHED_OTHER
;
1256 case LINUX_SCHED_FIFO
:
1257 bsd
.policy
= SCHED_FIFO
;
1259 case LINUX_SCHED_RR
:
1260 bsd
.policy
= SCHED_RR
;
1265 bsd
.sysmsg_result
= 0;
1267 error
= sys_sched_get_priority_min(&bsd
);
1268 args
->sysmsg_result
= bsd
.sysmsg_result
;
1272 #define REBOOT_CAD_ON 0x89abcdef
1273 #define REBOOT_CAD_OFF 0
1274 #define REBOOT_HALT 0xcdef0123
1277 sys_linux_reboot(struct linux_reboot_args
*args
)
1279 struct reboot_args bsd_args
;
1284 kprintf(ARGS(reboot
, "0x%x"), args
->cmd
);
1286 if (args
->cmd
== REBOOT_CAD_ON
|| args
->cmd
== REBOOT_CAD_OFF
)
1288 bsd_args
.opt
= (args
->cmd
== REBOOT_HALT
) ? RB_HALT
: 0;
1289 bsd_args
.sysmsg_result
= 0;
1291 error
= sys_reboot(&bsd_args
);
1292 args
->sysmsg_result
= bsd_args
.sysmsg_result
;
1297 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1298 * p->p_retval[1] when COMPAT_43 or COMPAT_SUNOS is defined. This
1299 * globbers registers that are assumed to be preserved. The following
1300 * lightweight syscalls fixes this. See also linux_getgid16() and
1301 * linux_getuid16() in linux_uid16.c.
1303 * linux_getpid() - MP SAFE
1304 * linux_getgid() - MP SAFE
1305 * linux_getuid() - MP SAFE
1309 sys_linux_getpid(struct linux_getpid_args
*args
)
1311 struct thread
*td
= curthread
;
1312 struct proc
*p
= td
->td_proc
;
1316 args
->sysmsg_result
= p
->p_pid
;
1321 sys_linux_getgid(struct linux_getgid_args
*args
)
1323 struct thread
*td
= curthread
;
1324 struct proc
*p
= td
->td_proc
;
1328 args
->sysmsg_result
= p
->p_ucred
->cr_rgid
;
1333 sys_linux_getuid(struct linux_getuid_args
*args
)
1335 struct thread
*td
= curthread
;
1336 struct proc
*p
= td
->td_proc
;
1340 args
->sysmsg_result
= p
->p_ucred
->cr_ruid
;
1345 sys_linux_getsid(struct linux_getsid_args
*args
)
1347 struct getsid_args bsd
;
1350 bsd
.sysmsg_result
= 0;
1351 bsd
.pid
= args
->pid
;
1352 error
= sys_getsid(&bsd
);
1353 args
->sysmsg_result
= bsd
.sysmsg_result
;
1358 linux_nosys(struct nosys_args
*args
)