tm-vmx-unavail.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2017, Michael Neuling, IBM Corp.
  4. * Original: Breno Leitao <[email protected]> &
  5. * Gustavo Bueno Romero <[email protected]>
  6. * Edited: Michael Neuling
  7. *
  8. * Force VMX unavailable during a transaction and see if it corrupts
  9. * the checkpointed VMX register state after the abort.
  10. */
  11. #include <inttypes.h>
  12. #include <htmintrin.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <pthread.h>
  17. #include <sys/mman.h>
  18. #include <unistd.h>
  19. #include "tm.h"
  20. #include "utils.h"
  21. int passed;
  22. void *worker(void *unused)
  23. {
  24. __int128 vmx0;
  25. uint64_t texasr;
  26. asm goto (
  27. "li 3, 1;" /* Stick non-zero value in VMX0 */
  28. "std 3, 0(%[vmx0_ptr]);"
  29. "lvx 0, 0, %[vmx0_ptr];"
  30. /* Wait here a bit so we get scheduled out 255 times */
  31. "lis 3, 0x3fff;"
  32. "1: ;"
  33. "addi 3, 3, -1;"
  34. "cmpdi 3, 0;"
  35. "bne 1b;"
  36. /* Kernel will hopefully turn VMX off now */
  37. "tbegin. ;"
  38. "beq failure;"
  39. /* Cause VMX unavail. Any VMX instruction */
  40. "vaddcuw 0,0,0;"
  41. "tend. ;"
  42. "b %l[success];"
  43. /* Check VMX0 sanity after abort */
  44. "failure: ;"
  45. "lvx 1, 0, %[vmx0_ptr];"
  46. "vcmpequb. 2, 0, 1;"
  47. "bc 4, 24, %l[value_mismatch];"
  48. "b %l[value_match];"
  49. :
  50. : [vmx0_ptr] "r"(&vmx0)
  51. : "r3"
  52. : success, value_match, value_mismatch
  53. );
  54. /* HTM aborted and VMX0 is corrupted */
  55. value_mismatch:
  56. texasr = __builtin_get_texasr();
  57. printf("\n\n==============\n\n");
  58. printf("Failure with error: %lx\n", _TEXASR_FAILURE_CODE(texasr));
  59. printf("Summary error : %lx\n", _TEXASR_FAILURE_SUMMARY(texasr));
  60. printf("TFIAR exact : %lx\n\n", _TEXASR_TFIAR_EXACT(texasr));
  61. passed = 0;
  62. return NULL;
  63. /* HTM aborted but VMX0 is correct */
  64. value_match:
  65. // printf("!");
  66. return NULL;
  67. success:
  68. // printf(".");
  69. return NULL;
  70. }
  71. int tm_vmx_unavail_test()
  72. {
  73. int threads;
  74. pthread_t *thread;
  75. SKIP_IF(!have_htm());
  76. SKIP_IF(htm_is_synthetic());
  77. passed = 1;
  78. threads = sysconf(_SC_NPROCESSORS_ONLN) * 4;
  79. thread = malloc(sizeof(pthread_t)*threads);
  80. if (!thread)
  81. return EXIT_FAILURE;
  82. for (uint64_t i = 0; i < threads; i++)
  83. pthread_create(&thread[i], NULL, &worker, NULL);
  84. for (uint64_t i = 0; i < threads; i++)
  85. pthread_join(thread[i], NULL);
  86. free(thread);
  87. return passed ? EXIT_SUCCESS : EXIT_FAILURE;
  88. }
  89. int main(int argc, char **argv)
  90. {
  91. return test_harness(tm_vmx_unavail_test, "tm_vmx_unavail_test");
  92. }