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