Skip to content

Instantly share code, notes, and snippets.

@bgaff
Created November 8, 2021 17:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bgaff/9f8cbfc8dd22e60f9492e4f0aff8f04f to your computer and use it in GitHub Desktop.
Save bgaff/9f8cbfc8dd22e60f9492e4f0aff8f04f to your computer and use it in GitHub Desktop.
rdpkru - Intel 11th Gen Core CPU bug
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/mman.h>
#define BITS_PER_PKEY 2
static uint32_t rdpkru(void)
{
uint32_t ecx = 0;
uint32_t edx, pkru;
/*
* "rdpkru" instruction. Places PKRU contents in to EAX,
* clears EDX and requires that ecx=0.
*/
asm volatile(".byte 0x0f,0x01,0xee\n\t"
: "=a" (pkru), "=d" (edx)
: "c" (ecx));
return pkru;
}
static void print_pkru()
{
uint32_t pkru = rdpkru();
for (int i = 0; i < 32; i+=2) {
printf("(pkey: %d val:%d) ",i/2, pkru >> i & 3);
}
printf("\n");
}
int main(void) {
int pkey = pkey_alloc(0,0);
assert(pkey >= 0);
for (int iterations = 0; true ;iterations++) {
uint32_t pkru = rdpkru();
if (((pkru >> (pkey*BITS_PER_PKEY)) & 3) != 0) {
print_pkru();
fprintf(stderr, "pkey %d has an unexpected value on iteration %d\n", pkey, iterations);
exit(1);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment