File: prctl-riscv-v-ctrl.c

package info (click to toggle)
strace 6.13%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 71,488 kB
  • sloc: ansic: 176,497; sh: 9,675; makefile: 4,133; cpp: 885; awk: 353; perl: 267; exp: 62; sed: 9
file content (165 lines) | stat: -rw-r--r-- 4,155 bytes parent folder | download | duplicates (6)
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
/*
 * Check decoding of prctl PR_RISCV_V_SET_CONTROL and PR_RISCV_V_GET_CONTROL
 * operations.
 *
 * Copyright (c) 2021-2024 The strace developers.
 * All rights reserved.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "tests.h"
#include "scno.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/prctl.h>

#if !XLAT_RAW
static void
print_riscv_v_ctrl_val(kernel_ulong_t val)
{
	switch (val) {
	case PR_RISCV_V_VSTATE_CTRL_DEFAULT:
		printf("PR_RISCV_V_VSTATE_CTRL_DEFAULT");
		break;
	case PR_RISCV_V_VSTATE_CTRL_OFF:
		printf("PR_RISCV_V_VSTATE_CTRL_OFF");
		break;
	case PR_RISCV_V_VSTATE_CTRL_ON:
		printf("PR_RISCV_V_VSTATE_CTRL_ON");
		break;
	default:
		printf("%#llx", (unsigned long long) val);
	}
}

static void
print_riscv_v_ctrl(kernel_ulong_t arg)
{
	const kernel_ulong_t cur = arg & PR_RISCV_V_VSTATE_CTRL_CUR_MASK;
	const kernel_ulong_t next =
			(arg & PR_RISCV_V_VSTATE_CTRL_NEXT_MASK) >> 2;
	const kernel_ulong_t inherit = arg & PR_RISCV_V_VSTATE_CTRL_INHERIT;
	const kernel_ulong_t rest = arg & ~PR_RISCV_V_VSTATE_CTRL_MASK;

	print_riscv_v_ctrl_val(cur);
	printf("|");
	print_riscv_v_ctrl_val(next);
	printf("<<2|%sPR_RISCV_V_VSTATE_CTRL_INHERIT", inherit ? "" : "!");
	if (rest)
		printf("|%#llx", (unsigned long long) rest);
}
#endif /* !XLAT_RAW */

#ifdef INJECT_RETVAL
# define INJ_STR " (INJECTED)"
#else
# define INJ_STR ""
#endif

#if SIZEOF_KERNEL_LONG_T == 8
# define HIBITS(a) a
#else
# define HIBITS(a)
#endif


int
main(int argc, char *argv[])
{
	prctl_marker();

#ifdef INJECT_RETVAL
	unsigned long num_skip;
	long inject_retval;
	bool locked = false;

	if (argc < 3)
		error_msg_and_fail("Usage: %s NUM_SKIP INJECT_RETVAL", argv[0]);

	num_skip = strtoul(argv[1], NULL, 0);
	inject_retval = strtol(argv[2], NULL, 0);

	for (size_t i = 0; i < num_skip; i++) {
		if (prctl_marker() != inject_retval)
			continue;

		locked = true;
		break;
	}

	if (!locked)
		error_msg_and_fail("Have not locked on prctl(-1, -2, -3, -4"
				   ", -5) returning %ld", inject_retval);
#endif /* INJECT_RETVAL */

	static const struct {
		kernel_ulong_t val;
		const char *str;
	} args[] = {
		{ 0, "PR_RISCV_V_VSTATE_CTRL_DEFAULT"
		     "|PR_RISCV_V_VSTATE_CTRL_DEFAULT<<2"
		     "|!PR_RISCV_V_VSTATE_CTRL_INHERIT" },
		{ 6, "PR_RISCV_V_VSTATE_CTRL_ON"
		     "|PR_RISCV_V_VSTATE_CTRL_OFF<<2"
		     "|!PR_RISCV_V_VSTATE_CTRL_INHERIT" },
		{ 11, "0x3|PR_RISCV_V_VSTATE_CTRL_ON<<2"
		      "|!PR_RISCV_V_VSTATE_CTRL_INHERIT" },
		{ 12, "PR_RISCV_V_VSTATE_CTRL_DEFAULT|0x3<<2"
		      "|!PR_RISCV_V_VSTATE_CTRL_INHERIT" },
		{ 17, "PR_RISCV_V_VSTATE_CTRL_OFF"
		      "|PR_RISCV_V_VSTATE_CTRL_DEFAULT<<2"
		      "|PR_RISCV_V_VSTATE_CTRL_INHERIT" },
		{ 31, "0x3|0x3<<2|PR_RISCV_V_VSTATE_CTRL_INHERIT" },
		{ 32, "PR_RISCV_V_VSTATE_CTRL_DEFAULT"
		      "|PR_RISCV_V_VSTATE_CTRL_DEFAULT<<2"
		      "|!PR_RISCV_V_VSTATE_CTRL_INHERIT|0x20" },
		{ 0x57ae57e9, "PR_RISCV_V_VSTATE_CTRL_OFF"
			      "|PR_RISCV_V_VSTATE_CTRL_ON<<2"
			      "|!PR_RISCV_V_VSTATE_CTRL_INHERIT|0x57ae57e0" },
		{ (kernel_ulong_t) -1LLU, "0x3|0x3<<2"
					  "|PR_RISCV_V_VSTATE_CTRL_INHERIT"
					  "|0x" HIBITS("ffffffff") "ffffffe0" },
	};
	long rc;

	for (size_t i = 0; i < ARRAY_SIZE(args); i++) {
		rc = syscall(__NR_prctl, PR_RISCV_V_SET_CONTROL,
			     args[i].val, 1, 2, 3);
		printf("prctl(" XLAT_KNOWN(0x45, "PR_RISCV_V_SET_CONTROL") ", "
		       XLAT_FMT_LL ") = %s" INJ_STR "\n",
		       XLAT_SEL((unsigned long long) args[i].val, args[i].str),
		       sprintrc(rc));

		rc = syscall(__NR_prctl, PR_RISCV_V_GET_CONTROL, 1, 2, 3, 4);
		const char *errstr = sprintrc(rc);
		printf("prctl(" XLAT_KNOWN(0x46, "PR_RISCV_V_GET_CONTROL")
		       ") = ");
		if (rc >= 0) {
			printf("%#lx", rc);
#if !XLAT_RAW
			size_t j;

			for (j = 0; j < ARRAY_SIZE(args); j++) {
				if (args[j].val == (unsigned long) rc) {
					printf(" (%s)", args[j].str);
					break;
				}
			}

			if (j == ARRAY_SIZE(args)) {
				printf(" (");
				print_riscv_v_ctrl(rc);
				printf(")");
			}
#endif /* !XLAT_RAW */
			puts(INJ_STR);
		} else {
			printf("%s" INJ_STR "\n", errstr);
		}
	}

	puts("+++ exited with 0 +++");
	return 0;
}