2

I'm trying to compile the following TSX sample code on an Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz that supports TSX using gcc tsc.x -mrtm:

#include <immintrin.h>
#include <stdio.h>

int max_tries=100;
unsigned status = _XABORT_EXPLICIT;


int main(){

    for (int n_tries = 0; n_tries < max_tries; n_tries++) 
    {
        status = _xbegin();
        if (status == _XBEGIN_STARTED || !(status & _XABORT_RETRY))
        {
                break;
        }

        if(status == _XBEGIN_STARTED) 
        {
                printf("TSX started\n");
                _xend ();
        } 
        else 
        {
            printf("TSX failed\n");
        }

    }


return 0;
}

But the output would either result into "Illegal instruction (core dumped)" or simply no output and in rare cases TSX failed. But never TSX started.

Gua
  • 33
  • 5
  • Does your OS enable (or not disable) TSX? Most do disable it because of possible security problems with side-channels and stuff like that, especially TAA (TSX Asynchronous Abort, CVE-2019-11135): https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/tsx_async_abort.html Your program tries to use TSX features without checking CPUID to see if they're available. – Peter Cordes Nov 08 '22 at 11:20
  • [Assembler xbegin raise Illegal instruction](https://stackoverflow.com/a/59209610) says CPUID may actually still show it enabled even though it's disabled. – Peter Cordes Nov 08 '22 at 11:24
  • Assuming you're using Linux, does `dmesg | grep -i tsx` say something like `TAA: Mitigation: TSX disabled`? If so, yeah SIGILL is probably to be expected. (I've tried booting with `tsx_async_abort=off tsx=on` but 6.0.7-arch1-1 Linux says it doesn't recognize the `tsx=on` part, and disables TSX anyway. :/ IDK if an Arch Linux patch took out support for that option which kernel.org documents, but Arch generally doesn't do a lot of patching vs. upstream) – Peter Cordes Nov 08 '22 at 12:09
  • No, TSX is definitely on. – Gua Nov 08 '22 at 12:30
  • @PeterCordes Is TSX suppose to support printf()? – Gua Nov 11 '22 at 03:10
  • Generally no, and definitely not if it flushes the IO buffer requiring a system call! Then you'll always get a transaction abort. – Peter Cordes Nov 11 '22 at 03:13

0 Answers0