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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c28413a9 was 1066041, checked in by Adam Hraska <adam.hraska+hos@…>, 13 years ago

preemption_disable: Turned functions into macros. Moved THREAD, AS, TASK, CPU into thread.h, as.h, task.h, cpu.h to fix the include hell that ensued.

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