From e62522c5a166c9466d1e90b015d57240743e9e60 Mon Sep 17 00:00:00 2001 From: Marcelo Politzer <251334+mpolitzer@users.noreply.github.com> Date: Thu, 24 Aug 2023 15:51:38 -0300 Subject: [PATCH] feat: forward application exit status --- Dockerfile | 2 + Makefile | 2 +- linux/xhalt/.gitignore | 2 + linux/xhalt/Makefile | 77 +++++++++++++++++++++++++++++++++++++++ linux/xhalt/README.md | 41 +++++++++++++++++++++ linux/xhalt/xhalt.c | 9 +++++ skel/opt/cartesi/bin/init | 4 +- 7 files changed, 135 insertions(+), 2 deletions(-) create mode 100644 linux/xhalt/.gitignore create mode 100644 linux/xhalt/Makefile create mode 100644 linux/xhalt/README.md create mode 100644 linux/xhalt/xhalt.c diff --git a/Dockerfile b/Dockerfile index 477e7569..0f833da3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -62,6 +62,7 @@ COPY linux/ ${BUILD_BASE}tools/linux/ # build C/C++ tools # ------------------------------------------------------------------------------ +RUN make -C ${BUILD_BASE}tools/linux/xhalt/ CROSS_COMPILE="" xhalt.toolchain RUN make -C ${BUILD_BASE}tools/linux/htif/ CROSS_COMPILE="" yield.toolchain RUN make -C ${BUILD_BASE}tools/linux/rollup/ioctl-echo-loop/ CROSS_COMPILE="" ioctl-echo-loop.toolchain RUN make -C ${BUILD_BASE}tools/linux/rollup/rollup/ CROSS_COMPILE="" rollup.toolchain @@ -87,6 +88,7 @@ ARG MACHINE_EMULATOR_TOOLS_TAR_GZ=machine-emulator-tools.tar.gz COPY skel/ ${STAGING_BASE} RUN mkdir -p ${STAGING_BIN} && \ cp ${BUILD_BASE}twuewand/rndaddentropy/rndaddentropy ${STAGING_BIN} && \ + cp ${BUILD_BASE}tools/linux/xhalt/xhalt ${STAGING_BIN} && \ cp ${BUILD_BASE}tools/linux/htif/yield ${STAGING_BIN} && \ cp ${BUILD_BASE}tools/linux/rollup/ioctl-echo-loop/ioctl-echo-loop ${STAGING_BIN} && \ cp ${BUILD_BASE}tools/linux/rollup/rollup/rollup ${STAGING_BIN} && \ diff --git a/Makefile b/Makefile index 87a6ec2d..61465640 100644 --- a/Makefile +++ b/Makefile @@ -38,7 +38,7 @@ SHASUMFILES := $(LINUX_SOURCES_FILEPATH) $(RNDADDENTROPY_FILEPATH) all: build copy build: Dockerfile checksum - docker buildx build --platform=linux/riscv64 \ + docker buildx build --platform=linux/riscv64 --load \ --build-arg MACHINE_EMULATOR_TOOLS_TAR_GZ=$(MACHINE_EMULATOR_TOOLS_TAR_GZ) \ --build-arg MACHINE_EMULATOR_TOOLS_DEB=$(MACHINE_EMULATOR_TOOLS_DEB) \ --build-arg LINUX_SOURCES_VERSION=$(LINUX_SOURCES_VERSION) \ diff --git a/linux/xhalt/.gitignore b/linux/xhalt/.gitignore new file mode 100644 index 00000000..99593ec3 --- /dev/null +++ b/linux/xhalt/.gitignore @@ -0,0 +1,2 @@ +extra/halt +halt diff --git a/linux/xhalt/Makefile b/linux/xhalt/Makefile new file mode 100644 index 00000000..76481a66 --- /dev/null +++ b/linux/xhalt/Makefile @@ -0,0 +1,77 @@ +# 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. +# + +UNAME:=$(shell uname) + +TOOLCHAIN_IMAGE ?= cartesi/toolchain +TOOLCHAIN_TAG ?= 0.15.0 +RISCV_ARCH ?= rv64gc +RISCV_ABI ?= lp64d + +CROSS_COMPILE = riscv64-cartesi-linux-gnu- +RVCC = $(CROSS_COMPILE)gcc +RVCXX = $(CROSS_COMPILE)g++ +RVCOPY = $(CROSS_COMPILE)objcopy +RVDUMP = $(CROSS_COMPILE)objdump +STRIP = $(CROSS_COMPILE)strip +RISCV_CFLAGS :=-march=$(RISCV_ARCH) -mabi=$(RISCV_ABI) + +CONTAINER_MAKE := /usr/bin/make +CONTAINER_BASE := /opt/cartesi/tools +KERNEL_HEADERS_PATH := /opt/riscv/usr/include + +all: xhalt + +xhalt: xhalt.c + $(MAKE) toolchain-exec CONTAINER_COMMAND="$(CONTAINER_MAKE) $@.toolchain" + +extra.ext2: xhalt + $(MAKE) toolchain-exec CONTAINER_COMMAND="$(CONTAINER_MAKE) $@.toolchain" + +xhalt.toolchain: + $(RVCC) -O2 -o xhalt xhalt.c + $(STRIP) xhalt + +extra.ext2.toolchain: + mkdir -m 0755 ./extra + cp ./xhalt ./extra/xhalt + genext2fs -i 512 -b 8192 -d extra $(basename $@) + rm -rf ./extra + +toolchain-exec: + @docker run --hostname $@ --rm \ + -e USER=$$(id -u -n) \ + -e GROUP=$$(id -g -n) \ + -e UID=$$(id -u) \ + -e GID=$$(id -g) \ + -v `pwd`:$(CONTAINER_BASE) \ + -w $(CONTAINER_BASE) \ + $(TOOLCHAIN_IMAGE):$(TOOLCHAIN_TAG) $(CONTAINER_COMMAND) + +toolchain-env: + @docker run --hostname toolchain-env -it --rm \ + -e USER=$$(id -u -n) \ + -e GROUP=$$(id -g -n) \ + -e UID=$$(id -u) \ + -e GID=$$(id -g) \ + -v `pwd`:$(CONTAINER_BASE) \ + -w $(CONTAINER_BASE) \ + $(TOOLCHAIN_IMAGE):$(TOOLCHAIN_TAG) + +clean: + \rm -rf xhalt extra.ext2 extra + +.PHONY: toolchain-exec toolchain-env diff --git a/linux/xhalt/README.md b/linux/xhalt/README.md new file mode 100644 index 00000000..16a33a71 --- /dev/null +++ b/linux/xhalt/README.md @@ -0,0 +1,41 @@ +## HTIF xhalt tool + +### Requirements + +- Docker >= 18.x +- GNU Make >= 3.81 + +### Building + +```bash +$ cd linux/htif +$ make +``` + +#### Makefile targets + +The following options are available as `make` targets: + +- **all**: builds the RISC-V xhalt executable +- **extra.ext2**: builds the extra.ext2 filesystem image with the xhalt tool inside +- **toolchain-env**: runs the toolchain image with current user UID and GID +- **clean**: clean generated artifacts + +#### Makefile container options + +You can pass the following variables to the make target if you wish to use different docker image tags. + +- TOOLCHAIN\_IMAGE: toolchain image name +- TOOLCHAIN\_TAG: toolchain image tag + +``` +$ make TOOLCHAIN_TAG=mytag +``` + +It's useful when you want to use prebuilt images like `cartesi/toolchain:latest` + +#### Usage + +The purpose of the xhalt tool please see the emulator documentation. + +The purpose of the `extra.ext2` image is to help the development creating a filesystem that contains the xhalt tool so it can be used with the emulator. For instructions on how to do that, please see the emulator documentation. diff --git a/linux/xhalt/xhalt.c b/linux/xhalt/xhalt.c new file mode 100644 index 00000000..4a01e3d3 --- /dev/null +++ b/linux/xhalt/xhalt.c @@ -0,0 +1,9 @@ +#include +#include +#include +#include + +int main(int argc, char *argv[]) { + const char *halt = argc > 1? argv[1] : "0"; + syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, halt); +} diff --git a/skel/opt/cartesi/bin/init b/skel/opt/cartesi/bin/init index d462a692..7995d1ee 100755 --- a/skel/opt/cartesi/bin/init +++ b/skel/opt/cartesi/bin/init @@ -74,10 +74,12 @@ if [ -n "$*" ]; then busybox setsid \ busybox cttyhack \ busybox su -p $USER -c "$*" + RC=$? else echo "Nothing to do." + RC=0 fi busybox mount -o ro,remount / busybox umount -af -busybox poweroff -f +[ $RC == 0 ] && busybox poweroff -f || xhalt "$RC"