source: mainline/kernel/generic/src/mm/backend_anon.c@ 3deb0155

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

Unify the use of virtual addresses and virtual page addresses in mm code.

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