Skip to content

Commit

Permalink
Modify JIT to support double mapped memory (dotnet#52600)
Browse files Browse the repository at this point in the history
* Modify JIT to support double mapped memory

This change modifies JIT so that it can generate code into
double mapped memory. The code is written into RW mapped memory,
but the relative offsets are computed using the related RX locations.

The change doesn't modify the runtime to provide double mapped memory
yet, the JIT2EE interface allocMem returns the same addresses as RW and
RX. The runtime changes will be part of follow-up PRs. However, it was
already tested with the double mapping enabled locally.

It also changes signature of allocMem to pass everything in a single structure
janvorli authored May 12, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 8eb7692 commit 4ca5d81
Showing 23 changed files with 337 additions and 320 deletions.
Original file line number Diff line number Diff line change
@@ -1929,21 +1929,12 @@ bool interceptor_ICJI::runWithErrorTrap(void (*function)(void*), void* param)
}

// get a block of memory for the code, readonly data, and read-write data
void interceptor_ICJI::allocMem(uint32_t hotCodeSize, /* IN */
uint32_t coldCodeSize, /* IN */
uint32_t roDataSize, /* IN */
uint32_t xcptnsCount, /* IN */
CorJitAllocMemFlag flag, /* IN */
void** hotCodeBlock, /* OUT */
void** coldCodeBlock, /* OUT */
void** roDataBlock /* OUT */
)
void interceptor_ICJI::allocMem(AllocMemArgs *pArgs)
{
mc->cr->AddCall("allocMem");
original_ICorJitInfo->allocMem(hotCodeSize, coldCodeSize, roDataSize, xcptnsCount, flag, hotCodeBlock,
coldCodeBlock, roDataBlock);
mc->cr->recAllocMem(hotCodeSize, coldCodeSize, roDataSize, xcptnsCount, flag, hotCodeBlock, coldCodeBlock,
roDataBlock);
original_ICorJitInfo->allocMem(pArgs);
mc->cr->recAllocMem(pArgs->hotCodeSize, pArgs->coldCodeSize, pArgs->roDataSize, pArgs->xcptnsCount, pArgs->flag, &pArgs->hotCodeBlock, &pArgs->coldCodeBlock,
&pArgs->roDataBlock);
}

// Reserve memory for the method/funclet's unwind information.
@@ -2101,14 +2092,15 @@ void interceptor_ICJI::recordCallSite(uint32_t instrOffset, /* IN *
// A relocation is recorded if we are pre-jitting.
// A jump thunk may be inserted if we are jitting
void interceptor_ICJI::recordRelocation(void* location, /* IN */
void* locationRW, /* IN */
void* target, /* IN */
uint16_t fRelocType, /* IN */
uint16_t slotNum, /* IN */
int32_t addlDelta /* IN */
)
{
mc->cr->AddCall("recordRelocation");
original_ICorJitInfo->recordRelocation(location, target, fRelocType, slotNum, addlDelta);
original_ICorJitInfo->recordRelocation(location, locationRW, target, fRelocType, slotNum, addlDelta);
mc->cr->recRecordRelocation(location, target, fRelocType, slotNum, addlDelta);
}

14 changes: 4 additions & 10 deletions src/coreclr/ToolBox/superpmi/superpmi-shim-counter/icorjitinfo.cpp
Original file line number Diff line number Diff line change
@@ -1250,17 +1250,10 @@ bool interceptor_ICJI::notifyInstructionSetUsage(
}

void interceptor_ICJI::allocMem(
uint32_t hotCodeSize,
uint32_t coldCodeSize,
uint32_t roDataSize,
uint32_t xcptnsCount,
CorJitAllocMemFlag flag,
void** hotCodeBlock,
void** coldCodeBlock,
void** roDataBlock)
AllocMemArgs* pArgs)
{
mcs->AddCall("allocMem");
original_ICorJitInfo->allocMem(hotCodeSize, coldCodeSize, roDataSize, xcptnsCount, flag, hotCodeBlock, coldCodeBlock, roDataBlock);
original_ICorJitInfo->allocMem(pArgs);
}

void interceptor_ICJI::reserveUnwindInfo(
@@ -1363,13 +1356,14 @@ void interceptor_ICJI::recordCallSite(

void interceptor_ICJI::recordRelocation(
void* location,
void* locationRW,
void* target,
uint16_t fRelocType,
uint16_t slotNum,
int32_t addlDelta)
{
mcs->AddCall("recordRelocation");
original_ICorJitInfo->recordRelocation(location, target, fRelocType, slotNum, addlDelta);
original_ICorJitInfo->recordRelocation(location, locationRW, target, fRelocType, slotNum, addlDelta);
}

uint16_t interceptor_ICJI::getRelocTypeHint(
14 changes: 4 additions & 10 deletions src/coreclr/ToolBox/superpmi/superpmi-shim-simple/icorjitinfo.cpp
Original file line number Diff line number Diff line change
@@ -1094,16 +1094,9 @@ bool interceptor_ICJI::notifyInstructionSetUsage(
}

void interceptor_ICJI::allocMem(
uint32_t hotCodeSize,
uint32_t coldCodeSize,
uint32_t roDataSize,
uint32_t xcptnsCount,
CorJitAllocMemFlag flag,
void** hotCodeBlock,
void** coldCodeBlock,
void** roDataBlock)
AllocMemArgs* pArgs)
{
original_ICorJitInfo->allocMem(hotCodeSize, coldCodeSize, roDataSize, xcptnsCount, flag, hotCodeBlock, coldCodeBlock, roDataBlock);
original_ICorJitInfo->allocMem(pArgs);
}

void interceptor_ICJI::reserveUnwindInfo(
@@ -1195,12 +1188,13 @@ void interceptor_ICJI::recordCallSite(

void interceptor_ICJI::recordRelocation(
void* location,
void* locationRW,
void* target,
uint16_t fRelocType,
uint16_t slotNum,
int32_t addlDelta)
{
original_ICorJitInfo->recordRelocation(location, target, fRelocType, slotNum, addlDelta);
original_ICorJitInfo->recordRelocation(location, locationRW, target, fRelocType, slotNum, addlDelta);
}

uint16_t interceptor_ICJI::getRelocTypeHint(
51 changes: 24 additions & 27 deletions src/coreclr/ToolBox/superpmi/superpmi/icorjitinfo.cpp
Original file line number Diff line number Diff line change
@@ -1601,54 +1601,46 @@ static void* ALIGN_UP_SPMI(void* val, size_t alignment)
}

// get a block of memory for the code, readonly data, and read-write data
void MyICJI::allocMem(uint32_t hotCodeSize, /* IN */
uint32_t coldCodeSize, /* IN */
uint32_t roDataSize, /* IN */
uint32_t xcptnsCount, /* IN */
CorJitAllocMemFlag flag, /* IN */
void** hotCodeBlock, /* OUT */
void** coldCodeBlock, /* OUT */
void** roDataBlock /* OUT */
)
void MyICJI::allocMem(AllocMemArgs* pArgs)
{
jitInstance->mc->cr->AddCall("allocMem");

// TODO-Cleanup: Could hot block size be ever 0?
size_t codeAlignment = sizeof(void*);
size_t hotCodeAlignedSize = static_cast<size_t>(hotCodeSize);
size_t hotCodeAlignedSize = static_cast<size_t>(pArgs->hotCodeSize);

if ((flag & CORJIT_ALLOCMEM_FLG_32BYTE_ALIGN) != 0)
if ((pArgs->flag & CORJIT_ALLOCMEM_FLG_32BYTE_ALIGN) != 0)
{
codeAlignment = 32;
}
else if ((flag & CORJIT_ALLOCMEM_FLG_16BYTE_ALIGN) != 0)
else if ((pArgs->flag & CORJIT_ALLOCMEM_FLG_16BYTE_ALIGN) != 0)
{
codeAlignment = 16;
}
hotCodeAlignedSize = ALIGN_UP_SPMI(hotCodeAlignedSize, codeAlignment);
hotCodeAlignedSize = hotCodeAlignedSize + (codeAlignment - sizeof(void*));
*hotCodeBlock = jitInstance->mc->cr->allocateMemory(hotCodeAlignedSize);
*hotCodeBlock = ALIGN_UP_SPMI(*hotCodeBlock, codeAlignment);
pArgs->hotCodeBlock = jitInstance->mc->cr->allocateMemory(hotCodeAlignedSize);
pArgs->hotCodeBlock = ALIGN_UP_SPMI(pArgs->hotCodeBlock, codeAlignment);

if (coldCodeSize > 0)
*coldCodeBlock = jitInstance->mc->cr->allocateMemory(coldCodeSize);
if (pArgs->coldCodeSize > 0)
pArgs->coldCodeBlock = jitInstance->mc->cr->allocateMemory(pArgs->coldCodeSize);
else
*coldCodeBlock = nullptr;
pArgs->coldCodeBlock = nullptr;

if (roDataSize > 0)
if (pArgs->roDataSize > 0)
{
size_t roDataAlignment = sizeof(void*);
size_t roDataAlignedSize = static_cast<size_t>(roDataSize);
size_t roDataAlignedSize = static_cast<size_t>(pArgs->roDataSize);

if ((flag & CORJIT_ALLOCMEM_FLG_RODATA_32BYTE_ALIGN) != 0)
if ((pArgs->flag & CORJIT_ALLOCMEM_FLG_RODATA_32BYTE_ALIGN) != 0)
{
roDataAlignment = 32;
}
else if ((flag & CORJIT_ALLOCMEM_FLG_RODATA_16BYTE_ALIGN) != 0)
else if ((pArgs->flag & CORJIT_ALLOCMEM_FLG_RODATA_16BYTE_ALIGN) != 0)
{
roDataAlignment = 16;
}
else if (roDataSize >= 8)
else if (pArgs->roDataSize >= 8)
{
roDataAlignment = 8;
}
@@ -1660,14 +1652,18 @@ void MyICJI::allocMem(uint32_t hotCodeSize, /* IN */

roDataAlignedSize = ALIGN_UP_SPMI(roDataAlignedSize, roDataAlignment);
roDataAlignedSize = roDataAlignedSize + (roDataAlignment - sizeof(void*));
*roDataBlock = jitInstance->mc->cr->allocateMemory(roDataAlignedSize);
*roDataBlock = ALIGN_UP_SPMI(*roDataBlock, roDataAlignment);
pArgs->roDataBlock = jitInstance->mc->cr->allocateMemory(roDataAlignedSize);
pArgs->roDataBlock = ALIGN_UP_SPMI(pArgs->roDataBlock, roDataAlignment);
}
else
*roDataBlock = nullptr;
pArgs->roDataBlock = nullptr;

jitInstance->mc->cr->recAllocMem(hotCodeSize, coldCodeSize, roDataSize, xcptnsCount, flag, hotCodeBlock,
coldCodeBlock, roDataBlock);
pArgs->hotCodeBlockRW = pArgs->hotCodeBlock;
pArgs->coldCodeBlockRW = pArgs->coldCodeBlock;
pArgs->roDataBlockRW = pArgs->roDataBlock;

jitInstance->mc->cr->recAllocMem(pArgs->hotCodeSize, pArgs->coldCodeSize, pArgs->roDataSize, pArgs->xcptnsCount, pArgs->flag, &pArgs->hotCodeBlock,
&pArgs->coldCodeBlock, &pArgs->roDataBlock);
}

// Reserve memory for the method/funclet's unwind information.
@@ -1841,6 +1837,7 @@ void MyICJI::recordCallSite(uint32_t instrOffset, /* IN */
// A relocation is recorded if we are pre-jitting.
// A jump thunk may be inserted if we are jitting
void MyICJI::recordRelocation(void* location, /* IN */
void* locationRW, /* IN */
void* target, /* IN */
uint16_t fRelocType, /* IN */
uint16_t slotNum, /* IN */
28 changes: 20 additions & 8 deletions src/coreclr/inc/corjit.h
Original file line number Diff line number Diff line change
@@ -142,6 +142,24 @@ enum CheckedWriteBarrierKinds {
CWBKind_AddrOfLocal, // Store through the address of a local (arguably a bug that this happens at all).
};

struct AllocMemArgs
{
// Input arguments
uint32_t hotCodeSize;
uint32_t coldCodeSize;
uint32_t roDataSize;
uint32_t xcptnsCount;
CorJitAllocMemFlag flag;

// Output arguments
void* hotCodeBlock;
void* hotCodeBlockRW;
void* coldCodeBlock;
void* coldCodeBlockRW;
void* roDataBlock;
void* roDataBlockRW;
};

#include "corjithost.h"

extern "C" void jitStartup(ICorJitHost* host);
@@ -212,14 +230,7 @@ class ICorJitInfo : public ICorDynamicInfo
public:
// get a block of memory for the code, readonly data, and read-write data
virtual void allocMem (
uint32_t hotCodeSize, /* IN */
uint32_t coldCodeSize, /* IN */
uint32_t roDataSize, /* IN */
uint32_t xcptnsCount, /* IN */
CorJitAllocMemFlag flag, /* IN */
void ** hotCodeBlock, /* OUT */
void ** coldCodeBlock, /* OUT */
void ** roDataBlock /* OUT */
AllocMemArgs *pArgs
) = 0;

// Reserve memory for the method/funclet's unwind information.
@@ -430,6 +441,7 @@ class ICorJitInfo : public ICorDynamicInfo
// A jump thunk may be inserted if we are jitting
virtual void recordRelocation(
void * location, /* IN */
void * locationRW, /* IN */
void * target, /* IN */
uint16_t fRelocType, /* IN */
uint16_t slotNum = 0, /* IN */
10 changes: 2 additions & 8 deletions src/coreclr/inc/icorjitinfoimpl_generated.h
Original file line number Diff line number Diff line change
@@ -635,14 +635,7 @@ bool notifyInstructionSetUsage(
bool supportEnabled) override;

void allocMem(
uint32_t hotCodeSize,
uint32_t coldCodeSize,
uint32_t roDataSize,
uint32_t xcptnsCount,
CorJitAllocMemFlag flag,
void** hotCodeBlock,
void** coldCodeBlock,
void** roDataBlock) override;
AllocMemArgs* pArgs) override;

void reserveUnwindInfo(
bool isFunclet,
@@ -700,6 +693,7 @@ void recordCallSite(

void recordRelocation(
void* location,
void* locationRW,
void* target,
uint16_t fRelocType,
uint16_t slotNum,
11 changes: 6 additions & 5 deletions src/coreclr/inc/jiteeversionguid.h
Original file line number Diff line number Diff line change
@@ -43,13 +43,14 @@ typedef const GUID *LPCGUID;
#define GUID_DEFINED
#endif // !GUID_DEFINED

constexpr GUID JITEEVersionIdentifier = { /* 12234eca-dfc2-48bc-a320-6155cf25ce17 */
0x12234eca,
0xdfc2,
0x48bc,
{0xa3, 0x20, 0x61, 0x55, 0xcf, 0x25, 0xce, 0x17}
constexpr GUID JITEEVersionIdentifier = { /* 529be99f-ce88-426e-a099-7528a691226c */
0x529be99f,
0xce88,
0x426e,
{ 0xa0, 0x99, 0x75, 0x28, 0xa6, 0x91, 0x22, 0x6c }
};


//////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// END JITEEVersionIdentifier
14 changes: 4 additions & 10 deletions src/coreclr/jit/ICorJitInfo_API_wrapper.hpp
Original file line number Diff line number Diff line change
@@ -1526,17 +1526,10 @@ bool WrapICorJitInfo::notifyInstructionSetUsage(
}

void WrapICorJitInfo::allocMem(
uint32_t hotCodeSize,
uint32_t coldCodeSize,
uint32_t roDataSize,
uint32_t xcptnsCount,
CorJitAllocMemFlag flag,
void** hotCodeBlock,
void** coldCodeBlock,
void** roDataBlock)
AllocMemArgs* pArgs)
{
API_ENTER(allocMem);
wrapHnd->allocMem(hotCodeSize, coldCodeSize, roDataSize, xcptnsCount, flag, hotCodeBlock, coldCodeBlock, roDataBlock);
wrapHnd->allocMem(pArgs);
API_LEAVE(allocMem);
}

@@ -1656,13 +1649,14 @@ void WrapICorJitInfo::recordCallSite(

void WrapICorJitInfo::recordRelocation(
void* location,
void* locationRW,
void* target,
uint16_t fRelocType,
uint16_t slotNum,
int32_t addlDelta)
{
API_ENTER(recordRelocation);
wrapHnd->recordRelocation(location, target, fRelocType, slotNum, addlDelta);
wrapHnd->recordRelocation(location, locationRW, target, fRelocType, slotNum, addlDelta);
API_LEAVE(recordRelocation);
}

Loading

0 comments on commit 4ca5d81

Please sign in to comment.