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