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