source: mainline/kernel/generic/include/mm/as.h@ 666f492

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 666f492 was 0321109, checked in by Jakub Jermar <jakub@…>, 15 years ago

Add as_hold() and as_release().

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