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