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 | |
| 16 | #include <memory> |
| 17 | #include <span> |
| 18 | #include <vector> |
| 19 | |
| 20 | #include "mm_test.hh" |
| 21 | |
| 22 | namespace |
| 23 | { |
| 24 | using namespace ::std::placeholders; |
| 25 | |
| 26 | using ::testing::AllOf; |
| 27 | using ::testing::Each; |
| 28 | using ::testing::SizeIs; |
| 29 | |
| 30 | using struct_vm = struct vm; |
| 31 | |
| 32 | constexpr size_t TEST_HEAP_SIZE = PAGE_SIZE * 16; |
| 33 | const int TOP_LEVEL = arch_mm_stage2_max_level(); |
| 34 | |
| 35 | class vm : public ::testing::Test |
| 36 | { |
| 37 | void SetUp() override |
| 38 | { |
| 39 | /* |
| 40 | * TODO: replace with direct use of stdlib allocator so |
| 41 | * sanitizers are more effective. |
| 42 | */ |
| 43 | test_heap = std::make_unique<uint8_t[]>(TEST_HEAP_SIZE); |
| 44 | mpool_init(&ppool, sizeof(struct mm_page_table)); |
| 45 | mpool_add_chunk(&ppool, test_heap.get(), TEST_HEAP_SIZE); |
| 46 | } |
| 47 | |
| 48 | std::unique_ptr<uint8_t[]> test_heap; |
| 49 | |
| 50 | protected: |
| 51 | struct mpool ppool; |
| 52 | }; |
| 53 | |
| 54 | /** |
| 55 | * If nothing is mapped, unmapping the hypervisor has no effect. |
| 56 | */ |
| 57 | TEST_F(vm, vm_unmap_hypervisor_not_mapped) |
| 58 | { |
| 59 | struct_vm *vm; |
| 60 | struct vm_locked vm_locked; |
| 61 | |
Andrew Walbran | 9daa57e | 2019-09-27 13:33:20 +0100 | [diff] [blame] | 62 | EXPECT_TRUE(vm_init_next(1, &ppool, &vm)); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 63 | vm_locked = vm_lock(vm); |
| 64 | ASSERT_TRUE(mm_vm_init(&vm->ptable, &ppool)); |
| 65 | EXPECT_TRUE(vm_unmap_hypervisor(vm_locked, &ppool)); |
| 66 | EXPECT_THAT( |
| 67 | mm_test::get_ptable(vm->ptable), |
| 68 | AllOf(SizeIs(4), Each(Each(arch_mm_absent_pte(TOP_LEVEL))))); |
| 69 | mm_vm_fini(&vm->ptable, &ppool); |
| 70 | vm_unlock(&vm_locked); |
| 71 | } |
| 72 | |
| 73 | } /* namespace */ |