commit b4cef989e63c5a0c1709ed36ec72c5a96009d12f from: Benjamin Stürz date: Wed Feb 21 17:21:55 2024 UTC add more examples commit - e46639b37c8c6d9ceeab59634d61b1eccdef9357 commit + b4cef989e63c5a0c1709ed36ec72c5a96009d12f blob - 3f9f29c92857affc14e718a29487efa11effdaa8 blob + 2a1de2bff811b976859fde1d89b39e4bd0ab4c6a --- .gitignore +++ .gitignore @@ -1,5 +1,6 @@ examples/*.elf src/syscalls.h +src/*.swp src/*.o tools/bin tools/build blob - e9573f14c36a97ee872016ce528b881e7c5e7ed0 blob + 0efcc635f578c04a6e86dc755f45f22c068d40f2 --- Makefile +++ Makefile @@ -13,7 +13,8 @@ OBJ = src/rvemu.o src/ecall.o src/cpu.o src/exec.o T = test PROGS = examples/test.elf \ examples/echo.elf \ - examples/cat.elf + examples/cat.elf \ + examples/hello.elf all: rvemu ${PROGS} @@ -24,6 +25,7 @@ run: rvemu ${PROGS} mkdir -p rootfs/bin cp -f rvemu rootfs/bin cp -f ${PROGS} rootfs/bin + cp -f test.txt rootfs/ ${SUDO} chroot rootfs /bin/rvemu /bin/$T.elf clean: blob - 57cb5e69d3c66c9307ecd7ca4a09c3a67be9208d blob + 0e89aeb97561b48709142d4d8ba5d2e364e0ad71 --- examples/test.c +++ examples/test.c @@ -1,23 +1,20 @@ #include #include +#include #include +#include -volatile int x = 42; - __attribute__((always_inline)) inline static void ebreak (void) { __asm __volatile__ ("ebreak"); } -unsigned mul (unsigned a, unsigned b) -{ - return a * b; -} - int main (int argc, char *argv[]) { - printf ("argc = %d\n", argc); - for (int i = 1; i < argc; ++i) - puts (argv[i]); + if (fork () == 0) { + execl ("/bin/hello.elf", "/bin/hello.elf", NULL); + } else { + puts ("Parent"); + } return 0; } blob - /dev/null blob + 71815ce588e10e29920511b1b3e406dd9f76dcdd (mode 644) --- /dev/null +++ examples/hello.c @@ -0,0 +1,7 @@ +#include + +int main (void) +{ + printf ("Hello World\n"); + return 0; +} blob - 23d31d68cbaa4919d7ea80f36ba6af592227e811 blob + 467153209bc6ecb5dfa15d6898e09de5318e8891 --- src/ecall.c +++ src/ecall.c @@ -159,6 +159,13 @@ static u64 my_brk (u64 new) return brkval; } +static int open_flags (int x) +{ + int o = O_RDONLY; + eprintf ("open_flags(%d) = %d\n", x, o); + return o; +} + #define ptr(T, x) ((T *)(x)) #define str(x) ptr (const char, x) void ecall (void) @@ -175,7 +182,8 @@ void ecall (void) const u64 a4 = cpu_get (14); const u64 a5 = cpu_get (15); const u64 a7 = cpu_get (17); - u64 ret; + int tmp, tmp2; + i64 ret; switch (a7) { case SYS_getcwd: ret = enosys ("getcwd"); @@ -266,7 +274,11 @@ void ecall (void) ret = map (fchown ((int)a0, (uid_t)a1, (gid_t)a2)); break; case SYS_openat: - ret = map (openat ((int)a0, str (a1), (int)a2, (int)a3)); + tmp = (int)a0; + if (tmp == -100) + tmp = AT_FDCWD; + tmp2 = open_flags ((int)a2); + ret = map (openat (tmp, str (a1), tmp2, (int)a3)); break; case SYS_close: ret = map (close ((int)a0)); @@ -923,7 +935,8 @@ void ecall (void) ret = enosys ("kexec_file_load"); break; case SYS_open: - ret = map (open ((const char *)(size_t)a0, (int)a1, (int)a2)); + tmp = open_flags ((int)a1); + ret = map (open ((const char *)(size_t)a0, tmp, (int)a2)); break; case SYS_link: ret = map (link (str (a0), str (a1))); blob - /dev/null blob + 0527e6bd2d76b45e2933183f1b506c7ac49f5872 (mode 644) --- /dev/null +++ test.txt @@ -0,0 +1 @@ +This is a test