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