| 1 | /*
|
|---|
| 2 | * Copyright (c) 2005 Jakub Jermar
|
|---|
| 3 | * Copyright (c) 2008 Pavel Rimsky
|
|---|
| 4 | * All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 7 | * modification, are permitted provided that the following conditions
|
|---|
| 8 | * are met:
|
|---|
| 9 | *
|
|---|
| 10 | * - Redistributions of source code must retain the above copyright
|
|---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 14 | * documentation and/or other materials provided with the distribution.
|
|---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 16 | * derived from this software without specific prior written permission.
|
|---|
| 17 | *
|
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | /** @addtogroup sparc64mm
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /** @file
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <mm/tlb.h>
|
|---|
| 37 | #include <mm/as.h>
|
|---|
| 38 | #include <mm/asid.h>
|
|---|
| 39 | #include <arch/sun4v/hypercall.h>
|
|---|
| 40 | #include <arch/mm/frame.h>
|
|---|
| 41 | #include <arch/mm/page.h>
|
|---|
| 42 | #include <arch/mm/tte.h>
|
|---|
| 43 | #include <arch/mm/tlb.h>
|
|---|
| 44 | #include <arch/interrupt.h>
|
|---|
| 45 | #include <interrupt.h>
|
|---|
| 46 | #include <arch.h>
|
|---|
| 47 | #include <print.h>
|
|---|
| 48 | #include <log.h>
|
|---|
| 49 | #include <typedefs.h>
|
|---|
| 50 | #include <config.h>
|
|---|
| 51 | #include <arch/trap/trap.h>
|
|---|
| 52 | #include <arch/trap/exception.h>
|
|---|
| 53 | #include <panic.h>
|
|---|
| 54 | #include <arch/asm.h>
|
|---|
| 55 | #include <arch/cpu.h>
|
|---|
| 56 | #include <arch/mm/pagesize.h>
|
|---|
| 57 | #include <genarch/mm/page_ht.h>
|
|---|
| 58 |
|
|---|
| 59 | #ifdef CONFIG_TSB
|
|---|
| 60 | #include <arch/mm/tsb.h>
|
|---|
| 61 | #endif
|
|---|
| 62 |
|
|---|
| 63 | static void itlb_pte_copy(pte_t *);
|
|---|
| 64 | static void dtlb_pte_copy(pte_t *, bool);
|
|---|
| 65 |
|
|---|
| 66 | /*
|
|---|
| 67 | * The assembly language routine passes a 64-bit parameter to the Data Access
|
|---|
| 68 | * MMU Miss and Data Access protection handlers, the parameter encapsulates
|
|---|
| 69 | * a virtual address of the faulting page and the faulting context. The most
|
|---|
| 70 | * significant 51 bits represent the VA of the faulting page and the least
|
|---|
| 71 | * significant 13 vits represent the faulting context. The following macros
|
|---|
| 72 | * extract the page and context out of the 64-bit parameter:
|
|---|
| 73 | */
|
|---|
| 74 |
|
|---|
| 75 | /* extracts the VA of the faulting page */
|
|---|
| 76 | #define DMISS_ADDRESS(page_and_ctx) (((page_and_ctx) >> 13) << 13)
|
|---|
| 77 |
|
|---|
| 78 | /* extracts the faulting context */
|
|---|
| 79 | #define DMISS_CONTEXT(page_and_ctx) ((page_and_ctx) & 0x1fff)
|
|---|
| 80 |
|
|---|
| 81 | /**
|
|---|
| 82 | * Descriptions of fault types from the MMU Fault status area.
|
|---|
| 83 | *
|
|---|
| 84 | * fault_type[i] contains description of error for which the IFT or DFT
|
|---|
| 85 | * field of the MMU fault status area is i.
|
|---|
| 86 | */
|
|---|
| 87 | static const char *fault_types[] = {
|
|---|
| 88 | "unknown",
|
|---|
| 89 | "fast miss",
|
|---|
| 90 | "fast protection",
|
|---|
| 91 | "MMU miss",
|
|---|
| 92 | "invalid RA",
|
|---|
| 93 | "privileged violation",
|
|---|
| 94 | "protection violation",
|
|---|
| 95 | "NFO access",
|
|---|
| 96 | "so page/NFO side effect",
|
|---|
| 97 | "invalid VA",
|
|---|
| 98 | "invalid ASI",
|
|---|
| 99 | "nc atomic",
|
|---|
| 100 | "privileged action",
|
|---|
| 101 | "unknown",
|
|---|
| 102 | "unaligned access",
|
|---|
| 103 | "invalid page size"
|
|---|
| 104 | };
|
|---|
| 105 |
|
|---|
| 106 | /** Array of MMU fault status areas. */
|
|---|
| 107 | extern mmu_fault_status_area_t mmu_fsas[MAX_NUM_STRANDS];
|
|---|
| 108 |
|
|---|
| 109 | /*
|
|---|
| 110 | * Invalidate all non-locked DTLB and ITLB entries.
|
|---|
| 111 | */
|
|---|
| 112 | void tlb_arch_init(void)
|
|---|
| 113 | {
|
|---|
| 114 | tlb_invalidate_all();
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | /** Insert privileged mapping into DMMU TLB.
|
|---|
| 118 | *
|
|---|
| 119 | * @param page Virtual page address.
|
|---|
| 120 | * @param frame Physical frame address.
|
|---|
| 121 | * @param pagesize Page size.
|
|---|
| 122 | * @param locked True for permanent mappings, false otherwise.
|
|---|
| 123 | * @param cacheable True if the mapping is cacheable, false otherwise.
|
|---|
| 124 | */
|
|---|
| 125 | void dtlb_insert_mapping(uintptr_t page, uintptr_t frame, int pagesize,
|
|---|
| 126 | bool locked, bool cacheable)
|
|---|
| 127 | {
|
|---|
| 128 | tte_data_t data;
|
|---|
| 129 |
|
|---|
| 130 | data.value = 0;
|
|---|
| 131 | data.v = true;
|
|---|
| 132 | data.nfo = false;
|
|---|
| 133 | data.ra = frame >> FRAME_WIDTH;
|
|---|
| 134 | data.ie = false;
|
|---|
| 135 | data.e = false;
|
|---|
| 136 | data.cp = cacheable;
|
|---|
| 137 | #ifdef CONFIG_VIRT_IDX_DCACHE
|
|---|
| 138 | data.cv = cacheable;
|
|---|
| 139 | #endif
|
|---|
| 140 | data.p = true;
|
|---|
| 141 | data.x = false;
|
|---|
| 142 | data.w = true;
|
|---|
| 143 | data.size = pagesize;
|
|---|
| 144 |
|
|---|
| 145 | if (locked) {
|
|---|
| 146 | __hypercall_fast4(
|
|---|
| 147 | MMU_MAP_PERM_ADDR, page, 0, data.value, MMU_FLAG_DTLB);
|
|---|
| 148 | } else {
|
|---|
| 149 | __hypercall_hyperfast(
|
|---|
| 150 | page, ASID_KERNEL, data.value, MMU_FLAG_DTLB, 0,
|
|---|
| 151 | MMU_MAP_ADDR);
|
|---|
| 152 | }
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | /** Copy PTE to TLB.
|
|---|
| 156 | *
|
|---|
| 157 | * @param t Page Table Entry to be copied.
|
|---|
| 158 | * @param ro If true, the entry will be created read-only, regardless
|
|---|
| 159 | * of its w field.
|
|---|
| 160 | */
|
|---|
| 161 | void dtlb_pte_copy(pte_t *t, bool ro)
|
|---|
| 162 | {
|
|---|
| 163 | tte_data_t data;
|
|---|
| 164 |
|
|---|
| 165 | data.value = 0;
|
|---|
| 166 | data.v = true;
|
|---|
| 167 | data.nfo = false;
|
|---|
| 168 | data.ra = (t->frame) >> FRAME_WIDTH;
|
|---|
| 169 | data.ie = false;
|
|---|
| 170 | data.e = false;
|
|---|
| 171 | data.cp = t->c;
|
|---|
| 172 | #ifdef CONFIG_VIRT_IDX_DCACHE
|
|---|
| 173 | data.cv = t->c;
|
|---|
| 174 | #endif
|
|---|
| 175 | data.p = t->k;
|
|---|
| 176 | data.x = false;
|
|---|
| 177 | data.w = ro ? false : t->w;
|
|---|
| 178 | data.size = PAGESIZE_8K;
|
|---|
| 179 |
|
|---|
| 180 | __hypercall_hyperfast(
|
|---|
| 181 | t->page, t->as->asid, data.value, MMU_FLAG_DTLB, 0, MMU_MAP_ADDR);
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | /** Copy PTE to ITLB.
|
|---|
| 185 | *
|
|---|
| 186 | * @param t Page Table Entry to be copied.
|
|---|
| 187 | */
|
|---|
| 188 | void itlb_pte_copy(pte_t *t)
|
|---|
| 189 | {
|
|---|
| 190 | tte_data_t data;
|
|---|
| 191 |
|
|---|
| 192 | data.value = 0;
|
|---|
| 193 | data.v = true;
|
|---|
| 194 | data.nfo = false;
|
|---|
| 195 | data.ra = (t->frame) >> FRAME_WIDTH;
|
|---|
| 196 | data.ie = false;
|
|---|
| 197 | data.e = false;
|
|---|
| 198 | data.cp = t->c;
|
|---|
| 199 | data.cv = false;
|
|---|
| 200 | data.p = t->k;
|
|---|
| 201 | data.x = true;
|
|---|
| 202 | data.w = false;
|
|---|
| 203 | data.size = PAGESIZE_8K;
|
|---|
| 204 |
|
|---|
| 205 | __hypercall_hyperfast(
|
|---|
| 206 | t->page, t->as->asid, data.value, MMU_FLAG_ITLB, 0, MMU_MAP_ADDR);
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | /** ITLB miss handler. */
|
|---|
| 210 | void fast_instruction_access_mmu_miss(unsigned int tt, istate_t *istate)
|
|---|
| 211 | {
|
|---|
| 212 | uintptr_t va = ALIGN_DOWN(istate->tpc, PAGE_SIZE);
|
|---|
| 213 | pte_t t;
|
|---|
| 214 |
|
|---|
| 215 | bool found = page_mapping_find(AS, va, true, &t);
|
|---|
| 216 | if (found && PTE_EXECUTABLE(&t)) {
|
|---|
| 217 | /*
|
|---|
| 218 | * The mapping was found in the software page hash table.
|
|---|
| 219 | * Insert it into ITLB.
|
|---|
| 220 | */
|
|---|
| 221 | t.a = true;
|
|---|
| 222 | itlb_pte_copy(&t);
|
|---|
| 223 | #ifdef CONFIG_TSB
|
|---|
| 224 | itsb_pte_copy(&t);
|
|---|
| 225 | #endif
|
|---|
| 226 | page_mapping_update(AS, va, true, &t);
|
|---|
| 227 | } else {
|
|---|
| 228 | /*
|
|---|
| 229 | * Forward the page fault to the address space page fault
|
|---|
| 230 | * handler.
|
|---|
| 231 | */
|
|---|
| 232 | as_page_fault(va, PF_ACCESS_EXEC, istate);
|
|---|
| 233 | }
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | /** DTLB miss handler.
|
|---|
| 237 | *
|
|---|
| 238 | * Note that some faults (e.g. kernel faults) were already resolved by the
|
|---|
| 239 | * low-level, assembly language part of the fast_data_access_mmu_miss handler.
|
|---|
| 240 | *
|
|---|
| 241 | * @param tt Trap type.
|
|---|
| 242 | * @param istate Interrupted state saved on the stack.
|
|---|
| 243 | */
|
|---|
| 244 | void fast_data_access_mmu_miss(unsigned int tt, istate_t *istate)
|
|---|
| 245 | {
|
|---|
| 246 | pte_t t;
|
|---|
| 247 | uintptr_t va = DMISS_ADDRESS(istate->tlb_tag_access);
|
|---|
| 248 | uint16_t ctx = DMISS_CONTEXT(istate->tlb_tag_access);
|
|---|
| 249 | as_t *as = AS;
|
|---|
| 250 |
|
|---|
| 251 | if (ctx == ASID_KERNEL) {
|
|---|
| 252 | if (va == 0) {
|
|---|
| 253 | /* NULL access in kernel */
|
|---|
| 254 | panic("NULL pointer dereference.");
|
|---|
| 255 | } else if (va >= end_of_identity) {
|
|---|
| 256 | /* Kernel non-identity */
|
|---|
| 257 | as = AS_KERNEL;
|
|---|
| 258 | } else {
|
|---|
| 259 | panic("Unexpected kernel page fault.");
|
|---|
| 260 | }
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | bool found = page_mapping_find(as, va, true, &t);
|
|---|
| 264 | if (found) {
|
|---|
| 265 | /*
|
|---|
| 266 | * The mapping was found in the software page hash table.
|
|---|
| 267 | * Insert it into DTLB.
|
|---|
| 268 | */
|
|---|
| 269 | t.a = true;
|
|---|
| 270 | dtlb_pte_copy(&t, true);
|
|---|
| 271 | #ifdef CONFIG_TSB
|
|---|
| 272 | dtsb_pte_copy(&t, true);
|
|---|
| 273 | #endif
|
|---|
| 274 | page_mapping_update(as, va, true, &t);
|
|---|
| 275 | } else {
|
|---|
| 276 | /*
|
|---|
| 277 | * Forward the page fault to the address space page fault
|
|---|
| 278 | * handler.
|
|---|
| 279 | */
|
|---|
| 280 | as_page_fault(va, PF_ACCESS_READ, istate);
|
|---|
| 281 | }
|
|---|
| 282 | }
|
|---|
| 283 |
|
|---|
| 284 | /** DTLB protection fault handler.
|
|---|
| 285 | *
|
|---|
| 286 | * @param tt Trap type.
|
|---|
| 287 | * @param istate Interrupted state saved on the stack.
|
|---|
| 288 | */
|
|---|
| 289 | void fast_data_access_protection(unsigned int tt, istate_t *istate)
|
|---|
| 290 | {
|
|---|
| 291 | pte_t t;
|
|---|
| 292 | uintptr_t va = DMISS_ADDRESS(istate->tlb_tag_access);
|
|---|
| 293 | uint16_t ctx = DMISS_CONTEXT(istate->tlb_tag_access);
|
|---|
| 294 | as_t *as = AS;
|
|---|
| 295 |
|
|---|
| 296 | if (ctx == ASID_KERNEL)
|
|---|
| 297 | as = AS_KERNEL;
|
|---|
| 298 |
|
|---|
| 299 | bool found = page_mapping_find(as, va, true, &t);
|
|---|
| 300 | if (found && PTE_WRITABLE(&t)) {
|
|---|
| 301 | /*
|
|---|
| 302 | * The mapping was found in the software page hash table and is
|
|---|
| 303 | * writable. Demap the old mapping and insert an updated mapping
|
|---|
| 304 | * into DTLB.
|
|---|
| 305 | */
|
|---|
| 306 | t.a = true;
|
|---|
| 307 | t.d = true;
|
|---|
| 308 | mmu_demap_page(va, ctx, MMU_FLAG_DTLB);
|
|---|
| 309 | dtlb_pte_copy(&t, false);
|
|---|
| 310 | #ifdef CONFIG_TSB
|
|---|
| 311 | dtsb_pte_copy(&t, false);
|
|---|
| 312 | #endif
|
|---|
| 313 | page_mapping_update(as, va, true, &t);
|
|---|
| 314 | } else {
|
|---|
| 315 | /*
|
|---|
| 316 | * Forward the page fault to the address space page fault
|
|---|
| 317 | * handler.
|
|---|
| 318 | */
|
|---|
| 319 | as_page_fault(va, PF_ACCESS_WRITE, istate);
|
|---|
| 320 | }
|
|---|
| 321 | }
|
|---|
| 322 |
|
|---|
| 323 | /*
|
|---|
| 324 | * On Niagara this function does not work, as supervisor software is isolated
|
|---|
| 325 | * from the TLB by the hypervisor and has no chance to investigate the TLB
|
|---|
| 326 | * entries.
|
|---|
| 327 | */
|
|---|
| 328 | void tlb_print(void)
|
|---|
| 329 | {
|
|---|
| 330 | log(LF_ARCH, LVL_WARN, "Operation not possible on Niagara.");
|
|---|
| 331 | }
|
|---|
| 332 |
|
|---|
| 333 | /**
|
|---|
| 334 | * Describes the exact condition which caused the last DMMU fault.
|
|---|
| 335 | */
|
|---|
| 336 | void describe_dmmu_fault(void)
|
|---|
| 337 | {
|
|---|
| 338 | uint64_t myid;
|
|---|
| 339 | __hypercall_fast_ret1(0, 0, 0, 0, 0, CPU_MYID, &myid);
|
|---|
| 340 |
|
|---|
| 341 | ASSERT(mmu_fsas[myid].dft < 16);
|
|---|
| 342 |
|
|---|
| 343 | printf("condition which caused the fault: %s\n",
|
|---|
| 344 | fault_types[mmu_fsas[myid].dft]);
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | /** Invalidate all unlocked ITLB and DTLB entries. */
|
|---|
| 348 | void tlb_invalidate_all(void)
|
|---|
| 349 | {
|
|---|
| 350 | uint64_t errno = __hypercall_fast3(MMU_DEMAP_ALL, 0, 0,
|
|---|
| 351 | MMU_FLAG_DTLB | MMU_FLAG_ITLB);
|
|---|
| 352 | if (errno != HV_EOK)
|
|---|
| 353 | panic("Error code = %" PRIu64 ".\n", errno);
|
|---|
| 354 | }
|
|---|
| 355 |
|
|---|
| 356 | /** Invalidate all ITLB and DTLB entries that belong to specified ASID
|
|---|
| 357 | * (Context).
|
|---|
| 358 | *
|
|---|
| 359 | * @param asid Address Space ID.
|
|---|
| 360 | */
|
|---|
| 361 | void tlb_invalidate_asid(asid_t asid)
|
|---|
| 362 | {
|
|---|
| 363 | /* switch to nucleus because we are mapped by the primary context */
|
|---|
| 364 | nucleus_enter();
|
|---|
| 365 |
|
|---|
| 366 | __hypercall_fast4(MMU_DEMAP_CTX, 0, 0, asid,
|
|---|
| 367 | MMU_FLAG_ITLB | MMU_FLAG_DTLB);
|
|---|
| 368 |
|
|---|
| 369 | nucleus_leave();
|
|---|
| 370 | }
|
|---|
| 371 |
|
|---|
| 372 | /** Invalidate all ITLB and DTLB entries for specified page range in specified
|
|---|
| 373 | * address space.
|
|---|
| 374 | *
|
|---|
| 375 | * @param asid Address Space ID.
|
|---|
| 376 | * @param page First page which to sweep out from ITLB and DTLB.
|
|---|
| 377 | * @param cnt Number of ITLB and DTLB entries to invalidate.
|
|---|
| 378 | */
|
|---|
| 379 | void tlb_invalidate_pages(asid_t asid, uintptr_t page, size_t cnt)
|
|---|
| 380 | {
|
|---|
| 381 | unsigned int i;
|
|---|
| 382 |
|
|---|
| 383 | /* switch to nucleus because we are mapped by the primary context */
|
|---|
| 384 | nucleus_enter();
|
|---|
| 385 |
|
|---|
| 386 | for (i = 0; i < cnt; i++) {
|
|---|
| 387 | __hypercall_fast5(MMU_DEMAP_PAGE, 0, 0, page + i * PAGE_SIZE,
|
|---|
| 388 | asid, MMU_FLAG_DTLB | MMU_FLAG_ITLB);
|
|---|
| 389 | }
|
|---|
| 390 |
|
|---|
| 391 | nucleus_leave();
|
|---|
| 392 | }
|
|---|
| 393 |
|
|---|
| 394 | /** @}
|
|---|
| 395 | */
|
|---|