| 1 | /*
|
|---|
| 2 | * Copyright (c) 2010 Jakub Jermar
|
|---|
| 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 |
|
|---|
| 29 | /** @addtogroup genericmm
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #ifndef KERN_AS_H_
|
|---|
| 36 | #define KERN_AS_H_
|
|---|
| 37 |
|
|---|
| 38 | #ifdef KERNEL
|
|---|
| 39 | #include <typedefs.h>
|
|---|
| 40 | #else
|
|---|
| 41 | #include <sys/types.h>
|
|---|
| 42 | #endif
|
|---|
| 43 |
|
|---|
| 44 | /** Address space area flags. */
|
|---|
| 45 | #define AS_AREA_READ 1
|
|---|
| 46 | #define AS_AREA_WRITE 2
|
|---|
| 47 | #define AS_AREA_EXEC 4
|
|---|
| 48 | #define AS_AREA_CACHEABLE 8
|
|---|
| 49 |
|
|---|
| 50 | /** Address space area info exported to userspace. */
|
|---|
| 51 | typedef struct {
|
|---|
| 52 | /** Starting address */
|
|---|
| 53 | uintptr_t start_addr;
|
|---|
| 54 |
|
|---|
| 55 | /** Area size */
|
|---|
| 56 | size_t size;
|
|---|
| 57 |
|
|---|
| 58 | /** Area flags */
|
|---|
| 59 | unsigned int flags;
|
|---|
| 60 | } as_area_info_t;
|
|---|
| 61 |
|
|---|
| 62 | #ifdef KERNEL
|
|---|
| 63 |
|
|---|
| 64 | #include <arch/mm/page.h>
|
|---|
| 65 | #include <arch/mm/as.h>
|
|---|
| 66 | #include <arch/mm/asid.h>
|
|---|
| 67 | #include <typedefs.h>
|
|---|
| 68 | #include <synch/spinlock.h>
|
|---|
| 69 | #include <synch/mutex.h>
|
|---|
| 70 | #include <adt/list.h>
|
|---|
| 71 | #include <adt/btree.h>
|
|---|
| 72 | #include <lib/elf.h>
|
|---|
| 73 |
|
|---|
| 74 | /**
|
|---|
| 75 | * Defined to be true if user address space and kernel address space shadow each
|
|---|
| 76 | * other.
|
|---|
| 77 | *
|
|---|
| 78 | */
|
|---|
| 79 | #define KERNEL_ADDRESS_SPACE_SHADOWED KERNEL_ADDRESS_SPACE_SHADOWED_ARCH
|
|---|
| 80 |
|
|---|
| 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
|
|---|
| 85 |
|
|---|
| 86 | #define USTACK_ADDRESS USTACK_ADDRESS_ARCH
|
|---|
| 87 |
|
|---|
| 88 | /** Kernel address space. */
|
|---|
| 89 | #define FLAG_AS_KERNEL (1 << 0)
|
|---|
| 90 |
|
|---|
| 91 | /* Address space area attributes. */
|
|---|
| 92 | #define AS_AREA_ATTR_NONE 0
|
|---|
| 93 | #define AS_AREA_ATTR_PARTIAL 1 /**< Not fully initialized area. */
|
|---|
| 94 |
|
|---|
| 95 | /** The page fault was not resolved by as_page_fault(). */
|
|---|
| 96 | #define AS_PF_FAULT 0
|
|---|
| 97 |
|
|---|
| 98 | /** The page fault was resolved by as_page_fault(). */
|
|---|
| 99 | #define AS_PF_OK 1
|
|---|
| 100 |
|
|---|
| 101 | /** The page fault was caused by memcpy_from_uspace() or memcpy_to_uspace(). */
|
|---|
| 102 | #define AS_PF_DEFER 2
|
|---|
| 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.
|
|---|
| 110 | *
|
|---|
| 111 | */
|
|---|
| 112 | typedef struct as {
|
|---|
| 113 | /** Protected by asidlock. */
|
|---|
| 114 | link_t inactive_as_with_asid_link;
|
|---|
| 115 |
|
|---|
| 116 | /**
|
|---|
| 117 | * Number of processors on wich is this address space active.
|
|---|
| 118 | * Protected by asidlock.
|
|---|
| 119 | */
|
|---|
| 120 | size_t cpu_refcount;
|
|---|
| 121 |
|
|---|
| 122 | /**
|
|---|
| 123 | * Address space identifier.
|
|---|
| 124 | * Constant on architectures that do not support ASIDs.
|
|---|
| 125 | * Protected by asidlock.
|
|---|
| 126 | */
|
|---|
| 127 | asid_t asid;
|
|---|
| 128 |
|
|---|
| 129 | /** Number of references (i.e tasks that reference this as). */
|
|---|
| 130 | atomic_t refcount;
|
|---|
| 131 |
|
|---|
| 132 | mutex_t lock;
|
|---|
| 133 |
|
|---|
| 134 | /** B+tree of address space areas. */
|
|---|
| 135 | btree_t as_area_btree;
|
|---|
| 136 |
|
|---|
| 137 | /** Non-generic content. */
|
|---|
| 138 | as_genarch_t genarch;
|
|---|
| 139 |
|
|---|
| 140 | /** Architecture specific content. */
|
|---|
| 141 | as_arch_t arch;
|
|---|
| 142 | } as_t;
|
|---|
| 143 |
|
|---|
| 144 | typedef struct {
|
|---|
| 145 | pte_t *(* page_table_create)(unsigned int);
|
|---|
| 146 | void (* page_table_destroy)(pte_t *);
|
|---|
| 147 | void (* page_table_lock)(as_t *, bool);
|
|---|
| 148 | void (* page_table_unlock)(as_t *, bool);
|
|---|
| 149 | bool (* page_table_locked)(as_t *);
|
|---|
| 150 | } as_operations_t;
|
|---|
| 151 |
|
|---|
| 152 | /**
|
|---|
| 153 | * This structure contains information associated with the shared address space
|
|---|
| 154 | * area.
|
|---|
| 155 | *
|
|---|
| 156 | */
|
|---|
| 157 | typedef struct {
|
|---|
| 158 | /** This lock must be acquired only when the as_area lock is held. */
|
|---|
| 159 | mutex_t lock;
|
|---|
| 160 | /** This structure can be deallocated if refcount drops to 0. */
|
|---|
| 161 | size_t refcount;
|
|---|
| 162 |
|
|---|
| 163 | /**
|
|---|
| 164 | * B+tree containing complete map of anonymous pages of the shared area.
|
|---|
| 165 | */
|
|---|
| 166 | btree_t pagemap;
|
|---|
| 167 | } share_info_t;
|
|---|
| 168 |
|
|---|
| 169 | /** Page fault access type. */
|
|---|
| 170 | typedef enum {
|
|---|
| 171 | PF_ACCESS_READ,
|
|---|
| 172 | PF_ACCESS_WRITE,
|
|---|
| 173 | PF_ACCESS_EXEC
|
|---|
| 174 | } pf_access_t;
|
|---|
| 175 |
|
|---|
| 176 | struct mem_backend;
|
|---|
| 177 |
|
|---|
| 178 | /** Backend data stored in address space area. */
|
|---|
| 179 | typedef union mem_backend_data {
|
|---|
| 180 | /** elf_backend members */
|
|---|
| 181 | struct {
|
|---|
| 182 | elf_header_t *elf;
|
|---|
| 183 | elf_segment_header_t *segment;
|
|---|
| 184 | };
|
|---|
| 185 |
|
|---|
| 186 | /** phys_backend members */
|
|---|
| 187 | struct {
|
|---|
| 188 | uintptr_t base;
|
|---|
| 189 | size_t frames;
|
|---|
| 190 | };
|
|---|
| 191 | } mem_backend_data_t;
|
|---|
| 192 |
|
|---|
| 193 | /** Address space area structure.
|
|---|
| 194 | *
|
|---|
| 195 | * Each as_area_t structure describes one contiguous area of virtual memory.
|
|---|
| 196 | *
|
|---|
| 197 | */
|
|---|
| 198 | typedef struct {
|
|---|
| 199 | mutex_t lock;
|
|---|
| 200 | /** Containing address space. */
|
|---|
| 201 | as_t *as;
|
|---|
| 202 |
|
|---|
| 203 | /**
|
|---|
| 204 | * Flags related to the memory represented by the address space area.
|
|---|
| 205 | */
|
|---|
| 206 | unsigned int flags;
|
|---|
| 207 |
|
|---|
| 208 | /** Attributes related to the address space area itself. */
|
|---|
| 209 | unsigned int attributes;
|
|---|
| 210 | /** Size of this area in multiples of PAGE_SIZE. */
|
|---|
| 211 | size_t pages;
|
|---|
| 212 | /** Base address of this area. */
|
|---|
| 213 | uintptr_t base;
|
|---|
| 214 | /** Map of used space. */
|
|---|
| 215 | btree_t used_space;
|
|---|
| 216 |
|
|---|
| 217 | /**
|
|---|
| 218 | * If the address space area has been shared, this pointer will
|
|---|
| 219 | * reference the share info structure.
|
|---|
| 220 | */
|
|---|
| 221 | share_info_t *sh_info;
|
|---|
| 222 |
|
|---|
| 223 | /** Memory backend backing this address space area. */
|
|---|
| 224 | struct mem_backend *backend;
|
|---|
| 225 |
|
|---|
| 226 | /** Data to be used by the backend. */
|
|---|
| 227 | mem_backend_data_t backend_data;
|
|---|
| 228 | } as_area_t;
|
|---|
| 229 |
|
|---|
| 230 | /** Address space area backend structure. */
|
|---|
| 231 | typedef struct mem_backend {
|
|---|
| 232 | int (* page_fault)(as_area_t *, uintptr_t, pf_access_t);
|
|---|
| 233 | void (* frame_free)(as_area_t *, uintptr_t, uintptr_t);
|
|---|
| 234 | void (* share)(as_area_t *);
|
|---|
| 235 | } mem_backend_t;
|
|---|
| 236 |
|
|---|
| 237 | extern as_t *AS_KERNEL;
|
|---|
| 238 |
|
|---|
| 239 | extern as_operations_t *as_operations;
|
|---|
| 240 | extern link_t inactive_as_with_asid_head;
|
|---|
| 241 |
|
|---|
| 242 | extern void as_init(void);
|
|---|
| 243 |
|
|---|
| 244 | extern as_t *as_create(unsigned int);
|
|---|
| 245 | extern void as_destroy(as_t *);
|
|---|
| 246 | extern void as_hold(as_t *);
|
|---|
| 247 | extern void as_release(as_t *);
|
|---|
| 248 | extern void as_switch(as_t *, as_t *);
|
|---|
| 249 | extern int as_page_fault(uintptr_t, pf_access_t, istate_t *);
|
|---|
| 250 |
|
|---|
| 251 | extern as_area_t *as_area_create(as_t *, unsigned int, size_t, uintptr_t,
|
|---|
| 252 | unsigned int, mem_backend_t *, mem_backend_data_t *);
|
|---|
| 253 | extern int as_area_destroy(as_t *, uintptr_t);
|
|---|
| 254 | extern int as_area_resize(as_t *, uintptr_t, size_t, unsigned int);
|
|---|
| 255 | extern int as_area_share(as_t *, uintptr_t, size_t, as_t *, uintptr_t,
|
|---|
| 256 | unsigned int);
|
|---|
| 257 | extern int as_area_change_flags(as_t *, unsigned int, uintptr_t);
|
|---|
| 258 |
|
|---|
| 259 | extern unsigned int as_area_get_flags(as_area_t *);
|
|---|
| 260 | extern bool as_area_check_access(as_area_t *, pf_access_t);
|
|---|
| 261 | extern size_t as_area_get_size(uintptr_t);
|
|---|
| 262 | extern int used_space_insert(as_area_t *, uintptr_t, size_t);
|
|---|
| 263 | extern int used_space_remove(as_area_t *, uintptr_t, size_t);
|
|---|
| 264 |
|
|---|
| 265 |
|
|---|
| 266 | /* Interface to be implemented by architectures. */
|
|---|
| 267 |
|
|---|
| 268 | #ifndef as_constructor_arch
|
|---|
| 269 | extern int as_constructor_arch(as_t *, unsigned int);
|
|---|
| 270 | #endif /* !def as_constructor_arch */
|
|---|
| 271 |
|
|---|
| 272 | #ifndef as_destructor_arch
|
|---|
| 273 | extern int as_destructor_arch(as_t *);
|
|---|
| 274 | #endif /* !def as_destructor_arch */
|
|---|
| 275 |
|
|---|
| 276 | #ifndef as_create_arch
|
|---|
| 277 | extern int as_create_arch(as_t *, unsigned int);
|
|---|
| 278 | #endif /* !def as_create_arch */
|
|---|
| 279 |
|
|---|
| 280 | #ifndef as_install_arch
|
|---|
| 281 | extern void as_install_arch(as_t *);
|
|---|
| 282 | #endif /* !def as_install_arch */
|
|---|
| 283 |
|
|---|
| 284 | #ifndef as_deinstall_arch
|
|---|
| 285 | extern void as_deinstall_arch(as_t *);
|
|---|
| 286 | #endif /* !def as_deinstall_arch */
|
|---|
| 287 |
|
|---|
| 288 | /* Backend declarations and functions. */
|
|---|
| 289 | extern mem_backend_t anon_backend;
|
|---|
| 290 | extern mem_backend_t elf_backend;
|
|---|
| 291 | extern mem_backend_t phys_backend;
|
|---|
| 292 |
|
|---|
| 293 | /**
|
|---|
| 294 | * This flags is passed when running the loader, otherwise elf_load()
|
|---|
| 295 | * would return with a EE_LOADER error code.
|
|---|
| 296 | *
|
|---|
| 297 | */
|
|---|
| 298 | #define ELD_F_NONE 0
|
|---|
| 299 | #define ELD_F_LOADER 1
|
|---|
| 300 |
|
|---|
| 301 | extern unsigned int elf_load(elf_header_t *, as_t *, unsigned int);
|
|---|
| 302 |
|
|---|
| 303 | /* Address space area related syscalls. */
|
|---|
| 304 | extern unative_t sys_as_area_create(uintptr_t, size_t, unsigned int);
|
|---|
| 305 | extern unative_t sys_as_area_resize(uintptr_t, size_t, unsigned int);
|
|---|
| 306 | extern unative_t sys_as_area_change_flags(uintptr_t, unsigned int);
|
|---|
| 307 | extern unative_t sys_as_area_destroy(uintptr_t);
|
|---|
| 308 |
|
|---|
| 309 | /* Introspection functions. */
|
|---|
| 310 | extern void as_get_area_info(as_t *, as_area_info_t **, size_t *);
|
|---|
| 311 | extern void as_print(as_t *);
|
|---|
| 312 |
|
|---|
| 313 | #endif /* KERNEL */
|
|---|
| 314 |
|
|---|
| 315 | #endif
|
|---|
| 316 |
|
|---|
| 317 | /** @}
|
|---|
| 318 | */
|
|---|