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

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

Fix hw_map() by using the new km_alloc_page() interface.

  • Property mode set to 100644
File size: 6.7 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
180uintptr_t hw_map(uintptr_t physaddr, size_t size)
181{
182 uintptr_t virtaddr;
183 size_t asize;
184 pfn_t i;
185
186 asize = ALIGN_UP(size, PAGE_SIZE);
187 virtaddr = km_page_alloc(asize, PAGE_SIZE);
188
189 page_table_lock(AS_KERNEL, true);
190 for (i = 0; i < ADDR2PFN(asize); i++) {
191 uintptr_t addr = PFN2ADDR(i);
192 page_mapping_insert(AS_KERNEL, virtaddr + addr, physaddr + addr,
193 PAGE_NOT_CACHEABLE | PAGE_WRITE);
194 }
195 page_table_unlock(AS_KERNEL, true);
196
197 return virtaddr;
198}
199
200
201/** Syscall wrapper for getting mapping of a virtual page.
202 *
203 * @retval EOK Everything went find, @p uspace_frame and @p uspace_node
204 * contains correct values.
205 * @retval ENOENT Virtual address has no mapping.
206 */
207sysarg_t sys_page_find_mapping(uintptr_t virt_address,
208 uintptr_t *uspace_frame)
209{
210 mutex_lock(&AS->lock);
211
212 pte_t *pte = page_mapping_find(AS, virt_address, false);
213 if (!PTE_VALID(pte) || !PTE_PRESENT(pte)) {
214 mutex_unlock(&AS->lock);
215
216 return (sysarg_t) ENOENT;
217 }
218
219 uintptr_t phys_address = PTE_GET_FRAME(pte);
220
221 mutex_unlock(&AS->lock);
222
223 int rc = copy_to_uspace(uspace_frame,
224 &phys_address, sizeof(phys_address));
225 if (rc != EOK) {
226 return (sysarg_t) rc;
227 }
228
229 return EOK;
230}
231
232/** @}
233 */
Note: See TracBrowser for help on using the repository browser.