^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Tests for prctl(PR_GET_TSC, ...) / prctl(PR_SET_TSC, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Tests if the control register is updated correctly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * at context switches
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Warning: this test will cause a very high load for a few seconds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <inttypes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <sys/prctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/prctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) /* Get/set the process' ability to use the timestamp counter instruction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #ifndef PR_GET_TSC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define PR_GET_TSC 25
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define PR_SET_TSC 26
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) # define PR_TSC_ENABLE 1 /* allow the use of the timestamp counter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) # define PR_TSC_SIGSEGV 2 /* throw a SIGSEGV instead of reading the TSC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static uint64_t rdtsc(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) uint32_t lo, hi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) /* We cannot use "=A", since this would use %rax on x86_64 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) return (uint64_t)hi << 32 | lo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) static void sigsegv_expect(int sig)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) /* */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static void segvtask(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (prctl(PR_SET_TSC, PR_TSC_SIGSEGV) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) perror("prctl");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) signal(SIGSEGV, sigsegv_expect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) alarm(10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) rdtsc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) fprintf(stderr, "FATAL ERROR, rdtsc() succeeded while disabled\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static void sigsegv_fail(int sig)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) fprintf(stderr, "FATAL ERROR, rdtsc() failed while enabled\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static void rdtsctask(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if (prctl(PR_SET_TSC, PR_TSC_ENABLE) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) perror("prctl");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) signal(SIGSEGV, sigsegv_fail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) alarm(10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) for(;;) rdtsc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) int main(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) int n_tasks = 100, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) fprintf(stderr, "[No further output means we're allright]\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) for (i=0; i<n_tasks; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (fork() == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (i & 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) segvtask();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) rdtsctask();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) for (i=0; i<n_tasks; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) wait(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)