source: mainline/kernel/generic/include/mm/as.h@ 207e8880

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 207e8880 was 69146b93, checked in by Adam Hraska <adam.hraska+hos@…>, 13 years ago

Merged mainline,1723.

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