[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 |
|
---|
[174156fd] | 29 | /** @addtogroup kernel_generic_mm
|
---|
[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 <errno.h>
|
---|
[d99c1d2] | 51 | #include <typedefs.h>
|
---|
[0ee077ee] | 52 | #include <align.h>
|
---|
[44a7ee5] | 53 | #include <mem.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 | {
|
---|
[63e27ef] | 115 | assert(mutex_locked(&area->as->lock));
|
---|
| 116 | assert(mutex_locked(&area->lock));
|
---|
| 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);
|
---|
[7be8d4d] | 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));
|
---|
[03523dc] | 141 |
|
---|
[7be8d4d] | 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);
|
---|
[03523dc] | 148 | }
|
---|
[7be8d4d] | 149 |
|
---|
| 150 | ival = used_space_next(ival);
|
---|
[03523dc] | 151 | }
|
---|
| 152 | mutex_unlock(&area->sh_info->lock);
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | void anon_destroy(as_area_t *area)
|
---|
| 156 | {
|
---|
[5892ec1] | 157 | if (area->flags & AS_AREA_LATE_RESERVE)
|
---|
[692bd3f2] | 158 | return;
|
---|
| 159 |
|
---|
[03523dc] | 160 | reserve_free(area->pages);
|
---|
| 161 | }
|
---|
| 162 |
|
---|
[01029fc] | 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 | }
|
---|
[03523dc] | 172 |
|
---|
[0ee077ee] | 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.
|
---|
[59fb782] | 178 | * @param upage Faulting virtual page.
|
---|
[0ee077ee] | 179 | * @param access Access mode that caused the fault (i.e. read/write/exec).
|
---|
| 180 | *
|
---|
[d5bd8d7] | 181 | * @return AS_PF_FAULT on failure (i.e. page fault) or AS_PF_OK on success (i.e.
|
---|
| 182 | * serviced).
|
---|
[0ee077ee] | 183 | */
|
---|
[59fb782] | 184 | int anon_page_fault(as_area_t *area, uintptr_t upage, pf_access_t access)
|
---|
[0ee077ee] | 185 | {
|
---|
[c142860] | 186 | uintptr_t kpage;
|
---|
[7f1c620] | 187 | uintptr_t frame;
|
---|
[0ee077ee] | 188 |
|
---|
[63e27ef] | 189 | assert(page_table_locked(AS));
|
---|
| 190 | assert(mutex_locked(&area->lock));
|
---|
| 191 | assert(IS_ALIGNED(upage, PAGE_SIZE));
|
---|
[1d432f9] | 192 |
|
---|
[0ee077ee] | 193 | if (!as_area_check_access(area, access))
|
---|
| 194 | return AS_PF_FAULT;
|
---|
| 195 |
|
---|
[83b6ba9f] | 196 | mutex_lock(&area->sh_info->lock);
|
---|
| 197 | if (area->sh_info->shared) {
|
---|
[0ee077ee] | 198 | /*
|
---|
| 199 | * The area is shared, chances are that the mapping can be found
|
---|
[d5bd8d7] | 200 | * in the pagemap of the address space area share info
|
---|
| 201 | * structure.
|
---|
[0ee077ee] | 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 | */
|
---|
[de0af3a] | 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);
|
---|
[a35b458] | 213 |
|
---|
[0ee077ee] | 214 | /*
|
---|
[de0af3a] | 215 | * Insert the address of the newly allocated
|
---|
| 216 | * frame to the pagemap.
|
---|
[0ee077ee] | 217 | */
|
---|
[de0af3a] | 218 | as_pagemap_insert(&area->sh_info->pagemap,
|
---|
| 219 | upage - area->base, frame);
|
---|
[0ee077ee] | 220 | }
|
---|
[f9b2f305] | 221 | frame_reference_add(ADDR2PFN(frame));
|
---|
[0ee077ee] | 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 | */
|
---|
[692bd3f2] | 238 |
|
---|
[5892ec1] | 239 | if (area->flags & AS_AREA_LATE_RESERVE) {
|
---|
[692bd3f2] | 240 | /*
|
---|
[9a9c805] | 241 | * Reserve the memory for this page now.
|
---|
[692bd3f2] | 242 | */
|
---|
[83b6ba9f] | 243 | if (!reserve_try_alloc(1)) {
|
---|
| 244 | mutex_unlock(&area->sh_info->lock);
|
---|
[908bb96] | 245 | return AS_PF_SILENT;
|
---|
[83b6ba9f] | 246 | }
|
---|
[692bd3f2] | 247 | }
|
---|
| 248 |
|
---|
[9a9c805] | 249 | kpage = km_temporary_page_get(&frame, FRAME_NO_RESERVE);
|
---|
[c142860] | 250 | memsetb((void *) kpage, PAGE_SIZE, 0);
|
---|
| 251 | km_temporary_page_put(kpage);
|
---|
[0ee077ee] | 252 | }
|
---|
[83b6ba9f] | 253 | mutex_unlock(&area->sh_info->lock);
|
---|
[a35b458] | 254 |
|
---|
[0ee077ee] | 255 | /*
|
---|
[c142860] | 256 | * Map 'upage' to 'frame'.
|
---|
[d5bd8d7] | 257 | * Note that TLB shootdown is not attempted as only new information is
|
---|
| 258 | * being inserted into page tables.
|
---|
[0ee077ee] | 259 | */
|
---|
[c142860] | 260 | page_mapping_insert(AS, upage, frame, as_area_get_flags(area));
|
---|
[7be8d4d] | 261 | if (!used_space_insert(&area->used_space, upage, 1))
|
---|
[f651e80] | 262 | panic("Cannot insert used space.");
|
---|
[a35b458] | 263 |
|
---|
[0ee077ee] | 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.
|
---|
[9f63a83] | 272 | * @param page Virtual address of the page corresponding to the frame.
|
---|
[0ee077ee] | 273 | * @param frame Frame to be released.
|
---|
| 274 | */
|
---|
[7f1c620] | 275 | void anon_frame_free(as_area_t *area, uintptr_t page, uintptr_t frame)
|
---|
[0ee077ee] | 276 | {
|
---|
[63e27ef] | 277 | assert(page_table_locked(area->as));
|
---|
| 278 | assert(mutex_locked(&area->lock));
|
---|
[1d432f9] | 279 |
|
---|
[5892ec1] | 280 | if (area->flags & AS_AREA_LATE_RESERVE) {
|
---|
[692bd3f2] | 281 | /*
|
---|
[5892ec1] | 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().
|
---|
[692bd3f2] | 285 | */
|
---|
[5df1963] | 286 | frame_free(frame, 1);
|
---|
[692bd3f2] | 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 | */
|
---|
[5df1963] | 293 | frame_free_noreserve(frame, 1);
|
---|
[692bd3f2] | 294 | }
|
---|
[0ee077ee] | 295 | }
|
---|
| 296 |
|
---|
[cc73a8a1] | 297 | /** @}
|
---|
[b45c443] | 298 | */
|
---|