source: mainline/kernel/generic/src/mm/backend_user.c@ 62ca560

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 62ca560 was 62ca560, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Add str_error() and str_error_name() to kernel, and use it to print error codes.

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