Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Host Mode #67

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ jobs:
- name: Build rootfs
run: make fs

- name: Build [${{ env.TOOLS_LIBCMT }}]
- name: Build libcmt
id: docker_build_libcmt
uses: docker/build-push-action@v5
with:
Expand All @@ -85,9 +85,9 @@ jobs:
tags: ${{ env.TOOLS_IMAGE }}-libcmt
push: false
load: true
target: libcmt-debian-packager
build-args: |
TOOLS_LIBCMT=${{ env.TOOLS_LIBCMT }}
VERSION=${{ env.VERSION }}
target: libcmt-debian-packager
cache-from: type=gha,scope=regular,mode=max
cache-to: type=gha,scope=regular

Expand All @@ -101,14 +101,14 @@ jobs:
path: |
${{ env.TOOLS_DEB }}
${{ env.TOOLS_ROOTFS }}
${{ env.TOOLS_LIBCMT }}
libcmt/deb/*

- name: Checksum artifacts
if: startsWith(github.ref, 'refs/tags/v')
run: |
sha512sum ${{ env.TOOLS_DEB }} > ${{ env.TOOLS_DEB }}.sha512
sha512sum ${{ env.TOOLS_ROOTFS }} > ${{ env.TOOLS_ROOTFS }}.sha512
sha512sum ${{ env.TOOLS_LIBCMT }} > ${{ env.TOOLS_LIBCMT }}.sha512
for f in libcmt/deb/*; sha512sum $f > libcmt/$f.sha512; done

- uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/v')
Expand All @@ -119,8 +119,8 @@ jobs:
${{ env.TOOLS_DEB }}.sha512
${{ env.TOOLS_ROOTFS }}
${{ env.TOOLS_ROOTFS }}.sha512
${{ env.TOOLS_LIBCMT }}
${{ env.TOOLS_LIBCMT }}.sha512
libcmt/deb/*
libcmt/deb/*.sha512

test:
runs-on: ubuntu-latest-8-cores
Expand Down
24 changes: 19 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,26 @@ RUN make -C ${BUILD_BASE}/tools/sys-utils/ -j$(nproc) all
# ------------------------------------------------------------------------------
FROM c-builder as libcmt-debian-packager
ARG CMT_BASE=${BUILD_BASE}/tools/sys-utils/libcmt
ARG TOOLS_LIBCMT=libcmt.deb
ARG VERSION=0.0.0
USER root
RUN make -C ${CMT_BASE} debian-package \
TARGET_DESTDIR=${BUILD_BASE}/_install \
TARGET_PREFIX=/usr/riscv64-linux-gnu \
LIBCMT_DEB_FILENAME=${BUILD_BASE}/${TOOLS_LIBCMT}

RUN make -C ${CMT_BASE} \
ARG_VERSION=${VERSION} \
TARGET_PREFIX=/usr \
TARGET_DESTDIR=${BUILD_BASE}/install/run \
install-run libcmt-v${VERSION}.deb

RUN make -C ${CMT_BASE} \
ARG_VERSION=${VERSION} \
TARGET_PREFIX=/usr \
TARGET_DESTDIR=${BUILD_BASE}/install/dev \
install libcmt-dev-v${VERSION}.deb

RUN make -C ${CMT_BASE} \
ARG_VERSION=${VERSION} \
TARGET_PREFIX=/usr/riscv64-linux-gnu \
TARGET_DESTDIR=${BUILD_BASE}/install/cross \
install libcmt-dev-riscv64-cross-v${VERSION}.deb

# build rust tools
# ------------------------------------------------------------------------------
Expand Down
14 changes: 7 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ VERSION := $(MAJOR).$(MINOR).$(PATCH)$(LABEL)
TOOLS_DEB := machine-emulator-tools-v$(VERSION).deb
TOOLS_IMAGE := cartesi/machine-emulator-tools:$(VERSION)
TOOLS_ROOTFS := rootfs-tools-v$(VERSION).ext2
TOOLS_LIBCMT := libcmt-v$(VERSION)-dev.deb

IMAGE_KERNEL_VERSION ?= v0.20.0
LINUX_VERSION ?= 6.5.13-ctsi-1
Expand Down Expand Up @@ -63,25 +62,26 @@ $(TOOLS_ROOTFS) fs: $(TOOLS_DEB)
xgenext2fs -fzB 4096 -b 25600 -i 4096 -a rootfs.tar -L rootfs $(TOOLS_ROOTFS) && \
rm -f rootfs.tar

$(TOOLS_LIBCMT) libcmt:
libcmt:
@docker buildx build --load \
--target libcmt-debian-packager \
--build-arg TOOLS_LIBCMT=$(TOOLS_LIBCMT) \
--build-arg VERSION=$(VERSION) \
-t $(TOOLS_IMAGE)-libcmt \
-f Dockerfile \
.
$(MAKE) copy-libcmt

copy-libcmt:
@mkdir libcmt
@ID=`docker create $(TOOLS_IMAGE)-libcmt` && \
docker cp $$ID:/opt/cartesi/$(TOOLS_LIBCMT) . && \
docker cp $$ID:/opt/cartesi/tools/sys-utils/libcmt/build/deb/ libcmt && \
docker rm $$ID

env:
@echo VERSION=$(VERSION)
@echo TOOLS_DEB=$(TOOLS_DEB)
@echo TOOLS_ROOTFS=$(TOOLS_ROOTFS)
@echo TOOLS_IMAGE=$(TOOLS_IMAGE)
@echo TOOLS_LIBCMT=$(TOOLS_LIBCMT)
@echo IMAGE_KERNEL_VERSION=$(IMAGE_KERNEL_VERSION)
@echo LINUX_VERSION=$(LINUX_VERSION)
@echo LINUX_HEADERS_URLPATH=$(LINUX_HEADERS_URLPATH)
Expand Down Expand Up @@ -128,7 +128,7 @@ clean-image:
@(docker rmi $(TOOLS_IMAGE) > /dev/null 2>&1 || true)

clean:
@rm -f $(TOOLS_DEB) control rootfs* libcmt-*
@rm -f $(TOOLS_DEB) control rootfs* libcmt*
@$(MAKE) -C sys-utils clean

distclean: clean clean-image
Expand All @@ -146,4 +146,4 @@ help:
@echo ' env - print useful Makefile variables as a KEY=VALUE list'
@echo ' clean - remove the generated artifacts'

.PHONY: build fs deb env setup setup-required help distclean
.PHONY: build fs deb libcmt env setup setup-required help distclean
111 changes: 94 additions & 17 deletions sys-utils/libcmt/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@
# 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.

LIBCMT_VERSION ?=0.0.0

INSTALL_FILE= install -m0644
INSTALL_EXEC= install -m0755

# paths
PREFIX = /usr
TARGET_PREFIX ?= $(PREFIX)

Expand All @@ -32,8 +25,7 @@ TARGET_CFLAGS := $(COMMON_CFLAGS) -ftrivial-auto-var-init=zero -Wstrict-aliasing
CFLAGS := $(COMMON_CFLAGS)
CC := gcc

all: libcmt host
host: mock tools
all: libcmt host mock tools
#-------------------------------------------------------------------------------
examples_SRC := \
doc/examples/abi_encode_000.c \
Expand Down Expand Up @@ -65,7 +57,7 @@ libcmt_SRC := \
src/util.c \
src/io.c

libcmt_OBJDIR := build/lib
libcmt_OBJDIR := build/riscv64
libcmt_OBJ := $(patsubst %.c,$(libcmt_OBJDIR)/%.o,$(libcmt_SRC))
libcmt_LIB := $(libcmt_OBJDIR)/libcmt.a
libcmt_SO := $(libcmt_OBJDIR)/libcmt.so
Expand All @@ -81,19 +73,62 @@ $(libcmt_SO): $(libcmt_OBJ)
$(TARGET_CC) -shared -o $@ $^

libcmt: $(libcmt_LIB) $(libcmt_SO)
install-run: $(libcmt_SO)
mkdir -p $(TARGET_DESTDIR)$(TARGET_PREFIX)/lib
cp -f $(libcmt_SO) $(TARGET_DESTDIR)$(TARGET_PREFIX)/lib

install: $(libcmt_LIB) $(libcmt_SO) build/ffi.h
mkdir -p $(TARGET_DESTDIR)$(TARGET_PREFIX)/lib
cp -f $(libcmt_LIB) $(libcmt_SO) $(TARGET_DESTDIR)$(TARGET_PREFIX)/lib
cp -f $(libcmt_SO) $(libcmt_LIB) $(TARGET_DESTDIR)$(TARGET_PREFIX)/lib
mkdir -p $(TARGET_DESTDIR)$(TARGET_PREFIX)/include/libcmt/
cp -f include/libcmt/*.h $(TARGET_DESTDIR)$(TARGET_PREFIX)/include/libcmt/
cp -f build/ffi.h $(TARGET_DESTDIR)$(TARGET_PREFIX)/include/libcmt/
mkdir -p $(TARGET_DESTDIR)$(TARGET_PREFIX)/lib/pkgconfig
sed -e 's|@ARG_PREFIX@|$(TARGET_PREFIX)|g' src/libcmt.pc > $(TARGET_DESTDIR)$(TARGET_PREFIX)/lib/pkgconfig/libcmt.pc

debian-package: install
mkdir -p $(TARGET_DESTDIR)/DEBIAN
sed 's|ARG_VERSION|$(LIBCMT_VERSION)|g;' tools/template/cross-control.template > $(TARGET_DESTDIR)/DEBIAN/control
dpkg-deb -Zxz --root-owner-group --build $(TARGET_DESTDIR) $(LIBCMT_DEB_FILENAME)
sed -e 's|@PREFIX@|$(TARGET_PREFIX)|g' \
tools/libcmt.pc.in > $(TARGET_DESTDIR)$(TARGET_PREFIX)/lib/pkgconfig/libcmt.pc

# requires either install-run or install
debian-package:
mkdir -p $(dir $(DEB_FILENAME))
dpkg-deb -Zxz --root-owner-group --build $(TARGET_DESTDIR) $(DEB_FILENAME)

$(TARGET_DESTDIR)/DEBIAN/control: tools/control.in
mkdir -p $(@D)
sed -e 's|@PACKAGE@|$(ARG_PACKAGE)|' \
-e 's|@VERSION@|$(ARG_VERSION)|' \
-e 's|@ARCHITECTURE@|$(ARG_ARCHITECTURE)|' \
-e 's|@SECTION@|$(ARG_SECTION)|' \
-e 's|@PROVIDES@|$(ARG_PROVIDES)|' \
-e 's|@DESCRIPTION@|$(ARG_DESCRIPTION)|' \
$< > $@

# debian package variants
libcmt-v$(ARG_VERSION).deb:
$(MAKE) $(TARGET_DESTDIR)/DEBIAN/control \
ARG_PACKAGE=libcmt \
ARG_ARCHITECTURE=riscv64 \
ARG_PROVIDES=libcmt \
ARG_SECTION=libs \
ARG_DESCRIPTION="Cartesi Machine Tools"
$(MAKE) debian-package DEB_FILENAME=build/deb/$@

libcmt-dev-v$(ARG_VERSION).deb:
$(MAKE) $(TARGET_DESTDIR)/DEBIAN/control \
ARG_PACKAGE=libcmt-dev \
ARG_ARCHITECTURE=any \
ARG_PROVIDES=libcmt-dev \
ARG_SECTION=devel \
ARG_DESCRIPTION="Cartesi Machine Tools - (development files)"
$(MAKE) debian-package DEB_FILENAME=build/deb/$@

libcmt-dev-riscv64-cross-v$(ARG_VERSION).deb:
$(MAKE) $(TARGET_DESTDIR)/DEBIAN/control \
ARG_PACKAGE=libcmt \
ARG_ARCHITECTURE=riscv64 \
ARG_PROVIDES=libcmt-dev-riscv64-cross \
ARG_SECTION=devel \
ARG_DESCRIPTION="Cartesi Machine Tools - (development files) (riscv64)"
$(MAKE) debian-package DEB_FILENAME=build/deb/$@

#-------------------------------------------------------------------------------
mock_SRC := \
Expand Down Expand Up @@ -131,6 +166,47 @@ install-mock: $(mock_LIB) $(mock_SO) build/ffi.h
mkdir -p $(DESTDIR)$(PREFIX)/lib/pkgconfig
sed -e 's|@ARG_PREFIX@|$(PREFIX)|g' src/libcmt_mock.pc > $(DESTDIR)$(PREFIX)/lib/pkgconfig/libcmt.pc

#-------------------------------------------------------------------------------
host_SRC := \
src/abi.c \
src/buf.c \
src/keccak.c \
src/merkle.c \
src/rollup.c \
src/util.c \
src/sock.c \
src/io-host.c

host_OBJDIR := build/host
host_OBJ := $(patsubst %.c,$(host_OBJDIR)/%.o,$(host_SRC))
host_LIB := $(host_OBJDIR)/libcmt.a
host_SO := $(host_OBJDIR)/libcmt.so

$(host_OBJ): $(host_OBJDIR)/%.o: %.c
@mkdir -p $(@D)
$(CC) $(CFLAGS) -MT $@ -MMD -MP -MF $(@:.o=.d) -c -o $@ $<

$(host_LIB): $(host_OBJ)
$(AR) rcs $@ $^

$(host_SO): $(host_OBJ)
$(CC) -shared -o $@ $^

host: $(host_LIB) $(host_SO) $(host_OBJDIR)/libcmt-host-sample

install-host: $(host_LIB) $(host_SO) build/ffi.h
mkdir -p $(DESTDIR)$(PREFIX)/lib
cp -f $(host_LIB) $(host_SO) $(DESTDIR)$(PREFIX)/lib
mkdir -p $(DESTDIR)$(PREFIX)/include/libcmt/
cp -f include/libcmt/*.h $(DESTDIR)$(PREFIX)/include/libcmt/
cp -f build/ffi.h $(DESTDIR)$(PREFIX)/include/libcmt/
mkdir -p $(DESTDIR)$(PREFIX)/lib/pkgconfig
sed -e 's|@ARG_PREFIX@|$(PREFIX)|g' src/libcmt_host.pc > $(DESTDIR)$(PREFIX)/lib/pkgconfig/libcmt.pc

$(host_OBJDIR)/libcmt-host-sample: src/libcmt-host-sample.c $(host_LIB)
$(CC) $(CFLAGS) -o $@ $^


#-------------------------------------------------------------------------------
unittests_BINS := \
$(mock_OBJDIR)/abi-multi \
Expand Down Expand Up @@ -259,4 +335,5 @@ distclean: clean

OBJ := $(mock_OBJ) $(libcmt_OBJ) $(examples_OBJ) $(tools_OBJ)

.PHONY: install
-include $(OBJ:%.o=%.d)
9 changes: 9 additions & 0 deletions sys-utils/libcmt/include/libcmt/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#ifndef CMT_IO_H
#define CMT_IO_H
#include "buf.h"
#include "sock.h"
#include <stdint.h>

/** Device */
Expand Down Expand Up @@ -109,10 +110,18 @@ typedef struct {
int gio_seq;
} cmt_io_driver_mock_t;

typedef struct {
cmt_buf_t tx[1];
cmt_buf_t rx[1];
cmt_conn_t sock;
int fds[2]; // to close files when we are done
} cmt_io_driver_host_t;

/** Implementation specific cmio state. */
typedef union cmt_io_driver {
cmt_io_driver_ioctl_t ioctl;
cmt_io_driver_mock_t mock;
cmt_io_driver_host_t host;
} cmt_io_driver_t;

/** yield struct cmt_io_yield */
Expand Down
31 changes: 31 additions & 0 deletions sys-utils/libcmt/include/libcmt/sock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef CMT_SOCK_H
#define CMT_SOCK_H
#include <sys/socket.h>
#include <sys/un.h>

typedef union {
struct sockaddr_storage ss;
struct sockaddr_un un;
} cmt_sock_addr_t;

typedef struct {
cmt_sock_addr_t addr, other;
int fd;
} cmt_conn_t;

const char *cmt_sock_client_get_path(void);
const char *cmt_sock_server_get_path(void);

int cmt_sock_addr_init(struct sockaddr_un *me, size_t length, const char path[]);

int cmt_sock_init(cmt_conn_t *me, const char *self, const char *other);
void cmt_sock_fini(cmt_conn_t *me);

int cmt_sock_send(cmt_conn_t *me, size_t length, const void *data);
int cmt_sock_recv(cmt_conn_t *me, size_t length, void *data);

int cmt_sock_send_fds(cmt_conn_t *me, size_t length, int fds[]);
int cmt_sock_recv_fds(cmt_conn_t *me, size_t length, int fds[]);


#endif /* CMT_SOCK_H */
Loading
Loading