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
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 <mm/km.h>
47#include <genarch/mm/page_pt.h>
48#include <genarch/mm/page_ht.h>
49#include <align.h>
50#include <memstr.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 int elf_page_fault(as_area_t *, uintptr_t, pf_access_t);
61static void elf_frame_free(as_area_t *, uintptr_t, uintptr_t);
62
63mem_backend_t elf_backend = {
64 .create = elf_create,
65 .resize = elf_resize,
66 .share = elf_share,
67 .destroy = elf_destroy,
68
69 .page_fault = elf_page_fault,
70 .frame_free = elf_frame_free,
71};
72
73static size_t elf_nonanon_pages_get(as_area_t *area)
74{
75 elf_segment_header_t *entry = area->backend_data.segment;
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);
79
80 if (entry->p_flags & PF_W)
81 return 0;
82
83 if (last < first)
84 return 0;
85
86 return last - first;
87}
88
89bool elf_create(as_area_t *area)
90{
91 size_t nonanon_pages = elf_nonanon_pages_get(area);
92
93 if (area->pages <= nonanon_pages)
94 return true;
95
96 return reserve_try_alloc(area->pages - nonanon_pages);
97}
98
99bool elf_resize(as_area_t *area, size_t new_pages)
100{
101 size_t nonanon_pages = elf_nonanon_pages_get(area);
102
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 }
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) {
142 node = list_get_instance(list_first(&area->used_space.leaf_list),
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);
156 for (cur = &node->leaf_link; cur != &area->used_space.leaf_list.head;
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 &&
173 base + P2SZ(count) <= start_anon)
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 &&
185 base + P2SZ(j + 1) <= start_anon)
186 continue;
187
188 page_table_lock(area->as, false);
189 pte = page_mapping_find(area->as,
190 base + P2SZ(j), false);
191 ASSERT(pte && PTE_VALID(pte) &&
192 PTE_PRESENT(pte));
193 btree_insert(&area->sh_info->pagemap,
194 (base + P2SZ(j)) - area->base,
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{
209 size_t nonanon_pages = elf_nonanon_pages_get(area);
210
211 if (area->pages > nonanon_pages)
212 reserve_free(area->pages - nonanon_pages);
213}
214
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 *
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).
223 *
224 * @return AS_PF_FAULT on failure (i.e. page fault) or AS_PF_OK
225 * on success (i.e. serviced).
226 */
227int elf_page_fault(as_area_t *area, uintptr_t addr, pf_access_t access)
228{
229 elf_header_t *elf = area->backend_data.elf;
230 elf_segment_header_t *entry = area->backend_data.segment;
231 btree_node_t *leaf;
232 uintptr_t base;
233 uintptr_t frame;
234 uintptr_t kpage;
235 uintptr_t upage;
236 uintptr_t start_anon;
237 size_t i;
238 bool dirty = false;
239
240 ASSERT(page_table_locked(AS));
241 ASSERT(mutex_locked(&area->lock));
242
243 if (!as_area_check_access(area, access))
244 return AS_PF_FAULT;
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
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
256 /* Virtual address of faulting page */
257 upage = ALIGN_DOWN(addr, PAGE_SIZE);
258
259 /* Virtual address of the end of initialized part of segment */
260 start_anon = entry->p_vaddr + entry->p_filesz;
261
262 if (area->sh_info) {
263 bool found = false;
264
265 /*
266 * The address space area is shared.
267 */
268
269 mutex_lock(&area->sh_info->lock);
270 frame = (uintptr_t) btree_search(&area->sh_info->pagemap,
271 upage - area->base, &leaf);
272 if (!frame) {
273 unsigned int i;
274
275 /*
276 * Workaround for valid NULL address.
277 */
278
279 for (i = 0; i < leaf->keys; i++) {
280 if (leaf->key[i] == upage - area->base) {
281 found = true;
282 break;
283 }
284 }
285 }
286 if (frame || found) {
287 frame_reference_add(ADDR2PFN(frame));
288 page_mapping_insert(AS, upage, frame,
289 as_area_get_flags(area));
290 if (!used_space_insert(area, upage, 1))
291 panic("Cannot insert used space.");
292 mutex_unlock(&area->sh_info->lock);
293 return AS_PF_OK;
294 }
295 }
296
297 /*
298 * The area is either not shared or the pagemap does not contain the
299 * mapping.
300 */
301 if (upage >= entry->p_vaddr && upage + PAGE_SIZE <= start_anon) {
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) {
311 kpage = km_temporary_page_get(&frame, FRAME_NO_RESERVE);
312 memcpy((void *) kpage, (void *) (base + i * PAGE_SIZE),
313 PAGE_SIZE);
314 if (entry->p_flags & PF_X) {
315 smc_coherence_block((void *) kpage, PAGE_SIZE);
316 }
317 km_temporary_page_put(kpage);
318 dirty = true;
319 } else {
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);
327 }
328 } else if (upage >= start_anon) {
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 */
335 kpage = km_temporary_page_get(&frame, FRAME_NO_RESERVE);
336 memsetb((void *) kpage, PAGE_SIZE, 0);
337 km_temporary_page_put(kpage);
338 dirty = true;
339 } else {
340 size_t pad_lo, pad_hi;
341 /*
342 * The mixed case.
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).
347 */
348 if (upage < entry->p_vaddr)
349 pad_lo = entry->p_vaddr - upage;
350 else
351 pad_lo = 0;
352
353 if (start_anon < upage + PAGE_SIZE)
354 pad_hi = upage + PAGE_SIZE - start_anon;
355 else
356 pad_hi = 0;
357
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);
362 if (entry->p_flags & PF_X) {
363 smc_coherence_block((void *) (kpage + pad_lo),
364 PAGE_SIZE - pad_lo - pad_hi);
365 }
366 memsetb((void *) kpage, pad_lo, 0);
367 memsetb((void *) (kpage + PAGE_SIZE - pad_hi), pad_hi, 0);
368 km_temporary_page_put(kpage);
369 dirty = true;
370 }
371
372 if (dirty && area->sh_info) {
373 frame_reference_add(ADDR2PFN(frame));
374 btree_insert(&area->sh_info->pagemap, upage - area->base,
375 (void *) frame, leaf);
376 }
377
378 if (area->sh_info)
379 mutex_unlock(&area->sh_info->lock);
380
381 page_mapping_insert(AS, upage, frame, as_area_get_flags(area));
382 if (!used_space_insert(area, upage, 1))
383 panic("Cannot insert used space.");
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 *
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.
396 *
397 */
398void elf_frame_free(as_area_t *area, uintptr_t page, uintptr_t frame)
399{
400 elf_segment_header_t *entry = area->backend_data.segment;
401 uintptr_t start_anon;
402
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
409 start_anon = entry->p_vaddr + entry->p_filesz;
410
411 if (page >= entry->p_vaddr && page + PAGE_SIZE <= start_anon) {
412 if (entry->p_flags & PF_W) {
413 /*
414 * Free the frame with the copy of writable segment
415 * data.
416 */
417 frame_free_noreserve(frame);
418 }
419 } else {
420 /*
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.
424 */
425 frame_free_noreserve(frame);
426 }
427}
428
429/** @}
430 */
Note: See TracBrowser for help on using the repository browser.