kerneltest/e32test/mmu/d_shbuf.cpp
changeset 9 96e5fb8b040d
child 6 0173bcd7697c
equal deleted inserted replaced
-1:000000000000 9:96e5fb8b040d
       
     1 // Copyright (c) 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_shbuf.cpp
       
    15 //
       
    16 
       
    17 #include "d_shbuf.h"
       
    18 #include <kernel/kernel.h>
       
    19 #include <kernel/cache.h>
       
    20 #include "plat_priv.h"
       
    21 #include <kernel/sshbuf.h>
       
    22 
       
    23 
       
    24 #define TEST_EXP(a)					CheckPoint(a, __LINE__)
       
    25 #define TEST_KERRNONE(a)			CheckPointError(a, __LINE__)
       
    26 
       
    27 #ifdef TEST_CLIENT_THREAD
       
    28 #define TEST_ENTERCS()	NKern::ThreadEnterCS()
       
    29 #define TEST_LEAVECS()	NKern::ThreadLeaveCS()
       
    30 #else
       
    31 #define TEST_ENTERCS()
       
    32 #define TEST_LEAVECS()
       
    33 #endif // TEST_CLIENT_THREAD
       
    34 
       
    35 const TInt KMaxPhysicalMemoryBlockSize = 512 << 10; // 512KB;
       
    36 
       
    37 // ----------------------------------------------------------------------------
       
    38 
       
    39 class DShBufTestDrvFactory : public DLogicalDevice
       
    40 	{
       
    41 public:
       
    42 	DShBufTestDrvFactory();
       
    43 	~DShBufTestDrvFactory();
       
    44 	virtual TInt Install();
       
    45 	virtual void GetCaps(TDes8& aDes) const;
       
    46 	virtual TInt Create(DLogicalChannelBase*& aChannel);
       
    47 public:
       
    48 #ifndef TEST_CLIENT_THREAD
       
    49 	TDynamicDfcQue* iDfcQ;
       
    50 #endif
       
    51 	};
       
    52 
       
    53 // ----------------------------------------------------------------------------
       
    54 
       
    55 #ifdef TEST_CLIENT_THREAD
       
    56 class DShBufTestDrvChannel : public DLogicalChannelBase
       
    57 #else
       
    58 class DShBufTestDrvChannel : public DLogicalChannel
       
    59 #endif
       
    60 	{
       
    61 public:
       
    62 	DShBufTestDrvChannel();
       
    63 	~DShBufTestDrvChannel();
       
    64 	// Inherited from DLogicalChannel
       
    65 	virtual TInt DoCreate(TInt aUnit, const TDesC8* anInfo, const TVersion& aVer);
       
    66 #ifdef TEST_CLIENT_THREAD
       
    67 	// Inherited from DLogicalChannelBase: process all DoControl in the user's context
       
    68 	virtual TInt Request(TInt aReqNo, TAny* a1, TAny* a2);
       
    69 #else
       
    70 	TInt DoControl(TInt aReqNo, TAny* a1, TAny* a2);
       
    71 	virtual void HandleMsg(TMessageBase* aMsg);
       
    72     virtual TInt SendMsg(TMessageBase* aMsg);
       
    73 #endif
       
    74 public:
       
    75 	TShPoolCreateInfo* iCreateinfo;
       
    76 	TShPoolInfo iUserpoolinfo;
       
    77 	TShPool* iPools[2];
       
    78 	TShBuf* iAdopted;
       
    79 	TUint8  iDriverTxBuffer[8192];
       
    80 	TUint8  iDriverRxBuffer[8192];
       
    81 #ifndef TEST_CLIENT_THREAD
       
    82 	DThread* iClient;
       
    83 	TVirtualPinObject* iPin;
       
    84 #endif
       
    85 	};
       
    86 
       
    87 // ----------------------------------------------------------------------------
       
    88 
       
    89 void CheckPoint(TInt aCondition, TInt aLine)
       
    90 	{
       
    91 	if (!aCondition)
       
    92 		{
       
    93 		Kern::Printf("Device driver test failed (line %d)", aLine);
       
    94 		}
       
    95 	}
       
    96 
       
    97 void CheckPointError(TInt aErrorCode, TInt aLine)
       
    98 	{
       
    99 	if (aErrorCode != KErrNone)
       
   100 		{
       
   101 		Kern::Printf("Device driver error %d (line %d)", aErrorCode, aLine);
       
   102 		}
       
   103 	}
       
   104 
       
   105 TInt Log2(TInt aNum)
       
   106 	{
       
   107 	TInt res = -1;
       
   108 	while(aNum)
       
   109 		{
       
   110 		res++;
       
   111 		aNum >>= 1;
       
   112 		}
       
   113 	return res;
       
   114 	}
       
   115 
       
   116 TInt RoundUp(TInt aNum, TInt aAlignmentLog2)
       
   117 	{
       
   118 	if (aNum % (1 << aAlignmentLog2) == 0)
       
   119 		{
       
   120 		return aNum;
       
   121 		}
       
   122 	return (aNum & ~((1 << aAlignmentLog2) - 1)) + (1 << aAlignmentLog2);
       
   123 	}
       
   124 
       
   125 #ifdef __WINS__
       
   126 #define SHBUF_NOT_WINS(x)
       
   127 #else
       
   128 #define SHBUF_NOT_WINS(x)	x
       
   129 #endif
       
   130 TBool IsBufferContiguous(TShBuf* SHBUF_NOT_WINS(aBuf))
       
   131 	{
       
   132 	TInt pagesize;
       
   133 	TInt r = Kern::HalFunction(EHalGroupKernel, EKernelHalPageSizeInBytes, &pagesize, 0);
       
   134 	TEST_KERRNONE(r);
       
   135 
       
   136 #ifdef __WINS__
       
   137 	return ETrue;
       
   138 #else
       
   139 	TUint8* ptr = Kern::ShBufPtr(aBuf);
       
   140 	TUint size = Kern::ShBufSize(aBuf);
       
   141 
       
   142 	TBool iscontiguous = ETrue;
       
   143 
       
   144 	TPhysAddr startphys = Epoc::LinearToPhysical((TLinAddr) ptr);
       
   145 	TUint i;
       
   146 
       
   147 	for (i = 0; i < size; i += pagesize)
       
   148 		{
       
   149 		TPhysAddr current = Epoc::LinearToPhysical((TLinAddr) ptr + i);
       
   150 		if (current != startphys + i)
       
   151 			{
       
   152 			Kern::Printf("Page %d: 0x%08x (started@0x%08x expected 0x%08x)", i, current, startphys, startphys + i);
       
   153 			iscontiguous = EFalse;
       
   154 			break;
       
   155 			}
       
   156 		}
       
   157 
       
   158 	return iscontiguous;
       
   159 #endif // __WINS__
       
   160 	}
       
   161 
       
   162 DECLARE_STANDARD_LDD()
       
   163 	{
       
   164 	return new DShBufTestDrvFactory;
       
   165 	}
       
   166 
       
   167 DShBufTestDrvFactory::DShBufTestDrvFactory()
       
   168 	{
       
   169 	iParseMask=0; //no units, no info, no pdd
       
   170 	iUnitsMask=0;
       
   171 	iVersion=TVersion(1,0,KE32BuildVersionNumber);
       
   172 	}
       
   173 
       
   174 DShBufTestDrvFactory::~DShBufTestDrvFactory()
       
   175 	{
       
   176 #ifndef TEST_CLIENT_THREAD
       
   177 	if (iDfcQ)
       
   178 		iDfcQ->Destroy();
       
   179 #endif
       
   180 	}
       
   181 
       
   182 #ifndef TEST_CLIENT_THREAD
       
   183 const TInt KShBufTestThreadPriority = 1;
       
   184 _LIT(KShBufTestThread,"ShBufTestThread");
       
   185 #endif
       
   186 
       
   187 TInt DShBufTestDrvFactory::Install()
       
   188 	{
       
   189 #ifndef TEST_CLIENT_THREAD
       
   190 	TInt r = Kern::DynamicDfcQCreate(iDfcQ, KShBufTestThreadPriority, KShBufTestThread);
       
   191 
       
   192 	if (r != KErrNone)
       
   193 		return r;
       
   194 	return(SetName(&KTestShBufOwn));
       
   195 #else
       
   196 	return(SetName(&KTestShBufClient));
       
   197 #endif
       
   198 	}
       
   199 
       
   200 
       
   201 void DShBufTestDrvFactory::GetCaps(TDes8& /*aDes*/) const
       
   202 	{
       
   203 	// Get capabilities - overriding pure virtual
       
   204 	}
       
   205 
       
   206 TInt DShBufTestDrvFactory::Create(DLogicalChannelBase*& aChannel)
       
   207 	{
       
   208 	aChannel=new DShBufTestDrvChannel;
       
   209 	return aChannel?KErrNone:KErrNoMemory;
       
   210 	}
       
   211 
       
   212 // ----------------------------------------------------------------------------
       
   213 
       
   214 DShBufTestDrvChannel::DShBufTestDrvChannel()
       
   215 	{
       
   216 #ifndef TEST_CLIENT_THREAD
       
   217 	iClient=&Kern::CurrentThread();
       
   218 	iClient->Open();
       
   219 #endif
       
   220 
       
   221 	TPtr8 bufp(iDriverRxBuffer,0,sizeof(iDriverRxBuffer));
       
   222 
       
   223 	for(TInt pos = 0;  pos < bufp.Length();  pos++)
       
   224 		{
       
   225 		bufp[pos] = (TUint8)(pos & 31);
       
   226 		}
       
   227 	}
       
   228 
       
   229 DShBufTestDrvChannel::~DShBufTestDrvChannel()
       
   230 	{
       
   231 	NKern::ThreadEnterCS();
       
   232 #ifndef TEST_CLIENT_THREAD
       
   233 	Kern::SafeClose((DObject*&)iClient, NULL);
       
   234 	if(iPin)
       
   235 		{
       
   236 		Kern::DestroyVirtualPinObject(iPin);
       
   237 		}
       
   238 #endif
       
   239 	delete iCreateinfo;
       
   240 	NKern::ThreadLeaveCS();
       
   241 	}
       
   242 
       
   243 TInt DShBufTestDrvChannel::DoCreate(TInt /*aUnit*/, const TDesC8* /*aInfo*/, const TVersion& /*aVer*/)
       
   244 	{
       
   245 #ifndef TEST_CLIENT_THREAD
       
   246 	SetDfcQ(((DShBufTestDrvFactory*)iDevice)->iDfcQ);
       
   247 	iMsgQ.Receive();
       
   248 #endif
       
   249 	
       
   250 	return KErrNone;
       
   251 	}
       
   252 
       
   253 #ifndef TEST_CLIENT_THREAD
       
   254 void DShBufTestDrvChannel::HandleMsg(TMessageBase* aMsg)
       
   255 	{
       
   256     TInt r=KErrNone;
       
   257 	TThreadMessage& m=*(TThreadMessage*)aMsg;
       
   258 	TInt id=m.iValue;
       
   259 	if (id==(TInt)ECloseMsg)
       
   260 		{
       
   261 		m.Complete(KErrNone,EFalse);
       
   262 		return;
       
   263 		}
       
   264 	else
       
   265 		{
       
   266 		r=DoControl(id,m.Ptr0(),m.Ptr1());
       
   267 		}
       
   268 	m.Complete(r,ETrue);
       
   269 	}
       
   270 
       
   271 TInt DShBufTestDrvChannel::SendMsg(TMessageBase* aMsg)
       
   272 	{
       
   273 	// We can only handle one request at a time.
       
   274 	TEST_EXP(!iCreateinfo && !iPin);
       
   275 	if(iCreateinfo || iPin)
       
   276 		{
       
   277 		return KErrInUse;
       
   278 		}
       
   279 
       
   280 	TThreadMessage& m = *(TThreadMessage*)aMsg;
       
   281 	TAny* a1 = m.Ptr0();
       
   282 	TAny* a2 = m.Ptr1();
       
   283 	TInt r = KErrNone;
       
   284 
       
   285 	// Make a copy of the parameters in the asynchronous read case so that we don't
       
   286 	// risk a page fault by reading user-mode memory from the msg DFC.
       
   287 	//
       
   288 	// Manage writes using a TClientBufferRequest.
       
   289 	switch(aMsg->iValue)
       
   290 		{
       
   291 		// Reads
       
   292 		case RShBufTestChannel::ETestOpenUserPool:
       
   293 			kumemget(&iUserpoolinfo, a2, sizeof(iUserpoolinfo));
       
   294 		break;
       
   295 		case RShBufTestChannel::ETestCreatePoolContiguousPool:
       
   296 			NKern::ThreadEnterCS();
       
   297 			iCreateinfo = new TShPoolCreateInfo;
       
   298 			NKern::ThreadLeaveCS();
       
   299 			TEST_EXP(iCreateinfo != NULL);
       
   300 			if(!iCreateinfo)
       
   301 				{
       
   302 				r = KErrNoMemory;
       
   303 				break;
       
   304 				}
       
   305 
       
   306 			kumemget(iCreateinfo, a1, sizeof(TShPoolInfo));
       
   307 		break;
       
   308 		case RShBufTestChannel::EFromTPtr8ProcessAndRelease:
       
   309 			{
       
   310 			TPtr8 dest(iDriverTxBuffer, sizeof(iDriverTxBuffer));
       
   311 			Kern::ThreadDesRead(iClient, a1, dest, 0, KChunkShiftBy0);
       
   312 			}
       
   313 		break;
       
   314 
       
   315 		// Writes
       
   316 		case RShBufTestChannel::ETestOpenKernelPool:
       
   317 			NKern::ThreadEnterCS();
       
   318 			iCreateinfo = new TShPoolCreateInfo;
       
   319 			NKern::ThreadLeaveCS();
       
   320 			TEST_EXP(iCreateinfo != NULL);
       
   321 			if(!iCreateinfo)
       
   322 				{
       
   323 				r = KErrNoMemory;
       
   324 				break;
       
   325 				}
       
   326 
       
   327 			kumemget(iCreateinfo, a1, sizeof(TShPoolInfo));
       
   328 
       
   329 			// Fallthrough...
       
   330 		case RShBufTestChannel::ETestAllocateMax:
       
   331 		case RShBufTestChannel::ETestAllocateKernelBuffer:
       
   332 			{
       
   333 			NKern::ThreadEnterCS();
       
   334 			r = Kern::CreateAndPinVirtualMemory(iPin, (TLinAddr)a2, sizeof(TInt));
       
   335 			NKern::ThreadLeaveCS();
       
   336 			}
       
   337 		break;
       
   338 
       
   339 		// Descriptor writes
       
   340 		case RShBufTestChannel::EFromTPtr8ProcessAndReturn:
       
   341 			{
       
   342 			TUint size = ((const TDes8*)a1)->Size();
       
   343 			
       
   344 			if(size <= sizeof(iDriverRxBuffer))
       
   345 				{
       
   346 				NKern::ThreadEnterCS();
       
   347 				r = Kern::CreateAndPinVirtualMemory(iPin, (TLinAddr)((const TDes8*)a1)->Ptr(), size);
       
   348 				NKern::ThreadLeaveCS();
       
   349 				}
       
   350 			else
       
   351 				{
       
   352 				r = KErrNoMemory;
       
   353 				}
       
   354 			}
       
   355 		break;
       
   356 		}
       
   357 
       
   358 	if(r == KErrNone)
       
   359 		{
       
   360 		r = DLogicalChannel::SendMsg(aMsg);
       
   361 		}
       
   362 
       
   363 	return r;
       
   364 	}
       
   365 #endif
       
   366 
       
   367 #ifdef TEST_CLIENT_THREAD
       
   368 TInt DShBufTestDrvChannel::Request(TInt aReqNo, TAny* a1, TAny* a2)
       
   369 #else
       
   370 TInt DShBufTestDrvChannel::DoControl(TInt aReqNo, TAny* a1, TAny* a2)
       
   371 #endif
       
   372 	{
       
   373 	TInt r=KErrNotSupported;
       
   374 
       
   375 	switch (aReqNo)
       
   376 		{
       
   377 // ----------------------------------------------------------------------------
       
   378 // TInt RShBufTestChannel::OpenUserPool(TInt aHandle, TShPoolInfo& aPoolInfo)
       
   379 		case RShBufTestChannel::ETestOpenUserPool:
       
   380 			{
       
   381 			DThread* tP=NULL;
       
   382 			
       
   383 #ifdef TEST_CLIENT_THREAD
       
   384 			kumemget(&iUserpoolinfo, a2, sizeof(iUserpoolinfo));
       
   385 			tP=&Kern::CurrentThread();
       
   386 #else
       
   387 			tP=iClient;
       
   388 #endif
       
   389 
       
   390 			TEST_EXP(!iPools[0]);
       
   391 			if(iPools[0])
       
   392 				{
       
   393 				r = KErrAlreadyExists;
       
   394 				break;
       
   395 				}
       
   396 
       
   397 			NKern::ThreadEnterCS();
       
   398 			r = Kern::ShPoolOpen(iPools[0], tP, (TInt) a1, ETrue, KDefaultPoolHandleFlags);
       
   399 			NKern::ThreadLeaveCS();
       
   400 
       
   401 			TEST_KERRNONE(r);
       
   402 			if (r)
       
   403 				{
       
   404 				break;
       
   405 				}
       
   406 
       
   407 			TInt n;
       
   408 			n = reinterpret_cast<DShPool*>(iPools[0])->AccessCount();
       
   409 			TEST_EXP(n == 2);
       
   410 			if (n != 2)
       
   411 				{
       
   412 				r = KErrUnknown;
       
   413 				break;
       
   414 				}
       
   415 			
       
   416 			TShPoolInfo poolinfo;
       
   417 			Kern::ShPoolGetInfo(iPools[0], poolinfo);
       
   418 			if (!((poolinfo.iBufSize == iUserpoolinfo.iBufSize) &&
       
   419 				((TUint)Kern::ShPoolBufSize(iPools[0]) == iUserpoolinfo.iBufSize) &&
       
   420 				(poolinfo.iInitialBufs == iUserpoolinfo.iInitialBufs) &&
       
   421 				(poolinfo.iMaxBufs == iUserpoolinfo.iMaxBufs) &&
       
   422 				(poolinfo.iGrowTriggerRatio == iUserpoolinfo.iGrowTriggerRatio) &&
       
   423 				(poolinfo.iGrowByRatio == iUserpoolinfo.iGrowByRatio) &&
       
   424 				(poolinfo.iShrinkHysteresisRatio == iUserpoolinfo.iShrinkHysteresisRatio) &&
       
   425 				(poolinfo.iAlignment == iUserpoolinfo.iAlignment) &&
       
   426 				((poolinfo.iFlags & EShPoolNonPageAlignedBuffer) == (iUserpoolinfo.iFlags & EShPoolNonPageAlignedBuffer)) &&
       
   427 				((poolinfo.iFlags & EShPoolPageAlignedBuffer) == (iUserpoolinfo.iFlags & EShPoolPageAlignedBuffer))))
       
   428 				{
       
   429 				TEST_EXP(EFalse);
       
   430 				Kern::Printf("poolinfo.iBufSize == %d (expected %d)", poolinfo.iBufSize, iUserpoolinfo.iBufSize);
       
   431 				Kern::Printf("BufSize() == %d", Kern::ShPoolBufSize(iPools[0]));
       
   432 				Kern::Printf("poolinfo.iInitialBufs == %d (expected %d)", poolinfo.iInitialBufs, iUserpoolinfo.iInitialBufs);
       
   433 				Kern::Printf("poolinfo.iMaxBufs == %d (expected %d)", poolinfo.iMaxBufs, iUserpoolinfo.iMaxBufs);
       
   434 				Kern::Printf("poolinfo.iGrowTriggerRatio == %d (expected %d)", poolinfo.iGrowTriggerRatio, iUserpoolinfo.iGrowTriggerRatio);
       
   435 				Kern::Printf("poolinfo.iGrowByRatio == %d (expected %d)", poolinfo.iGrowByRatio, iUserpoolinfo.iGrowByRatio);
       
   436 				Kern::Printf("poolinfo.iShrinkHysteresisRatio == %d (expected %d)", poolinfo.iShrinkHysteresisRatio, iUserpoolinfo.iShrinkHysteresisRatio);
       
   437 				Kern::Printf("poolinfo.iAlignment == %d (expected %d)", poolinfo.iAlignment, iUserpoolinfo.iAlignment);
       
   438 				Kern::Printf("poolinfo.iFlags == 0x%08x (user=0x%08x)", poolinfo.iFlags, iUserpoolinfo.iFlags);
       
   439 
       
   440 				r = KErrUnknown;
       
   441 				break;
       
   442 				}
       
   443 
       
   444 			if(poolinfo.iFlags & EShPoolPageAlignedBuffer)
       
   445 				{
       
   446 				NKern::ThreadEnterCS();
       
   447 				r = Kern::ShPoolSetBufferWindow(iPools[0],-1);
       
   448 				NKern::ThreadLeaveCS();
       
   449 				TEST_KERRNONE(r);
       
   450 				if(r!=KErrNone)
       
   451 					break;
       
   452 				}
       
   453 
       
   454 			r = KErrNone;
       
   455 			}
       
   456 		break;
       
   457 // ----------------------------------------------------------------------------
       
   458 // TInt RShBufTestChannel::OpenKernelPool(TShPoolCreateInfo& aInfo, TInt& aHandle)
       
   459 		case RShBufTestChannel::ETestOpenKernelPool:
       
   460 			{
       
   461 			TInt handle;
       
   462 #ifdef TEST_CLIENT_THREAD
       
   463 			// We can only handle one request at a time.
       
   464 			TEST_EXP(!iCreateinfo);
       
   465 			if(iCreateinfo)
       
   466 				{
       
   467 				r = KErrInUse;
       
   468 				break;
       
   469 				}
       
   470 
       
   471 			NKern::ThreadEnterCS();
       
   472 			iCreateinfo = new TShPoolCreateInfo;
       
   473 			NKern::ThreadLeaveCS();
       
   474 			TEST_EXP(iCreateinfo != NULL);
       
   475 			if(!iCreateinfo)
       
   476 				{
       
   477 				r = KErrNoMemory;
       
   478 				break;
       
   479 				}
       
   480 
       
   481 			kumemget(iCreateinfo, a1, sizeof(TShPoolInfo));
       
   482 #endif
       
   483 
       
   484 			TEST_EXP(!iPools[1]);
       
   485 			if(iPools[1])
       
   486 				{
       
   487 				r = KErrAlreadyExists;
       
   488 				break;
       
   489 				}
       
   490 
       
   491 			NKern::ThreadEnterCS();
       
   492 			r = Kern::ShPoolCreate(iPools[1], *iCreateinfo, ETrue, KDefaultPoolHandleFlags);
       
   493 			delete iCreateinfo;
       
   494 			iCreateinfo = NULL;
       
   495 			NKern::ThreadLeaveCS();
       
   496 
       
   497 			TEST_KERRNONE(r);
       
   498 			if (r)
       
   499 				{
       
   500 #ifndef TEST_CLIENT_THREAD
       
   501 					NKern::ThreadEnterCS();
       
   502 					Kern::DestroyVirtualPinObject(iPin);
       
   503 					NKern::ThreadLeaveCS();
       
   504 #endif
       
   505 				break;
       
   506 				}
       
   507 
       
   508 			TInt n;
       
   509 			n = reinterpret_cast<DShPool*>(iPools[1])->AccessCount();
       
   510 			TEST_EXP(n == 1);
       
   511 			if (n != 1)
       
   512 				{
       
   513 #ifndef TEST_CLIENT_THREAD
       
   514 					NKern::ThreadEnterCS();
       
   515 					Kern::DestroyVirtualPinObject(iPin);
       
   516 					NKern::ThreadLeaveCS();
       
   517 #endif
       
   518 
       
   519 				r = KErrUnknown;
       
   520 				break;
       
   521 				}
       
   522 
       
   523 			TShPoolInfo poolinfo;
       
   524 			Kern::ShPoolGetInfo(iPools[1], poolinfo);
       
   525 			if(poolinfo.iFlags & EShPoolPageAlignedBuffer)
       
   526 				{
       
   527 				NKern::ThreadEnterCS();
       
   528 				r = Kern::ShPoolSetBufferWindow(iPools[1],-1);
       
   529 				NKern::ThreadLeaveCS();
       
   530 				TEST_KERRNONE(r);
       
   531 				if(r!=KErrNone)
       
   532 					{
       
   533 #ifndef TEST_CLIENT_THREAD
       
   534 						NKern::ThreadEnterCS();
       
   535 						Kern::DestroyVirtualPinObject(iPin);
       
   536 						NKern::ThreadLeaveCS();
       
   537 #endif
       
   538 
       
   539 					break;
       
   540 					}
       
   541 				}
       
   542 
       
   543 #ifdef TEST_CLIENT_THREAD
       
   544 			// Now create a handle for the client
       
   545 			NKern::ThreadEnterCS();
       
   546 			handle = Kern::ShPoolMakeHandleAndOpen(iPools[1], NULL, KDefaultPoolHandleFlags);
       
   547 			NKern::ThreadLeaveCS();
       
   548 #else
       
   549 			handle = Kern::ShPoolMakeHandleAndOpen(iPools[1], iClient, KDefaultPoolHandleFlags);
       
   550 #endif
       
   551 			TEST_EXP(handle > 0);
       
   552 			if (handle < 0)
       
   553 				{
       
   554 #ifndef TEST_CLIENT_THREAD
       
   555 					NKern::ThreadEnterCS();
       
   556 					Kern::DestroyVirtualPinObject(iPin);
       
   557 					NKern::ThreadLeaveCS();
       
   558 #endif
       
   559 
       
   560 				r = handle;
       
   561 				break;
       
   562 				}
       
   563 
       
   564 			n = reinterpret_cast<DShPool*>(iPools[1])->AccessCount();
       
   565 
       
   566 			TEST_EXP(n == 2);
       
   567 			if (n != 2)
       
   568 				{
       
   569 #ifndef TEST_CLIENT_THREAD
       
   570 					NKern::ThreadEnterCS();
       
   571 					Kern::DestroyVirtualPinObject(iPin);
       
   572 					NKern::ThreadLeaveCS();
       
   573 #endif
       
   574 
       
   575 				r = KErrUnknown;
       
   576 				break;
       
   577 				}
       
   578 
       
   579 #ifdef TEST_CLIENT_THREAD
       
   580 			kumemput(a2, &handle, sizeof(handle));
       
   581 #else
       
   582 			Kern::ThreadRawWrite(iClient, a2, &handle, sizeof(handle), iClient);
       
   583 			
       
   584 			NKern::ThreadEnterCS();
       
   585 			Kern::DestroyVirtualPinObject(iPin);
       
   586 			NKern::ThreadLeaveCS();
       
   587 #endif
       
   588 			}
       
   589 		break;
       
   590 // ----------------------------------------------------------------------------
       
   591 // TInt RShBufTestChannel::CloseUserPool()
       
   592 		case RShBufTestChannel::ETestCloseUserPool:
       
   593 			{
       
   594 			TInt n;
       
   595 			n = reinterpret_cast<DShPool*>(iPools[0])->AccessCount();
       
   596 
       
   597 			TEST_EXP(n == 1);
       
   598 			if (n != 1)
       
   599 				{
       
   600 				r = KErrUnknown;
       
   601 				break;
       
   602 				}
       
   603 			TEST_ENTERCS();
       
   604 			r = Kern::ShPoolClose(iPools[0]);
       
   605 			iPools[0] = 0;
       
   606 			TEST_LEAVECS();
       
   607 			if (r>0)
       
   608 				{
       
   609 				r = KErrNone;
       
   610 				}
       
   611 
       
   612 			TEST_KERRNONE(r);
       
   613 			}
       
   614 		break;
       
   615 // ----------------------------------------------------------------------------
       
   616 // TInt RShBufTestChannel::CloseKernelPool()
       
   617 		case RShBufTestChannel::ETestCloseKernelPool:
       
   618 			{
       
   619 #if 0
       
   620 			TInt n;
       
   621 			n = reinterpret_cast<DShPool*>(iPools[1])->AccessCount();
       
   622 			TEST_EXP(n == 2);
       
   623 			if (n != 2)
       
   624 				{
       
   625 				r = KErrUnknown;
       
   626 				break;
       
   627 				}
       
   628 #endif
       
   629 			TEST_ENTERCS();
       
   630 			r = Kern::ShPoolClose(iPools[1]);
       
   631 			iPools[1] = 0;
       
   632 			TEST_LEAVECS();
       
   633 			if (r>0)
       
   634 				{
       
   635 				r = KErrNone;
       
   636 				}
       
   637 
       
   638 			TEST_KERRNONE(r);
       
   639 			}
       
   640 		break;
       
   641 // ----------------------------------------------------------------------------
       
   642 // TInt RShBufTestChannel::ManipulateUserBuffer(TInt aHandle)
       
   643 		case RShBufTestChannel::ETestManipulateUserBuffer:
       
   644 			{
       
   645 			TShBuf* ubuf = NULL;
       
   646 			DThread* tP;
       
   647 
       
   648 #ifdef TEST_CLIENT_THREAD
       
   649 			tP=&Kern::CurrentThread();
       
   650 #else
       
   651 			tP=iClient;
       
   652 #endif
       
   653 			NKern::ThreadEnterCS();
       
   654 
       
   655 			r = Kern::ShBufOpen(ubuf, tP, (TInt) a1);
       
   656 
       
   657 			TEST_KERRNONE(r);
       
   658 			if (r!=KErrNone)
       
   659 				{
       
   660 				NKern::ThreadLeaveCS();
       
   661 				break;
       
   662 				}
       
   663 
       
   664 			TInt n;
       
   665 			n = reinterpret_cast<DShBuf*>(ubuf)->AccessCount();
       
   666 
       
   667 			TEST_EXP(n == 2);
       
   668 			if (n != 2)
       
   669 				{
       
   670 				r = KErrUnknown;
       
   671 				Kern::ShBufClose(ubuf);
       
   672 				NKern::ThreadLeaveCS();
       
   673 				break;
       
   674 				}
       
   675 
       
   676 			TInt i;
       
   677 
       
   678 			TInt blocks = Kern::ShBufSize(ubuf) / KTestData1().Length();
       
   679 
       
   680 			for (i = 0; i < blocks; i++)
       
   681 				{
       
   682 
       
   683 				TPtr8 ptr(Kern::ShBufPtr(ubuf) + (i * KTestData1().Length()), KTestData1().Length(), KTestData1().Length());
       
   684 				r = KTestData1().Compare(ptr);
       
   685 
       
   686 				if (r)
       
   687 					{
       
   688 					break;
       
   689 					}
       
   690 				ptr.Fill(i);
       
   691 				}
       
   692 
       
   693 			TEST_EXP(r == KErrNone);
       
   694 			if (r)
       
   695 				{
       
   696 				r = KErrUnknown;
       
   697 				}
       
   698 			Kern::ShBufClose(ubuf);
       
   699 			NKern::ThreadLeaveCS();
       
   700 			}
       
   701 		break;
       
   702 // ----------------------------------------------------------------------------
       
   703 // TInt RShBufTestChannel::AllocateKernelBuffer(TInt aPoolIndex, TInt& aHandle)
       
   704 		case RShBufTestChannel::ETestAllocateKernelBuffer:
       
   705 			{
       
   706 			TInt poolindex = (TInt) a1;
       
   707 			if ((poolindex != 0) && (poolindex != 1))
       
   708 				{
       
   709 				r = KErrArgument;
       
   710 				break;
       
   711 				}
       
   712 
       
   713 			NKern::ThreadEnterCS();
       
   714 
       
   715 			// Allocate kernel-side buffer
       
   716 			TShBuf* kbuf;
       
   717 			r = Kern::ShPoolAlloc(iPools[poolindex], kbuf, 0);
       
   718 
       
   719 			TEST_KERRNONE(r);
       
   720 			if (r)
       
   721 				{
       
   722 				NKern::ThreadLeaveCS();
       
   723 				break;
       
   724 				}
       
   725 
       
   726 			// Fill it with test data
       
   727 			TUint i;
       
   728 			for (i = 0; i < Kern::ShPoolBufSize(iPools[poolindex]) / KTestData2().Length(); i++)
       
   729 				{
       
   730 				TPtr8 ptr(Kern::ShBufPtr(kbuf) + (i * KTestData2().Length()), KTestData2().Length(), KTestData2().Length());
       
   731 				ptr.Copy(KTestData2());
       
   732 				}
       
   733 
       
   734 			// Now create a handle for the client
       
   735 			TInt handle;
       
   736 #ifdef TEST_CLIENT_THREAD
       
   737 			handle = Kern::ShBufMakeHandleAndOpen(kbuf, NULL);
       
   738 #else
       
   739 			handle = Kern::ShBufMakeHandleAndOpen(kbuf, iClient);
       
   740 #endif
       
   741 
       
   742 			TEST_EXP(handle > 0);
       
   743 			if (handle < 0)
       
   744 				{
       
   745 				r = handle;
       
   746 				Kern::ShBufClose(kbuf);
       
   747 				NKern::ThreadLeaveCS();
       
   748 
       
   749 				break;
       
   750 				}
       
   751 			TInt n;
       
   752 			n = reinterpret_cast<DShBuf*>(kbuf)->AccessCount();
       
   753 
       
   754 			TEST_EXP(n == 2);
       
   755 			if (n != 2)
       
   756 				{
       
   757 				r = KErrUnknown;
       
   758 				Kern::ShBufClose(kbuf);
       
   759 				NKern::ThreadLeaveCS();
       
   760 
       
   761 				break;
       
   762 				}
       
   763 #ifdef TEST_CLIENT_THREAD
       
   764 			NKern::ThreadLeaveCS();
       
   765 
       
   766 			kumemput(a2, &handle, sizeof(handle));
       
   767 
       
   768 			NKern::ThreadEnterCS();
       
   769 			Kern::ShBufClose(kbuf);
       
   770 			NKern::ThreadLeaveCS();
       
   771 #else
       
   772 			NKern::ThreadLeaveCS();
       
   773 
       
   774 			Kern::ThreadRawWrite(iClient, a2, &handle, sizeof(handle), iClient);
       
   775 			
       
   776 			NKern::ThreadEnterCS();
       
   777 			Kern::DestroyVirtualPinObject(iPin);
       
   778 
       
   779 			// Close buffer - but it is still referenced by client handle
       
   780 			Kern::ShBufClose(kbuf);
       
   781 			NKern::ThreadLeaveCS();
       
   782 #endif
       
   783 			}
       
   784 		break;
       
   785 // ----------------------------------------------------------------------------
       
   786 // TInt ContiguousPoolKernel(TShPoolCreateInfo& aInfo)
       
   787 		case RShBufTestChannel::ETestCreatePoolContiguousPool:
       
   788 			{
       
   789 #ifdef TEST_CLIENT_THREAD
       
   790 			NKern::ThreadEnterCS();
       
   791 			iCreateinfo = new TShPoolCreateInfo;
       
   792 			NKern::ThreadLeaveCS();
       
   793 			TEST_EXP(iCreateinfo != NULL);
       
   794 			if(!iCreateinfo)
       
   795 				{
       
   796 				r = KErrNoMemory;
       
   797 				break;
       
   798 				}
       
   799 
       
   800 			kumemget(iCreateinfo, a1, sizeof(TShPoolInfo));
       
   801 #endif
       
   802 
       
   803 			TShPool* mainpool;
       
   804 			TShPool* otherpool;
       
   805 
       
   806 			NKern::ThreadEnterCS();
       
   807 
       
   808 			r = Kern::ShPoolCreate(otherpool, *iCreateinfo, ETrue, KDefaultPoolHandleFlags);
       
   809 			TEST_KERRNONE(r);
       
   810 
       
   811 			r = Kern::ShPoolSetBufferWindow(otherpool,-1);
       
   812 			TEST_KERRNONE(r);
       
   813 
       
   814 			iCreateinfo->SetContiguous();
       
   815 			TEST_KERRNONE(r);
       
   816 
       
   817 			r = Kern::ShPoolCreate(mainpool, *iCreateinfo, ETrue, KDefaultPoolHandleFlags);
       
   818 			NKern::ThreadEnterCS();
       
   819 			delete iCreateinfo;
       
   820 			iCreateinfo = NULL;
       
   821 			NKern::ThreadLeaveCS();
       
   822 			TEST_KERRNONE(r);
       
   823 
       
   824 			r = Kern::ShPoolSetBufferWindow(mainpool,-1);
       
   825 			TEST_KERRNONE(r);
       
   826 
       
   827 			TInt i;
       
   828 			TShBuf* mainbuf[KTestPoolSizeInBufs];
       
   829 			TShBuf* otherbuf[KTestPoolSizeInBufs];
       
   830 			for (i = 0; i < KTestPoolSizeInBufs; i++)
       
   831 				{
       
   832 				r = Kern::ShPoolAlloc(mainpool, mainbuf[i], 0);
       
   833 				if (r)
       
   834 					{
       
   835 					Kern::Printf("i=%d r=%d\n", i, r);
       
   836 					TEST_KERRNONE(r);
       
   837 					}
       
   838 				r = Kern::ShPoolAlloc(otherpool, otherbuf[i], 0);
       
   839 				if (r)
       
   840 					{
       
   841 					Kern::Printf("i=%d r=%d\n", i, r);
       
   842 					TEST_KERRNONE(r);
       
   843 					}
       
   844 				TBool iscontiguous;
       
   845 				iscontiguous = IsBufferContiguous(mainbuf[i]);
       
   846 				if (!iscontiguous)
       
   847 					{
       
   848 					Kern::Printf("i=%d\n", i, r);
       
   849 					TEST_EXP(iscontiguous);
       
   850 					}
       
   851 				// delay?
       
   852 				}
       
   853 
       
   854 			// Free every other buffer
       
   855 			for (i = 0; i < KTestPoolSizeInBufs; i += 2)
       
   856 				{
       
   857 				Kern::ShBufClose(mainbuf[i]);
       
   858 				Kern::ShBufClose(otherbuf[i]);
       
   859 				}
       
   860 
       
   861 			// Re-allocate buffers
       
   862 			for (i = 0; i < KTestPoolSizeInBufs; i += 2)
       
   863 				{
       
   864 				r = Kern::ShPoolAlloc(otherpool, otherbuf[i], 0);
       
   865 				if (r)
       
   866 					{
       
   867 					Kern::Printf("i=%d r=%d\n", i, r);
       
   868 					TEST_KERRNONE(r);
       
   869 					}
       
   870 				r = Kern::ShPoolAlloc(mainpool, mainbuf[i], 0);
       
   871 				if (r)
       
   872 					{
       
   873 					Kern::Printf("i=%d r=%d\n", i, r);
       
   874 					TEST_KERRNONE(r);
       
   875 					}
       
   876 				TBool iscontiguous;
       
   877 				iscontiguous = IsBufferContiguous(mainbuf[i]);
       
   878 				if (!iscontiguous)
       
   879 					{
       
   880 					Kern::Printf("i=%d\n", i, r);
       
   881 					TEST_EXP(iscontiguous);
       
   882 					// bang
       
   883 					}
       
   884 				}
       
   885 			for (i = 0; i < KTestPoolSizeInBufs; i++)
       
   886 				{
       
   887 				Kern::ShBufClose(mainbuf[i]);
       
   888 				Kern::ShBufClose(otherbuf[i]);
       
   889 				}
       
   890 
       
   891 			Kern::ShPoolClose(mainpool);
       
   892 			Kern::ShPoolClose(otherpool);
       
   893 			NKern::ThreadLeaveCS();
       
   894 			}
       
   895 		break;
       
   896 // ----------------------------------------------------------------------------
       
   897 // TInt CreatePoolPhysAddrCont(TInt aBufSize)
       
   898 // TInt CreatePoolPhysAddrNonCont(TInt aBufSize)
       
   899 		case RShBufTestChannel::ETestCreatePoolPhysAddrCont:
       
   900 		case RShBufTestChannel::ETestCreatePoolPhysAddrNonCont:
       
   901 			{
       
   902 			r = KErrNone;
       
   903 #ifndef __WINS__
       
   904 			TInt bufsize = (TInt) a1;
       
   905 			TInt minimumAlignmentLog2 = __e32_find_ms1_32(Cache::DmaBufferAlignment());
       
   906 			if (minimumAlignmentLog2 < 5)
       
   907 				minimumAlignmentLog2 = 5;
       
   908 			TInt pagesize;
       
   909 			r = Kern::HalFunction(EHalGroupKernel, EKernelHalPageSizeInBytes, &pagesize, 0);
       
   910 			TEST_KERRNONE(r);
       
   911 			if (r)
       
   912 				{
       
   913 				break;
       
   914 				}
       
   915 
       
   916 			if (bufsize > KMaxPhysicalMemoryBlockSize)
       
   917 				{
       
   918 				// Buffer too large
       
   919 				return KErrNone;
       
   920 				}
       
   921 			TInt physicalblocksize = RoundUp(128 * RoundUp(bufsize, Log2(minimumAlignmentLog2)), Log2(pagesize) + 1);
       
   922 			if (physicalblocksize > KMaxPhysicalMemoryBlockSize)
       
   923 				{
       
   924 				physicalblocksize = KMaxPhysicalMemoryBlockSize;
       
   925 				}
       
   926 			if (physicalblocksize < pagesize * 4)
       
   927 				{
       
   928 				physicalblocksize = pagesize * 4;
       
   929 				}
       
   930 
       
   931 			NKern::ThreadEnterCS();
       
   932 
       
   933 			// Allocate an array of physical addresses
       
   934 			TPhysAddr* addrtable = NULL;
       
   935 
       
   936 			// Allocate physical memory
       
   937 			TPhysAddr physaddr;
       
   938 			if (aReqNo == RShBufTestChannel::ETestCreatePoolPhysAddrCont)
       
   939 				{
       
   940 				r = Epoc::AllocPhysicalRam(physicalblocksize, physaddr, 0);
       
   941 				}
       
   942 			else
       
   943 				{
       
   944 				addrtable = (TPhysAddr*) Kern::Alloc((physicalblocksize / pagesize) * sizeof(TPhysAddr));
       
   945 				TEST_EXP(addrtable != NULL);
       
   946 				if (addrtable == NULL)
       
   947 					{
       
   948 					r = KErrNoMemory;
       
   949 					NKern::ThreadLeaveCS();
       
   950 					break;
       
   951 					}
       
   952 
       
   953 				TPhysAddr* addrtabletmp;
       
   954 				addrtabletmp = (TPhysAddr*) Kern::Alloc((physicalblocksize / pagesize / 2) * sizeof(TPhysAddr));
       
   955 				TEST_EXP(addrtabletmp != NULL);
       
   956 				if (addrtabletmp == NULL)
       
   957 					{
       
   958 					r = KErrNoMemory;
       
   959 					}
       
   960 				else
       
   961 					{
       
   962 					// Allocate discontiguous memory
       
   963 					r = Epoc::AllocPhysicalRam(1, addrtable);
       
   964 					TEST_KERRNONE(r);
       
   965 					if (r == KErrNone)
       
   966 						{
       
   967 						r = Epoc::AllocPhysicalRam(1, addrtabletmp); // 1 page gap
       
   968 						TEST_KERRNONE(r);
       
   969 						if (r == KErrNone)
       
   970 							{
       
   971 							r = Epoc::AllocPhysicalRam(physicalblocksize / pagesize / 2 - 1, addrtable + 1);
       
   972 							TEST_KERRNONE(r);
       
   973 							if (r == KErrNone)
       
   974 								{
       
   975 								r = Epoc::AllocPhysicalRam(physicalblocksize / pagesize / 2 - 1, addrtabletmp + 1); // big gap
       
   976 								TEST_KERRNONE(r);
       
   977 							if (r == KErrNone)
       
   978 									{
       
   979 									r = Epoc::AllocPhysicalRam(physicalblocksize / pagesize / 2, addrtable + physicalblocksize / pagesize / 2);
       
   980 									TEST_KERRNONE(r);
       
   981 									r = Epoc::FreePhysicalRam(physicalblocksize / pagesize / 2 - 1, addrtabletmp + 1);
       
   982 									TEST_KERRNONE(r);
       
   983 									}
       
   984 								}
       
   985 							r = Epoc::FreePhysicalRam(1, addrtabletmp);
       
   986 							TEST_KERRNONE(r);
       
   987 							}
       
   988 						}
       
   989 					Kern::Free(addrtabletmp);
       
   990 					}
       
   991 				}
       
   992 			
       
   993 			if (r)
       
   994 				{
       
   995 				Kern::Free(addrtable);
       
   996 				NKern::ThreadLeaveCS();
       
   997 				break;
       
   998 				}
       
   999 
       
  1000 			// Create pool
       
  1001 			TInt poolsizeinbufs;
       
  1002 			poolsizeinbufs = physicalblocksize / RoundUp(bufsize, minimumAlignmentLog2);
       
  1003 
       
  1004 			TShPool* pool = NULL;
       
  1005 			if (aReqNo == RShBufTestChannel::ETestCreatePoolPhysAddrCont)
       
  1006 				{
       
  1007 				TShPoolCreateInfo inf(TShPoolCreateInfo::EDevice, bufsize,
       
  1008 									  poolsizeinbufs, 0, physicalblocksize / pagesize, physaddr);
       
  1009 				r = Kern::ShPoolCreate(pool, inf, ETrue, KDefaultPoolHandleFlags);
       
  1010 				}
       
  1011 			else
       
  1012 				{
       
  1013 				TShPoolCreateInfo inf(TShPoolCreateInfo::EDevice, bufsize,
       
  1014 									  poolsizeinbufs, 0, physicalblocksize / pagesize, addrtable);
       
  1015 				r = Kern::ShPoolCreate(pool, inf, ETrue, KDefaultPoolHandleFlags);
       
  1016 				}
       
  1017 			TEST_KERRNONE(r);
       
  1018 			if (r == KErrNone)
       
  1019 				{
       
  1020 				// Do some buffer allocation with the pool
       
  1021 				TInt freecount1 = Kern::ShPoolFreeCount(pool);
       
  1022 				RPointerArray<TShBuf> bufarray;
       
  1023 				TInt allocated = 0;
       
  1024 				do
       
  1025 					{
       
  1026 					TShBuf* buf;
       
  1027 					r = Kern::ShPoolAlloc(pool, buf, 0);
       
  1028 					if (r == KErrNone)
       
  1029 						{
       
  1030 						TPtr8 ptr(Kern::ShBufPtr(buf), Kern::ShBufSize(buf), Kern::ShBufSize(buf));
       
  1031 						ptr.Fill('$');
       
  1032 						bufarray.Append(buf);
       
  1033 						allocated++;
       
  1034 						}
       
  1035 					}
       
  1036 				while (r == KErrNone);
       
  1037 				TInt freecount2 = Kern::ShPoolFreeCount(pool);
       
  1038 
       
  1039 				if (r != KErrNoMemory)
       
  1040 					{
       
  1041 					TEST_KERRNONE(r);
       
  1042 					}
       
  1043 				while (bufarray.Count())
       
  1044 					{
       
  1045 					if (bufarray[0])
       
  1046 						{
       
  1047 						Kern::ShBufClose(bufarray[0]);
       
  1048 						}
       
  1049 					bufarray.Remove(0);
       
  1050 					}
       
  1051 				TInt freecount3 = Kern::ShPoolFreeCount(pool);
       
  1052 				bufarray.Close();
       
  1053 				//
       
  1054 				r = Kern::ShPoolClose(pool);
       
  1055 				if (r>0)
       
  1056 					{
       
  1057 					r = KErrNone;
       
  1058 					}
       
  1059 
       
  1060 				TEST_KERRNONE(r);
       
  1061 
       
  1062 				if ((freecount1 != freecount3) || (freecount1 != allocated) || (freecount1 != poolsizeinbufs) || (freecount2))
       
  1063 					{
       
  1064 					r = KErrUnknown;
       
  1065 					Kern::Printf("fc1=%d fc2=%d fc3=%d alloc=%d", freecount1, freecount2, freecount3, allocated);
       
  1066 					TEST_EXP(EFalse);
       
  1067 					}
       
  1068 				}
       
  1069 			NKern::Sleep(5000);
       
  1070 			TInt r2;
       
  1071 			if (aReqNo == RShBufTestChannel::ETestCreatePoolPhysAddrCont)
       
  1072 				{
       
  1073 				r2 = Epoc::FreePhysicalRam(physaddr, physicalblocksize);
       
  1074 				}
       
  1075 			else
       
  1076 				{
       
  1077 				r2 = Epoc::FreePhysicalRam(physicalblocksize / pagesize, addrtable);
       
  1078 				Kern::Free(addrtable);
       
  1079 				}
       
  1080 			TEST_KERRNONE(r2);
       
  1081 			if (!r && r2)
       
  1082 				{
       
  1083 				r = r2; // if an error occurred whilst freeing physical memory, report it
       
  1084 				}
       
  1085 			NKern::ThreadLeaveCS();
       
  1086 #endif // __WINS__
       
  1087 			}
       
  1088 		break;
       
  1089 // ----------------------------------------------------------------------------
       
  1090 // TInt AllocateMax(TInt aPoolIndex, TInt& aAllocated)
       
  1091 		case RShBufTestChannel::ETestAllocateMax:
       
  1092 			{
       
  1093 			TInt r2;
       
  1094 			TInt poolindex = (TInt) a1;
       
  1095 			if ((poolindex != 0) && (poolindex != 1))
       
  1096 				{
       
  1097 				r2 = KErrArgument;
       
  1098 				break;
       
  1099 				}
       
  1100 			TShPoolInfo poolinfo;
       
  1101 			Kern::ShPoolGetInfo(iPools[poolindex], poolinfo);
       
  1102 
       
  1103 			NKern::ThreadEnterCS();
       
  1104 
       
  1105 			RPointerArray<TShBuf> bufarray;
       
  1106 			do
       
  1107 				{
       
  1108 				TShBuf* buf;
       
  1109 				r2 = Kern::ShPoolAlloc(iPools[poolindex], buf, 0);
       
  1110 				if(r2==KErrNoMemory && (TUint)bufarray.Count()<poolinfo.iMaxBufs)
       
  1111 					{
       
  1112 					NKern::Sleep(1000);
       
  1113 					r2 = Kern::ShPoolAlloc(iPools[poolindex], buf, 0);
       
  1114 					}
       
  1115 				if (r2 == KErrNone)
       
  1116 					{
       
  1117 					r2 = bufarray.Append(buf);
       
  1118 					TEST_KERRNONE(r2);
       
  1119 					if (r2!=KErrNone)
       
  1120 						{
       
  1121 						Kern::ShBufClose(buf);
       
  1122 						r2 = KErrGeneral;
       
  1123 						}
       
  1124 					}
       
  1125 				}
       
  1126 			while (r2 == KErrNone);
       
  1127 
       
  1128 			// close all buffers...
       
  1129 			TInt n = bufarray.Count();
       
  1130 			while (n)
       
  1131 				Kern::ShBufClose(bufarray[--n]);
       
  1132 
       
  1133 			if (r2 != KErrNoMemory)
       
  1134 				{
       
  1135 				TEST_KERRNONE(r2);
       
  1136 				}
       
  1137 			else
       
  1138 				{
       
  1139 				// Do it once more
       
  1140 				n = 0;
       
  1141 				while (n<bufarray.Count())
       
  1142 					{
       
  1143 					r2 = Kern::ShPoolAlloc(iPools[poolindex], bufarray[n], 0);
       
  1144 					if(r2==KErrNoMemory)
       
  1145 						{
       
  1146 						NKern::Sleep(1000);
       
  1147 						r2 = Kern::ShPoolAlloc(iPools[poolindex], bufarray[n], 0);
       
  1148 						}
       
  1149 					if (r2)
       
  1150 						{
       
  1151 						Kern::Printf("Line %d: n=%d r2=%d", __LINE__, n, r2);
       
  1152 						break;
       
  1153 						}
       
  1154 					++n;
       
  1155 					}
       
  1156 
       
  1157 				if (r2 == KErrNone)
       
  1158 					{
       
  1159 					TShBuf* extrabuf;
       
  1160 					r2 = Kern::ShPoolAlloc(iPools[poolindex], extrabuf, 0);
       
  1161 
       
  1162 					TEST_EXP(r2 == KErrNoMemory);
       
  1163 					}
       
  1164 
       
  1165 				while (n)
       
  1166 					Kern::ShBufClose(bufarray[--n]);
       
  1167 				}
       
  1168 			
       
  1169 			TInt allocated = bufarray.Count();
       
  1170 
       
  1171 			bufarray.Close();
       
  1172 			if (r2 == KErrNoMemory)
       
  1173 				{
       
  1174 				r = KErrNone;
       
  1175 				}
       
  1176 			else
       
  1177 				{
       
  1178 				r = r2;
       
  1179 				}
       
  1180 
       
  1181 #ifdef TEST_CLIENT_THREAD
       
  1182 			NKern::ThreadLeaveCS();
       
  1183 			kumemput(a2, &allocated, sizeof(allocated));
       
  1184 #else
       
  1185 			NKern::ThreadLeaveCS();
       
  1186 
       
  1187 			Kern::ThreadRawWrite(iClient, a2, &allocated, sizeof(allocated), iClient);
       
  1188 			
       
  1189 			NKern::ThreadEnterCS();
       
  1190 			Kern::DestroyVirtualPinObject(iPin);
       
  1191 			NKern::ThreadLeaveCS();
       
  1192 #endif
       
  1193 
       
  1194 			}
       
  1195 		break;
       
  1196 // ----------------------------------------------------------------------------
       
  1197 // TInt BufferAlignmentKernel(TInt aBufSize)
       
  1198 		case RShBufTestChannel::ETestBufferAlignmentKernel:
       
  1199 			{
       
  1200 			TInt bufsize = (TInt) a1;
       
  1201 			TInt alignment = (TInt) a2;
       
  1202 
       
  1203 			TInt pagesize;
       
  1204 			r = Kern::HalFunction(EHalGroupKernel, EKernelHalPageSizeInBytes, &pagesize, 0);
       
  1205 			TEST_KERRNONE(r);
       
  1206 			if (r)
       
  1207 				{
       
  1208 				break;
       
  1209 				}
       
  1210 
       
  1211 			NKern::ThreadEnterCS();
       
  1212 
       
  1213 			const TInt KNumBuffers = 20;
       
  1214 
       
  1215 			{
       
  1216 			TShPoolCreateInfo inf(TShPoolCreateInfo::ENonPageAlignedBuffer, bufsize, KNumBuffers, alignment); // TODO: Change minbufs back to 8 when the pool growing code works
       
  1217 			TShPool* pool;
       
  1218 			r = Kern::ShPoolCreate(pool, inf, ETrue, KDefaultPoolHandleFlags);
       
  1219 			TEST_KERRNONE(r);
       
  1220 			if (r)
       
  1221 				{
       
  1222 				NKern::ThreadLeaveCS();
       
  1223 				break;
       
  1224 				}
       
  1225 
       
  1226 			TInt j;
       
  1227 			TShBuf* buf[KNumBuffers];
       
  1228 			memclr(buf,sizeof(buf));
       
  1229 			for (j = 0; j < KNumBuffers; j++)
       
  1230 				{
       
  1231 				r = Kern::ShPoolAlloc(pool, buf[j], 0);
       
  1232 				TEST_KERRNONE(r);
       
  1233 				if (r)
       
  1234 					{
       
  1235 					Kern::Printf("i=%d j=%d", alignment, j);
       
  1236 					break;
       
  1237 					}
       
  1238 				}
       
  1239 			if (r == KErrNone)
       
  1240 				{
       
  1241 				if (alignment < KTestMinimumAlignmentLog2)
       
  1242 					{
       
  1243 					alignment = KTestMinimumAlignmentLog2;
       
  1244 					}
       
  1245 				for (j = 0; j < KNumBuffers; j++)
       
  1246 					{
       
  1247 					if (((TUint32) Kern::ShBufPtr(buf[j]) & ((1 << alignment) - 1)))
       
  1248 						{
       
  1249 						Kern::Printf("Pool%d buf[%d]->Base() == 0x%08x", alignment, j, Kern::ShBufPtr(buf[j]));
       
  1250 						r = KErrUnknown;
       
  1251 						break;
       
  1252 						}
       
  1253 					}
       
  1254 				}
       
  1255 			for (j = 0; j < KNumBuffers; j++)
       
  1256 				{
       
  1257 				if (buf[j])
       
  1258 					{
       
  1259 					Kern::ShBufClose(buf[j]);
       
  1260 					}
       
  1261 				}
       
  1262 			TInt r2;
       
  1263 			r2 = Kern::ShPoolClose(pool);
       
  1264 
       
  1265 			if (r2>0)
       
  1266 				{
       
  1267 				r2 = KErrNone;
       
  1268 				}
       
  1269 
       
  1270 			TEST_KERRNONE(r2);
       
  1271 			if (r == KErrNone)
       
  1272 				{
       
  1273 				r = r2;
       
  1274 				}
       
  1275 			if (r)
       
  1276 				{
       
  1277 				NKern::ThreadLeaveCS();
       
  1278 				break;
       
  1279 				}
       
  1280 			}
       
  1281 			// Page aligned buffers
       
  1282 			TShPoolCreateInfo inf(TShPoolCreateInfo::EPageAlignedBuffer, bufsize, KNumBuffers); // TODO: Change minbufs back to 8 when the pool growing code works
       
  1283 			TShPool* pool;
       
  1284 			r = Kern::ShPoolCreate(pool, inf, ETrue, KDefaultPoolHandleFlags);
       
  1285 			TEST_KERRNONE(r);
       
  1286 			if (r)
       
  1287 				{
       
  1288 				NKern::ThreadLeaveCS();
       
  1289 				break;
       
  1290 				}
       
  1291 
       
  1292 			r = Kern::ShPoolSetBufferWindow(pool,-1);
       
  1293 			TEST_KERRNONE(r);
       
  1294 			if (r)
       
  1295 				{
       
  1296 				Kern::ShPoolClose(pool);
       
  1297 				NKern::ThreadLeaveCS();
       
  1298 				break;
       
  1299 				}
       
  1300 
       
  1301 			TInt j;
       
  1302 			TShBuf* buf[KNumBuffers];
       
  1303 			memclr(buf,sizeof(buf));
       
  1304 			for (j = 0; j < KNumBuffers; j++)
       
  1305 				{
       
  1306 				r = Kern::ShPoolAlloc(pool, buf[j], 0);
       
  1307 				TEST_KERRNONE(r);
       
  1308 				if (r)
       
  1309 					{
       
  1310 					Kern::Printf("j=%d", j);
       
  1311 					break;
       
  1312 					}
       
  1313 				}
       
  1314 			if (r == KErrNone)
       
  1315 				{
       
  1316 				for (j = 0; j < KNumBuffers; j++)
       
  1317 					{
       
  1318 					if ((TUint32) Kern::ShBufPtr(buf[j]) & (pagesize - 1))
       
  1319 						{
       
  1320 						Kern::Printf("buf[%d]->Base() == 0x%08x", j, Kern::ShBufPtr(buf[j]));
       
  1321 						r = KErrUnknown;
       
  1322 						break;
       
  1323 						}
       
  1324 					}
       
  1325 				}
       
  1326 			for (j = 0; j < KNumBuffers; j++)
       
  1327 				{
       
  1328 				if (buf[j])
       
  1329 					{
       
  1330 					Kern::ShBufClose(buf[j]);
       
  1331 					}
       
  1332 				}
       
  1333 			TInt r2;
       
  1334 			r2 = Kern::ShPoolClose(pool);
       
  1335 			if (r2>0)
       
  1336 				{
       
  1337 				r2 = KErrNone;
       
  1338 				}
       
  1339 
       
  1340 			TEST_KERRNONE(r2);
       
  1341 			if (!r)
       
  1342 				{
       
  1343 				r = r2;
       
  1344 				}
       
  1345 
       
  1346 			NKern::ThreadLeaveCS();
       
  1347 			}
       
  1348 		break;
       
  1349 // ----------------------------------------------------------------------------
       
  1350 // TInt NegativeTestsKernel()
       
  1351 		case RShBufTestChannel::ETestNegativeTestsKernel:
       
  1352 			{
       
  1353 			TInt pagesize;
       
  1354 			r = Kern::HalFunction(EHalGroupKernel, EKernelHalPageSizeInBytes, &pagesize, 0);
       
  1355 			TEST_KERRNONE(r);
       
  1356 			if (r)
       
  1357 				{
       
  1358 				break;
       
  1359 				}
       
  1360 			
       
  1361             #define TEST_POOLCREATE_FAIL(i, p, e, r) \
       
  1362                 {                               \
       
  1363                 TInt r2;                        \
       
  1364                 TEST_ENTERCS();                 \
       
  1365                 r2 = Kern::ShPoolCreate(p, i, ETrue, KDefaultPoolHandleFlags);  \
       
  1366                 TEST_LEAVECS();                 \
       
  1367                 if (r2 != e)                    \
       
  1368                     {                           \
       
  1369                     Kern::Printf("Device drive (line %d) r=%d", __LINE__, r2); \
       
  1370                     TEST_EXP(EFalse);               \
       
  1371                     r = KErrUnknown;            \
       
  1372                     break;                      \
       
  1373                     }                           \
       
  1374                 }
       
  1375 
       
  1376 			TShPool* pool;
       
  1377 			{ TShPoolCreateInfo inf(TShPoolCreateInfo::EPageAlignedBuffer, 0, 0); TEST_POOLCREATE_FAIL(inf, pool, KErrArgument, r); }
       
  1378 			{ TShPoolCreateInfo inf(TShPoolCreateInfo::EPageAlignedBuffer, 100, 0); TEST_POOLCREATE_FAIL(inf, pool, KErrArgument, r); }
       
  1379 			{ TShPoolCreateInfo inf(TShPoolCreateInfo::EPageAlignedBuffer, 0, 100); TEST_POOLCREATE_FAIL(inf, pool, KErrArgument, r); }
       
  1380 			{ TShPoolCreateInfo inf(TShPoolCreateInfo::EPageAlignedBuffer, KMaxTUint, 10); TEST_POOLCREATE_FAIL(inf, pool, KErrArgument, r); }
       
  1381 			{ TShPoolCreateInfo inf(TShPoolCreateInfo::EPageAlignedBuffer, 10, KMaxTUint); TEST_POOLCREATE_FAIL(inf, pool, KErrArgument, r); }
       
  1382 			{ TShPoolCreateInfo inf(TShPoolCreateInfo::EPageAlignedBuffer, KMaxTUint, KMaxTUint); TEST_POOLCREATE_FAIL(inf, pool, KErrArgument, r); }
       
  1383 			{ TShPoolCreateInfo inf(TShPoolCreateInfo::EPageAlignedBuffer, 65537, 65536); TEST_POOLCREATE_FAIL(inf, pool, KErrArgument, r); }
       
  1384 			{ TShPoolCreateInfo inf(TShPoolCreateInfo::EPageAlignedBuffer, 10, 1 + (1 << (32 - Log2(pagesize)))); TEST_POOLCREATE_FAIL(inf, pool, KErrArgument, r); }
       
  1385 #ifndef __WINS__
       
  1386 			{ TShPoolCreateInfo inf(TShPoolCreateInfo::EPageAlignedBuffer, 128 * pagesize, (Kern::FreeRamInBytes() / (128 * pagesize)) + 1); TEST_POOLCREATE_FAIL(inf, pool, KErrNoMemory, r); }
       
  1387 #endif
       
  1388 			{ TShPoolCreateInfo inf(TShPoolCreateInfo::ENonPageAlignedBuffer, 0, 0, 0); TEST_POOLCREATE_FAIL(inf, pool, KErrArgument, r); }
       
  1389 			{ TShPoolCreateInfo inf(TShPoolCreateInfo::ENonPageAlignedBuffer, 100, 0, 0); TEST_POOLCREATE_FAIL(inf, pool, KErrArgument, r); }
       
  1390 			{ TShPoolCreateInfo inf(TShPoolCreateInfo::ENonPageAlignedBuffer, 0, 100, 0); TEST_POOLCREATE_FAIL(inf, pool, KErrArgument, r); }
       
  1391 			{ TShPoolCreateInfo inf(TShPoolCreateInfo::ENonPageAlignedBuffer, KMaxTUint, 10, 0); TEST_POOLCREATE_FAIL(inf, pool, KErrArgument, r); }
       
  1392 			{ TShPoolCreateInfo inf(TShPoolCreateInfo::ENonPageAlignedBuffer, 10, KMaxTUint, 0); TEST_POOLCREATE_FAIL(inf, pool, KErrArgument, r); }
       
  1393 			{ TShPoolCreateInfo inf(TShPoolCreateInfo::ENonPageAlignedBuffer, KMaxTUint, KMaxTUint, 0); TEST_POOLCREATE_FAIL(inf, pool, KErrArgument, r); }
       
  1394 			{ TShPoolCreateInfo inf(TShPoolCreateInfo::ENonPageAlignedBuffer, 65537, 65536, 0); TEST_POOLCREATE_FAIL(inf, pool, KErrArgument, r); }
       
  1395 			{ TShPoolCreateInfo inf(TShPoolCreateInfo::ENonPageAlignedBuffer, 10, 10, KMaxTUint); TEST_POOLCREATE_FAIL(inf, pool, KErrArgument, r); }
       
  1396 			{ TShPoolCreateInfo inf(TShPoolCreateInfo::ENonPageAlignedBuffer, 10, 10, 33); TEST_POOLCREATE_FAIL(inf, pool, KErrArgument, r); }
       
  1397 			{ TShPoolCreateInfo inf(TShPoolCreateInfo::ENonPageAlignedBuffer, 10, 300, 24); TEST_POOLCREATE_FAIL(inf, pool, KErrArgument, r); }
       
  1398 			{ TShPoolCreateInfo inf(TShPoolCreateInfo::ENonPageAlignedBuffer, 10, 65537, 16); TEST_POOLCREATE_FAIL(inf, pool, KErrArgument, r); }
       
  1399 			{ TShPoolCreateInfo inf(TShPoolCreateInfo::ENonPageAlignedBuffer, 10, 10, Log2(pagesize) + 1); TEST_POOLCREATE_FAIL(inf, pool, KErrArgument, r); }
       
  1400 			{ TShPoolCreateInfo inf(TShPoolCreateInfo::ENonPageAlignedBuffer, 128, 10, 0); inf.SetGuardPages(); TEST_POOLCREATE_FAIL(inf, pool, KErrArgument, r); }
       
  1401 			}
       
  1402 		break;
       
  1403 // ----------------------------------------------------------------------------
       
  1404 
       
  1405 // ----------------------------------------------------------------------------
       
  1406 // TInt RShBufTestChannel::PinBuffer(TInt aPoolHandle, TInt aBufferHandle)
       
  1407 #ifndef __WINS__
       
  1408 		case RShBufTestChannel::ETestPinBuffer:
       
  1409 			{
       
  1410 			TInt rignore;
       
  1411 			TInt pagesize;
       
  1412 			r = Kern::HalFunction(EHalGroupKernel, EKernelHalPageSizeInBytes, &pagesize, 0);
       
  1413 			TEST_KERRNONE(r);
       
  1414 			if (r)
       
  1415 				{
       
  1416 				break;
       
  1417 				}
       
  1418 			TShPool* upool = NULL;
       
  1419 			TShBuf* ubufu = NULL; // User buffer unmapped
       
  1420 			TShBuf* ubufm = NULL; // User buffer mapped
       
  1421 			DThread* tP;
       
  1422 #ifdef TEST_CLIENT_THREAD
       
  1423 			tP=&Kern::CurrentThread();
       
  1424 #else
       
  1425 			tP=iClient;
       
  1426 #endif
       
  1427 
       
  1428 			// Create pin object
       
  1429 			TPhysicalPinObject* pinobj;
       
  1430 			TEST_ENTERCS();
       
  1431 			r = Kern::CreatePhysicalPinObject(pinobj);
       
  1432 			TEST_LEAVECS();
       
  1433 			TEST_KERRNONE(r);
       
  1434 			if (r)
       
  1435 				{
       
  1436 				break;
       
  1437 				}
       
  1438 
       
  1439 			// Open user pool
       
  1440 			TEST_ENTERCS();
       
  1441 			r = Kern::ShPoolOpen(upool, tP, (TInt) a1, ETrue, KDefaultPoolHandleFlags);
       
  1442 			TEST_LEAVECS();
       
  1443 			TEST_KERRNONE(r);
       
  1444 			if (r)
       
  1445 				{
       
  1446 				TEST_ENTERCS();
       
  1447 				rignore = Kern::DestroyPhysicalPinObject(pinobj);
       
  1448 				TEST_LEAVECS();
       
  1449 				TEST_KERRNONE(rignore);
       
  1450 				break;
       
  1451 				}
       
  1452 			TShPoolInfo poolinfo;
       
  1453 			Kern::ShPoolGetInfo(upool, poolinfo);
       
  1454 
       
  1455 			// Open user buffer but do not map it
       
  1456 			TEST_ENTERCS();
       
  1457 			r = Kern::ShBufOpen(ubufu, tP, (TInt) a2);
       
  1458 			TEST_LEAVECS();
       
  1459 			TEST_KERRNONE(r);
       
  1460 			if (r)
       
  1461 				{
       
  1462 				TEST_ENTERCS();
       
  1463 				rignore = Kern::DestroyPhysicalPinObject(pinobj);
       
  1464 				TEST_LEAVECS();
       
  1465 				TEST_KERRNONE(rignore);
       
  1466 
       
  1467 				TEST_ENTERCS();
       
  1468 				rignore = Kern::ShPoolClose(upool);
       
  1469 				TEST_LEAVECS();
       
  1470 				TEST_KERRNONE(rignore);
       
  1471 
       
  1472 				break;
       
  1473 				}
       
  1474 
       
  1475 			// Allocate an array of physical addresses
       
  1476 			TPhysAddr* addrtable;
       
  1477 			TUint size = Kern::ShBufSize(ubufu);
       
  1478 			TEST_ENTERCS();
       
  1479 			addrtable = (TPhysAddr*) Kern::Alloc((RoundUp(size, Log2(pagesize)) / pagesize) * sizeof(TPhysAddr));
       
  1480 			TEST_LEAVECS();
       
  1481 			TEST_EXP(addrtable != NULL);
       
  1482 			if (!addrtable)
       
  1483 				{
       
  1484 				TEST_ENTERCS();
       
  1485 				rignore = Kern::DestroyPhysicalPinObject(pinobj);
       
  1486 				TEST_LEAVECS();
       
  1487 				TEST_KERRNONE(rignore);
       
  1488 				TEST_ENTERCS();
       
  1489 				rignore = Kern::ShBufClose(ubufu);
       
  1490 				TEST_LEAVECS();
       
  1491 
       
  1492 				TEST_KERRNONE(rignore);
       
  1493 				TEST_ENTERCS();
       
  1494 				rignore = Kern::ShPoolClose(upool);
       
  1495 				TEST_LEAVECS();
       
  1496 				TEST_KERRNONE(rignore);
       
  1497 				r = KErrNoMemory;
       
  1498 
       
  1499 				break;
       
  1500 				}
       
  1501 
       
  1502 			// Pin buffer
       
  1503 			TPhysAddr addr;
       
  1504 			TUint32 mapattr;
       
  1505 			TUint color;
       
  1506 			NKern::ThreadEnterCS();
       
  1507 			r = Kern::ShBufPin(ubufu, pinobj, ETrue, addr, addrtable, mapattr, color);
       
  1508 			NKern::ThreadLeaveCS();
       
  1509 			TEST_KERRNONE(r);
       
  1510 			if (addr != addrtable[0])
       
  1511 				{
       
  1512 				TEST_EXP(addr == KPhysAddrInvalid);
       
  1513 				if (poolinfo.iFlags & EShPoolContiguous)
       
  1514 					{
       
  1515 					TEST_EXP(EFalse); // Shouldn't happen with contiguous pools
       
  1516 					Kern::Printf("addr=0x%08x addrtable[0]=0x%08x", addr, addrtable[0]);
       
  1517 					r = KErrUnknown;
       
  1518 					}
       
  1519 				else
       
  1520 					{
       
  1521 					if (addr != KPhysAddrInvalid)
       
  1522 						{
       
  1523 						TEST_EXP(EFalse); // if buffer is not contiguous addr must be KPhysAddrInvalid
       
  1524 						Kern::Printf("addr=0x%08x addrtable[0]=0x%08x", addr, addrtable[0]);
       
  1525 						r = KErrUnknown;
       
  1526 						}
       
  1527 					}
       
  1528 				}
       
  1529 			// Leave later if this fails
       
  1530 
       
  1531 			// Destroy pin object
       
  1532 			TEST_ENTERCS();
       
  1533 			TInt r2 = Kern::DestroyPhysicalPinObject(pinobj);
       
  1534 			TEST_LEAVECS();
       
  1535 			TEST_KERRNONE(r2);
       
  1536 
       
  1537 			// Close unmapped buffer
       
  1538 			TEST_ENTERCS();
       
  1539 			rignore = Kern::ShBufClose(ubufu);
       
  1540 			TEST_LEAVECS();
       
  1541 
       
  1542 			TEST_KERRNONE(rignore);
       
  1543 
       
  1544 			// Leave test now if previous call to Kern::ShBufPin failed
       
  1545 			if (r)
       
  1546 				{
       
  1547 				TEST_ENTERCS();
       
  1548 				Kern::Free(addrtable);
       
  1549 				rignore = Kern::ShPoolClose(upool);
       
  1550 				TEST_LEAVECS();
       
  1551 
       
  1552 				TEST_KERRNONE(rignore);
       
  1553 				
       
  1554 				break;
       
  1555 				}
       
  1556 
       
  1557 			// Open window if pool is buffer-aligned
       
  1558 			if (poolinfo.iFlags & EShPoolPageAlignedBuffer)
       
  1559 				{
       
  1560 				NKern::ThreadEnterCS();
       
  1561 				r = Kern::ShPoolSetBufferWindow(upool, -1);
       
  1562 				NKern::ThreadLeaveCS();
       
  1563 				TEST_KERRNONE(r);
       
  1564 				if (r)
       
  1565 					{
       
  1566 					TEST_ENTERCS();
       
  1567 					Kern::Free(addrtable);
       
  1568 					rignore = Kern::ShPoolClose(upool);
       
  1569 
       
  1570 					TEST_LEAVECS();
       
  1571 					TEST_KERRNONE(rignore);
       
  1572 					
       
  1573 					break;
       
  1574 					}
       
  1575 				}
       
  1576 
       
  1577 			// Open user buffer and map it this time
       
  1578 			TEST_ENTERCS();
       
  1579 			r = Kern::ShBufOpen(ubufm, tP, (TInt) a2);
       
  1580 			TEST_LEAVECS();
       
  1581 			TEST_KERRNONE(r);
       
  1582 			if (r)
       
  1583 				{
       
  1584 				TEST_ENTERCS();
       
  1585 				Kern::Free(addrtable);
       
  1586 				rignore = Kern::ShPoolClose(upool);
       
  1587 				TEST_LEAVECS();
       
  1588 
       
  1589 				TEST_KERRNONE(rignore);
       
  1590 
       
  1591 				break;
       
  1592 				}
       
  1593 
       
  1594 			// Ensure that physical addresses match
       
  1595 			TUint8* ptr = Kern::ShBufPtr(ubufm);
       
  1596 			TEST_EXP(ptr != NULL);
       
  1597 			TBool isok = ETrue;
       
  1598 			TInt i;
       
  1599 			for (i = 0; i < RoundUp(size, Log2(pagesize)) / pagesize; i++)
       
  1600 				{
       
  1601 				TPhysAddr current = Epoc::LinearToPhysical((TLinAddr) ptr + i * pagesize);
       
  1602 				if (current != addrtable[i])
       
  1603 					{
       
  1604 					Kern::Printf("Page %d: Current=0x%08x addrtable=0x%08x (linaddr=0x%08x)", i, current, addrtable[i], ptr + i * pagesize);
       
  1605 					isok = EFalse;
       
  1606 					break;
       
  1607 					}
       
  1608 				}
       
  1609 			if (!isok)
       
  1610 				{
       
  1611 				r = KErrUnknown;
       
  1612 				}
       
  1613 			TEST_KERRNONE(r);
       
  1614 
       
  1615 			// Close mapped buffer
       
  1616 			TEST_ENTERCS();
       
  1617 			rignore = Kern::ShBufClose(ubufm);
       
  1618 			TEST_LEAVECS();
       
  1619 
       
  1620 			TEST_KERRNONE(rignore);
       
  1621 
       
  1622 			// Close pool
       
  1623 			TEST_ENTERCS();
       
  1624 			rignore = Kern::ShPoolClose(upool);
       
  1625 			TEST_LEAVECS();
       
  1626 
       
  1627 			TEST_KERRNONE(rignore);
       
  1628 
       
  1629 			// Free address table
       
  1630 			TEST_ENTERCS();
       
  1631 			Kern::Free(addrtable);
       
  1632 			TEST_LEAVECS();
       
  1633 
       
  1634 			if (!r && r2)
       
  1635 				{
       
  1636 				r = r2;
       
  1637 				}
       
  1638 			}
       
  1639 		break;
       
  1640 #endif // __WINS__
       
  1641 // ----------------------------------------------------------------------------
       
  1642 		case RShBufTestChannel::EFromRShBufProcessAndReturn:
       
  1643 			{
       
  1644 			// inline TInt FromRShBufProcessAndReturn(TInt aHandle);
       
  1645 			TInt bufsize = (TInt) a1;
       
  1646 
       
  1647 			TEST_ENTERCS();
       
  1648 			// Allocate kernel-side buffer
       
  1649 			TShBuf* kbuf;
       
  1650 			r = Kern::ShPoolAlloc(iPools[0], kbuf, 0);
       
  1651 
       
  1652 			if (r)
       
  1653 				{
       
  1654 				TEST_LEAVECS();
       
  1655 				break;
       
  1656 				}
       
  1657 
       
  1658 			TUint8*  ptr = Kern::ShBufPtr(kbuf);
       
  1659 			TInt* lengthPtr = (TInt*)ptr;
       
  1660 			*lengthPtr = bufsize - 2;
       
  1661 
       
  1662 #if 0 // do not cache
       
  1663 			for(TInt pos = 4;  pos < bufsize;  pos++)
       
  1664 				{
       
  1665 				ptr[pos] = (TUint8)(pos & 31);
       
  1666 				}
       
  1667 #endif
       
  1668 
       
  1669 			// Now create a handle for the client
       
  1670 			TInt handle;
       
  1671 #ifdef TEST_CLIENT_THREAD
       
  1672 			handle = Kern::ShBufMakeHandleAndOpen(kbuf, NULL);
       
  1673 #else
       
  1674 			handle = Kern::ShBufMakeHandleAndOpen(kbuf, iClient);
       
  1675 #endif
       
  1676 
       
  1677 			if (handle < 0)
       
  1678 				{
       
  1679 				r = handle;
       
  1680 				Kern::ShBufClose(kbuf);
       
  1681 				TEST_LEAVECS();
       
  1682 				break;
       
  1683 				}
       
  1684 
       
  1685 			// Close buffer - but it is still referenced by client handle
       
  1686 			Kern::ShBufClose(kbuf);
       
  1687 			TEST_LEAVECS();
       
  1688 
       
  1689 			r=handle;
       
  1690 			}
       
  1691 		break;
       
  1692 		case RShBufTestChannel::EFromRShBufProcessAndRelease:
       
  1693 			{
       
  1694 			// inline TInt FromRShBufProcessAndRelease(TInt aHandle);
       
  1695 			TShBuf* ubuf = NULL;
       
  1696 			DThread* tP;
       
  1697 
       
  1698 #ifdef TEST_CLIENT_THREAD
       
  1699 			tP=&Kern::CurrentThread();
       
  1700 #else
       
  1701 			tP=iClient;
       
  1702 #endif
       
  1703 
       
  1704 			TEST_ENTERCS();
       
  1705 			r = Kern::ShBufOpen(ubuf, tP, (TInt) a1);
       
  1706 			// close handle on behalf of user side application
       
  1707 			Kern::CloseHandle(tP, (TInt) a1);
       
  1708 			TEST_LEAVECS();
       
  1709 
       
  1710 			if(r!=KErrNone)
       
  1711 				{
       
  1712 				Kern::Printf("Buf not found");
       
  1713 				break;
       
  1714 				}
       
  1715 
       
  1716 #ifdef _DEBUG
       
  1717 			TUint8*  dataPtr = Kern::ShBufPtr(ubuf);
       
  1718 
       
  1719 			TInt*  lengthPtr = (TInt*)(&dataPtr[0]);
       
  1720 			
       
  1721 			for(TInt pos = 4;  pos < *lengthPtr;  pos++)
       
  1722 				{
       
  1723 				if (dataPtr[pos] != (TUint8)(pos & 31))
       
  1724 					{
       
  1725 					r=KErrCorrupt;
       
  1726 					Kern::Printf("Buf corrupt");
       
  1727 					break;
       
  1728 					}
       
  1729 				}
       
  1730 #endif
       
  1731 
       
  1732 			TEST_ENTERCS();
       
  1733 			Kern::ShBufClose(ubuf);
       
  1734 			TEST_LEAVECS();
       
  1735 
       
  1736 			r=KErrNone;
       
  1737 			}
       
  1738 		break;
       
  1739 		case RShBufTestChannel::EFromTPtr8ProcessAndReturn:
       
  1740 			{
       
  1741 			TInt bufsize = (TInt) a2;
       
  1742 			TPtr8 rxBuf(iDriverRxBuffer,sizeof(iDriverRxBuffer),sizeof(iDriverRxBuffer));
       
  1743 
       
  1744 #if 0
       
  1745 			for(TInt pos = 0;  pos < bufsize;  pos++)
       
  1746 				{
       
  1747 				rxBuf[pos] = (TUint8)(pos & 31);
       
  1748 				}
       
  1749 #endif
       
  1750 			rxBuf.SetLength(bufsize-2);
       
  1751 
       
  1752 #ifdef TEST_CLIENT_THREAD
       
  1753 			Kern::KUDesPut(*(TDes8*)a1, rxBuf);		// put content from test app
       
  1754 			r=KErrNone;
       
  1755 #else
       
  1756 			r = Kern::ThreadDesWrite(iClient, a1, rxBuf, 0, iClient);
       
  1757 			
       
  1758 			NKern::ThreadEnterCS();
       
  1759 			Kern::DestroyVirtualPinObject(iPin);
       
  1760 			NKern::ThreadLeaveCS();
       
  1761 #endif
       
  1762 			}
       
  1763 		break;
       
  1764 		case RShBufTestChannel::EFromTPtr8ProcessAndRelease:
       
  1765 			{
       
  1766 			// inline TInt FromTPtr8ProcessAndRelease(TDes8& aBuf);
       
  1767 #if defined _DEBUG  || defined TEST_CLIENT_THREAD
       
  1768 			TPtr8 bufp(iDriverTxBuffer, sizeof(iDriverTxBuffer), sizeof(iDriverTxBuffer));
       
  1769 #endif
       
  1770 #ifdef TEST_CLIENT_THREAD
       
  1771 			Kern::KUDesGet(bufp,*(const TDesC8*)a1);		// get content from test app
       
  1772 #endif
       
  1773 			
       
  1774 #ifdef _DEBUG
       
  1775 			TUint8* bufptr = const_cast<TUint8*>(bufp.Ptr());
       
  1776 			for(TInt pos = 0; pos < bufp.Length();  pos++)
       
  1777 				{
       
  1778 				if (bufptr[pos] != (TUint8)(pos & 31))
       
  1779 					{
       
  1780 					r=KErrCorrupt;
       
  1781 					Kern::Printf("Buf corrupt");
       
  1782 					break;
       
  1783 					}
       
  1784 				}
       
  1785 #endif
       
  1786 
       
  1787 			// Nothing to release here!
       
  1788 			
       
  1789 			r=KErrNone;
       
  1790 			}
       
  1791 		break;
       
  1792 		}
       
  1793 	
       
  1794 	return r;
       
  1795 	}