1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/system/d_atomic.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,214 @@
1.4 +// Copyright (c) 2008-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 +// e32test\system\d_atomic.cpp
1.18 +// LDD for testing atomic operations
1.19 +//
1.20 +//
1.21 +
1.22 +#include <kernel/kern_priv.h>
1.23 +#include "t_atomic.h"
1.24 +
1.25 +class DAtomicTest;
1.26 +struct TPerThreadK : public TPerThread
1.27 + {
1.28 + TAny* iOldExtraContext;
1.29 + const SSlowExecEntry* iOldSlowExecTable;
1.30 + DAtomicTest* iCh;
1.31 + TInt iId;
1.32 + };
1.33 +
1.34 +
1.35 +class DAtomicTestFactory : public DLogicalDevice
1.36 + {
1.37 +public:
1.38 + DAtomicTestFactory();
1.39 + ~DAtomicTestFactory();
1.40 + virtual TInt Install();
1.41 + virtual void GetCaps(TDes8& aDes) const;
1.42 + virtual TInt Create(DLogicalChannelBase*& aChannel);
1.43 +public:
1.44 + };
1.45 +
1.46 +
1.47 +class DAtomicTest : public DLogicalChannelBase
1.48 + {
1.49 +public:
1.50 + virtual ~DAtomicTest();
1.51 +protected:
1.52 + virtual TInt DoCreate(TInt aUnit, const TDesC8* anInfo, const TVersion& aVer);
1.53 + virtual TInt Request(TInt aFunction, TAny* a1, TAny* a2);
1.54 +private:
1.55 + void SetupExecTable();
1.56 + static TInt XGetThreadInfo(TAny* aDest);
1.57 + static TInt XSetThreadInfo(const TAny* aSrc);
1.58 + static TInt XAtomicAction(TAny* aPtr);
1.59 + static TInt XRestore();
1.60 +private:
1.61 + TUint64A iReg;
1.62 + TPerThreadK iPerThread[KMaxThreads];
1.63 + TUint32 iXT[19];
1.64 + };
1.65 +
1.66 +
1.67 +DAtomicTestFactory::DAtomicTestFactory()
1.68 + {
1.69 + }
1.70 +
1.71 +DAtomicTestFactory::~DAtomicTestFactory()
1.72 + {
1.73 + }
1.74 +TInt DAtomicTestFactory::Create(DLogicalChannelBase*& aChannel)
1.75 + {
1.76 + aChannel = new DAtomicTest;
1.77 + return KErrNone;
1.78 + }
1.79 +
1.80 +TInt DAtomicTestFactory::Install()
1.81 + {
1.82 + return SetName(&KAtomicTestLddName);
1.83 + }
1.84 +
1.85 +void DAtomicTestFactory::GetCaps(TDes8& aDes) const
1.86 + {
1.87 + // Not used but required as DLogicalDevice::GetCaps is pure virtual
1.88 + }
1.89 +
1.90 +DECLARE_STANDARD_LDD()
1.91 + {
1.92 + return new DAtomicTestFactory;
1.93 + }
1.94 +
1.95 +
1.96 +TInt DAtomicTest::DoCreate(TInt /*aUnit*/, const TDesC8* /*aInfo*/, const TVersion& /*aVer*/)
1.97 + {
1.98 + SetupExecTable();
1.99 + return KErrNone;
1.100 + }
1.101 +
1.102 +DAtomicTest::~DAtomicTest()
1.103 + {
1.104 + }
1.105 +
1.106 +void DAtomicTest::SetupExecTable()
1.107 + {
1.108 + NThread* t = NKern::CurrentThread();
1.109 + SSlowExecTable& sxt = *(SSlowExecTable*)iXT;
1.110 + const SSlowExecTable& orig_sxt = *_LOFF(t->iSlowExecTable, SSlowExecTable, iEntries);
1.111 + sxt.iSlowExecCount = 4; // get thread info, set thread info, atomic action, restore exec table
1.112 + sxt.iInvalidExecHandler = orig_sxt.iInvalidExecHandler;
1.113 + sxt.iPreprocessHandler = 0; // not used
1.114 + sxt.iEntries[0].iFlags = 0; // don't do anything special on entry or exit
1.115 + sxt.iEntries[0].iFunction = (TLinAddr)&XGetThreadInfo;
1.116 + sxt.iEntries[1].iFlags = 0; // don't do anything special on entry or exit
1.117 + sxt.iEntries[1].iFunction = (TLinAddr)&XSetThreadInfo;
1.118 + sxt.iEntries[2].iFlags = 0; // don't do anything special on entry or exit
1.119 + sxt.iEntries[2].iFunction = (TLinAddr)&XAtomicAction;
1.120 + sxt.iEntries[3].iFlags = 0; // don't do anything special on entry or exit
1.121 + sxt.iEntries[3].iFunction = (TLinAddr)&XRestore;
1.122 + }
1.123 +
1.124 +TInt DAtomicTest::Request(TInt aFunction, TAny* a1, TAny* a2)
1.125 + {
1.126 + TInt r = KErrNotSupported;
1.127 + switch (aFunction)
1.128 + {
1.129 + case RTestAtomic::ETDGExecuteK:
1.130 + {
1.131 + TDGBase tdg;
1.132 + kumemget32(&tdg, a1, sizeof(tdg));
1.133 + r = tdg.Execute();
1.134 + kumemput32(a1, &tdg, sizeof(tdg));
1.135 + break;
1.136 + }
1.137 + case RTestAtomic::EInitialise:
1.138 + {
1.139 + kumemget32(&iReg, a1, sizeof(TUint64));
1.140 + break;
1.141 + }
1.142 + case RTestAtomic::ERetrieve:
1.143 + {
1.144 + kumemput32(a1, &iReg, sizeof(TUint64));
1.145 + break;
1.146 + }
1.147 + case RTestAtomic::ESetCurrentThreadTimeslice:
1.148 + {
1.149 + NKern::ThreadSetTimeslice(NKern::CurrentThread(), NKern::TimesliceTicks((TInt)a1));
1.150 + r = KErrNone;
1.151 + break;
1.152 + }
1.153 + case RTestAtomic::ESwitchExecTables:
1.154 + {
1.155 + TUint tid = (TUint)a1;
1.156 + NThread* nt = NKern::CurrentThread();
1.157 + DThread& t = Kern::CurrentThread();
1.158 + if (Kern::NThreadToDThread(nt)==&t && tid<TUint(KMaxThreads))
1.159 + {
1.160 + TPerThreadK* p = iPerThread + tid;
1.161 + p->iOldExtraContext = nt->iExtraContext;
1.162 + p->iOldSlowExecTable = nt->iSlowExecTable;
1.163 + p->iId = tid;
1.164 + p->iCh = this;
1.165 + nt->iExtraContext = p;
1.166 + SSlowExecTable& sxt = *(SSlowExecTable*)iXT;
1.167 + nt->iSlowExecTable = sxt.iEntries;
1.168 + r = KErrNone;
1.169 + }
1.170 + break;
1.171 + }
1.172 + case RTestAtomic::EGetKernelMemoryAddress:
1.173 + {
1.174 + r = (TInt)NKern::CurrentThread(); // if we trash this the test should go bang
1.175 + break;
1.176 + }
1.177 + default:
1.178 + break;
1.179 + }
1.180 + return r;
1.181 + }
1.182 +
1.183 +TInt DAtomicTest::XGetThreadInfo(TAny* aDest)
1.184 + {
1.185 + NThread* t = NKern::CurrentThread();
1.186 + TPerThreadK* p = (TPerThreadK*)t->iExtraContext;
1.187 + kumemput32(aDest, p, sizeof(TPerThread));
1.188 + return KErrNone;
1.189 + }
1.190 +
1.191 +TInt DAtomicTest::XSetThreadInfo(const TAny* aSrc)
1.192 + {
1.193 + NThread* t = NKern::CurrentThread();
1.194 + TPerThreadK* p = (TPerThreadK*)t->iExtraContext;
1.195 + kumemget32(p, aSrc, sizeof(TPerThread));
1.196 + return KErrNone;
1.197 + }
1.198 +
1.199 +TInt DAtomicTest::XAtomicAction(TAny* aPtr)
1.200 + {
1.201 + NThread* t = NKern::CurrentThread();
1.202 + TPerThreadK* p = (TPerThreadK*)t->iExtraContext;
1.203 + TAtomicAction action;
1.204 + kumemget32(&action, aPtr, sizeof(TAtomicAction));
1.205 + if (action.iThread!=p->iId || TUint(action.iIndex)>=TUint(TOTAL_INDEXES))
1.206 + return KErrNotSupported;
1.207 + return DoAtomicAction(&p->iCh->iReg, p, action);
1.208 + }
1.209 +
1.210 +TInt DAtomicTest::XRestore()
1.211 + {
1.212 + NThread* t = NKern::CurrentThread();
1.213 + TPerThreadK* p = (TPerThreadK*)t->iExtraContext;
1.214 + t->iExtraContext = p->iOldExtraContext;
1.215 + t->iSlowExecTable = p->iOldSlowExecTable;
1.216 + return KErrNone;
1.217 + }