[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>
|
---|
[27ba40f] | 67 | #include <arch/istate.h>
|
---|
[d99c1d2] | 68 | #include <typedefs.h>
|
---|
[f761f1eb] | 69 | #include <synch/spinlock.h>
|
---|
[1068f6a] | 70 | #include <synch/mutex.h>
|
---|
[5c9a08b] | 71 | #include <adt/list.h>
|
---|
[252127e] | 72 | #include <adt/btree.h>
|
---|
[d4b5542] | 73 | #include <lib/elf.h>
|
---|
[f761f1eb] | 74 |
|
---|
[80bcaed] | 75 | /**
|
---|
| 76 | * Defined to be true if user address space and kernel address space shadow each
|
---|
| 77 | * other.
|
---|
[da1bafb] | 78 | *
|
---|
[80bcaed] | 79 | */
|
---|
[da1bafb] | 80 | #define KERNEL_ADDRESS_SPACE_SHADOWED KERNEL_ADDRESS_SPACE_SHADOWED_ARCH
|
---|
[5a7d9d1] | 81 |
|
---|
[da1bafb] | 82 | #define KERNEL_ADDRESS_SPACE_START KERNEL_ADDRESS_SPACE_START_ARCH
|
---|
| 83 | #define KERNEL_ADDRESS_SPACE_END KERNEL_ADDRESS_SPACE_END_ARCH
|
---|
| 84 | #define USER_ADDRESS_SPACE_START USER_ADDRESS_SPACE_START_ARCH
|
---|
| 85 | #define USER_ADDRESS_SPACE_END USER_ADDRESS_SPACE_END_ARCH
|
---|
[f761f1eb] | 86 |
|
---|
[26aafe8] | 87 | #ifdef USTACK_ADDRESS_ARCH
|
---|
| 88 | #define USTACK_ADDRESS USTACK_ADDRESS_ARCH
|
---|
| 89 | #else
|
---|
| 90 | #define USTACK_ADDRESS (USER_ADDRESS_SPACE_END - (STACK_SIZE - 1))
|
---|
| 91 | #endif
|
---|
[f761f1eb] | 92 |
|
---|
[80bcaed] | 93 | /** Kernel address space. */
|
---|
[da1bafb] | 94 | #define FLAG_AS_KERNEL (1 << 0)
|
---|
[4512d7e] | 95 |
|
---|
[80bcaed] | 96 | /* Address space area attributes. */
|
---|
[da1bafb] | 97 | #define AS_AREA_ATTR_NONE 0
|
---|
| 98 | #define AS_AREA_ATTR_PARTIAL 1 /**< Not fully initialized area. */
|
---|
[8182031] | 99 |
|
---|
[80bcaed] | 100 | /** The page fault was not resolved by as_page_fault(). */
|
---|
[da1bafb] | 101 | #define AS_PF_FAULT 0
|
---|
| 102 |
|
---|
[80bcaed] | 103 | /** The page fault was resolved by as_page_fault(). */
|
---|
[da1bafb] | 104 | #define AS_PF_OK 1
|
---|
| 105 |
|
---|
[80bcaed] | 106 | /** The page fault was caused by memcpy_from_uspace() or memcpy_to_uspace(). */
|
---|
[da1bafb] | 107 | #define AS_PF_DEFER 2
|
---|
[80bcaed] | 108 |
|
---|
| 109 | /** Address space structure.
|
---|
| 110 | *
|
---|
| 111 | * as_t contains the list of as_areas of userspace accessible
|
---|
| 112 | * pages for one or more tasks. Ranges of kernel memory pages are not
|
---|
| 113 | * supposed to figure in the list as they are shared by all tasks and
|
---|
| 114 | * set up during system initialization.
|
---|
[da1bafb] | 115 | *
|
---|
[80bcaed] | 116 | */
|
---|
| 117 | typedef struct as {
|
---|
| 118 | /** Protected by asidlock. */
|
---|
| 119 | link_t inactive_as_with_asid_link;
|
---|
[da1bafb] | 120 |
|
---|
[879585a3] | 121 | /**
|
---|
[fc47885] | 122 | * Number of processors on which this
|
---|
| 123 | * address space is active. Protected by
|
---|
| 124 | * asidlock.
|
---|
[879585a3] | 125 | */
|
---|
[98000fb] | 126 | size_t cpu_refcount;
|
---|
[da1bafb] | 127 |
|
---|
[fc47885] | 128 | /** Address space identifier.
|
---|
| 129 | *
|
---|
| 130 | * Constant on architectures that do not
|
---|
| 131 | * support ASIDs. Protected by asidlock.
|
---|
| 132 | *
|
---|
[879585a3] | 133 | */
|
---|
| 134 | asid_t asid;
|
---|
[da1bafb] | 135 |
|
---|
[fc47885] | 136 | /** Number of references (i.e. tasks that reference this as). */
|
---|
[31d8e10] | 137 | atomic_t refcount;
|
---|
[da1bafb] | 138 |
|
---|
[31d8e10] | 139 | mutex_t lock;
|
---|
[da1bafb] | 140 |
|
---|
[80bcaed] | 141 | /** B+tree of address space areas. */
|
---|
| 142 | btree_t as_area_btree;
|
---|
| 143 |
|
---|
| 144 | /** Non-generic content. */
|
---|
| 145 | as_genarch_t genarch;
|
---|
[da1bafb] | 146 |
|
---|
[80bcaed] | 147 | /** Architecture specific content. */
|
---|
| 148 | as_arch_t arch;
|
---|
| 149 | } as_t;
|
---|
[b3f8fb7] | 150 |
|
---|
| 151 | typedef struct {
|
---|
[da1bafb] | 152 | pte_t *(* page_table_create)(unsigned int);
|
---|
| 153 | void (* page_table_destroy)(pte_t *);
|
---|
| 154 | void (* page_table_lock)(as_t *, bool);
|
---|
| 155 | void (* page_table_unlock)(as_t *, bool);
|
---|
[ada559c] | 156 | bool (* page_table_locked)(as_t *);
|
---|
[b3f8fb7] | 157 | } as_operations_t;
|
---|
[8182031] | 158 |
|
---|
[80bcaed] | 159 | /**
|
---|
| 160 | * This structure contains information associated with the shared address space
|
---|
| 161 | * area.
|
---|
[da1bafb] | 162 | *
|
---|
[80bcaed] | 163 | */
|
---|
[0ee077ee] | 164 | typedef struct {
|
---|
[80bcaed] | 165 | /** This lock must be acquired only when the as_area lock is held. */
|
---|
[da1bafb] | 166 | mutex_t lock;
|
---|
[80bcaed] | 167 | /** This structure can be deallocated if refcount drops to 0. */
|
---|
[98000fb] | 168 | size_t refcount;
|
---|
[da1bafb] | 169 |
|
---|
[80bcaed] | 170 | /**
|
---|
| 171 | * B+tree containing complete map of anonymous pages of the shared area.
|
---|
| 172 | */
|
---|
| 173 | btree_t pagemap;
|
---|
[0ee077ee] | 174 | } share_info_t;
|
---|
| 175 |
|
---|
[b3f8fb7] | 176 | /** Page fault access type. */
|
---|
| 177 | typedef enum {
|
---|
| 178 | PF_ACCESS_READ,
|
---|
| 179 | PF_ACCESS_WRITE,
|
---|
[c15b374] | 180 | PF_ACCESS_EXEC,
|
---|
| 181 | PF_ACCESS_UNKNOWN
|
---|
[b3f8fb7] | 182 | } pf_access_t;
|
---|
| 183 |
|
---|
| 184 | struct mem_backend;
|
---|
[0ee077ee] | 185 |
|
---|
| 186 | /** Backend data stored in address space area. */
|
---|
[b3f8fb7] | 187 | typedef union mem_backend_data {
|
---|
[da1bafb] | 188 | /** elf_backend members */
|
---|
| 189 | struct {
|
---|
[127c957b] | 190 | elf_header_t *elf;
|
---|
| 191 | elf_segment_header_t *segment;
|
---|
| 192 | };
|
---|
[da1bafb] | 193 |
|
---|
| 194 | /** phys_backend members */
|
---|
| 195 | struct {
|
---|
[7f1c620] | 196 | uintptr_t base;
|
---|
[98000fb] | 197 | size_t frames;
|
---|
[127c957b] | 198 | };
|
---|
[0ee077ee] | 199 | } mem_backend_data_t;
|
---|
[8182031] | 200 |
|
---|
| 201 | /** Address space area structure.
|
---|
| 202 | *
|
---|
| 203 | * Each as_area_t structure describes one contiguous area of virtual memory.
|
---|
[da1bafb] | 204 | *
|
---|
[8182031] | 205 | */
|
---|
[b3f8fb7] | 206 | typedef struct {
|
---|
[8182031] | 207 | mutex_t lock;
|
---|
[fc47885] | 208 |
|
---|
[80bcaed] | 209 | /** Containing address space. */
|
---|
[da1bafb] | 210 | as_t *as;
|
---|
| 211 |
|
---|
[fc47885] | 212 | /** Memory flags. */
|
---|
[da1bafb] | 213 | unsigned int flags;
|
---|
| 214 |
|
---|
[fc47885] | 215 | /** Address space area attributes. */
|
---|
[da1bafb] | 216 | unsigned int attributes;
|
---|
[fc47885] | 217 |
|
---|
| 218 | /** Number of pages in the area. */
|
---|
[98000fb] | 219 | size_t pages;
|
---|
[fc47885] | 220 |
|
---|
| 221 | /** Number of resident pages in the area. */
|
---|
| 222 | size_t resident;
|
---|
| 223 |
|
---|
[80bcaed] | 224 | /** Base address of this area. */
|
---|
| 225 | uintptr_t base;
|
---|
[fc47885] | 226 |
|
---|
[80bcaed] | 227 | /** Map of used space. */
|
---|
| 228 | btree_t used_space;
|
---|
[da1bafb] | 229 |
|
---|
[80bcaed] | 230 | /**
|
---|
[fc47885] | 231 | * If the address space area is shared. this is
|
---|
| 232 | * a reference to the share info structure.
|
---|
[80bcaed] | 233 | */
|
---|
| 234 | share_info_t *sh_info;
|
---|
[da1bafb] | 235 |
|
---|
[80bcaed] | 236 | /** Memory backend backing this address space area. */
|
---|
| 237 | struct mem_backend *backend;
|
---|
[da1bafb] | 238 |
|
---|
[0ee077ee] | 239 | /** Data to be used by the backend. */
|
---|
| 240 | mem_backend_data_t backend_data;
|
---|
[b3f8fb7] | 241 | } as_area_t;
|
---|
| 242 |
|
---|
| 243 | /** Address space area backend structure. */
|
---|
| 244 | typedef struct mem_backend {
|
---|
[03523dc] | 245 | bool (* create)(as_area_t *);
|
---|
| 246 | bool (* resize)(as_area_t *, size_t);
|
---|
| 247 | void (* share)(as_area_t *);
|
---|
| 248 | void (* destroy)(as_area_t *);
|
---|
| 249 |
|
---|
[da1bafb] | 250 | int (* page_fault)(as_area_t *, uintptr_t, pf_access_t);
|
---|
| 251 | void (* frame_free)(as_area_t *, uintptr_t, uintptr_t);
|
---|
[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 |
|
---|
[da1bafb] | 268 | extern as_area_t *as_area_create(as_t *, unsigned int, size_t, uintptr_t,
|
---|
| 269 | unsigned int, mem_backend_t *, mem_backend_data_t *);
|
---|
[9150781] | 270 | extern int as_area_destroy(as_t *, uintptr_t);
|
---|
[da1bafb] | 271 | extern int as_area_resize(as_t *, uintptr_t, size_t, unsigned int);
|
---|
| 272 | extern int as_area_share(as_t *, uintptr_t, size_t, as_t *, uintptr_t,
|
---|
| 273 | unsigned int);
|
---|
| 274 | extern int 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
|
---|
[da1bafb] | 285 | extern int 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
|
---|
[da1bafb] | 293 | extern int 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;
|
---|
[8182031] | 308 |
|
---|
[da1bafb] | 309 | /**
|
---|
[c98e6ee] | 310 | * This flags is passed when running the loader, otherwise elf_load()
|
---|
| 311 | * would return with a EE_LOADER error code.
|
---|
[da1bafb] | 312 | *
|
---|
[c98e6ee] | 313 | */
|
---|
[da1bafb] | 314 | #define ELD_F_NONE 0
|
---|
| 315 | #define ELD_F_LOADER 1
|
---|
[c98e6ee] | 316 |
|
---|
[da1bafb] | 317 | extern unsigned int elf_load(elf_header_t *, as_t *, unsigned int);
|
---|
[b3f8fb7] | 318 |
|
---|
[df0103f7] | 319 | /* Address space area related syscalls. */
|
---|
[96b02eb9] | 320 | extern sysarg_t sys_as_area_create(uintptr_t, size_t, unsigned int);
|
---|
| 321 | extern sysarg_t sys_as_area_resize(uintptr_t, size_t, unsigned int);
|
---|
| 322 | extern sysarg_t sys_as_area_change_flags(uintptr_t, unsigned int);
|
---|
| 323 | extern sysarg_t sys_as_area_destroy(uintptr_t);
|
---|
[0b37882] | 324 | extern sysarg_t sys_as_get_unmapped_area(uintptr_t, size_t);
|
---|
[7c23af9] | 325 |
|
---|
[64c2ad5] | 326 | /* Introspection functions. */
|
---|
[9150781] | 327 | extern void as_get_area_info(as_t *, as_area_info_t **, size_t *);
|
---|
| 328 | extern void as_print(as_t *);
|
---|
[64c2ad5] | 329 |
|
---|
[7c23af9] | 330 | #endif /* KERNEL */
|
---|
[df0103f7] | 331 |
|
---|
[f761f1eb] | 332 | #endif
|
---|
[b45c443] | 333 |
|
---|
[f47fd19] | 334 | /** @}
|
---|
[b45c443] | 335 | */
|
---|