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