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