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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since dd0502ae was 84176f3, checked in by Jakub Jermář <jakub@…>, 7 years ago

arm64: Add support for the architecture

This changeset adds basic support to run HelenOS on AArch64, targeting
the QEMU virt platform.

Boot:

  • Boot relies on the EDK II firmware, GRUB2 for EFI and the HelenOS bootloader (UEFI application). EDK II loads GRUB2 from a CD, GRUB2 loads the HelenOS bootloader (via UEFI) which loads OS components.
  • UEFI applications use the PE/COFF format and must be relocatable. The first problem is solved by manually having the PE/COFF headers and tables written in assembler. The relocatable requirement is addressed by compiling the code with -fpic and having the bootloader relocate itself at its startup.

Kernel:

  • Kernel code for AArch64 consists mostly of stubbing out various architecture-specific hooks: virtual memory management, interrupt and exception handling, context switching (including FPU lazy switching), support for virtual timer, atomic sequences and barriers, cache and TLB maintenance, thread and process initialization.
  • The patch adds a kernel driver for GICv2 (interrupt controller).
  • The PL011 kernel driver is extended to allow userspace to take ownership of the console.
  • The current code is not able to dynamically obtain information about available devices on the underlying machine. The port instead implements a machine-func interface similar to the one implemented by arm32. It defines a machine for the QEMU AArch64 virt platform. The configuration (device addresses and IRQ numbers) is then baked into the machine definition.

User space:

  • Uspace code for AArch64 similarly mostly implements architecture-specific hooks: context saving/restoring, syscall support, TLS support.

The patchset allows to boot the system but user interaction with the OS
is not yet possible.

  • Property mode set to 100644
File size: 10.7 KB
Line 
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 kernel_generic_mm
30 * @{
31 */
32/** @file
33 */
34
35#ifndef KERN_AS_H_
36#define KERN_AS_H_
37
38#include <typedefs.h>
39#include <abi/mm/as.h>
40#include <arch/mm/page.h>
41#include <arch/mm/as.h>
42#include <arch/mm/asid.h>
43#include <arch/istate.h>
44#include <synch/spinlock.h>
45#include <synch/mutex.h>
46#include <adt/list.h>
47#include <adt/odict.h>
48#include <lib/elf.h>
49#include <arch.h>
50#include <lib/refcount.h>
51
52#define AS CURRENT->as
53
54/**
55 * Defined to be true if user address space and kernel address space shadow each
56 * other.
57 *
58 */
59#define KERNEL_ADDRESS_SPACE_SHADOWED KERNEL_ADDRESS_SPACE_SHADOWED_ARCH
60
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
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
71
72/** Kernel address space. */
73#define FLAG_AS_KERNEL (1 << 0)
74
75/* Address space area attributes. */
76#define AS_AREA_ATTR_NONE 0
77#define AS_AREA_ATTR_PARTIAL 1 /**< Not fully initialized area. */
78
79/** The page fault was resolved by as_page_fault(). */
80#define AS_PF_OK 0
81
82/** The page fault was caused by memcpy_from_uspace() or memcpy_to_uspace(). */
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
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.
97 *
98 */
99typedef struct as {
100 /** Protected by asidlock. */
101 link_t inactive_as_with_asid_link;
102
103 /**
104 * Number of processors on which this
105 * address space is active. Protected by
106 * asidlock.
107 */
108 size_t cpu_refcount;
109
110 /** Address space identifier.
111 *
112 * Constant on architectures that do not
113 * support ASIDs. Protected by asidlock.
114 *
115 */
116 asid_t asid;
117
118 /** Number of references (i.e. tasks that reference this as). */
119 atomic_refcount_t refcount;
120
121 mutex_t lock;
122
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;
128
129 /** Non-generic content. */
130 as_genarch_t genarch;
131
132 /** Architecture specific content. */
133 as_arch_t arch;
134} as_t;
135
136typedef struct {
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 *);
142} as_operations_t;
143
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
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
188/**
189 * This structure contains information associated with the shared address space
190 * area.
191 *
192 */
193typedef struct {
194 /** This lock must be acquired only when the as_area lock is held. */
195 mutex_t lock;
196 /** This structure can be deallocated if refcount drops to 0. */
197 size_t refcount;
198 /** True if the area has been ever shared. */
199 bool shared;
200
201 /** Complete map of anonymous pages of the shared area. */
202 as_pagemap_t pagemap;
203
204 /** Address space area backend. */
205 struct mem_backend *backend;
206 /** Address space area shared data. */
207 void *backend_shared_data;
208} share_info_t;
209
210/** Page fault access type. */
211typedef enum {
212 PF_ACCESS_READ,
213 PF_ACCESS_WRITE,
214 PF_ACCESS_EXEC,
215 PF_ACCESS_UNKNOWN
216} pf_access_t;
217
218struct mem_backend;
219
220/** Backend data stored in address space area. */
221typedef union mem_backend_data {
222 /* anon_backend members */
223 struct {
224 };
225
226 /** elf_backend members */
227 struct {
228 uintptr_t elf_base;
229 elf_header_t *elf;
230 elf_segment_header_t *segment;
231 };
232
233 /** phys_backend members */
234 struct {
235 uintptr_t base;
236 size_t frames;
237 bool anonymous;
238 };
239
240 /** user_backend members */
241 struct {
242 as_area_pager_info_t pager_info;
243 };
244
245} mem_backend_data_t;
246
247/** Address space area structure.
248 *
249 * Each as_area_t structure describes one contiguous area of virtual memory.
250 *
251 */
252typedef struct {
253 mutex_t lock;
254
255 /** Containing address space. */
256 as_t *as;
257
258 /** Link to @c as->as_areas */
259 odlink_t las_areas;
260
261 /** Memory flags. */
262 unsigned int flags;
263
264 /** Address space area attributes. */
265 unsigned int attributes;
266
267 /** Number of pages in the area. */
268 size_t pages;
269
270 /** Base address of this area. */
271 uintptr_t base;
272
273 /** Map of used space. */
274 used_space_t used_space;
275
276 /**
277 * If the address space area is shared. this is
278 * a reference to the share info structure.
279 */
280 share_info_t *sh_info;
281
282 /** Memory backend backing this address space area. */
283 struct mem_backend *backend;
284
285 /** Data to be used by the backend. */
286 mem_backend_data_t backend_data;
287} as_area_t;
288
289/** Address space area backend structure. */
290typedef struct mem_backend {
291 bool (*create)(as_area_t *);
292 bool (*resize)(as_area_t *, size_t);
293 void (*share)(as_area_t *);
294 void (*destroy)(as_area_t *);
295
296 bool (*is_resizable)(as_area_t *);
297 bool (*is_shareable)(as_area_t *);
298
299 int (*page_fault)(as_area_t *, uintptr_t, pf_access_t);
300 void (*frame_free)(as_area_t *, uintptr_t, uintptr_t);
301
302 bool (*create_shared_data)(as_area_t *);
303 void (*destroy_shared_data)(void *);
304} mem_backend_t;
305
306extern as_t *AS_KERNEL;
307
308extern as_operations_t *as_operations;
309extern list_t inactive_as_with_asid_list;
310
311extern void as_init(void);
312
313extern as_t *as_create(unsigned int);
314extern void as_hold(as_t *);
315extern void as_release(as_t *);
316extern void as_switch(as_t *, as_t *);
317extern int as_page_fault(uintptr_t, pf_access_t, istate_t *);
318
319extern as_area_t *as_area_create(as_t *, unsigned int, size_t, unsigned int,
320 mem_backend_t *, mem_backend_data_t *, uintptr_t *, uintptr_t);
321extern errno_t as_area_destroy(as_t *, uintptr_t);
322extern errno_t as_area_resize(as_t *, uintptr_t, size_t, unsigned int);
323extern errno_t as_area_share(as_t *, uintptr_t, size_t, as_t *, unsigned int,
324 uintptr_t *, uintptr_t);
325extern errno_t as_area_change_flags(as_t *, unsigned int, uintptr_t);
326extern as_area_t *as_area_first(as_t *);
327extern as_area_t *as_area_next(as_area_t *);
328
329extern void as_pagemap_initialize(as_pagemap_t *);
330extern void as_pagemap_finalize(as_pagemap_t *);
331extern as_page_mapping_t *as_pagemap_first(as_pagemap_t *);
332extern as_page_mapping_t *as_pagemap_next(as_page_mapping_t *);
333extern errno_t as_pagemap_find(as_pagemap_t *, uintptr_t, uintptr_t *);
334extern void as_pagemap_insert(as_pagemap_t *, uintptr_t, uintptr_t);
335extern void as_pagemap_remove(as_page_mapping_t *);
336
337extern unsigned int as_area_get_flags(as_area_t *);
338extern bool as_area_check_access(as_area_t *, pf_access_t);
339extern size_t as_area_get_size(uintptr_t);
340extern used_space_ival_t *used_space_first(used_space_t *);
341extern used_space_ival_t *used_space_next(used_space_ival_t *);
342extern used_space_ival_t *used_space_find_gteq(used_space_t *, uintptr_t);
343extern bool used_space_insert(used_space_t *, uintptr_t, size_t);
344
345/* Interface to be implemented by architectures. */
346
347#ifndef as_constructor_arch
348extern errno_t as_constructor_arch(as_t *, unsigned int);
349#endif /* !def as_constructor_arch */
350
351#ifndef as_destructor_arch
352extern int as_destructor_arch(as_t *);
353#endif /* !def as_destructor_arch */
354
355#ifndef as_create_arch
356extern errno_t as_create_arch(as_t *, unsigned int);
357#endif /* !def as_create_arch */
358
359#ifndef as_install_arch
360extern void as_install_arch(as_t *);
361#endif /* !def as_install_arch */
362
363#ifndef as_deinstall_arch
364extern void as_deinstall_arch(as_t *);
365#endif /* !def as_deinstall_arch */
366
367/* Backend declarations and functions. */
368extern mem_backend_t anon_backend;
369extern mem_backend_t elf_backend;
370extern mem_backend_t phys_backend;
371extern mem_backend_t user_backend;
372
373/* Address space area related syscalls. */
374extern sysarg_t sys_as_area_create(uintptr_t, size_t, unsigned int, uintptr_t,
375 as_area_pager_info_t *);
376extern sys_errno_t sys_as_area_resize(uintptr_t, size_t, unsigned int);
377extern sys_errno_t sys_as_area_change_flags(uintptr_t, unsigned int);
378extern sys_errno_t sys_as_area_get_info(uintptr_t, as_area_info_t *);
379extern sys_errno_t sys_as_area_destroy(uintptr_t);
380
381/* Introspection functions. */
382extern as_area_info_t *as_get_area_info(as_t *, size_t *);
383extern void as_print(as_t *);
384
385#endif
386
387/** @}
388 */
Note: See TracBrowser for help on using the repository browser.