2 * Copyright (c) 2006 Jakub Jermar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
35 * @brief Backend for anonymous memory address space areas.
42 #include <mm/reserve.h>
43 #include <genarch/mm/page_pt.h>
44 #include <genarch/mm/page_ht.h>
48 #include <synch/mutex.h>
56 static bool anon_create(as_area_t
*);
57 static bool anon_resize(as_area_t
*, size_t);
58 static void anon_share(as_area_t
*);
59 static void anon_destroy(as_area_t
*);
61 static bool anon_is_resizable(as_area_t
*);
62 static bool anon_is_shareable(as_area_t
*);
64 static int anon_page_fault(as_area_t
*, uintptr_t, pf_access_t
);
65 static void anon_frame_free(as_area_t
*, uintptr_t, uintptr_t);
67 mem_backend_t anon_backend
= {
68 .create
= anon_create
,
69 .resize
= anon_resize
,
71 .destroy
= anon_destroy
,
73 .is_resizable
= anon_is_resizable
,
74 .is_shareable
= anon_is_shareable
,
76 .page_fault
= anon_page_fault
,
77 .frame_free
= anon_frame_free
,
79 .create_shared_data
= NULL
,
80 .destroy_shared_data
= NULL
83 bool anon_create(as_area_t
*area
)
85 if (area
->flags
& AS_AREA_LATE_RESERVE
)
88 return reserve_try_alloc(area
->pages
);
91 bool anon_resize(as_area_t
*area
, size_t new_pages
)
93 if (area
->flags
& AS_AREA_LATE_RESERVE
)
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
);
104 /** Share the anonymous address space area.
106 * Sharing of anonymous area is done by duplicating its entire mapping
107 * to the pagemap. Page faults will primarily search for frames there.
109 * The address space and address space area must be already locked.
111 * @param area Address space area to be shared.
113 void anon_share(as_area_t
*area
)
115 assert(mutex_locked(&area
->as
->lock
));
116 assert(mutex_locked(&area
->lock
));
117 assert(!(area
->flags
& AS_AREA_LATE_RESERVE
));
120 * Copy used portions of the area to sh_info's page map.
122 mutex_lock(&area
->sh_info
->lock
);
123 used_space_ival_t
*ival
= used_space_first(&area
->used_space
);
124 while (ival
!= NULL
) {
125 uintptr_t base
= ival
->page
;
126 size_t count
= ival
->count
;
129 for (j
= 0; j
< count
; j
++) {
133 page_table_lock(area
->as
, false);
134 found
= page_mapping_find(area
->as
, base
+ P2SZ(j
),
139 assert(PTE_VALID(&pte
));
140 assert(PTE_PRESENT(&pte
));
142 as_pagemap_insert(&area
->sh_info
->pagemap
,
143 (base
+ P2SZ(j
)) - area
->base
, PTE_GET_FRAME(&pte
));
144 page_table_unlock(area
->as
, false);
146 pfn_t pfn
= ADDR2PFN(PTE_GET_FRAME(&pte
));
147 frame_reference_add(pfn
);
150 ival
= used_space_next(ival
);
152 mutex_unlock(&area
->sh_info
->lock
);
155 void anon_destroy(as_area_t
*area
)
157 if (area
->flags
& AS_AREA_LATE_RESERVE
)
160 reserve_free(area
->pages
);
163 bool anon_is_resizable(as_area_t
*area
)
168 bool anon_is_shareable(as_area_t
*area
)
170 return !(area
->flags
& AS_AREA_LATE_RESERVE
);
173 /** Service a page fault in the anonymous memory address space area.
175 * The address space area and page tables must be already locked.
177 * @param area Pointer to the address space area.
178 * @param upage Faulting virtual page.
179 * @param access Access mode that caused the fault (i.e. read/write/exec).
181 * @return AS_PF_FAULT on failure (i.e. page fault) or AS_PF_OK on success (i.e.
184 int anon_page_fault(as_area_t
*area
, uintptr_t upage
, pf_access_t access
)
189 assert(page_table_locked(AS
));
190 assert(mutex_locked(&area
->lock
));
191 assert(IS_ALIGNED(upage
, PAGE_SIZE
));
193 if (!as_area_check_access(area
, access
))
196 mutex_lock(&area
->sh_info
->lock
);
197 if (area
->sh_info
->shared
) {
199 * The area is shared, chances are that the mapping can be found
200 * in the pagemap of the address space area share info
202 * In the case that the pagemap does not contain the respective
203 * mapping, a new frame is allocated and the mapping is created.
205 errno_t rc
= as_pagemap_find(&area
->sh_info
->pagemap
,
206 upage
- area
->base
, &frame
);
208 /* Need to allocate the frame */
209 kpage
= km_temporary_page_get(&frame
,
211 memsetb((void *) kpage
, PAGE_SIZE
, 0);
212 km_temporary_page_put(kpage
);
215 * Insert the address of the newly allocated
216 * frame to the pagemap.
218 as_pagemap_insert(&area
->sh_info
->pagemap
,
219 upage
- area
->base
, frame
);
221 frame_reference_add(ADDR2PFN(frame
));
225 * In general, there can be several reasons that
226 * can have caused this fault.
228 * - non-existent mapping: the area is an anonymous
229 * area (e.g. heap or stack) and so far has not been
230 * allocated a frame for the faulting page
232 * - non-present mapping: another possibility,
233 * currently not implemented, would be frame
234 * reuse; when this becomes a possibility,
235 * do not forget to distinguish between
236 * the different causes
239 if (area
->flags
& AS_AREA_LATE_RESERVE
) {
241 * Reserve the memory for this page now.
243 if (!reserve_try_alloc(1)) {
244 mutex_unlock(&area
->sh_info
->lock
);
249 kpage
= km_temporary_page_get(&frame
, FRAME_NO_RESERVE
);
250 memsetb((void *) kpage
, PAGE_SIZE
, 0);
251 km_temporary_page_put(kpage
);
253 mutex_unlock(&area
->sh_info
->lock
);
256 * Map 'upage' to 'frame'.
257 * Note that TLB shootdown is not attempted as only new information is
258 * being inserted into page tables.
260 page_mapping_insert(AS
, upage
, frame
, as_area_get_flags(area
));
261 if (!used_space_insert(&area
->used_space
, upage
, 1))
262 panic("Cannot insert used space.");
267 /** Free a frame that is backed by the anonymous memory backend.
269 * The address space area and page tables must be already locked.
271 * @param area Ignored.
272 * @param page Virtual address of the page corresponding to the frame.
273 * @param frame Frame to be released.
275 void anon_frame_free(as_area_t
*area
, uintptr_t page
, uintptr_t frame
)
277 assert(page_table_locked(area
->as
));
278 assert(mutex_locked(&area
->lock
));
280 if (area
->flags
& AS_AREA_LATE_RESERVE
) {
282 * In case of the late reserve areas, physical memory will not
283 * be unreserved when the area is destroyed so we need to use
284 * the normal unreserving frame_free().
286 frame_free(frame
, 1);
289 * The reserve will be given back when the area is destroyed or
290 * resized, so use the frame_free_noreserve() which does not
291 * manipulate the reserve or it would be given back twice.
293 frame_free_noreserve(frame
, 1);