OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [uclinux/] [uClinux-2.0.x/] [arch/] [m68knommu/] [platform/] [68EZ328/] [signal.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
/*
2
 *  linux/arch/m68knommu/kernel/signal.c
3
 *
4
 *  Copyright (C) 1998  D. Jeff Dionne <jeff@ryeham.ee.ryerson.ca>,
5
 *                      Kenneth Albanowski <kjahds@kjahds.com>,
6
 *                      The Silver Hammer Group, Ltd.
7
 *
8
 *  Based on:
9
 *
10
 *  linux/arch/m68k/kernel/signal.c
11
 *
12
 *  Copyright (C) 1991, 1992  Linus Torvalds
13
 *
14
 * This file is subject to the terms and conditions of the GNU General Public
15
 * License.  See the file COPYING in the main directory of this archive
16
 * for more details.
17
 */
18
 
19
/*
20
 * Linux/m68k support by Hamish Macdonald
21
 *
22
 * 68000 and CPU32 support D. Jeff Dionne
23
 * 68060 fixes by Jesper Skov
24
 */
25
 
26
/*
27
 * ++roman (07/09/96): implemented signal stacks (specially for tosemu on
28
 * Atari :-) Current limitation: Only one sigstack can be active at one time.
29
 * If a second signal with SA_STACK set arrives while working on a sigstack,
30
 * SA_STACK is ignored. This behaviour avoids lots of trouble with nested
31
 * signal handlers!
32
 */
33
 
34
#include <linux/config.h>
35
 
36
#include <linux/sched.h>
37
#include <linux/mm.h>
38
#include <linux/kernel.h>
39
#include <linux/signal.h>
40
#include <linux/errno.h>
41
#include <linux/wait.h>
42
#include <linux/ptrace.h>
43
#include <linux/unistd.h>
44
 
45
#include <asm/setup.h>
46
#include <asm/segment.h>
47
#include <asm/pgtable.h>
48
#include <asm/traps.h>
49
 
50
/*
51
#define DEBUG
52
*/
53
 
54
#define offsetof(type, member)  ((size_t)(&((type *)0)->member))
55
 
56
#define _S(nr) (1<<((nr)-1))
57
 
58
#define _BLOCKABLE (~(_S(SIGKILL) | _S(SIGSTOP)))
59
 
60
asmlinkage int sys_waitpid(pid_t pid,unsigned long * stat_addr, int options);
61
asmlinkage int do_signal(unsigned long oldmask, struct pt_regs *regs);
62
 
63
/*
64
 * atomically swap in the new signal mask, and wait for a signal.
65
 */
66
asmlinkage int do_sigsuspend(struct pt_regs *regs)
67
{
68
        unsigned long oldmask = current->blocked;
69
        unsigned long newmask = regs->d3;
70
 
71
        current->blocked = newmask & _BLOCKABLE;
72
        regs->d0 = -EINTR;
73
        while (1) {
74
                current->state = TASK_INTERRUPTIBLE;
75
                schedule();
76
                if (do_signal(oldmask, regs))
77
                        return -EINTR;
78
        }
79
}
80
 
81
asmlinkage int do_sigreturn(unsigned long __unused)
82
{
83
        struct sigcontext_struct context;
84
        struct pt_regs *regs;
85
        struct switch_stack *sw;
86
        int fsize = 0;
87
        unsigned long fp;
88
        unsigned long usp = rdusp();
89
 
90
#ifdef DEBUG
91
        printk("sys_sigreturn, usp=%08x\n", (unsigned) usp);
92
#endif
93
 
94
        /* get stack frame pointer */
95
        sw = (struct switch_stack *) &__unused;
96
        regs = (struct pt_regs *) (sw + 1);
97
 
98
        /* get previous context (including pointer to possible extra junk) */
99
        if (verify_area(VERIFY_READ, (void *)usp, sizeof(context)))
100
                goto badframe;
101
 
102
        memcpy_fromfs(&context,(void *)usp, sizeof(context));
103
 
104
        fp = usp + sizeof (context);
105
 
106
        /* restore signal mask */
107
        current->blocked = context.sc_mask & _BLOCKABLE;
108
 
109
        /* restore passed registers */
110
        regs->d0 = context.sc_d0;
111
        regs->d1 = context.sc_d1;
112
        regs->a0 = context.sc_a0;
113
        regs->a1 = context.sc_a1;
114
        regs->sr = (regs->sr & 0xff00) | (context.sc_sr & 0xff);
115
        regs->pc = context.sc_pc;
116
        regs->orig_d0 = -1;             /* disable syscall checks */
117
        wrusp(context.sc_usp);
118
 
119
        if (context.sc_usp != fp+fsize) {
120
#ifdef DEBUG
121
                printk("Stack off by %d\n",context.sc_usp - (fp+fsize));
122
#endif
123
                current->flags &= ~PF_ONSIGSTK;
124
        }
125
 
126
        return regs->d0;
127
badframe:
128
        do_exit(SIGSEGV);
129
}
130
 
131
/*
132
 * Set up a signal frame...
133
 *
134
 * This routine is somewhat complicated by the fact that if the
135
 * kernel may be entered by an exception other than a system call;
136
 * e.g. a bus error or other "bad" exception.  If this is the case,
137
 * then *all* the context on the kernel stack frame must be saved.
138
 *
139
 * For a large number of exceptions, the stack frame format is the same
140
 * as that which will be created when the process traps back to the kernel
141
 * when finished executing the signal handler.  In this case, nothing
142
 * must be done.  This is exception frame format "0".  For exception frame
143
 * formats "2", "9", "A" and "B", the extra information on the frame must
144
 * be saved.  This information is saved on the user stack and restored
145
 * when the signal handler is returned.
146
 *
147
 * The format of the user stack when executing the signal handler is:
148
 *
149
 *     usp ->  RETADDR (points to code below)
150
 *             signum  (parm #1)
151
 *             sigcode (parm #2 ; vector number)
152
 *             scp     (parm #3 ; sigcontext pointer, pointer to #1 below)
153
 *             code1   (addaw #20,sp) ; pop parms and code off stack
154
 *             code2   (moveq #119,d0; trap #0) ; sigreturn syscall
155
 *     #1|     oldmask
156
 *       |     old usp
157
 *       |     d0      (first saved reg)
158
 *       |     d1
159
 *       |     a0
160
 *       |     a1
161
 *       |     sr      (saved status register)
162
 *       |     pc      (old pc; one to return to)
163
 * ----->|     forvec  (format and vector word of old supervisor stack frame)
164
 *       |     floating point context
165
 *
166
 * These are optionally followed by some extra stuff, depending on the
167
 * stack frame interrupted. This is 1 longword for format "2", 3
168
 * longwords for format "9", 6 longwords for format "A", and 21
169
 * longwords for format "B".
170
 *
171
 * everything after ------> is not present if the processor does not push a vector
172
 * format.  Only 68000 and the new 68000 modular core are like this
173
 */
174
 
175
#define UFRAME_SIZE(fs) (sizeof(struct sigcontext_struct)/4 + 6 + fs/4)
176
 
177
static void setup_frame (struct sigaction * sa, struct pt_regs *regs,
178
                         int signr, unsigned long oldmask)
179
{
180
        struct sigcontext_struct context;
181
        unsigned long *frame, *tframe;
182
#define fsize 0
183
 
184
        frame = (unsigned long *)rdusp();
185
        if (!(current->flags & PF_ONSIGSTK) && (sa->sa_flags & SA_STACK)) {
186
                frame = (unsigned long *)sa->sa_restorer;
187
                current->flags |= PF_ONSIGSTK;
188
        }
189
#ifdef DEBUG
190
printk("setup_frame: usp %.8x\n",frame);
191
#endif
192
        frame -= UFRAME_SIZE(fsize);
193
#ifdef DEBUG
194
printk("setup_frame: frame %.8x\n",frame);
195
#endif
196
        if (verify_area(VERIFY_WRITE,frame,UFRAME_SIZE(fsize)*4))
197
                do_exit(SIGSEGV);
198
        if (fsize) {
199
                memcpy_tofs (frame + UFRAME_SIZE(0), regs + 1, fsize);
200
                regs->stkadj = fsize;
201
        }
202
 
203
/* set up the "normal" stack seen by the signal handler */
204
        tframe = frame;
205
 
206
        /* return address points to code on stack */
207
        put_user((ulong)(frame+4), tframe); tframe++;
208
        if (current->exec_domain && current->exec_domain->signal_invmap)
209
            put_user(current->exec_domain->signal_invmap[signr], tframe);
210
        else
211
            put_user(signr, tframe);
212
        tframe++;
213
        tframe++;
214
        /* "scp" parameter.  points to sigcontext */
215
        put_user((ulong)(frame+6), tframe); tframe++;
216
 
217
/* set up the return code... */
218
        put_user(0xdefc0014,tframe); tframe++; /* addaw #20,sp */
219
        put_user(0x70774e40,tframe); tframe++; /* moveq #119,d0; trap #0 */
220
 
221
/* setup and copy the sigcontext structure */
222
        context.sc_mask       = oldmask;
223
        context.sc_usp        = rdusp();
224
        context.sc_d0         = regs->d0;
225
        context.sc_d1         = regs->d1;
226
        context.sc_a0         = regs->a0;
227
        context.sc_a1         = regs->a1;
228
        context.sc_sr         = regs->sr;
229
        context.sc_pc         = regs->pc;
230
 
231
        memcpy_tofs (tframe, &context, sizeof(context));
232
#ifdef DEBUG
233
printk("setup_frame: top tframe %.8x\n",tframe + sizeof(context));
234
#endif
235
 
236
        /* Set up registers for signal handler */
237
        wrusp ((unsigned long) frame);
238
        regs->pc = (unsigned long) sa->sa_handler;
239
 
240
        /* Prepare to skip over the extra stuff in the exception frame.  */
241
        if (regs->stkadj) {
242
                struct pt_regs *tregs =
243
                        (struct pt_regs *)((ulong)regs + regs->stkadj);
244
 
245
#ifdef DEBUG
246
                printk("Performing stackadjust=%04x\n", regs->stkadj);
247
#endif
248
 
249
                /* This must be copied with decreasing addresses to
250
                   handle overlaps.  */
251
                tregs->pc = regs->pc;
252
                tregs->sr = regs->sr;
253
        }
254
}
255
 
256
/*
257
 * OK, we're invoking a handler
258
 */
259
static void handle_signal(unsigned long signr, struct sigaction *sa,
260
                          unsigned long oldmask, struct pt_regs *regs)
261
{
262
#ifdef DEBUG
263
        printk("Entering handle_signal, signr=%d\n", signr);
264
#endif
265
        /* are we from a system call? */
266
        if (regs->orig_d0 >= 0) {
267
                /* If so, check system call restarting.. */
268
                switch (regs->d0) {
269
                        case -ERESTARTNOHAND:
270
                                regs->d0 = -EINTR;
271
                                break;
272
 
273
                        case -ERESTARTSYS:
274
                                if (!(sa->sa_flags & SA_RESTART)) {
275
                                        regs->d0 = -EINTR;
276
                                        break;
277
                                }
278
                /* fallthrough */
279
                        case -ERESTARTNOINTR:
280
                                regs->d0 = regs->orig_d0;
281
                                regs->pc -= 2;
282
                }
283
        }
284
 
285
        /* set up the stack frame */
286
        setup_frame(sa, regs, signr, oldmask);
287
 
288
        if (sa->sa_flags & SA_ONESHOT)
289
                sa->sa_handler = NULL;
290
        if (!(sa->sa_flags & SA_NOMASK))
291
                current->blocked |= (sa->sa_mask | _S(signr)) & _BLOCKABLE;
292
}
293
 
294
/*
295
 * Note that 'init' is a special process: it doesn't get signals it doesn't
296
 * want to handle. Thus you cannot kill init even with a SIGKILL even by
297
 * mistake.
298
 *
299
 * Note that we go through the signals twice: once to check the signals
300
 * that the kernel can handle, and then we build all the user-level signal
301
 * handling stack-frames in one go after that.
302
 */
303
asmlinkage int do_signal(unsigned long oldmask, struct pt_regs *regs)
304
{
305
        unsigned long mask = ~current->blocked;
306
        unsigned long signr;
307
        struct sigaction * sa;
308
 
309
        current->tss.esp0 = (unsigned long) regs;
310
 
311
        /* If the process is traced, all signals are passed to the debugger. */
312
        if (current->flags & PF_PTRACED)
313
                mask = ~0UL;
314
        while ((signr = current->signal & mask) != 0) {
315
                signr = ffz(~signr);
316
                clear_bit(signr, &current->signal);
317
                sa = current->sig->action + signr;
318
                signr++;
319
 
320
#ifdef DEBUG
321
                printk("Signal %d: ", sa->sa_handler);
322
#endif
323
 
324
                if ((current->flags & PF_PTRACED) && signr != SIGKILL) {
325
                        current->exit_code = signr;
326
                        current->state = TASK_STOPPED;
327
 
328
                        /* Did we come from a system call? */
329
                        if (regs->orig_d0 >= 0) {
330
                                /* Restart the system call */
331
                                if (regs->d0 == -ERESTARTNOHAND ||
332
                                    regs->d0 == -ERESTARTSYS ||
333
                                    regs->d0 == -ERESTARTNOINTR) {
334
                                        regs->d0 = regs->orig_d0;
335
                                        regs->pc -= 2;
336
                                }
337
                        }
338
                        notify_parent(current, SIGCHLD);
339
                        schedule();
340
                        if (!(signr = current->exit_code)) {
341
                        discard_frame:
342
                            continue;
343
                        }
344
                        current->exit_code = 0;
345
                        if (signr == SIGSTOP)
346
                                goto discard_frame;
347
                        if (_S(signr) & current->blocked) {
348
                                current->signal |= _S(signr);
349
                                mask &= ~_S(signr);
350
                                continue;
351
                        }
352
                        sa = current->sig->action + signr - 1;
353
                }
354
                if (sa->sa_handler == SIG_IGN) {
355
#ifdef DEBUG
356
                        printk("Ignoring.\n");
357
#endif
358
                        if (signr != SIGCHLD)
359
                                continue;
360
                        /* check for SIGCHLD: it's special */
361
                        while (sys_waitpid(-1,NULL,WNOHANG) > 0)
362
                                /* nothing */;
363
                        continue;
364
                }
365
                if (sa->sa_handler == SIG_DFL) {
366
#ifdef DEBUG
367
                        printk("Default.\n");
368
#endif
369
                        if (current->pid == 1)
370
                                continue;
371
                        switch (signr) {
372
                        case SIGCONT: case SIGCHLD: case SIGWINCH:
373
                                continue;
374
 
375
                        case SIGTSTP: case SIGTTIN: case SIGTTOU:
376
                                if (is_orphaned_pgrp(current->pgrp))
377
                                        continue;
378
                        case SIGSTOP:
379
                                if (current->flags & PF_PTRACED)
380
                                        continue;
381
                                current->state = TASK_STOPPED;
382
                                current->exit_code = signr;
383
                                if (!(current->p_pptr->sig->action[SIGCHLD-1].sa_flags &
384
                                      SA_NOCLDSTOP))
385
                                        notify_parent(current, SIGCHLD);
386
                                schedule();
387
                                continue;
388
 
389
                        case SIGQUIT: case SIGILL: case SIGTRAP:
390
                        case SIGIOT: case SIGFPE: case SIGSEGV:
391
                                if (current->binfmt && current->binfmt->core_dump) {
392
                                    if (current->binfmt->core_dump(signr, regs))
393
                                        signr |= 0x80;
394
                                }
395
                                /* fall through */
396
                        default:
397
                                current->signal |= _S(signr & 0x7f);
398
                                current->flags |= PF_SIGNALED;
399
                                do_exit(signr);
400
                        }
401
                }
402
                handle_signal(signr, sa, oldmask, regs);
403
                return 1;
404
        }
405
 
406
        /* Did we come from a system call? */
407
        if (regs->orig_d0 >= 0) {
408
                /* Restart the system call - no handlers present */
409
                if (regs->d0 == -ERESTARTNOHAND ||
410
                    regs->d0 == -ERESTARTSYS ||
411
                    regs->d0 == -ERESTARTNOINTR) {
412
                        regs->d0 = regs->orig_d0;
413
                        regs->pc -= 2;
414
                }
415
        }
416
 
417
        /* If we are about to discard some frame stuff we must copy
418
           over the remaining frame. */
419
        if (regs->stkadj) {
420
                struct pt_regs *tregs =
421
                  (struct pt_regs *) ((ulong) regs + regs->stkadj);
422
#if defined(DEBUG) || 1
423
        printk("!!!!!!!!!!!!!! stkadj !!!\n");
424
#endif
425
                /* This must be copied with decreasing addresses to
426
                   handle overlaps.  */
427
                tregs->pc = regs->pc;
428
                tregs->sr = regs->sr;
429
        }
430
        return 0;
431
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.