Statistics
| Branch: | Revision:

root / tests / testsig.c @ 394411ac

History | View | Annotate | Download (4.7 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 2a29ca73 bellard
    volatile 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 2a29ca73 bellard
    sigaction(SIGTRAP, &act, NULL);
79 a69d83b6 bellard
80 9de5e440 bellard
    /* test division by zero reporting */
81 9de5e440 bellard
    if (setjmp(jmp_env) == 0) {
82 9de5e440 bellard
        /* now divide by zero */
83 9de5e440 bellard
        v1 = 0;
84 9de5e440 bellard
        v1 = 2 / v1;
85 9de5e440 bellard
    }
86 9de5e440 bellard
87 9de5e440 bellard
    /* test illegal instruction reporting */
88 9de5e440 bellard
    if (setjmp(jmp_env) == 0) {
89 9de5e440 bellard
        /* now execute an invalid instruction */
90 9de5e440 bellard
        asm volatile("ud2");
91 9de5e440 bellard
    }
92 9de5e440 bellard
    
93 9de5e440 bellard
    /* test SEGV reporting */
94 9de5e440 bellard
    if (setjmp(jmp_env) == 0) {
95 9de5e440 bellard
        /* now store in an invalid address */
96 9de5e440 bellard
        *(char *)0x1234 = 1;
97 9de5e440 bellard
    }
98 a69d83b6 bellard
99 a69d83b6 bellard
    /* test SEGV reporting */
100 a69d83b6 bellard
    if (setjmp(jmp_env) == 0) {
101 a69d83b6 bellard
        /* read from an invalid address */
102 a69d83b6 bellard
        v1 = *(char *)0x1234;
103 a69d83b6 bellard
    }
104 9de5e440 bellard
    
105 a69d83b6 bellard
    printf("segment GPF exception:\n");
106 a69d83b6 bellard
    if (setjmp(jmp_env) == 0) {
107 a69d83b6 bellard
        /* load an invalid segment */
108 a69d83b6 bellard
        asm volatile ("movl %0, %%fs" : : "r" ((0x1234 << 3) | 0));
109 a69d83b6 bellard
    }
110 a69d83b6 bellard
111 a69d83b6 bellard
    printf("INT exception:\n");
112 a69d83b6 bellard
    if (setjmp(jmp_env) == 0) {
113 a69d83b6 bellard
        asm volatile ("int $0xfd");
114 a69d83b6 bellard
    }
115 a69d83b6 bellard
116 2a29ca73 bellard
    printf("INT3 exception:\n");
117 2a29ca73 bellard
    if (setjmp(jmp_env) == 0) {
118 2a29ca73 bellard
        asm volatile ("int3");
119 2a29ca73 bellard
    }
120 2a29ca73 bellard
121 a69d83b6 bellard
    printf("CLI exception:\n");
122 a69d83b6 bellard
    if (setjmp(jmp_env) == 0) {
123 a69d83b6 bellard
        asm volatile ("cli");
124 a69d83b6 bellard
    }
125 a69d83b6 bellard
126 a69d83b6 bellard
    printf("STI exception:\n");
127 a69d83b6 bellard
    if (setjmp(jmp_env) == 0) {
128 a69d83b6 bellard
        asm volatile ("cli");
129 a69d83b6 bellard
    }
130 a69d83b6 bellard
131 a69d83b6 bellard
    printf("INTO exception:\n");
132 a69d83b6 bellard
    if (setjmp(jmp_env) == 0) {
133 a69d83b6 bellard
        /* overflow exception */
134 a69d83b6 bellard
        asm volatile ("addl $1, %0 ; into" : : "r" (0x7fffffff));
135 a69d83b6 bellard
    }
136 a69d83b6 bellard
137 a69d83b6 bellard
    printf("BOUND exception:\n");
138 a69d83b6 bellard
    if (setjmp(jmp_env) == 0) {
139 a69d83b6 bellard
        /* bound exception */
140 a69d83b6 bellard
        tab[0] = 1;
141 a69d83b6 bellard
        tab[1] = 10;
142 a69d83b6 bellard
        asm volatile ("bound %0, %1" : : "r" (11), "m" (tab));
143 a69d83b6 bellard
    }
144 a69d83b6 bellard
145 a69d83b6 bellard
    printf("OUTB exception:\n");
146 a69d83b6 bellard
    if (setjmp(jmp_env) == 0) {
147 a69d83b6 bellard
        asm volatile ("outb %%al, %%dx" : : "d" (0x4321), "a" (0));
148 a69d83b6 bellard
    }
149 a69d83b6 bellard
150 a69d83b6 bellard
    printf("INB exception:\n");
151 a69d83b6 bellard
    if (setjmp(jmp_env) == 0) {
152 a69d83b6 bellard
        asm volatile ("inb %%dx, %%al" : "=a" (val) : "d" (0x4321));
153 1b6b029e bellard
    }
154 a69d83b6 bellard
155 a69d83b6 bellard
    printf("REP OUTSB exception:\n");
156 a69d83b6 bellard
    if (setjmp(jmp_env) == 0) {
157 a69d83b6 bellard
        asm volatile ("rep outsb" : : "d" (0x4321), "S" (tab), "c" (1));
158 a69d83b6 bellard
    }
159 a69d83b6 bellard
160 a69d83b6 bellard
    printf("REP INSB exception:\n");
161 a69d83b6 bellard
    if (setjmp(jmp_env) == 0) {
162 a69d83b6 bellard
        asm volatile ("rep insb" : : "d" (0x4321), "D" (tab), "c" (1));
163 a69d83b6 bellard
    }
164 a69d83b6 bellard
165 a69d83b6 bellard
    printf("HLT exception:\n");
166 a69d83b6 bellard
    if (setjmp(jmp_env) == 0) {
167 2a29ca73 bellard
        asm volatile ("hlt");
168 a69d83b6 bellard
    }
169 a69d83b6 bellard
170 2a29ca73 bellard
    printf("single step exception:\n");
171 2a29ca73 bellard
    val = 0;
172 2a29ca73 bellard
    if (setjmp(jmp_env) == 0) {
173 2a29ca73 bellard
        asm volatile ("pushf\n"
174 2a29ca73 bellard
                      "orl $0x00100, (%%esp)\n"
175 2a29ca73 bellard
                      "popf\n"
176 2a29ca73 bellard
                      "movl $0xabcd, %0\n" : "=m" (val) : : "cc", "memory");
177 2a29ca73 bellard
    }
178 2a29ca73 bellard
    printf("val=0x%x\n", val);
179 2a29ca73 bellard
    
180 2a29ca73 bellard
#if 1
181 a69d83b6 bellard
    {
182 a69d83b6 bellard
        int i;
183 a69d83b6 bellard
        act.sa_handler = alarm_handler;
184 a69d83b6 bellard
        sigemptyset(&act.sa_mask);
185 a69d83b6 bellard
        act.sa_flags = 0;
186 a69d83b6 bellard
        sigaction(SIGALRM, &act, NULL);
187 a69d83b6 bellard
        alarm(1);
188 a69d83b6 bellard
        for(i = 0;i < 2; i++) {
189 a69d83b6 bellard
            sleep(1);
190 a69d83b6 bellard
        }
191 a69d83b6 bellard
    }
192 a69d83b6 bellard
#endif
193 1b6b029e bellard
    return 0;
194 1b6b029e bellard
}