tsc.c 829 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include "libcflat.h"
  2. #include "processor.h"
  3. #define CPUID_80000001_EDX_RDTSCP (1 << 27)
  4. int check_cpuid_80000001_edx(unsigned int bit)
  5. {
  6. return (cpuid(0x80000001).d & bit) != 0;
  7. }
  8. void test_wrtsc(u64 t1)
  9. {
  10. u64 t2;
  11. wrtsc(t1);
  12. t2 = rdtsc();
  13. printf("rdtsc after wrtsc(%" PRId64 "): %" PRId64 "\n", t1, t2);
  14. }
  15. void test_rdtscp(u64 aux)
  16. {
  17. u32 ecx;
  18. wrmsr(MSR_TSC_AUX, aux);
  19. rdtscp(&ecx);
  20. report("Test RDTSCP %" PRIu64, ecx == aux, aux);
  21. }
  22. int main()
  23. {
  24. u64 t1, t2;
  25. t1 = rdtsc();
  26. t2 = rdtsc();
  27. printf("rdtsc latency %u\n", (unsigned)(t2 - t1));
  28. test_wrtsc(0);
  29. test_wrtsc(100000000000ull);
  30. if (check_cpuid_80000001_edx(CPUID_80000001_EDX_RDTSCP)) {
  31. test_rdtscp(0);
  32. test_rdtscp(10);
  33. test_rdtscp(0x100);
  34. } else
  35. printf("rdtscp not supported\n");
  36. return report_summary();
  37. }