Merge branch 'vendor/NCURSES'
[dragonfly.git] / sys / emulation / linux / i386 / linux_machdep.c
1 /*-
2  * Copyright (c) 2000 Marcel Moolenaar
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 without 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/i386/linux/linux_machdep.c,v 1.6.2.4 2001/11/05 19:08:23 marcel Exp $
29  * $DragonFly: src/sys/emulation/linux/i386/linux_machdep.c,v 1.23 2007/07/30 17:41:23 pavalos Exp $
30  */
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/imgact.h>
35 #include <sys/kern_syscall.h>
36 #include <sys/lock.h>
37 #include <sys/mman.h>
38 #include <sys/nlookup.h>
39 #include <sys/proc.h>
40 #include <sys/priv.h>
41 #include <sys/resource.h>
42 #include <sys/resourcevar.h>
43 #include <sys/sysproto.h>
44 #include <sys/unistd.h>
45 #include <sys/wait.h>
46
47 #include <machine/frame.h>
48 #include <machine/psl.h>
49 #include <machine/segments.h>
50 #include <machine/sysarch.h>
51
52 #include <vm/vm.h>
53 #include <vm/pmap.h>
54 #include <vm/vm_map.h>
55
56 #include "linux.h"
57 #include "linux_proto.h"
58 #include "../linux_ipc.h"
59 #include "../linux_signal.h"
60 #include "../linux_util.h"
61
62 struct l_descriptor {
63         l_uint          entry_number;
64         l_ulong         base_addr;
65         l_uint          limit;
66         l_uint          seg_32bit:1;
67         l_uint          contents:2;
68         l_uint          read_exec_only:1;
69         l_uint          limit_in_pages:1;
70         l_uint          seg_not_present:1;
71         l_uint          useable:1;
72 };
73
74 struct l_old_select_argv {
75         l_int           nfds;
76         l_fd_set        *readfds;
77         l_fd_set        *writefds;
78         l_fd_set        *exceptfds;
79         struct l_timeval        *timeout;
80 };
81
82 int
83 linux_to_bsd_sigaltstack(int lsa)
84 {
85         int bsa = 0;
86
87         if (lsa & LINUX_SS_DISABLE)
88                 bsa |= SS_DISABLE;
89         if (lsa & LINUX_SS_ONSTACK)
90                 bsa |= SS_ONSTACK;
91         return (bsa);
92 }
93
94 int
95 bsd_to_linux_sigaltstack(int bsa)
96 {
97         int lsa = 0;
98
99         if (bsa & SS_DISABLE)
100                 lsa |= LINUX_SS_DISABLE;
101         if (bsa & SS_ONSTACK)
102                 lsa |= LINUX_SS_ONSTACK;
103         return (lsa);
104 }
105
106 /*
107  * MPALMOSTSAFE
108  */
109 int
110 sys_linux_execve(struct linux_execve_args *args)
111 {
112         struct nlookupdata nd;
113         struct image_args exec_args;
114         char *path;
115         int error;
116
117         error = linux_copyin_path(args->path, &path, LINUX_PATH_EXISTS);
118         if (error)
119                 return (error);
120 #ifdef DEBUG
121         if (ldebug(execve))
122                 kprintf(ARGS(execve, "%s"), path);
123 #endif
124         get_mplock();
125         error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
126         bzero(&exec_args, sizeof(exec_args));
127         if (error == 0) {
128                 error = exec_copyin_args(&exec_args, path, PATH_SYSSPACE,
129                                         args->argp, args->envp);
130         }
131         if (error == 0)
132                 error = kern_execve(&nd, &exec_args);
133         nlookup_done(&nd);
134
135         /*
136          * The syscall result is returned in registers to the new program.
137          * Linux will register %edx as an atexit function and we must be
138          * sure to set it to 0.  XXX
139          */
140         if (error == 0)
141                 args->sysmsg_result64 = 0;
142
143         exec_free_args(&exec_args);
144         linux_free_path(&path);
145
146         if (error < 0) {
147                 /* We hit a lethal error condition.  Let's die now. */
148                 exit1(W_EXITCODE(0, SIGABRT));
149                 /* NOTREACHED */
150         }
151         rel_mplock();
152
153         return(error);
154 }
155
156 struct l_ipc_kludge {
157         struct l_msgbuf *msgp;
158         l_long msgtyp;
159 };
160
161 /*
162  * MPALMOSTSAFE
163  */
164 int
165 sys_linux_ipc(struct linux_ipc_args *args)
166 {
167         int error = 0;
168
169         get_mplock();
170
171         switch (args->what & 0xFFFF) {
172         case LINUX_SEMOP: {
173                 struct linux_semop_args a;
174
175                 a.semid = args->arg1;
176                 a.tsops = args->ptr;
177                 a.nsops = args->arg2;
178                 a.sysmsg_lresult = 0;
179                 error = linux_semop(&a);
180                 args->sysmsg_lresult = a.sysmsg_lresult;
181                 break;
182         }
183         case LINUX_SEMGET: {
184                 struct linux_semget_args a;
185
186                 a.key = args->arg1;
187                 a.nsems = args->arg2;
188                 a.semflg = args->arg3;
189                 a.sysmsg_lresult = 0;
190                 error = linux_semget(&a);
191                 args->sysmsg_lresult = a.sysmsg_lresult;
192                 break;
193         }
194         case LINUX_SEMCTL: {
195                 struct linux_semctl_args a;
196                 int error;
197
198                 a.semid = args->arg1;
199                 a.semnum = args->arg2;
200                 a.cmd = args->arg3;
201                 a.sysmsg_lresult = 0;
202                 error = copyin((caddr_t)args->ptr, &a.arg, sizeof(a.arg));
203                 if (error)
204                         break;
205                 error = linux_semctl(&a);
206                 args->sysmsg_lresult = a.sysmsg_lresult;
207                 break;
208         }
209         case LINUX_MSGSND: {
210                 struct linux_msgsnd_args a;
211
212                 a.msqid = args->arg1;
213                 a.msgp = args->ptr;
214                 a.msgsz = args->arg2;
215                 a.msgflg = args->arg3;
216                 a.sysmsg_lresult = 0;
217                 error = linux_msgsnd(&a);
218                 args->sysmsg_lresult = a.sysmsg_lresult;
219                 break;
220         }
221         case LINUX_MSGRCV: {
222                 struct linux_msgrcv_args a;
223
224                 a.msqid = args->arg1;
225                 a.msgsz = args->arg2;
226                 a.msgflg = args->arg3;
227                 a.sysmsg_lresult = 0;
228                 if ((args->what >> 16) == 0) {
229                         struct l_ipc_kludge tmp;
230                         int error;
231
232                         if (args->ptr == NULL) {
233                                 error = EINVAL;
234                                 break;
235                         }
236                         error = copyin((caddr_t)args->ptr, &tmp, sizeof(tmp));
237                         if (error)
238                                 break;
239                         a.msgp = tmp.msgp;
240                         a.msgtyp = tmp.msgtyp;
241                 } else {
242                         a.msgp = args->ptr;
243                         a.msgtyp = args->arg5;
244                 }
245                 error = linux_msgrcv(&a);
246                 args->sysmsg_lresult = a.sysmsg_lresult;
247                 break;
248         }
249         case LINUX_MSGGET: {
250                 struct linux_msgget_args a;
251
252                 a.key = args->arg1;
253                 a.msgflg = args->arg2;
254                 a.sysmsg_lresult = 0;
255                 error = linux_msgget(&a);
256                 args->sysmsg_lresult = a.sysmsg_lresult;
257                 break;
258         }
259         case LINUX_MSGCTL: {
260                 struct linux_msgctl_args a;
261
262                 a.msqid = args->arg1;
263                 a.cmd = args->arg2;
264                 a.buf = args->ptr;
265                 a.sysmsg_lresult = 0;
266                 error = linux_msgctl(&a);
267                 args->sysmsg_lresult = a.sysmsg_lresult;
268                 break;
269         }
270         case LINUX_SHMAT: {
271                 struct linux_shmat_args a;
272
273                 a.shmid = args->arg1;
274                 a.shmaddr = args->ptr;
275                 a.shmflg = args->arg2;
276                 a.raddr = (l_ulong *)args->arg3;
277                 a.sysmsg_lresult = 0;
278                 error = linux_shmat(&a);
279                 args->sysmsg_lresult = a.sysmsg_lresult;
280                 break;
281         }
282         case LINUX_SHMDT: {
283                 struct linux_shmdt_args a;
284
285                 a.shmaddr = args->ptr;
286                 a.sysmsg_lresult = 0;
287                 error = linux_shmdt(&a);
288                 args->sysmsg_lresult = a.sysmsg_lresult;
289                 break;
290         }
291         case LINUX_SHMGET: {
292                 struct linux_shmget_args a;
293
294                 a.key = args->arg1;
295                 a.size = args->arg2;
296                 a.shmflg = args->arg3;
297                 a.sysmsg_lresult = 0;
298                 error = linux_shmget(&a);
299                 args->sysmsg_lresult = a.sysmsg_lresult;
300                 break;
301         }
302         case LINUX_SHMCTL: {
303                 struct linux_shmctl_args a;
304
305                 a.shmid = args->arg1;
306                 a.cmd = args->arg2;
307                 a.buf = args->ptr;
308                 a.sysmsg_lresult = 0;
309                 error = linux_shmctl(&a);
310                 args->sysmsg_lresult = a.sysmsg_lresult;
311                 break;
312         }
313         default:
314                 error = EINVAL;
315                 break;
316         }
317         rel_mplock();
318         return(error);
319 }
320
321 /*
322  * MPSAFE
323  */
324 int
325 sys_linux_old_select(struct linux_old_select_args *args)
326 {
327         struct l_old_select_argv linux_args;
328         struct linux_select_args newsel;
329         int error;
330
331 #ifdef DEBUG
332         if (ldebug(old_select))
333                 kprintf(ARGS(old_select, "%p"), args->ptr);
334 #endif
335
336         error = copyin((caddr_t)args->ptr, &linux_args, sizeof(linux_args));
337         if (error)
338                 return (error);
339
340         newsel.sysmsg_iresult = 0;
341         newsel.nfds = linux_args.nfds;
342         newsel.readfds = linux_args.readfds;
343         newsel.writefds = linux_args.writefds;
344         newsel.exceptfds = linux_args.exceptfds;
345         newsel.timeout = linux_args.timeout;
346         error = sys_linux_select(&newsel);
347         args->sysmsg_iresult = newsel.sysmsg_iresult;
348         return(error);
349 }
350
351 /*
352  * MPSAFE
353  */
354 int
355 sys_linux_fork(struct linux_fork_args *args)
356 {
357         int error;
358
359 #ifdef DEBUG
360         if (ldebug(fork))
361                 kprintf(ARGS(fork, ""));
362 #endif
363
364         if ((error = sys_fork((struct fork_args *)args)) != 0)
365                 return (error);
366
367         if (args->sysmsg_iresult == 1)
368                 args->sysmsg_iresult = 0;
369         return (0);
370 }
371
372 /*
373  * MPSAFE
374  */
375 int
376 sys_linux_exit_group(struct linux_exit_group_args *args)
377 {
378         struct exit_args newargs;
379         int error;
380
381         newargs.sysmsg_iresult = 0;
382         newargs.rval = args->rval;
383         error = sys_exit(&newargs);
384         args->sysmsg_iresult = newargs.sysmsg_iresult;
385         return (error);
386 }
387
388 /*
389  * MPSAFE
390  */
391 int
392 sys_linux_vfork(struct linux_vfork_args *args)
393 {
394         int error;
395
396 #ifdef DEBUG
397         if (ldebug(vfork))
398                 kprintf(ARGS(vfork, ""));
399 #endif
400
401         if ((error = sys_vfork((struct vfork_args *)args)) != 0)
402                 return (error);
403         /* Are we the child? */
404         if (args->sysmsg_iresult == 1)
405                 args->sysmsg_iresult = 0;
406         return (0);
407 }
408
409 #define CLONE_VM        0x100
410 #define CLONE_FS        0x200
411 #define CLONE_FILES     0x400
412 #define CLONE_SIGHAND   0x800
413 #define CLONE_PID       0x1000
414
415 /*
416  * MPALMOSTSAFE
417  */
418 int
419 sys_linux_clone(struct linux_clone_args *args)
420 {
421         int error, ff = RFPROC;
422         struct proc *p2;
423         int exit_signal;
424         vm_offset_t start;
425         struct rfork_args rf_args;
426
427 #ifdef DEBUG
428         if (ldebug(clone)) {
429                 kprintf(ARGS(clone, "flags %x, stack %x"), 
430                     (unsigned int)args->flags, (unsigned int)args->stack);
431                 if (args->flags & CLONE_PID)
432                         kprintf(LMSG("CLONE_PID not yet supported"));
433         }
434 #endif
435
436         if (!args->stack)
437                 return (EINVAL);
438
439         exit_signal = args->flags & 0x000000ff;
440         if (exit_signal >= LINUX_NSIG)
441                 return (EINVAL);
442
443         if (exit_signal <= LINUX_SIGTBLSZ)
444                 exit_signal = linux_to_bsd_signal[_SIG_IDX(exit_signal)];
445
446         /* RFTHREAD probably not necessary here, but it shouldn't hurt */
447         ff |= RFTHREAD;
448
449         if (args->flags & CLONE_VM)
450                 ff |= RFMEM;
451         if (args->flags & CLONE_SIGHAND)
452                 ff |= RFSIGSHARE;
453         if (!(args->flags & CLONE_FILES))
454                 ff |= RFFDG;
455
456         error = 0;
457         start = 0;
458
459         rf_args.flags = ff;
460         rf_args.sysmsg_iresult = 0;
461         get_mplock();
462         if ((error = sys_rfork(&rf_args)) == 0) {
463                 args->sysmsg_iresult = rf_args.sysmsg_iresult;
464
465                 p2 = pfind(rf_args.sysmsg_iresult);
466                 if (p2 == NULL)
467                         error = ESRCH;
468         }
469         rel_mplock();
470         if (error == 0) {
471                 p2->p_sigparent = exit_signal;
472                 ONLY_LWP_IN_PROC(p2)->lwp_md.md_regs->tf_esp =
473                                                 (unsigned long)args->stack;
474
475 #ifdef DEBUG
476                 if (ldebug(clone))
477                         kprintf(LMSG("clone: successful rfork to %ld"),
478                             (long)p2->p_pid);
479 #endif
480         }
481         return (error);
482 }
483
484 /* XXX move */
485 struct l_mmap_argv {
486         l_caddr_t       addr;
487         l_int           len;
488         l_int           prot;
489         l_int           flags;
490         l_int           fd;
491         l_int           pos;
492 };
493
494 #define STACK_SIZE  (2 * 1024 * 1024)
495 #define GUARD_SIZE  (4 * PAGE_SIZE)
496
497 /*
498  * MPALMOSTSAFE
499  */
500 static int
501 linux_mmap_common(caddr_t linux_addr, size_t linux_len, int linux_prot,
502                   int linux_flags, int linux_fd, off_t pos, void **res)
503 {
504         struct thread *td = curthread;
505         struct proc *p = td->td_proc;
506         caddr_t addr;
507         void *new;
508         int error, flags, len, prot, fd;
509
510         flags = 0;
511         if (linux_flags & LINUX_MAP_SHARED)
512                 flags |= MAP_SHARED;
513         if (linux_flags & LINUX_MAP_PRIVATE)
514                 flags |= MAP_PRIVATE;
515         if (linux_flags & LINUX_MAP_FIXED)
516                 flags |= MAP_FIXED;
517         if (linux_flags & LINUX_MAP_ANON) {
518                 flags |= MAP_ANON;
519         } else {
520                 flags |= MAP_NOSYNC;
521         }
522
523         get_mplock();
524
525         if (linux_flags & LINUX_MAP_GROWSDOWN) {
526                 flags |= MAP_STACK;
527                 /* The linux MAP_GROWSDOWN option does not limit auto
528                  * growth of the region.  Linux mmap with this option
529                  * takes as addr the inital BOS, and as len, the initial
530                  * region size.  It can then grow down from addr without
531                  * limit.  However, linux threads has an implicit internal
532                  * limit to stack size of STACK_SIZE.  Its just not
533                  * enforced explicitly in linux.  But, here we impose
534                  * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
535                  * region, since we can do this with our mmap.
536                  *
537                  * Our mmap with MAP_STACK takes addr as the maximum
538                  * downsize limit on BOS, and as len the max size of
539                  * the region.  It them maps the top SGROWSIZ bytes,
540                  * and autgrows the region down, up to the limit
541                  * in addr.
542                  *
543                  * If we don't use the MAP_STACK option, the effect
544                  * of this code is to allocate a stack region of a
545                  * fixed size of (STACK_SIZE - GUARD_SIZE).
546                  */
547
548                 /* This gives us TOS */
549                 addr = linux_addr + linux_len;
550
551                 if (addr > p->p_vmspace->vm_maxsaddr) {
552                         /* Some linux apps will attempt to mmap
553                          * thread stacks near the top of their
554                          * address space.  If their TOS is greater
555                          * than vm_maxsaddr, vm_map_growstack()
556                          * will confuse the thread stack with the
557                          * process stack and deliver a SEGV if they
558                          * attempt to grow the thread stack past their
559                          * current stacksize rlimit.  To avoid this,
560                          * adjust vm_maxsaddr upwards to reflect
561                          * the current stacksize rlimit rather
562                          * than the maximum possible stacksize.
563                          * It would be better to adjust the
564                          * mmap'ed region, but some apps do not check
565                          * mmap's return value.
566                          */
567                         p->p_vmspace->vm_maxsaddr = (char *)USRSTACK -
568                             p->p_rlimit[RLIMIT_STACK].rlim_cur;
569                 }
570
571                 /* This gives us our maximum stack size */
572                 if (linux_len > STACK_SIZE - GUARD_SIZE) {
573                         len = linux_len;
574                 } else {
575                         len = STACK_SIZE - GUARD_SIZE;
576                 }
577                 /* This gives us a new BOS.  If we're using VM_STACK, then
578                  * mmap will just map the top SGROWSIZ bytes, and let
579                  * the stack grow down to the limit at BOS.  If we're
580                  * not using VM_STACK we map the full stack, since we
581                  * don't have a way to autogrow it.
582                  */
583                 addr -= len;
584         } else {
585                 addr = linux_addr;
586                 len = linux_len;
587         }
588
589         prot = linux_prot | PROT_READ;
590         if (linux_flags & LINUX_MAP_ANON) {
591                 fd = -1;
592         } else {
593                 fd = linux_fd;
594         }
595         
596 #ifdef DEBUG
597         if (ldebug(mmap) || ldebug(mmap2))
598                 kprintf("-> (%p, %d, %d, 0x%08x, %d, %lld)\n",
599                     addr, len, prot, flags, fd, pos);
600 #endif
601         error = kern_mmap(curproc->p_vmspace, addr, len,
602                           prot, flags, fd, pos, &new);
603         rel_mplock();
604
605         if (error == 0)
606                 *res = new;
607         return (error);
608 }
609
610 /*
611  * MPSAFE
612  */
613 int
614 sys_linux_mmap(struct linux_mmap_args *args)
615 {
616         struct l_mmap_argv linux_args;
617         int error;
618
619         error = copyin((caddr_t)args->ptr, &linux_args, sizeof(linux_args));
620         if (error)
621                 return (error);
622
623 #ifdef DEBUG
624         if (ldebug(mmap))
625                 kprintf(ARGS(mmap, "%p, %d, %d, 0x%08x, %d, %d"),
626                     (void *)linux_args.addr, linux_args.len, linux_args.prot,
627                     linux_args.flags, linux_args.fd, linux_args.pos);
628 #endif
629         error = linux_mmap_common(linux_args.addr, linux_args.len,
630             linux_args.prot, linux_args.flags, linux_args.fd,
631             linux_args.pos, &args->sysmsg_resultp);
632 #ifdef DEBUG
633         if (ldebug(mmap))
634                 kprintf("-> %p\n", args->sysmsg_resultp);
635 #endif
636         return(error);
637 }
638
639 /*
640  * MPSAFE
641  */
642 int
643 sys_linux_mmap2(struct linux_mmap2_args *args)
644 {
645         int error;
646
647 #ifdef DEBUG
648         if (ldebug(mmap2))
649                 kprintf(ARGS(mmap2, "%p, %d, %d, 0x%08x, %d, %d"),
650                     (void *)args->addr, args->len, args->prot, args->flags,
651                     args->fd, args->pgoff);
652 #endif
653         error = linux_mmap_common((void *)args->addr, args->len, args->prot,
654             args->flags, args->fd, args->pgoff * PAGE_SIZE,
655             &args->sysmsg_resultp);
656 #ifdef DEBUG
657         if (ldebug(mmap2))
658                 kprintf("-> %p\n", args->sysmsg_resultp);
659 #endif
660         return (error);
661 }
662
663 /*
664  * MPSAFE
665  */
666 int
667 sys_linux_pipe(struct linux_pipe_args *args)
668 {
669         int error;
670         int reg_edx;
671         struct pipe_args bsd_args;
672
673 #ifdef DEBUG
674         if (ldebug(pipe))
675                 kprintf(ARGS(pipe, "*"));
676 #endif
677
678         reg_edx = args->sysmsg_fds[1];
679         error = sys_pipe(&bsd_args);
680         if (error) {
681                 args->sysmsg_fds[1] = reg_edx;
682                 return (error);
683         }
684
685         error = copyout(bsd_args.sysmsg_fds, args->pipefds, 2*sizeof(int));
686         if (error) {
687                 args->sysmsg_fds[1] = reg_edx;
688                 return (error);
689         }
690
691         args->sysmsg_fds[1] = reg_edx;
692         args->sysmsg_fds[0] = 0;
693         return (0);
694 }
695
696 /*
697  * MPSAFE
698  */
699 int
700 sys_linux_ioperm(struct linux_ioperm_args *args)
701 {
702         struct sysarch_args sa;
703         struct i386_ioperm_args *iia;
704         caddr_t sg;
705         int error;
706
707         sg = stackgap_init();
708         iia = stackgap_alloc(&sg, sizeof(struct i386_ioperm_args));
709         iia->start = args->start;
710         iia->length = args->length;
711         iia->enable = args->enable;
712         sa.sysmsg_resultp = NULL;
713         sa.op = I386_SET_IOPERM;
714         sa.parms = (char *)iia;
715         error = sys_sysarch(&sa);
716         args->sysmsg_resultp = sa.sysmsg_resultp;
717         return(error);
718 }
719
720 /*
721  * MPSAFE
722  */
723 int
724 sys_linux_iopl(struct linux_iopl_args *args)
725 {
726         struct thread *td = curthread;
727         struct lwp *lp = td->td_lwp;
728         int error;
729
730         if (args->level < 0 || args->level > 3)
731                 return (EINVAL);
732         if ((error = priv_check(td, PRIV_ROOT)) != 0)
733                 return (error);
734         if (securelevel > 0)
735                 return (EPERM);
736         lp->lwp_md.md_regs->tf_eflags =
737             (lp->lwp_md.md_regs->tf_eflags & ~PSL_IOPL) |
738             (args->level * (PSL_IOPL / 3));
739         return (0);
740 }
741
742 /*
743  * MPSAFE
744  */
745 int
746 sys_linux_modify_ldt(struct linux_modify_ldt_args *uap)
747 {
748         int error;
749         caddr_t sg;
750         struct sysarch_args args;
751         struct i386_ldt_args *ldt;
752         struct l_descriptor ld;
753         union descriptor *desc;
754
755         sg = stackgap_init();
756
757         if (uap->ptr == NULL)
758                 return (EINVAL);
759
760         switch (uap->func) {
761         case 0x00: /* read_ldt */
762                 ldt = stackgap_alloc(&sg, sizeof(*ldt));
763                 ldt->start = 0;
764                 ldt->descs = uap->ptr;
765                 ldt->num = uap->bytecount / sizeof(union descriptor);
766                 args.op = I386_GET_LDT;
767                 args.parms = (char*)ldt;
768                 args.sysmsg_iresult = 0;
769                 error = sys_sysarch(&args);
770                 uap->sysmsg_iresult = args.sysmsg_iresult *
771                                       sizeof(union descriptor);
772                 break;
773         case 0x01: /* write_ldt */
774         case 0x11: /* write_ldt */
775                 if (uap->bytecount != sizeof(ld))
776                         return (EINVAL);
777
778                 error = copyin(uap->ptr, &ld, sizeof(ld));
779                 if (error)
780                         return (error);
781
782                 ldt = stackgap_alloc(&sg, sizeof(*ldt));
783                 desc = stackgap_alloc(&sg, sizeof(*desc));
784                 ldt->start = ld.entry_number;
785                 ldt->descs = desc;
786                 ldt->num = 1;
787                 desc->sd.sd_lolimit = (ld.limit & 0x0000ffff);
788                 desc->sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
789                 desc->sd.sd_lobase = (ld.base_addr & 0x00ffffff);
790                 desc->sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
791                 desc->sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
792                         (ld.contents << 2);
793                 desc->sd.sd_dpl = 3;
794                 desc->sd.sd_p = (ld.seg_not_present ^ 1);
795                 desc->sd.sd_xx = 0;
796                 desc->sd.sd_def32 = ld.seg_32bit;
797                 desc->sd.sd_gran = ld.limit_in_pages;
798                 args.op = I386_SET_LDT;
799                 args.parms = (char*)ldt;
800                 args.sysmsg_iresult = 0;
801                 error = sys_sysarch(&args);
802                 uap->sysmsg_iresult = args.sysmsg_iresult;
803                 break;
804         default:
805                 error = EINVAL;
806                 break;
807         }
808
809         return (error);
810 }
811
812 /*
813  * MPALMOSTSAFE
814  */
815 int
816 sys_linux_sigaction(struct linux_sigaction_args *args)
817 {
818         l_osigaction_t osa;
819         l_sigaction_t linux_act, linux_oact;
820         struct sigaction act, oact;
821         int error;
822
823 #ifdef DEBUG
824         if (ldebug(sigaction))
825                 kprintf(ARGS(sigaction, "%d, %p, %p"),
826                     args->sig, (void *)args->nsa, (void *)args->osa);
827 #endif
828
829         if (args->nsa) {
830                 error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
831                 if (error)
832                         return (error);
833                 linux_act.lsa_handler = osa.lsa_handler;
834                 linux_act.lsa_flags = osa.lsa_flags;
835                 linux_act.lsa_restorer = osa.lsa_restorer;
836                 LINUX_SIGEMPTYSET(linux_act.lsa_mask);
837                 linux_act.lsa_mask.__bits[0] = osa.lsa_mask;
838                 linux_to_bsd_sigaction(&linux_act, &act);
839         }
840
841         get_mplock();
842         error = kern_sigaction(args->sig, args->nsa ? &act : NULL,
843                                args->osa ? &oact : NULL);
844         rel_mplock();
845
846         if (args->osa != NULL && !error) {
847                 bsd_to_linux_sigaction(&oact, &linux_oact);
848                 osa.lsa_handler = linux_oact.lsa_handler;
849                 osa.lsa_flags = linux_oact.lsa_flags;
850                 osa.lsa_restorer = linux_oact.lsa_restorer;
851                 osa.lsa_mask = linux_oact.lsa_mask.__bits[0];
852                 error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
853         }
854         return (error);
855 }
856
857 /*
858  * Linux has two extra args, restart and oldmask.  We dont use these,
859  * but it seems that "restart" is actually a context pointer that
860  * enables the signal to happen with a different register set.
861  *
862  * MPALMOSTSAFE
863  */
864 int
865 sys_linux_sigsuspend(struct linux_sigsuspend_args *args)
866 {
867         l_sigset_t linux_mask;
868         sigset_t mask;
869         int error;
870
871 #ifdef DEBUG
872         if (ldebug(sigsuspend))
873                 kprintf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
874 #endif
875
876         LINUX_SIGEMPTYSET(mask);
877         mask.__bits[0] = args->mask;
878         linux_to_bsd_sigset(&linux_mask, &mask);
879
880         get_mplock();
881         error = kern_sigsuspend(&mask);
882         rel_mplock();
883
884         return(error);
885 }
886
887 /*
888  * MPALMOSTSAFE
889  */
890 int
891 sys_linux_rt_sigsuspend(struct linux_rt_sigsuspend_args *uap)
892 {
893         l_sigset_t linux_mask;
894         sigset_t mask;
895         int error;
896
897 #ifdef DEBUG
898         if (ldebug(rt_sigsuspend))
899                 kprintf(ARGS(rt_sigsuspend, "%p, %d"),
900                     (void *)uap->newset, uap->sigsetsize);
901 #endif
902
903         if (uap->sigsetsize != sizeof(l_sigset_t))
904                 return (EINVAL);
905
906         error = copyin(uap->newset, &linux_mask, sizeof(l_sigset_t));
907         if (error)
908                 return (error);
909
910         linux_to_bsd_sigset(&linux_mask, &mask);
911
912         get_mplock();
913         error = kern_sigsuspend(&mask);
914         rel_mplock();
915
916         return(error);
917 }
918
919 /*
920  * MPALMOSTSAFE
921  */
922 int
923 sys_linux_pause(struct linux_pause_args *args)
924 {
925         struct thread *td = curthread;
926         struct lwp *lp = td->td_lwp;
927         sigset_t mask;
928         int error;
929
930 #ifdef DEBUG
931         if (ldebug(pause))
932                 kprintf(ARGS(pause, ""));
933 #endif
934
935         mask = lp->lwp_sigmask;
936
937         get_mplock();
938         error = kern_sigsuspend(&mask);
939         rel_mplock();
940
941         return(error);
942 }
943
944 /*
945  * MPALMOSTSAFE
946  */
947 int
948 sys_linux_sigaltstack(struct linux_sigaltstack_args *uap)
949 {
950         stack_t ss, oss;
951         l_stack_t linux_ss;
952         int error;
953
954 #ifdef DEBUG
955         if (ldebug(sigaltstack))
956                 kprintf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
957 #endif
958
959         if (uap->uss) {
960                 error = copyin(uap->uss, &linux_ss, sizeof(l_stack_t));
961                 if (error)
962                         return (error);
963
964                 ss.ss_sp = linux_ss.ss_sp;
965                 ss.ss_size = linux_ss.ss_size;
966                 ss.ss_flags = linux_to_bsd_sigaltstack(linux_ss.ss_flags);
967         }
968
969         get_mplock();
970         error = kern_sigaltstack(uap->uss ? &ss : NULL,
971                                  uap->uoss ? &oss : NULL);
972         rel_mplock();
973
974         if (error == 0 && uap->uoss) {
975                 linux_ss.ss_sp = oss.ss_sp;
976                 linux_ss.ss_size = oss.ss_size;
977                 linux_ss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
978                 error = copyout(&linux_ss, uap->uoss, sizeof(l_stack_t));
979         }
980
981         return (error);
982 }