sigcontext.c revision e739ac0589b4fb43561f801c4faba8c1b89f8680
1#define _GNU_SOURCE
2
3#include <stdio.h>
4#include <signal.h>
5#include <unistd.h>
6#include <sys/ucontext.h>
7#include <asm/unistd.h>
8
9#define VAL1 0x11223344
10#define VAL2 0x44332211
11
12static void handler1(int sig, siginfo_t *si, ucontext_t *uc)
13{
14	/* Since the handler will be called as kill leaves the kernel,
15	   this is replacing the kill syscall's return value. */
16	if (uc->uc_mcontext.gregs[REG_EAX] != 0)
17		printf("FAILED: handler2 expected eax == 0, not %d\n", uc->uc_mcontext.gregs[REG_EAX]);
18	uc->uc_mcontext.gregs[REG_EAX] = VAL1;
19
20	asm volatile (
21		"movl	$0, %%edx\n"
22		"movl	$0, %%esi\n"
23		"movl	$0, %%edi\n"
24		: : : "edx", "esi", "edi");
25}
26
27static void handler2(int sig, struct sigcontext sc)
28{
29	/* Since the handler will be called as kill leaves the kernel,
30	   this is replacing the kill syscall's return value. */
31	if (sc.eax != 0)
32		printf("FAILED: handler2 expected eax == 0, not %p\n", (void*)sc.eax);
33
34	sc.eax = VAL2;
35
36	asm volatile (
37		"movl	$0, %%edx\n"
38		"movl	$0, %%esi\n"
39		"movl	$0, %%edi\n"
40		: : : "edx", "esi", "edi");
41}
42
43int main()
44{
45	struct sigaction sa;
46	int ret;
47	int v2, v3, v4;
48
49	sa.sa_handler = (void*)handler1;
50	sa.sa_flags = SA_SIGINFO;
51	sigfillset(&sa.sa_mask);
52
53	sigaction(SIGUSR1, &sa, NULL);
54
55	sa.sa_handler = (void*)handler2;
56	sa.sa_flags = 0;
57	sigfillset(&sa.sa_mask);
58
59	sigaction(SIGUSR2, &sa, NULL);
60
61	asm volatile (
62		//"movl	$0x11111111, %%ebp\n"
63		"movl	$0x22222222, %%edx\n"
64		"movl	$0x33333333, %%esi\n"
65		"movl	$0x44444444, %%edi\n"
66		"int $0x80"
67		: "=a" (ret),  "=d" (v2), "=S" (v3), "=D" (v4)
68		: "0" (__NR_kill), "b" (getpid()), "c" (SIGUSR1));
69	printf("v2=%x v3=%x v4=%x\n", v2, v3, v4);
70
71	if (ret == VAL1)
72		printf("PASS %x\n", ret);
73	else
74		printf("FAIL ret=%x not %x\n", ret, VAL1);
75
76	asm volatile (
77		//"movl	$0x11111111, %%ebp\n"
78		"movl	$0x22222222, %%edx\n"
79		"movl	$0x33333333, %%esi\n"
80		"movl	$0x44444444, %%edi\n"
81		"int $0x80"
82		: "=a" (ret),  "=d" (v2), "=S" (v3), "=D" (v4)
83		: "0" (__NR_kill), "b" (getpid()), "c" (SIGUSR2));
84	printf("v2=%x v3=%x v4=%x\n", v2, v3, v4);
85
86	if (ret == VAL2)
87		printf("PASS %x\n", ret);
88	else
89		printf("FAIL ret=%x not %x\n", ret, VAL2);
90
91	return 0;
92}
93