cb032fb5a8719ab75f78d749b19add97328c57be
[dragonfly.git] / sys / emulation / linux / linux_misc.c
1 /*-
2  * Copyright (c) 1994-1995 Søren Schmidt
3  * All rights reserved.
4  *
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 without specific prior written permission
16  *
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.
27  *
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 $
30  */
31
32 #include "opt_compat.h"
33
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>
59
60 #include <sys/signal2.h>
61 #include <sys/thread2.h>
62 #include <sys/mplock2.h>
63
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>
72
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
80
81 #include <sys/sched.h>
82
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"
90
91 #define BSD_TO_LINUX_SIGNAL(sig)        \
92         (((sig) <= LINUX_SIGTBLSZ) ? bsd_to_linux_signal[_SIG_IDX(sig)] : sig)
93
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
98 };
99
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         l_ushort        pad;            /* explicit padding */
111         l_ulong         totalhigh;      /* Total high memory size */
112         l_ulong         freehigh;       /* Available high memory size */
113         l_uint          mem_unit;       /* Memory unit size in bytes */
114         char            _f[20-2*sizeof(l_long)-sizeof(l_int)]; /* Padding for libc5 */
115 };
116
117 int
118 sys_linux_madvise(struct linux_madvise_args *args)
119 {
120         return 0;
121 }
122
123 /*
124  * MPALMOSTSAFE
125  */
126 int
127 sys_linux_sysinfo(struct linux_sysinfo_args *args)
128 {
129         struct l_sysinfo sysinfo;
130         vm_object_t object;
131         struct timespec ts;
132         int error;
133         int i;
134
135         /* Uptime is copied out of print_uptime() in kern_shutdown.c */
136         getnanouptime(&ts);
137         i = 0;
138         if (ts.tv_sec >= 86400) {
139                 ts.tv_sec %= 86400;
140                 i = 1;
141         }
142         if (i || ts.tv_sec >= 3600) {
143                 ts.tv_sec %= 3600;
144                 i = 1;
145         }
146         if (i || ts.tv_sec >= 60) {
147                 ts.tv_sec %= 60;
148                 i = 1;
149         }
150         sysinfo.uptime=ts.tv_sec;
151
152         /* Use the information from the mib to get our load averages */
153         for (i = 0; i < 3; i++)
154                 sysinfo.loads[i] = averunnable.ldavg[i];
155
156         sysinfo.totalram = Maxmem * PAGE_SIZE;
157         sysinfo.freeram = sysinfo.totalram - vmstats.v_wire_count * PAGE_SIZE;
158
159         get_mplock();
160         sysinfo.sharedram = 0;
161         for (object = TAILQ_FIRST(&vm_object_list); object != NULL;
162              object = TAILQ_NEXT(object, object_list)) {
163                 if (object->type == OBJT_MARKER)
164                         continue;
165                 if (object->shadow_count > 1)
166                         sysinfo.sharedram += object->resident_page_count;
167         }
168
169         sysinfo.sharedram *= PAGE_SIZE;
170         sysinfo.bufferram = 0;
171
172         if (swapblist == NULL) {
173                 sysinfo.totalswap= 0;
174                 sysinfo.freeswap = 0;
175         } else {
176                 sysinfo.totalswap = swapblist->bl_blocks * 1024;
177                 sysinfo.freeswap = swapblist->bl_root->u.bmu_avail * PAGE_SIZE;
178         }
179         rel_mplock();
180
181         sysinfo.procs = nprocs;
182         sysinfo.totalhigh = 0;
183         sysinfo.freehigh = 0;
184         sysinfo.mem_unit = 1; /* Set the basic mem unit to 1 */
185
186         error = copyout(&sysinfo, (caddr_t)args->info, sizeof(sysinfo));
187         return (error);
188 }
189
190 /*
191  * MPALMOSTSAFE
192  */
193 int
194 sys_linux_alarm(struct linux_alarm_args *args)
195 {
196         struct thread *td = curthread;
197         struct proc *p = td->td_proc;
198         struct itimerval it, old_it;
199         struct timeval tv;
200
201 #ifdef DEBUG
202         if (ldebug(alarm))
203                 kprintf(ARGS(alarm, "%u"), args->secs);
204 #endif
205
206         if (args->secs > 100000000)
207                 return EINVAL;
208
209         it.it_value.tv_sec = (long)args->secs;
210         it.it_value.tv_usec = 0;
211         it.it_interval.tv_sec = 0;
212         it.it_interval.tv_usec = 0;
213         get_mplock();
214         crit_enter();
215         old_it = p->p_realtimer;
216         getmicrouptime(&tv);
217         if (timevalisset(&old_it.it_value))
218                 callout_stop(&p->p_ithandle);
219         if (it.it_value.tv_sec != 0) {
220                 callout_reset(&p->p_ithandle, tvtohz_high(&it.it_value),
221                              realitexpire, p);
222                 timevaladd(&it.it_value, &tv);
223         }
224         p->p_realtimer = it;
225         crit_exit();
226         rel_mplock();
227         if (timevalcmp(&old_it.it_value, &tv, >)) {
228                 timevalsub(&old_it.it_value, &tv);
229                 if (old_it.it_value.tv_usec != 0)
230                         old_it.it_value.tv_sec++;
231                 args->sysmsg_result = old_it.it_value.tv_sec;
232         }
233         return 0;
234 }
235
236 /*
237  * MPALMOSTSAFE
238  */
239 int
240 sys_linux_brk(struct linux_brk_args *args)
241 {
242         struct thread *td = curthread;
243         struct proc *p = td->td_proc;
244         struct vmspace *vm;
245         vm_offset_t new, old;
246         struct obreak_args bsd_args;
247
248         get_mplock();
249         vm = p->p_vmspace;
250 #ifdef DEBUG
251         if (ldebug(brk))
252                 kprintf(ARGS(brk, "%p"), (void *)args->dsend);
253 #endif
254         old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
255         new = (vm_offset_t)args->dsend;
256         bsd_args.sysmsg_result = 0;
257         bsd_args.nsize = (char *) new;
258         bsd_args.sysmsg_result = 0;
259         if (((caddr_t)new > vm->vm_daddr) && !sys_obreak(&bsd_args))
260                 args->sysmsg_result = (long)new;
261         else
262                 args->sysmsg_result = (long)old;
263         rel_mplock();
264
265         return 0;
266 }
267
268 /*
269  * MPALMOSTSAFE
270  */
271 int
272 sys_linux_uselib(struct linux_uselib_args *args)
273 {
274         struct thread *td = curthread;
275         struct proc *p;
276         struct nlookupdata nd;
277         struct vnode *vp;
278         struct exec *a_out;
279         struct vattr attr;
280         vm_offset_t vmaddr;
281         unsigned long file_offset;
282         vm_offset_t buffer;
283         unsigned long bss_size;
284         int error;
285         int locked;
286         char *path;
287
288         p = td->td_proc;
289
290         error = linux_copyin_path(args->library, &path, LINUX_PATH_EXISTS);
291         if (error)
292                 return (error);
293 #ifdef DEBUG
294         if (ldebug(uselib))
295                 kprintf(ARGS(uselib, "%s"), path);
296 #endif
297
298         a_out = NULL;
299         locked = 0;
300         vp = NULL;
301
302         get_mplock();
303         error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
304         nd.nl_flags |= NLC_EXEC;
305         if (error == 0)
306                 error = nlookup(&nd);
307         if (error == 0)
308                 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
309         if (error)
310                 goto cleanup;
311         /*
312          * From here on down, we have a locked vnode that must be unlocked.
313          */
314         locked = 1;
315
316         /* Writable? */
317         if (vp->v_writecount) {
318                 error = ETXTBSY;
319                 goto cleanup;
320         }
321
322         /* Executable? */
323         error = VOP_GETATTR(vp, &attr);
324         if (error)
325                 goto cleanup;
326
327         if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
328             ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
329                 error = ENOEXEC;
330                 goto cleanup;
331         }
332
333         /* Sensible size? */
334         if (attr.va_size == 0) {
335                 error = ENOEXEC;
336                 goto cleanup;
337         }
338
339         error = VOP_OPEN(vp, FREAD, td->td_ucred, NULL);
340         if (error)
341                 goto cleanup;
342
343         /*
344          * Lock no longer needed
345          */
346         vn_unlock(vp);
347         locked = 0;
348
349         /* Pull in executable header into kernel_map */
350         error = vm_mmap(&kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE,
351             VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp, 0);
352         if (error)
353                 goto cleanup;
354
355         /* Is it a Linux binary ? */
356         if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
357                 error = ENOEXEC;
358                 goto cleanup;
359         }
360
361         /*
362          * While we are here, we should REALLY do some more checks
363          */
364
365         /* Set file/virtual offset based on a.out variant. */
366         switch ((int)(a_out->a_magic & 0xffff)) {
367         case 0413:      /* ZMAGIC */
368                 file_offset = 1024;
369                 break;
370         case 0314:      /* QMAGIC */
371                 file_offset = 0;
372                 break;
373         default:
374                 error = ENOEXEC;
375                 goto cleanup;
376         }
377
378         bss_size = round_page(a_out->a_bss);
379
380         /* Check various fields in header for validity/bounds. */
381         if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
382                 error = ENOEXEC;
383                 goto cleanup;
384         }
385
386         /* text + data can't exceed file size */
387         if (a_out->a_data + a_out->a_text > attr.va_size) {
388                 error = EFAULT;
389                 goto cleanup;
390         }
391
392         /*
393          * text/data/bss must not exceed limits
394          * XXX - this is not complete. it should check current usage PLUS
395          * the resources needed by this library.
396          */
397         if (a_out->a_text > maxtsiz ||
398             a_out->a_data + bss_size > p->p_rlimit[RLIMIT_DATA].rlim_cur) {
399                 error = ENOMEM;
400                 goto cleanup;
401         }
402
403         /* prevent more writers */
404         vsetflags(vp, VTEXT);
405
406         /*
407          * Check if file_offset page aligned. Currently we cannot handle
408          * misalinged file offsets, and so we read in the entire image
409          * (what a waste).
410          */
411         if (file_offset & PAGE_MASK) {
412 #ifdef DEBUG
413                 kprintf("uselib: Non page aligned binary %lu\n", file_offset);
414 #endif
415                 /* Map text+data read/write/execute */
416
417                 /* a_entry is the load address and is page aligned */
418                 vmaddr = trunc_page(a_out->a_entry);
419
420                 /* get anon user mapping, read+write+execute */
421                 error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0,
422                                     &vmaddr, a_out->a_text + a_out->a_data,
423                                     PAGE_SIZE,
424                                     FALSE, VM_MAPTYPE_NORMAL,
425                                     VM_PROT_ALL, VM_PROT_ALL,
426                                     0);
427                 if (error)
428                         goto cleanup;
429
430                 /* map file into kernel_map */
431                 error = vm_mmap(&kernel_map, &buffer,
432                     round_page(a_out->a_text + a_out->a_data + file_offset),
433                     VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp,
434                     trunc_page(file_offset));
435                 if (error)
436                         goto cleanup;
437
438                 /* copy from kernel VM space to user space */
439                 error = copyout((caddr_t)(uintptr_t)(buffer + file_offset),
440                     (caddr_t)vmaddr, a_out->a_text + a_out->a_data);
441
442                 /* release temporary kernel space */
443                 vm_map_remove(&kernel_map, buffer, buffer +
444                     round_page(a_out->a_text + a_out->a_data + file_offset));
445
446                 if (error)
447                         goto cleanup;
448         } else {
449 #ifdef DEBUG
450                 kprintf("uselib: Page aligned binary %lu\n", file_offset);
451 #endif
452                 /*
453                  * for QMAGIC, a_entry is 20 bytes beyond the load address
454                  * to skip the executable header
455                  */
456                 vmaddr = trunc_page(a_out->a_entry);
457
458                 /*
459                  * Map it all into the process's space as a single
460                  * copy-on-write "data" segment.
461                  */
462                 error = vm_mmap(&p->p_vmspace->vm_map, &vmaddr,
463                     a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
464                     MAP_PRIVATE | MAP_FIXED, (caddr_t)vp, file_offset);
465                 if (error)
466                         goto cleanup;
467         }
468 #ifdef DEBUG
469         kprintf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long*)vmaddr)[0],
470             ((long*)vmaddr)[1]);
471 #endif
472         if (bss_size != 0) {
473                 /* Calculate BSS start address */
474                 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
475                     a_out->a_data;
476
477                 /* allocate some 'anon' space */
478                 error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0,
479                                     &vmaddr, bss_size,
480                                     PAGE_SIZE,
481                                     FALSE, VM_MAPTYPE_NORMAL,
482                                     VM_PROT_ALL, VM_PROT_ALL,
483                                     0);
484                 if (error)
485                         goto cleanup;
486         }
487
488 cleanup:
489         /* Unlock/release vnode */
490         if (vp) {
491                 if (locked)
492                         vn_unlock(vp);
493                 vrele(vp);
494         }
495         /* Release the kernel mapping. */
496         if (a_out) {
497                 vm_map_remove(&kernel_map, (vm_offset_t)a_out,
498                     (vm_offset_t)a_out + PAGE_SIZE);
499         }
500         nlookup_done(&nd);
501         rel_mplock();
502         linux_free_path(&path);
503         return (error);
504 }
505
506 /*
507  * MPSAFE
508  */
509 int
510 sys_linux_select(struct linux_select_args *args)
511 {
512         struct select_args bsa;
513         struct timeval tv0, tv1, utv, *tvp;
514         caddr_t sg;
515         int error;
516
517 #ifdef DEBUG
518         if (ldebug(select))
519                 kprintf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
520                     (void *)args->readfds, (void *)args->writefds,
521                     (void *)args->exceptfds, (void *)args->timeout);
522 #endif
523
524         error = 0;
525         bsa.sysmsg_result = 0;
526         bsa.nd = args->nfds;
527         bsa.in = args->readfds;
528         bsa.ou = args->writefds;
529         bsa.ex = args->exceptfds;
530         bsa.tv = (struct timeval *)args->timeout;
531
532         /*
533          * Store current time for computation of the amount of
534          * time left.
535          */
536         if (args->timeout) {
537                 if ((error = copyin((caddr_t)args->timeout, &utv,
538                     sizeof(utv))))
539                         goto select_out;
540 #ifdef DEBUG
541                 if (ldebug(select))
542                         kprintf(LMSG("incoming timeout (%ld/%ld)"),
543                             utv.tv_sec, utv.tv_usec);
544 #endif
545
546                 if (itimerfix(&utv)) {
547                         /*
548                          * The timeval was invalid.  Convert it to something
549                          * valid that will act as it does under Linux.
550                          */
551                         sg = stackgap_init();
552                         tvp = stackgap_alloc(&sg, sizeof(utv));
553                         utv.tv_sec += utv.tv_usec / 1000000;
554                         utv.tv_usec %= 1000000;
555                         if (utv.tv_usec < 0) {
556                                 utv.tv_sec -= 1;
557                                 utv.tv_usec += 1000000;
558                         }
559                         if (utv.tv_sec < 0)
560                                 timevalclear(&utv);
561                         if ((error = copyout(&utv, tvp, sizeof(utv))))
562                                 goto select_out;
563                         bsa.tv = tvp;
564                 }
565                 microtime(&tv0);
566         }
567
568         error = sys_select(&bsa);
569         args->sysmsg_result = bsa.sysmsg_result;
570 #ifdef DEBUG
571         if (ldebug(select))
572                 kprintf(LMSG("real select returns %d"), error);
573 #endif
574         if (error) {
575                 /*
576                  * See fs/select.c in the Linux kernel.  Without this,
577                  * Maelstrom doesn't work.
578                  */
579                 if (error == ERESTART)
580                         error = EINTR;
581                 goto select_out;
582         }
583
584         if (args->timeout) {
585                 if (args->sysmsg_result) {
586                         /*
587                          * Compute how much time was left of the timeout,
588                          * by subtracting the current time and the time
589                          * before we started the call, and subtracting
590                          * that result from the user-supplied value.
591                          */
592                         microtime(&tv1);
593                         timevalsub(&tv1, &tv0);
594                         timevalsub(&utv, &tv1);
595                         if (utv.tv_sec < 0)
596                                 timevalclear(&utv);
597                 } else
598                         timevalclear(&utv);
599 #ifdef DEBUG
600                 if (ldebug(select))
601                         kprintf(LMSG("outgoing timeout (%ld/%ld)"),
602                             utv.tv_sec, utv.tv_usec);
603 #endif
604                 if ((error = copyout(&utv, (caddr_t)args->timeout,
605                     sizeof(utv))))
606                         goto select_out;
607         }
608
609 select_out:
610 #ifdef DEBUG
611         if (ldebug(select))
612                 kprintf(LMSG("select_out -> %d"), error);
613 #endif
614         return error;
615 }
616
617 /*
618  * MPSAFE
619  */
620 int     
621 sys_linux_mremap(struct linux_mremap_args *args)
622 {
623         struct munmap_args bsd_args; 
624         int error = 0;
625
626 #ifdef DEBUG
627         if (ldebug(mremap))
628                 kprintf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
629                     (void *)args->addr, 
630                     (unsigned long)args->old_len, 
631                     (unsigned long)args->new_len,
632                     (unsigned long)args->flags);
633 #endif
634         if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) {
635                 args->sysmsg_resultp = NULL;
636                 return (EINVAL);
637         }
638
639         /*
640          * Check for the page alignment.
641          * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK.
642          */
643         if (args->addr & PAGE_MASK) {
644                 args->sysmsg_resultp = NULL;
645                 return (EINVAL);
646         }
647
648         args->new_len = round_page(args->new_len);
649         args->old_len = round_page(args->old_len);
650
651         if (args->new_len > args->old_len) {
652                 args->sysmsg_result = 0;
653                 return ENOMEM;
654         }
655
656         if (args->new_len < args->old_len) {
657                 bsd_args.sysmsg_result = 0;
658                 bsd_args.addr = (caddr_t)(args->addr + args->new_len);
659                 bsd_args.len = args->old_len - args->new_len;
660                 error = sys_munmap(&bsd_args);
661         }
662
663         args->sysmsg_resultp = error ? NULL : (void *)args->addr;
664         return error;
665 }
666
667 #define LINUX_MS_ASYNC          0x0001
668 #define LINUX_MS_INVALIDATE     0x0002
669 #define LINUX_MS_SYNC           0x0004
670
671 /*
672  * MPSAFE
673  */
674 int
675 sys_linux_msync(struct linux_msync_args *args)
676 {
677         struct msync_args bsd_args;
678         int error;
679
680         bsd_args.addr = (caddr_t)args->addr;
681         bsd_args.len = args->len;
682         bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
683         bsd_args.sysmsg_result = 0;
684
685         error = sys_msync(&bsd_args);
686         args->sysmsg_result = bsd_args.sysmsg_result;
687         return(error);
688 }
689
690 /*
691  * MPSAFE
692  */
693 int
694 sys_linux_time(struct linux_time_args *args)
695 {
696         struct timeval tv;
697         l_time_t tm;
698         int error;
699
700 #ifdef DEBUG
701         if (ldebug(time))
702                 kprintf(ARGS(time, "*"));
703 #endif
704
705         microtime(&tv);
706         tm = tv.tv_sec;
707         if (args->tm && (error = copyout(&tm, (caddr_t)args->tm, sizeof(tm))))
708                 return error;
709         args->sysmsg_lresult = tm;
710         return 0;
711 }
712
713 struct l_times_argv {
714         l_long          tms_utime;
715         l_long          tms_stime;
716         l_long          tms_cutime;
717         l_long          tms_cstime;
718 };
719
720 #define CLK_TCK 100     /* Linux uses 100 */
721
722 #define CONVTCK(r)      (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
723
724 /*
725  * MPALMOSTSAFE
726  */
727 int
728 sys_linux_times(struct linux_times_args *args)
729 {
730         struct thread *td = curthread;
731         struct proc *p = td->td_proc;
732         struct timeval tv;
733         struct l_times_argv tms;
734         struct rusage ru;
735         int error;
736
737 #ifdef DEBUG
738         if (ldebug(times))
739                 kprintf(ARGS(times, "*"));
740 #endif
741
742         get_mplock();
743         calcru_proc(p, &ru);
744         rel_mplock();
745
746         tms.tms_utime = CONVTCK(ru.ru_utime);
747         tms.tms_stime = CONVTCK(ru.ru_stime);
748
749         tms.tms_cutime = CONVTCK(p->p_cru.ru_utime);
750         tms.tms_cstime = CONVTCK(p->p_cru.ru_stime);
751
752         if ((error = copyout(&tms, (caddr_t)args->buf, sizeof(tms))))
753                 return error;
754
755         microuptime(&tv);
756         args->sysmsg_result = (int)CONVTCK(tv);
757         return 0;
758 }
759
760 /*
761  * MPALMOSTSAFE
762  */
763 int
764 sys_linux_newuname(struct linux_newuname_args *args)
765 {
766         struct thread *td = curthread;
767         struct l_new_utsname utsname;
768         char *osrelease, *osname;
769
770 #ifdef DEBUG
771         if (ldebug(newuname))
772                 kprintf(ARGS(newuname, "*"));
773 #endif
774
775         get_mplock();
776         osname = linux_get_osname(td);
777         osrelease = linux_get_osrelease(td);
778
779         bzero(&utsname, sizeof(utsname));
780         strncpy(utsname.sysname, osname, LINUX_MAX_UTSNAME-1);
781         strncpy(utsname.nodename, hostname, LINUX_MAX_UTSNAME-1);
782         strncpy(utsname.release, osrelease, LINUX_MAX_UTSNAME-1);
783         strncpy(utsname.version, version, LINUX_MAX_UTSNAME-1);
784         strncpy(utsname.machine, machine, LINUX_MAX_UTSNAME-1);
785         strncpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME-1);
786         rel_mplock();
787
788         return (copyout(&utsname, (caddr_t)args->buf, sizeof(utsname)));
789 }
790
791 /* XXX: why would this be i386-only? most of these are wrong! */
792 #if defined(__i386__)
793 struct l_utimbuf {
794         l_time_t l_actime;
795         l_time_t l_modtime;
796 };
797
798 /*
799  * MPALMOSTSAFE
800  */
801 int
802 sys_linux_utime(struct linux_utime_args *args)
803 {
804         struct timeval tv[2];
805         struct l_utimbuf lut;
806         struct nlookupdata nd;
807         char *path;
808         int error;
809
810         error = linux_copyin_path(args->fname, &path, LINUX_PATH_EXISTS);
811         if (error)
812                 return (error);
813 #ifdef DEBUG
814         if (ldebug(utime))
815                 kprintf(ARGS(utime, "%s, *"), path);
816 #endif
817
818         if (args->times) {
819                 error = copyin(args->times, &lut, sizeof(lut));
820                 if (error)
821                         goto cleanup;
822                 tv[0].tv_sec = lut.l_actime;
823                 tv[0].tv_usec = 0;
824                 tv[1].tv_sec = lut.l_modtime;
825                 tv[1].tv_usec = 0;
826         }
827         get_mplock();
828         error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
829         if (error == 0)
830                 error = kern_utimes(&nd, args->times ? tv : NULL);
831         nlookup_done(&nd);
832         rel_mplock();
833 cleanup:
834         linux_free_path(&path);
835         return (error);
836 }
837
838 int
839 sys_linux_utimes(struct linux_utimes_args *args)
840 {
841         l_timeval ltv[2];
842         struct timeval tv[2], *tvp = NULL;
843         struct nlookupdata nd;
844         char *path;
845         int error;
846
847         error = linux_copyin_path(args->fname, &path, LINUX_PATH_EXISTS);
848         if (error)
849                 return (error);
850 #ifdef DEBUG
851         if (ldebug(utimes))
852                 kprintf(ARGS(utimes, "%s, *"), path);
853 #endif
854
855         if (args->tptr) {
856                 error = copyin(args->tptr, ltv, sizeof(ltv));
857                 if (error)
858                         goto cleanup;
859                 tv[0].tv_sec = ltv[0].tv_sec;
860                 tv[0].tv_usec = ltv[0].tv_usec;
861                 tv[1].tv_sec = ltv[1].tv_sec;
862                 tv[1].tv_usec = ltv[1].tv_usec;
863                 tvp = tv;
864         }
865         get_mplock();
866         error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
867         if (error == 0)
868                 error = kern_utimes(&nd, tvp);
869         nlookup_done(&nd);
870         rel_mplock();
871 cleanup:
872         linux_free_path(&path);
873         return (error);
874 }
875
876 int
877 sys_linux_futimesat(struct linux_futimesat_args *args)
878 {
879         l_timeval ltv[2];
880         struct timeval tv[2], *tvp = NULL;
881         struct file *fp;
882         struct nlookupdata nd;
883         char *path;
884         int dfd,error;
885
886         error = linux_copyin_path(args->fname, &path, LINUX_PATH_EXISTS);
887         if (error)
888                 return (error);
889 #ifdef DEBUG
890         if (ldebug(futimesat))
891                 kprintf(ARGS(futimesat, "%s, *"), path);
892 #endif
893         if (args->tptr) {
894                 error = copyin(args->tptr, ltv, sizeof(ltv));
895                 if (error)
896                         goto cleanup;
897                 tv[0].tv_sec = ltv[0].tv_sec;
898                 tv[0].tv_usec = ltv[0].tv_usec;
899                 tv[1].tv_sec = ltv[1].tv_sec;
900                 tv[1].tv_usec = ltv[1].tv_usec;
901                 tvp = tv;
902         }
903         dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
904         get_mplock();
905         error = nlookup_init_at(&nd, &fp, dfd, path, UIO_SYSSPACE, NLC_FOLLOW);
906         if (error == 0)
907                 error = kern_utimes(&nd, tvp);
908         nlookup_done_at(&nd, fp);
909         rel_mplock();
910 cleanup:
911         linux_free_path(&path);
912         return (error);
913 }
914
915
916 int
917 sys_linux_utimensat(struct linux_utimensat_args *args)
918 {
919         struct l_timespec ltv[2];
920         struct timeval tv[2], *tvp = NULL;
921         struct file *fp;
922         struct nlookupdata nd;
923         char *path;
924         int dfd, flags, error = 0;
925
926         if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
927                 return (EINVAL);
928
929         if (args->dfd == LINUX_AT_FDCWD && args->fname == NULL)
930                 return (EINVAL);
931
932         if (args->fname) {
933                 error = linux_copyin_path(args->fname, &path, LINUX_PATH_EXISTS);
934                 if (error)
935                         return (error);
936         }
937 #ifdef DEBUG
938         if (ldebug(utimensat))
939                 kprintf(ARGS(utimensat, "%s, *"), path);
940 #endif
941         if (args->tptr) {
942                 error = copyin(args->tptr, ltv, sizeof(ltv));
943                 if (error)
944                         goto cleanup;
945
946                 if (ltv[0].tv_sec == LINUX_UTIME_NOW) {
947                         microtime(&tv[0]);
948                 } else if (ltv[0].tv_sec == LINUX_UTIME_OMIT) {
949                         /* XXX: this is not right, but will do for now */
950                         microtime(&tv[0]);
951                 } else {
952                         tv[0].tv_sec = ltv[0].tv_sec;
953                         /* XXX: we lose precision here, as we don't have ns */
954                         tv[0].tv_usec = ltv[0].tv_nsec/1000;
955                 }
956                 if (ltv[1].tv_sec == LINUX_UTIME_NOW) {
957                         microtime(&tv[1]);
958                 } else if (ltv[1].tv_sec == LINUX_UTIME_OMIT) {
959                         /* XXX: this is not right, but will do for now */
960                         microtime(&tv[1]);
961                 } else {
962                         tv[1].tv_sec = ltv[1].tv_sec;
963                         /* XXX: we lose precision here, as we don't have ns */
964                         tv[1].tv_usec = ltv[1].tv_nsec/1000;
965                 }
966                 tvp = tv;
967         }
968
969         dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
970         flags = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
971
972         get_mplock();
973         if (args->fname) {
974                 error = nlookup_init_at(&nd, &fp, dfd, path, UIO_SYSSPACE, flags);
975                 if (error == 0)
976                         error = kern_utimes(&nd, tvp);
977                 nlookup_done_at(&nd, fp);
978         } else {
979                 /* Thank you, Linux, for another non-standard "feature" */
980                 KKASSERT(dfd != AT_FDCWD);
981                 error = kern_futimes(dfd, tvp);
982         }
983         rel_mplock();
984 cleanup:
985         if (args->fname)
986                 linux_free_path(&path);
987
988         return (error);
989 }
990 #endif /* __i386__ */
991
992 #define __WCLONE 0x80000000
993
994 /*
995  * MPALMOSTSAFE
996  */
997 int
998 sys_linux_waitpid(struct linux_waitpid_args *args)
999 {
1000         int error, options, status;
1001
1002 #ifdef DEBUG
1003         if (ldebug(waitpid))
1004                 kprintf(ARGS(waitpid, "%d, %p, %d"),
1005                     args->pid, (void *)args->status, args->options);
1006 #endif
1007         options = args->options & (WNOHANG | WUNTRACED);
1008         /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
1009         if (args->options & __WCLONE)
1010                 options |= WLINUXCLONE;
1011
1012         error = kern_wait(args->pid, args->status ? &status : NULL, options,
1013                           NULL, &args->sysmsg_result);
1014
1015         if (error == 0 && args->status) {
1016                 status &= 0xffff;
1017                 if (WIFSIGNALED(status))
1018                         status = (status & 0xffffff80) |
1019                             BSD_TO_LINUX_SIGNAL(WTERMSIG(status));
1020                 else if (WIFSTOPPED(status))
1021                         status = (status & 0xffff00ff) |
1022                             (BSD_TO_LINUX_SIGNAL(WSTOPSIG(status)) << 8);
1023                 error = copyout(&status, args->status, sizeof(status));
1024         }
1025
1026         return (error);
1027 }
1028
1029 /*
1030  * MPALMOSTSAFE
1031  */
1032 int
1033 sys_linux_wait4(struct linux_wait4_args *args)
1034 {
1035         struct thread *td = curthread;
1036         struct lwp *lp = td->td_lwp;
1037         struct rusage rusage;
1038         int error, options, status;
1039
1040 #ifdef DEBUG
1041         if (ldebug(wait4))
1042                 kprintf(ARGS(wait4, "%d, %p, %d, %p"),
1043                     args->pid, (void *)args->status, args->options,
1044                     (void *)args->rusage);
1045 #endif
1046         options = args->options & (WNOHANG | WUNTRACED);
1047         /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
1048         if (args->options & __WCLONE)
1049                 options |= WLINUXCLONE;
1050
1051         error = kern_wait(args->pid, args->status ? &status : NULL, options,
1052                           args->rusage ? &rusage : NULL, &args->sysmsg_result);
1053
1054         if (error == 0)
1055                 lwp_delsig(lp, SIGCHLD);
1056
1057         if (error == 0 && args->status) {
1058                 status &= 0xffff;
1059                 if (WIFSIGNALED(status))
1060                         status = (status & 0xffffff80) |
1061                             BSD_TO_LINUX_SIGNAL(WTERMSIG(status));
1062                 else if (WIFSTOPPED(status))
1063                         status = (status & 0xffff00ff) |
1064                             (BSD_TO_LINUX_SIGNAL(WSTOPSIG(status)) << 8);
1065                 error = copyout(&status, args->status, sizeof(status));
1066         }
1067         if (error == 0 && args->rusage)
1068                 error = copyout(&rusage, args->rusage, sizeof(rusage));
1069
1070         return (error);
1071 }
1072
1073 /*
1074  * MPALMOSTSAFE
1075  */
1076 int
1077 sys_linux_mknod(struct linux_mknod_args *args)
1078 {
1079         struct nlookupdata nd;
1080         char *path;
1081         int error;
1082
1083         error = linux_copyin_path(args->path, &path, LINUX_PATH_CREATE);
1084         if (error)
1085                 return (error);
1086 #ifdef DEBUG
1087         if (ldebug(mknod))
1088                 kprintf(ARGS(mknod, "%s, %d, %d"),
1089                     path, args->mode, args->dev);
1090 #endif
1091         get_mplock();
1092         error = nlookup_init(&nd, path, UIO_SYSSPACE, 0);
1093         if (error == 0) {
1094                 if (args->mode & S_IFIFO) {
1095                         error = kern_mkfifo(&nd, args->mode);
1096                 } else {
1097                         error = kern_mknod(&nd, args->mode,
1098                                            umajor(args->dev),
1099                                            uminor(args->dev));
1100                 }
1101         }
1102         nlookup_done(&nd);
1103         rel_mplock();
1104
1105         linux_free_path(&path);
1106         return(error);
1107 }
1108
1109 int
1110 sys_linux_mknodat(struct linux_mknodat_args *args)
1111 {
1112         struct nlookupdata nd;
1113         struct file *fp;
1114         char *path;
1115         int dfd, error;
1116
1117         error = linux_copyin_path(args->path, &path, LINUX_PATH_CREATE);
1118         if (error)
1119                 return (error);
1120 #ifdef DEBUG
1121         if (ldebug(mknod))
1122                 kprintf(ARGS(mknod, "%s, %d, %d"),
1123                     path, args->mode, args->dev);
1124 #endif
1125         get_mplock();
1126         dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1127         error = nlookup_init_at(&nd, &fp, dfd, path, UIO_SYSSPACE, 0);
1128         if (error == 0) {
1129                 if (args->mode & S_IFIFO) {
1130                         error = kern_mkfifo(&nd, args->mode);
1131                 } else {
1132                         error = kern_mknod(&nd, args->mode,
1133                                            umajor(args->dev),
1134                                            uminor(args->dev));
1135                 }
1136         }
1137         nlookup_done_at(&nd, fp);
1138         rel_mplock();
1139
1140         linux_free_path(&path);
1141         return(error);
1142 }
1143
1144 /*
1145  * UGH! This is just about the dumbest idea I've ever heard!!
1146  *
1147  * MPSAFE
1148  */
1149 int
1150 sys_linux_personality(struct linux_personality_args *args)
1151 {
1152 #ifdef DEBUG
1153         if (ldebug(personality))
1154                 kprintf(ARGS(personality, "%d"), args->per);
1155 #endif
1156         if (args->per != 0)
1157                 return EINVAL;
1158
1159         /* Yes Jim, it's still a Linux... */
1160         args->sysmsg_result = 0;
1161         return 0;
1162 }
1163
1164 /*
1165  * Wrappers for get/setitimer for debugging..
1166  *
1167  * MPSAFE
1168  */
1169 int
1170 sys_linux_setitimer(struct linux_setitimer_args *args)
1171 {
1172         struct setitimer_args bsa;
1173         struct itimerval foo;
1174         int error;
1175
1176 #ifdef DEBUG
1177         if (ldebug(setitimer))
1178                 kprintf(ARGS(setitimer, "%p, %p"),
1179                     (void *)args->itv, (void *)args->oitv);
1180 #endif
1181         bsa.which = args->which;
1182         bsa.itv = (struct itimerval *)args->itv;
1183         bsa.oitv = (struct itimerval *)args->oitv;
1184         bsa.sysmsg_result = 0;
1185         if (args->itv) {
1186             if ((error = copyin((caddr_t)args->itv, &foo, sizeof(foo))))
1187                 return error;
1188 #ifdef DEBUG
1189             if (ldebug(setitimer)) {
1190                 kprintf("setitimer: value: sec: %ld, usec: %ld\n",
1191                     foo.it_value.tv_sec, foo.it_value.tv_usec);
1192                 kprintf("setitimer: interval: sec: %ld, usec: %ld\n",
1193                     foo.it_interval.tv_sec, foo.it_interval.tv_usec);
1194             }
1195 #endif
1196         }
1197         error = sys_setitimer(&bsa);
1198         args->sysmsg_result = bsa.sysmsg_result;
1199         return(error);
1200 }
1201
1202 /*
1203  * MPSAFE
1204  */
1205 int
1206 sys_linux_getitimer(struct linux_getitimer_args *args)
1207 {
1208         struct getitimer_args bsa;
1209         int error;
1210 #ifdef DEBUG
1211         if (ldebug(getitimer))
1212                 kprintf(ARGS(getitimer, "%p"), (void *)args->itv);
1213 #endif
1214         bsa.which = args->which;
1215         bsa.itv = (struct itimerval *)args->itv;
1216         bsa.sysmsg_result = 0;
1217         error = sys_getitimer(&bsa);
1218         args->sysmsg_result = bsa.sysmsg_result;
1219         return(error);
1220 }
1221
1222 /*
1223  * MPSAFE
1224  */
1225 int
1226 sys_linux_nice(struct linux_nice_args *args)
1227 {
1228         struct setpriority_args bsd_args;
1229         int error;
1230
1231         bsd_args.which = PRIO_PROCESS;
1232         bsd_args.who = 0;       /* current process */
1233         bsd_args.prio = args->inc;
1234         bsd_args.sysmsg_result = 0;
1235         error = sys_setpriority(&bsd_args);
1236         args->sysmsg_result = bsd_args.sysmsg_result;
1237         return(error);
1238 }
1239
1240 /*
1241  * MPALMOSTSAFE
1242  */
1243 int
1244 sys_linux_setgroups(struct linux_setgroups_args *args)
1245 {
1246         struct thread *td = curthread;
1247         struct proc *p = td->td_proc;
1248         struct ucred *newcred, *oldcred;
1249         l_gid_t linux_gidset[NGROUPS];
1250         gid_t *bsd_gidset;
1251         int ngrp, error;
1252
1253         ngrp = args->gidsetsize;
1254         oldcred = td->td_ucred;
1255
1256         /*
1257          * cr_groups[0] holds egid. Setting the whole set from
1258          * the supplied set will cause egid to be changed too.
1259          * Keep cr_groups[0] unchanged to prevent that.
1260          */
1261
1262         if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0)
1263                 return (error);
1264
1265         if ((u_int)ngrp >= NGROUPS)
1266                 return (EINVAL);
1267
1268         get_mplock();
1269         newcred = crdup(oldcred);
1270         if (ngrp > 0) {
1271                 error = copyin((caddr_t)args->grouplist, linux_gidset,
1272                                ngrp * sizeof(l_gid_t));
1273                 if (error) {
1274                         crfree(newcred);
1275                         rel_mplock();
1276                         return (error);
1277                 }
1278
1279                 newcred->cr_ngroups = ngrp + 1;
1280
1281                 bsd_gidset = newcred->cr_groups;
1282                 ngrp--;
1283                 while (ngrp >= 0) {
1284                         bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1285                         ngrp--;
1286                 }
1287         } else {
1288                 newcred->cr_ngroups = 1;
1289         }
1290
1291         setsugid();
1292         oldcred = p->p_ucred;   /* reload, deal with threads race */
1293         p->p_ucred = newcred;
1294         crfree(oldcred);
1295         rel_mplock();
1296         return (0);
1297 }
1298
1299 /*
1300  * MPSAFE
1301  */
1302 int
1303 sys_linux_getgroups(struct linux_getgroups_args *args)
1304 {
1305         struct thread *td = curthread;
1306         struct ucred *cred;
1307         l_gid_t linux_gidset[NGROUPS];
1308         gid_t *bsd_gidset;
1309         int bsd_gidsetsz, ngrp, error;
1310
1311         cred = td->td_ucred;
1312         bsd_gidset = cred->cr_groups;
1313         bsd_gidsetsz = cred->cr_ngroups - 1;
1314
1315         /*
1316          * cr_groups[0] holds egid. Returning the whole set
1317          * here will cause a duplicate. Exclude cr_groups[0]
1318          * to prevent that.
1319          */
1320
1321         if ((ngrp = args->gidsetsize) == 0) {
1322                 args->sysmsg_result = bsd_gidsetsz;
1323                 return (0);
1324         }
1325
1326         if ((u_int)ngrp < bsd_gidsetsz)
1327                 return (EINVAL);
1328
1329         ngrp = 0;
1330         while (ngrp < bsd_gidsetsz) {
1331                 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1332                 ngrp++;
1333         }
1334
1335         if ((error = copyout(linux_gidset, args->grouplist,
1336                              ngrp * sizeof(l_gid_t)))) {
1337                 return (error);
1338         }
1339
1340         args->sysmsg_result = ngrp;
1341         return (0);
1342 }
1343
1344 /*
1345  * MPSAFE
1346  */
1347 int
1348 sys_linux_setrlimit(struct linux_setrlimit_args *args)
1349 {
1350         struct l_rlimit linux_rlim;
1351         struct rlimit rlim;
1352         u_int which;
1353         int error;
1354
1355 #ifdef DEBUG
1356         if (ldebug(setrlimit))
1357                 kprintf(ARGS(setrlimit, "%d, %p"),
1358                     args->resource, (void *)args->rlim);
1359 #endif
1360         if (args->resource >= LINUX_RLIM_NLIMITS)
1361                 return (EINVAL);
1362         which = linux_to_bsd_resource[args->resource];
1363         if (which == -1)
1364                 return (EINVAL);
1365
1366         error = copyin(args->rlim, &linux_rlim, sizeof(linux_rlim));
1367         if (error)
1368                 return (error);
1369         rlim.rlim_cur = (rlim_t)linux_rlim.rlim_cur;
1370         rlim.rlim_max = (rlim_t)linux_rlim.rlim_max;
1371
1372         error = kern_setrlimit(which, &rlim);
1373
1374         return(error);
1375 }
1376
1377 /*
1378  * MPSAFE
1379  */
1380 int
1381 sys_linux_old_getrlimit(struct linux_old_getrlimit_args *args)
1382 {
1383         struct l_rlimit linux_rlim;
1384         struct rlimit rlim;
1385         u_int which;
1386         int error;
1387
1388 #ifdef DEBUG
1389         if (ldebug(old_getrlimit))
1390                 kprintf(ARGS(old_getrlimit, "%d, %p"),
1391                     args->resource, (void *)args->rlim);
1392 #endif
1393         if (args->resource >= LINUX_RLIM_NLIMITS)
1394                 return (EINVAL);
1395         which = linux_to_bsd_resource[args->resource];
1396         if (which == -1)
1397                 return (EINVAL);
1398
1399         error = kern_getrlimit(which, &rlim);
1400
1401         if (error == 0) {
1402                 linux_rlim.rlim_cur = (l_ulong)rlim.rlim_cur;
1403                 if (linux_rlim.rlim_cur == ULONG_MAX)
1404                         linux_rlim.rlim_cur = LONG_MAX;
1405                 linux_rlim.rlim_max = (l_ulong)rlim.rlim_max;
1406                 if (linux_rlim.rlim_max == ULONG_MAX)
1407                         linux_rlim.rlim_max = LONG_MAX;
1408                 error = copyout(&linux_rlim, args->rlim, sizeof(linux_rlim));
1409         }
1410         return (error);
1411 }
1412
1413 /*
1414  * MPSAFE
1415  */
1416 int
1417 sys_linux_getrlimit(struct linux_getrlimit_args *args)
1418 {
1419         struct l_rlimit linux_rlim;
1420         struct rlimit rlim;
1421         u_int which;
1422         int error;
1423
1424 #ifdef DEBUG
1425         if (ldebug(getrlimit))
1426                 kprintf(ARGS(getrlimit, "%d, %p"),
1427                     args->resource, (void *)args->rlim);
1428 #endif
1429         if (args->resource >= LINUX_RLIM_NLIMITS)
1430                 return (EINVAL);
1431         which = linux_to_bsd_resource[args->resource];
1432         if (which == -1)
1433                 return (EINVAL);
1434
1435         error = kern_getrlimit(which, &rlim);
1436
1437         if (error == 0) {
1438                 linux_rlim.rlim_cur = (l_ulong)rlim.rlim_cur;
1439                 linux_rlim.rlim_max = (l_ulong)rlim.rlim_max;
1440                 error = copyout(&linux_rlim, args->rlim, sizeof(linux_rlim));
1441         }
1442         return (error);
1443 }
1444
1445 /*
1446  * MPSAFE
1447  */
1448 int
1449 sys_linux_sched_setscheduler(struct linux_sched_setscheduler_args *args)
1450 {
1451         struct sched_setscheduler_args bsd;
1452         int error;
1453
1454 #ifdef DEBUG
1455         if (ldebug(sched_setscheduler))
1456                 kprintf(ARGS(sched_setscheduler, "%d, %d, %p"),
1457                     args->pid, args->policy, (const void *)args->param);
1458 #endif
1459
1460         switch (args->policy) {
1461         case LINUX_SCHED_OTHER:
1462                 bsd.policy = SCHED_OTHER;
1463                 break;
1464         case LINUX_SCHED_FIFO:
1465                 bsd.policy = SCHED_FIFO;
1466                 break;
1467         case LINUX_SCHED_RR:
1468                 bsd.policy = SCHED_RR;
1469                 break;
1470         default:
1471                 return EINVAL;
1472         }
1473
1474         bsd.pid = args->pid;
1475         bsd.param = (struct sched_param *)args->param;
1476         bsd.sysmsg_result = 0;
1477
1478         error = sys_sched_setscheduler(&bsd);
1479         args->sysmsg_result = bsd.sysmsg_result;
1480         return(error);
1481 }
1482
1483 /*
1484  * MPSAFE
1485  */
1486 int
1487 sys_linux_sched_getscheduler(struct linux_sched_getscheduler_args *args)
1488 {
1489         struct sched_getscheduler_args bsd;
1490         int error;
1491
1492 #ifdef DEBUG
1493         if (ldebug(sched_getscheduler))
1494                 kprintf(ARGS(sched_getscheduler, "%d"), args->pid);
1495 #endif
1496
1497         bsd.sysmsg_result = 0;
1498         bsd.pid = args->pid;
1499         error = sys_sched_getscheduler(&bsd);
1500         args->sysmsg_result = bsd.sysmsg_result;
1501
1502         switch (args->sysmsg_result) {
1503         case SCHED_OTHER:
1504                 args->sysmsg_result = LINUX_SCHED_OTHER;
1505                 break;
1506         case SCHED_FIFO:
1507                 args->sysmsg_result = LINUX_SCHED_FIFO;
1508                 break;
1509         case SCHED_RR:
1510                 args->sysmsg_result = LINUX_SCHED_RR;
1511                 break;
1512         }
1513         return error;
1514 }
1515
1516 /*
1517  * MPSAFE
1518  */
1519 int
1520 sys_linux_sched_get_priority_max(struct linux_sched_get_priority_max_args *args)
1521 {
1522         struct sched_get_priority_max_args bsd;
1523         int error;
1524
1525 #ifdef DEBUG
1526         if (ldebug(sched_get_priority_max))
1527                 kprintf(ARGS(sched_get_priority_max, "%d"), args->policy);
1528 #endif
1529
1530         switch (args->policy) {
1531         case LINUX_SCHED_OTHER:
1532                 bsd.policy = SCHED_OTHER;
1533                 break;
1534         case LINUX_SCHED_FIFO:
1535                 bsd.policy = SCHED_FIFO;
1536                 break;
1537         case LINUX_SCHED_RR:
1538                 bsd.policy = SCHED_RR;
1539                 break;
1540         default:
1541                 return EINVAL;
1542         }
1543         bsd.sysmsg_result = 0;
1544
1545         error = sys_sched_get_priority_max(&bsd);
1546         args->sysmsg_result = bsd.sysmsg_result;
1547         return(error);
1548 }
1549
1550 /*
1551  * MPSAFE
1552  */
1553 int
1554 sys_linux_sched_get_priority_min(struct linux_sched_get_priority_min_args *args)
1555 {
1556         struct sched_get_priority_min_args bsd;
1557         int error;
1558
1559 #ifdef DEBUG
1560         if (ldebug(sched_get_priority_min))
1561                 kprintf(ARGS(sched_get_priority_min, "%d"), args->policy);
1562 #endif
1563
1564         switch (args->policy) {
1565         case LINUX_SCHED_OTHER:
1566                 bsd.policy = SCHED_OTHER;
1567                 break;
1568         case LINUX_SCHED_FIFO:
1569                 bsd.policy = SCHED_FIFO;
1570                 break;
1571         case LINUX_SCHED_RR:
1572                 bsd.policy = SCHED_RR;
1573                 break;
1574         default:
1575                 return EINVAL;
1576         }
1577         bsd.sysmsg_result = 0;
1578
1579         error = sys_sched_get_priority_min(&bsd);
1580         args->sysmsg_result = bsd.sysmsg_result;
1581         return(error);
1582 }
1583
1584 #define REBOOT_CAD_ON   0x89abcdef
1585 #define REBOOT_CAD_OFF  0
1586 #define REBOOT_HALT     0xcdef0123
1587 #define REBOOT_RESTART  0x01234567
1588 #define REBOOT_RESTART2 0xA1B2C3D4
1589 #define REBOOT_POWEROFF 0x4321FEDC
1590 #define REBOOT_MAGIC1   0xfee1dead
1591 #define REBOOT_MAGIC2   0x28121969
1592 #define REBOOT_MAGIC2A  0x05121996
1593 #define REBOOT_MAGIC2B  0x16041998
1594
1595 /*
1596  * MPSAFE
1597  */
1598 int
1599 sys_linux_reboot(struct linux_reboot_args *args)
1600 {
1601         struct reboot_args bsd_args;
1602         int error;
1603
1604 #ifdef DEBUG
1605         if (ldebug(reboot))
1606                 kprintf(ARGS(reboot, "0x%x"), args->cmd);
1607 #endif
1608
1609         if ((args->magic1 != REBOOT_MAGIC1) ||
1610             ((args->magic2 != REBOOT_MAGIC2) &&
1611             (args->magic2 != REBOOT_MAGIC2A) &&
1612             (args->magic2 != REBOOT_MAGIC2B)))
1613                 return EINVAL;
1614
1615         switch (args->cmd) {
1616         case REBOOT_CAD_ON:
1617         case REBOOT_CAD_OFF:
1618                 return (priv_check(curthread, PRIV_REBOOT));
1619                 /* NOTREACHED */
1620         case REBOOT_HALT:
1621                 bsd_args.opt = RB_HALT;
1622                 break;
1623         case REBOOT_RESTART:
1624         case REBOOT_RESTART2:
1625                 bsd_args.opt = 0;
1626                 break;
1627         case REBOOT_POWEROFF:
1628                 bsd_args.opt = RB_POWEROFF;
1629                 break;
1630         default:
1631                 return EINVAL;
1632                 /* NOTREACHED */
1633         }
1634
1635         bsd_args.sysmsg_result = 0;
1636
1637         error = sys_reboot(&bsd_args);
1638         args->sysmsg_result = bsd_args.sysmsg_result;
1639         return(error);
1640 }
1641
1642 /*
1643  * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1644  * p->p_retval[1] when COMPAT_43 or COMPAT_SUNOS is defined. This
1645  * globbers registers that are assumed to be preserved. The following
1646  * lightweight syscalls fixes this. See also linux_getgid16() and
1647  * linux_getuid16() in linux_uid16.c.
1648  *
1649  * linux_getpid() - MP SAFE
1650  * linux_getgid() - MP SAFE
1651  * linux_getuid() - MP SAFE
1652  */
1653
1654 /*
1655  * MPALMOSTSAFE
1656  */
1657 int
1658 sys_linux_getpid(struct linux_getpid_args *args)
1659 {
1660         struct linux_emuldata *em;
1661         struct proc *p = curproc;
1662
1663
1664         EMUL_LOCK();
1665         em = emuldata_get(p);
1666         if (em == NULL) /* this should never happen */
1667                 args->sysmsg_result = p->p_pid;
1668         else
1669                 args->sysmsg_result = em->s->group_pid;
1670         EMUL_UNLOCK();
1671
1672         return (0);
1673 }
1674
1675 /*
1676  * MPALMOSTSAFE
1677  */
1678 int
1679 sys_linux_getppid(struct linux_getppid_args *args)
1680 {
1681         struct linux_emuldata *em;
1682         struct proc *parent;
1683         struct proc *p;
1684         pid_t group_pid;
1685
1686         EMUL_LOCK();
1687         em = emuldata_get(curproc);
1688         KKASSERT(em != NULL);
1689         group_pid = em->s->group_pid;
1690         EMUL_UNLOCK();
1691
1692         p = pfind(group_pid);
1693         /* We are not allowed to fail */
1694         if (p == NULL)
1695                 goto out;
1696
1697         parent = p->p_pptr;
1698         if (parent->p_sysent == &elf_linux_sysvec) {
1699                 EMUL_LOCK();
1700                 em = emuldata_get(parent);
1701                 args->sysmsg_result = em->s->group_pid;
1702                 EMUL_UNLOCK();
1703         } else {
1704                 args->sysmsg_result = parent->p_pid;
1705         }
1706
1707 out:
1708         return (0);
1709 }
1710
1711 /*
1712  * MPSAFE
1713  */
1714 int
1715 sys_linux_getgid(struct linux_getgid_args *args)
1716 {
1717         struct thread *td = curthread;
1718
1719         args->sysmsg_result = td->td_ucred->cr_rgid;
1720         return (0);
1721 }
1722
1723 /*
1724  * MPSAFE
1725  */
1726 int
1727 sys_linux_getuid(struct linux_getuid_args *args)
1728 {
1729         struct thread *td = curthread;
1730
1731         args->sysmsg_result = td->td_ucred->cr_ruid;
1732         return (0);
1733 }
1734
1735 /*
1736  * MPSAFE
1737  */
1738 int
1739 sys_linux_getsid(struct linux_getsid_args *args)
1740 {
1741         struct getsid_args bsd;
1742         int error;
1743
1744         bsd.sysmsg_result = 0;
1745         bsd.pid = args->pid;
1746         error = sys_getsid(&bsd);
1747         args->sysmsg_result = bsd.sysmsg_result;
1748         return(error);
1749 }
1750
1751 /*
1752  * MPSAFE
1753  */
1754 int
1755 linux_nosys(struct nosys_args *args)
1756 {
1757         /* XXX */
1758         return (ENOSYS);
1759 }
1760
1761 int
1762 sys_linux_mq_open(struct linux_mq_open_args *args)
1763 {
1764         struct mq_open_args moa;
1765         int error, oflag;
1766
1767         oflag = 0;
1768         if (args->oflag & LINUX_O_RDONLY)
1769                 oflag |= O_RDONLY;
1770         if (args->oflag & LINUX_O_WRONLY)
1771                 oflag |= O_WRONLY;
1772         if (args->oflag & LINUX_O_RDWR)
1773                 oflag |= O_RDWR;
1774
1775         if (args->oflag & LINUX_O_NONBLOCK)
1776                 oflag |= O_NONBLOCK;
1777         if (args->oflag & LINUX_O_CREAT)
1778                 oflag |= O_CREAT;
1779         if (args->oflag & LINUX_O_EXCL)
1780                 oflag |= O_EXCL;
1781
1782         moa.name = args->name;
1783         moa.oflag = oflag;
1784         moa.mode = args->mode;
1785         moa.attr = args->attr;
1786
1787         error = sys_mq_open(&moa);
1788
1789         return (error);
1790 }
1791
1792 int
1793 sys_linux_mq_getsetattr(struct linux_mq_getsetattr_args *args)
1794 {
1795         struct mq_getattr_args gaa;
1796         struct mq_setattr_args saa;
1797         int error;
1798
1799         gaa.mqdes = args->mqd;
1800         gaa.mqstat = args->oattr;
1801
1802         saa.mqdes = args->mqd;
1803         saa.mqstat = args->attr;
1804         saa.mqstat = args->oattr;
1805
1806         if (args->attr != NULL) {
1807                 error = sys_mq_setattr(&saa);
1808         } else {
1809                 error = sys_mq_getattr(&gaa);
1810         }
1811
1812         return error;
1813 }
1814
1815 /*
1816  * Get affinity of a process.
1817  */
1818 int
1819 sys_linux_sched_getaffinity(struct linux_sched_getaffinity_args *args)
1820 {
1821         cpumask_t mask;
1822         struct proc *p;
1823         struct lwp *lp;
1824         int error = 0;
1825
1826 #ifdef DEBUG
1827         if (ldebug(sched_getaffinity))
1828                 kprintf(ARGS(sched_getaffinity, "%d, %d, *"), args->pid,
1829                     args->len);
1830 #endif
1831         if (args->len < sizeof(cpumask_t))
1832                 return (EINVAL);
1833 #if 0
1834         if ((error = priv_check(curthread, PRIV_SCHED_CPUSET)) != 0)
1835                 return (EPERM);
1836 #endif
1837         /* Get the mplock to ensure that the proc is not running */
1838         get_mplock();
1839         if (args->pid == 0) {
1840                 p = curproc;
1841         } else {
1842                 p = pfind(args->pid);
1843                 if (p == NULL) {
1844                         error = ESRCH;
1845                         goto done;
1846                 }
1847         }
1848
1849         lp = FIRST_LWP_IN_PROC(p);
1850         /*
1851          * XXX: if lwp_cpumask is ever changed to support more than
1852          *      32 processors, this needs to be changed to a bcopy.
1853          */
1854         mask = lp->lwp_cpumask;
1855         if ((error = copyout(&mask, args->user_mask_ptr, sizeof(cpumask_t))))
1856                 error = EFAULT;
1857 done:
1858         rel_mplock();
1859 #if 0
1860         if (error == 0)
1861                 args->sysmsg_iresult = sizeof(cpumask_t);
1862 #endif
1863         return (error);
1864 }
1865
1866 /*
1867  *  Set affinity of a process.
1868  */
1869 int
1870 sys_linux_sched_setaffinity(struct linux_sched_setaffinity_args *args)
1871 {
1872 #ifdef DEBUG
1873         if (ldebug(sched_setaffinity))
1874                 kprintf(ARGS(sched_setaffinity, "%d, %d, *"), args->pid,
1875                     args->len);
1876 #endif
1877         /*
1878          * From Linux man page:
1879          * sched_setaffinity() sets the CPU affinity mask of the process
1880          * whose ID is pid to the value specified by mask. If pid is zero, 
1881          * then the calling process is used. The argument cpusetsize is 
1882          * the length (in bytes) of the data pointed to by mask. Normally
1883          * this argument would be specified as sizeof(cpu_set_t).
1884          *
1885          * If the process specified by pid is not currently running on one
1886          * of the CPUs specified in mask, then that process is migrated to
1887          * one of the CPUs specified in mask.
1888          */
1889         /*
1890          * About our implementation: I don't think that it is too important
1891          * to have a working implementation, but if it was ever needed,
1892          * the best approach would be to implement the whole mechanism
1893          * properly in kern_usched.
1894          * The idea has to be to change the affinity mask AND migrate the
1895          * lwp to one of the new valid CPUs for the lwp, in case the current
1896          * CPU isn't anymore in the affinity mask passed in.
1897          * For now, we'll just signal success even if we didn't do anything.
1898          */
1899         return 0;
1900 }
1901
1902 int
1903 sys_linux_gettid(struct linux_gettid_args *args)
1904 {
1905         args->sysmsg_iresult = curproc->p_pid;
1906         return 0;
1907 }
1908
1909 int
1910 sys_linux_getcpu(struct linux_getcpu_args *args)
1911 {
1912         struct globaldata *gd;
1913         l_uint node = 0;
1914         int error;
1915
1916         gd = mycpu;
1917         error = copyout(&gd->gd_cpuid, args->pcpu, sizeof(gd->gd_cpuid));
1918         if (error)
1919                 return (error);
1920         /*
1921          * XXX: this should be the NUMA node, but since we don't implement it,
1922          *      just return 0 for it.
1923          */
1924         error = copyout(&node, args->pnode, sizeof(node));
1925         return (error);
1926 }