1 | /*
|
---|
2 | * Copyright (c) 2001-2006 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 genericmm
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file
|
---|
35 | * @brief Virtual Address Translation subsystem.
|
---|
36 | *
|
---|
37 | * This file contains code for creating, destroying and searching
|
---|
38 | * mappings between virtual addresses and physical addresses.
|
---|
39 | * Functions here are mere wrappers that call the real implementation.
|
---|
40 | * They however, define the single interface.
|
---|
41 | *
|
---|
42 | */
|
---|
43 |
|
---|
44 | /*
|
---|
45 | * Note on memory prefetching and updating memory mappings, also described in:
|
---|
46 | * AMD x86-64 Architecture Programmer's Manual, Volume 2, System Programming,
|
---|
47 | * 7.2.1 Special Coherency Considerations.
|
---|
48 | *
|
---|
49 | * The processor which modifies a page table mapping can access prefetched data
|
---|
50 | * from the old mapping. In order to prevent this, we place a memory barrier
|
---|
51 | * after a mapping is updated.
|
---|
52 | *
|
---|
53 | * We assume that the other processors are either not using the mapping yet
|
---|
54 | * (i.e. during the bootstrap) or are executing the TLB shootdown code. While
|
---|
55 | * we don't care much about the former case, the processors in the latter case
|
---|
56 | * will do an implicit serialization by virtue of running the TLB shootdown
|
---|
57 | * interrupt handler.
|
---|
58 | *
|
---|
59 | */
|
---|
60 |
|
---|
61 | #include <mm/page.h>
|
---|
62 | #include <genarch/mm/page_ht.h>
|
---|
63 | #include <genarch/mm/page_pt.h>
|
---|
64 | #include <arch/mm/page.h>
|
---|
65 | #include <arch/mm/asid.h>
|
---|
66 | #include <mm/as.h>
|
---|
67 | #include <mm/km.h>
|
---|
68 | #include <mm/frame.h>
|
---|
69 | #include <arch/barrier.h>
|
---|
70 | #include <typedefs.h>
|
---|
71 | #include <arch/asm.h>
|
---|
72 | #include <memstr.h>
|
---|
73 | #include <debug.h>
|
---|
74 | #include <arch.h>
|
---|
75 | #include <syscall/copy.h>
|
---|
76 | #include <errno.h>
|
---|
77 | #include <align.h>
|
---|
78 | #include <macros.h>
|
---|
79 | #include <bitops.h>
|
---|
80 |
|
---|
81 | /** Virtual operations for page subsystem. */
|
---|
82 | page_mapping_operations_t *page_mapping_operations = NULL;
|
---|
83 |
|
---|
84 | void page_init(void)
|
---|
85 | {
|
---|
86 | page_arch_init();
|
---|
87 | }
|
---|
88 |
|
---|
89 | /** Map memory structure
|
---|
90 | *
|
---|
91 | * Identity-map memory structure
|
---|
92 | * considering possible crossings
|
---|
93 | * of page boundaries.
|
---|
94 | *
|
---|
95 | * @param addr Address of the structure.
|
---|
96 | * @param size Size of the structure.
|
---|
97 | *
|
---|
98 | */
|
---|
99 | void map_structure(uintptr_t addr, size_t size)
|
---|
100 | {
|
---|
101 | size_t length = size + (addr - (addr & ~(PAGE_SIZE - 1)));
|
---|
102 | size_t cnt = length / PAGE_SIZE + (length % PAGE_SIZE > 0);
|
---|
103 |
|
---|
104 | size_t i;
|
---|
105 | for (i = 0; i < cnt; i++)
|
---|
106 | page_mapping_insert(AS_KERNEL, addr + i * PAGE_SIZE,
|
---|
107 | addr + i * PAGE_SIZE, PAGE_NOT_CACHEABLE | PAGE_WRITE);
|
---|
108 |
|
---|
109 | /* Repel prefetched accesses to the old mapping. */
|
---|
110 | memory_barrier();
|
---|
111 | }
|
---|
112 |
|
---|
113 | /** Insert mapping of page to frame.
|
---|
114 | *
|
---|
115 | * Map virtual address page to physical address frame
|
---|
116 | * using flags. Allocate and setup any missing page tables.
|
---|
117 | *
|
---|
118 | * @param as Address space to which page belongs.
|
---|
119 | * @param page Virtual address of the page to be mapped.
|
---|
120 | * @param frame Physical address of memory frame to which the mapping is
|
---|
121 | * done.
|
---|
122 | * @param flags Flags to be used for mapping.
|
---|
123 | *
|
---|
124 | */
|
---|
125 | NO_TRACE void page_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame,
|
---|
126 | unsigned int flags)
|
---|
127 | {
|
---|
128 | ASSERT(page_table_locked(as));
|
---|
129 |
|
---|
130 | ASSERT(page_mapping_operations);
|
---|
131 | ASSERT(page_mapping_operations->mapping_insert);
|
---|
132 |
|
---|
133 | page_mapping_operations->mapping_insert(as, page, frame, flags);
|
---|
134 |
|
---|
135 | /* Repel prefetched accesses to the old mapping. */
|
---|
136 | memory_barrier();
|
---|
137 | }
|
---|
138 |
|
---|
139 | /** Remove mapping of page.
|
---|
140 | *
|
---|
141 | * Remove any mapping of page within address space as.
|
---|
142 | * TLB shootdown should follow in order to make effects of
|
---|
143 | * this call visible.
|
---|
144 | *
|
---|
145 | * @param as Address space to which page belongs.
|
---|
146 | * @param page Virtual address of the page to be demapped.
|
---|
147 | *
|
---|
148 | */
|
---|
149 | NO_TRACE void page_mapping_remove(as_t *as, uintptr_t page)
|
---|
150 | {
|
---|
151 | ASSERT(page_table_locked(as));
|
---|
152 |
|
---|
153 | ASSERT(page_mapping_operations);
|
---|
154 | ASSERT(page_mapping_operations->mapping_remove);
|
---|
155 |
|
---|
156 | page_mapping_operations->mapping_remove(as, page);
|
---|
157 |
|
---|
158 | /* Repel prefetched accesses to the old mapping. */
|
---|
159 | memory_barrier();
|
---|
160 | }
|
---|
161 |
|
---|
162 | /** Find mapping for virtual page.
|
---|
163 | *
|
---|
164 | * @param as Address space to which page belongs.
|
---|
165 | * @param page Virtual page.
|
---|
166 | * @param nolock True if the page tables need not be locked.
|
---|
167 | *
|
---|
168 | * @return NULL if there is no such mapping; requested mapping
|
---|
169 | * otherwise.
|
---|
170 | *
|
---|
171 | */
|
---|
172 | NO_TRACE pte_t *page_mapping_find(as_t *as, uintptr_t page, bool nolock)
|
---|
173 | {
|
---|
174 | ASSERT(nolock || page_table_locked(as));
|
---|
175 |
|
---|
176 | ASSERT(page_mapping_operations);
|
---|
177 | ASSERT(page_mapping_operations->mapping_find);
|
---|
178 |
|
---|
179 | return page_mapping_operations->mapping_find(as, page, nolock);
|
---|
180 | }
|
---|
181 |
|
---|
182 | /** Make the mapping shared by all page tables (not address spaces).
|
---|
183 | *
|
---|
184 | * @param base Starting virtual address of the range that is made global.
|
---|
185 | * @param size Size of the address range that is made global.
|
---|
186 | */
|
---|
187 | void page_mapping_make_global(uintptr_t base, size_t size)
|
---|
188 | {
|
---|
189 | ASSERT(page_mapping_operations);
|
---|
190 | ASSERT(page_mapping_operations->mapping_make_global);
|
---|
191 |
|
---|
192 | return page_mapping_operations->mapping_make_global(base, size);
|
---|
193 | }
|
---|
194 |
|
---|
195 | uintptr_t hw_map(uintptr_t physaddr, size_t size)
|
---|
196 | {
|
---|
197 | uintptr_t virtaddr;
|
---|
198 | size_t asize;
|
---|
199 | size_t align;
|
---|
200 | pfn_t i;
|
---|
201 |
|
---|
202 | asize = ALIGN_UP(size, PAGE_SIZE);
|
---|
203 | align = ispwr2(size) ? size : (1U << (fnzb(size) + 1));
|
---|
204 | virtaddr = km_page_alloc(asize, align);
|
---|
205 |
|
---|
206 | page_table_lock(AS_KERNEL, true);
|
---|
207 | for (i = 0; i < ADDR2PFN(asize); i++) {
|
---|
208 | uintptr_t addr = PFN2ADDR(i);
|
---|
209 | page_mapping_insert(AS_KERNEL, virtaddr + addr, physaddr + addr,
|
---|
210 | PAGE_NOT_CACHEABLE | PAGE_WRITE);
|
---|
211 | }
|
---|
212 | page_table_unlock(AS_KERNEL, true);
|
---|
213 |
|
---|
214 | return virtaddr;
|
---|
215 | }
|
---|
216 |
|
---|
217 | int page_find_mapping(uintptr_t virt, void **phys)
|
---|
218 | {
|
---|
219 | mutex_lock(&AS->lock);
|
---|
220 |
|
---|
221 | pte_t *pte = page_mapping_find(AS, virt, false);
|
---|
222 | if ((!PTE_VALID(pte)) || (!PTE_PRESENT(pte))) {
|
---|
223 | mutex_unlock(&AS->lock);
|
---|
224 | return ENOENT;
|
---|
225 | }
|
---|
226 |
|
---|
227 | *phys = (void *) PTE_GET_FRAME(pte) +
|
---|
228 | (virt - ALIGN_DOWN(virt, PAGE_SIZE));
|
---|
229 |
|
---|
230 | mutex_unlock(&AS->lock);
|
---|
231 |
|
---|
232 | return EOK;
|
---|
233 | }
|
---|
234 |
|
---|
235 | /** Syscall wrapper for getting mapping of a virtual page.
|
---|
236 | *
|
---|
237 | * @return EOK on success.
|
---|
238 | * @return ENOENT if no virtual address mapping found.
|
---|
239 | *
|
---|
240 | */
|
---|
241 | sysarg_t sys_page_find_mapping(uintptr_t virt, void *phys_ptr)
|
---|
242 | {
|
---|
243 | void *phys;
|
---|
244 | int rc = page_find_mapping(virt, &phys);
|
---|
245 | if (rc != EOK)
|
---|
246 | return rc;
|
---|
247 |
|
---|
248 | rc = copy_to_uspace(phys_ptr, &phys, sizeof(phys));
|
---|
249 | return (sysarg_t) rc;
|
---|
250 | }
|
---|
251 |
|
---|
252 | /** @}
|
---|
253 | */
|
---|