source: mainline/kernel/arch/sparc64/src/mm/sun4v/tlb.c@ 59fb782

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 59fb782 was 1dbc43f, checked in by Jakub Jermar <jakub@…>, 13 years ago

Unify user page fault handling in as_page_fault().

  • Remove lots of architecture-dependent boilerplate code.
  • Property mode set to 100644
File size: 10.0 KB
Line 
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 <mm/tlb.h>
37#include <mm/as.h>
38#include <mm/asid.h>
39#include <arch/sun4v/hypercall.h>
40#include <arch/mm/frame.h>
41#include <arch/mm/page.h>
42#include <arch/mm/tte.h>
43#include <arch/mm/tlb.h>
44#include <arch/interrupt.h>
45#include <interrupt.h>
46#include <arch.h>
47#include <print.h>
48#include <typedefs.h>
49#include <config.h>
50#include <arch/trap/trap.h>
51#include <arch/trap/exception.h>
52#include <panic.h>
53#include <arch/asm.h>
54#include <arch/cpu.h>
55#include <arch/mm/pagesize.h>
56#include <genarch/mm/page_ht.h>
57
58#ifdef CONFIG_TSB
59#include <arch/mm/tsb.h>
60#endif
61
62static void itlb_pte_copy(pte_t *);
63static void dtlb_pte_copy(pte_t *, bool);
64
65/*
66 * The assembly language routine passes a 64-bit parameter to the Data Access
67 * MMU Miss and Data Access protection handlers, the parameter encapsulates
68 * a virtual address of the faulting page and the faulting context. The most
69 * significant 51 bits represent the VA of the faulting page and the least
70 * significant 13 vits represent the faulting context. The following macros
71 * extract the page and context out of the 64-bit parameter:
72 */
73
74/* extracts the VA of the faulting page */
75#define DMISS_ADDRESS(page_and_ctx) (((page_and_ctx) >> 13) << 13)
76
77/* extracts the faulting context */
78#define DMISS_CONTEXT(page_and_ctx) ((page_and_ctx) & 0x1fff)
79
80/**
81 * Descriptions of fault types from the MMU Fault status area.
82 *
83 * fault_type[i] contains description of error for which the IFT or DFT
84 * field of the MMU fault status area is i.
85 */
86static const char *fault_types[] = {
87 "unknown",
88 "fast miss",
89 "fast protection",
90 "MMU miss",
91 "invalid RA",
92 "privileged violation",
93 "protection violation",
94 "NFO access",
95 "so page/NFO side effect",
96 "invalid VA",
97 "invalid ASI",
98 "nc atomic",
99 "privileged action",
100 "unknown",
101 "unaligned access",
102 "invalid page size"
103 };
104
105/** Array of MMU fault status areas. */
106extern mmu_fault_status_area_t mmu_fsas[MAX_NUM_STRANDS];
107
108/*
109 * Invalidate all non-locked DTLB and ITLB entries.
110 */
111void tlb_arch_init(void)
112{
113 tlb_invalidate_all();
114}
115
116/** Insert privileged mapping into DMMU TLB.
117 *
118 * @param page Virtual page address.
119 * @param frame Physical frame address.
120 * @param pagesize Page size.
121 * @param locked True for permanent mappings, false otherwise.
122 * @param cacheable True if the mapping is cacheable, false otherwise.
123 */
124void dtlb_insert_mapping(uintptr_t page, uintptr_t frame, int pagesize,
125 bool locked, bool cacheable)
126{
127 tte_data_t data;
128
129 data.value = 0;
130 data.v = true;
131 data.nfo = false;
132 data.ra = frame >> FRAME_WIDTH;
133 data.ie = false;
134 data.e = false;
135 data.cp = cacheable;
136#ifdef CONFIG_VIRT_IDX_DCACHE
137 data.cv = cacheable;
138#endif
139 data.p = true;
140 data.x = false;
141 data.w = true;
142 data.size = pagesize;
143
144 if (locked) {
145 __hypercall_fast4(
146 MMU_MAP_PERM_ADDR, page, 0, data.value, MMU_FLAG_DTLB);
147 } else {
148 __hypercall_hyperfast(
149 page, ASID_KERNEL, data.value, MMU_FLAG_DTLB, 0,
150 MMU_MAP_ADDR);
151 }
152}
153
154/** Copy PTE to TLB.
155 *
156 * @param t Page Table Entry to be copied.
157 * @param ro If true, the entry will be created read-only, regardless
158 * of its w field.
159 */
160void dtlb_pte_copy(pte_t *t, bool ro)
161{
162 tte_data_t data;
163
164 data.value = 0;
165 data.v = true;
166 data.nfo = false;
167 data.ra = (t->frame) >> FRAME_WIDTH;
168 data.ie = false;
169 data.e = false;
170 data.cp = t->c;
171#ifdef CONFIG_VIRT_IDX_DCACHE
172 data.cv = t->c;
173#endif
174 data.p = t->k;
175 data.x = false;
176 data.w = ro ? false : t->w;
177 data.size = PAGESIZE_8K;
178
179 __hypercall_hyperfast(
180 t->page, t->as->asid, data.value, MMU_FLAG_DTLB, 0, MMU_MAP_ADDR);
181}
182
183/** Copy PTE to ITLB.
184 *
185 * @param t Page Table Entry to be copied.
186 */
187void itlb_pte_copy(pte_t *t)
188{
189 tte_data_t data;
190
191 data.value = 0;
192 data.v = true;
193 data.nfo = false;
194 data.ra = (t->frame) >> FRAME_WIDTH;
195 data.ie = false;
196 data.e = false;
197 data.cp = t->c;
198 data.cv = false;
199 data.p = t->k;
200 data.x = true;
201 data.w = false;
202 data.size = PAGESIZE_8K;
203
204 __hypercall_hyperfast(
205 t->page, t->as->asid, data.value, MMU_FLAG_ITLB, 0, MMU_MAP_ADDR);
206}
207
208/** ITLB miss handler. */
209void fast_instruction_access_mmu_miss(sysarg_t unused, istate_t *istate)
210{
211 uintptr_t va = ALIGN_DOWN(istate->tpc, PAGE_SIZE);
212 pte_t *t;
213
214 t = page_mapping_find(AS, va, true);
215
216 if (t && PTE_EXECUTABLE(t)) {
217 /*
218 * The mapping was found in the software page hash table.
219 * Insert it into ITLB.
220 */
221 t->a = true;
222 itlb_pte_copy(t);
223#ifdef CONFIG_TSB
224 itsb_pte_copy(t);
225#endif
226 } else {
227 /*
228 * Forward the page fault to the address space page fault
229 * handler.
230 */
231 as_page_fault(va, PF_ACCESS_EXEC, istate);
232 }
233}
234
235/** DTLB miss handler.
236 *
237 * Note that some faults (e.g. kernel faults) were already resolved by the
238 * low-level, assembly language part of the fast_data_access_mmu_miss handler.
239 *
240 * @param page_and_ctx A 64-bit value describing the fault. The most
241 * significant 51 bits of the value contain the virtual
242 * address which caused the fault truncated to the page
243 * boundary. The least significant 13 bits of the value
244 * contain the number of the context in which the fault
245 * occurred.
246 * @param istate Interrupted state saved on the stack.
247 */
248void fast_data_access_mmu_miss(uint64_t page_and_ctx, istate_t *istate)
249{
250 pte_t *t;
251 uintptr_t va = DMISS_ADDRESS(page_and_ctx);
252 uint16_t ctx = DMISS_CONTEXT(page_and_ctx);
253
254 if (ctx == ASID_KERNEL) {
255 if (va == 0) {
256 /* NULL access in kernel */
257 panic("NULL pointer dereference.");
258 }
259 panic("Unexpected kernel page fault.");
260 }
261
262 t = page_mapping_find(AS, va, true);
263 if (t) {
264 /*
265 * The mapping was found in the software page hash table.
266 * Insert it into DTLB.
267 */
268 t->a = true;
269 dtlb_pte_copy(t, true);
270#ifdef CONFIG_TSB
271 dtsb_pte_copy(t, true);
272#endif
273 } else {
274 /*
275 * Forward the page fault to the address space page fault
276 * handler.
277 */
278 as_page_fault(va, PF_ACCESS_READ, istate);
279 }
280}
281
282/** DTLB protection fault handler.
283 *
284 * @param page_and_ctx A 64-bit value describing the fault. The most
285 * significant 51 bits of the value contain the virtual
286 * address which caused the fault truncated to the page
287 * boundary. The least significant 13 bits of the value
288 * contain the number of the context in which the fault
289 * occurred.
290 * @param istate Interrupted state saved on the stack.
291 */
292void fast_data_access_protection(uint64_t page_and_ctx, istate_t *istate)
293{
294 pte_t *t;
295 uintptr_t va = DMISS_ADDRESS(page_and_ctx);
296 uint16_t ctx = DMISS_CONTEXT(page_and_ctx);
297
298 t = page_mapping_find(AS, va, true);
299 if (t && PTE_WRITABLE(t)) {
300 /*
301 * The mapping was found in the software page hash table and is
302 * writable. Demap the old mapping and insert an updated mapping
303 * into DTLB.
304 */
305 t->a = true;
306 t->d = true;
307 mmu_demap_page(va, ctx, MMU_FLAG_DTLB);
308 dtlb_pte_copy(t, false);
309#ifdef CONFIG_TSB
310 dtsb_pte_copy(t, false);
311#endif
312 } else {
313 /*
314 * Forward the page fault to the address space page fault
315 * handler.
316 */
317 as_page_fault(va, PF_ACCESS_WRITE, istate);
318 }
319}
320
321/*
322 * On Niagara this function does not work, as supervisor software is isolated
323 * from the TLB by the hypervisor and has no chance to investigate the TLB
324 * entries.
325 */
326void tlb_print(void)
327{
328 printf("Operation not possible on Niagara.\n");
329}
330
331/**
332 * Describes the exact condition which caused the last DMMU fault.
333 */
334void describe_dmmu_fault(void)
335{
336 uint64_t myid;
337 __hypercall_fast_ret1(0, 0, 0, 0, 0, CPU_MYID, &myid);
338
339 ASSERT(mmu_fsas[myid].dft < 16);
340
341 printf("condition which caused the fault: %s\n",
342 fault_types[mmu_fsas[myid].dft]);
343}
344
345/** Invalidate all unlocked ITLB and DTLB entries. */
346void tlb_invalidate_all(void)
347{
348 uint64_t errno = __hypercall_fast3(MMU_DEMAP_ALL, 0, 0,
349 MMU_FLAG_DTLB | MMU_FLAG_ITLB);
350 if (errno != HV_EOK)
351 panic("Error code = %" PRIu64 ".\n", errno);
352}
353
354/** Invalidate all ITLB and DTLB entries that belong to specified ASID
355 * (Context).
356 *
357 * @param asid Address Space ID.
358 */
359void tlb_invalidate_asid(asid_t asid)
360{
361 /* switch to nucleus because we are mapped by the primary context */
362 nucleus_enter();
363
364 __hypercall_fast4(MMU_DEMAP_CTX, 0, 0, asid,
365 MMU_FLAG_ITLB | MMU_FLAG_DTLB);
366
367 nucleus_leave();
368}
369
370/** Invalidate all ITLB and DTLB entries for specified page range in specified
371 * address space.
372 *
373 * @param asid Address Space ID.
374 * @param page First page which to sweep out from ITLB and DTLB.
375 * @param cnt Number of ITLB and DTLB entries to invalidate.
376 */
377void tlb_invalidate_pages(asid_t asid, uintptr_t page, size_t cnt)
378{
379 unsigned int i;
380
381 /* switch to nucleus because we are mapped by the primary context */
382 nucleus_enter();
383
384 for (i = 0; i < cnt; i++) {
385 __hypercall_fast5(MMU_DEMAP_PAGE, 0, 0, page, asid,
386 MMU_FLAG_DTLB | MMU_FLAG_ITLB);
387 }
388
389 nucleus_leave();
390}
391
392/** @}
393 */
Note: See TracBrowser for help on using the repository browser.