author | Pat Downey <patd@symbian.org> |
Wed, 01 Sep 2010 12:34:56 +0100 | |
branch | RCL_3 |
changeset 44 | 3e88ff8f41d5 |
parent 43 | c1f20ce4abcf |
permissions | -rw-r--r-- |
0 | 1 |
// Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies). |
2 |
// All rights reserved. |
|
3 |
// This component and the accompanying materials are made available |
|
4 |
// under the terms of the License "Eclipse Public License v1.0" |
|
5 |
// which accompanies this distribution, and is available |
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 |
// |
|
8 |
// Initial Contributors: |
|
9 |
// Nokia Corporation - initial contribution. |
|
10 |
// |
|
11 |
// Contributors: |
|
12 |
// |
|
13 |
// Description: |
|
14 |
// e32\memmodel\epoc\multiple\arm\xmmu.cpp |
|
15 |
// |
|
16 |
// |
|
17 |
||
18 |
#include "arm_mem.h" |
|
19 |
#include <mmubase.inl> |
|
20 |
#include <ramcache.h> |
|
21 |
#include <demand_paging.h> |
|
22 |
#include "execs.h" |
|
23 |
#include <defrag.h> |
|
24 |
#include "cache_maintenance.inl" |
|
25 |
||
26 |
#undef __MMU_MACHINE_CODED__ |
|
27 |
||
28 |
// SECTION_PDE(perm, attr, domain, execute, global) |
|
29 |
// PT_PDE(domain) |
|
30 |
// LP_PTE(perm, attr, execute, global) |
|
31 |
// SP_PTE(perm, attr, execute, global) |
|
32 |
||
33 |
const TInt KPageColourShift=2; |
|
34 |
const TInt KPageColourCount=(1<<KPageColourShift); |
|
35 |
const TInt KPageColourMask=KPageColourCount-1; |
|
36 |
||
37 |
||
38 |
const TPde KPdPdePerm=PT_PDE(0); |
|
39 |
const TPde KPtPdePerm=PT_PDE(0); |
|
40 |
const TPde KShadowPdePerm=PT_PDE(0); |
|
41 |
||
42 |
#if defined(__CPU_MEMORY_TYPE_REMAPPING) |
|
43 |
// ARM1176, ARM11MPCore, ARMv7 and later |
|
44 |
// __CPU_MEMORY_TYPE_REMAPPING means that only three bits (TEX0:C:B) in page table define |
|
45 |
// memory attributes. Kernel runs with a limited set of memory types: stronlgy ordered, |
|
46 |
// device, normal un-cached & and normal WBWA. Due to lack of write through mode, page tables are |
|
47 |
// write-back which means that cache has to be cleaned on every page/directory table update. |
|
48 |
const TPte KPdPtePerm= SP_PTE(KArmV6PermRWNO, EMemAttNormalCached, 0, 1); |
|
49 |
const TPte KPtPtePerm= SP_PTE(KArmV6PermRWNO, EMemAttNormalCached, 0, 1); |
|
50 |
const TPte KPtInfoPtePerm= SP_PTE(KArmV6PermRWNO, EMemAttNormalCached, 0, 1); |
|
51 |
const TPte KRomPtePerm= SP_PTE(KArmV6PermRORO, EMemAttNormalCached, 1, 1); |
|
52 |
const TPte KShadowPtePerm= SP_PTE(KArmV6PermRORO, EMemAttNormalCached, 1, 1); |
|
53 |
const TPde KRomSectionPermissions= SECTION_PDE(KArmV6PermRORO, EMemAttNormalCached, 0, 1, 1); |
|
54 |
const TPte KUserCodeLoadPte= SP_PTE(KArmV6PermRWNO, EMemAttNormalCached, 1, 0); |
|
55 |
const TPte KUserCodeRunPte= SP_PTE(KArmV6PermRORO, EMemAttNormalCached, 1, 0); |
|
56 |
const TPte KGlobalCodeRunPte= SP_PTE(KArmV6PermRORO, EMemAttNormalCached, 1, 1); |
|
57 |
const TPte KKernelCodeRunPte= SP_PTE(KArmV6PermRONO, EMemAttNormalCached, 1, 1); |
|
58 |
||
59 |
const TInt KNormalUncachedAttr = EMemAttNormalUncached; |
|
60 |
const TInt KNormalCachedAttr = EMemAttNormalCached; |
|
61 |
||
62 |
#else |
|
63 |
||
64 |
//ARM1136 |
|
65 |
const TPte KPtInfoPtePerm=SP_PTE(KArmV6PermRWNO, KArmV6MemAttWBWAWBWA, 0, 1); |
|
66 |
#if defined (__CPU_WriteThroughDisabled) |
|
67 |
const TPte KPdPtePerm=SP_PTE(KArmV6PermRWNO, KArmV6MemAttWBWAWBWA, 0, 1); |
|
68 |
const TPte KPtPtePerm=SP_PTE(KArmV6PermRWNO, KArmV6MemAttWBWAWBWA, 0, 1); |
|
69 |
const TPte KRomPtePerm=SP_PTE(KArmV6PermRORO, KArmV6MemAttWBWAWBWA, 1, 1); |
|
70 |
const TPte KShadowPtePerm=SP_PTE(KArmV6PermRWRO, KArmV6MemAttWBWAWBWA, 1, 1); |
|
71 |
const TPde KRomSectionPermissions = SECTION_PDE(KArmV6PermRORO, KArmV6MemAttWBWAWBWA, 0, 1, 1); |
|
72 |
const TPte KUserCodeLoadPte=SP_PTE(KArmV6PermRWNO, KArmV6MemAttWBWAWBWA, 1, 0); |
|
73 |
const TPte KUserCodeRunPte=SP_PTE(KArmV6PermRWRO, KArmV6MemAttWBWAWBWA, 1, 0); |
|
74 |
const TPte KGlobalCodeRunPte=SP_PTE(KArmV6PermRWRO, KArmV6MemAttWBWAWBWA, 1, 1); |
|
75 |
const TInt KKernelCodeRunPteAttr = KArmV6MemAttWBWAWBWA; |
|
76 |
#else |
|
77 |
const TPte KPdPtePerm=SP_PTE(KArmV6PermRWNO, KArmV6MemAttWBRAWTRA, 0, 1); |
|
78 |
const TPte KPtPtePerm=SP_PTE(KArmV6PermRWNO, KArmV6MemAttWBRAWTRA, 0, 1); |
|
79 |
const TPte KRomPtePerm=SP_PTE(KArmV6PermRORO, KArmV6MemAttWTRAWTRA, 1, 1); |
|
80 |
const TPte KShadowPtePerm=SP_PTE(KArmV6PermRWRO, KArmV6MemAttWTRAWTRA, 1, 1); |
|
81 |
const TPde KRomSectionPermissions = SECTION_PDE(KArmV6PermRORO, KArmV6MemAttWTRAWTRA, 0, 1, 1); |
|
82 |
const TPte KUserCodeLoadPte=SP_PTE(KArmV6PermRWNO, KArmV6MemAttWTRAWTRA, 1, 0); |
|
83 |
const TPte KUserCodeRunPte=SP_PTE(KArmV6PermRWRO, KArmV6MemAttWTRAWTRA, 1, 0); |
|
84 |
const TPte KGlobalCodeRunPte=SP_PTE(KArmV6PermRWRO, KArmV6MemAttWTRAWTRA, 1, 1); |
|
85 |
const TInt KKernelCodeRunPteAttr = KArmV6MemAttWTRAWTRA; |
|
86 |
#endif |
|
87 |
||
88 |
||
89 |
#if defined(__CPU_ARM1136_ERRATUM_353494_FIXED) |
|
90 |
const TInt KKernelCodeRunPtePerm = KArmV6PermRONO; |
|
91 |
#else |
|
92 |
const TInt KKernelCodeRunPtePerm = KArmV6PermRORO; |
|
93 |
#endif |
|
94 |
const TPte KKernelCodeRunPte=SP_PTE(KKernelCodeRunPtePerm, KKernelCodeRunPteAttr, 1, 1); |
|
95 |
||
96 |
const TInt KNormalUncachedAttr = KArmV6MemAttNCNC; |
|
97 |
const TInt KNormalCachedAttr = KArmV6MemAttWBWAWBWA; |
|
98 |
||
99 |
#endif |
|
100 |
||
101 |
||
102 |
extern void __FlushBtb(); |
|
103 |
||
104 |
#if defined(__CPU_ARM1136__) && !defined(__CPU_ARM1136_ERRATUM_353494_FIXED) |
|
105 |
extern void remove_and_invalidate_page(TPte* aPte, TLinAddr aAddr, TInt aAsid); |
|
106 |
extern void remove_and_invalidate_section(TPde* aPde, TLinAddr aAddr, TInt aAsid); |
|
107 |
#endif |
|
108 |
||
109 |
||
110 |
LOCAL_D const TPte ChunkPtePermissions[ENumChunkTypes] = |
|
111 |
{ |
|
112 |
#if defined(__CPU_MEMORY_TYPE_REMAPPING) |
|
113 |
// ARM1176, ARM11 mcore, ARMv7 and later |
|
114 |
SP_PTE(KArmV6PermRWNO, EMemAttNormalCached, 0, 1), // EKernelData |
|
115 |
SP_PTE(KArmV6PermRWNO, EMemAttNormalCached, 0, 1), // EKernelStack |
|
116 |
SP_PTE(KArmV6PermRWNO, EMemAttNormalCached, 1, 1), // EKernelCode - loading |
|
117 |
SP_PTE(KArmV6PermRWNO, EMemAttNormalCached, 1, 1), // EDll (used for global code) - loading |
|
118 |
SP_PTE(KArmV6PermRORO, EMemAttNormalCached, 1, 0), // EUserCode - run |
|
119 |
SP_PTE(KArmV6PermRWRW, EMemAttNormalCached, 0, 1), // ERamDrive |
|
120 |
SP_PTE(KArmV6PermRWRW, EMemAttNormalCached, 0, 0), // EUserData |
|
121 |
SP_PTE(KArmV6PermRWRW, EMemAttNormalCached, 0, 0), // EDllData |
|
122 |
SP_PTE(KArmV6PermRWRW, EMemAttNormalCached, 1, 0), // EUserSelfModCode |
|
123 |
SP_PTE(KArmV6PermRWRW, EMemAttNormalCached, 0, 0), // ESharedKernelSingle |
|
124 |
SP_PTE(KArmV6PermRWRW, EMemAttNormalCached, 0, 0), // ESharedKernelMultiple |
|
125 |
SP_PTE(KArmV6PermRWRW, EMemAttNormalCached, 0, 0), // ESharedIo |
|
126 |
SP_PTE(KArmV6PermRWNO, EMemAttNormalCached, 0, 1), // ESharedKernelMirror |
|
127 |
SP_PTE(KArmV6PermRWNO, EMemAttNormalCached, 0, 1), // EKernelMessage |
|
128 |
#else |
|
129 |
SP_PTE(KArmV6PermRWNO, KArmV6MemAttWBWAWBWA, 0, 1), // EKernelData |
|
130 |
SP_PTE(KArmV6PermRWNO, KArmV6MemAttWBWAWBWA, 0, 1), // EKernelStack |
|
131 |
#if defined (__CPU_WriteThroughDisabled) |
|
132 |
SP_PTE(KArmV6PermRWNO, KArmV6MemAttWBWAWBWA, 1, 1), // EKernelCode - loading |
|
133 |
SP_PTE(KArmV6PermRWNO, KArmV6MemAttWBWAWBWA, 1, 1), // EDll (used for global code) - loading |
|
134 |
SP_PTE(KArmV6PermRWRO, KArmV6MemAttWBWAWBWA, 1, 0), // EUserCode - run |
|
135 |
#else |
|
136 |
SP_PTE(KArmV6PermRWNO, KArmV6MemAttWTRAWTRA, 1, 1), // EKernelCode - loading |
|
137 |
SP_PTE(KArmV6PermRWNO, KArmV6MemAttWTRAWTRA, 1, 1), // EDll (used for global code) - loading |
|
138 |
SP_PTE(KArmV6PermRWRO, KArmV6MemAttWTRAWTRA, 1, 0), // EUserCode - run |
|
139 |
#endif |
|
140 |
SP_PTE(KArmV6PermRWRW, KArmV6MemAttWBWAWBWA, 0, 1), // ERamDrive |
|
141 |
SP_PTE(KArmV6PermRWRW, KArmV6MemAttWBWAWBWA, 0, 0), // EUserData |
|
142 |
SP_PTE(KArmV6PermRWRW, KArmV6MemAttWBWAWBWA, 0, 0), // EDllData |
|
143 |
SP_PTE(KArmV6PermRWRW, KArmV6MemAttWBWAWBWA, 1, 0), // EUserSelfModCode |
|
144 |
SP_PTE(KArmV6PermRWRW, KArmV6MemAttWBWAWBWA, 0, 0), // ESharedKernelSingle |
|
145 |
SP_PTE(KArmV6PermRWRW, KArmV6MemAttWBWAWBWA, 0, 0), // ESharedKernelMultiple |
|
146 |
SP_PTE(KArmV6PermRWRW, KArmV6MemAttWBWAWBWA, 0, 0), // ESharedIo |
|
147 |
SP_PTE(KArmV6PermRWNO, KArmV6MemAttWBWAWBWA, 0, 1), // ESharedKernelMirror |
|
148 |
SP_PTE(KArmV6PermRWNO, KArmV6MemAttWBWAWBWA, 0, 1), // EKernelMessage |
|
149 |
#endif |
|
150 |
}; |
|
151 |
||
4
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
152 |
// The domain for each chunk is selected according to its type. |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
153 |
// The RamDrive lives in a separate domain, to minimise the risk |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
154 |
// of accidental access and corruption. User chunks may also be |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
155 |
// located in a separate domain (15) in DEBUG builds. |
0 | 156 |
LOCAL_D const TPde ChunkPdePermissions[ENumChunkTypes] = |
157 |
{ |
|
4
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
158 |
PT_PDE(0), // EKernelData |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
159 |
PT_PDE(0), // EKernelStack |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
160 |
PT_PDE(0), // EKernelCode |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
161 |
PT_PDE(0), // EDll |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
162 |
PT_PDE(USER_MEMORY_DOMAIN), // EUserCode |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
163 |
PT_PDE(1), // ERamDrive |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
164 |
PT_PDE(USER_MEMORY_DOMAIN), // EUserData |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
165 |
PT_PDE(USER_MEMORY_DOMAIN), // EDllData |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
166 |
PT_PDE(USER_MEMORY_DOMAIN), // EUserSelfModCode |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
167 |
PT_PDE(USER_MEMORY_DOMAIN), // ESharedKernelSingle |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
168 |
PT_PDE(USER_MEMORY_DOMAIN), // ESharedKernelMultiple |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
169 |
PT_PDE(0), // ESharedIo |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
170 |
PT_PDE(0), // ESharedKernelMirror |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
171 |
PT_PDE(0), // EKernelMessage |
0 | 172 |
}; |
173 |
||
174 |
// Inline functions for simple transformations |
|
175 |
inline TLinAddr PageTableLinAddr(TInt aId) |
|
176 |
{ |
|
177 |
return (KPageTableBase+(aId<<KPageTableShift)); |
|
178 |
} |
|
179 |
||
180 |
inline TPte* PageTable(TInt aId) |
|
181 |
{ |
|
182 |
return (TPte*)(KPageTableBase+(aId<<KPageTableShift)); |
|
183 |
} |
|
184 |
||
185 |
inline TPte* PageTableEntry(TInt aId, TLinAddr aAddress) |
|
186 |
{ |
|
187 |
return PageTable(aId) + ((aAddress >> KPageShift) & (KChunkMask >> KPageShift)); |
|
188 |
} |
|
189 |
||
190 |
inline TLinAddr PageDirectoryLinAddr(TInt aOsAsid) |
|
191 |
{ |
|
192 |
return (KPageDirectoryBase+(aOsAsid<<KPageDirectoryShift)); |
|
193 |
} |
|
194 |
||
195 |
inline TPde* PageDirectoryEntry(TInt aOsAsid, TLinAddr aAddress) |
|
196 |
{ |
|
197 |
return PageDirectory(aOsAsid) + (aAddress >> KChunkShift); |
|
198 |
} |
|
199 |
||
200 |
extern void InvalidateTLBForPage(TLinAddr /*aLinAddr*/, TInt /*aAsid*/); |
|
201 |
extern void FlushTLBs(); |
|
202 |
extern TUint32 TTCR(); |
|
203 |
||
204 |
TPte* SafePageTableFromPde(TPde aPde) |
|
205 |
{ |
|
206 |
if((aPde&KPdeTypeMask)==KArmV6PdePageTable) |
|
207 |
{ |
|
208 |
SPageInfo* pi = SPageInfo::SafeFromPhysAddr(aPde); |
|
209 |
if(pi) |
|
210 |
{ |
|
211 |
TInt id = (pi->Offset()<<KPtClusterShift) | ((aPde>>KPageTableShift)&KPtClusterMask); |
|
212 |
return PageTable(id); |
|
213 |
} |
|
214 |
} |
|
215 |
return 0; |
|
216 |
} |
|
217 |
||
218 |
TPte* SafePtePtrFromLinAddr(TLinAddr aAddress, TInt aOsAsid=0) |
|
219 |
{ |
|
220 |
if ((TInt)(aAddress>>KChunkShift)>=(TheMmu.iLocalPdSize>>2)) |
|
221 |
aOsAsid = 0; |
|
222 |
TPde pde = PageDirectory(aOsAsid)[aAddress>>KChunkShift]; |
|
223 |
TPte* pt = SafePageTableFromPde(pde); |
|
224 |
if(pt) |
|
225 |
pt += (aAddress>>KPageShift)&(KChunkMask>>KPageShift); |
|
226 |
return pt; |
|
227 |
} |
|
228 |
||
229 |
#ifndef _DEBUG |
|
230 |
// inline in UREL builds... |
|
231 |
#ifdef __ARMCC__ |
|
232 |
__forceinline /* RVCT ignores normal inline qualifier :-( */ |
|
233 |
#else |
|
234 |
inline |
|
235 |
#endif |
|
236 |
#endif |
|
237 |
TPte* PtePtrFromLinAddr(TLinAddr aAddress, TInt aOsAsid=0) |
|
238 |
{ |
|
239 |
// this function only works for process local memory addresses, or for kernel memory (asid==0). |
|
240 |
__NK_ASSERT_DEBUG(aOsAsid==0 || (TInt)(aAddress>>KChunkShift)<(TheMmu.iLocalPdSize>>2)); |
|
241 |
TPde pde = PageDirectory(aOsAsid)[aAddress>>KChunkShift]; |
|
242 |
SPageInfo* pi = SPageInfo::FromPhysAddr(pde); |
|
243 |
TInt id = (pi->Offset()<<KPtClusterShift) | ((pde>>KPageTableShift)&KPtClusterMask); |
|
244 |
TPte* pt = PageTable(id); |
|
245 |
pt += (aAddress>>KPageShift)&(KChunkMask>>KPageShift); |
|
246 |
return pt; |
|
247 |
} |
|
248 |
||
249 |
||
250 |
TInt ArmMmu::LinearToPhysical(TLinAddr aLinAddr, TInt aSize, TPhysAddr& aPhysicalAddress, TPhysAddr* aPhysicalPageList, TInt aOsAsid) |
|
251 |
{ |
|
252 |
__KTRACE_OPT(KMMU,Kern::Printf("ArmMmu::LinearToPhysical %08x+%08x, asid=%d",aLinAddr,aSize,aOsAsid)); |
|
253 |
TPhysAddr physStart = ArmMmu::LinearToPhysical(aLinAddr,aOsAsid); |
|
254 |
TPhysAddr nextPhys = physStart&~KPageMask; |
|
255 |
||
256 |
TUint32* pageList = aPhysicalPageList; |
|
257 |
||
258 |
TInt pageIndex = aLinAddr>>KPageShift; |
|
259 |
TInt pagesLeft = ((aLinAddr+aSize-1)>>KPageShift)+1 - pageIndex; |
|
260 |
TInt pdeIndex = aLinAddr>>KChunkShift; |
|
261 |
TPde* pdePtr = (pdeIndex<(iLocalPdSize>>2) || (iAsidInfo[aOsAsid]&1)) |
|
262 |
? PageDirectory(aOsAsid) |
|
263 |
: ::InitPageDirectory; |
|
264 |
pdePtr += pdeIndex; |
|
265 |
while(pagesLeft) |
|
266 |
{ |
|
267 |
pageIndex &= KChunkMask>>KPageShift; |
|
268 |
TInt pagesLeftInChunk = (1<<(KChunkShift-KPageShift))-pageIndex; |
|
269 |
if(pagesLeftInChunk>pagesLeft) |
|
270 |
pagesLeftInChunk = pagesLeft; |
|
271 |
pagesLeft -= pagesLeftInChunk; |
|
272 |
||
273 |
TPhysAddr phys; |
|
274 |
TPde pde = *pdePtr++; |
|
275 |
TUint pdeType = pde&KPdeTypeMask; |
|
276 |
if(pdeType==KArmV6PdeSection) |
|
277 |
{ |
|
278 |
phys = (pde & KPdeSectionAddrMask) + (pageIndex*KPageSize); |
|
279 |
__KTRACE_OPT(KMMU2,Kern::Printf("ArmMmu::LinearToPhysical Section phys=%8x",phys)); |
|
280 |
TInt n=pagesLeftInChunk; |
|
281 |
phys==nextPhys ? nextPhys+=n*KPageSize : nextPhys=KPhysAddrInvalid; |
|
282 |
if(pageList) |
|
283 |
{ |
|
284 |
TUint32* pageEnd = pageList+n; |
|
285 |
do |
|
286 |
{ |
|
287 |
*pageList++ = phys; |
|
288 |
phys+=KPageSize; |
|
289 |
} |
|
290 |
while(pageList<pageEnd); |
|
291 |
} |
|
292 |
} |
|
293 |
else |
|
294 |
{ |
|
295 |
TPte* pt = SafePageTableFromPde(pde); |
|
296 |
if(!pt) |
|
297 |
{ |
|
298 |
__KTRACE_OPT(KMMU,Kern::Printf("ArmMmu::LinearToPhysical missing page table: PDE=%8x",pde)); |
|
299 |
return KErrNotFound; |
|
300 |
} |
|
301 |
pt += pageIndex; |
|
302 |
for(;;) |
|
303 |
{ |
|
304 |
TPte pte = *pt++; |
|
305 |
TUint pte_type = pte & KPteTypeMask; |
|
306 |
if (pte_type >= KArmV6PteSmallPage) |
|
307 |
{ |
|
308 |
phys = (pte & KPteSmallPageAddrMask); |
|
309 |
__KTRACE_OPT(KMMU2,Kern::Printf("ArmMmu::LinearToPhysical Small Page phys=%8x",phys)); |
|
310 |
phys==nextPhys ? nextPhys+=KPageSize : nextPhys=KPhysAddrInvalid; |
|
311 |
if(pageList) |
|
312 |
*pageList++ = phys; |
|
313 |
if(--pagesLeftInChunk) |
|
314 |
continue; |
|
315 |
break; |
|
316 |
} |
|
317 |
if (pte_type == KArmV6PteLargePage) |
|
318 |
{ |
|
319 |
--pt; // back up ptr |
|
320 |
TUint pageOffset = ((TUint)pt>>2)&(KLargeSmallPageRatio-1); |
|
321 |
phys = (pte & KPteLargePageAddrMask) + pageOffset*KPageSize; |
|
322 |
__KTRACE_OPT(KMMU2,Kern::Printf("ArmMmu::LinearToPhysical Large Page phys=%8x",phys)); |
|
323 |
TInt n=KLargeSmallPageRatio-pageOffset; |
|
324 |
if(n>pagesLeftInChunk) |
|
325 |
n = pagesLeftInChunk; |
|
326 |
phys==nextPhys ? nextPhys+=n*KPageSize : nextPhys=KPhysAddrInvalid; |
|
327 |
if(pageList) |
|
328 |
{ |
|
329 |
TUint32* pageEnd = pageList+n; |
|
330 |
do |
|
331 |
{ |
|
332 |
*pageList++ = phys; |
|
333 |
phys+=KPageSize; |
|
334 |
} |
|
335 |
while(pageList<pageEnd); |
|
336 |
} |
|
337 |
pt += n; |
|
338 |
if(pagesLeftInChunk-=n) |
|
339 |
continue; |
|
340 |
break; |
|
341 |
} |
|
342 |
__KTRACE_OPT(KMMU,Kern::Printf("ArmMmu::LinearToPhysical bad PTE %8x",pte)); |
|
343 |
return KErrNotFound; |
|
344 |
} |
|
345 |
} |
|
346 |
if(!pageList && nextPhys==KPhysAddrInvalid) |
|
347 |
{ |
|
348 |
__KTRACE_OPT(KMMU,Kern::Printf("ArmMmu::LinearToPhysical not contiguous")); |
|
349 |
return KErrNotFound; |
|
350 |
} |
|
351 |
pageIndex = 0; |
|
352 |
} |
|
353 |
||
354 |
if(nextPhys==KPhysAddrInvalid) |
|
355 |
{ |
|
356 |
// Memory is discontiguous... |
|
357 |
aPhysicalAddress = KPhysAddrInvalid; |
|
358 |
return 1; |
|
359 |
} |
|
360 |
else |
|
361 |
{ |
|
362 |
// Memory is contiguous... |
|
363 |
aPhysicalAddress = physStart; |
|
364 |
return KErrNone; |
|
365 |
} |
|
366 |
} |
|
367 |
||
22
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
368 |
|
0 | 369 |
TInt ArmMmu::PreparePagesForDMA(TLinAddr aLinAddr, TInt aSize, TInt aOsAsid, TPhysAddr* aPhysicalPageList) |
370 |
//Returns the list of physical pages belonging to the specified memory space. |
|
371 |
//Checks these pages belong to a chunk marked as being trusted. |
|
22
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
372 |
//Locks these pages so they can not be moved by e.g. ram defragmentation. |
0 | 373 |
{ |
374 |
SPageInfo* pi = NULL; |
|
375 |
DChunk* chunk = NULL; |
|
376 |
TInt err = KErrNone; |
|
22
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
377 |
|
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
378 |
__NK_ASSERT_DEBUG(MM::MaxPagesInOneGo == 32); // Needs to be a power of 2. |
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
379 |
TUint flashMask = MM::MaxPagesInOneGo - 1; |
0 | 380 |
|
381 |
__KTRACE_OPT(KMMU2,Kern::Printf("ArmMmu::PreparePagesForDMA %08x+%08x, asid=%d",aLinAddr,aSize,aOsAsid)); |
|
382 |
||
383 |
TUint32* pageList = aPhysicalPageList; |
|
384 |
TInt pagesInList = 0; //The number of pages we put in the list so far |
|
385 |
||
386 |
TInt pageIndex = (aLinAddr & KChunkMask) >> KPageShift; // Index of the page within the section |
|
387 |
TInt pagesLeft = ((aLinAddr & KPageMask) + aSize + KPageMask) >> KPageShift; |
|
388 |
||
389 |
TInt pdeIndex = aLinAddr>>KChunkShift; |
|
390 |
||
391 |
||
392 |
MmuBase::Wait(); // RamAlloc Mutex for accessing page/directory tables. |
|
393 |
NKern::LockSystem();// SystemlLock for accessing SPageInfo objects. |
|
394 |
||
22
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
395 |
// Get the page directory entry that maps aLinAddr. |
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
396 |
// If the address is in the global region check whether this asid maps |
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
397 |
// global pdes (i.e. the LSB of iAsidInfo is set), if not find the pde from |
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
398 |
// the kernel's initial page directory. |
0 | 399 |
TPde* pdePtr = (pdeIndex<(iLocalPdSize>>2) || (iAsidInfo[aOsAsid]&1)) ? PageDirectory(aOsAsid) : ::InitPageDirectory; |
400 |
pdePtr += pdeIndex;//This points to the first pde |
|
401 |
||
402 |
while(pagesLeft) |
|
403 |
{ |
|
404 |
TInt pagesLeftInChunk = (1<<(KChunkShift-KPageShift))-pageIndex; |
|
405 |
if(pagesLeftInChunk>pagesLeft) |
|
406 |
pagesLeftInChunk = pagesLeft; |
|
407 |
||
408 |
pagesLeft -= pagesLeftInChunk; |
|
409 |
||
22
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
410 |
TPte* pPte = SafePageTableFromPde(*pdePtr++); |
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
411 |
if(!pPte) |
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
412 |
{// Cannot get page table. |
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
413 |
err = KErrNotFound; |
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
414 |
goto fail; |
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
415 |
} |
0 | 416 |
|
22
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
417 |
pPte += pageIndex; |
0 | 418 |
|
419 |
for(;pagesLeftInChunk--;) |
|
22
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
420 |
{// This pte must be of type ArmV6 small page, the pde type will |
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
421 |
// have already been checked by SafePageTableFromPde(). |
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
422 |
__NK_ASSERT_DEBUG((*pPte & KArmV6PteTypeMask) >= KArmV6PteSmallPage); |
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
423 |
TPhysAddr phys = (*pPte++ & KPteSmallPageAddrMask); |
0 | 424 |
pi = SPageInfo::SafeFromPhysAddr(phys); |
22
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
425 |
if(!pi) |
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
426 |
{// Invalid address |
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
427 |
err = KErrNotFound; |
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
428 |
goto fail; |
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
429 |
} |
0 | 430 |
|
431 |
__KTRACE_OPT(KMMU2,Kern::Printf("PageInfo: PA:%x T:%x S:%x O:%x C:%x",phys, pi->Type(), pi->State(), pi->Owner(), pi->LockCount())); |
|
22
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
432 |
if (chunk == NULL) |
0 | 433 |
{//This is the first page. Check 'trusted' bit. |
434 |
if (pi->Type()!= SPageInfo::EChunk) |
|
22
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
435 |
{// The first page does not belong to a chunk. |
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
436 |
err = KErrAccessDenied; |
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
437 |
goto fail; |
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
438 |
} |
0 | 439 |
|
440 |
chunk = (DChunk*)pi->Owner(); |
|
22
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
441 |
if ((chunk == NULL) || ((chunk->iAttributes & DChunk::ETrustedChunk) == 0)) |
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
442 |
{// Not a trusted chunk |
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
443 |
err = KErrAccessDenied; |
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
444 |
goto fail; |
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
445 |
} |
0 | 446 |
} |
447 |
pi->Lock(); |
|
448 |
||
449 |
*pageList++ = phys; |
|
22
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
450 |
|
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
451 |
if(!(++pagesInList & flashMask)) |
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
452 |
{ |
0 | 453 |
NKern::FlashSystem(); |
22
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
454 |
} |
0 | 455 |
} |
456 |
pageIndex = 0; |
|
457 |
} |
|
458 |
||
22
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
459 |
if (pi->Type() != SPageInfo::EChunk) |
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
460 |
{// The last page does not belong to a chunk. |
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
461 |
err = KErrAccessDenied; |
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
462 |
goto fail; |
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
463 |
} |
0 | 464 |
|
465 |
if (chunk && (chunk != (DChunk*)pi->Owner())) |
|
22
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
466 |
{//The first & the last page do not belong to the same chunk. |
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
467 |
err = KErrArgument; |
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
468 |
goto fail; |
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
469 |
} |
0 | 470 |
|
471 |
NKern::UnlockSystem(); |
|
472 |
MmuBase::Signal(); |
|
473 |
return KErrNone; |
|
474 |
||
475 |
fail: |
|
476 |
__KTRACE_OPT(KMMU2,Kern::Printf("ArmMmu::PreparePagesForDMA failed")); |
|
477 |
NKern::UnlockSystem(); |
|
478 |
MmuBase::Signal(); |
|
479 |
ReleasePagesFromDMA(aPhysicalPageList, pagesInList); |
|
480 |
return err; |
|
481 |
} |
|
482 |
||
22
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
483 |
|
0 | 484 |
TInt ArmMmu::ReleasePagesFromDMA(TPhysAddr* aPhysicalPageList, TInt aPageCount) |
485 |
// Unlocks physical pages. |
|
486 |
// @param aPhysicalPageList - points to the list of physical pages that should be released. |
|
487 |
// @param aPageCount - the number of physical pages in the list. |
|
488 |
{ |
|
489 |
NKern::LockSystem(); |
|
490 |
__KTRACE_OPT(KMMU2,Kern::Printf("ArmMmu::ReleasePagesFromDMA count:%d",aPageCount)); |
|
491 |
||
22
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
492 |
TUint flashMask = MM::MaxPagesInOneGo - 1; |
0 | 493 |
while (aPageCount--) |
494 |
{ |
|
495 |
SPageInfo* pi = SPageInfo::SafeFromPhysAddr(*aPhysicalPageList++); |
|
496 |
if(!pi) |
|
497 |
{ |
|
498 |
NKern::UnlockSystem(); |
|
499 |
return KErrArgument; |
|
500 |
} |
|
501 |
__KTRACE_OPT(KMMU2,Kern::Printf("PageInfo: T:%x S:%x O:%x C:%x",pi->Type(), pi->State(), pi->Owner(), pi->LockCount())); |
|
502 |
pi->Unlock(); |
|
22
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
503 |
|
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
504 |
if(!(aPageCount & flashMask)) |
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
505 |
{ |
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
506 |
NKern::FlashSystem(); |
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
507 |
} |
0 | 508 |
} |
509 |
NKern::UnlockSystem(); |
|
510 |
return KErrNone; |
|
511 |
} |
|
512 |
||
22
2f92ad2dc5db
Revision: 201013
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
513 |
|
0 | 514 |
TPhysAddr ArmMmu::LinearToPhysical(TLinAddr aLinAddr, TInt aOsAsid) |
515 |
// |
|
516 |
// Find the physical address corresponding to a given linear address in a specified OS |
|
517 |
// address space. Call with system locked. |
|
518 |
// |
|
519 |
{ |
|
520 |
__KTRACE_OPT(KMMU,Kern::Printf("ArmMmu::LinearToPhysical(%08x,%d)",aLinAddr,aOsAsid)); |
|
521 |
TInt pdeIndex=aLinAddr>>KChunkShift; |
|
522 |
TPde pde = (pdeIndex<(iLocalPdSize>>2) || (iAsidInfo[aOsAsid]&1)) ? PageDirectory(aOsAsid)[pdeIndex] : ::InitPageDirectory[pdeIndex]; |
|
523 |
TPhysAddr pa=KPhysAddrInvalid; |
|
524 |
if ((pde&KPdePresentMask)==KArmV6PdePageTable) |
|
525 |
{ |
|
526 |
SPageInfo* pi = SPageInfo::SafeFromPhysAddr(pde); |
|
527 |
if (pi) |
|
528 |
{ |
|
529 |
TInt id = (pi->Offset()<<KPtClusterShift) | ((pde>>KPageTableShift)&KPtClusterMask); |
|
530 |
TPte* pPte=PageTable(id); |
|
531 |
TPte pte=pPte[(aLinAddr&KChunkMask)>>KPageShift]; |
|
532 |
if (pte & KArmV6PteSmallPage) |
|
533 |
{ |
|
534 |
pa=(pte&KPteSmallPageAddrMask)+(aLinAddr&~KPteSmallPageAddrMask); |
|
535 |
__KTRACE_OPT(KMMU,Kern::Printf("Mapped with small page - returning %08x",pa)); |
|
536 |
} |
|
537 |
else if ((pte & KArmV6PteTypeMask) == KArmV6PteLargePage) |
|
538 |
{ |
|
539 |
pa=(pte&KPteLargePageAddrMask)+(aLinAddr&~KPteLargePageAddrMask); |
|
540 |
__KTRACE_OPT(KMMU,Kern::Printf("Mapped with large page - returning %08x",pa)); |
|
541 |
} |
|
542 |
} |
|
543 |
} |
|
544 |
else if ((pde&KPdePresentMask)==KArmV6PdeSection) |
|
545 |
{ |
|
546 |
pa=(pde&KPdeSectionAddrMask)|(aLinAddr&~KPdeSectionAddrMask); |
|
547 |
__KTRACE_OPT(KMMU,Kern::Printf("Mapped with section - returning %08x",pa)); |
|
548 |
} |
|
549 |
return pa; |
|
550 |
} |
|
551 |
||
552 |
// permission table indexed by XN:APX:AP1:AP0 |
|
553 |
static const TInt PermissionLookup[16]= |
|
554 |
{ //XN:APX:AP1:AP0 |
|
555 |
0, //0 0 0 0 no access |
|
556 |
EMapAttrWriteSup|EMapAttrReadSup|EMapAttrExecSup, //0 0 0 1 RW sup execute |
|
557 |
EMapAttrWriteSup|EMapAttrReadUser|EMapAttrExecUser, //0 0 1 0 supRW usrR execute |
|
558 |
EMapAttrWriteUser|EMapAttrReadUser|EMapAttrExecUser,//0 0 1 1 supRW usrRW execute |
|
559 |
0, //0 1 0 0 reserved |
|
560 |
EMapAttrReadSup|EMapAttrExecSup, //0 1 0 1 supR execute |
|
561 |
EMapAttrReadUser|EMapAttrExecUser, //0 1 1 0 supR usrR execute |
|
562 |
0, //0 1 1 1 reserved |
|
563 |
0, //1 0 0 0 no access |
|
564 |
EMapAttrWriteSup|EMapAttrReadSup, //1 0 0 1 RW sup |
|
565 |
EMapAttrWriteSup|EMapAttrReadUser, //1 0 1 0 supRW usrR |
|
566 |
EMapAttrWriteUser|EMapAttrReadUser, //1 0 1 1 supRW usrRW |
|
567 |
0, //1 1 0 0 reserved |
|
568 |
EMapAttrReadSup, //1 1 0 1 supR |
|
569 |
EMapAttrReadUser, //1 1 1 0 supR usrR |
|
570 |
EMapAttrReadUser, //1 1 1 1 supR usrR |
|
571 |
}; |
|
572 |
||
573 |
TInt ArmMmu::PageTableId(TLinAddr aAddr, TInt aOsAsid) |
|
574 |
{ |
|
575 |
TInt id=-1; |
|
576 |
__KTRACE_OPT(KMMU,Kern::Printf("ArmMmu::PageTableId(%08x,%d)",aAddr,aOsAsid)); |
|
577 |
TInt pdeIndex=aAddr>>KChunkShift; |
|
578 |
TPde pde = (pdeIndex<(iLocalPdSize>>2) || (iAsidInfo[aOsAsid]&1)) ? PageDirectory(aOsAsid)[pdeIndex] : ::InitPageDirectory[pdeIndex]; |
|
579 |
if ((pde&KArmV6PdeTypeMask)==KArmV6PdePageTable) |
|
580 |
{ |
|
581 |
SPageInfo* pi = SPageInfo::SafeFromPhysAddr(pde); |
|
582 |
if (pi) |
|
583 |
id = (pi->Offset()<<KPtClusterShift) | ((pde>>KPageTableShift)&KPtClusterMask); |
|
584 |
} |
|
585 |
__KTRACE_OPT(KMMU,Kern::Printf("ID=%d",id)); |
|
586 |
return id; |
|
587 |
} |
|
588 |
||
589 |
// Used only during boot for recovery of RAM drive |
|
590 |
TInt ArmMmu::BootPageTableId(TLinAddr aAddr, TPhysAddr& aPtPhys) |
|
591 |
{ |
|
592 |
TInt id=KErrNotFound; |
|
593 |
__KTRACE_OPT(KMMU,Kern::Printf("ArmMmu:BootPageTableId(%08x,&)",aAddr)); |
|
594 |
TPde* kpd=(TPde*)KPageDirectoryBase; // kernel page directory |
|
595 |
TInt pdeIndex=aAddr>>KChunkShift; |
|
596 |
TPde pde = kpd[pdeIndex]; |
|
597 |
if ((pde & KArmV6PdeTypeMask) == KArmV6PdePageTable) |
|
598 |
{ |
|
599 |
aPtPhys = pde & KPdePageTableAddrMask; |
|
600 |
SPageInfo* pi = SPageInfo::SafeFromPhysAddr(pde); |
|
601 |
if (pi) |
|
602 |
{ |
|
603 |
SPageInfo::TType type = pi->Type(); |
|
604 |
if (type == SPageInfo::EPageTable) |
|
605 |
id = (pi->Offset()<<KPtClusterShift) | ((pde>>KPageTableShift)&KPtClusterMask); |
|
606 |
else if (type == SPageInfo::EUnused) |
|
607 |
id = KErrUnknown; |
|
608 |
} |
|
609 |
} |
|
610 |
__KTRACE_OPT(KMMU,Kern::Printf("ID=%d",id)); |
|
611 |
return id; |
|
612 |
} |
|
613 |
||
614 |
TBool ArmMmu::PteIsPresent(TPte aPte) |
|
615 |
{ |
|
616 |
return aPte & KArmV6PteTypeMask; |
|
617 |
} |
|
618 |
||
619 |
TPhysAddr ArmMmu::PtePhysAddr(TPte aPte, TInt aPteIndex) |
|
620 |
{ |
|
621 |
TUint32 pte_type = aPte & KArmV6PteTypeMask; |
|
622 |
if (pte_type == KArmV6PteLargePage) |
|
623 |
return (aPte & KPteLargePageAddrMask) + (TPhysAddr(aPteIndex << KPageShift) & KLargePageMask); |
|
624 |
else if (pte_type != 0) |
|
625 |
return aPte & KPteSmallPageAddrMask; |
|
626 |
return KPhysAddrInvalid; |
|
627 |
} |
|
628 |
||
629 |
TPhysAddr ArmMmu::PdePhysAddr(TLinAddr aAddr) |
|
630 |
{ |
|
631 |
TPde* kpd = (TPde*)KPageDirectoryBase; // kernel page directory |
|
632 |
TPde pde = kpd[aAddr>>KChunkShift]; |
|
633 |
if ((pde & KPdePresentMask) == KArmV6PdeSection) |
|
634 |
return pde & KPdeSectionAddrMask; |
|
635 |
return KPhysAddrInvalid; |
|
636 |
} |
|
637 |
||
638 |
void ArmMmu::Init1() |
|
639 |
{ |
|
640 |
__KTRACE_OPT2(KBOOT,KMMU,Kern::Printf("ArmMmu::Init1")); |
|
641 |
||
642 |
// MmuBase data |
|
643 |
iPageSize=KPageSize; |
|
644 |
iPageMask=KPageMask; |
|
645 |
iPageShift=KPageShift; |
|
646 |
iChunkSize=KChunkSize; |
|
647 |
iChunkMask=KChunkMask; |
|
648 |
iChunkShift=KChunkShift; |
|
649 |
iPageTableSize=KPageTableSize; |
|
650 |
iPageTableMask=KPageTableMask; |
|
651 |
iPageTableShift=KPageTableShift; |
|
652 |
iPtClusterSize=KPtClusterSize; |
|
653 |
iPtClusterMask=KPtClusterMask; |
|
654 |
iPtClusterShift=KPtClusterShift; |
|
655 |
iPtBlockSize=KPtBlockSize; |
|
656 |
iPtBlockMask=KPtBlockMask; |
|
657 |
iPtBlockShift=KPtBlockShift; |
|
658 |
iPtGroupSize=KChunkSize/KPageTableSize; |
|
659 |
iPtGroupMask=iPtGroupSize-1; |
|
660 |
iPtGroupShift=iChunkShift-iPageTableShift; |
|
661 |
//TInt* iPtBlockCount; // dynamically allocated - Init2 |
|
662 |
//TInt* iPtGroupCount; // dynamically allocated - Init2 |
|
663 |
iPtInfo=(SPageTableInfo*)KPageTableInfoBase; |
|
664 |
iPageTableLinBase=KPageTableBase; |
|
665 |
//iRamPageAllocator; // dynamically allocated - Init2 |
|
666 |
//iAsyncFreeList; // dynamically allocated - Init2 |
|
667 |
//iPageTableAllocator; // dynamically allocated - Init2 |
|
668 |
//iPageTableLinearAllocator;// dynamically allocated - Init2 |
|
669 |
iPtInfoPtePerm=KPtInfoPtePerm; |
|
670 |
iPtPtePerm=KPtPtePerm; |
|
671 |
iPtPdePerm=KPtPdePerm; |
|
672 |
iUserCodeLoadPtePerm=KUserCodeLoadPte; |
|
673 |
iKernelCodePtePerm=KKernelCodeRunPte; |
|
674 |
iTempAddr=KTempAddr; |
|
675 |
iSecondTempAddr=KSecondTempAddr; |
|
676 |
iMapSizes=KPageSize|KLargePageSize|KChunkSize; |
|
677 |
iRomLinearBase = ::RomHeaderAddress; |
|
678 |
iRomLinearEnd = KRomLinearEnd; |
|
679 |
iShadowPtePerm = KShadowPtePerm; |
|
680 |
iShadowPdePerm = KShadowPdePerm; |
|
681 |
||
682 |
// Mmu data |
|
683 |
TInt total_ram=TheSuperPage().iTotalRamSize; |
|
684 |
||
685 |
// Large or small configuration? |
|
686 |
// This is determined by the bootstrap based on RAM size |
|
687 |
TUint32 ttcr=TTCR(); |
|
688 |
__NK_ASSERT_ALWAYS(ttcr==1 || ttcr==2); |
|
689 |
TBool large = (ttcr==1); |
|
690 |
||
691 |
// calculate cache colouring... |
|
692 |
TInt iColourCount = 0; |
|
693 |
TInt dColourCount = 0; |
|
694 |
TUint32 ctr = InternalCache::TypeRegister(); |
|
695 |
__KTRACE_OPT2(KBOOT,KMMU,Kern::Printf("CacheTypeRegister = %08x",ctr)); |
|
696 |
#ifdef __CPU_ARMV6 |
|
697 |
__NK_ASSERT_ALWAYS((ctr>>29)==0); // check ARMv6 format |
|
698 |
if(ctr&0x800) |
|
699 |
iColourCount = 4; |
|
700 |
if(ctr&0x800000) |
|
701 |
dColourCount = 4; |
|
702 |
#else |
|
703 |
__KTRACE_OPT2(KBOOT,KMMU,Kern::Printf("CacheTypeRegister = %08x",ctr)); |
|
704 |
__NK_ASSERT_ALWAYS((ctr>>29)==4); // check ARMv7 format |
|
705 |
TUint l1ip = (ctr>>14)&3; // L1 instruction cache indexing and tagging policy |
|
706 |
__NK_ASSERT_ALWAYS(l1ip>=2); // check I cache is physically tagged |
|
707 |
||
708 |
TUint32 clidr = InternalCache::LevelIDRegister(); |
|
709 |
__KTRACE_OPT2(KBOOT,KMMU,Kern::Printf("CacheLevelIDRegister = %08x",clidr)); |
|
710 |
TUint l1type = clidr&7; |
|
711 |
if(l1type) |
|
712 |
{ |
|
713 |
if(l1type==2 || l1type==3 || l1type==4) |
|
714 |
{ |
|
715 |
// we have an L1 data cache... |
|
716 |
TUint32 csir = InternalCache::SizeIdRegister(0,0); |
|
717 |
TUint sets = ((csir>>13)&0x7fff)+1; |
|
44 | 718 |
TUint ways = ((csir>>3)&0x3ff)+1; |
0 | 719 |
TUint lineSizeShift = (csir&7)+4; |
720 |
// assume L1 data cache is VIPT and alias checks broken and so we need data cache colouring... |
|
721 |
dColourCount = (sets<<lineSizeShift)>>KPageShift; |
|
722 |
if(l1type==4) // unified cache, so set instruction cache colour as well... |
|
723 |
iColourCount = (sets<<lineSizeShift)>>KPageShift; |
|
724 |
__KTRACE_OPT2(KBOOT,KMMU,Kern::Printf("L1DCache = 0x%x,0x%x,%d colourCount=%d",sets,ways,lineSizeShift,(sets<<lineSizeShift)>>KPageShift)); |
|
725 |
} |
|
726 |
||
727 |
if(l1type==1 || l1type==3) |
|
728 |
{ |
|
729 |
// we have a separate L1 instruction cache... |
|
730 |
TUint32 csir = InternalCache::SizeIdRegister(1,0); |
|
731 |
TUint sets = ((csir>>13)&0x7fff)+1; |
|
44 | 732 |
TUint ways = ((csir>>3)&0x3ff)+1; |
0 | 733 |
TUint lineSizeShift = (csir&7)+4; |
734 |
iColourCount = (sets<<lineSizeShift)>>KPageShift; |
|
735 |
__KTRACE_OPT2(KBOOT,KMMU,Kern::Printf("L1ICache = 0x%x,0x%x,%d colourCount=%d",sets,ways,lineSizeShift,(sets<<lineSizeShift)>>KPageShift)); |
|
736 |
} |
|
737 |
} |
|
738 |
if(l1ip==3) |
|
739 |
{ |
|
740 |
// PIPT cache, so no colouring restrictions... |
|
741 |
__KTRACE_OPT2(KBOOT,KMMU,Kern::Printf("L1ICache is PIPT")); |
|
742 |
iColourCount = 0; |
|
743 |
} |
|
744 |
else |
|
745 |
{ |
|
746 |
// VIPT cache... |
|
747 |
__KTRACE_OPT2(KBOOT,KMMU,Kern::Printf("L1ICache is VIPT")); |
|
748 |
} |
|
749 |
#endif |
|
750 |
TUint colourShift = 0; |
|
751 |
for(TUint colourCount=Max(iColourCount,dColourCount); colourCount!=0; colourCount>>=1) |
|
752 |
++colourShift; |
|
753 |
iAliasSize=KPageSize<<colourShift; |
|
754 |
__KTRACE_OPT2(KBOOT,KMMU,Kern::Printf("iAliasSize=0x%x",iAliasSize)); |
|
755 |
iAliasMask=iAliasSize-1; |
|
756 |
iAliasShift=KPageShift+colourShift; |
|
757 |
||
758 |
iDecommitThreshold = CacheMaintenance::SyncAllPerformanceThresholdPages(); |
|
759 |
||
760 |
iNumOsAsids=KArmV6NumAsids; |
|
761 |
iNumGlobalPageDirs=1; |
|
762 |
//iOsAsidAllocator; // dynamically allocated - Init2 |
|
763 |
iGlobalPdSize=KPageDirectorySize; |
|
764 |
iGlobalPdShift=KPageDirectoryShift; |
|
765 |
iAsidGroupSize=KChunkSize/KPageDirectorySize; |
|
766 |
iAsidGroupMask=iAsidGroupSize-1; |
|
767 |
iAsidGroupShift=KChunkShift-KPageDirectoryShift; |
|
768 |
iUserLocalBase=KUserLocalDataBase; |
|
769 |
iAsidInfo=(TUint32*)KAsidInfoBase; |
|
770 |
iPdeBase=KPageDirectoryBase; |
|
771 |
iPdPtePerm=KPdPtePerm; |
|
772 |
iPdPdePerm=KPdPdePerm; |
|
773 |
iRamDriveMask=0x00f00000; |
|
774 |
iGlobalCodePtePerm=KGlobalCodeRunPte; |
|
775 |
#if defined(__CPU_MEMORY_TYPE_REMAPPING) |
|
776 |
iCacheMaintenanceTempMapAttr = CacheMaintenance::TemporaryMapping(); |
|
777 |
#else |
|
778 |
switch(CacheMaintenance::TemporaryMapping()) |
|
779 |
{ |
|
780 |
case EMemAttNormalUncached: |
|
781 |
iCacheMaintenanceTempMapAttr = KArmV6MemAttNCNC; |
|
782 |
break; |
|
783 |
case EMemAttNormalCached: |
|
784 |
iCacheMaintenanceTempMapAttr = KArmV6MemAttWBWAWBWA; |
|
785 |
break; |
|
786 |
default: |
|
787 |
Panic(ETempMappingFailed); |
|
788 |
} |
|
789 |
#endif |
|
790 |
iMaxDllDataSize=Min(total_ram/2, 0x08000000); // phys RAM/2 up to 128Mb |
|
791 |
iMaxDllDataSize=(iMaxDllDataSize+iChunkMask)&~iChunkMask; // round up to chunk size |
|
792 |
iMaxUserCodeSize=Min(total_ram, 0x10000000); // phys RAM up to 256Mb |
|
793 |
iMaxUserCodeSize=(iMaxUserCodeSize+iChunkMask)&~iChunkMask; // round up to chunk size |
|
794 |
if (large) |
|
795 |
{ |
|
796 |
iLocalPdSize=KPageDirectorySize/2; |
|
797 |
iLocalPdShift=KPageDirectoryShift-1; |
|
798 |
iUserSharedBase=KUserSharedDataBase2GB; |
|
799 |
iUserLocalEnd=iUserSharedBase-iMaxDllDataSize; |
|
800 |
iUserSharedEnd=KUserSharedDataEnd2GB-iMaxUserCodeSize; |
|
801 |
iDllDataBase=iUserLocalEnd; |
|
802 |
iUserCodeBase=iUserSharedEnd; |
|
803 |
} |
|
804 |
else |
|
805 |
{ |
|
806 |
iLocalPdSize=KPageDirectorySize/4; |
|
807 |
iLocalPdShift=KPageDirectoryShift-2; |
|
808 |
iUserSharedBase=KUserSharedDataBase1GB; |
|
809 |
iUserLocalEnd=iUserSharedBase; |
|
810 |
iDllDataBase=KUserSharedDataEnd1GB-iMaxDllDataSize; |
|
811 |
iUserCodeBase=iDllDataBase-iMaxUserCodeSize; |
|
812 |
iUserSharedEnd=iUserCodeBase; |
|
813 |
} |
|
814 |
__KTRACE_OPT(KMMU,Kern::Printf("LPD size %08x GPD size %08x Alias size %08x", |
|
815 |
iLocalPdSize, iGlobalPdSize, iAliasSize)); |
|
816 |
__KTRACE_OPT(KMMU,Kern::Printf("ULB %08x ULE %08x USB %08x USE %08x",iUserLocalBase,iUserLocalEnd, |
|
817 |
iUserSharedBase,iUserSharedEnd)); |
|
818 |
__KTRACE_OPT(KMMU,Kern::Printf("DDB %08x UCB %08x",iDllDataBase,iUserCodeBase)); |
|
819 |
||
820 |
// ArmMmu data |
|
821 |
||
822 |
// other |
|
823 |
PP::MaxUserThreadStack=0x14000; // 80K - STDLIB asks for 64K for PosixServer!!!! |
|
824 |
PP::UserThreadStackGuard=0x2000; // 8K |
|
825 |
PP::MaxStackSpacePerProcess=0x200000; // 2Mb |
|
826 |
K::SupervisorThreadStackSize=0x1000; // 4K |
|
827 |
PP::SupervisorThreadStackGuard=0x1000; // 4K |
|
828 |
K::MachineConfig=(TMachineConfig*)KMachineConfigLinAddr; |
|
829 |
PP::RamDriveStartAddress=KRamDriveStartAddress; |
|
830 |
PP::RamDriveRange=KRamDriveMaxSize; |
|
831 |
PP::RamDriveMaxSize=KRamDriveMaxSize; // may be reduced later |
|
832 |
K::MemModelAttributes=EMemModelTypeMultiple|EMemModelAttrNonExProt|EMemModelAttrKernProt|EMemModelAttrWriteProt| |
|
833 |
EMemModelAttrVA|EMemModelAttrProcessProt|EMemModelAttrSameVA|EMemModelAttrSvKernProt| |
|
834 |
EMemModelAttrIPCKernProt|EMemModelAttrRamCodeProt; |
|
835 |
||
836 |
Arm::DefaultDomainAccess=KDefaultDomainAccess; |
|
837 |
||
838 |
Mmu::Init1(); |
|
839 |
} |
|
840 |
||
841 |
void ArmMmu::DoInit2() |
|
842 |
{ |
|
843 |
__KTRACE_OPT2(KBOOT,KMMU,Kern::Printf("ArmMmu::DoInit2")); |
|
844 |
iTempPte=PageTable(PageTableId(iTempAddr,0))+((iTempAddr&KChunkMask)>>KPageShift); |
|
845 |
iSecondTempPte=PageTable(PageTableId(iSecondTempAddr,0))+((iSecondTempAddr&KChunkMask)>>KPageShift); |
|
846 |
__KTRACE_OPT2(KBOOT,KMMU,Kern::Printf("iTempAddr=%08x, iTempPte=%08x, iSecondTempAddr=%08x, iSecondTempPte=%08x", |
|
847 |
iTempAddr, iTempPte, iSecondTempAddr, iSecondTempPte)); |
|
848 |
CreateKernelSection(KKernelSectionEnd, iAliasShift); |
|
849 |
CreateUserGlobalSection(KUserGlobalDataBase, KUserGlobalDataEnd); |
|
850 |
Mmu::DoInit2(); |
|
851 |
} |
|
852 |
||
853 |
#ifndef __MMU_MACHINE_CODED__ |
|
854 |
void ArmMmu::MapRamPages(TInt aId, SPageInfo::TType aType, TAny* aPtr, TUint32 aOffset, const TPhysAddr* aPageList, TInt aNumPages, TPte aPtePerm) |
|
855 |
// |
|
856 |
// Map a list of physical RAM pages into a specified page table with specified PTE permissions. |
|
857 |
// Update the page information array. |
|
858 |
// Call this with the system locked. |
|
859 |
// |
|
860 |
{ |
|
861 |
__KTRACE_OPT(KMMU,Kern::Printf("ArmMmu::MapRamPages() id=%d type=%d ptr=%08x off=%08x n=%d perm=%08x", |
|
862 |
aId, aType, aPtr, aOffset, aNumPages, aPtePerm)); |
|
863 |
||
864 |
SPageTableInfo& ptinfo=iPtInfo[aId]; |
|
865 |
ptinfo.iCount+=aNumPages; |
|
866 |
aOffset>>=KPageShift; |
|
867 |
TInt ptOffset=aOffset & KPagesInPDEMask; // entry number in page table |
|
868 |
TPte* pPte=PageTable(aId)+ptOffset; // address of first PTE |
|
869 |
||
870 |
TLinAddr firstPte = (TLinAddr)pPte; //Will need this to clean page table changes in cache. |
|
871 |
||
872 |
while(aNumPages--) |
|
873 |
{ |
|
874 |
TPhysAddr pa = *aPageList++; |
|
875 |
if(pa==KPhysAddrInvalid) |
|
876 |
{ |
|
877 |
++pPte; |
|
878 |
__NK_ASSERT_DEBUG(aType==SPageInfo::EInvalid); |
|
879 |
continue; |
|
880 |
} |
|
881 |
*pPte++ = pa | aPtePerm; // insert PTE |
|
882 |
__KTRACE_OPT(KMMU,Kern::Printf("Writing PTE %08x to %08x",pPte[-1],pPte-1)); |
|
883 |
if (aType!=SPageInfo::EInvalid) |
|
884 |
{ |
|
885 |
SPageInfo* pi = SPageInfo::SafeFromPhysAddr(pa); |
|
886 |
if(pi) |
|
887 |
{ |
|
888 |
pi->Set(aType,aPtr,aOffset); |
|
889 |
__KTRACE_OPT(KMMU,Kern::Printf("I: %d %08x %08x",aType,aPtr,aOffset)); |
|
890 |
++aOffset; // increment offset for next page |
|
891 |
} |
|
892 |
} |
|
893 |
} |
|
894 |
CacheMaintenance::MultiplePtesUpdated(firstPte, (TUint)pPte-firstPte); |
|
895 |
} |
|
896 |
||
897 |
void ArmMmu::MapPhysicalPages(TInt aId, SPageInfo::TType aType, TAny* aPtr, TUint32 aOffset, TPhysAddr aPhysAddr, TInt aNumPages, TPte aPtePerm) |
|
898 |
// |
|
899 |
// Map consecutive physical pages into a specified page table with specified PTE permissions. |
|
900 |
// Update the page information array if RAM pages are being mapped. |
|
901 |
// Call this with the system locked. |
|
902 |
// |
|
903 |
{ |
|
904 |
__KTRACE_OPT(KMMU,Kern::Printf("ArmMmu::MapPhysicalPages() id=%d type=%d ptr=%08x off=%08x phys=%08x n=%d perm=%08x", |
|
905 |
aId, aType, aPtr, aOffset, aPhysAddr, aNumPages, aPtePerm)); |
|
906 |
SPageTableInfo& ptinfo=iPtInfo[aId]; |
|
907 |
ptinfo.iCount+=aNumPages; |
|
908 |
aOffset>>=KPageShift; |
|
909 |
TInt ptOffset=aOffset & KPagesInPDEMask; // entry number in page table |
|
910 |
TPte* pPte=(TPte*)(PageTableLinAddr(aId))+ptOffset; // address of first PTE |
|
911 |
||
912 |
TLinAddr firstPte = (TLinAddr)pPte; //Will need this to clean page table changes in cache |
|
913 |
||
914 |
SPageInfo* pi; |
|
915 |
if(aType==SPageInfo::EInvalid) |
|
916 |
pi = NULL; |
|
917 |
else |
|
918 |
pi = SPageInfo::SafeFromPhysAddr(aPhysAddr); |
|
919 |
while(aNumPages--) |
|
920 |
{ |
|
921 |
*pPte++ = aPhysAddr|aPtePerm; // insert PTE |
|
922 |
aPhysAddr+=KPageSize; |
|
923 |
__KTRACE_OPT(KMMU,Kern::Printf("Writing PTE %08x to %08x",pPte[-1],pPte-1)); |
|
924 |
if (pi) |
|
925 |
{ |
|
926 |
pi->Set(aType,aPtr,aOffset); |
|
927 |
__KTRACE_OPT(KMMU,Kern::Printf("I: %d %08x %08x",aType,aPtr,aOffset)); |
|
928 |
++aOffset; // increment offset for next page |
|
929 |
++pi; |
|
930 |
} |
|
931 |
} |
|
932 |
||
933 |
CacheMaintenance::MultiplePtesUpdated(firstPte, (TUint)pPte-firstPte); |
|
934 |
} |
|
935 |
||
936 |
void ArmMmu::MapVirtual(TInt aId, TInt aNumPages) |
|
937 |
// |
|
938 |
// Called in place of MapRamPages or MapPhysicalPages to update mmu data structures when committing |
|
939 |
// virtual address space to a chunk. No pages are mapped. |
|
940 |
// Call this with the system locked. |
|
941 |
// |
|
942 |
{ |
|
943 |
SPageTableInfo& ptinfo=iPtInfo[aId]; |
|
944 |
ptinfo.iCount+=aNumPages; |
|
945 |
} |
|
946 |
||
947 |
void ArmMmu::RemapPage(TInt aId, TUint32 aAddr, TPhysAddr aOldAddr, TPhysAddr aNewAddr, TPte aPtePerm, DProcess* aProcess) |
|
948 |
// |
|
949 |
// Replace the mapping at address aAddr in page table aId. |
|
950 |
// Update the page information array for both the old and new pages. |
|
951 |
// Return physical address of old page if it is now ready to be freed. |
|
952 |
// Call this with the system locked. |
|
953 |
// May be called with interrupts disabled, do not enable/disable them. |
|
954 |
// |
|
955 |
{ |
|
956 |
TInt ptOffset=(aAddr&KChunkMask)>>KPageShift; // entry number in page table |
|
957 |
TPte* pPte=PageTable(aId)+ptOffset; // address of PTE |
|
958 |
TPte pte=*pPte; |
|
959 |
TInt asid=aProcess ? ((DMemModelProcess*)aProcess)->iOsAsid : |
|
960 |
(aAddr<KRomLinearBase ? (TInt)UNKNOWN_MAPPING : (TInt)KERNEL_MAPPING ); |
|
961 |
||
962 |
if (pte & KArmV6PteSmallPage) |
|
963 |
{ |
|
964 |
__ASSERT_ALWAYS((pte & KPteSmallPageAddrMask) == aOldAddr, Panic(ERemapPageFailed)); |
|
965 |
SPageInfo* oldpi = SPageInfo::FromPhysAddr(aOldAddr); |
|
966 |
__ASSERT_DEBUG(oldpi->LockCount()==0,Panic(ERemapPageFailed)); |
|
967 |
||
968 |
// remap page |
|
969 |
*pPte = aNewAddr | aPtePerm; // overwrite PTE |
|
970 |
CacheMaintenance::SinglePteUpdated((TLinAddr)pPte); |
|
971 |
InvalidateTLBForPage(aAddr,asid); // flush TLB entry |
|
972 |
||
973 |
// update new pageinfo, clear old |
|
974 |
SPageInfo* pi = SPageInfo::FromPhysAddr(aNewAddr); |
|
975 |
pi->Set(oldpi->Type(),oldpi->Owner(),oldpi->Offset()); |
|
976 |
oldpi->SetUnused(); |
|
977 |
} |
|
978 |
else |
|
979 |
{ |
|
980 |
Panic(ERemapPageFailed); |
|
981 |
} |
|
982 |
} |
|
983 |
||
984 |
void ArmMmu::RemapPageByAsid(TBitMapAllocator* aOsAsids, TLinAddr aLinAddr, TPhysAddr aOldAddr, TPhysAddr aNewAddr, TPte aPtePerm) |
|
985 |
// |
|
986 |
// Replace the mapping at address aLinAddr in the relevant page table for all |
|
987 |
// ASIDs specified in aOsAsids, but only if the currently mapped address is |
|
988 |
// aOldAddr. |
|
989 |
// Update the page information array for both the old and new pages. |
|
990 |
// Call this with the system unlocked. |
|
991 |
// |
|
992 |
{ |
|
993 |
__KTRACE_OPT(KMMU,Kern::Printf("ArmMmu::RemapPageByAsid() linaddr=%08x oldaddr=%08x newaddr=%08x perm=%08x", aLinAddr, aOldAddr, aNewAddr, aPtePerm)); |
|
994 |
||
995 |
TInt asid = -1; |
|
996 |
TInt lastAsid = KArmV6NumAsids - 1; |
|
997 |
TUint32* ptr = aOsAsids->iMap; |
|
998 |
NKern::LockSystem(); |
|
999 |
do |
|
1000 |
{ |
|
1001 |
TUint32 bits = *ptr++; |
|
1002 |
do |
|
1003 |
{ |
|
1004 |
++asid; |
|
1005 |
if(bits & 0x80000000u) |
|
1006 |
{ |
|
1007 |
// mapped in this address space, so update PTE... |
|
1008 |
TPte* pPte = PtePtrFromLinAddr(aLinAddr, asid); |
|
1009 |
TPte pte = *pPte; |
|
1010 |
if ((pte&~KPageMask) == aOldAddr) |
|
1011 |
{ |
|
1012 |
*pPte = aNewAddr | aPtePerm; |
|
1013 |
__KTRACE_OPT(KMMU,Kern::Printf("Writing PTE %08x to %08x in asid %d",*pPte,pPte,asid)); |
|
1014 |
CacheMaintenance::SinglePteUpdated((TLinAddr)pPte); |
|
1015 |
InvalidateTLBForPage(aLinAddr,asid); // flush TLB entry |
|
1016 |
} |
|
1017 |
} |
|
1018 |
} |
|
1019 |
while(bits<<=1); |
|
1020 |
NKern::FlashSystem(); |
|
1021 |
asid |= 31; |
|
1022 |
} |
|
1023 |
while(asid<lastAsid); |
|
1024 |
||
1025 |
// copy pageinfo attributes and mark old page unused |
|
1026 |
SPageInfo* oldpi = SPageInfo::FromPhysAddr(aOldAddr); |
|
1027 |
SPageInfo::FromPhysAddr(aNewAddr)->Set(oldpi->Type(),oldpi->Owner(),oldpi->Offset()); |
|
1028 |
oldpi->SetUnused(); |
|
1029 |
||
1030 |
NKern::UnlockSystem(); |
|
1031 |
} |
|
1032 |
||
1033 |
TInt ArmMmu::UnmapPages(TInt aId, TUint32 aAddr, TInt aNumPages, TPhysAddr* aPageList, TBool aSetPagesFree, TInt& aNumPtes, TInt& aNumFree, DProcess* aProcess) |
|
1034 |
// |
|
1035 |
// Unmap a specified area at address aAddr in page table aId. Place physical addresses of unmapped |
|
1036 |
// pages into aPageList, and count of unmapped pages into aNumPtes. |
|
1037 |
// Return number of pages still mapped using this page table. |
|
1038 |
// Call this with the system locked. |
|
1039 |
// On multiple memory model, do not call this method with aSetPagesFree false. Call UnmapUnownedPages instead. |
|
1040 |
{ |
|
1041 |
__KTRACE_OPT(KMMU,Kern::Printf("ArmMmu::UnmapPages() id=%d addr=%08x n=%d pl=%08x set-free=%d",aId,aAddr,aNumPages,aPageList,aSetPagesFree)); |
|
1042 |
TInt ptOffset=(aAddr&KChunkMask)>>KPageShift; // entry number in page table |
|
1043 |
TPte* pPte=PageTable(aId)+ptOffset; // address of first PTE |
|
1044 |
TInt np=0; |
|
1045 |
TInt nf=0; |
|
1046 |
TUint32 ng=0; |
|
1047 |
TInt asid=aProcess ? ((DMemModelProcess*)aProcess)->iOsAsid : |
|
1048 |
(aAddr<KRomLinearBase ? (TInt)UNKNOWN_MAPPING : (TInt)KERNEL_MAPPING ); |
|
1049 |
||
1050 |
||
1051 |
while(aNumPages--) |
|
1052 |
{ |
|
1053 |
TPte pte=*pPte; // get original PTE |
|
1054 |
#if defined(__CPU_ARM1136__) && !defined(__CPU_ARM1136_ERRATUM_353494_FIXED) |
|
1055 |
remove_and_invalidate_page(pPte, aAddr, asid); |
|
1056 |
++pPte; |
|
1057 |
#else |
|
1058 |
*pPte++=0; // clear PTE |
|
1059 |
#endif |
|
1060 |
||
1061 |
// We count all unmapped pages in np, including demand paged 'old' pages - but we don't pass |
|
1062 |
// these to PageUnmapped, as the page doesn't become free until it's unmapped from all |
|
1063 |
// processes |
|
1064 |
if (pte != KPteNotPresentEntry) |
|
1065 |
++np; |
|
1066 |
||
1067 |
if (pte & KArmV6PteSmallPage) |
|
1068 |
{ |
|
1069 |
ng |= pte; |
|
1070 |
#if !defined(__CPU_ARM1136__) || defined(__CPU_ARM1136_ERRATUM_353494_FIXED) |
|
1071 |
// Remove_and_invalidate_page will sort out cache and TLB. |
|
1072 |
// When __CPU_ARM1136_ERRATUM_353494_FIXED, we have to do it here. |
|
1073 |
CacheMaintenance::SinglePteUpdated((TLinAddr)(pPte-1)); |
|
1074 |
if (asid >= 0) //otherwise, KUnmapPagesTLBFlushDeferred will be returned. |
|
1075 |
InvalidateTLBForPage(aAddr,asid); // flush any corresponding TLB entry |
|
1076 |
#endif |
|
1077 |
TPhysAddr pa=pte & KPteSmallPageAddrMask; // physical address of unmapped page |
|
1078 |
if (aSetPagesFree) |
|
1079 |
{ |
|
1080 |
SPageInfo* pi = SPageInfo::FromPhysAddr(pa); |
|
1081 |
if(iRamCache->PageUnmapped(pi)) |
|
1082 |
{ |
|
1083 |
pi->SetUnused(); // mark page as unused |
|
1084 |
if (pi->LockCount()==0) |
|
1085 |
{ |
|
1086 |
*aPageList++=pa; // store in page list |
|
1087 |
++nf; // count free pages |
|
1088 |
} |
|
1089 |
} |
|
1090 |
} |
|
1091 |
else |
|
1092 |
*aPageList++=pa; // store in page list |
|
1093 |
} |
|
1094 |
aAddr+=KPageSize; |
|
1095 |
} |
|
1096 |
||
1097 |
aNumPtes=np; |
|
1098 |
aNumFree=nf; |
|
1099 |
SPageTableInfo& ptinfo=iPtInfo[aId]; |
|
1100 |
TInt r=(ptinfo.iCount-=np); |
|
1101 |
if (asid<0) |
|
1102 |
r|=KUnmapPagesTLBFlushDeferred; |
|
1103 |
||
1104 |
||
1105 |
#if defined(__CPU_ARM1136__) && !defined(__CPU_ARM1136_ERRATUM_353494_FIXED) |
|
1106 |
__FlushBtb(); |
|
1107 |
#endif |
|
1108 |
||
1109 |
__KTRACE_OPT(KMMU,Kern::Printf("Unmapped %d; Freed: %d; Return %08x",np,nf,r)); |
|
1110 |
return r; // return number of pages remaining in this page table |
|
1111 |
} |
|
1112 |
||
1113 |
TInt ArmMmu::UnmapVirtual(TInt aId, TUint32 aAddr, TInt aNumPages, TPhysAddr* aPageList, TBool aSetPagesFree, TInt& aNumPtes, TInt& aNumFree, DProcess* aProcess) |
|
1114 |
// |
|
1115 |
// Unmap a specified area at address aAddr in page table aId. Place physical addresses of unmapped |
|
1116 |
// pages into aPageList, and count of unmapped pages into aNumPtes. |
|
1117 |
// Adjust the page table reference count as if aNumPages pages were unmapped. |
|
1118 |
// Return number of pages still mapped using this page table. |
|
1119 |
// Call this with the system locked. |
|
1120 |
// On multiple memory model, do not call this method with aSetPagesFree false. Call UnmapUnownedVirtual instead. |
|
1121 |
// |
|
1122 |
{ |
|
1123 |
SPageTableInfo& ptinfo=iPtInfo[aId]; |
|
1124 |
TInt newCount = ptinfo.iCount - aNumPages; |
|
1125 |
UnmapPages(aId, aAddr, aNumPages, aPageList, aSetPagesFree, aNumPtes, aNumFree, aProcess); |
|
1126 |
ptinfo.iCount = newCount; |
|
1127 |
aNumPtes = aNumPages; |
|
1128 |
return newCount; |
|
1129 |
} |
|
1130 |
||
1131 |
TInt ArmMmu::UnmapUnownedPages(TInt aId, TUint32 aAddr, TInt aNumPages, |
|
1132 |
TPhysAddr* aPageList, TLinAddr* aLAPageList,TInt& aNumPtes, TInt& aNumFree, DProcess* aProcess) |
|
1133 |
/* |
|
1134 |
* Unmaps specified area at address aAddr in page table aId. |
|
1135 |
* Places physical addresses of not-demaned-paged unmapped pages into aPageList. |
|
1136 |
* Corresponding linear addresses are placed into aLAPageList. |
|
1137 |
* 'Old' demand-paged pages (holds invalid PE entry with physucal address) are neither unmapped nor |
|
1138 |
* encountered in aPageList but are still counted in aNumPtes. |
|
1139 |
* |
|
1140 |
* This method should be called to decommit physical memory not owned by the chunk. As we do not know |
|
1141 |
* the origin of such memory, PtInfo could be invalid (or does't exist) so cache maintenance may not be |
|
1142 |
* able to obtain mapping colour. For that reason, this also returns former linear address of each page |
|
1143 |
* in aPageList. |
|
1144 |
* |
|
1145 |
* @pre All pages are mapped within a single page table identified by aId. |
|
1146 |
* @pre On entry, system locked is held and is not released during the execution. |
|
1147 |
* |
|
1148 |
* @arg aId Id of the page table that maps tha pages. |
|
1149 |
* @arg aAddr Linear address of the start of the area. |
|
1150 |
* @arg aNumPages The number of pages to unmap. |
|
1151 |
* @arg aProcess The owning process of the mamory area to unmap. |
|
1152 |
* @arg aPageList On exit, holds the list of unmapped pages. |
|
1153 |
* @arg aLAPageList On exit, holds the list of linear addresses of unmapped pages. |
|
1154 |
* @arg aNumFree On exit, holds the number of pages in aPageList. |
|
1155 |
* @arg aNumPtes On exit, holds the number of unmapped pages. This includes demand-paged 'old' |
|
1156 |
* pages (with invalid page table entry still holding the address of physical page.) |
|
1157 |
* |
|
1158 |
* @return The number of pages still mapped using this page table. It is orred by |
|
1159 |
* KUnmapPagesTLBFlushDeferred if TLB flush is not executed - which requires |
|
1160 |
* the caller to do global TLB flush. |
|
1161 |
*/ |
|
1162 |
{ |
|
1163 |
__KTRACE_OPT(KMMU,Kern::Printf("ArmMmu::UnmapUnownedPages() id=%d addr=%08x n=%d pl=%08x",aId,aAddr,aNumPages,aPageList)); |
|
1164 |
TInt ptOffset=(aAddr&KChunkMask)>>KPageShift; // entry number in page table |
|
1165 |
TPte* pPte=PageTable(aId)+ptOffset; // address of first PTE |
|
1166 |
TInt np=0; |
|
1167 |
TInt nf=0; |
|
1168 |
TUint32 ng=0; |
|
1169 |
TInt asid=aProcess ? ((DMemModelProcess*)aProcess)->iOsAsid : |
|
1170 |
(aAddr<KRomLinearBase ? (TInt)UNKNOWN_MAPPING : (TInt)KERNEL_MAPPING ); |
|
1171 |
||
1172 |
while(aNumPages--) |
|
1173 |
{ |
|
1174 |
TPte pte=*pPte; // get original PTE |
|
1175 |
#if defined(__CPU_ARM1136__) && !defined(__CPU_ARM1136_ERRATUM_353494_FIXED) |
|
1176 |
remove_and_invalidate_page(pPte, aAddr, asid); |
|
1177 |
++pPte; |
|
1178 |
#else |
|
1179 |
*pPte++=0; // clear PTE |
|
1180 |
#endif |
|
1181 |
||
1182 |
// We count all unmapped pages in np, including demand paged 'old' pages - but we don't pass |
|
1183 |
// these to PageUnmapped, as the page doesn't become free until it's unmapped from all |
|
1184 |
// processes |
|
1185 |
if (pte != KPteNotPresentEntry) |
|
1186 |
++np; |
|
1187 |
||
1188 |
if (pte & KArmV6PteSmallPage) |
|
1189 |
{ |
|
1190 |
ng |= pte; |
|
1191 |
#if !defined(__CPU_ARM1136__) || defined(__CPU_ARM1136_ERRATUM_353494_FIXED) |
|
1192 |
// Remove_and_invalidate_page will sort out cache and TLB. |
|
1193 |
// When __CPU_ARM1136_ERRATUM_353494_FIXED, we have to do it here. |
|
1194 |
CacheMaintenance::SinglePteUpdated((TLinAddr)(pPte-1)); |
|
1195 |
if (asid >= 0) //otherwise, KUnmapPagesTLBFlushDeferred will be returned. |
|
1196 |
InvalidateTLBForPage(aAddr,asid); // flush any corresponding TLB entry |
|
1197 |
#endif |
|
1198 |
TPhysAddr pa=pte & KPteSmallPageAddrMask; // physical address of unmapped page |
|
1199 |
++nf; |
|
1200 |
*aPageList++=pa; // store physical aaddress in page list |
|
1201 |
*aLAPageList++=aAddr; // store linear address in page list |
|
1202 |
} |
|
1203 |
aAddr+=KPageSize; |
|
1204 |
} |
|
1205 |
||
1206 |
aNumPtes=np; |
|
1207 |
aNumFree=nf; |
|
1208 |
SPageTableInfo& ptinfo=iPtInfo[aId]; |
|
1209 |
TInt r=(ptinfo.iCount-=np); |
|
1210 |
if (asid<0) |
|
1211 |
r|=KUnmapPagesTLBFlushDeferred; |
|
1212 |
||
1213 |
||
1214 |
#if defined(__CPU_ARM1136__) && !defined(__CPU_ARM1136_ERRATUM_353494_FIXED) |
|
1215 |
__FlushBtb(); |
|
1216 |
#endif |
|
1217 |
||
1218 |
__KTRACE_OPT(KMMU,Kern::Printf("Unmapped %d; Freed: %d; Return %08x",np,nf,r)); |
|
1219 |
return r; // return number of pages remaining in this page table |
|
1220 |
} |
|
1221 |
||
1222 |
||
1223 |
TInt ArmMmu::UnmapUnownedVirtual(TInt aId, TUint32 aAddr, TInt aNumPages, |
|
1224 |
TPhysAddr* aPageList, TLinAddr* aLAPageList,TInt& aNumPtes, TInt& aNumFree, DProcess* aProcess) |
|
1225 |
// |
|
1226 |
// Unmap a specified area at address aAddr in page table aId. Place physical addresses of unmapped |
|
1227 |
// pages into aPageList, and count of unmapped pages into aNumPtes. |
|
1228 |
// Adjust the page table reference count as if aNumPages pages were unmapped. |
|
1229 |
// Return number of pages still mapped using this page table. |
|
1230 |
// Call this with the system locked. |
|
1231 |
// |
|
1232 |
{ |
|
1233 |
SPageTableInfo& ptinfo=iPtInfo[aId]; |
|
1234 |
TInt newCount = ptinfo.iCount - aNumPages; |
|
1235 |
UnmapUnownedPages(aId, aAddr, aNumPages, aPageList, aLAPageList, aNumPtes, aNumFree, aProcess); |
|
1236 |
ptinfo.iCount = newCount; |
|
1237 |
aNumPtes = aNumPages; |
|
1238 |
return newCount; |
|
1239 |
} |
|
1240 |
||
1241 |
void ArmMmu::DoAssignPageTable(TInt aId, TLinAddr aAddr, TPde aPdePerm, const TAny* aOsAsids) |
|
1242 |
// |
|
1243 |
// Assign an allocated page table to map a given linear address with specified permissions. |
|
1244 |
// This should be called with the system unlocked and the MMU mutex held. |
|
1245 |
// |
|
1246 |
{ |
|
1247 |
__KTRACE_OPT(KMMU,Kern::Printf("ArmMmu::DoAssignPageTable %d to %08x perm %08x asid %08x",aId,aAddr,aPdePerm,aOsAsids)); |
|
1248 |
TLinAddr ptLin=PageTableLinAddr(aId); |
|
1249 |
TPhysAddr ptPhys=LinearToPhysical(ptLin,0); |
|
1250 |
TInt pdeIndex=TInt(aAddr>>KChunkShift); |
|
1251 |
TBool gpd=(pdeIndex>=(iLocalPdSize>>2)); |
|
1252 |
TInt os_asid=(TInt)aOsAsids; |
|
1253 |
if (TUint32(os_asid)<TUint32(iNumOsAsids)) |
|
1254 |
{ |
|
1255 |
// single OS ASID |
|
1256 |
TPde* pageDir=PageDirectory(os_asid); |
|
1257 |
NKern::LockSystem(); |
|
1258 |
pageDir[pdeIndex]=ptPhys|aPdePerm; // will blow up here if address is in global region aOsAsid doesn't have a global PD |
|
1259 |
CacheMaintenance::SinglePteUpdated((TLinAddr)(pageDir+pdeIndex)); |
|
1260 |
NKern::UnlockSystem(); |
|
1261 |
||
1262 |
__KTRACE_OPT(KMMU,Kern::Printf("Writing PDE %08x to %08x",ptPhys|aPdePerm,pageDir+pdeIndex)); |
|
1263 |
} |
|
1264 |
else if (os_asid==-1 && gpd) |
|
1265 |
{ |
|
1266 |
// all OS ASIDs, address in global region |
|
1267 |
TInt num_os_asids=iNumGlobalPageDirs; |
|
1268 |
const TBitMapAllocator& b=*(const TBitMapAllocator*)iOsAsidAllocator; |
|
1269 |
for (os_asid=0; num_os_asids; ++os_asid) |
|
1270 |
{ |
|
1271 |
if (!b.NotAllocated(os_asid,1) && (iAsidInfo[os_asid]&1)) |
|
1272 |
{ |
|
1273 |
// this OS ASID exists and has a global page directory |
|
1274 |
TPde* pageDir=PageDirectory(os_asid); |
|
1275 |
NKern::LockSystem(); |
|
1276 |
pageDir[pdeIndex]=ptPhys|aPdePerm; |
|
1277 |
CacheMaintenance::SinglePteUpdated((TLinAddr)(pageDir+pdeIndex)); |
|
1278 |
NKern::UnlockSystem(); |
|
1279 |
||
1280 |
__KTRACE_OPT(KMMU,Kern::Printf("Writing PDE %08x to %08x",ptPhys|aPdePerm,pageDir+pdeIndex)); |
|
1281 |
--num_os_asids; |
|
1282 |
} |
|
1283 |
} |
|
1284 |
} |
|
1285 |
else |
|
1286 |
{ |
|
1287 |
// selection of OS ASIDs or all OS ASIDs |
|
1288 |
const TBitMapAllocator* pB=(const TBitMapAllocator*)aOsAsids; |
|
1289 |
if (os_asid==-1) |
|
1290 |
pB=iOsAsidAllocator; // 0's in positions which exist |
|
1291 |
TInt num_os_asids=pB->iSize-pB->iAvail; |
|
1292 |
for (os_asid=0; num_os_asids; ++os_asid) |
|
1293 |
{ |
|
1294 |
if (pB->NotAllocated(os_asid,1)) |
|
1295 |
continue; // os_asid is not needed |
|
1296 |
TPde* pageDir=PageDirectory(os_asid); |
|
1297 |
NKern::LockSystem(); |
|
1298 |
pageDir[pdeIndex]=ptPhys|aPdePerm; |
|
1299 |
CacheMaintenance::SinglePteUpdated((TLinAddr)(pageDir+pdeIndex)); |
|
1300 |
NKern::UnlockSystem(); |
|
1301 |
||
1302 |
__KTRACE_OPT(KMMU,Kern::Printf("Writing PDE %08x to %08x",ptPhys|aPdePerm,pageDir+pdeIndex)); |
|
1303 |
--num_os_asids; |
|
1304 |
} |
|
1305 |
} |
|
1306 |
} |
|
1307 |
||
1308 |
void ArmMmu::RemapPageTableSingle(TPhysAddr aOld, TPhysAddr aNew, TLinAddr aAddr, TInt aOsAsid) |
|
1309 |
// |
|
1310 |
// Replace a single page table mapping the specified linear address. |
|
1311 |
// This should be called with the system locked and the MMU mutex held. |
|
1312 |
// |
|
1313 |
{ |
|
1314 |
__KTRACE_OPT(KMMU,Kern::Printf("ArmMmu::RemapPageTableSingle %08x to %08x at %08x asid %d",aOld,aNew,aAddr,aOsAsid)); |
|
1315 |
TPde* pageDir=PageDirectory(aOsAsid); |
|
1316 |
TInt pdeIndex=TInt(aAddr>>KChunkShift); |
|
1317 |
TPde pde=pageDir[pdeIndex]; |
|
1318 |
__ASSERT_ALWAYS((pde & KPdePageTableAddrMask) == aOld, Panic(ERemapPageTableFailed)); |
|
1319 |
TPde newPde=aNew|(pde&~KPdePageTableAddrMask); |
|
1320 |
pageDir[pdeIndex]=newPde; // will blow up here if address is in global region aOsAsid doesn't have a global PD |
|
1321 |
CacheMaintenance::SinglePteUpdated((TLinAddr)(pageDir+pdeIndex)); |
|
1322 |
||
1323 |
__KTRACE_OPT(KMMU,Kern::Printf("Writing PDE %08x to %08x",newPde,pageDir+pdeIndex)); |
|
1324 |
} |
|
1325 |
||
1326 |
void ArmMmu::RemapPageTableGlobal(TPhysAddr aOld, TPhysAddr aNew, TLinAddr aAddr) |
|
1327 |
// |
|
1328 |
// Replace a global page table mapping the specified linear address. |
|
1329 |
// This should be called with the system locked and the MMU mutex held. |
|
1330 |
// |
|
1331 |
{ |
|
1332 |
__KTRACE_OPT(KMMU,Kern::Printf("ArmMmu::RemapPageTableGlobal %08x to %08x at %08x",aOld,aNew,aAddr)); |
|
1333 |
TInt pdeIndex=TInt(aAddr>>KChunkShift); |
|
1334 |
TInt num_os_asids=iNumGlobalPageDirs; |
|
1335 |
const TBitMapAllocator& b=*(const TBitMapAllocator*)iOsAsidAllocator; |
|
1336 |
for (TInt os_asid=0; num_os_asids; ++os_asid) |
|
1337 |
{ |
|
1338 |
if (!b.NotAllocated(os_asid,1) && (iAsidInfo[os_asid]&1)) |
|
1339 |
{ |
|
1340 |
// this OS ASID exists and has a global page directory |
|
1341 |
TPde* pageDir=PageDirectory(os_asid); |
|
1342 |
TPde pde=pageDir[pdeIndex]; |
|
1343 |
if ((pde & KPdePageTableAddrMask) == aOld) |
|
1344 |
{ |
|
1345 |
TPde newPde=aNew|(pde&~KPdePageTableAddrMask); |
|
1346 |
pageDir[pdeIndex]=newPde; |
|
1347 |
CacheMaintenance::SinglePteUpdated((TLinAddr)(pageDir+pdeIndex)); |
|
1348 |
||
1349 |
__KTRACE_OPT(KMMU,Kern::Printf("Writing PDE %08x to %08x",newPde,pageDir+pdeIndex)); |
|
1350 |
} |
|
1351 |
--num_os_asids; |
|
1352 |
} |
|
1353 |
if ((os_asid&31)==31) |
|
1354 |
NKern::FlashSystem(); |
|
1355 |
} |
|
1356 |
} |
|
1357 |
||
1358 |
void ArmMmu::RemapPageTableMultiple(TPhysAddr aOld, TPhysAddr aNew, TLinAddr aAddr, const TAny* aOsAsids) |
|
1359 |
// |
|
1360 |
// Replace multiple page table mappings of the specified linear address. |
|
1361 |
// This should be called with the system locked and the MMU mutex held. |
|
1362 |
// |
|
1363 |
{ |
|
1364 |
__KTRACE_OPT(KMMU,Kern::Printf("ArmMmu::RemapPageTableMultiple %08x to %08x at %08x asids %08x",aOld,aNew,aAddr,aOsAsids)); |
|
1365 |
TInt pdeIndex=TInt(aAddr>>KChunkShift); |
|
1366 |
const TBitMapAllocator* pB=(const TBitMapAllocator*)aOsAsids; |
|
1367 |
if ((TInt)aOsAsids==-1) |
|
1368 |
pB=iOsAsidAllocator; // 0's in positions which exist |
|
1369 |
||
1370 |
TInt asid = -1; |
|
1371 |
TInt lastAsid = KArmV6NumAsids - 1; |
|
1372 |
const TUint32* ptr = pB->iMap; |
|
1373 |
do |
|
1374 |
{ |
|
1375 |
TUint32 bits = *ptr++; |
|
1376 |
do |
|
1377 |
{ |
|
1378 |
++asid; |
|
1379 |
if ((bits & 0x80000000u) == 0) |
|
1380 |
{ |
|
1381 |
// mapped in this address space - bitmap is inverted |
|
1382 |
TPde* pageDir=PageDirectory(asid); |
|
1383 |
TPde pde=pageDir[pdeIndex]; |
|
1384 |
if ((pde & KPdePageTableAddrMask) == aOld) |
|
1385 |
{ |
|
1386 |
TPde newPde=aNew|(pde&~KPdePageTableAddrMask); |
|
1387 |
pageDir[pdeIndex]=newPde; |
|
1388 |
CacheMaintenance::SinglePteUpdated((TLinAddr)(pageDir+pdeIndex)); |
|
1389 |
||
1390 |
__KTRACE_OPT(KMMU,Kern::Printf("Writing PDE %08x to %08x",newPde,pageDir+pdeIndex)); |
|
1391 |
} |
|
1392 |
} |
|
1393 |
} |
|
1394 |
while(bits<<=1); |
|
1395 |
NKern::FlashSystem(); |
|
1396 |
asid |= 31; |
|
1397 |
} |
|
1398 |
while(asid<lastAsid); |
|
1399 |
} |
|
1400 |
||
1401 |
void ArmMmu::RemapPageTableAliases(TPhysAddr aOld, TPhysAddr aNew) |
|
1402 |
// |
|
1403 |
// Replace aliases of the specified page table. |
|
1404 |
// This should be called with the system locked and the MMU mutex held. |
|
1405 |
// |
|
1406 |
{ |
|
1407 |
__KTRACE_OPT(KMMU,Kern::Printf("ArmMmu::RemapPageTableAliases %08x to %08x",aOld,aNew)); |
|
1408 |
SDblQue checkedList; |
|
1409 |
SDblQueLink* next; |
|
1410 |
||
1411 |
while(!iAliasList.IsEmpty()) |
|
1412 |
{ |
|
1413 |
next = iAliasList.First()->Deque(); |
|
1414 |
checkedList.Add(next); |
|
1415 |
DMemModelThread* thread = (DMemModelThread*)((TInt)next-_FOFF(DMemModelThread,iAliasLink)); |
|
1416 |
TPde pde = thread->iAliasPde; |
|
1417 |
if ((pde & ~KPageMask) == aOld) |
|
1418 |
{ |
|
1419 |
// a page table in this page is being aliased by the thread, so update it... |
|
1420 |
thread->iAliasPde = (pde & KPageMask) | aNew; |
|
1421 |
} |
|
1422 |
NKern::FlashSystem(); |
|
1423 |
} |
|
1424 |
||
1425 |
// copy checkedList back to iAliasList |
|
1426 |
iAliasList.MoveFrom(&checkedList); |
|
1427 |
} |
|
1428 |
||
1429 |
void ArmMmu::DoUnassignPageTable(TLinAddr aAddr, const TAny* aOsAsids) |
|
1430 |
// |
|
1431 |
// Unassign a now-empty page table currently mapping the specified linear address. |
|
1432 |
// We assume that TLB and/or cache flushing has been done when any RAM pages were unmapped. |
|
1433 |
// This should be called with the system unlocked and the MMU mutex held. |
|
1434 |
// |
|
1435 |
{ |
|
1436 |
__KTRACE_OPT(KMMU,Kern::Printf("ArmMmu::DoUnassignPageTable at %08x a=%08x",aAddr,aOsAsids)); |
|
1437 |
TInt pdeIndex=TInt(aAddr>>KChunkShift); |
|
1438 |
TBool gpd=(pdeIndex>=(iLocalPdSize>>2)); |
|
1439 |
TInt os_asid=(TInt)aOsAsids; |
|
1440 |
TUint pde=0; |
|
1441 |
||
1442 |
SDblQue checkedList; |
|
1443 |
SDblQueLink* next; |
|
1444 |
||
1445 |
if (TUint32(os_asid)<TUint32(iNumOsAsids)) |
|
1446 |
{ |
|
1447 |
// single OS ASID |
|
1448 |
TPde* pageDir=PageDirectory(os_asid); |
|
1449 |
NKern::LockSystem(); |
|
1450 |
pde = pageDir[pdeIndex]; |
|
1451 |
pageDir[pdeIndex]=0; |
|
1452 |
CacheMaintenance::SinglePteUpdated((TLinAddr)(pageDir+pdeIndex)); |
|
1453 |
__KTRACE_OPT(KMMU,Kern::Printf("Clearing PDE at %08x",pageDir+pdeIndex)); |
|
1454 |
||
1455 |
// remove any aliases of the page table... |
|
1456 |
TUint ptId = pde>>KPageTableShift; |
|
1457 |
while(!iAliasList.IsEmpty()) |
|
1458 |
{ |
|
1459 |
next = iAliasList.First()->Deque(); |
|
1460 |
checkedList.Add(next); |
|
1461 |
DMemModelThread* thread = (DMemModelThread*)((TInt)next-_FOFF(DMemModelThread,iAliasLink)); |
|
1462 |
if(thread->iAliasOsAsid==os_asid && (thread->iAliasPde>>KPageTableShift)==ptId) |
|
1463 |
{ |
|
1464 |
// the page table is being aliased by the thread, so remove it... |
|
1465 |
thread->iAliasPde = 0; |
|
1466 |
} |
|
1467 |
NKern::FlashSystem(); |
|
1468 |
} |
|
1469 |
} |
|
1470 |
else if (os_asid==-1 && gpd) |
|
1471 |
{ |
|
1472 |
// all OS ASIDs, address in global region |
|
1473 |
TInt num_os_asids=iNumGlobalPageDirs; |
|
1474 |
const TBitMapAllocator& b=*(const TBitMapAllocator*)iOsAsidAllocator; |
|
1475 |
for (os_asid=0; num_os_asids; ++os_asid) |
|
1476 |
{ |
|
1477 |
if (!b.NotAllocated(os_asid,1) && (iAsidInfo[os_asid]&1)) |
|
1478 |
{ |
|
1479 |
// this OS ASID exists and has a global page directory |
|
1480 |
TPde* pageDir=PageDirectory(os_asid); |
|
1481 |
NKern::LockSystem(); |
|
1482 |
pageDir[pdeIndex]=0; |
|
1483 |
CacheMaintenance::SinglePteUpdated((TLinAddr)(pageDir+pdeIndex)); |
|
1484 |
NKern::UnlockSystem(); |
|
1485 |
||
1486 |
__KTRACE_OPT(KMMU,Kern::Printf("Clearing PDE at %08x",pageDir+pdeIndex)); |
|
1487 |
--num_os_asids; |
|
1488 |
} |
|
1489 |
} |
|
1490 |
// we don't need to look for aliases in this case, because these aren't |
|
1491 |
// created for page tables in the global region. |
|
1492 |
NKern::LockSystem(); |
|
1493 |
} |
|
1494 |
else |
|
1495 |
{ |
|
1496 |
// selection of OS ASIDs or all OS ASIDs |
|
1497 |
const TBitMapAllocator* pB=(const TBitMapAllocator*)aOsAsids; |
|
1498 |
if (os_asid==-1) |
|
1499 |
pB=iOsAsidAllocator; // 0's in positions which exist |
|
1500 |
TInt num_os_asids=pB->iSize-pB->iAvail; |
|
1501 |
for (os_asid=0; num_os_asids; ++os_asid) |
|
1502 |
{ |
|
1503 |
if (pB->NotAllocated(os_asid,1)) |
|
1504 |
continue; // os_asid is not needed |
|
1505 |
TPde* pageDir=PageDirectory(os_asid); |
|
1506 |
NKern::LockSystem(); |
|
1507 |
pde = pageDir[pdeIndex]; |
|
1508 |
pageDir[pdeIndex]=0; |
|
1509 |
CacheMaintenance::SinglePteUpdated((TLinAddr)(pageDir+pdeIndex)); |
|
1510 |
NKern::UnlockSystem(); |
|
1511 |
||
1512 |
__KTRACE_OPT(KMMU,Kern::Printf("Clearing PDE at %08x",pageDir+pdeIndex)); |
|
1513 |
--num_os_asids; |
|
1514 |
} |
|
1515 |
||
1516 |
// remove any aliases of the page table... |
|
1517 |
TUint ptId = pde>>KPageTableShift; |
|
1518 |
NKern::LockSystem(); |
|
1519 |
while(!iAliasList.IsEmpty()) |
|
1520 |
{ |
|
1521 |
next = iAliasList.First()->Deque(); |
|
1522 |
checkedList.Add(next); |
|
1523 |
DMemModelThread* thread = (DMemModelThread*)((TInt)next-_FOFF(DMemModelThread,iAliasLink)); |
|
1524 |
if((thread->iAliasPde>>KPageTableShift)==ptId && !pB->NotAllocated(thread->iAliasOsAsid,1)) |
|
1525 |
{ |
|
1526 |
// the page table is being aliased by the thread, so remove it... |
|
1527 |
thread->iAliasPde = 0; |
|
1528 |
} |
|
1529 |
NKern::FlashSystem(); |
|
1530 |
} |
|
1531 |
} |
|
1532 |
||
1533 |
// copy checkedList back to iAliasList |
|
1534 |
iAliasList.MoveFrom(&checkedList); |
|
1535 |
||
1536 |
NKern::UnlockSystem(); |
|
1537 |
} |
|
1538 |
#endif |
|
1539 |
||
1540 |
// Initialise page table at physical address aXptPhys to be used as page table aXptId |
|
1541 |
// to expand the virtual address range used for mapping page tables. Map the page table |
|
1542 |
// at aPhysAddr as page table aId using the expanded range. |
|
1543 |
// Assign aXptPhys to kernel's Page Directory. |
|
1544 |
// Called with system unlocked and MMU mutex held. |
|
1545 |
void ArmMmu::BootstrapPageTable(TInt aXptId, TPhysAddr aXptPhys, TInt aId, TPhysAddr aPhysAddr) |
|
1546 |
{ |
|
1547 |
__KTRACE_OPT(KMMU,Kern::Printf("ArmMmu::BootstrapPageTable xptid=%04x, xptphys=%08x, id=%04x, phys=%08x", |
|
1548 |
aXptId, aXptPhys, aId, aPhysAddr)); |
|
1549 |
||
1550 |
// put in a temporary mapping for aXptPhys |
|
1551 |
// make it noncacheable |
|
1552 |
TPhysAddr pa=aXptPhys&~KPageMask; |
|
1553 |
*iTempPte = pa | SP_PTE(KArmV6PermRWNO, KNormalUncachedAttr, 0, 1); |
|
1554 |
CacheMaintenance::SinglePteUpdated((TLinAddr)iTempPte); |
|
1555 |
||
1556 |
// clear XPT |
|
1557 |
TPte* xpt=(TPte*)(iTempAddr+(aXptPhys&KPageMask)); |
|
1558 |
memclr(xpt, KPageTableSize); |
|
1559 |
||
1560 |
// must in fact have aXptPhys and aPhysAddr in same physical page |
|
1561 |
__ASSERT_ALWAYS( TUint32(aXptPhys^aPhysAddr)<TUint32(KPageSize), MM::Panic(MM::EBootstrapPageTableBadAddr)); |
|
1562 |
||
1563 |
// so only need one mapping |
|
1564 |
xpt[(aXptId>>KPtClusterShift)&KPagesInPDEMask] = pa | KPtPtePerm; |
|
1565 |
CacheMaintenance::MultiplePtesUpdated((TLinAddr)xpt, KPageTableSize); |
|
1566 |
||
1567 |
// remove temporary mapping |
|
1568 |
*iTempPte=0; |
|
1569 |
CacheMaintenance::SinglePteUpdated((TLinAddr)iTempPte); |
|
1570 |
||
1571 |
InvalidateTLBForPage(iTempAddr, KERNEL_MAPPING); |
|
1572 |
||
1573 |
// initialise PtInfo... |
|
1574 |
TLinAddr xptAddr = PageTableLinAddr(aXptId); |
|
1575 |
iPtInfo[aXptId].SetGlobal(xptAddr>>KChunkShift); |
|
1576 |
||
1577 |
// map xpt... |
|
1578 |
TInt pdeIndex=TInt(xptAddr>>KChunkShift); |
|
1579 |
TPde* pageDir=PageDirectory(0); |
|
1580 |
NKern::LockSystem(); |
|
1581 |
pageDir[pdeIndex]=aXptPhys|KPtPdePerm; |
|
1582 |
CacheMaintenance::SinglePteUpdated((TLinAddr)(pageDir+pdeIndex)); |
|
1583 |
||
1584 |
NKern::UnlockSystem(); |
|
1585 |
} |
|
1586 |
||
1587 |
// Edit the self-mapping entry in page table aId, mapped at aTempMap, to |
|
1588 |
// change the physical address from aOld to aNew. Used when moving page |
|
1589 |
// tables which were created by BootstrapPageTable. |
|
1590 |
// Called with system locked and MMU mutex held. |
|
1591 |
void ArmMmu::FixupXPageTable(TInt aId, TLinAddr aTempMap, TPhysAddr aOld, TPhysAddr aNew) |
|
1592 |
{ |
|
1593 |
__KTRACE_OPT(KMMU,Kern::Printf("ArmMmu::FixupXPageTable id=%04x, tempmap=%08x, old=%08x, new=%08x", |
|
1594 |
aId, aTempMap, aOld, aNew)); |
|
1595 |
||
1596 |
// find correct page table inside the page |
|
1597 |
TPte* xpt=(TPte*)(aTempMap + ((aId & KPtClusterMask) << KPageTableShift)); |
|
1598 |
// find the pte in that page table |
|
1599 |
xpt += (aId>>KPtClusterShift)&KPagesInPDEMask; |
|
1600 |
||
1601 |
// switch the mapping |
|
1602 |
__ASSERT_ALWAYS((*xpt&~KPageMask)==aOld, Panic(EFixupXPTFailed)); |
|
1603 |
*xpt = aNew | KPtPtePerm; |
|
1604 |
// mapped with MapTemp, and thus not mapped as a PTE - have to do real cache clean. |
|
1605 |
CacheMaintenance::SinglePteUpdated((TLinAddr)xpt); |
|
1606 |
} |
|
1607 |
||
1608 |
TInt ArmMmu::NewPageDirectory(TInt aOsAsid, TBool aSeparateGlobal, TPhysAddr& aPhysAddr, TInt& aNumPages) |
|
1609 |
{ |
|
1610 |
__KTRACE_OPT(KMMU,Kern::Printf("ArmMmu::NewPageDirectory(%d,%d)",aOsAsid,aSeparateGlobal)); |
|
1611 |
TInt r=0; |
|
1612 |
TInt nlocal=iLocalPdSize>>KPageShift; |
|
1613 |
aNumPages=aSeparateGlobal ? KPageDirectorySize/KPageSize : nlocal; |
|
1614 |
__KTRACE_OPT(KMMU,Kern::Printf("nlocal=%d, aNumPages=%d",nlocal,aNumPages)); |
|
1615 |
if (aNumPages>1) |
|
1616 |
{ |
|
1617 |
TInt align=aSeparateGlobal ? KPageDirectoryShift : KPageDirectoryShift-1; |
|
26
c734af59ce98
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
22
diff
changeset
|
1618 |
r=AllocContiguousRam(aNumPages<<KPageShift, aPhysAddr, align); |
0 | 1619 |
} |
1620 |
else |
|
1621 |
r=AllocRamPages(&aPhysAddr,1, EPageFixed); |
|
1622 |
__KTRACE_OPT(KMMU,Kern::Printf("r=%d, phys=%08x",r,aPhysAddr)); |
|
1623 |
if (r!=KErrNone) |
|
1624 |
return r; |
|
1625 |
#ifdef BTRACE_KERNEL_MEMORY |
|
1626 |
BTrace4(BTrace::EKernelMemory, BTrace::EKernelMemoryMiscAlloc, aNumPages<<KPageShift); |
|
1627 |
Epoc::KernelMiscPages += aNumPages; |
|
1628 |
#endif |
|
1629 |
SPageInfo* pi = SPageInfo::FromPhysAddr(aPhysAddr); |
|
1630 |
NKern::LockSystem(); |
|
1631 |
TInt i; |
|
1632 |
for (i=0; i<aNumPages; ++i) |
|
1633 |
pi[i].SetPageDir(aOsAsid,i); |
|
1634 |
NKern::UnlockSystem(); |
|
1635 |
return KErrNone; |
|
1636 |
} |
|
1637 |
||
1638 |
inline void CopyPdes(TPde* aDest, const TPde* aSrc, TLinAddr aBase, TLinAddr aEnd) |
|
1639 |
{ |
|
1640 |
memcpy(aDest+(aBase>>KChunkShift), aSrc+(aBase>>KChunkShift), ((aEnd-aBase)>>KChunkShift)*sizeof(TPde)); |
|
1641 |
CacheMaintenance::MultiplePtesUpdated((TLinAddr)(aDest+(aBase>>KChunkShift)), ((aEnd-aBase)>>KChunkShift)*sizeof(TPde)); |
|
1642 |
} |
|
1643 |
||
1644 |
inline void ZeroPdes(TPde* aDest, TLinAddr aBase, TLinAddr aEnd) |
|
1645 |
{ |
|
1646 |
memclr(aDest+(aBase>>KChunkShift), ((aEnd-aBase)>>KChunkShift)*sizeof(TPde)); |
|
1647 |
CacheMaintenance::MultiplePtesUpdated((TLinAddr)(aDest+(aBase>>KChunkShift)), ((aEnd-aBase)>>KChunkShift)*sizeof(TPde)); |
|
1648 |
} |
|
1649 |
||
1650 |
void ArmMmu::InitPageDirectory(TInt aOsAsid, TBool aSeparateGlobal) |
|
1651 |
{ |
|
1652 |
__KTRACE_OPT(KMMU,Kern::Printf("ArmMmu::InitPageDirectory(%d,%d)",aOsAsid,aSeparateGlobal)); |
|
1653 |
TPde* newpd=PageDirectory(aOsAsid); // new page directory |
|
1654 |
memclr(newpd, iLocalPdSize); // clear local page directory |
|
1655 |
CacheMaintenance::MultiplePtesUpdated((TLinAddr)newpd, iLocalPdSize); |
|
1656 |
if (aSeparateGlobal) |
|
1657 |
{ |
|
1658 |
const TPde* kpd=(const TPde*)KPageDirectoryBase; // kernel page directory |
|
1659 |
if (iLocalPdSize==KPageSize) |
|
1660 |
ZeroPdes(newpd, KUserSharedDataEnd1GB, KUserSharedDataEnd2GB); |
|
1661 |
ZeroPdes(newpd, KRamDriveStartAddress, KRamDriveEndAddress); // don't copy RAM drive |
|
1662 |
CopyPdes(newpd, kpd, KRomLinearBase, KUserGlobalDataEnd); // copy ROM + user global |
|
1663 |
CopyPdes(newpd, kpd, KRamDriveEndAddress, 0x00000000); // copy kernel mappings |
|
1664 |
} |
|
1665 |
} |
|
1666 |
||
1667 |
void ArmMmu::ClearPageTable(TInt aId, TInt aFirstIndex) |
|
1668 |
{ |
|
1669 |
__KTRACE_OPT(KMMU,Kern::Printf("ArmMmu::ClearPageTable(%d,%d)",aId,aFirstIndex)); |
|
1670 |
TPte* pte=PageTable(aId); |
|
1671 |
memclr(pte+aFirstIndex, KPageTableSize-aFirstIndex*sizeof(TPte)); |
|
1672 |
CacheMaintenance::MultiplePtesUpdated((TLinAddr)(pte+aFirstIndex), KPageTableSize-aFirstIndex*sizeof(TPte)); |
|
1673 |
} |
|
1674 |
||
1675 |
void ArmMmu::ApplyTopLevelPermissions(TLinAddr aAddr, TInt aOsAsid, TInt aNumPdes, TPde aPdePerm) |
|
1676 |
{ |
|
1677 |
__KTRACE_OPT(KMMU,Kern::Printf("ArmMmu::ApplyTopLevelPermissions %04x:%08x->%08x count %d", |
|
1678 |
aOsAsid, aAddr, aPdePerm, aNumPdes)); |
|
1679 |
TInt ix=aAddr>>KChunkShift; |
|
1680 |
TPde* pPde=PageDirectory(aOsAsid)+ix; |
|
1681 |
TLinAddr firstPde = (TLinAddr)pPde; //Will need this to clean page table memory region in cache |
|
1682 |
||
1683 |
TPde* pPdeEnd=pPde+aNumPdes; |
|
1684 |
NKern::LockSystem(); |
|
1685 |
for (; pPde<pPdeEnd; ++pPde) |
|
1686 |
{ |
|
1687 |
TPde pde=*pPde; |
|
1688 |
if (pde) |
|
1689 |
*pPde = (pde&KPdePageTableAddrMask)|aPdePerm; |
|
1690 |
} |
|
1691 |
CacheMaintenance::MultiplePtesUpdated(firstPde, aNumPdes*sizeof(TPde)); |
|
1692 |
FlushTLBs(); |
|
1693 |
NKern::UnlockSystem(); |
|
1694 |
} |
|
1695 |
||
1696 |
void ArmMmu::ApplyPagePermissions(TInt aId, TInt aPageOffset, TInt aNumPages, TPte aPtePerm) |
|
1697 |
{ |
|
1698 |
__KTRACE_OPT(KMMU,Kern::Printf("ArmMmu::ApplyPagePermissions %04x:%03x+%03x perm %08x", |
|
1699 |
aId, aPageOffset, aNumPages, aPtePerm)); |
|
1700 |
TPte* pPte=PageTable(aId)+aPageOffset; |
|
1701 |
TLinAddr firstPte = (TLinAddr)pPte; //Will need this to clean page table memory region in cache |
|
1702 |
||
1703 |
TPde* pPteEnd=pPte+aNumPages; |
|
1704 |
NKern::LockSystem(); |
|
1705 |
for (; pPte<pPteEnd; ++pPte) |
|
1706 |
{ |
|
1707 |
TPte pte=*pPte; |
|
1708 |
if (pte) |
|
1709 |
*pPte = (pte&KPteSmallPageAddrMask)|aPtePerm; |
|
1710 |
} |
|
1711 |
CacheMaintenance::MultiplePtesUpdated(firstPte, aNumPages*sizeof(TPte)); |
|
1712 |
FlushTLBs(); |
|
1713 |
NKern::UnlockSystem(); |
|
1714 |
} |
|
1715 |
||
1716 |
void ArmMmu::ClearRamDrive(TLinAddr aStart) |
|
1717 |
{ |
|
1718 |
// clear the page directory entries corresponding to the RAM drive |
|
1719 |
TPde* kpd=(TPde*)KPageDirectoryBase; // kernel page directory |
|
1720 |
ZeroPdes(kpd, aStart, KRamDriveEndAddress); |
|
1721 |
} |
|
1722 |
||
1723 |
TPde ArmMmu::PdePermissions(TChunkType aChunkType, TBool aRO) |
|
1724 |
{ |
|
1725 |
// if (aChunkType==EUserData && aRO) |
|
1726 |
// return KPdePtePresent|KPdePteUser; |
|
1727 |
return ChunkPdePermissions[aChunkType]; |
|
1728 |
} |
|
1729 |
||
1730 |
TPte ArmMmu::PtePermissions(TChunkType aChunkType) |
|
1731 |
{ |
|
1732 |
return ChunkPtePermissions[aChunkType]; |
|
1733 |
} |
|
1734 |
||
1735 |
// Set up a page table (specified by aId) to map a 1Mb section of ROM containing aRomAddr |
|
1736 |
// using ROM at aOrigPhys. |
|
1737 |
void ArmMmu::InitShadowPageTable(TInt aId, TLinAddr aRomAddr, TPhysAddr aOrigPhys) |
|
1738 |
{ |
|
1739 |
__KTRACE_OPT(KMMU, Kern::Printf("ArmMmu:InitShadowPageTable id=%04x aRomAddr=%08x aOrigPhys=%08x", |
|
1740 |
aId, aRomAddr, aOrigPhys)); |
|
1741 |
TPte* ppte = PageTable(aId); |
|
1742 |
TLinAddr firstPte = (TLinAddr)ppte; //Will need this to clean page table memory region in cache |
|
1743 |
||
1744 |
TPte* ppte_End = ppte + KChunkSize/KPageSize; |
|
1745 |
TPhysAddr phys = aOrigPhys - (aRomAddr & KChunkMask); |
|
1746 |
for (; ppte<ppte_End; ++ppte, phys+=KPageSize) |
|
1747 |
*ppte = phys | KRomPtePerm; |
|
1748 |
CacheMaintenance::MultiplePtesUpdated(firstPte, sizeof(TPte)*KChunkSize/KPageSize); |
|
1749 |
} |
|
1750 |
||
1751 |
// Copy the contents of ROM at aRomAddr to a shadow page at physical address aShadowPhys |
|
1752 |
// It is assumed aShadowPage is not mapped, therefore any mapping colour is OK. |
|
1753 |
void ArmMmu::InitShadowPage(TPhysAddr aShadowPhys, TLinAddr aRomAddr) |
|
1754 |
{ |
|
1755 |
__KTRACE_OPT(KMMU, Kern::Printf("ArmMmu:InitShadowPage aShadowPhys=%08x aRomAddr=%08x", |
|
1756 |
aShadowPhys, aRomAddr)); |
|
1757 |
||
1758 |
// put in a temporary mapping for aShadowPhys |
|
1759 |
// make it noncacheable |
|
1760 |
*iTempPte = aShadowPhys | SP_PTE(KArmV6PermRWNO, KNormalUncachedAttr, 0, 1); |
|
1761 |
CacheMaintenance::SinglePteUpdated((TLinAddr)iTempPte); |
|
1762 |
||
1763 |
// copy contents of ROM |
|
1764 |
wordmove( (TAny*)iTempAddr, (const TAny*)aRomAddr, KPageSize ); |
|
1765 |
//Temp address is uncached. No need to clean cache, just flush write buffer |
|
1766 |
CacheMaintenance::MemoryToPreserveAndReuse((TLinAddr)iTempAddr, KPageSize, EMapAttrBufferedC); |
|
1767 |
||
1768 |
// remove temporary mapping |
|
1769 |
*iTempPte=0; |
|
1770 |
CacheMaintenance::SinglePteUpdated((TLinAddr)iTempPte); |
|
1771 |
InvalidateTLBForPage(iTempAddr, KERNEL_MAPPING); |
|
1772 |
} |
|
1773 |
||
1774 |
// Assign a shadow page table to replace a ROM section mapping |
|
1775 |
// Enter and return with system locked |
|
1776 |
void ArmMmu::AssignShadowPageTable(TInt aId, TLinAddr aRomAddr) |
|
1777 |
{ |
|
1778 |
__KTRACE_OPT(KMMU, Kern::Printf("ArmMmu:AssignShadowPageTable aId=%04x aRomAddr=%08x", |
|
1779 |
aId, aRomAddr)); |
|
1780 |
TLinAddr ptLin=PageTableLinAddr(aId); |
|
1781 |
TPhysAddr ptPhys=LinearToPhysical(ptLin, 0); |
|
1782 |
TPde* ppde = ::InitPageDirectory + (aRomAddr>>KChunkShift); |
|
1783 |
TPde newpde = ptPhys | KShadowPdePerm; |
|
1784 |
__KTRACE_OPT(KMMU,Kern::Printf("Writing PDE %08x to %08x", newpde, ppde)); |
|
1785 |
TInt irq=NKern::DisableAllInterrupts(); |
|
1786 |
*ppde = newpde; // map in the page table |
|
1787 |
CacheMaintenance::SinglePteUpdated((TLinAddr)ppde); |
|
1788 |
||
1789 |
FlushTLBs(); // flush both TLBs (no need to flush cache yet) |
|
1790 |
NKern::RestoreInterrupts(irq); |
|
1791 |
} |
|
1792 |
||
1793 |
void ArmMmu::DoUnmapShadowPage(TInt aId, TLinAddr aRomAddr, TPhysAddr aOrigPhys) |
|
1794 |
{ |
|
1795 |
__KTRACE_OPT(KMMU,Kern::Printf("ArmMmu:DoUnmapShadowPage, id=%04x lin=%08x origphys=%08x", aId, aRomAddr, aOrigPhys)); |
|
1796 |
TPte* ppte = PageTable(aId) + ((aRomAddr & KChunkMask)>>KPageShift); |
|
1797 |
TPte newpte = aOrigPhys | KRomPtePerm; |
|
1798 |
__KTRACE_OPT(KMMU,Kern::Printf("Writing PTE %08x to %08x", newpte, ppte)); |
|
1799 |
TInt irq=NKern::DisableAllInterrupts(); |
|
1800 |
*ppte = newpte; |
|
1801 |
CacheMaintenance::SinglePteUpdated((TLinAddr)ppte); |
|
1802 |
||
1803 |
InvalidateTLBForPage(aRomAddr, KERNEL_MAPPING); |
|
1804 |
#if defined(__CPU_ARM1136__) && !defined(__CPU_ARM1136_ERRATUM_353494_FIXED) |
|
1805 |
__FlushBtb(); |
|
1806 |
#endif |
|
1807 |
||
1808 |
CacheMaintenance::CodeChanged(aRomAddr, KPageSize, CacheMaintenance::EMemoryRemap); |
|
1809 |
CacheMaintenance::PageToReuse(aRomAddr, EMemAttNormalCached, KPhysAddrInvalid); |
|
1810 |
NKern::RestoreInterrupts(irq); |
|
1811 |
} |
|
1812 |
||
1813 |
TInt ArmMmu::UnassignShadowPageTable(TLinAddr aRomAddr, TPhysAddr aOrigPhys) |
|
1814 |
{ |
|
1815 |
__KTRACE_OPT(KMMU,Kern::Printf("ArmMmu:UnassignShadowPageTable, lin=%08x origphys=%08x", aRomAddr, aOrigPhys)); |
|
1816 |
TPde* ppde = ::InitPageDirectory + (aRomAddr>>KChunkShift); |
|
1817 |
TPde newpde = (aOrigPhys &~ KChunkMask) | KRomSectionPermissions; |
|
1818 |
__KTRACE_OPT(KMMU,Kern::Printf("Writing PDE %08x to %08x", newpde, ppde)); |
|
1819 |
TInt irq=NKern::DisableAllInterrupts(); |
|
1820 |
*ppde = newpde; // revert to section mapping |
|
1821 |
CacheMaintenance::SinglePteUpdated((TLinAddr)ppde); |
|
1822 |
||
1823 |
FlushTLBs(); // flush both TLBs |
|
1824 |
NKern::RestoreInterrupts(irq); |
|
1825 |
return KErrNone; |
|
1826 |
} |
|
1827 |
||
1828 |
||
1829 |
#if defined(__CPU_MEMORY_TYPE_REMAPPING) // arm1176, arm11mcore, armv7, ... |
|
1830 |
/** |
|
1831 |
Shadow pages on platforms with remapping (mpcore, 1176, cortex...) are not writable. |
|
1832 |
This will map the region into writable memory first. |
|
1833 |
@pre No Fast Mutex held |
|
1834 |
*/ |
|
1835 |
TInt ArmMmu::CopyToShadowMemory(TLinAddr aDest, TLinAddr aSrc, TUint32 aLength) |
|
1836 |
{ |
|
1837 |
__KTRACE_OPT(KMMU, Kern::Printf("ArmMmu:CopyToShadowMemory aDest=%08x aSrc=%08x aLength=%08x", aDest, aSrc, aLength)); |
|
1838 |
||
1839 |
// Check that destination is ROM |
|
1840 |
if (aDest<iRomLinearBase || (aDest+aLength) > iRomLinearEnd) |
|
1841 |
{ |
|
1842 |
__KTRACE_OPT(KMMU,Kern::Printf("ArmMmu:CopyToShadowMemory: Destination not entirely in ROM")); |
|
1843 |
return KErrArgument; |
|
1844 |
} |
|
1845 |
// do operation with RamAlloc mutex held (to prevent shadow pages from being released from under us) |
|
1846 |
MmuBase::Wait(); |
|
1847 |
||
1848 |
||
1849 |
TInt r = KErrNone; |
|
1850 |
while (aLength) |
|
1851 |
{ |
|
1852 |
// Calculate memory size to copy in this loop. A single page region will be copied per loop |
|
1853 |
TInt copySize = Min(aLength, iPageSize - (aDest&iPageMask)); |
|
1854 |
||
1855 |
// Get physical address |
|
1856 |
TPhysAddr physAddr = LinearToPhysical(aDest&~iPageMask, 0); |
|
1857 |
if (KPhysAddrInvalid==physAddr) |
|
1858 |
{ |
|
1859 |
r = KErrArgument; |
|
1860 |
break; |
|
1861 |
} |
|
1862 |
||
1863 |
//check whether it is shadowed rom |
|
1864 |
SPageInfo* pi = SPageInfo::SafeFromPhysAddr(physAddr); |
|
1865 |
if (pi==0 || pi->Type()!=SPageInfo::EShadow) |
|
1866 |
{ |
|
1867 |
__KTRACE_OPT(KMMU,Kern::Printf("ArmMmu:CopyToShadowMemory: No shadow page at this address")); |
|
1868 |
r = KErrArgument; |
|
1869 |
break; |
|
1870 |
} |
|
1871 |
||
1872 |
//Temporarily map into writable memory and copy data. RamAllocator DMutex is required |
|
1873 |
TLinAddr tempAddr = MapTemp (physAddr, aDest&~iPageMask); |
|
1874 |
__KTRACE_OPT(KMMU, Kern::Printf("ArmMmu:CopyToShadowMemory Copy aDest=%08x aSrc=%08x aSize=%08x", tempAddr+(aDest&iPageMask), aSrc, copySize)); |
|
1875 |
memcpy ((TAny*)(tempAddr+(aDest&iPageMask)), (const TAny*)aSrc, copySize); //Kernel-to-Kernel copy is presumed |
|
1876 |
UnmapTemp(); |
|
1877 |
||
1878 |
//Update variables for the next loop/page |
|
1879 |
aDest+=copySize; |
|
1880 |
aSrc+=copySize; |
|
1881 |
aLength-=copySize; |
|
1882 |
} |
|
1883 |
MmuBase::Signal(); |
|
1884 |
return r; |
|
1885 |
} |
|
1886 |
#endif |
|
1887 |
||
1888 |
void ArmMmu::DoFreezeShadowPage(TInt aId, TLinAddr aRomAddr) |
|
1889 |
{ |
|
1890 |
#if defined(__CPU_MEMORY_TYPE_REMAPPING) //arm1176, arm11mcore, armv7 and later |
|
1891 |
__KTRACE_OPT(KMMU, Kern::Printf("ArmMmu:DoFreezeShadowPage not required with MEMORY_TYPE_REMAPPING")); |
|
1892 |
#else |
|
1893 |
__KTRACE_OPT(KMMU, Kern::Printf("ArmMmu:DoFreezeShadowPage aId=%04x aRomAddr=%08x", |
|
1894 |
aId, aRomAddr)); |
|
1895 |
TPte* ppte = PageTable(aId) + ((aRomAddr & KChunkMask)>>KPageShift); |
|
1896 |
TPte newpte = (*ppte & KPteSmallPageAddrMask) | KRomPtePerm; |
|
1897 |
__KTRACE_OPT(KMMU,Kern::Printf("Writing PTE %08x to %08x", newpte, ppte)); |
|
1898 |
*ppte = newpte; |
|
1899 |
CacheMaintenance::SinglePteUpdated((TLinAddr)ppte); |
|
1900 |
InvalidateTLBForPage(aRomAddr, KERNEL_MAPPING); |
|
1901 |
#endif |
|
1902 |
} |
|
1903 |
||
1904 |
/** Replaces large page(64K) entry in page table with small page(4K) entries.*/ |
|
1905 |
void ArmMmu::Pagify(TInt aId, TLinAddr aLinAddr) |
|
1906 |
{ |
|
1907 |
__KTRACE_OPT(KMMU, Kern::Printf("ArmMmu:Pagify aId=%04x aLinAddr=%08x", aId, aLinAddr)); |
|
1908 |
||
1909 |
TInt pteIndex = (aLinAddr & KChunkMask)>>KPageShift; |
|
1910 |
TPte* pte = PageTable(aId); |
|
1911 |
if ((pte[pteIndex] & KArmV6PteTypeMask) == KArmV6PteLargePage) |
|
1912 |
{ |
|
1913 |
__KTRACE_OPT(KMMU,Kern::Printf("Converting 64K page to 4K pages")); |
|
1914 |
pteIndex &= ~0xf; |
|
1915 |
TPte source = pte[pteIndex]; |
|
1916 |
source = (source & KPteLargePageAddrMask) | SP_PTE_FROM_LP_PTE(source); |
|
1917 |
pte += pteIndex; |
|
1918 |
for (TInt entry=0; entry<16; entry++) |
|
1919 |
{ |
|
1920 |
pte[entry] = source | (entry<<12); |
|
1921 |
} |
|
1922 |
CacheMaintenance::MultiplePtesUpdated((TLinAddr)pte, 16*sizeof(TPte)); |
|
1923 |
FlushTLBs(); |
|
1924 |
} |
|
1925 |
} |
|
1926 |
||
1927 |
void ArmMmu::FlushShadow(TLinAddr aRomAddr) |
|
1928 |
{ |
|
1929 |
CacheMaintenance::CodeChanged(aRomAddr, KPageSize, CacheMaintenance::EMemoryRemap); |
|
1930 |
CacheMaintenance::PageToReuse(aRomAddr, EMemAttNormalCached, KPhysAddrInvalid); |
|
1931 |
InvalidateTLBForPage(aRomAddr, KERNEL_MAPPING); // remove all TLB references to original ROM page |
|
1932 |
} |
|
1933 |
||
1934 |
||
1935 |
#if defined(__CPU_MEMORY_TYPE_REMAPPING) //arm1176, arm11mcore, armv7 |
|
1936 |
/** |
|
1937 |
Calculates page directory/table entries for memory type described in aMapAttr. |
|
1938 |
Global, small page (4KB) mapping is assumed. |
|
1939 |
(All magic numbers come from ARM page table descriptions.) |
|
1940 |
@param aMapAttr On entry, holds description(memory type, access permisions,...) of the memory. |
|
1941 |
It is made up of TMappingAttributes constants or TMappingAttributes2 object. If TMappingAttributes, |
|
1942 |
may be altered on exit to hold the actual cache attributes & access permissions. |
|
1943 |
@param aPde On exit, holds page-table-entry for the 1st level descriptor |
|
1944 |
for given type of memory, with base address set to 0. |
|
1945 |
@param aPte On exit, holds small-page-entry (4K) for the 2nd level descriptor |
|
1946 |
for given type of memory, with base address set to 0. |
|
1947 |
@return KErrNotSupported If memory described in aMapAttr is not supported |
|
1948 |
KErrNone Otherwise |
|
1949 |
*/ |
|
1950 |
TInt ArmMmu::PdePtePermissions(TUint& aMapAttr, TPde& aPde, TPte& aPte) |
|
1951 |
{ |
|
1952 |
__KTRACE_OPT(KMMU,Kern::Printf(">ArmMmu::PdePtePermissions, mapattr=%08x",aMapAttr)); |
|
1953 |
||
1954 |
TMappingAttributes2& memory = (TMappingAttributes2&)aMapAttr; |
|
1955 |
||
1956 |
if(memory.ObjectType2()) |
|
1957 |
{ |
|
1958 |
//---------Memory described by TMappingAttributes2 object----------------- |
|
1959 |
aPde = KArmV6PdePageTable | |
|
1960 |
(memory.Parity() ? KArmV6PdeECCEnable : 0); |
|
1961 |
#if defined(FAULTY_NONSHARED_DEVICE_MEMORY) |
|
1962 |
if(!memory.Shared() && (memory.Type() == EMemAttDevice )) |
|
1963 |
{ |
|
1964 |
aMapAttr ^= EMapAttrBufferedNC; |
|
1965 |
aMapAttr |= EMapAttrFullyBlocking; |
|
1966 |
// Clear EMemAttDevice |
|
1967 |
aMapAttr ^= (EMemAttDevice << 26); |
|
1968 |
aMapAttr |= (EMemAttStronglyOrdered << 26); |
|
1969 |
} |
|
1970 |
#endif |
|
1971 |
aPte = KArmV6PteSmallPage | |
|
1972 |
KArmV6PteAP0 | // AP0 bit always 1 |
|
1973 |
((memory.Type()&3)<<2) | ((memory.Type()&4)<<4) | // memory type |
|
1974 |
(memory.Executable() ? 0 : KArmV6PteSmallXN) | // eXecuteNever bit |
|
1975 |
#if defined (__CPU_USE_SHARED_MEMORY) |
|
1976 |
KArmV6PteS | // Memory is always shared. |
|
1977 |
#else |
|
1978 |
(memory.Shared() ? KArmV6PteS : 0) | // Shared bit |
|
1979 |
#endif |
|
1980 |
(memory.Writable() ? 0 : KArmV6PteAPX) | // APX = !Writable |
|
1981 |
(memory.UserAccess() ? KArmV6PteAP1: 0); // AP1 = UserAccess |
|
1982 |
// aMapAttr remains the same |
|
1983 |
} |
|
1984 |
else |
|
1985 |
{ |
|
1986 |
//---------Memory described by TMappingAttributes bitmask----------------- |
|
1987 |
#if defined(FAULTY_NONSHARED_DEVICE_MEMORY) |
|
1988 |
if(((aMapAttr & EMapAttrL1CacheMask) == EMapAttrBufferedNC) && !(aMapAttr & EMapAttrShared)) |
|
1989 |
{ |
|
1990 |
// Clear EMapAttrBufferedNC attribute |
|
1991 |
aMapAttr ^= EMapAttrBufferedNC; |
|
1992 |
aMapAttr |= EMapAttrFullyBlocking; |
|
1993 |
} |
|
1994 |
#endif |
|
1995 |
// 1. Calculate TEX0:C:B bits in page table and actual cache attributes. |
|
1996 |
// Only L1 cache attribute from aMapAttr matters. Outer (L2) cache policy will be the same as inner one. |
|
1997 |
TUint l1cache=aMapAttr & EMapAttrL1CacheMask; // Inner cache attributes. May change to actual value. |
|
1998 |
TUint l2cache; // Will hold actual L2 cache attributes (in terms of TMappingAttributes constants) |
|
1999 |
TUint tex0_c_b; // Will hold TEX[0]:C:B value in page table |
|
2000 |
||
2001 |
switch (l1cache) |
|
2002 |
{ |
|
2003 |
case EMapAttrFullyBlocking: |
|
2004 |
tex0_c_b = EMemAttStronglyOrdered; |
|
2005 |
l2cache = EMapAttrL2Uncached; |
|
2006 |
break; |
|
2007 |
case EMapAttrBufferedNC: |
|
2008 |
tex0_c_b = EMemAttDevice; |
|
2009 |
l2cache = EMapAttrL2Uncached; |
|
2010 |
break; |
|
2011 |
case EMapAttrBufferedC: |
|
2012 |
case EMapAttrL1Uncached: |
|
2013 |
case EMapAttrCachedWTRA: |
|
2014 |
case EMapAttrCachedWTWA: |
|
2015 |
tex0_c_b = EMemAttNormalUncached; |
|
2016 |
l1cache = EMapAttrBufferedC; |
|
2017 |
l2cache = EMapAttrL2Uncached; |
|
2018 |
break; |
|
2019 |
case EMapAttrCachedWBRA: |
|
2020 |
case EMapAttrCachedWBWA: |
|
2021 |
case EMapAttrL1CachedMax: |
|
2022 |
tex0_c_b = EMemAttNormalCached; |
|
2023 |
l1cache = EMapAttrCachedWBWA; |
|
2024 |
l2cache = EMapAttrL2CachedWBWA; |
|
2025 |
break; |
|
2026 |
default: |
|
2027 |
return KErrNotSupported; |
|
2028 |
} |
|
2029 |
||
2030 |
// 2. Step 2 has been removed :) |
|
2031 |
||
2032 |
// 3. Calculate access permissions (apx:ap bits in page table + eXecute it) |
|
2033 |
TUint read=aMapAttr & EMapAttrReadMask; |
|
2034 |
TUint write=(aMapAttr & EMapAttrWriteMask)>>4; |
|
2035 |
TUint exec=(aMapAttr & EMapAttrExecMask)>>8; |
|
2036 |
||
2037 |
read|=exec; // User/Sup execute access requires User/Sup read access. |
|
2038 |
if (exec) exec = 1; // There is a single eXecute bit in page table. Set to one if User or Sup exec is required. |
|
2039 |
||
2040 |
TUint apxap=0; |
|
2041 |
if (write==0) // no write required |
|
2042 |
{ |
|
2043 |
if (read>=4) apxap=KArmV6PermRORO; // user read required |
|
2044 |
else if (read==1) apxap=KArmV6PermRONO; // supervisor read required |
|
2045 |
else return KErrNotSupported; // no read required |
|
2046 |
} |
|
2047 |
else if (write<4) // supervisor write required |
|
2048 |
{ |
|
2049 |
if (read<4) apxap=KArmV6PermRWNO; // user read not required |
|
2050 |
else return KErrNotSupported; // user read required |
|
2051 |
} |
|
2052 |
else // user & supervisor writes required |
|
2053 |
{ |
|
2054 |
apxap=KArmV6PermRWRW; |
|
2055 |
} |
|
2056 |
||
2057 |
// 4. Calculate page-table-entry for the 1st level (aka page directory) descriptor |
|
2058 |
aPde=((aMapAttr&EMapAttrUseECC)>>8)|KArmV6PdePageTable; |
|
2059 |
||
2060 |
// 5. Calculate small-page-entry for the 2nd level (aka page table) descriptor |
|
2061 |
aPte=SP_PTE(apxap, tex0_c_b, exec, 1); // always global |
|
2062 |
if (aMapAttr&EMapAttrShared) |
|
2063 |
aPte |= KArmV6PteS; |
|
2064 |
||
2065 |
// 6. Fix aMapAttr to hold the actual values for access permission & cache attributes |
|
2066 |
TUint xnapxap=((aPte<<3)&8)|((aPte>>7)&4)|((aPte>>4)&3); |
|
2067 |
aMapAttr &= ~(EMapAttrAccessMask|EMapAttrL1CacheMask|EMapAttrL2CacheMask); |
|
2068 |
aMapAttr |= PermissionLookup[xnapxap]; // Set actual access permissions |
|
2069 |
aMapAttr |= l1cache; // Set actual inner cache attributes |
|
2070 |
aMapAttr |= l2cache; // Set actual outer cache attributes |
|
2071 |
} |
|
2072 |
||
2073 |
__KTRACE_OPT(KMMU,Kern::Printf("<ArmMmu::PdePtePermissions, mapattr=%08x, pde=%08x, pte=%08x", aMapAttr, aPde, aPte)); |
|
2074 |
return KErrNone; |
|
2075 |
} |
|
2076 |
||
2077 |
#else //ARMv6 (arm1136) |
|
2078 |
||
2079 |
const TUint FBLK=(EMapAttrFullyBlocking>>12); |
|
2080 |
const TUint BFNC=(EMapAttrBufferedNC>>12); |
|
2081 |
//const TUint BUFC=(EMapAttrBufferedC>>12); |
|
2082 |
const TUint L1UN=(EMapAttrL1Uncached>>12); |
|
2083 |
const TUint WTRA=(EMapAttrCachedWTRA>>12); |
|
2084 |
//const TUint WTWA=(EMapAttrCachedWTWA>>12); |
|
2085 |
const TUint WBRA=(EMapAttrCachedWBRA>>12); |
|
2086 |
const TUint WBWA=(EMapAttrCachedWBWA>>12); |
|
2087 |
const TUint AWTR=(EMapAttrAltCacheWTRA>>12); |
|
2088 |
//const TUint AWTW=(EMapAttrAltCacheWTWA>>12); |
|
2089 |
//const TUint AWBR=(EMapAttrAltCacheWBRA>>12); |
|
2090 |
const TUint AWBW=(EMapAttrAltCacheWBWA>>12); |
|
2091 |
const TUint MAXC=(EMapAttrL1CachedMax>>12); |
|
2092 |
||
2093 |
const TUint L2UN=(EMapAttrL2Uncached>>16); |
|
2094 |
||
2095 |
const TUint8 UNS=0xffu; // Unsupported attribute |
|
2096 |
||
2097 |
//Maps L1 & L2 cache attributes into TEX[4:2]:CB[1:0] |
|
2098 |
//ARMv6 doesn't do WTWA so we use WTRA instead |
|
2099 |
||
2100 |
#if !defined(__CPU_ARM1136_ERRATUM_399234_FIXED) |
|
2101 |
// L1 Write-Through mode is outlawed, L1WT acts as L1UN. |
|
2102 |
static const TUint8 CBTEX[40]= |
|
2103 |
{ // L1CACHE: |
|
2104 |
// FBLK BFNC BUFC L1UN WTRA WTWA WBRA WBWA L2CACHE: |
|
2105 |
0x00, 0x01, 0x01, 0x04, 0x04, 0x04, 0x13, 0x11, //NC |
|
2106 |
0x00, 0x01, 0x01, 0x18, 0x18, 0x18, 0x1b, 0x19, //WTRA |
|
2107 |
0x00, 0x01, 0x01, 0x18, 0x18, 0x18, 0x1b, 0x19, //WTWA |
|
2108 |
0x00, 0x01, 0x01, 0x1c, 0x1c, 0x1c, 0x1f, 0x1d, //WBRA |
|
2109 |
0x00, 0x01, 0x01, 0x14, 0x14, 0x14, 0x17, 0x15 //WBWA |
|
2110 |
}; |
|
2111 |
#else |
|
2112 |
static const TUint8 CBTEX[40]= |
|
2113 |
{ // L1CACHE: |
|
2114 |
// FBLK BFNC BUFC L1UN WTRA WTWA WBRA WBWA L2CACHE: |
|
2115 |
0x00, 0x01, 0x01, 0x04, 0x12, 0x12, 0x13, 0x11, //NC |
|
2116 |
0x00, 0x01, 0x01, 0x18, 0x02, 0x02, 0x1b, 0x19, //WTRA |
|
2117 |
0x00, 0x01, 0x01, 0x18, 0x02, 0x02, 0x1b, 0x19, //WTWA |
|
2118 |
0x00, 0x01, 0x01, 0x1c, 0x1e, 0x1e, 0x1f, 0x1d, //WBRA |
|
2119 |
0x00, 0x01, 0x01, 0x14, 0x16, 0x16, 0x17, 0x15 //WBWA |
|
2120 |
}; |
|
2121 |
#endif |
|
2122 |
||
2123 |
//Maps TEX[4:2]:CB[1:0] value into L1 cache attributes |
|
2124 |
static const TUint8 L1Actual[32]= |
|
2125 |
{ |
|
2126 |
//CB 00 01 10 11 //TEX |
|
2127 |
FBLK, BFNC, WTRA, WBRA, //000 |
|
2128 |
L1UN, UNS, UNS, WBWA, //001 |
|
2129 |
BFNC, UNS, UNS, UNS, //010 |
|
2130 |
UNS, UNS, UNS, UNS, //011 |
|
2131 |
L1UN, WBWA, WTRA, WBRA, //100 |
|
2132 |
L1UN, WBWA, WTRA, WBRA, //101 |
|
2133 |
L1UN, WBWA, WTRA, WBRA, //110 |
|
2134 |
L1UN, WBWA, WTRA, WBRA //111 |
|
2135 |
}; |
|
2136 |
||
2137 |
//Maps TEX[4:2]:CB[1:0] value into L2 cache attributes |
|
2138 |
static const TUint8 L2Actual[32]= |
|
2139 |
{ |
|
2140 |
//CB 00 01 10 11 //TEX |
|
2141 |
L2UN, L2UN, WTRA, WBRA, //000 |
|
2142 |
L2UN, UNS, UNS, WBWA, //001 |
|
2143 |
L2UN, UNS, UNS, UNS, //010 |
|
2144 |
UNS, UNS, UNS, UNS, //011 |
|
2145 |
L2UN, L2UN, L2UN, L2UN, //100 |
|
2146 |
WBWA, WBWA, WBWA, WBWA, //101 |
|
2147 |
WTRA, WTRA, WTRA, WTRA, //110 |
|
2148 |
WBRA, WBRA, WBRA, WBRA //111 |
|
2149 |
}; |
|
2150 |
||
2151 |
TInt ArmMmu::PdePtePermissions(TUint& aMapAttr, TPde& aPde, TPte& aPte) |
|
2152 |
{ |
|
2153 |
__KTRACE_OPT(KMMU,Kern::Printf(">ArmMmu::PdePtePermissions, mapattr=%08x",aMapAttr)); |
|
2154 |
||
2155 |
TUint read=aMapAttr & EMapAttrReadMask; |
|
2156 |
TUint write=(aMapAttr & EMapAttrWriteMask)>>4; |
|
2157 |
TUint exec=(aMapAttr & EMapAttrExecMask)>>8; |
|
2158 |
TUint l1cache=(aMapAttr & EMapAttrL1CacheMask)>>12; |
|
2159 |
TUint l2cache=(aMapAttr & EMapAttrL2CacheMask)>>16; |
|
2160 |
if (l1cache==MAXC) l1cache=WBRA; // map max cache to WBRA |
|
2161 |
if (l1cache>AWBW) |
|
2162 |
return KErrNotSupported; // undefined attribute |
|
2163 |
if (l1cache>=AWTR) l1cache-=4; // no alternate cache, so use normal cache |
|
2164 |
if (l1cache<L1UN) l2cache=0; // for blocking/device, don't cache L2 |
|
2165 |
if (l2cache==MAXC) l2cache=WBRA; // map max cache to WBRA |
|
2166 |
if (l2cache>WBWA) |
|
2167 |
return KErrNotSupported; // undefined attribute |
|
2168 |
if (l2cache) l2cache-=(WTRA-1); // l2cache now in range 0-4 |
|
2169 |
aPde=((aMapAttr&EMapAttrUseECC)>>8)|KArmV6PdePageTable; |
|
2170 |
||
2171 |
#if defined(__CPU_ARM1136__) && !defined(__CPU_ARM1136_ERRATUM_353494_FIXED) |
|
2172 |
// if broken 1136, can't have supervisor only code |
|
2173 |
if (exec) |
|
2174 |
exec = TUint(EMapAttrExecUser>>8); |
|
2175 |
#endif |
|
2176 |
||
2177 |
// if any execute access, must have read=execute |
|
2178 |
if (exec) |
|
2179 |
(void)(read>=exec || (read=exec)!=0), exec=1; |
|
2180 |
||
2181 |
// l1cache between 0 and 7, l2cache between 0 and 4; look up CBTEX |
|
2182 |
TUint cbtex=CBTEX[(l2cache<<3)|l1cache]; |
|
2183 |
||
2184 |
// work out apx:ap |
|
2185 |
TUint apxap; |
|
2186 |
if (write==0) |
|
2187 |
apxap=(read>=4)?KArmV6PermRORO:(read?KArmV6PermRONO:KArmV6PermNONO); |
|
2188 |
else if (write<4) |
|
2189 |
apxap=(read>=4)?KArmV6PermRWRO:KArmV6PermRWNO; |
|
2190 |
else |
|
2191 |
apxap=KArmV6PermRWRW; |
|
2192 |
TPte pte=SP_PTE(apxap, cbtex, exec, 1); // always global |
|
2193 |
if (aMapAttr&EMapAttrShared) |
|
2194 |
pte |= KArmV6PteS; |
|
2195 |
||
2196 |
// Translate back to get actual map attributes |
|
2197 |
TUint xnapxap=((pte<<3)&8)|((pte>>7)&4)|((pte>>4)&3); |
|
2198 |
cbtex=((pte>>4)&0x1c)|((pte>>2)&3); // = TEX[4:2]::CB[1:0] |
|
2199 |
aMapAttr &= ~(EMapAttrAccessMask|EMapAttrL1CacheMask|EMapAttrL2CacheMask); |
|
2200 |
aMapAttr |= PermissionLookup[xnapxap]; |
|
2201 |
aMapAttr |= (L1Actual[cbtex]<<12); |
|
2202 |
aMapAttr |= (L2Actual[cbtex]<<16); |
|
2203 |
aPte=pte; |
|
2204 |
__KTRACE_OPT(KMMU,Kern::Printf("<ArmMmu::PdePtePermissions, mapattr=%08x, pde=%08x, pte=%08x", |
|
2205 |
aMapAttr, aPde, aPte)); |
|
2206 |
return KErrNone; |
|
2207 |
} |
|
2208 |
#endif |
|
2209 |
||
2210 |
void ArmMmu::Map(TLinAddr aLinAddr, TPhysAddr aPhysAddr, TInt aSize, TPde aPdePerm, TPte aPtePerm, TInt aMapShift) |
|
2211 |
// |
|
2212 |
// Map a region of physical addresses aPhysAddr to aPhysAddr+aSize-1 to virtual address aLinAddr. |
|
2213 |
// Use permissions specified by aPdePerm and aPtePerm. Use mapping sizes up to and including (1<<aMapShift). |
|
2214 |
// Assume any page tables required are already assigned. |
|
2215 |
// aLinAddr, aPhysAddr, aSize must be page-aligned. |
|
2216 |
// |
|
2217 |
{ |
|
2218 |
__KTRACE_OPT(KMMU, Kern::Printf("ArmMmu::Map lin=%08x phys=%08x size=%08x", aLinAddr, aPhysAddr, aSize)); |
|
2219 |
__KTRACE_OPT(KMMU, Kern::Printf("pde=%08x pte=%08x mapshift=%d", aPdePerm, aPtePerm, aMapShift)); |
|
2220 |
TPde pt_pde=aPdePerm; |
|
2221 |
TPte sp_pte=aPtePerm; |
|
2222 |
TPde section_pde=SECTION_PDE_FROM_PDEPTE(pt_pde, sp_pte); |
|
2223 |
TPte lp_pte=LP_PTE_FROM_SP_PTE(sp_pte); |
|
2224 |
TLinAddr la=aLinAddr; |
|
2225 |
TPhysAddr pa=aPhysAddr; |
|
2226 |
TInt remain=aSize; |
|
2227 |
while (remain) |
|
2228 |
{ |
|
2229 |
if (aMapShift>=KChunkShift && (la & KChunkMask)==0 && remain>=KChunkSize) |
|
2230 |
{ |
|
2231 |
// use sections - ASSUMES ADDRESS IS IN GLOBAL REGION |
|
2232 |
TInt npdes=remain>>KChunkShift; |
|
2233 |
const TBitMapAllocator& b=*iOsAsidAllocator; |
|
2234 |
TInt num_os_asids=iNumGlobalPageDirs; |
|
2235 |
TInt os_asid=0; |
|
2236 |
for (; num_os_asids; ++os_asid) |
|
2237 |
{ |
|
2238 |
if (b.NotAllocated(os_asid,1) || (iAsidInfo[os_asid]&1)==0) |
|
2239 |
continue; // os_asid is not needed |
|
2240 |
TPde* p_pde=PageDirectory(os_asid)+(la>>KChunkShift); |
|
2241 |
TPde* p_pde_E=p_pde+npdes; |
|
2242 |
TPde pde=pa|section_pde; |
|
2243 |
TLinAddr firstPde = (TLinAddr)p_pde; //Will need this to clean page table memory region from cache |
|
2244 |
||
2245 |
NKern::LockSystem(); |
|
2246 |
for (; p_pde < p_pde_E; pde+=KChunkSize) |
|
2247 |
{ |
|
2248 |
__ASSERT_DEBUG(*p_pde==0, MM::Panic(MM::EPdeAlreadyInUse)); |
|
2249 |
__KTRACE_OPT(KMMU,Kern::Printf("Writing PDE %08x to %08x", pde, p_pde)); |
|
2250 |
*p_pde++=pde; |
|
2251 |
} |
|
2252 |
CacheMaintenance::MultiplePtesUpdated(firstPde, (TUint)p_pde-firstPde); |
|
2253 |
NKern::UnlockSystem(); |
|
2254 |
--num_os_asids; |
|
2255 |
} |
|
2256 |
npdes<<=KChunkShift; |
|
2257 |
la+=npdes, pa+=npdes, remain-=npdes; |
|
2258 |
continue; |
|
2259 |
} |
|
2260 |
TInt block_size = Min(remain, KChunkSize-(la&KChunkMask)); |
|
2261 |
TPte pa_mask=~KPageMask; |
|
2262 |
TPte pte_perm=sp_pte; |
|
2263 |
if (aMapShift>=KLargePageShift && block_size>=KLargePageSize) |
|
2264 |
{ |
|
2265 |
if ((la & KLargePageMask)==0) |
|
2266 |
{ |
|
2267 |
// use 64K large pages |
|
2268 |
pa_mask=~KLargePageMask; |
|
2269 |
pte_perm=lp_pte; |
|
2270 |
} |
|
2271 |
else |
|
2272 |
block_size = Min(remain, KLargePageSize-(la&KLargePageMask)); |
|
2273 |
} |
|
2274 |
block_size &= pa_mask; |
|
2275 |
||
2276 |
// use pages (large or small) |
|
2277 |
TInt id=PageTableId(la, 0); |
|
2278 |
__ASSERT_DEBUG(id>=0, MM::Panic(MM::EMmuMapNoPageTable)); |
|
2279 |
TPte* p_pte=PageTable(id)+((la&KChunkMask)>>KPageShift); |
|
2280 |
TPte* p_pte_E=p_pte + (block_size>>KPageShift); |
|
2281 |
SPageTableInfo& ptinfo=iPtInfo[id]; |
|
2282 |
TLinAddr firstPte = (TLinAddr)p_pte; //Will need this to clean page table memory region from cache |
|
2283 |
||
2284 |
NKern::LockSystem(); |
|
2285 |
for (; p_pte < p_pte_E; pa+=KPageSize) |
|
2286 |
{ |
|
2287 |
__ASSERT_DEBUG(*p_pte==0, MM::Panic(MM::EPteAlreadyInUse)); |
|
2288 |
TPte pte = (pa & pa_mask) | pte_perm; |
|
2289 |
__KTRACE_OPT(KMMU,Kern::Printf("Writing PTE %08x to %08x", pte, p_pte)); |
|
2290 |
*p_pte++=pte; |
|
2291 |
++ptinfo.iCount; |
|
2292 |
NKern::FlashSystem(); |
|
2293 |
} |
|
2294 |
CacheMaintenance::MultiplePtesUpdated(firstPte, (TUint)p_pte-firstPte); |
|
2295 |
NKern::UnlockSystem(); |
|
2296 |
la+=block_size, remain-=block_size; |
|
2297 |
} |
|
2298 |
} |
|
2299 |
||
2300 |
void ArmMmu::Unmap(TLinAddr aLinAddr, TInt aSize) |
|
2301 |
// |
|
2302 |
// Remove all mappings in the specified range of addresses. |
|
2303 |
// Assumes there are only global mappings involved. |
|
2304 |
// Don't free page tables. |
|
2305 |
// aLinAddr, aSize must be page-aligned. |
|
2306 |
// |
|
2307 |
{ |
|
2308 |
__KTRACE_OPT(KMMU, Kern::Printf("ArmMmu::Unmap lin=%08x size=%08x", aLinAddr, aSize)); |
|
2309 |
TLinAddr a=aLinAddr; |
|
2310 |
TLinAddr end=a+aSize; |
|
2311 |
__KTRACE_OPT(KMMU,Kern::Printf("a=%08x end=%08x",a,end)); |
|
2312 |
NKern::LockSystem(); |
|
2313 |
while(a!=end) |
|
2314 |
{ |
|
2315 |
TInt pdeIndex=a>>KChunkShift; |
|
2316 |
TLinAddr next=(pdeIndex<<KChunkShift)+KChunkSize; |
|
2317 |
TInt to_do = Min(TInt(end-a), TInt(next-a))>>KPageShift; |
|
2318 |
__KTRACE_OPT(KMMU,Kern::Printf("a=%08x next=%08x to_do=%d",a,next,to_do)); |
|
2319 |
TPde pde=::InitPageDirectory[pdeIndex]; |
|
2320 |
if ( (pde&KArmV6PdeTypeMask)==KArmV6PdeSection ) |
|
2321 |
{ |
|
2322 |
__ASSERT_DEBUG(!(a&KChunkMask), MM::Panic(MM::EUnmapBadAlignment)); |
|
2323 |
#if defined(__CPU_ARM1136__) && !defined(__CPU_ARM1136_ERRATUM_353494_FIXED) |
|
2324 |
remove_and_invalidate_section(::InitPageDirectory + pdeIndex, a, KERNEL_MAPPING); |
|
2325 |
#else |
|
2326 |
::InitPageDirectory[pdeIndex]=0; |
|
2327 |
CacheMaintenance::SinglePteUpdated(TLinAddr(::InitPageDirectory + pdeIndex)); |
|
2328 |
InvalidateTLBForPage(a, KERNEL_MAPPING); // ASID irrelevant since global |
|
2329 |
#endif |
|
2330 |
a=next; |
|
2331 |
NKern::FlashSystem(); |
|
2332 |
continue; |
|
2333 |
} |
|
2334 |
TInt ptid=PageTableId(a,0); |
|
2335 |
SPageTableInfo& ptinfo=iPtInfo[ptid]; |
|
2336 |
if (ptid>=0) |
|
2337 |
{ |
|
2338 |
TPte* ppte=PageTable(ptid)+((a&KChunkMask)>>KPageShift); |
|
2339 |
TPte* ppte_End=ppte+to_do; |
|
2340 |
for (; ppte<ppte_End; ++ppte, a+=KPageSize) |
|
2341 |
{ |
|
2342 |
if (*ppte & KArmV6PteSmallPage) |
|
2343 |
{ |
|
2344 |
--ptinfo.iCount; |
|
2345 |
#if defined(__CPU_ARM1136__) && !defined(__CPU_ARM1136_ERRATUM_353494_FIXED) |
|
2346 |
remove_and_invalidate_page(ppte, a, KERNEL_MAPPING); |
|
2347 |
#else |
|
2348 |
*ppte=0; |
|
2349 |
CacheMaintenance::SinglePteUpdated((TLinAddr)ppte); |
|
2350 |
InvalidateTLBForPage(a, KERNEL_MAPPING); |
|
2351 |
#endif |
|
2352 |
} |
|
2353 |
else if ((*ppte & KArmV6PteTypeMask) == KArmV6PteLargePage) |
|
2354 |
{ |
|
2355 |
__ASSERT_DEBUG(!(a&KLargePageMask), MM::Panic(MM::EUnmapBadAlignment)); |
|
2356 |
ptinfo.iCount-=KLargeSmallPageRatio; |
|
2357 |
#if defined(__CPU_ARM1136__) && !defined(__CPU_ARM1136_ERRATUM_353494_FIXED) |
|
2358 |
remove_and_invalidate_page(ppte, a, KERNEL_MAPPING); |
|
2359 |
#else |
|
2360 |
memclr(ppte, KLargeSmallPageRatio*sizeof(TPte)); |
|
2361 |
CacheMaintenance::MultiplePtesUpdated((TLinAddr)ppte, KLargeSmallPageRatio*sizeof(TPte)); |
|
2362 |
InvalidateTLBForPage(a, KERNEL_MAPPING); |
|
2363 |
#endif |
|
2364 |
a+=(KLargePageSize-KPageSize); |
|
2365 |
ppte+=(KLargeSmallPageRatio-1); |
|
2366 |
} |
|
2367 |
NKern::FlashSystem(); |
|
2368 |
} |
|
2369 |
} |
|
2370 |
else |
|
2371 |
a += (to_do<<KPageShift); |
|
2372 |
} |
|
2373 |
NKern::UnlockSystem(); |
|
2374 |
#if defined(__CPU_ARM1136__) && !defined(__CPU_ARM1136_ERRATUM_353494_FIXED) |
|
2375 |
__FlushBtb(); |
|
2376 |
#endif |
|
2377 |
} |
|
2378 |
||
2379 |
||
2380 |
void ArmMmu::ClearPages(TInt aNumPages, TPhysAddr* aPageList, TUint8 aClearByte) |
|
2381 |
{ |
|
2382 |
//map the pages at a temporary address, clear them and unmap |
|
2383 |
__ASSERT_MUTEX(RamAllocatorMutex); |
|
2384 |
while (--aNumPages >= 0) |
|
2385 |
{ |
|
2386 |
TPhysAddr pa; |
|
2387 |
if((TInt)aPageList&1) |
|
2388 |
{ |
|
2389 |
pa = (TPhysAddr)aPageList&~1; |
|
2390 |
*(TPhysAddr*)&aPageList += iPageSize; |
|
2391 |
} |
|
2392 |
else |
|
2393 |
pa = *aPageList++; |
|
2394 |
||
2395 |
*iTempPte = pa | SP_PTE(KArmV6PermRWNO, KNormalUncachedAttr, 0, 1); |
|
2396 |
CacheMaintenance::SinglePteUpdated((TLinAddr)iTempPte); |
|
2397 |
InvalidateTLBForPage(iTempAddr, KERNEL_MAPPING); |
|
2398 |
memset((TAny*)iTempAddr, aClearByte, iPageSize); |
|
2399 |
// This temporary mapping is noncached => No need to flush cache here. |
|
2400 |
// Still, we have to make sure that write buffer(s) are drained. |
|
2401 |
CacheMaintenance::MemoryToPreserveAndReuse((TLinAddr)iTempAddr, iPageSize, EMapAttrBufferedC); |
|
2402 |
} |
|
2403 |
*iTempPte=0; |
|
2404 |
CacheMaintenance::SinglePteUpdated((TLinAddr)iTempPte); |
|
2405 |
InvalidateTLBForPage(iTempAddr, KERNEL_MAPPING); |
|
2406 |
} |
|
2407 |
||
2408 |
||
2409 |
/** |
|
2410 |
Create a temporary mapping of one or more contiguous physical pages. |
|
2411 |
Fully cached memory attributes apply. |
|
2412 |
The RamAllocatorMutex must be held before this function is called and not released |
|
2413 |
until after UnmapTemp has been called. |
|
2414 |
||
2415 |
@param aPage The physical address of the pages to be mapped. |
|
2416 |
@param aLinAddr The linear address of any existing location where the page is mapped. |
|
2417 |
If the page isn't already mapped elsewhere as a cachable page then |
|
2418 |
this value irrelevent. (It is used for page colouring.) |
|
2419 |
@param aPages Number of pages to map. |
|
2420 |
||
2421 |
@return The linear address of where the pages have been mapped. |
|
2422 |
*/ |
|
2423 |
TLinAddr ArmMmu::MapTemp(TPhysAddr aPage,TLinAddr aLinAddr,TInt aPages) |
|
2424 |
{ |
|
2425 |
__ASSERT_MUTEX(RamAllocatorMutex); |
|
2426 |
__ASSERT_DEBUG(!*iTempPte,MM::Panic(MM::ETempMappingAlreadyInUse)); |
|
2427 |
iTempMapColor = (aLinAddr>>KPageShift)&KPageColourMask; |
|
2428 |
iTempMapCount = aPages; |
|
2429 |
if (aPages==1) |
|
2430 |
{ |
|
2431 |
iTempPte[iTempMapColor] = (aPage&~KPageMask) | SP_PTE(KArmV6PermRWNO, KNormalCachedAttr, 0, 1); |
|
2432 |
CacheMaintenance::SinglePteUpdated((TLinAddr)(iTempPte+iTempMapColor)); |
|
2433 |
} |
|
2434 |
else |
|
2435 |
{ |
|
2436 |
__ASSERT_DEBUG(iTempMapColor+aPages<=KPageColourCount,MM::Panic(MM::ETempMappingNoRoom)); |
|
2437 |
for (TInt i=0; i<aPages; i++) |
|
2438 |
iTempPte[iTempMapColor+i] = ((aPage&~KPageMask)+(i<<KPageShift)) | SP_PTE(KArmV6PermRWNO, KNormalCachedAttr, 0, 1); |
|
2439 |
CacheMaintenance::MultiplePtesUpdated((TLinAddr)(iTempPte+iTempMapColor), aPages*sizeof(TPte)); |
|
2440 |
} |
|
2441 |
return iTempAddr+(iTempMapColor<<KPageShift); |
|
2442 |
} |
|
2443 |
||
2444 |
/** |
|
2445 |
Create a temporary mapping of one or more contiguous physical pages. |
|
2446 |
Memory attributes as specified by aMemType apply. |
|
2447 |
@See ArmMmu::MapTemp(TPhysAddr aPage,TLinAddr aLinAddr,TInt aPages) for other details. |
|
2448 |
*/ |
|
2449 |
TLinAddr ArmMmu::MapTemp(TPhysAddr aPage,TLinAddr aLinAddr,TInt aPages, TMemoryType aMemType) |
|
2450 |
{ |
|
2451 |
__ASSERT_MUTEX(RamAllocatorMutex); |
|
2452 |
__ASSERT_DEBUG(!*iTempPte,MM::Panic(MM::ETempMappingAlreadyInUse)); |
|
2453 |
iTempMapColor = (aLinAddr>>KPageShift)&KPageColourMask; |
|
2454 |
iTempMapCount = aPages; |
|
2455 |
TUint pte = SP_PTE(KArmV6PermRWNO, aMemType, 0, 1); |
|
2456 |
if (aPages==1) |
|
2457 |
{ |
|
2458 |
iTempPte[iTempMapColor] = (aPage&~KPageMask) | SP_PTE(KArmV6PermRWNO, pte, 0, 1); |
|
2459 |
CacheMaintenance::SinglePteUpdated((TLinAddr)(iTempPte+iTempMapColor)); |
|
2460 |
} |
|
2461 |
else |
|
2462 |
{ |
|
2463 |
__ASSERT_DEBUG(iTempMapColor+aPages<=KPageColourCount,MM::Panic(MM::ETempMappingNoRoom)); |
|
2464 |
for (TInt i=0; i<aPages; i++) |
|
2465 |
iTempPte[iTempMapColor+i] = ((aPage&~KPageMask)+(i<<KPageShift)) | SP_PTE(KArmV6PermRWNO, pte, 0, 1); |
|
2466 |
CacheMaintenance::MultiplePtesUpdated((TLinAddr)(iTempPte+iTempMapColor), aPages*sizeof(TPte)); |
|
2467 |
} |
|
2468 |
return iTempAddr+(iTempMapColor<<KPageShift); |
|
2469 |
} |
|
2470 |
||
2471 |
/** |
|
2472 |
Create a temporary mapping of one or more contiguous physical pages, distinct from |
|
2473 |
that created by MapTemp. |
|
2474 |
The RamAllocatorMutex must be held before this function is called and not released |
|
2475 |
until after UnmapSecondTemp has been called. |
|
2476 |
||
2477 |
@param aPage The physical address of the pages to be mapped. |
|
2478 |
@param aLinAddr The linear address of any existing location where the page is mapped. |
|
2479 |
If the page isn't already mapped elsewhere as a cachable page then |
|
2480 |
this value irrelevent. (It is used for page colouring.) |
|
2481 |
@param aPages Number of pages to map. |
|
2482 |
||
2483 |
@return The linear address of where the pages have been mapped. |
|
2484 |
*/ |
|
2485 |
TLinAddr ArmMmu::MapSecondTemp(TPhysAddr aPage,TLinAddr aLinAddr,TInt aPages) |
|
2486 |
{ |
|
2487 |
__ASSERT_MUTEX(RamAllocatorMutex); |
|
2488 |
__ASSERT_DEBUG(!*iSecondTempPte,MM::Panic(MM::ETempMappingAlreadyInUse)); |
|
2489 |
iSecondTempMapColor = (aLinAddr>>KPageShift)&KPageColourMask; |
|
2490 |
iSecondTempMapCount = aPages; |
|
2491 |
if (aPages==1) |
|
2492 |
{ |
|
2493 |
iSecondTempPte[iSecondTempMapColor] = (aPage&~KPageMask) | SP_PTE(KArmV6PermRWNO, KNormalCachedAttr, 0, 1); |
|
2494 |
CacheMaintenance::SinglePteUpdated((TLinAddr)(iSecondTempPte+iSecondTempMapColor)); |
|
2495 |
} |
|
2496 |
else |
|
2497 |
{ |
|
2498 |
__ASSERT_DEBUG(iSecondTempMapColor+aPages<=KPageColourCount,MM::Panic(MM::ETempMappingNoRoom)); |
|
2499 |
for (TInt i=0; i<aPages; i++) |
|
2500 |
iSecondTempPte[iSecondTempMapColor+i] = ((aPage&~KPageMask)+(i<<KPageShift)) | SP_PTE(KArmV6PermRWNO, KNormalCachedAttr, 0, 1); |
|
2501 |
CacheMaintenance::MultiplePtesUpdated((TLinAddr)(iSecondTempPte+iSecondTempMapColor), aPages*sizeof(TPte)); |
|
2502 |
} |
|
2503 |
return iSecondTempAddr+(iSecondTempMapColor<<KPageShift); |
|
2504 |
} |
|
2505 |
||
2506 |
/** |
|
2507 |
Remove the temporary mapping created with MapTemp. |
|
2508 |
*/ |
|
2509 |
void ArmMmu::UnmapTemp() |
|
2510 |
{ |
|
2511 |
__ASSERT_MUTEX(RamAllocatorMutex); |
|
2512 |
for (TInt i=0; i<iTempMapCount; i++) |
|
2513 |
{ |
|
2514 |
iTempPte[iTempMapColor+i] = 0; |
|
2515 |
CacheMaintenance::SinglePteUpdated((TLinAddr)(iTempPte+iTempMapColor+i)); |
|
2516 |
InvalidateTLBForPage(iTempAddr+((iTempMapColor+i)<<KPageShift), KERNEL_MAPPING); |
|
2517 |
} |
|
2518 |
} |
|
2519 |
||
2520 |
/** |
|
2521 |
Remove the temporary mapping created with MapSecondTemp. |
|
2522 |
*/ |
|
2523 |
void ArmMmu::UnmapSecondTemp() |
|
2524 |
{ |
|
2525 |
__ASSERT_MUTEX(RamAllocatorMutex); |
|
2526 |
for (TInt i=0; i<iSecondTempMapCount; i++) |
|
2527 |
{ |
|
2528 |
iSecondTempPte[iSecondTempMapColor+i] = 0; |
|
2529 |
CacheMaintenance::SinglePteUpdated((TLinAddr)(iSecondTempPte+iSecondTempMapColor+i)); |
|
2530 |
InvalidateTLBForPage(iSecondTempAddr+((iSecondTempMapColor+i)<<KPageShift), KERNEL_MAPPING); |
|
2531 |
} |
|
2532 |
} |
|
2533 |
||
2534 |
||
2535 |
TBool ArmMmu::ValidateLocalIpcAddress(TLinAddr aAddr,TInt aSize,TBool aWrite) |
|
2536 |
{ |
|
2537 |
__NK_ASSERT_DEBUG(aSize<=KChunkSize); |
|
2538 |
TLinAddr end = aAddr+aSize-1; |
|
2539 |
if(end<aAddr) |
|
2540 |
end = ~0u; |
|
2541 |
||
2542 |
if(TUint(aAddr^KIPCAlias)<TUint(KChunkSize) || TUint(end^KIPCAlias)<TUint(KChunkSize)) |
|
2543 |
{ |
|
2544 |
// local address is in alias region. |
|
2545 |
// remove alias... |
|
2546 |
NKern::LockSystem(); |
|
2547 |
((DMemModelThread*)TheCurrentThread)->RemoveAlias(); |
|
2548 |
NKern::UnlockSystem(); |
|
2549 |
// access memory, which will cause an exception... |
|
2550 |
if(!(TUint(aAddr^KIPCAlias)<TUint(KChunkSize))) |
|
2551 |
aAddr = end; |
|
2552 |
InvalidateTLBForPage(aAddr,((DMemModelProcess*)TheCurrentThread->iOwningProcess)->iOsAsid); |
|
2553 |
if(aWrite) |
|
2554 |
*(volatile TUint8*)aAddr = 0; |
|
2555 |
else |
|
2556 |
aWrite = *(volatile TUint8*)aAddr; |
|
2557 |
// can't get here |
|
2558 |
__NK_ASSERT_DEBUG(0); |
|
2559 |
} |
|
2560 |
||
2561 |
TUint32 local_mask; |
|
2562 |
DMemModelProcess* process=(DMemModelProcess*)TheCurrentThread->iOwningProcess; |
|
2563 |
if(aWrite) |
|
2564 |
local_mask = process->iAddressCheckMaskW; |
|
2565 |
else |
|
2566 |
local_mask = process->iAddressCheckMaskR; |
|
2567 |
TUint32 mask = 2<<(end>>27); |
|
2568 |
mask -= 1<<(aAddr>>27); |
|
2569 |
if((local_mask&mask)!=mask) |
|
2570 |
return EFalse; |
|
2571 |
||
2572 |
if(!aWrite) |
|
2573 |
return ETrue; // reads are ok |
|
2574 |
||
2575 |
// writes need further checking... |
|
2576 |
TLinAddr userCodeStart = iUserCodeBase; |
|
2577 |
TLinAddr userCodeEnd = userCodeStart+iMaxUserCodeSize; |
|
2578 |
if(end>=userCodeStart && aAddr<userCodeEnd) |
|
2579 |
return EFalse; // trying to write to user code area |
|
2580 |
||
2581 |
return ETrue; |
|
2582 |
} |
|
2583 |
||
2584 |
TInt DMemModelThread::Alias(TLinAddr aAddr, DMemModelProcess* aProcess, TInt aSize, TInt aPerm, TLinAddr& aAliasAddr, TInt& aAliasSize) |
|
2585 |
// |
|
2586 |
// Set up an alias mapping starting at address aAddr in specified process. |
|
2587 |
// Check permissions aPerm. |
|
2588 |
// Enter and return with system locked. |
|
2589 |
// Note: Alias is removed if an exception if trapped by DThread::IpcExcHandler. |
|
2590 |
// |
|
2591 |
{ |
|
2592 |
__KTRACE_OPT(KMMU2,Kern::Printf("Thread %O Alias %08x+%x Process %O perm %x",this,aAddr,aSize,aProcess,aPerm)); |
|
2593 |
__ASSERT_SYSTEM_LOCK |
|
2594 |
||
2595 |
if(TUint(aAddr^KIPCAlias)<TUint(KChunkSize)) |
|
2596 |
return KErrBadDescriptor; // prevent access to alias region |
|
2597 |
||
2598 |
ArmMmu& m=::TheMmu; |
|
2599 |
||
2600 |
// check if memory is in region which is safe to access with supervisor permissions... |
|
2601 |
TBool okForSupervisorAccess = aPerm&(EMapAttrReadSup|EMapAttrWriteSup) ? 1 : 0; |
|
2602 |
if(!okForSupervisorAccess) |
|
2603 |
{ |
|
2604 |
TInt shift = aAddr>>27; |
|
2605 |
if(!(aPerm&EMapAttrWriteUser)) |
|
2606 |
{ |
|
2607 |
// reading with user permissions... |
|
2608 |
okForSupervisorAccess = (aProcess->iAddressCheckMaskR>>shift)&1; |
|
2609 |
} |
|
2610 |
else |
|
2611 |
{ |
|
2612 |
// writing with user permissions... |
|
2613 |
okForSupervisorAccess = (aProcess->iAddressCheckMaskW>>shift)&1; |
|
2614 |
if(okForSupervisorAccess) |
|
2615 |
{ |
|
2616 |
// check for user code, because this is supervisor r/w and so |
|
2617 |
// is not safe to write to access with supervisor permissions. |
|
2618 |
if(TUint(aAddr-m.iUserCodeBase)<TUint(m.iMaxUserCodeSize)) |
|
2619 |
return KErrBadDescriptor; // prevent write to this... |
|
2620 |
} |
|
2621 |
} |
|
2622 |
} |
|
2623 |
||
2624 |
TInt pdeIndex = aAddr>>KChunkShift; |
|
2625 |
if(pdeIndex>=(m.iLocalPdSize>>2)) |
|
2626 |
{ |
|
2627 |
// address is in global section, don't bother aliasing it... |
|
2628 |
if(iAliasLinAddr) |
|
2629 |
RemoveAlias(); |
|
2630 |
aAliasAddr = aAddr; |
|
2631 |
TInt maxSize = KChunkSize-(aAddr&KChunkMask); |
|
2632 |
aAliasSize = aSize<maxSize ? aSize : maxSize; |
|
2633 |
__KTRACE_OPT(KMMU2,Kern::Printf("DMemModelThread::Alias() abandoned as memory is globaly mapped")); |
|
2634 |
return okForSupervisorAccess; |
|
2635 |
} |
|
2636 |
||
2637 |
TInt asid = aProcess->iOsAsid; |
|
2638 |
TPde* pd = PageDirectory(asid); |
|
2639 |
TPde pde = pd[pdeIndex]; |
|
2640 |
if ((TPhysAddr)(pde&~KPageMask) == AliasRemapOld) |
|
2641 |
pde = AliasRemapNew|(pde&KPageMask); |
|
4
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2642 |
pde = PDE_IN_DOMAIN(pde, KIPCAliasDomain); |
0 | 2643 |
TLinAddr aliasAddr = KIPCAlias+(aAddr&(KChunkMask & ~KPageMask)); |
2644 |
if(pde==iAliasPde && iAliasLinAddr) |
|
2645 |
{ |
|
2646 |
// pde already aliased, so just update linear address... |
|
2647 |
iAliasLinAddr = aliasAddr; |
|
2648 |
} |
|
2649 |
else |
|
2650 |
{ |
|
2651 |
// alias PDE changed... |
|
2652 |
iAliasPde = pde; |
|
2653 |
iAliasOsAsid = asid; |
|
2654 |
if(!iAliasLinAddr) |
|
2655 |
{ |
|
2656 |
ArmMmu::UnlockAlias(); |
|
2657 |
::TheMmu.iAliasList.Add(&iAliasLink); // add to list if not already aliased |
|
2658 |
} |
|
2659 |
iAliasLinAddr = aliasAddr; |
|
2660 |
*iAliasPdePtr = pde; |
|
2661 |
CacheMaintenance::SinglePteUpdated((TLinAddr)iAliasPdePtr); |
|
2662 |
} |
|
2663 |
||
2664 |
__KTRACE_OPT(KMMU2,Kern::Printf("DMemModelThread::Alias() PDEntry=%x, iAliasLinAddr=%x",pde, aliasAddr)); |
|
2665 |
InvalidateTLBForPage(aliasAddr, ((DMemModelProcess*)iOwningProcess)->iOsAsid); |
|
2666 |
TInt offset = aAddr&KPageMask; |
|
2667 |
aAliasAddr = aliasAddr | offset; |
|
2668 |
TInt maxSize = KPageSize - offset; |
|
2669 |
aAliasSize = aSize<maxSize ? aSize : maxSize; |
|
2670 |
iAliasTarget = aAddr & ~KPageMask; |
|
2671 |
return okForSupervisorAccess; |
|
2672 |
} |
|
2673 |
||
2674 |
void DMemModelThread::RemoveAlias() |
|
2675 |
// |
|
2676 |
// Remove alias mapping (if present) |
|
2677 |
// Enter and return with system locked. |
|
2678 |
// |
|
2679 |
{ |
|
2680 |
__KTRACE_OPT(KMMU2,Kern::Printf("Thread %O RemoveAlias", this)); |
|
2681 |
__ASSERT_SYSTEM_LOCK |
|
2682 |
TLinAddr addr = iAliasLinAddr; |
|
2683 |
if(addr) |
|
2684 |
{ |
|
2685 |
ArmMmu::LockAlias(); |
|
2686 |
iAliasLinAddr = 0; |
|
2687 |
iAliasPde = 0; |
|
2688 |
*iAliasPdePtr = 0; |
|
2689 |
CacheMaintenance::SinglePteUpdated((TLinAddr)iAliasPdePtr); |
|
2690 |
InvalidateTLBForPage(addr, ((DMemModelProcess*)iOwningProcess)->iOsAsid); |
|
2691 |
iAliasLink.Deque(); |
|
2692 |
} |
|
2693 |
} |
|
2694 |
||
2695 |
/* |
|
2696 |
* Performs cache maintenance for physical page that is going to be reused. |
|
2697 |
* Fully cached attributes are assumed. |
|
2698 |
*/ |
|
2699 |
void ArmMmu::CacheMaintenanceOnDecommit(TPhysAddr a) |
|
2700 |
{ |
|
2701 |
// purge a single page from the cache following decommit |
|
2702 |
ArmMmu& m=::TheMmu; |
|
2703 |
TInt colour = SPageInfo::FromPhysAddr(a)->Offset()&KPageColourMask; |
|
2704 |
TPte& pte=m.iTempPte[colour]; |
|
2705 |
TLinAddr va=m.iTempAddr+(colour<<KPageShift); |
|
2706 |
pte=a|SP_PTE(KArmV6PermRWNO, iCacheMaintenanceTempMapAttr, 1, 1); |
|
2707 |
CacheMaintenance::SinglePteUpdated((TLinAddr)&pte); |
|
2708 |
||
2709 |
CacheMaintenance::PageToReuse(va,EMemAttNormalCached, a); |
|
2710 |
||
2711 |
pte=0; |
|
2712 |
CacheMaintenance::SinglePteUpdated((TLinAddr)&pte); |
|
2713 |
InvalidateTLBForPage(va,KERNEL_MAPPING); |
|
2714 |
} |
|
2715 |
||
2716 |
void ArmMmu::CacheMaintenanceOnDecommit(const TPhysAddr* al, TInt n) |
|
2717 |
{ |
|
2718 |
// purge a list of pages from the cache following decommit |
|
2719 |
while (--n>=0) |
|
2720 |
ArmMmu::CacheMaintenanceOnDecommit(*al++); |
|
2721 |
} |
|
2722 |
||
2723 |
/* |
|
2724 |
* Performs cache maintenance to preserve physical page that is going to be reused. |
|
2725 |
*/ |
|
2726 |
void ArmMmu::CacheMaintenanceOnPreserve(TPhysAddr a, TUint aMapAttr) |
|
2727 |
{ |
|
2728 |
// purge a single page from the cache following decommit |
|
2729 |
ArmMmu& m=::TheMmu; |
|
2730 |
TInt colour = SPageInfo::FromPhysAddr(a)->Offset()&KPageColourMask; |
|
2731 |
TPte& pte=m.iTempPte[colour]; |
|
2732 |
TLinAddr va=m.iTempAddr+(colour<<KPageShift); |
|
2733 |
pte=a|SP_PTE(KArmV6PermRWNO, iCacheMaintenanceTempMapAttr, 1, 1); |
|
2734 |
CacheMaintenance::SinglePteUpdated((TLinAddr)&pte); |
|
2735 |
||
2736 |
CacheMaintenance::MemoryToPreserveAndReuse(va, KPageSize,aMapAttr); |
|
2737 |
||
2738 |
pte=0; |
|
2739 |
CacheMaintenance::SinglePteUpdated((TLinAddr)&pte); |
|
2740 |
InvalidateTLBForPage(va,KERNEL_MAPPING); |
|
2741 |
} |
|
2742 |
||
2743 |
void ArmMmu::CacheMaintenanceOnPreserve(const TPhysAddr* al, TInt n, TUint aMapAttr) |
|
2744 |
{ |
|
2745 |
// purge a list of pages from the cache following decommit |
|
2746 |
while (--n>=0) |
|
2747 |
ArmMmu::CacheMaintenanceOnPreserve(*al++, aMapAttr); |
|
2748 |
} |
|
2749 |
||
2750 |
/* |
|
2751 |
* Performs cache maintenance of physical memory that has been decommited and has to be preserved. |
|
2752 |
* Call this method for physical pages with no page info updated (or no page info at all). |
|
2753 |
* @arg aPhysAddr The address of contiguous physical memory to be preserved. |
|
2754 |
* @arg aSize The size of the region |
|
2755 |
* @arg aLinAddr Former linear address of the region. As said above, the physical memory is |
|
2756 |
* already remapped from this linear address. |
|
2757 |
* @arg aMapAttr Mapping attributes of the region when it was mapped in aLinAddr. |
|
2758 |
* @pre MMU mutex is held. |
|
2759 |
*/ |
|
2760 |
void ArmMmu::CacheMaintenanceOnPreserve(TPhysAddr aPhysAddr, TInt aSize, TLinAddr aLinAddr, TUint aMapAttr ) |
|
2761 |
{ |
|
2762 |
__NK_ASSERT_DEBUG((aPhysAddr&KPageMask)==0); |
|
2763 |
__NK_ASSERT_DEBUG((aSize&KPageMask)==0); |
|
2764 |
__NK_ASSERT_DEBUG((aLinAddr&KPageMask)==0); |
|
2765 |
||
2766 |
TPhysAddr pa = aPhysAddr; |
|
2767 |
TInt size = aSize; |
|
2768 |
TInt colour = (aLinAddr>>KPageShift)&KPageColourMask; |
|
2769 |
TPte* pte = &(iTempPte[colour]); |
|
2770 |
while (size) |
|
2771 |
{ |
|
2772 |
pte=&(iTempPte[colour]); |
|
2773 |
TLinAddr va=iTempAddr+(colour<<KPageShift); |
|
2774 |
*pte=pa|SP_PTE(KArmV6PermRWNO, iCacheMaintenanceTempMapAttr, 1, 1); |
|
2775 |
CacheMaintenance::SinglePteUpdated((TLinAddr)pte); |
|
2776 |
CacheMaintenance::MemoryToPreserveAndReuse(va, KPageSize,aMapAttr); |
|
2777 |
||
2778 |
*pte=0; |
|
2779 |
CacheMaintenance::SinglePteUpdated((TLinAddr)pte); |
|
2780 |
InvalidateTLBForPage(va,KERNEL_MAPPING); |
|
2781 |
||
2782 |
colour = (colour+1)&KPageColourMask; |
|
2783 |
pa += KPageSize; |
|
2784 |
size -=KPageSize; |
|
2785 |
} |
|
2786 |
} |
|
2787 |
||
2788 |
TInt ArmMmu::UnlockRamCachePages(TLinAddr aLinAddr, TInt aNumPages, DProcess* aProcess) |
|
2789 |
{ |
|
2790 |
TInt asid = ((DMemModelProcess*)aProcess)->iOsAsid; |
|
2791 |
TInt page = aLinAddr>>KPageShift; |
|
2792 |
NKern::LockSystem(); |
|
2793 |
for(;;) |
|
2794 |
{ |
|
2795 |
TPde* pd = PageDirectory(asid)+(page>>(KChunkShift-KPageShift)); |
|
2796 |
TPte* pt = SafePageTableFromPde(*pd++); |
|
2797 |
TInt pteIndex = page&(KChunkMask>>KPageShift); |
|
2798 |
if(!pt) |
|
2799 |
{ |
|
2800 |
// whole page table has gone, so skip all pages in it... |
|
2801 |
TInt pagesInPt = (KChunkSize>>KPageShift)-pteIndex; |
|
2802 |
aNumPages -= pagesInPt; |
|
2803 |
page += pagesInPt; |
|
2804 |
if(aNumPages>0) |
|
2805 |
continue; |
|
2806 |
NKern::UnlockSystem(); |
|
2807 |
return KErrNone; |
|
2808 |
} |
|
2809 |
pt += pteIndex; |
|
2810 |
do |
|
2811 |
{ |
|
2812 |
TInt pagesInPt = (KChunkSize>>KPageShift)-pteIndex; |
|
2813 |
if(pagesInPt>aNumPages) |
|
2814 |
pagesInPt = aNumPages; |
|
2815 |
if(pagesInPt>KMaxPages) |
|
2816 |
pagesInPt = KMaxPages; |
|
2817 |
||
2818 |
aNumPages -= pagesInPt; |
|
2819 |
page += pagesInPt; |
|
2820 |
||
2821 |
do |
|
2822 |
{ |
|
2823 |
TPte pte = *pt++; |
|
2824 |
if(pte) // pte may be null if page has already been unlocked and reclaimed by system |
|
2825 |
iRamCache->DonateRamCachePage(SPageInfo::FromPhysAddr(pte)); |
|
2826 |
} |
|
2827 |
while(--pagesInPt); |
|
2828 |
||
2829 |
if(!aNumPages) |
|
2830 |
{ |
|
2831 |
NKern::UnlockSystem(); |
|
2832 |
return KErrNone; |
|
2833 |
} |
|
2834 |
||
2835 |
pteIndex = page&(KChunkMask>>KPageShift); |
|
2836 |
} |
|
2837 |
while(!NKern::FlashSystem() && pteIndex); |
|
2838 |
} |
|
2839 |
} |
|
2840 |
||
2841 |
||
2842 |
TInt ArmMmu::LockRamCachePages(TLinAddr aLinAddr, TInt aNumPages, DProcess* aProcess) |
|
2843 |
{ |
|
2844 |
TInt asid = ((DMemModelProcess*)aProcess)->iOsAsid; |
|
2845 |
TInt page = aLinAddr>>KPageShift; |
|
2846 |
NKern::LockSystem(); |
|
2847 |
for(;;) |
|
2848 |
{ |
|
2849 |
TPde* pd = PageDirectory(asid)+(page>>(KChunkShift-KPageShift)); |
|
2850 |
TPte* pt = SafePageTableFromPde(*pd++); |
|
2851 |
TInt pteIndex = page&(KChunkMask>>KPageShift); |
|
2852 |
if(!pt) |
|
2853 |
goto not_found; |
|
2854 |
pt += pteIndex; |
|
2855 |
do |
|
2856 |
{ |
|
2857 |
TInt pagesInPt = (KChunkSize>>KPageShift)-pteIndex; |
|
2858 |
if(pagesInPt>aNumPages) |
|
2859 |
pagesInPt = aNumPages; |
|
2860 |
if(pagesInPt>KMaxPages) |
|
2861 |
pagesInPt = KMaxPages; |
|
2862 |
||
2863 |
aNumPages -= pagesInPt; |
|
2864 |
page += pagesInPt; |
|
2865 |
||
2866 |
do |
|
2867 |
{ |
|
2868 |
TPte pte = *pt++; |
|
2869 |
if(pte==0) |
|
2870 |
goto not_found; |
|
2871 |
if(!iRamCache->ReclaimRamCachePage(SPageInfo::FromPhysAddr(pte))) |
|
2872 |
goto not_found; |
|
2873 |
} |
|
2874 |
while(--pagesInPt); |
|
2875 |
||
2876 |
if(!aNumPages) |
|
2877 |
{ |
|
2878 |
NKern::UnlockSystem(); |
|
2879 |
return KErrNone; |
|
2880 |
} |
|
2881 |
||
2882 |
pteIndex = page&(KChunkMask>>KPageShift); |
|
2883 |
} |
|
2884 |
while(!NKern::FlashSystem() && pteIndex); |
|
2885 |
} |
|
2886 |
not_found: |
|
2887 |
NKern::UnlockSystem(); |
|
2888 |
return KErrNotFound; |
|
2889 |
} |
|
2890 |
||
2891 |
||
2892 |
void RamCache::SetFree(SPageInfo* aPageInfo) |
|
2893 |
{ |
|
2894 |
ArmMmu& m=::TheMmu; |
|
2895 |
// Make a page free |
|
2896 |
SPageInfo::TType type = aPageInfo->Type(); |
|
2897 |
if(type==SPageInfo::EPagedCache) |
|
2898 |
{ |
|
2899 |
TInt offset = aPageInfo->Offset()<<KPageShift; |
|
2900 |
DMemModelChunk* chunk = (DMemModelChunk*)aPageInfo->Owner(); |
|
2901 |
__NK_ASSERT_DEBUG(TUint(offset)<TUint(chunk->iMaxSize)); |
|
2902 |
TLinAddr lin = ((TLinAddr)chunk->iBase)+offset; |
|
2903 |
TInt asid = ((DMemModelProcess*)chunk->iOwningProcess)->iOsAsid; |
|
2904 |
TPte* pt = PtePtrFromLinAddr(lin,asid); |
|
2905 |
TPhysAddr phys = (*pt)&~KPageMask; |
|
2906 |
*pt = KPteNotPresentEntry; |
|
2907 |
CacheMaintenance::SinglePteUpdated((TLinAddr)pt); |
|
2908 |
InvalidateTLBForPage(lin,asid); |
|
2909 |
m.CacheMaintenanceOnDecommit(phys); |
|
2910 |
||
2911 |
// actually decommit it from chunk... |
|
2912 |
TInt ptid = ((TLinAddr)pt-KPageTableBase)>>KPageTableShift; |
|
2913 |
SPageTableInfo& ptinfo=((ArmMmu*)iMmu)->iPtInfo[ptid]; |
|
2914 |
if(!--ptinfo.iCount) |
|
2915 |
{ |
|
2916 |
chunk->iPageTables[offset>>KChunkShift] = 0xffff; |
|
2917 |
NKern::UnlockSystem(); |
|
2918 |
((ArmMmu*)iMmu)->DoUnassignPageTable(lin, (TAny*)asid); |
|
2919 |
((ArmMmu*)iMmu)->FreePageTable(ptid); |
|
2920 |
NKern::LockSystem(); |
|
2921 |
} |
|
2922 |
} |
|
2923 |
else |
|
2924 |
{ |
|
2925 |
__KTRACE_OPT2(KPAGING,KPANIC,Kern::Printf("DP: SetFree() with bad page type = %d",aPageInfo->Type())); |
|
2926 |
Panic(EUnexpectedPageType); |
|
2927 |
} |
|
2928 |
} |
|
2929 |
||
2930 |
||
2931 |
// |
|
2932 |
// MemModelDemandPaging |
|
2933 |
// |
|
2934 |
||
2935 |
class MemModelDemandPaging : public DemandPaging |
|
2936 |
{ |
|
2937 |
public: |
|
2938 |
// From RamCacheBase |
|
2939 |
virtual void Init2(); |
|
2940 |
virtual TInt Init3(); |
|
2941 |
virtual TBool PageUnmapped(SPageInfo* aPageInfo); |
|
2942 |
// From DemandPaging |
|
2943 |
virtual TInt Fault(TAny* aExceptionInfo); |
|
2944 |
virtual void SetOld(SPageInfo* aPageInfo); |
|
2945 |
virtual void SetFree(SPageInfo* aPageInfo); |
|
2946 |
virtual void NotifyPageFree(TPhysAddr aPage); |
|
2947 |
virtual TInt EnsurePagePresent(TLinAddr aPage, DProcess* aProcess); |
|
2948 |
virtual TPhysAddr LinearToPhysical(TLinAddr aPage, DProcess* aProcess); |
|
2949 |
virtual void AllocLoadAddress(DPagingRequest& aReq, TInt aDeviceId); |
|
2950 |
virtual TInt PageState(TLinAddr aAddr); |
|
2951 |
virtual TBool NeedsMutexOrderCheck(TLinAddr aStartAddr, TUint aLength); |
|
2952 |
// New |
|
2953 |
inline ArmMmu& Mmu() { return (ArmMmu&)*iMmu; } |
|
2954 |
void InitRomPaging(); |
|
2955 |
void InitCodePaging(); |
|
2956 |
TInt HandleFault(TArmExcInfo& aExc, TLinAddr aFaultAddress, TInt aAsid); |
|
2957 |
TInt PageIn(TLinAddr aAddress, TInt aAsid, DMemModelCodeSegMemory* aCodeSegMemory); |
|
2958 |
public: |
|
2959 |
// use of the folowing members is protected by the system lock.. |
|
2960 |
TPte* iPurgePte; // PTE used for temporary mappings during cache purge operations |
|
2961 |
TLinAddr iPurgeAddr; // address corresponding to iPurgePte |
|
2962 |
}; |
|
2963 |
||
2964 |
extern void MakeGlobalPTEInaccessible(TPte* aPtePtr, TPte aNewPte, TLinAddr aLinAddr); |
|
2965 |
extern void MakePTEInaccessible(TPte* aPtePtr, TPte aNewPte, TLinAddr aLinAddr, TInt aAsid); |
|
2966 |
||
2967 |
// |
|
2968 |
// MemModelDemandPaging |
|
2969 |
// |
|
2970 |
||
2971 |
||
2972 |
DemandPaging* DemandPaging::New() |
|
2973 |
{ |
|
2974 |
return new MemModelDemandPaging(); |
|
2975 |
} |
|
2976 |
||
2977 |
||
2978 |
void MemModelDemandPaging::Init2() |
|
2979 |
{ |
|
2980 |
__KTRACE_OPT2(KPAGING,KBOOT,Kern::Printf(">MemModelDemandPaging::Init2")); |
|
2981 |
DemandPaging::Init2(); |
|
2982 |
||
2983 |
iPurgeAddr = KDemandPagingTempAddr; |
|
2984 |
iPurgePte = PtePtrFromLinAddr(iPurgeAddr); |
|
2985 |
||
2986 |
__KTRACE_OPT2(KPAGING,KBOOT,Kern::Printf("<MemModelDemandPaging::Init2")); |
|
2987 |
} |
|
2988 |
||
2989 |
||
2990 |
void MemModelDemandPaging::AllocLoadAddress(DPagingRequest& aReq, TInt aReqId) |
|
2991 |
{ |
|
2992 |
aReq.iLoadAddr = iTempPages + aReqId * KPageSize * KPageColourCount; |
|
2993 |
aReq.iLoadPte = PtePtrFromLinAddr(aReq.iLoadAddr); |
|
2994 |
} |
|
2995 |
||
2996 |
||
2997 |
TInt MemModelDemandPaging::Init3() |
|
2998 |
{ |
|
2999 |
TInt r=DemandPaging::Init3(); |
|
3000 |
if(r!=KErrNone) |
|
3001 |
return r; |
|
3002 |
||
3003 |
// Create a region for mapping pages during page in |
|
3004 |
DPlatChunkHw* chunk; |
|
3005 |
TInt chunkSize = (KMaxPagingDevices * KPagingRequestsPerDevice + 1) * KPageColourCount * KPageSize; |
|
3006 |
DPlatChunkHw::DoNew(chunk, KPhysAddrInvalid, chunkSize, EMapAttrSupRw|EMapAttrFullyBlocking); |
|
3007 |
if(!chunk) |
|
3008 |
Panic(EInitialiseFailed); |
|
3009 |
TInt colourMask = KPageColourMask << KPageShift; |
|
3010 |
iTempPages = (chunk->iLinAddr + colourMask) & ~colourMask; |
|
3011 |
||
3012 |
if(RomPagingRequested()) |
|
3013 |
InitRomPaging(); |
|
3014 |
||
3015 |
if (CodePagingRequested()) |
|
3016 |
InitCodePaging(); |
|
3017 |
||
3018 |
__KTRACE_OPT2(KPAGING,KBOOT,Kern::Printf("<MemModelDemandPaging::Init3")); |
|
3019 |
return KErrNone; |
|
3020 |
} |
|
3021 |
||
3022 |
void MemModelDemandPaging::InitRomPaging() |
|
3023 |
{ |
|
3024 |
// Make page tables for demand paged part of ROM... |
|
3025 |
__KTRACE_OPT2(KPAGING,KBOOT,Kern::Printf("MemModelDemandPaging::Init3 making page tables for paged ROM")); |
|
3026 |
TLinAddr lin = iRomPagedLinearBase&~KChunkMask; // first chunk with paged ROM in |
|
3027 |
TLinAddr linEnd = iRomLinearBase+iRomSize; |
|
3028 |
while(lin<linEnd) |
|
3029 |
{ |
|
3030 |
// Get a Page Table |
|
3031 |
TInt ptid = Mmu().PageTableId(lin,0); |
|
3032 |
if(ptid<0) |
|
3033 |
{ |
|
3034 |
MmuBase::Wait(); |
|
3035 |
ptid = Mmu().AllocPageTable(); |
|
3036 |
MmuBase::Signal(); |
|
3037 |
__NK_ASSERT_DEBUG(ptid>=0); |
|
3038 |
Mmu().PtInfo(ptid).SetGlobal(lin >> KChunkShift); |
|
3039 |
} |
|
3040 |
||
3041 |
// Get new page table addresses |
|
3042 |
TPte* pt = PageTable(ptid); |
|
3043 |
TPhysAddr ptPhys=Mmu().LinearToPhysical((TLinAddr)pt,0); |
|
3044 |
||
3045 |
// Pointer to page directory entry |
|
3046 |
TPde* ppde = ::InitPageDirectory + (lin>>KChunkShift); |
|
3047 |
||
3048 |
// Fill in Page Table |
|
3049 |
TPte* ptEnd = pt+(1<<(KChunkShift-KPageShift)); |
|
3050 |
pt += (lin&KChunkMask)>>KPageShift; |
|
3051 |
TLinAddr firstPte = (TLinAddr)pt; // Will need this to clean page table memory region from cache |
|
3052 |
||
3053 |
do |
|
3054 |
{ |
|
3055 |
if(lin<iRomPagedLinearBase) |
|
3056 |
*pt++ = Mmu().LinearToPhysical(lin,0) | KRomPtePerm; |
|
3057 |
else |
|
3058 |
{ |
|
3059 |
MakeGlobalPTEInaccessible(pt, KPteNotPresentEntry, lin); |
|
3060 |
++pt; |
|
3061 |
} |
|
3062 |
lin += KPageSize; |
|
3063 |
} |
|
3064 |
while(pt<ptEnd && lin<=linEnd); |
|
3065 |
||
3066 |
CacheMaintenance::MultiplePtesUpdated((TLinAddr)firstPte, (TUint)pt-firstPte); |
|
3067 |
||
3068 |
// Add new Page Table to the Page Directory |
|
3069 |
TPde newpde = ptPhys | KShadowPdePerm; |
|
3070 |
__KTRACE_OPT2(KPAGING,KMMU,Kern::Printf("Writing PDE %08x to %08x", newpde, ppde)); |
|
3071 |
TInt irq=NKern::DisableAllInterrupts(); |
|
3072 |
*ppde = newpde; |
|
3073 |
CacheMaintenance::SinglePteUpdated((TLinAddr)ppde); |
|
3074 |
FlushTLBs(); |
|
3075 |
NKern::RestoreInterrupts(irq); |
|
3076 |
} |
|
3077 |
} |
|
3078 |
||
3079 |
||
3080 |
void MemModelDemandPaging::InitCodePaging() |
|
3081 |
{ |
|
3082 |
// Initialise code paging info |
|
3083 |
iCodeLinearBase = Mmu().iUserCodeBase; |
|
3084 |
iCodeSize = Mmu().iMaxUserCodeSize; |
|
3085 |
} |
|
3086 |
||
3087 |
||
3088 |
/** |
|
3089 |
@return ETrue when the unmapped page should be freed, EFalse otherwise |
|
3090 |
*/ |
|
3091 |
TBool MemModelDemandPaging::PageUnmapped(SPageInfo* aPageInfo) |
|
3092 |
{ |
|
3093 |
SPageInfo::TType type = aPageInfo->Type(); |
|
3094 |
||
3095 |
// Only have to deal with cache pages - pages containg code don't get returned to the system |
|
3096 |
// when they are decommitted from an individual process, only when the code segment is destroyed |
|
3097 |
if(type!=SPageInfo::EPagedCache) |
|
3098 |
{ |
|
3099 |
__NK_ASSERT_DEBUG(type!=SPageInfo::EPagedCode); // shouldn't happen |
|
3100 |
__NK_ASSERT_DEBUG(type!=SPageInfo::EPagedData); // not supported yet |
|
3101 |
return ETrue; |
|
3102 |
} |
|
3103 |
||
3104 |
RemovePage(aPageInfo); |
|
3105 |
AddAsFreePage(aPageInfo); |
|
3106 |
// Return false to stop DMemModelChunk::DoDecommit from freeing this page |
|
3107 |
return EFalse; |
|
3108 |
} |
|
3109 |
||
3110 |
||
3111 |
void DoSetCodeOld(SPageInfo* aPageInfo, DMemModelCodeSegMemory* aCodeSegMemory, TLinAddr aLinAddr) |
|
3112 |
{ |
|
3113 |
NThread* currentThread = NKern::CurrentThread(); |
|
3114 |
aPageInfo->SetModifier(currentThread); |
|
3115 |
// scan all address spaces... |
|
3116 |
TInt asid = -1; |
|
3117 |
TInt lastAsid = KArmV6NumAsids-1; |
|
3118 |
TUint32* ptr = aCodeSegMemory->iOsAsids->iMap; |
|
3119 |
do |
|
3120 |
{ |
|
3121 |
TUint32 bits = *ptr++; |
|
3122 |
do |
|
3123 |
{ |
|
3124 |
++asid; |
|
3125 |
if(bits&0x80000000u) |
|
3126 |
{ |
|
3127 |
// codeseg is mapped in this address space, so update PTE... |
|
3128 |
TPte* pt = PtePtrFromLinAddr(aLinAddr,asid); |
|
3129 |
TPte pte = *pt; |
|
3130 |
if(pte&KPtePresentMask) |
|
3131 |
{ |
|
3132 |
__NK_ASSERT_DEBUG((pte&~KPageMask) == aPageInfo->PhysAddr()); |
|
3133 |
MakePTEInaccessible(pt, pte&~KPtePresentMask, aLinAddr, asid); |
|
3134 |
} |
|
3135 |
} |
|
3136 |
} |
|
3137 |
while(bits<<=1); |
|
3138 |
if(NKern::FlashSystem() && aPageInfo->CheckModified(currentThread)) |
|
3139 |
return; // page was modified by another thread |
|
3140 |
asid |= 31; |
|
3141 |
} |
|
3142 |
while(asid<lastAsid); |
|
3143 |
} |
|
3144 |
||
3145 |
||
3146 |
void MemModelDemandPaging::SetOld(SPageInfo* aPageInfo) |
|
3147 |
{ |
|
3148 |
__ASSERT_SYSTEM_LOCK; |
|
3149 |
__NK_ASSERT_DEBUG(aPageInfo->State() == SPageInfo::EStatePagedOld); |
|
3150 |
||
3151 |
SPageInfo::TType type = aPageInfo->Type(); |
|
3152 |
||
3153 |
if(type==SPageInfo::EPagedROM) |
|
3154 |
{ |
|
3155 |
// get linear address of page... |
|
3156 |
TInt offset = aPageInfo->Offset()<<KPageShift; |
|
3157 |
__NK_ASSERT_DEBUG(TUint(offset)<iRomSize); |
|
3158 |
||
3159 |
// make page inaccessible... |
|
3160 |
TLinAddr lin = iRomLinearBase+offset; |
|
3161 |
TPte* pt = PtePtrFromLinAddr(lin); |
|
3162 |
MakeGlobalPTEInaccessible(pt, *pt&~KPtePresentMask, lin); |
|
3163 |
} |
|
3164 |
else if(type==SPageInfo::EPagedCode) |
|
3165 |
{ |
|
3166 |
START_PAGING_BENCHMARK; |
|
3167 |
||
3168 |
// get linear address of page... |
|
3169 |
TInt offset = aPageInfo->Offset()<<KPageShift; |
|
3170 |
__NK_ASSERT_DEBUG(TUint(offset)<iCodeSize); |
|
3171 |
TLinAddr lin = iCodeLinearBase+offset; |
|
3172 |
||
3173 |
// get CodeSegMemory... |
|
3174 |
DMemModelCodeSegMemory* codeSegMemory = (DMemModelCodeSegMemory*)aPageInfo->Owner(); |
|
3175 |
__NK_ASSERT_DEBUG(codeSegMemory && codeSegMemory->iPages && codeSegMemory->iIsDemandPaged); |
|
3176 |
||
3177 |
#ifdef _DEBUG |
|
3178 |
TInt pageNumber = (lin - codeSegMemory->iRamInfo.iCodeRunAddr) >> KPageShift; |
|
3179 |
__NK_ASSERT_DEBUG(codeSegMemory->iPages[pageNumber] == aPageInfo->PhysAddr()); |
|
3180 |
#endif |
|
3181 |
||
3182 |
// make page inaccessible... |
|
3183 |
DoSetCodeOld(aPageInfo,codeSegMemory,lin); |
|
3184 |
||
3185 |
END_PAGING_BENCHMARK(this, EPagingBmSetCodePageOld); |
|
3186 |
} |
|
3187 |
else if(type==SPageInfo::EPagedCache) |
|
3188 |
{ |
|
3189 |
// leave page accessible |
|
3190 |
} |
|
3191 |
else if(type!=SPageInfo::EPagedFree) |
|
3192 |
{ |
|
3193 |
__KTRACE_OPT2(KPAGING,KPANIC,Kern::Printf("DP: SetOld() with bad page type = %d",aPageInfo->Type())); |
|
3194 |
Panic(EUnexpectedPageType); |
|
3195 |
} |
|
3196 |
NKern::FlashSystem(); |
|
3197 |
} |
|
3198 |
||
3199 |
||
3200 |
void DoSetCodeFree(SPageInfo* aPageInfo, TPhysAddr aPhysAddr, DMemModelCodeSegMemory* aCodeSegMemory, TLinAddr aLinAddr) |
|
3201 |
{ |
|
3202 |
NThread* currentThread = NKern::CurrentThread(); |
|
3203 |
aPageInfo->SetModifier(currentThread); |
|
3204 |
// scan all address spaces... |
|
3205 |
TInt asid = -1; |
|
3206 |
TInt lastAsid = KArmV6NumAsids-1; |
|
3207 |
TUint32* ptr = aCodeSegMemory->iOsAsids->iMap; |
|
3208 |
do |
|
3209 |
{ |
|
3210 |
TUint32 bits = *ptr++; |
|
3211 |
do |
|
3212 |
{ |
|
3213 |
++asid; |
|
3214 |
if(bits&0x80000000u) |
|
3215 |
{ |
|
3216 |
// codeseg is mapped in this address space, so update PTE... |
|
3217 |
TPte* pt = PtePtrFromLinAddr(aLinAddr,asid); |
|
3218 |
TPte pte = *pt; |
|
3219 |
if (pte!=KPteNotPresentEntry && (pte&~KPageMask) == aPhysAddr) |
|
3220 |
MakePTEInaccessible(pt, KPteNotPresentEntry, aLinAddr, asid); |
|
3221 |
} |
|
3222 |
} |
|
3223 |
while(bits<<=1); |
|
3224 |
if(NKern::FlashSystem()) |
|
3225 |
{ |
|
3226 |
// nobody else should modify page! |
|
3227 |
__NK_ASSERT_DEBUG(!aPageInfo->CheckModified(currentThread)); |
|
3228 |
} |
|
3229 |
asid |= 31; |
|
3230 |
} |
|
3231 |
while(asid<lastAsid); |
|
3232 |
} |
|
3233 |
||
3234 |
||
3235 |
void MemModelDemandPaging::SetFree(SPageInfo* aPageInfo) |
|
3236 |
{ |
|
3237 |
__ASSERT_SYSTEM_LOCK; |
|
3238 |
__ASSERT_MUTEX(MmuBase::RamAllocatorMutex); |
|
3239 |
__NK_ASSERT_DEBUG(aPageInfo->State() == SPageInfo::EStatePagedDead); |
|
3240 |
if(aPageInfo->LockCount()) |
|
3241 |
Panic(ERamPageLocked); |
|
3242 |
||
3243 |
SPageInfo::TType type = aPageInfo->Type(); |
|
3244 |
TPhysAddr phys = aPageInfo->PhysAddr(); |
|
3245 |
||
3246 |
if(type==SPageInfo::EPagedROM) |
|
3247 |
{ |
|
3248 |
// get linear address of page... |
|
3249 |
TInt offset = aPageInfo->Offset()<<KPageShift; |
|
3250 |
__NK_ASSERT_DEBUG(TUint(offset)<iRomSize); |
|
3251 |
TLinAddr lin = iRomLinearBase+offset; |
|
3252 |
||
3253 |
// unmap it... |
|
3254 |
TPte* pt = PtePtrFromLinAddr(lin); |
|
3255 |
MakeGlobalPTEInaccessible(pt, KPteNotPresentEntry, lin); |
|
3256 |
||
3257 |
#ifdef BTRACE_PAGING |
|
3258 |
BTraceContext8(BTrace::EPaging,BTrace::EPagingPageOutROM,phys,lin); |
|
3259 |
#endif |
|
3260 |
} |
|
3261 |
else if(type==SPageInfo::EPagedCode) |
|
3262 |
{ |
|
3263 |
START_PAGING_BENCHMARK; |
|
3264 |
||
3265 |
// get linear address of page... |
|
3266 |
TInt offset = aPageInfo->Offset()<<KPageShift; |
|
3267 |
__NK_ASSERT_DEBUG(TUint(offset)<iCodeSize); |
|
3268 |
TLinAddr lin = iCodeLinearBase+offset; |
|
3269 |
||
3270 |
// get CodeSegMemory... |
|
3271 |
// NOTE, this cannot die because we hold the RamAlloc mutex, and the CodeSegMemory |
|
3272 |
// destructor also needs this mutex to do it's cleanup... |
|
3273 |
DMemModelCodeSegMemory* codeSegMemory = (DMemModelCodeSegMemory*)aPageInfo->Owner(); |
|
3274 |
__NK_ASSERT_DEBUG(codeSegMemory && codeSegMemory->iPages && codeSegMemory->iIsDemandPaged); |
|
3275 |
||
3276 |
// remove page from CodeSegMemory (must come before System Lock is released)... |
|
3277 |
TInt pageNumber = (lin - codeSegMemory->iRamInfo.iCodeRunAddr) >> KPageShift; |
|
3278 |
__NK_ASSERT_DEBUG(codeSegMemory->iPages[pageNumber] == aPageInfo->PhysAddr()); |
|
3279 |
codeSegMemory->iPages[pageNumber] = KPhysAddrInvalid; |
|
3280 |
||
3281 |
// unmap page from all processes it's mapped into... |
|
3282 |
DoSetCodeFree(aPageInfo,phys,codeSegMemory,lin); |
|
3283 |
||
3284 |
END_PAGING_BENCHMARK(this, EPagingBmSetCodePageFree); |
|
3285 |
#ifdef BTRACE_PAGING |
|
3286 |
BTraceContext8(BTrace::EPaging,BTrace::EPagingPageOutCode,phys,lin); |
|
3287 |
#endif |
|
3288 |
} |
|
3289 |
else if(type==SPageInfo::EPagedCache) |
|
3290 |
{ |
|
3291 |
// get linear address of page... |
|
3292 |
TInt offset = aPageInfo->Offset()<<KPageShift; |
|
3293 |
DMemModelChunk* chunk = (DMemModelChunk*)aPageInfo->Owner(); |
|
3294 |
__NK_ASSERT_DEBUG(TUint(offset)<TUint(chunk->iMaxSize)); |
|
3295 |
TLinAddr lin = ((TLinAddr)chunk->iBase)+offset; |
|
3296 |
||
3297 |
// unmap it... |
|
3298 |
TInt asid = ((DMemModelProcess*)chunk->iOwningProcess)->iOsAsid; |
|
3299 |
TPte* pt = PtePtrFromLinAddr(lin,asid); |
|
3300 |
*pt = KPteNotPresentEntry; |
|
3301 |
CacheMaintenance::SinglePteUpdated((TLinAddr)pt); |
|
3302 |
||
3303 |
InvalidateTLBForPage(lin,asid); |
|
3304 |
||
3305 |
// actually decommit it from chunk... |
|
3306 |
TInt ptid = ((TLinAddr)pt-KPageTableBase)>>KPageTableShift; |
|
3307 |
SPageTableInfo& ptinfo=Mmu().iPtInfo[ptid]; |
|
3308 |
if(!--ptinfo.iCount) |
|
3309 |
{ |
|
3310 |
chunk->iPageTables[offset>>KChunkShift] = 0xffff; |
|
3311 |
NKern::UnlockSystem(); |
|
3312 |
Mmu().DoUnassignPageTable(lin, (TAny*)asid); |
|
3313 |
Mmu().FreePageTable(ptid); |
|
3314 |
NKern::LockSystem(); |
|
3315 |
} |
|
3316 |
||
3317 |
#ifdef BTRACE_PAGING |
|
3318 |
BTraceContext8(BTrace::EPaging,BTrace::EPagingPageOutCache,phys,lin); |
|
3319 |
#endif |
|
3320 |
} |
|
3321 |
else if(type==SPageInfo::EPagedFree) |
|
3322 |
{ |
|
3323 |
// already free... |
|
3324 |
#ifdef BTRACE_PAGING |
|
3325 |
BTraceContext4(BTrace::EPaging,BTrace::EPagingPageOutFree,phys); |
|
3326 |
#endif |
|
3327 |
// fall through to cache purge code because cache may not have been |
|
3328 |
// cleaned for this page if PageUnmapped called |
|
3329 |
} |
|
3330 |
else |
|
3331 |
{ |
|
3332 |
__KTRACE_OPT2(KPAGING,KPANIC,Kern::Printf("DP: SetFree() with bad page type = %d",aPageInfo->Type())); |
|
3333 |
Panic(EUnexpectedPageType); |
|
3334 |
return; |
|
3335 |
} |
|
3336 |
||
3337 |
NKern::UnlockSystem(); |
|
3338 |
||
3339 |
// purge cache for page... |
|
3340 |
TInt colour = aPageInfo->Offset()&KPageColourMask; |
|
3341 |
TPte& pte=iPurgePte[colour]; |
|
3342 |
TLinAddr va=iPurgeAddr+(colour<<KPageShift); |
|
3343 |
pte=phys|SP_PTE(KArmV6PermRWNO, TheMmu.iCacheMaintenanceTempMapAttr, 1, 1); |
|
3344 |
CacheMaintenance::SinglePteUpdated((TLinAddr)&pte); |
|
3345 |
||
3346 |
CacheMaintenance::PageToReuse(va,EMemAttNormalCached, KPhysAddrInvalid); |
|
3347 |
||
3348 |
pte=0; |
|
3349 |
CacheMaintenance::SinglePteUpdated((TLinAddr)&pte); |
|
3350 |
InvalidateTLBForPage(va,KERNEL_MAPPING); |
|
3351 |
||
3352 |
NKern::LockSystem(); |
|
3353 |
} |
|
3354 |
||
3355 |
||
3356 |
void MemModelDemandPaging::NotifyPageFree(TPhysAddr aPage) |
|
3357 |
{ |
|
3358 |
__KTRACE_OPT(KPAGING, Kern::Printf("MemModelDemandPaging::NotifyPageFree %08x", aPage)); |
|
3359 |
__ASSERT_SYSTEM_LOCK; |
|
3360 |
||
3361 |
SPageInfo* pageInfo = SPageInfo::FromPhysAddr(aPage); |
|
3362 |
__ASSERT_DEBUG(pageInfo->Type()==SPageInfo::EPagedCode, MM::Panic(MM::EUnexpectedPageType)); |
|
3363 |
RemovePage(pageInfo); |
|
3364 |
SetFree(pageInfo); |
|
3365 |
AddAsFreePage(pageInfo); |
|
3366 |
} |
|
3367 |
||
3368 |
||
3369 |
TInt MemModelDemandPaging::Fault(TAny* aExceptionInfo) |
|
3370 |
{ |
|
3371 |
TArmExcInfo& exc=*(TArmExcInfo*)aExceptionInfo; |
|
3372 |
||
3373 |
// Get faulting address |
|
3374 |
TLinAddr faultAddress = exc.iFaultAddress; |
|
3375 |
if(exc.iExcCode==EArmExceptionDataAbort) |
|
3376 |
{ |
|
3377 |
// Let writes take an exception rather than page in any memory... |
|
3378 |
if(exc.iFaultStatus&(1<<11)) |
|
3379 |
return KErrUnknown; |
|
3380 |
} |
|
3381 |
else if (exc.iExcCode != EArmExceptionPrefetchAbort) |
|
3382 |
return KErrUnknown; // Not prefetch or data abort |
|
3383 |
||
3384 |
// Only handle page translation faults |
|
3385 |
if((exc.iFaultStatus & 0x40f) != 0x7) |
|
3386 |
return KErrUnknown; |
|
3387 |
||
3388 |
DMemModelThread* thread = (DMemModelThread*)TheCurrentThread; |
|
3389 |
||
3390 |
// check which ragion fault occured in... |
|
3391 |
TInt asid = 0; // asid != 0 => code paging fault |
|
3392 |
if(TUint(faultAddress-iRomPagedLinearBase)<iRomPagedSize) |
|
3393 |
{ |
|
3394 |
// in ROM |
|
3395 |
} |
|
3396 |
else if(TUint(faultAddress-iCodeLinearBase)<iCodeSize) |
|
3397 |
{ |
|
3398 |
// in code |
|
3399 |
asid = ((DMemModelProcess*)TheScheduler.iAddressSpace)->iOsAsid; |
|
3400 |
} |
|
3401 |
else if (thread->iAliasLinAddr && TUint(faultAddress - thread->iAliasLinAddr) < TUint(KPageSize)) |
|
3402 |
{ |
|
3403 |
// in aliased memory |
|
3404 |
faultAddress = (faultAddress - thread->iAliasLinAddr) + thread->iAliasTarget; |
|
3405 |
if(TUint(faultAddress-iCodeLinearBase)>=iCodeSize) |
|
3406 |
return KErrUnknown; // not in alias of code |
|
3407 |
asid = thread->iAliasOsAsid; |
|
3408 |
__NK_ASSERT_DEBUG(asid != 0); |
|
3409 |
} |
|
3410 |
else |
|
3411 |
return KErrUnknown; // Not in pageable region |
|
3412 |
||
3413 |
// Check if thread holds fast mutex and claim system lock |
|
3414 |
NFastMutex* fm = NKern::HeldFastMutex(); |
|
3415 |
TPagingExcTrap* trap = thread->iPagingExcTrap; |
|
3416 |
if(!fm) |
|
3417 |
NKern::LockSystem(); |
|
3418 |
else |
|
3419 |
{ |
|
3420 |
if(!trap || fm!=&TheScheduler.iLock) |
|
3421 |
{ |
|
3422 |
__KTRACE_OPT2(KPAGING,KPANIC,Kern::Printf("DP: Fault with FM Held! %x (%O pc=%x)",faultAddress,&Kern::CurrentThread(),exc.iR15)); |
|
3423 |
Panic(EPageFaultWhilstFMHeld); // Not allowed to hold mutexes |
|
3424 |
} |
|
3425 |
// restore address space on multiple memory model (because the trap will |
|
3426 |
// bypass any code which would have done this.)... |
|
3427 |
DMemModelThread::RestoreAddressSpace(); |
|
3428 |
||
3429 |
// Current thread already has the system lock... |
|
3430 |
NKern::FlashSystem(); // Let someone else have a go with the system lock. |
|
3431 |
} |
|
3432 |
||
3433 |
// System locked here |
|
3434 |
||
3435 |
TInt r = KErrNone; |
|
3436 |
if(thread->IsRealtime()) |
|
3437 |
r = CheckRealtimeThreadFault(thread, aExceptionInfo); |
|
3438 |
if (r == KErrNone) |
|
3439 |
r = HandleFault(exc, faultAddress, asid); |
|
3440 |
||
3441 |
// Restore system lock state |
|
3442 |
if (fm != NKern::HeldFastMutex()) |
|
3443 |
{ |
|
3444 |
if (fm) |
|
3445 |
NKern::LockSystem(); |
|
3446 |
else |
|
3447 |
NKern::UnlockSystem(); |
|
3448 |
} |
|
3449 |
||
3450 |
// Deal with XTRAP_PAGING |
|
3451 |
if(r == KErrNone && trap) |
|
3452 |
{ |
|
3453 |
trap->Exception(1); // Return from exception trap with result '1' (value>0) |
|
3454 |
// code doesn't continue beyond this point. |
|
3455 |
} |
|
3456 |
||
3457 |
return r; |
|
3458 |
} |
|
3459 |
||
3460 |
||
3461 |
||
3462 |
TInt MemModelDemandPaging::HandleFault(TArmExcInfo& aExc, TLinAddr aFaultAddress, TInt aAsid) |
|
3463 |
{ |
|
3464 |
++iEventInfo.iPageFaultCount; |
|
3465 |
||
3466 |
// get page table entry... |
|
3467 |
TPte* pt = SafePtePtrFromLinAddr(aFaultAddress, aAsid); |
|
3468 |
if(!pt) |
|
3469 |
return KErrNotFound; |
|
3470 |
TPte pte = *pt; |
|
3471 |
||
3472 |
// Do what is required to make page accessible... |
|
3473 |
||
3474 |
if(pte&KPtePresentMask) |
|
3475 |
{ |
|
3476 |
// PTE is present, so assume it has already been dealt with |
|
3477 |
#ifdef BTRACE_PAGING |
|
3478 |
BTraceContext12(BTrace::EPaging,BTrace::EPagingPageNop,pte&~KPageMask,aFaultAddress,aExc.iR15); |
|
3479 |
#endif |
|
3480 |
return KErrNone; |
|
3481 |
} |
|
3482 |
||
3483 |
if(pte!=KPteNotPresentEntry) |
|
3484 |
{ |
|
3485 |
// PTE alread has a page |
|
3486 |
SPageInfo* pageInfo = SPageInfo::FromPhysAddr(pte); |
|
3487 |
if(pageInfo->State()==SPageInfo::EStatePagedDead) |
|
3488 |
{ |
|
3489 |
// page currently being unmapped, so do that here... |
|
3490 |
MakePTEInaccessible(pt, KPteNotPresentEntry, aFaultAddress, aAsid); |
|
3491 |
} |
|
3492 |
else |
|
3493 |
{ |
|
3494 |
// page just needs making young again... |
|
3495 |
*pt = TPte(pte|KArmV6PteSmallPage); // Update page table |
|
3496 |
CacheMaintenance::SinglePteUpdated((TLinAddr)pt); |
|
3497 |
Rejuvenate(pageInfo); |
|
3498 |
#ifdef BTRACE_PAGING |
|
3499 |
BTraceContext12(BTrace::EPaging,BTrace::EPagingRejuvenate,pte&~KPageMask,aFaultAddress,aExc.iR15); |
|
3500 |
#endif |
|
3501 |
return KErrNone; |
|
3502 |
} |
|
3503 |
} |
|
3504 |
||
3505 |
// PTE not present, so page it in... |
|
3506 |
// check if fault in a CodeSeg... |
|
3507 |
DMemModelCodeSegMemory* codeSegMemory = NULL; |
|
3508 |
if (!aAsid) |
|
3509 |
NKern::ThreadEnterCS(); |
|
3510 |
else |
|
3511 |
{ |
|
3512 |
// find CodeSeg... |
|
3513 |
DMemModelCodeSeg* codeSeg = (DMemModelCodeSeg*)DCodeSeg::CodeSegsByAddress.Find(aFaultAddress); |
|
3514 |
if (!codeSeg) |
|
3515 |
return KErrNotFound; |
|
3516 |
codeSegMemory = codeSeg->Memory(); |
|
3517 |
if (codeSegMemory==0 || !codeSegMemory->iIsDemandPaged || codeSegMemory->iOsAsids->NotFree(aAsid, 1)) |
|
3518 |
return KErrNotFound; |
|
3519 |
||
3520 |
// check if it's paged in but not yet mapped into this process... |
|
3521 |
TInt pageNumber = (aFaultAddress - codeSegMemory->iRamInfo.iCodeRunAddr) >> KPageShift; |
|
3522 |
TPhysAddr page = codeSegMemory->iPages[pageNumber]; |
|
3523 |
if (page != KPhysAddrInvalid) |
|
3524 |
{ |
|
3525 |
// map it into this process... |
|
3526 |
SPageInfo* pageInfo = SPageInfo::FromPhysAddr(page); |
|
3527 |
__NK_ASSERT_DEBUG(pageInfo->State()!=SPageInfo::EStatePagedDead); |
|
3528 |
*pt = page | (codeSegMemory->iCreator ? KUserCodeLoadPte : KUserCodeRunPte); |
|
3529 |
CacheMaintenance::SinglePteUpdated((TLinAddr)pt); |
|
3530 |
Rejuvenate(pageInfo); |
|
3531 |
#ifdef BTRACE_PAGING |
|
3532 |
BTraceContext8(BTrace::EPaging,BTrace::EPagingMapCode,page,aFaultAddress); |
|
3533 |
#endif |
|
3534 |
return KErrNone; |
|
3535 |
} |
|
3536 |
||
3537 |
// open reference on CodeSegMemory |
|
3538 |
NKern::ThreadEnterCS(); |
|
3539 |
#ifdef _DEBUG |
|
3540 |
TInt r = |
|
3541 |
#endif |
|
3542 |
codeSegMemory->Open(); |
|
3543 |
__NK_ASSERT_DEBUG(r==KErrNone); |
|
3544 |
NKern::FlashSystem(); |
|
3545 |
} |
|
3546 |
||
3547 |
#ifdef BTRACE_PAGING |
|
3548 |
BTraceContext8(BTrace::EPaging,BTrace::EPagingPageInBegin,aFaultAddress,aExc.iR15); |
|
3549 |
#endif |
|
3550 |
TInt r = PageIn(aFaultAddress, aAsid, codeSegMemory); |
|
3551 |
||
3552 |
NKern::UnlockSystem(); |
|
3553 |
||
3554 |
if(codeSegMemory) |
|
3555 |
codeSegMemory->Close(); |
|
3556 |
||
3557 |
NKern::ThreadLeaveCS(); |
|
3558 |
||
3559 |
return r; |
|
3560 |
} |
|
3561 |
||
3562 |
||
3563 |
TInt MemModelDemandPaging::PageIn(TLinAddr aAddress, TInt aAsid, DMemModelCodeSegMemory* aCodeSegMemory) |
|
3564 |
{ |
|
3565 |
// Get a request object - this may block until one is available |
|
3566 |
DPagingRequest* req = AcquireRequestObject(); |
|
3567 |
||
3568 |
// Get page table entry |
|
3569 |
TPte* pt = SafePtePtrFromLinAddr(aAddress, aAsid); |
|
3570 |
||
3571 |
// Check page is still required... |
|
3572 |
if(!pt || *pt!=KPteNotPresentEntry) |
|
3573 |
{ |
|
3574 |
#ifdef BTRACE_PAGING |
|
3575 |
BTraceContext0(BTrace::EPaging,BTrace::EPagingPageInUnneeded); |
|
3576 |
#endif |
|
3577 |
ReleaseRequestObject(req); |
|
3578 |
return pt ? KErrNone : KErrNotFound; |
|
3579 |
} |
|
3580 |
||
3581 |
++iEventInfo.iPageInReadCount; |
|
3582 |
||
3583 |
// Get a free page |
|
3584 |
SPageInfo* pageInfo = AllocateNewPage(); |
|
3585 |
__NK_ASSERT_DEBUG(pageInfo); |
|
3586 |
||
3587 |
// Get physical address of free page |
|
3588 |
TPhysAddr phys = pageInfo->PhysAddr(); |
|
3589 |
__NK_ASSERT_DEBUG(phys!=KPhysAddrInvalid); |
|
3590 |
||
3591 |
// Temporarily map free page |
|
3592 |
TInt colour = (aAddress>>KPageShift)&KPageColourMask; |
|
3593 |
__NK_ASSERT_DEBUG((req->iLoadAddr & (KPageColourMask << KPageShift)) == 0); |
|
3594 |
req->iLoadAddr |= colour << KPageShift; |
|
3595 |
TLinAddr loadAddr = req->iLoadAddr; |
|
3596 |
pt = req->iLoadPte+colour; |
|
3597 |
// *pt = phys | SP_PTE(KArmV6PermRWNO, KArmV6MemAttWTWAWTWA, 0, 1); |
|
3598 |
*pt = phys | SP_PTE(KArmV6PermRWNO, KNormalUncachedAttr, 0, 1); |
|
3599 |
CacheMaintenance::SinglePteUpdated((TLinAddr)pt); |
|
3600 |
||
3601 |
// Read page from backing store |
|
3602 |
aAddress &= ~KPageMask; |
|
3603 |
NKern::UnlockSystem(); |
|
3604 |
||
3605 |
TInt r; |
|
3606 |
if (!aCodeSegMemory) |
|
3607 |
r = ReadRomPage(req, aAddress); |
|
3608 |
else |
|
3609 |
{ |
|
3610 |
r = ReadCodePage(req, aCodeSegMemory, aAddress); |
|
3611 |
if (r == KErrNone) |
|
3612 |
aCodeSegMemory->ApplyCodeFixups((TUint32*)loadAddr, aAddress); |
|
3613 |
} |
|
3614 |
if(r!=KErrNone) |
|
3615 |
Panic(EPageInFailed); |
|
3616 |
||
3617 |
// make caches consistant... |
|
3618 |
// Cache::IMB_Range(loadAddr, KPageSize); |
|
3619 |
*pt = phys | SP_PTE(KArmV6PermRWNO, KNormalCachedAttr, 0, 1); |
|
3620 |
CacheMaintenance::SinglePteUpdated((TLinAddr)pt); |
|
3621 |
InvalidateTLBForPage(loadAddr,KERNEL_MAPPING); |
|
3622 |
CacheMaintenance::CodeChanged(loadAddr, KPageSize, CacheMaintenance::ECPUUncached); |
|
3623 |
||
3624 |
NKern::LockSystem(); |
|
3625 |
||
3626 |
// Invalidate temporary mapping |
|
3627 |
MakeGlobalPTEInaccessible(pt, KPteNotPresentEntry, loadAddr); |
|
3628 |
||
3629 |
// Release request object now we're finished with it |
|
3630 |
req->iLoadAddr &= ~(KPageColourMask << KPageShift); |
|
3631 |
ReleaseRequestObject(req); |
|
3632 |
||
3633 |
// Get page table entry |
|
3634 |
pt = SafePtePtrFromLinAddr(aAddress, aAsid); |
|
3635 |
||
3636 |
// Check page still needs updating |
|
3637 |
TBool notNeeded = pt==0 || *pt!=KPteNotPresentEntry; |
|
3638 |
if(aCodeSegMemory) |
|
3639 |
notNeeded |= aCodeSegMemory->iOsAsids->NotFree(aAsid, 1); |
|
3640 |
if(notNeeded) |
|
3641 |
{ |
|
3642 |
// We don't need the new page after all, so put it on the active list as a free page |
|
3643 |
__KTRACE_OPT(KPAGING,Kern::Printf("DP: PageIn (New page not used)")); |
|
3644 |
#ifdef BTRACE_PAGING |
|
3645 |
BTraceContext0(BTrace::EPaging,BTrace::EPagingPageInUnneeded); |
|
3646 |
#endif |
|
3647 |
AddAsFreePage(pageInfo); |
|
3648 |
return pt ? KErrNone : KErrNotFound; |
|
3649 |
} |
|
3650 |
||
3651 |
// Update page info |
|
3652 |
if (!aCodeSegMemory) |
|
3653 |
pageInfo->SetPagedROM((aAddress-iRomLinearBase)>>KPageShift); |
|
3654 |
else |
|
3655 |
{ |
|
3656 |
// Check if page has been paged in and mapped into another process while we were waiting |
|
3657 |
TInt pageNumber = (aAddress - aCodeSegMemory->iRamInfo.iCodeRunAddr) >> KPageShift; |
|
3658 |
TPhysAddr page = aCodeSegMemory->iPages[pageNumber]; |
|
3659 |
if (page != KPhysAddrInvalid) |
|
3660 |
{ |
|
3661 |
// don't need page we've just paged in... |
|
3662 |
AddAsFreePage(pageInfo); |
|
3663 |
||
3664 |
// map existing page into this process... |
|
3665 |
pageInfo = SPageInfo::FromPhysAddr(page); |
|
3666 |
__NK_ASSERT_DEBUG(pageInfo->State()!=SPageInfo::EStatePagedDead); |
|
3667 |
*pt = page | (aCodeSegMemory->iCreator ? KUserCodeLoadPte : KUserCodeRunPte); |
|
3668 |
CacheMaintenance::SinglePteUpdated((TLinAddr)pt); |
|
3669 |
#ifdef BTRACE_PAGING |
|
3670 |
BTraceContext0(BTrace::EPaging,BTrace::EPagingPageInUnneeded); |
|
3671 |
#endif |
|
3672 |
Rejuvenate(pageInfo); |
|
3673 |
return KErrNone; |
|
3674 |
} |
|
3675 |
aCodeSegMemory->iPages[pageNumber] = phys; |
|
3676 |
||
3677 |
pageInfo->SetPagedCode(aCodeSegMemory,(aAddress-Mmu().iUserCodeBase)>>KPageShift); |
|
3678 |
} |
|
3679 |
||
3680 |
// Map page into final location |
|
3681 |
*pt = phys | (aCodeSegMemory ? (aCodeSegMemory->iCreator ? KUserCodeLoadPte : KUserCodeRunPte) : KRomPtePerm); |
|
3682 |
CacheMaintenance::SinglePteUpdated((TLinAddr)pt); |
|
3683 |
#ifdef BTRACE_PAGING |
|
3684 |
TInt subCat = aCodeSegMemory ? BTrace::EPagingPageInCode : BTrace::EPagingPageInROM; |
|
3685 |
BTraceContext8(BTrace::EPaging,subCat,phys,aAddress); |
|
3686 |
#endif |
|
3687 |
||
3688 |
AddAsYoungest(pageInfo); |
|
3689 |
BalanceAges(); |
|
3690 |
||
3691 |
return KErrNone; |
|
3692 |
} |
|
3693 |
||
3694 |
||
3695 |
inline TUint8 ReadByte(TLinAddr aAddress) |
|
3696 |
{ return *(volatile TUint8*)aAddress; } |
|
3697 |
||
3698 |
||
3699 |
TInt MemModelDemandPaging::EnsurePagePresent(TLinAddr aPage, DProcess* aProcess) |
|
3700 |
{ |
|
3701 |
TInt r = KErrBadDescriptor; |
|
3702 |
XTRAPD(exc,XT_DEFAULT, |
|
3703 |
if (!aProcess) |
|
3704 |
{ |
|
3705 |
XTRAP_PAGING_RETRY(CHECK_PAGING_SAFE; ReadByte(aPage);); |
|
3706 |
r = KErrNone; |
|
3707 |
} |
|
3708 |
else |
|
3709 |
{ |
|
3710 |
DMemModelThread& t=*(DMemModelThread*)TheCurrentThread; |
|
3711 |
retry: |
|
3712 |
TInt pagingFault; |
|
3713 |
XTRAP_PAGING_START(pagingFault); |
|
3714 |
CHECK_PAGING_SAFE; |
|
3715 |
// make alias of page in this process |
|
3716 |
TLinAddr alias_src; |
|
3717 |
TInt alias_size; |
|
3718 |
TInt aliasResult = t.Alias(aPage, (DMemModelProcess*)aProcess, 1, EMapAttrReadUser, alias_src, alias_size); |
|
3719 |
if (aliasResult>=0) |
|
3720 |
{ |
|
3721 |
// ensure page to be locked is mapped in, by reading from it... |
|
3722 |
ReadByte(alias_src); |
|
3723 |
r = KErrNone; |
|
3724 |
} |
|
3725 |
XTRAP_PAGING_END; |
|
3726 |
t.RemoveAlias(); |
|
3727 |
if(pagingFault>0) |
|
3728 |
goto retry; |
|
3729 |
} |
|
3730 |
); // end of XTRAPD |
|
3731 |
if(exc) |
|
3732 |
return KErrBadDescriptor; |
|
3733 |
return r; |
|
3734 |
} |
|
3735 |
||
3736 |
||
3737 |
TPhysAddr MemModelDemandPaging::LinearToPhysical(TLinAddr aPage, DProcess* aProcess) |
|
3738 |
{ |
|
3739 |
TInt asid = 0; |
|
3740 |
if (aProcess) |
|
3741 |
asid = ((DMemModelProcess*)aProcess)->iOsAsid; |
|
3742 |
return Mmu().LinearToPhysical(aPage, asid); |
|
3743 |
} |
|
3744 |
||
3745 |
||
3746 |
TInt MemModelDemandPaging::PageState(TLinAddr aAddr) |
|
3747 |
{ |
|
3748 |
DMemModelProcess* process = (DMemModelProcess*)TheCurrentThread->iOwningProcess; |
|
3749 |
TInt asid = 0; |
|
3750 |
TPte* ptePtr = 0; |
|
3751 |
TPte pte = 0; |
|
3752 |
TInt r = 0; |
|
3753 |
SPageInfo* pageInfo = NULL; |
|
3754 |
||
3755 |
NKern::LockSystem(); |
|
3756 |
||
3757 |
DMemModelCodeSegMemory* codeSegMemory = 0; |
|
3758 |
if(TUint(aAddr-iRomPagedLinearBase)<iRomPagedSize) |
|
3759 |
r |= EPageStateInRom; |
|
3760 |
else if (TUint(aAddr-iCodeLinearBase)<iCodeSize) |
|
3761 |
{ |
|
3762 |
DMemModelCodeSeg* codeSeg = (DMemModelCodeSeg*)DCodeSeg::CodeSegsByAddress.Find(aAddr); |
|
3763 |
if(codeSeg) |
|
3764 |
codeSegMemory = codeSeg->Memory(); |
|
3765 |
asid = process->iOsAsid; |
|
3766 |
if (codeSegMemory && codeSegMemory->iOsAsids->NotAllocated(asid, 1)) |
|
3767 |
{ |
|
3768 |
r |= EPageStateInRamCode; |
|
3769 |
if (codeSegMemory->iIsDemandPaged) |
|
3770 |
r |= EPageStatePaged; |
|
3771 |
} |
|
3772 |
if(process->iCodeChunk) |
|
3773 |
r |= EPageStateCodeChunkPresent; |
|
3774 |
} |
|
3775 |
||
3776 |
ptePtr = SafePtePtrFromLinAddr(aAddr,asid); |
|
3777 |
if (!ptePtr) |
|
3778 |
goto done; |
|
3779 |
r |= EPageStatePageTablePresent; |
|
3780 |
pte = *ptePtr; |
|
3781 |
if (pte == KPteNotPresentEntry) |
|
3782 |
goto done; |
|
3783 |
r |= EPageStatePtePresent; |
|
3784 |
if (pte & KPtePresentMask) |
|
3785 |
r |= EPageStatePteValid; |
|
3786 |
||
3787 |
pageInfo = SPageInfo::FromPhysAddr(pte); |
|
3788 |
r |= pageInfo->Type(); |
|
3789 |
r |= pageInfo->State()<<8; |
|
3790 |
||
3791 |
if (codeSegMemory && codeSegMemory->iPages) |
|
3792 |
{ |
|
3793 |
TPhysAddr phys = pte & ~KPageMask; |
|
3794 |
TInt pageNumber = (aAddr - codeSegMemory->iRamInfo.iCodeRunAddr) >> KPageShift; |
|
3795 |
if (codeSegMemory->iPages[pageNumber] == phys) |
|
3796 |
r |= EPageStatePhysAddrPresent; |
|
3797 |
} |
|
3798 |
||
3799 |
done: |
|
3800 |
NKern::UnlockSystem(); |
|
3801 |
return r; |
|
3802 |
} |
|
3803 |
||
3804 |
||
3805 |
TBool MemModelDemandPaging::NeedsMutexOrderCheck(TLinAddr aStartAddr, TUint aLength) |
|
3806 |
{ |
|
3807 |
// Don't check mutex order for reads from global area, except for the paged part of rom |
|
3808 |
TBool rangeInGlobalArea = aStartAddr >= KRomLinearBase; |
|
3809 |
TBool rangeInPagedRom = iRomPagedLinearBase != 0 && aStartAddr < (iRomLinearBase + iRomSize) && (aStartAddr + aLength) > iRomPagedLinearBase; |
|
3810 |
return !rangeInGlobalArea || rangeInPagedRom; |
|
3811 |
} |
|
3812 |
||
3813 |
||
3814 |
EXPORT_C TBool DDemandPagingLock::Lock(DThread* aThread, TLinAddr aStart, TInt aSize) |
|
3815 |
{ |
|
3816 |
MemModelDemandPaging* pager = (MemModelDemandPaging*)iThePager; |
|
3817 |
if(pager) |
|
3818 |
{ |
|
3819 |
ArmMmu& m = pager->Mmu(); |
|
3820 |
TLinAddr end = aStart+aSize; |
|
3821 |
||
3822 |
if ((aStart < TUint(pager->iRomPagedLinearBase+pager->iRomPagedSize) && end > pager->iRomPagedLinearBase) || |
|
3823 |
(aStart < TUint(m.iUserCodeBase + m.iMaxUserCodeSize) && end > m.iUserCodeBase)) |
|
3824 |
return pager->ReserveLock(aThread,aStart,aSize,*this); |
|
3825 |
} |
|
3826 |
return EFalse; |
|
3827 |
} |
|
3828 |
||
3829 |
void ArmMmu::DisablePageModification(DMemModelChunk* aChunk, TInt aOffset) |
|
3830 |
// |
|
3831 |
// Mark the page at aOffset in aChunk read-only to prevent it being |
|
3832 |
// modified while defrag is in progress. Save the required information |
|
3833 |
// to allow the fault handler to deal with this. |
|
3834 |
// Call this with the system unlocked. |
|
3835 |
// |
|
3836 |
{ |
|
3837 |
__KTRACE_OPT(KMMU,Kern::Printf("ArmMmu::DisablePageModification() offset=%08x", aOffset)); |
|
3838 |
||
3839 |
TInt ptid = aChunk->iPageTables[aOffset>>KChunkShift]; |
|
3840 |
if(ptid == 0xffff) |
|
3841 |
Panic(EDefragDisablePageFailed); |
|
3842 |
||
3843 |
NKern::LockSystem(); |
|
3844 |
TPte* pPte = PageTable(ptid) + ((aOffset&KChunkMask)>>KPageShift); |
|
3845 |
TPte pte = *pPte; |
|
3846 |
if ((pte & KArmV6PteSmallPage) != KArmV6PteSmallPage |
|
3847 |
|| SP_PTE_PERM_GET(pte) != (TUint)KArmV6PermRWRW) |
|
3848 |
Panic(EDefragDisablePageFailed); |
|
3849 |
||
3850 |
iDisabledAddr = (TLinAddr)(aChunk->iBase) + aOffset; |
|
3851 |
if (aChunk->iOwningProcess) |
|
3852 |
iDisabledAddrAsid = ((DMemModelProcess*)(aChunk->iOwningProcess))->iOsAsid; |
|
3853 |
else |
|
3854 |
iDisabledAddrAsid = iDisabledAddr<KRomLinearBase ? UNKNOWN_MAPPING : KERNEL_MAPPING; |
|
3855 |
iDisabledPte = pPte; |
|
3856 |
iDisabledOldVal = pte; |
|
3857 |
||
3858 |
*pPte = SP_PTE_PERM_SET(pte, KArmV6PermRORO); |
|
3859 |
CacheMaintenance::SinglePteUpdated((TLinAddr)pPte); |
|
3860 |
InvalidateTLBForPage(iDisabledAddr, iDisabledAddrAsid); |
|
3861 |
NKern::UnlockSystem(); |
|
3862 |
} |
|
3863 |
||
3864 |
TInt ArmMmu::RamDefragFault(TAny* aExceptionInfo) |
|
3865 |
{ |
|
3866 |
TArmExcInfo& exc=*(TArmExcInfo*)aExceptionInfo; |
|
3867 |
||
3868 |
// Get faulting address |
|
3869 |
TLinAddr faultAddress; |
|
3870 |
if(exc.iExcCode==EArmExceptionDataAbort) |
|
3871 |
{ |
|
3872 |
faultAddress = exc.iFaultAddress; |
|
3873 |
// Defrag can only cause writes to fault on multiple model |
|
3874 |
if(!(exc.iFaultStatus&(1<<11))) |
|
3875 |
return KErrUnknown; |
|
3876 |
} |
|
3877 |
else |
|
3878 |
return KErrUnknown; // Not data abort |
|
3879 |
||
3880 |
// Only handle page permission faults |
|
3881 |
if((exc.iFaultStatus & 0x40f) != 0xf) |
|
3882 |
return KErrUnknown; |
|
3883 |
||
3884 |
DMemModelThread* thread = (DMemModelThread*)TheCurrentThread; |
|
3885 |
TInt asid = ((DMemModelProcess*)TheScheduler.iAddressSpace)->iOsAsid; |
|
3886 |
||
3887 |
TBool aliased = EFalse; |
|
3888 |
if (thread->iAliasLinAddr && TUint(faultAddress - thread->iAliasLinAddr) < TUint(KPageSize)) |
|
3889 |
{ |
|
3890 |
// in aliased memory |
|
3891 |
aliased = ETrue; |
|
3892 |
faultAddress = (faultAddress - thread->iAliasLinAddr) + thread->iAliasTarget; |
|
3893 |
asid = thread->iAliasOsAsid; |
|
3894 |
__NK_ASSERT_DEBUG(asid != 0); |
|
3895 |
} |
|
3896 |
||
3897 |
// Take system lock if not already held |
|
3898 |
NFastMutex* fm = NKern::HeldFastMutex(); |
|
3899 |
if(!fm) |
|
3900 |
NKern::LockSystem(); |
|
3901 |
else if(fm!=&TheScheduler.iLock) |
|
3902 |
{ |
|
3903 |
__KTRACE_OPT2(KMMU,KPANIC,Kern::Printf("Defrag: Fault with FM Held! %x (%O pc=%x)",faultAddress,&Kern::CurrentThread(),exc.iR15)); |
|
3904 |
Panic(EDefragFaultWhilstFMHeld); // Not allowed to hold mutexes |
|
3905 |
} |
|
3906 |
||
3907 |
TInt r = KErrUnknown; |
|
3908 |
||
3909 |
// check if write access to the page has already been restored and retry if so |
|
3910 |
TPte* pt = SafePtePtrFromLinAddr(faultAddress, asid); |
|
3911 |
if(!pt) |
|
3912 |
{ |
|
3913 |
r = KErrNotFound; |
|
3914 |
goto leave; |
|
3915 |
} |
|
3916 |
if (SP_PTE_PERM_GET(*pt) == (TUint)KArmV6PermRWRW) |
|
3917 |
{ |
|
3918 |
r = KErrNone; |
|
3919 |
goto leave; |
|
3920 |
} |
|
3921 |
||
3922 |
// check if the fault occurred in the page we are moving |
|
3923 |
if ( iDisabledPte |
|
3924 |
&& TUint(faultAddress - iDisabledAddr) < TUint(KPageSize) |
|
3925 |
&& (iDisabledAddrAsid < 0 || asid == iDisabledAddrAsid) ) |
|
3926 |
{ |
|
3927 |
// restore access to the page |
|
3928 |
*iDisabledPte = iDisabledOldVal; |
|
3929 |
CacheMaintenance::SinglePteUpdated((TLinAddr)iDisabledPte); |
|
3930 |
InvalidateTLBForPage(iDisabledAddr, iDisabledAddrAsid); |
|
3931 |
if (aliased) |
|
3932 |
InvalidateTLBForPage(exc.iFaultAddress, ((DMemModelProcess*)TheScheduler.iAddressSpace)->iOsAsid); |
|
3933 |
iDisabledAddr = 0; |
|
3934 |
iDisabledAddrAsid = -1; |
|
3935 |
iDisabledPte = NULL; |
|
3936 |
iDisabledOldVal = 0; |
|
3937 |
r = KErrNone; |
|
3938 |
} |
|
3939 |
||
3940 |
leave: |
|
3941 |
// Restore system lock state |
|
3942 |
if (!fm) |
|
3943 |
NKern::UnlockSystem(); |
|
3944 |
||
3945 |
return r; |
|
3946 |
} |