source: mainline/kernel/generic/src/mm/backend_elf.c@ 9970a5a

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

Make the kernel ready for init tasks loaded to high memory.

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