File Index
Files and directories
Sign in
FILE

sigreturn.c

File preview with syntax highlighting.

linux-6.19.14/tools/testing/selftests/riscv/sigreturn/sigreturn.c C
 1// SPDX-License-Identifier: GPL-2.0-only
 2#include <signal.h>
 3#include <stdio.h>
 4#include <stdlib.h>
 5#include <ucontext.h>
 6#include <linux/ptrace.h>
 7#include "kselftest_harness.h"
 8
 9#define RISCV_V_MAGIC		0x53465457
10#define DEFAULT_VALUE		2
11#define SIGNAL_HANDLER_OVERRIDE	3
12
13static void simple_handle(int sig_no, siginfo_t *info, void *vcontext)
14{
15	ucontext_t *context = vcontext;
16
17	context->uc_mcontext.__gregs[REG_PC] = context->uc_mcontext.__gregs[REG_PC] + 4;
18}
19
20static void vector_override(int sig_no, siginfo_t *info, void *vcontext)
21{
22	ucontext_t *context = vcontext;
23
24	// vector state
25	struct __riscv_extra_ext_header *ext;
26	struct __riscv_v_ext_state *v_ext_state;
27
28	/* Find the vector context. */
29	ext = (void *)(&context->uc_mcontext.__fpregs);
30	if (ext->hdr.magic != RISCV_V_MAGIC) {
31		fprintf(stderr, "bad vector magic: %x\n", ext->hdr.magic);
32		abort();
33	}
34
35	v_ext_state = (void *)((char *)(ext) + sizeof(*ext));
36
37	*(int *)v_ext_state->datap = SIGNAL_HANDLER_OVERRIDE;
38
39	context->uc_mcontext.__gregs[REG_PC] = context->uc_mcontext.__gregs[REG_PC] + 4;
40}
41
42static int vector_sigreturn(int data, void (*handler)(int, siginfo_t *, void *))
43{
44	int after_sigreturn;
45	struct sigaction sig_action = {
46		.sa_sigaction = handler,
47		.sa_flags = SA_SIGINFO
48	};
49
50	sigaction(SIGSEGV, &sig_action, 0);
51
52	asm(".option push				\n\
53		.option		arch, +v		\n\
54		vsetivli	x0, 1, e32, m1, ta, ma	\n\
55		vmv.s.x		v0, %1			\n\
56		# Generate SIGSEGV			\n\
57		lw		a0, 0(x0)		\n\
58		vmv.x.s		%0, v0			\n\
59		.option pop" : "=r" (after_sigreturn) : "r" (data));
60
61	return after_sigreturn;
62}
63
64TEST(vector_restore)
65{
66	int result;
67
68	result = vector_sigreturn(DEFAULT_VALUE, &simple_handle);
69
70	EXPECT_EQ(DEFAULT_VALUE, result);
71}
72
73TEST(vector_restore_signal_handler_override)
74{
75	int result;
76
77	result = vector_sigreturn(DEFAULT_VALUE, &vector_override);
78
79	EXPECT_EQ(SIGNAL_HANDLER_OVERRIDE, result);
80}
81
82TEST_HARNESS_MAIN
~/ Read-only file preview.