10 #if defined(__x86_64__) || defined(__i386__)
12 static u64
rdpmc(unsigned int counter
)
14 unsigned int low
, high
;
16 asm volatile("rdpmc" : "=a" (low
), "=d" (high
) : "c" (counter
));
18 return low
| ((u64
)high
) << 32;
21 static u64
rdtsc(void)
23 unsigned int low
, high
;
25 asm volatile("rdtsc" : "=a" (low
), "=d" (high
));
27 return low
| ((u64
)high
) << 32;
30 static u64
mmap_read_self(void *addr
)
32 struct perf_event_mmap_page
*pc
= addr
;
33 u32 seq
, idx
, time_mult
= 0, time_shift
= 0;
34 u64 count
, cyc
= 0, time_offset
= 0, enabled
, running
, delta
;
40 enabled
= pc
->time_enabled
;
41 running
= pc
->time_running
;
43 if (enabled
!= running
) {
45 time_mult
= pc
->time_mult
;
46 time_shift
= pc
->time_shift
;
47 time_offset
= pc
->time_offset
;
53 count
+= rdpmc(idx
- 1);
56 } while (pc
->lock
!= seq
);
58 if (enabled
!= running
) {
61 quot
= (cyc
>> time_shift
);
62 rem
= cyc
& ((1 << time_shift
) - 1);
63 delta
= time_offset
+ quot
* time_mult
+
64 ((rem
* time_mult
) >> time_shift
);
70 quot
= count
/ running
;
71 rem
= count
% running
;
72 count
= quot
* enabled
+ (rem
* enabled
) / running
;
79 * If the RDPMC instruction faults then signal this back to the test parent task:
81 static void segfault_handler(int sig __maybe_unused
,
82 siginfo_t
*info __maybe_unused
,
83 void *uc __maybe_unused
)
88 static int __test__rdpmc(void)
95 struct perf_event_attr attr
= {
96 .type
= PERF_TYPE_HARDWARE
,
97 .config
= PERF_COUNT_HW_INSTRUCTIONS
,
103 sigfillset(&sa
.sa_mask
);
104 sa
.sa_sigaction
= segfault_handler
;
105 sigaction(SIGSEGV
, &sa
, NULL
);
107 fd
= sys_perf_event_open(&attr
, 0, -1, -1, 0);
109 pr_err("Error: sys_perf_event_open() syscall returned "
110 "with %d (%s)\n", fd
, strerror(errno
));
114 addr
= mmap(NULL
, page_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
115 if (addr
== (void *)(-1)) {
116 pr_err("Error: mmap() syscall returned with (%s)\n",
121 for (n
= 0; n
< 6; n
++) {
122 u64 stamp
, now
, delta
;
124 stamp
= mmap_read_self(addr
);
126 for (i
= 0; i
< loops
; i
++)
129 now
= mmap_read_self(addr
);
133 pr_debug("%14d: %14Lu\n", n
, (long long)delta
);
138 munmap(addr
, page_size
);
149 int test__rdpmc(void)
161 ret
= __test__rdpmc();
166 wret
= waitpid(pid
, &status
, 0);
167 if (wret
< 0 || status
)