// SPDX-License-Identifier: GPL-2.0-only /* * Usage: to be run via nx_huge_page_test.sh, which does the necessary * environment setup and teardown * * Copyright (C) 2022, Google LLC.
*/ #include <fcntl.h> #include <stdint.h> #include <time.h>
/* * Passed by nx_huge_pages_test.sh to provide an easy warning if this test is * being run without it.
*/ #define MAGIC_TOKEN 887563923
/* * x86 opcode for the return instruction. Used to call into, and then * immediately return from, memory backed with hugepages.
*/ #define RETURN_OPCODE 0xC3
/* * Exit the VM after each memory access so that the userspace component of the * test can make assertions about the pages backing the VM. * * See the below for an explanation of how each access should affect the * backing mappings.
*/ void guest_code(void)
{
uint64_t hpage_1 = HPAGE_GVA;
uint64_t hpage_2 = hpage_1 + (PAGE_SIZE * 512);
uint64_t hpage_3 = hpage_2 + (PAGE_SIZE * 512);
READ_ONCE(*(uint64_t *)hpage_1);
GUEST_SYNC(1);
READ_ONCE(*(uint64_t *)hpage_2);
GUEST_SYNC(2);
guest_do_CALL(hpage_1);
GUEST_SYNC(3);
guest_do_CALL(hpage_3);
GUEST_SYNC(4);
READ_ONCE(*(uint64_t *)hpage_1);
GUEST_SYNC(5);
READ_ONCE(*(uint64_t *)hpage_3);
GUEST_SYNC(6);
}
staticvoid check_2m_page_count(struct kvm_vm *vm, int expected_pages_2m)
{ int actual_pages_2m;
if (disable_nx_huge_pages) {
r = __vm_disable_nx_huge_pages(vm); if (reboot_permissions) {
TEST_ASSERT(!r, "Disabling NX huge pages should succeed if process has reboot permissions");
} else {
TEST_ASSERT(r == -1 && errno == EPERM, "This process should not have permission to disable NX huge pages"); return;
}
}
/* * Ensure that KVM can map HPAGE_SLOT with huge pages by mapping the * region into the guest with 2MiB pages whenever TDP is disabled (i.e. * whenever KVM is shadowing the guest page tables). * * When TDP is enabled, KVM should be able to map HPAGE_SLOT with huge * pages irrespective of the guest page size, so map with 4KiB pages * to test that that is the case.
*/ if (kvm_is_tdp_enabled())
virt_map_level(vm, HPAGE_GVA, HPAGE_GPA, nr_bytes, PG_LEVEL_4K); else
virt_map_level(vm, HPAGE_GVA, HPAGE_GPA, nr_bytes, PG_LEVEL_2M);
hva = addr_gpa2hva(vm, HPAGE_GPA);
memset(hva, RETURN_OPCODE, nr_bytes);
/* * The guest code will first read from the first hugepage, resulting * in a huge page mapping being created.
*/
vcpu_run(vcpu);
check_2m_page_count(vm, 1);
check_split_count(vm, 0);
/* * Then the guest code will read from the second hugepage, resulting * in another huge page mapping being created.
*/
vcpu_run(vcpu);
check_2m_page_count(vm, 2);
check_split_count(vm, 0);
/* * Next, the guest will execute from the first huge page, causing it * to be remapped at 4k. * * If NX huge pages are disabled, this should have no effect.
*/
vcpu_run(vcpu);
check_2m_page_count(vm, disable_nx_huge_pages ? 2 : 1);
check_split_count(vm, disable_nx_huge_pages ? 0 : 1);
/* * Executing from the third huge page (previously unaccessed) will * cause part to be mapped at 4k. * * If NX huge pages are disabled, it should be mapped at 2M.
*/
vcpu_run(vcpu);
check_2m_page_count(vm, disable_nx_huge_pages ? 3 : 1);
check_split_count(vm, disable_nx_huge_pages ? 0 : 2);
/* Reading from the first huge page again should have no effect. */
vcpu_run(vcpu);
check_2m_page_count(vm, disable_nx_huge_pages ? 3 : 1);
check_split_count(vm, disable_nx_huge_pages ? 0 : 2);
/* Give recovery thread time to run. */
wait_for_reclaim(reclaim_period_ms);
/* * Now that the reclaimer has run, all the split pages should be gone. * * If NX huge pages are disabled, the relaimer will not run, so * nothing should change from here on.
*/
check_2m_page_count(vm, disable_nx_huge_pages ? 3 : 1);
check_split_count(vm, 0);
/* * The 4k mapping on hpage 3 should have been removed, so check that * reading from it causes a huge page mapping to be installed.
*/
vcpu_run(vcpu);
check_2m_page_count(vm, disable_nx_huge_pages ? 3 : 2);
check_split_count(vm, 0);
kvm_vm_free(vm);
}
staticvoid help(char *name)
{
puts("");
printf("usage: %s [-h] [-p period_ms] [-t token]\n", name);
puts("");
printf(" -p: The NX reclaim period in milliseconds.\n");
printf(" -t: The magic token to indicate environment setup is done.\n");
printf(" -r: The test has reboot permissions and can disable NX huge pages.\n");
puts(""); exit(0);
}
int main(int argc, char **argv)
{ int reclaim_period_ms = 0, token = 0, opt; bool reboot_permissions = false;
__TEST_REQUIRE(token == MAGIC_TOKEN, "This test must be run with the magic token via '-t %d'.\n" "Running via nx_huge_pages_test.sh, which also handles " "environment setup, is strongly recommended.", MAGIC_TOKEN);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.