1 // SPDX-License-Identifier: GPL-2.0+
2 
3 /*
4  * Copyright 2018 IBM Corporation.
5  */
6 
7 #define __SANE_USERSPACE_TYPES__
8 
9 #include <sys/types.h>
10 #include <stdint.h>
11 #include <malloc.h>
12 #include <unistd.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <stdio.h>
16 #include "utils.h"
17 #include "flush_utils.h"
18 
19 
rfi_flush_test(void)20 int rfi_flush_test(void)
21 {
22 	char *p;
23 	int repetitions = 10;
24 	int fd, passes = 0, iter, rc = 0;
25 	struct perf_event_read v;
26 	__u64 l1d_misses_total = 0;
27 	unsigned long iterations = 100000, zero_size = 24 * 1024;
28 	unsigned long l1d_misses_expected;
29 	int rfi_flush_orig, rfi_flush;
30 	int have_entry_flush, entry_flush_orig;
31 
32 	SKIP_IF(geteuid() != 0);
33 
34 	// The PMU event we use only works on Power7 or later
35 	SKIP_IF(!have_hwcap(PPC_FEATURE_ARCH_2_06));
36 
37 	if (read_debugfs_file("powerpc/rfi_flush", &rfi_flush_orig) < 0) {
38 		perror("Unable to read powerpc/rfi_flush debugfs file");
39 		SKIP_IF(1);
40 	}
41 
42 	if (read_debugfs_file("powerpc/entry_flush", &entry_flush_orig) < 0) {
43 		have_entry_flush = 0;
44 	} else {
45 		have_entry_flush = 1;
46 
47 		if (entry_flush_orig != 0) {
48 			if (write_debugfs_file("powerpc/entry_flush", 0) < 0) {
49 				perror("error writing to powerpc/entry_flush debugfs file");
50 				return 1;
51 			}
52 		}
53 	}
54 
55 	rfi_flush = rfi_flush_orig;
56 
57 	fd = perf_event_open_counter(PERF_TYPE_HW_CACHE, PERF_L1D_READ_MISS_CONFIG, -1);
58 	FAIL_IF(fd < 0);
59 
60 	p = (char *)memalign(zero_size, CACHELINE_SIZE);
61 
62 	FAIL_IF(perf_event_enable(fd));
63 
64 	// disable L1 prefetching
65 	set_dscr(1);
66 
67 	iter = repetitions;
68 
69 	/*
70 	 * We expect to see l1d miss for each cacheline access when rfi_flush
71 	 * is set. Allow a small variation on this.
72 	 */
73 	l1d_misses_expected = iterations * (zero_size / CACHELINE_SIZE - 2);
74 
75 again:
76 	FAIL_IF(perf_event_reset(fd));
77 
78 	syscall_loop(p, iterations, zero_size);
79 
80 	FAIL_IF(read(fd, &v, sizeof(v)) != sizeof(v));
81 
82 	if (rfi_flush && v.l1d_misses >= l1d_misses_expected)
83 		passes++;
84 	else if (!rfi_flush && v.l1d_misses < (l1d_misses_expected / 2))
85 		passes++;
86 
87 	l1d_misses_total += v.l1d_misses;
88 
89 	while (--iter)
90 		goto again;
91 
92 	if (passes < repetitions) {
93 		printf("FAIL (L1D misses with rfi_flush=%d: %llu %c %lu) [%d/%d failures]\n",
94 		       rfi_flush, l1d_misses_total, rfi_flush ? '<' : '>',
95 		       rfi_flush ? repetitions * l1d_misses_expected :
96 		       repetitions * l1d_misses_expected / 2,
97 		       repetitions - passes, repetitions);
98 		rc = 1;
99 	} else
100 		printf("PASS (L1D misses with rfi_flush=%d: %llu %c %lu) [%d/%d pass]\n",
101 		       rfi_flush, l1d_misses_total, rfi_flush ? '>' : '<',
102 		       rfi_flush ? repetitions * l1d_misses_expected :
103 		       repetitions * l1d_misses_expected / 2,
104 		       passes, repetitions);
105 
106 	if (rfi_flush == rfi_flush_orig) {
107 		rfi_flush = !rfi_flush_orig;
108 		if (write_debugfs_file("powerpc/rfi_flush", rfi_flush) < 0) {
109 			perror("error writing to powerpc/rfi_flush debugfs file");
110 			return 1;
111 		}
112 		iter = repetitions;
113 		l1d_misses_total = 0;
114 		passes = 0;
115 		goto again;
116 	}
117 
118 	perf_event_disable(fd);
119 	close(fd);
120 
121 	set_dscr(0);
122 
123 	if (write_debugfs_file("powerpc/rfi_flush", rfi_flush_orig) < 0) {
124 		perror("unable to restore original value of powerpc/rfi_flush debugfs file");
125 		return 1;
126 	}
127 
128 	if (have_entry_flush) {
129 		if (write_debugfs_file("powerpc/entry_flush", entry_flush_orig) < 0) {
130 			perror("unable to restore original value of powerpc/entry_flush "
131 			       "debugfs file");
132 			return 1;
133 		}
134 	}
135 
136 	return rc;
137 }
138 
main(int argc,char * argv[])139 int main(int argc, char *argv[])
140 {
141 	return test_harness(rfi_flush_test, "rfi_flush_test");
142 }
143