Skip to content

Commit

Permalink
Adjust to new uarch reset
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcos Pernambuco Motta committed Nov 26, 2023
1 parent a7aeb1a commit fdc384b
Show file tree
Hide file tree
Showing 23 changed files with 293 additions and 145 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ test/uarch-log
test/UArchReplay_*.t.sol
src/UArchConstants.sol
src/UArchStep.sol
src/UArchReset.sol
.DS_Store
35 changes: 22 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ DOWNLOADDIR := downloads
READY_SRC_DIR := ready_src

BIN_TEST_VERSION ?= v0.29.0
BIN_TEST_DIR := $(TEST_DIR)/uarch-bin
BIN_TEST_FILE := machine-tests-$(BIN_TEST_VERSION).tar.gz
BIN_DOWNLOAD_URL := https://github.com/cartesi/machine-tests/releases/download/$(BIN_TEST_VERSION)/$(BIN_TEST_FILE)
BIN_DOWNLOAD_FILEPATH := $(DOWNLOADDIR)/$(BIN_TEST_FILE)
BIN_TEST_DIR ?= $(TEST_DIR)/uarch-bin
BIN_TEST_FILE ?= machine-tests-$(BIN_TEST_VERSION).tar.gz
BIN_DOWNLOAD_URL ?= https://github.com/cartesi/machine-tests/releases/download/$(BIN_TEST_VERSION)/$(BIN_TEST_FILE)
BIN_DOWNLOAD_FILEPATH ?= $(DOWNLOADDIR)/$(BIN_TEST_FILE)

LOG_TEST_VERSION ?= v0.15.2
LOG_TEST_DIR := $(TEST_DIR)/uarch-log
LOG_TEST_FILE := uarch-riscv-tests-json-logs-$(LOG_TEST_VERSION).tar.gz
LOG_DOWNLOAD_URL := https://github.com/cartesi/machine-emulator/releases/download/$(LOG_TEST_VERSION)/$(LOG_TEST_FILE)
LOG_DOWNLOAD_FILEPATH := $(DOWNLOADDIR)/$(LOG_TEST_FILE)
LOG_TEST_DIR ?= $(TEST_DIR)/uarch-log
LOG_TEST_FILE ?= uarch-riscv-tests-json-logs-$(LOG_TEST_VERSION).tar.gz
LOG_DOWNLOAD_URL ?= https://github.com/cartesi/machine-emulator/releases/download/$(LOG_TEST_VERSION)/$(LOG_TEST_FILE)
LOG_DOWNLOAD_FILEPATH ?= $(DOWNLOADDIR)/$(LOG_TEST_FILE)

DOWNLOADFILES := $(BIN_DOWNLOAD_FILEPATH) $(LOG_DOWNLOAD_FILEPATH)
GENERATEDFILES := $(READY_SRC_DIR)/*.sol
Expand All @@ -25,6 +25,8 @@ help:
@echo '* all - build solidity code. To build from a clean clone, run: make submodules all'
@echo ' build - build solidity code'
@echo ' generate-step - generate solidity-step code from cpp'
@echo ' generate-reset - generate solidity-reset code from cpp'
@echo ' generate-constants - generate solidity-constants code by querying the cartesi machine'
@echo ' generate-mock - generate mock library code'
@echo ' generate-prod - generate production library code'
@echo ' generate-replay - generate replay tests'
Expand All @@ -44,11 +46,11 @@ $(LOG_DOWNLOAD_FILEPATH):

all: build test-all

build: generate-step
build: generate-step generate-reset generate-constants
forge build

clean:
rm -rf src/UArchConstants.sol src/UArchStep.sol test/UArchReplay_*.t.sol
rm -rf src/UArchConstants.sol src/UArchStep.sol src/UArchReset.sol test/UArchReplay_*.t.sol
forge clean

shasum-download: $(DOWNLOADFILES)
Expand All @@ -70,6 +72,9 @@ pretest: checksum-download
@mkdir -p $(BIN_TEST_DIR)
@mkdir -p $(LOG_TEST_DIR)
@tar -xzf $(BIN_DOWNLOAD_FILEPATH) -C $(BIN_TEST_DIR)
echo "opa opa"
echo $(LOG_DOWNLOAD_FILEPATH)
echo $(LOG_TEST_DIR)
@tar -xzf $(LOG_DOWNLOAD_FILEPATH) -C $(LOG_TEST_DIR)
@rm $(BIN_TEST_DIR)/*.dump $(BIN_TEST_DIR)/*.elf

Expand Down Expand Up @@ -105,15 +110,19 @@ generate-replay:
./helper_scripts/generate_ReplayTests.sh
$(MAKE) fmt

generate-constants: $(EMULATOR_DIR)
EMULATOR_DIR=$(EMULATOR_DIR) ./helper_scripts/generate_UArchConstants.sh

generate-step: $(EMULATOR_DIR)/src/uarch-step.h $(EMULATOR_DIR)/src/uarch-step.cpp
EMULATOR_DIR=$(EMULATOR_DIR) ./helper_scripts/generate_UArchStep.sh
EMULATOR_DIR=$(EMULATOR_DIR) ./helper_scripts/generate_UArchConstants.sh
$(MAKE) generate-prod

generate-reset: $(EMULATOR_DIR)/src/uarch-reset-state.cpp
EMULATOR_DIR=$(EMULATOR_DIR) ./helper_scripts/generate_UArchReset.sh

fmt:
forge fmt

submodules:
git submodule update --init --recursive

.PHONY: help all build clean checksum-download checksum-mock checksum-prod fmt generate-mock generate-prod generate-replay generate-step pretest submodules test-all test-mock test-prod test-replay
.PHONY: help all build clean checksum-download checksum-mock checksum-prod fmt generate-mock generate-prod generate-replay generate-step pretest submodules test-all test-mock test-prod test-replay generate-constants generate-reset
6 changes: 4 additions & 2 deletions helper_scripts/generate_AccessLogs.sh
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
#!/bin/bash

SED=${SED:-"sed"}

if [ "$1" == "-h" ] || ([ "$1" != "prod" ] && [ "$1" != "mock" ] && [ $# -gt 0 ] ) || [ $# -gt 1 ]; then
echo "Usage: `basename $0` <prod|mock>"
exit 0
fi

if [ "$1" == "prod" ] || [ $# -eq 0 ]; then
find src -type f -name '*.sol' | sed 's/src\///' | xargs -I {} gpp \
find src -type f -name '*.sol' | $SED 's/src\///' | xargs -I {} gpp \
-U "" "" "(" "," ")" "(" ")" "//:#" "\\" \
-M "//:#" "\n" " " " " "\n" "(" ")" \
-I "src" \
src/{} -o ready_src/{}
else
find src -type f -name '*.sol' | sed 's/src\///' | xargs -I {} gpp \
find src -type f -name '*.sol' | $SED 's/src\///' | xargs -I {} gpp \
-U "" "" "(" "," ")" "(" ")" "//:#" "\\" \
-M "//:#" "\n" " " " " "\n" "(" ")" \
-I "src" \
Expand Down
9 changes: 6 additions & 3 deletions helper_scripts/generate_ReplayTests.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#!/bin/bash

SED=${SED:-"sed"}

for i in test/uarch-log/rv64ui*; do
BASE=`basename $i .json | sed "s/-/_/g"`
BASE=`basename $i .json | $SED "s/-/_/g"`
cp templates/UArchReplay.t.sol.template test/UArchReplay_$BASE.t.sol
sed -i "s/@X@/$BASE/g" test/UArchReplay_$BASE.t.sol
$SED -i "s/@X@/$BASE/g" test/UArchReplay_$BASE.t.sol
P=`basename $i`
sed -i "s/@PATH@/$P/g" test/UArchReplay_$BASE.t.sol
$SED -i "s/@PATH@/$P/g" test/UArchReplay_$BASE.t.sol
done
30 changes: 30 additions & 0 deletions helper_scripts/generate_UArchConstants.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/lua5.4

-- This scripts generates a snipet of solidity code to be inserted
-- in the UarchConstants.sol file.

local cartesi = require("cartesi")

local function hex(n)
return string.format("%x", n)
end

local function hexstring(hash)
return (string.gsub(hash, ".", function(c) return string.format("%02x", string.byte(c)) end))
end

local out = io.stdout

out:write(' uint64 constant UCYCLE = 0x' .. hex(cartesi.machine.get_csr_address("uarch_cycle")) .. ';\n')
out:write(' uint64 constant UHALT = 0x' .. hex(cartesi.machine.get_csr_address("uarch_halt_flag")) .. ';\n')
out:write(' uint64 constant UPC = 0x' .. hex(cartesi.machine.get_csr_address("uarch_pc")) .. ';\n')
out:write(' uint64 constant UX0 = 0x' .. hex(cartesi.machine.get_uarch_x_address(0)) .. ';\n')
out:write(' uint64 constant UARCH_SHADOW_START_ADDRESS = 0x' .. hex(cartesi.UARCH_SHADOW_START_ADDRESS) .. ';\n')
out:write(' uint64 constant UARCH_SHADOW_LENGTH = 0x' .. hex(cartesi.UARCH_SHADOW_LENGTH) .. ';\n')
out:write(' uint64 constant UARCH_RAM_START_ADDRESS = 0x' .. hex(cartesi.UARCH_RAM_START_ADDRESS) .. ';\n')
out:write(' uint64 constant UARCH_RAM_LENGTH = 0x' .. hex(cartesi.UARCH_RAM_LENGTH) .. ';\n')
out:write(' uint64 constant RESET_POSITION = 0x' .. hex(cartesi.UARCH_STATE_START_ADDRESS) .. ';\n')
out:write(' uint8 constant RESET_ALIGNED_SIZE = ' .. cartesi.UARCH_STATE_LOG2_SIZE .. ';\n')
out:write(' bytes32 constant PRESTINE_STATE = 0x' .. hexstring(cartesi.UARCH_PRISTINE_STATE_HASH) .. ';\n')

out:close()
30 changes: 11 additions & 19 deletions helper_scripts/generate_UArchConstants.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ TARGET_FILE="src/UArchConstants.sol"
KEYWORD_START="START OF AUTO-GENERATED CODE"
KEYWORD_END="END OF AUTO-GENERATED CODE"

MACHINE_CMD_TEMPLATE="docker run -w /opt/cartesi/lib/lua/5.4 \
-it cartesi/machine-emulator:devel lua \
-e 'print(string.format(\"%x\", require(\"cartesi\").machine.%API(\"%ARG\")))'"
CONSTANTS_ARR=( "UCYCLE;get_csr_address;uarch_cycle" \
"UHALT;get_csr_address;uarch_halt_flag" \
"UPC;get_csr_address;uarch_pc" \
"UX0;get_uarch_x_address;0")

# grab head and tail of the template
start=`cat "$TEMPLATE_FILE" | grep "$KEYWORD_START" -n | grep -Eo "[0-9]*"`
end=`cat "$TEMPLATE_FILE" | grep "$KEYWORD_END" -n | grep -Eo "[0-9]*"`
Expand All @@ -23,19 +15,19 @@ let last=total-end+1
h=`head -n $start $TEMPLATE_FILE`
t=`tail -n -$last $TEMPLATE_FILE`

cd $EMULATOR_DIR
make build-debian-image

constants=""
for c in ${CONSTANTS_ARR[@]}; do
read var api arg <<<$(IFS=";"; echo $c)
machine_cmd=`echo $MACHINE_CMD_TEMPLATE | sed s#%API#$api#g | sed s#%ARG#$arg#g`
addr=`eval $machine_cmd | tr -d "\r"`
constants="$constants uint64 constant $var = 0x$addr;\n"
done
cd $EMULATOR_DIR
make build-debian-image

cd - > /dev/null
# run the Lua script that instantiates the cartesi module and
# outputs the uarch constants values
constants=$(docker run \
-v`pwd`:/opt/cartesi/machine-solidity-step \
-w /opt/cartesi/machine-solidity-step \
-it cartesi/machine-emulator:devel \
/opt/cartesi/machine-solidity-step/helper_scripts/generate_UArchConstants.lua)

# compose the solidity file from all components
echo -e "$h" "\n\n$constants" > $TARGET_FILE
echo -e "$t" >> $TARGET_FILE

echo "wrote $TARGET_FILE"
39 changes: 39 additions & 0 deletions helper_scripts/generate_UArchReset.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash
SED=${SED:-"sed"}
EMULATOR_DIR=${EMULATOR_DIR:-"../emulator"}
CPP_RESET_PATH=${EMULATOR_DIR}"/src/uarch-reset-state.cpp"

TEMPLATE_FILE="./templates/UArchReset.sol.template"
TARGET_FILE="src/UArchReset.sol"
COMPAT_FILE="src/UArchCompat.sol"
KEYWORD_START="START OF AUTO-GENERATED CODE"
KEYWORD_END="END OF AUTO-GENERATED CODE"

# get function names from UArchCompat.sol
COMPAT_FNS=`cat $COMPAT_FILE | grep -o "function [^(]*(" | $SED "s/function//g" | $SED "s/(//g"`
COMPAT_FNS=`echo $COMPAT_FNS | $SED -E "s/( |\n)/|/g"`

# grab head and tail of the template
start=`cat "$TEMPLATE_FILE" | grep "$KEYWORD_START" -n | grep -Eo "[0-9]*"`
end=`cat "$TEMPLATE_FILE" | grep "$KEYWORD_END" -n | grep -Eo "[0-9]*"`
total=`wc -l "$TEMPLATE_FILE" | grep -Eo "[0-9]*"`
let last=total-end+1

h=`head -n $start $TEMPLATE_FILE`
t=`tail -n -$last $TEMPLATE_FILE`

cpp_src=`cat "$CPP_RESET_PATH"`
pattern="namespace cartesi \{(.*)\}"
[[ $cpp_src =~ $pattern ]]

# replace cpp specific syntaxes with solidity ones
cpp_src=`echo "${BASH_REMATCH[1]}" \
| $SED "/Explicit instantiatio/d" \
| $SED "/template/d" \
| $SED -E "s/($COMPAT_FNS)/UArchCompat.\1/g" \
| $SED "s/void uarch_reset_state(UarchState &a) {/function reset(AccessLogs.Context memory a) internal pure {/"`

# compose the solidity file from all components
echo -e "$h" "\n\n$h_src" > $TARGET_FILE
echo "$cpp_src" >> $TARGET_FILE
echo -e "\n$t" >> $TARGET_FILE
33 changes: 17 additions & 16 deletions helper_scripts/generate_UArchStep.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash
SED=${SED:-"sed"}
EMULATOR_DIR=${EMULATOR_DIR:-"../emulator"}
CPP_STEP_PATH=${EMULATOR_DIR}"/src/uarch-step.cpp"
CPP_STEP_H_PATH=${EMULATOR_DIR}"/src/uarch-step.h"
Expand Down Expand Up @@ -30,28 +31,28 @@ pattern="enum class (.*) : int \{(.*)\};"
h_src=`echo "enum ${BASH_REMATCH[1]} {${BASH_REMATCH[2]}}"`

# get function names from UArchCompat.sol
COMPAT_FNS=`cat $COMPAT_FILE | grep -o "function [^(]*(" | sed "s/function//g" | sed "s/(//g"`
COMPAT_FNS=`echo $COMPAT_FNS | sed -E "s/( |\n)/|/g"`
COMPAT_FNS=`cat $COMPAT_FILE | grep -o "function [^(]*(" | $SED "s/function//g" | $SED "s/(//g"`
COMPAT_FNS=`echo $COMPAT_FNS | $SED -E "s/( |\n)/|/g"`

cpp_src=`cat "$CPP_STEP_PATH"`
pattern="namespace cartesi \{(.*)\}"
[[ $cpp_src =~ $pattern ]]
# replace cpp specific syntaxes with solidity ones
cpp_src=`echo "${BASH_REMATCH[1]}" \
| sed "/template/d" \
| sed "/dumpInsn/d" \
| sed "/note/d" \
| sed "s/constexpr//g" \
| sed "s/UarchState &a/AccessLogs.Context memory a/g" \
| sed "s/throw std::runtime_error/revert/g" \
| sed "s/::/./g" \
| sed "s/UINT64_MAX/type(uint64).max/g" \
| sed -E "s/UArchStepStatus uarch_step/static inline UArchStepStatus step/g" \
| sed -E "s/static inline (\w+) ($INTERNAL_FN)\(([^\n]*)\) \{/function \2\(\3\) internal pure returns \(\1\)\{/g" \
| sed -E "s/static inline (\w+) (\w+)\(([^\n]*)\) \{/function \2\(\3\) private pure returns \(\1\)\{/g" \
| sed -E "s/([^\n]*) $UNUSED_INSN_FN([^\n]*) uint32 insn,([^\n]*)/\1 $UNUSED_INSN_FN\2 uint32,\3/g" \
| sed -E "s/($COMPAT_FNS)/UArchCompat.\1/g" \
| sed "s/ returns (void)//g"`
| $SED "/template/d" \
| $SED "/dumpInsn/d" \
| $SED "/note/d" \
| $SED "s/constexpr//g" \
| $SED "s/UarchState &a/AccessLogs.Context memory a/g" \
| $SED "s/throw std::runtime_error/revert/g" \
| $SED "s/::/./g" \
| $SED "s/UINT64_MAX/type(uint64).max/g" \
| $SED -E "s/UArchStepStatus uarch_step/static inline UArchStepStatus step/g" \
| $SED -E "s/static inline (\w+) ($INTERNAL_FN)\(([^\n]*)\) \{/function \2\(\3\) internal pure returns \(\1\)\{/g" \
| $SED -E "s/static inline (\w+) (\w+)\(([^\n]*)\) \{/function \2\(\3\) private pure returns \(\1\)\{/g" \
| $SED -E "s/([^\n]*) $UNUSED_INSN_FN([^\n]*) uint32 insn,([^\n]*)/\1 $UNUSED_INSN_FN\2 uint32,\3/g" \
| $SED -E "s/($COMPAT_FNS)/UArchCompat.\1/g" \
| $SED "s/ returns (void)//g"`

# compose the solidity file from all components
echo -e "$h" "\n\n$h_src" > $TARGET_FILE
Expand Down
9 changes: 2 additions & 7 deletions ready_src/MetaStep.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
pragma solidity ^0.8.0;

import "./UArchStep.sol";
import "./UArchReset.sol";

library MetaStep {
using AccessLogs for AccessLogs.Context;
Expand All @@ -39,13 +40,7 @@ library MetaStep {
<< UArchConstants.LOG2_CYCLES_TO_RESET
) {
// if counter is a multiple of (1 << UArchConstants.LOG2_CYCLES_TO_RESET), run uarch reset
accessLogs.writeRegion(
Memory.regionFromPhysicalAddress(
Memory.PhysicalAddress.wrap(UArchConstants.RESET_POSITION),
Memory.AlignedSize.wrap(UArchConstants.RESET_ALIGNED_SIZE)
),
UArchConstants.PRESTINE_STATE
);
UArchReset.reset(accessLogs);
machineState = accessLogs.currentRootHash;
}

Expand Down
10 changes: 10 additions & 0 deletions ready_src/UArchCompat.sol
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ library UArchCompat {
a.writeWord(paddr.toPhysicalAddress(), val);
}

function resetState(AccessLogs.Context memory a) internal pure {
a.writeRegion(
Memory.regionFromPhysicalAddress(
Memory.PhysicalAddress.wrap(UArchConstants.RESET_POSITION),
Memory.AlignedSize.wrap(UArchConstants.RESET_ALIGNED_SIZE)
),
UArchConstants.PRESTINE_STATE
);
}

function int8ToUint64(int8 val) internal pure returns (uint64) {
return uint64(int64(val));
}
Expand Down
21 changes: 12 additions & 9 deletions ready_src/UArchConstants.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@ pragma solidity ^0.8.0;
library UArchConstants {
// START OF AUTO-GENERATED CODE

uint64 constant UCYCLE = 0x320;
uint64 constant UHALT = 0x328;
uint64 constant UPC = 0x330;
uint64 constant UX0 = 0x340;

uint64 constant UCYCLE = 0x400008;
uint64 constant UHALT = 0x400000;
uint64 constant UPC = 0x400010;
uint64 constant UX0 = 0x400018;
uint64 constant UARCH_SHADOW_START_ADDRESS = 0x400000;
uint64 constant UARCH_SHADOW_LENGTH = 0x1000;
uint64 constant UARCH_RAM_START_ADDRESS = 0x600000;
uint64 constant UARCH_RAM_LENGTH = 0x200000;
uint64 constant RESET_POSITION = 0x400000;
uint8 constant RESET_ALIGNED_SIZE = 22;
bytes32 constant PRESTINE_STATE =
0x990691da914eed5270e1aee557bd190fb0c79b67f8f6d87ee2c491c525fe2c98;
// END OF AUTO-GENERATED CODE

uint64 constant LOG2_CYCLES_TO_RESET = 10;
uint64 constant RESET_POSITION = 100;
uint8 constant RESET_ALIGNED_SIZE = 32;
bytes32 constant PRESTINE_STATE =
0x1234567812345678123456781234567812345678123456781234567812345678;
}
33 changes: 33 additions & 0 deletions ready_src/UArchReset.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

/// @title UArchReset
/// @notice Reset microarchitecture to pristine state
/// @dev This file is generated from templates/UArchReset.sol.template, one should not modify the content directly

pragma solidity ^0.8.0;

import "./UArchCompat.sol";

library UArchReset {
// START OF AUTO-GENERATED CODE

function reset(AccessLogs.Context memory a) internal pure {
UArchCompat.resetState(a);
}

// END OF AUTO-GENERATED CODE
}
Loading

0 comments on commit fdc384b

Please sign in to comment.