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

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [rc203soc/] [sw/] [uClinux/] [arch/] [i386/] [kernel/] [signal.c] - Blame information for rev 1623

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1623 jcastillo
/*
2
 *  linux/arch/i386/kernel/signal.c
3
 *
4
 *  Copyright (C) 1991, 1992  Linus Torvalds
5
 */
6
 
7
#include <linux/config.h>
8
 
9
#include <linux/sched.h>
10
#include <linux/mm.h>
11
#include <linux/kernel.h>
12
#include <linux/signal.h>
13
#include <linux/errno.h>
14
#include <linux/wait.h>
15
#include <linux/ptrace.h>
16
#include <linux/unistd.h>
17
 
18
#include <asm/segment.h>
19
 
20
#define _S(nr) (1<<((nr)-1))
21
 
22
#define _BLOCKABLE (~(_S(SIGKILL) | _S(SIGSTOP)))
23
 
24
asmlinkage int sys_waitpid(pid_t pid,unsigned long * stat_addr, int options);
25
asmlinkage int do_signal(unsigned long oldmask, struct pt_regs * regs);
26
 
27
/*
28
 * atomically swap in the new signal mask, and wait for a signal.
29
 */
30
asmlinkage int sys_sigsuspend(int restart, unsigned long oldmask, unsigned long set)
31
{
32
        unsigned long mask;
33
        struct pt_regs * regs = (struct pt_regs *) &restart;
34
 
35
        mask = current->blocked;
36
        current->blocked = set & _BLOCKABLE;
37
        regs->eax = -EINTR;
38
        while (1) {
39
                current->state = TASK_INTERRUPTIBLE;
40
                schedule();
41
                if (do_signal(mask,regs))
42
                        return -EINTR;
43
        }
44
}
45
 
46
static inline void restore_i387_hard(struct _fpstate *buf)
47
{
48
#ifdef __SMP__
49
        if (current->flags & PF_USEDFPU) {
50
                stts();
51
        }
52
#else
53
        if (current == last_task_used_math) {
54
                last_task_used_math = NULL;
55
                stts();
56
        }
57
#endif
58
        current->used_math = 1;
59
        current->flags &= ~PF_USEDFPU;
60
        memcpy_fromfs(&current->tss.i387.hard, buf, sizeof(*buf));
61
}
62
 
63
static void restore_i387(struct _fpstate *buf)
64
{
65
#ifndef CONFIG_MATH_EMULATION
66
        restore_i387_hard(buf);
67
#else
68
        if (hard_math) {
69
                restore_i387_hard(buf);
70
                return;
71
        }
72
        restore_i387_soft(buf);
73
#endif  
74
}
75
 
76
 
77
/*
78
 * This sets regs->esp even though we don't actually use sigstacks yet..
79
 */
80
asmlinkage int sys_sigreturn(unsigned long __unused)
81
{
82
#define COPY(x) regs->x = context.x
83
#define COPY_SEG(x) \
84
if (   (context.x & 0xfffc)     /* not a NULL selectors */ \
85
    && (context.x & 0x4) != 0x4 /* not a LDT selector */ \
86
    && (context.x & 3) != 3     /* not a RPL3 GDT selector */ \
87
   ) goto badframe; COPY(x);
88
#define COPY_SEG_STRICT(x) \
89
if (!(context.x & 0xfffc) || (context.x & 3) != 3) goto badframe; COPY(x);
90
        struct sigcontext_struct context;
91
        struct pt_regs * regs;
92
 
93
        regs = (struct pt_regs *) &__unused;
94
        if (verify_area(VERIFY_READ, (void *) regs->esp, sizeof(context)))
95
                goto badframe;
96
        memcpy_fromfs(&context,(void *) regs->esp, sizeof(context));
97
        current->blocked = context.oldmask & _BLOCKABLE;
98
        COPY_SEG(ds);
99
        COPY_SEG(es);
100
        COPY_SEG(fs);
101
        COPY_SEG(gs);
102
        COPY_SEG_STRICT(ss);
103
        COPY_SEG_STRICT(cs);
104
        COPY(eip);
105
        COPY(ecx); COPY(edx);
106
        COPY(ebx);
107
        COPY(esp); COPY(ebp);
108
        COPY(edi); COPY(esi);
109
        regs->eflags &= ~0x40DD5;
110
        regs->eflags |= context.eflags & 0x40DD5;
111
        regs->orig_eax = -1;            /* disable syscall checks */
112
        if (context.fpstate) {
113
                struct _fpstate * buf = context.fpstate;
114
                if (verify_area(VERIFY_READ, buf, sizeof(*buf)))
115
                        goto badframe;
116
                restore_i387(buf);
117
        }
118
        return context.eax;
119
badframe:
120
        do_exit(SIGSEGV);
121
}
122
 
123
static inline struct _fpstate * save_i387_hard(struct _fpstate * buf)
124
{
125
#ifdef __SMP__
126
        if (current->flags & PF_USEDFPU) {
127
                __asm__ __volatile__("fnsave %0":"=m" (current->tss.i387.hard));
128
                stts();
129
                current->flags &= ~PF_USEDFPU;
130
        }
131
#else
132
        if (current == last_task_used_math) {
133
                __asm__ __volatile__("fnsave %0":"=m" (current->tss.i387.hard));
134
                last_task_used_math = NULL;
135
                __asm__ __volatile__("fwait");  /* not needed on 486+ */
136
                stts();
137
        }
138
#endif
139
        current->tss.i387.hard.status = current->tss.i387.hard.swd;
140
        memcpy_tofs(buf, &current->tss.i387.hard, sizeof(*buf));
141
        current->used_math = 0;
142
        return buf;
143
}
144
 
145
static struct _fpstate * save_i387(struct _fpstate * buf)
146
{
147
        if (!current->used_math)
148
                return NULL;
149
 
150
#ifndef CONFIG_MATH_EMULATION
151
        return save_i387_hard(buf);
152
#else
153
        if (hard_math)
154
                return save_i387_hard(buf);
155
        return save_i387_soft(buf);
156
#endif
157
}
158
 
159
/*
160
 * Set up a signal frame... Make the stack look the way iBCS2 expects
161
 * it to look.
162
 */
163
static void setup_frame(struct sigaction * sa,
164
        struct pt_regs * regs, int signr,
165
        unsigned long oldmask)
166
{
167
        unsigned long * frame;
168
 
169
        frame = (unsigned long *) regs->esp;
170
        if (regs->ss != USER_DS && sa->sa_restorer)
171
                frame = (unsigned long *) sa->sa_restorer;
172
        frame -= 64;
173
        if (verify_area(VERIFY_WRITE,frame,64*4))
174
                do_exit(SIGSEGV);
175
 
176
/* set up the "normal" stack seen by the signal handler (iBCS2) */
177
#define __CODE ((unsigned long)(frame+24))
178
#define CODE(x) ((unsigned long *) ((x)+__CODE))
179
        put_user(__CODE,frame);
180
        if (current->exec_domain && current->exec_domain->signal_invmap)
181
                put_user(current->exec_domain->signal_invmap[signr], frame+1);
182
        else
183
                put_user(signr, frame+1);
184
        put_user(regs->gs, frame+2);
185
        put_user(regs->fs, frame+3);
186
        put_user(regs->es, frame+4);
187
        put_user(regs->ds, frame+5);
188
        put_user(regs->edi, frame+6);
189
        put_user(regs->esi, frame+7);
190
        put_user(regs->ebp, frame+8);
191
        put_user(regs->esp, frame+9);
192
        put_user(regs->ebx, frame+10);
193
        put_user(regs->edx, frame+11);
194
        put_user(regs->ecx, frame+12);
195
        put_user(regs->eax, frame+13);
196
        put_user(current->tss.trap_no, frame+14);
197
        put_user(current->tss.error_code, frame+15);
198
        put_user(regs->eip, frame+16);
199
        put_user(regs->cs, frame+17);
200
        put_user(regs->eflags, frame+18);
201
        put_user(regs->esp, frame+19);
202
        put_user(regs->ss, frame+20);
203
        put_user(save_i387((struct _fpstate *)(frame+32)),frame+21);
204
/* non-iBCS2 extensions.. */
205
        put_user(oldmask, frame+22);
206
        put_user(current->tss.cr2, frame+23);
207
/* set up the return code... */
208
        put_user(0x0000b858, CODE(0));   /* popl %eax ; movl $,%eax */
209
        put_user(0x80cd0000, CODE(4));  /* int $0x80 */
210
        put_user(__NR_sigreturn, CODE(2));
211
#undef __CODE
212
#undef CODE
213
 
214
        /* Set up registers for signal handler */
215
        regs->esp = (unsigned long) frame;
216
        regs->eip = (unsigned long) sa->sa_handler;
217
        regs->cs = USER_CS; regs->ss = USER_DS;
218
        regs->ds = USER_DS; regs->es = USER_DS;
219
        regs->gs = USER_DS; regs->fs = USER_DS;
220
        regs->eflags &= ~TF_MASK;
221
}
222
 
223
/*
224
 * OK, we're invoking a handler
225
 */
226
static void handle_signal(unsigned long signr, struct sigaction *sa,
227
        unsigned long oldmask, struct pt_regs * regs)
228
{
229
        /* are we from a system call? */
230
        if (regs->orig_eax >= 0) {
231
                /* If so, check system call restarting.. */
232
                switch (regs->eax) {
233
                        case -ERESTARTNOHAND:
234
                                regs->eax = -EINTR;
235
                                break;
236
 
237
                        case -ERESTARTSYS:
238
                                if (!(sa->sa_flags & SA_RESTART)) {
239
                                        regs->eax = -EINTR;
240
                                        break;
241
                                }
242
                        /* fallthrough */
243
                        case -ERESTARTNOINTR:
244
                                regs->eax = regs->orig_eax;
245
                                regs->eip -= 2;
246
                }
247
        }
248
 
249
        /* set up the stack frame */
250
        setup_frame(sa, regs, signr, oldmask);
251
 
252
        if (sa->sa_flags & SA_ONESHOT)
253
                sa->sa_handler = NULL;
254
        if (!(sa->sa_flags & SA_NOMASK))
255
                current->blocked |= (sa->sa_mask | _S(signr)) & _BLOCKABLE;
256
}
257
 
258
/*
259
 * Note that 'init' is a special process: it doesn't get signals it doesn't
260
 * want to handle. Thus you cannot kill init even with a SIGKILL even by
261
 * mistake.
262
 *
263
 * Note that we go through the signals twice: once to check the signals that
264
 * the kernel can handle, and then we build all the user-level signal handling
265
 * stack-frames in one go after that.
266
 */
267
asmlinkage int do_signal(unsigned long oldmask, struct pt_regs * regs)
268
{
269
        unsigned long mask = ~current->blocked;
270
        unsigned long signr;
271
        struct sigaction * sa;
272
 
273
        while ((signr = current->signal & mask)) {
274
                /*
275
                 *      This stops gcc flipping out. Otherwise the assembler
276
                 *      including volatiles for the inline function to get
277
                 *      current combined with this gets it confused.
278
                 */
279
                struct task_struct *t=current;
280
                __asm__("bsf %3,%1\n\t"
281
                        "btrl %1,%0"
282
                        :"=m" (t->signal),"=r" (signr)
283
                        :"0" (t->signal), "1" (signr));
284
                sa = current->sig->action + signr;
285
                signr++;
286
                if ((current->flags & PF_PTRACED) && signr != SIGKILL) {
287
                        current->exit_code = signr;
288
                        current->state = TASK_STOPPED;
289
                        notify_parent(current, SIGCHLD);
290
                        schedule();
291
                        if (!(signr = current->exit_code))
292
                                continue;
293
                        current->exit_code = 0;
294
                        if (signr == SIGSTOP)
295
                                continue;
296
                        if (_S(signr) & current->blocked) {
297
                                current->signal |= _S(signr);
298
                                continue;
299
                        }
300
                        sa = current->sig->action + signr - 1;
301
                }
302
                if (sa->sa_handler == SIG_IGN) {
303
                        if (signr != SIGCHLD)
304
                                continue;
305
                        /* check for SIGCHLD: it's special */
306
                        while (sys_waitpid(-1,NULL,WNOHANG) > 0)
307
                                /* nothing */;
308
                        continue;
309
                }
310
                if (sa->sa_handler == SIG_DFL) {
311
                        if (current->pid == 1)
312
                                continue;
313
                        switch (signr) {
314
                        case SIGCONT: case SIGCHLD: case SIGWINCH:
315
                                continue;
316
 
317
                        case SIGTSTP: case SIGTTIN: case SIGTTOU:
318
                                if (is_orphaned_pgrp(current->pgrp))
319
                                        continue;
320
                        case SIGSTOP:
321
                                if (current->flags & PF_PTRACED)
322
                                        continue;
323
                                current->state = TASK_STOPPED;
324
                                current->exit_code = signr;
325
                                if (!(current->p_pptr->sig->action[SIGCHLD-1].sa_flags &
326
                                                SA_NOCLDSTOP))
327
                                        notify_parent(current, SIGCHLD);
328
                                schedule();
329
                                continue;
330
 
331
                        case SIGQUIT: case SIGILL: case SIGTRAP:
332
                        case SIGABRT: case SIGFPE: case SIGSEGV:
333
                                if (current->binfmt && current->binfmt->core_dump) {
334
                                        if (current->binfmt->core_dump(signr, regs))
335
                                                signr |= 0x80;
336
                                }
337
                                /* fall through */
338
                        default:
339
                                current->signal |= _S(signr & 0x7f);
340
                                current->flags |= PF_SIGNALED;
341
                                do_exit(signr);
342
                        }
343
                }
344
                handle_signal(signr, sa, oldmask, regs);
345
                return 1;
346
        }
347
 
348
        /* Did we come from a system call? */
349
        if (regs->orig_eax >= 0) {
350
                /* Restart the system call - no handlers present */
351
                if (regs->eax == -ERESTARTNOHAND ||
352
                    regs->eax == -ERESTARTSYS ||
353
                    regs->eax == -ERESTARTNOINTR) {
354
                        regs->eax = regs->orig_eax;
355
                        regs->eip -= 2;
356
                }
357
        }
358
        return 0;
359
}

powered by: WebSVN 2.1.0

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