diff options
Diffstat (limited to 'Documentation/prctl')
-rw-r--r-- | Documentation/prctl/disable-tsc-ctxt-sw-stress-test.c | 96 | ||||
-rw-r--r-- | Documentation/prctl/disable-tsc-on-off-stress-test.c | 95 | ||||
-rw-r--r-- | Documentation/prctl/disable-tsc-test.c | 94 |
3 files changed, 285 insertions, 0 deletions
diff --git a/Documentation/prctl/disable-tsc-ctxt-sw-stress-test.c b/Documentation/prctl/disable-tsc-ctxt-sw-stress-test.c new file mode 100644 index 000000000000..f8e8e95e81fd --- /dev/null +++ b/Documentation/prctl/disable-tsc-ctxt-sw-stress-test.c | |||
@@ -0,0 +1,96 @@ | |||
1 | /* | ||
2 | * Tests for prctl(PR_GET_TSC, ...) / prctl(PR_SET_TSC, ...) | ||
3 | * | ||
4 | * Tests if the control register is updated correctly | ||
5 | * at context switches | ||
6 | * | ||
7 | * Warning: this test will cause a very high load for a few seconds | ||
8 | * | ||
9 | */ | ||
10 | |||
11 | #include <stdio.h> | ||
12 | #include <stdlib.h> | ||
13 | #include <unistd.h> | ||
14 | #include <signal.h> | ||
15 | #include <inttypes.h> | ||
16 | #include <wait.h> | ||
17 | |||
18 | |||
19 | #include <sys/prctl.h> | ||
20 | #include <linux/prctl.h> | ||
21 | |||
22 | /* Get/set the process' ability to use the timestamp counter instruction */ | ||
23 | #ifndef PR_GET_TSC | ||
24 | #define PR_GET_TSC 25 | ||
25 | #define PR_SET_TSC 26 | ||
26 | # define PR_TSC_ENABLE 1 /* allow the use of the timestamp counter */ | ||
27 | # define PR_TSC_SIGSEGV 2 /* throw a SIGSEGV instead of reading the TSC */ | ||
28 | #endif | ||
29 | |||
30 | uint64_t rdtsc() { | ||
31 | uint32_t lo, hi; | ||
32 | /* We cannot use "=A", since this would use %rax on x86_64 */ | ||
33 | __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi)); | ||
34 | return (uint64_t)hi << 32 | lo; | ||
35 | } | ||
36 | |||
37 | void sigsegv_expect(int sig) | ||
38 | { | ||
39 | /* */ | ||
40 | } | ||
41 | |||
42 | void segvtask(void) | ||
43 | { | ||
44 | if (prctl(PR_SET_TSC, PR_TSC_SIGSEGV) < 0) | ||
45 | { | ||
46 | perror("prctl"); | ||
47 | exit(0); | ||
48 | } | ||
49 | signal(SIGSEGV, sigsegv_expect); | ||
50 | alarm(10); | ||
51 | rdtsc(); | ||
52 | fprintf(stderr, "FATAL ERROR, rdtsc() succeeded while disabled\n"); | ||
53 | exit(0); | ||
54 | } | ||
55 | |||
56 | |||
57 | void sigsegv_fail(int sig) | ||
58 | { | ||
59 | fprintf(stderr, "FATAL ERROR, rdtsc() failed while enabled\n"); | ||
60 | exit(0); | ||
61 | } | ||
62 | |||
63 | void rdtsctask(void) | ||
64 | { | ||
65 | if (prctl(PR_SET_TSC, PR_TSC_ENABLE) < 0) | ||
66 | { | ||
67 | perror("prctl"); | ||
68 | exit(0); | ||
69 | } | ||
70 | signal(SIGSEGV, sigsegv_fail); | ||
71 | alarm(10); | ||
72 | for(;;) rdtsc(); | ||
73 | } | ||
74 | |||
75 | |||
76 | int main(int argc, char **argv) | ||
77 | { | ||
78 | int n_tasks = 100, i; | ||
79 | |||
80 | fprintf(stderr, "[No further output means we're allright]\n"); | ||
81 | |||
82 | for (i=0; i<n_tasks; i++) | ||
83 | if (fork() == 0) | ||
84 | { | ||
85 | if (i & 1) | ||
86 | segvtask(); | ||
87 | else | ||
88 | rdtsctask(); | ||
89 | } | ||
90 | |||
91 | for (i=0; i<n_tasks; i++) | ||
92 | wait(NULL); | ||
93 | |||
94 | exit(0); | ||
95 | } | ||
96 | |||
diff --git a/Documentation/prctl/disable-tsc-on-off-stress-test.c b/Documentation/prctl/disable-tsc-on-off-stress-test.c new file mode 100644 index 000000000000..1fcd91445375 --- /dev/null +++ b/Documentation/prctl/disable-tsc-on-off-stress-test.c | |||
@@ -0,0 +1,95 @@ | |||
1 | /* | ||
2 | * Tests for prctl(PR_GET_TSC, ...) / prctl(PR_SET_TSC, ...) | ||
3 | * | ||
4 | * Tests if the control register is updated correctly | ||
5 | * when set with prctl() | ||
6 | * | ||
7 | * Warning: this test will cause a very high load for a few seconds | ||
8 | * | ||
9 | */ | ||
10 | |||
11 | #include <stdio.h> | ||
12 | #include <stdlib.h> | ||
13 | #include <unistd.h> | ||
14 | #include <signal.h> | ||
15 | #include <inttypes.h> | ||
16 | #include <wait.h> | ||
17 | |||
18 | |||
19 | #include <sys/prctl.h> | ||
20 | #include <linux/prctl.h> | ||
21 | |||
22 | /* Get/set the process' ability to use the timestamp counter instruction */ | ||
23 | #ifndef PR_GET_TSC | ||
24 | #define PR_GET_TSC 25 | ||
25 | #define PR_SET_TSC 26 | ||
26 | # define PR_TSC_ENABLE 1 /* allow the use of the timestamp counter */ | ||
27 | # define PR_TSC_SIGSEGV 2 /* throw a SIGSEGV instead of reading the TSC */ | ||
28 | #endif | ||
29 | |||
30 | /* snippet from wikipedia :-) */ | ||
31 | |||
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; | ||
37 | } | ||
38 | |||
39 | int should_segv = 0; | ||
40 | |||
41 | void sigsegv_cb(int sig) | ||
42 | { | ||
43 | if (!should_segv) | ||
44 | { | ||
45 | fprintf(stderr, "FATAL ERROR, rdtsc() failed while enabled\n"); | ||
46 | exit(0); | ||
47 | } | ||
48 | if (prctl(PR_SET_TSC, PR_TSC_ENABLE) < 0) | ||
49 | { | ||
50 | perror("prctl"); | ||
51 | exit(0); | ||
52 | } | ||
53 | should_segv = 0; | ||
54 | |||
55 | rdtsc(); | ||
56 | } | ||
57 | |||
58 | void task(void) | ||
59 | { | ||
60 | signal(SIGSEGV, sigsegv_cb); | ||
61 | alarm(10); | ||
62 | for(;;) | ||
63 | { | ||
64 | rdtsc(); | ||
65 | if (should_segv) | ||
66 | { | ||
67 | fprintf(stderr, "FATAL ERROR, rdtsc() succeeded while disabled\n"); | ||
68 | exit(0); | ||
69 | } | ||
70 | if (prctl(PR_SET_TSC, PR_TSC_SIGSEGV) < 0) | ||
71 | { | ||
72 | perror("prctl"); | ||
73 | exit(0); | ||
74 | } | ||
75 | should_segv = 1; | ||
76 | } | ||
77 | } | ||
78 | |||
79 | |||
80 | int main(int argc, char **argv) | ||
81 | { | ||
82 | int n_tasks = 100, i; | ||
83 | |||
84 | fprintf(stderr, "[No further output means we're allright]\n"); | ||
85 | |||
86 | for (i=0; i<n_tasks; i++) | ||
87 | if (fork() == 0) | ||
88 | task(); | ||
89 | |||
90 | for (i=0; i<n_tasks; i++) | ||
91 | wait(NULL); | ||
92 | |||
93 | exit(0); | ||
94 | } | ||
95 | |||
diff --git a/Documentation/prctl/disable-tsc-test.c b/Documentation/prctl/disable-tsc-test.c new file mode 100644 index 000000000000..843c81eac235 --- /dev/null +++ b/Documentation/prctl/disable-tsc-test.c | |||
@@ -0,0 +1,94 @@ | |||
1 | /* | ||
2 | * Tests for prctl(PR_GET_TSC, ...) / prctl(PR_SET_TSC, ...) | ||
3 | * | ||
4 | * Basic test to test behaviour of PR_GET_TSC and PR_SET_TSC | ||
5 | */ | ||
6 | |||
7 | #include <stdio.h> | ||
8 | #include <stdlib.h> | ||
9 | #include <unistd.h> | ||
10 | #include <signal.h> | ||
11 | #include <inttypes.h> | ||
12 | |||
13 | |||
14 | #include <sys/prctl.h> | ||
15 | #include <linux/prctl.h> | ||
16 | |||
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 | ||
24 | |||
25 | const char *tsc_names[] = | ||
26 | { | ||
27 | [0] = "[not set]", | ||
28 | [PR_TSC_ENABLE] = "PR_TSC_ENABLE", | ||
29 | [PR_TSC_SIGSEGV] = "PR_TSC_SIGSEGV", | ||
30 | }; | ||
31 | |||
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; | ||
37 | } | ||
38 | |||
39 | void sigsegv_cb(int sig) | ||
40 | { | ||
41 | int tsc_val = 0; | ||
42 | |||
43 | printf("[ SIG_SEGV ]\n"); | ||
44 | printf("prctl(PR_GET_TSC, &tsc_val); "); | ||
45 | fflush(stdout); | ||
46 | |||
47 | if ( prctl(PR_GET_TSC, &tsc_val) == -1) | ||
48 | perror("prctl"); | ||
49 | |||
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"); | ||
55 | |||
56 | printf("rdtsc() == "); | ||
57 | } | ||
58 | |||
59 | int main(int argc, char **argv) | ||
60 | { | ||
61 | int tsc_val = 0; | ||
62 | |||
63 | signal(SIGSEGV, sigsegv_cb); | ||
64 | |||
65 | printf("rdtsc() == %llu\n", (unsigned long long)rdtsc()); | ||
66 | printf("prctl(PR_GET_TSC, &tsc_val); "); | ||
67 | fflush(stdout); | ||
68 | |||
69 | if ( prctl(PR_GET_TSC, &tsc_val) == -1) | ||
70 | perror("prctl"); | ||
71 | |||
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); | ||
76 | |||
77 | if ( prctl(PR_SET_TSC, PR_TSC_ENABLE) == -1) | ||
78 | perror("prctl"); | ||
79 | |||
80 | printf("rdtsc() == %llu\n", (unsigned long long)rdtsc()); | ||
81 | printf("prctl(PR_SET_TSC, PR_TSC_SIGSEGV)\n"); | ||
82 | fflush(stdout); | ||
83 | |||
84 | if ( prctl(PR_SET_TSC, PR_TSC_SIGSEGV) == -1) | ||
85 | perror("prctl"); | ||
86 | |||
87 | printf("rdtsc() == "); | ||
88 | fflush(stdout); | ||
89 | printf("%llu\n", (unsigned long long)rdtsc()); | ||
90 | fflush(stdout); | ||
91 | |||
92 | exit(EXIT_SUCCESS); | ||
93 | } | ||
94 | |||