HAMMER 61E/Many: Stabilization, Performance
[dragonfly.git] / sys / emulation / linux / linux_misc.c
blob4e8ae149644b2f7f35733da0fc167c79c7ff5120
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/nlookup.h>
46 #include <sys/blist.h>
47 #include <sys/reboot.h>
48 #include <sys/resourcevar.h>
49 #include <sys/signalvar.h>
50 #include <sys/signal2.h>
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>
59 #include <sys/thread2.h>
61 #include <vm/vm.h>
62 #include <vm/pmap.h>
63 #include <vm/vm_kern.h>
64 #include <vm/vm_map.h>
65 #include <vm/vm_extern.h>
66 #include <vm/vm_object.h>
67 #include <vm/vm_zone.h>
68 #include <vm/swap_pager.h>
70 #include <machine/frame.h>
71 #include <machine/limits.h>
72 #include <machine/psl.h>
73 #include <machine/sysarch.h>
74 #ifdef __i386__
75 #include <machine/segments.h>
76 #endif
78 #include <sys/sched.h>
80 #include <emulation/linux/linux_sysproto.h>
81 #include <arch_linux/linux.h>
82 #include <arch_linux/linux_proto.h>
83 #include "linux_mib.h"
84 #include "linux_util.h"
86 #define BSD_TO_LINUX_SIGNAL(sig) \
87 (((sig) <= LINUX_SIGTBLSZ) ? bsd_to_linux_signal[_SIG_IDX(sig)] : sig)
89 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
90 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
91 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
92 RLIMIT_MEMLOCK, -1
95 struct l_sysinfo {
96 l_long uptime; /* Seconds since boot */
97 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */
98 l_ulong totalram; /* Total usable main memory size */
99 l_ulong freeram; /* Available memory size */
100 l_ulong sharedram; /* Amount of shared memory */
101 l_ulong bufferram; /* Memory used by buffers */
102 l_ulong totalswap; /* Total swap space size */
103 l_ulong freeswap; /* swap space still available */
104 l_ushort procs; /* Number of current processes */
105 char _f[22]; /* Pads structure to 64 bytes */
109 sys_linux_sysinfo(struct linux_sysinfo_args *args)
111 struct l_sysinfo sysinfo;
112 vm_object_t object;
113 int i;
114 struct timespec ts;
116 /* Uptime is copied out of print_uptime() in kern_shutdown.c */
117 getnanouptime(&ts);
118 i = 0;
119 if (ts.tv_sec >= 86400) {
120 ts.tv_sec %= 86400;
121 i = 1;
123 if (i || ts.tv_sec >= 3600) {
124 ts.tv_sec %= 3600;
125 i = 1;
127 if (i || ts.tv_sec >= 60) {
128 ts.tv_sec %= 60;
129 i = 1;
131 sysinfo.uptime=ts.tv_sec;
133 /* Use the information from the mib to get our load averages */
134 for (i = 0; i < 3; i++)
135 sysinfo.loads[i] = averunnable.ldavg[i];
137 sysinfo.totalram = Maxmem * PAGE_SIZE;
138 sysinfo.freeram = sysinfo.totalram - vmstats.v_wire_count * PAGE_SIZE;
140 sysinfo.sharedram = 0;
141 for (object = TAILQ_FIRST(&vm_object_list); object != NULL;
142 object = TAILQ_NEXT(object, object_list))
143 if (object->shadow_count > 1)
144 sysinfo.sharedram += object->resident_page_count;
146 sysinfo.sharedram *= PAGE_SIZE;
147 sysinfo.bufferram = 0;
149 if (swapblist == NULL) {
150 sysinfo.totalswap= 0;
151 sysinfo.freeswap = 0;
152 } else {
153 sysinfo.totalswap = swapblist->bl_blocks * 1024;
154 sysinfo.freeswap = swapblist->bl_root->u.bmu_avail * PAGE_SIZE;
157 sysinfo.procs = 20; /* Hack */
159 return copyout(&sysinfo, (caddr_t)args->info, sizeof(sysinfo));
163 sys_linux_alarm(struct linux_alarm_args *args)
165 struct thread *td = curthread;
166 struct proc *p = td->td_proc;
167 struct itimerval it, old_it;
168 struct timeval tv;
170 KKASSERT(p);
172 #ifdef DEBUG
173 if (ldebug(alarm))
174 kprintf(ARGS(alarm, "%u"), args->secs);
175 #endif
177 if (args->secs > 100000000)
178 return EINVAL;
180 it.it_value.tv_sec = (long)args->secs;
181 it.it_value.tv_usec = 0;
182 it.it_interval.tv_sec = 0;
183 it.it_interval.tv_usec = 0;
184 crit_enter();
185 old_it = p->p_realtimer;
186 getmicrouptime(&tv);
187 if (timevalisset(&old_it.it_value))
188 callout_stop(&p->p_ithandle);
189 if (it.it_value.tv_sec != 0) {
190 callout_reset(&p->p_ithandle, tvtohz_high(&it.it_value),
191 realitexpire, p);
192 timevaladd(&it.it_value, &tv);
194 p->p_realtimer = it;
195 crit_exit();
196 if (timevalcmp(&old_it.it_value, &tv, >)) {
197 timevalsub(&old_it.it_value, &tv);
198 if (old_it.it_value.tv_usec != 0)
199 old_it.it_value.tv_sec++;
200 args->sysmsg_result = old_it.it_value.tv_sec;
202 return 0;
206 sys_linux_brk(struct linux_brk_args *args)
208 struct thread *td = curthread;
209 struct proc *p = td->td_proc;
210 struct vmspace *vm;
211 vm_offset_t new, old;
212 struct obreak_args bsd_args;
214 KKASSERT(p);
215 vm = p->p_vmspace;
216 #ifdef DEBUG
217 if (ldebug(brk))
218 kprintf(ARGS(brk, "%p"), (void *)args->dsend);
219 #endif
220 old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
221 new = (vm_offset_t)args->dsend;
222 bsd_args.sysmsg_result = 0;
223 bsd_args.nsize = (char *) new;
224 bsd_args.sysmsg_result = 0;
225 if (((caddr_t)new > vm->vm_daddr) && !sys_obreak(&bsd_args))
226 args->sysmsg_result = (long)new;
227 else
228 args->sysmsg_result = (long)old;
230 return 0;
234 sys_linux_uselib(struct linux_uselib_args *args)
236 struct thread *td = curthread;
237 struct proc *p;
238 struct nlookupdata nd;
239 struct vnode *vp;
240 struct exec *a_out;
241 struct vattr attr;
242 vm_offset_t vmaddr;
243 unsigned long file_offset;
244 vm_offset_t buffer;
245 unsigned long bss_size;
246 int error;
247 int locked;
248 char *path;
250 KKASSERT(td->td_proc);
251 p = td->td_proc;
253 error = linux_copyin_path(args->library, &path, LINUX_PATH_EXISTS);
254 if (error)
255 return (error);
256 #ifdef DEBUG
257 if (ldebug(uselib))
258 kprintf(ARGS(uselib, "%s"), path);
259 #endif
261 a_out = NULL;
262 locked = 0;
263 vp = NULL;
265 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
266 if (error == 0)
267 error = nlookup(&nd);
268 if (error == 0)
269 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
270 if (error)
271 goto cleanup;
273 * From here on down, we have a locked vnode that must be unlocked.
275 locked = 1;
277 /* Writable? */
278 if (vp->v_writecount) {
279 error = ETXTBSY;
280 goto cleanup;
283 /* Executable? */
284 error = VOP_GETATTR(vp, &attr);
285 if (error)
286 goto cleanup;
288 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
289 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
290 error = ENOEXEC;
291 goto cleanup;
294 /* Sensible size? */
295 if (attr.va_size == 0) {
296 error = ENOEXEC;
297 goto cleanup;
300 /* Can we access it? */
301 error = VOP_ACCESS(vp, VEXEC, p->p_ucred);
302 if (error)
303 goto cleanup;
305 error = VOP_OPEN(vp, FREAD, p->p_ucred, NULL);
306 if (error)
307 goto cleanup;
310 * Lock no longer needed
312 vn_unlock(vp);
313 locked = 0;
315 /* Pull in executable header into kernel_map */
316 error = vm_mmap(&kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE,
317 VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp, 0);
318 if (error)
319 goto cleanup;
321 /* Is it a Linux binary ? */
322 if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
323 error = ENOEXEC;
324 goto cleanup;
328 * While we are here, we should REALLY do some more checks
331 /* Set file/virtual offset based on a.out variant. */
332 switch ((int)(a_out->a_magic & 0xffff)) {
333 case 0413: /* ZMAGIC */
334 file_offset = 1024;
335 break;
336 case 0314: /* QMAGIC */
337 file_offset = 0;
338 break;
339 default:
340 error = ENOEXEC;
341 goto cleanup;
344 bss_size = round_page(a_out->a_bss);
346 /* Check various fields in header for validity/bounds. */
347 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
348 error = ENOEXEC;
349 goto cleanup;
352 /* text + data can't exceed file size */
353 if (a_out->a_data + a_out->a_text > attr.va_size) {
354 error = EFAULT;
355 goto cleanup;
359 * text/data/bss must not exceed limits
360 * XXX - this is not complete. it should check current usage PLUS
361 * the resources needed by this library.
363 if (a_out->a_text > maxtsiz ||
364 a_out->a_data + bss_size > p->p_rlimit[RLIMIT_DATA].rlim_cur) {
365 error = ENOMEM;
366 goto cleanup;
369 /* prevent more writers */
370 vp->v_flag |= VTEXT;
373 * Check if file_offset page aligned. Currently we cannot handle
374 * misalinged file offsets, and so we read in the entire image
375 * (what a waste).
377 if (file_offset & PAGE_MASK) {
378 #ifdef DEBUG
379 kprintf("uselib: Non page aligned binary %lu\n", file_offset);
380 #endif
381 /* Map text+data read/write/execute */
383 /* a_entry is the load address and is page aligned */
384 vmaddr = trunc_page(a_out->a_entry);
386 /* get anon user mapping, read+write+execute */
387 error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0,
388 &vmaddr, a_out->a_text + a_out->a_data,
389 FALSE,
390 VM_MAPTYPE_NORMAL,
391 VM_PROT_ALL, VM_PROT_ALL,
393 if (error)
394 goto cleanup;
396 /* map file into kernel_map */
397 error = vm_mmap(&kernel_map, &buffer,
398 round_page(a_out->a_text + a_out->a_data + file_offset),
399 VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp,
400 trunc_page(file_offset));
401 if (error)
402 goto cleanup;
404 /* copy from kernel VM space to user space */
405 error = copyout((caddr_t)(uintptr_t)(buffer + file_offset),
406 (caddr_t)vmaddr, a_out->a_text + a_out->a_data);
408 /* release temporary kernel space */
409 vm_map_remove(&kernel_map, buffer, buffer +
410 round_page(a_out->a_text + a_out->a_data + file_offset));
412 if (error)
413 goto cleanup;
414 } else {
415 #ifdef DEBUG
416 kprintf("uselib: Page aligned binary %lu\n", file_offset);
417 #endif
419 * for QMAGIC, a_entry is 20 bytes beyond the load address
420 * to skip the executable header
422 vmaddr = trunc_page(a_out->a_entry);
425 * Map it all into the process's space as a single
426 * copy-on-write "data" segment.
428 error = vm_mmap(&p->p_vmspace->vm_map, &vmaddr,
429 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
430 MAP_PRIVATE | MAP_FIXED, (caddr_t)vp, file_offset);
431 if (error)
432 goto cleanup;
434 #ifdef DEBUG
435 kprintf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long*)vmaddr)[0],
436 ((long*)vmaddr)[1]);
437 #endif
438 if (bss_size != 0) {
439 /* Calculate BSS start address */
440 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
441 a_out->a_data;
443 /* allocate some 'anon' space */
444 error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0,
445 &vmaddr, bss_size,
446 FALSE,
447 VM_MAPTYPE_NORMAL,
448 VM_PROT_ALL, VM_PROT_ALL,
450 if (error)
451 goto cleanup;
454 cleanup:
455 /* Unlock/release vnode */
456 if (vp) {
457 if (locked)
458 vn_unlock(vp);
459 vrele(vp);
461 /* Release the kernel mapping. */
462 if (a_out) {
463 vm_map_remove(&kernel_map, (vm_offset_t)a_out,
464 (vm_offset_t)a_out + PAGE_SIZE);
466 nlookup_done(&nd);
467 linux_free_path(&path);
468 return (error);
472 sys_linux_select(struct linux_select_args *args)
474 struct select_args bsa;
475 struct timeval tv0, tv1, utv, *tvp;
476 caddr_t sg;
477 int error;
479 #ifdef DEBUG
480 if (ldebug(select))
481 kprintf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
482 (void *)args->readfds, (void *)args->writefds,
483 (void *)args->exceptfds, (void *)args->timeout);
484 #endif
486 error = 0;
487 bsa.sysmsg_result = 0;
488 bsa.nd = args->nfds;
489 bsa.in = args->readfds;
490 bsa.ou = args->writefds;
491 bsa.ex = args->exceptfds;
492 bsa.tv = (struct timeval *)args->timeout;
495 * Store current time for computation of the amount of
496 * time left.
498 if (args->timeout) {
499 if ((error = copyin((caddr_t)args->timeout, &utv,
500 sizeof(utv))))
501 goto select_out;
502 #ifdef DEBUG
503 if (ldebug(select))
504 kprintf(LMSG("incoming timeout (%ld/%ld)"),
505 utv.tv_sec, utv.tv_usec);
506 #endif
508 if (itimerfix(&utv)) {
510 * The timeval was invalid. Convert it to something
511 * valid that will act as it does under Linux.
513 sg = stackgap_init();
514 tvp = stackgap_alloc(&sg, sizeof(utv));
515 utv.tv_sec += utv.tv_usec / 1000000;
516 utv.tv_usec %= 1000000;
517 if (utv.tv_usec < 0) {
518 utv.tv_sec -= 1;
519 utv.tv_usec += 1000000;
521 if (utv.tv_sec < 0)
522 timevalclear(&utv);
523 if ((error = copyout(&utv, tvp, sizeof(utv))))
524 goto select_out;
525 bsa.tv = tvp;
527 microtime(&tv0);
530 error = sys_select(&bsa);
531 args->sysmsg_result = bsa.sysmsg_result;
532 #ifdef DEBUG
533 if (ldebug(select))
534 kprintf(LMSG("real select returns %d"), error);
535 #endif
536 if (error) {
538 * See fs/select.c in the Linux kernel. Without this,
539 * Maelstrom doesn't work.
541 if (error == ERESTART)
542 error = EINTR;
543 goto select_out;
546 if (args->timeout) {
547 if (args->sysmsg_result) {
549 * Compute how much time was left of the timeout,
550 * by subtracting the current time and the time
551 * before we started the call, and subtracting
552 * that result from the user-supplied value.
554 microtime(&tv1);
555 timevalsub(&tv1, &tv0);
556 timevalsub(&utv, &tv1);
557 if (utv.tv_sec < 0)
558 timevalclear(&utv);
559 } else
560 timevalclear(&utv);
561 #ifdef DEBUG
562 if (ldebug(select))
563 kprintf(LMSG("outgoing timeout (%ld/%ld)"),
564 utv.tv_sec, utv.tv_usec);
565 #endif
566 if ((error = copyout(&utv, (caddr_t)args->timeout,
567 sizeof(utv))))
568 goto select_out;
571 select_out:
572 #ifdef DEBUG
573 if (ldebug(select))
574 kprintf(LMSG("select_out -> %d"), error);
575 #endif
576 return error;
579 int
580 sys_linux_mremap(struct linux_mremap_args *args)
582 struct munmap_args bsd_args;
583 int error = 0;
585 #ifdef DEBUG
586 if (ldebug(mremap))
587 kprintf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
588 (void *)args->addr,
589 (unsigned long)args->old_len,
590 (unsigned long)args->new_len,
591 (unsigned long)args->flags);
592 #endif
593 args->new_len = round_page(args->new_len);
594 args->old_len = round_page(args->old_len);
596 if (args->new_len > args->old_len) {
597 args->sysmsg_result = 0;
598 return ENOMEM;
601 if (args->new_len < args->old_len) {
602 bsd_args.sysmsg_result = 0;
603 bsd_args.addr = (caddr_t)(args->addr + args->new_len);
604 bsd_args.len = args->old_len - args->new_len;
605 error = sys_munmap(&bsd_args);
608 args->sysmsg_resultp = error ? NULL : (void *)args->addr;
609 return error;
612 #define LINUX_MS_ASYNC 0x0001
613 #define LINUX_MS_INVALIDATE 0x0002
614 #define LINUX_MS_SYNC 0x0004
617 sys_linux_msync(struct linux_msync_args *args)
619 struct msync_args bsd_args;
620 int error;
622 bsd_args.addr = (caddr_t)args->addr;
623 bsd_args.len = args->len;
624 bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
625 bsd_args.sysmsg_result = 0;
627 error = sys_msync(&bsd_args);
628 args->sysmsg_result = bsd_args.sysmsg_result;
629 return(error);
633 sys_linux_time(struct linux_time_args *args)
635 struct timeval tv;
636 l_time_t tm;
637 int error;
639 #ifdef DEBUG
640 if (ldebug(time))
641 kprintf(ARGS(time, "*"));
642 #endif
644 microtime(&tv);
645 tm = tv.tv_sec;
646 if (args->tm && (error = copyout(&tm, (caddr_t)args->tm, sizeof(tm))))
647 return error;
648 args->sysmsg_lresult = tm;
649 return 0;
652 struct l_times_argv {
653 l_long tms_utime;
654 l_long tms_stime;
655 l_long tms_cutime;
656 l_long tms_cstime;
659 #define CLK_TCK 100 /* Linux uses 100 */
661 #define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
664 sys_linux_times(struct linux_times_args *args)
666 struct thread *td = curthread;
667 struct proc *p = td->td_proc;
668 struct timeval tv;
669 struct l_times_argv tms;
670 struct rusage ru;
671 int error;
673 KKASSERT(p);
674 #ifdef DEBUG
675 if (ldebug(times))
676 kprintf(ARGS(times, "*"));
677 #endif
679 calcru_proc(p, &ru);
681 tms.tms_utime = CONVTCK(ru.ru_utime);
682 tms.tms_stime = CONVTCK(ru.ru_stime);
684 tms.tms_cutime = CONVTCK(p->p_cru.ru_utime);
685 tms.tms_cstime = CONVTCK(p->p_cru.ru_stime);
687 if ((error = copyout(&tms, (caddr_t)args->buf, sizeof(tms))))
688 return error;
690 microuptime(&tv);
691 args->sysmsg_result = (int)CONVTCK(tv);
692 return 0;
696 sys_linux_newuname(struct linux_newuname_args *args)
698 struct thread *td = curthread;
699 struct l_new_utsname utsname;
700 char *osrelease, *osname;
702 #ifdef DEBUG
703 if (ldebug(newuname))
704 kprintf(ARGS(newuname, "*"));
705 #endif
707 osname = linux_get_osname(td);
708 osrelease = linux_get_osrelease(td);
710 bzero(&utsname, sizeof(utsname));
711 strncpy(utsname.sysname, osname, LINUX_MAX_UTSNAME-1);
712 strncpy(utsname.nodename, hostname, LINUX_MAX_UTSNAME-1);
713 strncpy(utsname.release, osrelease, LINUX_MAX_UTSNAME-1);
714 strncpy(utsname.version, version, LINUX_MAX_UTSNAME-1);
715 strncpy(utsname.machine, machine, LINUX_MAX_UTSNAME-1);
716 strncpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME-1);
718 return (copyout(&utsname, (caddr_t)args->buf, sizeof(utsname)));
721 #if defined(__i386__)
722 struct l_utimbuf {
723 l_time_t l_actime;
724 l_time_t l_modtime;
728 sys_linux_utime(struct linux_utime_args *args)
730 struct timeval tv[2];
731 struct l_utimbuf lut;
732 struct nlookupdata nd;
733 char *path;
734 int error;
736 error = linux_copyin_path(args->fname, &path, LINUX_PATH_EXISTS);
737 if (error)
738 return (error);
739 #ifdef DEBUG
740 if (ldebug(utime))
741 kprintf(ARGS(utime, "%s, *"), path);
742 #endif
744 if (args->times) {
745 error = copyin(args->times, &lut, sizeof(lut));
746 if (error)
747 goto cleanup;
748 tv[0].tv_sec = lut.l_actime;
749 tv[0].tv_usec = 0;
750 tv[1].tv_sec = lut.l_modtime;
751 tv[1].tv_usec = 0;
753 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
754 if (error == 0)
755 error = kern_utimes(&nd, args->times ? tv : NULL);
756 nlookup_done(&nd);
757 cleanup:
758 linux_free_path(&path);
759 return (error);
761 #endif /* __i386__ */
763 #define __WCLONE 0x80000000
766 sys_linux_waitpid(struct linux_waitpid_args *args)
768 int error, options, status;
770 #ifdef DEBUG
771 if (ldebug(waitpid))
772 kprintf(ARGS(waitpid, "%d, %p, %d"),
773 args->pid, (void *)args->status, args->options);
774 #endif
775 options = args->options & (WNOHANG | WUNTRACED);
776 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
777 if (args->options & __WCLONE)
778 options |= WLINUXCLONE;
780 error = kern_wait(args->pid, args->status ? &status : NULL, options,
781 NULL, &args->sysmsg_result);
783 if (error == 0 && args->status) {
784 status &= 0xffff;
785 if (WIFSIGNALED(status))
786 status = (status & 0xffffff80) |
787 BSD_TO_LINUX_SIGNAL(WTERMSIG(status));
788 else if (WIFSTOPPED(status))
789 status = (status & 0xffff00ff) |
790 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(status)) << 8);
791 error = copyout(&status, args->status, sizeof(status));
794 return (error);
798 sys_linux_wait4(struct linux_wait4_args *args)
800 struct thread *td = curthread;
801 struct lwp *lp = td->td_lwp;
802 struct rusage rusage;
803 int error, options, status;
805 KKASSERT(lp);
807 #ifdef DEBUG
808 if (ldebug(wait4))
809 kprintf(ARGS(wait4, "%d, %p, %d, %p"),
810 args->pid, (void *)args->status, args->options,
811 (void *)args->rusage);
812 #endif
813 options = args->options & (WNOHANG | WUNTRACED);
814 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
815 if (args->options & __WCLONE)
816 options |= WLINUXCLONE;
818 error = kern_wait(args->pid, args->status ? &status : NULL, options,
819 args->rusage ? &rusage : NULL, &args->sysmsg_result);
821 if (error == 0)
822 lwp_delsig(lp, SIGCHLD);
824 if (error == 0 && args->status) {
825 status &= 0xffff;
826 if (WIFSIGNALED(status))
827 status = (status & 0xffffff80) |
828 BSD_TO_LINUX_SIGNAL(WTERMSIG(status));
829 else if (WIFSTOPPED(status))
830 status = (status & 0xffff00ff) |
831 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(status)) << 8);
832 error = copyout(&status, args->status, sizeof(status));
834 if (error == 0 && args->rusage)
835 error = copyout(&rusage, args->rusage, sizeof(rusage));
837 return (error);
841 sys_linux_mknod(struct linux_mknod_args *args)
843 struct nlookupdata nd;
844 char *path;
845 int error;
847 error = linux_copyin_path(args->path, &path, LINUX_PATH_CREATE);
848 if (error)
849 return (error);
850 #ifdef DEBUG
851 if (ldebug(mknod))
852 kprintf(ARGS(mknod, "%s, %d, %d"),
853 path, args->mode, args->dev);
854 #endif
855 error = nlookup_init(&nd, path, UIO_SYSSPACE, 0);
856 if (error == 0) {
857 if (args->mode & S_IFIFO) {
858 error = kern_mkfifo(&nd, args->mode);
859 } else {
860 error = kern_mknod(&nd, args->mode,
861 umajor(args->dev),
862 uminor(args->dev));
865 nlookup_done(&nd);
867 linux_free_path(&path);
868 return(error);
872 * UGH! This is just about the dumbest idea I've ever heard!!
875 sys_linux_personality(struct linux_personality_args *args)
877 #ifdef DEBUG
878 if (ldebug(personality))
879 kprintf(ARGS(personality, "%d"), args->per);
880 #endif
881 if (args->per != 0)
882 return EINVAL;
884 /* Yes Jim, it's still a Linux... */
885 args->sysmsg_result = 0;
886 return 0;
890 * Wrappers for get/setitimer for debugging..
893 sys_linux_setitimer(struct linux_setitimer_args *args)
895 struct setitimer_args bsa;
896 struct itimerval foo;
897 int error;
899 #ifdef DEBUG
900 if (ldebug(setitimer))
901 kprintf(ARGS(setitimer, "%p, %p"),
902 (void *)args->itv, (void *)args->oitv);
903 #endif
904 bsa.which = args->which;
905 bsa.itv = (struct itimerval *)args->itv;
906 bsa.oitv = (struct itimerval *)args->oitv;
907 bsa.sysmsg_result = 0;
908 if (args->itv) {
909 if ((error = copyin((caddr_t)args->itv, &foo, sizeof(foo))))
910 return error;
911 #ifdef DEBUG
912 if (ldebug(setitimer)) {
913 kprintf("setitimer: value: sec: %ld, usec: %ld\n",
914 foo.it_value.tv_sec, foo.it_value.tv_usec);
915 kprintf("setitimer: interval: sec: %ld, usec: %ld\n",
916 foo.it_interval.tv_sec, foo.it_interval.tv_usec);
918 #endif
920 error = sys_setitimer(&bsa);
921 args->sysmsg_result = bsa.sysmsg_result;
922 return(error);
926 sys_linux_getitimer(struct linux_getitimer_args *args)
928 struct getitimer_args bsa;
929 int error;
930 #ifdef DEBUG
931 if (ldebug(getitimer))
932 kprintf(ARGS(getitimer, "%p"), (void *)args->itv);
933 #endif
934 bsa.which = args->which;
935 bsa.itv = (struct itimerval *)args->itv;
936 bsa.sysmsg_result = 0;
937 error = sys_getitimer(&bsa);
938 args->sysmsg_result = bsa.sysmsg_result;
939 return(error);
943 sys_linux_nice(struct linux_nice_args *args)
945 struct setpriority_args bsd_args;
946 int error;
948 bsd_args.which = PRIO_PROCESS;
949 bsd_args.who = 0; /* current process */
950 bsd_args.prio = args->inc;
951 bsd_args.sysmsg_result = 0;
952 error = sys_setpriority(&bsd_args);
953 args->sysmsg_result = bsd_args.sysmsg_result;
954 return(error);
958 sys_linux_setgroups(struct linux_setgroups_args *args)
960 struct thread *td = curthread;
961 struct proc *p = td->td_proc;
962 struct ucred *newcred, *oldcred;
963 l_gid_t linux_gidset[NGROUPS];
964 gid_t *bsd_gidset;
965 int ngrp, error;
967 KKASSERT(p);
969 ngrp = args->gidsetsize;
970 oldcred = p->p_ucred;
973 * cr_groups[0] holds egid. Setting the whole set from
974 * the supplied set will cause egid to be changed too.
975 * Keep cr_groups[0] unchanged to prevent that.
978 if ((error = suser_cred(oldcred, PRISON_ROOT)) != 0)
979 return (error);
981 if (ngrp >= NGROUPS)
982 return (EINVAL);
984 newcred = crdup(oldcred);
985 if (ngrp > 0) {
986 error = copyin((caddr_t)args->grouplist, linux_gidset,
987 ngrp * sizeof(l_gid_t));
988 if (error)
989 return (error);
991 newcred->cr_ngroups = ngrp + 1;
993 bsd_gidset = newcred->cr_groups;
994 ngrp--;
995 while (ngrp >= 0) {
996 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
997 ngrp--;
999 } else {
1000 newcred->cr_ngroups = 1;
1003 setsugid();
1004 p->p_ucred = newcred;
1005 crfree(oldcred);
1006 return (0);
1010 sys_linux_getgroups(struct linux_getgroups_args *args)
1012 struct thread *td = curthread;
1013 struct proc *p = td->td_proc;
1014 struct ucred *cred;
1015 l_gid_t linux_gidset[NGROUPS];
1016 gid_t *bsd_gidset;
1017 int bsd_gidsetsz, ngrp, error;
1019 KKASSERT(p);
1021 cred = p->p_ucred;
1022 bsd_gidset = cred->cr_groups;
1023 bsd_gidsetsz = cred->cr_ngroups - 1;
1026 * cr_groups[0] holds egid. Returning the whole set
1027 * here will cause a duplicate. Exclude cr_groups[0]
1028 * to prevent that.
1031 if ((ngrp = args->gidsetsize) == 0) {
1032 args->sysmsg_result = bsd_gidsetsz;
1033 return (0);
1036 if (ngrp < bsd_gidsetsz)
1037 return (EINVAL);
1039 ngrp = 0;
1040 while (ngrp < bsd_gidsetsz) {
1041 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1042 ngrp++;
1045 if ((error = copyout(linux_gidset, (caddr_t)args->grouplist,
1046 ngrp * sizeof(l_gid_t))))
1047 return (error);
1049 args->sysmsg_result = ngrp;
1050 return (0);
1054 sys_linux_setrlimit(struct linux_setrlimit_args *args)
1056 struct l_rlimit linux_rlim;
1057 struct rlimit rlim;
1058 u_int which;
1059 int error;
1061 #ifdef DEBUG
1062 if (ldebug(setrlimit))
1063 kprintf(ARGS(setrlimit, "%d, %p"),
1064 args->resource, (void *)args->rlim);
1065 #endif
1066 if (args->resource >= LINUX_RLIM_NLIMITS)
1067 return (EINVAL);
1068 which = linux_to_bsd_resource[args->resource];
1069 if (which == -1)
1070 return (EINVAL);
1072 error = copyin(args->rlim, &linux_rlim, sizeof(linux_rlim));
1073 if (error)
1074 return (error);
1075 rlim.rlim_cur = (rlim_t)linux_rlim.rlim_cur;
1076 rlim.rlim_max = (rlim_t)linux_rlim.rlim_max;
1078 error = kern_setrlimit(which, &rlim);
1080 return(error);
1084 sys_linux_old_getrlimit(struct linux_old_getrlimit_args *args)
1086 struct l_rlimit linux_rlim;
1087 struct rlimit rlim;
1088 u_int which;
1089 int error;
1091 #ifdef DEBUG
1092 if (ldebug(old_getrlimit))
1093 kprintf(ARGS(old_getrlimit, "%d, %p"),
1094 args->resource, (void *)args->rlim);
1095 #endif
1096 if (args->resource >= LINUX_RLIM_NLIMITS)
1097 return (EINVAL);
1098 which = linux_to_bsd_resource[args->resource];
1099 if (which == -1)
1100 return (EINVAL);
1102 error = kern_getrlimit(which, &rlim);
1104 if (error == 0) {
1105 linux_rlim.rlim_cur = (l_ulong)rlim.rlim_cur;
1106 if (linux_rlim.rlim_cur == ULONG_MAX)
1107 linux_rlim.rlim_cur = LONG_MAX;
1108 linux_rlim.rlim_max = (l_ulong)rlim.rlim_max;
1109 if (linux_rlim.rlim_max == ULONG_MAX)
1110 linux_rlim.rlim_max = LONG_MAX;
1111 error = copyout(&linux_rlim, args->rlim, sizeof(linux_rlim));
1113 return (error);
1117 sys_linux_getrlimit(struct linux_getrlimit_args *args)
1119 struct l_rlimit linux_rlim;
1120 struct rlimit rlim;
1121 u_int which;
1122 int error;
1124 #ifdef DEBUG
1125 if (ldebug(getrlimit))
1126 kprintf(ARGS(getrlimit, "%d, %p"),
1127 args->resource, (void *)args->rlim);
1128 #endif
1129 if (args->resource >= LINUX_RLIM_NLIMITS)
1130 return (EINVAL);
1131 which = linux_to_bsd_resource[args->resource];
1132 if (which == -1)
1133 return (EINVAL);
1135 error = kern_getrlimit(which, &rlim);
1137 if (error == 0) {
1138 linux_rlim.rlim_cur = (l_ulong)rlim.rlim_cur;
1139 linux_rlim.rlim_max = (l_ulong)rlim.rlim_max;
1140 error = copyout(&linux_rlim, args->rlim, sizeof(linux_rlim));
1142 return (error);
1146 sys_linux_sched_setscheduler(struct linux_sched_setscheduler_args *args)
1148 struct sched_setscheduler_args bsd;
1149 int error;
1151 #ifdef DEBUG
1152 if (ldebug(sched_setscheduler))
1153 kprintf(ARGS(sched_setscheduler, "%d, %d, %p"),
1154 args->pid, args->policy, (const void *)args->param);
1155 #endif
1157 switch (args->policy) {
1158 case LINUX_SCHED_OTHER:
1159 bsd.policy = SCHED_OTHER;
1160 break;
1161 case LINUX_SCHED_FIFO:
1162 bsd.policy = SCHED_FIFO;
1163 break;
1164 case LINUX_SCHED_RR:
1165 bsd.policy = SCHED_RR;
1166 break;
1167 default:
1168 return EINVAL;
1171 bsd.pid = args->pid;
1172 bsd.param = (struct sched_param *)args->param;
1173 bsd.sysmsg_result = 0;
1175 error = sys_sched_setscheduler(&bsd);
1176 args->sysmsg_result = bsd.sysmsg_result;
1177 return(error);
1181 sys_linux_sched_getscheduler(struct linux_sched_getscheduler_args *args)
1183 struct sched_getscheduler_args bsd;
1184 int error;
1186 #ifdef DEBUG
1187 if (ldebug(sched_getscheduler))
1188 kprintf(ARGS(sched_getscheduler, "%d"), args->pid);
1189 #endif
1191 bsd.sysmsg_result = 0;
1192 bsd.pid = args->pid;
1193 error = sys_sched_getscheduler(&bsd);
1194 args->sysmsg_result = bsd.sysmsg_result;
1196 switch (args->sysmsg_result) {
1197 case SCHED_OTHER:
1198 args->sysmsg_result = LINUX_SCHED_OTHER;
1199 break;
1200 case SCHED_FIFO:
1201 args->sysmsg_result = LINUX_SCHED_FIFO;
1202 break;
1203 case SCHED_RR:
1204 args->sysmsg_result = LINUX_SCHED_RR;
1205 break;
1207 return error;
1211 sys_linux_sched_get_priority_max(struct linux_sched_get_priority_max_args *args)
1213 struct sched_get_priority_max_args bsd;
1214 int error;
1216 #ifdef DEBUG
1217 if (ldebug(sched_get_priority_max))
1218 kprintf(ARGS(sched_get_priority_max, "%d"), args->policy);
1219 #endif
1221 switch (args->policy) {
1222 case LINUX_SCHED_OTHER:
1223 bsd.policy = SCHED_OTHER;
1224 break;
1225 case LINUX_SCHED_FIFO:
1226 bsd.policy = SCHED_FIFO;
1227 break;
1228 case LINUX_SCHED_RR:
1229 bsd.policy = SCHED_RR;
1230 break;
1231 default:
1232 return EINVAL;
1234 bsd.sysmsg_result = 0;
1236 error = sys_sched_get_priority_max(&bsd);
1237 args->sysmsg_result = bsd.sysmsg_result;
1238 return(error);
1242 sys_linux_sched_get_priority_min(struct linux_sched_get_priority_min_args *args)
1244 struct sched_get_priority_min_args bsd;
1245 int error;
1247 #ifdef DEBUG
1248 if (ldebug(sched_get_priority_min))
1249 kprintf(ARGS(sched_get_priority_min, "%d"), args->policy);
1250 #endif
1252 switch (args->policy) {
1253 case LINUX_SCHED_OTHER:
1254 bsd.policy = SCHED_OTHER;
1255 break;
1256 case LINUX_SCHED_FIFO:
1257 bsd.policy = SCHED_FIFO;
1258 break;
1259 case LINUX_SCHED_RR:
1260 bsd.policy = SCHED_RR;
1261 break;
1262 default:
1263 return EINVAL;
1265 bsd.sysmsg_result = 0;
1267 error = sys_sched_get_priority_min(&bsd);
1268 args->sysmsg_result = bsd.sysmsg_result;
1269 return(error);
1272 #define REBOOT_CAD_ON 0x89abcdef
1273 #define REBOOT_CAD_OFF 0
1274 #define REBOOT_HALT 0xcdef0123
1277 sys_linux_reboot(struct linux_reboot_args *args)
1279 struct reboot_args bsd_args;
1280 int error;
1282 #ifdef DEBUG
1283 if (ldebug(reboot))
1284 kprintf(ARGS(reboot, "0x%x"), args->cmd);
1285 #endif
1286 if (args->cmd == REBOOT_CAD_ON || args->cmd == REBOOT_CAD_OFF)
1287 return (0);
1288 bsd_args.opt = (args->cmd == REBOOT_HALT) ? RB_HALT : 0;
1289 bsd_args.sysmsg_result = 0;
1291 error = sys_reboot(&bsd_args);
1292 args->sysmsg_result = bsd_args.sysmsg_result;
1293 return(error);
1297 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1298 * p->p_retval[1] when COMPAT_43 or COMPAT_SUNOS is defined. This
1299 * globbers registers that are assumed to be preserved. The following
1300 * lightweight syscalls fixes this. See also linux_getgid16() and
1301 * linux_getuid16() in linux_uid16.c.
1303 * linux_getpid() - MP SAFE
1304 * linux_getgid() - MP SAFE
1305 * linux_getuid() - MP SAFE
1309 sys_linux_getpid(struct linux_getpid_args *args)
1311 struct thread *td = curthread;
1312 struct proc *p = td->td_proc;
1314 KKASSERT(p);
1316 args->sysmsg_result = p->p_pid;
1317 return (0);
1321 sys_linux_getgid(struct linux_getgid_args *args)
1323 struct thread *td = curthread;
1324 struct proc *p = td->td_proc;
1326 KKASSERT(p);
1328 args->sysmsg_result = p->p_ucred->cr_rgid;
1329 return (0);
1333 sys_linux_getuid(struct linux_getuid_args *args)
1335 struct thread *td = curthread;
1336 struct proc *p = td->td_proc;
1338 KKASSERT(p);
1340 args->sysmsg_result = p->p_ucred->cr_ruid;
1341 return (0);
1345 sys_linux_getsid(struct linux_getsid_args *args)
1347 struct getsid_args bsd;
1348 int error;
1350 bsd.sysmsg_result = 0;
1351 bsd.pid = args->pid;
1352 error = sys_getsid(&bsd);
1353 args->sysmsg_result = bsd.sysmsg_result;
1354 return(error);
1358 linux_nosys(struct nosys_args *args)
1360 /* XXX */
1361 return (ENOSYS);