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