[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 |
|
---|
| 29 | /** @addtogroup genericmm
|
---|
| 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>
|
---|
[c868e2d] | 40 | #include <mm/page.h>
|
---|
[f7f47a7] | 41 | #include <mm/frame.h>
|
---|
| 42 | #include <mm/asid.h>
|
---|
[55896b6] | 43 | #include <config.h>
|
---|
[29938b2] | 44 | #include <typedefs.h>
|
---|
[7c3e482] | 45 | #include <lib/ra.h>
|
---|
| 46 | #include <debug.h>
|
---|
[f7f47a7] | 47 | #include <arch.h>
|
---|
[d4673296] | 48 | #include <align.h>
|
---|
| 49 | #include <macros.h>
|
---|
| 50 | #include <bitops.h>
|
---|
[7c3e482] | 51 |
|
---|
| 52 | static ra_arena_t *km_ni_arena;
|
---|
[622f409] | 53 |
|
---|
[f7f47a7] | 54 | #define DEFERRED_PAGES_MAX (PAGE_SIZE / sizeof(uintptr_t))
|
---|
| 55 |
|
---|
| 56 | /** Number of freed pages in the deferred buffer. */
|
---|
| 57 | static volatile unsigned deferred_pages;
|
---|
| 58 | /** Buffer of deferred freed pages. */
|
---|
| 59 | static uintptr_t deferred_page[DEFERRED_PAGES_MAX];
|
---|
| 60 |
|
---|
| 61 | /** Flush the buffer of deferred freed pages.
|
---|
| 62 | *
|
---|
| 63 | * @return Number of freed pages.
|
---|
| 64 | */
|
---|
| 65 | static unsigned km_flush_deferred(void)
|
---|
| 66 | {
|
---|
| 67 | unsigned i = 0;
|
---|
| 68 | ipl_t ipl;
|
---|
| 69 |
|
---|
| 70 | ipl = tlb_shootdown_start(TLB_INVL_ASID, ASID_KERNEL, 0, 0);
|
---|
| 71 |
|
---|
| 72 | for (i = 0; i < deferred_pages; i++) {
|
---|
| 73 | page_mapping_remove(AS_KERNEL, deferred_page[i]);
|
---|
| 74 | km_page_free(deferred_page[i], PAGE_SIZE);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | tlb_invalidate_asid(ASID_KERNEL);
|
---|
| 78 |
|
---|
| 79 | as_invalidate_translation_cache(AS_KERNEL, 0, -1);
|
---|
| 80 | tlb_shootdown_finalize(ipl);
|
---|
| 81 |
|
---|
| 82 | return i;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[622f409] | 85 | /** Architecture dependent setup of identity-mapped kernel memory. */
|
---|
| 86 | void km_identity_init(void)
|
---|
| 87 | {
|
---|
[55896b6] | 88 | km_identity_arch_init();
|
---|
| 89 | config.identity_configured = true;
|
---|
[622f409] | 90 | }
|
---|
| 91 |
|
---|
| 92 | /** Architecture dependent setup of non-identity-mapped kernel memory. */
|
---|
| 93 | void km_non_identity_init(void)
|
---|
| 94 | {
|
---|
[7c3e482] | 95 | km_ni_arena = ra_arena_create();
|
---|
| 96 | ASSERT(km_ni_arena != NULL);
|
---|
[55896b6] | 97 | km_non_identity_arch_init();
|
---|
| 98 | config.non_identity_configured = true;
|
---|
[622f409] | 99 | }
|
---|
| 100 |
|
---|
[1b478f6] | 101 | bool km_is_non_identity(uintptr_t addr)
|
---|
| 102 | {
|
---|
| 103 | return km_is_non_identity_arch(addr);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[29938b2] | 106 | void km_non_identity_span_add(uintptr_t base, size_t size)
|
---|
| 107 | {
|
---|
[7c3e482] | 108 | bool span_added;
|
---|
| 109 |
|
---|
[c868e2d] | 110 | page_mapping_make_global(base, size);
|
---|
| 111 |
|
---|
[7c3e482] | 112 | span_added = ra_span_add(km_ni_arena, base, size);
|
---|
| 113 | ASSERT(span_added);
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | uintptr_t km_page_alloc(size_t size, size_t align)
|
---|
| 117 | {
|
---|
| 118 | return ra_alloc(km_ni_arena, size, align);
|
---|
[29938b2] | 119 | }
|
---|
| 120 |
|
---|
[7c3e482] | 121 | void km_page_free(uintptr_t page, size_t size)
|
---|
| 122 | {
|
---|
| 123 | ra_free(km_ni_arena, page, size);
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[adec5b45] | 126 | uintptr_t km_map(uintptr_t paddr, size_t size, unsigned int flags)
|
---|
[d4673296] | 127 | {
|
---|
[adec5b45] | 128 | uintptr_t vaddr;
|
---|
[d4673296] | 129 | size_t asize;
|
---|
| 130 | size_t align;
|
---|
[adec5b45] | 131 | uintptr_t offs;
|
---|
[d4673296] | 132 |
|
---|
| 133 | asize = ALIGN_UP(size, PAGE_SIZE);
|
---|
| 134 | align = ispwr2(size) ? size : (1U << (fnzb(size) + 1));
|
---|
[adec5b45] | 135 | vaddr = km_page_alloc(asize, max(PAGE_SIZE, align));
|
---|
[d4673296] | 136 |
|
---|
| 137 | page_table_lock(AS_KERNEL, true);
|
---|
[adec5b45] | 138 | for (offs = 0; offs < asize; offs += PAGE_SIZE) {
|
---|
| 139 | page_mapping_insert(AS_KERNEL, vaddr + offs, paddr + offs,
|
---|
| 140 | flags);
|
---|
[d4673296] | 141 | }
|
---|
| 142 | page_table_unlock(AS_KERNEL, true);
|
---|
| 143 |
|
---|
[adec5b45] | 144 | return vaddr;
|
---|
[d4673296] | 145 | }
|
---|
| 146 |
|
---|
| 147 |
|
---|
[f7f47a7] | 148 | /** Unmap kernen non-identity page.
|
---|
| 149 | *
|
---|
| 150 | * @param[in] page Non-identity page to be unmapped.
|
---|
| 151 | */
|
---|
| 152 | static void km_unmap_deferred(uintptr_t page)
|
---|
| 153 | {
|
---|
| 154 | page_table_lock(AS_KERNEL, true);
|
---|
| 155 |
|
---|
| 156 | if (deferred_pages == DEFERRED_PAGES_MAX) {
|
---|
| 157 | (void) km_flush_deferred();
|
---|
| 158 | deferred_pages = 0;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | deferred_page[deferred_pages++] = page;
|
---|
| 162 |
|
---|
| 163 | page_table_unlock(AS_KERNEL, true);
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | /** Create a temporary page.
|
---|
| 167 | *
|
---|
| 168 | * The page is mapped read/write to a newly allocated frame of physical memory.
|
---|
| 169 | * The page must be returned back to the system by a call to
|
---|
| 170 | * km_temporary_page_put().
|
---|
| 171 | *
|
---|
| 172 | * @param[inout] framep Pointer to a variable which will receive the physical
|
---|
| 173 | * address of the allocated frame.
|
---|
| 174 | * @param[in] flags Frame allocation flags. FRAME_NONE or FRAME_NO_RESERVE.
|
---|
| 175 | * @return Virtual address of the allocated frame.
|
---|
| 176 | */
|
---|
| 177 | uintptr_t km_temporary_page_get(uintptr_t *framep, frame_flags_t flags)
|
---|
| 178 | {
|
---|
| 179 | uintptr_t frame;
|
---|
| 180 | uintptr_t page;
|
---|
| 181 |
|
---|
| 182 | ASSERT(THREAD);
|
---|
| 183 | ASSERT(framep);
|
---|
| 184 | ASSERT(!(flags & ~FRAME_NO_RESERVE));
|
---|
| 185 |
|
---|
| 186 | /*
|
---|
| 187 | * Allocate a frame, preferably from high memory.
|
---|
| 188 | */
|
---|
| 189 | frame = (uintptr_t) frame_alloc(ONE_FRAME,
|
---|
| 190 | FRAME_HIGHMEM | FRAME_ATOMIC | flags);
|
---|
| 191 | if (frame) {
|
---|
[1ccd0aa] | 192 | page = km_map(frame, PAGE_SIZE,
|
---|
| 193 | PAGE_READ | PAGE_WRITE | PAGE_CACHEABLE);
|
---|
[f7f47a7] | 194 | ASSERT(page); // FIXME
|
---|
| 195 | } else {
|
---|
| 196 | frame = (uintptr_t) frame_alloc_noreserve(ONE_FRAME,
|
---|
| 197 | FRAME_LOWMEM);
|
---|
| 198 | page = PA2KA(frame);
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | *framep = frame;
|
---|
| 202 | return page;
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | /** Destroy a temporary page.
|
---|
| 206 | *
|
---|
| 207 | * This function destroys a temporary page previously created by
|
---|
| 208 | * km_temporary_page_get(). The page destruction may be immediate or deferred.
|
---|
| 209 | * The frame mapped by the destroyed page is not freed.
|
---|
| 210 | *
|
---|
| 211 | * @param[in] page Temporary page to be destroyed.
|
---|
| 212 | */
|
---|
| 213 | void km_temporary_page_put(uintptr_t page)
|
---|
| 214 | {
|
---|
| 215 | ASSERT(THREAD);
|
---|
| 216 |
|
---|
| 217 | if (km_is_non_identity(page))
|
---|
| 218 | km_unmap_deferred(page);
|
---|
| 219 | }
|
---|
[7c3e482] | 220 |
|
---|
[622f409] | 221 | /** @}
|
---|
| 222 | */
|
---|
| 223 |
|
---|