author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Sat, 20 Feb 2010 00:10:51 +0200 | |
branch | RCL_3 |
changeset 62 | 4a8fed1c0ef6 |
parent 0 | a41df078684a |
child 87 | 2f92ad2dc5db |
permissions | -rw-r--r-- |
0 | 1 |
// Copyright (c) 2005-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 |
// |
|
15 |
||
16 |
#include "memmodel.h" |
|
17 |
#include "mm.h" |
|
18 |
#include "mmu.h" |
|
19 |
||
20 |
#include "mpager.h" |
|
21 |
#include "mrom.h" |
|
22 |
#include "mobject.h" |
|
23 |
#include "mmapping.h" |
|
24 |
#include "maddressspace.h" |
|
25 |
#include "mmanager.h" |
|
26 |
#include "mptalloc.h" |
|
27 |
#include "mpagearray.h" |
|
28 |
#include "mswap.h" |
|
29 |
#include "mthrash.h" |
|
30 |
#include "cache_maintenance.inl" |
|
31 |
||
32 |
||
33 |
const TUint16 KDefaultYoungOldRatio = 3; |
|
34 |
const TUint16 KDefaultMinPages = 256; |
|
35 |
#ifdef _USE_OLDEST_LISTS |
|
36 |
const TUint16 KDefaultOldOldestRatio = 3; |
|
37 |
#endif |
|
38 |
||
39 |
const TUint KMinOldPages = 1; |
|
40 |
||
41 |
/* On a 32 bit system without PAE can't have more than 2^(32-KPageShift) pages. |
|
42 |
* Subtract 1 so it doesn't overflow when converted to bytes. |
|
43 |
*/ |
|
44 |
const TUint KAbsoluteMaxPageCount = (1u<<(32-KPageShift))-1u; |
|
45 |
||
46 |
||
47 |
||
48 |
DPager ThePager; |
|
49 |
||
50 |
||
51 |
DPager::DPager() |
|
52 |
: iMinimumPageCount(0), iMaximumPageCount(0), iYoungOldRatio(0), |
|
62
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
53 |
iYoungCount(0),iOldCount(0), |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
54 |
#ifdef _USE_OLDEST_LISTS |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
55 |
iOldestCleanCount(0), |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
56 |
#endif |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
57 |
iNumberOfFreePages(0), iReservePageCount(0), iMinimumPageLimit(0) |
0 | 58 |
{ |
59 |
} |
|
60 |
||
61 |
||
62
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
62 |
void DPager::InitCache() |
0 | 63 |
{ |
62
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
64 |
// |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
65 |
// This routine doesn't acquire any mutexes because it should be called before the system |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
66 |
// is fully up and running. I.e. called before another thread can preempt this. |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
67 |
// |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
68 |
TRACEB(("DPager::InitCache()")); |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
69 |
// If any pages have been reserved then they will have already been allocated and |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
70 |
// therefore should be counted as part of iMinimumPageCount. |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
71 |
__NK_ASSERT_DEBUG(iReservePageCount == iMinimumPageCount); |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
72 |
__NK_ASSERT_DEBUG(!CacheInitialised()); |
0 | 73 |
|
74 |
#if defined(__CPU_ARM) |
|
75 |
||
76 |
/** Minimum number of young pages the demand paging live list may have. |
|
77 |
Need at least 4 mapped pages to guarantee to be able to execute all ARM instructions, |
|
78 |
plus enough pages for 4 page tables to map those pages, plus enough pages for the |
|
79 |
page table info structures of those page tables. |
|
80 |
(Worst case is a Thumb-2 STM instruction with both instruction and data straddling chunk |
|
81 |
boundaries.) |
|
82 |
*/ |
|
83 |
iMinYoungPages = 4 // pages |
|
84 |
+(4+KPtClusterSize-1)/KPtClusterSize // page table pages |
|
85 |
+(4+KPageTableInfosPerPage-1)/KPageTableInfosPerPage; // page table info pages |
|
86 |
||
87 |
#elif defined(__CPU_X86) |
|
88 |
||
89 |
/* Need at least 6 mapped pages to guarantee to be able to execute all ARM instructions, |
|
90 |
plus enough pages for 6 page tables to map those pages, plus enough pages for the |
|
91 |
page table info structures of those page tables. |
|
92 |
(Worst case is (?) a MOV [X],[Y] instruction with instruction, 'X' and 'Y' all |
|
93 |
straddling chunk boundaries.) |
|
94 |
*/ |
|
95 |
iMinYoungPages = 6 // pages |
|
96 |
+(6+KPtClusterSize-1)/KPtClusterSize // page table pages |
|
97 |
+(6+KPageTableInfosPerPage-1)/KPageTableInfosPerPage; // page table info pages |
|
98 |
||
99 |
#else |
|
100 |
#error Unknown CPU |
|
101 |
#endif |
|
102 |
||
103 |
#ifdef __SMP__ |
|
104 |
// Adjust min page count so that all CPUs are guaranteed to make progress. |
|
105 |
// NOTE: Can't use NKern::NumberOfCpus here because we haven't finished booting yet and will |
|
106 |
// always have only one CPU running at this point... |
|
107 |
||
108 |
// TODO: Before we can enable this the base test configuration needs |
|
109 |
// updating to have a sufficient minimum page size... |
|
110 |
// |
|
111 |
// iMinYoungPages *= KMaxCpus; |
|
112 |
#endif |
|
113 |
||
114 |
// A minimum young/old ratio of 1 means that we need at least twice iMinYoungPages pages... |
|
115 |
iAbsoluteMinPageCount = 2*iMinYoungPages; |
|
116 |
||
117 |
__NK_ASSERT_DEBUG(KMinOldPages<=iAbsoluteMinPageCount/2); |
|
118 |
||
62
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
119 |
// Read any paging config data. |
0 | 120 |
SDemandPagingConfig config = TheRomHeader().iDemandPagingConfig; |
121 |
||
62
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
122 |
// Set the list ratios... |
0 | 123 |
iYoungOldRatio = KDefaultYoungOldRatio; |
124 |
if(config.iYoungOldRatio) |
|
125 |
iYoungOldRatio = config.iYoungOldRatio; |
|
126 |
#ifdef _USE_OLDEST_LISTS |
|
127 |
iOldOldestRatio = KDefaultOldOldestRatio; |
|
128 |
if(config.iSpare[2]) |
|
129 |
iOldOldestRatio = config.iSpare[2]; |
|
130 |
#endif |
|
131 |
||
62
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
132 |
// Set the minimum page counts... |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
133 |
iMinimumPageLimit = iMinYoungPages * (1 + iYoungOldRatio) / iYoungOldRatio |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
134 |
+ DPageReadRequest::ReservedPagesRequired(); |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
135 |
|
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
136 |
if(iMinimumPageLimit < iAbsoluteMinPageCount) |
0 | 137 |
iMinimumPageLimit = iAbsoluteMinPageCount; |
138 |
||
62
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
139 |
if (K::MemModelAttributes & (EMemModelAttrRomPaging | EMemModelAttrCodePaging | EMemModelAttrDataPaging)) |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
140 |
iMinimumPageCount = KDefaultMinPages; |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
141 |
else |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
142 |
{// No paging is enabled so set the minimum cache size to the minimum |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
143 |
// allowable with the current young old ratio. |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
144 |
iMinimumPageCount = iMinYoungPages * (iYoungOldRatio + 1); |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
145 |
} |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
146 |
|
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
147 |
if(config.iMinPages) |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
148 |
iMinimumPageCount = config.iMinPages; |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
149 |
if(iMinimumPageCount < iAbsoluteMinPageCount) |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
150 |
iMinimumPageCount = iAbsoluteMinPageCount; |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
151 |
if (iMinimumPageLimit + iReservePageCount > iMinimumPageCount) |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
152 |
iMinimumPageCount = iMinimumPageLimit + iReservePageCount; |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
153 |
|
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
154 |
iInitMinimumPageCount = iMinimumPageCount; |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
155 |
|
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
156 |
// Set the maximum page counts... |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
157 |
iMaximumPageCount = KMaxTInt; |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
158 |
if(config.iMaxPages) |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
159 |
iMaximumPageCount = config.iMaxPages; |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
160 |
if (iMaximumPageCount > KAbsoluteMaxPageCount) |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
161 |
iMaximumPageCount = KAbsoluteMaxPageCount; |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
162 |
iInitMaximumPageCount = iMaximumPageCount; |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
163 |
|
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
164 |
|
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
165 |
TRACEB(("DPager::InitCache() live list min=%d max=%d ratio=%d",iMinimumPageCount,iMaximumPageCount,iYoungOldRatio)); |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
166 |
|
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
167 |
// Verify the page counts are valid. |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
168 |
__NK_ASSERT_ALWAYS(iMaximumPageCount >= iMinimumPageCount); |
0 | 169 |
TUint minOldAndOldest = iMinimumPageCount / (1 + iYoungOldRatio); |
62
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
170 |
__NK_ASSERT_ALWAYS(minOldAndOldest >= KMinOldPages); |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
171 |
__NK_ASSERT_ALWAYS(iMinimumPageCount >= minOldAndOldest); |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
172 |
|
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
173 |
// Need at least iMinYoungPages pages mapped to execute worst case CPU instruction |
0 | 174 |
TUint minYoung = iMinimumPageCount - minOldAndOldest; |
62
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
175 |
__NK_ASSERT_ALWAYS(minYoung >= iMinYoungPages); |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
176 |
|
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
177 |
// Verify that the young old ratio can be met even when there is only the |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
178 |
// minimum number of old pages. |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
179 |
TInt ratioLimit = (iMinimumPageCount-KMinOldPages)/KMinOldPages; |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
180 |
__NK_ASSERT_ALWAYS(iYoungOldRatio <= ratioLimit); |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
181 |
|
0 | 182 |
#ifdef _USE_OLDEST_LISTS |
183 |
// There should always be enough old pages to allow the oldest lists ratio. |
|
184 |
TUint oldestCount = minOldAndOldest / (1 + iOldOldestRatio); |
|
62
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
185 |
__NK_ASSERT_ALWAYS(oldestCount); |
0 | 186 |
#endif |
62
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
187 |
|
0 | 188 |
iNumberOfFreePages = 0; |
189 |
iNumberOfDirtyPages = 0; |
|
190 |
||
62
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
191 |
// Allocate RAM pages and put them all on the old list. |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
192 |
// Reserved pages have already been allocated and already placed on the |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
193 |
// old list so don't allocate them again. |
0 | 194 |
RamAllocLock::Lock(); |
195 |
iYoungCount = 0; |
|
62
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
196 |
#ifdef _USE_OLDEST_LISTS |
0 | 197 |
iOldCount = 0; |
198 |
iOldestDirtyCount = 0; |
|
62
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
199 |
__NK_ASSERT_DEBUG(iOldestCleanCount == iReservePageCount); |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
200 |
#else |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
201 |
__NK_ASSERT_DEBUG(iOldCount == iReservePageCount); |
0 | 202 |
#endif |
203 |
Mmu& m = TheMmu; |
|
62
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
204 |
for(TUint i = iReservePageCount; i < iMinimumPageCount; i++) |
0 | 205 |
{ |
206 |
// Allocate a single page |
|
207 |
TPhysAddr pagePhys; |
|
208 |
TInt r = m.AllocRam(&pagePhys, 1, |
|
209 |
(Mmu::TRamAllocFlags)(EMemAttNormalCached|Mmu::EAllocNoWipe|Mmu::EAllocNoPagerReclaim), |
|
210 |
EPageDiscard); |
|
211 |
if(r!=KErrNone) |
|
212 |
__NK_ASSERT_ALWAYS(0); |
|
213 |
MmuLock::Lock(); |
|
214 |
AddAsFreePage(SPageInfo::FromPhysAddr(pagePhys)); |
|
215 |
MmuLock::Unlock(); |
|
216 |
} |
|
217 |
RamAllocLock::Unlock(); |
|
218 |
||
62
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
219 |
__NK_ASSERT_DEBUG(CacheInitialised()); |
0 | 220 |
#ifdef _USE_OLDEST_LISTS |
62
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
221 |
TRACEB(("DPager::InitCache() end with young=%d old=%d oldClean=%d oldDirty=%d min=%d free=%d max=%d",iYoungCount,iOldCount,iOldestCleanCount,iOldestDirtyCount,iMinimumPageCount,iNumberOfFreePages,iMaximumPageCount)); |
0 | 222 |
#else |
62
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
223 |
TRACEB(("DPager::InitCache() end with young=%d old=%d min=%d free=%d max=%d",iYoungCount,iOldCount,iMinimumPageCount,iNumberOfFreePages,iMaximumPageCount)); |
0 | 224 |
#endif |
225 |
} |
|
226 |
||
227 |
||
228 |
#ifdef _DEBUG |
|
229 |
TBool DPager::CheckLists() |
|
230 |
{ |
|
231 |
#if 0 |
|
232 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
233 |
SDblQueLink* head = &iOldList.iA; |
|
234 |
TInt n = iOldCount; |
|
235 |
SDblQueLink* link = head; |
|
236 |
while(n--) |
|
237 |
{ |
|
238 |
link = link->iNext; |
|
239 |
if(link==head) |
|
240 |
return false; |
|
241 |
} |
|
242 |
link = link->iNext; |
|
243 |
if(link!=head) |
|
244 |
return false; |
|
245 |
||
246 |
head = &iYoungList.iA; |
|
247 |
n = iYoungCount; |
|
248 |
link = head; |
|
249 |
while(n--) |
|
250 |
{ |
|
251 |
link = link->iNext; |
|
252 |
if(link==head) |
|
253 |
return false; |
|
254 |
} |
|
255 |
link = link->iNext; |
|
256 |
if(link!=head) |
|
257 |
return false; |
|
258 |
||
259 |
// TRACEP(("DP: y=%d o=%d f=%d",iYoungCount,iOldCount,iNumberOfFreePages)); |
|
260 |
#endif |
|
261 |
// TraceCounts(); |
|
262 |
return true; |
|
263 |
} |
|
264 |
||
265 |
void DPager::TraceCounts() |
|
266 |
{ |
|
267 |
TRACEP(("DP: y=%d o=%d f=%d min=%d max=%d ml=%d res=%d", |
|
268 |
iYoungCount,iOldCount,iNumberOfFreePages,iMinimumPageCount, |
|
269 |
iMaximumPageCount,iMinimumPageLimit,iReservePageCount)); |
|
270 |
} |
|
271 |
||
272 |
#endif |
|
273 |
||
274 |
||
275 |
TBool DPager::HaveTooManyPages() |
|
276 |
{ |
|
277 |
__NK_ASSERT_DEBUG(RamAllocLock::IsHeld()); |
|
278 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
279 |
return iMinimumPageCount+iNumberOfFreePages > iMaximumPageCount; |
|
280 |
} |
|
281 |
||
282 |
||
283 |
TBool DPager::HaveMaximumPages() |
|
284 |
{ |
|
285 |
__NK_ASSERT_DEBUG(RamAllocLock::IsHeld()); |
|
286 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
287 |
return iMinimumPageCount+iNumberOfFreePages >= iMaximumPageCount; |
|
288 |
} |
|
289 |
||
290 |
||
291 |
void DPager::AddAsYoungestPage(SPageInfo* aPageInfo) |
|
292 |
{ |
|
293 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
294 |
__NK_ASSERT_DEBUG(CheckLists()); |
|
295 |
__NK_ASSERT_DEBUG(aPageInfo->PagedState()==SPageInfo::EUnpaged); |
|
296 |
||
297 |
aPageInfo->SetPagedState(SPageInfo::EPagedYoung); |
|
298 |
iYoungList.AddHead(&aPageInfo->iLink); |
|
299 |
++iYoungCount; |
|
300 |
} |
|
301 |
||
302 |
||
303 |
void DPager::AddAsFreePage(SPageInfo* aPageInfo) |
|
304 |
{ |
|
305 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
306 |
__NK_ASSERT_DEBUG(CheckLists()); |
|
307 |
||
308 |
__NK_ASSERT_DEBUG(aPageInfo->PagedState()==SPageInfo::EUnpaged); |
|
309 |
TheMmu.PageFreed(aPageInfo); |
|
310 |
__NK_ASSERT_DEBUG(aPageInfo->PagedState()==SPageInfo::EUnpaged); |
|
311 |
||
312 |
// add as oldest page... |
|
313 |
#ifdef _USE_OLDEST_LISTS |
|
314 |
aPageInfo->SetPagedState(SPageInfo::EPagedOldestClean); |
|
315 |
iOldestCleanList.Add(&aPageInfo->iLink); |
|
316 |
++iOldestCleanCount; |
|
317 |
#else |
|
318 |
aPageInfo->SetPagedState(SPageInfo::EPagedOld); |
|
319 |
iOldList.Add(&aPageInfo->iLink); |
|
320 |
++iOldCount; |
|
321 |
#endif |
|
322 |
||
323 |
Event(EEventPageInFree,aPageInfo); |
|
324 |
} |
|
325 |
||
326 |
||
327 |
TInt DPager::PageFreed(SPageInfo* aPageInfo) |
|
328 |
{ |
|
329 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
330 |
__NK_ASSERT_DEBUG(CheckLists()); |
|
331 |
||
332 |
switch(aPageInfo->PagedState()) |
|
333 |
{ |
|
334 |
case SPageInfo::EUnpaged: |
|
335 |
return KErrNotFound; |
|
336 |
||
337 |
case SPageInfo::EPagedYoung: |
|
338 |
__NK_ASSERT_DEBUG(iYoungCount); |
|
339 |
aPageInfo->iLink.Deque(); |
|
340 |
--iYoungCount; |
|
341 |
break; |
|
342 |
||
343 |
case SPageInfo::EPagedOld: |
|
344 |
__NK_ASSERT_DEBUG(iOldCount); |
|
345 |
aPageInfo->iLink.Deque(); |
|
346 |
--iOldCount; |
|
347 |
break; |
|
348 |
||
349 |
#ifdef _USE_OLDEST_LISTS |
|
350 |
case SPageInfo::EPagedOldestClean: |
|
351 |
__NK_ASSERT_DEBUG(iOldestCleanCount); |
|
352 |
aPageInfo->iLink.Deque(); |
|
353 |
--iOldestCleanCount; |
|
354 |
break; |
|
355 |
||
356 |
case SPageInfo::EPagedOldestDirty: |
|
357 |
__NK_ASSERT_DEBUG(iOldestDirtyCount); |
|
358 |
aPageInfo->iLink.Deque(); |
|
359 |
--iOldestDirtyCount; |
|
360 |
break; |
|
361 |
#endif |
|
362 |
||
363 |
case SPageInfo::EPagedPinned: |
|
364 |
// this can occur if a pinned mapping is being unmapped when memory is decommitted. |
|
365 |
// the decommit will have succeeded because the the mapping no longer vetoes this, |
|
366 |
// however the unpinning hasn't yet got around to changing the page state. |
|
367 |
// When the state change happens the page will be put back on the live list so |
|
368 |
// we don't have to do anything now... |
|
369 |
return KErrNone; |
|
370 |
||
371 |
case SPageInfo::EPagedPinnedMoved: |
|
372 |
// This page was pinned when it was moved but it has not been returned |
|
373 |
// to the free pool yet so make sure it is... |
|
374 |
aPageInfo->SetPagedState(SPageInfo::EUnpaged); // Must be unpaged before returned to free pool. |
|
375 |
return KErrNotFound; |
|
376 |
||
377 |
default: |
|
378 |
__NK_ASSERT_DEBUG(0); |
|
379 |
return KErrNotFound; |
|
380 |
} |
|
381 |
||
382 |
// Update the dirty page count as required... |
|
383 |
if (aPageInfo->IsDirty()) |
|
384 |
SetClean(*aPageInfo); |
|
385 |
||
386 |
// add as oldest page... |
|
387 |
#ifdef _USE_OLDEST_LISTS |
|
388 |
aPageInfo->SetPagedState(SPageInfo::EPagedOldestClean); |
|
389 |
iOldestCleanList.Add(&aPageInfo->iLink); |
|
390 |
++iOldestCleanCount; |
|
391 |
#else |
|
392 |
aPageInfo->SetPagedState(SPageInfo::EPagedOld); |
|
393 |
iOldList.Add(&aPageInfo->iLink); |
|
394 |
++iOldCount; |
|
395 |
#endif |
|
396 |
||
397 |
return KErrNone; |
|
398 |
} |
|
399 |
||
400 |
||
401 |
extern TBool IsPageTableUnpagedRemoveAllowed(SPageInfo* aPageInfo); |
|
402 |
||
403 |
void DPager::RemovePage(SPageInfo* aPageInfo) |
|
404 |
{ |
|
405 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
406 |
__NK_ASSERT_DEBUG(CheckLists()); |
|
407 |
||
408 |
switch(aPageInfo->PagedState()) |
|
409 |
{ |
|
410 |
case SPageInfo::EPagedYoung: |
|
411 |
__NK_ASSERT_DEBUG(iYoungCount); |
|
412 |
aPageInfo->iLink.Deque(); |
|
413 |
--iYoungCount; |
|
414 |
break; |
|
415 |
||
416 |
case SPageInfo::EPagedOld: |
|
417 |
__NK_ASSERT_DEBUG(iOldCount); |
|
418 |
aPageInfo->iLink.Deque(); |
|
419 |
--iOldCount; |
|
420 |
break; |
|
421 |
||
422 |
#ifdef _USE_OLDEST_LISTS |
|
423 |
case SPageInfo::EPagedOldestClean: |
|
424 |
__NK_ASSERT_DEBUG(iOldestCleanCount); |
|
425 |
aPageInfo->iLink.Deque(); |
|
426 |
--iOldestCleanCount; |
|
427 |
break; |
|
428 |
||
429 |
case SPageInfo::EPagedOldestDirty: |
|
430 |
__NK_ASSERT_DEBUG(iOldestDirtyCount); |
|
431 |
aPageInfo->iLink.Deque(); |
|
432 |
--iOldestDirtyCount; |
|
433 |
break; |
|
434 |
#endif |
|
435 |
||
436 |
case SPageInfo::EPagedPinned: |
|
437 |
__NK_ASSERT_DEBUG(0); |
|
438 |
case SPageInfo::EUnpaged: |
|
439 |
#ifdef _DEBUG |
|
440 |
if (!IsPageTableUnpagedRemoveAllowed(aPageInfo)) |
|
441 |
__NK_ASSERT_DEBUG(0); |
|
442 |
break; |
|
443 |
#endif |
|
444 |
default: |
|
445 |
__NK_ASSERT_DEBUG(0); |
|
446 |
return; |
|
447 |
} |
|
448 |
||
449 |
aPageInfo->SetPagedState(SPageInfo::EUnpaged); |
|
450 |
} |
|
451 |
||
452 |
||
453 |
void DPager::ReplacePage(SPageInfo& aOldPageInfo, SPageInfo& aNewPageInfo) |
|
454 |
{ |
|
455 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
456 |
__NK_ASSERT_DEBUG(CheckLists()); |
|
457 |
||
458 |
__NK_ASSERT_DEBUG(aOldPageInfo.PagedState() == aNewPageInfo.PagedState()); |
|
459 |
switch(aOldPageInfo.PagedState()) |
|
460 |
{ |
|
461 |
case SPageInfo::EPagedYoung: |
|
462 |
case SPageInfo::EPagedOld: |
|
463 |
case SPageInfo::EPagedOldestClean: |
|
464 |
case SPageInfo::EPagedOldestDirty: |
|
465 |
{// Update the list links point to the new page. |
|
466 |
__NK_ASSERT_DEBUG(iYoungCount); |
|
467 |
SDblQueLink* prevLink = aOldPageInfo.iLink.iPrev; |
|
468 |
#ifdef _DEBUG |
|
469 |
SDblQueLink* nextLink = aOldPageInfo.iLink.iNext; |
|
470 |
__NK_ASSERT_DEBUG(prevLink == aOldPageInfo.iLink.iPrev); |
|
471 |
__NK_ASSERT_DEBUG(prevLink->iNext == &aOldPageInfo.iLink); |
|
472 |
__NK_ASSERT_DEBUG(nextLink == aOldPageInfo.iLink.iNext); |
|
473 |
__NK_ASSERT_DEBUG(nextLink->iPrev == &aOldPageInfo.iLink); |
|
474 |
#endif |
|
475 |
aOldPageInfo.iLink.Deque(); |
|
476 |
aNewPageInfo.iLink.InsertAfter(prevLink); |
|
477 |
aOldPageInfo.SetPagedState(SPageInfo::EUnpaged); |
|
478 |
#ifdef _DEBUG |
|
479 |
__NK_ASSERT_DEBUG(prevLink == aNewPageInfo.iLink.iPrev); |
|
480 |
__NK_ASSERT_DEBUG(prevLink->iNext == &aNewPageInfo.iLink); |
|
481 |
__NK_ASSERT_DEBUG(nextLink == aNewPageInfo.iLink.iNext); |
|
482 |
__NK_ASSERT_DEBUG(nextLink->iPrev == &aNewPageInfo.iLink); |
|
483 |
#endif |
|
484 |
} |
|
485 |
break; |
|
486 |
case SPageInfo::EPagedPinned: |
|
487 |
// Mark the page as 'pinned moved' so that when the page moving invokes |
|
488 |
// Mmu::FreeRam() it returns this page to the free pool. |
|
489 |
aOldPageInfo.ClearPinCount(); |
|
490 |
aOldPageInfo.SetPagedState(SPageInfo::EPagedPinnedMoved); |
|
491 |
break; |
|
492 |
case SPageInfo::EPagedPinnedMoved: |
|
493 |
// Shouldn't happen as the ram alloc mutex will be held for the |
|
494 |
// entire time the page's is paged state == EPagedPinnedMoved. |
|
495 |
case SPageInfo::EUnpaged: |
|
496 |
// Shouldn't happen as we only move pinned memory and unpinning will |
|
497 |
// atomically add the page to the live list and it can't be removed |
|
498 |
// from the live list without the ram alloc mutex. |
|
499 |
__NK_ASSERT_DEBUG(0); |
|
500 |
break; |
|
501 |
} |
|
502 |
} |
|
503 |
||
504 |
||
505 |
SPageInfo* DPager::StealOldestPage() |
|
506 |
{ |
|
507 |
__NK_ASSERT_DEBUG(RamAllocLock::IsHeld()); |
|
508 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
509 |
||
510 |
for(;;) |
|
511 |
{ |
|
512 |
// find oldest page in list... |
|
513 |
SDblQueLink* link; |
|
514 |
#ifdef _USE_OLDEST_LISTS |
|
515 |
if (iOldestCleanCount) |
|
516 |
{ |
|
517 |
__NK_ASSERT_DEBUG(!iOldestCleanList.IsEmpty()); |
|
518 |
link = iOldestCleanList.Last(); |
|
519 |
} |
|
520 |
else if (iOldestDirtyCount) |
|
521 |
{ |
|
522 |
__NK_ASSERT_DEBUG(!iOldestDirtyList.IsEmpty()); |
|
523 |
link = iOldestDirtyList.Last(); |
|
524 |
} |
|
525 |
else if (iOldCount) |
|
526 |
#else |
|
527 |
if (iOldCount) |
|
528 |
#endif |
|
529 |
{ |
|
530 |
__NK_ASSERT_DEBUG(!iOldList.IsEmpty()); |
|
531 |
link = iOldList.Last(); |
|
532 |
} |
|
533 |
else |
|
534 |
{ |
|
535 |
__NK_ASSERT_DEBUG(iYoungCount); |
|
536 |
__NK_ASSERT_ALWAYS(!iYoungList.IsEmpty()); |
|
537 |
link = iYoungList.Last(); |
|
538 |
} |
|
539 |
SPageInfo* pageInfo = SPageInfo::FromLink(link); |
|
540 |
||
541 |
// steal it from owning object... |
|
542 |
TInt r = StealPage(pageInfo); |
|
543 |
||
544 |
BalanceAges(); |
|
545 |
||
546 |
if(r==KErrNone) |
|
547 |
return pageInfo; // done |
|
548 |
||
549 |
// loop back and try again |
|
550 |
} |
|
551 |
} |
|
552 |
||
553 |
||
554 |
TInt DPager::RestrictPage(SPageInfo* aPageInfo, TRestrictPagesType aRestriction) |
|
555 |
{ |
|
556 |
TRACE(("DPager::RestrictPage(0x%08x,%d)",aPageInfo,aRestriction)); |
|
557 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
558 |
||
559 |
TInt r; |
|
560 |
if(aPageInfo->Type()==SPageInfo::EUnused) |
|
561 |
{ |
|
562 |
// page was unused, so nothing to do... |
|
563 |
r = KErrNone; |
|
564 |
} |
|
565 |
else |
|
566 |
{ |
|
567 |
// get memory object which owns the page... |
|
568 |
__NK_ASSERT_DEBUG(aPageInfo->Type()==SPageInfo::EManaged); |
|
569 |
DMemoryObject* memory = aPageInfo->Owner(); |
|
570 |
memory->Open(); |
|
571 |
||
572 |
// try restricting access to page... |
|
573 |
r = memory->iManager->RestrictPage(memory,aPageInfo,aRestriction); |
|
574 |
__NK_ASSERT_DEBUG(r!=KErrNotSupported); |
|
575 |
||
576 |
// close memory object... |
|
577 |
MmuLock::Unlock(); |
|
578 |
memory->AsyncClose(); |
|
579 |
MmuLock::Lock(); |
|
580 |
} |
|
581 |
||
582 |
TRACE(("DPager::RestrictPage returns %d",r)); |
|
583 |
return r; |
|
584 |
} |
|
585 |
||
586 |
||
587 |
TInt DPager::StealPage(SPageInfo* aPageInfo) |
|
588 |
{ |
|
589 |
TRACE(("DPager::StealPage(0x%08x)",aPageInfo)); |
|
590 |
__NK_ASSERT_DEBUG(RamAllocLock::IsHeld()); |
|
591 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
592 |
||
593 |
__UNLOCK_GUARD_START(MmuLock); |
|
594 |
RemovePage(aPageInfo); |
|
595 |
||
596 |
TInt r; |
|
597 |
if(aPageInfo->Type()==SPageInfo::EUnused) |
|
598 |
{ |
|
599 |
// page was unused, so nothing to do... |
|
600 |
r = KErrNone; |
|
601 |
__UNLOCK_GUARD_END(MmuLock); |
|
602 |
MmuLock::Unlock(); |
|
603 |
} |
|
604 |
else |
|
605 |
{ |
|
606 |
// get memory object which owns the page... |
|
607 |
__NK_ASSERT_DEBUG(aPageInfo->Type()==SPageInfo::EManaged); |
|
608 |
DMemoryObject* memory = aPageInfo->Owner(); |
|
609 |
memory->Open(); |
|
610 |
||
611 |
// try and steal page from memory object... |
|
612 |
__UNLOCK_GUARD_END(MmuLock); // StealPage must be called without releasing the MmuLock |
|
613 |
r = memory->iManager->StealPage(memory,aPageInfo); |
|
614 |
__NK_ASSERT_DEBUG(r!=KErrNotSupported); |
|
615 |
||
616 |
// close memory object... |
|
617 |
MmuLock::Unlock(); |
|
618 |
memory->AsyncClose(); |
|
619 |
} |
|
620 |
||
621 |
MmuLock::Lock(); |
|
622 |
||
623 |
if(r==KErrNone) |
|
624 |
Event(EEventPageOut,aPageInfo); |
|
625 |
||
626 |
TRACE(("DPager::StealPage returns %d",r)); |
|
627 |
return r; |
|
628 |
} |
|
629 |
||
630 |
||
631 |
TInt DPager::DiscardPage(SPageInfo* aOldPageInfo, TUint aBlockZoneId, TBool aBlockRest) |
|
632 |
{ |
|
633 |
__NK_ASSERT_DEBUG(RamAllocLock::IsHeld()); |
|
634 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
635 |
||
636 |
TInt r; |
|
637 |
// If the page is pinned or if the page is dirty and a general defrag is being |
|
638 |
// performed then don't attempt to steal it. |
|
639 |
if (aOldPageInfo->Type() != SPageInfo::EUnused && |
|
640 |
(aOldPageInfo->PagedState() == SPageInfo::EPagedPinned || |
|
641 |
(aBlockRest && aOldPageInfo->IsDirty()))) |
|
642 |
{// The page is pinned or is dirty and this is a general defrag so move the page. |
|
643 |
DMemoryObject* memory = aOldPageInfo->Owner(); |
|
644 |
// Page must be managed if it is pinned or dirty. |
|
645 |
__NK_ASSERT_DEBUG(aOldPageInfo->Type()==SPageInfo::EManaged); |
|
646 |
__NK_ASSERT_DEBUG(memory); |
|
647 |
MmuLock::Unlock(); |
|
648 |
TPhysAddr newAddr; |
|
649 |
return memory->iManager->MovePage(memory, aOldPageInfo, newAddr, aBlockZoneId, aBlockRest); |
|
650 |
} |
|
651 |
||
652 |
if (!iNumberOfFreePages) |
|
653 |
{ |
|
654 |
// Allocate a new page for the live list as it has reached its minimum size. |
|
655 |
MmuLock::Unlock(); |
|
656 |
SPageInfo* newPageInfo = GetPageFromSystem((Mmu::TRamAllocFlags)(EMemAttNormalCached|Mmu::EAllocNoWipe), |
|
657 |
aBlockZoneId, aBlockRest); |
|
658 |
if (!newPageInfo) |
|
659 |
return KErrNoMemory; |
|
660 |
||
661 |
// Re-acquire the mmulock and re-check that the page is not pinned or dirty. |
|
662 |
MmuLock::Lock(); |
|
663 |
if (aOldPageInfo->Type() != SPageInfo::EUnused && |
|
664 |
(aOldPageInfo->PagedState() == SPageInfo::EPagedPinned || |
|
665 |
(aBlockRest && aOldPageInfo->IsDirty()))) |
|
666 |
{// Page is now pinned or dirty so give up as it is inuse. |
|
667 |
ReturnPageToSystem(*newPageInfo); |
|
668 |
MmuLock::Unlock(); |
|
669 |
return KErrInUse; |
|
670 |
} |
|
671 |
||
672 |
// Attempt to steal the page |
|
673 |
r = StealPage(aOldPageInfo); |
|
674 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
675 |
||
676 |
if (r == KErrCompletion) |
|
677 |
{// This was a page table that has been freed but added to the |
|
678 |
// live list as a free page. Remove from live list and continue. |
|
679 |
__NK_ASSERT_DEBUG(!aOldPageInfo->IsDirty()); |
|
680 |
RemovePage(aOldPageInfo); |
|
681 |
r = KErrNone; |
|
682 |
} |
|
683 |
||
684 |
if (r == KErrNone) |
|
685 |
{// Add the new page to the live list as discarding the old page |
|
686 |
// will reduce the live list below the minimum. |
|
687 |
AddAsFreePage(newPageInfo); |
|
688 |
// We've successfully discarded the page so return it to the free pool. |
|
689 |
ReturnPageToSystem(*aOldPageInfo); |
|
690 |
BalanceAges(); |
|
691 |
} |
|
692 |
else |
|
693 |
{ |
|
694 |
// New page not required so just return it to the system. This is safe as |
|
695 |
// iNumberOfFreePages will have this page counted but as it is not on the live list |
|
696 |
// noone else can touch it. |
|
697 |
ReturnPageToSystem(*newPageInfo); |
|
698 |
} |
|
699 |
} |
|
700 |
else |
|
701 |
{ |
|
702 |
// Attempt to steal the page |
|
703 |
r = StealPage(aOldPageInfo); |
|
704 |
||
705 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
706 |
||
707 |
if (r == KErrCompletion) |
|
708 |
{// This was a page table that has been freed but added to the |
|
709 |
// live list as a free page. Remove from live list. |
|
710 |
__NK_ASSERT_DEBUG(!aOldPageInfo->IsDirty()); |
|
711 |
RemovePage(aOldPageInfo); |
|
712 |
r = KErrNone; |
|
713 |
} |
|
714 |
||
715 |
if (r == KErrNone) |
|
716 |
{// We've successfully discarded the page so return it to the free pool. |
|
717 |
ReturnPageToSystem(*aOldPageInfo); |
|
718 |
BalanceAges(); |
|
719 |
} |
|
720 |
} |
|
721 |
MmuLock::Unlock(); |
|
722 |
return r; |
|
723 |
} |
|
724 |
||
725 |
||
726 |
TBool DPager::TryGrowLiveList() |
|
727 |
{ |
|
728 |
__NK_ASSERT_DEBUG(RamAllocLock::IsHeld()); |
|
729 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
730 |
||
731 |
MmuLock::Unlock(); |
|
732 |
SPageInfo* sparePage = GetPageFromSystem((Mmu::TRamAllocFlags)(EMemAttNormalCached|Mmu::EAllocNoWipe)); |
|
733 |
MmuLock::Lock(); |
|
734 |
||
735 |
if(!sparePage) |
|
736 |
return false; |
|
737 |
||
738 |
// add page to live list... |
|
739 |
AddAsFreePage(sparePage); |
|
740 |
return true; |
|
741 |
} |
|
742 |
||
743 |
||
744 |
SPageInfo* DPager::GetPageFromSystem(Mmu::TRamAllocFlags aAllocFlags, TUint aBlockZoneId, TBool aBlockRest) |
|
745 |
{ |
|
746 |
__NK_ASSERT_DEBUG(RamAllocLock::IsHeld()); |
|
747 |
||
748 |
TPhysAddr pagePhys; |
|
749 |
TInt r = TheMmu.AllocRam(&pagePhys, 1, |
|
750 |
(Mmu::TRamAllocFlags)(aAllocFlags|Mmu::EAllocNoPagerReclaim), |
|
751 |
EPageDiscard, aBlockZoneId, aBlockRest); |
|
752 |
if(r!=KErrNone) |
|
753 |
return NULL; |
|
754 |
||
755 |
MmuLock::Lock(); |
|
756 |
++iNumberOfFreePages; |
|
757 |
MmuLock::Unlock(); |
|
758 |
||
759 |
return SPageInfo::FromPhysAddr(pagePhys); |
|
760 |
} |
|
761 |
||
762 |
||
763 |
void DPager::ReturnPageToSystem() |
|
764 |
{ |
|
765 |
__NK_ASSERT_DEBUG(RamAllocLock::IsHeld()); |
|
766 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
767 |
||
768 |
ReturnPageToSystem(*StealOldestPage()); |
|
769 |
} |
|
770 |
||
771 |
||
772 |
void DPager::ReturnPageToSystem(SPageInfo& aPageInfo) |
|
773 |
{ |
|
774 |
__NK_ASSERT_DEBUG(RamAllocLock::IsHeld()); |
|
775 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
776 |
||
777 |
__NK_ASSERT_DEBUG(iNumberOfFreePages>0); |
|
778 |
--iNumberOfFreePages; |
|
779 |
||
780 |
MmuLock::Unlock(); |
|
781 |
||
782 |
TPhysAddr pagePhys = aPageInfo.PhysAddr(); |
|
783 |
TheMmu.FreeRam(&pagePhys, 1, EPageDiscard); |
|
784 |
||
785 |
MmuLock::Lock(); |
|
786 |
} |
|
787 |
||
788 |
||
789 |
SPageInfo* DPager::PageInAllocPage(Mmu::TRamAllocFlags aAllocFlags) |
|
790 |
{ |
|
791 |
SPageInfo* pageInfo; |
|
792 |
TPhysAddr pagePhys; |
|
793 |
||
794 |
RamAllocLock::Lock(); |
|
795 |
MmuLock::Lock(); |
|
796 |
||
797 |
// try getting a free page from our live list... |
|
798 |
#ifdef _USE_OLDEST_LISTS |
|
799 |
if (iOldestCleanCount) |
|
800 |
{ |
|
801 |
pageInfo = SPageInfo::FromLink(iOldestCleanList.Last()); |
|
802 |
if(pageInfo->Type()==SPageInfo::EUnused) |
|
803 |
goto get_oldest; |
|
804 |
} |
|
805 |
#else |
|
806 |
if(iOldCount) |
|
807 |
{ |
|
808 |
pageInfo = SPageInfo::FromLink(iOldList.Last()); |
|
809 |
if(pageInfo->Type()==SPageInfo::EUnused) |
|
810 |
goto get_oldest; |
|
811 |
} |
|
812 |
#endif |
|
813 |
||
814 |
// try getting a free page from the system pool... |
|
815 |
if(!HaveMaximumPages()) |
|
816 |
{ |
|
817 |
MmuLock::Unlock(); |
|
818 |
pageInfo = GetPageFromSystem(aAllocFlags); |
|
819 |
if(pageInfo) |
|
820 |
goto done; |
|
821 |
MmuLock::Lock(); |
|
822 |
} |
|
823 |
||
824 |
// as a last resort, steal a page from the live list... |
|
825 |
get_oldest: |
|
826 |
#ifdef _USE_OLDEST_LISTS |
|
827 |
__NK_ASSERT_ALWAYS(iOldestCleanCount|iOldestDirtyCount|iOldCount|iYoungCount); |
|
828 |
#else |
|
829 |
__NK_ASSERT_ALWAYS(iOldCount|iYoungCount); |
|
830 |
#endif |
|
831 |
pageInfo = StealOldestPage(); |
|
832 |
MmuLock::Unlock(); |
|
833 |
||
834 |
// make page state same as a freshly allocated page... |
|
835 |
pagePhys = pageInfo->PhysAddr(); |
|
836 |
TheMmu.PagesAllocated(&pagePhys,1,aAllocFlags); |
|
837 |
||
838 |
done: |
|
839 |
RamAllocLock::Unlock(); |
|
840 |
return pageInfo; |
|
841 |
} |
|
842 |
||
843 |
||
844 |
TBool DPager::GetFreePages(TInt aNumPages) |
|
845 |
{ |
|
846 |
TRACE(("DPager::GetFreePages(%d)",aNumPages)); |
|
847 |
||
848 |
__NK_ASSERT_DEBUG(RamAllocLock::IsHeld()); |
|
849 |
||
850 |
MmuLock::Lock(); |
|
851 |
while(aNumPages>0 && (TInt)NumberOfFreePages()>=aNumPages) |
|
852 |
{ |
|
853 |
ReturnPageToSystem(); |
|
854 |
--aNumPages; |
|
855 |
} |
|
856 |
MmuLock::Unlock(); |
|
857 |
||
858 |
TRACE(("DPager::GetFreePages returns %d",!aNumPages)); |
|
859 |
return !aNumPages; |
|
860 |
} |
|
861 |
||
862 |
||
863 |
void DPager::DonatePages(TUint aCount, TPhysAddr* aPages) |
|
864 |
{ |
|
865 |
TRACE(("DPager::DonatePages(%d,?)",aCount)); |
|
866 |
__ASSERT_CRITICAL; |
|
867 |
RamAllocLock::Lock(); |
|
868 |
MmuLock::Lock(); |
|
869 |
||
870 |
TPhysAddr* end = aPages+aCount; |
|
871 |
while(aPages<end) |
|
872 |
{ |
|
873 |
TPhysAddr pagePhys = *aPages++; |
|
874 |
if(RPageArray::State(pagePhys)!=RPageArray::ECommitted) |
|
875 |
continue; // page is not present |
|
876 |
||
877 |
#ifdef _DEBUG |
|
878 |
SPageInfo* pi = SPageInfo::SafeFromPhysAddr(pagePhys&~KPageMask); |
|
879 |
__NK_ASSERT_DEBUG(pi); |
|
880 |
#else |
|
881 |
SPageInfo* pi = SPageInfo::FromPhysAddr(pagePhys); |
|
882 |
#endif |
|
883 |
switch(pi->PagedState()) |
|
884 |
{ |
|
885 |
case SPageInfo::EUnpaged: |
|
886 |
// Change the type of this page to discardable and |
|
887 |
// then add it to live list. |
|
888 |
// Only the DDiscardableMemoryManager should be invoking this and |
|
889 |
// its pages will be movable before they are donated. |
|
890 |
__NK_ASSERT_DEBUG(pi->Owner()->iManager->PageType() == EPageMovable); |
|
891 |
TheMmu.ChangePageType(pi, EPageMovable, EPageDiscard); |
|
892 |
break; |
|
893 |
||
894 |
case SPageInfo::EPagedYoung: |
|
895 |
case SPageInfo::EPagedOld: |
|
896 |
#ifdef _USE_OLDEST_LISTS |
|
897 |
case SPageInfo::EPagedOldestDirty: |
|
898 |
case SPageInfo::EPagedOldestClean: |
|
899 |
#endif |
|
900 |
continue; // discard already been allowed |
|
901 |
||
902 |
case SPageInfo::EPagedPinned: |
|
903 |
__NK_ASSERT_DEBUG(0); |
|
904 |
default: |
|
905 |
__NK_ASSERT_DEBUG(0); |
|
906 |
continue; |
|
907 |
} |
|
908 |
||
909 |
// put page on live list... |
|
910 |
AddAsYoungestPage(pi); |
|
911 |
++iNumberOfFreePages; |
|
912 |
||
913 |
Event(EEventPageDonate,pi); |
|
914 |
||
915 |
// re-balance live list... |
|
916 |
RemoveExcessPages(); |
|
917 |
BalanceAges(); |
|
918 |
} |
|
919 |
||
920 |
MmuLock::Unlock(); |
|
921 |
RamAllocLock::Unlock(); |
|
922 |
} |
|
923 |
||
924 |
||
925 |
TInt DPager::ReclaimPages(TUint aCount, TPhysAddr* aPages) |
|
926 |
{ |
|
927 |
TRACE(("DPager::ReclaimPages(%d,?)",aCount)); |
|
928 |
__ASSERT_CRITICAL; |
|
929 |
RamAllocLock::Lock(); |
|
930 |
MmuLock::Lock(); |
|
931 |
||
932 |
TInt r = KErrNone; |
|
933 |
TPhysAddr* end = aPages+aCount; |
|
934 |
while(aPages<end) |
|
935 |
{ |
|
936 |
TPhysAddr pagePhys = *aPages++; |
|
937 |
TBool changeType = EFalse; |
|
938 |
||
939 |
if(RPageArray::State(pagePhys)!=RPageArray::ECommitted) |
|
940 |
{ |
|
941 |
r = KErrNotFound; // too late, page has gone |
|
942 |
continue; |
|
943 |
} |
|
944 |
||
945 |
#ifdef _DEBUG |
|
946 |
SPageInfo* pi = SPageInfo::SafeFromPhysAddr(pagePhys&~KPageMask); |
|
947 |
__NK_ASSERT_DEBUG(pi); |
|
948 |
#else |
|
949 |
SPageInfo* pi = SPageInfo::FromPhysAddr(pagePhys); |
|
950 |
#endif |
|
951 |
switch(pi->PagedState()) |
|
952 |
{ |
|
953 |
case SPageInfo::EUnpaged: |
|
954 |
continue; // discard already been disallowed |
|
955 |
||
956 |
case SPageInfo::EPagedYoung: |
|
957 |
case SPageInfo::EPagedOld: |
|
958 |
#ifdef _USE_OLDEST_LISTS |
|
959 |
case SPageInfo::EPagedOldestClean: |
|
960 |
case SPageInfo::EPagedOldestDirty: |
|
961 |
#endif |
|
962 |
changeType = ETrue; |
|
963 |
break; // remove from live list |
|
964 |
||
965 |
case SPageInfo::EPagedPinned: |
|
966 |
__NK_ASSERT_DEBUG(0); |
|
967 |
default: |
|
968 |
__NK_ASSERT_DEBUG(0); |
|
969 |
break; |
|
970 |
} |
|
971 |
||
972 |
// check paging list has enough pages before we remove one... |
|
973 |
if(iNumberOfFreePages<1) |
|
974 |
{ |
|
975 |
// need more pages so get a page from the system... |
|
976 |
if(!TryGrowLiveList()) |
|
977 |
{ |
|
978 |
// out of memory... |
|
979 |
r = KErrNoMemory; |
|
980 |
break; |
|
981 |
} |
|
982 |
// retry the page reclaim... |
|
983 |
--aPages; |
|
984 |
continue; |
|
985 |
} |
|
986 |
||
987 |
if (changeType) |
|
988 |
{// Change the type of this page to movable, wait until any retries |
|
989 |
// have been attempted as we can't change a page's type twice. |
|
990 |
// Only the DDiscardableMemoryManager should be invoking this and |
|
991 |
// its pages should be movable once they are reclaimed. |
|
992 |
__NK_ASSERT_DEBUG(pi->Owner()->iManager->PageType() == EPageMovable); |
|
993 |
TheMmu.ChangePageType(pi, EPageDiscard, EPageMovable); |
|
994 |
} |
|
995 |
||
996 |
// remove page from paging list... |
|
997 |
__NK_ASSERT_DEBUG(iNumberOfFreePages>0); |
|
998 |
--iNumberOfFreePages; |
|
999 |
RemovePage(pi); |
|
1000 |
||
1001 |
Event(EEventPageReclaim,pi); |
|
1002 |
||
1003 |
// re-balance live list... |
|
1004 |
BalanceAges(); |
|
1005 |
} |
|
1006 |
||
1007 |
// we may have added a spare free page to the live list without removing one, |
|
1008 |
// this could cause us to have too many pages, so deal with this... |
|
1009 |
RemoveExcessPages(); |
|
1010 |
||
1011 |
MmuLock::Unlock(); |
|
1012 |
RamAllocLock::Unlock(); |
|
1013 |
return r; |
|
1014 |
} |
|
1015 |
||
1016 |
||
1017 |
TInt VMHalFunction(TAny*, TInt aFunction, TAny* a1, TAny* a2); |
|
1018 |
||
1019 |
void DPager::Init3() |
|
1020 |
{ |
|
1021 |
TRACEB(("DPager::Init3()")); |
|
1022 |
TheRomMemoryManager->Init3(); |
|
1023 |
TheDataPagedMemoryManager->Init3(); |
|
1024 |
TheCodePagedMemoryManager->Init3(); |
|
1025 |
TInt r = Kern::AddHalEntry(EHalGroupVM, VMHalFunction, 0); |
|
1026 |
__NK_ASSERT_ALWAYS(r==KErrNone); |
|
1027 |
} |
|
1028 |
||
1029 |
||
1030 |
void DPager::Fault(TFault aFault) |
|
1031 |
{ |
|
1032 |
Kern::Fault("DPager",aFault); |
|
1033 |
} |
|
1034 |
||
1035 |
||
1036 |
void DPager::BalanceAges() |
|
1037 |
{ |
|
1038 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
1039 |
TBool restrictPage = EFalse; |
|
1040 |
SPageInfo* pageInfo = NULL; |
|
1041 |
#ifdef _USE_OLDEST_LISTS |
|
1042 |
TUint oldestCount = iOldestCleanCount + iOldestDirtyCount; |
|
1043 |
if((iOldCount + oldestCount) * iYoungOldRatio < iYoungCount) |
|
1044 |
#else |
|
1045 |
if (iOldCount * iYoungOldRatio < iYoungCount) |
|
1046 |
#endif |
|
1047 |
{ |
|
1048 |
// Need more old pages so make one young page into an old page... |
|
1049 |
__NK_ASSERT_DEBUG(!iYoungList.IsEmpty()); |
|
1050 |
__NK_ASSERT_DEBUG(iYoungCount); |
|
1051 |
SDblQueLink* link = iYoungList.Last()->Deque(); |
|
1052 |
--iYoungCount; |
|
1053 |
||
1054 |
pageInfo = SPageInfo::FromLink(link); |
|
1055 |
pageInfo->SetPagedState(SPageInfo::EPagedOld); |
|
1056 |
||
1057 |
iOldList.AddHead(link); |
|
1058 |
++iOldCount; |
|
1059 |
||
1060 |
Event(EEventPageAged,pageInfo); |
|
1061 |
// Delay restricting the page until it is safe to release the MmuLock. |
|
1062 |
restrictPage = ETrue; |
|
1063 |
} |
|
1064 |
||
1065 |
#ifdef _USE_OLDEST_LISTS |
|
1066 |
// Check we have enough oldest pages. |
|
1067 |
if (oldestCount * iOldOldestRatio < iOldCount) |
|
1068 |
{ |
|
1069 |
__NK_ASSERT_DEBUG(!iOldList.IsEmpty()); |
|
1070 |
__NK_ASSERT_DEBUG(iOldCount); |
|
1071 |
SDblQueLink* link = iOldList.Last()->Deque(); |
|
1072 |
--iOldCount; |
|
1073 |
||
1074 |
SPageInfo* oldestPageInfo = SPageInfo::FromLink(link); |
|
1075 |
if (oldestPageInfo->IsDirty()) |
|
1076 |
{ |
|
1077 |
oldestPageInfo->SetPagedState(SPageInfo::EPagedOldestDirty); |
|
1078 |
iOldestDirtyList.AddHead(link); |
|
1079 |
++iOldestDirtyCount; |
|
1080 |
Event(EEventPageAgedDirty,oldestPageInfo); |
|
1081 |
} |
|
1082 |
else |
|
1083 |
{ |
|
1084 |
oldestPageInfo->SetPagedState(SPageInfo::EPagedOldestClean); |
|
1085 |
iOldestCleanList.AddHead(link); |
|
1086 |
++iOldestCleanCount; |
|
1087 |
Event(EEventPageAgedClean,oldestPageInfo); |
|
1088 |
} |
|
1089 |
} |
|
1090 |
#endif |
|
1091 |
if (restrictPage) |
|
1092 |
{ |
|
1093 |
// Make the recently aged old page inaccessible. This is done last as it |
|
1094 |
// will release the MmuLock and therefore the page counts may otherwise change. |
|
1095 |
RestrictPage(pageInfo,ERestrictPagesNoAccessForOldPage); |
|
1096 |
} |
|
1097 |
} |
|
1098 |
||
1099 |
||
1100 |
void DPager::RemoveExcessPages() |
|
1101 |
{ |
|
1102 |
__NK_ASSERT_DEBUG(RamAllocLock::IsHeld()); |
|
1103 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
1104 |
while(HaveTooManyPages()) |
|
1105 |
ReturnPageToSystem(); |
|
1106 |
} |
|
1107 |
||
1108 |
||
1109 |
void DPager::RejuvenatePageTable(TPte* aPt) |
|
1110 |
{ |
|
1111 |
SPageInfo* pi = SPageInfo::FromPhysAddr(Mmu::PageTablePhysAddr(aPt)); |
|
1112 |
||
1113 |
SPageTableInfo* pti = SPageTableInfo::FromPtPtr(aPt); |
|
1114 |
if(!pti->IsDemandPaged()) |
|
1115 |
{ |
|
1116 |
__NK_ASSERT_DEBUG(pi->PagedState()==SPageInfo::EUnpaged); |
|
1117 |
return; |
|
1118 |
} |
|
1119 |
||
1120 |
TRACE2(("DP: %O Rejuvenate PT 0x%08x 0x%08x",TheCurrentThread,pi->PhysAddr(),aPt)); |
|
1121 |
switch(pi->PagedState()) |
|
1122 |
{ |
|
1123 |
case SPageInfo::EPagedYoung: |
|
1124 |
case SPageInfo::EPagedOld: |
|
1125 |
#ifdef _USE_OLDEST_LISTS |
|
1126 |
case SPageInfo::EPagedOldestClean: |
|
1127 |
case SPageInfo::EPagedOldestDirty: |
|
1128 |
#endif |
|
1129 |
RemovePage(pi); |
|
1130 |
AddAsYoungestPage(pi); |
|
1131 |
BalanceAges(); |
|
1132 |
break; |
|
1133 |
||
1134 |
case SPageInfo::EUnpaged: |
|
1135 |
AddAsYoungestPage(pi); |
|
1136 |
BalanceAges(); |
|
1137 |
break; |
|
1138 |
||
1139 |
case SPageInfo::EPagedPinned: |
|
1140 |
break; |
|
1141 |
||
1142 |
default: |
|
1143 |
__NK_ASSERT_DEBUG(0); |
|
1144 |
break; |
|
1145 |
} |
|
1146 |
} |
|
1147 |
||
1148 |
TInt DPager::PteAndInfoFromLinAddr( TInt aOsAsid, TLinAddr aAddress, DMemoryMappingBase* aMapping, |
|
1149 |
TUint aMapInstanceCount, TPte*& aPte, SPageInfo*& aPageInfo) |
|
1150 |
{ |
|
1151 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
1152 |
||
1153 |
// Verify the mapping is still mapped and has not been reused. |
|
1154 |
if (aMapInstanceCount != aMapping->MapInstanceCount() || aMapping->BeingDetached()) |
|
1155 |
return KErrAbort; |
|
1156 |
||
1157 |
aPte = Mmu::SafePtePtrFromLinAddr(aAddress,aOsAsid); |
|
1158 |
if(!aPte) |
|
1159 |
return KErrNotFound; |
|
1160 |
||
1161 |
TPte pte = *aPte; |
|
1162 |
if(pte==KPteUnallocatedEntry) |
|
1163 |
return KErrNotFound; |
|
1164 |
||
1165 |
SPageInfo* pi = SPageInfo::SafeFromPhysAddr(pte & ~KPageMask); |
|
1166 |
if(!pi) |
|
1167 |
return KErrNotFound; |
|
1168 |
aPageInfo = pi; |
|
1169 |
||
1170 |
return KErrNone; |
|
1171 |
} |
|
1172 |
||
1173 |
TInt DPager::TryRejuvenate( TInt aOsAsid, TLinAddr aAddress, TUint aAccessPermissions, TLinAddr aPc, |
|
1174 |
DMemoryMappingBase* aMapping, TUint aMapInstanceCount, DThread* aThread, |
|
1175 |
TAny* aExceptionInfo) |
|
1176 |
{ |
|
1177 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
1178 |
||
1179 |
SPageInfo* pi; |
|
1180 |
TPte* pPte; |
|
1181 |
TPte pte; |
|
1182 |
TInt r = PteAndInfoFromLinAddr(aOsAsid, aAddress, aMapping, aMapInstanceCount, pPte, pi); |
|
1183 |
if (r != KErrNone) |
|
1184 |
{ |
|
1185 |
if (aThread->IsRealtime()) |
|
1186 |
{// This thread is real time so it shouldn't be accessing paged out paged memory |
|
1187 |
// unless there is a paging trap. |
|
1188 |
MmuLock::Unlock(); |
|
1189 |
// Ensure that we abort when the thread is not allowed to access paged out pages. |
|
1190 |
if (CheckRealtimeThreadFault(aThread, aExceptionInfo) != KErrNone) |
|
1191 |
r = KErrAbort; |
|
1192 |
MmuLock::Lock(); |
|
1193 |
} |
|
1194 |
return r; |
|
1195 |
} |
|
1196 |
pte = *pPte; |
|
1197 |
SPageInfo::TType type = pi->Type(); |
|
1198 |
SPageInfo::TPagedState state = pi->PagedState(); |
|
1199 |
||
1200 |
if (aThread->IsRealtime() && |
|
1201 |
state != SPageInfo::EPagedPinned && |
|
1202 |
state != SPageInfo::EPagedPinnedMoved) |
|
1203 |
{// This thread is real time so it shouldn't be accessing unpinned paged memory |
|
1204 |
// unless there is a paging trap. |
|
1205 |
MmuLock::Unlock(); |
|
1206 |
r = CheckRealtimeThreadFault(aThread, aExceptionInfo); |
|
1207 |
MmuLock::Lock(); |
|
1208 |
if (r != KErrNone) |
|
1209 |
return r; |
|
1210 |
// We had to release the MmuLock have to reverify the status of the page and mappings. |
|
1211 |
r = PteAndInfoFromLinAddr(aOsAsid, aAddress, aMapping, aMapInstanceCount, pPte, pi); |
|
1212 |
if (r != KErrNone) |
|
1213 |
return r; |
|
1214 |
pte = *pPte; |
|
1215 |
type = pi->Type(); |
|
1216 |
state = pi->PagedState(); |
|
1217 |
} |
|
1218 |
||
1219 |
if (type != SPageInfo::EManaged) |
|
1220 |
return KErrNotFound; |
|
1221 |
||
1222 |
if(state==SPageInfo::EUnpaged) |
|
1223 |
return KErrNotFound; |
|
1224 |
||
1225 |
DMemoryObject* memory = pi->Owner(); |
|
1226 |
TUint index = pi->Index(); |
|
1227 |
||
1228 |
TPhysAddr page = memory->iPages.Page(index); |
|
1229 |
if(!RPageArray::IsPresent(page)) |
|
1230 |
return KErrNotFound; |
|
1231 |
||
1232 |
TPhysAddr physAddr = pi->PhysAddr(); |
|
1233 |
if ((page^physAddr) >= (TPhysAddr)KPageSize) |
|
1234 |
{// Page array entry should contain same physical address as PTE unless the |
|
1235 |
// page has or is being moved and this mapping accessed the page. |
|
1236 |
// Get the page info for the page that we should be using. |
|
1237 |
physAddr = page & ~KPageMask; |
|
1238 |
pi = SPageInfo::SafeFromPhysAddr(physAddr); |
|
1239 |
if(!pi) |
|
1240 |
return KErrNotFound; |
|
1241 |
||
1242 |
type = pi->Type(); |
|
1243 |
if (type!=SPageInfo::EManaged) |
|
1244 |
return KErrNotFound; |
|
1245 |
||
1246 |
state = pi->PagedState(); |
|
1247 |
if(state==SPageInfo::EUnpaged) |
|
1248 |
return KErrNotFound; |
|
1249 |
||
1250 |
memory = pi->Owner(); |
|
1251 |
index = pi->Index(); |
|
1252 |
||
1253 |
// Update pte to point to the correct physical address for this memory object's page. |
|
1254 |
pte = (pte & KPageMask) | physAddr; |
|
1255 |
} |
|
1256 |
||
1257 |
if(aAccessPermissions&EReadWrite) |
|
1258 |
{// The mapping that took the fault permits writes and is still attached |
|
1259 |
// to the memory object therefore the object can't be read only. |
|
1260 |
__NK_ASSERT_DEBUG(!memory->IsReadOnly()); |
|
1261 |
SetWritable(*pi); |
|
1262 |
} |
|
1263 |
||
1264 |
pte = Mmu::MakePteAccessible(pte,aAccessPermissions&EReadWrite); |
|
1265 |
TRACE2(("!PTE %x=%x",pPte,pte)); |
|
1266 |
*pPte = pte; |
|
1267 |
CacheMaintenance::SinglePteUpdated((TLinAddr)pPte); |
|
1268 |
InvalidateTLBForPage((aAddress&~KPageMask)|aOsAsid); |
|
1269 |
||
1270 |
Event(EEventPageRejuvenate,pi,aPc,aAddress,aAccessPermissions); |
|
1271 |
||
1272 |
TBool balance = false; |
|
1273 |
#ifdef _USE_OLDEST_LISTS |
|
1274 |
if( state==SPageInfo::EPagedYoung || state==SPageInfo::EPagedOld || |
|
1275 |
state==SPageInfo::EPagedOldestClean || state==SPageInfo::EPagedOldestDirty) |
|
1276 |
#else |
|
1277 |
if(state==SPageInfo::EPagedYoung || state==SPageInfo::EPagedOld) |
|
1278 |
#endif |
|
1279 |
{ |
|
1280 |
RemovePage(pi); |
|
1281 |
AddAsYoungestPage(pi); |
|
1282 |
// delay BalanceAges because we don't want to release MmuLock until after |
|
1283 |
// RejuvenatePageTable has chance to look at the page table page... |
|
1284 |
balance = true; |
|
1285 |
} |
|
1286 |
else |
|
1287 |
{// Clear the modifier so that if this page is being moved then this |
|
1288 |
// access is detected. For non-pinned pages the modifier is cleared |
|
1289 |
// by RemovePage(). |
|
1290 |
__NK_ASSERT_DEBUG(state==SPageInfo::EPagedPinned); |
|
1291 |
pi->SetModifier(0); |
|
1292 |
} |
|
1293 |
||
1294 |
RejuvenatePageTable(pPte); |
|
1295 |
||
1296 |
if(balance) |
|
1297 |
BalanceAges(); |
|
1298 |
||
1299 |
return KErrNone; |
|
1300 |
} |
|
1301 |
||
1302 |
||
1303 |
TInt DPager::PageInAllocPages(TPhysAddr* aPages, TUint aCount, Mmu::TRamAllocFlags aAllocFlags) |
|
1304 |
{ |
|
1305 |
TUint n = 0; |
|
1306 |
while(n<aCount) |
|
1307 |
{ |
|
1308 |
SPageInfo* pi = PageInAllocPage(aAllocFlags); |
|
1309 |
if(!pi) |
|
1310 |
goto fail; |
|
1311 |
aPages[n++] = pi->PhysAddr(); |
|
1312 |
} |
|
1313 |
return KErrNone; |
|
1314 |
fail: |
|
1315 |
PageInFreePages(aPages,n); |
|
1316 |
return KErrNoMemory; |
|
1317 |
} |
|
1318 |
||
1319 |
||
1320 |
void DPager::PageInFreePages(TPhysAddr* aPages, TUint aCount) |
|
1321 |
{ |
|
1322 |
while(aCount--) |
|
1323 |
{ |
|
1324 |
MmuLock::Lock(); |
|
1325 |
SPageInfo* pi = SPageInfo::FromPhysAddr(aPages[aCount]); |
|
1326 |
switch(pi->PagedState()) |
|
1327 |
{ |
|
1328 |
case SPageInfo::EPagedYoung: |
|
1329 |
case SPageInfo::EPagedOld: |
|
1330 |
#ifdef _USE_OLDEST_LISTS |
|
1331 |
case SPageInfo::EPagedOldestClean: |
|
1332 |
case SPageInfo::EPagedOldestDirty: |
|
1333 |
#endif |
|
1334 |
RemovePage(pi); |
|
1335 |
// fall through... |
|
1336 |
case SPageInfo::EUnpaged: |
|
1337 |
AddAsFreePage(pi); |
|
1338 |
break; |
|
1339 |
||
1340 |
case SPageInfo::EPagedPinned: |
|
1341 |
__NK_ASSERT_DEBUG(0); |
|
1342 |
break; |
|
1343 |
default: |
|
1344 |
__NK_ASSERT_DEBUG(0); |
|
1345 |
break; |
|
1346 |
} |
|
1347 |
MmuLock::Unlock(); |
|
1348 |
} |
|
1349 |
} |
|
1350 |
||
1351 |
||
1352 |
void DPager::PagedInUnneeded(SPageInfo* aPageInfo) |
|
1353 |
{ |
|
1354 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
1355 |
Event(EEventPageInUnneeded,aPageInfo); |
|
1356 |
AddAsFreePage(aPageInfo); |
|
1357 |
} |
|
1358 |
||
1359 |
||
1360 |
void DPager::PagedIn(SPageInfo* aPageInfo) |
|
1361 |
{ |
|
1362 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
1363 |
switch(aPageInfo->PagedState()) |
|
1364 |
{ |
|
1365 |
case SPageInfo::EPagedYoung: |
|
1366 |
case SPageInfo::EPagedOld: |
|
1367 |
#ifdef _USE_OLDEST_LISTS |
|
1368 |
case SPageInfo::EPagedOldestClean: |
|
1369 |
case SPageInfo::EPagedOldestDirty: |
|
1370 |
#endif |
|
1371 |
RemovePage(aPageInfo); |
|
1372 |
AddAsYoungestPage(aPageInfo); |
|
1373 |
BalanceAges(); |
|
1374 |
break; |
|
1375 |
||
1376 |
case SPageInfo::EUnpaged: |
|
1377 |
AddAsYoungestPage(aPageInfo); |
|
1378 |
BalanceAges(); |
|
1379 |
break; |
|
1380 |
||
1381 |
case SPageInfo::EPagedPinned: |
|
1382 |
// Clear the modifier so that if this page is being moved then this |
|
1383 |
// access is detected. For non-pinned pages the modifier is cleared by RemovePage(). |
|
1384 |
aPageInfo->SetModifier(0); |
|
1385 |
break; |
|
1386 |
||
1387 |
default: |
|
1388 |
__NK_ASSERT_DEBUG(0); |
|
1389 |
break; |
|
1390 |
} |
|
1391 |
} |
|
1392 |
||
1393 |
||
1394 |
void DPager::PagedInPinned(SPageInfo* aPageInfo, TPinArgs& aPinArgs) |
|
1395 |
{ |
|
1396 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
1397 |
Pin(aPageInfo,aPinArgs); |
|
1398 |
} |
|
1399 |
||
1400 |
||
1401 |
void DPager::Pin(SPageInfo* aPageInfo, TPinArgs& aPinArgs) |
|
1402 |
{ |
|
1403 |
__ASSERT_CRITICAL; |
|
1404 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
1405 |
__NK_ASSERT_DEBUG(aPinArgs.HaveSufficientPages(1)); |
|
1406 |
||
1407 |
aPageInfo->IncPinCount(); |
|
1408 |
Event(EEventPagePin,aPageInfo); |
|
1409 |
||
1410 |
// remove page from live list... |
|
1411 |
switch(aPageInfo->PagedState()) |
|
1412 |
{ |
|
1413 |
case SPageInfo::EPagedYoung: |
|
1414 |
__NK_ASSERT_DEBUG(iYoungCount); |
|
1415 |
aPageInfo->iLink.Deque(); |
|
1416 |
--iYoungCount; |
|
1417 |
__NK_ASSERT_DEBUG(aPageInfo->PinCount()==1); |
|
1418 |
break; |
|
1419 |
||
1420 |
case SPageInfo::EPagedOld: |
|
1421 |
__NK_ASSERT_DEBUG(iOldCount); |
|
1422 |
aPageInfo->iLink.Deque(); |
|
1423 |
--iOldCount; |
|
1424 |
__NK_ASSERT_DEBUG(aPageInfo->PinCount()==1); |
|
1425 |
break; |
|
1426 |
||
1427 |
#ifdef _USE_OLDEST_LISTS |
|
1428 |
case SPageInfo::EPagedOldestClean: |
|
1429 |
__NK_ASSERT_DEBUG(iOldestCleanCount); |
|
1430 |
aPageInfo->iLink.Deque(); |
|
1431 |
--iOldestCleanCount; |
|
1432 |
__NK_ASSERT_DEBUG(aPageInfo->PinCount()==1); |
|
1433 |
break; |
|
1434 |
||
1435 |
case SPageInfo::EPagedOldestDirty: |
|
1436 |
__NK_ASSERT_DEBUG(iOldestDirtyCount); |
|
1437 |
aPageInfo->iLink.Deque(); |
|
1438 |
--iOldestDirtyCount; |
|
1439 |
__NK_ASSERT_DEBUG(aPageInfo->PinCount()==1); |
|
1440 |
break; |
|
1441 |
#endif |
|
1442 |
||
1443 |
case SPageInfo::EPagedPinned: |
|
1444 |
// nothing more to do... |
|
1445 |
__NK_ASSERT_DEBUG(aPageInfo->PinCount()>1); |
|
1446 |
return; |
|
1447 |
||
1448 |
case SPageInfo::EUnpaged: |
|
1449 |
__NK_ASSERT_DEBUG(aPageInfo->PinCount()==1); |
|
1450 |
TRACE2(("DPager::PinPage page was unpaged")); |
|
1451 |
// This could be a page in the process of being stolen. |
|
1452 |
// Could also be page for storing page table infos, which aren't necessarily |
|
1453 |
// on the live list. |
|
1454 |
break; |
|
1455 |
||
1456 |
default: |
|
1457 |
__NK_ASSERT_DEBUG(0); |
|
1458 |
return; |
|
1459 |
} |
|
1460 |
||
1461 |
// page has now been removed from the live list and is pinned... |
|
1462 |
aPageInfo->SetPagedState(SPageInfo::EPagedPinned); |
|
1463 |
||
1464 |
if(aPinArgs.iReplacementPages==TPinArgs::EUseReserveForPinReplacementPages) |
|
1465 |
{ |
|
1466 |
// pinned paged counts as coming from reserve pool... |
|
1467 |
aPageInfo->SetPinnedReserve(); |
|
1468 |
} |
|
1469 |
else |
|
1470 |
{ |
|
1471 |
// we used up a replacement page... |
|
1472 |
--aPinArgs.iReplacementPages; |
|
1473 |
} |
|
1474 |
||
1475 |
BalanceAges(); |
|
1476 |
} |
|
1477 |
||
1478 |
||
1479 |
void DPager::Unpin(SPageInfo* aPageInfo, TPinArgs& aPinArgs) |
|
1480 |
{ |
|
1481 |
__ASSERT_CRITICAL; |
|
1482 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
1483 |
__NK_ASSERT_DEBUG(aPageInfo->PagedState()==SPageInfo::EPagedPinned); |
|
1484 |
__NK_ASSERT_DEBUG(aPageInfo->PinCount()>0); |
|
1485 |
||
1486 |
TUint pinCount = aPageInfo->DecPinCount(); |
|
1487 |
Event(EEventPageUnpin,aPageInfo); |
|
1488 |
||
1489 |
if(pinCount) |
|
1490 |
return; |
|
1491 |
||
1492 |
aPageInfo->SetPagedState(SPageInfo::EUnpaged); |
|
1493 |
||
1494 |
if(!aPageInfo->ClearPinnedReserve()) |
|
1495 |
{ |
|
1496 |
// was not a pinned reserve page, so we how have a spare replacement page, |
|
1497 |
// which can be used again or freed later ... |
|
1498 |
__NK_ASSERT_DEBUG(aPinArgs.iReplacementPages!=TPinArgs::EUseReserveForPinReplacementPages); |
|
1499 |
++aPinArgs.iReplacementPages; |
|
1500 |
} |
|
1501 |
||
1502 |
AddAsYoungestPage(aPageInfo); |
|
1503 |
BalanceAges(); |
|
1504 |
} |
|
1505 |
||
1506 |
||
1507 |
TInt TPinArgs::AllocReplacementPages(TUint aNumPages) |
|
1508 |
{ |
|
1509 |
if(iUseReserve) |
|
1510 |
{ |
|
1511 |
__NK_ASSERT_DEBUG(iReplacementPages==0 || iReplacementPages==EUseReserveForPinReplacementPages); |
|
1512 |
iReplacementPages = EUseReserveForPinReplacementPages; |
|
1513 |
} |
|
1514 |
else |
|
1515 |
{ |
|
1516 |
if(aNumPages>iReplacementPages) |
|
1517 |
{ |
|
1518 |
if(!ThePager.AllocPinReplacementPages(aNumPages-iReplacementPages)) |
|
1519 |
return KErrNoMemory; |
|
1520 |
iReplacementPages = aNumPages; |
|
1521 |
} |
|
1522 |
} |
|
1523 |
return KErrNone; |
|
1524 |
} |
|
1525 |
||
1526 |
||
1527 |
void TPinArgs::FreeReplacementPages() |
|
1528 |
{ |
|
1529 |
if(iReplacementPages!=0 && iReplacementPages!=EUseReserveForPinReplacementPages) |
|
1530 |
ThePager.FreePinReplacementPages(iReplacementPages); |
|
1531 |
iReplacementPages = 0; |
|
1532 |
} |
|
1533 |
||
1534 |
||
1535 |
TBool DPager::AllocPinReplacementPages(TUint aNumPages) |
|
1536 |
{ |
|
1537 |
TRACE2(("DPager::AllocPinReplacementPages(0x%x)",aNumPages)); |
|
1538 |
__ASSERT_CRITICAL; |
|
1539 |
RamAllocLock::Lock(); |
|
1540 |
MmuLock::Lock(); |
|
1541 |
||
1542 |
TBool ok = false; |
|
1543 |
do |
|
1544 |
{ |
|
1545 |
if(iNumberOfFreePages>=aNumPages) |
|
1546 |
{ |
|
1547 |
iNumberOfFreePages -= aNumPages; |
|
1548 |
ok = true; |
|
1549 |
break; |
|
1550 |
} |
|
1551 |
} |
|
1552 |
while(TryGrowLiveList()); |
|
1553 |
||
1554 |
MmuLock::Unlock(); |
|
1555 |
RamAllocLock::Unlock(); |
|
1556 |
return ok; |
|
1557 |
} |
|
1558 |
||
1559 |
||
1560 |
void DPager::FreePinReplacementPages(TUint aNumPages) |
|
1561 |
{ |
|
1562 |
TRACE2(("DPager::FreePinReplacementPage(0x%x)",aNumPages)); |
|
1563 |
__ASSERT_CRITICAL; |
|
1564 |
||
1565 |
RamAllocLock::Lock(); |
|
1566 |
MmuLock::Lock(); |
|
1567 |
||
1568 |
iNumberOfFreePages += aNumPages; |
|
1569 |
RemoveExcessPages(); |
|
1570 |
||
1571 |
MmuLock::Unlock(); |
|
1572 |
RamAllocLock::Unlock(); |
|
1573 |
} |
|
1574 |
||
1575 |
||
1576 |
TBool DPager::ReservePage() |
|
1577 |
{ |
|
1578 |
__NK_ASSERT_DEBUG(RamAllocLock::IsHeld()); |
|
1579 |
__NK_ASSERT_DEBUG(MmuLock::IsHeld()); |
|
1580 |
__ASSERT_CRITICAL; |
|
1581 |
__NK_ASSERT_DEBUG(iMinimumPageCount >= iMinimumPageLimit+iReservePageCount); |
|
1582 |
while(iMinimumPageCount==iMinimumPageLimit+iReservePageCount && iNumberOfFreePages==0) |
|
1583 |
{ |
|
1584 |
if(!TryGrowLiveList()) |
|
1585 |
return false; |
|
1586 |
} |
|
1587 |
if(iMinimumPageCount==iMinimumPageLimit+iReservePageCount) |
|
1588 |
{ |
|
1589 |
++iMinimumPageCount; |
|
1590 |
--iNumberOfFreePages; |
|
1591 |
if(iMinimumPageCount>iMaximumPageCount) |
|
1592 |
iMaximumPageCount = iMinimumPageCount; |
|
1593 |
} |
|
1594 |
++iReservePageCount; |
|
1595 |
__NK_ASSERT_DEBUG(iMinimumPageCount >= iMinimumPageLimit+iReservePageCount); |
|
1596 |
__NK_ASSERT_DEBUG(iMinimumPageCount+iNumberOfFreePages <= iMaximumPageCount); |
|
1597 |
return ETrue; |
|
1598 |
} |
|
1599 |
||
1600 |
||
1601 |
TBool DPager::ReservePages(TUint aRequiredCount, TUint& aCount) |
|
1602 |
{ |
|
1603 |
__ASSERT_CRITICAL; |
|
1604 |
||
1605 |
RamAllocLock::Lock(); |
|
1606 |
MmuLock::Lock(); |
|
1607 |
while(aCount<aRequiredCount) |
|
1608 |
{ |
|
1609 |
if(!ReservePage()) |
|
1610 |
break; |
|
1611 |
++aCount; |
|
1612 |
MmuLock::Flash(); |
|
1613 |
} |
|
1614 |
TBool enoughPages = aCount==aRequiredCount; |
|
1615 |
MmuLock::Unlock(); |
|
1616 |
RamAllocLock::Unlock(); |
|
1617 |
||
1618 |
if(!enoughPages) |
|
1619 |
UnreservePages(aCount); |
|
1620 |
||
1621 |
return enoughPages; |
|
1622 |
} |
|
1623 |
||
1624 |
||
1625 |
void DPager::UnreservePages(TUint& aCount) |
|
1626 |
{ |
|
1627 |
MmuLock::Lock(); |
|
1628 |
iReservePageCount -= aCount; |
|
1629 |
aCount = 0; |
|
1630 |
MmuLock::Unlock(); |
|
1631 |
} |
|
1632 |
||
1633 |
||
1634 |
TInt DPager::CheckRealtimeThreadFault(DThread* aThread, TAny* aExceptionInfo) |
|
1635 |
{ |
|
1636 |
// realtime threads shouldn't take paging faults... |
|
1637 |
DThread* client = aThread->iIpcClient; |
|
1638 |
||
1639 |
// If iIpcClient is set then we are accessing the address space of a remote thread. If we are |
|
1640 |
// in an IPC trap, this will contain information the local and remote addresses being accessed. |
|
1641 |
// If this is not set then we assume than any fault must be the fault of a bad remote address. |
|
1642 |
TIpcExcTrap* ipcTrap = (TIpcExcTrap*)aThread->iExcTrap; |
|
1643 |
if (ipcTrap && !ipcTrap->IsTIpcExcTrap()) |
|
1644 |
ipcTrap = 0; |
|
1645 |
if (client && (!ipcTrap || ipcTrap->ExcLocation(aThread, aExceptionInfo) == TIpcExcTrap::EExcRemote)) |
|
1646 |
{ |
|
1647 |
// kill client thread... |
|
1648 |
if(K::IllegalFunctionForRealtimeThread(client,"Access to Paged Memory (by other thread)")) |
|
1649 |
{ |
|
1650 |
// treat memory access as bad... |
|
1651 |
return KErrAbort; |
|
1652 |
} |
|
1653 |
// else thread is in 'warning only' state so allow paging... |
|
1654 |
} |
|
1655 |
else |
|
1656 |
{ |
|
1657 |
// kill current thread... |
|
1658 |
if(K::IllegalFunctionForRealtimeThread(NULL,"Access to Paged Memory")) |
|
1659 |
{ |
|
1660 |
// if current thread is in critical section, then the above kill will be deferred |
|
1661 |
// and we will continue executing. We will handle this by returning an error |
|
1662 |
// which means that the thread will take an exception (which hopefully is XTRAPed!) |
|
1663 |
return KErrAbort; |
|
1664 |
} |
|
1665 |
// else thread is in 'warning only' state so allow paging... |
|
1666 |
} |
|
1667 |
return KErrNone; |
|
1668 |
} |
|
1669 |
||
1670 |
||
1671 |
TInt DPager::HandlePageFault( TLinAddr aPc, TLinAddr aFaultAddress, TUint aFaultAsid, TUint aFaultIndex, |
|
1672 |
TUint aAccessPermissions, DMemoryObject* aMemory, DMemoryMapping* aMapping, |
|
1673 |
TUint aMapInstanceCount, DThread* aThread, TAny* aExceptionInfo) |
|
1674 |
{ |
|
1675 |
MmuLock::Lock(); |
|
1676 |
TInt r = TryRejuvenate( aFaultAsid, aFaultAddress, aAccessPermissions, aPc, aMapping, aMapInstanceCount, |
|
1677 |
aThread, aExceptionInfo); |
|
1678 |
if(r == KErrNone || r == KErrAbort) |
|
1679 |
{ |
|
1680 |
MmuLock::Unlock(); |
|
1681 |
} |
|
1682 |
else |
|
1683 |
{ |
|
1684 |
// rejuvenate failed, call memory manager to page in memory... |
|
1685 |
Event(EEventPageInStart, 0, aPc, aFaultAddress, aAccessPermissions); |
|
1686 |
MmuLock::Unlock(); |
|
1687 |
TheThrashMonitor.NotifyStartPaging(); |
|
1688 |
||
1689 |
DMemoryManager* manager = aMemory->iManager; |
|
1690 |
r = manager->HandleFault(aMemory, aFaultIndex, aMapping, aMapInstanceCount, aAccessPermissions); |
|
1691 |
||
1692 |
TheThrashMonitor.NotifyEndPaging(); |
|
1693 |
} |
|
1694 |
return r; |
|
1695 |
} |
|
1696 |
||
1697 |
||
1698 |
TInt DPager::ResizeLiveList() |
|
1699 |
{ |
|
1700 |
MmuLock::Lock(); |
|
1701 |
TUint min = iMinimumPageCount; |
|
1702 |
TUint max = iMaximumPageCount; |
|
1703 |
MmuLock::Unlock(); |
|
1704 |
return ResizeLiveList(min,max); |
|
1705 |
} |
|
1706 |
||
1707 |
||
1708 |
TInt DPager::ResizeLiveList(TUint aMinimumPageCount, TUint aMaximumPageCount) |
|
1709 |
{ |
|
1710 |
TRACE(("DPager::ResizeLiveList(%d,%d) current young=%d old=%d min=%d free=%d max=%d",aMinimumPageCount,aMaximumPageCount,iYoungCount,iOldCount,iMinimumPageCount,iNumberOfFreePages,iMaximumPageCount)); |
|
62
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1711 |
__NK_ASSERT_DEBUG(CacheInitialised()); |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1712 |
|
0 | 1713 |
if(!aMaximumPageCount) |
1714 |
{ |
|
1715 |
aMinimumPageCount = iInitMinimumPageCount; |
|
1716 |
aMaximumPageCount = iInitMaximumPageCount; |
|
1717 |
} |
|
1718 |
if (aMaximumPageCount > KAbsoluteMaxPageCount) |
|
1719 |
aMaximumPageCount = KAbsoluteMaxPageCount; |
|
1720 |
||
1721 |
// Min must not be greater than max... |
|
1722 |
if(aMinimumPageCount>aMaximumPageCount) |
|
1723 |
return KErrArgument; |
|
1724 |
||
1725 |
NKern::ThreadEnterCS(); |
|
1726 |
RamAllocLock::Lock(); |
|
1727 |
||
1728 |
MmuLock::Lock(); |
|
1729 |
||
62
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1730 |
__NK_ASSERT_ALWAYS(iYoungOldRatio!=0); |
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1731 |
|
0 | 1732 |
// Make sure aMinimumPageCount is not less than absolute minimum we can cope with... |
1733 |
iMinimumPageLimit = iMinYoungPages * (1 + iYoungOldRatio) / iYoungOldRatio |
|
1734 |
+ DPageReadRequest::ReservedPagesRequired(); |
|
1735 |
if(iMinimumPageLimit<iAbsoluteMinPageCount) |
|
1736 |
iMinimumPageLimit = iAbsoluteMinPageCount; |
|
1737 |
if(aMinimumPageCount<iMinimumPageLimit+iReservePageCount) |
|
1738 |
aMinimumPageCount = iMinimumPageLimit+iReservePageCount; |
|
1739 |
if(aMaximumPageCount<aMinimumPageCount) |
|
1740 |
aMaximumPageCount=aMinimumPageCount; |
|
1741 |
||
1742 |
// Increase iMaximumPageCount? |
|
1743 |
TInt extra = aMaximumPageCount-iMaximumPageCount; |
|
1744 |
if(extra>0) |
|
1745 |
iMaximumPageCount += extra; |
|
1746 |
||
1747 |
// Reduce iMinimumPageCount? |
|
1748 |
TInt spare = iMinimumPageCount-aMinimumPageCount; |
|
1749 |
if(spare>0) |
|
1750 |
{ |
|
1751 |
iMinimumPageCount -= spare; |
|
1752 |
iNumberOfFreePages += spare; |
|
1753 |
} |
|
1754 |
||
1755 |
// Increase iMinimumPageCount? |
|
1756 |
TInt r=KErrNone; |
|
1757 |
while(iMinimumPageCount<aMinimumPageCount) |
|
1758 |
{ |
|
1759 |
TUint newMin = aMinimumPageCount; |
|
1760 |
TUint maxMin = iMinimumPageCount+iNumberOfFreePages; |
|
1761 |
if(newMin>maxMin) |
|
1762 |
newMin = maxMin; |
|
1763 |
||
1764 |
TUint delta = newMin-iMinimumPageCount; |
|
1765 |
if(delta) |
|
1766 |
{ |
|
1767 |
iMinimumPageCount = newMin; |
|
1768 |
iNumberOfFreePages -= delta; |
|
1769 |
continue; |
|
1770 |
} |
|
1771 |
||
1772 |
if(!TryGrowLiveList()) |
|
1773 |
{ |
|
1774 |
r=KErrNoMemory; |
|
1775 |
break; |
|
1776 |
} |
|
1777 |
} |
|
1778 |
||
1779 |
// Reduce iMaximumPageCount? |
|
1780 |
while(iMaximumPageCount>aMaximumPageCount) |
|
1781 |
{ |
|
1782 |
TUint newMax = aMaximumPageCount; |
|
1783 |
TUint minMax = iMinimumPageCount+iNumberOfFreePages; |
|
1784 |
if(newMax<minMax) |
|
1785 |
newMax = minMax; |
|
1786 |
||
1787 |
TUint delta = iMaximumPageCount-newMax; |
|
1788 |
if(delta) |
|
1789 |
{ |
|
1790 |
iMaximumPageCount = newMax; |
|
1791 |
continue; |
|
1792 |
} |
|
1793 |
||
1794 |
ReturnPageToSystem(); |
|
1795 |
} |
|
1796 |
||
1797 |
TRACE(("DPager::ResizeLiveList end with young=%d old=%d min=%d free=%d max=%d",iYoungCount,iOldCount,iMinimumPageCount,iNumberOfFreePages,iMaximumPageCount)); |
|
1798 |
||
1799 |
#ifdef BTRACE_KERNEL_MEMORY |
|
1800 |
BTrace4(BTrace::EKernelMemory,BTrace::EKernelMemoryDemandPagingCache,iMinimumPageCount << KPageShift); |
|
1801 |
#endif |
|
1802 |
||
1803 |
MmuLock::Unlock(); |
|
1804 |
||
1805 |
RamAllocLock::Unlock(); |
|
1806 |
NKern::ThreadLeaveCS(); |
|
1807 |
||
1808 |
return r; |
|
1809 |
} |
|
1810 |
||
1811 |
||
1812 |
void DPager::FlushAll() |
|
1813 |
{ |
|
1814 |
NKern::ThreadEnterCS(); |
|
1815 |
RamAllocLock::Lock(); |
|
1816 |
||
1817 |
TRACE(("DPager::FlushAll() live list young=%d old=%d min=%d free=%d max=%d",iYoungCount,iOldCount,iMinimumPageCount,iNumberOfFreePages,iMaximumPageCount)); |
|
1818 |
||
1819 |
// look at all RAM pages in the system, and unmap all those used for paging |
|
1820 |
const TUint32* piMap = (TUint32*)KPageInfoMap; |
|
1821 |
const TUint32* piMapEnd = piMap+(KNumPageInfoPages>>5); |
|
1822 |
SPageInfo* pi = (SPageInfo*)KPageInfoLinearBase; |
|
1823 |
MmuLock::Lock(); |
|
1824 |
do |
|
1825 |
{ |
|
1826 |
SPageInfo* piNext = pi+(KPageInfosPerPage<<5); |
|
1827 |
for(TUint32 piFlags=*piMap++; piFlags; piFlags>>=1) |
|
1828 |
{ |
|
1829 |
if(!(piFlags&1)) |
|
1830 |
{ |
|
1831 |
pi += KPageInfosPerPage; |
|
1832 |
continue; |
|
1833 |
} |
|
1834 |
SPageInfo* piEnd = pi+KPageInfosPerPage; |
|
1835 |
do |
|
1836 |
{ |
|
1837 |
SPageInfo::TPagedState state = pi->PagedState(); |
|
1838 |
#ifdef _USE_OLDEST_LISTS |
|
1839 |
if (state==SPageInfo::EPagedYoung || state==SPageInfo::EPagedOld || |
|
1840 |
state==SPageInfo::EPagedOldestClean || state==SPageInfo::EPagedOldestDirty) |
|
1841 |
#else |
|
1842 |
if(state==SPageInfo::EPagedYoung || state==SPageInfo::EPagedOld) |
|
1843 |
#endif |
|
1844 |
{ |
|
1845 |
if (pi->Type() != SPageInfo::EUnused) |
|
1846 |
{ |
|
1847 |
TInt r = StealPage(pi); |
|
1848 |
if(r==KErrNone) |
|
1849 |
AddAsFreePage(pi); |
|
1850 |
MmuLock::Flash(); |
|
1851 |
} |
|
1852 |
} |
|
1853 |
++pi; |
|
1854 |
if(((TUint)pi&(0xf<<KPageInfoShift))==0) |
|
1855 |
MmuLock::Flash(); // every 16 page infos |
|
1856 |
} |
|
1857 |
while(pi<piEnd); |
|
1858 |
} |
|
1859 |
pi = piNext; |
|
1860 |
} |
|
1861 |
while(piMap<piMapEnd); |
|
1862 |
MmuLock::Unlock(); |
|
1863 |
||
1864 |
// reduce live page list to a minimum |
|
1865 |
while(GetFreePages(1)) {}; |
|
1866 |
||
1867 |
TRACE(("DPager::FlushAll() end with young=%d old=%d min=%d free=%d max=%d",iYoungCount,iOldCount,iMinimumPageCount,iNumberOfFreePages,iMaximumPageCount)); |
|
1868 |
||
1869 |
RamAllocLock::Unlock(); |
|
1870 |
NKern::ThreadLeaveCS(); |
|
1871 |
} |
|
1872 |
||
1873 |
||
1874 |
void DPager::GetLiveListInfo(SVMCacheInfo& aInfo) |
|
1875 |
{ |
|
1876 |
MmuLock::Lock(); // ensure consistent set of values are read... |
|
1877 |
aInfo.iMinSize = iMinimumPageCount<<KPageShift; |
|
1878 |
aInfo.iMaxSize = iMaximumPageCount<<KPageShift; |
|
1879 |
aInfo.iCurrentSize = (iMinimumPageCount+iNumberOfFreePages)<<KPageShift; |
|
1880 |
aInfo.iMaxFreeSize = iNumberOfFreePages<<KPageShift; |
|
1881 |
MmuLock::Unlock(); |
|
1882 |
} |
|
1883 |
||
1884 |
||
1885 |
void DPager::GetEventInfo(SVMEventInfo& aInfoOut) |
|
1886 |
{ |
|
1887 |
MmuLock::Lock(); // ensure consistent set of values are read... |
|
1888 |
aInfoOut = iEventInfo; |
|
1889 |
MmuLock::Unlock(); |
|
1890 |
} |
|
1891 |
||
1892 |
||
1893 |
void DPager::ResetEventInfo() |
|
1894 |
{ |
|
1895 |
MmuLock::Lock(); |
|
1896 |
memclr(&iEventInfo, sizeof(iEventInfo)); |
|
1897 |
MmuLock::Unlock(); |
|
1898 |
} |
|
1899 |
||
1900 |
||
1901 |
TInt TestPageState(TLinAddr aAddr) |
|
1902 |
{ |
|
1903 |
DMemModelProcess* process = (DMemModelProcess*)TheCurrentThread->iOwningProcess; |
|
1904 |
// Get the os asid of current thread's process so no need to open a reference on it. |
|
1905 |
TInt osAsid = process->OsAsid(); |
|
1906 |
TPte* ptePtr = 0; |
|
1907 |
TPte pte = 0; |
|
1908 |
TInt r = 0; |
|
1909 |
SPageInfo* pageInfo = NULL; |
|
1910 |
||
1911 |
NKern::ThreadEnterCS(); |
|
1912 |
||
1913 |
TUint offsetInMapping; |
|
1914 |
TUint mapInstanceCount; |
|
1915 |
DMemoryMapping* mapping = MM::FindMappingInAddressSpace(osAsid, aAddr, 1, offsetInMapping, mapInstanceCount); |
|
1916 |
||
1917 |
MmuLock::Lock(); |
|
1918 |
||
1919 |
if(mapping) |
|
1920 |
{ |
|
1921 |
DMemoryObject* memory = mapping->Memory(); |
|
1922 |
if(mapInstanceCount == mapping->MapInstanceCount() && memory) |
|
1923 |
{ |
|
1924 |
DMemoryManager* manager = memory->iManager; |
|
1925 |
if(manager==TheCodePagedMemoryManager) |
|
1926 |
r |= EPageStateInRamCode|EPageStatePaged; |
|
1927 |
} |
|
1928 |
} |
|
1929 |
||
1930 |
ptePtr = Mmu::SafePtePtrFromLinAddr(aAddr,osAsid); |
|
1931 |
if (!ptePtr) |
|
1932 |
goto done; |
|
1933 |
pte = *ptePtr; |
|
1934 |
if (pte == KPteUnallocatedEntry) |
|
1935 |
goto done; |
|
1936 |
r |= EPageStatePtePresent; |
|
1937 |
if (pte!=Mmu::MakePteInaccessible(pte,0)) |
|
1938 |
r |= EPageStatePteValid; |
|
1939 |
||
1940 |
pageInfo = SPageInfo::SafeFromPhysAddr(pte&~KPageMask); |
|
1941 |
if(pageInfo) |
|
1942 |
{ |
|
1943 |
r |= pageInfo->Type(); |
|
1944 |
r |= pageInfo->PagedState()<<8; |
|
1945 |
} |
|
1946 |
done: |
|
1947 |
MmuLock::Unlock(); |
|
1948 |
if(mapping) |
|
1949 |
mapping->Close(); |
|
1950 |
NKern::ThreadLeaveCS(); |
|
1951 |
return r; |
|
1952 |
} |
|
1953 |
||
1954 |
||
1955 |
||
1956 |
TInt VMHalFunction(TAny*, TInt aFunction, TAny* a1, TAny* a2) |
|
1957 |
{ |
|
1958 |
switch(aFunction) |
|
1959 |
{ |
|
1960 |
case EVMHalFlushCache: |
|
1961 |
if(!TheCurrentThread->HasCapability(ECapabilityWriteDeviceData,__PLATSEC_DIAGNOSTIC_STRING("Checked by VMHalFunction(EVMHalFlushCache)"))) |
|
1962 |
K::UnlockedPlatformSecurityPanic(); |
|
1963 |
ThePager.FlushAll(); |
|
1964 |
return KErrNone; |
|
1965 |
||
1966 |
case EVMHalSetCacheSize: |
|
1967 |
{ |
|
1968 |
if(!TheCurrentThread->HasCapability(ECapabilityWriteDeviceData,__PLATSEC_DIAGNOSTIC_STRING("Checked by VMHalFunction(EVMHalSetCacheSize)"))) |
|
1969 |
K::UnlockedPlatformSecurityPanic(); |
|
1970 |
TUint min = TUint(a1)>>KPageShift; |
|
1971 |
if(TUint(a1)&KPageMask) |
|
1972 |
++min; |
|
1973 |
TUint max = TUint(a2)>>KPageShift; |
|
1974 |
if(TUint(a2)&KPageMask) |
|
1975 |
++max; |
|
1976 |
return ThePager.ResizeLiveList(min,max); |
|
1977 |
} |
|
1978 |
||
1979 |
case EVMHalGetCacheSize: |
|
1980 |
{ |
|
1981 |
SVMCacheInfo info; |
|
1982 |
ThePager.GetLiveListInfo(info); |
|
1983 |
kumemput32(a1,&info,sizeof(info)); |
|
1984 |
} |
|
1985 |
return KErrNone; |
|
1986 |
||
1987 |
case EVMHalGetEventInfo: |
|
1988 |
{ |
|
1989 |
SVMEventInfo info; |
|
1990 |
ThePager.GetEventInfo(info); |
|
1991 |
Kern::InfoCopy(*(TDes8*)a1,(TUint8*)&info,sizeof(info)); |
|
1992 |
} |
|
1993 |
return KErrNone; |
|
1994 |
||
1995 |
case EVMHalResetEventInfo: |
|
1996 |
ThePager.ResetEventInfo(); |
|
1997 |
return KErrNone; |
|
1998 |
||
1999 |
#ifdef __SUPPORT_DEMAND_PAGING_EMULATION__ |
|
2000 |
case EVMHalGetOriginalRomPages: |
|
2001 |
RomOriginalPages(*((TPhysAddr**)a1), *((TUint*)a2)); |
|
2002 |
return KErrNone; |
|
2003 |
#endif |
|
2004 |
||
2005 |
case EVMPageState: |
|
2006 |
return TestPageState((TLinAddr)a1); |
|
2007 |
||
2008 |
case EVMHalGetSwapInfo: |
|
2009 |
{ |
|
2010 |
if ((K::MemModelAttributes & EMemModelAttrDataPaging) == 0) |
|
2011 |
return KErrNotSupported; |
|
2012 |
SVMSwapInfo info; |
|
2013 |
GetSwapInfo(info); |
|
2014 |
kumemput32(a1,&info,sizeof(info)); |
|
2015 |
} |
|
2016 |
return KErrNone; |
|
2017 |
||
2018 |
case EVMHalGetThrashLevel: |
|
2019 |
return TheThrashMonitor.ThrashLevel(); |
|
2020 |
||
2021 |
case EVMHalSetSwapThresholds: |
|
2022 |
{ |
|
2023 |
if(!TheCurrentThread->HasCapability(ECapabilityWriteDeviceData,__PLATSEC_DIAGNOSTIC_STRING("Checked by VMHalFunction(EVMHalSetSwapThresholds)"))) |
|
2024 |
K::UnlockedPlatformSecurityPanic(); |
|
2025 |
if ((K::MemModelAttributes & EMemModelAttrDataPaging) == 0) |
|
2026 |
return KErrNotSupported; |
|
2027 |
SVMSwapThresholds thresholds; |
|
2028 |
kumemget32(&thresholds,a1,sizeof(thresholds)); |
|
2029 |
return SetSwapThresholds(thresholds); |
|
2030 |
} |
|
2031 |
||
2032 |
case EVMHalSetThrashThresholds: |
|
2033 |
if(!TheCurrentThread->HasCapability(ECapabilityWriteDeviceData,__PLATSEC_DIAGNOSTIC_STRING("Checked by VMHalFunction(EVMHalSetThrashThresholds)"))) |
|
2034 |
K::UnlockedPlatformSecurityPanic(); |
|
2035 |
return TheThrashMonitor.SetThresholds((TUint)a1, (TUint)a2); |
|
2036 |
||
2037 |
#ifdef __DEMAND_PAGING_BENCHMARKS__ |
|
2038 |
case EVMHalGetPagingBenchmark: |
|
2039 |
{ |
|
2040 |
TUint index = (TInt) a1; |
|
2041 |
if (index >= EMaxPagingBm) |
|
2042 |
return KErrNotFound; |
|
2043 |
NKern::LockSystem(); |
|
2044 |
SPagingBenchmarkInfo info = ThePager.iBenchmarkInfo[index]; |
|
2045 |
NKern::UnlockSystem(); |
|
2046 |
kumemput32(a2,&info,sizeof(info)); |
|
2047 |
} |
|
2048 |
return KErrNone; |
|
2049 |
||
2050 |
case EVMHalResetPagingBenchmark: |
|
2051 |
{ |
|
2052 |
TUint index = (TInt) a1; |
|
2053 |
if (index >= EMaxPagingBm) |
|
2054 |
return KErrNotFound; |
|
2055 |
NKern::LockSystem(); |
|
2056 |
ThePager.ResetBenchmarkData((TPagingBenchmark)index); |
|
2057 |
NKern::UnlockSystem(); |
|
2058 |
} |
|
2059 |
return KErrNone; |
|
2060 |
#endif |
|
2061 |
||
2062 |
default: |
|
2063 |
return KErrNotSupported; |
|
2064 |
} |
|
2065 |
} |
|
2066 |
||
2067 |
||
2068 |
#ifdef __DEMAND_PAGING_BENCHMARKS__ |
|
2069 |
||
2070 |
void DPager::ResetBenchmarkData(TPagingBenchmark aBm) |
|
2071 |
{ |
|
2072 |
SPagingBenchmarkInfo& info = iBenchmarkInfo[aBm]; |
|
2073 |
info.iCount = 0; |
|
2074 |
info.iTotalTime = 0; |
|
2075 |
info.iMaxTime = 0; |
|
2076 |
info.iMinTime = KMaxTInt; |
|
2077 |
} |
|
2078 |
||
2079 |
void DPager::RecordBenchmarkData(TPagingBenchmark aBm, TUint32 aStartTime, TUint32 aEndTime) |
|
2080 |
{ |
|
2081 |
SPagingBenchmarkInfo& info = iBenchmarkInfo[aBm]; |
|
2082 |
++info.iCount; |
|
2083 |
#if !defined(HIGH_RES_TIMER) || defined(HIGH_RES_TIMER_COUNTS_UP) |
|
2084 |
TInt64 elapsed = aEndTime - aStartTime; |
|
2085 |
#else |
|
2086 |
TInt64 elapsed = aStartTime - aEndTime; |
|
2087 |
#endif |
|
2088 |
info.iTotalTime += elapsed; |
|
2089 |
if (elapsed > info.iMaxTime) |
|
2090 |
info.iMaxTime = elapsed; |
|
2091 |
if (elapsed < info.iMinTime) |
|
2092 |
info.iMinTime = elapsed; |
|
2093 |
} |
|
2094 |
||
2095 |
#endif //__DEMAND_PAGING_BENCHMARKS__ |
|
2096 |
||
2097 |
||
2098 |
// |
|
2099 |
// Paging request management... |
|
2100 |
// |
|
2101 |
||
2102 |
// |
|
2103 |
// DPagingRequest |
|
2104 |
// |
|
2105 |
||
2106 |
DPagingRequest::DPagingRequest(DPagingRequestPool::TGroup& aPoolGroup) |
|
2107 |
: iPoolGroup(aPoolGroup), iUseRegionMemory(0), iUseRegionIndex(0), iUseRegionCount(0) |
|
2108 |
{ |
|
2109 |
} |
|
2110 |
||
2111 |
||
2112 |
FORCE_INLINE void DPagingRequest::SetUse(DMemoryObject* aMemory, TUint aIndex, TUint aCount) |
|
2113 |
{ |
|
2114 |
__ASSERT_SYSTEM_LOCK; |
|
2115 |
iUseRegionMemory = aMemory; |
|
2116 |
iUseRegionIndex = aIndex; |
|
2117 |
iUseRegionCount = aCount; |
|
2118 |
} |
|
2119 |
||
2120 |
||
2121 |
TBool DPagingRequest::CheckUse(DMemoryObject* aMemory, TUint aIndex, TUint aCount) |
|
2122 |
{ |
|
2123 |
return aMemory==iUseRegionMemory |
|
2124 |
&& TUint(aIndex-iUseRegionIndex) < iUseRegionCount |
|
2125 |
&& TUint(iUseRegionCount-TUint(aIndex-iUseRegionIndex)) <= aCount; |
|
2126 |
} |
|
2127 |
||
2128 |
||
2129 |
void DPagingRequest::Release() |
|
2130 |
{ |
|
2131 |
NKern::LockSystem(); |
|
2132 |
SetUse(0,0,0); |
|
2133 |
Signal(); |
|
2134 |
} |
|
2135 |
||
2136 |
||
2137 |
void DPagingRequest::Wait() |
|
2138 |
{ |
|
2139 |
__ASSERT_SYSTEM_LOCK; |
|
2140 |
++iUsageCount; |
|
2141 |
TInt r = iMutex->Wait(); |
|
2142 |
__NK_ASSERT_ALWAYS(r == KErrNone); |
|
2143 |
} |
|
2144 |
||
2145 |
||
2146 |
void DPagingRequest::Signal() |
|
2147 |
{ |
|
2148 |
__ASSERT_SYSTEM_LOCK; |
|
2149 |
iPoolGroup.Signal(this); |
|
2150 |
} |
|
2151 |
||
2152 |
||
2153 |
FORCE_INLINE TBool DPagingRequest::IsCollision(DMemoryObject* aMemory, TUint aIndex, TUint aCount) |
|
2154 |
{ |
|
2155 |
__ASSERT_SYSTEM_LOCK; |
|
2156 |
DMemoryObject* memory = iUseRegionMemory; |
|
2157 |
TUint index = iUseRegionIndex; |
|
2158 |
TUint count = iUseRegionCount; |
|
2159 |
// note, this comparison would fail if either region includes page number KMaxTUint, |
|
2160 |
// but it isn't possible to create a memory object which is > KMaxTUint pages... |
|
2161 |
return memory == aMemory && index+count > aIndex && index < aIndex+aCount; |
|
2162 |
} |
|
2163 |
||
2164 |
||
2165 |
TLinAddr DPagingRequest::MapPages(TUint aColour, TUint aCount, TPhysAddr* aPages) |
|
2166 |
{ |
|
2167 |
__NK_ASSERT_DEBUG(iMutex->iCleanup.iThread == &Kern::CurrentThread()); |
|
2168 |
return iTempMapping.Map(aPages,aCount,aColour); |
|
2169 |
} |
|
2170 |
||
2171 |
||
2172 |
void DPagingRequest::UnmapPages(TBool aIMBRequired) |
|
2173 |
{ |
|
2174 |
__NK_ASSERT_DEBUG(iMutex->iCleanup.iThread == &Kern::CurrentThread()); |
|
2175 |
iTempMapping.Unmap(aIMBRequired); |
|
2176 |
} |
|
2177 |
||
2178 |
||
2179 |
// |
|
2180 |
// DPageReadRequest |
|
2181 |
// |
|
2182 |
||
2183 |
TInt DPageReadRequest::iAllocNext = 0; |
|
2184 |
||
2185 |
TInt DPageReadRequest::Construct() |
|
2186 |
{ |
|
2187 |
// allocate id and mutex... |
|
2188 |
TUint id = (TUint)__e32_atomic_add_ord32(&iAllocNext, 1); |
|
2189 |
_LIT(KLitPagingRequest,"PageReadRequest-"); |
|
2190 |
TBuf<sizeof("PageReadRequest-")+10> mutexName(KLitPagingRequest); |
|
2191 |
mutexName.AppendNum(id); |
|
2192 |
TInt r = K::MutexCreate(iMutex, mutexName, NULL, EFalse, KMutexOrdPageIn); |
|
2193 |
if(r!=KErrNone) |
|
2194 |
return r; |
|
2195 |
||
2196 |
// allocate space for mapping pages whilst they're being loaded... |
|
2197 |
iTempMapping.Alloc(EMaxPages); |
|
2198 |
||
2199 |
// create memory buffer... |
|
2200 |
TUint bufferSize = EMaxPages+1; |
|
2201 |
DMemoryObject* bufferMemory; |
|
2202 |
r = MM::MemoryNew(bufferMemory,EMemoryObjectUnpaged,bufferSize,EMemoryCreateNoWipe); |
|
2203 |
if(r!=KErrNone) |
|
2204 |
return r; |
|
2205 |
MM::MemorySetLock(bufferMemory,iMutex); |
|
2206 |
TPhysAddr physAddr; |
|
2207 |
r = MM::MemoryAllocContiguous(bufferMemory,0,bufferSize,0,physAddr); |
|
2208 |
(void)physAddr; |
|
2209 |
if(r!=KErrNone) |
|
2210 |
return r; |
|
2211 |
DMemoryMapping* bufferMapping; |
|
2212 |
r = MM::MappingNew(bufferMapping,bufferMemory,ESupervisorReadWrite,KKernelOsAsid); |
|
2213 |
if(r!=KErrNone) |
|
2214 |
return r; |
|
2215 |
iBuffer = MM::MappingBase(bufferMapping); |
|
2216 |
||
2217 |
return r; |
|
2218 |
} |
|
2219 |
||
2220 |
||
2221 |
// |
|
2222 |
// DPageWriteRequest |
|
2223 |
// |
|
2224 |
||
2225 |
TInt DPageWriteRequest::iAllocNext = 0; |
|
2226 |
||
2227 |
TInt DPageWriteRequest::Construct() |
|
2228 |
{ |
|
2229 |
// allocate id and mutex... |
|
2230 |
TUint id = (TUint)__e32_atomic_add_ord32(&iAllocNext, 1); |
|
2231 |
_LIT(KLitPagingRequest,"PageWriteRequest-"); |
|
2232 |
TBuf<sizeof("PageWriteRequest-")+10> mutexName(KLitPagingRequest); |
|
2233 |
mutexName.AppendNum(id); |
|
2234 |
TInt r = K::MutexCreate(iMutex, mutexName, NULL, EFalse, KMutexOrdPageOut); |
|
2235 |
if(r!=KErrNone) |
|
2236 |
return r; |
|
2237 |
||
2238 |
// allocate space for mapping pages whilst they're being loaded... |
|
2239 |
iTempMapping.Alloc(EMaxPages); |
|
2240 |
||
2241 |
return r; |
|
2242 |
} |
|
2243 |
||
2244 |
||
2245 |
// |
|
2246 |
// DPagingRequestPool |
|
2247 |
// |
|
2248 |
||
2249 |
DPagingRequestPool::DPagingRequestPool(TUint aNumPageReadRequest,TUint aNumPageWriteRequest) |
|
2250 |
: iPageReadRequests(aNumPageReadRequest), iPageWriteRequests(aNumPageWriteRequest) |
|
2251 |
{ |
|
2252 |
TUint i; |
|
2253 |
||
2254 |
for(i=0; i<aNumPageReadRequest; ++i) |
|
2255 |
{ |
|
2256 |
DPageReadRequest* req = new DPageReadRequest(iPageReadRequests); |
|
2257 |
__NK_ASSERT_ALWAYS(req); |
|
2258 |
TInt r = req->Construct(); |
|
2259 |
__NK_ASSERT_ALWAYS(r==KErrNone); |
|
2260 |
iPageReadRequests.iRequests[i] = req; |
|
2261 |
iPageReadRequests.iFreeList.Add(req); |
|
2262 |
} |
|
2263 |
||
2264 |
for(i=0; i<aNumPageWriteRequest; ++i) |
|
2265 |
{ |
|
2266 |
DPageWriteRequest* req = new DPageWriteRequest(iPageWriteRequests); |
|
2267 |
__NK_ASSERT_ALWAYS(req); |
|
2268 |
TInt r = req->Construct(); |
|
2269 |
__NK_ASSERT_ALWAYS(r==KErrNone); |
|
2270 |
iPageWriteRequests.iRequests[i] = req; |
|
2271 |
iPageWriteRequests.iFreeList.Add(req); |
|
2272 |
} |
|
2273 |
} |
|
2274 |
||
2275 |
||
2276 |
DPagingRequestPool::~DPagingRequestPool() |
|
2277 |
{ |
|
2278 |
__NK_ASSERT_ALWAYS(0); // deletion not implemented |
|
2279 |
} |
|
2280 |
||
2281 |
||
2282 |
DPageReadRequest* DPagingRequestPool::AcquirePageReadRequest(DMemoryObject* aMemory, TUint aIndex, TUint aCount) |
|
2283 |
{ |
|
2284 |
NKern::LockSystem(); |
|
2285 |
||
2286 |
DPagingRequest* req; |
|
2287 |
||
2288 |
// if we collide with page write operation... |
|
2289 |
req = iPageWriteRequests.FindCollision(aMemory,aIndex,aCount); |
|
2290 |
if(req) |
|
2291 |
{ |
|
2292 |
// wait until write completes... |
|
2293 |
req->Wait(); |
|
2294 |
req->Signal(); |
|
2295 |
return 0; // caller expected to retry if needed |
|
2296 |
} |
|
2297 |
||
2298 |
// get a request object to use... |
|
2299 |
req = iPageReadRequests.GetRequest(aMemory,aIndex,aCount); |
|
2300 |
||
2301 |
// check no new requests collide with us... |
|
2302 |
if(iPageWriteRequests.FindCollision(aMemory,aIndex,aCount) |
|
2303 |
|| iPageReadRequests.FindCollision(aMemory,aIndex,aCount)) |
|
2304 |
{ |
|
2305 |
// another operation is colliding with this region, give up and retry... |
|
2306 |
req->Signal(); |
|
2307 |
return 0; // caller expected to retry if needed |
|
2308 |
} |
|
2309 |
||
2310 |
// we have a request object which we can use... |
|
2311 |
req->SetUse(aMemory,aIndex,aCount); |
|
2312 |
||
2313 |
NKern::UnlockSystem(); |
|
2314 |
return (DPageReadRequest*)req; |
|
2315 |
} |
|
2316 |
||
2317 |
||
2318 |
DPageWriteRequest* DPagingRequestPool::AcquirePageWriteRequest(DMemoryObject* aMemory, TUint aIndex, TUint aCount) |
|
2319 |
{ |
|
2320 |
NKern::LockSystem(); |
|
2321 |
||
2322 |
DPagingRequest* req; |
|
2323 |
||
2324 |
for(;;) |
|
2325 |
{ |
|
2326 |
// get a request object to use... |
|
2327 |
req = iPageWriteRequests.GetRequest(aMemory,aIndex,aCount); |
|
2328 |
||
2329 |
if(iPageWriteRequests.FindCollision(aMemory,aIndex,aCount)) |
|
2330 |
{ |
|
2331 |
// another write operation is colliding with this region, give up and retry... |
|
2332 |
req->Signal(); |
|
2333 |
// Reacquire the system lock as Signal() above will release it. |
|
2334 |
NKern::LockSystem(); |
|
2335 |
continue; |
|
2336 |
} |
|
2337 |
||
2338 |
break; |
|
2339 |
} |
|
2340 |
||
2341 |
// we have a request object which we can use... |
|
2342 |
req->SetUse(aMemory,aIndex,aCount); |
|
2343 |
||
2344 |
NKern::UnlockSystem(); |
|
2345 |
return (DPageWriteRequest*)req; |
|
2346 |
} |
|
2347 |
||
2348 |
||
2349 |
DPagingRequestPool::TGroup::TGroup(TUint aNumRequests) |
|
2350 |
{ |
|
2351 |
iNumRequests = aNumRequests; |
|
2352 |
iRequests = new DPagingRequest*[aNumRequests]; |
|
2353 |
__NK_ASSERT_ALWAYS(iRequests); |
|
2354 |
} |
|
2355 |
||
2356 |
||
2357 |
DPagingRequest* DPagingRequestPool::TGroup::FindCollision(DMemoryObject* aMemory, TUint aIndex, TUint aCount) |
|
2358 |
{ |
|
2359 |
__ASSERT_SYSTEM_LOCK; |
|
2360 |
DPagingRequest** ptr = iRequests; |
|
2361 |
DPagingRequest** ptrEnd = ptr+iNumRequests; |
|
2362 |
while(ptr<ptrEnd) |
|
2363 |
{ |
|
2364 |
DPagingRequest* req = *ptr++; |
|
2365 |
if(req->IsCollision(aMemory,aIndex,aCount)) |
|
2366 |
return req; |
|
2367 |
} |
|
2368 |
return 0; |
|
2369 |
} |
|
2370 |
||
2371 |
||
2372 |
static TUint32 RandomSeed = 33333; |
|
2373 |
||
2374 |
DPagingRequest* DPagingRequestPool::TGroup::GetRequest(DMemoryObject* aMemory, TUint aIndex, TUint aCount) |
|
2375 |
{ |
|
2376 |
__NK_ASSERT_DEBUG(iNumRequests > 0); |
|
2377 |
||
2378 |
// try using an existing request which collides with this region... |
|
2379 |
DPagingRequest* req = FindCollision(aMemory,aIndex,aCount); |
|
2380 |
if(!req) |
|
2381 |
{ |
|
2382 |
// use a free request... |
|
2383 |
req = (DPagingRequest*)iFreeList.GetFirst(); |
|
2384 |
if(req) |
|
2385 |
{ |
|
2386 |
// free requests aren't being used... |
|
2387 |
__NK_ASSERT_DEBUG(req->iUsageCount == 0); |
|
2388 |
} |
|
2389 |
else |
|
2390 |
{ |
|
2391 |
// pick a random request... |
|
2392 |
RandomSeed = RandomSeed*69069+1; // next 'random' number |
|
2393 |
TUint index = (TUint64(RandomSeed) * TUint64(iNumRequests)) >> 32; |
|
2394 |
req = iRequests[index]; |
|
2395 |
__NK_ASSERT_DEBUG(req->iUsageCount > 0); // we only pick random when none are free |
|
2396 |
} |
|
2397 |
} |
|
2398 |
||
2399 |
// wait for chosen request object... |
|
2400 |
req->Wait(); |
|
2401 |
||
2402 |
return req; |
|
2403 |
} |
|
2404 |
||
2405 |
||
2406 |
void DPagingRequestPool::TGroup::Signal(DPagingRequest* aRequest) |
|
2407 |
{ |
|
2408 |
// if there are no threads waiting on the mutex then return it to the free pool... |
|
2409 |
__NK_ASSERT_DEBUG(aRequest->iUsageCount > 0); |
|
2410 |
if (--aRequest->iUsageCount==0) |
|
2411 |
iFreeList.AddHead(aRequest); |
|
2412 |
||
2413 |
aRequest->iMutex->Signal(); |
|
2414 |
} |
|
2415 |
||
2416 |
||
2417 |
/** |
|
2418 |
Register the specified paging device with the kernel. |
|
2419 |
||
2420 |
@param aDevice A pointer to the paging device to install |
|
2421 |
||
2422 |
@return KErrNone on success |
|
2423 |
*/ |
|
2424 |
EXPORT_C TInt Kern::InstallPagingDevice(DPagingDevice* aDevice) |
|
2425 |
{ |
|
2426 |
TRACEB(("Kern::InstallPagingDevice(0x%08x) name='%s' type=%d",aDevice,aDevice->iName,aDevice->iType)); |
|
2427 |
||
62
4a8fed1c0ef6
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2428 |
__NK_ASSERT_DEBUG(!ThePager.CacheInitialised()); |
0 | 2429 |
__NK_ASSERT_ALWAYS(aDevice->iReadUnitShift <= KPageShift); |
2430 |
||
2431 |
TInt r = KErrNotSupported; // Will return this if unsupported device type is installed |
|
2432 |
||
2433 |
// create the pools of page out and page in requests... |
|
2434 |
const TInt writeReqs = (aDevice->iType & DPagingDevice::EData) ? KPagingRequestsPerDevice : 0; |
|
2435 |
aDevice->iRequestPool = new DPagingRequestPool(KPagingRequestsPerDevice,writeReqs); |
|
2436 |
if(!aDevice->iRequestPool) |
|
2437 |
{ |
|
2438 |
r = KErrNoMemory; |
|
2439 |
goto exit; |
|
2440 |
} |
|
2441 |
||
2442 |
if(aDevice->iType & DPagingDevice::ERom) |
|
2443 |
{ |
|
2444 |
r = TheRomMemoryManager->InstallPagingDevice(aDevice); |
|
2445 |
if(r!=KErrNone) |
|
2446 |
goto exit; |
|
2447 |
} |
|
2448 |
||
2449 |
if(aDevice->iType & DPagingDevice::ECode) |
|
2450 |
{ |
|
2451 |
r = TheCodePagedMemoryManager->InstallPagingDevice(aDevice); |
|
2452 |
if(r!=KErrNone) |
|
2453 |
goto exit; |
|
2454 |
} |
|
2455 |
||
2456 |
if(aDevice->iType & DPagingDevice::EData) |
|
2457 |
{ |
|
2458 |
r = TheDataPagedMemoryManager->InstallPagingDevice(aDevice); |
|
2459 |
if(r!=KErrNone) |
|
2460 |
goto exit; |
|
2461 |
} |
|
2462 |
||
2463 |
if (K::MemModelAttributes & (EMemModelAttrRomPaging | EMemModelAttrCodePaging | EMemModelAttrDataPaging)) |
|
2464 |
TheThrashMonitor.Start(); |
|
2465 |
||
2466 |
exit: |
|
2467 |
TRACEB(("Kern::InstallPagingDevice returns %d",r)); |
|
2468 |
return r; |
|
2469 |
} |
|
2470 |
||
2471 |
||
2472 |
||
2473 |
// |
|
2474 |
// DDemandPagingLock |
|
2475 |
// |
|
2476 |
||
2477 |
EXPORT_C DDemandPagingLock::DDemandPagingLock() |
|
2478 |
: iReservedPageCount(0), iLockedPageCount(0), iPinMapping(0) |
|
2479 |
{ |
|
2480 |
} |
|
2481 |
||
2482 |
||
2483 |
EXPORT_C TInt DDemandPagingLock::Alloc(TInt aSize) |
|
2484 |
{ |
|
2485 |
TRACEP(("DDemandPagingLock[0x%08x]::Alloc(0x%x)",this,aSize)); |
|
2486 |
iMaxPageCount = ((aSize-1+KPageMask)>>KPageShift)+1; |
|
2487 |
||
2488 |
TInt r = KErrNoMemory; |
|
2489 |
||
2490 |
NKern::ThreadEnterCS(); |
|
2491 |
||
2492 |
TUint maxPt = DVirtualPinMapping::MaxPageTables(iMaxPageCount); |
|
2493 |
// Note, we need to reserve whole pages even for page tables which are smaller |
|
2494 |
// because pinning can remove the page from live list... |
|
2495 |
TUint reserve = iMaxPageCount+maxPt*KNumPagesToPinOnePageTable; |
|
2496 |
if(ThePager.ReservePages(reserve,(TUint&)iReservedPageCount)) |
|
2497 |
{ |
|
2498 |
iPinMapping = DVirtualPinMapping::New(iMaxPageCount); |
|
2499 |
if(iPinMapping) |
|
2500 |
r = KErrNone; |
|
2501 |
else |
|
2502 |
ThePager.UnreservePages((TUint&)iReservedPageCount); |
|
2503 |
} |
|
2504 |
||
2505 |
NKern::ThreadLeaveCS(); |
|
2506 |
TRACEP(("DDemandPagingLock[0x%08x]::Alloc returns %d, iMaxPageCount=%d, iReservedPageCount=%d",this,r,iMaxPageCount,iReservedPageCount)); |
|
2507 |
return r; |
|
2508 |
} |
|
2509 |
||
2510 |
||
2511 |
EXPORT_C void DDemandPagingLock::Free() |
|
2512 |
{ |
|
2513 |
TRACEP(("DDemandPagingLock[0x%08x]::Free()")); |
|
2514 |
Unlock(); |
|
2515 |
NKern::ThreadEnterCS(); |
|
2516 |
DVirtualPinMapping* pinMapping = (DVirtualPinMapping*)__e32_atomic_swp_ord_ptr(&iPinMapping, 0); |
|
2517 |
if (pinMapping) |
|
2518 |
pinMapping->Close(); |
|
2519 |
NKern::ThreadLeaveCS(); |
|
2520 |
ThePager.UnreservePages((TUint&)iReservedPageCount); |
|
2521 |
} |
|
2522 |
||
2523 |
||
2524 |
EXPORT_C TInt DDemandPagingLock::Lock(DThread* aThread, TLinAddr aStart, TInt aSize) |
|
2525 |
{ |
|
2526 |
// TRACEP(("DDemandPagingLock[0x%08x]::Lock(0x%08x,0x%08x,0x%08x)",this,aThread,aStart,aSize)); |
|
2527 |
if(iLockedPageCount) |
|
2528 |
__NK_ASSERT_ALWAYS(0); // lock already used |
|
2529 |
||
2530 |
// calculate the number of pages that need to be locked... |
|
2531 |
TUint mask=KPageMask; |
|
2532 |
TUint offset=aStart&mask; |
|
2533 |
TInt numPages = (aSize+offset+mask)>>KPageShift; |
|
2534 |
if(numPages>iMaxPageCount) |
|
2535 |
__NK_ASSERT_ALWAYS(0); |
|
2536 |
||
2537 |
NKern::ThreadEnterCS(); |
|
2538 |
||
2539 |
// find mapping which covers the specified region... |
|
2540 |
TUint offsetInMapping; |
|
2541 |
TUint mapInstanceCount; |
|
2542 |
DMemoryMapping* mapping = MM::FindMappingInThread((DMemModelThread*)aThread, aStart, aSize, offsetInMapping, mapInstanceCount); |
|
2543 |
if(!mapping) |
|
2544 |
{ |
|
2545 |
NKern::ThreadLeaveCS(); |
|
2546 |
return KErrBadDescriptor; |
|
2547 |
} |
|
2548 |
||
2549 |
MmuLock::Lock(); |
|
2550 |
DMemoryObject* memory = mapping->Memory(); |
|
2551 |
if(mapInstanceCount != mapping->MapInstanceCount() || !memory) |
|
2552 |
{// Mapping has been reused or no memory. |
|
2553 |
MmuLock::Unlock(); |
|
2554 |
mapping->Close(); |
|
2555 |
NKern::ThreadLeaveCS(); |
|
2556 |
return KErrBadDescriptor; |
|
2557 |
} |
|
2558 |
||
2559 |
if(!memory->IsDemandPaged()) |
|
2560 |
{ |
|
2561 |
// memory not demand paged, so we have nothing to do... |
|
2562 |
MmuLock::Unlock(); |
|
2563 |
mapping->Close(); |
|
2564 |
NKern::ThreadLeaveCS(); |
|
2565 |
return KErrNone; |
|
2566 |
} |
|
2567 |
||
2568 |
// Open a reference on the memory so it doesn't get deleted. |
|
2569 |
memory->Open(); |
|
2570 |
MmuLock::Unlock(); |
|
2571 |
||
2572 |
// pin memory... |
|
2573 |
TUint index = (offsetInMapping>>KPageShift)+mapping->iStartIndex; |
|
2574 |
TUint count = ((offsetInMapping&KPageMask)+aSize+KPageMask)>>KPageShift; |
|
2575 |
TInt r = ((DVirtualPinMapping*)iPinMapping)->Pin( memory,index,count,mapping->Permissions(), |
|
2576 |
mapping, mapInstanceCount); |
|
2577 |
||
2578 |
if(r==KErrNotFound) |
|
2579 |
{ |
|
2580 |
// some memory wasn't present, so treat this as an error... |
|
2581 |
memory->Close(); |
|
2582 |
mapping->Close(); |
|
2583 |
NKern::ThreadLeaveCS(); |
|
2584 |
return KErrBadDescriptor; |
|
2585 |
} |
|
2586 |
||
2587 |
// we can't fail to pin otherwise... |
|
2588 |
__NK_ASSERT_DEBUG(r!=KErrNoMemory); // separate OOM assert to aid debugging |
|
2589 |
__NK_ASSERT_ALWAYS(r==KErrNone); |
|
2590 |
||
2591 |
// indicate that we have actually pinned... |
|
2592 |
__NK_ASSERT_DEBUG(iLockedPageCount==0); |
|
2593 |
iLockedPageCount = count; |
|
2594 |
||
2595 |
// cleanup... |
|
2596 |
memory->Close(); |
|
2597 |
mapping->Close(); |
|
2598 |
NKern::ThreadLeaveCS(); |
|
2599 |
||
2600 |
return 1; |
|
2601 |
} |
|
2602 |
||
2603 |
||
2604 |
EXPORT_C void DDemandPagingLock::DoUnlock() |
|
2605 |
{ |
|
2606 |
NKern::ThreadEnterCS(); |
|
2607 |
((DVirtualPinMapping*)iPinMapping)->Unpin(); |
|
2608 |
__NK_ASSERT_DEBUG(iLockedPageCount); |
|
2609 |
iLockedPageCount = 0; |
|
2610 |
NKern::ThreadLeaveCS(); |
|
2611 |
} |
|
2612 |
||
2613 |