| [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>
|
|---|
| [404be7c] | 67 | #include <mm/km.h>
|
|---|
| [677a6d5] | 68 | #include <mm/frame.h>
|
|---|
| [11e9061d] | 69 | #include <arch/barrier.h>
|
|---|
| [d99c1d2] | 70 | #include <typedefs.h>
|
|---|
| [ff9f858] | 71 | #include <arch/asm.h>
|
|---|
| [9c0a9b3] | 72 | #include <memstr.h>
|
|---|
| [6d7ffa65] | 73 | #include <debug.h>
|
|---|
| [677a6d5] | 74 | #include <arch.h>
|
|---|
| [b93d637] | 75 | #include <syscall/copy.h>
|
|---|
| 76 | #include <errno.h>
|
|---|
| [1c01e6c] | 77 | #include <align.h>
|
|---|
| [6d7ffa65] | 78 |
|
|---|
| 79 | /** Virtual operations for page subsystem. */
|
|---|
| [f5935ed] | 80 | page_mapping_operations_t *page_mapping_operations = NULL;
|
|---|
| [9c0a9b3] | 81 |
|
|---|
| [f761f1eb] | 82 | void page_init(void)
|
|---|
| [db3341e] | 83 | {
|
|---|
| [f761f1eb] | 84 | page_arch_init();
|
|---|
| 85 | }
|
|---|
| [74df77d] | 86 |
|
|---|
| 87 | /** Map memory structure
|
|---|
| 88 | *
|
|---|
| 89 | * Identity-map memory structure
|
|---|
| 90 | * considering possible crossings
|
|---|
| 91 | * of page boundaries.
|
|---|
| 92 | *
|
|---|
| [da1bafb] | 93 | * @param addr Address of the structure.
|
|---|
| 94 | * @param size Size of the structure.
|
|---|
| 95 | *
|
|---|
| [74df77d] | 96 | */
|
|---|
| [da1bafb] | 97 | void map_structure(uintptr_t addr, size_t size)
|
|---|
| [74df77d] | 98 | {
|
|---|
| [da1bafb] | 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;
|
|---|
| [76cec1e] | 103 | for (i = 0; i < cnt; i++)
|
|---|
| [da1bafb] | 104 | page_mapping_insert(AS_KERNEL, addr + i * PAGE_SIZE,
|
|---|
| 105 | addr + i * PAGE_SIZE, PAGE_NOT_CACHEABLE | PAGE_WRITE);
|
|---|
| 106 |
|
|---|
| [11e9061d] | 107 | /* Repel prefetched accesses to the old mapping. */
|
|---|
| 108 | memory_barrier();
|
|---|
| [74df77d] | 109 | }
|
|---|
| [ff9f858] | 110 |
|
|---|
| [8f00329] | 111 | /** Insert mapping of page to frame.
|
|---|
| [ff9f858] | 112 | *
|
|---|
| [9179d0a] | 113 | * Map virtual address page to physical address frame
|
|---|
| 114 | * using flags. Allocate and setup any missing page tables.
|
|---|
| [ff9f858] | 115 | *
|
|---|
| [0ff03f3] | 116 | * @param as Address space to which page belongs.
|
|---|
| [da1bafb] | 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 | *
|
|---|
| [ff9f858] | 122 | */
|
|---|
| [97bdb4a] | 123 | NO_TRACE void page_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame,
|
|---|
| [da1bafb] | 124 | unsigned int flags)
|
|---|
| [ff9f858] | 125 | {
|
|---|
| [1d432f9] | 126 | ASSERT(page_table_locked(as));
|
|---|
| 127 |
|
|---|
| [f5935ed] | 128 | ASSERT(page_mapping_operations);
|
|---|
| 129 | ASSERT(page_mapping_operations->mapping_insert);
|
|---|
| [1d432f9] | 130 |
|
|---|
| [f5935ed] | 131 | page_mapping_operations->mapping_insert(as, page, frame, flags);
|
|---|
| [11e9061d] | 132 |
|
|---|
| 133 | /* Repel prefetched accesses to the old mapping. */
|
|---|
| 134 | memory_barrier();
|
|---|
| [ff9f858] | 135 | }
|
|---|
| [1084a784] | 136 |
|
|---|
| [8f00329] | 137 | /** Remove mapping of page.
|
|---|
| 138 | *
|
|---|
| [9179d0a] | 139 | * Remove any mapping of page within address space as.
|
|---|
| [8f00329] | 140 | * TLB shootdown should follow in order to make effects of
|
|---|
| 141 | * this call visible.
|
|---|
| 142 | *
|
|---|
| [0ff03f3] | 143 | * @param as Address space to which page belongs.
|
|---|
| [da1bafb] | 144 | * @param page Virtual address of the page to be demapped.
|
|---|
| 145 | *
|
|---|
| [8f00329] | 146 | */
|
|---|
| [97bdb4a] | 147 | NO_TRACE void page_mapping_remove(as_t *as, uintptr_t page)
|
|---|
| [8f00329] | 148 | {
|
|---|
| [1d432f9] | 149 | ASSERT(page_table_locked(as));
|
|---|
| 150 |
|
|---|
| [8f00329] | 151 | ASSERT(page_mapping_operations);
|
|---|
| 152 | ASSERT(page_mapping_operations->mapping_remove);
|
|---|
| 153 |
|
|---|
| 154 | page_mapping_operations->mapping_remove(as, page);
|
|---|
| [da1bafb] | 155 |
|
|---|
| [11e9061d] | 156 | /* Repel prefetched accesses to the old mapping. */
|
|---|
| 157 | memory_barrier();
|
|---|
| [8f00329] | 158 | }
|
|---|
| 159 |
|
|---|
| [0ff03f3] | 160 | /** Find mapping for virtual page.
|
|---|
| [1084a784] | 161 | *
|
|---|
| [0ff03f3] | 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.
|
|---|
| [da1bafb] | 165 | *
|
|---|
| 166 | * @return NULL if there is no such mapping; requested mapping
|
|---|
| 167 | * otherwise.
|
|---|
| [1084a784] | 168 | *
|
|---|
| 169 | */
|
|---|
| [0ff03f3] | 170 | NO_TRACE pte_t *page_mapping_find(as_t *as, uintptr_t page, bool nolock)
|
|---|
| [1084a784] | 171 | {
|
|---|
| [0ff03f3] | 172 | ASSERT(nolock || page_table_locked(as));
|
|---|
| [1d432f9] | 173 |
|
|---|
| [f5935ed] | 174 | ASSERT(page_mapping_operations);
|
|---|
| 175 | ASSERT(page_mapping_operations->mapping_find);
|
|---|
| [da1bafb] | 176 |
|
|---|
| [235e6c7] | 177 | return page_mapping_operations->mapping_find(as, page, nolock);
|
|---|
| [1084a784] | 178 | }
|
|---|
| [b45c443] | 179 |
|
|---|
| [1c01e6c] | 180 | uintptr_t hw_map(uintptr_t physaddr, size_t size)
|
|---|
| 181 | {
|
|---|
| [404be7c] | 182 | uintptr_t virtaddr;
|
|---|
| 183 | size_t asize;
|
|---|
| [1c01e6c] | 184 | pfn_t i;
|
|---|
| 185 |
|
|---|
| [404be7c] | 186 | asize = ALIGN_UP(size, PAGE_SIZE);
|
|---|
| 187 | virtaddr = km_page_alloc(asize, PAGE_SIZE);
|
|---|
| 188 |
|
|---|
| [1c01e6c] | 189 | page_table_lock(AS_KERNEL, true);
|
|---|
| [404be7c] | 190 | for (i = 0; i < ADDR2PFN(asize); i++) {
|
|---|
| [1c01e6c] | 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 |
|
|---|
| [b93d637] | 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 | */
|
|---|
| 207 | sysarg_t sys_page_find_mapping(uintptr_t virt_address,
|
|---|
| 208 | uintptr_t *uspace_frame)
|
|---|
| 209 | {
|
|---|
| 210 | mutex_lock(&AS->lock);
|
|---|
| 211 |
|
|---|
| [8d6c1f1] | 212 | pte_t *pte = page_mapping_find(AS, virt_address, false);
|
|---|
| [b93d637] | 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 |
|
|---|
| [cc73a8a1] | 232 | /** @}
|
|---|
| [b45c443] | 233 | */
|
|---|