WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / selftests / prctl / disable-tsc-on-off-stress-test.c
blob79950f9a26fd660fa49ef4f75e614a620e378e81
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Tests for prctl(PR_GET_TSC, ...) / prctl(PR_SET_TSC, ...)
5 * Tests if the control register is updated correctly
6 * when set with prctl()
8 * Warning: this test will cause a very high load for a few seconds
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <signal.h>
16 #include <inttypes.h>
17 #include <wait.h>
20 #include <sys/prctl.h>
21 #include <linux/prctl.h>
23 /* Get/set the process' ability to use the timestamp counter instruction */
24 #ifndef PR_GET_TSC
25 #define PR_GET_TSC 25
26 #define PR_SET_TSC 26
27 # define PR_TSC_ENABLE 1 /* allow the use of the timestamp counter */
28 # define PR_TSC_SIGSEGV 2 /* throw a SIGSEGV instead of reading the TSC */
29 #endif
31 /* snippet from wikipedia :-) */
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 int should_segv = 0;
43 static void sigsegv_cb(int sig)
45 if (!should_segv)
47 fprintf(stderr, "FATAL ERROR, rdtsc() failed while enabled\n");
48 exit(0);
50 if (prctl(PR_SET_TSC, PR_TSC_ENABLE) < 0)
52 perror("prctl");
53 exit(0);
55 should_segv = 0;
57 rdtsc();
60 static void task(void)
62 signal(SIGSEGV, sigsegv_cb);
63 alarm(10);
64 for(;;)
66 rdtsc();
67 if (should_segv)
69 fprintf(stderr, "FATAL ERROR, rdtsc() succeeded while disabled\n");
70 exit(0);
72 if (prctl(PR_SET_TSC, PR_TSC_SIGSEGV) < 0)
74 perror("prctl");
75 exit(0);
77 should_segv = 1;
82 int main(void)
84 int n_tasks = 100, i;
86 fprintf(stderr, "[No further output means we're allright]\n");
88 for (i=0; i<n_tasks; i++)
89 if (fork() == 0)
90 task();
92 for (i=0; i<n_tasks; i++)
93 wait(NULL);
95 exit(0);