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