1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2021 ARM Limited
4  *
5  * Verify that the ZA register context in signal frames is set up as
6  * expected.
7  */
8 
9 #include <kselftest.h>
10 #include <signal.h>
11 #include <ucontext.h>
12 #include <sys/prctl.h>
13 
14 #include "test_signals_utils.h"
15 #include "sve_helpers.h"
16 #include "testcases.h"
17 
18 static union {
19 	ucontext_t uc;
20 	char buf[1024 * 128];
21 } context;
22 
23 static bool sme_get_vls(struct tdescr *td)
24 {
25 	int res = sve_fill_vls(VLS_USE_SME, 1);
26 
27 	if (!res)
28 		return true;
29 
30 	if (res == KSFT_SKIP)
31 		td->result = KSFT_SKIP;
32 
33 	return false;
34 }
35 
36 static int do_one_sme_vl(struct tdescr *td, siginfo_t *si, ucontext_t *uc,
37 			 unsigned int vl)
38 {
39 	size_t offset;
40 	struct _aarch64_ctx *head = GET_BUF_RESV_HEAD(context);
41 	struct za_context *za;
42 
43 	fprintf(stderr, "Testing VL %d\n", vl);
44 
45 	if (prctl(PR_SME_SET_VL, vl) != vl) {
46 		fprintf(stderr, "Failed to set VL\n");
47 		return 1;
48 	}
49 
50 	/*
51 	 * Get a signal context which should have a SVE frame and registers
52 	 * in it.
53 	 */
54 	if (!get_current_context(td, &context.uc, sizeof(context)))
55 		return 1;
56 
57 	head = get_header(head, ZA_MAGIC, GET_BUF_RESV_SIZE(context), &offset);
58 	if (!head) {
59 		fprintf(stderr, "No ZA context\n");
60 		return 1;
61 	}
62 
63 	za = (struct za_context *)head;
64 	if (za->vl != vl) {
65 		fprintf(stderr, "Got VL %d, expected %d\n", za->vl, vl);
66 		return 1;
67 	}
68 
69 	if (head->size != ZA_SIG_REGS_OFFSET) {
70 		fprintf(stderr, "Context size %u, expected %lu\n",
71 			head->size, ZA_SIG_REGS_OFFSET);
72 		return 1;
73 	}
74 
75 	/* The actual size validation is done in get_current_context() */
76 	fprintf(stderr, "Got expected size %u and VL %d\n",
77 		head->size, za->vl);
78 
79 	return 0;
80 }
81 
82 static int sme_regs(struct tdescr *td, siginfo_t *si, ucontext_t *uc)
83 {
84 	int i;
85 
86 	for (i = 0; i < nvls; i++) {
87 		if (do_one_sme_vl(td, si, uc, vls[i]))
88 			return 1;
89 	}
90 
91 	td->pass = 1;
92 
93 	return 0;
94 }
95 
96 struct tdescr tde = {
97 	.name = "ZA registers - ZA disabled",
98 	.descr = "Check ZA context with ZA disabled",
99 	.feats_required = FEAT_SME,
100 	.timeout = 3,
101 	.init = sme_get_vls,
102 	.run = sme_regs,
103 };
104