Rev 2076 | Rev 2134 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2076 | Rev 2089 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2006 Jakub Jermar |
2 | * Copyright (c) 2006 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup genericmm |
29 | /** @addtogroup genericmm |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** |
33 | /** |
34 | * @file |
34 | * @file |
35 | * @brief Backend for address space areas backed by an ELF image. |
35 | * @brief Backend for address space areas backed by an ELF image. |
36 | */ |
36 | */ |
37 | 37 | ||
38 | #include <lib/elf.h> |
38 | #include <lib/elf.h> |
39 | #include <debug.h> |
39 | #include <debug.h> |
40 | #include <arch/types.h> |
40 | #include <arch/types.h> |
41 | #include <typedefs.h> |
- | |
42 | #include <mm/as.h> |
41 | #include <mm/as.h> |
43 | #include <mm/frame.h> |
42 | #include <mm/frame.h> |
44 | #include <mm/slab.h> |
43 | #include <mm/slab.h> |
45 | #include <mm/page.h> |
44 | #include <mm/page.h> |
46 | #include <genarch/mm/page_pt.h> |
45 | #include <genarch/mm/page_pt.h> |
47 | #include <genarch/mm/page_ht.h> |
46 | #include <genarch/mm/page_ht.h> |
48 | #include <align.h> |
47 | #include <align.h> |
49 | #include <memstr.h> |
48 | #include <memstr.h> |
50 | #include <macros.h> |
49 | #include <macros.h> |
51 | #include <arch.h> |
50 | #include <arch.h> |
52 | 51 | ||
53 | #ifdef CONFIG_VIRT_IDX_DCACHE |
52 | #ifdef CONFIG_VIRT_IDX_DCACHE |
54 | #include <arch/mm/cache.h> |
53 | #include <arch/mm/cache.h> |
55 | #endif |
54 | #endif |
56 | 55 | ||
57 | static int elf_page_fault(as_area_t *area, uintptr_t addr, pf_access_t access); |
56 | static int elf_page_fault(as_area_t *area, uintptr_t addr, pf_access_t access); |
58 | static void elf_frame_free(as_area_t *area, uintptr_t page, uintptr_t frame); |
57 | static void elf_frame_free(as_area_t *area, uintptr_t page, uintptr_t frame); |
59 | static void elf_share(as_area_t *area); |
58 | static void elf_share(as_area_t *area); |
60 | 59 | ||
61 | mem_backend_t elf_backend = { |
60 | mem_backend_t elf_backend = { |
62 | .page_fault = elf_page_fault, |
61 | .page_fault = elf_page_fault, |
63 | .frame_free = elf_frame_free, |
62 | .frame_free = elf_frame_free, |
64 | .share = elf_share |
63 | .share = elf_share |
65 | }; |
64 | }; |
66 | 65 | ||
67 | /** Service a page fault in the ELF backend address space area. |
66 | /** Service a page fault in the ELF backend address space area. |
68 | * |
67 | * |
69 | * The address space area and page tables must be already locked. |
68 | * The address space area and page tables must be already locked. |
70 | * |
69 | * |
71 | * @param area Pointer to the address space area. |
70 | * @param area Pointer to the address space area. |
72 | * @param addr Faulting virtual address. |
71 | * @param addr Faulting virtual address. |
73 | * @param access Access mode that caused the fault (i.e. read/write/exec). |
72 | * @param access Access mode that caused the fault (i.e. read/write/exec). |
74 | * |
73 | * |
75 | * @return AS_PF_FAULT on failure (i.e. page fault) or AS_PF_OK on success (i.e. serviced). |
74 | * @return AS_PF_FAULT on failure (i.e. page fault) or AS_PF_OK on success (i.e. serviced). |
76 | */ |
75 | */ |
77 | int elf_page_fault(as_area_t *area, uintptr_t addr, pf_access_t access) |
76 | int elf_page_fault(as_area_t *area, uintptr_t addr, pf_access_t access) |
78 | { |
77 | { |
79 | elf_header_t *elf = area->backend_data.elf; |
78 | elf_header_t *elf = area->backend_data.elf; |
80 | elf_segment_header_t *entry = area->backend_data.segment; |
79 | elf_segment_header_t *entry = area->backend_data.segment; |
81 | btree_node_t *leaf; |
80 | btree_node_t *leaf; |
82 | uintptr_t base, frame; |
81 | uintptr_t base, frame; |
83 | index_t i; |
82 | index_t i; |
84 | 83 | ||
85 | if (!as_area_check_access(area, access)) |
84 | if (!as_area_check_access(area, access)) |
86 | return AS_PF_FAULT; |
85 | return AS_PF_FAULT; |
87 | 86 | ||
88 | ASSERT((addr >= entry->p_vaddr) && (addr < entry->p_vaddr + entry->p_memsz)); |
87 | ASSERT((addr >= entry->p_vaddr) && (addr < entry->p_vaddr + entry->p_memsz)); |
89 | i = (addr - entry->p_vaddr) >> PAGE_WIDTH; |
88 | i = (addr - entry->p_vaddr) >> PAGE_WIDTH; |
90 | base = (uintptr_t) (((void *) elf) + entry->p_offset); |
89 | base = (uintptr_t) (((void *) elf) + entry->p_offset); |
91 | ASSERT(ALIGN_UP(base, FRAME_SIZE) == base); |
90 | ASSERT(ALIGN_UP(base, FRAME_SIZE) == base); |
92 | 91 | ||
93 | if (area->sh_info) { |
92 | if (area->sh_info) { |
94 | bool found = false; |
93 | bool found = false; |
95 | 94 | ||
96 | /* |
95 | /* |
97 | * The address space area is shared. |
96 | * The address space area is shared. |
98 | */ |
97 | */ |
99 | 98 | ||
100 | mutex_lock(&area->sh_info->lock); |
99 | mutex_lock(&area->sh_info->lock); |
101 | frame = (uintptr_t) btree_search(&area->sh_info->pagemap, |
100 | frame = (uintptr_t) btree_search(&area->sh_info->pagemap, |
102 | ALIGN_DOWN(addr, PAGE_SIZE) - area->base, &leaf); |
101 | ALIGN_DOWN(addr, PAGE_SIZE) - area->base, &leaf); |
103 | if (!frame) { |
102 | if (!frame) { |
104 | int i; |
103 | int i; |
105 | 104 | ||
106 | /* |
105 | /* |
107 | * Workaround for valid NULL address. |
106 | * Workaround for valid NULL address. |
108 | */ |
107 | */ |
109 | 108 | ||
110 | for (i = 0; i < leaf->keys; i++) { |
109 | for (i = 0; i < leaf->keys; i++) { |
111 | if (leaf->key[i] == ALIGN_DOWN(addr, PAGE_SIZE)) { |
110 | if (leaf->key[i] == ALIGN_DOWN(addr, PAGE_SIZE)) { |
112 | found = true; |
111 | found = true; |
113 | break; |
112 | break; |
114 | } |
113 | } |
115 | } |
114 | } |
116 | } |
115 | } |
117 | if (frame || found) { |
116 | if (frame || found) { |
118 | frame_reference_add(ADDR2PFN(frame)); |
117 | frame_reference_add(ADDR2PFN(frame)); |
119 | page_mapping_insert(AS, addr, frame, as_area_get_flags(area)); |
118 | page_mapping_insert(AS, addr, frame, as_area_get_flags(area)); |
120 | if (!used_space_insert(area, ALIGN_DOWN(addr, PAGE_SIZE), 1)) |
119 | if (!used_space_insert(area, ALIGN_DOWN(addr, PAGE_SIZE), 1)) |
121 | panic("Could not insert used space.\n"); |
120 | panic("Could not insert used space.\n"); |
122 | mutex_unlock(&area->sh_info->lock); |
121 | mutex_unlock(&area->sh_info->lock); |
123 | return AS_PF_OK; |
122 | return AS_PF_OK; |
124 | } |
123 | } |
125 | } |
124 | } |
126 | 125 | ||
127 | /* |
126 | /* |
128 | * The area is either not shared or the pagemap does not contain the mapping. |
127 | * The area is either not shared or the pagemap does not contain the mapping. |
129 | */ |
128 | */ |
130 | 129 | ||
131 | if (ALIGN_DOWN(addr, PAGE_SIZE) + PAGE_SIZE < entry->p_vaddr + entry->p_filesz) { |
130 | if (ALIGN_DOWN(addr, PAGE_SIZE) + PAGE_SIZE < entry->p_vaddr + entry->p_filesz) { |
132 | /* |
131 | /* |
133 | * Initialized portion of the segment. The memory is backed |
132 | * Initialized portion of the segment. The memory is backed |
134 | * directly by the content of the ELF image. Pages are |
133 | * directly by the content of the ELF image. Pages are |
135 | * only copied if the segment is writable so that there |
134 | * only copied if the segment is writable so that there |
136 | * can be more instantions of the same memory ELF image |
135 | * can be more instantions of the same memory ELF image |
137 | * used at a time. Note that this could be later done |
136 | * used at a time. Note that this could be later done |
138 | * as COW. |
137 | * as COW. |
139 | */ |
138 | */ |
140 | if (entry->p_flags & PF_W) { |
139 | if (entry->p_flags & PF_W) { |
141 | frame = (uintptr_t)frame_alloc(ONE_FRAME, 0); |
140 | frame = (uintptr_t)frame_alloc(ONE_FRAME, 0); |
142 | memcpy((void *) PA2KA(frame), (void *) (base + i*FRAME_SIZE), FRAME_SIZE); |
141 | memcpy((void *) PA2KA(frame), (void *) (base + i*FRAME_SIZE), FRAME_SIZE); |
143 | 142 | ||
144 | if (area->sh_info) { |
143 | if (area->sh_info) { |
145 | frame_reference_add(ADDR2PFN(frame)); |
144 | frame_reference_add(ADDR2PFN(frame)); |
146 | btree_insert(&area->sh_info->pagemap, ALIGN_DOWN(addr, PAGE_SIZE) - area->base, |
145 | btree_insert(&area->sh_info->pagemap, ALIGN_DOWN(addr, PAGE_SIZE) - area->base, |
147 | (void *) frame, leaf); |
146 | (void *) frame, leaf); |
148 | } |
147 | } |
149 | 148 | ||
150 | } else { |
149 | } else { |
151 | frame = KA2PA(base + i*FRAME_SIZE); |
150 | frame = KA2PA(base + i*FRAME_SIZE); |
152 | } |
151 | } |
153 | } else if (ALIGN_DOWN(addr, PAGE_SIZE) >= ALIGN_UP(entry->p_vaddr + entry->p_filesz, PAGE_SIZE)) { |
152 | } else if (ALIGN_DOWN(addr, PAGE_SIZE) >= ALIGN_UP(entry->p_vaddr + entry->p_filesz, PAGE_SIZE)) { |
154 | /* |
153 | /* |
155 | * This is the uninitialized portion of the segment. |
154 | * This is the uninitialized portion of the segment. |
156 | * It is not physically present in the ELF image. |
155 | * It is not physically present in the ELF image. |
157 | * To resolve the situation, a frame must be allocated |
156 | * To resolve the situation, a frame must be allocated |
158 | * and cleared. |
157 | * and cleared. |
159 | */ |
158 | */ |
160 | frame = (uintptr_t)frame_alloc(ONE_FRAME, 0); |
159 | frame = (uintptr_t)frame_alloc(ONE_FRAME, 0); |
161 | memsetb(PA2KA(frame), FRAME_SIZE, 0); |
160 | memsetb(PA2KA(frame), FRAME_SIZE, 0); |
162 | 161 | ||
163 | if (area->sh_info) { |
162 | if (area->sh_info) { |
164 | frame_reference_add(ADDR2PFN(frame)); |
163 | frame_reference_add(ADDR2PFN(frame)); |
165 | btree_insert(&area->sh_info->pagemap, ALIGN_DOWN(addr, PAGE_SIZE) - area->base, |
164 | btree_insert(&area->sh_info->pagemap, ALIGN_DOWN(addr, PAGE_SIZE) - area->base, |
166 | (void *) frame, leaf); |
165 | (void *) frame, leaf); |
167 | } |
166 | } |
168 | 167 | ||
169 | } else { |
168 | } else { |
170 | size_t size; |
169 | size_t size; |
171 | /* |
170 | /* |
172 | * The mixed case. |
171 | * The mixed case. |
173 | * The lower part is backed by the ELF image and |
172 | * The lower part is backed by the ELF image and |
174 | * the upper part is anonymous memory. |
173 | * the upper part is anonymous memory. |
175 | */ |
174 | */ |
176 | size = entry->p_filesz - (i<<PAGE_WIDTH); |
175 | size = entry->p_filesz - (i<<PAGE_WIDTH); |
177 | frame = (uintptr_t)frame_alloc(ONE_FRAME, 0); |
176 | frame = (uintptr_t)frame_alloc(ONE_FRAME, 0); |
178 | memsetb(PA2KA(frame) + size, FRAME_SIZE - size, 0); |
177 | memsetb(PA2KA(frame) + size, FRAME_SIZE - size, 0); |
179 | memcpy((void *) PA2KA(frame), (void *) (base + i*FRAME_SIZE), size); |
178 | memcpy((void *) PA2KA(frame), (void *) (base + i*FRAME_SIZE), size); |
180 | 179 | ||
181 | if (area->sh_info) { |
180 | if (area->sh_info) { |
182 | frame_reference_add(ADDR2PFN(frame)); |
181 | frame_reference_add(ADDR2PFN(frame)); |
183 | btree_insert(&area->sh_info->pagemap, ALIGN_DOWN(addr, PAGE_SIZE) - area->base, |
182 | btree_insert(&area->sh_info->pagemap, ALIGN_DOWN(addr, PAGE_SIZE) - area->base, |
184 | (void *) frame, leaf); |
183 | (void *) frame, leaf); |
185 | } |
184 | } |
186 | 185 | ||
187 | } |
186 | } |
188 | 187 | ||
189 | if (area->sh_info) |
188 | if (area->sh_info) |
190 | mutex_unlock(&area->sh_info->lock); |
189 | mutex_unlock(&area->sh_info->lock); |
191 | 190 | ||
192 | page_mapping_insert(AS, addr, frame, as_area_get_flags(area)); |
191 | page_mapping_insert(AS, addr, frame, as_area_get_flags(area)); |
193 | if (!used_space_insert(area, ALIGN_DOWN(addr, PAGE_SIZE), 1)) |
192 | if (!used_space_insert(area, ALIGN_DOWN(addr, PAGE_SIZE), 1)) |
194 | panic("Could not insert used space.\n"); |
193 | panic("Could not insert used space.\n"); |
195 | 194 | ||
196 | return AS_PF_OK; |
195 | return AS_PF_OK; |
197 | } |
196 | } |
198 | 197 | ||
199 | /** Free a frame that is backed by the ELF backend. |
198 | /** Free a frame that is backed by the ELF backend. |
200 | * |
199 | * |
201 | * The address space area and page tables must be already locked. |
200 | * The address space area and page tables must be already locked. |
202 | * |
201 | * |
203 | * @param area Pointer to the address space area. |
202 | * @param area Pointer to the address space area. |
204 | * @param page Page that is mapped to frame. Must be aligned to PAGE_SIZE. |
203 | * @param page Page that is mapped to frame. Must be aligned to PAGE_SIZE. |
205 | * @param frame Frame to be released. |
204 | * @param frame Frame to be released. |
206 | * |
205 | * |
207 | */ |
206 | */ |
208 | void elf_frame_free(as_area_t *area, uintptr_t page, uintptr_t frame) |
207 | void elf_frame_free(as_area_t *area, uintptr_t page, uintptr_t frame) |
209 | { |
208 | { |
210 | elf_header_t *elf = area->backend_data.elf; |
209 | elf_header_t *elf = area->backend_data.elf; |
211 | elf_segment_header_t *entry = area->backend_data.segment; |
210 | elf_segment_header_t *entry = area->backend_data.segment; |
212 | uintptr_t base; |
211 | uintptr_t base; |
213 | index_t i; |
212 | index_t i; |
214 | 213 | ||
215 | ASSERT((page >= entry->p_vaddr) && (page < entry->p_vaddr + entry->p_memsz)); |
214 | ASSERT((page >= entry->p_vaddr) && (page < entry->p_vaddr + entry->p_memsz)); |
216 | i = (page - entry->p_vaddr) >> PAGE_WIDTH; |
215 | i = (page - entry->p_vaddr) >> PAGE_WIDTH; |
217 | base = (uintptr_t) (((void *) elf) + entry->p_offset); |
216 | base = (uintptr_t) (((void *) elf) + entry->p_offset); |
218 | ASSERT(ALIGN_UP(base, FRAME_SIZE) == base); |
217 | ASSERT(ALIGN_UP(base, FRAME_SIZE) == base); |
219 | 218 | ||
220 | if (page + PAGE_SIZE < ALIGN_UP(entry->p_vaddr + entry->p_filesz, PAGE_SIZE)) { |
219 | if (page + PAGE_SIZE < ALIGN_UP(entry->p_vaddr + entry->p_filesz, PAGE_SIZE)) { |
221 | if (entry->p_flags & PF_W) { |
220 | if (entry->p_flags & PF_W) { |
222 | /* |
221 | /* |
223 | * Free the frame with the copy of writable segment data. |
222 | * Free the frame with the copy of writable segment data. |
224 | */ |
223 | */ |
225 | frame_free(frame); |
224 | frame_free(frame); |
226 | #ifdef CONFIG_VIRT_IDX_DCACHE |
225 | #ifdef CONFIG_VIRT_IDX_DCACHE |
227 | dcache_flush_frame(page, frame); |
226 | dcache_flush_frame(page, frame); |
228 | #endif |
227 | #endif |
229 | } |
228 | } |
230 | } else { |
229 | } else { |
231 | /* |
230 | /* |
232 | * The frame is either anonymous memory or the mixed case (i.e. lower |
231 | * The frame is either anonymous memory or the mixed case (i.e. lower |
233 | * part is backed by the ELF image and the upper is anonymous). |
232 | * part is backed by the ELF image and the upper is anonymous). |
234 | * In any case, a frame needs to be freed. |
233 | * In any case, a frame needs to be freed. |
235 | */ |
234 | */ |
236 | frame_free(frame); |
235 | frame_free(frame); |
237 | #ifdef CONFIG_VIRT_IDX_DCACHE |
236 | #ifdef CONFIG_VIRT_IDX_DCACHE |
238 | dcache_flush_frame(page, frame); |
237 | dcache_flush_frame(page, frame); |
239 | #endif |
238 | #endif |
240 | } |
239 | } |
241 | } |
240 | } |
242 | 241 | ||
243 | /** Share ELF image backed address space area. |
242 | /** Share ELF image backed address space area. |
244 | * |
243 | * |
245 | * If the area is writable, then all mapped pages are duplicated in the pagemap. |
244 | * If the area is writable, then all mapped pages are duplicated in the pagemap. |
246 | * Otherwise only portions of the area that are not backed by the ELF image |
245 | * Otherwise only portions of the area that are not backed by the ELF image |
247 | * are put into the pagemap. |
246 | * are put into the pagemap. |
248 | * |
247 | * |
249 | * The address space and address space area must be locked prior to the call. |
248 | * The address space and address space area must be locked prior to the call. |
250 | * |
249 | * |
251 | * @param area Address space area. |
250 | * @param area Address space area. |
252 | */ |
251 | */ |
253 | void elf_share(as_area_t *area) |
252 | void elf_share(as_area_t *area) |
254 | { |
253 | { |
255 | elf_segment_header_t *entry = area->backend_data.segment; |
254 | elf_segment_header_t *entry = area->backend_data.segment; |
256 | link_t *cur; |
255 | link_t *cur; |
257 | btree_node_t *leaf, *node; |
256 | btree_node_t *leaf, *node; |
258 | uintptr_t start_anon = entry->p_vaddr + entry->p_filesz; |
257 | uintptr_t start_anon = entry->p_vaddr + entry->p_filesz; |
259 | 258 | ||
260 | /* |
259 | /* |
261 | * Find the node in which to start linear search. |
260 | * Find the node in which to start linear search. |
262 | */ |
261 | */ |
263 | if (area->flags & AS_AREA_WRITE) { |
262 | if (area->flags & AS_AREA_WRITE) { |
264 | node = list_get_instance(area->used_space.leaf_head.next, btree_node_t, leaf_link); |
263 | node = list_get_instance(area->used_space.leaf_head.next, btree_node_t, leaf_link); |
265 | } else { |
264 | } else { |
266 | (void) btree_search(&area->sh_info->pagemap, start_anon, &leaf); |
265 | (void) btree_search(&area->sh_info->pagemap, start_anon, &leaf); |
267 | node = btree_leaf_node_left_neighbour(&area->sh_info->pagemap, leaf); |
266 | node = btree_leaf_node_left_neighbour(&area->sh_info->pagemap, leaf); |
268 | if (!node) |
267 | if (!node) |
269 | node = leaf; |
268 | node = leaf; |
270 | } |
269 | } |
271 | 270 | ||
272 | /* |
271 | /* |
273 | * Copy used anonymous portions of the area to sh_info's page map. |
272 | * Copy used anonymous portions of the area to sh_info's page map. |
274 | */ |
273 | */ |
275 | mutex_lock(&area->sh_info->lock); |
274 | mutex_lock(&area->sh_info->lock); |
276 | for (cur = &node->leaf_link; cur != &area->used_space.leaf_head; cur = cur->next) { |
275 | for (cur = &node->leaf_link; cur != &area->used_space.leaf_head; cur = cur->next) { |
277 | int i; |
276 | int i; |
278 | 277 | ||
279 | node = list_get_instance(cur, btree_node_t, leaf_link); |
278 | node = list_get_instance(cur, btree_node_t, leaf_link); |
280 | 279 | ||
281 | for (i = 0; i < node->keys; i++) { |
280 | for (i = 0; i < node->keys; i++) { |
282 | uintptr_t base = node->key[i]; |
281 | uintptr_t base = node->key[i]; |
283 | count_t count = (count_t) node->value[i]; |
282 | count_t count = (count_t) node->value[i]; |
284 | int j; |
283 | int j; |
285 | 284 | ||
286 | /* |
285 | /* |
287 | * Skip read-only areas of used space that are backed |
286 | * Skip read-only areas of used space that are backed |
288 | * by the ELF image. |
287 | * by the ELF image. |
289 | */ |
288 | */ |
290 | if (!(area->flags & AS_AREA_WRITE)) |
289 | if (!(area->flags & AS_AREA_WRITE)) |
291 | if (base + count*PAGE_SIZE <= start_anon) |
290 | if (base + count*PAGE_SIZE <= start_anon) |
292 | continue; |
291 | continue; |
293 | 292 | ||
294 | for (j = 0; j < count; j++) { |
293 | for (j = 0; j < count; j++) { |
295 | pte_t *pte; |
294 | pte_t *pte; |
296 | 295 | ||
297 | /* |
296 | /* |
298 | * Skip read-only pages that are backed by the ELF image. |
297 | * Skip read-only pages that are backed by the ELF image. |
299 | */ |
298 | */ |
300 | if (!(area->flags & AS_AREA_WRITE)) |
299 | if (!(area->flags & AS_AREA_WRITE)) |
301 | if (base + (j + 1)*PAGE_SIZE <= start_anon) |
300 | if (base + (j + 1)*PAGE_SIZE <= start_anon) |
302 | continue; |
301 | continue; |
303 | 302 | ||
304 | page_table_lock(area->as, false); |
303 | page_table_lock(area->as, false); |
305 | pte = page_mapping_find(area->as, base + j*PAGE_SIZE); |
304 | pte = page_mapping_find(area->as, base + j*PAGE_SIZE); |
306 | ASSERT(pte && PTE_VALID(pte) && PTE_PRESENT(pte)); |
305 | ASSERT(pte && PTE_VALID(pte) && PTE_PRESENT(pte)); |
307 | btree_insert(&area->sh_info->pagemap, (base + j*PAGE_SIZE) - area->base, |
306 | btree_insert(&area->sh_info->pagemap, (base + j*PAGE_SIZE) - area->base, |
308 | (void *) PTE_GET_FRAME(pte), NULL); |
307 | (void *) PTE_GET_FRAME(pte), NULL); |
309 | page_table_unlock(area->as, false); |
308 | page_table_unlock(area->as, false); |
310 | frame_reference_add(ADDR2PFN(PTE_GET_FRAME(pte))); |
309 | frame_reference_add(ADDR2PFN(PTE_GET_FRAME(pte))); |
311 | } |
310 | } |
312 | 311 | ||
313 | } |
312 | } |
314 | } |
313 | } |
315 | mutex_unlock(&area->sh_info->lock); |
314 | mutex_unlock(&area->sh_info->lock); |
316 | } |
315 | } |
317 | 316 | ||
318 | /** @} |
317 | /** @} |
319 | */ |
318 | */ |
320 | 319 |