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