Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LoongArch64] fix the compiling errors within the CLFS linux #89422

Merged
merged 3 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions src/coreclr/debug/createdump/threadinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ class CrashInfo;

#if defined(__loongarch64)
// See src/coreclr/pal/src/include/pal/context.h
#define MCREG_Ra(mc) ((mc).gpr[1])
#define MCREG_Fp(mc) ((mc).gpr[22])
#define MCREG_Sp(mc) ((mc).gpr[3])
#define MCREG_Pc(mc) ((mc).pc)
#define MCREG_Ra(mc) ((mc).regs[1])
#define MCREG_Fp(mc) ((mc).regs[22])
#define MCREG_Sp(mc) ((mc).regs[3])
#define MCREG_Pc(mc) ((mc).csr_era)
#endif

#if defined(__riscv)
Expand All @@ -35,14 +35,6 @@ class CrashInfo;
#if defined(__arm__)
#define user_regs_struct user_regs
#define user_fpregs_struct user_fpregs
#elif defined(__loongarch64)
// struct user_regs_struct {} defined `/usr/include/loongarch64-linux-gnu/sys/user.h`

struct user_fpregs_struct
{
unsigned long long fpregs[32];
unsigned long fpscr;
} __attribute__((__packed__));
#elif defined(__riscv)
struct user_fpregs_struct
{
Expand All @@ -63,6 +55,10 @@ struct user_vfpregs_struct
} __attribute__((__packed__));
#endif

#if defined(__loongarch64)
#define user_fpregs_struct user_fp_struct
#endif

#define STACK_OVERFLOW_EXCEPTION 0x800703e9

class ThreadInfo
Expand Down
15 changes: 8 additions & 7 deletions src/coreclr/debug/createdump/threadinfounix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ ThreadInfo::Initialize()
#elif defined(__x86_64__)
TRACE("Thread %04x RIP %016llx RSP %016llx\n", m_tid, (unsigned long long)m_gpRegisters.rip, (unsigned long long)m_gpRegisters.rsp);
#elif defined(__loongarch64)
TRACE("Thread %04x PC %016llx SP %016llx\n", m_tid, (unsigned long long)m_gpRegisters.pc, (unsigned long long)m_gpRegisters.gpr[3]);
TRACE("Thread %04x PC %016llx SP %016llx\n", m_tid, (unsigned long long)m_gpRegisters.csr_era, (unsigned long long)m_gpRegisters.regs[3]);
#elif defined(__riscv)
TRACE("Thread %04x PC %016llx SP %016llx\n", m_tid, (unsigned long long)m_gpRegisters.pc, (unsigned long long)m_gpRegisters.sp);
#else
Expand Down Expand Up @@ -235,15 +235,16 @@ ThreadInfo::GetThreadContext(uint32_t flags, CONTEXT* context) const
}
if ((flags & CONTEXT_INTEGER) == CONTEXT_INTEGER)
{
context->Tp = m_gpRegisters.gpr[2];
memcpy(&context->A0, &m_gpRegisters.gpr[4], sizeof(context->A0)*(21 - 4 + 1));
memcpy(&context->S0, &m_gpRegisters.gpr[23], sizeof(context->S0)*9);
context->Tp = m_gpRegisters.regs[2];
memcpy(&context->A0, &m_gpRegisters.regs[4], sizeof(context->A0)*(21 - 4 + 1));
memcpy(&context->S0, &m_gpRegisters.regs[23], sizeof(context->S0)*9);
}
if ((flags & CONTEXT_FLOATING_POINT) == CONTEXT_FLOATING_POINT)
{
assert(sizeof(context->F) == sizeof(m_fpRegisters.fpregs));
memcpy(context->F, m_fpRegisters.fpregs, sizeof(context->F));
context->Fcsr = m_fpRegisters.fpscr;
assert(sizeof(context->F) == sizeof(m_fpRegisters.fpr));
memcpy(context->F, m_fpRegisters.fpr, sizeof(context->F));
context->Fcsr = m_fpRegisters.fcsr;
context->Fcc = m_fpRegisters.fcc;
}
#elif defined(__riscv)
assert(!"TODO RISCV64 NYI");
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/debug/ee/loongarch64/dbghelpers.S
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ NESTED_ENTRY FuncEvalHijack, _TEXT, UnhandledExceptionHandlerUnix
bl C_FUNC(FuncEvalHijackWorker)

EPILOG_STACK_FREE 32
EPILOG_BRANCH_REG $v0
EPILOG_BRANCH_REG $a0
NESTED_END FuncEvalHijack

// This is the general purpose hijacking stub. The DacDbi Hijack primitive will
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/debug/inc/dbgtargetcontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@ typedef struct DECLSPEC_ALIGN(16) {
// Floating Point Registers
//
ULONGLONG F[32];
DWORD64 Fcc;
DWORD Fcsr;
} DT_CONTEXT;

Expand Down
1 change: 1 addition & 0 deletions src/coreclr/debug/shared/loongarch64/primitives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ void CORDbgCopyThreadContext(DT_CONTEXT* pDst, const DT_CONTEXT* pSrc)
CopyContextChunk(&pDst->F[0], &pSrc->F[0], &pDst->F[32],
DT_CONTEXT_FLOATING_POINT);
pDst->Fcsr = pSrc->Fcsr;
pDst->Fcc = pSrc->Fcc;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/gc/gc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6228,7 +6228,7 @@ extern "C" uint64_t __rdtsc();
{
////FIXME: TODO for LOONGARCH64:
//ptrdiff_t cycle;
__asm__ volatile ("break \n");
__asm__ volatile ("break 0 \n");
return 0;
}
#else
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/inc/crosscomp.h
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ typedef struct DECLSPEC_ALIGN(16) _T_CONTEXT {
//
//TODO-LoongArch64: support the SIMD.
ULONGLONG F[32];
DWORD64 Fcc;
DWORD Fcsr;
} T_CONTEXT, *PT_CONTEXT;

Expand Down
1 change: 1 addition & 0 deletions src/coreclr/pal/inc/pal.h
Original file line number Diff line number Diff line change
Expand Up @@ -2175,6 +2175,7 @@ typedef struct DECLSPEC_ALIGN(16) _CONTEXT {
//
// TODO-LoongArch64: support the SIMD.
ULONGLONG F[32];
DWORD64 Fcc;
DWORD Fcsr;
} CONTEXT, *PCONTEXT, *LPCONTEXT;

Expand Down
3 changes: 1 addition & 2 deletions src/coreclr/pal/inc/unixasmmacrosloongarch64.inc
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,11 @@ C_FUNC(\Name\()_End):
.endm

.macro EMIT_BREAKPOINT
break
break 0
.endm

.macro EPILOG_BRANCH Target
b \Target
//break
.endm

.macro EPILOG_BRANCH_REG reg
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/pal/src/include/pal/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ bool Xstate_IsAvx512Supported();
#define FPREG_MxCsr(uc) (((struct fxsave*)(&(uc)->uc_mcontext.__fpregs))->fx_mxcsr)
#define FPREG_MxCsr_Mask(uc) (((struct fxsave*)(&(uc)->uc_mcontext.__fpregs))->fx_mxcsr_mask)

#endif // HOST_LOONGARCH64
#endif // !HOST_LOONGARCH64 && !HOST_RISCV64

#else // HOST_64BIT

Expand Down
26 changes: 16 additions & 10 deletions src/coreclr/pal/src/thread/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -734,11 +734,14 @@ void CONTEXTToNativeContext(CONST CONTEXT *lpContext, native_context_t *native)
static_assert_no_msg(sizeof(fp->fprs) == sizeof(lpContext->Fpr));
memcpy(fp->fprs, lpContext->Fpr, sizeof(lpContext->Fpr));
#elif defined(HOST_LOONGARCH64)
native->uc_mcontext.__fcsr = lpContext->Fcsr;
for (int i = 0; i < 32; i++)
{
native->uc_mcontext.__fpregs[i].__val64[0] = lpContext->F[i];
}
struct sctx_info* info = (struct sctx_info*) native->uc_mcontext.__extcontext;
// TODO-LoongArch: supports SIMD128 and SIMD256.
_ASSERTE(FPU_CTX_MAGIC == info->magic);

struct fpu_context* fpr = (struct fpu_context*) ++info;
fpr->fcsr = lpContext->Fcsr;
fpr->fcc = lpContext->Fcc;
memcpy(fpr->regs, lpContext->F, sizeof(lpContext->F));
#elif defined(HOST_RISCV64)
native->uc_mcontext.__fpregs.__d.__fcsr = lpContext->Fcsr;
for (int i = 0; i < 32; i++)
Expand Down Expand Up @@ -928,11 +931,14 @@ void CONTEXTFromNativeContext(const native_context_t *native, LPCONTEXT lpContex
static_assert_no_msg(sizeof(fp->fprs) == sizeof(lpContext->Fpr));
memcpy(lpContext->Fpr, fp->fprs, sizeof(lpContext->Fpr));
#elif defined(HOST_LOONGARCH64)
lpContext->Fcsr = native->uc_mcontext.__fcsr;
for (int i = 0; i < 32; i++)
{
lpContext->F[i] = native->uc_mcontext.__fpregs[i].__val64[0];
}
struct sctx_info* info = (struct sctx_info*) native->uc_mcontext.__extcontext;
// TODO-LoongArch: supports SIMD128 and SIMD256.
_ASSERTE(FPU_CTX_MAGIC == info->magic);

struct fpu_context* fpr = (struct fpu_context*) ++info;
lpContext->Fcsr = fpr->fcsr;
lpContext->Fcc = fpr->fcc;
memcpy(lpContext->F, fpr->regs, sizeof(lpContext->F));
#elif defined(HOST_RISCV64)
lpContext->Fcsr = native->uc_mcontext.__fpregs.__d.__fcsr;
for (int i = 0; i < 32; i++)
Expand Down
8 changes: 8 additions & 0 deletions src/coreclr/unwinder/loongarch64/unwinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ typedef struct _LOONGARCH64_VFP_STATE
{
struct _LOONGARCH64_VFP_STATE *Link; // link to next state entry
ULONG Fcsr; // FCSR register
ULONG64 Fcc; // Fcc flags.
ULONG64 F[32]; // All F registers (0-31)
} LOONGARCH64_VFP_STATE, *PLOONGARCH64_VFP_STATE, KLOONGARCH64_VFP_STATE, *PKLOONGARCH64_VFP_STATE;

Expand Down Expand Up @@ -184,6 +185,7 @@ Return Value:

{
ULONG Fcsr;
ULONG Fcc;
ULONG RegIndex;
ULONG_PTR SourceAddress;
ULONG_PTR StartingSp;
Expand Down Expand Up @@ -238,6 +240,9 @@ Return Value:
if (Fcsr != (ULONG)-1) {

ContextRecord->Fcsr = Fcsr;
SourceAddress = VfpStateAddress + offsetof(KLOONGARCH64_VFP_STATE, Fcc);
Fcc = MEMORY_READ_DWORD(UnwindParams, SourceAddress);
ContextRecord->Fcc = Fcc;

SourceAddress = VfpStateAddress + offsetof(KLOONGARCH64_VFP_STATE, F);
for (RegIndex = 0; RegIndex < 32; RegIndex++) {
Expand Down Expand Up @@ -328,6 +333,9 @@ Return Value:
SourceAddress = StartingSp + offsetof(T_CONTEXT, Fcsr);
ContextRecord->Fcsr = MEMORY_READ_DWORD(UnwindParams, SourceAddress);

SourceAddress = StartingSp + offsetof(T_CONTEXT, Fcc);
ContextRecord->Fcc = MEMORY_READ_DWORD(UnwindParams, SourceAddress);

//
// Inherit the unwound-to-call flag from this context
//
Expand Down
18 changes: 9 additions & 9 deletions src/coreclr/vm/loongarch64/asmhelpers.S
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ LOCAL_LABEL(Done):
// Its imperative that the return value of HelperMethodFrameRestoreState is zero
// as it is used in the state machine to loop until it becomes zero.
// Refer to HELPER_METHOD_FRAME_END macro for details.
ori $v0, $zero, 0
ori $a0, $zero, 0
jirl $r0, $ra, 0
LEAF_END HelperMethodFrameRestoreState, _TEXT

Expand Down Expand Up @@ -508,7 +508,7 @@ NESTED_ENTRY VirtualMethodFixupStub, _TEXT, NoHandler
bl VirtualMethodFixupWorker
ori $t4,$a0,0

// On return, v0 contains the target to tailcall to
// On return, a0 contains the target to tailcall to

// pop the stack and restore original register state
RESTORE_FLOAT_ARGUMENT_REGISTERS $sp, 96
Expand Down Expand Up @@ -608,7 +608,7 @@ LEAF_ENTRY JIT_GetSharedGCStaticBase_SingleAppDomain, _TEXT
andi $t8, $a2, 1
beq $t8, $zero, 1f //LOCAL_LABEL(JIT_GetSharedGCStaticBase_SingleAppDomain_CallHelper)

ld.d $v0, $a0, DomainLocalModule__m_pGCStatics
ld.d $a0, $a0, DomainLocalModule__m_pGCStatics
jirl $r0, $ra, 0

1:
Expand Down Expand Up @@ -771,8 +771,8 @@ NESTED_ENTRY OnHijackTripThread, _TEXT, NoHandler
PROLOG_SAVE_REG 31, 80

// save any integral return value(s)
st.d $v0, $sp, 96
st.d $v1, $sp, 104
st.d $a0, $sp, 96
st.d $a1, $sp, 104

// save any FP/HFA return value(s)
fst.d $f0, $sp, 112
Expand All @@ -784,8 +784,8 @@ NESTED_ENTRY OnHijackTripThread, _TEXT, NoHandler
// restore callee saved registers

// restore any integral return value(s)
ld.d $v0, $sp, 96
ld.d $v1, $sp, 104
ld.d $a0, $sp, 96
ld.d $a1, $sp, 104

// restore any FP/HFA return value(s)
fst.d $f0, $sp, 112
Expand Down Expand Up @@ -987,9 +987,9 @@ DelayLoad_Helper\suffix:
addi.d $a0, $sp, __PWTB_TransitionBlock // pTransitionBlock
bl DynamicHelperWorker

bne $v0, $r0, LOCAL_LABEL(FakeProlog\suffix\()_0)
bne $a0, $r0, LOCAL_LABEL(FakeProlog\suffix\()_0)

ld.d $v0, $sp, __PWTB_ArgumentRegisters
ld.d $a0, $sp, __PWTB_ArgumentRegisters
EPILOG_WITH_TRANSITION_BLOCK_RETURN
LOCAL_LABEL(FakeProlog\suffix\()_0):
ori $t4,$a0,0
Expand Down
10 changes: 5 additions & 5 deletions src/coreclr/vm/loongarch64/pinvokestubs.S
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,20 @@ NESTED_ENTRY JIT_PInvokeBegin, _TEXT, NoHandler
ld.d $t4, $sp, 0
st.d $t4, $s0, InlinedCallFrame__m_pCalleeSavedFP

// v0 = GetThread()
// a0 = GetThread()
bl GetThreadHelper

st.d $v0, $s0, InlinedCallFrame__m_pThread
st.d $a0, $s0, InlinedCallFrame__m_pThread

// pFrame->m_Next = pThread->m_pFrame;
ld.d $t4, $v0, Thread_m_pFrame
ld.d $t4, $a0, Thread_m_pFrame
st.d $t4, $s0, Frame__m_Next

// pThread->m_pFrame = pFrame;
st.d $s0, $v0, Thread_m_pFrame
st.d $s0, $a0, Thread_m_pFrame

// pThread->m_fPreemptiveGCDisabled = 0
st.w $zero, $v0, Thread_m_fPreemptiveGCDisabled
st.w $zero, $a0, Thread_m_fPreemptiveGCDisabled

EPILOG_RESTORE_REG 23, 16 //the stack slot at $sp+24 is empty for 16 byte alignment
EPILOG_RESTORE_REG_PAIR_INDEXED 22, 1, 32
Expand Down