Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the implementation of rdtsc in src/mevent_test.c:68 #181

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Fix the implementation of rdtsc in src/mevent_test.c:68 #181

wants to merge 1 commit into from

Conversation

wierton
Copy link

@wierton wierton commented Dec 1, 2019

In inline assembly of xhyve/src/mevent_test.c:68, an unused inline
assembly __asm__ __volatile__ ("cpuid") is written here. cpuid is an
instruction which overrides register eax, ebx, ecx and edx by the
information of cpu, (details can be found in x86_64 specification).
This inline assembly is not only unuseful, under some context, it may
corrupt the normal C data flow, and cause errors in C world. For example:

static __inline uint64_t rdtsc(void)
{
	unsigned a, d;
	__asm__ __volatile__ ("cpuid");
	__asm__ __volatile__ ("rdtsc" : "=a" (a), "=d" (d));

	return (((uint64_t) a) | (((uint64_t) d) << 32));
}

int __attribute__((noinline)) set_val(int v) { return v; }

int main() {
  int a = set_val(0);
  int b = set_val(1);
  int c = set_val(2);
  a = a + b; b = b + c; c = c + a;
  uint64_t d = rdtsc();
  printf("%d, %d, %d, %lx\n", a, b, c, d);
  return 0;
}

The above code on my pc (gcc-8 -O2) will output 1, 2, 3, 7bcbf4733e902,
(assume rdtsc now is 7bcbf4733e902), but the actual output should be 1, 3,
3, 7bcbf4733e902, the reason is that cpuid override some registers
unexpectedly.

In inline assembly of xhyve/src/mevent_test.c:68, an unused inline
assembly `__asm__ __volatile__ ("cpuid")` is written here. `cpuid` is a
instruction which override register eax, ebx, ecx and edx by the
information of cpu, (details can be found in x86_64 specification).
This inline assembly is not only unuseful, under some context, this
inline assembly may corrupt the normal C data flow, and cause errors in
C world. For example:
```
static __inline uint64_t rdtsc(void)
{
	unsigned a, d;
	__asm__ __volatile__ ("cpuid");
	__asm__ __volatile__ ("rdtsc" : "=a" (a), "=d" (d));

	return (((uint64_t) a) | (((uint64_t) d) << 32));
}

int __attribute__((noinline)) set_val(int v) { return v; }

int main() {
  int a = set_val(0);
  int b = set_val(1);
  int c = set_val(2);
  a = a + b; b = b + c; c = c + a;
  uint64_t d = rdtsc();
  printf("%d, %d, %d, %lx\n", a, b, c, d);
  return 0;
}
```
The above code on my pc (gcc-8 -O2) will output 1, 2, 3, 7bcbf4733e902,
(assume rdtsc now is 7bcbf4733e902), but the actual output should be 1, 3,
3, 7bcbf4733e902, the reason is that cpuid override some registers
unexpectedly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant