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