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