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

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

Use temporary pages in the anonymous backend.

  • Property mode set to 100644
File size: 7.3 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 int anon_page_fault(as_area_t *, uintptr_t, pf_access_t);
62static void anon_frame_free(as_area_t *, uintptr_t, uintptr_t);
63
64mem_backend_t anon_backend = {
65 .create = anon_create,
66 .resize = anon_resize,
67 .share = anon_share,
68 .destroy = anon_destroy,
69
70 .page_fault = anon_page_fault,
71 .frame_free = anon_frame_free,
72};
73
74bool anon_create(as_area_t *area)
75{
76 return reserve_try_alloc(area->pages);
77}
78
79bool anon_resize(as_area_t *area, size_t new_pages)
80{
81 if (new_pages > area->pages)
82 return reserve_try_alloc(new_pages - area->pages);
83 else if (new_pages < area->pages)
84 reserve_free(area->pages - new_pages);
85
86 return true;
87}
88
89/** Share the anonymous address space area.
90 *
91 * Sharing of anonymous area is done by duplicating its entire mapping
92 * to the pagemap. Page faults will primarily search for frames there.
93 *
94 * The address space and address space area must be already locked.
95 *
96 * @param area Address space area to be shared.
97 */
98void anon_share(as_area_t *area)
99{
100 ASSERT(mutex_locked(&area->as->lock));
101 ASSERT(mutex_locked(&area->lock));
102
103 /*
104 * Copy used portions of the area to sh_info's page map.
105 */
106 mutex_lock(&area->sh_info->lock);
107 list_foreach(area->used_space.leaf_list, cur) {
108 btree_node_t *node;
109 unsigned int i;
110
111 node = list_get_instance(cur, btree_node_t, leaf_link);
112 for (i = 0; i < node->keys; i++) {
113 uintptr_t base = node->key[i];
114 size_t count = (size_t) node->value[i];
115 unsigned int j;
116
117 for (j = 0; j < count; j++) {
118 pte_t *pte;
119
120 page_table_lock(area->as, false);
121 pte = page_mapping_find(area->as,
122 base + P2SZ(j), false);
123 ASSERT(pte && PTE_VALID(pte) &&
124 PTE_PRESENT(pte));
125 btree_insert(&area->sh_info->pagemap,
126 (base + P2SZ(j)) - area->base,
127 (void *) PTE_GET_FRAME(pte), NULL);
128 page_table_unlock(area->as, false);
129
130 pfn_t pfn = ADDR2PFN(PTE_GET_FRAME(pte));
131 frame_reference_add(pfn);
132 }
133
134 }
135 }
136 mutex_unlock(&area->sh_info->lock);
137}
138
139void anon_destroy(as_area_t *area)
140{
141 reserve_free(area->pages);
142}
143
144
145/** Service a page fault in the anonymous memory address space area.
146 *
147 * The address space area and page tables must be already locked.
148 *
149 * @param area Pointer to the address space area.
150 * @param addr Faulting virtual address.
151 * @param access Access mode that caused the fault (i.e. read/write/exec).
152 *
153 * @return AS_PF_FAULT on failure (i.e. page fault) or AS_PF_OK on success (i.e.
154 * serviced).
155 */
156int anon_page_fault(as_area_t *area, uintptr_t addr, pf_access_t access)
157{
158 uintptr_t upage = ALIGN_DOWN(addr, PAGE_SIZE);
159 uintptr_t kpage;
160 uintptr_t frame;
161
162 ASSERT(page_table_locked(AS));
163 ASSERT(mutex_locked(&area->lock));
164
165 if (!as_area_check_access(area, access))
166 return AS_PF_FAULT;
167
168 if (area->sh_info) {
169 btree_node_t *leaf;
170
171 /*
172 * The area is shared, chances are that the mapping can be found
173 * in the pagemap of the address space area share info
174 * structure.
175 * In the case that the pagemap does not contain the respective
176 * mapping, a new frame is allocated and the mapping is created.
177 */
178 mutex_lock(&area->sh_info->lock);
179 frame = (uintptr_t) btree_search(&area->sh_info->pagemap,
180 upage - area->base, &leaf);
181 if (!frame) {
182 bool allocate = true;
183 unsigned int i;
184
185 /*
186 * Zero can be returned as a valid frame address.
187 * Just a small workaround.
188 */
189 for (i = 0; i < leaf->keys; i++) {
190 if (leaf->key[i] == upage - area->base) {
191 allocate = false;
192 break;
193 }
194 }
195 if (allocate) {
196 kpage = km_temporary_page_get(&frame,
197 FRAME_NO_RESERVE);
198 memsetb((void *) kpage, PAGE_SIZE, 0);
199 km_temporary_page_put(kpage);
200
201 /*
202 * Insert the address of the newly allocated
203 * frame to the pagemap.
204 */
205 btree_insert(&area->sh_info->pagemap,
206 upage - area->base, (void *) frame, leaf);
207 }
208 }
209 frame_reference_add(ADDR2PFN(frame));
210 mutex_unlock(&area->sh_info->lock);
211 } else {
212
213 /*
214 * In general, there can be several reasons that
215 * can have caused this fault.
216 *
217 * - non-existent mapping: the area is an anonymous
218 * area (e.g. heap or stack) and so far has not been
219 * allocated a frame for the faulting page
220 *
221 * - non-present mapping: another possibility,
222 * currently not implemented, would be frame
223 * reuse; when this becomes a possibility,
224 * do not forget to distinguish between
225 * the different causes
226 */
227 kpage = km_temporary_page_get(&frame, FRAME_NO_RESERVE);
228 memsetb((void *) kpage, PAGE_SIZE, 0);
229 km_temporary_page_put(kpage);
230 }
231
232 /*
233 * Map 'upage' to 'frame'.
234 * Note that TLB shootdown is not attempted as only new information is
235 * being inserted into page tables.
236 */
237 page_mapping_insert(AS, upage, frame, as_area_get_flags(area));
238 if (!used_space_insert(area, upage, 1))
239 panic("Cannot insert used space.");
240
241 return AS_PF_OK;
242}
243
244/** Free a frame that is backed by the anonymous memory backend.
245 *
246 * The address space area and page tables must be already locked.
247 *
248 * @param area Ignored.
249 * @param page Virtual address of the page corresponding to the frame.
250 * @param frame Frame to be released.
251 */
252void anon_frame_free(as_area_t *area, uintptr_t page, uintptr_t frame)
253{
254 ASSERT(page_table_locked(area->as));
255 ASSERT(mutex_locked(&area->lock));
256
257 frame_free_noreserve(frame);
258}
259
260/** @}
261 */
Note: See TracBrowser for help on using the repository browser.