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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c520034 was fbcdeb8, checked in by Martin Decky <martin@…>, 14 years ago

Remove the two-phase way of creating virtual memory areas (first asking for a mappable address and then mapping it) which was prone to race conditions when two or more calls to as_get_mappable_page() and as_area_create() were interleaved. This for example caused the e1k driver to randomly fail.

The memory area related syscalls and IPC calls have all been altered to accept a special value (void *) -1, representing a demand to atomically search for a mappable address space "hole" and map to it.

Individual changes:

  • IPC_M_SHARE_OUT: the destination address space area is supplied by the kernel, the callee only specifies the lower bound

(the address is returned to the callee via a pointer in an IPC reply argument)

  • IPC_M_SHARE_IN: the destination address space ares is supplied by the kernel, the callee only specifies the lower bound

(the address is returned to the caller as usual via an IPC argument)

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