xref: /xnu-11215.61.5/tests/arm_matrix.c (revision 4f1223e81cd707a65cc109d0b8ad6653699da3c4)
1*4f1223e8SApple OSS Distributions /*
2*4f1223e8SApple OSS Distributions  * Copyright (c) 2019 Apple Computer, Inc. All rights reserved.
3*4f1223e8SApple OSS Distributions  *
4*4f1223e8SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*4f1223e8SApple OSS Distributions  *
6*4f1223e8SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*4f1223e8SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*4f1223e8SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*4f1223e8SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*4f1223e8SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*4f1223e8SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*4f1223e8SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*4f1223e8SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*4f1223e8SApple OSS Distributions  *
15*4f1223e8SApple OSS Distributions  * Please obtain a copy of the License at
16*4f1223e8SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*4f1223e8SApple OSS Distributions  *
18*4f1223e8SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*4f1223e8SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*4f1223e8SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*4f1223e8SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*4f1223e8SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*4f1223e8SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*4f1223e8SApple OSS Distributions  * limitations under the License.
25*4f1223e8SApple OSS Distributions  *
26*4f1223e8SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*4f1223e8SApple OSS Distributions  */
28*4f1223e8SApple OSS Distributions /**
29*4f1223e8SApple OSS Distributions  * On devices that support it, this test ensures that a mach exception is
30*4f1223e8SApple OSS Distributions  * generated when a matrix-math exception is triggered, and that the
31*4f1223e8SApple OSS Distributions  * matrix register file is correctly preserved or zeroed on context switch.
32*4f1223e8SApple OSS Distributions  */
33*4f1223e8SApple OSS Distributions 
34*4f1223e8SApple OSS Distributions /*
35*4f1223e8SApple OSS Distributions  * IMPLEMENTATION NOTE:
36*4f1223e8SApple OSS Distributions  *
37*4f1223e8SApple OSS Distributions  * This test code goes to some unusual lengths to avoid calling out to libc or
38*4f1223e8SApple OSS Distributions  * libdarwintest while the CPU is in streaming SVE mode (i.e., between
39*4f1223e8SApple OSS Distributions  * ops->start() and ops->stop()).  Both of these libraries are built with SIMD
40*4f1223e8SApple OSS Distributions  * instructions that will cause the test executable to crash while in streaming
41*4f1223e8SApple OSS Distributions  * SVE mode.
42*4f1223e8SApple OSS Distributions  *
43*4f1223e8SApple OSS Distributions  * Ordinarily this is the wrong way to solve this problem.  Functions that use
44*4f1223e8SApple OSS Distributions  * streaming SVE mode should have annotations telling the compiler so, and the
45*4f1223e8SApple OSS Distributions  * compiler will automatically generate appropriate interworking code.  However
46*4f1223e8SApple OSS Distributions  * this interworking code will stash SME state to memory and temporarily exit
47*4f1223e8SApple OSS Distributions  * streaming SVE mode.  We're specifically testing how xnu manages live SME
48*4f1223e8SApple OSS Distributions  * register state, so we can't let the compiler stash and disable this state
49*4f1223e8SApple OSS Distributions  * behind our backs.
50*4f1223e8SApple OSS Distributions  */
51*4f1223e8SApple OSS Distributions 
52*4f1223e8SApple OSS Distributions #ifdef __arm64__
53*4f1223e8SApple OSS Distributions #include <mach/error.h>
54*4f1223e8SApple OSS Distributions #endif /* __arm64__ */
55*4f1223e8SApple OSS Distributions 
56*4f1223e8SApple OSS Distributions #include <darwintest.h>
57*4f1223e8SApple OSS Distributions #include <pthread.h>
58*4f1223e8SApple OSS Distributions #include <stdlib.h>
59*4f1223e8SApple OSS Distributions #include <mach/mach.h>
60*4f1223e8SApple OSS Distributions #include <mach/thread_status.h>
61*4f1223e8SApple OSS Distributions #include <mach/exception.h>
62*4f1223e8SApple OSS Distributions #include <machine/cpu_capabilities.h>
63*4f1223e8SApple OSS Distributions #include <sys/types.h>
64*4f1223e8SApple OSS Distributions #include <sys/sysctl.h>
65*4f1223e8SApple OSS Distributions 
66*4f1223e8SApple OSS Distributions #include "arm_matrix.h"
67*4f1223e8SApple OSS Distributions #include "exc_helpers.h"
68*4f1223e8SApple OSS Distributions #include "test_utils.h"
69*4f1223e8SApple OSS Distributions 
70*4f1223e8SApple OSS Distributions T_GLOBAL_META(
71*4f1223e8SApple OSS Distributions 	T_META_NAMESPACE("xnu.arm"),
72*4f1223e8SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
73*4f1223e8SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("arm"),
74*4f1223e8SApple OSS Distributions 	T_META_OWNER("ghackmann"),
75*4f1223e8SApple OSS Distributions 	T_META_RUN_CONCURRENTLY(true)
76*4f1223e8SApple OSS Distributions 	);
77*4f1223e8SApple OSS Distributions 
78*4f1223e8SApple OSS Distributions #ifdef __arm64__
79*4f1223e8SApple OSS Distributions 
80*4f1223e8SApple OSS Distributions #ifndef EXC_ARM_SME_DISALLOWED
81*4f1223e8SApple OSS Distributions #define EXC_ARM_SME_DISALLOWED 2
82*4f1223e8SApple OSS Distributions #endif
83*4f1223e8SApple OSS Distributions 
84*4f1223e8SApple OSS Distributions /* Whether we caught the EXC_BAD_INSTRUCTION mach exception or not. */
85*4f1223e8SApple OSS Distributions static volatile bool mach_exc_caught = false;
86*4f1223e8SApple OSS Distributions 
87*4f1223e8SApple OSS Distributions static size_t
bad_instruction_exception_handler(__unused mach_port_t task,__unused mach_port_t thread,exception_type_t type,mach_exception_data_t codes)88*4f1223e8SApple OSS Distributions bad_instruction_exception_handler(
89*4f1223e8SApple OSS Distributions 	__unused mach_port_t task,
90*4f1223e8SApple OSS Distributions 	__unused mach_port_t thread,
91*4f1223e8SApple OSS Distributions 	exception_type_t type,
92*4f1223e8SApple OSS Distributions 	mach_exception_data_t codes)
93*4f1223e8SApple OSS Distributions {
94*4f1223e8SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(type, EXC_BAD_INSTRUCTION, "Caught an EXC_BAD_INSTRUCTION exception");
95*4f1223e8SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(codes[0], (uint64_t)EXC_ARM_UNDEFINED, "The subcode is EXC_ARM_UNDEFINED");
96*4f1223e8SApple OSS Distributions 
97*4f1223e8SApple OSS Distributions 	mach_exc_caught = true;
98*4f1223e8SApple OSS Distributions 	return 4;
99*4f1223e8SApple OSS Distributions }
100*4f1223e8SApple OSS Distributions #endif
101*4f1223e8SApple OSS Distributions 
102*4f1223e8SApple OSS Distributions 
103*4f1223e8SApple OSS Distributions #ifdef __arm64__
104*4f1223e8SApple OSS Distributions static void
test_matrix_not_started(const struct arm_matrix_operations * ops)105*4f1223e8SApple OSS Distributions test_matrix_not_started(const struct arm_matrix_operations *ops)
106*4f1223e8SApple OSS Distributions {
107*4f1223e8SApple OSS Distributions 	if (!ops->is_available()) {
108*4f1223e8SApple OSS Distributions 		T_SKIP("Running on non-%s target, skipping...", ops->name);
109*4f1223e8SApple OSS Distributions 	}
110*4f1223e8SApple OSS Distributions 
111*4f1223e8SApple OSS Distributions 	mach_port_t exc_port = create_exception_port(EXC_MASK_BAD_INSTRUCTION);
112*4f1223e8SApple OSS Distributions 
113*4f1223e8SApple OSS Distributions 	size_t size = ops->data_size();
114*4f1223e8SApple OSS Distributions 	uint8_t *d = ops->alloc_data();
115*4f1223e8SApple OSS Distributions 	bzero(d, size);
116*4f1223e8SApple OSS Distributions 
117*4f1223e8SApple OSS Distributions 	ops->start();
118*4f1223e8SApple OSS Distributions 	ops->load_one_vector(d);
119*4f1223e8SApple OSS Distributions 	ops->stop();
120*4f1223e8SApple OSS Distributions 	T_PASS("%s instruction after start instruction should not cause an exception", ops->name);
121*4f1223e8SApple OSS Distributions 
122*4f1223e8SApple OSS Distributions 	mach_exc_caught = false;
123*4f1223e8SApple OSS Distributions 	run_exception_handler(exc_port, bad_instruction_exception_handler);
124*4f1223e8SApple OSS Distributions 	ops->load_one_vector(d);
125*4f1223e8SApple OSS Distributions 	T_EXPECT_TRUE(mach_exc_caught, "%s instruction before start instruction should cause an exception", ops->name);
126*4f1223e8SApple OSS Distributions 
127*4f1223e8SApple OSS Distributions 	free(d);
128*4f1223e8SApple OSS Distributions }
129*4f1223e8SApple OSS Distributions #endif
130*4f1223e8SApple OSS Distributions 
131*4f1223e8SApple OSS Distributions 
132*4f1223e8SApple OSS Distributions T_DECL(sme_not_started,
133*4f1223e8SApple OSS Distributions     "Test that SME instructions before smstart generate mach exceptions.", T_META_TAG_VM_NOT_ELIGIBLE)
134*4f1223e8SApple OSS Distributions {
135*4f1223e8SApple OSS Distributions #ifndef __arm64__
136*4f1223e8SApple OSS Distributions 	T_SKIP("Running on non-arm64 target, skipping...");
137*4f1223e8SApple OSS Distributions #else
138*4f1223e8SApple OSS Distributions 	test_matrix_not_started(&sme_operations);
139*4f1223e8SApple OSS Distributions #endif
140*4f1223e8SApple OSS Distributions }
141*4f1223e8SApple OSS Distributions 
142*4f1223e8SApple OSS Distributions #ifdef __arm64__
143*4f1223e8SApple OSS Distributions typedef bool (*thread_fn_t)(const struct arm_matrix_operations *, uint32_t);
144*4f1223e8SApple OSS Distributions 
145*4f1223e8SApple OSS Distributions struct test_thread {
146*4f1223e8SApple OSS Distributions 	pthread_t thread;
147*4f1223e8SApple OSS Distributions 	thread_fn_t thread_fn;
148*4f1223e8SApple OSS Distributions 	uint32_t cpuid;
149*4f1223e8SApple OSS Distributions 	uint32_t thread_id;
150*4f1223e8SApple OSS Distributions 	const struct arm_matrix_operations *ops;
151*4f1223e8SApple OSS Distributions };
152*4f1223e8SApple OSS Distributions 
153*4f1223e8SApple OSS Distributions static uint32_t barrier;
154*4f1223e8SApple OSS Distributions static pthread_cond_t barrier_cond = PTHREAD_COND_INITIALIZER;
155*4f1223e8SApple OSS Distributions static pthread_mutex_t barrier_lock = PTHREAD_MUTEX_INITIALIZER;
156*4f1223e8SApple OSS Distributions 
157*4f1223e8SApple OSS Distributions static void
test_thread_barrier(void)158*4f1223e8SApple OSS Distributions test_thread_barrier(void)
159*4f1223e8SApple OSS Distributions {
160*4f1223e8SApple OSS Distributions 	/* Wait for all threads to reach this barrier */
161*4f1223e8SApple OSS Distributions 	pthread_mutex_lock(&barrier_lock);
162*4f1223e8SApple OSS Distributions 	barrier--;
163*4f1223e8SApple OSS Distributions 	if (barrier) {
164*4f1223e8SApple OSS Distributions 		while (barrier) {
165*4f1223e8SApple OSS Distributions 			pthread_cond_wait(&barrier_cond, &barrier_lock);
166*4f1223e8SApple OSS Distributions 		}
167*4f1223e8SApple OSS Distributions 	} else {
168*4f1223e8SApple OSS Distributions 		pthread_cond_broadcast(&barrier_cond);
169*4f1223e8SApple OSS Distributions 	}
170*4f1223e8SApple OSS Distributions 	pthread_mutex_unlock(&barrier_lock);
171*4f1223e8SApple OSS Distributions }
172*4f1223e8SApple OSS Distributions 
173*4f1223e8SApple OSS Distributions static uint32_t
ncpus(void)174*4f1223e8SApple OSS Distributions ncpus(void)
175*4f1223e8SApple OSS Distributions {
176*4f1223e8SApple OSS Distributions 	uint32_t ncpu;
177*4f1223e8SApple OSS Distributions 	size_t ncpu_size = sizeof(ncpu);
178*4f1223e8SApple OSS Distributions 	int err = sysctlbyname("hw.ncpu", &ncpu, &ncpu_size, NULL, 0);
179*4f1223e8SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_ZERO(err, "Retrieved CPU count");
180*4f1223e8SApple OSS Distributions 
181*4f1223e8SApple OSS Distributions 	return ncpu;
182*4f1223e8SApple OSS Distributions }
183*4f1223e8SApple OSS Distributions 
184*4f1223e8SApple OSS Distributions static int
thread_bind_cpu_unchecked(uint32_t cpuid)185*4f1223e8SApple OSS Distributions thread_bind_cpu_unchecked(uint32_t cpuid)
186*4f1223e8SApple OSS Distributions {
187*4f1223e8SApple OSS Distributions 	/*
188*4f1223e8SApple OSS Distributions 	 * libc's sysctl() implementation calls strlen(name), which is
189*4f1223e8SApple OSS Distributions 	 * SIMD-accelerated.  Avoid this by directly invoking the libsyscall
190*4f1223e8SApple OSS Distributions 	 * wrapper with namelen computed at compile time.
191*4f1223e8SApple OSS Distributions 	 */
192*4f1223e8SApple OSS Distributions #define THREAD_BIND_CPU "kern.sched_thread_bind_cpu"
193*4f1223e8SApple OSS Distributions 	extern int __sysctlbyname(const char *name, size_t namelen, void *oldp, size_t *oldlenp, void *newp, size_t newlen);
194*4f1223e8SApple OSS Distributions 	const char *name = THREAD_BIND_CPU;
195*4f1223e8SApple OSS Distributions 	size_t namelen = sizeof(THREAD_BIND_CPU) - 1;
196*4f1223e8SApple OSS Distributions 	return __sysctlbyname(name, namelen, NULL, 0, &cpuid, sizeof(cpuid));
197*4f1223e8SApple OSS Distributions }
198*4f1223e8SApple OSS Distributions 
199*4f1223e8SApple OSS Distributions static void
thread_bind_cpu(uint32_t cpuid)200*4f1223e8SApple OSS Distributions thread_bind_cpu(uint32_t cpuid)
201*4f1223e8SApple OSS Distributions {
202*4f1223e8SApple OSS Distributions 	int err = thread_bind_cpu_unchecked(cpuid);
203*4f1223e8SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_ZERO(err, "Bound thread to CPU %u", cpuid);
204*4f1223e8SApple OSS Distributions }
205*4f1223e8SApple OSS Distributions 
206*4f1223e8SApple OSS Distributions static void *
test_thread_shim(void * arg)207*4f1223e8SApple OSS Distributions test_thread_shim(void *arg)
208*4f1223e8SApple OSS Distributions {
209*4f1223e8SApple OSS Distributions 	struct test_thread *thread = arg;
210*4f1223e8SApple OSS Distributions 
211*4f1223e8SApple OSS Distributions 	thread_bind_cpu(thread->cpuid);
212*4f1223e8SApple OSS Distributions 	bool ret = thread->thread_fn(thread->ops, thread->thread_id);
213*4f1223e8SApple OSS Distributions 	return (void *)(uintptr_t)ret;
214*4f1223e8SApple OSS Distributions }
215*4f1223e8SApple OSS Distributions 
216*4f1223e8SApple OSS Distributions static void
test_on_each_cpu(thread_fn_t thread_fn,const struct arm_matrix_operations * ops,const char * desc)217*4f1223e8SApple OSS Distributions test_on_each_cpu(thread_fn_t thread_fn, const struct arm_matrix_operations *ops, const char *desc)
218*4f1223e8SApple OSS Distributions {
219*4f1223e8SApple OSS Distributions 	uint32_t ncpu = ncpus();
220*4f1223e8SApple OSS Distributions 	uint32_t nthreads = ncpu * 2;
221*4f1223e8SApple OSS Distributions 	barrier = nthreads;
222*4f1223e8SApple OSS Distributions 	struct test_thread *threads = calloc(nthreads, sizeof(threads[0]));
223*4f1223e8SApple OSS Distributions 	for (uint32_t i = 0; i < nthreads; i++) {
224*4f1223e8SApple OSS Distributions 		threads[i].thread_fn = thread_fn;
225*4f1223e8SApple OSS Distributions 		threads[i].cpuid = i % ncpu;
226*4f1223e8SApple OSS Distributions 		threads[i].thread_id = i;
227*4f1223e8SApple OSS Distributions 		threads[i].ops = ops;
228*4f1223e8SApple OSS Distributions 
229*4f1223e8SApple OSS Distributions 		int err = pthread_create(&threads[i].thread, NULL, test_thread_shim, &threads[i]);
230*4f1223e8SApple OSS Distributions 		T_QUIET; T_ASSERT_EQ(err, 0, "%s: created thread #%u", desc, i);
231*4f1223e8SApple OSS Distributions 	}
232*4f1223e8SApple OSS Distributions 
233*4f1223e8SApple OSS Distributions 	for (uint32_t i = 0; i < nthreads; i++) {
234*4f1223e8SApple OSS Distributions 		void *thread_ret_ptr;
235*4f1223e8SApple OSS Distributions 		int err = pthread_join(threads[i].thread, &thread_ret_ptr);
236*4f1223e8SApple OSS Distributions 		T_QUIET; T_ASSERT_EQ(err, 0, "%s: joined thread #%u", desc, i);
237*4f1223e8SApple OSS Distributions 
238*4f1223e8SApple OSS Distributions 		bool thread_ret = (uintptr_t)thread_ret_ptr;
239*4f1223e8SApple OSS Distributions 		if (thread_ret) {
240*4f1223e8SApple OSS Distributions 			T_PASS("%s: thread #%u passed", desc, i);
241*4f1223e8SApple OSS Distributions 		} else {
242*4f1223e8SApple OSS Distributions 			T_FAIL("%s: thread #%u failed", desc, i);
243*4f1223e8SApple OSS Distributions 		}
244*4f1223e8SApple OSS Distributions 	}
245*4f1223e8SApple OSS Distributions 
246*4f1223e8SApple OSS Distributions 	free(threads);
247*4f1223e8SApple OSS Distributions }
248*4f1223e8SApple OSS Distributions 
249*4f1223e8SApple OSS Distributions static bool
active_context_switch_thread(const struct arm_matrix_operations * ops,uint32_t thread_id)250*4f1223e8SApple OSS Distributions active_context_switch_thread(const struct arm_matrix_operations *ops, uint32_t thread_id)
251*4f1223e8SApple OSS Distributions {
252*4f1223e8SApple OSS Distributions 	size_t size = ops->data_size();
253*4f1223e8SApple OSS Distributions 	uint8_t *d1 = ops->alloc_data();
254*4f1223e8SApple OSS Distributions 	memset(d1, (char)thread_id, size);
255*4f1223e8SApple OSS Distributions 
256*4f1223e8SApple OSS Distributions 	uint8_t *d2 = ops->alloc_data();
257*4f1223e8SApple OSS Distributions 
258*4f1223e8SApple OSS Distributions 	test_thread_barrier();
259*4f1223e8SApple OSS Distributions 
260*4f1223e8SApple OSS Distributions 	bool ok = true;
261*4f1223e8SApple OSS Distributions 	for (unsigned int i = 0; i < 100000 && ok; i++) {
262*4f1223e8SApple OSS Distributions 		ops->start();
263*4f1223e8SApple OSS Distributions 		ops->load_data(d1);
264*4f1223e8SApple OSS Distributions 
265*4f1223e8SApple OSS Distributions 		/*
266*4f1223e8SApple OSS Distributions 		 * Rescheduling with the matrix registers active must preserve
267*4f1223e8SApple OSS Distributions 		 * state, even after a context switch.
268*4f1223e8SApple OSS Distributions 		 */
269*4f1223e8SApple OSS Distributions 		sched_yield();
270*4f1223e8SApple OSS Distributions 
271*4f1223e8SApple OSS Distributions 		ops->store_data(d2);
272*4f1223e8SApple OSS Distributions 		ops->stop();
273*4f1223e8SApple OSS Distributions 
274*4f1223e8SApple OSS Distributions 		if (memcmp(d1, d2, size)) {
275*4f1223e8SApple OSS Distributions 			ok = false;
276*4f1223e8SApple OSS Distributions 		}
277*4f1223e8SApple OSS Distributions 	}
278*4f1223e8SApple OSS Distributions 
279*4f1223e8SApple OSS Distributions 	free(d2);
280*4f1223e8SApple OSS Distributions 	free(d1);
281*4f1223e8SApple OSS Distributions 	return ok;
282*4f1223e8SApple OSS Distributions }
283*4f1223e8SApple OSS Distributions 
284*4f1223e8SApple OSS Distributions static bool
inactive_context_switch_thread(const struct arm_matrix_operations * ops,uint32_t thread_id)285*4f1223e8SApple OSS Distributions inactive_context_switch_thread(const struct arm_matrix_operations *ops, uint32_t thread_id)
286*4f1223e8SApple OSS Distributions {
287*4f1223e8SApple OSS Distributions 	size_t size = ops->data_size();
288*4f1223e8SApple OSS Distributions 	uint8_t *d1 = ops->alloc_data();
289*4f1223e8SApple OSS Distributions 	memset(d1, (char)thread_id, size);
290*4f1223e8SApple OSS Distributions 
291*4f1223e8SApple OSS Distributions 	uint8_t *d2 = ops->alloc_data();
292*4f1223e8SApple OSS Distributions 
293*4f1223e8SApple OSS Distributions 	test_thread_barrier();
294*4f1223e8SApple OSS Distributions 
295*4f1223e8SApple OSS Distributions 	bool ok = true;
296*4f1223e8SApple OSS Distributions 	for (unsigned int i = 0; i < 100000 && ok; i++) {
297*4f1223e8SApple OSS Distributions 		ops->start();
298*4f1223e8SApple OSS Distributions 		ops->load_data(d1);
299*4f1223e8SApple OSS Distributions 		ops->stop();
300*4f1223e8SApple OSS Distributions 
301*4f1223e8SApple OSS Distributions 		/*
302*4f1223e8SApple OSS Distributions 		 * Rescheduling with the matrix registers inactive may preserve
303*4f1223e8SApple OSS Distributions 		 * state or may zero it out.
304*4f1223e8SApple OSS Distributions 		 */
305*4f1223e8SApple OSS Distributions 		sched_yield();
306*4f1223e8SApple OSS Distributions 
307*4f1223e8SApple OSS Distributions 		ops->start();
308*4f1223e8SApple OSS Distributions 		ops->store_data(d2);
309*4f1223e8SApple OSS Distributions 		ops->stop();
310*4f1223e8SApple OSS Distributions 
311*4f1223e8SApple OSS Distributions 		for (size_t j = 0; j < size; j++) {
312*4f1223e8SApple OSS Distributions 			if (d1[j] != d2[j] && d2[j] != 0) {
313*4f1223e8SApple OSS Distributions 				ok = false;
314*4f1223e8SApple OSS Distributions 			}
315*4f1223e8SApple OSS Distributions 		}
316*4f1223e8SApple OSS Distributions 	}
317*4f1223e8SApple OSS Distributions 
318*4f1223e8SApple OSS Distributions 	free(d2);
319*4f1223e8SApple OSS Distributions 	free(d1);
320*4f1223e8SApple OSS Distributions 	return ok;
321*4f1223e8SApple OSS Distributions }
322*4f1223e8SApple OSS Distributions 
323*4f1223e8SApple OSS Distributions static void
test_thread_migration(const struct arm_matrix_operations * ops)324*4f1223e8SApple OSS Distributions test_thread_migration(const struct arm_matrix_operations *ops)
325*4f1223e8SApple OSS Distributions {
326*4f1223e8SApple OSS Distributions 	size_t size = ops->data_size();
327*4f1223e8SApple OSS Distributions 	uint8_t *d = ops->alloc_data();
328*4f1223e8SApple OSS Distributions 	arc4random_buf(d, size);
329*4f1223e8SApple OSS Distributions 
330*4f1223e8SApple OSS Distributions 	uint32_t ncpu = ncpus();
331*4f1223e8SApple OSS Distributions 	uint8_t *cpu_d[ncpu];
332*4f1223e8SApple OSS Distributions 	for (uint32_t cpuid = 0; cpuid < ncpu; cpuid++) {
333*4f1223e8SApple OSS Distributions 		cpu_d[cpuid] = ops->alloc_data();
334*4f1223e8SApple OSS Distributions 		memset(cpu_d[cpuid], 0, size);
335*4f1223e8SApple OSS Distributions 	}
336*4f1223e8SApple OSS Distributions 
337*4f1223e8SApple OSS Distributions 	ops->start();
338*4f1223e8SApple OSS Distributions 	ops->load_data(d);
339*4f1223e8SApple OSS Distributions 	for (uint32_t cpuid = 0; cpuid < ncpu; cpuid++) {
340*4f1223e8SApple OSS Distributions 		int err = thread_bind_cpu_unchecked(cpuid);
341*4f1223e8SApple OSS Distributions 		if (err) {
342*4f1223e8SApple OSS Distributions 			ops->stop();
343*4f1223e8SApple OSS Distributions 			T_ASSERT_POSIX_ZERO(err, "Bound thread to CPU %u", cpuid);
344*4f1223e8SApple OSS Distributions 		}
345*4f1223e8SApple OSS Distributions 		ops->store_data(cpu_d[cpuid]);
346*4f1223e8SApple OSS Distributions 	}
347*4f1223e8SApple OSS Distributions 	ops->stop();
348*4f1223e8SApple OSS Distributions 
349*4f1223e8SApple OSS Distributions 	for (uint32_t cpuid = 0; cpuid < ncpu; cpuid++) {
350*4f1223e8SApple OSS Distributions 		int cmp = memcmp(d, cpu_d[cpuid], size);
351*4f1223e8SApple OSS Distributions 		T_EXPECT_EQ(cmp, 0, "Matrix state migrated to CPU %u", cpuid);
352*4f1223e8SApple OSS Distributions 		free(cpu_d[cpuid]);
353*4f1223e8SApple OSS Distributions 	}
354*4f1223e8SApple OSS Distributions 	free(d);
355*4f1223e8SApple OSS Distributions }
356*4f1223e8SApple OSS Distributions #endif
357*4f1223e8SApple OSS Distributions 
358*4f1223e8SApple OSS Distributions 
359*4f1223e8SApple OSS Distributions T_DECL(sme_context_switch,
360*4f1223e8SApple OSS Distributions     "Test that SME contexts are migrated during context switch and do not leak between process contexts.",
361*4f1223e8SApple OSS Distributions     T_META_BOOTARGS_SET("enable_skstb=1"),
362*4f1223e8SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_SME2", 1),
363*4f1223e8SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC, T_META_TAG_VM_NOT_ELIGIBLE)
364*4f1223e8SApple OSS Distributions {
365*4f1223e8SApple OSS Distributions #ifndef __arm64__
366*4f1223e8SApple OSS Distributions 	T_SKIP("Running on non-arm64 target, skipping...");
367*4f1223e8SApple OSS Distributions #else
368*4f1223e8SApple OSS Distributions 	if (!sme_operations.is_available()) {
369*4f1223e8SApple OSS Distributions 		T_SKIP("Running on non-SME target, skipping...");
370*4f1223e8SApple OSS Distributions 	}
371*4f1223e8SApple OSS Distributions 
372*4f1223e8SApple OSS Distributions 	test_thread_migration(&sme_operations);
373*4f1223e8SApple OSS Distributions 	test_on_each_cpu(active_context_switch_thread, &sme_operations, "SME context migrates when active");
374*4f1223e8SApple OSS Distributions 	test_on_each_cpu(inactive_context_switch_thread, &sme_operations, "SME context does not leak across processes");
375*4f1223e8SApple OSS Distributions #endif
376*4f1223e8SApple OSS Distributions }
377*4f1223e8SApple OSS Distributions 
378