Useful assembly instructions

The instruction rdtsc can be used to read the time stamp counter.

  • it is a non-serializing instruction (the processor may reorder its execution).

  • the value returned depends on the core where it was executed.

The instruction rdtscp can be used to read the time stamp counter and the core signature (usually the code ID number).

  • it is a serializing instruction.

  • the value returned depends on the core where it was executed.

  • asm volatile("rdtscp" : "=a" (rax),"=c" (rcx),"=d" (rdx));

  • as in the rdtsc instruction, the counter value is given by (rdx<<32)+rax.

  • the core signature is given by rcx; on GNU/Linux, its value is the core id that executed the instruction.

The instruction cpuid can be used to get information about the processor.

  • it is a serializing instruction.

  • this instruction unconditionally generates a trap (vmexit) in a virtualized environment.

  • it can be used when CPL is 3 (least privileged mode).

The instruction invd can be used to invalidate a cache line.

  • this instruction unconditionally generates a trap when executed in a virtualized environment.

  • it can be used only when CPL is 0 (kernel mode).

  • use it only on a memory region whose contents are irrelevant.

The instructions rdseed and rdrand are used to generate random numbers on recent Intel/AMD processors.

  • the virtual machine hypervisor can set things up so that these instructions generate a trap.

  • it can be used when CPL is 3 (least privileged mode).

The instructions in, out, rdtsc, rdmsrd, and rdpmcd can also be used to generate a trap in a virtualized environment (if the hypervisors want that to happen).

Last updated