[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
|
---|
[6645a14] | 55 | * we don't care much about the former case, the processors in the latter case
|
---|
[80dabb8d] | 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>
|
---|
[1c01e6c] | 76 | #include <align.h>
|
---|
[6d7ffa65] | 77 |
|
---|
| 78 | /** Virtual operations for page subsystem. */
|
---|
[f5935ed] | 79 | page_mapping_operations_t *page_mapping_operations = NULL;
|
---|
[9c0a9b3] | 80 |
|
---|
[f761f1eb] | 81 | void page_init(void)
|
---|
[db3341e] | 82 | {
|
---|
[f761f1eb] | 83 | page_arch_init();
|
---|
| 84 | }
|
---|
[74df77d] | 85 |
|
---|
[8f00329] | 86 | /** Insert mapping of page to frame.
|
---|
[ff9f858] | 87 | *
|
---|
[9179d0a] | 88 | * Map virtual address page to physical address frame
|
---|
| 89 | * using flags. Allocate and setup any missing page tables.
|
---|
[ff9f858] | 90 | *
|
---|
[0ff03f3] | 91 | * @param as Address space to which page belongs.
|
---|
[da1bafb] | 92 | * @param page Virtual address of the page to be mapped.
|
---|
| 93 | * @param frame Physical address of memory frame to which the mapping is
|
---|
| 94 | * done.
|
---|
| 95 | * @param flags Flags to be used for mapping.
|
---|
| 96 | *
|
---|
[ff9f858] | 97 | */
|
---|
[97bdb4a] | 98 | NO_TRACE void page_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame,
|
---|
[da1bafb] | 99 | unsigned int flags)
|
---|
[ff9f858] | 100 | {
|
---|
[1d432f9] | 101 | ASSERT(page_table_locked(as));
|
---|
| 102 |
|
---|
[f5935ed] | 103 | ASSERT(page_mapping_operations);
|
---|
| 104 | ASSERT(page_mapping_operations->mapping_insert);
|
---|
[1d432f9] | 105 |
|
---|
[59fb782] | 106 | page_mapping_operations->mapping_insert(as, ALIGN_DOWN(page, PAGE_SIZE),
|
---|
| 107 | ALIGN_DOWN(frame, FRAME_SIZE), flags);
|
---|
[11e9061d] | 108 |
|
---|
| 109 | /* Repel prefetched accesses to the old mapping. */
|
---|
| 110 | memory_barrier();
|
---|
[ff9f858] | 111 | }
|
---|
[1084a784] | 112 |
|
---|
[8f00329] | 113 | /** Remove mapping of page.
|
---|
| 114 | *
|
---|
[9179d0a] | 115 | * Remove any mapping of page within address space as.
|
---|
[8f00329] | 116 | * TLB shootdown should follow in order to make effects of
|
---|
| 117 | * this call visible.
|
---|
| 118 | *
|
---|
[0ff03f3] | 119 | * @param as Address space to which page belongs.
|
---|
[da1bafb] | 120 | * @param page Virtual address of the page to be demapped.
|
---|
| 121 | *
|
---|
[8f00329] | 122 | */
|
---|
[97bdb4a] | 123 | NO_TRACE void page_mapping_remove(as_t *as, uintptr_t page)
|
---|
[8f00329] | 124 | {
|
---|
[1d432f9] | 125 | ASSERT(page_table_locked(as));
|
---|
| 126 |
|
---|
[8f00329] | 127 | ASSERT(page_mapping_operations);
|
---|
| 128 | ASSERT(page_mapping_operations->mapping_remove);
|
---|
| 129 |
|
---|
[59fb782] | 130 | page_mapping_operations->mapping_remove(as,
|
---|
| 131 | ALIGN_DOWN(page, PAGE_SIZE));
|
---|
[da1bafb] | 132 |
|
---|
[11e9061d] | 133 | /* Repel prefetched accesses to the old mapping. */
|
---|
| 134 | memory_barrier();
|
---|
[8f00329] | 135 | }
|
---|
| 136 |
|
---|
[0ff03f3] | 137 | /** Find mapping for virtual page.
|
---|
[1084a784] | 138 | *
|
---|
[0ff03f3] | 139 | * @param as Address space to which page belongs.
|
---|
| 140 | * @param page Virtual page.
|
---|
| 141 | * @param nolock True if the page tables need not be locked.
|
---|
[da1bafb] | 142 | *
|
---|
| 143 | * @return NULL if there is no such mapping; requested mapping
|
---|
| 144 | * otherwise.
|
---|
[1084a784] | 145 | *
|
---|
| 146 | */
|
---|
[0ff03f3] | 147 | NO_TRACE pte_t *page_mapping_find(as_t *as, uintptr_t page, bool nolock)
|
---|
[1084a784] | 148 | {
|
---|
[0ff03f3] | 149 | ASSERT(nolock || page_table_locked(as));
|
---|
[1d432f9] | 150 |
|
---|
[f5935ed] | 151 | ASSERT(page_mapping_operations);
|
---|
| 152 | ASSERT(page_mapping_operations->mapping_find);
|
---|
[da1bafb] | 153 |
|
---|
[59fb782] | 154 | return page_mapping_operations->mapping_find(as,
|
---|
| 155 | ALIGN_DOWN(page, PAGE_SIZE), nolock);
|
---|
[1084a784] | 156 | }
|
---|
[b45c443] | 157 |
|
---|
[c868e2d] | 158 | /** Make the mapping shared by all page tables (not address spaces).
|
---|
| 159 | *
|
---|
| 160 | * @param base Starting virtual address of the range that is made global.
|
---|
| 161 | * @param size Size of the address range that is made global.
|
---|
| 162 | */
|
---|
| 163 | void page_mapping_make_global(uintptr_t base, size_t size)
|
---|
| 164 | {
|
---|
| 165 | ASSERT(page_mapping_operations);
|
---|
| 166 | ASSERT(page_mapping_operations->mapping_make_global);
|
---|
| 167 |
|
---|
| 168 | return page_mapping_operations->mapping_make_global(base, size);
|
---|
| 169 | }
|
---|
| 170 |
|
---|
[6645a14] | 171 | int page_find_mapping(uintptr_t virt, void **phys)
|
---|
[b93d637] | 172 | {
|
---|
[0428f77] | 173 | page_table_lock(AS, true);
|
---|
[b93d637] | 174 |
|
---|
[6645a14] | 175 | pte_t *pte = page_mapping_find(AS, virt, false);
|
---|
| 176 | if ((!PTE_VALID(pte)) || (!PTE_PRESENT(pte))) {
|
---|
[0428f77] | 177 | page_table_unlock(AS, true);
|
---|
[6645a14] | 178 | return ENOENT;
|
---|
[b93d637] | 179 | }
|
---|
| 180 |
|
---|
[6645a14] | 181 | *phys = (void *) PTE_GET_FRAME(pte) +
|
---|
| 182 | (virt - ALIGN_DOWN(virt, PAGE_SIZE));
|
---|
[b93d637] | 183 |
|
---|
[0428f77] | 184 | page_table_unlock(AS, true);
|
---|
[b93d637] | 185 |
|
---|
| 186 | return EOK;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
[6645a14] | 189 | /** Syscall wrapper for getting mapping of a virtual page.
|
---|
| 190 | *
|
---|
| 191 | * @return EOK on success.
|
---|
| 192 | * @return ENOENT if no virtual address mapping found.
|
---|
| 193 | *
|
---|
| 194 | */
|
---|
| 195 | sysarg_t sys_page_find_mapping(uintptr_t virt, void *phys_ptr)
|
---|
| 196 | {
|
---|
| 197 | void *phys;
|
---|
| 198 | int rc = page_find_mapping(virt, &phys);
|
---|
| 199 | if (rc != EOK)
|
---|
| 200 | return rc;
|
---|
| 201 |
|
---|
| 202 | rc = copy_to_uspace(phys_ptr, &phys, sizeof(phys));
|
---|
| 203 | return (sysarg_t) rc;
|
---|
| 204 | }
|
---|
| 205 |
|
---|
[cc73a8a1] | 206 | /** @}
|
---|
[b45c443] | 207 | */
|
---|