sl@0
|
1 |
// Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
2 |
// All rights reserved.
|
sl@0
|
3 |
// This component and the accompanying materials are made available
|
sl@0
|
4 |
// under the terms of the License "Eclipse Public License v1.0"
|
sl@0
|
5 |
// which accompanies this distribution, and is available
|
sl@0
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
7 |
//
|
sl@0
|
8 |
// Initial Contributors:
|
sl@0
|
9 |
// Nokia Corporation - initial contribution.
|
sl@0
|
10 |
//
|
sl@0
|
11 |
// Contributors:
|
sl@0
|
12 |
//
|
sl@0
|
13 |
// Description:
|
sl@0
|
14 |
// e32\memmodel\epoc\multiple\mdefrag.cpp
|
sl@0
|
15 |
//
|
sl@0
|
16 |
//
|
sl@0
|
17 |
#include <memmodel.h>
|
sl@0
|
18 |
#include <defrag.h>
|
sl@0
|
19 |
#include "mmboot.h"
|
sl@0
|
20 |
#include <ramalloc.h>
|
sl@0
|
21 |
#include "cache_maintenance.h"
|
sl@0
|
22 |
/*
|
sl@0
|
23 |
* Move a kernel page from aOld to aNew, updating the page table in aChunk.
|
sl@0
|
24 |
* Enter with system locked, exit with system unlocked (!!)
|
sl@0
|
25 |
* Must hold RAM alloc mutex.
|
sl@0
|
26 |
*/
|
sl@0
|
27 |
TInt Mmu::MoveKernelPage(DChunk* aChunk, TUint32 aOffset, TPhysAddr aOld, TPhysAddr& aNew, TUint aBlockZoneId, TBool aBlockRest)
|
sl@0
|
28 |
{
|
sl@0
|
29 |
__KTRACE_OPT(KMMU,Kern::Printf("Defrag::MoveKernelPage() off=%08x old=%08x",aOffset,aOld));
|
sl@0
|
30 |
Mmu& m=Mmu::Get();
|
sl@0
|
31 |
|
sl@0
|
32 |
// Release the system lock - the kernel chunks can't ever be freed
|
sl@0
|
33 |
// and the ramalloc mutex protects us from decommit.
|
sl@0
|
34 |
NKern::UnlockSystem();
|
sl@0
|
35 |
|
sl@0
|
36 |
// Allocate new page, map old and new
|
sl@0
|
37 |
TPhysAddr newPage;
|
sl@0
|
38 |
if (m.AllocRamPages(&newPage, 1, EPageMovable, aBlockZoneId, aBlockRest) != KErrNone)
|
sl@0
|
39 |
return KErrNoMemory;
|
sl@0
|
40 |
TLinAddr vOld = m.MapTemp(aOld, aOffset); // enough of address for page colour
|
sl@0
|
41 |
TLinAddr vNew = m.MapSecondTemp(newPage, aOffset);
|
sl@0
|
42 |
|
sl@0
|
43 |
// With interrupts disabled, copy the page's contents and remap its PTE
|
sl@0
|
44 |
// System lock is required as well for Substitute
|
sl@0
|
45 |
NKern::LockSystem();
|
sl@0
|
46 |
TInt irq = NKern::DisableAllInterrupts();
|
sl@0
|
47 |
pagecpy((TAny*)vNew, (TAny*)vOld);
|
sl@0
|
48 |
aChunk->Substitute(aOffset, aOld, newPage);
|
sl@0
|
49 |
NKern::RestoreInterrupts(irq);
|
sl@0
|
50 |
NKern::UnlockSystem();
|
sl@0
|
51 |
|
sl@0
|
52 |
// Before we sort out cache for the old page, check if the required mapping
|
sl@0
|
53 |
// atributes for that operation is what we have at the moment.
|
sl@0
|
54 |
if (CacheMaintenance::TemporaryMapping() != EMemAttNormalCached)
|
sl@0
|
55 |
{
|
sl@0
|
56 |
// Remove temporary mapping and map old page as required by CacheMaintenance
|
sl@0
|
57 |
m.UnmapTemp();
|
sl@0
|
58 |
vOld = m.MapTemp(aOld, aOffset,1, CacheMaintenance::TemporaryMapping());
|
sl@0
|
59 |
}
|
sl@0
|
60 |
|
sl@0
|
61 |
//Sort out cache for the memory not in use anymore.
|
sl@0
|
62 |
CacheMaintenance::PageToReuse(vOld, EMemAttNormalCached, aOld);
|
sl@0
|
63 |
|
sl@0
|
64 |
// Unalias pages
|
sl@0
|
65 |
m.UnmapTemp();
|
sl@0
|
66 |
m.UnmapSecondTemp();
|
sl@0
|
67 |
|
sl@0
|
68 |
// Free old page
|
sl@0
|
69 |
#ifdef _DEBUG
|
sl@0
|
70 |
m.ClearPages(1, (TPhysAddr*)(aOld|1));
|
sl@0
|
71 |
#endif
|
sl@0
|
72 |
m.iRamPageAllocator->FreeRamPage(aOld, EPageMovable);
|
sl@0
|
73 |
|
sl@0
|
74 |
aNew = newPage;
|
sl@0
|
75 |
return KErrNone;
|
sl@0
|
76 |
}
|
sl@0
|
77 |
|
sl@0
|
78 |
/*
|
sl@0
|
79 |
* Move a code page from aOld to aNew, updating all page tables which refer
|
sl@0
|
80 |
* to it.
|
sl@0
|
81 |
* Enter with system locked, exit with system unlocked (!!)
|
sl@0
|
82 |
* Must hold RAM alloc mutex.
|
sl@0
|
83 |
*/
|
sl@0
|
84 |
TInt Mmu::MoveCodeSegMemoryPage(DMemModelCodeSegMemory* aCodeSegMemory, TUint32 aOffset, TPhysAddr aOld,
|
sl@0
|
85 |
TPhysAddr& aNew, TUint aBlockZoneId, TBool aBlockRest)
|
sl@0
|
86 |
{
|
sl@0
|
87 |
__KTRACE_OPT(KMMU,Kern::Printf("Defrag::MoveCodeSegMemoryPage() off=%08x old=%08x",aOffset,aOld));
|
sl@0
|
88 |
Mmu& m=Mmu::Get();
|
sl@0
|
89 |
|
sl@0
|
90 |
// if the code seg is not done loading yet, we can't move it the easy way
|
sl@0
|
91 |
// also, if it's being unloaded the codeseg will have gone.
|
sl@0
|
92 |
DCodeSeg* codeseg = aCodeSegMemory->iCodeSeg;
|
sl@0
|
93 |
if (!codeseg || !(codeseg->iMark & DCodeSeg::EMarkLoaded))
|
sl@0
|
94 |
{
|
sl@0
|
95 |
NKern::UnlockSystem();
|
sl@0
|
96 |
return KErrInUse;
|
sl@0
|
97 |
}
|
sl@0
|
98 |
|
sl@0
|
99 |
// Release system lock as page can't be decommitted while we hold ramalloc mutex
|
sl@0
|
100 |
NKern::UnlockSystem();
|
sl@0
|
101 |
|
sl@0
|
102 |
// Allocate new page, map old and new
|
sl@0
|
103 |
TPhysAddr newPage;
|
sl@0
|
104 |
if (m.AllocRamPages(&newPage, 1, EPageMovable, aBlockZoneId, aBlockRest) != KErrNone)
|
sl@0
|
105 |
return KErrNoMemory;
|
sl@0
|
106 |
TLinAddr vOld = m.MapTemp(aOld, aOffset); // enough of address for page colour
|
sl@0
|
107 |
TLinAddr vNew = m.MapSecondTemp(newPage, aOffset);
|
sl@0
|
108 |
|
sl@0
|
109 |
// Copy the page and remap it wherever it's still mapped
|
sl@0
|
110 |
// Need to clean the new page to get the data to icache
|
sl@0
|
111 |
pagecpy((TAny*)vNew, (TAny*)vOld);
|
sl@0
|
112 |
|
sl@0
|
113 |
//Sort out cache for the code that has just been altered.
|
sl@0
|
114 |
CacheMaintenance::CodeChanged(vNew, KPageSize);
|
sl@0
|
115 |
|
sl@0
|
116 |
//Replace old page in the mapping with the new one.
|
sl@0
|
117 |
aCodeSegMemory->Substitute(aOffset, aOld, newPage);
|
sl@0
|
118 |
|
sl@0
|
119 |
// Before we sort out cache for the old page, check if the required mapping
|
sl@0
|
120 |
// atributes for that operation is what we have at the moment.
|
sl@0
|
121 |
if (CacheMaintenance::TemporaryMapping() != EMemAttNormalCached)
|
sl@0
|
122 |
{
|
sl@0
|
123 |
// Remove temporary mapping and map old page as required by CacheMaintenance
|
sl@0
|
124 |
m.UnmapTemp();
|
sl@0
|
125 |
vOld = m.MapTemp(aOld, aOffset,1, CacheMaintenance::TemporaryMapping());
|
sl@0
|
126 |
}
|
sl@0
|
127 |
|
sl@0
|
128 |
//Sort out cache for the memory not in use anymore.
|
sl@0
|
129 |
CacheMaintenance::PageToReuse(vOld, EMemAttNormalCached, aOld);
|
sl@0
|
130 |
|
sl@0
|
131 |
// Unalias pages
|
sl@0
|
132 |
m.UnmapTemp();
|
sl@0
|
133 |
m.UnmapSecondTemp();
|
sl@0
|
134 |
|
sl@0
|
135 |
// Free old page
|
sl@0
|
136 |
#ifdef _DEBUG
|
sl@0
|
137 |
m.ClearPages(1, (TPhysAddr*)(aOld|1));
|
sl@0
|
138 |
#endif
|
sl@0
|
139 |
m.iRamPageAllocator->FreeRamPage(aOld, EPageMovable);
|
sl@0
|
140 |
|
sl@0
|
141 |
aNew = newPage;
|
sl@0
|
142 |
return KErrNone;
|
sl@0
|
143 |
}
|
sl@0
|
144 |
|
sl@0
|
145 |
/*
|
sl@0
|
146 |
* Move a code chunk page from aOld to aNew, updating the page table in aChunk.
|
sl@0
|
147 |
* Enter with system locked, exit with system unlocked (!!)
|
sl@0
|
148 |
* Must hold RAM alloc mutex.
|
sl@0
|
149 |
*/
|
sl@0
|
150 |
TInt Mmu::MoveCodeChunkPage(DChunk* aChunk, TUint32 aOffset, TPhysAddr aOld, TPhysAddr& aNew, TUint aBlockZoneId, TBool aBlockRest)
|
sl@0
|
151 |
{
|
sl@0
|
152 |
__KTRACE_OPT(KMMU,Kern::Printf("Defrag::MoveCodeChunkPage() off=%08x old=%08x",aOffset,aOld));
|
sl@0
|
153 |
Mmu& m=Mmu::Get();
|
sl@0
|
154 |
|
sl@0
|
155 |
// look up the code seg that corresponds to this page
|
sl@0
|
156 |
TLinAddr aLinearAddress = (TLinAddr)(aChunk->Base() + (aOffset));
|
sl@0
|
157 |
DMemModelCodeSeg* codeseg = (DMemModelCodeSeg*)DCodeSeg::CodeSegsByAddress.Find(aLinearAddress);
|
sl@0
|
158 |
|
sl@0
|
159 |
// if the code seg is not done loading yet, we can't move it the easy way
|
sl@0
|
160 |
if (!(codeseg->iMark & DCodeSeg::EMarkLoaded))
|
sl@0
|
161 |
{
|
sl@0
|
162 |
NKern::UnlockSystem();
|
sl@0
|
163 |
return KErrInUse;
|
sl@0
|
164 |
}
|
sl@0
|
165 |
|
sl@0
|
166 |
// Release system lock as page can't be decommitted while we hold ramalloc mutex
|
sl@0
|
167 |
NKern::UnlockSystem();
|
sl@0
|
168 |
|
sl@0
|
169 |
// Allocate new page, map old and new
|
sl@0
|
170 |
TPhysAddr newPage;
|
sl@0
|
171 |
if (m.AllocRamPages(&newPage, 1, EPageMovable, aBlockZoneId, aBlockRest) != KErrNone)
|
sl@0
|
172 |
return KErrNoMemory;
|
sl@0
|
173 |
TLinAddr vOld = m.MapTemp(aOld, aOffset); // enough of address for page colour
|
sl@0
|
174 |
TLinAddr vNew = m.MapSecondTemp(newPage, aOffset);
|
sl@0
|
175 |
|
sl@0
|
176 |
// Copy the page and remap it
|
sl@0
|
177 |
// Need to clean the new page to get the data to icache
|
sl@0
|
178 |
pagecpy((TAny*)vNew, (TAny*)vOld);
|
sl@0
|
179 |
|
sl@0
|
180 |
//Sort out cache for the code that has just been altered.
|
sl@0
|
181 |
CacheMaintenance::CodeChanged(vNew, KPageSize);
|
sl@0
|
182 |
|
sl@0
|
183 |
NKern::LockSystem();
|
sl@0
|
184 |
aChunk->Substitute(aOffset, aOld, newPage);
|
sl@0
|
185 |
NKern::UnlockSystem();
|
sl@0
|
186 |
|
sl@0
|
187 |
// Before we sort out cache for the old page, check if the required mapping
|
sl@0
|
188 |
// atributes for that operation is what we have at the moment.
|
sl@0
|
189 |
if (CacheMaintenance::TemporaryMapping() != EMemAttNormalCached)
|
sl@0
|
190 |
{
|
sl@0
|
191 |
// Remove temporary mapping and map old page as required by CacheMaintenance
|
sl@0
|
192 |
m.UnmapTemp();
|
sl@0
|
193 |
vOld = m.MapTemp(aOld, aOffset,1, CacheMaintenance::TemporaryMapping());
|
sl@0
|
194 |
}
|
sl@0
|
195 |
|
sl@0
|
196 |
//Sort out cache for the memory not in use anymore.
|
sl@0
|
197 |
CacheMaintenance::PageToReuse(vOld, EMemAttNormalCached, aOld);
|
sl@0
|
198 |
|
sl@0
|
199 |
// Unalias pages
|
sl@0
|
200 |
m.UnmapTemp();
|
sl@0
|
201 |
m.UnmapSecondTemp();
|
sl@0
|
202 |
|
sl@0
|
203 |
// Free old page
|
sl@0
|
204 |
#ifdef _DEBUG
|
sl@0
|
205 |
m.ClearPages(1, (TPhysAddr*)(aOld|1));
|
sl@0
|
206 |
#endif
|
sl@0
|
207 |
m.iRamPageAllocator->FreeRamPage(aOld, EPageMovable);
|
sl@0
|
208 |
|
sl@0
|
209 |
aNew = newPage;
|
sl@0
|
210 |
return KErrNone;
|
sl@0
|
211 |
}
|
sl@0
|
212 |
|
sl@0
|
213 |
/*
|
sl@0
|
214 |
* Move a data chunk page from aOld to aNew, updating the page table in aChunk.
|
sl@0
|
215 |
* Enter with system locked, exit with system unlocked (!!)
|
sl@0
|
216 |
* Must hold RAM alloc mutex.
|
sl@0
|
217 |
*/
|
sl@0
|
218 |
TInt Mmu::MoveDataChunkPage(DChunk* aChunk, TUint32 aOffset, TPhysAddr aOld, TPhysAddr& aNew, TUint aBlockZoneId, TBool aBlockRest)
|
sl@0
|
219 |
{
|
sl@0
|
220 |
__KTRACE_OPT(KMMU,Kern::Printf("Defrag::MoveDataChunkPage() off=%08x old=%08x",aOffset,aOld));
|
sl@0
|
221 |
Mmu& m=Mmu::Get();
|
sl@0
|
222 |
TInt r;
|
sl@0
|
223 |
|
sl@0
|
224 |
// Release system lock as page can't be decommitted while we hold ramalloc mutex
|
sl@0
|
225 |
NKern::UnlockSystem();
|
sl@0
|
226 |
|
sl@0
|
227 |
// Allocate new page, map old and new
|
sl@0
|
228 |
TPhysAddr newPage;
|
sl@0
|
229 |
if (m.AllocRamPages(&newPage, 1, EPageMovable, aBlockZoneId, aBlockRest) != KErrNone)
|
sl@0
|
230 |
return KErrNoMemory;
|
sl@0
|
231 |
TLinAddr vOld = m.MapTemp(aOld, aOffset); // enough of address for page colour
|
sl@0
|
232 |
TLinAddr vNew = m.MapSecondTemp(newPage, aOffset);
|
sl@0
|
233 |
|
sl@0
|
234 |
// Mark the PTE as readonly to avoid the data being overwritten while we copy
|
sl@0
|
235 |
DisablePageModification((DMemModelChunk*)aChunk, aOffset);
|
sl@0
|
236 |
|
sl@0
|
237 |
// Copy the page's contents and remap its PTE
|
sl@0
|
238 |
pagecpy((TAny*)vNew, (TAny*)vOld);
|
sl@0
|
239 |
if (aChunk->iChunkType == EUserSelfModCode)//Sort out cache for the code that has just been altered
|
sl@0
|
240 |
CacheMaintenance::CodeChanged(vNew, KPageSize);
|
sl@0
|
241 |
|
sl@0
|
242 |
NKern::LockSystem();
|
sl@0
|
243 |
if (iDisabledPte != NULL)
|
sl@0
|
244 |
{
|
sl@0
|
245 |
// Access wasn't reenabled, so we can continue
|
sl@0
|
246 |
aChunk->Substitute(aOffset, aOld, newPage);
|
sl@0
|
247 |
iDisabledAddr = 0;
|
sl@0
|
248 |
iDisabledAddrAsid = -1;
|
sl@0
|
249 |
iDisabledPte = NULL;
|
sl@0
|
250 |
iDisabledOldVal = 0;
|
sl@0
|
251 |
r = KErrNone;
|
sl@0
|
252 |
}
|
sl@0
|
253 |
else
|
sl@0
|
254 |
r = KErrInUse;
|
sl@0
|
255 |
NKern::UnlockSystem();
|
sl@0
|
256 |
|
sl@0
|
257 |
|
sl@0
|
258 |
TLinAddr vUnused = vOld;
|
sl@0
|
259 |
TPhysAddr pUnused = aOld;
|
sl@0
|
260 |
|
sl@0
|
261 |
if (r != KErrNone)
|
sl@0
|
262 |
{
|
sl@0
|
263 |
//Substitute has failed. Sort out cache for the new page, not the old one.
|
sl@0
|
264 |
vUnused = vNew;
|
sl@0
|
265 |
pUnused = newPage;
|
sl@0
|
266 |
}
|
sl@0
|
267 |
// Before we sort out cache for the unused page, check if the required mapping
|
sl@0
|
268 |
// atributes for that operation is what we have at the moment.
|
sl@0
|
269 |
if (CacheMaintenance::TemporaryMapping() != EMemAttNormalCached)
|
sl@0
|
270 |
{
|
sl@0
|
271 |
// Remove temporary mapping and map the page as required by CacheMaintenance
|
sl@0
|
272 |
m.UnmapTemp();
|
sl@0
|
273 |
vUnused = m.MapTemp(pUnused, aOffset,1, CacheMaintenance::TemporaryMapping());
|
sl@0
|
274 |
}
|
sl@0
|
275 |
|
sl@0
|
276 |
//Sort out cache for the memory not in use anymore.
|
sl@0
|
277 |
CacheMaintenance::PageToReuse(vUnused, EMemAttNormalCached, pUnused);
|
sl@0
|
278 |
|
sl@0
|
279 |
// Unalias pages
|
sl@0
|
280 |
m.UnmapTemp();
|
sl@0
|
281 |
m.UnmapSecondTemp();
|
sl@0
|
282 |
|
sl@0
|
283 |
if (r == KErrNone)
|
sl@0
|
284 |
{
|
sl@0
|
285 |
// Free old page
|
sl@0
|
286 |
#ifdef _DEBUG
|
sl@0
|
287 |
m.ClearPages(1, (TPhysAddr*)(aOld|1));
|
sl@0
|
288 |
#endif
|
sl@0
|
289 |
m.iRamPageAllocator->FreeRamPage(aOld, EPageMovable);
|
sl@0
|
290 |
aNew = newPage;
|
sl@0
|
291 |
}
|
sl@0
|
292 |
else
|
sl@0
|
293 |
{
|
sl@0
|
294 |
// Free new page
|
sl@0
|
295 |
m.iRamPageAllocator->FreeRamPage(newPage, EPageMovable);
|
sl@0
|
296 |
}
|
sl@0
|
297 |
|
sl@0
|
298 |
return r;
|
sl@0
|
299 |
}
|