| 1 | /*
|
|---|
| 2 | * Copyright (c) 2016 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 address space areas backed by the user pager.
|
|---|
| 36 | *
|
|---|
| 37 | */
|
|---|
| 38 |
|
|---|
| 39 | #include <mm/as.h>
|
|---|
| 40 | #include <mm/page.h>
|
|---|
| 41 | #include <abi/ipc/methods.h>
|
|---|
| 42 | #include <ipc/sysipc.h>
|
|---|
| 43 | #include <synch/mutex.h>
|
|---|
| 44 | #include <typedefs.h>
|
|---|
| 45 | #include <align.h>
|
|---|
| 46 | #include <debug.h>
|
|---|
| 47 | #include <errno.h>
|
|---|
| 48 | #include <log.h>
|
|---|
| 49 |
|
|---|
| 50 | static bool user_create(as_area_t *);
|
|---|
| 51 | static bool user_resize(as_area_t *, size_t);
|
|---|
| 52 | static void user_share(as_area_t *);
|
|---|
| 53 | static void user_destroy(as_area_t *);
|
|---|
| 54 |
|
|---|
| 55 | static bool user_is_resizable(as_area_t *);
|
|---|
| 56 | static bool user_is_shareable(as_area_t *);
|
|---|
| 57 |
|
|---|
| 58 | static int user_page_fault(as_area_t *, uintptr_t, pf_access_t);
|
|---|
| 59 | static void user_frame_free(as_area_t *, uintptr_t, uintptr_t);
|
|---|
| 60 |
|
|---|
| 61 | mem_backend_t user_backend = {
|
|---|
| 62 | .create = user_create,
|
|---|
| 63 | .resize = user_resize,
|
|---|
| 64 | .share = user_share,
|
|---|
| 65 | .destroy = user_destroy,
|
|---|
| 66 |
|
|---|
| 67 | .is_resizable = user_is_resizable,
|
|---|
| 68 | .is_shareable = user_is_shareable,
|
|---|
| 69 |
|
|---|
| 70 | .page_fault = user_page_fault,
|
|---|
| 71 | .frame_free = user_frame_free,
|
|---|
| 72 |
|
|---|
| 73 | .create_shared_data = NULL,
|
|---|
| 74 | .destroy_shared_data = NULL
|
|---|
| 75 | };
|
|---|
| 76 |
|
|---|
| 77 | bool user_create(as_area_t *area)
|
|---|
| 78 | {
|
|---|
| 79 | return true;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | bool user_resize(as_area_t *area, size_t new_pages)
|
|---|
| 83 | {
|
|---|
| 84 | return true;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | /** Share the user-paged address space area.
|
|---|
| 88 | *
|
|---|
| 89 | * The address space and address space area must be already locked.
|
|---|
| 90 | *
|
|---|
| 91 | * @param area Address space area to be shared.
|
|---|
| 92 | */
|
|---|
| 93 | void user_share(as_area_t *area)
|
|---|
| 94 | {
|
|---|
| 95 | ASSERT(mutex_locked(&area->as->lock));
|
|---|
| 96 | ASSERT(mutex_locked(&area->lock));
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | void user_destroy(as_area_t *area)
|
|---|
| 100 | {
|
|---|
| 101 | return;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | bool user_is_resizable(as_area_t *area)
|
|---|
| 105 | {
|
|---|
| 106 | return true;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | bool user_is_shareable(as_area_t *area)
|
|---|
| 110 | {
|
|---|
| 111 | return true;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | /** Service a page fault in the user-paged address space area.
|
|---|
| 115 | *
|
|---|
| 116 | * The address space area and page tables must be already locked.
|
|---|
| 117 | *
|
|---|
| 118 | * @param area Pointer to the address space area.
|
|---|
| 119 | * @param upage Faulting virtual page.
|
|---|
| 120 | * @param access Access mode that caused the fault (i.e. read/write/exec).
|
|---|
| 121 | *
|
|---|
| 122 | * @return AS_PF_FAULT on failure (i.e. page fault) or AS_PF_OK on success (i.e.
|
|---|
| 123 | * serviced).
|
|---|
| 124 | */
|
|---|
| 125 | int user_page_fault(as_area_t *area, uintptr_t upage, pf_access_t access)
|
|---|
| 126 | {
|
|---|
| 127 | ASSERT(page_table_locked(AS));
|
|---|
| 128 | ASSERT(mutex_locked(&area->lock));
|
|---|
| 129 | ASSERT(IS_ALIGNED(upage, PAGE_SIZE));
|
|---|
| 130 |
|
|---|
| 131 | if (!as_area_check_access(area, access))
|
|---|
| 132 | return AS_PF_FAULT;
|
|---|
| 133 |
|
|---|
| 134 | ipc_data_t data = {};
|
|---|
| 135 | IPC_SET_IMETHOD(data, IPC_M_PAGE_IN);
|
|---|
| 136 | IPC_SET_ARG1(data, upage - area->base);
|
|---|
| 137 | IPC_SET_ARG2(data, PAGE_SIZE);
|
|---|
| 138 |
|
|---|
| 139 | int rc = ipc_req_internal(area->backend_data.pager, &data);
|
|---|
| 140 |
|
|---|
| 141 | if (rc != EOK) {
|
|---|
| 142 | log(LF_USPACE, LVL_FATAL,
|
|---|
| 143 | "Page-in request for page %#" PRIxn
|
|---|
| 144 | " at pager %d failed with error %d.",
|
|---|
| 145 | upage, area->backend_data.pager, rc);
|
|---|
| 146 | return AS_PF_FAULT;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | if (IPC_GET_RETVAL(data) != EOK)
|
|---|
| 150 | return AS_PF_FAULT;
|
|---|
| 151 |
|
|---|
| 152 | /*
|
|---|
| 153 | * A successful reply will contain the physical frame in ARG1.
|
|---|
| 154 | * The physical frame will have the reference count already
|
|---|
| 155 | * incremented (if applicable).
|
|---|
| 156 | */
|
|---|
| 157 |
|
|---|
| 158 | uintptr_t frame = IPC_GET_ARG1(data);
|
|---|
| 159 | page_mapping_insert(AS, upage, frame, as_area_get_flags(area));
|
|---|
| 160 | if (!used_space_insert(area, upage, 1))
|
|---|
| 161 | panic("Cannot insert used space.");
|
|---|
| 162 |
|
|---|
| 163 | return AS_PF_OK;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | /** Free a frame that is backed by the user memory backend.
|
|---|
| 167 | *
|
|---|
| 168 | * The address space area and page tables must be already locked.
|
|---|
| 169 | *
|
|---|
| 170 | * @param area Ignored.
|
|---|
| 171 | * @param page Virtual address of the page corresponding to the frame.
|
|---|
| 172 | * @param frame Frame to be released.
|
|---|
| 173 | */
|
|---|
| 174 | void user_frame_free(as_area_t *area, uintptr_t page, uintptr_t frame)
|
|---|
| 175 | {
|
|---|
| 176 | ASSERT(page_table_locked(area->as));
|
|---|
| 177 | ASSERT(mutex_locked(&area->lock));
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | /** @}
|
|---|
| 181 | */
|
|---|