ebb.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. * Copyright 2014, Michael Ellerman, IBM Corp.
  3. * Licensed under GPLv2.
  4. */
  5. #define _GNU_SOURCE /* For CPU_ZERO etc. */
  6. #include <sched.h>
  7. #include <sys/wait.h>
  8. #include <setjmp.h>
  9. #include <signal.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <sys/ioctl.h>
  14. #include "trace.h"
  15. #include "ebb.h"
  16. void (*ebb_user_func)(void);
  17. void ebb_hook(void)
  18. {
  19. if (ebb_user_func)
  20. ebb_user_func();
  21. }
  22. struct ebb_state ebb_state;
  23. u64 sample_period = 0x40000000ull;
  24. void reset_ebb_with_clear_mask(unsigned long mmcr0_clear_mask)
  25. {
  26. u64 val;
  27. /* 2) clear MMCR0[PMAO] - docs say BESCR[PMEO] should do this */
  28. /* 3) set MMCR0[PMAE] - docs say BESCR[PME] should do this */
  29. val = mfspr(SPRN_MMCR0);
  30. mtspr(SPRN_MMCR0, (val & ~mmcr0_clear_mask) | MMCR0_PMAE);
  31. /* 4) clear BESCR[PMEO] */
  32. mtspr(SPRN_BESCRR, BESCR_PMEO);
  33. /* 5) set BESCR[PME] */
  34. mtspr(SPRN_BESCRS, BESCR_PME);
  35. /* 6) rfebb 1 - done in our caller */
  36. }
  37. void reset_ebb(void)
  38. {
  39. reset_ebb_with_clear_mask(MMCR0_PMAO | MMCR0_FC);
  40. }
  41. /* Called outside of the EBB handler to check MMCR0 is sane */
  42. int ebb_check_mmcr0(void)
  43. {
  44. u64 val;
  45. val = mfspr(SPRN_MMCR0);
  46. if ((val & (MMCR0_FC | MMCR0_PMAO)) == MMCR0_FC) {
  47. /* It's OK if we see FC & PMAO, but not FC by itself */
  48. printf("Outside of loop, only FC set 0x%llx\n", val);
  49. return 1;
  50. }
  51. return 0;
  52. }
  53. bool ebb_check_count(int pmc, u64 sample_period, int fudge)
  54. {
  55. u64 count, upper, lower;
  56. count = ebb_state.stats.pmc_count[PMC_INDEX(pmc)];
  57. lower = ebb_state.stats.ebb_count * (sample_period - fudge);
  58. if (count < lower) {
  59. printf("PMC%d count (0x%llx) below lower limit 0x%llx (-0x%llx)\n",
  60. pmc, count, lower, lower - count);
  61. return false;
  62. }
  63. upper = ebb_state.stats.ebb_count * (sample_period + fudge);
  64. if (count > upper) {
  65. printf("PMC%d count (0x%llx) above upper limit 0x%llx (+0x%llx)\n",
  66. pmc, count, upper, count - upper);
  67. return false;
  68. }
  69. printf("PMC%d count (0x%llx) is between 0x%llx and 0x%llx delta +0x%llx/-0x%llx\n",
  70. pmc, count, lower, upper, count - lower, upper - count);
  71. return true;
  72. }
  73. void standard_ebb_callee(void)
  74. {
  75. int found, i;
  76. u64 val;
  77. val = mfspr(SPRN_BESCR);
  78. if (!(val & BESCR_PMEO)) {
  79. ebb_state.stats.spurious++;
  80. goto out;
  81. }
  82. ebb_state.stats.ebb_count++;
  83. trace_log_counter(ebb_state.trace, ebb_state.stats.ebb_count);
  84. val = mfspr(SPRN_MMCR0);
  85. trace_log_reg(ebb_state.trace, SPRN_MMCR0, val);
  86. found = 0;
  87. for (i = 1; i <= 6; i++) {
  88. if (ebb_state.pmc_enable[PMC_INDEX(i)])
  89. found += count_pmc(i, sample_period);
  90. }
  91. if (!found)
  92. ebb_state.stats.no_overflow++;
  93. out:
  94. reset_ebb();
  95. }
  96. extern void ebb_handler(void);
  97. void setup_ebb_handler(void (*callee)(void))
  98. {
  99. u64 entry;
  100. #if defined(_CALL_ELF) && _CALL_ELF == 2
  101. entry = (u64)ebb_handler;
  102. #else
  103. struct opd
  104. {
  105. u64 entry;
  106. u64 toc;
  107. } *opd;
  108. opd = (struct opd *)ebb_handler;
  109. entry = opd->entry;
  110. #endif
  111. printf("EBB Handler is at %#llx\n", entry);
  112. ebb_user_func = callee;
  113. /* Ensure ebb_user_func is set before we set the handler */
  114. mb();
  115. mtspr(SPRN_EBBHR, entry);
  116. /* Make sure the handler is set before we return */
  117. mb();
  118. }
  119. void clear_ebb_stats(void)
  120. {
  121. memset(&ebb_state.stats, 0, sizeof(ebb_state.stats));
  122. }
  123. void dump_summary_ebb_state(void)
  124. {
  125. printf("ebb_state:\n" \
  126. " ebb_count = %d\n" \
  127. " spurious = %d\n" \
  128. " negative = %d\n" \
  129. " no_overflow = %d\n" \
  130. " pmc[1] count = 0x%llx\n" \
  131. " pmc[2] count = 0x%llx\n" \
  132. " pmc[3] count = 0x%llx\n" \
  133. " pmc[4] count = 0x%llx\n" \
  134. " pmc[5] count = 0x%llx\n" \
  135. " pmc[6] count = 0x%llx\n",
  136. ebb_state.stats.ebb_count, ebb_state.stats.spurious,
  137. ebb_state.stats.negative, ebb_state.stats.no_overflow,
  138. ebb_state.stats.pmc_count[0], ebb_state.stats.pmc_count[1],
  139. ebb_state.stats.pmc_count[2], ebb_state.stats.pmc_count[3],
  140. ebb_state.stats.pmc_count[4], ebb_state.stats.pmc_count[5]);
  141. }
  142. static char *decode_mmcr0(u32 value)
  143. {
  144. static char buf[16];
  145. buf[0] = '\0';
  146. if (value & (1 << 31))
  147. strcat(buf, "FC ");
  148. if (value & (1 << 26))
  149. strcat(buf, "PMAE ");
  150. if (value & (1 << 7))
  151. strcat(buf, "PMAO ");
  152. return buf;
  153. }
  154. static char *decode_bescr(u64 value)
  155. {
  156. static char buf[16];
  157. buf[0] = '\0';
  158. if (value & (1ull << 63))
  159. strcat(buf, "GE ");
  160. if (value & (1ull << 32))
  161. strcat(buf, "PMAE ");
  162. if (value & 1)
  163. strcat(buf, "PMAO ");
  164. return buf;
  165. }
  166. void dump_ebb_hw_state(void)
  167. {
  168. u64 bescr;
  169. u32 mmcr0;
  170. mmcr0 = mfspr(SPRN_MMCR0);
  171. bescr = mfspr(SPRN_BESCR);
  172. printf("HW state:\n" \
  173. "MMCR0 0x%016x %s\n" \
  174. "MMCR2 0x%016lx\n" \
  175. "EBBHR 0x%016lx\n" \
  176. "BESCR 0x%016llx %s\n" \
  177. "PMC1 0x%016lx\n" \
  178. "PMC2 0x%016lx\n" \
  179. "PMC3 0x%016lx\n" \
  180. "PMC4 0x%016lx\n" \
  181. "PMC5 0x%016lx\n" \
  182. "PMC6 0x%016lx\n" \
  183. "SIAR 0x%016lx\n",
  184. mmcr0, decode_mmcr0(mmcr0), mfspr(SPRN_MMCR2),
  185. mfspr(SPRN_EBBHR), bescr, decode_bescr(bescr),
  186. mfspr(SPRN_PMC1), mfspr(SPRN_PMC2), mfspr(SPRN_PMC3),
  187. mfspr(SPRN_PMC4), mfspr(SPRN_PMC5), mfspr(SPRN_PMC6),
  188. mfspr(SPRN_SIAR));
  189. }
  190. void dump_ebb_state(void)
  191. {
  192. dump_summary_ebb_state();
  193. dump_ebb_hw_state();
  194. trace_buffer_print(ebb_state.trace);
  195. }
  196. int count_pmc(int pmc, uint32_t sample_period)
  197. {
  198. uint32_t start_value;
  199. u64 val;
  200. /* 0) Read PMC */
  201. start_value = pmc_sample_period(sample_period);
  202. val = read_pmc(pmc);
  203. if (val < start_value)
  204. ebb_state.stats.negative++;
  205. else
  206. ebb_state.stats.pmc_count[PMC_INDEX(pmc)] += val - start_value;
  207. trace_log_reg(ebb_state.trace, SPRN_PMC1 + pmc - 1, val);
  208. /* 1) Reset PMC */
  209. write_pmc(pmc, start_value);
  210. /* Report if we overflowed */
  211. return val >= COUNTER_OVERFLOW;
  212. }
  213. int ebb_event_enable(struct event *e)
  214. {
  215. int rc;
  216. /* Ensure any SPR writes are ordered vs us */
  217. mb();
  218. rc = ioctl(e->fd, PERF_EVENT_IOC_ENABLE);
  219. if (rc)
  220. return rc;
  221. rc = event_read(e);
  222. /* Ditto */
  223. mb();
  224. return rc;
  225. }
  226. void ebb_freeze_pmcs(void)
  227. {
  228. mtspr(SPRN_MMCR0, mfspr(SPRN_MMCR0) | MMCR0_FC);
  229. mb();
  230. }
  231. void ebb_unfreeze_pmcs(void)
  232. {
  233. /* Unfreeze counters */
  234. mtspr(SPRN_MMCR0, mfspr(SPRN_MMCR0) & ~MMCR0_FC);
  235. mb();
  236. }
  237. void ebb_global_enable(void)
  238. {
  239. /* Enable EBBs globally and PMU EBBs */
  240. mtspr(SPRN_BESCR, 0x8000000100000000ull);
  241. mb();
  242. }
  243. void ebb_global_disable(void)
  244. {
  245. /* Disable EBBs & freeze counters, events are still scheduled */
  246. mtspr(SPRN_BESCRR, BESCR_PME);
  247. mb();
  248. }
  249. bool ebb_is_supported(void)
  250. {
  251. #ifdef PPC_FEATURE2_EBB
  252. /* EBB requires at least POWER8 */
  253. return have_hwcap2(PPC_FEATURE2_EBB);
  254. #else
  255. return false;
  256. #endif
  257. }
  258. void event_ebb_init(struct event *e)
  259. {
  260. e->attr.config |= (1ull << 63);
  261. }
  262. void event_bhrb_init(struct event *e, unsigned ifm)
  263. {
  264. e->attr.config |= (1ull << 62) | ((u64)ifm << 60);
  265. }
  266. void event_leader_ebb_init(struct event *e)
  267. {
  268. event_ebb_init(e);
  269. e->attr.exclusive = 1;
  270. e->attr.pinned = 1;
  271. }
  272. int ebb_child(union pipe read_pipe, union pipe write_pipe)
  273. {
  274. struct event event;
  275. uint64_t val;
  276. FAIL_IF(wait_for_parent(read_pipe));
  277. event_init_named(&event, 0x1001e, "cycles");
  278. event_leader_ebb_init(&event);
  279. event.attr.exclude_kernel = 1;
  280. event.attr.exclude_hv = 1;
  281. event.attr.exclude_idle = 1;
  282. FAIL_IF(event_open(&event));
  283. ebb_enable_pmc_counting(1);
  284. setup_ebb_handler(standard_ebb_callee);
  285. ebb_global_enable();
  286. FAIL_IF(event_enable(&event));
  287. if (event_read(&event)) {
  288. /*
  289. * Some tests expect to fail here, so don't report an error on
  290. * this line, and return a distinguisable error code. Tell the
  291. * parent an error happened.
  292. */
  293. notify_parent_of_error(write_pipe);
  294. return 2;
  295. }
  296. mtspr(SPRN_PMC1, pmc_sample_period(sample_period));
  297. FAIL_IF(notify_parent(write_pipe));
  298. FAIL_IF(wait_for_parent(read_pipe));
  299. FAIL_IF(notify_parent(write_pipe));
  300. while (ebb_state.stats.ebb_count < 20) {
  301. FAIL_IF(core_busy_loop());
  302. /* To try and hit SIGILL case */
  303. val = mfspr(SPRN_MMCRA);
  304. val |= mfspr(SPRN_MMCR2);
  305. val |= mfspr(SPRN_MMCR0);
  306. }
  307. ebb_global_disable();
  308. ebb_freeze_pmcs();
  309. count_pmc(1, sample_period);
  310. dump_ebb_state();
  311. event_close(&event);
  312. FAIL_IF(ebb_state.stats.ebb_count == 0);
  313. return 0;
  314. }
  315. static jmp_buf setjmp_env;
  316. static void sigill_handler(int signal)
  317. {
  318. printf("Took sigill\n");
  319. longjmp(setjmp_env, 1);
  320. }
  321. static struct sigaction sigill_action = {
  322. .sa_handler = sigill_handler,
  323. };
  324. int catch_sigill(void (*func)(void))
  325. {
  326. if (sigaction(SIGILL, &sigill_action, NULL)) {
  327. perror("sigaction");
  328. return 1;
  329. }
  330. if (setjmp(setjmp_env) == 0) {
  331. func();
  332. return 1;
  333. }
  334. return 0;
  335. }
  336. void write_pmc1(void)
  337. {
  338. mtspr(SPRN_PMC1, 0);
  339. }
  340. void write_pmc(int pmc, u64 value)
  341. {
  342. switch (pmc) {
  343. case 1: mtspr(SPRN_PMC1, value); break;
  344. case 2: mtspr(SPRN_PMC2, value); break;
  345. case 3: mtspr(SPRN_PMC3, value); break;
  346. case 4: mtspr(SPRN_PMC4, value); break;
  347. case 5: mtspr(SPRN_PMC5, value); break;
  348. case 6: mtspr(SPRN_PMC6, value); break;
  349. }
  350. }
  351. u64 read_pmc(int pmc)
  352. {
  353. switch (pmc) {
  354. case 1: return mfspr(SPRN_PMC1);
  355. case 2: return mfspr(SPRN_PMC2);
  356. case 3: return mfspr(SPRN_PMC3);
  357. case 4: return mfspr(SPRN_PMC4);
  358. case 5: return mfspr(SPRN_PMC5);
  359. case 6: return mfspr(SPRN_PMC6);
  360. }
  361. return 0;
  362. }
  363. static void term_handler(int signal)
  364. {
  365. dump_summary_ebb_state();
  366. dump_ebb_hw_state();
  367. abort();
  368. }
  369. struct sigaction term_action = {
  370. .sa_handler = term_handler,
  371. };
  372. static void __attribute__((constructor)) ebb_init(void)
  373. {
  374. clear_ebb_stats();
  375. if (sigaction(SIGTERM, &term_action, NULL))
  376. perror("sigaction");
  377. ebb_state.trace = trace_buffer_allocate(1 * 1024 * 1024);
  378. }