[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>
|
---|
[677a6d5] | 71 | #include <arch.h>
|
---|
[63e27ef] | 72 | #include <assert.h>
|
---|
[b93d637] | 73 | #include <syscall/copy.h>
|
---|
| 74 | #include <errno.h>
|
---|
[1c01e6c] | 75 | #include <align.h>
|
---|
[6d7ffa65] | 76 |
|
---|
| 77 | /** Virtual operations for page subsystem. */
|
---|
[f5935ed] | 78 | page_mapping_operations_t *page_mapping_operations = NULL;
|
---|
[9c0a9b3] | 79 |
|
---|
[f761f1eb] | 80 | void page_init(void)
|
---|
[db3341e] | 81 | {
|
---|
[f761f1eb] | 82 | page_arch_init();
|
---|
| 83 | }
|
---|
[74df77d] | 84 |
|
---|
[8f00329] | 85 | /** Insert mapping of page to frame.
|
---|
[ff9f858] | 86 | *
|
---|
[9179d0a] | 87 | * Map virtual address page to physical address frame
|
---|
| 88 | * using flags. Allocate and setup any missing page tables.
|
---|
[ff9f858] | 89 | *
|
---|
[0ff03f3] | 90 | * @param as Address space to which page belongs.
|
---|
[da1bafb] | 91 | * @param page Virtual address of the page to be mapped.
|
---|
| 92 | * @param frame Physical address of memory frame to which the mapping is
|
---|
| 93 | * done.
|
---|
| 94 | * @param flags Flags to be used for mapping.
|
---|
| 95 | *
|
---|
[ff9f858] | 96 | */
|
---|
[97bdb4a] | 97 | NO_TRACE void page_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame,
|
---|
[da1bafb] | 98 | unsigned int flags)
|
---|
[ff9f858] | 99 | {
|
---|
[63e27ef] | 100 | assert(page_table_locked(as));
|
---|
[a35b458] | 101 |
|
---|
[63e27ef] | 102 | assert(page_mapping_operations);
|
---|
| 103 | assert(page_mapping_operations->mapping_insert);
|
---|
[1d432f9] | 104 |
|
---|
[59fb782] | 105 | page_mapping_operations->mapping_insert(as, ALIGN_DOWN(page, PAGE_SIZE),
|
---|
| 106 | ALIGN_DOWN(frame, FRAME_SIZE), flags);
|
---|
[a35b458] | 107 |
|
---|
[11e9061d] | 108 | /* Repel prefetched accesses to the old mapping. */
|
---|
| 109 | memory_barrier();
|
---|
[ff9f858] | 110 | }
|
---|
[1084a784] | 111 |
|
---|
[8f00329] | 112 | /** Remove mapping of page.
|
---|
| 113 | *
|
---|
[9179d0a] | 114 | * Remove any mapping of page within address space as.
|
---|
[8f00329] | 115 | * TLB shootdown should follow in order to make effects of
|
---|
| 116 | * this call visible.
|
---|
| 117 | *
|
---|
[0ff03f3] | 118 | * @param as Address space to which page belongs.
|
---|
[da1bafb] | 119 | * @param page Virtual address of the page to be demapped.
|
---|
| 120 | *
|
---|
[8f00329] | 121 | */
|
---|
[97bdb4a] | 122 | NO_TRACE void page_mapping_remove(as_t *as, uintptr_t page)
|
---|
[8f00329] | 123 | {
|
---|
[63e27ef] | 124 | assert(page_table_locked(as));
|
---|
[a35b458] | 125 |
|
---|
[63e27ef] | 126 | assert(page_mapping_operations);
|
---|
| 127 | assert(page_mapping_operations->mapping_remove);
|
---|
[a35b458] | 128 |
|
---|
[59fb782] | 129 | page_mapping_operations->mapping_remove(as,
|
---|
| 130 | ALIGN_DOWN(page, PAGE_SIZE));
|
---|
[a35b458] | 131 |
|
---|
[11e9061d] | 132 | /* Repel prefetched accesses to the old mapping. */
|
---|
| 133 | memory_barrier();
|
---|
[8f00329] | 134 | }
|
---|
| 135 |
|
---|
[0ff03f3] | 136 | /** Find mapping for virtual page.
|
---|
[1084a784] | 137 | *
|
---|
[38dc82d] | 138 | * @param as Address space to which page belongs.
|
---|
| 139 | * @param page Virtual page.
|
---|
| 140 | * @param nolock True if the page tables need not be locked.
|
---|
| 141 | * @param[out] pte Structure that will receive a copy of the found PTE.
|
---|
[1084a784] | 142 | *
|
---|
[560b81c] | 143 | * @return True if a valid PTE is returned, false otherwise. Note that
|
---|
| 144 | * the PTE is not guaranteed to be present.
|
---|
[1084a784] | 145 | */
|
---|
[38dc82d] | 146 | NO_TRACE bool page_mapping_find(as_t *as, uintptr_t page, bool nolock,
|
---|
| 147 | pte_t *pte)
|
---|
[1084a784] | 148 | {
|
---|
[63e27ef] | 149 | assert(nolock || page_table_locked(as));
|
---|
[a35b458] | 150 |
|
---|
[63e27ef] | 151 | assert(page_mapping_operations);
|
---|
| 152 | assert(page_mapping_operations->mapping_find);
|
---|
[a35b458] | 153 |
|
---|
[59fb782] | 154 | return page_mapping_operations->mapping_find(as,
|
---|
[38dc82d] | 155 | ALIGN_DOWN(page, PAGE_SIZE), nolock, pte);
|
---|
[1084a784] | 156 | }
|
---|
[b45c443] | 157 |
|
---|
[346b12a2] | 158 | /** Update mapping for virtual page.
|
---|
| 159 | *
|
---|
| 160 | * Use only to update accessed and modified/dirty bits.
|
---|
| 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 | * @param pte New PTE.
|
---|
| 166 | */
|
---|
| 167 | NO_TRACE void page_mapping_update(as_t *as, uintptr_t page, bool nolock,
|
---|
| 168 | pte_t *pte)
|
---|
| 169 | {
|
---|
[63e27ef] | 170 | assert(nolock || page_table_locked(as));
|
---|
[a35b458] | 171 |
|
---|
[63e27ef] | 172 | assert(page_mapping_operations);
|
---|
| 173 | assert(page_mapping_operations->mapping_find);
|
---|
[a35b458] | 174 |
|
---|
[346b12a2] | 175 | page_mapping_operations->mapping_update(as,
|
---|
| 176 | ALIGN_DOWN(page, PAGE_SIZE), nolock, pte);
|
---|
| 177 | }
|
---|
| 178 |
|
---|
[c868e2d] | 179 | /** Make the mapping shared by all page tables (not address spaces).
|
---|
[1b20da0] | 180 | *
|
---|
[c868e2d] | 181 | * @param base Starting virtual address of the range that is made global.
|
---|
| 182 | * @param size Size of the address range that is made global.
|
---|
| 183 | */
|
---|
| 184 | void page_mapping_make_global(uintptr_t base, size_t size)
|
---|
| 185 | {
|
---|
[63e27ef] | 186 | assert(page_mapping_operations);
|
---|
| 187 | assert(page_mapping_operations->mapping_make_global);
|
---|
[a35b458] | 188 |
|
---|
[c868e2d] | 189 | return page_mapping_operations->mapping_make_global(base, size);
|
---|
| 190 | }
|
---|
| 191 |
|
---|
[b7fd2a0] | 192 | errno_t page_find_mapping(uintptr_t virt, uintptr_t *phys)
|
---|
[b93d637] | 193 | {
|
---|
[0428f77] | 194 | page_table_lock(AS, true);
|
---|
[a35b458] | 195 |
|
---|
[38dc82d] | 196 | pte_t pte;
|
---|
| 197 | bool found = page_mapping_find(AS, virt, false, &pte);
|
---|
| 198 | if (!found || !PTE_VALID(&pte) || !PTE_PRESENT(&pte)) {
|
---|
[0428f77] | 199 | page_table_unlock(AS, true);
|
---|
[6645a14] | 200 | return ENOENT;
|
---|
[b93d637] | 201 | }
|
---|
[a35b458] | 202 |
|
---|
[38dc82d] | 203 | *phys = PTE_GET_FRAME(&pte) +
|
---|
[6645a14] | 204 | (virt - ALIGN_DOWN(virt, PAGE_SIZE));
|
---|
[a35b458] | 205 |
|
---|
[0428f77] | 206 | page_table_unlock(AS, true);
|
---|
[a35b458] | 207 |
|
---|
[b93d637] | 208 | return EOK;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
[6645a14] | 211 | /** Syscall wrapper for getting mapping of a virtual page.
|
---|
| 212 | *
|
---|
| 213 | * @return EOK on success.
|
---|
| 214 | * @return ENOENT if no virtual address mapping found.
|
---|
| 215 | *
|
---|
| 216 | */
|
---|
[b7fd2a0] | 217 | sys_errno_t sys_page_find_mapping(uintptr_t virt, uintptr_t *phys_ptr)
|
---|
[6645a14] | 218 | {
|
---|
[8cbf1c3] | 219 | uintptr_t phys;
|
---|
[b7fd2a0] | 220 | errno_t rc = page_find_mapping(virt, &phys);
|
---|
[6645a14] | 221 | if (rc != EOK)
|
---|
| 222 | return rc;
|
---|
[a35b458] | 223 |
|
---|
[6645a14] | 224 | rc = copy_to_uspace(phys_ptr, &phys, sizeof(phys));
|
---|
[b7fd2a0] | 225 | return (sys_errno_t) rc;
|
---|
[6645a14] | 226 | }
|
---|
| 227 |
|
---|
[cc73a8a1] | 228 | /** @}
|
---|
[b45c443] | 229 | */
|
---|