1 // SPDX-License-Identifier: GPL-2.0
3 * Tests for prctl(PR_GET_TSC, ...) / prctl(PR_SET_TSC, ...)
5 * Tests if the control register is updated correctly
8 * Warning: this test will cause a very high load for a few seconds
20 #include <sys/prctl.h>
21 #include <linux/prctl.h>
23 /* Get/set the process' ability to use the timestamp counter instruction */
27 # define PR_TSC_ENABLE 1 /* allow the use of the timestamp counter */
28 # define PR_TSC_SIGSEGV 2 /* throw a SIGSEGV instead of reading the TSC */
31 static uint64_t rdtsc(void)
34 /* We cannot use "=A", since this would use %rax on x86_64 */
35 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
36 return (uint64_t)hi << 32 | lo;
39 static void sigsegv_expect(int sig)
44 static void segvtask(void)
46 if (prctl(PR_SET_TSC, PR_TSC_SIGSEGV) < 0)
51 signal(SIGSEGV, sigsegv_expect);
54 fprintf(stderr, "FATAL ERROR, rdtsc() succeeded while disabled\n");
59 static void sigsegv_fail(int sig)
61 fprintf(stderr, "FATAL ERROR, rdtsc() failed while enabled\n");
65 static void rdtsctask(void)
67 if (prctl(PR_SET_TSC, PR_TSC_ENABLE) < 0)
72 signal(SIGSEGV, sigsegv_fail);
82 fprintf(stderr, "[No further output means we're all right]\n");
84 for (i=0; i<n_tasks; i++)
93 for (i=0; i<n_tasks; i++)