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 
18 #define CACHELINE_SIZE 128
19 
20 struct perf_event_read {
21 	__u64 nr;
22 	__u64 l1d_misses;
23 };
24 
25 static inline __u64 load(void *addr)
26 {
27 	__u64 tmp;
28 
29 	asm volatile("ld %0,0(%1)" : "=r"(tmp) : "b"(addr));
30 
31 	return tmp;
32 }
33 
34 static void syscall_loop(char *p, unsigned long iterations,
35 			 unsigned long zero_size)
36 {
37 	for (unsigned long i = 0; i < iterations; i++) {
38 		for (unsigned long j = 0; j < zero_size; j += CACHELINE_SIZE)
39 			load(p + j);
40 		getppid();
41 	}
42 }
43 
44 int rfi_flush_test(void)
45 {
46 	char *p;
47 	int repetitions = 10;
48 	int fd, passes = 0, iter, rc = 0;
49 	struct perf_event_read v;
50 	__u64 l1d_misses_total = 0;
51 	unsigned long iterations = 100000, zero_size = 24 * 1024;
52 	unsigned long l1d_misses_expected;
53 	int rfi_flush_org, rfi_flush;
54 
55 	SKIP_IF(geteuid() != 0);
56 
57 	if (read_debugfs_file("powerpc/rfi_flush", &rfi_flush_org)) {
58 		perror("Unable to read powerpc/rfi_flush debugfs file");
59 		SKIP_IF(1);
60 	}
61 
62 	rfi_flush = rfi_flush_org;
63 
64 	fd = perf_event_open_counter(PERF_TYPE_RAW, /* L1d miss */ 0x400f0, -1);
65 	FAIL_IF(fd < 0);
66 
67 	p = (char *)memalign(zero_size, CACHELINE_SIZE);
68 
69 	FAIL_IF(perf_event_enable(fd));
70 
71 	set_dscr(1);
72 
73 	iter = repetitions;
74 
75 	/*
76 	 * We expect to see l1d miss for each cacheline access when rfi_flush
77 	 * is set. Allow a small variation on this.
78 	 */
79 	l1d_misses_expected = iterations * (zero_size / CACHELINE_SIZE - 2);
80 
81 again:
82 	FAIL_IF(perf_event_reset(fd));
83 
84 	syscall_loop(p, iterations, zero_size);
85 
86 	FAIL_IF(read(fd, &v, sizeof(v)) != sizeof(v));
87 
88 	if (rfi_flush && v.l1d_misses >= l1d_misses_expected)
89 		passes++;
90 	else if (!rfi_flush && v.l1d_misses < (l1d_misses_expected / 2))
91 		passes++;
92 
93 	l1d_misses_total += v.l1d_misses;
94 
95 	while (--iter)
96 		goto again;
97 
98 	if (passes < repetitions) {
99 		printf("FAIL (L1D misses with rfi_flush=%d: %llu %c %lu) [%d/%d failures]\n",
100 		       rfi_flush, l1d_misses_total, rfi_flush ? '<' : '>',
101 		       rfi_flush ? repetitions * l1d_misses_expected :
102 		       repetitions * l1d_misses_expected / 2,
103 		       repetitions - passes, repetitions);
104 		rc = 1;
105 	} else
106 		printf("PASS (L1D misses with rfi_flush=%d: %llu %c %lu) [%d/%d pass]\n",
107 		       rfi_flush, l1d_misses_total, rfi_flush ? '>' : '<',
108 		       rfi_flush ? repetitions * l1d_misses_expected :
109 		       repetitions * l1d_misses_expected / 2,
110 		       passes, repetitions);
111 
112 	if (rfi_flush == rfi_flush_org) {
113 		rfi_flush = !rfi_flush_org;
114 		if (write_debugfs_file("powerpc/rfi_flush", rfi_flush) < 0) {
115 			perror("error writing to powerpc/rfi_flush debugfs file");
116 			return 1;
117 		}
118 		iter = repetitions;
119 		l1d_misses_total = 0;
120 		passes = 0;
121 		goto again;
122 	}
123 
124 	perf_event_disable(fd);
125 	close(fd);
126 
127 	set_dscr(0);
128 
129 	if (write_debugfs_file("powerpc/rfi_flush", rfi_flush_org) < 0) {
130 		perror("unable to restore original value of powerpc/rfi_flush debugfs file");
131 		return 1;
132 	}
133 
134 	return rc;
135 }
136 
137 int main(int argc, char *argv[])
138 {
139 	return test_harness(rfi_flush_test, "rfi_flush_test");
140 }
141