Merge from vendor branch CVS:
[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.15 2004/11/12 00:09:19 dillon 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 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                 printf(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 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 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                 printf(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 = linux_select(&newsel);
321         args->sysmsg_result = newsel.sysmsg_result;
322         return(error);
323 }
324
325 int
326 linux_fork(struct linux_fork_args *args)
327 {
328         int error;
329
330 #ifdef DEBUG
331         if (ldebug(fork))
332                 printf(ARGS(fork, ""));
333 #endif
334
335         if ((error = 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 linux_vfork(struct linux_vfork_args *args)
345 {
346         int error;
347
348 #ifdef DEBUG
349         if (ldebug(vfork))
350                 printf(ARGS(vfork, ""));
351 #endif
352
353         if ((error = vfork((struct vfork_args *)args)) != 0)
354                 return (error);
355         /* Are we the child? */
356         if (args->sysmsg_result == 1)
357                 args->sysmsg_result = 0;
358         return (0);
359 }
360
361 #define CLONE_VM        0x100
362 #define CLONE_FS        0x200
363 #define CLONE_FILES     0x400
364 #define CLONE_SIGHAND   0x800
365 #define CLONE_PID       0x1000
366
367 int
368 linux_clone(struct linux_clone_args *args)
369 {
370         int error, ff = RFPROC;
371         struct proc *p2;
372         int exit_signal;
373         vm_offset_t start;
374         struct rfork_args rf_args;
375
376 #ifdef DEBUG
377         if (ldebug(clone)) {
378                 printf(ARGS(clone, "flags %x, stack %x"), 
379                     (unsigned int)args->flags, (unsigned int)args->stack);
380                 if (args->flags & CLONE_PID)
381                         printf(LMSG("CLONE_PID not yet supported"));
382         }
383 #endif
384
385         if (!args->stack)
386                 return (EINVAL);
387
388         exit_signal = args->flags & 0x000000ff;
389         if (exit_signal >= LINUX_NSIG)
390                 return (EINVAL);
391
392         if (exit_signal <= LINUX_SIGTBLSZ)
393                 exit_signal = linux_to_bsd_signal[_SIG_IDX(exit_signal)];
394
395         /* RFTHREAD probably not necessary here, but it shouldn't hurt */
396         ff |= RFTHREAD;
397
398         if (args->flags & CLONE_VM)
399                 ff |= RFMEM;
400         if (args->flags & CLONE_SIGHAND)
401                 ff |= RFSIGSHARE;
402         if (!(args->flags & CLONE_FILES))
403                 ff |= RFFDG;
404
405         error = 0;
406         start = 0;
407
408         rf_args.flags = ff;
409         rf_args.sysmsg_result = 0;
410         if ((error = rfork(&rf_args)) != 0)
411                 return (error);
412         args->sysmsg_result = rf_args.sysmsg_result;
413
414         p2 = pfind(rf_args.sysmsg_result);
415         if (p2 == NULL)
416                 return (ESRCH);
417
418         p2->p_sigparent = exit_signal;
419         p2->p_md.md_regs->tf_esp = (unsigned int)args->stack;
420
421 #ifdef DEBUG
422         if (ldebug(clone))
423                 printf(LMSG("clone: successful rfork to %ld"),
424                     (long)p2->p_pid);
425 #endif
426
427         return (0);
428 }
429
430 /* XXX move */
431 struct l_mmap_argv {
432         l_caddr_t       addr;
433         l_int           len;
434         l_int           prot;
435         l_int           flags;
436         l_int           fd;
437         l_int           pos;
438 };
439
440 #define STACK_SIZE  (2 * 1024 * 1024)
441 #define GUARD_SIZE  (4 * PAGE_SIZE)
442
443 static int
444 linux_mmap_common(caddr_t linux_addr, size_t linux_len, int linux_prot,
445     int linux_flags, int linux_fd, off_t pos, void **res)
446 {
447         struct thread *td = curthread;
448         struct proc *p = td->td_proc;
449         caddr_t addr;
450         void *new;
451         int error, flags, len, prot, fd;
452
453         flags = 0;
454         if (linux_flags & LINUX_MAP_SHARED)
455                 flags |= MAP_SHARED;
456         if (linux_flags & LINUX_MAP_PRIVATE)
457                 flags |= MAP_PRIVATE;
458         if (linux_flags & LINUX_MAP_FIXED)
459                 flags |= MAP_FIXED;
460         if (linux_flags & LINUX_MAP_ANON) {
461                 flags |= MAP_ANON;
462         } else {
463                 flags |= MAP_NOSYNC;
464         }
465         if (linux_flags & LINUX_MAP_GROWSDOWN) {
466                 flags |= MAP_STACK;
467                 /* The linux MAP_GROWSDOWN option does not limit auto
468                  * growth of the region.  Linux mmap with this option
469                  * takes as addr the inital BOS, and as len, the initial
470                  * region size.  It can then grow down from addr without
471                  * limit.  However, linux threads has an implicit internal
472                  * limit to stack size of STACK_SIZE.  Its just not
473                  * enforced explicitly in linux.  But, here we impose
474                  * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
475                  * region, since we can do this with our mmap.
476                  *
477                  * Our mmap with MAP_STACK takes addr as the maximum
478                  * downsize limit on BOS, and as len the max size of
479                  * the region.  It them maps the top SGROWSIZ bytes,
480                  * and autgrows the region down, up to the limit
481                  * in addr.
482                  *
483                  * If we don't use the MAP_STACK option, the effect
484                  * of this code is to allocate a stack region of a
485                  * fixed size of (STACK_SIZE - GUARD_SIZE).
486                  */
487
488                 /* This gives us TOS */
489                 addr = linux_addr + linux_len;
490
491                 if (addr > p->p_vmspace->vm_maxsaddr) {
492                         /* Some linux apps will attempt to mmap
493                          * thread stacks near the top of their
494                          * address space.  If their TOS is greater
495                          * than vm_maxsaddr, vm_map_growstack()
496                          * will confuse the thread stack with the
497                          * process stack and deliver a SEGV if they
498                          * attempt to grow the thread stack past their
499                          * current stacksize rlimit.  To avoid this,
500                          * adjust vm_maxsaddr upwards to reflect
501                          * the current stacksize rlimit rather
502                          * than the maximum possible stacksize.
503                          * It would be better to adjust the
504                          * mmap'ed region, but some apps do not check
505                          * mmap's return value.
506                          */
507                         p->p_vmspace->vm_maxsaddr = (char *)USRSTACK -
508                             p->p_rlimit[RLIMIT_STACK].rlim_cur;
509                 }
510
511                 /* This gives us our maximum stack size */
512                 if (linux_len > STACK_SIZE - GUARD_SIZE) {
513                         len = linux_len;
514                 } else {
515                         len = STACK_SIZE - GUARD_SIZE;
516                 }
517                 /* This gives us a new BOS.  If we're using VM_STACK, then
518                  * mmap will just map the top SGROWSIZ bytes, and let
519                  * the stack grow down to the limit at BOS.  If we're
520                  * not using VM_STACK we map the full stack, since we
521                  * don't have a way to autogrow it.
522                  */
523                 addr -= len;
524         } else {
525                 addr = linux_addr;
526                 len = linux_len;
527         }
528
529         prot = linux_prot | PROT_READ;
530         if (linux_flags & LINUX_MAP_ANON) {
531                 fd = -1;
532         } else {
533                 fd = linux_fd;
534         }
535         
536 #ifdef DEBUG
537         if (ldebug(mmap) || ldebug(mmap2))
538                 printf("-> (%p, %d, %d, 0x%08x, %d, %lld)\n",
539                     addr, len, prot, flags, fd, pos);
540 #endif
541         error = kern_mmap(addr, len, prot, flags, fd, pos, &new);
542
543         if (error == 0)
544                 *res = new;
545         return (error);
546 }
547
548 int
549 linux_mmap(struct linux_mmap_args *args)
550 {
551         struct l_mmap_argv linux_args;
552         int error;
553
554         error = copyin((caddr_t)args->ptr, &linux_args, sizeof(linux_args));
555         if (error)
556                 return (error);
557
558 #ifdef DEBUG
559         if (ldebug(mmap))
560                 printf(ARGS(mmap, "%p, %d, %d, 0x%08x, %d, %d"),
561                     (void *)linux_args.addr, linux_args.len, linux_args.prot,
562                     linux_args.flags, linux_args.fd, linux_args.pos);
563 #endif
564         error = linux_mmap_common(linux_args.addr, linux_args.len,
565             linux_args.prot, linux_args.flags, linux_args.fd,
566             linux_args.pos, &args->sysmsg_resultp);
567 #ifdef DEBUG
568         if (ldebug(mmap))
569                 printf("-> %p\n", args->sysmsg_resultp);
570 #endif
571         return(error);
572 }
573
574 int
575 linux_mmap2(struct linux_mmap2_args *args)
576 {
577         int error;
578
579 #ifdef DEBUG
580         if (ldebug(mmap2))
581                 printf(ARGS(mmap2, "%p, %d, %d, 0x%08x, %d, %d"),
582                     (void *)args->addr, args->len, args->prot, args->flags,
583                     args->fd, args->pgoff);
584 #endif
585         error = linux_mmap_common((void *)args->addr, args->len, args->prot,
586             args->flags, args->fd, args->pgoff * PAGE_SIZE,
587             &args->sysmsg_resultp);
588 #ifdef DEBUG
589         if (ldebug(mmap2))
590                 printf("-> %p\n", args->sysmsg_resultp);
591 #endif
592         return (error);
593 }
594
595 int
596 linux_pipe(struct linux_pipe_args *args)
597 {
598         int error;
599         int reg_edx;
600         struct pipe_args bsd_args;
601
602 #ifdef DEBUG
603         if (ldebug(pipe))
604                 printf(ARGS(pipe, "*"));
605 #endif
606
607         reg_edx = args->sysmsg_fds[1];
608         error = pipe(&bsd_args);
609         if (error) {
610                 args->sysmsg_fds[1] = reg_edx;
611                 return (error);
612         }
613
614         error = copyout(bsd_args.sysmsg_fds, args->pipefds, 2*sizeof(int));
615         if (error) {
616                 args->sysmsg_fds[1] = reg_edx;
617                 return (error);
618         }
619
620         args->sysmsg_fds[1] = reg_edx;
621         args->sysmsg_fds[0] = 0;
622         return (0);
623 }
624
625 int
626 linux_ioperm(struct linux_ioperm_args *args)
627 {
628         struct sysarch_args sa;
629         struct i386_ioperm_args *iia;
630         caddr_t sg;
631         int error;
632
633         sg = stackgap_init();
634         iia = stackgap_alloc(&sg, sizeof(struct i386_ioperm_args));
635         iia->start = args->start;
636         iia->length = args->length;
637         iia->enable = args->enable;
638         sa.sysmsg_resultp = NULL;
639         sa.op = I386_SET_IOPERM;
640         sa.parms = (char *)iia;
641         error = sysarch(&sa);
642         args->sysmsg_resultp = sa.sysmsg_resultp;
643         return(error);
644 }
645
646 int
647 linux_iopl(struct linux_iopl_args *args)
648 {
649         struct thread *td = curthread;
650         struct proc *p = td->td_proc;
651         int error;
652
653         KKASSERT(p);
654
655         if (args->level < 0 || args->level > 3)
656                 return (EINVAL);
657         if ((error = suser(td)) != 0)
658                 return (error);
659         if (securelevel > 0)
660                 return (EPERM);
661         p->p_md.md_regs->tf_eflags = (p->p_md.md_regs->tf_eflags & ~PSL_IOPL) |
662             (args->level * (PSL_IOPL / 3));
663         return (0);
664 }
665
666 int
667 linux_modify_ldt(struct linux_modify_ldt_args *uap)
668 {
669         int error;
670         caddr_t sg;
671         struct sysarch_args args;
672         struct i386_ldt_args *ldt;
673         struct l_descriptor ld;
674         union descriptor *desc;
675
676         sg = stackgap_init();
677
678         if (uap->ptr == NULL)
679                 return (EINVAL);
680
681         switch (uap->func) {
682         case 0x00: /* read_ldt */
683                 ldt = stackgap_alloc(&sg, sizeof(*ldt));
684                 ldt->start = 0;
685                 ldt->descs = uap->ptr;
686                 ldt->num = uap->bytecount / sizeof(union descriptor);
687                 args.op = I386_GET_LDT;
688                 args.parms = (char*)ldt;
689                 args.sysmsg_result = 0;
690                 error = sysarch(&args);
691                 uap->sysmsg_result = args.sysmsg_result *
692                                             sizeof(union descriptor);
693                 break;
694         case 0x01: /* write_ldt */
695         case 0x11: /* write_ldt */
696                 if (uap->bytecount != sizeof(ld))
697                         return (EINVAL);
698
699                 error = copyin(uap->ptr, &ld, sizeof(ld));
700                 if (error)
701                         return (error);
702
703                 ldt = stackgap_alloc(&sg, sizeof(*ldt));
704                 desc = stackgap_alloc(&sg, sizeof(*desc));
705                 ldt->start = ld.entry_number;
706                 ldt->descs = desc;
707                 ldt->num = 1;
708                 desc->sd.sd_lolimit = (ld.limit & 0x0000ffff);
709                 desc->sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
710                 desc->sd.sd_lobase = (ld.base_addr & 0x00ffffff);
711                 desc->sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
712                 desc->sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
713                         (ld.contents << 2);
714                 desc->sd.sd_dpl = 3;
715                 desc->sd.sd_p = (ld.seg_not_present ^ 1);
716                 desc->sd.sd_xx = 0;
717                 desc->sd.sd_def32 = ld.seg_32bit;
718                 desc->sd.sd_gran = ld.limit_in_pages;
719                 args.op = I386_SET_LDT;
720                 args.parms = (char*)ldt;
721                 args.sysmsg_result = 0;
722                 error = sysarch(&args);
723                 uap->sysmsg_result = args.sysmsg_result;
724                 break;
725         default:
726                 error = EINVAL;
727                 break;
728         }
729
730         return (error);
731 }
732
733 int
734 linux_sigaction(struct linux_sigaction_args *args)
735 {
736         l_osigaction_t osa;
737         l_sigaction_t linux_act, linux_oact;
738         struct sigaction act, oact;
739         int error;
740
741 #ifdef DEBUG
742         if (ldebug(sigaction))
743                 printf(ARGS(sigaction, "%d, %p, %p"),
744                     args->sig, (void *)args->nsa, (void *)args->osa);
745 #endif
746
747         if (args->nsa) {
748                 error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
749                 if (error)
750                         return (error);
751                 linux_act.lsa_handler = osa.lsa_handler;
752                 linux_act.lsa_flags = osa.lsa_flags;
753                 linux_act.lsa_restorer = osa.lsa_restorer;
754                 LINUX_SIGEMPTYSET(linux_act.lsa_mask);
755                 linux_act.lsa_mask.__bits[0] = osa.lsa_mask;
756                 linux_to_bsd_sigaction(&linux_act, &act);
757         }
758
759         error = kern_sigaction(args->sig, args->nsa ? &act : NULL,
760             args->osa ? &oact : NULL);
761
762         if (args->osa != NULL && !error) {
763                 bsd_to_linux_sigaction(&oact, &linux_oact);
764                 osa.lsa_handler = linux_oact.lsa_handler;
765                 osa.lsa_flags = linux_oact.lsa_flags;
766                 osa.lsa_restorer = linux_oact.lsa_restorer;
767                 osa.lsa_mask = linux_oact.lsa_mask.__bits[0];
768                 error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
769         }
770         return (error);
771 }
772
773 /*
774  * Linux has two extra args, restart and oldmask.  We dont use these,
775  * but it seems that "restart" is actually a context pointer that
776  * enables the signal to happen with a different register set.
777  */
778 int
779 linux_sigsuspend(struct linux_sigsuspend_args *args)
780 {
781         l_sigset_t linux_mask;
782         sigset_t mask;
783         int error;
784
785 #ifdef DEBUG
786         if (ldebug(sigsuspend))
787                 printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
788 #endif
789
790         LINUX_SIGEMPTYSET(mask);
791         mask.__bits[0] = args->mask;
792         linux_to_bsd_sigset(&linux_mask, &mask);
793
794         error = kern_sigsuspend(&mask);
795
796         return(error);
797 }
798
799 int
800 linux_rt_sigsuspend(struct linux_rt_sigsuspend_args *uap)
801 {
802         l_sigset_t linux_mask;
803         sigset_t mask;
804         int error;
805
806 #ifdef DEBUG
807         if (ldebug(rt_sigsuspend))
808                 printf(ARGS(rt_sigsuspend, "%p, %d"),
809                     (void *)uap->newset, uap->sigsetsize);
810 #endif
811
812         if (uap->sigsetsize != sizeof(l_sigset_t))
813                 return (EINVAL);
814
815         error = copyin(uap->newset, &linux_mask, sizeof(l_sigset_t));
816         if (error)
817                 return (error);
818
819         linux_to_bsd_sigset(&linux_mask, &mask);
820
821         error = kern_sigsuspend(&mask);
822
823         return(error);
824 }
825
826 int
827 linux_pause(struct linux_pause_args *args)
828 {
829         struct thread *td = curthread;
830         struct proc *p = td->td_proc;
831         sigset_t mask;
832         int error;
833
834 #ifdef DEBUG
835         if (ldebug(pause))
836                 printf(ARGS(pause, ""));
837 #endif
838
839         mask = p->p_sigmask;
840
841         error = kern_sigsuspend(&mask);
842
843         return(error);
844 }
845
846 int
847 linux_sigaltstack(struct linux_sigaltstack_args *uap)
848 {
849         stack_t ss, oss;
850         l_stack_t linux_ss;
851         int error;
852
853 #ifdef DEBUG
854         if (ldebug(sigaltstack))
855                 printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
856 #endif
857
858         if (uap->uss) {
859                 error = copyin(uap->uss, &linux_ss, sizeof(l_stack_t));
860                 if (error)
861                         return (error);
862
863                 ss.ss_sp = linux_ss.ss_sp;
864                 ss.ss_size = linux_ss.ss_size;
865                 ss.ss_flags = linux_to_bsd_sigaltstack(linux_ss.ss_flags);
866         }
867
868         error = kern_sigaltstack(uap->uss ? &ss : NULL,
869             uap->uoss ? &oss : NULL);
870
871         if (error == 0 && uap->uoss) {
872                 linux_ss.ss_sp = oss.ss_sp;
873                 linux_ss.ss_size = oss.ss_size;
874                 linux_ss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
875                 error = copyout(&linux_ss, uap->uoss, sizeof(l_stack_t));
876         }
877
878         return (error);
879 }