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