source: mainline/kernel/generic/src/mm/backend_elf.c@ 3ca0a2d

Last change on this file since 3ca0a2d was d91488d, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix endless loop in elf_share() walking the wrong B+tree. Fix ELF areas shared to a different address than in the originating task.

  • Property mode set to 100644
File size: 12.7 KB
RevLine 
[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]55static bool elf_create(as_area_t *);
56static bool elf_resize(as_area_t *, size_t);
57static void elf_share(as_area_t *);
58static void elf_destroy(as_area_t *);
59
[01029fc]60static bool elf_is_resizable(as_area_t *);
61static bool elf_is_shareable(as_area_t *);
62
[cda1378]63static int elf_page_fault(as_area_t *, uintptr_t, pf_access_t);
64static void elf_frame_free(as_area_t *, uintptr_t, uintptr_t);
[0ee077ee]65
66mem_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]82static 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 */
108static 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]113bool 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
123bool 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 */
152void elf_share(as_area_t *area)
153{
154 elf_segment_header_t *entry = area->backend_data.segment;
155 link_t *cur;
156 btree_node_t *leaf, *node;
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) {
[55b77d9]166 node = list_get_instance(list_first(&area->used_space.leaf_list),
[03523dc]167 btree_node_t, leaf_link);
168 } else {
[d91488d]169 (void) btree_search(&area->used_space, start_anon, &leaf);
170 node = btree_leaf_node_left_neighbour(&area->used_space, leaf);
[03523dc]171 if (!node)
172 node = leaf;
173 }
174
175 /*
176 * Copy used anonymous portions of the area to sh_info's page map.
177 */
178 mutex_lock(&area->sh_info->lock);
[55b77d9]179 for (cur = &node->leaf_link; cur != &area->used_space.leaf_list.head;
[03523dc]180 cur = cur->next) {
181 unsigned int i;
[a35b458]182
[03523dc]183 node = list_get_instance(cur, btree_node_t, leaf_link);
[a35b458]184
[03523dc]185 for (i = 0; i < node->keys; i++) {
186 uintptr_t base = node->key[i];
187 size_t count = (size_t) node->value[i];
188 unsigned int j;
[a35b458]189
[03523dc]190 /*
191 * Skip read-only areas of used space that are backed
192 * by the ELF image.
193 */
194 if (!(area->flags & AS_AREA_WRITE))
195 if (base >= entry->p_vaddr &&
[b4ffe5bc]196 base + P2SZ(count) <= start_anon)
[03523dc]197 continue;
[a35b458]198
[03523dc]199 for (j = 0; j < count; j++) {
[38dc82d]200 pte_t pte;
201 bool found;
[a35b458]202
[03523dc]203 /*
204 * Skip read-only pages that are backed by the
205 * ELF image.
206 */
207 if (!(area->flags & AS_AREA_WRITE))
208 if (base >= entry->p_vaddr &&
[b4ffe5bc]209 base + P2SZ(j + 1) <= start_anon)
[03523dc]210 continue;
[a35b458]211
[03523dc]212 page_table_lock(area->as, false);
[38dc82d]213 found = page_mapping_find(area->as,
214 base + P2SZ(j), false, &pte);
215
[0705fc5]216 (void) found;
[63e27ef]217 assert(found);
218 assert(PTE_VALID(&pte));
219 assert(PTE_PRESENT(&pte));
[38dc82d]220
[03523dc]221 btree_insert(&area->sh_info->pagemap,
[b4ffe5bc]222 (base + P2SZ(j)) - area->base,
[38dc82d]223 (void *) PTE_GET_FRAME(&pte), NULL);
[03523dc]224 page_table_unlock(area->as, false);
225
[38dc82d]226 pfn_t pfn = ADDR2PFN(PTE_GET_FRAME(&pte));
[03523dc]227 frame_reference_add(pfn);
228 }
[a35b458]229
[03523dc]230 }
231 }
232 mutex_unlock(&area->sh_info->lock);
233}
234
235void elf_destroy(as_area_t *area)
236{
[3ac69647]237 size_t nonanon_pages = elf_nonanon_pages_get(area);
[2c86f81]238
[9dd730d1]239 if (area->pages > nonanon_pages)
240 reserve_free(area->pages - nonanon_pages);
[03523dc]241}
242
[01029fc]243bool elf_is_resizable(as_area_t *area)
244{
245 return true;
246}
247
248bool elf_is_shareable(as_area_t *area)
249{
250 return true;
251}
252
[0ee077ee]253/** Service a page fault in the ELF backend address space area.
254 *
255 * The address space area and page tables must be already locked.
256 *
[36e86862]257 * @param area Pointer to the address space area.
[59fb782]258 * @param upage Faulting virtual page.
[36e86862]259 * @param access Access mode that caused the fault (i.e.
260 * read/write/exec).
[0ee077ee]261 *
[36e86862]262 * @return AS_PF_FAULT on failure (i.e. page fault) or AS_PF_OK
263 * on success (i.e. serviced).
[0ee077ee]264 */
[59fb782]265int elf_page_fault(as_area_t *area, uintptr_t upage, pf_access_t access)
[0ee077ee]266{
[127c957b]267 elf_header_t *elf = area->backend_data.elf;
268 elf_segment_header_t *entry = area->backend_data.segment;
[00b595b]269 btree_node_t *leaf;
[c7f8fc5]270 uintptr_t base;
271 uintptr_t frame;
272 uintptr_t kpage;
273 uintptr_t start_anon;
[d91488d]274 uintptr_t elfpage;
[98000fb]275 size_t i;
[454f1da]276 bool dirty = false;
[0ee077ee]277
[63e27ef]278 assert(page_table_locked(AS));
279 assert(mutex_locked(&area->lock));
280 assert(IS_ALIGNED(upage, PAGE_SIZE));
[1d432f9]281
[d91488d]282 elfpage = elf_orig_page(area, upage);
283
[0ee077ee]284 if (!as_area_check_access(area, access))
285 return AS_PF_FAULT;
[a35b458]286
[d91488d]287 if (elfpage < ALIGN_DOWN(entry->p_vaddr, PAGE_SIZE))
[917a8c8]288 return AS_PF_FAULT;
[a35b458]289
[d91488d]290 if (elfpage >= entry->p_vaddr + entry->p_memsz)
[917a8c8]291 return AS_PF_FAULT;
[a35b458]292
[d91488d]293 i = (elfpage - ALIGN_DOWN(entry->p_vaddr, PAGE_SIZE)) >>
294 PAGE_WIDTH;
[1cc2974]295 base = (uintptr_t)
296 (((void *) elf) + ALIGN_DOWN(entry->p_offset, PAGE_SIZE));
297
298 /* Virtual address of the end of initialized part of segment */
299 start_anon = entry->p_vaddr + entry->p_filesz;
[00b595b]300
[83b6ba9f]301 mutex_lock(&area->sh_info->lock);
302 if (area->sh_info->shared) {
[00b595b]303 bool found = false;
304
305 /*
306 * The address space area is shared.
307 */
[a35b458]308
[7f1c620]309 frame = (uintptr_t) btree_search(&area->sh_info->pagemap,
[c7f8fc5]310 upage - area->base, &leaf);
[00b595b]311 if (!frame) {
[6c441cf8]312 unsigned int i;
[00b595b]313
314 /*
315 * Workaround for valid NULL address.
316 */
317
318 for (i = 0; i < leaf->keys; i++) {
[c7f8fc5]319 if (leaf->key[i] == upage - area->base) {
[00b595b]320 found = true;
321 break;
322 }
323 }
324 }
325 if (frame || found) {
[c9d2235b]326 frame_reference_add(ADDR2PFN(frame));
[c7f8fc5]327 page_mapping_insert(AS, upage, frame,
[d5bd8d7]328 as_area_get_flags(area));
[c7f8fc5]329 if (!used_space_insert(area, upage, 1))
[f651e80]330 panic("Cannot insert used space.");
[00b595b]331 mutex_unlock(&area->sh_info->lock);
332 return AS_PF_OK;
333 }
334 }
[1cc2974]335
[00b595b]336 /*
[d5bd8d7]337 * The area is either not shared or the pagemap does not contain the
338 * mapping.
[00b595b]339 */
[d91488d]340 if (elfpage >= entry->p_vaddr && elfpage + PAGE_SIZE <= start_anon) {
[0ee077ee]341 /*
342 * Initialized portion of the segment. The memory is backed
343 * directly by the content of the ELF image. Pages are
344 * only copied if the segment is writable so that there
[d91488d]345 * can be more instances of the same memory ELF image
[0ee077ee]346 * used at a time. Note that this could be later done
347 * as COW.
348 */
349 if (entry->p_flags & PF_W) {
[c7f8fc5]350 kpage = km_temporary_page_get(&frame, FRAME_NO_RESERVE);
[d56382d]351 memcpy((void *) kpage, (void *) (base + i * PAGE_SIZE),
[c7f8fc5]352 PAGE_SIZE);
[d56382d]353 if (entry->p_flags & PF_X) {
[0abc2ae]354 smc_coherence((void *) kpage, PAGE_SIZE);
[d56382d]355 }
[c7f8fc5]356 km_temporary_page_put(kpage);
[454f1da]357 dirty = true;
[0ee077ee]358 } else {
[38dc82d]359 pte_t pte;
360 bool found;
361
362 found = page_mapping_find(AS_KERNEL,
363 base + i * FRAME_SIZE, true, &pte);
[32817cc]364
[0705fc5]365 (void) found;
[63e27ef]366 assert(found);
367 assert(PTE_PRESENT(&pte));
[32817cc]368
[38dc82d]369 frame = PTE_GET_FRAME(&pte);
[1b20da0]370 }
[d91488d]371 } else if (elfpage >= start_anon) {
[0ee077ee]372 /*
373 * This is the uninitialized portion of the segment.
374 * It is not physically present in the ELF image.
375 * To resolve the situation, a frame must be allocated
376 * and cleared.
377 */
[c7f8fc5]378 kpage = km_temporary_page_get(&frame, FRAME_NO_RESERVE);
379 memsetb((void *) kpage, PAGE_SIZE, 0);
380 km_temporary_page_put(kpage);
[454f1da]381 dirty = true;
[0ee077ee]382 } else {
[1cc2974]383 size_t pad_lo, pad_hi;
[0ee077ee]384 /*
385 * The mixed case.
[1cc2974]386 *
387 * The middle part is backed by the ELF image and
388 * the lower and upper parts are anonymous memory.
389 * (The segment can be and often is shorter than 1 page).
[0ee077ee]390 */
[c7f8fc5]391 if (upage < entry->p_vaddr)
392 pad_lo = entry->p_vaddr - upage;
[1cc2974]393 else
394 pad_lo = 0;
395
[c7f8fc5]396 if (start_anon < upage + PAGE_SIZE)
397 pad_hi = upage + PAGE_SIZE - start_anon;
[1cc2974]398 else
399 pad_hi = 0;
400
[c7f8fc5]401 kpage = km_temporary_page_get(&frame, FRAME_NO_RESERVE);
402 memcpy((void *) (kpage + pad_lo),
403 (void *) (base + i * PAGE_SIZE + pad_lo),
404 PAGE_SIZE - pad_lo - pad_hi);
[62cd66f]405 if (entry->p_flags & PF_X) {
[0abc2ae]406 smc_coherence((void *) (kpage + pad_lo),
[c7f8fc5]407 PAGE_SIZE - pad_lo - pad_hi);
[62cd66f]408 }
[c7f8fc5]409 memsetb((void *) kpage, pad_lo, 0);
410 memsetb((void *) (kpage + PAGE_SIZE - pad_hi), pad_hi, 0);
411 km_temporary_page_put(kpage);
[454f1da]412 dirty = true;
[1cc2974]413 }
[00b595b]414
[83b6ba9f]415 if (dirty && area->sh_info->shared) {
[1cc2974]416 frame_reference_add(ADDR2PFN(frame));
[c7f8fc5]417 btree_insert(&area->sh_info->pagemap, upage - area->base,
[1cc2974]418 (void *) frame, leaf);
[0ee077ee]419 }
[1cc2974]420
[83b6ba9f]421 mutex_unlock(&area->sh_info->lock);
[1cc2974]422
[c7f8fc5]423 page_mapping_insert(AS, upage, frame, as_area_get_flags(area));
424 if (!used_space_insert(area, upage, 1))
[f651e80]425 panic("Cannot insert used space.");
[0ee077ee]426
427 return AS_PF_OK;
428}
429
430/** Free a frame that is backed by the ELF backend.
431 *
432 * The address space area and page tables must be already locked.
433 *
[36e86862]434 * @param area Pointer to the address space area.
435 * @param page Page that is mapped to frame. Must be aligned to
436 * PAGE_SIZE.
437 * @param frame Frame to be released.
[0ee077ee]438 *
439 */
[7f1c620]440void elf_frame_free(as_area_t *area, uintptr_t page, uintptr_t frame)
[0ee077ee]441{
[127c957b]442 elf_segment_header_t *entry = area->backend_data.segment;
[137691a]443 uintptr_t start_anon;
[d91488d]444 uintptr_t elfpage;
[1cc2974]445
[63e27ef]446 assert(page_table_locked(area->as));
447 assert(mutex_locked(&area->lock));
[1d432f9]448
[d91488d]449 elfpage = elf_orig_page(area, page);
450
451 assert(elfpage >= ALIGN_DOWN(entry->p_vaddr, PAGE_SIZE));
452 assert(elfpage < entry->p_vaddr + entry->p_memsz);
[1d432f9]453
[1cc2974]454 start_anon = entry->p_vaddr + entry->p_filesz;
455
[d91488d]456 if (elfpage >= entry->p_vaddr && elfpage + PAGE_SIZE <= start_anon) {
[0ee077ee]457 if (entry->p_flags & PF_W) {
458 /*
[d5bd8d7]459 * Free the frame with the copy of writable segment
460 * data.
[0ee077ee]461 */
[5df1963]462 frame_free_noreserve(frame, 1);
[0ee077ee]463 }
464 } else {
465 /*
[d5bd8d7]466 * The frame is either anonymous memory or the mixed case (i.e.
467 * lower part is backed by the ELF image and the upper is
468 * anonymous). In any case, a frame needs to be freed.
[137691a]469 */
[5df1963]470 frame_free_noreserve(frame, 1);
[0ee077ee]471 }
472}
[00b595b]473
[cc73a8a1]474/** @}
[b45c443]475 */
Note: See TracBrowser for help on using the repository browser.