[f761f1eb] | 1 | /*
|
---|
[9150781] | 2 | * Copyright (c) 2010 Jakub Jermar
|
---|
[f761f1eb] | 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 |
|
---|
[f47fd19] | 29 | /** @addtogroup genericmm
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[06e1e95] | 35 | #ifndef KERN_AS_H_
|
---|
| 36 | #define KERN_AS_H_
|
---|
[f761f1eb] | 37 |
|
---|
[c0699467] | 38 | #include <typedefs.h>
|
---|
| 39 | #include <abi/mm/as.h>
|
---|
[a1a03f9] | 40 | #include <arch/mm/page.h>
|
---|
[20d50a1] | 41 | #include <arch/mm/as.h>
|
---|
[1084a784] | 42 | #include <arch/mm/asid.h>
|
---|
[27ba40f] | 43 | #include <arch/istate.h>
|
---|
[d99c1d2] | 44 | #include <typedefs.h>
|
---|
[f761f1eb] | 45 | #include <synch/spinlock.h>
|
---|
[1068f6a] | 46 | #include <synch/mutex.h>
|
---|
[5c9a08b] | 47 | #include <adt/list.h>
|
---|
[252127e] | 48 | #include <adt/btree.h>
|
---|
[d4b5542] | 49 | #include <lib/elf.h>
|
---|
[f761f1eb] | 50 |
|
---|
[80bcaed] | 51 | /**
|
---|
| 52 | * Defined to be true if user address space and kernel address space shadow each
|
---|
| 53 | * other.
|
---|
[da1bafb] | 54 | *
|
---|
[80bcaed] | 55 | */
|
---|
[da1bafb] | 56 | #define KERNEL_ADDRESS_SPACE_SHADOWED KERNEL_ADDRESS_SPACE_SHADOWED_ARCH
|
---|
[5a7d9d1] | 57 |
|
---|
[da1bafb] | 58 | #define KERNEL_ADDRESS_SPACE_START KERNEL_ADDRESS_SPACE_START_ARCH
|
---|
| 59 | #define KERNEL_ADDRESS_SPACE_END KERNEL_ADDRESS_SPACE_END_ARCH
|
---|
| 60 | #define USER_ADDRESS_SPACE_START USER_ADDRESS_SPACE_START_ARCH
|
---|
| 61 | #define USER_ADDRESS_SPACE_END USER_ADDRESS_SPACE_END_ARCH
|
---|
[f761f1eb] | 62 |
|
---|
[80bcaed] | 63 | /** Kernel address space. */
|
---|
[da1bafb] | 64 | #define FLAG_AS_KERNEL (1 << 0)
|
---|
[4512d7e] | 65 |
|
---|
[80bcaed] | 66 | /* Address space area attributes. */
|
---|
[da1bafb] | 67 | #define AS_AREA_ATTR_NONE 0
|
---|
| 68 | #define AS_AREA_ATTR_PARTIAL 1 /**< Not fully initialized area. */
|
---|
[8182031] | 69 |
|
---|
[80bcaed] | 70 | /** The page fault was resolved by as_page_fault(). */
|
---|
[908bb96] | 71 | #define AS_PF_OK 0
|
---|
[da1bafb] | 72 |
|
---|
[80bcaed] | 73 | /** The page fault was caused by memcpy_from_uspace() or memcpy_to_uspace(). */
|
---|
[908bb96] | 74 | #define AS_PF_DEFER 1
|
---|
| 75 |
|
---|
| 76 | /** The page fault was not resolved by as_page_fault(). */
|
---|
| 77 | #define AS_PF_FAULT 2
|
---|
| 78 |
|
---|
| 79 | /** The page fault was not resolved by as_page_fault(). Non-verbose version. */
|
---|
| 80 | #define AS_PF_SILENT 3
|
---|
[80bcaed] | 81 |
|
---|
| 82 | /** Address space structure.
|
---|
| 83 | *
|
---|
| 84 | * as_t contains the list of as_areas of userspace accessible
|
---|
| 85 | * pages for one or more tasks. Ranges of kernel memory pages are not
|
---|
| 86 | * supposed to figure in the list as they are shared by all tasks and
|
---|
| 87 | * set up during system initialization.
|
---|
[da1bafb] | 88 | *
|
---|
[80bcaed] | 89 | */
|
---|
| 90 | typedef struct as {
|
---|
| 91 | /** Protected by asidlock. */
|
---|
| 92 | link_t inactive_as_with_asid_link;
|
---|
[da1bafb] | 93 |
|
---|
[879585a3] | 94 | /**
|
---|
[fc47885] | 95 | * Number of processors on which this
|
---|
| 96 | * address space is active. Protected by
|
---|
| 97 | * asidlock.
|
---|
[879585a3] | 98 | */
|
---|
[98000fb] | 99 | size_t cpu_refcount;
|
---|
[da1bafb] | 100 |
|
---|
[fc47885] | 101 | /** Address space identifier.
|
---|
| 102 | *
|
---|
| 103 | * Constant on architectures that do not
|
---|
| 104 | * support ASIDs. Protected by asidlock.
|
---|
| 105 | *
|
---|
[879585a3] | 106 | */
|
---|
| 107 | asid_t asid;
|
---|
[da1bafb] | 108 |
|
---|
[fc47885] | 109 | /** Number of references (i.e. tasks that reference this as). */
|
---|
[31d8e10] | 110 | atomic_t refcount;
|
---|
[da1bafb] | 111 |
|
---|
[31d8e10] | 112 | mutex_t lock;
|
---|
[da1bafb] | 113 |
|
---|
[80bcaed] | 114 | /** B+tree of address space areas. */
|
---|
| 115 | btree_t as_area_btree;
|
---|
| 116 |
|
---|
| 117 | /** Non-generic content. */
|
---|
| 118 | as_genarch_t genarch;
|
---|
[da1bafb] | 119 |
|
---|
[80bcaed] | 120 | /** Architecture specific content. */
|
---|
| 121 | as_arch_t arch;
|
---|
| 122 | } as_t;
|
---|
[b3f8fb7] | 123 |
|
---|
| 124 | typedef struct {
|
---|
[da1bafb] | 125 | pte_t *(* page_table_create)(unsigned int);
|
---|
| 126 | void (* page_table_destroy)(pte_t *);
|
---|
| 127 | void (* page_table_lock)(as_t *, bool);
|
---|
| 128 | void (* page_table_unlock)(as_t *, bool);
|
---|
[ada559c] | 129 | bool (* page_table_locked)(as_t *);
|
---|
[b3f8fb7] | 130 | } as_operations_t;
|
---|
[8182031] | 131 |
|
---|
[80bcaed] | 132 | /**
|
---|
| 133 | * This structure contains information associated with the shared address space
|
---|
| 134 | * area.
|
---|
[da1bafb] | 135 | *
|
---|
[80bcaed] | 136 | */
|
---|
[0ee077ee] | 137 | typedef struct {
|
---|
[80bcaed] | 138 | /** This lock must be acquired only when the as_area lock is held. */
|
---|
[da1bafb] | 139 | mutex_t lock;
|
---|
[80bcaed] | 140 | /** This structure can be deallocated if refcount drops to 0. */
|
---|
[98000fb] | 141 | size_t refcount;
|
---|
[83b6ba9f] | 142 | /** True if the area has been ever shared. */
|
---|
| 143 | bool shared;
|
---|
| 144 |
|
---|
[80bcaed] | 145 | /**
|
---|
| 146 | * B+tree containing complete map of anonymous pages of the shared area.
|
---|
| 147 | */
|
---|
| 148 | btree_t pagemap;
|
---|
[83b6ba9f] | 149 |
|
---|
| 150 | /** Address space area backend. */
|
---|
| 151 | struct mem_backend *backend;
|
---|
| 152 | /** Address space area shared data. */
|
---|
| 153 | void *backend_shared_data;
|
---|
[0ee077ee] | 154 | } share_info_t;
|
---|
| 155 |
|
---|
[b3f8fb7] | 156 | /** Page fault access type. */
|
---|
| 157 | typedef enum {
|
---|
| 158 | PF_ACCESS_READ,
|
---|
| 159 | PF_ACCESS_WRITE,
|
---|
[c15b374] | 160 | PF_ACCESS_EXEC,
|
---|
| 161 | PF_ACCESS_UNKNOWN
|
---|
[b3f8fb7] | 162 | } pf_access_t;
|
---|
| 163 |
|
---|
| 164 | struct mem_backend;
|
---|
[0ee077ee] | 165 |
|
---|
| 166 | /** Backend data stored in address space area. */
|
---|
[b3f8fb7] | 167 | typedef union mem_backend_data {
|
---|
[da1bafb] | 168 | /** elf_backend members */
|
---|
| 169 | struct {
|
---|
[127c957b] | 170 | elf_header_t *elf;
|
---|
| 171 | elf_segment_header_t *segment;
|
---|
| 172 | };
|
---|
[da1bafb] | 173 |
|
---|
| 174 | /** phys_backend members */
|
---|
| 175 | struct {
|
---|
[7f1c620] | 176 | uintptr_t base;
|
---|
[98000fb] | 177 | size_t frames;
|
---|
[c101dc0] | 178 | bool anonymous;
|
---|
[127c957b] | 179 | };
|
---|
[0ee077ee] | 180 | } mem_backend_data_t;
|
---|
[8182031] | 181 |
|
---|
| 182 | /** Address space area structure.
|
---|
| 183 | *
|
---|
| 184 | * Each as_area_t structure describes one contiguous area of virtual memory.
|
---|
[da1bafb] | 185 | *
|
---|
[8182031] | 186 | */
|
---|
[b3f8fb7] | 187 | typedef struct {
|
---|
[8182031] | 188 | mutex_t lock;
|
---|
[fc47885] | 189 |
|
---|
[80bcaed] | 190 | /** Containing address space. */
|
---|
[da1bafb] | 191 | as_t *as;
|
---|
| 192 |
|
---|
[fc47885] | 193 | /** Memory flags. */
|
---|
[da1bafb] | 194 | unsigned int flags;
|
---|
| 195 |
|
---|
[fc47885] | 196 | /** Address space area attributes. */
|
---|
[da1bafb] | 197 | unsigned int attributes;
|
---|
[fc47885] | 198 |
|
---|
| 199 | /** Number of pages in the area. */
|
---|
[98000fb] | 200 | size_t pages;
|
---|
[fc47885] | 201 |
|
---|
| 202 | /** Number of resident pages in the area. */
|
---|
| 203 | size_t resident;
|
---|
| 204 |
|
---|
[80bcaed] | 205 | /** Base address of this area. */
|
---|
| 206 | uintptr_t base;
|
---|
[fc47885] | 207 |
|
---|
[80bcaed] | 208 | /** Map of used space. */
|
---|
| 209 | btree_t used_space;
|
---|
[da1bafb] | 210 |
|
---|
[80bcaed] | 211 | /**
|
---|
[fc47885] | 212 | * If the address space area is shared. this is
|
---|
| 213 | * a reference to the share info structure.
|
---|
[80bcaed] | 214 | */
|
---|
| 215 | share_info_t *sh_info;
|
---|
[da1bafb] | 216 |
|
---|
[80bcaed] | 217 | /** Memory backend backing this address space area. */
|
---|
| 218 | struct mem_backend *backend;
|
---|
[da1bafb] | 219 |
|
---|
[0ee077ee] | 220 | /** Data to be used by the backend. */
|
---|
| 221 | mem_backend_data_t backend_data;
|
---|
[b3f8fb7] | 222 | } as_area_t;
|
---|
| 223 |
|
---|
| 224 | /** Address space area backend structure. */
|
---|
| 225 | typedef struct mem_backend {
|
---|
[03523dc] | 226 | bool (* create)(as_area_t *);
|
---|
| 227 | bool (* resize)(as_area_t *, size_t);
|
---|
| 228 | void (* share)(as_area_t *);
|
---|
| 229 | void (* destroy)(as_area_t *);
|
---|
| 230 |
|
---|
[01029fc] | 231 | bool (* is_resizable)(as_area_t *);
|
---|
| 232 | bool (* is_shareable)(as_area_t *);
|
---|
| 233 |
|
---|
[da1bafb] | 234 | int (* page_fault)(as_area_t *, uintptr_t, pf_access_t);
|
---|
| 235 | void (* frame_free)(as_area_t *, uintptr_t, uintptr_t);
|
---|
[83b6ba9f] | 236 |
|
---|
| 237 | bool (* create_shared_data)(as_area_t *);
|
---|
| 238 | void (* destroy_shared_data)(void *);
|
---|
[b3f8fb7] | 239 | } mem_backend_t;
|
---|
[8182031] | 240 |
|
---|
[fc1e4f6] | 241 | extern as_t *AS_KERNEL;
|
---|
[bd1deed] | 242 |
|
---|
[ef67bab] | 243 | extern as_operations_t *as_operations;
|
---|
[55b77d9] | 244 | extern list_t inactive_as_with_asid_list;
|
---|
[7e4e532] | 245 |
|
---|
[ef67bab] | 246 | extern void as_init(void);
|
---|
[482826d] | 247 |
|
---|
[da1bafb] | 248 | extern as_t *as_create(unsigned int);
|
---|
[9150781] | 249 | extern void as_destroy(as_t *);
|
---|
[0321109] | 250 | extern void as_hold(as_t *);
|
---|
| 251 | extern void as_release(as_t *);
|
---|
[9150781] | 252 | extern void as_switch(as_t *, as_t *);
|
---|
| 253 | extern int as_page_fault(uintptr_t, pf_access_t, istate_t *);
|
---|
[482826d] | 254 |
|
---|
[fbcdeb8] | 255 | extern as_area_t *as_area_create(as_t *, unsigned int, size_t, unsigned int,
|
---|
| 256 | mem_backend_t *, mem_backend_data_t *, uintptr_t *, uintptr_t);
|
---|
[9150781] | 257 | extern int as_area_destroy(as_t *, uintptr_t);
|
---|
[da1bafb] | 258 | extern int as_area_resize(as_t *, uintptr_t, size_t, unsigned int);
|
---|
[fbcdeb8] | 259 | extern int as_area_share(as_t *, uintptr_t, size_t, as_t *, unsigned int,
|
---|
| 260 | uintptr_t *, uintptr_t);
|
---|
[da1bafb] | 261 | extern int as_area_change_flags(as_t *, unsigned int, uintptr_t);
|
---|
[482826d] | 262 |
|
---|
[da1bafb] | 263 | extern unsigned int as_area_get_flags(as_area_t *);
|
---|
[9150781] | 264 | extern bool as_area_check_access(as_area_t *, pf_access_t);
|
---|
| 265 | extern size_t as_area_get_size(uintptr_t);
|
---|
[fc47885] | 266 | extern bool used_space_insert(as_area_t *, uintptr_t, size_t);
|
---|
| 267 | extern bool used_space_remove(as_area_t *, uintptr_t, size_t);
|
---|
[29b2bbf] | 268 |
|
---|
[4512d7e] | 269 | /* Interface to be implemented by architectures. */
|
---|
[da1bafb] | 270 |
|
---|
[29b2bbf] | 271 | #ifndef as_constructor_arch
|
---|
[da1bafb] | 272 | extern int as_constructor_arch(as_t *, unsigned int);
|
---|
[29b2bbf] | 273 | #endif /* !def as_constructor_arch */
|
---|
[da1bafb] | 274 |
|
---|
[29b2bbf] | 275 | #ifndef as_destructor_arch
|
---|
[9150781] | 276 | extern int as_destructor_arch(as_t *);
|
---|
[29b2bbf] | 277 | #endif /* !def as_destructor_arch */
|
---|
[da1bafb] | 278 |
|
---|
[29b2bbf] | 279 | #ifndef as_create_arch
|
---|
[da1bafb] | 280 | extern int as_create_arch(as_t *, unsigned int);
|
---|
[29b2bbf] | 281 | #endif /* !def as_create_arch */
|
---|
[da1bafb] | 282 |
|
---|
[20d50a1] | 283 | #ifndef as_install_arch
|
---|
[9150781] | 284 | extern void as_install_arch(as_t *);
|
---|
[20d50a1] | 285 | #endif /* !def as_install_arch */
|
---|
[da1bafb] | 286 |
|
---|
[57da95c] | 287 | #ifndef as_deinstall_arch
|
---|
[9150781] | 288 | extern void as_deinstall_arch(as_t *);
|
---|
[57da95c] | 289 | #endif /* !def as_deinstall_arch */
|
---|
[f761f1eb] | 290 |
|
---|
[b3f8fb7] | 291 | /* Backend declarations and functions. */
|
---|
[8182031] | 292 | extern mem_backend_t anon_backend;
|
---|
| 293 | extern mem_backend_t elf_backend;
|
---|
[0ee077ee] | 294 | extern mem_backend_t phys_backend;
|
---|
[8182031] | 295 |
|
---|
[df0103f7] | 296 | /* Address space area related syscalls. */
|
---|
[fbcdeb8] | 297 | extern sysarg_t sys_as_area_create(uintptr_t, size_t, unsigned int, uintptr_t);
|
---|
[96b02eb9] | 298 | extern sysarg_t sys_as_area_resize(uintptr_t, size_t, unsigned int);
|
---|
| 299 | extern sysarg_t sys_as_area_change_flags(uintptr_t, unsigned int);
|
---|
| 300 | extern sysarg_t sys_as_area_destroy(uintptr_t);
|
---|
[7c23af9] | 301 |
|
---|
[64c2ad5] | 302 | /* Introspection functions. */
|
---|
[9150781] | 303 | extern void as_get_area_info(as_t *, as_area_info_t **, size_t *);
|
---|
| 304 | extern void as_print(as_t *);
|
---|
[64c2ad5] | 305 |
|
---|
[f761f1eb] | 306 | #endif
|
---|
[b45c443] | 307 |
|
---|
[f47fd19] | 308 | /** @}
|
---|
[b45c443] | 309 | */
|
---|