Skip to content

Commit

Permalink
feat: convert yield to use the driver library
Browse files Browse the repository at this point in the history
  • Loading branch information
mpolitzer committed Oct 11, 2023
1 parent 4dcb5eb commit e848edd
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 144 deletions.
5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,16 @@ RUN cd ${BUILD_BASE}twuewand/rndaddentropy/ && \
# copy tools
COPY linux/ ${BUILD_BASE}tools/linux/

# build C/C++ libs
# ------------------------------------------------------------------------------
RUN make -C ${BUILD_BASE}tools/linux/libdriver/ CROSS_COMPILE=""

# 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
RUN make -C ${BUILD_BASE}tools/linux/rollup/libdriver/ CROSS_COMPILE="" rollup.toolchain

# build rust tools
# ------------------------------------------------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ MACHINE_EMULATOR_TOOLS_TAR_GZ := machine-emulator-tools-$(MACHINE_EMULATOR_TOOL
MACHINE_EMULATOR_TOOLS_DEB := machine-emulator-tools-$(MACHINE_EMULATOR_TOOLS_VERSION).deb
MACHINE_EMULATOR_TOOLS_IMAGE := cartesi/machine-emulator-tools:$(MACHINE_EMULATOR_TOOLS_VERSION)

LINUX_SOURCES_VERSION ?= 5.15.63-ctsi-2
LINUX_SOURCES_FILEPATH := dep/linux-$(LINUX_SOURCES_VERSION).tar.gz
LINUX_SOURCES_URLPATH := https://github.com/cartesi/linux/archive/refs/tags/v$(LINUX_SOURCES_VERSION).tar.gz
LINUX_SOURCES_VERSION ?= rollup-rework
LINUX_SOURCES_FILEPATH := dep/$(LINUX_SOURCES_VERSION).tar.gz
LINUX_SOURCES_URLPATH := https://github.com/cartesi/linux/archive/refs/heads/feature/$(LINUX_SOURCES_VERSION).tar.gz

RNDADDENTROPY_VERSION ?= 3.0.0
RNDADDENTROPY_FILEPATH := dep/twuewand-$(RNDADDENTROPY_VERSION).tar.gz
Expand Down
5 changes: 3 additions & 2 deletions linux/htif/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ RVCXX = $(CROSS_COMPILE)g++
RVCOPY = $(CROSS_COMPILE)objcopy
RVDUMP = $(CROSS_COMPILE)objdump
STRIP = $(CROSS_COMPILE)strip
RISCV_CFLAGS :=-march=$(RISCV_ARCH) -mabi=$(RISCV_ABI)
CFLAGS := -O2 -Wall -pedantic -march=$(RISCV_ARCH) -mabi=$(RISCV_ABI) -I../libdriver/ioctl
LDLIBS := -L../libdriver -ldriver

CONTAINER_MAKE := /usr/bin/make
CONTAINER_BASE := /opt/cartesi/tools
Expand All @@ -42,7 +43,7 @@ extra.ext2: yield
$(MAKE) toolchain-exec CONTAINER_COMMAND="$(CONTAINER_MAKE) $@.toolchain"

yield.toolchain:
$(RVCC) -O2 -o yield yield.c
$(RVCC) $(CFLAGS) -o yield yield.c $(LDLIBS)
$(STRIP) yield

extra.ext2.toolchain:
Expand Down
152 changes: 14 additions & 138 deletions linux/htif/yield.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,161 +14,37 @@
* limitations under the License.
*/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <inttypes.h>
#include <fcntl.h>
#include "sys/ioctl.h"

#include <linux/types.h>
#include <linux/cartesi/yield.h>

#define YIELD_DEVICE_NAME "/dev/yield"
#include <yield.h>

static void help(const char *progname) {
fprintf(stderr,
"Usage: %s <mode> <reason> [<data>]\n"
"Usage: %s <progress>\n"
"Where: \n"
" <mode> \"manual\" or \"automatic\"\n"
" <reason> \"progress\", \"rx-accepted\", \"rx-rejected\",\n"
" \"tx-voucher\", \"tx-notice\", \"tx-exception\" or\n"
" \"tx-report\"\n"
" <data> 32-bit unsigned integer (decimal, default 0)\n",
" <progress> floating point value in the range [0..100]\n",
progname);

exit(1);
}

int send_request(struct yield_request* request) {
int fd, res;

fd = open(YIELD_DEVICE_NAME, O_RDWR);
if (fd < 0) {
fprintf(stderr, "Error opening device " YIELD_DEVICE_NAME "\n");
return fd;
}

res = ioctl(fd, IOCTL_YIELD, (unsigned long)request);

close(fd);
if (res != 0) {
fprintf(stderr, "Device returned error %d\n", res);
return res;
}
return 0;
}

struct name_value {
const char *name;
uint64_t value;
};
static int find_value(const struct name_value *pairs, size_t n, const char *s, uint64_t *value)
{
for (size_t i=0; i<n; ++i) {
if (strcmp(s, pairs[i].name) == 0) {
*value = pairs[i].value;
return 1;
}
}
return 0;
}

int get_mode(const char *s, uint64_t *mode) {
struct name_value modes[] = {
{"manual", HTIF_YIELD_MANUAL},
{"automatic", HTIF_YIELD_AUTOMATIC},
};
return find_value(modes, sizeof(modes)/sizeof(*modes), s, mode);
}

int get_reason(const char *s, uint64_t *reason) {
struct name_value reasons[] = {
{"progress", HTIF_YIELD_REASON_PROGRESS},
{"rx-accepted", HTIF_YIELD_REASON_RX_ACCEPTED},
{"rx-rejected", HTIF_YIELD_REASON_RX_REJECTED},
{"tx-exception", HTIF_YIELD_REASON_TX_EXCEPTION},
{"tx-notice", HTIF_YIELD_REASON_TX_NOTICE},
{"tx-report", HTIF_YIELD_REASON_TX_REPORT},
{"tx-voucher", HTIF_YIELD_REASON_TX_VOUCHER},
};
return find_value(reasons, sizeof(reasons)/sizeof(*reasons), s, reason);
}

int get_data(const char *s, uint64_t *data) {
int end = 0;
if (sscanf(s, "%" SCNu64 "%n", data, &end) != 1 ||
s[end] != 0 ||
*data > UINT32_MAX) {
return 0;
}
return 1;
}

struct parsed_args {
uint64_t mode;
uint64_t reason;
uint64_t data;
};

void parse_args(int argc, char *argv[], struct parsed_args *p) {
int i = 0;
const char *progname = argv[0];

memset(p, 0, sizeof(*p));

i = 1;
if (i >= argc) {
fprintf(stderr, "Too few arguments.\n\n");
help(progname);
}

if (!get_mode(argv[i], &p->mode)) {
fprintf(stderr, "Invalid <mode> argument.\n\n");
help(progname);
}

i++;
if (i >= argc) {
help(progname);
}

if (!get_reason(argv[i], &p->reason)) {
fprintf(stderr, "Invalid <reason> argument.\n\n");
help(progname);
}

i++;
if (i < argc) {
if (get_data(argv[i], &p->data)) {
i++;
} else {
fprintf(stderr, "Invalid <data> argument.\n\n");
help(progname);
}
}
int main(int argc, char *argv[]) {
struct yield_driver Y[1];

if (i != argc) {
fprintf(stderr, "Too many arguments.\n\n");
help(progname);
for (int i=1; i<argc; ++i) {
if (strcmp(argv[i], "--help") == 0)
help(argv[0]);
}
}

int main(int argc, char *argv[]) {
struct yield_request request;
int res = 0;
struct parsed_args args;
if (yield_driver_init(Y))
return EXIT_FAILURE;

parse_args(argc, argv, &args);
const char *s = argc < 2? "100.0" : argv[1];
float progress = strtof(s, NULL);

request.dev = HTIF_DEVICE_YIELD;
request.cmd = args.mode;
request.reason = args.reason;
request.data = args.data;
if ((res = send_request(&request)))
return res;
if (yield_driver_progress(Y, progress))
return EXIT_FAILURE;

return 0;
}

0 comments on commit e848edd

Please sign in to comment.