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