linux emulation - Major update
[dragonfly.git] / sys / emulation / linux / linux_misc.c
blob124d8409d85bfa88323382208c4db1cf14c319c8
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"
88 #include "linux_emuldata.h"
89 #include "i386/linux.h"
91 #define BSD_TO_LINUX_SIGNAL(sig) \
92 (((sig) <= LINUX_SIGTBLSZ) ? bsd_to_linux_signal[_SIG_IDX(sig)] : sig)
94 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
95 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
96 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
97 RLIMIT_MEMLOCK, -1
100 struct l_sysinfo {
101 l_long uptime; /* Seconds since boot */
102 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */
103 l_ulong totalram; /* Total usable main memory size */
104 l_ulong freeram; /* Available memory size */
105 l_ulong sharedram; /* Amount of shared memory */
106 l_ulong bufferram; /* Memory used by buffers */
107 l_ulong totalswap; /* Total swap space size */
108 l_ulong freeswap; /* swap space still available */
109 l_ushort procs; /* Number of current processes */
110 char _f[22]; /* Pads structure to 64 bytes */
114 sys_linux_madvise(struct linux_madvise_args *args)
116 return 0;
120 * MPALMOSTSAFE
123 sys_linux_sysinfo(struct linux_sysinfo_args *args)
125 struct l_sysinfo sysinfo;
126 vm_object_t object;
127 struct timespec ts;
128 int error;
129 int i;
131 /* Uptime is copied out of print_uptime() in kern_shutdown.c */
132 getnanouptime(&ts);
133 i = 0;
134 if (ts.tv_sec >= 86400) {
135 ts.tv_sec %= 86400;
136 i = 1;
138 if (i || ts.tv_sec >= 3600) {
139 ts.tv_sec %= 3600;
140 i = 1;
142 if (i || ts.tv_sec >= 60) {
143 ts.tv_sec %= 60;
144 i = 1;
146 sysinfo.uptime=ts.tv_sec;
148 /* Use the information from the mib to get our load averages */
149 for (i = 0; i < 3; i++)
150 sysinfo.loads[i] = averunnable.ldavg[i];
152 sysinfo.totalram = Maxmem * PAGE_SIZE;
153 sysinfo.freeram = sysinfo.totalram - vmstats.v_wire_count * PAGE_SIZE;
155 get_mplock();
156 sysinfo.sharedram = 0;
157 for (object = TAILQ_FIRST(&vm_object_list); object != NULL;
158 object = TAILQ_NEXT(object, object_list)) {
159 if (object->type == OBJT_MARKER)
160 continue;
161 if (object->shadow_count > 1)
162 sysinfo.sharedram += object->resident_page_count;
165 sysinfo.sharedram *= PAGE_SIZE;
166 sysinfo.bufferram = 0;
168 if (swapblist == NULL) {
169 sysinfo.totalswap= 0;
170 sysinfo.freeswap = 0;
171 } else {
172 sysinfo.totalswap = swapblist->bl_blocks * 1024;
173 sysinfo.freeswap = swapblist->bl_root->u.bmu_avail * PAGE_SIZE;
175 rel_mplock();
177 sysinfo.procs = 20; /* Hack */
179 error = copyout(&sysinfo, (caddr_t)args->info, sizeof(sysinfo));
180 return (error);
184 * MPALMOSTSAFE
187 sys_linux_alarm(struct linux_alarm_args *args)
189 struct thread *td = curthread;
190 struct proc *p = td->td_proc;
191 struct itimerval it, old_it;
192 struct timeval tv;
194 #ifdef DEBUG
195 if (ldebug(alarm))
196 kprintf(ARGS(alarm, "%u"), args->secs);
197 #endif
199 if (args->secs > 100000000)
200 return EINVAL;
202 it.it_value.tv_sec = (long)args->secs;
203 it.it_value.tv_usec = 0;
204 it.it_interval.tv_sec = 0;
205 it.it_interval.tv_usec = 0;
206 get_mplock();
207 crit_enter();
208 old_it = p->p_realtimer;
209 getmicrouptime(&tv);
210 if (timevalisset(&old_it.it_value))
211 callout_stop(&p->p_ithandle);
212 if (it.it_value.tv_sec != 0) {
213 callout_reset(&p->p_ithandle, tvtohz_high(&it.it_value),
214 realitexpire, p);
215 timevaladd(&it.it_value, &tv);
217 p->p_realtimer = it;
218 crit_exit();
219 rel_mplock();
220 if (timevalcmp(&old_it.it_value, &tv, >)) {
221 timevalsub(&old_it.it_value, &tv);
222 if (old_it.it_value.tv_usec != 0)
223 old_it.it_value.tv_sec++;
224 args->sysmsg_result = old_it.it_value.tv_sec;
226 return 0;
230 * MPALMOSTSAFE
233 sys_linux_brk(struct linux_brk_args *args)
235 struct thread *td = curthread;
236 struct proc *p = td->td_proc;
237 struct vmspace *vm;
238 vm_offset_t new, old;
239 struct obreak_args bsd_args;
241 get_mplock();
242 vm = p->p_vmspace;
243 #ifdef DEBUG
244 if (ldebug(brk))
245 kprintf(ARGS(brk, "%p"), (void *)args->dsend);
246 #endif
247 old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
248 new = (vm_offset_t)args->dsend;
249 bsd_args.sysmsg_result = 0;
250 bsd_args.nsize = (char *) new;
251 bsd_args.sysmsg_result = 0;
252 if (((caddr_t)new > vm->vm_daddr) && !sys_obreak(&bsd_args))
253 args->sysmsg_result = (long)new;
254 else
255 args->sysmsg_result = (long)old;
256 rel_mplock();
258 return 0;
262 * MPALMOSTSAFE
265 sys_linux_uselib(struct linux_uselib_args *args)
267 struct thread *td = curthread;
268 struct proc *p;
269 struct nlookupdata nd;
270 struct vnode *vp;
271 struct exec *a_out;
272 struct vattr attr;
273 vm_offset_t vmaddr;
274 unsigned long file_offset;
275 vm_offset_t buffer;
276 unsigned long bss_size;
277 int error;
278 int locked;
279 char *path;
281 p = td->td_proc;
283 error = linux_copyin_path(args->library, &path, LINUX_PATH_EXISTS);
284 if (error)
285 return (error);
286 #ifdef DEBUG
287 if (ldebug(uselib))
288 kprintf(ARGS(uselib, "%s"), path);
289 #endif
291 a_out = NULL;
292 locked = 0;
293 vp = NULL;
295 get_mplock();
296 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
297 nd.nl_flags |= NLC_EXEC;
298 if (error == 0)
299 error = nlookup(&nd);
300 if (error == 0)
301 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
302 if (error)
303 goto cleanup;
305 * From here on down, we have a locked vnode that must be unlocked.
307 locked = 1;
309 /* Writable? */
310 if (vp->v_writecount) {
311 error = ETXTBSY;
312 goto cleanup;
315 /* Executable? */
316 error = VOP_GETATTR(vp, &attr);
317 if (error)
318 goto cleanup;
320 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
321 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
322 error = ENOEXEC;
323 goto cleanup;
326 /* Sensible size? */
327 if (attr.va_size == 0) {
328 error = ENOEXEC;
329 goto cleanup;
332 error = VOP_OPEN(vp, FREAD, td->td_ucred, NULL);
333 if (error)
334 goto cleanup;
337 * Lock no longer needed
339 vn_unlock(vp);
340 locked = 0;
342 /* Pull in executable header into kernel_map */
343 error = vm_mmap(&kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE,
344 VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp, 0);
345 if (error)
346 goto cleanup;
348 /* Is it a Linux binary ? */
349 if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
350 error = ENOEXEC;
351 goto cleanup;
355 * While we are here, we should REALLY do some more checks
358 /* Set file/virtual offset based on a.out variant. */
359 switch ((int)(a_out->a_magic & 0xffff)) {
360 case 0413: /* ZMAGIC */
361 file_offset = 1024;
362 break;
363 case 0314: /* QMAGIC */
364 file_offset = 0;
365 break;
366 default:
367 error = ENOEXEC;
368 goto cleanup;
371 bss_size = round_page(a_out->a_bss);
373 /* Check various fields in header for validity/bounds. */
374 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
375 error = ENOEXEC;
376 goto cleanup;
379 /* text + data can't exceed file size */
380 if (a_out->a_data + a_out->a_text > attr.va_size) {
381 error = EFAULT;
382 goto cleanup;
386 * text/data/bss must not exceed limits
387 * XXX - this is not complete. it should check current usage PLUS
388 * the resources needed by this library.
390 if (a_out->a_text > maxtsiz ||
391 a_out->a_data + bss_size > p->p_rlimit[RLIMIT_DATA].rlim_cur) {
392 error = ENOMEM;
393 goto cleanup;
396 /* prevent more writers */
397 vsetflags(vp, VTEXT);
400 * Check if file_offset page aligned. Currently we cannot handle
401 * misalinged file offsets, and so we read in the entire image
402 * (what a waste).
404 if (file_offset & PAGE_MASK) {
405 #ifdef DEBUG
406 kprintf("uselib: Non page aligned binary %lu\n", file_offset);
407 #endif
408 /* Map text+data read/write/execute */
410 /* a_entry is the load address and is page aligned */
411 vmaddr = trunc_page(a_out->a_entry);
413 /* get anon user mapping, read+write+execute */
414 error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0,
415 &vmaddr, a_out->a_text + a_out->a_data,
416 PAGE_SIZE,
417 FALSE, VM_MAPTYPE_NORMAL,
418 VM_PROT_ALL, VM_PROT_ALL,
420 if (error)
421 goto cleanup;
423 /* map file into kernel_map */
424 error = vm_mmap(&kernel_map, &buffer,
425 round_page(a_out->a_text + a_out->a_data + file_offset),
426 VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp,
427 trunc_page(file_offset));
428 if (error)
429 goto cleanup;
431 /* copy from kernel VM space to user space */
432 error = copyout((caddr_t)(uintptr_t)(buffer + file_offset),
433 (caddr_t)vmaddr, a_out->a_text + a_out->a_data);
435 /* release temporary kernel space */
436 vm_map_remove(&kernel_map, buffer, buffer +
437 round_page(a_out->a_text + a_out->a_data + file_offset));
439 if (error)
440 goto cleanup;
441 } else {
442 #ifdef DEBUG
443 kprintf("uselib: Page aligned binary %lu\n", file_offset);
444 #endif
446 * for QMAGIC, a_entry is 20 bytes beyond the load address
447 * to skip the executable header
449 vmaddr = trunc_page(a_out->a_entry);
452 * Map it all into the process's space as a single
453 * copy-on-write "data" segment.
455 error = vm_mmap(&p->p_vmspace->vm_map, &vmaddr,
456 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
457 MAP_PRIVATE | MAP_FIXED, (caddr_t)vp, file_offset);
458 if (error)
459 goto cleanup;
461 #ifdef DEBUG
462 kprintf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long*)vmaddr)[0],
463 ((long*)vmaddr)[1]);
464 #endif
465 if (bss_size != 0) {
466 /* Calculate BSS start address */
467 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
468 a_out->a_data;
470 /* allocate some 'anon' space */
471 error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0,
472 &vmaddr, bss_size,
473 PAGE_SIZE,
474 FALSE, VM_MAPTYPE_NORMAL,
475 VM_PROT_ALL, VM_PROT_ALL,
477 if (error)
478 goto cleanup;
481 cleanup:
482 /* Unlock/release vnode */
483 if (vp) {
484 if (locked)
485 vn_unlock(vp);
486 vrele(vp);
488 /* Release the kernel mapping. */
489 if (a_out) {
490 vm_map_remove(&kernel_map, (vm_offset_t)a_out,
491 (vm_offset_t)a_out + PAGE_SIZE);
493 nlookup_done(&nd);
494 rel_mplock();
495 linux_free_path(&path);
496 return (error);
500 * MPSAFE
503 sys_linux_select(struct linux_select_args *args)
505 struct select_args bsa;
506 struct timeval tv0, tv1, utv, *tvp;
507 caddr_t sg;
508 int error;
510 #ifdef DEBUG
511 if (ldebug(select))
512 kprintf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
513 (void *)args->readfds, (void *)args->writefds,
514 (void *)args->exceptfds, (void *)args->timeout);
515 #endif
517 error = 0;
518 bsa.sysmsg_result = 0;
519 bsa.nd = args->nfds;
520 bsa.in = args->readfds;
521 bsa.ou = args->writefds;
522 bsa.ex = args->exceptfds;
523 bsa.tv = (struct timeval *)args->timeout;
526 * Store current time for computation of the amount of
527 * time left.
529 if (args->timeout) {
530 if ((error = copyin((caddr_t)args->timeout, &utv,
531 sizeof(utv))))
532 goto select_out;
533 #ifdef DEBUG
534 if (ldebug(select))
535 kprintf(LMSG("incoming timeout (%ld/%ld)"),
536 utv.tv_sec, utv.tv_usec);
537 #endif
539 if (itimerfix(&utv)) {
541 * The timeval was invalid. Convert it to something
542 * valid that will act as it does under Linux.
544 sg = stackgap_init();
545 tvp = stackgap_alloc(&sg, sizeof(utv));
546 utv.tv_sec += utv.tv_usec / 1000000;
547 utv.tv_usec %= 1000000;
548 if (utv.tv_usec < 0) {
549 utv.tv_sec -= 1;
550 utv.tv_usec += 1000000;
552 if (utv.tv_sec < 0)
553 timevalclear(&utv);
554 if ((error = copyout(&utv, tvp, sizeof(utv))))
555 goto select_out;
556 bsa.tv = tvp;
558 microtime(&tv0);
561 error = sys_select(&bsa);
562 args->sysmsg_result = bsa.sysmsg_result;
563 #ifdef DEBUG
564 if (ldebug(select))
565 kprintf(LMSG("real select returns %d"), error);
566 #endif
567 if (error) {
569 * See fs/select.c in the Linux kernel. Without this,
570 * Maelstrom doesn't work.
572 if (error == ERESTART)
573 error = EINTR;
574 goto select_out;
577 if (args->timeout) {
578 if (args->sysmsg_result) {
580 * Compute how much time was left of the timeout,
581 * by subtracting the current time and the time
582 * before we started the call, and subtracting
583 * that result from the user-supplied value.
585 microtime(&tv1);
586 timevalsub(&tv1, &tv0);
587 timevalsub(&utv, &tv1);
588 if (utv.tv_sec < 0)
589 timevalclear(&utv);
590 } else
591 timevalclear(&utv);
592 #ifdef DEBUG
593 if (ldebug(select))
594 kprintf(LMSG("outgoing timeout (%ld/%ld)"),
595 utv.tv_sec, utv.tv_usec);
596 #endif
597 if ((error = copyout(&utv, (caddr_t)args->timeout,
598 sizeof(utv))))
599 goto select_out;
602 select_out:
603 #ifdef DEBUG
604 if (ldebug(select))
605 kprintf(LMSG("select_out -> %d"), error);
606 #endif
607 return error;
611 * MPSAFE
613 int
614 sys_linux_mremap(struct linux_mremap_args *args)
616 struct munmap_args bsd_args;
617 int error = 0;
619 #ifdef DEBUG
620 if (ldebug(mremap))
621 kprintf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
622 (void *)args->addr,
623 (unsigned long)args->old_len,
624 (unsigned long)args->new_len,
625 (unsigned long)args->flags);
626 #endif
627 if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) {
628 args->sysmsg_resultp = NULL;
629 return (EINVAL);
633 * Check for the page alignment.
634 * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK.
636 if (args->addr & PAGE_MASK) {
637 args->sysmsg_resultp = NULL;
638 return (EINVAL);
641 args->new_len = round_page(args->new_len);
642 args->old_len = round_page(args->old_len);
644 if (args->new_len > args->old_len) {
645 args->sysmsg_result = 0;
646 return ENOMEM;
649 if (args->new_len < args->old_len) {
650 bsd_args.sysmsg_result = 0;
651 bsd_args.addr = (caddr_t)(args->addr + args->new_len);
652 bsd_args.len = args->old_len - args->new_len;
653 error = sys_munmap(&bsd_args);
656 args->sysmsg_resultp = error ? NULL : (void *)args->addr;
657 return error;
660 #define LINUX_MS_ASYNC 0x0001
661 #define LINUX_MS_INVALIDATE 0x0002
662 #define LINUX_MS_SYNC 0x0004
665 * MPSAFE
668 sys_linux_msync(struct linux_msync_args *args)
670 struct msync_args bsd_args;
671 int error;
673 bsd_args.addr = (caddr_t)args->addr;
674 bsd_args.len = args->len;
675 bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
676 bsd_args.sysmsg_result = 0;
678 error = sys_msync(&bsd_args);
679 args->sysmsg_result = bsd_args.sysmsg_result;
680 return(error);
684 * MPSAFE
687 sys_linux_time(struct linux_time_args *args)
689 struct timeval tv;
690 l_time_t tm;
691 int error;
693 #ifdef DEBUG
694 if (ldebug(time))
695 kprintf(ARGS(time, "*"));
696 #endif
698 microtime(&tv);
699 tm = tv.tv_sec;
700 if (args->tm && (error = copyout(&tm, (caddr_t)args->tm, sizeof(tm))))
701 return error;
702 args->sysmsg_lresult = tm;
703 return 0;
706 struct l_times_argv {
707 l_long tms_utime;
708 l_long tms_stime;
709 l_long tms_cutime;
710 l_long tms_cstime;
713 #define CLK_TCK 100 /* Linux uses 100 */
715 #define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
718 * MPALMOSTSAFE
721 sys_linux_times(struct linux_times_args *args)
723 struct thread *td = curthread;
724 struct proc *p = td->td_proc;
725 struct timeval tv;
726 struct l_times_argv tms;
727 struct rusage ru;
728 int error;
730 #ifdef DEBUG
731 if (ldebug(times))
732 kprintf(ARGS(times, "*"));
733 #endif
735 get_mplock();
736 calcru_proc(p, &ru);
737 rel_mplock();
739 tms.tms_utime = CONVTCK(ru.ru_utime);
740 tms.tms_stime = CONVTCK(ru.ru_stime);
742 tms.tms_cutime = CONVTCK(p->p_cru.ru_utime);
743 tms.tms_cstime = CONVTCK(p->p_cru.ru_stime);
745 if ((error = copyout(&tms, (caddr_t)args->buf, sizeof(tms))))
746 return error;
748 microuptime(&tv);
749 args->sysmsg_result = (int)CONVTCK(tv);
750 return 0;
754 * MPALMOSTSAFE
757 sys_linux_newuname(struct linux_newuname_args *args)
759 struct thread *td = curthread;
760 struct l_new_utsname utsname;
761 char *osrelease, *osname;
763 #ifdef DEBUG
764 if (ldebug(newuname))
765 kprintf(ARGS(newuname, "*"));
766 #endif
768 get_mplock();
769 osname = linux_get_osname(td);
770 osrelease = linux_get_osrelease(td);
772 bzero(&utsname, sizeof(utsname));
773 strncpy(utsname.sysname, osname, LINUX_MAX_UTSNAME-1);
774 strncpy(utsname.nodename, hostname, LINUX_MAX_UTSNAME-1);
775 strncpy(utsname.release, osrelease, LINUX_MAX_UTSNAME-1);
776 strncpy(utsname.version, version, LINUX_MAX_UTSNAME-1);
777 strncpy(utsname.machine, machine, LINUX_MAX_UTSNAME-1);
778 strncpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME-1);
779 rel_mplock();
781 return (copyout(&utsname, (caddr_t)args->buf, sizeof(utsname)));
784 /* XXX: why would this be i386-only? most of these are wrong! */
785 #if defined(__i386__)
786 struct l_utimbuf {
787 l_time_t l_actime;
788 l_time_t l_modtime;
792 * MPALMOSTSAFE
795 sys_linux_utime(struct linux_utime_args *args)
797 struct timeval tv[2];
798 struct l_utimbuf lut;
799 struct nlookupdata nd;
800 char *path;
801 int error;
803 error = linux_copyin_path(args->fname, &path, LINUX_PATH_EXISTS);
804 if (error)
805 return (error);
806 #ifdef DEBUG
807 if (ldebug(utime))
808 kprintf(ARGS(utime, "%s, *"), path);
809 #endif
811 if (args->times) {
812 error = copyin(args->times, &lut, sizeof(lut));
813 if (error)
814 goto cleanup;
815 tv[0].tv_sec = lut.l_actime;
816 tv[0].tv_usec = 0;
817 tv[1].tv_sec = lut.l_modtime;
818 tv[1].tv_usec = 0;
820 get_mplock();
821 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
822 if (error == 0)
823 error = kern_utimes(&nd, args->times ? tv : NULL);
824 nlookup_done(&nd);
825 rel_mplock();
826 cleanup:
827 linux_free_path(&path);
828 return (error);
832 sys_linux_utimes(struct linux_utimes_args *args)
834 l_timeval ltv[2];
835 struct timeval tv[2], *tvp = NULL;
836 struct nlookupdata nd;
837 char *path;
838 int error;
840 error = linux_copyin_path(args->fname, &path, LINUX_PATH_EXISTS);
841 if (error)
842 return (error);
843 #ifdef DEBUG
844 if (ldebug(utimes))
845 kprintf(ARGS(utimes, "%s, *"), path);
846 #endif
848 if (args->tptr) {
849 error = copyin(args->tptr, ltv, sizeof(ltv));
850 if (error)
851 goto cleanup;
852 tv[0].tv_sec = ltv[0].tv_sec;
853 tv[0].tv_usec = ltv[0].tv_usec;
854 tv[1].tv_sec = ltv[1].tv_sec;
855 tv[1].tv_usec = ltv[1].tv_usec;
856 tvp = tv;
858 get_mplock();
859 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
860 if (error == 0)
861 error = kern_utimes(&nd, tvp);
862 nlookup_done(&nd);
863 rel_mplock();
864 cleanup:
865 linux_free_path(&path);
866 return (error);
870 sys_linux_futimesat(struct linux_futimesat_args *args)
872 l_timeval ltv[2];
873 struct timeval tv[2], *tvp = NULL;
874 struct file *fp;
875 struct nlookupdata nd;
876 char *path;
877 int dfd,error;
879 error = linux_copyin_path(args->fname, &path, LINUX_PATH_EXISTS);
880 if (error)
881 return (error);
882 #ifdef DEBUG
883 if (ldebug(futimesat))
884 kprintf(ARGS(futimesat, "%s, *"), path);
885 #endif
886 if (args->tptr) {
887 error = copyin(args->tptr, ltv, sizeof(ltv));
888 if (error)
889 goto cleanup;
890 tv[0].tv_sec = ltv[0].tv_sec;
891 tv[0].tv_usec = ltv[0].tv_usec;
892 tv[1].tv_sec = ltv[1].tv_sec;
893 tv[1].tv_usec = ltv[1].tv_usec;
894 tvp = tv;
896 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
897 get_mplock();
898 error = nlookup_init_at(&nd, &fp, dfd, path, UIO_SYSSPACE, NLC_FOLLOW);
899 if (error == 0)
900 error = kern_utimes(&nd, tvp);
901 nlookup_done_at(&nd, fp);
902 rel_mplock();
903 cleanup:
904 linux_free_path(&path);
905 return (error);
910 sys_linux_utimensat(struct linux_utimensat_args *args)
912 struct l_timespec ltv[2];
913 struct timeval tv[2], *tvp = NULL;
914 struct file *fp;
915 struct nlookupdata nd;
916 char *path;
917 int dfd, flags, error = 0;
919 if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
920 return (EINVAL);
922 if (args->dfd == LINUX_AT_FDCWD && args->fname == NULL)
923 return (EINVAL);
925 if (args->fname) {
926 error = linux_copyin_path(args->fname, &path, LINUX_PATH_EXISTS);
927 if (error)
928 return (error);
930 #ifdef DEBUG
931 if (ldebug(utimensat))
932 kprintf(ARGS(utimensat, "%s, *"), path);
933 #endif
934 if (args->tptr) {
935 error = copyin(args->tptr, ltv, sizeof(ltv));
936 if (error)
937 goto cleanup;
939 if (ltv[0].tv_sec == LINUX_UTIME_NOW) {
940 microtime(&tv[0]);
941 } else if (ltv[0].tv_sec == LINUX_UTIME_OMIT) {
942 /* XXX: this is not right, but will do for now */
943 microtime(&tv[0]);
944 } else {
945 tv[0].tv_sec = ltv[0].tv_sec;
946 /* XXX: we lose precision here, as we don't have ns */
947 tv[0].tv_usec = ltv[0].tv_nsec/1000;
949 if (ltv[1].tv_sec == LINUX_UTIME_NOW) {
950 microtime(&tv[1]);
951 } else if (ltv[1].tv_sec == LINUX_UTIME_OMIT) {
952 /* XXX: this is not right, but will do for now */
953 microtime(&tv[1]);
954 } else {
955 tv[1].tv_sec = ltv[1].tv_sec;
956 /* XXX: we lose precision here, as we don't have ns */
957 tv[1].tv_usec = ltv[1].tv_nsec/1000;
959 tvp = tv;
962 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
963 flags = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
965 get_mplock();
966 if (args->fname) {
967 error = nlookup_init_at(&nd, &fp, dfd, path, UIO_SYSSPACE, flags);
968 if (error == 0)
969 error = kern_utimes(&nd, tvp);
970 nlookup_done_at(&nd, fp);
971 } else {
972 /* Thank you, Linux, for another non-standard "feature" */
973 KKASSERT(dfd != AT_FDCWD);
974 error = kern_futimes(dfd, tvp);
976 rel_mplock();
977 cleanup:
978 if (args->fname)
979 linux_free_path(&path);
981 return (error);
983 #endif /* __i386__ */
985 #define __WCLONE 0x80000000
988 * MPALMOSTSAFE
991 sys_linux_waitpid(struct linux_waitpid_args *args)
993 int error, options, status;
995 #ifdef DEBUG
996 if (ldebug(waitpid))
997 kprintf(ARGS(waitpid, "%d, %p, %d"),
998 args->pid, (void *)args->status, args->options);
999 #endif
1000 options = args->options & (WNOHANG | WUNTRACED);
1001 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
1002 if (args->options & __WCLONE)
1003 options |= WLINUXCLONE;
1005 error = kern_wait(args->pid, args->status ? &status : NULL, options,
1006 NULL, &args->sysmsg_result);
1008 if (error == 0 && args->status) {
1009 status &= 0xffff;
1010 if (WIFSIGNALED(status))
1011 status = (status & 0xffffff80) |
1012 BSD_TO_LINUX_SIGNAL(WTERMSIG(status));
1013 else if (WIFSTOPPED(status))
1014 status = (status & 0xffff00ff) |
1015 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(status)) << 8);
1016 error = copyout(&status, args->status, sizeof(status));
1019 return (error);
1023 * MPALMOSTSAFE
1026 sys_linux_wait4(struct linux_wait4_args *args)
1028 struct thread *td = curthread;
1029 struct lwp *lp = td->td_lwp;
1030 struct rusage rusage;
1031 int error, options, status;
1033 #ifdef DEBUG
1034 if (ldebug(wait4))
1035 kprintf(ARGS(wait4, "%d, %p, %d, %p"),
1036 args->pid, (void *)args->status, args->options,
1037 (void *)args->rusage);
1038 #endif
1039 options = args->options & (WNOHANG | WUNTRACED);
1040 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
1041 if (args->options & __WCLONE)
1042 options |= WLINUXCLONE;
1044 error = kern_wait(args->pid, args->status ? &status : NULL, options,
1045 args->rusage ? &rusage : NULL, &args->sysmsg_result);
1047 if (error == 0)
1048 lwp_delsig(lp, SIGCHLD);
1050 if (error == 0 && args->status) {
1051 status &= 0xffff;
1052 if (WIFSIGNALED(status))
1053 status = (status & 0xffffff80) |
1054 BSD_TO_LINUX_SIGNAL(WTERMSIG(status));
1055 else if (WIFSTOPPED(status))
1056 status = (status & 0xffff00ff) |
1057 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(status)) << 8);
1058 error = copyout(&status, args->status, sizeof(status));
1060 if (error == 0 && args->rusage)
1061 error = copyout(&rusage, args->rusage, sizeof(rusage));
1063 return (error);
1067 * MPALMOSTSAFE
1070 sys_linux_mknod(struct linux_mknod_args *args)
1072 struct nlookupdata nd;
1073 char *path;
1074 int error;
1076 error = linux_copyin_path(args->path, &path, LINUX_PATH_CREATE);
1077 if (error)
1078 return (error);
1079 #ifdef DEBUG
1080 if (ldebug(mknod))
1081 kprintf(ARGS(mknod, "%s, %d, %d"),
1082 path, args->mode, args->dev);
1083 #endif
1084 get_mplock();
1085 error = nlookup_init(&nd, path, UIO_SYSSPACE, 0);
1086 if (error == 0) {
1087 if (args->mode & S_IFIFO) {
1088 error = kern_mkfifo(&nd, args->mode);
1089 } else {
1090 error = kern_mknod(&nd, args->mode,
1091 umajor(args->dev),
1092 uminor(args->dev));
1095 nlookup_done(&nd);
1096 rel_mplock();
1098 linux_free_path(&path);
1099 return(error);
1103 sys_linux_mknodat(struct linux_mknodat_args *args)
1105 struct nlookupdata nd;
1106 struct file *fp;
1107 char *path;
1108 int dfd, error;
1110 error = linux_copyin_path(args->path, &path, LINUX_PATH_CREATE);
1111 if (error)
1112 return (error);
1113 #ifdef DEBUG
1114 if (ldebug(mknod))
1115 kprintf(ARGS(mknod, "%s, %d, %d"),
1116 path, args->mode, args->dev);
1117 #endif
1118 get_mplock();
1119 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1120 error = nlookup_init_at(&nd, &fp, dfd, path, UIO_SYSSPACE, 0);
1121 if (error == 0) {
1122 if (args->mode & S_IFIFO) {
1123 error = kern_mkfifo(&nd, args->mode);
1124 } else {
1125 error = kern_mknod(&nd, args->mode,
1126 umajor(args->dev),
1127 uminor(args->dev));
1130 nlookup_done_at(&nd, fp);
1131 rel_mplock();
1133 linux_free_path(&path);
1134 return(error);
1138 * UGH! This is just about the dumbest idea I've ever heard!!
1140 * MPSAFE
1143 sys_linux_personality(struct linux_personality_args *args)
1145 #ifdef DEBUG
1146 if (ldebug(personality))
1147 kprintf(ARGS(personality, "%d"), args->per);
1148 #endif
1149 if (args->per != 0)
1150 return EINVAL;
1152 /* Yes Jim, it's still a Linux... */
1153 args->sysmsg_result = 0;
1154 return 0;
1158 * Wrappers for get/setitimer for debugging..
1160 * MPSAFE
1163 sys_linux_setitimer(struct linux_setitimer_args *args)
1165 struct setitimer_args bsa;
1166 struct itimerval foo;
1167 int error;
1169 #ifdef DEBUG
1170 if (ldebug(setitimer))
1171 kprintf(ARGS(setitimer, "%p, %p"),
1172 (void *)args->itv, (void *)args->oitv);
1173 #endif
1174 bsa.which = args->which;
1175 bsa.itv = (struct itimerval *)args->itv;
1176 bsa.oitv = (struct itimerval *)args->oitv;
1177 bsa.sysmsg_result = 0;
1178 if (args->itv) {
1179 if ((error = copyin((caddr_t)args->itv, &foo, sizeof(foo))))
1180 return error;
1181 #ifdef DEBUG
1182 if (ldebug(setitimer)) {
1183 kprintf("setitimer: value: sec: %ld, usec: %ld\n",
1184 foo.it_value.tv_sec, foo.it_value.tv_usec);
1185 kprintf("setitimer: interval: sec: %ld, usec: %ld\n",
1186 foo.it_interval.tv_sec, foo.it_interval.tv_usec);
1188 #endif
1190 error = sys_setitimer(&bsa);
1191 args->sysmsg_result = bsa.sysmsg_result;
1192 return(error);
1196 * MPSAFE
1199 sys_linux_getitimer(struct linux_getitimer_args *args)
1201 struct getitimer_args bsa;
1202 int error;
1203 #ifdef DEBUG
1204 if (ldebug(getitimer))
1205 kprintf(ARGS(getitimer, "%p"), (void *)args->itv);
1206 #endif
1207 bsa.which = args->which;
1208 bsa.itv = (struct itimerval *)args->itv;
1209 bsa.sysmsg_result = 0;
1210 error = sys_getitimer(&bsa);
1211 args->sysmsg_result = bsa.sysmsg_result;
1212 return(error);
1216 * MPSAFE
1219 sys_linux_nice(struct linux_nice_args *args)
1221 struct setpriority_args bsd_args;
1222 int error;
1224 bsd_args.which = PRIO_PROCESS;
1225 bsd_args.who = 0; /* current process */
1226 bsd_args.prio = args->inc;
1227 bsd_args.sysmsg_result = 0;
1228 error = sys_setpriority(&bsd_args);
1229 args->sysmsg_result = bsd_args.sysmsg_result;
1230 return(error);
1234 * MPALMOSTSAFE
1237 sys_linux_setgroups(struct linux_setgroups_args *args)
1239 struct thread *td = curthread;
1240 struct proc *p = td->td_proc;
1241 struct ucred *newcred, *oldcred;
1242 l_gid_t linux_gidset[NGROUPS];
1243 gid_t *bsd_gidset;
1244 int ngrp, error;
1246 ngrp = args->gidsetsize;
1247 oldcred = td->td_ucred;
1250 * cr_groups[0] holds egid. Setting the whole set from
1251 * the supplied set will cause egid to be changed too.
1252 * Keep cr_groups[0] unchanged to prevent that.
1255 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0)
1256 return (error);
1258 if ((u_int)ngrp >= NGROUPS)
1259 return (EINVAL);
1261 get_mplock();
1262 newcred = crdup(oldcred);
1263 if (ngrp > 0) {
1264 error = copyin((caddr_t)args->grouplist, linux_gidset,
1265 ngrp * sizeof(l_gid_t));
1266 if (error) {
1267 crfree(newcred);
1268 rel_mplock();
1269 return (error);
1272 newcred->cr_ngroups = ngrp + 1;
1274 bsd_gidset = newcred->cr_groups;
1275 ngrp--;
1276 while (ngrp >= 0) {
1277 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1278 ngrp--;
1280 } else {
1281 newcred->cr_ngroups = 1;
1284 setsugid();
1285 oldcred = p->p_ucred; /* reload, deal with threads race */
1286 p->p_ucred = newcred;
1287 crfree(oldcred);
1288 rel_mplock();
1289 return (0);
1293 * MPSAFE
1296 sys_linux_getgroups(struct linux_getgroups_args *args)
1298 struct thread *td = curthread;
1299 struct ucred *cred;
1300 l_gid_t linux_gidset[NGROUPS];
1301 gid_t *bsd_gidset;
1302 int bsd_gidsetsz, ngrp, error;
1304 cred = td->td_ucred;
1305 bsd_gidset = cred->cr_groups;
1306 bsd_gidsetsz = cred->cr_ngroups - 1;
1309 * cr_groups[0] holds egid. Returning the whole set
1310 * here will cause a duplicate. Exclude cr_groups[0]
1311 * to prevent that.
1314 if ((ngrp = args->gidsetsize) == 0) {
1315 args->sysmsg_result = bsd_gidsetsz;
1316 return (0);
1319 if ((u_int)ngrp < bsd_gidsetsz)
1320 return (EINVAL);
1322 ngrp = 0;
1323 while (ngrp < bsd_gidsetsz) {
1324 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1325 ngrp++;
1328 if ((error = copyout(linux_gidset, args->grouplist,
1329 ngrp * sizeof(l_gid_t)))) {
1330 return (error);
1333 args->sysmsg_result = ngrp;
1334 return (0);
1338 * MPSAFE
1341 sys_linux_setrlimit(struct linux_setrlimit_args *args)
1343 struct l_rlimit linux_rlim;
1344 struct rlimit rlim;
1345 u_int which;
1346 int error;
1348 #ifdef DEBUG
1349 if (ldebug(setrlimit))
1350 kprintf(ARGS(setrlimit, "%d, %p"),
1351 args->resource, (void *)args->rlim);
1352 #endif
1353 if (args->resource >= LINUX_RLIM_NLIMITS)
1354 return (EINVAL);
1355 which = linux_to_bsd_resource[args->resource];
1356 if (which == -1)
1357 return (EINVAL);
1359 error = copyin(args->rlim, &linux_rlim, sizeof(linux_rlim));
1360 if (error)
1361 return (error);
1362 rlim.rlim_cur = (rlim_t)linux_rlim.rlim_cur;
1363 rlim.rlim_max = (rlim_t)linux_rlim.rlim_max;
1365 error = kern_setrlimit(which, &rlim);
1367 return(error);
1371 * MPSAFE
1374 sys_linux_old_getrlimit(struct linux_old_getrlimit_args *args)
1376 struct l_rlimit linux_rlim;
1377 struct rlimit rlim;
1378 u_int which;
1379 int error;
1381 #ifdef DEBUG
1382 if (ldebug(old_getrlimit))
1383 kprintf(ARGS(old_getrlimit, "%d, %p"),
1384 args->resource, (void *)args->rlim);
1385 #endif
1386 if (args->resource >= LINUX_RLIM_NLIMITS)
1387 return (EINVAL);
1388 which = linux_to_bsd_resource[args->resource];
1389 if (which == -1)
1390 return (EINVAL);
1392 error = kern_getrlimit(which, &rlim);
1394 if (error == 0) {
1395 linux_rlim.rlim_cur = (l_ulong)rlim.rlim_cur;
1396 if (linux_rlim.rlim_cur == ULONG_MAX)
1397 linux_rlim.rlim_cur = LONG_MAX;
1398 linux_rlim.rlim_max = (l_ulong)rlim.rlim_max;
1399 if (linux_rlim.rlim_max == ULONG_MAX)
1400 linux_rlim.rlim_max = LONG_MAX;
1401 error = copyout(&linux_rlim, args->rlim, sizeof(linux_rlim));
1403 return (error);
1407 * MPSAFE
1410 sys_linux_getrlimit(struct linux_getrlimit_args *args)
1412 struct l_rlimit linux_rlim;
1413 struct rlimit rlim;
1414 u_int which;
1415 int error;
1417 #ifdef DEBUG
1418 if (ldebug(getrlimit))
1419 kprintf(ARGS(getrlimit, "%d, %p"),
1420 args->resource, (void *)args->rlim);
1421 #endif
1422 if (args->resource >= LINUX_RLIM_NLIMITS)
1423 return (EINVAL);
1424 which = linux_to_bsd_resource[args->resource];
1425 if (which == -1)
1426 return (EINVAL);
1428 error = kern_getrlimit(which, &rlim);
1430 if (error == 0) {
1431 linux_rlim.rlim_cur = (l_ulong)rlim.rlim_cur;
1432 linux_rlim.rlim_max = (l_ulong)rlim.rlim_max;
1433 error = copyout(&linux_rlim, args->rlim, sizeof(linux_rlim));
1435 return (error);
1439 * MPSAFE
1442 sys_linux_sched_setscheduler(struct linux_sched_setscheduler_args *args)
1444 struct sched_setscheduler_args bsd;
1445 int error;
1447 #ifdef DEBUG
1448 if (ldebug(sched_setscheduler))
1449 kprintf(ARGS(sched_setscheduler, "%d, %d, %p"),
1450 args->pid, args->policy, (const void *)args->param);
1451 #endif
1453 switch (args->policy) {
1454 case LINUX_SCHED_OTHER:
1455 bsd.policy = SCHED_OTHER;
1456 break;
1457 case LINUX_SCHED_FIFO:
1458 bsd.policy = SCHED_FIFO;
1459 break;
1460 case LINUX_SCHED_RR:
1461 bsd.policy = SCHED_RR;
1462 break;
1463 default:
1464 return EINVAL;
1467 bsd.pid = args->pid;
1468 bsd.param = (struct sched_param *)args->param;
1469 bsd.sysmsg_result = 0;
1471 error = sys_sched_setscheduler(&bsd);
1472 args->sysmsg_result = bsd.sysmsg_result;
1473 return(error);
1477 * MPSAFE
1480 sys_linux_sched_getscheduler(struct linux_sched_getscheduler_args *args)
1482 struct sched_getscheduler_args bsd;
1483 int error;
1485 #ifdef DEBUG
1486 if (ldebug(sched_getscheduler))
1487 kprintf(ARGS(sched_getscheduler, "%d"), args->pid);
1488 #endif
1490 bsd.sysmsg_result = 0;
1491 bsd.pid = args->pid;
1492 error = sys_sched_getscheduler(&bsd);
1493 args->sysmsg_result = bsd.sysmsg_result;
1495 switch (args->sysmsg_result) {
1496 case SCHED_OTHER:
1497 args->sysmsg_result = LINUX_SCHED_OTHER;
1498 break;
1499 case SCHED_FIFO:
1500 args->sysmsg_result = LINUX_SCHED_FIFO;
1501 break;
1502 case SCHED_RR:
1503 args->sysmsg_result = LINUX_SCHED_RR;
1504 break;
1506 return error;
1510 * MPSAFE
1513 sys_linux_sched_get_priority_max(struct linux_sched_get_priority_max_args *args)
1515 struct sched_get_priority_max_args bsd;
1516 int error;
1518 #ifdef DEBUG
1519 if (ldebug(sched_get_priority_max))
1520 kprintf(ARGS(sched_get_priority_max, "%d"), args->policy);
1521 #endif
1523 switch (args->policy) {
1524 case LINUX_SCHED_OTHER:
1525 bsd.policy = SCHED_OTHER;
1526 break;
1527 case LINUX_SCHED_FIFO:
1528 bsd.policy = SCHED_FIFO;
1529 break;
1530 case LINUX_SCHED_RR:
1531 bsd.policy = SCHED_RR;
1532 break;
1533 default:
1534 return EINVAL;
1536 bsd.sysmsg_result = 0;
1538 error = sys_sched_get_priority_max(&bsd);
1539 args->sysmsg_result = bsd.sysmsg_result;
1540 return(error);
1544 * MPSAFE
1547 sys_linux_sched_get_priority_min(struct linux_sched_get_priority_min_args *args)
1549 struct sched_get_priority_min_args bsd;
1550 int error;
1552 #ifdef DEBUG
1553 if (ldebug(sched_get_priority_min))
1554 kprintf(ARGS(sched_get_priority_min, "%d"), args->policy);
1555 #endif
1557 switch (args->policy) {
1558 case LINUX_SCHED_OTHER:
1559 bsd.policy = SCHED_OTHER;
1560 break;
1561 case LINUX_SCHED_FIFO:
1562 bsd.policy = SCHED_FIFO;
1563 break;
1564 case LINUX_SCHED_RR:
1565 bsd.policy = SCHED_RR;
1566 break;
1567 default:
1568 return EINVAL;
1570 bsd.sysmsg_result = 0;
1572 error = sys_sched_get_priority_min(&bsd);
1573 args->sysmsg_result = bsd.sysmsg_result;
1574 return(error);
1577 #define REBOOT_CAD_ON 0x89abcdef
1578 #define REBOOT_CAD_OFF 0
1579 #define REBOOT_HALT 0xcdef0123
1580 #define REBOOT_RESTART 0x01234567
1581 #define REBOOT_RESTART2 0xA1B2C3D4
1582 #define REBOOT_POWEROFF 0x4321FEDC
1583 #define REBOOT_MAGIC1 0xfee1dead
1584 #define REBOOT_MAGIC2 0x28121969
1585 #define REBOOT_MAGIC2A 0x05121996
1586 #define REBOOT_MAGIC2B 0x16041998
1589 * MPSAFE
1592 sys_linux_reboot(struct linux_reboot_args *args)
1594 struct reboot_args bsd_args;
1595 int error;
1597 #ifdef DEBUG
1598 if (ldebug(reboot))
1599 kprintf(ARGS(reboot, "0x%x"), args->cmd);
1600 #endif
1602 if ((args->magic1 != REBOOT_MAGIC1) ||
1603 ((args->magic2 != REBOOT_MAGIC2) &&
1604 (args->magic2 != REBOOT_MAGIC2A) &&
1605 (args->magic2 != REBOOT_MAGIC2B)))
1606 return EINVAL;
1608 switch (args->cmd) {
1609 case REBOOT_CAD_ON:
1610 case REBOOT_CAD_OFF:
1611 return (priv_check(curthread, PRIV_REBOOT));
1612 /* NOTREACHED */
1613 case REBOOT_HALT:
1614 bsd_args.opt = RB_HALT;
1615 break;
1616 case REBOOT_RESTART:
1617 case REBOOT_RESTART2:
1618 bsd_args.opt = 0;
1619 break;
1620 case REBOOT_POWEROFF:
1621 bsd_args.opt = RB_POWEROFF;
1622 break;
1623 default:
1624 return EINVAL;
1625 /* NOTREACHED */
1628 bsd_args.sysmsg_result = 0;
1630 error = sys_reboot(&bsd_args);
1631 args->sysmsg_result = bsd_args.sysmsg_result;
1632 return(error);
1636 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1637 * p->p_retval[1] when COMPAT_43 or COMPAT_SUNOS is defined. This
1638 * globbers registers that are assumed to be preserved. The following
1639 * lightweight syscalls fixes this. See also linux_getgid16() and
1640 * linux_getuid16() in linux_uid16.c.
1642 * linux_getpid() - MP SAFE
1643 * linux_getgid() - MP SAFE
1644 * linux_getuid() - MP SAFE
1648 * MPALMOSTSAFE
1651 sys_linux_getpid(struct linux_getpid_args *args)
1653 struct linux_emuldata *em;
1654 struct proc *p = curproc;
1656 get_mplock();
1657 EMUL_LOCK();
1658 em = emuldata_get(p);
1659 if (em == NULL) /* this should never happen */
1660 args->sysmsg_result = p->p_pid;
1661 else
1662 args->sysmsg_result = em->s->group_pid;
1663 kprintf("curproc %s requested getpid, return pid = %d\n", curproc->p_comm, em->s->group_pid);
1664 EMUL_UNLOCK();
1665 rel_mplock();
1666 return (0);
1670 * MPALMOSTSAFE
1673 sys_linux_getppid(struct linux_getppid_args *args)
1675 struct linux_emuldata *em;
1676 struct proc *parent;
1677 struct proc *p;
1679 get_mplock();
1680 EMUL_LOCK();
1681 em = emuldata_get(curproc);
1682 KKASSERT(em != NULL);
1684 p = pfind(em->s->group_pid);
1685 /* We are not allowed to fail */
1686 if (p == NULL)
1687 goto out;
1689 parent = p->p_pptr;
1690 if (parent->p_sysent == &elf_linux_sysvec) {
1691 em = emuldata_get(parent);
1692 args->sysmsg_result = em->s->group_pid;
1693 kprintf("(a) curproc %s requested getppid, return pid = %d\n", curproc->p_comm, em->s->group_pid);
1694 } else {
1695 args->sysmsg_result = parent->p_pid;
1696 kprintf("(b) curproc %s requested getppid, return pid = %d\n", curproc->p_comm, em->s->group_pid);
1698 out:
1699 EMUL_UNLOCK();
1700 rel_mplock();
1701 return (0);
1705 * MPSAFE
1708 sys_linux_getgid(struct linux_getgid_args *args)
1710 struct thread *td = curthread;
1712 args->sysmsg_result = td->td_ucred->cr_rgid;
1713 return (0);
1717 * MPSAFE
1720 sys_linux_getuid(struct linux_getuid_args *args)
1722 struct thread *td = curthread;
1724 args->sysmsg_result = td->td_ucred->cr_ruid;
1725 return (0);
1729 * MPSAFE
1732 sys_linux_getsid(struct linux_getsid_args *args)
1734 struct getsid_args bsd;
1735 int error;
1737 bsd.sysmsg_result = 0;
1738 bsd.pid = args->pid;
1739 error = sys_getsid(&bsd);
1740 args->sysmsg_result = bsd.sysmsg_result;
1741 return(error);
1745 * MPSAFE
1748 linux_nosys(struct nosys_args *args)
1750 /* XXX */
1751 return (ENOSYS);
1755 sys_linux_mq_open(struct linux_mq_open_args *args)
1757 struct mq_open_args moa;
1758 int error, oflag;
1760 oflag = 0;
1761 if (args->oflag & LINUX_O_RDONLY)
1762 oflag |= O_RDONLY;
1763 if (args->oflag & LINUX_O_WRONLY)
1764 oflag |= O_WRONLY;
1765 if (args->oflag & LINUX_O_RDWR)
1766 oflag |= O_RDWR;
1768 if (args->oflag & LINUX_O_NONBLOCK)
1769 oflag |= O_NONBLOCK;
1770 if (args->oflag & LINUX_O_CREAT)
1771 oflag |= O_CREAT;
1772 if (args->oflag & LINUX_O_EXCL)
1773 oflag |= O_EXCL;
1775 moa.name = args->name;
1776 moa.oflag = oflag;
1777 moa.mode = args->mode;
1778 moa.attr = args->attr;
1780 error = sys_mq_open(&moa);
1782 return (error);
1786 sys_linux_mq_getsetattr(struct linux_mq_getsetattr_args *args)
1788 struct mq_getattr_args gaa;
1789 struct mq_setattr_args saa;
1790 int error;
1792 gaa.mqdes = args->mqd;
1793 gaa.mqstat = args->oattr;
1795 saa.mqdes = args->mqd;
1796 saa.mqstat = args->attr;
1797 saa.mqstat = args->oattr;
1799 if (args->attr != NULL) {
1800 error = sys_mq_setattr(&saa);
1801 } else {
1802 error = sys_mq_getattr(&gaa);
1805 return error;
1809 * Get affinity of a process.
1812 sys_linux_sched_getaffinity(struct linux_sched_getaffinity_args *args)
1814 cpumask_t mask;
1815 struct proc *p;
1816 struct lwp *lp;
1817 int error = 0;
1819 #ifdef DEBUG
1820 if (ldebug(sched_getaffinity))
1821 kprintf(ARGS(sched_getaffinity, "%d, %d, *"), args->pid,
1822 args->len);
1823 #endif
1824 if (args->len < sizeof(cpumask_t))
1825 return (EINVAL);
1826 #if 0
1827 if ((error = priv_check(curthread, PRIV_SCHED_CPUSET)) != 0)
1828 return (EPERM);
1829 #endif
1830 /* Get the mplock to ensure that the proc is not running */
1831 get_mplock();
1832 if (args->pid == 0) {
1833 p = curproc;
1834 } else {
1835 p = pfind(args->pid);
1836 if (p == NULL) {
1837 error = ESRCH;
1838 goto done;
1842 lp = FIRST_LWP_IN_PROC(p);
1844 * XXX: if lwp_cpumask is ever changed to support more than
1845 * 32 processors, this needs to be changed to a bcopy.
1847 mask = lp->lwp_cpumask;
1848 if ((error = copyout(&mask, args->user_mask_ptr, sizeof(cpumask_t))))
1849 error = EFAULT;
1850 done:
1851 rel_mplock();
1852 #if 0
1853 if (error == 0)
1854 args->sysmsg_iresult = sizeof(cpumask_t);
1855 #endif
1856 return (error);
1860 * Set affinity of a process.
1863 sys_linux_sched_setaffinity(struct linux_sched_setaffinity_args *args)
1865 #ifdef DEBUG
1866 if (ldebug(sched_setaffinity))
1867 kprintf(ARGS(sched_setaffinity, "%d, %d, *"), args->pid,
1868 args->len);
1869 #endif
1871 * From Linux man page:
1872 * sched_setaffinity() sets the CPU affinity mask of the process
1873 * whose ID is pid to the value specified by mask. If pid is zero,
1874 * then the calling process is used. The argument cpusetsize is
1875 * the length (in bytes) of the data pointed to by mask. Normally
1876 * this argument would be specified as sizeof(cpu_set_t).
1878 * If the process specified by pid is not currently running on one
1879 * of the CPUs specified in mask, then that process is migrated to
1880 * one of the CPUs specified in mask.
1883 * About our implementation: I don't think that it is too important
1884 * to have a working implementation, but if it was ever needed,
1885 * the best approach would be to implement the whole mechanism
1886 * properly in kern_usched.
1887 * The idea has to be to change the affinity mask AND migrate the
1888 * lwp to one of the new valid CPUs for the lwp, in case the current
1889 * CPU isn't anymore in the affinity mask passed in.
1890 * For now, we'll just signal success even if we didn't do anything.
1892 return 0;
1896 sys_linux_gettid(struct linux_gettid_args *args)
1898 args->sysmsg_iresult = curproc->p_pid;
1899 return 0;
1903 sys_linux_getcpu(struct linux_getcpu_args *args)
1905 struct globaldata *gd;
1906 l_uint node = 0;
1907 int error;
1909 gd = mycpu;
1910 error = copyout(&gd->gd_cpuid, args->pcpu, sizeof(gd->gd_cpuid));
1911 if (error)
1912 return (error);
1914 * XXX: this should be the NUMA node, but since we don't implement it,
1915 * just return 0 for it.
1917 error = copyout(&node, args->pnode, sizeof(node));
1918 return (error);