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
Line 
1/*
2 * Copyright (c) 2006 Jakub Jermar
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
29/** @addtogroup genericmm
30 * @{
31 */
32
33/**
34 * @file
35 * @brief Backend for address space areas backed by an ELF image.
36 */
37
38#include <lib/elf.h>
39#include <debug.h>
40#include <typedefs.h>
41#include <mm/as.h>
42#include <mm/frame.h>
43#include <mm/slab.h>
44#include <mm/page.h>
45#include <mm/reserve.h>
46#include <genarch/mm/page_pt.h>
47#include <genarch/mm/page_ht.h>
48#include <align.h>
49#include <memstr.h>
50#include <macros.h>
51#include <arch.h>
52#include <arch/barrier.h>
53
54#ifdef CONFIG_VIRT_IDX_DCACHE
55#include <arch/mm/cache.h>
56#endif
57
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
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);
65
66mem_backend_t elf_backend = {
67 .create = elf_create,
68 .resize = elf_resize,
69 .share = elf_share,
70 .destroy = elf_destroy,
71
72 .page_fault = elf_page_fault,
73 .frame_free = elf_frame_free,
74};
75
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
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 *
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).
201 *
202 * @return AS_PF_FAULT on failure (i.e. page fault) or AS_PF_OK
203 * on success (i.e. serviced).
204 */
205int elf_page_fault(as_area_t *area, uintptr_t addr, pf_access_t access)
206{
207 elf_header_t *elf = area->backend_data.elf;
208 elf_segment_header_t *entry = area->backend_data.segment;
209 btree_node_t *leaf;
210 uintptr_t base, frame, page, start_anon;
211 size_t i;
212 bool dirty = false;
213
214 ASSERT(page_table_locked(AS));
215 ASSERT(mutex_locked(&area->lock));
216
217 if (!as_area_check_access(area, access))
218 return AS_PF_FAULT;
219
220 ASSERT((addr >= ALIGN_DOWN(entry->p_vaddr, PAGE_SIZE)) &&
221 (addr < entry->p_vaddr + entry->p_memsz));
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;
231
232 if (area->sh_info) {
233 bool found = false;
234
235 /*
236 * The address space area is shared.
237 */
238
239 mutex_lock(&area->sh_info->lock);
240 frame = (uintptr_t) btree_search(&area->sh_info->pagemap,
241 page - area->base, &leaf);
242 if (!frame) {
243 unsigned int i;
244
245 /*
246 * Workaround for valid NULL address.
247 */
248
249 for (i = 0; i < leaf->keys; i++) {
250 if (leaf->key[i] == page - area->base) {
251 found = true;
252 break;
253 }
254 }
255 }
256 if (frame || found) {
257 frame_reference_add(ADDR2PFN(frame));
258 page_mapping_insert(AS, addr, frame,
259 as_area_get_flags(area));
260 if (!used_space_insert(area, page, 1))
261 panic("Cannot insert used space.");
262 mutex_unlock(&area->sh_info->lock);
263 return AS_PF_OK;
264 }
265 }
266
267 /*
268 * The area is either not shared or the pagemap does not contain the
269 * mapping.
270 */
271 if (page >= entry->p_vaddr && page + PAGE_SIZE <= start_anon) {
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) {
281 frame = (uintptr_t)frame_alloc_noreserve(ONE_FRAME, 0);
282 memcpy((void *) PA2KA(frame),
283 (void *) (base + i * FRAME_SIZE), FRAME_SIZE);
284 if (entry->p_flags & PF_X) {
285 smc_coherence_block((void *) PA2KA(frame),
286 FRAME_SIZE);
287 }
288 dirty = true;
289 } else {
290 frame = KA2PA(base + i * FRAME_SIZE);
291 }
292 } else if (page >= start_anon) {
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 */
299 frame = (uintptr_t) frame_alloc_noreserve(ONE_FRAME, 0);
300 memsetb((void *) PA2KA(frame), FRAME_SIZE, 0);
301 dirty = true;
302 } else {
303 size_t pad_lo, pad_hi;
304 /*
305 * The mixed case.
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).
310 */
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
321 frame = (uintptr_t) frame_alloc_noreserve(ONE_FRAME, 0);
322 memcpy((void *) (PA2KA(frame) + pad_lo),
323 (void *) (base + i * FRAME_SIZE + pad_lo),
324 FRAME_SIZE - pad_lo - pad_hi);
325 if (entry->p_flags & PF_X) {
326 smc_coherence_block((void *) (PA2KA(frame) + pad_lo),
327 FRAME_SIZE - pad_lo - pad_hi);
328 }
329 memsetb((void *) PA2KA(frame), pad_lo, 0);
330 memsetb((void *) (PA2KA(frame) + FRAME_SIZE - pad_hi), pad_hi,
331 0);
332 dirty = true;
333 }
334
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);
339 }
340
341 if (area->sh_info)
342 mutex_unlock(&area->sh_info->lock);
343
344 page_mapping_insert(AS, addr, frame, as_area_get_flags(area));
345 if (!used_space_insert(area, page, 1))
346 panic("Cannot insert used space.");
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 *
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.
359 *
360 */
361void elf_frame_free(as_area_t *area, uintptr_t page, uintptr_t frame)
362{
363 elf_segment_header_t *entry = area->backend_data.segment;
364 uintptr_t start_anon;
365
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
372 start_anon = entry->p_vaddr + entry->p_filesz;
373
374 if (page >= entry->p_vaddr && page + PAGE_SIZE <= start_anon) {
375 if (entry->p_flags & PF_W) {
376 /*
377 * Free the frame with the copy of writable segment
378 * data.
379 */
380 frame_free_noreserve(frame);
381 }
382 } else {
383 /*
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.
387 */
388 frame_free_noreserve(frame);
389 }
390}
391
392/** @}
393 */
Note: See TracBrowser for help on using the repository browser.