10 #if defined(__x86_64__) || defined(__i386__)
12 #define barrier() asm volatile("" ::: "memory")
14 static u64 rdpmc(unsigned int counter)
16 unsigned int low, high;
18 asm volatile("rdpmc" : "=a" (low), "=d" (high) : "c" (counter));
20 return low | ((u64)high) << 32;
23 static u64 rdtsc(void)
25 unsigned int low, high;
27 asm volatile("rdtsc" : "=a" (low), "=d" (high));
29 return low | ((u64)high) << 32;
32 static u64 mmap_read_self(void *addr)
34 struct perf_event_mmap_page *pc = addr;
35 u32 seq, idx, time_mult = 0, time_shift = 0;
36 u64 count, cyc = 0, time_offset = 0, enabled, running, delta;
42 enabled = pc->time_enabled;
43 running = pc->time_running;
45 if (enabled != running) {
47 time_mult = pc->time_mult;
48 time_shift = pc->time_shift;
49 time_offset = pc->time_offset;
55 count += rdpmc(idx - 1);
58 } while (pc->lock != seq);
60 if (enabled != running) {
63 quot = (cyc >> time_shift);
64 rem = cyc & ((1 << time_shift) - 1);
65 delta = time_offset + quot * time_mult +
66 ((rem * time_mult) >> time_shift);
72 quot = count / running;
73 rem = count % running;
74 count = quot * enabled + (rem * enabled) / running;
81 * If the RDPMC instruction faults then signal this back to the test parent task:
83 static void segfault_handler(int sig __maybe_unused,
84 siginfo_t *info __maybe_unused,
85 void *uc __maybe_unused)
90 static int __test__rdpmc(void)
97 struct perf_event_attr attr = {
98 .type = PERF_TYPE_HARDWARE,
99 .config = PERF_COUNT_HW_INSTRUCTIONS,
105 sigfillset(&sa.sa_mask);
106 sa.sa_sigaction = segfault_handler;
107 sigaction(SIGSEGV, &sa, NULL);
109 fd = sys_perf_event_open(&attr, 0, -1, -1, 0);
111 pr_err("Error: sys_perf_event_open() syscall returned "
112 "with %d (%s)\n", fd, strerror(errno));
116 addr = mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd, 0);
117 if (addr == (void *)(-1)) {
118 pr_err("Error: mmap() syscall returned with (%s)\n",
123 for (n = 0; n < 6; n++) {
124 u64 stamp, now, delta;
126 stamp = mmap_read_self(addr);
128 for (i = 0; i < loops; i++)
131 now = mmap_read_self(addr);
135 pr_debug("%14d: %14Lu\n", n, (long long)delta);
140 munmap(addr, page_size);
151 int test__rdpmc(void)
163 ret = __test__rdpmc();
168 wret = waitpid(pid, &status, 0);
169 if (wret < 0 || status)