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