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