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