source: mainline/kernel/generic/src/mm/page.c@ c868e2d

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

Add and apply page_mapping_make_global() on each span added to the
kernel non-identity. This ensures that the kernel non-identity will use
the same set of page tables in all address spaces.

  • Property mode set to 100644
File size: 7.1 KB
Line 
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
79/** Virtual operations for page subsystem. */
80page_mapping_operations_t *page_mapping_operations = NULL;
81
82void page_init(void)
83{
84 page_arch_init();
85}
86
87/** Map memory structure
88 *
89 * Identity-map memory structure
90 * considering possible crossings
91 * of page boundaries.
92 *
93 * @param addr Address of the structure.
94 * @param size Size of the structure.
95 *
96 */
97void map_structure(uintptr_t addr, size_t size)
98{
99 size_t length = size + (addr - (addr & ~(PAGE_SIZE - 1)));
100 size_t cnt = length / PAGE_SIZE + (length % PAGE_SIZE > 0);
101
102 size_t i;
103 for (i = 0; i < cnt; i++)
104 page_mapping_insert(AS_KERNEL, addr + i * PAGE_SIZE,
105 addr + i * PAGE_SIZE, PAGE_NOT_CACHEABLE | PAGE_WRITE);
106
107 /* Repel prefetched accesses to the old mapping. */
108 memory_barrier();
109}
110
111/** Insert mapping of page to frame.
112 *
113 * Map virtual address page to physical address frame
114 * using flags. Allocate and setup any missing page tables.
115 *
116 * @param as Address space to which page belongs.
117 * @param page Virtual address of the page to be mapped.
118 * @param frame Physical address of memory frame to which the mapping is
119 * done.
120 * @param flags Flags to be used for mapping.
121 *
122 */
123NO_TRACE void page_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame,
124 unsigned int flags)
125{
126 ASSERT(page_table_locked(as));
127
128 ASSERT(page_mapping_operations);
129 ASSERT(page_mapping_operations->mapping_insert);
130
131 page_mapping_operations->mapping_insert(as, page, frame, flags);
132
133 /* Repel prefetched accesses to the old mapping. */
134 memory_barrier();
135}
136
137/** Remove mapping of page.
138 *
139 * Remove any mapping of page within address space as.
140 * TLB shootdown should follow in order to make effects of
141 * this call visible.
142 *
143 * @param as Address space to which page belongs.
144 * @param page Virtual address of the page to be demapped.
145 *
146 */
147NO_TRACE void page_mapping_remove(as_t *as, uintptr_t page)
148{
149 ASSERT(page_table_locked(as));
150
151 ASSERT(page_mapping_operations);
152 ASSERT(page_mapping_operations->mapping_remove);
153
154 page_mapping_operations->mapping_remove(as, page);
155
156 /* Repel prefetched accesses to the old mapping. */
157 memory_barrier();
158}
159
160/** Find mapping for virtual page.
161 *
162 * @param as Address space to which page belongs.
163 * @param page Virtual page.
164 * @param nolock True if the page tables need not be locked.
165 *
166 * @return NULL if there is no such mapping; requested mapping
167 * otherwise.
168 *
169 */
170NO_TRACE pte_t *page_mapping_find(as_t *as, uintptr_t page, bool nolock)
171{
172 ASSERT(nolock || page_table_locked(as));
173
174 ASSERT(page_mapping_operations);
175 ASSERT(page_mapping_operations->mapping_find);
176
177 return page_mapping_operations->mapping_find(as, page, nolock);
178}
179
180/** Make the mapping shared by all page tables (not address spaces).
181 *
182 * @param base Starting virtual address of the range that is made global.
183 * @param size Size of the address range that is made global.
184 */
185void page_mapping_make_global(uintptr_t base, size_t size)
186{
187 ASSERT(page_mapping_operations);
188 ASSERT(page_mapping_operations->mapping_make_global);
189
190 return page_mapping_operations->mapping_make_global(base, size);
191}
192
193uintptr_t hw_map(uintptr_t physaddr, size_t size)
194{
195 uintptr_t virtaddr;
196 size_t asize;
197 pfn_t i;
198
199 asize = ALIGN_UP(size, PAGE_SIZE);
200 virtaddr = km_page_alloc(asize, PAGE_SIZE);
201
202 page_table_lock(AS_KERNEL, true);
203 for (i = 0; i < ADDR2PFN(asize); i++) {
204 uintptr_t addr = PFN2ADDR(i);
205 page_mapping_insert(AS_KERNEL, virtaddr + addr, physaddr + addr,
206 PAGE_NOT_CACHEABLE | PAGE_WRITE);
207 }
208 page_table_unlock(AS_KERNEL, true);
209
210 return virtaddr;
211}
212
213int page_find_mapping(uintptr_t virt, void **phys)
214{
215 mutex_lock(&AS->lock);
216
217 pte_t *pte = page_mapping_find(AS, virt, false);
218 if ((!PTE_VALID(pte)) || (!PTE_PRESENT(pte))) {
219 mutex_unlock(&AS->lock);
220 return ENOENT;
221 }
222
223 *phys = (void *) PTE_GET_FRAME(pte) +
224 (virt - ALIGN_DOWN(virt, PAGE_SIZE));
225
226 mutex_unlock(&AS->lock);
227
228 return EOK;
229}
230
231/** Syscall wrapper for getting mapping of a virtual page.
232 *
233 * @return EOK on success.
234 * @return ENOENT if no virtual address mapping found.
235 *
236 */
237sysarg_t sys_page_find_mapping(uintptr_t virt, void *phys_ptr)
238{
239 void *phys;
240 int rc = page_find_mapping(virt, &phys);
241 if (rc != EOK)
242 return rc;
243
244 rc = copy_to_uspace(phys_ptr, &phys, sizeof(phys));
245 return (sysarg_t) rc;
246}
247
248/** @}
249 */
Note: See TracBrowser for help on using the repository browser.