Skip to contentSkip to conversation

nSVM: Add a test for the P (present) bit in NPT entry

PATCH Superseded Krish Sadhukhan 4 participants 8 messages Aug 29, 2020 – Sep 18, 2020 KVM

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nSVM: Add a test for the P (present) bit in NPT entry

If the P (present) bit in an NPT entry is cleared, VMRUN will fail and the guest will exit to the host with an exit code of 0x400 (#NPF). The following bits of importance in EXITINFO1 will be set/cleared to indicate the failure:

	bit# 0: cleared
	bit# 32: set
---
 x86/svm_tests.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
x86/svm_tests.c +29 -0
index 1908c7c..e9f178a 100644
@@ -720,6 +720,32 @@ static bool npt_nx_check(struct svm_test *test)
            && (vmcb->control.exit_info_1 == 0x100000015ULL);
 }
 
+static void npt_np_prepare(struct svm_test *test)
+{
+    u64 *pte;
+
+    scratch_page = alloc_page();
+    vmcb_ident(vmcb);
+    pte = npt_get_pte((u64)scratch_page);
+
+    *pte &= ~1ULL;
+}
+
+static void npt_np_test(struct svm_test *test)
+{
+    (void) *(volatile u64 *)scratch_page;
+}
+
+static bool npt_np_check(struct svm_test *test)
+{
+    u64 *pte = npt_get_pte((u64)scratch_page);
+
+    *pte |= 1ULL;
+
+    return (vmcb->control.exit_code == SVM_EXIT_NPF)
+           && (vmcb->control.exit_info_1 == 0x100000004ULL);
+}
+
 static void npt_us_prepare(struct svm_test *test)
 {
     u64 *pte;
@@ -2119,6 +2145,9 @@ struct svm_test svm_tests[] = {
     { "npt_nx", npt_supported, npt_nx_prepare,
       default_prepare_gif_clear, null_test,
       default_finished, npt_nx_check },
+    { "npt_np", npt_supported, npt_np_prepare,
+      default_prepare_gif_clear, npt_np_test,
+      default_finished, npt_np_check },
     { "npt_us", npt_supported, npt_us_prepare,
       default_prepare_gif_clear, npt_us_test,
       default_finished, npt_us_check },
Signature · 1 line
2.18.4
#1 lore raw
Quoted 7 lines · Krish Sadhukhan

If the P (present) bit in an NPT entry is cleared, VMRUN will fail and the guest will exit to the host with an exit code of 0x400 (#NPF). The following bits of importance in EXITINFO1 will be set/cleared to indicate the failure:

bit# 0: cleared
bit# 32: set

This seems like a terrible commit description. First, the P bit can be cleared in a plethora of NPT entries without having any effect on guest execution. It's only if the guest tries to access a GPA whose translation uses the non-present NPT entry that there is an issue. Second, the VMRUN does not fail. If the VM-exit code is anything other than -1, the VMRUN has succeeded. Third, the bits in EXITINFO that get set/cleared depend very much on the actual access. Yes, if the nested page walk terminates due to a non-present page, bit 0 will be cleared. However, bit 32 will only be set if the non-present page was encountered while translating the final guest physical address (not the guest physical address of a page table page encountered during the
walk). Moreover, older AMD hardware never sets bits 32 or 33 at all.
Bit 1 will be set if the access was a write (or a page table walk). Bit 2 will be set for a user access. Bit 4 will be set for a code read (while translating the final guest physical address).

maintainer
#2 lore raw
Quoted 10 lines · Jim Mattson
Quoted 7 lines · Krish Sadhukhan

If the P (present) bit in an NPT entry is cleared, VMRUN will fail and the guest will exit to the host with an exit code of 0x400 (#NPF). The following bits of importance in EXITINFO1 will be set/cleared to indicate the failure:

bit# 0: cleared
bit# 32: set

Quoted 16 lines

This seems like a terrible commit description. First, the P bit can be cleared in a plethora of NPT entries without having any effect on guest execution. It's only if the guest tries to access a GPA whose translation uses the non-present NPT entry that there is an issue. Second, the VMRUN does not fail. If the VM-exit code is anything other than -1, the VMRUN has succeeded. Third, the bits in EXITINFO that get set/cleared depend very much on the actual access. Yes, if the nested page walk terminates due to a non-present page, bit 0 will be cleared. However, bit 32 will only be set if the non-present page was encountered while translating the final guest physical address (not the guest physical address of a page table page encountered during the
walk). Moreover, older AMD hardware never sets bits 32 or 33 at all.
Bit 1 will be set if the access was a write (or a page table walk). Bit 2 will be set for a user access. Bit 4 will be set for a code read (while translating the final guest physical address).

Queued, with an adjusted commit message.

I am currently on leave so I am going through the patches and queuing them, but I will only push kvm/next and kvm/queue next week. kvm/master patches will be sent to Linus for the next -rc though.

Thanks,

Paolo

maintainer
#3 lore raw
In reply to Jim Mattson
Quoted 1 line · Jim Mattson

Moreover, older AMD hardware never sets bits 32 or 33 at all.

Interesting, I didn't know this. Is it documented at all?

Paolo

#4 lore raw
Quoted 5 lines · Paolo Bonzini
Quoted 1 line · Jim Mattson

Moreover, older AMD hardware never sets bits 32 or 33 at all.

Interesting, I didn't know this. Is it documented at all?

You'll have to find an old version of the APM. For example, see page 56 of http://www.0x04.net/doc/amd/33047.pdf (this was back when SVM was a separate document).

#5 lore raw
Quoted 11 lines · Jim Mattson
Quoted 5 lines · Paolo Bonzini
Quoted 1 line · Jim Mattson

Moreover, older AMD hardware never sets bits 32 or 33 at all.

Interesting, I didn't know this. Is it documented at all?

You'll have to find an old version of the APM. For example, see page 56 of http://www.0x04.net/doc/amd/33047.pdf (this was back when SVM was a separate document).

Ah ha. It looks like bits 32 and 33 weren't there in version 3.14 of the APM volume 2 in September 2007. (See
http://application-notes.digchip.com/019/19-44680.pdf, pages 410-411.) They had appeared by version 3.17 in June 2010. Maybe someone from AMD can enlighten us. My memory's just not that good.

#6 lore raw
Quoted 14 lines · Jim Mattson
Quoted 11 lines · Jim Mattson
Quoted 5 lines · Paolo Bonzini
Quoted 1 line · Jim Mattson

Moreover, older AMD hardware never sets bits 32 or 33 at all.

Interesting, I didn't know this. Is it documented at all?

You'll have to find an old version of the APM. For example, see page 56 of http://www.0x04.net/doc/amd/33047.pdf (this was back when SVM was a separate document).

Quoted 5 lines

Ah ha. It looks like bits 32 and 33 weren't there in version 3.14 of the APM volume 2 in September 2007. (See
http://application-notes.digchip.com/019/19-44680.pdf, pages 410-411.) They had appeared by version 3.17 in June 2010. Maybe someone from AMD can enlighten us. My memory's just not that good.

In fact, if I recall correctly, this is why AMD has *two* bits where it would seem that one bit should suffice; they actually have three states:

00: No information provided
01: Final translation
10: Page table walk

#7 lore raw
In reply to Jim Mattson
Quoted 13 lines · Jim Mattson
Quoted 11 lines · Jim Mattson
Quoted 5 lines · Paolo Bonzini
Quoted 1 line · Jim Mattson

Moreover, older AMD hardware never sets bits 32 or 33 at all.

Interesting, I didn't know this. Is it documented at all?

You'll have to find an old version of the APM. For example, see page
56 of https://nam11.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.0x04.net%2Fdoc%2Famd%2F33047.pdf&data=02%7C01%7Cthomas.lendacky%40amd.com%7C9ee685813f444f314d4808d859255108%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637357364669001358&sdata=3a%2F0OrkZHC7r8vsoq2yhKqt8cQ8siPt6PG%2Be674yiXs%3D&reserved=0 (this was back when SVM was a separate document).

Quoted 5 lines

Ah ha. It looks like bits 32 and 33 weren't there in version 3.14 of
the APM volume 2 in September 2007. (See
https://nam11.safelinks.protection.outlook.com/?url=http%3A%2F%2Fapplication-notes.digchip.com%2F019%2F19-44680.pdf&data=02%7C01%7Cthomas.lendacky%40amd.com%7C9ee685813f444f314d4808d859255108%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637357364669001358&sdata=iZ0lotbZ4kRtRrjE9bVb%2F9NSy7H3RMJauUfe3FvGDT0%3D&reserved=0, pages 410-411.) They had appeared by version 3.17 in June 2010. Maybe someone from AMD
can enlighten us. My memory's just not that good.

I tried to do some digging on it. It looks like the support was available back in fam14h. Not sure about families before that. It would have been nice to have a CPUID bit for this.

Thanks,
Tom

Quoted 1 line