source: mainline/kernel/generic/src/mm/backend_anon.c@ 83b6ba9f

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

Support for AS area backend shared data.

  • share_info_t is now created for every new address space area.
  • share_info_t was extended to point to AS area backend shared data.
  • AS area backend may decide to define create/destroy_shared_data() methods.
  • Anonymous backend_phys frames are now freed from phys_destroy_shared_data().
  • Fixed one case of forgotten locked mutex.
  • Property mode set to 100644
File size: 8.5 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 anonymous memory address space areas.
36 *
37 */
38
39#include <mm/as.h>
40#include <mm/page.h>
41#include <mm/reserve.h>
42#include <genarch/mm/page_pt.h>
43#include <genarch/mm/page_ht.h>
44#include <mm/frame.h>
45#include <mm/slab.h>
46#include <mm/km.h>
47#include <synch/mutex.h>
48#include <adt/list.h>
49#include <adt/btree.h>
50#include <errno.h>
51#include <typedefs.h>
52#include <align.h>
53#include <memstr.h>
54#include <arch.h>
55
56static bool anon_create(as_area_t *);
57static bool anon_resize(as_area_t *, size_t);
58static void anon_share(as_area_t *);
59static void anon_destroy(as_area_t *);
60
61static bool anon_is_resizable(as_area_t *);
62static bool anon_is_shareable(as_area_t *);
63
64static int anon_page_fault(as_area_t *, uintptr_t, pf_access_t);
65static void anon_frame_free(as_area_t *, uintptr_t, uintptr_t);
66
67mem_backend_t anon_backend = {
68 .create = anon_create,
69 .resize = anon_resize,
70 .share = anon_share,
71 .destroy = anon_destroy,
72
73 .is_resizable = anon_is_resizable,
74 .is_shareable = anon_is_shareable,
75
76 .page_fault = anon_page_fault,
77 .frame_free = anon_frame_free,
78
79 .create_shared_data = NULL,
80 .destroy_shared_data = NULL
81};
82
83bool anon_create(as_area_t *area)
84{
85 if (area->flags & AS_AREA_LATE_RESERVE)
86 return true;
87
88 return reserve_try_alloc(area->pages);
89}
90
91bool anon_resize(as_area_t *area, size_t new_pages)
92{
93 if (area->flags & AS_AREA_LATE_RESERVE)
94 return true;
95
96 if (new_pages > area->pages)
97 return reserve_try_alloc(new_pages - area->pages);
98 else if (new_pages < area->pages)
99 reserve_free(area->pages - new_pages);
100
101 return true;
102}
103
104/** Share the anonymous address space area.
105 *
106 * Sharing of anonymous area is done by duplicating its entire mapping
107 * to the pagemap. Page faults will primarily search for frames there.
108 *
109 * The address space and address space area must be already locked.
110 *
111 * @param area Address space area to be shared.
112 */
113void anon_share(as_area_t *area)
114{
115 ASSERT(mutex_locked(&area->as->lock));
116 ASSERT(mutex_locked(&area->lock));
117 ASSERT(!(area->flags & AS_AREA_LATE_RESERVE));
118
119 /*
120 * Copy used portions of the area to sh_info's page map.
121 */
122 mutex_lock(&area->sh_info->lock);
123 list_foreach(area->used_space.leaf_list, leaf_link, btree_node_t,
124 node) {
125 unsigned int i;
126
127 for (i = 0; i < node->keys; i++) {
128 uintptr_t base = node->key[i];
129 size_t count = (size_t) node->value[i];
130 unsigned int j;
131
132 for (j = 0; j < count; j++) {
133 pte_t *pte;
134
135 page_table_lock(area->as, false);
136 pte = page_mapping_find(area->as,
137 base + P2SZ(j), false);
138 ASSERT(pte && PTE_VALID(pte) &&
139 PTE_PRESENT(pte));
140 btree_insert(&area->sh_info->pagemap,
141 (base + P2SZ(j)) - area->base,
142 (void *) PTE_GET_FRAME(pte), NULL);
143 page_table_unlock(area->as, false);
144
145 pfn_t pfn = ADDR2PFN(PTE_GET_FRAME(pte));
146 frame_reference_add(pfn);
147 }
148
149 }
150 }
151 mutex_unlock(&area->sh_info->lock);
152}
153
154void anon_destroy(as_area_t *area)
155{
156 if (area->flags & AS_AREA_LATE_RESERVE)
157 return;
158
159 reserve_free(area->pages);
160}
161
162bool anon_is_resizable(as_area_t *area)
163{
164 return true;
165}
166
167bool anon_is_shareable(as_area_t *area)
168{
169 return !(area->flags & AS_AREA_LATE_RESERVE);
170}
171
172/** Service a page fault in the anonymous memory address space area.
173 *
174 * The address space area and page tables must be already locked.
175 *
176 * @param area Pointer to the address space area.
177 * @param upage Faulting virtual page.
178 * @param access Access mode that caused the fault (i.e. read/write/exec).
179 *
180 * @return AS_PF_FAULT on failure (i.e. page fault) or AS_PF_OK on success (i.e.
181 * serviced).
182 */
183int anon_page_fault(as_area_t *area, uintptr_t upage, pf_access_t access)
184{
185 uintptr_t kpage;
186 uintptr_t frame;
187
188 ASSERT(page_table_locked(AS));
189 ASSERT(mutex_locked(&area->lock));
190 ASSERT(IS_ALIGNED(upage, PAGE_SIZE));
191
192 if (!as_area_check_access(area, access))
193 return AS_PF_FAULT;
194
195 mutex_lock(&area->sh_info->lock);
196 if (area->sh_info->shared) {
197 btree_node_t *leaf;
198
199 /*
200 * The area is shared, chances are that the mapping can be found
201 * in the pagemap of the address space area share info
202 * structure.
203 * In the case that the pagemap does not contain the respective
204 * mapping, a new frame is allocated and the mapping is created.
205 */
206 frame = (uintptr_t) btree_search(&area->sh_info->pagemap,
207 upage - area->base, &leaf);
208 if (!frame) {
209 bool allocate = true;
210 unsigned int i;
211
212 /*
213 * Zero can be returned as a valid frame address.
214 * Just a small workaround.
215 */
216 for (i = 0; i < leaf->keys; i++) {
217 if (leaf->key[i] == upage - area->base) {
218 allocate = false;
219 break;
220 }
221 }
222 if (allocate) {
223 kpage = km_temporary_page_get(&frame,
224 FRAME_NO_RESERVE);
225 memsetb((void *) kpage, PAGE_SIZE, 0);
226 km_temporary_page_put(kpage);
227
228 /*
229 * Insert the address of the newly allocated
230 * frame to the pagemap.
231 */
232 btree_insert(&area->sh_info->pagemap,
233 upage - area->base, (void *) frame, leaf);
234 }
235 }
236 frame_reference_add(ADDR2PFN(frame));
237 } else {
238
239 /*
240 * In general, there can be several reasons that
241 * can have caused this fault.
242 *
243 * - non-existent mapping: the area is an anonymous
244 * area (e.g. heap or stack) and so far has not been
245 * allocated a frame for the faulting page
246 *
247 * - non-present mapping: another possibility,
248 * currently not implemented, would be frame
249 * reuse; when this becomes a possibility,
250 * do not forget to distinguish between
251 * the different causes
252 */
253
254 if (area->flags & AS_AREA_LATE_RESERVE) {
255 /*
256 * Reserve the memory for this page now.
257 */
258 if (!reserve_try_alloc(1)) {
259 mutex_unlock(&area->sh_info->lock);
260 return AS_PF_SILENT;
261 }
262 }
263
264 kpage = km_temporary_page_get(&frame, FRAME_NO_RESERVE);
265 memsetb((void *) kpage, PAGE_SIZE, 0);
266 km_temporary_page_put(kpage);
267 }
268 mutex_unlock(&area->sh_info->lock);
269
270 /*
271 * Map 'upage' to 'frame'.
272 * Note that TLB shootdown is not attempted as only new information is
273 * being inserted into page tables.
274 */
275 page_mapping_insert(AS, upage, frame, as_area_get_flags(area));
276 if (!used_space_insert(area, upage, 1))
277 panic("Cannot insert used space.");
278
279 return AS_PF_OK;
280}
281
282/** Free a frame that is backed by the anonymous memory backend.
283 *
284 * The address space area and page tables must be already locked.
285 *
286 * @param area Ignored.
287 * @param page Virtual address of the page corresponding to the frame.
288 * @param frame Frame to be released.
289 */
290void anon_frame_free(as_area_t *area, uintptr_t page, uintptr_t frame)
291{
292 ASSERT(page_table_locked(area->as));
293 ASSERT(mutex_locked(&area->lock));
294
295 if (area->flags & AS_AREA_LATE_RESERVE) {
296 /*
297 * In case of the late reserve areas, physical memory will not
298 * be unreserved when the area is destroyed so we need to use
299 * the normal unreserving frame_free().
300 */
301 frame_free(frame, 1);
302 } else {
303 /*
304 * The reserve will be given back when the area is destroyed or
305 * resized, so use the frame_free_noreserve() which does not
306 * manipulate the reserve or it would be given back twice.
307 */
308 frame_free_noreserve(frame, 1);
309 }
310}
311
312/** @}
313 */
Note: See TracBrowser for help on using the repository browser.