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 kernel_ppc32_mm
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <arch/mm/pht.h>
|
---|
36 | #include <arch/mm/tlb.h>
|
---|
37 | #include <assert.h>
|
---|
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.
|
---|
52 | * @param[out] pte Structure that will receive a copy of the found PTE.
|
---|
53 | *
|
---|
54 | * @return True if the mapping was found, false otherwise.
|
---|
55 | *
|
---|
56 | */
|
---|
57 | static bool find_mapping_and_check(as_t *as, uintptr_t badvaddr, int access,
|
---|
58 | istate_t *istate, pte_t *pte)
|
---|
59 | {
|
---|
60 | /*
|
---|
61 | * Check if the mapping exists in page tables.
|
---|
62 | */
|
---|
63 | bool found = page_mapping_find(as, badvaddr, true, pte);
|
---|
64 | if (found && pte->present) {
|
---|
65 | /*
|
---|
66 | * Mapping found in page tables.
|
---|
67 | * Immediately succeed.
|
---|
68 | */
|
---|
69 | return true;
|
---|
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) {
|
---|
76 | /*
|
---|
77 | * The higher-level page fault handler succeeded,
|
---|
78 | * The mapping ought to be in place.
|
---|
79 | */
|
---|
80 | found = page_mapping_find(as, badvaddr, true, pte);
|
---|
81 |
|
---|
82 | assert(found);
|
---|
83 | assert(pte->present);
|
---|
84 |
|
---|
85 | return found;
|
---|
86 | }
|
---|
87 |
|
---|
88 | return false;
|
---|
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;
|
---|
95 |
|
---|
96 | uint32_t vsid = sr_get(vaddr);
|
---|
97 | uint32_t sdr1 = sdr1_get();
|
---|
98 |
|
---|
99 | // FIXME: compute size of PHT exactly
|
---|
100 | phte_t *phte = (phte_t *) PA2KA(sdr1 & 0xffff0000);
|
---|
101 |
|
---|
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;
|
---|
108 |
|
---|
109 | /* Find colliding PTE in PTEG */
|
---|
110 | for (i = 0; i < 8; i++) {
|
---|
111 | if ((phte[base + i].v) &&
|
---|
112 | (phte[base + i].vsid == vsid) &&
|
---|
113 | (phte[base + i].api == api) &&
|
---|
114 | (phte[base + i].h == 0)) {
|
---|
115 | found = true;
|
---|
116 | break;
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
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 | }
|
---|
129 |
|
---|
130 | if (!found) {
|
---|
131 | /* Secondary hash (not) */
|
---|
132 | uint32_t base2 = (~hash & 0x3ff) << 3;
|
---|
133 |
|
---|
134 | /* Find colliding PTE in PTEG */
|
---|
135 | for (i = 0; i < 8; i++) {
|
---|
136 | if ((phte[base2 + i].v) &&
|
---|
137 | (phte[base2 + i].vsid == vsid) &&
|
---|
138 | (phte[base2 + i].api == api) &&
|
---|
139 | (phte[base2 + i].h == 1)) {
|
---|
140 | found = true;
|
---|
141 | base = base2;
|
---|
142 | h = 1;
|
---|
143 | break;
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
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 | }
|
---|
158 |
|
---|
159 | if (!found)
|
---|
160 | i = RANDI(seed) % 8;
|
---|
161 | }
|
---|
162 |
|
---|
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;
|
---|
183 |
|
---|
184 | if (n == VECTOR_DATA_STORAGE)
|
---|
185 | badvaddr = istate->dar;
|
---|
186 | else
|
---|
187 | badvaddr = istate->pc;
|
---|
188 |
|
---|
189 | pte_t pte;
|
---|
190 | bool found = find_mapping_and_check(AS, badvaddr,
|
---|
191 | PF_ACCESS_READ /* FIXME */, istate, &pte);
|
---|
192 |
|
---|
193 | if (found) {
|
---|
194 | /* Record access to PTE */
|
---|
195 | pte.accessed = 1;
|
---|
196 | pht_insert(badvaddr, &pte);
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | void pht_invalidate(as_t *as, uintptr_t page, size_t pages)
|
---|
201 | {
|
---|
202 | uint32_t sdr1 = sdr1_get();
|
---|
203 |
|
---|
204 | // FIXME: compute size of PHT exactly
|
---|
205 | phte_t *phte = (phte_t *) PA2KA(sdr1 & 0xffff0000);
|
---|
206 |
|
---|
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 | */
|
---|