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

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

Remove comments.

  • Property mode set to 100644
File size: 7.3 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>
46#include <synch/mutex.h>
47#include <adt/list.h>
48#include <adt/btree.h>
49#include <errno.h>
[d99c1d2]50#include <typedefs.h>
[0ee077ee]51#include <align.h>
52#include <arch.h>
53
[03523dc]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
[7f1c620]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);
[0ee077ee]61
62mem_backend_t anon_backend = {
[03523dc]63 .create = anon_create,
64 .resize = anon_resize,
65 .share = anon_share,
66 .destroy = anon_destroy,
67
[0ee077ee]68 .page_fault = anon_page_fault,
69 .frame_free = anon_frame_free,
70};
71
[03523dc]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 if (new_pages > area->pages)
80 return reserve_try_alloc(new_pages - area->pages);
81 else if (new_pages < area->pages)
82 reserve_free(area->pages - new_pages);
83
84 return true;
85}
86
87/** Share the anonymous address space area.
88 *
89 * Sharing of anonymous area is done by duplicating its entire mapping
90 * to the pagemap. Page faults will primarily search for frames there.
91 *
92 * The address space and address space area must be already locked.
93 *
94 * @param area Address space area to be shared.
95 */
96void anon_share(as_area_t *area)
97{
98 link_t *cur;
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 for (cur = area->used_space.leaf_head.next;
108 cur != &area->used_space.leaf_head; cur = cur->next) {
109 btree_node_t *node;
110 unsigned int i;
111
112 node = list_get_instance(cur, btree_node_t, leaf_link);
113 for (i = 0; i < node->keys; i++) {
114 uintptr_t base = node->key[i];
115 size_t count = (size_t) node->value[i];
116 unsigned int j;
117
118 for (j = 0; j < count; j++) {
119 pte_t *pte;
120
121 page_table_lock(area->as, false);
122 pte = page_mapping_find(area->as,
123 base + j * PAGE_SIZE);
124 ASSERT(pte && PTE_VALID(pte) &&
125 PTE_PRESENT(pte));
126 btree_insert(&area->sh_info->pagemap,
127 (base + j * PAGE_SIZE) - area->base,
128 (void *) PTE_GET_FRAME(pte), NULL);
129 page_table_unlock(area->as, false);
130
131 pfn_t pfn = ADDR2PFN(PTE_GET_FRAME(pte));
132 frame_reference_add(pfn);
133 }
134
135 }
136 }
137 mutex_unlock(&area->sh_info->lock);
138}
139
140void anon_destroy(as_area_t *area)
141{
142 reserve_free(area->pages);
143}
144
145
[0ee077ee]146/** Service a page fault in the anonymous memory address space area.
147 *
148 * The address space area and page tables must be already locked.
149 *
150 * @param area Pointer to the address space area.
151 * @param addr Faulting virtual address.
152 * @param access Access mode that caused the fault (i.e. read/write/exec).
153 *
[d5bd8d7]154 * @return AS_PF_FAULT on failure (i.e. page fault) or AS_PF_OK on success (i.e.
155 * serviced).
[0ee077ee]156 */
[7f1c620]157int anon_page_fault(as_area_t *area, uintptr_t addr, pf_access_t access)
[0ee077ee]158{
[7f1c620]159 uintptr_t frame;
[0ee077ee]160
[1d432f9]161 ASSERT(page_table_locked(AS));
162 ASSERT(mutex_locked(&area->lock));
163
[0ee077ee]164 if (!as_area_check_access(area, access))
165 return AS_PF_FAULT;
166
167 if (area->sh_info) {
168 btree_node_t *leaf;
169
170 /*
171 * The area is shared, chances are that the mapping can be found
[d5bd8d7]172 * in the pagemap of the address space area share info
173 * structure.
[0ee077ee]174 * In the case that the pagemap does not contain the respective
175 * mapping, a new frame is allocated and the mapping is created.
176 */
177 mutex_lock(&area->sh_info->lock);
[7f1c620]178 frame = (uintptr_t) btree_search(&area->sh_info->pagemap,
[454f1da]179 ALIGN_DOWN(addr, PAGE_SIZE) - area->base, &leaf);
[0ee077ee]180 if (!frame) {
181 bool allocate = true;
[6c441cf8]182 unsigned int i;
[0ee077ee]183
184 /*
185 * Zero can be returned as a valid frame address.
186 * Just a small workaround.
187 */
188 for (i = 0; i < leaf->keys; i++) {
[d5bd8d7]189 if (leaf->key[i] ==
[6461d286]190 ALIGN_DOWN(addr, PAGE_SIZE) - area->base) {
[0ee077ee]191 allocate = false;
192 break;
193 }
194 }
195 if (allocate) {
[b838fdf]196 frame = (uintptr_t) frame_alloc_noreserve(
197 ONE_FRAME, 0);
[e32e092]198 memsetb((void *) PA2KA(frame), FRAME_SIZE, 0);
[0ee077ee]199
200 /*
[d5bd8d7]201 * Insert the address of the newly allocated
202 * frame to the pagemap.
[0ee077ee]203 */
[d5bd8d7]204 btree_insert(&area->sh_info->pagemap,
205 ALIGN_DOWN(addr, PAGE_SIZE) - area->base,
206 (void *) frame, leaf);
[0ee077ee]207 }
208 }
[f9b2f305]209 frame_reference_add(ADDR2PFN(frame));
[0ee077ee]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 */
[b838fdf]227 frame = (uintptr_t) frame_alloc_noreserve(ONE_FRAME, 0);
[e32e092]228 memsetb((void *) PA2KA(frame), FRAME_SIZE, 0);
[0ee077ee]229 }
230
231 /*
232 * Map 'page' to 'frame'.
[d5bd8d7]233 * Note that TLB shootdown is not attempted as only new information is
234 * being inserted into page tables.
[0ee077ee]235 */
236 page_mapping_insert(AS, addr, frame, as_area_get_flags(area));
237 if (!used_space_insert(area, ALIGN_DOWN(addr, PAGE_SIZE), 1))
[f651e80]238 panic("Cannot insert used space.");
[0ee077ee]239
240 return AS_PF_OK;
241}
242
243/** Free a frame that is backed by the anonymous memory backend.
244 *
245 * The address space area and page tables must be already locked.
246 *
247 * @param area Ignored.
[9f63a83]248 * @param page Virtual address of the page corresponding to the frame.
[0ee077ee]249 * @param frame Frame to be released.
250 */
[7f1c620]251void anon_frame_free(as_area_t *area, uintptr_t page, uintptr_t frame)
[0ee077ee]252{
[1d432f9]253 ASSERT(page_table_locked(area->as));
254 ASSERT(mutex_locked(&area->lock));
255
[b838fdf]256 frame_free_noreserve(frame);
[0ee077ee]257}
258
[cc73a8a1]259/** @}
[b45c443]260 */
Note: See TracBrowser for help on using the repository browser.