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

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

Merge mainline changes

Conflicts - trivial to solve
In kernel/generic/src/mm/page.c - sys_page_find_mapping does
not use locking when accessing page tables (hope that is correct).

  • Property mode set to 100644
File size: 6.2 KB
RevLine 
[f761f1eb]1/*
[df4ed85]2 * Copyright (c) 2001-2006 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
[cc73a8a1]29/** @addtogroup genericmm
[b45c443]30 * @{
31 */
32
[9179d0a]33/**
[b45c443]34 * @file
[da1bafb]35 * @brief Virtual Address Translation subsystem.
[9179d0a]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.
[e3ee9b9]40 * They however, define the single interface.
[da1bafb]41 *
[20d50a1]42 */
43
[80dabb8d]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.
[da1bafb]58 *
[80dabb8d]59 */
60
[f761f1eb]61#include <mm/page.h>
[b93d637]62#include <genarch/mm/page_ht.h>
63#include <genarch/mm/page_pt.h>
[f761f1eb]64#include <arch/mm/page.h>
[677a6d5]65#include <arch/mm/asid.h>
[fc1e4f6]66#include <mm/as.h>
[677a6d5]67#include <mm/frame.h>
[11e9061d]68#include <arch/barrier.h>
[d99c1d2]69#include <typedefs.h>
[ff9f858]70#include <arch/asm.h>
[9c0a9b3]71#include <memstr.h>
[6d7ffa65]72#include <debug.h>
[677a6d5]73#include <arch.h>
[b93d637]74#include <syscall/copy.h>
75#include <errno.h>
[6d7ffa65]76
77/** Virtual operations for page subsystem. */
[f5935ed]78page_mapping_operations_t *page_mapping_operations = NULL;
[9c0a9b3]79
[f761f1eb]80void page_init(void)
[db3341e]81{
[f761f1eb]82 page_arch_init();
83}
[74df77d]84
85/** Map memory structure
86 *
87 * Identity-map memory structure
88 * considering possible crossings
89 * of page boundaries.
90 *
[da1bafb]91 * @param addr Address of the structure.
92 * @param size Size of the structure.
93 *
[74df77d]94 */
[da1bafb]95void map_structure(uintptr_t addr, size_t size)
[74df77d]96{
[da1bafb]97 size_t length = size + (addr - (addr & ~(PAGE_SIZE - 1)));
98 size_t cnt = length / PAGE_SIZE + (length % PAGE_SIZE > 0);
99
100 size_t i;
[76cec1e]101 for (i = 0; i < cnt; i++)
[da1bafb]102 page_mapping_insert(AS_KERNEL, addr + i * PAGE_SIZE,
103 addr + i * PAGE_SIZE, PAGE_NOT_CACHEABLE | PAGE_WRITE);
104
[11e9061d]105 /* Repel prefetched accesses to the old mapping. */
106 memory_barrier();
[74df77d]107}
[ff9f858]108
[8f00329]109/** Insert mapping of page to frame.
[ff9f858]110 *
[9179d0a]111 * Map virtual address page to physical address frame
112 * using flags. Allocate and setup any missing page tables.
[ff9f858]113 *
[0ff03f3]114 * @param as Address space to which page belongs.
[da1bafb]115 * @param page Virtual address of the page to be mapped.
116 * @param frame Physical address of memory frame to which the mapping is
117 * done.
118 * @param flags Flags to be used for mapping.
119 *
[ff9f858]120 */
[97bdb4a]121NO_TRACE void page_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame,
[da1bafb]122 unsigned int flags)
[ff9f858]123{
[1d432f9]124 ASSERT(page_table_locked(as));
125
[f5935ed]126 ASSERT(page_mapping_operations);
127 ASSERT(page_mapping_operations->mapping_insert);
[1d432f9]128
[f5935ed]129 page_mapping_operations->mapping_insert(as, page, frame, flags);
[11e9061d]130
131 /* Repel prefetched accesses to the old mapping. */
132 memory_barrier();
[ff9f858]133}
[1084a784]134
[8f00329]135/** Remove mapping of page.
136 *
[9179d0a]137 * Remove any mapping of page within address space as.
[8f00329]138 * TLB shootdown should follow in order to make effects of
139 * this call visible.
140 *
[0ff03f3]141 * @param as Address space to which page belongs.
[da1bafb]142 * @param page Virtual address of the page to be demapped.
143 *
[8f00329]144 */
[97bdb4a]145NO_TRACE void page_mapping_remove(as_t *as, uintptr_t page)
[8f00329]146{
[1d432f9]147 ASSERT(page_table_locked(as));
148
[8f00329]149 ASSERT(page_mapping_operations);
150 ASSERT(page_mapping_operations->mapping_remove);
151
152 page_mapping_operations->mapping_remove(as, page);
[da1bafb]153
[11e9061d]154 /* Repel prefetched accesses to the old mapping. */
155 memory_barrier();
[8f00329]156}
157
[0ff03f3]158/** Find mapping for virtual page.
[1084a784]159 *
[0ff03f3]160 * @param as Address space to which page belongs.
161 * @param page Virtual page.
162 * @param nolock True if the page tables need not be locked.
[da1bafb]163 *
164 * @return NULL if there is no such mapping; requested mapping
165 * otherwise.
[1084a784]166 *
167 */
[0ff03f3]168NO_TRACE pte_t *page_mapping_find(as_t *as, uintptr_t page, bool nolock)
[1084a784]169{
[0ff03f3]170 ASSERT(nolock || page_table_locked(as));
[1d432f9]171
[f5935ed]172 ASSERT(page_mapping_operations);
173 ASSERT(page_mapping_operations->mapping_find);
[da1bafb]174
[235e6c7]175 return page_mapping_operations->mapping_find(as, page, nolock);
[1084a784]176}
[b45c443]177
[b93d637]178/** Syscall wrapper for getting mapping of a virtual page.
179 *
180 * @retval EOK Everything went find, @p uspace_frame and @p uspace_node
181 * contains correct values.
182 * @retval ENOENT Virtual address has no mapping.
183 */
184sysarg_t sys_page_find_mapping(uintptr_t virt_address,
185 uintptr_t *uspace_frame)
186{
187 mutex_lock(&AS->lock);
188
[df29f24]189 pte_t *pte = page_mapping_find(AS, virt_address, true);
[b93d637]190 if (!PTE_VALID(pte) || !PTE_PRESENT(pte)) {
191 mutex_unlock(&AS->lock);
192
193 return (sysarg_t) ENOENT;
194 }
195
196 uintptr_t phys_address = PTE_GET_FRAME(pte);
197
198 mutex_unlock(&AS->lock);
199
200 int rc = copy_to_uspace(uspace_frame,
201 &phys_address, sizeof(phys_address));
202 if (rc != EOK) {
203 return (sysarg_t) rc;
204 }
205
206 return EOK;
207}
208
[cc73a8a1]209/** @}
[b45c443]210 */
Note: See TracBrowser for help on using the repository browser.