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