Welcome to mirror list, hosted at ThFree Co, Russian Federation.

JITCodeManager.cpp « jitinterface « Native « src - github.com/mono/corert.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 198671f14eed78955c23722f0d251924acf98279 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#include "common.h"

#include "JITCodeManager.h"

#include "CodeHeap.h"
#include <mutex>

#include "../Runtime/coreclr/GCInfoDecoder.h"

#ifdef USE_GROWABLE_FUNCTION_TABLE
#include <Windows.h>
#endif

void EnumGCRefs(PTR_VOID pGCInfo, UINT32 curOffs, REGDISPLAY * pRD, GCEnumContext * hCallback, bool executionAborted)
{
    GcInfoDecoder gcInfoDecoder(
                        GCInfoToken(pGCInfo),
                        GcInfoDecoderFlags (DECODE_GC_LIFETIMES | DECODE_SECURITY_OBJECT | DECODE_VARARG),
                        curOffs
                        );

    if (!gcInfoDecoder.EnumerateLiveSlots(
                        pRD,
                        false /* reportScratchSlots */,
                        executionAborted ? ICodeManagerFlags::ExecutionAborted : 0, // TODO: Flags?
                        hCallback->pCallback,
                        hCallback
                        ))
    {
        // TODO: Conservative GC?
        assert(false);
    }
}

#define NYI(name) { wprintf(L"Not yet implemented: %S\n", name); DebugBreak(); ExitProcess(1); }

typedef bool (__stdcall *pfnRegisterCodeManager)(ICodeManager * pCodeManager, PTR_VOID pvStartRange, UInt32 cbRange);
typedef void (__stdcall *pfnUnregisterCodeManager)(ICodeManager * pCodeManager);

std::once_flag s_RuntimeInit;
HMODULE s_hRuntime = NULL;
pfnRegisterCodeManager s_pfnRegisterCodeManager;
pfnUnregisterCodeManager s_pfnUnregisterCodeManager;

bool InitializeCodeManagerRuntime()
{
    std::call_once(s_RuntimeInit, []()
    {
        if (s_hRuntime != NULL)
        {
            s_pfnRegisterCodeManager = (pfnRegisterCodeManager)GetProcAddress(s_hRuntime, "RegisterCodeManager");
            s_pfnUnregisterCodeManager = (pfnUnregisterCodeManager)GetProcAddress(s_hRuntime, "UnregisterCodeManager");
        }
    });

    return (s_pfnRegisterCodeManager != NULL) && (s_pfnUnregisterCodeManager != NULL);
}

// These are the flags set on an CORINFO_EH_CLAUSE
enum CORINFO_EH_CLAUSE_FLAGS
{
    CORINFO_EH_CLAUSE_NONE      = 0,
    CORINFO_EH_CLAUSE_FILTER    = 0x0001, // If this bit is on, then this EH entry is for a filter
    CORINFO_EH_CLAUSE_FINALLY   = 0x0002, // This clause is a finally clause
    CORINFO_EH_CLAUSE_FAULT     = 0x0004, // This clause is a fault clause
    CORINFO_EH_CLAUSE_DUPLICATE = 0x0008, // Duplicated clause. This clause was duplicated to a funclet which was pulled out of line
    CORINFO_EH_CLAUSE_SAMETRY   = 0x0010, // This clause covers same try block as the previous one. (Used by CoreRT ABI.)
};

extern "C" __declspec(dllexport) void __stdcall InitJitCodeManager(HMODULE mrtModule);
extern "C" __declspec(dllexport) void* __stdcall AllocJittedCode(UInt32 cbCode, UInt32 align, JITCodeManager** pCodeManager);
extern "C" __declspec(dllexport) void __stdcall SetEHInfoPtr(JITCodeManager* pCodeManager, uint8_t *pbCode, void* ehInfo);

extern "C" __declspec(dllexport) PTR_RUNTIME_FUNCTION __stdcall PublishRuntimeFunction(
    JITCodeManager *pCodeManager, 
    uint8_t *pbCode, 
    PTR_RUNTIME_FUNCTION pMainRuntimeFunction, 
    UInt32 startOffset, 
    UInt32 endOffset, 
    uint8_t* pUnwindInfo, 
    UInt32 cbUnwindInfo,
    uint8_t* pGCData, 
    UInt32 cbGCData);

extern "C" __declspec(dllexport) void __stdcall UpdateRuntimeFunctionTable(JITCodeManager *pCodeManager);

__declspec(dllexport) void __stdcall InitJitCodeManager(HMODULE mrtModule)
{
    s_hRuntime = mrtModule;
}

__declspec(dllexport) void* __stdcall AllocJittedCode(UInt32 cbCode, UInt32 align, JITCodeManager** pCodeManager)
{
    void *pCode;
    JITCodeManager::AllocCode(cbCode, align, &pCode, pCodeManager);
    return pCode;
}

CodeHeader *GetCodeHeader(uint8_t* pbCode)
{
    return (CodeHeader*)((BYTE*)pbCode - sizeof(CodeHeader));
}

__declspec(dllexport) void __stdcall SetEHInfoPtr(JITCodeManager* pCodeManager, uint8_t *pbCode, void* ehInfo)
{
    CodeHeader *hdr = GetCodeHeader(pbCode);
    hdr->SetEHInfo(ehInfo);
}

__declspec(dllexport) PTR_RUNTIME_FUNCTION __stdcall PublishRuntimeFunction(
    JITCodeManager *pCodeManager, 
    uint8_t *pbCode, 
    PTR_RUNTIME_FUNCTION pMainRuntimeFunction, 
    UInt32 startOffset, 
    UInt32 endOffset, 
    uint8_t* pUnwindInfo, 
    UInt32 cbUnwindInfo,
    uint8_t* pGCData, 
    UInt32 cbGCData)
{
    CodeHeader *hdr = GetCodeHeader(pbCode);
    DWORD codeOffset = hdr->GetCodeOffset();
    BYTE *pdataBase = (BYTE*)hdr->GetHeapBase();

    DWORD beginAddr = codeOffset + startOffset;
    DWORD endAddr = codeOffset + endOffset;

    uint8_t* pUnwindData = (uint8_t*)pCodeManager->AllocPData(cbUnwindInfo + cbGCData);
    if (pUnwindData == nullptr)
        return nullptr;

    memcpy(pUnwindData, pUnwindInfo, cbUnwindInfo);
    memcpy(pUnwindData + cbUnwindInfo, pGCData, cbGCData);
    assert(pUnwindData > pdataBase);
    assert((LONGLONG)pUnwindData - (LONGLONG)pdataBase < (LONGLONG)INT_MAX);
    DWORD unwindData = (DWORD)((PBYTE)pUnwindData - pdataBase);
    return pCodeManager->AllocRuntimeFunction(pMainRuntimeFunction, beginAddr, endAddr, unwindData);
}

__declspec(dllexport) void __stdcall UpdateRuntimeFunctionTable(JITCodeManager *pCodeManager)
{
    pCodeManager->UpdateRuntimeFunctionTable();
}

CodeHeader::CodeHeader(void *heapBase, DWORD codeOffs)
: m_heapBase((BYTE*)heapBase), m_codeOffset(codeOffs), m_ehInfo(NULL)
{
    assert(m_heapBase != nullptr);
    assert(codeOffs > 0);
}

CodeHeader::~CodeHeader()
{
}

std::list<JITCodeManager*> JITCodeManager::s_instances;
JITCodeManager * volatile JITCodeManager::s_pLastCodeManager = nullptr;
std::mutex JITCodeManager::s_instanceLock;

JITCodeManager *JITCodeManager::FindCodeManager(PTR_VOID addr)
{
    JITCodeManager *curr = s_pLastCodeManager;
    if (curr != nullptr && curr->Contains(addr))
        return curr;

    MutexHolder lock(s_instanceLock);
    for (auto instance : s_instances)
        if (instance->Contains(addr))
            return instance;

    return nullptr;
}

void JITCodeManager::AllocCode(size_t size, DWORD align, void **ppCode, JITCodeManager **ppManager)
{
    assert(ppCode != nullptr);
    JITCodeManager *curr = s_pLastCodeManager;
    
    // In practice we will only go around this loop once, and hopefully not take a lock.
    while (true)
    {
        if (curr != nullptr)
        {
            void *result = curr->m_codeHeap.AllocMemoryWithCodeHeader_NoThrow(size, align);
            if (result != nullptr)
            {
                *ppCode = result;
                if (ppManager)
                    *ppManager = curr;

                return;
            }
        }

        // Couldn't allocate with the last code manager, we now have to take a lock.
        MutexHolder lock(s_instanceLock);

        // Whoops, another thread came along and allocated a code manager.  Try again.
        if (s_pLastCodeManager != curr)
        {
            curr = s_pLastCodeManager;
            continue;
        }

        // Create a new code manager, and use it to allocate.
        JITCodeManager *pCodeMgr = new JITCodeManager();
        if (!pCodeMgr->Initialize())
            DebugBreak();  // TODO: We need to clean up error handling in mrtjit.dll.

        s_instances.push_back(pCodeMgr);
        curr = s_pLastCodeManager = pCodeMgr;
    }
}


// 8 meg code heap should be fine for bringup.
#define DEFAULT_JIT_CODE_SIZE 0x800000

JITCodeManager::JITCodeManager() : 
    m_pvStartRange(0), m_cbRange(0), 
    m_pRuntimeFunctionTable(NULL), m_nRuntimeFunctionTable(0)
{
#ifdef USE_GROWABLE_FUNCTION_TABLE
    m_hGrowableFunctionTable = NULL;
#endif

    // TODO: Clean up error handling.  This will only fail due to OOM.
    if (!m_codeHeap.Init(DEFAULT_JIT_CODE_SIZE))
        DebugBreak();

    m_pvStartRange = m_codeHeap.GetBase();
    m_cbRange = (UInt32)m_codeHeap.GetSize();
}

JITCodeManager::~JITCodeManager()
{
#ifdef USE_GROWABLE_FUNCTION_TABLE
    if (m_hGrowableFunctionTable != NULL)
        RtlDeleteGrowableFunctionTable(m_hGrowableFunctionTable);
#endif

    s_pfnUnregisterCodeManager(this);
}

bool JITCodeManager::Initialize()
{
    if (!InitializeCodeManagerRuntime())
        return false;

    return s_pfnRegisterCodeManager(this, m_pvStartRange, m_cbRange);
}

// Allocates RUNTIME_FUNCTION entry. If it corresponds to a funclet also adds a mapping
// from funclet's RUNTIME_FUNCTION to its main method's RUNTIME_FUNCTION.
// Note that main method bodies will not have an entry in the map.
PTR_RUNTIME_FUNCTION JITCodeManager::AllocRuntimeFunction(PTR_RUNTIME_FUNCTION mainMethod, DWORD beginAddr, DWORD endAddr, DWORD unwindData)
{
    ReaderWriterLock::WriteHolder lh(&m_lock);

    m_runtimeFunctions.push_back(RUNTIME_FUNCTION());
    PTR_RUNTIME_FUNCTION method = &m_runtimeFunctions.back();

    method->BeginAddress = beginAddr;
    method->EndAddress = endAddr;
    method->UnwindData = unwindData;

    // also add an entry to map funclet to its main method
    if (mainMethod != NULL)
        m_FuncletToMainMethodMap[method->BeginAddress] = mainMethod->BeginAddress;

    return method;
}


void JITCodeManager::UpdateRuntimeFunctionTable()
{
    ReaderWriterLock::WriteHolder lh(&m_lock);
    
    PTR_RUNTIME_FUNCTION pFunctionTable = &m_runtimeFunctions[0];
    DWORD nEntryCount = (DWORD)m_runtimeFunctions.size();
    DWORD nMaximumEntryCount = (DWORD)m_runtimeFunctions.capacity();

#ifdef USE_GROWABLE_FUNCTION_TABLE
    if (m_pRuntimeFunctionTable == pFunctionTable)
    {
        if (m_hGrowableFunctionTable != NULL)
            RtlGrowFunctionTable(m_hGrowableFunctionTable, nEntryCount);
    }
    else
    {
        if (m_hGrowableFunctionTable != NULL)
        {
            RtlDeleteGrowableFunctionTable(m_hGrowableFunctionTable);
            m_hGrowableFunctionTable = NULL;
        }

        // Note that there is a short time when the table is not published...

        DWORD ret = RtlAddGrowableFunctionTable(&m_hGrowableFunctionTable, pFunctionTable, nEntryCount, nMaximumEntryCount,
                                                dac_cast<TADDR>(m_pvStartRange), dac_cast<TADDR>(m_pvStartRange) + m_cbRange);
        if (ret != 0)
        {
            OutputDebugString(L"Failed to register unwindinfo");
            m_hGrowableFunctionTable = NULL;
        }
    }
#endif

    m_pRuntimeFunctionTable = pFunctionTable;
    m_nRuntimeFunctionTable = nEntryCount;
}

static int LookupUnwindInfoForMethod(UInt32 RelativePc,
                                     PTR_RUNTIME_FUNCTION pRuntimeFunctionTable,
                                     int Low,
                                     int High)
{
#ifdef _TARGET_ARM_
    RelativePc |= THUMB_CODE;
#endif 

    // Entries are sorted and terminated by sentinel value (DWORD)-1

    // Binary search the RUNTIME_FUNCTION table
    // Use linear search once we get down to a small number of elements
    // to avoid Binary search overhead.
    while (High - Low > 10) 
    {
       int Middle = Low + (High - Low) / 2;

       PTR_RUNTIME_FUNCTION pFunctionEntry = pRuntimeFunctionTable + Middle;
       if (RelativePc < pFunctionEntry->BeginAddress) 
       {
           High = Middle - 1;
       } 
       else 
       {
           Low = Middle;
       }
    }

    for (int i = Low; i <= High; ++i)
    {
        // This is safe because of entries are terminated by sentinel value (DWORD)-1
        PTR_RUNTIME_FUNCTION pNextFunctionEntry = pRuntimeFunctionTable + (i + 1);

        if (RelativePc < pNextFunctionEntry->BeginAddress)
        {
            PTR_RUNTIME_FUNCTION pFunctionEntry = pRuntimeFunctionTable + i;
            if (RelativePc >= pFunctionEntry->BeginAddress)
            {
                return i;
            }
            break;
        }
    }

    return -1;
}

struct JITMethodInfo
{
    RUNTIME_FUNCTION mainRuntimeFunction;
    RUNTIME_FUNCTION runtimeFunction;
    bool executionAborted;
};

static_assert(sizeof(JITMethodInfo) <= sizeof(MethodInfo), "Ensure that EEMethodInfo fits into the space reserved by MethodInfo");

bool JITCodeManager::FindMethodInfo(PTR_VOID        ControlPC, 
                                    MethodInfo *    pMethodInfoOut)
{
    JITMethodInfo * pMethodInfo = (JITMethodInfo *)pMethodInfoOut;

    TADDR RelativePC = dac_cast<TADDR>(ControlPC) - dac_cast<TADDR>(m_pvStartRange);

    if (RelativePC >= m_cbRange)
        return false;

    ReaderWriterLock::ReadHolder lh(&m_lock);

    int MethodIndex = LookupUnwindInfoForMethod((UInt32)RelativePC, m_pRuntimeFunctionTable, 
        0, m_nRuntimeFunctionTable - 1);
    if (MethodIndex < 0)
        return false;

    pMethodInfo->runtimeFunction = m_pRuntimeFunctionTable[MethodIndex];

    // The runtime function could correspond to a funclet.  We need to get to the 
    // runtime function of the main method. Note that main method bodies will not
    // have an entry in the map.
    int mainMethodIndex;
    std::unordered_map<DWORD, DWORD>::const_iterator iter = m_FuncletToMainMethodMap.find(pMethodInfo->runtimeFunction.BeginAddress);
    if (iter != m_FuncletToMainMethodMap.end())
    {
        DWORD mainMethodBeginAddr = iter->second;
        
        mainMethodIndex = LookupUnwindInfoForMethod(mainMethodBeginAddr, m_pRuntimeFunctionTable,
                                                    0, m_nRuntimeFunctionTable - 1);
        if (MethodIndex < 0)
            return false;
    }
    else
    {
        // only main methods will not have an entry in this map
        mainMethodIndex = MethodIndex;
    }
    pMethodInfo->mainRuntimeFunction = m_pRuntimeFunctionTable[mainMethodIndex];
    pMethodInfo->executionAborted = false;

    return true;
}

bool JITCodeManager::IsFunclet(MethodInfo * pMethInfo)
{    
    JITMethodInfo * pMethodInfo = (JITMethodInfo *)pMethInfo;
    
    // A funclet will have an entry in funclet to main method map
    ReaderWriterLock::ReadHolder lh(&m_lock);
    return m_FuncletToMainMethodMap.find(pMethodInfo->runtimeFunction.BeginAddress) != m_FuncletToMainMethodMap.end();
}

PTR_VOID JITCodeManager::GetFramePointer(MethodInfo *   pMethodInfo,
                                         REGDISPLAY *   pRegisterSet)
{
    // If the method has EHinfo then it is guaranteed to have a frame pointer.
    JITMethodInfo * pJITMethodInfo = (JITMethodInfo *)pMethodInfo;

    // Get to unwind info
    PUNWIND_INFO pUnwindInfo = (PUNWIND_INFO)(dac_cast<TADDR>(m_pvStartRange) + dac_cast<TADDR>(pJITMethodInfo->mainRuntimeFunction.UnwindData));

    if (pUnwindInfo->FrameRegister != 0)
    {
        (PTR_VOID)pRegisterSet->GetFP();
    }

    return NULL;
}

static PTR_VOID GetUnwindDataBlob(TADDR moduleBase, RUNTIME_FUNCTION * pRuntimeFunction, /* out */ SIZE_T * pSize)
{
#if defined(_TARGET_AMD64_)
    PTR_UNWIND_INFO pUnwindInfo(dac_cast<PTR_UNWIND_INFO>(moduleBase + pRuntimeFunction->UnwindData));

    SIZE_T size = offsetof(UNWIND_INFO, UnwindCode) + sizeof(UNWIND_CODE) * pUnwindInfo->CountOfUnwindCodes;

    // TODO: Personality routine
    // size = ALIGN_UP(size, sizeof(DWORD))+ sizeof(DWORD);

    *pSize = size;

    return pUnwindInfo;

#elif defined(_TARGET_ARM_)

    // if this function uses packed unwind data then at least one of the two least significant bits
    // will be non-zero.  if this is the case then there will be no xdata record to enumerate.
    _ASSERTE((pRuntimeFunction->UnwindData & 0x3) == 0);

    // compute the size of the unwind info
    PTR_TADDR xdata = dac_cast<PTR_TADDR>(pRuntimeFunction->UnwindData + moduleBase);

    ULONG epilogScopes = 0;
    ULONG unwindWords = 0;
    ULONG size = 0;

    if ((xdata[0] >> 23) != 0)
    {
        size = 4;
        epilogScopes = (xdata[0] >> 23) & 0x1f;
        unwindWords = (xdata[0] >> 28) & 0x0f;
    }
    else
    {
        size = 8;
        epilogScopes = xdata[1] & 0xffff;
        unwindWords = (xdata[1] >> 16) & 0xff;
    }

    if (!(xdata[0] & (1 << 21)))
        size += 4 * epilogScopes;

    size += 4 * unwindWords;

    _ASSERTE(xdata[0] & (1 << 20)); // personality routine should be always present
    size += 4;

    *pSize = size;
    return xdata;
#else
    POTABILITY_ASSERT("GetUnwindDataBlob");
    return NULL;
#endif
}

void EnumGCRefs(PTR_VOID pGCInfo, UINT32 curOffs, REGDISPLAY * pRD, GCEnumContext * hCallback, bool executionAborted);

void JITCodeManager::EnumGcRefs(MethodInfo *    pMethodInfo, 
                                PTR_VOID        safePointAddress,
                                REGDISPLAY *    pRegisterSet,
                                GCEnumContext * hCallback)
{
    JITMethodInfo * pJITMethodInfo = (JITMethodInfo *)pMethodInfo;
    void *methodStartAddr = (BYTE*)m_pvStartRange + pJITMethodInfo->mainRuntimeFunction.BeginAddress;

    UInt32 codeOffset = (UInt32)(dac_cast<TADDR>(safePointAddress) - dac_cast<TADDR>(methodStartAddr));

    SIZE_T nUnwindDataSize;
    PTR_VOID pUnwindData = GetUnwindDataBlob(dac_cast<TADDR>(m_pvStartRange), &pJITMethodInfo->mainRuntimeFunction, &nUnwindDataSize);

    // GCInfo immediatelly follows unwind data
    PTR_VOID pGCInfo = dac_cast<PTR_VOID>(dac_cast<TADDR>(pUnwindData) + nUnwindDataSize);

    ::EnumGCRefs(pGCInfo, codeOffset, pRegisterSet, hCallback, pJITMethodInfo->executionAborted);
}

bool JITCodeManager::UnwindStackFrame(MethodInfo *    pMethodInfo,
                                      REGDISPLAY *    pRegisterSet,                 // in/out
                                      PTR_VOID *      ppPreviousTransitionFrame)    // out
{
    JITMethodInfo * pJITMethodInfo = (JITMethodInfo *)pMethodInfo;

    // TODO: PInvoke transitions
    *ppPreviousTransitionFrame = NULL;

    CONTEXT context;
    KNONVOLATILE_CONTEXT_POINTERS contextPointers;

#ifdef _DEBUG
    memset(&context, 0xDD, sizeof(context));
    memset(&contextPointers, 0xDD, sizeof(contextPointers));
#endif

    // TODO: Local copy of the OS unwinder to avoid the CONTEXT copying?

#define FOR_EACH_NONVOLATILE_REGISTER(F) \
    F(Rax) F(Rcx) F(Rdx) F(Rbx) F(Rbp) F(Rsi) F(Rdi) F(R8) F(R9) F(R10) F(R11) F(R12) F(R13) F(R14) F(R15)

#define REGDISPLAY_TO_CONTEXT(reg) \
    contextPointers.reg = (PDWORD64) pRegisterSet->p##reg; \
    if (pRegisterSet->p##reg != NULL) context.reg = *(pRegisterSet->p##reg);

#define CONTEXT_TO_REGDISPLAY(reg) \
    pRegisterSet->p##reg = (PTR_UIntNative) contextPointers.reg;

    FOR_EACH_NONVOLATILE_REGISTER(REGDISPLAY_TO_CONTEXT);

    memcpy(&context.Xmm6, pRegisterSet->Xmm, sizeof(pRegisterSet->Xmm));

    context.Rsp = pRegisterSet->SP;
    context.Rip = pRegisterSet->IP;

    SIZE_T  EstablisherFrame;
    PVOID   HandlerData;

    RtlVirtualUnwind(NULL,
                    dac_cast<TADDR>(m_pvStartRange),
                    pRegisterSet->IP,
                    &pJITMethodInfo->runtimeFunction,
                    &context,
                    &HandlerData,
                    &EstablisherFrame,
                    &contextPointers);

    pRegisterSet->SP = context.Rsp;
    pRegisterSet->IP = context.Rip;

    pRegisterSet->pIP = PTR_PCODE(pRegisterSet->SP - sizeof(TADDR));

    memcpy(pRegisterSet->Xmm, &context.Xmm6, sizeof(pRegisterSet->Xmm));

    FOR_EACH_NONVOLATILE_REGISTER(CONTEXT_TO_REGDISPLAY);

#undef FOR_EACH_NONVOLATILE_REGISTER
#undef REGDISPLAY_TO_CONTEXT
#undef CONTEXT_TO_REGDISPLAY

    return true;
}

bool JITCodeManager::GetReturnAddressHijackInfo(MethodInfo *    pMethodInfo,
                                                REGDISPLAY *    pRegisterSet,       // in
                                                PTR_PTR_VOID *  ppvRetAddrLocation, // out
                                                GCRefKind *     pRetValueKind)      // out
{
    NYI("GetReturnAddressHijackInfo");
}

void JITCodeManager::UnsynchronizedHijackMethodLoops(MethodInfo * pMethodInfo)
{
    NYI("UnsynchronizedHijackMethodLoops");
}

PTR_VOID JITCodeManager::RemapHardwareFaultToGCSafePoint(MethodInfo * pMethodInfo, PTR_VOID controlPC)
{
    // TODO - GCInfo decoder needs to know whether execution of the method is aborted 
    // while querying for gc-info.  But ICodeManager::EnumGCRef() doesn't receive any
    // flags from mrt.  For this reason on short-term, call to this method is used as
    // a cue to mark the method info as execution aborted.  If pMethodInfo is cached 
    // by mrt, this scheme will not work.
    //
    // If the method has EH, then JIT will make sure the method is fully interruptible
    // and we will have GC-info available at the faulting address as well.

    JITMethodInfo * pJITMethodInfo = (JITMethodInfo *)pMethodInfo;
    pJITMethodInfo->executionAborted = true;

    return controlPC;
}

struct EEEHEnumState
{
    PTR_UInt8 pMethodStartAddress;
    PTR_UInt8 pEHInfo;
    UInt32 uClause;
    UInt32 nClauses;
};

static_assert(sizeof(EEEHEnumState) <= sizeof(EHEnumState), "Ensure that EEEHEnumState fits into the space reserved by EHEnumState");

bool JITCodeManager::EHEnumInit(MethodInfo * pMethodInfo, PTR_VOID * pMethodStartAddress, EHEnumState * pEHEnumStateOut)
{
    assert(pMethodInfo != NULL);
    assert(pMethodStartAddress != NULL);
    assert(pEHEnumStateOut != NULL);

    // return if there is no EH info associated with this method
    JITMethodInfo * pJITMethodInfo = (JITMethodInfo *)pMethodInfo;
    void *methodStartAddr = (BYTE*)m_pvStartRange + pJITMethodInfo->mainRuntimeFunction.BeginAddress;
    CodeHeader* hdr = GetCodeHeader(methodStartAddr);
    void *ehInfo = hdr->GetEHInfo();
    if (ehInfo == NULL)
    {
        return false;
    }

    *pMethodStartAddress = methodStartAddr;

    EEEHEnumState * pEnumState = (EEEHEnumState *)pEHEnumStateOut;
    pEnumState->pMethodStartAddress = dac_cast<PTR_UInt8>(methodStartAddr);
    pEnumState->pEHInfo = (PTR_UInt8)ehInfo;
    pEnumState->uClause = 0;
    pEnumState->nClauses = VarInt::ReadUnsigned(pEnumState->pEHInfo);

    return true;
}

bool JITCodeManager::EHEnumNext(EHEnumState * pEHEnumState, EHClause * pEHClauseOut)
{
    assert(pEHEnumState != NULL);
    assert(pEHClauseOut != NULL);

    EEEHEnumState * pEnumState = (EEEHEnumState *)pEHEnumState;
    if (pEnumState->uClause >= pEnumState->nClauses)
    {
        return false;
    }

    if (pEnumState->uClause >= pEnumState->nClauses)
    {
        return false;
    }

    pEnumState->uClause++;

    pEHClauseOut->m_tryStartOffset = VarInt::ReadUnsigned(pEnumState->pEHInfo);

    UInt32 tryEndDeltaAndClauseKind = VarInt::ReadUnsigned(pEnumState->pEHInfo);
    pEHClauseOut->m_clauseKind = (EHClauseKind)(tryEndDeltaAndClauseKind & 0x3);
    pEHClauseOut->m_tryEndOffset = pEHClauseOut->m_tryStartOffset + (tryEndDeltaAndClauseKind >> 2);

    // For each clause, we have up to 4 integers:
    //      1)  try start offset
    //      2)  (try length << 2) | clauseKind
    //      3)  if (typed || fault || filter)    { handler start offset }
    //      4a) if (typed)                       { type RVA }
    //      4b) if (filter)                      { filter start offset }
    //
    // The first two integers have already been decoded

    switch (pEHClauseOut->m_clauseKind)
    {
    case EH_CLAUSE_TYPED:
        pEHClauseOut->m_handlerAddress = pEnumState->pMethodStartAddress + VarInt::ReadUnsigned(pEnumState->pEHInfo);

        // Read target type
        {
            // @TODO: CORERT: Compress EHInfo using type table index scheme
            // https://github.com/dotnet/corert/issues/972
            Int32 typeRelAddr = *((PTR_Int32&)pEnumState->pEHInfo)++;
            pEHClauseOut->m_pTargetType = *dac_cast<PTR_PTR_VOID>(pEnumState->pEHInfo + typeRelAddr);
        }
        break;
    case EH_CLAUSE_FAULT:
        pEHClauseOut->m_handlerAddress = pEnumState->pMethodStartAddress + VarInt::ReadUnsigned(pEnumState->pEHInfo);
        break;
    case EH_CLAUSE_FILTER:
        pEHClauseOut->m_handlerAddress = pEnumState->pMethodStartAddress + VarInt::ReadUnsigned(pEnumState->pEHInfo);
        pEHClauseOut->m_filterAddress = pEnumState->pMethodStartAddress + VarInt::ReadUnsigned(pEnumState->pEHInfo);
        break;
    default:
        assert(!"unexpected EHClauseKind");
    }

    return true;
}

UIntNative JITCodeManager::GetConservativeUpperBoundForOutgoingArgs(MethodInfo *   pMethodInfo, REGDISPLAY *   pRegisterSet)
{
    // @TODO: CORERT: GetConservativeUpperBoundForOutgoingArgs
    assert(false);
    return false;
}

PTR_VOID JITCodeManager::GetOsModuleHandle()
{
    // Should not be called
    assert(false);
    return nullptr;
}

PTR_VOID JITCodeManager::GetMethodStartAddress(MethodInfo * pMethodInfo)
{
    JITMethodInfo * pJITMethodInfo = (JITMethodInfo *)pMethodInfo;
    void *methodStartAddr = (BYTE*)m_pvStartRange + pJITMethodInfo->mainRuntimeFunction.BeginAddress;
    return methodStartAddr;
}

void * JITCodeManager::GetClasslibFunction(ClasslibFunctionId functionId)
{
    // @TODO: CORERT: GetClasslibFunction
    // Implement by delegating to corelib code manager
    assert(false);
    return false;
}

PTR_VOID JITCodeManager::GetAssociatedData(PTR_VOID ControlPC)
{
    // @TODO: CORERT: GetAssociatedData
    assert(false);
    return NULL;
}