2 * Copyright (c) 2011-2018 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System Copyright (c) 1991,1990,1989,1988,1987 Carnegie
33 * Mellon University All Rights Reserved.
35 * Permission to use, copy, modify and distribute this software and its
36 * documentation is hereby granted, provided that both the copyright notice
37 * and this permission notice appear in all copies of the software,
38 * derivative works or modified versions, and any portions thereof, and that
39 * both notices appear in supporting documentation.
41 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.
42 * CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES
43 * WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45 * Carnegie Mellon requests users of this software to return to
47 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
48 * School of Computer Science Carnegie Mellon University Pittsburgh PA
51 * any improvements or extensions that they make and grant Carnegie Mellon the
52 * rights to redistribute these changes.
55 #include <mach_ldebug.h>
57 #define LOCK_PRIVATE 1
60 #include <kern/kalloc.h>
61 #include <kern/locks.h>
62 #include <kern/misc_protos.h>
63 #include <kern/thread.h>
64 #include <kern/processor.h>
65 #include <kern/sched_prim.h>
66 #include <kern/debug.h>
68 #include <tests/xnupost.h>
71 #include <ddb/db_command.h>
72 #include <ddb/db_output.h>
73 #include <ddb/db_sym.h>
74 #include <ddb/db_print.h>
77 #include <sys/kdebug.h>
78 #include <sys/munge.h>
79 #include <machine/cpu_capabilities.h>
80 #include <arm/cpu_data_internal.h>
83 kern_return_t
arm64_lock_test(void);
84 kern_return_t
arm64_munger_test(void);
85 kern_return_t
ex_cb_test(void);
86 kern_return_t
arm64_pan_test(void);
87 kern_return_t
arm64_late_pan_test(void);
88 #if defined(HAS_APPLE_PAC)
90 kern_return_t
arm64_ropjop_test(void);
92 #if HAS_TWO_STAGE_SPR_LOCK
93 kern_return_t
arm64_spr_lock_test(void);
94 extern void arm64_msr_lock_test(uint64_t);
97 // exception handler ignores this fault address during PAN test
98 #if __ARM_PAN_AVAILABLE__
99 const uint64_t pan_ro_value
= 0xFEEDB0B0DEADBEEF;
100 vm_offset_t pan_test_addr
= 0;
101 vm_offset_t pan_ro_addr
= 0;
102 volatile int pan_exception_level
= 0;
103 volatile char pan_fault_value
= 0;
106 #include <libkern/OSAtomic.h>
107 #define LOCK_TEST_ITERATIONS 50
108 static hw_lock_data_t lt_hw_lock
;
109 static lck_spin_t lt_lck_spin_t
;
110 static lck_mtx_t lt_mtx
;
111 static lck_rw_t lt_rwlock
;
112 static volatile uint32_t lt_counter
= 0;
113 static volatile int lt_spinvolatile
;
114 static volatile uint32_t lt_max_holders
= 0;
115 static volatile uint32_t lt_upgrade_holders
= 0;
116 static volatile uint32_t lt_max_upgrade_holders
= 0;
117 static volatile uint32_t lt_num_holders
= 0;
118 static volatile uint32_t lt_done_threads
;
119 static volatile uint32_t lt_target_done_threads
;
120 static volatile uint32_t lt_cpu_bind_id
= 0;
123 lt_note_another_blocking_lock_holder()
125 hw_lock_lock(<_hw_lock
, LCK_GRP_NULL
);
127 lt_max_holders
= (lt_max_holders
< lt_num_holders
) ? lt_num_holders
: lt_max_holders
;
128 hw_lock_unlock(<_hw_lock
);
132 lt_note_blocking_lock_release()
134 hw_lock_lock(<_hw_lock
, LCK_GRP_NULL
);
136 hw_lock_unlock(<_hw_lock
);
140 lt_spin_a_little_bit()
144 for (i
= 0; i
< 10000; i
++) {
150 lt_sleep_a_little_bit()
158 lck_mtx_lock(<_mtx
);
159 lt_note_another_blocking_lock_holder();
160 lt_sleep_a_little_bit();
162 lt_note_blocking_lock_release();
163 lck_mtx_unlock(<_mtx
);
167 lt_grab_mutex_with_try()
169 while (0 == lck_mtx_try_lock(<_mtx
)) {
172 lt_note_another_blocking_lock_holder();
173 lt_sleep_a_little_bit();
175 lt_note_blocking_lock_release();
176 lck_mtx_unlock(<_mtx
);
180 lt_grab_rw_exclusive()
182 lck_rw_lock_exclusive(<_rwlock
);
183 lt_note_another_blocking_lock_holder();
184 lt_sleep_a_little_bit();
186 lt_note_blocking_lock_release();
187 lck_rw_done(<_rwlock
);
191 lt_grab_rw_exclusive_with_try()
193 while (0 == lck_rw_try_lock_exclusive(<_rwlock
)) {
194 lt_sleep_a_little_bit();
197 lt_note_another_blocking_lock_holder();
198 lt_sleep_a_little_bit();
200 lt_note_blocking_lock_release();
201 lck_rw_done(<_rwlock
);
204 /* Disabled until lt_grab_rw_shared() is fixed (rdar://30685840)
206 * lt_grab_rw_shared()
208 * lck_rw_lock_shared(<_rwlock);
211 * lt_note_another_blocking_lock_holder();
212 * lt_sleep_a_little_bit();
213 * lt_note_blocking_lock_release();
215 * lck_rw_done(<_rwlock);
219 /* Disabled until lt_grab_rw_shared_with_try() is fixed (rdar://30685840)
221 * lt_grab_rw_shared_with_try()
223 * while(0 == lck_rw_try_lock_shared(<_rwlock));
226 * lt_note_another_blocking_lock_holder();
227 * lt_sleep_a_little_bit();
228 * lt_note_blocking_lock_release();
230 * lck_rw_done(<_rwlock);
235 lt_upgrade_downgrade_rw()
237 boolean_t upgraded
, success
;
239 success
= lck_rw_try_lock_shared(<_rwlock
);
241 lck_rw_lock_shared(<_rwlock
);
244 lt_note_another_blocking_lock_holder();
245 lt_sleep_a_little_bit();
246 lt_note_blocking_lock_release();
248 upgraded
= lck_rw_lock_shared_to_exclusive(<_rwlock
);
250 success
= lck_rw_try_lock_exclusive(<_rwlock
);
253 lck_rw_lock_exclusive(<_rwlock
);
257 lt_upgrade_holders
++;
258 if (lt_upgrade_holders
> lt_max_upgrade_holders
) {
259 lt_max_upgrade_holders
= lt_upgrade_holders
;
263 lt_sleep_a_little_bit();
265 lt_upgrade_holders
--;
267 lck_rw_lock_exclusive_to_shared(<_rwlock
);
269 lt_spin_a_little_bit();
270 lck_rw_done(<_rwlock
);
277 hw_lock_lock(<_hw_lock
, LCK_GRP_NULL
);
279 lt_spin_a_little_bit();
280 hw_lock_unlock(<_hw_lock
);
284 lt_grab_hw_lock_with_try()
286 while (0 == hw_lock_try(<_hw_lock
, LCK_GRP_NULL
)) {
290 lt_spin_a_little_bit();
291 hw_lock_unlock(<_hw_lock
);
295 lt_grab_hw_lock_with_to()
297 while (0 == hw_lock_to(<_hw_lock
, LockTimeOut
, LCK_GRP_NULL
)) {
298 mp_enable_preemption();
301 lt_spin_a_little_bit();
302 hw_lock_unlock(<_hw_lock
);
308 lck_spin_lock(<_lck_spin_t
);
310 lt_spin_a_little_bit();
311 lck_spin_unlock(<_lck_spin_t
);
315 lt_grab_spin_lock_with_try()
317 while (0 == lck_spin_try_lock(<_lck_spin_t
)) {
321 lt_spin_a_little_bit();
322 lck_spin_unlock(<_lck_spin_t
);
325 static volatile boolean_t lt_thread_lock_grabbed
;
326 static volatile boolean_t lt_thread_lock_success
;
334 lt_max_upgrade_holders
= 0;
335 lt_upgrade_holders
= 0;
337 lt_target_done_threads
= 0;
344 lt_trylock_hw_lock_with_to()
347 while (!lt_thread_lock_grabbed
) {
348 lt_sleep_a_little_bit();
351 lt_thread_lock_success
= hw_lock_to(<_hw_lock
, 100, LCK_GRP_NULL
);
353 mp_enable_preemption();
357 lt_trylock_spin_try_lock()
360 while (!lt_thread_lock_grabbed
) {
361 lt_sleep_a_little_bit();
364 lt_thread_lock_success
= lck_spin_try_lock(<_lck_spin_t
);
369 lt_trylock_thread(void *arg
, wait_result_t wres __unused
)
371 void (*func
)(void) = (void (*)(void))arg
;
375 OSIncrementAtomic((volatile SInt32
*) <_done_threads
);
379 lt_start_trylock_thread(thread_continue_t func
)
384 kr
= kernel_thread_start(lt_trylock_thread
, func
, &thread
);
385 assert(kr
== KERN_SUCCESS
);
387 thread_deallocate(thread
);
391 lt_wait_for_lock_test_threads()
394 /* Spin to reduce dependencies */
395 while (lt_done_threads
< lt_target_done_threads
) {
396 lt_sleep_a_little_bit();
406 extern unsigned int real_ncpus
;
409 * First mtx try lock succeeds, second fails.
411 success
= lck_mtx_try_lock(<_mtx
);
412 T_ASSERT_NOTNULL(success
, "First mtx try lock");
413 success
= lck_mtx_try_lock(<_mtx
);
414 T_ASSERT_NULL(success
, "Second mtx try lock for a locked mtx");
415 lck_mtx_unlock(<_mtx
);
418 * After regular grab, can't try lock.
420 lck_mtx_lock(<_mtx
);
421 success
= lck_mtx_try_lock(<_mtx
);
422 T_ASSERT_NULL(success
, "try lock should fail after regular lck_mtx_lock");
423 lck_mtx_unlock(<_mtx
);
426 * Two shared try locks on a previously unheld rwlock suceed, and a
427 * subsequent exclusive attempt fails.
429 success
= lck_rw_try_lock_shared(<_rwlock
);
430 T_ASSERT_NOTNULL(success
, "Two shared try locks on a previously unheld rwlock should succeed");
431 success
= lck_rw_try_lock_shared(<_rwlock
);
432 T_ASSERT_NOTNULL(success
, "Two shared try locks on a previously unheld rwlock should succeed");
433 success
= lck_rw_try_lock_exclusive(<_rwlock
);
434 T_ASSERT_NULL(success
, "exclusive lock attempt on previously held lock should fail");
435 lck_rw_done(<_rwlock
);
436 lck_rw_done(<_rwlock
);
439 * After regular shared grab, can trylock
440 * for shared but not for exclusive.
442 lck_rw_lock_shared(<_rwlock
);
443 success
= lck_rw_try_lock_shared(<_rwlock
);
444 T_ASSERT_NOTNULL(success
, "After regular shared grab another shared try lock should succeed.");
445 success
= lck_rw_try_lock_exclusive(<_rwlock
);
446 T_ASSERT_NULL(success
, "After regular shared grab an exclusive lock attempt should fail.");
447 lck_rw_done(<_rwlock
);
448 lck_rw_done(<_rwlock
);
451 * An exclusive try lock succeeds, subsequent shared and exclusive
454 success
= lck_rw_try_lock_exclusive(<_rwlock
);
455 T_ASSERT_NOTNULL(success
, "An exclusive try lock should succeed");
456 success
= lck_rw_try_lock_shared(<_rwlock
);
457 T_ASSERT_NULL(success
, "try lock in shared mode attempt after an exclusive grab should fail");
458 success
= lck_rw_try_lock_exclusive(<_rwlock
);
459 T_ASSERT_NULL(success
, "try lock in exclusive mode attempt after an exclusive grab should fail");
460 lck_rw_done(<_rwlock
);
463 * After regular exclusive grab, neither kind of trylock succeeds.
465 lck_rw_lock_exclusive(<_rwlock
);
466 success
= lck_rw_try_lock_shared(<_rwlock
);
467 T_ASSERT_NULL(success
, "After regular exclusive grab, shared trylock should not succeed");
468 success
= lck_rw_try_lock_exclusive(<_rwlock
);
469 T_ASSERT_NULL(success
, "After regular exclusive grab, exclusive trylock should not succeed");
470 lck_rw_done(<_rwlock
);
473 * First spin lock attempts succeed, second attempts fail.
475 success
= hw_lock_try(<_hw_lock
, LCK_GRP_NULL
);
476 T_ASSERT_NOTNULL(success
, "First spin lock attempts should succeed");
477 success
= hw_lock_try(<_hw_lock
, LCK_GRP_NULL
);
478 T_ASSERT_NULL(success
, "Second attempt to spin lock should fail");
479 hw_lock_unlock(<_hw_lock
);
481 hw_lock_lock(<_hw_lock
, LCK_GRP_NULL
);
482 success
= hw_lock_try(<_hw_lock
, LCK_GRP_NULL
);
483 T_ASSERT_NULL(success
, "After taking spin lock, trylock attempt should fail");
484 hw_lock_unlock(<_hw_lock
);
487 lt_thread_lock_grabbed
= false;
488 lt_thread_lock_success
= true;
489 lt_target_done_threads
= 1;
491 lt_start_trylock_thread(lt_trylock_hw_lock_with_to
);
492 success
= hw_lock_to(<_hw_lock
, 100, LCK_GRP_NULL
);
493 T_ASSERT_NOTNULL(success
, "First spin lock with timeout should succeed");
494 if (real_ncpus
== 1) {
495 mp_enable_preemption(); /* if we re-enable preemption, the other thread can timeout and exit */
497 OSIncrementAtomic((volatile SInt32
*)<_thread_lock_grabbed
);
498 lt_wait_for_lock_test_threads();
499 T_ASSERT_NULL(lt_thread_lock_success
, "Second spin lock with timeout should fail and timeout");
500 if (real_ncpus
== 1) {
501 mp_disable_preemption(); /* don't double-enable when we unlock */
503 hw_lock_unlock(<_hw_lock
);
506 lt_thread_lock_grabbed
= false;
507 lt_thread_lock_success
= true;
508 lt_target_done_threads
= 1;
510 lt_start_trylock_thread(lt_trylock_hw_lock_with_to
);
511 hw_lock_lock(<_hw_lock
, LCK_GRP_NULL
);
512 if (real_ncpus
== 1) {
513 mp_enable_preemption(); /* if we re-enable preemption, the other thread can timeout and exit */
515 OSIncrementAtomic((volatile SInt32
*)<_thread_lock_grabbed
);
516 lt_wait_for_lock_test_threads();
517 T_ASSERT_NULL(lt_thread_lock_success
, "after taking a spin lock, lock attempt with timeout should fail");
518 if (real_ncpus
== 1) {
519 mp_disable_preemption(); /* don't double-enable when we unlock */
521 hw_lock_unlock(<_hw_lock
);
523 success
= lck_spin_try_lock(<_lck_spin_t
);
524 T_ASSERT_NOTNULL(success
, "spin trylock of previously unheld lock should succeed");
525 success
= lck_spin_try_lock(<_lck_spin_t
);
526 T_ASSERT_NULL(success
, "spin trylock attempt of previously held lock (with trylock) should fail");
527 lck_spin_unlock(<_lck_spin_t
);
530 lt_thread_lock_grabbed
= false;
531 lt_thread_lock_success
= true;
532 lt_target_done_threads
= 1;
533 lt_start_trylock_thread(lt_trylock_spin_try_lock
);
534 lck_spin_lock(<_lck_spin_t
);
535 if (real_ncpus
== 1) {
536 mp_enable_preemption(); /* if we re-enable preemption, the other thread can timeout and exit */
538 OSIncrementAtomic((volatile SInt32
*)<_thread_lock_grabbed
);
539 lt_wait_for_lock_test_threads();
540 T_ASSERT_NULL(lt_thread_lock_success
, "spin trylock attempt of previously held lock should fail");
541 if (real_ncpus
== 1) {
542 mp_disable_preemption(); /* don't double-enable when we unlock */
544 lck_spin_unlock(<_lck_spin_t
);
550 lt_thread(void *arg
, wait_result_t wres __unused
)
552 void (*func
)(void) = (void (*)(void))arg
;
555 for (i
= 0; i
< LOCK_TEST_ITERATIONS
; i
++) {
559 OSIncrementAtomic((volatile SInt32
*) <_done_threads
);
563 lt_start_lock_thread(thread_continue_t func
)
568 kr
= kernel_thread_start(lt_thread
, func
, &thread
);
569 assert(kr
== KERN_SUCCESS
);
571 thread_deallocate(thread
);
578 kern_return_t kr
= KERN_SUCCESS
;
579 lck_grp_attr_t
*lga
= lck_grp_attr_alloc_init();
580 lck_grp_t
*lg
= lck_grp_alloc_init("lock test", lga
);
582 lck_mtx_init(<_mtx
, lg
, LCK_ATTR_NULL
);
583 lck_rw_init(<_rwlock
, lg
, LCK_ATTR_NULL
);
584 lck_spin_init(<_lck_spin_t
, lg
, LCK_ATTR_NULL
);
585 hw_lock_init(<_hw_lock
);
587 T_LOG("Testing locks.");
589 /* Try locks (custom) */
592 T_LOG("Running try lock test.");
593 kr
= lt_test_trylocks();
594 T_EXPECT_NULL(kr
, "try lock test failed.");
596 /* Uncontended mutex */
597 T_LOG("Running uncontended mutex test.");
599 lt_target_done_threads
= 1;
600 lt_start_lock_thread(lt_grab_mutex
);
601 lt_wait_for_lock_test_threads();
602 T_EXPECT_EQ_UINT(lt_counter
, LOCK_TEST_ITERATIONS
* lt_target_done_threads
, NULL
);
603 T_EXPECT_EQ_UINT(lt_max_holders
, 1, NULL
);
605 /* Contended mutex:try locks*/
606 T_LOG("Running contended mutex test.");
608 lt_target_done_threads
= 3;
609 lt_start_lock_thread(lt_grab_mutex
);
610 lt_start_lock_thread(lt_grab_mutex
);
611 lt_start_lock_thread(lt_grab_mutex
);
612 lt_wait_for_lock_test_threads();
613 T_EXPECT_EQ_UINT(lt_counter
, LOCK_TEST_ITERATIONS
* lt_target_done_threads
, NULL
);
614 T_EXPECT_EQ_UINT(lt_max_holders
, 1, NULL
);
616 /* Contended mutex: try locks*/
617 T_LOG("Running contended mutex trylock test.");
619 lt_target_done_threads
= 3;
620 lt_start_lock_thread(lt_grab_mutex_with_try
);
621 lt_start_lock_thread(lt_grab_mutex_with_try
);
622 lt_start_lock_thread(lt_grab_mutex_with_try
);
623 lt_wait_for_lock_test_threads();
624 T_EXPECT_EQ_UINT(lt_counter
, LOCK_TEST_ITERATIONS
* lt_target_done_threads
, NULL
);
625 T_EXPECT_EQ_UINT(lt_max_holders
, 1, NULL
);
627 /* Uncontended exclusive rwlock */
628 T_LOG("Running uncontended exclusive rwlock test.");
630 lt_target_done_threads
= 1;
631 lt_start_lock_thread(lt_grab_rw_exclusive
);
632 lt_wait_for_lock_test_threads();
633 T_EXPECT_EQ_UINT(lt_counter
, LOCK_TEST_ITERATIONS
* lt_target_done_threads
, NULL
);
634 T_EXPECT_EQ_UINT(lt_max_holders
, 1, NULL
);
636 /* Uncontended shared rwlock */
638 /* Disabled until lt_grab_rw_shared() is fixed (rdar://30685840)
639 * T_LOG("Running uncontended shared rwlock test.");
641 * lt_target_done_threads = 1;
642 * lt_start_lock_thread(lt_grab_rw_shared);
643 * lt_wait_for_lock_test_threads();
644 * T_EXPECT_EQ_UINT(lt_counter, LOCK_TEST_ITERATIONS * lt_target_done_threads, NULL);
645 * T_EXPECT_EQ_UINT(lt_max_holders, 1, NULL);
648 /* Contended exclusive rwlock */
649 T_LOG("Running contended exclusive rwlock test.");
651 lt_target_done_threads
= 3;
652 lt_start_lock_thread(lt_grab_rw_exclusive
);
653 lt_start_lock_thread(lt_grab_rw_exclusive
);
654 lt_start_lock_thread(lt_grab_rw_exclusive
);
655 lt_wait_for_lock_test_threads();
656 T_EXPECT_EQ_UINT(lt_counter
, LOCK_TEST_ITERATIONS
* lt_target_done_threads
, NULL
);
657 T_EXPECT_EQ_UINT(lt_max_holders
, 1, NULL
);
659 /* One shared, two exclusive */
660 /* Disabled until lt_grab_rw_shared() is fixed (rdar://30685840)
661 * T_LOG("Running test with one shared and two exclusive rw lock threads.");
663 * lt_target_done_threads = 3;
664 * lt_start_lock_thread(lt_grab_rw_shared);
665 * lt_start_lock_thread(lt_grab_rw_exclusive);
666 * lt_start_lock_thread(lt_grab_rw_exclusive);
667 * lt_wait_for_lock_test_threads();
668 * T_EXPECT_EQ_UINT(lt_counter, LOCK_TEST_ITERATIONS * lt_target_done_threads, NULL);
669 * T_EXPECT_EQ_UINT(lt_max_holders, 1, NULL);
673 /* Disabled until lt_grab_rw_shared() is fixed (rdar://30685840)
674 * T_LOG("Running test with four shared holders.");
676 * lt_target_done_threads = 4;
677 * lt_start_lock_thread(lt_grab_rw_shared);
678 * lt_start_lock_thread(lt_grab_rw_shared);
679 * lt_start_lock_thread(lt_grab_rw_shared);
680 * lt_start_lock_thread(lt_grab_rw_shared);
681 * lt_wait_for_lock_test_threads();
682 * T_EXPECT_LE_UINT(lt_max_holders, 4, NULL);
685 /* Three doing upgrades and downgrades */
686 T_LOG("Running test with threads upgrading and downgrading.");
688 lt_target_done_threads
= 3;
689 lt_start_lock_thread(lt_upgrade_downgrade_rw
);
690 lt_start_lock_thread(lt_upgrade_downgrade_rw
);
691 lt_start_lock_thread(lt_upgrade_downgrade_rw
);
692 lt_wait_for_lock_test_threads();
693 T_EXPECT_EQ_UINT(lt_counter
, LOCK_TEST_ITERATIONS
* lt_target_done_threads
, NULL
);
694 T_EXPECT_LE_UINT(lt_max_holders
, 3, NULL
);
695 T_EXPECT_EQ_UINT(lt_max_upgrade_holders
, 1, NULL
);
697 /* Uncontended - exclusive trylocks */
698 T_LOG("Running test with single thread doing exclusive rwlock trylocks.");
700 lt_target_done_threads
= 1;
701 lt_start_lock_thread(lt_grab_rw_exclusive_with_try
);
702 lt_wait_for_lock_test_threads();
703 T_EXPECT_EQ_UINT(lt_counter
, LOCK_TEST_ITERATIONS
* lt_target_done_threads
, NULL
);
704 T_EXPECT_EQ_UINT(lt_max_holders
, 1, NULL
);
706 /* Uncontended - shared trylocks */
707 /* Disabled until lt_grab_rw_shared_with_try() is fixed (rdar://30685840)
708 * T_LOG("Running test with single thread doing shared rwlock trylocks.");
710 * lt_target_done_threads = 1;
711 * lt_start_lock_thread(lt_grab_rw_shared_with_try);
712 * lt_wait_for_lock_test_threads();
713 * T_EXPECT_EQ_UINT(lt_counter, LOCK_TEST_ITERATIONS * lt_target_done_threads, NULL);
714 * T_EXPECT_EQ_UINT(lt_max_holders, 1, NULL);
717 /* Three doing exclusive trylocks */
718 T_LOG("Running test with threads doing exclusive rwlock trylocks.");
720 lt_target_done_threads
= 3;
721 lt_start_lock_thread(lt_grab_rw_exclusive_with_try
);
722 lt_start_lock_thread(lt_grab_rw_exclusive_with_try
);
723 lt_start_lock_thread(lt_grab_rw_exclusive_with_try
);
724 lt_wait_for_lock_test_threads();
725 T_EXPECT_EQ_UINT(lt_counter
, LOCK_TEST_ITERATIONS
* lt_target_done_threads
, NULL
);
726 T_EXPECT_EQ_UINT(lt_max_holders
, 1, NULL
);
728 /* Three doing shared trylocks */
729 /* Disabled until lt_grab_rw_shared_with_try() is fixed (rdar://30685840)
730 * T_LOG("Running test with threads doing shared rwlock trylocks.");
732 * lt_target_done_threads = 3;
733 * lt_start_lock_thread(lt_grab_rw_shared_with_try);
734 * lt_start_lock_thread(lt_grab_rw_shared_with_try);
735 * lt_start_lock_thread(lt_grab_rw_shared_with_try);
736 * lt_wait_for_lock_test_threads();
737 * T_EXPECT_LE_UINT(lt_counter, LOCK_TEST_ITERATIONS * lt_target_done_threads, NULL);
738 * T_EXPECT_LE_UINT(lt_max_holders, 3, NULL);
741 /* Three doing various trylocks */
742 /* Disabled until lt_grab_rw_shared_with_try() is fixed (rdar://30685840)
743 * T_LOG("Running test with threads doing mixed rwlock trylocks.");
745 * lt_target_done_threads = 4;
746 * lt_start_lock_thread(lt_grab_rw_shared_with_try);
747 * lt_start_lock_thread(lt_grab_rw_shared_with_try);
748 * lt_start_lock_thread(lt_grab_rw_exclusive_with_try);
749 * lt_start_lock_thread(lt_grab_rw_exclusive_with_try);
750 * lt_wait_for_lock_test_threads();
751 * T_EXPECT_LE_UINT(lt_counter, LOCK_TEST_ITERATIONS * lt_target_done_threads, NULL);
752 * T_EXPECT_LE_UINT(lt_max_holders, 2, NULL);
756 T_LOG("Running test with hw_lock_lock()");
758 lt_target_done_threads
= 3;
759 lt_start_lock_thread(lt_grab_hw_lock
);
760 lt_start_lock_thread(lt_grab_hw_lock
);
761 lt_start_lock_thread(lt_grab_hw_lock
);
762 lt_wait_for_lock_test_threads();
763 T_EXPECT_EQ_UINT(lt_counter
, LOCK_TEST_ITERATIONS
* lt_target_done_threads
, NULL
);
766 /* HW locks: trylocks */
767 T_LOG("Running test with hw_lock_try()");
769 lt_target_done_threads
= 3;
770 lt_start_lock_thread(lt_grab_hw_lock_with_try
);
771 lt_start_lock_thread(lt_grab_hw_lock_with_try
);
772 lt_start_lock_thread(lt_grab_hw_lock_with_try
);
773 lt_wait_for_lock_test_threads();
774 T_EXPECT_EQ_UINT(lt_counter
, LOCK_TEST_ITERATIONS
* lt_target_done_threads
, NULL
);
776 /* HW locks: with timeout */
777 T_LOG("Running test with hw_lock_to()");
779 lt_target_done_threads
= 3;
780 lt_start_lock_thread(lt_grab_hw_lock_with_to
);
781 lt_start_lock_thread(lt_grab_hw_lock_with_to
);
782 lt_start_lock_thread(lt_grab_hw_lock_with_to
);
783 lt_wait_for_lock_test_threads();
784 T_EXPECT_EQ_UINT(lt_counter
, LOCK_TEST_ITERATIONS
* lt_target_done_threads
, NULL
);
787 T_LOG("Running test with lck_spin_lock()");
789 lt_target_done_threads
= 3;
790 lt_start_lock_thread(lt_grab_spin_lock
);
791 lt_start_lock_thread(lt_grab_spin_lock
);
792 lt_start_lock_thread(lt_grab_spin_lock
);
793 lt_wait_for_lock_test_threads();
794 T_EXPECT_EQ_UINT(lt_counter
, LOCK_TEST_ITERATIONS
* lt_target_done_threads
, NULL
);
796 /* Spin locks: trylocks */
797 T_LOG("Running test with lck_spin_try_lock()");
799 lt_target_done_threads
= 3;
800 lt_start_lock_thread(lt_grab_spin_lock_with_try
);
801 lt_start_lock_thread(lt_grab_spin_lock_with_try
);
802 lt_start_lock_thread(lt_grab_spin_lock_with_try
);
803 lt_wait_for_lock_test_threads();
804 T_EXPECT_EQ_UINT(lt_counter
, LOCK_TEST_ITERATIONS
* lt_target_done_threads
, NULL
);
809 #define MT_MAX_ARGS 8
810 #define MT_INITIAL_VALUE 0xfeedbeef
811 #define MT_W_VAL (0x00000000feedbeefULL) /* Drop in zeros */
812 #define MT_S_VAL (0xfffffffffeedbeefULL) /* High bit is 1, so sign-extends as negative */
813 #define MT_L_VAL (((uint64_t)MT_INITIAL_VALUE) | (((uint64_t)MT_INITIAL_VALUE) << 32)) /* Two back-to-back */
815 typedef void (*sy_munge_t
)(void*);
817 #define MT_FUNC(x) #x, x
821 uint32_t mt_in_words
;
823 uint64_t mt_expected
[MT_MAX_ARGS
];
825 {MT_FUNC(munge_w
), 1, 1, {MT_W_VAL
}},
826 {MT_FUNC(munge_ww
), 2, 2, {MT_W_VAL
, MT_W_VAL
}},
827 {MT_FUNC(munge_www
), 3, 3, {MT_W_VAL
, MT_W_VAL
, MT_W_VAL
}},
828 {MT_FUNC(munge_wwww
), 4, 4, {MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
}},
829 {MT_FUNC(munge_wwwww
), 5, 5, {MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
}},
830 {MT_FUNC(munge_wwwwww
), 6, 6, {MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
}},
831 {MT_FUNC(munge_wwwwwww
), 7, 7, {MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
}},
832 {MT_FUNC(munge_wwwwwwww
), 8, 8, {MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
}},
833 {MT_FUNC(munge_wl
), 3, 2, {MT_W_VAL
, MT_L_VAL
}},
834 {MT_FUNC(munge_wwl
), 4, 3, {MT_W_VAL
, MT_W_VAL
, MT_L_VAL
}},
835 {MT_FUNC(munge_wwlll
), 8, 5, {MT_W_VAL
, MT_W_VAL
, MT_L_VAL
, MT_L_VAL
, MT_L_VAL
}},
836 {MT_FUNC(munge_wlw
), 4, 3, {MT_W_VAL
, MT_L_VAL
, MT_W_VAL
}},
837 {MT_FUNC(munge_wlwwwll
), 10, 7, {MT_W_VAL
, MT_L_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_L_VAL
, MT_L_VAL
}},
838 {MT_FUNC(munge_wlwwwllw
), 11, 8, {MT_W_VAL
, MT_L_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_L_VAL
, MT_L_VAL
, MT_W_VAL
}},
839 {MT_FUNC(munge_wlwwlwlw
), 11, 8, {MT_W_VAL
, MT_L_VAL
, MT_W_VAL
, MT_W_VAL
, MT_L_VAL
, MT_W_VAL
, MT_L_VAL
, MT_W_VAL
}},
840 {MT_FUNC(munge_wll
), 5, 3, {MT_W_VAL
, MT_L_VAL
, MT_L_VAL
}},
841 {MT_FUNC(munge_wlll
), 7, 4, {MT_W_VAL
, MT_L_VAL
, MT_L_VAL
, MT_L_VAL
}},
842 {MT_FUNC(munge_wllwwll
), 11, 7, {MT_W_VAL
, MT_L_VAL
, MT_L_VAL
, MT_W_VAL
, MT_W_VAL
, MT_L_VAL
, MT_L_VAL
}},
843 {MT_FUNC(munge_wwwlw
), 6, 5, {MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_L_VAL
, MT_W_VAL
}},
844 {MT_FUNC(munge_wwwlww
), 7, 6, {MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_L_VAL
, MT_W_VAL
, MT_W_VAL
}},
845 {MT_FUNC(munge_wwwl
), 5, 4, {MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_L_VAL
}},
846 {MT_FUNC(munge_wwwwlw
), 7, 6, {MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_L_VAL
, MT_W_VAL
}},
847 {MT_FUNC(munge_wwwwl
), 6, 5, {MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_L_VAL
}},
848 {MT_FUNC(munge_wwwwwl
), 7, 6, {MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_L_VAL
}},
849 {MT_FUNC(munge_wwwwwlww
), 9, 8, {MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_L_VAL
, MT_W_VAL
, MT_W_VAL
}},
850 {MT_FUNC(munge_wwwwwllw
), 10, 8, {MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_L_VAL
, MT_L_VAL
, MT_W_VAL
}},
851 {MT_FUNC(munge_wwwwwlll
), 11, 8, {MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_L_VAL
, MT_L_VAL
, MT_L_VAL
}},
852 {MT_FUNC(munge_wwwwwwl
), 8, 7, {MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_L_VAL
}},
853 {MT_FUNC(munge_wwwwwwlw
), 9, 8, {MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_L_VAL
, MT_W_VAL
}},
854 {MT_FUNC(munge_wwwwwwll
), 10, 8, {MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_L_VAL
, MT_L_VAL
}},
855 {MT_FUNC(munge_wsw
), 3, 3, {MT_W_VAL
, MT_S_VAL
, MT_W_VAL
}},
856 {MT_FUNC(munge_wws
), 3, 3, {MT_W_VAL
, MT_W_VAL
, MT_S_VAL
}},
857 {MT_FUNC(munge_wwwsw
), 5, 5, {MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_S_VAL
, MT_W_VAL
}},
858 {MT_FUNC(munge_llllll
), 12, 6, {MT_L_VAL
, MT_L_VAL
, MT_L_VAL
, MT_L_VAL
, MT_L_VAL
, MT_L_VAL
}},
859 {MT_FUNC(munge_l
), 2, 1, {MT_L_VAL
}},
860 {MT_FUNC(munge_lw
), 3, 2, {MT_L_VAL
, MT_W_VAL
}},
861 {MT_FUNC(munge_lwww
), 5, 4, {MT_L_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
}},
862 {MT_FUNC(munge_lwwwwwww
), 9, 8, {MT_L_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
}},
863 {MT_FUNC(munge_wlwwwl
), 8, 6, {MT_W_VAL
, MT_L_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_L_VAL
}},
864 {MT_FUNC(munge_wwlwwwl
), 9, 7, {MT_W_VAL
, MT_W_VAL
, MT_L_VAL
, MT_W_VAL
, MT_W_VAL
, MT_W_VAL
, MT_L_VAL
}}
867 #define MT_TEST_COUNT (sizeof(munger_tests) / sizeof(struct munger_test))
870 mt_reset(uint32_t in_words
, size_t total_size
, uint32_t *data
)
874 for (i
= 0; i
< in_words
; i
++) {
875 data
[i
] = MT_INITIAL_VALUE
;
878 if (in_words
* sizeof(uint32_t) < total_size
) {
879 bzero(&data
[in_words
], total_size
- in_words
* sizeof(uint32_t));
886 uint64_t data
[MT_MAX_ARGS
];
889 for (i
= 0; i
< MT_TEST_COUNT
; i
++) {
890 struct munger_test
*test
= &munger_tests
[i
];
893 T_LOG("Testing %s", test
->mt_name
);
895 mt_reset(test
->mt_in_words
, sizeof(data
), (uint32_t*)data
);
898 for (j
= 0; j
< test
->mt_nout
; j
++) {
899 if (data
[j
] != test
->mt_expected
[j
]) {
900 T_FAIL("Index %d: expected %llx, got %llx.", j
, test
->mt_expected
[j
], data
[j
]);
905 T_PASS(test
->mt_name
);
910 /* Exception Callback Test */
911 static ex_cb_action_t
913 ex_cb_class_t cb_class
,
915 const ex_cb_state_t
*state
918 ex_cb_state_t
*context
= (ex_cb_state_t
*)refcon
;
920 if ((NULL
== refcon
) || (NULL
== state
)) {
921 return EXCB_ACTION_TEST_FAIL
;
924 context
->far
= state
->far
;
927 case EXCB_CLASS_TEST1
:
928 return EXCB_ACTION_RERUN
;
929 case EXCB_CLASS_TEST2
:
930 return EXCB_ACTION_NONE
;
932 return EXCB_ACTION_TEST_FAIL
;
940 const vm_offset_t far1
= 0xdead0001;
941 const vm_offset_t far2
= 0xdead0002;
943 ex_cb_state_t test_context_1
= {0xdeadbeef};
944 ex_cb_state_t test_context_2
= {0xdeadbeef};
945 ex_cb_action_t action
;
947 T_LOG("Testing Exception Callback.");
949 T_LOG("Running registration test.");
951 kr
= ex_cb_register(EXCB_CLASS_TEST1
, &excb_test_action
, &test_context_1
);
952 T_ASSERT(KERN_SUCCESS
== kr
, "First registration of TEST1 exception callback");
953 kr
= ex_cb_register(EXCB_CLASS_TEST2
, &excb_test_action
, &test_context_2
);
954 T_ASSERT(KERN_SUCCESS
== kr
, "First registration of TEST2 exception callback");
956 kr
= ex_cb_register(EXCB_CLASS_TEST2
, &excb_test_action
, &test_context_2
);
957 T_ASSERT(KERN_SUCCESS
!= kr
, "Second registration of TEST2 exception callback");
958 kr
= ex_cb_register(EXCB_CLASS_TEST1
, &excb_test_action
, &test_context_1
);
959 T_ASSERT(KERN_SUCCESS
!= kr
, "Second registration of TEST1 exception callback");
961 T_LOG("Running invocation test.");
963 action
= ex_cb_invoke(EXCB_CLASS_TEST1
, far1
);
964 T_ASSERT(EXCB_ACTION_RERUN
== action
, NULL
);
965 T_ASSERT(far1
== test_context_1
.far
, NULL
);
967 action
= ex_cb_invoke(EXCB_CLASS_TEST2
, far2
);
968 T_ASSERT(EXCB_ACTION_NONE
== action
, NULL
);
969 T_ASSERT(far2
== test_context_2
.far
, NULL
);
971 action
= ex_cb_invoke(EXCB_CLASS_TEST3
, 0);
972 T_ASSERT(EXCB_ACTION_NONE
== action
, NULL
);
977 #if defined(HAS_APPLE_PAC)
981 * arm64_ropjop_test - basic xnu ROP/JOP test plan
983 * - assert ROP/JOP configured and running status match
984 * - assert all AppleMode ROP/JOP features enabled
985 * - ensure ROP/JOP keys are set and diversified
986 * - sign a KVA (the address of this function),assert it was signed (changed)
987 * - authenticate the newly signed KVA
988 * - assert the authed KVA is the original KVA
989 * - corrupt a signed ptr, auth it, ensure auth failed
990 * - assert the failed authIB of corrupted pointer is tagged
997 T_LOG("Testing ROP/JOP");
999 /* how is ROP/JOP configured */
1000 boolean_t config_rop_enabled
= TRUE
;
1001 boolean_t config_jop_enabled
= !(BootArgs
->bootFlags
& kBootFlagsDisableJOP
);
1004 /* assert all AppleMode ROP/JOP features enabled */
1005 uint64_t apctl
= __builtin_arm_rsr64(ARM64_REG_APCTL_EL1
);
1006 #if __APSTS_SUPPORTED__
1007 uint64_t apsts
= __builtin_arm_rsr64(ARM64_REG_APSTS_EL1
);
1008 T_ASSERT(apsts
& APSTS_EL1_MKEYVld
, NULL
);
1010 T_ASSERT(apctl
& APCTL_EL1_MKEYVld
, NULL
);
1011 #endif /* __APSTS_SUPPORTED__ */
1012 T_ASSERT(apctl
& APCTL_EL1_AppleMode
, NULL
);
1013 T_ASSERT(apctl
& APCTL_EL1_KernKeyEn
, NULL
);
1015 /* ROP/JOP keys enabled current status */
1016 bool status_jop_enabled
, status_rop_enabled
;
1017 #if __APSTS_SUPPORTED__ /* H13+ */
1018 // TODO: update unit test to understand ROP/JOP enabled config for H13+
1019 status_jop_enabled
= status_rop_enabled
= apctl
& APCTL_EL1_EnAPKey1
;
1020 #elif __APCFG_SUPPORTED__ /* H12 */
1021 uint64_t apcfg_el1
= __builtin_arm_rsr64(APCFG_EL1
);
1022 status_jop_enabled
= status_rop_enabled
= apcfg_el1
& APCFG_EL1_ELXENKEY
;
1023 #else /* !__APCFG_SUPPORTED__ H11 */
1024 uint64_t sctlr_el1
= __builtin_arm_rsr64("SCTLR_EL1");
1025 status_jop_enabled
= sctlr_el1
& SCTLR_PACIA_ENABLED
;
1026 status_rop_enabled
= sctlr_el1
& SCTLR_PACIB_ENABLED
;
1027 #endif /* __APSTS_SUPPORTED__ */
1029 /* assert configured and running status match */
1030 T_ASSERT(config_rop_enabled
== status_rop_enabled
, NULL
);
1031 T_ASSERT(config_jop_enabled
== status_jop_enabled
, NULL
);
1034 if (config_jop_enabled
) {
1036 uint64_t apiakey_hi
= __builtin_arm_rsr64(ARM64_REG_APIAKEYHI_EL1
);
1037 uint64_t apiakey_lo
= __builtin_arm_rsr64(ARM64_REG_APIAKEYLO_EL1
);
1039 /* ensure JOP key is set and diversified */
1040 T_EXPECT(apiakey_hi
!= KERNEL_ROP_ID
&& apiakey_lo
!= KERNEL_ROP_ID
, NULL
);
1041 T_EXPECT(apiakey_hi
!= 0 && apiakey_lo
!= 0, NULL
);
1044 if (config_rop_enabled
) {
1046 uint64_t apibkey_hi
= __builtin_arm_rsr64(ARM64_REG_APIBKEYHI_EL1
);
1047 uint64_t apibkey_lo
= __builtin_arm_rsr64(ARM64_REG_APIBKEYLO_EL1
);
1049 /* ensure ROP key is set and diversified */
1050 T_EXPECT(apibkey_hi
!= KERNEL_ROP_ID
&& apibkey_lo
!= KERNEL_ROP_ID
, NULL
);
1051 T_EXPECT(apibkey_hi
!= 0 && apibkey_lo
!= 0, NULL
);
1053 /* sign a KVA (the address of this function) */
1054 uint64_t kva_signed
= (uint64_t) ptrauth_sign_unauthenticated((void *)&config_rop_enabled
, ptrauth_key_asib
, 0);
1056 /* assert it was signed (changed) */
1057 T_EXPECT(kva_signed
!= (uint64_t)&config_rop_enabled
, NULL
);
1059 /* authenticate the newly signed KVA */
1060 uint64_t kva_authed
= (uint64_t) ml_auth_ptr_unchecked((void *)kva_signed
, ptrauth_key_asib
, 0);
1062 /* assert the authed KVA is the original KVA */
1063 T_EXPECT(kva_authed
== (uint64_t)&config_rop_enabled
, NULL
);
1065 /* corrupt a signed ptr, auth it, ensure auth failed */
1066 uint64_t kva_corrupted
= kva_signed
^ 1;
1068 /* authenticate the corrupted pointer */
1069 kva_authed
= (uint64_t) ml_auth_ptr_unchecked((void *)kva_corrupted
, ptrauth_key_asib
, 0);
1071 /* when AuthIB fails, bits 63:62 will be set to 2'b10 */
1072 uint64_t auth_fail_mask
= 3ULL << 61;
1073 uint64_t authib_fail
= 2ULL << 61;
1075 /* assert the failed authIB of corrupted pointer is tagged */
1076 T_EXPECT((kva_authed
& auth_fail_mask
) == authib_fail
, NULL
);
1079 return KERN_SUCCESS
;
1081 #endif /* defined(HAS_APPLE_PAC) */
1083 #if __ARM_PAN_AVAILABLE__
1085 struct pan_test_thread_args
{
1090 arm64_pan_test_thread(void *arg
, wait_result_t __unused wres
)
1092 T_ASSERT(__builtin_arm_rsr("pan") != 0, NULL
);
1094 struct pan_test_thread_args
*args
= arg
;
1096 for (processor_t p
= processor_list
; p
!= NULL
; p
= p
->processor_list
) {
1098 thread_block(THREAD_CONTINUE_NULL
);
1099 kprintf("Running PAN test on cpu %d\n", p
->cpu_id
);
1103 /* unbind thread from specific cpu */
1104 thread_bind(PROCESSOR_NULL
);
1105 thread_block(THREAD_CONTINUE_NULL
);
1107 while (!args
->join
) {
1111 thread_wakeup(args
);
1115 arm64_late_pan_test()
1120 struct pan_test_thread_args args
;
1123 kr
= kernel_thread_start(arm64_pan_test_thread
, &args
, &thread
);
1124 assert(kr
== KERN_SUCCESS
);
1126 thread_deallocate(thread
);
1128 assert_wait(&args
, THREAD_UNINT
);
1130 thread_block(THREAD_CONTINUE_NULL
);
1131 return KERN_SUCCESS
;
1137 vm_offset_t priv_addr
= _COMM_PAGE_SIGNATURE
;
1139 T_LOG("Testing PAN.");
1142 T_ASSERT((__builtin_arm_rsr("SCTLR_EL1") & SCTLR_PAN_UNCHANGED
) == 0, "SCTLR_EL1.SPAN must be cleared");
1144 T_ASSERT(__builtin_arm_rsr("pan") != 0, NULL
);
1146 pan_exception_level
= 0;
1147 pan_fault_value
= 0xDE;
1148 // convert priv_addr to one that is accessible from user mode
1149 pan_test_addr
= priv_addr
+ _COMM_HIGH_PAGE64_BASE_ADDRESS
-
1150 _COMM_PAGE_START_ADDRESS
;
1152 // Below should trigger a PAN exception as pan_test_addr is accessible
1154 // The exception handler, upon recognizing the fault address is pan_test_addr,
1155 // will disable PAN and rerun this instruction successfully
1156 T_ASSERT(*(char *)pan_test_addr
== *(char *)priv_addr
, NULL
);
1158 T_ASSERT(pan_exception_level
== 2, NULL
);
1160 T_ASSERT(__builtin_arm_rsr("pan") == 0, NULL
);
1162 T_ASSERT(pan_fault_value
== *(char *)priv_addr
, NULL
);
1164 pan_exception_level
= 0;
1165 pan_fault_value
= 0xAD;
1166 pan_ro_addr
= (vm_offset_t
) &pan_ro_value
;
1168 // Force a permission fault while PAN is disabled to make sure PAN is
1169 // re-enabled during the exception handler.
1170 *((volatile uint64_t*)pan_ro_addr
) = 0xFEEDFACECAFECAFE;
1172 T_ASSERT(pan_exception_level
== 2, NULL
);
1174 T_ASSERT(__builtin_arm_rsr("pan") == 0, NULL
);
1176 T_ASSERT(pan_fault_value
== *(char *)priv_addr
, NULL
);
1181 __builtin_arm_wsr("pan", 1);
1183 return KERN_SUCCESS
;
1185 #endif /* __ARM_PAN_AVAILABLE__ */
1191 return lt_test_locks();
1202 #if HAS_TWO_STAGE_SPR_LOCK
1205 #define STR(x) STR1(x)
1207 volatile vm_offset_t spr_lock_test_addr
;
1208 volatile uint32_t spr_lock_exception_esr
;
1211 arm64_spr_lock_test()
1215 for (p
= processor_list
; p
!= NULL
; p
= p
->processor_list
) {
1217 thread_block(THREAD_CONTINUE_NULL
);
1218 T_LOG("Running SPR lock test on cpu %d\n", p
->cpu_id
);
1220 uint64_t orig_value
= __builtin_arm_rsr64(STR(ARM64_REG_HID8
));
1221 spr_lock_test_addr
= (vm_offset_t
)VM_KERNEL_STRIP_PTR(arm64_msr_lock_test
);
1222 spr_lock_exception_esr
= 0;
1223 arm64_msr_lock_test(~orig_value
);
1224 T_EXPECT(spr_lock_exception_esr
!= 0, "MSR write generated synchronous abort");
1226 uint64_t new_value
= __builtin_arm_rsr64(STR(ARM64_REG_HID8
));
1227 T_EXPECT(orig_value
== new_value
, "MSR write did not succeed");
1229 spr_lock_test_addr
= 0;
1232 /* unbind thread from specific cpu */
1233 thread_bind(PROCESSOR_NULL
);
1234 thread_block(THREAD_CONTINUE_NULL
);
1236 T_PASS("Done running SPR lock tests");
1238 return KERN_SUCCESS
;
1241 #endif /* HAS_TWO_STAGE_SPR_LOCK */