WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / selftests / prctl / disable-tsc-test.c
blobf84d4ee111d37bba19fab3b077dadb2073891429
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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
6 */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <signal.h>
12 #include <inttypes.h>
15 #include <sys/prctl.h>
16 #include <linux/prctl.h>
18 /* Get/set the process' ability to use the timestamp counter instruction */
19 #ifndef PR_GET_TSC
20 #define PR_GET_TSC 25
21 #define PR_SET_TSC 26
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 */
24 #endif
26 const char *tsc_names[] =
28 [0] = "[not set]",
29 [PR_TSC_ENABLE] = "PR_TSC_ENABLE",
30 [PR_TSC_SIGSEGV] = "PR_TSC_SIGSEGV",
33 static uint64_t rdtsc(void)
35 uint32_t lo, hi;
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)
43 int tsc_val = 0;
45 printf("[ SIG_SEGV ]\n");
46 printf("prctl(PR_GET_TSC, &tsc_val); ");
47 fflush(stdout);
49 if ( prctl(PR_GET_TSC, &tsc_val) == -1)
50 perror("prctl");
52 printf("tsc_val == %s\n", tsc_names[tsc_val]);
53 printf("prctl(PR_SET_TSC, PR_TSC_ENABLE)\n");
54 fflush(stdout);
55 if ( prctl(PR_SET_TSC, PR_TSC_ENABLE) == -1)
56 perror("prctl");
58 printf("rdtsc() == ");
61 int main(void)
63 int tsc_val = 0;
65 signal(SIGSEGV, sigsegv_cb);
67 printf("rdtsc() == %llu\n", (unsigned long long)rdtsc());
68 printf("prctl(PR_GET_TSC, &tsc_val); ");
69 fflush(stdout);
71 if ( prctl(PR_GET_TSC, &tsc_val) == -1)
72 perror("prctl");
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");
77 fflush(stdout);
79 if ( prctl(PR_SET_TSC, PR_TSC_ENABLE) == -1)
80 perror("prctl");
82 printf("rdtsc() == %llu\n", (unsigned long long)rdtsc());
83 printf("prctl(PR_SET_TSC, PR_TSC_SIGSEGV)\n");
84 fflush(stdout);
86 if ( prctl(PR_SET_TSC, PR_TSC_SIGSEGV) == -1)
87 perror("prctl");
89 printf("rdtsc() == ");
90 fflush(stdout);
91 printf("%llu\n", (unsigned long long)rdtsc());
92 fflush(stdout);
94 exit(EXIT_SUCCESS);