2 * Tests for prctl(PR_GET_TSC, ...) / prctl(PR_SET_TSC, ...)
4 * Basic test to test behaviour of PR_GET_TSC and PR_SET_TSC
14 #include <sys/prctl.h>
15 #include <linux/prctl.h>
17 /* Get/set the process' ability to use the timestamp counter instruction */
21 # define PR_TSC_ENABLE 1 /* allow the use of the timestamp counter */
22 # define PR_TSC_SIGSEGV 2 /* throw a SIGSEGV instead of reading the TSC */
25 const char *tsc_names
[] =
28 [PR_TSC_ENABLE
] = "PR_TSC_ENABLE",
29 [PR_TSC_SIGSEGV
] = "PR_TSC_SIGSEGV",
32 static uint64_t rdtsc(void)
35 /* We cannot use "=A", since this would use %rax on x86_64 */
36 __asm__
__volatile__ ("rdtsc" : "=a" (lo
), "=d" (hi
));
37 return (uint64_t)hi
<< 32 | lo
;
40 static void sigsegv_cb(int sig
)
44 printf("[ SIG_SEGV ]\n");
45 printf("prctl(PR_GET_TSC, &tsc_val); ");
48 if ( prctl(PR_GET_TSC
, &tsc_val
) == -1)
51 printf("tsc_val == %s\n", tsc_names
[tsc_val
]);
52 printf("prctl(PR_SET_TSC, PR_TSC_ENABLE)\n");
54 if ( prctl(PR_SET_TSC
, PR_TSC_ENABLE
) == -1)
57 printf("rdtsc() == ");
60 int main(int argc
, char **argv
)
64 signal(SIGSEGV
, sigsegv_cb
);
66 printf("rdtsc() == %llu\n", (unsigned long long)rdtsc());
67 printf("prctl(PR_GET_TSC, &tsc_val); ");
70 if ( prctl(PR_GET_TSC
, &tsc_val
) == -1)
73 printf("tsc_val == %s\n", tsc_names
[tsc_val
]);
74 printf("rdtsc() == %llu\n", (unsigned long long)rdtsc());
75 printf("prctl(PR_SET_TSC, PR_TSC_ENABLE)\n");
78 if ( prctl(PR_SET_TSC
, PR_TSC_ENABLE
) == -1)
81 printf("rdtsc() == %llu\n", (unsigned long long)rdtsc());
82 printf("prctl(PR_SET_TSC, PR_TSC_SIGSEGV)\n");
85 if ( prctl(PR_SET_TSC
, PR_TSC_SIGSEGV
) == -1)
88 printf("rdtsc() == ");
90 printf("%llu\n", (unsigned long long)rdtsc());