source: mainline/kernel/generic/include/mm/as.h@ de9a18e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since de9a18e was de0af3a, checked in by jxsvoboda <5887334+jxsvoboda@…>, 7 years ago

Use ordered dictionary for pagemap instead of B+tree

At the same time we add a little bit more abstraction around the pagemap.

  • Property mode set to 100644
File size: 9.9 KB
Line 
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 */
94typedef 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
131typedef 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/** Single anonymous page mapping. */
140typedef struct {
141 /** Containing pagemap structure */
142 struct as_pagemap *pagemap;
143 /** Link to @c shinfo->pagemap ordered dictionary */
144 odlink_t lpagemap;
145 /** Virtual address */
146 uintptr_t vaddr;
147 /** Physical frame address */
148 uintptr_t frame;
149} as_page_mapping_t;
150
151/** Map of anonymous pages in a shared area. */
152typedef struct as_pagemap {
153 /**
154 * Dictionary ordered by virtual address. Members are of type
155 * as_page_mapping_t
156 */
157 odict_t map;
158} as_pagemap_t;
159
160/**
161 * This structure contains information associated with the shared address space
162 * area.
163 *
164 */
165typedef struct {
166 /** This lock must be acquired only when the as_area lock is held. */
167 mutex_t lock;
168 /** This structure can be deallocated if refcount drops to 0. */
169 size_t refcount;
170 /** True if the area has been ever shared. */
171 bool shared;
172
173 /** Complete map of anonymous pages of the shared area. */
174 as_pagemap_t pagemap;
175
176 /** Address space area backend. */
177 struct mem_backend *backend;
178 /** Address space area shared data. */
179 void *backend_shared_data;
180} share_info_t;
181
182/** Page fault access type. */
183typedef enum {
184 PF_ACCESS_READ,
185 PF_ACCESS_WRITE,
186 PF_ACCESS_EXEC,
187 PF_ACCESS_UNKNOWN
188} pf_access_t;
189
190struct mem_backend;
191
192/** Backend data stored in address space area. */
193typedef union mem_backend_data {
194 /* anon_backend members */
195 struct {
196 };
197
198 /** elf_backend members */
199 struct {
200 uintptr_t elf_base;
201 elf_header_t *elf;
202 elf_segment_header_t *segment;
203 };
204
205 /** phys_backend members */
206 struct {
207 uintptr_t base;
208 size_t frames;
209 bool anonymous;
210 };
211
212 /** user_backend members */
213 struct {
214 as_area_pager_info_t pager_info;
215 };
216
217} mem_backend_data_t;
218
219/** Address space area structure.
220 *
221 * Each as_area_t structure describes one contiguous area of virtual memory.
222 *
223 */
224typedef struct {
225 mutex_t lock;
226
227 /** Containing address space. */
228 as_t *as;
229
230 /** Link to @c as->as_areas */
231 odlink_t las_areas;
232
233 /** Memory flags. */
234 unsigned int flags;
235
236 /** Address space area attributes. */
237 unsigned int attributes;
238
239 /** Number of pages in the area. */
240 size_t pages;
241
242 /** Number of resident pages in the area. */
243 size_t resident;
244
245 /** Base address of this area. */
246 uintptr_t base;
247
248 /** Map of used space. */
249 btree_t used_space;
250
251 /**
252 * If the address space area is shared. this is
253 * a reference to the share info structure.
254 */
255 share_info_t *sh_info;
256
257 /** Memory backend backing this address space area. */
258 struct mem_backend *backend;
259
260 /** Data to be used by the backend. */
261 mem_backend_data_t backend_data;
262} as_area_t;
263
264/** Address space area backend structure. */
265typedef struct mem_backend {
266 bool (*create)(as_area_t *);
267 bool (*resize)(as_area_t *, size_t);
268 void (*share)(as_area_t *);
269 void (*destroy)(as_area_t *);
270
271 bool (*is_resizable)(as_area_t *);
272 bool (*is_shareable)(as_area_t *);
273
274 int (*page_fault)(as_area_t *, uintptr_t, pf_access_t);
275 void (*frame_free)(as_area_t *, uintptr_t, uintptr_t);
276
277 bool (*create_shared_data)(as_area_t *);
278 void (*destroy_shared_data)(void *);
279} mem_backend_t;
280
281extern as_t *AS_KERNEL;
282
283extern as_operations_t *as_operations;
284extern list_t inactive_as_with_asid_list;
285
286extern void as_init(void);
287
288extern as_t *as_create(unsigned int);
289extern void as_hold(as_t *);
290extern void as_release(as_t *);
291extern void as_switch(as_t *, as_t *);
292extern int as_page_fault(uintptr_t, pf_access_t, istate_t *);
293
294extern as_area_t *as_area_create(as_t *, unsigned int, size_t, unsigned int,
295 mem_backend_t *, mem_backend_data_t *, uintptr_t *, uintptr_t);
296extern errno_t as_area_destroy(as_t *, uintptr_t);
297extern errno_t as_area_resize(as_t *, uintptr_t, size_t, unsigned int);
298extern errno_t as_area_share(as_t *, uintptr_t, size_t, as_t *, unsigned int,
299 uintptr_t *, uintptr_t);
300extern errno_t as_area_change_flags(as_t *, unsigned int, uintptr_t);
301extern as_area_t *as_area_first(as_t *);
302extern as_area_t *as_area_next(as_area_t *);
303
304extern void as_pagemap_initialize(as_pagemap_t *);
305extern void as_pagemap_finalize(as_pagemap_t *);
306extern as_page_mapping_t *as_pagemap_first(as_pagemap_t *);
307extern as_page_mapping_t *as_pagemap_next(as_page_mapping_t *);
308extern errno_t as_pagemap_find(as_pagemap_t *, uintptr_t, uintptr_t *);
309extern void as_pagemap_insert(as_pagemap_t *, uintptr_t, uintptr_t);
310extern void as_pagemap_remove(as_page_mapping_t *);
311
312extern unsigned int as_area_get_flags(as_area_t *);
313extern bool as_area_check_access(as_area_t *, pf_access_t);
314extern size_t as_area_get_size(uintptr_t);
315extern bool used_space_insert(as_area_t *, uintptr_t, size_t);
316extern bool used_space_remove(as_area_t *, uintptr_t, size_t);
317
318/* Interface to be implemented by architectures. */
319
320#ifndef as_constructor_arch
321extern errno_t as_constructor_arch(as_t *, unsigned int);
322#endif /* !def as_constructor_arch */
323
324#ifndef as_destructor_arch
325extern int as_destructor_arch(as_t *);
326#endif /* !def as_destructor_arch */
327
328#ifndef as_create_arch
329extern errno_t as_create_arch(as_t *, unsigned int);
330#endif /* !def as_create_arch */
331
332#ifndef as_install_arch
333extern void as_install_arch(as_t *);
334#endif /* !def as_install_arch */
335
336#ifndef as_deinstall_arch
337extern void as_deinstall_arch(as_t *);
338#endif /* !def as_deinstall_arch */
339
340/* Backend declarations and functions. */
341extern mem_backend_t anon_backend;
342extern mem_backend_t elf_backend;
343extern mem_backend_t phys_backend;
344extern mem_backend_t user_backend;
345
346/* Address space area related syscalls. */
347extern sysarg_t sys_as_area_create(uintptr_t, size_t, unsigned int, uintptr_t,
348 as_area_pager_info_t *);
349extern sys_errno_t sys_as_area_resize(uintptr_t, size_t, unsigned int);
350extern sys_errno_t sys_as_area_change_flags(uintptr_t, unsigned int);
351extern sys_errno_t sys_as_area_get_info(uintptr_t, as_area_info_t *);
352extern sys_errno_t sys_as_area_destroy(uintptr_t);
353
354/* Introspection functions. */
355extern as_area_info_t *as_get_area_info(as_t *, size_t *);
356extern void as_print(as_t *);
357
358#endif
359
360/** @}
361 */
Note: See TracBrowser for help on using the repository browser.