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