]>
git.saurik.com Git - apple/xnu.git/blob - tools/tests/TLBcoherency/TLBcoherency.c
2 * Copyright (c) 2011 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@
29 /* A pool of threads which attempt to verify multiprocessor TLB coherency.
30 * Creates -t threads, by default 4
31 * Creates -s separate mmap(MAP_ANON) R/W mappings, sized at 1 page each but
32 * alterable via -z <npages>
33 * Initially read-faults each mapping in, verifying first-word zerofill--
34 * The kernel typically uses the physical aperture to perform the zerofill
35 * Writes map_address (page_aligned) | low 12 bits of the PID at the first word
36 * This can help verify ASID related inconsistencies
37 * Records a timestamp in a Structure associated with each mapping
38 * With a custom kernel, it has the option of creating a remapping of the page in
39 * the kernel's address space to exercise shared kernel mapping coherency.
40 * Each thread subsequently loops around on the set of mappings. One thread is designated
41 * the observer thread. The thread acquires a lock on the arena element,
42 * verifies that the mapping has the expected pattern (Address | PID), if the
43 * element is in the MAPPED state. Can optionally tell the kernel to check its
44 * alias as well. If it notices a mismatch, it has the option to issue a syscall
45 * to stop kernel tracing. If the -f option is supplied, the test is terminated.
46 * If the page has lingered beyond -l microseconds, non-observer threads will
47 * unmap the page, optionally calling into the kernel to unmap its alias, and
48 * repopulate the element.
49 * After this sequence, the thread will optionally usleep for -p microseconds,
50 * to allow for idle power management to engage if possible (errata might exist
51 * in those areas), or context switches to occur.
52 * Created Derek Kumar, 2011.
61 #include <mach/mach_time.h>
62 #include <libkern/OSAtomic.h>
63 #include <sys/syscall.h>
64 #include <sys/types.h>
65 #include <sys/sysctl.h>
76 #define ARENASIZE (1024)
78 #define PAGE_LINGER_TIME (2000000)
79 #define MAX_THREADS (512)
81 #define CONSISTENCY(...) fprintf(stderr, __VA_ARGS__ );
83 unsigned arenasize
= ARENASIZE
, mapping_size
;
84 uint64_t page_linger_time
= PAGE_LINGER_TIME
;
85 enum arenastates
{MTOUCHED
= 1, UNMAPPED
= 2, MAPPED
= 4, WP
=8};
86 enum syscaction
{MDOMAP
= 1, MDOUNMAP
= 2, MDOCHECK
= 4};
87 enum ttypes
{OBSERVER
= 1, LOOPER
= 2};
89 bool all_stop
= false;
90 bool stop_on_failure
= false;
91 bool reuse_addrs
= true;
92 bool dosyscall
= false;
97 pthread_t threads
[MAX_THREADS
];
98 uint32_t roles
[MAX_THREADS
];
100 void usage(char **a
) {
104 void set_enable(int val
)
110 mib
[1] = KERN_KDEBUG
;
111 mib
[2] = KERN_KDENABLE
;
116 if (sysctl(mib
, 4, NULL
, &needed
, NULL
, 0) < 0) {
117 printf("trace facility failure, KERN_KDENABLE\n");
121 void initialize_arena_element(int i
) {
123 void *hint
= reuse_addrs
? (void *)0x1000 : NULL
;
124 parray
[i
].taddr
= (uintptr_t)mmap(hint
, mapping_size
, PROT_READ
| PROT_WRITE
, MAP_ANON
| MAP_SHARED
, -1, 0);
126 if (parray
[i
].taddr
== (uintptr_t)MAP_FAILED
) {
131 #if !defined(__LP64__)
132 uint32_t pattern
= parray
[i
].taddr
;
133 pattern
|= cpid
& 0xFFF;
134 // memset_pattern4((void *)parray[i].taddr, &pattern, PAGE_SIZE); //
135 // uncomment to fill the whole page, but a sufficiently unique first word
136 // gets the job done without slowing down the test
139 uint64_t pattern
= parray
[i
].taddr
;
140 pattern
|= (cpid
& 0xFFF);
141 // memset_pattern8(parray[i].taddr, &pattern, PAGE_SIZE);
144 uint64_t val
= (*(uintptr_t *)parray
[i
].taddr
);
147 CONSISTENCY("Mismatch, actual: 0x%llx, expected: 0x%llx\n", (unsigned long long)val
, 0ULL);
148 if (stop_on_failure
) {
153 for (int k
= 0; k
< (mapping_size
>> PAGE_SHIFT
); k
++) {
154 *(uintptr_t *)(parray
[i
].taddr
+ k
* PAGE_SIZE
) = pattern
;
157 parray
[i
].tctime
= mach_absolute_time();
158 parray
[i
].tstate
= MTOUCHED
;
161 sysret
= syscall(MYSYS
, MDOMAP
, parray
[i
].taddr
, pattern
, i
, mapping_size
);
165 void initialize_arena(void) {
166 for (int i
= 0; i
< arenasize
; i
++) {
167 initialize_arena_element(i
);
171 void *tlbexerciser(void *targs
) {
172 uint32_t role
= *(uint32_t *)targs
;
174 printf("Starting thread %p, role: %u\n", pthread_self(), role
);
177 for (int i
= 0; i
< arenasize
; i
++) {
182 if (OSSpinLockTry(&parray
[i
].tlock
) == false)
185 OSSpinLockLock(&parray
[i
].tlock
);
188 if (parray
[i
].tstate
!= UNMAPPED
) {
190 ad
= parray
[i
].taddr
| (cpid
& 0xFFF);
191 uintptr_t val
= *(uintptr_t *)parray
[i
].taddr
;
196 syscall(180, 0x71BC0000, (ad
>> 32), (ad
& ~0), 0, 0, 0);
197 CONSISTENCY("Mismatch, actual: 0x%llx, expected: 0x%llx\n", (unsigned long long)val
, (unsigned long long)ad
);
198 if (stop_on_failure
) {
205 sysret
= syscall(MYSYS
, MDOCHECK
, parray
[i
].taddr
, ad
, i
, 0);
208 if ((role
!= OBSERVER
) && ((mach_absolute_time() - parray
[i
].tctime
) > page_linger_time
)) {
209 parray
[i
].tstate
= UNMAPPED
;
210 if (munmap((void *)parray
[i
].taddr
, mapping_size
) != 0) {
215 sysret
= syscall(MYSYS
, MDOUNMAP
, parray
[i
].taddr
, ad
, i
, mapping_size
);
219 if (role
!= OBSERVER
) {
220 initialize_arena_element(i
);
224 parray
[i
].tlock
= 0; //unlock
234 int main(int argc
, char **argv
) {
237 unsigned nthreads
= NTHREADS
;
239 mapping_size
= PAGE_SIZE
;
241 while ((arg
= getopt(argc
, argv
, "l:t:h:s:p:z:fry")) != -1) {
244 page_linger_time
= strtoull(optarg
, NULL
, 0);
247 nthreads
= atoi(optarg
);
250 arenasize
= atoi(optarg
); // we typically want this to
251 // be sized < 2nd level TLB
254 stop_on_failure
= true;
260 sleepus
= atoi(optarg
);
266 mapping_size
= atoi(optarg
) * PAGE_SIZE
;
277 printf("page_linger_time: 0x%llx, nthreads: %u, arenasize: %u sleepus: %d reuse_addrs: %u, stop_on_failure: %u, dosyscall: %u, mappingsize: 0x%x\n", page_linger_time
, nthreads
, arenasize
, sleepus
, reuse_addrs
, (unsigned) stop_on_failure
, dosyscall
, mapping_size
);
279 parray
= calloc(arenasize
, sizeof(cpage
));
284 for (int dex
= 0; dex
< nthreads
; dex
++) {
287 roles
[dex
] = OBSERVER
;
288 int result
= pthread_create(&threads
[dex
], NULL
, tlbexerciser
, &roles
[dex
]);
290 printf("pthread_create: %d starting worker thread; aborting.\n", result
);
295 for(int dex
= 0; dex
< nthreads
; dex
++) {
297 int result
= pthread_join(threads
[dex
], &rtn
);
300 printf("pthread_join(): %d, aborting\n", result
);
305 printf("***Aborting on worker error\n");