summaryrefslogtreecommitdiffstats
path: root/kernel/tests/include/lapi/rt_sigaction.h
blob: 3af91362f6c8bd97b18918fed2686014dbf77cd5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (c) 2009 Cisco Systems, Inc.  All Rights Reserved.
 * Copyright (c) 2009 FUJITSU LIMITED.  All Rights Reserved.
 * Author: Liu Bo <liubo2009@cn.fujitsu.com>
 * Author: Ngie Cooper <yaneurabeya@gmail.com>
 */

#ifndef LTP_RT_SIGACTION_H
#define LTP_RT_SIGACTION_H

#include "ltp_signal.h"

#define INVAL_SA_PTR ((void *)-1)

#if defined(__mips__)
struct kernel_sigaction {
	unsigned int sa_flags;
	void (* k_sa_handler)(int);
	sigset_t sa_mask;
};
#else
struct kernel_sigaction {
	void (* k_sa_handler)(int);
	unsigned long sa_flags;
	void (*sa_restorer) (void);
	sigset_t sa_mask;
};
#endif

/* This macro marks if (struct sigaction) has .sa_restorer member */
#if !defined(__ia64__) && !defined(__alpha__) && !defined(__hppa__) && !defined(__mips__)
# define HAVE_SA_RESTORER
#endif

#ifdef __x86_64__

/*
 * From asm/signal.h -- this value isn't exported anywhere outside of glibc and
 * asm/signal.h and is only required for the rt_sig* function family because
 * sigaction(2), et all, appends this if necessary to
 * (struct sigaction).sa_flags. HEH.
 *
 * I do #undef though, just in case...
 *
 * Also, from .../arch/x86/kernel/signal.c:448 for v2.6.30 (something or
 * other):
 *
 * x86-64 should always use SA_RESTORER.
 *
 * -- thus SA_RESTORER must always be defined along with
 * (struct sigaction).sa_restorer for this architecture.
 */
#undef SA_RESTORER
#define SA_RESTORER     0x04000000

void (*restore_rt)(void);

static void handler_h(int signal)
{
	return;
}

/* Setup an initial signal handler for signal number = sig for x86_64. */
static inline int sig_initial(int sig)
{
	int ret_code = -1;
	struct sigaction act, oact;

	act.sa_handler = handler_h;
	act.sa_flags = 0;
	/* Clear out the signal set. */
	if (sigemptyset(&act.sa_mask) < 0) {
		/* Add the signal to the mask set. */
	} else if (sigaddset(&act.sa_mask, sig) < 0) {
		/* Set act.sa_restorer via syscall(2) */
	} else if (sigaction(sig, &act, &oact) < 0) {
		/* Copy oact.sa_restorer via syscall(2) */
	} else if (sigaction(sig, &act, &oact) < 0) {
		/* And voila -- we just tricked the kernel into giving us our
		 * restorer function! */
	} else {
		restore_rt = oact.sa_restorer;
		ret_code = 0;
	}

	return ret_code;
}

#endif /* __x86_64__ */

#ifdef __sparc__
# if defined __arch64__ || defined __sparcv9

/*
 * Based on glibc/sysdeps/unix/sysv/linux/sparc/sparc64/sigaction.c
 */

extern char *__rt_sig_stub;

static void __attribute__((used)) __rt_sigreturn_stub(void)
{
	__asm__ ("__rt_sig_stub: mov %0, %%g1\n\t"
		"ta  0x6d\n\t"
		: /* no outputs */
		: "i" (__NR_rt_sigreturn));
}

# else /* sparc32 */

/*
 * Based on glibc/sysdeps/unix/sysv/linux/sparc/sparc32/sigaction.c
 */

extern char *__rt_sig_stub, *__sig_stub;

static void __attribute__((used)) __rt_sigreturn_stub(void)
{
	__asm__ ("__rt_sig_stub: mov %0, %%g1\n\t"
		"ta  0x10\n\t"
		: /* no outputs */
		: "i" (__NR_rt_sigreturn));
}

static void __attribute__((used)) __sigreturn_stub(void)
{
	__asm__ ("__sig_stub: mov %0, %%g1\n\t"
		"ta  0x10\n\t"
		: /* no outputs */
		: "i" (__NR_sigreturn));
}

# endif
#endif /* __sparc__ */

#ifdef __arc__

#undef SA_RESTORER
#define SA_RESTORER     0x04000000

/*
 * based on uClibc/libc/sysdeps/linux/arc/sigaction.c
 */
static void
__attribute__ ((optimize("Os"))) __attribute__((used)) restore_rt(void)
{
	__asm__ (
		"mov r8, %0	\n\t"
#ifdef __ARCHS__
		"trap_s	0	\n\t"
#else
		"trap0	\n\t"
#endif
		: /* no outputs */
		: "i" (__NR_rt_sigreturn)
		: "r8");
}
#endif

#ifdef TST_TEST_H__
# define TST_SYSCALL tst_syscall
#else
# define TST_SYSCALL ltp_syscall
#endif

/* This is a wrapper for __NR_rt_sigaction syscall.
 * act/oact values of INVAL_SA_PTR is used to pass
 * an invalid pointer to syscall(__NR_rt_sigaction)
 *
 * Based on glibc/sysdeps/unix/sysv/linux/{...}/sigaction.c
 */

static int ltp_rt_sigaction(int signum, const struct sigaction *act,
			struct sigaction *oact, size_t sigsetsize)
{
	int ret;
	struct kernel_sigaction kact, koact;
	struct kernel_sigaction *kact_p = NULL;
	struct kernel_sigaction *koact_p = NULL;

	if (act == INVAL_SA_PTR) {
		kact_p = INVAL_SA_PTR;
	} else if (act) {
		kact.k_sa_handler = act->sa_handler;
		memcpy(&kact.sa_mask, &act->sa_mask, sizeof(sigset_t));
		kact.sa_flags = act->sa_flags;
#ifndef __mips__
		kact.sa_restorer = NULL;
#endif
		kact_p = &kact;
	}

	if (oact == INVAL_SA_PTR)
		koact_p = INVAL_SA_PTR;
	else if (oact)
		koact_p = &koact;

#ifdef __x86_64__
	sig_initial(signum);
#endif

#if defined __x86_64__ || defined __arc__
	kact.sa_flags |= SA_RESTORER;
	kact.sa_restorer = restore_rt;
#endif

#ifdef __sparc__
	unsigned long stub = 0;
# if defined __arch64__ || defined __sparcv9
	stub = ((unsigned long) &__rt_sig_stub) - 8;
# else /* sparc32 */
	if ((kact.sa_flags & SA_SIGINFO) != 0)
		stub = ((unsigned long) &__rt_sig_stub) - 8;
	else
		stub = ((unsigned long) &__sig_stub) - 8;
# endif
#endif


#ifdef __sparc__
	ret = TST_SYSCALL(__NR_rt_sigaction, signum,
			kact_p, koact_p,
			stub, sigsetsize);
#else
	ret = TST_SYSCALL(__NR_rt_sigaction, signum,
			kact_p, koact_p,
			sigsetsize);
#endif

	if (ret >= 0) {
		if (oact && (oact != INVAL_SA_PTR)) {
			oact->sa_handler = koact.k_sa_handler;
			memcpy(&oact->sa_mask, &koact.sa_mask,
				sizeof(sigset_t));
			oact->sa_flags = koact.sa_flags;
#ifdef HAVE_SA_RESTORER
			oact->sa_restorer = koact.sa_restorer;
#endif
		}
	}

	return ret;
}

#endif /* LTP_RT_SIGACTION_H */