~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/Documentation/prctl/disable-tsc-test.c

Version: ~ [ linux-5.2-rc4 ] ~ [ linux-5.1.9 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.50 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.125 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.181 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.181 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.68 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp