tm-vmxcopy.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2015, Michael Neuling, IBM Corp.
  4. *
  5. * Original: Michael Neuling 4/12/2013
  6. * Edited: Rashmica Gupta 4/12/2015
  7. *
  8. * See if the altivec state is leaked out of an aborted transaction due to
  9. * kernel vmx copy loops.
  10. *
  11. * When the transaction aborts, VSR values should rollback to the values
  12. * they held before the transaction commenced. Using VSRs while transaction
  13. * is suspended should not affect the checkpointed values.
  14. *
  15. * (1) write A to a VSR
  16. * (2) start transaction
  17. * (3) suspend transaction
  18. * (4) change the VSR to B
  19. * (5) trigger kernel vmx copy loop
  20. * (6) abort transaction
  21. * (7) check that the VSR value is A
  22. */
  23. #include <inttypes.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <unistd.h>
  27. #include <sys/mman.h>
  28. #include <string.h>
  29. #include <assert.h>
  30. #include "tm.h"
  31. #include "utils.h"
  32. int test_vmxcopy()
  33. {
  34. long double vecin = 1.3;
  35. long double vecout;
  36. unsigned long pgsize = getpagesize();
  37. int i;
  38. int fd;
  39. int size = pgsize*16;
  40. char tmpfile[] = "/tmp/page_faultXXXXXX";
  41. char buf[pgsize];
  42. char *a;
  43. uint64_t aborted = 0;
  44. SKIP_IF(!have_htm());
  45. SKIP_IF(htm_is_synthetic());
  46. SKIP_IF(!is_ppc64le());
  47. fd = mkstemp(tmpfile);
  48. assert(fd >= 0);
  49. memset(buf, 0, pgsize);
  50. for (i = 0; i < size; i += pgsize)
  51. assert(write(fd, buf, pgsize) == pgsize);
  52. unlink(tmpfile);
  53. a = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
  54. assert(a != MAP_FAILED);
  55. asm __volatile__(
  56. "lxvd2x 40,0,%[vecinptr];" /* set 40 to initial value*/
  57. "tbegin.;"
  58. "beq 3f;"
  59. "tsuspend.;"
  60. "xxlxor 40,40,40;" /* set 40 to 0 */
  61. "std 5, 0(%[map]);" /* cause kernel vmx copy page */
  62. "tabort. 0;"
  63. "tresume.;"
  64. "tend.;"
  65. "li %[res], 0;"
  66. "b 5f;"
  67. /* Abort handler */
  68. "3:;"
  69. "li %[res], 1;"
  70. "5:;"
  71. "stxvd2x 40,0,%[vecoutptr];"
  72. : [res]"=&r"(aborted)
  73. : [vecinptr]"r"(&vecin),
  74. [vecoutptr]"r"(&vecout),
  75. [map]"r"(a)
  76. : "memory", "r0", "r3", "r4", "r5", "r6", "r7");
  77. if (aborted && (vecin != vecout)){
  78. printf("FAILED: vector state leaked on abort %f != %f\n",
  79. (double)vecin, (double)vecout);
  80. return 1;
  81. }
  82. munmap(a, size);
  83. close(fd);
  84. return 0;
  85. }
  86. int main(void)
  87. {
  88. return test_harness(test_vmxcopy, "tm_vmxcopy");
  89. }