8 #include <linux/types.h>
11 #include "tests/tests.h"
14 #include "arch-tests.h"
16 static u64
rdpmc(unsigned int counter
)
18 unsigned int low
, high
;
20 asm volatile("rdpmc" : "=a" (low
), "=d" (high
) : "c" (counter
));
22 return low
| ((u64
)high
) << 32;
25 static u64
rdtsc(void)
27 unsigned int low
, high
;
29 asm volatile("rdtsc" : "=a" (low
), "=d" (high
));
31 return low
| ((u64
)high
) << 32;
34 static u64
mmap_read_self(void *addr
)
36 struct perf_event_mmap_page
*pc
= addr
;
37 u32 seq
, idx
, time_mult
= 0, time_shift
= 0;
38 u64 count
, cyc
= 0, time_offset
= 0, enabled
, running
, delta
;
44 enabled
= pc
->time_enabled
;
45 running
= pc
->time_running
;
47 if (enabled
!= running
) {
49 time_mult
= pc
->time_mult
;
50 time_shift
= pc
->time_shift
;
51 time_offset
= pc
->time_offset
;
57 count
+= rdpmc(idx
- 1);
60 } while (pc
->lock
!= seq
);
62 if (enabled
!= running
) {
65 quot
= (cyc
>> time_shift
);
66 rem
= cyc
& (((u64
)1 << time_shift
) - 1);
67 delta
= time_offset
+ quot
* time_mult
+
68 ((rem
* time_mult
) >> time_shift
);
74 quot
= count
/ running
;
75 rem
= count
% running
;
76 count
= quot
* enabled
+ (rem
* enabled
) / running
;
83 * If the RDPMC instruction faults then signal this back to the test parent task:
85 static void segfault_handler(int sig __maybe_unused
,
86 siginfo_t
*info __maybe_unused
,
87 void *uc __maybe_unused
)
92 static int __test__rdpmc(void)
99 struct perf_event_attr attr
= {
100 .type
= PERF_TYPE_HARDWARE
,
101 .config
= PERF_COUNT_HW_INSTRUCTIONS
,
106 char sbuf
[STRERR_BUFSIZE
];
108 sigfillset(&sa
.sa_mask
);
109 sa
.sa_sigaction
= segfault_handler
;
111 sigaction(SIGSEGV
, &sa
, NULL
);
113 fd
= sys_perf_event_open(&attr
, 0, -1, -1,
114 perf_event_open_cloexec_flag());
116 pr_err("Error: sys_perf_event_open() syscall returned "
117 "with %d (%s)\n", fd
,
118 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
122 addr
= mmap(NULL
, page_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
123 if (addr
== (void *)(-1)) {
124 pr_err("Error: mmap() syscall returned with (%s)\n",
125 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
129 for (n
= 0; n
< 6; n
++) {
130 u64 stamp
, now
, delta
;
132 stamp
= mmap_read_self(addr
);
134 for (i
= 0; i
< loops
; i
++)
137 now
= mmap_read_self(addr
);
141 pr_debug("%14d: %14Lu\n", n
, (long long)delta
);
146 munmap(addr
, page_size
);
157 int test__rdpmc(struct test
*test __maybe_unused
, int subtest __maybe_unused
)
169 ret
= __test__rdpmc();
174 wret
= waitpid(pid
, &status
, 0);
175 if (wret
< 0 || status
)