os/kernelhwsrv/kerneltest/e32test/mmu/t_btb.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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_btb.cpp
    15 // 
    16 //
    17 
    18 //! @SYMTestCaseID KBASE/T_BTB
    19 //! @SYMTestType UT
    20 //! @SYMTestCaseDesc Make sure processes can't interfere with each other via the BTB.  Make sure the BTB is turned on.
    21 //! @SYMREQ REQ4095
    22 //! @SYMTestActions Make concurent processes with code areas and thrash them.
    23 //! @SYMTestExpectedResults  They shouldn't interfere.  The BTB should be enabled.
    24 //! @SYMTestPriority Low
    25 //! @SYMTestStatus Defined
    26 
    27 #include <e32test.h>
    28 #include "u32std.h"
    29 
    30 #ifdef __CPU_ARM
    31 
    32 extern TInt BranchTest1();
    33 extern TInt BranchTest2();
    34 extern TInt BranchTest3();
    35 void BranchTest4(TInt);
    36 extern void BranchTest1End();
    37 extern void BranchTest2End();
    38 extern void BranchTest3End();
    39 extern void BranchTest4End();
    40 
    41 typedef TInt (*PFI)(void);
    42 typedef void (*PFV)(TInt);
    43 
    44 void SecondaryProcess(const TDesC& aCmd, RTest& test)
    45 	{
    46 	test.Start(_L("Secondary Process"));
    47 	TLex lex(aCmd);
    48 	TUint32 addr;
    49 	TInt r=lex.Val(addr,EHex);
    50 	test(r==KErrNone);
    51 	test.Printf(_L("Primary process says: RAM code at %08x\n"),addr);
    52 	TInt pageSize;
    53 	r=UserHal::PageSizeInBytes(pageSize);
    54 	test(r==KErrNone);
    55 	RChunk c;
    56 	r=c.CreateLocalCode(pageSize,0x100000);
    57 	test(r==KErrNone);
    58 	PFI pBranchTest2=(PFI)c.Base();
    59 	test.Printf(_L("Secondary test loop function at %08x\n"),pBranchTest2);
    60 	
    61 	test((TUint32)pBranchTest2==addr);
    62 
    63 	TInt fnLength = (TInt)&BranchTest2End-(TInt)&BranchTest2;
    64 
    65 	test.Printf(_L("SecProc: Copying %d bytes from %08x...\n"), (TInt)&BranchTest2End-(TInt)&BranchTest2, (TAny*)&BranchTest2 );
    66 	Mem::Copy((TAny*)pBranchTest2, (TAny*)&BranchTest2, fnLength);
    67 	User::IMB_Range((TAny*)pBranchTest2, fnLength+(TUint8*)pBranchTest2);
    68 
    69 	TTime begin;
    70 	begin.HomeTime();
    71 
    72 	test.Printf(_L("Running secondary loop...\n"));
    73 
    74 	TInt n=0;
    75 	FOREVER
    76 		{
    77 		n=pBranchTest2();
    78 
    79 		if (n)
    80 			break;
    81 
    82 		TTime now;
    83 		now.HomeTime();
    84 		if (now.MicroSecondsFrom(begin).Int64()>20000000)  // ten seconds for each test ought to be long enough
    85 			break;
    86 		}
    87 		test.Printf(_L("Ending secondary loop.\n"));
    88 		test(n==0);
    89 		//test.End();
    90 	}
    91 
    92 
    93 GLREF_C TInt E32Main()
    94 	{
    95 	RTest test(_L("T_BTB"));
    96 	test.Title();
    97 
    98 	TBuf<16> cmd;
    99 	User::CommandLine(cmd);
   100 	if (cmd.Length()!=0)  // if we get a command line, we're the secondary process
   101 		{
   102 		SecondaryProcess(cmd,test);
   103 		return 0;
   104 		}
   105 
   106 	test.Start(_L("Create primary process code chunk"));
   107 	TInt pageSize;
   108 	TInt r=UserHal::PageSizeInBytes(pageSize);
   109 	test(r==KErrNone);
   110 
   111 	RChunk c;
   112 	r=c.CreateLocalCode(pageSize,0x100000);
   113 	test(r==KErrNone);
   114 	TUint8* pCode=c.Base();
   115 	test.Printf(_L("Primary process code chunk at %08x\n"),pCode);
   116 
   117 	// Spawn a secondary process
   118 	RProcess p;
   119 	TRequestStatus s;
   120 	TBuf<16> codeBaseHex;
   121 	codeBaseHex.Format(_L("%08x"),pCode);
   122 	r=p.Create(RProcess().FileName(),codeBaseHex);
   123 	test(r==KErrNone);
   124 	p.Logon(s);
   125 	p.Resume();
   126 
   127 
   128 	TTime begin, now;
   129 	TInt n=0, m=0, q=0, fnLength;
   130 
   131 	PFI pBranchTest1=(PFI)pCode;
   132 	test.Printf(_L("Primary test loop function at %08x\n"),pBranchTest1);
   133 
   134 	fnLength = (TInt)&BranchTest1End-(TInt)&BranchTest1;
   135 	
   136 	//test.Printf(_L("PriProc: Copying %d bytes from 0x%08x to 0x%08x for forward-branching test...\n"), fnLength, &BranchTest1, pBranchTest1);
   137 	Mem::Copy((TAny *)pBranchTest1, (const TAny*)&BranchTest1, fnLength);   // copy in the asm test code
   138 	User::IMB_Range((TAny*)pBranchTest1, fnLength+(TUint8*)pBranchTest1);
   139 
   140 	test.Printf(_L("Running primary loop...\n"));
   141 	begin.HomeTime();
   142 	FOREVER
   143 		{
   144 		m++;
   145 		n=pBranchTest1();
   146 		if (n)
   147 			break;	
   148 		now.HomeTime();
   149 		if (now.MicroSecondsFrom(begin).Int64()>10000000)  // ten seconds ought to be long enough
   150 			break;
   151 		}
   152 	test(n==0);
   153 	test.Printf(_L("Ending primary loop.  Ran %d times in 10s.\n"), m);
   154 
   155 	// run the second test
   156 
   157 	PFI pBranchTest3=(PFI)pCode;
   158 
   159 	fnLength = (TInt)&BranchTest3End-(TInt)&BranchTest3;
   160 	//test.Printf(_L("PriProc: Copying %d bytes from 0x%08x to 0x%08x for back-branching test...\n"), fnLength, &BranchTest3, pBranchTest3);
   161 	Mem::Copy((TAny *)pBranchTest3, (const TAny*)&BranchTest3, fnLength);   // copy in the asm test code
   162 	User::IMB_Range((TAny*)pBranchTest3, fnLength+(TUint8*)pBranchTest3);
   163 
   164 	test.Printf(_L("PriProc: Starting back-branch test loop.\n"));
   165 	m=0;  
   166 	begin.HomeTime();
   167 	FOREVER
   168 		{
   169 		m++;
   170 		n=pBranchTest3();
   171 		if (n)
   172 			break;	
   173 		now.HomeTime();
   174 		if (now.MicroSecondsFrom(begin).Int64()>10000000)  // ten seconds ought to be long enough
   175 			break;
   176 		}
   177 	test(n==0);
   178 	test.Printf(_L("Ending primary back-branching loop.  Ran %d times in 10s.\n"), m);
   179 
   180 	p.Kill(0);
   181 	User::WaitForRequest(s);
   182 	TInt exitType=p.ExitType();
   183 	TInt exitReason=p.ExitReason();
   184 	TExitCategoryName exitCat=p.ExitCategory();
   185 	CLOSE_AND_WAIT(p);
   186 	test.Printf(_L("SecProc: %d,%d,%S\n"),exitType,exitReason,&exitCat);
   187 	test(exitType==EExitKill);
   188 	test(exitReason==KErrNone);
   189 
   190 	// speed test
   191 
   192 	PFV pBranchTest4 = (PFV)pCode;
   193 	fnLength = (TInt)&BranchTest4End-(TInt)&BranchTest4;
   194 	//test.Printf(_L("PriProc: Copying %d bytes from 0x%08x to 0x%08x for forward-branching speed test...\n"), fnLength, &BranchTest4, pBranchTest1);
   195 	Mem::Copy((TAny *)pBranchTest4, (const TAny*)&BranchTest4, fnLength);   // copy in the asm test code
   196 	User::IMB_Range((TAny*)pBranchTest4, fnLength+(TUint8*)pBranchTest4);
   197 
   198 	test.Printf(_L("Speed test running with f(0)...\n"));
   199 	m=0;
   200 	begin.HomeTime();
   201 	FOREVER
   202 		{
   203 		m++;
   204 		pBranchTest4(0);
   205 		now.HomeTime();
   206 		if (now.MicroSecondsFrom(begin).Int64()>10000000)  // ten seconds ought to be long enough
   207 			break;
   208 		}
   209 	test.Printf(_L("Ending f(0) test.  Ran %d times in 10s.\n"), m);
   210 
   211 	test.Printf(_L("PriProc: Starting f(1) run.\n"));
   212 	q=0;	
   213 	begin.HomeTime();
   214 	FOREVER
   215 		{
   216 		q++;
   217 		pBranchTest4(1);
   218 		now.HomeTime();
   219 		if (now.MicroSecondsFrom(begin).Int64()>10000000)  // ten seconds ought to be long enough
   220 			break;
   221 		}
   222 	test.Printf(_L("Ending test loop with f(1).  Ran %d times in 10s.\n"), q);
   223 
   224 	test(m-q>m/4);  // the difference between m and q should be large, indicating successful prediction
   225 
   226 	c.Close();
   227 
   228 	test.End();
   229 	return 0;
   230 	}
   231 #else
   232 GLREF_C TInt E32Main()
   233 	{
   234 	return 0;
   235 	}
   236 #endif