Skip to content

Commit

Permalink
feat: forward application exit status
Browse files Browse the repository at this point in the history
  • Loading branch information
mpolitzer committed Sep 22, 2023
1 parent 0d3ee9e commit e62522c
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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} && \
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand Down
2 changes: 2 additions & 0 deletions linux/xhalt/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
extra/halt
halt
77 changes: 77 additions & 0 deletions linux/xhalt/Makefile
Original file line number Diff line number Diff line change
@@ -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
41 changes: 41 additions & 0 deletions linux/xhalt/README.md
Original file line number Diff line number Diff line change
@@ -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.
9 changes: 9 additions & 0 deletions linux/xhalt/xhalt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <stdio.h>
#include <unistd.h>
#include <syscall.h>
#include <linux/reboot.h>

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);
}
4 changes: 3 additions & 1 deletion skel/opt/cartesi/bin/init
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit e62522c

Please sign in to comment.