| 1 | /*
|
|---|
| 2 | * Copyright (c) 2006 Martin Decky
|
|---|
| 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 |
|
|---|
| 29 | /** @addtogroup ppc32mm
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #include <arch/mm/pht.h>
|
|---|
| 36 | #include <arch/mm/tlb.h>
|
|---|
| 37 | #include <interrupt.h>
|
|---|
| 38 | #include <mm/as.h>
|
|---|
| 39 | #include <mm/page.h>
|
|---|
| 40 | #include <macros.h>
|
|---|
| 41 | #include <typedefs.h>
|
|---|
| 42 |
|
|---|
| 43 | static unsigned int seed = 42;
|
|---|
| 44 |
|
|---|
| 45 | /** Try to find PTE for faulting address
|
|---|
| 46 | *
|
|---|
| 47 | * @param as Address space.
|
|---|
| 48 | * @param badvaddr Faulting virtual address.
|
|---|
| 49 | * @param access Access mode that caused the fault.
|
|---|
| 50 | * @param istate Pointer to interrupted state.
|
|---|
| 51 | *
|
|---|
| 52 | * @return PTE on success, NULL otherwise.
|
|---|
| 53 | *
|
|---|
| 54 | */
|
|---|
| 55 | static pte_t *find_mapping_and_check(as_t *as, uintptr_t badvaddr, int access,
|
|---|
| 56 | istate_t *istate)
|
|---|
| 57 | {
|
|---|
| 58 | /*
|
|---|
| 59 | * Check if the mapping exists in page tables.
|
|---|
| 60 | */
|
|---|
| 61 | pte_t *pte = page_mapping_find(as, badvaddr, true);
|
|---|
| 62 | if ((pte) && (pte->present)) {
|
|---|
| 63 | /*
|
|---|
| 64 | * Mapping found in page tables.
|
|---|
| 65 | * Immediately succeed.
|
|---|
| 66 | */
|
|---|
| 67 | return pte;
|
|---|
| 68 | }
|
|---|
| 69 | /*
|
|---|
| 70 | * Mapping not found in page tables.
|
|---|
| 71 | * Resort to higher-level page fault handler.
|
|---|
| 72 | */
|
|---|
| 73 | if (as_page_fault(badvaddr, access, istate) == AS_PF_OK) {
|
|---|
| 74 | /*
|
|---|
| 75 | * The higher-level page fault handler succeeded,
|
|---|
| 76 | * The mapping ought to be in place.
|
|---|
| 77 | */
|
|---|
| 78 | pte = page_mapping_find(as, badvaddr, true);
|
|---|
| 79 | ASSERT((pte) && (pte->present));
|
|---|
| 80 | return pte;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | return NULL;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | static void pht_insert(const uintptr_t vaddr, const pte_t *pte)
|
|---|
| 87 | {
|
|---|
| 88 | uint32_t page = (vaddr >> 12) & 0xffff;
|
|---|
| 89 | uint32_t api = (vaddr >> 22) & 0x3f;
|
|---|
| 90 |
|
|---|
| 91 | uint32_t vsid = sr_get(vaddr);
|
|---|
| 92 | uint32_t sdr1 = sdr1_get();
|
|---|
| 93 |
|
|---|
| 94 | // FIXME: compute size of PHT exactly
|
|---|
| 95 | phte_t *phte = (phte_t *) PA2KA(sdr1 & 0xffff0000);
|
|---|
| 96 |
|
|---|
| 97 | /* Primary hash (xor) */
|
|---|
| 98 | uint32_t h = 0;
|
|---|
| 99 | uint32_t hash = vsid ^ page;
|
|---|
| 100 | uint32_t base = (hash & 0x3ff) << 3;
|
|---|
| 101 | uint32_t i;
|
|---|
| 102 | bool found = false;
|
|---|
| 103 |
|
|---|
| 104 | /* Find colliding PTE in PTEG */
|
|---|
| 105 | for (i = 0; i < 8; i++) {
|
|---|
| 106 | if ((phte[base + i].v)
|
|---|
| 107 | && (phte[base + i].vsid == vsid)
|
|---|
| 108 | && (phte[base + i].api == api)
|
|---|
| 109 | && (phte[base + i].h == 0)) {
|
|---|
| 110 | found = true;
|
|---|
| 111 | break;
|
|---|
| 112 | }
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | if (!found) {
|
|---|
| 116 | /* Find unused PTE in PTEG */
|
|---|
| 117 | for (i = 0; i < 8; i++) {
|
|---|
| 118 | if (!phte[base + i].v) {
|
|---|
| 119 | found = true;
|
|---|
| 120 | break;
|
|---|
| 121 | }
|
|---|
| 122 | }
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | if (!found) {
|
|---|
| 126 | /* Secondary hash (not) */
|
|---|
| 127 | uint32_t base2 = (~hash & 0x3ff) << 3;
|
|---|
| 128 |
|
|---|
| 129 | /* Find colliding PTE in PTEG */
|
|---|
| 130 | for (i = 0; i < 8; i++) {
|
|---|
| 131 | if ((phte[base2 + i].v)
|
|---|
| 132 | && (phte[base2 + i].vsid == vsid)
|
|---|
| 133 | && (phte[base2 + i].api == api)
|
|---|
| 134 | && (phte[base2 + i].h == 1)) {
|
|---|
| 135 | found = true;
|
|---|
| 136 | base = base2;
|
|---|
| 137 | h = 1;
|
|---|
| 138 | break;
|
|---|
| 139 | }
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | if (!found) {
|
|---|
| 143 | /* Find unused PTE in PTEG */
|
|---|
| 144 | for (i = 0; i < 8; i++) {
|
|---|
| 145 | if (!phte[base2 + i].v) {
|
|---|
| 146 | found = true;
|
|---|
| 147 | base = base2;
|
|---|
| 148 | h = 1;
|
|---|
| 149 | break;
|
|---|
| 150 | }
|
|---|
| 151 | }
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | if (!found)
|
|---|
| 155 | i = RANDI(seed) % 8;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | phte[base + i].v = 1;
|
|---|
| 159 | phte[base + i].vsid = vsid;
|
|---|
| 160 | phte[base + i].h = h;
|
|---|
| 161 | phte[base + i].api = api;
|
|---|
| 162 | phte[base + i].rpn = pte->pfn;
|
|---|
| 163 | phte[base + i].r = 0;
|
|---|
| 164 | phte[base + i].c = 0;
|
|---|
| 165 | phte[base + i].wimg = (pte->page_cache_disable ? WIMG_NO_CACHE : 0);
|
|---|
| 166 | phte[base + i].pp = 2; // FIXME
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | /** Process Instruction/Data Storage Exception
|
|---|
| 170 | *
|
|---|
| 171 | * @param n Exception vector number.
|
|---|
| 172 | * @param istate Interrupted register context.
|
|---|
| 173 | *
|
|---|
| 174 | */
|
|---|
| 175 | void pht_refill(unsigned int n, istate_t *istate)
|
|---|
| 176 | {
|
|---|
| 177 | uintptr_t badvaddr;
|
|---|
| 178 |
|
|---|
| 179 | if (n == VECTOR_DATA_STORAGE)
|
|---|
| 180 | badvaddr = istate->dar;
|
|---|
| 181 | else
|
|---|
| 182 | badvaddr = istate->pc;
|
|---|
| 183 |
|
|---|
| 184 | pte_t *pte = find_mapping_and_check(AS, badvaddr,
|
|---|
| 185 | PF_ACCESS_READ /* FIXME */, istate);
|
|---|
| 186 |
|
|---|
| 187 | if (pte) {
|
|---|
| 188 | /* Record access to PTE */
|
|---|
| 189 | pte->accessed = 1;
|
|---|
| 190 | pht_insert(badvaddr, pte);
|
|---|
| 191 | }
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | void pht_invalidate(as_t *as, uintptr_t page, size_t pages)
|
|---|
| 195 | {
|
|---|
| 196 | uint32_t sdr1 = sdr1_get();
|
|---|
| 197 |
|
|---|
| 198 | // FIXME: compute size of PHT exactly
|
|---|
| 199 | phte_t *phte = (phte_t *) PA2KA(sdr1 & 0xffff0000);
|
|---|
| 200 |
|
|---|
| 201 | // FIXME: this invalidates all PHT entries,
|
|---|
| 202 | // which is an overkill, invalidate only
|
|---|
| 203 | // selectively
|
|---|
| 204 | for (size_t i = 0; i < 8192; i++) {
|
|---|
| 205 | phte[i].v = 0;
|
|---|
| 206 | }
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | /** @}
|
|---|
| 210 | */
|
|---|