source: mainline/kernel/generic/src/mm/backend_elf.c@ 03523dc

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 03523dc was 03523dc, checked in by Jakub Jermar <jakub@…>, 14 years ago

Add more hooks to address space area backends so that each backend can
take action also on:

  • as_area_create()
  • as_area_resize()
  • as_area_destroy()

Add basic memory reservation code to anonymous and elf address space
area backends.

  • Property mode set to 100644
File size: 10.8 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
[cc73a8a1]29/** @addtogroup genericmm
[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>
[0ee077ee]39#include <debug.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>
[00b595b]46#include <genarch/mm/page_pt.h>
47#include <genarch/mm/page_ht.h>
[0ee077ee]48#include <align.h>
49#include <memstr.h>
50#include <macros.h>
51#include <arch.h>
[36e86862]52#include <arch/barrier.h>
[0ee077ee]53
[9f63a83]54#ifdef CONFIG_VIRT_IDX_DCACHE
55#include <arch/mm/cache.h>
56#endif
57
[03523dc]58static bool elf_create(as_area_t *);
59static bool elf_resize(as_area_t *, size_t);
60static void elf_share(as_area_t *);
61static void elf_destroy(as_area_t *);
62
[7f1c620]63static int elf_page_fault(as_area_t *area, uintptr_t addr, pf_access_t access);
64static void elf_frame_free(as_area_t *area, uintptr_t page, uintptr_t frame);
[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
[0ee077ee]72 .page_fault = elf_page_fault,
73 .frame_free = elf_frame_free,
74};
75
[03523dc]76bool elf_create(as_area_t *area)
77{
78 /**
79 * @todo:
80 * Reserve only how much is necessary for anonymous pages plus the
81 * supporting structures allocated during the page fault.
82 */
83 return reserve_try_alloc(area->pages);
84}
85
86bool elf_resize(as_area_t *area, size_t new_pages)
87{
88 if (new_pages > area->pages)
89 return reserve_try_alloc(new_pages - area->pages);
90 else if (new_pages < area->pages)
91 reserve_free(area->pages - new_pages);
92
93 return true;
94}
95
96/** Share ELF image backed address space area.
97 *
98 * If the area is writable, then all mapped pages are duplicated in the pagemap.
99 * Otherwise only portions of the area that are not backed by the ELF image
100 * are put into the pagemap.
101 *
102 * @param area Address space area.
103 */
104void elf_share(as_area_t *area)
105{
106 elf_segment_header_t *entry = area->backend_data.segment;
107 link_t *cur;
108 btree_node_t *leaf, *node;
109 uintptr_t start_anon = entry->p_vaddr + entry->p_filesz;
110
111 ASSERT(mutex_locked(&area->as->lock));
112 ASSERT(mutex_locked(&area->lock));
113
114 /*
115 * Find the node in which to start linear search.
116 */
117 if (area->flags & AS_AREA_WRITE) {
118 node = list_get_instance(area->used_space.leaf_head.next,
119 btree_node_t, leaf_link);
120 } else {
121 (void) btree_search(&area->sh_info->pagemap, start_anon, &leaf);
122 node = btree_leaf_node_left_neighbour(&area->sh_info->pagemap,
123 leaf);
124 if (!node)
125 node = leaf;
126 }
127
128 /*
129 * Copy used anonymous portions of the area to sh_info's page map.
130 */
131 mutex_lock(&area->sh_info->lock);
132 for (cur = &node->leaf_link; cur != &area->used_space.leaf_head;
133 cur = cur->next) {
134 unsigned int i;
135
136 node = list_get_instance(cur, btree_node_t, leaf_link);
137
138 for (i = 0; i < node->keys; i++) {
139 uintptr_t base = node->key[i];
140 size_t count = (size_t) node->value[i];
141 unsigned int j;
142
143 /*
144 * Skip read-only areas of used space that are backed
145 * by the ELF image.
146 */
147 if (!(area->flags & AS_AREA_WRITE))
148 if (base >= entry->p_vaddr &&
149 base + count * PAGE_SIZE <= start_anon)
150 continue;
151
152 for (j = 0; j < count; j++) {
153 pte_t *pte;
154
155 /*
156 * Skip read-only pages that are backed by the
157 * ELF image.
158 */
159 if (!(area->flags & AS_AREA_WRITE))
160 if (base >= entry->p_vaddr &&
161 base + (j + 1) * PAGE_SIZE <=
162 start_anon)
163 continue;
164
165 page_table_lock(area->as, false);
166 pte = page_mapping_find(area->as,
167 base + j * PAGE_SIZE);
168 ASSERT(pte && PTE_VALID(pte) &&
169 PTE_PRESENT(pte));
170 btree_insert(&area->sh_info->pagemap,
171 (base + j * PAGE_SIZE) - area->base,
172 (void *) PTE_GET_FRAME(pte), NULL);
173 page_table_unlock(area->as, false);
174
175 pfn_t pfn = ADDR2PFN(PTE_GET_FRAME(pte));
176 frame_reference_add(pfn);
177 }
178
179 }
180 }
181 mutex_unlock(&area->sh_info->lock);
182}
183
184void elf_destroy(as_area_t *area)
185{
186 /**
187 * @todo:
188 * Unreserve only how much was really reserved.
189 */
190 reserve_free(area->pages);
191}
192
[0ee077ee]193/** Service a page fault in the ELF backend address space area.
194 *
195 * The address space area and page tables must be already locked.
196 *
[36e86862]197 * @param area Pointer to the address space area.
198 * @param addr Faulting virtual address.
199 * @param access Access mode that caused the fault (i.e.
200 * read/write/exec).
[0ee077ee]201 *
[36e86862]202 * @return AS_PF_FAULT on failure (i.e. page fault) or AS_PF_OK
203 * on success (i.e. serviced).
[0ee077ee]204 */
[7f1c620]205int elf_page_fault(as_area_t *area, uintptr_t addr, pf_access_t access)
[0ee077ee]206{
[127c957b]207 elf_header_t *elf = area->backend_data.elf;
208 elf_segment_header_t *entry = area->backend_data.segment;
[00b595b]209 btree_node_t *leaf;
[1cc2974]210 uintptr_t base, frame, page, start_anon;
[98000fb]211 size_t i;
[454f1da]212 bool dirty = false;
[0ee077ee]213
[1d432f9]214 ASSERT(page_table_locked(AS));
215 ASSERT(mutex_locked(&area->lock));
216
[0ee077ee]217 if (!as_area_check_access(area, access))
218 return AS_PF_FAULT;
219
[1cc2974]220 ASSERT((addr >= ALIGN_DOWN(entry->p_vaddr, PAGE_SIZE)) &&
[d5bd8d7]221 (addr < entry->p_vaddr + entry->p_memsz));
[1cc2974]222 i = (addr - ALIGN_DOWN(entry->p_vaddr, PAGE_SIZE)) >> PAGE_WIDTH;
223 base = (uintptr_t)
224 (((void *) elf) + ALIGN_DOWN(entry->p_offset, PAGE_SIZE));
225
226 /* Virtual address of faulting page*/
227 page = ALIGN_DOWN(addr, PAGE_SIZE);
228
229 /* Virtual address of the end of initialized part of segment */
230 start_anon = entry->p_vaddr + entry->p_filesz;
[00b595b]231
232 if (area->sh_info) {
233 bool found = false;
234
235 /*
236 * The address space area is shared.
237 */
[1cc2974]238
[00b595b]239 mutex_lock(&area->sh_info->lock);
[7f1c620]240 frame = (uintptr_t) btree_search(&area->sh_info->pagemap,
[1cc2974]241 page - area->base, &leaf);
[00b595b]242 if (!frame) {
[6c441cf8]243 unsigned int i;
[00b595b]244
245 /*
246 * Workaround for valid NULL address.
247 */
248
249 for (i = 0; i < leaf->keys; i++) {
[6461d286]250 if (leaf->key[i] == page - area->base) {
[00b595b]251 found = true;
252 break;
253 }
254 }
255 }
256 if (frame || found) {
[c9d2235b]257 frame_reference_add(ADDR2PFN(frame));
[d5bd8d7]258 page_mapping_insert(AS, addr, frame,
259 as_area_get_flags(area));
[1cc2974]260 if (!used_space_insert(area, page, 1))
[f651e80]261 panic("Cannot insert used space.");
[00b595b]262 mutex_unlock(&area->sh_info->lock);
263 return AS_PF_OK;
264 }
265 }
[1cc2974]266
[00b595b]267 /*
[d5bd8d7]268 * The area is either not shared or the pagemap does not contain the
269 * mapping.
[00b595b]270 */
[1cc2974]271 if (page >= entry->p_vaddr && page + PAGE_SIZE <= start_anon) {
[0ee077ee]272 /*
273 * Initialized portion of the segment. The memory is backed
274 * directly by the content of the ELF image. Pages are
275 * only copied if the segment is writable so that there
276 * can be more instantions of the same memory ELF image
277 * used at a time. Note that this could be later done
278 * as COW.
279 */
280 if (entry->p_flags & PF_W) {
[b838fdf]281 frame = (uintptr_t)frame_alloc_noreserve(ONE_FRAME, 0);
[d5bd8d7]282 memcpy((void *) PA2KA(frame),
283 (void *) (base + i * FRAME_SIZE), FRAME_SIZE);
[62cd66f]284 if (entry->p_flags & PF_X) {
[3759681]285 smc_coherence_block((void *) PA2KA(frame),
286 FRAME_SIZE);
[62cd66f]287 }
[454f1da]288 dirty = true;
[0ee077ee]289 } else {
[1cc2974]290 frame = KA2PA(base + i * FRAME_SIZE);
[0ee077ee]291 }
[1cc2974]292 } else if (page >= start_anon) {
[0ee077ee]293 /*
294 * This is the uninitialized portion of the segment.
295 * It is not physically present in the ELF image.
296 * To resolve the situation, a frame must be allocated
297 * and cleared.
298 */
[b838fdf]299 frame = (uintptr_t) frame_alloc_noreserve(ONE_FRAME, 0);
[e32e092]300 memsetb((void *) PA2KA(frame), FRAME_SIZE, 0);
[454f1da]301 dirty = true;
[0ee077ee]302 } else {
[1cc2974]303 size_t pad_lo, pad_hi;
[0ee077ee]304 /*
305 * The mixed case.
[1cc2974]306 *
307 * The middle part is backed by the ELF image and
308 * the lower and upper parts are anonymous memory.
309 * (The segment can be and often is shorter than 1 page).
[0ee077ee]310 */
[1cc2974]311 if (page < entry->p_vaddr)
312 pad_lo = entry->p_vaddr - page;
313 else
314 pad_lo = 0;
315
316 if (start_anon < page + PAGE_SIZE)
317 pad_hi = page + PAGE_SIZE - start_anon;
318 else
319 pad_hi = 0;
320
[b838fdf]321 frame = (uintptr_t) frame_alloc_noreserve(ONE_FRAME, 0);
[1cc2974]322 memcpy((void *) (PA2KA(frame) + pad_lo),
323 (void *) (base + i * FRAME_SIZE + pad_lo),
324 FRAME_SIZE - pad_lo - pad_hi);
[62cd66f]325 if (entry->p_flags & PF_X) {
[3759681]326 smc_coherence_block((void *) (PA2KA(frame) + pad_lo),
327 FRAME_SIZE - pad_lo - pad_hi);
[62cd66f]328 }
[e32e092]329 memsetb((void *) PA2KA(frame), pad_lo, 0);
[36e86862]330 memsetb((void *) (PA2KA(frame) + FRAME_SIZE - pad_hi), pad_hi,
331 0);
[454f1da]332 dirty = true;
[1cc2974]333 }
[00b595b]334
[1cc2974]335 if (dirty && area->sh_info) {
336 frame_reference_add(ADDR2PFN(frame));
337 btree_insert(&area->sh_info->pagemap, page - area->base,
338 (void *) frame, leaf);
[0ee077ee]339 }
[1cc2974]340
[00b595b]341 if (area->sh_info)
342 mutex_unlock(&area->sh_info->lock);
[1cc2974]343
[0ee077ee]344 page_mapping_insert(AS, addr, frame, as_area_get_flags(area));
[1cc2974]345 if (!used_space_insert(area, page, 1))
[f651e80]346 panic("Cannot insert used space.");
[0ee077ee]347
348 return AS_PF_OK;
349}
350
351/** Free a frame that is backed by the ELF backend.
352 *
353 * The address space area and page tables must be already locked.
354 *
[36e86862]355 * @param area Pointer to the address space area.
356 * @param page Page that is mapped to frame. Must be aligned to
357 * PAGE_SIZE.
358 * @param frame Frame to be released.
[0ee077ee]359 *
360 */
[7f1c620]361void elf_frame_free(as_area_t *area, uintptr_t page, uintptr_t frame)
[0ee077ee]362{
[127c957b]363 elf_segment_header_t *entry = area->backend_data.segment;
[137691a]364 uintptr_t start_anon;
[1cc2974]365
[1d432f9]366 ASSERT(page_table_locked(area->as));
367 ASSERT(mutex_locked(&area->lock));
368
369 ASSERT(page >= ALIGN_DOWN(entry->p_vaddr, PAGE_SIZE));
370 ASSERT(page < entry->p_vaddr + entry->p_memsz);
371
[1cc2974]372 start_anon = entry->p_vaddr + entry->p_filesz;
373
374 if (page >= entry->p_vaddr && page + PAGE_SIZE <= start_anon) {
[0ee077ee]375 if (entry->p_flags & PF_W) {
376 /*
[d5bd8d7]377 * Free the frame with the copy of writable segment
378 * data.
[0ee077ee]379 */
[b838fdf]380 frame_free_noreserve(frame);
[0ee077ee]381 }
382 } else {
383 /*
[d5bd8d7]384 * The frame is either anonymous memory or the mixed case (i.e.
385 * lower part is backed by the ELF image and the upper is
386 * anonymous). In any case, a frame needs to be freed.
[137691a]387 */
[b838fdf]388 frame_free_noreserve(frame);
[0ee077ee]389 }
390}
[00b595b]391
[cc73a8a1]392/** @}
[b45c443]393 */
Note: See TracBrowser for help on using the repository browser.