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