1 // SPDX-License-Identifier: GPL-2.0
3 * Tests for prctl(PR_GET_TSC, ...) / prctl(PR_SET_TSC, ...)
5 * Basic test to test behaviour of PR_GET_TSC and PR_SET_TSC
15 #include <sys/prctl.h>
16 #include <linux/prctl.h>
18 /* Get/set the process' ability to use the timestamp counter instruction */
22 # define PR_TSC_ENABLE 1 /* allow the use of the timestamp counter */
23 # define PR_TSC_SIGSEGV 2 /* throw a SIGSEGV instead of reading the TSC */
26 const char *tsc_names[] =
29 [PR_TSC_ENABLE] = "PR_TSC_ENABLE",
30 [PR_TSC_SIGSEGV] = "PR_TSC_SIGSEGV",
33 static uint64_t rdtsc(void)
36 /* We cannot use "=A", since this would use %rax on x86_64 */
37 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
38 return (uint64_t)hi << 32 | lo;
41 static void sigsegv_cb(int sig)
45 printf("[ SIG_SEGV ]\n");
46 printf("prctl(PR_GET_TSC, &tsc_val); ");
49 if ( prctl(PR_GET_TSC, &tsc_val) == -1)
52 printf("tsc_val == %s\n", tsc_names[tsc_val]);
53 printf("prctl(PR_SET_TSC, PR_TSC_ENABLE)\n");
55 if ( prctl(PR_SET_TSC, PR_TSC_ENABLE) == -1)
58 printf("rdtsc() == ");
65 signal(SIGSEGV, sigsegv_cb);
67 printf("rdtsc() == %llu\n", (unsigned long long)rdtsc());
68 printf("prctl(PR_GET_TSC, &tsc_val); ");
71 if ( prctl(PR_GET_TSC, &tsc_val) == -1)
74 printf("tsc_val == %s\n", tsc_names[tsc_val]);
75 printf("rdtsc() == %llu\n", (unsigned long long)rdtsc());
76 printf("prctl(PR_SET_TSC, PR_TSC_ENABLE)\n");
79 if ( prctl(PR_SET_TSC, PR_TSC_ENABLE) == -1)
82 printf("rdtsc() == %llu\n", (unsigned long long)rdtsc());
83 printf("prctl(PR_SET_TSC, PR_TSC_SIGSEGV)\n");
86 if ( prctl(PR_SET_TSC, PR_TSC_SIGSEGV) == -1)
89 printf("rdtsc() == ");
91 printf("%llu\n", (unsigned long long)rdtsc());