os/kernelhwsrv/kerneltest/e32test/misc/d_ipccpy.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) 1997-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\misc\d_ipccpy.cpp
sl@0
    15
// LDD for testing IPC copy functions
sl@0
    16
// 
sl@0
    17
//
sl@0
    18
sl@0
    19
#include "platform.h"
sl@0
    20
#include <kernel/kern_priv.h>
sl@0
    21
#include "d_ipccpy.h"
sl@0
    22
sl@0
    23
const TInt KMajorVersionNumber=0;
sl@0
    24
const TInt KMinorVersionNumber=1;
sl@0
    25
const TInt KBuildVersionNumber=1;
sl@0
    26
sl@0
    27
const TInt KBigBufferSize = 65536;
sl@0
    28
sl@0
    29
_LIT(KDIpcCpyPanicCategory,"DIpcCpy");
sl@0
    30
sl@0
    31
class DIpcCpyFactory : public DLogicalDevice
sl@0
    32
//
sl@0
    33
// IPC copy LDD factory
sl@0
    34
//
sl@0
    35
	{
sl@0
    36
public:
sl@0
    37
	DIpcCpyFactory();
sl@0
    38
	~DIpcCpyFactory();
sl@0
    39
	virtual TInt Install();						//overriding pure virtual
sl@0
    40
	virtual void GetCaps(TDes8& aDes) const;	//overriding pure virtual
sl@0
    41
	virtual TInt Create(DLogicalChannelBase*& aChannel);	//overriding pure virtual
sl@0
    42
sl@0
    43
private:
sl@0
    44
	TDynamicDfcQue* iDfcQ;
sl@0
    45
	};
sl@0
    46
sl@0
    47
class DIpcCpy : public DLogicalChannel
sl@0
    48
//
sl@0
    49
// Millisecond timer LDD channel
sl@0
    50
//
sl@0
    51
	{
sl@0
    52
public:
sl@0
    53
	DIpcCpy(TDfcQue* aDfcQ);
sl@0
    54
	virtual ~DIpcCpy();
sl@0
    55
protected:
sl@0
    56
	virtual TInt DoCreate(TInt aUnit, const TDesC8* anInfo, const TVersion& aVer);
sl@0
    57
	virtual TInt Request(TInt aFunc, TAny* a1, TAny* a2);
sl@0
    58
	virtual void HandleMsg(TMessageBase* aMsg);
sl@0
    59
public:
sl@0
    60
	void TimerExpired();
sl@0
    61
	TInt CreateHardwareChunks(TPtr8& aUserDes);
sl@0
    62
sl@0
    63
	// Panic reasons
sl@0
    64
	enum TPanic
sl@0
    65
		{
sl@0
    66
		ERequestAlreadyPending = 1
sl@0
    67
		};
sl@0
    68
public:
sl@0
    69
	DThread* iThread;
sl@0
    70
	TClientRequest* iAsyncRequest;
sl@0
    71
	TAny* iDest;
sl@0
    72
	TInt iSeqNum;
sl@0
    73
	NTimer iTimer;
sl@0
    74
	TDfc iDfc;
sl@0
    75
	TUint8 iBuffer[260];
sl@0
    76
	TUint8* iBigBuffer;
sl@0
    77
#ifdef __EPOC32__
sl@0
    78
	DPlatChunkHw* iHwChunks[RIpcCpy::ENumHwChunkTypes];
sl@0
    79
#endif
sl@0
    80
	TLinAddr iHwChunkLinAddrs[RIpcCpy::ENumHwChunkTypes];
sl@0
    81
	};
sl@0
    82
sl@0
    83
DECLARE_STANDARD_LDD()
sl@0
    84
	{
sl@0
    85
    return new DIpcCpyFactory;
sl@0
    86
    }
sl@0
    87
sl@0
    88
DIpcCpyFactory::DIpcCpyFactory()
sl@0
    89
//
sl@0
    90
// Constructor
sl@0
    91
//
sl@0
    92
    {
sl@0
    93
    iVersion=TVersion(KMajorVersionNumber,KMinorVersionNumber,KBuildVersionNumber);
sl@0
    94
    //iParseMask=0;//No units, no info, no PDD
sl@0
    95
    //iUnitsMask=0;//Only one thing
sl@0
    96
    }
sl@0
    97
sl@0
    98
DIpcCpyFactory::~DIpcCpyFactory()
sl@0
    99
//
sl@0
   100
// Destructor
sl@0
   101
//
sl@0
   102
	{
sl@0
   103
	if (iDfcQ)
sl@0
   104
		iDfcQ->Destroy();
sl@0
   105
	}
sl@0
   106
sl@0
   107
TInt DIpcCpyFactory::Create(DLogicalChannelBase*& aChannel)
sl@0
   108
//
sl@0
   109
// Create a new DIpcCpy on this logical device
sl@0
   110
//
sl@0
   111
    {
sl@0
   112
	aChannel=new DIpcCpy(iDfcQ);
sl@0
   113
    return aChannel?KErrNone:KErrNoMemory;
sl@0
   114
    }
sl@0
   115
sl@0
   116
const TInt KIpcCpyThreadPriority = 27;
sl@0
   117
_LIT(KIpcCpyThread,"IpcCpyThread");
sl@0
   118
sl@0
   119
TInt DIpcCpyFactory::Install()
sl@0
   120
//
sl@0
   121
// Install the LDD - overriding pure virtual
sl@0
   122
//
sl@0
   123
    {
sl@0
   124
	// Allocate a kernel thread to run the DFC 
sl@0
   125
	TInt r = Kern::DynamicDfcQCreate(iDfcQ, KIpcCpyThreadPriority, KIpcCpyThread);
sl@0
   126
sl@0
   127
#ifdef CPU_AFFINITY_ANY
sl@0
   128
	NKern::ThreadSetCpuAffinity((NThread*)(iDfcQ->iThread), KCpuAffinityAny);			
sl@0
   129
#endif
sl@0
   130
sl@0
   131
	if (r != KErrNone)
sl@0
   132
		return r; 	
sl@0
   133
sl@0
   134
    return SetName(&KIpcCpyLddName);
sl@0
   135
    }
sl@0
   136
sl@0
   137
void DIpcCpyFactory::GetCaps(TDes8& aDes) const
sl@0
   138
//
sl@0
   139
// Get capabilities - overriding pure virtual
sl@0
   140
//
sl@0
   141
    {
sl@0
   142
    TCapsIpcCpyV01 b;
sl@0
   143
    b.iVersion=TVersion(KMajorVersionNumber,KMinorVersionNumber,KBuildVersionNumber);
sl@0
   144
    Kern::InfoCopy(aDes,(TUint8*)&b,sizeof(b));
sl@0
   145
    }
sl@0
   146
sl@0
   147
void timerExpired(TAny* aPtr)
sl@0
   148
	{
sl@0
   149
	DIpcCpy* p=(DIpcCpy*)aPtr;
sl@0
   150
	p->iDfc.Add();
sl@0
   151
	}
sl@0
   152
sl@0
   153
void dfcFn(TAny* aPtr)
sl@0
   154
	{
sl@0
   155
	DIpcCpy* p=(DIpcCpy*)aPtr;
sl@0
   156
	p->TimerExpired();
sl@0
   157
	}
sl@0
   158
sl@0
   159
DIpcCpy::DIpcCpy(TDfcQue* aDfcQ)
sl@0
   160
//
sl@0
   161
// Constructor
sl@0
   162
//
sl@0
   163
	:	iTimer(timerExpired,this),
sl@0
   164
		iDfc(dfcFn,this,aDfcQ,1)
sl@0
   165
    {
sl@0
   166
	iThread=&Kern::CurrentThread();
sl@0
   167
	iThread->Open();
sl@0
   168
//	iSeqNum=0;
sl@0
   169
//	iDest=NULL;
sl@0
   170
	SetDfcQ(aDfcQ);
sl@0
   171
    }
sl@0
   172
sl@0
   173
DIpcCpy::~DIpcCpy()
sl@0
   174
	{
sl@0
   175
	if (iAsyncRequest)
sl@0
   176
		{
sl@0
   177
		Kern::QueueRequestComplete(iThread, iAsyncRequest, KErrCancel);	// does nothing if request not pending
sl@0
   178
		Kern::DestroyClientRequest(iAsyncRequest);
sl@0
   179
		}
sl@0
   180
	Kern::Free(iBigBuffer);
sl@0
   181
	Kern::SafeClose((DObject*&)iThread, NULL);
sl@0
   182
sl@0
   183
#ifdef __EPOC32__
sl@0
   184
	for(TInt i=0; i<RIpcCpy::ENumHwChunkTypes; i++)
sl@0
   185
		Kern::SafeClose((DObject*&)iHwChunks[i], NULL);
sl@0
   186
#endif
sl@0
   187
	}
sl@0
   188
sl@0
   189
TInt DIpcCpy::DoCreate(TInt /*aUnit*/, const TDesC8* /*anInfo*/, const TVersion& aVer)
sl@0
   190
//
sl@0
   191
// Create channel
sl@0
   192
//
sl@0
   193
    {
sl@0
   194
sl@0
   195
    if (!Kern::QueryVersionSupported(TVersion(KMajorVersionNumber,KMinorVersionNumber,KBuildVersionNumber),aVer))
sl@0
   196
    	return KErrNotSupported;
sl@0
   197
	TInt r = Kern::CreateClientRequest(iAsyncRequest);
sl@0
   198
	if (r!=KErrNone)
sl@0
   199
		return r;
sl@0
   200
	iBigBuffer = (TUint8*)Kern::Alloc(KBigBufferSize);
sl@0
   201
	if (!iBigBuffer)
sl@0
   202
		return KErrNoMemory;
sl@0
   203
	iMsgQ.Receive();
sl@0
   204
	return KErrNone;
sl@0
   205
	}
sl@0
   206
sl@0
   207
sl@0
   208
TInt DIpcCpy::CreateHardwareChunks(TPtr8& aUserDes)
sl@0
   209
	{
sl@0
   210
#ifndef __EPOC32__
sl@0
   211
	(void)aUserDes;
sl@0
   212
	return KErrNone;
sl@0
   213
#else
sl@0
   214
	NKern::ThreadEnterCS();
sl@0
   215
	TInt r;
sl@0
   216
	TUint32 size=Kern::RoundToPageSize(1);
sl@0
   217
#ifdef __X86__
sl@0
   218
	const TUint attrs[] = { EMapAttrSupRw, EMapAttrUserRw, EMapAttrReadUser }; // X86 does support EMapAttrUserRo, so use EMapAttrReadUser
sl@0
   219
#else
sl@0
   220
	const TUint attrs[] = { EMapAttrSupRw, EMapAttrUserRw, EMapAttrUserRo };
sl@0
   221
#endif
sl@0
   222
	for(TInt i=0; i<RIpcCpy::ENumHwChunkTypes; i++)
sl@0
   223
		{
sl@0
   224
		TPhysAddr phys;
sl@0
   225
		r = Epoc::AllocPhysicalRam(size,phys);
sl@0
   226
		if(r!=KErrNone)
sl@0
   227
			{
sl@0
   228
			NKern::ThreadLeaveCS();
sl@0
   229
			return r;
sl@0
   230
			}
sl@0
   231
sl@0
   232
		TChunkCreateInfo info;
sl@0
   233
		info.iType = TChunkCreateInfo::ESharedKernelMultiple;
sl@0
   234
		info.iMaxSize = size;
sl@0
   235
		info.iMapAttr = 0;
sl@0
   236
		info.iOwnsMemory = EFalse;
sl@0
   237
		DChunk* chunk;
sl@0
   238
		TLinAddr base;
sl@0
   239
		TUint32 attr;
sl@0
   240
		r = Kern::ChunkCreate(info,chunk,base,attr);
sl@0
   241
		if(r==KErrNone)
sl@0
   242
			{
sl@0
   243
			r=Kern::ChunkCommitPhysical(chunk, 0, size, phys);
sl@0
   244
			if(r==KErrNone)
sl@0
   245
				{
sl@0
   246
				memcpy((TAny*)base,&aUserDes,sizeof(TPtr8));
sl@0
   247
				}
sl@0
   248
			Kern::ChunkClose(chunk);
sl@0
   249
			if(r==KErrNone)
sl@0
   250
				r = DPlatChunkHw::New(iHwChunks[i], phys, size, attrs[i]);
sl@0
   251
			}
sl@0
   252
sl@0
   253
		if(r==KErrNone)
sl@0
   254
			{
sl@0
   255
			iHwChunkLinAddrs[i] = iHwChunks[i]->LinearAddress();
sl@0
   256
			}
sl@0
   257
		else if (r==KErrNotSupported) //ARMv6K && ARMv7 do not support EMapAttrUserRo
sl@0
   258
			{
sl@0
   259
			iHwChunkLinAddrs[i] = 0;
sl@0
   260
			r = KErrNone;			
sl@0
   261
			}
sl@0
   262
		else
sl@0
   263
			{
sl@0
   264
			Epoc::FreePhysicalRam(phys,size);
sl@0
   265
			NKern::ThreadLeaveCS();
sl@0
   266
			return r;
sl@0
   267
			}
sl@0
   268
sl@0
   269
		}
sl@0
   270
	NKern::ThreadLeaveCS();
sl@0
   271
	return r;
sl@0
   272
#endif
sl@0
   273
	}
sl@0
   274
sl@0
   275
sl@0
   276
TInt DIpcCpy::Request(TInt aFunc, TAny* a1, TAny* a2)
sl@0
   277
	{
sl@0
   278
	if (aFunc == RIpcCpy::EControlBigRead)
sl@0
   279
		{
sl@0
   280
		TUint size = (TUint)a2;
sl@0
   281
		if (size > (TUint)KBigBufferSize)
sl@0
   282
			return KErrOverflow;
sl@0
   283
		kumemput(a1, iBigBuffer, size);
sl@0
   284
		return KErrNone;
sl@0
   285
		}
sl@0
   286
	else if (aFunc == RIpcCpy::EControlBigWrite)
sl@0
   287
		{
sl@0
   288
		TUint size = (TUint)a2;
sl@0
   289
		if (size > (TUint)KBigBufferSize)
sl@0
   290
			return KErrOverflow;
sl@0
   291
		kumemget(iBigBuffer, a1, size);
sl@0
   292
		return KErrNone;
sl@0
   293
		}
sl@0
   294
	else if (aFunc == RIpcCpy::EControlHardwareChunks)
sl@0
   295
		{
sl@0
   296
		TPtr8 des(0,0,0);
sl@0
   297
		kumemget(&des,a2,sizeof(TPtr8));
sl@0
   298
		TInt r=CreateHardwareChunks(des);
sl@0
   299
		if(r==KErrNone)
sl@0
   300
			kumemput(a1, iHwChunkLinAddrs, sizeof(iHwChunkLinAddrs));
sl@0
   301
		return r;
sl@0
   302
		}
sl@0
   303
	return DLogicalChannel::Request(aFunc, a1, a2);
sl@0
   304
	}
sl@0
   305
sl@0
   306
void DIpcCpy::HandleMsg(TMessageBase* aMsg)
sl@0
   307
	{
sl@0
   308
	TInt r=KErrNone;
sl@0
   309
	TThreadMessage& m=*(TThreadMessage*)aMsg;
sl@0
   310
	TInt id=m.iValue;
sl@0
   311
	if (id==(TInt)ECloseMsg)
sl@0
   312
		{
sl@0
   313
		iTimer.Cancel();
sl@0
   314
		iDfc.Cancel();
sl@0
   315
		m.Complete(KErrNone,EFalse);
sl@0
   316
		iMsgQ.CompleteAll(KErrServerTerminated);
sl@0
   317
		return;
sl@0
   318
		}
sl@0
   319
	else if (id<0)
sl@0
   320
		{
sl@0
   321
		TRequestStatus* pS=(TRequestStatus*)m.Ptr0();
sl@0
   322
		if (iAsyncRequest->SetStatus(pS) != KErrNone)
sl@0
   323
			Kern::ThreadKill(iThread,EExitPanic,ERequestAlreadyPending,KDIpcCpyPanicCategory);
sl@0
   324
sl@0
   325
		if (id==~RIpcCpy::ERequestIpcCpy)
sl@0
   326
			{
sl@0
   327
			iDest=m.Ptr1();
sl@0
   328
			iTimer.OneShot(1);
sl@0
   329
			}
sl@0
   330
		else
sl@0
   331
			{
sl@0
   332
			r=KErrNotSupported;
sl@0
   333
			}
sl@0
   334
sl@0
   335
		if(r!=KErrNone)
sl@0
   336
			{
sl@0
   337
			Kern::QueueRequestComplete(iThread, iAsyncRequest, r);
sl@0
   338
			r = KErrNone;
sl@0
   339
			}
sl@0
   340
		}
sl@0
   341
	else
sl@0
   342
		{
sl@0
   343
		r=KErrNotSupported;
sl@0
   344
		}
sl@0
   345
sl@0
   346
	m.Complete(r,ETrue);
sl@0
   347
	}
sl@0
   348
sl@0
   349
void DIpcCpy::TimerExpired()
sl@0
   350
	{
sl@0
   351
	TInt src_offset=iSeqNum&3;
sl@0
   352
	TInt dest_offset=(iSeqNum>>2)&3;
sl@0
   353
	TInt length=(iSeqNum>>4)+1;
sl@0
   354
	TInt i;
sl@0
   355
	for (i=src_offset; i<length+src_offset; ++i)
sl@0
   356
		iBuffer[i]=(TUint8)(i+1);
sl@0
   357
	TPtrC8 ptr(iBuffer+src_offset, length);
sl@0
   358
	TInt r=Kern::ThreadDesWrite(iThread, iDest, ptr, dest_offset, KChunkShiftBy0, NULL);
sl@0
   359
	if (r==KErrNone)
sl@0
   360
		{
sl@0
   361
		r=iSeqNum;
sl@0
   362
		if (++iSeqNum==4096)
sl@0
   363
			iSeqNum=0;
sl@0
   364
		}
sl@0
   365
	Kern::QueueRequestComplete(iThread, iAsyncRequest, r);
sl@0
   366
	}
sl@0
   367