Merge pull request #9263 from chcosta/refactor_packaging
[platform/upstream/coreclr.git] / src / inc / regdisp.h
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.
4
5 #ifndef __REGDISP_H
6 #define __REGDISP_H
7
8
9 #ifdef DEBUG_REGDISPLAY
10 class Thread;
11 struct REGDISPLAY;
12 void CheckRegDisplaySP (REGDISPLAY *pRD);
13 #endif // DEBUG_REGDISPLAY
14
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
18                             // exception time.
19                             //
20                             // This will be used to resume execution, so
21                             // do NOT trash it! But DO update any static
22                             // registers here.
23
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
27
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;
32
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.
35
36     T_CONTEXT  ctxOne;    // used by stackwalk
37     T_CONTEXT  ctxTwo;    // used by stackwalk
38
39     T_KNONVOLATILE_CONTEXT_POINTERS ctxPtrsOne;  // used by stackwalk
40     T_KNONVOLATILE_CONTEXT_POINTERS ctxPtrsTwo;  // used by stackwalk
41 #endif // WIN64EXCEPTIONS
42
43 #ifdef DEBUG_REGDISPLAY
44     Thread *_pThread;
45 #endif // DEBUG_REGDISPLAY
46
47     TADDR SP;
48     TADDR ControlPC;
49 };
50
51 inline PCODE GetControlPC(REGDISPLAY_BASE *pRD) {
52     LIMITED_METHOD_DAC_CONTRACT;
53     return (PCODE)(pRD->ControlPC);
54 }
55
56 inline TADDR GetRegdisplaySP(REGDISPLAY_BASE *pRD) {
57     LIMITED_METHOD_DAC_CONTRACT;
58
59     return pRD->SP;
60 }
61
62 inline void SetRegdisplaySP(REGDISPLAY_BASE *pRD, LPVOID sp) {
63     LIMITED_METHOD_DAC_CONTRACT;
64
65     pRD->SP = (TADDR)sp;
66 }
67
68 #if defined(_TARGET_X86_)
69
70 struct REGDISPLAY : public REGDISPLAY_BASE {
71
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
77
78     DWORD * pEdi;
79     DWORD * pEsi;
80     DWORD * pEbx;
81     DWORD * pEdx;
82     DWORD * pEcx;
83     DWORD * pEax;
84
85     DWORD * pEbp;
86 #endif // !WIN64EXCEPTIONS
87
88 #ifndef WIN64EXCEPTIONS
89
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; }
93
94 #else // !WIN64EXCEPTIONS
95
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; }
99
100 #endif // WIN64EXCEPTIONS
101
102     REG_METHODS(Eax)
103     REG_METHODS(Ecx)
104     REG_METHODS(Edx)
105
106     REG_METHODS(Ebx)
107     REG_METHODS(Esi)
108     REG_METHODS(Edi)
109     REG_METHODS(Ebp)
110
111 #undef REG_METHODS
112
113     TADDR   PCTAddr;
114 };
115
116 inline TADDR GetRegdisplayFP(REGDISPLAY *display) {
117     LIMITED_METHOD_DAC_CONTRACT;
118
119     return (TADDR)*display->GetEbpLocation();
120 }
121
122 inline LPVOID GetRegdisplayFPAddress(REGDISPLAY *display) {
123     LIMITED_METHOD_CONTRACT;
124     
125     return (LPVOID)display->GetEbpLocation();
126 }
127
128
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;
132
133 #ifdef WIN64EXCEPTIONS
134     return stackPointer < ((LPVOID)(display->SP));
135 #else
136     return (TADDR)stackPointer < display->PCTAddr;
137 #endif
138 }
139 inline TADDR GetRegdisplayStackMark(REGDISPLAY *display) {
140     LIMITED_METHOD_DAC_CONTRACT;
141
142 #ifdef WIN64EXCEPTIONS
143     _ASSERTE(GetRegdisplaySP(display) == GetSP(display->pCurrentContext));
144     return GetRegdisplaySP(display);
145 #else
146     return display->PCTAddr;
147 #endif
148 }
149
150 #elif defined(_WIN64)
151
152 #if defined(_TARGET_ARM64_)
153 typedef struct _Arm64VolatileContextPointer
154 {
155     union {
156         struct {
157             PDWORD64 X0;
158             PDWORD64 X1;
159             PDWORD64 X2;
160             PDWORD64 X3;
161             PDWORD64 X4;
162             PDWORD64 X5;
163             PDWORD64 X6;
164             PDWORD64 X7;
165             PDWORD64 X8;
166             PDWORD64 X9;
167             PDWORD64 X10;
168             PDWORD64 X11;
169             PDWORD64 X12;
170             PDWORD64 X13;
171             PDWORD64 X14;
172             PDWORD64 X15;
173             PDWORD64 X16;
174             PDWORD64 X17;
175             //X18 is reserved by OS, in userspace it represents TEB
176         };
177         PDWORD64 X[18];
178     };
179 } Arm64VolatileContextPointer;
180 #endif //_TARGET_ARM64_
181 struct REGDISPLAY : public REGDISPLAY_BASE {
182 #ifdef _TARGET_ARM64_
183     Arm64VolatileContextPointer     volatileCurrContextPointers;
184 #endif
185
186     REGDISPLAY()
187     {
188         // Initialize
189         memset(this, 0, sizeof(REGDISPLAY));
190     }
191 };
192
193
194 inline TADDR GetRegdisplayFP(REGDISPLAY *display) {
195     LIMITED_METHOD_CONTRACT;
196     return NULL; 
197 }
198
199 inline TADDR GetRegdisplayFPAddress(REGDISPLAY *display) {
200     LIMITED_METHOD_CONTRACT;
201     return NULL; 
202 }
203
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)
206 {
207     LIMITED_METHOD_CONTRACT;
208     return stackPointer < ((LPVOID)(display->SP));
209 }
210
211 inline TADDR GetRegdisplayStackMark(REGDISPLAY *display)
212 {
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);
217
218 #elif defined(_TARGET_ARM64_)
219
220     _ASSERTE(display->IsCallerContextValid);
221     return GetSP(display->pCallerContext);
222
223 #else  // _TARGET_AMD64_
224     PORTABILITY_ASSERT("GetRegdisplayStackMark NYI for this platform (Regdisp.h)");
225     return NULL;
226 #endif // _TARGET_AMD64_
227 }
228
229 #elif defined(_TARGET_ARM_)
230
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
241 {
242     PDWORD R0;
243     PDWORD R1;
244     PDWORD R2;
245     PDWORD R3;
246     PDWORD R12;
247 } ArmVolatileContextPointer;
248
249 struct REGDISPLAY : public REGDISPLAY_BASE {
250     ArmVolatileContextPointer     volatileCurrContextPointers;
251
252     DWORD *  pPC;                // processor neutral name
253
254     REGDISPLAY()
255     {
256         // Initialize regdisplay
257         memset(this, 0, sizeof(REGDISPLAY));
258
259         // Setup the pointer to ControlPC field
260         pPC = &ControlPC;
261     }
262 };
263
264 // This function tells us if the given stack pointer is in one of the frames of the functions called by the given frame
265 inline BOOL IsInCalleesFrames(REGDISPLAY *display, LPVOID stackPointer) {
266     LIMITED_METHOD_CONTRACT;
267     return stackPointer < ((LPVOID)(TADDR)(display->SP));
268 }
269
270 inline TADDR GetRegdisplayStackMark(REGDISPLAY *display) {
271     LIMITED_METHOD_CONTRACT;
272     // ARM uses the establisher frame as the marker
273     _ASSERTE(display->IsCallerContextValid);
274     return GetSP(display->pCallerContext);
275 }
276
277 #else // none of the above processors
278
279 PORTABILITY_WARNING("RegDisplay functions are not implemented on this platform.")
280
281 struct REGDISPLAY : public REGDISPLAY_BASE {
282     size_t * FramePtr;
283     SLOT   * pPC;
284 };
285
286 inline TADDR GetRegdisplayFP(REGDISPLAY *display) {
287     LIMITED_METHOD_CONTRACT;
288     return (TADDR)*(display->FramePtr);
289 }
290
291 inline BOOL IsInCalleesFrames(REGDISPLAY *display, LPVOID stackPointer) {
292     LIMITED_METHOD_CONTRACT;
293     return FALSE;
294 }
295 inline LPVOID GetRegdisplayStackMark(REGDISPLAY *display) {
296     LIMITED_METHOD_CONTRACT;
297     return (LPVOID)display->SP;
298 }
299
300 #endif
301
302 #if defined(_WIN64) || defined(_TARGET_ARM_) || (defined(_TARGET_X86_) && defined(WIN64EXCEPTIONS))
303 // This needs to be implemented for platforms that have funclets.
304 inline LPVOID GetRegdisplayReturnValue(REGDISPLAY *display)
305 {
306     LIMITED_METHOD_CONTRACT;
307
308 #if defined(_TARGET_AMD64_)
309     return (LPVOID)display->pCurrentContext->Rax;
310 #elif defined(_TARGET_ARM64_)
311     return (LPVOID)display->pCurrentContext->X0;
312 #elif defined(_TARGET_ARM_)
313     return (LPVOID)display->pCurrentContext->R0;
314 #elif defined(_TARGET_X86_)
315     return (LPVOID)display->pCurrentContext->Eax;
316 #else
317     PORTABILITY_ASSERT("GetRegdisplayReturnValue NYI for this platform (Regdisp.h)");
318     return NULL;
319 #endif
320 }
321
322 inline void SyncRegDisplayToCurrentContext(REGDISPLAY* pRD)
323 {
324     LIMITED_METHOD_CONTRACT;
325
326 #if defined(_WIN64)
327     pRD->SP         = (INT_PTR)GetSP(pRD->pCurrentContext);
328     pRD->ControlPC  = INT_PTR(GetIP(pRD->pCurrentContext));
329 #elif defined(_TARGET_ARM_) // _WIN64
330     pRD->SP         = (DWORD)GetSP(pRD->pCurrentContext);
331     pRD->ControlPC  = (DWORD)GetIP(pRD->pCurrentContext);
332 #elif defined(_TARGET_X86_) // _TARGET_ARM_
333     pRD->SP         = (DWORD)GetSP(pRD->pCurrentContext);
334     pRD->ControlPC  = (DWORD)GetIP(pRD->pCurrentContext);
335 #else // _TARGET_X86_
336     PORTABILITY_ASSERT("SyncRegDisplayToCurrentContext");
337 #endif // _TARGET_ARM_ || _TARGET_X86_
338
339 #ifdef DEBUG_REGDISPLAY
340     CheckRegDisplaySP(pRD);
341 #endif // DEBUG_REGDISPLAY
342 }
343 #endif // _WIN64 || _TARGET_ARM_ || (_TARGET_X86_ && WIN64EXCEPTIONS)
344
345 typedef REGDISPLAY *PREGDISPLAY;
346
347
348 inline void FillRegDisplay(const PREGDISPLAY pRD, PT_CONTEXT pctx, PT_CONTEXT pCallerCtx = NULL)
349 {
350     WRAPPER_NO_CONTRACT;
351
352     SUPPORTS_DAC;
353
354 #ifndef WIN64EXCEPTIONS
355 #ifdef _TARGET_X86_
356     pRD->pContext = pctx;
357     pRD->pContextForUnwind = NULL;
358     pRD->pEdi = &(pctx->Edi);
359     pRD->pEsi = &(pctx->Esi);
360     pRD->pEbx = &(pctx->Ebx);
361     pRD->pEbp = &(pctx->Ebp);
362     pRD->pEax = &(pctx->Eax);
363     pRD->pEcx = &(pctx->Ecx);
364     pRD->pEdx = &(pctx->Edx);
365     pRD->SP   = pctx->Esp;
366     pRD->ControlPC = (PCODE)(pctx->Eip);
367     pRD->PCTAddr = (UINT_PTR)&(pctx->Eip);
368 #else // _TARGET_X86_
369     PORTABILITY_ASSERT("FillRegDisplay");
370 #endif // _TARGET_???_ (ELSE)
371
372 #else // !WIN64EXCEPTIONS
373     pRD->pContext   = pctx;
374
375     // Setup the references
376     pRD->pCurrentContextPointers = &pRD->ctxPtrsOne;
377     pRD->pCallerContextPointers = &pRD->ctxPtrsTwo;
378
379     pRD->pCurrentContext = &(pRD->ctxOne);
380     pRD->pCallerContext  = &(pRD->ctxTwo);
381
382     // copy the active context to initialize our stackwalk
383     *(pRD->pCurrentContext)     = *(pctx);
384
385     // copy the caller context as well if it's specified
386     if (pCallerCtx == NULL)
387     {
388         pRD->IsCallerContextValid = FALSE;
389         pRD->IsCallerSPValid      = FALSE;        // Don't add usage of this field.  This is only temporary.
390     }
391     else
392     {
393         *(pRD->pCallerContext)    = *(pCallerCtx);
394         pRD->IsCallerContextValid = TRUE;
395         pRD->IsCallerSPValid      = TRUE;        // Don't add usage of this field.  This is only temporary.
396     }
397
398 #ifdef _TARGET_AMD64_
399     for (int i = 0; i < 16; i++)
400     {
401         *(&pRD->ctxPtrsOne.Rax + i) = (&pctx->Rax + i);
402     }
403 #elif defined(_TARGET_ARM64_) // _TARGET_AMD64_
404     for (int i = 0; i < 12; i++)
405     {
406         *(&pRD->ctxPtrsOne.X19 + i) = (&pctx->X19 + i);
407     }
408 #elif defined(_TARGET_ARM_) // _TARGET_ARM64_
409     // Copy over the nonvolatile integer registers (R4-R11)
410     for (int i = 0; i < 8; i++)
411     {
412         *(&pRD->ctxPtrsOne.R4 + i) = (&pctx->R4 + i);
413     }
414
415     pRD->ctxPtrsOne.Lr = &pctx->Lr;
416     pRD->pPC = &pRD->pCurrentContext->Pc;
417 #elif defined(_TARGET_X86_) // _TARGET_ARM_
418     for (int i = 0; i < 7; i++)
419     {
420         *(&pRD->ctxPtrsOne.Esi + i) = (&pctx->Esi + i);
421     }
422 #else // _TARGET_X86_
423     PORTABILITY_ASSERT("FillRegDisplay");
424 #endif // _TARGET_???_ (ELSE)
425
426 #ifdef DEBUG_REGDISPLAY
427     pRD->_pThread = NULL;
428 #endif // DEBUG_REGDISPLAY
429
430     // This will setup the PC and SP
431     SyncRegDisplayToCurrentContext(pRD);
432 #endif // !WIN64EXCEPTIONS
433 }
434
435 // Initialize a new REGDISPLAY/CONTEXT pair from an existing valid REGDISPLAY.
436 inline void CopyRegDisplay(const PREGDISPLAY pInRD, PREGDISPLAY pOutRD, T_CONTEXT *pOutCtx)
437 {
438     WRAPPER_NO_CONTRACT;
439
440     // The general strategy is to extract the register state from the input REGDISPLAY 
441     // into the new CONTEXT then simply call FillRegDisplay.
442
443     T_CONTEXT* pOutCallerCtx = NULL;
444
445 #ifndef WIN64EXCEPTIONS
446
447 #if defined(_TARGET_X86_)
448     if (pInRD->pEdi != NULL) {pOutCtx->Edi = *pInRD->pEdi;} else {pInRD->pEdi = NULL;}
449     if (pInRD->pEsi != NULL) {pOutCtx->Esi = *pInRD->pEsi;} else {pInRD->pEsi = NULL;}
450     if (pInRD->pEbx != NULL) {pOutCtx->Ebx = *pInRD->pEbx;} else {pInRD->pEbx = NULL;}
451     if (pInRD->pEbp != NULL) {pOutCtx->Ebp = *pInRD->pEbp;} else {pInRD->pEbp = NULL;}
452     if (pInRD->pEax != NULL) {pOutCtx->Eax = *pInRD->pEax;} else {pInRD->pEax = NULL;}
453     if (pInRD->pEcx != NULL) {pOutCtx->Ecx = *pInRD->pEcx;} else {pInRD->pEcx = NULL;}
454     if (pInRD->pEdx != NULL) {pOutCtx->Edx = *pInRD->pEdx;} else {pInRD->pEdx = NULL;}
455     pOutCtx->Esp = pInRD->SP;
456     pOutCtx->Eip = pInRD->ControlPC;
457 #else // _TARGET_X86_
458     PORTABILITY_ASSERT("CopyRegDisplay");
459 #endif // _TARGET_???_
460
461 #else // WIN64EXCEPTIONS
462
463     *pOutCtx = *(pInRD->pCurrentContext);
464     if (pInRD->IsCallerContextValid)
465     {
466         pOutCallerCtx = pInRD->pCallerContext;
467     }
468
469 #endif // WIN64EXCEPTIONS
470
471     if (pOutRD)
472         FillRegDisplay(pOutRD, pOutCtx, pOutCallerCtx);
473 }
474
475 // Get address of a register in a CONTEXT given the reg number. For X86, 
476 // the reg number is the R/M number from ModR/M byte or base in SIB byte
477 inline size_t * getRegAddr (unsigned regNum, PTR_CONTEXT regs)
478 {
479 #ifdef _TARGET_X86_
480     _ASSERTE(regNum < 8);
481
482     static const SIZE_T OFFSET_OF_REGISTERS[] =
483     {
484         offsetof(CONTEXT, Eax),
485         offsetof(CONTEXT, Ecx),
486         offsetof(CONTEXT, Edx),
487         offsetof(CONTEXT, Ebx),
488         offsetof(CONTEXT, Esp),
489         offsetof(CONTEXT, Ebp),
490         offsetof(CONTEXT, Esi),
491         offsetof(CONTEXT, Edi),
492     };
493
494     return (PTR_size_t)(PTR_BYTE(regs) + OFFSET_OF_REGISTERS[regNum]);
495 #elif defined(_TARGET_AMD64_)
496     _ASSERTE(regNum < 16);
497     return &regs->Rax + regNum;
498 #elif defined(_TARGET_ARM_)
499         _ASSERTE(regNum < 16);
500         return (size_t *)&regs->R0 + regNum;
501 #elif defined(_TARGET_ARM64_)
502     _ASSERTE(regNum < 31);
503     return (size_t *)&regs->X0 + regNum;
504 #else
505     _ASSERTE(!"@TODO Port - getRegAddr (Regdisp.h)");
506 #endif
507     return(0);
508 }
509
510 //---------------------------------------------------------------------------------------
511 //
512 // This is just a simpler helper function to convert a REGDISPLAY to a CONTEXT.
513 //
514 // Arguments:
515 //    pRegDisp - the REGDISPLAY to be converted
516 //    pContext - the buffer for storing the converted CONTEXT
517 //
518 inline void UpdateContextFromRegDisp(PREGDISPLAY pRegDisp, PT_CONTEXT pContext)
519 {
520     _ASSERTE((pRegDisp != NULL) && (pContext != NULL));
521
522 #ifndef WIN64EXCEPTIONS
523
524 #if defined(_TARGET_X86_)
525     pContext->ContextFlags = (CONTEXT_INTEGER | CONTEXT_CONTROL);
526     pContext->Edi = *pRegDisp->pEdi;
527     pContext->Esi = *pRegDisp->pEsi;
528     pContext->Ebx = *pRegDisp->pEbx;
529     pContext->Ebp = *pRegDisp->pEbp;
530     pContext->Eax = *pRegDisp->pEax;
531     pContext->Ecx = *pRegDisp->pEcx;
532     pContext->Edx = *pRegDisp->pEdx;
533     pContext->Esp = pRegDisp->SP;
534     pContext->Eip = pRegDisp->ControlPC;
535 #else // _TARGET_X86_
536     PORTABILITY_ASSERT("UpdateContextFromRegDisp");
537 #endif // _TARGET_???_
538
539 #else // WIN64EXCEPTIONS
540
541     *pContext = *pRegDisp->pCurrentContext;
542
543 #endif // WIN64EXCEPTIONS
544 }
545
546
547 #endif  // __REGDISP_H
548
549