source: mainline/kernel/generic/src/mm/backend_elf.c@ 05913fe7

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 05913fe7 was 63e27ef, checked in by Jiri Svoboda <jiri@…>, 8 years ago

ASSERT → assert

  • Property mode set to 100644
File size: 12.0 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 <assert.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 <mm/km.h>
47#include <genarch/mm/page_pt.h>
48#include <genarch/mm/page_ht.h>
49#include <align.h>
50#include <mem.h>
51#include <macros.h>
52#include <arch.h>
53#include <arch/barrier.h>
54
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
60static bool elf_is_resizable(as_area_t *);
61static bool elf_is_shareable(as_area_t *);
62
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);
65
66mem_backend_t elf_backend = {
67 .create = elf_create,
68 .resize = elf_resize,
69 .share = elf_share,
70 .destroy = elf_destroy,
71
72 .is_resizable = elf_is_resizable,
73 .is_shareable = elf_is_shareable,
74
75 .page_fault = elf_page_fault,
76 .frame_free = elf_frame_free,
77
78 .create_shared_data = NULL,
79 .destroy_shared_data = NULL
80};
81
82static size_t elf_nonanon_pages_get(as_area_t *area)
83{
84 elf_segment_header_t *entry = area->backend_data.segment;
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);
88
89 if (entry->p_flags & PF_W)
90 return 0;
91
92 if (last < first)
93 return 0;
94
95 return last - first;
96}
97
98bool elf_create(as_area_t *area)
99{
100 size_t nonanon_pages = elf_nonanon_pages_get(area);
101
102 if (area->pages <= nonanon_pages)
103 return true;
104
105 return reserve_try_alloc(area->pages - nonanon_pages);
106}
107
108bool elf_resize(as_area_t *area, size_t new_pages)
109{
110 size_t nonanon_pages = elf_nonanon_pages_get(area);
111
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 }
125
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 */
137void 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
144 assert(mutex_locked(&area->as->lock));
145 assert(mutex_locked(&area->lock));
146
147 /*
148 * Find the node in which to start linear search.
149 */
150 if (area->flags & AS_AREA_WRITE) {
151 node = list_get_instance(list_first(&area->used_space.leaf_list),
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);
165 for (cur = &node->leaf_link; cur != &area->used_space.leaf_list.head;
166 cur = cur->next) {
167 unsigned int i;
168
169 node = list_get_instance(cur, btree_node_t, leaf_link);
170
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;
175
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 &&
182 base + P2SZ(count) <= start_anon)
183 continue;
184
185 for (j = 0; j < count; j++) {
186 pte_t pte;
187 bool found;
188
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 &&
195 base + P2SZ(j + 1) <= start_anon)
196 continue;
197
198 page_table_lock(area->as, false);
199 found = page_mapping_find(area->as,
200 base + P2SZ(j), false, &pte);
201
202 assert(found);
203 assert(PTE_VALID(&pte));
204 assert(PTE_PRESENT(&pte));
205
206 btree_insert(&area->sh_info->pagemap,
207 (base + P2SZ(j)) - area->base,
208 (void *) PTE_GET_FRAME(&pte), NULL);
209 page_table_unlock(area->as, false);
210
211 pfn_t pfn = ADDR2PFN(PTE_GET_FRAME(&pte));
212 frame_reference_add(pfn);
213 }
214
215 }
216 }
217 mutex_unlock(&area->sh_info->lock);
218}
219
220void elf_destroy(as_area_t *area)
221{
222 size_t nonanon_pages = elf_nonanon_pages_get(area);
223
224 if (area->pages > nonanon_pages)
225 reserve_free(area->pages - nonanon_pages);
226}
227
228bool elf_is_resizable(as_area_t *area)
229{
230 return true;
231}
232
233bool elf_is_shareable(as_area_t *area)
234{
235 return true;
236}
237
238
239/** Service a page fault in the ELF backend address space area.
240 *
241 * The address space area and page tables must be already locked.
242 *
243 * @param area Pointer to the address space area.
244 * @param upage Faulting virtual page.
245 * @param access Access mode that caused the fault (i.e.
246 * read/write/exec).
247 *
248 * @return AS_PF_FAULT on failure (i.e. page fault) or AS_PF_OK
249 * on success (i.e. serviced).
250 */
251int elf_page_fault(as_area_t *area, uintptr_t upage, pf_access_t access)
252{
253 elf_header_t *elf = area->backend_data.elf;
254 elf_segment_header_t *entry = area->backend_data.segment;
255 btree_node_t *leaf;
256 uintptr_t base;
257 uintptr_t frame;
258 uintptr_t kpage;
259 uintptr_t start_anon;
260 size_t i;
261 bool dirty = false;
262
263 assert(page_table_locked(AS));
264 assert(mutex_locked(&area->lock));
265 assert(IS_ALIGNED(upage, PAGE_SIZE));
266
267 if (!as_area_check_access(area, access))
268 return AS_PF_FAULT;
269
270 if (upage < ALIGN_DOWN(entry->p_vaddr, PAGE_SIZE))
271 return AS_PF_FAULT;
272
273 if (upage >= entry->p_vaddr + entry->p_memsz)
274 return AS_PF_FAULT;
275
276 i = (upage - ALIGN_DOWN(entry->p_vaddr, PAGE_SIZE)) >> PAGE_WIDTH;
277 base = (uintptr_t)
278 (((void *) elf) + ALIGN_DOWN(entry->p_offset, PAGE_SIZE));
279
280 /* Virtual address of the end of initialized part of segment */
281 start_anon = entry->p_vaddr + entry->p_filesz;
282
283 mutex_lock(&area->sh_info->lock);
284 if (area->sh_info->shared) {
285 bool found = false;
286
287 /*
288 * The address space area is shared.
289 */
290
291 frame = (uintptr_t) btree_search(&area->sh_info->pagemap,
292 upage - area->base, &leaf);
293 if (!frame) {
294 unsigned int i;
295
296 /*
297 * Workaround for valid NULL address.
298 */
299
300 for (i = 0; i < leaf->keys; i++) {
301 if (leaf->key[i] == upage - area->base) {
302 found = true;
303 break;
304 }
305 }
306 }
307 if (frame || found) {
308 frame_reference_add(ADDR2PFN(frame));
309 page_mapping_insert(AS, upage, frame,
310 as_area_get_flags(area));
311 if (!used_space_insert(area, upage, 1))
312 panic("Cannot insert used space.");
313 mutex_unlock(&area->sh_info->lock);
314 return AS_PF_OK;
315 }
316 }
317
318 /*
319 * The area is either not shared or the pagemap does not contain the
320 * mapping.
321 */
322 if (upage >= entry->p_vaddr && upage + PAGE_SIZE <= start_anon) {
323 /*
324 * Initialized portion of the segment. The memory is backed
325 * directly by the content of the ELF image. Pages are
326 * only copied if the segment is writable so that there
327 * can be more instantions of the same memory ELF image
328 * used at a time. Note that this could be later done
329 * as COW.
330 */
331 if (entry->p_flags & PF_W) {
332 kpage = km_temporary_page_get(&frame, FRAME_NO_RESERVE);
333 memcpy((void *) kpage, (void *) (base + i * PAGE_SIZE),
334 PAGE_SIZE);
335 if (entry->p_flags & PF_X) {
336 smc_coherence_block((void *) kpage, PAGE_SIZE);
337 }
338 km_temporary_page_put(kpage);
339 dirty = true;
340 } else {
341 pte_t pte;
342 bool found;
343
344 found = page_mapping_find(AS_KERNEL,
345 base + i * FRAME_SIZE, true, &pte);
346
347 assert(found);
348 assert(PTE_PRESENT(&pte));
349
350 frame = PTE_GET_FRAME(&pte);
351 }
352 } else if (upage >= start_anon) {
353 /*
354 * This is the uninitialized portion of the segment.
355 * It is not physically present in the ELF image.
356 * To resolve the situation, a frame must be allocated
357 * and cleared.
358 */
359 kpage = km_temporary_page_get(&frame, FRAME_NO_RESERVE);
360 memsetb((void *) kpage, PAGE_SIZE, 0);
361 km_temporary_page_put(kpage);
362 dirty = true;
363 } else {
364 size_t pad_lo, pad_hi;
365 /*
366 * The mixed case.
367 *
368 * The middle part is backed by the ELF image and
369 * the lower and upper parts are anonymous memory.
370 * (The segment can be and often is shorter than 1 page).
371 */
372 if (upage < entry->p_vaddr)
373 pad_lo = entry->p_vaddr - upage;
374 else
375 pad_lo = 0;
376
377 if (start_anon < upage + PAGE_SIZE)
378 pad_hi = upage + PAGE_SIZE - start_anon;
379 else
380 pad_hi = 0;
381
382 kpage = km_temporary_page_get(&frame, FRAME_NO_RESERVE);
383 memcpy((void *) (kpage + pad_lo),
384 (void *) (base + i * PAGE_SIZE + pad_lo),
385 PAGE_SIZE - pad_lo - pad_hi);
386 if (entry->p_flags & PF_X) {
387 smc_coherence_block((void *) (kpage + pad_lo),
388 PAGE_SIZE - pad_lo - pad_hi);
389 }
390 memsetb((void *) kpage, pad_lo, 0);
391 memsetb((void *) (kpage + PAGE_SIZE - pad_hi), pad_hi, 0);
392 km_temporary_page_put(kpage);
393 dirty = true;
394 }
395
396 if (dirty && area->sh_info->shared) {
397 frame_reference_add(ADDR2PFN(frame));
398 btree_insert(&area->sh_info->pagemap, upage - area->base,
399 (void *) frame, leaf);
400 }
401
402 mutex_unlock(&area->sh_info->lock);
403
404 page_mapping_insert(AS, upage, frame, as_area_get_flags(area));
405 if (!used_space_insert(area, upage, 1))
406 panic("Cannot insert used space.");
407
408 return AS_PF_OK;
409}
410
411/** Free a frame that is backed by the ELF backend.
412 *
413 * The address space area and page tables must be already locked.
414 *
415 * @param area Pointer to the address space area.
416 * @param page Page that is mapped to frame. Must be aligned to
417 * PAGE_SIZE.
418 * @param frame Frame to be released.
419 *
420 */
421void elf_frame_free(as_area_t *area, uintptr_t page, uintptr_t frame)
422{
423 elf_segment_header_t *entry = area->backend_data.segment;
424 uintptr_t start_anon;
425
426 assert(page_table_locked(area->as));
427 assert(mutex_locked(&area->lock));
428
429 assert(page >= ALIGN_DOWN(entry->p_vaddr, PAGE_SIZE));
430 assert(page < entry->p_vaddr + entry->p_memsz);
431
432 start_anon = entry->p_vaddr + entry->p_filesz;
433
434 if (page >= entry->p_vaddr && page + PAGE_SIZE <= start_anon) {
435 if (entry->p_flags & PF_W) {
436 /*
437 * Free the frame with the copy of writable segment
438 * data.
439 */
440 frame_free_noreserve(frame, 1);
441 }
442 } else {
443 /*
444 * The frame is either anonymous memory or the mixed case (i.e.
445 * lower part is backed by the ELF image and the upper is
446 * anonymous). In any case, a frame needs to be freed.
447 */
448 frame_free_noreserve(frame, 1);
449 }
450}
451
452/** @}
453 */
Note: See TracBrowser for help on using the repository browser.