-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathwebcm.cpp
More file actions
359 lines (318 loc) · 11.6 KB
/
webcm.cpp
File metadata and controls
359 lines (318 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cstdlib>
#include <cstdint>
#include <cstddef>
#include <string_view>
#include <ctime>
#include <unordered_map>
#include <vector>
#include <string>
#include <sstream>
#include <memory>
#include "cartesi-machine/machine-c-api.h"
#include <emscripten.h>
#include <emscripten/fetch.h>
#define MINIZ_NO_ARCHIVE_APIS
#define MINIZ_NO_STDIO
#define MINIZ_NO_TIME
#define MINIZ_EXPORT static
#include "third-party/miniz.h"
#include "third-party/miniz.c"
#define RAM_SIZE (UINT64_C(256)*1024*1024)
#define ROOTFS_SIZE (UINT64_C(384)*1024*1024)
#define RAM_START UINT64_C(0x80000000)
#define ROOTFS_START UINT64_C(0x80000000000000)
extern "C" {
static uint8_t linux_bin_zz[] = {
#embed "linux.bin.zz"
};
static uint8_t rootfs_ext2_zz[] = {
#embed "rootfs.ext2.zz"
};
}
typedef struct uncompress_env {
cm_machine *machine;
uint64_t offset;
} uncompress_env;
int uncompress_cb(uint8_t *data, int size, uncompress_env *env) {
if (cm_write_memory(env->machine, env->offset, data, size) != CM_ERROR_OK) {
printf("failed to write machine memory: %s\n", cm_get_last_error_message());
exit(1);
}
env->offset += size;
return 1;
}
uint64_t uncompress_memory(cm_machine *machine, uint64_t paddr, uint8_t *data, uint64_t size) {
uncompress_env env = {machine, paddr};
size_t uncompressed_size = size;
if (tinfl_decompress_mem_to_callback(data, &uncompressed_size, (tinfl_put_buf_func_ptr)uncompress_cb, &env, TINFL_FLAG_PARSE_ZLIB_HEADER) != 1) {
printf("failed to uncompress memory\n");
exit(1);
}
return uncompressed_size;
}
enum class yield_type : uint64_t {
INVALID = 0,
REQUEST,
POLL_RESPONSE,
POLL_RESPONSE_BODY,
};
struct yield_mmio_req final {
uint64_t headers_count{0};
uint64_t body_vaddr{0};
uint64_t body_length{0};
char url[4096]{};
char method[32]{};
char headers[64][2][256]{};
};
struct yield_mmio_res final {
uint64_t ready_state{0};
uint64_t status{0};
uint64_t body_total_length{0};
uint64_t headers_count{0};
char headers[64][2][256]{};
};
struct fetch_object final {
uint64_t uid{0};
emscripten_fetch_t *fetch{nullptr};
std::string body;
bool done{false};
};
static std::unordered_map<uint64_t, std::unique_ptr<fetch_object>> fetches;
template <size_t N>
static void strsvcopy(char (&dest)[N], std::string_view sv) {
memcpy(dest, sv.data(), std::min(sv.length(), N));
dest[std::min(sv.length(), N - 1)] = 0;
}
static void on_fetch_success(emscripten_fetch_t *fetch) {
fetch_object *o = reinterpret_cast<fetch_object*>(fetch->userData);
o->done = true;
}
static void on_fetch_error(emscripten_fetch_t *fetch) {
fetch_object *o = reinterpret_cast<fetch_object*>(fetch->userData);
o->done = true;
}
bool handle_softyield(cm_machine *machine) {
uint64_t type = 0;
uint64_t uid = 0;
uint64_t vaddr = 0;
cm_read_reg(machine, CM_REG_X10, &type); // a0
cm_read_reg(machine, CM_REG_X11, &uid); // a1
cm_read_reg(machine, CM_REG_X12, &vaddr); // a2
switch (static_cast<yield_type>(type)) {
case yield_type::REQUEST: {
// Read request data
yield_mmio_req mmio_req;
if (cm_read_virtual_memory(machine, vaddr, (uint8_t*)(&mmio_req), sizeof(mmio_req)) != 0) {
printf("failed to read virtual memory: %s\n", cm_get_last_error_message());
return false;
}
if (fetches.find(uid) != fetches.end()) {
return true;
}
// Set headers
std::vector<const char*> headers;
for (uint64_t i = 0; i < mmio_req.headers_count; i++) {
headers.push_back(mmio_req.headers[i][0]);
headers.push_back(mmio_req.headers[i][1]);
}
headers.push_back(nullptr);
// Set fetch attributes
auto o = std::make_unique<fetch_object>();
emscripten_fetch_attr_t attr;
emscripten_fetch_attr_init(&attr);
attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY;
attr.timeoutMSecs = 0;
attr.requestHeaders = headers.data();
attr.onsuccess = on_fetch_success;
attr.onerror = on_fetch_error;
attr.userData = reinterpret_cast<void*>(o.get());
if (mmio_req.body_length > 0) {
o->body.resize(mmio_req.body_length);
// Write attr.requestData by reading mmio_req.body_vaddr from machine memory
if (cm_read_virtual_memory(machine, mmio_req.body_vaddr, reinterpret_cast<uint8_t*>(o->body.data()), mmio_req.body_length) != 0) {
printf("failed to read virtual memory: %s\n", cm_get_last_error_message());
return false;
}
attr.requestData = reinterpret_cast<const char*>(o->body.data());
attr.requestDataSize = o->body.size();
}
strcpy(attr.requestMethod, mmio_req.method);
// Initiate fetch
o->fetch = emscripten_fetch(&attr, mmio_req.url);
fetches[uid] = std::move(o);
break;
}
case yield_type::POLL_RESPONSE: {
// Retrieve fetch
auto it = fetches.find(uid);
if (it == fetches.end()) {
printf("failed to retrieve fetch\n");
return true;
}
auto& o = it->second;
emscripten_fetch_t *fetch = o->fetch;
// Wait fetch to complete
while (!o->done) {
emscripten_sleep(4);
}
// Set response
yield_mmio_res mmio_res;
mmio_res.ready_state = fetch->readyState;
mmio_res.status = fetch->status;
mmio_res.body_total_length = fetch->totalBytes;
// Set response headers
std::string headers_str(emscripten_fetch_get_response_headers_length(fetch) + 1, '\x0');
emscripten_fetch_get_response_headers(fetch, headers_str.data(), headers_str.size());
mmio_res.headers_count = 0;
for (size_t pos = 0; mmio_res.headers_count < 64; ) {
const size_t end = headers_str.find('\n', pos);
if (end == std::string::npos || end == pos) {
break;
}
std::string_view line(headers_str.data() + pos, end - pos);
if (line.back() == '\r') {
line.remove_suffix(1);
}
const auto colon_pos = line.find(": ");
if (colon_pos != std::string_view::npos) {
strsvcopy(mmio_res.headers[mmio_res.headers_count][0], line.substr(0, colon_pos));
strsvcopy(mmio_res.headers[mmio_res.headers_count][1], line.substr(colon_pos + 2));
mmio_res.headers_count++;
}
pos = end + 1;
}
// Write response
if (cm_write_virtual_memory(machine, vaddr, (uint8_t*)(&mmio_res), sizeof(mmio_res)) != 0) {
printf("failed to write virtual memory: %s\n", cm_get_last_error_message());
return false;
}
// Free
if (mmio_res.body_total_length == 0) {
emscripten_fetch_close(fetch);
fetches.erase(it);
}
break;
}
case yield_type::POLL_RESPONSE_BODY: {
// Retrieve fetch
auto it = fetches.find(uid);
if (it == fetches.end()) {
printf("failed to retrieve fetch\n");
return true;
}
auto& o = it->second;
emscripten_fetch_t *fetch = o->fetch;
// Write body
if (cm_write_virtual_memory(machine, vaddr, (uint8_t*)(fetch->data), fetch->totalBytes) != 0) {
printf("failed to write virtual memory: %s\n", cm_get_last_error_message());
return false;
}
// Free
emscripten_fetch_close(fetch);
fetches.erase(it);
break;
}
default:
printf("invalid yield type\n");
return false;
}
// Success
cm_write_reg(machine, CM_REG_X10, 0); // ret a0
return true;
}
int main() {
printf("Allocating...\n");
// Set machine configuration
unsigned long long now = (unsigned long long)time(NULL);
char config[4096];
snprintf(config, sizeof(config), R"({
"dtb": {
"bootargs": "quiet earlycon=sbi console=hvc1 root=/dev/pmem0 rw init=/usr/sbin/cartesi-init",
"init": "date -s @%llu >> /dev/null && https-proxy 127.254.254.254 80 443 > /dev/null 2>&1 &",
"entrypoint": "exec ash -l"
},
"ram": {"length": %llu},
"flash_drive": [
{"length": %llu}
],
"virtio": [
{"type": "console"}
],
"processor": {
"iunrep": 1
}
})", now, static_cast<unsigned long long>(RAM_SIZE), static_cast<unsigned long long>(ROOTFS_SIZE));
const char runtime_config[] = R"({
"soft_yield": true
})";
// Create a new machine
cm_machine *machine = NULL;
if (cm_create_new(config, runtime_config, &machine) != CM_ERROR_OK) {
printf("failed to create machine: %s\n", cm_get_last_error_message());
exit(1);
}
printf("Decompressing...\n");
// Decompress kernel and rootfs
uncompress_memory(machine, RAM_START, linux_bin_zz, sizeof(linux_bin_zz));
uncompress_memory(machine, ROOTFS_START, rootfs_ext2_zz, sizeof(rootfs_ext2_zz));
printf("Booting...\n");
// Run the machine
cm_break_reason break_reason;
do {
uint64_t mcycle;
if (cm_read_reg(machine, CM_REG_MCYCLE, &mcycle) != CM_ERROR_OK) {
printf("failed to read machine cycle: %s\n", cm_get_last_error_message());
cm_delete(machine);
exit(1);
}
if (cm_run(machine, mcycle + 4*1024*1024, &break_reason) != CM_ERROR_OK) {
printf("failed to run machine: %s\n", cm_get_last_error_message());
cm_delete(machine);
exit(1);
}
if (break_reason == CM_BREAK_REASON_YIELDED_SOFTLY) {
if (!handle_softyield(machine)) {
printf("failed to handle soft yield!\n");
cm_delete(machine);
exit(1);
}
}
emscripten_sleep(0);
} while(break_reason == CM_BREAK_REASON_REACHED_TARGET_MCYCLE || break_reason == CM_BREAK_REASON_YIELDED_SOFTLY);
// Print reason for run interruption
switch (break_reason) {
case CM_BREAK_REASON_HALTED:
printf("Halted\n");
break;
case CM_BREAK_REASON_YIELDED_MANUALLY:
printf("Yielded manually\n");
break;
case CM_BREAK_REASON_YIELDED_AUTOMATICALLY:
printf("Yielded automatically\n");
break;
case CM_BREAK_REASON_YIELDED_SOFTLY:
printf("Yielded softly\n");
break;
case CM_BREAK_REASON_REACHED_TARGET_MCYCLE:
printf("Reached target machine cycle\n");
break;
case CM_BREAK_REASON_FAILED:
default:
printf("Interpreter failed\n");
break;
}
// Read and print machine cycles
uint64_t mcycle;
if (cm_read_reg(machine, CM_REG_MCYCLE, &mcycle) != CM_ERROR_OK) {
printf("failed to read machine cycle: %s\n", cm_get_last_error_message());
cm_delete(machine);
exit(1);
}
printf("Cycles: %lu\n", (unsigned long)mcycle);
// Cleanup and exit
cm_delete(machine);
return 0;
}