blob: 6fb2bce8236e7aabadfdfafb6eff707c066d5513 [file] [log] [blame]
Andrew Scull3c257452019-11-26 13:32:50 +00001/*
2 * Copyright 2019 The Hafnium Authors.
3 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * 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 Scull3c257452019-11-26 13:32:50 +00007 */
8
9#include <gmock/gmock.h>
10
11extern "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
22namespace
23{
24using namespace ::std::placeholders;
25
26using ::testing::AllOf;
27using ::testing::Each;
28using ::testing::SizeIs;
29
30using struct_vm = struct vm;
31
32constexpr size_t TEST_HEAP_SIZE = PAGE_SIZE * 16;
33const int TOP_LEVEL = arch_mm_stage2_max_level();
34
35class 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 */
57TEST_F(vm, vm_unmap_hypervisor_not_mapped)
58{
59 struct_vm *vm;
60 struct vm_locked vm_locked;
61
Andrew Walbran9daa57e2019-09-27 13:33:20 +010062 EXPECT_TRUE(vm_init_next(1, &ppool, &vm));
Andrew Scull3c257452019-11-26 13:32:50 +000063 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 */