Statistics
| Branch: | Revision:

root / tests / testsig.c @ 2792c4f2

History | View | Annotate | Download (4.3 kB)

1 9de5e440 bellard
#define _GNU_SOURCE
2 1b6b029e bellard
#include <stdlib.h>
3 1b6b029e bellard
#include <stdio.h>
4 9de5e440 bellard
#include <string.h>
5 1b6b029e bellard
#include <signal.h>
6 1b6b029e bellard
#include <unistd.h>
7 9de5e440 bellard
#include <setjmp.h>
8 9de5e440 bellard
#include <sys/ucontext.h>
9 9de5e440 bellard
10 9de5e440 bellard
jmp_buf jmp_env;
11 1b6b029e bellard
12 1b6b029e bellard
void alarm_handler(int sig)
13 1b6b029e bellard
{
14 1b6b029e bellard
    printf("alarm signal=%d\n", sig);
15 1b6b029e bellard
    alarm(1);
16 1b6b029e bellard
}
17 1b6b029e bellard
18 d691f669 bellard
#ifndef REG_EAX
19 d691f669 bellard
#define REG_EAX EAX
20 d691f669 bellard
#define REG_EBX EBX
21 d691f669 bellard
#define REG_ECX ECX
22 d691f669 bellard
#define REG_EDX EDX
23 d691f669 bellard
#define REG_ESI ESI
24 d691f669 bellard
#define REG_EDI EDI
25 d691f669 bellard
#define REG_EBP EBP
26 d691f669 bellard
#define REG_ESP ESP
27 d691f669 bellard
#define REG_EIP EIP
28 d691f669 bellard
#define REG_EFL EFL
29 a69d83b6 bellard
#define REG_TRAPNO TRAPNO
30 a69d83b6 bellard
#define REG_ERR ERR
31 d691f669 bellard
#endif
32 d691f669 bellard
33 9de5e440 bellard
void dump_regs(struct ucontext *uc)
34 9de5e440 bellard
{
35 9de5e440 bellard
    printf("EAX=%08x EBX=%08x ECX=%08x EDX=%08x\n"
36 9de5e440 bellard
           "ESI=%08x EDI=%08x EBP=%08x ESP=%08x\n"
37 a69d83b6 bellard
           "EFL=%08x EIP=%08x trapno=%02x err=%08x\n",
38 d691f669 bellard
           uc->uc_mcontext.gregs[REG_EAX],
39 d691f669 bellard
           uc->uc_mcontext.gregs[REG_EBX],
40 d691f669 bellard
           uc->uc_mcontext.gregs[REG_ECX],
41 d691f669 bellard
           uc->uc_mcontext.gregs[REG_EDX],
42 d691f669 bellard
           uc->uc_mcontext.gregs[REG_ESI],
43 d691f669 bellard
           uc->uc_mcontext.gregs[REG_EDI],
44 d691f669 bellard
           uc->uc_mcontext.gregs[REG_EBP],
45 d691f669 bellard
           uc->uc_mcontext.gregs[REG_ESP],
46 d691f669 bellard
           uc->uc_mcontext.gregs[REG_EFL],
47 a69d83b6 bellard
           uc->uc_mcontext.gregs[REG_EIP],
48 a69d83b6 bellard
           uc->uc_mcontext.gregs[REG_TRAPNO],
49 a69d83b6 bellard
           uc->uc_mcontext.gregs[REG_ERR]);
50 9de5e440 bellard
}
51 9de5e440 bellard
52 9de5e440 bellard
void sig_handler(int sig, siginfo_t *info, void *puc)
53 9de5e440 bellard
{
54 9de5e440 bellard
    struct ucontext *uc = puc;
55 9de5e440 bellard
56 9de5e440 bellard
    printf("%s: si_signo=%d si_errno=%d si_code=%d si_addr=0x%08lx\n",
57 9de5e440 bellard
           strsignal(info->si_signo),
58 9de5e440 bellard
           info->si_signo, info->si_errno, info->si_code, 
59 9de5e440 bellard
           (unsigned long)info->si_addr);
60 9de5e440 bellard
    dump_regs(uc);
61 9de5e440 bellard
    longjmp(jmp_env, 1);
62 9de5e440 bellard
}
63 9de5e440 bellard
64 9de5e440 bellard
int v1;
65 a69d83b6 bellard
int tab[2];
66 9de5e440 bellard
67 1b6b029e bellard
int main(int argc, char **argv)
68 1b6b029e bellard
{
69 1b6b029e bellard
    struct sigaction act;
70 a69d83b6 bellard
    int val;
71 9de5e440 bellard
    
72 a69d83b6 bellard
    act.sa_sigaction = sig_handler;
73 a69d83b6 bellard
    sigemptyset(&act.sa_mask);
74 a69d83b6 bellard
    act.sa_flags = SA_SIGINFO;
75 a69d83b6 bellard
    sigaction(SIGFPE, &act, NULL);
76 a69d83b6 bellard
    sigaction(SIGILL, &act, NULL);
77 a69d83b6 bellard
    sigaction(SIGSEGV, &act, NULL);
78 a69d83b6 bellard
79 9de5e440 bellard
    /* test division by zero reporting */
80 9de5e440 bellard
    if (setjmp(jmp_env) == 0) {
81 9de5e440 bellard
        /* now divide by zero */
82 9de5e440 bellard
        v1 = 0;
83 9de5e440 bellard
        v1 = 2 / v1;
84 9de5e440 bellard
    }
85 9de5e440 bellard
86 9de5e440 bellard
    /* test illegal instruction reporting */
87 9de5e440 bellard
    if (setjmp(jmp_env) == 0) {
88 9de5e440 bellard
        /* now execute an invalid instruction */
89 9de5e440 bellard
        asm volatile("ud2");
90 9de5e440 bellard
    }
91 9de5e440 bellard
    
92 9de5e440 bellard
    /* test SEGV reporting */
93 9de5e440 bellard
    if (setjmp(jmp_env) == 0) {
94 9de5e440 bellard
        /* now store in an invalid address */
95 9de5e440 bellard
        *(char *)0x1234 = 1;
96 9de5e440 bellard
    }
97 a69d83b6 bellard
98 a69d83b6 bellard
    /* test SEGV reporting */
99 a69d83b6 bellard
    if (setjmp(jmp_env) == 0) {
100 a69d83b6 bellard
        /* read from an invalid address */
101 a69d83b6 bellard
        v1 = *(char *)0x1234;
102 a69d83b6 bellard
    }
103 9de5e440 bellard
    
104 a69d83b6 bellard
    printf("segment GPF exception:\n");
105 a69d83b6 bellard
    if (setjmp(jmp_env) == 0) {
106 a69d83b6 bellard
        /* load an invalid segment */
107 a69d83b6 bellard
        asm volatile ("movl %0, %%fs" : : "r" ((0x1234 << 3) | 0));
108 a69d83b6 bellard
    }
109 a69d83b6 bellard
110 a69d83b6 bellard
    printf("INT exception:\n");
111 a69d83b6 bellard
    if (setjmp(jmp_env) == 0) {
112 a69d83b6 bellard
        asm volatile ("int $0xfd");
113 a69d83b6 bellard
    }
114 a69d83b6 bellard
115 a69d83b6 bellard
    printf("CLI exception:\n");
116 a69d83b6 bellard
    if (setjmp(jmp_env) == 0) {
117 a69d83b6 bellard
        asm volatile ("cli");
118 a69d83b6 bellard
    }
119 a69d83b6 bellard
120 a69d83b6 bellard
    printf("STI exception:\n");
121 a69d83b6 bellard
    if (setjmp(jmp_env) == 0) {
122 a69d83b6 bellard
        asm volatile ("cli");
123 a69d83b6 bellard
    }
124 a69d83b6 bellard
125 a69d83b6 bellard
    printf("INTO exception:\n");
126 a69d83b6 bellard
    if (setjmp(jmp_env) == 0) {
127 a69d83b6 bellard
        /* overflow exception */
128 a69d83b6 bellard
        asm volatile ("addl $1, %0 ; into" : : "r" (0x7fffffff));
129 a69d83b6 bellard
    }
130 a69d83b6 bellard
131 a69d83b6 bellard
    printf("BOUND exception:\n");
132 a69d83b6 bellard
    if (setjmp(jmp_env) == 0) {
133 a69d83b6 bellard
        /* bound exception */
134 a69d83b6 bellard
        tab[0] = 1;
135 a69d83b6 bellard
        tab[1] = 10;
136 a69d83b6 bellard
        asm volatile ("bound %0, %1" : : "r" (11), "m" (tab));
137 a69d83b6 bellard
    }
138 a69d83b6 bellard
139 a69d83b6 bellard
    printf("OUTB exception:\n");
140 a69d83b6 bellard
    if (setjmp(jmp_env) == 0) {
141 a69d83b6 bellard
        asm volatile ("outb %%al, %%dx" : : "d" (0x4321), "a" (0));
142 a69d83b6 bellard
    }
143 a69d83b6 bellard
144 a69d83b6 bellard
    printf("INB exception:\n");
145 a69d83b6 bellard
    if (setjmp(jmp_env) == 0) {
146 a69d83b6 bellard
        asm volatile ("inb %%dx, %%al" : "=a" (val) : "d" (0x4321));
147 1b6b029e bellard
    }
148 a69d83b6 bellard
149 a69d83b6 bellard
    printf("REP OUTSB exception:\n");
150 a69d83b6 bellard
    if (setjmp(jmp_env) == 0) {
151 a69d83b6 bellard
        asm volatile ("rep outsb" : : "d" (0x4321), "S" (tab), "c" (1));
152 a69d83b6 bellard
    }
153 a69d83b6 bellard
154 a69d83b6 bellard
    printf("REP INSB exception:\n");
155 a69d83b6 bellard
    if (setjmp(jmp_env) == 0) {
156 a69d83b6 bellard
        asm volatile ("rep insb" : : "d" (0x4321), "D" (tab), "c" (1));
157 a69d83b6 bellard
    }
158 a69d83b6 bellard
159 a69d83b6 bellard
    printf("HLT exception:\n");
160 a69d83b6 bellard
    if (setjmp(jmp_env) == 0) {
161 a69d83b6 bellard
        asm volatile ("hlt" : : "d" (0x4321), "D" (tab), "c" (1));
162 a69d83b6 bellard
    }
163 a69d83b6 bellard
164 a69d83b6 bellard
#if 0
165 a69d83b6 bellard
    {
166 a69d83b6 bellard
        int i;
167 a69d83b6 bellard
        act.sa_handler = alarm_handler;
168 a69d83b6 bellard
        sigemptyset(&act.sa_mask);
169 a69d83b6 bellard
        act.sa_flags = 0;
170 a69d83b6 bellard
        sigaction(SIGALRM, &act, NULL);
171 a69d83b6 bellard
        alarm(1);
172 a69d83b6 bellard
        for(i = 0;i < 2; i++) {
173 a69d83b6 bellard
            sleep(1);
174 a69d83b6 bellard
        }
175 a69d83b6 bellard
    }
176 a69d83b6 bellard
#endif
177 1b6b029e bellard
    return 0;
178 1b6b029e bellard
}