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