Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright 2015, Michael Neuling, IBM Corp.
0004  *
0005  * Original: Michael Neuling 3/4/2014
0006  * Modified: Rashmica Gupta 8/12/2015
0007  *
0008  * Check if any of the Transaction Memory SPRs get corrupted.
0009  * - TFIAR  - stores address of location of transaction failure
0010  * - TFHAR  - stores address of software failure handler (if transaction
0011  *   fails)
0012  * - TEXASR - lots of info about the transacion(s)
0013  *
0014  * (1) create more threads than cpus
0015  * (2) in each thread:
0016  *  (a) set TFIAR and TFHAR a unique value
0017  *  (b) loop for awhile, continually checking to see if
0018  *  either register has been corrupted.
0019  *
0020  * (3) Loop:
0021  *  (a) begin transaction
0022  *      (b) abort transaction
0023  *  (c) check TEXASR to see if FS has been corrupted
0024  */
0025 
0026 #define _GNU_SOURCE
0027 #include <stdio.h>
0028 #include <stdlib.h>
0029 #include <unistd.h>
0030 #include <pthread.h>
0031 #include <string.h>
0032 
0033 #include "utils.h"
0034 #include "tm.h"
0035 
0036 int num_loops   = 1000000;
0037 int passed = 1;
0038 
0039 void tfiar_tfhar(void *in)
0040 {
0041     unsigned long tfhar, tfhar_rd, tfiar, tfiar_rd;
0042     int i;
0043 
0044     /* TFIAR: Last bit has to be high so userspace can read register */
0045     tfiar = ((unsigned long)in) + 1;
0046     tfiar += 2;
0047     mtspr(SPRN_TFIAR, tfiar);
0048 
0049     /* TFHAR: Last two bits are reserved */
0050     tfhar = ((unsigned long)in);
0051     tfhar &= ~0x3UL;
0052     tfhar += 4;
0053     mtspr(SPRN_TFHAR, tfhar);
0054 
0055     for (i = 0; i < num_loops; i++) {
0056         tfhar_rd = mfspr(SPRN_TFHAR);
0057         tfiar_rd = mfspr(SPRN_TFIAR);
0058         if ( (tfhar != tfhar_rd) || (tfiar != tfiar_rd) ) {
0059             passed = 0;
0060             return;
0061         }
0062     }
0063     return;
0064 }
0065 
0066 void texasr(void *in)
0067 {
0068     unsigned long i;
0069     uint64_t result = 0;
0070 
0071     for (i = 0; i < num_loops; i++) {
0072         asm __volatile__(
0073             "tbegin.;"
0074             "beq    3f ;"
0075             "tabort. 0 ;"
0076             "tend.;"
0077 
0078             /* Abort handler */
0079             "3: ;"
0080             ::: "memory");
0081 
0082                 /* Check the TEXASR */
0083                 result = mfspr(SPRN_TEXASR);
0084         if ((result & TEXASR_FS) == 0) {
0085             passed = 0;
0086             return;
0087         }
0088     }
0089     return;
0090 }
0091 
0092 int test_tmspr()
0093 {
0094     pthread_t   *thread;
0095     int     thread_num;
0096     unsigned long   i;
0097 
0098     SKIP_IF(!have_htm());
0099     SKIP_IF(htm_is_synthetic());
0100 
0101     /* To cause some context switching */
0102     thread_num = 10 * sysconf(_SC_NPROCESSORS_ONLN);
0103 
0104     thread = malloc(thread_num * sizeof(pthread_t));
0105     if (thread == NULL)
0106         return EXIT_FAILURE;
0107 
0108     /* Test TFIAR and TFHAR */
0109     for (i = 0; i < thread_num; i += 2) {
0110         if (pthread_create(&thread[i], NULL, (void *)tfiar_tfhar,
0111                    (void *)i))
0112             return EXIT_FAILURE;
0113     }
0114     /* Test TEXASR */
0115     for (i = 1; i < thread_num; i += 2) {
0116         if (pthread_create(&thread[i], NULL, (void *)texasr, (void *)i))
0117             return EXIT_FAILURE;
0118     }
0119 
0120     for (i = 0; i < thread_num; i++) {
0121         if (pthread_join(thread[i], NULL) != 0)
0122             return EXIT_FAILURE;
0123     }
0124 
0125     free(thread);
0126 
0127     if (passed)
0128         return 0;
0129     else
0130         return 1;
0131 }
0132 
0133 int main(int argc, char *argv[])
0134 {
0135     if (argc > 1) {
0136         if (strcmp(argv[1], "-h") == 0) {
0137             printf("Syntax:\t [<num loops>]\n");
0138             return 0;
0139         } else {
0140             num_loops = atoi(argv[1]);
0141         }
0142     }
0143     return test_harness(test_tmspr, "tm_tmspr");
0144 }