[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>
|
---|
[88cc71c0] | 47 | #include <adt/odict.h>
|
---|
[d4b5542] | 48 | #include <lib/elf.h>
|
---|
[1066041] | 49 | #include <arch.h>
|
---|
[78de83de] | 50 | #include <lib/refcount.h>
|
---|
[1066041] | 51 |
|
---|
[a6e55886] | 52 | #define AS CURRENT->as
|
---|
[1066041] | 53 |
|
---|
[80bcaed] | 54 | /**
|
---|
| 55 | * Defined to be true if user address space and kernel address space shadow each
|
---|
| 56 | * other.
|
---|
[da1bafb] | 57 | *
|
---|
[80bcaed] | 58 | */
|
---|
[da1bafb] | 59 | #define KERNEL_ADDRESS_SPACE_SHADOWED KERNEL_ADDRESS_SPACE_SHADOWED_ARCH
|
---|
[5a7d9d1] | 60 |
|
---|
[da1bafb] | 61 | #define KERNEL_ADDRESS_SPACE_START KERNEL_ADDRESS_SPACE_START_ARCH
|
---|
| 62 | #define KERNEL_ADDRESS_SPACE_END KERNEL_ADDRESS_SPACE_END_ARCH
|
---|
| 63 | #define USER_ADDRESS_SPACE_START USER_ADDRESS_SPACE_START_ARCH
|
---|
| 64 | #define USER_ADDRESS_SPACE_END USER_ADDRESS_SPACE_END_ARCH
|
---|
[f761f1eb] | 65 |
|
---|
[80bcaed] | 66 | /** Kernel address space. */
|
---|
[da1bafb] | 67 | #define FLAG_AS_KERNEL (1 << 0)
|
---|
[4512d7e] | 68 |
|
---|
[80bcaed] | 69 | /* Address space area attributes. */
|
---|
[da1bafb] | 70 | #define AS_AREA_ATTR_NONE 0
|
---|
| 71 | #define AS_AREA_ATTR_PARTIAL 1 /**< Not fully initialized area. */
|
---|
[8182031] | 72 |
|
---|
[80bcaed] | 73 | /** The page fault was resolved by as_page_fault(). */
|
---|
[1b20da0] | 74 | #define AS_PF_OK 0
|
---|
[da1bafb] | 75 |
|
---|
[80bcaed] | 76 | /** The page fault was caused by memcpy_from_uspace() or memcpy_to_uspace(). */
|
---|
[908bb96] | 77 | #define AS_PF_DEFER 1
|
---|
| 78 |
|
---|
| 79 | /** The page fault was not resolved by as_page_fault(). */
|
---|
| 80 | #define AS_PF_FAULT 2
|
---|
| 81 |
|
---|
| 82 | /** The page fault was not resolved by as_page_fault(). Non-verbose version. */
|
---|
| 83 | #define AS_PF_SILENT 3
|
---|
[80bcaed] | 84 |
|
---|
| 85 | /** Address space structure.
|
---|
| 86 | *
|
---|
| 87 | * as_t contains the list of as_areas of userspace accessible
|
---|
| 88 | * pages for one or more tasks. Ranges of kernel memory pages are not
|
---|
| 89 | * supposed to figure in the list as they are shared by all tasks and
|
---|
| 90 | * set up during system initialization.
|
---|
[da1bafb] | 91 | *
|
---|
[80bcaed] | 92 | */
|
---|
| 93 | typedef struct as {
|
---|
| 94 | /** Protected by asidlock. */
|
---|
| 95 | link_t inactive_as_with_asid_link;
|
---|
[a35b458] | 96 |
|
---|
[879585a3] | 97 | /**
|
---|
[fc47885] | 98 | * Number of processors on which this
|
---|
| 99 | * address space is active. Protected by
|
---|
| 100 | * asidlock.
|
---|
[879585a3] | 101 | */
|
---|
[98000fb] | 102 | size_t cpu_refcount;
|
---|
[a35b458] | 103 |
|
---|
[fc47885] | 104 | /** Address space identifier.
|
---|
| 105 | *
|
---|
| 106 | * Constant on architectures that do not
|
---|
| 107 | * support ASIDs. Protected by asidlock.
|
---|
| 108 | *
|
---|
[879585a3] | 109 | */
|
---|
| 110 | asid_t asid;
|
---|
[a35b458] | 111 |
|
---|
[fc47885] | 112 | /** Number of references (i.e. tasks that reference this as). */
|
---|
[78de83de] | 113 | atomic_refcount_t refcount;
|
---|
[a35b458] | 114 |
|
---|
[31d8e10] | 115 | mutex_t lock;
|
---|
[a35b458] | 116 |
|
---|
[88cc71c0] | 117 | /** Address space areas in this address space by base address.
|
---|
| 118 | *
|
---|
| 119 | * Members are of type as_area_t.
|
---|
| 120 | */
|
---|
| 121 | odict_t as_areas;
|
---|
[a35b458] | 122 |
|
---|
[80bcaed] | 123 | /** Non-generic content. */
|
---|
| 124 | as_genarch_t genarch;
|
---|
[a35b458] | 125 |
|
---|
[80bcaed] | 126 | /** Architecture specific content. */
|
---|
| 127 | as_arch_t arch;
|
---|
| 128 | } as_t;
|
---|
[b3f8fb7] | 129 |
|
---|
| 130 | typedef struct {
|
---|
[1433ecda] | 131 | pte_t *(*page_table_create)(unsigned int);
|
---|
| 132 | void (*page_table_destroy)(pte_t *);
|
---|
| 133 | void (*page_table_lock)(as_t *, bool);
|
---|
| 134 | void (*page_table_unlock)(as_t *, bool);
|
---|
| 135 | bool (*page_table_locked)(as_t *);
|
---|
[b3f8fb7] | 136 | } as_operations_t;
|
---|
[8182031] | 137 |
|
---|
[de0af3a] | 138 | /** Single anonymous page mapping. */
|
---|
| 139 | typedef struct {
|
---|
| 140 | /** Containing pagemap structure */
|
---|
| 141 | struct as_pagemap *pagemap;
|
---|
| 142 | /** Link to @c shinfo->pagemap ordered dictionary */
|
---|
| 143 | odlink_t lpagemap;
|
---|
| 144 | /** Virtual address */
|
---|
| 145 | uintptr_t vaddr;
|
---|
| 146 | /** Physical frame address */
|
---|
| 147 | uintptr_t frame;
|
---|
| 148 | } as_page_mapping_t;
|
---|
| 149 |
|
---|
| 150 | /** Map of anonymous pages in a shared area. */
|
---|
| 151 | typedef struct as_pagemap {
|
---|
| 152 | /**
|
---|
| 153 | * Dictionary ordered by virtual address. Members are of type
|
---|
| 154 | * as_page_mapping_t
|
---|
| 155 | */
|
---|
| 156 | odict_t map;
|
---|
| 157 | } as_pagemap_t;
|
---|
| 158 |
|
---|
[2fc3b2d] | 159 | /** Used space interval */
|
---|
| 160 | typedef struct {
|
---|
| 161 | /** Containing used_space structure */
|
---|
| 162 | struct used_space *used_space;
|
---|
| 163 | /** Link to @c used_space->ivals */
|
---|
| 164 | odlink_t lused_space;
|
---|
| 165 | /** First page address */
|
---|
| 166 | uintptr_t page;
|
---|
| 167 | /** Count of pages */
|
---|
| 168 | size_t count;
|
---|
| 169 | } used_space_ival_t;
|
---|
| 170 |
|
---|
| 171 | /** Map of used space in an address space area */
|
---|
| 172 | typedef struct used_space {
|
---|
| 173 | /**
|
---|
| 174 | * Dictionary of intervals by start address.
|
---|
| 175 | * Members are of type @c used_space_ival_t.
|
---|
| 176 | */
|
---|
| 177 | odict_t ivals;
|
---|
| 178 | /** Total number of used pages. */
|
---|
| 179 | size_t pages;
|
---|
| 180 | } used_space_t;
|
---|
| 181 |
|
---|
[80bcaed] | 182 | /**
|
---|
| 183 | * This structure contains information associated with the shared address space
|
---|
| 184 | * area.
|
---|
[da1bafb] | 185 | *
|
---|
[80bcaed] | 186 | */
|
---|
[0ee077ee] | 187 | typedef struct {
|
---|
[80bcaed] | 188 | /** This lock must be acquired only when the as_area lock is held. */
|
---|
[da1bafb] | 189 | mutex_t lock;
|
---|
[80bcaed] | 190 | /** This structure can be deallocated if refcount drops to 0. */
|
---|
[98000fb] | 191 | size_t refcount;
|
---|
[83b6ba9f] | 192 | /** True if the area has been ever shared. */
|
---|
| 193 | bool shared;
|
---|
| 194 |
|
---|
[de0af3a] | 195 | /** Complete map of anonymous pages of the shared area. */
|
---|
| 196 | as_pagemap_t pagemap;
|
---|
[83b6ba9f] | 197 |
|
---|
| 198 | /** Address space area backend. */
|
---|
| 199 | struct mem_backend *backend;
|
---|
| 200 | /** Address space area shared data. */
|
---|
| 201 | void *backend_shared_data;
|
---|
[0ee077ee] | 202 | } share_info_t;
|
---|
| 203 |
|
---|
[b3f8fb7] | 204 | /** Page fault access type. */
|
---|
| 205 | typedef enum {
|
---|
| 206 | PF_ACCESS_READ,
|
---|
| 207 | PF_ACCESS_WRITE,
|
---|
[c15b374] | 208 | PF_ACCESS_EXEC,
|
---|
| 209 | PF_ACCESS_UNKNOWN
|
---|
[b3f8fb7] | 210 | } pf_access_t;
|
---|
| 211 |
|
---|
| 212 | struct mem_backend;
|
---|
[0ee077ee] | 213 |
|
---|
| 214 | /** Backend data stored in address space area. */
|
---|
[b3f8fb7] | 215 | typedef union mem_backend_data {
|
---|
[75b139f] | 216 | /* anon_backend members */
|
---|
| 217 | struct {
|
---|
| 218 | };
|
---|
| 219 |
|
---|
[da1bafb] | 220 | /** elf_backend members */
|
---|
| 221 | struct {
|
---|
[d91488d] | 222 | uintptr_t elf_base;
|
---|
[127c957b] | 223 | elf_header_t *elf;
|
---|
| 224 | elf_segment_header_t *segment;
|
---|
| 225 | };
|
---|
[a35b458] | 226 |
|
---|
[da1bafb] | 227 | /** phys_backend members */
|
---|
| 228 | struct {
|
---|
[7f1c620] | 229 | uintptr_t base;
|
---|
[98000fb] | 230 | size_t frames;
|
---|
[c101dc0] | 231 | bool anonymous;
|
---|
[127c957b] | 232 | };
|
---|
[75b139f] | 233 |
|
---|
| 234 | /** user_backend members */
|
---|
| 235 | struct {
|
---|
[ae6021d] | 236 | as_area_pager_info_t pager_info;
|
---|
[75b139f] | 237 | };
|
---|
| 238 |
|
---|
[0ee077ee] | 239 | } mem_backend_data_t;
|
---|
[8182031] | 240 |
|
---|
| 241 | /** Address space area structure.
|
---|
| 242 | *
|
---|
| 243 | * Each as_area_t structure describes one contiguous area of virtual memory.
|
---|
[da1bafb] | 244 | *
|
---|
[8182031] | 245 | */
|
---|
[b3f8fb7] | 246 | typedef struct {
|
---|
[8182031] | 247 | mutex_t lock;
|
---|
[a35b458] | 248 |
|
---|
[80bcaed] | 249 | /** Containing address space. */
|
---|
[da1bafb] | 250 | as_t *as;
|
---|
[a35b458] | 251 |
|
---|
[88cc71c0] | 252 | /** Link to @c as->as_areas */
|
---|
| 253 | odlink_t las_areas;
|
---|
| 254 |
|
---|
[fc47885] | 255 | /** Memory flags. */
|
---|
[da1bafb] | 256 | unsigned int flags;
|
---|
[a35b458] | 257 |
|
---|
[fc47885] | 258 | /** Address space area attributes. */
|
---|
[da1bafb] | 259 | unsigned int attributes;
|
---|
[a35b458] | 260 |
|
---|
[fc47885] | 261 | /** Number of pages in the area. */
|
---|
[98000fb] | 262 | size_t pages;
|
---|
[a35b458] | 263 |
|
---|
[80bcaed] | 264 | /** Base address of this area. */
|
---|
| 265 | uintptr_t base;
|
---|
[a35b458] | 266 |
|
---|
[80bcaed] | 267 | /** Map of used space. */
|
---|
[2fc3b2d] | 268 | used_space_t used_space;
|
---|
[a35b458] | 269 |
|
---|
[80bcaed] | 270 | /**
|
---|
[fc47885] | 271 | * If the address space area is shared. this is
|
---|
| 272 | * a reference to the share info structure.
|
---|
[80bcaed] | 273 | */
|
---|
| 274 | share_info_t *sh_info;
|
---|
[a35b458] | 275 |
|
---|
[80bcaed] | 276 | /** Memory backend backing this address space area. */
|
---|
| 277 | struct mem_backend *backend;
|
---|
[a35b458] | 278 |
|
---|
[0ee077ee] | 279 | /** Data to be used by the backend. */
|
---|
| 280 | mem_backend_data_t backend_data;
|
---|
[b3f8fb7] | 281 | } as_area_t;
|
---|
| 282 |
|
---|
| 283 | /** Address space area backend structure. */
|
---|
| 284 | typedef struct mem_backend {
|
---|
[1433ecda] | 285 | bool (*create)(as_area_t *);
|
---|
| 286 | bool (*resize)(as_area_t *, size_t);
|
---|
| 287 | void (*share)(as_area_t *);
|
---|
| 288 | void (*destroy)(as_area_t *);
|
---|
[03523dc] | 289 |
|
---|
[1433ecda] | 290 | bool (*is_resizable)(as_area_t *);
|
---|
| 291 | bool (*is_shareable)(as_area_t *);
|
---|
[01029fc] | 292 |
|
---|
[1433ecda] | 293 | int (*page_fault)(as_area_t *, uintptr_t, pf_access_t);
|
---|
| 294 | void (*frame_free)(as_area_t *, uintptr_t, uintptr_t);
|
---|
[83b6ba9f] | 295 |
|
---|
[1433ecda] | 296 | bool (*create_shared_data)(as_area_t *);
|
---|
| 297 | void (*destroy_shared_data)(void *);
|
---|
[b3f8fb7] | 298 | } mem_backend_t;
|
---|
[8182031] | 299 |
|
---|
[fc1e4f6] | 300 | extern as_t *AS_KERNEL;
|
---|
[bd1deed] | 301 |
|
---|
[ef67bab] | 302 | extern as_operations_t *as_operations;
|
---|
[55b77d9] | 303 | extern list_t inactive_as_with_asid_list;
|
---|
[7e4e532] | 304 |
|
---|
[ef67bab] | 305 | extern void as_init(void);
|
---|
[482826d] | 306 |
|
---|
[da1bafb] | 307 | extern as_t *as_create(unsigned int);
|
---|
[0321109] | 308 | extern void as_hold(as_t *);
|
---|
| 309 | extern void as_release(as_t *);
|
---|
[9150781] | 310 | extern void as_switch(as_t *, as_t *);
|
---|
| 311 | extern int as_page_fault(uintptr_t, pf_access_t, istate_t *);
|
---|
[482826d] | 312 |
|
---|
[fbcdeb8] | 313 | extern as_area_t *as_area_create(as_t *, unsigned int, size_t, unsigned int,
|
---|
| 314 | mem_backend_t *, mem_backend_data_t *, uintptr_t *, uintptr_t);
|
---|
[b7fd2a0] | 315 | extern errno_t as_area_destroy(as_t *, uintptr_t);
|
---|
| 316 | extern errno_t as_area_resize(as_t *, uintptr_t, size_t, unsigned int);
|
---|
| 317 | extern errno_t as_area_share(as_t *, uintptr_t, size_t, as_t *, unsigned int,
|
---|
[fbcdeb8] | 318 | uintptr_t *, uintptr_t);
|
---|
[b7fd2a0] | 319 | extern errno_t as_area_change_flags(as_t *, unsigned int, uintptr_t);
|
---|
[88cc71c0] | 320 | extern as_area_t *as_area_first(as_t *);
|
---|
| 321 | extern as_area_t *as_area_next(as_area_t *);
|
---|
[482826d] | 322 |
|
---|
[de0af3a] | 323 | extern void as_pagemap_initialize(as_pagemap_t *);
|
---|
| 324 | extern void as_pagemap_finalize(as_pagemap_t *);
|
---|
| 325 | extern as_page_mapping_t *as_pagemap_first(as_pagemap_t *);
|
---|
| 326 | extern as_page_mapping_t *as_pagemap_next(as_page_mapping_t *);
|
---|
| 327 | extern errno_t as_pagemap_find(as_pagemap_t *, uintptr_t, uintptr_t *);
|
---|
| 328 | extern void as_pagemap_insert(as_pagemap_t *, uintptr_t, uintptr_t);
|
---|
| 329 | extern void as_pagemap_remove(as_page_mapping_t *);
|
---|
| 330 |
|
---|
[da1bafb] | 331 | extern unsigned int as_area_get_flags(as_area_t *);
|
---|
[9150781] | 332 | extern bool as_area_check_access(as_area_t *, pf_access_t);
|
---|
| 333 | extern size_t as_area_get_size(uintptr_t);
|
---|
[2fc3b2d] | 334 | extern used_space_ival_t *used_space_first(used_space_t *);
|
---|
| 335 | extern used_space_ival_t *used_space_next(used_space_ival_t *);
|
---|
| 336 | extern used_space_ival_t *used_space_find_gteq(used_space_t *, uintptr_t);
|
---|
| 337 | extern bool used_space_insert(used_space_t *, uintptr_t, size_t);
|
---|
[29b2bbf] | 338 |
|
---|
[4512d7e] | 339 | /* Interface to be implemented by architectures. */
|
---|
[da1bafb] | 340 |
|
---|
[29b2bbf] | 341 | #ifndef as_constructor_arch
|
---|
[b7fd2a0] | 342 | extern errno_t as_constructor_arch(as_t *, unsigned int);
|
---|
[29b2bbf] | 343 | #endif /* !def as_constructor_arch */
|
---|
[da1bafb] | 344 |
|
---|
[29b2bbf] | 345 | #ifndef as_destructor_arch
|
---|
[9150781] | 346 | extern int as_destructor_arch(as_t *);
|
---|
[29b2bbf] | 347 | #endif /* !def as_destructor_arch */
|
---|
[da1bafb] | 348 |
|
---|
[29b2bbf] | 349 | #ifndef as_create_arch
|
---|
[b7fd2a0] | 350 | extern errno_t as_create_arch(as_t *, unsigned int);
|
---|
[29b2bbf] | 351 | #endif /* !def as_create_arch */
|
---|
[da1bafb] | 352 |
|
---|
[20d50a1] | 353 | #ifndef as_install_arch
|
---|
[9150781] | 354 | extern void as_install_arch(as_t *);
|
---|
[20d50a1] | 355 | #endif /* !def as_install_arch */
|
---|
[da1bafb] | 356 |
|
---|
[57da95c] | 357 | #ifndef as_deinstall_arch
|
---|
[9150781] | 358 | extern void as_deinstall_arch(as_t *);
|
---|
[57da95c] | 359 | #endif /* !def as_deinstall_arch */
|
---|
[f761f1eb] | 360 |
|
---|
[b3f8fb7] | 361 | /* Backend declarations and functions. */
|
---|
[8182031] | 362 | extern mem_backend_t anon_backend;
|
---|
| 363 | extern mem_backend_t elf_backend;
|
---|
[0ee077ee] | 364 | extern mem_backend_t phys_backend;
|
---|
[75b139f] | 365 | extern mem_backend_t user_backend;
|
---|
[8182031] | 366 |
|
---|
[df0103f7] | 367 | /* Address space area related syscalls. */
|
---|
[6aeca0d] | 368 | extern sysarg_t sys_as_area_create(uintptr_t, size_t, unsigned int, uintptr_t,
|
---|
[ae6021d] | 369 | as_area_pager_info_t *);
|
---|
[b7fd2a0] | 370 | extern sys_errno_t sys_as_area_resize(uintptr_t, size_t, unsigned int);
|
---|
| 371 | extern sys_errno_t sys_as_area_change_flags(uintptr_t, unsigned int);
|
---|
[3b3fcf36] | 372 | extern sys_errno_t sys_as_area_get_info(uintptr_t, as_area_info_t *);
|
---|
[b7fd2a0] | 373 | extern sys_errno_t sys_as_area_destroy(uintptr_t);
|
---|
[7c23af9] | 374 |
|
---|
[64c2ad5] | 375 | /* Introspection functions. */
|
---|
[b389f95] | 376 | extern as_area_info_t *as_get_area_info(as_t *, size_t *);
|
---|
[9150781] | 377 | extern void as_print(as_t *);
|
---|
[64c2ad5] | 378 |
|
---|
[f761f1eb] | 379 | #endif
|
---|
[b45c443] | 380 |
|
---|
[f47fd19] | 381 | /** @}
|
---|
[b45c443] | 382 | */
|
---|