fork_cleanup_test.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright 2014, Michael Ellerman, IBM Corp.
  3. * Licensed under GPLv2.
  4. */
  5. #include <signal.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <stdbool.h>
  9. #include <sys/types.h>
  10. #include <sys/wait.h>
  11. #include <unistd.h>
  12. #include <setjmp.h>
  13. #include <signal.h>
  14. #include "ebb.h"
  15. /*
  16. * Test that a fork clears the PMU state of the child. eg. BESCR/EBBHR/EBBRR
  17. * are cleared, and MMCR0_PMCC is reset, preventing the child from accessing
  18. * the PMU.
  19. */
  20. static struct event event;
  21. static int child(void)
  22. {
  23. /* Even though we have EBE=0 we can still see the EBB regs */
  24. FAIL_IF(mfspr(SPRN_BESCR) != 0);
  25. FAIL_IF(mfspr(SPRN_EBBHR) != 0);
  26. FAIL_IF(mfspr(SPRN_EBBRR) != 0);
  27. FAIL_IF(catch_sigill(write_pmc1));
  28. /* We can still read from the event, though it is on our parent */
  29. FAIL_IF(event_read(&event));
  30. return 0;
  31. }
  32. /* Tests that fork clears EBB state */
  33. int fork_cleanup(void)
  34. {
  35. pid_t pid;
  36. event_init_named(&event, 0x1001e, "cycles");
  37. event_leader_ebb_init(&event);
  38. FAIL_IF(event_open(&event));
  39. ebb_enable_pmc_counting(1);
  40. setup_ebb_handler(standard_ebb_callee);
  41. ebb_global_enable();
  42. FAIL_IF(ebb_event_enable(&event));
  43. mtspr(SPRN_MMCR0, MMCR0_FC);
  44. mtspr(SPRN_PMC1, pmc_sample_period(sample_period));
  45. /* Don't need to actually take any EBBs */
  46. pid = fork();
  47. if (pid == 0)
  48. exit(child());
  49. /* Child does the actual testing */
  50. FAIL_IF(wait_for_child(pid));
  51. /* After fork */
  52. event_close(&event);
  53. return 0;
  54. }
  55. int main(void)
  56. {
  57. return test_harness(fork_cleanup, "fork_cleanup");
  58. }