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