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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b1c57a8 was b1c57a8, checked in by Jakub Jermar <jakub@…>, 11 years ago

Merge from lp:~adam-hraska+lp/helenos/rcu/.

Only merge from the feature branch and resolve all conflicts.

  • Property mode set to 100644
File size: 8.5 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 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#include <arch.h>
51
52#define AS THE->as
53
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_t refcount;
115
116 mutex_t lock;
117
118 /** B+tree of address space areas. */
119 btree_t as_area_btree;
120
121 /** Non-generic content. */
122 as_genarch_t genarch;
123
124 /** Architecture specific content. */
125 as_arch_t arch;
126} as_t;
127
128typedef struct {
129 pte_t *(* page_table_create)(unsigned int);
130 void (* page_table_destroy)(pte_t *);
131 void (* page_table_lock)(as_t *, bool);
132 void (* page_table_unlock)(as_t *, bool);
133 bool (* page_table_locked)(as_t *);
134} as_operations_t;
135
136/**
137 * This structure contains information associated with the shared address space
138 * area.
139 *
140 */
141typedef struct {
142 /** This lock must be acquired only when the as_area lock is held. */
143 mutex_t lock;
144 /** This structure can be deallocated if refcount drops to 0. */
145 size_t refcount;
146 /** True if the area has been ever shared. */
147 bool shared;
148
149 /**
150 * B+tree containing complete map of anonymous pages of the shared area.
151 */
152 btree_t pagemap;
153
154 /** Address space area backend. */
155 struct mem_backend *backend;
156 /** Address space area shared data. */
157 void *backend_shared_data;
158} share_info_t;
159
160/** Page fault access type. */
161typedef enum {
162 PF_ACCESS_READ,
163 PF_ACCESS_WRITE,
164 PF_ACCESS_EXEC,
165 PF_ACCESS_UNKNOWN
166} pf_access_t;
167
168struct mem_backend;
169
170/** Backend data stored in address space area. */
171typedef union mem_backend_data {
172 /** elf_backend members */
173 struct {
174 elf_header_t *elf;
175 elf_segment_header_t *segment;
176 };
177
178 /** phys_backend members */
179 struct {
180 uintptr_t base;
181 size_t frames;
182 bool anonymous;
183 };
184} mem_backend_data_t;
185
186/** Address space area structure.
187 *
188 * Each as_area_t structure describes one contiguous area of virtual memory.
189 *
190 */
191typedef struct {
192 mutex_t lock;
193
194 /** Containing address space. */
195 as_t *as;
196
197 /** Memory flags. */
198 unsigned int flags;
199
200 /** Address space area attributes. */
201 unsigned int attributes;
202
203 /** Number of pages in the area. */
204 size_t pages;
205
206 /** Number of resident pages in the area. */
207 size_t resident;
208
209 /** Base address of this area. */
210 uintptr_t base;
211
212 /** Map of used space. */
213 btree_t used_space;
214
215 /**
216 * If the address space area is shared. this is
217 * a reference to the share info structure.
218 */
219 share_info_t *sh_info;
220
221 /** Memory backend backing this address space area. */
222 struct mem_backend *backend;
223
224 /** Data to be used by the backend. */
225 mem_backend_data_t backend_data;
226} as_area_t;
227
228/** Address space area backend structure. */
229typedef struct mem_backend {
230 bool (* create)(as_area_t *);
231 bool (* resize)(as_area_t *, size_t);
232 void (* share)(as_area_t *);
233 void (* destroy)(as_area_t *);
234
235 bool (* is_resizable)(as_area_t *);
236 bool (* is_shareable)(as_area_t *);
237
238 int (* page_fault)(as_area_t *, uintptr_t, pf_access_t);
239 void (* frame_free)(as_area_t *, uintptr_t, uintptr_t);
240
241 bool (* create_shared_data)(as_area_t *);
242 void (* destroy_shared_data)(void *);
243} mem_backend_t;
244
245extern as_t *AS_KERNEL;
246
247extern as_operations_t *as_operations;
248extern list_t inactive_as_with_asid_list;
249
250extern void as_init(void);
251
252extern as_t *as_create(unsigned int);
253extern void as_destroy(as_t *);
254extern void as_hold(as_t *);
255extern void as_release(as_t *);
256extern void as_switch(as_t *, as_t *);
257extern int as_page_fault(uintptr_t, pf_access_t, istate_t *);
258
259extern as_area_t *as_area_create(as_t *, unsigned int, size_t, unsigned int,
260 mem_backend_t *, mem_backend_data_t *, uintptr_t *, uintptr_t);
261extern int as_area_destroy(as_t *, uintptr_t);
262extern int as_area_resize(as_t *, uintptr_t, size_t, unsigned int);
263extern int as_area_share(as_t *, uintptr_t, size_t, as_t *, unsigned int,
264 uintptr_t *, uintptr_t);
265extern int as_area_change_flags(as_t *, unsigned int, uintptr_t);
266
267extern unsigned int as_area_get_flags(as_area_t *);
268extern bool as_area_check_access(as_area_t *, pf_access_t);
269extern size_t as_area_get_size(uintptr_t);
270extern bool used_space_insert(as_area_t *, uintptr_t, size_t);
271extern bool used_space_remove(as_area_t *, uintptr_t, size_t);
272
273/* Interface to be implemented by architectures. */
274
275#ifndef as_constructor_arch
276extern int as_constructor_arch(as_t *, unsigned int);
277#endif /* !def as_constructor_arch */
278
279#ifndef as_destructor_arch
280extern int as_destructor_arch(as_t *);
281#endif /* !def as_destructor_arch */
282
283#ifndef as_create_arch
284extern int as_create_arch(as_t *, unsigned int);
285#endif /* !def as_create_arch */
286
287#ifndef as_install_arch
288extern void as_install_arch(as_t *);
289#endif /* !def as_install_arch */
290
291#ifndef as_deinstall_arch
292extern void as_deinstall_arch(as_t *);
293#endif /* !def as_deinstall_arch */
294
295/* Backend declarations and functions. */
296extern mem_backend_t anon_backend;
297extern mem_backend_t elf_backend;
298extern mem_backend_t phys_backend;
299
300/* Address space area related syscalls. */
301extern sysarg_t sys_as_area_create(uintptr_t, size_t, unsigned int, uintptr_t);
302extern sysarg_t sys_as_area_resize(uintptr_t, size_t, unsigned int);
303extern sysarg_t sys_as_area_change_flags(uintptr_t, unsigned int);
304extern sysarg_t sys_as_area_destroy(uintptr_t);
305
306/* Introspection functions. */
307extern void as_get_area_info(as_t *, as_area_info_t **, size_t *);
308extern void as_print(as_t *);
309
310#endif
311
312/** @}
313 */
Note: See TracBrowser for help on using the repository browser.