blob: 1612c88439b369973383a9d1a552c21376a84513 [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
Andrew Walbran692b3252019-03-07 15:51:31 +00002 * Copyright 2018 The Hafnium Authors.
Andrew Scull18834872018-10-12 11:48:09 +01003 *
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 Scull18834872018-10-12 11:48:09 +01007 */
8
Andrew Scull18c78fc2018-08-20 12:57:41 +01009#include "hf/mm.h"
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010010
11#include <stdatomic.h>
12#include <stdint.h>
13
Maksims Svecovs134b8f92022-03-04 15:14:09 +000014#include "hf/arch/init.h"
Karl Meakin07a69ab2025-02-07 14:53:19 +000015#include "hf/arch/mm.h"
Maksims Svecovs134b8f92022-03-04 15:14:09 +000016
Andrew Scull877ae4b2019-07-02 12:52:33 +010017#include "hf/check.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010018#include "hf/dlog.h"
Andrew Scull5991ec92018-10-08 14:55:02 +010019#include "hf/layout.h"
Andrew Walbran48699362019-05-20 14:38:00 +010020#include "hf/plat/console.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010021#include "hf/static_assert.h"
Karl Meakin25954e32025-02-07 16:12:51 +000022#include "hf/std.h"
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010023
Andrew Walbran2400ed22018-09-27 14:45:58 +010024/**
25 * This file has functions for managing the level 1 and 2 page tables used by
26 * Hafnium. There is a level 1 mapping used by Hafnium itself to access memory,
27 * and then a level 2 mapping per VM. The design assumes that all page tables
28 * contain only 1-1 mappings, aligned on the block boundaries.
29 */
30
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +010031/*
32 * For stage 2, the input is an intermediate physical addresses rather than a
33 * virtual address so:
34 */
Andrew Scull80871322018-08-06 12:04:09 +010035static_assert(
36 sizeof(ptable_addr_t) == sizeof(uintpaddr_t),
37 "Currently, the same code manages the stage 1 and stage 2 page tables "
38 "which only works if the virtual and intermediate physical addresses "
39 "are the same size. It looks like that assumption might not be holding "
40 "so we need to check that everything is going to be ok.");
41
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010042static struct mm_ptable ptable;
Andrew Scull3c0a90a2019-07-01 11:55:53 +010043static struct spinlock ptable_lock;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010044
Andrew Scullda241972019-01-05 18:17:48 +000045static bool mm_stage2_invalidate = false;
46
47/**
48 * After calling this function, modifications to stage-2 page tables will use
49 * break-before-make and invalidate the TLB for the affected range.
50 */
51void mm_vm_enable_invalidation(void)
52{
53 mm_stage2_invalidate = true;
54}
55
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010056/**
Andrew Scull80871322018-08-06 12:04:09 +010057 * Rounds an address down to a page boundary.
58 */
59static ptable_addr_t mm_round_down_to_page(ptable_addr_t addr)
60{
Karl Meakin25954e32025-02-07 16:12:51 +000061 return align_down(addr, PAGE_SIZE);
Andrew Scull80871322018-08-06 12:04:09 +010062}
63
64/**
65 * Rounds an address up to a page boundary.
66 */
67static ptable_addr_t mm_round_up_to_page(ptable_addr_t addr)
68{
Karl Meakin25954e32025-02-07 16:12:51 +000069 return align_up(addr, PAGE_SIZE);
Andrew Scull80871322018-08-06 12:04:09 +010070}
71
72/**
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010073 * Calculates the size of the address space represented by a page table entry at
Karl Meakina3a9f952025-02-08 00:11:16 +000074 * the given level. See also Arm ARM, table D8-15
75 * - `level == 4`: 256 TiB (1 << 48)
76 * - `level == 3`: 512 GiB (1 << 39)
77 * - `level == 2`: 1 GiB (1 << 30)
78 * - `level == 1`: 2 MiB (1 << 21)
79 * - `level == 0`: 4 KiB (1 << 12)
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010080 */
Karl Meakin07a69ab2025-02-07 14:53:19 +000081static size_t mm_entry_size(mm_level_t level)
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010082{
Karl Meakina3a9f952025-02-08 00:11:16 +000083 assert(level <= 4);
Andrew Scull78d6fd92018-09-06 15:08:36 +010084 return UINT64_C(1) << (PAGE_BITS + level * PAGE_LEVEL_BITS);
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010085}
86
87/**
Karl Meakina3a9f952025-02-08 00:11:16 +000088 * Get the start address of the range mapped by the next block of the given
89 * level.
Andrew Scullcae45572018-12-13 15:46:30 +000090 */
91static ptable_addr_t mm_start_of_next_block(ptable_addr_t addr,
Karl Meakin25954e32025-02-07 16:12:51 +000092 mm_level_t level)
Andrew Scullcae45572018-12-13 15:46:30 +000093{
Karl Meakina3a9f952025-02-08 00:11:16 +000094 assert(level <= 4);
Karl Meakin25954e32025-02-07 16:12:51 +000095 return align_up(addr + 1, mm_entry_size(level));
Andrew Scullcae45572018-12-13 15:46:30 +000096}
97
98/**
Andrew Scull80871322018-08-06 12:04:09 +010099 * For a given address, calculates the maximum (plus one) address that can be
100 * represented by the same table at the given level.
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100101 */
Karl Meakin07a69ab2025-02-07 14:53:19 +0000102static ptable_addr_t mm_level_end(ptable_addr_t addr, mm_level_t level)
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100103{
104 size_t offset = PAGE_BITS + (level + 1) * PAGE_LEVEL_BITS;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000105
Andrew Scull80871322018-08-06 12:04:09 +0100106 return ((addr >> offset) + 1) << offset;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100107}
108
109/**
Andrew Scull80871322018-08-06 12:04:09 +0100110 * For a given address, calculates the index at which its entry is stored in a
Karl Meakina3a9f952025-02-08 00:11:16 +0000111 * table at the given level. See also Arm ARM, table D8-14
112 * - `level == 4`: bits[51:48]
113 * - `level == 3`: bits[47:39]
114 * - `level == 2`: bits[38:30]
115 * - `level == 1`: bits[29:21]
116 * - `level == 0`: bits[20:12]
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100117 */
Karl Meakin07a69ab2025-02-07 14:53:19 +0000118static size_t mm_index(ptable_addr_t addr, mm_level_t level)
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100119{
Andrew Scull80871322018-08-06 12:04:09 +0100120 ptable_addr_t v = addr >> (PAGE_BITS + level * PAGE_LEVEL_BITS);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000121
Andrew Scull78d6fd92018-09-06 15:08:36 +0100122 return v & ((UINT64_C(1) << PAGE_LEVEL_BITS) - 1);
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100123}
124
125/**
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000126 * Allocates a new page table.
Andrew Scull4e5f8142018-10-12 14:37:19 +0100127 */
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000128static struct mm_page_table *mm_alloc_page_tables(size_t count,
129 struct mpool *ppool)
Andrew Scull4e5f8142018-10-12 14:37:19 +0100130{
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000131 if (count == 1) {
132 return mpool_alloc(ppool);
Andrew Scull4e5f8142018-10-12 14:37:19 +0100133 }
134
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000135 return mpool_alloc_contiguous(ppool, count, count);
Andrew Scull4e5f8142018-10-12 14:37:19 +0100136}
137
138/**
Karl Meakina3a9f952025-02-08 00:11:16 +0000139 * Returns the root level in the page table given the flags.
Andrew Scullda3df7f2019-01-05 17:49:27 +0000140 */
Karl Meakin0f506a12025-02-08 23:28:45 +0000141static mm_level_t mm_root_level(const struct mm_ptable *ptable)
Andrew Scullda3df7f2019-01-05 17:49:27 +0000142{
Karl Meakin0f506a12025-02-08 23:28:45 +0000143 return ptable->stage1 ? arch_mm_stage1_root_level()
144 : arch_mm_stage2_root_level();
Andrew Scullda3df7f2019-01-05 17:49:27 +0000145}
146
147/**
148 * Returns the number of root-level tables given the flags.
149 */
Karl Meakin0f506a12025-02-08 23:28:45 +0000150static uint8_t mm_root_table_count(const struct mm_ptable *ptable)
Andrew Scullda3df7f2019-01-05 17:49:27 +0000151{
Karl Meakin0f506a12025-02-08 23:28:45 +0000152 return ptable->stage1 ? arch_mm_stage1_root_table_count()
153 : arch_mm_stage2_root_table_count();
Andrew Scullda3df7f2019-01-05 17:49:27 +0000154}
155
156/**
Wedson Almeida Filho7c913232018-11-23 18:20:29 +0000157 * Invalidates the TLB for the given address range.
158 */
Karl Meakin00dbf1b2025-02-07 17:58:39 +0000159static void mm_invalidate_tlb(const struct mm_ptable *ptable,
160 ptable_addr_t begin, ptable_addr_t end,
Karl Meakin0f506a12025-02-08 23:28:45 +0000161 bool non_secure)
Wedson Almeida Filho7c913232018-11-23 18:20:29 +0000162{
Karl Meakin0f506a12025-02-08 23:28:45 +0000163 if (ptable->stage1) {
Karl Meakin00dbf1b2025-02-07 17:58:39 +0000164 arch_mm_invalidate_stage1_range(ptable->id, va_init(begin),
Raghu Krishnamurthy8fdd6df2021-02-03 18:30:59 -0800165 va_init(end));
Wedson Almeida Filho7c913232018-11-23 18:20:29 +0000166 } else {
Karl Meakin00dbf1b2025-02-07 17:58:39 +0000167 arch_mm_invalidate_stage2_range(ptable->id, ipa_init(begin),
Olivier Deprez6f400372022-03-07 09:31:08 +0100168 ipa_init(end), non_secure);
Wedson Almeida Filho7c913232018-11-23 18:20:29 +0000169 }
170}
171
172/**
173 * Frees all page-table-related memory associated with the given pte at the
174 * given level, including any subtables recursively.
175 */
Daniel Boulby8adf7482021-09-22 15:12:44 +0100176// NOLINTNEXTLINE(misc-no-recursion)
Karl Meakin07a69ab2025-02-07 14:53:19 +0000177static void mm_free_page_pte(pte_t pte, mm_level_t level, struct mpool *ppool)
Wedson Almeida Filho7c913232018-11-23 18:20:29 +0000178{
179 struct mm_page_table *table;
Wedson Almeida Filho7c913232018-11-23 18:20:29 +0000180
181 if (!arch_mm_pte_is_table(pte, level)) {
182 return;
183 }
184
185 /* Recursively free any subtables. */
Karl Meakinaacfd4f2025-02-08 19:30:52 +0000186 table = arch_mm_table_from_pte(pte, level);
Karl Meakind64aaf82025-02-08 01:12:55 +0000187 for (size_t i = 0; i < MM_PTE_PER_PAGE; ++i) {
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000188 mm_free_page_pte(table->entries[i], level - 1, ppool);
Wedson Almeida Filho7c913232018-11-23 18:20:29 +0000189 }
190
191 /* Free the table itself. */
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000192 mpool_free(ppool, table);
Wedson Almeida Filho7c913232018-11-23 18:20:29 +0000193}
194
195/**
David Brazdil711fbe92019-08-06 13:39:58 +0100196 * Returns the first address which cannot be encoded in page tables given by
197 * `flags`. It is the exclusive end of the address space created by the tables.
198 */
Karl Meakin0f506a12025-02-08 23:28:45 +0000199ptable_addr_t mm_ptable_addr_space_end(const struct mm_ptable *ptable)
David Brazdil711fbe92019-08-06 13:39:58 +0100200{
Karl Meakin0f506a12025-02-08 23:28:45 +0000201 return mm_root_table_count(ptable) *
202 mm_entry_size(mm_root_level(ptable));
David Brazdil711fbe92019-08-06 13:39:58 +0100203}
204
205/**
Andrew Scullda3df7f2019-01-05 17:49:27 +0000206 * Initialises the given page table.
207 */
Karl Meakin0f506a12025-02-08 23:28:45 +0000208bool mm_ptable_init(struct mm_ptable *ptable, mm_asid_t id, bool stage1,
209 struct mpool *ppool)
Andrew Scullda3df7f2019-01-05 17:49:27 +0000210{
Karl Meakine1aeb1d2025-02-08 00:35:14 +0000211 struct mm_page_table *root_tables;
Karl Meakin0f506a12025-02-08 23:28:45 +0000212 uint8_t root_table_count = stage1 ? arch_mm_stage1_root_table_count()
213 : arch_mm_stage2_root_table_count();
214 mm_level_t root_level = stage1 ? arch_mm_stage1_root_level()
215 : arch_mm_stage2_root_level();
Andrew Scullda3df7f2019-01-05 17:49:27 +0000216
Karl Meakine1aeb1d2025-02-08 00:35:14 +0000217 root_tables = mm_alloc_page_tables(root_table_count, ppool);
218 if (root_tables == NULL) {
Andrew Scullda3df7f2019-01-05 17:49:27 +0000219 return false;
220 }
221
Karl Meakind64aaf82025-02-08 01:12:55 +0000222 for (size_t i = 0; i < root_table_count; i++) {
223 for (size_t j = 0; j < MM_PTE_PER_PAGE; j++) {
Karl Meakine1aeb1d2025-02-08 00:35:14 +0000224 root_tables[i].entries[j] =
Karl Meakina3a9f952025-02-08 00:11:16 +0000225 arch_mm_absent_pte(root_level - 1);
Andrew Scullda3df7f2019-01-05 17:49:27 +0000226 }
227 }
228
229 /*
230 * TODO: halloc could return a virtual or physical address if mm not
231 * enabled?
232 */
Karl Meakind64aaf82025-02-08 01:12:55 +0000233 ptable->id = id;
Karl Meakin0f506a12025-02-08 23:28:45 +0000234 ptable->root_tables = root_tables;
235 ptable->stage1 = stage1;
Andrew Scullda3df7f2019-01-05 17:49:27 +0000236 return true;
237}
238
239/**
240 * Frees all memory associated with the give page table.
241 */
Karl Meakin0f506a12025-02-08 23:28:45 +0000242static void mm_ptable_fini(const struct mm_ptable *ptable, struct mpool *ppool)
Andrew Scullda3df7f2019-01-05 17:49:27 +0000243{
Karl Meakine1aeb1d2025-02-08 00:35:14 +0000244 struct mm_page_table *root_tables = ptable->root_tables;
Karl Meakin0f506a12025-02-08 23:28:45 +0000245 mm_level_t root_level = mm_root_level(ptable);
246 uint8_t root_table_count = mm_root_table_count(ptable);
Andrew Scullda3df7f2019-01-05 17:49:27 +0000247
Karl Meakind64aaf82025-02-08 01:12:55 +0000248 for (size_t i = 0; i < root_table_count; ++i) {
249 for (size_t j = 0; j < MM_PTE_PER_PAGE; ++j) {
Karl Meakine1aeb1d2025-02-08 00:35:14 +0000250 mm_free_page_pte(root_tables[i].entries[j],
251 root_level - 1, ppool);
Andrew Scullda3df7f2019-01-05 17:49:27 +0000252 }
253 }
254
Karl Meakine1aeb1d2025-02-08 00:35:14 +0000255 mpool_add_chunk(ppool, root_tables,
Andrew Scullda3df7f2019-01-05 17:49:27 +0000256 sizeof(struct mm_page_table) * root_table_count);
257}
258
259/**
Wedson Almeida Filho7c913232018-11-23 18:20:29 +0000260 * Replaces a page table entry with the given value. If both old and new values
Andrew Scullc66a04d2018-12-07 13:41:56 +0000261 * are valid, it performs a break-before-make sequence where it first writes an
262 * invalid value to the PTE, flushes the TLB, then writes the actual new value.
263 * This is to prevent cases where CPUs have different 'valid' values in their
264 * TLBs, which may result in issues for example in cache coherency.
Wedson Almeida Filho7c913232018-11-23 18:20:29 +0000265 */
Karl Meakin00dbf1b2025-02-07 17:58:39 +0000266static void mm_replace_entry(const struct mm_ptable *ptable,
267 ptable_addr_t begin, pte_t *pte, pte_t new_pte,
Karl Meakin0f506a12025-02-08 23:28:45 +0000268 mm_level_t level, bool non_secure,
269 struct mpool *ppool)
Wedson Almeida Filho7c913232018-11-23 18:20:29 +0000270{
271 pte_t v = *pte;
272
273 /*
274 * We need to do the break-before-make sequence if both values are
Andrew Scull3cd9e262019-01-08 17:59:22 +0000275 * present and the TLB is being invalidated.
Wedson Almeida Filho7c913232018-11-23 18:20:29 +0000276 */
Karl Meakin0f506a12025-02-08 23:28:45 +0000277 if ((ptable->stage1 || mm_stage2_invalidate) &&
Raghu Krishnamurthyc1012d62021-01-24 19:19:31 -0800278 arch_mm_pte_is_valid(v, level)) {
Wedson Almeida Filho7c913232018-11-23 18:20:29 +0000279 *pte = arch_mm_absent_pte(level);
Karl Meakin00dbf1b2025-02-07 17:58:39 +0000280 mm_invalidate_tlb(ptable, begin, begin + mm_entry_size(level),
Karl Meakin0f506a12025-02-08 23:28:45 +0000281 non_secure);
Wedson Almeida Filho7c913232018-11-23 18:20:29 +0000282 }
283
284 /* Assign the new pte. */
285 *pte = new_pte;
286
287 /* Free pages that aren't in use anymore. */
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000288 mm_free_page_pte(v, level, ppool);
Wedson Almeida Filho7c913232018-11-23 18:20:29 +0000289}
290
291/**
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100292 * Populates the provided page table entry with a reference to another table if
293 * needed, that is, if it does not yet point to another table.
294 *
295 * Returns a pointer to the table the entry now points to.
296 */
Karl Meakin0f506a12025-02-08 23:28:45 +0000297static struct mm_page_table *mm_populate_table_pte(struct mm_ptable *ptable,
298 ptable_addr_t begin,
299 pte_t *pte, mm_level_t level,
300 bool non_secure,
301 struct mpool *ppool)
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100302{
Andrew Scull4e5f8142018-10-12 14:37:19 +0100303 struct mm_page_table *ntable;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100304 pte_t v = *pte;
305 pte_t new_pte;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100306 size_t inc;
Karl Meakin07a69ab2025-02-07 14:53:19 +0000307 mm_level_t level_below = level - 1;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100308
309 /* Just return pointer to table if it's already populated. */
Andrew Scull78d6fd92018-09-06 15:08:36 +0100310 if (arch_mm_pte_is_table(v, level)) {
Karl Meakinaacfd4f2025-02-08 19:30:52 +0000311 return arch_mm_table_from_pte(v, level);
Andrew Scull7364a8e2018-07-19 15:39:29 +0100312 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100313
314 /* Allocate a new table. */
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000315 ntable = mm_alloc_page_tables(1, ppool);
Andrew Scull4e5f8142018-10-12 14:37:19 +0100316 if (ntable == NULL) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000317 dlog_error("Failed to allocate memory for page table\n");
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100318 return NULL;
319 }
320
321 /* Determine template for new pte and its increment. */
Andrew Scull78d6fd92018-09-06 15:08:36 +0100322 if (arch_mm_pte_is_block(v, level)) {
Andrew Scull78d6fd92018-09-06 15:08:36 +0100323 inc = mm_entry_size(level_below);
324 new_pte = arch_mm_block_pte(level_below,
Andrew Scull3681b8d2018-12-12 14:22:59 +0000325 arch_mm_block_from_pte(v, level),
326 arch_mm_pte_attrs(v, level));
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100327 } else {
Andrew Scull78d6fd92018-09-06 15:08:36 +0100328 inc = 0;
Andrew Walbran1b99f9d2018-10-03 17:54:40 +0100329 new_pte = arch_mm_absent_pte(level_below);
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100330 }
331
332 /* Initialise entries in the new table. */
Karl Meakind64aaf82025-02-08 01:12:55 +0000333 for (size_t i = 0; i < MM_PTE_PER_PAGE; i++) {
Andrew Scull4e5f8142018-10-12 14:37:19 +0100334 ntable->entries[i] = new_pte;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100335 new_pte += inc;
336 }
337
Wedson Almeida Filho7c913232018-11-23 18:20:29 +0000338 /* Ensure initialisation is visible before updating the pte. */
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100339 atomic_thread_fence(memory_order_release);
Wedson Almeida Filho7c913232018-11-23 18:20:29 +0000340
341 /* Replace the pte entry, doing a break-before-make if needed. */
Karl Meakin00dbf1b2025-02-07 17:58:39 +0000342 mm_replace_entry(ptable, begin, pte,
Wedson Almeida Filho7c913232018-11-23 18:20:29 +0000343 arch_mm_table_pte(level, pa_init((uintpaddr_t)ntable)),
Karl Meakin0f506a12025-02-08 23:28:45 +0000344 level, non_secure, ppool);
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100345
346 return ntable;
347}
348
349/**
Andrew Scull80871322018-08-06 12:04:09 +0100350 * Updates the page table at the given level to map the given address range to a
Andrew Walbran6324fc92018-10-03 11:46:43 +0100351 * physical range using the provided (architecture-specific) attributes. Or if
Karl Meakin1fd4b822025-02-01 17:13:47 +0000352 * `flags.unmap` is set, unmap the given range instead.
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100353 *
354 * This function calls itself recursively if it needs to update additional
355 * levels, but the recursion is bound by the maximum number of levels in a page
356 * table.
357 */
Daniel Boulby8adf7482021-09-22 15:12:44 +0100358// NOLINTNEXTLINE(misc-no-recursion)
Karl Meakin00dbf1b2025-02-07 17:58:39 +0000359static bool mm_map_level(struct mm_ptable *ptable, ptable_addr_t begin,
Karl Meakin25954e32025-02-07 16:12:51 +0000360 ptable_addr_t end, mm_attr_t attrs,
Karl Meakin00dbf1b2025-02-07 17:58:39 +0000361 struct mm_page_table *child_table, mm_level_t level,
362 struct mm_flags flags, struct mpool *ppool)
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100363{
Karl Meakin00dbf1b2025-02-07 17:58:39 +0000364 pte_t *pte = &child_table->entries[mm_index(begin, level)];
Andrew Scull80871322018-08-06 12:04:09 +0100365 ptable_addr_t level_end = mm_level_end(begin, level);
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100366 size_t entry_size = mm_entry_size(level);
Karl Meakin1fd4b822025-02-01 17:13:47 +0000367 bool commit = flags.commit;
368 bool unmap = flags.unmap;
369 bool non_secure = ((attrs & (1ULL << 57)) != 0);
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100370
Andrew Scull265ada92018-07-30 15:19:01 +0100371 /* Cap end so that we don't go over the current level max. */
372 if (end > level_end) {
373 end = level_end;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100374 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100375
376 /* Fill each entry in the table. */
Andrew Scull265ada92018-07-30 15:19:01 +0100377 while (begin < end) {
Andrew Walbran6324fc92018-10-03 11:46:43 +0100378 if (unmap ? !arch_mm_pte_is_present(*pte, level)
379 : arch_mm_pte_is_block(*pte, level) &&
Andrew Scull3681b8d2018-12-12 14:22:59 +0000380 arch_mm_pte_attrs(*pte, level) == attrs) {
Andrew Walbran6324fc92018-10-03 11:46:43 +0100381 /*
382 * If the entry is already mapped with the right
383 * attributes, or already absent in the case of
384 * unmapping, no need to do anything; carry on to the
385 * next entry.
386 */
387 } else if ((end - begin) >= entry_size &&
388 (unmap || arch_mm_is_block_allowed(level)) &&
Karl Meakin25954e32025-02-07 16:12:51 +0000389 is_aligned(begin, entry_size)) {
Andrew Walbran6324fc92018-10-03 11:46:43 +0100390 /*
391 * If the entire entry is within the region we want to
392 * map, map/unmap the whole entry.
393 */
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100394 if (commit) {
Wedson Almeida Filho7c913232018-11-23 18:20:29 +0000395 pte_t new_pte =
396 unmap ? arch_mm_absent_pte(level)
Karl Meakin25954e32025-02-07 16:12:51 +0000397 : arch_mm_block_pte(
398 level, pa_init(begin),
399 attrs);
Karl Meakin00dbf1b2025-02-07 17:58:39 +0000400 mm_replace_entry(ptable, begin, pte, new_pte,
Karl Meakin0f506a12025-02-08 23:28:45 +0000401 level, non_secure, ppool);
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100402 }
403 } else {
Andrew Walbran6324fc92018-10-03 11:46:43 +0100404 /*
405 * If the entry is already a subtable get it; otherwise
406 * replace it with an equivalent subtable and get that.
407 */
Karl Meakin0f506a12025-02-08 23:28:45 +0000408 struct mm_page_table *nt = mm_populate_table_pte(
409 ptable, begin, pte, level, non_secure, ppool);
Andrew Scull4e5f8142018-10-12 14:37:19 +0100410 if (nt == NULL) {
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100411 return false;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100412 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100413
Andrew Walbran6324fc92018-10-03 11:46:43 +0100414 /*
415 * Recurse to map/unmap the appropriate entries within
416 * the subtable.
417 */
Karl Meakin25954e32025-02-07 16:12:51 +0000418 if (!mm_map_level(ptable, begin, end, attrs, nt,
Karl Meakin00dbf1b2025-02-07 17:58:39 +0000419 level - 1, flags, ppool)) {
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100420 return false;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100421 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100422 }
423
Karl Meakin25954e32025-02-07 16:12:51 +0000424 begin = mm_start_of_next_block(begin, level);
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100425 pte++;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100426 }
427
428 return true;
429}
430
431/**
Andrew Scull1ba470e2018-10-31 15:14:31 +0000432 * Updates the page table from the root to map the given address range to a
Karl Meakinaac38012025-02-07 23:57:37 +0000433 * physical range using the provided (architecture-specific) attributes.
434 *
435 * Flags:
436 * - `flags.unmap`: unmap the given range instead of mapping it.
437 * - `flags.commit`: the change is only committed if this flag is set.
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100438 */
Karl Meakind64aaf82025-02-08 01:12:55 +0000439static bool mm_ptable_identity_map(struct mm_ptable *ptable, paddr_t pa_begin,
Karl Meakin07a69ab2025-02-07 14:53:19 +0000440 paddr_t pa_end, mm_attr_t attrs,
Karl Meakin1fd4b822025-02-01 17:13:47 +0000441 struct mm_flags flags, struct mpool *ppool)
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100442{
Karl Meakin0f506a12025-02-08 23:28:45 +0000443 mm_level_t root_level = mm_root_level(ptable);
444 ptable_addr_t ptable_end = mm_ptable_addr_space_end(ptable);
Andrew Scull1ba470e2018-10-31 15:14:31 +0000445 ptable_addr_t end = mm_round_up_to_page(pa_addr(pa_end));
Karl Meakinc17ab272025-02-08 03:29:17 +0000446 ptable_addr_t begin = mm_round_down_to_page(pa_addr(pa_begin));
Karl Meakine1aeb1d2025-02-08 00:35:14 +0000447 struct mm_page_table *root_table =
448 &ptable->root_tables[mm_index(begin, root_level)];
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100449
Andrew Scull1ba470e2018-10-31 15:14:31 +0000450 /*
Karl Meakina3a9f952025-02-08 00:11:16 +0000451 * Assert condition to communicate the API constraint of
452 * mm_root_level(), that isn't encoded in the types, to the static
453 * analyzer.
Andrew Scull1ba470e2018-10-31 15:14:31 +0000454 */
Karl Meakina3a9f952025-02-08 00:11:16 +0000455 assert(root_level >= 3);
Andrew Scull1ba470e2018-10-31 15:14:31 +0000456
457 /* Cap end to stay within the bounds of the page table. */
458 if (end > ptable_end) {
459 end = ptable_end;
460 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100461
Karl Meakinaac38012025-02-07 23:57:37 +0000462 while (begin < end) {
463 if (!mm_map_level(ptable, begin, end, attrs, root_table,
464 root_level - 1, flags, ppool)) {
465 return false;
466 }
467 begin = mm_start_of_next_block(begin, root_level);
468 root_table++;
Andrew Walbran58a6e542019-11-19 14:23:15 +0000469 }
470
Raghu Krishnamurthyc1012d62021-01-24 19:19:31 -0800471 /*
472 * All TLB invalidations must be complete already if any entries were
473 * replaced by mm_replace_entry. Sync all page table writes so that code
474 * following this can use them.
475 */
476 arch_mm_sync_table_writes();
Andrew Walbran58a6e542019-11-19 14:23:15 +0000477
478 return true;
479}
480
Andrew Scull4e83cef2019-11-19 14:17:54 +0000481/*
482 * Prepares the given page table for the given address mapping such that it
483 * will be able to commit the change without failure. It does so by ensuring
484 * the smallest granularity needed is available. This remains valid provided
Fuad Tabba9dc276f2020-07-16 09:29:32 +0100485 * subsequent operations do not decrease the granularity.
Andrew Scull4e83cef2019-11-19 14:17:54 +0000486 *
487 * In particular, multiple calls to this function will result in the
488 * corresponding calls to commit the changes to succeed.
489 */
Karl Meakind64aaf82025-02-08 01:12:55 +0000490static bool mm_ptable_identity_prepare(struct mm_ptable *ptable,
491 paddr_t pa_begin, paddr_t pa_end,
Karl Meakin07a69ab2025-02-07 14:53:19 +0000492 mm_attr_t attrs, struct mm_flags flags,
Karl Meakind64aaf82025-02-08 01:12:55 +0000493 struct mpool *ppool)
Andrew Scull4e83cef2019-11-19 14:17:54 +0000494{
Karl Meakin1fd4b822025-02-01 17:13:47 +0000495 flags.commit = false;
Karl Meakind64aaf82025-02-08 01:12:55 +0000496 return mm_ptable_identity_map(ptable, pa_begin, pa_end, attrs, flags,
497 ppool);
Andrew Scull4e83cef2019-11-19 14:17:54 +0000498}
499
500/**
501 * Commits the given address mapping to the page table assuming the operation
502 * cannot fail. `mm_ptable_identity_prepare` must used correctly before this to
503 * ensure this condition.
504 *
505 * Without the table being properly prepared, the commit may only partially
506 * complete if it runs out of memory resulting in an inconsistent state that
507 * isn't handled.
508 *
509 * Since the non-failure assumtion is used in the reasoning about the atomicity
510 * of higher level memory operations, any detected violations result in a panic.
511 *
512 * TODO: remove ppool argument to be sure no changes are made.
513 */
Karl Meakind64aaf82025-02-08 01:12:55 +0000514static void mm_ptable_identity_commit(struct mm_ptable *ptable,
515 paddr_t pa_begin, paddr_t pa_end,
Karl Meakin07a69ab2025-02-07 14:53:19 +0000516 mm_attr_t attrs, struct mm_flags flags,
Andrew Scull4e83cef2019-11-19 14:17:54 +0000517 struct mpool *ppool)
518{
Karl Meakin1fd4b822025-02-01 17:13:47 +0000519 flags.commit = true;
520 CHECK(mm_ptable_identity_map(ptable, pa_begin, pa_end, attrs, flags,
521 ppool));
Andrew Scull4e83cef2019-11-19 14:17:54 +0000522}
523
Andrew Walbran58a6e542019-11-19 14:23:15 +0000524/**
525 * Updates the given table such that the given physical address range is mapped
526 * or not mapped into the address space with the architecture-agnostic mode
Andrew Scull4e83cef2019-11-19 14:17:54 +0000527 * provided.
528 *
529 * The page table is updated using the separate prepare and commit stages so
530 * that, on failure, a partial update of the address space cannot happen. The
531 * table may be left with extra internal tables but the address space is
532 * unchanged.
Andrew Walbran58a6e542019-11-19 14:23:15 +0000533 */
Karl Meakind64aaf82025-02-08 01:12:55 +0000534static bool mm_ptable_identity_update(struct mm_ptable *ptable,
535 paddr_t pa_begin, paddr_t pa_end,
Karl Meakin07a69ab2025-02-07 14:53:19 +0000536 mm_attr_t attrs, struct mm_flags flags,
Andrew Walbran58a6e542019-11-19 14:23:15 +0000537 struct mpool *ppool)
538{
Karl Meakind64aaf82025-02-08 01:12:55 +0000539 if (!mm_ptable_identity_prepare(ptable, pa_begin, pa_end, attrs, flags,
Andrew Scull4e83cef2019-11-19 14:17:54 +0000540 ppool)) {
541 return false;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100542 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100543
Karl Meakind64aaf82025-02-08 01:12:55 +0000544 mm_ptable_identity_commit(ptable, pa_begin, pa_end, attrs, flags,
545 ppool);
Andrew Scull4e83cef2019-11-19 14:17:54 +0000546
547 return true;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100548}
549
Karl Meakinc88ad412025-02-11 16:04:49 +0000550static void mm_dump_entries(const pte_t *entries, mm_level_t level,
551 uint32_t indent);
552
553static void mm_dump_block_entry(pte_t entry, mm_level_t level, uint32_t indent)
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100554{
Karl Meakinc88ad412025-02-11 16:04:49 +0000555 mm_attr_t attrs = arch_mm_pte_attrs(entry, level);
556 paddr_t addr = arch_mm_block_from_pte(entry, level);
557
558 if (arch_mm_pte_is_valid(entry, level)) {
559 if (level == 0) {
560 dlog("page {\n");
561 } else {
562 dlog("block {\n");
563 }
564 } else {
565 dlog("invalid_block {\n");
566 }
567
568 indent += 1;
569 {
570 dlog_indent(indent, ".addr = %#016lx\n", pa_addr(addr));
571 dlog_indent(indent, ".attrs = %#016lx\n", attrs);
572 }
573 indent -= 1;
574 dlog_indent(indent, "}");
575}
576
577// NOLINTNEXTLINE(misc-no-recursion)
578static void mm_dump_table_entry(pte_t entry, mm_level_t level, uint32_t indent)
579{
580 dlog("table {\n");
581 indent += 1;
582 {
583 mm_attr_t attrs = arch_mm_pte_attrs(entry, level);
Karl Meakinc88ad412025-02-11 16:04:49 +0000584 const struct mm_page_table *child_table =
Karl Meakinaacfd4f2025-02-08 19:30:52 +0000585 arch_mm_table_from_pte(entry, level);
586 paddr_t addr = pa_init((uintpaddr_t)child_table);
Karl Meakinc88ad412025-02-11 16:04:49 +0000587
588 dlog_indent(indent, ".pte = %#016lx,\n", entry);
589 dlog_indent(indent, ".attrs = %#016lx,\n", attrs);
590 dlog_indent(indent, ".addr = %#016lx,\n", pa_addr(addr));
591 dlog_indent(indent, ".entries = ");
592 mm_dump_entries(child_table->entries, level - 1, indent);
593 dlog(",\n");
594 }
595 indent -= 1;
596 dlog_indent(indent, "}");
597}
598
599// NOLINTNEXTLINE(misc-no-recursion)
600static void mm_dump_entry(pte_t entry, mm_level_t level, uint32_t indent)
601{
602 switch (arch_mm_pte_type(entry, level)) {
603 case PTE_TYPE_ABSENT:
604 dlog("absent {}");
605 break;
606 case PTE_TYPE_INVALID_BLOCK:
607 case PTE_TYPE_VALID_BLOCK: {
608 mm_dump_block_entry(entry, level, indent);
609 break;
610 }
611 case PTE_TYPE_TABLE: {
612 mm_dump_table_entry(entry, level, indent);
613 break;
614 }
615 }
616}
617
618// NOLINTNEXTLINE(misc-no-recursion)
619static void mm_dump_entries(const pte_t *entries, mm_level_t level,
620 uint32_t indent)
621{
622 dlog("{\n");
623 indent += 1;
624
Karl Meakind64aaf82025-02-08 01:12:55 +0000625 for (size_t i = 0; i < MM_PTE_PER_PAGE; i++) {
Karl Meakinc88ad412025-02-11 16:04:49 +0000626 pte_t entry = entries[i];
Karl Meakin100b0b22025-02-08 00:59:25 +0000627
628 if (arch_mm_pte_is_absent(entry, level)) {
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100629 continue;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100630 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100631
Karl Meakinc88ad412025-02-11 16:04:49 +0000632 dlog_indent(indent, "[level = %u, index = %zu] = ", level, i);
633 mm_dump_entry(entry, level, indent);
634 dlog(",\n");
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100635 }
Karl Meakinc88ad412025-02-11 16:04:49 +0000636
637 indent -= 1;
638 dlog_indent(indent, "}");
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100639}
640
641/**
Wedson Almeida Filhoac8ad012018-12-17 18:00:29 +0000642 * Writes the given table to the debug log.
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100643 */
Karl Meakin0f506a12025-02-08 23:28:45 +0000644static void mm_ptable_dump(const struct mm_ptable *ptable)
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100645{
Karl Meakine1aeb1d2025-02-08 00:35:14 +0000646 struct mm_page_table *root_tables = ptable->root_tables;
Karl Meakin0f506a12025-02-08 23:28:45 +0000647 mm_level_t root_level = mm_root_level(ptable);
648 uint8_t root_table_count = mm_root_table_count(ptable);
Karl Meakinc88ad412025-02-11 16:04:49 +0000649 uint32_t indent = 0;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000650
Karl Meakinc88ad412025-02-11 16:04:49 +0000651 dlog_indent(indent, "mm_ptable {\n");
652 indent += 1;
653 {
654 dlog_indent(indent, ".stage = %s,\n",
Karl Meakin0f506a12025-02-08 23:28:45 +0000655 ptable->stage1 ? "stage1" : "stage2");
Karl Meakinc88ad412025-02-11 16:04:49 +0000656 dlog_indent(indent, ".id = %hu,\n", ptable->id);
657 dlog_indent(indent, ".root_tables = {\n");
658
659 indent += 1;
660 {
661 for (size_t i = 0; i < root_table_count; ++i) {
662 dlog_indent(
663 indent,
664 "[level = %u, index = %zu].entries = ",
665 root_level, i);
666 mm_dump_entries(root_tables[i].entries,
667 root_level - 1, indent);
668 dlog(",\n");
669 }
670 }
671 indent -= 1;
672 dlog_indent(indent, "},\n");
Andrew Scull1ba470e2018-10-31 15:14:31 +0000673 }
Karl Meakinc88ad412025-02-11 16:04:49 +0000674 indent -= 1;
675 dlog_indent(indent, "}\n");
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100676}
677
678/**
Wedson Almeida Filhoac8ad012018-12-17 18:00:29 +0000679 * Given the table PTE entries all have identical attributes, returns the single
Raghu Krishnamurthyc1012d62021-01-24 19:19:31 -0800680 * entry with which it can be replaced.
Andrew Walbran2400ed22018-09-27 14:45:58 +0100681 */
Karl Meakin07a69ab2025-02-07 14:53:19 +0000682static pte_t mm_merge_table_pte(pte_t table_pte, mm_level_t level)
Andrew Walbran2400ed22018-09-27 14:45:58 +0100683{
Andrew Scull4e5f8142018-10-12 14:37:19 +0100684 struct mm_page_table *table;
Karl Meakin07a69ab2025-02-07 14:53:19 +0000685 mm_attr_t block_attrs;
686 mm_attr_t table_attrs;
687 mm_attr_t combined_attrs;
Andrew Walbran2400ed22018-09-27 14:45:58 +0100688 paddr_t block_address;
689
Karl Meakinaacfd4f2025-02-08 19:30:52 +0000690 table = arch_mm_table_from_pte(table_pte, level);
Andrew Scullb6b9b562018-12-21 14:41:35 +0000691
692 if (!arch_mm_pte_is_present(table->entries[0], level - 1)) {
Andrew Scullb6b9b562018-12-21 14:41:35 +0000693 return arch_mm_absent_pte(level);
Andrew Walbran2400ed22018-09-27 14:45:58 +0100694 }
695
Andrew Scullb6b9b562018-12-21 14:41:35 +0000696 /* Might not be possible to merge the table into a single block. */
697 if (!arch_mm_is_block_allowed(level)) {
698 return table_pte;
699 }
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000700
Andrew Scullb6b9b562018-12-21 14:41:35 +0000701 /* Replace table with a single block, with equivalent attributes. */
Andrew Scull3681b8d2018-12-12 14:22:59 +0000702 block_attrs = arch_mm_pte_attrs(table->entries[0], level - 1);
Andrew Scullb6b9b562018-12-21 14:41:35 +0000703 table_attrs = arch_mm_pte_attrs(table_pte, level);
Andrew Walbran2400ed22018-09-27 14:45:58 +0100704 combined_attrs =
705 arch_mm_combine_table_entry_attrs(table_attrs, block_attrs);
Andrew Scull3681b8d2018-12-12 14:22:59 +0000706 block_address = arch_mm_block_from_pte(table->entries[0], level - 1);
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000707
Andrew Walbran2400ed22018-09-27 14:45:58 +0100708 return arch_mm_block_pte(level, block_address, combined_attrs);
709}
710
711/**
Wedson Almeida Filhoac8ad012018-12-17 18:00:29 +0000712 * Defragments the given PTE by recursively replacing any tables with blocks or
Andrew Scullb6b9b562018-12-21 14:41:35 +0000713 * absent entries where possible.
Andrew Walbran2400ed22018-09-27 14:45:58 +0100714 */
Daniel Boulby8adf7482021-09-22 15:12:44 +0100715// NOLINTNEXTLINE(misc-no-recursion)
Karl Meakin00dbf1b2025-02-07 17:58:39 +0000716static void mm_ptable_defrag_entry(struct mm_ptable *ptable,
717 ptable_addr_t base_addr, pte_t *entry,
Karl Meakin0f506a12025-02-08 23:28:45 +0000718 mm_level_t level, bool non_secure,
719 struct mpool *ppool)
Andrew Walbran2400ed22018-09-27 14:45:58 +0100720{
Karl Meakin00dbf1b2025-02-07 17:58:39 +0000721 struct mm_page_table *child_table;
Andrew Scull12122ce2019-11-19 14:21:07 +0000722 bool mergeable;
723 bool base_present;
Karl Meakin07a69ab2025-02-07 14:53:19 +0000724 mm_attr_t base_attrs;
Raghu Krishnamurthyc1012d62021-01-24 19:19:31 -0800725 pte_t new_entry;
Andrew Walbran2400ed22018-09-27 14:45:58 +0100726
Raghu Krishnamurthyc1012d62021-01-24 19:19:31 -0800727 if (!arch_mm_pte_is_table(*entry, level)) {
728 return;
Andrew Walbran2400ed22018-09-27 14:45:58 +0100729 }
730
Karl Meakinaacfd4f2025-02-08 19:30:52 +0000731 child_table = arch_mm_table_from_pte(*entry, level);
Andrew Walbran2400ed22018-09-27 14:45:58 +0100732
Andrew Scull12122ce2019-11-19 14:21:07 +0000733 /* Defrag the first entry in the table and use it as the base entry. */
734 static_assert(MM_PTE_PER_PAGE >= 1, "There must be at least one PTE.");
Raghu Krishnamurthyc1012d62021-01-24 19:19:31 -0800735
Karl Meakin00dbf1b2025-02-07 17:58:39 +0000736 mm_ptable_defrag_entry(ptable, base_addr, &(child_table->entries[0]),
Karl Meakin0f506a12025-02-08 23:28:45 +0000737 level - 1, non_secure, ppool);
Raghu Krishnamurthyc1012d62021-01-24 19:19:31 -0800738
Karl Meakin00dbf1b2025-02-07 17:58:39 +0000739 base_present =
740 arch_mm_pte_is_present(child_table->entries[0], level - 1);
741 base_attrs = arch_mm_pte_attrs(child_table->entries[0], level - 1);
Andrew Scull12122ce2019-11-19 14:21:07 +0000742
Andrew Walbran2400ed22018-09-27 14:45:58 +0100743 /*
Andrew Scull12122ce2019-11-19 14:21:07 +0000744 * Defrag the remaining entries in the table and check whether they are
745 * compatible with the base entry meaning the table can be merged into a
746 * block entry. It assumes addresses are contiguous due to identity
747 * mapping.
Andrew Walbran2400ed22018-09-27 14:45:58 +0100748 */
Andrew Scull12122ce2019-11-19 14:21:07 +0000749 mergeable = true;
Karl Meakind64aaf82025-02-08 01:12:55 +0000750 for (size_t i = 1; i < MM_PTE_PER_PAGE; ++i) {
Andrew Scull12122ce2019-11-19 14:21:07 +0000751 bool present;
Raghu Krishnamurthyc1012d62021-01-24 19:19:31 -0800752 ptable_addr_t block_addr =
753 base_addr + (i * mm_entry_size(level - 1));
Andrew Scull12122ce2019-11-19 14:21:07 +0000754
Karl Meakin00dbf1b2025-02-07 17:58:39 +0000755 mm_ptable_defrag_entry(ptable, block_addr,
756 &(child_table->entries[i]), level - 1,
Karl Meakin0f506a12025-02-08 23:28:45 +0000757 non_secure, ppool);
Raghu Krishnamurthyc1012d62021-01-24 19:19:31 -0800758
Karl Meakin00dbf1b2025-02-07 17:58:39 +0000759 present = arch_mm_pte_is_present(child_table->entries[i],
760 level - 1);
Andrew Walbran2400ed22018-09-27 14:45:58 +0100761
Andrew Scull12122ce2019-11-19 14:21:07 +0000762 if (present != base_present) {
763 mergeable = false;
764 continue;
765 }
766
767 if (!present) {
768 continue;
769 }
770
Karl Meakin00dbf1b2025-02-07 17:58:39 +0000771 if (!arch_mm_pte_is_block(child_table->entries[i], level - 1)) {
Andrew Scull12122ce2019-11-19 14:21:07 +0000772 mergeable = false;
773 continue;
774 }
775
Karl Meakin00dbf1b2025-02-07 17:58:39 +0000776 if (arch_mm_pte_attrs(child_table->entries[i], level - 1) !=
Andrew Scull12122ce2019-11-19 14:21:07 +0000777 base_attrs) {
778 mergeable = false;
779 continue;
Andrew Walbran2400ed22018-09-27 14:45:58 +0100780 }
781 }
Andrew Scullb6b9b562018-12-21 14:41:35 +0000782
Raghu Krishnamurthyc1012d62021-01-24 19:19:31 -0800783 if (!mergeable) {
784 return;
Andrew Scull12122ce2019-11-19 14:21:07 +0000785 }
786
Raghu Krishnamurthyc1012d62021-01-24 19:19:31 -0800787 new_entry = mm_merge_table_pte(*entry, level);
788 if (*entry != new_entry) {
Karl Meakin00dbf1b2025-02-07 17:58:39 +0000789 mm_replace_entry(ptable, base_addr, entry, (uintptr_t)new_entry,
Karl Meakin0f506a12025-02-08 23:28:45 +0000790 level, non_secure, ppool);
Raghu Krishnamurthyc1012d62021-01-24 19:19:31 -0800791 }
Andrew Walbran2400ed22018-09-27 14:45:58 +0100792}
793
794/**
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100795 * Defragments the given page table by converting page table references to
796 * blocks whenever possible.
797 */
Karl Meakin0f506a12025-02-08 23:28:45 +0000798static void mm_ptable_defrag(struct mm_ptable *ptable, bool non_secure,
799 struct mpool *ppool)
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100800{
Karl Meakine1aeb1d2025-02-08 00:35:14 +0000801 struct mm_page_table *root_tables = ptable->root_tables;
Karl Meakin0f506a12025-02-08 23:28:45 +0000802 mm_level_t root_level = mm_root_level(ptable);
803 uint8_t root_table_count = mm_root_table_count(ptable);
Raghu Krishnamurthyc1012d62021-01-24 19:19:31 -0800804 ptable_addr_t block_addr = 0;
Andrew Walbran2400ed22018-09-27 14:45:58 +0100805
806 /*
807 * Loop through each entry in the table. If it points to another table,
808 * check if that table can be replaced by a block or an absent entry.
809 */
Karl Meakind64aaf82025-02-08 01:12:55 +0000810 for (size_t i = 0; i < root_table_count; ++i) {
811 for (size_t j = 0; j < MM_PTE_PER_PAGE; ++j) {
Karl Meakina3a9f952025-02-08 00:11:16 +0000812 mm_ptable_defrag_entry(
Karl Meakine1aeb1d2025-02-08 00:35:14 +0000813 ptable, block_addr, &root_tables[i].entries[j],
Karl Meakin0f506a12025-02-08 23:28:45 +0000814 root_level - 1, non_secure, ppool);
Karl Meakina3a9f952025-02-08 00:11:16 +0000815 block_addr = mm_start_of_next_block(block_addr,
816 root_level - 1);
Andrew Scull1ba470e2018-10-31 15:14:31 +0000817 }
Andrew Walbran2400ed22018-09-27 14:45:58 +0100818 }
Raghu Krishnamurthyc1012d62021-01-24 19:19:31 -0800819
820 arch_mm_sync_table_writes();
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100821}
822
823/**
Andrew Scull81e85092018-12-12 12:56:20 +0000824 * Gets the attributes applied to the given range of stage-2 addresses at the
825 * given level.
826 *
827 * The `got_attrs` argument is initially passed as false until `attrs` contains
828 * attributes of the memory region at which point it is passed as true.
829 *
830 * The value returned in `attrs` is only valid if the function returns true.
831 *
832 * Returns true if the whole range has the same attributes and false otherwise.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100833 */
Daniel Boulby8adf7482021-09-22 15:12:44 +0100834// NOLINTNEXTLINE(misc-no-recursion)
Karl Meakind64aaf82025-02-08 01:12:55 +0000835static bool mm_ptable_get_attrs_level(const struct mm_page_table *table,
Andrew Scull81e85092018-12-12 12:56:20 +0000836 ptable_addr_t begin, ptable_addr_t end,
Karl Meakin07a69ab2025-02-07 14:53:19 +0000837 mm_level_t level, bool got_attrs,
838 mm_attr_t *attrs)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100839{
Karl Meakind64aaf82025-02-08 01:12:55 +0000840 const pte_t *pte = &table->entries[mm_index(begin, level)];
Andrew Scull81e85092018-12-12 12:56:20 +0000841 ptable_addr_t level_end = mm_level_end(begin, level);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100842
Andrew Scull81e85092018-12-12 12:56:20 +0000843 /* Cap end so that we don't go over the current level max. */
844 if (end > level_end) {
845 end = level_end;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100846 }
847
Andrew Scull81e85092018-12-12 12:56:20 +0000848 /* Check that each entry is owned. */
849 while (begin < end) {
850 if (arch_mm_pte_is_table(*pte, level)) {
851 if (!mm_ptable_get_attrs_level(
Karl Meakinaacfd4f2025-02-08 19:30:52 +0000852 arch_mm_table_from_pte(*pte, level), begin,
853 end, level - 1, got_attrs, attrs)) {
Andrew Scull81e85092018-12-12 12:56:20 +0000854 return false;
855 }
856 got_attrs = true;
857 } else {
858 if (!got_attrs) {
859 *attrs = arch_mm_pte_attrs(*pte, level);
860 got_attrs = true;
861 } else if (arch_mm_pte_attrs(*pte, level) != *attrs) {
862 return false;
863 }
864 }
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100865
Karl Meakin25954e32025-02-07 16:12:51 +0000866 begin = mm_start_of_next_block(begin, level);
Andrew Scull81e85092018-12-12 12:56:20 +0000867 pte++;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100868 }
869
Andrew Scullc66a04d2018-12-07 13:41:56 +0000870 /* The entry is a valid block. */
Andrew Scull81e85092018-12-12 12:56:20 +0000871 return got_attrs;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100872}
873
874/**
Raghu Krishnamurthy2323d722021-02-12 22:55:38 -0800875 * Gets the attributes applied to the given range of addresses in the page
876 * tables.
Andrew Scull81e85092018-12-12 12:56:20 +0000877 *
878 * The value returned in `attrs` is only valid if the function returns true.
879 *
880 * Returns true if the whole range has the same attributes and false otherwise.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100881 */
Karl Meakind64aaf82025-02-08 01:12:55 +0000882static bool mm_get_attrs(const struct mm_ptable *ptable, ptable_addr_t begin,
Karl Meakin0f506a12025-02-08 23:28:45 +0000883 ptable_addr_t end, mm_attr_t *attrs)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100884{
Karl Meakin0f506a12025-02-08 23:28:45 +0000885 mm_level_t root_level = mm_root_level(ptable);
886 ptable_addr_t ptable_end = mm_ptable_addr_space_end(ptable);
Karl Meakine1aeb1d2025-02-08 00:35:14 +0000887 struct mm_page_table *root_table;
Andrew Scull81e85092018-12-12 12:56:20 +0000888 bool got_attrs = false;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100889
Andrew Scull81e85092018-12-12 12:56:20 +0000890 begin = mm_round_down_to_page(begin);
891 end = mm_round_up_to_page(end);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100892
Andrew Scull81e85092018-12-12 12:56:20 +0000893 /* Fail if the addresses are out of range. */
894 if (end > ptable_end) {
Andrew Scull1ba470e2018-10-31 15:14:31 +0000895 return false;
896 }
897
Karl Meakine1aeb1d2025-02-08 00:35:14 +0000898 root_table = &ptable->root_tables[mm_index(begin, root_level)];
Andrew Scull81e85092018-12-12 12:56:20 +0000899 while (begin < end) {
Karl Meakine1aeb1d2025-02-08 00:35:14 +0000900 if (!mm_ptable_get_attrs_level(root_table, begin, end,
Karl Meakina3a9f952025-02-08 00:11:16 +0000901 root_level - 1, got_attrs,
902 attrs)) {
Andrew Scull81e85092018-12-12 12:56:20 +0000903 return false;
904 }
905
906 got_attrs = true;
Karl Meakin25954e32025-02-07 16:12:51 +0000907 begin = mm_start_of_next_block(begin, root_level);
Karl Meakine1aeb1d2025-02-08 00:35:14 +0000908 root_table++;
Andrew Scull81e85092018-12-12 12:56:20 +0000909 }
910
911 return got_attrs;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100912}
913
Karl Meakin07a69ab2025-02-07 14:53:19 +0000914bool mm_vm_init(struct mm_ptable *ptable, mm_asid_t id, struct mpool *ppool)
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100915{
Karl Meakin0f506a12025-02-08 23:28:45 +0000916 return mm_ptable_init(ptable, id, false, ppool);
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100917}
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100918
Karl Meakind64aaf82025-02-08 01:12:55 +0000919void mm_vm_fini(const struct mm_ptable *ptable, struct mpool *ppool)
Andrew Scull1ba470e2018-10-31 15:14:31 +0000920{
Karl Meakin0f506a12025-02-08 23:28:45 +0000921 mm_ptable_fini(ptable, ppool);
Andrew Scull1ba470e2018-10-31 15:14:31 +0000922}
923
924/**
Andrew Scull73b89542019-11-20 17:31:26 +0000925 * Selects flags to pass to the page table manipulation operation based on the
926 * mapping mode.
927 */
Karl Meakin07a69ab2025-02-07 14:53:19 +0000928static struct mm_flags mm_mode_to_flags(mm_mode_t mode)
Andrew Scull73b89542019-11-20 17:31:26 +0000929{
Karl Meakin1fd4b822025-02-01 17:13:47 +0000930 struct mm_flags flags = {0};
931
Andrew Scull73b89542019-11-20 17:31:26 +0000932 if ((mode & MM_MODE_UNMAPPED_MASK) == MM_MODE_UNMAPPED_MASK) {
Karl Meakin1fd4b822025-02-01 17:13:47 +0000933 flags.unmap = true;
Andrew Scull73b89542019-11-20 17:31:26 +0000934 }
935
Karl Meakin1fd4b822025-02-01 17:13:47 +0000936 return flags;
Andrew Scull73b89542019-11-20 17:31:26 +0000937}
938
939/**
Andrew Scull4e83cef2019-11-19 14:17:54 +0000940 * See `mm_ptable_identity_prepare`.
941 *
Raghu Krishnamurthy43fe93a2021-01-31 16:38:38 -0800942 * This must be called before `mm_identity_commit` for the same mapping.
943 *
944 * Returns true on success, or false if the update would fail.
945 */
Karl Meakind64aaf82025-02-08 01:12:55 +0000946bool mm_identity_prepare(struct mm_ptable *ptable, paddr_t begin, paddr_t end,
Karl Meakin07a69ab2025-02-07 14:53:19 +0000947 mm_mode_t mode, struct mpool *ppool)
Raghu Krishnamurthy43fe93a2021-01-31 16:38:38 -0800948{
Karl Meakin1fd4b822025-02-01 17:13:47 +0000949 struct mm_flags flags = mm_mode_to_flags(mode);
Karl Meakin07a69ab2025-02-07 14:53:19 +0000950
Karl Meakin0f506a12025-02-08 23:28:45 +0000951 assert(ptable->stage1);
Karl Meakind64aaf82025-02-08 01:12:55 +0000952 return mm_ptable_identity_prepare(ptable, begin, end,
Raghu Krishnamurthy43fe93a2021-01-31 16:38:38 -0800953 arch_mm_mode_to_stage1_attrs(mode),
954 flags, ppool);
955}
956
957/**
958 * See `mm_ptable_identity_commit`.
959 *
960 * `mm_identity_prepare` must be called before this for the same mapping.
961 */
Karl Meakind64aaf82025-02-08 01:12:55 +0000962void *mm_identity_commit(struct mm_ptable *ptable, paddr_t begin, paddr_t end,
Karl Meakin07a69ab2025-02-07 14:53:19 +0000963 mm_mode_t mode, struct mpool *ppool)
Raghu Krishnamurthy43fe93a2021-01-31 16:38:38 -0800964{
Karl Meakin1fd4b822025-02-01 17:13:47 +0000965 struct mm_flags flags = mm_mode_to_flags(mode);
Karl Meakin07a69ab2025-02-07 14:53:19 +0000966
Karl Meakin0f506a12025-02-08 23:28:45 +0000967 assert(ptable->stage1);
Karl Meakind64aaf82025-02-08 01:12:55 +0000968 mm_ptable_identity_commit(ptable, begin, end,
Raghu Krishnamurthy43fe93a2021-01-31 16:38:38 -0800969 arch_mm_mode_to_stage1_attrs(mode), flags,
970 ppool);
971 return ptr_from_va(va_from_pa(begin));
972}
973
974/**
975 * See `mm_ptable_identity_prepare`.
976 *
Andrew Scull4e83cef2019-11-19 14:17:54 +0000977 * This must be called before `mm_vm_identity_commit` for the same mapping.
Andrew Walbran8ec2b9f2019-11-25 15:05:40 +0000978 *
979 * Returns true on success, or false if the update would fail.
Andrew Scull4e83cef2019-11-19 14:17:54 +0000980 */
Karl Meakind64aaf82025-02-08 01:12:55 +0000981bool mm_vm_identity_prepare(struct mm_ptable *ptable, paddr_t begin,
Karl Meakin07a69ab2025-02-07 14:53:19 +0000982 paddr_t end, mm_mode_t mode, struct mpool *ppool)
Andrew Scull4e83cef2019-11-19 14:17:54 +0000983{
Karl Meakin1fd4b822025-02-01 17:13:47 +0000984 struct mm_flags flags = mm_mode_to_flags(mode);
Andrew Scull4e83cef2019-11-19 14:17:54 +0000985
Karl Meakind64aaf82025-02-08 01:12:55 +0000986 return mm_ptable_identity_prepare(ptable, begin, end,
Andrew Scull4e83cef2019-11-19 14:17:54 +0000987 arch_mm_mode_to_stage2_attrs(mode),
988 flags, ppool);
989}
990
991/**
992 * See `mm_ptable_identity_commit`.
993 *
994 * `mm_vm_identity_prepare` must be called before this for the same mapping.
995 */
Karl Meakind64aaf82025-02-08 01:12:55 +0000996void mm_vm_identity_commit(struct mm_ptable *ptable, paddr_t begin, paddr_t end,
Karl Meakin07a69ab2025-02-07 14:53:19 +0000997 mm_mode_t mode, struct mpool *ppool, ipaddr_t *ipa)
Andrew Scull4e83cef2019-11-19 14:17:54 +0000998{
Karl Meakin1fd4b822025-02-01 17:13:47 +0000999 struct mm_flags flags = mm_mode_to_flags(mode);
Andrew Scull4e83cef2019-11-19 14:17:54 +00001000
Karl Meakind64aaf82025-02-08 01:12:55 +00001001 mm_ptable_identity_commit(ptable, begin, end,
Andrew Scull4e83cef2019-11-19 14:17:54 +00001002 arch_mm_mode_to_stage2_attrs(mode), flags,
1003 ppool);
1004
1005 if (ipa != NULL) {
1006 *ipa = ipa_from_pa(begin);
1007 }
1008}
1009
1010/**
Andrew Scull80871322018-08-06 12:04:09 +01001011 * Updates a VM's page table such that the given physical address range is
1012 * mapped in the address space at the corresponding address range in the
Andrew Scullfe636b12018-07-30 14:15:54 +01001013 * architecture-agnostic mode provided.
Andrew Walbran8ec2b9f2019-11-25 15:05:40 +00001014 *
1015 * mm_vm_defrag should always be called after a series of page table updates,
1016 * whether they succeed or fail. This is because on failure extra page table
1017 * entries may have been allocated and then not used, while on success it may be
1018 * possible to compact the page table by merging several entries into a block.
1019 *
1020 * Returns true on success, or false if the update failed and no changes were
1021 * made.
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +01001022 */
Karl Meakind64aaf82025-02-08 01:12:55 +00001023bool mm_vm_identity_map(struct mm_ptable *ptable, paddr_t begin, paddr_t end,
Karl Meakin07a69ab2025-02-07 14:53:19 +00001024 mm_mode_t mode, struct mpool *ppool, ipaddr_t *ipa)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +01001025{
Karl Meakin1fd4b822025-02-01 17:13:47 +00001026 struct mm_flags flags = mm_mode_to_flags(mode);
Andrew Scullda3df7f2019-01-05 17:49:27 +00001027 bool success = mm_ptable_identity_update(
Karl Meakind64aaf82025-02-08 01:12:55 +00001028 ptable, begin, end, arch_mm_mode_to_stage2_attrs(mode), flags,
Andrew Scullda3df7f2019-01-05 17:49:27 +00001029 ppool);
Andrew Scull80871322018-08-06 12:04:09 +01001030
1031 if (success && ipa != NULL) {
1032 *ipa = ipa_from_pa(begin);
1033 }
1034
1035 return success;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +01001036}
1037
1038/**
Andrew Scullda3df7f2019-01-05 17:49:27 +00001039 * Updates the VM's table such that the given physical address range has no
1040 * connection to the VM.
Andrew Scull80871322018-08-06 12:04:09 +01001041 */
Karl Meakind64aaf82025-02-08 01:12:55 +00001042bool mm_vm_unmap(struct mm_ptable *ptable, paddr_t begin, paddr_t end,
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +00001043 struct mpool *ppool)
Andrew Scull80871322018-08-06 12:04:09 +01001044{
Karl Meakin07a69ab2025-02-07 14:53:19 +00001045 mm_mode_t mode = MM_MODE_UNMAPPED_MASK;
Andrew Scull73b89542019-11-20 17:31:26 +00001046
Karl Meakind64aaf82025-02-08 01:12:55 +00001047 return mm_vm_identity_map(ptable, begin, end, mode, ppool, NULL);
Andrew Scull80871322018-08-06 12:04:09 +01001048}
1049
1050/**
Andrew Scullda3df7f2019-01-05 17:49:27 +00001051 * Write the given page table of a VM to the debug log.
1052 */
Karl Meakind64aaf82025-02-08 01:12:55 +00001053void mm_vm_dump(const struct mm_ptable *ptable)
Andrew Scullda3df7f2019-01-05 17:49:27 +00001054{
Karl Meakin0f506a12025-02-08 23:28:45 +00001055 mm_ptable_dump(ptable);
Andrew Scullda3df7f2019-01-05 17:49:27 +00001056}
1057
1058/**
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001059 * Defragments a stage1 page table.
1060 */
Karl Meakind64aaf82025-02-08 01:12:55 +00001061void mm_stage1_defrag(struct mm_ptable *ptable, struct mpool *ppool)
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001062{
Karl Meakin0f506a12025-02-08 23:28:45 +00001063 assert(ptable->stage1);
1064 mm_ptable_defrag(ptable, false, ppool);
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001065}
1066
1067/**
Andrew Scullda3df7f2019-01-05 17:49:27 +00001068 * Defragments the VM page table.
1069 */
Karl Meakind64aaf82025-02-08 01:12:55 +00001070void mm_vm_defrag(struct mm_ptable *ptable, struct mpool *ppool,
1071 bool non_secure)
Andrew Scullda3df7f2019-01-05 17:49:27 +00001072{
Karl Meakin0f506a12025-02-08 23:28:45 +00001073 mm_ptable_defrag(ptable, non_secure, ppool);
Andrew Scullda3df7f2019-01-05 17:49:27 +00001074}
1075
1076/**
Fuad Tabba9dc276f2020-07-16 09:29:32 +01001077 * Gets the mode of the given range of intermediate physical addresses if they
Andrew Scull81e85092018-12-12 12:56:20 +00001078 * are mapped with the same mode.
1079 *
1080 * Returns true if the range is mapped with the same mode and false otherwise.
Andrew Scull80871322018-08-06 12:04:09 +01001081 */
Karl Meakind64aaf82025-02-08 01:12:55 +00001082bool mm_vm_get_mode(const struct mm_ptable *ptable, ipaddr_t begin,
Karl Meakin07a69ab2025-02-07 14:53:19 +00001083 ipaddr_t end, mm_mode_t *mode)
Andrew Scull80871322018-08-06 12:04:09 +01001084{
Karl Meakin07a69ab2025-02-07 14:53:19 +00001085 mm_attr_t attrs;
Andrew Scull81e85092018-12-12 12:56:20 +00001086 bool ret;
1087
Karl Meakin0f506a12025-02-08 23:28:45 +00001088 ret = mm_get_attrs(ptable, ipa_addr(begin), ipa_addr(end), &attrs);
Andrew Scull81e85092018-12-12 12:56:20 +00001089 if (ret) {
1090 *mode = arch_mm_stage2_attrs_to_mode(attrs);
1091 }
1092
1093 return ret;
Andrew Scull80871322018-08-06 12:04:09 +01001094}
1095
Raghu Krishnamurthy2323d722021-02-12 22:55:38 -08001096/**
1097 * Gets the mode of the given range of virtual addresses if they
1098 * are mapped with the same mode.
1099 *
1100 * Returns true if the range is mapped with the same mode and false otherwise.
1101 */
Karl Meakind64aaf82025-02-08 01:12:55 +00001102bool mm_get_mode(const struct mm_ptable *ptable, vaddr_t begin, vaddr_t end,
Karl Meakin07a69ab2025-02-07 14:53:19 +00001103 mm_mode_t *mode)
Raghu Krishnamurthy2323d722021-02-12 22:55:38 -08001104{
Karl Meakin07a69ab2025-02-07 14:53:19 +00001105 mm_attr_t attrs;
Raghu Krishnamurthy2323d722021-02-12 22:55:38 -08001106 bool ret;
1107
Karl Meakin0f506a12025-02-08 23:28:45 +00001108 assert(ptable->stage1);
1109 ret = mm_get_attrs(ptable, va_addr(begin), va_addr(end), &attrs);
Raghu Krishnamurthy2323d722021-02-12 22:55:38 -08001110 if (ret) {
1111 *mode = arch_mm_stage1_attrs_to_mode(attrs);
1112 }
1113
1114 return ret;
1115}
1116
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001117static struct mm_stage1_locked mm_stage1_lock_unsafe(void)
1118{
1119 return (struct mm_stage1_locked){.ptable = &ptable};
1120}
1121
Raghu Krishnamurthyd3ab8c32021-02-10 19:11:30 -08001122struct mm_stage1_locked mm_lock_ptable_unsafe(struct mm_ptable *ptable)
1123{
1124 return (struct mm_stage1_locked){.ptable = ptable};
1125}
1126
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001127struct mm_stage1_locked mm_lock_stage1(void)
1128{
1129 sl_lock(&ptable_lock);
1130 return mm_stage1_lock_unsafe();
1131}
1132
1133void mm_unlock_stage1(struct mm_stage1_locked *lock)
1134{
Andrew Scull877ae4b2019-07-02 12:52:33 +01001135 CHECK(lock->ptable == &ptable);
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001136 sl_unlock(&ptable_lock);
1137 lock->ptable = NULL;
1138}
1139
Andrew Scull80871322018-08-06 12:04:09 +01001140/**
Andrew Scull80871322018-08-06 12:04:09 +01001141 * Updates the hypervisor page table such that the given physical address range
1142 * is mapped into the address space at the corresponding address range in the
1143 * architecture-agnostic mode provided.
1144 */
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001145void *mm_identity_map(struct mm_stage1_locked stage1_locked, paddr_t begin,
Karl Meakin07a69ab2025-02-07 14:53:19 +00001146 paddr_t end, mm_mode_t mode, struct mpool *ppool)
Andrew Scull80871322018-08-06 12:04:09 +01001147{
Karl Meakin1fd4b822025-02-01 17:13:47 +00001148 struct mm_flags flags = mm_mode_to_flags(mode);
Karl Meakin07a69ab2025-02-07 14:53:19 +00001149
Karl Meakin0f506a12025-02-08 23:28:45 +00001150 assert(stage1_locked.ptable->stage1);
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001151 if (mm_ptable_identity_update(stage1_locked.ptable, begin, end,
Andrew Scull73b89542019-11-20 17:31:26 +00001152 arch_mm_mode_to_stage1_attrs(mode), flags,
1153 ppool)) {
Andrew Scull4e5f8142018-10-12 14:37:19 +01001154 return ptr_from_va(va_from_pa(begin));
Andrew Scull80871322018-08-06 12:04:09 +01001155 }
1156
1157 return NULL;
1158}
1159
1160/**
1161 * Updates the hypervisor table such that the given physical address range is
1162 * not mapped in the address space.
1163 */
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001164bool mm_unmap(struct mm_stage1_locked stage1_locked, paddr_t begin, paddr_t end,
1165 struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +01001166{
Karl Meakin07a69ab2025-02-07 14:53:19 +00001167 mm_mode_t mode = MM_MODE_UNMAPPED_MASK;
Andrew Scull73b89542019-11-20 17:31:26 +00001168
1169 return mm_identity_map(stage1_locked, begin, end, mode, ppool);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +01001170}
1171
1172/**
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001173 * Defragments the hypervisor page table.
1174 */
1175void mm_defrag(struct mm_stage1_locked stage1_locked, struct mpool *ppool)
1176{
Karl Meakin0f506a12025-02-08 23:28:45 +00001177 assert(stage1_locked.ptable->stage1);
1178 mm_ptable_defrag(stage1_locked.ptable, false, ppool);
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001179}
1180
1181/**
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +01001182 * Initialises memory management for the hypervisor itself.
1183 */
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +00001184bool mm_init(struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +01001185{
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001186 /* Locking is not enabled yet so fake it, */
1187 struct mm_stage1_locked stage1_locked = mm_stage1_lock_unsafe();
1188
Karl Meakine8937d92024-03-19 16:04:25 +00001189 dlog_info("text: %#lx - %#lx\n", pa_addr(layout_text_begin()),
Andrew Walbran17eebf92020-02-05 16:35:49 +00001190 pa_addr(layout_text_end()));
Karl Meakine8937d92024-03-19 16:04:25 +00001191 dlog_info("rodata: %#lx - %#lx\n", pa_addr(layout_rodata_begin()),
Andrew Walbran17eebf92020-02-05 16:35:49 +00001192 pa_addr(layout_rodata_end()));
Karl Meakine8937d92024-03-19 16:04:25 +00001193 dlog_info("data: %#lx - %#lx\n", pa_addr(layout_data_begin()),
Andrew Walbran17eebf92020-02-05 16:35:49 +00001194 pa_addr(layout_data_end()));
Karl Meakine8937d92024-03-19 16:04:25 +00001195 dlog_info("stacks: %#lx - %#lx\n", pa_addr(layout_stacks_begin()),
Maksims Svecovs134b8f92022-03-04 15:14:09 +00001196 pa_addr(layout_stacks_end()));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +01001197
Raghu Krishnamurthy0132b512021-02-03 14:13:26 -08001198 /* ASID 0 is reserved for use by the hypervisor. */
Karl Meakin0f506a12025-02-08 23:28:45 +00001199 if (!mm_ptable_init(&ptable, 0, true, ppool)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +00001200 dlog_error("Unable to allocate memory for page table.\n");
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +01001201 return false;
1202 }
1203
Arunachalam Ganapathy0f0f7062022-01-26 17:09:53 +00001204 /* Initialize arch_mm before calling below mapping routines */
Karl Meakine1aeb1d2025-02-08 00:35:14 +00001205 if (!arch_mm_init(&ptable)) {
Arunachalam Ganapathy0f0f7062022-01-26 17:09:53 +00001206 return false;
1207 }
1208
Andrew Walbran48699362019-05-20 14:38:00 +01001209 /* Let console driver map pages for itself. */
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001210 plat_console_mm_init(stage1_locked, ppool);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +01001211
1212 /* Map each section. */
Raghu Krishnamurthy472a8822022-10-04 21:28:59 -07001213 CHECK(mm_identity_map(stage1_locked, layout_text_begin(),
1214 layout_text_end(), MM_MODE_X, ppool) != NULL);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +01001215
Raghu Krishnamurthy472a8822022-10-04 21:28:59 -07001216 CHECK(mm_identity_map(stage1_locked, layout_rodata_begin(),
1217 layout_rodata_end(), MM_MODE_R, ppool) != NULL);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +01001218
Raghu Krishnamurthy472a8822022-10-04 21:28:59 -07001219 CHECK(mm_identity_map(stage1_locked, layout_data_begin(),
1220 layout_data_end(), MM_MODE_R | MM_MODE_W,
1221 ppool) != NULL);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +01001222
Maksims Svecovs134b8f92022-03-04 15:14:09 +00001223 /* Arch-specific stack mapping. */
Raghu Krishnamurthy472a8822022-10-04 21:28:59 -07001224 CHECK(arch_stack_mm_init(stage1_locked, ppool));
Maksims Svecovs134b8f92022-03-04 15:14:09 +00001225
Arunachalam Ganapathy0f0f7062022-01-26 17:09:53 +00001226 return true;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +01001227}