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