Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Hafnium Authors. |
| 3 | * |
Andrew Walbran | e959ec1 | 2020-06-17 15:01:09 +0100 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style |
| 5 | * license that can be found in the LICENSE file or at |
| 6 | * https://opensource.org/licenses/BSD-3-Clause. |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <gmock/gmock.h> |
| 10 | |
| 11 | extern "C" { |
| 12 | #include "hf/mpool.h" |
| 13 | #include "hf/vm.h" |
| 14 | } |
| 15 | |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 16 | #include <list> |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 17 | #include <memory> |
| 18 | #include <span> |
| 19 | #include <vector> |
| 20 | |
| 21 | #include "mm_test.hh" |
| 22 | |
| 23 | namespace |
| 24 | { |
| 25 | using namespace ::std::placeholders; |
| 26 | |
| 27 | using ::testing::AllOf; |
| 28 | using ::testing::Each; |
| 29 | using ::testing::SizeIs; |
| 30 | |
| 31 | using struct_vm = struct vm; |
| 32 | |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 33 | constexpr size_t TEST_HEAP_SIZE = PAGE_SIZE * 32; |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 34 | const int TOP_LEVEL = arch_mm_stage2_max_level(); |
| 35 | |
| 36 | class vm : public ::testing::Test |
| 37 | { |
| 38 | void SetUp() override |
| 39 | { |
| 40 | /* |
| 41 | * TODO: replace with direct use of stdlib allocator so |
| 42 | * sanitizers are more effective. |
| 43 | */ |
| 44 | test_heap = std::make_unique<uint8_t[]>(TEST_HEAP_SIZE); |
| 45 | mpool_init(&ppool, sizeof(struct mm_page_table)); |
| 46 | mpool_add_chunk(&ppool, test_heap.get(), TEST_HEAP_SIZE); |
| 47 | } |
| 48 | |
| 49 | std::unique_ptr<uint8_t[]> test_heap; |
| 50 | |
| 51 | protected: |
| 52 | struct mpool ppool; |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 53 | |
| 54 | public: |
| 55 | static bool BootOrderBiggerThan(struct_vm *vm1, struct_vm *vm2) |
| 56 | { |
| 57 | return vm1->boot_order > vm2->boot_order; |
| 58 | } |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | /** |
| 62 | * If nothing is mapped, unmapping the hypervisor has no effect. |
| 63 | */ |
| 64 | TEST_F(vm, vm_unmap_hypervisor_not_mapped) |
| 65 | { |
| 66 | struct_vm *vm; |
| 67 | struct vm_locked vm_locked; |
| 68 | |
Andrew Walbran | 9daa57e | 2019-09-27 13:33:20 +0100 | [diff] [blame] | 69 | EXPECT_TRUE(vm_init_next(1, &ppool, &vm)); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 70 | vm_locked = vm_lock(vm); |
| 71 | ASSERT_TRUE(mm_vm_init(&vm->ptable, &ppool)); |
| 72 | EXPECT_TRUE(vm_unmap_hypervisor(vm_locked, &ppool)); |
| 73 | EXPECT_THAT( |
| 74 | mm_test::get_ptable(vm->ptable), |
| 75 | AllOf(SizeIs(4), Each(Each(arch_mm_absent_pte(TOP_LEVEL))))); |
| 76 | mm_vm_fini(&vm->ptable, &ppool); |
| 77 | vm_unlock(&vm_locked); |
| 78 | } |
| 79 | |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 80 | /** |
| 81 | * Validate the "boot_list" is created properly, according to vm's "boot_order" |
| 82 | * field. |
| 83 | */ |
| 84 | TEST_F(vm, vm_boot_order) |
| 85 | { |
| 86 | struct_vm *vm_cur; |
| 87 | std::list<struct_vm *> expected_final_order; |
| 88 | |
| 89 | EXPECT_FALSE(vm_get_first_boot()); |
| 90 | |
| 91 | /* |
| 92 | * Insertion when no call to "vm_update_boot" has been made yet. |
| 93 | * The "boot_list" is expected to be empty. |
| 94 | */ |
| 95 | EXPECT_TRUE(vm_init_next(1, &ppool, &vm_cur)); |
| 96 | vm_cur->boot_order = 1; |
| 97 | vm_update_boot(vm_cur); |
| 98 | expected_final_order.push_back(vm_cur); |
| 99 | |
| 100 | EXPECT_EQ(vm_get_first_boot()->id, vm_cur->id); |
| 101 | |
| 102 | /* Insertion at the head of the boot list */ |
| 103 | EXPECT_TRUE(vm_init_next(1, &ppool, &vm_cur)); |
| 104 | vm_cur->boot_order = 3; |
| 105 | vm_update_boot(vm_cur); |
| 106 | expected_final_order.push_back(vm_cur); |
| 107 | |
| 108 | EXPECT_EQ(vm_get_first_boot()->id, vm_cur->id); |
| 109 | |
| 110 | /* Insertion of two in the middle of the boot list */ |
| 111 | for (int i = 0; i < 2; i++) { |
| 112 | EXPECT_TRUE(vm_init_next(1, &ppool, &vm_cur)); |
| 113 | vm_cur->boot_order = 2; |
| 114 | vm_update_boot(vm_cur); |
| 115 | expected_final_order.push_back(vm_cur); |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * Insertion in the end of the list. |
| 120 | * This tests shares the data with "vm_unmap_hypervisor_not_mapped". |
| 121 | * As such, a VM is expected to have been initialized before this |
| 122 | * test, with ID 1 and boot_order 0. |
| 123 | */ |
| 124 | vm_cur = vm_find(1); |
| 125 | EXPECT_FALSE(vm_cur == NULL); |
| 126 | vm_update_boot(vm_cur); |
| 127 | expected_final_order.push_back(vm_cur); |
| 128 | |
| 129 | /* |
| 130 | * Number of VMs initialized should be the same as in the |
| 131 | * "expected_final_order", before the final verification. |
| 132 | */ |
| 133 | EXPECT_EQ(expected_final_order.size(), vm_get_count()) |
| 134 | << "Something went wrong with the test itself...\n"; |
| 135 | |
| 136 | /* Sort "expected_final_order" by "boot_order" field */ |
| 137 | expected_final_order.sort(vm::BootOrderBiggerThan); |
| 138 | |
| 139 | std::list<struct_vm *>::iterator it; |
| 140 | for (it = expected_final_order.begin(), vm_cur = vm_get_first_boot(); |
| 141 | it != expected_final_order.end() && vm_cur != NULL; |
| 142 | it++, vm_cur = vm_cur->next_boot) { |
| 143 | EXPECT_EQ((*it)->id, vm_cur->id); |
| 144 | } |
| 145 | } |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 146 | } /* namespace */ |