Skip to content

Commit 796f439

Browse files
committed
feat: interpreter update
1 parent 97fbc29 commit 796f439

11 files changed

Lines changed: 247 additions & 107 deletions

helper_scripts/generate_EmulatorConstants.lua

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,36 @@ local out = io.stdout
1818

1919

2020
out:write(' uint64 constant UARCH_CYCLE_ADDRESS = 0x' .. hex(cartesi.machine:get_reg_address("uarch_cycle")) .. ';\n')
21+
out:write(' uint64 constant UARCH_CYCLE_MAX = 0x' .. hex(cartesi.UARCH_CYCLE_MAX) .. ';\n')
2122
out:write(' uint64 constant UARCH_HALT_FLAG_ADDRESS = 0x' ..
2223
hex(cartesi.machine:get_reg_address("uarch_halt_flag")) .. ';\n')
2324
out:write(' uint64 constant UARCH_PC_ADDRESS = 0x' .. hex(cartesi.machine:get_reg_address("uarch_pc")) .. ';\n')
2425
out:write(' uint64 constant UARCH_X0_ADDRESS = 0x' .. hex(cartesi.machine:get_reg_address("uarch_x0")) .. ';\n')
2526
out:write(' uint64 constant UARCH_SHADOW_START_ADDRESS = 0x' .. hex(cartesi.UARCH_SHADOW_START_ADDRESS) .. ';\n')
2627
out:write(' uint64 constant UARCH_SHADOW_LENGTH = 0x' .. hex(cartesi.UARCH_SHADOW_LENGTH) .. ';\n')
28+
out:write(' uint64 constant AR_SHADOW_TLB_START = 0x' .. hex(cartesi.AR_SHADOW_TLB_START) .. ';\n')
29+
out:write(' uint64 constant AR_SHADOW_TLB_LENGTH = 0x' .. hex(cartesi.AR_SHADOW_TLB_LENGTH) .. ';\n')
2730
out:write(' uint64 constant UARCH_RAM_START_ADDRESS = 0x' .. hex(cartesi.UARCH_RAM_START_ADDRESS) .. ';\n')
2831
out:write(' uint64 constant UARCH_RAM_LENGTH = 0x' .. hex(cartesi.UARCH_RAM_LENGTH) .. ';\n')
2932
out:write(' uint64 constant UARCH_STATE_START_ADDRESS = 0x' .. hex(cartesi.UARCH_STATE_START_ADDRESS) .. ';\n')
3033
out:write(' uint8 constant UARCH_STATE_LOG2_SIZE = ' .. cartesi.UARCH_STATE_LOG2_SIZE .. ';\n')
3134
out:write(' bytes32 constant UARCH_PRISTINE_STATE_HASH = 0x' .. hexstring(cartesi.UARCH_PRISTINE_STATE_HASH) .. ';\n')
3235
out:write(' uint64 constant UARCH_ECALL_FN_HALT = ' .. cartesi.UARCH_ECALL_FN_HALT .. ';\n')
3336
out:write(' uint64 constant UARCH_ECALL_FN_PUTCHAR = ' .. cartesi.UARCH_ECALL_FN_PUTCHAR .. ';\n')
37+
out:write(' uint64 constant UARCH_ECALL_FN_MARK_DIRTY_PAGE = ' .. cartesi.UARCH_ECALL_FN_MARK_DIRTY_PAGE .. ';\n')
38+
out:write(' uint64 constant UARCH_ECALL_FN_WRITE_TLB = ' .. cartesi.UARCH_ECALL_FN_WRITE_TLB .. ';\n')
3439
out:write(' uint64 constant HTIF_YIELD = 0x' .. hex(cartesi.machine:get_reg_address("htif_iyield")) .. ';\n')
3540
out:write(' uint64 constant IFLAGS_Y_ADDRESS = 0x' .. hex(cartesi.machine:get_reg_address("iflags_Y")) .. ';\n')
3641
out:write(' uint64 constant HTIF_FROMHOST_ADDRESS = 0x' ..
3742
hex(cartesi.machine:get_reg_address("htif_fromhost")) .. ';\n')
3843
out:write(' uint8 constant CMIO_YIELD_REASON_ADVANCE_STATE = 0x' ..
3944
hex(cartesi.CMIO_YIELD_REASON_ADVANCE_STATE) .. ';\n')
40-
out:write(' uint32 constant TREE_LOG2_WORD_SIZE = 0x' .. hex(cartesi.TREE_LOG2_WORD_SIZE) .. ';\n')
41-
out:write(' uint32 constant TREE_WORD_SIZE = uint32(1) << TREE_LOG2_WORD_SIZE;\n')
42-
out:write(' uint64 constant PMA_CMIO_RX_BUFFER_START = 0x' .. hex(cartesi.PMA_CMIO_RX_BUFFER_START) .. ';\n')
43-
out:write(' uint8 constant PMA_CMIO_RX_BUFFER_LOG2_SIZE = 0x' .. hex(cartesi.PMA_CMIO_RX_BUFFER_LOG2_SIZE) .. ';\n')
44-
out:write(' uint64 constant PMA_CMIO_TX_BUFFER_START = 0x' .. hex(cartesi.PMA_CMIO_TX_BUFFER_START) .. ';\n')
45-
out:write(' uint8 constant PMA_CMIO_TX_BUFFER_LOG2_SIZE = 0x' .. hex(cartesi.PMA_CMIO_TX_BUFFER_LOG2_SIZE) .. ';\n')
45+
out:write(' uint32 constant HASH_TREE_LOG2_WORD_SIZE = 0x' .. hex(cartesi.HASH_TREE_LOG2_WORD_SIZE) .. ';\n')
46+
out:write(' uint32 constant HASH_TREE_WORD_SIZE = uint32(1) << HASH_TREE_LOG2_WORD_SIZE;\n')
47+
out:write(' uint64 constant AR_CMIO_RX_BUFFER_START = 0x' .. hex(cartesi.AR_CMIO_RX_BUFFER_START) .. ';\n')
48+
out:write(' uint8 constant AR_CMIO_RX_BUFFER_LOG2_SIZE = 0x' .. hex(cartesi.AR_CMIO_RX_BUFFER_LOG2_SIZE) .. ';\n')
49+
out:write(' uint64 constant AR_CMIO_TX_BUFFER_START = 0x' .. hex(cartesi.AR_CMIO_TX_BUFFER_START) .. ';\n')
50+
out:write(' uint8 constant AR_CMIO_TX_BUFFER_LOG2_SIZE = 0x' .. hex(cartesi.AR_CMIO_TX_BUFFER_LOG2_SIZE) .. ';\n')
4651
out:close()
4752

4853
out:close()

helper_scripts/generate_SendCmioResponse.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ cpp_src=`echo "${BASH_REMATCH[1]}" \
4040
| $SED "/ uint32 length);/d" \
4141
| $SED "s/machine_merkle_tree::get_log2_word_size()/TREE_LOG2_WORD_SIZE/g" \
4242
| $SED -E "s/($COMPAT_FNS)/EmulatorCompat.\1/g" \
43-
| $SED "s/writeMemoryWithPadding(a, PMA_CMIO_RX_BUFFER_START, data, dataLength, writeLengthLog2Size);/a.writeRegion(Memory.regionFromPhysicalAddress(PMA_CMIO_RX_BUFFER_START.toPhysicalAddress(),Memory.alignedSizeFromLog2(uint8(writeLengthLog2Size - TREE_LOG2_WORD_SIZE))),dataHash);"/g \
43+
| $SED "s/writeMemoryWithPadding(a, AR_CMIO_RX_BUFFER_START, data, dataLength, writeLengthLog2Size);/a.writeRegion(Memory.regionFromPhysicalAddress(AR_CMIO_RX_BUFFER_START.toPhysicalAddress(),Memory.alignedSizeFromLog2(uint8(writeLengthLog2Size - HASH_TREE_LOG2_WORD_SIZE))),dataHash);"/g \
4444
| $SED -E "s/($CONSTANTS)([^a-zA-Z])/EmulatorConstants.\1\2/g" \
4545
| $SED "s/void send_cmio_response(STATE_ACCESS a, uint16 reason, bytes data, uint32 dataLength) {/function sendCmioResponse(AccessLogs.Context memory a, uint16 reason, bytes32 dataHash, uint32 dataLength) internal pure {/" \
4646
| $SED "s/const uint64/uint64/g" \

helper_scripts/generate_UArchStep.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ pattern="namespace cartesi \{(.*)\}"
4545
# replace cpp specific syntaxes with solidity ones
4646
cpp_src=`echo "${BASH_REMATCH[1]}" \
4747
| $SED "/template/d" \
48-
| $SED "/note = a.make_scoped_note/d" \
48+
| $SED "/[[maybe_unused]]/d" \
4949
| $SED "/(void) note/d" \
5050
| $SED "s/constexpr//g" \
51-
| $SED "s/UarchState &a/AccessLogs.Context memory a/g" \
51+
| $SED "s/const UarchState a/AccessLogs.Context memory a/g" \
5252
| $SED "s/::/./g" \
5353
| $SED "s/UINT64_MAX/type(uint64).max/g" \
5454
| $SED -E "s/UArchStepStatus uarch_step/static inline UArchStepStatus step/g" \

src/AccessLogs.sol

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -68,28 +68,6 @@ library AccessLogs {
6868
//
6969
// Read methods
7070
//
71-
function readRegion(
72-
AccessLogs.Context memory a,
73-
Memory.Region memory region
74-
) internal pure returns (bytes32) {
75-
bytes32 drive = a.buffer.consumeBytes32();
76-
bytes32 rootHash = a.buffer.getRoot(region, drive);
77-
78-
require(a.currentRootHash == rootHash, "Read region root doesn't match");
79-
80-
return drive;
81-
}
82-
83-
function readLeaf(AccessLogs.Context memory a, Memory.Stride readStride)
84-
internal
85-
pure
86-
returns (bytes32)
87-
{
88-
Memory.Region memory r =
89-
Memory.regionFromStride(readStride, Memory.alignedSizeFromLog2(0));
90-
return readRegion(a, r);
91-
}
92-
9371
function readWord(
9472
AccessLogs.Context memory a,
9573
Memory.PhysicalAddress readAddress
@@ -190,4 +168,29 @@ library AccessLogs {
190168

191169
return (leaf & mask) | toWrite;
192170
}
171+
172+
/// @notice Writes 4 64-bit words (32 bytes total) to a single leaf in memory
173+
/// @dev This function is specifically designed for writing TLB entries and similar structures
174+
function write4Words(
175+
AccessLogs.Context memory a,
176+
Memory.Stride writeStride,
177+
uint64 word0,
178+
uint64 word1,
179+
uint64 word2,
180+
uint64 word3
181+
) internal pure {
182+
// Flip endianess and pack data
183+
bytes8 w0 = solidityUint64ToMachineWord(word0);
184+
bytes8 w1 = solidityUint64ToMachineWord(word1);
185+
bytes8 w2 = solidityUint64ToMachineWord(word2);
186+
bytes8 w3 = solidityUint64ToMachineWord(word3);
187+
bytes32 packed = bytes32(abi.encodePacked(w0, w1, w2, w3));
188+
// compute hash and write region
189+
bytes32 newHash = keccak256(abi.encodePacked(packed));
190+
writeRegion(
191+
a,
192+
Memory.regionFromStride(writeStride, Memory.alignedSizeFromLog2(0)),
193+
newHash
194+
);
195+
}
193196
}

src/EmulatorCompat.sol

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,19 @@ library EmulatorCompat {
3535
function readHaltFlag(AccessLogs.Context memory a)
3636
internal
3737
pure
38-
returns (bool)
38+
returns (uint64)
39+
{
40+
return a.readWord(
41+
EmulatorConstants.UARCH_HALT_FLAG_ADDRESS.toPhysicalAddress()
42+
);
43+
}
44+
45+
function writeHaltFlag(AccessLogs.Context memory a, uint64 val)
46+
internal
47+
pure
3948
{
40-
return (
41-
a.readWord(
42-
EmulatorConstants.UARCH_HALT_FLAG_ADDRESS.toPhysicalAddress()
43-
) != 0
49+
a.writeWord(
50+
EmulatorConstants.UARCH_HALT_FLAG_ADDRESS.toPhysicalAddress(), val
4451
);
4552
}
4653

@@ -82,12 +89,6 @@ library EmulatorCompat {
8289
);
8390
}
8491

85-
function setHaltFlag(AccessLogs.Context memory a) internal pure {
86-
a.writeWord(
87-
EmulatorConstants.UARCH_HALT_FLAG_ADDRESS.toPhysicalAddress(), 1
88-
);
89-
}
90-
9192
function writePc(AccessLogs.Context memory a, uint64 val) internal pure {
9293
a.writeWord(EmulatorConstants.UARCH_PC_ADDRESS.toPhysicalAddress(), val);
9394
}
@@ -280,7 +281,40 @@ library EmulatorCompat {
280281
revert(text);
281282
}
282283

283-
function putChar(AccessLogs.Context memory a, uint8 c) internal pure {}
284+
function putCharECALL(AccessLogs.Context memory a, uint8 c) internal pure {}
285+
286+
function markDirtyPageECALL(
287+
AccessLogs.Context memory a,
288+
uint64 paddr,
289+
uint64 pma_index
290+
) internal pure {}
291+
292+
function writeTlbECALL(
293+
AccessLogs.Context memory a,
294+
uint64 setIndex,
295+
uint64 slotIndex,
296+
uint64 vaddrPage,
297+
uint64 vpOffset,
298+
uint64 pmaIindex
299+
) internal pure {
300+
// compute physical address of the TLB slot
301+
uint64 paddress = EmulatorConstants.AR_SHADOW_TLB_START
302+
+ (setIndex * EmulatorConstants.TLB_SET_LENGTH)
303+
+ (slotIndex * EmulatorConstants.TLB_SLOT_LENGTH);
304+
305+
// compute stride from physical address (stride = address / leaf_size)
306+
Memory.Stride writeStride =
307+
Memory.strideFromLeafAddress(paddress.toPhysicalAddress());
308+
309+
// write the 4 words
310+
a.write4Words(
311+
writeStride,
312+
vaddrPage,
313+
vpOffset,
314+
pmaIindex,
315+
0 // zero_padding_
316+
);
317+
}
284318

285319
function uint32Log2(uint32 value) internal pure returns (uint32) {
286320
require(value > 0, "EmulatorCompat: log2(0) is undefined");

src/EmulatorConstants.sol

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,40 @@ library EmulatorConstants {
2525
// START OF AUTO-GENERATED CODE
2626

2727
uint64 constant UARCH_CYCLE_ADDRESS = 0x400008;
28+
uint64 constant UARCH_CYCLE_MAX = 0x100000;
2829
uint64 constant UARCH_HALT_FLAG_ADDRESS = 0x400000;
2930
uint64 constant UARCH_PC_ADDRESS = 0x400010;
3031
uint64 constant UARCH_X0_ADDRESS = 0x400018;
3132
uint64 constant UARCH_SHADOW_START_ADDRESS = 0x400000;
3233
uint64 constant UARCH_SHADOW_LENGTH = 0x1000;
34+
uint64 constant AR_SHADOW_TLB_START = 0x1000;
35+
uint64 constant AR_SHADOW_TLB_LENGTH = 0x6000;
3336
uint64 constant UARCH_RAM_START_ADDRESS = 0x600000;
3437
uint64 constant UARCH_RAM_LENGTH = 0x200000;
3538
uint64 constant UARCH_STATE_START_ADDRESS = 0x400000;
3639
uint8 constant UARCH_STATE_LOG2_SIZE = 22;
3740
bytes32 constant UARCH_PRISTINE_STATE_HASH =
38-
0x1bbf39b2c4324c9c8862b8e5550fe35b06ebccb0dcd2c3f114cc1411813ca5fc;
41+
0xa73214303203557aea4dd151d18244b87f90532fc40e0f25c8795456e33f2b8f;
3942
uint64 constant UARCH_ECALL_FN_HALT = 1;
4043
uint64 constant UARCH_ECALL_FN_PUTCHAR = 2;
44+
uint64 constant UARCH_ECALL_FN_MARK_DIRTY_PAGE = 3;
45+
uint64 constant UARCH_ECALL_FN_WRITE_TLB = 4;
4146
uint64 constant HTIF_YIELD = 0x348;
42-
uint64 constant IFLAGS_Y_ADDRESS = 0x2f8;
47+
uint64 constant IFLAGS_Y_ADDRESS = 0x300;
4348
uint64 constant HTIF_FROMHOST_ADDRESS = 0x330;
4449
uint8 constant CMIO_YIELD_REASON_ADVANCE_STATE = 0x0;
45-
uint32 constant TREE_LOG2_WORD_SIZE = 0x5;
46-
uint32 constant TREE_WORD_SIZE = uint32(1) << TREE_LOG2_WORD_SIZE;
47-
uint64 constant PMA_CMIO_RX_BUFFER_START = 0x60000000;
48-
uint8 constant PMA_CMIO_RX_BUFFER_LOG2_SIZE = 0x15;
49-
uint64 constant PMA_CMIO_TX_BUFFER_START = 0x60800000;
50-
uint8 constant PMA_CMIO_TX_BUFFER_LOG2_SIZE = 0x15;
50+
uint32 constant HASH_TREE_LOG2_WORD_SIZE = 0x5;
51+
uint32 constant HASH_TREE_WORD_SIZE = uint32(1) << HASH_TREE_LOG2_WORD_SIZE;
52+
uint64 constant AR_CMIO_RX_BUFFER_START = 0x60000000;
53+
uint8 constant AR_CMIO_RX_BUFFER_LOG2_SIZE = 0x15;
54+
uint64 constant AR_CMIO_TX_BUFFER_START = 0x60800000;
55+
uint8 constant AR_CMIO_TX_BUFFER_LOG2_SIZE = 0x15;
5156
// END OF AUTO-GENERATED CODE
5257

5358
uint32 constant IFLAGS_Y_SHIFT = 1;
5459
uint64 constant LOG2_CYCLES_TO_RESET = 10;
60+
61+
uint64 constant TLB_SLOT_LENGTH = 32;
62+
uint64 constant TLB_SET_SIZE = 256;
63+
uint64 constant TLB_SET_LENGTH = TLB_SET_SIZE * TLB_SLOT_LENGTH;
5564
}

src/SendCmioResponse.sol

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ library SendCmioResponse {
4242
if (dataLength > 0) {
4343
// Find the write length: the smallest power of 2 that is >= dataLength and >= tree leaf size
4444
uint32 writeLengthLog2Size = EmulatorCompat.uint32Log2(dataLength);
45-
if (writeLengthLog2Size < EmulatorConstants.TREE_LOG2_WORD_SIZE) {
46-
writeLengthLog2Size = EmulatorConstants.TREE_LOG2_WORD_SIZE; // minimum write size is the tree leaf size
45+
if (
46+
writeLengthLog2Size < EmulatorConstants.HASH_TREE_LOG2_WORD_SIZE
47+
) {
48+
writeLengthLog2Size = EmulatorConstants.HASH_TREE_LOG2_WORD_SIZE; // minimum write size is the tree leaf size
4749
}
4850
if (
4951
EmulatorCompat.uint32ShiftLeft(1, writeLengthLog2Size)
@@ -53,20 +55,20 @@ library SendCmioResponse {
5355
}
5456
if (
5557
writeLengthLog2Size
56-
> EmulatorConstants.PMA_CMIO_RX_BUFFER_LOG2_SIZE
58+
> EmulatorConstants.AR_CMIO_RX_BUFFER_LOG2_SIZE
5759
) {
5860
EmulatorCompat.throwRuntimeError(
5961
a, "CMIO response data is too large"
6062
);
6163
}
6264
a.writeRegion(
6365
Memory.regionFromPhysicalAddress(
64-
EmulatorConstants.PMA_CMIO_RX_BUFFER_START.toPhysicalAddress(
66+
EmulatorConstants.AR_CMIO_RX_BUFFER_START.toPhysicalAddress(
6567
),
6668
Memory.alignedSizeFromLog2(
6769
uint8(
6870
writeLengthLog2Size
69-
- EmulatorConstants.TREE_LOG2_WORD_SIZE
71+
- EmulatorConstants.HASH_TREE_LOG2_WORD_SIZE
7072
)
7173
)
7274
),

0 commit comments

Comments
 (0)