dscr_explicit_test.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * POWER Data Stream Control Register (DSCR) explicit test
  4. *
  5. * This test modifies the DSCR value using mtspr instruction and
  6. * verifies the change with mfspr instruction. It uses both the
  7. * privilege state SPR and the problem state SPR for this purpose.
  8. *
  9. * When using the privilege state SPR, the instructions such as
  10. * mfspr or mtspr are privileged and the kernel emulates them
  11. * for us. Instructions using problem state SPR can be executed
  12. * directly without any emulation if the HW supports them. Else
  13. * they also get emulated by the kernel.
  14. *
  15. * Copyright 2012, Anton Blanchard, IBM Corporation.
  16. * Copyright 2015, Anshuman Khandual, IBM Corporation.
  17. */
  18. #define _GNU_SOURCE
  19. #include "dscr.h"
  20. #include "utils.h"
  21. #include <pthread.h>
  22. #include <sched.h>
  23. #include <semaphore.h>
  24. void *dscr_explicit_lockstep_thread(void *args)
  25. {
  26. sem_t *prev = (sem_t *)args;
  27. sem_t *next = (sem_t *)args + 1;
  28. unsigned long expected_dscr = 0;
  29. set_dscr(expected_dscr);
  30. srand(gettid());
  31. for (int i = 0; i < COUNT; i++) {
  32. FAIL_IF_EXIT(sem_wait(prev));
  33. FAIL_IF_EXIT(expected_dscr != get_dscr());
  34. FAIL_IF_EXIT(expected_dscr != get_dscr_usr());
  35. expected_dscr = (expected_dscr + 1) % DSCR_MAX;
  36. set_dscr(expected_dscr);
  37. FAIL_IF_EXIT(sem_post(next));
  38. }
  39. return NULL;
  40. }
  41. int dscr_explicit_lockstep_test(void)
  42. {
  43. pthread_t thread;
  44. sem_t semaphores[2];
  45. sem_t *prev = &semaphores[1]; /* reversed prev/next than for the other thread */
  46. sem_t *next = &semaphores[0];
  47. unsigned long expected_dscr = 0;
  48. SKIP_IF(!have_hwcap2(PPC_FEATURE2_DSCR));
  49. srand(gettid());
  50. set_dscr(expected_dscr);
  51. FAIL_IF(sem_init(prev, 0, 0));
  52. FAIL_IF(sem_init(next, 0, 1)); /* other thread starts first */
  53. FAIL_IF(bind_to_cpu(BIND_CPU_ANY) < 0);
  54. FAIL_IF(pthread_create(&thread, NULL, dscr_explicit_lockstep_thread, (void *)semaphores));
  55. for (int i = 0; i < COUNT; i++) {
  56. FAIL_IF(sem_wait(prev));
  57. FAIL_IF(expected_dscr != get_dscr());
  58. FAIL_IF(expected_dscr != get_dscr_usr());
  59. expected_dscr = (expected_dscr - 1) % DSCR_MAX;
  60. set_dscr(expected_dscr);
  61. FAIL_IF(sem_post(next));
  62. }
  63. FAIL_IF(pthread_join(thread, NULL));
  64. FAIL_IF(sem_destroy(prev));
  65. FAIL_IF(sem_destroy(next));
  66. return 0;
  67. }
  68. struct random_thread_args {
  69. pthread_t thread_id;
  70. bool do_yields;
  71. pthread_barrier_t *barrier;
  72. };
  73. void *dscr_explicit_random_thread(void *in)
  74. {
  75. struct random_thread_args *args = (struct random_thread_args *)in;
  76. unsigned long expected_dscr = 0;
  77. int err;
  78. srand(gettid());
  79. err = pthread_barrier_wait(args->barrier);
  80. FAIL_IF_EXIT(err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD);
  81. for (int i = 0; i < COUNT; i++) {
  82. expected_dscr = rand() % DSCR_MAX;
  83. set_dscr(expected_dscr);
  84. for (int j = rand() % 5; j > 0; --j) {
  85. FAIL_IF_EXIT(get_dscr() != expected_dscr);
  86. FAIL_IF_EXIT(get_dscr_usr() != expected_dscr);
  87. if (args->do_yields && rand() % 2)
  88. sched_yield();
  89. }
  90. expected_dscr = rand() % DSCR_MAX;
  91. set_dscr_usr(expected_dscr);
  92. for (int j = rand() % 5; j > 0; --j) {
  93. FAIL_IF_EXIT(get_dscr() != expected_dscr);
  94. FAIL_IF_EXIT(get_dscr_usr() != expected_dscr);
  95. if (args->do_yields && rand() % 2)
  96. sched_yield();
  97. }
  98. }
  99. return NULL;
  100. }
  101. int dscr_explicit_random_test(void)
  102. {
  103. struct random_thread_args threads[THREADS];
  104. pthread_barrier_t barrier;
  105. SKIP_IF(!have_hwcap2(PPC_FEATURE2_DSCR));
  106. FAIL_IF(pthread_barrier_init(&barrier, NULL, THREADS));
  107. for (int i = 0; i < THREADS; i++) {
  108. threads[i].do_yields = i % 2 == 0;
  109. threads[i].barrier = &barrier;
  110. FAIL_IF(pthread_create(&threads[i].thread_id, NULL,
  111. dscr_explicit_random_thread, (void *)&threads[i]));
  112. }
  113. for (int i = 0; i < THREADS; i++)
  114. FAIL_IF(pthread_join(threads[i].thread_id, NULL));
  115. FAIL_IF(pthread_barrier_destroy(&barrier));
  116. return 0;
  117. }
  118. int main(int argc, char *argv[])
  119. {
  120. unsigned long orig_dscr_default = 0;
  121. int err = 0;
  122. if (have_hwcap2(PPC_FEATURE2_DSCR))
  123. orig_dscr_default = get_default_dscr();
  124. err |= test_harness(dscr_explicit_lockstep_test, "dscr_explicit_lockstep_test");
  125. err |= test_harness(dscr_explicit_random_test, "dscr_explicit_random_test");
  126. if (have_hwcap2(PPC_FEATURE2_DSCR))
  127. set_default_dscr(orig_dscr_default);
  128. return err;
  129. }