[f761f1eb] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2003-2004 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 |
|
---|
[a6dd361] | 29 | /** @addtogroup mips32mm
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[f761f1eb] | 35 | #include <arch/mm/tlb.h>
|
---|
[4512d7e] | 36 | #include <mm/asid.h>
|
---|
[f761f1eb] | 37 | #include <mm/tlb.h>
|
---|
[1084a784] | 38 | #include <mm/page.h>
|
---|
[20d50a1] | 39 | #include <mm/as.h>
|
---|
[f761f1eb] | 40 | #include <arch/cp0.h>
|
---|
| 41 | #include <panic.h>
|
---|
| 42 | #include <arch.h>
|
---|
[ab08b42] | 43 | #include <symtab.h>
|
---|
[1084a784] | 44 | #include <synch/spinlock.h>
|
---|
| 45 | #include <print.h>
|
---|
[cc205f1] | 46 | #include <debug.h>
|
---|
[2d01bbd] | 47 | #include <align.h>
|
---|
[874621f] | 48 | #include <interrupt.h>
|
---|
[9c0a9b3] | 49 |
|
---|
[25d7709] | 50 | static void tlb_refill_fail(istate_t *istate);
|
---|
| 51 | static void tlb_invalid_fail(istate_t *istate);
|
---|
| 52 | static void tlb_modified_fail(istate_t *istate);
|
---|
[1084a784] | 53 |
|
---|
[7f1c620] | 54 | static pte_t *find_mapping_and_check(uintptr_t badvaddr, int access, istate_t *istate, int *pfrc);
|
---|
[8c5e6c7] | 55 |
|
---|
[7f1c620] | 56 | static void prepare_entry_lo(entry_lo_t *lo, bool g, bool v, bool d, bool cacheable, uintptr_t pfn);
|
---|
| 57 | static void prepare_entry_hi(entry_hi_t *hi, asid_t asid, uintptr_t addr);
|
---|
[38a1a84] | 58 |
|
---|
[1084a784] | 59 | /** Initialize TLB
|
---|
| 60 | *
|
---|
| 61 | * Initialize TLB.
|
---|
| 62 | * Invalidate all entries and mark wired entries.
|
---|
| 63 | */
|
---|
[b00fdde] | 64 | void tlb_arch_init(void)
|
---|
[ce031f0] | 65 | {
|
---|
[dd14cced] | 66 | int i;
|
---|
| 67 |
|
---|
[ce031f0] | 68 | cp0_pagemask_write(TLB_PAGE_MASK_16K);
|
---|
[dd14cced] | 69 | cp0_entry_hi_write(0);
|
---|
| 70 | cp0_entry_lo0_write(0);
|
---|
| 71 | cp0_entry_lo1_write(0);
|
---|
[ce031f0] | 72 |
|
---|
[dd14cced] | 73 | /* Clear and initialize TLB. */
|
---|
| 74 |
|
---|
| 75 | for (i = 0; i < TLB_ENTRY_COUNT; i++) {
|
---|
| 76 | cp0_index_write(i);
|
---|
| 77 | tlbwi();
|
---|
| 78 | }
|
---|
[0bd4f56d] | 79 |
|
---|
[a98d2ec] | 80 |
|
---|
[ce031f0] | 81 | /*
|
---|
| 82 | * The kernel is going to make use of some wired
|
---|
[1084a784] | 83 | * entries (e.g. mapping kernel stacks in kseg3).
|
---|
[ce031f0] | 84 | */
|
---|
| 85 | cp0_wired_write(TLB_WIRED);
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[1084a784] | 88 | /** Process TLB Refill Exception
|
---|
| 89 | *
|
---|
| 90 | * Process TLB Refill Exception.
|
---|
| 91 | *
|
---|
[25d7709] | 92 | * @param istate Interrupted register context.
|
---|
[1084a784] | 93 | */
|
---|
[25d7709] | 94 | void tlb_refill(istate_t *istate)
|
---|
[1084a784] | 95 | {
|
---|
[cc205f1] | 96 | entry_lo_t lo;
|
---|
[2299914] | 97 | entry_hi_t hi;
|
---|
| 98 | asid_t asid;
|
---|
[7f1c620] | 99 | uintptr_t badvaddr;
|
---|
[1084a784] | 100 | pte_t *pte;
|
---|
[e3c762cd] | 101 | int pfrc;
|
---|
[fd3c9e5] | 102 |
|
---|
[1084a784] | 103 | badvaddr = cp0_badvaddr_read();
|
---|
[fd3c9e5] | 104 |
|
---|
[2299914] | 105 | spinlock_lock(&AS->lock);
|
---|
| 106 | asid = AS->asid;
|
---|
| 107 | spinlock_unlock(&AS->lock);
|
---|
| 108 |
|
---|
| 109 | page_table_lock(AS, true);
|
---|
[8c5e6c7] | 110 |
|
---|
[567807b1] | 111 | pte = find_mapping_and_check(badvaddr, PF_ACCESS_READ, istate, &pfrc);
|
---|
[e3c762cd] | 112 | if (!pte) {
|
---|
| 113 | switch (pfrc) {
|
---|
| 114 | case AS_PF_FAULT:
|
---|
| 115 | goto fail;
|
---|
| 116 | break;
|
---|
| 117 | case AS_PF_DEFER:
|
---|
| 118 | /*
|
---|
| 119 | * The page fault came during copy_from_uspace()
|
---|
| 120 | * or copy_to_uspace().
|
---|
| 121 | */
|
---|
| 122 | page_table_unlock(AS, true);
|
---|
| 123 | return;
|
---|
| 124 | default:
|
---|
| 125 | panic("unexpected pfrc (%d)\n", pfrc);
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
[38a1a84] | 128 |
|
---|
[1084a784] | 129 | /*
|
---|
[38a1a84] | 130 | * Record access to PTE.
|
---|
[1084a784] | 131 | */
|
---|
[38a1a84] | 132 | pte->a = 1;
|
---|
| 133 |
|
---|
[2299914] | 134 | prepare_entry_hi(&hi, asid, badvaddr);
|
---|
[0882a9a] | 135 | prepare_entry_lo(&lo, pte->g, pte->p, pte->d, pte->cacheable, pte->pfn);
|
---|
[1084a784] | 136 |
|
---|
| 137 | /*
|
---|
| 138 | * New entry is to be inserted into TLB
|
---|
| 139 | */
|
---|
[8c5e6c7] | 140 | cp0_entry_hi_write(hi.value);
|
---|
[1084a784] | 141 | if ((badvaddr/PAGE_SIZE) % 2 == 0) {
|
---|
[cc205f1] | 142 | cp0_entry_lo0_write(lo.value);
|
---|
[1084a784] | 143 | cp0_entry_lo1_write(0);
|
---|
| 144 | }
|
---|
| 145 | else {
|
---|
| 146 | cp0_entry_lo0_write(0);
|
---|
[cc205f1] | 147 | cp0_entry_lo1_write(lo.value);
|
---|
[1084a784] | 148 | }
|
---|
[0bd4f56d] | 149 | cp0_pagemask_write(TLB_PAGE_MASK_16K);
|
---|
[1084a784] | 150 | tlbwr();
|
---|
| 151 |
|
---|
[2299914] | 152 | page_table_unlock(AS, true);
|
---|
[1084a784] | 153 | return;
|
---|
| 154 |
|
---|
| 155 | fail:
|
---|
[2299914] | 156 | page_table_unlock(AS, true);
|
---|
[25d7709] | 157 | tlb_refill_fail(istate);
|
---|
[1084a784] | 158 | }
|
---|
| 159 |
|
---|
[38a1a84] | 160 | /** Process TLB Invalid Exception
|
---|
| 161 | *
|
---|
| 162 | * Process TLB Invalid Exception.
|
---|
| 163 | *
|
---|
[25d7709] | 164 | * @param istate Interrupted register context.
|
---|
[38a1a84] | 165 | */
|
---|
[25d7709] | 166 | void tlb_invalid(istate_t *istate)
|
---|
[1084a784] | 167 | {
|
---|
[cc205f1] | 168 | tlb_index_t index;
|
---|
[7f1c620] | 169 | uintptr_t badvaddr;
|
---|
[cc205f1] | 170 | entry_lo_t lo;
|
---|
[8c5e6c7] | 171 | entry_hi_t hi;
|
---|
[38a1a84] | 172 | pte_t *pte;
|
---|
[e3c762cd] | 173 | int pfrc;
|
---|
[38a1a84] | 174 |
|
---|
| 175 | badvaddr = cp0_badvaddr_read();
|
---|
| 176 |
|
---|
| 177 | /*
|
---|
| 178 | * Locate the faulting entry in TLB.
|
---|
| 179 | */
|
---|
[8c5e6c7] | 180 | hi.value = cp0_entry_hi_read();
|
---|
| 181 | prepare_entry_hi(&hi, hi.asid, badvaddr);
|
---|
| 182 | cp0_entry_hi_write(hi.value);
|
---|
[38a1a84] | 183 | tlbp();
|
---|
[cc205f1] | 184 | index.value = cp0_index_read();
|
---|
[2299914] | 185 |
|
---|
| 186 | page_table_lock(AS, true);
|
---|
[38a1a84] | 187 |
|
---|
| 188 | /*
|
---|
| 189 | * Fail if the entry is not in TLB.
|
---|
| 190 | */
|
---|
[cc205f1] | 191 | if (index.p) {
|
---|
| 192 | printf("TLB entry not found.\n");
|
---|
[38a1a84] | 193 | goto fail;
|
---|
[cc205f1] | 194 | }
|
---|
[38a1a84] | 195 |
|
---|
[567807b1] | 196 | pte = find_mapping_and_check(badvaddr, PF_ACCESS_READ, istate, &pfrc);
|
---|
[e3c762cd] | 197 | if (!pte) {
|
---|
| 198 | switch (pfrc) {
|
---|
| 199 | case AS_PF_FAULT:
|
---|
| 200 | goto fail;
|
---|
| 201 | break;
|
---|
| 202 | case AS_PF_DEFER:
|
---|
| 203 | /*
|
---|
| 204 | * The page fault came during copy_from_uspace()
|
---|
| 205 | * or copy_to_uspace().
|
---|
| 206 | */
|
---|
| 207 | page_table_unlock(AS, true);
|
---|
| 208 | return;
|
---|
| 209 | default:
|
---|
| 210 | panic("unexpected pfrc (%d)\n", pfrc);
|
---|
| 211 | }
|
---|
| 212 | }
|
---|
[38a1a84] | 213 |
|
---|
| 214 | /*
|
---|
| 215 | * Read the faulting TLB entry.
|
---|
| 216 | */
|
---|
| 217 | tlbr();
|
---|
| 218 |
|
---|
| 219 | /*
|
---|
| 220 | * Record access to PTE.
|
---|
| 221 | */
|
---|
| 222 | pte->a = 1;
|
---|
| 223 |
|
---|
[0882a9a] | 224 | prepare_entry_lo(&lo, pte->g, pte->p, pte->d, pte->cacheable, pte->pfn);
|
---|
[38a1a84] | 225 |
|
---|
| 226 | /*
|
---|
| 227 | * The entry is to be updated in TLB.
|
---|
| 228 | */
|
---|
| 229 | if ((badvaddr/PAGE_SIZE) % 2 == 0)
|
---|
[cc205f1] | 230 | cp0_entry_lo0_write(lo.value);
|
---|
[38a1a84] | 231 | else
|
---|
[cc205f1] | 232 | cp0_entry_lo1_write(lo.value);
|
---|
[0bd4f56d] | 233 | cp0_pagemask_write(TLB_PAGE_MASK_16K);
|
---|
[38a1a84] | 234 | tlbwi();
|
---|
| 235 |
|
---|
[2299914] | 236 | page_table_unlock(AS, true);
|
---|
[38a1a84] | 237 | return;
|
---|
| 238 |
|
---|
| 239 | fail:
|
---|
[2299914] | 240 | page_table_unlock(AS, true);
|
---|
[25d7709] | 241 | tlb_invalid_fail(istate);
|
---|
[1084a784] | 242 | }
|
---|
| 243 |
|
---|
[38a1a84] | 244 | /** Process TLB Modified Exception
|
---|
| 245 | *
|
---|
| 246 | * Process TLB Modified Exception.
|
---|
| 247 | *
|
---|
[25d7709] | 248 | * @param istate Interrupted register context.
|
---|
[38a1a84] | 249 | */
|
---|
[25d7709] | 250 | void tlb_modified(istate_t *istate)
|
---|
[1084a784] | 251 | {
|
---|
[cc205f1] | 252 | tlb_index_t index;
|
---|
[7f1c620] | 253 | uintptr_t badvaddr;
|
---|
[cc205f1] | 254 | entry_lo_t lo;
|
---|
[8c5e6c7] | 255 | entry_hi_t hi;
|
---|
[38a1a84] | 256 | pte_t *pte;
|
---|
[e3c762cd] | 257 | int pfrc;
|
---|
[38a1a84] | 258 |
|
---|
| 259 | badvaddr = cp0_badvaddr_read();
|
---|
| 260 |
|
---|
| 261 | /*
|
---|
| 262 | * Locate the faulting entry in TLB.
|
---|
| 263 | */
|
---|
[8c5e6c7] | 264 | hi.value = cp0_entry_hi_read();
|
---|
| 265 | prepare_entry_hi(&hi, hi.asid, badvaddr);
|
---|
| 266 | cp0_entry_hi_write(hi.value);
|
---|
[38a1a84] | 267 | tlbp();
|
---|
[cc205f1] | 268 | index.value = cp0_index_read();
|
---|
[2299914] | 269 |
|
---|
| 270 | page_table_lock(AS, true);
|
---|
[38a1a84] | 271 |
|
---|
| 272 | /*
|
---|
| 273 | * Fail if the entry is not in TLB.
|
---|
| 274 | */
|
---|
[cc205f1] | 275 | if (index.p) {
|
---|
| 276 | printf("TLB entry not found.\n");
|
---|
[38a1a84] | 277 | goto fail;
|
---|
[cc205f1] | 278 | }
|
---|
[38a1a84] | 279 |
|
---|
[567807b1] | 280 | pte = find_mapping_and_check(badvaddr, PF_ACCESS_WRITE, istate, &pfrc);
|
---|
[e3c762cd] | 281 | if (!pte) {
|
---|
| 282 | switch (pfrc) {
|
---|
| 283 | case AS_PF_FAULT:
|
---|
| 284 | goto fail;
|
---|
| 285 | break;
|
---|
| 286 | case AS_PF_DEFER:
|
---|
| 287 | /*
|
---|
| 288 | * The page fault came during copy_from_uspace()
|
---|
| 289 | * or copy_to_uspace().
|
---|
| 290 | */
|
---|
| 291 | page_table_unlock(AS, true);
|
---|
| 292 | return;
|
---|
| 293 | default:
|
---|
| 294 | panic("unexpected pfrc (%d)\n", pfrc);
|
---|
| 295 | }
|
---|
| 296 | }
|
---|
[38a1a84] | 297 |
|
---|
| 298 | /*
|
---|
| 299 | * Fail if the page is not writable.
|
---|
| 300 | */
|
---|
| 301 | if (!pte->w)
|
---|
| 302 | goto fail;
|
---|
| 303 |
|
---|
| 304 | /*
|
---|
| 305 | * Read the faulting TLB entry.
|
---|
| 306 | */
|
---|
| 307 | tlbr();
|
---|
| 308 |
|
---|
| 309 | /*
|
---|
| 310 | * Record access and write to PTE.
|
---|
| 311 | */
|
---|
| 312 | pte->a = 1;
|
---|
[0882a9a] | 313 | pte->d = 1;
|
---|
[38a1a84] | 314 |
|
---|
[0882a9a] | 315 | prepare_entry_lo(&lo, pte->g, pte->p, pte->w, pte->cacheable, pte->pfn);
|
---|
[38a1a84] | 316 |
|
---|
| 317 | /*
|
---|
| 318 | * The entry is to be updated in TLB.
|
---|
| 319 | */
|
---|
| 320 | if ((badvaddr/PAGE_SIZE) % 2 == 0)
|
---|
[cc205f1] | 321 | cp0_entry_lo0_write(lo.value);
|
---|
[38a1a84] | 322 | else
|
---|
[cc205f1] | 323 | cp0_entry_lo1_write(lo.value);
|
---|
[0bd4f56d] | 324 | cp0_pagemask_write(TLB_PAGE_MASK_16K);
|
---|
[38a1a84] | 325 | tlbwi();
|
---|
| 326 |
|
---|
[2299914] | 327 | page_table_unlock(AS, true);
|
---|
[38a1a84] | 328 | return;
|
---|
| 329 |
|
---|
| 330 | fail:
|
---|
[2299914] | 331 | page_table_unlock(AS, true);
|
---|
[25d7709] | 332 | tlb_modified_fail(istate);
|
---|
[1084a784] | 333 | }
|
---|
| 334 |
|
---|
[25d7709] | 335 | void tlb_refill_fail(istate_t *istate)
|
---|
[f761f1eb] | 336 | {
|
---|
[38de8a5] | 337 | char *symbol = "";
|
---|
| 338 | char *sym2 = "";
|
---|
| 339 |
|
---|
[25d7709] | 340 | char *s = get_symtab_entry(istate->epc);
|
---|
[3156582] | 341 | if (s)
|
---|
| 342 | symbol = s;
|
---|
[25d7709] | 343 | s = get_symtab_entry(istate->ra);
|
---|
[3156582] | 344 | if (s)
|
---|
| 345 | sym2 = s;
|
---|
[874621f] | 346 |
|
---|
[fbf7b4c] | 347 | fault_if_from_uspace(istate, "TLB Refill Exception on %p", cp0_badvaddr_read());
|
---|
| 348 | panic("%x: TLB Refill Exception at %x(%s<-%s)\n", cp0_badvaddr_read(), istate->epc, symbol, sym2);
|
---|
[f761f1eb] | 349 | }
|
---|
| 350 |
|
---|
[1084a784] | 351 |
|
---|
[25d7709] | 352 | void tlb_invalid_fail(istate_t *istate)
|
---|
[f761f1eb] | 353 | {
|
---|
[ab08b42] | 354 | char *symbol = "";
|
---|
| 355 |
|
---|
[25d7709] | 356 | char *s = get_symtab_entry(istate->epc);
|
---|
[3156582] | 357 | if (s)
|
---|
| 358 | symbol = s;
|
---|
[fbf7b4c] | 359 | fault_if_from_uspace(istate, "TLB Invalid Exception on %p", cp0_badvaddr_read());
|
---|
| 360 | panic("%x: TLB Invalid Exception at %x(%s)\n", cp0_badvaddr_read(), istate->epc, symbol);
|
---|
[f761f1eb] | 361 | }
|
---|
| 362 |
|
---|
[25d7709] | 363 | void tlb_modified_fail(istate_t *istate)
|
---|
[ce031f0] | 364 | {
|
---|
| 365 | char *symbol = "";
|
---|
| 366 |
|
---|
[25d7709] | 367 | char *s = get_symtab_entry(istate->epc);
|
---|
[ce031f0] | 368 | if (s)
|
---|
| 369 | symbol = s;
|
---|
[fbf7b4c] | 370 | fault_if_from_uspace(istate, "TLB Modified Exception on %p", cp0_badvaddr_read());
|
---|
| 371 | panic("%x: TLB Modified Exception at %x(%s)\n", cp0_badvaddr_read(), istate->epc, symbol);
|
---|
[ce031f0] | 372 | }
|
---|
| 373 |
|
---|
[38a1a84] | 374 | /** Try to find PTE for faulting address
|
---|
| 375 | *
|
---|
| 376 | * Try to find PTE for faulting address.
|
---|
[20d50a1] | 377 | * The AS->lock must be held on entry to this function.
|
---|
[38a1a84] | 378 | *
|
---|
| 379 | * @param badvaddr Faulting virtual address.
|
---|
[567807b1] | 380 | * @param access Access mode that caused the fault.
|
---|
[e3c762cd] | 381 | * @param istate Pointer to interrupted state.
|
---|
| 382 | * @param pfrc Pointer to variable where as_page_fault() return code will be stored.
|
---|
[38a1a84] | 383 | *
|
---|
| 384 | * @return PTE on success, NULL otherwise.
|
---|
| 385 | */
|
---|
[7f1c620] | 386 | pte_t *find_mapping_and_check(uintptr_t badvaddr, int access, istate_t *istate, int *pfrc)
|
---|
[38a1a84] | 387 | {
|
---|
[cc205f1] | 388 | entry_hi_t hi;
|
---|
[38a1a84] | 389 | pte_t *pte;
|
---|
| 390 |
|
---|
[cc205f1] | 391 | hi.value = cp0_entry_hi_read();
|
---|
[38a1a84] | 392 |
|
---|
| 393 | /*
|
---|
| 394 | * Handler cannot succeed if the ASIDs don't match.
|
---|
| 395 | */
|
---|
[20d50a1] | 396 | if (hi.asid != AS->asid) {
|
---|
| 397 | printf("EntryHi.asid=%d, AS->asid=%d\n", hi.asid, AS->asid);
|
---|
[38a1a84] | 398 | return NULL;
|
---|
[cc205f1] | 399 | }
|
---|
[20d50a1] | 400 |
|
---|
| 401 | /*
|
---|
| 402 | * Check if the mapping exists in page tables.
|
---|
| 403 | */
|
---|
[ef67bab] | 404 | pte = page_mapping_find(AS, badvaddr);
|
---|
[0882a9a] | 405 | if (pte && pte->p) {
|
---|
[20d50a1] | 406 | /*
|
---|
| 407 | * Mapping found in page tables.
|
---|
| 408 | * Immediately succeed.
|
---|
| 409 | */
|
---|
| 410 | return pte;
|
---|
| 411 | } else {
|
---|
[e3c762cd] | 412 | int rc;
|
---|
| 413 |
|
---|
[20d50a1] | 414 | /*
|
---|
| 415 | * Mapping not found in page tables.
|
---|
| 416 | * Resort to higher-level page fault handler.
|
---|
| 417 | */
|
---|
[2299914] | 418 | page_table_unlock(AS, true);
|
---|
[567807b1] | 419 | switch (rc = as_page_fault(badvaddr, access, istate)) {
|
---|
[e3c762cd] | 420 | case AS_PF_OK:
|
---|
[20d50a1] | 421 | /*
|
---|
| 422 | * The higher-level page fault handler succeeded,
|
---|
| 423 | * The mapping ought to be in place.
|
---|
| 424 | */
|
---|
[2299914] | 425 | page_table_lock(AS, true);
|
---|
[ef67bab] | 426 | pte = page_mapping_find(AS, badvaddr);
|
---|
[0882a9a] | 427 | ASSERT(pte && pte->p);
|
---|
[20d50a1] | 428 | return pte;
|
---|
[e3c762cd] | 429 | break;
|
---|
| 430 | case AS_PF_DEFER:
|
---|
| 431 | page_table_lock(AS, true);
|
---|
| 432 | *pfrc = AS_PF_DEFER;
|
---|
| 433 | return NULL;
|
---|
| 434 | break;
|
---|
| 435 | case AS_PF_FAULT:
|
---|
[2299914] | 436 | page_table_lock(AS, true);
|
---|
| 437 | printf("Page fault.\n");
|
---|
[e3c762cd] | 438 | *pfrc = AS_PF_FAULT;
|
---|
[2299914] | 439 | return NULL;
|
---|
[e3c762cd] | 440 | break;
|
---|
| 441 | default:
|
---|
| 442 | panic("unexpected rc (%d)\n", rc);
|
---|
[20d50a1] | 443 | }
|
---|
[2299914] | 444 |
|
---|
[20d50a1] | 445 | }
|
---|
[38a1a84] | 446 | }
|
---|
| 447 |
|
---|
[7f1c620] | 448 | void prepare_entry_lo(entry_lo_t *lo, bool g, bool v, bool d, bool cacheable, uintptr_t pfn)
|
---|
[38a1a84] | 449 | {
|
---|
[8c5e6c7] | 450 | lo->value = 0;
|
---|
[38a1a84] | 451 | lo->g = g;
|
---|
| 452 | lo->v = v;
|
---|
| 453 | lo->d = d;
|
---|
[0882a9a] | 454 | lo->c = cacheable ? PAGE_CACHEABLE_EXC_WRITE : PAGE_UNCACHED;
|
---|
[38a1a84] | 455 | lo->pfn = pfn;
|
---|
[8c5e6c7] | 456 | }
|
---|
| 457 |
|
---|
[7f1c620] | 458 | void prepare_entry_hi(entry_hi_t *hi, asid_t asid, uintptr_t addr)
|
---|
[8c5e6c7] | 459 | {
|
---|
[2d01bbd] | 460 | hi->value = ALIGN_DOWN(addr, PAGE_SIZE * 2);
|
---|
[8c5e6c7] | 461 | hi->asid = asid;
|
---|
[38a1a84] | 462 | }
|
---|
[b00fdde] | 463 |
|
---|
[02055415] | 464 | /** Print contents of TLB. */
|
---|
[b00fdde] | 465 | void tlb_print(void)
|
---|
| 466 | {
|
---|
[0bd4f56d] | 467 | page_mask_t mask;
|
---|
[02055415] | 468 | entry_lo_t lo0, lo1;
|
---|
[f9425006] | 469 | entry_hi_t hi, hi_save;
|
---|
[a0f6a61] | 470 | unsigned int i;
|
---|
[02055415] | 471 |
|
---|
[f9425006] | 472 | hi_save.value = cp0_entry_hi_read();
|
---|
[a0f6a61] | 473 |
|
---|
| 474 | printf("# ASID VPN2 MASK G V D C PFN\n");
|
---|
| 475 | printf("-- ---- ------ ---- - - - - ------\n");
|
---|
| 476 |
|
---|
[02055415] | 477 | for (i = 0; i < TLB_ENTRY_COUNT; i++) {
|
---|
| 478 | cp0_index_write(i);
|
---|
| 479 | tlbr();
|
---|
| 480 |
|
---|
[0bd4f56d] | 481 | mask.value = cp0_pagemask_read();
|
---|
[02055415] | 482 | hi.value = cp0_entry_hi_read();
|
---|
| 483 | lo0.value = cp0_entry_lo0_read();
|
---|
| 484 | lo1.value = cp0_entry_lo1_read();
|
---|
| 485 |
|
---|
[a0f6a61] | 486 | printf("%-2u %-4u %#6x %#4x %1u %1u %1u %1u %#6x\n",
|
---|
| 487 | i, hi.asid, hi.vpn2, mask.mask,
|
---|
| 488 | lo0.g, lo0.v, lo0.d, lo0.c, lo0.pfn);
|
---|
| 489 | printf(" %1u %1u %1u %1u %#6x\n",
|
---|
| 490 | lo1.g, lo1.v, lo1.d, lo1.c, lo1.pfn);
|
---|
[02055415] | 491 | }
|
---|
[f9425006] | 492 |
|
---|
| 493 | cp0_entry_hi_write(hi_save.value);
|
---|
[b00fdde] | 494 | }
|
---|
[a98d2ec] | 495 |
|
---|
[8ad925c] | 496 | /** Invalidate all not wired TLB entries. */
|
---|
[a98d2ec] | 497 | void tlb_invalidate_all(void)
|
---|
| 498 | {
|
---|
[dd14cced] | 499 | ipl_t ipl;
|
---|
| 500 | entry_lo_t lo0, lo1;
|
---|
[f9425006] | 501 | entry_hi_t hi_save;
|
---|
[a98d2ec] | 502 | int i;
|
---|
| 503 |
|
---|
[f9425006] | 504 | hi_save.value = cp0_entry_hi_read();
|
---|
[dd14cced] | 505 | ipl = interrupts_disable();
|
---|
[a98d2ec] | 506 |
|
---|
[8ad925c] | 507 | for (i = TLB_WIRED; i < TLB_ENTRY_COUNT; i++) {
|
---|
[a98d2ec] | 508 | cp0_index_write(i);
|
---|
[dd14cced] | 509 | tlbr();
|
---|
| 510 |
|
---|
| 511 | lo0.value = cp0_entry_lo0_read();
|
---|
| 512 | lo1.value = cp0_entry_lo1_read();
|
---|
| 513 |
|
---|
| 514 | lo0.v = 0;
|
---|
| 515 | lo1.v = 0;
|
---|
| 516 |
|
---|
| 517 | cp0_entry_lo0_write(lo0.value);
|
---|
| 518 | cp0_entry_lo1_write(lo1.value);
|
---|
| 519 |
|
---|
[a98d2ec] | 520 | tlbwi();
|
---|
| 521 | }
|
---|
[dd14cced] | 522 |
|
---|
| 523 | interrupts_restore(ipl);
|
---|
[f9425006] | 524 | cp0_entry_hi_write(hi_save.value);
|
---|
[a98d2ec] | 525 | }
|
---|
| 526 |
|
---|
| 527 | /** Invalidate all TLB entries belonging to specified address space.
|
---|
| 528 | *
|
---|
| 529 | * @param asid Address space identifier.
|
---|
| 530 | */
|
---|
| 531 | void tlb_invalidate_asid(asid_t asid)
|
---|
| 532 | {
|
---|
[dd14cced] | 533 | ipl_t ipl;
|
---|
| 534 | entry_lo_t lo0, lo1;
|
---|
[f9425006] | 535 | entry_hi_t hi, hi_save;
|
---|
[a98d2ec] | 536 | int i;
|
---|
| 537 |
|
---|
[dd14cced] | 538 | ASSERT(asid != ASID_INVALID);
|
---|
| 539 |
|
---|
[f9425006] | 540 | hi_save.value = cp0_entry_hi_read();
|
---|
[dd14cced] | 541 | ipl = interrupts_disable();
|
---|
| 542 |
|
---|
[a98d2ec] | 543 | for (i = 0; i < TLB_ENTRY_COUNT; i++) {
|
---|
| 544 | cp0_index_write(i);
|
---|
| 545 | tlbr();
|
---|
| 546 |
|
---|
[dd14cced] | 547 | hi.value = cp0_entry_hi_read();
|
---|
| 548 |
|
---|
[a98d2ec] | 549 | if (hi.asid == asid) {
|
---|
[dd14cced] | 550 | lo0.value = cp0_entry_lo0_read();
|
---|
| 551 | lo1.value = cp0_entry_lo1_read();
|
---|
| 552 |
|
---|
| 553 | lo0.v = 0;
|
---|
| 554 | lo1.v = 0;
|
---|
| 555 |
|
---|
| 556 | cp0_entry_lo0_write(lo0.value);
|
---|
| 557 | cp0_entry_lo1_write(lo1.value);
|
---|
| 558 |
|
---|
[a98d2ec] | 559 | tlbwi();
|
---|
| 560 | }
|
---|
| 561 | }
|
---|
[dd14cced] | 562 |
|
---|
| 563 | interrupts_restore(ipl);
|
---|
[f9425006] | 564 | cp0_entry_hi_write(hi_save.value);
|
---|
[a98d2ec] | 565 | }
|
---|
| 566 |
|
---|
[4512d7e] | 567 | /** Invalidate TLB entries for specified page range belonging to specified address space.
|
---|
[a98d2ec] | 568 | *
|
---|
| 569 | * @param asid Address space identifier.
|
---|
[4512d7e] | 570 | * @param page First page whose TLB entry is to be invalidated.
|
---|
| 571 | * @param cnt Number of entries to invalidate.
|
---|
[a98d2ec] | 572 | */
|
---|
[7f1c620] | 573 | void tlb_invalidate_pages(asid_t asid, uintptr_t page, count_t cnt)
|
---|
[a98d2ec] | 574 | {
|
---|
[4512d7e] | 575 | int i;
|
---|
[dd14cced] | 576 | ipl_t ipl;
|
---|
| 577 | entry_lo_t lo0, lo1;
|
---|
[f9425006] | 578 | entry_hi_t hi, hi_save;
|
---|
[a98d2ec] | 579 | tlb_index_t index;
|
---|
[dd14cced] | 580 |
|
---|
| 581 | ASSERT(asid != ASID_INVALID);
|
---|
| 582 |
|
---|
[f9425006] | 583 | hi_save.value = cp0_entry_hi_read();
|
---|
[dd14cced] | 584 | ipl = interrupts_disable();
|
---|
[a98d2ec] | 585 |
|
---|
[2d01bbd] | 586 | for (i = 0; i < cnt+1; i+=2) {
|
---|
[4512d7e] | 587 | hi.value = 0;
|
---|
| 588 | prepare_entry_hi(&hi, asid, page + i * PAGE_SIZE);
|
---|
| 589 | cp0_entry_hi_write(hi.value);
|
---|
[dd14cced] | 590 |
|
---|
[4512d7e] | 591 | tlbp();
|
---|
| 592 | index.value = cp0_index_read();
|
---|
[a98d2ec] | 593 |
|
---|
[4512d7e] | 594 | if (!index.p) {
|
---|
| 595 | /* Entry was found, index register contains valid index. */
|
---|
| 596 | tlbr();
|
---|
[dd14cced] | 597 |
|
---|
[4512d7e] | 598 | lo0.value = cp0_entry_lo0_read();
|
---|
| 599 | lo1.value = cp0_entry_lo1_read();
|
---|
[dd14cced] | 600 |
|
---|
[4512d7e] | 601 | lo0.v = 0;
|
---|
| 602 | lo1.v = 0;
|
---|
[dd14cced] | 603 |
|
---|
[4512d7e] | 604 | cp0_entry_lo0_write(lo0.value);
|
---|
| 605 | cp0_entry_lo1_write(lo1.value);
|
---|
[dd14cced] | 606 |
|
---|
[4512d7e] | 607 | tlbwi();
|
---|
| 608 | }
|
---|
[a98d2ec] | 609 | }
|
---|
[dd14cced] | 610 |
|
---|
| 611 | interrupts_restore(ipl);
|
---|
[f9425006] | 612 | cp0_entry_hi_write(hi_save.value);
|
---|
[a98d2ec] | 613 | }
|
---|
[b45c443] | 614 |
|
---|
[a6dd361] | 615 | /** @}
|
---|
[b45c443] | 616 | */
|
---|