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