kernel - fine-grained namecache and partial vnode MPSAFE work
[dragonfly.git] / sys / emulation / linux / linux_misc.c
blobd282a65edfe68f34dc1fecc8ad7986ab714a57e2
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->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;
160 } else {
161 sysinfo.totalswap = swapblist->bl_blocks * 1024;
162 sysinfo.freeswap = swapblist->bl_root->u.bmu_avail * PAGE_SIZE;
164 rel_mplock();
166 sysinfo.procs = 20; /* Hack */
168 error = copyout(&sysinfo, (caddr_t)args->info, sizeof(sysinfo));
169 return (error);
173 * MPALMOSTSAFE
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;
181 struct timeval tv;
183 #ifdef DEBUG
184 if (ldebug(alarm))
185 kprintf(ARGS(alarm, "%u"), args->secs);
186 #endif
188 if (args->secs > 100000000)
189 return EINVAL;
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;
195 get_mplock();
196 crit_enter();
197 old_it = p->p_realtimer;
198 getmicrouptime(&tv);
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),
203 realitexpire, p);
204 timevaladd(&it.it_value, &tv);
206 p->p_realtimer = it;
207 crit_exit();
208 rel_mplock();
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;
215 return 0;
219 * MPALMOSTSAFE
222 sys_linux_brk(struct linux_brk_args *args)
224 struct thread *td = curthread;
225 struct proc *p = td->td_proc;
226 struct vmspace *vm;
227 vm_offset_t new, old;
228 struct obreak_args bsd_args;
230 get_mplock();
231 vm = p->p_vmspace;
232 #ifdef DEBUG
233 if (ldebug(brk))
234 kprintf(ARGS(brk, "%p"), (void *)args->dsend);
235 #endif
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;
243 else
244 args->sysmsg_result = (long)old;
245 rel_mplock();
247 return 0;
251 * MPALMOSTSAFE
254 sys_linux_uselib(struct linux_uselib_args *args)
256 struct thread *td = curthread;
257 struct proc *p;
258 struct nlookupdata nd;
259 struct vnode *vp;
260 struct exec *a_out;
261 struct vattr attr;
262 vm_offset_t vmaddr;
263 unsigned long file_offset;
264 vm_offset_t buffer;
265 unsigned long bss_size;
266 int error;
267 int locked;
268 char *path;
270 p = td->td_proc;
272 error = linux_copyin_path(args->library, &path, LINUX_PATH_EXISTS);
273 if (error)
274 return (error);
275 #ifdef DEBUG
276 if (ldebug(uselib))
277 kprintf(ARGS(uselib, "%s"), path);
278 #endif
280 a_out = NULL;
281 locked = 0;
282 vp = NULL;
284 get_mplock();
285 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
286 nd.nl_flags |= NLC_EXEC;
287 if (error == 0)
288 error = nlookup(&nd);
289 if (error == 0)
290 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
291 if (error)
292 goto cleanup;
294 * From here on down, we have a locked vnode that must be unlocked.
296 locked = 1;
298 /* Writable? */
299 if (vp->v_writecount) {
300 error = ETXTBSY;
301 goto cleanup;
304 /* Executable? */
305 error = VOP_GETATTR(vp, &attr);
306 if (error)
307 goto cleanup;
309 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
310 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
311 error = ENOEXEC;
312 goto cleanup;
315 /* Sensible size? */
316 if (attr.va_size == 0) {
317 error = ENOEXEC;
318 goto cleanup;
321 error = VOP_OPEN(vp, FREAD, td->td_ucred, NULL);
322 if (error)
323 goto cleanup;
326 * Lock no longer needed
328 vn_unlock(vp);
329 locked = 0;
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);
334 if (error)
335 goto cleanup;
337 /* Is it a Linux binary ? */
338 if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
339 error = ENOEXEC;
340 goto cleanup;
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 */
350 file_offset = 1024;
351 break;
352 case 0314: /* QMAGIC */
353 file_offset = 0;
354 break;
355 default:
356 error = ENOEXEC;
357 goto cleanup;
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) {
364 error = ENOEXEC;
365 goto cleanup;
368 /* text + data can't exceed file size */
369 if (a_out->a_data + a_out->a_text > attr.va_size) {
370 error = EFAULT;
371 goto cleanup;
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) {
381 error = ENOMEM;
382 goto cleanup;
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
391 * (what a waste).
393 if (file_offset & PAGE_MASK) {
394 #ifdef DEBUG
395 kprintf("uselib: Non page aligned binary %lu\n", file_offset);
396 #endif
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,
405 FALSE,
406 VM_MAPTYPE_NORMAL,
407 VM_PROT_ALL, VM_PROT_ALL,
409 if (error)
410 goto cleanup;
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));
417 if (error)
418 goto cleanup;
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));
428 if (error)
429 goto cleanup;
430 } else {
431 #ifdef DEBUG
432 kprintf("uselib: Page aligned binary %lu\n", file_offset);
433 #endif
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);
447 if (error)
448 goto cleanup;
450 #ifdef DEBUG
451 kprintf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long*)vmaddr)[0],
452 ((long*)vmaddr)[1]);
453 #endif
454 if (bss_size != 0) {
455 /* Calculate BSS start address */
456 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
457 a_out->a_data;
459 /* allocate some 'anon' space */
460 error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0,
461 &vmaddr, bss_size,
462 FALSE,
463 VM_MAPTYPE_NORMAL,
464 VM_PROT_ALL, VM_PROT_ALL,
466 if (error)
467 goto cleanup;
470 cleanup:
471 /* Unlock/release vnode */
472 if (vp) {
473 if (locked)
474 vn_unlock(vp);
475 vrele(vp);
477 /* Release the kernel mapping. */
478 if (a_out) {
479 vm_map_remove(&kernel_map, (vm_offset_t)a_out,
480 (vm_offset_t)a_out + PAGE_SIZE);
482 nlookup_done(&nd);
483 rel_mplock();
484 linux_free_path(&path);
485 return (error);
489 * MPSAFE
492 sys_linux_select(struct linux_select_args *args)
494 struct select_args bsa;
495 struct timeval tv0, tv1, utv, *tvp;
496 caddr_t sg;
497 int error;
499 #ifdef DEBUG
500 if (ldebug(select))
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);
504 #endif
506 error = 0;
507 bsa.sysmsg_result = 0;
508 bsa.nd = args->nfds;
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
516 * time left.
518 if (args->timeout) {
519 if ((error = copyin((caddr_t)args->timeout, &utv,
520 sizeof(utv))))
521 goto select_out;
522 #ifdef DEBUG
523 if (ldebug(select))
524 kprintf(LMSG("incoming timeout (%ld/%ld)"),
525 utv.tv_sec, utv.tv_usec);
526 #endif
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) {
538 utv.tv_sec -= 1;
539 utv.tv_usec += 1000000;
541 if (utv.tv_sec < 0)
542 timevalclear(&utv);
543 if ((error = copyout(&utv, tvp, sizeof(utv))))
544 goto select_out;
545 bsa.tv = tvp;
547 microtime(&tv0);
550 error = sys_select(&bsa);
551 args->sysmsg_result = bsa.sysmsg_result;
552 #ifdef DEBUG
553 if (ldebug(select))
554 kprintf(LMSG("real select returns %d"), error);
555 #endif
556 if (error) {
558 * See fs/select.c in the Linux kernel. Without this,
559 * Maelstrom doesn't work.
561 if (error == ERESTART)
562 error = EINTR;
563 goto select_out;
566 if (args->timeout) {
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.
574 microtime(&tv1);
575 timevalsub(&tv1, &tv0);
576 timevalsub(&utv, &tv1);
577 if (utv.tv_sec < 0)
578 timevalclear(&utv);
579 } else
580 timevalclear(&utv);
581 #ifdef DEBUG
582 if (ldebug(select))
583 kprintf(LMSG("outgoing timeout (%ld/%ld)"),
584 utv.tv_sec, utv.tv_usec);
585 #endif
586 if ((error = copyout(&utv, (caddr_t)args->timeout,
587 sizeof(utv))))
588 goto select_out;
591 select_out:
592 #ifdef DEBUG
593 if (ldebug(select))
594 kprintf(LMSG("select_out -> %d"), error);
595 #endif
596 return error;
600 * MPSAFE
602 int
603 sys_linux_mremap(struct linux_mremap_args *args)
605 struct munmap_args bsd_args;
606 int error = 0;
608 #ifdef DEBUG
609 if (ldebug(mremap))
610 kprintf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
611 (void *)args->addr,
612 (unsigned long)args->old_len,
613 (unsigned long)args->new_len,
614 (unsigned long)args->flags);
615 #endif
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;
621 return ENOMEM;
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;
632 return error;
635 #define LINUX_MS_ASYNC 0x0001
636 #define LINUX_MS_INVALIDATE 0x0002
637 #define LINUX_MS_SYNC 0x0004
640 * MPSAFE
643 sys_linux_msync(struct linux_msync_args *args)
645 struct msync_args bsd_args;
646 int error;
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;
655 return(error);
659 * MPSAFE
662 sys_linux_time(struct linux_time_args *args)
664 struct timeval tv;
665 l_time_t tm;
666 int error;
668 #ifdef DEBUG
669 if (ldebug(time))
670 kprintf(ARGS(time, "*"));
671 #endif
673 microtime(&tv);
674 tm = tv.tv_sec;
675 if (args->tm && (error = copyout(&tm, (caddr_t)args->tm, sizeof(tm))))
676 return error;
677 args->sysmsg_lresult = tm;
678 return 0;
681 struct l_times_argv {
682 l_long tms_utime;
683 l_long tms_stime;
684 l_long tms_cutime;
685 l_long tms_cstime;
688 #define CLK_TCK 100 /* Linux uses 100 */
690 #define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
693 * MPALMOSTSAFE
696 sys_linux_times(struct linux_times_args *args)
698 struct thread *td = curthread;
699 struct proc *p = td->td_proc;
700 struct timeval tv;
701 struct l_times_argv tms;
702 struct rusage ru;
703 int error;
705 #ifdef DEBUG
706 if (ldebug(times))
707 kprintf(ARGS(times, "*"));
708 #endif
710 get_mplock();
711 calcru_proc(p, &ru);
712 rel_mplock();
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))))
721 return error;
723 microuptime(&tv);
724 args->sysmsg_result = (int)CONVTCK(tv);
725 return 0;
729 * MPALMOSTSAFE
732 sys_linux_newuname(struct linux_newuname_args *args)
734 struct thread *td = curthread;
735 struct l_new_utsname utsname;
736 char *osrelease, *osname;
738 #ifdef DEBUG
739 if (ldebug(newuname))
740 kprintf(ARGS(newuname, "*"));
741 #endif
743 get_mplock();
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);
754 rel_mplock();
756 return (copyout(&utsname, (caddr_t)args->buf, sizeof(utsname)));
759 #if defined(__i386__)
760 struct l_utimbuf {
761 l_time_t l_actime;
762 l_time_t l_modtime;
766 * MPALMOSTSAFE
769 sys_linux_utime(struct linux_utime_args *args)
771 struct timeval tv[2];
772 struct l_utimbuf lut;
773 struct nlookupdata nd;
774 char *path;
775 int error;
777 error = linux_copyin_path(args->fname, &path, LINUX_PATH_EXISTS);
778 if (error)
779 return (error);
780 #ifdef DEBUG
781 if (ldebug(utime))
782 kprintf(ARGS(utime, "%s, *"), path);
783 #endif
785 if (args->times) {
786 error = copyin(args->times, &lut, sizeof(lut));
787 if (error)
788 goto cleanup;
789 tv[0].tv_sec = lut.l_actime;
790 tv[0].tv_usec = 0;
791 tv[1].tv_sec = lut.l_modtime;
792 tv[1].tv_usec = 0;
794 get_mplock();
795 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
796 if (error == 0)
797 error = kern_utimes(&nd, args->times ? tv : NULL);
798 nlookup_done(&nd);
799 rel_mplock();
800 cleanup:
801 linux_free_path(&path);
802 return (error);
804 #endif /* __i386__ */
806 #define __WCLONE 0x80000000
809 * MPALMOSTSAFE
812 sys_linux_waitpid(struct linux_waitpid_args *args)
814 int error, options, status;
816 #ifdef DEBUG
817 if (ldebug(waitpid))
818 kprintf(ARGS(waitpid, "%d, %p, %d"),
819 args->pid, (void *)args->status, args->options);
820 #endif
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) {
830 status &= 0xffff;
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));
840 return (error);
844 * MPALMOSTSAFE
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;
854 #ifdef DEBUG
855 if (ldebug(wait4))
856 kprintf(ARGS(wait4, "%d, %p, %d, %p"),
857 args->pid, (void *)args->status, args->options,
858 (void *)args->rusage);
859 #endif
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);
868 if (error == 0)
869 lwp_delsig(lp, SIGCHLD);
871 if (error == 0 && args->status) {
872 status &= 0xffff;
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));
884 return (error);
888 * MPALMOSTSAFE
891 sys_linux_mknod(struct linux_mknod_args *args)
893 struct nlookupdata nd;
894 char *path;
895 int error;
897 error = linux_copyin_path(args->path, &path, LINUX_PATH_CREATE);
898 if (error)
899 return (error);
900 #ifdef DEBUG
901 if (ldebug(mknod))
902 kprintf(ARGS(mknod, "%s, %d, %d"),
903 path, args->mode, args->dev);
904 #endif
905 get_mplock();
906 error = nlookup_init(&nd, path, UIO_SYSSPACE, 0);
907 if (error == 0) {
908 if (args->mode & S_IFIFO) {
909 error = kern_mkfifo(&nd, args->mode);
910 } else {
911 error = kern_mknod(&nd, args->mode,
912 umajor(args->dev),
913 uminor(args->dev));
916 nlookup_done(&nd);
917 rel_mplock();
919 linux_free_path(&path);
920 return(error);
924 * UGH! This is just about the dumbest idea I've ever heard!!
926 * MPSAFE
929 sys_linux_personality(struct linux_personality_args *args)
931 #ifdef DEBUG
932 if (ldebug(personality))
933 kprintf(ARGS(personality, "%d"), args->per);
934 #endif
935 if (args->per != 0)
936 return EINVAL;
938 /* Yes Jim, it's still a Linux... */
939 args->sysmsg_result = 0;
940 return 0;
944 * Wrappers for get/setitimer for debugging..
946 * MPSAFE
949 sys_linux_setitimer(struct linux_setitimer_args *args)
951 struct setitimer_args bsa;
952 struct itimerval foo;
953 int error;
955 #ifdef DEBUG
956 if (ldebug(setitimer))
957 kprintf(ARGS(setitimer, "%p, %p"),
958 (void *)args->itv, (void *)args->oitv);
959 #endif
960 bsa.which = args->which;
961 bsa.itv = (struct itimerval *)args->itv;
962 bsa.oitv = (struct itimerval *)args->oitv;
963 bsa.sysmsg_result = 0;
964 if (args->itv) {
965 if ((error = copyin((caddr_t)args->itv, &foo, sizeof(foo))))
966 return error;
967 #ifdef DEBUG
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);
974 #endif
976 error = sys_setitimer(&bsa);
977 args->sysmsg_result = bsa.sysmsg_result;
978 return(error);
982 * MPSAFE
985 sys_linux_getitimer(struct linux_getitimer_args *args)
987 struct getitimer_args bsa;
988 int error;
989 #ifdef DEBUG
990 if (ldebug(getitimer))
991 kprintf(ARGS(getitimer, "%p"), (void *)args->itv);
992 #endif
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;
998 return(error);
1002 * MPSAFE
1005 sys_linux_nice(struct linux_nice_args *args)
1007 struct setpriority_args bsd_args;
1008 int error;
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;
1016 return(error);
1020 * MPALMOSTSAFE
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];
1029 gid_t *bsd_gidset;
1030 int ngrp, error;
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)
1042 return (error);
1044 if ((u_int)ngrp >= NGROUPS)
1045 return (EINVAL);
1047 get_mplock();
1048 newcred = crdup(oldcred);
1049 if (ngrp > 0) {
1050 error = copyin((caddr_t)args->grouplist, linux_gidset,
1051 ngrp * sizeof(l_gid_t));
1052 if (error) {
1053 crfree(newcred);
1054 rel_mplock();
1055 return (error);
1058 newcred->cr_ngroups = ngrp + 1;
1060 bsd_gidset = newcred->cr_groups;
1061 ngrp--;
1062 while (ngrp >= 0) {
1063 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1064 ngrp--;
1066 } else {
1067 newcred->cr_ngroups = 1;
1070 setsugid();
1071 oldcred = p->p_ucred; /* reload, deal with threads race */
1072 p->p_ucred = newcred;
1073 crfree(oldcred);
1074 rel_mplock();
1075 return (0);
1079 * MPSAFE
1082 sys_linux_getgroups(struct linux_getgroups_args *args)
1084 struct thread *td = curthread;
1085 struct ucred *cred;
1086 l_gid_t linux_gidset[NGROUPS];
1087 gid_t *bsd_gidset;
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]
1097 * to prevent that.
1100 if ((ngrp = args->gidsetsize) == 0) {
1101 args->sysmsg_result = bsd_gidsetsz;
1102 return (0);
1105 if ((u_int)ngrp < bsd_gidsetsz)
1106 return (EINVAL);
1108 ngrp = 0;
1109 while (ngrp < bsd_gidsetsz) {
1110 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1111 ngrp++;
1114 if ((error = copyout(linux_gidset, args->grouplist,
1115 ngrp * sizeof(l_gid_t)))) {
1116 return (error);
1119 args->sysmsg_result = ngrp;
1120 return (0);
1124 * MPSAFE
1127 sys_linux_setrlimit(struct linux_setrlimit_args *args)
1129 struct l_rlimit linux_rlim;
1130 struct rlimit rlim;
1131 u_int which;
1132 int error;
1134 #ifdef DEBUG
1135 if (ldebug(setrlimit))
1136 kprintf(ARGS(setrlimit, "%d, %p"),
1137 args->resource, (void *)args->rlim);
1138 #endif
1139 if (args->resource >= LINUX_RLIM_NLIMITS)
1140 return (EINVAL);
1141 which = linux_to_bsd_resource[args->resource];
1142 if (which == -1)
1143 return (EINVAL);
1145 error = copyin(args->rlim, &linux_rlim, sizeof(linux_rlim));
1146 if (error)
1147 return (error);
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);
1153 return(error);
1157 * MPSAFE
1160 sys_linux_old_getrlimit(struct linux_old_getrlimit_args *args)
1162 struct l_rlimit linux_rlim;
1163 struct rlimit rlim;
1164 u_int which;
1165 int error;
1167 #ifdef DEBUG
1168 if (ldebug(old_getrlimit))
1169 kprintf(ARGS(old_getrlimit, "%d, %p"),
1170 args->resource, (void *)args->rlim);
1171 #endif
1172 if (args->resource >= LINUX_RLIM_NLIMITS)
1173 return (EINVAL);
1174 which = linux_to_bsd_resource[args->resource];
1175 if (which == -1)
1176 return (EINVAL);
1178 error = kern_getrlimit(which, &rlim);
1180 if (error == 0) {
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));
1189 return (error);
1193 * MPSAFE
1196 sys_linux_getrlimit(struct linux_getrlimit_args *args)
1198 struct l_rlimit linux_rlim;
1199 struct rlimit rlim;
1200 u_int which;
1201 int error;
1203 #ifdef DEBUG
1204 if (ldebug(getrlimit))
1205 kprintf(ARGS(getrlimit, "%d, %p"),
1206 args->resource, (void *)args->rlim);
1207 #endif
1208 if (args->resource >= LINUX_RLIM_NLIMITS)
1209 return (EINVAL);
1210 which = linux_to_bsd_resource[args->resource];
1211 if (which == -1)
1212 return (EINVAL);
1214 error = kern_getrlimit(which, &rlim);
1216 if (error == 0) {
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));
1221 return (error);
1225 * MPSAFE
1228 sys_linux_sched_setscheduler(struct linux_sched_setscheduler_args *args)
1230 struct sched_setscheduler_args bsd;
1231 int error;
1233 #ifdef DEBUG
1234 if (ldebug(sched_setscheduler))
1235 kprintf(ARGS(sched_setscheduler, "%d, %d, %p"),
1236 args->pid, args->policy, (const void *)args->param);
1237 #endif
1239 switch (args->policy) {
1240 case LINUX_SCHED_OTHER:
1241 bsd.policy = SCHED_OTHER;
1242 break;
1243 case LINUX_SCHED_FIFO:
1244 bsd.policy = SCHED_FIFO;
1245 break;
1246 case LINUX_SCHED_RR:
1247 bsd.policy = SCHED_RR;
1248 break;
1249 default:
1250 return EINVAL;
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;
1259 return(error);
1263 * MPSAFE
1266 sys_linux_sched_getscheduler(struct linux_sched_getscheduler_args *args)
1268 struct sched_getscheduler_args bsd;
1269 int error;
1271 #ifdef DEBUG
1272 if (ldebug(sched_getscheduler))
1273 kprintf(ARGS(sched_getscheduler, "%d"), args->pid);
1274 #endif
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) {
1282 case SCHED_OTHER:
1283 args->sysmsg_result = LINUX_SCHED_OTHER;
1284 break;
1285 case SCHED_FIFO:
1286 args->sysmsg_result = LINUX_SCHED_FIFO;
1287 break;
1288 case SCHED_RR:
1289 args->sysmsg_result = LINUX_SCHED_RR;
1290 break;
1292 return error;
1296 * MPSAFE
1299 sys_linux_sched_get_priority_max(struct linux_sched_get_priority_max_args *args)
1301 struct sched_get_priority_max_args bsd;
1302 int error;
1304 #ifdef DEBUG
1305 if (ldebug(sched_get_priority_max))
1306 kprintf(ARGS(sched_get_priority_max, "%d"), args->policy);
1307 #endif
1309 switch (args->policy) {
1310 case LINUX_SCHED_OTHER:
1311 bsd.policy = SCHED_OTHER;
1312 break;
1313 case LINUX_SCHED_FIFO:
1314 bsd.policy = SCHED_FIFO;
1315 break;
1316 case LINUX_SCHED_RR:
1317 bsd.policy = SCHED_RR;
1318 break;
1319 default:
1320 return EINVAL;
1322 bsd.sysmsg_result = 0;
1324 error = sys_sched_get_priority_max(&bsd);
1325 args->sysmsg_result = bsd.sysmsg_result;
1326 return(error);
1330 * MPSAFE
1333 sys_linux_sched_get_priority_min(struct linux_sched_get_priority_min_args *args)
1335 struct sched_get_priority_min_args bsd;
1336 int error;
1338 #ifdef DEBUG
1339 if (ldebug(sched_get_priority_min))
1340 kprintf(ARGS(sched_get_priority_min, "%d"), args->policy);
1341 #endif
1343 switch (args->policy) {
1344 case LINUX_SCHED_OTHER:
1345 bsd.policy = SCHED_OTHER;
1346 break;
1347 case LINUX_SCHED_FIFO:
1348 bsd.policy = SCHED_FIFO;
1349 break;
1350 case LINUX_SCHED_RR:
1351 bsd.policy = SCHED_RR;
1352 break;
1353 default:
1354 return EINVAL;
1356 bsd.sysmsg_result = 0;
1358 error = sys_sched_get_priority_min(&bsd);
1359 args->sysmsg_result = bsd.sysmsg_result;
1360 return(error);
1363 #define REBOOT_CAD_ON 0x89abcdef
1364 #define REBOOT_CAD_OFF 0
1365 #define REBOOT_HALT 0xcdef0123
1368 * MPSAFE
1371 sys_linux_reboot(struct linux_reboot_args *args)
1373 struct reboot_args bsd_args;
1374 int error;
1376 #ifdef DEBUG
1377 if (ldebug(reboot))
1378 kprintf(ARGS(reboot, "0x%x"), args->cmd);
1379 #endif
1380 if (args->cmd == REBOOT_CAD_ON || args->cmd == REBOOT_CAD_OFF)
1381 return (0);
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;
1387 return(error);
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
1403 * MPSAFE
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;
1412 return (0);
1416 * MPSAFE
1419 sys_linux_getgid(struct linux_getgid_args *args)
1421 struct thread *td = curthread;
1423 args->sysmsg_result = td->td_ucred->cr_rgid;
1424 return (0);
1428 * MPSAFE
1431 sys_linux_getuid(struct linux_getuid_args *args)
1433 struct thread *td = curthread;
1435 args->sysmsg_result = td->td_ucred->cr_ruid;
1436 return (0);
1440 * MPSAFE
1443 sys_linux_getsid(struct linux_getsid_args *args)
1445 struct getsid_args bsd;
1446 int error;
1448 bsd.sysmsg_result = 0;
1449 bsd.pid = args->pid;
1450 error = sys_getsid(&bsd);
1451 args->sysmsg_result = bsd.sysmsg_result;
1452 return(error);
1456 * MPSAFE
1459 linux_nosys(struct nosys_args *args)
1461 /* XXX */
1462 return (ENOSYS);