rdpmc.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <errno.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <signal.h>
  6. #include <sys/mman.h>
  7. #include <sys/types.h>
  8. #include <sys/wait.h>
  9. #include <linux/types.h>
  10. #include "perf.h"
  11. #include "debug.h"
  12. #include "tests/tests.h"
  13. #include "cloexec.h"
  14. #include "util.h"
  15. #include "arch-tests.h"
  16. static u64 rdpmc(unsigned int counter)
  17. {
  18. unsigned int low, high;
  19. asm volatile("rdpmc" : "=a" (low), "=d" (high) : "c" (counter));
  20. return low | ((u64)high) << 32;
  21. }
  22. static u64 rdtsc(void)
  23. {
  24. unsigned int low, high;
  25. asm volatile("rdtsc" : "=a" (low), "=d" (high));
  26. return low | ((u64)high) << 32;
  27. }
  28. static u64 mmap_read_self(void *addr)
  29. {
  30. struct perf_event_mmap_page *pc = addr;
  31. u32 seq, idx, time_mult = 0, time_shift = 0;
  32. u64 count, cyc = 0, time_offset = 0, enabled, running, delta;
  33. do {
  34. seq = pc->lock;
  35. barrier();
  36. enabled = pc->time_enabled;
  37. running = pc->time_running;
  38. if (enabled != running) {
  39. cyc = rdtsc();
  40. time_mult = pc->time_mult;
  41. time_shift = pc->time_shift;
  42. time_offset = pc->time_offset;
  43. }
  44. idx = pc->index;
  45. count = pc->offset;
  46. if (idx)
  47. count += rdpmc(idx - 1);
  48. barrier();
  49. } while (pc->lock != seq);
  50. if (enabled != running) {
  51. u64 quot, rem;
  52. quot = (cyc >> time_shift);
  53. rem = cyc & (((u64)1 << time_shift) - 1);
  54. delta = time_offset + quot * time_mult +
  55. ((rem * time_mult) >> time_shift);
  56. enabled += delta;
  57. if (idx)
  58. running += delta;
  59. quot = count / running;
  60. rem = count % running;
  61. count = quot * enabled + (rem * enabled) / running;
  62. }
  63. return count;
  64. }
  65. /*
  66. * If the RDPMC instruction faults then signal this back to the test parent task:
  67. */
  68. static void segfault_handler(int sig __maybe_unused,
  69. siginfo_t *info __maybe_unused,
  70. void *uc __maybe_unused)
  71. {
  72. exit(-1);
  73. }
  74. static int __test__rdpmc(void)
  75. {
  76. volatile int tmp = 0;
  77. u64 i, loops = 1000;
  78. int n;
  79. int fd;
  80. void *addr;
  81. struct perf_event_attr attr = {
  82. .type = PERF_TYPE_HARDWARE,
  83. .config = PERF_COUNT_HW_INSTRUCTIONS,
  84. .exclude_kernel = 1,
  85. };
  86. u64 delta_sum = 0;
  87. struct sigaction sa;
  88. char sbuf[STRERR_BUFSIZE];
  89. sigfillset(&sa.sa_mask);
  90. sa.sa_sigaction = segfault_handler;
  91. sa.sa_flags = 0;
  92. sigaction(SIGSEGV, &sa, NULL);
  93. fd = sys_perf_event_open(&attr, 0, -1, -1,
  94. perf_event_open_cloexec_flag());
  95. if (fd < 0) {
  96. pr_err("Error: sys_perf_event_open() syscall returned "
  97. "with %d (%s)\n", fd,
  98. str_error_r(errno, sbuf, sizeof(sbuf)));
  99. return -1;
  100. }
  101. addr = mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd, 0);
  102. if (addr == (void *)(-1)) {
  103. pr_err("Error: mmap() syscall returned with (%s)\n",
  104. str_error_r(errno, sbuf, sizeof(sbuf)));
  105. goto out_close;
  106. }
  107. for (n = 0; n < 6; n++) {
  108. u64 stamp, now, delta;
  109. stamp = mmap_read_self(addr);
  110. for (i = 0; i < loops; i++)
  111. tmp++;
  112. now = mmap_read_self(addr);
  113. loops *= 10;
  114. delta = now - stamp;
  115. pr_debug("%14d: %14Lu\n", n, (long long)delta);
  116. delta_sum += delta;
  117. }
  118. munmap(addr, page_size);
  119. pr_debug(" ");
  120. out_close:
  121. close(fd);
  122. if (!delta_sum)
  123. return -1;
  124. return 0;
  125. }
  126. int test__rdpmc(struct test *test __maybe_unused, int subtest __maybe_unused)
  127. {
  128. int status = 0;
  129. int wret = 0;
  130. int ret;
  131. int pid;
  132. pid = fork();
  133. if (pid < 0)
  134. return -1;
  135. if (!pid) {
  136. ret = __test__rdpmc();
  137. exit(ret);
  138. }
  139. wret = waitpid(pid, &status, 0);
  140. if (wret < 0 || status)
  141. return -1;
  142. return 0;
  143. }