diff --git a/Dockerfile b/Dockerfile index 809031521..97ebebd1a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,55 +1,86 @@ -FROM --platform=$TARGETPLATFORM cartesi/toolchain:0.17.0-rv64ima-lp64 as linux-env -ARG GIT_COMMIT="" -ARG DEBUG=no -ARG COVERAGE=no -ARG SANITIZE=no +FROM --platform=$TARGETPLATFORM debian:bookworm-20241016 AS toolchain RUN apt-get update && \ - DEBIAN_FRONTEND="noninteractive" apt-get install --no-install-recommends -y \ - build-essential vim wget git lcov \ - libboost1.81-dev libssl-dev libslirp-dev \ - ca-certificates pkg-config lua5.4 liblua5.4-dev \ - luarocks xxd procps && \ - rm -rf /var/lib/apt/lists/* /var/cache/apt/* - + DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y \ + build-essential vim wget git lcov \ + libboost1.81-dev libssl-dev libslirp-dev \ + ca-certificates pkg-config lua5.4 liblua5.4-dev \ + luarocks xxd procps \ + g++-12-riscv64-linux-gnu=12.2.0-13cross1 \ + gcc-riscv64-unknown-elf=12.2.0-14+11+b1 && \ + rm -rf /var/lib/apt/lists/* + +# Install clang 18 RUN apt-get update && \ - DEBIAN_FRONTEND="noninteractive" apt-get install --no-install-recommends -y wget software-properties-common gnupg && \ + DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y \ + wget software-properties-common gnupg && \ wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc && \ add-apt-repository -y 'deb http://apt.llvm.org/bookworm/ llvm-toolchain-bookworm-18 main' && \ add-apt-repository -y 'deb http://apt.llvm.org/bookworm/ llvm-toolchain-bookworm-18 main' && \ apt-get update && \ - apt-get install --no-install-recommends -y clang-tidy-18 clang-format-18 && \ + DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y \ + clang-tidy-18 clang-format-18 && \ update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-18 120 && \ update-alternatives --install /usr/bin/clang-tidy clang-tidy /usr/bin/clang-tidy-18 120 && \ - rm -rf /var/lib/apt/lists/* /var/cache/apt/* + rm -rf /var/lib/apt/lists/* +# Install lua packages RUN luarocks install --lua-version=5.4 luasocket && \ luarocks install --lua-version=5.4 luasec && \ luarocks install --lua-version=5.4 luaposix && \ - luarocks install --lua-version=5.4 luacheck && \ - cargo install stylua@0.20.0 --features lua54 - -# Environment has the riscv64-cartesi-linux-gnu-* toolchain + luarocks install --lua-version=5.4 luacheck + +# Install stylua +RUN cd /tmp && \ + wget https://github.com/JohnnyMorganz/StyLua/releases/download/v0.20.0/stylua-linux-`uname -m`.zip && \ + case $(uname -m) in \ + x86_64) echo "28eddb9257bf85b20b1c337e536b7a3d16ba308863f067d447c1f4d24c6dec64 stylua-linux-x86_64.zip" | sha256sum --check ;; \ + aarch64) echo "376b675766bc0b9261b2b82c8d0f624c7e5f78e83bd8490330e0bf3d8f770ad7 stylua-linux-aarch64.zip" | sha256sum --check ;; \ + esac && \ + unzip stylua-linux-*.zip && \ + mv stylua /usr/local/bin/ && \ + rm -f stylua-linux-*.zip + +# Environment has the riscv64 toolchains ENV DEV_ENV_HAS_TOOLCHAIN=yes -WORKDIR /usr/src/emulator +# Install su-exec +RUN cd /tmp && \ + git clone --branch v0.2 --depth 1 https://github.com/ncopa/su-exec.git && \ + cd su-exec && \ + if [ `git rev-parse --verify HEAD` != 'f85e5bde1afef399021fbc2a99c837cf851ceafa' ]; then exit 1; fi && \ + make && \ + cp su-exec /usr/local/bin/ && \ + rm -rf /tmp/su-exec -FROM --platform=$TARGETPLATFORM linux-env as dep-builder +# Install workaround to run as current user +COPY tools/docker-entrypoint.sh /usr/local/bin/entrypoint.sh +RUN chmod +x /usr/local/bin/entrypoint.sh -COPY Makefile . -COPY third-party third-party +# Install necessary headers to make GNU libc work with lp64 ABI +COPY tools/gnu/stubs-lp64.h /usr/riscv64-linux-gnu/include/gnu/stubs-lp64.h -FROM --platform=$TARGETPLATFORM dep-builder as builder +ENTRYPOINT ["/usr/local/bin/entrypoint.sh"] +WORKDIR /usr/src/emulator +CMD ["/bin/bash", "-l"] + +#################################################################################################### +FROM --platform=$TARGETPLATFORM toolchain AS builder +ARG GIT_COMMIT="" +ARG DEBUG=no +ARG COVERAGE=no +ARG SANITIZE=no COPY . . RUN make -j$(nproc) git_commit=$GIT_COMMIT debug=$DEBUG coverage=$COVERAGE sanitize=$SANITIZE +#################################################################################################### FROM --platform=$TARGETPLATFORM builder as debian-packager -ARG MACHINE_EMULATOR_VERSION=0.0.0 RUN make install-uarch debian-package DESTDIR=$PWD/_install -FROM --platform=$TARGETPLATFORM debian:bookworm-20230725-slim +#################################################################################################### +FROM --platform=$TARGETPLATFORM debian:bookworm-20241016-slim ARG MACHINE_EMULATOR_VERSION=0.0.0 ARG TARGETARCH @@ -59,10 +90,9 @@ COPY --from=debian-packager \ COPY --from=debian-packager /usr/local/lib/lua /usr/local/lib/lua COPY --from=debian-packager /usr/local/share/lua /usr/local/share/lua -RUN apt-get update && DEBIAN_FRONTEND="noninteractive" apt-get install -y \ - ./cartesi-machine.deb \ - && rm -rf /var/lib/apt/lists/* \ - && rm cartesi-machine.deb +RUN apt-get update && \ + apt-get install -y ./cartesi-machine.deb && \ + rm -rf /var/lib/apt/lists/* /var/cache/apt/* cartesi-machine.deb RUN addgroup --system --gid 102 cartesi && \ adduser --system --uid 102 --ingroup cartesi --disabled-login --no-create-home --home /nonexistent --gecos "cartesi user" --shell /bin/false cartesi diff --git a/Makefile b/Makefile index 462d1d01a..1d692987a 100644 --- a/Makefile +++ b/Makefile @@ -117,6 +117,7 @@ relwithdebinfo?=no release?=no sanitize?=no coverage?=no +git_commit?= # If not build type is chosen, set the default to release with debug information, # so the emulator is packaged correctly by default. @@ -129,6 +130,7 @@ export debug export relwithdebinfo export release export coverage +export git_commit COVERAGE_TOOLCHAIN?=gcc export COVERAGE_TOOLCHAIN @@ -161,7 +163,7 @@ help: @echo 'Main targets:' @echo '* all - Build the src/ code. To build from a clean clone, run: make submodules all' @echo ' uarch - Build microarchitecture (requires riscv64-cartesi-linux-gnu-* toolchain)' - @echo ' uarch-with-linux-env - Build microarchitecture using the linux-env docker image' + @echo ' uarch-with-toolchain - Build microarchitecture using the toolchain docker image' @echo ' build-tests-all - Build all tests (machine, uarch and misc)' @echo ' build-tests-machine - Build machine emulator tests (requires rv64gc-lp64d riscv64-cartesi-linux-gnu-* toolchain)' @echo ' build-tests-machine-with-toolchain - Build machine emulator tests using the rv64gc-lp64d toolchain docker image' @@ -177,7 +179,7 @@ help: @echo 'Docker images targets:' @echo ' build-emulator-image - Build the machine-emulator debian based docker image' @echo ' build-debian-package - Build the cartesi-machine.deb package from image' - @echo ' build-linux-env - Build the linux environment docker image' + @echo ' build-toolchain - Build the emulator toolchain docker image' @echo ' create-generated-files-patch - Create patch that adds generated files to source tree' @echo 'Cleaning targets:' @echo ' clean - Clean the src/ artifacts' @@ -251,8 +253,8 @@ $(SRCDIR)/machine-c-version.h: build-emulator-builder-image: docker build $(DOCKER_PLATFORM) --build-arg DEBUG=$(debug) --build-arg COVERAGE=$(coverage) --build-arg SANITIZE=$(sanitize) --target builder -t cartesi/machine-emulator:builder -f Dockerfile . -build-emulator-linux-env-image build-linux-env: - docker build $(DOCKER_PLATFORM) --target linux-env -t cartesi/machine-emulator:linux-env -f Dockerfile . +build-emulator-toolchain-image build-toolchain: + docker build $(DOCKER_PLATFORM) --target toolchain -t cartesi/machine-emulator:toolchain -f Dockerfile . build-emulator-image: docker build $(DOCKER_PLATFORM) --build-arg DEBUG=$(debug) --build-arg COVERAGE=$(coverage) --build-arg SANITIZE=$(sanitize) --build-arg MACHINE_EMULATOR_VERSION=$(MACHINE_EMULATOR_VERSION) -t cartesi/machine-emulator:$(TAG) -f Dockerfile . @@ -285,36 +287,36 @@ copy: docker cp uarch-ram-bin:/usr/src/emulator/uarch/uarch-pristine-hash.c . docker rm uarch-ram-bin -check-linux-env: - @if docker images $(DOCKER_PLATFORM) -q cartesi/machine-emulator:linux-env 2>/dev/null | grep -q .; then \ - echo "Docker image cartesi/machine-emulator:linux-env exists"; \ +check-toolchain: + @if docker images $(DOCKER_PLATFORM) -q cartesi/machine-emulator:toolchain 2>/dev/null | grep -q .; then \ + echo "Docker image cartesi/machine-emulator:toolchain exists"; \ else \ - echo "Docker image cartesi/machine-emulator:linux-env does not exist. Creating:"; \ - $(MAKE) build-linux-env; \ + echo "Docker image cartesi/machine-emulator:toolchain does not exist. Creating:"; \ + $(MAKE) build-toolchain; \ fi -linux-env: check-linux-env - @docker run $(DOCKER_PLATFORM) --hostname linux-env -it --rm \ +toolchain-env: check-toolchain + @docker run $(DOCKER_PLATFORM) --hostname toolchain -it --rm \ -e USER=$$(id -u -n) \ -e GROUP=$$(id -g -n) \ -e UID=$$(id -u) \ -e GID=$$(id -g) \ -v `pwd`:/opt/cartesi/machine-emulator \ -w /opt/cartesi/machine-emulator \ - cartesi/machine-emulator:linux-env /bin/bash + cartesi/machine-emulator:toolchain /bin/bash -linux-env-exec: check-linux-env - @docker run --hostname linux-env --rm \ +toolchain-exec: check-toolchain + @docker run --hostname toolchain --rm \ -e USER=$$(id -u -n) \ -e GROUP=$$(id -g -n) \ -e UID=$$(id -u) \ -e GID=$$(id -g) \ -v `pwd`:/opt/cartesi/machine-emulator \ -w /opt/cartesi/machine-emulator \ - cartesi/machine-emulator:linux-env /bin/bash -c "$(CONTAINER_COMMAND)" + cartesi/machine-emulator:toolchain /bin/bash -c "$(CONTAINER_COMMAND)" -uarch-with-linux-env: - @$(MAKE) linux-env-exec CONTAINER_COMMAND="make uarch" +uarch-with-toolchain: + @$(MAKE) toolchain-exec CONTAINER_COMMAND="make uarch" # Create install directories $(BIN_INSTALL_PATH) $(LIB_INSTALL_PATH) $(LUA_INSTALL_PATH) $(LUA_INSTALL_CPATH) $(LUA_INSTALL_CPATH)/cartesi $(LUA_INSTALL_PATH)/cartesi $(INC_INSTALL_PATH) $(IMAGES_INSTALL_PATH) $(UARCH_INSTALL_PATH) $(TESTS_DATA_INSTALL_PATH) $(TESTS_SCRIPTS_INSTALL_PATH) $(TESTS_LUA_INSTALL_PATH): diff --git a/README.md b/README.md index 46c1b1afc..58e83ca64 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Run `make help` for a list of target options. Here are some of them: Main targets: * all - Build the src/ code. To build from a clean clone, run: make submodules all uarch - Build microarchitecture (requires riscv64-cartesi-linux-gnu-* toolchain) - uarch-with-linux-env - Build microarchitecture using the linux-env docker image + uarch-with-toolchain - Build microarchitecture using the toolchain docker image build-tests-all - Build all tests (machine, uarch and misc) build-tests-machine - Build machine emulator tests (requires rv64gc-lp64d riscv64-cartesi-linux-gnu-* toolchain) build-tests-machine-with-toolchain - Build machine emulator tests using the rv64gc-lp64d toolchain docker image @@ -28,7 +28,7 @@ Main targets: Docker images targets: build-emulator-image - Build the machine-emulator debian based docker image build-debian-package - Build the cartesi-machine.deb package from image - build-linux-env - Build the linux environment docker image + build-toolchain - Build the emulator toolchain docker image create-generated-files-patch - Create patch that adds generated files to source tree Cleaning targets: clean - Clean the src/ artifacts diff --git a/src/Makefile b/src/Makefile index 56f63dcf6..8834659b4 100644 --- a/src/Makefile +++ b/src/Makefile @@ -575,7 +575,7 @@ ifeq (,$(wildcard ../uarch/uarch-pristine-hash.c)) @if [ "$(DEV_ENV_HAS_TOOLCHAIN)" = "yes" ]; then \ $(MAKE) -C .. uarch; \ else \ - $(MAKE) -C .. uarch-with-linux-env; \ + $(MAKE) -C .. uarch-with-toolchain; \ fi endif diff --git a/tests/Dockerfile b/tests/Dockerfile index da954e5b5..eebba3626 100644 --- a/tests/Dockerfile +++ b/tests/Dockerfile @@ -1,34 +1,20 @@ ARG TAG=devel -FROM --platform=$TARGETPLATFORM cartesi/toolchain:0.17.0 as machine-tests-builder +FROM --platform=$TARGETPLATFORM cartesi/machine-emulator:builder AS tests-builder ARG DEBUG=no ARG COVERAGE=no ARG SANITIZE=no -COPY . /usr/src/emulator - -WORKDIR /usr/src/emulator - RUN make -j$(nproc) build-tests-machine debug=$DEBUG coverage=$COVERAGE sanitize=$SANITIZE - -FROM --platform=$TARGETPLATFORM cartesi/machine-emulator:builder as tests-builder -ARG DEBUG=no -ARG COVERAGE=no -ARG SANITIZE=no - -COPY --from=machine-tests-builder /usr/src/emulator/tests/build/machine /usr/src/emulator/tests/build/machine - RUN make -j$(nproc) build-tests-misc build-tests-uarch build-tests-images debug=$DEBUG coverage=$COVERAGE sanitize=$SANITIZE -FROM tests-builder as tests-debian-packager -ARG MACHINE_EMULATOR_VERSION=0.0.0 -ARG TARGETARCH +#################################################################################################### +FROM tests-builder AS tests-debian-packager RUN make tests-debian-package DESTDIR=$PWD/dpkg/tests-install && \ make tests-data-debian-package DESTDIR=$PWD/dpkg/tests-data-install +#################################################################################################### FROM --platform=$TARGETPLATFORM cartesi/machine-emulator:$TAG -ARG MACHINE_EMULATOR_VERSION=0.0.0 -ARG TARGETARCH ENV CARTESI_IMAGES_PATH=/usr/share/cartesi-machine/tests/data/images ENV CARTESI_TESTS_PATH=/usr/share/cartesi-machine/tests/data/machine diff --git a/tests/Makefile b/tests/Makefile index 7dac34fe0..77cbf4b72 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -19,11 +19,9 @@ INSTALLDIR = $(PREFIX)/tests LUA_BIN ?= $(shell which lua5.4) -TOOLCHAIN_DOCKER_REPOSITORY ?= cartesi/toolchain -TOOLCHAIN_TAG ?= 0.17.0 - BUILDDIR = $(abspath build) -RISCV_PREFIX = riscv64-cartesi-linux-gnu- +RISCV_PREFIX = riscv64-unknown-elf- +RISCV_GCC_OPTS = -static -mcmodel=medany -fvisibility=hidden -ffreestanding -nostdlib -nostartfiles -I/usr/riscv64-linux-gnu/include MACHINE_EMULATOR_SRC_DIR = $(abspath ../src) @@ -97,18 +95,19 @@ build-tests-misc: misc build-tests-images: images -build-tests-uarch-with-toolchain uarch-with-toolchain riscv-arch-test-with-toolchain: TOOLCHAIN_TAG:=0.17.0-rv64ima-lp64 - +build-tests-uarch-with-toolchain uarch-with-toolchain riscv-arch-test-with-toolchain: machine uarch misc: @$(MAKE) $(BUILDDIR)/$@ @$(MAKE) -C $@ BUILDDIR=$(BUILDDIR)/$@ -riscv-tests: $(BUILDDIR)/riscv-tests $(BUILDDIR)/machine +riscv-tests: $(BUILDDIR)/riscv-tests/Makefile | $(BUILDDIR)/machine @cd $(BUILDDIR)/$@ && ../../../third-party/riscv-tests/configure - @$(MAKE) -C $(BUILDDIR)/$@ RISCV_PREFIX=$(RISCV_PREFIX) isa + @$(MAKE) -C $(BUILDDIR)/$@ RISCV_PREFIX=$(RISCV_PREFIX) RISCV_GCC_OPTS="$(RISCV_GCC_OPTS)" isa @cp -a $(BUILDDIR)/riscv-tests/isa/*.bin $(BUILDDIR)/riscv-tests/isa/*.dump $(BUILDDIR)/machine @cd $(BUILDDIR)/riscv-tests/isa && find . -maxdepth 1 -type f ! -name "*.*" -exec cp -a {} $(BUILDDIR)/machine/{}.elf \; - @rm -rf $(BUILDDIR)/riscv-tests + +$(BUILDDIR)/riscv-tests/Makefile: | $(BUILDDIR)/riscv-tests + @cd $(BUILDDIR)/riscv-tests && ../../../third-party/riscv-tests/configure riscv-arch-test: @$(MAKE) $(BUILDDIR)/uarch-$@ @@ -144,7 +143,7 @@ toolchain-env: -e GID=$$(id -g) \ -v `pwd`/../:/usr/src/emulator \ -w /usr/src/emulator/tests \ - $(TOOLCHAIN_DOCKER_REPOSITORY):$(TOOLCHAIN_TAG) /bin/bash + cartesi/machine-emulator:toolchain /bin/bash toolchain-exec: @docker run --hostname toolchain-env --rm \ @@ -154,7 +153,7 @@ toolchain-exec: -e GID=$$(id -g) \ -v `pwd`/../:/usr/src/emulator \ -w /usr/src/emulator/tests \ - $(TOOLCHAIN_DOCKER_REPOSITORY):$(TOOLCHAIN_TAG) $(CONTAINER_COMMAND) + cartesi/machine-emulator:toolchain $(CONTAINER_COMMAND) emulator-builder-exec: @docker run --hostname toolchain-env --rm \ @@ -167,7 +166,7 @@ emulator-builder-exec: cartesi/machine-emulator:builder $(CONTAINER_COMMAND) $(TARGETS_WITH_TOOLCHAIN): - $(MAKE) toolchain-exec CONTAINER_COMMAND="make -j\$$(nproc) $(subst -with-toolchain,,$@)" TOOLCHAIN_DOCKER_REPOSITORY=$(TOOLCHAIN_DOCKER_REPOSITORY) TOOLCHAIN_TAG=$(TOOLCHAIN_TAG) + $(MAKE) toolchain-exec CONTAINER_COMMAND="make -j\$$(nproc) $(subst -with-toolchain,,$@)" build-tests-misc-with-builder-image: $(MAKE) emulator-builder-exec CONTAINER_COMMAND="make -j\$$(nproc) build-tests-misc" diff --git a/tests/lua/cartesi-machine-tests.lua b/tests/lua/cartesi-machine-tests.lua index aa9c1a1a2..b611df2eb 100755 --- a/tests/lua/cartesi-machine-tests.lua +++ b/tests/lua/cartesi-machine-tests.lua @@ -67,25 +67,25 @@ local riscv_tests = { { "rv64ua-p-amoxor_d.bin", 102 }, { "rv64ua-p-amoxor_w.bin", 104 }, { "rv64ua-p-lrsc.bin", 6276 }, - { "rv64ua-v-amoadd_d.bin", 9373 }, - { "rv64ua-v-amoadd_w.bin", 9370 }, - { "rv64ua-v-amoand_d.bin", 9382 }, - { "rv64ua-v-amoand_w.bin", 9381 }, - { "rv64ua-v-amomax_d.bin", 9363 }, - { "rv64ua-v-amomax_w.bin", 9363 }, - { "rv64ua-v-amomaxu_d.bin", 9363 }, - { "rv64ua-v-amomaxu_w.bin", 9363 }, - { "rv64ua-v-amomin_d.bin", 9363 }, - { "rv64ua-v-amomin_w.bin", 9363 }, - { "rv64ua-v-amominu_d.bin", 9369 }, - { "rv64ua-v-amominu_w.bin", 9369 }, - { "rv64ua-v-amoor_d.bin", 9362 }, - { "rv64ua-v-amoor_w.bin", 9362 }, - { "rv64ua-v-amoswap_d.bin", 9382 }, - { "rv64ua-v-amoswap_w.bin", 9381 }, - { "rv64ua-v-amoxor_d.bin", 9365 }, - { "rv64ua-v-amoxor_w.bin", 9367 }, - { "rv64ua-v-lrsc.bin", 15539 }, + { "rv64ua-v-amoadd_d.bin", 12908 }, + { "rv64ua-v-amoadd_w.bin", 12905 }, + { "rv64ua-v-amoand_d.bin", 12917 }, + { "rv64ua-v-amoand_w.bin", 12916 }, + { "rv64ua-v-amomax_d.bin", 12898 }, + { "rv64ua-v-amomax_w.bin", 12898 }, + { "rv64ua-v-amomaxu_d.bin", 12898 }, + { "rv64ua-v-amomaxu_w.bin", 12898 }, + { "rv64ua-v-amomin_d.bin", 12898 }, + { "rv64ua-v-amomin_w.bin", 12898 }, + { "rv64ua-v-amominu_d.bin", 12904 }, + { "rv64ua-v-amominu_w.bin", 12904 }, + { "rv64ua-v-amoor_d.bin", 12897 }, + { "rv64ua-v-amoor_w.bin", 12897 }, + { "rv64ua-v-amoswap_d.bin", 12917 }, + { "rv64ua-v-amoswap_w.bin", 12916 }, + { "rv64ua-v-amoxor_d.bin", 12900 }, + { "rv64ua-v-amoxor_w.bin", 12902 }, + { "rv64ua-v-lrsc.bin", 19074 }, { "rv64ui-p-add.bin", 505 }, { "rv64ui-p-addi.bin", 280 }, { "rv64ui-p-addiw.bin", 277 }, @@ -137,57 +137,57 @@ local riscv_tests = { { "rv64ui-p-subw.bin", 492 }, { "rv64ui-p-xor.bin", 608 }, { "rv64ui-p-xori.bin", 242 }, - { "rv64ui-v-add.bin", 6712 }, - { "rv64ui-v-addi.bin", 6487 }, - { "rv64ui-v-addiw.bin", 6484 }, - { "rv64ui-v-addw.bin", 6707 }, - { "rv64ui-v-and.bin", 6787 }, - { "rv64ui-v-andi.bin", 6458 }, - { "rv64ui-v-auipc.bin", 6300 }, - { "rv64ui-v-beq.bin", 6533 }, - { "rv64ui-v-bge.bin", 6551 }, - { "rv64ui-v-bgeu.bin", 6641 }, - { "rv64ui-v-blt.bin", 6533 }, - { "rv64ui-v-bltu.bin", 6619 }, - { "rv64ui-v-bne.bin", 6533 }, - { "rv64ui-v-fence_i.bin", 9630 }, - { "rv64ui-v-jal.bin", 6297 }, - { "rv64ui-v-jalr.bin", 6357 }, - { "rv64ui-v-lb.bin", 11201 }, - { "rv64ui-v-lbu.bin", 11201 }, - { "rv64ui-v-ld.bin", 11383 }, - { "rv64ui-v-lh.bin", 11217 }, - { "rv64ui-v-lhu.bin", 11226 }, - { "rv64ui-v-lui.bin", 6307 }, - { "rv64ui-v-lw.bin", 11231 }, - { "rv64ui-v-lwu.bin", 11265 }, - { "rv64ui-v-or.bin", 6820 }, - { "rv64ui-v-ori.bin", 6451 }, - { "rv64ui-v-sb.bin", 9752 }, - { "rv64ui-v-sd.bin", 14630 }, - { "rv64ui-v-sh.bin", 9805 }, - { "rv64ui-v-simple.bin", 6283 }, - { "rv64ui-v-sll.bin", 11488 }, - { "rv64ui-v-slli.bin", 6512 }, - { "rv64ui-v-slliw.bin", 6519 }, - { "rv64ui-v-sllw.bin", 11488 }, - { "rv64ui-v-slt.bin", 6701 }, - { "rv64ui-v-slti.bin", 6479 }, - { "rv64ui-v-sltiu.bin", 6479 }, - { "rv64ui-v-sltu.bin", 6718 }, - { "rv64ui-v-sra.bin", 6754 }, - { "rv64ui-v-srai.bin", 6500 }, - { "rv64ui-v-sraiw.bin", 6546 }, - { "rv64ui-v-sraw.bin", 11500 }, - { "rv64ui-v-srl.bin", 11502 }, - { "rv64ui-v-srli.bin", 6521 }, - { "rv64ui-v-srliw.bin", 6528 }, - { "rv64ui-v-srlw.bin", 11494 }, - { "rv64ui-v-sub.bin", 6703 }, - { "rv64ui-v-subw.bin", 6699 }, - { "rv64ui-v-sw.bin", 9812 }, - { "rv64ui-v-xor.bin", 6815 }, - { "rv64ui-v-xori.bin", 6449 }, + { "rv64ui-v-add.bin", 7926 }, + { "rv64ui-v-addi.bin", 7701 }, + { "rv64ui-v-addiw.bin", 7698 }, + { "rv64ui-v-addw.bin", 7921 }, + { "rv64ui-v-and.bin", 8001 }, + { "rv64ui-v-andi.bin", 7672 }, + { "rv64ui-v-auipc.bin", 7514 }, + { "rv64ui-v-beq.bin", 7747 }, + { "rv64ui-v-bge.bin", 7765 }, + { "rv64ui-v-bgeu.bin", 7855 }, + { "rv64ui-v-blt.bin", 7747 }, + { "rv64ui-v-bltu.bin", 7833 }, + { "rv64ui-v-bne.bin", 7747 }, + { "rv64ui-v-fence_i.bin", 13165 }, + { "rv64ui-v-jal.bin", 7511 }, + { "rv64ui-v-jalr.bin", 7571 }, + { "rv64ui-v-lb.bin", 13575 }, + { "rv64ui-v-lbu.bin", 13575 }, + { "rv64ui-v-ld.bin", 13757 }, + { "rv64ui-v-lh.bin", 13591 }, + { "rv64ui-v-lhu.bin", 13600 }, + { "rv64ui-v-lui.bin", 7521 }, + { "rv64ui-v-lw.bin", 13605 }, + { "rv64ui-v-lwu.bin", 13639 }, + { "rv64ui-v-or.bin", 8034 }, + { "rv64ui-v-ori.bin", 7665 }, + { "rv64ui-v-sb.bin", 13287 }, + { "rv64ui-v-sd.bin", 19325 }, + { "rv64ui-v-sh.bin", 13340 }, + { "rv64ui-v-simple.bin", 7497 }, + { "rv64ui-v-sll.bin", 7996 }, + { "rv64ui-v-slli.bin", 7726 }, + { "rv64ui-v-slliw.bin", 7733 }, + { "rv64ui-v-sllw.bin", 7996 }, + { "rv64ui-v-slt.bin", 7915 }, + { "rv64ui-v-slti.bin", 7693 }, + { "rv64ui-v-sltiu.bin", 7693 }, + { "rv64ui-v-sltu.bin", 7932 }, + { "rv64ui-v-sra.bin", 7968 }, + { "rv64ui-v-srai.bin", 7714 }, + { "rv64ui-v-sraiw.bin", 7760 }, + { "rv64ui-v-sraw.bin", 8008 }, + { "rv64ui-v-srl.bin", 8010 }, + { "rv64ui-v-srli.bin", 7735 }, + { "rv64ui-v-srliw.bin", 7742 }, + { "rv64ui-v-srlw.bin", 8002 }, + { "rv64ui-v-sub.bin", 7917 }, + { "rv64ui-v-subw.bin", 7913 }, + { "rv64ui-v-sw.bin", 13347 }, + { "rv64ui-v-xor.bin", 8029 }, + { "rv64ui-v-xori.bin", 7663 }, { "rv64um-p-div.bin", 136 }, { "rv64um-p-divu.bin", 142 }, { "rv64um-p-divuw.bin", 134 }, @@ -201,22 +201,22 @@ local riscv_tests = { { "rv64um-p-remu.bin", 136 }, { "rv64um-p-remuw.bin", 131 }, { "rv64um-p-remw.bin", 137 }, - { "rv64um-v-div.bin", 6343 }, - { "rv64um-v-divu.bin", 6349 }, - { "rv64um-v-divuw.bin", 6341 }, - { "rv64um-v-divw.bin", 6338 }, - { "rv64um-v-mul.bin", 6702 }, - { "rv64um-v-mulh.bin", 6710 }, - { "rv64um-v-mulhsu.bin", 6710 }, - { "rv64um-v-mulhu.bin", 6742 }, - { "rv64um-v-mulw.bin", 6641 }, - { "rv64um-v-rem.bin", 6342 }, - { "rv64um-v-remu.bin", 6343 }, - { "rv64um-v-remuw.bin", 6338 }, - { "rv64um-v-remw.bin", 6344 }, + { "rv64um-v-div.bin", 7557 }, + { "rv64um-v-divu.bin", 7563 }, + { "rv64um-v-divuw.bin", 7555 }, + { "rv64um-v-divw.bin", 7552 }, + { "rv64um-v-mul.bin", 7916 }, + { "rv64um-v-mulh.bin", 7924 }, + { "rv64um-v-mulhsu.bin", 7924 }, + { "rv64um-v-mulhu.bin", 7956 }, + { "rv64um-v-mulw.bin", 7855 }, + { "rv64um-v-rem.bin", 7556 }, + { "rv64um-v-remu.bin", 7557 }, + { "rv64um-v-remuw.bin", 7552 }, + { "rv64um-v-remw.bin", 7558 }, -- C extension tests { "rv64uc-p-rvc.bin", 295 }, - { "rv64uc-v-rvc.bin", 14280 }, + { "rv64uc-v-rvc.bin", 18975 }, -- float tests { "rv64uf-p-fadd.bin", 210 }, { "rv64uf-p-fclass.bin", 147 }, @@ -229,17 +229,17 @@ local riscv_tests = { { "rv64uf-p-ldst.bin", 106 }, { "rv64uf-p-move.bin", 255 }, { "rv64uf-p-recoding.bin", 113 }, - { "rv64uf-v-fadd.bin", 11121 }, - { "rv64uf-v-fclass.bin", 6352 }, - { "rv64uf-v-fcmp.bin", 11171 }, - { "rv64uf-v-fcvt.bin", 11063 }, - { "rv64uf-v-fcvt_w.bin", 16167 }, - { "rv64uf-v-fdiv.bin", 11082 }, - { "rv64uf-v-fmadd.bin", 11147 }, - { "rv64uf-v-fmin.bin", 11225 }, - { "rv64uf-v-ldst.bin", 9401 }, - { "rv64uf-v-move.bin", 6460 }, - { "rv64uf-v-recoding.bin", 11024 }, + { "rv64uf-v-fadd.bin", 13495 }, + { "rv64uf-v-fclass.bin", 7566 }, + { "rv64uf-v-fcmp.bin", 13545 }, + { "rv64uf-v-fcvt.bin", 13437 }, + { "rv64uf-v-fcvt_w.bin", 19701 }, + { "rv64uf-v-fdiv.bin", 13456 }, + { "rv64uf-v-fmadd.bin", 13521 }, + { "rv64uf-v-fmin.bin", 13599 }, + { "rv64uf-v-ldst.bin", 12936 }, + { "rv64uf-v-move.bin", 7674 }, + { "rv64uf-v-recoding.bin", 13398 }, { "rv64ud-p-fadd.bin", 210 }, { "rv64ud-p-fclass.bin", 153 }, { "rv64ud-p-fcmp.bin", 260 }, @@ -252,18 +252,18 @@ local riscv_tests = { { "rv64ud-p-move.bin", 1030 }, { "rv64ud-p-recoding.bin", 138 }, { "rv64ud-p-structural.bin", 203 }, - { "rv64ud-v-fadd.bin", 11121 }, - { "rv64ud-v-fclass.bin", 6358 }, - { "rv64ud-v-fcmp.bin", 11171 }, - { "rv64ud-v-fcvt.bin", 11103 }, - { "rv64ud-v-fcvt_w.bin", 16227 }, - { "rv64ud-v-fdiv.bin", 11095 }, - { "rv64ud-v-fmadd.bin", 11147 }, - { "rv64ud-v-fmin.bin", 11225 }, - { "rv64ud-v-ldst.bin", 9396 }, - { "rv64ud-v-move.bin", 11941 }, - { "rv64ud-v-recoding.bin", 9439 }, - { "rv64ud-v-structural.bin", 6408 }, + { "rv64ud-v-fadd.bin", 13495 }, + { "rv64ud-v-fclass.bin", 7572 }, + { "rv64ud-v-fcmp.bin", 13545 }, + { "rv64ud-v-fcvt.bin", 13477 }, + { "rv64ud-v-fcvt_w.bin", 19761 }, + { "rv64ud-v-fdiv.bin", 13469 }, + { "rv64ud-v-fmadd.bin", 13521 }, + { "rv64ud-v-fmin.bin", 13599 }, + { "rv64ud-v-ldst.bin", 12931 }, + { "rv64ud-v-move.bin", 14315 }, + { "rv64ud-v-recoding.bin", 12974 }, + { "rv64ud-v-structural.bin", 7622 }, { "fclass.bin", 453 }, { "fcvt.bin", 17610 }, { "fcmp.bin", 46783 }, diff --git a/tests/machine/Makefile b/tests/machine/Makefile index 8b6bfc2f9..9ae962a12 100644 --- a/tests/machine/Makefile +++ b/tests/machine/Makefile @@ -21,7 +21,7 @@ SRCDIR := $(abspath src) BUILDDIR ?= $(abspath build) SRCCLEAN := $(addsuffix .clean,$(SRCDIR)) -RISCV_PREFIX = riscv64-cartesi-linux-gnu- +RISCV_PREFIX = riscv64-unknown-elf- RVCC = $(RISCV_PREFIX)gcc RVCXX = $(RISCV_PREFIX)g++ RVCOPY = $(RISCV_PREFIX)objcopy diff --git a/tests/machine/README.md b/tests/machine/README.md index a1e706536..ae338abd2 100644 --- a/tests/machine/README.md +++ b/tests/machine/README.md @@ -8,7 +8,6 @@ The Cartesi Machine Tests is a repository containing RISC-V testing code. - RISCV64 C/C++ Compiler with support for C++20 (tested with GCC >= 8+). - GNU Make >= 3.81 -- Docker image `cartesi/toolchain` ### Build diff --git a/tests/machine/src/Makefile b/tests/machine/src/Makefile index 37cc2af37..4c68fdc78 100644 --- a/tests/machine/src/Makefile +++ b/tests/machine/src/Makefile @@ -17,7 +17,7 @@ BUILDDIR ?= $(abspath ../build) RISCV_TESTS_DIR ?= $(abspath ../../../third-party/riscv-tests) -RISCV_PREFIX = riscv64-cartesi-linux-gnu- +RISCV_PREFIX = riscv64-unknown-elf- CC = $(RISCV_PREFIX)gcc CXX = $(RISCV_PREFIX)g++ OBJCOPY = $(RISCV_PREFIX)objcopy @@ -30,7 +30,7 @@ INCS=\ -I$(RISCV_TESTS_DIR)/isa/macros/scalar \ -I$(abspath ../../../src) -CFLAGS=-g -march=rv64g -mabi=lp64d -static -mcmodel=medany -fvisibility=hidden -nostdlib -nostartfiles $(INCS) +CFLAGS=-g -march=rv64g -mabi=lp64d -static -mcmodel=medany -fvisibility=hidden -ffreestanding -nostdlib -nostartfiles $(INCS) CXXFLAGS=-fno-exceptions $(CFLAGS) SRCS := $(wildcard *.S) diff --git a/tests/uarch/Makefile b/tests/uarch/Makefile index 681af3a33..eeea1c6d3 100644 --- a/tests/uarch/Makefile +++ b/tests/uarch/Makefile @@ -19,7 +19,7 @@ INSTALLDIR = $(PREFIX)/tests UARCH_ENV_DIR := $(abspath .) EMULATOR_SRC_DIR := $(abspath ../../src) -RISCV_PREFIX ?= riscv64-cartesi-linux-gnu- +RISCV_PREFIX ?= riscv64-unknown-elf- RISCV_GCC ?= $(RISCV_PREFIX)gcc RISCV_GCC_OPTS ?= -static -march=rv64i -mabi=lp64 -mcmodel=medany -fvisibility=hidden -nostdlib -nostartfiles RISCV_OBJDUMP ?= $(RISCV_PREFIX)objdump --disassemble-all --disassemble-zeroes --section=.text --section=.text.startup --section=.text.init --section=.data @@ -84,14 +84,14 @@ clean: catalog: cp rv64ui-uarch-catalog.json $(BUILDDIR)/ -$(TARGET_THIRDPARTY_TESTS): $(BUILDDIR) $(THIRDPARTY_TESTS_SOURCES) +$(TARGET_THIRDPARTY_TESTS): $(THIRDPARTY_TESTS_SOURCES) | $(BUILDDIR) $(RISCV_GCC) $(RISCV_GCC_OPTS) -I$(UARCH_ENV_DIR) \ -I$(THIRDPARTY_SRC_DIR)/../macros/scalar \ -I$(EMULATOR_SRC_DIR) \ -T$(UARCH_ENV_DIR)/link.ld \ $(subst $(TESTS_PREFIX),$(THIRDPARTY_SRC_DIR)/,$@).S -o $@ -$(TARGET_TESTS): $(BUILDDIR) $(TESTS_SOURCES) +$(TARGET_TESTS): $(TESTS_SOURCES) | $(BUILDDIR) $(RISCV_GCC) $(RISCV_GCC_OPTS) -I$(UARCH_ENV_DIR) \ -I$(THIRDPARTY_SRC_DIR)/../macros/scalar \ -I$(EMULATOR_SRC_DIR) \ diff --git a/tools/docker-entrypoint.sh b/tools/docker-entrypoint.sh new file mode 100644 index 000000000..2c401e674 --- /dev/null +++ b/tools/docker-entrypoint.sh @@ -0,0 +1,56 @@ +#!/bin/bash + +# 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. +# + +if [ -z "$GID" -o -z "$UID" -o -z "$USER" -o -z "$GROUP" ]; then + echo Running as $(whoami) + exec "$@" +else + if [ ! $(getent group $GID) ]; then + if [ $(getent group $GROUP) ]; then + echo Group name $GROUP already exists + GROUP=container-group-$GID + fi + groupadd -g $GID $GROUP + else + echo The id $GID of group $GROUP already exists + fi + if [ ! $(getent passwd $UID) ]; then + if [ $(getent passwd $USER) ]; then + echo User name $USER already exists. + USER=container-user-$UID + fi + useradd -u $UID -g $GID -G $GROUP $USER + else + echo The id $UID of user $USER already exists + fi + USERNAME=$(id -nu $UID) + export HOME=/home/$USERNAME + mkdir -p $HOME + chown $UID:$GID $HOME + +# Workaround for issue with su-exec tty ownership +# Should be removed once ticket https://github.com/ncopa/su-exec/issues/33 +# is resolved, or alternative solution with reusing file descriptors is found +# Test if stdin is associated with a terminal + if [ -t 0 ]; then + chown $UID:$GID $(/usr/bin/tty) + fi + + echo Running as $USERNAME and group $(id -ng $UID) + exec /usr/local/bin/su-exec $USERNAME "$@" +fi diff --git a/tools/gnu/stubs-lp64.h b/tools/gnu/stubs-lp64.h new file mode 100644 index 000000000..6ce02418e --- /dev/null +++ b/tools/gnu/stubs-lp64.h @@ -0,0 +1,38 @@ +/* This file is automatically generated. + It defines a symbol `__stub_FUNCTION' for each function + in the C library which is a stub, meaning it will fail + every time called, usually setting errno to ENOSYS. */ + +#ifdef _LIBC + #error Applications may not define the macro _LIBC +#endif + +#define __stub___compat_bdflush +#define __stub___compat_create_module +#define __stub___compat_get_kernel_syms +#define __stub___compat_query_module +#define __stub___compat_uselib +#define __stub_chflags +#define __stub_fchflags +#define __stub_feclearexcept +#define __stub_fedisableexcept +#define __stub_feenableexcept +#define __stub_fegetenv +#define __stub_fegetexcept +#define __stub_fegetexceptflag +#define __stub_fegetmode +#define __stub_fegetround +#define __stub_feholdexcept +#define __stub_feraiseexcept +#define __stub_fesetenv +#define __stub_fesetexcept +#define __stub_fesetexceptflag +#define __stub_fesetmode +#define __stub_fesetround +#define __stub_fetestexcept +#define __stub_feupdateenv +#define __stub_gtty +#define __stub_revoke +#define __stub_setlogin +#define __stub_sigreturn +#define __stub_stty \ No newline at end of file diff --git a/uarch/.gitignore b/uarch/.gitignore index 6717e095a..d5e9728b7 100644 --- a/uarch/.gitignore +++ b/uarch/.gitignore @@ -8,3 +8,5 @@ uarch-pristine-hash.c uarch-pristine-ram.c compute-uarch-pristine-hash +*.insn.txt +*.objdump \ No newline at end of file diff --git a/uarch/Makefile b/uarch/Makefile index fe55bd9ab..cd80e391c 100644 --- a/uarch/Makefile +++ b/uarch/Makefile @@ -1,17 +1,20 @@ -TOOLCHAIN_PREFIX ?= riscv64-cartesi-linux-gnu- +TOOLCHAIN_PREFIX ?= riscv64-unknown-elf- +TOOLCHAIN_LIBS = -lgcc + +ifeq ($(DEV_ENV_HAS_TOOLCHAIN),yes) +TOOLCHAIN_INCS=\ + -I/usr/riscv64-linux-gnu/include/c++/12 \ + -I/usr/riscv64-linux-gnu/include/c++/12/riscv64-linux-gnu \ + -I/usr/riscv64-linux-gnu/include +else +TOOLCHAIN_INCS= +endif EMULATOR_SRC_DIR = ../src THIRD_PARTY_DIR := ../third-party -BOOST_INC_DIR = /usr/include/boost ifeq ($(UNAME),Darwin) HOST_CXX := clang++ -# Homebrew installation -ifneq (,$(wildcard /usr/local/opt/boost/lib)) -BOOST_INC_DIR =/usr/local/opt/boost/include -else # Macports installation -BOOST_INC_DIR = /opt/local/include -endif else HOST_CXX := g++ endif @@ -24,6 +27,11 @@ CXX := $(TOOLCHAIN_PREFIX)g++ OBJCOPY := $(TOOLCHAIN_PREFIX)objcopy OBJDUMP := $(TOOLCHAIN_PREFIX)objdump +# Instructions supported by the microarchitecture interpreter (rv64ui) +SUPPORTED_UARCH_INSN := add|addi|addiw|addw|and|andi|auipc|beq|bge|bgeu|blt|bltu|bne|ebreak|ecall|fence|jal|jalr|lb|lbu|ld|lh|$\ + lhu|lui|lw|lwu|or|ori|sb|sd|sh|sll|slli|slliw|sllw|slt|slti|sltiu|sltu|sra|srai|sraiw|sraw|srl|srli|srliw|srlw|sub|$\ + subw|sw|xor|xori + OPTFLAGS=-O2 -g0 # Asserts are always enabled by now, but in the far future we should disable them @@ -45,7 +53,7 @@ CFLAGS := -march=rv64i -mabi=lp64 -Wl,--gc-sections $(OPTFLAGS) $(UBFLAGS) $(WAR -I. \ -I$(THIRD_PARTY_DIR)/llvm-flang-uint128 \ -I$(EMULATOR_SRC_DIR) \ - -I$(BOOST_INC_DIR) + $(TOOLCHAIN_INCS) CXXFLAGS := -std=c++20 -fno-rtti @@ -89,9 +97,9 @@ CLANG_TIDY_HEADER_FILTER=$(CURDIR)/($(subst $(SPACE),|,$(LINTER_HEADERS))) CLANG_TIDY=clang-tidy CLANG_TIDY_TARGETS=$(patsubst %.cpp,%.clang-tidy,$(patsubst %.c,%.clang-tidy,$(LINTER_SOURCES))) -.PHONY: all clean +.PHONY: all clean validate-instruction-set -all: uarch-ram.bin uarch-pristine-ram.c uarch-pristine-hash.c +all: uarch-ram.bin uarch-pristine-ram.c uarch-pristine-hash.c validate-instruction-set compute-uarch-pristine-hash: $(COMPUTE_UARCH_CPP_SOURCES) $(COMPUTE_UARCH_C_SOURCES) $(HOST_CXX) $(HOST_CFLAGS) -o $@ -x c $(COMPUTE_UARCH_C_SOURCES) -x c++ $(COMPUTE_UARCH_CPP_SOURCES) @@ -107,8 +115,21 @@ uarch-pristine-ram.c: uarch-ram.bin uarch-ram.bin: uarch-ram.elf $(OBJCOPY) -S -O binary $^ $@ +uarch-ram.elf.insn.txt: uarch-ram.elf.objdump + grep -oP '^\s*[0-9a-f]{4,8}\:\s+[0-9a-f]{4,8}\s+\K[a-z]\S+' $(^F) | sort -u > $@ + +uarch-ram.elf.objdump: uarch-ram.elf + @$(OBJDUMP) -M no-aliases -d $(^F) > $@ + +validate-instruction-set: uarch-ram.elf.insn.txt + @grep -v -w -E "$(SUPPORTED_UARCH_INSN)" $(