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