Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 1 | /* |
Andrew Walbran | 692b325 | 2019-03-07 15:51:31 +0000 | [diff] [blame] | 2 | * Copyright 2018 The Hafnium Authors. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 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 | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
Andrew Scull | fbc938a | 2018-08-20 14:09:28 +0100 | [diff] [blame] | 9 | #pragma once |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 10 | |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 11 | #include <stdalign.h> |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 12 | #include <stdbool.h> |
J-Alves | 715d623 | 2023-02-16 16:33:28 +0000 | [diff] [blame^] | 13 | #include <stddef.h> |
Wedson Almeida Filho | 84a30a0 | 2018-07-23 20:05:05 +0100 | [diff] [blame] | 14 | #include <stdint.h> |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 15 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 16 | #include "hf/arch/mm.h" |
| 17 | |
| 18 | #include "hf/addr.h" |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 19 | #include "hf/mpool.h" |
Andrew Scull | 877ae4b | 2019-07-02 12:52:33 +0100 | [diff] [blame] | 20 | #include "hf/static_assert.h" |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 21 | |
Andrew Scull | c66a04d | 2018-12-07 13:41:56 +0000 | [diff] [blame] | 22 | /* Keep macro alignment */ |
| 23 | /* clang-format off */ |
| 24 | |
J-Alves | 715d623 | 2023-02-16 16:33:28 +0000 | [diff] [blame^] | 25 | #define PAGE_SIZE ((size_t)(1 << PAGE_BITS)) |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 26 | #define MM_PTE_PER_PAGE (PAGE_SIZE / sizeof(pte_t)) |
| 27 | |
Andrew Scull | c66a04d | 2018-12-07 13:41:56 +0000 | [diff] [blame] | 28 | /* The following are arch-independent page mapping modes. */ |
Andrew Walbran | 1281ed4 | 2019-10-22 17:23:40 +0100 | [diff] [blame] | 29 | #define MM_MODE_R UINT32_C(0x0001) /* read */ |
| 30 | #define MM_MODE_W UINT32_C(0x0002) /* write */ |
| 31 | #define MM_MODE_X UINT32_C(0x0004) /* execute */ |
| 32 | #define MM_MODE_D UINT32_C(0x0008) /* device */ |
Andrew Scull | c66a04d | 2018-12-07 13:41:56 +0000 | [diff] [blame] | 33 | |
| 34 | /* |
| 35 | * Memory in stage-1 is either valid (present) or invalid (absent). |
| 36 | * |
| 37 | * Memory in stage-2 has more states to track sharing, borrowing and giving of |
| 38 | * memory. The states are made up of three parts: |
| 39 | * |
| 40 | * 1. V = valid/invalid : Whether the memory is part of the VM's address |
| 41 | * space. A fault will be generated if accessed when |
| 42 | * invalid. |
| 43 | * 2. O = owned/unowned : Whether the memory is owned by the VM. |
| 44 | * 3. X = exclusive/shared : Whether access is exclusive to the VM or shared |
| 45 | * with at most one other. |
| 46 | * |
| 47 | * These parts compose to form the following state: |
| 48 | * |
| 49 | * - V O X : Owner of memory with exclusive access. |
| 50 | * - V O !X : Owner of memory with access shared with at most one other VM. |
| 51 | * - V !O X : Borrower of memory with exclusive access. |
| 52 | * - V !O !X : Borrower of memory where access is shared with the owner. |
| 53 | * - !V O X : Owner of memory lent to a VM that has exclusive access. |
| 54 | * |
| 55 | * - !V O !X : Unused. Owner of shared memory always has access. |
Andrew Scull | 73b8954 | 2019-11-20 17:31:26 +0000 | [diff] [blame] | 56 | * - !V !O X : Unused. Next entry is used for invalid memory. |
Andrew Scull | c66a04d | 2018-12-07 13:41:56 +0000 | [diff] [blame] | 57 | * |
Andrew Scull | c66a04d | 2018-12-07 13:41:56 +0000 | [diff] [blame] | 58 | * - !V !O !X : Invalid memory. Memory is unrelated to the VM. |
| 59 | * |
| 60 | * Modes are selected so that owner of exclusive memory is the default. |
| 61 | */ |
Andrew Walbran | 1281ed4 | 2019-10-22 17:23:40 +0100 | [diff] [blame] | 62 | #define MM_MODE_INVALID UINT32_C(0x0010) |
| 63 | #define MM_MODE_UNOWNED UINT32_C(0x0020) |
| 64 | #define MM_MODE_SHARED UINT32_C(0x0040) |
Andrew Scull | c66a04d | 2018-12-07 13:41:56 +0000 | [diff] [blame] | 65 | |
Raghu Krishnamurthy | 25daaea | 2021-02-10 13:19:16 -0800 | [diff] [blame] | 66 | /* Specifies if a mapping will be a user mapping(EL0). */ |
| 67 | #define MM_MODE_USER UINT32_C(0x0200) |
| 68 | |
| 69 | /* Map page as non-global. */ |
| 70 | #define MM_MODE_NG UINT32_C(0x0100) /* non-global */ |
| 71 | |
Andrew Scull | 73b8954 | 2019-11-20 17:31:26 +0000 | [diff] [blame] | 72 | /* The mask for a mode that is considered unmapped. */ |
| 73 | #define MM_MODE_UNMAPPED_MASK (MM_MODE_INVALID | MM_MODE_UNOWNED) |
| 74 | |
David Brazdil | 711fbe9 | 2019-08-06 13:39:58 +0100 | [diff] [blame] | 75 | #define MM_FLAG_COMMIT 0x01 |
| 76 | #define MM_FLAG_UNMAP 0x02 |
| 77 | #define MM_FLAG_STAGE1 0x04 |
| 78 | |
Andrew Scull | c66a04d | 2018-12-07 13:41:56 +0000 | [diff] [blame] | 79 | /* clang-format on */ |
| 80 | |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 81 | #define MM_PPOOL_ENTRY_SIZE sizeof(struct mm_page_table) |
| 82 | |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 83 | struct mm_page_table { |
| 84 | alignas(PAGE_SIZE) pte_t entries[MM_PTE_PER_PAGE]; |
| 85 | }; |
| 86 | static_assert(sizeof(struct mm_page_table) == PAGE_SIZE, |
| 87 | "A page table must take exactly one page."); |
| 88 | static_assert(alignof(struct mm_page_table) == PAGE_SIZE, |
| 89 | "A page table must be page aligned."); |
| 90 | |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 91 | struct mm_ptable { |
Raghu Krishnamurthy | 0132b51 | 2021-02-03 14:13:26 -0800 | [diff] [blame] | 92 | /** |
| 93 | * VMID/ASID associated with a page table. ASID 0 is reserved for use by |
| 94 | * the hypervisor. |
| 95 | */ |
| 96 | uint16_t id; |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 97 | /** Address of the root of the page table. */ |
| 98 | paddr_t root; |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 99 | }; |
| 100 | |
David Brazdil | 711fbe9 | 2019-08-06 13:39:58 +0100 | [diff] [blame] | 101 | /** The type of addresses stored in the page table. */ |
| 102 | typedef uintvaddr_t ptable_addr_t; |
| 103 | |
Andrew Scull | ba79b0a | 2019-07-03 11:26:53 +0100 | [diff] [blame] | 104 | /** Represents the currently locked stage-1 page table of the hypervisor. */ |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 105 | struct mm_stage1_locked { |
| 106 | struct mm_ptable *ptable; |
| 107 | }; |
| 108 | |
Andrew Scull | da24197 | 2019-01-05 18:17:48 +0000 | [diff] [blame] | 109 | void mm_vm_enable_invalidation(void); |
| 110 | |
Raghu Krishnamurthy | 0132b51 | 2021-02-03 14:13:26 -0800 | [diff] [blame] | 111 | bool mm_ptable_init(struct mm_ptable *t, uint16_t id, int flags, |
| 112 | struct mpool *ppool); |
David Brazdil | 711fbe9 | 2019-08-06 13:39:58 +0100 | [diff] [blame] | 113 | ptable_addr_t mm_ptable_addr_space_end(int flags); |
| 114 | |
Raghu Krishnamurthy | 0132b51 | 2021-02-03 14:13:26 -0800 | [diff] [blame] | 115 | bool mm_vm_init(struct mm_ptable *t, uint16_t id, struct mpool *ppool); |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 116 | void mm_vm_fini(struct mm_ptable *t, struct mpool *ppool); |
Raghu Krishnamurthy | 43fe93a | 2021-01-31 16:38:38 -0800 | [diff] [blame] | 117 | |
| 118 | bool mm_identity_prepare(struct mm_ptable *t, paddr_t begin, paddr_t end, |
| 119 | uint32_t mode, struct mpool *ppool); |
| 120 | void *mm_identity_commit(struct mm_ptable *t, paddr_t begin, paddr_t end, |
| 121 | uint32_t mode, struct mpool *ppool); |
| 122 | |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 123 | bool mm_vm_identity_map(struct mm_ptable *t, paddr_t begin, paddr_t end, |
Andrew Walbran | 8ec2b9f | 2019-11-25 15:05:40 +0000 | [diff] [blame] | 124 | uint32_t mode, struct mpool *ppool, ipaddr_t *ipa); |
Andrew Scull | 4e83cef | 2019-11-19 14:17:54 +0000 | [diff] [blame] | 125 | bool mm_vm_identity_prepare(struct mm_ptable *t, paddr_t begin, paddr_t end, |
| 126 | uint32_t mode, struct mpool *ppool); |
| 127 | void mm_vm_identity_commit(struct mm_ptable *t, paddr_t begin, paddr_t end, |
Andrew Walbran | 8ec2b9f | 2019-11-25 15:05:40 +0000 | [diff] [blame] | 128 | uint32_t mode, struct mpool *ppool, ipaddr_t *ipa); |
Andrew Scull | da24197 | 2019-01-05 18:17:48 +0000 | [diff] [blame] | 129 | bool mm_vm_unmap(struct mm_ptable *t, paddr_t begin, paddr_t end, |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 130 | struct mpool *ppool); |
Raghu Krishnamurthy | 7ad3d14 | 2021-03-28 00:47:35 -0700 | [diff] [blame] | 131 | void mm_stage1_defrag(struct mm_ptable *t, struct mpool *ppool); |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 132 | void mm_vm_defrag(struct mm_ptable *t, struct mpool *ppool); |
| 133 | void mm_vm_dump(struct mm_ptable *t); |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 134 | bool mm_vm_get_mode(struct mm_ptable *t, ipaddr_t begin, ipaddr_t end, |
Andrew Walbran | 1281ed4 | 2019-10-22 17:23:40 +0100 | [diff] [blame] | 135 | uint32_t *mode); |
Raghu Krishnamurthy | 2323d72 | 2021-02-12 22:55:38 -0800 | [diff] [blame] | 136 | bool mm_get_mode(struct mm_ptable *t, vaddr_t begin, vaddr_t end, |
| 137 | uint32_t *mode); |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 138 | |
Raghu Krishnamurthy | d3ab8c3 | 2021-02-10 19:11:30 -0800 | [diff] [blame] | 139 | struct mm_stage1_locked mm_lock_ptable_unsafe(struct mm_ptable *ptable); |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 140 | struct mm_stage1_locked mm_lock_stage1(void); |
| 141 | void mm_unlock_stage1(struct mm_stage1_locked *lock); |
| 142 | void *mm_identity_map(struct mm_stage1_locked stage1_locked, paddr_t begin, |
Andrew Walbran | 1281ed4 | 2019-10-22 17:23:40 +0100 | [diff] [blame] | 143 | paddr_t end, uint32_t mode, struct mpool *ppool); |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 144 | bool mm_unmap(struct mm_stage1_locked stage1_locked, paddr_t begin, paddr_t end, |
| 145 | struct mpool *ppool); |
| 146 | void mm_defrag(struct mm_stage1_locked stage1_locked, struct mpool *ppool); |
| 147 | |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 148 | bool mm_init(struct mpool *ppool); |