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