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