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