kerneltest/e32test/mmu/t_imb.cpp
changeset 0 a41df078684a
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 1995-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\t_imb.cpp
       
    15 // Overview:
       
    16 // Test the RChunk Create Local Code and Instruction Memory Barrier 
       
    17 // control interface.
       
    18 // API Information:
       
    19 // RChunk::CreateLocalCode & User::IMB_Range
       
    20 // Details:
       
    21 // - Create a code chunk, write a small test function to the chunk, use
       
    22 // User::IMB_Range to prepare the virtual address range for code execution.
       
    23 // - Verify the success and failure of the IMB with various processes and with
       
    24 // different base and size values.
       
    25 // Platforms/Drives/Compatibility:
       
    26 // All.
       
    27 // Assumptions/Requirement/Pre-requisites:
       
    28 // Failures and causes:
       
    29 // Base Port information:
       
    30 // 
       
    31 //
       
    32 
       
    33 #include <e32test.h>
       
    34 #include "u32std.h"
       
    35 #include <e32math.h>
       
    36 
       
    37 #ifdef __CPU_ARM
       
    38 typedef TInt (*TSqrtFn)(TReal&, const TReal&);
       
    39 extern TInt Sqrt(TReal& /*aDest*/, const TReal& /*aSrc*/);
       
    40 extern TUint Sqrt_Length();
       
    41 
       
    42 typedef TInt (*TDivideFn)(TRealX&, const TRealX&);
       
    43 extern TInt Divide(TRealX& /*aDividend*/, const TRealX& /*aDivisor*/);
       
    44 extern TUint Divide_Length();
       
    45 
       
    46 extern TInt SDummy(TInt);
       
    47 extern TUint SDummy_Length();
       
    48 
       
    49 extern TInt Increment(TInt);
       
    50 extern TUint Increment_Length();
       
    51 
       
    52 typedef TInt (*PFI)(TInt);
       
    53 
       
    54 class RTestHeap : public RHeap
       
    55 	{
       
    56 public: 
       
    57 	TUint8* GetTop() {return iTop;}
       
    58 	};
       
    59 
       
    60 TInt Thread2(TAny* aPtr)
       
    61 	{
       
    62 	TSqrtFn pSqrt=(TSqrtFn)aPtr;
       
    63 	TReal x,y;
       
    64 	x=2.0;
       
    65 	return pSqrt(y,x);
       
    66 	}
       
    67 
       
    68 TInt Thread3(TAny* aPtr)
       
    69 	{
       
    70 	return *(TInt*)aPtr;
       
    71 	}
       
    72 
       
    73 TInt Thread4(TAny* aPtr)
       
    74 	{
       
    75 	*(TInt*)aPtr=0xe7ffffff;
       
    76 	return 0;
       
    77 	}
       
    78 
       
    79 void SecondaryProcess(const TDesC& aCmd, RTest& test)
       
    80 	{
       
    81 	test.Start(_L("Secondary Process"));
       
    82 	TLex lex(aCmd);
       
    83 	TUint32 addr;
       
    84 	TInt r=lex.Val(addr,EHex);
       
    85 	test(r==KErrNone);
       
    86 	test.Printf(_L("Main process RAM code at %08x\n"),addr);
       
    87 	TInt n=0;
       
    88 	FOREVER
       
    89 		{
       
    90 		RThread t;
       
    91 		TRequestStatus s;
       
    92 		if (n==0)
       
    93 			{
       
    94 			// Create another thread which attempts to execute code from the other process
       
    95 			r=t.Create(_L("Thread2"),Thread2,0x1000,NULL,(TAny*)addr);
       
    96 			}
       
    97 		else if (n==1)
       
    98 			{
       
    99 			// Create another thread which attempts to read code from the other process
       
   100 			r=t.Create(_L("Thread3"),Thread3,0x1000,NULL,(TAny*)addr);
       
   101 			}
       
   102 		else if (n==2)
       
   103 			{
       
   104 			// Create another thread which attempts to write to the the other process' code
       
   105 			r=t.Create(_L("Thread4"),Thread4,0x1000,NULL,(TAny*)addr);
       
   106 			}
       
   107 		test(r==KErrNone);
       
   108 		t.SetPriority(EPriorityMore);
       
   109 		t.Logon(s);
       
   110 		t.Resume();
       
   111 		User::WaitForRequest(s);
       
   112 		TInt exitType=t.ExitType();
       
   113 		TInt exitReason=t.ExitReason();
       
   114 		TBuf<32> exitCat=t.ExitCategory();
       
   115 		CLOSE_AND_WAIT(t);
       
   116 		test(exitType==EExitPanic);
       
   117 		test(exitReason==ECausedException);
       
   118 		test(exitCat==_L("KERN-EXEC"));
       
   119 		if (++n==3)
       
   120 			n=0;
       
   121 		User::After(0);//Force rescheduling of the primary process's thread.
       
   122 		}
       
   123 	}
       
   124 
       
   125 void Fill32(TUint32* aBase, TUint aSize, TUint32 aValue)
       
   126 	{
       
   127 	for (; aSize; aSize-=4)
       
   128 		*aBase++=aValue;
       
   129 	}
       
   130 
       
   131 void TestIMB(RTest& test, TUint32* aBase, TUint aOffset, TUint aSize)
       
   132 	{
       
   133 	test.Printf(_L("TestIMB: Base %08x Offset %x Size %x\n"),aBase,aOffset,aSize);
       
   134 	// First fill entire area
       
   135 	Fill32(aBase,0x20000,0xe3a00000);			// mov r0, #0
       
   136 #ifdef __SUPPORT_THUMB_INTERWORKING
       
   137 	aBase[0x8000]=0xe12fff1e;					// bx lr
       
   138 #else
       
   139 	aBase[0x8000]=0xe1a0f00e;					// mov pc, lr
       
   140 #endif
       
   141 	PFI pBase=(PFI)aBase;
       
   142 	PFI pCode=(PFI)((TUint8*)aBase+aOffset);
       
   143 	User::IMB_Range(aBase,aBase+0x8001);
       
   144 	TInt r=pBase(0);
       
   145 	test(r==0);
       
   146 
       
   147 	TUint32* p32=(TUint32*)pCode;
       
   148 	TUint32* pEnd32=p32+aSize/4;
       
   149 	Fill32(p32,aSize-4,0xe2800001);				// add r0, r0, #1
       
   150 #ifdef __SUPPORT_THUMB_INTERWORKING
       
   151 	pEnd32[-1]=0xe12fff1e;						// bx lr
       
   152 #else
       
   153 	pEnd32[-1]=0xe1a0f00e;						// mov pc, lr
       
   154 #endif
       
   155 	User::IMB_Range(p32,pEnd32);
       
   156 	r=pCode(0);
       
   157 	if (r!=(TInt)(aSize/4-1))
       
   158 		{
       
   159 		test.Printf(_L("f(0) expected %d got %d\n"),aSize/4-1,r);
       
   160 		test(0);
       
   161 		}
       
   162 	r=pCode(487);
       
   163 	if (r!=(TInt)(487+aSize/4-1))
       
   164 		{
       
   165 		test.Printf(_L("f(487) expected %d got %d\n"),487+aSize/4-1,r);
       
   166 		test(0);
       
   167 		}
       
   168 	}
       
   169 
       
   170 GLREF_C TInt E32Main()
       
   171 	{
       
   172 	RTest test(_L("T_IMB"));
       
   173 	test.Title();
       
   174 
       
   175 	TBuf<16> cmd;
       
   176 	User::CommandLine(cmd);
       
   177 	if (cmd.Length()!=0)
       
   178 		{
       
   179 		SecondaryProcess(cmd,test);
       
   180 		return 0;
       
   181 		}
       
   182 
       
   183 	test.Start(_L("Create code chunk"));
       
   184 	TInt pageSize;
       
   185 	TInt r=UserHal::PageSizeInBytes(pageSize);
       
   186 	test(r==KErrNone);
       
   187 
       
   188 	RChunk c;
       
   189 	r=c.CreateLocalCode(pageSize,0x100000);
       
   190 	test(r==KErrNone);
       
   191 	TUint8* pCode=c.Base();
       
   192 	test.Printf(_L("Code chunk at %08x\n"),pCode);
       
   193 
       
   194 	// Copy increment function
       
   195 	Mem::Copy(pCode, (const TAny*)&Increment, Increment_Length());
       
   196 	User::IMB_Range(pCode,pCode+Increment_Length());
       
   197 	PFI pFI=(PFI)pCode;
       
   198 	r=pFI(29);
       
   199 	test(r==30);
       
   200 
       
   201 	// Copy dummy without IMB
       
   202 	Mem::Copy(pCode, (const TAny*)&SDummy, SDummy_Length());
       
   203 	r=pFI(29);
       
   204 	test.Printf(_L("Copy without IMB 1: r=%d\n"),r);
       
   205 
       
   206 	// Now do IMB
       
   207 	User::IMB_Range(pCode,pCode+SDummy_Length());
       
   208 	r=pFI(29);
       
   209 	test(r==29);
       
   210 
       
   211 	// Read the code so it's in DCache
       
   212 	TInt i;
       
   213 	TInt sum=0;
       
   214 	for (i=0; i<15; ++i)
       
   215 		sum+=pCode[i];
       
   216 
       
   217 	// Copy increment function
       
   218 	Mem::Copy(pCode, (const TAny*)&Increment, Increment_Length());
       
   219 	r=pFI(29);
       
   220 	test.Printf(_L("Copy without IMB 2: r=%d\n"),r);
       
   221 
       
   222 	// Now do IMB
       
   223 	User::IMB_Range(pCode,pCode+Increment_Length());
       
   224 	r=pFI(29);
       
   225 	test(r==30);
       
   226 
       
   227 	// Now adjust to 2 pages
       
   228 	r=c.Adjust(2*pageSize);
       
   229 	test(r==KErrNone);
       
   230 	TUint8* pCode2=pCode+pageSize;
       
   231 
       
   232 	// Create another thread
       
   233 	RThread t;
       
   234 	TRequestStatus s;
       
   235 	r=t.Create(_L("Thread2"),Thread2,0x1000,NULL,pCode2);
       
   236 	test(r==KErrNone);
       
   237 	t.SetPriority(EPriorityMore);
       
   238 	t.Logon(s);
       
   239 
       
   240 	// Copy Sqrt code to 2nd page
       
   241 	Mem::Copy(pCode2, (const TAny*)&Sqrt, Sqrt_Length());
       
   242 	User::IMB_Range(pCode2,pCode2+Sqrt_Length());
       
   243 	TSqrtFn pSqrt=(TSqrtFn)pCode2;
       
   244 	TReal x,y,z;
       
   245 	x=2.0;
       
   246 	r=Math::Sqrt(y,x);
       
   247 	test(r==KErrNone);
       
   248 	r=pSqrt(z,x);
       
   249 	test(r==KErrNone);
       
   250 	test(z==y);
       
   251 
       
   252 	// Unmap the second page
       
   253 	r=c.Adjust(pageSize);
       
   254 	test(r==KErrNone);
       
   255 
       
   256 	// Get the second thread to attempt to execute the unmapped code
       
   257 	t.Resume();
       
   258 	User::WaitForRequest(s);
       
   259 	TInt exitType=t.ExitType();
       
   260 	TInt exitReason=t.ExitReason();
       
   261 	TBuf<32> exitCat=t.ExitCategory();
       
   262 	CLOSE_AND_WAIT(t);
       
   263 	test.Printf(_L("Thread2: %d,%d,%S\n"),exitType,exitReason,&exitCat);
       
   264 	test(exitType==EExitPanic);
       
   265 	test(exitReason==ECausedException);
       
   266 	test(exitCat==_L("KERN-EXEC"));
       
   267 
       
   268 	// Copy Sqrt code to 1st page
       
   269 	Mem::Copy(pCode, (const TAny*)&Sqrt, Sqrt_Length());
       
   270 	User::IMB_Range(pCode,pCode+Sqrt_Length());
       
   271 	pSqrt=(TSqrtFn)pCode;
       
   272 
       
   273 	// Do a long test to allow multiple copies of this process to run concurrently
       
   274 	// Spawn a secondary process
       
   275 	RProcess p;
       
   276 	TBuf<16> codeBaseHex;
       
   277 	codeBaseHex.Format(_L("%08x"),pCode);
       
   278 	r=p.Create(RProcess().FileName(),codeBaseHex);
       
   279 	test(r==KErrNone);
       
   280 	p.Logon(s);
       
   281 	p.Resume();
       
   282 
       
   283 	TTime begin;
       
   284 	begin.HomeTime();
       
   285 	i=1;
       
   286 	for (;;)
       
   287 		{
       
   288 		TReal x,y,z;
       
   289 		x=i;
       
   290 		r=Math::Sqrt(y,x);
       
   291 		test(r==KErrNone);
       
   292 		r=pSqrt(z,x);
       
   293 		test(r==KErrNone);
       
   294 		test(z==y);
       
   295 		++i;
       
   296 		TTime now;
       
   297 		now.HomeTime();
       
   298 		if (now.MicroSecondsFrom(begin).Int64()>10000000)
       
   299 			break;
       
   300 		User::After(0);//Force rescheduling of the secondary process's thread
       
   301 		}
       
   302 	p.Kill(0);
       
   303 	User::WaitForRequest(s);
       
   304 	exitType=p.ExitType();
       
   305 	exitReason=p.ExitReason();
       
   306 	exitCat=p.ExitCategory();
       
   307 	CLOSE_AND_WAIT(p);
       
   308 	test.Printf(_L("SecProc: %d,%d,%S\n"),exitType,exitReason,&exitCat);
       
   309 	test(exitType==EExitKill);
       
   310 	test(exitReason==KErrNone);
       
   311 
       
   312 	// Test heap in code chunk
       
   313 	RTestHeap* pCodeHeap=(RTestHeap*) UserHeap::ChunkHeap(c,pageSize,pageSize);
       
   314 	test(pCodeHeap==(RHeap*)c.Base());
       
   315 	test(c.Size()==pageSize);
       
   316 	TUint32* pCode3=(TUint32*)pCodeHeap->Alloc(pageSize);
       
   317 	test(pCode3!=NULL);
       
   318 	test(c.Size()==2*pageSize);
       
   319 	TAny* pCode4=pCodeHeap->Alloc(3*pageSize);
       
   320 	test(pCode4!=NULL);
       
   321 	test(c.Size()==5*pageSize);
       
   322 	pCodeHeap->Free(pCode4);
       
   323 	test(c.Size()==2*pageSize);
       
   324 	TUint8 * oldTop = pCodeHeap->GetTop();
       
   325 	pCodeHeap->Free(pCode3);
       
   326 	TUint8 * newTop = pCodeHeap->GetTop();
       
   327 	// Under some conditions (KHeapShrinkRatio value is low and iGrowBy is at its default value of a page size) 
       
   328 	// heap may be reduced at the end of Free() operation
       
   329 	if (oldTop==newTop) // heap was not reduced
       
   330 		test(c.Size()==2*pageSize);
       
   331 
       
   332 	// Test IMB with various base/size values
       
   333 	pCode3=(TUint32*)pCodeHeap->Alloc(0x20004);
       
   334 	test(pCode3!=NULL);
       
   335 
       
   336 	for (i=8; i<1024; i+=32)
       
   337 		{
       
   338 		TestIMB(test,pCode3,0,i);
       
   339 		TestIMB(test,pCode3,4,i);
       
   340 		}
       
   341 
       
   342 	for (i=1024; i<131072; i+=844)
       
   343 		{
       
   344 		TestIMB(test,pCode3,0,i);
       
   345 		TestIMB(test,pCode3,4,i);
       
   346 		}
       
   347 
       
   348 	c.Close();
       
   349 
       
   350 	test.End();
       
   351 	return 0;
       
   352 	}
       
   353 #else
       
   354 GLREF_C TInt E32Main()
       
   355 	{
       
   356 	return 0;
       
   357 	}
       
   358 #endif