5 #include <linux/types.h>
11 #if defined(__x86_64__) || defined(__i386__)
13 static u64 rdpmc(unsigned int counter)
15 unsigned int low, high;
17 asm volatile("rdpmc" : "=a" (low), "=d" (high) : "c" (counter));
19 return low | ((u64)high) << 32;
22 static u64 rdtsc(void)
24 unsigned int low, high;
26 asm volatile("rdtsc" : "=a" (low), "=d" (high));
28 return low | ((u64)high) << 32;
31 static u64 mmap_read_self(void *addr)
33 struct perf_event_mmap_page *pc = addr;
34 u32 seq, idx, time_mult = 0, time_shift = 0;
35 u64 count, cyc = 0, time_offset = 0, enabled, running, delta;
41 enabled = pc->time_enabled;
42 running = pc->time_running;
44 if (enabled != running) {
46 time_mult = pc->time_mult;
47 time_shift = pc->time_shift;
48 time_offset = pc->time_offset;
54 count += rdpmc(idx - 1);
57 } while (pc->lock != seq);
59 if (enabled != running) {
62 quot = (cyc >> time_shift);
63 rem = cyc & ((1 << time_shift) - 1);
64 delta = time_offset + quot * time_mult +
65 ((rem * time_mult) >> time_shift);
71 quot = count / running;
72 rem = count % running;
73 count = quot * enabled + (rem * enabled) / running;
80 * If the RDPMC instruction faults then signal this back to the test parent task:
82 static void segfault_handler(int sig __maybe_unused,
83 siginfo_t *info __maybe_unused,
84 void *uc __maybe_unused)
89 static int __test__rdpmc(void)
96 struct perf_event_attr attr = {
97 .type = PERF_TYPE_HARDWARE,
98 .config = PERF_COUNT_HW_INSTRUCTIONS,
103 char sbuf[STRERR_BUFSIZE];
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,
110 perf_event_open_cloexec_flag());
112 pr_err("Error: sys_perf_event_open() syscall returned "
113 "with %d (%s)\n", fd,
114 strerror_r(errno, sbuf, sizeof(sbuf)));
118 addr = mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd, 0);
119 if (addr == (void *)(-1)) {
120 pr_err("Error: mmap() syscall returned with (%s)\n",
121 strerror_r(errno, sbuf, sizeof(sbuf)));
125 for (n = 0; n < 6; n++) {
126 u64 stamp, now, delta;
128 stamp = mmap_read_self(addr);
130 for (i = 0; i < loops; i++)
133 now = mmap_read_self(addr);
137 pr_debug("%14d: %14Lu\n", n, (long long)delta);
142 munmap(addr, page_size);
153 int test__rdpmc(void)
165 ret = __test__rdpmc();
170 wret = waitpid(pid, &status, 0);
171 if (wret < 0 || status)