-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathece391emulate.c
227 lines (202 loc) · 5.09 KB
/
ece391emulate.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
#include <dirent.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/wait.h>
#include <sys/mman.h>
#include <unistd.h>
#include "ece391support.h"
#include "ece391syscall.h"
#include "ece391sysnum.h"
static uint32_t start_esp;
static int32_t dir_fd = -1;
static DIR* dir = NULL;
/*
* (copied from the real system call support)
*
* Rather than create a case for each number of arguments, we simplify
* and use one macro for up to three arguments; the system calls should
* ignore the other registers, and they're caller-saved anyway.
*/
#define DO_CALL(name,number) \
asm volatile (" \
.GLOBL " #name " ;\
" #name ": ;\
PUSHL %EBX ;\
MOVL $" #number ",%EAX ;\
MOVL 8(%ESP),%EBX ;\
MOVL 12(%ESP),%ECX ;\
MOVL 16(%ESP),%EDX ;\
INT $0x80 ;\
CMP $0xFFFFC000,%EAX ;\
JBE 1f ;\
MOVL $-1,%EAX ;\
1: POPL %EBX ;\
RET \
")
/* these wrappers require no changes */
extern int32_t __ece391_read (int32_t fd, void* buf, int32_t nbytes);
extern int32_t __ece391_write (int32_t fd, const void* buf, int32_t nbytes);
extern int32_t __ece391_close (int32_t fd);
void fake_function () {
DO_CALL(ece391_halt,1 /* SYS_HALT */);
DO_CALL(__ece391_read,3 /* SYS_READ */);
DO_CALL(__ece391_write,4 /* SYS_WRITE */);
DO_CALL(__ece391_close,6 /* SYS_CLOSE */);
/* Call the main() function, then halt with its return value. */
asm volatile (" \n\
.GLOBAL _start \n\
_start: \n\
MOVL %ESP,start_esp \n\
CALL main \n\
PUSHL %EAX \n\
CALL ece391_halt \n\
");
/* end of fake container function */
}
int32_t
ece391_execute (const uint8_t* command)
{
int status;
uint8_t buf[1026];
char* args[1024];
uint8_t* scan;
uint32_t n_arg;
if (1023 < ece391_strlen (command))
return -1;
buf[0] = '.';
buf[1] = '/';
ece391_strcpy (buf + 2, command);
for (scan = buf + 2; '\0' != *scan && ' ' != *scan && '\n' != *scan;
scan++);
args[0] = (char*)buf;
n_arg = 1;
if ('\0' != *scan) {
*scan++ = '\0';
/* parse arguments */
while (1) {
while (' ' == *scan) scan++;
if ('\0' == *scan || '\n' == *scan) {
*scan = '\0';
break;
}
args[n_arg++] = (char*)scan;
while ('\0' != *scan && ' ' != *scan && '\n' != *scan) scan++;
if ('\0' != *scan)
*scan++ = '\0';
}
}
args[n_arg] = NULL;
if (0 == fork ()) {
execv ((char*)buf, args);
kill (getpid (), 9);
}
(void)wait (&status);
if (WIFEXITED (status))
return WEXITSTATUS (status);
if (9 == WTERMSIG (status))
return -1;
return 256;
}
int32_t
ece391_open (const uint8_t* filename)
{
uint32_t rval;
if (0 == ece391_strcmp (filename, (uint8_t*)".")) {
dir = opendir (".");
dir_fd = open ("/dev/null", O_RDONLY);
return dir_fd;
}
asm volatile ("INT $0x80" : "=a" (rval) :
"a" (5), "b" (filename), "c" (O_RDONLY));
if (rval > 0xFFFFC000)
return -1;
return rval;
}
int32_t
ece391_getargs (uint8_t* buf, int32_t nbytes)
{
int32_t argc = *(uint32_t*)start_esp;
uint8_t** argv = (uint8_t**)(start_esp + 4);
int32_t idx, len;
idx = 1;
while (idx < argc) {
len = ece391_strlen (argv[idx]);
if (len > nbytes)
return -1;
ece391_strcpy (buf, argv[idx]);
buf += len;
nbytes -= len;
if (++idx >= argc)
break;
if (nbytes < 1)
return -1;
*buf++ = ' ';
nbytes--;
}
if (nbytes < 1)
return -1;
*buf = '\0';
return 0;
}
int32_t
ece391_vidmap (uint8_t** screen_start)
{
static int mem_fd = -1;
void* mem_image;
if(mem_fd == -1) {
mem_fd = open ("/dev/mem", O_RDWR);
}
if ((mem_image = mmap((void*)0, 1024*1024, PROT_READ | PROT_WRITE,
MAP_SHARED, mem_fd, 0)) == MAP_FAILED) {
perror ("mmap low memory");
return -1;
}
*screen_start = (uint8_t*)(mem_image + 0xb8000);
return 0;
}
int32_t
ece391_read (int32_t fd, void* buf, int32_t nbytes)
{
struct dirent* de;
int32_t copied;
uint8_t* from;
uint8_t* to;
if (NULL == dir || dir_fd != fd)
return __ece391_read (fd, buf, nbytes);
if (NULL == (de = readdir (dir)))
return 0;
to = buf;
from = (uint8_t*)de->d_name;
copied = 0;
while ('\0' != *from) {
*to++ = *from++;
if (++copied == nbytes)
return nbytes;
if (32 == copied)
return 32;
}
while (nbytes > copied && 32 > copied) {
*to++ = '\0';
copied++;
}
return copied;
}
int32_t
ece391_write (int32_t fd, const void* buf, int32_t nbytes)
{
if (NULL == dir || dir_fd != fd)
return __ece391_write (fd, buf, nbytes);
return -1;
}
int32_t
ece391_close (int32_t fd)
{
if (NULL == dir || dir_fd != fd)
return __ece391_close (fd);
(void)closedir (dir);
dir = NULL;
(void)close (dir_fd);
dir_fd = -1;
return 0;
}