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