| 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 kernel_generic_mm
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| 33 | /**
|
|---|
| 34 | * @file
|
|---|
| 35 | * @brief Backend for anonymous memory address space areas.
|
|---|
| 36 | *
|
|---|
| 37 | */
|
|---|
| 38 |
|
|---|
| 39 | #include <assert.h>
|
|---|
| 40 | #include <mm/as.h>
|
|---|
| 41 | #include <mm/page.h>
|
|---|
| 42 | #include <mm/reserve.h>
|
|---|
| 43 | #include <genarch/mm/page_pt.h>
|
|---|
| 44 | #include <genarch/mm/page_ht.h>
|
|---|
| 45 | #include <mm/frame.h>
|
|---|
| 46 | #include <mm/slab.h>
|
|---|
| 47 | #include <mm/km.h>
|
|---|
| 48 | #include <synch/mutex.h>
|
|---|
| 49 | #include <adt/list.h>
|
|---|
| 50 | #include <errno.h>
|
|---|
| 51 | #include <typedefs.h>
|
|---|
| 52 | #include <align.h>
|
|---|
| 53 | #include <mem.h>
|
|---|
| 54 | #include <arch.h>
|
|---|
| 55 |
|
|---|
| 56 | static bool anon_create(as_area_t *);
|
|---|
| 57 | static bool anon_resize(as_area_t *, size_t);
|
|---|
| 58 | static void anon_share(as_area_t *);
|
|---|
| 59 | static void anon_destroy(as_area_t *);
|
|---|
| 60 |
|
|---|
| 61 | static bool anon_is_resizable(as_area_t *);
|
|---|
| 62 | static bool anon_is_shareable(as_area_t *);
|
|---|
| 63 |
|
|---|
| 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);
|
|---|
| 66 |
|
|---|
| 67 | mem_backend_t anon_backend = {
|
|---|
| 68 | .create = anon_create,
|
|---|
| 69 | .resize = anon_resize,
|
|---|
| 70 | .share = anon_share,
|
|---|
| 71 | .destroy = anon_destroy,
|
|---|
| 72 |
|
|---|
| 73 | .is_resizable = anon_is_resizable,
|
|---|
| 74 | .is_shareable = anon_is_shareable,
|
|---|
| 75 |
|
|---|
| 76 | .page_fault = anon_page_fault,
|
|---|
| 77 | .frame_free = anon_frame_free,
|
|---|
| 78 |
|
|---|
| 79 | .create_shared_data = NULL,
|
|---|
| 80 | .destroy_shared_data = NULL
|
|---|
| 81 | };
|
|---|
| 82 |
|
|---|
| 83 | bool anon_create(as_area_t *area)
|
|---|
| 84 | {
|
|---|
| 85 | if (area->flags & AS_AREA_LATE_RESERVE)
|
|---|
| 86 | return true;
|
|---|
| 87 |
|
|---|
| 88 | return reserve_try_alloc(area->pages);
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | bool anon_resize(as_area_t *area, size_t new_pages)
|
|---|
| 92 | {
|
|---|
| 93 | if (area->flags & AS_AREA_LATE_RESERVE)
|
|---|
| 94 | return true;
|
|---|
| 95 |
|
|---|
| 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));
|
|---|
| 117 | assert(!(area->flags & AS_AREA_LATE_RESERVE));
|
|---|
| 118 |
|
|---|
| 119 | /*
|
|---|
| 120 | * Copy used portions of the area to sh_info's page map.
|
|---|
| 121 | */
|
|---|
| 122 | mutex_lock(&area->sh_info->lock);
|
|---|
| 123 | used_space_ival_t *ival = used_space_first(&area->used_space);
|
|---|
| 124 | while (ival != NULL) {
|
|---|
| 125 | uintptr_t base = ival->page;
|
|---|
| 126 | size_t count = ival->count;
|
|---|
| 127 | unsigned int j;
|
|---|
| 128 |
|
|---|
| 129 | for (j = 0; j < count; j++) {
|
|---|
| 130 | pte_t pte;
|
|---|
| 131 | bool found;
|
|---|
| 132 |
|
|---|
| 133 | page_table_lock(area->as, false);
|
|---|
| 134 | found = page_mapping_find(area->as, base + P2SZ(j),
|
|---|
| 135 | false, &pte);
|
|---|
| 136 |
|
|---|
| 137 | (void)found;
|
|---|
| 138 | assert(found);
|
|---|
| 139 | assert(PTE_VALID(&pte));
|
|---|
| 140 | assert(PTE_PRESENT(&pte));
|
|---|
| 141 |
|
|---|
| 142 | as_pagemap_insert(&area->sh_info->pagemap,
|
|---|
| 143 | (base + P2SZ(j)) - area->base, PTE_GET_FRAME(&pte));
|
|---|
| 144 | page_table_unlock(area->as, false);
|
|---|
| 145 |
|
|---|
| 146 | pfn_t pfn = ADDR2PFN(PTE_GET_FRAME(&pte));
|
|---|
| 147 | frame_reference_add(pfn);
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | ival = used_space_next(ival);
|
|---|
| 151 | }
|
|---|
| 152 | mutex_unlock(&area->sh_info->lock);
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | void anon_destroy(as_area_t *area)
|
|---|
| 156 | {
|
|---|
| 157 | if (area->flags & AS_AREA_LATE_RESERVE)
|
|---|
| 158 | return;
|
|---|
| 159 |
|
|---|
| 160 | reserve_free(area->pages);
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | bool anon_is_resizable(as_area_t *area)
|
|---|
| 164 | {
|
|---|
| 165 | return true;
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | bool anon_is_shareable(as_area_t *area)
|
|---|
| 169 | {
|
|---|
| 170 | return !(area->flags & AS_AREA_LATE_RESERVE);
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | /** Service a page fault in the anonymous memory address space area.
|
|---|
| 174 | *
|
|---|
| 175 | * The address space area and page tables must be already locked.
|
|---|
| 176 | *
|
|---|
| 177 | * @param area Pointer to the address space area.
|
|---|
| 178 | * @param upage Faulting virtual page.
|
|---|
| 179 | * @param access Access mode that caused the fault (i.e. read/write/exec).
|
|---|
| 180 | *
|
|---|
| 181 | * @return AS_PF_FAULT on failure (i.e. page fault) or AS_PF_OK on success (i.e.
|
|---|
| 182 | * serviced).
|
|---|
| 183 | */
|
|---|
| 184 | int anon_page_fault(as_area_t *area, uintptr_t upage, pf_access_t access)
|
|---|
| 185 | {
|
|---|
| 186 | uintptr_t kpage;
|
|---|
| 187 | uintptr_t frame;
|
|---|
| 188 |
|
|---|
| 189 | assert(page_table_locked(AS));
|
|---|
| 190 | assert(mutex_locked(&area->lock));
|
|---|
| 191 | assert(IS_ALIGNED(upage, PAGE_SIZE));
|
|---|
| 192 |
|
|---|
| 193 | if (!as_area_check_access(area, access))
|
|---|
| 194 | return AS_PF_FAULT;
|
|---|
| 195 |
|
|---|
| 196 | mutex_lock(&area->sh_info->lock);
|
|---|
| 197 | if (area->sh_info->shared) {
|
|---|
| 198 | /*
|
|---|
| 199 | * The area is shared, chances are that the mapping can be found
|
|---|
| 200 | * in the pagemap of the address space area share info
|
|---|
| 201 | * structure.
|
|---|
| 202 | * In the case that the pagemap does not contain the respective
|
|---|
| 203 | * mapping, a new frame is allocated and the mapping is created.
|
|---|
| 204 | */
|
|---|
| 205 | errno_t rc = as_pagemap_find(&area->sh_info->pagemap,
|
|---|
| 206 | upage - area->base, &frame);
|
|---|
| 207 | if (rc != EOK) {
|
|---|
| 208 | /* Need to allocate the frame */
|
|---|
| 209 | kpage = km_temporary_page_get(&frame,
|
|---|
| 210 | FRAME_NO_RESERVE);
|
|---|
| 211 | memsetb((void *) kpage, PAGE_SIZE, 0);
|
|---|
| 212 | km_temporary_page_put(kpage);
|
|---|
| 213 |
|
|---|
| 214 | /*
|
|---|
| 215 | * Insert the address of the newly allocated
|
|---|
| 216 | * frame to the pagemap.
|
|---|
| 217 | */
|
|---|
| 218 | as_pagemap_insert(&area->sh_info->pagemap,
|
|---|
| 219 | upage - area->base, frame);
|
|---|
| 220 | }
|
|---|
| 221 | frame_reference_add(ADDR2PFN(frame));
|
|---|
| 222 | } else {
|
|---|
| 223 |
|
|---|
| 224 | /*
|
|---|
| 225 | * In general, there can be several reasons that
|
|---|
| 226 | * can have caused this fault.
|
|---|
| 227 | *
|
|---|
| 228 | * - non-existent mapping: the area is an anonymous
|
|---|
| 229 | * area (e.g. heap or stack) and so far has not been
|
|---|
| 230 | * allocated a frame for the faulting page
|
|---|
| 231 | *
|
|---|
| 232 | * - non-present mapping: another possibility,
|
|---|
| 233 | * currently not implemented, would be frame
|
|---|
| 234 | * reuse; when this becomes a possibility,
|
|---|
| 235 | * do not forget to distinguish between
|
|---|
| 236 | * the different causes
|
|---|
| 237 | */
|
|---|
| 238 |
|
|---|
| 239 | if (area->flags & AS_AREA_LATE_RESERVE) {
|
|---|
| 240 | /*
|
|---|
| 241 | * Reserve the memory for this page now.
|
|---|
| 242 | */
|
|---|
| 243 | if (!reserve_try_alloc(1)) {
|
|---|
| 244 | mutex_unlock(&area->sh_info->lock);
|
|---|
| 245 | return AS_PF_SILENT;
|
|---|
| 246 | }
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 | kpage = km_temporary_page_get(&frame, FRAME_NO_RESERVE);
|
|---|
| 250 | memsetb((void *) kpage, PAGE_SIZE, 0);
|
|---|
| 251 | km_temporary_page_put(kpage);
|
|---|
| 252 | }
|
|---|
| 253 | mutex_unlock(&area->sh_info->lock);
|
|---|
| 254 |
|
|---|
| 255 | /*
|
|---|
| 256 | * Map 'upage' to 'frame'.
|
|---|
| 257 | * Note that TLB shootdown is not attempted as only new information is
|
|---|
| 258 | * being inserted into page tables.
|
|---|
| 259 | */
|
|---|
| 260 | page_mapping_insert(AS, upage, frame, as_area_get_flags(area));
|
|---|
| 261 | if (!used_space_insert(&area->used_space, upage, 1))
|
|---|
| 262 | panic("Cannot insert used space.");
|
|---|
| 263 |
|
|---|
| 264 | return AS_PF_OK;
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | /** Free a frame that is backed by the anonymous memory backend.
|
|---|
| 268 | *
|
|---|
| 269 | * The address space area and page tables must be already locked.
|
|---|
| 270 | *
|
|---|
| 271 | * @param area Ignored.
|
|---|
| 272 | * @param page Virtual address of the page corresponding to the frame.
|
|---|
| 273 | * @param frame Frame to be released.
|
|---|
| 274 | */
|
|---|
| 275 | void anon_frame_free(as_area_t *area, uintptr_t page, uintptr_t frame)
|
|---|
| 276 | {
|
|---|
| 277 | assert(page_table_locked(area->as));
|
|---|
| 278 | assert(mutex_locked(&area->lock));
|
|---|
| 279 |
|
|---|
| 280 | if (area->flags & AS_AREA_LATE_RESERVE) {
|
|---|
| 281 | /*
|
|---|
| 282 | * In case of the late reserve areas, physical memory will not
|
|---|
| 283 | * be unreserved when the area is destroyed so we need to use
|
|---|
| 284 | * the normal unreserving frame_free().
|
|---|
| 285 | */
|
|---|
| 286 | frame_free(frame, 1);
|
|---|
| 287 | } else {
|
|---|
| 288 | /*
|
|---|
| 289 | * The reserve will be given back when the area is destroyed or
|
|---|
| 290 | * resized, so use the frame_free_noreserve() which does not
|
|---|
| 291 | * manipulate the reserve or it would be given back twice.
|
|---|
| 292 | */
|
|---|
| 293 | frame_free_noreserve(frame, 1);
|
|---|
| 294 | }
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 | /** @}
|
|---|
| 298 | */
|
|---|