os/kernelhwsrv/kernel/eka/common/win32/seh.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) 2004-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
// e32\common\win32\seh.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include "seh.h"
sl@0
    19
sl@0
    20
// Fill in the blank types for TWin32SEHTrap
sl@0
    21
#define __WIN32_SEH_TYPES_KNOWN__
sl@0
    22
#define __UnknownWindowsType1 EXCEPTION_RECORD
sl@0
    23
#define __UnknownWindowsType2 CONTEXT
sl@0
    24
sl@0
    25
// Pretend we're tools to avoid clashes with Win32 headers
sl@0
    26
#define __TOOLS__
sl@0
    27
#define __IN_SEH_CPP__
sl@0
    28
#include <e32cmn.h>
sl@0
    29
#include <e32cmn_private.h>
sl@0
    30
sl@0
    31
#include <emulator.h>
sl@0
    32
sl@0
    33
#include <e32panic.h>
sl@0
    34
GLREF_C void Panic(TCdtPanic);
sl@0
    35
sl@0
    36
// magic value denoting the end of the SEH handler list
sl@0
    37
static const TWin32SEHTrap* const KFencePost = (TWin32SEHTrap*)-1;
sl@0
    38
sl@0
    39
//
sl@0
    40
// Class TWin32SEHTrap
sl@0
    41
//
sl@0
    42
sl@0
    43
#ifdef __KERNEL_MODE__
sl@0
    44
sl@0
    45
extern DWORD CallFinalSEHHandler(EXCEPTION_RECORD* aException, CONTEXT* aContext)
sl@0
    46
	{
sl@0
    47
	// Get the final SEH entry on the chain
sl@0
    48
	TWin32SEHTrap* finalHandler = TWin32SEHTrap::IterateForFinal();
sl@0
    49
sl@0
    50
	// Call the handler - ignoring return value
sl@0
    51
	(void)(*finalHandler->ExceptionHandler())(aException, finalHandler, aContext);
sl@0
    52
sl@0
    53
	// Explicitly tell Win32 the exception has been handled
sl@0
    54
	return ExceptionContinueExecution;
sl@0
    55
	}
sl@0
    56
sl@0
    57
TWin32SEHTrap* TWin32SEHTrap::IterateForFinal()
sl@0
    58
	{
sl@0
    59
	TWin32SEHTrap* p = (TWin32SEHTrap*)Tib()->ExceptionList;
sl@0
    60
sl@0
    61
	// Iterate through the SEH chain to find the final SEH record that we wish to skip to
sl@0
    62
    for (; p && p!=KFencePost && p->iPrevExceptionRegistrationRecord!=KFencePost; p=p->iPrevExceptionRegistrationRecord)
sl@0
    63
		{}
sl@0
    64
	return p;
sl@0
    65
	}
sl@0
    66
sl@0
    67
TWin32SEHExceptionHandler* TWin32SEHTrap::ExceptionHandler()
sl@0
    68
	{
sl@0
    69
	return iExceptionHandler;
sl@0
    70
	}
sl@0
    71
sl@0
    72
#else // !__KERNEL_MODE__
sl@0
    73
#include <u32exec.h>
sl@0
    74
sl@0
    75
sl@0
    76
extern "C" void trap_check(TWin32SEHTrap* a)
sl@0
    77
	{
sl@0
    78
	TWin32SEHTrap* p = a->iPrevExceptionRegistrationRecord;
sl@0
    79
	if (p && p!=KFencePost && a->iExceptionHandler == p->iExceptionHandler && a->iExceptionHandler == &TWin32SEHTrap::ExceptionHandler)
sl@0
    80
		Exec::PushTrapFrame((TTrap*)p);
sl@0
    81
	else
sl@0
    82
		Exec::PushTrapFrame(0);
sl@0
    83
	}
sl@0
    84
sl@0
    85
extern "C" void untrap_check()
sl@0
    86
	{
sl@0
    87
	// search back for consecutive TWin32SEHTrap and remember the second one
sl@0
    88
	TWin32SEHTrap* p = (TWin32SEHTrap*)Tib()->ExceptionList;
sl@0
    89
	TWin32SEHTrap* q = 0;
sl@0
    90
	TWin32SEHTrap* s = 0;
sl@0
    91
	if (p && p!=KFencePost)
sl@0
    92
		{
sl@0
    93
		for(;;)
sl@0
    94
			{
sl@0
    95
			q = p->iPrevExceptionRegistrationRecord;
sl@0
    96
			if (!q || q==KFencePost)
sl@0
    97
				break;
sl@0
    98
			if (p->iExceptionHandler == &TWin32SEHTrap::ExceptionHandler && q->iExceptionHandler == &TWin32SEHTrap::ExceptionHandler)
sl@0
    99
				{
sl@0
   100
				s = q;
sl@0
   101
				break;
sl@0
   102
				}
sl@0
   103
			p = q;
sl@0
   104
			}
sl@0
   105
		}
sl@0
   106
	Exec::PushTrapFrame((TTrap*)s);
sl@0
   107
	}
sl@0
   108
sl@0
   109
// Use assembler to ensure no extra SEH frame is created by the compiler
sl@0
   110
UEXPORT_C __NAKED__ void TWin32SEHTrap::Trap()
sl@0
   111
	{
sl@0
   112
	_asm mov eax, fs:[0]
sl@0
   113
	_asm mov [ecx], eax
sl@0
   114
	_asm mov fs:[0], ecx
sl@0
   115
	_asm push ecx
sl@0
   116
	_asm call trap_check
sl@0
   117
	_asm pop ecx
sl@0
   118
	_asm ret
sl@0
   119
	}
sl@0
   120
sl@0
   121
extern "C" void panic_chain_corrupt()
sl@0
   122
	{
sl@0
   123
	Panic(EWin32SEHChainCorrupt);
sl@0
   124
	}
sl@0
   125
sl@0
   126
// Use assembler to ensure no extra SEH frame is created by the compiler
sl@0
   127
UEXPORT_C __NAKED__ void TWin32SEHTrap::UnTrap()
sl@0
   128
	{
sl@0
   129
	_asm mov eax, fs:[0]
sl@0
   130
	_asm cmp eax, ecx
sl@0
   131
	_asm ja untrap_0
sl@0
   132
	_asm jb untrap_error
sl@0
   133
	_asm mov eax, [ecx]
sl@0
   134
	_asm mov fs:[0], eax
sl@0
   135
	_asm xor eax, eax
sl@0
   136
	_asm mov [ecx], eax
sl@0
   137
	_asm call untrap_check
sl@0
   138
untrap_0:
sl@0
   139
	_asm ret
sl@0
   140
untrap_error:
sl@0
   141
	_asm jmp panic_chain_corrupt
sl@0
   142
	}
sl@0
   143
sl@0
   144
UEXPORT_C TWin32SEHTrap::TWin32SEHTrap()
sl@0
   145
	:	iPrevExceptionRegistrationRecord(NULL),
sl@0
   146
		iExceptionHandler(&ExceptionHandler)
sl@0
   147
	{
sl@0
   148
	}
sl@0
   149
sl@0
   150
// Handler called whilst Win32 is walking the SEH chain
sl@0
   151
DWORD TWin32SEHTrap::ExceptionHandler(EXCEPTION_RECORD* aException, TWin32SEHTrap* /*aRegistrationRecord*/, CONTEXT* aContext)
sl@0
   152
	{
sl@0
   153
	if (aException->ExceptionCode != EXCEPTION_MSCPP)
sl@0
   154
		{
sl@0
   155
		return Emulator::Win32SEHException(aException, aContext);
sl@0
   156
		}
sl@0
   157
	else
sl@0
   158
		{
sl@0
   159
		return ExceptionContinueSearch;
sl@0
   160
		}
sl@0
   161
	}
sl@0
   162
sl@0
   163
#if defined(__LEAVE_EQUALS_THROW__) && defined(__WINS__)
sl@0
   164
extern "C" TWin32SEHTrap* pop_trap_frame()
sl@0
   165
	{
sl@0
   166
	return (TWin32SEHTrap*)Exec::PopTrapFrame();
sl@0
   167
	}
sl@0
   168
sl@0
   169
extern "C" void leave_end()
sl@0
   170
	{
sl@0
   171
	Exec::LeaveEnd();
sl@0
   172
	}
sl@0
   173
sl@0
   174
sl@0
   175
EXPORT_C __NAKED__ TInt XLeaveException::GetReason() const
sl@0
   176
	{
sl@0
   177
	_asm push ecx
sl@0
   178
	_asm call pop_trap_frame
sl@0
   179
	_asm test eax, eax
sl@0
   180
	_asm jz no_nested_trap
sl@0
   181
sl@0
   182
	// eax points to TWin32SEHTrap to be restored
sl@0
   183
	// if current exception record is above eax on the stack just restore eax
sl@0
   184
	_asm cmp eax, esp
sl@0
   185
	_asm jbe nested_trap_error
sl@0
   186
	_asm mov edx, fs:[0]
sl@0
   187
	_asm cmp eax, edx
sl@0
   188
	_asm ja nested_trap_insert_in_middle
sl@0
   189
	_asm je no_nested_trap	// we haven't been unwound after all
sl@0
   190
sl@0
   191
	// check we eventually reach current exception record from eax
sl@0
   192
	_asm mov ecx, eax
sl@0
   193
	_asm mov edx, [eax+4]	// &TWin32SEHTrap::ExceptionHandler
sl@0
   194
nested_trap_check:
sl@0
   195
	_asm mov ecx, [ecx]
sl@0
   196
	_asm cmp ecx, 0
sl@0
   197
	_asm jz nested_trap_error
sl@0
   198
	_asm cmp ecx, 0ffffffffh
sl@0
   199
	_asm jz nested_trap_error
sl@0
   200
	_asm cmp ecx, fs:[0]
sl@0
   201
	_asm jz nested_trap_check_ok
sl@0
   202
	_asm cmp edx, [ecx+4]	// all intervening entries should be TWin32SEHTrap
sl@0
   203
	_asm jz nested_trap_check
sl@0
   204
	_asm jmp nested_trap_error
sl@0
   205
sl@0
   206
	// other SEH handlers have been added after we were unwound so we need to insert eax 'in the middle'
sl@0
   207
nested_trap_insert_in_middle:
sl@0
   208
	_asm cmp eax, [edx]
sl@0
   209
	_asm je no_nested_trap	// eax is still in the chain
sl@0
   210
	_asm jb nested_trap_insert_in_middle_found
sl@0
   211
	_asm mov edx, [edx]
sl@0
   212
	_asm jmp nested_trap_insert_in_middle
sl@0
   213
sl@0
   214
nested_trap_insert_in_middle_found:
sl@0
   215
	_asm mov ecx, [edx]		// first SEH above eax on stack
sl@0
   216
	_asm cmp ecx, 0ffffffffh
sl@0
   217
	_asm je nested_trap_error	// reached end of SEH list
sl@0
   218
	_asm push edx
sl@0
   219
sl@0
   220
	// ECX should be reachable from EAX
sl@0
   221
	_asm mov edx, eax
sl@0
   222
nested_trap_insert_in_middle_check:
sl@0
   223
	_asm mov edx, [edx]
sl@0
   224
	_asm cmp ecx, edx
sl@0
   225
	_asm je nested_trap_insert_in_middle_ok
sl@0
   226
	_asm cmp edx, 0ffffffffh
sl@0
   227
	_asm je nested_trap_error	// reached end of SEH list
sl@0
   228
	_asm test edx, edx
sl@0
   229
	_asm jnz nested_trap_insert_in_middle_check
sl@0
   230
	_asm jmp nested_trap_error
sl@0
   231
sl@0
   232
nested_trap_insert_in_middle_ok:
sl@0
   233
	_asm pop edx
sl@0
   234
	_asm mov [edx], eax		// insert eax back into chain
sl@0
   235
	_asm jmp no_nested_trap
sl@0
   236
sl@0
   237
nested_trap_check_ok:
sl@0
   238
	_asm mov fs:[0], eax	// reinstall nested trap SEH handlers
sl@0
   239
sl@0
   240
no_nested_trap:
sl@0
   241
	_asm call untrap_check	// check for other nested TRAPs
sl@0
   242
	_asm call leave_end
sl@0
   243
	_asm pop ecx
sl@0
   244
	_asm mov eax, [ecx]XLeaveException.iR
sl@0
   245
	_asm ret
sl@0
   246
sl@0
   247
nested_trap_error:
sl@0
   248
	_asm jmp panic_chain_corrupt
sl@0
   249
	}
sl@0
   250
#endif	// defined(__LEAVE_EQUALS_THROW__) && defined(__WINS__)
sl@0
   251
sl@0
   252
#endif //__KERNEL_MODE__