os/kernelhwsrv/kerneltest/e32test/mmu/t_chunk4.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/mmu/t_chunk4.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,485 @@
     1.4 +// Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// e32test\mmu\t_chunk4.cpp
    1.18 +// Overview:
    1.19 +// Test disconnected chunks
    1.20 +// API Information:
    1.21 +// RChunk, CBase
    1.22 +// Details:
    1.23 +// - Check Allocate/Commit/Decommit methods on local disconnected chunk and write/read 
    1.24 +// access to both committed and uncommitted regions.
    1.25 +// - Check IPC that involves local disconnected chunk and verify results are as expected.
    1.26 +// Platforms/Drives/Compatibility:
    1.27 +// All.
    1.28 +// Assumptions/Requirement/Pre-requisites:
    1.29 +// Failures and causes:
    1.30 +// Base Port information:
    1.31 +// 
    1.32 +//
    1.33 +
    1.34 +#define __E32TEST_EXTENSION__
    1.35 +#include <e32test.h>
    1.36 +#include "u32std.h"
    1.37 +#include "mmudetect.h"
    1.38 +#include "../misc/prbs.h"
    1.39 +#include "d_memorytest.h"
    1.40 +#include "freeram.h"
    1.41 +
    1.42 +RTest test(_L("T_CHUNK4"));
    1.43 +
    1.44 +RMemoryTestLdd TestLdd;
    1.45 +
    1.46 +TUint RndSeed[2];
    1.47 +
    1.48 +class CChunk : public CBase
    1.49 +	{
    1.50 +public:
    1.51 +	static CChunk* New(TInt aMaxSize);
    1.52 +public:
    1.53 +	virtual ~CChunk();
    1.54 +	TInt Verify();
    1.55 +	TInt Commit(TInt anOffset, TInt aSize);
    1.56 +	TInt Allocate(TInt aSize);
    1.57 +	TInt Decommit(TInt anOffset, TInt aSize);
    1.58 +	void CheckL(volatile TUint* aPtr);
    1.59 +	TInt AddPages(TInt anOffset, TInt aSize);
    1.60 +	TInt RemovePages(TInt anOffset, TInt aSize);
    1.61 +public:
    1.62 +	RChunk iChunk;
    1.63 +	TUint8* iPageInfo;
    1.64 +	TInt iPageSize;
    1.65 +	TInt iMaxSize;
    1.66 +	TInt iNumPages;
    1.67 +	};
    1.68 +
    1.69 +CChunk* CChunk::New(TInt aMaxSize)
    1.70 +	{
    1.71 +	CChunk* pC=new CChunk;
    1.72 +	if (pC)
    1.73 +		{
    1.74 +		TInt p;
    1.75 +		UserHal::PageSizeInBytes(p);
    1.76 +		pC->iPageSize=p;
    1.77 +		pC->iMaxSize=aMaxSize;
    1.78 +		TInt n=aMaxSize/p;
    1.79 +		pC->iNumPages=n;
    1.80 +		TInt r=pC->iChunk.CreateDisconnectedLocal(0,0,aMaxSize);
    1.81 +		if (r==KErrNone)
    1.82 +			{
    1.83 +			TUint8* pI=(TUint8*)User::Alloc(n);
    1.84 +			if (pI)
    1.85 +				{
    1.86 +				pC->iPageInfo=pI;
    1.87 +				Mem::FillZ(pI,n);
    1.88 +				}
    1.89 +			else
    1.90 +				r=KErrNoMemory;
    1.91 +			}
    1.92 +		if (r!=KErrNone)
    1.93 +			{
    1.94 +			delete pC;
    1.95 +			pC=NULL;
    1.96 +			}
    1.97 +		}
    1.98 +	return pC;
    1.99 +	}
   1.100 +
   1.101 +CChunk::~CChunk()
   1.102 +	{
   1.103 +	delete iPageInfo;
   1.104 +	iChunk.Close();
   1.105 +	}
   1.106 +
   1.107 +void CChunk::CheckL(volatile TUint* aPtr)
   1.108 +	{
   1.109 +	TUint x=*aPtr;
   1.110 +	*aPtr=x;
   1.111 +	}
   1.112 +
   1.113 +TInt CChunk::Verify()
   1.114 +	{
   1.115 +//	test.Getch();
   1.116 +	TInt i;
   1.117 +	TUint8* base=iChunk.Base();
   1.118 +	for (i=0; i<iNumPages; i++)
   1.119 +		{
   1.120 +		volatile TUint* pX=(volatile TUint*)base;
   1.121 +		TInt r=TestLdd.ReadWriteMemory((TAny*)pX);
   1.122 +		TUint8 info=iPageInfo[i];
   1.123 +		if (info==0 && r==KErrNone)
   1.124 +			return KErrGeneral;
   1.125 +		if (info!=0 && r!=KErrNone)
   1.126 +			return KErrAccessDenied;
   1.127 +		if (info!=0)
   1.128 +			{
   1.129 +			TUint seed[2];
   1.130 +			seed[0]=info<<8;
   1.131 +			seed[1]=0;
   1.132 +			TInt j;
   1.133 +			for (j=0; j<iPageSize; j+=4)
   1.134 +				{
   1.135 +				if (*pX++!=Random(seed))
   1.136 +					return KErrArgument;
   1.137 +				}
   1.138 +			}
   1.139 +		base+=iPageSize;
   1.140 +		}
   1.141 +	return KErrNone;
   1.142 +	}
   1.143 +
   1.144 +TInt CChunk::AddPages(TInt anOffset, TInt aSize)
   1.145 +	{
   1.146 +	TInt i=anOffset/iPageSize;
   1.147 +	TInt n=aSize/iPageSize;
   1.148 +	TInt e=i+n;
   1.149 +	TUint* p=(TUint*)(iChunk.Base()+anOffset);
   1.150 +	for (; i<e; i++)
   1.151 +		{
   1.152 +		TUint8 s=(TUint8)Random(RndSeed);
   1.153 +		if (s==0)
   1.154 +			s=1;
   1.155 +		iPageInfo[i]=s;
   1.156 +		TUint seed[2];
   1.157 +		seed[0]=s<<8;
   1.158 +		seed[1]=0;
   1.159 +		TInt j;
   1.160 +		for (j=0; j<iPageSize; j+=4)
   1.161 +			{
   1.162 +			*p++=Random(seed);
   1.163 +			}
   1.164 +		}
   1.165 +	return KErrNone;
   1.166 +	}
   1.167 +
   1.168 +TInt CChunk::RemovePages(TInt anOffset, TInt aSize)
   1.169 +	{
   1.170 +	TInt i=anOffset/iPageSize;
   1.171 +	TInt n=aSize/iPageSize;
   1.172 +	TInt e=i+n;
   1.173 +	for (; i<e; i++)
   1.174 +		iPageInfo[i]=0;
   1.175 +	return KErrNone;
   1.176 +	}
   1.177 +
   1.178 +TInt CChunk::Commit(TInt anOffset, TInt aSize)
   1.179 +	{
   1.180 +	TInt r=iChunk.Commit(anOffset,aSize);
   1.181 +	if (r==KErrNone)
   1.182 +		{
   1.183 +		AddPages(anOffset,aSize);
   1.184 +		}
   1.185 +	return r;
   1.186 +	}
   1.187 +
   1.188 +TInt CChunk::Allocate(TInt aSize)
   1.189 +	{
   1.190 +	TInt r=iChunk.Allocate(aSize);
   1.191 +	if (r>=0)
   1.192 +		{
   1.193 +		AddPages(r,aSize);
   1.194 +		}
   1.195 +	return r;
   1.196 +	}
   1.197 +
   1.198 +TInt CChunk::Decommit(TInt anOffset, TInt aSize)
   1.199 +	{
   1.200 +	TInt r=iChunk.Decommit(anOffset,aSize);
   1.201 +	if (r==KErrNone)
   1.202 +		{
   1.203 +		RemovePages(anOffset,aSize);
   1.204 +		}
   1.205 +	return r;
   1.206 +	}
   1.207 +
   1.208 +// Stuff to test remote writes
   1.209 +_LIT(KServerName, "Chunk4Test");
   1.210 +
   1.211 +class CTestSession : public CSession2
   1.212 +	{
   1.213 +public:
   1.214 +	CTestSession();
   1.215 +	virtual void ServiceL(const RMessage2& aMessage);
   1.216 +	};
   1.217 +
   1.218 +class CTestServer : public CServer2
   1.219 +	{
   1.220 +public:
   1.221 +	CTestServer();
   1.222 +	virtual CSession2* NewSessionL(const TVersion& aVersion, const RMessage2& aMessage) const;
   1.223 +	};
   1.224 +
   1.225 +class RTestSession : public RSessionBase
   1.226 +	{
   1.227 +public:
   1.228 +	enum {ETestIpc, ETestStop};
   1.229 +	TInt Connect();
   1.230 +	void Stop();
   1.231 +	TInt TestRemoteWrite(TInt aLength, TInt anOffset1, TInt anOffset2, TInt anOffset3, TInt anOffset4);
   1.232 +	TInt IpcWrite(TDes8* aRemoteDest, const TAny* aLocalSrc, TInt aOffset);
   1.233 +	};
   1.234 +
   1.235 +CTestSession::CTestSession()
   1.236 +	{
   1.237 +	}
   1.238 +
   1.239 +void CTestSession::ServiceL(const RMessage2& aMessage)
   1.240 +	{
   1.241 +	switch (aMessage.Function())
   1.242 +		{
   1.243 +		case RTestSession::ETestIpc:
   1.244 +			{
   1.245 +			const TDesC8& localSrc = *(const TDesC8*)aMessage.Ptr1();
   1.246 +			TInt offset = aMessage.Int2();
   1.247 +			TInt r = aMessage.Write(0, localSrc, offset);
   1.248 +			aMessage.Complete(r);
   1.249 +			break;
   1.250 +			}
   1.251 +		case RTestSession::ETestStop:
   1.252 +			CActiveScheduler::Stop();
   1.253 +			break;
   1.254 +		default:
   1.255 +			User::Invariant();
   1.256 +		}
   1.257 +	}
   1.258 +
   1.259 +CTestServer::CTestServer()
   1.260 +	: CServer2(0)
   1.261 +	{
   1.262 +	}
   1.263 +
   1.264 +CSession2* CTestServer::NewSessionL(const TVersion& /*aVersion*/, const RMessage2& /*aMessage*/) const
   1.265 +	{
   1.266 +	return new (ELeave) CTestSession;
   1.267 +	}
   1.268 +
   1.269 +TInt ServerThread(TAny*)
   1.270 +	{
   1.271 +	CActiveScheduler* pA = new CActiveScheduler;
   1.272 +	CTestServer* pS = new CTestServer;
   1.273 +	if (!pA || !pS)
   1.274 +		return KErrNoMemory;
   1.275 +	CActiveScheduler::Install(pA);
   1.276 +	TInt r = pS->Start(KServerName);
   1.277 +	if (r!=KErrNone)
   1.278 +		return r;
   1.279 +	RThread::Rendezvous(KErrNone);
   1.280 +	CActiveScheduler::Start();
   1.281 +	return KErrNone;
   1.282 +	}
   1.283 +
   1.284 +TInt RTestSession::Connect()
   1.285 +	{
   1.286 +	RThread t;
   1.287 +	TInt r = t.Create(KServerName, ServerThread, 0x1000, 0x1000, 0x10000, NULL);
   1.288 +	test(r==KErrNone);
   1.289 +	t.SetPriority(EPriorityMore);
   1.290 +	TRequestStatus s;
   1.291 +	t.Rendezvous(s);
   1.292 +	test(s==KRequestPending);
   1.293 +	t.Resume();
   1.294 +	User::WaitForRequest(s);
   1.295 +	test(t.ExitType()==EExitPending);
   1.296 +	test(s==KErrNone);
   1.297 +	t.Close();
   1.298 +	r = CreateSession(KServerName, TVersion());
   1.299 +	return r;
   1.300 +	}
   1.301 +
   1.302 +void RTestSession::Stop()
   1.303 +	{
   1.304 +	TInt r = SendReceive(ETestStop);
   1.305 +	test(r==KErrServerTerminated);
   1.306 +	Close();
   1.307 +	}
   1.308 +
   1.309 +TInt RTestSession::IpcWrite(TDes8* aRemoteDest, const TAny* aLocalSrc, TInt aOffset)
   1.310 +	{
   1.311 +	return SendReceive(ETestIpc, TIpcArgs(aRemoteDest, aLocalSrc, aOffset));
   1.312 +	}
   1.313 +
   1.314 +TInt RTestSession::TestRemoteWrite(TInt aLength, TInt anOffset1, TInt anOffset2, TInt anOffset3, TInt anOffset4)
   1.315 +	{
   1.316 +	test.Printf(_L("%x %x %x %x %x\n"),aLength,anOffset1,anOffset2,anOffset3,anOffset4);
   1.317 +
   1.318 +	TBool ptr=(anOffset1>=0);
   1.319 +	TDes8* pDes;
   1.320 +	TUint8* pData;
   1.321 +	RChunk c;
   1.322 +	TInt r=c.CreateDisconnectedLocal(0,0,0x800000);
   1.323 +	test(r==KErrNone);
   1.324 +	TUint8* base=c.Base();
   1.325 +	if (ptr)
   1.326 +		{
   1.327 +		r=c.Commit(anOffset1,(TInt)sizeof(TPtr8));
   1.328 +		test(r==KErrNone);
   1.329 +		pDes=(TDes8*)(base+anOffset1);
   1.330 +		Mem::FillZ(pDes,(TInt)sizeof(TPtr8));
   1.331 +		r=c.Commit(anOffset2,aLength);
   1.332 +		test(r==KErrNone);
   1.333 +		pData=base+anOffset2;
   1.334 +		Mem::FillZ(pData,aLength);
   1.335 +		new(pDes) TPtr8(pData,0,aLength);
   1.336 +		test(pDes->Length()==0);
   1.337 +		test(pDes->MaxLength()==aLength);
   1.338 +		test(pDes->Ptr()==pData);
   1.339 +		}
   1.340 +	else
   1.341 +		{
   1.342 +		TInt len=(TInt)sizeof(TDes8)+aLength;
   1.343 +		r=c.Commit(anOffset2,len);
   1.344 +		test(r==KErrNone);
   1.345 +		pDes=(TDes8*)(base+anOffset2);
   1.346 +		Mem::FillZ(pDes,len);
   1.347 +		pData=base+anOffset2+(TInt)sizeof(TDes8);
   1.348 +		new(pDes) TBuf8<1>;
   1.349 +		((TInt*)pDes)[1]=aLength;
   1.350 +		test(pDes->Length()==0);
   1.351 +		test(pDes->MaxLength()==aLength);
   1.352 +		test(pDes->Ptr()==pData);
   1.353 +		}
   1.354 +	TInt slen=aLength-anOffset3;
   1.355 +	TUint8* pS=(TUint8*)User::Alloc(aLength);
   1.356 +	test(pS!=NULL);
   1.357 +	Mem::FillZ(pS,aLength);
   1.358 +	TPtrC8 src(pS+anOffset3,slen);
   1.359 +	TInt i;
   1.360 +	for (i=anOffset3; i<aLength; i++)
   1.361 +		pS[i]=(TUint8)Random(RndSeed);
   1.362 +	if (anOffset4>=0)
   1.363 +		c.Decommit(anOffset4,0x1000);
   1.364 +	r = IpcWrite(pDes, &src, anOffset3);
   1.365 +	if (r==KErrNone)
   1.366 +		{
   1.367 +		TPtrC8 tsrc(pS,aLength);
   1.368 +		if (*pDes!=tsrc)
   1.369 +			r=KErrCorrupt;
   1.370 +		}
   1.371 +	User::Free(pS);
   1.372 +	c.Close();
   1.373 +	test.Printf(_L("Return value %d\n"),r);
   1.374 +	return r;
   1.375 +	}
   1.376 +
   1.377 +GLDEF_C TInt E32Main()
   1.378 +	{
   1.379 +	RndSeed[0]=0xddb3d743;
   1.380 +	RndSeed[1]=0;
   1.381 +
   1.382 +	test.Title();
   1.383 +	if (!HaveVirtMem())
   1.384 +		{
   1.385 +		test.Printf(_L("This test requires an MMU\n"));
   1.386 +		return KErrNone;
   1.387 +		}
   1.388 +	test.Start(_L("Testing Disconnected Chunks"));
   1.389 +
   1.390 +	test.Printf(_L("Load test LDD\n"));
   1.391 +	test(TestLdd.Open()==KErrNone);
   1.392 +
   1.393 +	CChunk* pC=CChunk::New(0x800000);
   1.394 +	test(pC!=NULL);
   1.395 +	TInt free=FreeRam();
   1.396 +	test.Printf(_L("Free RAM %08x\n"),free);
   1.397 +	test(pC->Verify()==KErrNone);
   1.398 +	test.Printf(_L("Commit 0+0x1000\n"));
   1.399 +	test(pC->Commit(0,0x1000)==KErrNone);
   1.400 +	test(pC->Verify()==KErrNone);
   1.401 +	test.Printf(_L("Commit 0+0x1000 (again)\n"));
   1.402 +	test(pC->Commit(0,0x1000)==KErrAlreadyExists);
   1.403 +	test(pC->Verify()==KErrNone);
   1.404 +	test.Printf(_L("Commit 0x3000+0x1000\n"));
   1.405 +	test(pC->Commit(0x3000,0x1000)==KErrNone);
   1.406 +	test(pC->Verify()==KErrNone);
   1.407 +	test.Printf(_L("Commit 0x2000+0x3000 (overlaps previous)\n"));
   1.408 +	test(pC->Commit(0x2000,0x3000)==KErrAlreadyExists);
   1.409 +	test(pC->Verify()==KErrNone);
   1.410 +	test.Printf(_L("Allocate 0x5000\n"));
   1.411 +	test(pC->Allocate(0x5000)==0x4000);
   1.412 +	test(pC->Verify()==KErrNone);
   1.413 +	test.Printf(_L("Allocate 0x1000\n"));
   1.414 +	test(pC->Allocate(0x1000)==0x1000);
   1.415 +	test(pC->Verify()==KErrNone);
   1.416 +	test.Printf(_L("Decommit 0+0x4000\n"));
   1.417 +	test(pC->Decommit(0,0x4000)==KErrNone);
   1.418 +	test(pC->Verify()==KErrNone);
   1.419 +	test.Printf(_L("Decommit 0+0x4000 (again)\n"));
   1.420 +	test(pC->Decommit(0,0x4000)==KErrNone);
   1.421 +	test(pC->Verify()==KErrNone);
   1.422 +	test.Printf(_L("Allocate 0x4000\n"));
   1.423 +	test(pC->Allocate(0x4000)==0x0000);
   1.424 +	test(pC->Verify()==KErrNone);
   1.425 +	test.Printf(_L("Decommit 0+0x10000\n"));
   1.426 +	test(pC->Decommit(0,0x10000)==KErrNone);
   1.427 +	test(pC->Verify()==KErrNone);
   1.428 +	test.Printf(_L("Check Free RAM\n"));
   1.429 +	test(FreeRam()==free);
   1.430 +
   1.431 +	test.Printf(_L("Commit 0x700000+0x10000\n"));
   1.432 +	test(pC->Commit(0x700000,0x10000)==KErrNone);
   1.433 +	test(pC->Verify()==KErrNone);
   1.434 +	test.Printf(_L("Allocate 0x4000\n"));
   1.435 +	test(pC->Allocate(0x4000)==0x0000);
   1.436 +	test(pC->Verify()==KErrNone);
   1.437 +	test.Printf(_L("Decommit 0+0x800000\n"));
   1.438 +	test(pC->Decommit(0,0x800000)==KErrNone);
   1.439 +	test(pC->Verify()==KErrNone);
   1.440 +	test.Printf(_L("Check Free RAM\n"));
   1.441 +	test(FreeRam()==free);
   1.442 +
   1.443 +	delete pC;
   1.444 +
   1.445 +	// Test decommiting from a chunk which has no memory commited
   1.446 +	// in the first megabyte. On the moving memory model, such
   1.447 +	// chunks have a non-zero iHomeRegionOffset value and has been
   1.448 +	// the cause of defects (DEF121857)
   1.449 +	test.Printf(_L("Create new chunk\n"));
   1.450 +	pC=CChunk::New(0x800000);
   1.451 +	test(pC!=NULL);
   1.452 +	test.Printf(_L("Commit 0x100000+0x3000\n"));
   1.453 +	test(pC->Commit(0x100000,0x3000)==KErrNone);
   1.454 +	test(pC->Verify()==KErrNone);
   1.455 +	test.Printf(_L("Decommit 0+0x101000\n"));
   1.456 +	test(pC->Decommit(0,0x101000)==KErrNone);
   1.457 +	test(pC->Verify()==KErrNone);
   1.458 +	test.Printf(_L("Decommit 0x101000+0x1000\n"));
   1.459 +	test(pC->Decommit(0x101000,0x1000)==KErrNone);
   1.460 +	test(pC->Verify()==KErrNone);
   1.461 +	test.Printf(_L("Decommit 0x100000+0x100000\n"));
   1.462 +	test(pC->Decommit(0x100000,0x100000)==KErrNone);
   1.463 +	test(pC->Verify()==KErrNone);
   1.464 +	delete pC;
   1.465 +
   1.466 +	test.Next(_L("Testing RThread::Write to disconnected chunks"));
   1.467 +	RTestSession ts;
   1.468 +	TInt r = ts.Connect();
   1.469 +	test(r==KErrNone);
   1.470 +	test(ts.TestRemoteWrite(64,0,0x3000,0,-1)==KErrNone);
   1.471 +	test(ts.TestRemoteWrite(64,0xffc,0x8000,0,-1)==KErrNone);
   1.472 +	test(ts.TestRemoteWrite(256,0xffc,0x7f80,0,-1)==KErrNone);
   1.473 +	test(ts.TestRemoteWrite(256,0xffc,0x7f80,128,-1)==KErrNone);
   1.474 +	test(ts.TestRemoteWrite(0x10000,0xffc,0x707f80,0,-1)==KErrNone);
   1.475 +	test(ts.TestRemoteWrite(0x10000,0xffc,0x707f80,0x2000,-1)==KErrNone);
   1.476 +	test(ts.TestRemoteWrite(64,-1,0x3000,0,-1)==KErrNone);
   1.477 +	test(ts.TestRemoteWrite(0x10000,-1,0xfff00,0x2000,-1)==KErrNone);
   1.478 +	test(ts.TestRemoteWrite(256,0xffc,0x7f80,16,0)==KErrBadDescriptor);
   1.479 +	test(ts.TestRemoteWrite(256,0xffc,0x7f80,16,0x1000)==KErrBadDescriptor);
   1.480 +	test(ts.TestRemoteWrite(256,0xffc,0x7f80,16,0x7000)==KErrBadDescriptor);
   1.481 +	test(ts.TestRemoteWrite(256,0xffc,0x7f80,16,0x8000)==KErrBadDescriptor);
   1.482 +	test(ts.TestRemoteWrite(0x10000,0xffc,0x707f80,0x2000,0x710000)==KErrBadDescriptor);
   1.483 +	test(ts.TestRemoteWrite(0x10000,-1,0xfff00,0x1000,0x100000)==KErrBadDescriptor);
   1.484 +	ts.Stop();
   1.485 +
   1.486 +	test.End();
   1.487 +	return 0;
   1.488 +	}