Skip to content

Instantly share code, notes, and snippets.

@bgaff
Created November 9, 2021 01:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bgaff/e4b5457ab1cf5126fea6327666c63441 to your computer and use it in GitHub Desktop.
Save bgaff/e4b5457ab1cf5126fea6327666c63441 to your computer and use it in GitHub Desktop.
11th Gen Core CPU PKRU
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/mman.h>
static uint32_t rdpkru(void)
{
uint32_t ecx = 0;
uint32_t edx, pkru;
asm volatile(".byte 0x0f,0x01,0xee\n\t"
: "=a" (pkru), "=d" (edx)
: "c" (ecx));
return pkru;
}
int main(void) {
int pkey = pkey_alloc(0,0);
assert(pkey >= 0);
uint32_t original_pkru = rdpkru();
for (int iterations = 0; true ; iterations++) {
uint32_t pkru = rdpkru();
if (pkru != original_pkru) {
fprintf(stderr, "unexpected value on iteration %d value:0x%08x expected:0x%08x\n",
iterations, pkru, original_pkru);
exit(1);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment