source: mainline/kernel/generic/src/mm/backend_anon.c@ 38dc82d

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

Make page_mapping_find() return a copy rather than the actual PTE

This makes page_mapping_find() more suitable for use with lock-free data
structures such as CHT that guarantee existence of the data only for
some limited time while a condition holds (e.g. inside of a RCU-protected
critical section that must be around all CHT lookups).

  • 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 bool found;
135
136 page_table_lock(area->as, false);
137 found = page_mapping_find(area->as,
138 base + P2SZ(j), false, &pte);
139
140 ASSERT(found);
141 ASSERT(PTE_VALID(&pte));
142 ASSERT(PTE_PRESENT(&pte));
143
144 btree_insert(&area->sh_info->pagemap,
145 (base + P2SZ(j)) - area->base,
146 (void *) PTE_GET_FRAME(&pte), NULL);
147 page_table_unlock(area->as, false);
148
149 pfn_t pfn = ADDR2PFN(PTE_GET_FRAME(&pte));
150 frame_reference_add(pfn);
151 }
152
153 }
154 }
155 mutex_unlock(&area->sh_info->lock);
156}
157
158void anon_destroy(as_area_t *area)
159{
160 if (area->flags & AS_AREA_LATE_RESERVE)
161 return;
162
163 reserve_free(area->pages);
164}
165
166bool anon_is_resizable(as_area_t *area)
167{
168 return true;
169}
170
171bool anon_is_shareable(as_area_t *area)
172{
173 return !(area->flags & AS_AREA_LATE_RESERVE);
174}
175
176/** Service a page fault in the anonymous memory address space area.
177 *
178 * The address space area and page tables must be already locked.
179 *
180 * @param area Pointer to the address space area.
181 * @param upage Faulting virtual page.
182 * @param access Access mode that caused the fault (i.e. read/write/exec).
183 *
184 * @return AS_PF_FAULT on failure (i.e. page fault) or AS_PF_OK on success (i.e.
185 * serviced).
186 */
187int anon_page_fault(as_area_t *area, uintptr_t upage, pf_access_t access)
188{
189 uintptr_t kpage;
190 uintptr_t frame;
191
192 ASSERT(page_table_locked(AS));
193 ASSERT(mutex_locked(&area->lock));
194 ASSERT(IS_ALIGNED(upage, PAGE_SIZE));
195
196 if (!as_area_check_access(area, access))
197 return AS_PF_FAULT;
198
199 mutex_lock(&area->sh_info->lock);
200 if (area->sh_info->shared) {
201 btree_node_t *leaf;
202
203 /*
204 * The area is shared, chances are that the mapping can be found
205 * in the pagemap of the address space area share info
206 * structure.
207 * In the case that the pagemap does not contain the respective
208 * mapping, a new frame is allocated and the mapping is created.
209 */
210 frame = (uintptr_t) btree_search(&area->sh_info->pagemap,
211 upage - area->base, &leaf);
212 if (!frame) {
213 bool allocate = true;
214 unsigned int i;
215
216 /*
217 * Zero can be returned as a valid frame address.
218 * Just a small workaround.
219 */
220 for (i = 0; i < leaf->keys; i++) {
221 if (leaf->key[i] == upage - area->base) {
222 allocate = false;
223 break;
224 }
225 }
226 if (allocate) {
227 kpage = km_temporary_page_get(&frame,
228 FRAME_NO_RESERVE);
229 memsetb((void *) kpage, PAGE_SIZE, 0);
230 km_temporary_page_put(kpage);
231
232 /*
233 * Insert the address of the newly allocated
234 * frame to the pagemap.
235 */
236 btree_insert(&area->sh_info->pagemap,
237 upage - area->base, (void *) frame, leaf);
238 }
239 }
240 frame_reference_add(ADDR2PFN(frame));
241 } else {
242
243 /*
244 * In general, there can be several reasons that
245 * can have caused this fault.
246 *
247 * - non-existent mapping: the area is an anonymous
248 * area (e.g. heap or stack) and so far has not been
249 * allocated a frame for the faulting page
250 *
251 * - non-present mapping: another possibility,
252 * currently not implemented, would be frame
253 * reuse; when this becomes a possibility,
254 * do not forget to distinguish between
255 * the different causes
256 */
257
258 if (area->flags & AS_AREA_LATE_RESERVE) {
259 /*
260 * Reserve the memory for this page now.
261 */
262 if (!reserve_try_alloc(1)) {
263 mutex_unlock(&area->sh_info->lock);
264 return AS_PF_SILENT;
265 }
266 }
267
268 kpage = km_temporary_page_get(&frame, FRAME_NO_RESERVE);
269 memsetb((void *) kpage, PAGE_SIZE, 0);
270 km_temporary_page_put(kpage);
271 }
272 mutex_unlock(&area->sh_info->lock);
273
274 /*
275 * Map 'upage' to 'frame'.
276 * Note that TLB shootdown is not attempted as only new information is
277 * being inserted into page tables.
278 */
279 page_mapping_insert(AS, upage, frame, as_area_get_flags(area));
280 if (!used_space_insert(area, upage, 1))
281 panic("Cannot insert used space.");
282
283 return AS_PF_OK;
284}
285
286/** Free a frame that is backed by the anonymous memory backend.
287 *
288 * The address space area and page tables must be already locked.
289 *
290 * @param area Ignored.
291 * @param page Virtual address of the page corresponding to the frame.
292 * @param frame Frame to be released.
293 */
294void anon_frame_free(as_area_t *area, uintptr_t page, uintptr_t frame)
295{
296 ASSERT(page_table_locked(area->as));
297 ASSERT(mutex_locked(&area->lock));
298
299 if (area->flags & AS_AREA_LATE_RESERVE) {
300 /*
301 * In case of the late reserve areas, physical memory will not
302 * be unreserved when the area is destroyed so we need to use
303 * the normal unreserving frame_free().
304 */
305 frame_free(frame, 1);
306 } else {
307 /*
308 * The reserve will be given back when the area is destroyed or
309 * resized, so use the frame_free_noreserve() which does not
310 * manipulate the reserve or it would be given back twice.
311 */
312 frame_free_noreserve(frame, 1);
313 }
314}
315
316/** @}
317 */
Note: See TracBrowser for help on using the repository browser.