lost_exception_test.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright 2014, Michael Ellerman, IBM Corp.
  3. * Licensed under GPLv2.
  4. */
  5. #include <sched.h>
  6. #include <signal.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <sys/mman.h>
  10. #include "ebb.h"
  11. /*
  12. * Test that tries to trigger CPU_FTR_PMAO_BUG. Which is a hardware defect
  13. * where an exception triggers but we context switch before it is delivered and
  14. * lose the exception.
  15. */
  16. static int test_body(void)
  17. {
  18. int i, orig_period, max_period;
  19. struct event event;
  20. SKIP_IF(!ebb_is_supported());
  21. /* We use PMC4 to make sure the kernel switches all counters correctly */
  22. event_init_named(&event, 0x40002, "instructions");
  23. event_leader_ebb_init(&event);
  24. event.attr.exclude_kernel = 1;
  25. event.attr.exclude_hv = 1;
  26. event.attr.exclude_idle = 1;
  27. FAIL_IF(event_open(&event));
  28. ebb_enable_pmc_counting(4);
  29. setup_ebb_handler(standard_ebb_callee);
  30. ebb_global_enable();
  31. FAIL_IF(ebb_event_enable(&event));
  32. /*
  33. * We want a low sample period, but we also want to get out of the EBB
  34. * handler without tripping up again.
  35. *
  36. * This value picked after much experimentation.
  37. */
  38. orig_period = max_period = sample_period = 400;
  39. mtspr(SPRN_PMC4, pmc_sample_period(sample_period));
  40. while (ebb_state.stats.ebb_count < 1000000) {
  41. /*
  42. * We are trying to get the EBB exception to race exactly with
  43. * us entering the kernel to do the syscall. We then need the
  44. * kernel to decide our timeslice is up and context switch to
  45. * the other thread. When we come back our EBB will have been
  46. * lost and we'll spin in this while loop forever.
  47. */
  48. for (i = 0; i < 100000; i++)
  49. sched_yield();
  50. /* Change the sample period slightly to try and hit the race */
  51. if (sample_period >= (orig_period + 200))
  52. sample_period = orig_period;
  53. else
  54. sample_period++;
  55. if (sample_period > max_period)
  56. max_period = sample_period;
  57. }
  58. ebb_freeze_pmcs();
  59. ebb_global_disable();
  60. count_pmc(4, sample_period);
  61. mtspr(SPRN_PMC4, 0xdead);
  62. dump_summary_ebb_state();
  63. dump_ebb_hw_state();
  64. event_close(&event);
  65. FAIL_IF(ebb_state.stats.ebb_count == 0);
  66. /* We vary our sample period so we need extra fudge here */
  67. FAIL_IF(!ebb_check_count(4, orig_period, 2 * (max_period - orig_period)));
  68. return 0;
  69. }
  70. static int lost_exception(void)
  71. {
  72. return eat_cpu(test_body);
  73. }
  74. int main(void)
  75. {
  76. return test_harness(lost_exception, "lost_exception");
  77. }