[622f409] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 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 |
|
---|
[174156fd] | 29 | /** @addtogroup kernel_generic_mm
|
---|
[622f409] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
| 34 | * @file
|
---|
| 35 | * @brief Kernel virtual memory setup.
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | #include <mm/km.h>
|
---|
[55896b6] | 39 | #include <arch/mm/km.h>
|
---|
[63e27ef] | 40 | #include <assert.h>
|
---|
[c868e2d] | 41 | #include <mm/page.h>
|
---|
[f7f47a7] | 42 | #include <mm/frame.h>
|
---|
| 43 | #include <mm/asid.h>
|
---|
[55896b6] | 44 | #include <config.h>
|
---|
[29938b2] | 45 | #include <typedefs.h>
|
---|
[7c3e482] | 46 | #include <lib/ra.h>
|
---|
[f7f47a7] | 47 | #include <arch.h>
|
---|
[d4673296] | 48 | #include <align.h>
|
---|
| 49 | #include <macros.h>
|
---|
| 50 | #include <bitops.h>
|
---|
[1066041] | 51 | #include <proc/thread.h>
|
---|
[7c3e482] | 52 |
|
---|
| 53 | static ra_arena_t *km_ni_arena;
|
---|
[622f409] | 54 |
|
---|
[1b20da0] | 55 | #define DEFERRED_PAGES_MAX (PAGE_SIZE / sizeof(uintptr_t))
|
---|
[f7f47a7] | 56 |
|
---|
| 57 | /** Number of freed pages in the deferred buffer. */
|
---|
| 58 | static volatile unsigned deferred_pages;
|
---|
| 59 | /** Buffer of deferred freed pages. */
|
---|
| 60 | static uintptr_t deferred_page[DEFERRED_PAGES_MAX];
|
---|
| 61 |
|
---|
| 62 | /** Flush the buffer of deferred freed pages.
|
---|
| 63 | *
|
---|
| 64 | * @return Number of freed pages.
|
---|
| 65 | */
|
---|
| 66 | static unsigned km_flush_deferred(void)
|
---|
| 67 | {
|
---|
| 68 | unsigned i = 0;
|
---|
| 69 | ipl_t ipl;
|
---|
| 70 |
|
---|
| 71 | ipl = tlb_shootdown_start(TLB_INVL_ASID, ASID_KERNEL, 0, 0);
|
---|
| 72 |
|
---|
| 73 | for (i = 0; i < deferred_pages; i++) {
|
---|
| 74 | page_mapping_remove(AS_KERNEL, deferred_page[i]);
|
---|
| 75 | km_page_free(deferred_page[i], PAGE_SIZE);
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | tlb_invalidate_asid(ASID_KERNEL);
|
---|
| 79 |
|
---|
| 80 | as_invalidate_translation_cache(AS_KERNEL, 0, -1);
|
---|
| 81 | tlb_shootdown_finalize(ipl);
|
---|
| 82 |
|
---|
| 83 | return i;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[622f409] | 86 | /** Architecture dependent setup of identity-mapped kernel memory. */
|
---|
| 87 | void km_identity_init(void)
|
---|
| 88 | {
|
---|
[55896b6] | 89 | km_identity_arch_init();
|
---|
| 90 | config.identity_configured = true;
|
---|
[622f409] | 91 | }
|
---|
| 92 |
|
---|
| 93 | /** Architecture dependent setup of non-identity-mapped kernel memory. */
|
---|
| 94 | void km_non_identity_init(void)
|
---|
| 95 | {
|
---|
[7c3e482] | 96 | km_ni_arena = ra_arena_create();
|
---|
[63e27ef] | 97 | assert(km_ni_arena != NULL);
|
---|
[55896b6] | 98 | km_non_identity_arch_init();
|
---|
| 99 | config.non_identity_configured = true;
|
---|
[622f409] | 100 | }
|
---|
| 101 |
|
---|
[1b478f6] | 102 | bool km_is_non_identity(uintptr_t addr)
|
---|
| 103 | {
|
---|
| 104 | return km_is_non_identity_arch(addr);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[29938b2] | 107 | void km_non_identity_span_add(uintptr_t base, size_t size)
|
---|
| 108 | {
|
---|
[7c3e482] | 109 | bool span_added;
|
---|
| 110 |
|
---|
[c868e2d] | 111 | page_mapping_make_global(base, size);
|
---|
| 112 |
|
---|
[7c3e482] | 113 | span_added = ra_span_add(km_ni_arena, base, size);
|
---|
[63e27ef] | 114 | assert(span_added);
|
---|
[7c3e482] | 115 | }
|
---|
| 116 |
|
---|
| 117 | uintptr_t km_page_alloc(size_t size, size_t align)
|
---|
| 118 | {
|
---|
[300f4c4] | 119 | uintptr_t base;
|
---|
| 120 | if (ra_alloc(km_ni_arena, size, align, &base))
|
---|
| 121 | return base;
|
---|
| 122 | else
|
---|
[482f968] | 123 | panic("Kernel ran out of virtual address space.");
|
---|
[29938b2] | 124 | }
|
---|
| 125 |
|
---|
[7c3e482] | 126 | void km_page_free(uintptr_t page, size_t size)
|
---|
| 127 | {
|
---|
| 128 | ra_free(km_ni_arena, page, size);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[bf3dd35] | 131 | static uintptr_t
|
---|
[a1b9f63] | 132 | km_map_aligned(uintptr_t paddr, size_t size, size_t align, unsigned int flags)
|
---|
[d4673296] | 133 | {
|
---|
[adec5b45] | 134 | uintptr_t vaddr;
|
---|
| 135 | uintptr_t offs;
|
---|
[d4673296] | 136 |
|
---|
[a1b9f63] | 137 | if (align == KM_NATURAL_ALIGNMENT)
|
---|
| 138 | align = ispwr2(size) ? size : (1U << (fnzb(size) + 1));
|
---|
| 139 |
|
---|
[63e27ef] | 140 | assert(ALIGN_DOWN(paddr, FRAME_SIZE) == paddr);
|
---|
| 141 | assert(ALIGN_UP(size, FRAME_SIZE) == size);
|
---|
[a1b9f63] | 142 | assert(ispwr2(align));
|
---|
[03cdd2b] | 143 |
|
---|
[a1b9f63] | 144 | /* Enforce at least PAGE_SIZE alignment. */
|
---|
[03cdd2b] | 145 | vaddr = km_page_alloc(size, max(PAGE_SIZE, align));
|
---|
[d4673296] | 146 |
|
---|
| 147 | page_table_lock(AS_KERNEL, true);
|
---|
[03cdd2b] | 148 | for (offs = 0; offs < size; offs += PAGE_SIZE) {
|
---|
[adec5b45] | 149 | page_mapping_insert(AS_KERNEL, vaddr + offs, paddr + offs,
|
---|
| 150 | flags);
|
---|
[d4673296] | 151 | }
|
---|
| 152 | page_table_unlock(AS_KERNEL, true);
|
---|
[a35b458] | 153 |
|
---|
[adec5b45] | 154 | return vaddr;
|
---|
[d4673296] | 155 | }
|
---|
| 156 |
|
---|
[03cdd2b] | 157 | static void km_unmap_aligned(uintptr_t vaddr, size_t size)
|
---|
| 158 | {
|
---|
| 159 | uintptr_t offs;
|
---|
| 160 | ipl_t ipl;
|
---|
| 161 |
|
---|
[63e27ef] | 162 | assert(ALIGN_DOWN(vaddr, PAGE_SIZE) == vaddr);
|
---|
| 163 | assert(ALIGN_UP(size, PAGE_SIZE) == size);
|
---|
[03cdd2b] | 164 |
|
---|
| 165 | page_table_lock(AS_KERNEL, true);
|
---|
| 166 |
|
---|
| 167 | ipl = tlb_shootdown_start(TLB_INVL_ASID, ASID_KERNEL, 0, 0);
|
---|
| 168 |
|
---|
| 169 | for (offs = 0; offs < size; offs += PAGE_SIZE)
|
---|
| 170 | page_mapping_remove(AS_KERNEL, vaddr + offs);
|
---|
| 171 |
|
---|
| 172 | tlb_invalidate_asid(ASID_KERNEL);
|
---|
| 173 |
|
---|
| 174 | as_invalidate_translation_cache(AS_KERNEL, 0, -1);
|
---|
| 175 | tlb_shootdown_finalize(ipl);
|
---|
| 176 | page_table_unlock(AS_KERNEL, true);
|
---|
| 177 |
|
---|
[96d9cdd] | 178 | km_page_free(vaddr, size);
|
---|
[03cdd2b] | 179 | }
|
---|
| 180 |
|
---|
| 181 | /** Map a piece of physical address space into the virtual address space.
|
---|
| 182 | *
|
---|
| 183 | * @param paddr Physical address to be mapped. May be unaligned.
|
---|
| 184 | * @param size Size of area starting at paddr to be mapped.
|
---|
| 185 | * @param flags Protection flags to be used for the mapping.
|
---|
| 186 | *
|
---|
| 187 | * @return New virtual address mapped to paddr.
|
---|
| 188 | */
|
---|
[a1b9f63] | 189 | uintptr_t km_map(uintptr_t paddr, size_t size, size_t align, unsigned int flags)
|
---|
[babb57e] | 190 | {
|
---|
| 191 | uintptr_t page;
|
---|
[1b20da0] | 192 | size_t offs;
|
---|
[03cdd2b] | 193 |
|
---|
[1b20da0] | 194 | offs = paddr - ALIGN_DOWN(paddr, FRAME_SIZE);
|
---|
[03cdd2b] | 195 | page = km_map_aligned(ALIGN_DOWN(paddr, FRAME_SIZE),
|
---|
[a1b9f63] | 196 | ALIGN_UP(size + offs, FRAME_SIZE), align, flags);
|
---|
[babb57e] | 197 |
|
---|
| 198 | return page + offs;
|
---|
| 199 | }
|
---|
[d4673296] | 200 |
|
---|
[03cdd2b] | 201 | /** Unmap a piece of virtual address space.
|
---|
| 202 | *
|
---|
| 203 | * @param vaddr Virtual address to be unmapped. May be unaligned, but
|
---|
| 204 | * it must a value previously returned by km_map().
|
---|
| 205 | * @param size Size of area starting at vaddr to be unmapped.
|
---|
| 206 | */
|
---|
| 207 | void km_unmap(uintptr_t vaddr, size_t size)
|
---|
| 208 | {
|
---|
[1b20da0] | 209 | size_t offs;
|
---|
[03cdd2b] | 210 |
|
---|
[1b20da0] | 211 | offs = vaddr - ALIGN_DOWN(vaddr, PAGE_SIZE);
|
---|
[03cdd2b] | 212 | km_unmap_aligned(ALIGN_DOWN(vaddr, PAGE_SIZE),
|
---|
| 213 | ALIGN_UP(size + offs, PAGE_SIZE));
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | /** Unmap kernel non-identity page.
|
---|
[f7f47a7] | 217 | *
|
---|
| 218 | * @param[in] page Non-identity page to be unmapped.
|
---|
| 219 | */
|
---|
| 220 | static void km_unmap_deferred(uintptr_t page)
|
---|
| 221 | {
|
---|
| 222 | page_table_lock(AS_KERNEL, true);
|
---|
| 223 |
|
---|
| 224 | if (deferred_pages == DEFERRED_PAGES_MAX) {
|
---|
| 225 | (void) km_flush_deferred();
|
---|
| 226 | deferred_pages = 0;
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | deferred_page[deferred_pages++] = page;
|
---|
| 230 |
|
---|
| 231 | page_table_unlock(AS_KERNEL, true);
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | /** Create a temporary page.
|
---|
| 235 | *
|
---|
| 236 | * The page is mapped read/write to a newly allocated frame of physical memory.
|
---|
| 237 | * The page must be returned back to the system by a call to
|
---|
| 238 | * km_temporary_page_put().
|
---|
| 239 | *
|
---|
| 240 | * @param[inout] framep Pointer to a variable which will receive the physical
|
---|
| 241 | * address of the allocated frame.
|
---|
[338810f] | 242 | * @param[in] flags Frame allocation flags. FRAME_NONE, FRAME_NO_RESERVE
|
---|
| 243 | * and FRAME_ATOMIC bits are allowed.
|
---|
[f7f47a7] | 244 | * @return Virtual address of the allocated frame.
|
---|
| 245 | */
|
---|
| 246 | uintptr_t km_temporary_page_get(uintptr_t *framep, frame_flags_t flags)
|
---|
| 247 | {
|
---|
[63e27ef] | 248 | assert(THREAD);
|
---|
| 249 | assert(framep);
|
---|
| 250 | assert(!(flags & ~(FRAME_NO_RESERVE | FRAME_ATOMIC)));
|
---|
[a35b458] | 251 |
|
---|
[f7f47a7] | 252 | /*
|
---|
| 253 | * Allocate a frame, preferably from high memory.
|
---|
| 254 | */
|
---|
[e2a0d76] | 255 | uintptr_t page;
|
---|
[5c7be3e] | 256 | uintptr_t frame;
|
---|
| 257 |
|
---|
[482f968] | 258 | frame = frame_alloc(1, FRAME_HIGHMEM | flags, 0);
|
---|
| 259 | if (frame >= config.identity_size) {
|
---|
[a1b9f63] | 260 | page = km_map(frame, PAGE_SIZE, PAGE_SIZE,
|
---|
[1ccd0aa] | 261 | PAGE_READ | PAGE_WRITE | PAGE_CACHEABLE);
|
---|
[f7f47a7] | 262 | } else {
|
---|
| 263 | page = PA2KA(frame);
|
---|
| 264 | }
|
---|
[a35b458] | 265 |
|
---|
[f7f47a7] | 266 | *framep = frame;
|
---|
[e2a0d76] | 267 | return page;
|
---|
[f7f47a7] | 268 | }
|
---|
| 269 |
|
---|
| 270 | /** Destroy a temporary page.
|
---|
| 271 | *
|
---|
| 272 | * This function destroys a temporary page previously created by
|
---|
| 273 | * km_temporary_page_get(). The page destruction may be immediate or deferred.
|
---|
| 274 | * The frame mapped by the destroyed page is not freed.
|
---|
| 275 | *
|
---|
| 276 | * @param[in] page Temporary page to be destroyed.
|
---|
| 277 | */
|
---|
| 278 | void km_temporary_page_put(uintptr_t page)
|
---|
| 279 | {
|
---|
[63e27ef] | 280 | assert(THREAD);
|
---|
[f7f47a7] | 281 |
|
---|
| 282 | if (km_is_non_identity(page))
|
---|
| 283 | km_unmap_deferred(page);
|
---|
| 284 | }
|
---|
[7c3e482] | 285 |
|
---|
[622f409] | 286 | /** @}
|
---|
| 287 | */
|
---|