| 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/frame.h>
|
|---|
| 68 | #include <arch/barrier.h>
|
|---|
| 69 | #include <typedefs.h>
|
|---|
| 70 | #include <arch/asm.h>
|
|---|
| 71 | #include <memstr.h>
|
|---|
| 72 | #include <debug.h>
|
|---|
| 73 | #include <arch.h>
|
|---|
| 74 | #include <syscall/copy.h>
|
|---|
| 75 | #include <errno.h>
|
|---|
| 76 |
|
|---|
| 77 | /** Virtual operations for page subsystem. */
|
|---|
| 78 | page_mapping_operations_t *page_mapping_operations = NULL;
|
|---|
| 79 |
|
|---|
| 80 | void page_init(void)
|
|---|
| 81 | {
|
|---|
| 82 | page_arch_init();
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | /** Map memory structure
|
|---|
| 86 | *
|
|---|
| 87 | * Identity-map memory structure
|
|---|
| 88 | * considering possible crossings
|
|---|
| 89 | * of page boundaries.
|
|---|
| 90 | *
|
|---|
| 91 | * @param addr Address of the structure.
|
|---|
| 92 | * @param size Size of the structure.
|
|---|
| 93 | *
|
|---|
| 94 | */
|
|---|
| 95 | void map_structure(uintptr_t addr, size_t size)
|
|---|
| 96 | {
|
|---|
| 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;
|
|---|
| 101 | for (i = 0; i < cnt; i++)
|
|---|
| 102 | page_mapping_insert(AS_KERNEL, addr + i * PAGE_SIZE,
|
|---|
| 103 | addr + i * PAGE_SIZE, PAGE_NOT_CACHEABLE | PAGE_WRITE);
|
|---|
| 104 |
|
|---|
| 105 | /* Repel prefetched accesses to the old mapping. */
|
|---|
| 106 | memory_barrier();
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | /** Insert mapping of page to frame.
|
|---|
| 110 | *
|
|---|
| 111 | * Map virtual address page to physical address frame
|
|---|
| 112 | * using flags. Allocate and setup any missing page tables.
|
|---|
| 113 | *
|
|---|
| 114 | * @param as Address space to which page belongs.
|
|---|
| 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 | *
|
|---|
| 120 | */
|
|---|
| 121 | NO_TRACE void page_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame,
|
|---|
| 122 | unsigned int flags)
|
|---|
| 123 | {
|
|---|
| 124 | ASSERT(page_table_locked(as));
|
|---|
| 125 |
|
|---|
| 126 | ASSERT(page_mapping_operations);
|
|---|
| 127 | ASSERT(page_mapping_operations->mapping_insert);
|
|---|
| 128 |
|
|---|
| 129 | page_mapping_operations->mapping_insert(as, page, frame, flags);
|
|---|
| 130 |
|
|---|
| 131 | /* Repel prefetched accesses to the old mapping. */
|
|---|
| 132 | memory_barrier();
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | /** Remove mapping of page.
|
|---|
| 136 | *
|
|---|
| 137 | * Remove any mapping of page within address space as.
|
|---|
| 138 | * TLB shootdown should follow in order to make effects of
|
|---|
| 139 | * this call visible.
|
|---|
| 140 | *
|
|---|
| 141 | * @param as Address space to which page belongs.
|
|---|
| 142 | * @param page Virtual address of the page to be demapped.
|
|---|
| 143 | *
|
|---|
| 144 | */
|
|---|
| 145 | NO_TRACE void page_mapping_remove(as_t *as, uintptr_t page)
|
|---|
| 146 | {
|
|---|
| 147 | ASSERT(page_table_locked(as));
|
|---|
| 148 |
|
|---|
| 149 | ASSERT(page_mapping_operations);
|
|---|
| 150 | ASSERT(page_mapping_operations->mapping_remove);
|
|---|
| 151 |
|
|---|
| 152 | page_mapping_operations->mapping_remove(as, page);
|
|---|
| 153 |
|
|---|
| 154 | /* Repel prefetched accesses to the old mapping. */
|
|---|
| 155 | memory_barrier();
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | /** Find mapping for virtual page.
|
|---|
| 159 | *
|
|---|
| 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.
|
|---|
| 163 | *
|
|---|
| 164 | * @return NULL if there is no such mapping; requested mapping
|
|---|
| 165 | * otherwise.
|
|---|
| 166 | *
|
|---|
| 167 | */
|
|---|
| 168 | NO_TRACE pte_t *page_mapping_find(as_t *as, uintptr_t page, bool nolock)
|
|---|
| 169 | {
|
|---|
| 170 | ASSERT(nolock || page_table_locked(as));
|
|---|
| 171 |
|
|---|
| 172 | ASSERT(page_mapping_operations);
|
|---|
| 173 | ASSERT(page_mapping_operations->mapping_find);
|
|---|
| 174 |
|
|---|
| 175 | return page_mapping_operations->mapping_find(as, page, nolock);
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 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 | */
|
|---|
| 184 | sysarg_t sys_page_find_mapping(uintptr_t virt_address,
|
|---|
| 185 | uintptr_t *uspace_frame)
|
|---|
| 186 | {
|
|---|
| 187 | mutex_lock(&AS->lock);
|
|---|
| 188 |
|
|---|
| 189 | pte_t *pte = page_mapping_find(AS, virt_address, false);
|
|---|
| 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 |
|
|---|
| 209 | /** @}
|
|---|
| 210 | */
|
|---|