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