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