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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
|
/*
* Virtual machine of finsh shell.
*
* COPYRIGHT (C) 2006 - 2013, RT-Thread Development Team
*
* This file is part of RT-Thread (http://www.rt-thread.org)
* Maintainer: bernard.xiong <bernard.xiong at gmail.com>
*
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2010-03-22 Bernard first version
*/
#include <finsh.h>
#include "finsh_vm.h"
#include "finsh_ops.h"
#include "finsh_var.h"
/* stack */
union finsh_value finsh_vm_stack[FINSH_STACK_MAX];
/* text segment */
u_char text_segment[FINSH_TEXT_MAX];
union finsh_value* finsh_sp; /* stack pointer */
u_char* finsh_pc; /* PC */
/* syscall list, for dynamic system call register */
struct finsh_syscall_item* global_syscall_list = NULL;
// #define FINSH_VM_DISASSEMBLE
void finsh_vm_run()
{
u_char op;
/* if you want to disassemble the byte code, please define FINSH_VM_DISASSEMBLE */
#ifdef FINSH_VM_DISASSEMBLE
void finsh_disassemble();
finsh_disassemble();
#endif
/* set sp(stack pointer) to the beginning of stack */
finsh_sp = &finsh_vm_stack[0];
/* set pc to the beginning of text segment */
finsh_pc = &text_segment[0];
while ((finsh_pc - &text_segment[0] >= 0) &&
(finsh_pc - &text_segment[0] < FINSH_TEXT_MAX))
{
/* get op */
op = *finsh_pc++;
/* call op function */
op_table[op]();
}
}
#ifdef RT_USING_HEAP
void finsh_syscall_append(const char* name, syscall_func func)
{
/* create the syscall */
struct finsh_syscall_item* item;
item = (struct finsh_syscall_item*)rt_malloc(sizeof(struct finsh_syscall_item));
if (item != RT_NULL)
{
item->next = NULL;
item->syscall.name = rt_strdup(name);
item->syscall.func = func;
if (global_syscall_list == NULL)
{
global_syscall_list = item;
}
else
{
item->next = global_syscall_list;
global_syscall_list = item;
}
}
}
#endif
#if defined(_MSC_VER) || (defined(__GNUC__) && defined(__x86_64__))
struct finsh_syscall* finsh_syscall_next(struct finsh_syscall* call)
{
unsigned int *ptr;
ptr = (unsigned int*) (call + 1);
while ((*ptr == 0) && ((unsigned int*)ptr < (unsigned int*) _syscall_table_end))
ptr ++;
return (struct finsh_syscall*)ptr;
}
struct finsh_sysvar* finsh_sysvar_next(struct finsh_sysvar* call)
{
unsigned int *ptr;
ptr = (unsigned int*) (call + 1);
while ((*ptr == 0) && ((unsigned int*)ptr < (unsigned int*) _sysvar_table_end))
ptr ++;
return (struct finsh_sysvar*)ptr;
}
#endif
struct finsh_syscall* finsh_syscall_lookup(const char* name)
{
struct finsh_syscall* index;
struct finsh_syscall_item* item;
for (index = _syscall_table_begin; index < _syscall_table_end; FINSH_NEXT_SYSCALL(index))
{
if (strcmp(index->name, name) == 0)
return index;
}
/* find on syscall list */
item = global_syscall_list;
while (item != NULL)
{
if (strncmp(item->syscall.name, name, strlen(name)) == 0)
{
return &(item->syscall);
}
item = item->next;
}
return NULL;
}
#ifdef FINSH_VM_DISASSEMBLE
void finsh_disassemble()
{
u_char *pc, op;
pc = &text_segment[0];
while (*pc != 0)
{
op = *pc;
switch (op)
{
case FINSH_OP_ADD_BYTE:
pc ++;
rt_kprintf("addb\n");
break;
case FINSH_OP_SUB_BYTE:
pc ++;
rt_kprintf("subb\n");
break;
case FINSH_OP_DIV_BYTE:
pc ++;
rt_kprintf("divb\n");
break;
case FINSH_OP_MOD_BYTE:
pc ++;
rt_kprintf("modb\n");
break;
case FINSH_OP_MUL_BYTE:
pc ++;
rt_kprintf("mulb\n");
break;
case FINSH_OP_AND_BYTE:
pc ++;
rt_kprintf("andb\n");
break;
case FINSH_OP_OR_BYTE:
pc ++;
rt_kprintf("orb\n");
break;
case FINSH_OP_XOR_BYTE:
pc ++;
rt_kprintf("xorb\n");
break;
case FINSH_OP_BITWISE_BYTE:
pc ++;
rt_kprintf("bwb\n");
break;
case FINSH_OP_SHL_BYTE:
pc ++;
rt_kprintf("shlb\n");
break;
case FINSH_OP_SHR_BYTE:
pc ++;
rt_kprintf("shrb\n");
break;
case FINSH_OP_LD_BYTE:
pc ++;
rt_kprintf("ldb %d\n", *pc++);
break;
case FINSH_OP_LD_VALUE_BYTE:
pc ++;
rt_kprintf("ldb [0x%x]\n", FINSH_GET32(pc));
pc += 4;
break;
case FINSH_OP_ST_BYTE:
pc ++;
rt_kprintf("stb\n");
break;
case FINSH_OP_ADD_WORD:
pc ++;
rt_kprintf("addw\n");
break;
case FINSH_OP_SUB_WORD:
pc ++;
rt_kprintf("subw\n");
break;
case FINSH_OP_DIV_WORD:
pc ++;
rt_kprintf("divw\n");
break;
case FINSH_OP_MOD_WORD:
pc ++;
rt_kprintf("modw\n");
break;
case FINSH_OP_MUL_WORD:
pc ++;
rt_kprintf("mulw\n");
break;
case FINSH_OP_AND_WORD:
pc ++;
rt_kprintf("andw\n");
break;
case FINSH_OP_OR_WORD:
pc ++;
rt_kprintf("orw\n");
break;
case FINSH_OP_XOR_WORD:
pc ++;
rt_kprintf("xorw\n");
break;
case FINSH_OP_BITWISE_WORD:
pc ++;
rt_kprintf("bww\n");
break;
case FINSH_OP_SHL_WORD:
pc ++;
rt_kprintf("shlw\n");
break;
case FINSH_OP_SHR_WORD:
pc ++;
rt_kprintf("shrw\n");
break;
case FINSH_OP_LD_WORD:
pc ++;
rt_kprintf("ldw %d\n", FINSH_GET16(pc));
pc += 2;
break;
case FINSH_OP_LD_VALUE_WORD:
pc ++;
rt_kprintf("ldw [0x%x]\n", FINSH_GET32(pc));
pc += 4;
break;
case FINSH_OP_ST_WORD:
pc ++;
rt_kprintf("stw\n");
break;
case FINSH_OP_ADD_DWORD:
pc ++;
rt_kprintf("addd\n");
break;
case FINSH_OP_SUB_DWORD:
pc ++;
rt_kprintf("subd\n");
break;
case FINSH_OP_DIV_DWORD:
pc ++;
rt_kprintf("divd\n");
break;
case FINSH_OP_MOD_DWORD:
pc ++;
rt_kprintf("modd\n");
break;
case FINSH_OP_MUL_DWORD:
pc ++;
rt_kprintf("muld\n");
break;
case FINSH_OP_AND_DWORD:
pc ++;
rt_kprintf("andd\n");
break;
case FINSH_OP_OR_DWORD:
pc ++;
rt_kprintf("ord\n");
break;
case FINSH_OP_XOR_DWORD:
pc ++;
rt_kprintf("xord\n");
break;
case FINSH_OP_BITWISE_DWORD:
pc ++;
rt_kprintf("bwd\n");
break;
case FINSH_OP_SHL_DWORD:
pc ++;
rt_kprintf("shld\n");
break;
case FINSH_OP_SHR_DWORD:
pc ++;
rt_kprintf("shrd\n");
break;
case FINSH_OP_LD_DWORD:
pc ++;
rt_kprintf("ldd 0x%x\n", FINSH_GET32(pc));
pc += 4;
break;
case FINSH_OP_LD_VALUE_DWORD:
pc ++;
rt_kprintf("ldd [0x%x]\n", FINSH_GET32(pc));
pc += 4;
break;
case FINSH_OP_ST_DWORD:
pc ++;
rt_kprintf("std\n");
break;
case FINSH_OP_POP:
rt_kprintf("pop\n");
pc ++;
break;
case FINSH_OP_SYSCALL:
pc ++;
rt_kprintf("syscall %d\n", *pc++);
break;
case FINSH_OP_LD_VALUE_BYTE_STACK:
pc ++;
rt_kprintf("ldb [sp]\n");
break;
case FINSH_OP_LD_VALUE_WORD_STACK:
pc ++;
rt_kprintf("ldw [sp]\n");
break;
case FINSH_OP_LD_VALUE_DWORD_STACK:
pc ++;
rt_kprintf("ldd [sp]\n");
break;
default:
return;
}
}
}
#endif
|