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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3666d386 was e16e2ba4, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Eliminate userspace copy of elf.h (ELF format definitions).

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