Use ordered dictionary for pagemap instead of B+tree
[helenos.git] / kernel / generic / src / mm / backend_anon.c
blob0831194afd44cd3653134433ac95e0338e8237c2
1 /*
2 * Copyright (c) 2006 Jakub Jermar
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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.
29 /** @addtogroup kernel_generic_mm
30 * @{
33 /**
34 * @file
35 * @brief Backend for anonymous memory address space areas.
39 #include <assert.h>
40 #include <mm/as.h>
41 #include <mm/page.h>
42 #include <mm/reserve.h>
43 #include <genarch/mm/page_pt.h>
44 #include <genarch/mm/page_ht.h>
45 #include <mm/frame.h>
46 #include <mm/slab.h>
47 #include <mm/km.h>
48 #include <synch/mutex.h>
49 #include <adt/list.h>
50 #include <adt/btree.h>
51 #include <errno.h>
52 #include <typedefs.h>
53 #include <align.h>
54 #include <mem.h>
55 #include <arch.h>
57 static bool anon_create(as_area_t *);
58 static bool anon_resize(as_area_t *, size_t);
59 static void anon_share(as_area_t *);
60 static void anon_destroy(as_area_t *);
62 static bool anon_is_resizable(as_area_t *);
63 static bool anon_is_shareable(as_area_t *);
65 static int anon_page_fault(as_area_t *, uintptr_t, pf_access_t);
66 static void anon_frame_free(as_area_t *, uintptr_t, uintptr_t);
68 mem_backend_t anon_backend = {
69 .create = anon_create,
70 .resize = anon_resize,
71 .share = anon_share,
72 .destroy = anon_destroy,
74 .is_resizable = anon_is_resizable,
75 .is_shareable = anon_is_shareable,
77 .page_fault = anon_page_fault,
78 .frame_free = anon_frame_free,
80 .create_shared_data = NULL,
81 .destroy_shared_data = NULL
84 bool anon_create(as_area_t *area)
86 if (area->flags & AS_AREA_LATE_RESERVE)
87 return true;
89 return reserve_try_alloc(area->pages);
92 bool anon_resize(as_area_t *area, size_t new_pages)
94 if (area->flags & AS_AREA_LATE_RESERVE)
95 return true;
97 if (new_pages > area->pages)
98 return reserve_try_alloc(new_pages - area->pages);
99 else if (new_pages < area->pages)
100 reserve_free(area->pages - new_pages);
102 return true;
105 /** Share the anonymous address space area.
107 * Sharing of anonymous area is done by duplicating its entire mapping
108 * to the pagemap. Page faults will primarily search for frames there.
110 * The address space and address space area must be already locked.
112 * @param area Address space area to be shared.
114 void anon_share(as_area_t *area)
116 assert(mutex_locked(&area->as->lock));
117 assert(mutex_locked(&area->lock));
118 assert(!(area->flags & AS_AREA_LATE_RESERVE));
121 * Copy used portions of the area to sh_info's page map.
123 mutex_lock(&area->sh_info->lock);
124 list_foreach(area->used_space.leaf_list, leaf_link, btree_node_t,
125 node) {
126 unsigned int i;
128 for (i = 0; i < node->keys; i++) {
129 uintptr_t base = node->key[i];
130 size_t count = (size_t) node->value[i];
131 unsigned int j;
133 for (j = 0; j < count; j++) {
134 pte_t pte;
135 bool found;
137 page_table_lock(area->as, false);
138 found = page_mapping_find(area->as,
139 base + P2SZ(j), false, &pte);
141 (void)found;
142 assert(found);
143 assert(PTE_VALID(&pte));
144 assert(PTE_PRESENT(&pte));
146 as_pagemap_insert(&area->sh_info->pagemap,
147 (base + P2SZ(j)) - area->base,
148 PTE_GET_FRAME(&pte));
149 page_table_unlock(area->as, false);
151 pfn_t pfn = ADDR2PFN(PTE_GET_FRAME(&pte));
152 frame_reference_add(pfn);
157 mutex_unlock(&area->sh_info->lock);
160 void anon_destroy(as_area_t *area)
162 if (area->flags & AS_AREA_LATE_RESERVE)
163 return;
165 reserve_free(area->pages);
168 bool anon_is_resizable(as_area_t *area)
170 return true;
173 bool anon_is_shareable(as_area_t *area)
175 return !(area->flags & AS_AREA_LATE_RESERVE);
178 /** Service a page fault in the anonymous memory address space area.
180 * The address space area and page tables must be already locked.
182 * @param area Pointer to the address space area.
183 * @param upage Faulting virtual page.
184 * @param access Access mode that caused the fault (i.e. read/write/exec).
186 * @return AS_PF_FAULT on failure (i.e. page fault) or AS_PF_OK on success (i.e.
187 * serviced).
189 int anon_page_fault(as_area_t *area, uintptr_t upage, pf_access_t access)
191 uintptr_t kpage;
192 uintptr_t frame;
194 assert(page_table_locked(AS));
195 assert(mutex_locked(&area->lock));
196 assert(IS_ALIGNED(upage, PAGE_SIZE));
198 if (!as_area_check_access(area, access))
199 return AS_PF_FAULT;
201 mutex_lock(&area->sh_info->lock);
202 if (area->sh_info->shared) {
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.
210 errno_t rc = as_pagemap_find(&area->sh_info->pagemap,
211 upage - area->base, &frame);
212 if (rc != EOK) {
213 /* Need to allocate the frame */
214 kpage = km_temporary_page_get(&frame,
215 FRAME_NO_RESERVE);
216 memsetb((void *) kpage, PAGE_SIZE, 0);
217 km_temporary_page_put(kpage);
220 * Insert the address of the newly allocated
221 * frame to the pagemap.
223 as_pagemap_insert(&area->sh_info->pagemap,
224 upage - area->base, frame);
226 frame_reference_add(ADDR2PFN(frame));
227 } else {
230 * In general, there can be several reasons that
231 * can have caused this fault.
233 * - non-existent mapping: the area is an anonymous
234 * area (e.g. heap or stack) and so far has not been
235 * allocated a frame for the faulting page
237 * - non-present mapping: another possibility,
238 * currently not implemented, would be frame
239 * reuse; when this becomes a possibility,
240 * do not forget to distinguish between
241 * the different causes
244 if (area->flags & AS_AREA_LATE_RESERVE) {
246 * Reserve the memory for this page now.
248 if (!reserve_try_alloc(1)) {
249 mutex_unlock(&area->sh_info->lock);
250 return AS_PF_SILENT;
254 kpage = km_temporary_page_get(&frame, FRAME_NO_RESERVE);
255 memsetb((void *) kpage, PAGE_SIZE, 0);
256 km_temporary_page_put(kpage);
258 mutex_unlock(&area->sh_info->lock);
261 * Map 'upage' to 'frame'.
262 * Note that TLB shootdown is not attempted as only new information is
263 * being inserted into page tables.
265 page_mapping_insert(AS, upage, frame, as_area_get_flags(area));
266 if (!used_space_insert(area, upage, 1))
267 panic("Cannot insert used space.");
269 return AS_PF_OK;
272 /** Free a frame that is backed by the anonymous memory backend.
274 * The address space area and page tables must be already locked.
276 * @param area Ignored.
277 * @param page Virtual address of the page corresponding to the frame.
278 * @param frame Frame to be released.
280 void anon_frame_free(as_area_t *area, uintptr_t page, uintptr_t frame)
282 assert(page_table_locked(area->as));
283 assert(mutex_locked(&area->lock));
285 if (area->flags & AS_AREA_LATE_RESERVE) {
287 * In case of the late reserve areas, physical memory will not
288 * be unreserved when the area is destroyed so we need to use
289 * the normal unreserving frame_free().
291 frame_free(frame, 1);
292 } else {
294 * The reserve will be given back when the area is destroyed or
295 * resized, so use the frame_free_noreserve() which does not
296 * manipulate the reserve or it would be given back twice.
298 frame_free_noreserve(frame, 1);
302 /** @}