xref: /openbmc/linux/tools/testing/selftests/arm64/signal/test_signals_utils.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
1f96bf434SCristian Marussi // SPDX-License-Identifier: GPL-2.0
2f96bf434SCristian Marussi /* Copyright (C) 2019 ARM Limited */
3f96bf434SCristian Marussi 
4f96bf434SCristian Marussi #include <stdio.h>
5f96bf434SCristian Marussi #include <stdlib.h>
6f96bf434SCristian Marussi #include <signal.h>
7f96bf434SCristian Marussi #include <string.h>
8f96bf434SCristian Marussi #include <unistd.h>
9f96bf434SCristian Marussi #include <assert.h>
10f96bf434SCristian Marussi #include <sys/auxv.h>
11f96bf434SCristian Marussi #include <linux/auxvec.h>
12f96bf434SCristian Marussi #include <ucontext.h>
13f96bf434SCristian Marussi 
1434306b05SCristian Marussi #include <asm/unistd.h>
1534306b05SCristian Marussi 
16837387a2SCristian Marussi #include <kselftest.h>
17837387a2SCristian Marussi 
18f96bf434SCristian Marussi #include "test_signals.h"
19f96bf434SCristian Marussi #include "test_signals_utils.h"
20f96bf434SCristian Marussi #include "testcases/testcases.h"
21f96bf434SCristian Marussi 
2234306b05SCristian Marussi 
23f96bf434SCristian Marussi extern struct tdescr *current;
24f96bf434SCristian Marussi 
2534306b05SCristian Marussi static int sig_copyctx = SIGTRAP;
2634306b05SCristian Marussi 
27f96bf434SCristian Marussi static char const *const feats_names[FMAX_END] = {
28f96bf434SCristian Marussi 	" SSBS ",
29d4e4dc4fSMark Brown 	" SVE ",
304963aeb3SMark Brown 	" SME ",
314963aeb3SMark Brown 	" FA64 ",
3263829373SMark Brown 	" SME2 ",
33f96bf434SCristian Marussi };
34f96bf434SCristian Marussi 
35f96bf434SCristian Marussi #define MAX_FEATS_SZ	128
36f96bf434SCristian Marussi static char feats_string[MAX_FEATS_SZ];
37f96bf434SCristian Marussi 
feats_to_string(unsigned long feats)38f96bf434SCristian Marussi static inline char *feats_to_string(unsigned long feats)
39f96bf434SCristian Marussi {
40f96bf434SCristian Marussi 	size_t flen = MAX_FEATS_SZ - 1;
41f96bf434SCristian Marussi 
4232de73e8SMark Brown 	feats_string[0] = '\0';
4332de73e8SMark Brown 
44f96bf434SCristian Marussi 	for (int i = 0; i < FMAX_END; i++) {
45f96bf434SCristian Marussi 		if (feats & (1UL << i)) {
46f96bf434SCristian Marussi 			size_t tlen = strlen(feats_names[i]);
47f96bf434SCristian Marussi 
48f96bf434SCristian Marussi 			assert(flen > tlen);
49f96bf434SCristian Marussi 			flen -= tlen;
50f96bf434SCristian Marussi 			strncat(feats_string, feats_names[i], flen);
51f96bf434SCristian Marussi 		}
52f96bf434SCristian Marussi 	}
53f96bf434SCristian Marussi 
54f96bf434SCristian Marussi 	return feats_string;
55f96bf434SCristian Marussi }
56f96bf434SCristian Marussi 
unblock_signal(int signum)57f96bf434SCristian Marussi static void unblock_signal(int signum)
58f96bf434SCristian Marussi {
59f96bf434SCristian Marussi 	sigset_t sset;
60f96bf434SCristian Marussi 
61f96bf434SCristian Marussi 	sigemptyset(&sset);
62f96bf434SCristian Marussi 	sigaddset(&sset, signum);
63f96bf434SCristian Marussi 	sigprocmask(SIG_UNBLOCK, &sset, NULL);
64f96bf434SCristian Marussi }
65f96bf434SCristian Marussi 
default_result(struct tdescr * td,bool force_exit)66f96bf434SCristian Marussi static void default_result(struct tdescr *td, bool force_exit)
67f96bf434SCristian Marussi {
68837387a2SCristian Marussi 	if (td->result == KSFT_SKIP) {
69837387a2SCristian Marussi 		fprintf(stderr, "==>> completed. SKIP.\n");
70837387a2SCristian Marussi 	} else if (td->pass) {
71f96bf434SCristian Marussi 		fprintf(stderr, "==>> completed. PASS(1)\n");
72837387a2SCristian Marussi 		td->result = KSFT_PASS;
73837387a2SCristian Marussi 	} else {
74f96bf434SCristian Marussi 		fprintf(stdout, "==>> completed. FAIL(0)\n");
75837387a2SCristian Marussi 		td->result = KSFT_FAIL;
76837387a2SCristian Marussi 	}
77837387a2SCristian Marussi 
78f96bf434SCristian Marussi 	if (force_exit)
79837387a2SCristian Marussi 		exit(td->result);
80f96bf434SCristian Marussi }
81f96bf434SCristian Marussi 
82f96bf434SCristian Marussi /*
83f96bf434SCristian Marussi  * The following handle_signal_* helpers are used by main default_handler
84f96bf434SCristian Marussi  * and are meant to return true when signal is handled successfully:
85f96bf434SCristian Marussi  * when false is returned instead, it means that the signal was somehow
86f96bf434SCristian Marussi  * unexpected in that context and it was NOT handled; default_handler will
87f96bf434SCristian Marussi  * take care of such unexpected situations.
88f96bf434SCristian Marussi  */
89f96bf434SCristian Marussi 
handle_signal_unsupported(struct tdescr * td,siginfo_t * si,void * uc)90f96bf434SCristian Marussi static bool handle_signal_unsupported(struct tdescr *td,
91f96bf434SCristian Marussi 				      siginfo_t *si, void *uc)
92f96bf434SCristian Marussi {
93f96bf434SCristian Marussi 	if (feats_ok(td))
94f96bf434SCristian Marussi 		return false;
95f96bf434SCristian Marussi 
96f96bf434SCristian Marussi 	/* Mangling PC to avoid loops on original SIGILL */
97f96bf434SCristian Marussi 	((ucontext_t *)uc)->uc_mcontext.pc += 4;
98f96bf434SCristian Marussi 
99f96bf434SCristian Marussi 	if (!td->initialized) {
100f96bf434SCristian Marussi 		fprintf(stderr,
101f96bf434SCristian Marussi 			"Got SIG_UNSUPP @test_init. Ignore.\n");
102f96bf434SCristian Marussi 	} else {
103f96bf434SCristian Marussi 		fprintf(stderr,
104f96bf434SCristian Marussi 			"-- RX SIG_UNSUPP on unsupported feat...OK\n");
105f96bf434SCristian Marussi 		td->pass = 1;
106f96bf434SCristian Marussi 		default_result(current, 1);
107f96bf434SCristian Marussi 	}
108f96bf434SCristian Marussi 
109f96bf434SCristian Marussi 	return true;
110f96bf434SCristian Marussi }
111f96bf434SCristian Marussi 
handle_signal_trigger(struct tdescr * td,siginfo_t * si,void * uc)112f96bf434SCristian Marussi static bool handle_signal_trigger(struct tdescr *td,
113f96bf434SCristian Marussi 				  siginfo_t *si, void *uc)
114f96bf434SCristian Marussi {
115f96bf434SCristian Marussi 	td->triggered = 1;
116f96bf434SCristian Marussi 	/* ->run was asserted NON-NULL in test_setup() already */
117f96bf434SCristian Marussi 	td->run(td, si, uc);
118f96bf434SCristian Marussi 
119f96bf434SCristian Marussi 	return true;
120f96bf434SCristian Marussi }
121f96bf434SCristian Marussi 
handle_signal_ok(struct tdescr * td,siginfo_t * si,void * uc)122f96bf434SCristian Marussi static bool handle_signal_ok(struct tdescr *td,
123f96bf434SCristian Marussi 			     siginfo_t *si, void *uc)
124f96bf434SCristian Marussi {
125f96bf434SCristian Marussi 	/*
126f96bf434SCristian Marussi 	 * it's a bug in the test code when this assert fail:
127f96bf434SCristian Marussi 	 * if sig_trig was defined, it must have been used before getting here.
128f96bf434SCristian Marussi 	 */
129f96bf434SCristian Marussi 	assert(!td->sig_trig || td->triggered);
130f96bf434SCristian Marussi 	fprintf(stderr,
131f96bf434SCristian Marussi 		"SIG_OK -- SP:0x%llX  si_addr@:%p  si_code:%d  token@:%p  offset:%ld\n",
132f96bf434SCristian Marussi 		((ucontext_t *)uc)->uc_mcontext.sp,
133f96bf434SCristian Marussi 		si->si_addr, si->si_code, td->token, td->token - si->si_addr);
134f96bf434SCristian Marussi 	/*
135f96bf434SCristian Marussi 	 * fake_sigreturn tests, which have sanity_enabled=1, set, at the very
136f96bf434SCristian Marussi 	 * last time, the token field to the SP address used to place the fake
137f96bf434SCristian Marussi 	 * sigframe: so token==0 means we never made it to the end,
138f96bf434SCristian Marussi 	 * segfaulting well-before, and the test is possibly broken.
139f96bf434SCristian Marussi 	 */
140f96bf434SCristian Marussi 	if (!td->sanity_disabled && !td->token) {
141f96bf434SCristian Marussi 		fprintf(stdout,
142f96bf434SCristian Marussi 			"current->token ZEROED...test is probably broken!\n");
143f96bf434SCristian Marussi 		abort();
144f96bf434SCristian Marussi 	}
145f96bf434SCristian Marussi 	/*
146f96bf434SCristian Marussi 	 * Trying to narrow down the SEGV to the ones generated by Kernel itself
147f96bf434SCristian Marussi 	 * via arm64_notify_segfault(). This is a best-effort check anyway, and
148f96bf434SCristian Marussi 	 * the si_code check may need to change if this aspect of the kernel
149f96bf434SCristian Marussi 	 * ABI changes.
150f96bf434SCristian Marussi 	 */
151f96bf434SCristian Marussi 	if (td->sig_ok == SIGSEGV && si->si_code != SEGV_ACCERR) {
152f96bf434SCristian Marussi 		fprintf(stdout,
153f96bf434SCristian Marussi 			"si_code != SEGV_ACCERR...test is probably broken!\n");
154f96bf434SCristian Marussi 		abort();
155f96bf434SCristian Marussi 	}
156f96bf434SCristian Marussi 	td->pass = 1;
157f96bf434SCristian Marussi 	/*
158f96bf434SCristian Marussi 	 * Some tests can lead to SEGV loops: in such a case we want to
159f96bf434SCristian Marussi 	 * terminate immediately exiting straight away; some others are not
160f96bf434SCristian Marussi 	 * supposed to outlive the signal handler code, due to the content of
161f96bf434SCristian Marussi 	 * the fake sigframe which caused the signal itself.
162f96bf434SCristian Marussi 	 */
163f96bf434SCristian Marussi 	default_result(current, 1);
164f96bf434SCristian Marussi 
165f96bf434SCristian Marussi 	return true;
166f96bf434SCristian Marussi }
167f96bf434SCristian Marussi 
handle_signal_copyctx(struct tdescr * td,siginfo_t * si,void * uc_in)16834306b05SCristian Marussi static bool handle_signal_copyctx(struct tdescr *td,
169e40422c9SMark Brown 				  siginfo_t *si, void *uc_in)
17034306b05SCristian Marussi {
171e40422c9SMark Brown 	ucontext_t *uc = uc_in;
1722fa1116bSMark Brown 	struct _aarch64_ctx *head;
1732fa1116bSMark Brown 	struct extra_context *extra, *copied_extra;
1742fa1116bSMark Brown 	size_t offset = 0;
1752fa1116bSMark Brown 	size_t to_copy;
176e40422c9SMark Brown 
177e40422c9SMark Brown 	ASSERT_GOOD_CONTEXT(uc);
178e40422c9SMark Brown 
17934306b05SCristian Marussi 	/* Mangling PC to avoid loops on original BRK instr */
180e40422c9SMark Brown 	uc->uc_mcontext.pc += 4;
1812fa1116bSMark Brown 
1822fa1116bSMark Brown 	/*
1832fa1116bSMark Brown 	 * Check for an preserve any extra data too with fixups.
1842fa1116bSMark Brown 	 */
1852fa1116bSMark Brown 	head = (struct _aarch64_ctx *)uc->uc_mcontext.__reserved;
1862fa1116bSMark Brown 	head = get_header(head, EXTRA_MAGIC, td->live_sz, &offset);
1872fa1116bSMark Brown 	if (head) {
1882fa1116bSMark Brown 		extra = (struct extra_context *)head;
1892fa1116bSMark Brown 
1902fa1116bSMark Brown 		/*
1912fa1116bSMark Brown 		 * The extra buffer must be immediately after the
1922fa1116bSMark Brown 		 * extra_context and a 16 byte terminator. Include it
1932fa1116bSMark Brown 		 * in the copy, this was previously validated in
1942fa1116bSMark Brown 		 * ASSERT_GOOD_CONTEXT().
1952fa1116bSMark Brown 		 */
1966012b820SMark Brown 		to_copy = __builtin_offsetof(ucontext_t,
1976012b820SMark Brown 					     uc_mcontext.__reserved);
1986012b820SMark Brown 		to_copy += offset + sizeof(struct extra_context) + 16;
1996012b820SMark Brown 		to_copy += extra->size;
2002fa1116bSMark Brown 		copied_extra = (struct extra_context *)&(td->live_uc->uc_mcontext.__reserved[offset]);
2012fa1116bSMark Brown 	} else {
2022fa1116bSMark Brown 		copied_extra = NULL;
2032fa1116bSMark Brown 		to_copy = sizeof(ucontext_t);
2042fa1116bSMark Brown 	}
2052fa1116bSMark Brown 
2062fa1116bSMark Brown 	if (to_copy > td->live_sz) {
2072fa1116bSMark Brown 		fprintf(stderr,
2082fa1116bSMark Brown 			"Not enough space to grab context, %lu/%lu bytes\n",
2092fa1116bSMark Brown 			td->live_sz, to_copy);
2102fa1116bSMark Brown 		return false;
2112fa1116bSMark Brown 	}
2122fa1116bSMark Brown 
2132fa1116bSMark Brown 	memcpy(td->live_uc, uc, to_copy);
2142fa1116bSMark Brown 
2152fa1116bSMark Brown 	/*
2162fa1116bSMark Brown 	 * If there was any EXTRA_CONTEXT fix up the size to be the
2172fa1116bSMark Brown 	 * struct extra_context and the following terminator record,
2182fa1116bSMark Brown 	 * this means that the rest of the code does not need to have
2192fa1116bSMark Brown 	 * special handling for the record and we don't need to fix up
2202fa1116bSMark Brown 	 * datap for the new location.
2212fa1116bSMark Brown 	 */
2222fa1116bSMark Brown 	if (copied_extra)
2232fa1116bSMark Brown 		copied_extra->head.size = sizeof(*copied_extra) + 16;
2242fa1116bSMark Brown 
22534306b05SCristian Marussi 	td->live_uc_valid = 1;
22634306b05SCristian Marussi 	fprintf(stderr,
2272fa1116bSMark Brown 		"%lu byte GOOD CONTEXT grabbed from sig_copyctx handler\n",
2282fa1116bSMark Brown 		to_copy);
22934306b05SCristian Marussi 
23034306b05SCristian Marussi 	return true;
23134306b05SCristian Marussi }
23234306b05SCristian Marussi 
default_handler(int signum,siginfo_t * si,void * uc)233f96bf434SCristian Marussi static void default_handler(int signum, siginfo_t *si, void *uc)
234f96bf434SCristian Marussi {
235f96bf434SCristian Marussi 	if (current->sig_unsupp && signum == current->sig_unsupp &&
236f96bf434SCristian Marussi 	    handle_signal_unsupported(current, si, uc)) {
237f96bf434SCristian Marussi 		fprintf(stderr, "Handled SIG_UNSUPP\n");
238f96bf434SCristian Marussi 	} else if (current->sig_trig && signum == current->sig_trig &&
239f96bf434SCristian Marussi 		   handle_signal_trigger(current, si, uc)) {
240f96bf434SCristian Marussi 		fprintf(stderr, "Handled SIG_TRIG\n");
241f96bf434SCristian Marussi 	} else if (current->sig_ok && signum == current->sig_ok &&
242f96bf434SCristian Marussi 		   handle_signal_ok(current, si, uc)) {
243f96bf434SCristian Marussi 		fprintf(stderr, "Handled SIG_OK\n");
24434306b05SCristian Marussi 	} else if (signum == sig_copyctx && current->live_uc &&
24534306b05SCristian Marussi 		   handle_signal_copyctx(current, si, uc)) {
24634306b05SCristian Marussi 		fprintf(stderr, "Handled SIG_COPYCTX\n");
247f96bf434SCristian Marussi 	} else {
248f96bf434SCristian Marussi 		if (signum == SIGALRM && current->timeout) {
249f96bf434SCristian Marussi 			fprintf(stderr, "-- Timeout !\n");
250f96bf434SCristian Marussi 		} else {
251f96bf434SCristian Marussi 			fprintf(stderr,
252*833b0f07SMark Brown 				"-- RX UNEXPECTED SIGNAL: %d code %d address %p\n",
253*833b0f07SMark Brown 				signum, si->si_code, si->si_addr);
254f96bf434SCristian Marussi 		}
255f96bf434SCristian Marussi 		default_result(current, 1);
256f96bf434SCristian Marussi 	}
257f96bf434SCristian Marussi }
258f96bf434SCristian Marussi 
default_setup(struct tdescr * td)259f96bf434SCristian Marussi static int default_setup(struct tdescr *td)
260f96bf434SCristian Marussi {
261f96bf434SCristian Marussi 	struct sigaction sa;
262f96bf434SCristian Marussi 
263f96bf434SCristian Marussi 	sa.sa_sigaction = default_handler;
264f96bf434SCristian Marussi 	sa.sa_flags = SA_SIGINFO | SA_RESTART;
265f96bf434SCristian Marussi 	sa.sa_flags |= td->sa_flags;
266f96bf434SCristian Marussi 	sigemptyset(&sa.sa_mask);
267f96bf434SCristian Marussi 	/* uncatchable signals naturally skipped ... */
268f96bf434SCristian Marussi 	for (int sig = 1; sig < 32; sig++)
269f96bf434SCristian Marussi 		sigaction(sig, &sa, NULL);
270f96bf434SCristian Marussi 	/*
271f96bf434SCristian Marussi 	 * RT Signals default disposition is Term but they cannot be
272f96bf434SCristian Marussi 	 * generated by the Kernel in response to our tests; so just catch
273f96bf434SCristian Marussi 	 * them all and report them as UNEXPECTED signals.
274f96bf434SCristian Marussi 	 */
275f96bf434SCristian Marussi 	for (int sig = SIGRTMIN; sig <= SIGRTMAX; sig++)
276f96bf434SCristian Marussi 		sigaction(sig, &sa, NULL);
277f96bf434SCristian Marussi 
278f96bf434SCristian Marussi 	/* just in case...unblock explicitly all we need */
279f96bf434SCristian Marussi 	if (td->sig_trig)
280f96bf434SCristian Marussi 		unblock_signal(td->sig_trig);
281f96bf434SCristian Marussi 	if (td->sig_ok)
282f96bf434SCristian Marussi 		unblock_signal(td->sig_ok);
283f96bf434SCristian Marussi 	if (td->sig_unsupp)
284f96bf434SCristian Marussi 		unblock_signal(td->sig_unsupp);
285f96bf434SCristian Marussi 
286f96bf434SCristian Marussi 	if (td->timeout) {
287f96bf434SCristian Marussi 		unblock_signal(SIGALRM);
288f96bf434SCristian Marussi 		alarm(td->timeout);
289f96bf434SCristian Marussi 	}
290f96bf434SCristian Marussi 	fprintf(stderr, "Registered handlers for all signals.\n");
291f96bf434SCristian Marussi 
292f96bf434SCristian Marussi 	return 1;
293f96bf434SCristian Marussi }
294f96bf434SCristian Marussi 
default_trigger(struct tdescr * td)295f96bf434SCristian Marussi static inline int default_trigger(struct tdescr *td)
296f96bf434SCristian Marussi {
297f96bf434SCristian Marussi 	return !raise(td->sig_trig);
298f96bf434SCristian Marussi }
299f96bf434SCristian Marussi 
test_init(struct tdescr * td)300837387a2SCristian Marussi int test_init(struct tdescr *td)
301f96bf434SCristian Marussi {
30234306b05SCristian Marussi 	if (td->sig_trig == sig_copyctx) {
30334306b05SCristian Marussi 		fprintf(stdout,
30434306b05SCristian Marussi 			"Signal %d is RESERVED, cannot be used as a trigger. Aborting\n",
30534306b05SCristian Marussi 			sig_copyctx);
30634306b05SCristian Marussi 		return 0;
30734306b05SCristian Marussi 	}
30834306b05SCristian Marussi 	/* just in case */
30934306b05SCristian Marussi 	unblock_signal(sig_copyctx);
31034306b05SCristian Marussi 
311f96bf434SCristian Marussi 	td->minsigstksz = getauxval(AT_MINSIGSTKSZ);
312f96bf434SCristian Marussi 	if (!td->minsigstksz)
313f96bf434SCristian Marussi 		td->minsigstksz = MINSIGSTKSZ;
314f96bf434SCristian Marussi 	fprintf(stderr, "Detected MINSTKSIGSZ:%d\n", td->minsigstksz);
315f96bf434SCristian Marussi 
31632de73e8SMark Brown 	if (td->feats_required || td->feats_incompatible) {
317f96bf434SCristian Marussi 		td->feats_supported = 0;
318f96bf434SCristian Marussi 		/*
319f96bf434SCristian Marussi 		 * Checking for CPU required features using both the
320f96bf434SCristian Marussi 		 * auxval and the arm64 MRS Emulation to read sysregs.
321f96bf434SCristian Marussi 		 */
322f96bf434SCristian Marussi 		if (getauxval(AT_HWCAP) & HWCAP_SSBS)
323f96bf434SCristian Marussi 			td->feats_supported |= FEAT_SSBS;
324d4e4dc4fSMark Brown 		if (getauxval(AT_HWCAP) & HWCAP_SVE)
325d4e4dc4fSMark Brown 			td->feats_supported |= FEAT_SVE;
3264963aeb3SMark Brown 		if (getauxval(AT_HWCAP2) & HWCAP2_SME)
3274963aeb3SMark Brown 			td->feats_supported |= FEAT_SME;
3284963aeb3SMark Brown 		if (getauxval(AT_HWCAP2) & HWCAP2_SME_FA64)
3294963aeb3SMark Brown 			td->feats_supported |= FEAT_SME_FA64;
33063829373SMark Brown 		if (getauxval(AT_HWCAP2) & HWCAP2_SME2)
33163829373SMark Brown 			td->feats_supported |= FEAT_SME2;
3320e3dbf76SCristian Marussi 		if (feats_ok(td)) {
33332de73e8SMark Brown 			if (td->feats_required & td->feats_supported)
334f96bf434SCristian Marussi 				fprintf(stderr,
335f96bf434SCristian Marussi 					"Required Features: [%s] supported\n",
336f96bf434SCristian Marussi 					feats_to_string(td->feats_required &
337f96bf434SCristian Marussi 							td->feats_supported));
33832de73e8SMark Brown 			if (!(td->feats_incompatible & td->feats_supported))
33932de73e8SMark Brown 				fprintf(stderr,
34032de73e8SMark Brown 					"Incompatible Features: [%s] absent\n",
34132de73e8SMark Brown 					feats_to_string(td->feats_incompatible));
3420e3dbf76SCristian Marussi 		} else {
34332de73e8SMark Brown 			if ((td->feats_required & td->feats_supported) !=
34432de73e8SMark Brown 			    td->feats_supported)
345f96bf434SCristian Marussi 				fprintf(stderr,
346f96bf434SCristian Marussi 					"Required Features: [%s] NOT supported\n",
347f96bf434SCristian Marussi 					feats_to_string(td->feats_required &
348f96bf434SCristian Marussi 							~td->feats_supported));
34932de73e8SMark Brown 			if (td->feats_incompatible & td->feats_supported)
35032de73e8SMark Brown 				fprintf(stderr,
35132de73e8SMark Brown 					"Incompatible Features: [%s] supported\n",
35232de73e8SMark Brown 					feats_to_string(td->feats_incompatible &
35332de73e8SMark Brown 							~td->feats_supported));
35432de73e8SMark Brown 
35532de73e8SMark Brown 
3560e3dbf76SCristian Marussi 			td->result = KSFT_SKIP;
3570e3dbf76SCristian Marussi 			return 0;
3580e3dbf76SCristian Marussi 		}
359f96bf434SCristian Marussi 	}
360f96bf434SCristian Marussi 
361837387a2SCristian Marussi 	/* Perform test specific additional initialization */
362837387a2SCristian Marussi 	if (td->init && !td->init(td)) {
363837387a2SCristian Marussi 		fprintf(stderr, "FAILED Testcase initialization.\n");
364837387a2SCristian Marussi 		return 0;
365837387a2SCristian Marussi 	}
366f96bf434SCristian Marussi 	td->initialized = 1;
367837387a2SCristian Marussi 	fprintf(stderr, "Testcase initialized.\n");
368837387a2SCristian Marussi 
369f96bf434SCristian Marussi 	return 1;
370f96bf434SCristian Marussi }
371f96bf434SCristian Marussi 
test_setup(struct tdescr * td)372f96bf434SCristian Marussi int test_setup(struct tdescr *td)
373f96bf434SCristian Marussi {
374f96bf434SCristian Marussi 	/* assert core invariants symptom of a rotten testcase */
375f96bf434SCristian Marussi 	assert(current);
376f96bf434SCristian Marussi 	assert(td);
377f96bf434SCristian Marussi 	assert(td->name);
378f96bf434SCristian Marussi 	assert(td->run);
379f96bf434SCristian Marussi 
380837387a2SCristian Marussi 	/* Default result is FAIL if test setup fails */
381837387a2SCristian Marussi 	td->result = KSFT_FAIL;
382f96bf434SCristian Marussi 	if (td->setup)
383f96bf434SCristian Marussi 		return td->setup(td);
384f96bf434SCristian Marussi 	else
385f96bf434SCristian Marussi 		return default_setup(td);
386f96bf434SCristian Marussi }
387f96bf434SCristian Marussi 
test_run(struct tdescr * td)388f96bf434SCristian Marussi int test_run(struct tdescr *td)
389f96bf434SCristian Marussi {
390f96bf434SCristian Marussi 	if (td->trigger)
391f96bf434SCristian Marussi 		return td->trigger(td);
3929331a604SMark Brown 	else if (td->sig_trig)
393f96bf434SCristian Marussi 		return default_trigger(td);
3949331a604SMark Brown 	else
395f96bf434SCristian Marussi 		return td->run(td, NULL, NULL);
396f96bf434SCristian Marussi }
397f96bf434SCristian Marussi 
test_result(struct tdescr * td)398f96bf434SCristian Marussi void test_result(struct tdescr *td)
399f96bf434SCristian Marussi {
400837387a2SCristian Marussi 	if (td->initialized && td->result != KSFT_SKIP && td->check_result)
401f96bf434SCristian Marussi 		td->check_result(td);
402f96bf434SCristian Marussi 	default_result(td, 0);
403f96bf434SCristian Marussi }
404f96bf434SCristian Marussi 
test_cleanup(struct tdescr * td)405f96bf434SCristian Marussi void test_cleanup(struct tdescr *td)
406f96bf434SCristian Marussi {
407f96bf434SCristian Marussi 	if (td->cleanup)
408f96bf434SCristian Marussi 		td->cleanup(td);
409f96bf434SCristian Marussi }
410