source: mainline/kernel/generic/src/mm/backend_anon.c@ 692bd3f2

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

Define AS_AREA_NORESERVE and allow its use in the anonymous backend.

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