[0ee077ee] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Jakub Jermar
|
---|
[0ee077ee] | 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 |
|
---|
[174156fd] | 29 | /** @addtogroup kernel_generic_mm
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
[0ee077ee] | 33 | /**
|
---|
[b45c443] | 34 | * @file
|
---|
[0ee077ee] | 35 | * @brief Backend for address space areas backed by an ELF image.
|
---|
| 36 | */
|
---|
| 37 |
|
---|
[d4b5542] | 38 | #include <lib/elf.h>
|
---|
[63e27ef] | 39 | #include <assert.h>
|
---|
[d99c1d2] | 40 | #include <typedefs.h>
|
---|
[0ee077ee] | 41 | #include <mm/as.h>
|
---|
| 42 | #include <mm/frame.h>
|
---|
| 43 | #include <mm/slab.h>
|
---|
[00b595b] | 44 | #include <mm/page.h>
|
---|
[03523dc] | 45 | #include <mm/reserve.h>
|
---|
[c7f8fc5] | 46 | #include <mm/km.h>
|
---|
[00b595b] | 47 | #include <genarch/mm/page_pt.h>
|
---|
| 48 | #include <genarch/mm/page_ht.h>
|
---|
[0ee077ee] | 49 | #include <align.h>
|
---|
[44a7ee5] | 50 | #include <mem.h>
|
---|
[0ee077ee] | 51 | #include <macros.h>
|
---|
| 52 | #include <arch.h>
|
---|
[05882233] | 53 | #include <barrier.h>
|
---|
[0ee077ee] | 54 |
|
---|
[03523dc] | 55 | static bool elf_create(as_area_t *);
|
---|
| 56 | static bool elf_resize(as_area_t *, size_t);
|
---|
| 57 | static void elf_share(as_area_t *);
|
---|
| 58 | static void elf_destroy(as_area_t *);
|
---|
| 59 |
|
---|
[01029fc] | 60 | static bool elf_is_resizable(as_area_t *);
|
---|
| 61 | static bool elf_is_shareable(as_area_t *);
|
---|
| 62 |
|
---|
[cda1378] | 63 | static int elf_page_fault(as_area_t *, uintptr_t, pf_access_t);
|
---|
| 64 | static void elf_frame_free(as_area_t *, uintptr_t, uintptr_t);
|
---|
[0ee077ee] | 65 |
|
---|
| 66 | mem_backend_t elf_backend = {
|
---|
[03523dc] | 67 | .create = elf_create,
|
---|
| 68 | .resize = elf_resize,
|
---|
| 69 | .share = elf_share,
|
---|
| 70 | .destroy = elf_destroy,
|
---|
| 71 |
|
---|
[01029fc] | 72 | .is_resizable = elf_is_resizable,
|
---|
| 73 | .is_shareable = elf_is_shareable,
|
---|
| 74 |
|
---|
[0ee077ee] | 75 | .page_fault = elf_page_fault,
|
---|
| 76 | .frame_free = elf_frame_free,
|
---|
[83b6ba9f] | 77 |
|
---|
| 78 | .create_shared_data = NULL,
|
---|
| 79 | .destroy_shared_data = NULL
|
---|
[0ee077ee] | 80 | };
|
---|
| 81 |
|
---|
[3ac69647] | 82 | static size_t elf_nonanon_pages_get(as_area_t *area)
|
---|
[03523dc] | 83 | {
|
---|
[9dd730d1] | 84 | elf_segment_header_t *entry = area->backend_data.segment;
|
---|
[8f6c6264] | 85 | uintptr_t first = ALIGN_UP(entry->p_vaddr, PAGE_SIZE);
|
---|
| 86 | uintptr_t last = ALIGN_DOWN(entry->p_vaddr + entry->p_filesz,
|
---|
| 87 | PAGE_SIZE);
|
---|
[9dd730d1] | 88 |
|
---|
[2c86f81] | 89 | if (entry->p_flags & PF_W)
|
---|
[3ac69647] | 90 | return 0;
|
---|
| 91 |
|
---|
[8f6c6264] | 92 | if (last < first)
|
---|
| 93 | return 0;
|
---|
| 94 |
|
---|
| 95 | return last - first;
|
---|
[3ac69647] | 96 | }
|
---|
| 97 |
|
---|
| 98 | bool elf_create(as_area_t *area)
|
---|
| 99 | {
|
---|
| 100 | size_t nonanon_pages = elf_nonanon_pages_get(area);
|
---|
[2c86f81] | 101 |
|
---|
[9dd730d1] | 102 | if (area->pages <= nonanon_pages)
|
---|
| 103 | return true;
|
---|
[a35b458] | 104 |
|
---|
[9dd730d1] | 105 | return reserve_try_alloc(area->pages - nonanon_pages);
|
---|
[03523dc] | 106 | }
|
---|
| 107 |
|
---|
| 108 | bool elf_resize(as_area_t *area, size_t new_pages)
|
---|
| 109 | {
|
---|
[3ac69647] | 110 | size_t nonanon_pages = elf_nonanon_pages_get(area);
|
---|
[2c86f81] | 111 |
|
---|
[9dd730d1] | 112 | if (new_pages > area->pages) {
|
---|
| 113 | /* The area is growing. */
|
---|
| 114 | if (area->pages >= nonanon_pages)
|
---|
| 115 | return reserve_try_alloc(new_pages - area->pages);
|
---|
| 116 | else if (new_pages > nonanon_pages)
|
---|
| 117 | return reserve_try_alloc(new_pages - nonanon_pages);
|
---|
| 118 | } else if (new_pages < area->pages) {
|
---|
| 119 | /* The area is shrinking. */
|
---|
| 120 | if (new_pages >= nonanon_pages)
|
---|
| 121 | reserve_free(area->pages - new_pages);
|
---|
| 122 | else if (area->pages > nonanon_pages)
|
---|
| 123 | reserve_free(nonanon_pages - new_pages);
|
---|
| 124 | }
|
---|
[a35b458] | 125 |
|
---|
[03523dc] | 126 | return true;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | /** Share ELF image backed address space area.
|
---|
| 130 | *
|
---|
| 131 | * If the area is writable, then all mapped pages are duplicated in the pagemap.
|
---|
| 132 | * Otherwise only portions of the area that are not backed by the ELF image
|
---|
| 133 | * are put into the pagemap.
|
---|
| 134 | *
|
---|
| 135 | * @param area Address space area.
|
---|
| 136 | */
|
---|
| 137 | void elf_share(as_area_t *area)
|
---|
| 138 | {
|
---|
| 139 | elf_segment_header_t *entry = area->backend_data.segment;
|
---|
| 140 | link_t *cur;
|
---|
| 141 | btree_node_t *leaf, *node;
|
---|
| 142 | uintptr_t start_anon = entry->p_vaddr + entry->p_filesz;
|
---|
| 143 |
|
---|
[63e27ef] | 144 | assert(mutex_locked(&area->as->lock));
|
---|
| 145 | assert(mutex_locked(&area->lock));
|
---|
[03523dc] | 146 |
|
---|
| 147 | /*
|
---|
| 148 | * Find the node in which to start linear search.
|
---|
| 149 | */
|
---|
| 150 | if (area->flags & AS_AREA_WRITE) {
|
---|
[55b77d9] | 151 | node = list_get_instance(list_first(&area->used_space.leaf_list),
|
---|
[03523dc] | 152 | btree_node_t, leaf_link);
|
---|
| 153 | } else {
|
---|
| 154 | (void) btree_search(&area->sh_info->pagemap, start_anon, &leaf);
|
---|
| 155 | node = btree_leaf_node_left_neighbour(&area->sh_info->pagemap,
|
---|
| 156 | leaf);
|
---|
| 157 | if (!node)
|
---|
| 158 | node = leaf;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | /*
|
---|
| 162 | * Copy used anonymous portions of the area to sh_info's page map.
|
---|
| 163 | */
|
---|
| 164 | mutex_lock(&area->sh_info->lock);
|
---|
[55b77d9] | 165 | for (cur = &node->leaf_link; cur != &area->used_space.leaf_list.head;
|
---|
[03523dc] | 166 | cur = cur->next) {
|
---|
| 167 | unsigned int i;
|
---|
[a35b458] | 168 |
|
---|
[03523dc] | 169 | node = list_get_instance(cur, btree_node_t, leaf_link);
|
---|
[a35b458] | 170 |
|
---|
[03523dc] | 171 | for (i = 0; i < node->keys; i++) {
|
---|
| 172 | uintptr_t base = node->key[i];
|
---|
| 173 | size_t count = (size_t) node->value[i];
|
---|
| 174 | unsigned int j;
|
---|
[a35b458] | 175 |
|
---|
[03523dc] | 176 | /*
|
---|
| 177 | * Skip read-only areas of used space that are backed
|
---|
| 178 | * by the ELF image.
|
---|
| 179 | */
|
---|
| 180 | if (!(area->flags & AS_AREA_WRITE))
|
---|
| 181 | if (base >= entry->p_vaddr &&
|
---|
[b4ffe5bc] | 182 | base + P2SZ(count) <= start_anon)
|
---|
[03523dc] | 183 | continue;
|
---|
[a35b458] | 184 |
|
---|
[03523dc] | 185 | for (j = 0; j < count; j++) {
|
---|
[38dc82d] | 186 | pte_t pte;
|
---|
| 187 | bool found;
|
---|
[a35b458] | 188 |
|
---|
[03523dc] | 189 | /*
|
---|
| 190 | * Skip read-only pages that are backed by the
|
---|
| 191 | * ELF image.
|
---|
| 192 | */
|
---|
| 193 | if (!(area->flags & AS_AREA_WRITE))
|
---|
| 194 | if (base >= entry->p_vaddr &&
|
---|
[b4ffe5bc] | 195 | base + P2SZ(j + 1) <= start_anon)
|
---|
[03523dc] | 196 | continue;
|
---|
[a35b458] | 197 |
|
---|
[03523dc] | 198 | page_table_lock(area->as, false);
|
---|
[38dc82d] | 199 | found = page_mapping_find(area->as,
|
---|
| 200 | base + P2SZ(j), false, &pte);
|
---|
| 201 |
|
---|
[63e27ef] | 202 | assert(found);
|
---|
| 203 | assert(PTE_VALID(&pte));
|
---|
| 204 | assert(PTE_PRESENT(&pte));
|
---|
[38dc82d] | 205 |
|
---|
[03523dc] | 206 | btree_insert(&area->sh_info->pagemap,
|
---|
[b4ffe5bc] | 207 | (base + P2SZ(j)) - area->base,
|
---|
[38dc82d] | 208 | (void *) PTE_GET_FRAME(&pte), NULL);
|
---|
[03523dc] | 209 | page_table_unlock(area->as, false);
|
---|
| 210 |
|
---|
[38dc82d] | 211 | pfn_t pfn = ADDR2PFN(PTE_GET_FRAME(&pte));
|
---|
[03523dc] | 212 | frame_reference_add(pfn);
|
---|
| 213 | }
|
---|
[a35b458] | 214 |
|
---|
[03523dc] | 215 | }
|
---|
| 216 | }
|
---|
| 217 | mutex_unlock(&area->sh_info->lock);
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | void elf_destroy(as_area_t *area)
|
---|
| 221 | {
|
---|
[3ac69647] | 222 | size_t nonanon_pages = elf_nonanon_pages_get(area);
|
---|
[2c86f81] | 223 |
|
---|
[9dd730d1] | 224 | if (area->pages > nonanon_pages)
|
---|
| 225 | reserve_free(area->pages - nonanon_pages);
|
---|
[03523dc] | 226 | }
|
---|
| 227 |
|
---|
[01029fc] | 228 | bool elf_is_resizable(as_area_t *area)
|
---|
| 229 | {
|
---|
| 230 | return true;
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | bool elf_is_shareable(as_area_t *area)
|
---|
| 234 | {
|
---|
| 235 | return true;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
[0ee077ee] | 238 | /** Service a page fault in the ELF backend address space area.
|
---|
| 239 | *
|
---|
| 240 | * The address space area and page tables must be already locked.
|
---|
| 241 | *
|
---|
[36e86862] | 242 | * @param area Pointer to the address space area.
|
---|
[59fb782] | 243 | * @param upage Faulting virtual page.
|
---|
[36e86862] | 244 | * @param access Access mode that caused the fault (i.e.
|
---|
| 245 | * read/write/exec).
|
---|
[0ee077ee] | 246 | *
|
---|
[36e86862] | 247 | * @return AS_PF_FAULT on failure (i.e. page fault) or AS_PF_OK
|
---|
| 248 | * on success (i.e. serviced).
|
---|
[0ee077ee] | 249 | */
|
---|
[59fb782] | 250 | int elf_page_fault(as_area_t *area, uintptr_t upage, pf_access_t access)
|
---|
[0ee077ee] | 251 | {
|
---|
[127c957b] | 252 | elf_header_t *elf = area->backend_data.elf;
|
---|
| 253 | elf_segment_header_t *entry = area->backend_data.segment;
|
---|
[00b595b] | 254 | btree_node_t *leaf;
|
---|
[c7f8fc5] | 255 | uintptr_t base;
|
---|
| 256 | uintptr_t frame;
|
---|
| 257 | uintptr_t kpage;
|
---|
| 258 | uintptr_t start_anon;
|
---|
[98000fb] | 259 | size_t i;
|
---|
[454f1da] | 260 | bool dirty = false;
|
---|
[0ee077ee] | 261 |
|
---|
[63e27ef] | 262 | assert(page_table_locked(AS));
|
---|
| 263 | assert(mutex_locked(&area->lock));
|
---|
| 264 | assert(IS_ALIGNED(upage, PAGE_SIZE));
|
---|
[1d432f9] | 265 |
|
---|
[0ee077ee] | 266 | if (!as_area_check_access(area, access))
|
---|
| 267 | return AS_PF_FAULT;
|
---|
[a35b458] | 268 |
|
---|
[59fb782] | 269 | if (upage < ALIGN_DOWN(entry->p_vaddr, PAGE_SIZE))
|
---|
[917a8c8] | 270 | return AS_PF_FAULT;
|
---|
[a35b458] | 271 |
|
---|
[59fb782] | 272 | if (upage >= entry->p_vaddr + entry->p_memsz)
|
---|
[917a8c8] | 273 | return AS_PF_FAULT;
|
---|
[a35b458] | 274 |
|
---|
[59fb782] | 275 | i = (upage - ALIGN_DOWN(entry->p_vaddr, PAGE_SIZE)) >> PAGE_WIDTH;
|
---|
[1cc2974] | 276 | base = (uintptr_t)
|
---|
| 277 | (((void *) elf) + ALIGN_DOWN(entry->p_offset, PAGE_SIZE));
|
---|
| 278 |
|
---|
| 279 | /* Virtual address of the end of initialized part of segment */
|
---|
| 280 | start_anon = entry->p_vaddr + entry->p_filesz;
|
---|
[00b595b] | 281 |
|
---|
[83b6ba9f] | 282 | mutex_lock(&area->sh_info->lock);
|
---|
| 283 | if (area->sh_info->shared) {
|
---|
[00b595b] | 284 | bool found = false;
|
---|
| 285 |
|
---|
| 286 | /*
|
---|
| 287 | * The address space area is shared.
|
---|
| 288 | */
|
---|
[a35b458] | 289 |
|
---|
[7f1c620] | 290 | frame = (uintptr_t) btree_search(&area->sh_info->pagemap,
|
---|
[c7f8fc5] | 291 | upage - area->base, &leaf);
|
---|
[00b595b] | 292 | if (!frame) {
|
---|
[6c441cf8] | 293 | unsigned int i;
|
---|
[00b595b] | 294 |
|
---|
| 295 | /*
|
---|
| 296 | * Workaround for valid NULL address.
|
---|
| 297 | */
|
---|
| 298 |
|
---|
| 299 | for (i = 0; i < leaf->keys; i++) {
|
---|
[c7f8fc5] | 300 | if (leaf->key[i] == upage - area->base) {
|
---|
[00b595b] | 301 | found = true;
|
---|
| 302 | break;
|
---|
| 303 | }
|
---|
| 304 | }
|
---|
| 305 | }
|
---|
| 306 | if (frame || found) {
|
---|
[c9d2235b] | 307 | frame_reference_add(ADDR2PFN(frame));
|
---|
[c7f8fc5] | 308 | page_mapping_insert(AS, upage, frame,
|
---|
[d5bd8d7] | 309 | as_area_get_flags(area));
|
---|
[c7f8fc5] | 310 | if (!used_space_insert(area, upage, 1))
|
---|
[f651e80] | 311 | panic("Cannot insert used space.");
|
---|
[00b595b] | 312 | mutex_unlock(&area->sh_info->lock);
|
---|
| 313 | return AS_PF_OK;
|
---|
| 314 | }
|
---|
| 315 | }
|
---|
[1cc2974] | 316 |
|
---|
[00b595b] | 317 | /*
|
---|
[d5bd8d7] | 318 | * The area is either not shared or the pagemap does not contain the
|
---|
| 319 | * mapping.
|
---|
[00b595b] | 320 | */
|
---|
[c7f8fc5] | 321 | if (upage >= entry->p_vaddr && upage + PAGE_SIZE <= start_anon) {
|
---|
[0ee077ee] | 322 | /*
|
---|
| 323 | * Initialized portion of the segment. The memory is backed
|
---|
| 324 | * directly by the content of the ELF image. Pages are
|
---|
| 325 | * only copied if the segment is writable so that there
|
---|
| 326 | * can be more instantions of the same memory ELF image
|
---|
| 327 | * used at a time. Note that this could be later done
|
---|
| 328 | * as COW.
|
---|
| 329 | */
|
---|
| 330 | if (entry->p_flags & PF_W) {
|
---|
[c7f8fc5] | 331 | kpage = km_temporary_page_get(&frame, FRAME_NO_RESERVE);
|
---|
[d56382d] | 332 | memcpy((void *) kpage, (void *) (base + i * PAGE_SIZE),
|
---|
[c7f8fc5] | 333 | PAGE_SIZE);
|
---|
[d56382d] | 334 | if (entry->p_flags & PF_X) {
|
---|
[0abc2ae] | 335 | smc_coherence((void *) kpage, PAGE_SIZE);
|
---|
[d56382d] | 336 | }
|
---|
[c7f8fc5] | 337 | km_temporary_page_put(kpage);
|
---|
[454f1da] | 338 | dirty = true;
|
---|
[0ee077ee] | 339 | } else {
|
---|
[38dc82d] | 340 | pte_t pte;
|
---|
| 341 | bool found;
|
---|
| 342 |
|
---|
| 343 | found = page_mapping_find(AS_KERNEL,
|
---|
| 344 | base + i * FRAME_SIZE, true, &pte);
|
---|
[32817cc] | 345 |
|
---|
[63e27ef] | 346 | assert(found);
|
---|
| 347 | assert(PTE_PRESENT(&pte));
|
---|
[32817cc] | 348 |
|
---|
[38dc82d] | 349 | frame = PTE_GET_FRAME(&pte);
|
---|
[1b20da0] | 350 | }
|
---|
[c7f8fc5] | 351 | } else if (upage >= start_anon) {
|
---|
[0ee077ee] | 352 | /*
|
---|
| 353 | * This is the uninitialized portion of the segment.
|
---|
| 354 | * It is not physically present in the ELF image.
|
---|
| 355 | * To resolve the situation, a frame must be allocated
|
---|
| 356 | * and cleared.
|
---|
| 357 | */
|
---|
[c7f8fc5] | 358 | kpage = km_temporary_page_get(&frame, FRAME_NO_RESERVE);
|
---|
| 359 | memsetb((void *) kpage, PAGE_SIZE, 0);
|
---|
| 360 | km_temporary_page_put(kpage);
|
---|
[454f1da] | 361 | dirty = true;
|
---|
[0ee077ee] | 362 | } else {
|
---|
[1cc2974] | 363 | size_t pad_lo, pad_hi;
|
---|
[0ee077ee] | 364 | /*
|
---|
| 365 | * The mixed case.
|
---|
[1cc2974] | 366 | *
|
---|
| 367 | * The middle part is backed by the ELF image and
|
---|
| 368 | * the lower and upper parts are anonymous memory.
|
---|
| 369 | * (The segment can be and often is shorter than 1 page).
|
---|
[0ee077ee] | 370 | */
|
---|
[c7f8fc5] | 371 | if (upage < entry->p_vaddr)
|
---|
| 372 | pad_lo = entry->p_vaddr - upage;
|
---|
[1cc2974] | 373 | else
|
---|
| 374 | pad_lo = 0;
|
---|
| 375 |
|
---|
[c7f8fc5] | 376 | if (start_anon < upage + PAGE_SIZE)
|
---|
| 377 | pad_hi = upage + PAGE_SIZE - start_anon;
|
---|
[1cc2974] | 378 | else
|
---|
| 379 | pad_hi = 0;
|
---|
| 380 |
|
---|
[c7f8fc5] | 381 | kpage = km_temporary_page_get(&frame, FRAME_NO_RESERVE);
|
---|
| 382 | memcpy((void *) (kpage + pad_lo),
|
---|
| 383 | (void *) (base + i * PAGE_SIZE + pad_lo),
|
---|
| 384 | PAGE_SIZE - pad_lo - pad_hi);
|
---|
[62cd66f] | 385 | if (entry->p_flags & PF_X) {
|
---|
[0abc2ae] | 386 | smc_coherence((void *) (kpage + pad_lo),
|
---|
[c7f8fc5] | 387 | PAGE_SIZE - pad_lo - pad_hi);
|
---|
[62cd66f] | 388 | }
|
---|
[c7f8fc5] | 389 | memsetb((void *) kpage, pad_lo, 0);
|
---|
| 390 | memsetb((void *) (kpage + PAGE_SIZE - pad_hi), pad_hi, 0);
|
---|
| 391 | km_temporary_page_put(kpage);
|
---|
[454f1da] | 392 | dirty = true;
|
---|
[1cc2974] | 393 | }
|
---|
[00b595b] | 394 |
|
---|
[83b6ba9f] | 395 | if (dirty && area->sh_info->shared) {
|
---|
[1cc2974] | 396 | frame_reference_add(ADDR2PFN(frame));
|
---|
[c7f8fc5] | 397 | btree_insert(&area->sh_info->pagemap, upage - area->base,
|
---|
[1cc2974] | 398 | (void *) frame, leaf);
|
---|
[0ee077ee] | 399 | }
|
---|
[1cc2974] | 400 |
|
---|
[83b6ba9f] | 401 | mutex_unlock(&area->sh_info->lock);
|
---|
[1cc2974] | 402 |
|
---|
[c7f8fc5] | 403 | page_mapping_insert(AS, upage, frame, as_area_get_flags(area));
|
---|
| 404 | if (!used_space_insert(area, upage, 1))
|
---|
[f651e80] | 405 | panic("Cannot insert used space.");
|
---|
[0ee077ee] | 406 |
|
---|
| 407 | return AS_PF_OK;
|
---|
| 408 | }
|
---|
| 409 |
|
---|
| 410 | /** Free a frame that is backed by the ELF backend.
|
---|
| 411 | *
|
---|
| 412 | * The address space area and page tables must be already locked.
|
---|
| 413 | *
|
---|
[36e86862] | 414 | * @param area Pointer to the address space area.
|
---|
| 415 | * @param page Page that is mapped to frame. Must be aligned to
|
---|
| 416 | * PAGE_SIZE.
|
---|
| 417 | * @param frame Frame to be released.
|
---|
[0ee077ee] | 418 | *
|
---|
| 419 | */
|
---|
[7f1c620] | 420 | void elf_frame_free(as_area_t *area, uintptr_t page, uintptr_t frame)
|
---|
[0ee077ee] | 421 | {
|
---|
[127c957b] | 422 | elf_segment_header_t *entry = area->backend_data.segment;
|
---|
[137691a] | 423 | uintptr_t start_anon;
|
---|
[1cc2974] | 424 |
|
---|
[63e27ef] | 425 | assert(page_table_locked(area->as));
|
---|
| 426 | assert(mutex_locked(&area->lock));
|
---|
[1d432f9] | 427 |
|
---|
[63e27ef] | 428 | assert(page >= ALIGN_DOWN(entry->p_vaddr, PAGE_SIZE));
|
---|
| 429 | assert(page < entry->p_vaddr + entry->p_memsz);
|
---|
[1d432f9] | 430 |
|
---|
[1cc2974] | 431 | start_anon = entry->p_vaddr + entry->p_filesz;
|
---|
| 432 |
|
---|
| 433 | if (page >= entry->p_vaddr && page + PAGE_SIZE <= start_anon) {
|
---|
[0ee077ee] | 434 | if (entry->p_flags & PF_W) {
|
---|
| 435 | /*
|
---|
[d5bd8d7] | 436 | * Free the frame with the copy of writable segment
|
---|
| 437 | * data.
|
---|
[0ee077ee] | 438 | */
|
---|
[5df1963] | 439 | frame_free_noreserve(frame, 1);
|
---|
[0ee077ee] | 440 | }
|
---|
| 441 | } else {
|
---|
| 442 | /*
|
---|
[d5bd8d7] | 443 | * The frame is either anonymous memory or the mixed case (i.e.
|
---|
| 444 | * lower part is backed by the ELF image and the upper is
|
---|
| 445 | * anonymous). In any case, a frame needs to be freed.
|
---|
[137691a] | 446 | */
|
---|
[5df1963] | 447 | frame_free_noreserve(frame, 1);
|
---|
[0ee077ee] | 448 | }
|
---|
| 449 | }
|
---|
[00b595b] | 450 |
|
---|
[cc73a8a1] | 451 | /** @}
|
---|
[b45c443] | 452 | */
|
---|