os/kernelhwsrv/kerneltest/e32test/debug/d_schedhook.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 2002-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\debug\d_schedhook.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include <kernel/kernel.h>
sl@0
    19
#include <kernel/kern_priv.h>
sl@0
    20
#include "platform.h"
sl@0
    21
#include <kernel/cache.h>
sl@0
    22
#include <arm.h>
sl@0
    23
#include "d_schedhook.h"
sl@0
    24
sl@0
    25
_LIT(KLddName,"D_SCHEDHOOK");
sl@0
    26
sl@0
    27
NThread* TestThread;
sl@0
    28
TInt TestCount;
sl@0
    29
sl@0
    30
const TInt KMajorVersionNumber = 0;
sl@0
    31
const TInt KMinorVersionNumber = 1;
sl@0
    32
const TInt KBuildVersionNumber = 1;
sl@0
    33
sl@0
    34
class DSchedhookTest;
sl@0
    35
sl@0
    36
class DSchedhookTestFactory : public DLogicalDevice
sl@0
    37
	{
sl@0
    38
public:
sl@0
    39
	DSchedhookTestFactory();
sl@0
    40
	virtual TInt Install();
sl@0
    41
	virtual void GetCaps(TDes8& aDes) const;
sl@0
    42
	virtual TInt Create(DLogicalChannelBase*& aChannel);
sl@0
    43
	};
sl@0
    44
sl@0
    45
class DHwExcHandler : public DKernelEventHandler
sl@0
    46
	{
sl@0
    47
public:
sl@0
    48
	DHwExcHandler() : DKernelEventHandler(HandleEvent, this) {}
sl@0
    49
private:
sl@0
    50
	static TUint HandleEvent(TKernelEvent aEvent, TAny* a1, TAny* a2, TAny* aThis);
sl@0
    51
	};
sl@0
    52
sl@0
    53
class DSchedhookTest : public DLogicalChannelBase
sl@0
    54
	{
sl@0
    55
public:
sl@0
    56
	virtual ~DSchedhookTest();
sl@0
    57
protected:
sl@0
    58
	virtual TInt DoCreate(TInt aUnit, const TDesC8* anInfo, const TVersion& aVer);
sl@0
    59
	virtual TInt Request(TInt aFunction, TAny* a1, TAny* a2);
sl@0
    60
public:
sl@0
    61
	DHwExcHandler* iHwExcHandler;
sl@0
    62
	};
sl@0
    63
sl@0
    64
DECLARE_STANDARD_LDD()
sl@0
    65
	{
sl@0
    66
	return new DSchedhookTestFactory;
sl@0
    67
	}
sl@0
    68
sl@0
    69
//
sl@0
    70
// DSchedhookTestFactory
sl@0
    71
//
sl@0
    72
sl@0
    73
DSchedhookTestFactory::DSchedhookTestFactory()
sl@0
    74
	{
sl@0
    75
	iVersion=TVersion(KMajorVersionNumber,KMinorVersionNumber,KBuildVersionNumber);
sl@0
    76
	}
sl@0
    77
sl@0
    78
TInt DSchedhookTestFactory::Create(DLogicalChannelBase*& aChannel)
sl@0
    79
/**
sl@0
    80
 Create a new DSchedhookTest on this logical device
sl@0
    81
*/
sl@0
    82
	{
sl@0
    83
	aChannel=new DSchedhookTest;
sl@0
    84
	return aChannel ? KErrNone : KErrNoMemory;
sl@0
    85
	}
sl@0
    86
sl@0
    87
TInt DSchedhookTestFactory::Install()
sl@0
    88
/**
sl@0
    89
 Install the LDD - overriding pure virtual
sl@0
    90
*/
sl@0
    91
	{
sl@0
    92
	return SetName(&KLddName);
sl@0
    93
	}
sl@0
    94
sl@0
    95
void DSchedhookTestFactory::GetCaps(TDes8& aDes) const
sl@0
    96
/**
sl@0
    97
 Get capabilities - overriding pure virtual
sl@0
    98
*/
sl@0
    99
	{
sl@0
   100
	TCapsTestV01 b;
sl@0
   101
	b.iVersion=TVersion(KMajorVersionNumber,KMinorVersionNumber,KBuildVersionNumber);
sl@0
   102
    Kern::InfoCopy(aDes,(TUint8*)&b,sizeof(b));
sl@0
   103
	}
sl@0
   104
sl@0
   105
//
sl@0
   106
// DHwExcHandler
sl@0
   107
//
sl@0
   108
sl@0
   109
/**
sl@0
   110
 Handle exceptions on our test thread by suspending it
sl@0
   111
*/
sl@0
   112
TUint DHwExcHandler::HandleEvent(TKernelEvent aEvent, TAny* a1, TAny* /*a2*/, TAny* /*aThis*/)
sl@0
   113
	{
sl@0
   114
	if (aEvent == EEventHwExc)
sl@0
   115
		{
sl@0
   116
		if(&Kern::CurrentThread().iNThread!=TestThread)
sl@0
   117
			return ERunNext;
sl@0
   118
		TArmExcInfo *pR=(TArmExcInfo*)a1;
sl@0
   119
		pR->iR15+=4;
sl@0
   120
		Kern::ThreadSuspend(Kern::CurrentThread(),1);
sl@0
   121
		return (TUint)EExcHandled;
sl@0
   122
		}
sl@0
   123
	return (TUint)ERunNext;
sl@0
   124
	}
sl@0
   125
sl@0
   126
//
sl@0
   127
// DSchedhookTest
sl@0
   128
//
sl@0
   129
sl@0
   130
TInt DSchedhookTest::DoCreate(TInt /*aUnit*/, const TDesC8* /*aInfo*/, const TVersion& aVer)
sl@0
   131
/**
sl@0
   132
 Create channel
sl@0
   133
*/
sl@0
   134
	{
sl@0
   135
	if (!Kern::QueryVersionSupported(TVersion(KMajorVersionNumber,KMinorVersionNumber,KBuildVersionNumber),aVer))
sl@0
   136
		return KErrNotSupported;
sl@0
   137
	iHwExcHandler = new DHwExcHandler;
sl@0
   138
	if (! iHwExcHandler)
sl@0
   139
		return KErrNoMemory;
sl@0
   140
	return iHwExcHandler->Add();
sl@0
   141
	}
sl@0
   142
sl@0
   143
DSchedhookTest::~DSchedhookTest()
sl@0
   144
	{
sl@0
   145
	if (iHwExcHandler)
sl@0
   146
		iHwExcHandler->Close();
sl@0
   147
	}
sl@0
   148
sl@0
   149
sl@0
   150
sl@0
   151
//
sl@0
   152
// Scheduler Hook functions
sl@0
   153
//
sl@0
   154
sl@0
   155
NThread* CurrentThread=NULL;
sl@0
   156
sl@0
   157
void DisableRescheduleCallback()
sl@0
   158
/**
sl@0
   159
 Stops the Scheduler calling us on every context switch
sl@0
   160
*/
sl@0
   161
	{
sl@0
   162
	// Prevent rescheduling whilst we disable the callback
sl@0
   163
	NKern::Lock();
sl@0
   164
	// Disable Callback
sl@0
   165
	NKern::SetRescheduleCallback(NULL);
sl@0
   166
	// Invalidate CurrentThread
sl@0
   167
	CurrentThread = NULL;
sl@0
   168
	// Callback now disabled...
sl@0
   169
	NKern::Unlock();
sl@0
   170
	}
sl@0
   171
sl@0
   172
sl@0
   173
sl@0
   174
void RemoveSchedulerHooks()
sl@0
   175
/**
sl@0
   176
 Removes the patches added to the Scheduler code.
sl@0
   177
*/
sl@0
   178
	{
sl@0
   179
	// Make sure callback is disabled (required before removing hooks)
sl@0
   180
	DisableRescheduleCallback();
sl@0
   181
	// Get range of memory used by hooks
sl@0
   182
	TLinAddr start,end;
sl@0
   183
	NKern::Lock();
sl@0
   184
	NKern::SchedulerHooks(start,end);
sl@0
   185
	NKern::Unlock();
sl@0
   186
	// Free shadow pages which cover hooks
sl@0
   187
	TUint32 pageSize=Kern::RoundToPageSize(1);
sl@0
   188
	NKern::ThreadEnterCS();
sl@0
   189
	for(TLinAddr a=start; a<end; a+=pageSize)
sl@0
   190
		Epoc::FreeShadowPage(a);   // Ignore errors because we're trying to clean up anyway
sl@0
   191
	NKern::ThreadLeaveCS();
sl@0
   192
	}
sl@0
   193
sl@0
   194
sl@0
   195
sl@0
   196
TInt InsertSchedulerHooks()
sl@0
   197
/**
sl@0
   198
 Enables use of the Scheduler callback by using shadow pages to patch the Scheduler code.
sl@0
   199
*/
sl@0
   200
	{
sl@0
   201
	// Get range of memory used by hooks	
sl@0
   202
	TLinAddr start=0,end=0;
sl@0
   203
	NKern::Lock();
sl@0
   204
	NKern::SchedulerHooks(start,end);
sl@0
   205
	NKern::Unlock();
sl@0
   206
	if (start==0 && end==0) return KErrNotSupported;
sl@0
   207
sl@0
   208
	// Create shadow pages for hooks
sl@0
   209
	TUint32 pageSize=Kern::RoundToPageSize(1);
sl@0
   210
	for(TLinAddr a=start; a<end; a+=pageSize)
sl@0
   211
		{
sl@0
   212
		NKern::ThreadEnterCS();
sl@0
   213
		TInt r=Epoc::AllocShadowPage(a);
sl@0
   214
		NKern::ThreadLeaveCS();
sl@0
   215
		if(r!=KErrNone && r!=KErrAlreadyExists)
sl@0
   216
			{
sl@0
   217
			RemoveSchedulerHooks();
sl@0
   218
			return r;
sl@0
   219
			}
sl@0
   220
		}
sl@0
   221
	// Put hooks in
sl@0
   222
	NKern::Lock();
sl@0
   223
	NKern::InsertSchedulerHooks();
sl@0
   224
	NKern::Unlock();
sl@0
   225
	// Make I and D caches consistant for hook region
sl@0
   226
	Cache::IMB_Range(start,end-start);
sl@0
   227
sl@0
   228
	return KErrNone;
sl@0
   229
	}
sl@0
   230
sl@0
   231
sl@0
   232
sl@0
   233
void RescheduleTestFunction()
sl@0
   234
/**
sl@0
   235
 Test function to be called on each thread reschedule
sl@0
   236
*/
sl@0
   237
	{
sl@0
   238
	// Count rechedules to the test thread
sl@0
   239
	if(CurrentThread==TestThread)
sl@0
   240
		++TestCount;
sl@0
   241
	}
sl@0
   242
sl@0
   243
sl@0
   244
sl@0
   245
void RescheduleCallback(NThread* aNThread)
sl@0
   246
/**
sl@0
   247
 Main scheduler callback.
sl@0
   248
 Called with the Kernel Lock (Preemption Lock) held.
sl@0
   249
*/
sl@0
   250
	{
sl@0
   251
#ifndef __SMP__
sl@0
   252
	// The 'CurrentThread' is now unscheduled and has become the 'previous thread'
sl@0
   253
	// Set this thread 'UserContextType'...
sl@0
   254
	CurrentThread->SetUserContextType();
sl@0
   255
#endif
sl@0
   256
	// Make the newly scheduled thread the CurrentThread
sl@0
   257
	CurrentThread = aNThread;
sl@0
   258
	// Test function
sl@0
   259
	RescheduleTestFunction();
sl@0
   260
	}
sl@0
   261
sl@0
   262
sl@0
   263
sl@0
   264
void RescheduleCallbackFirst(NThread* aNThread)
sl@0
   265
/**
sl@0
   266
 Scheduler callback used once for initialisation.
sl@0
   267
 Called with the Kernel Lock (Preemption Lock) held.
sl@0
   268
*/
sl@0
   269
	{
sl@0
   270
	// Initialise CurrentThread
sl@0
   271
	CurrentThread = aNThread;
sl@0
   272
	// Switch future callbacks to the main RescheduleCallback
sl@0
   273
	NKern::SetRescheduleCallback(RescheduleCallback);
sl@0
   274
	// Test function
sl@0
   275
	RescheduleTestFunction();
sl@0
   276
	}
sl@0
   277
sl@0
   278
sl@0
   279
sl@0
   280
void EnableRescheduleCallback()
sl@0
   281
/**
sl@0
   282
 Sets the Scheduler to call us back on every thread reschedule
sl@0
   283
*/
sl@0
   284
	{
sl@0
   285
	// Reset the User Context Type for all threads, because these values
sl@0
   286
	// will be out-of-date. (It is our Rescheduler callback which set's them.
sl@0
   287
	// and we're just about enable that.)
sl@0
   288
sl@0
   289
	NKern::ThreadEnterCS();  // Prevent us from dying or suspending whilst holding a DMutex
sl@0
   290
	DObjectCon& threads=*Kern::Containers()[EThread];  // Get containing holding threads
sl@0
   291
	threads.Wait();  // Obtain the container mutex so the list does get changed under us
sl@0
   292
sl@0
   293
#ifndef __SMP__
sl@0
   294
	// For each thread...
sl@0
   295
	TInt c=threads.Count();
sl@0
   296
	for (TInt i=0; i<c; i++)
sl@0
   297
		((DThread*)threads[i])->iNThread.ResetUserContextType();
sl@0
   298
#endif
sl@0
   299
sl@0
   300
	threads.Signal();  // Release the container mutex
sl@0
   301
	NKern::ThreadLeaveCS();  // End of critical section
sl@0
   302
sl@0
   303
	// Ask for callback
sl@0
   304
	NKern::SetRescheduleCallback(RescheduleCallbackFirst);
sl@0
   305
	}
sl@0
   306
sl@0
   307
sl@0
   308
sl@0
   309
TInt GetThreadUserContext(NThread* aThread,TArmRegSet& aContext)
sl@0
   310
/**
sl@0
   311
 Test function to get a threads User Context
sl@0
   312
*/
sl@0
   313
	{
sl@0
   314
	// Get the User Context Type which our Reschedule hook set
sl@0
   315
	TInt type = aThread->iSpare3; /*iUserContextType*/
sl@0
   316
sl@0
   317
	// Get the table corresponding to the User Context Type
sl@0
   318
	const TArmContextElement* c = NThread::UserContextTables()[type];
sl@0
   319
sl@0
   320
	// Set the User Context by using the table we got
sl@0
   321
	TUint32* sp  = (TUint32*)aThread->iSavedSP;
sl@0
   322
	TUint32* st  = (TUint32*)((TUint32)aThread->iStackBase+(TUint32)aThread->iStackSize);
sl@0
   323
	TArmReg* out = (TArmReg*)(&aContext);
sl@0
   324
	TArmReg* end = (TArmReg*)(&aContext+1);
sl@0
   325
	do
sl@0
   326
		{
sl@0
   327
		TInt v = c->iValue;
sl@0
   328
		TInt t = c->iType;
sl@0
   329
		++c;
sl@0
   330
		if(t==TArmContextElement::EOffsetFromSp)
sl@0
   331
			v = sp[v];
sl@0
   332
		else if(t==TArmContextElement::EOffsetFromStackTop)
sl@0
   333
			v = st[-v];
sl@0
   334
		*out++ = v;
sl@0
   335
		}
sl@0
   336
	while(out<end);
sl@0
   337
sl@0
   338
	return type;
sl@0
   339
	}
sl@0
   340
sl@0
   341
sl@0
   342
sl@0
   343
void DumpContext(TArmRegSet& aContext,TInt aType)
sl@0
   344
	{
sl@0
   345
	Kern::Printf("  Context type %d",aType);
sl@0
   346
	Kern::Printf("  r0 =%08x r1 =%08x r2 =%08x r3 =%08x",aContext.iR0,aContext.iR1,aContext.iR2,aContext.iR3);
sl@0
   347
	Kern::Printf("  r4 =%08x r5 =%08x r6 =%08x r7 =%08x",aContext.iR4,aContext.iR5,aContext.iR6,aContext.iR7);
sl@0
   348
	Kern::Printf("  r8 =%08x r9 =%08x r10=%08x r11=%08x",aContext.iR8,aContext.iR9,aContext.iR10,aContext.iR11);
sl@0
   349
	Kern::Printf("  r12=%08x r13=%08x r14=%08x r15=%08x",aContext.iR12,aContext.iR13,aContext.iR14,aContext.iR15);
sl@0
   350
	Kern::Printf("  cpsr=%08x dacr=%08x",aContext.iFlags, aContext.iDacr);
sl@0
   351
	}
sl@0
   352
sl@0
   353
sl@0
   354
sl@0
   355
TInt TestGetThreadContext(DThread* aThread,TDes8* aContext)
sl@0
   356
	{
sl@0
   357
	TArmRegSet context1;
sl@0
   358
	TArmRegSet context2;
sl@0
   359
sl@0
   360
	memclr(&context1,sizeof(context1));
sl@0
   361
	memclr(&context2,sizeof(context2));
sl@0
   362
	NKern::Lock();
sl@0
   363
	TUint32 unused;
sl@0
   364
	NKern::ThreadGetUserContext(&aThread->iNThread,&context1,unused);
sl@0
   365
	TInt r=GetThreadUserContext(&aThread->iNThread,context2);
sl@0
   366
	NKern::Unlock();
sl@0
   367
	DumpContext(context1,-1);
sl@0
   368
	DumpContext(context2,r);
sl@0
   369
sl@0
   370
	TInt len,maxLen;
sl@0
   371
	Kern::KUDesInfo(*aContext,len,maxLen);
sl@0
   372
	if(maxLen>KMaxThreadContext)
sl@0
   373
		maxLen = KMaxThreadContext;
sl@0
   374
	TPtr8 ptr((TUint8*)&context1,maxLen,maxLen);
sl@0
   375
	Kern::KUDesPut(*aContext,ptr);
sl@0
   376
sl@0
   377
	for(TUint i=0; i<sizeof(context1); i++)
sl@0
   378
		if(((TUint8*)&context1)[i]!=((TUint8*)&context2)[i])
sl@0
   379
			return KErrGeneral;
sl@0
   380
	return r;
sl@0
   381
	}
sl@0
   382
sl@0
   383
sl@0
   384
DThread* ThreadFromId(TUint aId)
sl@0
   385
	{
sl@0
   386
	// This is risky because the thread could die on us an the DThread* become invalid.
sl@0
   387
	// We are relying on this never happening in our test code.
sl@0
   388
	NKern::ThreadEnterCS();  // Prevent us from dying or suspending whilst holding a DMutex
sl@0
   389
	DObjectCon& threads=*Kern::Containers()[EThread];  // Get containing holding threads
sl@0
   390
	threads.Wait();  // Obtain the container mutex so the list does get changed under us
sl@0
   391
	DThread* thread = Kern::ThreadFromId(aId);
sl@0
   392
	threads.Signal();  // Release the container mutex
sl@0
   393
	NKern::ThreadLeaveCS();  // End of critical section
sl@0
   394
	return thread;
sl@0
   395
	}
sl@0
   396
sl@0
   397
TInt DSchedhookTest::Request(TInt aFunction, TAny* a1, TAny* a2)
sl@0
   398
/**
sl@0
   399
 Handle requests from the test program
sl@0
   400
*/
sl@0
   401
	{
sl@0
   402
	TInt r=KErrNone;
sl@0
   403
	switch (aFunction)
sl@0
   404
		{
sl@0
   405
		case RSchedhookTest::EInstall:
sl@0
   406
			r=InsertSchedulerHooks();
sl@0
   407
			break;
sl@0
   408
sl@0
   409
		case RSchedhookTest::EUninstall:
sl@0
   410
			RemoveSchedulerHooks();
sl@0
   411
			return KErrNone;
sl@0
   412
sl@0
   413
		case RSchedhookTest::EInsertHooks:
sl@0
   414
			{
sl@0
   415
			NKern::Lock();
sl@0
   416
			NKern::InsertSchedulerHooks();
sl@0
   417
			TLinAddr start,end;
sl@0
   418
			NKern::SchedulerHooks(start,end);
sl@0
   419
			NKern::Unlock();
sl@0
   420
			Cache::IMB_Range(start,end-start);
sl@0
   421
			}
sl@0
   422
			return KErrNone;
sl@0
   423
sl@0
   424
		case RSchedhookTest::ERemoveHooks:
sl@0
   425
			{
sl@0
   426
			NKern::Lock();
sl@0
   427
			NKern::SetRescheduleCallback(NULL);
sl@0
   428
			NKern::RemoveSchedulerHooks();
sl@0
   429
			TLinAddr start,end;
sl@0
   430
			NKern::SchedulerHooks(start,end);
sl@0
   431
			NKern::Unlock();
sl@0
   432
			Cache::IMB_Range(start,end-start);
sl@0
   433
			}
sl@0
   434
			return KErrNone;
sl@0
   435
sl@0
   436
		case RSchedhookTest::EEnableCallback:
sl@0
   437
			NKern::Lock();
sl@0
   438
			NKern::SetRescheduleCallback(RescheduleCallbackFirst);
sl@0
   439
			NKern::Unlock();
sl@0
   440
			return KErrNone;
sl@0
   441
sl@0
   442
		case RSchedhookTest::EDisableCallback:
sl@0
   443
			DisableRescheduleCallback();
sl@0
   444
			return KErrNone;
sl@0
   445
sl@0
   446
		case RSchedhookTest::ESetTestThread:
sl@0
   447
			{
sl@0
   448
			NKern::ThreadEnterCS();  // Prevent us from dying or suspending whilst holding a DMutex
sl@0
   449
			DObjectCon& threads=*Kern::Containers()[EThread];  // Get containing holding threads
sl@0
   450
			threads.Wait();  // Obtain the container mutex so the list does get changed under us
sl@0
   451
			TestThread = &ThreadFromId((TUint)a1)->iNThread;
sl@0
   452
			threads.Signal();  // Release the container mutex
sl@0
   453
			NKern::ThreadLeaveCS();  // End of critical section
sl@0
   454
			TestCount = 0;
sl@0
   455
			}
sl@0
   456
			break;
sl@0
   457
sl@0
   458
		case RSchedhookTest::EGetThreadContext:
sl@0
   459
			return TestGetThreadContext(ThreadFromId((TUint)a1),(TDes8*)a2);
sl@0
   460
sl@0
   461
		case RSchedhookTest::EGetTestCount:
sl@0
   462
			return TestCount;
sl@0
   463
sl@0
   464
		default:
sl@0
   465
			r=KErrNotSupported;
sl@0
   466
			break;
sl@0
   467
		}
sl@0
   468
	return r;
sl@0
   469
	}
sl@0
   470