pmae_handling_test.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright 2014, Michael Ellerman, IBM Corp.
  3. * Licensed under GPLv2.
  4. */
  5. #include <sched.h>
  6. #include <signal.h>
  7. #include <stdbool.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include "ebb.h"
  11. /*
  12. * Test that the kernel properly handles PMAE across context switches.
  13. *
  14. * We test this by calling into the kernel inside our EBB handler, where PMAE
  15. * is clear. A cpu eater companion thread is running on the same CPU as us to
  16. * encourage the scheduler to switch us.
  17. *
  18. * The kernel must make sure that when it context switches us back in, it
  19. * honours the fact that we had PMAE clear.
  20. *
  21. * Observed to hit the failing case on the first EBB with a broken kernel.
  22. */
  23. static bool mmcr0_mismatch;
  24. static uint64_t before, after;
  25. static void syscall_ebb_callee(void)
  26. {
  27. uint64_t val;
  28. val = mfspr(SPRN_BESCR);
  29. if (!(val & BESCR_PMEO)) {
  30. ebb_state.stats.spurious++;
  31. goto out;
  32. }
  33. ebb_state.stats.ebb_count++;
  34. count_pmc(1, sample_period);
  35. before = mfspr(SPRN_MMCR0);
  36. /* Try and get ourselves scheduled, to force a PMU context switch */
  37. sched_yield();
  38. after = mfspr(SPRN_MMCR0);
  39. if (before != after)
  40. mmcr0_mismatch = true;
  41. out:
  42. reset_ebb();
  43. }
  44. static int test_body(void)
  45. {
  46. struct event event;
  47. event_init_named(&event, 0x1001e, "cycles");
  48. event_leader_ebb_init(&event);
  49. event.attr.exclude_kernel = 1;
  50. event.attr.exclude_hv = 1;
  51. event.attr.exclude_idle = 1;
  52. FAIL_IF(event_open(&event));
  53. setup_ebb_handler(syscall_ebb_callee);
  54. ebb_global_enable();
  55. FAIL_IF(ebb_event_enable(&event));
  56. mtspr(SPRN_PMC1, pmc_sample_period(sample_period));
  57. while (ebb_state.stats.ebb_count < 20 && !mmcr0_mismatch)
  58. FAIL_IF(core_busy_loop());
  59. ebb_global_disable();
  60. ebb_freeze_pmcs();
  61. count_pmc(1, sample_period);
  62. dump_ebb_state();
  63. if (mmcr0_mismatch)
  64. printf("Saw MMCR0 before 0x%lx after 0x%lx\n", before, after);
  65. event_close(&event);
  66. FAIL_IF(ebb_state.stats.ebb_count == 0);
  67. FAIL_IF(mmcr0_mismatch);
  68. return 0;
  69. }
  70. int pmae_handling(void)
  71. {
  72. return eat_cpu(test_body);
  73. }
  74. int main(void)
  75. {
  76. return test_harness(pmae_handling, "pmae_handling");
  77. }