Root/
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 | |
97 |
Branches:
ben-wpan
ben-wpan-stefan
javiroman/ks7010
jz-2.6.34
jz-2.6.34-rc5
jz-2.6.34-rc6
jz-2.6.34-rc7
jz-2.6.35
jz-2.6.36
jz-2.6.37
jz-2.6.38
jz-2.6.39
jz-3.0
jz-3.1
jz-3.11
jz-3.12
jz-3.13
jz-3.15
jz-3.16
jz-3.18-dt
jz-3.2
jz-3.3
jz-3.4
jz-3.5
jz-3.6
jz-3.6-rc2-pwm
jz-3.9
jz-3.9-clk
jz-3.9-rc8
jz47xx
jz47xx-2.6.38
master
Tags:
od-2011-09-04
od-2011-09-18
v2.6.34-rc5
v2.6.34-rc6
v2.6.34-rc7
v3.9