Update contrib.
1 // Copyright (c) 2003-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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32test\debug\t_context.cpp
16 // Exercise kernel-side functions to get, set user-side context and
17 // kernel-side exception handlers.
19 // RBusLogicalChannel, DLogicalChannel.
21 // - Load the context logical device driver, open user side handle to
22 // a LDD channel and check software exception scheme when handle is
24 // - Check user-side handler called when kernel-side handler is not
25 // created and exception is handled as expected.
26 // - Make a synchronous Kernel Executive type request to logical channel
27 // for a specific functionality and check that it is as expected.
28 // - Check that kernel-side handler is not triggered by IsExceptionHandler
30 // - Test user-side, kernel-side handlers are as expected.
31 // - In the context of thread taking hardware exception with synchronous
32 // and asynchronous killing:
33 // - check context captured in hardware exception hook and
34 // context captured in RemoveThread hook are as expected.
35 // - In the context of thread taking software exception with synchronous
36 // and asynchronous killing:
37 // - check context captured in software exception hook and
38 // context captured in RemoveThread hook are as expected.
39 // - In the context of thread blocked on WFAR:
40 // - check context captured while thread is blocked,
41 // context can be modified while thread is blocked,
42 // context captured in RemoveThread hook are as expected.
43 // - In the context of thread killed after being pre-empted by interrupt
44 // while in user mode:
45 // - check context captured while thread is spinning, context can be
46 // modified while thread is blocked, context captured in RemoveThread
47 // hook are as expected.
48 // - Check whether heap has been corrupted by all the tests.
49 // - Check that the system doesn't crash when the context of a dead thread is
51 // Platforms/Drives/Compatibility:
52 // Hardware (Automatic).
53 // Assumptions/Requirement/Pre-requisites:
54 // Failures and causes:
55 // Base Port information:
59 #define __E32TEST_EXTENSION__
63 #include <e32def_private.h>
65 #include "d_context.h"
70 ESynchronousKill, // thread kills itself in exception handler
71 EAsynchronousKill // thread suspends itself in exception handler and is killed by main thread
74 _LIT(KUserExecPanic, "USER-EXEC");
76 RTest test(_L("T_CONTEXT"));
78 TInt KernelExcCount; // incremented when user-side exception handler called
79 TInt UserExcCount; // incremented when kernel-side exception handler called
80 TExcType LastUserExcType; // set by user-side exception handler when called
85 TInt CallDeviceDriverAndSpin(TAny* aExpected)
87 TArmRegSet* expectedContext = (TArmRegSet*)aExpected;
88 expectedContext->iR13 = Channel.SpinInKernel(EFalse);
89 Channel.SpinInKernel(ETrue);
93 TInt UserRaiseExceptionThread(TAny* aExcType)
95 TExcType e = static_cast<TExcType>(reinterpret_cast<TInt>(aExcType));
96 User::RaiseException(e);
100 TInt RThreadRaiseExceptionThread(TAny* aExcType)
102 TExcType e = static_cast<TExcType>(reinterpret_cast<TInt>(aExcType));
103 User::RaiseException(e);
107 void UserExceptionHandler(TExcType aType)
109 // Check kernel-side handler was called before user-side one
110 if (Channel.IsHooked() && KernelExcCount != 1)
111 User::Invariant(); // force failure in RaiseExceptionHarness()
113 LastUserExcType = aType;
115 User::Panic(KUserExecPanic, ECausedException);
118 TInt UserTrapExceptionThread(TAny* aExcType)
120 TInt r = User::SetExceptionHandler(UserExceptionHandler, 0xFFFFFFFF);
122 return 0; // force failure in RaiseExceptionHarness()
123 TExcType e = static_cast<TExcType>(reinterpret_cast<TInt>(aExcType));
124 User::RaiseException(e);
128 TInt NaturalDeathFunc(TAny*)
129 {// Don't do too much but be sure to complete.
134 void RaiseExceptionHarness(TThreadFunction aFn, TExcType aType)
137 TInt r = t.Create(_L("RaiseException"), aFn, KDefaultStackSize, NULL,
138 reinterpret_cast<TAny*>(aType));
143 TBool oldJitSetting = User::JustInTime();
144 User::SetJustInTime(EFalse);
146 User::WaitForRequest(s);
147 User::SetJustInTime(oldJitSetting);
149 test(t.ExitType() == EExitPanic);
150 test(t.ExitCategory() == KUserExecPanic);
151 test(t.ExitReason() == ECausedException);
156 void TestSwExcNoHandlers()
158 test.Next(_L("Check software exception scheme when no handler"));
160 RaiseExceptionHarness(UserRaiseExceptionThread, EExcGeneral);
161 RaiseExceptionHarness(RThreadRaiseExceptionThread, EExcGeneral);
164 void TestUserSwExcHandlerAlone()
166 test.Next(_L("Check user-side handler called when no kernel-side handler"));
169 RaiseExceptionHarness(UserTrapExceptionThread, EExcIntegerDivideByZero);
170 test(LastUserExcType == EExcIntegerDivideByZero);
171 test(UserExcCount == 1);
174 void TestUserAndKernelSwExcHandlers()
176 test.Next(_L("Check kernel-side exception handler"));
177 test.Start(_L("Check kernel-side handler called"));
180 RaiseExceptionHarness(UserRaiseExceptionThread, EExcUserInterrupt);
181 test(KernelExcCount == 1);
182 test(Channel.LastException() == EExcUserInterrupt);
185 RaiseExceptionHarness(RThreadRaiseExceptionThread, EExcIntegerDivideByZero);
186 test(KernelExcCount == 1);
187 test(Channel.LastException() == EExcIntegerDivideByZero);
189 test.Next(_L("Check kernel-side handler not triggered by IsExceptionHandler()"));
192 (void) User::IsExceptionHandled(EExcUserInterrupt);
193 test(KernelExcCount == 0);
195 test.Next(_L("Check user-side + kernel-side handler"));
199 RaiseExceptionHarness(UserTrapExceptionThread, EExcIntegerDivideByZero);
200 test(LastUserExcType == EExcIntegerDivideByZero);
201 test(UserExcCount == 1);
202 test(KernelExcCount == 1);
203 test(Channel.LastException() == EExcIntegerDivideByZero);
209 //////////////////////////////////////////////////////////////////////////////
211 void DumpContext(const TDesC& aTitle, TArmRegSet& aContext)
213 test.Printf(_L("%S\n"), &aTitle);
214 test.Printf(_L(" r0 =%08x r1 =%08x r2 =%08x r3 =%08x\n"),aContext.iR0,aContext.iR1,aContext.iR2,aContext.iR3);
215 test.Printf(_L(" r4 =%08x r5 =%08x r6 =%08x r7 =%08x\n"),aContext.iR4,aContext.iR5,aContext.iR6,aContext.iR7);
216 test.Printf(_L(" r8 =%08x r9 =%08x r10=%08x r11=%08x\n"),aContext.iR8,aContext.iR9,aContext.iR10,aContext.iR11);
217 test.Printf(_L(" r12=%08x r13=%08x r14=%08x r15=%08x\n"),aContext.iR12,aContext.iR13,aContext.iR14,aContext.iR15);
218 test.Printf(_L(" cpsr=%08x dacr=%08x\n"),aContext.iFlags, aContext.iDacr);
221 void ModifyContext(TArmRegSet& aContext)
223 TArmReg* end = (TArmReg*)(&aContext+1);
224 for (TArmReg* p = (TArmReg*)&aContext; p<end; ++p)
229 void TestHwExcContext(TKillMode aKillMode, TUserCallbackState aCallback)
231 test.Next(_L("Check context of thread taking hardware exception"));
232 if (aKillMode == EAsynchronousKill)
233 test.Printf(_L("Thread will be asynchronously killed\n"));
235 test.Printf(_L("Thread will kill itself\n"));
237 TArmRegSet expectedContext;
239 TInt r = t.Create(_L("ContextHwExc"), ThreadContextHwExc, KDefaultStackSize, NULL, &expectedContext);
242 TArmRegSet exceptionContext;
243 TRequestStatus exceptionStatus;
244 Channel.TrapNextHwExc(t.Id(), &exceptionContext, exceptionStatus, aKillMode==ESynchronousKill);
246 TRequestStatus deathStatus;
247 TArmRegSet deathContext;
248 Channel.TrapNextDeath(t.Id(), &deathContext, deathStatus);
250 TBool oldJitSetting = User::JustInTime();
251 User::SetJustInTime(EFalse);
253 User::WaitForRequest(exceptionStatus);
254 User::SetJustInTime(oldJitSetting);
255 test(exceptionStatus == KErrNone); // See CheckSetContext() in LDD
257 test.Start(_L("Check context captured in hardware exception hook"));
258 exceptionContext.iR15 += 4; // See CheckContextHwExc()
259 DumpContext(_L("Expected context:"), expectedContext);
260 DumpContext(_L("Context in hw exception hook:"), exceptionContext);
261 test(CheckContextHwExc(&exceptionContext, &expectedContext));
263 if (aCallback != ENoCallback)
265 test.Printf(_L("Adding user callback type %d\n"), aCallback);
266 Channel.AddUserCallback(t.Id(), aCallback);
267 Channel.ResumeTrappedThread(t.Id());
270 test.Next(_L("Check context captured while thread is in callback"));
271 TArmRegSet callbackContext;
272 Channel.GetContext(t.Id(), &callbackContext);
273 callbackContext.iR15 += 4; // See CheckContextHwExc()
274 DumpContext(_L("Expected context:"), expectedContext);
275 DumpContext(_L("Actual context:"), callbackContext);
276 test(CheckContextHwExc(&callbackContext, &expectedContext));
279 ModifyContext(expectedContext);
280 DumpContext(_L("Expected context after modification:"), expectedContext);
282 if (aKillMode == EAsynchronousKill)
284 test.Next(_L("Check context can be modified by another thread"));
285 TArmRegSet modifiedContext = expectedContext;
286 test(Channel.SetAndGetBackContext(t.Id(), &modifiedContext) == KErrNone);
287 modifiedContext.iR15 = exceptionContext.iR15; // See CheckContextHwExc()
288 DumpContext(_L("Actual context after modification:"), modifiedContext);
289 test(CheckContextHwExc(&modifiedContext, &expectedContext));
291 User::SetJustInTime(EFalse);
292 t.Panic(_L("TEST"), 42);
296 // The thread has modified its own user context in the exception hook.
297 User::SetJustInTime(EFalse);
300 User::WaitForRequest(deathStatus);
301 User::SetJustInTime(oldJitSetting);
304 test.Next(_L("Check context captured in RemoveThread hook"));
305 deathContext.iR15 = exceptionContext.iR15; // See CheckContextHwExc()
306 DumpContext(_L("context in RemoveThread hook:"), deathContext);
307 test(CheckContextHwExc(&deathContext, &expectedContext));
312 void TestSwExcContext(TKillMode aKillMode, TUserCallbackState aCallback)
314 test.Next(_L("Check context of thread taking software exception (slow executive call)"));
315 if (aKillMode == EAsynchronousKill)
316 test.Printf(_L("Thread will be asynchronously killed\n"));
318 test.Printf(_L("Thread will kill itself\n"));
320 TArmRegSet expectedContext;
322 TInt r = t.Create(_L("ContextSwExc"), ThreadContextSwExc, KDefaultStackSize, NULL, &expectedContext);
325 TArmRegSet exceptionContext;
326 TRequestStatus exceptionStatus;
327 Channel.TrapNextSwExc(t.Id(), &exceptionContext, exceptionStatus, aKillMode == ESynchronousKill);
329 TRequestStatus deathStatus;
330 TArmRegSet deathContext;
331 Channel.TrapNextDeath(t.Id(), &deathContext, deathStatus);
333 TBool oldJitSetting = User::JustInTime();
334 User::SetJustInTime(EFalse);
336 User::WaitForRequest(exceptionStatus);
337 User::SetJustInTime(oldJitSetting);
338 test(exceptionStatus == KErrNone); // See CheckSetContext() in LDD
340 test.Start(_L("Check context captured in software exception hook"));
341 DumpContext(_L("Expected context:"), expectedContext);
342 DumpContext(_L("Context in sw exception hook:"), exceptionContext);
343 test(CheckContextSwExc(&exceptionContext, &expectedContext));
345 if (aCallback != ENoCallback)
347 test.Printf(_L("Adding user callback type %d\n"), aCallback);
348 Channel.AddUserCallback(t.Id(), aCallback);
349 Channel.ResumeTrappedThread(t.Id());
352 test.Next(_L("Check context captured while thread is in callback"));
353 TArmRegSet callbackContext;
354 Channel.GetContext(t.Id(), &callbackContext);
355 DumpContext(_L("Expected context:"), expectedContext);
356 DumpContext(_L("Actual context:"), callbackContext);
357 test(CheckContextSwExc(&callbackContext, &expectedContext));
360 ModifyContext(expectedContext);
361 DumpContext(_L("Expected context after modification:"), expectedContext);
363 if (aKillMode == EAsynchronousKill)
365 test.Next(_L("Check context can be modified by another thread"));
366 TArmRegSet modifiedContext = expectedContext;
367 test(Channel.SetAndGetBackContext(t.Id(), &modifiedContext) == KErrNone);
368 modifiedContext.iR14 = exceptionContext.iR14; // See CheckContextSwExc()
369 modifiedContext.iR15 = exceptionContext.iR15; // See CheckContextSwExc()
370 DumpContext(_L("Actual context after modification:"), modifiedContext);
371 test(CheckContextSwExc(&modifiedContext, &expectedContext));
373 User::SetJustInTime(EFalse);
374 t.Panic(_L("TEST"), 24);
378 // The thread has modified its own user context in the exception hook.
379 User::SetJustInTime(EFalse);
382 User::WaitForRequest(deathStatus);
383 User::SetJustInTime(oldJitSetting);
386 test.Next(_L("Check context captured in RemoveThread hook"));
387 deathContext.iR14 = exceptionContext.iR14; // See CheckContextSwExc()
388 deathContext.iR15 = exceptionContext.iR15; // See CheckContextSwExc()
389 DumpContext(_L("context in RemoveThread hook:"), deathContext);
390 test(CheckContextSwExc(&deathContext, &expectedContext));
394 void TestKillWFAR(TUserCallbackState aCallback)
396 test.Next(_L("Check context of thread blocked on WFAR"));
398 TArmRegSet expectedContext;
400 TInt r = t.Create(_L("ContextWFAR"), ThreadContextWFAR, KDefaultStackSize, NULL, &expectedContext);
403 TRequestStatus deathStatus;
404 TArmRegSet deathContext;
405 Channel.TrapNextDeath(t.Id(), &deathContext, deathStatus);
407 // Boost thread's priority and kick it off. This ensures we'll
408 // run again only after it is blocked on its request semaphore.
409 t.SetPriority(EPriorityMore);
412 if (aCallback != ENoCallback)
414 test.Printf(_L("Adding user callback type %d\n"), aCallback);
415 Channel.AddUserCallback(t.Id(), aCallback);
416 t.SetPriority(EPriorityNormal);
421 test.Start(_L("Check context captured while thread is blocked"));
422 TArmRegSet blockedContext;
423 Channel.GetContext(t.Id(), &blockedContext);
424 DumpContext(_L("Expected context:"), expectedContext);
425 DumpContext(_L("Actual context:"), blockedContext);
426 test(CheckContextWFAR(&blockedContext, &expectedContext));
428 test.Next(_L("Check context can be modified while thread is blocked"));
429 ModifyContext(expectedContext);
430 expectedContext.iR14 = blockedContext.iR14; // See CheckContextWFAR()
431 expectedContext.iR15 = blockedContext.iR15; // See CheckContextWFAR()
432 TArmRegSet modifiedContext = expectedContext;
433 test(Channel.SetAndGetBackContext(t.Id(), &modifiedContext) == KErrNone);
434 DumpContext(_L("Expected context after modification:"), expectedContext);
435 DumpContext(_L("Actual context after modification:"), modifiedContext);
436 test(CheckContextWFAR(&modifiedContext, &expectedContext));
438 TBool oldJitSetting = User::JustInTime();
439 User::SetJustInTime(EFalse);
440 t.Panic(_L("TEST"), 999);
441 User::WaitForRequest(deathStatus);
442 User::SetJustInTime(oldJitSetting);
445 test.Next(_L("Check context captured in RemoveThread hook"));
446 DumpContext(_L("Expected context:"), expectedContext);
447 DumpContext(_L("context in RemoveThread hook:\n"), deathContext);
448 test(CheckContextWFARDied(&deathContext, &expectedContext));
452 void TestKillInterrupt(TUserCallbackState aCallback)
454 test.Next(_L("Check context of thread killed after being preempted by interrupt while in user mode"));
456 TArmRegSet expectedContext;
458 TInt r = t.Create(_L("ContextKillInterrupt"), ThreadContextUserInt, KDefaultStackSize, NULL, &expectedContext);
461 TArmRegSet trappedContext;
462 TRequestStatus trapStatus;
463 Channel.TrapNextDeath(t.Id(), &trappedContext, trapStatus);
468 if (aCallback != ENoCallback)
470 test.Printf(_L("Adding user callback type %d\n"), aCallback);
471 Channel.AddUserCallback(t.Id(), aCallback);
475 test.Start(_L("Check context captured while thread is spinning"));
476 TArmRegSet spinContext;
477 Channel.GetContext(t.Id(), &spinContext);
478 DumpContext(_L("Expected context:"), expectedContext);
479 DumpContext(_L("Actual context:"), spinContext);
480 test(CheckContextUserInt(&spinContext, &expectedContext));
482 test.Next(_L("Check context can be modified while thread is spinning"));
483 ModifyContext(expectedContext);
484 expectedContext.iR15 = spinContext.iR15; // see CheckContextUserInt()
485 expectedContext.iFlags = spinContext.iFlags; // must be left unmodified
486 expectedContext.iDacr = spinContext.iDacr; // must be left unmodified
487 DumpContext(_L("Expected context after modification:"), expectedContext);
488 TArmRegSet modifiedContext = expectedContext;
489 test(Channel.SetAndGetBackContext(t.Id(), &modifiedContext) == KErrNone);
490 DumpContext(_L("Actual context after modification:"), modifiedContext);
491 test(CheckContextUserInt(&modifiedContext, &expectedContext));
493 TBool oldJitSetting = User::JustInTime();
494 User::SetJustInTime(EFalse);
496 User::WaitForRequest(trapStatus);
497 User::SetJustInTime(oldJitSetting);
500 test.Next(_L("Check context captured in RemoveThread hook"));
501 DumpContext(_L("Expected context:"), expectedContext);
502 DumpContext(_L("Trapped context:"), trappedContext);
503 test(CheckContextUserIntDied(&trappedContext, &expectedContext));
507 void TestKernelContext()
509 test.Start(_L("Test kernel context function"));
511 TArmRegSet kernelContext, expectedContext;
512 for (TInt i=0; i<15; ++i)
513 *(((TUint32*)&expectedContext)+i) = 0xa0000000 + i;
516 TInt r = t.Create(_L("SpinInKernel"), CallDeviceDriverAndSpin, KDefaultStackSize, NULL, &expectedContext);
521 Channel.GetKernelContext(t.Id(), &kernelContext);
523 TBool oldJitSetting = User::JustInTime();
524 User::SetJustInTime(EFalse);
526 User::SetJustInTime(oldJitSetting);
529 test.Next(_L("Check kernel context is as expected"));
530 DumpContext(_L("Expected context:"), expectedContext);
531 DumpContext(_L("Actual context:"), kernelContext);
532 test(CheckContextKernel(&kernelContext, &expectedContext));
537 void TestNaturalDeathContext()
539 test.Start(_L("Test setting and retrieving the context of a dead thread"));
540 TArmFullContext context;
541 memclr(&context, sizeof context);
544 TInt r = t.Create(_L("NaturalDeath"), NaturalDeathFunc, KDefaultStackSize, NULL, NULL);
549 Channel.SetAndGetFullContext(t.Id(), &context);
551 // Verify that no registers should be available for this dead thread.
552 test_Equal(0, context.iUserAvail);
553 test_Equal(0, context.iSystemAvail);
563 test.Start(_L("Loading LDD"));
564 TInt r = User::LoadLogicalDevice(_L("D_CONTEXT"));
565 test(r == KErrNone || r == KErrAlreadyExists);
567 // Must be after LDD loading because User::FreeLogicalDevice() does not
568 // free anything currently.
571 test.Next(_L("Opening LDD channel"));
575 TestSwExcNoHandlers();
576 TestUserSwExcHandlerAlone();
578 test.Next(_L("Enable event hook"));
579 r = Channel.Hook(&KernelExcCount);
582 TestUserAndKernelSwExcHandlers();
583 TestHwExcContext(ESynchronousKill, ENoCallback);
584 TestHwExcContext(EAsynchronousKill, ENoCallback);
585 TestSwExcContext(ESynchronousKill, ENoCallback);
586 TestSwExcContext(EAsynchronousKill, ENoCallback);
587 TestKillWFAR(ENoCallback);
588 TestKillInterrupt(ENoCallback);
590 TestNaturalDeathContext();
592 TestHwExcContext(EAsynchronousKill, ESpinningCallback);
593 TestHwExcContext(EAsynchronousKill, ESleepingCallback);
594 TestSwExcContext(EAsynchronousKill, ESpinningCallback);
595 TestSwExcContext(EAsynchronousKill, ESleepingCallback);
596 TestKillWFAR(ESpinningCallback);
597 TestKillWFAR(ESleepingCallback);
598 TestKillInterrupt(ESpinningCallback);
599 TestKillInterrupt(ESleepingCallback);
604 // Wait for idle + async cleanup (waits for DKernelEventHandler to go away)
605 r = UserSvr::HalFunction(EHalGroupKernel, EKernelHalSupervisorBarrier, (TAny*)5000, 0);
610 // Don't check user heap because if TLS is stored on the user-side this will not be freed when
611 // the thread exits due to an exception.