First public contribution.
1 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32test\mmu\d_shadow.cpp
15 // LDD for testing ROM shadowing
19 #include <kernel/kern_priv.h>
21 #include <kernel/cache.h>
24 const TInt KMajorVersionNumber=0;
25 const TInt KMinorVersionNumber=1;
26 const TInt KBuildVersionNumber=1;
28 _LIT(KLddName,"Shadow");
32 class DShadowFactory : public DLogicalDevice
34 // Shadow ROM LDD factory
39 virtual TInt Install(); //overriding pure virtual
40 virtual void GetCaps(TDes8& aDes) const; //overriding pure virtual
41 virtual TInt Create(DLogicalChannelBase*& aChannel); //overriding pure virtual
44 class DShadow : public DLogicalChannelBase
46 // Shadow ROM logical channel
53 virtual TInt DoCreate(TInt aUnit, const TDesC8* anInfo, const TVersion& aVer);
54 virtual TInt Request(TInt aFunction, TAny* a1, TAny* a2);
57 DECLARE_STANDARD_LDD()
59 return new DShadowFactory;
62 DShadowFactory::DShadowFactory()
67 iVersion=TVersion(KMajorVersionNumber,KMinorVersionNumber,KBuildVersionNumber);
68 //iParseMask=0;//No units, no info, no PDD
69 //iUnitsMask=0;//Only one thing
72 TInt DShadowFactory::Create(DLogicalChannelBase*& aChannel)
74 // Create a new DShadow on this logical device
78 return aChannel?KErrNone:KErrNoMemory;
81 TInt DShadowFactory::Install()
83 // Install the LDD - overriding pure virtual
86 return SetName(&KLddName);
89 void DShadowFactory::GetCaps(TDes8& aDes) const
91 // Get capabilities - overriding pure virtual
95 b.iVersion=TVersion(KMajorVersionNumber,KMinorVersionNumber,KBuildVersionNumber);
96 Kern::InfoCopy(aDes,(TUint8*)&b,sizeof(b));
106 TInt DShadow::DoCreate(TInt /*aUnit*/, const TDesC8* /*anInfo*/, const TVersion& aVer)
112 if (!Kern::QueryVersionSupported(TVersion(KMajorVersionNumber,KMinorVersionNumber,KBuildVersionNumber),aVer))
113 return KErrNotSupported;
125 extern TInt DoRead(TAny*);
126 extern TInt GetMmuId();
127 extern TInt GetCacheType();
128 extern TUint GetTTBCR();
129 extern TUint GetControlRegister();
132 LOCAL_C TInt KernStackSize()
134 TRomEntry* pE=(TRomEntry*)Kern::SuperPage().iPrimaryEntry;
135 TRomImageHeader* pI=(TRomImageHeader*)pE->iAddressLin;
136 return pI->iStackSize;
139 LOCAL_C TInt MeasureKernStackUse()
141 TLinAddr kstack=Kern::RoundToPageSize(Epoc::RomHeader().iKernDataAddress + Epoc::RomHeader().iTotalSvDataSize);
142 TLinAddr kstackEnd=kstack+KernStackSize();
143 TUint32 *pS=(TUint32*)kstack;
144 while(*pS==0xffffffff) pS++;
145 TUint used=kstackEnd-TLinAddr(pS);
149 #if !defined(__WINS__)
150 TInt DShadow::Request(TInt aFunction, TAny* a1, TAny* a2)
152 TInt pageSize=Kern::RoundToPageSize(1);
156 case RShadow::EControlAllocShadow:
157 NKern::ThreadEnterCS();
158 r=Epoc::AllocShadowPage(TLinAddr(a1));
159 NKern::ThreadLeaveCS();
161 case RShadow::EControlFreeShadow:
162 NKern::ThreadEnterCS();
163 r=Epoc::FreeShadowPage(TLinAddr(a1));
164 NKern::ThreadLeaveCS();
167 case RShadow::EControlWriteShadow: //copy 4KB(page size) data to shadow page
169 NKern::ThreadEnterCS();
170 void* alloc = Kern::Alloc(pageSize); //CopyToShadowMemory assumes Kernel adress space. Copy here first
173 kumemget(alloc,a2,pageSize);//From user space to kernel heap
174 for (TInt i=0;i<pageSize;i+=32)
175 Epoc::CopyToShadowMemory((TLinAddr)((TInt)a1+i),(TLinAddr)((TInt)alloc+i),32);
176 Cache::IMB_Range((TLinAddr)a1,pageSize);
182 NKern::ThreadLeaveCS();
185 case RShadow::EControlFreezeShadow:
186 NKern::ThreadEnterCS();
187 r=Epoc::FreezeShadowPage(TLinAddr(a1));
188 NKern::ThreadLeaveCS();
190 case RShadow::EControlSetPriority:
195 DThread *pT=(DThread*)Kern::CurrentThread().ObjectFromHandle(h);
196 pT->SetThreadPriority(p);
197 NKern::UnlockSystem();
201 case RShadow::EControlRead:
204 case RShadow::EControlMmuId:
207 case RShadow::EControlCacheType:
211 case RShadow::EControlMeasureKernStackUse:
212 r=MeasureKernStackUse();
214 case RShadow::EControlMeasureKernHeapFree:
217 case RShadow::EControlCallFunction:
219 TThreadFunction f=(TThreadFunction)a1;
223 case RShadow::EControlAllocPhys:
230 NKern::ThreadEnterCS();
231 r=Epoc::AllocPhysicalRam(size,pa,align);
232 NKern::ThreadLeaveCS();
244 case RShadow::EControlFreePhys:
247 TPhysAddr pa=(TPhysAddr)a1;
249 NKern::ThreadEnterCS();
250 r=Epoc::FreePhysicalRam(pa,size);
251 NKern::ThreadLeaveCS();
254 case RShadow::EControlClaimPhys:
257 TPhysAddr pa=(TPhysAddr)a1;
259 NKern::ThreadEnterCS();
260 r=Epoc::ClaimPhysicalRam(pa,size);
261 NKern::ThreadLeaveCS();
265 // GetMemoryArchitecture
266 case RShadow::EControlGetMemoryArchitecture:
268 TCpu* cpu = (TCpu*) a1;
269 TUint* flags = (TUint*) a2;
271 #if defined(__CPU_ARM)
273 *flags = GetControlRegister();
274 #elif defined(__CPU_X86)
287 case RShadow::EControlGetMemModelInfo:
294 #if defined(__MEMMODEL_MULTIPLE__) || defined(__MEMMODEL_FLEXIBLE__)
295 numPds = KMaxNumberOfPageDirectories;
301 pageTable = KPageTableBase;
303 kumemput(a1, &pageTable, sizeof(TUint));
304 kumemput(a2, &numPds, sizeof(TUint));
310 case RShadow::EControlGetPdInfo:
313 kumemget(&pd, a1, sizeof(TUint));
320 #if defined(__MEMMODEL_MOVING__)
322 if (pd==KGlobalPageDirectory)
324 pdSize=KPageDirectorySize;
325 pdBase=KPageDirectoryBase;
329 #elif defined(__MEMMODEL_MULTIPLE__) || defined(__MEMMODEL_FLEXIBLE__)
332 TUint ttbcr = KPsudoX86TTBCR;
334 TUint ttbcr = KPageDirectorySize >> GetTTBCR();
337 if (pd==KGlobalPageDirectory)
339 pdSize=KPageDirectorySize - ttbcr;
340 pdBase=KPageDirectoryBase + ttbcr*4;
341 r = ttbcr & KPageOffsetMask;
346 pdBase=KPageDirectoryBase + pd * KPageDirectorySize * 4;
348 TPhysAddr phys=Epoc::LinearToPhysical((TLinAddr)pdBase);
349 r = (phys==KPhysAddrInvalid) ? KErrNoPageTable : KErrNone;
354 if ((r & KErrNoPageTable) == 0)
356 kumemput(a1, &pdSize, sizeof(TUint));
357 kumemput(a2, &pdBase, sizeof(TUint));
370 TInt DShadow::Request(TInt /*aFunction*/, TAny* /*a1*/, TAny* /*a2*/)
373 return KErrNotSupported;