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