1
0

tsc_adjust.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #include "libcflat.h"
  2. #include "processor.h"
  3. #define IA32_TSC_ADJUST 0x3b
  4. int main()
  5. {
  6. u64 t1, t2, t3, t4, t5;
  7. u64 est_delta_time;
  8. if (cpuid(7).b & (1 << 1)) { // IA32_TSC_ADJUST Feature is enabled?
  9. report("IA32_TSC_ADJUST msr initialization",
  10. rdmsr(IA32_TSC_ADJUST) == 0x0);
  11. t3 = 100000000000ull;
  12. t1 = rdtsc();
  13. wrmsr(IA32_TSC_ADJUST, t3);
  14. t2 = rdtsc();
  15. report("IA32_TSC_ADJUST msr read / write",
  16. rdmsr(IA32_TSC_ADJUST) == t3);
  17. report("TSC adjustment for IA32_TSC_ADJUST value",
  18. (t2 - t1) >= t3);
  19. t3 = 0x0;
  20. wrmsr(IA32_TSC_ADJUST, t3);
  21. report("IA32_TSC_ADJUST msr read / write",
  22. rdmsr(IA32_TSC_ADJUST) == t3);
  23. t4 = 100000000000ull;
  24. t1 = rdtsc();
  25. wrtsc(t4);
  26. t2 = rdtsc();
  27. t5 = rdmsr(IA32_TSC_ADJUST);
  28. // est of time between reading tsc and writing tsc,
  29. // (based on IA32_TSC_ADJUST msr value) should be small
  30. est_delta_time = t4 - t5 - t1;
  31. // arbitray 2x latency (wrtsc->rdtsc) threshold
  32. report("IA32_TSC_ADJUST msr adjustment on tsc write",
  33. est_delta_time <= (2 * (t2 - t4)));
  34. }
  35. else {
  36. report("IA32_TSC_ADJUST feature not enabled", true);
  37. }
  38. return report_summary();
  39. }