[6d7ffa65] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Jakub Jermar
|
---|
[6d7ffa65] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[f47fd19] | 29 | /** @addtogroup genarchmm
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
[0f27b4c] | 33 | /**
|
---|
[b45c443] | 34 | * @file
|
---|
[da1bafb] | 35 | * @brief Virtual Address Translation for hierarchical 4-level page tables.
|
---|
[0f27b4c] | 36 | */
|
---|
| 37 |
|
---|
[6d7ffa65] | 38 | #include <genarch/mm/page_pt.h>
|
---|
| 39 | #include <mm/page.h>
|
---|
| 40 | #include <mm/frame.h>
|
---|
[ef67bab] | 41 | #include <mm/as.h>
|
---|
[6d7ffa65] | 42 | #include <arch/mm/page.h>
|
---|
[fc1e4f6] | 43 | #include <arch/mm/as.h>
|
---|
[d99c1d2] | 44 | #include <typedefs.h>
|
---|
[6d7ffa65] | 45 | #include <arch/asm.h>
|
---|
| 46 | #include <memstr.h>
|
---|
| 47 |
|
---|
[da1bafb] | 48 | static void pt_mapping_insert(as_t *, uintptr_t, uintptr_t, unsigned int);
|
---|
| 49 | static void pt_mapping_remove(as_t *, uintptr_t);
|
---|
| 50 | static pte_t *pt_mapping_find(as_t *, uintptr_t);
|
---|
[6d7ffa65] | 51 |
|
---|
[f5935ed] | 52 | page_mapping_operations_t pt_mapping_operations = {
|
---|
[6d7ffa65] | 53 | .mapping_insert = pt_mapping_insert,
|
---|
[8f00329] | 54 | .mapping_remove = pt_mapping_remove,
|
---|
[6d7ffa65] | 55 | .mapping_find = pt_mapping_find
|
---|
| 56 | };
|
---|
| 57 |
|
---|
| 58 | /** Map page to frame using hierarchical page tables.
|
---|
| 59 | *
|
---|
[9179d0a] | 60 | * Map virtual address page to physical address frame
|
---|
| 61 | * using flags.
|
---|
[6d7ffa65] | 62 | *
|
---|
[da1bafb] | 63 | * @param as Address space to wich page belongs.
|
---|
| 64 | * @param page Virtual address of the page to be mapped.
|
---|
[6d7ffa65] | 65 | * @param frame Physical address of memory frame to which the mapping is done.
|
---|
| 66 | * @param flags Flags to be used for mapping.
|
---|
[da1bafb] | 67 | *
|
---|
[6d7ffa65] | 68 | */
|
---|
[da1bafb] | 69 | void pt_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame,
|
---|
| 70 | unsigned int flags)
|
---|
[6d7ffa65] | 71 | {
|
---|
[da1bafb] | 72 | pte_t *ptl0 = (pte_t *) PA2KA((uintptr_t) as->genarch.page_table);
|
---|
[1d432f9] | 73 |
|
---|
| 74 | ASSERT(page_table_locked(as));
|
---|
[da1bafb] | 75 |
|
---|
[6d7ffa65] | 76 | if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT) {
|
---|
[da1bafb] | 77 | pte_t *newpt = (pte_t *) frame_alloc(PTL1_SIZE, FRAME_KA);
|
---|
[e32e092] | 78 | memsetb(newpt, FRAME_SIZE << PTL1_SIZE, 0);
|
---|
[6d7ffa65] | 79 | SET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page), KA2PA(newpt));
|
---|
| 80 | SET_PTL1_FLAGS(ptl0, PTL0_INDEX(page), PAGE_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE | PAGE_WRITE);
|
---|
| 81 | }
|
---|
[da1bafb] | 82 |
|
---|
| 83 | pte_t *ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
|
---|
| 84 |
|
---|
[6d7ffa65] | 85 | if (GET_PTL2_FLAGS(ptl1, PTL1_INDEX(page)) & PAGE_NOT_PRESENT) {
|
---|
[da1bafb] | 86 | pte_t *newpt = (pte_t *) frame_alloc(PTL2_SIZE, FRAME_KA);
|
---|
[e32e092] | 87 | memsetb(newpt, FRAME_SIZE << PTL2_SIZE, 0);
|
---|
[6d7ffa65] | 88 | SET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page), KA2PA(newpt));
|
---|
| 89 | SET_PTL2_FLAGS(ptl1, PTL1_INDEX(page), PAGE_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE | PAGE_WRITE);
|
---|
| 90 | }
|
---|
[da1bafb] | 91 |
|
---|
| 92 | pte_t *ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page)));
|
---|
| 93 |
|
---|
[6d7ffa65] | 94 | if (GET_PTL3_FLAGS(ptl2, PTL2_INDEX(page)) & PAGE_NOT_PRESENT) {
|
---|
[da1bafb] | 95 | pte_t *newpt = (pte_t *) frame_alloc(PTL3_SIZE, FRAME_KA);
|
---|
[e32e092] | 96 | memsetb(newpt, FRAME_SIZE << PTL3_SIZE, 0);
|
---|
[6d7ffa65] | 97 | SET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page), KA2PA(newpt));
|
---|
| 98 | SET_PTL3_FLAGS(ptl2, PTL2_INDEX(page), PAGE_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE | PAGE_WRITE);
|
---|
| 99 | }
|
---|
[da1bafb] | 100 |
|
---|
| 101 | pte_t *ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
|
---|
| 102 |
|
---|
[6d7ffa65] | 103 | SET_FRAME_ADDRESS(ptl3, PTL3_INDEX(page), frame);
|
---|
| 104 | SET_FRAME_FLAGS(ptl3, PTL3_INDEX(page), flags);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[8f00329] | 107 | /** Remove mapping of page from hierarchical page tables.
|
---|
| 108 | *
|
---|
[9179d0a] | 109 | * Remove any mapping of page within address space as.
|
---|
[8f00329] | 110 | * TLB shootdown should follow in order to make effects of
|
---|
| 111 | * this call visible.
|
---|
| 112 | *
|
---|
[ecbdc724] | 113 | * Empty page tables except PTL0 are freed.
|
---|
| 114 | *
|
---|
[da1bafb] | 115 | * @param as Address space to wich page belongs.
|
---|
[8f00329] | 116 | * @param page Virtual address of the page to be demapped.
|
---|
[da1bafb] | 117 | *
|
---|
[8f00329] | 118 | */
|
---|
[7f1c620] | 119 | void pt_mapping_remove(as_t *as, uintptr_t page)
|
---|
[8f00329] | 120 | {
|
---|
[1d432f9] | 121 | ASSERT(page_table_locked(as));
|
---|
| 122 |
|
---|
[ecbdc724] | 123 | /*
|
---|
| 124 | * First, remove the mapping, if it exists.
|
---|
[da1bafb] | 125 | *
|
---|
[ecbdc724] | 126 | */
|
---|
[da1bafb] | 127 |
|
---|
| 128 | pte_t *ptl0 = (pte_t *) PA2KA((uintptr_t) as->genarch.page_table);
|
---|
[8f00329] | 129 | if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT)
|
---|
| 130 | return;
|
---|
[da1bafb] | 131 |
|
---|
| 132 | pte_t *ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
|
---|
[8f00329] | 133 | if (GET_PTL2_FLAGS(ptl1, PTL1_INDEX(page)) & PAGE_NOT_PRESENT)
|
---|
| 134 | return;
|
---|
[da1bafb] | 135 |
|
---|
| 136 | pte_t *ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page)));
|
---|
[8f00329] | 137 | if (GET_PTL3_FLAGS(ptl2, PTL2_INDEX(page)) & PAGE_NOT_PRESENT)
|
---|
| 138 | return;
|
---|
[da1bafb] | 139 |
|
---|
| 140 | pte_t *ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
|
---|
| 141 |
|
---|
[8f00329] | 142 | /* Destroy the mapping. Setting to PAGE_NOT_PRESENT is not sufficient. */
|
---|
[e32e092] | 143 | memsetb(&ptl3[PTL3_INDEX(page)], sizeof(pte_t), 0);
|
---|
[da1bafb] | 144 |
|
---|
[ecbdc724] | 145 | /*
|
---|
| 146 | * Second, free all empty tables along the way from PTL3 down to PTL0.
|
---|
[da1bafb] | 147 | *
|
---|
[ecbdc724] | 148 | */
|
---|
| 149 |
|
---|
[da1bafb] | 150 | /* Check PTL3 */
|
---|
| 151 | bool empty = true;
|
---|
| 152 |
|
---|
| 153 | unsigned int i;
|
---|
[ecbdc724] | 154 | for (i = 0; i < PTL3_ENTRIES; i++) {
|
---|
| 155 | if (PTE_VALID(&ptl3[i])) {
|
---|
| 156 | empty = false;
|
---|
| 157 | break;
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
[da1bafb] | 160 |
|
---|
[ecbdc724] | 161 | if (empty) {
|
---|
| 162 | /*
|
---|
| 163 | * PTL3 is empty.
|
---|
| 164 | * Release the frame and remove PTL3 pointer from preceding table.
|
---|
[da1bafb] | 165 | *
|
---|
[ecbdc724] | 166 | */
|
---|
[7f1c620] | 167 | frame_free(KA2PA((uintptr_t) ptl3));
|
---|
[da1bafb] | 168 | #if (PTL2_ENTRIES != 0)
|
---|
| 169 | memsetb(&ptl2[PTL2_INDEX(page)], sizeof(pte_t), 0);
|
---|
| 170 | #elif (PTL1_ENTRIES != 0)
|
---|
| 171 | memsetb(&ptl1[PTL1_INDEX(page)], sizeof(pte_t), 0);
|
---|
| 172 | #else
|
---|
| 173 | memsetb(&ptl0[PTL0_INDEX(page)], sizeof(pte_t), 0);
|
---|
| 174 | #endif
|
---|
[ecbdc724] | 175 | } else {
|
---|
| 176 | /*
|
---|
| 177 | * PTL3 is not empty.
|
---|
| 178 | * Therefore, there must be a path from PTL0 to PTL3 and
|
---|
| 179 | * thus nothing to free in higher levels.
|
---|
[da1bafb] | 180 | *
|
---|
[ecbdc724] | 181 | */
|
---|
| 182 | return;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
[da1bafb] | 185 | /* Check PTL2, empty is still true */
|
---|
| 186 | #if (PTL2_ENTRIES != 0)
|
---|
| 187 | for (i = 0; i < PTL2_ENTRIES; i++) {
|
---|
| 188 | if (PTE_VALID(&ptl2[i])) {
|
---|
| 189 | empty = false;
|
---|
| 190 | break;
|
---|
[ecbdc724] | 191 | }
|
---|
| 192 | }
|
---|
[da1bafb] | 193 |
|
---|
| 194 | if (empty) {
|
---|
| 195 | /*
|
---|
| 196 | * PTL2 is empty.
|
---|
| 197 | * Release the frame and remove PTL2 pointer from preceding table.
|
---|
| 198 | *
|
---|
| 199 | */
|
---|
| 200 | frame_free(KA2PA((uintptr_t) ptl2));
|
---|
| 201 | #if (PTL1_ENTRIES != 0)
|
---|
| 202 | memsetb(&ptl1[PTL1_INDEX(page)], sizeof(pte_t), 0);
|
---|
| 203 | #else
|
---|
| 204 | memsetb(&ptl0[PTL0_INDEX(page)], sizeof(pte_t), 0);
|
---|
| 205 | #endif
|
---|
| 206 | } else {
|
---|
| 207 | /*
|
---|
| 208 | * PTL2 is not empty.
|
---|
| 209 | * Therefore, there must be a path from PTL0 to PTL2 and
|
---|
| 210 | * thus nothing to free in higher levels.
|
---|
| 211 | *
|
---|
| 212 | */
|
---|
| 213 | return;
|
---|
| 214 | }
|
---|
| 215 | #endif /* PTL2_ENTRIES != 0 */
|
---|
| 216 |
|
---|
[ecbdc724] | 217 | /* check PTL1, empty is still true */
|
---|
[da1bafb] | 218 | #if (PTL1_ENTRIES != 0)
|
---|
| 219 | for (i = 0; i < PTL1_ENTRIES; i++) {
|
---|
| 220 | if (PTE_VALID(&ptl1[i])) {
|
---|
| 221 | empty = false;
|
---|
| 222 | break;
|
---|
[ecbdc724] | 223 | }
|
---|
| 224 | }
|
---|
[da1bafb] | 225 |
|
---|
| 226 | if (empty) {
|
---|
| 227 | /*
|
---|
| 228 | * PTL1 is empty.
|
---|
| 229 | * Release the frame and remove PTL1 pointer from preceding table.
|
---|
| 230 | *
|
---|
| 231 | */
|
---|
| 232 | frame_free(KA2PA((uintptr_t) ptl1));
|
---|
| 233 | memsetb(&ptl0[PTL0_INDEX(page)], sizeof(pte_t), 0);
|
---|
| 234 | }
|
---|
| 235 | #endif /* PTL1_ENTRIES != 0 */
|
---|
[8f00329] | 236 | }
|
---|
| 237 |
|
---|
[6d7ffa65] | 238 | /** Find mapping for virtual page in hierarchical page tables.
|
---|
| 239 | *
|
---|
| 240 | * Find mapping for virtual page.
|
---|
| 241 | *
|
---|
[da1bafb] | 242 | * @param as Address space to which page belongs.
|
---|
[6d7ffa65] | 243 | * @param page Virtual page.
|
---|
| 244 | *
|
---|
[da1bafb] | 245 | * @return NULL if there is no such mapping; entry from PTL3 describing
|
---|
| 246 | * the mapping otherwise.
|
---|
| 247 | *
|
---|
[6d7ffa65] | 248 | */
|
---|
[7f1c620] | 249 | pte_t *pt_mapping_find(as_t *as, uintptr_t page)
|
---|
[6d7ffa65] | 250 | {
|
---|
[1d432f9] | 251 | ASSERT(page_table_locked(as));
|
---|
| 252 |
|
---|
[da1bafb] | 253 | pte_t *ptl0 = (pte_t *) PA2KA((uintptr_t) as->genarch.page_table);
|
---|
[6d7ffa65] | 254 | if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT)
|
---|
| 255 | return NULL;
|
---|
[da1bafb] | 256 |
|
---|
| 257 | pte_t *ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
|
---|
[6d7ffa65] | 258 | if (GET_PTL2_FLAGS(ptl1, PTL1_INDEX(page)) & PAGE_NOT_PRESENT)
|
---|
| 259 | return NULL;
|
---|
[da1bafb] | 260 |
|
---|
| 261 | pte_t *ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page)));
|
---|
[6d7ffa65] | 262 | if (GET_PTL3_FLAGS(ptl2, PTL2_INDEX(page)) & PAGE_NOT_PRESENT)
|
---|
| 263 | return NULL;
|
---|
[da1bafb] | 264 |
|
---|
| 265 | pte_t *ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
|
---|
| 266 |
|
---|
[6d7ffa65] | 267 | return &ptl3[PTL3_INDEX(page)];
|
---|
| 268 | }
|
---|
[b45c443] | 269 |
|
---|
[f47fd19] | 270 | /** @}
|
---|
[b45c443] | 271 | */
|
---|