[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>
|
---|
[c142860] | 46 | #include <mm/km.h>
|
---|
[0ee077ee] | 47 | #include <synch/mutex.h>
|
---|
| 48 | #include <adt/list.h>
|
---|
| 49 | #include <adt/btree.h>
|
---|
| 50 | #include <errno.h>
|
---|
[d99c1d2] | 51 | #include <typedefs.h>
|
---|
[0ee077ee] | 52 | #include <align.h>
|
---|
[b6f3e7e] | 53 | #include <memstr.h>
|
---|
[0ee077ee] | 54 | #include <arch.h>
|
---|
| 55 |
|
---|
[03523dc] | 56 | static bool anon_create(as_area_t *);
|
---|
| 57 | static bool anon_resize(as_area_t *, size_t);
|
---|
[ceac698] | 58 | static void anon_share(as_area_t *);
|
---|
[03523dc] | 59 | static void anon_destroy(as_area_t *);
|
---|
| 60 |
|
---|
[01029fc] | 61 | static bool anon_is_resizable(as_area_t *);
|
---|
| 62 | static bool anon_is_shareable(as_area_t *);
|
---|
| 63 |
|
---|
[cda1378] | 64 | static int anon_page_fault(as_area_t *, uintptr_t, pf_access_t);
|
---|
| 65 | static void anon_frame_free(as_area_t *, uintptr_t, uintptr_t);
|
---|
[0ee077ee] | 66 |
|
---|
| 67 | mem_backend_t anon_backend = {
|
---|
[03523dc] | 68 | .create = anon_create,
|
---|
| 69 | .resize = anon_resize,
|
---|
| 70 | .share = anon_share,
|
---|
| 71 | .destroy = anon_destroy,
|
---|
| 72 |
|
---|
[01029fc] | 73 | .is_resizable = anon_is_resizable,
|
---|
| 74 | .is_shareable = anon_is_shareable,
|
---|
| 75 |
|
---|
[0ee077ee] | 76 | .page_fault = anon_page_fault,
|
---|
| 77 | .frame_free = anon_frame_free,
|
---|
[83b6ba9f] | 78 |
|
---|
| 79 | .create_shared_data = NULL,
|
---|
| 80 | .destroy_shared_data = NULL
|
---|
[0ee077ee] | 81 | };
|
---|
| 82 |
|
---|
[03523dc] | 83 | bool anon_create(as_area_t *area)
|
---|
| 84 | {
|
---|
[5892ec1] | 85 | if (area->flags & AS_AREA_LATE_RESERVE)
|
---|
[692bd3f2] | 86 | return true;
|
---|
| 87 |
|
---|
[03523dc] | 88 | return reserve_try_alloc(area->pages);
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | bool anon_resize(as_area_t *area, size_t new_pages)
|
---|
| 92 | {
|
---|
[5892ec1] | 93 | if (area->flags & AS_AREA_LATE_RESERVE)
|
---|
[692bd3f2] | 94 | return true;
|
---|
| 95 |
|
---|
[03523dc] | 96 | if (new_pages > area->pages)
|
---|
| 97 | return reserve_try_alloc(new_pages - area->pages);
|
---|
| 98 | else if (new_pages < area->pages)
|
---|
| 99 | reserve_free(area->pages - new_pages);
|
---|
| 100 |
|
---|
| 101 | return true;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | /** Share the anonymous address space area.
|
---|
| 105 | *
|
---|
| 106 | * Sharing of anonymous area is done by duplicating its entire mapping
|
---|
| 107 | * to the pagemap. Page faults will primarily search for frames there.
|
---|
| 108 | *
|
---|
| 109 | * The address space and address space area must be already locked.
|
---|
| 110 | *
|
---|
| 111 | * @param area Address space area to be shared.
|
---|
| 112 | */
|
---|
| 113 | void anon_share(as_area_t *area)
|
---|
| 114 | {
|
---|
| 115 | ASSERT(mutex_locked(&area->as->lock));
|
---|
| 116 | ASSERT(mutex_locked(&area->lock));
|
---|
[5892ec1] | 117 | ASSERT(!(area->flags & AS_AREA_LATE_RESERVE));
|
---|
[03523dc] | 118 |
|
---|
| 119 | /*
|
---|
| 120 | * Copy used portions of the area to sh_info's page map.
|
---|
| 121 | */
|
---|
| 122 | mutex_lock(&area->sh_info->lock);
|
---|
[feeac0d] | 123 | list_foreach(area->used_space.leaf_list, leaf_link, btree_node_t,
|
---|
| 124 | node) {
|
---|
[03523dc] | 125 | unsigned int i;
|
---|
| 126 |
|
---|
| 127 | for (i = 0; i < node->keys; i++) {
|
---|
| 128 | uintptr_t base = node->key[i];
|
---|
| 129 | size_t count = (size_t) node->value[i];
|
---|
| 130 | unsigned int j;
|
---|
| 131 |
|
---|
| 132 | for (j = 0; j < count; j++) {
|
---|
[38dc82d] | 133 | pte_t pte;
|
---|
| 134 | bool found;
|
---|
[03523dc] | 135 |
|
---|
| 136 | page_table_lock(area->as, false);
|
---|
[38dc82d] | 137 | found = page_mapping_find(area->as,
|
---|
| 138 | base + P2SZ(j), false, &pte);
|
---|
| 139 |
|
---|
| 140 | ASSERT(found);
|
---|
| 141 | ASSERT(PTE_VALID(&pte));
|
---|
| 142 | ASSERT(PTE_PRESENT(&pte));
|
---|
| 143 |
|
---|
[03523dc] | 144 | btree_insert(&area->sh_info->pagemap,
|
---|
[b4ffe5bc] | 145 | (base + P2SZ(j)) - area->base,
|
---|
[38dc82d] | 146 | (void *) PTE_GET_FRAME(&pte), NULL);
|
---|
[03523dc] | 147 | page_table_unlock(area->as, false);
|
---|
| 148 |
|
---|
[38dc82d] | 149 | pfn_t pfn = ADDR2PFN(PTE_GET_FRAME(&pte));
|
---|
[03523dc] | 150 | frame_reference_add(pfn);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 | mutex_unlock(&area->sh_info->lock);
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | void anon_destroy(as_area_t *area)
|
---|
| 159 | {
|
---|
[5892ec1] | 160 | if (area->flags & AS_AREA_LATE_RESERVE)
|
---|
[692bd3f2] | 161 | return;
|
---|
| 162 |
|
---|
[03523dc] | 163 | reserve_free(area->pages);
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[01029fc] | 166 | bool anon_is_resizable(as_area_t *area)
|
---|
| 167 | {
|
---|
| 168 | return true;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | bool anon_is_shareable(as_area_t *area)
|
---|
| 172 | {
|
---|
| 173 | return !(area->flags & AS_AREA_LATE_RESERVE);
|
---|
| 174 | }
|
---|
[03523dc] | 175 |
|
---|
[0ee077ee] | 176 | /** Service a page fault in the anonymous memory address space area.
|
---|
| 177 | *
|
---|
| 178 | * The address space area and page tables must be already locked.
|
---|
| 179 | *
|
---|
| 180 | * @param area Pointer to the address space area.
|
---|
[59fb782] | 181 | * @param upage Faulting virtual page.
|
---|
[0ee077ee] | 182 | * @param access Access mode that caused the fault (i.e. read/write/exec).
|
---|
| 183 | *
|
---|
[d5bd8d7] | 184 | * @return AS_PF_FAULT on failure (i.e. page fault) or AS_PF_OK on success (i.e.
|
---|
| 185 | * serviced).
|
---|
[0ee077ee] | 186 | */
|
---|
[59fb782] | 187 | int anon_page_fault(as_area_t *area, uintptr_t upage, pf_access_t access)
|
---|
[0ee077ee] | 188 | {
|
---|
[c142860] | 189 | uintptr_t kpage;
|
---|
[7f1c620] | 190 | uintptr_t frame;
|
---|
[0ee077ee] | 191 |
|
---|
[1d432f9] | 192 | ASSERT(page_table_locked(AS));
|
---|
| 193 | ASSERT(mutex_locked(&area->lock));
|
---|
[59fb782] | 194 | ASSERT(IS_ALIGNED(upage, PAGE_SIZE));
|
---|
[1d432f9] | 195 |
|
---|
[0ee077ee] | 196 | if (!as_area_check_access(area, access))
|
---|
| 197 | return AS_PF_FAULT;
|
---|
| 198 |
|
---|
[83b6ba9f] | 199 | mutex_lock(&area->sh_info->lock);
|
---|
| 200 | if (area->sh_info->shared) {
|
---|
[0ee077ee] | 201 | btree_node_t *leaf;
|
---|
| 202 |
|
---|
| 203 | /*
|
---|
| 204 | * The area is shared, chances are that the mapping can be found
|
---|
[d5bd8d7] | 205 | * in the pagemap of the address space area share info
|
---|
| 206 | * structure.
|
---|
[0ee077ee] | 207 | * In the case that the pagemap does not contain the respective
|
---|
| 208 | * mapping, a new frame is allocated and the mapping is created.
|
---|
| 209 | */
|
---|
[7f1c620] | 210 | frame = (uintptr_t) btree_search(&area->sh_info->pagemap,
|
---|
[c142860] | 211 | upage - area->base, &leaf);
|
---|
[0ee077ee] | 212 | if (!frame) {
|
---|
| 213 | bool allocate = true;
|
---|
[6c441cf8] | 214 | unsigned int i;
|
---|
[0ee077ee] | 215 |
|
---|
| 216 | /*
|
---|
| 217 | * Zero can be returned as a valid frame address.
|
---|
| 218 | * Just a small workaround.
|
---|
| 219 | */
|
---|
| 220 | for (i = 0; i < leaf->keys; i++) {
|
---|
[c142860] | 221 | if (leaf->key[i] == upage - area->base) {
|
---|
[0ee077ee] | 222 | allocate = false;
|
---|
| 223 | break;
|
---|
| 224 | }
|
---|
| 225 | }
|
---|
| 226 | if (allocate) {
|
---|
[c142860] | 227 | kpage = km_temporary_page_get(&frame,
|
---|
| 228 | FRAME_NO_RESERVE);
|
---|
| 229 | memsetb((void *) kpage, PAGE_SIZE, 0);
|
---|
| 230 | km_temporary_page_put(kpage);
|
---|
[0ee077ee] | 231 |
|
---|
| 232 | /*
|
---|
[d5bd8d7] | 233 | * Insert the address of the newly allocated
|
---|
| 234 | * frame to the pagemap.
|
---|
[0ee077ee] | 235 | */
|
---|
[d5bd8d7] | 236 | btree_insert(&area->sh_info->pagemap,
|
---|
[c142860] | 237 | upage - area->base, (void *) frame, leaf);
|
---|
[0ee077ee] | 238 | }
|
---|
| 239 | }
|
---|
[f9b2f305] | 240 | frame_reference_add(ADDR2PFN(frame));
|
---|
[0ee077ee] | 241 | } else {
|
---|
| 242 |
|
---|
| 243 | /*
|
---|
| 244 | * In general, there can be several reasons that
|
---|
| 245 | * can have caused this fault.
|
---|
| 246 | *
|
---|
| 247 | * - non-existent mapping: the area is an anonymous
|
---|
| 248 | * area (e.g. heap or stack) and so far has not been
|
---|
| 249 | * allocated a frame for the faulting page
|
---|
| 250 | *
|
---|
| 251 | * - non-present mapping: another possibility,
|
---|
| 252 | * currently not implemented, would be frame
|
---|
| 253 | * reuse; when this becomes a possibility,
|
---|
| 254 | * do not forget to distinguish between
|
---|
| 255 | * the different causes
|
---|
| 256 | */
|
---|
[692bd3f2] | 257 |
|
---|
[5892ec1] | 258 | if (area->flags & AS_AREA_LATE_RESERVE) {
|
---|
[692bd3f2] | 259 | /*
|
---|
[9a9c805] | 260 | * Reserve the memory for this page now.
|
---|
[692bd3f2] | 261 | */
|
---|
[83b6ba9f] | 262 | if (!reserve_try_alloc(1)) {
|
---|
| 263 | mutex_unlock(&area->sh_info->lock);
|
---|
[908bb96] | 264 | return AS_PF_SILENT;
|
---|
[83b6ba9f] | 265 | }
|
---|
[692bd3f2] | 266 | }
|
---|
| 267 |
|
---|
[9a9c805] | 268 | kpage = km_temporary_page_get(&frame, FRAME_NO_RESERVE);
|
---|
[c142860] | 269 | memsetb((void *) kpage, PAGE_SIZE, 0);
|
---|
| 270 | km_temporary_page_put(kpage);
|
---|
[0ee077ee] | 271 | }
|
---|
[83b6ba9f] | 272 | mutex_unlock(&area->sh_info->lock);
|
---|
[0ee077ee] | 273 |
|
---|
| 274 | /*
|
---|
[c142860] | 275 | * Map 'upage' to 'frame'.
|
---|
[d5bd8d7] | 276 | * Note that TLB shootdown is not attempted as only new information is
|
---|
| 277 | * being inserted into page tables.
|
---|
[0ee077ee] | 278 | */
|
---|
[c142860] | 279 | page_mapping_insert(AS, upage, frame, as_area_get_flags(area));
|
---|
| 280 | if (!used_space_insert(area, upage, 1))
|
---|
[f651e80] | 281 | panic("Cannot insert used space.");
|
---|
[0ee077ee] | 282 |
|
---|
| 283 | return AS_PF_OK;
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | /** Free a frame that is backed by the anonymous memory backend.
|
---|
| 287 | *
|
---|
| 288 | * The address space area and page tables must be already locked.
|
---|
| 289 | *
|
---|
| 290 | * @param area Ignored.
|
---|
[9f63a83] | 291 | * @param page Virtual address of the page corresponding to the frame.
|
---|
[0ee077ee] | 292 | * @param frame Frame to be released.
|
---|
| 293 | */
|
---|
[7f1c620] | 294 | void anon_frame_free(as_area_t *area, uintptr_t page, uintptr_t frame)
|
---|
[0ee077ee] | 295 | {
|
---|
[1d432f9] | 296 | ASSERT(page_table_locked(area->as));
|
---|
| 297 | ASSERT(mutex_locked(&area->lock));
|
---|
| 298 |
|
---|
[5892ec1] | 299 | if (area->flags & AS_AREA_LATE_RESERVE) {
|
---|
[692bd3f2] | 300 | /*
|
---|
[5892ec1] | 301 | * In case of the late reserve areas, physical memory will not
|
---|
| 302 | * be unreserved when the area is destroyed so we need to use
|
---|
| 303 | * the normal unreserving frame_free().
|
---|
[692bd3f2] | 304 | */
|
---|
[5df1963] | 305 | frame_free(frame, 1);
|
---|
[692bd3f2] | 306 | } else {
|
---|
| 307 | /*
|
---|
| 308 | * The reserve will be given back when the area is destroyed or
|
---|
| 309 | * resized, so use the frame_free_noreserve() which does not
|
---|
| 310 | * manipulate the reserve or it would be given back twice.
|
---|
| 311 | */
|
---|
[5df1963] | 312 | frame_free_noreserve(frame, 1);
|
---|
[692bd3f2] | 313 | }
|
---|
[0ee077ee] | 314 | }
|
---|
| 315 |
|
---|
[cc73a8a1] | 316 | /** @}
|
---|
[b45c443] | 317 | */
|
---|