1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2021 ARM Limited
4  *
5  * Verify that the SVE register context in signal frames is set up as
6  * 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 sve_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_SVE_SET_VL, vq * 16);
32 		if (vl == -1)
33 			return false;
34 
35 		vl &= PR_SVE_VL_LEN_MASK;
36 
37 		/* Skip missing VLs */
38 		vq = sve_vq_from_vl(vl);
39 
40 		vls[nvls++] = vl;
41 	}
42 
43 	/* We need at least one VL */
44 	if (nvls < 1) {
45 		fprintf(stderr, "Only %d VL supported\n", nvls);
46 		return false;
47 	}
48 
49 	return true;
50 }
51 
52 static void setup_sve_regs(void)
53 {
54 	/* RDVL x16, #1 so we should have SVE regs; real data is TODO */
55 	asm volatile(".inst 0x04bf5030" : : : "x16" );
56 }
57 
58 static int do_one_sve_vl(struct tdescr *td, siginfo_t *si, ucontext_t *uc,
59 			 unsigned int vl)
60 {
61 	size_t offset;
62 	struct _aarch64_ctx *head = GET_BUF_RESV_HEAD(context);
63 	struct sve_context *sve;
64 
65 	fprintf(stderr, "Testing VL %d\n", vl);
66 
67 	if (prctl(PR_SVE_SET_VL, vl) == -1) {
68 		fprintf(stderr, "Failed to set VL\n");
69 		return 1;
70 	}
71 
72 	/*
73 	 * Get a signal context which should have a SVE frame and registers
74 	 * in it.
75 	 */
76 	setup_sve_regs();
77 	if (!get_current_context(td, &context.uc, sizeof(context)))
78 		return 1;
79 
80 	head = get_header(head, SVE_MAGIC, GET_BUF_RESV_SIZE(context),
81 			  &offset);
82 	if (!head) {
83 		fprintf(stderr, "No SVE context\n");
84 		return 1;
85 	}
86 
87 	sve = (struct sve_context *)head;
88 	if (sve->vl != vl) {
89 		fprintf(stderr, "Got VL %d, expected %d\n", sve->vl, vl);
90 		return 1;
91 	}
92 
93 	/* The actual size validation is done in get_current_context() */
94 	fprintf(stderr, "Got expected size %u and VL %d\n",
95 		head->size, sve->vl);
96 
97 	return 0;
98 }
99 
100 static int sve_regs(struct tdescr *td, siginfo_t *si, ucontext_t *uc)
101 {
102 	int i;
103 
104 	for (i = 0; i < nvls; i++) {
105 		if (do_one_sve_vl(td, si, uc, vls[i]))
106 			return 1;
107 	}
108 
109 	td->pass = 1;
110 
111 	return 0;
112 }
113 
114 struct tdescr tde = {
115 	.name = "SVE registers",
116 	.descr = "Check that we get the right SVE registers reported",
117 	.feats_required = FEAT_SVE,
118 	.timeout = 3,
119 	.init = sve_get_vls,
120 	.run = sve_regs,
121 };
122