blob: 7bec31a0966a36090726faebe502eb7ff4b1a724 [file] [log] [blame]
Andrew Scull3c257452019-11-26 13:32:50 +00001/*
2 * Copyright 2019 The Hafnium Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gmock/gmock.h>
18
19extern "C" {
20#include "hf/mpool.h"
21#include "hf/vm.h"
22}
23
24#include <memory>
25#include <span>
26#include <vector>
27
28#include "mm_test.hh"
29
30namespace
31{
32using namespace ::std::placeholders;
33
34using ::testing::AllOf;
35using ::testing::Each;
36using ::testing::SizeIs;
37
38using struct_vm = struct vm;
39
40constexpr size_t TEST_HEAP_SIZE = PAGE_SIZE * 16;
41const int TOP_LEVEL = arch_mm_stage2_max_level();
42
43class vm : public ::testing::Test
44{
45 void SetUp() override
46 {
47 /*
48 * TODO: replace with direct use of stdlib allocator so
49 * sanitizers are more effective.
50 */
51 test_heap = std::make_unique<uint8_t[]>(TEST_HEAP_SIZE);
52 mpool_init(&ppool, sizeof(struct mm_page_table));
53 mpool_add_chunk(&ppool, test_heap.get(), TEST_HEAP_SIZE);
54 }
55
56 std::unique_ptr<uint8_t[]> test_heap;
57
58 protected:
59 struct mpool ppool;
60};
61
62/**
63 * If nothing is mapped, unmapping the hypervisor has no effect.
64 */
65TEST_F(vm, vm_unmap_hypervisor_not_mapped)
66{
67 struct_vm *vm;
68 struct vm_locked vm_locked;
69
Andrew Walbran9daa57e2019-09-27 13:33:20 +010070 EXPECT_TRUE(vm_init_next(1, &ppool, &vm));
Andrew Scull3c257452019-11-26 13:32:50 +000071 vm_locked = vm_lock(vm);
72 ASSERT_TRUE(mm_vm_init(&vm->ptable, &ppool));
73 EXPECT_TRUE(vm_unmap_hypervisor(vm_locked, &ppool));
74 EXPECT_THAT(
75 mm_test::get_ptable(vm->ptable),
76 AllOf(SizeIs(4), Each(Each(arch_mm_absent_pte(TOP_LEVEL)))));
77 mm_vm_fini(&vm->ptable, &ppool);
78 vm_unlock(&vm_locked);
79}
80
81} /* namespace */