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

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

Reflect assumptions about lock and interrupt state in functions themselves.

  • Property mode set to 100644
File size: 6.8 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>
41#include <genarch/mm/page_pt.h>
42#include <genarch/mm/page_ht.h>
43#include <mm/frame.h>
44#include <mm/slab.h>
45#include <synch/mutex.h>
46#include <adt/list.h>
47#include <adt/btree.h>
48#include <errno.h>
[d99c1d2]49#include <typedefs.h>
[0ee077ee]50#include <align.h>
51#include <arch.h>
52
[9f63a83]53#ifdef CONFIG_VIRT_IDX_DCACHE
54#include <arch/mm/cache.h>
55#endif
56
[7f1c620]57static int anon_page_fault(as_area_t *area, uintptr_t addr, pf_access_t access);
58static void anon_frame_free(as_area_t *area, uintptr_t page, uintptr_t frame);
[0ee077ee]59static void anon_share(as_area_t *area);
60
61mem_backend_t anon_backend = {
62 .page_fault = anon_page_fault,
63 .frame_free = anon_frame_free,
64 .share = anon_share
65};
66
67/** Service a page fault in the anonymous memory address space area.
68 *
69 * The address space area and page tables must be already locked.
70 *
71 * @param area Pointer to the address space area.
72 * @param addr Faulting virtual address.
73 * @param access Access mode that caused the fault (i.e. read/write/exec).
74 *
[d5bd8d7]75 * @return AS_PF_FAULT on failure (i.e. page fault) or AS_PF_OK on success (i.e.
76 * serviced).
[0ee077ee]77 */
[7f1c620]78int anon_page_fault(as_area_t *area, uintptr_t addr, pf_access_t access)
[0ee077ee]79{
[7f1c620]80 uintptr_t frame;
[0ee077ee]81
[1d432f9]82 ASSERT(page_table_locked(AS));
83 ASSERT(mutex_locked(&area->lock));
84
[0ee077ee]85 if (!as_area_check_access(area, access))
86 return AS_PF_FAULT;
87
88 if (area->sh_info) {
89 btree_node_t *leaf;
90
91 /*
92 * The area is shared, chances are that the mapping can be found
[d5bd8d7]93 * in the pagemap of the address space area share info
94 * structure.
[0ee077ee]95 * In the case that the pagemap does not contain the respective
96 * mapping, a new frame is allocated and the mapping is created.
97 */
98 mutex_lock(&area->sh_info->lock);
[7f1c620]99 frame = (uintptr_t) btree_search(&area->sh_info->pagemap,
[454f1da]100 ALIGN_DOWN(addr, PAGE_SIZE) - area->base, &leaf);
[0ee077ee]101 if (!frame) {
102 bool allocate = true;
[6c441cf8]103 unsigned int i;
[0ee077ee]104
105 /*
106 * Zero can be returned as a valid frame address.
107 * Just a small workaround.
108 */
109 for (i = 0; i < leaf->keys; i++) {
[d5bd8d7]110 if (leaf->key[i] ==
[6461d286]111 ALIGN_DOWN(addr, PAGE_SIZE) - area->base) {
[0ee077ee]112 allocate = false;
113 break;
114 }
115 }
116 if (allocate) {
[7f1c620]117 frame = (uintptr_t) frame_alloc(ONE_FRAME, 0);
[e32e092]118 memsetb((void *) PA2KA(frame), FRAME_SIZE, 0);
[0ee077ee]119
120 /*
[d5bd8d7]121 * Insert the address of the newly allocated
122 * frame to the pagemap.
[0ee077ee]123 */
[d5bd8d7]124 btree_insert(&area->sh_info->pagemap,
125 ALIGN_DOWN(addr, PAGE_SIZE) - area->base,
126 (void *) frame, leaf);
[0ee077ee]127 }
128 }
[f9b2f305]129 frame_reference_add(ADDR2PFN(frame));
[0ee077ee]130 mutex_unlock(&area->sh_info->lock);
131 } else {
132
133 /*
134 * In general, there can be several reasons that
135 * can have caused this fault.
136 *
137 * - non-existent mapping: the area is an anonymous
138 * area (e.g. heap or stack) and so far has not been
139 * allocated a frame for the faulting page
140 *
141 * - non-present mapping: another possibility,
142 * currently not implemented, would be frame
143 * reuse; when this becomes a possibility,
144 * do not forget to distinguish between
145 * the different causes
146 */
[8440473]147 frame = (uintptr_t) frame_alloc(ONE_FRAME, 0);
[e32e092]148 memsetb((void *) PA2KA(frame), FRAME_SIZE, 0);
[0ee077ee]149 }
150
151 /*
152 * Map 'page' to 'frame'.
[d5bd8d7]153 * Note that TLB shootdown is not attempted as only new information is
154 * being inserted into page tables.
[0ee077ee]155 */
156 page_mapping_insert(AS, addr, frame, as_area_get_flags(area));
157 if (!used_space_insert(area, ALIGN_DOWN(addr, PAGE_SIZE), 1))
[f651e80]158 panic("Cannot insert used space.");
[0ee077ee]159
160 return AS_PF_OK;
161}
162
163/** Free a frame that is backed by the anonymous memory backend.
164 *
165 * The address space area and page tables must be already locked.
166 *
167 * @param area Ignored.
[9f63a83]168 * @param page Virtual address of the page corresponding to the frame.
[0ee077ee]169 * @param frame Frame to be released.
170 */
[7f1c620]171void anon_frame_free(as_area_t *area, uintptr_t page, uintptr_t frame)
[0ee077ee]172{
[1d432f9]173 ASSERT(page_table_locked(area->as));
174 ASSERT(mutex_locked(&area->lock));
175
[2e9eae2]176 frame_free(frame);
[0ee077ee]177}
178
179/** Share the anonymous address space area.
180 *
181 * Sharing of anonymous area is done by duplicating its entire mapping
182 * to the pagemap. Page faults will primarily search for frames there.
183 *
[00b595b]184 * The address space and address space area must be already locked.
[0ee077ee]185 *
186 * @param area Address space area to be shared.
187 */
188void anon_share(as_area_t *area)
189{
190 link_t *cur;
191
[1d432f9]192 ASSERT(mutex_locked(&area->as->lock));
193 ASSERT(mutex_locked(&area->lock));
194
[0ee077ee]195 /*
196 * Copy used portions of the area to sh_info's page map.
197 */
198 mutex_lock(&area->sh_info->lock);
[d5bd8d7]199 for (cur = area->used_space.leaf_head.next;
200 cur != &area->used_space.leaf_head; cur = cur->next) {
[0ee077ee]201 btree_node_t *node;
[6c441cf8]202 unsigned int i;
[0ee077ee]203
204 node = list_get_instance(cur, btree_node_t, leaf_link);
205 for (i = 0; i < node->keys; i++) {
[7f1c620]206 uintptr_t base = node->key[i];
[98000fb]207 size_t count = (size_t) node->value[i];
[6c441cf8]208 unsigned int j;
[0ee077ee]209
210 for (j = 0; j < count; j++) {
211 pte_t *pte;
212
213 page_table_lock(area->as, false);
[d5bd8d7]214 pte = page_mapping_find(area->as,
215 base + j * PAGE_SIZE);
216 ASSERT(pte && PTE_VALID(pte) &&
217 PTE_PRESENT(pte));
218 btree_insert(&area->sh_info->pagemap,
219 (base + j * PAGE_SIZE) - area->base,
220 (void *) PTE_GET_FRAME(pte), NULL);
[0ee077ee]221 page_table_unlock(area->as, false);
[d5bd8d7]222
223 pfn_t pfn = ADDR2PFN(PTE_GET_FRAME(pte));
224 frame_reference_add(pfn);
[0ee077ee]225 }
[454f1da]226
[0ee077ee]227 }
228 }
229 mutex_unlock(&area->sh_info->lock);
230}
[b45c443]231
[cc73a8a1]232/** @}
[b45c443]233 */
Note: See TracBrowser for help on using the repository browser.