Skip to content

Commit e75cca6

Browse files
junjiemao1lijinxia
authored andcommitted
HV: timer: keep TSC frequency in KHz
This patch represents TSC freqeuency in KHz using a 32-bit unsigned integer. The conversion macros between ticks and us/ms are changed to inline functions to enforce the types of the input parameters. Note that us_to_ticks accepts only uint32_t (~4K us at most) and never overflows. Results of some unit tests on the conversion functions: calibrate_tsc, tsc_khz=1881600 64us -> ticks: 120422 64us -> ticks -> us: 63 511us -> ticks: 961497 511us -> ticks -> us: 510 1280000 ticks -> us: 680 1280000 ticks -> us -> ticks: 1279488 Signed-off-by: Junjie Mao <junjie.mao@intel.com> Acked-by: Eddie Dong <eddie.dong@intel.com>
1 parent ffc0b27 commit e75cca6

File tree

7 files changed

+33
-15
lines changed

7 files changed

+33
-15
lines changed

hypervisor/arch/x86/cpu.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,12 +497,12 @@ static void bsp_boot_post(void)
497497
pr_acrnlog("HV version %d.%d-rc%d-%s-%s %s build by %s, start time %lluus",
498498
HV_MAJOR_VERSION, HV_MINOR_VERSION, HV_RC_VERSION,
499499
HV_BUILD_TIME, HV_BUILD_VERSION, HV_BUILD_TYPE,
500-
HV_BUILD_USER, TICKS_TO_US(start_tsc));
500+
HV_BUILD_USER, ticks_to_us(start_tsc));
501501
else
502502
pr_acrnlog("HV version %d.%d-%s-%s %s build by %s, start time %lluus",
503503
HV_MAJOR_VERSION, HV_MINOR_VERSION,
504504
HV_BUILD_TIME, HV_BUILD_VERSION, HV_BUILD_TYPE,
505-
HV_BUILD_USER, TICKS_TO_US(start_tsc));
505+
HV_BUILD_USER, ticks_to_us(start_tsc));
506506

507507
pr_acrnlog("API version %d.%d",
508508
HV_API_MAJOR_VERSION, HV_API_MINOR_VERSION);

hypervisor/arch/x86/cpuid.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ static void init_vcpuid_entry(__unused struct vm *vm,
139139
* EBX, ECX, EDX: RESERVED (reserved fields are set to zero).
140140
*/
141141
case 0x40000010:
142-
entry->eax = (uint32_t)(tsc_hz / 1000);
142+
entry->eax = tsc_khz;
143143
entry->ebx = 0;
144144
entry->ecx = 0;
145145
entry->edx = 0;

hypervisor/arch/x86/timer.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#define CAL_MS 10
1212
#define MIN_TIMER_PERIOD_US 500
1313

14-
uint64_t tsc_hz = 1000000000;
14+
uint32_t tsc_khz = 0U;
1515

1616
static void run_timer(struct timer *timer)
1717
{
@@ -79,7 +79,7 @@ int add_timer(struct timer *timer)
7979
/* limit minimal periodic timer cycle period */
8080
if (timer->mode == TICK_MODE_PERIODIC)
8181
timer->period_in_cycle = max(timer->period_in_cycle,
82-
US_TO_TICKS(MIN_TIMER_PERIOD_US));
82+
us_to_ticks(MIN_TIMER_PERIOD_US));
8383

8484
pcpu_id = get_cpu_id();
8585
cpu_timer = &per_cpu(cpu_timers, pcpu_id);
@@ -285,8 +285,10 @@ static uint64_t native_calibrate_tsc(void)
285285

286286
void calibrate_tsc(void)
287287
{
288+
uint64_t tsc_hz;
288289
tsc_hz = native_calibrate_tsc();
289290
if (tsc_hz == 0U)
290291
tsc_hz = pit_calibrate_tsc(CAL_MS);
291-
printf("%s, tsc_hz=%lu\n", __func__, tsc_hz);
292+
tsc_khz = (uint32_t)(tsc_hz / 1000UL);
293+
printf("%s, tsc_khz=%lu\n", __func__, tsc_khz);
292294
}

hypervisor/common/hv_main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ void get_vmexit_profile(char *str, int str_max)
148148
int len, size = str_max;
149149

150150
len = snprintf(str, size, "\r\nNow(us) = %16lld\r\n",
151-
TICKS_TO_US(rdtsc()));
151+
ticks_to_us(rdtsc()));
152152
size -= len;
153153
str += len;
154154

@@ -169,7 +169,7 @@ void get_vmexit_profile(char *str, int str_max)
169169
for (cpu = 0; cpu < phys_cpu_num; cpu++) {
170170
len = snprintf(str, size, "\t%10lld\t%10lld",
171171
per_cpu(vmexit_cnt, cpu)[i],
172-
TICKS_TO_US(per_cpu(vmexit_time, cpu)[i]));
172+
ticks_to_us(per_cpu(vmexit_time, cpu)[i]));
173173
size -= len;
174174
str += len;
175175
}

hypervisor/debug/logmsg.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ void do_logmsg(uint32_t severity, const char *fmt, ...)
101101
timestamp = rdtsc();
102102

103103
/* Scale time-stamp appropriately */
104-
timestamp = TICKS_TO_US(timestamp);
104+
timestamp = ticks_to_us(timestamp);
105105

106106
/* Get CPU ID */
107107
cpu_id = get_cpu_id();

hypervisor/include/lib/rtl.h

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,28 @@ int atoi(const char *str);
4444
long strtol_deci(const char *nptr);
4545
uint64_t strtoul_hex(const char *nptr);
4646

47-
extern uint64_t tsc_hz;
48-
#define US_TO_TICKS(x) ((x) * tsc_hz / 1000000UL)
49-
#define CYCLES_PER_MS US_TO_TICKS(1000UL)
47+
/**
48+
* Frequency of TSC in KHz (where 1KHz = 1000Hz). Only valid after
49+
* calibrate_tsc() returns.
50+
*/
51+
extern uint32_t tsc_khz;
52+
53+
static inline uint64_t us_to_ticks(uint32_t us)
54+
{
55+
return ((uint64_t)us * (uint64_t)tsc_khz / 1000UL);
56+
}
57+
58+
#define CYCLES_PER_MS us_to_ticks(1000U)
5059

51-
#define TICKS_TO_US(x) ((((x) * (1000000UL >> 8)) / tsc_hz) << 8)
52-
#define TICKS_TO_MS(x) (((x) * 1000UL) / tsc_hz)
60+
static inline uint64_t ticks_to_us(uint64_t ticks)
61+
{
62+
return (ticks * 1000UL) / (uint64_t)tsc_khz;
63+
}
64+
65+
static inline uint64_t ticks_to_ms(uint64_t ticks)
66+
{
67+
return ticks / (uint64_t)tsc_khz;
68+
}
5369

5470
static inline uint64_t rdtsc(void)
5571
{

hypervisor/lib/udelay.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ void udelay(uint32_t us)
1111
uint64_t dest_tsc, delta_tsc;
1212

1313
/* Calculate number of ticks to wait */
14-
delta_tsc = US_TO_TICKS(us);
14+
delta_tsc = us_to_ticks(us);
1515
dest_tsc = rdtsc() + delta_tsc;
1616

1717
/* Loop until time expired */

0 commit comments

Comments
 (0)