Split the struct vmmeter cnt structure into a global vmstats structure and
[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 withough 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.6 2003/07/03 17:23:59 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/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/namei.h>
45 #include <sys/blist.h>
46 #include <sys/reboot.h>
47 #include <sys/resourcevar.h>
48 #include <sys/signalvar.h>
49 #include <sys/stat.h>
50 #include <sys/sysctl.h>
51 #include <sys/sysproto.h>
52 #include <sys/time.h>
53 #include <sys/unistd.h>
54 #include <sys/vmmeter.h>
55 #include <sys/vnode.h>
56 #include <sys/wait.h>
57
58 #include <vm/vm.h>
59 #include <vm/pmap.h>
60 #include <vm/vm_kern.h>
61 #include <vm/vm_map.h>
62 #include <vm/vm_extern.h>
63 #include <vm/vm_object.h>
64 #include <vm/vm_zone.h>
65 #include <vm/swap_pager.h>
66
67 #include <machine/frame.h>
68 #include <machine/limits.h>
69 #include <machine/psl.h>
70 #include <machine/sysarch.h>
71 #ifdef __i386__
72 #include <machine/segments.h>
73 #endif
74
75 #include <posix4/sched.h>
76
77 #include <machine/../linux/linux.h>
78 #include <machine/../linux/linux_proto.h>
79 #include <compat/linux/linux_mib.h>
80 #include <compat/linux/linux_util.h>
81
82 #ifdef __alpha__
83 #define BSD_TO_LINUX_SIGNAL(sig)       (sig)
84 #else
85 #define BSD_TO_LINUX_SIGNAL(sig)        \
86         (((sig) <= LINUX_SIGTBLSZ) ? bsd_to_linux_signal[_SIG_IDX(sig)] : sig)
87 #endif
88
89 #ifndef __alpha__
90 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
91         RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
92         RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
93         RLIMIT_MEMLOCK, -1
94 };
95 #endif /*!__alpha__*/
96
97 struct l_sysinfo {
98         l_long          uptime;         /* Seconds since boot */
99         l_ulong         loads[3];       /* 1, 5, and 15 minute load averages */
100         l_ulong         totalram;       /* Total usable main memory size */
101         l_ulong         freeram;        /* Available memory size */
102         l_ulong         sharedram;      /* Amount of shared memory */
103         l_ulong         bufferram;      /* Memory used by buffers */
104         l_ulong         totalswap;      /* Total swap space size */
105         l_ulong         freeswap;       /* swap space still available */
106         l_ushort        procs;          /* Number of current processes */
107         char            _f[22];         /* Pads structure to 64 bytes */
108 };
109 #ifndef __alpha__
110 int
111 linux_sysinfo(struct linux_sysinfo_args *args)
112 {
113         struct l_sysinfo sysinfo;
114         vm_object_t object;
115         int i;
116         struct timespec ts;
117
118         /* Uptime is copied out of print_uptime() in kern_shutdown.c */
119         getnanouptime(&ts);
120         i = 0;
121         if (ts.tv_sec >= 86400) {
122                 ts.tv_sec %= 86400;
123                 i = 1;
124         }
125         if (i || ts.tv_sec >= 3600) {
126                 ts.tv_sec %= 3600;
127                 i = 1;
128         }
129         if (i || ts.tv_sec >= 60) {
130                 ts.tv_sec %= 60;
131                 i = 1;
132         }
133         sysinfo.uptime=ts.tv_sec;
134
135         /* Use the information from the mib to get our load averages */
136         for (i = 0; i < 3; i++)
137                 sysinfo.loads[i] = averunnable.ldavg[i];
138
139         sysinfo.totalram = physmem * PAGE_SIZE;
140         sysinfo.freeram = sysinfo.totalram - vmstats.v_wire_count * PAGE_SIZE;
141
142         sysinfo.sharedram = 0;
143         for (object = TAILQ_FIRST(&vm_object_list); object != NULL;
144              object = TAILQ_NEXT(object, object_list))
145                 if (object->shadow_count > 1)
146                         sysinfo.sharedram += object->resident_page_count;
147
148         sysinfo.sharedram *= PAGE_SIZE;
149         sysinfo.bufferram = 0;
150
151         if (swapblist == NULL) {
152                 sysinfo.totalswap= 0;
153                 sysinfo.freeswap = 0;
154         } else {
155                 sysinfo.totalswap = swapblist->bl_blocks * 1024;
156                 sysinfo.freeswap = swapblist->bl_root->u.bmu_avail * PAGE_SIZE;
157         }
158
159         sysinfo.procs = 20; /* Hack */
160
161         return copyout(&sysinfo, (caddr_t)args->info, sizeof(sysinfo));
162 }
163 #endif /*!__alpha__*/
164
165 #ifndef __alpha__
166 int
167 linux_alarm(struct linux_alarm_args *args)
168 {
169         struct thread *td = curthread;
170         struct proc *p = td->td_proc;
171         struct itimerval it, old_it;
172         struct timeval tv;
173         int s;
174
175         KKASSERT(p);
176
177 #ifdef DEBUG
178         if (ldebug(alarm))
179                 printf(ARGS(alarm, "%u"), args->secs);
180 #endif
181
182         if (args->secs > 100000000)
183                 return EINVAL;
184
185         it.it_value.tv_sec = (long)args->secs;
186         it.it_value.tv_usec = 0;
187         it.it_interval.tv_sec = 0;
188         it.it_interval.tv_usec = 0;
189         s = splsoftclock();
190         old_it = p->p_realtimer;
191         getmicrouptime(&tv);
192         if (timevalisset(&old_it.it_value))
193                 untimeout(realitexpire, (caddr_t)p, p->p_ithandle);
194         if (it.it_value.tv_sec != 0) {
195                 p->p_ithandle = timeout(realitexpire, (caddr_t)p,
196                     tvtohz(&it.it_value));
197                 timevaladd(&it.it_value, &tv);
198         }
199         p->p_realtimer = it;
200         splx(s);
201         if (timevalcmp(&old_it.it_value, &tv, >)) {
202                 timevalsub(&old_it.it_value, &tv);
203                 if (old_it.it_value.tv_usec != 0)
204                         old_it.it_value.tv_sec++;
205                 p->p_retval[0] = old_it.it_value.tv_sec;
206         }
207         return 0;
208 }
209 #endif /*!__alpha__*/
210
211 int
212 linux_brk(struct linux_brk_args *args)
213 {
214         struct thread *td = curthread;
215         struct proc *p = td->td_proc;
216         struct vmspace *vm;
217         vm_offset_t new, old;
218         struct obreak_args /* {
219                 char * nsize;
220         } */ tmp;
221
222         KKASSERT(p);
223         vm = p->p_vmspace;
224 #ifdef DEBUG
225         if (ldebug(brk))
226                 printf(ARGS(brk, "%p"), (void *)args->dsend);
227 #endif
228         old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
229         new = (vm_offset_t)args->dsend;
230         tmp.nsize = (char *) new;
231         if (((caddr_t)new > vm->vm_daddr) && !obreak(&tmp))
232                 p->p_retval[0] = (long)new;
233         else
234                 p->p_retval[0] = (long)old;
235
236         return 0;
237 }
238
239 int
240 linux_uselib(struct linux_uselib_args *args)
241 {
242         struct thread *td = curthread;
243         struct proc *p;
244         struct nameidata ni;
245         struct vnode *vp;
246         struct exec *a_out;
247         struct vattr attr;
248         vm_offset_t vmaddr;
249         unsigned long file_offset;
250         vm_offset_t buffer;
251         unsigned long bss_size;
252         int error;
253         caddr_t sg;
254         int locked;
255
256         KKASSERT(td->td_proc);
257         p = td->td_proc;
258
259         sg = stackgap_init();
260         CHECKALTEXIST(&sg, args->library);
261
262 #ifdef DEBUG
263         if (ldebug(uselib))
264                 printf(ARGS(uselib, "%s"), args->library);
265 #endif
266
267         a_out = NULL;
268         locked = 0;
269         vp = NULL;
270
271         NDINIT(&ni, LOOKUP, FOLLOW|LOCKLEAF, UIO_USERSPACE, args->library, td);
272         error = namei(&ni);
273         if (error)
274                 goto cleanup;
275
276         vp = ni.ni_vp;
277         /*
278          * XXX - This looks like a bogus check. A LOCKLEAF namei should not
279          * succeed without returning a vnode.
280          */
281         if (vp == NULL) {
282                 error = ENOEXEC;        /* ?? */
283                 goto cleanup;
284         }
285         NDFREE(&ni, NDF_ONLY_PNBUF);
286
287         /*
288          * From here on down, we have a locked vnode that must be unlocked.
289          */
290         locked++;
291
292         /* Writable? */
293         if (vp->v_writecount) {
294                 error = ETXTBSY;
295                 goto cleanup;
296         }
297
298         /* Executable? */
299         error = VOP_GETATTR(vp, &attr, td);
300         if (error)
301                 goto cleanup;
302
303         if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
304             ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
305                 error = ENOEXEC;
306                 goto cleanup;
307         }
308
309         /* Sensible size? */
310         if (attr.va_size == 0) {
311                 error = ENOEXEC;
312                 goto cleanup;
313         }
314
315         /* Can we access it? */
316         error = VOP_ACCESS(vp, VEXEC, p->p_ucred, td);
317         if (error)
318                 goto cleanup;
319
320         error = VOP_OPEN(vp, FREAD, p->p_ucred, td);
321         if (error)
322                 goto cleanup;
323
324         /*
325          * Lock no longer needed
326          */
327         VOP_UNLOCK(vp, 0, td);
328         locked = 0;
329
330         /* Pull in executable header into kernel_map */
331         error = vm_mmap(kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE,
332             VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp, 0);
333         if (error)
334                 goto cleanup;
335
336         /* Is it a Linux binary ? */
337         if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
338                 error = ENOEXEC;
339                 goto cleanup;
340         }
341
342         /*
343          * While we are here, we should REALLY do some more checks
344          */
345
346         /* Set file/virtual offset based on a.out variant. */
347         switch ((int)(a_out->a_magic & 0xffff)) {
348         case 0413:      /* ZMAGIC */
349                 file_offset = 1024;
350                 break;
351         case 0314:      /* QMAGIC */
352                 file_offset = 0;
353                 break;
354         default:
355                 error = ENOEXEC;
356                 goto cleanup;
357         }
358
359         bss_size = round_page(a_out->a_bss);
360
361         /* Check various fields in header for validity/bounds. */
362         if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
363                 error = ENOEXEC;
364                 goto cleanup;
365         }
366
367         /* text + data can't exceed file size */
368         if (a_out->a_data + a_out->a_text > attr.va_size) {
369                 error = EFAULT;
370                 goto cleanup;
371         }
372
373         /*
374          * text/data/bss must not exceed limits
375          * XXX - this is not complete. it should check current usage PLUS
376          * the resources needed by this library.
377          */
378         if (a_out->a_text > maxtsiz ||
379             a_out->a_data + bss_size > p->p_rlimit[RLIMIT_DATA].rlim_cur) {
380                 error = ENOMEM;
381                 goto cleanup;
382         }
383
384         /* prevent more writers */
385         vp->v_flag |= VTEXT;
386
387         /*
388          * Check if file_offset page aligned. Currently we cannot handle
389          * misalinged file offsets, and so we read in the entire image
390          * (what a waste).
391          */
392         if (file_offset & PAGE_MASK) {
393 #ifdef DEBUG
394                 printf("uselib: Non page aligned binary %lu\n", file_offset);
395 #endif
396                 /* Map text+data read/write/execute */
397
398                 /* a_entry is the load address and is page aligned */
399                 vmaddr = trunc_page(a_out->a_entry);
400
401                 /* get anon user mapping, read+write+execute */
402                 error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0,
403                     &vmaddr, a_out->a_text + a_out->a_data, FALSE, VM_PROT_ALL,
404                     VM_PROT_ALL, 0);
405                 if (error)
406                         goto cleanup;
407
408                 /* map file into kernel_map */
409                 error = vm_mmap(kernel_map, &buffer,
410                     round_page(a_out->a_text + a_out->a_data + file_offset),
411                     VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp,
412                     trunc_page(file_offset));
413                 if (error)
414                         goto cleanup;
415
416                 /* copy from kernel VM space to user space */
417                 error = copyout((caddr_t)(uintptr_t)(buffer + file_offset),
418                     (caddr_t)vmaddr, a_out->a_text + a_out->a_data);
419
420                 /* release temporary kernel space */
421                 vm_map_remove(kernel_map, buffer, buffer +
422                     round_page(a_out->a_text + a_out->a_data + file_offset));
423
424                 if (error)
425                         goto cleanup;
426         } else {
427 #ifdef DEBUG
428                 printf("uselib: Page aligned binary %lu\n", file_offset);
429 #endif
430                 /*
431                  * for QMAGIC, a_entry is 20 bytes beyond the load address
432                  * to skip the executable header
433                  */
434                 vmaddr = trunc_page(a_out->a_entry);
435
436                 /*
437                  * Map it all into the process's space as a single
438                  * copy-on-write "data" segment.
439                  */
440                 error = vm_mmap(&p->p_vmspace->vm_map, &vmaddr,
441                     a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
442                     MAP_PRIVATE | MAP_FIXED, (caddr_t)vp, file_offset);
443                 if (error)
444                         goto cleanup;
445         }
446 #ifdef DEBUG
447         printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long*)vmaddr)[0],
448             ((long*)vmaddr)[1]);
449 #endif
450         if (bss_size != 0) {
451                 /* Calculate BSS start address */
452                 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
453                     a_out->a_data;
454
455                 /* allocate some 'anon' space */
456                 error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0,
457                     &vmaddr, bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
458                 if (error)
459                         goto cleanup;
460         }
461
462 cleanup:
463         /* Unlock vnode if needed */
464         if (locked)
465                 VOP_UNLOCK(vp, 0, td);
466
467         /* Release the kernel mapping. */
468         if (a_out)
469                 vm_map_remove(kernel_map, (vm_offset_t)a_out,
470                     (vm_offset_t)a_out + PAGE_SIZE);
471
472         return error;
473 }
474
475 int
476 linux_select(struct linux_select_args *args)
477 {
478         struct thread *td = curthread;
479         struct proc *p = td->td_proc;
480         struct select_args bsa;
481         struct timeval tv0, tv1, utv, *tvp;
482         caddr_t sg;
483         int error;
484
485         KKASSERT(p);
486
487 #ifdef DEBUG
488         if (ldebug(select))
489                 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
490                     (void *)args->readfds, (void *)args->writefds,
491                     (void *)args->exceptfds, (void *)args->timeout);
492 #endif
493
494         error = 0;
495         bsa.nd = args->nfds;
496         bsa.in = args->readfds;
497         bsa.ou = args->writefds;
498         bsa.ex = args->exceptfds;
499         bsa.tv = (struct timeval *)args->timeout;
500
501         /*
502          * Store current time for computation of the amount of
503          * time left.
504          */
505         if (args->timeout) {
506                 if ((error = copyin((caddr_t)args->timeout, &utv,
507                     sizeof(utv))))
508                         goto select_out;
509 #ifdef DEBUG
510                 if (ldebug(select))
511                         printf(LMSG("incoming timeout (%ld/%ld)"),
512                             utv.tv_sec, utv.tv_usec);
513 #endif
514
515                 if (itimerfix(&utv)) {
516                         /*
517                          * The timeval was invalid.  Convert it to something
518                          * valid that will act as it does under Linux.
519                          */
520                         sg = stackgap_init();
521                         tvp = stackgap_alloc(&sg, sizeof(utv));
522                         utv.tv_sec += utv.tv_usec / 1000000;
523                         utv.tv_usec %= 1000000;
524                         if (utv.tv_usec < 0) {
525                                 utv.tv_sec -= 1;
526                                 utv.tv_usec += 1000000;
527                         }
528                         if (utv.tv_sec < 0)
529                                 timevalclear(&utv);
530                         if ((error = copyout(&utv, tvp, sizeof(utv))))
531                                 goto select_out;
532                         bsa.tv = tvp;
533                 }
534                 microtime(&tv0);
535         }
536
537         error = select(&bsa);
538 #ifdef DEBUG
539         if (ldebug(select))
540                 printf(LMSG("real select returns %d"), error);
541 #endif
542         if (error) {
543                 /*
544                  * See fs/select.c in the Linux kernel.  Without this,
545                  * Maelstrom doesn't work.
546                  */
547                 if (error == ERESTART)
548                         error = EINTR;
549                 goto select_out;
550         }
551
552         if (args->timeout) {
553                 if (p->p_retval[0]) {
554                         /*
555                          * Compute how much time was left of the timeout,
556                          * by subtracting the current time and the time
557                          * before we started the call, and subtracting
558                          * that result from the user-supplied value.
559                          */
560                         microtime(&tv1);
561                         timevalsub(&tv1, &tv0);
562                         timevalsub(&utv, &tv1);
563                         if (utv.tv_sec < 0)
564                                 timevalclear(&utv);
565                 } else
566                         timevalclear(&utv);
567 #ifdef DEBUG
568                 if (ldebug(select))
569                         printf(LMSG("outgoing timeout (%ld/%ld)"),
570                             utv.tv_sec, utv.tv_usec);
571 #endif
572                 if ((error = copyout(&utv, (caddr_t)args->timeout,
573                     sizeof(utv))))
574                         goto select_out;
575         }
576
577 select_out:
578 #ifdef DEBUG
579         if (ldebug(select))
580                 printf(LMSG("select_out -> %d"), error);
581 #endif
582         return error;
583 }
584
585 int     
586 linux_mremap(struct linux_mremap_args *args)
587 {
588         struct thread *td = curthread;
589         struct munmap_args /* {
590                 void *addr;
591                 size_t len;
592         } */ bsd_args; 
593         int error = 0;
594         struct proc *p = td->td_proc;
595
596         KKASSERT(p);
597  
598 #ifdef DEBUG
599         if (ldebug(mremap))
600                 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
601                     (void *)args->addr, 
602                     (unsigned long)args->old_len, 
603                     (unsigned long)args->new_len,
604                     (unsigned long)args->flags);
605 #endif
606         args->new_len = round_page(args->new_len);
607         args->old_len = round_page(args->old_len);
608
609         if (args->new_len > args->old_len) {
610                 p->p_retval[0] = 0;
611                 return ENOMEM;
612         }
613
614         if (args->new_len < args->old_len) {
615                 bsd_args.addr = (caddr_t)(args->addr + args->new_len);
616                 bsd_args.len = args->old_len - args->new_len;
617                 error = munmap(&bsd_args);
618         }
619
620         p->p_retval[0] = error ? 0 : (u_long)args->addr;
621         return error;
622 }
623
624 #define LINUX_MS_ASYNC          0x0001
625 #define LINUX_MS_INVALIDATE     0x0002
626 #define LINUX_MS_SYNC           0x0004
627
628 int
629 linux_msync(struct linux_msync_args *args)
630 {
631         struct msync_args bsd_args;
632
633         bsd_args.addr = (caddr_t)args->addr;
634         bsd_args.len = args->len;
635         bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
636
637         return msync(&bsd_args);
638 }
639
640 #ifndef __alpha__
641 int
642 linux_time(struct linux_time_args *args)
643 {
644         struct thread *td = curthread;
645         struct proc *p = td->td_proc;
646         struct timeval tv;
647         l_time_t tm;
648         int error;
649
650         KKASSERT(p);
651
652 #ifdef DEBUG
653         if (ldebug(time))
654                 printf(ARGS(time, "*"));
655 #endif
656
657         microtime(&tv);
658         tm = tv.tv_sec;
659         if (args->tm && (error = copyout(&tm, (caddr_t)args->tm, sizeof(tm))))
660                 return error;
661         p->p_retval[0] = tm;
662         return 0;
663 }
664 #endif  /*!__alpha__*/
665
666 struct l_times_argv {
667         l_long          tms_utime;
668         l_long          tms_stime;
669         l_long          tms_cutime;
670         l_long          tms_cstime;
671 };
672
673 #ifdef __alpha__
674 #define CLK_TCK 1024    /* Linux uses 1024 on alpha */
675 #else
676 #define CLK_TCK 100     /* Linux uses 100 */
677 #endif
678
679 #define CONVTCK(r)      (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
680
681 int
682 linux_times(struct linux_times_args *args)
683 {
684         struct thread *td = curthread;
685         struct proc *p = td->td_proc;
686         struct timeval tv;
687         struct l_times_argv tms;
688         struct rusage ru;
689         int error;
690
691         KKASSERT(p);
692 #ifdef DEBUG
693         if (ldebug(times))
694                 printf(ARGS(times, "*"));
695 #endif
696
697         calcru(p, &ru.ru_utime, &ru.ru_stime, NULL);
698
699         tms.tms_utime = CONVTCK(ru.ru_utime);
700         tms.tms_stime = CONVTCK(ru.ru_stime);
701
702         tms.tms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
703         tms.tms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
704
705         if ((error = copyout(&tms, (caddr_t)args->buf, sizeof(tms))))
706                 return error;
707
708         microuptime(&tv);
709         p->p_retval[0] = (int)CONVTCK(tv);
710         return 0;
711 }
712
713 int
714 linux_newuname(struct linux_newuname_args *args)
715 {
716         struct thread *td = curthread;
717         struct l_new_utsname utsname;
718         char *osrelease, *osname;
719
720 #ifdef DEBUG
721         if (ldebug(newuname))
722                 printf(ARGS(newuname, "*"));
723 #endif
724
725         osname = linux_get_osname(td);
726         osrelease = linux_get_osrelease(td);
727
728         bzero(&utsname, sizeof(utsname));
729         strncpy(utsname.sysname, osname, LINUX_MAX_UTSNAME-1);
730         strncpy(utsname.nodename, hostname, LINUX_MAX_UTSNAME-1);
731         strncpy(utsname.release, osrelease, LINUX_MAX_UTSNAME-1);
732         strncpy(utsname.version, version, LINUX_MAX_UTSNAME-1);
733         strncpy(utsname.machine, machine, LINUX_MAX_UTSNAME-1);
734         strncpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME-1);
735
736         return (copyout(&utsname, (caddr_t)args->buf, sizeof(utsname)));
737 }
738
739 #if defined(__i386__)
740 struct l_utimbuf {
741         l_time_t l_actime;
742         l_time_t l_modtime;
743 };
744
745 int
746 linux_utime(struct linux_utime_args *args)
747 {
748         struct utimes_args /* {
749                 char    *path;
750                 struct  timeval *tptr;
751         } */ bsdutimes;
752         struct timeval tv[2], *tvp;
753         struct l_utimbuf lut;
754         int error;
755         caddr_t sg;
756
757         sg = stackgap_init();
758         CHECKALTEXIST(&sg, args->fname);
759
760 #ifdef DEBUG
761         if (ldebug(utime))
762                 printf(ARGS(utime, "%s, *"), args->fname);
763 #endif
764
765         if (args->times) {
766                 if ((error = copyin((caddr_t)args->times, &lut, sizeof lut)))
767                         return error;
768                 tv[0].tv_sec = lut.l_actime;
769                 tv[0].tv_usec = 0;
770                 tv[1].tv_sec = lut.l_modtime;
771                 tv[1].tv_usec = 0;
772                 /* so that utimes can copyin */
773                 tvp = (struct timeval *)stackgap_alloc(&sg, sizeof(tv));
774                 if (tvp == NULL)
775                         return (ENAMETOOLONG);
776                 if ((error = copyout(tv, tvp, sizeof(tv))))
777                         return error;
778                 bsdutimes.tptr = tvp;
779         } else
780                 bsdutimes.tptr = NULL;
781
782         bsdutimes.path = args->fname;
783         return utimes(&bsdutimes);
784 }
785 #endif /* __i386__ */
786
787 #define __WCLONE 0x80000000
788
789 #ifndef __alpha__
790 int
791 linux_waitpid(struct linux_waitpid_args *args)
792 {
793         struct wait_args /* {
794                 int pid;
795                 int *status;
796                 int options;
797                 struct  rusage *rusage;
798         } */ tmp;
799         int error, tmpstat;
800
801 #ifdef DEBUG
802         if (ldebug(waitpid))
803                 printf(ARGS(waitpid, "%d, %p, %d"),
804                     args->pid, (void *)args->status, args->options);
805 #endif
806
807         tmp.pid = args->pid;
808         tmp.status = args->status;
809         tmp.options = (args->options & (WNOHANG | WUNTRACED));
810         /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
811         if (args->options & __WCLONE)
812                 tmp.options |= WLINUXCLONE;
813         tmp.rusage = NULL;
814
815         if ((error = wait4(&tmp)) != 0)
816                 return error;
817
818         if (args->status) {
819                 if ((error = copyin((caddr_t)args->status, &tmpstat,
820                     sizeof(int))) != 0)
821                         return error;
822                 tmpstat &= 0xffff;
823                 if (WIFSIGNALED(tmpstat))
824                         tmpstat = (tmpstat & 0xffffff80) |
825                             BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
826                 else if (WIFSTOPPED(tmpstat))
827                         tmpstat = (tmpstat & 0xffff00ff) |
828                             (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
829                 return copyout(&tmpstat, (caddr_t)args->status, sizeof(int));
830         }
831
832         return 0;
833 }
834 #endif  /*!__alpha__*/
835
836 int
837 linux_wait4(struct linux_wait4_args *args)
838 {
839         struct thread *td = curthread;
840         struct proc *p = td->td_proc;
841         struct wait_args /* {
842                 int pid;
843                 int *status;
844                 int options;
845                 struct  rusage *rusage;
846         } */ tmp;
847         int error, tmpstat;
848
849         KKASSERT(p);
850
851 #ifdef DEBUG
852         if (ldebug(wait4))
853                 printf(ARGS(wait4, "%d, %p, %d, %p"),
854                     args->pid, (void *)args->status, args->options,
855                     (void *)args->rusage);
856 #endif
857
858         tmp.pid = args->pid;
859         tmp.status = args->status;
860         tmp.options = (args->options & (WNOHANG | WUNTRACED));
861         /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
862         if (args->options & __WCLONE)
863                 tmp.options |= WLINUXCLONE;
864         tmp.rusage = (struct rusage *)args->rusage;
865
866         if ((error = wait4(&tmp)) != 0)
867                 return error;
868
869         SIGDELSET(p->p_siglist, SIGCHLD);
870
871         if (args->status) {
872                 if ((error = copyin((caddr_t)args->status, &tmpstat,
873                     sizeof(int))) != 0)
874                         return error;
875                 tmpstat &= 0xffff;
876                 if (WIFSIGNALED(tmpstat))
877                         tmpstat = (tmpstat & 0xffffff80) |
878                             BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
879                 else if (WIFSTOPPED(tmpstat))
880                         tmpstat = (tmpstat & 0xffff00ff) |
881                             (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
882                 return copyout(&tmpstat, (caddr_t)args->status, sizeof(int));
883         }
884
885         return 0;
886 }
887
888 int
889 linux_mknod(struct linux_mknod_args *args)
890 {
891         caddr_t sg;
892         struct mknod_args bsd_mknod;
893         struct mkfifo_args bsd_mkfifo;
894
895         sg = stackgap_init();
896
897         CHECKALTCREAT(&sg, args->path);
898
899 #ifdef DEBUG
900         if (ldebug(mknod))
901                 printf(ARGS(mknod, "%s, %d, %d"),
902                     args->path, args->mode, args->dev);
903 #endif
904
905         if (args->mode & S_IFIFO) {
906                 bsd_mkfifo.path = args->path;
907                 bsd_mkfifo.mode = args->mode;
908                 return mkfifo(&bsd_mkfifo);
909         } else {
910                 bsd_mknod.path = args->path;
911                 bsd_mknod.mode = args->mode;
912                 bsd_mknod.dev = args->dev;
913                 return mknod(&bsd_mknod);
914         }
915 }
916
917 /*
918  * UGH! This is just about the dumbest idea I've ever heard!!
919  */
920 int
921 linux_personality(struct linux_personality_args *args)
922 {
923         struct thread *td = curthread;
924         struct proc *p = td->td_proc;
925
926         KKASSERT(p);
927 #ifdef DEBUG
928         if (ldebug(personality))
929                 printf(ARGS(personality, "%d"), args->per);
930 #endif
931 #ifndef __alpha__
932         if (args->per != 0)
933                 return EINVAL;
934 #endif
935
936         /* Yes Jim, it's still a Linux... */
937         p->p_retval[0] = 0;
938         return 0;
939 }
940
941 /*
942  * Wrappers for get/setitimer for debugging..
943  */
944 int
945 linux_setitimer(struct linux_setitimer_args *args)
946 {
947         struct setitimer_args bsa;
948         struct itimerval foo;
949         int error;
950
951 #ifdef DEBUG
952         if (ldebug(setitimer))
953                 printf(ARGS(setitimer, "%p, %p"),
954                     (void *)args->itv, (void *)args->oitv);
955 #endif
956         bsa.which = args->which;
957         bsa.itv = (struct itimerval *)args->itv;
958         bsa.oitv = (struct itimerval *)args->oitv;
959         if (args->itv) {
960             if ((error = copyin((caddr_t)args->itv, &foo, sizeof(foo))))
961                 return error;
962 #ifdef DEBUG
963             if (ldebug(setitimer)) {
964                 printf("setitimer: value: sec: %ld, usec: %ld\n",
965                     foo.it_value.tv_sec, foo.it_value.tv_usec);
966                 printf("setitimer: interval: sec: %ld, usec: %ld\n",
967                     foo.it_interval.tv_sec, foo.it_interval.tv_usec);
968             }
969 #endif
970         }
971         return setitimer(&bsa);
972 }
973
974 int
975 linux_getitimer(struct linux_getitimer_args *args)
976 {
977         struct getitimer_args bsa;
978 #ifdef DEBUG
979         if (ldebug(getitimer))
980                 printf(ARGS(getitimer, "%p"), (void *)args->itv);
981 #endif
982         bsa.which = args->which;
983         bsa.itv = (struct itimerval *)args->itv;
984         return getitimer(&bsa);
985 }
986
987 #ifndef __alpha__
988 int
989 linux_nice(struct linux_nice_args *args)
990 {
991         struct setpriority_args bsd_args;
992
993         bsd_args.which = PRIO_PROCESS;
994         bsd_args.who = 0;       /* current process */
995         bsd_args.prio = args->inc;
996         return setpriority(&bsd_args);
997 }
998 #endif  /*!__alpha__*/
999
1000 int
1001 linux_setgroups(struct linux_setgroups_args *args)
1002 {
1003         struct thread *td = curthread;
1004         struct proc *p = td->td_proc;
1005         struct ucred *newcred, *oldcred;
1006         l_gid_t linux_gidset[NGROUPS];
1007         gid_t *bsd_gidset;
1008         int ngrp, error;
1009
1010         KKASSERT(p);
1011
1012         ngrp = args->gidsetsize;
1013         oldcred = p->p_ucred;
1014
1015         /*
1016          * cr_groups[0] holds egid. Setting the whole set from
1017          * the supplied set will cause egid to be changed too.
1018          * Keep cr_groups[0] unchanged to prevent that.
1019          */
1020
1021         if ((error = suser_cred(oldcred, PRISON_ROOT)) != 0)
1022                 return (error);
1023
1024         if (ngrp >= NGROUPS)
1025                 return (EINVAL);
1026
1027         newcred = crdup(oldcred);
1028         if (ngrp > 0) {
1029                 error = copyin((caddr_t)args->grouplist, linux_gidset,
1030                                ngrp * sizeof(l_gid_t));
1031                 if (error)
1032                         return (error);
1033
1034                 newcred->cr_ngroups = ngrp + 1;
1035
1036                 bsd_gidset = newcred->cr_groups;
1037                 ngrp--;
1038                 while (ngrp >= 0) {
1039                         bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1040                         ngrp--;
1041                 }
1042         } else {
1043                 newcred->cr_ngroups = 1;
1044         }
1045
1046         setsugid();
1047         p->p_ucred = newcred;
1048         crfree(oldcred);
1049         return (0);
1050 }
1051
1052 int
1053 linux_getgroups(struct linux_getgroups_args *args)
1054 {
1055         struct thread *td = curthread;
1056         struct proc *p = td->td_proc;
1057         struct ucred *cred;
1058         l_gid_t linux_gidset[NGROUPS];
1059         gid_t *bsd_gidset;
1060         int bsd_gidsetsz, ngrp, error;
1061
1062         KKASSERT(p);
1063
1064         cred = p->p_ucred;
1065         bsd_gidset = cred->cr_groups;
1066         bsd_gidsetsz = cred->cr_ngroups - 1;
1067
1068         /*
1069          * cr_groups[0] holds egid. Returning the whole set
1070          * here will cause a duplicate. Exclude cr_groups[0]
1071          * to prevent that.
1072          */
1073
1074         if ((ngrp = args->gidsetsize) == 0) {
1075                 p->p_retval[0] = bsd_gidsetsz;
1076                 return (0);
1077         }
1078
1079         if (ngrp < bsd_gidsetsz)
1080                 return (EINVAL);
1081
1082         ngrp = 0;
1083         while (ngrp < bsd_gidsetsz) {
1084                 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1085                 ngrp++;
1086         }
1087
1088         if ((error = copyout(linux_gidset, (caddr_t)args->grouplist,
1089             ngrp * sizeof(l_gid_t))))
1090                 return (error);
1091
1092         p->p_retval[0] = ngrp;
1093         return (0);
1094 }
1095
1096 #ifndef __alpha__
1097 int
1098 linux_setrlimit(struct linux_setrlimit_args *args)
1099 {
1100         struct __setrlimit_args bsd;
1101         struct l_rlimit rlim;
1102         int error;
1103         caddr_t sg = stackgap_init();
1104
1105 #ifdef DEBUG
1106         if (ldebug(setrlimit))
1107                 printf(ARGS(setrlimit, "%d, %p"),
1108                     args->resource, (void *)args->rlim);
1109 #endif
1110
1111         if (args->resource >= LINUX_RLIM_NLIMITS)
1112                 return (EINVAL);
1113
1114         bsd.which = linux_to_bsd_resource[args->resource];
1115         if (bsd.which == -1)
1116                 return (EINVAL);
1117
1118         error = copyin((caddr_t)args->rlim, &rlim, sizeof(rlim));
1119         if (error)
1120                 return (error);
1121
1122         bsd.rlp = stackgap_alloc(&sg, sizeof(struct rlimit));
1123         bsd.rlp->rlim_cur = (rlim_t)rlim.rlim_cur;
1124         bsd.rlp->rlim_max = (rlim_t)rlim.rlim_max;
1125         return (setrlimit(&bsd));
1126 }
1127
1128 int
1129 linux_old_getrlimit(struct linux_old_getrlimit_args *args)
1130 {
1131         struct __getrlimit_args bsd;
1132         struct l_rlimit rlim;
1133         int error;
1134         caddr_t sg = stackgap_init();
1135
1136 #ifdef DEBUG
1137         if (ldebug(old_getrlimit))
1138                 printf(ARGS(old_getrlimit, "%d, %p"),
1139                     args->resource, (void *)args->rlim);
1140 #endif
1141
1142         if (args->resource >= LINUX_RLIM_NLIMITS)
1143                 return (EINVAL);
1144
1145         bsd.which = linux_to_bsd_resource[args->resource];
1146         if (bsd.which == -1)
1147                 return (EINVAL);
1148
1149         bsd.rlp = stackgap_alloc(&sg, sizeof(struct rlimit));
1150         error = getrlimit(&bsd);
1151         if (error)
1152                 return (error);
1153
1154         rlim.rlim_cur = (unsigned long)bsd.rlp->rlim_cur;
1155         if (rlim.rlim_cur == ULONG_MAX)
1156                 rlim.rlim_cur = LONG_MAX;
1157         rlim.rlim_max = (unsigned long)bsd.rlp->rlim_max;
1158         if (rlim.rlim_max == ULONG_MAX)
1159                 rlim.rlim_max = LONG_MAX;
1160         return (copyout(&rlim, (caddr_t)args->rlim, sizeof(rlim)));
1161 }
1162
1163 int
1164 linux_getrlimit(struct linux_getrlimit_args *args)
1165 {
1166         struct __getrlimit_args bsd;
1167         struct l_rlimit rlim;
1168         int error;
1169         caddr_t sg = stackgap_init();
1170
1171 #ifdef DEBUG
1172         if (ldebug(getrlimit))
1173                 printf(ARGS(getrlimit, "%d, %p"),
1174                     args->resource, (void *)args->rlim);
1175 #endif
1176
1177         if (args->resource >= LINUX_RLIM_NLIMITS)
1178                 return (EINVAL);
1179
1180         bsd.which = linux_to_bsd_resource[args->resource];
1181         if (bsd.which == -1)
1182                 return (EINVAL);
1183
1184         bsd.rlp = stackgap_alloc(&sg, sizeof(struct rlimit));
1185         error = getrlimit(&bsd);
1186         if (error)
1187                 return (error);
1188
1189         rlim.rlim_cur = (l_ulong)bsd.rlp->rlim_cur;
1190         rlim.rlim_max = (l_ulong)bsd.rlp->rlim_max;
1191         return (copyout(&rlim, (caddr_t)args->rlim, sizeof(rlim)));
1192 }
1193 #endif /*!__alpha__*/
1194
1195 int
1196 linux_sched_setscheduler(struct linux_sched_setscheduler_args *args)
1197 {
1198         struct sched_setscheduler_args bsd;
1199
1200 #ifdef DEBUG
1201         if (ldebug(sched_setscheduler))
1202                 printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1203                     args->pid, args->policy, (const void *)args->param);
1204 #endif
1205
1206         switch (args->policy) {
1207         case LINUX_SCHED_OTHER:
1208                 bsd.policy = SCHED_OTHER;
1209                 break;
1210         case LINUX_SCHED_FIFO:
1211                 bsd.policy = SCHED_FIFO;
1212                 break;
1213         case LINUX_SCHED_RR:
1214                 bsd.policy = SCHED_RR;
1215                 break;
1216         default:
1217                 return EINVAL;
1218         }
1219
1220         bsd.pid = args->pid;
1221         bsd.param = (struct sched_param *)args->param;
1222         return sched_setscheduler(&bsd);
1223 }
1224
1225 int
1226 linux_sched_getscheduler(struct linux_sched_getscheduler_args *args)
1227 {
1228         struct thread *td = curthread;
1229         struct proc *p = td->td_proc;
1230         struct sched_getscheduler_args bsd;
1231         int error;
1232
1233         KKASSERT(p);
1234
1235 #ifdef DEBUG
1236         if (ldebug(sched_getscheduler))
1237                 printf(ARGS(sched_getscheduler, "%d"), args->pid);
1238 #endif
1239
1240         bsd.pid = args->pid;
1241         error = sched_getscheduler(&bsd);
1242
1243         switch (p->p_retval[0]) {
1244         case SCHED_OTHER:
1245                 p->p_retval[0] = LINUX_SCHED_OTHER;
1246                 break;
1247         case SCHED_FIFO:
1248                 p->p_retval[0] = LINUX_SCHED_FIFO;
1249                 break;
1250         case SCHED_RR:
1251                 p->p_retval[0] = LINUX_SCHED_RR;
1252                 break;
1253         }
1254
1255         return error;
1256 }
1257
1258 int
1259 linux_sched_get_priority_max(struct linux_sched_get_priority_max_args *args)
1260 {
1261         struct sched_get_priority_max_args bsd;
1262
1263 #ifdef DEBUG
1264         if (ldebug(sched_get_priority_max))
1265                 printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1266 #endif
1267
1268         switch (args->policy) {
1269         case LINUX_SCHED_OTHER:
1270                 bsd.policy = SCHED_OTHER;
1271                 break;
1272         case LINUX_SCHED_FIFO:
1273                 bsd.policy = SCHED_FIFO;
1274                 break;
1275         case LINUX_SCHED_RR:
1276                 bsd.policy = SCHED_RR;
1277                 break;
1278         default:
1279                 return EINVAL;
1280         }
1281         return sched_get_priority_max(&bsd);
1282 }
1283
1284 int
1285 linux_sched_get_priority_min(struct linux_sched_get_priority_min_args *args)
1286 {
1287         struct sched_get_priority_min_args bsd;
1288
1289 #ifdef DEBUG
1290         if (ldebug(sched_get_priority_min))
1291                 printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1292 #endif
1293
1294         switch (args->policy) {
1295         case LINUX_SCHED_OTHER:
1296                 bsd.policy = SCHED_OTHER;
1297                 break;
1298         case LINUX_SCHED_FIFO:
1299                 bsd.policy = SCHED_FIFO;
1300                 break;
1301         case LINUX_SCHED_RR:
1302                 bsd.policy = SCHED_RR;
1303                 break;
1304         default:
1305                 return EINVAL;
1306         }
1307         return sched_get_priority_min(&bsd);
1308 }
1309
1310 #define REBOOT_CAD_ON   0x89abcdef
1311 #define REBOOT_CAD_OFF  0
1312 #define REBOOT_HALT     0xcdef0123
1313
1314 int
1315 linux_reboot(struct linux_reboot_args *args)
1316 {
1317         struct reboot_args bsd_args;
1318
1319 #ifdef DEBUG
1320         if (ldebug(reboot))
1321                 printf(ARGS(reboot, "0x%x"), args->cmd);
1322 #endif
1323         if (args->cmd == REBOOT_CAD_ON || args->cmd == REBOOT_CAD_OFF)
1324                 return (0);
1325         bsd_args.opt = (args->cmd == REBOOT_HALT) ? RB_HALT : 0;
1326         return (reboot(&bsd_args));
1327 }
1328
1329 #ifndef __alpha__
1330
1331 /*
1332  * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1333  * p->p_retval[1] when COMPAT_43 or COMPAT_SUNOS is defined. This
1334  * globbers registers that are assumed to be preserved. The following
1335  * lightweight syscalls fixes this. See also linux_getgid16() and
1336  * linux_getuid16() in linux_uid16.c.
1337  *
1338  * linux_getpid() - MP SAFE
1339  * linux_getgid() - MP SAFE
1340  * linux_getuid() - MP SAFE
1341  */
1342
1343 int
1344 linux_getpid(struct linux_getpid_args *args)
1345 {
1346         struct thread *td = curthread;
1347         struct proc *p = td->td_proc;
1348
1349         KKASSERT(p);
1350
1351         p->p_retval[0] = p->p_pid;
1352         return (0);
1353 }
1354
1355 int
1356 linux_getgid(struct linux_getgid_args *args)
1357 {
1358         struct thread *td = curthread;
1359         struct proc *p = td->td_proc;
1360
1361         KKASSERT(p);
1362
1363         p->p_retval[0] = p->p_ucred->cr_rgid;
1364         return (0);
1365 }
1366
1367 int
1368 linux_getuid(struct linux_getuid_args *args)
1369 {
1370         struct thread *td = curthread;
1371         struct proc *p = td->td_proc;
1372
1373         KKASSERT(p);
1374
1375         p->p_retval[0] = p->p_ucred->cr_ruid;
1376         return (0);
1377 }
1378
1379 #endif /*!__alpha__*/
1380
1381 int
1382 linux_getsid(struct linux_getsid_args *args)
1383 {
1384         struct getsid_args bsd;
1385         bsd.pid = args->pid;
1386         return getsid(&bsd);
1387 }