799abc3141a6fba3b3b5fd283bc3af8cbcc66c6e
[dragonfly.git] / sys / emulation / linux / linux_misc.c
blob799abc3141a6fba3b3b5fd283bc3af8cbcc66c6e
1 /*-
2 * Copyright (c) 1994-1995 Søren Schmidt
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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>
40 #include <sys/lock.h>
41 #include <sys/mman.h>
42 #include <sys/mount.h>
43 #include <sys/poll.h>
44 #include <sys/proc.h>
45 #include <sys/priv.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>
51 #include <sys/stat.h>
52 #include <sys/sysctl.h>
53 #include <sys/sysproto.h>
54 #include <sys/time.h>
55 #include <sys/unistd.h>
56 #include <sys/vmmeter.h>
57 #include <sys/vnode.h>
58 #include <sys/wait.h>
60 #include <sys/signal2.h>
61 #include <sys/thread2.h>
62 #include <sys/mplock2.h>
64 #include <vm/vm.h>
65 #include <vm/pmap.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>
77 #ifdef __i386__
78 #include <machine/segments.h>
79 #endif
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,
95 RLIMIT_MEMLOCK, -1
98 struct l_sysinfo {
99 l_long uptime; /* Seconds since boot */
100 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */
101 l_ulong totalram; /* Total usable main memory size */
102 l_ulong freeram; /* Available memory size */
103 l_ulong sharedram; /* Amount of shared memory */
104 l_ulong bufferram; /* Memory used by buffers */
105 l_ulong totalswap; /* Total swap space size */
106 l_ulong freeswap; /* swap space still available */
107 l_ushort procs; /* Number of current processes */
108 char _f[22]; /* Pads structure to 64 bytes */
112 * MPALMOSTSAFE
115 sys_linux_sysinfo(struct linux_sysinfo_args *args)
117 struct l_sysinfo sysinfo;
118 vm_object_t object;
119 struct timespec ts;
120 int error;
121 int i;
123 /* Uptime is copied out of print_uptime() in kern_shutdown.c */
124 getnanouptime(&ts);
125 i = 0;
126 if (ts.tv_sec >= 86400) {
127 ts.tv_sec %= 86400;
128 i = 1;
130 if (i || ts.tv_sec >= 3600) {
131 ts.tv_sec %= 3600;
132 i = 1;
134 if (i || ts.tv_sec >= 60) {
135 ts.tv_sec %= 60;
136 i = 1;
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;
147 get_mplock();
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)
152 continue;
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;
163 } else {
164 sysinfo.totalswap = swapblist->bl_blocks * 1024;
165 sysinfo.freeswap = swapblist->bl_root->u.bmu_avail * PAGE_SIZE;
167 rel_mplock();
169 sysinfo.procs = 20; /* Hack */
171 error = copyout(&sysinfo, (caddr_t)args->info, sizeof(sysinfo));
172 return (error);
176 * MPALMOSTSAFE
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;
184 struct timeval tv;
186 #ifdef DEBUG
187 if (ldebug(alarm))
188 kprintf(ARGS(alarm, "%u"), args->secs);
189 #endif
191 if (args->secs > 100000000)
192 return EINVAL;
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;
198 get_mplock();
199 crit_enter();
200 old_it = p->p_realtimer;
201 getmicrouptime(&tv);
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),
206 realitexpire, p);
207 timevaladd(&it.it_value, &tv);
209 p->p_realtimer = it;
210 crit_exit();
211 rel_mplock();
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;
218 return 0;
222 * MPALMOSTSAFE
225 sys_linux_brk(struct linux_brk_args *args)
227 struct thread *td = curthread;
228 struct proc *p = td->td_proc;
229 struct vmspace *vm;
230 vm_offset_t new, old;
231 struct obreak_args bsd_args;
233 get_mplock();
234 vm = p->p_vmspace;
235 #ifdef DEBUG
236 if (ldebug(brk))
237 kprintf(ARGS(brk, "%p"), (void *)args->dsend);
238 #endif
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;
246 else
247 args->sysmsg_result = (long)old;
248 rel_mplock();
250 return 0;
254 * MPALMOSTSAFE
257 sys_linux_uselib(struct linux_uselib_args *args)
259 struct thread *td = curthread;
260 struct proc *p;
261 struct nlookupdata nd;
262 struct vnode *vp;
263 struct exec *a_out;
264 struct vattr attr;
265 vm_offset_t vmaddr;
266 unsigned long file_offset;
267 vm_offset_t buffer;
268 unsigned long bss_size;
269 int error;
270 int locked;
271 char *path;
273 p = td->td_proc;
275 error = linux_copyin_path(args->library, &path, LINUX_PATH_EXISTS);
276 if (error)
277 return (error);
278 #ifdef DEBUG
279 if (ldebug(uselib))
280 kprintf(ARGS(uselib, "%s"), path);
281 #endif
283 a_out = NULL;
284 locked = 0;
285 vp = NULL;
287 get_mplock();
288 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
289 nd.nl_flags |= NLC_EXEC;
290 if (error == 0)
291 error = nlookup(&nd);
292 if (error == 0)
293 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
294 if (error)
295 goto cleanup;
297 * From here on down, we have a locked vnode that must be unlocked.
299 locked = 1;
301 /* Writable? */
302 if (vp->v_writecount) {
303 error = ETXTBSY;
304 goto cleanup;
307 /* Executable? */
308 error = VOP_GETATTR(vp, &attr);
309 if (error)
310 goto cleanup;
312 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
313 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
314 error = ENOEXEC;
315 goto cleanup;
318 /* Sensible size? */
319 if (attr.va_size == 0) {
320 error = ENOEXEC;
321 goto cleanup;
324 error = VOP_OPEN(vp, FREAD, td->td_ucred, NULL);
325 if (error)
326 goto cleanup;
329 * Lock no longer needed
331 vn_unlock(vp);
332 locked = 0;
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);
337 if (error)
338 goto cleanup;
340 /* Is it a Linux binary ? */
341 if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
342 error = ENOEXEC;
343 goto cleanup;
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 */
353 file_offset = 1024;
354 break;
355 case 0314: /* QMAGIC */
356 file_offset = 0;
357 break;
358 default:
359 error = ENOEXEC;
360 goto cleanup;
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) {
367 error = ENOEXEC;
368 goto cleanup;
371 /* text + data can't exceed file size */
372 if (a_out->a_data + a_out->a_text > attr.va_size) {
373 error = EFAULT;
374 goto cleanup;
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) {
384 error = ENOMEM;
385 goto cleanup;
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
394 * (what a waste).
396 if (file_offset & PAGE_MASK) {
397 #ifdef DEBUG
398 kprintf("uselib: Non page aligned binary %lu\n", file_offset);
399 #endif
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,
408 PAGE_SIZE,
409 FALSE, VM_MAPTYPE_NORMAL,
410 VM_PROT_ALL, VM_PROT_ALL,
412 if (error)
413 goto cleanup;
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));
420 if (error)
421 goto cleanup;
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));
431 if (error)
432 goto cleanup;
433 } else {
434 #ifdef DEBUG
435 kprintf("uselib: Page aligned binary %lu\n", file_offset);
436 #endif
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);
450 if (error)
451 goto cleanup;
453 #ifdef DEBUG
454 kprintf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long*)vmaddr)[0],
455 ((long*)vmaddr)[1]);
456 #endif
457 if (bss_size != 0) {
458 /* Calculate BSS start address */
459 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
460 a_out->a_data;
462 /* allocate some 'anon' space */
463 error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0,
464 &vmaddr, bss_size,
465 PAGE_SIZE,
466 FALSE, VM_MAPTYPE_NORMAL,
467 VM_PROT_ALL, VM_PROT_ALL,
469 if (error)
470 goto cleanup;
473 cleanup:
474 /* Unlock/release vnode */
475 if (vp) {
476 if (locked)
477 vn_unlock(vp);
478 vrele(vp);
480 /* Release the kernel mapping. */
481 if (a_out) {
482 vm_map_remove(&kernel_map, (vm_offset_t)a_out,
483 (vm_offset_t)a_out + PAGE_SIZE);
485 nlookup_done(&nd);
486 rel_mplock();
487 linux_free_path(&path);
488 return (error);
492 * MPSAFE
495 sys_linux_select(struct linux_select_args *args)
497 struct select_args bsa;
498 struct timeval tv0, tv1, utv, *tvp;
499 caddr_t sg;
500 int error;
502 #ifdef DEBUG
503 if (ldebug(select))
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);
507 #endif
509 error = 0;
510 bsa.sysmsg_result = 0;
511 bsa.nd = args->nfds;
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
519 * time left.
521 if (args->timeout) {
522 if ((error = copyin((caddr_t)args->timeout, &utv,
523 sizeof(utv))))
524 goto select_out;
525 #ifdef DEBUG
526 if (ldebug(select))
527 kprintf(LMSG("incoming timeout (%ld/%ld)"),
528 utv.tv_sec, utv.tv_usec);
529 #endif
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) {
541 utv.tv_sec -= 1;
542 utv.tv_usec += 1000000;
544 if (utv.tv_sec < 0)
545 timevalclear(&utv);
546 if ((error = copyout(&utv, tvp, sizeof(utv))))
547 goto select_out;
548 bsa.tv = tvp;
550 microtime(&tv0);
553 error = sys_select(&bsa);
554 args->sysmsg_result = bsa.sysmsg_result;
555 #ifdef DEBUG
556 if (ldebug(select))
557 kprintf(LMSG("real select returns %d"), error);
558 #endif
559 if (error) {
561 * See fs/select.c in the Linux kernel. Without this,
562 * Maelstrom doesn't work.
564 if (error == ERESTART)
565 error = EINTR;
566 goto select_out;
569 if (args->timeout) {
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.
577 microtime(&tv1);
578 timevalsub(&tv1, &tv0);
579 timevalsub(&utv, &tv1);
580 if (utv.tv_sec < 0)
581 timevalclear(&utv);
582 } else
583 timevalclear(&utv);
584 #ifdef DEBUG
585 if (ldebug(select))
586 kprintf(LMSG("outgoing timeout (%ld/%ld)"),
587 utv.tv_sec, utv.tv_usec);
588 #endif
589 if ((error = copyout(&utv, (caddr_t)args->timeout,
590 sizeof(utv))))
591 goto select_out;
594 select_out:
595 #ifdef DEBUG
596 if (ldebug(select))
597 kprintf(LMSG("select_out -> %d"), error);
598 #endif
599 return error;
603 * MPSAFE
605 int
606 sys_linux_mremap(struct linux_mremap_args *args)
608 struct munmap_args bsd_args;
609 int error = 0;
611 #ifdef DEBUG
612 if (ldebug(mremap))
613 kprintf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
614 (void *)args->addr,
615 (unsigned long)args->old_len,
616 (unsigned long)args->new_len,
617 (unsigned long)args->flags);
618 #endif
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;
624 return ENOMEM;
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;
635 return error;
638 #define LINUX_MS_ASYNC 0x0001
639 #define LINUX_MS_INVALIDATE 0x0002
640 #define LINUX_MS_SYNC 0x0004
643 * MPSAFE
646 sys_linux_msync(struct linux_msync_args *args)
648 struct msync_args bsd_args;
649 int error;
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;
658 return(error);
662 * MPSAFE
665 sys_linux_time(struct linux_time_args *args)
667 struct timeval tv;
668 l_time_t tm;
669 int error;
671 #ifdef DEBUG
672 if (ldebug(time))
673 kprintf(ARGS(time, "*"));
674 #endif
676 microtime(&tv);
677 tm = tv.tv_sec;
678 if (args->tm && (error = copyout(&tm, (caddr_t)args->tm, sizeof(tm))))
679 return error;
680 args->sysmsg_lresult = tm;
681 return 0;
684 struct l_times_argv {
685 l_long tms_utime;
686 l_long tms_stime;
687 l_long tms_cutime;
688 l_long tms_cstime;
691 #define CLK_TCK 100 /* Linux uses 100 */
693 #define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
696 * MPALMOSTSAFE
699 sys_linux_times(struct linux_times_args *args)
701 struct thread *td = curthread;
702 struct proc *p = td->td_proc;
703 struct timeval tv;
704 struct l_times_argv tms;
705 struct rusage ru;
706 int error;
708 #ifdef DEBUG
709 if (ldebug(times))
710 kprintf(ARGS(times, "*"));
711 #endif
713 get_mplock();
714 calcru_proc(p, &ru);
715 rel_mplock();
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))))
724 return error;
726 microuptime(&tv);
727 args->sysmsg_result = (int)CONVTCK(tv);
728 return 0;
732 * MPALMOSTSAFE
735 sys_linux_newuname(struct linux_newuname_args *args)
737 struct thread *td = curthread;
738 struct l_new_utsname utsname;
739 char *osrelease, *osname;
741 #ifdef DEBUG
742 if (ldebug(newuname))
743 kprintf(ARGS(newuname, "*"));
744 #endif
746 get_mplock();
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);
757 rel_mplock();
759 return (copyout(&utsname, (caddr_t)args->buf, sizeof(utsname)));
762 #if defined(__i386__)
763 struct l_utimbuf {
764 l_time_t l_actime;
765 l_time_t l_modtime;
769 * MPALMOSTSAFE
772 sys_linux_utime(struct linux_utime_args *args)
774 struct timeval tv[2];
775 struct l_utimbuf lut;
776 struct nlookupdata nd;
777 char *path;
778 int error;
780 error = linux_copyin_path(args->fname, &path, LINUX_PATH_EXISTS);
781 if (error)
782 return (error);
783 #ifdef DEBUG
784 if (ldebug(utime))
785 kprintf(ARGS(utime, "%s, *"), path);
786 #endif
788 if (args->times) {
789 error = copyin(args->times, &lut, sizeof(lut));
790 if (error)
791 goto cleanup;
792 tv[0].tv_sec = lut.l_actime;
793 tv[0].tv_usec = 0;
794 tv[1].tv_sec = lut.l_modtime;
795 tv[1].tv_usec = 0;
797 get_mplock();
798 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
799 if (error == 0)
800 error = kern_utimes(&nd, args->times ? tv : NULL);
801 nlookup_done(&nd);
802 rel_mplock();
803 cleanup:
804 linux_free_path(&path);
805 return (error);
807 #endif /* __i386__ */
809 #define __WCLONE 0x80000000
812 * MPALMOSTSAFE
815 sys_linux_waitpid(struct linux_waitpid_args *args)
817 int error, options, status;
819 #ifdef DEBUG
820 if (ldebug(waitpid))
821 kprintf(ARGS(waitpid, "%d, %p, %d"),
822 args->pid, (void *)args->status, args->options);
823 #endif
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) {
833 status &= 0xffff;
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));
843 return (error);
847 * MPALMOSTSAFE
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;
857 #ifdef DEBUG
858 if (ldebug(wait4))
859 kprintf(ARGS(wait4, "%d, %p, %d, %p"),
860 args->pid, (void *)args->status, args->options,
861 (void *)args->rusage);
862 #endif
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);
871 if (error == 0)
872 lwp_delsig(lp, SIGCHLD);
874 if (error == 0 && args->status) {
875 status &= 0xffff;
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));
887 return (error);
891 * MPALMOSTSAFE
894 sys_linux_mknod(struct linux_mknod_args *args)
896 struct nlookupdata nd;
897 char *path;
898 int error;
900 error = linux_copyin_path(args->path, &path, LINUX_PATH_CREATE);
901 if (error)
902 return (error);
903 #ifdef DEBUG
904 if (ldebug(mknod))
905 kprintf(ARGS(mknod, "%s, %d, %d"),
906 path, args->mode, args->dev);
907 #endif
908 get_mplock();
909 error = nlookup_init(&nd, path, UIO_SYSSPACE, 0);
910 if (error == 0) {
911 if (args->mode & S_IFIFO) {
912 error = kern_mkfifo(&nd, args->mode);
913 } else {
914 error = kern_mknod(&nd, args->mode,
915 umajor(args->dev),
916 uminor(args->dev));
919 nlookup_done(&nd);
920 rel_mplock();
922 linux_free_path(&path);
923 return(error);
927 * UGH! This is just about the dumbest idea I've ever heard!!
929 * MPSAFE
932 sys_linux_personality(struct linux_personality_args *args)
934 #ifdef DEBUG
935 if (ldebug(personality))
936 kprintf(ARGS(personality, "%d"), args->per);
937 #endif
938 if (args->per != 0)
939 return EINVAL;
941 /* Yes Jim, it's still a Linux... */
942 args->sysmsg_result = 0;
943 return 0;
947 * Wrappers for get/setitimer for debugging..
949 * MPSAFE
952 sys_linux_setitimer(struct linux_setitimer_args *args)
954 struct setitimer_args bsa;
955 struct itimerval foo;
956 int error;
958 #ifdef DEBUG
959 if (ldebug(setitimer))
960 kprintf(ARGS(setitimer, "%p, %p"),
961 (void *)args->itv, (void *)args->oitv);
962 #endif
963 bsa.which = args->which;
964 bsa.itv = (struct itimerval *)args->itv;
965 bsa.oitv = (struct itimerval *)args->oitv;
966 bsa.sysmsg_result = 0;
967 if (args->itv) {
968 if ((error = copyin((caddr_t)args->itv, &foo, sizeof(foo))))
969 return error;
970 #ifdef DEBUG
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);
977 #endif
979 error = sys_setitimer(&bsa);
980 args->sysmsg_result = bsa.sysmsg_result;
981 return(error);
985 * MPSAFE
988 sys_linux_getitimer(struct linux_getitimer_args *args)
990 struct getitimer_args bsa;
991 int error;
992 #ifdef DEBUG
993 if (ldebug(getitimer))
994 kprintf(ARGS(getitimer, "%p"), (void *)args->itv);
995 #endif
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;
1001 return(error);
1005 * MPSAFE
1008 sys_linux_nice(struct linux_nice_args *args)
1010 struct setpriority_args bsd_args;
1011 int error;
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;
1019 return(error);
1023 * MPALMOSTSAFE
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];
1032 gid_t *bsd_gidset;
1033 int ngrp, error;
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)
1045 return (error);
1047 if ((u_int)ngrp >= NGROUPS)
1048 return (EINVAL);
1050 get_mplock();
1051 newcred = crdup(oldcred);
1052 if (ngrp > 0) {
1053 error = copyin((caddr_t)args->grouplist, linux_gidset,
1054 ngrp * sizeof(l_gid_t));
1055 if (error) {
1056 crfree(newcred);
1057 rel_mplock();
1058 return (error);
1061 newcred->cr_ngroups = ngrp + 1;
1063 bsd_gidset = newcred->cr_groups;
1064 ngrp--;
1065 while (ngrp >= 0) {
1066 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1067 ngrp--;
1069 } else {
1070 newcred->cr_ngroups = 1;
1073 setsugid();
1074 oldcred = p->p_ucred; /* reload, deal with threads race */
1075 p->p_ucred = newcred;
1076 crfree(oldcred);
1077 rel_mplock();
1078 return (0);
1082 * MPSAFE
1085 sys_linux_getgroups(struct linux_getgroups_args *args)
1087 struct thread *td = curthread;
1088 struct ucred *cred;
1089 l_gid_t linux_gidset[NGROUPS];
1090 gid_t *bsd_gidset;
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]
1100 * to prevent that.
1103 if ((ngrp = args->gidsetsize) == 0) {
1104 args->sysmsg_result = bsd_gidsetsz;
1105 return (0);
1108 if ((u_int)ngrp < bsd_gidsetsz)
1109 return (EINVAL);
1111 ngrp = 0;
1112 while (ngrp < bsd_gidsetsz) {
1113 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1114 ngrp++;
1117 if ((error = copyout(linux_gidset, args->grouplist,
1118 ngrp * sizeof(l_gid_t)))) {
1119 return (error);
1122 args->sysmsg_result = ngrp;
1123 return (0);
1127 * MPSAFE
1130 sys_linux_setrlimit(struct linux_setrlimit_args *args)
1132 struct l_rlimit linux_rlim;
1133 struct rlimit rlim;
1134 u_int which;
1135 int error;
1137 #ifdef DEBUG
1138 if (ldebug(setrlimit))
1139 kprintf(ARGS(setrlimit, "%d, %p"),
1140 args->resource, (void *)args->rlim);
1141 #endif
1142 if (args->resource >= LINUX_RLIM_NLIMITS)
1143 return (EINVAL);
1144 which = linux_to_bsd_resource[args->resource];
1145 if (which == -1)
1146 return (EINVAL);
1148 error = copyin(args->rlim, &linux_rlim, sizeof(linux_rlim));
1149 if (error)
1150 return (error);
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);
1156 return(error);
1160 * MPSAFE
1163 sys_linux_old_getrlimit(struct linux_old_getrlimit_args *args)
1165 struct l_rlimit linux_rlim;
1166 struct rlimit rlim;
1167 u_int which;
1168 int error;
1170 #ifdef DEBUG
1171 if (ldebug(old_getrlimit))
1172 kprintf(ARGS(old_getrlimit, "%d, %p"),
1173 args->resource, (void *)args->rlim);
1174 #endif
1175 if (args->resource >= LINUX_RLIM_NLIMITS)
1176 return (EINVAL);
1177 which = linux_to_bsd_resource[args->resource];
1178 if (which == -1)
1179 return (EINVAL);
1181 error = kern_getrlimit(which, &rlim);
1183 if (error == 0) {
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));
1192 return (error);
1196 * MPSAFE
1199 sys_linux_getrlimit(struct linux_getrlimit_args *args)
1201 struct l_rlimit linux_rlim;
1202 struct rlimit rlim;
1203 u_int which;
1204 int error;
1206 #ifdef DEBUG
1207 if (ldebug(getrlimit))
1208 kprintf(ARGS(getrlimit, "%d, %p"),
1209 args->resource, (void *)args->rlim);
1210 #endif
1211 if (args->resource >= LINUX_RLIM_NLIMITS)
1212 return (EINVAL);
1213 which = linux_to_bsd_resource[args->resource];
1214 if (which == -1)
1215 return (EINVAL);
1217 error = kern_getrlimit(which, &rlim);
1219 if (error == 0) {
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));
1224 return (error);
1228 * MPSAFE
1231 sys_linux_sched_setscheduler(struct linux_sched_setscheduler_args *args)
1233 struct sched_setscheduler_args bsd;
1234 int error;
1236 #ifdef DEBUG
1237 if (ldebug(sched_setscheduler))
1238 kprintf(ARGS(sched_setscheduler, "%d, %d, %p"),
1239 args->pid, args->policy, (const void *)args->param);
1240 #endif
1242 switch (args->policy) {
1243 case LINUX_SCHED_OTHER:
1244 bsd.policy = SCHED_OTHER;
1245 break;
1246 case LINUX_SCHED_FIFO:
1247 bsd.policy = SCHED_FIFO;
1248 break;
1249 case LINUX_SCHED_RR:
1250 bsd.policy = SCHED_RR;
1251 break;
1252 default:
1253 return EINVAL;
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;
1262 return(error);
1266 * MPSAFE
1269 sys_linux_sched_getscheduler(struct linux_sched_getscheduler_args *args)
1271 struct sched_getscheduler_args bsd;
1272 int error;
1274 #ifdef DEBUG
1275 if (ldebug(sched_getscheduler))
1276 kprintf(ARGS(sched_getscheduler, "%d"), args->pid);
1277 #endif
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) {
1285 case SCHED_OTHER:
1286 args->sysmsg_result = LINUX_SCHED_OTHER;
1287 break;
1288 case SCHED_FIFO:
1289 args->sysmsg_result = LINUX_SCHED_FIFO;
1290 break;
1291 case SCHED_RR:
1292 args->sysmsg_result = LINUX_SCHED_RR;
1293 break;
1295 return error;
1299 * MPSAFE
1302 sys_linux_sched_get_priority_max(struct linux_sched_get_priority_max_args *args)
1304 struct sched_get_priority_max_args bsd;
1305 int error;
1307 #ifdef DEBUG
1308 if (ldebug(sched_get_priority_max))
1309 kprintf(ARGS(sched_get_priority_max, "%d"), args->policy);
1310 #endif
1312 switch (args->policy) {
1313 case LINUX_SCHED_OTHER:
1314 bsd.policy = SCHED_OTHER;
1315 break;
1316 case LINUX_SCHED_FIFO:
1317 bsd.policy = SCHED_FIFO;
1318 break;
1319 case LINUX_SCHED_RR:
1320 bsd.policy = SCHED_RR;
1321 break;
1322 default:
1323 return EINVAL;
1325 bsd.sysmsg_result = 0;
1327 error = sys_sched_get_priority_max(&bsd);
1328 args->sysmsg_result = bsd.sysmsg_result;
1329 return(error);
1333 * MPSAFE
1336 sys_linux_sched_get_priority_min(struct linux_sched_get_priority_min_args *args)
1338 struct sched_get_priority_min_args bsd;
1339 int error;
1341 #ifdef DEBUG
1342 if (ldebug(sched_get_priority_min))
1343 kprintf(ARGS(sched_get_priority_min, "%d"), args->policy);
1344 #endif
1346 switch (args->policy) {
1347 case LINUX_SCHED_OTHER:
1348 bsd.policy = SCHED_OTHER;
1349 break;
1350 case LINUX_SCHED_FIFO:
1351 bsd.policy = SCHED_FIFO;
1352 break;
1353 case LINUX_SCHED_RR:
1354 bsd.policy = SCHED_RR;
1355 break;
1356 default:
1357 return EINVAL;
1359 bsd.sysmsg_result = 0;
1361 error = sys_sched_get_priority_min(&bsd);
1362 args->sysmsg_result = bsd.sysmsg_result;
1363 return(error);
1366 #define REBOOT_CAD_ON 0x89abcdef
1367 #define REBOOT_CAD_OFF 0
1368 #define REBOOT_HALT 0xcdef0123
1371 * MPSAFE
1374 sys_linux_reboot(struct linux_reboot_args *args)
1376 struct reboot_args bsd_args;
1377 int error;
1379 #ifdef DEBUG
1380 if (ldebug(reboot))
1381 kprintf(ARGS(reboot, "0x%x"), args->cmd);
1382 #endif
1383 if (args->cmd == REBOOT_CAD_ON || args->cmd == REBOOT_CAD_OFF)
1384 return (0);
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;
1390 return(error);
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
1406 * MPSAFE
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;
1415 return (0);
1419 * MPSAFE
1422 sys_linux_getgid(struct linux_getgid_args *args)
1424 struct thread *td = curthread;
1426 args->sysmsg_result = td->td_ucred->cr_rgid;
1427 return (0);
1431 * MPSAFE
1434 sys_linux_getuid(struct linux_getuid_args *args)
1436 struct thread *td = curthread;
1438 args->sysmsg_result = td->td_ucred->cr_ruid;
1439 return (0);
1443 * MPSAFE
1446 sys_linux_getsid(struct linux_getsid_args *args)
1448 struct getsid_args bsd;
1449 int error;
1451 bsd.sysmsg_result = 0;
1452 bsd.pid = args->pid;
1453 error = sys_getsid(&bsd);
1454 args->sysmsg_result = bsd.sysmsg_result;
1455 return(error);
1459 * MPSAFE
1462 linux_nosys(struct nosys_args *args)
1464 /* XXX */
1465 return (ENOSYS);