1
2
3
4
5
6
7
8
9
10
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <unistd.h>
15#include <signal.h>
16#include <inttypes.h>
17#include <wait.h>
18
19
20#include <sys/prctl.h>
21#include <linux/prctl.h>
22
23
24#ifndef PR_GET_TSC
25#define PR_GET_TSC 25
26#define PR_SET_TSC 26
27# define PR_TSC_ENABLE 1
28# define PR_TSC_SIGSEGV 2
29#endif
30
31
32
33static uint64_t rdtsc(void)
34{
35uint32_t lo, hi;
36
37__asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
38return (uint64_t)hi << 32 | lo;
39}
40
41int should_segv = 0;
42
43static void sigsegv_cb(int sig)
44{
45 if (!should_segv)
46 {
47 fprintf(stderr, "FATAL ERROR, rdtsc() failed while enabled\n");
48 exit(0);
49 }
50 if (prctl(PR_SET_TSC, PR_TSC_ENABLE) < 0)
51 {
52 perror("prctl");
53 exit(0);
54 }
55 should_segv = 0;
56
57 rdtsc();
58}
59
60static void task(void)
61{
62 signal(SIGSEGV, sigsegv_cb);
63 alarm(10);
64 for(;;)
65 {
66 rdtsc();
67 if (should_segv)
68 {
69 fprintf(stderr, "FATAL ERROR, rdtsc() succeeded while disabled\n");
70 exit(0);
71 }
72 if (prctl(PR_SET_TSC, PR_TSC_SIGSEGV) < 0)
73 {
74 perror("prctl");
75 exit(0);
76 }
77 should_segv = 1;
78 }
79}
80
81
82int main(void)
83{
84 int n_tasks = 100, i;
85
86 fprintf(stderr, "[No further output means we're allright]\n");
87
88 for (i=0; i<n_tasks; i++)
89 if (fork() == 0)
90 task();
91
92 for (i=0; i<n_tasks; i++)
93 wait(NULL);
94
95 exit(0);
96}
97
98