^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) * Basic test to test behaviour of PR_GET_TSC and PR_SET_TSC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <inttypes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <sys/prctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/prctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) /* Get/set the process' ability to use the timestamp counter instruction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #ifndef PR_GET_TSC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define PR_GET_TSC 25
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define PR_SET_TSC 26
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) # define PR_TSC_ENABLE 1 /* allow the use of the timestamp counter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) # define PR_TSC_SIGSEGV 2 /* throw a SIGSEGV instead of reading the TSC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) const char *tsc_names[] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) [0] = "[not set]",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) [PR_TSC_ENABLE] = "PR_TSC_ENABLE",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) [PR_TSC_SIGSEGV] = "PR_TSC_SIGSEGV",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) static uint64_t rdtsc(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) uint32_t lo, hi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /* We cannot use "=A", since this would use %rax on x86_64 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) return (uint64_t)hi << 32 | lo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static void sigsegv_cb(int sig)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) int tsc_val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) printf("[ SIG_SEGV ]\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) printf("prctl(PR_GET_TSC, &tsc_val); ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) fflush(stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if ( prctl(PR_GET_TSC, &tsc_val) == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) perror("prctl");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) printf("tsc_val == %s\n", tsc_names[tsc_val]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) printf("prctl(PR_SET_TSC, PR_TSC_ENABLE)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) fflush(stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if ( prctl(PR_SET_TSC, PR_TSC_ENABLE) == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) perror("prctl");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) printf("rdtsc() == ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) int main(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) int tsc_val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) signal(SIGSEGV, sigsegv_cb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) printf("rdtsc() == %llu\n", (unsigned long long)rdtsc());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) printf("prctl(PR_GET_TSC, &tsc_val); ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) fflush(stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if ( prctl(PR_GET_TSC, &tsc_val) == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) perror("prctl");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) printf("tsc_val == %s\n", tsc_names[tsc_val]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) printf("rdtsc() == %llu\n", (unsigned long long)rdtsc());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) printf("prctl(PR_SET_TSC, PR_TSC_ENABLE)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) fflush(stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if ( prctl(PR_SET_TSC, PR_TSC_ENABLE) == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) perror("prctl");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) printf("rdtsc() == %llu\n", (unsigned long long)rdtsc());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) printf("prctl(PR_SET_TSC, PR_TSC_SIGSEGV)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) fflush(stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if ( prctl(PR_SET_TSC, PR_TSC_SIGSEGV) == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) perror("prctl");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) printf("rdtsc() == ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) fflush(stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) printf("%llu\n", (unsigned long long)rdtsc());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) fflush(stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) exit(EXIT_SUCCESS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)