Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 //===- subzero/runtime/wasm-runtime.c - Subzero WASM runtime source -------===// | |
| 2 // | |
| 3 // The Subzero Code Generator | |
| 4 // | |
| 5 // This file is distributed under the University of Illinois Open Source | |
| 6 // License. See LICENSE.TXT for details. | |
| 7 // | |
| 8 //===----------------------------------------------------------------------===// | |
| 9 // | |
| 10 // 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..."
| |
| 11 // in WebAssembly programs. | |
| 12 // | |
| 13 //===----------------------------------------------------------------------===// | |
| 14 | |
| 15 #include <stdlib.h> | |
| 16 #include <stdio.h> | |
|
Jim Stichnoth
2016/04/14 20:03:43
sort?
Eric Holk
2016/04/15 15:24:26
Done.
| |
| 17 #include <string.h> | |
| 18 #include <unistd.h> | |
| 19 | |
| 20 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
| |
| 21 | |
| 22 void env$$abort() { | |
| 23 fprintf(stderr, "Aborting...\n"); | |
| 24 abort(); | |
| 25 } | |
| 26 | |
| 27 void env$$_abort() { env$$abort(); } | |
| 28 | |
| 29 #define UNIMPLEMENTED(f) \ | |
| 30 void env$$##f() { \ | |
| 31 fprintf(stderr, "Unimplemented: " #f "\n"); \ | |
| 32 abort(); \ | |
| 33 } | |
| 34 | |
| 35 UNIMPLEMENTED(sbrk) | |
| 36 UNIMPLEMENTED(pthread_cleanup_push) | |
| 37 UNIMPLEMENTED(pthread_cleanup_pop) | |
| 38 UNIMPLEMENTED(pthread_self) | |
| 39 UNIMPLEMENTED(__lock) | |
| 40 UNIMPLEMENTED(__unlock) | |
| 41 UNIMPLEMENTED(__syscall6) | |
| 42 UNIMPLEMENTED(__syscall140) | |
| 43 | |
| 44 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
| |
| 45 // TODO (eholk): get the mask from the WASM file. | |
| 46 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
| |
| 47 Index &= MASK; | |
| 48 | |
| 49 return &WASM_MEMORY + Index; | |
| 50 } | |
| 51 | |
| 52 extern int __szwasm_main(int, char **); | |
| 53 | |
| 54 #define WASM_REF(Type, Index) ((Type *)wasmPtr(Index)) | |
| 55 #define WASM_DEREF(Type, Index) (*WASM_REF(Type, Index)) | |
| 56 | |
| 57 int main(int argc, char **argv) { return __szwasm_main(argc, argv); } | |
| 58 | |
| 59 /// sys_write | |
| 60 int env$$__syscall4(int Which, int VarArgs) { | |
| 61 int Fd = WASM_DEREF(int, VarArgs); | |
| 62 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.
| |
| 63 int Length = WASM_DEREF(int, VarArgs + 8); | |
| 64 | |
| 65 return write(Fd, WASM_REF(char *, Buffer), Length); | |
| 66 } | |
| 67 | |
| 68 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.
| |
| 69 // 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.
| |
| 70 int Fd = WASM_DEREF(int, B); | |
| 71 int Op = WASM_DEREF(int, B + 4); | |
| 72 int ArgP = WASM_DEREF(int, B + 8); | |
| 73 // fprintf(stderr, "Fd=%d, Op=%d, ArgP=%d\n", Fd, Op, ArgP); | |
| 74 // abort(); | |
| 75 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.
| |
| 76 } | |
| 77 | |
| 78 int env$$__syscall146(int Which, int VarArgs) { | |
| 79 fprintf(stderr, "syscall146\n"); | |
| 80 | |
| 81 int Fd = WASM_DEREF(int, VarArgs); | |
| 82 int Iov = WASM_DEREF(int, VarArgs + 4); | |
| 83 int Iovcnt = WASM_DEREF(int, VarArgs + 8); | |
| 84 | |
| 85 fprintf(stderr, " Fd=%d, Iov=%d (%p), Iovcnt=%d\n", Fd, Iov, wasmPtr(Iov), | |
| 86 Iovcnt); | |
| 87 | |
| 88 int Count = 0; | |
| 89 | |
| 90 for (int I = 0; I < Iovcnt; ++I) { | |
| 91 void *Ptr = WASM_REF(void, WASM_DEREF(int, Iov + I * 8)); | |
| 92 int Length = WASM_DEREF(int, Iov + I * 8 + 4); | |
| 93 | |
| 94 fprintf(stderr, " [%d] write(%d, %p, %d) = ", I, Fd, Ptr, Length); | |
| 95 int Curr = write(Fd, Ptr, Length); | |
| 96 | |
| 97 fprintf(stderr, "%d\n", Curr); | |
| 98 | |
| 99 if (Curr < 0) { | |
| 100 return -1; | |
| 101 } | |
| 102 Count += Curr; | |
| 103 } | |
| 104 fprintf(stderr, " Count = %d\n", Count); | |
| 105 return Count; | |
| 106 } | |
| OLD | NEW |