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