1 | /*
|
---|
2 | * Copyright (c) 2010 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 | /** @file
|
---|
33 | */
|
---|
34 |
|
---|
35 | #ifndef KERN_AS_H_
|
---|
36 | #define KERN_AS_H_
|
---|
37 |
|
---|
38 | #include <typedefs.h>
|
---|
39 | #include <abi/mm/as.h>
|
---|
40 | #include <arch/mm/page.h>
|
---|
41 | #include <arch/mm/as.h>
|
---|
42 | #include <arch/mm/asid.h>
|
---|
43 | #include <arch/istate.h>
|
---|
44 | #include <synch/spinlock.h>
|
---|
45 | #include <synch/mutex.h>
|
---|
46 | #include <adt/list.h>
|
---|
47 | #include <adt/btree.h>
|
---|
48 | #include <lib/elf.h>
|
---|
49 | #include <arch.h>
|
---|
50 | #include <lib/refcount.h>
|
---|
51 |
|
---|
52 | #define AS THE->as
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Defined to be true if user address space and kernel address space shadow each
|
---|
56 | * other.
|
---|
57 | *
|
---|
58 | */
|
---|
59 | #define KERNEL_ADDRESS_SPACE_SHADOWED KERNEL_ADDRESS_SPACE_SHADOWED_ARCH
|
---|
60 |
|
---|
61 | #define KERNEL_ADDRESS_SPACE_START KERNEL_ADDRESS_SPACE_START_ARCH
|
---|
62 | #define KERNEL_ADDRESS_SPACE_END KERNEL_ADDRESS_SPACE_END_ARCH
|
---|
63 | #define USER_ADDRESS_SPACE_START USER_ADDRESS_SPACE_START_ARCH
|
---|
64 | #define USER_ADDRESS_SPACE_END USER_ADDRESS_SPACE_END_ARCH
|
---|
65 |
|
---|
66 | /** Kernel address space. */
|
---|
67 | #define FLAG_AS_KERNEL (1 << 0)
|
---|
68 |
|
---|
69 | /* Address space area attributes. */
|
---|
70 | #define AS_AREA_ATTR_NONE 0
|
---|
71 | #define AS_AREA_ATTR_PARTIAL 1 /**< Not fully initialized area. */
|
---|
72 |
|
---|
73 | /** The page fault was resolved by as_page_fault(). */
|
---|
74 | #define AS_PF_OK 0
|
---|
75 |
|
---|
76 | /** The page fault was caused by memcpy_from_uspace() or memcpy_to_uspace(). */
|
---|
77 | #define AS_PF_DEFER 1
|
---|
78 |
|
---|
79 | /** The page fault was not resolved by as_page_fault(). */
|
---|
80 | #define AS_PF_FAULT 2
|
---|
81 |
|
---|
82 | /** The page fault was not resolved by as_page_fault(). Non-verbose version. */
|
---|
83 | #define AS_PF_SILENT 3
|
---|
84 |
|
---|
85 | /** Address space structure.
|
---|
86 | *
|
---|
87 | * as_t contains the list of as_areas of userspace accessible
|
---|
88 | * pages for one or more tasks. Ranges of kernel memory pages are not
|
---|
89 | * supposed to figure in the list as they are shared by all tasks and
|
---|
90 | * set up during system initialization.
|
---|
91 | *
|
---|
92 | */
|
---|
93 | typedef struct as {
|
---|
94 | /** Protected by asidlock. */
|
---|
95 | link_t inactive_as_with_asid_link;
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Number of processors on which this
|
---|
99 | * address space is active. Protected by
|
---|
100 | * asidlock.
|
---|
101 | */
|
---|
102 | size_t cpu_refcount;
|
---|
103 |
|
---|
104 | /** Address space identifier.
|
---|
105 | *
|
---|
106 | * Constant on architectures that do not
|
---|
107 | * support ASIDs. Protected by asidlock.
|
---|
108 | *
|
---|
109 | */
|
---|
110 | asid_t asid;
|
---|
111 |
|
---|
112 | /** Number of references (i.e. tasks that reference this as). */
|
---|
113 | atomic_refcount_t refcount;
|
---|
114 |
|
---|
115 | mutex_t lock;
|
---|
116 |
|
---|
117 | /** B+tree of address space areas. */
|
---|
118 | btree_t as_area_btree;
|
---|
119 |
|
---|
120 | /** Non-generic content. */
|
---|
121 | as_genarch_t genarch;
|
---|
122 |
|
---|
123 | /** Architecture specific content. */
|
---|
124 | as_arch_t arch;
|
---|
125 | } as_t;
|
---|
126 |
|
---|
127 | typedef struct {
|
---|
128 | pte_t *(*page_table_create)(unsigned int);
|
---|
129 | void (*page_table_destroy)(pte_t *);
|
---|
130 | void (*page_table_lock)(as_t *, bool);
|
---|
131 | void (*page_table_unlock)(as_t *, bool);
|
---|
132 | bool (*page_table_locked)(as_t *);
|
---|
133 | } as_operations_t;
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * This structure contains information associated with the shared address space
|
---|
137 | * area.
|
---|
138 | *
|
---|
139 | */
|
---|
140 | typedef struct {
|
---|
141 | /** This lock must be acquired only when the as_area lock is held. */
|
---|
142 | mutex_t lock;
|
---|
143 | /** This structure can be deallocated if refcount drops to 0. */
|
---|
144 | size_t refcount;
|
---|
145 | /** True if the area has been ever shared. */
|
---|
146 | bool shared;
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * B+tree containing complete map of anonymous pages of the shared area.
|
---|
150 | */
|
---|
151 | btree_t pagemap;
|
---|
152 |
|
---|
153 | /** Address space area backend. */
|
---|
154 | struct mem_backend *backend;
|
---|
155 | /** Address space area shared data. */
|
---|
156 | void *backend_shared_data;
|
---|
157 | } share_info_t;
|
---|
158 |
|
---|
159 | /** Page fault access type. */
|
---|
160 | typedef enum {
|
---|
161 | PF_ACCESS_READ,
|
---|
162 | PF_ACCESS_WRITE,
|
---|
163 | PF_ACCESS_EXEC,
|
---|
164 | PF_ACCESS_UNKNOWN
|
---|
165 | } pf_access_t;
|
---|
166 |
|
---|
167 | struct mem_backend;
|
---|
168 |
|
---|
169 | /** Backend data stored in address space area. */
|
---|
170 | typedef union mem_backend_data {
|
---|
171 | /* anon_backend members */
|
---|
172 | struct {
|
---|
173 | };
|
---|
174 |
|
---|
175 | /** elf_backend members */
|
---|
176 | struct {
|
---|
177 | elf_header_t *elf;
|
---|
178 | elf_segment_header_t *segment;
|
---|
179 | };
|
---|
180 |
|
---|
181 | /** phys_backend members */
|
---|
182 | struct {
|
---|
183 | uintptr_t base;
|
---|
184 | size_t frames;
|
---|
185 | bool anonymous;
|
---|
186 | };
|
---|
187 |
|
---|
188 | /** user_backend members */
|
---|
189 | struct {
|
---|
190 | as_area_pager_info_t pager_info;
|
---|
191 | };
|
---|
192 |
|
---|
193 | } mem_backend_data_t;
|
---|
194 |
|
---|
195 | /** Address space area structure.
|
---|
196 | *
|
---|
197 | * Each as_area_t structure describes one contiguous area of virtual memory.
|
---|
198 | *
|
---|
199 | */
|
---|
200 | typedef struct {
|
---|
201 | mutex_t lock;
|
---|
202 |
|
---|
203 | /** Containing address space. */
|
---|
204 | as_t *as;
|
---|
205 |
|
---|
206 | /** Memory flags. */
|
---|
207 | unsigned int flags;
|
---|
208 |
|
---|
209 | /** Address space area attributes. */
|
---|
210 | unsigned int attributes;
|
---|
211 |
|
---|
212 | /** Number of pages in the area. */
|
---|
213 | size_t pages;
|
---|
214 |
|
---|
215 | /** Number of resident pages in the area. */
|
---|
216 | size_t resident;
|
---|
217 |
|
---|
218 | /** Base address of this area. */
|
---|
219 | uintptr_t base;
|
---|
220 |
|
---|
221 | /** Map of used space. */
|
---|
222 | btree_t used_space;
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * If the address space area is shared. this is
|
---|
226 | * a reference to the share info structure.
|
---|
227 | */
|
---|
228 | share_info_t *sh_info;
|
---|
229 |
|
---|
230 | /** Memory backend backing this address space area. */
|
---|
231 | struct mem_backend *backend;
|
---|
232 |
|
---|
233 | /** Data to be used by the backend. */
|
---|
234 | mem_backend_data_t backend_data;
|
---|
235 | } as_area_t;
|
---|
236 |
|
---|
237 | /** Address space area backend structure. */
|
---|
238 | typedef struct mem_backend {
|
---|
239 | bool (*create)(as_area_t *);
|
---|
240 | bool (*resize)(as_area_t *, size_t);
|
---|
241 | void (*share)(as_area_t *);
|
---|
242 | void (*destroy)(as_area_t *);
|
---|
243 |
|
---|
244 | bool (*is_resizable)(as_area_t *);
|
---|
245 | bool (*is_shareable)(as_area_t *);
|
---|
246 |
|
---|
247 | int (*page_fault)(as_area_t *, uintptr_t, pf_access_t);
|
---|
248 | void (*frame_free)(as_area_t *, uintptr_t, uintptr_t);
|
---|
249 |
|
---|
250 | bool (*create_shared_data)(as_area_t *);
|
---|
251 | void (*destroy_shared_data)(void *);
|
---|
252 | } mem_backend_t;
|
---|
253 |
|
---|
254 | extern as_t *AS_KERNEL;
|
---|
255 |
|
---|
256 | extern as_operations_t *as_operations;
|
---|
257 | extern list_t inactive_as_with_asid_list;
|
---|
258 |
|
---|
259 | extern void as_init(void);
|
---|
260 |
|
---|
261 | extern as_t *as_create(unsigned int);
|
---|
262 | extern void as_destroy(as_t *);
|
---|
263 | extern void as_hold(as_t *);
|
---|
264 | extern void as_release(as_t *);
|
---|
265 | extern void as_switch(as_t *, as_t *);
|
---|
266 | extern int as_page_fault(uintptr_t, pf_access_t, istate_t *);
|
---|
267 |
|
---|
268 | extern as_area_t *as_area_create(as_t *, unsigned int, size_t, unsigned int,
|
---|
269 | mem_backend_t *, mem_backend_data_t *, uintptr_t *, uintptr_t);
|
---|
270 | extern errno_t as_area_destroy(as_t *, uintptr_t);
|
---|
271 | extern errno_t as_area_resize(as_t *, uintptr_t, size_t, unsigned int);
|
---|
272 | extern errno_t as_area_share(as_t *, uintptr_t, size_t, as_t *, unsigned int,
|
---|
273 | uintptr_t *, uintptr_t);
|
---|
274 | extern errno_t as_area_change_flags(as_t *, unsigned int, uintptr_t);
|
---|
275 |
|
---|
276 | extern unsigned int as_area_get_flags(as_area_t *);
|
---|
277 | extern bool as_area_check_access(as_area_t *, pf_access_t);
|
---|
278 | extern size_t as_area_get_size(uintptr_t);
|
---|
279 | extern bool used_space_insert(as_area_t *, uintptr_t, size_t);
|
---|
280 | extern bool used_space_remove(as_area_t *, uintptr_t, size_t);
|
---|
281 |
|
---|
282 | /* Interface to be implemented by architectures. */
|
---|
283 |
|
---|
284 | #ifndef as_constructor_arch
|
---|
285 | extern errno_t as_constructor_arch(as_t *, unsigned int);
|
---|
286 | #endif /* !def as_constructor_arch */
|
---|
287 |
|
---|
288 | #ifndef as_destructor_arch
|
---|
289 | extern int as_destructor_arch(as_t *);
|
---|
290 | #endif /* !def as_destructor_arch */
|
---|
291 |
|
---|
292 | #ifndef as_create_arch
|
---|
293 | extern errno_t as_create_arch(as_t *, unsigned int);
|
---|
294 | #endif /* !def as_create_arch */
|
---|
295 |
|
---|
296 | #ifndef as_install_arch
|
---|
297 | extern void as_install_arch(as_t *);
|
---|
298 | #endif /* !def as_install_arch */
|
---|
299 |
|
---|
300 | #ifndef as_deinstall_arch
|
---|
301 | extern void as_deinstall_arch(as_t *);
|
---|
302 | #endif /* !def as_deinstall_arch */
|
---|
303 |
|
---|
304 | /* Backend declarations and functions. */
|
---|
305 | extern mem_backend_t anon_backend;
|
---|
306 | extern mem_backend_t elf_backend;
|
---|
307 | extern mem_backend_t phys_backend;
|
---|
308 | extern mem_backend_t user_backend;
|
---|
309 |
|
---|
310 | /* Address space area related syscalls. */
|
---|
311 | extern sysarg_t sys_as_area_create(uintptr_t, size_t, unsigned int, uintptr_t,
|
---|
312 | as_area_pager_info_t *);
|
---|
313 | extern sys_errno_t sys_as_area_resize(uintptr_t, size_t, unsigned int);
|
---|
314 | extern sys_errno_t sys_as_area_change_flags(uintptr_t, unsigned int);
|
---|
315 | extern sys_errno_t sys_as_area_destroy(uintptr_t);
|
---|
316 |
|
---|
317 | /* Introspection functions. */
|
---|
318 | extern void as_get_area_info(as_t *, as_area_info_t **, size_t *);
|
---|
319 | extern void as_print(as_t *);
|
---|
320 |
|
---|
321 | #endif
|
---|
322 |
|
---|
323 | /** @}
|
---|
324 | */
|
---|