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

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

Remove sparc64's cache.h and all references to it.

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