summaryrefslogtreecommitdiff
path: root/src/inc/regdisp.h
blob: d08c44c90491831cf1aa7cf556dae2809f551249 (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
// 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.

#ifndef __REGDISP_H
#define __REGDISP_H


#ifdef DEBUG_REGDISPLAY
class Thread;
struct REGDISPLAY;
void CheckRegDisplaySP (REGDISPLAY *pRD);
#endif // DEBUG_REGDISPLAY

struct REGDISPLAY_BASE {
    PT_CONTEXT pContext;    // This is the context of the active call frame;
                            // either returned by GetContext or provided at
                            // exception time.
                            //
                            // This will be used to resume execution, so
                            // do NOT trash it! But DO update any static
                            // registers here.

#ifdef WIN64EXCEPTIONS
    PT_CONTEXT pCurrentContext;   // [trashed] points to current Context of stackwalk
    PT_CONTEXT pCallerContext;    // [trashed] points to the Context of the caller during stackwalk -- used for GC crawls

    // [trashed] points to current context pointers of stackwalk
    T_KNONVOLATILE_CONTEXT_POINTERS *pCurrentContextPointers;
    // [trashed] points to the context pointers of the caller during stackwalk -- used for GC crawls
    T_KNONVOLATILE_CONTEXT_POINTERS *pCallerContextPointers;

    BOOL IsCallerContextValid;  // TRUE if pCallerContext really contains the caller's context
    BOOL IsCallerSPValid;       // Don't add usage of this field.  This is only temporary.

    T_CONTEXT  ctxOne;    // used by stackwalk
    T_CONTEXT  ctxTwo;    // used by stackwalk

    T_KNONVOLATILE_CONTEXT_POINTERS ctxPtrsOne;  // used by stackwalk
    T_KNONVOLATILE_CONTEXT_POINTERS ctxPtrsTwo;  // used by stackwalk
#endif // WIN64EXCEPTIONS

#ifdef DEBUG_REGDISPLAY
    Thread *_pThread;
#endif // DEBUG_REGDISPLAY

    TADDR SP;
    TADDR ControlPC;
};

inline PCODE GetControlPC(REGDISPLAY_BASE *pRD) {
    LIMITED_METHOD_DAC_CONTRACT;
    return (PCODE)(pRD->ControlPC);
}

inline TADDR GetRegdisplaySP(REGDISPLAY_BASE *pRD) {
    LIMITED_METHOD_DAC_CONTRACT;

    return pRD->SP;
}

inline void SetRegdisplaySP(REGDISPLAY_BASE *pRD, LPVOID sp) {
    LIMITED_METHOD_DAC_CONTRACT;

    pRD->SP = (TADDR)sp;
}

#if defined(_TARGET_X86_)

struct REGDISPLAY : public REGDISPLAY_BASE {

#ifndef WIN64EXCEPTIONS
    // TODO: Unify with pCurrentContext / pCallerContext used on 64-bit
    PCONTEXT pContextForUnwind; // scratch context for unwinding
                                // used to preserve context saved in the frame that
                                // could be otherwise wiped by the unwinding

    DWORD * pEdi;
    DWORD * pEsi;
    DWORD * pEbx;
    DWORD * pEdx;
    DWORD * pEcx;
    DWORD * pEax;

    DWORD * pEbp;
#endif // !WIN64EXCEPTIONS

#ifndef WIN64EXCEPTIONS

#define REG_METHODS(reg) \
    inline PDWORD Get##reg##Location(void) { return p##reg;  } \
    inline void   Set##reg##Location(PDWORD p##reg) { this->p##reg = p##reg; }

#else // !WIN64EXCEPTIONS

#define REG_METHODS(reg) \
    inline PDWORD Get##reg##Location(void) { return pCurrentContextPointers->reg; } \
    inline void   Set##reg##Location(PDWORD p##reg) { pCurrentContextPointers->reg = p##reg; }

#endif // WIN64EXCEPTIONS

    REG_METHODS(Eax)
    REG_METHODS(Ecx)
    REG_METHODS(Edx)

    REG_METHODS(Ebx)
    REG_METHODS(Esi)
    REG_METHODS(Edi)
    REG_METHODS(Ebp)

#undef REG_METHODS

    TADDR   PCTAddr;
};

inline TADDR GetRegdisplayFP(REGDISPLAY *display) {
    LIMITED_METHOD_DAC_CONTRACT;

    return (TADDR)*display->GetEbpLocation();
}

inline LPVOID GetRegdisplayFPAddress(REGDISPLAY *display) {
    LIMITED_METHOD_CONTRACT;
    
    return (LPVOID)display->GetEbpLocation();
}


// This function tells us if the given stack pointer is in one of the frames of the functions called by the given frame
inline BOOL IsInCalleesFrames(REGDISPLAY *display, LPVOID stackPointer) {
    LIMITED_METHOD_CONTRACT;

#ifdef WIN64EXCEPTIONS
    return stackPointer < ((LPVOID)(display->SP));
#else
    return (TADDR)stackPointer < display->PCTAddr;
#endif
}
inline TADDR GetRegdisplayStackMark(REGDISPLAY *display) {
    LIMITED_METHOD_DAC_CONTRACT;

#ifdef WIN64EXCEPTIONS
    _ASSERTE(GetRegdisplaySP(display) == GetSP(display->pCurrentContext));
    return GetRegdisplaySP(display);
#else
    return display->PCTAddr;
#endif
}

#elif defined(_WIN64)

#if defined(_TARGET_ARM64_)
typedef struct _Arm64VolatileContextPointer
{
    union {
        struct {
            PDWORD64 X0;
            PDWORD64 X1;
            PDWORD64 X2;
            PDWORD64 X3;
            PDWORD64 X4;
            PDWORD64 X5;
            PDWORD64 X6;
            PDWORD64 X7;
            PDWORD64 X8;
            PDWORD64 X9;
            PDWORD64 X10;
            PDWORD64 X11;
            PDWORD64 X12;
            PDWORD64 X13;
            PDWORD64 X14;
            PDWORD64 X15;
            PDWORD64 X16;
            PDWORD64 X17;
            //X18 is reserved by OS, in userspace it represents TEB
        };
        PDWORD64 X[18];
    };
} Arm64VolatileContextPointer;
#endif //_TARGET_ARM64_
struct REGDISPLAY : public REGDISPLAY_BASE {
#ifdef _TARGET_ARM64_
    Arm64VolatileContextPointer     volatileCurrContextPointers;
#endif

    REGDISPLAY()
    {
        // Initialize
        memset(this, 0, sizeof(REGDISPLAY));
    }
};


inline TADDR GetRegdisplayFP(REGDISPLAY *display) {
    LIMITED_METHOD_CONTRACT;
    return NULL; 
}

inline TADDR GetRegdisplayFPAddress(REGDISPLAY *display) {
    LIMITED_METHOD_CONTRACT;
    return NULL; 
}

// This function tells us if the given stack pointer is in one of the frames of the functions called by the given frame
inline BOOL IsInCalleesFrames(REGDISPLAY *display, LPVOID stackPointer)
{
    LIMITED_METHOD_CONTRACT;
    return stackPointer < ((LPVOID)(display->SP));
}

inline TADDR GetRegdisplayStackMark(REGDISPLAY *display)
{
#if defined(_TARGET_AMD64_)
    // On AMD64, the MemoryStackFp value is the current sp (i.e. the sp value when calling another method).
    _ASSERTE(GetRegdisplaySP(display) == GetSP(display->pCurrentContext));
    return GetRegdisplaySP(display);

#elif defined(_TARGET_ARM64_)

    _ASSERTE(display->IsCallerContextValid);
    return GetSP(display->pCallerContext);

#else  // _TARGET_AMD64_
    PORTABILITY_ASSERT("GetRegdisplayStackMark NYI for this platform (Regdisp.h)");
    return NULL;
#endif // _TARGET_AMD64_
}

#elif defined(_TARGET_ARM_)

// ResumableFrame is pushed on the stack before
// starting the GC. registers r0-r3 in ResumableFrame can 
// contain roots which might need to be updated if they are
// relocated. On Stack walking the addresses of the registers in the
// resumable Frame are passed to GC using pCurrentContextPointers
// member in _REGDISPLAY. However On ARM KNONVOLATILE_CONTEXT_POINTERS
// does not contain pointers for volatile registers. Therefore creating
// this structure to store pointers to volatile registers and adding an object 
// as member in _REGDISPLAY
typedef struct _ArmVolatileContextPointer
{
    PDWORD R0;
    PDWORD R1;
    PDWORD R2;
    PDWORD R3;
    PDWORD R12;
} ArmVolatileContextPointer;

struct REGDISPLAY : public REGDISPLAY_BASE {
    ArmVolatileContextPointer     volatileCurrContextPointers;

    DWORD *  pPC;                // processor neutral name

    REGDISPLAY()
    {
        // Initialize regdisplay
        memset(this, 0, sizeof(REGDISPLAY));

        // Setup the pointer to ControlPC field
        pPC = &ControlPC;
    }
};

// This function tells us if the given stack pointer is in one of the frames of the functions called by the given frame
inline BOOL IsInCalleesFrames(REGDISPLAY *display, LPVOID stackPointer) {
    LIMITED_METHOD_CONTRACT;
    return stackPointer < ((LPVOID)(TADDR)(display->SP));
}

inline TADDR GetRegdisplayStackMark(REGDISPLAY *display) {
    LIMITED_METHOD_CONTRACT;
    // ARM uses the establisher frame as the marker
    _ASSERTE(display->IsCallerContextValid);
    return GetSP(display->pCallerContext);
}

#else // none of the above processors

PORTABILITY_WARNING("RegDisplay functions are not implemented on this platform.")

struct REGDISPLAY : public REGDISPLAY_BASE {
    size_t * FramePtr;
    SLOT   * pPC;
};

inline TADDR GetRegdisplayFP(REGDISPLAY *display) {
    LIMITED_METHOD_CONTRACT;
    return (TADDR)*(display->FramePtr);
}

inline BOOL IsInCalleesFrames(REGDISPLAY *display, LPVOID stackPointer) {
    LIMITED_METHOD_CONTRACT;
    return FALSE;
}
inline LPVOID GetRegdisplayStackMark(REGDISPLAY *display) {
    LIMITED_METHOD_CONTRACT;
    return (LPVOID)display->SP;
}

#endif

#if defined(_WIN64) || defined(_TARGET_ARM_) || (defined(_TARGET_X86_) && defined(WIN64EXCEPTIONS))
// This needs to be implemented for platforms that have funclets.
inline LPVOID GetRegdisplayReturnValue(REGDISPLAY *display)
{
    LIMITED_METHOD_CONTRACT;

#if defined(_TARGET_AMD64_)
    return (LPVOID)display->pCurrentContext->Rax;
#elif defined(_TARGET_ARM64_)
    return (LPVOID)display->pCurrentContext->X0;
#elif defined(_TARGET_ARM_)
    return (LPVOID)display->pCurrentContext->R0;
#elif defined(_TARGET_X86_)
    return (LPVOID)display->pCurrentContext->Eax;
#else
    PORTABILITY_ASSERT("GetRegdisplayReturnValue NYI for this platform (Regdisp.h)");
    return NULL;
#endif
}

inline void SyncRegDisplayToCurrentContext(REGDISPLAY* pRD)
{
    LIMITED_METHOD_CONTRACT;

#if defined(_WIN64)
    pRD->SP         = (INT_PTR)GetSP(pRD->pCurrentContext);
    pRD->ControlPC  = INT_PTR(GetIP(pRD->pCurrentContext));
#elif defined(_TARGET_ARM_) // _WIN64
    pRD->SP         = (DWORD)GetSP(pRD->pCurrentContext);
    pRD->ControlPC  = (DWORD)GetIP(pRD->pCurrentContext);
#elif defined(_TARGET_X86_) // _TARGET_ARM_
    pRD->SP         = (DWORD)GetSP(pRD->pCurrentContext);
    pRD->ControlPC  = (DWORD)GetIP(pRD->pCurrentContext);
#else // _TARGET_X86_
    PORTABILITY_ASSERT("SyncRegDisplayToCurrentContext");
#endif // _TARGET_ARM_ || _TARGET_X86_

#ifdef DEBUG_REGDISPLAY
    CheckRegDisplaySP(pRD);
#endif // DEBUG_REGDISPLAY
}
#endif // _WIN64 || _TARGET_ARM_ || (_TARGET_X86_ && WIN64EXCEPTIONS)

typedef REGDISPLAY *PREGDISPLAY;


inline void FillRegDisplay(const PREGDISPLAY pRD, PT_CONTEXT pctx, PT_CONTEXT pCallerCtx = NULL)
{
    WRAPPER_NO_CONTRACT;

    SUPPORTS_DAC;

#ifndef WIN64EXCEPTIONS
#ifdef _TARGET_X86_
    pRD->pContext = pctx;
    pRD->pContextForUnwind = NULL;
    pRD->pEdi = &(pctx->Edi);
    pRD->pEsi = &(pctx->Esi);
    pRD->pEbx = &(pctx->Ebx);
    pRD->pEbp = &(pctx->Ebp);
    pRD->pEax = &(pctx->Eax);
    pRD->pEcx = &(pctx->Ecx);
    pRD->pEdx = &(pctx->Edx);
    pRD->SP   = pctx->Esp;
    pRD->ControlPC = (PCODE)(pctx->Eip);
    pRD->PCTAddr = (UINT_PTR)&(pctx->Eip);
#else // _TARGET_X86_
    PORTABILITY_ASSERT("FillRegDisplay");
#endif // _TARGET_???_ (ELSE)

#else // !WIN64EXCEPTIONS
    pRD->pContext   = pctx;

    // Setup the references
    pRD->pCurrentContextPointers = &pRD->ctxPtrsOne;
    pRD->pCallerContextPointers = &pRD->ctxPtrsTwo;

    pRD->pCurrentContext = &(pRD->ctxOne);
    pRD->pCallerContext  = &(pRD->ctxTwo);

    // copy the active context to initialize our stackwalk
    *(pRD->pCurrentContext)     = *(pctx);

    // copy the caller context as well if it's specified
    if (pCallerCtx == NULL)
    {
        pRD->IsCallerContextValid = FALSE;
        pRD->IsCallerSPValid      = FALSE;        // Don't add usage of this field.  This is only temporary.
    }
    else
    {
        *(pRD->pCallerContext)    = *(pCallerCtx);
        pRD->IsCallerContextValid = TRUE;
        pRD->IsCallerSPValid      = TRUE;        // Don't add usage of this field.  This is only temporary.
    }

#ifdef _TARGET_AMD64_
    for (int i = 0; i < 16; i++)
    {
        *(&pRD->ctxPtrsOne.Rax + i) = (&pctx->Rax + i);
    }
#elif defined(_TARGET_ARM64_) // _TARGET_AMD64_
    for (int i = 0; i < 12; i++)
    {
        *(&pRD->ctxPtrsOne.X19 + i) = (&pctx->X19 + i);
    }
#elif defined(_TARGET_ARM_) // _TARGET_ARM64_
    // Copy over the nonvolatile integer registers (R4-R11)
    for (int i = 0; i < 8; i++)
    {
        *(&pRD->ctxPtrsOne.R4 + i) = (&pctx->R4 + i);
    }

    pRD->ctxPtrsOne.Lr = &pctx->Lr;
    pRD->pPC = &pRD->pCurrentContext->Pc;
#elif defined(_TARGET_X86_) // _TARGET_ARM_
    for (int i = 0; i < 7; i++)
    {
        *(&pRD->ctxPtrsOne.Esi + i) = (&pctx->Esi + i);
    }
#else // _TARGET_X86_
    PORTABILITY_ASSERT("FillRegDisplay");
#endif // _TARGET_???_ (ELSE)

#ifdef DEBUG_REGDISPLAY
    pRD->_pThread = NULL;
#endif // DEBUG_REGDISPLAY

    // This will setup the PC and SP
    SyncRegDisplayToCurrentContext(pRD);
#endif // !WIN64EXCEPTIONS
}

// Initialize a new REGDISPLAY/CONTEXT pair from an existing valid REGDISPLAY.
inline void CopyRegDisplay(const PREGDISPLAY pInRD, PREGDISPLAY pOutRD, T_CONTEXT *pOutCtx)
{
    WRAPPER_NO_CONTRACT;

    // The general strategy is to extract the register state from the input REGDISPLAY 
    // into the new CONTEXT then simply call FillRegDisplay.

    T_CONTEXT* pOutCallerCtx = NULL;

#ifndef WIN64EXCEPTIONS

#if defined(_TARGET_X86_)
    if (pInRD->pEdi != NULL) {pOutCtx->Edi = *pInRD->pEdi;} else {pInRD->pEdi = NULL;}
    if (pInRD->pEsi != NULL) {pOutCtx->Esi = *pInRD->pEsi;} else {pInRD->pEsi = NULL;}
    if (pInRD->pEbx != NULL) {pOutCtx->Ebx = *pInRD->pEbx;} else {pInRD->pEbx = NULL;}
    if (pInRD->pEbp != NULL) {pOutCtx->Ebp = *pInRD->pEbp;} else {pInRD->pEbp = NULL;}
    if (pInRD->pEax != NULL) {pOutCtx->Eax = *pInRD->pEax;} else {pInRD->pEax = NULL;}
    if (pInRD->pEcx != NULL) {pOutCtx->Ecx = *pInRD->pEcx;} else {pInRD->pEcx = NULL;}
    if (pInRD->pEdx != NULL) {pOutCtx->Edx = *pInRD->pEdx;} else {pInRD->pEdx = NULL;}
    pOutCtx->Esp = pInRD->SP;
    pOutCtx->Eip = pInRD->ControlPC;
#else // _TARGET_X86_
    PORTABILITY_ASSERT("CopyRegDisplay");
#endif // _TARGET_???_

#else // WIN64EXCEPTIONS

    *pOutCtx = *(pInRD->pCurrentContext);
    if (pInRD->IsCallerContextValid)
    {
        pOutCallerCtx = pInRD->pCallerContext;
    }

#endif // WIN64EXCEPTIONS

    if (pOutRD)
        FillRegDisplay(pOutRD, pOutCtx, pOutCallerCtx);
}

// Get address of a register in a CONTEXT given the reg number. For X86, 
// the reg number is the R/M number from ModR/M byte or base in SIB byte
inline size_t * getRegAddr (unsigned regNum, PTR_CONTEXT regs)
{
#ifdef _TARGET_X86_
    switch (regNum)
    {
    case 0:
        return (size_t *)&regs->Eax;
        break;
    case 1:
        return (size_t *)&regs->Ecx;
        break;
    case 2:
        return (size_t *)&regs->Edx;
        break;
    case 3:
        return (size_t *)&regs->Ebx;
        break;
    case 4:
        return (size_t *)&regs->Esp;
        break;
    case 5:
        return (size_t *)&regs->Ebp;
        break;
    case 6:
        return (size_t *)&regs->Esi;
        break;
    case 7:
        return (size_t *)&regs->Edi;
        break;
    default:
        _ASSERTE (!"unknown regNum");
    }
#elif defined(_TARGET_AMD64_)
    _ASSERTE(regNum < 16);
    return &regs->Rax + regNum;
#elif defined(_TARGET_ARM_)
        _ASSERTE(regNum < 16);
        return (size_t *)&regs->R0 + regNum;
#elif defined(_TARGET_ARM64_)
    _ASSERTE(regNum < 31);
    return (size_t *)&regs->X0 + regNum;
#else
    _ASSERTE(!"@TODO Port - getRegAddr (Regdisp.h)");
#endif
    return(0);
}

//---------------------------------------------------------------------------------------
//
// This is just a simpler helper function to convert a REGDISPLAY to a CONTEXT.
//
// Arguments:
//    pRegDisp - the REGDISPLAY to be converted
//    pContext - the buffer for storing the converted CONTEXT
//
inline void UpdateContextFromRegDisp(PREGDISPLAY pRegDisp, PT_CONTEXT pContext)
{
    _ASSERTE((pRegDisp != NULL) && (pContext != NULL));

#ifndef WIN64EXCEPTIONS

#if defined(_TARGET_X86_)
    pContext->ContextFlags = (CONTEXT_INTEGER | CONTEXT_CONTROL);
    pContext->Edi = *pRegDisp->pEdi;
    pContext->Esi = *pRegDisp->pEsi;
    pContext->Ebx = *pRegDisp->pEbx;
    pContext->Ebp = *pRegDisp->pEbp;
    pContext->Eax = *pRegDisp->pEax;
    pContext->Ecx = *pRegDisp->pEcx;
    pContext->Edx = *pRegDisp->pEdx;
    pContext->Esp = pRegDisp->SP;
    pContext->Eip = pRegDisp->ControlPC;
#else // _TARGET_X86_
    PORTABILITY_ASSERT("UpdateContextFromRegDisp");
#endif // _TARGET_???_

#else // WIN64EXCEPTIONS

    *pContext = *pRegDisp->pCurrentContext;

#endif // WIN64EXCEPTIONS
}


#endif  // __REGDISP_H