^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) * when set with prctl()
^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) /* snippet from wikipedia :-) */
^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) int should_segv = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static void sigsegv_cb(int sig)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) if (!should_segv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) fprintf(stderr, "FATAL ERROR, rdtsc() failed while enabled\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (prctl(PR_SET_TSC, PR_TSC_ENABLE) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) perror("prctl");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) should_segv = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) rdtsc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static void task(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) signal(SIGSEGV, sigsegv_cb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) alarm(10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) for(;;)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) rdtsc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if (should_segv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) fprintf(stderr, "FATAL ERROR, rdtsc() succeeded while disabled\n");
^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) if (prctl(PR_SET_TSC, PR_TSC_SIGSEGV) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) perror("prctl");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) should_segv = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) int main(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) int n_tasks = 100, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) fprintf(stderr, "[No further output means we're allright]\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) for (i=0; i<n_tasks; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (fork() == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) task();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) for (i=0; i<n_tasks; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) wait(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)