Index: runtime/wasm-runtime.c |
diff --git a/runtime/wasm-runtime.c b/runtime/wasm-runtime.c |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a0c68d9e0a35fa657e9d323f6cce97145cff4e6c |
--- /dev/null |
+++ b/runtime/wasm-runtime.c |
@@ -0,0 +1,106 @@ |
+//===- subzero/runtime/wasm-runtime.c - Subzero WASM runtime source -------===// |
+// |
+// The Subzero Code Generator |
+// |
+// This file is distributed under the University of Illinois Open Source |
+// License. See LICENSE.TXT for details. |
+// |
+//===----------------------------------------------------------------------===// |
+// |
+// This file implements the system calls required the the libc that is included |
Jim Stichnoth
2016/04/14 20:03:43
the the ==> in the
?
Eric Holk
2016/04/15 15:24:26
I went with "by the..."
|
+// in WebAssembly programs. |
+// |
+//===----------------------------------------------------------------------===// |
+ |
+#include <stdlib.h> |
+#include <stdio.h> |
Jim Stichnoth
2016/04/14 20:03:43
sort?
Eric Holk
2016/04/15 15:24:26
Done.
|
+#include <string.h> |
+#include <unistd.h> |
+ |
+extern char WASM_MEMORY; |
Jim Stichnoth
2016/04/14 20:03:43
I wonder if this could be extern char WASM_MEMORY[
Eric Holk
2016/04/15 15:24:26
Ah, much better. I tried `char *` initially and th
|
+ |
+void env$$abort() { |
+ fprintf(stderr, "Aborting...\n"); |
+ abort(); |
+} |
+ |
+void env$$_abort() { env$$abort(); } |
+ |
+#define UNIMPLEMENTED(f) \ |
+ void env$$##f() { \ |
+ fprintf(stderr, "Unimplemented: " #f "\n"); \ |
+ abort(); \ |
+ } |
+ |
+UNIMPLEMENTED(sbrk) |
+UNIMPLEMENTED(pthread_cleanup_push) |
+UNIMPLEMENTED(pthread_cleanup_pop) |
+UNIMPLEMENTED(pthread_self) |
+UNIMPLEMENTED(__lock) |
+UNIMPLEMENTED(__unlock) |
+UNIMPLEMENTED(__syscall6) |
+UNIMPLEMENTED(__syscall140) |
+ |
+void *wasmPtr(int Index) { |
Jim Stichnoth
2016/04/14 20:03:43
Is Index allowed to be negative? It seems like no
Eric Holk
2016/04/15 15:24:26
It is, and it's actually quite common for it to be
|
+ // TODO (eholk): get the mask from the WASM file. |
+ const int MASK = 0x3fffff; |
Jim Stichnoth
2016/04/14 20:03:43
If I'm reading this right, there are only 22 bits
Eric Holk
2016/04/15 15:24:26
The Wasm file says how many 64KB pages of memory i
|
+ Index &= MASK; |
+ |
+ return &WASM_MEMORY + Index; |
+} |
+ |
+extern int __szwasm_main(int, char **); |
+ |
+#define WASM_REF(Type, Index) ((Type *)wasmPtr(Index)) |
+#define WASM_DEREF(Type, Index) (*WASM_REF(Type, Index)) |
+ |
+int main(int argc, char **argv) { return __szwasm_main(argc, argv); } |
+ |
+/// sys_write |
+int env$$__syscall4(int Which, int VarArgs) { |
+ int Fd = WASM_DEREF(int, VarArgs); |
+ int Buffer = WASM_DEREF(int, VarArgs + 4); |
Jim Stichnoth
2016/04/14 20:03:43
Can you use sizeof() calculations instead of 4/8?
Eric Holk
2016/04/15 15:24:26
Done.
|
+ int Length = WASM_DEREF(int, VarArgs + 8); |
+ |
+ return write(Fd, WASM_REF(char *, Buffer), Length); |
+} |
+ |
+int env$$__syscall54(int A, int B) { |
Jim Stichnoth
2016/04/14 20:03:44
syscall4 was documented as sys_write. What is thi
Eric Holk
2016/04/15 15:24:26
sys_ioctl. I added a comment for it.
|
+ // fprintf(stderr, "Unimplemented: sys_ioctl(%d, %d)\n", a, b); |
Jim Stichnoth
2016/04/14 20:03:44
Remove the commented-out code?
Eric Holk
2016/04/15 15:24:26
Done.
|
+ int Fd = WASM_DEREF(int, B); |
+ int Op = WASM_DEREF(int, B + 4); |
+ int ArgP = WASM_DEREF(int, B + 8); |
+ // fprintf(stderr, "Fd=%d, Op=%d, ArgP=%d\n", Fd, Op, ArgP); |
+ // abort(); |
+ return -25; /* ENOTTY */ |
Jim Stichnoth
2016/04/14 20:03:44
Can you just return -ENOTTY ?
Eric Holk
2016/04/15 15:24:26
Done.
|
+} |
+ |
+int env$$__syscall146(int Which, int VarArgs) { |
+ fprintf(stderr, "syscall146\n"); |
+ |
+ int Fd = WASM_DEREF(int, VarArgs); |
+ int Iov = WASM_DEREF(int, VarArgs + 4); |
+ int Iovcnt = WASM_DEREF(int, VarArgs + 8); |
+ |
+ fprintf(stderr, " Fd=%d, Iov=%d (%p), Iovcnt=%d\n", Fd, Iov, wasmPtr(Iov), |
+ Iovcnt); |
+ |
+ int Count = 0; |
+ |
+ for (int I = 0; I < Iovcnt; ++I) { |
+ void *Ptr = WASM_REF(void, WASM_DEREF(int, Iov + I * 8)); |
+ int Length = WASM_DEREF(int, Iov + I * 8 + 4); |
+ |
+ fprintf(stderr, " [%d] write(%d, %p, %d) = ", I, Fd, Ptr, Length); |
+ int Curr = write(Fd, Ptr, Length); |
+ |
+ fprintf(stderr, "%d\n", Curr); |
+ |
+ if (Curr < 0) { |
+ return -1; |
+ } |
+ Count += Curr; |
+ } |
+ fprintf(stderr, " Count = %d\n", Count); |
+ return Count; |
+} |