# Linux Kernel Module Cheat

##### 29.13.1.1. x86 RDTSCP instruction

RDTSCP is like RDTSP, but it also stores the CPU ID into ECX: this is convenient because the value of RDTSC depends on which core we are currently on, so you often also want the core ID when you want the RDTSC.

Sources:

- userland/arch/x86_64/rdtscp.S

- userland/arch/x86_64/intrinsics/rdtscp.c

We can observe its operation with the good and old `taskset`, for example:

    taskset -c 0 ./userland/arch/x86_64/rdtscp.out | tail -n 1
    taskset -c 1 ./userland/arch/x86_64/rdtscp.out | tail -n 1

produces:

    0x00000000
    0x00000001

There is also the RDPID instruction that reads just the processor ID, but it appears to be very new for QEMU 4.0.0 or 2017 Lenovo ThinkPad P51, as it fails with SIGILL on both.

Bibliography:

- ARM has an analogous ARM PMCCNTR register

- https://stackoverflow.com/questions/22310028/is-there-an-x86-instruction-to-tell-which-core-the-instruction-is-being-run-on/56622112#56622112