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