xref: /xnu-11417.140.69/tests/arm_matrix.c (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
1*43a90889SApple OSS Distributions /*
2*43a90889SApple OSS Distributions  * Copyright (c) 2019 Apple Computer, Inc. All rights reserved.
3*43a90889SApple OSS Distributions  *
4*43a90889SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*43a90889SApple OSS Distributions  *
6*43a90889SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*43a90889SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*43a90889SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*43a90889SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*43a90889SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*43a90889SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*43a90889SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*43a90889SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*43a90889SApple OSS Distributions  *
15*43a90889SApple OSS Distributions  * Please obtain a copy of the License at
16*43a90889SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*43a90889SApple OSS Distributions  *
18*43a90889SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*43a90889SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*43a90889SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*43a90889SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*43a90889SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*43a90889SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*43a90889SApple OSS Distributions  * limitations under the License.
25*43a90889SApple OSS Distributions  *
26*43a90889SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*43a90889SApple OSS Distributions  */
28*43a90889SApple OSS Distributions /**
29*43a90889SApple OSS Distributions  * On devices that support it, this test ensures that a mach exception is
30*43a90889SApple OSS Distributions  * generated when a matrix-math exception is triggered, and that the
31*43a90889SApple OSS Distributions  * matrix register file is correctly preserved or zeroed on context switch.
32*43a90889SApple OSS Distributions  */
33*43a90889SApple OSS Distributions 
34*43a90889SApple OSS Distributions /*
35*43a90889SApple OSS Distributions  * IMPLEMENTATION NOTE:
36*43a90889SApple OSS Distributions  *
37*43a90889SApple OSS Distributions  * This test code goes to some unusual lengths to avoid calling out to libc or
38*43a90889SApple OSS Distributions  * libdarwintest while the CPU is in streaming SVE mode (i.e., between
39*43a90889SApple OSS Distributions  * ops->start() and ops->stop()).  Both of these libraries are built with SIMD
40*43a90889SApple OSS Distributions  * instructions that will cause the test executable to crash while in streaming
41*43a90889SApple OSS Distributions  * SVE mode.
42*43a90889SApple OSS Distributions  *
43*43a90889SApple OSS Distributions  * Ordinarily this is the wrong way to solve this problem.  Functions that use
44*43a90889SApple OSS Distributions  * streaming SVE mode should have annotations telling the compiler so, and the
45*43a90889SApple OSS Distributions  * compiler will automatically generate appropriate interworking code.  However
46*43a90889SApple OSS Distributions  * this interworking code will stash SME state to memory and temporarily exit
47*43a90889SApple OSS Distributions  * streaming SVE mode.  We're specifically testing how xnu manages live SME
48*43a90889SApple OSS Distributions  * register state, so we can't let the compiler stash and disable this state
49*43a90889SApple OSS Distributions  * behind our backs.
50*43a90889SApple OSS Distributions  */
51*43a90889SApple OSS Distributions 
52*43a90889SApple OSS Distributions #ifdef __arm64__
53*43a90889SApple OSS Distributions #include <mach/error.h>
54*43a90889SApple OSS Distributions #endif /* __arm64__ */
55*43a90889SApple OSS Distributions 
56*43a90889SApple OSS Distributions #include <darwintest.h>
57*43a90889SApple OSS Distributions #include <pthread.h>
58*43a90889SApple OSS Distributions #include <stdlib.h>
59*43a90889SApple OSS Distributions #include <mach/mach.h>
60*43a90889SApple OSS Distributions #include <mach/thread_act.h>
61*43a90889SApple OSS Distributions #include <mach/thread_status.h>
62*43a90889SApple OSS Distributions #include <mach/exception.h>
63*43a90889SApple OSS Distributions #include <machine/cpu_capabilities.h>
64*43a90889SApple OSS Distributions #include <sys/types.h>
65*43a90889SApple OSS Distributions #include <sys/sysctl.h>
66*43a90889SApple OSS Distributions 
67*43a90889SApple OSS Distributions #include "arm_matrix.h"
68*43a90889SApple OSS Distributions #include "exc_helpers.h"
69*43a90889SApple OSS Distributions #include "test_utils.h"
70*43a90889SApple OSS Distributions 
71*43a90889SApple OSS Distributions T_GLOBAL_META(
72*43a90889SApple OSS Distributions 	T_META_NAMESPACE("xnu.arm"),
73*43a90889SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
74*43a90889SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("arm"),
75*43a90889SApple OSS Distributions 	T_META_OWNER("ghackmann"),
76*43a90889SApple OSS Distributions 	T_META_RUN_CONCURRENTLY(true)
77*43a90889SApple OSS Distributions 	);
78*43a90889SApple OSS Distributions 
79*43a90889SApple OSS Distributions #ifdef __arm64__
80*43a90889SApple OSS Distributions 
81*43a90889SApple OSS Distributions #ifndef EXC_ARM_SME_DISALLOWED
82*43a90889SApple OSS Distributions #define EXC_ARM_SME_DISALLOWED 2
83*43a90889SApple OSS Distributions #endif
84*43a90889SApple OSS Distributions 
85*43a90889SApple OSS Distributions /* Whether we caught the EXC_BAD_INSTRUCTION mach exception or not. */
86*43a90889SApple OSS Distributions static volatile bool mach_exc_caught = false;
87*43a90889SApple OSS Distributions 
88*43a90889SApple 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)89*43a90889SApple OSS Distributions bad_instruction_exception_handler(
90*43a90889SApple OSS Distributions 	__unused mach_port_t task,
91*43a90889SApple OSS Distributions 	__unused mach_port_t thread,
92*43a90889SApple OSS Distributions 	exception_type_t type,
93*43a90889SApple OSS Distributions 	mach_exception_data_t codes)
94*43a90889SApple OSS Distributions {
95*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(type, EXC_BAD_INSTRUCTION, "Caught an EXC_BAD_INSTRUCTION exception");
96*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(codes[0], (uint64_t)EXC_ARM_UNDEFINED, "The subcode is EXC_ARM_UNDEFINED");
97*43a90889SApple OSS Distributions 
98*43a90889SApple OSS Distributions 	mach_exc_caught = true;
99*43a90889SApple OSS Distributions 	return 4;
100*43a90889SApple OSS Distributions }
101*43a90889SApple OSS Distributions #endif
102*43a90889SApple OSS Distributions 
103*43a90889SApple OSS Distributions 
104*43a90889SApple OSS Distributions #ifdef __arm64__
105*43a90889SApple OSS Distributions static void
test_matrix_not_started(const struct arm_matrix_operations * ops)106*43a90889SApple OSS Distributions test_matrix_not_started(const struct arm_matrix_operations *ops)
107*43a90889SApple OSS Distributions {
108*43a90889SApple OSS Distributions 	if (!ops->is_available()) {
109*43a90889SApple OSS Distributions 		T_SKIP("Running on non-%s target, skipping...", ops->name);
110*43a90889SApple OSS Distributions 	}
111*43a90889SApple OSS Distributions 
112*43a90889SApple OSS Distributions 	mach_port_t exc_port = create_exception_port(EXC_MASK_BAD_INSTRUCTION);
113*43a90889SApple OSS Distributions 
114*43a90889SApple OSS Distributions 	size_t size = ops->data_size();
115*43a90889SApple OSS Distributions 	uint8_t *d = ops->alloc_data();
116*43a90889SApple OSS Distributions 	bzero(d, size);
117*43a90889SApple OSS Distributions 
118*43a90889SApple OSS Distributions 	ops->start();
119*43a90889SApple OSS Distributions 	ops->load_one_vector(d);
120*43a90889SApple OSS Distributions 	ops->stop();
121*43a90889SApple OSS Distributions 	T_PASS("%s instruction after start instruction should not cause an exception", ops->name);
122*43a90889SApple OSS Distributions 
123*43a90889SApple OSS Distributions 	mach_exc_caught = false;
124*43a90889SApple OSS Distributions 	run_exception_handler(exc_port, bad_instruction_exception_handler);
125*43a90889SApple OSS Distributions 	ops->load_one_vector(d);
126*43a90889SApple OSS Distributions 	T_EXPECT_TRUE(mach_exc_caught, "%s instruction before start instruction should cause an exception", ops->name);
127*43a90889SApple OSS Distributions 
128*43a90889SApple OSS Distributions 	free(d);
129*43a90889SApple OSS Distributions }
130*43a90889SApple OSS Distributions #endif
131*43a90889SApple OSS Distributions 
132*43a90889SApple OSS Distributions 
133*43a90889SApple OSS Distributions T_DECL(sme_not_started,
134*43a90889SApple OSS Distributions     "Test that SME instructions before smstart generate mach exceptions.", T_META_TAG_VM_NOT_ELIGIBLE)
135*43a90889SApple OSS Distributions {
136*43a90889SApple OSS Distributions #ifndef __arm64__
137*43a90889SApple OSS Distributions 	T_SKIP("Running on non-arm64 target, skipping...");
138*43a90889SApple OSS Distributions #else
139*43a90889SApple OSS Distributions 	test_matrix_not_started(&sme_operations);
140*43a90889SApple OSS Distributions #endif
141*43a90889SApple OSS Distributions }
142*43a90889SApple OSS Distributions 
143*43a90889SApple OSS Distributions #ifdef __arm64__
144*43a90889SApple OSS Distributions struct test_thread;
145*43a90889SApple OSS Distributions typedef bool (*thread_fn_t)(struct test_thread const* thread);
146*43a90889SApple OSS Distributions 
147*43a90889SApple OSS Distributions struct test_thread {
148*43a90889SApple OSS Distributions 	pthread_t thread;
149*43a90889SApple OSS Distributions 	pthread_t companion_thread;
150*43a90889SApple OSS Distributions 	thread_fn_t thread_fn;
151*43a90889SApple OSS Distributions 	uint32_t cpuid;
152*43a90889SApple OSS Distributions 	uint32_t thread_id;
153*43a90889SApple OSS Distributions 	const struct arm_matrix_operations *ops;
154*43a90889SApple OSS Distributions };
155*43a90889SApple OSS Distributions 
156*43a90889SApple OSS Distributions static uint32_t barrier;
157*43a90889SApple OSS Distributions static pthread_cond_t barrier_cond = PTHREAD_COND_INITIALIZER;
158*43a90889SApple OSS Distributions static pthread_mutex_t barrier_lock = PTHREAD_MUTEX_INITIALIZER;
159*43a90889SApple OSS Distributions 
160*43a90889SApple OSS Distributions static uint32_t end_barrier;
161*43a90889SApple OSS Distributions static pthread_cond_t end_barrier_cond = PTHREAD_COND_INITIALIZER;
162*43a90889SApple OSS Distributions static pthread_mutex_t end_barrier_lock = PTHREAD_MUTEX_INITIALIZER;
163*43a90889SApple OSS Distributions 
164*43a90889SApple OSS Distributions static void
test_thread_barrier(void)165*43a90889SApple OSS Distributions test_thread_barrier(void)
166*43a90889SApple OSS Distributions {
167*43a90889SApple OSS Distributions 	/* Wait for all threads to reach this barrier */
168*43a90889SApple OSS Distributions 	pthread_mutex_lock(&barrier_lock);
169*43a90889SApple OSS Distributions 	barrier--;
170*43a90889SApple OSS Distributions 	if (barrier) {
171*43a90889SApple OSS Distributions 		while (barrier) {
172*43a90889SApple OSS Distributions 			pthread_cond_wait(&barrier_cond, &barrier_lock);
173*43a90889SApple OSS Distributions 		}
174*43a90889SApple OSS Distributions 	} else {
175*43a90889SApple OSS Distributions 		pthread_cond_broadcast(&barrier_cond);
176*43a90889SApple OSS Distributions 	}
177*43a90889SApple OSS Distributions 	pthread_mutex_unlock(&barrier_lock);
178*43a90889SApple OSS Distributions }
179*43a90889SApple OSS Distributions 
180*43a90889SApple OSS Distributions static void
test_thread_notify_exited(void)181*43a90889SApple OSS Distributions test_thread_notify_exited(void)
182*43a90889SApple OSS Distributions {
183*43a90889SApple OSS Distributions 	pthread_mutex_lock(&end_barrier_lock);
184*43a90889SApple OSS Distributions 	if (0 == --end_barrier) {
185*43a90889SApple OSS Distributions 		pthread_cond_signal(&end_barrier_cond);
186*43a90889SApple OSS Distributions 	}
187*43a90889SApple OSS Distributions 	pthread_mutex_unlock(&end_barrier_lock);
188*43a90889SApple OSS Distributions }
189*43a90889SApple OSS Distributions 
190*43a90889SApple OSS Distributions static void
wait_for_test_threads(void)191*43a90889SApple OSS Distributions wait_for_test_threads(void)
192*43a90889SApple OSS Distributions {
193*43a90889SApple OSS Distributions 	pthread_mutex_lock(&end_barrier_lock);
194*43a90889SApple OSS Distributions 	while (end_barrier) {
195*43a90889SApple OSS Distributions 		pthread_cond_wait(&end_barrier_cond, &end_barrier_lock);
196*43a90889SApple OSS Distributions 	}
197*43a90889SApple OSS Distributions 	pthread_mutex_unlock(&end_barrier_lock);
198*43a90889SApple OSS Distributions }
199*43a90889SApple OSS Distributions 
200*43a90889SApple OSS Distributions static uint32_t
ncpus(void)201*43a90889SApple OSS Distributions ncpus(void)
202*43a90889SApple OSS Distributions {
203*43a90889SApple OSS Distributions 	uint32_t ncpu;
204*43a90889SApple OSS Distributions 	size_t ncpu_size = sizeof(ncpu);
205*43a90889SApple OSS Distributions 	int err = sysctlbyname("hw.ncpu", &ncpu, &ncpu_size, NULL, 0);
206*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_ZERO(err, "Retrieved CPU count");
207*43a90889SApple OSS Distributions 
208*43a90889SApple OSS Distributions 	return ncpu;
209*43a90889SApple OSS Distributions }
210*43a90889SApple OSS Distributions 
211*43a90889SApple OSS Distributions static int
thread_bind_cpu_unchecked(uint32_t cpuid)212*43a90889SApple OSS Distributions thread_bind_cpu_unchecked(uint32_t cpuid)
213*43a90889SApple OSS Distributions {
214*43a90889SApple OSS Distributions 	/*
215*43a90889SApple OSS Distributions 	 * libc's sysctl() implementation calls strlen(name), which is
216*43a90889SApple OSS Distributions 	 * SIMD-accelerated.  Avoid this by directly invoking the libsyscall
217*43a90889SApple OSS Distributions 	 * wrapper with namelen computed at compile time.
218*43a90889SApple OSS Distributions 	 */
219*43a90889SApple OSS Distributions #define THREAD_BIND_CPU "kern.sched_thread_bind_cpu"
220*43a90889SApple OSS Distributions 	extern int __sysctlbyname(const char *name, size_t namelen, void *oldp, size_t *oldlenp, void *newp, size_t newlen);
221*43a90889SApple OSS Distributions 	const char *name = THREAD_BIND_CPU;
222*43a90889SApple OSS Distributions 	size_t namelen = sizeof(THREAD_BIND_CPU) - 1;
223*43a90889SApple OSS Distributions 	return __sysctlbyname(name, namelen, NULL, 0, &cpuid, sizeof(cpuid));
224*43a90889SApple OSS Distributions }
225*43a90889SApple OSS Distributions 
226*43a90889SApple OSS Distributions static void
thread_bind_cpu(uint32_t cpuid)227*43a90889SApple OSS Distributions thread_bind_cpu(uint32_t cpuid)
228*43a90889SApple OSS Distributions {
229*43a90889SApple OSS Distributions 	int err = thread_bind_cpu_unchecked(cpuid);
230*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_ZERO(err, "Bound thread to CPU %u", cpuid);
231*43a90889SApple OSS Distributions }
232*43a90889SApple OSS Distributions 
233*43a90889SApple OSS Distributions static void *
test_thread_shim(void * arg)234*43a90889SApple OSS Distributions test_thread_shim(void *arg)
235*43a90889SApple OSS Distributions {
236*43a90889SApple OSS Distributions 	struct test_thread const *thread = arg;
237*43a90889SApple OSS Distributions 
238*43a90889SApple OSS Distributions 	thread_bind_cpu(thread->cpuid);
239*43a90889SApple OSS Distributions 	bool const ret = thread->thread_fn(thread);
240*43a90889SApple OSS Distributions 	test_thread_notify_exited();
241*43a90889SApple OSS Distributions 	return (void *)(uintptr_t)ret;
242*43a90889SApple OSS Distributions }
243*43a90889SApple OSS Distributions 
244*43a90889SApple OSS Distributions static void
test_on_each_cpu(thread_fn_t thread_fn,const struct arm_matrix_operations * ops,const char * desc)245*43a90889SApple OSS Distributions test_on_each_cpu(thread_fn_t thread_fn, const struct arm_matrix_operations *ops, const char *desc)
246*43a90889SApple OSS Distributions {
247*43a90889SApple OSS Distributions 	uint32_t ncpu = ncpus();
248*43a90889SApple OSS Distributions 	uint32_t nthreads = ncpu * 2;
249*43a90889SApple OSS Distributions 	barrier = 1 /* This thread */ + nthreads;
250*43a90889SApple OSS Distributions 	end_barrier = nthreads;
251*43a90889SApple OSS Distributions 	struct test_thread *threads = calloc(nthreads, sizeof(threads[0]));
252*43a90889SApple OSS Distributions 
253*43a90889SApple OSS Distributions 	for (uint32_t i = 0; i < nthreads; i++) {
254*43a90889SApple OSS Distributions 		threads[i].thread_fn = thread_fn;
255*43a90889SApple OSS Distributions 		threads[i].cpuid = i % ncpu;
256*43a90889SApple OSS Distributions 		threads[i].thread_id = i;
257*43a90889SApple OSS Distributions 		threads[i].ops = ops;
258*43a90889SApple OSS Distributions 
259*43a90889SApple OSS Distributions 		int const err = pthread_create(&threads[i].thread, NULL, test_thread_shim, &threads[i]);
260*43a90889SApple OSS Distributions 		T_QUIET; T_ASSERT_EQ(err, 0, "%s: created thread #%u", desc, i);
261*43a90889SApple OSS Distributions 
262*43a90889SApple OSS Distributions 		// The other of two threads under test pinned to the same CPU.
263*43a90889SApple OSS Distributions 		threads[(ncpu + i) % nthreads].companion_thread = threads[i].thread;
264*43a90889SApple OSS Distributions 	}
265*43a90889SApple OSS Distributions 
266*43a90889SApple OSS Distributions 	// Wait for all companion_threads to be set.
267*43a90889SApple OSS Distributions 	test_thread_barrier();
268*43a90889SApple OSS Distributions 
269*43a90889SApple OSS Distributions 	// like pthread_join()ing all threads, but without the priority boosting shenanigans.
270*43a90889SApple OSS Distributions 	wait_for_test_threads();
271*43a90889SApple OSS Distributions 
272*43a90889SApple OSS Distributions 	for (uint32_t i = 0; i < nthreads; i++) {
273*43a90889SApple OSS Distributions 		void *thread_ret_ptr;
274*43a90889SApple OSS Distributions 		int err = pthread_join(threads[i].thread, &thread_ret_ptr);
275*43a90889SApple OSS Distributions 		T_QUIET; T_ASSERT_EQ(err, 0, "%s: joined thread #%u", desc, i);
276*43a90889SApple OSS Distributions 
277*43a90889SApple OSS Distributions 		bool thread_ret = (uintptr_t)thread_ret_ptr;
278*43a90889SApple OSS Distributions 		if (thread_ret) {
279*43a90889SApple OSS Distributions 			T_PASS("%s: thread #%u passed", desc, i);
280*43a90889SApple OSS Distributions 		} else {
281*43a90889SApple OSS Distributions 			T_FAIL("%s: thread #%u failed", desc, i);
282*43a90889SApple OSS Distributions 		}
283*43a90889SApple OSS Distributions 	}
284*43a90889SApple OSS Distributions 
285*43a90889SApple OSS Distributions 	free(threads);
286*43a90889SApple OSS Distributions }
287*43a90889SApple OSS Distributions 
288*43a90889SApple OSS Distributions static bool
active_context_switch_thread(struct test_thread const * thread)289*43a90889SApple OSS Distributions active_context_switch_thread(struct test_thread const* thread)
290*43a90889SApple OSS Distributions {
291*43a90889SApple OSS Distributions 	const struct arm_matrix_operations *ops = thread->ops;
292*43a90889SApple OSS Distributions 	const uint32_t thread_id = thread->thread_id;
293*43a90889SApple OSS Distributions 	size_t size = ops->data_size();
294*43a90889SApple OSS Distributions 	uint8_t *d1 = ops->alloc_data();
295*43a90889SApple OSS Distributions 	memset(d1, (char)thread_id, size);
296*43a90889SApple OSS Distributions 
297*43a90889SApple OSS Distributions 	uint8_t *d2 = ops->alloc_data();
298*43a90889SApple OSS Distributions 
299*43a90889SApple OSS Distributions 	test_thread_barrier();
300*43a90889SApple OSS Distributions 
301*43a90889SApple OSS Distributions 	// companion_thread will be valid only after the barrier.
302*43a90889SApple OSS Distributions 	thread_t const companion_thread = pthread_mach_thread_np(thread->companion_thread);
303*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_NE(companion_thread, THREAD_NULL, "pthread_mach_thread_np");
304*43a90889SApple OSS Distributions 
305*43a90889SApple OSS Distributions 	bool ok = true;
306*43a90889SApple OSS Distributions 	for (unsigned int i = 0; i < 100000 && ok; i++) {
307*43a90889SApple OSS Distributions 		ops->start();
308*43a90889SApple OSS Distributions 		ops->load_data(d1);
309*43a90889SApple OSS Distributions 
310*43a90889SApple OSS Distributions 		/*
311*43a90889SApple OSS Distributions 		 * Rescheduling with the matrix registers active must preserve
312*43a90889SApple OSS Distributions 		 * state, even after a context switch.
313*43a90889SApple OSS Distributions 		 */
314*43a90889SApple OSS Distributions 		thread_switch(companion_thread, SWITCH_OPTION_NONE, 0);
315*43a90889SApple OSS Distributions 
316*43a90889SApple OSS Distributions 		ops->store_data(d2);
317*43a90889SApple OSS Distributions 		ops->stop();
318*43a90889SApple OSS Distributions 
319*43a90889SApple OSS Distributions 		if (memcmp(d1, d2, size)) {
320*43a90889SApple OSS Distributions 			ok = false;
321*43a90889SApple OSS Distributions 		}
322*43a90889SApple OSS Distributions 	}
323*43a90889SApple OSS Distributions 
324*43a90889SApple OSS Distributions 	free(d2);
325*43a90889SApple OSS Distributions 	free(d1);
326*43a90889SApple OSS Distributions 	return ok;
327*43a90889SApple OSS Distributions }
328*43a90889SApple OSS Distributions 
329*43a90889SApple OSS Distributions static bool
inactive_context_switch_thread(struct test_thread const * thread)330*43a90889SApple OSS Distributions inactive_context_switch_thread(struct test_thread const* thread)
331*43a90889SApple OSS Distributions {
332*43a90889SApple OSS Distributions 	const struct arm_matrix_operations *ops = thread->ops;
333*43a90889SApple OSS Distributions 	const uint32_t thread_id = thread->thread_id;
334*43a90889SApple OSS Distributions 	size_t size = ops->data_size();
335*43a90889SApple OSS Distributions 	uint8_t *d1 = ops->alloc_data();
336*43a90889SApple OSS Distributions 	memset(d1, (char)thread_id, size);
337*43a90889SApple OSS Distributions 
338*43a90889SApple OSS Distributions 	uint8_t *d2 = ops->alloc_data();
339*43a90889SApple OSS Distributions 
340*43a90889SApple OSS Distributions 	test_thread_barrier();
341*43a90889SApple OSS Distributions 
342*43a90889SApple OSS Distributions 	// companion_thread will be valid only after the barrier.
343*43a90889SApple OSS Distributions 	thread_t const companion_thread = pthread_mach_thread_np(thread->companion_thread);
344*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_NE(companion_thread, THREAD_NULL, "pthread_mach_thread_np");
345*43a90889SApple OSS Distributions 
346*43a90889SApple OSS Distributions 	bool ok = true;
347*43a90889SApple OSS Distributions 	for (unsigned int i = 0; i < 100000 && ok; i++) {
348*43a90889SApple OSS Distributions 		ops->start();
349*43a90889SApple OSS Distributions 		ops->load_data(d1);
350*43a90889SApple OSS Distributions 		ops->stop();
351*43a90889SApple OSS Distributions 
352*43a90889SApple OSS Distributions 		/*
353*43a90889SApple OSS Distributions 		 * Rescheduling with the matrix registers inactive may preserve
354*43a90889SApple OSS Distributions 		 * state or may zero it out.
355*43a90889SApple OSS Distributions 		 */
356*43a90889SApple OSS Distributions 		thread_switch(companion_thread, SWITCH_OPTION_NONE, 0);
357*43a90889SApple OSS Distributions 
358*43a90889SApple OSS Distributions 		ops->start();
359*43a90889SApple OSS Distributions 		ops->store_data(d2);
360*43a90889SApple OSS Distributions 		ops->stop();
361*43a90889SApple OSS Distributions 
362*43a90889SApple OSS Distributions 		for (size_t j = 0; j < size; j++) {
363*43a90889SApple OSS Distributions 			if (d1[j] != d2[j] && d2[j] != 0) {
364*43a90889SApple OSS Distributions 				ok = false;
365*43a90889SApple OSS Distributions 			}
366*43a90889SApple OSS Distributions 		}
367*43a90889SApple OSS Distributions 	}
368*43a90889SApple OSS Distributions 
369*43a90889SApple OSS Distributions 	free(d2);
370*43a90889SApple OSS Distributions 	free(d1);
371*43a90889SApple OSS Distributions 	return ok;
372*43a90889SApple OSS Distributions }
373*43a90889SApple OSS Distributions 
374*43a90889SApple OSS Distributions static void
test_thread_migration(const struct arm_matrix_operations * ops)375*43a90889SApple OSS Distributions test_thread_migration(const struct arm_matrix_operations *ops)
376*43a90889SApple OSS Distributions {
377*43a90889SApple OSS Distributions 	size_t size = ops->data_size();
378*43a90889SApple OSS Distributions 	uint8_t *d = ops->alloc_data();
379*43a90889SApple OSS Distributions 	arc4random_buf(d, size);
380*43a90889SApple OSS Distributions 
381*43a90889SApple OSS Distributions 	uint32_t ncpu = ncpus();
382*43a90889SApple OSS Distributions 	uint8_t *cpu_d[ncpu];
383*43a90889SApple OSS Distributions 	for (uint32_t cpuid = 0; cpuid < ncpu; cpuid++) {
384*43a90889SApple OSS Distributions 		cpu_d[cpuid] = ops->alloc_data();
385*43a90889SApple OSS Distributions 		memset(cpu_d[cpuid], 0, size);
386*43a90889SApple OSS Distributions 	}
387*43a90889SApple OSS Distributions 
388*43a90889SApple OSS Distributions 	ops->start();
389*43a90889SApple OSS Distributions 	ops->load_data(d);
390*43a90889SApple OSS Distributions 	for (uint32_t cpuid = 0; cpuid < ncpu; cpuid++) {
391*43a90889SApple OSS Distributions 		int err = thread_bind_cpu_unchecked(cpuid);
392*43a90889SApple OSS Distributions 		if (err) {
393*43a90889SApple OSS Distributions 			ops->stop();
394*43a90889SApple OSS Distributions 			T_ASSERT_POSIX_ZERO(err, "Bound thread to CPU %u", cpuid);
395*43a90889SApple OSS Distributions 		}
396*43a90889SApple OSS Distributions 		ops->store_data(cpu_d[cpuid]);
397*43a90889SApple OSS Distributions 	}
398*43a90889SApple OSS Distributions 	ops->stop();
399*43a90889SApple OSS Distributions 
400*43a90889SApple OSS Distributions 	for (uint32_t cpuid = 0; cpuid < ncpu; cpuid++) {
401*43a90889SApple OSS Distributions 		int cmp = memcmp(d, cpu_d[cpuid], size);
402*43a90889SApple OSS Distributions 		T_EXPECT_EQ(cmp, 0, "Matrix state migrated to CPU %u", cpuid);
403*43a90889SApple OSS Distributions 		free(cpu_d[cpuid]);
404*43a90889SApple OSS Distributions 	}
405*43a90889SApple OSS Distributions 	free(d);
406*43a90889SApple OSS Distributions }
407*43a90889SApple OSS Distributions #endif
408*43a90889SApple OSS Distributions 
409*43a90889SApple OSS Distributions 
410*43a90889SApple OSS Distributions T_DECL(sme_context_switch,
411*43a90889SApple OSS Distributions     "Test that SME contexts are migrated during context switch and do not leak between process contexts.",
412*43a90889SApple OSS Distributions     T_META_BOOTARGS_SET("enable_skstb=1"),
413*43a90889SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_SME2", 1),
414*43a90889SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC, T_META_TAG_VM_NOT_ELIGIBLE)
415*43a90889SApple OSS Distributions {
416*43a90889SApple OSS Distributions #ifndef __arm64__
417*43a90889SApple OSS Distributions 	T_SKIP("Running on non-arm64 target, skipping...");
418*43a90889SApple OSS Distributions #else
419*43a90889SApple OSS Distributions 	if (!sme_operations.is_available()) {
420*43a90889SApple OSS Distributions 		T_SKIP("Running on non-SME target, skipping...");
421*43a90889SApple OSS Distributions 	}
422*43a90889SApple OSS Distributions 
423*43a90889SApple OSS Distributions 	test_thread_migration(&sme_operations);
424*43a90889SApple OSS Distributions 	test_on_each_cpu(active_context_switch_thread, &sme_operations, "SME context migrates when active");
425*43a90889SApple OSS Distributions 	test_on_each_cpu(inactive_context_switch_thread, &sme_operations, "SME context does not leak across processes");
426*43a90889SApple OSS Distributions #endif
427*43a90889SApple OSS Distributions }
428*43a90889SApple OSS Distributions 
429*43a90889SApple OSS Distributions 
430*43a90889SApple OSS Distributions #if __arm64__
431*43a90889SApple OSS Distributions /*
432*43a90889SApple OSS Distributions  * Sequence of events in thread_{get,set}_state test:
433*43a90889SApple OSS Distributions  *
434*43a90889SApple OSS Distributions  * 1. Parent creates child thread.
435*43a90889SApple OSS Distributions  * 2. Child thread signals parent thread to proceed.
436*43a90889SApple OSS Distributions  * 3. Parent populates child's matrix state registers via thread_set_state(),
437*43a90889SApple OSS Distributions  *    and signals child thread to proceed.
438*43a90889SApple OSS Distributions  * 4. Child arbitrarily updates each byte in its local matrix register state
439*43a90889SApple OSS Distributions  *    by adding 1, and signals parent thread to proceed.
440*43a90889SApple OSS Distributions  * 5. Parent reads back the child's updated matrix state with
441*43a90889SApple OSS Distributions  *    thread_get_state(), and confirms that every byte has been modified as
442*43a90889SApple OSS Distributions  *    expected.
443*43a90889SApple OSS Distributions  */
444*43a90889SApple OSS Distributions static enum thread_state_test_state {
445*43a90889SApple OSS Distributions 	INIT,
446*43a90889SApple OSS Distributions 	CHILD_READY,
447*43a90889SApple OSS Distributions 	PARENT_POPULATED_MATRIX_STATE,
448*43a90889SApple OSS Distributions 	CHILD_UPDATED_MATRIX_STATE,
449*43a90889SApple OSS Distributions 	DONE
450*43a90889SApple OSS Distributions } thread_state_test_state;
451*43a90889SApple OSS Distributions 
452*43a90889SApple OSS Distributions static pthread_cond_t thread_state_test_cond = PTHREAD_COND_INITIALIZER;
453*43a90889SApple OSS Distributions static pthread_mutex_t thread_state_test_lock = PTHREAD_MUTEX_INITIALIZER;
454*43a90889SApple OSS Distributions 
455*43a90889SApple OSS Distributions static void
wait_for_thread_state_test_state(enum thread_state_test_state state)456*43a90889SApple OSS Distributions wait_for_thread_state_test_state(enum thread_state_test_state state)
457*43a90889SApple OSS Distributions {
458*43a90889SApple OSS Distributions 	pthread_mutex_lock(&thread_state_test_lock);
459*43a90889SApple OSS Distributions 	while (thread_state_test_state != state) {
460*43a90889SApple OSS Distributions 		pthread_cond_wait(&thread_state_test_cond, &thread_state_test_lock);
461*43a90889SApple OSS Distributions 	}
462*43a90889SApple OSS Distributions 	pthread_mutex_unlock(&thread_state_test_lock);
463*43a90889SApple OSS Distributions }
464*43a90889SApple OSS Distributions 
465*43a90889SApple OSS Distributions static void
thread_set_state_test_state(enum thread_state_test_state state)466*43a90889SApple OSS Distributions thread_set_state_test_state(enum thread_state_test_state state)
467*43a90889SApple OSS Distributions {
468*43a90889SApple OSS Distributions 	pthread_mutex_lock(&thread_state_test_lock);
469*43a90889SApple OSS Distributions 	thread_state_test_state = state;
470*43a90889SApple OSS Distributions 	pthread_cond_broadcast(&thread_state_test_cond);
471*43a90889SApple OSS Distributions 	pthread_mutex_unlock(&thread_state_test_lock);
472*43a90889SApple OSS Distributions }
473*43a90889SApple OSS Distributions 
474*43a90889SApple OSS Distributions static void *
test_matrix_thread_state_child(void * arg __unused)475*43a90889SApple OSS Distributions test_matrix_thread_state_child(void *arg __unused)
476*43a90889SApple OSS Distributions {
477*43a90889SApple OSS Distributions 	const struct arm_matrix_operations *ops = arg;
478*43a90889SApple OSS Distributions 
479*43a90889SApple OSS Distributions 	size_t size = ops->data_size();
480*43a90889SApple OSS Distributions 	uint8_t *d = ops->alloc_data();
481*43a90889SApple OSS Distributions 
482*43a90889SApple OSS Distributions 
483*43a90889SApple OSS Distributions 	thread_set_state_test_state(CHILD_READY);
484*43a90889SApple OSS Distributions 	wait_for_thread_state_test_state(PARENT_POPULATED_MATRIX_STATE);
485*43a90889SApple OSS Distributions 	ops->store_data(d);
486*43a90889SApple OSS Distributions 	for (size_t i = 0; i < size; i++) {
487*43a90889SApple OSS Distributions 		d[i]++;
488*43a90889SApple OSS Distributions 	}
489*43a90889SApple OSS Distributions 	ops->load_data(d);
490*43a90889SApple OSS Distributions 	thread_set_state_test_state(CHILD_UPDATED_MATRIX_STATE);
491*43a90889SApple OSS Distributions 
492*43a90889SApple OSS Distributions 	wait_for_thread_state_test_state(DONE);
493*43a90889SApple OSS Distributions 	ops->stop();
494*43a90889SApple OSS Distributions 	return NULL;
495*43a90889SApple OSS Distributions }
496*43a90889SApple OSS Distributions 
497*43a90889SApple OSS Distributions static void
test_matrix_thread_state(const struct arm_matrix_operations * ops)498*43a90889SApple OSS Distributions test_matrix_thread_state(const struct arm_matrix_operations *ops)
499*43a90889SApple OSS Distributions {
500*43a90889SApple OSS Distributions 	if (!ops->is_available()) {
501*43a90889SApple OSS Distributions 		T_SKIP("Running on non-%s target, skipping...", ops->name);
502*43a90889SApple OSS Distributions 	}
503*43a90889SApple OSS Distributions 
504*43a90889SApple OSS Distributions 	size_t size = ops->data_size();
505*43a90889SApple OSS Distributions 	uint8_t *d = ops->alloc_data();
506*43a90889SApple OSS Distributions 	arc4random_buf(d, size);
507*43a90889SApple OSS Distributions 
508*43a90889SApple OSS Distributions 	thread_state_test_state = INIT;
509*43a90889SApple OSS Distributions 
510*43a90889SApple OSS Distributions 	pthread_t thread;
511*43a90889SApple OSS Distributions #pragma clang diagnostic push
512*43a90889SApple OSS Distributions #pragma clang diagnostic ignored "-Wincompatible-pointer-types-discards-qualifiers"
513*43a90889SApple OSS Distributions 	void *arg = ops;
514*43a90889SApple OSS Distributions #pragma clang diagnostic pop
515*43a90889SApple OSS Distributions 	int err = pthread_create(&thread, NULL, test_matrix_thread_state_child, arg);
516*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(err, 0, "pthread_create()");
517*43a90889SApple OSS Distributions 
518*43a90889SApple OSS Distributions 	mach_port_t mach_thread = pthread_mach_thread_np(thread);
519*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_NE(mach_thread, MACH_PORT_NULL, "pthread_mach_thread_np()");
520*43a90889SApple OSS Distributions 
521*43a90889SApple OSS Distributions 	wait_for_thread_state_test_state(CHILD_READY);
522*43a90889SApple OSS Distributions 	kern_return_t kr = ops->thread_set_state(mach_thread, d);
523*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(kr, KERN_SUCCESS, "%s thread_set_state()", ops->name);
524*43a90889SApple OSS Distributions 	thread_set_state_test_state(PARENT_POPULATED_MATRIX_STATE);
525*43a90889SApple OSS Distributions 
526*43a90889SApple OSS Distributions 	wait_for_thread_state_test_state(CHILD_UPDATED_MATRIX_STATE);
527*43a90889SApple OSS Distributions 	uint8_t *thread_d = ops->alloc_data();
528*43a90889SApple OSS Distributions 	kr = ops->thread_get_state(mach_thread, thread_d);
529*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(kr, KERN_SUCCESS, "%s thread_get_state()", ops->name);
530*43a90889SApple OSS Distributions 	for (size_t i = 0; i < size; i++) {
531*43a90889SApple OSS Distributions 		d[i]++;
532*43a90889SApple OSS Distributions 	}
533*43a90889SApple OSS Distributions 	T_EXPECT_EQ(memcmp(d, thread_d, size), 0, "thread_get_state() read expected %s data from child thread", ops->name);
534*43a90889SApple OSS Distributions 
535*43a90889SApple OSS Distributions 	thread_set_state_test_state(DONE);
536*43a90889SApple OSS Distributions 	free(thread_d);
537*43a90889SApple OSS Distributions 	free(d);
538*43a90889SApple OSS Distributions 	pthread_join(thread, NULL);
539*43a90889SApple OSS Distributions }
540*43a90889SApple OSS Distributions 
541*43a90889SApple OSS Distributions #endif
542*43a90889SApple OSS Distributions 
543*43a90889SApple OSS Distributions #ifdef __arm64__
544*43a90889SApple OSS Distributions 
545*43a90889SApple OSS Distributions T_DECL(sme_thread_state,
546*43a90889SApple OSS Distributions     "Test thread_{get,set}_state with SME thread state.",
547*43a90889SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC)
548*43a90889SApple OSS Distributions {
549*43a90889SApple OSS Distributions 	test_matrix_thread_state(&sme_operations);
550*43a90889SApple OSS Distributions }
551*43a90889SApple OSS Distributions 
552*43a90889SApple OSS Distributions T_DECL(sme_exception_ports,
553*43a90889SApple OSS Distributions     "Test that thread_set_exception_ports rejects SME thread-state flavors.",
554*43a90889SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC)
555*43a90889SApple OSS Distributions {
556*43a90889SApple OSS Distributions 	mach_port_t exc_port;
557*43a90889SApple OSS Distributions 	mach_port_t task = mach_task_self();
558*43a90889SApple OSS Distributions 	mach_port_t thread = mach_thread_self();
559*43a90889SApple OSS Distributions 
560*43a90889SApple OSS Distributions 	kern_return_t kr = mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, &exc_port);
561*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "Allocated mach exception port");
562*43a90889SApple OSS Distributions 	kr = mach_port_insert_right(task, exc_port, exc_port, MACH_MSG_TYPE_MAKE_SEND);
563*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "Inserted a SEND right into the exception port");
564*43a90889SApple OSS Distributions 
565*43a90889SApple OSS Distributions 	kr = thread_set_exception_ports(thread, EXC_MASK_ALL, exc_port, EXCEPTION_STATE, ARM_THREAD_STATE64);
566*43a90889SApple OSS Distributions 	T_EXPECT_MACH_SUCCESS(kr, "thread_set_exception_ports accepts flavor %u", (unsigned int)ARM_THREAD_STATE64);
567*43a90889SApple OSS Distributions 
568*43a90889SApple OSS Distributions 	for (thread_state_flavor_t flavor = ARM_SME_STATE; flavor <= ARM_SME2_STATE; flavor++) {
569*43a90889SApple OSS Distributions 		kr = thread_set_exception_ports(thread, EXC_MASK_ALL, exc_port, EXCEPTION_STATE, flavor);
570*43a90889SApple OSS Distributions 		T_EXPECT_MACH_ERROR(kr, KERN_INVALID_ARGUMENT, "thread_set_exception_ports rejects flavor %u", (unsigned int)flavor);
571*43a90889SApple OSS Distributions 	}
572*43a90889SApple OSS Distributions }
573*43a90889SApple OSS Distributions 
574*43a90889SApple OSS Distributions T_DECL(sme_max_svl_b_sysctl,
575*43a90889SApple OSS Distributions     "Test the hw.optional.arm.sme_max_svl_b sysctl",
576*43a90889SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC)
577*43a90889SApple OSS Distributions {
578*43a90889SApple OSS Distributions 	unsigned int max_svl_b;
579*43a90889SApple OSS Distributions 	size_t max_svl_b_size = sizeof(max_svl_b);
580*43a90889SApple OSS Distributions 
581*43a90889SApple OSS Distributions 	int err = sysctlbyname("hw.optional.arm.sme_max_svl_b", &max_svl_b, &max_svl_b_size, NULL, 0);
582*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(err, "sysctlbyname(hw.optional.arm.sme_max_svl_b)");
583*43a90889SApple OSS Distributions 	if (sme_operations.is_available()) {
584*43a90889SApple OSS Distributions 		/* Architecturally SVL must be a power-of-two between 128 and 2048 bits */
585*43a90889SApple OSS Distributions 		const unsigned int ARCH_MIN_SVL_B = 128 / 8;
586*43a90889SApple OSS Distributions 		const unsigned int ARCH_MAX_SVL_B = 2048 / 8;
587*43a90889SApple OSS Distributions 
588*43a90889SApple OSS Distributions 		T_EXPECT_EQ(__builtin_popcount(max_svl_b), 1, "Maximum SVL_B is a power of 2");
589*43a90889SApple OSS Distributions 		T_EXPECT_GE(max_svl_b, ARCH_MIN_SVL_B, "Maximum SVL_B >= architectural minimum");
590*43a90889SApple OSS Distributions 		T_EXPECT_LE(max_svl_b, ARCH_MAX_SVL_B, "Maximum SVL_B <= architectural maximum");
591*43a90889SApple OSS Distributions 	} else {
592*43a90889SApple OSS Distributions 		T_EXPECT_EQ(max_svl_b, 0, "Maximum SVL_B is 0 when SME is unavailable");
593*43a90889SApple OSS Distributions 	}
594*43a90889SApple OSS Distributions }
595*43a90889SApple OSS Distributions #endif /* __arm64__ */
596