pmae_handling_test.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. SKIP_IF(!ebb_is_supported());
  48. event_init_named(&event, 0x1001e, "cycles");
  49. event_leader_ebb_init(&event);
  50. event.attr.exclude_kernel = 1;
  51. event.attr.exclude_hv = 1;
  52. event.attr.exclude_idle = 1;
  53. FAIL_IF(event_open(&event));
  54. setup_ebb_handler(syscall_ebb_callee);
  55. ebb_global_enable();
  56. FAIL_IF(ebb_event_enable(&event));
  57. mtspr(SPRN_PMC1, pmc_sample_period(sample_period));
  58. while (ebb_state.stats.ebb_count < 20 && !mmcr0_mismatch)
  59. FAIL_IF(core_busy_loop());
  60. ebb_global_disable();
  61. ebb_freeze_pmcs();
  62. count_pmc(1, sample_period);
  63. dump_ebb_state();
  64. if (mmcr0_mismatch)
  65. printf("Saw MMCR0 before 0x%lx after 0x%lx\n", before, after);
  66. event_close(&event);
  67. FAIL_IF(ebb_state.stats.ebb_count == 0);
  68. FAIL_IF(mmcr0_mismatch);
  69. return 0;
  70. }
  71. int pmae_handling(void)
  72. {
  73. return eat_cpu(test_body);
  74. }
  75. int main(void)
  76. {
  77. return test_harness(pmae_handling, "pmae_handling");
  78. }