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