[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 |
|
---|
[d91488d] | 98 | /** Get page number in the task where the ELF page originates from.
|
---|
| 99 | *
|
---|
| 100 | * The ELF page can be shared to a different address than it originated from,
|
---|
| 101 | * but we need the originating address since that corresponds to the ELF's
|
---|
| 102 | * virtual addesses.
|
---|
| 103 | *
|
---|
| 104 | * @param area Area in which the page resides
|
---|
| 105 | * @param page Virtual address of the page in @a area
|
---|
| 106 | * @return Virtual address of the page in the origin address space
|
---|
| 107 | */
|
---|
| 108 | static uintptr_t elf_orig_page(as_area_t *area, uintptr_t page)
|
---|
| 109 | {
|
---|
| 110 | return page - area->base + area->backend_data.elf_base;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
[3ac69647] | 113 | bool elf_create(as_area_t *area)
|
---|
| 114 | {
|
---|
| 115 | size_t nonanon_pages = elf_nonanon_pages_get(area);
|
---|
[2c86f81] | 116 |
|
---|
[9dd730d1] | 117 | if (area->pages <= nonanon_pages)
|
---|
| 118 | return true;
|
---|
[a35b458] | 119 |
|
---|
[9dd730d1] | 120 | return reserve_try_alloc(area->pages - nonanon_pages);
|
---|
[03523dc] | 121 | }
|
---|
| 122 |
|
---|
| 123 | bool elf_resize(as_area_t *area, size_t new_pages)
|
---|
| 124 | {
|
---|
[3ac69647] | 125 | size_t nonanon_pages = elf_nonanon_pages_get(area);
|
---|
[2c86f81] | 126 |
|
---|
[9dd730d1] | 127 | if (new_pages > area->pages) {
|
---|
| 128 | /* The area is growing. */
|
---|
| 129 | if (area->pages >= nonanon_pages)
|
---|
| 130 | return reserve_try_alloc(new_pages - area->pages);
|
---|
| 131 | else if (new_pages > nonanon_pages)
|
---|
| 132 | return reserve_try_alloc(new_pages - nonanon_pages);
|
---|
| 133 | } else if (new_pages < area->pages) {
|
---|
| 134 | /* The area is shrinking. */
|
---|
| 135 | if (new_pages >= nonanon_pages)
|
---|
| 136 | reserve_free(area->pages - new_pages);
|
---|
| 137 | else if (area->pages > nonanon_pages)
|
---|
| 138 | reserve_free(nonanon_pages - new_pages);
|
---|
| 139 | }
|
---|
[a35b458] | 140 |
|
---|
[03523dc] | 141 | return true;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | /** Share ELF image backed address space area.
|
---|
| 145 | *
|
---|
| 146 | * If the area is writable, then all mapped pages are duplicated in the pagemap.
|
---|
| 147 | * Otherwise only portions of the area that are not backed by the ELF image
|
---|
| 148 | * are put into the pagemap.
|
---|
| 149 | *
|
---|
| 150 | * @param area Address space area.
|
---|
| 151 | */
|
---|
| 152 | void elf_share(as_area_t *area)
|
---|
| 153 | {
|
---|
| 154 | elf_segment_header_t *entry = area->backend_data.segment;
|
---|
[7be8d4d] | 155 | used_space_ival_t *start;
|
---|
| 156 | used_space_ival_t *cur;
|
---|
[03523dc] | 157 | uintptr_t start_anon = entry->p_vaddr + entry->p_filesz;
|
---|
| 158 |
|
---|
[63e27ef] | 159 | assert(mutex_locked(&area->as->lock));
|
---|
| 160 | assert(mutex_locked(&area->lock));
|
---|
[03523dc] | 161 |
|
---|
| 162 | /*
|
---|
| 163 | * Find the node in which to start linear search.
|
---|
| 164 | */
|
---|
| 165 | if (area->flags & AS_AREA_WRITE) {
|
---|
[7be8d4d] | 166 | start = used_space_first(&area->used_space);
|
---|
[03523dc] | 167 | } else {
|
---|
[7be8d4d] | 168 | /* Find first interval containing addresses >= start_anon */
|
---|
| 169 | start = used_space_find_gteq(&area->used_space, start_anon);
|
---|
[03523dc] | 170 | }
|
---|
| 171 |
|
---|
| 172 | /*
|
---|
| 173 | * Copy used anonymous portions of the area to sh_info's page map.
|
---|
| 174 | */
|
---|
| 175 | mutex_lock(&area->sh_info->lock);
|
---|
[7be8d4d] | 176 | cur = start;
|
---|
| 177 | while (cur != NULL) {
|
---|
| 178 | uintptr_t base = cur->page;
|
---|
| 179 | size_t count = cur->count;
|
---|
[03523dc] | 180 | unsigned int i;
|
---|
[a35b458] | 181 |
|
---|
[7be8d4d] | 182 | /*
|
---|
| 183 | * Skip read-only areas of used space that are backed
|
---|
| 184 | * by the ELF image.
|
---|
| 185 | */
|
---|
| 186 | if (!(area->flags & AS_AREA_WRITE))
|
---|
| 187 | if (base >= entry->p_vaddr &&
|
---|
| 188 | base + P2SZ(count) <= start_anon)
|
---|
| 189 | continue;
|
---|
[a35b458] | 190 |
|
---|
[7be8d4d] | 191 | for (i = 0; i < count; i++) {
|
---|
| 192 | pte_t pte;
|
---|
| 193 | bool found;
|
---|
[a35b458] | 194 |
|
---|
[03523dc] | 195 | /*
|
---|
[7be8d4d] | 196 | * Skip read-only pages that are backed by the
|
---|
| 197 | * ELF image.
|
---|
[03523dc] | 198 | */
|
---|
| 199 | if (!(area->flags & AS_AREA_WRITE))
|
---|
| 200 | if (base >= entry->p_vaddr &&
|
---|
[7be8d4d] | 201 | base + P2SZ(i + 1) <= start_anon)
|
---|
[03523dc] | 202 | continue;
|
---|
[a35b458] | 203 |
|
---|
[7be8d4d] | 204 | page_table_lock(area->as, false);
|
---|
| 205 | found = page_mapping_find(area->as,
|
---|
| 206 | base + P2SZ(i), false, &pte);
|
---|
| 207 |
|
---|
| 208 | (void) found;
|
---|
| 209 | assert(found);
|
---|
| 210 | assert(PTE_VALID(&pte));
|
---|
| 211 | assert(PTE_PRESENT(&pte));
|
---|
[a35b458] | 212 |
|
---|
[7be8d4d] | 213 | as_pagemap_insert(&area->sh_info->pagemap,
|
---|
| 214 | (base + P2SZ(i)) - area->base,
|
---|
| 215 | PTE_GET_FRAME(&pte));
|
---|
| 216 | page_table_unlock(area->as, false);
|
---|
| 217 |
|
---|
| 218 | pfn_t pfn = ADDR2PFN(PTE_GET_FRAME(&pte));
|
---|
| 219 | frame_reference_add(pfn);
|
---|
[03523dc] | 220 | }
|
---|
[7be8d4d] | 221 |
|
---|
| 222 | cur = used_space_next(cur);
|
---|
[03523dc] | 223 | }
|
---|
[7be8d4d] | 224 |
|
---|
[03523dc] | 225 | mutex_unlock(&area->sh_info->lock);
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | void elf_destroy(as_area_t *area)
|
---|
| 229 | {
|
---|
[3ac69647] | 230 | size_t nonanon_pages = elf_nonanon_pages_get(area);
|
---|
[2c86f81] | 231 |
|
---|
[9dd730d1] | 232 | if (area->pages > nonanon_pages)
|
---|
| 233 | reserve_free(area->pages - nonanon_pages);
|
---|
[03523dc] | 234 | }
|
---|
| 235 |
|
---|
[01029fc] | 236 | bool elf_is_resizable(as_area_t *area)
|
---|
| 237 | {
|
---|
| 238 | return true;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | bool elf_is_shareable(as_area_t *area)
|
---|
| 242 | {
|
---|
| 243 | return true;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
[0ee077ee] | 246 | /** Service a page fault in the ELF backend address space area.
|
---|
| 247 | *
|
---|
| 248 | * The address space area and page tables must be already locked.
|
---|
| 249 | *
|
---|
[36e86862] | 250 | * @param area Pointer to the address space area.
|
---|
[59fb782] | 251 | * @param upage Faulting virtual page.
|
---|
[36e86862] | 252 | * @param access Access mode that caused the fault (i.e.
|
---|
| 253 | * read/write/exec).
|
---|
[0ee077ee] | 254 | *
|
---|
[36e86862] | 255 | * @return AS_PF_FAULT on failure (i.e. page fault) or AS_PF_OK
|
---|
| 256 | * on success (i.e. serviced).
|
---|
[0ee077ee] | 257 | */
|
---|
[59fb782] | 258 | int elf_page_fault(as_area_t *area, uintptr_t upage, pf_access_t access)
|
---|
[0ee077ee] | 259 | {
|
---|
[127c957b] | 260 | elf_header_t *elf = area->backend_data.elf;
|
---|
| 261 | elf_segment_header_t *entry = area->backend_data.segment;
|
---|
[c7f8fc5] | 262 | uintptr_t base;
|
---|
| 263 | uintptr_t frame;
|
---|
| 264 | uintptr_t kpage;
|
---|
| 265 | uintptr_t start_anon;
|
---|
[d91488d] | 266 | uintptr_t elfpage;
|
---|
[98000fb] | 267 | size_t i;
|
---|
[454f1da] | 268 | bool dirty = false;
|
---|
[0ee077ee] | 269 |
|
---|
[63e27ef] | 270 | assert(page_table_locked(AS));
|
---|
| 271 | assert(mutex_locked(&area->lock));
|
---|
| 272 | assert(IS_ALIGNED(upage, PAGE_SIZE));
|
---|
[1d432f9] | 273 |
|
---|
[d91488d] | 274 | elfpage = elf_orig_page(area, upage);
|
---|
| 275 |
|
---|
[0ee077ee] | 276 | if (!as_area_check_access(area, access))
|
---|
| 277 | return AS_PF_FAULT;
|
---|
[a35b458] | 278 |
|
---|
[d91488d] | 279 | if (elfpage < ALIGN_DOWN(entry->p_vaddr, PAGE_SIZE))
|
---|
[917a8c8] | 280 | return AS_PF_FAULT;
|
---|
[a35b458] | 281 |
|
---|
[d91488d] | 282 | if (elfpage >= entry->p_vaddr + entry->p_memsz)
|
---|
[917a8c8] | 283 | return AS_PF_FAULT;
|
---|
[a35b458] | 284 |
|
---|
[d91488d] | 285 | i = (elfpage - ALIGN_DOWN(entry->p_vaddr, PAGE_SIZE)) >>
|
---|
| 286 | PAGE_WIDTH;
|
---|
[1cc2974] | 287 | base = (uintptr_t)
|
---|
| 288 | (((void *) elf) + ALIGN_DOWN(entry->p_offset, PAGE_SIZE));
|
---|
| 289 |
|
---|
| 290 | /* Virtual address of the end of initialized part of segment */
|
---|
| 291 | start_anon = entry->p_vaddr + entry->p_filesz;
|
---|
[00b595b] | 292 |
|
---|
[83b6ba9f] | 293 | mutex_lock(&area->sh_info->lock);
|
---|
| 294 | if (area->sh_info->shared) {
|
---|
[00b595b] | 295 | /*
|
---|
| 296 | * The address space area is shared.
|
---|
| 297 | */
|
---|
[a35b458] | 298 |
|
---|
[de0af3a] | 299 | errno_t rc = as_pagemap_find(&area->sh_info->pagemap,
|
---|
| 300 | upage - area->base, &frame);
|
---|
| 301 | if (rc == EOK) {
|
---|
[c9d2235b] | 302 | frame_reference_add(ADDR2PFN(frame));
|
---|
[c7f8fc5] | 303 | page_mapping_insert(AS, upage, frame,
|
---|
[d5bd8d7] | 304 | as_area_get_flags(area));
|
---|
[7be8d4d] | 305 | if (!used_space_insert(&area->used_space, upage, 1))
|
---|
[f651e80] | 306 | panic("Cannot insert used space.");
|
---|
[00b595b] | 307 | mutex_unlock(&area->sh_info->lock);
|
---|
| 308 | return AS_PF_OK;
|
---|
| 309 | }
|
---|
| 310 | }
|
---|
[1cc2974] | 311 |
|
---|
[00b595b] | 312 | /*
|
---|
[d5bd8d7] | 313 | * The area is either not shared or the pagemap does not contain the
|
---|
| 314 | * mapping.
|
---|
[00b595b] | 315 | */
|
---|
[d91488d] | 316 | if (elfpage >= entry->p_vaddr && elfpage + PAGE_SIZE <= start_anon) {
|
---|
[0ee077ee] | 317 | /*
|
---|
| 318 | * Initialized portion of the segment. The memory is backed
|
---|
| 319 | * directly by the content of the ELF image. Pages are
|
---|
| 320 | * only copied if the segment is writable so that there
|
---|
[d91488d] | 321 | * can be more instances of the same memory ELF image
|
---|
[0ee077ee] | 322 | * used at a time. Note that this could be later done
|
---|
| 323 | * as COW.
|
---|
| 324 | */
|
---|
| 325 | if (entry->p_flags & PF_W) {
|
---|
[c7f8fc5] | 326 | kpage = km_temporary_page_get(&frame, FRAME_NO_RESERVE);
|
---|
[d56382d] | 327 | memcpy((void *) kpage, (void *) (base + i * PAGE_SIZE),
|
---|
[c7f8fc5] | 328 | PAGE_SIZE);
|
---|
[d56382d] | 329 | if (entry->p_flags & PF_X) {
|
---|
[0abc2ae] | 330 | smc_coherence((void *) kpage, PAGE_SIZE);
|
---|
[d56382d] | 331 | }
|
---|
[c7f8fc5] | 332 | km_temporary_page_put(kpage);
|
---|
[454f1da] | 333 | dirty = true;
|
---|
[0ee077ee] | 334 | } else {
|
---|
[38dc82d] | 335 | pte_t pte;
|
---|
| 336 | bool found;
|
---|
| 337 |
|
---|
| 338 | found = page_mapping_find(AS_KERNEL,
|
---|
| 339 | base + i * FRAME_SIZE, true, &pte);
|
---|
[32817cc] | 340 |
|
---|
[0705fc5] | 341 | (void) found;
|
---|
[63e27ef] | 342 | assert(found);
|
---|
| 343 | assert(PTE_PRESENT(&pte));
|
---|
[32817cc] | 344 |
|
---|
[38dc82d] | 345 | frame = PTE_GET_FRAME(&pte);
|
---|
[1b20da0] | 346 | }
|
---|
[d91488d] | 347 | } else if (elfpage >= start_anon) {
|
---|
[0ee077ee] | 348 | /*
|
---|
| 349 | * This is the uninitialized portion of the segment.
|
---|
| 350 | * It is not physically present in the ELF image.
|
---|
| 351 | * To resolve the situation, a frame must be allocated
|
---|
| 352 | * and cleared.
|
---|
| 353 | */
|
---|
[c7f8fc5] | 354 | kpage = km_temporary_page_get(&frame, FRAME_NO_RESERVE);
|
---|
| 355 | memsetb((void *) kpage, PAGE_SIZE, 0);
|
---|
| 356 | km_temporary_page_put(kpage);
|
---|
[454f1da] | 357 | dirty = true;
|
---|
[0ee077ee] | 358 | } else {
|
---|
[1cc2974] | 359 | size_t pad_lo, pad_hi;
|
---|
[0ee077ee] | 360 | /*
|
---|
| 361 | * The mixed case.
|
---|
[1cc2974] | 362 | *
|
---|
| 363 | * The middle part is backed by the ELF image and
|
---|
| 364 | * the lower and upper parts are anonymous memory.
|
---|
| 365 | * (The segment can be and often is shorter than 1 page).
|
---|
[0ee077ee] | 366 | */
|
---|
[c7f8fc5] | 367 | if (upage < entry->p_vaddr)
|
---|
| 368 | pad_lo = entry->p_vaddr - upage;
|
---|
[1cc2974] | 369 | else
|
---|
| 370 | pad_lo = 0;
|
---|
| 371 |
|
---|
[c7f8fc5] | 372 | if (start_anon < upage + PAGE_SIZE)
|
---|
| 373 | pad_hi = upage + PAGE_SIZE - start_anon;
|
---|
[1cc2974] | 374 | else
|
---|
| 375 | pad_hi = 0;
|
---|
| 376 |
|
---|
[c7f8fc5] | 377 | kpage = km_temporary_page_get(&frame, FRAME_NO_RESERVE);
|
---|
| 378 | memcpy((void *) (kpage + pad_lo),
|
---|
| 379 | (void *) (base + i * PAGE_SIZE + pad_lo),
|
---|
| 380 | PAGE_SIZE - pad_lo - pad_hi);
|
---|
[62cd66f] | 381 | if (entry->p_flags & PF_X) {
|
---|
[0abc2ae] | 382 | smc_coherence((void *) (kpage + pad_lo),
|
---|
[c7f8fc5] | 383 | PAGE_SIZE - pad_lo - pad_hi);
|
---|
[62cd66f] | 384 | }
|
---|
[c7f8fc5] | 385 | memsetb((void *) kpage, pad_lo, 0);
|
---|
| 386 | memsetb((void *) (kpage + PAGE_SIZE - pad_hi), pad_hi, 0);
|
---|
| 387 | km_temporary_page_put(kpage);
|
---|
[454f1da] | 388 | dirty = true;
|
---|
[1cc2974] | 389 | }
|
---|
[00b595b] | 390 |
|
---|
[83b6ba9f] | 391 | if (dirty && area->sh_info->shared) {
|
---|
[1cc2974] | 392 | frame_reference_add(ADDR2PFN(frame));
|
---|
[de0af3a] | 393 | as_pagemap_insert(&area->sh_info->pagemap, upage - area->base,
|
---|
| 394 | frame);
|
---|
[0ee077ee] | 395 | }
|
---|
[1cc2974] | 396 |
|
---|
[83b6ba9f] | 397 | mutex_unlock(&area->sh_info->lock);
|
---|
[1cc2974] | 398 |
|
---|
[c7f8fc5] | 399 | page_mapping_insert(AS, upage, frame, as_area_get_flags(area));
|
---|
[7be8d4d] | 400 | if (!used_space_insert(&area->used_space, upage, 1))
|
---|
[f651e80] | 401 | panic("Cannot insert used space.");
|
---|
[0ee077ee] | 402 |
|
---|
| 403 | return AS_PF_OK;
|
---|
| 404 | }
|
---|
| 405 |
|
---|
| 406 | /** Free a frame that is backed by the ELF backend.
|
---|
| 407 | *
|
---|
| 408 | * The address space area and page tables must be already locked.
|
---|
| 409 | *
|
---|
[36e86862] | 410 | * @param area Pointer to the address space area.
|
---|
| 411 | * @param page Page that is mapped to frame. Must be aligned to
|
---|
| 412 | * PAGE_SIZE.
|
---|
| 413 | * @param frame Frame to be released.
|
---|
[0ee077ee] | 414 | *
|
---|
| 415 | */
|
---|
[7f1c620] | 416 | void elf_frame_free(as_area_t *area, uintptr_t page, uintptr_t frame)
|
---|
[0ee077ee] | 417 | {
|
---|
[127c957b] | 418 | elf_segment_header_t *entry = area->backend_data.segment;
|
---|
[137691a] | 419 | uintptr_t start_anon;
|
---|
[d91488d] | 420 | uintptr_t elfpage;
|
---|
[1cc2974] | 421 |
|
---|
[63e27ef] | 422 | assert(page_table_locked(area->as));
|
---|
| 423 | assert(mutex_locked(&area->lock));
|
---|
[1d432f9] | 424 |
|
---|
[d91488d] | 425 | elfpage = elf_orig_page(area, page);
|
---|
| 426 |
|
---|
| 427 | assert(elfpage >= ALIGN_DOWN(entry->p_vaddr, PAGE_SIZE));
|
---|
| 428 | assert(elfpage < entry->p_vaddr + entry->p_memsz);
|
---|
[1d432f9] | 429 |
|
---|
[1cc2974] | 430 | start_anon = entry->p_vaddr + entry->p_filesz;
|
---|
| 431 |
|
---|
[d91488d] | 432 | if (elfpage >= entry->p_vaddr && elfpage + PAGE_SIZE <= start_anon) {
|
---|
[0ee077ee] | 433 | if (entry->p_flags & PF_W) {
|
---|
| 434 | /*
|
---|
[d5bd8d7] | 435 | * Free the frame with the copy of writable segment
|
---|
| 436 | * data.
|
---|
[0ee077ee] | 437 | */
|
---|
[5df1963] | 438 | frame_free_noreserve(frame, 1);
|
---|
[0ee077ee] | 439 | }
|
---|
| 440 | } else {
|
---|
| 441 | /*
|
---|
[d5bd8d7] | 442 | * The frame is either anonymous memory or the mixed case (i.e.
|
---|
| 443 | * lower part is backed by the ELF image and the upper is
|
---|
| 444 | * anonymous). In any case, a frame needs to be freed.
|
---|
[137691a] | 445 | */
|
---|
[5df1963] | 446 | frame_free_noreserve(frame, 1);
|
---|
[0ee077ee] | 447 | }
|
---|
| 448 | }
|
---|
[00b595b] | 449 |
|
---|
[cc73a8a1] | 450 | /** @}
|
---|
[b45c443] | 451 | */
|
---|