linux/Documentation/prctl/disable-tsc-test.c
<<
>>
Prefs
   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
  25const char *tsc_names[] =
  26{
  27        [0] = "[not set]",
  28        [PR_TSC_ENABLE] = "PR_TSC_ENABLE",
  29        [PR_TSC_SIGSEGV] = "PR_TSC_SIGSEGV",
  30};
  31
  32uint64_t rdtsc() {
  33uint32_t lo, hi;
  34/* We cannot use "=A", since this would use %rax on x86_64 */
  35__asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
  36return (uint64_t)hi << 32 | lo;
  37}
  38
  39void sigsegv_cb(int sig)
  40{
  41        int tsc_val = 0;
  42
  43        printf("[ SIG_SEGV ]\n");
  44        printf("prctl(PR_GET_TSC, &tsc_val); ");
  45        fflush(stdout);
  46
  47        if ( prctl(PR_GET_TSC, &tsc_val) == -1)
  48                perror("prctl");
  49
  50        printf("tsc_val == %s\n", tsc_names[tsc_val]);
  51        printf("prctl(PR_SET_TSC, PR_TSC_ENABLE)\n");
  52        fflush(stdout);
  53        if ( prctl(PR_SET_TSC, PR_TSC_ENABLE) == -1)
  56                perror("prctl");
  52        
  v2.code=PR_T("prctl(PR_SET_TSC, PR_TSC_ENABLE)\n");

  v2.6.16.28
  
  5#L38" id="L38" class="line" name="L38">  38
  39void []] =
  40{
  41        i6 href="+6ode=printf" class="sref6>prin6tsc_val = 0;
  42
  43        <6 ( 6f(&tsc-teisable-tsc-test.c#naSC, "PR_TSGV") == -1)
code=sig" class="sref">sig)

  v2.6.16.28
  
    25const cha6">  52        ();
tsc_names[<(un.c#ned long long"prctl(PR_SET_TSC, PR_TSC_ENABLE)\n");
  45        <6 ( 67(stdout);
  53        i6        39void PR_TSC_SIref="+code=PR_GET_TSC" class="sref">PR_GET_TSC, &tsc_val) == -1)
  48         7 href="+7ode=printf" class="sref7>prin7f("prctl");
  32, tsc_names[tsc_val]);
  51        <7 ( 7f("[ SIG_SEGV ]\n");
tsc_names[<(un.c#ned long long"prctl(PR_GET_TSC, &tsc_val); ");
  52        <7">  52        (stdout);
  46
   7#include <;7rctl(PR_GET_TSC, &PR_TSC_ENABLE) == -1)
  56         7      perror("prctl");
  49
  50        <8 href="+8ode=printf" class="sref8>prin8f("tsc_val == %s\n", tsc_names[<(un.c#ned long long"prctl(PR_SET_TSC, PR_TSC_ENABLE)\n");
  52        <8 href="+8ode=fflush" class="sref8>fflu8h(stdout);
  53        i8 ( 8L14" id="L14" class="line" name="L14">  14#include 8      PR_GET_TSC, &PR_TSC_ENABLE) == -1)
"PR_TSC_SIGSEGV"  56         8">  52        perror("prctl");
  49
   7#include <8;87();

  v2.6.16.28
  
  stdout);
  53        i8Document8tion/prctl/disable-tsc-8est.c89();
tsc_names[<(un.c#ned long long& class="sref">stdout);
  53        i9 href="+9ode=printf" class="sref9>prin9L32" id="L32" class="line" name="L32">  32  53        i9 ( 93L38" id="L38" class="line" name="L38">  38
  25const cha9">  52        
The original LXR software bye-tscd="L25" chttp://sourceforge.netameojects/lxr">LXR ouldunityC_ENABocumeexn>)i="linl vers-tesbyed="L25" cmailto:lxr@able-.no">lxr@able-.noC_EN.
re clas="don      
lxr.able-.no kindly hosted byed="L25" chttp://www.redpill-ablmeo.no">Redpill Lblmeo ASC_ENABmeovidetatioLble- _namultableableon>)ne" ns serviceshref="D1995.
re class=/bodyas=/htmlse