1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/dll/d_ldrtst.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,276 @@
1.4 +// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of the License "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// f32test\loader\d_ldrtst.cpp
1.18 +// LDD for testing loader
1.19 +//
1.20 +//
1.21 +
1.22 +#include <kernel/kern_priv.h>
1.23 +#include "d_ldrtst.h"
1.24 +
1.25 +const TInt KMajorVersionNumber=0;
1.26 +const TInt KMinorVersionNumber=1;
1.27 +const TInt KBuildVersionNumber=1;
1.28 +
1.29 +class DLdrTest;
1.30 +class DLdrTestFactory : public DLogicalDevice
1.31 +//
1.32 +// Test LDD factory
1.33 +//
1.34 + {
1.35 +public:
1.36 + DLdrTestFactory();
1.37 + virtual TInt Install();
1.38 + virtual void GetCaps(TDes8& aDes) const;
1.39 + virtual TInt Create(DLogicalChannelBase*& aChannel);
1.40 + };
1.41 +
1.42 +class DLdrTest : public DLogicalChannelBase
1.43 +//
1.44 +// Test logical channel
1.45 +//
1.46 + {
1.47 +public:
1.48 + virtual ~DLdrTest();
1.49 +protected:
1.50 + virtual TInt DoCreate(TInt aUnit, const TDesC8* anInfo, const TVersion& aVer);
1.51 + virtual TInt Request(TInt aFunction, TAny* a1, TAny* a2);
1.52 +public:
1.53 + TInt GetCodeSegInfo(TAny* aHandle, TAny* aDest);
1.54 + TAny* ModuleCodeSeg(TModuleHandle aModuleHandle);
1.55 + TAny* ProcessCodeSeg(TInt aProcessHandle);
1.56 + TAny* LibraryCodeSeg(TInt aLibraryHandle);
1.57 + TInt GetCodeSegList(RLdrTest::SEntry* aList, TInt aMax);
1.58 + TAny* CodeSegFromAddr(TLinAddr aAddr);
1.59 + TModuleHandle ModuleHandleFromAddr(TLinAddr aAddr);
1.60 + TInt ProcessSMPUnsafeCount(TInt aProcessHandle);
1.61 +private:
1.62 + SDblQueLink* FindCodeSegQueueAnchor();
1.63 + };
1.64 +
1.65 +DECLARE_STANDARD_LDD()
1.66 + {
1.67 + return new DLdrTestFactory;
1.68 + }
1.69 +
1.70 +DLdrTestFactory::DLdrTestFactory()
1.71 +//
1.72 +// Constructor
1.73 +//
1.74 + {
1.75 + iVersion=TVersion(KMajorVersionNumber,KMinorVersionNumber,KBuildVersionNumber);
1.76 + }
1.77 +
1.78 +TInt DLdrTestFactory::Create(DLogicalChannelBase*& aChannel)
1.79 +//
1.80 +// Create a new DLdrTest on this logical device
1.81 +//
1.82 + {
1.83 + aChannel=new DLdrTest;
1.84 + return aChannel?KErrNone:KErrNoMemory;
1.85 + }
1.86 +
1.87 +TInt DLdrTestFactory::Install()
1.88 +//
1.89 +// Install the LDD - overriding pure virtual
1.90 +//
1.91 + {
1.92 + return SetName(&KLdrTestLddName);
1.93 + }
1.94 +
1.95 +void DLdrTestFactory::GetCaps(TDes8& aDes) const
1.96 +//
1.97 +// Get capabilities - overriding pure virtual
1.98 +//
1.99 + {
1.100 + TCapsTestV01 b;
1.101 + b.iVersion=TVersion(KMajorVersionNumber,KMinorVersionNumber,KBuildVersionNumber);
1.102 + Kern::InfoCopy(aDes,(TUint8*)&b,sizeof(b));
1.103 + }
1.104 +
1.105 +TInt DLdrTest::DoCreate(TInt /*aUnit*/, const TDesC8* /*anInfo*/, const TVersion& aVer)
1.106 +//
1.107 +// Create channel
1.108 +//
1.109 + {
1.110 +
1.111 + if (!Kern::QueryVersionSupported(TVersion(KMajorVersionNumber,KMinorVersionNumber,KBuildVersionNumber),aVer))
1.112 + return KErrNotSupported;
1.113 + return KErrNone;
1.114 + }
1.115 +
1.116 +DLdrTest::~DLdrTest()
1.117 +//
1.118 +// Destructor
1.119 +//
1.120 + {
1.121 + }
1.122 +
1.123 +TInt DLdrTest::GetCodeSegInfo(TAny* aHandle, TAny* aDest)
1.124 + {
1.125 + TCodeSegCreateInfo info;
1.126 + DCodeSeg* pS=DCodeSeg::VerifyHandle(aHandle);
1.127 + if (pS)
1.128 + {
1.129 + Kern::AccessCode();
1.130 + pS->Info(info);
1.131 + Kern::EndAccessCode();
1.132 + kumemput32(aDest, &info, sizeof(info));
1.133 + return KErrNone;
1.134 + }
1.135 + return KErrArgument;
1.136 + }
1.137 +
1.138 +TAny* DLdrTest::ModuleCodeSeg(TModuleHandle aModuleHandle)
1.139 + {
1.140 + DCodeSeg* pS=DCodeSeg::VerifyHandle(aModuleHandle);
1.141 + if(!pS)
1.142 + {
1.143 + Kern::AccessCode();
1.144 + DCodeSeg::CodeSegFromEntryPoint((TLinAddr)aModuleHandle); // ignore returned DCodeSeg*
1.145 + Kern::EndAccessCode();
1.146 + }
1.147 + return pS;
1.148 + }
1.149 +
1.150 +TAny* DLdrTest::CodeSegFromAddr(TLinAddr aAddr)
1.151 + {
1.152 + Kern::AccessCode();
1.153 + DCodeSeg* s = Kern::CodeSegFromAddress(aAddr, Kern::CurrentThread().iOwningProcess);
1.154 + Kern::EndAccessCode();
1.155 + return s;
1.156 + }
1.157 +
1.158 +TModuleHandle DLdrTest::ModuleHandleFromAddr(TLinAddr aAddr)
1.159 + {
1.160 + TModuleHandle h = (TModuleHandle)CodeSegFromAddr(aAddr);
1.161 + if(!h)
1.162 + h = (TModuleHandle)aAddr;
1.163 + return h;
1.164 + }
1.165 +
1.166 +TAny* DLdrTest::ProcessCodeSeg(TInt aProcessHandle)
1.167 + {
1.168 + DCodeSeg* pS=NULL;
1.169 + DThread& t=Kern::CurrentThread();
1.170 + NKern::LockSystem();
1.171 + DProcess* pP=(DProcess*)t.ObjectFromHandle(aProcessHandle, EProcess);
1.172 + if (pP)
1.173 + {
1.174 + pS=pP->iCodeSeg;
1.175 + if (!pS)
1.176 + pS=pP->iTempCodeSeg;
1.177 + }
1.178 + NKern::UnlockSystem();
1.179 + return pS;
1.180 + }
1.181 +
1.182 +TAny* DLdrTest::LibraryCodeSeg(TInt aLibraryHandle)
1.183 + {
1.184 + DCodeSeg* pS=NULL;
1.185 + DThread& t=Kern::CurrentThread();
1.186 + NKern::LockSystem();
1.187 + DLibrary* pL=(DLibrary*)t.ObjectFromHandle(aLibraryHandle, ELibrary);
1.188 + if (pL)
1.189 + pS=pL->iCodeSeg;
1.190 + NKern::UnlockSystem();
1.191 + return pS;
1.192 + }
1.193 +
1.194 +SDblQueLink* DLdrTest::FindCodeSegQueueAnchor()
1.195 + {
1.196 + SDblQueLink* p=&iDevice->iCodeSeg->iLink; // this device driver's code segment
1.197 + for (;;)
1.198 + {
1.199 + p=p->iPrev;
1.200 + DCodeSeg* s=_LOFF(p, DCodeSeg, iLink);
1.201 + if (s->iExeCodeSeg==s && (s->iAttr & ECodeSegAttKernel))
1.202 + {
1.203 + // s is the kernel's code segment, which is the first one to be created
1.204 + return s->iLink.iPrev;
1.205 + }
1.206 + }
1.207 + }
1.208 +
1.209 +TInt DLdrTest::GetCodeSegList(RLdrTest::SEntry* aList, TInt aMax)
1.210 + {
1.211 + if (aMax<=0)
1.212 + return KErrArgument;
1.213 + RLdrTest::SEntry list[128];
1.214 + Kern::AccessCode();
1.215 + SDblQueLink* anchor=FindCodeSegQueueAnchor();
1.216 + SDblQueLink* p=anchor->iNext;
1.217 + if (aMax>128)
1.218 + aMax=128;
1.219 + TInt n=0;
1.220 + for(; p!=anchor && n<aMax; p=p->iNext, ++n)
1.221 + {
1.222 + DCodeSeg* s=_LOFF(p, DCodeSeg, iLink);
1.223 + list[n].iHandle=s;
1.224 + list[n].iUid3=(TUint32)s->iUids.iUid[2].iUid;
1.225 + }
1.226 + Kern::EndAccessCode();
1.227 + if (n>0)
1.228 + kumemput32(aList, list, n*sizeof(RLdrTest::SEntry));
1.229 + return n;
1.230 + }
1.231 +
1.232 +TInt DLdrTest::ProcessSMPUnsafeCount(TInt aProcessHandle)
1.233 + {
1.234 + TInt count=KErrNotFound;
1.235 + DThread& t=Kern::CurrentThread();
1.236 + NKern::LockSystem();
1.237 + DProcess* pP=(DProcess*)t.ObjectFromHandle(aProcessHandle, EProcess);
1.238 + if (pP)
1.239 + count=pP->iSMPUnsafeCount;
1.240 + NKern::UnlockSystem();
1.241 + return count;
1.242 + }
1.243 +
1.244 +TInt DLdrTest::Request(TInt aFunction, TAny* a1, TAny* a2)
1.245 + {
1.246 + TInt r=KErrNone;
1.247 + switch (aFunction)
1.248 + {
1.249 + case RLdrTest::EControlGetCodeSegInfo:
1.250 + r=GetCodeSegInfo(a1,a2);
1.251 + break;
1.252 + case RLdrTest::EControlProcessCodeSeg:
1.253 + r=(TInt)ProcessCodeSeg((TInt)a1);
1.254 + break;
1.255 + case RLdrTest::EControlLibraryCodeSeg:
1.256 + r=(TInt)LibraryCodeSeg((TInt)a1);
1.257 + break;
1.258 + case RLdrTest::EControlModuleCodeSeg:
1.259 + r=(TInt)ModuleCodeSeg((TModuleHandle)a1);
1.260 + break;
1.261 + case RLdrTest::EControlGetCodeSegList:
1.262 + r=GetCodeSegList( (RLdrTest::SEntry*)a1, (TInt)a2 );
1.263 + break;
1.264 + case RLdrTest::EControlCodeSegFromAddr:
1.265 + r=(TInt)CodeSegFromAddr((TLinAddr)a1);
1.266 + break;
1.267 + case RLdrTest::EControlModuleHandleFromAddr:
1.268 + r=(TInt)ModuleHandleFromAddr((TLinAddr)a1);
1.269 + break;
1.270 + case RLdrTest::EControlProcessSMPUnsafeCount:
1.271 + r=ProcessSMPUnsafeCount((TInt)a1);
1.272 + break;
1.273 + default:
1.274 + r=KErrNotSupported;
1.275 + break;
1.276 + }
1.277 + return r;
1.278 + }
1.279 +