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