Merge branch 'mini2440-dev-unlikely' into mini2440-dev
[linux-2.6/mini2440.git] / Documentation / prctl / disable-tsc-test.c
blob843c81eac2356d1b9d0ef6ee4aca542c4822e2b2
1 /*
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
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <signal.h>
11 #include <inttypes.h>
14 #include <sys/prctl.h>
15 #include <linux/prctl.h>
17 /* Get/set the process' ability to use the timestamp counter instruction */
18 #ifndef PR_GET_TSC
19 #define PR_GET_TSC 25
20 #define PR_SET_TSC 26
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 */
23 #endif
25 const char *tsc_names[] =
27 [0] = "[not set]",
28 [PR_TSC_ENABLE] = "PR_TSC_ENABLE",
29 [PR_TSC_SIGSEGV] = "PR_TSC_SIGSEGV",
32 uint64_t rdtsc() {
33 uint32_t lo, hi;
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 void sigsegv_cb(int sig)
41 int tsc_val = 0;
43 printf("[ SIG_SEGV ]\n");
44 printf("prctl(PR_GET_TSC, &tsc_val); ");
45 fflush(stdout);
47 if ( prctl(PR_GET_TSC, &tsc_val) == -1)
48 perror("prctl");
50 printf("tsc_val == %s\n", tsc_names[tsc_val]);
51 printf("prctl(PR_SET_TSC, PR_TSC_ENABLE)\n");
52 fflush(stdout);
53 if ( prctl(PR_SET_TSC, PR_TSC_ENABLE) == -1)
54 perror("prctl");
56 printf("rdtsc() == ");
59 int main(int argc, char **argv)
61 int tsc_val = 0;
63 signal(SIGSEGV, sigsegv_cb);
65 printf("rdtsc() == %llu\n", (unsigned long long)rdtsc());
66 printf("prctl(PR_GET_TSC, &tsc_val); ");
67 fflush(stdout);
69 if ( prctl(PR_GET_TSC, &tsc_val) == -1)
70 perror("prctl");
72 printf("tsc_val == %s\n", tsc_names[tsc_val]);
73 printf("rdtsc() == %llu\n", (unsigned long long)rdtsc());
74 printf("prctl(PR_SET_TSC, PR_TSC_ENABLE)\n");
75 fflush(stdout);
77 if ( prctl(PR_SET_TSC, PR_TSC_ENABLE) == -1)
78 perror("prctl");
80 printf("rdtsc() == %llu\n", (unsigned long long)rdtsc());
81 printf("prctl(PR_SET_TSC, PR_TSC_SIGSEGV)\n");
82 fflush(stdout);
84 if ( prctl(PR_SET_TSC, PR_TSC_SIGSEGV) == -1)
85 perror("prctl");
87 printf("rdtsc() == ");
88 fflush(stdout);
89 printf("%llu\n", (unsigned long long)rdtsc());
90 fflush(stdout);
92 exit(EXIT_SUCCESS);