tm-vmx-unavail.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright 2017, Michael Neuling, IBM Corp.
  3. * Licensed under GPLv2.
  4. * Original: Breno Leitao <brenohl@br.ibm.com> &
  5. * Gustavo Bueno Romero <gromero@br.ibm.com>
  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 <pthread.h>
  20. #include "tm.h"
  21. #include "utils.h"
  22. int passed;
  23. void *worker(void *unused)
  24. {
  25. __int128 vmx0;
  26. uint64_t texasr;
  27. asm goto (
  28. "li 3, 1;" /* Stick non-zero value in VMX0 */
  29. "std 3, 0(%[vmx0_ptr]);"
  30. "lvx 0, 0, %[vmx0_ptr];"
  31. /* Wait here a bit so we get scheduled out 255 times */
  32. "lis 3, 0x3fff;"
  33. "1: ;"
  34. "addi 3, 3, -1;"
  35. "cmpdi 3, 0;"
  36. "bne 1b;"
  37. /* Kernel will hopefully turn VMX off now */
  38. "tbegin. ;"
  39. "beq failure;"
  40. /* Cause VMX unavail. Any VMX instruction */
  41. "vaddcuw 0,0,0;"
  42. "tend. ;"
  43. "b %l[success];"
  44. /* Check VMX0 sanity after abort */
  45. "failure: ;"
  46. "lvx 1, 0, %[vmx0_ptr];"
  47. "vcmpequb. 2, 0, 1;"
  48. "bc 4, 24, %l[value_mismatch];"
  49. "b %l[value_match];"
  50. :
  51. : [vmx0_ptr] "r"(&vmx0)
  52. : "r3"
  53. : success, value_match, value_mismatch
  54. );
  55. /* HTM aborted and VMX0 is corrupted */
  56. value_mismatch:
  57. texasr = __builtin_get_texasr();
  58. printf("\n\n==============\n\n");
  59. printf("Failure with error: %lx\n", _TEXASR_FAILURE_CODE(texasr));
  60. printf("Summary error : %lx\n", _TEXASR_FAILURE_SUMMARY(texasr));
  61. printf("TFIAR exact : %lx\n\n", _TEXASR_TFIAR_EXACT(texasr));
  62. passed = 0;
  63. return NULL;
  64. /* HTM aborted but VMX0 is correct */
  65. value_match:
  66. // printf("!");
  67. return NULL;
  68. success:
  69. // printf(".");
  70. return NULL;
  71. }
  72. int tm_vmx_unavail_test()
  73. {
  74. int threads;
  75. pthread_t *thread;
  76. SKIP_IF(!have_htm());
  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. }