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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b2fa1204 was b2fa1204, checked in by Martin Sucha <sucha14@…>, 12 years ago

Cherrypick usage of kernel logger

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