1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2021 ARM Limited
4  *
5  * Verify that the streaming SVE register context in signal frames is
6  * set up as expected.
7  */
8 
9 #include <signal.h>
10 #include <ucontext.h>
11 #include <sys/prctl.h>
12 
13 #include "test_signals_utils.h"
14 #include "testcases.h"
15 
16 static union {
17 	ucontext_t uc;
18 	char buf[1024 * 64];
19 } context;
20 static unsigned int vls[SVE_VQ_MAX];
21 unsigned int nvls = 0;
22 
23 static bool sme_get_vls(struct tdescr *td)
24 {
25 	int vq, vl;
26 
27 	/*
28 	 * Enumerate up to SVE_VQ_MAX vector lengths
29 	 */
30 	for (vq = SVE_VQ_MAX; vq > 0; --vq) {
31 		vl = prctl(PR_SME_SET_VL, vq * 16);
32 		if (vl == -1)
33 			return false;
34 
35 		vl &= PR_SME_VL_LEN_MASK;
36 
37 		/* Did we find the lowest supported VL? */
38 		if (vq < sve_vq_from_vl(vl))
39 			break;
40 
41 		/* Skip missing VLs */
42 		vq = sve_vq_from_vl(vl);
43 
44 		vls[nvls++] = vl;
45 	}
46 
47 	/* We need at least one VL */
48 	if (nvls < 1) {
49 		fprintf(stderr, "Only %d VL supported\n", nvls);
50 		return false;
51 	}
52 
53 	return true;
54 }
55 
56 static void setup_ssve_regs(void)
57 {
58 	/* smstart sm; real data is TODO */
59 	asm volatile(".inst 0xd503437f" : : : );
60 }
61 
62 static int do_one_sme_vl(struct tdescr *td, siginfo_t *si, ucontext_t *uc,
63 			 unsigned int vl)
64 {
65 	size_t offset;
66 	struct _aarch64_ctx *head = GET_BUF_RESV_HEAD(context);
67 	struct sve_context *ssve;
68 	int ret;
69 
70 	fprintf(stderr, "Testing VL %d\n", vl);
71 
72 	ret = prctl(PR_SME_SET_VL, vl);
73 	if (ret != vl) {
74 		fprintf(stderr, "Failed to set VL, got %d\n", ret);
75 		return 1;
76 	}
77 
78 	/*
79 	 * Get a signal context which should have a SVE frame and registers
80 	 * in it.
81 	 */
82 	setup_ssve_regs();
83 	if (!get_current_context(td, &context.uc, sizeof(context)))
84 		return 1;
85 
86 	head = get_header(head, SVE_MAGIC, GET_BUF_RESV_SIZE(context),
87 			  &offset);
88 	if (!head) {
89 		fprintf(stderr, "No SVE context\n");
90 		return 1;
91 	}
92 
93 	ssve = (struct sve_context *)head;
94 	if (ssve->vl != vl) {
95 		fprintf(stderr, "Got VL %d, expected %d\n", ssve->vl, vl);
96 		return 1;
97 	}
98 
99 	if (!(ssve->flags & SVE_SIG_FLAG_SM)) {
100 		fprintf(stderr, "SVE_SIG_FLAG_SM not set in SVE record\n");
101 		return 1;
102 	}
103 
104 	/* The actual size validation is done in get_current_context() */
105 	fprintf(stderr, "Got expected size %u and VL %d\n",
106 		head->size, ssve->vl);
107 
108 	return 0;
109 }
110 
111 static int sme_regs(struct tdescr *td, siginfo_t *si, ucontext_t *uc)
112 {
113 	int i;
114 
115 	for (i = 0; i < nvls; i++) {
116 		if (do_one_sme_vl(td, si, uc, vls[i]))
117 			return 1;
118 	}
119 
120 	td->pass = 1;
121 
122 	return 0;
123 }
124 
125 struct tdescr tde = {
126 	.name = "Streaming SVE registers",
127 	.descr = "Check that we get the right Streaming SVE registers reported",
128 	.feats_required = FEAT_SME,
129 	.timeout = 3,
130 	.init = sme_get_vls,
131 	.run = sme_regs,
132 };
133