Andrew Walbran | 9fa106c | 2018-09-28 14:19:29 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2018 Google LLC |
| 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 | extern "C" { |
| 18 | #include "hf/mm.h" |
| 19 | |
| 20 | #include "hf/arch/mm.h" |
| 21 | |
| 22 | #include "hf/alloc.h" |
| 23 | } |
| 24 | |
| 25 | #include <memory> |
| 26 | |
| 27 | #include <gmock/gmock.h> |
| 28 | |
| 29 | using ::testing::Eq; |
| 30 | |
| 31 | constexpr size_t TEST_HEAP_SIZE = PAGE_SIZE * 10; |
| 32 | constexpr size_t ENTRY_COUNT = PAGE_SIZE / sizeof(pte_t); |
| 33 | const static int TOP_LEVEL = arch_mm_max_level(0); |
| 34 | const static pte_t ABSENT_ENTRY = arch_mm_absent_pte(TOP_LEVEL); |
| 35 | |
| 36 | /** |
| 37 | * Calculates the size of the address space represented by a page table entry at |
| 38 | * the given level. |
| 39 | */ |
| 40 | static size_t mm_entry_size(int level) |
| 41 | { |
| 42 | return UINT64_C(1) << (PAGE_BITS + level * PAGE_LEVEL_BITS); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Fill a ptable with absent entries. |
| 47 | */ |
| 48 | static void init_absent(pte_t *table) |
| 49 | { |
| 50 | for (uint64_t i = 0; i < ENTRY_COUNT; ++i) { |
| 51 | table[i] = ABSENT_ENTRY; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Fill a ptable with block entries. |
| 57 | */ |
| 58 | static void init_blocks(pte_t *table, int level, paddr_t start_address, |
| 59 | uint64_t attrs) |
| 60 | { |
| 61 | for (uint64_t i = 0; i < ENTRY_COUNT; ++i) { |
| 62 | table[i] = arch_mm_block_pte( |
| 63 | level, pa_add(start_address, i * mm_entry_size(level)), |
| 64 | attrs); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Defragging an entirely empty table should have no effect. |
| 70 | */ |
| 71 | TEST(mm, ptable_defrag_empty) |
| 72 | { |
| 73 | auto test_heap = std::make_unique<uint8_t[]>(TEST_HEAP_SIZE); |
| 74 | halloc_init((size_t)test_heap.get(), TEST_HEAP_SIZE); |
| 75 | |
| 76 | pte_t *table = (pte_t *)halloc_aligned(PAGE_SIZE, PAGE_SIZE); |
| 77 | init_absent(table); |
| 78 | struct mm_ptable ptable; |
| 79 | ptable.table = pa_init((uintpaddr_t)table); |
| 80 | |
| 81 | mm_ptable_defrag(&ptable, 0); |
| 82 | |
| 83 | for (uint64_t i = 0; i < ENTRY_COUNT; ++i) { |
| 84 | EXPECT_THAT(table[i], Eq(ABSENT_ENTRY)) << "i=" << i; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Defragging a table with some empty subtables (even nested) should result in |
| 90 | * an empty table. |
| 91 | */ |
| 92 | TEST(mm, ptable_defrag_empty_subtables) |
| 93 | { |
| 94 | auto test_heap = std::make_unique<uint8_t[]>(TEST_HEAP_SIZE); |
| 95 | halloc_init((size_t)test_heap.get(), TEST_HEAP_SIZE); |
| 96 | |
| 97 | pte_t *subtable_a = (pte_t *)halloc_aligned(PAGE_SIZE, PAGE_SIZE); |
| 98 | pte_t *subtable_aa = (pte_t *)halloc_aligned(PAGE_SIZE, PAGE_SIZE); |
| 99 | pte_t *subtable_b = (pte_t *)halloc_aligned(PAGE_SIZE, PAGE_SIZE); |
| 100 | pte_t *table = (pte_t *)halloc_aligned(PAGE_SIZE, PAGE_SIZE); |
| 101 | init_absent(subtable_a); |
| 102 | init_absent(subtable_aa); |
| 103 | init_absent(subtable_b); |
| 104 | init_absent(table); |
| 105 | |
| 106 | subtable_a[3] = arch_mm_table_pte(TOP_LEVEL - 1, |
| 107 | pa_init((uintpaddr_t)subtable_aa)); |
| 108 | table[0] = |
| 109 | arch_mm_table_pte(TOP_LEVEL, pa_init((uintpaddr_t)subtable_a)); |
| 110 | table[5] = |
| 111 | arch_mm_table_pte(TOP_LEVEL, pa_init((uintpaddr_t)subtable_b)); |
| 112 | |
| 113 | struct mm_ptable ptable; |
| 114 | ptable.table = pa_init((uintpaddr_t)table); |
| 115 | |
| 116 | mm_ptable_defrag(&ptable, 0); |
| 117 | |
| 118 | for (uint64_t i = 0; i < ENTRY_COUNT; ++i) { |
| 119 | EXPECT_THAT(table[i], Eq(ABSENT_ENTRY)) << "i=" << i; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Any subtable with all blocks with the same attributes should be replaced |
| 125 | * with a single block. |
| 126 | */ |
| 127 | TEST(mm, ptable_defrag_block_subtables) |
| 128 | { |
| 129 | auto test_heap = std::make_unique<uint8_t[]>(TEST_HEAP_SIZE); |
| 130 | halloc_init((size_t)test_heap.get(), TEST_HEAP_SIZE); |
| 131 | |
| 132 | pte_t *subtable_a = (pte_t *)halloc_aligned(PAGE_SIZE, PAGE_SIZE); |
| 133 | pte_t *subtable_aa = (pte_t *)halloc_aligned(PAGE_SIZE, PAGE_SIZE); |
| 134 | pte_t *subtable_b = (pte_t *)halloc_aligned(PAGE_SIZE, PAGE_SIZE); |
| 135 | pte_t *table = (pte_t *)halloc_aligned(PAGE_SIZE, PAGE_SIZE); |
| 136 | init_blocks(subtable_a, TOP_LEVEL - 1, pa_init(0), 0); |
| 137 | init_blocks(subtable_aa, TOP_LEVEL - 2, |
| 138 | pa_init(3 * mm_entry_size(TOP_LEVEL - 1)), 0); |
| 139 | init_blocks(subtable_b, TOP_LEVEL - 1, |
| 140 | pa_init(5 * mm_entry_size(TOP_LEVEL)), 0); |
| 141 | init_blocks(table, TOP_LEVEL, pa_init(0), 0); |
| 142 | |
| 143 | subtable_a[3] = arch_mm_table_pte(TOP_LEVEL - 1, |
| 144 | pa_init((uintpaddr_t)subtable_aa)); |
| 145 | table[0] = |
| 146 | arch_mm_table_pte(TOP_LEVEL, pa_init((uintpaddr_t)subtable_a)); |
| 147 | table[5] = |
| 148 | arch_mm_table_pte(TOP_LEVEL, pa_init((uintpaddr_t)subtable_b)); |
| 149 | |
| 150 | struct mm_ptable ptable; |
| 151 | ptable.table = pa_init((uintpaddr_t)table); |
| 152 | |
| 153 | mm_ptable_defrag(&ptable, 0); |
| 154 | |
| 155 | for (uint64_t i = 0; i < ENTRY_COUNT; ++i) { |
| 156 | EXPECT_TRUE(arch_mm_pte_is_present(table[i], TOP_LEVEL)) |
| 157 | << "i=" << i; |
| 158 | EXPECT_TRUE(arch_mm_pte_is_block(table[i], TOP_LEVEL)) |
| 159 | << "i=" << i; |
| 160 | EXPECT_THAT(pa_addr(arch_mm_block_from_pte(table[i])), |
| 161 | Eq(i * mm_entry_size(TOP_LEVEL))) |
| 162 | << "i=" << i; |
| 163 | } |
| 164 | } |