1 | /*
|
---|
2 | * Copyright (c) 2005 Jakub Jermar
|
---|
3 | * Copyright (c) 2008 Pavel Rimsky
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** @addtogroup sparc64mm
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 | /** @file
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <arch/mm/tlb.h>
|
---|
37 | #include <mm/tlb.h>
|
---|
38 | #include <mm/as.h>
|
---|
39 | #include <mm/asid.h>
|
---|
40 | #include <arch/sun4v/hypercall.h>
|
---|
41 | #include <arch/mm/frame.h>
|
---|
42 | #include <arch/mm/page.h>
|
---|
43 | #include <arch/mm/tte.h>
|
---|
44 | #include <arch/mm/tlb.h>
|
---|
45 | #include <arch/interrupt.h>
|
---|
46 | #include <interrupt.h>
|
---|
47 | #include <arch.h>
|
---|
48 | #include <print.h>
|
---|
49 | #include <arch/types.h>
|
---|
50 | #include <config.h>
|
---|
51 | #include <arch/trap/trap.h>
|
---|
52 | #include <arch/trap/exception.h>
|
---|
53 | #include <panic.h>
|
---|
54 | #include <arch/asm.h>
|
---|
55 | #include <arch/cpu.h>
|
---|
56 | #include <arch/mm/pagesize.h>
|
---|
57 |
|
---|
58 | #ifdef CONFIG_TSB
|
---|
59 | #include <arch/mm/tsb.h>
|
---|
60 | #endif
|
---|
61 |
|
---|
62 | void dtlb_pte_copy(pte_t *t, bool ro);
|
---|
63 | static void itlb_pte_copy(pte_t *);
|
---|
64 | static void do_fast_instruction_access_mmu_miss_fault(istate_t *, const char *);
|
---|
65 | void do_fast_data_access_mmu_miss_fault(istate_t *istate,
|
---|
66 | uint64_t page_and_ctx, const char *str);
|
---|
67 | static void do_fast_data_access_protection_fault(istate_t *,
|
---|
68 | uint64_t, const char *);
|
---|
69 |
|
---|
70 | /*
|
---|
71 | * The assembly language routine passes a 64-bit parameter to the Data Access
|
---|
72 | * MMU Miss and Data Access protection handlers, the parameter encapsulates
|
---|
73 | * a virtual address of the faulting page and the faulting context. The most
|
---|
74 | * significant 51 bits represent the VA of the faulting page and the least
|
---|
75 | * significant 13 vits represent the faulting context. The following macros
|
---|
76 | * extract the page and context out of the 64-bit parameter:
|
---|
77 | */
|
---|
78 |
|
---|
79 | /* extracts the VA of the faulting page */
|
---|
80 | #define DMISS_ADDRESS(page_and_ctx) (((page_and_ctx) >> 13) << 13)
|
---|
81 |
|
---|
82 | /* extracts the faulting context */
|
---|
83 | #define DMISS_CONTEXT(page_and_ctx) ((page_and_ctx) & 0x1fff)
|
---|
84 |
|
---|
85 | char *context_encoding[] = {
|
---|
86 | "Primary",
|
---|
87 | "Secondary",
|
---|
88 | "Nucleus",
|
---|
89 | "Reserved"
|
---|
90 | };
|
---|
91 |
|
---|
92 | /** Invalidate all unlocked ITLB and DTLB entries. */
|
---|
93 | void tlb_invalidate_all(void)
|
---|
94 | {
|
---|
95 | uint64_t errno = __hypercall_fast3(MMU_DEMAP_ALL, 0, 0,
|
---|
96 | MMU_FLAG_DTLB | MMU_FLAG_ITLB);
|
---|
97 | if (errno != EOK) {
|
---|
98 | panic("Error code = %d.\n", errno);
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | void tlb_arch_init(void)
|
---|
103 | {
|
---|
104 | tlb_invalidate_all();
|
---|
105 | }
|
---|
106 |
|
---|
107 | /** Insert privileged mapping into DMMU TLB.
|
---|
108 | *
|
---|
109 | * @param page Virtual page address.
|
---|
110 | * @param frame Physical frame address.
|
---|
111 | * @param pagesize Page size.
|
---|
112 | * @param locked True for permanent mappings, false otherwise.
|
---|
113 | * @param cacheable True if the mapping is cacheable, false otherwise.
|
---|
114 | */
|
---|
115 | void dtlb_insert_mapping(uintptr_t page, uintptr_t frame, int pagesize,
|
---|
116 | bool locked, bool cacheable)
|
---|
117 | {
|
---|
118 | #if 0
|
---|
119 | tlb_tag_access_reg_t tag;
|
---|
120 | tlb_data_t data;
|
---|
121 | page_address_t pg;
|
---|
122 | frame_address_t fr;
|
---|
123 |
|
---|
124 | pg.address = page;
|
---|
125 | fr.address = frame;
|
---|
126 |
|
---|
127 | tag.context = ASID_KERNEL;
|
---|
128 | tag.vpn = pg.vpn;
|
---|
129 |
|
---|
130 | dtlb_tag_access_write(tag.value);
|
---|
131 |
|
---|
132 | data.value = 0;
|
---|
133 | data.v = true;
|
---|
134 | data.size = pagesize;
|
---|
135 | data.pfn = fr.pfn;
|
---|
136 | data.l = locked;
|
---|
137 | data.cp = cacheable;
|
---|
138 | #ifdef CONFIG_VIRT_IDX_DCACHE
|
---|
139 | data.cv = cacheable;
|
---|
140 | #endif /* CONFIG_VIRT_IDX_DCACHE */
|
---|
141 | data.p = true;
|
---|
142 | data.w = true;
|
---|
143 | data.g = false;
|
---|
144 |
|
---|
145 | dtlb_data_in_write(data.value);
|
---|
146 | #endif
|
---|
147 | }
|
---|
148 |
|
---|
149 | /** Copy PTE to TLB.
|
---|
150 | *
|
---|
151 | * @param t Page Table Entry to be copied.
|
---|
152 | * @param ro If true, the entry will be created read-only, regardless
|
---|
153 | * of its w field.
|
---|
154 | */
|
---|
155 | void dtlb_pte_copy(pte_t *t, bool ro)
|
---|
156 | {
|
---|
157 | tte_data_t data;
|
---|
158 |
|
---|
159 | data.value = 0;
|
---|
160 | data.v = true;
|
---|
161 | data.nfo = false;
|
---|
162 | data.ra = (t->frame) >> FRAME_WIDTH;
|
---|
163 | data.ie = false;
|
---|
164 | data.e = false;
|
---|
165 | data.cp = t->c;
|
---|
166 | #ifdef CONFIG_VIRT_IDX_DCACHE
|
---|
167 | data.cv = t->c;
|
---|
168 | #endif
|
---|
169 | data.p = t->k;
|
---|
170 | data.x = false;
|
---|
171 | data.w = ro ? false : t->w;
|
---|
172 | data.size = PAGESIZE_8K;
|
---|
173 |
|
---|
174 | __hypercall_hyperfast(
|
---|
175 | t->page, t->as->asid, data.value, MMU_FLAG_DTLB, 0, MMU_MAP_ADDR);
|
---|
176 | }
|
---|
177 |
|
---|
178 |
|
---|
179 | /** Copy PTE to ITLB.
|
---|
180 | *
|
---|
181 | * @param t Page Table Entry to be copied.
|
---|
182 | */
|
---|
183 | void itlb_pte_copy(pte_t *t)
|
---|
184 | {
|
---|
185 | tte_data_t data;
|
---|
186 |
|
---|
187 | data.value = 0;
|
---|
188 | data.v = true;
|
---|
189 | data.nfo = false;
|
---|
190 | data.ra = (t->frame) >> FRAME_WIDTH;
|
---|
191 | data.ie = false;
|
---|
192 | data.e = false;
|
---|
193 | data.cp = t->c;
|
---|
194 | data.cv = false;
|
---|
195 | data.p = t->k;
|
---|
196 | data.x = true;
|
---|
197 | data.w = false;
|
---|
198 | data.size = PAGESIZE_8K;
|
---|
199 |
|
---|
200 | __hypercall_hyperfast(
|
---|
201 | t->page, t->as->asid, data.value, MMU_FLAG_ITLB, 0, MMU_MAP_ADDR);
|
---|
202 | }
|
---|
203 |
|
---|
204 | /** ITLB miss handler. */
|
---|
205 | void fast_instruction_access_mmu_miss(unative_t unused, istate_t *istate)
|
---|
206 | {
|
---|
207 | uintptr_t va = ALIGN_DOWN(istate->tpc, PAGE_SIZE);
|
---|
208 | pte_t *t;
|
---|
209 |
|
---|
210 | page_table_lock(AS, true);
|
---|
211 | t = page_mapping_find(AS, va);
|
---|
212 |
|
---|
213 | if (t && PTE_EXECUTABLE(t)) {
|
---|
214 | /*
|
---|
215 | * The mapping was found in the software page hash table.
|
---|
216 | * Insert it into ITLB.
|
---|
217 | */
|
---|
218 | t->a = true;
|
---|
219 | itlb_pte_copy(t);
|
---|
220 | #ifdef CONFIG_TSB
|
---|
221 | itsb_pte_copy(t);
|
---|
222 | #endif
|
---|
223 | page_table_unlock(AS, true);
|
---|
224 | } else {
|
---|
225 | /*
|
---|
226 | * Forward the page fault to the address space page fault
|
---|
227 | * handler.
|
---|
228 | */
|
---|
229 | page_table_unlock(AS, true);
|
---|
230 | if (as_page_fault(va, PF_ACCESS_EXEC, istate) == AS_PF_FAULT) {
|
---|
231 | do_fast_instruction_access_mmu_miss_fault(istate,
|
---|
232 | __func__);
|
---|
233 | }
|
---|
234 | }
|
---|
235 | }
|
---|
236 |
|
---|
237 | /** DTLB miss handler.
|
---|
238 | *
|
---|
239 | * Note that some faults (e.g. kernel faults) were already resolved by the
|
---|
240 | * low-level, assembly language part of the fast_data_access_mmu_miss handler.
|
---|
241 | *
|
---|
242 | * @param page_and_ctx A 64-bit value describing the fault. The most
|
---|
243 | * significant 51 bits of the value contain the virtual
|
---|
244 | * address which caused the fault truncated to the page
|
---|
245 | * boundary. The least significant 13 bits of the value
|
---|
246 | * contain the number of the context in which the fault
|
---|
247 | * occurred.
|
---|
248 | * @param istate Interrupted state saved on the stack.
|
---|
249 | */
|
---|
250 | void fast_data_access_mmu_miss(uint64_t page_and_ctx, istate_t *istate)
|
---|
251 | {
|
---|
252 | pte_t *t;
|
---|
253 | uintptr_t va = DMISS_ADDRESS(page_and_ctx);
|
---|
254 | uint16_t ctx = DMISS_CONTEXT(page_and_ctx);
|
---|
255 |
|
---|
256 | if (ctx == ASID_KERNEL) {
|
---|
257 | if (va == 0) {
|
---|
258 | /* NULL access in kernel */
|
---|
259 | do_fast_data_access_mmu_miss_fault(istate, page_and_ctx,
|
---|
260 | __func__);
|
---|
261 | }
|
---|
262 | do_fast_data_access_mmu_miss_fault(istate, page_and_ctx, "Unexpected "
|
---|
263 | "kernel page fault.");
|
---|
264 | }
|
---|
265 |
|
---|
266 | page_table_lock(AS, true);
|
---|
267 | t = page_mapping_find(AS, va);
|
---|
268 | if (t) {
|
---|
269 | /*
|
---|
270 | * The mapping was found in the software page hash table.
|
---|
271 | * Insert it into DTLB.
|
---|
272 | */
|
---|
273 | t->a = true;
|
---|
274 | dtlb_pte_copy(t, true);
|
---|
275 | #ifdef CONFIG_TSB
|
---|
276 | dtsb_pte_copy(t, true);
|
---|
277 | #endif
|
---|
278 | page_table_unlock(AS, true);
|
---|
279 | } else {
|
---|
280 | /*
|
---|
281 | * Forward the page fault to the address space page fault
|
---|
282 | * handler.
|
---|
283 | */
|
---|
284 | page_table_unlock(AS, true);
|
---|
285 | if (as_page_fault(va, PF_ACCESS_READ, istate) == AS_PF_FAULT) {
|
---|
286 | do_fast_data_access_mmu_miss_fault(istate, page_and_ctx,
|
---|
287 | __func__);
|
---|
288 | }
|
---|
289 | }
|
---|
290 | asm volatile ("sethi 0x41941, %g0");
|
---|
291 | }
|
---|
292 |
|
---|
293 | /** DTLB protection fault handler.
|
---|
294 | *
|
---|
295 | * @param page_and_ctx A 64-bit value describing the fault. The most
|
---|
296 | * significant 51 bits of the value contain the virtual
|
---|
297 | * address which caused the fault truncated to the page
|
---|
298 | * boundary. The least significant 13 bits of the value
|
---|
299 | * contain the number of the context in which the fault
|
---|
300 | * occurred.
|
---|
301 | * @param istate Interrupted state saved on the stack.
|
---|
302 | */
|
---|
303 | void fast_data_access_protection(uint64_t page_and_ctx, istate_t *istate)
|
---|
304 | {
|
---|
305 | pte_t *t;
|
---|
306 | uintptr_t va = DMISS_ADDRESS(page_and_ctx);
|
---|
307 | uint16_t ctx = DMISS_CONTEXT(page_and_ctx);
|
---|
308 |
|
---|
309 | page_table_lock(AS, true);
|
---|
310 | t = page_mapping_find(AS, va);
|
---|
311 | if (t && PTE_WRITABLE(t)) {
|
---|
312 | /*
|
---|
313 | * The mapping was found in the software page hash table and is
|
---|
314 | * writable. Demap the old mapping and insert an updated mapping
|
---|
315 | * into DTLB.
|
---|
316 | */
|
---|
317 | t->a = true;
|
---|
318 | t->d = true;
|
---|
319 | mmu_demap_page(va, ctx, MMU_FLAG_DTLB);
|
---|
320 | dtlb_pte_copy(t, false);
|
---|
321 | #ifdef CONFIG_TSB
|
---|
322 | dtsb_pte_copy(t, false);
|
---|
323 | #endif
|
---|
324 | page_table_unlock(AS, true);
|
---|
325 | } else {
|
---|
326 | /*
|
---|
327 | * Forward the page fault to the address space page fault
|
---|
328 | * handler.
|
---|
329 | */
|
---|
330 | page_table_unlock(AS, true);
|
---|
331 | if (as_page_fault(va, PF_ACCESS_WRITE, istate) == AS_PF_FAULT) {
|
---|
332 | do_fast_data_access_protection_fault(istate, page_and_ctx,
|
---|
333 | __func__);
|
---|
334 | }
|
---|
335 | }
|
---|
336 | }
|
---|
337 |
|
---|
338 |
|
---|
339 | /** Print TLB entry (for debugging purposes).
|
---|
340 | *
|
---|
341 | * The diag field has been left out in order to make this function more generic
|
---|
342 | * (there is no diag field in US3 architeture).
|
---|
343 | *
|
---|
344 | * @param i TLB entry number
|
---|
345 | * @param t TLB entry tag
|
---|
346 | * @param d TLB entry data
|
---|
347 | */
|
---|
348 | void tlb_print(void)
|
---|
349 | {
|
---|
350 | printf("Operation not possible on Niagara.\n");
|
---|
351 | }
|
---|
352 |
|
---|
353 | void do_fast_instruction_access_mmu_miss_fault(istate_t *istate,
|
---|
354 | const char *str)
|
---|
355 | {
|
---|
356 | fault_if_from_uspace(istate, "%s.", str);
|
---|
357 | dump_istate(istate);
|
---|
358 | panic("%s.", str);
|
---|
359 | }
|
---|
360 |
|
---|
361 | void do_fast_data_access_mmu_miss_fault(istate_t *istate,
|
---|
362 | uint64_t page_and_ctx, const char *str)
|
---|
363 | {
|
---|
364 | if (DMISS_CONTEXT(page_and_ctx)) {
|
---|
365 | fault_if_from_uspace(istate, "%s, Page=%p (ASID=%d)\n", str, DMISS_ADDRESS(page_and_ctx),
|
---|
366 | DMISS_CONTEXT(page_and_ctx));
|
---|
367 | }
|
---|
368 | dump_istate(istate);
|
---|
369 | printf("Faulting page: %p, ASID=%d\n", DMISS_ADDRESS(page_and_ctx), DMISS_CONTEXT(page_and_ctx));
|
---|
370 | panic("%s\n", str);
|
---|
371 | }
|
---|
372 |
|
---|
373 | void do_fast_data_access_protection_fault(istate_t *istate,
|
---|
374 | uint64_t page_and_ctx, const char *str)
|
---|
375 | {
|
---|
376 | if (DMISS_CONTEXT(page_and_ctx)) {
|
---|
377 | fault_if_from_uspace(istate, "%s, Page=%p (ASID=%d)\n", str, DMISS_ADDRESS(page_and_ctx),
|
---|
378 | DMISS_CONTEXT(page_and_ctx));
|
---|
379 | }
|
---|
380 | printf("Faulting page: %p, ASID=%d\n", DMISS_ADDRESS(page_and_ctx), DMISS_CONTEXT(page_and_ctx));
|
---|
381 | dump_istate(istate);
|
---|
382 | panic("%s\n", str);
|
---|
383 | }
|
---|
384 |
|
---|
385 | /**
|
---|
386 | * Describes the exact condition which caused the last DMMU fault.
|
---|
387 | */
|
---|
388 | void describe_dmmu_fault(void)
|
---|
389 | {
|
---|
390 | #if 0
|
---|
391 | uint64_t myid;
|
---|
392 | __hypercall_fast_ret1(0, 0, 0, 0, 0, CPU_MYID, &myid);
|
---|
393 |
|
---|
394 | ASSERT(mmu_fsas[myid].dft < 16);
|
---|
395 |
|
---|
396 | printf("condition which caused the fault: %s\n",
|
---|
397 | fault_types[mmu_fsas[myid].dft]);
|
---|
398 | }
|
---|
399 |
|
---|
400 | /** Invalidate all unlocked ITLB and DTLB entries. */
|
---|
401 | void tlb_invalidate_all(void)
|
---|
402 | {
|
---|
403 | uint64_t errno = __hypercall_fast3(MMU_DEMAP_ALL, 0, 0,
|
---|
404 | MMU_FLAG_DTLB | MMU_FLAG_ITLB);
|
---|
405 | if (errno != EOK) {
|
---|
406 | panic("Error code = %d.\n", errno);
|
---|
407 | }
|
---|
408 | #endif
|
---|
409 | }
|
---|
410 |
|
---|
411 | /** Invalidate all ITLB and DTLB entries that belong to specified ASID
|
---|
412 | * (Context).
|
---|
413 | *
|
---|
414 | * @param asid Address Space ID.
|
---|
415 | */
|
---|
416 | void tlb_invalidate_asid(asid_t asid)
|
---|
417 | {
|
---|
418 | /* switch to nucleus because we are mapped by the primary context */
|
---|
419 | nucleus_enter();
|
---|
420 | __hypercall_fast4(MMU_DEMAP_CTX, 0, 0, asid,
|
---|
421 | MMU_FLAG_ITLB | MMU_FLAG_DTLB);
|
---|
422 |
|
---|
423 | nucleus_leave();
|
---|
424 | }
|
---|
425 |
|
---|
426 | /** Invalidate all ITLB and DTLB entries for specified page range in specified
|
---|
427 | * address space.
|
---|
428 | *
|
---|
429 | * @param asid Address Space ID.
|
---|
430 | * @param page First page which to sweep out from ITLB and DTLB.
|
---|
431 | * @param cnt Number of ITLB and DTLB entries to invalidate.
|
---|
432 | */
|
---|
433 | void tlb_invalidate_pages(asid_t asid, uintptr_t page, size_t cnt)
|
---|
434 | {
|
---|
435 | unsigned int i;
|
---|
436 |
|
---|
437 | /* switch to nucleus because we are mapped by the primary context */
|
---|
438 | nucleus_enter();
|
---|
439 |
|
---|
440 | for (i = 0; i < cnt; i++) {
|
---|
441 | __hypercall_fast5(MMU_DEMAP_PAGE, 0, 0, page, asid,
|
---|
442 | MMU_FLAG_DTLB | MMU_FLAG_ITLB);
|
---|
443 | }
|
---|
444 |
|
---|
445 | nucleus_leave();
|
---|
446 | }
|
---|
447 |
|
---|
448 | /** @}
|
---|
449 | */
|
---|