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

Last change on this file was bab75df6, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Let kernel code get printf via the standard stdio header. Clean up unused includes.

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