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 <mm/frame.h>
|
---|
42 | #include <abi/mm/as.h>
|
---|
43 | #include <abi/ipc/methods.h>
|
---|
44 | #include <ipc/sysipc.h>
|
---|
45 | #include <synch/mutex.h>
|
---|
46 | #include <typedefs.h>
|
---|
47 | #include <align.h>
|
---|
48 | #include <assert.h>
|
---|
49 | #include <errno.h>
|
---|
50 | #include <log.h>
|
---|
51 |
|
---|
52 | static bool user_create(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 = NULL,
|
---|
64 | .share = NULL,
|
---|
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 | void user_destroy(as_area_t *area)
|
---|
83 | {
|
---|
84 | return;
|
---|
85 | }
|
---|
86 |
|
---|
87 | bool user_is_resizable(as_area_t *area)
|
---|
88 | {
|
---|
89 | return false;
|
---|
90 | }
|
---|
91 |
|
---|
92 | bool user_is_shareable(as_area_t *area)
|
---|
93 | {
|
---|
94 | return false;
|
---|
95 | }
|
---|
96 |
|
---|
97 | /** Service a page fault in the user-paged address space area.
|
---|
98 | *
|
---|
99 | * The address space area and page tables must be already locked.
|
---|
100 | *
|
---|
101 | * @param area Pointer to the address space area.
|
---|
102 | * @param upage Faulting virtual page.
|
---|
103 | * @param access Access mode that caused the fault (i.e. read/write/exec).
|
---|
104 | *
|
---|
105 | * @return AS_PF_FAULT on failure (i.e. page fault) or AS_PF_OK on success (i.e.
|
---|
106 | * serviced).
|
---|
107 | */
|
---|
108 | int user_page_fault(as_area_t *area, uintptr_t upage, pf_access_t access)
|
---|
109 | {
|
---|
110 | assert(page_table_locked(AS));
|
---|
111 | assert(mutex_locked(&area->lock));
|
---|
112 | assert(IS_ALIGNED(upage, PAGE_SIZE));
|
---|
113 |
|
---|
114 | if (!as_area_check_access(area, access))
|
---|
115 | return AS_PF_FAULT;
|
---|
116 |
|
---|
117 | as_area_pager_info_t *pager_info = &area->backend_data.pager_info;
|
---|
118 |
|
---|
119 | ipc_data_t data = {};
|
---|
120 | IPC_SET_IMETHOD(data, IPC_M_PAGE_IN);
|
---|
121 | IPC_SET_ARG1(data, upage - area->base);
|
---|
122 | IPC_SET_ARG2(data, PAGE_SIZE);
|
---|
123 | IPC_SET_ARG3(data, pager_info->id1);
|
---|
124 | IPC_SET_ARG4(data, pager_info->id2);
|
---|
125 | IPC_SET_ARG5(data, pager_info->id3);
|
---|
126 |
|
---|
127 | int rc = ipc_req_internal(pager_info->pager, &data, (sysarg_t) true);
|
---|
128 |
|
---|
129 | if (rc != EOK) {
|
---|
130 | log(LF_USPACE, LVL_FATAL,
|
---|
131 | "Page-in request for page %#" PRIxn
|
---|
132 | " at pager %d failed with error %d.",
|
---|
133 | upage, pager_info->pager, rc);
|
---|
134 | return AS_PF_FAULT;
|
---|
135 | }
|
---|
136 |
|
---|
137 | if (IPC_GET_RETVAL(data) != EOK)
|
---|
138 | return AS_PF_FAULT;
|
---|
139 |
|
---|
140 | /*
|
---|
141 | * A successful reply will contain the physical frame in ARG1.
|
---|
142 | * The physical frame will have the reference count already
|
---|
143 | * incremented (if applicable).
|
---|
144 | */
|
---|
145 |
|
---|
146 | uintptr_t frame = IPC_GET_ARG1(data);
|
---|
147 | page_mapping_insert(AS, upage, frame, as_area_get_flags(area));
|
---|
148 | if (!used_space_insert(area, upage, 1))
|
---|
149 | panic("Cannot insert used space.");
|
---|
150 |
|
---|
151 | return AS_PF_OK;
|
---|
152 | }
|
---|
153 |
|
---|
154 | /** Free a frame that is backed by the user memory backend.
|
---|
155 | *
|
---|
156 | * The address space area and page tables must be already locked.
|
---|
157 | *
|
---|
158 | * @param area Ignored.
|
---|
159 | * @param page Virtual address of the page corresponding to the frame.
|
---|
160 | * @param frame Frame to be released.
|
---|
161 | */
|
---|
162 | void user_frame_free(as_area_t *area, uintptr_t page, uintptr_t frame)
|
---|
163 | {
|
---|
164 | assert(page_table_locked(area->as));
|
---|
165 | assert(mutex_locked(&area->lock));
|
---|
166 |
|
---|
167 | pfn_t pfn = ADDR2PFN(frame);
|
---|
168 | if (find_zone(pfn, 1, 0) != (size_t) -1) {
|
---|
169 | frame_free(frame, 1);
|
---|
170 | } else {
|
---|
171 | /* Nothing to do */
|
---|
172 | }
|
---|
173 |
|
---|
174 | }
|
---|
175 |
|
---|
176 | /** @}
|
---|
177 | */
|
---|