1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
9 #ifdef DEBUG_REGDISPLAY
12 void CheckRegDisplaySP (REGDISPLAY *pRD);
13 #endif // DEBUG_REGDISPLAY
15 struct REGDISPLAY_BASE {
16 PT_CONTEXT pContext; // This is the context of the active call frame;
17 // either returned by GetContext or provided at
20 // This will be used to resume execution, so
21 // do NOT trash it! But DO update any static
24 #ifdef WIN64EXCEPTIONS
25 PT_CONTEXT pCurrentContext; // [trashed] points to current Context of stackwalk
26 PT_CONTEXT pCallerContext; // [trashed] points to the Context of the caller during stackwalk -- used for GC crawls
28 // [trashed] points to current context pointers of stackwalk
29 T_KNONVOLATILE_CONTEXT_POINTERS *pCurrentContextPointers;
30 // [trashed] points to the context pointers of the caller during stackwalk -- used for GC crawls
31 T_KNONVOLATILE_CONTEXT_POINTERS *pCallerContextPointers;
33 BOOL IsCallerContextValid; // TRUE if pCallerContext really contains the caller's context
34 BOOL IsCallerSPValid; // Don't add usage of this field. This is only temporary.
36 T_CONTEXT ctxOne; // used by stackwalk
37 T_CONTEXT ctxTwo; // used by stackwalk
39 T_KNONVOLATILE_CONTEXT_POINTERS ctxPtrsOne; // used by stackwalk
40 T_KNONVOLATILE_CONTEXT_POINTERS ctxPtrsTwo; // used by stackwalk
41 #endif // WIN64EXCEPTIONS
43 #ifdef DEBUG_REGDISPLAY
45 #endif // DEBUG_REGDISPLAY
51 inline PCODE GetControlPC(REGDISPLAY_BASE *pRD) {
52 LIMITED_METHOD_DAC_CONTRACT;
53 return (PCODE)(pRD->ControlPC);
56 inline TADDR GetRegdisplaySP(REGDISPLAY_BASE *pRD) {
57 LIMITED_METHOD_DAC_CONTRACT;
62 inline void SetRegdisplaySP(REGDISPLAY_BASE *pRD, LPVOID sp) {
63 LIMITED_METHOD_DAC_CONTRACT;
68 #if defined(_TARGET_X86_)
70 struct REGDISPLAY : public REGDISPLAY_BASE {
72 #ifndef WIN64EXCEPTIONS
73 // TODO: Unify with pCurrentContext / pCallerContext used on 64-bit
74 PCONTEXT pContextForUnwind; // scratch context for unwinding
75 // used to preserve context saved in the frame that
76 // could be otherwise wiped by the unwinding
86 #endif // !WIN64EXCEPTIONS
88 #ifndef WIN64EXCEPTIONS
90 #define REG_METHODS(reg) \
91 inline PDWORD Get##reg##Location(void) { return p##reg; } \
92 inline void Set##reg##Location(PDWORD p##reg) { this->p##reg = p##reg; }
94 #else // !WIN64EXCEPTIONS
96 #define REG_METHODS(reg) \
97 inline PDWORD Get##reg##Location(void) { return pCurrentContextPointers->reg; } \
98 inline void Set##reg##Location(PDWORD p##reg) { pCurrentContextPointers->reg = p##reg; }
100 #endif // WIN64EXCEPTIONS
116 inline TADDR GetRegdisplayFP(REGDISPLAY *display) {
117 LIMITED_METHOD_DAC_CONTRACT;
119 return (TADDR)*display->GetEbpLocation();
122 inline LPVOID GetRegdisplayFPAddress(REGDISPLAY *display) {
123 LIMITED_METHOD_CONTRACT;
125 return (LPVOID)display->GetEbpLocation();
129 // This function tells us if the given stack pointer is in one of the frames of the functions called by the given frame
130 inline BOOL IsInCalleesFrames(REGDISPLAY *display, LPVOID stackPointer) {
131 LIMITED_METHOD_CONTRACT;
133 #ifdef WIN64EXCEPTIONS
134 return stackPointer < ((LPVOID)(display->SP));
136 return (TADDR)stackPointer < display->PCTAddr;
139 inline TADDR GetRegdisplayStackMark(REGDISPLAY *display) {
140 LIMITED_METHOD_DAC_CONTRACT;
142 #ifdef WIN64EXCEPTIONS
143 _ASSERTE(GetRegdisplaySP(display) == GetSP(display->pCurrentContext));
144 return GetRegdisplaySP(display);
146 return display->PCTAddr;
150 #elif defined(_TARGET_64BIT_)
152 #if defined(_TARGET_ARM64_)
153 typedef struct _Arm64VolatileContextPointer
175 //X18 is reserved by OS, in userspace it represents TEB
179 } Arm64VolatileContextPointer;
180 #endif //_TARGET_ARM64_
181 struct REGDISPLAY : public REGDISPLAY_BASE {
182 #ifdef _TARGET_ARM64_
183 Arm64VolatileContextPointer volatileCurrContextPointers;
189 memset(this, 0, sizeof(REGDISPLAY));
194 inline TADDR GetRegdisplayFP(REGDISPLAY *display) {
195 LIMITED_METHOD_CONTRACT;
199 inline TADDR GetRegdisplayFPAddress(REGDISPLAY *display) {
200 LIMITED_METHOD_CONTRACT;
204 // This function tells us if the given stack pointer is in one of the frames of the functions called by the given frame
205 inline BOOL IsInCalleesFrames(REGDISPLAY *display, LPVOID stackPointer)
207 LIMITED_METHOD_CONTRACT;
208 return stackPointer < ((LPVOID)(display->SP));
211 inline TADDR GetRegdisplayStackMark(REGDISPLAY *display)
213 #if defined(_TARGET_AMD64_)
214 // On AMD64, the MemoryStackFp value is the current sp (i.e. the sp value when calling another method).
215 _ASSERTE(GetRegdisplaySP(display) == GetSP(display->pCurrentContext));
216 return GetRegdisplaySP(display);
218 #elif defined(_TARGET_ARM64_)
220 _ASSERTE(display->IsCallerContextValid);
221 return GetSP(display->pCallerContext);
223 #else // _TARGET_AMD64_
224 PORTABILITY_ASSERT("GetRegdisplayStackMark NYI for this platform (Regdisp.h)");
226 #endif // _TARGET_AMD64_
229 #elif defined(_TARGET_ARM_)
231 // ResumableFrame is pushed on the stack before
232 // starting the GC. registers r0-r3 in ResumableFrame can
233 // contain roots which might need to be updated if they are
234 // relocated. On Stack walking the addresses of the registers in the
235 // resumable Frame are passed to GC using pCurrentContextPointers
236 // member in _REGDISPLAY. However On ARM KNONVOLATILE_CONTEXT_POINTERS
237 // does not contain pointers for volatile registers. Therefore creating
238 // this structure to store pointers to volatile registers and adding an object
239 // as member in _REGDISPLAY
240 typedef struct _ArmVolatileContextPointer
247 } ArmVolatileContextPointer;
249 struct REGDISPLAY : public REGDISPLAY_BASE {
250 ArmVolatileContextPointer volatileCurrContextPointers;
252 DWORD * pPC; // processor neutral name
253 #ifndef CROSSGEN_COMPILE
256 // Initialize regdisplay
257 memset(this, 0, sizeof(REGDISPLAY));
259 // Setup the pointer to ControlPC field
268 // This function tells us if the given stack pointer is in one of the frames of the functions called by the given frame
269 inline BOOL IsInCalleesFrames(REGDISPLAY *display, LPVOID stackPointer) {
270 LIMITED_METHOD_CONTRACT;
271 return stackPointer < ((LPVOID)(TADDR)(display->SP));
274 inline TADDR GetRegdisplayStackMark(REGDISPLAY *display) {
275 LIMITED_METHOD_CONTRACT;
276 // ARM uses the establisher frame as the marker
277 _ASSERTE(display->IsCallerContextValid);
278 return GetSP(display->pCallerContext);
281 #else // none of the above processors
282 #error "RegDisplay functions are not implemented on this platform."
285 #if defined(_TARGET_64BIT_) || defined(_TARGET_ARM_) || (defined(_TARGET_X86_) && defined(WIN64EXCEPTIONS))
286 // This needs to be implemented for platforms that have funclets.
287 inline LPVOID GetRegdisplayReturnValue(REGDISPLAY *display)
289 LIMITED_METHOD_CONTRACT;
291 #if defined(_TARGET_AMD64_)
292 return (LPVOID)display->pCurrentContext->Rax;
293 #elif defined(_TARGET_ARM64_)
294 return (LPVOID)display->pCurrentContext->X0;
295 #elif defined(_TARGET_ARM_)
296 return (LPVOID)((TADDR)display->pCurrentContext->R0);
297 #elif defined(_TARGET_X86_)
298 return (LPVOID)display->pCurrentContext->Eax;
300 PORTABILITY_ASSERT("GetRegdisplayReturnValue NYI for this platform (Regdisp.h)");
305 inline void SyncRegDisplayToCurrentContext(REGDISPLAY* pRD)
307 LIMITED_METHOD_CONTRACT;
309 #if defined(_TARGET_64BIT_)
310 pRD->SP = (INT_PTR)GetSP(pRD->pCurrentContext);
311 pRD->ControlPC = INT_PTR(GetIP(pRD->pCurrentContext));
312 #elif defined(_TARGET_ARM_)
313 pRD->SP = (DWORD)GetSP(pRD->pCurrentContext);
314 pRD->ControlPC = (DWORD)GetIP(pRD->pCurrentContext);
315 #elif defined(_TARGET_X86_)
316 pRD->SP = (DWORD)GetSP(pRD->pCurrentContext);
317 pRD->ControlPC = (DWORD)GetIP(pRD->pCurrentContext);
318 #else // _TARGET_X86_
319 PORTABILITY_ASSERT("SyncRegDisplayToCurrentContext");
322 #ifdef DEBUG_REGDISPLAY
323 CheckRegDisplaySP(pRD);
324 #endif // DEBUG_REGDISPLAY
326 #endif // _TARGET_64BIT_ || _TARGET_ARM_ || (_TARGET_X86_ && WIN64EXCEPTIONS)
328 typedef REGDISPLAY *PREGDISPLAY;
330 #ifdef WIN64EXCEPTIONS
331 inline void FillContextPointers(PT_KNONVOLATILE_CONTEXT_POINTERS pCtxPtrs, PT_CONTEXT pCtx)
333 #ifdef _TARGET_AMD64_
334 for (int i = 0; i < 16; i++)
336 *(&pCtxPtrs->Rax + i) = (&pCtx->Rax + i);
338 #elif defined(_TARGET_ARM64_) // _TARGET_AMD64_
339 for (int i = 0; i < 12; i++)
341 *(&pCtxPtrs->X19 + i) = (&pCtx->X19 + i);
343 #elif defined(_TARGET_ARM_) // _TARGET_ARM64_
344 // Copy over the nonvolatile integer registers (R4-R11)
345 for (int i = 0; i < 8; i++)
347 *(&pCtxPtrs->R4 + i) = (&pCtx->R4 + i);
349 #elif defined(_TARGET_X86_) // _TARGET_ARM_
350 for (int i = 0; i < 7; i++)
352 *(&pCtxPtrs->Edi + i) = (&pCtx->Edi + i);
354 #else // _TARGET_X86_
355 PORTABILITY_ASSERT("FillContextPointers");
356 #endif // _TARGET_???_ (ELSE)
358 #endif // WIN64EXCEPTIONS
360 inline void FillRegDisplay(const PREGDISPLAY pRD, PT_CONTEXT pctx, PT_CONTEXT pCallerCtx = NULL)
366 #ifndef WIN64EXCEPTIONS
368 pRD->pContext = pctx;
369 pRD->pContextForUnwind = NULL;
370 pRD->pEdi = &(pctx->Edi);
371 pRD->pEsi = &(pctx->Esi);
372 pRD->pEbx = &(pctx->Ebx);
373 pRD->pEbp = &(pctx->Ebp);
374 pRD->pEax = &(pctx->Eax);
375 pRD->pEcx = &(pctx->Ecx);
376 pRD->pEdx = &(pctx->Edx);
378 pRD->ControlPC = (PCODE)(pctx->Eip);
379 pRD->PCTAddr = (UINT_PTR)&(pctx->Eip);
380 #else // _TARGET_X86_
381 PORTABILITY_ASSERT("FillRegDisplay");
382 #endif // _TARGET_???_ (ELSE)
384 #else // !WIN64EXCEPTIONS
385 pRD->pContext = pctx;
387 // Setup the references
388 pRD->pCurrentContextPointers = &pRD->ctxPtrsOne;
389 pRD->pCallerContextPointers = &pRD->ctxPtrsTwo;
391 pRD->pCurrentContext = &(pRD->ctxOne);
392 pRD->pCallerContext = &(pRD->ctxTwo);
394 // copy the active context to initialize our stackwalk
395 *(pRD->pCurrentContext) = *(pctx);
397 // copy the caller context as well if it's specified
398 if (pCallerCtx == NULL)
400 pRD->IsCallerContextValid = FALSE;
401 pRD->IsCallerSPValid = FALSE; // Don't add usage of this field. This is only temporary.
405 *(pRD->pCallerContext) = *(pCallerCtx);
406 pRD->IsCallerContextValid = TRUE;
407 pRD->IsCallerSPValid = TRUE; // Don't add usage of this field. This is only temporary.
410 FillContextPointers(&pRD->ctxPtrsOne, pctx);
412 #if defined(_TARGET_ARM_)
413 // Fill volatile context pointers. They can be used by GC in the case of the leaf frame
414 pRD->volatileCurrContextPointers.R0 = &pctx->R0;
415 pRD->volatileCurrContextPointers.R1 = &pctx->R1;
416 pRD->volatileCurrContextPointers.R2 = &pctx->R2;
417 pRD->volatileCurrContextPointers.R3 = &pctx->R3;
418 pRD->volatileCurrContextPointers.R12 = &pctx->R12;
420 pRD->ctxPtrsOne.Lr = &pctx->Lr;
421 pRD->pPC = &pRD->pCurrentContext->Pc;
422 #elif defined(_TARGET_ARM64_) // _TARGET_ARM_
423 // Fill volatile context pointers. They can be used by GC in the case of the leaf frame
424 for (int i=0; i < 18; i++)
425 pRD->volatileCurrContextPointers.X[i] = &pctx->X[i];
426 #endif // _TARGET_ARM64_
428 #ifdef DEBUG_REGDISPLAY
429 pRD->_pThread = NULL;
430 #endif // DEBUG_REGDISPLAY
432 // This will setup the PC and SP
433 SyncRegDisplayToCurrentContext(pRD);
434 #endif // !WIN64EXCEPTIONS
437 // Initialize a new REGDISPLAY/CONTEXT pair from an existing valid REGDISPLAY.
438 inline void CopyRegDisplay(const PREGDISPLAY pInRD, PREGDISPLAY pOutRD, T_CONTEXT *pOutCtx)
442 // The general strategy is to extract the register state from the input REGDISPLAY
443 // into the new CONTEXT then simply call FillRegDisplay.
445 T_CONTEXT* pOutCallerCtx = NULL;
447 #ifndef WIN64EXCEPTIONS
449 #if defined(_TARGET_X86_)
450 if (pInRD->pEdi != NULL) {pOutCtx->Edi = *pInRD->pEdi;} else {pInRD->pEdi = NULL;}
451 if (pInRD->pEsi != NULL) {pOutCtx->Esi = *pInRD->pEsi;} else {pInRD->pEsi = NULL;}
452 if (pInRD->pEbx != NULL) {pOutCtx->Ebx = *pInRD->pEbx;} else {pInRD->pEbx = NULL;}
453 if (pInRD->pEbp != NULL) {pOutCtx->Ebp = *pInRD->pEbp;} else {pInRD->pEbp = NULL;}
454 if (pInRD->pEax != NULL) {pOutCtx->Eax = *pInRD->pEax;} else {pInRD->pEax = NULL;}
455 if (pInRD->pEcx != NULL) {pOutCtx->Ecx = *pInRD->pEcx;} else {pInRD->pEcx = NULL;}
456 if (pInRD->pEdx != NULL) {pOutCtx->Edx = *pInRD->pEdx;} else {pInRD->pEdx = NULL;}
457 pOutCtx->Esp = pInRD->SP;
458 pOutCtx->Eip = pInRD->ControlPC;
459 #else // _TARGET_X86_
460 PORTABILITY_ASSERT("CopyRegDisplay");
461 #endif // _TARGET_???_
463 #else // WIN64EXCEPTIONS
465 *pOutCtx = *(pInRD->pCurrentContext);
466 if (pInRD->IsCallerContextValid)
468 pOutCallerCtx = pInRD->pCallerContext;
471 #endif // WIN64EXCEPTIONS
474 FillRegDisplay(pOutRD, pOutCtx, pOutCallerCtx);
477 // Get address of a register in a CONTEXT given the reg number. For X86,
478 // the reg number is the R/M number from ModR/M byte or base in SIB byte
479 inline size_t * getRegAddr (unsigned regNum, PTR_CONTEXT regs)
482 _ASSERTE(regNum < 8);
484 static const SIZE_T OFFSET_OF_REGISTERS[] =
486 offsetof(CONTEXT, Eax),
487 offsetof(CONTEXT, Ecx),
488 offsetof(CONTEXT, Edx),
489 offsetof(CONTEXT, Ebx),
490 offsetof(CONTEXT, Esp),
491 offsetof(CONTEXT, Ebp),
492 offsetof(CONTEXT, Esi),
493 offsetof(CONTEXT, Edi),
496 return (PTR_size_t)(PTR_BYTE(regs) + OFFSET_OF_REGISTERS[regNum]);
497 #elif defined(_TARGET_AMD64_)
498 _ASSERTE(regNum < 16);
499 return ®s->Rax + regNum;
500 #elif defined(_TARGET_ARM_)
501 _ASSERTE(regNum < 16);
502 return (size_t *)®s->R0 + regNum;
503 #elif defined(_TARGET_ARM64_)
504 _ASSERTE(regNum < 31);
505 return (size_t *)®s->X0 + regNum;
507 _ASSERTE(!"@TODO Port - getRegAddr (Regdisp.h)");
512 //---------------------------------------------------------------------------------------
514 // This is just a simpler helper function to convert a REGDISPLAY to a CONTEXT.
517 // pRegDisp - the REGDISPLAY to be converted
518 // pContext - the buffer for storing the converted CONTEXT
520 inline void UpdateContextFromRegDisp(PREGDISPLAY pRegDisp, PT_CONTEXT pContext)
522 _ASSERTE((pRegDisp != NULL) && (pContext != NULL));
524 #ifndef WIN64EXCEPTIONS
526 #if defined(_TARGET_X86_)
527 pContext->ContextFlags = (CONTEXT_INTEGER | CONTEXT_CONTROL);
528 pContext->Edi = *pRegDisp->pEdi;
529 pContext->Esi = *pRegDisp->pEsi;
530 pContext->Ebx = *pRegDisp->pEbx;
531 pContext->Ebp = *pRegDisp->pEbp;
532 pContext->Eax = *pRegDisp->pEax;
533 pContext->Ecx = *pRegDisp->pEcx;
534 pContext->Edx = *pRegDisp->pEdx;
535 pContext->Esp = pRegDisp->SP;
536 pContext->Eip = pRegDisp->ControlPC;
537 #else // _TARGET_X86_
538 PORTABILITY_ASSERT("UpdateContextFromRegDisp");
539 #endif // _TARGET_???_
541 #else // WIN64EXCEPTIONS
543 *pContext = *pRegDisp->pCurrentContext;
545 #endif // WIN64EXCEPTIONS
549 #endif // __REGDISP_H