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