]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/compat/linux/linux_misc.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / compat / linux / linux_misc.c
1 /*-
2  * Copyright (c) 2002 Doug Rabson
3  * Copyright (c) 1994-1995 Søren Schmidt
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer
11  *    in this position and unchanged.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include "opt_compat.h"
34 #include "opt_kdtrace.h"
35
36 #include <sys/param.h>
37 #include <sys/blist.h>
38 #include <sys/fcntl.h>
39 #if defined(__i386__)
40 #include <sys/imgact_aout.h>
41 #endif
42 #include <sys/jail.h>
43 #include <sys/kernel.h>
44 #include <sys/limits.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/mman.h>
48 #include <sys/mount.h>
49 #include <sys/mutex.h>
50 #include <sys/namei.h>
51 #include <sys/priv.h>
52 #include <sys/proc.h>
53 #include <sys/reboot.h>
54 #include <sys/racct.h>
55 #include <sys/resourcevar.h>
56 #include <sys/sched.h>
57 #include <sys/sdt.h>
58 #include <sys/signalvar.h>
59 #include <sys/stat.h>
60 #include <sys/syscallsubr.h>
61 #include <sys/sysctl.h>
62 #include <sys/sysproto.h>
63 #include <sys/systm.h>
64 #include <sys/time.h>
65 #include <sys/vmmeter.h>
66 #include <sys/vnode.h>
67 #include <sys/wait.h>
68 #include <sys/cpuset.h>
69
70 #include <security/mac/mac_framework.h>
71
72 #include <vm/vm.h>
73 #include <vm/pmap.h>
74 #include <vm/vm_kern.h>
75 #include <vm/vm_map.h>
76 #include <vm/vm_extern.h>
77 #include <vm/vm_object.h>
78 #include <vm/swap_pager.h>
79
80 #ifdef COMPAT_LINUX32
81 #include <machine/../linux32/linux.h>
82 #include <machine/../linux32/linux32_proto.h>
83 #else
84 #include <machine/../linux/linux.h>
85 #include <machine/../linux/linux_proto.h>
86 #endif
87
88 #include <compat/linux/linux_dtrace.h>
89 #include <compat/linux/linux_file.h>
90 #include <compat/linux/linux_mib.h>
91 #include <compat/linux/linux_signal.h>
92 #include <compat/linux/linux_util.h>
93 #include <compat/linux/linux_sysproto.h>
94 #include <compat/linux/linux_emul.h>
95 #include <compat/linux/linux_misc.h>
96
97 /* DTrace init */
98 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE);
99
100 /* Linuxulator-global DTrace probes */
101 LIN_SDT_PROBE_DECLARE(locks, emul_lock, locked);
102 LIN_SDT_PROBE_DECLARE(locks, emul_lock, unlock);
103 LIN_SDT_PROBE_DECLARE(locks, emul_shared_rlock, locked);
104 LIN_SDT_PROBE_DECLARE(locks, emul_shared_rlock, unlock);
105 LIN_SDT_PROBE_DECLARE(locks, emul_shared_wlock, locked);
106 LIN_SDT_PROBE_DECLARE(locks, emul_shared_wlock, unlock);
107
108 int stclohz;                            /* Statistics clock frequency */
109
110 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
111         RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
112         RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
113         RLIMIT_MEMLOCK, RLIMIT_AS 
114 };
115
116 struct l_sysinfo {
117         l_long          uptime;         /* Seconds since boot */
118         l_ulong         loads[3];       /* 1, 5, and 15 minute load averages */
119 #define LINUX_SYSINFO_LOADS_SCALE 65536
120         l_ulong         totalram;       /* Total usable main memory size */
121         l_ulong         freeram;        /* Available memory size */
122         l_ulong         sharedram;      /* Amount of shared memory */
123         l_ulong         bufferram;      /* Memory used by buffers */
124         l_ulong         totalswap;      /* Total swap space size */
125         l_ulong         freeswap;       /* swap space still available */
126         l_ushort        procs;          /* Number of current processes */
127         l_ushort        pads;
128         l_ulong         totalbig;
129         l_ulong         freebig;
130         l_uint          mem_unit;
131         char            _f[20-2*sizeof(l_long)-sizeof(l_int)];  /* padding */
132 };
133 int
134 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args)
135 {
136         struct l_sysinfo sysinfo;
137         vm_object_t object;
138         int i, j;
139         struct timespec ts;
140
141         getnanouptime(&ts);
142         if (ts.tv_nsec != 0)
143                 ts.tv_sec++;
144         sysinfo.uptime = ts.tv_sec;
145
146         /* Use the information from the mib to get our load averages */
147         for (i = 0; i < 3; i++)
148                 sysinfo.loads[i] = averunnable.ldavg[i] *
149                     LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale;
150
151         sysinfo.totalram = physmem * PAGE_SIZE;
152         sysinfo.freeram = sysinfo.totalram - cnt.v_wire_count * PAGE_SIZE;
153
154         sysinfo.sharedram = 0;
155         mtx_lock(&vm_object_list_mtx);
156         TAILQ_FOREACH(object, &vm_object_list, object_list)
157                 if (object->shadow_count > 1)
158                         sysinfo.sharedram += object->resident_page_count;
159         mtx_unlock(&vm_object_list_mtx);
160
161         sysinfo.sharedram *= PAGE_SIZE;
162         sysinfo.bufferram = 0;
163
164         swap_pager_status(&i, &j);
165         sysinfo.totalswap = i * PAGE_SIZE;
166         sysinfo.freeswap = (i - j) * PAGE_SIZE;
167
168         sysinfo.procs = nprocs;
169
170         /* The following are only present in newer Linux kernels. */
171         sysinfo.totalbig = 0;
172         sysinfo.freebig = 0;
173         sysinfo.mem_unit = 1;
174
175         return (copyout(&sysinfo, args->info, sizeof(sysinfo)));
176 }
177
178 int
179 linux_alarm(struct thread *td, struct linux_alarm_args *args)
180 {
181         struct itimerval it, old_it;
182         u_int secs;
183         int error;
184
185 #ifdef DEBUG
186         if (ldebug(alarm))
187                 printf(ARGS(alarm, "%u"), args->secs);
188 #endif
189         
190         secs = args->secs;
191
192         if (secs > INT_MAX)
193                 secs = INT_MAX;
194
195         it.it_value.tv_sec = (long) secs;
196         it.it_value.tv_usec = 0;
197         it.it_interval.tv_sec = 0;
198         it.it_interval.tv_usec = 0;
199         error = kern_setitimer(td, ITIMER_REAL, &it, &old_it);
200         if (error)
201                 return (error);
202         if (timevalisset(&old_it.it_value)) {
203                 if (old_it.it_value.tv_usec != 0)
204                         old_it.it_value.tv_sec++;
205                 td->td_retval[0] = old_it.it_value.tv_sec;
206         }
207         return (0);
208 }
209
210 int
211 linux_brk(struct thread *td, struct linux_brk_args *args)
212 {
213         struct vmspace *vm = td->td_proc->p_vmspace;
214         vm_offset_t new, old;
215         struct obreak_args /* {
216                 char * nsize;
217         } */ tmp;
218
219 #ifdef DEBUG
220         if (ldebug(brk))
221                 printf(ARGS(brk, "%p"), (void *)(uintptr_t)args->dsend);
222 #endif
223         old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
224         new = (vm_offset_t)args->dsend;
225         tmp.nsize = (char *)new;
226         if (((caddr_t)new > vm->vm_daddr) && !sys_obreak(td, &tmp))
227                 td->td_retval[0] = (long)new;
228         else
229                 td->td_retval[0] = (long)old;
230
231         return (0);
232 }
233
234 #if defined(__i386__)
235 /* XXX: what about amd64/linux32? */
236
237 int
238 linux_uselib(struct thread *td, struct linux_uselib_args *args)
239 {
240         struct nameidata ni;
241         struct vnode *vp;
242         struct exec *a_out;
243         struct vattr attr;
244         vm_offset_t vmaddr;
245         unsigned long file_offset;
246         unsigned long bss_size;
247         char *library;
248         ssize_t aresid;
249         int error, locked, vfslocked, writecount;
250
251         LCONVPATHEXIST(td, args->library, &library);
252
253 #ifdef DEBUG
254         if (ldebug(uselib))
255                 printf(ARGS(uselib, "%s"), library);
256 #endif
257
258         a_out = NULL;
259         vfslocked = 0;
260         locked = 0;
261         vp = NULL;
262
263         NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
264             UIO_SYSSPACE, library, td);
265         error = namei(&ni);
266         LFREEPATH(library);
267         if (error)
268                 goto cleanup;
269
270         vp = ni.ni_vp;
271         vfslocked = NDHASGIANT(&ni);
272         NDFREE(&ni, NDF_ONLY_PNBUF);
273
274         /*
275          * From here on down, we have a locked vnode that must be unlocked.
276          * XXX: The code below largely duplicates exec_check_permissions().
277          */
278         locked = 1;
279
280         /* Writable? */
281         error = VOP_GET_WRITECOUNT(vp, &writecount);
282         if (error != 0)
283                 goto cleanup;
284         if (writecount != 0) {
285                 error = ETXTBSY;
286                 goto cleanup;
287         }
288
289         /* Executable? */
290         error = VOP_GETATTR(vp, &attr, td->td_ucred);
291         if (error)
292                 goto cleanup;
293
294         if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
295             ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
296                 /* EACCESS is what exec(2) returns. */
297                 error = ENOEXEC;
298                 goto cleanup;
299         }
300
301         /* Sensible size? */
302         if (attr.va_size == 0) {
303                 error = ENOEXEC;
304                 goto cleanup;
305         }
306
307         /* Can we access it? */
308         error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
309         if (error)
310                 goto cleanup;
311
312         /*
313          * XXX: This should use vn_open() so that it is properly authorized,
314          * and to reduce code redundancy all over the place here.
315          * XXX: Not really, it duplicates far more of exec_check_permissions()
316          * than vn_open().
317          */
318 #ifdef MAC
319         error = mac_vnode_check_open(td->td_ucred, vp, VREAD);
320         if (error)
321                 goto cleanup;
322 #endif
323         error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL);
324         if (error)
325                 goto cleanup;
326
327         /* Pull in executable header into exec_map */
328         error = vm_mmap(exec_map, (vm_offset_t *)&a_out, PAGE_SIZE,
329             VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 0);
330         if (error)
331                 goto cleanup;
332
333         /* Is it a Linux binary ? */
334         if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
335                 error = ENOEXEC;
336                 goto cleanup;
337         }
338
339         /*
340          * While we are here, we should REALLY do some more checks
341          */
342
343         /* Set file/virtual offset based on a.out variant. */
344         switch ((int)(a_out->a_magic & 0xffff)) {
345         case 0413:                      /* ZMAGIC */
346                 file_offset = 1024;
347                 break;
348         case 0314:                      /* QMAGIC */
349                 file_offset = 0;
350                 break;
351         default:
352                 error = ENOEXEC;
353                 goto cleanup;
354         }
355
356         bss_size = round_page(a_out->a_bss);
357
358         /* Check various fields in header for validity/bounds. */
359         if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
360                 error = ENOEXEC;
361                 goto cleanup;
362         }
363
364         /* text + data can't exceed file size */
365         if (a_out->a_data + a_out->a_text > attr.va_size) {
366                 error = EFAULT;
367                 goto cleanup;
368         }
369
370         /*
371          * text/data/bss must not exceed limits
372          * XXX - this is not complete. it should check current usage PLUS
373          * the resources needed by this library.
374          */
375         PROC_LOCK(td->td_proc);
376         if (a_out->a_text > maxtsiz ||
377             a_out->a_data + bss_size > lim_cur(td->td_proc, RLIMIT_DATA) ||
378             racct_set(td->td_proc, RACCT_DATA, a_out->a_data +
379             bss_size) != 0) {
380                 PROC_UNLOCK(td->td_proc);
381                 error = ENOMEM;
382                 goto cleanup;
383         }
384         PROC_UNLOCK(td->td_proc);
385
386         /*
387          * Prevent more writers.
388          * XXX: Note that if any of the VM operations fail below we don't
389          * clear this flag.
390          */
391         VOP_SET_TEXT(vp);
392
393         /*
394          * Lock no longer needed
395          */
396         locked = 0;
397         VOP_UNLOCK(vp, 0);
398         VFS_UNLOCK_GIANT(vfslocked);
399
400         /*
401          * Check if file_offset page aligned. Currently we cannot handle
402          * misalinged file offsets, and so we read in the entire image
403          * (what a waste).
404          */
405         if (file_offset & PAGE_MASK) {
406 #ifdef DEBUG
407                 printf("uselib: Non page aligned binary %lu\n", file_offset);
408 #endif
409                 /* Map text+data read/write/execute */
410
411                 /* a_entry is the load address and is page aligned */
412                 vmaddr = trunc_page(a_out->a_entry);
413
414                 /* get anon user mapping, read+write+execute */
415                 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
416                     &vmaddr, a_out->a_text + a_out->a_data, FALSE, VM_PROT_ALL,
417                     VM_PROT_ALL, 0);
418                 if (error)
419                         goto cleanup;
420
421                 error = vn_rdwr(UIO_READ, vp, (void *)vmaddr, file_offset,
422                     a_out->a_text + a_out->a_data, UIO_USERSPACE, 0,
423                     td->td_ucred, NOCRED, &aresid, td);
424                 if (error != 0)
425                         goto cleanup;
426                 if (aresid != 0) {
427                         error = ENOEXEC;
428                         goto cleanup;
429                 }
430         } else {
431 #ifdef DEBUG
432                 printf("uselib: Page aligned binary %lu\n", file_offset);
433 #endif
434                 /*
435                  * for QMAGIC, a_entry is 20 bytes beyond the load address
436                  * to skip the executable header
437                  */
438                 vmaddr = trunc_page(a_out->a_entry);
439
440                 /*
441                  * Map it all into the process's space as a single
442                  * copy-on-write "data" segment.
443                  */
444                 error = vm_mmap(&td->td_proc->p_vmspace->vm_map, &vmaddr,
445                     a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
446                     MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset);
447                 if (error)
448                         goto cleanup;
449         }
450 #ifdef DEBUG
451         printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long *)vmaddr)[0],
452             ((long *)vmaddr)[1]);
453 #endif
454         if (bss_size != 0) {
455                 /* Calculate BSS start address */
456                 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
457                     a_out->a_data;
458
459                 /* allocate some 'anon' space */
460                 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
461                     &vmaddr, bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
462                 if (error)
463                         goto cleanup;
464         }
465
466 cleanup:
467         /* Unlock vnode if needed */
468         if (locked) {
469                 VOP_UNLOCK(vp, 0);
470                 VFS_UNLOCK_GIANT(vfslocked);
471         }
472
473         /* Release the temporary mapping. */
474         if (a_out)
475                 kmem_free_wakeup(exec_map, (vm_offset_t)a_out, PAGE_SIZE);
476
477         return (error);
478 }
479
480 #endif  /* __i386__ */
481
482 int
483 linux_select(struct thread *td, struct linux_select_args *args)
484 {
485         l_timeval ltv;
486         struct timeval tv0, tv1, utv, *tvp;
487         int error;
488
489 #ifdef DEBUG
490         if (ldebug(select))
491                 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
492                     (void *)args->readfds, (void *)args->writefds,
493                     (void *)args->exceptfds, (void *)args->timeout);
494 #endif
495
496         /*
497          * Store current time for computation of the amount of
498          * time left.
499          */
500         if (args->timeout) {
501                 if ((error = copyin(args->timeout, &ltv, sizeof(ltv))))
502                         goto select_out;
503                 utv.tv_sec = ltv.tv_sec;
504                 utv.tv_usec = ltv.tv_usec;
505 #ifdef DEBUG
506                 if (ldebug(select))
507                         printf(LMSG("incoming timeout (%jd/%ld)"),
508                             (intmax_t)utv.tv_sec, utv.tv_usec);
509 #endif
510
511                 if (itimerfix(&utv)) {
512                         /*
513                          * The timeval was invalid.  Convert it to something
514                          * valid that will act as it does under Linux.
515                          */
516                         utv.tv_sec += utv.tv_usec / 1000000;
517                         utv.tv_usec %= 1000000;
518                         if (utv.tv_usec < 0) {
519                                 utv.tv_sec -= 1;
520                                 utv.tv_usec += 1000000;
521                         }
522                         if (utv.tv_sec < 0)
523                                 timevalclear(&utv);
524                 }
525                 microtime(&tv0);
526                 tvp = &utv;
527         } else
528                 tvp = NULL;
529
530         error = kern_select(td, args->nfds, args->readfds, args->writefds,
531             args->exceptfds, tvp, sizeof(l_int) * 8);
532
533 #ifdef DEBUG
534         if (ldebug(select))
535                 printf(LMSG("real select returns %d"), error);
536 #endif
537         if (error)
538                 goto select_out;
539
540         if (args->timeout) {
541                 if (td->td_retval[0]) {
542                         /*
543                          * Compute how much time was left of the timeout,
544                          * by subtracting the current time and the time
545                          * before we started the call, and subtracting
546                          * that result from the user-supplied value.
547                          */
548                         microtime(&tv1);
549                         timevalsub(&tv1, &tv0);
550                         timevalsub(&utv, &tv1);
551                         if (utv.tv_sec < 0)
552                                 timevalclear(&utv);
553                 } else
554                         timevalclear(&utv);
555 #ifdef DEBUG
556                 if (ldebug(select))
557                         printf(LMSG("outgoing timeout (%jd/%ld)"),
558                             (intmax_t)utv.tv_sec, utv.tv_usec);
559 #endif
560                 ltv.tv_sec = utv.tv_sec;
561                 ltv.tv_usec = utv.tv_usec;
562                 if ((error = copyout(&ltv, args->timeout, sizeof(ltv))))
563                         goto select_out;
564         }
565
566 select_out:
567 #ifdef DEBUG
568         if (ldebug(select))
569                 printf(LMSG("select_out -> %d"), error);
570 #endif
571         return (error);
572 }
573
574 int
575 linux_mremap(struct thread *td, struct linux_mremap_args *args)
576 {
577         struct munmap_args /* {
578                 void *addr;
579                 size_t len;
580         } */ bsd_args;
581         int error = 0;
582
583 #ifdef DEBUG
584         if (ldebug(mremap))
585                 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
586                     (void *)(uintptr_t)args->addr,
587                     (unsigned long)args->old_len,
588                     (unsigned long)args->new_len,
589                     (unsigned long)args->flags);
590 #endif
591
592         if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) {
593                 td->td_retval[0] = 0;
594                 return (EINVAL);
595         }
596
597         /*
598          * Check for the page alignment.
599          * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK.
600          */
601         if (args->addr & PAGE_MASK) {
602                 td->td_retval[0] = 0;
603                 return (EINVAL);
604         }
605
606         args->new_len = round_page(args->new_len);
607         args->old_len = round_page(args->old_len);
608
609         if (args->new_len > args->old_len) {
610                 td->td_retval[0] = 0;
611                 return (ENOMEM);
612         }
613
614         if (args->new_len < args->old_len) {
615                 bsd_args.addr =
616                     (caddr_t)((uintptr_t)args->addr + args->new_len);
617                 bsd_args.len = args->old_len - args->new_len;
618                 error = sys_munmap(td, &bsd_args);
619         }
620
621         td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
622         return (error);
623 }
624
625 #define LINUX_MS_ASYNC       0x0001
626 #define LINUX_MS_INVALIDATE  0x0002
627 #define LINUX_MS_SYNC        0x0004
628
629 int
630 linux_msync(struct thread *td, struct linux_msync_args *args)
631 {
632         struct msync_args bsd_args;
633
634         bsd_args.addr = (caddr_t)(uintptr_t)args->addr;
635         bsd_args.len = (uintptr_t)args->len;
636         bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
637
638         return (sys_msync(td, &bsd_args));
639 }
640
641 int
642 linux_time(struct thread *td, struct linux_time_args *args)
643 {
644         struct timeval tv;
645         l_time_t tm;
646         int error;
647
648 #ifdef DEBUG
649         if (ldebug(time))
650                 printf(ARGS(time, "*"));
651 #endif
652
653         microtime(&tv);
654         tm = tv.tv_sec;
655         if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
656                 return (error);
657         td->td_retval[0] = tm;
658         return (0);
659 }
660
661 struct l_times_argv {
662         l_clock_t       tms_utime;
663         l_clock_t       tms_stime;
664         l_clock_t       tms_cutime;
665         l_clock_t       tms_cstime;
666 };
667
668
669 /*
670  * Glibc versions prior to 2.2.1 always use hard-coded CLK_TCK value.
671  * Since 2.2.1 Glibc uses value exported from kernel via AT_CLKTCK
672  * auxiliary vector entry.
673  */
674 #define CLK_TCK         100
675
676 #define CONVOTCK(r)     (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
677 #define CONVNTCK(r)     (r.tv_sec * stclohz + r.tv_usec / (1000000 / stclohz))
678
679 #define CONVTCK(r)      (linux_kernver(td) >= LINUX_KERNVER_2004000 ?           \
680                             CONVNTCK(r) : CONVOTCK(r))
681
682 int
683 linux_times(struct thread *td, struct linux_times_args *args)
684 {
685         struct timeval tv, utime, stime, cutime, cstime;
686         struct l_times_argv tms;
687         struct proc *p;
688         int error;
689
690 #ifdef DEBUG
691         if (ldebug(times))
692                 printf(ARGS(times, "*"));
693 #endif
694
695         if (args->buf != NULL) {
696                 p = td->td_proc;
697                 PROC_LOCK(p);
698                 PROC_SLOCK(p);
699                 calcru(p, &utime, &stime);
700                 PROC_SUNLOCK(p);
701                 calccru(p, &cutime, &cstime);
702                 PROC_UNLOCK(p);
703
704                 tms.tms_utime = CONVTCK(utime);
705                 tms.tms_stime = CONVTCK(stime);
706
707                 tms.tms_cutime = CONVTCK(cutime);
708                 tms.tms_cstime = CONVTCK(cstime);
709
710                 if ((error = copyout(&tms, args->buf, sizeof(tms))))
711                         return (error);
712         }
713
714         microuptime(&tv);
715         td->td_retval[0] = (int)CONVTCK(tv);
716         return (0);
717 }
718
719 int
720 linux_newuname(struct thread *td, struct linux_newuname_args *args)
721 {
722         struct l_new_utsname utsname;
723         char osname[LINUX_MAX_UTSNAME];
724         char osrelease[LINUX_MAX_UTSNAME];
725         char *p;
726
727 #ifdef DEBUG
728         if (ldebug(newuname))
729                 printf(ARGS(newuname, "*"));
730 #endif
731
732         linux_get_osname(td, osname);
733         linux_get_osrelease(td, osrelease);
734
735         bzero(&utsname, sizeof(utsname));
736         strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
737         getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
738         getcreddomainname(td->td_ucred, utsname.domainname, LINUX_MAX_UTSNAME);
739         strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
740         strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
741         for (p = utsname.version; *p != '\0'; ++p)
742                 if (*p == '\n') {
743                         *p = '\0';
744                         break;
745                 }
746         strlcpy(utsname.machine, linux_platform, LINUX_MAX_UTSNAME);
747
748         return (copyout(&utsname, args->buf, sizeof(utsname)));
749 }
750
751 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
752 struct l_utimbuf {
753         l_time_t l_actime;
754         l_time_t l_modtime;
755 };
756
757 int
758 linux_utime(struct thread *td, struct linux_utime_args *args)
759 {
760         struct timeval tv[2], *tvp;
761         struct l_utimbuf lut;
762         char *fname;
763         int error;
764
765         LCONVPATHEXIST(td, args->fname, &fname);
766
767 #ifdef DEBUG
768         if (ldebug(utime))
769                 printf(ARGS(utime, "%s, *"), fname);
770 #endif
771
772         if (args->times) {
773                 if ((error = copyin(args->times, &lut, sizeof lut))) {
774                         LFREEPATH(fname);
775                         return (error);
776                 }
777                 tv[0].tv_sec = lut.l_actime;
778                 tv[0].tv_usec = 0;
779                 tv[1].tv_sec = lut.l_modtime;
780                 tv[1].tv_usec = 0;
781                 tvp = tv;
782         } else
783                 tvp = NULL;
784
785         error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
786         LFREEPATH(fname);
787         return (error);
788 }
789
790 int
791 linux_utimes(struct thread *td, struct linux_utimes_args *args)
792 {
793         l_timeval ltv[2];
794         struct timeval tv[2], *tvp = NULL;
795         char *fname;
796         int error;
797
798         LCONVPATHEXIST(td, args->fname, &fname);
799
800 #ifdef DEBUG
801         if (ldebug(utimes))
802                 printf(ARGS(utimes, "%s, *"), fname);
803 #endif
804
805         if (args->tptr != NULL) {
806                 if ((error = copyin(args->tptr, ltv, sizeof ltv))) {
807                         LFREEPATH(fname);
808                         return (error);
809                 }
810                 tv[0].tv_sec = ltv[0].tv_sec;
811                 tv[0].tv_usec = ltv[0].tv_usec;
812                 tv[1].tv_sec = ltv[1].tv_sec;
813                 tv[1].tv_usec = ltv[1].tv_usec;
814                 tvp = tv;
815         }
816
817         error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
818         LFREEPATH(fname);
819         return (error);
820 }
821
822 int
823 linux_futimesat(struct thread *td, struct linux_futimesat_args *args)
824 {
825         l_timeval ltv[2];
826         struct timeval tv[2], *tvp = NULL;
827         char *fname;
828         int error, dfd;
829
830         dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
831         LCONVPATHEXIST_AT(td, args->filename, &fname, dfd);
832
833 #ifdef DEBUG
834         if (ldebug(futimesat))
835                 printf(ARGS(futimesat, "%s, *"), fname);
836 #endif
837
838         if (args->utimes != NULL) {
839                 if ((error = copyin(args->utimes, ltv, sizeof ltv))) {
840                         LFREEPATH(fname);
841                         return (error);
842                 }
843                 tv[0].tv_sec = ltv[0].tv_sec;
844                 tv[0].tv_usec = ltv[0].tv_usec;
845                 tv[1].tv_sec = ltv[1].tv_sec;
846                 tv[1].tv_usec = ltv[1].tv_usec;
847                 tvp = tv;
848         }
849
850         error = kern_utimesat(td, dfd, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
851         LFREEPATH(fname);
852         return (error);
853 }
854 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
855
856 int
857 linux_common_wait(struct thread *td, int pid, int *status,
858     int options, struct rusage *ru)
859 {
860         int error, tmpstat;
861
862         error = kern_wait(td, pid, &tmpstat, options, ru);
863         if (error)
864                 return (error);
865
866         if (status) {
867                 tmpstat &= 0xffff;
868                 if (WIFSIGNALED(tmpstat))
869                         tmpstat = (tmpstat & 0xffffff80) |
870                             BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
871                 else if (WIFSTOPPED(tmpstat))
872                         tmpstat = (tmpstat & 0xffff00ff) |
873                             (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
874                 error = copyout(&tmpstat, status, sizeof(int));
875         }
876
877         return (error);
878 }
879
880 int
881 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
882 {
883         int options;
884  
885 #ifdef DEBUG
886         if (ldebug(waitpid))
887                 printf(ARGS(waitpid, "%d, %p, %d"),
888                     args->pid, (void *)args->status, args->options);
889 #endif
890         /*
891          * this is necessary because the test in kern_wait doesn't work
892          * because we mess with the options here
893          */
894         if (args->options & ~(WUNTRACED | WNOHANG | WCONTINUED | __WCLONE))
895                 return (EINVAL);
896    
897         options = (args->options & (WNOHANG | WUNTRACED));
898         /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
899         if (args->options & __WCLONE)
900                 options |= WLINUXCLONE;
901
902         return (linux_common_wait(td, args->pid, args->status, options, NULL));
903 }
904
905
906 int
907 linux_mknod(struct thread *td, struct linux_mknod_args *args)
908 {
909         char *path;
910         int error;
911
912         LCONVPATHCREAT(td, args->path, &path);
913
914 #ifdef DEBUG
915         if (ldebug(mknod))
916                 printf(ARGS(mknod, "%s, %d, %d"), path, args->mode, args->dev);
917 #endif
918
919         switch (args->mode & S_IFMT) {
920         case S_IFIFO:
921         case S_IFSOCK:
922                 error = kern_mkfifo(td, path, UIO_SYSSPACE, args->mode);
923                 break;
924
925         case S_IFCHR:
926         case S_IFBLK:
927                 error = kern_mknod(td, path, UIO_SYSSPACE, args->mode,
928                     args->dev);
929                 break;
930
931         case S_IFDIR:
932                 error = EPERM;
933                 break;
934
935         case 0:
936                 args->mode |= S_IFREG;
937                 /* FALLTHROUGH */
938         case S_IFREG:
939                 error = kern_open(td, path, UIO_SYSSPACE,
940                     O_WRONLY | O_CREAT | O_TRUNC, args->mode);
941                 if (error == 0)
942                         kern_close(td, td->td_retval[0]);
943                 break;
944
945         default:
946                 error = EINVAL;
947                 break;
948         }
949         LFREEPATH(path);
950         return (error);
951 }
952
953 int
954 linux_mknodat(struct thread *td, struct linux_mknodat_args *args)
955 {
956         char *path;
957         int error, dfd;
958
959         dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
960         LCONVPATHCREAT_AT(td, args->filename, &path, dfd);
961
962 #ifdef DEBUG
963         if (ldebug(mknodat))
964                 printf(ARGS(mknodat, "%s, %d, %d"), path, args->mode, args->dev);
965 #endif
966
967         switch (args->mode & S_IFMT) {
968         case S_IFIFO:
969         case S_IFSOCK:
970                 error = kern_mkfifoat(td, dfd, path, UIO_SYSSPACE, args->mode);
971                 break;
972
973         case S_IFCHR:
974         case S_IFBLK:
975                 error = kern_mknodat(td, dfd, path, UIO_SYSSPACE, args->mode,
976                     args->dev);
977                 break;
978
979         case S_IFDIR:
980                 error = EPERM;
981                 break;
982
983         case 0:
984                 args->mode |= S_IFREG;
985                 /* FALLTHROUGH */
986         case S_IFREG:
987                 error = kern_openat(td, dfd, path, UIO_SYSSPACE,
988                     O_WRONLY | O_CREAT | O_TRUNC, args->mode);
989                 if (error == 0)
990                         kern_close(td, td->td_retval[0]);
991                 break;
992
993         default:
994                 error = EINVAL;
995                 break;
996         }
997         LFREEPATH(path);
998         return (error);
999 }
1000
1001 /*
1002  * UGH! This is just about the dumbest idea I've ever heard!!
1003  */
1004 int
1005 linux_personality(struct thread *td, struct linux_personality_args *args)
1006 {
1007 #ifdef DEBUG
1008         if (ldebug(personality))
1009                 printf(ARGS(personality, "%lu"), (unsigned long)args->per);
1010 #endif
1011         if (args->per != 0)
1012                 return (EINVAL);
1013
1014         /* Yes Jim, it's still a Linux... */
1015         td->td_retval[0] = 0;
1016         return (0);
1017 }
1018
1019 struct l_itimerval {
1020         l_timeval it_interval;
1021         l_timeval it_value;
1022 };
1023
1024 #define B2L_ITIMERVAL(bip, lip)                                         \
1025         (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec;          \
1026         (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec;        \
1027         (bip)->it_value.tv_sec = (lip)->it_value.tv_sec;                \
1028         (bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
1029
1030 int
1031 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
1032 {
1033         int error;
1034         struct l_itimerval ls;
1035         struct itimerval aitv, oitv;
1036
1037 #ifdef DEBUG
1038         if (ldebug(setitimer))
1039                 printf(ARGS(setitimer, "%p, %p"),
1040                     (void *)uap->itv, (void *)uap->oitv);
1041 #endif
1042
1043         if (uap->itv == NULL) {
1044                 uap->itv = uap->oitv;
1045                 return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
1046         }
1047
1048         error = copyin(uap->itv, &ls, sizeof(ls));
1049         if (error != 0)
1050                 return (error);
1051         B2L_ITIMERVAL(&aitv, &ls);
1052 #ifdef DEBUG
1053         if (ldebug(setitimer)) {
1054                 printf("setitimer: value: sec: %jd, usec: %ld\n",
1055                     (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec);
1056                 printf("setitimer: interval: sec: %jd, usec: %ld\n",
1057                     (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec);
1058         }
1059 #endif
1060         error = kern_setitimer(td, uap->which, &aitv, &oitv);
1061         if (error != 0 || uap->oitv == NULL)
1062                 return (error);
1063         B2L_ITIMERVAL(&ls, &oitv);
1064
1065         return (copyout(&ls, uap->oitv, sizeof(ls)));
1066 }
1067
1068 int
1069 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
1070 {
1071         int error;
1072         struct l_itimerval ls;
1073         struct itimerval aitv;
1074
1075 #ifdef DEBUG
1076         if (ldebug(getitimer))
1077                 printf(ARGS(getitimer, "%p"), (void *)uap->itv);
1078 #endif
1079         error = kern_getitimer(td, uap->which, &aitv);
1080         if (error != 0)
1081                 return (error);
1082         B2L_ITIMERVAL(&ls, &aitv);
1083         return (copyout(&ls, uap->itv, sizeof(ls)));
1084 }
1085
1086 int
1087 linux_nice(struct thread *td, struct linux_nice_args *args)
1088 {
1089         struct setpriority_args bsd_args;
1090
1091         bsd_args.which = PRIO_PROCESS;
1092         bsd_args.who = 0;               /* current process */
1093         bsd_args.prio = args->inc;
1094         return (sys_setpriority(td, &bsd_args));
1095 }
1096
1097 int
1098 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
1099 {
1100         struct ucred *newcred, *oldcred;
1101         l_gid_t *linux_gidset;
1102         gid_t *bsd_gidset;
1103         int ngrp, error;
1104         struct proc *p;
1105
1106         ngrp = args->gidsetsize;
1107         if (ngrp < 0 || ngrp >= ngroups_max + 1)
1108                 return (EINVAL);
1109         linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_TEMP, M_WAITOK);
1110         error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
1111         if (error)
1112                 goto out;
1113         newcred = crget();
1114         p = td->td_proc;
1115         PROC_LOCK(p);
1116         oldcred = crcopysafe(p, newcred);
1117
1118         /*
1119          * cr_groups[0] holds egid. Setting the whole set from
1120          * the supplied set will cause egid to be changed too.
1121          * Keep cr_groups[0] unchanged to prevent that.
1122          */
1123
1124         if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0) {
1125                 PROC_UNLOCK(p);
1126                 crfree(newcred);
1127                 goto out;
1128         }
1129
1130         if (ngrp > 0) {
1131                 newcred->cr_ngroups = ngrp + 1;
1132
1133                 bsd_gidset = newcred->cr_groups;
1134                 ngrp--;
1135                 while (ngrp >= 0) {
1136                         bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1137                         ngrp--;
1138                 }
1139         } else
1140                 newcred->cr_ngroups = 1;
1141
1142         setsugid(p);
1143         p->p_ucred = newcred;
1144         PROC_UNLOCK(p);
1145         crfree(oldcred);
1146         error = 0;
1147 out:
1148         free(linux_gidset, M_TEMP);
1149         return (error);
1150 }
1151
1152 int
1153 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1154 {
1155         struct ucred *cred;
1156         l_gid_t *linux_gidset;
1157         gid_t *bsd_gidset;
1158         int bsd_gidsetsz, ngrp, error;
1159
1160         cred = td->td_ucred;
1161         bsd_gidset = cred->cr_groups;
1162         bsd_gidsetsz = cred->cr_ngroups - 1;
1163
1164         /*
1165          * cr_groups[0] holds egid. Returning the whole set
1166          * here will cause a duplicate. Exclude cr_groups[0]
1167          * to prevent that.
1168          */
1169
1170         if ((ngrp = args->gidsetsize) == 0) {
1171                 td->td_retval[0] = bsd_gidsetsz;
1172                 return (0);
1173         }
1174
1175         if (ngrp < bsd_gidsetsz)
1176                 return (EINVAL);
1177
1178         ngrp = 0;
1179         linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset),
1180             M_TEMP, M_WAITOK);
1181         while (ngrp < bsd_gidsetsz) {
1182                 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1183                 ngrp++;
1184         }
1185
1186         error = copyout(linux_gidset, args->grouplist, ngrp * sizeof(l_gid_t));
1187         free(linux_gidset, M_TEMP);
1188         if (error)
1189                 return (error);
1190
1191         td->td_retval[0] = ngrp;
1192         return (0);
1193 }
1194
1195 int
1196 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1197 {
1198         struct rlimit bsd_rlim;
1199         struct l_rlimit rlim;
1200         u_int which;
1201         int error;
1202
1203 #ifdef DEBUG
1204         if (ldebug(setrlimit))
1205                 printf(ARGS(setrlimit, "%d, %p"),
1206                     args->resource, (void *)args->rlim);
1207 #endif
1208
1209         if (args->resource >= LINUX_RLIM_NLIMITS)
1210                 return (EINVAL);
1211
1212         which = linux_to_bsd_resource[args->resource];
1213         if (which == -1)
1214                 return (EINVAL);
1215
1216         error = copyin(args->rlim, &rlim, sizeof(rlim));
1217         if (error)
1218                 return (error);
1219
1220         bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1221         bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1222         return (kern_setrlimit(td, which, &bsd_rlim));
1223 }
1224
1225 int
1226 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1227 {
1228         struct l_rlimit rlim;
1229         struct proc *p = td->td_proc;
1230         struct rlimit bsd_rlim;
1231         u_int which;
1232
1233 #ifdef DEBUG
1234         if (ldebug(old_getrlimit))
1235                 printf(ARGS(old_getrlimit, "%d, %p"),
1236                     args->resource, (void *)args->rlim);
1237 #endif
1238
1239         if (args->resource >= LINUX_RLIM_NLIMITS)
1240                 return (EINVAL);
1241
1242         which = linux_to_bsd_resource[args->resource];
1243         if (which == -1)
1244                 return (EINVAL);
1245
1246         PROC_LOCK(p);
1247         lim_rlimit(p, which, &bsd_rlim);
1248         PROC_UNLOCK(p);
1249
1250 #ifdef COMPAT_LINUX32
1251         rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1252         if (rlim.rlim_cur == UINT_MAX)
1253                 rlim.rlim_cur = INT_MAX;
1254         rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1255         if (rlim.rlim_max == UINT_MAX)
1256                 rlim.rlim_max = INT_MAX;
1257 #else
1258         rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1259         if (rlim.rlim_cur == ULONG_MAX)
1260                 rlim.rlim_cur = LONG_MAX;
1261         rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1262         if (rlim.rlim_max == ULONG_MAX)
1263                 rlim.rlim_max = LONG_MAX;
1264 #endif
1265         return (copyout(&rlim, args->rlim, sizeof(rlim)));
1266 }
1267
1268 int
1269 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1270 {
1271         struct l_rlimit rlim;
1272         struct proc *p = td->td_proc;
1273         struct rlimit bsd_rlim;
1274         u_int which;
1275
1276 #ifdef DEBUG
1277         if (ldebug(getrlimit))
1278                 printf(ARGS(getrlimit, "%d, %p"),
1279                     args->resource, (void *)args->rlim);
1280 #endif
1281
1282         if (args->resource >= LINUX_RLIM_NLIMITS)
1283                 return (EINVAL);
1284
1285         which = linux_to_bsd_resource[args->resource];
1286         if (which == -1)
1287                 return (EINVAL);
1288
1289         PROC_LOCK(p);
1290         lim_rlimit(p, which, &bsd_rlim);
1291         PROC_UNLOCK(p);
1292
1293         rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1294         rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1295         return (copyout(&rlim, args->rlim, sizeof(rlim)));
1296 }
1297
1298 int
1299 linux_sched_setscheduler(struct thread *td,
1300     struct linux_sched_setscheduler_args *args)
1301 {
1302         struct sched_setscheduler_args bsd;
1303
1304 #ifdef DEBUG
1305         if (ldebug(sched_setscheduler))
1306                 printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1307                     args->pid, args->policy, (const void *)args->param);
1308 #endif
1309
1310         switch (args->policy) {
1311         case LINUX_SCHED_OTHER:
1312                 bsd.policy = SCHED_OTHER;
1313                 break;
1314         case LINUX_SCHED_FIFO:
1315                 bsd.policy = SCHED_FIFO;
1316                 break;
1317         case LINUX_SCHED_RR:
1318                 bsd.policy = SCHED_RR;
1319                 break;
1320         default:
1321                 return (EINVAL);
1322         }
1323
1324         bsd.pid = args->pid;
1325         bsd.param = (struct sched_param *)args->param;
1326         return (sys_sched_setscheduler(td, &bsd));
1327 }
1328
1329 int
1330 linux_sched_getscheduler(struct thread *td,
1331     struct linux_sched_getscheduler_args *args)
1332 {
1333         struct sched_getscheduler_args bsd;
1334         int error;
1335
1336 #ifdef DEBUG
1337         if (ldebug(sched_getscheduler))
1338                 printf(ARGS(sched_getscheduler, "%d"), args->pid);
1339 #endif
1340
1341         bsd.pid = args->pid;
1342         error = sys_sched_getscheduler(td, &bsd);
1343
1344         switch (td->td_retval[0]) {
1345         case SCHED_OTHER:
1346                 td->td_retval[0] = LINUX_SCHED_OTHER;
1347                 break;
1348         case SCHED_FIFO:
1349                 td->td_retval[0] = LINUX_SCHED_FIFO;
1350                 break;
1351         case SCHED_RR:
1352                 td->td_retval[0] = LINUX_SCHED_RR;
1353                 break;
1354         }
1355
1356         return (error);
1357 }
1358
1359 int
1360 linux_sched_get_priority_max(struct thread *td,
1361     struct linux_sched_get_priority_max_args *args)
1362 {
1363         struct sched_get_priority_max_args bsd;
1364
1365 #ifdef DEBUG
1366         if (ldebug(sched_get_priority_max))
1367                 printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1368 #endif
1369
1370         switch (args->policy) {
1371         case LINUX_SCHED_OTHER:
1372                 bsd.policy = SCHED_OTHER;
1373                 break;
1374         case LINUX_SCHED_FIFO:
1375                 bsd.policy = SCHED_FIFO;
1376                 break;
1377         case LINUX_SCHED_RR:
1378                 bsd.policy = SCHED_RR;
1379                 break;
1380         default:
1381                 return (EINVAL);
1382         }
1383         return (sys_sched_get_priority_max(td, &bsd));
1384 }
1385
1386 int
1387 linux_sched_get_priority_min(struct thread *td,
1388     struct linux_sched_get_priority_min_args *args)
1389 {
1390         struct sched_get_priority_min_args bsd;
1391
1392 #ifdef DEBUG
1393         if (ldebug(sched_get_priority_min))
1394                 printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1395 #endif
1396
1397         switch (args->policy) {
1398         case LINUX_SCHED_OTHER:
1399                 bsd.policy = SCHED_OTHER;
1400                 break;
1401         case LINUX_SCHED_FIFO:
1402                 bsd.policy = SCHED_FIFO;
1403                 break;
1404         case LINUX_SCHED_RR:
1405                 bsd.policy = SCHED_RR;
1406                 break;
1407         default:
1408                 return (EINVAL);
1409         }
1410         return (sys_sched_get_priority_min(td, &bsd));
1411 }
1412
1413 #define REBOOT_CAD_ON   0x89abcdef
1414 #define REBOOT_CAD_OFF  0
1415 #define REBOOT_HALT     0xcdef0123
1416 #define REBOOT_RESTART  0x01234567
1417 #define REBOOT_RESTART2 0xA1B2C3D4
1418 #define REBOOT_POWEROFF 0x4321FEDC
1419 #define REBOOT_MAGIC1   0xfee1dead
1420 #define REBOOT_MAGIC2   0x28121969
1421 #define REBOOT_MAGIC2A  0x05121996
1422 #define REBOOT_MAGIC2B  0x16041998
1423
1424 int
1425 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1426 {
1427         struct reboot_args bsd_args;
1428
1429 #ifdef DEBUG
1430         if (ldebug(reboot))
1431                 printf(ARGS(reboot, "0x%x"), args->cmd);
1432 #endif
1433
1434         if (args->magic1 != REBOOT_MAGIC1)
1435                 return (EINVAL);
1436
1437         switch (args->magic2) {
1438         case REBOOT_MAGIC2:
1439         case REBOOT_MAGIC2A:
1440         case REBOOT_MAGIC2B:
1441                 break;
1442         default:
1443                 return (EINVAL);
1444         }
1445
1446         switch (args->cmd) {
1447         case REBOOT_CAD_ON:
1448         case REBOOT_CAD_OFF:
1449                 return (priv_check(td, PRIV_REBOOT));
1450         case REBOOT_HALT:
1451                 bsd_args.opt = RB_HALT;
1452                 break;
1453         case REBOOT_RESTART:
1454         case REBOOT_RESTART2:
1455                 bsd_args.opt = 0;
1456                 break;
1457         case REBOOT_POWEROFF:
1458                 bsd_args.opt = RB_POWEROFF;
1459                 break;
1460         default:
1461                 return (EINVAL);
1462         }
1463         return (sys_reboot(td, &bsd_args));
1464 }
1465
1466
1467 /*
1468  * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1469  * td->td_retval[1] when COMPAT_43 is defined. This clobbers registers that
1470  * are assumed to be preserved. The following lightweight syscalls fixes
1471  * this. See also linux_getgid16() and linux_getuid16() in linux_uid16.c
1472  *
1473  * linux_getpid() - MP SAFE
1474  * linux_getgid() - MP SAFE
1475  * linux_getuid() - MP SAFE
1476  */
1477
1478 int
1479 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1480 {
1481         struct linux_emuldata *em;
1482
1483 #ifdef DEBUG
1484         if (ldebug(getpid))
1485                 printf(ARGS(getpid, ""));
1486 #endif
1487
1488         if (linux_use26(td)) {
1489                 em = em_find(td->td_proc, EMUL_DONTLOCK);
1490                 KASSERT(em != NULL, ("getpid: emuldata not found.\n"));
1491                 td->td_retval[0] = em->shared->group_pid;
1492         } else {
1493                 td->td_retval[0] = td->td_proc->p_pid;
1494         }
1495
1496         return (0);
1497 }
1498
1499 int
1500 linux_gettid(struct thread *td, struct linux_gettid_args *args)
1501 {
1502
1503 #ifdef DEBUG
1504         if (ldebug(gettid))
1505                 printf(ARGS(gettid, ""));
1506 #endif
1507
1508         td->td_retval[0] = td->td_proc->p_pid;
1509         return (0);
1510 }
1511
1512
1513 int
1514 linux_getppid(struct thread *td, struct linux_getppid_args *args)
1515 {
1516         struct linux_emuldata *em;
1517         struct proc *p, *pp;
1518
1519 #ifdef DEBUG
1520         if (ldebug(getppid))
1521                 printf(ARGS(getppid, ""));
1522 #endif
1523
1524         if (!linux_use26(td)) {
1525                 PROC_LOCK(td->td_proc);
1526                 td->td_retval[0] = td->td_proc->p_pptr->p_pid;
1527                 PROC_UNLOCK(td->td_proc);
1528                 return (0);
1529         }
1530
1531         em = em_find(td->td_proc, EMUL_DONTLOCK);
1532
1533         KASSERT(em != NULL, ("getppid: process emuldata not found.\n"));
1534
1535         /* find the group leader */
1536         p = pfind(em->shared->group_pid);
1537
1538         if (p == NULL) {
1539 #ifdef DEBUG
1540                 printf(LMSG("parent process not found.\n"));
1541 #endif
1542                 return (0);
1543         }
1544
1545         pp = p->p_pptr;         /* switch to parent */
1546         PROC_LOCK(pp);
1547         PROC_UNLOCK(p);
1548
1549         /* if its also linux process */
1550         if (pp->p_sysent == &elf_linux_sysvec) {
1551                 em = em_find(pp, EMUL_DONTLOCK);
1552                 KASSERT(em != NULL, ("getppid: parent emuldata not found.\n"));
1553
1554                 td->td_retval[0] = em->shared->group_pid;
1555         } else
1556                 td->td_retval[0] = pp->p_pid;
1557
1558         PROC_UNLOCK(pp);
1559
1560         return (0);
1561 }
1562
1563 int
1564 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1565 {
1566
1567 #ifdef DEBUG
1568         if (ldebug(getgid))
1569                 printf(ARGS(getgid, ""));
1570 #endif
1571
1572         td->td_retval[0] = td->td_ucred->cr_rgid;
1573         return (0);
1574 }
1575
1576 int
1577 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1578 {
1579
1580 #ifdef DEBUG
1581         if (ldebug(getuid))
1582                 printf(ARGS(getuid, ""));
1583 #endif
1584
1585         td->td_retval[0] = td->td_ucred->cr_ruid;
1586         return (0);
1587 }
1588
1589
1590 int
1591 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1592 {
1593         struct getsid_args bsd;
1594
1595 #ifdef DEBUG
1596         if (ldebug(getsid))
1597                 printf(ARGS(getsid, "%i"), args->pid);
1598 #endif
1599
1600         bsd.pid = args->pid;
1601         return (sys_getsid(td, &bsd));
1602 }
1603
1604 int
1605 linux_nosys(struct thread *td, struct nosys_args *ignore)
1606 {
1607
1608         return (ENOSYS);
1609 }
1610
1611 int
1612 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1613 {
1614         struct getpriority_args bsd_args;
1615         int error;
1616
1617 #ifdef DEBUG
1618         if (ldebug(getpriority))
1619                 printf(ARGS(getpriority, "%i, %i"), args->which, args->who);
1620 #endif
1621
1622         bsd_args.which = args->which;
1623         bsd_args.who = args->who;
1624         error = sys_getpriority(td, &bsd_args);
1625         td->td_retval[0] = 20 - td->td_retval[0];
1626         return (error);
1627 }
1628
1629 int
1630 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1631 {
1632         int name[2];
1633
1634 #ifdef DEBUG
1635         if (ldebug(sethostname))
1636                 printf(ARGS(sethostname, "*, %i"), args->len);
1637 #endif
1638
1639         name[0] = CTL_KERN;
1640         name[1] = KERN_HOSTNAME;
1641         return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1642             args->len, 0, 0));
1643 }
1644
1645 int
1646 linux_setdomainname(struct thread *td, struct linux_setdomainname_args *args)
1647 {
1648         int name[2];
1649
1650 #ifdef DEBUG
1651         if (ldebug(setdomainname))
1652                 printf(ARGS(setdomainname, "*, %i"), args->len);
1653 #endif
1654
1655         name[0] = CTL_KERN;
1656         name[1] = KERN_NISDOMAINNAME;
1657         return (userland_sysctl(td, name, 2, 0, 0, 0, args->name,
1658             args->len, 0, 0));
1659 }
1660
1661 int
1662 linux_exit_group(struct thread *td, struct linux_exit_group_args *args)
1663 {
1664         struct linux_emuldata *em;
1665
1666 #ifdef DEBUG
1667         if (ldebug(exit_group))
1668                 printf(ARGS(exit_group, "%i"), args->error_code);
1669 #endif
1670
1671         em = em_find(td->td_proc, EMUL_DONTLOCK);
1672         if (em->shared->refs > 1) {
1673                 EMUL_SHARED_WLOCK(&emul_shared_lock);
1674                 em->shared->flags |= EMUL_SHARED_HASXSTAT;
1675                 em->shared->xstat = W_EXITCODE(args->error_code, 0);
1676                 EMUL_SHARED_WUNLOCK(&emul_shared_lock);
1677                 if (linux_use26(td))
1678                         linux_kill_threads(td, SIGKILL);
1679         }
1680
1681         /*
1682          * XXX: we should send a signal to the parent if
1683          * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?)
1684          * as it doesnt occur often.
1685          */
1686         exit1(td, W_EXITCODE(args->error_code, 0));
1687
1688         return (0);
1689 }
1690
1691 #define _LINUX_CAPABILITY_VERSION  0x19980330
1692
1693 struct l_user_cap_header {
1694         l_int   version;
1695         l_int   pid;
1696 };
1697
1698 struct l_user_cap_data {
1699         l_int   effective;
1700         l_int   permitted;
1701         l_int   inheritable;
1702 };
1703
1704 int
1705 linux_capget(struct thread *td, struct linux_capget_args *args)
1706 {
1707         struct l_user_cap_header luch;
1708         struct l_user_cap_data lucd;
1709         int error;
1710
1711         if (args->hdrp == NULL)
1712                 return (EFAULT);
1713
1714         error = copyin(args->hdrp, &luch, sizeof(luch));
1715         if (error != 0)
1716                 return (error);
1717
1718         if (luch.version != _LINUX_CAPABILITY_VERSION) {
1719                 luch.version = _LINUX_CAPABILITY_VERSION;
1720                 error = copyout(&luch, args->hdrp, sizeof(luch));
1721                 if (error)
1722                         return (error);
1723                 return (EINVAL);
1724         }
1725
1726         if (luch.pid)
1727                 return (EPERM);
1728
1729         if (args->datap) {
1730                 /*
1731                  * The current implementation doesn't support setting
1732                  * a capability (it's essentially a stub) so indicate
1733                  * that no capabilities are currently set or available
1734                  * to request.
1735                  */
1736                 bzero (&lucd, sizeof(lucd));
1737                 error = copyout(&lucd, args->datap, sizeof(lucd));
1738         }
1739
1740         return (error);
1741 }
1742
1743 int
1744 linux_capset(struct thread *td, struct linux_capset_args *args)
1745 {
1746         struct l_user_cap_header luch;
1747         struct l_user_cap_data lucd;
1748         int error;
1749
1750         if (args->hdrp == NULL || args->datap == NULL)
1751                 return (EFAULT);
1752
1753         error = copyin(args->hdrp, &luch, sizeof(luch));
1754         if (error != 0)
1755                 return (error);
1756
1757         if (luch.version != _LINUX_CAPABILITY_VERSION) {
1758                 luch.version = _LINUX_CAPABILITY_VERSION;
1759                 error = copyout(&luch, args->hdrp, sizeof(luch));
1760                 if (error)
1761                         return (error);
1762                 return (EINVAL);
1763         }
1764
1765         if (luch.pid)
1766                 return (EPERM);
1767
1768         error = copyin(args->datap, &lucd, sizeof(lucd));
1769         if (error != 0)
1770                 return (error);
1771
1772         /* We currently don't support setting any capabilities. */
1773         if (lucd.effective || lucd.permitted || lucd.inheritable) {
1774                 linux_msg(td,
1775                           "capset effective=0x%x, permitted=0x%x, "
1776                           "inheritable=0x%x is not implemented",
1777                           (int)lucd.effective, (int)lucd.permitted,
1778                           (int)lucd.inheritable);
1779                 return (EPERM);
1780         }
1781
1782         return (0);
1783 }
1784
1785 int
1786 linux_prctl(struct thread *td, struct linux_prctl_args *args)
1787 {
1788         int error = 0, max_size;
1789         struct proc *p = td->td_proc;
1790         char comm[LINUX_MAX_COMM_LEN];
1791         struct linux_emuldata *em;
1792         int pdeath_signal;
1793
1794 #ifdef DEBUG
1795         if (ldebug(prctl))
1796                 printf(ARGS(prctl, "%d, %d, %d, %d, %d"), args->option,
1797                     args->arg2, args->arg3, args->arg4, args->arg5);
1798 #endif
1799
1800         switch (args->option) {
1801         case LINUX_PR_SET_PDEATHSIG:
1802                 if (!LINUX_SIG_VALID(args->arg2))
1803                         return (EINVAL);
1804                 em = em_find(p, EMUL_DOLOCK);
1805                 KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1806                 em->pdeath_signal = args->arg2;
1807                 EMUL_UNLOCK(&emul_lock);
1808                 break;
1809         case LINUX_PR_GET_PDEATHSIG:
1810                 em = em_find(p, EMUL_DOLOCK);
1811                 KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1812                 pdeath_signal = em->pdeath_signal;
1813                 EMUL_UNLOCK(&emul_lock);
1814                 error = copyout(&pdeath_signal,
1815                     (void *)(register_t)args->arg2,
1816                     sizeof(pdeath_signal));
1817                 break;
1818         case LINUX_PR_GET_KEEPCAPS:
1819                 /*
1820                  * Indicate that we always clear the effective and
1821                  * permitted capability sets when the user id becomes
1822                  * non-zero (actually the capability sets are simply
1823                  * always zero in the current implementation).
1824                  */
1825                 td->td_retval[0] = 0;
1826                 break;
1827         case LINUX_PR_SET_KEEPCAPS:
1828                 /*
1829                  * Ignore requests to keep the effective and permitted
1830                  * capability sets when the user id becomes non-zero.
1831                  */
1832                 break;
1833         case LINUX_PR_SET_NAME:
1834                 /*
1835                  * To be on the safe side we need to make sure to not
1836                  * overflow the size a linux program expects. We already
1837                  * do this here in the copyin, so that we don't need to
1838                  * check on copyout.
1839                  */
1840                 max_size = MIN(sizeof(comm), sizeof(p->p_comm));
1841                 error = copyinstr((void *)(register_t)args->arg2, comm,
1842                     max_size, NULL);
1843
1844                 /* Linux silently truncates the name if it is too long. */
1845                 if (error == ENAMETOOLONG) {
1846                         /*
1847                          * XXX: copyinstr() isn't documented to populate the
1848                          * array completely, so do a copyin() to be on the
1849                          * safe side. This should be changed in case
1850                          * copyinstr() is changed to guarantee this.
1851                          */
1852                         error = copyin((void *)(register_t)args->arg2, comm,
1853                             max_size - 1);
1854                         comm[max_size - 1] = '\0';
1855                 }
1856                 if (error)
1857                         return (error);
1858
1859                 PROC_LOCK(p);
1860                 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
1861                 PROC_UNLOCK(p);
1862                 break;
1863         case LINUX_PR_GET_NAME:
1864                 PROC_LOCK(p);
1865                 strlcpy(comm, p->p_comm, sizeof(comm));
1866                 PROC_UNLOCK(p);
1867                 error = copyout(comm, (void *)(register_t)args->arg2,
1868                     strlen(comm) + 1);
1869                 break;
1870         default:
1871                 error = EINVAL;
1872                 break;
1873         }
1874
1875         return (error);
1876 }
1877
1878 /*
1879  * Get affinity of a process.
1880  */
1881 int
1882 linux_sched_getaffinity(struct thread *td,
1883     struct linux_sched_getaffinity_args *args)
1884 {
1885         int error;
1886         struct cpuset_getaffinity_args cga;
1887
1888 #ifdef DEBUG
1889         if (ldebug(sched_getaffinity))
1890                 printf(ARGS(sched_getaffinity, "%d, %d, *"), args->pid,
1891                     args->len);
1892 #endif
1893         if (args->len < sizeof(cpuset_t))
1894                 return (EINVAL);
1895
1896         cga.level = CPU_LEVEL_WHICH;
1897         cga.which = CPU_WHICH_PID;
1898         cga.id = args->pid;
1899         cga.cpusetsize = sizeof(cpuset_t);
1900         cga.mask = (cpuset_t *) args->user_mask_ptr;
1901
1902         if ((error = sys_cpuset_getaffinity(td, &cga)) == 0)
1903                 td->td_retval[0] = sizeof(cpuset_t);
1904
1905         return (error);
1906 }
1907
1908 /*
1909  *  Set affinity of a process.
1910  */
1911 int
1912 linux_sched_setaffinity(struct thread *td,
1913     struct linux_sched_setaffinity_args *args)
1914 {
1915         struct cpuset_setaffinity_args csa;
1916
1917 #ifdef DEBUG
1918         if (ldebug(sched_setaffinity))
1919                 printf(ARGS(sched_setaffinity, "%d, %d, *"), args->pid,
1920                     args->len);
1921 #endif
1922         if (args->len < sizeof(cpuset_t))
1923                 return (EINVAL);
1924
1925         csa.level = CPU_LEVEL_WHICH;
1926         csa.which = CPU_WHICH_PID;
1927         csa.id = args->pid;
1928         csa.cpusetsize = sizeof(cpuset_t);
1929         csa.mask = (cpuset_t *) args->user_mask_ptr;
1930
1931         return (sys_cpuset_setaffinity(td, &csa));
1932 }