WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / selftests / prctl / disable-tsc-ctxt-sw-stress-test.c
blob62a93cc61b7cc377be7eb749e2509d9939f09838
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 * at context switches
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 static uint64_t rdtsc(void)
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 static void sigsegv_expect(int sig)
41 /* */
44 static void segvtask(void)
46 if (prctl(PR_SET_TSC, PR_TSC_SIGSEGV) < 0)
48 perror("prctl");
49 exit(0);
51 signal(SIGSEGV, sigsegv_expect);
52 alarm(10);
53 rdtsc();
54 fprintf(stderr, "FATAL ERROR, rdtsc() succeeded while disabled\n");
55 exit(0);
59 static void sigsegv_fail(int sig)
61 fprintf(stderr, "FATAL ERROR, rdtsc() failed while enabled\n");
62 exit(0);
65 static void rdtsctask(void)
67 if (prctl(PR_SET_TSC, PR_TSC_ENABLE) < 0)
69 perror("prctl");
70 exit(0);
72 signal(SIGSEGV, sigsegv_fail);
73 alarm(10);
74 for(;;) rdtsc();
78 int main(void)
80 int n_tasks = 100, i;
82 fprintf(stderr, "[No further output means we're allright]\n");
84 for (i=0; i<n_tasks; i++)
85 if (fork() == 0)
87 if (i & 1)
88 segvtask();
89 else
90 rdtsctask();
93 for (i=0; i<n_tasks; i++)
94 wait(NULL);
96 exit(0);