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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e037cf37 was e037cf37, checked in by Jiri Svoboda <jiri@…>, 4 years ago

Show kernel console again when its physical area is unmapped

It's good to be able to see the stack trace if e.g. display server
crashes.

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