-
Notifications
You must be signed in to change notification settings - Fork 2
/
able.c
241 lines (219 loc) · 4.11 KB
/
able.c
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
#include <stdatomic.h>
#include <pthread.h>
#include <able/able.h>
#include <able/misc/misc.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <err.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <string.h>
#include <stdio.h>
#include <signal.h>
#include "trap.h"
#include "host.h"
#include "term.h"
extern char *__progname;
void
usage() {
fprintf(stderr, "usage: %s [-h] [-d size] [-c size] [-r size] [-m size] [-b base] [file]\n", __progname);
exit(1);
}
long
eatoi(const char *s) {
long v;
char *ep;
v = strtol(s, &ep, 10);
switch (ep[0]) {
case 'G':
v *= 1024;
case 'M':
v *= 1024;
case 'K':
case 'B':
v *= 1024;
}
return v;
}
int
main(int argc, char *argv[]) {
bool dopt;
dopt = false;
uint16_t doptarg;
doptarg = 32;
bool copt;
copt = false;
uint16_t coptarg;
coptarg = 32;
bool ropt;
ropt = false;
uint8_t roptarg;
roptarg = 32;
bool mopt;
mopt = false;
uint64_t moptarg;
moptarg = 0;
bool bopt;
bopt = false;
uint64_t boptarg;
boptarg = 0;
char c;
while ((c = getopt(argc, argv, "hd:c:r:m:b:")) != -1) {
switch (c) {
case 'd':
if (dopt)
usage();
dopt = true;
doptarg = eatoi(optarg);
if (doptarg > UINT16_MAX)
usage();
break;
case 'c':
if (copt)
usage();
copt = true;
coptarg = eatoi(optarg);
if (coptarg > UINT16_MAX)
usage();
break;
case 'r':
if (ropt)
usage();
ropt = true;
roptarg = eatoi(optarg);
if (roptarg > UINT8_MAX)
usage();
break;
case 'm':
if (mopt)
usage();
mopt = true;
moptarg = eatoi(optarg);
break;
case 'b':
if (bopt)
usage();
bopt = true;
boptarg = eatoi(optarg);
break;
case 'h':
default:
usage();
}
}
argc -= optind;
argv += optind;
char *ifn;
switch (argc) {
case 0:
asprintf(&ifn, "%s.img", __progname);
break;
case 1:
ifn = argv[0];
break;
default:
usage();
}
if (moptarg == 0) {
struct stat ifs;
int y;
y = stat(ifn, &ifs);
if (y == -1)
err(3, "%s", ifn);
moptarg = ifs.st_size;
}
if (boptarg > moptarg)
usage();
int ifd;
ifd = open(ifn, O_RDWR);
if (ifd == -1)
err(3, "open");
uint8_t *m;
m = mmap(NULL, moptarg, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
if (m == MAP_FAILED)
err(3, "mmap");
close(ifd);
// virtual machine config
// terminal input
term_recv_t i0;
memset(&i0, 0, sizeof(i0));
able_task_t ti0;
memset(&ti0, 0, sizeof(ti0));
ti0.ef = (able_task_exec_t)term_recv_exec;
ti0.t = &i0;
// terminal output
void *o0s;
o0s = malloc(1024);
if (o0s == NULL)
err(4, "malloc");
term_send_t o0;
memset(&o0, 0, sizeof(o0));
able_node_init(&o0.n);
o0.s = o0s;
o0.sc = 1024;
able_task_t to0;
memset(&to0, 0, sizeof(to0));
to0.ef = (able_task_exec_t)term_send_exec;
to0.t = &o0;
// host 0
able_node_t h0n;
able_node_init(&h0n);
able_edge_t h0e[256];
memset(h0e, 0, sizeof(h0e));
able_misc_host_buff_t h0b[256];
memset(h0b, 0, sizeof(h0b));
able_link_t h0l_[256];
memset(h0l_, 0, sizeof(h0l_));
void *h0l[256];
for (int i = 0; i < 256; i++)
h0l[i] = &h0l_[i];
int64_t *h0d;
h0d = calloc(sizeof(int64_t), doptarg);
if (h0d == NULL)
err(4, "calloc");
uint64_t *h0c;
h0c = calloc(sizeof(uint64_t), coptarg);
if (h0c == NULL)
err(4, "calloc");
uint64_t *h0r;
h0r = calloc(sizeof(uint64_t), roptarg);
if (h0r == NULL)
err(4, "calloc");
able_misc_host_t h0;
memset(&h0, 0, sizeof(h0));
h0.n = &h0n;
h0.e = h0e;
h0.ec = 256;
h0.b = h0b;
h0.bc = 256;
h0.l = h0l;
h0.lc = 256;
h0.c.m = m + boptarg;
h0.c.mc = moptarg - boptarg;
h0.c.d = h0d;
h0.c.dc = doptarg;
h0.c.c = h0c;
h0.c.cc = coptarg;
h0.c.r = h0r;
h0.c.rc = roptarg;
h0.ts = 1000;
host_init(&h0);
able_task_t th0;
memset(&th0, 0, sizeof(th0));
th0.ef = (able_task_exec_t)host_exec;
th0.t = &h0;
trap_data.u = &h0;
signal(SIGINT, trap);
// virtual network config
// host 0
able_link_join(&i0.l, &h0.e[0], 1, h0.n);
able_link_join(h0.l[0], &o0.e, 0, &o0.n);
able_link_join(&o0.l, &h0.e[0], 0, h0.n);
able_task_fork_exec(&ti0);
able_task_fork_exec(&to0);
able_task_exec(&th0);
// should not happen
return 2;
}