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 by the libc that is included |
| 11 // in WebAssembly programs. |
| 12 // |
| 13 //===----------------------------------------------------------------------===// |
| 14 |
| 15 #include <errno.h> |
| 16 #include <stdio.h> |
| 17 #include <stdlib.h> |
| 18 #include <string.h> |
| 19 #include <unistd.h> |
| 20 |
| 21 extern char WASM_MEMORY[]; |
| 22 |
| 23 void env$$abort() { |
| 24 fprintf(stderr, "Aborting...\n"); |
| 25 abort(); |
| 26 } |
| 27 |
| 28 void env$$_abort() { env$$abort(); } |
| 29 |
| 30 #define UNIMPLEMENTED(f) \ |
| 31 void env$$##f() { \ |
| 32 fprintf(stderr, "Unimplemented: " #f "\n"); \ |
| 33 abort(); \ |
| 34 } |
| 35 |
| 36 UNIMPLEMENTED(sbrk) |
| 37 UNIMPLEMENTED(pthread_cleanup_push) |
| 38 UNIMPLEMENTED(pthread_cleanup_pop) |
| 39 UNIMPLEMENTED(pthread_self) |
| 40 UNIMPLEMENTED(__lock) |
| 41 UNIMPLEMENTED(__unlock) |
| 42 UNIMPLEMENTED(__syscall6) |
| 43 UNIMPLEMENTED(__syscall140) |
| 44 |
| 45 void *wasmPtr(int Index) { |
| 46 // TODO (eholk): get the mask from the WASM file. |
| 47 const int MASK = 0x3fffff; |
| 48 Index &= MASK; |
| 49 |
| 50 return WASM_MEMORY + Index; |
| 51 } |
| 52 |
| 53 extern int __szwasm_main(int, char **); |
| 54 |
| 55 #define WASM_REF(Type, Index) ((Type *)wasmPtr(Index)) |
| 56 #define WASM_DEREF(Type, Index) (*WASM_REF(Type, Index)) |
| 57 |
| 58 int main(int argc, char **argv) { return __szwasm_main(argc, argv); } |
| 59 |
| 60 /// sys_write |
| 61 int env$$__syscall4(int Which, int VarArgs) { |
| 62 int Fd = WASM_DEREF(int, VarArgs + 0 * sizeof(int)); |
| 63 int Buffer = WASM_DEREF(int, VarArgs + 1 * sizeof(int)); |
| 64 int Length = WASM_DEREF(int, VarArgs + 2 * sizeof(int)); |
| 65 |
| 66 return write(Fd, WASM_REF(char *, Buffer), Length); |
| 67 } |
| 68 |
| 69 /// sys_ioctl |
| 70 int env$$__syscall54(int A, int B) { |
| 71 int Fd = WASM_DEREF(int, B + 0 * sizeof(int)); |
| 72 int Op = WASM_DEREF(int, B + 1 * sizeof(int)); |
| 73 int ArgP = WASM_DEREF(int, B + 2 * sizeof(int)); |
| 74 // TODO (eholk): implement sys_ioctl |
| 75 return -ENOTTY; |
| 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 + sizeof(int)); |
| 83 int Iovcnt = WASM_DEREF(int, VarArgs + 2 * sizeof(int)); |
| 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 |