commit c2b4813c74b08dda75ed57b11761d7a362bb9404 from: Benjamin Stürz date: Sat Feb 24 09:27:04 2024 UTC make fopen() work commit - b4cef989e63c5a0c1709ed36ec72c5a96009d12f commit + c2b4813c74b08dda75ed57b11761d7a362bb9404 blob - 2a1de2bff811b976859fde1d89b39e4bd0ab4c6a blob + f651b1c7922c5b6b13376d3fefaa03ed2ed1405f --- .gitignore +++ .gitignore @@ -1,6 +1,5 @@ examples/*.elf src/syscalls.h -src/*.swp src/*.o tools/bin tools/build @@ -11,6 +10,7 @@ tools/riscv64-unknown-linux-musl tools/share tools/src microcoreutils +*/*.swp *.swp *.core *.pdf blob - 0efcc635f578c04a6e86dc755f45f22c068d40f2 blob + 8bd6abd55a0641087d858cbd60caa51d2a4d27e1 --- Makefile +++ Makefile @@ -51,4 +51,4 @@ src/syscalls.h: src/syscalls.inc .c.elf: - ${CROSS}-gcc -o $@ $< -O2 + ${CROSS}-gcc -g -o $@ $< -O2 blob - 0e89aeb97561b48709142d4d8ba5d2e364e0ad71 blob + 461d04c28c839ed4785c5de2636e20ba22fea3b6 --- examples/test.c +++ examples/test.c @@ -11,10 +11,20 @@ inline static void ebreak (void) } int main (int argc, char *argv[]) { - if (fork () == 0) { - execl ("/bin/hello.elf", "/bin/hello.elf", NULL); - } else { - puts ("Parent"); - } + FILE *file; + char buf[256]; + + file = fopen ("test.txt", "r"); + if (file == NULL) + err (1, "fopen()"); + + if (fgets (buf, sizeof (buf), file) == NULL) + err (1, "fgets()"); + + if (puts (buf) < 0) + err (1, "puts()"); + + if (fclose (file) != 0) + err (1, "fclose()"); return 0; } blob - 467153209bc6ecb5dfa15d6898e09de5318e8891 blob + 2a2be702a6b8af0ce87e0001d697567fcf2681dc --- src/ecall.c +++ src/ecall.c @@ -163,6 +163,31 @@ static int open_flags (int x) { int o = O_RDONLY; eprintf ("open_flags(%d) = %d\n", x, o); + return o; +} + +static int mmap_flags (int x) +{ + int o = 0; + if (x & 0x01) + o |= MAP_SHARED; + if (x & 0x02) + o |= MAP_PRIVATE; + if (x & 0x10) + o |= MAP_FIXED; + if (x & 0x20) + o |= MAP_ANON; + + return o; +} + +static int mmap_prot (int x) +{ + int o = 0; + if (x & 0x05) + o |= PROT_READ; + if (x & 0x02) + o |= PROT_WRITE; return o; } @@ -278,6 +303,7 @@ void ecall (void) if (tmp == -100) tmp = AT_FDCWD; tmp2 = open_flags ((int)a2); + eprintf ("openat(%d, %s, %d, %d);\n", tmp, str (a1), tmp2, (int)a3); ret = map (openat (tmp, str (a1), tmp2, (int)a3)); break; case SYS_close: @@ -740,8 +766,10 @@ void ecall (void) ret = map (my_execve (str (a0), ptr (char *, a1), ptr (char *, a2))); break; case SYS_mmap: - //eprintf ("mmap (%p, %zu, %d, %d, %d, %lld);\n", ptr (void, a0), (size_t)a1, (int)a2, (int)a3, (int)a4, (off_t)a5); - ptr = mmap (ptr (void, a0), (size_t)a1, (int)a2, (int)a3, (int)a4, (off_t)a5); + tmp = mmap_flags ((int)a3); + tmp2 = mmap_prot ((int)a2); + eprintf ("mmap (%p, %zu, %d, %d, %d, %lld);\n", ptr (void, a0), (size_t)a1, tmp2, tmp, (int)a4, (off_t)a5); + ptr = mmap (ptr (void, a0), (size_t)a1, tmp2, tmp, (int)a4, (off_t)a5); if (ptr == NULL) { ret = -map_errno (errno); } else {