2 * Copyright (c) 1994-1995 Søren Schmidt
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 withough specific prior written permission
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.
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/sysproto.h>
35 #include <sys/signalvar.h>
37 #include <i386/linux/linux.h>
38 #include <i386/linux/linux_proto.h>
39 #include <i386/linux/linux_util.h>
42 linux_to_bsd_sigset(linux_sigset_t *lss, sigset_t *bss)
47 bss->__bits[0] = lss->__bits[0] & ~((1U << LINUX_SIGTBLSZ) - 1);
48 bss->__bits[1] = lss->__bits[1];
49 for (l = 1; l <= LINUX_SIGTBLSZ; l++) {
50 if (LINUX_SIGISMEMBER(*lss, l)) {
51 b = linux_to_bsd_signal[_SIG_IDX(l)];
59 bsd_to_linux_sigset(sigset_t *bss, linux_sigset_t *lss)
63 LINUX_SIGEMPTYSET(*lss);
64 lss->__bits[0] = bss->__bits[0] & ~((1U << LINUX_SIGTBLSZ) - 1);
65 lss->__bits[1] = bss->__bits[1];
66 for (b = 1; b <= LINUX_SIGTBLSZ; b++) {
67 if (SIGISMEMBER(*bss, b)) {
68 l = bsd_to_linux_signal[_SIG_IDX(b)];
70 LINUX_SIGADDSET(*lss, l);
76 linux_to_bsd_sigaction(linux_sigaction_t *lsa, struct sigaction *bsa)
79 linux_to_bsd_sigset(&lsa->lsa_mask, &bsa->sa_mask);
80 bsa->sa_handler = lsa->lsa_handler;
82 if (lsa->lsa_flags & LINUX_SA_NOCLDSTOP)
83 bsa->sa_flags |= SA_NOCLDSTOP;
84 if (lsa->lsa_flags & LINUX_SA_NOCLDWAIT)
85 bsa->sa_flags |= SA_NOCLDWAIT;
86 if (lsa->lsa_flags & LINUX_SA_SIGINFO)
87 bsa->sa_flags |= SA_SIGINFO;
88 if (lsa->lsa_flags & LINUX_SA_ONSTACK)
89 bsa->sa_flags |= SA_ONSTACK;
90 if (lsa->lsa_flags & LINUX_SA_RESTART)
91 bsa->sa_flags |= SA_RESTART;
92 if (lsa->lsa_flags & LINUX_SA_ONESHOT)
93 bsa->sa_flags |= SA_RESETHAND;
94 if (lsa->lsa_flags & LINUX_SA_NOMASK)
95 bsa->sa_flags |= SA_NODEFER;
99 bsd_to_linux_sigaction(struct sigaction *bsa, linux_sigaction_t *lsa)
102 bsd_to_linux_sigset(&bsa->sa_mask, &lsa->lsa_mask);
103 lsa->lsa_handler = bsa->sa_handler;
104 lsa->lsa_restorer = NULL; /* unsupported */
106 if (bsa->sa_flags & SA_NOCLDSTOP)
107 lsa->lsa_flags |= LINUX_SA_NOCLDSTOP;
108 if (bsa->sa_flags & SA_NOCLDWAIT)
109 lsa->lsa_flags |= LINUX_SA_NOCLDWAIT;
110 if (bsa->sa_flags & SA_SIGINFO)
111 lsa->lsa_flags |= LINUX_SA_SIGINFO;
112 if (bsa->sa_flags & SA_ONSTACK)
113 lsa->lsa_flags |= LINUX_SA_ONSTACK;
114 if (bsa->sa_flags & SA_RESTART)
115 lsa->lsa_flags |= LINUX_SA_RESTART;
116 if (bsa->sa_flags & SA_RESETHAND)
117 lsa->lsa_flags |= LINUX_SA_ONESHOT;
118 if (bsa->sa_flags & SA_NODEFER)
119 lsa->lsa_flags |= LINUX_SA_NOMASK;
123 linux_do_sigaction(struct proc *p, int linux_sig, linux_sigaction_t *linux_nsa,
124 linux_sigaction_t *linux_osa)
126 struct sigaction *nsa, *osa, sa;
127 struct sigaction_args sa_args;
129 caddr_t sg = stackgap_init();
131 if (linux_sig <= 0 || linux_sig > LINUX_NSIG)
134 if (linux_osa != NULL)
135 osa = stackgap_alloc(&sg, sizeof(struct sigaction));
139 if (linux_nsa != NULL) {
140 nsa = stackgap_alloc(&sg, sizeof(struct sigaction));
141 linux_to_bsd_sigaction(linux_nsa, &sa);
142 error = copyout(&sa, nsa, sizeof(struct sigaction));
149 if (linux_sig <= LINUX_SIGTBLSZ)
150 sa_args.sig = linux_to_bsd_signal[_SIG_IDX(linux_sig)];
152 sa_args.sig = linux_sig;
156 error = sigaction(p, &sa_args);
160 if (linux_osa != NULL) {
161 error = copyin(osa, &sa, sizeof(struct sigaction));
164 bsd_to_linux_sigaction(&sa, linux_osa);
171 linux_sigaction(struct proc *p, struct linux_sigaction_args *args)
173 linux_osigaction_t osa;
174 linux_sigaction_t act, oact;
178 printf("Linux-emul(%ld): sigaction(%d, %p, %p)\n", (long)p->p_pid,
179 args->sig, (void *)args->nsa, (void *)args->osa);
182 if (args->nsa != NULL) {
183 error = copyin(args->nsa, &osa, sizeof(linux_osigaction_t));
186 act.lsa_handler = osa.lsa_handler;
187 act.lsa_flags = osa.lsa_flags;
188 act.lsa_restorer = osa.lsa_restorer;
189 LINUX_SIGEMPTYSET(act.lsa_mask);
190 act.lsa_mask.__bits[0] = osa.lsa_mask;
193 error = linux_do_sigaction(p, args->sig,
194 args->nsa ? &act : NULL,
195 args->osa ? &oact : NULL);
197 if (args->osa != NULL && !error) {
198 osa.lsa_handler = oact.lsa_handler;
199 osa.lsa_flags = oact.lsa_flags;
200 osa.lsa_restorer = oact.lsa_restorer;
201 osa.lsa_mask = oact.lsa_mask.__bits[0];
202 error = copyout(&osa, args->osa, sizeof(linux_osigaction_t));
209 linux_signal(struct proc *p, struct linux_signal_args *args)
211 linux_sigaction_t nsa, osa;
215 printf("Linux-emul(%ld): signal(%d, %p)\n",
216 (long)p->p_pid, args->sig, (void *)args->handler);
219 nsa.lsa_handler = args->handler;
220 nsa.lsa_flags = LINUX_SA_ONESHOT | LINUX_SA_NOMASK;
221 LINUX_SIGEMPTYSET(nsa.lsa_mask);
223 error = linux_do_sigaction(p, args->sig, &nsa, &osa);
224 p->p_retval[0] = (int)osa.lsa_handler;
230 linux_rt_sigaction(struct proc *p, struct linux_rt_sigaction_args *args)
232 linux_sigaction_t nsa, osa;
236 printf("Linux-emul(%ld): rt_sigaction(%d, %p, %p, %d)\n",
237 (long)p->p_pid, args->sig, (void *)args->act,
238 (void *)args->oact, args->sigsetsize);
241 if (args->sigsetsize != sizeof(linux_sigset_t))
244 if (args->act != NULL) {
245 error = copyin(args->act, &nsa, sizeof(linux_sigaction_t));
250 error = linux_do_sigaction(p, args->sig,
251 args->act ? &nsa : NULL,
252 args->oact ? &osa : NULL);
254 if (args->oact != NULL && !error) {
255 error = copyout(&osa, args->oact, sizeof(linux_sigaction_t));
262 linux_do_sigprocmask(struct proc *p, int how, linux_sigset_t *new,
272 bsd_to_linux_sigset(&p->p_sigmask, old);
275 linux_to_bsd_sigset(new, &mask);
280 case LINUX_SIG_BLOCK:
281 SIGSETOR(p->p_sigmask, mask);
282 SIG_CANTMASK(p->p_sigmask);
284 case LINUX_SIG_UNBLOCK:
285 SIGSETNAND(p->p_sigmask, mask);
287 case LINUX_SIG_SETMASK:
289 SIG_CANTMASK(p->p_sigmask);
303 linux_sigprocmask(struct proc *p, struct linux_sigprocmask_args *args)
305 linux_osigset_t mask;
306 linux_sigset_t set, oset;
310 printf("Linux-emul(%d): sigprocmask(%d, *, *)\n", p->p_pid, args->how);
313 if (args->mask != NULL) {
314 error = copyin(args->mask, &mask, sizeof(linux_osigset_t));
317 LINUX_SIGEMPTYSET(set);
318 set.__bits[0] = mask;
321 error = linux_do_sigprocmask(p, args->how,
322 args->mask ? &set : NULL,
323 args->omask ? &oset : NULL);
325 if (args->omask != NULL && !error) {
326 mask = oset.__bits[0];
327 error = copyout(&mask, args->omask, sizeof(linux_osigset_t));
334 linux_rt_sigprocmask(struct proc *p, struct linux_rt_sigprocmask_args *args)
336 linux_sigset_t set, oset;
340 printf("Linux-emul(%ld): rt_sigprocmask(%d, %p, %p, %d)\n",
341 (long)p->p_pid, args->how, (void *)args->mask,
342 (void *)args->omask, args->sigsetsize);
345 if (args->sigsetsize != sizeof(linux_sigset_t))
348 if (args->mask != NULL) {
349 error = copyin(args->mask, &set, sizeof(linux_sigset_t));
354 error = linux_do_sigprocmask(p, args->how,
355 args->mask ? &set : NULL,
356 args->omask ? &oset : NULL);
358 if (args->omask != NULL && !error) {
359 error = copyout(&oset, args->omask, sizeof(linux_sigset_t));
366 linux_siggetmask(struct proc *p, struct linux_siggetmask_args *args)
371 printf("Linux-emul(%d): siggetmask()\n", p->p_pid);
374 bsd_to_linux_sigset(&p->p_sigmask, &mask);
375 p->p_retval[0] = mask.__bits[0];
380 linux_sigsetmask(struct proc *p, struct linux_sigsetmask_args *args)
387 printf("Linux-emul(%ld): sigsetmask(%08lx)\n",
388 (long)p->p_pid, (unsigned long)args->mask);
391 bsd_to_linux_sigset(&p->p_sigmask, &lset);
392 p->p_retval[0] = lset.__bits[0];
393 LINUX_SIGEMPTYSET(lset);
394 lset.__bits[0] = args->mask;
395 linux_to_bsd_sigset(&lset, &bset);
398 SIG_CANTMASK(p->p_sigmask);
404 linux_sigpending(struct proc *p, struct linux_sigpending_args *args)
408 linux_osigset_t mask;
411 printf("Linux-emul(%d): sigpending(*)\n", p->p_pid);
415 SIGSETAND(bset, p->p_sigmask);
416 bsd_to_linux_sigset(&bset, &lset);
417 mask = lset.__bits[0];
418 return (copyout(&mask, args->mask, sizeof(mask)));
422 * Linux has two extra args, restart and oldmask. We dont use these,
423 * but it seems that "restart" is actually a context pointer that
424 * enables the signal to happen with a different register set.
427 linux_sigsuspend(struct proc *p, struct linux_sigsuspend_args *args)
429 struct sigsuspend_args bsd;
432 caddr_t sg = stackgap_init();
435 printf("Linux-emul(%ld): sigsuspend(%08lx)\n",
436 (long)p->p_pid, (unsigned long)args->mask);
439 sigmask = stackgap_alloc(&sg, sizeof(sigset_t));
440 LINUX_SIGEMPTYSET(mask);
441 mask.__bits[0] = args->mask;
442 linux_to_bsd_sigset(&mask, sigmask);
443 bsd.sigmask = sigmask;
444 return (sigsuspend(p, &bsd));
448 linux_rt_sigsuspend(p, uap)
450 struct linux_rt_sigsuspend_args *uap;
452 linux_sigset_t lmask;
454 struct sigsuspend_args bsd;
455 caddr_t sg = stackgap_init();
459 printf("Linux-emul(%ld): rt_sigsuspend(%p, %d)\n", (long)p->p_pid,
460 (void *)uap->newset, uap->sigsetsize);
463 if (uap->sigsetsize != sizeof(linux_sigset_t))
466 error = copyin(uap->newset, &lmask, sizeof(linux_sigset_t));
470 bmask = stackgap_alloc(&sg, sizeof(sigset_t));
471 linux_to_bsd_sigset(&lmask, bmask);
473 return (sigsuspend(p, &bsd));
477 linux_pause(struct proc *p, struct linux_pause_args *args)
479 struct sigsuspend_args bsd;
481 caddr_t sg = stackgap_init();
484 printf("Linux-emul(%d): pause()\n", p->p_pid);
487 sigmask = stackgap_alloc(&sg, sizeof(sigset_t));
488 *sigmask = p->p_sigmask;
489 bsd.sigmask = sigmask;
490 return sigsuspend(p, &bsd);
494 linux_kill(struct proc *p, struct linux_kill_args *args)
496 struct kill_args /* {
502 printf("Linux-emul(%d): kill(%d, %d)\n",
503 p->p_pid, args->pid, args->signum);
507 * Allow signal 0 as a means to check for privileges
509 if (args->signum < 0 || args->signum > LINUX_NSIG)
512 if (args->signum > 0 && args->signum <= LINUX_SIGTBLSZ)
513 tmp.signum = linux_to_bsd_signal[_SIG_IDX(args->signum)];
515 tmp.signum = args->signum;
518 return (kill(p, &tmp));