kerneltest/e32test/mmu/d_shadow.cpp
changeset 0 a41df078684a
child 31 56f325a607ea
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of the License "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // e32test\mmu\d_shadow.cpp
       
    15 // LDD for testing ROM shadowing
       
    16 // 
       
    17 //
       
    18 
       
    19 #include <kernel/kern_priv.h>
       
    20 #include "platform.h"
       
    21 #include <kernel/cache.h>
       
    22 #include "d_shadow.h"
       
    23 
       
    24 const TInt KMajorVersionNumber=0;
       
    25 const TInt KMinorVersionNumber=1;
       
    26 const TInt KBuildVersionNumber=1;
       
    27 
       
    28 _LIT(KLddName,"Shadow");
       
    29 
       
    30 class DShadow;
       
    31 
       
    32 class DShadowFactory : public DLogicalDevice
       
    33 //
       
    34 // Shadow ROM LDD factory
       
    35 //
       
    36 	{
       
    37 public:
       
    38 	DShadowFactory();
       
    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
       
    42 	};
       
    43 
       
    44 class DShadow : public DLogicalChannelBase
       
    45 //
       
    46 // Shadow ROM logical channel
       
    47 //
       
    48 	{
       
    49 public:
       
    50 	DShadow();
       
    51 	~DShadow();
       
    52 protected:
       
    53 	virtual TInt DoCreate(TInt aUnit, const TDesC8* anInfo, const TVersion& aVer);
       
    54 	virtual TInt Request(TInt aFunction, TAny* a1, TAny* a2);
       
    55 	};
       
    56 
       
    57 DECLARE_STANDARD_LDD()
       
    58 	{
       
    59     return new DShadowFactory;
       
    60     }
       
    61 
       
    62 DShadowFactory::DShadowFactory()
       
    63 //
       
    64 // Constructor
       
    65 //
       
    66     {
       
    67     iVersion=TVersion(KMajorVersionNumber,KMinorVersionNumber,KBuildVersionNumber);
       
    68     //iParseMask=0;//No units, no info, no PDD
       
    69     //iUnitsMask=0;//Only one thing
       
    70     }
       
    71 
       
    72 TInt DShadowFactory::Create(DLogicalChannelBase*& aChannel)
       
    73 //
       
    74 // Create a new DShadow on this logical device
       
    75 //
       
    76     {
       
    77 	aChannel=new DShadow;
       
    78 	return aChannel?KErrNone:KErrNoMemory;
       
    79     }
       
    80 
       
    81 TInt DShadowFactory::Install()
       
    82 //
       
    83 // Install the LDD - overriding pure virtual
       
    84 //
       
    85     {
       
    86     return SetName(&KLddName);
       
    87     }
       
    88 
       
    89 void DShadowFactory::GetCaps(TDes8& aDes) const
       
    90 //
       
    91 // Get capabilities - overriding pure virtual
       
    92 //
       
    93     {
       
    94     TCapsShadowV01 b;
       
    95     b.iVersion=TVersion(KMajorVersionNumber,KMinorVersionNumber,KBuildVersionNumber);
       
    96     Kern::InfoCopy(aDes,(TUint8*)&b,sizeof(b));
       
    97     }
       
    98 
       
    99 DShadow::DShadow()
       
   100 //
       
   101 // Constructor
       
   102 //
       
   103     {
       
   104     }
       
   105 
       
   106 TInt DShadow::DoCreate(TInt /*aUnit*/, const TDesC8* /*anInfo*/, const TVersion& aVer)
       
   107 //
       
   108 // Create channel
       
   109 //
       
   110     {
       
   111 
       
   112     if (!Kern::QueryVersionSupported(TVersion(KMajorVersionNumber,KMinorVersionNumber,KBuildVersionNumber),aVer))
       
   113     	return KErrNotSupported;
       
   114 	return KErrNone;
       
   115 	}
       
   116 
       
   117 DShadow::~DShadow()
       
   118 //
       
   119 // Destructor
       
   120 //
       
   121     {
       
   122     }
       
   123 
       
   124 #ifdef __MARM__
       
   125 extern TInt DoRead(TAny*);
       
   126 extern TInt GetMmuId();
       
   127 extern TInt GetCacheType();
       
   128 extern TUint GetTTBCR();
       
   129 extern TUint GetControlRegister();
       
   130 #endif
       
   131 
       
   132 LOCAL_C TInt KernStackSize()
       
   133 	{
       
   134 	TRomEntry* pE=(TRomEntry*)Kern::SuperPage().iPrimaryEntry;
       
   135 	TRomImageHeader* pI=(TRomImageHeader*)pE->iAddressLin;
       
   136 	return pI->iStackSize;
       
   137 	}
       
   138 
       
   139 LOCAL_C TInt MeasureKernStackUse()
       
   140 	{
       
   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);
       
   146 	return (TInt)used;
       
   147 	}
       
   148 
       
   149 #if !defined(__WINS__)
       
   150 TInt DShadow::Request(TInt aFunction, TAny* a1, TAny* a2)
       
   151 	{
       
   152 	TInt pageSize=Kern::RoundToPageSize(1);
       
   153 	TInt r=KErrNone;
       
   154 	switch (aFunction)
       
   155 		{
       
   156 		case RShadow::EControlAllocShadow:
       
   157 			NKern::ThreadEnterCS();
       
   158 			r=Epoc::AllocShadowPage(TLinAddr(a1));
       
   159 			NKern::ThreadLeaveCS();
       
   160 			break;
       
   161 		case RShadow::EControlFreeShadow:
       
   162 			NKern::ThreadEnterCS();
       
   163 			r=Epoc::FreeShadowPage(TLinAddr(a1));
       
   164 			NKern::ThreadLeaveCS();
       
   165 			break;
       
   166 
       
   167 		case RShadow::EControlWriteShadow: //copy 4KB(page size) data to shadow page
       
   168 			{
       
   169 			NKern::ThreadEnterCS();
       
   170 			void* alloc = Kern::Alloc(pageSize); //CopyToShadowMemory assumes Kernel adress space. Copy here first
       
   171 			if (alloc)
       
   172 				{
       
   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);
       
   177 				Kern::Free(alloc);
       
   178 				}
       
   179 			else
       
   180 				r = KErrNoMemory;
       
   181 			
       
   182 			NKern::ThreadLeaveCS();
       
   183 			}
       
   184 			break;
       
   185 		case RShadow::EControlFreezeShadow:
       
   186 			NKern::ThreadEnterCS();
       
   187 			r=Epoc::FreezeShadowPage(TLinAddr(a1));
       
   188 			NKern::ThreadLeaveCS();
       
   189 			break;
       
   190 		case RShadow::EControlSetPriority:
       
   191 			{
       
   192 			TInt h=(TInt)a1;
       
   193 			TInt p=(TInt)a2;
       
   194 			NKern::LockSystem();
       
   195 			DThread *pT=(DThread*)Kern::CurrentThread().ObjectFromHandle(h);
       
   196 			pT->SetThreadPriority(p);
       
   197 			NKern::UnlockSystem();
       
   198 			break;
       
   199 			}
       
   200 #ifdef __MARM__
       
   201 		case RShadow::EControlRead:
       
   202 			r=DoRead(a1);
       
   203 			break;
       
   204 		case RShadow::EControlMmuId:
       
   205 			r=GetMmuId();
       
   206 			break;
       
   207 		case RShadow::EControlCacheType:
       
   208 			r=GetCacheType();
       
   209 			break;
       
   210 #endif
       
   211 		case RShadow::EControlMeasureKernStackUse:
       
   212 			r=MeasureKernStackUse();
       
   213 			break;
       
   214 		case RShadow::EControlMeasureKernHeapFree:
       
   215 			r=KErrNotSupported;
       
   216 			break;
       
   217 		case RShadow::EControlCallFunction:
       
   218 			{
       
   219 			TThreadFunction f=(TThreadFunction)a1;
       
   220 			r=(*f)(a2);
       
   221 			break;
       
   222 			}
       
   223 		case RShadow::EControlAllocPhys:
       
   224 			{
       
   225 			TInt size=(TInt)a1;
       
   226 			TInt align=(TInt)a2;
       
   227 			TPhysAddr pa;
       
   228 			r=Epoc::AllocPhysicalRam(size,pa,align);
       
   229 			if (r==KErrNone)
       
   230 				{
       
   231 				if (pa&0x0f)
       
   232 					r=KErrCorrupt;
       
   233 				else
       
   234 					r=pa>>4;
       
   235 				}
       
   236 			break;
       
   237 			}
       
   238 		case RShadow::EControlFreePhys:
       
   239 			{
       
   240 			TPhysAddr pa=(TPhysAddr)a1;
       
   241 			TInt size=(TInt)a2;
       
   242 			r=Epoc::FreePhysicalRam(pa,size);
       
   243 			break;
       
   244 			}
       
   245 		case RShadow::EControlClaimPhys:
       
   246 			{
       
   247 			TPhysAddr pa=(TPhysAddr)a1;
       
   248 			TInt size=(TInt)a2;
       
   249 			r=Epoc::ClaimPhysicalRam(pa,size);
       
   250 			break;
       
   251 			}
       
   252 			
       
   253 		// GetMemoryArchitecture
       
   254 		case RShadow::EControlGetMemoryArchitecture:
       
   255 			{
       
   256 			TCpu* cpu = (TCpu*) a1;
       
   257 			TUint* flags = (TUint*) a2;
       
   258 			
       
   259 #if defined(__CPU_ARM)
       
   260 			*cpu=ECpuArm;
       
   261 			*flags = GetControlRegister();
       
   262 #elif defined(__CPU_X86)
       
   263 			*cpu=ECpuX86;
       
   264 			*flags =0;
       
   265 #else
       
   266 			*cpu=ECpuUnknown;
       
   267 			*flags =0;
       
   268 #endif
       
   269 			
       
   270 			break;
       
   271 			}
       
   272 			
       
   273 			
       
   274 		// GetMemModelInfo
       
   275 		case RShadow::EControlGetMemModelInfo:
       
   276 			{
       
   277 			TUint pageTable;
       
   278 			TUint numPds;
       
   279 						 
       
   280 #ifdef __EPOC32__
       
   281 
       
   282 #if defined(__MEMMODEL_MULTIPLE__) || defined(__MEMMODEL_FLEXIBLE__)
       
   283 			numPds = KMaxNumberOfPageDirectories;
       
   284 #else
       
   285 			numPds = 0;								
       
   286 #endif			
       
   287 			r = KMemoryModel;
       
   288 #endif
       
   289 			pageTable = KPageTableBase;			
       
   290 
       
   291 			kumemput(a1, &pageTable, sizeof(TUint));
       
   292 			kumemput(a2, &numPds, sizeof(TUint));
       
   293 			break;
       
   294 			}
       
   295 			
       
   296 			
       
   297 		// GetPdInfo
       
   298 		case RShadow::EControlGetPdInfo:
       
   299 			{
       
   300 			TUint pd;
       
   301 			kumemget(&pd, a1, sizeof(TUint));
       
   302 
       
   303 			TUint pdSize;
       
   304 			TUint pdBase;
       
   305 
       
   306 			r=KErrNoPageTable;
       
   307 
       
   308 #if defined(__MEMMODEL_MOVING__)
       
   309 
       
   310 			if (pd==KGlobalPageDirectory)
       
   311 				{
       
   312 				pdSize=KPageDirectorySize;
       
   313 				pdBase=KPageDirectoryBase;
       
   314 				r = KErrNone;
       
   315 				}
       
   316 
       
   317 #elif defined(__MEMMODEL_MULTIPLE__) || defined(__MEMMODEL_FLEXIBLE__)
       
   318 
       
   319 #ifdef __CPU_X86
       
   320 			TUint ttbcr = KPsudoX86TTBCR;
       
   321 #else
       
   322 			TUint ttbcr = KPageDirectorySize >> GetTTBCR();
       
   323 #endif
       
   324 
       
   325 			if (pd==KGlobalPageDirectory)
       
   326 				{
       
   327 				pdSize=KPageDirectorySize - ttbcr;
       
   328 				pdBase=KPageDirectoryBase + ttbcr*4;
       
   329 				r = ttbcr & KPageOffsetMask;
       
   330 				}
       
   331 			else
       
   332 				{
       
   333 				pdSize = ttbcr;
       
   334 				pdBase=KPageDirectoryBase + pd * KPageDirectorySize * 4;
       
   335 
       
   336 				TPhysAddr phys=Epoc::LinearToPhysical((TLinAddr)pdBase);				
       
   337 				r = (phys==KPhysAddrInvalid) ? KErrNoPageTable : KErrNone;
       
   338 				}
       
   339 			
       
   340 #endif  //memmodel
       
   341 
       
   342 			if ((r & KErrNoPageTable) == 0)
       
   343 				{
       
   344 				kumemput(a1, &pdSize, sizeof(TUint));
       
   345 				kumemput(a2, &pdBase, sizeof(TUint));
       
   346 				}
       
   347 			
       
   348 			break;	
       
   349 			}
       
   350 			
       
   351 		default:
       
   352 			r=KErrNotSupported;
       
   353 			break;
       
   354 		}
       
   355 	return r;
       
   356 	}
       
   357 #else
       
   358 TInt DShadow::Request(TInt /*aFunction*/, TAny* /*a1*/, TAny* /*a2*/)
       
   359 	{
       
   360 
       
   361 	return KErrNotSupported;
       
   362 	}
       
   363 #endif
       
   364