PTEditor icon indicating copy to clipboard operation
PTEditor copied to clipboard

Compatibility with SEV-enabled kernels

Open d-we opened this issue 9 months ago • 1 comments

AMD SEV uses PFN bit 47 to indiciate whether a page is encrypted. This bit is also referred to as C-bit in the AMD context.

Thus, ptedit_get_pfn() returns wrong PFNs on these systems, which is easily detectable by running PTEditor's self tests.

A hacky workaround is to just clear that bit:

ptedit_fnc size_t ptedit_get_pfn(size_t pte) {
#if defined(__i386__) || defined(__x86_64__) || defined(_WIN64)
    size_t pfn = (pte & (((1ull << 40) - 1) << 12)) >> 12;
    // clear bit 47 (AMD SEV C-bit)
    return pfn & ~(1ull << 35);
#elif defined(__aarch64__)
    return (pte & (((1ull << 36) - 1) << 12)) >> 12;
#endif
}

That's probably fine as most system will not use PFN bit 47 but a clean solution would be to do that only when the kernel is actually an AMD SEV kernel. One possibility would be to check in the Makefile whether sev is contained in uname -r, e.g., SEV kernels are named like 6.1.0-sev-es. Based on that, we should set a compilation flag to enable/disable that fix

d-we avatar May 02 '25 14:05 d-we

also, in theory that bit position can change. It, however, looks like it's always the uppermost bit of the PFN range. Hence, we should probably be fine for all machines with 48bit physical addresses

d-we avatar May 02 '25 14:05 d-we