Skip to content

Commit e355697

Browse files
authored
[RISC-V] Fixing createdump for RISC-V (#93374)
* Fixing createdump for RISC-V * Fix for IMAGE_FILE_MACHINE_RISCV64
1 parent 0f2cf16 commit e355697

File tree

8 files changed

+64
-8
lines changed

8 files changed

+64
-8
lines changed

src/coreclr/debug/createdump/threadinfo.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ GetFrameLocation(CONTEXT* pContext, uint64_t* ip, uint64_t* sp)
2929
#elif defined(__arm__)
3030
*ip = pContext->Pc & ~THUMB_CODE;
3131
*sp = pContext->Sp;
32+
#elif defined(__riscv)
33+
*ip = pContext->Pc;
34+
*sp = pContext->Sp;
3235
#endif
3336
}
3437

src/coreclr/debug/createdump/threadinfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class CrashInfo;
3939
struct user_fpregs_struct
4040
{
4141
unsigned long long fpregs[32];
42-
unsigned long fpscr;
42+
unsigned long fcsr;
4343
} __attribute__((__packed__));
4444
#endif
4545

src/coreclr/debug/createdump/threadinfounix.cpp

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,51 @@ ThreadInfo::GetThreadContext(uint32_t flags, CONTEXT* context) const
247247
context->Fcc = m_fpRegisters.fcc;
248248
}
249249
#elif defined(__riscv)
250-
assert(!"TODO RISCV64 NYI");
250+
if ((flags & CONTEXT_CONTROL) == CONTEXT_CONTROL)
251+
{
252+
context->Ra = MCREG_Ra(m_gpRegisters);
253+
context->Sp = MCREG_Sp(m_gpRegisters);
254+
context->Fp = MCREG_Fp(m_gpRegisters);
255+
context->Pc = MCREG_Pc(m_gpRegisters);
256+
}
257+
258+
if (flags & CONTEXT_INTEGER)
259+
{
260+
context->Gp = m_gpRegisters.gp;
261+
context->Tp = m_gpRegisters.tp;
262+
context->T0 = m_gpRegisters.t0;
263+
context->T1 = m_gpRegisters.t1;
264+
context->T2 = m_gpRegisters.t2;
265+
context->S1 = m_gpRegisters.s1;
266+
context->A0 = m_gpRegisters.a0;
267+
context->A1 = m_gpRegisters.a1;
268+
context->A2 = m_gpRegisters.a2;
269+
context->A3 = m_gpRegisters.a3;
270+
context->A4 = m_gpRegisters.a4;
271+
context->A5 = m_gpRegisters.a5;
272+
context->A6 = m_gpRegisters.a6;
273+
context->A7 = m_gpRegisters.a7;
274+
context->S2 = m_gpRegisters.s2;
275+
context->S3 = m_gpRegisters.s3;
276+
context->S4 = m_gpRegisters.s4;
277+
context->S5 = m_gpRegisters.s5;
278+
context->S6 = m_gpRegisters.s6;
279+
context->S7 = m_gpRegisters.s7;
280+
context->S8 = m_gpRegisters.s8;
281+
context->S9 = m_gpRegisters.s9;
282+
context->S10 = m_gpRegisters.s10;
283+
context->S11 = m_gpRegisters.s11;
284+
context->T3 = m_gpRegisters.t3;
285+
context->T4 = m_gpRegisters.t4;
286+
context->T5 = m_gpRegisters.t5;
287+
context->T6 = m_gpRegisters.t6;
288+
}
289+
if (flags & CONTEXT_FLOATING_POINT)
290+
{
291+
assert(sizeof(context->F) == sizeof(m_fpRegisters.fpregs));
292+
memcpy(context->F, m_fpRegisters.fpregs, sizeof(context->F));
293+
context->Fcsr = m_fpRegisters.fcsr;
294+
}
251295
#else
252296
#error Platform not supported
253297
#endif

src/coreclr/debug/daccess/datatargetadapter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ DataTargetAdapter::GetPlatform(
131131
_ASSERTE_MSG(false, "Not supported platform.");
132132
return E_NOTIMPL;
133133

134+
case IMAGE_FILE_MACHINE_RISCV64:
135+
ulExpectedPointerSize = 8;
136+
platform = CORDB_PLATFORM_POSIX_RISCV64;
137+
break;
134138
#else // TARGET_UNIX
135139
case IMAGE_FILE_MACHINE_I386:
136140
ulExpectedPointerSize = 4;

src/coreclr/debug/dbgutil/elfreader.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,9 @@ ElfReader::PopulateForSymbolLookup(uint64_t baseAddress)
115115
// Enumerate program headers searching for the PT_DYNAMIC header, etc.
116116
if (!EnumerateProgramHeaders(
117117
baseAddress,
118-
#ifdef TARGET_LINUX_MUSL
119-
// On musl based platforms (Alpine), the below dynamic entries for hash,
118+
#if defined(TARGET_LINUX_MUSL) || defined(TARGET_RISCV64)
119+
// On musl based platforms (Alpine) and RISCV64 (VisionFive2 board),
120+
// the below dynamic entries for hash,
120121
// string table, etc. are RVAs instead of absolute address like on all
121122
// other Linux distros. Get the "loadbias" (basically the base address
122123
// of the module) and add to these RVAs.

src/coreclr/inc/clrnt.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
(MAKELCID(MAKELANGID(LANG_NEUTRAL, SUBLANG_CUSTOM_UNSPECIFIED), SORT_DEFAULT))
5656
#endif // !SUBLANG_CUSTOM_DEFAULT
5757

58+
#ifndef IMAGE_FILE_MACHINE_RISCV64
59+
#define IMAGE_FILE_MACHINE_RISCV64 0x5064 // RISCV64
60+
#endif // !IMAGE_FILE_MACHINE_RISCV64
61+
5862
#ifndef __out_xcount_opt
5963
#define __out_xcount_opt(var) __out
6064
#endif
@@ -1106,8 +1110,7 @@ RtlpGetFunctionEndAddress (
11061110
if ((FunctionLength & 3) != 0) {
11071111
FunctionLength = (FunctionLength >> 2) & 0x7ff;
11081112
} else {
1109-
memcpy(&FunctionLength, (void*)(ImageBase + FunctionLength), sizeof(UINT32));
1110-
FunctionLength &= 0x3ffff;
1113+
FunctionLength = *(PTR_ULONG64)(ImageBase + FunctionLength) & 0x3ffff;
11111114
}
11121115

11131116
return FunctionEntry->BeginAddress + 4 * FunctionLength;

src/coreclr/jit/jit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@
279279
#elif defined(TARGET_LOONGARCH64)
280280
#define IMAGE_FILE_MACHINE_TARGET IMAGE_FILE_MACHINE_LOONGARCH64 // 0x6264
281281
#elif defined(TARGET_RISCV64)
282-
#define IMAGE_FILE_MACHINE_TARGET IMAGE_FILE_MACHINE_RISCV64 // 0x5641
282+
#define IMAGE_FILE_MACHINE_TARGET IMAGE_FILE_MACHINE_RISCV64 // 0x5064
283283
#else
284284
#error Unsupported or unset target architecture
285285
#endif

src/coreclr/pal/src/exception/remote-unwind.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2175,6 +2175,7 @@ access_reg(unw_addr_space_t as, unw_regnum_t regnum, unw_word_t *valp, int write
21752175
case UNW_PPC64_NIP: *valp = (unw_word_t)winContext->Nip; break;
21762176
#elif defined(TARGET_RISCV64)
21772177
case UNW_RISCV_X1: *valp = (unw_word_t)winContext->Ra; break;
2178+
case UNW_RISCV_X2: *valp = (unw_word_t)winContext->Sp; break;
21782179
case UNW_RISCV_X3: *valp = (unw_word_t)winContext->Gp; break;
21792180
case UNW_RISCV_X4: *valp = (unw_word_t)winContext->Tp; break;
21802181
case UNW_RISCV_X8: *valp = (unw_word_t)winContext->Fp; break;
@@ -2189,7 +2190,7 @@ access_reg(unw_addr_space_t as, unw_regnum_t regnum, unw_word_t *valp, int write
21892190
case UNW_RISCV_X25: *valp = (unw_word_t)winContext->S9; break;
21902191
case UNW_RISCV_X26: *valp = (unw_word_t)winContext->S10; break;
21912192
case UNW_RISCV_X27: *valp = (unw_word_t)winContext->S11; break;
2192-
case UNW_RISCV_PC: *valp = (unw_word_t)winContext->Pc; break;
2193+
case UNW_RISCV_PC: *valp = (unw_word_t)winContext->Pc; break;
21932194
#else
21942195
#error unsupported architecture
21952196
#endif

0 commit comments

Comments
 (0)